hlcgcpu.pas 80 KB

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