hlcgcpu.pas 105 KB

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