hlcgcpu.pas 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917
  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. public
  34. br_blocks: integer;
  35. loopContBr: integer; // the value is different depending of the condition test
  36. // if it's in the beggning the jump should be done to the loop (1)
  37. // if the condition at the end, the jump should done to the end of block (0)
  38. loopBreakBr: integer;
  39. exitBr: integer;
  40. fntypelookup : TWasmProcTypeLookup;
  41. constructor create;
  42. destructor Destroy; override;
  43. procedure incblock;
  44. procedure decblock;
  45. procedure incstack(list : TAsmList;slots: longint);
  46. procedure decstack(list : TAsmList;slots: longint);
  47. procedure a_load_const_cgpara(list : TAsmList;tosize : tdef;a : tcgint;const cgpara : TCGPara);override;
  48. function a_call_name(list : TAsmList;pd : tprocdef;const s : TSymStr; const paras: array of pcgpara; forceresdef: tdef; weak: boolean): tcgpara;override;
  49. function a_call_reg(list: TAsmList; pd: tabstractprocdef; reg: tregister; const paras: array of pcgpara): tcgpara; override;
  50. { move instructions - a_load_FROM_TO }
  51. procedure a_load_const_reg(list : TAsmList;tosize : tdef;a : tcgint;register : tregister);override;
  52. procedure a_load_const_ref(list : TAsmList;tosize : tdef;a : tcgint;const ref : treference);override;
  53. procedure a_load_reg_ref(list : TAsmList;fromsize, tosize : tdef;register : tregister;const ref : treference);override;
  54. procedure a_load_reg_reg(list : TAsmList;fromsize, tosize : tdef;reg1,reg2 : tregister);override;
  55. procedure a_load_ref_reg(list : TAsmList;fromsize, tosize : tdef;const ref : treference;register : tregister);override;
  56. procedure a_load_ref_ref(list : TAsmList;fromsize, tosize : tdef;const sref : treference;const dref : treference);override;
  57. procedure a_loadaddr_ref_reg(list : TAsmList;fromsize, tosize : tdef;const ref : treference;r : tregister);override;
  58. { basic arithmetic operations }
  59. procedure a_op_const_reg(list: TAsmList; Op: TOpCG; size: tdef; a: tcgint; reg: TRegister); override;
  60. procedure a_op_const_reg_reg(list: TAsmList; op: TOpCg; size: tdef; a: tcgint; src, dst: tregister); override;
  61. procedure a_op_const_ref(list: TAsmList; Op: TOpCG; size: tdef; a: tcgint; const ref: TReference); override;
  62. procedure a_op_ref_reg(list: TAsmList; Op: TOpCG; size: tdef; const ref: TReference; reg: TRegister); override;
  63. procedure a_op_reg_reg_reg(list: TAsmList; op: TOpCg; size: tdef; src1, src2, dst: tregister); override;
  64. procedure a_op_reg_reg(list: TAsmList; Op: TOpCG; size: tdef; reg1, reg2: TRegister); override;
  65. procedure a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tdef; a: tcgint; src, dst: tregister;setflags : boolean;var ovloc : tlocation); override;
  66. procedure a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tdef; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation); override;
  67. procedure a_cmp_const_ref_label(list: TAsmList; size: tdef; cmp_op: topcmp; a: tcgint; const ref: treference; l: tasmlabel); override;
  68. procedure a_cmp_const_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; a: tcgint; reg: tregister; l: tasmlabel); override;
  69. procedure a_cmp_ref_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; const ref: treference; reg: tregister; l: tasmlabel); override;
  70. procedure a_cmp_reg_ref_label(list: TAsmList; size: tdef; cmp_op: topcmp; reg: tregister; const ref: treference; l: tasmlabel); override;
  71. procedure a_cmp_reg_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; reg1, reg2: tregister; l: tasmlabel); override;
  72. procedure a_jmp_always(list : TAsmList;l: tasmlabel); override;
  73. procedure a_loadfpu_ref_ref(list: TAsmList; fromsize, tosize: tdef; const ref1, ref2: treference); override;
  74. procedure a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; reg: tregister); override;
  75. procedure a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tdef; reg: tregister; const ref: treference); override;
  76. procedure a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tdef; reg1, reg2: tregister); override;
  77. procedure g_concatcopy(list : TAsmList;size: tdef; const source,dest : treference); override;
  78. procedure g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean); override;
  79. procedure g_proc_exit(list : TAsmList;parasize:longint;nostackframe:boolean); override;
  80. procedure g_overflowcheck(list: TAsmList; const Loc: tlocation; def: tdef); override;
  81. procedure g_overflowCheck_loc(List:TAsmList;const Loc:TLocation;def:TDef;var ovloc : tlocation); override;
  82. procedure maybe_change_load_node_reg(list: TAsmList; var n: tnode; reload: boolean); override;
  83. procedure gen_entry_code(list: TAsmList); override;
  84. procedure gen_exit_code(list: TAsmList); override;
  85. { unimplemented/unnecessary routines }
  86. procedure a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; srcsize, dstsize: tdef; src, dst: tregister); override;
  87. procedure a_loadmm_loc_reg(list: TAsmList; fromsize, tosize: tdef; const loc: tlocation; const reg: tregister; shuffle: pmmshuffle); override;
  88. procedure a_loadmm_reg_reg(list: TAsmList; fromsize, tosize: tdef; reg1, reg2: tregister; shuffle: pmmshuffle); override;
  89. procedure a_loadmm_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; reg: tregister; shuffle: pmmshuffle); override;
  90. procedure a_loadmm_reg_ref(list: TAsmList; fromsize, tosize: tdef; reg: tregister; const ref: treference; shuffle: pmmshuffle); override;
  91. procedure a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size: tdef; src, dst: tregister; shuffle: pmmshuffle); override;
  92. procedure a_loadmm_intreg_reg(list: TAsmList; fromsize, tosize: tdef; intreg, mmreg: tregister; shuffle: pmmshuffle); override;
  93. procedure a_loadmm_reg_intreg(list: TAsmList; fromsize, tosize: tdef; mmreg, intreg: tregister; shuffle: pmmshuffle); override;
  94. procedure g_stackpointer_alloc(list: TAsmList; size: longint); override;
  95. procedure g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint); override;
  96. procedure g_adjust_self_value(list: TAsmList; procdef: tprocdef; ioffset: aint); override;
  97. procedure g_local_unwind(list: TAsmList; l: TAsmLabel); override;
  98. { Wasm-specific routines }
  99. procedure g_procdef(list:TAsmList;pd: tprocdef);
  100. procedure a_load_stack_reg(list : TAsmList;size: tdef;reg: tregister);
  101. { extra_slots are the slots that are used by the reference, and that
  102. will be removed by the store operation }
  103. procedure a_load_stack_ref(list : TAsmList;size: tdef;const ref: treference;extra_slots: longint);
  104. procedure a_load_reg_stack(list : TAsmList;size: tdef;reg: tregister);
  105. { extra_slots are the slots that are used by the reference, and that
  106. will be removed by the load operation }
  107. procedure a_load_ref_stack(list : TAsmList;size: tdef;const ref: treference;extra_slots: longint);
  108. procedure a_load_const_stack(list : TAsmList;size: tdef;a :tcgint; typ: TRegisterType);
  109. procedure a_loadaddr_ref_stack(list : TAsmList;fromsize, tosize : tdef;const ref : treference);
  110. procedure a_load_stack_loc(list : TAsmList;size: tdef;const loc: tlocation);
  111. procedure a_load_loc_stack(list : TAsmList;size: tdef;const loc: tlocation);
  112. procedure a_loadfpu_const_stack(list : TAsmList;size: tdef;a :double);
  113. procedure a_op_stack(list : TAsmList;op: topcg; size: tdef);
  114. procedure a_op_const_stack(list : TAsmList;op: topcg; size: tdef;a : tcgint);
  115. procedure a_op_reg_stack(list : TAsmList;op: topcg; size: tdef;reg: tregister);
  116. procedure a_op_ref_stack(list : TAsmList;op: topcg; size: tdef;const ref: treference);
  117. procedure a_op_loc_stack(list : TAsmList;op: topcg; size: tdef;const loc: tlocation);
  118. procedure a_cmp_const_loc_stack(list: TAsmList; size: tdef;cmp_op: topcmp; a: tcgint; const loc: tlocation);
  119. procedure a_cmp_const_ref_stack(list: TAsmList; size: tdef; cmp_op: topcmp; a: tcgint; const ref: treference);
  120. procedure a_cmp_const_reg_stack(list: TAsmList; size: tdef; cmp_op: topcmp; a: tcgint; reg: tregister);
  121. procedure a_cmp_ref_reg_stack(list: TAsmList; size: tdef; cmp_op: topcmp; const ref: treference; reg: tregister);
  122. procedure a_cmp_reg_ref_stack(list: TAsmList; size: tdef; cmp_op: topcmp; reg: tregister; const ref: treference);
  123. procedure a_cmp_reg_reg_stack(list: TAsmList; size: tdef; cmp_op: topcmp; reg1, reg2: tregister);
  124. procedure a_cmp_subsetreg_reg_stack(list: TAsmList; fromsubsetsize, cmpsize: tdef; cmp_op: topcmp; const sreg: tsubsetregister; reg: tregister);
  125. procedure a_cmp_subsetref_reg_stack(list: TAsmList; fromsubsetsize, cmpsize: tdef; cmp_op: topcmp; const sref: tsubsetreference; reg: tregister);
  126. procedure a_cmp_loc_reg_stack(list : TAsmList;size : tdef;cmp_op : topcmp; const loc: tlocation; reg : tregister);
  127. procedure a_cmp_reg_loc_stack(list : TAsmList;size : tdef;cmp_op : topcmp; reg: tregister; const loc: tlocation);
  128. procedure a_cmp_ref_loc_stack(list: TAsmList; size: tdef;cmp_op: topcmp; const ref: treference; const loc: tlocation);
  129. procedure a_cmp_const_loc_br(list: TAsmList; size: tdef;cmp_op: topcmp; a: tcgint; const loc: tlocation; br: Integer);
  130. procedure a_cmp_const_ref_br(list: TAsmList; size: tdef; cmp_op: topcmp; a: tcgint; const ref: treference; br: Integer);
  131. procedure a_cmp_const_reg_br(list: TAsmList; size: tdef; cmp_op: topcmp; a: tcgint; reg: tregister; br: Integer);
  132. procedure a_cmp_ref_reg_br(list: TAsmList; size: tdef; cmp_op: topcmp; const ref: treference; reg: tregister; br: Integer);
  133. procedure a_cmp_reg_ref_br(list: TAsmList; size: tdef; cmp_op: topcmp; reg: tregister; const ref: treference; br: Integer);
  134. procedure a_cmp_reg_reg_br(list: TAsmList; size: tdef; cmp_op: topcmp; reg1, reg2: tregister; br: Integer);
  135. procedure a_cmp_subsetreg_reg_br(list: TAsmList; fromsubsetsize, cmpsize: tdef; cmp_op: topcmp; const sreg: tsubsetregister; reg: tregister; br: Integer);
  136. procedure a_cmp_subsetref_reg_br(list: TAsmList; fromsubsetsize, cmpsize: tdef; cmp_op: topcmp; const sref: tsubsetreference; reg: tregister; br: Integer);
  137. procedure a_cmp_loc_reg_br(list : TAsmList;size : tdef;cmp_op : topcmp; const loc: tlocation; reg : tregister; br: Integer);
  138. procedure a_cmp_reg_loc_br(list : TAsmList;size : tdef;cmp_op : topcmp; reg: tregister; const loc: tlocation; br: Integer);
  139. procedure a_cmp_ref_loc_br(list: TAsmList; size: tdef;cmp_op: topcmp; const ref: treference; const loc: tlocation; br: Integer);
  140. procedure g_reference_loc(list: TAsmList; def: tdef; const fromloc: tlocation; out toloc: tlocation); override;
  141. { this routine expects that all values are already massaged into the
  142. required form (sign bits xor'ed for gt/lt comparisons for OS_32/OS_64,
  143. see http://stackoverflow.com/questions/4068973/c-performing-signed-comparison-in-unsigned-variables-without-casting ) }
  144. procedure a_cmp_stack_stack(list : TAsmlist; size: tdef; cmp_op: topcmp);
  145. { these 2 routines perform the massaging expected by the previous one }
  146. procedure maybe_adjust_cmp_stackval(list : TAsmlist; size: tdef; cmp_op: topcmp);
  147. function maybe_adjust_cmp_constval(size: tdef; cmp_op: topcmp; a: tcgint): tcgint;
  148. { truncate/sign extend after performing operations on values < 32 bit
  149. that may have overflowed outside the range }
  150. procedure maybe_adjust_op_result(list: TAsmList; op: TOpCg; size: tdef);
  151. { performs sign/zero extension as required }
  152. procedure resize_stack_int_val(list: TAsmList;fromsize,tosize: tdef; formemstore: boolean);
  153. { 8/16 bit unsigned parameters and return values must be sign-extended on
  154. the producer side, because the JVM does not support unsigned variants;
  155. then they have to be zero-extended again on the consumer side }
  156. procedure maybe_resize_stack_para_val(list: TAsmList; retdef: tdef; callside: boolean);
  157. { adjust the stack height after a call based on the specified number of
  158. slots used for parameters and the provided resultdef }
  159. procedure g_adjust_stack_after_call(list: TAsmList; pd: tabstractprocdef);
  160. { because WebAssembly has no spec for any sort of debug info, and the
  161. only linker that we support (LLVM's wasm-ld) does not support creating
  162. map files in its stable version, and crashes when attempting to create
  163. a map file in its development version from git, we have no way to
  164. identify which procedure a crash occurred in. So, to identify the
  165. procedure, we call this procedure on proc entry, which generates a few
  166. useless loads of random numbers on the stack, that are immediately
  167. discarded, so they are essentially equivalent to a nop. This allows
  168. finding the procedure in the FPC output assembly, produced with -al by
  169. searching for these random numbers, as taken from the disassembly of the
  170. final binary. }
  171. procedure g_fingerprint(list: TAsmList);
  172. property maxevalstackheight: longint read fmaxevalstackheight;
  173. protected
  174. procedure gen_load_uninitialized_function_result(list: TAsmList; pd: tprocdef; resdef: tdef; const resloc: tcgpara); override;
  175. //procedure g_copyvalueparas(p: TObject; arg: pointer); override;
  176. procedure inittempvariables(list:TAsmList);override;
  177. function g_call_system_proc_intern(list: TAsmList; pd: tprocdef; const paras: array of pcgpara; forceresdef: tdef): tcgpara; override;
  178. public
  179. { in case of an array, the array base address and index have to be
  180. put on the evaluation stack before the stored value; similarly, for
  181. fields the self pointer has to be loaded first. Also checks whether
  182. the reference is valid. If dup is true, the necessary values are stored
  183. twice. Returns how many stack slots have been consumed, disregarding
  184. the "dup". }
  185. function prepare_stack_for_ref(list: TAsmList; var ref: treference; dup: boolean): longint;
  186. protected
  187. { return the load/store opcode to load/store from/to ref; if the result
  188. has to be and'ed after a load to get the final value, that constant
  189. is returned in finishandval (otherwise that value is set to -1) }
  190. function loadstoreopcref(def: tdef; isload: boolean; const ref: treference; out finishandval: tcgint): tasmop;
  191. procedure resizestackfpuval(list: TAsmList; fromsize, tosize: tcgsize);
  192. end;
  193. implementation
  194. uses
  195. verbose,cutils,globals,fmodule,constexp,
  196. defutil,
  197. aasmtai,aasmcpu,
  198. symtable,symcpu,
  199. procinfo,cpuinfo,cgcpu,tgobj,tgcpu,paramgr;
  200. const
  201. TOpCG2IAsmOp : array[topcg] of TAsmOp=(
  202. A_None, {OP_NONE}
  203. A_None, {OP_MOVE, replaced operation with direct load }
  204. a_i32_add, {OP_ADD, simple addition }
  205. a_i32_and, {OP_AND, simple logical and }
  206. a_i32_div_u, {OP_DIV, simple unsigned division }
  207. a_i32_div_s, {OP_IDIV, simple signed division }
  208. a_i32_mul, {OP_IMUL, simple signed multiply }
  209. a_i32_mul, {OP_MUL, simple unsigned multiply }
  210. A_None, {OP_NEG, simple negate } // neg = xor + 1
  211. A_None, {OP_NOT, simple logical not } // not = xor - 1
  212. a_i32_or, {OP_OR, simple logical or }
  213. a_i32_shr_s, {OP_SAR, arithmetic shift-right }
  214. a_i32_shl, {OP_SHL, logical shift left }
  215. a_i32_shr_u, {OP_SHR, logical shift right }
  216. a_i32_sub, {OP_SUB, simple subtraction }
  217. a_i32_xor, {OP_XOR, simple exclusive or }
  218. a_i32_rotl, {OP_ROL, rotate left }
  219. a_i32_rotr {OP_ROR rotate right }
  220. );
  221. TOpCG2LAsmOp : array[topcg] of TAsmOp=(
  222. A_None, {OP_NONE}
  223. a_i64_load, {OP_MOVE, replaced operation with direct load }
  224. a_i64_add, {OP_ADD, simple addition }
  225. a_i64_and, {OP_AND, simple logical and }
  226. a_i64_div_u, {OP_DIV, simple unsigned division }
  227. a_i64_div_s, {OP_IDIV, simple signed division }
  228. a_i64_mul, {OP_IMUL, simple signed multiply }
  229. a_i64_mul, {OP_MUL, simple unsigned multiply }
  230. A_None, {OP_NEG, simple negate } // neg = xor + 1
  231. A_None, {OP_NOT, simple logical not } // not = xor - 1
  232. a_i64_or, {OP_OR, simple logical or }
  233. a_i64_shr_s, {OP_SAR, arithmetic shift-right }
  234. a_i64_shl, {OP_SHL, logical shift left }
  235. a_i64_shr_u, {OP_SHR, logical shift right }
  236. a_i64_sub, {OP_SUB, simple subtraction }
  237. a_i64_xor, {OP_XOR, simple exclusive or }
  238. a_i64_rotl, {OP_ROL, rotate left }
  239. a_i64_rotr {OP_ROR rotate right }
  240. );
  241. constructor thlcgwasm.create;
  242. begin
  243. fevalstackheight:=0;
  244. fmaxevalstackheight:=0;
  245. fntypelookup:=TWasmProcTypeLookup.Create;
  246. end;
  247. destructor thlcgwasm.Destroy;
  248. begin
  249. fntypelookup.Free;
  250. inherited Destroy;
  251. end;
  252. procedure thlcgwasm.incblock;
  253. begin
  254. inc(br_blocks);
  255. end;
  256. procedure thlcgwasm.decblock;
  257. begin
  258. dec(br_blocks);
  259. if br_blocks<0 then Internalerror(2019091807); // out of block
  260. end;
  261. procedure thlcgwasm.incstack(list: TAsmList; slots: longint);
  262. begin
  263. if slots=0 then
  264. exit;
  265. inc(fevalstackheight,slots);
  266. if (fevalstackheight>fmaxevalstackheight) then
  267. fmaxevalstackheight:=fevalstackheight;
  268. if cs_asm_regalloc in current_settings.globalswitches then
  269. list.concat(tai_comment.Create(strpnew(' allocated '+tostr(slots)+', stack height = '+tostr(fevalstackheight))));
  270. end;
  271. procedure thlcgwasm.decstack(list: TAsmList;slots: longint);
  272. begin
  273. if slots=0 then
  274. exit;
  275. dec(fevalstackheight,slots);
  276. if (fevalstackheight<0) and
  277. not(cs_no_regalloc in current_settings.globalswitches) then
  278. internalerror(2010120501);
  279. if cs_asm_regalloc in current_settings.globalswitches then
  280. list.concat(tai_comment.Create(strpnew(' freed '+tostr(slots)+', stack height = '+tostr(fevalstackheight))));
  281. end;
  282. procedure thlcgwasm.a_load_const_cgpara(list: TAsmList; tosize: tdef; a: tcgint; const cgpara: TCGPara);
  283. begin
  284. tosize:=get_para_push_size(tosize);
  285. if tosize=s8inttype then
  286. a:=shortint(a)
  287. else if tosize=s16inttype then
  288. a:=smallint(a);
  289. inherited a_load_const_cgpara(list, tosize, a, cgpara);
  290. end;
  291. function thlcgwasm.a_call_name(list: TAsmList; pd: tprocdef; const s: TSymStr; const paras: array of pcgpara; forceresdef: tdef; weak: boolean): tcgpara;
  292. begin
  293. list.concat(taicpu.op_sym(a_call,current_asmdata.RefAsmSymbol(s,AT_FUNCTION)));
  294. result:=get_call_result_cgpara(pd,forceresdef);
  295. end;
  296. function thlcgwasm.a_call_reg(list: TAsmList; pd: tabstractprocdef; reg: tregister; const paras: array of pcgpara): tcgpara;
  297. begin
  298. a_load_reg_stack(list, ptrsinttype, reg);
  299. current_asmdata.CurrAsmList.Concat(taicpu.op_functype(a_call_indirect,tcpuprocdef(pd).create_functype));
  300. decstack(list,1);
  301. result:=hlcg.get_call_result_cgpara(pd, nil);
  302. end;
  303. procedure thlcgwasm.a_load_const_stack(list : TAsmList;size : tdef;a : tcgint; typ: TRegisterType);
  304. begin
  305. case typ of
  306. R_INTREGISTER,
  307. R_ADDRESSREGISTER:
  308. begin
  309. case def_cgsize(size) of
  310. OS_8,OS_16,OS_32,
  311. OS_S8,OS_S16,OS_S32:
  312. begin
  313. { convert cardinals to longints }
  314. list.concat(taicpu.op_const(a_i32_const, a));
  315. end;
  316. OS_64,OS_S64:
  317. begin
  318. list.concat(taicpu.op_const(a_i64_const, a));
  319. end;
  320. else
  321. internalerror(2010110702);
  322. end;
  323. end;
  324. else
  325. internalerror(2010110703);
  326. end;
  327. incstack(list,1);
  328. end;
  329. procedure thlcgwasm.a_loadaddr_ref_stack(list : TAsmList;fromsize, tosize : tdef;const ref : treference);
  330. var
  331. tmpref: treference;
  332. begin
  333. { you can't take the address of references, that are on the local stack }
  334. if (ref.base=NR_EVAL_STACK_BASE) or (ref.index=NR_EVAL_STACK_BASE) or
  335. (ref.base=NR_LOCAL_STACK_POINTER_REG) or (ref.index=NR_LOCAL_STACK_POINTER_REG) then
  336. internalerror(2021010101);
  337. tmpref:=ref;
  338. tmpref.base:=NR_NO;
  339. tmpref.index:=NR_NO;
  340. list.Concat(taicpu.op_ref(a_i32_const, tmpref));
  341. incstack(list, 1);
  342. if ref.base<>NR_NO then
  343. begin
  344. list.Concat(taicpu.op_reg(a_local_get,ref.base));
  345. incstack(list, 1);
  346. list.Concat(taicpu.op_none(a_i32_add));
  347. decstack(list, 1);
  348. end;
  349. if ref.index<>NR_NO then
  350. begin
  351. list.Concat(taicpu.op_reg(a_local_get,ref.index));
  352. incstack(list, 1);
  353. if ref.scalefactor>1 then
  354. begin
  355. list.Concat(taicpu.op_const(a_i32_const,ref.scalefactor));
  356. incstack(list, 1);
  357. list.Concat(taicpu.op_none(a_i32_mul));
  358. decstack(list, 1);
  359. end;
  360. list.Concat(taicpu.op_none(a_i32_add));
  361. decstack(list, 1);
  362. end;
  363. end;
  364. procedure thlcgwasm.a_load_stack_loc(list: TAsmList; size: tdef; const loc: tlocation);
  365. var
  366. tmpref: treference;
  367. begin
  368. case loc.loc of
  369. LOC_REGISTER,LOC_CREGISTER,
  370. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  371. a_load_stack_reg(list,size,loc.register);
  372. LOC_REFERENCE:
  373. begin
  374. tmpref:=loc.reference;
  375. a_load_stack_ref(list,size,loc.reference,prepare_stack_for_ref(list,tmpref,false));
  376. end;
  377. else
  378. internalerror(2011020501);
  379. end;
  380. end;
  381. procedure thlcgwasm.a_load_loc_stack(list: TAsmList;size: tdef;const loc: tlocation);
  382. var
  383. tmpref: treference;
  384. extra_slots: LongInt;
  385. begin
  386. case loc.loc of
  387. LOC_REGISTER,LOC_CREGISTER,
  388. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  389. a_load_reg_stack(list,size,loc.register);
  390. LOC_REFERENCE,LOC_CREFERENCE:
  391. begin
  392. tmpref:=loc.reference;
  393. extra_slots:=prepare_stack_for_ref(list,tmpref,false);
  394. a_load_ref_stack(list,size,tmpref,extra_slots);
  395. end;
  396. LOC_CONSTANT:
  397. a_load_const_stack(list,size,loc.value,def2regtyp(size));
  398. else
  399. internalerror(2011010401);
  400. end;
  401. end;
  402. procedure thlcgwasm.a_loadfpu_const_stack(list: TAsmList; size: tdef; a: double);
  403. begin
  404. case tfloatdef(size).floattype of
  405. s32real:
  406. begin
  407. list.concat(taicpu.op_single(a_f32_const, a));
  408. incstack(list,1);
  409. end;
  410. s64real:
  411. begin
  412. list.concat(taicpu.op_double(a_f64_const,a));
  413. incstack(list,1);
  414. end
  415. else
  416. internalerror(2011010501);
  417. end;
  418. end;
  419. procedure thlcgwasm.a_op_stack(list: TAsmList; op: topcg; size: tdef);
  420. begin
  421. case def_cgsize(size) of
  422. OS_8,OS_S8,
  423. OS_16,OS_S16,
  424. OS_32,OS_S32:
  425. begin
  426. { boolean not: =0? for boolean }
  427. { todo: should we also do this for cbool? }
  428. if (op=OP_NOT) and is_pasbool(size) then
  429. list.concat(taicpu.op_none(a_i32_eqz))
  430. else
  431. begin
  432. if op=OP_NOT then
  433. begin
  434. { not = xor -1 for integer }
  435. a_load_const_stack(list,s32inttype,high(cardinal),R_INTREGISTER);
  436. op:=OP_XOR;
  437. end
  438. else if op=OP_NEG then
  439. begin
  440. { neg = *(-1) }
  441. a_load_const_stack(list,s32inttype,-1,R_INTREGISTER);
  442. op:=OP_MUL;
  443. end;
  444. if TOpCG2IAsmOp[op]=A_None then
  445. internalerror(2010120532);
  446. list.concat(taicpu.op_none(TOpCG2IAsmOp[op]));
  447. decstack(list,1);
  448. end;
  449. maybe_adjust_op_result(list,op,size);
  450. end;
  451. OS_64,OS_S64:
  452. begin
  453. { unsigned 64 bit division must be done via a helper }
  454. if op=OP_DIV then
  455. internalerror(2010120530);
  456. { boolean not: =0? for boolean }
  457. { todo: should we also do this for cbool? }
  458. if (op=OP_NOT) and is_pasbool(size) then
  459. list.concat(taicpu.op_none(a_i64_eqz))
  460. else
  461. begin
  462. if op=OP_NOT then
  463. begin
  464. { not = xor -1 for integer }
  465. a_load_const_stack(list,s64inttype,-1,R_INTREGISTER);
  466. op:=OP_XOR;
  467. end
  468. else if op=OP_NEG then
  469. begin
  470. { neg = *(-1) }
  471. a_load_const_stack(list,s64inttype,-1,R_INTREGISTER);
  472. op:=OP_MUL;
  473. end;
  474. if TOpCG2LAsmOp[op]=A_None then
  475. internalerror(2010120533);
  476. list.concat(taicpu.op_none(TOpCG2LAsmOp[op]));
  477. decstack(list,1);
  478. end;
  479. end;
  480. else
  481. internalerror(2010120531);
  482. end;
  483. end;
  484. procedure thlcgwasm.a_op_const_stack(list: TAsmList;op: topcg;size: tdef;a: tcgint);
  485. begin
  486. case op of
  487. OP_NEG,OP_NOT:
  488. internalerror(2011010801);
  489. else
  490. a_load_const_stack(list,size,a,R_INTREGISTER);
  491. end;
  492. a_op_stack(list,op,size);
  493. end;
  494. procedure thlcgwasm.a_op_reg_stack(list: TAsmList; op: topcg; size: tdef; reg: tregister);
  495. begin
  496. a_load_reg_stack(list,size,reg);
  497. a_op_stack(list,op,size);
  498. end;
  499. procedure thlcgwasm.a_op_ref_stack(list: TAsmList; op: topcg; size: tdef; const ref: treference);
  500. var
  501. tmpref: treference;
  502. begin
  503. { ref must not be the stack top, because that may indicate an error
  504. (it means that we will perform an operation of the stack top onto
  505. itself, so that means the two values have been loaded manually prior
  506. to calling this routine, instead of letting this routine load one of
  507. them; if something like that is needed, call a_op_stack() directly) }
  508. if ref.base=NR_EVAL_STACK_BASE then
  509. internalerror(2010121102);
  510. tmpref:=ref;
  511. a_load_ref_stack(list,size,tmpref,prepare_stack_for_ref(list,tmpref,false));
  512. a_op_stack(list,op,size);
  513. end;
  514. procedure thlcgwasm.a_op_loc_stack(list: TAsmList; op: topcg; size: tdef; const loc: tlocation);
  515. begin
  516. case loc.loc of
  517. LOC_REGISTER,LOC_CREGISTER:
  518. a_op_reg_stack(list,op,size,loc.register);
  519. LOC_REFERENCE,LOC_CREFERENCE:
  520. a_op_ref_stack(list,op,size,loc.reference);
  521. LOC_CONSTANT:
  522. a_op_const_stack(list,op,size,loc.value);
  523. else
  524. internalerror(2011011415)
  525. end;
  526. end;
  527. procedure thlcgwasm.a_cmp_const_loc_stack(list: TAsmList; size: tdef; cmp_op: topcmp; a: tcgint; const loc: tlocation);
  528. var
  529. tmpreg: tregister;
  530. begin
  531. case loc.loc of
  532. LOC_REGISTER,LOC_CREGISTER:
  533. a_cmp_const_reg_stack(list,size,cmp_op,a,loc.register);
  534. LOC_REFERENCE,LOC_CREFERENCE:
  535. a_cmp_const_ref_stack(list,size,cmp_op,a,loc.reference);
  536. LOC_SUBSETREG, LOC_CSUBSETREG:
  537. begin
  538. tmpreg:=getintregister(list,size);
  539. a_load_subsetreg_reg(list,size,size,loc.sreg,tmpreg);
  540. a_cmp_const_reg_stack(list,size,cmp_op,a,tmpreg);
  541. end;
  542. LOC_SUBSETREF, LOC_CSUBSETREF:
  543. begin
  544. tmpreg:=getintregister(list,size);
  545. a_load_subsetref_reg(list,size,size,loc.sref,tmpreg);
  546. a_cmp_const_reg_stack(list,size,cmp_op,a,tmpreg);
  547. end;
  548. else
  549. internalerror(2010120430);
  550. end;
  551. end;
  552. procedure thlcgwasm.a_cmp_const_ref_stack(list: TAsmList; size: tdef; cmp_op: topcmp; a: tcgint; const ref: treference);
  553. var
  554. tmpref: treference;
  555. begin
  556. tmpref:=ref;
  557. if tmpref.base<>NR_EVAL_STACK_BASE then
  558. a_load_ref_stack(list,size,tmpref,prepare_stack_for_ref(list,tmpref,false));
  559. maybe_adjust_cmp_stackval(list,size,cmp_op);
  560. a_load_const_stack(list,size,maybe_adjust_cmp_constval(size,cmp_op,a),def2regtyp(size));
  561. a_cmp_stack_stack(list,size,cmp_op);
  562. end;
  563. procedure thlcgwasm.a_cmp_const_reg_stack(list: TAsmList; size: tdef; cmp_op: topcmp; a: tcgint; reg: tregister);
  564. begin
  565. a_load_reg_stack(list,size,reg);
  566. maybe_adjust_cmp_stackval(list,size,cmp_op);
  567. a_load_const_stack(list,size,maybe_adjust_cmp_constval(size,cmp_op,a),def2regtyp(size));
  568. a_cmp_stack_stack(list,size,cmp_op);
  569. end;
  570. procedure thlcgwasm.a_cmp_ref_reg_stack(list: TAsmList; size: tdef; cmp_op: topcmp; const ref: treference; reg: tregister);
  571. var
  572. tmpref: treference;
  573. begin
  574. tmpref:=ref;
  575. a_load_reg_stack(list,size,reg);
  576. maybe_adjust_cmp_stackval(list,size,cmp_op);
  577. if tmpref.base<>NR_EVAL_STACK_BASE then
  578. a_load_ref_stack(list,size,tmpref,prepare_stack_for_ref(list,tmpref,false))
  579. else begin
  580. // todo: need a swap operation?
  581. //list.concat(taicpu.op_none(a_swap));
  582. Internalerror(2019083003);
  583. end;
  584. maybe_adjust_cmp_stackval(list,size,cmp_op);
  585. a_cmp_stack_stack(list,size,cmp_op);
  586. end;
  587. procedure thlcgwasm.a_cmp_reg_ref_stack(list: TAsmList; size: tdef; cmp_op: topcmp; reg: tregister; const ref: treference);
  588. var
  589. tmpref: treference;
  590. begin
  591. tmpref:=ref;
  592. if tmpref.base<>NR_EVAL_STACK_BASE then
  593. a_load_ref_stack(list,size,ref,prepare_stack_for_ref(list,tmpref,false));
  594. maybe_adjust_cmp_stackval(list,size,cmp_op);
  595. a_load_reg_stack(list,size,reg);
  596. maybe_adjust_cmp_stackval(list,size,cmp_op);
  597. a_cmp_stack_stack(list,size,cmp_op);
  598. end;
  599. procedure thlcgwasm.a_cmp_reg_reg_stack(list: TAsmList; size: tdef; cmp_op: topcmp; reg1, reg2: tregister);
  600. begin
  601. a_load_reg_stack(list,size,reg2);
  602. maybe_adjust_cmp_stackval(list,size,cmp_op);
  603. a_load_reg_stack(list,size,reg1);
  604. maybe_adjust_cmp_stackval(list,size,cmp_op);
  605. a_cmp_stack_stack(list,size,cmp_op);
  606. end;
  607. procedure thlcgwasm.a_cmp_subsetreg_reg_stack(list: TAsmList; fromsubsetsize, cmpsize: tdef; cmp_op: topcmp; const sreg: tsubsetregister; reg: tregister);
  608. var
  609. tmpreg: tregister;
  610. begin
  611. tmpreg:=getintregister(list,cmpsize);
  612. a_load_subsetreg_reg(list,fromsubsetsize,cmpsize,sreg,tmpreg);
  613. a_cmp_reg_reg_stack(list,cmpsize,cmp_op,tmpreg,reg);
  614. end;
  615. procedure thlcgwasm.a_cmp_subsetref_reg_stack(list: TAsmList; fromsubsetsize, cmpsize: tdef; cmp_op: topcmp; const sref: tsubsetreference; reg: tregister);
  616. var
  617. tmpreg: tregister;
  618. begin
  619. tmpreg:=getintregister(list,cmpsize);
  620. a_load_subsetref_reg(list,fromsubsetsize,cmpsize,sref,tmpreg);
  621. a_cmp_reg_reg_stack(list,cmpsize,cmp_op,tmpreg,reg);
  622. end;
  623. procedure thlcgwasm.a_cmp_loc_reg_stack(list: TAsmList; size: tdef; cmp_op: topcmp; const loc: tlocation; reg: tregister);
  624. begin
  625. case loc.loc of
  626. LOC_REGISTER,
  627. LOC_CREGISTER:
  628. a_cmp_reg_reg_stack(list,size,cmp_op,loc.register,reg);
  629. LOC_REFERENCE,
  630. LOC_CREFERENCE :
  631. a_cmp_ref_reg_stack(list,size,cmp_op,loc.reference,reg);
  632. LOC_CONSTANT:
  633. a_cmp_const_reg_stack(list,size,cmp_op,loc.value,reg);
  634. LOC_SUBSETREG,
  635. LOC_CSUBSETREG:
  636. a_cmp_subsetreg_reg_stack(list,size,size,cmp_op,loc.sreg,reg);
  637. LOC_SUBSETREF,
  638. LOC_CSUBSETREF:
  639. a_cmp_subsetref_reg_stack(list,size,size,cmp_op,loc.sref,reg);
  640. else
  641. internalerror(2010120431);
  642. end;
  643. end;
  644. procedure thlcgwasm.a_cmp_reg_loc_stack(list: TAsmList; size: tdef; cmp_op: topcmp; reg: tregister; const loc: tlocation);
  645. begin
  646. a_cmp_loc_reg_stack(list,size,swap_opcmp(cmp_op),loc,reg);
  647. end;
  648. procedure thlcgwasm.a_cmp_ref_loc_stack(list: TAsmList; size: tdef; cmp_op: topcmp; const ref: treference; const loc: tlocation);
  649. var
  650. tmpreg: tregister;
  651. begin
  652. case loc.loc of
  653. LOC_REGISTER,LOC_CREGISTER:
  654. a_cmp_ref_reg_stack(list,size,cmp_op,ref,loc.register);
  655. LOC_REFERENCE,LOC_CREFERENCE:
  656. begin
  657. tmpreg:=getintregister(list,size);
  658. a_load_ref_reg(list,size,size,loc.reference,tmpreg);
  659. a_cmp_ref_reg_stack(list,size,cmp_op,ref,tmpreg);
  660. end;
  661. LOC_CONSTANT:
  662. begin
  663. a_cmp_const_ref_stack(list,size,swap_opcmp(cmp_op),loc.value,ref);
  664. end;
  665. LOC_SUBSETREG, LOC_CSUBSETREG:
  666. begin
  667. tmpreg:=getintregister(list,size);
  668. a_load_ref_reg(list,size,size,loc.reference,tmpreg);
  669. a_cmp_subsetreg_reg_stack(list,size,size,swap_opcmp(cmp_op),loc.sreg,tmpreg);
  670. end;
  671. LOC_SUBSETREF, LOC_CSUBSETREF:
  672. begin
  673. tmpreg:=getintregister(list,size);
  674. a_load_ref_reg(list,size,size,loc.reference,tmpreg);
  675. a_cmp_subsetref_reg_stack(list,size,size,swap_opcmp(cmp_op),loc.sref,tmpreg);
  676. end;
  677. else
  678. internalerror(2010120432);
  679. end;
  680. end;
  681. procedure thlcgwasm.a_cmp_const_loc_br(list: TAsmList; size: tdef;cmp_op: topcmp; a: tcgint; const loc: tlocation; br: Integer);
  682. begin
  683. a_cmp_const_loc_stack(list,size,cmp_op,a,loc);
  684. current_asmdata.CurrAsmList.concat(taicpu.op_const(a_br_if,br));
  685. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  686. end;
  687. procedure thlcgwasm.a_cmp_const_ref_br(list: TAsmList; size: tdef; cmp_op: topcmp; a: tcgint; const ref: treference; br: Integer);
  688. begin
  689. a_cmp_const_ref_stack(list,size,cmp_op,a,ref);
  690. current_asmdata.CurrAsmList.concat(taicpu.op_const(a_br_if,br));
  691. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  692. end;
  693. procedure thlcgwasm.a_cmp_const_reg_br(list: TAsmList; size: tdef; cmp_op: topcmp; a: tcgint; reg: tregister; br: Integer);
  694. begin
  695. a_cmp_const_reg_stack(list,size,cmp_op,a,reg);
  696. current_asmdata.CurrAsmList.concat(taicpu.op_const(a_br_if,br));
  697. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  698. end;
  699. procedure thlcgwasm.a_cmp_ref_reg_br(list: TAsmList; size: tdef; cmp_op: topcmp; const ref: treference; reg: tregister; br: Integer);
  700. begin
  701. a_cmp_ref_reg_stack(list,size,cmp_op,ref,reg);
  702. current_asmdata.CurrAsmList.concat(taicpu.op_const(a_br_if,br));
  703. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  704. end;
  705. procedure thlcgwasm.a_cmp_reg_ref_br(list: TAsmList; size: tdef; cmp_op: topcmp; reg: tregister; const ref: treference; br: Integer);
  706. begin
  707. a_cmp_reg_ref_stack(list,size,cmp_op,reg,ref);
  708. current_asmdata.CurrAsmList.concat(taicpu.op_const(a_br_if,br));
  709. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  710. end;
  711. procedure thlcgwasm.a_cmp_reg_reg_br(list: TAsmList; size: tdef; cmp_op: topcmp; reg1, reg2: tregister; br: Integer);
  712. begin
  713. a_cmp_reg_reg_stack(list,size,cmp_op,reg1,reg2);
  714. current_asmdata.CurrAsmList.concat(taicpu.op_const(a_br_if,br));
  715. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  716. end;
  717. procedure thlcgwasm.a_cmp_subsetreg_reg_br(list: TAsmList; fromsubsetsize, cmpsize: tdef; cmp_op: topcmp; const sreg: tsubsetregister; reg: tregister; br: Integer);
  718. begin
  719. a_cmp_subsetreg_reg_stack(list,fromsubsetsize,cmpsize,cmp_op,sreg,reg);
  720. current_asmdata.CurrAsmList.concat(taicpu.op_const(a_br_if,br));
  721. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  722. end;
  723. procedure thlcgwasm.a_cmp_subsetref_reg_br(list: TAsmList; fromsubsetsize, cmpsize: tdef; cmp_op: topcmp; const sref: tsubsetreference; reg: tregister; br: Integer);
  724. begin
  725. a_cmp_subsetref_reg_stack(list,fromsubsetsize,cmpsize,cmp_op,sref,reg);
  726. current_asmdata.CurrAsmList.concat(taicpu.op_const(a_br_if,br));
  727. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  728. end;
  729. procedure thlcgwasm.a_cmp_loc_reg_br(list : TAsmList;size : tdef;cmp_op : topcmp; const loc: tlocation; reg : tregister; br: Integer);
  730. begin
  731. a_cmp_loc_reg_stack(list,size,cmp_op,loc,reg);
  732. current_asmdata.CurrAsmList.concat(taicpu.op_const(a_br_if,br));
  733. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  734. end;
  735. procedure thlcgwasm.a_cmp_reg_loc_br(list : TAsmList;size : tdef;cmp_op : topcmp; reg: tregister; const loc: tlocation; br: Integer);
  736. begin
  737. a_cmp_reg_loc_stack(list,size,cmp_op,reg,loc);
  738. current_asmdata.CurrAsmList.concat(taicpu.op_const(a_br_if,br));
  739. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  740. end;
  741. procedure thlcgwasm.a_cmp_ref_loc_br(list: TAsmList; size: tdef;cmp_op: topcmp; const ref: treference; const loc: tlocation; br: Integer);
  742. begin
  743. a_cmp_ref_loc_stack(list,size,cmp_op,ref,loc);
  744. current_asmdata.CurrAsmList.concat(taicpu.op_const(a_br_if,br));
  745. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  746. end;
  747. procedure thlcgwasm.g_reference_loc(list: TAsmList; def: tdef; const fromloc: tlocation; out toloc: tlocation);
  748. begin
  749. case fromloc.loc of
  750. LOC_CREFERENCE,
  751. LOC_REFERENCE:
  752. begin
  753. toloc:=fromloc;
  754. if (fromloc.reference.base<>NR_NO) and
  755. (fromloc.reference.base<>current_procinfo.framepointer) and
  756. (fromloc.reference.base<>NR_STACK_POINTER_REG) then
  757. g_allocload_reg_reg(list,voidpointertype,fromloc.reference.base,toloc.reference.base,R_ADDRESSREGISTER);
  758. end;
  759. else
  760. inherited;
  761. end;
  762. end;
  763. procedure thlcgwasm.a_cmp_stack_stack(list: TAsmlist; size: tdef; cmp_op: topcmp);
  764. const
  765. opcmp32: array[topcmp] of tasmop = (
  766. A_None, { OC_NONE, }
  767. a_i32_eq, { OC_EQ, equality comparison }
  768. a_i32_gt_s, { OC_GT, greater than (signed) }
  769. a_i32_lt_s, { OC_LT, less than (signed) }
  770. a_i32_ge_s, { OC_GTE, greater or equal than (signed) }
  771. a_i32_le_s, { OC_LTE, less or equal than (signed) }
  772. a_i32_ne, { OC_NE, not equal }
  773. a_i32_le_u, { OC_BE, less or equal than (unsigned) }
  774. a_i32_lt_u, { OC_B, less than (unsigned) }
  775. a_i32_ge_u, { OC_AE, greater or equal than (unsigned) }
  776. a_i32_gt_u { OC_A greater than (unsigned) }
  777. );
  778. const
  779. opcmp64: array[TOpCmp] of TAsmOp = (A_None,
  780. a_i64_eq, // OC_EQ
  781. a_i64_gt_s, a_i64_lt_s, // OC_GT, OC_LT
  782. a_i64_ge_s, a_i64_le_s, // OC_GTE, OC_LTE
  783. a_i64_ne, // OC_NE
  784. a_i64_le_u, a_i64_lt_u, // OC_BE, OC_B
  785. a_i64_ge_u, a_i64_gt_u // OC_AE, OC_A
  786. );
  787. var
  788. cgsize: tcgsize;
  789. begin
  790. // WASM doesn't have compare+jump (to label) operation
  791. // thus even though this is a_cmp_stack_stack()
  792. // label operrand is ommited
  793. //
  794. // todo: it should NOT be ommitted when we're leaving a block
  795. // (i.e. Exit or break or continue operators)
  796. case def2regtyp(size) of
  797. R_INTREGISTER,
  798. R_ADDRESSREGISTER:
  799. begin
  800. cgsize:=def_cgsize(size);
  801. case cgsize of
  802. OS_S8,OS_8,
  803. OS_16,OS_S16,
  804. OS_S32,OS_32:
  805. begin
  806. list.concat(taicpu.op_none(opcmp32[cmp_op]));
  807. decstack(list,1);
  808. end;
  809. OS_64,OS_S64:
  810. begin
  811. list.concat(taicpu.op_none(opcmp64[cmp_op]));
  812. decstack(list,1);
  813. end;
  814. else
  815. internalerror(2010120538);
  816. end;
  817. end;
  818. else
  819. internalerror(2010120538);
  820. end;
  821. end;
  822. procedure thlcgwasm.maybe_adjust_cmp_stackval(list: TAsmlist; size: tdef; cmp_op: topcmp);
  823. begin
  824. { use cmp_op because eventually that's what indicates the
  825. signed/unsigned character of the operation, not the size... }
  826. if (cmp_op in [OC_EQ,OC_NE,OC_LT,OC_LTE,OC_GT,OC_GTE]) or
  827. (def2regtyp(size)<>R_INTREGISTER) then
  828. exit;
  829. { http://stackoverflow.com/questions/4068973/c-performing-signed-comparison-in-unsigned-variables-without-casting }
  830. case def_cgsize(size) of
  831. OS_32,OS_S32:
  832. a_op_const_stack(list,OP_XOR,size,cardinal($80000000));
  833. OS_64,OS_S64:
  834. a_op_const_stack(list,OP_XOR,size,tcgint($8000000000000000));
  835. else
  836. ;
  837. end;
  838. end;
  839. function thlcgwasm.maybe_adjust_cmp_constval(size: tdef; cmp_op: topcmp; a: tcgint): tcgint;
  840. begin
  841. result:=a;
  842. { use cmp_op because eventually that's what indicates the
  843. signed/unsigned character of the operation, not the size... }
  844. if (cmp_op in [OC_EQ,OC_NE,OC_LT,OC_LTE,OC_GT,OC_GTE]) or
  845. (def2regtyp(size)<>R_INTREGISTER) then
  846. exit;
  847. case def_cgsize(size) of
  848. OS_32,OS_S32:
  849. result:=a xor cardinal($80000000);
  850. OS_64,OS_S64:
  851. {$push}{$r-}
  852. result:=a xor tcgint($8000000000000000);
  853. {$pop}
  854. else
  855. ;
  856. end;
  857. end;
  858. procedure thlcgwasm.maybe_adjust_op_result(list: TAsmList; op: TOpCg; size: tdef);
  859. const
  860. overflowops = [OP_MUL,OP_SHL,OP_ADD,OP_SUB,OP_NOT,OP_NEG];
  861. begin
  862. if (op in overflowops) and
  863. (def_cgsize(size) in [OS_8,OS_S8,OS_16,OS_S16]) then
  864. resize_stack_int_val(list,s32inttype,size,false);
  865. end;
  866. procedure thlcgwasm.gen_load_uninitialized_function_result(list: TAsmList; pd: tprocdef; resdef: tdef; const resloc: tcgpara);
  867. begin
  868. { nothing to do for ret_in_param results }
  869. if paramanager.ret_in_param(pd.returndef,pd) then
  870. exit;
  871. { constructors don't return anything in Java }
  872. if pd.proctypeoption=potype_constructor then
  873. exit;
  874. { must return a value of the correct type on the evaluation stack }
  875. case def2regtyp(resdef) of
  876. R_INTREGISTER,
  877. R_ADDRESSREGISTER:
  878. a_load_const_cgpara(list,resdef,0,resloc);
  879. R_FPUREGISTER:
  880. case tfloatdef(resdef).floattype of
  881. s32real:
  882. begin
  883. list.concat(taicpu.op_single(a_f32_const, 0));
  884. incstack(list,1);
  885. end;
  886. s64real:
  887. begin
  888. list.concat(taicpu.op_double(a_f64_const, 0));
  889. incstack(list,1);
  890. end;
  891. else
  892. internalerror(2011010302);
  893. end
  894. else
  895. internalerror(2011010301);
  896. end;
  897. end;
  898. //procedure thlcgwasm.g_copyvalueparas(p: TObject; arg: pointer);
  899. // var
  900. // list: tasmlist;
  901. // tmpref: treference;
  902. // begin
  903. // { zero-extend < 32 bit primitive types (FPC can zero-extend when calling,
  904. // but that doesn't help when we're called from Java code or indirectly
  905. // as a procvar -- exceptions: widechar (Java-specific type) and ordinal
  906. // types whose upper bound does not set the sign bit }
  907. // if (tsym(p).typ=paravarsym) and
  908. // (tparavarsym(p).varspez in [vs_value,vs_const]) and
  909. // (tparavarsym(p).vardef.typ=orddef) and
  910. // not is_pasbool(tparavarsym(p).vardef) and
  911. // not is_widechar(tparavarsym(p).vardef) and
  912. // (tparavarsym(p).vardef.size<4) and
  913. // not is_signed(tparavarsym(p).vardef) and
  914. // (torddef(tparavarsym(p).vardef).high>=(1 shl (tparavarsym(p).vardef.size*8-1))) then
  915. // begin
  916. // list:=TAsmList(arg);
  917. // { store value in new location to keep Android verifier happy }
  918. // tg.gethltemp(list,tparavarsym(p).vardef,tparavarsym(p).vardef.size,tt_persistent,tmpref);
  919. // a_load_loc_stack(list,tparavarsym(p).vardef,tparavarsym(p).initialloc);
  920. // a_op_const_stack(list,OP_AND,tparavarsym(p).vardef,(1 shl (tparavarsym(p).vardef.size*8))-1);
  921. // a_load_stack_ref(list,tparavarsym(p).vardef,tmpref,prepare_stack_for_ref(list,tmpref,false));
  922. // location_reset_ref(tparavarsym(p).localloc,LOC_REFERENCE,def_cgsize(tparavarsym(p).vardef),4,tmpref.volatility);
  923. // tparavarsym(p).localloc.reference:=tmpref;
  924. // end;
  925. //
  926. // inherited g_copyvalueparas(p, arg);
  927. // end;
  928. procedure thlcgwasm.inittempvariables(list: TAsmList);
  929. begin
  930. { these are automatically initialised when allocated if necessary }
  931. end;
  932. function thlcgwasm.g_call_system_proc_intern(list: TAsmList; pd: tprocdef; const paras: array of pcgpara; forceresdef: tdef): tcgpara;
  933. begin
  934. result:=inherited;
  935. pd.init_paraloc_info(callerside);
  936. g_adjust_stack_after_call(list,pd);
  937. end;
  938. function thlcgwasm.prepare_stack_for_ref(list: TAsmList; var ref: treference; dup: boolean): longint;
  939. begin
  940. result:=0;
  941. { fake location that indicates the value is already on the stack? }
  942. if (ref.base=NR_EVAL_STACK_BASE) or (ref.base=NR_LOCAL_STACK_POINTER_REG) then
  943. exit;
  944. if (ref.base=NR_NO) and (ref.index<>NR_NO) and (ref.scalefactor<=1) then
  945. begin
  946. ref.base:=ref.index;
  947. ref.index:=NR_NO;
  948. end;
  949. // setting up memory offset
  950. if assigned(ref.symbol) and (ref.base=NR_NO) and (ref.index=NR_NO) then
  951. begin
  952. list.Concat(taicpu.op_const(a_i32_const,0));
  953. incstack(list,1);
  954. if dup then
  955. begin
  956. list.Concat(taicpu.op_const(a_i32_const,0));
  957. incstack(list,1);
  958. end;
  959. result:=1;
  960. end
  961. else if ref.index <> NR_NO then // array access
  962. begin
  963. // it's just faster to sum two of those together
  964. list.Concat(taicpu.op_reg(a_local_get, ref.base));
  965. incstack(list,1);
  966. list.Concat(taicpu.op_reg(a_local_get, ref.index));
  967. incstack(list,1);
  968. list.Concat(taicpu.op_none(a_i32_add));
  969. decstack(list,1);
  970. if dup then
  971. begin
  972. list.Concat(taicpu.op_reg(a_local_get, ref.base));
  973. incstack(list,1);
  974. list.Concat(taicpu.op_reg(a_local_get, ref.index));
  975. incstack(list,1);
  976. list.Concat(taicpu.op_none(a_i32_add));
  977. decstack(list,1);
  978. end;
  979. ref.base:=NR_NO;
  980. ref.index:=NR_NO;
  981. result:=1;
  982. end
  983. else if (ref.base<>NR_NO) then
  984. begin
  985. if (ref.base<>NR_STACK_POINTER_REG) then
  986. begin
  987. { regular field -> load self on the stack }
  988. a_load_reg_stack(list,voidpointertype,ref.base);
  989. if dup then
  990. a_load_reg_stack(list,voidpointertype,ref.base);
  991. { field name/type encoded in symbol, no index/offset }
  992. result:=1;
  993. ref.base:=NR_NO;
  994. end
  995. else // if (ref.base = NR_FRAME_POINTER_REG) then
  996. begin
  997. list.Concat(taicpu.op_sym(a_local_get, current_asmdata.RefAsmSymbol(FRAME_POINTER_SYM,AT_ADDR) ));
  998. incstack(list,1);
  999. end;
  1000. end
  1001. else
  1002. begin
  1003. { static field -> nothing to do here, except for validity check }
  1004. {if not assigned(ref.symbol) or
  1005. (ref.offset<>0) then
  1006. begin
  1007. internalerror(2010120525);
  1008. end;}
  1009. end;
  1010. end;
  1011. procedure thlcgwasm.a_load_const_reg(list: TAsmList; tosize: tdef; a: tcgint; register: tregister);
  1012. begin
  1013. a_load_const_stack(list,tosize,a,def2regtyp(tosize));
  1014. a_load_stack_reg(list,tosize,register);
  1015. end;
  1016. procedure thlcgwasm.a_load_const_ref(list: TAsmList; tosize: tdef; a: tcgint; const ref: treference);
  1017. var
  1018. extra_slots: longint;
  1019. tmpref: treference;
  1020. begin
  1021. tmpref:=ref;
  1022. extra_slots:=prepare_stack_for_ref(list,tmpref,false);
  1023. a_load_const_stack(list,tosize,a,def2regtyp(tosize));
  1024. a_load_stack_ref(list,tosize,tmpref,extra_slots);
  1025. end;
  1026. procedure thlcgwasm.a_load_reg_ref(list: TAsmList; fromsize, tosize: tdef; register: tregister; const ref: treference);
  1027. var
  1028. extra_slots: longint;
  1029. tmpref: treference;
  1030. begin
  1031. tmpref:=ref;
  1032. extra_slots:=prepare_stack_for_ref(list,tmpref,false);
  1033. a_load_reg_stack(list,fromsize,register);
  1034. if def2regtyp(fromsize)=R_INTREGISTER then
  1035. resize_stack_int_val(list,fromsize,tosize,assigned(tmpref.symbol));
  1036. a_load_stack_ref(list,tosize,tmpref,extra_slots);
  1037. end;
  1038. procedure thlcgwasm.a_load_reg_reg(list: TAsmList; fromsize, tosize: tdef; reg1, reg2: tregister);
  1039. begin
  1040. a_load_reg_stack(list,fromsize,reg1);
  1041. if def2regtyp(fromsize)=R_INTREGISTER then
  1042. resize_stack_int_val(list,fromsize,tosize,false);
  1043. a_load_stack_reg(list,tosize,reg2);
  1044. end;
  1045. procedure thlcgwasm.a_load_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; register: tregister);
  1046. var
  1047. extra_slots: longint;
  1048. tmpref: treference;
  1049. begin
  1050. tmpref:=ref;
  1051. extra_slots:=prepare_stack_for_ref(list,tmpref,false);
  1052. a_load_ref_stack(list,fromsize,tmpref,extra_slots);
  1053. if def2regtyp(fromsize)=R_INTREGISTER then
  1054. resize_stack_int_val(list,fromsize,tosize,false);
  1055. a_load_stack_reg(list,tosize,register);
  1056. end;
  1057. procedure thlcgwasm.a_load_ref_ref(list: TAsmList; fromsize, tosize: tdef; const sref: treference; const dref: treference);
  1058. var
  1059. extra_sslots,
  1060. extra_dslots: longint;
  1061. tmpsref, tmpdref: treference;
  1062. tmpreg: tregister;
  1063. begin
  1064. if sref.base<>NR_EVAL_STACK_BASE then
  1065. begin
  1066. tmpsref:=sref;
  1067. tmpdref:=dref;
  1068. { make sure the destination reference is on top, since in the end the
  1069. order has to be "destref, value" -> first create "destref, sourceref" }
  1070. extra_dslots:=prepare_stack_for_ref(list,tmpdref,false);
  1071. extra_sslots:=prepare_stack_for_ref(list,tmpsref,false);
  1072. a_load_ref_stack(list,fromsize,tmpsref,extra_sslots);
  1073. if def2regtyp(fromsize)=R_INTREGISTER then
  1074. resize_stack_int_val(list,fromsize,tosize,assigned(tmpdref.symbol));
  1075. a_load_stack_ref(list,tosize,tmpdref,extra_dslots);
  1076. end
  1077. else
  1078. begin
  1079. { verify if we have the same reference }
  1080. if references_equal(sref,dref) then
  1081. exit;
  1082. tmpreg:=getregisterfordef(list,tosize);
  1083. a_load_ref_reg(list,fromsize,tosize,sref,tmpreg);
  1084. a_load_reg_ref(list,tosize,tosize,tmpreg,dref);
  1085. end;
  1086. end;
  1087. procedure thlcgwasm.a_loadaddr_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; r: tregister);
  1088. begin
  1089. a_loadaddr_ref_stack(list,fromsize,tosize,ref);
  1090. a_load_stack_reg(list, tosize, r);
  1091. end;
  1092. procedure thlcgwasm.a_op_const_reg(list: TAsmList; Op: TOpCG; size: tdef; a: tcgint; reg: TRegister);
  1093. begin
  1094. a_op_const_reg_reg(list,op,size,a,reg,reg);
  1095. end;
  1096. procedure thlcgwasm.a_op_const_reg_reg(list: TAsmList; op: TOpCg; size: tdef; a: tcgint; src, dst: tregister);
  1097. begin
  1098. a_load_reg_stack(list,size,src);
  1099. a_op_const_stack(list,op,size,a);
  1100. a_load_stack_reg(list,size,dst);
  1101. end;
  1102. procedure thlcgwasm.a_op_const_ref(list: TAsmList; Op: TOpCG; size: tdef; a: tcgint; const ref: TReference);
  1103. var
  1104. extra_slots: longint;
  1105. tmpref: treference;
  1106. begin
  1107. tmpref:=ref;
  1108. extra_slots:=prepare_stack_for_ref(list,tmpref,true);
  1109. { TODO, here or in peepholeopt: use iinc when possible }
  1110. a_load_ref_stack(list,size,tmpref,extra_slots);
  1111. a_op_const_stack(list,op,size,a);
  1112. { for android verifier }
  1113. if (def2regtyp(size)=R_INTREGISTER) and
  1114. (assigned(tmpref.symbol)) then
  1115. resize_stack_int_val(list,size,size,true);
  1116. a_load_stack_ref(list,size,tmpref,extra_slots);
  1117. end;
  1118. procedure thlcgwasm.a_op_ref_reg(list: TAsmList; Op: TOpCG; size: tdef; const ref: TReference; reg: TRegister);
  1119. begin
  1120. if not(op in [OP_NOT,OP_NEG]) then
  1121. a_load_reg_stack(list,size,reg);
  1122. a_op_ref_stack(list,op,size,ref);
  1123. a_load_stack_reg(list,size,reg);
  1124. end;
  1125. procedure thlcgwasm.a_op_reg_reg_reg(list: TAsmList; op: TOpCg; size: tdef; src1, src2, dst: tregister);
  1126. begin
  1127. if not(op in [OP_NOT,OP_NEG]) then
  1128. a_load_reg_stack(list,size,src2);
  1129. a_op_reg_stack(list,op,size,src1);
  1130. a_load_stack_reg(list,size,dst);
  1131. end;
  1132. procedure thlcgwasm.a_op_reg_reg(list: TAsmList; Op: TOpCG; size: tdef; reg1, reg2: TRegister);
  1133. begin
  1134. a_op_reg_reg_reg(list,op,size,reg1,reg2,reg2);
  1135. end;
  1136. procedure thlcgwasm.a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tdef; a: tcgint; src, dst: tregister; setflags: boolean; var ovloc: tlocation);
  1137. var
  1138. tmpreg: tregister;
  1139. begin
  1140. if not setflags then
  1141. begin
  1142. inherited;
  1143. exit;
  1144. end;
  1145. tmpreg:=getintregister(list,size);
  1146. a_load_const_reg(list,size,a,tmpreg);
  1147. a_op_reg_reg_reg_checkoverflow(list,op,size,tmpreg,src,dst,true,ovloc);
  1148. end;
  1149. procedure thlcgwasm.a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tdef; src1, src2, dst: tregister; setflags: boolean; var ovloc: tlocation);
  1150. var
  1151. orgsrc1, orgsrc2: tregister;
  1152. docheck: boolean;
  1153. lab: tasmlabel;
  1154. begin
  1155. if not setflags then
  1156. begin
  1157. inherited;
  1158. exit;
  1159. end;
  1160. { anything else cannot overflow }
  1161. docheck:=size.size in [4,8];
  1162. if docheck then
  1163. begin
  1164. orgsrc1:=src1;
  1165. orgsrc2:=src2;
  1166. if src1=dst then
  1167. begin
  1168. orgsrc1:=getintregister(list,size);
  1169. a_load_reg_reg(list,size,size,src1,orgsrc1);
  1170. end;
  1171. if src2=dst then
  1172. begin
  1173. orgsrc2:=getintregister(list,size);
  1174. a_load_reg_reg(list,size,size,src2,orgsrc2);
  1175. end;
  1176. end;
  1177. a_op_reg_reg_reg(list,op,size,src1,src2,dst);
  1178. if docheck then
  1179. begin
  1180. { * signed overflow for addition iff
  1181. - src1 and src2 are negative and result is positive (excep in case of
  1182. subtraction, then sign of src1 has to be inverted)
  1183. - src1 and src2 are positive and result is negative
  1184. -> Simplified boolean equivalent (in terms of sign bits):
  1185. not(src1 xor src2) and (src1 xor dst)
  1186. for subtraction, multiplication: invert src1 sign bit
  1187. for division: handle separately (div by zero, low(inttype) div -1),
  1188. not supported by this code
  1189. * unsigned overflow iff carry out, aka dst < src1 or dst < src2
  1190. }
  1191. location_reset(ovloc,LOC_REGISTER,OS_S32);
  1192. { not pasbool8, because then we'd still have to convert the integer to
  1193. a boolean via branches for Dalvik}
  1194. ovloc.register:=getintregister(list,s32inttype);
  1195. if not ((size.typ=pointerdef) or
  1196. ((size.typ=orddef) and
  1197. (torddef(size).ordtype in [u64bit,u16bit,u32bit,u8bit,uchar,
  1198. pasbool1,pasbool8,pasbool16,pasbool32,pasbool64]))) then
  1199. begin
  1200. a_load_reg_stack(list,size,src1);
  1201. if op in [OP_SUB,OP_IMUL] then
  1202. a_op_stack(list,OP_NOT,size);
  1203. a_op_reg_stack(list,OP_XOR,size,src2);
  1204. a_op_stack(list,OP_NOT,size);
  1205. a_load_reg_stack(list,size,src1);
  1206. a_op_reg_stack(list,OP_XOR,size,dst);
  1207. a_op_stack(list,OP_AND,size);
  1208. a_op_const_stack(list,OP_SHR,size,(size.size*8)-1);
  1209. if size.size=8 then
  1210. begin
  1211. //todo: any operands needed?
  1212. list.concat(taicpu.op_none(a_i32_wrap_i64));
  1213. end;
  1214. end
  1215. else
  1216. begin
  1217. a_load_const_stack(list,s32inttype,0,R_INTREGISTER);
  1218. current_asmdata.getjumplabel(lab);
  1219. { can be optimized by removing duplicate xor'ing to convert dst from
  1220. signed to unsigned quadrant }
  1221. a_cmp_reg_reg_label(list,size,OC_B,dst,src1,lab);
  1222. a_cmp_reg_reg_label(list,size,OC_B,dst,src2,lab);
  1223. a_op_const_stack(list,OP_XOR,s32inttype,1);
  1224. a_label(list,lab);
  1225. end;
  1226. a_load_stack_reg(list,s32inttype,ovloc.register);
  1227. end
  1228. else
  1229. ovloc.loc:=LOC_VOID;
  1230. end;
  1231. procedure thlcgwasm.a_cmp_const_ref_label(list: TAsmList; size: tdef; cmp_op: topcmp; a: tcgint; const ref: treference; l: tasmlabel);
  1232. begin
  1233. internalerror(2021011802);
  1234. end;
  1235. procedure thlcgwasm.a_cmp_const_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; a: tcgint; reg: tregister; l: tasmlabel);
  1236. begin
  1237. internalerror(2021011802);
  1238. end;
  1239. procedure thlcgwasm.a_cmp_ref_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; const ref: treference; reg: tregister; l: tasmlabel);
  1240. begin
  1241. internalerror(2021011802);
  1242. end;
  1243. procedure thlcgwasm.a_cmp_reg_ref_label(list: TAsmList; size: tdef; cmp_op: topcmp; reg: tregister; const ref: treference; l: tasmlabel);
  1244. begin
  1245. internalerror(2021011802);
  1246. end;
  1247. procedure thlcgwasm.a_cmp_reg_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; reg1, reg2: tregister; l: tasmlabel);
  1248. begin
  1249. internalerror(2021011802);
  1250. end;
  1251. procedure thlcgwasm.a_jmp_always(list: TAsmList; l: tasmlabel);
  1252. begin
  1253. if l=current_procinfo.CurrBreakLabel then
  1254. list.concat(taicpu.op_const(a_br,br_blocks-loopBreakBr))
  1255. else if l=current_procinfo.CurrContinueLabel then
  1256. list.concat(taicpu.op_const(a_br,br_blocks-loopContBr))
  1257. else if l=current_procinfo.CurrExitLabel then
  1258. list.concat(taicpu.op_const(a_br,br_blocks-exitBr))
  1259. else
  1260. Internalerror(2019091806); // unexpected jump
  1261. end;
  1262. procedure thlcgwasm.a_loadfpu_ref_ref(list: TAsmList; fromsize, tosize: tdef; const ref1, ref2: treference);
  1263. var
  1264. dstack_slots: longint;
  1265. tmpref1, tmpref2: treference;
  1266. begin
  1267. tmpref1:=ref1;
  1268. tmpref2:=ref2;
  1269. dstack_slots:=prepare_stack_for_ref(list,tmpref2,false);
  1270. a_load_ref_stack(list,fromsize,tmpref1,prepare_stack_for_ref(list,tmpref1,false));
  1271. resizestackfpuval(list,def_cgsize(fromsize),def_cgsize(tosize));
  1272. a_load_stack_ref(list,tosize,tmpref2,dstack_slots);
  1273. end;
  1274. procedure thlcgwasm.a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; reg: tregister);
  1275. var
  1276. tmpref: treference;
  1277. begin
  1278. tmpref:=ref;
  1279. a_load_ref_stack(list,fromsize,tmpref,prepare_stack_for_ref(list,tmpref,false));
  1280. resizestackfpuval(list,def_cgsize(fromsize),def_cgsize(tosize));
  1281. a_load_stack_reg(list,tosize,reg);
  1282. end;
  1283. procedure thlcgwasm.a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tdef; reg: tregister; const ref: treference);
  1284. var
  1285. dstack_slots: longint;
  1286. tmpref: treference;
  1287. begin
  1288. tmpref:=ref;
  1289. dstack_slots:=prepare_stack_for_ref(list,tmpref,false);
  1290. a_load_reg_stack(list,fromsize,reg);
  1291. resizestackfpuval(list,def_cgsize(fromsize),def_cgsize(tosize));
  1292. a_load_stack_ref(list,tosize,tmpref,dstack_slots);
  1293. end;
  1294. procedure thlcgwasm.a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tdef; reg1, reg2: tregister);
  1295. begin
  1296. a_load_reg_stack(list,fromsize,reg1);
  1297. resizestackfpuval(list,def_cgsize(fromsize),def_cgsize(tosize));
  1298. a_load_stack_reg(list,tosize,reg2);
  1299. end;
  1300. procedure thlcgwasm.g_concatcopy(list: TAsmList; size: tdef; const source, dest: treference);
  1301. var
  1302. pd: tprocdef;
  1303. cgpara1,cgpara2,cgpara3 : TCGPara;
  1304. begin
  1305. if (source.base=NR_EVAL_STACK_BASE) or (source.base=NR_LOCAL_STACK_POINTER_REG) or
  1306. (source.index=NR_EVAL_STACK_BASE) or (source.index=NR_LOCAL_STACK_POINTER_REG) or
  1307. (dest.base=NR_EVAL_STACK_BASE) or (dest.base=NR_LOCAL_STACK_POINTER_REG) or
  1308. (dest.index=NR_EVAL_STACK_BASE) or (dest.index=NR_LOCAL_STACK_POINTER_REG) or
  1309. (size.size in [1,2,4,8]) then
  1310. inherited
  1311. else
  1312. begin
  1313. pd:=search_system_proc('MOVE');
  1314. cgpara1.init;
  1315. cgpara2.init;
  1316. cgpara3.init;
  1317. paramanager.getcgtempparaloc(list,pd,1,cgpara1);
  1318. paramanager.getcgtempparaloc(list,pd,2,cgpara2);
  1319. paramanager.getcgtempparaloc(list,pd,3,cgpara3);
  1320. if pd.is_pushleftright then
  1321. begin
  1322. { load source }
  1323. a_loadaddr_ref_cgpara(list,voidtype,source,cgpara1);
  1324. { load destination }
  1325. a_loadaddr_ref_cgpara(list,voidtype,dest,cgpara2);
  1326. { load size }
  1327. a_load_const_cgpara(list,sizesinttype,size.size,cgpara3);
  1328. end
  1329. else
  1330. begin
  1331. { load size }
  1332. a_load_const_cgpara(list,sizesinttype,size.size,cgpara3);
  1333. { load destination }
  1334. a_loadaddr_ref_cgpara(list,voidtype,dest,cgpara2);
  1335. { load source }
  1336. a_loadaddr_ref_cgpara(list,voidtype,source,cgpara1);
  1337. end;
  1338. paramanager.freecgpara(list,cgpara3);
  1339. paramanager.freecgpara(list,cgpara2);
  1340. paramanager.freecgpara(list,cgpara1);
  1341. g_call_system_proc(list,pd,[@cgpara1,@cgpara2,@cgpara3],nil).resetiftemp;
  1342. cgpara3.done;
  1343. cgpara2.done;
  1344. cgpara1.done;
  1345. end;
  1346. end;
  1347. procedure thlcgwasm.g_proc_entry(list: TAsmList; localsize: longint; nostackframe: boolean);
  1348. var
  1349. pd: tcpuprocdef;
  1350. begin
  1351. pd:=tcpuprocdef(current_procinfo.procdef);
  1352. g_procdef(list,pd);
  1353. ttgwasm(tg).allocframepointer(list,pd.frame_pointer_ref);
  1354. ttgwasm(tg).allocbasepointer(list,pd.base_pointer_ref);
  1355. list.Concat(tai_local.create(wbt_i32,FRAME_POINTER_SYM)); //TWasmBasicType
  1356. list.Concat(tai_local.create(wbt_i32,BASE_POINTER_SYM)); //TWasmBasicType
  1357. g_fingerprint(list);
  1358. list.Concat(taicpu.op_sym(a_global_get,current_asmdata.RefAsmSymbol(STACK_POINTER_SYM,AT_LABEL)));
  1359. incstack(list,1);
  1360. list.Concat(taicpu.op_ref(a_local_set,pd.base_pointer_ref));
  1361. decstack(list,1);
  1362. if (localsize>0) then begin
  1363. list.Concat(taicpu.op_ref(a_local_get,pd.base_pointer_ref));
  1364. incstack(list,1);
  1365. list.concat(taicpu.op_const(a_i32_const, localsize ));
  1366. incstack(list,1);
  1367. list.concat(taicpu.op_none(a_i32_sub));
  1368. decstack(list,1);
  1369. list.Concat(taicpu.op_ref(a_local_set,pd.frame_pointer_ref));
  1370. decstack(list,1);
  1371. list.Concat(taicpu.op_ref(a_local_get,pd.frame_pointer_ref));
  1372. incstack(list,1);
  1373. list.Concat(taicpu.op_sym(a_global_set,current_asmdata.RefAsmSymbol(STACK_POINTER_SYM,AT_LABEL)));
  1374. decstack(list,1);
  1375. end;
  1376. end;
  1377. procedure thlcgwasm.g_proc_exit(list: TAsmList; parasize: longint; nostackframe: boolean);
  1378. var
  1379. pd: tcpuprocdef;
  1380. begin
  1381. pd:=tcpuprocdef(current_procinfo.procdef);
  1382. list.Concat(taicpu.op_ref(a_local_get,pd.base_pointer_ref));
  1383. incstack(list,1);
  1384. list.Concat(taicpu.op_sym(a_global_set,current_asmdata.RefAsmSymbol(STACK_POINTER_SYM,AT_LABEL)));
  1385. decstack(list,1);
  1386. list.concat(taicpu.op_none(a_return));
  1387. list.concat(taicpu.op_none(a_end_function));
  1388. end;
  1389. procedure thlcgwasm.g_overflowcheck(list: TAsmList; const Loc: tlocation; def: tdef);
  1390. begin
  1391. { not possible, need the original operands }
  1392. internalerror(2012102101);
  1393. end;
  1394. procedure thlcgwasm.g_overflowCheck_loc(List: TAsmList; const Loc: TLocation; def: TDef; var ovloc: tlocation);
  1395. var
  1396. hl : tasmlabel;
  1397. begin
  1398. if not(cs_check_overflow in current_settings.localswitches) then
  1399. exit;
  1400. current_asmdata.getjumplabel(hl);
  1401. a_cmp_const_loc_label(list,s32inttype,OC_EQ,0,ovloc,hl);
  1402. g_call_system_proc(list,'fpc_overflow',[],nil);
  1403. a_label(list,hl);
  1404. end;
  1405. procedure thlcgwasm.maybe_change_load_node_reg(list: TAsmList; var n: tnode; reload: boolean);
  1406. begin
  1407. { don't do anything, all registers become stack locations anyway }
  1408. end;
  1409. procedure thlcgwasm.gen_entry_code(list: TAsmList);
  1410. begin
  1411. inherited;
  1412. list.concat(taicpu.op_none(a_block));
  1413. incblock;
  1414. exitBr:=br_blocks;
  1415. end;
  1416. procedure thlcgwasm.gen_exit_code(list: TAsmList);
  1417. begin
  1418. list.concat(taicpu.op_none(a_end_block));
  1419. decblock;
  1420. if fevalstackheight<>0 then
  1421. list.concat(tai_comment.Create(strpnew('!!! values remaining on stack at end of block !!!')));
  1422. inherited;
  1423. end;
  1424. procedure thlcgwasm.a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; srcsize, dstsize: tdef; src, dst: tregister);
  1425. begin
  1426. internalerror(2012090201);
  1427. end;
  1428. procedure thlcgwasm.a_loadmm_loc_reg(list: TAsmList; fromsize, tosize: tdef; const loc: tlocation; const reg: tregister; shuffle: pmmshuffle);
  1429. begin
  1430. internalerror(2012090202);
  1431. end;
  1432. procedure thlcgwasm.a_loadmm_reg_reg(list: TAsmList; fromsize, tosize: tdef; reg1, reg2: tregister; shuffle: pmmshuffle);
  1433. begin
  1434. internalerror(2012060130);
  1435. end;
  1436. procedure thlcgwasm.a_loadmm_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; reg: tregister; shuffle: pmmshuffle);
  1437. begin
  1438. internalerror(2012060131);
  1439. end;
  1440. procedure thlcgwasm.a_loadmm_reg_ref(list: TAsmList; fromsize, tosize: tdef; reg: tregister; const ref: treference; shuffle: pmmshuffle);
  1441. begin
  1442. internalerror(2012060132);
  1443. end;
  1444. procedure thlcgwasm.a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size: tdef; src, dst: tregister; shuffle: pmmshuffle);
  1445. begin
  1446. internalerror(2012060133);
  1447. end;
  1448. procedure thlcgwasm.a_loadmm_intreg_reg(list: TAsmList; fromsize, tosize: tdef; intreg, mmreg: tregister; shuffle: pmmshuffle);
  1449. begin
  1450. internalerror(2012060134);
  1451. end;
  1452. procedure thlcgwasm.a_loadmm_reg_intreg(list: TAsmList; fromsize, tosize: tdef; mmreg, intreg: tregister; shuffle: pmmshuffle);
  1453. begin
  1454. internalerror(2012060135);
  1455. end;
  1456. procedure thlcgwasm.g_stackpointer_alloc(list: TAsmList; size: longint);
  1457. begin
  1458. internalerror(2012090203);
  1459. end;
  1460. procedure thlcgwasm.g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);
  1461. begin
  1462. internalerror(2012090204);
  1463. end;
  1464. procedure thlcgwasm.g_adjust_self_value(list: TAsmList; procdef: tprocdef; ioffset: aint);
  1465. begin
  1466. internalerror(2012090205);
  1467. end;
  1468. procedure thlcgwasm.g_local_unwind(list: TAsmList; l: TAsmLabel);
  1469. begin
  1470. internalerror(2012090206);
  1471. end;
  1472. procedure thlcgwasm.g_procdef(list: TAsmList; pd: tprocdef);
  1473. begin
  1474. list.Concat(tai_functype.create(pd.mangledname,tcpuprocdef(pd).create_functype));
  1475. end;
  1476. procedure thlcgwasm.a_load_stack_reg(list: TAsmList; size: tdef; reg: tregister);
  1477. begin
  1478. list.concat(taicpu.op_reg(a_local_set,reg));
  1479. decstack(list,1);
  1480. end;
  1481. procedure thlcgwasm.a_load_stack_ref(list: TAsmList; size: tdef; const ref: treference; extra_slots: longint);
  1482. var
  1483. opc: tasmop;
  1484. finishandval: tcgint;
  1485. begin
  1486. { fake location that indicates the value has to remain on the stack }
  1487. if ref.base=NR_EVAL_STACK_BASE then
  1488. exit;
  1489. opc:=loadstoreopcref(size,false,ref,finishandval);
  1490. list.concat(taicpu.op_ref(opc,ref));
  1491. { avoid problems with getting the size of an open array etc }
  1492. if wasmAlwayInMem(size) then
  1493. size:=ptruinttype;
  1494. decstack(list,1+extra_slots);
  1495. end;
  1496. procedure thlcgwasm.a_load_reg_stack(list: TAsmList; size: tdef; reg: tregister);
  1497. begin
  1498. list.concat(taicpu.op_reg(a_local_get,reg));
  1499. incstack(list,1);
  1500. end;
  1501. procedure thlcgwasm.a_load_ref_stack(list: TAsmList; size: tdef; const ref: treference; extra_slots: longint);
  1502. var
  1503. opc: tasmop;
  1504. finishandval: tcgint;
  1505. begin
  1506. { fake location that indicates the value is already on the stack? }
  1507. if (ref.base=NR_EVAL_STACK_BASE) then
  1508. exit;
  1509. opc:=loadstoreopcref(size,true,ref,finishandval);
  1510. list.concat(taicpu.op_ref(opc,ref));
  1511. { avoid problems with getting the size of an open array etc }
  1512. if wasmAlwayInMem(size) then
  1513. size:=ptruinttype;
  1514. incstack(list,1-extra_slots);
  1515. if finishandval<>-1 then
  1516. a_op_const_stack(list,OP_AND,size,finishandval);
  1517. // there's no cast check in Wasm
  1518. //if ref.checkcast then
  1519. // gen_typecheck(list,a_checkcast,size);
  1520. end;
  1521. function thlcgwasm.loadstoreopcref(def: tdef; isload: boolean; const ref: treference; out finishandval: tcgint): tasmop;
  1522. const
  1523. {iisload} {issigned}
  1524. getputmem8 : array [boolean, boolean] of TAsmOp = ((a_i32_store8, a_i32_store8), (a_i32_load8_u, a_i32_load8_s));
  1525. getputmem16 : array [boolean, boolean] of TAsmOp = ((a_i32_store16, a_i32_store16), (a_i32_load16_u ,a_i32_load16_s));
  1526. getputmem32 : array [boolean, boolean] of TAsmOp = ((a_i32_store, a_i32_store), (a_i32_load, a_i32_load));
  1527. getputmem64 : array [boolean, boolean] of TAsmOp = ((a_i64_store, a_i64_store), (a_i64_load, a_i64_load));
  1528. getputmemf32 : array [boolean] of TAsmOp = (a_f32_store, a_f32_load);
  1529. getputmemf64 : array [boolean] of TAsmOp = (a_f64_store, a_f64_load);
  1530. begin
  1531. if (ref.base<>NR_LOCAL_STACK_POINTER_REG) or assigned(ref.symbol) then
  1532. begin
  1533. { -> either a global (static) field, or a regular field. If a regular
  1534. field, then ref.base contains the self pointer, otherwise
  1535. ref.base=NR_NO. In both cases, the symbol contains all other
  1536. information (combined field name and type descriptor) }
  1537. case def.size of
  1538. 1: result := getputmem8[isload, is_signed(def)];
  1539. 2: result := getputmem16[isload, is_signed(def)];
  1540. 4:
  1541. if is_single(def) then
  1542. result := getputmemf32[isload]
  1543. else
  1544. result := getputmem32[isload, is_signed(def)];
  1545. 8: if is_double(def) then
  1546. result := getputmemf64[isload]
  1547. else
  1548. result := getputmem64[isload, is_signed(def)];
  1549. else
  1550. Internalerror(2019091501);
  1551. end;
  1552. //result:=getputopc[isload,ref.base=NR_NO];
  1553. finishandval:=-1;
  1554. { erase sign extension for byte/smallint loads }
  1555. if (def2regtyp(def)=R_INTREGISTER) and
  1556. not is_signed(def) and
  1557. (def.typ=orddef) and
  1558. not is_widechar(def) then
  1559. case def.size of
  1560. 1: if (torddef(def).high>127) then
  1561. finishandval:=255;
  1562. 2: if (torddef(def).high>32767) then
  1563. finishandval:=65535;
  1564. end;
  1565. end
  1566. else
  1567. begin
  1568. finishandval:=-1;
  1569. if isload then
  1570. result := a_local_get
  1571. else
  1572. result := a_local_set;
  1573. end;
  1574. end;
  1575. procedure thlcgwasm.resize_stack_int_val(list: TAsmList; fromsize, tosize: tdef; formemstore: boolean);
  1576. var
  1577. fromcgsize, tocgsize: tcgsize;
  1578. begin
  1579. { When storing to an array, field or global variable, make sure the
  1580. static type verification can determine that the stored value fits
  1581. within the boundaries of the declared type (to appease the Dalvik VM).
  1582. Local variables either get their type upgraded in the debug info,
  1583. or have no type information at all }
  1584. if formemstore and
  1585. (tosize.typ=orddef) then
  1586. if (torddef(tosize).ordtype in [u8bit,uchar]) then
  1587. tosize:=s8inttype
  1588. else if torddef(tosize).ordtype=u16bit then
  1589. tosize:=s16inttype;
  1590. fromcgsize:=def_cgsize(fromsize);
  1591. tocgsize:=def_cgsize(tosize);
  1592. if fromcgsize in [OS_S64,OS_64] then
  1593. begin
  1594. if not(tocgsize in [OS_S64,OS_64]) then
  1595. begin
  1596. { truncate }
  1597. list.concat(taicpu.op_none(a_i32_wrap_i64));
  1598. end;
  1599. end
  1600. else if tocgsize in [OS_S64,OS_64] then
  1601. begin
  1602. { extend }
  1603. case fromcgsize of
  1604. OS_8:
  1605. begin
  1606. a_op_const_stack(list,OP_AND,s32inttype,255);
  1607. list.concat(taicpu.op_none(a_i64_extend_i32_u));
  1608. end;
  1609. OS_S8:
  1610. begin
  1611. list.concat(taicpu.op_none(a_i64_extend_i32_u));
  1612. list.concat(taicpu.op_none(a_i64_extend8_s));
  1613. end;
  1614. OS_16:
  1615. begin
  1616. a_op_const_stack(list,OP_AND,s32inttype,65535);
  1617. list.concat(taicpu.op_none(a_i64_extend_i32_u));
  1618. end;
  1619. OS_S16:
  1620. begin
  1621. list.concat(taicpu.op_none(a_i64_extend_i32_u));
  1622. list.concat(taicpu.op_none(a_i64_extend16_s));
  1623. end;
  1624. OS_32:
  1625. list.concat(taicpu.op_none(a_i64_extend_i32_u));
  1626. OS_S32:
  1627. list.concat(taicpu.op_none(a_i64_extend_i32_s));
  1628. OS_64,OS_S64:
  1629. ;
  1630. else
  1631. internalerror(2021010301);
  1632. end;
  1633. end
  1634. else
  1635. begin
  1636. case fromcgsize of
  1637. OS_8:
  1638. a_op_const_stack(list,OP_AND,s32inttype,255);
  1639. OS_S8:
  1640. list.concat(taicpu.op_none(a_i32_extend8_s));
  1641. OS_16:
  1642. a_op_const_stack(list,OP_AND,s32inttype,65535);
  1643. OS_S16:
  1644. list.concat(taicpu.op_none(a_i32_extend16_s));
  1645. OS_32,OS_S32:
  1646. ;
  1647. else
  1648. internalerror(2021010302);
  1649. end;
  1650. end;
  1651. end;
  1652. procedure thlcgwasm.maybe_resize_stack_para_val(list: TAsmList; retdef: tdef; callside: boolean);
  1653. var
  1654. convsize: tdef;
  1655. begin
  1656. if (retdef.typ=orddef) then
  1657. begin
  1658. if (torddef(retdef).ordtype in [u8bit,u16bit,uchar]) and
  1659. (torddef(retdef).high>=(1 shl (retdef.size*8-1))) then
  1660. begin
  1661. convsize:=nil;
  1662. if callside then
  1663. if torddef(retdef).ordtype in [u8bit,uchar] then
  1664. convsize:=s8inttype
  1665. else
  1666. convsize:=s16inttype
  1667. else if torddef(retdef).ordtype in [u8bit,uchar] then
  1668. convsize:=u8inttype
  1669. else
  1670. convsize:=u16inttype;
  1671. if assigned(convsize) then
  1672. resize_stack_int_val(list,s32inttype,convsize,false);
  1673. end;
  1674. end;
  1675. end;
  1676. procedure thlcgwasm.g_adjust_stack_after_call(list: TAsmList; pd: tabstractprocdef);
  1677. var
  1678. totalremovesize: longint;
  1679. realresdef: tdef;
  1680. ft: TWasmFuncType;
  1681. begin
  1682. ft:=tcpuprocdef(pd).create_functype;
  1683. totalremovesize:=Length(ft.params)-Length(ft.results);
  1684. if (Length(ft.results)=0) and (po_discardresult in pd.procoptions) then
  1685. dec(totalremovesize);
  1686. { remove parameters from internal evaluation stack counter (in case of
  1687. e.g. no parameters and a result, it can also increase) }
  1688. if totalremovesize>0 then
  1689. decstack(list,totalremovesize)
  1690. else if totalremovesize<0 then
  1691. incstack(list,-totalremovesize);
  1692. ft.free;
  1693. end;
  1694. procedure thlcgwasm.g_fingerprint(list: TAsmList);
  1695. begin
  1696. list.concat(taicpu.op_const(a_i64_const,Random(high(int64))));
  1697. list.concat(taicpu.op_const(a_i64_const,Random(high(int64))));
  1698. list.concat(taicpu.op_const(a_i64_const,Random(high(int64))));
  1699. list.concat(taicpu.op_const(a_i64_const,Random(high(int64))));
  1700. list.concat(taicpu.op_none(a_drop));
  1701. list.concat(taicpu.op_none(a_drop));
  1702. list.concat(taicpu.op_none(a_drop));
  1703. list.concat(taicpu.op_none(a_drop));
  1704. end;
  1705. procedure thlcgwasm.resizestackfpuval(list: TAsmList; fromsize, tosize: tcgsize);
  1706. begin
  1707. if (fromsize=OS_F32) and
  1708. (tosize=OS_F64) then
  1709. begin
  1710. list.concat(taicpu.op_none(a_f64_promote_f32));
  1711. end
  1712. else if (fromsize=OS_F64) and
  1713. (tosize=OS_F32) then
  1714. begin
  1715. list.concat(taicpu.op_none(a_f32_demote_f64));
  1716. end;
  1717. end;
  1718. procedure create_hlcodegen_cpu;
  1719. begin
  1720. hlcg:=thlcgwasm.create;
  1721. create_codegen;
  1722. end;
  1723. initialization
  1724. chlcgobj:=thlcgwasm;
  1725. create_hlcodegen:=@create_hlcodegen_cpu;
  1726. end.