hlcgcpu.pas 106 KB

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