hlcgcpu.pas 81 KB

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