hlcgcpu.pas 105 KB

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