hlcgcpu.pas 74 KB

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