hlcgcpu.pas 77 KB

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