hlcgcpu.pas 100 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549
  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. fntypelookup : TWasmProcTypeLookup;
  40. constructor create;
  41. destructor Destroy; override;
  42. procedure incblock;
  43. procedure decblock;
  44. procedure incstack(list : TAsmList;slots: longint);
  45. procedure decstack(list : TAsmList;slots: longint);
  46. class function def2regtyp(def: tdef): tregistertype; override;
  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 g_concatcopy(list : TAsmList;size: tdef; const source,dest : treference);override;
  74. procedure g_copyshortstring(list : TAsmList;const source,dest : treference;strdef:tstringdef);override;
  75. procedure a_loadfpu_ref_ref(list: TAsmList; fromsize, tosize: tdef; const ref1, ref2: treference); override;
  76. procedure a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; reg: tregister); override;
  77. procedure a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tdef; reg: tregister; const ref: treference); override;
  78. procedure a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tdef; reg1, reg2: tregister); override;
  79. procedure g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean); override;
  80. procedure g_proc_exit(list : TAsmList;parasize:longint;nostackframe:boolean); override;
  81. procedure gen_load_return_value(list:TAsmList);override;
  82. procedure record_generated_code_for_procdef(pd: tprocdef; code, data: TAsmList); override;
  83. procedure g_incrrefcount(list : TAsmList;t: tdef; const ref: treference);override;
  84. procedure g_array_rtti_helper(list: TAsmList; t: tdef; const ref: treference; const highloc: tlocation; const name: string); override;
  85. procedure g_initialize(list : TAsmList;t : tdef;const ref : treference);override;
  86. procedure g_finalize(list : TAsmList;t : tdef;const ref : treference);override;
  87. procedure g_overflowcheck(list: TAsmList; const Loc: tlocation; def: tdef); override;
  88. procedure g_overflowCheck_loc(List:TAsmList;const Loc:TLocation;def:TDef;var ovloc : tlocation); override;
  89. procedure location_get_data_ref(list:TAsmList;def: tdef; const l:tlocation;var ref:treference;loadref:boolean; alignment: longint);override;
  90. procedure maybe_change_load_node_reg(list: TAsmList; var n: tnode; reload: boolean); override;
  91. procedure g_copyvaluepara_openarray(list: TAsmList; const ref: treference; const lenloc: tlocation; arrdef: tarraydef; destreg: tregister); override;
  92. procedure g_releasevaluepara_openarray(list: TAsmList; arrdef: tarraydef; const l: tlocation); override;
  93. procedure gen_initialize_code(list: TAsmList); override;
  94. procedure gen_entry_code(list: TAsmList); override;
  95. procedure gen_exit_code(list: TAsmList); override;
  96. { unimplemented/unnecessary routines }
  97. procedure a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; srcsize, dstsize: tdef; src, dst: tregister); override;
  98. procedure a_loadmm_loc_reg(list: TAsmList; fromsize, tosize: tdef; const loc: tlocation; const reg: tregister; shuffle: pmmshuffle); override;
  99. procedure a_loadmm_reg_reg(list: TAsmList; fromsize, tosize: tdef; reg1, reg2: tregister; shuffle: pmmshuffle); override;
  100. procedure a_loadmm_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; reg: tregister; shuffle: pmmshuffle); override;
  101. procedure a_loadmm_reg_ref(list: TAsmList; fromsize, tosize: tdef; reg: tregister; const ref: treference; shuffle: pmmshuffle); override;
  102. procedure a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size: tdef; src, dst: tregister; shuffle: pmmshuffle); override;
  103. procedure a_loadmm_intreg_reg(list: TAsmList; fromsize, tosize: tdef; intreg, mmreg: tregister; shuffle: pmmshuffle); override;
  104. procedure a_loadmm_reg_intreg(list: TAsmList; fromsize, tosize: tdef; mmreg, intreg: tregister; shuffle: pmmshuffle); override;
  105. procedure g_stackpointer_alloc(list: TAsmList; size: longint); override;
  106. procedure g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint); override;
  107. procedure g_adjust_self_value(list: TAsmList; procdef: tprocdef; ioffset: aint); override;
  108. procedure g_local_unwind(list: TAsmList; l: TAsmLabel); override;
  109. { Wasm-specific routines }
  110. procedure g_procdef(list:TAsmList;pd: tprocdef);
  111. procedure a_load_stack_reg(list : TAsmList;size: tdef;reg: tregister);
  112. { extra_slots are the slots that are used by the reference, and that
  113. will be removed by the store operation }
  114. procedure a_load_stack_ref(list : TAsmList;size: tdef;const ref: treference;extra_slots: longint);
  115. procedure a_load_reg_stack(list : TAsmList;size: tdef;reg: tregister);
  116. { extra_slots are the slots that are used by the reference, and that
  117. will be removed by the load operation }
  118. procedure a_load_ref_stack(list : TAsmList;size: tdef;const ref: treference;extra_slots: longint);
  119. procedure a_load_const_stack(list : TAsmList;size: tdef;a :tcgint; typ: TRegisterType);
  120. procedure a_load_stack_loc(list : TAsmList;size: tdef;const loc: tlocation);
  121. procedure a_load_loc_stack(list : TAsmList;size: tdef;const loc: tlocation);
  122. procedure a_loadfpu_const_stack(list : TAsmList;size: tdef;a :double);
  123. procedure a_op_stack(list : TAsmList;op: topcg; size: tdef; trunc32: boolean);
  124. procedure a_op_const_stack(list : TAsmList;op: topcg; size: tdef;a : tcgint);
  125. procedure a_op_reg_stack(list : TAsmList;op: topcg; size: tdef;reg: tregister);
  126. procedure a_op_ref_stack(list : TAsmList;op: topcg; size: tdef;const ref: treference);
  127. procedure a_op_loc_stack(list : TAsmList;op: topcg; size: tdef;const loc: tlocation);
  128. procedure g_reference_loc(list: TAsmList; def: tdef; const fromloc: tlocation; out toloc: tlocation); override;
  129. { assumes that initdim dimensions have already been pushed on the
  130. evaluation stack, and creates a new array of type arrdef with these
  131. dimensions }
  132. procedure g_newarray(list : TAsmList; arrdef: tdef; initdim: longint);
  133. { gets the length of the array whose reference is stored in arrloc,
  134. and puts it on the evaluation stack }
  135. procedure g_getarraylen(list : TAsmList; const arrloc: tlocation);
  136. { this routine expects that all values are already massaged into the
  137. required form (sign bits xor'ed for gt/lt comparisons for OS_32/OS_64,
  138. see http://stackoverflow.com/questions/4068973/c-performing-signed-comparison-in-unsigned-variables-without-casting ) }
  139. procedure a_cmp_stack_label(list : TAsmlist; size: tdef; cmp_op: topcmp; lab: tasmlabel);
  140. { these 2 routines perform the massaging expected by the previous one }
  141. procedure maybe_adjust_cmp_stackval(list : TAsmlist; size: tdef; cmp_op: topcmp);
  142. function maybe_adjust_cmp_constval(size: tdef; cmp_op: topcmp; a: tcgint): tcgint;
  143. { truncate/sign extend after performing operations on values < 32 bit
  144. that may have overflowed outside the range }
  145. procedure maybe_adjust_op_result(list: TAsmList; op: TOpCg; size: tdef);
  146. { performs sign/zero extension as required }
  147. procedure resize_stack_int_val(list: TAsmList;fromsize,tosize: tdef; formemstore: boolean);
  148. { 8/16 bit unsigned parameters and return values must be sign-extended on
  149. the producer side, because the JVM does not support unsigned variants;
  150. then they have to be zero-extended again on the consumer side }
  151. procedure maybe_resize_stack_para_val(list: TAsmList; retdef: tdef; callside: boolean);
  152. { adjust the stack height after a call based on the specified number of
  153. slots used for parameters and the provided resultdef }
  154. procedure g_adjust_stack_after_call(list: TAsmList; pd: tabstractprocdef; paraheight: longint; forceresdef: tdef);
  155. property maxevalstackheight: longint read fmaxevalstackheight;
  156. procedure gen_initialize_fields_code(list:TAsmList);
  157. procedure gen_typecheck(list: TAsmList; checkop: tasmop; checkdef: tdef);
  158. protected
  159. function get_enum_init_val_ref(def: tdef; out ref: treference): boolean;
  160. procedure allocate_implicit_structs_for_st_with_base_ref(list: TAsmList; st: tsymtable; const ref: treference; allocvartyp: tsymtyp);
  161. procedure allocate_enum_with_base_ref(list: TAsmList; vs: tabstractvarsym; const initref: treference; destbaseref: treference);
  162. procedure allocate_implicit_struct_with_base_ref(list: TAsmList; vs: tabstractvarsym; ref: treference);
  163. procedure gen_load_uninitialized_function_result(list: TAsmList; pd: tprocdef; resdef: tdef; const resloc: tcgpara); override;
  164. //procedure g_copyvalueparas(p: TObject; arg: pointer); override;
  165. procedure inittempvariables(list:TAsmList);override;
  166. function g_call_system_proc_intern(list: TAsmList; pd: tprocdef; const paras: array of pcgpara; forceresdef: tdef): tcgpara; override;
  167. { in case of an array, the array base address and index have to be
  168. put on the evaluation stack before the stored value; similarly, for
  169. fields the self pointer has to be loaded first. Also checks whether
  170. the reference is valid. If dup is true, the necessary values are stored
  171. twice. Returns how many stack slots have been consumed, disregarding
  172. the "dup". }
  173. function prepare_stack_for_ref(list: TAsmList; var ref: treference; dup: boolean): longint;
  174. { return the load/store opcode to load/store from/to ref; if the result
  175. has to be and'ed after a load to get the final value, that constant
  176. is returned in finishandval (otherwise that value is set to -1) }
  177. function loadstoreopcref(def: tdef; isload: boolean; const ref: treference; out finishandval: tcgint): tasmop;
  178. { return the load/store opcode to load/store from/to reg; if the result
  179. has to be and'ed after a load to get the final value, that constant
  180. is returned in finishandval (otherwise that value is set to -1) }
  181. function loadstoreopc(def: tdef; isload, isarray: boolean; out finishandval: tcgint): tasmop;
  182. procedure resizestackfpuval(list: TAsmList; fromsize, tosize: tcgsize);
  183. { in case of an OS_32 OP_DIV, we have to use an OS_S64 OP_IDIV because the
  184. JVM does not support unsigned divisions }
  185. procedure maybepreparedivu32(list: TAsmList; var op: topcg; size: tdef; out isdivu32: boolean);
  186. { concatcopy helpers }
  187. procedure concatcopy_normal_array(list: TAsmList; size: tdef; const source, dest: treference);
  188. procedure concatcopy_record(list: TAsmList; size: tdef; const source, dest: treference);
  189. procedure concatcopy_set(list: TAsmList; size: tdef; const source, dest: treference);
  190. procedure concatcopy_shortstring(list: TAsmList; size: tdef; const source, dest: treference);
  191. end;
  192. implementation
  193. uses
  194. verbose,cutils,globals,fmodule,constexp,
  195. defutil,
  196. aasmtai,aasmcpu,
  197. symtable,symcpu,
  198. procinfo,cpuinfo,cgcpu,tgobj,tgcpu;
  199. const
  200. TOpCG2IAsmOp : array[topcg] of TAsmOp=(
  201. A_None, {OP_NONE}
  202. A_None, {OP_MOVE, replaced operation with direct load }
  203. a_i32_add, {OP_ADD, simple addition }
  204. a_i32_and, {OP_AND, simple logical and }
  205. a_i32_div_u, {OP_DIV, simple unsigned division }
  206. a_i32_div_s, {OP_IDIV, simple signed division }
  207. a_i32_mul, {OP_IMUL, simple signed multiply }
  208. a_i32_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_i32_or, {OP_OR, simple logical or }
  212. a_i32_shr_s, {OP_SAR, arithmetic shift-right }
  213. a_i32_shl, {OP_SHL, logical shift left }
  214. a_i32_shr_u, {OP_SHR, logical shift right }
  215. a_i32_sub, {OP_SUB, simple subtraction }
  216. a_i32_xor, {OP_XOR, simple exclusive or }
  217. a_i32_rotl, {OP_ROL, rotate left }
  218. a_i32_rotr {OP_ROR rotate right }
  219. );
  220. TOpCG2LAsmOp : array[topcg] of TAsmOp=(
  221. A_None, {OP_NONE}
  222. a_i64_load, {OP_MOVE, replaced operation with direct load }
  223. a_i64_add, {OP_ADD, simple addition }
  224. a_i64_and, {OP_AND, simple logical and }
  225. a_i64_div_u, {OP_DIV, simple unsigned division }
  226. a_i64_div_s, {OP_IDIV, simple signed division }
  227. a_i64_mul, {OP_IMUL, simple signed multiply }
  228. a_i64_mul, {OP_MUL, simple unsigned multiply }
  229. A_None, {OP_NEG, simple negate } // neg = xor + 1
  230. A_None, {OP_NOT, simple logical not } // not = xor - 1
  231. a_i64_or, {OP_OR, simple logical or }
  232. a_i64_shr_s, {OP_SAR, arithmetic shift-right }
  233. a_i64_shl, {OP_SHL, logical shift left }
  234. a_i64_shr_u, {OP_SHR, logical shift right }
  235. a_i64_sub, {OP_SUB, simple subtraction }
  236. a_i64_xor, {OP_XOR, simple exclusive or }
  237. a_i64_rotl, {OP_ROL, rotate left }
  238. a_i64_rotr {OP_ROR rotate right }
  239. );
  240. constructor thlcgwasm.create;
  241. begin
  242. fevalstackheight:=0;
  243. fmaxevalstackheight:=0;
  244. fntypelookup:=TWasmProcTypeLookup.Create;
  245. end;
  246. destructor thlcgwasm.Destroy;
  247. begin
  248. fntypelookup.Free;
  249. inherited Destroy;
  250. end;
  251. procedure thlcgwasm.incblock;
  252. begin
  253. inc(br_blocks);
  254. end;
  255. procedure thlcgwasm.decblock;
  256. begin
  257. dec(br_blocks);
  258. if br_blocks<0 then Internalerror(2019091807); // out of block
  259. end;
  260. procedure thlcgwasm.incstack(list: TAsmList; slots: longint);
  261. begin
  262. if slots=0 then
  263. exit;
  264. inc(fevalstackheight,slots);
  265. if (fevalstackheight>fmaxevalstackheight) then
  266. fmaxevalstackheight:=fevalstackheight;
  267. if cs_asm_regalloc in current_settings.globalswitches then
  268. list.concat(tai_comment.Create(strpnew(' allocated '+tostr(slots)+', stack height = '+tostr(fevalstackheight))));
  269. end;
  270. procedure thlcgwasm.decstack(list: TAsmList;slots: longint);
  271. begin
  272. if slots=0 then
  273. exit;
  274. dec(fevalstackheight,slots);
  275. if (fevalstackheight<0) and
  276. not(cs_no_regalloc in current_settings.globalswitches) then
  277. internalerror(2010120501);
  278. if cs_asm_regalloc in current_settings.globalswitches then
  279. list.concat(tai_comment.Create(strpnew(' freed '+tostr(slots)+', stack height = '+tostr(fevalstackheight))));
  280. end;
  281. class function thlcgwasm.def2regtyp(def: tdef): tregistertype;
  282. begin
  283. case def.typ of
  284. { records (including files) and enums are implemented via classes }
  285. recorddef,
  286. filedef,
  287. enumdef,
  288. setdef:
  289. result:=R_ADDRESSREGISTER;
  290. { shortstrings are implemented via classes }
  291. else if is_shortstring(def) or
  292. { voiddef can only be typecasted into (implicit) pointers }
  293. is_void(def) then
  294. result:=R_ADDRESSREGISTER
  295. else
  296. result:=inherited;
  297. end;
  298. end;
  299. procedure thlcgwasm.a_load_const_cgpara(list: TAsmList; tosize: tdef; a: tcgint; const cgpara: TCGPara);
  300. begin
  301. tosize:=get_para_push_size(tosize);
  302. if tosize=s8inttype then
  303. a:=shortint(a)
  304. else if tosize=s16inttype then
  305. a:=smallint(a);
  306. inherited a_load_const_cgpara(list, tosize, a, cgpara);
  307. end;
  308. function thlcgwasm.a_call_name(list: TAsmList; pd: tprocdef; const s: TSymStr; const paras: array of pcgpara; forceresdef: tdef; weak: boolean): tcgpara;
  309. begin
  310. list.concat(taicpu.op_sym(a_call,current_asmdata.RefAsmSymbol(s,AT_FUNCTION)));
  311. result:=get_call_result_cgpara(pd,forceresdef);
  312. end;
  313. function thlcgwasm.a_call_reg(list: TAsmList; pd: tabstractprocdef; reg: tregister; const paras: array of pcgpara): tcgpara;
  314. begin
  315. a_load_reg_stack(list, ptrsinttype, reg);
  316. current_asmdata.CurrAsmList.Concat(taicpu.op_functype(a_call_indirect,tcpuprocdef(pd).create_functype));
  317. result:=hlcg.get_call_result_cgpara(pd, nil);
  318. end;
  319. procedure thlcgwasm.a_load_const_stack(list : TAsmList;size : tdef;a : tcgint; typ: TRegisterType);
  320. begin
  321. case typ of
  322. R_INTREGISTER:
  323. begin
  324. case def_cgsize(size) of
  325. OS_8,OS_16,OS_32,
  326. OS_S8,OS_S16,OS_S32:
  327. begin
  328. { convert cardinals to longints }
  329. list.concat(taicpu.op_const(a_i32_const, a));
  330. end;
  331. OS_64,OS_S64:
  332. begin
  333. list.concat(taicpu.op_const(a_i64_const, a));
  334. end;
  335. else
  336. internalerror(2010110702);
  337. end;
  338. end;
  339. R_ADDRESSREGISTER:
  340. begin
  341. if a<>0 then
  342. internalerror(2010110701);
  343. list.concat(taicpu.op_none(a_none));
  344. end;
  345. else
  346. internalerror(2010110703);
  347. end;
  348. incstack(list,1);
  349. end;
  350. procedure thlcgwasm.a_load_stack_loc(list: TAsmList; size: tdef; const loc: tlocation);
  351. var
  352. tmpref: treference;
  353. begin
  354. case loc.loc of
  355. LOC_REGISTER,LOC_CREGISTER,
  356. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  357. a_load_stack_reg(list,size,loc.register);
  358. LOC_REFERENCE:
  359. begin
  360. tmpref:=loc.reference;
  361. a_load_stack_ref(list,size,loc.reference,prepare_stack_for_ref(list,tmpref,false));
  362. end;
  363. else
  364. internalerror(2011020501);
  365. end;
  366. end;
  367. procedure thlcgwasm.a_load_loc_stack(list: TAsmList;size: tdef;const loc: tlocation);
  368. var
  369. tmpref: treference;
  370. extra_slots: LongInt;
  371. begin
  372. case loc.loc of
  373. LOC_REGISTER,LOC_CREGISTER,
  374. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  375. a_load_reg_stack(list,size,loc.register);
  376. LOC_REFERENCE,LOC_CREFERENCE:
  377. begin
  378. tmpref:=loc.reference;
  379. extra_slots:=prepare_stack_for_ref(list,tmpref,false);
  380. a_load_ref_stack(list,size,tmpref,extra_slots);
  381. end;
  382. LOC_CONSTANT:
  383. a_load_const_stack(list,size,loc.value,def2regtyp(size));
  384. else
  385. internalerror(2011010401);
  386. end;
  387. end;
  388. procedure thlcgwasm.a_loadfpu_const_stack(list: TAsmList; size: tdef; a: double);
  389. begin
  390. case tfloatdef(size).floattype of
  391. s32real:
  392. begin
  393. list.concat(taicpu.op_single(a_f32_const, a));
  394. incstack(list,1);
  395. end;
  396. s64real:
  397. begin
  398. list.concat(taicpu.op_double(a_f64_const,a));
  399. incstack(list,2);
  400. end
  401. else
  402. internalerror(2011010501);
  403. end;
  404. end;
  405. procedure thlcgwasm.a_op_stack(list: TAsmList; op: topcg; size: tdef; trunc32: boolean);
  406. var
  407. cgsize: tcgsize;
  408. begin
  409. if not trunc32 then
  410. cgsize:=def_cgsize(size)
  411. else
  412. begin
  413. resize_stack_int_val(list,u32inttype,s64inttype,false);
  414. cgsize:=OS_S64;
  415. end;
  416. case cgsize of
  417. OS_8,OS_S8,
  418. OS_16,OS_S16,
  419. OS_32,OS_S32:
  420. begin
  421. { not = xor 1 for boolean, xor -1 for the rest}
  422. if op=OP_NOT then
  423. begin
  424. if not is_pasbool(size) then
  425. a_load_const_stack(list,s32inttype,high(cardinal),R_INTREGISTER)
  426. else
  427. a_load_const_stack(list,size,1,R_INTREGISTER);
  428. op:=OP_XOR;
  429. end;
  430. if TOpCG2IAsmOp[op]=A_None then
  431. internalerror(2010120532);
  432. list.concat(taicpu.op_none(TOpCG2IAsmOp[op]));
  433. maybe_adjust_op_result(list,op,size);
  434. if op<>OP_NEG then
  435. decstack(list,1);
  436. end;
  437. OS_64,OS_S64:
  438. begin
  439. { unsigned 64 bit division must be done via a helper }
  440. if op=OP_DIV then
  441. internalerror(2010120530);
  442. { not = xor 1 for boolean, xor -1 for the rest}
  443. if op=OP_NOT then
  444. begin
  445. if not is_pasbool(size) then
  446. a_load_const_stack(list,s64inttype,-1,R_INTREGISTER)
  447. else
  448. a_load_const_stack(list,s64inttype,1,R_INTREGISTER);
  449. op:=OP_XOR;
  450. end;
  451. if TOpCG2LAsmOp[op]=A_None then
  452. internalerror(2010120533);
  453. list.concat(taicpu.op_none(TOpCG2LAsmOp[op]));
  454. case op of
  455. OP_NOT,
  456. OP_NEG:
  457. ;
  458. { the second argument here is an int rather than a long }
  459. OP_SHL,OP_SHR,OP_SAR:
  460. decstack(list,1);
  461. else
  462. decstack(list,2);
  463. end;
  464. end;
  465. else
  466. internalerror(2010120531);
  467. end;
  468. if trunc32 then
  469. begin
  470. list.concat(taicpu.op_none(a_i32_trunc_s_f32)); // todo: there are several truncs
  471. decstack(list,1);
  472. end;
  473. end;
  474. procedure thlcgwasm.a_op_const_stack(list: TAsmList;op: topcg;size: tdef;a: tcgint);
  475. var
  476. trunc32: boolean;
  477. begin
  478. maybepreparedivu32(list,op,size,trunc32);
  479. case op of
  480. OP_NEG,OP_NOT:
  481. internalerror(2011010801);
  482. OP_SHL,OP_SHR,OP_SAR:
  483. { the second argument here is an int rather than a long }
  484. a_load_const_stack(list,s32inttype,a,R_INTREGISTER);
  485. else
  486. a_load_const_stack(list,size,a,R_INTREGISTER);
  487. end;
  488. a_op_stack(list,op,size,trunc32);
  489. end;
  490. procedure thlcgwasm.a_op_reg_stack(list: TAsmList; op: topcg; size: tdef; reg: tregister);
  491. var
  492. trunc32: boolean;
  493. begin
  494. maybepreparedivu32(list,op,size,trunc32);
  495. case op of
  496. OP_SHL,OP_SHR,OP_SAR:
  497. if not is_64bitint(size) then
  498. a_load_reg_stack(list,size,reg)
  499. else
  500. begin
  501. { the second argument here is an int rather than a long }
  502. if getsubreg(reg)=R_SUBQ then
  503. internalerror(2011010802);
  504. a_load_reg_stack(list,s32inttype,reg)
  505. end
  506. else
  507. a_load_reg_stack(list,size,reg);
  508. end;
  509. a_op_stack(list,op,size,trunc32);
  510. end;
  511. procedure thlcgwasm.a_op_ref_stack(list: TAsmList; op: topcg; size: tdef; const ref: treference);
  512. var
  513. trunc32: boolean;
  514. tmpref: treference;
  515. begin
  516. { ref must not be the stack top, because that may indicate an error
  517. (it means that we will perform an operation of the stack top onto
  518. itself, so that means the two values have been loaded manually prior
  519. to calling this routine, instead of letting this routine load one of
  520. them; if something like that is needed, call a_op_stack() directly) }
  521. if ref.base=NR_EVAL_STACK_BASE then
  522. internalerror(2010121102);
  523. tmpref:=ref;
  524. maybepreparedivu32(list,op,size,trunc32);
  525. case op of
  526. OP_SHL,OP_SHR,OP_SAR:
  527. begin
  528. if not is_64bitint(size) then
  529. a_load_ref_stack(list,size,ref,prepare_stack_for_ref(list,tmpref,false))
  530. else
  531. a_load_ref_stack(list,s32inttype,ref,prepare_stack_for_ref(list,tmpref,false));
  532. end;
  533. else
  534. a_load_ref_stack(list,size,ref,prepare_stack_for_ref(list,tmpref,false));
  535. end;
  536. a_op_stack(list,op,size,trunc32);
  537. end;
  538. procedure thlcgwasm.a_op_loc_stack(list: TAsmList; op: topcg; size: tdef; const loc: tlocation);
  539. begin
  540. case loc.loc of
  541. LOC_REGISTER,LOC_CREGISTER:
  542. a_op_reg_stack(list,op,size,loc.register);
  543. LOC_REFERENCE,LOC_CREFERENCE:
  544. a_op_ref_stack(list,op,size,loc.reference);
  545. LOC_CONSTANT:
  546. a_op_const_stack(list,op,size,loc.value);
  547. else
  548. internalerror(2011011415)
  549. end;
  550. end;
  551. procedure thlcgwasm.g_reference_loc(list: TAsmList; def: tdef; const fromloc: tlocation; out toloc: tlocation);
  552. begin
  553. case fromloc.loc of
  554. LOC_CREFERENCE,
  555. LOC_REFERENCE:
  556. begin
  557. toloc:=fromloc;
  558. if (fromloc.reference.base<>NR_NO) and
  559. (fromloc.reference.base<>current_procinfo.framepointer) and
  560. (fromloc.reference.base<>NR_STACK_POINTER_REG) then
  561. g_allocload_reg_reg(list,voidpointertype,fromloc.reference.base,toloc.reference.base,R_ADDRESSREGISTER);
  562. end;
  563. else
  564. inherited;
  565. end;
  566. end;
  567. procedure thlcgwasm.g_newarray(list: TAsmList; arrdef: tdef; initdim: longint);
  568. var
  569. recref,
  570. enuminitref: treference;
  571. elemdef: tdef;
  572. i: longint;
  573. mangledname: string;
  574. opc: tasmop;
  575. primitivetype: boolean;
  576. begin
  577. internalerror(2019083001); // arrays are note yet supported
  578. (*
  579. elemdef:=arrdef;
  580. if initdim>1 then
  581. begin
  582. { multianewarray typedesc ndim }
  583. { todo: WASM
  584. list.concat(taicpu.op_sym_const(a_multianewarray,
  585. current_asmdata.RefAsmSymbol(jvmarrtype(elemdef,primitivetype),AT_METADATA),initdim));
  586. }
  587. { has to be a multi-dimensional array type }
  588. if primitivetype then
  589. internalerror(2011012207);
  590. end
  591. else
  592. begin
  593. { for primitive types:
  594. newarray typedesc
  595. for reference types:
  596. anewarray typedesc
  597. }
  598. { get the type of the elements of the array we are creating }
  599. elemdef:=tarraydef(arrdef).elementdef;
  600. { todo: WASM. Todo: array data structures needs to be stored in Memory
  601. mangledname:=jvmarrtype(elemdef,primitivetype);
  602. if primitivetype then
  603. opc:=a_newarray
  604. else
  605. opc:=a_anewarray;
  606. list.concat(taicpu.op_sym(opc,current_asmdata.RefAsmSymbol(mangledname,AT_METADATA)));
  607. }
  608. end;
  609. { all dimensions are removed from the stack, an array reference is
  610. added }
  611. decstack(list,initdim-1);
  612. { in case of an array of records, sets or shortstrings, initialise }
  613. elemdef:=tarraydef(arrdef).elementdef;
  614. for i:=1 to pred(initdim) do
  615. elemdef:=tarraydef(elemdef).elementdef;
  616. if (elemdef.typ in [recorddef,setdef]) or
  617. ((elemdef.typ=enumdef) and
  618. get_enum_init_val_ref(elemdef,enuminitref)) or
  619. is_shortstring(elemdef) or
  620. ((elemdef.typ=procvardef) and
  621. not tprocvardef(elemdef).is_addressonly) or
  622. is_ansistring(elemdef) or
  623. is_wide_or_unicode_string(elemdef) or
  624. is_dynamic_array(elemdef) then
  625. begin
  626. { duplicate array instance }
  627. list.concat(taicpu.op_none(a_dup));
  628. incstack(list,1);
  629. a_load_const_stack(list,s32inttype,initdim-1,R_INTREGISTER);
  630. case elemdef.typ of
  631. arraydef:
  632. g_call_system_proc(list,'fpc_initialize_array_dynarr',[],nil);
  633. recorddef,setdef,procvardef:
  634. begin
  635. tg.gethltemp(list,elemdef,elemdef.size,tt_persistent,recref);
  636. a_load_ref_stack(list,elemdef,recref,prepare_stack_for_ref(list,recref,false));
  637. case elemdef.typ of
  638. recorddef:
  639. g_call_system_proc(list,'fpc_initialize_array_record',[],nil);
  640. setdef:
  641. begin
  642. if tsetdef(elemdef).elementdef.typ=enumdef then
  643. g_call_system_proc(list,'fpc_initialize_array_enumset',[],nil)
  644. else
  645. g_call_system_proc(list,'fpc_initialize_array_bitset',[],nil)
  646. end;
  647. procvardef:
  648. g_call_system_proc(list,'fpc_initialize_array_procvar',[],nil);
  649. else
  650. internalerror(2019051025);
  651. end;
  652. tg.ungettemp(list,recref);
  653. end;
  654. enumdef:
  655. begin
  656. a_load_ref_stack(list,java_jlobject,enuminitref,prepare_stack_for_ref(list,enuminitref,false));
  657. g_call_system_proc(list,'fpc_initialize_array_object',[],nil);
  658. end;
  659. stringdef:
  660. begin
  661. case tstringdef(elemdef).stringtype of
  662. st_shortstring:
  663. begin
  664. a_load_const_stack_intern(list,u8inttype,tstringdef(elemdef).len,R_INTREGISTER,true);
  665. g_call_system_proc(list,'fpc_initialize_array_shortstring',[],nil);
  666. end;
  667. st_ansistring:
  668. g_call_system_proc(list,'fpc_initialize_array_ansistring',[],nil);
  669. st_unicodestring,
  670. st_widestring:
  671. g_call_system_proc(list,'fpc_initialize_array_unicodestring',[],nil);
  672. else
  673. internalerror(2011081801);
  674. end;
  675. end;
  676. else
  677. internalerror(2011081801);
  678. end;
  679. end;
  680. *)
  681. end;
  682. procedure thlcgwasm.g_getarraylen(list: TAsmList; const arrloc: tlocation);
  683. var
  684. nillab,endlab: tasmlabel;
  685. begin
  686. internalerror(2019083001); // arrays are note yet supported
  687. (*
  688. { inline because we have to use the arraylength opcode, which
  689. cannot be represented directly in Pascal. Even though the JVM
  690. supports allocated arrays with length=0, we still also have to
  691. check for nil pointers because even if FPC always generates
  692. allocated empty arrays under all circumstances, external Java
  693. code could pass in nil pointers.
  694. Note that this means that assigned(arr) can be different from
  695. length(arr)<>0 for dynamic arrays when targeting the JVM.
  696. }
  697. current_asmdata.getjumplabel(nillab);
  698. current_asmdata.getjumplabel(endlab);
  699. { if assigned(arr) ... }
  700. a_load_loc_stack(list,java_jlobject,arrloc);
  701. list.concat(taicpu.op_none(a_dup));
  702. incstack(list,1);
  703. list.concat(taicpu.op_sym(a_ifnull,nillab));
  704. decstack(list,1);
  705. { ... then result:=arraylength(arr) ... }
  706. list.concat(taicpu.op_none(a_arraylength));
  707. a_jmp_always(list,endlab);
  708. { ... else result:=0 }
  709. a_label(list,nillab);
  710. list.concat(taicpu.op_none(a_pop));
  711. decstack(list,1);
  712. list.concat(taicpu.op_none(a_iconst_0));
  713. incstack(list,1);
  714. a_label(list,endlab);
  715. *)
  716. end;
  717. procedure thlcgwasm.a_cmp_stack_label(list: TAsmlist; size: tdef; cmp_op: topcmp; lab: tasmlabel);
  718. const
  719. opcmp32: array[topcmp] of tasmop = (
  720. A_None, { OC_NONE, }
  721. a_i32_eq, { OC_EQ, equality comparison }
  722. a_i32_gt_s, { OC_GT, greater than (signed) }
  723. a_i32_lt_s, { OC_LT, less than (signed) }
  724. a_i32_ge_s, { OC_GTE, greater or equal than (signed) }
  725. a_i32_le_s, { OC_LTE, less or equal than (signed) }
  726. a_i32_ne, { OC_NE, not equal }
  727. a_i32_le_u, { OC_BE, less or equal than (unsigned) }
  728. a_i32_lt_u, { OC_B, less than (unsigned) }
  729. a_i32_ge_u, { OC_AE, greater or equal than (unsigned) }
  730. a_i32_gt_u { OC_A greater than (unsigned) }
  731. );
  732. const
  733. opcmp64: array[TOpCmp] of TAsmOp = (A_None,
  734. a_i64_eq, // OC_EQ
  735. a_i64_gt_s, a_i64_lt_s, // OC_GT, OC_LT
  736. a_i64_ge_s, a_i64_le_s, // OC_GTE, OC_LTE
  737. a_i64_ne, // OC_NE
  738. a_i64_le_u, a_i64_lt_u, // OC_BE, OC_B
  739. a_i64_ge_u, a_i64_gt_u // OC_AE, OC_A
  740. );
  741. var
  742. cgsize: tcgsize;
  743. begin
  744. // WASM doesn't have compare+jump (to label) operation
  745. // thus even though this is a_cmp_stack_label()
  746. // label operrand is ommited
  747. //
  748. // todo: it should NOT be ommitted when we're leaving a block
  749. // (i.e. Exit or break or continue operators)
  750. case def2regtyp(size) of
  751. R_INTREGISTER:
  752. begin
  753. cgsize:=def_cgsize(size);
  754. case cgsize of
  755. OS_S8,OS_8,
  756. OS_16,OS_S16,
  757. OS_S32,OS_32:
  758. begin
  759. //list.concat(taicpu.op_sym(opcmp32[cmp_op],lab));
  760. list.concat(taicpu.op_none(opcmp32[cmp_op]));
  761. decstack(list,2);
  762. end;
  763. OS_64,OS_S64:
  764. begin
  765. //list.concat(taicpu.op_none(a_lcmp));
  766. //decstack(list,3);
  767. //list.concat(taicpu.op_sym(opcmp64[cmp_op],lab));
  768. list.concat(taicpu.op_none(opcmp64[cmp_op]));
  769. decstack(list,2);
  770. end;
  771. else
  772. internalerror(2010120538);
  773. end;
  774. end;
  775. R_ADDRESSREGISTER:
  776. begin
  777. case cmp_op of
  778. OC_EQ:
  779. //list.concat(taicpu.op_sym(a_i64_eq,lab));
  780. list.concat(taicpu.op_none(a_i32_eq));
  781. OC_NE:
  782. //list.concat(taicpu.op_sym(a_i64_ne,lab));
  783. list.concat(taicpu.op_none(a_i32_ne));
  784. else
  785. internalerror(2010120537);
  786. end;
  787. decstack(list,2);
  788. end;
  789. else
  790. internalerror(2010120538);
  791. end;
  792. end;
  793. procedure thlcgwasm.maybe_adjust_cmp_stackval(list: TAsmlist; size: tdef; cmp_op: topcmp);
  794. begin
  795. { use cmp_op because eventually that's what indicates the
  796. signed/unsigned character of the operation, not the size... }
  797. if (cmp_op in [OC_EQ,OC_NE,OC_LT,OC_LTE,OC_GT,OC_GTE]) or
  798. (def2regtyp(size)<>R_INTREGISTER) then
  799. exit;
  800. { http://stackoverflow.com/questions/4068973/c-performing-signed-comparison-in-unsigned-variables-without-casting }
  801. case def_cgsize(size) of
  802. OS_32,OS_S32:
  803. a_op_const_stack(list,OP_XOR,size,cardinal($80000000));
  804. OS_64,OS_S64:
  805. a_op_const_stack(list,OP_XOR,size,tcgint($8000000000000000));
  806. else
  807. ;
  808. end;
  809. end;
  810. function thlcgwasm.maybe_adjust_cmp_constval(size: tdef; cmp_op: topcmp; a: tcgint): tcgint;
  811. begin
  812. result:=a;
  813. { use cmp_op because eventually that's what indicates the
  814. signed/unsigned character of the operation, not the size... }
  815. if (cmp_op in [OC_EQ,OC_NE,OC_LT,OC_LTE,OC_GT,OC_GTE]) or
  816. (def2regtyp(size)<>R_INTREGISTER) then
  817. exit;
  818. case def_cgsize(size) of
  819. OS_32,OS_S32:
  820. result:=a xor cardinal($80000000);
  821. OS_64,OS_S64:
  822. {$push}{$r-}
  823. result:=a xor tcgint($8000000000000000);
  824. {$pop}
  825. else
  826. ;
  827. end;
  828. end;
  829. procedure thlcgwasm.maybe_adjust_op_result(list: TAsmList; op: TOpCg; size: tdef);
  830. const
  831. overflowops = [OP_MUL,OP_SHL,OP_ADD,OP_SUB,OP_NOT,OP_NEG];
  832. begin
  833. if (op in overflowops) and
  834. (def_cgsize(size) in [OS_8,OS_S8,OS_16,OS_S16]) then
  835. resize_stack_int_val(list,s32inttype,size,false);
  836. end;
  837. procedure thlcgwasm.gen_load_uninitialized_function_result(list: TAsmList; pd: tprocdef; resdef: tdef; const resloc: tcgpara);
  838. begin
  839. { constructors don't return anything in Java }
  840. if pd.proctypeoption=potype_constructor then
  841. exit;
  842. { must return a value of the correct type on the evaluation stack }
  843. case def2regtyp(resdef) of
  844. R_INTREGISTER,
  845. R_ADDRESSREGISTER:
  846. a_load_const_cgpara(list,resdef,0,resloc);
  847. R_FPUREGISTER:
  848. case tfloatdef(resdef).floattype of
  849. s32real:
  850. begin
  851. list.concat(taicpu.op_single(a_f32_const, 0));
  852. incstack(list,1);
  853. end;
  854. s64real:
  855. begin
  856. list.concat(taicpu.op_double(a_f64_const, 0));
  857. incstack(list,2);
  858. end;
  859. else
  860. internalerror(2011010302);
  861. end
  862. else
  863. internalerror(2011010301);
  864. end;
  865. end;
  866. //procedure thlcgwasm.g_copyvalueparas(p: TObject; arg: pointer);
  867. // var
  868. // list: tasmlist;
  869. // tmpref: treference;
  870. // begin
  871. // { zero-extend < 32 bit primitive types (FPC can zero-extend when calling,
  872. // but that doesn't help when we're called from Java code or indirectly
  873. // as a procvar -- exceptions: widechar (Java-specific type) and ordinal
  874. // types whose upper bound does not set the sign bit }
  875. // if (tsym(p).typ=paravarsym) and
  876. // (tparavarsym(p).varspez in [vs_value,vs_const]) and
  877. // (tparavarsym(p).vardef.typ=orddef) and
  878. // not is_pasbool(tparavarsym(p).vardef) and
  879. // not is_widechar(tparavarsym(p).vardef) and
  880. // (tparavarsym(p).vardef.size<4) and
  881. // not is_signed(tparavarsym(p).vardef) and
  882. // (torddef(tparavarsym(p).vardef).high>=(1 shl (tparavarsym(p).vardef.size*8-1))) then
  883. // begin
  884. // list:=TAsmList(arg);
  885. // { store value in new location to keep Android verifier happy }
  886. // tg.gethltemp(list,tparavarsym(p).vardef,tparavarsym(p).vardef.size,tt_persistent,tmpref);
  887. // a_load_loc_stack(list,tparavarsym(p).vardef,tparavarsym(p).initialloc);
  888. // a_op_const_stack(list,OP_AND,tparavarsym(p).vardef,(1 shl (tparavarsym(p).vardef.size*8))-1);
  889. // a_load_stack_ref(list,tparavarsym(p).vardef,tmpref,prepare_stack_for_ref(list,tmpref,false));
  890. // location_reset_ref(tparavarsym(p).localloc,LOC_REFERENCE,def_cgsize(tparavarsym(p).vardef),4,tmpref.volatility);
  891. // tparavarsym(p).localloc.reference:=tmpref;
  892. // end;
  893. //
  894. // inherited g_copyvalueparas(p, arg);
  895. // end;
  896. procedure thlcgwasm.inittempvariables(list: TAsmList);
  897. begin
  898. { these are automatically initialised when allocated if necessary }
  899. end;
  900. function thlcgwasm.g_call_system_proc_intern(list: TAsmList; pd: tprocdef; const paras: array of pcgpara; forceresdef: tdef): tcgpara;
  901. begin
  902. result:=inherited;
  903. pd.init_paraloc_info(callerside);
  904. g_adjust_stack_after_call(list,pd,pd.callerargareasize,forceresdef);
  905. end;
  906. function thlcgwasm.prepare_stack_for_ref(list: TAsmList; var ref: treference; dup: boolean): longint;
  907. begin
  908. result:=0;
  909. { fake location that indicates the value is already on the stack? }
  910. if (ref.base=NR_EVAL_STACK_BASE) or (ref.base=NR_LOCAL_STACK_POINTER_REG) then
  911. exit;
  912. // setting up memory offset
  913. if assigned(ref.symbol) and (ref.base=NR_NO) and (ref.index=NR_NO) then
  914. begin
  915. list.Concat(taicpu.op_const(a_i32_const,0));
  916. incstack(list,1);
  917. if dup then
  918. begin
  919. list.Concat(taicpu.op_const(a_i32_const,0));
  920. incstack(list,1);
  921. end;
  922. result:=1;
  923. end
  924. else if ref.index <> NR_NO then // array access
  925. begin
  926. // it's just faster to sum two of those together
  927. list.Concat(taicpu.op_reg(a_get_local, ref.base));
  928. list.Concat(taicpu.op_reg(a_get_local, ref.index));
  929. list.Concat(taicpu.op_none(a_i32_add));
  930. incstack(list,1);
  931. if dup then
  932. begin
  933. list.Concat(taicpu.op_reg(a_get_local, ref.base));
  934. list.Concat(taicpu.op_reg(a_get_local, ref.index));
  935. list.Concat(taicpu.op_none(a_i32_add));
  936. incstack(list,1);
  937. end;
  938. ref.base:=NR_NO;
  939. ref.index:=NR_NO;
  940. result:=1;
  941. end
  942. else if (ref.base<>NR_NO) then
  943. begin
  944. if (ref.base<>NR_STACK_POINTER_REG) then
  945. begin
  946. { regular field -> load self on the stack }
  947. a_load_reg_stack(list,voidpointertype,ref.base);
  948. if dup then
  949. a_load_reg_stack(list,voidpointertype,ref.base);
  950. { field name/type encoded in symbol, no index/offset }
  951. result:=1;
  952. ref.base:=NR_NO;
  953. end
  954. else // if (ref.base = NR_FRAME_POINTER_REG) then
  955. begin
  956. list.Concat(taicpu.op_sym(a_get_local, current_asmdata.RefAsmSymbol(FRAME_POINTER_SYM,AT_ADDR) ));
  957. end;
  958. end
  959. else
  960. begin
  961. { static field -> nothing to do here, except for validity check }
  962. {if not assigned(ref.symbol) or
  963. (ref.offset<>0) then
  964. begin
  965. internalerror(2010120525);
  966. end;}
  967. end;
  968. end;
  969. procedure thlcgwasm.a_load_const_reg(list: TAsmList; tosize: tdef; a: tcgint; register: tregister);
  970. begin
  971. a_load_const_stack(list,tosize,a,def2regtyp(tosize));
  972. a_load_stack_reg(list,tosize,register);
  973. end;
  974. procedure thlcgwasm.a_load_const_ref(list: TAsmList; tosize: tdef; a: tcgint; const ref: treference);
  975. var
  976. extra_slots: longint;
  977. tmpref: treference;
  978. begin
  979. tmpref:=ref;
  980. extra_slots:=prepare_stack_for_ref(list,tmpref,false);
  981. a_load_const_stack(list,tosize,a,def2regtyp(tosize));
  982. a_load_stack_ref(list,tosize,tmpref,extra_slots);
  983. end;
  984. procedure thlcgwasm.a_load_reg_ref(list: TAsmList; fromsize, tosize: tdef; register: tregister; const ref: treference);
  985. var
  986. extra_slots: longint;
  987. tmpref: treference;
  988. begin
  989. tmpref:=ref;
  990. extra_slots:=prepare_stack_for_ref(list,tmpref,false);
  991. a_load_reg_stack(list,fromsize,register);
  992. if def2regtyp(fromsize)=R_INTREGISTER then
  993. resize_stack_int_val(list,fromsize,tosize,assigned(tmpref.symbol));
  994. a_load_stack_ref(list,tosize,tmpref,extra_slots);
  995. end;
  996. procedure thlcgwasm.a_load_reg_reg(list: TAsmList; fromsize, tosize: tdef; reg1, reg2: tregister);
  997. begin
  998. a_load_reg_stack(list,fromsize,reg1);
  999. if def2regtyp(fromsize)=R_INTREGISTER then
  1000. resize_stack_int_val(list,fromsize,tosize,false);
  1001. a_load_stack_reg(list,tosize,reg2);
  1002. end;
  1003. procedure thlcgwasm.a_load_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; register: tregister);
  1004. var
  1005. extra_slots: longint;
  1006. tmpref: treference;
  1007. begin
  1008. tmpref:=ref;
  1009. extra_slots:=prepare_stack_for_ref(list,tmpref,false);
  1010. a_load_ref_stack(list,fromsize,tmpref,extra_slots);
  1011. if def2regtyp(fromsize)=R_INTREGISTER then
  1012. resize_stack_int_val(list,fromsize,tosize,false);
  1013. a_load_stack_reg(list,tosize,register);
  1014. end;
  1015. procedure thlcgwasm.a_load_ref_ref(list: TAsmList; fromsize, tosize: tdef; const sref: treference; const dref: treference);
  1016. var
  1017. extra_sslots,
  1018. extra_dslots: longint;
  1019. tmpsref, tmpdref: treference;
  1020. begin
  1021. if sref.base<>NR_EVAL_STACK_BASE then
  1022. begin
  1023. tmpsref:=sref;
  1024. tmpdref:=dref;
  1025. { make sure the destination reference is on top, since in the end the
  1026. order has to be "destref, value" -> first create "destref, sourceref" }
  1027. extra_dslots:=prepare_stack_for_ref(list,tmpdref,false);
  1028. extra_sslots:=prepare_stack_for_ref(list,tmpsref,false);
  1029. a_load_ref_stack(list,fromsize,tmpsref,extra_sslots);
  1030. if def2regtyp(fromsize)=R_INTREGISTER then
  1031. resize_stack_int_val(list,fromsize,tosize,assigned(tmpdref.symbol));
  1032. a_load_stack_ref(list,tosize,tmpdref,extra_dslots);
  1033. end
  1034. else
  1035. inherited;
  1036. end;
  1037. procedure thlcgwasm.a_loadaddr_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; r: tregister);
  1038. var
  1039. tmpref: treference;
  1040. begin
  1041. { you can't take the address of references, that are on the local stack }
  1042. if (ref.base=NR_EVAL_STACK_BASE) or (ref.index=NR_EVAL_STACK_BASE) or
  1043. (ref.base=NR_LOCAL_STACK_POINTER_REG) or (ref.index=NR_LOCAL_STACK_POINTER_REG) then
  1044. internalerror(2021010101);
  1045. tmpref:=ref;
  1046. tmpref.base:=NR_NO;
  1047. tmpref.index:=NR_NO;
  1048. list.Concat(taicpu.op_ref(a_i32_const, tmpref));
  1049. if ref.base<>NR_NO then
  1050. begin
  1051. list.Concat(taicpu.op_reg(a_get_local,ref.base));
  1052. list.Concat(taicpu.op_none(a_i32_add));
  1053. end;
  1054. if ref.index<>NR_NO then
  1055. begin
  1056. list.Concat(taicpu.op_reg(a_get_local,ref.index));
  1057. if ref.scalefactor>1 then
  1058. begin
  1059. list.Concat(taicpu.op_const(a_i32_const,ref.scalefactor));
  1060. list.Concat(taicpu.op_none(a_i32_mul));
  1061. end;
  1062. list.Concat(taicpu.op_none(a_i32_add));
  1063. end;
  1064. incstack(list, 1);
  1065. a_load_stack_reg(list, tosize, r);
  1066. end;
  1067. procedure thlcgwasm.a_op_const_reg(list: TAsmList; Op: TOpCG; size: tdef; a: tcgint; reg: TRegister);
  1068. begin
  1069. a_op_const_reg_reg(list,op,size,a,reg,reg);
  1070. end;
  1071. procedure thlcgwasm.a_op_const_reg_reg(list: TAsmList; op: TOpCg; size: tdef; a: tcgint; src, dst: tregister);
  1072. begin
  1073. a_load_reg_stack(list,size,src);
  1074. a_op_const_stack(list,op,size,a);
  1075. a_load_stack_reg(list,size,dst);
  1076. end;
  1077. procedure thlcgwasm.a_op_const_ref(list: TAsmList; Op: TOpCG; size: tdef; a: tcgint; const ref: TReference);
  1078. var
  1079. extra_slots: longint;
  1080. tmpref: treference;
  1081. begin
  1082. tmpref:=ref;
  1083. extra_slots:=prepare_stack_for_ref(list,tmpref,true);
  1084. { TODO, here or in peepholeopt: use iinc when possible }
  1085. a_load_ref_stack(list,size,tmpref,extra_slots);
  1086. a_op_const_stack(list,op,size,a);
  1087. { for android verifier }
  1088. if (def2regtyp(size)=R_INTREGISTER) and
  1089. (assigned(tmpref.symbol)) then
  1090. resize_stack_int_val(list,size,size,true);
  1091. a_load_stack_ref(list,size,tmpref,extra_slots);
  1092. end;
  1093. procedure thlcgwasm.a_op_ref_reg(list: TAsmList; Op: TOpCG; size: tdef; const ref: TReference; reg: TRegister);
  1094. begin
  1095. if not(op in [OP_NOT,OP_NEG]) then
  1096. a_load_reg_stack(list,size,reg);
  1097. a_op_ref_stack(list,op,size,ref);
  1098. a_load_stack_reg(list,size,reg);
  1099. end;
  1100. procedure thlcgwasm.a_op_reg_reg_reg(list: TAsmList; op: TOpCg; size: tdef; src1, src2, dst: tregister);
  1101. begin
  1102. if not(op in [OP_NOT,OP_NEG]) then
  1103. a_load_reg_stack(list,size,src2);
  1104. a_op_reg_stack(list,op,size,src1);
  1105. a_load_stack_reg(list,size,dst);
  1106. end;
  1107. procedure thlcgwasm.a_op_reg_reg(list: TAsmList; Op: TOpCG; size: tdef; reg1, reg2: TRegister);
  1108. begin
  1109. a_op_reg_reg_reg(list,op,size,reg1,reg2,reg2);
  1110. end;
  1111. procedure thlcgwasm.a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tdef; a: tcgint; src, dst: tregister; setflags: boolean; var ovloc: tlocation);
  1112. var
  1113. tmpreg: tregister;
  1114. begin
  1115. if not setflags then
  1116. begin
  1117. inherited;
  1118. exit;
  1119. end;
  1120. tmpreg:=getintregister(list,size);
  1121. a_load_const_reg(list,size,a,tmpreg);
  1122. a_op_reg_reg_reg_checkoverflow(list,op,size,tmpreg,src,dst,true,ovloc);
  1123. end;
  1124. procedure thlcgwasm.a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tdef; src1, src2, dst: tregister; setflags: boolean; var ovloc: tlocation);
  1125. var
  1126. orgsrc1, orgsrc2: tregister;
  1127. docheck: boolean;
  1128. lab: tasmlabel;
  1129. begin
  1130. if not setflags then
  1131. begin
  1132. inherited;
  1133. exit;
  1134. end;
  1135. { anything else cannot overflow }
  1136. docheck:=size.size in [4,8];
  1137. if docheck then
  1138. begin
  1139. orgsrc1:=src1;
  1140. orgsrc2:=src2;
  1141. if src1=dst then
  1142. begin
  1143. orgsrc1:=getintregister(list,size);
  1144. a_load_reg_reg(list,size,size,src1,orgsrc1);
  1145. end;
  1146. if src2=dst then
  1147. begin
  1148. orgsrc2:=getintregister(list,size);
  1149. a_load_reg_reg(list,size,size,src2,orgsrc2);
  1150. end;
  1151. end;
  1152. a_op_reg_reg_reg(list,op,size,src1,src2,dst);
  1153. if docheck then
  1154. begin
  1155. { * signed overflow for addition iff
  1156. - src1 and src2 are negative and result is positive (excep in case of
  1157. subtraction, then sign of src1 has to be inverted)
  1158. - src1 and src2 are positive and result is negative
  1159. -> Simplified boolean equivalent (in terms of sign bits):
  1160. not(src1 xor src2) and (src1 xor dst)
  1161. for subtraction, multiplication: invert src1 sign bit
  1162. for division: handle separately (div by zero, low(inttype) div -1),
  1163. not supported by this code
  1164. * unsigned overflow iff carry out, aka dst < src1 or dst < src2
  1165. }
  1166. location_reset(ovloc,LOC_REGISTER,OS_S32);
  1167. { not pasbool8, because then we'd still have to convert the integer to
  1168. a boolean via branches for Dalvik}
  1169. ovloc.register:=getintregister(list,s32inttype);
  1170. if not ((size.typ=pointerdef) or
  1171. ((size.typ=orddef) and
  1172. (torddef(size).ordtype in [u64bit,u16bit,u32bit,u8bit,uchar,
  1173. pasbool1,pasbool8,pasbool16,pasbool32,pasbool64]))) then
  1174. begin
  1175. a_load_reg_stack(list,size,src1);
  1176. if op in [OP_SUB,OP_IMUL] then
  1177. a_op_stack(list,OP_NOT,size,false);
  1178. a_op_reg_stack(list,OP_XOR,size,src2);
  1179. a_op_stack(list,OP_NOT,size,false);
  1180. a_load_reg_stack(list,size,src1);
  1181. a_op_reg_stack(list,OP_XOR,size,dst);
  1182. a_op_stack(list,OP_AND,size,false);
  1183. a_op_const_stack(list,OP_SHR,size,(size.size*8)-1);
  1184. if size.size=8 then
  1185. begin
  1186. //todo: any operands needed?
  1187. list.concat(taicpu.op_none(a_i32_wrap_i64));
  1188. decstack(list,1);
  1189. end;
  1190. end
  1191. else
  1192. begin
  1193. a_load_const_stack(list,s32inttype,0,R_INTREGISTER);
  1194. current_asmdata.getjumplabel(lab);
  1195. { can be optimized by removing duplicate xor'ing to convert dst from
  1196. signed to unsigned quadrant }
  1197. a_cmp_reg_reg_label(list,size,OC_B,dst,src1,lab);
  1198. a_cmp_reg_reg_label(list,size,OC_B,dst,src2,lab);
  1199. a_op_const_stack(list,OP_XOR,s32inttype,1);
  1200. a_label(list,lab);
  1201. end;
  1202. a_load_stack_reg(list,s32inttype,ovloc.register);
  1203. end
  1204. else
  1205. ovloc.loc:=LOC_VOID;
  1206. end;
  1207. procedure thlcgwasm.a_cmp_const_ref_label(list: TAsmList; size: tdef; cmp_op: topcmp; a: tcgint; const ref: treference; l: tasmlabel);
  1208. var
  1209. tmpref: treference;
  1210. begin
  1211. tmpref:=ref;
  1212. if tmpref.base<>NR_EVAL_STACK_BASE then
  1213. a_load_ref_stack(list,size,tmpref,prepare_stack_for_ref(list,tmpref,false));
  1214. maybe_adjust_cmp_stackval(list,size,cmp_op);
  1215. a_load_const_stack(list,size,maybe_adjust_cmp_constval(size,cmp_op,a),def2regtyp(size));
  1216. a_cmp_stack_label(list,size,cmp_op,l);
  1217. end;
  1218. procedure thlcgwasm.a_cmp_const_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; a: tcgint; reg: tregister; l: tasmlabel);
  1219. begin
  1220. a_load_reg_stack(list,size,reg);
  1221. maybe_adjust_cmp_stackval(list,size,cmp_op);
  1222. a_load_const_stack(list,size,maybe_adjust_cmp_constval(size,cmp_op,a),def2regtyp(size));
  1223. a_cmp_stack_label(list,size,cmp_op,l);
  1224. end;
  1225. procedure thlcgwasm.a_cmp_ref_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; const ref: treference; reg: tregister; l: tasmlabel);
  1226. var
  1227. tmpref: treference;
  1228. begin
  1229. tmpref:=ref;
  1230. a_load_reg_stack(list,size,reg);
  1231. maybe_adjust_cmp_stackval(list,size,cmp_op);
  1232. if tmpref.base<>NR_EVAL_STACK_BASE then
  1233. a_load_ref_stack(list,size,tmpref,prepare_stack_for_ref(list,tmpref,false))
  1234. else begin
  1235. // todo: need a swap operation?
  1236. //list.concat(taicpu.op_none(a_swap));
  1237. Internalerror(2019083003);
  1238. end;
  1239. maybe_adjust_cmp_stackval(list,size,cmp_op);
  1240. a_cmp_stack_label(list,size,cmp_op,l);
  1241. end;
  1242. procedure thlcgwasm.a_cmp_reg_ref_label(list: TAsmList; size: tdef; cmp_op: topcmp; reg: tregister; const ref: treference; l: tasmlabel);
  1243. var
  1244. tmpref: treference;
  1245. begin
  1246. tmpref:=ref;
  1247. if tmpref.base<>NR_EVAL_STACK_BASE then
  1248. a_load_ref_stack(list,size,ref,prepare_stack_for_ref(list,tmpref,false));
  1249. maybe_adjust_cmp_stackval(list,size,cmp_op);
  1250. a_load_reg_stack(list,size,reg);
  1251. maybe_adjust_cmp_stackval(list,size,cmp_op);
  1252. a_cmp_stack_label(list,size,cmp_op,l);
  1253. end;
  1254. procedure thlcgwasm.a_cmp_reg_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; reg1, reg2: tregister; l: tasmlabel);
  1255. begin
  1256. a_load_reg_stack(list,size,reg2);
  1257. maybe_adjust_cmp_stackval(list,size,cmp_op);
  1258. a_load_reg_stack(list,size,reg1);
  1259. maybe_adjust_cmp_stackval(list,size,cmp_op);
  1260. a_cmp_stack_label(list,size,cmp_op,l);
  1261. end;
  1262. procedure thlcgwasm.a_jmp_always(list: TAsmList; l: tasmlabel);
  1263. begin
  1264. //todo: for proper jumping it's necessary to check
  1265. // all active br_blocks (if, block, loops)
  1266. // and jump to the proper one.
  1267. //list.concat(taicpu.op_const(a_i32_const, 0));
  1268. if l = current_procinfo.CurrBreakLabel then begin
  1269. // todo: this should be moved to node generator pass2
  1270. list.concat(taicpu.op_const(a_br,br_blocks-loopBreakBr))
  1271. end else if l = current_procinfo.CurrContinueLabel then begin
  1272. list.concat(taicpu.op_const(a_br,br_blocks-loopContBr))
  1273. end else begin
  1274. Internalerror(2019091806); // unexpected jump
  1275. end;
  1276. end;
  1277. procedure thlcgwasm.concatcopy_normal_array(list: TAsmList; size: tdef; const source, dest: treference);
  1278. var
  1279. procname: string;
  1280. eledef: tdef;
  1281. ndim: longint;
  1282. adddefaultlenparas: boolean;
  1283. tmpsource, tmpdest: treference;
  1284. begin
  1285. tmpsource:=source;
  1286. tmpdest:=dest;
  1287. { load copy helper parameters on the stack }
  1288. a_load_ref_stack(list,ptruinttype,source,prepare_stack_for_ref(list,tmpsource,false));
  1289. a_load_ref_stack(list,ptruinttype,dest,prepare_stack_for_ref(list,tmpdest,false));
  1290. { call copy helper }
  1291. eledef:=tarraydef(size).elementdef;
  1292. ndim:=1;
  1293. adddefaultlenparas:=true;
  1294. case eledef.typ of
  1295. orddef:
  1296. begin
  1297. case torddef(eledef).ordtype of
  1298. pasbool1,pasbool8,s8bit,u8bit,bool8bit,uchar,
  1299. s16bit,u16bit,bool16bit,pasbool16,
  1300. uwidechar,
  1301. s32bit,u32bit,bool32bit,pasbool32,
  1302. s64bit,u64bit,bool64bit,pasbool64,scurrency:
  1303. procname:='FPC_COPY_SHALLOW_ARRAY'
  1304. else
  1305. internalerror(2011020504);
  1306. end;
  1307. end;
  1308. arraydef:
  1309. begin
  1310. { call fpc_setlength_dynarr_multidim with deepcopy=true, and extra
  1311. parameters }
  1312. while (eledef.typ=arraydef) and
  1313. not is_dynamic_array(eledef) do
  1314. begin
  1315. eledef:=tarraydef(eledef).elementdef;
  1316. inc(ndim)
  1317. end;
  1318. if (ndim=1) then
  1319. procname:='FPC_COPY_SHALLOW_ARRAY'
  1320. else
  1321. begin
  1322. { deepcopy=true }
  1323. a_load_const_stack(list,pasbool1type,1,R_INTREGISTER);
  1324. { ndim }
  1325. a_load_const_stack(list,s32inttype,ndim,R_INTREGISTER);
  1326. { eletype }
  1327. { todo: WASM
  1328. a_load_const_stack(list,cwidechartype,ord(jvmarrtype_setlength(eledef)),R_INTREGISTER);
  1329. }
  1330. adddefaultlenparas:=false;
  1331. procname:='FPC_SETLENGTH_DYNARR_MULTIDIM';
  1332. end;
  1333. end;
  1334. recorddef:
  1335. procname:='FPC_COPY_JRECORD_ARRAY';
  1336. procvardef:
  1337. if tprocvardef(eledef).is_addressonly then
  1338. procname:='FPC_COPY_SHALLOW_ARRAY'
  1339. else
  1340. procname:='FPC_COPY_JPROCVAR_ARRAY';
  1341. setdef:
  1342. if tsetdef(eledef).elementdef.typ=enumdef then
  1343. procname:='FPC_COPY_JENUMSET_ARRAY'
  1344. else
  1345. procname:='FPC_COPY_JBITSET_ARRAY';
  1346. floatdef:
  1347. procname:='FPC_COPY_SHALLOW_ARRAY';
  1348. stringdef:
  1349. if is_shortstring(eledef) then
  1350. procname:='FPC_COPY_JSHORTSTRING_ARRAY'
  1351. else
  1352. procname:='FPC_COPY_SHALLOW_ARRAY';
  1353. variantdef:
  1354. begin
  1355. {$ifndef nounsupported}
  1356. procname:='FPC_COPY_SHALLOW_ARRAY';
  1357. {$else}
  1358. { todo: make a deep copy via clone... }
  1359. internalerror(2011020505);
  1360. {$endif}
  1361. end;
  1362. else
  1363. procname:='FPC_COPY_SHALLOW_ARRAY';
  1364. end;
  1365. if adddefaultlenparas then
  1366. begin
  1367. { -1, -1 means "copy entire array" }
  1368. a_load_const_stack(list,s32inttype,-1,R_INTREGISTER);
  1369. a_load_const_stack(list,s32inttype,-1,R_INTREGISTER);
  1370. end;
  1371. g_call_system_proc(list,procname,[],nil);
  1372. if ndim<>1 then
  1373. begin
  1374. { pop return value, must be the same as dest }
  1375. //list.concat(taicpu.op_none(a_pop));
  1376. Internalerror(2019083001); // no support for arrays
  1377. decstack(list,1);
  1378. end;
  1379. end;
  1380. procedure thlcgwasm.concatcopy_record(list: TAsmList; size: tdef; const source, dest: treference);
  1381. var
  1382. srsym: tsym;
  1383. pd: tprocdef;
  1384. tmpsource, tmpdest: treference;
  1385. begin
  1386. tmpsource:=source;
  1387. tmpdest:=dest;
  1388. { self }
  1389. a_load_ref_stack(list,size,tmpsource,prepare_stack_for_ref(list,tmpsource,false));
  1390. { result }
  1391. a_load_ref_stack(list,size,tmpdest,prepare_stack_for_ref(list,tmpdest,false));
  1392. { call fpcDeepCopy helper }
  1393. srsym:=search_struct_member(tabstractrecorddef(size),'FPCDEEPCOPY');
  1394. if not assigned(srsym) or
  1395. (srsym.typ<>procsym) then
  1396. Message1(cg_f_unknown_compilerproc,size.typename+'.fpcDeepCopy');
  1397. pd:=tprocdef(tprocsym(srsym).procdeflist[0]);
  1398. a_call_name(list,pd,pd.mangledname,[],nil,false);
  1399. { both parameters are removed, no function result }
  1400. decstack(list,2);
  1401. end;
  1402. procedure thlcgwasm.concatcopy_set(list: TAsmList; size: tdef; const source, dest: treference);
  1403. var
  1404. tmpsource, tmpdest: treference;
  1405. begin
  1406. tmpsource:=source;
  1407. tmpdest:=dest;
  1408. a_load_ref_stack(list,size,tmpsource,prepare_stack_for_ref(list,tmpsource,false));
  1409. a_load_ref_stack(list,size,tmpdest,prepare_stack_for_ref(list,tmpdest,false));
  1410. { call set copy helper }
  1411. if tsetdef(size).elementdef.typ=enumdef then
  1412. g_call_system_proc(list,'fpc_enumset_copy',[],nil)
  1413. else
  1414. g_call_system_proc(list,'fpc_bitset_copy',[],nil);
  1415. end;
  1416. procedure thlcgwasm.concatcopy_shortstring(list: TAsmList; size: tdef; const source, dest: treference);
  1417. var
  1418. srsym: tsym;
  1419. pd: tprocdef;
  1420. tmpsource, tmpdest: treference;
  1421. begin
  1422. tmpsource:=source;
  1423. tmpdest:=dest;
  1424. { self }
  1425. a_load_ref_stack(list,size,tmpsource,prepare_stack_for_ref(list,tmpsource,false));
  1426. { result }
  1427. a_load_ref_stack(list,size,tmpdest,prepare_stack_for_ref(list,tmpdest,false));
  1428. { call fpcDeepCopy helper }
  1429. srsym:=search_struct_member(java_shortstring,'FPCDEEPCOPY');
  1430. if not assigned(srsym) or
  1431. (srsym.typ<>procsym) then
  1432. Message1(cg_f_unknown_compilerproc,'ShortstringClass.FpcDeepCopy');
  1433. pd:=tprocdef(tprocsym(srsym).procdeflist[0]);
  1434. a_call_name(list,pd,pd.mangledname,[],nil,false);
  1435. { both parameters are removed, no function result }
  1436. decstack(list,2);
  1437. end;
  1438. procedure thlcgwasm.g_concatcopy(list: TAsmList; size: tdef; const source, dest: treference);
  1439. var
  1440. handled: boolean;
  1441. begin
  1442. handled:=false;
  1443. case size.typ of
  1444. arraydef:
  1445. begin
  1446. if not is_dynamic_array(size) then
  1447. begin
  1448. concatcopy_normal_array(list,size,source,dest);
  1449. handled:=true;
  1450. end;
  1451. end;
  1452. recorddef:
  1453. begin
  1454. concatcopy_record(list,size,source,dest);
  1455. handled:=true;
  1456. end;
  1457. setdef:
  1458. begin
  1459. concatcopy_set(list,size,source,dest);
  1460. handled:=true;
  1461. end;
  1462. stringdef:
  1463. begin
  1464. if is_shortstring(size) then
  1465. begin
  1466. concatcopy_shortstring(list,size,source,dest);
  1467. handled:=true;
  1468. end;
  1469. end;
  1470. else
  1471. ;
  1472. end;
  1473. if not handled then
  1474. inherited;
  1475. end;
  1476. procedure thlcgwasm.g_copyshortstring(list: TAsmList; const source, dest: treference; strdef: tstringdef);
  1477. begin
  1478. concatcopy_shortstring(list,strdef,source,dest);
  1479. end;
  1480. procedure thlcgwasm.a_loadfpu_ref_ref(list: TAsmList; fromsize, tosize: tdef; const ref1, ref2: treference);
  1481. var
  1482. dstack_slots: longint;
  1483. tmpref1, tmpref2: treference;
  1484. begin
  1485. tmpref1:=ref1;
  1486. tmpref2:=ref2;
  1487. dstack_slots:=prepare_stack_for_ref(list,tmpref2,false);
  1488. a_load_ref_stack(list,fromsize,tmpref1,prepare_stack_for_ref(list,tmpref1,false));
  1489. resizestackfpuval(list,def_cgsize(fromsize),def_cgsize(tosize));
  1490. a_load_stack_ref(list,tosize,tmpref2,dstack_slots);
  1491. end;
  1492. procedure thlcgwasm.a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; reg: tregister);
  1493. var
  1494. tmpref: treference;
  1495. begin
  1496. tmpref:=ref;
  1497. a_load_ref_stack(list,fromsize,tmpref,prepare_stack_for_ref(list,tmpref,false));
  1498. resizestackfpuval(list,def_cgsize(fromsize),def_cgsize(tosize));
  1499. a_load_stack_reg(list,tosize,reg);
  1500. end;
  1501. procedure thlcgwasm.a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tdef; reg: tregister; const ref: treference);
  1502. var
  1503. dstack_slots: longint;
  1504. tmpref: treference;
  1505. begin
  1506. tmpref:=ref;
  1507. dstack_slots:=prepare_stack_for_ref(list,tmpref,false);
  1508. a_load_reg_stack(list,fromsize,reg);
  1509. resizestackfpuval(list,def_cgsize(fromsize),def_cgsize(tosize));
  1510. a_load_stack_ref(list,tosize,tmpref,dstack_slots);
  1511. end;
  1512. procedure thlcgwasm.a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tdef; reg1, reg2: tregister);
  1513. begin
  1514. a_load_reg_stack(list,fromsize,reg1);
  1515. resizestackfpuval(list,def_cgsize(fromsize),def_cgsize(tosize));
  1516. a_load_stack_reg(list,tosize,reg2);
  1517. end;
  1518. procedure thlcgwasm.g_proc_entry(list: TAsmList; localsize: longint; nostackframe: boolean);
  1519. var
  1520. pd: tcpuprocdef;
  1521. begin
  1522. pd:=tcpuprocdef(current_procinfo.procdef);
  1523. g_procdef(list,pd);
  1524. ttgwasm(tg).allocframepointer(list,pd.frame_pointer_ref);
  1525. ttgwasm(tg).allocbasepointer(list,pd.base_pointer_ref);
  1526. { the localsize is based on tg.lasttemp -> already in terms of stack
  1527. slots rather than bytes }
  1528. //list.concat(tai_directive.Create(asd_jlimit,'locals '+tostr(localsize)));
  1529. { we insert the unit initialisation code afterwards in the proginit code,
  1530. and it uses one stack slot }
  1531. //if (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1532. //fmaxevalstackheight:=max(1,fmaxevalstackheight);
  1533. list.Concat(tai_local.create(wbt_i32,FRAME_POINTER_SYM)); //TWasmBasicType
  1534. list.Concat(tai_local.create(wbt_i32,BASE_POINTER_SYM)); //TWasmBasicType
  1535. list.Concat(taicpu.op_sym(a_get_global,current_asmdata.RefAsmSymbol(STACK_POINTER_SYM,AT_LABEL)));
  1536. list.Concat(taicpu.op_ref(a_set_local,pd.base_pointer_ref));
  1537. if (localsize>0) then begin
  1538. list.Concat(taicpu.op_ref(a_get_local,pd.base_pointer_ref));
  1539. list.concat(taicpu.op_const(a_i32_const, localsize ));
  1540. list.concat(taicpu.op_none(a_i32_sub));
  1541. list.Concat(taicpu.op_ref(a_set_local,pd.frame_pointer_ref));
  1542. list.Concat(taicpu.op_ref(a_get_local,pd.frame_pointer_ref));
  1543. list.Concat(taicpu.op_sym(a_set_global,current_asmdata.RefAsmSymbol(STACK_POINTER_SYM,AT_LABEL)));
  1544. end;
  1545. //list.concat(tai_directive.Create(asd_jlimit,'stack '+tostr(fmaxevalstackheight)));
  1546. end;
  1547. procedure thlcgwasm.g_proc_exit(list: TAsmList; parasize: longint; nostackframe: boolean);
  1548. var
  1549. pd: tcpuprocdef;
  1550. begin
  1551. pd:=tcpuprocdef(current_procinfo.procdef);
  1552. list.Concat(taicpu.op_ref(a_get_local,pd.base_pointer_ref));
  1553. list.Concat(taicpu.op_sym(a_set_global,current_asmdata.RefAsmSymbol(STACK_POINTER_SYM,AT_LABEL)));
  1554. list.concat(taicpu.op_none(a_return));
  1555. list.concat(taicpu.op_none(a_end_function));
  1556. end;
  1557. procedure thlcgwasm.gen_load_return_value(list: TAsmList);
  1558. begin
  1559. { constructors don't return anything in the jvm }
  1560. if current_procinfo.procdef.proctypeoption in [potype_constructor,potype_class_constructor] then
  1561. exit;
  1562. inherited gen_load_return_value(list);
  1563. end;
  1564. procedure thlcgwasm.record_generated_code_for_procdef(pd: tprocdef; code, data: TAsmList);
  1565. begin
  1566. { add something to the al_procedures list as well, because if all al_*
  1567. lists are empty, the assembler writer isn't called }
  1568. if not code.empty and
  1569. current_asmdata.asmlists[al_procedures].empty then
  1570. current_asmdata.asmlists[al_procedures].concat(tai_align.Create(4));
  1571. tcpuprocdef(pd).exprasmlist:=TAsmList.create;
  1572. new_section(tcpuprocdef(pd).exprasmlist,sec_code,lower(pd.mangledname),current_settings.alignment.procalign);
  1573. tcpuprocdef(pd).exprasmlist.concatlist(code);
  1574. if assigned(data) and
  1575. not data.empty then
  1576. internalerror(2010122801);
  1577. end;
  1578. procedure thlcgwasm.g_incrrefcount(list: TAsmList; t: tdef; const ref: treference);
  1579. begin
  1580. // do nothing
  1581. end;
  1582. procedure thlcgwasm.g_array_rtti_helper(list: TAsmList; t: tdef; const ref: treference; const highloc: tlocation; const name: string);
  1583. var
  1584. normaldim: longint;
  1585. eleref, tmpref: treference;
  1586. begin
  1587. { only in case of initialisation, we have to set all elements to "empty" }
  1588. if name<>'fpc_initialize_array' then
  1589. exit;
  1590. { put array on the stack }
  1591. tmpref:=ref;
  1592. a_load_ref_stack(list,ptruinttype,tmpref,prepare_stack_for_ref(list,tmpref,false));
  1593. { in case it's an open array whose elements are regular arrays, put the
  1594. dimension of the regular arrays on the stack (otherwise pass 0) }
  1595. normaldim:=0;
  1596. while (t.typ=arraydef) and
  1597. not is_dynamic_array(t) do
  1598. begin
  1599. inc(normaldim);
  1600. t:=tarraydef(t).elementdef;
  1601. end;
  1602. a_load_const_stack(list,s32inttype,normaldim,R_INTREGISTER);
  1603. { highloc is invalid, the length is part of the array in Java }
  1604. if is_wide_or_unicode_string(t) then
  1605. g_call_system_proc(list,'fpc_initialize_array_unicodestring',[],nil)
  1606. else if is_ansistring(t) then
  1607. g_call_system_proc(list,'fpc_initialize_array_ansistring',[],nil)
  1608. else if is_dynamic_array(t) then
  1609. g_call_system_proc(list,'fpc_initialize_array_dynarr',[],nil)
  1610. else if is_record(t) or
  1611. (t.typ=setdef) then
  1612. begin
  1613. tg.gethltemp(list,t,t.size,tt_persistent,eleref);
  1614. a_load_ref_stack(list,t,eleref,prepare_stack_for_ref(list,eleref,false));
  1615. if is_record(t) then
  1616. g_call_system_proc(list,'fpc_initialize_array_record',[],nil)
  1617. else if tsetdef(t).elementdef.typ=enumdef then
  1618. g_call_system_proc(list,'fpc_initialize_array_enumset',[],nil)
  1619. else
  1620. g_call_system_proc(list,'fpc_initialize_array_bitset',[],nil);
  1621. tg.ungettemp(list,eleref);
  1622. end
  1623. else if (t.typ=enumdef) then
  1624. begin
  1625. if get_enum_init_val_ref(t,eleref) then
  1626. begin
  1627. a_load_ref_stack(list,ptruinttype,eleref,prepare_stack_for_ref(list,eleref,false));
  1628. g_call_system_proc(list,'fpc_initialize_array_object',[],nil);
  1629. end;
  1630. end
  1631. else
  1632. internalerror(2011031901);
  1633. end;
  1634. procedure thlcgwasm.g_initialize(list: TAsmList; t: tdef; const ref: treference);
  1635. var
  1636. dummyloc: tlocation;
  1637. sym: tsym;
  1638. pd: tprocdef;
  1639. tmpref: treference;
  1640. begin
  1641. if (t.typ=arraydef) and
  1642. not is_dynamic_array(t) then
  1643. begin
  1644. dummyloc.loc:=LOC_INVALID;
  1645. g_array_rtti_helper(list,tarraydef(t).elementdef,ref,dummyloc,'fpc_initialize_array')
  1646. end
  1647. else if is_record(t) then
  1648. begin
  1649. { call the fpcInitializeRec method }
  1650. sym:=tsym(trecorddef(t).symtable.find('FPCINITIALIZEREC'));
  1651. if assigned(sym) and
  1652. (sym.typ=procsym) then
  1653. begin
  1654. if tprocsym(sym).procdeflist.Count<>1 then
  1655. internalerror(2011071713);
  1656. pd:=tprocdef(tprocsym(sym).procdeflist[0]);
  1657. end
  1658. else
  1659. internalerror(2013113008);
  1660. tmpref:=ref;
  1661. a_load_ref_stack(list,ptruinttype,ref,prepare_stack_for_ref(list,tmpref,false));
  1662. a_call_name(list,pd,pd.mangledname,[],nil,false);
  1663. { parameter removed, no result }
  1664. decstack(list,1);
  1665. end
  1666. else
  1667. a_load_const_ref(list,t,0,ref);
  1668. end;
  1669. procedure thlcgwasm.g_finalize(list: TAsmList; t: tdef; const ref: treference);
  1670. begin
  1671. // do nothing
  1672. end;
  1673. procedure thlcgwasm.g_overflowcheck(list: TAsmList; const Loc: tlocation; def: tdef);
  1674. begin
  1675. { not possible, need the original operands }
  1676. internalerror(2012102101);
  1677. end;
  1678. procedure thlcgwasm.g_overflowCheck_loc(List: TAsmList; const Loc: TLocation; def: TDef; var ovloc: tlocation);
  1679. var
  1680. hl : tasmlabel;
  1681. begin
  1682. if not(cs_check_overflow in current_settings.localswitches) then
  1683. exit;
  1684. current_asmdata.getjumplabel(hl);
  1685. a_cmp_const_loc_label(list,s32inttype,OC_EQ,0,ovloc,hl);
  1686. g_call_system_proc(list,'fpc_overflow',[],nil);
  1687. a_label(list,hl);
  1688. end;
  1689. procedure thlcgwasm.location_get_data_ref(list: TAsmList; def: tdef; const l: tlocation; var ref: treference; loadref: boolean; alignment: longint);
  1690. var
  1691. tmploc: tlocation;
  1692. begin
  1693. { This routine is a combination of a generalised a_loadaddr_ref_reg()
  1694. that also works for addresses in registers (in case loadref is false)
  1695. and of a_load_ref_reg (in case loadref is true). It is used for
  1696. a) getting the address of managed var/out parameters
  1697. b) getting to the actual data of value types that are passed by
  1698. reference by the compiler (and then get a local copy at the caller
  1699. side). Normally, depending on whether this reference is passed in a
  1700. register or reference, we either need a reference with that register
  1701. as base or load the address in that reference and use that as a new
  1702. base.
  1703. Since the JVM cannot take the address of anything, all
  1704. "pass-by-reference" value parameters (which are always aggregate types)
  1705. are already simply the implicit pointer to the data (since arrays,
  1706. records, etc are already internally implicit pointers). This means
  1707. that if "loadref" is true, we must simply return this implicit pointer.
  1708. If it is false, we are supposed the take the address of this implicit
  1709. pointer, which is not possible.
  1710. However, managed types are also implicit pointers in Pascal, so in that
  1711. case "taking the address" again consists of simply returning the
  1712. implicit pointer/current value (in case of a var/out parameter, this
  1713. value is stored inside an array).
  1714. }
  1715. if not loadref then
  1716. begin
  1717. if not is_managed_type(def) then
  1718. internalerror(2011020601);
  1719. tmploc:=l;
  1720. end
  1721. else
  1722. begin
  1723. if not wasmAlwayInMem(def) then
  1724. begin
  1725. { passed by reference in array of single element; l contains the
  1726. base address of the array }
  1727. location_reset_ref(tmploc,LOC_REFERENCE,OS_ADDR,4,ref.volatility);
  1728. cgutils.reference_reset_base(tmploc.reference,getaddressregister(list,ptruinttype),0,tmploc.reference.temppos,4,ref.volatility);
  1729. a_load_loc_reg(list,ptruinttype,ptruinttype,l,tmploc.reference.base);
  1730. end
  1731. else
  1732. tmploc:=l;
  1733. end;
  1734. case tmploc.loc of
  1735. LOC_REGISTER,
  1736. LOC_CREGISTER :
  1737. begin
  1738. { the implicit pointer is in a register and has to be in a
  1739. reference -> create a reference and put it there }
  1740. location_force_mem(list,tmploc,ptruinttype);
  1741. ref:=tmploc.reference;
  1742. end;
  1743. LOC_REFERENCE,
  1744. LOC_CREFERENCE :
  1745. begin
  1746. ref:=tmploc.reference;
  1747. end;
  1748. else
  1749. internalerror(2011020603);
  1750. end;
  1751. end;
  1752. procedure thlcgwasm.maybe_change_load_node_reg(list: TAsmList; var n: tnode; reload: boolean);
  1753. begin
  1754. { don't do anything, all registers become stack locations anyway }
  1755. end;
  1756. procedure thlcgwasm.g_copyvaluepara_openarray(list: TAsmList; const ref: treference; const lenloc: tlocation; arrdef: tarraydef; destreg: tregister);
  1757. var
  1758. localref: treference;
  1759. arrloc: tlocation;
  1760. stackslots: longint;
  1761. begin
  1762. { temporary reference for passing to concatcopy }
  1763. tg.gethltemp(list,ptruinttype,ptruinttype.size,tt_persistent,localref);
  1764. stackslots:=prepare_stack_for_ref(list,localref,false);
  1765. { create the local copy of the array (lenloc is invalid, get length
  1766. directly from the array) }
  1767. location_reset_ref(arrloc,LOC_REFERENCE,OS_ADDR,sizeof(pint),ref.volatility);
  1768. arrloc.reference:=ref;
  1769. g_getarraylen(list,arrloc);
  1770. g_newarray(list,arrdef,1);
  1771. a_load_stack_ref(list,ptruinttype,localref,stackslots);
  1772. { copy the source array to the destination }
  1773. g_concatcopy(list,arrdef,ref,localref);
  1774. { and put the array pointer in the register as expected by the caller }
  1775. a_load_ref_reg(list,ptruinttype,ptruinttype,localref,destreg);
  1776. end;
  1777. procedure thlcgwasm.g_releasevaluepara_openarray(list: TAsmList; arrdef: tarraydef; const l: tlocation);
  1778. begin
  1779. // do nothing, long live garbage collection!
  1780. end;
  1781. procedure thlcgwasm.gen_initialize_code(list: TAsmList);
  1782. var
  1783. ref: treference;
  1784. begin
  1785. { create globals with wrapped types such as arrays/records }
  1786. case current_procinfo.procdef.proctypeoption of
  1787. potype_unitinit:
  1788. begin
  1789. cgutils.reference_reset_base(ref,NR_NO,0,ctempposinvalid,1,[]);
  1790. if assigned(current_module.globalsymtable) then
  1791. allocate_implicit_structs_for_st_with_base_ref(list,current_module.globalsymtable,ref,staticvarsym);
  1792. allocate_implicit_structs_for_st_with_base_ref(list,current_module.localsymtable,ref,staticvarsym);
  1793. end;
  1794. potype_class_constructor:
  1795. begin
  1796. { also initialise local variables, if any }
  1797. inherited;
  1798. { initialise class fields }
  1799. cgutils.reference_reset_base(ref,NR_NO,0,ctempposinvalid,1,[]);
  1800. allocate_implicit_structs_for_st_with_base_ref(list,tabstractrecorddef(current_procinfo.procdef.owner.defowner).symtable,ref,staticvarsym);
  1801. end
  1802. else
  1803. inherited
  1804. end;
  1805. end;
  1806. procedure thlcgwasm.gen_entry_code(list: TAsmList);
  1807. begin
  1808. list.concat(Tai_force_line.Create);
  1809. end;
  1810. procedure thlcgwasm.gen_exit_code(list: TAsmList);
  1811. begin
  1812. { nothing }
  1813. end;
  1814. procedure thlcgwasm.a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; srcsize, dstsize: tdef; src, dst: tregister);
  1815. begin
  1816. internalerror(2012090201);
  1817. end;
  1818. procedure thlcgwasm.a_loadmm_loc_reg(list: TAsmList; fromsize, tosize: tdef; const loc: tlocation; const reg: tregister; shuffle: pmmshuffle);
  1819. begin
  1820. internalerror(2012090202);
  1821. end;
  1822. procedure thlcgwasm.a_loadmm_reg_reg(list: TAsmList; fromsize, tosize: tdef; reg1, reg2: tregister; shuffle: pmmshuffle);
  1823. begin
  1824. internalerror(2012060130);
  1825. end;
  1826. procedure thlcgwasm.a_loadmm_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; reg: tregister; shuffle: pmmshuffle);
  1827. begin
  1828. internalerror(2012060131);
  1829. end;
  1830. procedure thlcgwasm.a_loadmm_reg_ref(list: TAsmList; fromsize, tosize: tdef; reg: tregister; const ref: treference; shuffle: pmmshuffle);
  1831. begin
  1832. internalerror(2012060132);
  1833. end;
  1834. procedure thlcgwasm.a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size: tdef; src, dst: tregister; shuffle: pmmshuffle);
  1835. begin
  1836. internalerror(2012060133);
  1837. end;
  1838. procedure thlcgwasm.a_loadmm_intreg_reg(list: TAsmList; fromsize, tosize: tdef; intreg, mmreg: tregister; shuffle: pmmshuffle);
  1839. begin
  1840. internalerror(2012060134);
  1841. end;
  1842. procedure thlcgwasm.a_loadmm_reg_intreg(list: TAsmList; fromsize, tosize: tdef; mmreg, intreg: tregister; shuffle: pmmshuffle);
  1843. begin
  1844. internalerror(2012060135);
  1845. end;
  1846. procedure thlcgwasm.g_stackpointer_alloc(list: TAsmList; size: longint);
  1847. begin
  1848. internalerror(2012090203);
  1849. end;
  1850. procedure thlcgwasm.g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);
  1851. begin
  1852. internalerror(2012090204);
  1853. end;
  1854. procedure thlcgwasm.g_adjust_self_value(list: TAsmList; procdef: tprocdef; ioffset: aint);
  1855. begin
  1856. internalerror(2012090205);
  1857. end;
  1858. procedure thlcgwasm.g_local_unwind(list: TAsmList; l: TAsmLabel);
  1859. begin
  1860. internalerror(2012090206);
  1861. end;
  1862. procedure thlcgwasm.g_procdef(list: TAsmList; pd: tprocdef);
  1863. begin
  1864. list.Concat(tai_functype.create(pd.mangledname,tcpuprocdef(pd).create_functype));
  1865. end;
  1866. procedure thlcgwasm.a_load_stack_reg(list: TAsmList; size: tdef; reg: tregister);
  1867. var
  1868. opc: tasmop;
  1869. finishandval: tcgint;
  1870. begin
  1871. opc:=loadstoreopc(size,false,false,finishandval);
  1872. list.concat(taicpu.op_reg(opc,reg));
  1873. { avoid problems with getting the size of an open array etc }
  1874. if wasmAlwayInMem(size) then
  1875. size:=ptruinttype;
  1876. decstack(list,1);
  1877. end;
  1878. procedure thlcgwasm.a_load_stack_ref(list: TAsmList; size: tdef; const ref: treference; extra_slots: longint);
  1879. var
  1880. opc: tasmop;
  1881. finishandval: tcgint;
  1882. begin
  1883. { fake location that indicates the value has to remain on the stack }
  1884. if ref.base=NR_EVAL_STACK_BASE then
  1885. exit;
  1886. opc:=loadstoreopcref(size,false,ref,finishandval);
  1887. list.concat(taicpu.op_ref(opc,ref));
  1888. { avoid problems with getting the size of an open array etc }
  1889. if wasmAlwayInMem(size) then
  1890. size:=ptruinttype;
  1891. decstack(list,1+extra_slots);
  1892. end;
  1893. procedure thlcgwasm.a_load_reg_stack(list: TAsmList; size: tdef; reg: tregister);
  1894. var
  1895. opc: tasmop;
  1896. finishandval: tcgint;
  1897. begin
  1898. opc:=loadstoreopc(size,true,false,finishandval);
  1899. list.concat(taicpu.op_reg(opc,reg));
  1900. { avoid problems with getting the size of an open array etc }
  1901. if wasmAlwayInMem(size) then
  1902. size:=ptruinttype;
  1903. incstack(list,1);
  1904. if finishandval<>-1 then
  1905. a_op_const_stack(list,OP_AND,size,finishandval);
  1906. end;
  1907. procedure thlcgwasm.a_load_ref_stack(list: TAsmList; size: tdef; const ref: treference; extra_slots: longint);
  1908. var
  1909. opc: tasmop;
  1910. finishandval: tcgint;
  1911. begin
  1912. { fake location that indicates the value is already on the stack? }
  1913. if (ref.base=NR_EVAL_STACK_BASE) then
  1914. exit;
  1915. opc:=loadstoreopcref(size,true,ref,finishandval);
  1916. list.concat(taicpu.op_ref(opc,ref));
  1917. { avoid problems with getting the size of an open array etc }
  1918. if wasmAlwayInMem(size) then
  1919. size:=ptruinttype;
  1920. incstack(list,1-extra_slots);
  1921. if finishandval<>-1 then
  1922. a_op_const_stack(list,OP_AND,size,finishandval);
  1923. // there's no cast check in Wasm
  1924. //if ref.checkcast then
  1925. // gen_typecheck(list,a_checkcast,size);
  1926. end;
  1927. function thlcgwasm.loadstoreopcref(def: tdef; isload: boolean; const ref: treference; out finishandval: tcgint): tasmop;
  1928. const
  1929. {iisload} {issigned}
  1930. getputmem8 : array [boolean, boolean] of TAsmOp = ((a_i32_store8, a_i32_store8), (a_i32_load8_u, a_i32_load8_s));
  1931. getputmem16 : array [boolean, boolean] of TAsmOp = ((a_i32_store16, a_i32_store16), (a_i32_load16_u ,a_i32_load16_s));
  1932. getputmem32 : array [boolean, boolean] of TAsmOp = ((a_i32_store, a_i32_store), (a_i32_load, a_i32_load));
  1933. getputmem64 : array [boolean, boolean] of TAsmOp = ((a_i64_store, a_i64_store), (a_i64_load, a_i64_load));
  1934. getputmemf32 : array [boolean] of TAsmOp = (a_f32_store, a_f32_load);
  1935. getputmemf64 : array [boolean] of TAsmOp = (a_f64_store, a_f64_load);
  1936. begin
  1937. if (ref.base<>NR_LOCAL_STACK_POINTER_REG) or assigned(ref.symbol) then
  1938. begin
  1939. { -> either a global (static) field, or a regular field. If a regular
  1940. field, then ref.base contains the self pointer, otherwise
  1941. ref.base=NR_NO. In both cases, the symbol contains all other
  1942. information (combined field name and type descriptor) }
  1943. case def.size of
  1944. 1: result := getputmem8[isload, is_signed(def)];
  1945. 2: result := getputmem16[isload, is_signed(def)];
  1946. 4:
  1947. if is_single(def) then
  1948. result := getputmemf32[isload]
  1949. else
  1950. result := getputmem32[isload, is_signed(def)];
  1951. 8: if is_double(def) then
  1952. result := getputmemf64[isload]
  1953. else
  1954. result := getputmem64[isload, is_signed(def)];
  1955. else
  1956. Internalerror(2019091501);
  1957. end;
  1958. //result:=getputopc[isload,ref.base=NR_NO];
  1959. finishandval:=-1;
  1960. { erase sign extension for byte/smallint loads }
  1961. if (def2regtyp(def)=R_INTREGISTER) and
  1962. not is_signed(def) and
  1963. (def.typ=orddef) and
  1964. not is_widechar(def) then
  1965. case def.size of
  1966. 1: if (torddef(def).high>127) then
  1967. finishandval:=255;
  1968. 2: if (torddef(def).high>32767) then
  1969. finishandval:=65535;
  1970. end;
  1971. end
  1972. else
  1973. result:=loadstoreopc(def,isload,false,finishandval);
  1974. end;
  1975. function thlcgwasm.loadstoreopc(def: tdef; isload, isarray: boolean; out finishandval: tcgint): tasmop;
  1976. var
  1977. size: longint;
  1978. begin
  1979. finishandval:=-1;
  1980. if isload then result := a_get_local
  1981. else result := a_set_local;
  1982. {case def2regtyp(def) of
  1983. R_INTREGISTER:
  1984. begin
  1985. size:=def.size;
  1986. case size of
  1987. 1,2,3,4:
  1988. if isload then
  1989. result:=a_i32_load
  1990. else
  1991. result:=a_i32_store;
  1992. 8:
  1993. if isload then
  1994. result:=a_i64_load
  1995. else
  1996. result:=a_i64_store;
  1997. else
  1998. internalerror(2011032814);
  1999. end;
  2000. end;
  2001. R_ADDRESSREGISTER:
  2002. if isload then
  2003. result:=a_i32_load
  2004. else
  2005. result:=a_i32_store;
  2006. R_FPUREGISTER:
  2007. begin
  2008. case tfloatdef(def).floattype of
  2009. s32real:
  2010. if isload then
  2011. result:=a_f32_load
  2012. else
  2013. result:=a_f32_store;
  2014. s64real:
  2015. if isload then
  2016. result:=a_f32_load
  2017. else
  2018. result:=a_f32_store
  2019. else
  2020. internalerror(2010120504);
  2021. end
  2022. end
  2023. else
  2024. internalerror(2010120502);
  2025. end;}
  2026. end;
  2027. procedure thlcgwasm.resize_stack_int_val(list: TAsmList; fromsize, tosize: tdef; formemstore: boolean);
  2028. var
  2029. fromcgsize, tocgsize: tcgsize;
  2030. begin
  2031. { When storing to an array, field or global variable, make sure the
  2032. static type verification can determine that the stored value fits
  2033. within the boundaries of the declared type (to appease the Dalvik VM).
  2034. Local variables either get their type upgraded in the debug info,
  2035. or have no type information at all }
  2036. if formemstore and
  2037. (tosize.typ=orddef) then
  2038. if (torddef(tosize).ordtype in [u8bit,uchar]) then
  2039. tosize:=s8inttype
  2040. else if torddef(tosize).ordtype=u16bit then
  2041. tosize:=s16inttype;
  2042. fromcgsize:=def_cgsize(fromsize);
  2043. tocgsize:=def_cgsize(tosize);
  2044. if fromcgsize in [OS_S64,OS_64] then
  2045. begin
  2046. if not(tocgsize in [OS_S64,OS_64]) then
  2047. begin
  2048. { truncate }
  2049. list.concat(taicpu.op_none(a_i32_wrap_i64));
  2050. end;
  2051. end
  2052. else if tocgsize in [OS_S64,OS_64] then
  2053. begin
  2054. { extend }
  2055. case fromcgsize of
  2056. OS_8:
  2057. begin
  2058. a_op_const_stack(list,OP_AND,s32inttype,255);
  2059. list.concat(taicpu.op_none(a_i64_extend_u_i32));
  2060. end;
  2061. OS_S8:
  2062. list.concat(taicpu.op_none(a_i64_extend_s_8));
  2063. OS_16:
  2064. begin
  2065. a_op_const_stack(list,OP_AND,s32inttype,65535);
  2066. list.concat(taicpu.op_none(a_i64_extend_u_i32));
  2067. end;
  2068. OS_S16:
  2069. list.concat(taicpu.op_none(a_i64_extend_s_16));
  2070. OS_32:
  2071. list.concat(taicpu.op_none(a_i64_extend_u_i32));
  2072. OS_S32:
  2073. list.concat(taicpu.op_none(a_i64_extend_s_i32));
  2074. OS_64,OS_S64:
  2075. ;
  2076. else
  2077. internalerror(2021010301);
  2078. end;
  2079. end;
  2080. { Conversions between 32 and 64 bit types have been completely handled
  2081. above. We still may have to truncate or sign extend in case the
  2082. destination type is smaller that the source type, or has a different
  2083. sign. In case the destination is a widechar and the source is not, we
  2084. also have to insert a conversion to widechar.
  2085. }
  2086. case fromcgsize of
  2087. OS_8:
  2088. a_op_const_stack(list,OP_AND,s32inttype,255);
  2089. OS_S8:
  2090. list.concat(taicpu.op_none(a_i32_extend_s_8));
  2091. OS_16:
  2092. a_op_const_stack(list,OP_AND,s32inttype,65535);
  2093. OS_S16:
  2094. list.concat(taicpu.op_none(a_i32_extend_s_16));
  2095. OS_32,OS_S32,OS_64,OS_S64:
  2096. ;
  2097. else
  2098. internalerror(2021010302);
  2099. end;
  2100. end;
  2101. procedure thlcgwasm.maybe_resize_stack_para_val(list: TAsmList; retdef: tdef; callside: boolean);
  2102. var
  2103. convsize: tdef;
  2104. begin
  2105. if (retdef.typ=orddef) then
  2106. begin
  2107. if (torddef(retdef).ordtype in [u8bit,u16bit,uchar]) and
  2108. (torddef(retdef).high>=(1 shl (retdef.size*8-1))) then
  2109. begin
  2110. convsize:=nil;
  2111. if callside then
  2112. if torddef(retdef).ordtype in [u8bit,uchar] then
  2113. convsize:=s8inttype
  2114. else
  2115. convsize:=s16inttype
  2116. else if torddef(retdef).ordtype in [u8bit,uchar] then
  2117. convsize:=u8inttype
  2118. else
  2119. convsize:=u16inttype;
  2120. if assigned(convsize) then
  2121. resize_stack_int_val(list,s32inttype,convsize,false);
  2122. end;
  2123. end;
  2124. end;
  2125. procedure thlcgwasm.g_adjust_stack_after_call(list: TAsmList; pd: tabstractprocdef; paraheight: longint; forceresdef: tdef);
  2126. var
  2127. totalremovesize: longint;
  2128. realresdef: tdef;
  2129. begin
  2130. if not assigned(forceresdef) then
  2131. realresdef:=pd.returndef
  2132. else
  2133. realresdef:=forceresdef;
  2134. { a constructor doesn't actually return a value in the jvm }
  2135. if (tabstractprocdef(pd).proctypeoption=potype_constructor) then
  2136. totalremovesize:=paraheight
  2137. else
  2138. { even a byte takes up a full stackslot -> align size to multiple of 4 }
  2139. totalremovesize:=paraheight-(align(realresdef.size,4) shr 2);
  2140. { remove parameters from internal evaluation stack counter (in case of
  2141. e.g. no parameters and a result, it can also increase) }
  2142. if totalremovesize>0 then
  2143. decstack(list,totalremovesize)
  2144. else if totalremovesize<0 then
  2145. incstack(list,-totalremovesize);
  2146. end;
  2147. procedure thlcgwasm.allocate_implicit_struct_with_base_ref(list: TAsmList; vs: tabstractvarsym; ref: treference);
  2148. var
  2149. tmpref: treference;
  2150. begin
  2151. ref.symbol:=current_asmdata.RefAsmSymbol(vs.mangledname,AT_DATA);
  2152. tg.gethltemp(list,vs.vardef,vs.vardef.size,tt_persistent,tmpref);
  2153. { only copy the reference, not the actual data }
  2154. a_load_ref_ref(list,ptruinttype,ptruinttype,tmpref,ref);
  2155. { remains live since there's still a reference to the created
  2156. entity }
  2157. tg.ungettemp(list,tmpref);
  2158. end;
  2159. procedure thlcgwasm.allocate_enum_with_base_ref(list: TAsmList; vs: tabstractvarsym; const initref: treference; destbaseref: treference);
  2160. begin
  2161. destbaseref.symbol:=current_asmdata.RefAsmSymbol(vs.mangledname,AT_DATA);
  2162. { only copy the reference, not the actual data }
  2163. a_load_ref_ref(list,ptruinttype,ptruinttype,initref,destbaseref);
  2164. end;
  2165. function thlcgwasm.get_enum_init_val_ref(def: tdef; out ref: treference): boolean;
  2166. var
  2167. sym: tstaticvarsym;
  2168. begin
  2169. result:=false;
  2170. sym:=tstaticvarsym(tcpuenumdef(tenumdef(def).getbasedef).classdef.symtable.Find('__FPC_ZERO_INITIALIZER'));
  2171. { no enum with ordinal value 0 -> exit }
  2172. if not assigned(sym) then
  2173. exit;
  2174. reference_reset_symbol(ref,current_asmdata.RefAsmSymbol(sym.mangledname,AT_DATA),0,4,[]);
  2175. result:=true;
  2176. end;
  2177. procedure thlcgwasm.allocate_implicit_structs_for_st_with_base_ref(list: TAsmList; st: tsymtable; const ref: treference; allocvartyp: tsymtyp);
  2178. var
  2179. vs: tabstractvarsym;
  2180. def: tdef;
  2181. i: longint;
  2182. initref: treference;
  2183. begin
  2184. for i:=0 to st.symlist.count-1 do
  2185. begin
  2186. if (tsym(st.symlist[i]).typ<>allocvartyp) then
  2187. continue;
  2188. vs:=tabstractvarsym(st.symlist[i]);
  2189. if sp_static in vs.symoptions then
  2190. continue;
  2191. { vo_is_external and vo_has_local_copy means a staticvarsym that is
  2192. alias for a constsym, whose sole purpose is for allocating and
  2193. intialising the constant }
  2194. if [vo_is_external,vo_has_local_copy]*vs.varoptions=[vo_is_external] then
  2195. continue;
  2196. { threadvar innitializations are handled at the node tree level }
  2197. if vo_is_thread_var in vs.varoptions then
  2198. begin
  2199. { nothing }
  2200. end
  2201. else if wasmAlwayInMem(vs.vardef) then
  2202. allocate_implicit_struct_with_base_ref(list,vs,ref)
  2203. { enums are class instances in Java, while they are ordinals in
  2204. Pascal. When they are initialized with enum(0), such as in
  2205. constructors or global variables, initialize them with the
  2206. enum instance for 0 if it exists (if not, it remains nil since
  2207. there is no valid enum value in it) }
  2208. else if (vs.vardef.typ=enumdef) and
  2209. ((vs.typ<>fieldvarsym) or
  2210. (tdef(vs.owner.defowner).typ<>objectdef) or
  2211. (ts_jvm_enum_field_init in current_settings.targetswitches)) and
  2212. get_enum_init_val_ref(vs.vardef,initref) then
  2213. allocate_enum_with_base_ref(list,vs,initref,ref);
  2214. end;
  2215. { process symtables of routines part of this symtable (for local typed
  2216. constants) }
  2217. if allocvartyp=staticvarsym then
  2218. begin
  2219. for i:=0 to st.deflist.count-1 do
  2220. begin
  2221. def:=tdef(st.deflist[i]);
  2222. { the unit symtable also contains the methods of classes defined
  2223. in that unit -> skip them when processing the unit itself.
  2224. Localst is not assigned for the main program code.
  2225. Localst can be the same as st in case of unit init code. }
  2226. if (def.typ<>procdef) or
  2227. (def.owner<>st) or
  2228. not assigned(tprocdef(def).localst) or
  2229. (tprocdef(def).localst=st) then
  2230. continue;
  2231. allocate_implicit_structs_for_st_with_base_ref(list,tprocdef(def).localst,ref,allocvartyp);
  2232. end;
  2233. end;
  2234. end;
  2235. procedure thlcgwasm.gen_initialize_fields_code(list: TAsmList);
  2236. var
  2237. sym: tsym;
  2238. selfpara: tparavarsym;
  2239. selfreg: tregister;
  2240. ref: treference;
  2241. obj: tabstractrecorddef;
  2242. i: longint;
  2243. needinit: boolean;
  2244. begin
  2245. obj:=tabstractrecorddef(current_procinfo.procdef.owner.defowner);
  2246. { check whether there are any fields that need initialisation }
  2247. needinit:=false;
  2248. for i:=0 to obj.symtable.symlist.count-1 do
  2249. begin
  2250. sym:=tsym(obj.symtable.symlist[i]);
  2251. if (sym.typ=fieldvarsym) and
  2252. not(sp_static in sym.symoptions) and
  2253. (wasmAlwayInMem(tfieldvarsym(sym).vardef) or
  2254. ((tfieldvarsym(sym).vardef.typ=enumdef) and
  2255. get_enum_init_val_ref(tfieldvarsym(sym).vardef,ref))) then
  2256. begin
  2257. needinit:=true;
  2258. break;
  2259. end;
  2260. end;
  2261. if not needinit then
  2262. exit;
  2263. selfpara:=tparavarsym(current_procinfo.procdef.parast.find('self'));
  2264. if not assigned(selfpara) then
  2265. internalerror(2011033001);
  2266. selfreg:=getaddressregister(list,selfpara.vardef);
  2267. a_load_loc_reg(list,obj,obj,selfpara.localloc,selfreg);
  2268. cgutils.reference_reset_base(ref,selfreg,0,ctempposinvalid,1,[]);
  2269. allocate_implicit_structs_for_st_with_base_ref(list,obj.symtable,ref,fieldvarsym);
  2270. end;
  2271. procedure thlcgwasm.gen_typecheck(list: TAsmList; checkop: tasmop; checkdef: tdef);
  2272. begin
  2273. { replace special types with their equivalent class type }
  2274. if (checkdef.typ=pointerdef) and
  2275. wasmAlwayInMem(tpointerdef(checkdef).pointeddef) then
  2276. checkdef:=tpointerdef(checkdef).pointeddef;
  2277. if (checkdef=voidpointertype) or
  2278. (checkdef.typ=formaldef) then
  2279. checkdef:=ptruinttype
  2280. else if checkdef.typ=enumdef then
  2281. checkdef:=tcpuenumdef(checkdef).classdef
  2282. else if checkdef.typ=setdef then
  2283. begin
  2284. if tsetdef(checkdef).elementdef.typ=enumdef then
  2285. checkdef:=java_juenumset
  2286. else
  2287. checkdef:=java_jubitset;
  2288. end
  2289. else if is_wide_or_unicode_string(checkdef) then
  2290. checkdef:=java_jlstring
  2291. else if is_ansistring(checkdef) then
  2292. checkdef:=java_ansistring
  2293. else if is_shortstring(checkdef) then
  2294. checkdef:=java_shortstring;
  2295. if checkdef.typ in [objectdef,recorddef] then
  2296. list.concat(taicpu.op_sym(checkop,current_asmdata.RefAsmSymbol(tabstractrecorddef(checkdef).jvm_full_typename(true),AT_METADATA)))
  2297. else if checkdef.typ=classrefdef then
  2298. list.concat(taicpu.op_sym(checkop,current_asmdata.RefAsmSymbol('java/lang/Class',AT_METADATA)))
  2299. { todo: WASM
  2300. else
  2301. list.concat(taicpu.op_sym(checkop,current_asmdata.RefAsmSymbol(jvmencodetype(checkdef,false),AT_METADATA)));
  2302. }
  2303. end;
  2304. procedure thlcgwasm.resizestackfpuval(list: TAsmList; fromsize, tosize: tcgsize);
  2305. begin
  2306. if (fromsize=OS_F32) and
  2307. (tosize=OS_F64) then
  2308. begin
  2309. list.concat(taicpu.op_none(a_f64_promote_f32));
  2310. incstack(list,1);
  2311. end
  2312. else if (fromsize=OS_F64) and
  2313. (tosize=OS_F32) then
  2314. begin
  2315. list.concat(taicpu.op_none(a_f32_demote_f64));
  2316. decstack(list,1);
  2317. end;
  2318. end;
  2319. procedure thlcgwasm.maybepreparedivu32(list: TAsmList; var op: topcg; size: tdef; out isdivu32: boolean);
  2320. begin
  2321. if (op=OP_DIV) and
  2322. (def_cgsize(size)=OS_32) then
  2323. begin
  2324. { needs zero-extension to 64 bit, because the JVM only supports
  2325. signed divisions }
  2326. resize_stack_int_val(list,u32inttype,s64inttype,false);
  2327. op:=OP_IDIV;
  2328. isdivu32:=true;
  2329. end
  2330. else
  2331. isdivu32:=false;
  2332. end;
  2333. procedure create_hlcodegen_cpu;
  2334. begin
  2335. hlcg:=thlcgwasm.create;
  2336. create_codegen;
  2337. end;
  2338. initialization
  2339. chlcgobj:=thlcgwasm;
  2340. create_hlcodegen:=@create_hlcodegen_cpu;
  2341. end.