hlcgcpu.pas 111 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784
  1. {
  2. Copyright (c) 1998-2010 by Florian Klaempfl and Jonas Maebe
  3. Member of the Free Pascal development team
  4. This unit implements the WebAssembly high level code generator
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit hlcgcpu;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. globtype,
  23. aasmbase,aasmdata,aasmcpu,
  24. symbase,symconst,symtype,symdef,symsym,
  25. node,
  26. cpubase, hlcgobj, cgbase, cgutils, parabase, wasmdef;
  27. type
  28. { thlcgwasm }
  29. thlcgwasm = class(thlcgobj)
  30. private
  31. fevalstackheight,
  32. fmaxevalstackheight: longint;
  33. { checks whether the type needs special methodptr-like handling, when stored
  34. in a LOC_REGISTER location. This applies to the following types:
  35. - method pointers
  36. - 8-byte records
  37. - nested proc ptrs
  38. When stored in a LOC_REGISTER tlocation, these types use both register
  39. and registerhi with the following sizes:
  40. register - cgsize = int_cgsize(voidcodepointertype.size)
  41. registerhi - cgsize = int_cgsize(voidpointertype.size) or int_cgsize(parentfpvoidpointertype.size)
  42. (check d.size to determine which one of the two)
  43. }
  44. function is_methodptr_like_type(d:tdef): boolean;
  45. function RefStackPointerSym: TWasmGlobalAsmSymbol;
  46. public
  47. fntypelookup : TWasmProcTypeLookup;
  48. constructor create;
  49. destructor Destroy; override;
  50. procedure incstack(list : TAsmList;slots: longint);
  51. procedure decstack(list : TAsmList;slots: longint);
  52. class function def2regtyp(def: tdef): tregistertype; override;
  53. function getintregister(list:TAsmList;size:tdef):Tregister;override;
  54. function getregisterfordef(list: TAsmList;size:tdef):Tregister;override;
  55. procedure a_load_const_cgpara(list : TAsmList;tosize : tdef;a : tcgint;const cgpara : TCGPara);override;
  56. function a_call_name(list : TAsmList;pd : tprocdef;const s : TSymStr; const paras: array of pcgpara; forceresdef: tdef; weak: boolean): tcgpara;override;
  57. function a_call_reg(list: TAsmList; pd: tabstractprocdef; reg: tregister; const paras: array of pcgpara): tcgpara; override;
  58. { move instructions - a_load_FROM_TO }
  59. procedure a_load_const_reg(list : TAsmList;tosize : tdef;a : tcgint;register : tregister);override;
  60. procedure a_load_const_ref(list : TAsmList;tosize : tdef;a : tcgint;const ref : treference);override;
  61. procedure a_load_reg_ref(list : TAsmList;fromsize, tosize : tdef;register : tregister;const ref : treference);override;
  62. procedure a_load_reg_reg(list : TAsmList;fromsize, tosize : tdef;reg1,reg2 : tregister);override;
  63. procedure a_load_ref_reg(list : TAsmList;fromsize, tosize : tdef;const ref : treference;register : tregister);override;
  64. procedure a_load_ref_ref(list : TAsmList;fromsize, tosize : tdef;const sref : treference;const dref : treference);override;
  65. procedure a_load_loc_ref(list : TAsmList;fromsize, tosize: tdef; const loc: tlocation; const ref : treference);override;
  66. procedure a_loadaddr_ref_reg(list : TAsmList;fromsize, tosize : tdef;const ref : treference;r : tregister);override;
  67. procedure a_load_subsetref_regs_index(list: TAsmList; subsetsize: tdef; loadbitsize: byte; const sref: tsubsetreference; valuereg: tregister); override;
  68. procedure a_load_regconst_subsetref_intern(list : TAsmList; fromsize, subsetsize: tdef; fromreg: tregister; const sref: tsubsetreference; slopt: tsubsetloadopt); override;
  69. { basic arithmetic operations }
  70. procedure a_op_const_reg(list: TAsmList; Op: TOpCG; size: tdef; a: tcgint; reg: TRegister); override;
  71. procedure a_op_const_reg_reg(list: TAsmList; op: TOpCg; size: tdef; a: tcgint; src, dst: tregister); override;
  72. procedure a_op_const_ref(list: TAsmList; Op: TOpCG; size: tdef; a: tcgint; const ref: TReference); override;
  73. procedure a_op_ref_reg(list: TAsmList; Op: TOpCG; size: tdef; const ref: TReference; reg: TRegister); override;
  74. procedure a_op_reg_reg_reg(list: TAsmList; op: TOpCg; size: tdef; src1, src2, dst: tregister); override;
  75. procedure a_op_reg_reg(list: TAsmList; Op: TOpCG; size: tdef; reg1, reg2: TRegister); override;
  76. procedure a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tdef; a: tcgint; src, dst: tregister;setflags : boolean;var ovloc : tlocation); override;
  77. procedure a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tdef; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation); override;
  78. procedure a_cmp_const_ref_label(list: TAsmList; size: tdef; cmp_op: topcmp; a: tcgint; const ref: treference; l: tasmlabel); override;
  79. procedure a_cmp_const_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; a: tcgint; reg: tregister; l: tasmlabel); override;
  80. procedure a_cmp_ref_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; const ref: treference; reg: tregister; l: tasmlabel); override;
  81. procedure a_cmp_reg_ref_label(list: TAsmList; size: tdef; cmp_op: topcmp; reg: tregister; const ref: treference; l: tasmlabel); override;
  82. procedure a_cmp_reg_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; reg1, reg2: tregister; l: tasmlabel); override;
  83. procedure a_jmp_always(list : TAsmList;l: tasmlabel); override;
  84. procedure a_jmp_always_pascal_goto(list : TAsmList;l: tasmlabel); override;
  85. procedure a_loadfpu_ref_ref(list: TAsmList; fromsize, tosize: tdef; const ref1, ref2: treference); override;
  86. procedure a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; reg: tregister); override;
  87. procedure a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tdef; reg: tregister; const ref: treference); override;
  88. procedure a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tdef; reg1, reg2: tregister); override;
  89. procedure g_unreachable(list: TAsmList); override;
  90. procedure g_concatcopy(list : TAsmList;size: tdef; const source,dest : treference); override;
  91. procedure g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean); override;
  92. procedure g_proc_exit(list : TAsmList;parasize:longint;nostackframe:boolean); override;
  93. procedure g_rangecheck(list: TAsmList; const l:tlocation; fromdef,todef: tdef); override;
  94. procedure g_overflowcheck(list: TAsmList; const Loc: tlocation; def: tdef); override;
  95. procedure g_overflowCheck_loc(List:TAsmList;const Loc:TLocation;def:TDef;var ovloc : tlocation); override;
  96. procedure maybe_change_load_node_reg(list: TAsmList; var n: tnode; reload: boolean); override;
  97. procedure gen_entry_code(list: TAsmList); override;
  98. procedure gen_exit_code(list: TAsmList); override;
  99. { unimplemented/unnecessary routines }
  100. procedure a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; srcsize, dstsize: tdef; src, dst: tregister); override;
  101. procedure a_loadmm_loc_reg(list: TAsmList; fromsize, tosize: tdef; const loc: tlocation; const reg: tregister; shuffle: pmmshuffle); override;
  102. procedure a_loadmm_reg_reg(list: TAsmList; fromsize, tosize: tdef; reg1, reg2: tregister; shuffle: pmmshuffle); override;
  103. procedure a_loadmm_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; reg: tregister; shuffle: pmmshuffle); override;
  104. procedure a_loadmm_reg_ref(list: TAsmList; fromsize, tosize: tdef; reg: tregister; const ref: treference; shuffle: pmmshuffle); override;
  105. procedure a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size: tdef; src, dst: tregister; shuffle: pmmshuffle); override;
  106. procedure a_loadmm_intreg_reg(list: TAsmList; fromsize, tosize: tdef; intreg, mmreg: tregister; shuffle: pmmshuffle); override;
  107. procedure a_loadmm_reg_intreg(list: TAsmList; fromsize, tosize: tdef; mmreg, intreg: tregister; shuffle: pmmshuffle); override;
  108. procedure g_stackpointer_alloc(list: TAsmList; size: longint); override;
  109. procedure g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint); override;
  110. procedure g_adjust_self_value(list: TAsmList; procdef: tprocdef; ioffset: aint); override;
  111. procedure g_local_unwind(list: TAsmList; l: TAsmLabel); override;
  112. { Wasm-specific routines }
  113. procedure g_procdef(list:TAsmList;pd: tprocdef);
  114. procedure g_maybe_checkforexceptions(list:TasmList); override;
  115. procedure a_load_stack_reg(list : TAsmList;size: tdef;reg: tregister);
  116. { extra_slots are the slots that are used by the reference, and that
  117. will be removed by the store operation }
  118. procedure a_load_stack_ref(list : TAsmList;size: tdef;const ref: treference;extra_slots: longint);
  119. procedure a_load_reg_stack(list : TAsmList;size: tdef;reg: tregister);
  120. { extra_slots are the slots that are used by the reference, and that
  121. will be removed by the load operation }
  122. procedure a_load_ref_stack(list : TAsmList;size: tdef;const ref: treference;extra_slots: longint);
  123. procedure a_load_const_stack(list : TAsmList;size: tdef;a :tcgint; typ: TRegisterType);
  124. procedure a_load_subsetref_stack(list : TAsmList;size: tdef; const sref: tsubsetreference);
  125. procedure a_loadaddr_ref_stack(list : TAsmList;fromsize, tosize : tdef;const ref : treference);
  126. procedure a_load_stack_loc(list : TAsmList;size: tdef;const loc: tlocation);
  127. procedure a_load_loc_stack(list : TAsmList;size: tdef;const loc: tlocation);
  128. procedure a_loadfpu_const_stack(list : TAsmList;size: tdef;a :double);
  129. procedure a_op_stack(list : TAsmList;op: topcg; size: tdef);
  130. procedure a_op_const_stack(list : TAsmList;op: topcg; size: tdef;a : tcgint);
  131. procedure a_op_reg_stack(list : TAsmList;op: topcg; size: tdef;reg: tregister);
  132. procedure a_op_ref_stack(list : TAsmList;op: topcg; size: tdef;const ref: treference);
  133. procedure a_op_loc_stack(list : TAsmList;op: topcg; size: tdef;const loc: tlocation);
  134. procedure a_cmp_const_loc_stack(list: TAsmList; size: tdef;cmp_op: topcmp; a: tcgint; const loc: tlocation);
  135. procedure a_cmp_const_ref_stack(list: TAsmList; size: tdef; cmp_op: topcmp; a: tcgint; const ref: treference);
  136. procedure a_cmp_const_reg_stack(list: TAsmList; size: tdef; cmp_op: topcmp; a: tcgint; reg: tregister);
  137. procedure a_cmp_ref_reg_stack(list: TAsmList; size: tdef; cmp_op: topcmp; const ref: treference; reg: tregister);
  138. procedure a_cmp_reg_ref_stack(list: TAsmList; size: tdef; cmp_op: topcmp; reg: tregister; const ref: treference);
  139. procedure a_cmp_reg_reg_stack(list: TAsmList; size: tdef; cmp_op: topcmp; reg1, reg2: tregister);
  140. procedure a_cmp_subsetreg_reg_stack(list: TAsmList; fromsubsetsize, cmpsize: tdef; cmp_op: topcmp; const sreg: tsubsetregister; reg: tregister);
  141. procedure a_cmp_subsetref_reg_stack(list: TAsmList; fromsubsetsize, cmpsize: tdef; cmp_op: topcmp; const sref: tsubsetreference; reg: tregister);
  142. procedure a_cmp_loc_reg_stack(list : TAsmList;size : tdef;cmp_op : topcmp; const loc: tlocation; reg : tregister);
  143. procedure a_cmp_reg_loc_stack(list : TAsmList;size : tdef;cmp_op : topcmp; reg: tregister; const loc: tlocation);
  144. procedure a_cmp_ref_loc_stack(list: TAsmList; size: tdef;cmp_op: topcmp; const ref: treference; const loc: tlocation);
  145. procedure a_cmp_const_loc_br(list: TAsmList; size: tdef;cmp_op: topcmp; a: tcgint; const loc: tlocation; br: Integer);
  146. procedure a_cmp_const_ref_br(list: TAsmList; size: tdef; cmp_op: topcmp; a: tcgint; const ref: treference; br: Integer);
  147. procedure a_cmp_const_reg_br(list: TAsmList; size: tdef; cmp_op: topcmp; a: tcgint; reg: tregister; br: Integer);
  148. procedure a_cmp_ref_reg_br(list: TAsmList; size: tdef; cmp_op: topcmp; const ref: treference; reg: tregister; br: Integer);
  149. procedure a_cmp_reg_ref_br(list: TAsmList; size: tdef; cmp_op: topcmp; reg: tregister; const ref: treference; br: Integer);
  150. procedure a_cmp_reg_reg_br(list: TAsmList; size: tdef; cmp_op: topcmp; reg1, reg2: tregister; br: Integer);
  151. procedure a_cmp_subsetreg_reg_br(list: TAsmList; fromsubsetsize, cmpsize: tdef; cmp_op: topcmp; const sreg: tsubsetregister; reg: tregister; br: Integer);
  152. procedure a_cmp_subsetref_reg_br(list: TAsmList; fromsubsetsize, cmpsize: tdef; cmp_op: topcmp; const sref: tsubsetreference; reg: tregister; br: Integer);
  153. procedure a_cmp_loc_reg_br(list : TAsmList;size : tdef;cmp_op : topcmp; const loc: tlocation; reg : tregister; br: Integer);
  154. procedure a_cmp_reg_loc_br(list : TAsmList;size : tdef;cmp_op : topcmp; reg: tregister; const loc: tlocation; br: Integer);
  155. procedure a_cmp_ref_loc_br(list: TAsmList; size: tdef;cmp_op: topcmp; const ref: treference; const loc: tlocation; br: Integer);
  156. procedure g_reference_loc(list: TAsmList; def: tdef; const fromloc: tlocation; out toloc: tlocation); override;
  157. procedure a_cmp_stack_stack(list : TAsmlist; size: tdef; cmp_op: topcmp);
  158. { truncate/sign extend after performing operations on values < 32 bit
  159. that may have overflowed outside the range }
  160. procedure maybe_adjust_op_result(list: TAsmList; op: TOpCg; size: tdef);
  161. { performs sign/zero extension as required }
  162. procedure resize_stack_int_val(list: TAsmList;fromsize,tosize: tdef; formemstore: boolean);
  163. { 8/16 bit unsigned parameters and return values must be sign-extended on
  164. the producer side, because the JVM does not support unsigned variants;
  165. then they have to be zero-extended again on the consumer side }
  166. procedure maybe_resize_stack_para_val(list: TAsmList; retdef: tdef; callside: boolean);
  167. { adjust the stack height after a call based on the specified number of
  168. slots used for parameters and the provided resultdef }
  169. procedure g_adjust_stack_after_call(list: TAsmList; pd: tabstractprocdef);
  170. { because WebAssembly has no spec for any sort of debug info, and the
  171. only linker that we support (LLVM's wasm-ld) does not support creating
  172. map files in its stable version, and crashes when attempting to create
  173. a map file in its development version from git, we have no way to
  174. identify which procedure a crash occurred in. So, to identify the
  175. procedure, we call this procedure on proc entry, which generates a few
  176. useless loads of random numbers on the stack, that are immediately
  177. discarded, so they are essentially equivalent to a nop. This allows
  178. finding the procedure in the FPC output assembly, produced with -al by
  179. searching for these random numbers, as taken from the disassembly of the
  180. final binary. }
  181. procedure g_fingerprint(list: TAsmList);
  182. property maxevalstackheight: longint read fmaxevalstackheight;
  183. protected
  184. procedure gen_load_uninitialized_function_result(list: TAsmList; pd: tprocdef; resdef: tdef; const resloc: tcgpara); override;
  185. function g_call_system_proc_intern(list: TAsmList; pd: tprocdef; const paras: array of pcgpara; forceresdef: tdef): tcgpara; override;
  186. public
  187. { in case of an array, the array base address and index have to be
  188. put on the evaluation stack before the stored value; similarly, for
  189. fields the self pointer has to be loaded first. Also checks whether
  190. the reference is valid. If dup is true, the necessary values are stored
  191. twice. Returns how many stack slots have been consumed, disregarding
  192. the "dup". }
  193. function prepare_stack_for_ref(list: TAsmList; var ref: treference; dup: boolean): longint;
  194. procedure gen_load_cgpara_loc(list: TAsmList; vardef: tdef; const para: TCGPara; var destloc: tlocation; reusepara: boolean);override;
  195. protected
  196. { return the load/store opcode to load/store from/to ref; if the result
  197. has to be and'ed after a load to get the final value, that constant
  198. is returned in finishandval (otherwise that value is set to -1) }
  199. function loadstoreopcref(def: tdef; isload: boolean; const ref: treference; out finishandval: tcgint): tasmop;
  200. procedure resizestackfpuval(list: TAsmList; fromsize, tosize: tcgsize);
  201. end;
  202. implementation
  203. uses
  204. verbose,cutils,globals,fmodule,constexp,
  205. defutil,cpupi,
  206. aasmtai,
  207. symtable,symcpu,
  208. procinfo,cpuinfo,cgobj,cgcpu,tgobj,tgcpu,paramgr;
  209. const
  210. TOpCG2IAsmOp : array[topcg] of TAsmOp=(
  211. A_None, {OP_NONE}
  212. A_None, {OP_MOVE, replaced operation with direct load }
  213. a_i32_add, {OP_ADD, simple addition }
  214. a_i32_and, {OP_AND, simple logical and }
  215. a_i32_div_u, {OP_DIV, simple unsigned division }
  216. a_i32_div_s, {OP_IDIV, simple signed division }
  217. a_i32_mul, {OP_IMUL, simple signed multiply }
  218. a_i32_mul, {OP_MUL, simple unsigned multiply }
  219. A_None, {OP_NEG, simple negate } // neg = xor + 1
  220. A_None, {OP_NOT, simple logical not } // not = xor - 1
  221. a_i32_or, {OP_OR, simple logical or }
  222. a_i32_shr_s, {OP_SAR, arithmetic shift-right }
  223. a_i32_shl, {OP_SHL, logical shift left }
  224. a_i32_shr_u, {OP_SHR, logical shift right }
  225. a_i32_sub, {OP_SUB, simple subtraction }
  226. a_i32_xor, {OP_XOR, simple exclusive or }
  227. a_i32_rotl, {OP_ROL, rotate left }
  228. a_i32_rotr {OP_ROR rotate right }
  229. );
  230. TOpCG2LAsmOp : array[topcg] of TAsmOp=(
  231. A_None, {OP_NONE}
  232. a_i64_load, {OP_MOVE, replaced operation with direct load }
  233. a_i64_add, {OP_ADD, simple addition }
  234. a_i64_and, {OP_AND, simple logical and }
  235. a_i64_div_u, {OP_DIV, simple unsigned division }
  236. a_i64_div_s, {OP_IDIV, simple signed division }
  237. a_i64_mul, {OP_IMUL, simple signed multiply }
  238. a_i64_mul, {OP_MUL, simple unsigned multiply }
  239. A_None, {OP_NEG, simple negate } // neg = xor + 1
  240. A_None, {OP_NOT, simple logical not } // not = xor - 1
  241. a_i64_or, {OP_OR, simple logical or }
  242. a_i64_shr_s, {OP_SAR, arithmetic shift-right }
  243. a_i64_shl, {OP_SHL, logical shift left }
  244. a_i64_shr_u, {OP_SHR, logical shift right }
  245. a_i64_sub, {OP_SUB, simple subtraction }
  246. a_i64_xor, {OP_XOR, simple exclusive or }
  247. a_i64_rotl, {OP_ROL, rotate left }
  248. a_i64_rotr {OP_ROR rotate right }
  249. );
  250. function thlcgwasm.is_methodptr_like_type(d:tdef): boolean;
  251. var
  252. is_8byterecord, is_methodptr, is_nestedprocptr: Boolean;
  253. begin
  254. is_8byterecord:=(d.typ=recorddef) and (d.size=8);
  255. is_methodptr:=(d.typ=procvardef)
  256. and (po_methodpointer in tprocvardef(d).procoptions)
  257. and not(po_addressonly in tprocvardef(d).procoptions);
  258. is_nestedprocptr:=(d.typ=procvardef)
  259. and is_nested_pd(tprocvardef(d))
  260. and not(po_addressonly in tprocvardef(d).procoptions);
  261. result:=is_8byterecord or is_methodptr or is_nestedprocptr;
  262. end;
  263. function thlcgwasm.RefStackPointerSym: TWasmGlobalAsmSymbol;
  264. begin
  265. result:=TWasmGlobalAsmSymbol(current_asmdata.RefAsmSymbolByClass(TWasmGlobalAsmSymbol,STACK_POINTER_SYM,AT_WASM_GLOBAL));
  266. result.WasmGlobalType:=wbt_i32;
  267. end;
  268. constructor thlcgwasm.create;
  269. begin
  270. fevalstackheight:=0;
  271. fmaxevalstackheight:=0;
  272. fntypelookup:=TWasmProcTypeLookup.Create;
  273. end;
  274. destructor thlcgwasm.Destroy;
  275. begin
  276. fntypelookup.Free;
  277. inherited Destroy;
  278. end;
  279. procedure thlcgwasm.incstack(list: TAsmList; slots: longint);
  280. begin
  281. if (fevalstackheight<0) and
  282. not(cs_no_regalloc in current_settings.globalswitches) then
  283. {$ifdef DEBUG_WASMSTACK}
  284. list.concat(tai_comment.Create(strpnew('!!! stack underflow')));
  285. {$else DEBUG_WASMSTACK}
  286. internalerror(2010120501);
  287. {$endif DEBUG_WASMSTACK}
  288. if slots=0 then
  289. exit;
  290. inc(fevalstackheight,slots);
  291. if (fevalstackheight>fmaxevalstackheight) then
  292. fmaxevalstackheight:=fevalstackheight;
  293. if cs_asm_regalloc in current_settings.globalswitches then
  294. list.concat(tai_comment.Create(strpnew(' allocated '+tostr(slots)+', stack height = '+tostr(fevalstackheight))));
  295. end;
  296. procedure thlcgwasm.decstack(list: TAsmList;slots: longint);
  297. begin
  298. if slots=0 then
  299. exit;
  300. dec(fevalstackheight,slots);
  301. if (fevalstackheight<0) and
  302. not(cs_no_regalloc in current_settings.globalswitches) then
  303. {$ifdef DEBUG_WASMSTACK}
  304. list.concat(tai_comment.Create(strpnew('!!! stack underflow')));
  305. {$else DEBUG_WASMSTACK}
  306. internalerror(2010120501);
  307. {$endif DEBUG_WASMSTACK}
  308. if cs_asm_regalloc in current_settings.globalswitches then
  309. list.concat(tai_comment.Create(strpnew(' freed '+tostr(slots)+', stack height = '+tostr(fevalstackheight))));
  310. end;
  311. class function thlcgwasm.def2regtyp(def: tdef): tregistertype;
  312. begin
  313. if is_wasm_externref(def) then
  314. result:=R_EXTERNREFREGISTER
  315. else if is_wasm_funcref(def) then
  316. result:=R_FUNCREFREGISTER
  317. else if (def.typ=recorddef) and (def.size in [4,8]) and (trecorddef(def).contains_float_field) then
  318. result:=R_FPUREGISTER
  319. else
  320. result:=inherited;
  321. end;
  322. function thlcgwasm.getintregister(list:TAsmList;size:tdef):Tregister;
  323. begin
  324. if is_wasm_reference_type(size) then
  325. internalerror(2023060702)
  326. else
  327. result:=inherited;
  328. end;
  329. function thlcgwasm.getregisterfordef(list: TAsmList;size:tdef):Tregister;
  330. begin
  331. case def2regtyp(size) of
  332. R_EXTERNREFREGISTER:
  333. result:=TCgWasm(cg).getexternrefregister(list);
  334. R_FUNCREFREGISTER:
  335. result:=TCgWasm(cg).getfuncrefregister(list);
  336. else
  337. result:=inherited;
  338. end;
  339. end;
  340. procedure thlcgwasm.a_load_const_cgpara(list: TAsmList; tosize: tdef; a: tcgint; const cgpara: TCGPara);
  341. begin
  342. tosize:=get_para_push_size(tosize);
  343. if tosize=s8inttype then
  344. a:=shortint(a)
  345. else if tosize=s16inttype then
  346. a:=smallint(a);
  347. inherited a_load_const_cgpara(list, tosize, a, cgpara);
  348. end;
  349. function thlcgwasm.a_call_name(list: TAsmList; pd: tprocdef; const s: TSymStr; const paras: array of pcgpara; forceresdef: tdef; weak: boolean): tcgpara;
  350. begin
  351. list.concat(taicpu.op_sym_functype(a_call,current_asmdata.RefAsmSymbol(s,AT_FUNCTION),tcpuprocdef(pd).create_functype));
  352. result:=get_call_result_cgpara(pd,forceresdef);
  353. end;
  354. function thlcgwasm.a_call_reg(list: TAsmList; pd: tabstractprocdef; reg: tregister; const paras: array of pcgpara): tcgpara;
  355. begin
  356. a_load_reg_stack(list, ptrsinttype, reg);
  357. if pd.typ=procvardef then
  358. current_asmdata.CurrAsmList.Concat(taicpu.op_functype(a_call_indirect,tcpuprocvardef(pd).create_functype))
  359. else
  360. current_asmdata.CurrAsmList.Concat(taicpu.op_functype(a_call_indirect,tcpuprocdef(pd).create_functype));
  361. decstack(list,1);
  362. result:=hlcg.get_call_result_cgpara(pd, nil);
  363. end;
  364. procedure thlcgwasm.a_load_const_stack(list : TAsmList;size : tdef;a : tcgint; typ: TRegisterType);
  365. begin
  366. case typ of
  367. R_INTREGISTER,
  368. R_ADDRESSREGISTER:
  369. begin
  370. case def_cgsize(size) of
  371. OS_8,OS_16,OS_32,
  372. OS_S8,OS_S16,OS_S32:
  373. begin
  374. { convert cardinals to longints }
  375. list.concat(taicpu.op_const(a_i32_const, a));
  376. end;
  377. OS_64,OS_S64:
  378. begin
  379. list.concat(taicpu.op_const(a_i64_const, a));
  380. end;
  381. else
  382. internalerror(2010110702);
  383. end;
  384. end;
  385. R_EXTERNREFREGISTER:
  386. begin
  387. if a<>0 then
  388. internalerror(2023061101);
  389. list.Concat(taicpu.op_none(a_ref_null_externref));
  390. end;
  391. R_FUNCREFREGISTER:
  392. begin
  393. if a<>0 then
  394. internalerror(2023061102);
  395. list.Concat(taicpu.op_none(a_ref_null_funcref));
  396. end;
  397. else
  398. internalerror(2010110703);
  399. end;
  400. incstack(list,1);
  401. end;
  402. procedure thlcgwasm.a_loadaddr_ref_stack(list : TAsmList;fromsize, tosize : tdef;const ref : treference);
  403. var
  404. tmpref: treference;
  405. begin
  406. { you can't take the address of references, that are on the local stack }
  407. if (ref.base=NR_EVAL_STACK_BASE) or (ref.index=NR_EVAL_STACK_BASE) or
  408. (ref.base=NR_LOCAL_STACK_POINTER_REG) or (ref.index=NR_LOCAL_STACK_POINTER_REG) then
  409. internalerror(2021010101);
  410. tmpref:=ref;
  411. tmpref.base:=NR_NO;
  412. tmpref.index:=NR_NO;
  413. if tmpref.refaddr=addr_got_tls then
  414. begin
  415. tmpref.offset:=0;
  416. list.Concat(taicpu.op_ref(a_global_get, tmpref));
  417. incstack(list, 1);
  418. if ref.offset<>0 then
  419. begin
  420. list.Concat(taicpu.op_const(a_i32_const,ref.offset));
  421. incstack(list, 1);
  422. list.Concat(taicpu.op_none(a_i32_add));
  423. decstack(list, 1);
  424. end;
  425. end
  426. else
  427. begin
  428. list.Concat(taicpu.op_ref(a_i32_const, tmpref));
  429. incstack(list, 1);
  430. end;
  431. if ref.base<>NR_NO then
  432. begin
  433. list.Concat(taicpu.op_reg(a_local_get,ref.base));
  434. incstack(list, 1);
  435. list.Concat(taicpu.op_none(a_i32_add));
  436. decstack(list, 1);
  437. end;
  438. if ref.index<>NR_NO then
  439. begin
  440. list.Concat(taicpu.op_reg(a_local_get,ref.index));
  441. incstack(list, 1);
  442. if ref.scalefactor>1 then
  443. begin
  444. list.Concat(taicpu.op_const(a_i32_const,ref.scalefactor));
  445. incstack(list, 1);
  446. list.Concat(taicpu.op_none(a_i32_mul));
  447. decstack(list, 1);
  448. end;
  449. list.Concat(taicpu.op_none(a_i32_add));
  450. decstack(list, 1);
  451. end;
  452. end;
  453. procedure thlcgwasm.a_load_stack_loc(list: TAsmList; size: tdef; const loc: tlocation);
  454. var
  455. tmpref: treference;
  456. begin
  457. case loc.loc of
  458. LOC_REGISTER,LOC_CREGISTER,
  459. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  460. a_load_stack_reg(list,size,loc.register);
  461. LOC_REFERENCE:
  462. begin
  463. tmpref:=loc.reference;
  464. a_load_stack_ref(list,size,loc.reference,prepare_stack_for_ref(list,tmpref,false));
  465. end;
  466. else
  467. internalerror(2011020501);
  468. end;
  469. end;
  470. procedure thlcgwasm.a_load_loc_stack(list: TAsmList;size: tdef;const loc: tlocation);
  471. var
  472. tmpref: treference;
  473. extra_slots: LongInt;
  474. begin
  475. case loc.loc of
  476. LOC_REGISTER,LOC_CREGISTER,
  477. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  478. a_load_reg_stack(list,size,loc.register);
  479. LOC_REFERENCE,LOC_CREFERENCE:
  480. begin
  481. tmpref:=loc.reference;
  482. extra_slots:=prepare_stack_for_ref(list,tmpref,false);
  483. a_load_ref_stack(list,size,tmpref,extra_slots);
  484. end;
  485. LOC_CONSTANT:
  486. a_load_const_stack(list,size,loc.value,def2regtyp(size));
  487. LOC_SUBSETREF,LOC_CSUBSETREF:
  488. a_load_subsetref_stack(list,size,loc.sref);
  489. else
  490. internalerror(2011010401);
  491. end;
  492. end;
  493. procedure thlcgwasm.a_loadfpu_const_stack(list: TAsmList; size: tdef; a: double);
  494. begin
  495. case tfloatdef(size).floattype of
  496. s32real:
  497. begin
  498. list.concat(taicpu.op_single(a_f32_const, a));
  499. incstack(list,1);
  500. end;
  501. s64real:
  502. begin
  503. list.concat(taicpu.op_double(a_f64_const,a));
  504. incstack(list,1);
  505. end
  506. else
  507. internalerror(2011010501);
  508. end;
  509. end;
  510. procedure thlcgwasm.a_op_stack(list: TAsmList; op: topcg; size: tdef);
  511. begin
  512. case def_cgsize(size) of
  513. OS_8,OS_S8,
  514. OS_16,OS_S16,
  515. OS_32,OS_S32:
  516. begin
  517. { boolean not: =0? for boolean }
  518. if (op=OP_NOT) and is_pasbool(size) then
  519. list.concat(taicpu.op_none(a_i32_eqz))
  520. else if (op=OP_NOT) and is_cbool(size) then
  521. begin
  522. current_asmdata.CurrAsmList.Concat(taicpu.op_functype(a_if,TWasmFuncType.Create([],[wbt_i32])));
  523. decstack(current_asmdata.CurrAsmList,1);
  524. current_asmdata.CurrAsmList.Concat( taicpu.op_const(a_i32_const, 0) );
  525. incstack(current_asmdata.CurrAsmList,1);
  526. current_asmdata.CurrAsmList.Concat( taicpu.op_none(a_else) );
  527. decstack(current_asmdata.CurrAsmList,1);
  528. case def_cgsize(size) of
  529. OS_32,OS_S32:
  530. current_asmdata.CurrAsmList.Concat( taicpu.op_const(a_i32_const, -1) );
  531. OS_16,OS_S16:
  532. current_asmdata.CurrAsmList.Concat( taicpu.op_const(a_i32_const, 65535) );
  533. OS_8,OS_S8:
  534. current_asmdata.CurrAsmList.Concat( taicpu.op_const(a_i32_const, 255) );
  535. else
  536. internalerror(2021100102);
  537. end;
  538. incstack(current_asmdata.CurrAsmList,1);
  539. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_end_if));
  540. end
  541. else
  542. begin
  543. if op=OP_NOT then
  544. begin
  545. { not = xor -1 for integer }
  546. a_load_const_stack(list,s32inttype,high(cardinal),R_INTREGISTER);
  547. op:=OP_XOR;
  548. end
  549. else if op=OP_NEG then
  550. begin
  551. { neg = *(-1) }
  552. a_load_const_stack(list,s32inttype,-1,R_INTREGISTER);
  553. op:=OP_MUL;
  554. end;
  555. if TOpCG2IAsmOp[op]=A_None then
  556. internalerror(2010120532);
  557. list.concat(taicpu.op_none(TOpCG2IAsmOp[op]));
  558. decstack(list,1);
  559. end;
  560. maybe_adjust_op_result(list,op,size);
  561. end;
  562. OS_64,OS_S64:
  563. begin
  564. { boolean not: =0? for boolean }
  565. if (op=OP_NOT) and is_pasbool(size) then
  566. begin
  567. list.concat(taicpu.op_none(a_i64_eqz));
  568. list.concat(taicpu.op_none(a_i64_extend_i32_u));
  569. end
  570. else if (op=OP_NOT) and is_cbool(size) then
  571. begin
  572. list.concat(taicpu.op_none(a_i64_eqz));
  573. current_asmdata.CurrAsmList.Concat(taicpu.op_functype(a_if,TWasmFuncType.Create([],[wbt_i64])));
  574. decstack(current_asmdata.CurrAsmList,1);
  575. current_asmdata.CurrAsmList.Concat( taicpu.op_const(a_i64_const, -1) );
  576. incstack(current_asmdata.CurrAsmList,1);
  577. current_asmdata.CurrAsmList.Concat( taicpu.op_none(a_else) );
  578. decstack(current_asmdata.CurrAsmList,1);
  579. current_asmdata.CurrAsmList.Concat( taicpu.op_const(a_i64_const, 0) );
  580. incstack(current_asmdata.CurrAsmList,1);
  581. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_end_if));
  582. end
  583. else
  584. begin
  585. if op=OP_NOT then
  586. begin
  587. { not = xor -1 for integer }
  588. a_load_const_stack(list,s64inttype,-1,R_INTREGISTER);
  589. op:=OP_XOR;
  590. end
  591. else if op=OP_NEG then
  592. begin
  593. { neg = *(-1) }
  594. a_load_const_stack(list,s64inttype,-1,R_INTREGISTER);
  595. op:=OP_MUL;
  596. end;
  597. if TOpCG2LAsmOp[op]=A_None then
  598. internalerror(2010120533);
  599. list.concat(taicpu.op_none(TOpCG2LAsmOp[op]));
  600. decstack(list,1);
  601. end;
  602. end;
  603. else
  604. internalerror(2010120531);
  605. end;
  606. end;
  607. procedure thlcgwasm.a_op_const_stack(list: TAsmList;op: topcg;size: tdef;a: tcgint);
  608. begin
  609. case op of
  610. OP_NEG,OP_NOT:
  611. internalerror(2011010801);
  612. else
  613. a_load_const_stack(list,size,a,R_INTREGISTER);
  614. end;
  615. a_op_stack(list,op,size);
  616. end;
  617. procedure thlcgwasm.a_op_reg_stack(list: TAsmList; op: topcg; size: tdef; reg: tregister);
  618. begin
  619. a_load_reg_stack(list,size,reg);
  620. a_op_stack(list,op,size);
  621. end;
  622. procedure thlcgwasm.a_op_ref_stack(list: TAsmList; op: topcg; size: tdef; const ref: treference);
  623. var
  624. tmpref: treference;
  625. begin
  626. { ref must not be the stack top, because that may indicate an error
  627. (it means that we will perform an operation of the stack top onto
  628. itself, so that means the two values have been loaded manually prior
  629. to calling this routine, instead of letting this routine load one of
  630. them; if something like that is needed, call a_op_stack() directly) }
  631. if ref.base=NR_EVAL_STACK_BASE then
  632. internalerror(2010121102);
  633. tmpref:=ref;
  634. a_load_ref_stack(list,size,tmpref,prepare_stack_for_ref(list,tmpref,false));
  635. a_op_stack(list,op,size);
  636. end;
  637. procedure thlcgwasm.a_op_loc_stack(list: TAsmList; op: topcg; size: tdef; const loc: tlocation);
  638. begin
  639. case loc.loc of
  640. LOC_REGISTER,LOC_CREGISTER:
  641. a_op_reg_stack(list,op,size,loc.register);
  642. LOC_REFERENCE,LOC_CREFERENCE:
  643. a_op_ref_stack(list,op,size,loc.reference);
  644. LOC_CONSTANT:
  645. a_op_const_stack(list,op,size,loc.value);
  646. else
  647. internalerror(2011011415)
  648. end;
  649. end;
  650. procedure thlcgwasm.a_cmp_const_loc_stack(list: TAsmList; size: tdef; cmp_op: topcmp; a: tcgint; const loc: tlocation);
  651. var
  652. tmpreg: tregister;
  653. begin
  654. case loc.loc of
  655. LOC_REGISTER,LOC_CREGISTER:
  656. a_cmp_const_reg_stack(list,size,cmp_op,a,loc.register);
  657. LOC_REFERENCE,LOC_CREFERENCE:
  658. a_cmp_const_ref_stack(list,size,cmp_op,a,loc.reference);
  659. LOC_SUBSETREG, LOC_CSUBSETREG:
  660. begin
  661. tmpreg:=getintregister(list,size);
  662. a_load_subsetreg_reg(list,size,size,loc.sreg,tmpreg);
  663. a_cmp_const_reg_stack(list,size,cmp_op,a,tmpreg);
  664. end;
  665. LOC_SUBSETREF, LOC_CSUBSETREF:
  666. begin
  667. tmpreg:=getintregister(list,size);
  668. a_load_subsetref_reg(list,size,size,loc.sref,tmpreg);
  669. a_cmp_const_reg_stack(list,size,cmp_op,a,tmpreg);
  670. end;
  671. else
  672. internalerror(2010120430);
  673. end;
  674. end;
  675. procedure thlcgwasm.a_cmp_const_ref_stack(list: TAsmList; size: tdef; cmp_op: topcmp; a: tcgint; const ref: treference);
  676. var
  677. tmpref: treference;
  678. regtyp: TRegisterType;
  679. begin
  680. tmpref:=ref;
  681. if tmpref.base<>NR_EVAL_STACK_BASE then
  682. a_load_ref_stack(list,size,tmpref,prepare_stack_for_ref(list,tmpref,false));
  683. regtyp:=def2regtyp(size);
  684. case regtyp of
  685. R_EXTERNREFREGISTER,
  686. R_FUNCREFREGISTER:
  687. begin
  688. if a<>0 then
  689. internalerror(2023061103);
  690. if not (cmp_op in [OC_EQ,OC_NE]) then
  691. internalerror(2023061104);
  692. list.Concat(taicpu.op_none(a_ref_is_null));
  693. if cmp_op=OC_NE then
  694. begin
  695. a_load_const_stack(list,s32inttype,0,R_INTREGISTER);
  696. a_cmp_stack_stack(list,s32inttype,OC_EQ);
  697. end;
  698. end;
  699. else
  700. begin
  701. a_load_const_stack(list,size,a,regtyp);
  702. a_cmp_stack_stack(list,size,cmp_op);
  703. end;
  704. end;
  705. end;
  706. procedure thlcgwasm.a_cmp_const_reg_stack(list: TAsmList; size: tdef; cmp_op: topcmp; a: tcgint; reg: tregister);
  707. var
  708. regtyp: TRegisterType;
  709. begin
  710. a_load_reg_stack(list,size,reg);
  711. regtyp:=def2regtyp(size);
  712. case regtyp of
  713. R_EXTERNREFREGISTER,
  714. R_FUNCREFREGISTER:
  715. begin
  716. if a<>0 then
  717. internalerror(2023061105);
  718. if not (cmp_op in [OC_EQ,OC_NE]) then
  719. internalerror(2023061106);
  720. list.Concat(taicpu.op_none(a_ref_is_null));
  721. if cmp_op=OC_NE then
  722. begin
  723. a_load_const_stack(list,s32inttype,0,R_INTREGISTER);
  724. a_cmp_stack_stack(list,s32inttype,OC_EQ);
  725. end;
  726. end;
  727. else
  728. begin
  729. a_load_const_stack(list,size,a,regtyp);
  730. a_cmp_stack_stack(list,size,cmp_op);
  731. end;
  732. end;
  733. end;
  734. procedure thlcgwasm.a_cmp_ref_reg_stack(list: TAsmList; size: tdef; cmp_op: topcmp; const ref: treference; reg: tregister);
  735. var
  736. tmpref: treference;
  737. begin
  738. tmpref:=ref;
  739. a_load_reg_stack(list,size,reg);
  740. if tmpref.base<>NR_EVAL_STACK_BASE then
  741. a_load_ref_stack(list,size,tmpref,prepare_stack_for_ref(list,tmpref,false))
  742. else
  743. cmp_op:=swap_opcmp(cmp_op);
  744. a_cmp_stack_stack(list,size,cmp_op);
  745. end;
  746. procedure thlcgwasm.a_cmp_reg_ref_stack(list: TAsmList; size: tdef; cmp_op: topcmp; reg: tregister; const ref: treference);
  747. var
  748. tmpref: treference;
  749. begin
  750. tmpref:=ref;
  751. if tmpref.base<>NR_EVAL_STACK_BASE then
  752. a_load_ref_stack(list,size,ref,prepare_stack_for_ref(list,tmpref,false));
  753. a_load_reg_stack(list,size,reg);
  754. a_cmp_stack_stack(list,size,cmp_op);
  755. end;
  756. procedure thlcgwasm.a_cmp_reg_reg_stack(list: TAsmList; size: tdef; cmp_op: topcmp; reg1, reg2: tregister);
  757. begin
  758. a_load_reg_stack(list,size,reg2);
  759. a_load_reg_stack(list,size,reg1);
  760. a_cmp_stack_stack(list,size,cmp_op);
  761. end;
  762. procedure thlcgwasm.a_cmp_subsetreg_reg_stack(list: TAsmList; fromsubsetsize, cmpsize: tdef; cmp_op: topcmp; const sreg: tsubsetregister; reg: tregister);
  763. var
  764. tmpreg: tregister;
  765. begin
  766. tmpreg:=getintregister(list,cmpsize);
  767. a_load_subsetreg_reg(list,fromsubsetsize,cmpsize,sreg,tmpreg);
  768. a_cmp_reg_reg_stack(list,cmpsize,cmp_op,tmpreg,reg);
  769. end;
  770. procedure thlcgwasm.a_cmp_subsetref_reg_stack(list: TAsmList; fromsubsetsize, cmpsize: tdef; cmp_op: topcmp; const sref: tsubsetreference; reg: tregister);
  771. var
  772. tmpreg: tregister;
  773. begin
  774. tmpreg:=getintregister(list,cmpsize);
  775. a_load_subsetref_reg(list,fromsubsetsize,cmpsize,sref,tmpreg);
  776. a_cmp_reg_reg_stack(list,cmpsize,cmp_op,tmpreg,reg);
  777. end;
  778. procedure thlcgwasm.a_cmp_loc_reg_stack(list: TAsmList; size: tdef; cmp_op: topcmp; const loc: tlocation; reg: tregister);
  779. begin
  780. case loc.loc of
  781. LOC_REGISTER,
  782. LOC_CREGISTER:
  783. a_cmp_reg_reg_stack(list,size,cmp_op,loc.register,reg);
  784. LOC_REFERENCE,
  785. LOC_CREFERENCE :
  786. a_cmp_ref_reg_stack(list,size,cmp_op,loc.reference,reg);
  787. LOC_CONSTANT:
  788. a_cmp_const_reg_stack(list,size,cmp_op,loc.value,reg);
  789. LOC_SUBSETREG,
  790. LOC_CSUBSETREG:
  791. a_cmp_subsetreg_reg_stack(list,size,size,cmp_op,loc.sreg,reg);
  792. LOC_SUBSETREF,
  793. LOC_CSUBSETREF:
  794. a_cmp_subsetref_reg_stack(list,size,size,cmp_op,loc.sref,reg);
  795. else
  796. internalerror(2010120431);
  797. end;
  798. end;
  799. procedure thlcgwasm.a_cmp_reg_loc_stack(list: TAsmList; size: tdef; cmp_op: topcmp; reg: tregister; const loc: tlocation);
  800. begin
  801. a_cmp_loc_reg_stack(list,size,swap_opcmp(cmp_op),loc,reg);
  802. end;
  803. procedure thlcgwasm.a_cmp_ref_loc_stack(list: TAsmList; size: tdef; cmp_op: topcmp; const ref: treference; const loc: tlocation);
  804. var
  805. tmpreg: tregister;
  806. begin
  807. case loc.loc of
  808. LOC_REGISTER,LOC_CREGISTER:
  809. a_cmp_ref_reg_stack(list,size,cmp_op,ref,loc.register);
  810. LOC_REFERENCE,LOC_CREFERENCE:
  811. begin
  812. tmpreg:=getintregister(list,size);
  813. a_load_ref_reg(list,size,size,loc.reference,tmpreg);
  814. a_cmp_ref_reg_stack(list,size,cmp_op,ref,tmpreg);
  815. end;
  816. LOC_CONSTANT:
  817. begin
  818. a_cmp_const_ref_stack(list,size,swap_opcmp(cmp_op),loc.value,ref);
  819. end;
  820. LOC_SUBSETREG, LOC_CSUBSETREG:
  821. begin
  822. tmpreg:=getintregister(list,size);
  823. a_load_ref_reg(list,size,size,loc.reference,tmpreg);
  824. a_cmp_subsetreg_reg_stack(list,size,size,swap_opcmp(cmp_op),loc.sreg,tmpreg);
  825. end;
  826. LOC_SUBSETREF, LOC_CSUBSETREF:
  827. begin
  828. tmpreg:=getintregister(list,size);
  829. a_load_ref_reg(list,size,size,loc.reference,tmpreg);
  830. a_cmp_subsetref_reg_stack(list,size,size,swap_opcmp(cmp_op),loc.sref,tmpreg);
  831. end;
  832. else
  833. internalerror(2010120432);
  834. end;
  835. end;
  836. procedure thlcgwasm.a_cmp_const_loc_br(list: TAsmList; size: tdef;cmp_op: topcmp; a: tcgint; const loc: tlocation; br: Integer);
  837. begin
  838. a_cmp_const_loc_stack(list,size,cmp_op,a,loc);
  839. current_asmdata.CurrAsmList.concat(taicpu.op_const(a_br_if,br));
  840. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  841. end;
  842. procedure thlcgwasm.a_cmp_const_ref_br(list: TAsmList; size: tdef; cmp_op: topcmp; a: tcgint; const ref: treference; br: Integer);
  843. begin
  844. a_cmp_const_ref_stack(list,size,cmp_op,a,ref);
  845. current_asmdata.CurrAsmList.concat(taicpu.op_const(a_br_if,br));
  846. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  847. end;
  848. procedure thlcgwasm.a_cmp_const_reg_br(list: TAsmList; size: tdef; cmp_op: topcmp; a: tcgint; reg: tregister; br: Integer);
  849. begin
  850. a_cmp_const_reg_stack(list,size,cmp_op,a,reg);
  851. current_asmdata.CurrAsmList.concat(taicpu.op_const(a_br_if,br));
  852. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  853. end;
  854. procedure thlcgwasm.a_cmp_ref_reg_br(list: TAsmList; size: tdef; cmp_op: topcmp; const ref: treference; reg: tregister; br: Integer);
  855. begin
  856. a_cmp_ref_reg_stack(list,size,cmp_op,ref,reg);
  857. current_asmdata.CurrAsmList.concat(taicpu.op_const(a_br_if,br));
  858. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  859. end;
  860. procedure thlcgwasm.a_cmp_reg_ref_br(list: TAsmList; size: tdef; cmp_op: topcmp; reg: tregister; const ref: treference; br: Integer);
  861. begin
  862. a_cmp_reg_ref_stack(list,size,cmp_op,reg,ref);
  863. current_asmdata.CurrAsmList.concat(taicpu.op_const(a_br_if,br));
  864. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  865. end;
  866. procedure thlcgwasm.a_cmp_reg_reg_br(list: TAsmList; size: tdef; cmp_op: topcmp; reg1, reg2: tregister; br: Integer);
  867. begin
  868. a_cmp_reg_reg_stack(list,size,cmp_op,reg1,reg2);
  869. current_asmdata.CurrAsmList.concat(taicpu.op_const(a_br_if,br));
  870. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  871. end;
  872. procedure thlcgwasm.a_cmp_subsetreg_reg_br(list: TAsmList; fromsubsetsize, cmpsize: tdef; cmp_op: topcmp; const sreg: tsubsetregister; reg: tregister; br: Integer);
  873. begin
  874. a_cmp_subsetreg_reg_stack(list,fromsubsetsize,cmpsize,cmp_op,sreg,reg);
  875. current_asmdata.CurrAsmList.concat(taicpu.op_const(a_br_if,br));
  876. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  877. end;
  878. procedure thlcgwasm.a_cmp_subsetref_reg_br(list: TAsmList; fromsubsetsize, cmpsize: tdef; cmp_op: topcmp; const sref: tsubsetreference; reg: tregister; br: Integer);
  879. begin
  880. a_cmp_subsetref_reg_stack(list,fromsubsetsize,cmpsize,cmp_op,sref,reg);
  881. current_asmdata.CurrAsmList.concat(taicpu.op_const(a_br_if,br));
  882. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  883. end;
  884. procedure thlcgwasm.a_cmp_loc_reg_br(list : TAsmList;size : tdef;cmp_op : topcmp; const loc: tlocation; reg : tregister; br: Integer);
  885. begin
  886. a_cmp_loc_reg_stack(list,size,cmp_op,loc,reg);
  887. current_asmdata.CurrAsmList.concat(taicpu.op_const(a_br_if,br));
  888. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  889. end;
  890. procedure thlcgwasm.a_cmp_reg_loc_br(list : TAsmList;size : tdef;cmp_op : topcmp; reg: tregister; const loc: tlocation; br: Integer);
  891. begin
  892. a_cmp_reg_loc_stack(list,size,cmp_op,reg,loc);
  893. current_asmdata.CurrAsmList.concat(taicpu.op_const(a_br_if,br));
  894. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  895. end;
  896. procedure thlcgwasm.a_cmp_ref_loc_br(list: TAsmList; size: tdef;cmp_op: topcmp; const ref: treference; const loc: tlocation; br: Integer);
  897. begin
  898. a_cmp_ref_loc_stack(list,size,cmp_op,ref,loc);
  899. current_asmdata.CurrAsmList.concat(taicpu.op_const(a_br_if,br));
  900. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  901. end;
  902. procedure thlcgwasm.g_reference_loc(list: TAsmList; def: tdef; const fromloc: tlocation; out toloc: tlocation);
  903. begin
  904. case fromloc.loc of
  905. LOC_CREFERENCE,
  906. LOC_REFERENCE:
  907. begin
  908. toloc:=fromloc;
  909. if (fromloc.reference.base<>NR_NO) and
  910. (fromloc.reference.base<>current_procinfo.framepointer) and
  911. (fromloc.reference.base<>NR_STACK_POINTER_REG) then
  912. g_allocload_reg_reg(list,voidpointertype,fromloc.reference.base,toloc.reference.base,R_ADDRESSREGISTER);
  913. end;
  914. else
  915. inherited;
  916. end;
  917. end;
  918. procedure thlcgwasm.a_cmp_stack_stack(list: TAsmlist; size: tdef; cmp_op: topcmp);
  919. const
  920. opcmp32: array[topcmp] of tasmop = (
  921. A_None, { OC_NONE, }
  922. a_i32_eq, { OC_EQ, equality comparison }
  923. a_i32_gt_s, { OC_GT, greater than (signed) }
  924. a_i32_lt_s, { OC_LT, less than (signed) }
  925. a_i32_ge_s, { OC_GTE, greater or equal than (signed) }
  926. a_i32_le_s, { OC_LTE, less or equal than (signed) }
  927. a_i32_ne, { OC_NE, not equal }
  928. a_i32_le_u, { OC_BE, less or equal than (unsigned) }
  929. a_i32_lt_u, { OC_B, less than (unsigned) }
  930. a_i32_ge_u, { OC_AE, greater or equal than (unsigned) }
  931. a_i32_gt_u { OC_A greater than (unsigned) }
  932. );
  933. const
  934. opcmp64: array[TOpCmp] of TAsmOp = (A_None,
  935. a_i64_eq, // OC_EQ
  936. a_i64_gt_s, a_i64_lt_s, // OC_GT, OC_LT
  937. a_i64_ge_s, a_i64_le_s, // OC_GTE, OC_LTE
  938. a_i64_ne, // OC_NE
  939. a_i64_le_u, a_i64_lt_u, // OC_BE, OC_B
  940. a_i64_ge_u, a_i64_gt_u // OC_AE, OC_A
  941. );
  942. var
  943. cgsize: tcgsize;
  944. begin
  945. case def2regtyp(size) of
  946. R_INTREGISTER,
  947. R_ADDRESSREGISTER:
  948. begin
  949. cgsize:=def_cgsize(size);
  950. case cgsize of
  951. OS_S8,OS_8,
  952. OS_16,OS_S16,
  953. OS_S32,OS_32:
  954. begin
  955. list.concat(taicpu.op_none(opcmp32[cmp_op]));
  956. decstack(list,1);
  957. end;
  958. OS_64,OS_S64:
  959. begin
  960. list.concat(taicpu.op_none(opcmp64[cmp_op]));
  961. decstack(list,1);
  962. end;
  963. else
  964. internalerror(2010120538);
  965. end;
  966. end;
  967. else
  968. internalerror(2010120538);
  969. end;
  970. end;
  971. procedure thlcgwasm.maybe_adjust_op_result(list: TAsmList; op: TOpCg; size: tdef);
  972. const
  973. overflowops = [OP_MUL,OP_SHL,OP_ADD,OP_SUB,OP_NOT,OP_NEG];
  974. begin
  975. if (op in overflowops) and
  976. (def_cgsize(size) in [OS_8,OS_S8,OS_16,OS_S16]) then
  977. resize_stack_int_val(list,s32inttype,size,false);
  978. end;
  979. procedure thlcgwasm.gen_load_uninitialized_function_result(list: TAsmList; pd: tprocdef; resdef: tdef; const resloc: tcgpara);
  980. begin
  981. { nothing to do for ret_in_param results }
  982. if paramanager.ret_in_param(pd.returndef,pd) then
  983. exit;
  984. { constructors don't return anything in Java }
  985. if pd.proctypeoption=potype_constructor then
  986. exit;
  987. { must return a value of the correct type on the evaluation stack }
  988. case def2regtyp(resdef) of
  989. R_INTREGISTER,
  990. R_ADDRESSREGISTER:
  991. a_load_const_cgpara(list,resdef,0,resloc);
  992. R_FPUREGISTER:
  993. case tfloatdef(resdef).floattype of
  994. s32real:
  995. begin
  996. list.concat(taicpu.op_single(a_f32_const, 0));
  997. incstack(list,1);
  998. end;
  999. s64real:
  1000. begin
  1001. list.concat(taicpu.op_double(a_f64_const, 0));
  1002. incstack(list,1);
  1003. end;
  1004. else
  1005. internalerror(2011010302);
  1006. end;
  1007. R_FUNCREFREGISTER:
  1008. begin
  1009. list.concat(taicpu.op_none(a_ref_null_funcref));
  1010. incstack(list,1);
  1011. end;
  1012. R_EXTERNREFREGISTER:
  1013. begin
  1014. list.concat(taicpu.op_none(a_ref_null_externref));
  1015. incstack(list,1);
  1016. end;
  1017. else
  1018. internalerror(2011010301);
  1019. end;
  1020. end;
  1021. function thlcgwasm.g_call_system_proc_intern(list: TAsmList; pd: tprocdef; const paras: array of pcgpara; forceresdef: tdef): tcgpara;
  1022. begin
  1023. result:=inherited;
  1024. pd.init_paraloc_info(callerside);
  1025. g_adjust_stack_after_call(list,pd);
  1026. end;
  1027. function thlcgwasm.prepare_stack_for_ref(list: TAsmList; var ref: treference; dup: boolean): longint;
  1028. var
  1029. tmpref: treference;
  1030. begin
  1031. result:=0;
  1032. { fake location that indicates the value is already on the stack? }
  1033. if (ref.base=NR_EVAL_STACK_BASE) or (ref.base=NR_LOCAL_STACK_POINTER_REG) then
  1034. exit;
  1035. if (ref.base=NR_NO) and (ref.index<>NR_NO) and (ref.scalefactor<=1) then
  1036. begin
  1037. ref.base:=ref.index;
  1038. ref.index:=NR_NO;
  1039. end;
  1040. if assigned(ref.symbol) and (ref.symbol.typ=AT_WASM_GLOBAL) then
  1041. begin
  1042. if ref.base<>NR_NO then
  1043. internalerror(2022072601);
  1044. if ref.index<>NR_NO then
  1045. internalerror(2022072602);
  1046. if ref.offset<>0 then
  1047. internalerror(2022072603);
  1048. end
  1049. else if ref.refaddr=addr_got_tls then
  1050. begin
  1051. if not assigned(ref.symbol) then
  1052. internalerror(2022071405);
  1053. if ref.base<>NR_NO then
  1054. internalerror(2022071406);
  1055. if ref.index<>NR_NO then
  1056. internalerror(2022071407);
  1057. tmpref:=ref;
  1058. tmpref.offset:=0;
  1059. list.Concat(taicpu.op_ref(a_global_get,tmpref));
  1060. incstack(list,1);
  1061. if dup then
  1062. begin
  1063. list.Concat(taicpu.op_ref(a_global_get,tmpref));
  1064. incstack(list,1);
  1065. end;
  1066. result:=1;
  1067. end
  1068. else if assigned(ref.symbol) and (ref.base=NR_NO) and (ref.index=NR_NO) then
  1069. begin
  1070. list.Concat(taicpu.op_const(a_i32_const,ref.offset));
  1071. incstack(list,1);
  1072. if dup then
  1073. begin
  1074. list.Concat(taicpu.op_const(a_i32_const,ref.offset));
  1075. incstack(list,1);
  1076. end;
  1077. ref.offset:=0;
  1078. result:=1;
  1079. end
  1080. else if ref.index <> NR_NO then // array access
  1081. begin
  1082. // it's just faster to sum two of those together
  1083. list.Concat(taicpu.op_reg(a_local_get, ref.base));
  1084. incstack(list,1);
  1085. list.Concat(taicpu.op_reg(a_local_get, ref.index));
  1086. incstack(list,1);
  1087. list.Concat(taicpu.op_none(a_i32_add));
  1088. decstack(list,1);
  1089. if assigned(ref.symbol) then
  1090. begin
  1091. list.Concat(taicpu.op_sym(a_i32_const,ref.symbol));
  1092. incstack(list,1);
  1093. list.Concat(taicpu.op_none(a_i32_add));
  1094. decstack(list,1);
  1095. end;
  1096. if ref.offset<0 then
  1097. begin
  1098. list.Concat(taicpu.op_const(a_i32_const,-ref.offset));
  1099. incstack(list,1);
  1100. list.Concat(taicpu.op_none(a_i32_sub));
  1101. decstack(list,1);
  1102. end
  1103. else if ref.offset>0 then
  1104. begin
  1105. list.Concat(taicpu.op_const(a_i32_const,ref.offset));
  1106. incstack(list,1);
  1107. list.Concat(taicpu.op_none(a_i32_add));
  1108. decstack(list,1);
  1109. end;
  1110. if dup then
  1111. begin
  1112. list.Concat(taicpu.op_reg(a_local_get, ref.base));
  1113. incstack(list,1);
  1114. list.Concat(taicpu.op_reg(a_local_get, ref.index));
  1115. incstack(list,1);
  1116. list.Concat(taicpu.op_none(a_i32_add));
  1117. decstack(list,1);
  1118. if assigned(ref.symbol) then
  1119. begin
  1120. list.Concat(taicpu.op_sym(a_i32_const,ref.symbol));
  1121. incstack(list,1);
  1122. list.Concat(taicpu.op_none(a_i32_add));
  1123. decstack(list,1);
  1124. end;
  1125. if ref.offset<0 then
  1126. begin
  1127. list.Concat(taicpu.op_const(a_i32_const,-ref.offset));
  1128. incstack(list,1);
  1129. list.Concat(taicpu.op_none(a_i32_sub));
  1130. decstack(list,1);
  1131. end
  1132. else if ref.offset>0 then
  1133. begin
  1134. list.Concat(taicpu.op_const(a_i32_const,ref.offset));
  1135. incstack(list,1);
  1136. list.Concat(taicpu.op_none(a_i32_add));
  1137. decstack(list,1);
  1138. end;
  1139. end;
  1140. ref.base:=NR_NO;
  1141. ref.index:=NR_NO;
  1142. ref.offset:=0;
  1143. ref.symbol:=nil;
  1144. result:=1;
  1145. end
  1146. else if (ref.base<>NR_NO) then
  1147. begin
  1148. if (ref.base<>NR_STACK_POINTER_REG) then
  1149. begin
  1150. { regular field -> load self on the stack }
  1151. a_load_reg_stack(list,voidpointertype,ref.base);
  1152. if assigned(ref.symbol) then
  1153. begin
  1154. list.Concat(taicpu.op_sym(a_i32_const,ref.symbol));
  1155. incstack(list,1);
  1156. list.Concat(taicpu.op_none(a_i32_add));
  1157. decstack(list,1);
  1158. end;
  1159. if ref.offset<0 then
  1160. begin
  1161. list.Concat(taicpu.op_const(a_i32_const,-ref.offset));
  1162. incstack(list,1);
  1163. list.Concat(taicpu.op_none(a_i32_sub));
  1164. decstack(list,1);
  1165. end
  1166. else if ref.offset>0 then
  1167. begin
  1168. list.Concat(taicpu.op_const(a_i32_const,ref.offset));
  1169. incstack(list,1);
  1170. list.Concat(taicpu.op_none(a_i32_add));
  1171. decstack(list,1);
  1172. end;
  1173. if dup then
  1174. begin
  1175. a_load_reg_stack(list,voidpointertype,ref.base);
  1176. if assigned(ref.symbol) then
  1177. begin
  1178. list.Concat(taicpu.op_sym(a_i32_const,ref.symbol));
  1179. incstack(list,1);
  1180. list.Concat(taicpu.op_none(a_i32_add));
  1181. decstack(list,1);
  1182. end;
  1183. if ref.offset<0 then
  1184. begin
  1185. list.Concat(taicpu.op_const(a_i32_const,-ref.offset));
  1186. incstack(list,1);
  1187. list.Concat(taicpu.op_none(a_i32_sub));
  1188. decstack(list,1);
  1189. end
  1190. else if ref.offset>0 then
  1191. begin
  1192. list.Concat(taicpu.op_const(a_i32_const,ref.offset));
  1193. incstack(list,1);
  1194. list.Concat(taicpu.op_none(a_i32_add));
  1195. decstack(list,1);
  1196. end;
  1197. end;
  1198. ref.offset:=0;
  1199. ref.symbol:=nil;
  1200. ref.base:=NR_NO;
  1201. result:=1;
  1202. end
  1203. else // if (ref.base = NR_FRAME_POINTER_REG) then
  1204. begin
  1205. internalerror(2021012202);
  1206. //list.Concat(taicpu.op_sym(a_local_get, current_asmdata.RefAsmSymbol(FRAME_POINTER_SYM,AT_ADDR) ));
  1207. //incstack(list,1);
  1208. end;
  1209. end
  1210. else
  1211. begin
  1212. { static field -> nothing to do here, except for validity check }
  1213. {if not assigned(ref.symbol) or
  1214. (ref.offset<>0) then
  1215. begin
  1216. internalerror(2010120525);
  1217. end;}
  1218. end;
  1219. end;
  1220. procedure thlcgwasm.gen_load_cgpara_loc(list: TAsmList; vardef: tdef; const para: TCGPara; var destloc: tlocation; reusepara: boolean);
  1221. begin
  1222. { support loading a function result (from the evaluation stack), to a register }
  1223. if assigned(para.location) and (not assigned(para.location^.next)) and
  1224. (para.location^.loc in [LOC_REFERENCE,LOC_CREFERENCE]) and
  1225. (para.location^.reference.index=NR_EVAL_STACK_BASE) and
  1226. (para.location^.reference.offset=0) and
  1227. (def_cgsize(para.location^.Def)=destloc.size) and
  1228. (destloc.loc=LOC_REGISTER) then
  1229. a_load_stack_loc(list,para.location^.Def,destloc)
  1230. else
  1231. inherited;
  1232. end;
  1233. procedure thlcgwasm.a_load_const_reg(list: TAsmList; tosize: tdef; a: tcgint; register: tregister);
  1234. begin
  1235. a_load_const_stack(list,tosize,a,def2regtyp(tosize));
  1236. a_load_stack_reg(list,tosize,register);
  1237. end;
  1238. procedure thlcgwasm.a_load_const_ref(list: TAsmList; tosize: tdef; a: tcgint; const ref: treference);
  1239. var
  1240. extra_slots: longint;
  1241. tmpref: treference;
  1242. begin
  1243. tmpref:=ref;
  1244. extra_slots:=prepare_stack_for_ref(list,tmpref,false);
  1245. a_load_const_stack(list,tosize,a,def2regtyp(tosize));
  1246. a_load_stack_ref(list,tosize,tmpref,extra_slots);
  1247. end;
  1248. procedure thlcgwasm.a_load_reg_ref(list: TAsmList; fromsize, tosize: tdef; register: tregister; const ref: treference);
  1249. var
  1250. extra_slots: longint;
  1251. tmpref: treference;
  1252. begin
  1253. tmpref:=ref;
  1254. extra_slots:=prepare_stack_for_ref(list,tmpref,false);
  1255. a_load_reg_stack(list,fromsize,register);
  1256. if def2regtyp(fromsize)=R_INTREGISTER then
  1257. resize_stack_int_val(list,fromsize,tosize,assigned(tmpref.symbol));
  1258. a_load_stack_ref(list,tosize,tmpref,extra_slots);
  1259. end;
  1260. procedure thlcgwasm.a_load_reg_reg(list: TAsmList; fromsize, tosize: tdef; reg1, reg2: tregister);
  1261. begin
  1262. a_load_reg_stack(list,fromsize,reg1);
  1263. if def2regtyp(fromsize)=R_INTREGISTER then
  1264. resize_stack_int_val(list,fromsize,tosize,false);
  1265. a_load_stack_reg(list,tosize,reg2);
  1266. end;
  1267. procedure thlcgwasm.a_load_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; register: tregister);
  1268. var
  1269. extra_slots: longint;
  1270. tmpref: treference;
  1271. begin
  1272. tmpref:=ref;
  1273. extra_slots:=prepare_stack_for_ref(list,tmpref,false);
  1274. a_load_ref_stack(list,fromsize,tmpref,extra_slots);
  1275. if def2regtyp(fromsize)=R_INTREGISTER then
  1276. resize_stack_int_val(list,fromsize,tosize,false);
  1277. a_load_stack_reg(list,tosize,register);
  1278. end;
  1279. procedure thlcgwasm.a_load_ref_ref(list: TAsmList; fromsize, tosize: tdef; const sref: treference; const dref: treference);
  1280. var
  1281. extra_sslots,
  1282. extra_dslots: longint;
  1283. tmpsref, tmpdref: treference;
  1284. tmpreg: tregister;
  1285. begin
  1286. if sref.base<>NR_EVAL_STACK_BASE then
  1287. begin
  1288. tmpsref:=sref;
  1289. tmpdref:=dref;
  1290. { make sure the destination reference is on top, since in the end the
  1291. order has to be "destref, value" -> first create "destref, sourceref" }
  1292. extra_dslots:=prepare_stack_for_ref(list,tmpdref,false);
  1293. extra_sslots:=prepare_stack_for_ref(list,tmpsref,false);
  1294. a_load_ref_stack(list,fromsize,tmpsref,extra_sslots);
  1295. if def2regtyp(fromsize)=R_INTREGISTER then
  1296. resize_stack_int_val(list,fromsize,tosize,assigned(tmpdref.symbol));
  1297. a_load_stack_ref(list,tosize,tmpdref,extra_dslots);
  1298. end
  1299. else
  1300. begin
  1301. { verify if we have the same reference }
  1302. if references_equal(sref,dref) then
  1303. exit;
  1304. tmpreg:=getregisterfordef(list,tosize);
  1305. a_load_ref_reg(list,fromsize,tosize,sref,tmpreg);
  1306. a_load_reg_ref(list,tosize,tosize,tmpreg,dref);
  1307. end;
  1308. end;
  1309. procedure thlcgwasm.a_load_loc_ref(list : TAsmList;fromsize, tosize: tdef; const loc: tlocation; const ref : treference);
  1310. var
  1311. tmpref: treference;
  1312. begin
  1313. if is_methodptr_like_type(tosize) and (loc.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  1314. begin
  1315. tmpref:=ref;
  1316. a_load_reg_ref(list,voidcodepointertype,voidcodepointertype,loc.register,tmpref);
  1317. inc(tmpref.offset,voidcodepointertype.size);
  1318. { the second part could be either self or parentfp }
  1319. if tosize.size=(voidcodepointertype.size+voidpointertype.size) then
  1320. a_load_reg_ref(list,voidpointertype,voidpointertype,loc.registerhi,tmpref)
  1321. else if tosize.size=(voidcodepointertype.size+parentfpvoidpointertype.size) then
  1322. a_load_reg_ref(list,parentfpvoidpointertype,parentfpvoidpointertype,loc.registerhi,tmpref)
  1323. else
  1324. internalerror(2021100301);
  1325. end
  1326. else
  1327. inherited;
  1328. end;
  1329. procedure thlcgwasm.a_loadaddr_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; r: tregister);
  1330. begin
  1331. a_loadaddr_ref_stack(list,fromsize,tosize,ref);
  1332. a_load_stack_reg(list, tosize, r);
  1333. end;
  1334. procedure thlcgwasm.a_load_subsetref_regs_index(list: TAsmList; subsetsize: tdef; loadbitsize: byte; const sref: tsubsetreference; valuereg: tregister);
  1335. var
  1336. tmpref: treference;
  1337. extra_value_reg,
  1338. tmpreg: tregister;
  1339. begin
  1340. tmpreg:=getintregister(list,osuinttype);
  1341. tmpref:=sref.ref;
  1342. inc(tmpref.offset,loadbitsize div 8);
  1343. extra_value_reg:=getintregister(list,osuinttype);
  1344. a_op_reg_reg(list,OP_SHR,osuinttype,sref.bitindexreg,valuereg);
  1345. { ensure we don't load anything past the end of the array }
  1346. a_cmp_const_reg_stack(list,osuinttype,OC_A,loadbitsize-sref.bitlen,sref.bitindexreg);
  1347. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_if));
  1348. decstack(current_asmdata.CurrAsmList,1);
  1349. { Y-x = -(Y-x) }
  1350. a_op_const_reg_reg(list,OP_SUB,osuinttype,loadbitsize,sref.bitindexreg,tmpreg);
  1351. a_op_reg_reg(list,OP_NEG,osuinttype,tmpreg,tmpreg);
  1352. { load next "loadbitsize" bits of the array }
  1353. a_load_ref_reg(list,cgsize_orddef(int_cgsize(loadbitsize div 8)),osuinttype,tmpref,extra_value_reg);
  1354. { tmpreg is in the range 1..<cpu_bitsize>-1 -> always ok }
  1355. a_op_reg_reg(list,OP_SHL,osuinttype,tmpreg,extra_value_reg);
  1356. { merge }
  1357. a_op_reg_reg(list,OP_OR,osuinttype,extra_value_reg,valuereg);
  1358. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_end_if));
  1359. { sign extend or mask other bits }
  1360. if is_signed(subsetsize) then
  1361. begin
  1362. a_op_const_reg(list,OP_SHL,osuinttype,AIntBits-sref.bitlen,valuereg);
  1363. a_op_const_reg(list,OP_SAR,osuinttype,AIntBits-sref.bitlen,valuereg);
  1364. end
  1365. else
  1366. a_op_const_reg(list,OP_AND,osuinttype,tcgint((aword(1) shl sref.bitlen)-1),valuereg);
  1367. end;
  1368. procedure thlcgwasm.a_load_regconst_subsetref_intern(list : TAsmList; fromsize, subsetsize: tdef; fromreg: tregister; const sref: tsubsetreference; slopt: tsubsetloadopt);
  1369. var
  1370. tmpreg, tmpindexreg, valuereg, extra_value_reg, maskreg: tregister;
  1371. tosreg, fromsreg: tsubsetregister;
  1372. tmpref: treference;
  1373. bitmask: aword;
  1374. loadsize: torddef;
  1375. loadbitsize: byte;
  1376. extra_load: boolean;
  1377. begin
  1378. { the register must be able to contain the requested value }
  1379. if (fromsize.size*8<sref.bitlen) then
  1380. internalerror(2006081613);
  1381. get_subsetref_load_info(sref,loadsize,extra_load);
  1382. loadbitsize:=loadsize.size*8;
  1383. { load the (first part) of the bit sequence }
  1384. valuereg:=getintregister(list,osuinttype);
  1385. a_load_ref_reg(list,loadsize,osuinttype,sref.ref,valuereg);
  1386. { constant offset of bit sequence? }
  1387. if not extra_load then
  1388. begin
  1389. if (sref.bitindexreg=NR_NO) then
  1390. begin
  1391. { use subsetreg routine, it may have been overridden with an optimized version }
  1392. tosreg.subsetreg:=valuereg;
  1393. tosreg.subsetregsize:=def_cgsize(osuinttype);
  1394. { subsetregs always count bits from right to left }
  1395. tosreg.startbit:=sref.startbit;
  1396. tosreg.bitlen:=sref.bitlen;
  1397. a_load_regconst_subsetreg_intern(list,fromsize,subsetsize,fromreg,tosreg,slopt);
  1398. end
  1399. else
  1400. begin
  1401. if (sref.startbit<>0) then
  1402. internalerror(2006081710);
  1403. { should be handled by normal code and will give wrong result }
  1404. { on x86 for the '1 shl bitlen' below }
  1405. if (sref.bitlen=AIntBits) then
  1406. internalerror(2006081711);
  1407. { zero the bits we have to insert }
  1408. if (slopt<>SL_SETMAX) then
  1409. begin
  1410. maskreg:=getintregister(list,osuinttype);
  1411. a_load_const_reg(list,osuinttype,tcgint((aword(1) shl sref.bitlen)-1),maskreg);
  1412. a_op_reg_reg(list,OP_SHL,osuinttype,sref.bitindexreg,maskreg);
  1413. a_op_reg_reg(list,OP_NOT,osuinttype,maskreg,maskreg);
  1414. a_op_reg_reg(list,OP_AND,osuinttype,maskreg,valuereg);
  1415. end;
  1416. { insert the value }
  1417. if (slopt<>SL_SETZERO) then
  1418. begin
  1419. tmpreg:=getintregister(list,osuinttype);
  1420. if (slopt<>SL_SETMAX) then
  1421. a_load_reg_reg(list,fromsize,osuinttype,fromreg,tmpreg)
  1422. else if (sref.bitlen<>AIntBits) then
  1423. a_load_const_reg(list,osuinttype,tcgint((aword(1) shl sref.bitlen)-1), tmpreg)
  1424. else
  1425. a_load_const_reg(list,osuinttype,-1,tmpreg);
  1426. if not(slopt in [SL_REGNOSRCMASK,SL_SETMAX]) then
  1427. a_op_const_reg(list,OP_AND,osuinttype,tcgint((aword(1) shl sref.bitlen)-1),tmpreg);
  1428. a_op_reg_reg(list,OP_SHL,osuinttype,sref.bitindexreg,tmpreg);
  1429. a_op_reg_reg(list,OP_OR,osuinttype,tmpreg,valuereg);
  1430. end;
  1431. end;
  1432. { store back to memory }
  1433. tmpreg:=getintregister(list,loadsize);
  1434. a_load_reg_reg(list,osuinttype,loadsize,valuereg,tmpreg);
  1435. a_load_reg_ref(list,loadsize,loadsize,tmpreg,sref.ref);
  1436. exit;
  1437. end
  1438. else
  1439. begin
  1440. { load next value }
  1441. extra_value_reg:=getintregister(list,osuinttype);
  1442. tmpref:=sref.ref;
  1443. inc(tmpref.offset,loadbitsize div 8);
  1444. { should maybe be taken out too, can be done more efficiently }
  1445. { on e.g. i386 with shld/shrd }
  1446. if (sref.bitindexreg = NR_NO) then
  1447. begin
  1448. a_load_ref_reg(list,loadsize,osuinttype,tmpref,extra_value_reg);
  1449. fromsreg.subsetreg:=fromreg;
  1450. fromsreg.subsetregsize:=def_cgsize(fromsize);
  1451. tosreg.subsetreg:=valuereg;
  1452. tosreg.subsetregsize:=def_cgsize(osuinttype);
  1453. { transfer first part }
  1454. fromsreg.bitlen:=loadbitsize-sref.startbit;
  1455. tosreg.bitlen:=fromsreg.bitlen;
  1456. { valuereg must contain the lower bits of the value at bits [startbit..loadbitsize] }
  1457. { lower bits of the value ... }
  1458. fromsreg.startbit:=0;
  1459. { ... to startbit }
  1460. tosreg.startbit:=sref.startbit;
  1461. case slopt of
  1462. SL_SETZERO,
  1463. SL_SETMAX:
  1464. a_load_regconst_subsetreg_intern(list,fromsize,subsetsize,fromreg,tosreg,slopt);
  1465. else
  1466. a_load_subsetreg_subsetreg(list,subsetsize,subsetsize,fromsreg,tosreg);
  1467. end;
  1468. {$ifndef cpuhighleveltarget}
  1469. valuereg:=cg.makeregsize(list,valuereg,def_cgsize(loadsize));
  1470. a_load_reg_ref(list,loadsize,loadsize,valuereg,sref.ref);
  1471. {$else}
  1472. tmpreg:=getintregister(list,loadsize);
  1473. a_load_reg_reg(list,osuinttype,loadsize,valuereg,tmpreg);
  1474. a_load_reg_ref(list,loadsize,loadsize,tmpreg,sref.ref);
  1475. {$endif}
  1476. { transfer second part }
  1477. { extra_value_reg must contain the upper bits of the value at bits [0..bitlen-(loadbitsize-startbit)] }
  1478. fromsreg.startbit:=fromsreg.bitlen;
  1479. tosreg.startbit:=0;
  1480. tosreg.subsetreg:=extra_value_reg;
  1481. fromsreg.bitlen:=sref.bitlen-fromsreg.bitlen;
  1482. tosreg.bitlen:=fromsreg.bitlen;
  1483. case slopt of
  1484. SL_SETZERO,
  1485. SL_SETMAX:
  1486. a_load_regconst_subsetreg_intern(list,fromsize,subsetsize,fromreg,tosreg,slopt);
  1487. else
  1488. a_load_subsetreg_subsetreg(list,subsetsize,subsetsize,fromsreg,tosreg);
  1489. end;
  1490. tmpreg:=getintregister(list,loadsize);
  1491. a_load_reg_reg(list,osuinttype,loadsize,extra_value_reg,tmpreg);
  1492. a_load_reg_ref(list,loadsize,loadsize,tmpreg,tmpref);
  1493. exit;
  1494. end
  1495. else
  1496. begin
  1497. if (sref.startbit <> 0) then
  1498. internalerror(2006081812);
  1499. { should be handled by normal code and will give wrong result }
  1500. { on x86 for the '1 shl bitlen' below }
  1501. if (sref.bitlen = AIntBits) then
  1502. internalerror(2006081713);
  1503. { generate mask to zero the bits we have to insert }
  1504. if (slopt <> SL_SETMAX) then
  1505. begin
  1506. maskreg := getintregister(list,osuinttype);
  1507. a_load_const_reg(list,osuinttype,tcgint((aword(1) shl sref.bitlen)-1),maskreg);
  1508. a_op_reg_reg(list,OP_SHL,osuinttype,sref.bitindexreg,maskreg);
  1509. a_op_reg_reg(list,OP_NOT,osuinttype,maskreg,maskreg);
  1510. a_op_reg_reg(list,OP_AND,osuinttype,maskreg,valuereg);
  1511. end;
  1512. { insert the value }
  1513. if (slopt <> SL_SETZERO) then
  1514. begin
  1515. tmpreg := getintregister(list,osuinttype);
  1516. if (slopt <> SL_SETMAX) then
  1517. a_load_reg_reg(list,fromsize,osuinttype,fromreg,tmpreg)
  1518. else if (sref.bitlen <> AIntBits) then
  1519. a_load_const_reg(list,osuinttype,tcgint((aword(1) shl sref.bitlen) - 1), tmpreg)
  1520. else
  1521. a_load_const_reg(list,osuinttype,-1,tmpreg);
  1522. if not(slopt in [SL_REGNOSRCMASK,SL_SETMAX]) then
  1523. { mask left over bits }
  1524. a_op_const_reg(list,OP_AND,osuinttype,tcgint((aword(1) shl sref.bitlen)-1),tmpreg);
  1525. a_op_reg_reg(list,OP_SHL,osuinttype,sref.bitindexreg,tmpreg);
  1526. a_op_reg_reg(list,OP_OR,osuinttype,tmpreg,valuereg);
  1527. end;
  1528. tmpreg:=getintregister(list,loadsize);
  1529. a_load_reg_reg(list,osuinttype,loadsize,valuereg,tmpreg);
  1530. a_load_reg_ref(list,loadsize,loadsize,tmpreg,sref.ref);
  1531. { make sure we do not read/write past the end of the array }
  1532. a_cmp_const_reg_stack(list,osuinttype,OC_A,loadbitsize-sref.bitlen,sref.bitindexreg);
  1533. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_if));
  1534. decstack(current_asmdata.CurrAsmList,1);
  1535. a_load_ref_reg(list,loadsize,osuinttype,tmpref,extra_value_reg);
  1536. tmpindexreg:=getintregister(list,osuinttype);
  1537. { load current array value }
  1538. if (slopt<>SL_SETZERO) then
  1539. begin
  1540. tmpreg:=getintregister(list,osuinttype);
  1541. if (slopt<>SL_SETMAX) then
  1542. a_load_reg_reg(list,fromsize,osuinttype,fromreg,tmpreg)
  1543. else if (sref.bitlen<>AIntBits) then
  1544. a_load_const_reg(list,osuinttype,tcgint((aword(1) shl sref.bitlen) - 1), tmpreg)
  1545. else
  1546. a_load_const_reg(list,osuinttype,-1,tmpreg);
  1547. end;
  1548. { generate mask to zero the bits we have to insert }
  1549. if (slopt<>SL_SETMAX) then
  1550. begin
  1551. maskreg:=getintregister(list,osuinttype);
  1552. { Y-x = -(x-Y) }
  1553. a_op_const_reg_reg(list,OP_SUB,osuinttype,loadbitsize,sref.bitindexreg,tmpindexreg);
  1554. a_op_reg_reg(list,OP_NEG,osuinttype,tmpindexreg,tmpindexreg);
  1555. a_load_const_reg(list,osuinttype,tcgint((aword(1) shl sref.bitlen)-1),maskreg);
  1556. a_op_reg_reg(list,OP_SHR,osuinttype,tmpindexreg,maskreg);
  1557. a_op_reg_reg(list,OP_NOT,osuinttype,maskreg,maskreg);
  1558. a_op_reg_reg(list,OP_AND,osuinttype,maskreg,extra_value_reg);
  1559. end;
  1560. if (slopt<>SL_SETZERO) then
  1561. begin
  1562. if not(slopt in [SL_REGNOSRCMASK,SL_SETMAX]) then
  1563. a_op_const_reg(list,OP_AND,osuinttype,tcgint((aword(1) shl sref.bitlen)-1),tmpreg);
  1564. a_op_reg_reg(list,OP_SHR,osuinttype,tmpindexreg,tmpreg);
  1565. a_op_reg_reg(list,OP_OR,osuinttype,tmpreg,extra_value_reg);
  1566. end;
  1567. {$ifndef cpuhighleveltarget}
  1568. extra_value_reg:=cg.makeregsize(list,extra_value_reg,def_cgsize(loadsize));
  1569. a_load_reg_ref(list,loadsize,loadsize,extra_value_reg,tmpref);
  1570. {$else}
  1571. tmpreg:=getintregister(list,loadsize);
  1572. a_load_reg_reg(list,osuinttype,loadsize,extra_value_reg,tmpreg);
  1573. a_load_reg_ref(list,loadsize,loadsize,tmpreg,tmpref);
  1574. {$endif}
  1575. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_end_if));
  1576. end;
  1577. end;
  1578. end;
  1579. procedure thlcgwasm.a_op_const_reg(list: TAsmList; Op: TOpCG; size: tdef; a: tcgint; reg: TRegister);
  1580. begin
  1581. a_op_const_reg_reg(list,op,size,a,reg,reg);
  1582. end;
  1583. procedure thlcgwasm.a_op_const_reg_reg(list: TAsmList; op: TOpCg; size: tdef; a: tcgint; src, dst: tregister);
  1584. begin
  1585. a_load_reg_stack(list,size,src);
  1586. a_op_const_stack(list,op,size,a);
  1587. a_load_stack_reg(list,size,dst);
  1588. end;
  1589. procedure thlcgwasm.a_op_const_ref(list: TAsmList; Op: TOpCG; size: tdef; a: tcgint; const ref: TReference);
  1590. var
  1591. extra_slots: longint;
  1592. tmpref: treference;
  1593. begin
  1594. tmpref:=ref;
  1595. extra_slots:=prepare_stack_for_ref(list,tmpref,true);
  1596. { TODO, here or in peepholeopt: use iinc when possible }
  1597. a_load_ref_stack(list,size,tmpref,extra_slots);
  1598. a_op_const_stack(list,op,size,a);
  1599. { for android verifier }
  1600. if (def2regtyp(size)=R_INTREGISTER) and
  1601. (assigned(tmpref.symbol)) then
  1602. resize_stack_int_val(list,size,size,true);
  1603. a_load_stack_ref(list,size,tmpref,extra_slots);
  1604. end;
  1605. procedure thlcgwasm.a_op_ref_reg(list: TAsmList; Op: TOpCG; size: tdef; const ref: TReference; reg: TRegister);
  1606. begin
  1607. if not(op in [OP_NOT,OP_NEG]) then
  1608. a_load_reg_stack(list,size,reg);
  1609. a_op_ref_stack(list,op,size,ref);
  1610. a_load_stack_reg(list,size,reg);
  1611. end;
  1612. procedure thlcgwasm.a_op_reg_reg_reg(list: TAsmList; op: TOpCg; size: tdef; src1, src2, dst: tregister);
  1613. begin
  1614. if not(op in [OP_NOT,OP_NEG]) then
  1615. a_load_reg_stack(list,size,src2);
  1616. a_op_reg_stack(list,op,size,src1);
  1617. a_load_stack_reg(list,size,dst);
  1618. end;
  1619. procedure thlcgwasm.a_op_reg_reg(list: TAsmList; Op: TOpCG; size: tdef; reg1, reg2: TRegister);
  1620. begin
  1621. a_op_reg_reg_reg(list,op,size,reg1,reg2,reg2);
  1622. end;
  1623. procedure thlcgwasm.a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tdef; a: tcgint; src, dst: tregister; setflags: boolean; var ovloc: tlocation);
  1624. var
  1625. tmpreg: tregister;
  1626. begin
  1627. if not setflags then
  1628. begin
  1629. inherited;
  1630. exit;
  1631. end;
  1632. tmpreg:=getintregister(list,size);
  1633. a_load_const_reg(list,size,a,tmpreg);
  1634. a_op_reg_reg_reg_checkoverflow(list,op,size,tmpreg,src,dst,true,ovloc);
  1635. end;
  1636. procedure thlcgwasm.a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tdef; src1, src2, dst: tregister; setflags: boolean; var ovloc: tlocation);
  1637. var
  1638. orgsrc1, orgsrc2: tregister;
  1639. docheck: boolean;
  1640. lab: tasmlabel;
  1641. begin
  1642. if not setflags then
  1643. begin
  1644. inherited;
  1645. exit;
  1646. end;
  1647. { anything else cannot overflow }
  1648. docheck:=size.size in [4,8];
  1649. if docheck then
  1650. begin
  1651. orgsrc1:=src1;
  1652. orgsrc2:=src2;
  1653. if src1=dst then
  1654. begin
  1655. orgsrc1:=getintregister(list,size);
  1656. a_load_reg_reg(list,size,size,src1,orgsrc1);
  1657. end;
  1658. if src2=dst then
  1659. begin
  1660. orgsrc2:=getintregister(list,size);
  1661. a_load_reg_reg(list,size,size,src2,orgsrc2);
  1662. end;
  1663. end;
  1664. a_op_reg_reg_reg(list,op,size,src1,src2,dst);
  1665. if docheck then
  1666. begin
  1667. { * signed overflow for addition iff
  1668. - src1 and src2 are negative and result is positive (excep in case of
  1669. subtraction, then sign of src1 has to be inverted)
  1670. - src1 and src2 are positive and result is negative
  1671. -> Simplified boolean equivalent (in terms of sign bits):
  1672. not(src1 xor src2) and (src1 xor dst)
  1673. for subtraction, multiplication: invert src1 sign bit
  1674. for division: handle separately (div by zero, low(inttype) div -1),
  1675. not supported by this code
  1676. * unsigned overflow iff carry out, aka dst < src1 or dst < src2
  1677. }
  1678. location_reset(ovloc,LOC_REGISTER,OS_S32);
  1679. { not pasbool8, because then we'd still have to convert the integer to
  1680. a boolean via branches for Dalvik}
  1681. ovloc.register:=getintregister(list,s32inttype);
  1682. if not ((size.typ=pointerdef) or
  1683. ((size.typ=orddef) and
  1684. (torddef(size).ordtype in [u64bit,u16bit,u32bit,u8bit,uchar,
  1685. pasbool1,pasbool8,pasbool16,pasbool32,pasbool64]))) then
  1686. begin
  1687. a_load_reg_stack(list,size,src1);
  1688. if op in [OP_SUB,OP_IMUL] then
  1689. a_op_stack(list,OP_NOT,size);
  1690. a_op_reg_stack(list,OP_XOR,size,src2);
  1691. a_op_stack(list,OP_NOT,size);
  1692. a_load_reg_stack(list,size,src1);
  1693. a_op_reg_stack(list,OP_XOR,size,dst);
  1694. a_op_stack(list,OP_AND,size);
  1695. a_op_const_stack(list,OP_SHR,size,(size.size*8)-1);
  1696. if size.size=8 then
  1697. begin
  1698. //todo: any operands needed?
  1699. list.concat(taicpu.op_none(a_i32_wrap_i64));
  1700. end;
  1701. a_load_stack_reg(list,s32inttype,ovloc.register);
  1702. end
  1703. else
  1704. begin
  1705. current_asmdata.getjumplabel(lab);
  1706. { can be optimized by removing duplicate xor'ing to convert dst from
  1707. signed to unsigned quadrant }
  1708. list.concat(taicpu.op_none(a_block));
  1709. a_load_const_reg(list,s32inttype,0,ovloc.register);
  1710. a_cmp_reg_reg_label(list,size,OC_B,dst,src1,lab);
  1711. a_cmp_reg_reg_label(list,size,OC_B,dst,src2,lab);
  1712. a_load_const_reg(list,s32inttype,1,ovloc.register);
  1713. list.concat(taicpu.op_none(a_end_block));
  1714. a_label(list,lab);
  1715. end;
  1716. end
  1717. else
  1718. ovloc.loc:=LOC_VOID;
  1719. end;
  1720. procedure thlcgwasm.a_cmp_const_ref_label(list: TAsmList; size: tdef; cmp_op: topcmp; a: tcgint; const ref: treference; l: tasmlabel);
  1721. begin
  1722. a_cmp_const_ref_stack(list,size,cmp_op,a,ref);
  1723. current_asmdata.CurrAsmList.concat(taicpu.op_sym(a_br_if,l));
  1724. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  1725. end;
  1726. procedure thlcgwasm.a_cmp_const_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; a: tcgint; reg: tregister; l: tasmlabel);
  1727. begin
  1728. a_cmp_const_reg_stack(list,size,cmp_op,a,reg);
  1729. current_asmdata.CurrAsmList.concat(taicpu.op_sym(a_br_if,l));
  1730. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  1731. end;
  1732. procedure thlcgwasm.a_cmp_ref_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; const ref: treference; reg: tregister; l: tasmlabel);
  1733. begin
  1734. a_cmp_ref_reg_stack(list,size,cmp_op,ref,reg);
  1735. current_asmdata.CurrAsmList.concat(taicpu.op_sym(a_br_if,l));
  1736. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  1737. end;
  1738. procedure thlcgwasm.a_cmp_reg_ref_label(list: TAsmList; size: tdef; cmp_op: topcmp; reg: tregister; const ref: treference; l: tasmlabel);
  1739. begin
  1740. a_cmp_reg_ref_stack(list,size,cmp_op,reg,ref);
  1741. current_asmdata.CurrAsmList.concat(taicpu.op_sym(a_br_if,l));
  1742. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  1743. end;
  1744. procedure thlcgwasm.a_cmp_reg_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; reg1, reg2: tregister; l: tasmlabel);
  1745. begin
  1746. a_cmp_reg_reg_stack(list,size,cmp_op,reg1,reg2);
  1747. current_asmdata.CurrAsmList.concat(taicpu.op_sym(a_br_if,l));
  1748. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  1749. end;
  1750. procedure thlcgwasm.a_jmp_always(list: TAsmList; l: tasmlabel);
  1751. begin
  1752. if (l=current_procinfo.CurrBreakLabel) or
  1753. (l=current_procinfo.CurrContinueLabel) or
  1754. (l=current_procinfo.CurrExitLabel) then
  1755. list.concat(taicpu.op_sym(a_br,l))
  1756. else
  1757. begin
  1758. {$ifndef EXTDEBUG}
  1759. Internalerror(2019091806); // unexpected jump
  1760. {$endif EXTDEBUG}
  1761. list.concat(tai_comment.create(strpnew('Unable to find destination of label '+l.name)));
  1762. end;
  1763. end;
  1764. procedure thlcgwasm.a_jmp_always_pascal_goto(list: TAsmList; l: tasmlabel);
  1765. var
  1766. br_ins: taicpu;
  1767. begin
  1768. br_ins:=taicpu.op_sym(a_br,l);
  1769. br_ins.is_br_generated_by_goto:=true;
  1770. list.concat(br_ins);
  1771. end;
  1772. procedure thlcgwasm.a_loadfpu_ref_ref(list: TAsmList; fromsize, tosize: tdef; const ref1, ref2: treference);
  1773. var
  1774. dstack_slots: longint;
  1775. tmpref1, tmpref2: treference;
  1776. begin
  1777. tmpref1:=ref1;
  1778. tmpref2:=ref2;
  1779. dstack_slots:=prepare_stack_for_ref(list,tmpref2,false);
  1780. a_load_ref_stack(list,fromsize,tmpref1,prepare_stack_for_ref(list,tmpref1,false));
  1781. resizestackfpuval(list,def_cgsize(fromsize),def_cgsize(tosize));
  1782. a_load_stack_ref(list,tosize,tmpref2,dstack_slots);
  1783. end;
  1784. procedure thlcgwasm.a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; reg: tregister);
  1785. var
  1786. tmpref: treference;
  1787. begin
  1788. tmpref:=ref;
  1789. a_load_ref_stack(list,fromsize,tmpref,prepare_stack_for_ref(list,tmpref,false));
  1790. resizestackfpuval(list,def_cgsize(fromsize),def_cgsize(tosize));
  1791. a_load_stack_reg(list,tosize,reg);
  1792. end;
  1793. procedure thlcgwasm.a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tdef; reg: tregister; const ref: treference);
  1794. var
  1795. dstack_slots: longint;
  1796. tmpref: treference;
  1797. begin
  1798. tmpref:=ref;
  1799. dstack_slots:=prepare_stack_for_ref(list,tmpref,false);
  1800. a_load_reg_stack(list,fromsize,reg);
  1801. resizestackfpuval(list,def_cgsize(fromsize),def_cgsize(tosize));
  1802. a_load_stack_ref(list,tosize,tmpref,dstack_slots);
  1803. end;
  1804. procedure thlcgwasm.a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tdef; reg1, reg2: tregister);
  1805. begin
  1806. a_load_reg_stack(list,fromsize,reg1);
  1807. resizestackfpuval(list,def_cgsize(fromsize),def_cgsize(tosize));
  1808. a_load_stack_reg(list,tosize,reg2);
  1809. end;
  1810. procedure thlcgwasm.g_unreachable(list: TAsmList);
  1811. begin
  1812. list.Concat(taicpu.op_none(a_unreachable));
  1813. end;
  1814. procedure thlcgwasm.g_concatcopy(list: TAsmList; size: tdef; const source, dest: treference);
  1815. var
  1816. pd: tprocdef;
  1817. cgpara1,cgpara2,cgpara3 : TCGPara;
  1818. begin
  1819. if (source.base=NR_EVAL_STACK_BASE) or (source.base=NR_LOCAL_STACK_POINTER_REG) or
  1820. (source.index=NR_EVAL_STACK_BASE) or (source.index=NR_LOCAL_STACK_POINTER_REG) or
  1821. (dest.base=NR_EVAL_STACK_BASE) or (dest.base=NR_LOCAL_STACK_POINTER_REG) or
  1822. (dest.index=NR_EVAL_STACK_BASE) or (dest.index=NR_LOCAL_STACK_POINTER_REG) or
  1823. (size.size in [1,2,4,8]) then
  1824. inherited
  1825. else
  1826. begin
  1827. pd:=search_system_proc('MOVE');
  1828. cgpara1.init;
  1829. cgpara2.init;
  1830. cgpara3.init;
  1831. paramanager.getcgtempparaloc(list,pd,1,cgpara1);
  1832. paramanager.getcgtempparaloc(list,pd,2,cgpara2);
  1833. paramanager.getcgtempparaloc(list,pd,3,cgpara3);
  1834. if pd.is_pushleftright then
  1835. begin
  1836. { load source }
  1837. a_loadaddr_ref_cgpara(list,voidtype,source,cgpara1);
  1838. { load destination }
  1839. a_loadaddr_ref_cgpara(list,voidtype,dest,cgpara2);
  1840. { load size }
  1841. a_load_const_cgpara(list,sizesinttype,size.size,cgpara3);
  1842. end
  1843. else
  1844. begin
  1845. { load size }
  1846. a_load_const_cgpara(list,sizesinttype,size.size,cgpara3);
  1847. { load destination }
  1848. a_loadaddr_ref_cgpara(list,voidtype,dest,cgpara2);
  1849. { load source }
  1850. a_loadaddr_ref_cgpara(list,voidtype,source,cgpara1);
  1851. end;
  1852. paramanager.freecgpara(list,cgpara3);
  1853. paramanager.freecgpara(list,cgpara2);
  1854. paramanager.freecgpara(list,cgpara1);
  1855. g_call_system_proc(list,pd,[@cgpara1,@cgpara2,@cgpara3],nil).resetiftemp;
  1856. cgpara3.done;
  1857. cgpara2.done;
  1858. cgpara1.done;
  1859. end;
  1860. end;
  1861. procedure thlcgwasm.g_proc_entry(list: TAsmList; localsize: longint; nostackframe: boolean);
  1862. var
  1863. pd: tcpuprocdef;
  1864. begin
  1865. pd:=tcpuprocdef(current_procinfo.procdef);
  1866. g_procdef(list,pd);
  1867. if not nostackframe then
  1868. begin
  1869. ttgwasm(tg).allocframepointer(list,pd.frame_pointer_ref);
  1870. if pd.base_pointer_ref.base<>NR_LOCAL_STACK_POINTER_REG then
  1871. ttgwasm(tg).allocbasepointer(list,pd.base_pointer_ref);
  1872. g_fingerprint(list);
  1873. list.Concat(taicpu.op_sym(a_global_get,RefStackPointerSym));
  1874. incstack(list,1);
  1875. list.Concat(taicpu.op_ref(a_local_set,pd.base_pointer_ref));
  1876. decstack(list,1);
  1877. if (localsize>0) then begin
  1878. list.Concat(taicpu.op_ref(a_local_get,pd.base_pointer_ref));
  1879. incstack(list,1);
  1880. list.concat(taicpu.op_const(a_i32_const, localsize ));
  1881. incstack(list,1);
  1882. list.concat(taicpu.op_none(a_i32_sub));
  1883. decstack(list,1);
  1884. list.Concat(taicpu.op_ref(a_local_set,pd.frame_pointer_ref));
  1885. decstack(list,1);
  1886. list.Concat(taicpu.op_ref(a_local_get,pd.frame_pointer_ref));
  1887. incstack(list,1);
  1888. list.Concat(taicpu.op_sym(a_global_set,RefStackPointerSym));
  1889. decstack(list,1);
  1890. end;
  1891. end;
  1892. end;
  1893. procedure thlcgwasm.g_proc_exit(list: TAsmList; parasize: longint; nostackframe: boolean);
  1894. var
  1895. pd: tcpuprocdef;
  1896. begin
  1897. pd:=tcpuprocdef(current_procinfo.procdef);
  1898. if not nostackframe then
  1899. begin
  1900. list.Concat(taicpu.op_ref(a_local_get,pd.base_pointer_ref));
  1901. incstack(list,1);
  1902. list.Concat(taicpu.op_sym(a_global_set,RefStackPointerSym));
  1903. decstack(list,1);
  1904. list.concat(taicpu.op_none(a_return));
  1905. end;
  1906. list.concat(taicpu.op_none(a_end_function));
  1907. end;
  1908. procedure thlcgwasm.g_rangecheck(list: TAsmList; const l: tlocation; fromdef, todef: tdef);
  1909. var
  1910. {$if defined(cpuhighleveltarget)}
  1911. aintmax: tcgint;
  1912. {$elseif defined(cpu64bitalu) or defined(cpu32bitalu)}
  1913. aintmax: aint;
  1914. {$else}
  1915. aintmax: longint;
  1916. {$endif}
  1917. //neglabel : tasmlabel;
  1918. //hreg : tregister;
  1919. lto,hto,
  1920. lfrom,hfrom : TConstExprInt;
  1921. fromsize, tosize: cardinal;
  1922. maxdef: tdef;
  1923. from_signed, to_signed: boolean;
  1924. begin
  1925. { range checking on and range checkable value? }
  1926. if not(cs_check_range in current_settings.localswitches) or
  1927. not(fromdef.typ in [orddef,enumdef]) or
  1928. { C-style booleans can't really fail range checks, }
  1929. { all values are always valid }
  1930. is_cbool(todef) then
  1931. exit;
  1932. {$if not defined(cpuhighleveltarget) and not defined(cpu64bitalu)}
  1933. { handle 64bit rangechecks separate for 32bit processors }
  1934. if is_64bit(fromdef) or is_64bit(todef) then
  1935. begin
  1936. cg64.g_rangecheck64(list,l,fromdef,todef);
  1937. exit;
  1938. end;
  1939. {$endif ndef cpuhighleveltarget and ndef cpu64bitalu}
  1940. { only check when assigning to scalar, subranges are different, }
  1941. { when todef=fromdef then the check is always generated }
  1942. getrange(fromdef,lfrom,hfrom);
  1943. getrange(todef,lto,hto);
  1944. from_signed := is_signed(fromdef);
  1945. to_signed := is_signed(todef);
  1946. { check the rangedef of the array, not the array itself }
  1947. { (only change now, since getrange needs the arraydef) }
  1948. if (todef.typ = arraydef) then
  1949. todef := tarraydef(todef).rangedef;
  1950. { no range check if from and to are equal and are both longint/dword }
  1951. { (if we have a 32bit processor) or int64/qword, since such }
  1952. { operations can at most cause overflows (JM) }
  1953. { Note that these checks are mostly processor independent, they only }
  1954. { have to be changed once we introduce 64bit subrange types }
  1955. {$if defined(cpuhighleveltarget) or defined(cpu64bitalu)}
  1956. if (fromdef=todef) and
  1957. (fromdef.typ=orddef) and
  1958. (((((torddef(fromdef).ordtype=s64bit) and
  1959. (lfrom = low(int64)) and
  1960. (hfrom = high(int64))) or
  1961. ((torddef(fromdef).ordtype=u64bit) and
  1962. (lfrom = low(qword)) and
  1963. (hfrom = high(qword))) or
  1964. ((torddef(fromdef).ordtype=scurrency) and
  1965. (lfrom = low(int64)) and
  1966. (hfrom = high(int64)))))) then
  1967. exit;
  1968. {$endif cpuhighleveltarget or cpu64bitalu}
  1969. { 32 bit operations are automatically widened to 64 bit on 64 bit addr
  1970. targets }
  1971. {$ifdef cpu32bitaddr}
  1972. if (fromdef = todef) and
  1973. (fromdef.typ=orddef) and
  1974. (((((torddef(fromdef).ordtype = s32bit) and
  1975. (lfrom = int64(low(longint))) and
  1976. (hfrom = int64(high(longint)))) or
  1977. ((torddef(fromdef).ordtype = u32bit) and
  1978. (lfrom = low(cardinal)) and
  1979. (hfrom = high(cardinal)))))) then
  1980. exit;
  1981. {$endif cpu32bitaddr}
  1982. { optimize some range checks away in safe cases }
  1983. fromsize := fromdef.size;
  1984. tosize := todef.size;
  1985. if ((from_signed = to_signed) or
  1986. (not from_signed)) and
  1987. (lto<=lfrom) and (hto>=hfrom) and
  1988. (fromsize <= tosize) then
  1989. begin
  1990. { if fromsize < tosize, and both have the same signed-ness or }
  1991. { fromdef is unsigned, then all bit patterns from fromdef are }
  1992. { valid for todef as well }
  1993. if (fromsize < tosize) then
  1994. exit;
  1995. if (fromsize = tosize) and
  1996. (from_signed = to_signed) then
  1997. { only optimize away if all bit patterns which fit in fromsize }
  1998. { are valid for the todef }
  1999. begin
  2000. {$ifopt Q+}
  2001. {$define overflowon}
  2002. {$Q-}
  2003. {$endif}
  2004. {$ifopt R+}
  2005. {$define rangeon}
  2006. {$R-}
  2007. {$endif}
  2008. if to_signed then
  2009. begin
  2010. { calculation of the low/high ranges must not overflow 64 bit
  2011. otherwise we end up comparing with zero for 64 bit data types on
  2012. 64 bit processors }
  2013. if (lto = (int64(-1) << (tosize * 8 - 1))) and
  2014. (hto = (-((int64(-1) << (tosize * 8 - 1))+1))) then
  2015. exit
  2016. end
  2017. else
  2018. begin
  2019. { calculation of the low/high ranges must not overflow 64 bit
  2020. otherwise we end up having all zeros for 64 bit data types on
  2021. 64 bit processors }
  2022. if (lto = 0) and
  2023. (qword(hto) = (qword(-1) >> (64-(tosize * 8))) ) then
  2024. exit
  2025. end;
  2026. {$ifdef overflowon}
  2027. {$Q+}
  2028. {$undef overflowon}
  2029. {$endif}
  2030. {$ifdef rangeon}
  2031. {$R+}
  2032. {$undef rangeon}
  2033. {$endif}
  2034. end
  2035. end;
  2036. { depending on the types involved, we perform the range check for 64 or
  2037. for 32 bit }
  2038. if fromsize=8 then
  2039. maxdef:=fromdef
  2040. else
  2041. maxdef:=todef;
  2042. {$if sizeof(aintmax) = 8}
  2043. if maxdef.size=8 then
  2044. aintmax:=high(int64)
  2045. else
  2046. {$endif}
  2047. begin
  2048. aintmax:=high(longint);
  2049. maxdef:=u32inttype;
  2050. end;
  2051. { generate the rangecheck code for the def where we are going to }
  2052. { store the result }
  2053. { use the trick that }
  2054. { a <= x <= b <=> 0 <= x-a <= b-a <=> unsigned(x-a) <= unsigned(b-a) }
  2055. { To be able to do that, we have to make sure however that either }
  2056. { fromdef and todef are both signed or unsigned, or that we leave }
  2057. { the parts < 0 and > maxlongint out }
  2058. if from_signed xor to_signed then
  2059. begin
  2060. if from_signed then
  2061. { from is signed, to is unsigned }
  2062. begin
  2063. { if high(from) < 0 -> always range error }
  2064. if (hfrom < 0) or
  2065. { if low(to) > maxlongint also range error }
  2066. (lto > aintmax) then
  2067. begin
  2068. g_call_system_proc(list,'fpc_rangeerror',[],nil).resetiftemp;
  2069. exit
  2070. end;
  2071. { from is signed and to is unsigned -> when looking at to }
  2072. { as an signed value, it must be < maxaint (otherwise }
  2073. { it will become negative, which is invalid since "to" is unsigned) }
  2074. if hto > aintmax then
  2075. hto := aintmax;
  2076. end
  2077. else
  2078. { from is unsigned, to is signed }
  2079. begin
  2080. if (lfrom > aintmax) or
  2081. (hto < 0) then
  2082. begin
  2083. g_call_system_proc(list,'fpc_rangeerror',[],nil).resetiftemp;
  2084. exit
  2085. end;
  2086. { from is unsigned and to is signed -> when looking at to }
  2087. { as an unsigned value, it must be >= 0 (since negative }
  2088. { values are the same as values > maxlongint) }
  2089. if lto < 0 then
  2090. lto := 0;
  2091. end;
  2092. end;
  2093. a_load_loc_stack(list,fromdef,l);
  2094. resize_stack_int_val(list,fromdef,maxdef,false);
  2095. a_load_const_stack(list,maxdef,tcgint(int64(lto)),R_INTREGISTER);
  2096. a_op_stack(list,OP_SUB,maxdef);
  2097. {
  2098. if from_signed then
  2099. a_cmp_const_reg_label(list,OS_INT,OC_GTE,aint(hto-lto),hreg,neglabel)
  2100. else
  2101. }
  2102. if qword(hto-lto)>qword(aintmax) then
  2103. a_load_const_stack(list,maxdef,aintmax,R_INTREGISTER)
  2104. else
  2105. a_load_const_stack(list,maxdef,tcgint(int64(hto-lto)),R_INTREGISTER);
  2106. a_cmp_stack_stack(list,maxdef,OC_A);
  2107. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_if));
  2108. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  2109. g_call_system_proc(list,'fpc_rangeerror',[],nil).resetiftemp;
  2110. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_end_if));
  2111. end;
  2112. procedure thlcgwasm.g_overflowcheck(list: TAsmList; const Loc: tlocation; def: tdef);
  2113. begin
  2114. { not possible, need the original operands }
  2115. internalerror(2012102101);
  2116. end;
  2117. procedure thlcgwasm.g_overflowCheck_loc(List: TAsmList; const Loc: TLocation; def: TDef; var ovloc: tlocation);
  2118. var
  2119. hl : tasmlabel;
  2120. begin
  2121. if not(cs_check_overflow in current_settings.localswitches) then
  2122. exit;
  2123. current_asmdata.getjumplabel(hl);
  2124. list.concat(taicpu.op_none(a_block));
  2125. a_cmp_const_loc_label(list,s32inttype,OC_EQ,0,ovloc,hl);
  2126. g_call_system_proc(list,'fpc_overflow',[],nil);
  2127. hlcg.g_maybe_checkforexceptions(current_asmdata.CurrAsmList);
  2128. list.concat(taicpu.op_none(a_end_block));
  2129. a_label(list,hl);
  2130. end;
  2131. procedure thlcgwasm.maybe_change_load_node_reg(list: TAsmList; var n: tnode; reload: boolean);
  2132. begin
  2133. { don't do anything, all registers become stack locations anyway }
  2134. end;
  2135. procedure thlcgwasm.gen_entry_code(list: TAsmList);
  2136. begin
  2137. inherited;
  2138. if not (po_assembler in current_procinfo.procdef.procoptions) then
  2139. begin
  2140. list.concat(taicpu.op_none(a_block));
  2141. list.concat(taicpu.op_none(a_block));
  2142. end;
  2143. end;
  2144. procedure thlcgwasm.gen_exit_code(list: TAsmList);
  2145. begin
  2146. if not (po_assembler in current_procinfo.procdef.procoptions) then
  2147. begin
  2148. list.concat(taicpu.op_none(a_end_block));
  2149. if ts_wasm_bf_exceptions in current_settings.targetswitches then
  2150. a_label(list,tcpuprocinfo(current_procinfo).CurrRaiseLabel);
  2151. if fevalstackheight<>0 then
  2152. {$ifdef DEBUG_WASMSTACK}
  2153. list.concat(tai_comment.Create(strpnew('!!! values remaining on stack at end of block !!!')));
  2154. {$else DEBUG_WASMSTACK}
  2155. internalerror(2021091801);
  2156. {$endif DEBUG_WASMSTACK}
  2157. end;
  2158. inherited;
  2159. end;
  2160. procedure thlcgwasm.a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; srcsize, dstsize: tdef; src, dst: tregister);
  2161. begin
  2162. internalerror(2012090201);
  2163. end;
  2164. procedure thlcgwasm.a_loadmm_loc_reg(list: TAsmList; fromsize, tosize: tdef; const loc: tlocation; const reg: tregister; shuffle: pmmshuffle);
  2165. begin
  2166. internalerror(2012090202);
  2167. end;
  2168. procedure thlcgwasm.a_loadmm_reg_reg(list: TAsmList; fromsize, tosize: tdef; reg1, reg2: tregister; shuffle: pmmshuffle);
  2169. begin
  2170. internalerror(2012060130);
  2171. end;
  2172. procedure thlcgwasm.a_loadmm_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; reg: tregister; shuffle: pmmshuffle);
  2173. begin
  2174. internalerror(2012060131);
  2175. end;
  2176. procedure thlcgwasm.a_loadmm_reg_ref(list: TAsmList; fromsize, tosize: tdef; reg: tregister; const ref: treference; shuffle: pmmshuffle);
  2177. begin
  2178. internalerror(2012060132);
  2179. end;
  2180. procedure thlcgwasm.a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size: tdef; src, dst: tregister; shuffle: pmmshuffle);
  2181. begin
  2182. internalerror(2012060133);
  2183. end;
  2184. procedure thlcgwasm.a_loadmm_intreg_reg(list: TAsmList; fromsize, tosize: tdef; intreg, mmreg: tregister; shuffle: pmmshuffle);
  2185. begin
  2186. internalerror(2012060134);
  2187. end;
  2188. procedure thlcgwasm.a_loadmm_reg_intreg(list: TAsmList; fromsize, tosize: tdef; mmreg, intreg: tregister; shuffle: pmmshuffle);
  2189. begin
  2190. internalerror(2012060135);
  2191. end;
  2192. procedure thlcgwasm.g_stackpointer_alloc(list: TAsmList; size: longint);
  2193. begin
  2194. internalerror(2012090203);
  2195. end;
  2196. procedure thlcgwasm.g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);
  2197. begin
  2198. internalerror(2012090204);
  2199. end;
  2200. procedure thlcgwasm.g_adjust_self_value(list: TAsmList; procdef: tprocdef; ioffset: aint);
  2201. begin
  2202. internalerror(2012090205);
  2203. end;
  2204. procedure thlcgwasm.g_local_unwind(list: TAsmList; l: TAsmLabel);
  2205. begin
  2206. internalerror(2012090206);
  2207. end;
  2208. procedure thlcgwasm.g_procdef(list: TAsmList; pd: tprocdef);
  2209. begin
  2210. list.Concat(tai_functype.create(pd.mangledname,tcpuprocdef(pd).create_functype));
  2211. end;
  2212. procedure thlcgwasm.g_maybe_checkforexceptions(list: TasmList);
  2213. var
  2214. pd: tprocdef;
  2215. begin
  2216. if ts_wasm_bf_exceptions in current_settings.targetswitches then
  2217. begin
  2218. pd:=search_system_proc('fpc_raised_exception_flag');
  2219. g_call_system_proc(list,pd,[],nil).resetiftemp;
  2220. decstack(current_asmdata.CurrAsmList,1);
  2221. list.concat(taicpu.op_sym(a_br_if,tcpuprocinfo(current_procinfo).CurrRaiseLabel));
  2222. end;
  2223. end;
  2224. procedure thlcgwasm.a_load_stack_reg(list: TAsmList; size: tdef; reg: tregister);
  2225. begin
  2226. list.concat(taicpu.op_reg(a_local_set,reg));
  2227. decstack(list,1);
  2228. end;
  2229. procedure thlcgwasm.a_load_stack_ref(list: TAsmList; size: tdef; const ref: treference; extra_slots: longint);
  2230. var
  2231. opc: tasmop;
  2232. finishandval: tcgint;
  2233. begin
  2234. { fake location that indicates the value has to remain on the stack }
  2235. if ref.base=NR_EVAL_STACK_BASE then
  2236. exit;
  2237. opc:=loadstoreopcref(size,false,ref,finishandval);
  2238. if ref.refaddr=addr_got_tls then
  2239. list.concat(taicpu.op_const(opc,ref.offset))
  2240. else
  2241. list.concat(taicpu.op_ref(opc,ref));
  2242. { avoid problems with getting the size of an open array etc }
  2243. if wasmAlwayInMem(size) then
  2244. size:=ptruinttype;
  2245. decstack(list,1+extra_slots);
  2246. end;
  2247. procedure thlcgwasm.a_load_reg_stack(list: TAsmList; size: tdef; reg: tregister);
  2248. begin
  2249. list.concat(taicpu.op_reg(a_local_get,reg));
  2250. incstack(list,1);
  2251. end;
  2252. procedure thlcgwasm.a_load_ref_stack(list: TAsmList; size: tdef; const ref: treference; extra_slots: longint);
  2253. var
  2254. opc: tasmop;
  2255. finishandval: tcgint;
  2256. begin
  2257. { fake location that indicates the value is already on the stack? }
  2258. if (ref.base=NR_EVAL_STACK_BASE) then
  2259. exit;
  2260. opc:=loadstoreopcref(size,true,ref,finishandval);
  2261. if ref.refaddr=addr_got_tls then
  2262. list.concat(taicpu.op_const(opc,ref.offset))
  2263. else
  2264. list.concat(taicpu.op_ref(opc,ref));
  2265. { avoid problems with getting the size of an open array etc }
  2266. if wasmAlwayInMem(size) then
  2267. size:=ptruinttype;
  2268. incstack(list,1-extra_slots);
  2269. if finishandval<>-1 then
  2270. a_op_const_stack(list,OP_AND,size,finishandval);
  2271. // there's no cast check in Wasm
  2272. //if ref.checkcast then
  2273. // gen_typecheck(list,a_checkcast,size);
  2274. end;
  2275. procedure thlcgwasm.a_load_subsetref_stack(list : TAsmList;size: tdef; const sref: tsubsetreference);
  2276. var
  2277. tmpreg: TRegister;
  2278. begin
  2279. tmpreg:=getintregister(list,size);
  2280. a_load_subsetref_reg(list,size,size,sref,tmpreg);
  2281. a_load_reg_stack(list,size,tmpreg);
  2282. end;
  2283. function thlcgwasm.loadstoreopcref(def: tdef; isload: boolean; const ref: treference; out finishandval: tcgint): tasmop;
  2284. const
  2285. {iisload} {issigned}
  2286. getputmem8 : array [boolean, boolean] of TAsmOp = ((a_i32_store8, a_i32_store8), (a_i32_load8_u, a_i32_load8_s));
  2287. getputmem16 : array [boolean, boolean] of TAsmOp = ((a_i32_store16, a_i32_store16), (a_i32_load16_u ,a_i32_load16_s));
  2288. getputmem32 : array [boolean, boolean] of TAsmOp = ((a_i32_store, a_i32_store), (a_i32_load, a_i32_load));
  2289. getputmem64 : array [boolean, boolean] of TAsmOp = ((a_i64_store, a_i64_store), (a_i64_load, a_i64_load));
  2290. getputmemf32 : array [boolean] of TAsmOp = (a_f32_store, a_f32_load);
  2291. getputmemf64 : array [boolean] of TAsmOp = (a_f64_store, a_f64_load);
  2292. begin
  2293. if assigned(ref.symbol) and (ref.symbol.typ=AT_WASM_GLOBAL) then
  2294. begin
  2295. if isload then
  2296. result:=a_global_get
  2297. else
  2298. result:=a_global_set;
  2299. finishandval:=-1;
  2300. end
  2301. else if (ref.base<>NR_LOCAL_STACK_POINTER_REG) or assigned(ref.symbol) then
  2302. begin
  2303. { -> either a global (static) field, or a regular field. If a regular
  2304. field, then ref.base contains the self pointer, otherwise
  2305. ref.base=NR_NO. In both cases, the symbol contains all other
  2306. information (combined field name and type descriptor) }
  2307. case def.size of
  2308. 1: result := getputmem8[isload, is_signed(def)];
  2309. 2: result := getputmem16[isload, is_signed(def)];
  2310. 4:
  2311. if is_single(def) or ((def.typ=recorddef) and (trecorddef(def).contains_float_field)) then
  2312. result := getputmemf32[isload]
  2313. else
  2314. result := getputmem32[isload, is_signed(def)];
  2315. 8: if is_double(def) or ((def.typ=recorddef) and (trecorddef(def).contains_float_field)) then
  2316. result := getputmemf64[isload]
  2317. else
  2318. result := getputmem64[isload, is_signed(def)];
  2319. else
  2320. Internalerror(2019091501);
  2321. end;
  2322. //result:=getputopc[isload,ref.base=NR_NO];
  2323. finishandval:=-1;
  2324. { erase sign extension for byte/smallint loads }
  2325. if (def2regtyp(def)=R_INTREGISTER) and
  2326. not is_signed(def) and
  2327. (def.typ=orddef) and
  2328. not is_widechar(def) then
  2329. case def.size of
  2330. 1: if (torddef(def).high>127) then
  2331. finishandval:=255;
  2332. 2: if (torddef(def).high>32767) then
  2333. finishandval:=65535;
  2334. end;
  2335. end
  2336. else
  2337. begin
  2338. finishandval:=-1;
  2339. if isload then
  2340. result := a_local_get
  2341. else
  2342. result := a_local_set;
  2343. end;
  2344. end;
  2345. procedure thlcgwasm.resize_stack_int_val(list: TAsmList; fromsize, tosize: tdef; formemstore: boolean);
  2346. var
  2347. fromcgsize, tocgsize: tcgsize;
  2348. begin
  2349. { When storing to an array, field or global variable, make sure the
  2350. static type verification can determine that the stored value fits
  2351. within the boundaries of the declared type (to appease the Dalvik VM).
  2352. Local variables either get their type upgraded in the debug info,
  2353. or have no type information at all }
  2354. if formemstore and
  2355. (tosize.typ=orddef) then
  2356. if (torddef(tosize).ordtype in [u8bit,uchar]) then
  2357. tosize:=s8inttype
  2358. else if torddef(tosize).ordtype=u16bit then
  2359. tosize:=s16inttype;
  2360. fromcgsize:=def_cgsize(fromsize);
  2361. tocgsize:=def_cgsize(tosize);
  2362. if fromcgsize in [OS_S64,OS_64] then
  2363. begin
  2364. if not(tocgsize in [OS_S64,OS_64]) then
  2365. begin
  2366. { truncate }
  2367. list.concat(taicpu.op_none(a_i32_wrap_i64));
  2368. case tocgsize of
  2369. OS_8:
  2370. a_op_const_stack(list,OP_AND,s32inttype,255);
  2371. OS_S8:
  2372. list.concat(taicpu.op_none(a_i32_extend8_s));
  2373. OS_16:
  2374. a_op_const_stack(list,OP_AND,s32inttype,65535);
  2375. OS_S16:
  2376. list.concat(taicpu.op_none(a_i32_extend16_s));
  2377. OS_32,OS_S32:
  2378. ;
  2379. else
  2380. internalerror(2021012201);
  2381. end;
  2382. end;
  2383. end
  2384. else if tocgsize in [OS_S64,OS_64] then
  2385. begin
  2386. { extend }
  2387. case fromcgsize of
  2388. OS_8:
  2389. begin
  2390. a_op_const_stack(list,OP_AND,s32inttype,255);
  2391. list.concat(taicpu.op_none(a_i64_extend_i32_u));
  2392. end;
  2393. OS_S8:
  2394. begin
  2395. list.concat(taicpu.op_none(a_i64_extend_i32_u));
  2396. list.concat(taicpu.op_none(a_i64_extend8_s));
  2397. end;
  2398. OS_16:
  2399. begin
  2400. a_op_const_stack(list,OP_AND,s32inttype,65535);
  2401. list.concat(taicpu.op_none(a_i64_extend_i32_u));
  2402. end;
  2403. OS_S16:
  2404. begin
  2405. list.concat(taicpu.op_none(a_i64_extend_i32_u));
  2406. list.concat(taicpu.op_none(a_i64_extend16_s));
  2407. end;
  2408. OS_32:
  2409. list.concat(taicpu.op_none(a_i64_extend_i32_u));
  2410. OS_S32:
  2411. list.concat(taicpu.op_none(a_i64_extend_i32_s));
  2412. OS_64,OS_S64:
  2413. ;
  2414. else
  2415. internalerror(2021010301);
  2416. end;
  2417. end
  2418. else
  2419. begin
  2420. if tcgsize2size[fromcgsize]<tcgsize2size[tocgsize] then
  2421. begin
  2422. { extend }
  2423. case fromcgsize of
  2424. OS_8:
  2425. a_op_const_stack(list,OP_AND,s32inttype,255);
  2426. OS_S8:
  2427. begin
  2428. list.concat(taicpu.op_none(a_i32_extend8_s));
  2429. if tocgsize=OS_16 then
  2430. a_op_const_stack(list,OP_AND,s32inttype,65535);
  2431. end;
  2432. OS_16:
  2433. a_op_const_stack(list,OP_AND,s32inttype,65535);
  2434. OS_S16:
  2435. list.concat(taicpu.op_none(a_i32_extend16_s));
  2436. OS_32,OS_S32:
  2437. ;
  2438. else
  2439. internalerror(2021010302);
  2440. end;
  2441. end
  2442. else if tcgsize2size[fromcgsize]>=tcgsize2size[tocgsize] then
  2443. begin
  2444. { truncate }
  2445. case tocgsize of
  2446. OS_8:
  2447. a_op_const_stack(list,OP_AND,s32inttype,255);
  2448. OS_S8:
  2449. list.concat(taicpu.op_none(a_i32_extend8_s));
  2450. OS_16:
  2451. a_op_const_stack(list,OP_AND,s32inttype,65535);
  2452. OS_S16:
  2453. list.concat(taicpu.op_none(a_i32_extend16_s));
  2454. OS_32,OS_S32:
  2455. ;
  2456. else
  2457. internalerror(2021010302);
  2458. end;
  2459. end;
  2460. end;
  2461. end;
  2462. procedure thlcgwasm.maybe_resize_stack_para_val(list: TAsmList; retdef: tdef; callside: boolean);
  2463. var
  2464. convsize: tdef;
  2465. begin
  2466. if (retdef.typ=orddef) then
  2467. begin
  2468. if (torddef(retdef).ordtype in [u8bit,u16bit,uchar]) and
  2469. (torddef(retdef).high>=(1 shl (retdef.size*8-1))) then
  2470. begin
  2471. convsize:=nil;
  2472. if callside then
  2473. if torddef(retdef).ordtype in [u8bit,uchar] then
  2474. convsize:=s8inttype
  2475. else
  2476. convsize:=s16inttype
  2477. else if torddef(retdef).ordtype in [u8bit,uchar] then
  2478. convsize:=u8inttype
  2479. else
  2480. convsize:=u16inttype;
  2481. if assigned(convsize) then
  2482. resize_stack_int_val(list,s32inttype,convsize,false);
  2483. end;
  2484. end;
  2485. end;
  2486. procedure thlcgwasm.g_adjust_stack_after_call(list: TAsmList; pd: tabstractprocdef);
  2487. var
  2488. totalremovesize: longint;
  2489. realresdef: tdef;
  2490. ft: TWasmFuncType;
  2491. begin
  2492. if pd.typ=procvardef then
  2493. ft:=tcpuprocvardef(pd).create_functype
  2494. else
  2495. ft:=tcpuprocdef(pd).create_functype;
  2496. totalremovesize:=Length(ft.params)-Length(ft.results);
  2497. { remove parameters from internal evaluation stack counter (in case of
  2498. e.g. no parameters and a result, it can also increase) }
  2499. if totalremovesize>0 then
  2500. decstack(list,totalremovesize)
  2501. else if totalremovesize<0 then
  2502. incstack(list,-totalremovesize);
  2503. ft.free;
  2504. end;
  2505. procedure thlcgwasm.g_fingerprint(list: TAsmList);
  2506. begin
  2507. list.concat(taicpu.op_const(a_i64_const,Random(high(int64))));
  2508. list.concat(taicpu.op_const(a_i64_const,Random(high(int64))));
  2509. list.concat(taicpu.op_const(a_i64_const,Random(high(int64))));
  2510. list.concat(taicpu.op_const(a_i64_const,Random(high(int64))));
  2511. list.concat(taicpu.op_none(a_drop));
  2512. list.concat(taicpu.op_none(a_drop));
  2513. list.concat(taicpu.op_none(a_drop));
  2514. list.concat(taicpu.op_none(a_drop));
  2515. end;
  2516. procedure thlcgwasm.resizestackfpuval(list: TAsmList; fromsize, tosize: tcgsize);
  2517. begin
  2518. if (fromsize=OS_F32) and
  2519. (tosize=OS_F64) then
  2520. begin
  2521. list.concat(taicpu.op_none(a_f64_promote_f32));
  2522. end
  2523. else if (fromsize=OS_F64) and
  2524. (tosize=OS_F32) then
  2525. begin
  2526. list.concat(taicpu.op_none(a_f32_demote_f64));
  2527. end;
  2528. end;
  2529. procedure create_hlcodegen_cpu;
  2530. begin
  2531. hlcg:=thlcgwasm.create;
  2532. create_codegen;
  2533. end;
  2534. initialization
  2535. chlcgobj:=thlcgwasm;
  2536. create_hlcodegen:=@create_hlcodegen_cpu;
  2537. end.