hlcgcpu.pas 117 KB

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