hlcgcpu.pas 75 KB

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