hlcgcpu.pas 99 KB

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