hlcgcpu.pas 81 KB

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