hlcgcpu.pas 100 KB

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