hlcgcpu.pas 100 KB

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