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