hlcgcpu.pas 74 KB

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