hlcgcpu.pas 105 KB

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