hlcgcpu.pas 105 KB

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