hlcgcpu.pas 99 KB

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