hlcgcpu.pas 109 KB

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