CoolFace
Datasetpublic

hschumann2/TempleOS-Source-Code

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes838downloads
AsmResolve.txt219 linesDownload Raw Back to Compiler
1 2U0 AsmResolve(CCmpCtrl *cc,CAsmUnresolvedRef *tmpu,U8 *label,Bool undefined)3{4  CAOTImportExport *tmpie;5  CAsmUndefHash *tmpauh;6  I64 res=Call(tmpu->machine_code);7  if (undefined) {8    tmpauh=tmpu->asm_undef_hash;9    while (tmpauh) {10      if (tmpauh->hash->type & HTF_UNRESOLVED) {11        tmpie=CAlloc(sizeof(CAOTImportExport));12        tmpie->next=tmpauh->hash->ie_lst;13        tmpauh->hash->ie_lst=tmpie;14        tmpie->rip=tmpu->rip;15        tmpie->aot=cc->aot;16        tmpie->type=tmpu->type;17      }18      tmpauh=tmpauh->next; //Technically, more than one won't work.19    }20  } else if (!(tmpu->type&IEF_IMM_NOT_REL)) {21    res-=tmpu->rel_rip;22    if (tmpu->type==IET_REL_I8 && !(I8_MIN<=res<=I8_MAX) ||23          tmpu->type==IET_REL_I16 && !(I16_MIN<=res<=I16_MAX)) {24      PrintErr("Branch out of range at line:%04d %s\n",25            tmpu->line_num,label);26      LexExcept(cc);27    }28    if (tmpu->U8_avail && tmpu->type>IET_IMM_U8 && -124<=res<=123)29      PrintWarn("could use I8 displacement at line:%04d %s %s\n",30            tmpu->line_num,cc->aotc->last_label,label);31  }32  AOTStoreCodeU8At(cc,tmpu->rip,res.u8[0]);33  if (tmpu->type>=IET_REL_I16) {34    AOTStoreCodeU8At(cc,tmpu->rip+1,res.u8[1]);35    if (tmpu->type>=IET_REL_I32) {36      AOTStoreCodeU8At(cc,tmpu->rip+2,res.u8[2],res.u8[3]);37      if (tmpu->type>=IET_REL_I64)38        AOTStoreCodeU8At(cc,tmpu->rip+4,res.u8[4],res.u8[5],39              res.u8[6],res.u8[7]);40    }41  }42}43 44U0 AOTLocalsResolve(CCmpCtrl *cc)45{46  CAOTCtrl *aotc=cc->aotc;47  CAsmUnresolvedRef *tmpu=aotc->local_unresolved,*tmpu1;48  CAsmUndefHash *tmpauh;49  Bool undefined;50  U8 *label=NULL;51 52  while (tmpu) {53    undefined=FALSE;54    tmpu1=tmpu->next;55    tmpauh=tmpu->asm_undef_hash;56    while (tmpauh) {57      if (tmpauh->hash->type & HTF_UNRESOLVED) {58        PrintErr("Undefined sym at line:%04d %s %s\n",59              tmpu->line_num,aotc->last_label,tmpauh->hash->str);60        LexExcept(cc);61      }62      label=tmpauh->hash->str;63      tmpauh=tmpauh->next;64    }65    if (!undefined)66      AsmResolve(cc,tmpu,label,FALSE);67    Free(tmpu->machine_code);68    LinkedLstDel(tmpu->asm_undef_hash);69    Free(tmpu);70    tmpu=tmpu1;71  }72  HashTableDel(cc->htc.local_hash_table);73  cc->htc.hash_table_lst=cc->htc.local_hash_table=HashTableNew(16);74  cc->htc.local_hash_table->next=cc->htc.glbl_hash_table;75  aotc->local_unresolved=NULL;76}77 78U0 AOTGlblsResolve(CCmpCtrl *cc,CAOT *tmpaot)79{80  CAOTCtrl *aotc=cc->aotc;81  CHashFun *tmpf;82  CAsmUnresolvedRef *tmpu=aotc->glbl_unresolved,*tmpu1;83  I64 i,j;84  CAOTImportExport *tmpie,*tmpie1;85  CAsmUndefHash *tmpauh;86  CHashExport *tmpex;87  U8 *label;88  Bool undefined;89  CExternUsage *tmpeu,*tmpeu8;90 91  while (tmpu) {92    label=NULL;93    undefined=FALSE;94    tmpu1=tmpu->next;95    tmpauh=tmpu->asm_undef_hash;96    while (tmpauh) {97      if (tmpauh->hash->type & HTF_UNRESOLVED) {98        tmpex=tmpauh->hash;99        if (tmpex->type & HTT_EXPORT_SYS_SYM&& tmpex->type & HTF_UNRESOLVED &&100              !(tmpex->type & HTF_IMPORT) &&101              (tmpf=HashFind(tmpex->str,cc->htc.hash_table_lst,HTT_FUN)) &&102              !Bt(&tmpf->flags,Cf_EXTERN)) {103          tmpex->val=tmpf->exe_addr;104          tmpex->type&=~HTF_UNRESOLVED;105          label=tmpauh->hash->str;106        } else {107          if (!(tmpex->type & HTF_IMPORT)) {108            if (cc->htc.local_var_lst) {109              tmpex->type|=HTF_GOTO_LABEL;110              tmpex->use_cnt++;111            } else {112              PrintErr("Undefined sym at line:%04d %s\n",113                    tmpu->line_num,tmpex->str);114              LexExcept(cc);115            }116          } else if (undefined) {117            PrintErr("Two imports in same expression "118                  "not allowed at line:%04d %s\n",119                  tmpu->line_num,tmpex->str);120            LexExcept(cc);121          }122          undefined=TRUE;123        }124      } else125        label=tmpauh->hash->str;126      tmpauh=tmpauh->next;127    }128    AsmResolve(cc,tmpu,label,undefined);129    Free(tmpu->machine_code);130    LinkedLstDel(tmpu->asm_undef_hash);131    Free(tmpu);132    tmpu=tmpu1;133  }134 135  for (i=0;i<=cc->htc.glbl_hash_table->mask;i++) {136    tmpex=cc->htc.glbl_hash_table->body[i];137    while (tmpex) {138      if (tmpex->type & (HTF_IMPORT|HTF_GOTO_LABEL)) {139        if (tmpex->use_cnt && (tmpie=tmpex->ie_lst)) {140          if (tmpex->type&HTF_GOTO_LABEL)141            tmpie->flags|=IEF_GOTO_LABEL;142          if (tmpex->import_name)143            tmpie->str=StrNew(tmpex->import_name);144          else145            tmpie->str=StrNew(tmpex->str);146          do {147            tmpie1=tmpie->next;148            QueIns(tmpie,tmpaot->last_ie);149          } while (tmpie=tmpie1);150          tmpex->ie_lst=NULL;151        }152      } else if (tmpex->type & (HTF_EXPORT|HTF_RESOLVE)) {153        if (tmpex->type & HTF_UNRESOLVED) {154          PrintErr("Undefined sym at %s\n",tmpex->str);155          LexExcept(cc);156        }157        if (tmpex->type & HTF_RESOLVE) {158          tmpf=tmpex;159          tmpeu=tmpf->ext_lst;160          while (tmpeu) {161            tmpeu8=tmpeu->next;162            j=tmpf->exe_addr-(tmpeu->rip+4);163            AOTStoreCodeU8At(cc,tmpeu->rip,j.u8[0],j.u8[1],j.u8[2],j.u8[3]);164            Free(tmpeu);165            tmpeu=tmpeu8;166          }167        }168        if (tmpex->type & HTF_EXPORT) {169          tmpie=CAlloc(sizeof(CAOTImportExport));170          tmpie->type=IET_REL32_EXPORT;171          if (tmpex->type & HTT_FUN)172            tmpie->rip=tmpf->exe_addr;173          else if (tmpex->type & HTT_GLBL_VAR)174            tmpie->rip=tmpex(CHashGlblVar *)->data_addr_rip;175          else176            tmpie->rip=tmpex->val;177          tmpie->aot=cc->aot;178          if (tmpex->type & HTF_IMM)179            tmpie->type++;180          tmpie->str=StrNew(tmpex->str);181          tmpie->src_link=StrNew(tmpex->src_link);182          QueIns(tmpie,tmpaot->last_ie);183        }184      }185      tmpex=tmpex->next;186    }187  }188}189 190U0 AsmUnresolvedAdd(CCmpCtrl *cc,U8 *machine_code,I64 type,I64 rip,I64 rel_rip,191        CAsmUndefHash *local_asm_undef_hash,CAsmUndefHash *glbl_asm_undef_hash,192        I64 line_num,Bool U8_avail)193{194  CAsmUnresolvedRef *tmpu=MAlloc(sizeof(CAsmUnresolvedRef));195  tmpu->machine_code=machine_code;196  tmpu->type=type;197  tmpu->rip=rip;198  tmpu->rel_rip=rel_rip;199  tmpu->aot=cc->aot;200  tmpu->imm_flag=FALSE;201  tmpu->line_num=line_num;202  tmpu->U8_avail=U8_avail;203  tmpu->str=NULL;204  if (local_asm_undef_hash) {205    tmpu->asm_undef_hash=local_asm_undef_hash;206    tmpu->next=cc->aotc->local_unresolved;207    cc->aotc->local_unresolved=tmpu;208  } else {209    tmpu->asm_undef_hash=glbl_asm_undef_hash;210    tmpu->next=cc->aotc->glbl_unresolved;211    cc->aotc->glbl_unresolved=tmpu;212    if (glbl_asm_undef_hash->hash->type & HTF_IMPORT) {213      tmpu->str=StrNew(glbl_asm_undef_hash->hash->str);214      if (glbl_asm_undef_hash->hash->type & HTF_IMM)215        tmpu->imm_flag=TRUE;216    }217  }218}219