hlcgcpu.pas 113 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813
  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,aasmcpu,
  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. { checks whether the type needs special methodptr-like handling, when stored
  34. in a LOC_REGISTER location. This applies to the following types:
  35. - method pointers
  36. - 8-byte records
  37. - nested proc ptrs
  38. When stored in a LOC_REGISTER tlocation, these types use both register
  39. and registerhi with the following sizes:
  40. register - cgsize = int_cgsize(voidcodepointertype.size)
  41. registerhi - cgsize = int_cgsize(voidpointertype.size) or int_cgsize(parentfpvoidpointertype.size)
  42. (check d.size to determine which one of the two)
  43. }
  44. function is_methodptr_like_type(d:tdef): boolean;
  45. public
  46. fntypelookup : TWasmProcTypeLookup;
  47. constructor create;
  48. destructor Destroy; override;
  49. function RefStackPointerSym: TWasmGlobalAsmSymbol;
  50. procedure incstack(list : TAsmList;slots: longint);
  51. procedure decstack(list : TAsmList;slots: longint);
  52. class function def2regtyp(def: tdef): tregistertype; override;
  53. function getintregister(list:TAsmList;size:tdef):Tregister;override;
  54. function getregisterfordef(list: TAsmList;size:tdef):Tregister;override;
  55. procedure a_load_const_cgpara(list : TAsmList;tosize : tdef;a : tcgint;const cgpara : TCGPara);override;
  56. function a_call_name(list : TAsmList;pd : tprocdef;const s : TSymStr; const paras: array of pcgpara; forceresdef: tdef; weak: boolean): tcgpara;override;
  57. function a_call_reg(list: TAsmList; pd: tabstractprocdef; reg: tregister; const paras: array of pcgpara): tcgpara; override;
  58. { move instructions - a_load_FROM_TO }
  59. procedure a_load_const_reg(list : TAsmList;tosize : tdef;a : tcgint;register : tregister);override;
  60. procedure a_load_const_ref(list : TAsmList;tosize : tdef;a : tcgint;const ref : treference);override;
  61. procedure a_load_reg_ref(list : TAsmList;fromsize, tosize : tdef;register : tregister;const ref : treference);override;
  62. procedure a_load_reg_reg(list : TAsmList;fromsize, tosize : tdef;reg1,reg2 : tregister);override;
  63. procedure a_load_ref_reg(list : TAsmList;fromsize, tosize : tdef;const ref : treference;register : tregister);override;
  64. procedure a_load_ref_ref(list : TAsmList;fromsize, tosize : tdef;const sref : treference;const dref : treference);override;
  65. procedure a_load_loc_ref(list : TAsmList;fromsize, tosize: tdef; const loc: tlocation; const ref : treference);override;
  66. procedure a_loadaddr_ref_reg(list : TAsmList;fromsize, tosize : tdef;const ref : treference;r : tregister);override;
  67. procedure a_load_subsetref_regs_index(list: TAsmList; subsetsize: tdef; loadbitsize: byte; const sref: tsubsetreference; valuereg: tregister); override;
  68. procedure a_load_regconst_subsetref_intern(list : TAsmList; fromsize, subsetsize: tdef; fromreg: tregister; const sref: tsubsetreference; slopt: tsubsetloadopt); override;
  69. { basic arithmetic operations }
  70. procedure a_op_const_reg(list: TAsmList; Op: TOpCG; size: tdef; a: tcgint; reg: TRegister); override;
  71. procedure a_op_const_reg_reg(list: TAsmList; op: TOpCg; size: tdef; a: tcgint; src, dst: tregister); override;
  72. procedure a_op_const_ref(list: TAsmList; Op: TOpCG; size: tdef; a: tcgint; const ref: TReference); override;
  73. procedure a_op_ref_reg(list: TAsmList; Op: TOpCG; size: tdef; const ref: TReference; reg: TRegister); override;
  74. procedure a_op_reg_reg_reg(list: TAsmList; op: TOpCg; size: tdef; src1, src2, dst: tregister); override;
  75. procedure a_op_reg_reg(list: TAsmList; Op: TOpCG; size: tdef; reg1, reg2: TRegister); override;
  76. procedure a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tdef; a: tcgint; src, dst: tregister;setflags : boolean;var ovloc : tlocation); override;
  77. procedure a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tdef; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation); override;
  78. procedure a_cmp_const_ref_label(list: TAsmList; size: tdef; cmp_op: topcmp; a: tcgint; const ref: treference; l: tasmlabel); override;
  79. procedure a_cmp_const_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; a: tcgint; reg: tregister; l: tasmlabel); override;
  80. procedure a_cmp_ref_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; const ref: treference; reg: tregister; l: tasmlabel); override;
  81. procedure a_cmp_reg_ref_label(list: TAsmList; size: tdef; cmp_op: topcmp; reg: tregister; const ref: treference; l: tasmlabel); override;
  82. procedure a_cmp_reg_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; reg1, reg2: tregister; l: tasmlabel); override;
  83. procedure a_jmp_always(list : TAsmList;l: tasmlabel); override;
  84. procedure a_jmp_always_pascal_goto(list : TAsmList;l: tasmlabel); override;
  85. procedure a_loadfpu_ref_ref(list: TAsmList; fromsize, tosize: tdef; const ref1, ref2: treference); override;
  86. procedure a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; reg: tregister); override;
  87. procedure a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tdef; reg: tregister; const ref: treference); override;
  88. procedure a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tdef; reg1, reg2: tregister); override;
  89. procedure g_unreachable(list: TAsmList); override;
  90. procedure g_concatcopy(list : TAsmList;size: tdef; const source,dest : treference); override;
  91. procedure g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean); override;
  92. procedure g_proc_exit(list : TAsmList;parasize:longint;nostackframe:boolean); override;
  93. procedure g_rangecheck(list: TAsmList; const l:tlocation; fromdef,todef: tdef); override;
  94. procedure g_overflowcheck(list: TAsmList; const Loc: tlocation; def: tdef); override;
  95. procedure g_overflowCheck_loc(List:TAsmList;const Loc:TLocation;def:TDef;var ovloc : tlocation); override;
  96. procedure maybe_change_load_node_reg(list: TAsmList; var n: tnode; reload: boolean); override;
  97. procedure gen_entry_code(list: TAsmList); override;
  98. procedure gen_exit_code(list: TAsmList); override;
  99. procedure gen_stack_check_size_para(list: TAsmList); override;
  100. procedure gen_stack_check_call(list: TAsmList); override;
  101. { unimplemented/unnecessary routines }
  102. procedure a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; srcsize, dstsize: tdef; src, dst: tregister); override;
  103. procedure a_loadmm_loc_reg(list: TAsmList; fromsize, tosize: tdef; const loc: tlocation; const reg: tregister; shuffle: pmmshuffle); override;
  104. procedure a_loadmm_reg_reg(list: TAsmList; fromsize, tosize: tdef; reg1, reg2: tregister; shuffle: pmmshuffle); override;
  105. procedure a_loadmm_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; reg: tregister; shuffle: pmmshuffle); override;
  106. procedure a_loadmm_reg_ref(list: TAsmList; fromsize, tosize: tdef; reg: tregister; const ref: treference; shuffle: pmmshuffle); override;
  107. procedure a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size: tdef; src, dst: tregister; shuffle: pmmshuffle); override;
  108. procedure a_loadmm_intreg_reg(list: TAsmList; fromsize, tosize: tdef; intreg, mmreg: tregister; shuffle: pmmshuffle); override;
  109. procedure a_loadmm_reg_intreg(list: TAsmList; fromsize, tosize: tdef; mmreg, intreg: tregister; shuffle: pmmshuffle); override;
  110. procedure g_stackpointer_alloc(list: TAsmList; size: longint); override;
  111. procedure g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint); override;
  112. procedure g_adjust_self_value(list: TAsmList; procdef: tprocdef; ioffset: aint); override;
  113. procedure g_local_unwind(list: TAsmList; l: TAsmLabel); override;
  114. { Wasm-specific routines }
  115. procedure g_procdef(list:TAsmList;pd: tprocdef;is_forward: Boolean);
  116. procedure g_maybe_checkforexceptions(list:TasmList); override;
  117. procedure a_load_stack_reg(list : TAsmList;size: tdef;reg: tregister);
  118. { extra_slots are the slots that are used by the reference, and that
  119. will be removed by the store operation }
  120. procedure a_load_stack_ref(list : TAsmList;size: tdef;const ref: treference;extra_slots: longint);
  121. procedure a_load_reg_stack(list : TAsmList;size: tdef;reg: tregister);
  122. { extra_slots are the slots that are used by the reference, and that
  123. will be removed by the load operation }
  124. procedure a_load_ref_stack(list : TAsmList;size: tdef;const ref: treference;extra_slots: longint);
  125. procedure a_load_const_stack(list : TAsmList;size: tdef;a :tcgint; typ: TRegisterType);
  126. procedure a_load_subsetref_stack(list : TAsmList;size: tdef; const sref: tsubsetreference);
  127. procedure a_loadaddr_ref_stack(list : TAsmList;fromsize, tosize : tdef;const ref : treference);
  128. procedure a_load_stack_loc(list : TAsmList;size: tdef;const loc: tlocation);
  129. procedure a_load_loc_stack(list : TAsmList;size: tdef;const loc: tlocation);
  130. procedure a_loadfpu_const_stack(list : TAsmList;size: tdef;a :double);
  131. procedure a_op_stack(list : TAsmList;op: topcg; size: tdef);
  132. procedure a_op_const_stack(list : TAsmList;op: topcg; size: tdef;a : tcgint);
  133. procedure a_op_reg_stack(list : TAsmList;op: topcg; size: tdef;reg: tregister);
  134. procedure a_op_ref_stack(list : TAsmList;op: topcg; size: tdef;const ref: treference);
  135. procedure a_op_loc_stack(list : TAsmList;op: topcg; size: tdef;const loc: tlocation);
  136. procedure a_cmp_const_loc_stack(list: TAsmList; size: tdef;cmp_op: topcmp; a: tcgint; const loc: tlocation);
  137. procedure a_cmp_const_ref_stack(list: TAsmList; size: tdef; cmp_op: topcmp; a: tcgint; const ref: treference);
  138. procedure a_cmp_const_reg_stack(list: TAsmList; size: tdef; cmp_op: topcmp; a: tcgint; reg: tregister);
  139. procedure a_cmp_ref_reg_stack(list: TAsmList; size: tdef; cmp_op: topcmp; const ref: treference; reg: tregister);
  140. procedure a_cmp_reg_ref_stack(list: TAsmList; size: tdef; cmp_op: topcmp; reg: tregister; const ref: treference);
  141. procedure a_cmp_reg_reg_stack(list: TAsmList; size: tdef; cmp_op: topcmp; reg1, reg2: tregister);
  142. procedure a_cmp_subsetreg_reg_stack(list: TAsmList; fromsubsetsize, cmpsize: tdef; cmp_op: topcmp; const sreg: tsubsetregister; reg: tregister);
  143. procedure a_cmp_subsetref_reg_stack(list: TAsmList; fromsubsetsize, cmpsize: tdef; cmp_op: topcmp; const sref: tsubsetreference; reg: tregister);
  144. procedure a_cmp_loc_reg_stack(list : TAsmList;size : tdef;cmp_op : topcmp; const loc: tlocation; reg : tregister);
  145. procedure a_cmp_reg_loc_stack(list : TAsmList;size : tdef;cmp_op : topcmp; reg: tregister; const loc: tlocation);
  146. procedure a_cmp_ref_loc_stack(list: TAsmList; size: tdef;cmp_op: topcmp; const ref: treference; const loc: tlocation);
  147. procedure a_cmp_const_loc_br(list: TAsmList; size: tdef;cmp_op: topcmp; a: tcgint; const loc: tlocation; br: Integer);
  148. procedure a_cmp_const_ref_br(list: TAsmList; size: tdef; cmp_op: topcmp; a: tcgint; const ref: treference; br: Integer);
  149. procedure a_cmp_const_reg_br(list: TAsmList; size: tdef; cmp_op: topcmp; a: tcgint; reg: tregister; br: Integer);
  150. procedure a_cmp_ref_reg_br(list: TAsmList; size: tdef; cmp_op: topcmp; const ref: treference; reg: tregister; br: Integer);
  151. procedure a_cmp_reg_ref_br(list: TAsmList; size: tdef; cmp_op: topcmp; reg: tregister; const ref: treference; br: Integer);
  152. procedure a_cmp_reg_reg_br(list: TAsmList; size: tdef; cmp_op: topcmp; reg1, reg2: tregister; br: Integer);
  153. procedure a_cmp_subsetreg_reg_br(list: TAsmList; fromsubsetsize, cmpsize: tdef; cmp_op: topcmp; const sreg: tsubsetregister; reg: tregister; br: Integer);
  154. procedure a_cmp_subsetref_reg_br(list: TAsmList; fromsubsetsize, cmpsize: tdef; cmp_op: topcmp; const sref: tsubsetreference; reg: tregister; br: Integer);
  155. procedure a_cmp_loc_reg_br(list : TAsmList;size : tdef;cmp_op : topcmp; const loc: tlocation; reg : tregister; br: Integer);
  156. procedure a_cmp_reg_loc_br(list : TAsmList;size : tdef;cmp_op : topcmp; reg: tregister; const loc: tlocation; br: Integer);
  157. procedure a_cmp_ref_loc_br(list: TAsmList; size: tdef;cmp_op: topcmp; const ref: treference; const loc: tlocation; br: Integer);
  158. procedure g_reference_loc(list: TAsmList; def: tdef; const fromloc: tlocation; out toloc: tlocation); override;
  159. procedure a_cmp_stack_stack(list : TAsmlist; size: tdef; cmp_op: topcmp);
  160. { truncate/sign extend after performing operations on values < 32 bit
  161. that may have overflowed outside the range }
  162. procedure maybe_adjust_op_result(list: TAsmList; op: TOpCg; size: tdef);
  163. { performs sign/zero extension as required }
  164. procedure resize_stack_int_val(list: TAsmList;fromsize,tosize: tdef; formemstore: boolean);
  165. { 8/16 bit unsigned parameters and return values must be sign-extended on
  166. the producer side, because the JVM does not support unsigned variants;
  167. then they have to be zero-extended again on the consumer side }
  168. procedure maybe_resize_stack_para_val(list: TAsmList; retdef: tdef; callside: boolean);
  169. { adjust the stack height after a call based on the specified number of
  170. slots used for parameters and the provided resultdef }
  171. procedure g_adjust_stack_after_call(list: TAsmList; pd: tabstractprocdef);
  172. { because WebAssembly has no spec for any sort of debug info, and the
  173. only linker that we support (LLVM's wasm-ld) does not support creating
  174. map files in its stable version, and crashes when attempting to create
  175. a map file in its development version from git, we have no way to
  176. identify which procedure a crash occurred in. So, to identify the
  177. procedure, we call this procedure on proc entry, which generates a few
  178. useless loads of random numbers on the stack, that are immediately
  179. discarded, so they are essentially equivalent to a nop. This allows
  180. finding the procedure in the FPC output assembly, produced with -al by
  181. searching for these random numbers, as taken from the disassembly of the
  182. final binary. }
  183. procedure g_fingerprint(list: TAsmList);
  184. property maxevalstackheight: longint read fmaxevalstackheight;
  185. protected
  186. procedure gen_load_uninitialized_function_result(list: TAsmList; pd: tprocdef; resdef: tdef; const resloc: tcgpara); override;
  187. function g_call_system_proc_intern(list: TAsmList; pd: tprocdef; const paras: array of pcgpara; forceresdef: tdef): tcgpara; override;
  188. public
  189. { in case of an array, the array base address and index have to be
  190. put on the evaluation stack before the stored value; similarly, for
  191. fields the self pointer has to be loaded first. Also checks whether
  192. the reference is valid. If dup is true, the necessary values are stored
  193. twice. Returns how many stack slots have been consumed, disregarding
  194. the "dup". }
  195. function prepare_stack_for_ref(list: TAsmList; var ref: treference; dup: boolean): longint;
  196. procedure gen_load_cgpara_loc(list: TAsmList; vardef: tdef; const para: TCGPara; var destloc: tlocation; reusepara: boolean);override;
  197. protected
  198. { return the load/store opcode to load/store from/to ref; if the result
  199. has to be and'ed after a load to get the final value, that constant
  200. is returned in finishandval (otherwise that value is set to -1) }
  201. function loadstoreopcref(def: tdef; isload: boolean; const ref: treference; out finishandval: tcgint): tasmop;
  202. procedure resizestackfpuval(list: TAsmList; fromsize, tosize: tcgsize);
  203. end;
  204. implementation
  205. uses
  206. verbose,cutils,globals,fmodule,constexp,
  207. defutil,cpupi,
  208. aasmtai,
  209. symtable,symcpu,
  210. procinfo,cpuinfo,cgobj,cgcpu,tgobj,tgcpu,paramgr;
  211. const
  212. TOpCG2IAsmOp : array[topcg] of TAsmOp=(
  213. A_None, {OP_NONE}
  214. A_None, {OP_MOVE, replaced operation with direct load }
  215. a_i32_add, {OP_ADD, simple addition }
  216. a_i32_and, {OP_AND, simple logical and }
  217. a_i32_div_u, {OP_DIV, simple unsigned division }
  218. a_i32_div_s, {OP_IDIV, simple signed division }
  219. a_i32_mul, {OP_IMUL, simple signed multiply }
  220. a_i32_mul, {OP_MUL, simple unsigned multiply }
  221. A_None, {OP_NEG, simple negate } // neg = xor + 1
  222. A_None, {OP_NOT, simple logical not } // not = xor - 1
  223. a_i32_or, {OP_OR, simple logical or }
  224. a_i32_shr_s, {OP_SAR, arithmetic shift-right }
  225. a_i32_shl, {OP_SHL, logical shift left }
  226. a_i32_shr_u, {OP_SHR, logical shift right }
  227. a_i32_sub, {OP_SUB, simple subtraction }
  228. a_i32_xor, {OP_XOR, simple exclusive or }
  229. a_i32_rotl, {OP_ROL, rotate left }
  230. a_i32_rotr {OP_ROR rotate right }
  231. );
  232. TOpCG2LAsmOp : array[topcg] of TAsmOp=(
  233. A_None, {OP_NONE}
  234. a_i64_load, {OP_MOVE, replaced operation with direct load }
  235. a_i64_add, {OP_ADD, simple addition }
  236. a_i64_and, {OP_AND, simple logical and }
  237. a_i64_div_u, {OP_DIV, simple unsigned division }
  238. a_i64_div_s, {OP_IDIV, simple signed division }
  239. a_i64_mul, {OP_IMUL, simple signed multiply }
  240. a_i64_mul, {OP_MUL, simple unsigned multiply }
  241. A_None, {OP_NEG, simple negate } // neg = xor + 1
  242. A_None, {OP_NOT, simple logical not } // not = xor - 1
  243. a_i64_or, {OP_OR, simple logical or }
  244. a_i64_shr_s, {OP_SAR, arithmetic shift-right }
  245. a_i64_shl, {OP_SHL, logical shift left }
  246. a_i64_shr_u, {OP_SHR, logical shift right }
  247. a_i64_sub, {OP_SUB, simple subtraction }
  248. a_i64_xor, {OP_XOR, simple exclusive or }
  249. a_i64_rotl, {OP_ROL, rotate left }
  250. a_i64_rotr {OP_ROR rotate right }
  251. );
  252. function thlcgwasm.is_methodptr_like_type(d:tdef): boolean;
  253. var
  254. is_8byterecord, is_methodptr, is_nestedprocptr: Boolean;
  255. begin
  256. is_8byterecord:=(d.typ=recorddef) and (d.size=8);
  257. is_methodptr:=(d.typ=procvardef)
  258. and (po_methodpointer in tprocvardef(d).procoptions)
  259. and not(po_addressonly in tprocvardef(d).procoptions);
  260. is_nestedprocptr:=(d.typ=procvardef)
  261. and is_nested_pd(tprocvardef(d))
  262. and not(po_addressonly in tprocvardef(d).procoptions);
  263. result:=is_8byterecord or is_methodptr or is_nestedprocptr;
  264. end;
  265. function thlcgwasm.RefStackPointerSym: TWasmGlobalAsmSymbol;
  266. begin
  267. result:=TWasmGlobalAsmSymbol(current_asmdata.RefAsmSymbolByClass(TWasmGlobalAsmSymbol,STACK_POINTER_SYM,AT_WASM_GLOBAL));
  268. result.WasmGlobalType:=wbt_i32;
  269. end;
  270. constructor thlcgwasm.create;
  271. begin
  272. fevalstackheight:=0;
  273. fmaxevalstackheight:=0;
  274. fntypelookup:=TWasmProcTypeLookup.Create;
  275. end;
  276. destructor thlcgwasm.Destroy;
  277. begin
  278. fntypelookup.Free;
  279. inherited Destroy;
  280. end;
  281. procedure thlcgwasm.incstack(list: TAsmList; slots: longint);
  282. begin
  283. if (fevalstackheight<0) and
  284. not(cs_no_regalloc in current_settings.globalswitches) then
  285. {$ifdef DEBUG_WASMSTACK}
  286. list.concat(tai_comment.Create(strpnew('!!! stack underflow')));
  287. {$else DEBUG_WASMSTACK}
  288. internalerror(2010120501);
  289. {$endif DEBUG_WASMSTACK}
  290. if slots=0 then
  291. exit;
  292. inc(fevalstackheight,slots);
  293. if (fevalstackheight>fmaxevalstackheight) then
  294. fmaxevalstackheight:=fevalstackheight;
  295. if cs_asm_regalloc in current_settings.globalswitches then
  296. list.concat(tai_comment.Create(strpnew(' allocated '+tostr(slots)+', stack height = '+tostr(fevalstackheight))));
  297. end;
  298. procedure thlcgwasm.decstack(list: TAsmList;slots: longint);
  299. begin
  300. if slots=0 then
  301. exit;
  302. dec(fevalstackheight,slots);
  303. if (fevalstackheight<0) and
  304. not(cs_no_regalloc in current_settings.globalswitches) then
  305. {$ifdef DEBUG_WASMSTACK}
  306. list.concat(tai_comment.Create(strpnew('!!! stack underflow')));
  307. {$else DEBUG_WASMSTACK}
  308. internalerror(2010120501);
  309. {$endif DEBUG_WASMSTACK}
  310. if cs_asm_regalloc in current_settings.globalswitches then
  311. list.concat(tai_comment.Create(strpnew(' freed '+tostr(slots)+', stack height = '+tostr(fevalstackheight))));
  312. end;
  313. class function thlcgwasm.def2regtyp(def: tdef): tregistertype;
  314. begin
  315. if is_wasm_externref(def) then
  316. result:=R_EXTERNREFREGISTER
  317. else if is_wasm_funcref(def) then
  318. result:=R_FUNCREFREGISTER
  319. else if (def.typ=recorddef) and (def.size in [4,8]) and (trecorddef(def).contains_float_field) then
  320. result:=R_FPUREGISTER
  321. else
  322. result:=inherited;
  323. end;
  324. function thlcgwasm.getintregister(list:TAsmList;size:tdef):Tregister;
  325. begin
  326. if is_wasm_reference_type(size) then
  327. internalerror(2023060702)
  328. else
  329. result:=inherited;
  330. end;
  331. function thlcgwasm.getregisterfordef(list: TAsmList;size:tdef):Tregister;
  332. begin
  333. case def2regtyp(size) of
  334. R_EXTERNREFREGISTER:
  335. result:=TCgWasm(cg).getexternrefregister(list);
  336. R_FUNCREFREGISTER:
  337. result:=TCgWasm(cg).getfuncrefregister(list);
  338. else
  339. result:=inherited;
  340. end;
  341. end;
  342. procedure thlcgwasm.a_load_const_cgpara(list: TAsmList; tosize: tdef; a: tcgint; const cgpara: TCGPara);
  343. begin
  344. tosize:=get_para_push_size(tosize);
  345. if tosize=s8inttype then
  346. a:=shortint(a)
  347. else if tosize=s16inttype then
  348. a:=smallint(a);
  349. inherited a_load_const_cgpara(list, tosize, a, cgpara);
  350. end;
  351. function thlcgwasm.a_call_name(list: TAsmList; pd: tprocdef; const s: TSymStr; const paras: array of pcgpara; forceresdef: tdef; weak: boolean): tcgpara;
  352. begin
  353. list.concat(taicpu.op_sym_functype(a_call,current_asmdata.RefAsmSymbol(s,AT_FUNCTION),tcpuprocdef(pd).create_functype));
  354. result:=get_call_result_cgpara(pd,forceresdef);
  355. end;
  356. function thlcgwasm.a_call_reg(list: TAsmList; pd: tabstractprocdef; reg: tregister; const paras: array of pcgpara): tcgpara;
  357. begin
  358. a_load_reg_stack(list, ptrsinttype, reg);
  359. if pd.typ=procvardef then
  360. current_asmdata.CurrAsmList.Concat(taicpu.op_functype(a_call_indirect,tcpuprocvardef(pd).create_functype))
  361. else
  362. current_asmdata.CurrAsmList.Concat(taicpu.op_functype(a_call_indirect,tcpuprocdef(pd).create_functype));
  363. decstack(list,1);
  364. result:=hlcg.get_call_result_cgpara(pd, nil);
  365. end;
  366. procedure thlcgwasm.a_load_const_stack(list : TAsmList;size : tdef;a : tcgint; typ: TRegisterType);
  367. begin
  368. case typ of
  369. R_INTREGISTER,
  370. R_ADDRESSREGISTER:
  371. begin
  372. case def_cgsize(size) of
  373. OS_8,OS_16,OS_32,
  374. OS_S8,OS_S16,OS_S32:
  375. begin
  376. { convert cardinals to longints }
  377. list.concat(taicpu.op_const(a_i32_const, a));
  378. end;
  379. OS_64,OS_S64:
  380. begin
  381. list.concat(taicpu.op_const(a_i64_const, a));
  382. end;
  383. else
  384. internalerror(2010110702);
  385. end;
  386. end;
  387. R_EXTERNREFREGISTER:
  388. begin
  389. if a<>0 then
  390. internalerror(2023061101);
  391. list.Concat(taicpu.op_none(a_ref_null_externref));
  392. end;
  393. R_FUNCREFREGISTER:
  394. begin
  395. if a<>0 then
  396. internalerror(2023061102);
  397. list.Concat(taicpu.op_none(a_ref_null_funcref));
  398. end;
  399. else
  400. internalerror(2010110703);
  401. end;
  402. incstack(list,1);
  403. end;
  404. procedure thlcgwasm.a_loadaddr_ref_stack(list : TAsmList;fromsize, tosize : tdef;const ref : treference);
  405. var
  406. tmpref: treference;
  407. begin
  408. { you can't take the address of references, that are on the local stack }
  409. if (ref.base=NR_EVAL_STACK_BASE) or (ref.index=NR_EVAL_STACK_BASE) or
  410. (ref.base=NR_LOCAL_STACK_POINTER_REG) or (ref.index=NR_LOCAL_STACK_POINTER_REG) then
  411. internalerror(2021010101);
  412. tmpref:=ref;
  413. tmpref.base:=NR_NO;
  414. tmpref.index:=NR_NO;
  415. if tmpref.refaddr=addr_got_tls then
  416. begin
  417. tmpref.offset:=0;
  418. list.Concat(taicpu.op_ref(a_global_get, tmpref));
  419. incstack(list, 1);
  420. if ref.offset<>0 then
  421. begin
  422. list.Concat(taicpu.op_const(a_i32_const,ref.offset));
  423. incstack(list, 1);
  424. list.Concat(taicpu.op_none(a_i32_add));
  425. decstack(list, 1);
  426. end;
  427. end
  428. else
  429. begin
  430. list.Concat(taicpu.op_ref(a_i32_const, tmpref));
  431. incstack(list, 1);
  432. end;
  433. if ref.base<>NR_NO then
  434. begin
  435. list.Concat(taicpu.op_reg(a_local_get,ref.base));
  436. incstack(list, 1);
  437. list.Concat(taicpu.op_none(a_i32_add));
  438. decstack(list, 1);
  439. end;
  440. if ref.index<>NR_NO then
  441. begin
  442. list.Concat(taicpu.op_reg(a_local_get,ref.index));
  443. incstack(list, 1);
  444. if ref.scalefactor>1 then
  445. begin
  446. list.Concat(taicpu.op_const(a_i32_const,ref.scalefactor));
  447. incstack(list, 1);
  448. list.Concat(taicpu.op_none(a_i32_mul));
  449. decstack(list, 1);
  450. end;
  451. list.Concat(taicpu.op_none(a_i32_add));
  452. decstack(list, 1);
  453. end;
  454. end;
  455. procedure thlcgwasm.a_load_stack_loc(list: TAsmList; size: tdef; const loc: tlocation);
  456. var
  457. tmpref: treference;
  458. begin
  459. case loc.loc of
  460. LOC_REGISTER,LOC_CREGISTER,
  461. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  462. a_load_stack_reg(list,size,loc.register);
  463. LOC_REFERENCE:
  464. begin
  465. tmpref:=loc.reference;
  466. a_load_stack_ref(list,size,loc.reference,prepare_stack_for_ref(list,tmpref,false));
  467. end;
  468. else
  469. internalerror(2011020501);
  470. end;
  471. end;
  472. procedure thlcgwasm.a_load_loc_stack(list: TAsmList;size: tdef;const loc: tlocation);
  473. var
  474. tmpref: treference;
  475. extra_slots: LongInt;
  476. begin
  477. case loc.loc of
  478. LOC_REGISTER,LOC_CREGISTER,
  479. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  480. a_load_reg_stack(list,size,loc.register);
  481. LOC_REFERENCE,LOC_CREFERENCE:
  482. begin
  483. tmpref:=loc.reference;
  484. extra_slots:=prepare_stack_for_ref(list,tmpref,false);
  485. a_load_ref_stack(list,size,tmpref,extra_slots);
  486. end;
  487. LOC_CONSTANT:
  488. a_load_const_stack(list,size,loc.value,def2regtyp(size));
  489. LOC_SUBSETREF,LOC_CSUBSETREF:
  490. a_load_subsetref_stack(list,size,loc.sref);
  491. else
  492. internalerror(2011010401);
  493. end;
  494. end;
  495. procedure thlcgwasm.a_loadfpu_const_stack(list: TAsmList; size: tdef; a: double);
  496. begin
  497. case tfloatdef(size).floattype of
  498. s32real:
  499. begin
  500. list.concat(taicpu.op_single(a_f32_const, a));
  501. incstack(list,1);
  502. end;
  503. s64real:
  504. begin
  505. list.concat(taicpu.op_double(a_f64_const,a));
  506. incstack(list,1);
  507. end
  508. else
  509. internalerror(2011010501);
  510. end;
  511. end;
  512. procedure thlcgwasm.a_op_stack(list: TAsmList; op: topcg; size: tdef);
  513. begin
  514. case def_cgsize(size) of
  515. OS_8,OS_S8,
  516. OS_16,OS_S16,
  517. OS_32,OS_S32:
  518. begin
  519. { boolean not: =0? for boolean }
  520. if (op=OP_NOT) and is_pasbool(size) then
  521. list.concat(taicpu.op_none(a_i32_eqz))
  522. else if (op=OP_NOT) and is_cbool(size) then
  523. begin
  524. current_asmdata.CurrAsmList.Concat(taicpu.op_functype(a_if,TWasmFuncType.Create([],[wbt_i32])));
  525. decstack(current_asmdata.CurrAsmList,1);
  526. current_asmdata.CurrAsmList.Concat( taicpu.op_const(a_i32_const, 0) );
  527. incstack(current_asmdata.CurrAsmList,1);
  528. current_asmdata.CurrAsmList.Concat( taicpu.op_none(a_else) );
  529. decstack(current_asmdata.CurrAsmList,1);
  530. case def_cgsize(size) of
  531. OS_32,OS_S32:
  532. current_asmdata.CurrAsmList.Concat( taicpu.op_const(a_i32_const, -1) );
  533. OS_16,OS_S16:
  534. current_asmdata.CurrAsmList.Concat( taicpu.op_const(a_i32_const, 65535) );
  535. OS_8,OS_S8:
  536. current_asmdata.CurrAsmList.Concat( taicpu.op_const(a_i32_const, 255) );
  537. else
  538. internalerror(2021100102);
  539. end;
  540. incstack(current_asmdata.CurrAsmList,1);
  541. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_end_if));
  542. end
  543. else
  544. begin
  545. if op=OP_NOT then
  546. begin
  547. { not = xor -1 for integer }
  548. a_load_const_stack(list,s32inttype,high(cardinal),R_INTREGISTER);
  549. op:=OP_XOR;
  550. end
  551. else if op=OP_NEG then
  552. begin
  553. { neg = *(-1) }
  554. a_load_const_stack(list,s32inttype,-1,R_INTREGISTER);
  555. op:=OP_MUL;
  556. end;
  557. if TOpCG2IAsmOp[op]=A_None then
  558. internalerror(2010120532);
  559. list.concat(taicpu.op_none(TOpCG2IAsmOp[op]));
  560. decstack(list,1);
  561. end;
  562. maybe_adjust_op_result(list,op,size);
  563. end;
  564. OS_64,OS_S64:
  565. begin
  566. { boolean not: =0? for boolean }
  567. if (op=OP_NOT) and is_pasbool(size) then
  568. begin
  569. list.concat(taicpu.op_none(a_i64_eqz));
  570. list.concat(taicpu.op_none(a_i64_extend_i32_u));
  571. end
  572. else if (op=OP_NOT) and is_cbool(size) then
  573. begin
  574. list.concat(taicpu.op_none(a_i64_eqz));
  575. current_asmdata.CurrAsmList.Concat(taicpu.op_functype(a_if,TWasmFuncType.Create([],[wbt_i64])));
  576. decstack(current_asmdata.CurrAsmList,1);
  577. current_asmdata.CurrAsmList.Concat( taicpu.op_const(a_i64_const, -1) );
  578. incstack(current_asmdata.CurrAsmList,1);
  579. current_asmdata.CurrAsmList.Concat( taicpu.op_none(a_else) );
  580. decstack(current_asmdata.CurrAsmList,1);
  581. current_asmdata.CurrAsmList.Concat( taicpu.op_const(a_i64_const, 0) );
  582. incstack(current_asmdata.CurrAsmList,1);
  583. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_end_if));
  584. end
  585. else
  586. begin
  587. if op=OP_NOT then
  588. begin
  589. { not = xor -1 for integer }
  590. a_load_const_stack(list,s64inttype,-1,R_INTREGISTER);
  591. op:=OP_XOR;
  592. end
  593. else if op=OP_NEG then
  594. begin
  595. { neg = *(-1) }
  596. a_load_const_stack(list,s64inttype,-1,R_INTREGISTER);
  597. op:=OP_MUL;
  598. end;
  599. if TOpCG2LAsmOp[op]=A_None then
  600. internalerror(2010120533);
  601. list.concat(taicpu.op_none(TOpCG2LAsmOp[op]));
  602. decstack(list,1);
  603. end;
  604. end;
  605. else
  606. internalerror(2010120531);
  607. end;
  608. end;
  609. procedure thlcgwasm.a_op_const_stack(list: TAsmList;op: topcg;size: tdef;a: tcgint);
  610. begin
  611. case op of
  612. OP_NEG,OP_NOT:
  613. internalerror(2011010801);
  614. else
  615. a_load_const_stack(list,size,a,R_INTREGISTER);
  616. end;
  617. a_op_stack(list,op,size);
  618. end;
  619. procedure thlcgwasm.a_op_reg_stack(list: TAsmList; op: topcg; size: tdef; reg: tregister);
  620. begin
  621. a_load_reg_stack(list,size,reg);
  622. a_op_stack(list,op,size);
  623. end;
  624. procedure thlcgwasm.a_op_ref_stack(list: TAsmList; op: topcg; size: tdef; const ref: treference);
  625. var
  626. tmpref: treference;
  627. begin
  628. { ref must not be the stack top, because that may indicate an error
  629. (it means that we will perform an operation of the stack top onto
  630. itself, so that means the two values have been loaded manually prior
  631. to calling this routine, instead of letting this routine load one of
  632. them; if something like that is needed, call a_op_stack() directly) }
  633. if ref.base=NR_EVAL_STACK_BASE then
  634. internalerror(2010121102);
  635. tmpref:=ref;
  636. a_load_ref_stack(list,size,tmpref,prepare_stack_for_ref(list,tmpref,false));
  637. a_op_stack(list,op,size);
  638. end;
  639. procedure thlcgwasm.a_op_loc_stack(list: TAsmList; op: topcg; size: tdef; const loc: tlocation);
  640. begin
  641. case loc.loc of
  642. LOC_REGISTER,LOC_CREGISTER:
  643. a_op_reg_stack(list,op,size,loc.register);
  644. LOC_REFERENCE,LOC_CREFERENCE:
  645. a_op_ref_stack(list,op,size,loc.reference);
  646. LOC_CONSTANT:
  647. a_op_const_stack(list,op,size,loc.value);
  648. else
  649. internalerror(2011011415)
  650. end;
  651. end;
  652. procedure thlcgwasm.a_cmp_const_loc_stack(list: TAsmList; size: tdef; cmp_op: topcmp; a: tcgint; const loc: tlocation);
  653. var
  654. tmpreg: tregister;
  655. begin
  656. case loc.loc of
  657. LOC_REGISTER,LOC_CREGISTER:
  658. a_cmp_const_reg_stack(list,size,cmp_op,a,loc.register);
  659. LOC_REFERENCE,LOC_CREFERENCE:
  660. a_cmp_const_ref_stack(list,size,cmp_op,a,loc.reference);
  661. LOC_SUBSETREG, LOC_CSUBSETREG:
  662. begin
  663. tmpreg:=getintregister(list,size);
  664. a_load_subsetreg_reg(list,size,size,loc.sreg,tmpreg);
  665. a_cmp_const_reg_stack(list,size,cmp_op,a,tmpreg);
  666. end;
  667. LOC_SUBSETREF, LOC_CSUBSETREF:
  668. begin
  669. tmpreg:=getintregister(list,size);
  670. a_load_subsetref_reg(list,size,size,loc.sref,tmpreg);
  671. a_cmp_const_reg_stack(list,size,cmp_op,a,tmpreg);
  672. end;
  673. else
  674. internalerror(2010120430);
  675. end;
  676. end;
  677. procedure thlcgwasm.a_cmp_const_ref_stack(list: TAsmList; size: tdef; cmp_op: topcmp; a: tcgint; const ref: treference);
  678. var
  679. tmpref: treference;
  680. regtyp: TRegisterType;
  681. begin
  682. tmpref:=ref;
  683. if tmpref.base<>NR_EVAL_STACK_BASE then
  684. a_load_ref_stack(list,size,tmpref,prepare_stack_for_ref(list,tmpref,false));
  685. regtyp:=def2regtyp(size);
  686. case regtyp of
  687. R_EXTERNREFREGISTER,
  688. R_FUNCREFREGISTER:
  689. begin
  690. if a<>0 then
  691. internalerror(2023061103);
  692. if not (cmp_op in [OC_EQ,OC_NE]) then
  693. internalerror(2023061104);
  694. list.Concat(taicpu.op_none(a_ref_is_null));
  695. if cmp_op=OC_NE then
  696. begin
  697. a_load_const_stack(list,s32inttype,0,R_INTREGISTER);
  698. a_cmp_stack_stack(list,s32inttype,OC_EQ);
  699. end;
  700. end;
  701. else
  702. begin
  703. a_load_const_stack(list,size,a,regtyp);
  704. a_cmp_stack_stack(list,size,cmp_op);
  705. end;
  706. end;
  707. end;
  708. procedure thlcgwasm.a_cmp_const_reg_stack(list: TAsmList; size: tdef; cmp_op: topcmp; a: tcgint; reg: tregister);
  709. var
  710. regtyp: TRegisterType;
  711. begin
  712. a_load_reg_stack(list,size,reg);
  713. regtyp:=def2regtyp(size);
  714. case regtyp of
  715. R_EXTERNREFREGISTER,
  716. R_FUNCREFREGISTER:
  717. begin
  718. if a<>0 then
  719. internalerror(2023061105);
  720. if not (cmp_op in [OC_EQ,OC_NE]) then
  721. internalerror(2023061106);
  722. list.Concat(taicpu.op_none(a_ref_is_null));
  723. if cmp_op=OC_NE then
  724. begin
  725. a_load_const_stack(list,s32inttype,0,R_INTREGISTER);
  726. a_cmp_stack_stack(list,s32inttype,OC_EQ);
  727. end;
  728. end;
  729. else
  730. begin
  731. a_load_const_stack(list,size,a,regtyp);
  732. a_cmp_stack_stack(list,size,cmp_op);
  733. end;
  734. end;
  735. end;
  736. procedure thlcgwasm.a_cmp_ref_reg_stack(list: TAsmList; size: tdef; cmp_op: topcmp; const ref: treference; reg: tregister);
  737. var
  738. tmpref: treference;
  739. begin
  740. tmpref:=ref;
  741. a_load_reg_stack(list,size,reg);
  742. if tmpref.base<>NR_EVAL_STACK_BASE then
  743. a_load_ref_stack(list,size,tmpref,prepare_stack_for_ref(list,tmpref,false))
  744. else
  745. cmp_op:=swap_opcmp(cmp_op);
  746. a_cmp_stack_stack(list,size,cmp_op);
  747. end;
  748. procedure thlcgwasm.a_cmp_reg_ref_stack(list: TAsmList; size: tdef; cmp_op: topcmp; reg: tregister; const ref: treference);
  749. var
  750. tmpref: treference;
  751. begin
  752. tmpref:=ref;
  753. if tmpref.base<>NR_EVAL_STACK_BASE then
  754. a_load_ref_stack(list,size,ref,prepare_stack_for_ref(list,tmpref,false));
  755. a_load_reg_stack(list,size,reg);
  756. a_cmp_stack_stack(list,size,cmp_op);
  757. end;
  758. procedure thlcgwasm.a_cmp_reg_reg_stack(list: TAsmList; size: tdef; cmp_op: topcmp; reg1, reg2: tregister);
  759. begin
  760. a_load_reg_stack(list,size,reg2);
  761. a_load_reg_stack(list,size,reg1);
  762. a_cmp_stack_stack(list,size,cmp_op);
  763. end;
  764. procedure thlcgwasm.a_cmp_subsetreg_reg_stack(list: TAsmList; fromsubsetsize, cmpsize: tdef; cmp_op: topcmp; const sreg: tsubsetregister; reg: tregister);
  765. var
  766. tmpreg: tregister;
  767. begin
  768. tmpreg:=getintregister(list,cmpsize);
  769. a_load_subsetreg_reg(list,fromsubsetsize,cmpsize,sreg,tmpreg);
  770. a_cmp_reg_reg_stack(list,cmpsize,cmp_op,tmpreg,reg);
  771. end;
  772. procedure thlcgwasm.a_cmp_subsetref_reg_stack(list: TAsmList; fromsubsetsize, cmpsize: tdef; cmp_op: topcmp; const sref: tsubsetreference; reg: tregister);
  773. var
  774. tmpreg: tregister;
  775. begin
  776. tmpreg:=getintregister(list,cmpsize);
  777. a_load_subsetref_reg(list,fromsubsetsize,cmpsize,sref,tmpreg);
  778. a_cmp_reg_reg_stack(list,cmpsize,cmp_op,tmpreg,reg);
  779. end;
  780. procedure thlcgwasm.a_cmp_loc_reg_stack(list: TAsmList; size: tdef; cmp_op: topcmp; const loc: tlocation; reg: tregister);
  781. begin
  782. case loc.loc of
  783. LOC_REGISTER,
  784. LOC_CREGISTER:
  785. a_cmp_reg_reg_stack(list,size,cmp_op,loc.register,reg);
  786. LOC_REFERENCE,
  787. LOC_CREFERENCE :
  788. a_cmp_ref_reg_stack(list,size,cmp_op,loc.reference,reg);
  789. LOC_CONSTANT:
  790. a_cmp_const_reg_stack(list,size,cmp_op,loc.value,reg);
  791. LOC_SUBSETREG,
  792. LOC_CSUBSETREG:
  793. a_cmp_subsetreg_reg_stack(list,size,size,cmp_op,loc.sreg,reg);
  794. LOC_SUBSETREF,
  795. LOC_CSUBSETREF:
  796. a_cmp_subsetref_reg_stack(list,size,size,cmp_op,loc.sref,reg);
  797. else
  798. internalerror(2010120431);
  799. end;
  800. end;
  801. procedure thlcgwasm.a_cmp_reg_loc_stack(list: TAsmList; size: tdef; cmp_op: topcmp; reg: tregister; const loc: tlocation);
  802. begin
  803. a_cmp_loc_reg_stack(list,size,swap_opcmp(cmp_op),loc,reg);
  804. end;
  805. procedure thlcgwasm.a_cmp_ref_loc_stack(list: TAsmList; size: tdef; cmp_op: topcmp; const ref: treference; const loc: tlocation);
  806. var
  807. tmpreg: tregister;
  808. begin
  809. case loc.loc of
  810. LOC_REGISTER,LOC_CREGISTER:
  811. a_cmp_ref_reg_stack(list,size,cmp_op,ref,loc.register);
  812. LOC_REFERENCE,LOC_CREFERENCE:
  813. begin
  814. tmpreg:=getintregister(list,size);
  815. a_load_ref_reg(list,size,size,loc.reference,tmpreg);
  816. a_cmp_ref_reg_stack(list,size,cmp_op,ref,tmpreg);
  817. end;
  818. LOC_CONSTANT:
  819. begin
  820. a_cmp_const_ref_stack(list,size,swap_opcmp(cmp_op),loc.value,ref);
  821. end;
  822. LOC_SUBSETREG, LOC_CSUBSETREG:
  823. begin
  824. tmpreg:=getintregister(list,size);
  825. a_load_ref_reg(list,size,size,loc.reference,tmpreg);
  826. a_cmp_subsetreg_reg_stack(list,size,size,swap_opcmp(cmp_op),loc.sreg,tmpreg);
  827. end;
  828. LOC_SUBSETREF, LOC_CSUBSETREF:
  829. begin
  830. tmpreg:=getintregister(list,size);
  831. a_load_ref_reg(list,size,size,loc.reference,tmpreg);
  832. a_cmp_subsetref_reg_stack(list,size,size,swap_opcmp(cmp_op),loc.sref,tmpreg);
  833. end;
  834. else
  835. internalerror(2010120432);
  836. end;
  837. end;
  838. procedure thlcgwasm.a_cmp_const_loc_br(list: TAsmList; size: tdef;cmp_op: topcmp; a: tcgint; const loc: tlocation; br: Integer);
  839. begin
  840. a_cmp_const_loc_stack(list,size,cmp_op,a,loc);
  841. current_asmdata.CurrAsmList.concat(taicpu.op_const(a_br_if,br));
  842. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  843. end;
  844. procedure thlcgwasm.a_cmp_const_ref_br(list: TAsmList; size: tdef; cmp_op: topcmp; a: tcgint; const ref: treference; br: Integer);
  845. begin
  846. a_cmp_const_ref_stack(list,size,cmp_op,a,ref);
  847. current_asmdata.CurrAsmList.concat(taicpu.op_const(a_br_if,br));
  848. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  849. end;
  850. procedure thlcgwasm.a_cmp_const_reg_br(list: TAsmList; size: tdef; cmp_op: topcmp; a: tcgint; reg: tregister; br: Integer);
  851. begin
  852. a_cmp_const_reg_stack(list,size,cmp_op,a,reg);
  853. current_asmdata.CurrAsmList.concat(taicpu.op_const(a_br_if,br));
  854. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  855. end;
  856. procedure thlcgwasm.a_cmp_ref_reg_br(list: TAsmList; size: tdef; cmp_op: topcmp; const ref: treference; reg: tregister; br: Integer);
  857. begin
  858. a_cmp_ref_reg_stack(list,size,cmp_op,ref,reg);
  859. current_asmdata.CurrAsmList.concat(taicpu.op_const(a_br_if,br));
  860. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  861. end;
  862. procedure thlcgwasm.a_cmp_reg_ref_br(list: TAsmList; size: tdef; cmp_op: topcmp; reg: tregister; const ref: treference; br: Integer);
  863. begin
  864. a_cmp_reg_ref_stack(list,size,cmp_op,reg,ref);
  865. current_asmdata.CurrAsmList.concat(taicpu.op_const(a_br_if,br));
  866. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  867. end;
  868. procedure thlcgwasm.a_cmp_reg_reg_br(list: TAsmList; size: tdef; cmp_op: topcmp; reg1, reg2: tregister; br: Integer);
  869. begin
  870. a_cmp_reg_reg_stack(list,size,cmp_op,reg1,reg2);
  871. current_asmdata.CurrAsmList.concat(taicpu.op_const(a_br_if,br));
  872. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  873. end;
  874. procedure thlcgwasm.a_cmp_subsetreg_reg_br(list: TAsmList; fromsubsetsize, cmpsize: tdef; cmp_op: topcmp; const sreg: tsubsetregister; reg: tregister; br: Integer);
  875. begin
  876. a_cmp_subsetreg_reg_stack(list,fromsubsetsize,cmpsize,cmp_op,sreg,reg);
  877. current_asmdata.CurrAsmList.concat(taicpu.op_const(a_br_if,br));
  878. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  879. end;
  880. procedure thlcgwasm.a_cmp_subsetref_reg_br(list: TAsmList; fromsubsetsize, cmpsize: tdef; cmp_op: topcmp; const sref: tsubsetreference; reg: tregister; br: Integer);
  881. begin
  882. a_cmp_subsetref_reg_stack(list,fromsubsetsize,cmpsize,cmp_op,sref,reg);
  883. current_asmdata.CurrAsmList.concat(taicpu.op_const(a_br_if,br));
  884. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  885. end;
  886. procedure thlcgwasm.a_cmp_loc_reg_br(list : TAsmList;size : tdef;cmp_op : topcmp; const loc: tlocation; reg : tregister; br: Integer);
  887. begin
  888. a_cmp_loc_reg_stack(list,size,cmp_op,loc,reg);
  889. current_asmdata.CurrAsmList.concat(taicpu.op_const(a_br_if,br));
  890. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  891. end;
  892. procedure thlcgwasm.a_cmp_reg_loc_br(list : TAsmList;size : tdef;cmp_op : topcmp; reg: tregister; const loc: tlocation; br: Integer);
  893. begin
  894. a_cmp_reg_loc_stack(list,size,cmp_op,reg,loc);
  895. current_asmdata.CurrAsmList.concat(taicpu.op_const(a_br_if,br));
  896. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  897. end;
  898. procedure thlcgwasm.a_cmp_ref_loc_br(list: TAsmList; size: tdef;cmp_op: topcmp; const ref: treference; const loc: tlocation; br: Integer);
  899. begin
  900. a_cmp_ref_loc_stack(list,size,cmp_op,ref,loc);
  901. current_asmdata.CurrAsmList.concat(taicpu.op_const(a_br_if,br));
  902. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  903. end;
  904. procedure thlcgwasm.g_reference_loc(list: TAsmList; def: tdef; const fromloc: tlocation; out toloc: tlocation);
  905. begin
  906. case fromloc.loc of
  907. LOC_CREFERENCE,
  908. LOC_REFERENCE:
  909. begin
  910. toloc:=fromloc;
  911. if (fromloc.reference.base<>NR_NO) and
  912. (fromloc.reference.base<>current_procinfo.framepointer) and
  913. (fromloc.reference.base<>NR_STACK_POINTER_REG) then
  914. g_allocload_reg_reg(list,voidpointertype,fromloc.reference.base,toloc.reference.base,R_ADDRESSREGISTER);
  915. end;
  916. else
  917. inherited;
  918. end;
  919. end;
  920. procedure thlcgwasm.a_cmp_stack_stack(list: TAsmlist; size: tdef; cmp_op: topcmp);
  921. const
  922. opcmp32: array[topcmp] of tasmop = (
  923. A_None, { OC_NONE, }
  924. a_i32_eq, { OC_EQ, equality comparison }
  925. a_i32_gt_s, { OC_GT, greater than (signed) }
  926. a_i32_lt_s, { OC_LT, less than (signed) }
  927. a_i32_ge_s, { OC_GTE, greater or equal than (signed) }
  928. a_i32_le_s, { OC_LTE, less or equal than (signed) }
  929. a_i32_ne, { OC_NE, not equal }
  930. a_i32_le_u, { OC_BE, less or equal than (unsigned) }
  931. a_i32_lt_u, { OC_B, less than (unsigned) }
  932. a_i32_ge_u, { OC_AE, greater or equal than (unsigned) }
  933. a_i32_gt_u { OC_A greater than (unsigned) }
  934. );
  935. const
  936. opcmp64: array[TOpCmp] of TAsmOp = (A_None,
  937. a_i64_eq, // OC_EQ
  938. a_i64_gt_s, a_i64_lt_s, // OC_GT, OC_LT
  939. a_i64_ge_s, a_i64_le_s, // OC_GTE, OC_LTE
  940. a_i64_ne, // OC_NE
  941. a_i64_le_u, a_i64_lt_u, // OC_BE, OC_B
  942. a_i64_ge_u, a_i64_gt_u // OC_AE, OC_A
  943. );
  944. var
  945. cgsize: tcgsize;
  946. begin
  947. case def2regtyp(size) of
  948. R_INTREGISTER,
  949. R_ADDRESSREGISTER:
  950. begin
  951. cgsize:=def_cgsize(size);
  952. case cgsize of
  953. OS_S8,OS_8,
  954. OS_16,OS_S16,
  955. OS_S32,OS_32:
  956. begin
  957. list.concat(taicpu.op_none(opcmp32[cmp_op]));
  958. decstack(list,1);
  959. end;
  960. OS_64,OS_S64:
  961. begin
  962. list.concat(taicpu.op_none(opcmp64[cmp_op]));
  963. decstack(list,1);
  964. end;
  965. else
  966. internalerror(2010120538);
  967. end;
  968. end;
  969. else
  970. internalerror(2010120538);
  971. end;
  972. end;
  973. procedure thlcgwasm.maybe_adjust_op_result(list: TAsmList; op: TOpCg; size: tdef);
  974. const
  975. overflowops = [OP_MUL,OP_SHL,OP_ADD,OP_SUB,OP_NOT,OP_NEG];
  976. begin
  977. if (op in overflowops) and
  978. (def_cgsize(size) in [OS_8,OS_S8,OS_16,OS_S16]) then
  979. resize_stack_int_val(list,s32inttype,size,false);
  980. end;
  981. procedure thlcgwasm.gen_load_uninitialized_function_result(list: TAsmList; pd: tprocdef; resdef: tdef; const resloc: tcgpara);
  982. begin
  983. { nothing to do for ret_in_param results }
  984. if paramanager.ret_in_param(pd.returndef,pd) then
  985. exit;
  986. { constructors don't return anything in Java }
  987. if pd.proctypeoption=potype_constructor then
  988. exit;
  989. { must return a value of the correct type on the evaluation stack }
  990. case def2regtyp(resdef) of
  991. R_INTREGISTER,
  992. R_ADDRESSREGISTER:
  993. a_load_const_cgpara(list,resdef,0,resloc);
  994. R_FPUREGISTER:
  995. case tfloatdef(resdef).floattype of
  996. s32real:
  997. begin
  998. list.concat(taicpu.op_single(a_f32_const, 0));
  999. incstack(list,1);
  1000. end;
  1001. s64real:
  1002. begin
  1003. list.concat(taicpu.op_double(a_f64_const, 0));
  1004. incstack(list,1);
  1005. end;
  1006. else
  1007. internalerror(2011010302);
  1008. end;
  1009. R_FUNCREFREGISTER:
  1010. begin
  1011. list.concat(taicpu.op_none(a_ref_null_funcref));
  1012. incstack(list,1);
  1013. end;
  1014. R_EXTERNREFREGISTER:
  1015. begin
  1016. list.concat(taicpu.op_none(a_ref_null_externref));
  1017. incstack(list,1);
  1018. end;
  1019. else
  1020. internalerror(2011010301);
  1021. end;
  1022. end;
  1023. function thlcgwasm.g_call_system_proc_intern(list: TAsmList; pd: tprocdef; const paras: array of pcgpara; forceresdef: tdef): tcgpara;
  1024. begin
  1025. result:=inherited;
  1026. pd.init_paraloc_info(callerside);
  1027. g_adjust_stack_after_call(list,pd);
  1028. end;
  1029. function thlcgwasm.prepare_stack_for_ref(list: TAsmList; var ref: treference; dup: boolean): longint;
  1030. var
  1031. tmpref: treference;
  1032. begin
  1033. result:=0;
  1034. { fake location that indicates the value is already on the stack? }
  1035. if (ref.base=NR_EVAL_STACK_BASE) or (ref.base=NR_LOCAL_STACK_POINTER_REG) then
  1036. exit;
  1037. if (ref.base=NR_NO) and (ref.index<>NR_NO) and (ref.scalefactor<=1) then
  1038. begin
  1039. ref.base:=ref.index;
  1040. ref.index:=NR_NO;
  1041. end;
  1042. if assigned(ref.symbol) and (ref.symbol.typ=AT_WASM_GLOBAL) then
  1043. begin
  1044. if ref.base<>NR_NO then
  1045. internalerror(2022072601);
  1046. if ref.index<>NR_NO then
  1047. internalerror(2022072602);
  1048. if ref.offset<>0 then
  1049. internalerror(2022072603);
  1050. end
  1051. else if ref.refaddr=addr_got_tls then
  1052. begin
  1053. if not assigned(ref.symbol) then
  1054. internalerror(2022071405);
  1055. if ref.base<>NR_NO then
  1056. internalerror(2022071406);
  1057. if ref.index<>NR_NO then
  1058. internalerror(2022071407);
  1059. tmpref:=ref;
  1060. tmpref.offset:=0;
  1061. list.Concat(taicpu.op_ref(a_global_get,tmpref));
  1062. incstack(list,1);
  1063. if dup then
  1064. begin
  1065. list.Concat(taicpu.op_ref(a_global_get,tmpref));
  1066. incstack(list,1);
  1067. end;
  1068. result:=1;
  1069. end
  1070. else if assigned(ref.symbol) and (ref.base=NR_NO) and (ref.index=NR_NO) then
  1071. begin
  1072. list.Concat(taicpu.op_const(a_i32_const,ref.offset));
  1073. incstack(list,1);
  1074. if dup then
  1075. begin
  1076. list.Concat(taicpu.op_const(a_i32_const,ref.offset));
  1077. incstack(list,1);
  1078. end;
  1079. ref.offset:=0;
  1080. result:=1;
  1081. end
  1082. else if ref.index <> NR_NO then // array access
  1083. begin
  1084. // it's just faster to sum two of those together
  1085. list.Concat(taicpu.op_reg(a_local_get, ref.base));
  1086. incstack(list,1);
  1087. list.Concat(taicpu.op_reg(a_local_get, ref.index));
  1088. incstack(list,1);
  1089. list.Concat(taicpu.op_none(a_i32_add));
  1090. decstack(list,1);
  1091. if assigned(ref.symbol) then
  1092. begin
  1093. list.Concat(taicpu.op_sym(a_i32_const,ref.symbol));
  1094. incstack(list,1);
  1095. list.Concat(taicpu.op_none(a_i32_add));
  1096. decstack(list,1);
  1097. end;
  1098. if ref.offset<0 then
  1099. begin
  1100. list.Concat(taicpu.op_const(a_i32_const,-ref.offset));
  1101. incstack(list,1);
  1102. list.Concat(taicpu.op_none(a_i32_sub));
  1103. decstack(list,1);
  1104. end
  1105. else if ref.offset>0 then
  1106. begin
  1107. list.Concat(taicpu.op_const(a_i32_const,ref.offset));
  1108. incstack(list,1);
  1109. list.Concat(taicpu.op_none(a_i32_add));
  1110. decstack(list,1);
  1111. end;
  1112. if dup then
  1113. begin
  1114. list.Concat(taicpu.op_reg(a_local_get, ref.base));
  1115. incstack(list,1);
  1116. list.Concat(taicpu.op_reg(a_local_get, ref.index));
  1117. incstack(list,1);
  1118. list.Concat(taicpu.op_none(a_i32_add));
  1119. decstack(list,1);
  1120. if assigned(ref.symbol) then
  1121. begin
  1122. list.Concat(taicpu.op_sym(a_i32_const,ref.symbol));
  1123. incstack(list,1);
  1124. list.Concat(taicpu.op_none(a_i32_add));
  1125. decstack(list,1);
  1126. end;
  1127. if ref.offset<0 then
  1128. begin
  1129. list.Concat(taicpu.op_const(a_i32_const,-ref.offset));
  1130. incstack(list,1);
  1131. list.Concat(taicpu.op_none(a_i32_sub));
  1132. decstack(list,1);
  1133. end
  1134. else if ref.offset>0 then
  1135. begin
  1136. list.Concat(taicpu.op_const(a_i32_const,ref.offset));
  1137. incstack(list,1);
  1138. list.Concat(taicpu.op_none(a_i32_add));
  1139. decstack(list,1);
  1140. end;
  1141. end;
  1142. ref.base:=NR_NO;
  1143. ref.index:=NR_NO;
  1144. ref.offset:=0;
  1145. ref.symbol:=nil;
  1146. result:=1;
  1147. end
  1148. else if (ref.base<>NR_NO) then
  1149. begin
  1150. if (ref.base<>NR_STACK_POINTER_REG) then
  1151. begin
  1152. { regular field -> load self on the stack }
  1153. a_load_reg_stack(list,voidpointertype,ref.base);
  1154. if assigned(ref.symbol) then
  1155. begin
  1156. list.Concat(taicpu.op_sym(a_i32_const,ref.symbol));
  1157. incstack(list,1);
  1158. list.Concat(taicpu.op_none(a_i32_add));
  1159. decstack(list,1);
  1160. end;
  1161. if ref.offset<0 then
  1162. begin
  1163. list.Concat(taicpu.op_const(a_i32_const,-ref.offset));
  1164. incstack(list,1);
  1165. list.Concat(taicpu.op_none(a_i32_sub));
  1166. decstack(list,1);
  1167. end
  1168. else if ref.offset>0 then
  1169. begin
  1170. list.Concat(taicpu.op_const(a_i32_const,ref.offset));
  1171. incstack(list,1);
  1172. list.Concat(taicpu.op_none(a_i32_add));
  1173. decstack(list,1);
  1174. end;
  1175. if dup then
  1176. begin
  1177. a_load_reg_stack(list,voidpointertype,ref.base);
  1178. if assigned(ref.symbol) then
  1179. begin
  1180. list.Concat(taicpu.op_sym(a_i32_const,ref.symbol));
  1181. incstack(list,1);
  1182. list.Concat(taicpu.op_none(a_i32_add));
  1183. decstack(list,1);
  1184. end;
  1185. if ref.offset<0 then
  1186. begin
  1187. list.Concat(taicpu.op_const(a_i32_const,-ref.offset));
  1188. incstack(list,1);
  1189. list.Concat(taicpu.op_none(a_i32_sub));
  1190. decstack(list,1);
  1191. end
  1192. else if ref.offset>0 then
  1193. begin
  1194. list.Concat(taicpu.op_const(a_i32_const,ref.offset));
  1195. incstack(list,1);
  1196. list.Concat(taicpu.op_none(a_i32_add));
  1197. decstack(list,1);
  1198. end;
  1199. end;
  1200. ref.offset:=0;
  1201. ref.symbol:=nil;
  1202. ref.base:=NR_NO;
  1203. result:=1;
  1204. end
  1205. else // if (ref.base = NR_FRAME_POINTER_REG) then
  1206. begin
  1207. internalerror(2021012202);
  1208. //list.Concat(taicpu.op_sym(a_local_get, current_asmdata.RefAsmSymbol(FRAME_POINTER_SYM,AT_ADDR) ));
  1209. //incstack(list,1);
  1210. end;
  1211. end
  1212. else
  1213. begin
  1214. { static field -> nothing to do here, except for validity check }
  1215. {if not assigned(ref.symbol) or
  1216. (ref.offset<>0) then
  1217. begin
  1218. internalerror(2010120525);
  1219. end;}
  1220. end;
  1221. end;
  1222. procedure thlcgwasm.gen_load_cgpara_loc(list: TAsmList; vardef: tdef; const para: TCGPara; var destloc: tlocation; reusepara: boolean);
  1223. begin
  1224. { support loading a function result (from the evaluation stack), to a register }
  1225. if assigned(para.location) and (not assigned(para.location^.next)) and
  1226. (para.location^.loc in [LOC_REFERENCE,LOC_CREFERENCE]) and
  1227. (para.location^.reference.index=NR_EVAL_STACK_BASE) and
  1228. (para.location^.reference.offset=0) and
  1229. (def_cgsize(para.location^.Def)=destloc.size) and
  1230. (destloc.loc=LOC_REGISTER) then
  1231. a_load_stack_loc(list,para.location^.Def,destloc)
  1232. else
  1233. inherited;
  1234. end;
  1235. procedure thlcgwasm.a_load_const_reg(list: TAsmList; tosize: tdef; a: tcgint; register: tregister);
  1236. begin
  1237. a_load_const_stack(list,tosize,a,def2regtyp(tosize));
  1238. a_load_stack_reg(list,tosize,register);
  1239. end;
  1240. procedure thlcgwasm.a_load_const_ref(list: TAsmList; tosize: tdef; a: tcgint; const ref: treference);
  1241. var
  1242. extra_slots: longint;
  1243. tmpref: treference;
  1244. begin
  1245. tmpref:=ref;
  1246. extra_slots:=prepare_stack_for_ref(list,tmpref,false);
  1247. a_load_const_stack(list,tosize,a,def2regtyp(tosize));
  1248. a_load_stack_ref(list,tosize,tmpref,extra_slots);
  1249. end;
  1250. procedure thlcgwasm.a_load_reg_ref(list: TAsmList; fromsize, tosize: tdef; register: tregister; const ref: treference);
  1251. var
  1252. extra_slots: longint;
  1253. tmpref: treference;
  1254. begin
  1255. tmpref:=ref;
  1256. extra_slots:=prepare_stack_for_ref(list,tmpref,false);
  1257. a_load_reg_stack(list,fromsize,register);
  1258. if def2regtyp(fromsize)=R_INTREGISTER then
  1259. resize_stack_int_val(list,fromsize,tosize,assigned(tmpref.symbol));
  1260. a_load_stack_ref(list,tosize,tmpref,extra_slots);
  1261. end;
  1262. procedure thlcgwasm.a_load_reg_reg(list: TAsmList; fromsize, tosize: tdef; reg1, reg2: tregister);
  1263. begin
  1264. a_load_reg_stack(list,fromsize,reg1);
  1265. if def2regtyp(fromsize)=R_INTREGISTER then
  1266. resize_stack_int_val(list,fromsize,tosize,false);
  1267. a_load_stack_reg(list,tosize,reg2);
  1268. end;
  1269. procedure thlcgwasm.a_load_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; register: tregister);
  1270. var
  1271. extra_slots: longint;
  1272. tmpref: treference;
  1273. begin
  1274. tmpref:=ref;
  1275. extra_slots:=prepare_stack_for_ref(list,tmpref,false);
  1276. a_load_ref_stack(list,fromsize,tmpref,extra_slots);
  1277. if def2regtyp(fromsize)=R_INTREGISTER then
  1278. resize_stack_int_val(list,fromsize,tosize,false);
  1279. a_load_stack_reg(list,tosize,register);
  1280. end;
  1281. procedure thlcgwasm.a_load_ref_ref(list: TAsmList; fromsize, tosize: tdef; const sref: treference; const dref: treference);
  1282. var
  1283. extra_sslots,
  1284. extra_dslots: longint;
  1285. tmpsref, tmpdref: treference;
  1286. tmpreg: tregister;
  1287. begin
  1288. if sref.base<>NR_EVAL_STACK_BASE then
  1289. begin
  1290. tmpsref:=sref;
  1291. tmpdref:=dref;
  1292. { make sure the destination reference is on top, since in the end the
  1293. order has to be "destref, value" -> first create "destref, sourceref" }
  1294. extra_dslots:=prepare_stack_for_ref(list,tmpdref,false);
  1295. extra_sslots:=prepare_stack_for_ref(list,tmpsref,false);
  1296. a_load_ref_stack(list,fromsize,tmpsref,extra_sslots);
  1297. if def2regtyp(fromsize)=R_INTREGISTER then
  1298. resize_stack_int_val(list,fromsize,tosize,assigned(tmpdref.symbol));
  1299. a_load_stack_ref(list,tosize,tmpdref,extra_dslots);
  1300. end
  1301. else
  1302. begin
  1303. { verify if we have the same reference }
  1304. if references_equal(sref,dref) then
  1305. exit;
  1306. tmpreg:=getregisterfordef(list,tosize);
  1307. a_load_ref_reg(list,fromsize,tosize,sref,tmpreg);
  1308. a_load_reg_ref(list,tosize,tosize,tmpreg,dref);
  1309. end;
  1310. end;
  1311. procedure thlcgwasm.a_load_loc_ref(list : TAsmList;fromsize, tosize: tdef; const loc: tlocation; const ref : treference);
  1312. var
  1313. tmpref: treference;
  1314. begin
  1315. if is_methodptr_like_type(tosize) and (loc.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  1316. begin
  1317. tmpref:=ref;
  1318. a_load_reg_ref(list,voidcodepointertype,voidcodepointertype,loc.register,tmpref);
  1319. inc(tmpref.offset,voidcodepointertype.size);
  1320. { the second part could be either self or parentfp }
  1321. if tosize.size=(voidcodepointertype.size+voidpointertype.size) then
  1322. a_load_reg_ref(list,voidpointertype,voidpointertype,loc.registerhi,tmpref)
  1323. else if tosize.size=(voidcodepointertype.size+parentfpvoidpointertype.size) then
  1324. a_load_reg_ref(list,parentfpvoidpointertype,parentfpvoidpointertype,loc.registerhi,tmpref)
  1325. else
  1326. internalerror(2021100301);
  1327. end
  1328. else
  1329. inherited;
  1330. end;
  1331. procedure thlcgwasm.a_loadaddr_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; r: tregister);
  1332. begin
  1333. a_loadaddr_ref_stack(list,fromsize,tosize,ref);
  1334. a_load_stack_reg(list, tosize, r);
  1335. end;
  1336. procedure thlcgwasm.a_load_subsetref_regs_index(list: TAsmList; subsetsize: tdef; loadbitsize: byte; const sref: tsubsetreference; valuereg: tregister);
  1337. var
  1338. tmpref: treference;
  1339. extra_value_reg,
  1340. tmpreg: tregister;
  1341. begin
  1342. tmpreg:=getintregister(list,osuinttype);
  1343. tmpref:=sref.ref;
  1344. inc(tmpref.offset,loadbitsize div 8);
  1345. extra_value_reg:=getintregister(list,osuinttype);
  1346. a_op_reg_reg(list,OP_SHR,osuinttype,sref.bitindexreg,valuereg);
  1347. { ensure we don't load anything past the end of the array }
  1348. a_cmp_const_reg_stack(list,osuinttype,OC_A,loadbitsize-sref.bitlen,sref.bitindexreg);
  1349. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_if));
  1350. decstack(current_asmdata.CurrAsmList,1);
  1351. { Y-x = -(Y-x) }
  1352. a_op_const_reg_reg(list,OP_SUB,osuinttype,loadbitsize,sref.bitindexreg,tmpreg);
  1353. a_op_reg_reg(list,OP_NEG,osuinttype,tmpreg,tmpreg);
  1354. { load next "loadbitsize" bits of the array }
  1355. a_load_ref_reg(list,cgsize_orddef(int_cgsize(loadbitsize div 8)),osuinttype,tmpref,extra_value_reg);
  1356. { tmpreg is in the range 1..<cpu_bitsize>-1 -> always ok }
  1357. a_op_reg_reg(list,OP_SHL,osuinttype,tmpreg,extra_value_reg);
  1358. { merge }
  1359. a_op_reg_reg(list,OP_OR,osuinttype,extra_value_reg,valuereg);
  1360. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_end_if));
  1361. { sign extend or mask other bits }
  1362. if is_signed(subsetsize) then
  1363. begin
  1364. a_op_const_reg(list,OP_SHL,osuinttype,AIntBits-sref.bitlen,valuereg);
  1365. a_op_const_reg(list,OP_SAR,osuinttype,AIntBits-sref.bitlen,valuereg);
  1366. end
  1367. else
  1368. a_op_const_reg(list,OP_AND,osuinttype,tcgint((aword(1) shl sref.bitlen)-1),valuereg);
  1369. end;
  1370. procedure thlcgwasm.a_load_regconst_subsetref_intern(list : TAsmList; fromsize, subsetsize: tdef; fromreg: tregister; const sref: tsubsetreference; slopt: tsubsetloadopt);
  1371. var
  1372. tmpreg, tmpindexreg, valuereg, extra_value_reg, maskreg: tregister;
  1373. tosreg, fromsreg: tsubsetregister;
  1374. tmpref: treference;
  1375. bitmask: aword;
  1376. loadsize: torddef;
  1377. loadbitsize: byte;
  1378. extra_load: boolean;
  1379. begin
  1380. { the register must be able to contain the requested value }
  1381. if (fromsize.size*8<sref.bitlen) then
  1382. internalerror(2006081613);
  1383. get_subsetref_load_info(sref,loadsize,extra_load);
  1384. loadbitsize:=loadsize.size*8;
  1385. { load the (first part) of the bit sequence }
  1386. valuereg:=getintregister(list,osuinttype);
  1387. a_load_ref_reg(list,loadsize,osuinttype,sref.ref,valuereg);
  1388. { constant offset of bit sequence? }
  1389. if not extra_load then
  1390. begin
  1391. if (sref.bitindexreg=NR_NO) then
  1392. begin
  1393. { use subsetreg routine, it may have been overridden with an optimized version }
  1394. tosreg.subsetreg:=valuereg;
  1395. tosreg.subsetregsize:=def_cgsize(osuinttype);
  1396. { subsetregs always count bits from right to left }
  1397. tosreg.startbit:=sref.startbit;
  1398. tosreg.bitlen:=sref.bitlen;
  1399. a_load_regconst_subsetreg_intern(list,fromsize,subsetsize,fromreg,tosreg,slopt);
  1400. end
  1401. else
  1402. begin
  1403. if (sref.startbit<>0) then
  1404. internalerror(2006081710);
  1405. { should be handled by normal code and will give wrong result }
  1406. { on x86 for the '1 shl bitlen' below }
  1407. if (sref.bitlen=AIntBits) then
  1408. internalerror(2006081711);
  1409. { zero the bits we have to insert }
  1410. if (slopt<>SL_SETMAX) then
  1411. begin
  1412. maskreg:=getintregister(list,osuinttype);
  1413. a_load_const_reg(list,osuinttype,tcgint((aword(1) shl sref.bitlen)-1),maskreg);
  1414. a_op_reg_reg(list,OP_SHL,osuinttype,sref.bitindexreg,maskreg);
  1415. a_op_reg_reg(list,OP_NOT,osuinttype,maskreg,maskreg);
  1416. a_op_reg_reg(list,OP_AND,osuinttype,maskreg,valuereg);
  1417. end;
  1418. { insert the value }
  1419. if (slopt<>SL_SETZERO) then
  1420. begin
  1421. tmpreg:=getintregister(list,osuinttype);
  1422. if (slopt<>SL_SETMAX) then
  1423. a_load_reg_reg(list,fromsize,osuinttype,fromreg,tmpreg)
  1424. else if (sref.bitlen<>AIntBits) then
  1425. a_load_const_reg(list,osuinttype,tcgint((aword(1) shl sref.bitlen)-1), tmpreg)
  1426. else
  1427. a_load_const_reg(list,osuinttype,-1,tmpreg);
  1428. if not(slopt in [SL_REGNOSRCMASK,SL_SETMAX]) then
  1429. a_op_const_reg(list,OP_AND,osuinttype,tcgint((aword(1) shl sref.bitlen)-1),tmpreg);
  1430. a_op_reg_reg(list,OP_SHL,osuinttype,sref.bitindexreg,tmpreg);
  1431. a_op_reg_reg(list,OP_OR,osuinttype,tmpreg,valuereg);
  1432. end;
  1433. end;
  1434. { store back to memory }
  1435. tmpreg:=getintregister(list,loadsize);
  1436. a_load_reg_reg(list,osuinttype,loadsize,valuereg,tmpreg);
  1437. a_load_reg_ref(list,loadsize,loadsize,tmpreg,sref.ref);
  1438. exit;
  1439. end
  1440. else
  1441. begin
  1442. { load next value }
  1443. extra_value_reg:=getintregister(list,osuinttype);
  1444. tmpref:=sref.ref;
  1445. inc(tmpref.offset,loadbitsize div 8);
  1446. { should maybe be taken out too, can be done more efficiently }
  1447. { on e.g. i386 with shld/shrd }
  1448. if (sref.bitindexreg = NR_NO) then
  1449. begin
  1450. a_load_ref_reg(list,loadsize,osuinttype,tmpref,extra_value_reg);
  1451. fromsreg.subsetreg:=fromreg;
  1452. fromsreg.subsetregsize:=def_cgsize(fromsize);
  1453. tosreg.subsetreg:=valuereg;
  1454. tosreg.subsetregsize:=def_cgsize(osuinttype);
  1455. { transfer first part }
  1456. fromsreg.bitlen:=loadbitsize-sref.startbit;
  1457. tosreg.bitlen:=fromsreg.bitlen;
  1458. { valuereg must contain the lower bits of the value at bits [startbit..loadbitsize] }
  1459. { lower bits of the value ... }
  1460. fromsreg.startbit:=0;
  1461. { ... to startbit }
  1462. tosreg.startbit:=sref.startbit;
  1463. case slopt of
  1464. SL_SETZERO,
  1465. SL_SETMAX:
  1466. a_load_regconst_subsetreg_intern(list,fromsize,subsetsize,fromreg,tosreg,slopt);
  1467. else
  1468. a_load_subsetreg_subsetreg(list,subsetsize,subsetsize,fromsreg,tosreg);
  1469. end;
  1470. {$ifndef cpuhighleveltarget}
  1471. valuereg:=cg.makeregsize(list,valuereg,def_cgsize(loadsize));
  1472. a_load_reg_ref(list,loadsize,loadsize,valuereg,sref.ref);
  1473. {$else}
  1474. tmpreg:=getintregister(list,loadsize);
  1475. a_load_reg_reg(list,osuinttype,loadsize,valuereg,tmpreg);
  1476. a_load_reg_ref(list,loadsize,loadsize,tmpreg,sref.ref);
  1477. {$endif}
  1478. { transfer second part }
  1479. { extra_value_reg must contain the upper bits of the value at bits [0..bitlen-(loadbitsize-startbit)] }
  1480. fromsreg.startbit:=fromsreg.bitlen;
  1481. tosreg.startbit:=0;
  1482. tosreg.subsetreg:=extra_value_reg;
  1483. fromsreg.bitlen:=sref.bitlen-fromsreg.bitlen;
  1484. tosreg.bitlen:=fromsreg.bitlen;
  1485. case slopt of
  1486. SL_SETZERO,
  1487. SL_SETMAX:
  1488. a_load_regconst_subsetreg_intern(list,fromsize,subsetsize,fromreg,tosreg,slopt);
  1489. else
  1490. a_load_subsetreg_subsetreg(list,subsetsize,subsetsize,fromsreg,tosreg);
  1491. end;
  1492. tmpreg:=getintregister(list,loadsize);
  1493. a_load_reg_reg(list,osuinttype,loadsize,extra_value_reg,tmpreg);
  1494. a_load_reg_ref(list,loadsize,loadsize,tmpreg,tmpref);
  1495. exit;
  1496. end
  1497. else
  1498. begin
  1499. if (sref.startbit <> 0) then
  1500. internalerror(2006081812);
  1501. { should be handled by normal code and will give wrong result }
  1502. { on x86 for the '1 shl bitlen' below }
  1503. if (sref.bitlen = AIntBits) then
  1504. internalerror(2006081713);
  1505. { generate mask to zero the bits we have to insert }
  1506. if (slopt <> SL_SETMAX) then
  1507. begin
  1508. maskreg := getintregister(list,osuinttype);
  1509. a_load_const_reg(list,osuinttype,tcgint((aword(1) shl sref.bitlen)-1),maskreg);
  1510. a_op_reg_reg(list,OP_SHL,osuinttype,sref.bitindexreg,maskreg);
  1511. a_op_reg_reg(list,OP_NOT,osuinttype,maskreg,maskreg);
  1512. a_op_reg_reg(list,OP_AND,osuinttype,maskreg,valuereg);
  1513. end;
  1514. { insert the value }
  1515. if (slopt <> SL_SETZERO) then
  1516. begin
  1517. tmpreg := getintregister(list,osuinttype);
  1518. if (slopt <> SL_SETMAX) then
  1519. a_load_reg_reg(list,fromsize,osuinttype,fromreg,tmpreg)
  1520. else if (sref.bitlen <> AIntBits) then
  1521. a_load_const_reg(list,osuinttype,tcgint((aword(1) shl sref.bitlen) - 1), tmpreg)
  1522. else
  1523. a_load_const_reg(list,osuinttype,-1,tmpreg);
  1524. if not(slopt in [SL_REGNOSRCMASK,SL_SETMAX]) then
  1525. { mask left over bits }
  1526. a_op_const_reg(list,OP_AND,osuinttype,tcgint((aword(1) shl sref.bitlen)-1),tmpreg);
  1527. a_op_reg_reg(list,OP_SHL,osuinttype,sref.bitindexreg,tmpreg);
  1528. a_op_reg_reg(list,OP_OR,osuinttype,tmpreg,valuereg);
  1529. end;
  1530. tmpreg:=getintregister(list,loadsize);
  1531. a_load_reg_reg(list,osuinttype,loadsize,valuereg,tmpreg);
  1532. a_load_reg_ref(list,loadsize,loadsize,tmpreg,sref.ref);
  1533. { make sure we do not read/write past the end of the array }
  1534. a_cmp_const_reg_stack(list,osuinttype,OC_A,loadbitsize-sref.bitlen,sref.bitindexreg);
  1535. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_if));
  1536. decstack(current_asmdata.CurrAsmList,1);
  1537. a_load_ref_reg(list,loadsize,osuinttype,tmpref,extra_value_reg);
  1538. tmpindexreg:=getintregister(list,osuinttype);
  1539. { load current array value }
  1540. if (slopt<>SL_SETZERO) then
  1541. begin
  1542. tmpreg:=getintregister(list,osuinttype);
  1543. if (slopt<>SL_SETMAX) then
  1544. a_load_reg_reg(list,fromsize,osuinttype,fromreg,tmpreg)
  1545. else if (sref.bitlen<>AIntBits) then
  1546. a_load_const_reg(list,osuinttype,tcgint((aword(1) shl sref.bitlen) - 1), tmpreg)
  1547. else
  1548. a_load_const_reg(list,osuinttype,-1,tmpreg);
  1549. end;
  1550. { generate mask to zero the bits we have to insert }
  1551. if (slopt<>SL_SETMAX) then
  1552. begin
  1553. maskreg:=getintregister(list,osuinttype);
  1554. { Y-x = -(x-Y) }
  1555. a_op_const_reg_reg(list,OP_SUB,osuinttype,loadbitsize,sref.bitindexreg,tmpindexreg);
  1556. a_op_reg_reg(list,OP_NEG,osuinttype,tmpindexreg,tmpindexreg);
  1557. a_load_const_reg(list,osuinttype,tcgint((aword(1) shl sref.bitlen)-1),maskreg);
  1558. a_op_reg_reg(list,OP_SHR,osuinttype,tmpindexreg,maskreg);
  1559. a_op_reg_reg(list,OP_NOT,osuinttype,maskreg,maskreg);
  1560. a_op_reg_reg(list,OP_AND,osuinttype,maskreg,extra_value_reg);
  1561. end;
  1562. if (slopt<>SL_SETZERO) then
  1563. begin
  1564. if not(slopt in [SL_REGNOSRCMASK,SL_SETMAX]) then
  1565. a_op_const_reg(list,OP_AND,osuinttype,tcgint((aword(1) shl sref.bitlen)-1),tmpreg);
  1566. a_op_reg_reg(list,OP_SHR,osuinttype,tmpindexreg,tmpreg);
  1567. a_op_reg_reg(list,OP_OR,osuinttype,tmpreg,extra_value_reg);
  1568. end;
  1569. {$ifndef cpuhighleveltarget}
  1570. extra_value_reg:=cg.makeregsize(list,extra_value_reg,def_cgsize(loadsize));
  1571. a_load_reg_ref(list,loadsize,loadsize,extra_value_reg,tmpref);
  1572. {$else}
  1573. tmpreg:=getintregister(list,loadsize);
  1574. a_load_reg_reg(list,osuinttype,loadsize,extra_value_reg,tmpreg);
  1575. a_load_reg_ref(list,loadsize,loadsize,tmpreg,tmpref);
  1576. {$endif}
  1577. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_end_if));
  1578. end;
  1579. end;
  1580. end;
  1581. procedure thlcgwasm.a_op_const_reg(list: TAsmList; Op: TOpCG; size: tdef; a: tcgint; reg: TRegister);
  1582. begin
  1583. a_op_const_reg_reg(list,op,size,a,reg,reg);
  1584. end;
  1585. procedure thlcgwasm.a_op_const_reg_reg(list: TAsmList; op: TOpCg; size: tdef; a: tcgint; src, dst: tregister);
  1586. begin
  1587. a_load_reg_stack(list,size,src);
  1588. a_op_const_stack(list,op,size,a);
  1589. a_load_stack_reg(list,size,dst);
  1590. end;
  1591. procedure thlcgwasm.a_op_const_ref(list: TAsmList; Op: TOpCG; size: tdef; a: tcgint; const ref: TReference);
  1592. var
  1593. extra_slots: longint;
  1594. tmpref: treference;
  1595. begin
  1596. tmpref:=ref;
  1597. extra_slots:=prepare_stack_for_ref(list,tmpref,true);
  1598. { TODO, here or in peepholeopt: use iinc when possible }
  1599. a_load_ref_stack(list,size,tmpref,extra_slots);
  1600. a_op_const_stack(list,op,size,a);
  1601. { for android verifier }
  1602. if (def2regtyp(size)=R_INTREGISTER) and
  1603. (assigned(tmpref.symbol)) then
  1604. resize_stack_int_val(list,size,size,true);
  1605. a_load_stack_ref(list,size,tmpref,extra_slots);
  1606. end;
  1607. procedure thlcgwasm.a_op_ref_reg(list: TAsmList; Op: TOpCG; size: tdef; const ref: TReference; reg: TRegister);
  1608. begin
  1609. if not(op in [OP_NOT,OP_NEG]) then
  1610. a_load_reg_stack(list,size,reg);
  1611. a_op_ref_stack(list,op,size,ref);
  1612. a_load_stack_reg(list,size,reg);
  1613. end;
  1614. procedure thlcgwasm.a_op_reg_reg_reg(list: TAsmList; op: TOpCg; size: tdef; src1, src2, dst: tregister);
  1615. begin
  1616. if not(op in [OP_NOT,OP_NEG]) then
  1617. a_load_reg_stack(list,size,src2);
  1618. a_op_reg_stack(list,op,size,src1);
  1619. a_load_stack_reg(list,size,dst);
  1620. end;
  1621. procedure thlcgwasm.a_op_reg_reg(list: TAsmList; Op: TOpCG; size: tdef; reg1, reg2: TRegister);
  1622. begin
  1623. a_op_reg_reg_reg(list,op,size,reg1,reg2,reg2);
  1624. end;
  1625. procedure thlcgwasm.a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tdef; a: tcgint; src, dst: tregister; setflags: boolean; var ovloc: tlocation);
  1626. var
  1627. tmpreg: tregister;
  1628. begin
  1629. if not setflags then
  1630. begin
  1631. inherited;
  1632. exit;
  1633. end;
  1634. tmpreg:=getintregister(list,size);
  1635. a_load_const_reg(list,size,a,tmpreg);
  1636. a_op_reg_reg_reg_checkoverflow(list,op,size,tmpreg,src,dst,true,ovloc);
  1637. end;
  1638. procedure thlcgwasm.a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tdef; src1, src2, dst: tregister; setflags: boolean; var ovloc: tlocation);
  1639. var
  1640. orgsrc1: tregister = NR_NO;
  1641. orgsrc2: tregister = NR_NO;
  1642. docheck: boolean;
  1643. begin
  1644. if not setflags then
  1645. begin
  1646. inherited;
  1647. exit;
  1648. end;
  1649. { anything else cannot overflow }
  1650. docheck:=size.size in [4,8];
  1651. if docheck then
  1652. begin
  1653. orgsrc1:=src1;
  1654. orgsrc2:=src2;
  1655. if src1=dst then
  1656. begin
  1657. orgsrc1:=getintregister(list,size);
  1658. a_load_reg_reg(list,size,size,src1,orgsrc1);
  1659. end;
  1660. if src2=dst then
  1661. begin
  1662. orgsrc2:=getintregister(list,size);
  1663. a_load_reg_reg(list,size,size,src2,orgsrc2);
  1664. end;
  1665. end;
  1666. a_op_reg_reg_reg(list,op,size,src1,src2,dst);
  1667. if docheck then
  1668. begin
  1669. { * signed overflow for addition iff
  1670. - src1 and src2 are negative and result is positive (excep in case of
  1671. subtraction, then sign of src1 has to be inverted)
  1672. - src1 and src2 are positive and result is negative
  1673. -> Simplified boolean equivalent (in terms of sign bits):
  1674. not(src1 xor src2) and (src1 xor dst)
  1675. for subtraction, multiplication: invert src1 sign bit
  1676. for division: handle separately (div by zero, low(inttype) div -1),
  1677. not supported by this code
  1678. * unsigned overflow iff carry out, aka dst < src1 or dst < src2
  1679. }
  1680. location_reset(ovloc,LOC_REGISTER,OS_S32);
  1681. { not pasbool8, because then we'd still have to convert the integer to
  1682. a boolean via branches for Dalvik}
  1683. ovloc.register:=getintregister(list,s32inttype);
  1684. if not ((size.typ=pointerdef) or
  1685. ((size.typ=orddef) and
  1686. (torddef(size).ordtype in [u64bit,u16bit,u32bit,u8bit,uchar,
  1687. pasbool1,pasbool8,pasbool16,pasbool32,pasbool64]))) then
  1688. begin
  1689. a_load_reg_stack(list,size,orgsrc1);
  1690. if op in [OP_SUB,OP_IMUL] then
  1691. a_op_stack(list,OP_NOT,size);
  1692. a_op_reg_stack(list,OP_XOR,size,orgsrc2);
  1693. a_op_stack(list,OP_NOT,size);
  1694. a_load_reg_stack(list,size,orgsrc1);
  1695. a_op_reg_stack(list,OP_XOR,size,dst);
  1696. a_op_stack(list,OP_AND,size);
  1697. a_op_const_stack(list,OP_SHR,size,(size.size*8)-1);
  1698. if size.size=8 then
  1699. begin
  1700. //todo: any operands needed?
  1701. list.concat(taicpu.op_none(a_i32_wrap_i64));
  1702. end;
  1703. a_load_stack_reg(list,s32inttype,ovloc.register);
  1704. end
  1705. else
  1706. begin
  1707. if op=OP_SUB then
  1708. begin
  1709. { unsigned (src1-src2) overflows iff (src1<src2) }
  1710. a_cmp_reg_reg_stack(list,size,OC_B,orgsrc1,orgsrc2);
  1711. a_load_stack_reg(list,s32inttype,ovloc.register);
  1712. end
  1713. else
  1714. begin
  1715. { can be optimized by removing duplicate xor'ing to convert dst from
  1716. signed to unsigned quadrant }
  1717. a_cmp_reg_reg_stack(list,size,OC_AE,dst,orgsrc1);
  1718. a_cmp_reg_reg_stack(list,size,OC_AE,dst,orgsrc2);
  1719. a_op_stack(list,OP_AND,s32inttype);
  1720. a_load_stack_reg(list,s32inttype,ovloc.register);
  1721. end;
  1722. end;
  1723. end
  1724. else
  1725. ovloc.loc:=LOC_VOID;
  1726. end;
  1727. procedure thlcgwasm.a_cmp_const_ref_label(list: TAsmList; size: tdef; cmp_op: topcmp; a: tcgint; const ref: treference; l: tasmlabel);
  1728. begin
  1729. a_cmp_const_ref_stack(list,size,cmp_op,a,ref);
  1730. current_asmdata.CurrAsmList.concat(taicpu.op_sym(a_br_if,l));
  1731. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  1732. end;
  1733. procedure thlcgwasm.a_cmp_const_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; a: tcgint; reg: tregister; l: tasmlabel);
  1734. begin
  1735. a_cmp_const_reg_stack(list,size,cmp_op,a,reg);
  1736. current_asmdata.CurrAsmList.concat(taicpu.op_sym(a_br_if,l));
  1737. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  1738. end;
  1739. procedure thlcgwasm.a_cmp_ref_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; const ref: treference; reg: tregister; l: tasmlabel);
  1740. begin
  1741. a_cmp_ref_reg_stack(list,size,cmp_op,ref,reg);
  1742. current_asmdata.CurrAsmList.concat(taicpu.op_sym(a_br_if,l));
  1743. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  1744. end;
  1745. procedure thlcgwasm.a_cmp_reg_ref_label(list: TAsmList; size: tdef; cmp_op: topcmp; reg: tregister; const ref: treference; l: tasmlabel);
  1746. begin
  1747. a_cmp_reg_ref_stack(list,size,cmp_op,reg,ref);
  1748. current_asmdata.CurrAsmList.concat(taicpu.op_sym(a_br_if,l));
  1749. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  1750. end;
  1751. procedure thlcgwasm.a_cmp_reg_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; reg1, reg2: tregister; l: tasmlabel);
  1752. begin
  1753. a_cmp_reg_reg_stack(list,size,cmp_op,reg1,reg2);
  1754. current_asmdata.CurrAsmList.concat(taicpu.op_sym(a_br_if,l));
  1755. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  1756. end;
  1757. procedure thlcgwasm.a_jmp_always(list: TAsmList; l: tasmlabel);
  1758. begin
  1759. if (l=current_procinfo.CurrBreakLabel) or
  1760. (l=current_procinfo.CurrContinueLabel) or
  1761. (l=current_procinfo.CurrExitLabel) then
  1762. list.concat(taicpu.op_sym(a_br,l))
  1763. else
  1764. begin
  1765. {$ifndef EXTDEBUG}
  1766. Internalerror(2019091806); // unexpected jump
  1767. {$endif EXTDEBUG}
  1768. list.concat(tai_comment.create(strpnew('Unable to find destination of label '+l.name)));
  1769. end;
  1770. end;
  1771. procedure thlcgwasm.a_jmp_always_pascal_goto(list: TAsmList; l: tasmlabel);
  1772. var
  1773. br_ins: taicpu;
  1774. begin
  1775. br_ins:=taicpu.op_sym(a_br,l);
  1776. br_ins.is_br_generated_by_goto:=true;
  1777. list.concat(br_ins);
  1778. end;
  1779. procedure thlcgwasm.a_loadfpu_ref_ref(list: TAsmList; fromsize, tosize: tdef; const ref1, ref2: treference);
  1780. var
  1781. dstack_slots: longint;
  1782. tmpref1, tmpref2: treference;
  1783. begin
  1784. tmpref1:=ref1;
  1785. tmpref2:=ref2;
  1786. dstack_slots:=prepare_stack_for_ref(list,tmpref2,false);
  1787. a_load_ref_stack(list,fromsize,tmpref1,prepare_stack_for_ref(list,tmpref1,false));
  1788. resizestackfpuval(list,def_cgsize(fromsize),def_cgsize(tosize));
  1789. a_load_stack_ref(list,tosize,tmpref2,dstack_slots);
  1790. end;
  1791. procedure thlcgwasm.a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; reg: tregister);
  1792. var
  1793. tmpref: treference;
  1794. begin
  1795. tmpref:=ref;
  1796. a_load_ref_stack(list,fromsize,tmpref,prepare_stack_for_ref(list,tmpref,false));
  1797. resizestackfpuval(list,def_cgsize(fromsize),def_cgsize(tosize));
  1798. a_load_stack_reg(list,tosize,reg);
  1799. end;
  1800. procedure thlcgwasm.a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tdef; reg: tregister; const ref: treference);
  1801. var
  1802. dstack_slots: longint;
  1803. tmpref: treference;
  1804. begin
  1805. tmpref:=ref;
  1806. dstack_slots:=prepare_stack_for_ref(list,tmpref,false);
  1807. a_load_reg_stack(list,fromsize,reg);
  1808. resizestackfpuval(list,def_cgsize(fromsize),def_cgsize(tosize));
  1809. a_load_stack_ref(list,tosize,tmpref,dstack_slots);
  1810. end;
  1811. procedure thlcgwasm.a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tdef; reg1, reg2: tregister);
  1812. begin
  1813. a_load_reg_stack(list,fromsize,reg1);
  1814. resizestackfpuval(list,def_cgsize(fromsize),def_cgsize(tosize));
  1815. a_load_stack_reg(list,tosize,reg2);
  1816. end;
  1817. procedure thlcgwasm.g_unreachable(list: TAsmList);
  1818. begin
  1819. list.Concat(taicpu.op_none(a_unreachable));
  1820. end;
  1821. procedure thlcgwasm.g_concatcopy(list: TAsmList; size: tdef; const source, dest: treference);
  1822. var
  1823. pd: tprocdef;
  1824. cgpara1,cgpara2,cgpara3 : TCGPara;
  1825. begin
  1826. if (source.base=NR_EVAL_STACK_BASE) or (source.base=NR_LOCAL_STACK_POINTER_REG) or
  1827. (source.index=NR_EVAL_STACK_BASE) or (source.index=NR_LOCAL_STACK_POINTER_REG) or
  1828. (dest.base=NR_EVAL_STACK_BASE) or (dest.base=NR_LOCAL_STACK_POINTER_REG) or
  1829. (dest.index=NR_EVAL_STACK_BASE) or (dest.index=NR_LOCAL_STACK_POINTER_REG) or
  1830. (size.size in [1,2,4,8]) then
  1831. inherited
  1832. else
  1833. begin
  1834. pd:=search_system_proc('MOVE');
  1835. cgpara1.init;
  1836. cgpara2.init;
  1837. cgpara3.init;
  1838. paramanager.getcgtempparaloc(list,pd,1,cgpara1);
  1839. paramanager.getcgtempparaloc(list,pd,2,cgpara2);
  1840. paramanager.getcgtempparaloc(list,pd,3,cgpara3);
  1841. if pd.is_pushleftright then
  1842. begin
  1843. { load source }
  1844. a_loadaddr_ref_cgpara(list,voidtype,source,cgpara1);
  1845. { load destination }
  1846. a_loadaddr_ref_cgpara(list,voidtype,dest,cgpara2);
  1847. { load size }
  1848. a_load_const_cgpara(list,sizesinttype,size.size,cgpara3);
  1849. end
  1850. else
  1851. begin
  1852. { load size }
  1853. a_load_const_cgpara(list,sizesinttype,size.size,cgpara3);
  1854. { load destination }
  1855. a_loadaddr_ref_cgpara(list,voidtype,dest,cgpara2);
  1856. { load source }
  1857. a_loadaddr_ref_cgpara(list,voidtype,source,cgpara1);
  1858. end;
  1859. paramanager.freecgpara(list,cgpara3);
  1860. paramanager.freecgpara(list,cgpara2);
  1861. paramanager.freecgpara(list,cgpara1);
  1862. g_call_system_proc(list,pd,[@cgpara1,@cgpara2,@cgpara3],nil).resetiftemp;
  1863. cgpara3.done;
  1864. cgpara2.done;
  1865. cgpara1.done;
  1866. end;
  1867. end;
  1868. procedure thlcgwasm.g_proc_entry(list: TAsmList; localsize: longint; nostackframe: boolean);
  1869. var
  1870. pd: tcpuprocdef;
  1871. begin
  1872. pd:=tcpuprocdef(current_procinfo.procdef);
  1873. g_procdef(list,pd,false);
  1874. if not nostackframe then
  1875. begin
  1876. ttgwasm(tg).allocframepointer(list,pd.frame_pointer_ref);
  1877. if pd.base_pointer_ref.base<>NR_LOCAL_STACK_POINTER_REG then
  1878. ttgwasm(tg).allocbasepointer(list,pd.base_pointer_ref);
  1879. g_fingerprint(list);
  1880. list.Concat(taicpu.op_sym(a_global_get,RefStackPointerSym));
  1881. incstack(list,1);
  1882. list.Concat(taicpu.op_ref(a_local_set,pd.base_pointer_ref));
  1883. decstack(list,1);
  1884. if (localsize>0) then begin
  1885. list.Concat(taicpu.op_ref(a_local_get,pd.base_pointer_ref));
  1886. incstack(list,1);
  1887. list.concat(taicpu.op_const(a_i32_const, localsize ));
  1888. incstack(list,1);
  1889. list.concat(taicpu.op_none(a_i32_sub));
  1890. decstack(list,1);
  1891. list.Concat(taicpu.op_ref(a_local_set,pd.frame_pointer_ref));
  1892. decstack(list,1);
  1893. list.Concat(taicpu.op_ref(a_local_get,pd.frame_pointer_ref));
  1894. incstack(list,1);
  1895. list.Concat(taicpu.op_sym(a_global_set,RefStackPointerSym));
  1896. decstack(list,1);
  1897. end;
  1898. end;
  1899. end;
  1900. procedure thlcgwasm.g_proc_exit(list: TAsmList; parasize: longint; nostackframe: boolean);
  1901. var
  1902. pd: tcpuprocdef;
  1903. begin
  1904. pd:=tcpuprocdef(current_procinfo.procdef);
  1905. if not nostackframe then
  1906. begin
  1907. list.Concat(taicpu.op_ref(a_local_get,pd.base_pointer_ref));
  1908. incstack(list,1);
  1909. list.Concat(taicpu.op_sym(a_global_set,RefStackPointerSym));
  1910. decstack(list,1);
  1911. end;
  1912. list.concat(taicpu.op_none(a_end_function));
  1913. end;
  1914. procedure thlcgwasm.g_rangecheck(list: TAsmList; const l: tlocation; fromdef, todef: tdef);
  1915. var
  1916. {$if defined(cpuhighleveltarget)}
  1917. aintmax: tcgint;
  1918. {$elseif defined(cpu64bitalu) or defined(cpu32bitalu)}
  1919. aintmax: aint;
  1920. {$else}
  1921. aintmax: longint;
  1922. {$endif}
  1923. //neglabel : tasmlabel;
  1924. //hreg : tregister;
  1925. lto,hto,
  1926. lfrom,hfrom : TConstExprInt;
  1927. fromsize, tosize: cardinal;
  1928. maxdef: tdef;
  1929. from_signed, to_signed: boolean;
  1930. begin
  1931. { range checking on and range checkable value? }
  1932. if not(cs_check_range in current_settings.localswitches) or
  1933. not(fromdef.typ in [orddef,enumdef]) or
  1934. { C-style booleans can't really fail range checks, }
  1935. { all values are always valid }
  1936. is_cbool(todef) then
  1937. exit;
  1938. {$if not defined(cpuhighleveltarget) and not defined(cpu64bitalu)}
  1939. { handle 64bit rangechecks separate for 32bit processors }
  1940. if is_64bit(fromdef) or is_64bit(todef) then
  1941. begin
  1942. cg64.g_rangecheck64(list,l,fromdef,todef);
  1943. exit;
  1944. end;
  1945. {$endif ndef cpuhighleveltarget and ndef cpu64bitalu}
  1946. { only check when assigning to scalar, subranges are different, }
  1947. { when todef=fromdef then the check is always generated }
  1948. getrange(fromdef,lfrom,hfrom);
  1949. getrange(todef,lto,hto);
  1950. from_signed := is_signed(fromdef);
  1951. to_signed := is_signed(todef);
  1952. { check the rangedef of the array, not the array itself }
  1953. { (only change now, since getrange needs the arraydef) }
  1954. if (todef.typ = arraydef) then
  1955. todef := tarraydef(todef).rangedef;
  1956. { no range check if from and to are equal and are both longint/dword }
  1957. { (if we have a 32bit processor) or int64/qword, since such }
  1958. { operations can at most cause overflows (JM) }
  1959. { Note that these checks are mostly processor independent, they only }
  1960. { have to be changed once we introduce 64bit subrange types }
  1961. {$if defined(cpuhighleveltarget) or defined(cpu64bitalu)}
  1962. if (fromdef=todef) and
  1963. (fromdef.typ=orddef) and
  1964. (((((torddef(fromdef).ordtype=s64bit) and
  1965. (lfrom = low(int64)) and
  1966. (hfrom = high(int64))) or
  1967. ((torddef(fromdef).ordtype=u64bit) and
  1968. (lfrom = low(qword)) and
  1969. (hfrom = high(qword))) or
  1970. ((torddef(fromdef).ordtype=scurrency) and
  1971. (lfrom = low(int64)) and
  1972. (hfrom = high(int64)))))) then
  1973. exit;
  1974. {$endif cpuhighleveltarget or cpu64bitalu}
  1975. { 32 bit operations are automatically widened to 64 bit on 64 bit addr
  1976. targets }
  1977. {$ifdef cpu32bitaddr}
  1978. if (fromdef = todef) and
  1979. (fromdef.typ=orddef) and
  1980. (((((torddef(fromdef).ordtype = s32bit) and
  1981. (lfrom = int64(low(longint))) and
  1982. (hfrom = int64(high(longint)))) or
  1983. ((torddef(fromdef).ordtype = u32bit) and
  1984. (lfrom = low(cardinal)) and
  1985. (hfrom = high(cardinal)))))) then
  1986. exit;
  1987. {$endif cpu32bitaddr}
  1988. { optimize some range checks away in safe cases }
  1989. fromsize := fromdef.size;
  1990. tosize := todef.size;
  1991. if ((from_signed = to_signed) or
  1992. (not from_signed)) and
  1993. (lto<=lfrom) and (hto>=hfrom) and
  1994. (fromsize <= tosize) then
  1995. begin
  1996. { if fromsize < tosize, and both have the same signed-ness or }
  1997. { fromdef is unsigned, then all bit patterns from fromdef are }
  1998. { valid for todef as well }
  1999. if (fromsize < tosize) then
  2000. exit;
  2001. if (fromsize = tosize) and
  2002. (from_signed = to_signed) then
  2003. { only optimize away if all bit patterns which fit in fromsize }
  2004. { are valid for the todef }
  2005. begin
  2006. {$ifopt Q+}
  2007. {$define overflowon}
  2008. {$Q-}
  2009. {$endif}
  2010. {$ifopt R+}
  2011. {$define rangeon}
  2012. {$R-}
  2013. {$endif}
  2014. if to_signed then
  2015. begin
  2016. { calculation of the low/high ranges must not overflow 64 bit
  2017. otherwise we end up comparing with zero for 64 bit data types on
  2018. 64 bit processors }
  2019. if (lto = (int64(-1) << (tosize * 8 - 1))) and
  2020. (hto = (-((int64(-1) << (tosize * 8 - 1))+1))) then
  2021. exit
  2022. end
  2023. else
  2024. begin
  2025. { calculation of the low/high ranges must not overflow 64 bit
  2026. otherwise we end up having all zeros for 64 bit data types on
  2027. 64 bit processors }
  2028. if (lto = 0) and
  2029. (qword(hto) = (qword(-1) >> (64-(tosize * 8))) ) then
  2030. exit
  2031. end;
  2032. {$ifdef overflowon}
  2033. {$Q+}
  2034. {$undef overflowon}
  2035. {$endif}
  2036. {$ifdef rangeon}
  2037. {$R+}
  2038. {$undef rangeon}
  2039. {$endif}
  2040. end
  2041. end;
  2042. { depending on the types involved, we perform the range check for 64 or
  2043. for 32 bit }
  2044. if fromsize=8 then
  2045. maxdef:=fromdef
  2046. else
  2047. maxdef:=todef;
  2048. {$if sizeof(aintmax) = 8}
  2049. if maxdef.size=8 then
  2050. aintmax:=high(int64)
  2051. else
  2052. {$endif}
  2053. begin
  2054. aintmax:=high(longint);
  2055. maxdef:=u32inttype;
  2056. end;
  2057. { generate the rangecheck code for the def where we are going to }
  2058. { store the result }
  2059. { use the trick that }
  2060. { a <= x <= b <=> 0 <= x-a <= b-a <=> unsigned(x-a) <= unsigned(b-a) }
  2061. { To be able to do that, we have to make sure however that either }
  2062. { fromdef and todef are both signed or unsigned, or that we leave }
  2063. { the parts < 0 and > maxlongint out }
  2064. if from_signed xor to_signed then
  2065. begin
  2066. if from_signed then
  2067. { from is signed, to is unsigned }
  2068. begin
  2069. { if high(from) < 0 -> always range error }
  2070. if (hfrom < 0) or
  2071. { if low(to) > maxlongint also range error }
  2072. (lto > aintmax) then
  2073. begin
  2074. g_call_system_proc(list,'fpc_rangeerror',[],nil).resetiftemp;
  2075. hlcg.g_maybe_checkforexceptions(current_asmdata.CurrAsmList);
  2076. exit
  2077. end;
  2078. { from is signed and to is unsigned -> when looking at to }
  2079. { as an signed value, it must be < maxaint (otherwise }
  2080. { it will become negative, which is invalid since "to" is unsigned) }
  2081. if hto > aintmax then
  2082. hto := aintmax;
  2083. end
  2084. else
  2085. { from is unsigned, to is signed }
  2086. begin
  2087. if (lfrom > aintmax) or
  2088. (hto < 0) then
  2089. begin
  2090. g_call_system_proc(list,'fpc_rangeerror',[],nil).resetiftemp;
  2091. hlcg.g_maybe_checkforexceptions(current_asmdata.CurrAsmList);
  2092. exit
  2093. end;
  2094. { from is unsigned and to is signed -> when looking at to }
  2095. { as an unsigned value, it must be >= 0 (since negative }
  2096. { values are the same as values > maxlongint) }
  2097. if lto < 0 then
  2098. lto := 0;
  2099. end;
  2100. end;
  2101. a_load_loc_stack(list,fromdef,l);
  2102. resize_stack_int_val(list,fromdef,maxdef,false);
  2103. a_load_const_stack(list,maxdef,tcgint(int64(lto)),R_INTREGISTER);
  2104. a_op_stack(list,OP_SUB,maxdef);
  2105. {
  2106. if from_signed then
  2107. a_cmp_const_reg_label(list,OS_INT,OC_GTE,aint(hto-lto),hreg,neglabel)
  2108. else
  2109. }
  2110. if qword(hto-lto)>qword(aintmax) then
  2111. a_load_const_stack(list,maxdef,aintmax,R_INTREGISTER)
  2112. else
  2113. a_load_const_stack(list,maxdef,tcgint(int64(hto-lto)),R_INTREGISTER);
  2114. a_cmp_stack_stack(list,maxdef,OC_A);
  2115. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_if));
  2116. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  2117. g_call_system_proc(list,'fpc_rangeerror',[],nil).resetiftemp;
  2118. hlcg.g_maybe_checkforexceptions(current_asmdata.CurrAsmList);
  2119. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_end_if));
  2120. end;
  2121. procedure thlcgwasm.g_overflowcheck(list: TAsmList; const Loc: tlocation; def: tdef);
  2122. begin
  2123. { not possible, need the original operands }
  2124. internalerror(2012102101);
  2125. end;
  2126. procedure thlcgwasm.g_overflowCheck_loc(List: TAsmList; const Loc: TLocation; def: TDef; var ovloc: tlocation);
  2127. var
  2128. hl : tasmlabel;
  2129. begin
  2130. if not(cs_check_overflow in current_settings.localswitches) then
  2131. exit;
  2132. current_asmdata.getjumplabel(hl);
  2133. list.concat(taicpu.op_none(a_block));
  2134. a_cmp_const_loc_label(list,s32inttype,OC_EQ,0,ovloc,hl);
  2135. g_call_system_proc(list,'fpc_overflow',[],nil);
  2136. hlcg.g_maybe_checkforexceptions(current_asmdata.CurrAsmList);
  2137. list.concat(taicpu.op_none(a_end_block));
  2138. a_label(list,hl);
  2139. end;
  2140. procedure thlcgwasm.maybe_change_load_node_reg(list: TAsmList; var n: tnode; reload: boolean);
  2141. begin
  2142. { don't do anything, all registers become stack locations anyway }
  2143. end;
  2144. procedure thlcgwasm.gen_entry_code(list: TAsmList);
  2145. begin
  2146. inherited;
  2147. if not (po_assembler in current_procinfo.procdef.procoptions) then
  2148. begin
  2149. list.concat(taicpu.op_none(a_block));
  2150. list.concat(taicpu.op_none(a_block));
  2151. end;
  2152. end;
  2153. procedure thlcgwasm.gen_exit_code(list: TAsmList);
  2154. begin
  2155. if not (po_assembler in current_procinfo.procdef.procoptions) then
  2156. begin
  2157. list.concat(taicpu.op_none(a_end_block));
  2158. if ts_wasm_bf_exceptions in current_settings.targetswitches then
  2159. a_label(list,tcpuprocinfo(current_procinfo).CurrRaiseLabel);
  2160. if fevalstackheight<>0 then
  2161. {$ifdef DEBUG_WASMSTACK}
  2162. list.concat(tai_comment.Create(strpnew('!!! values remaining on stack at end of block !!!')));
  2163. {$else DEBUG_WASMSTACK}
  2164. internalerror(2021091801);
  2165. {$endif DEBUG_WASMSTACK}
  2166. end;
  2167. inherited;
  2168. end;
  2169. procedure thlcgwasm.gen_stack_check_size_para(list: TAsmList);
  2170. begin
  2171. { HACK: this is called *after* gen_stack_check_call, but the code it
  2172. generates is inserted *before* the call. Thus, it breaks our
  2173. incstack/decstack tracking and causes an internal error 2010120501. We
  2174. workaround this by generating a const instruction without calling
  2175. incstack, and instead we call incstack before the call, in
  2176. gen_stack_check_call. }
  2177. list.concat(taicpu.op_const(a_i32_const,current_procinfo.calc_stackframe_size));
  2178. end;
  2179. procedure thlcgwasm.gen_stack_check_call(list: TAsmList);
  2180. begin
  2181. { HACK: see the comment in gen_stack_check_size_para }
  2182. incstack(list,1);
  2183. inherited;
  2184. end;
  2185. procedure thlcgwasm.a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; srcsize, dstsize: tdef; src, dst: tregister);
  2186. begin
  2187. internalerror(2012090201);
  2188. end;
  2189. procedure thlcgwasm.a_loadmm_loc_reg(list: TAsmList; fromsize, tosize: tdef; const loc: tlocation; const reg: tregister; shuffle: pmmshuffle);
  2190. begin
  2191. internalerror(2012090202);
  2192. end;
  2193. procedure thlcgwasm.a_loadmm_reg_reg(list: TAsmList; fromsize, tosize: tdef; reg1, reg2: tregister; shuffle: pmmshuffle);
  2194. begin
  2195. internalerror(2012060130);
  2196. end;
  2197. procedure thlcgwasm.a_loadmm_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; reg: tregister; shuffle: pmmshuffle);
  2198. begin
  2199. internalerror(2012060131);
  2200. end;
  2201. procedure thlcgwasm.a_loadmm_reg_ref(list: TAsmList; fromsize, tosize: tdef; reg: tregister; const ref: treference; shuffle: pmmshuffle);
  2202. begin
  2203. internalerror(2012060132);
  2204. end;
  2205. procedure thlcgwasm.a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size: tdef; src, dst: tregister; shuffle: pmmshuffle);
  2206. begin
  2207. internalerror(2012060133);
  2208. end;
  2209. procedure thlcgwasm.a_loadmm_intreg_reg(list: TAsmList; fromsize, tosize: tdef; intreg, mmreg: tregister; shuffle: pmmshuffle);
  2210. begin
  2211. internalerror(2012060134);
  2212. end;
  2213. procedure thlcgwasm.a_loadmm_reg_intreg(list: TAsmList; fromsize, tosize: tdef; mmreg, intreg: tregister; shuffle: pmmshuffle);
  2214. begin
  2215. internalerror(2012060135);
  2216. end;
  2217. procedure thlcgwasm.g_stackpointer_alloc(list: TAsmList; size: longint);
  2218. begin
  2219. internalerror(2012090203);
  2220. end;
  2221. procedure thlcgwasm.g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);
  2222. begin
  2223. internalerror(2012090204);
  2224. end;
  2225. procedure thlcgwasm.g_adjust_self_value(list: TAsmList; procdef: tprocdef; ioffset: aint);
  2226. begin
  2227. internalerror(2012090205);
  2228. end;
  2229. procedure thlcgwasm.g_local_unwind(list: TAsmList; l: TAsmLabel);
  2230. begin
  2231. internalerror(2012090206);
  2232. end;
  2233. procedure thlcgwasm.g_procdef(list: TAsmList; pd: tprocdef; is_forward: Boolean);
  2234. begin
  2235. if not pd.is_generic then
  2236. list.Concat(tai_functype.create(pd.mangledname,tcpuprocdef(pd).create_functype,is_forward));
  2237. end;
  2238. procedure thlcgwasm.g_maybe_checkforexceptions(list: TasmList);
  2239. var
  2240. pd: tprocdef;
  2241. begin
  2242. if ts_wasm_bf_exceptions in current_settings.targetswitches then
  2243. begin
  2244. pd:=search_system_proc('fpc_raised_exception_flag');
  2245. g_call_system_proc(list,pd,[],nil).resetiftemp;
  2246. decstack(current_asmdata.CurrAsmList,1);
  2247. list.concat(taicpu.op_sym(a_br_if,tcpuprocinfo(current_procinfo).CurrRaiseLabel));
  2248. end;
  2249. end;
  2250. procedure thlcgwasm.a_load_stack_reg(list: TAsmList; size: tdef; reg: tregister);
  2251. begin
  2252. list.concat(taicpu.op_reg(a_local_set,reg));
  2253. decstack(list,1);
  2254. end;
  2255. procedure thlcgwasm.a_load_stack_ref(list: TAsmList; size: tdef; const ref: treference; extra_slots: longint);
  2256. var
  2257. opc: tasmop;
  2258. finishandval: tcgint;
  2259. begin
  2260. { fake location that indicates the value has to remain on the stack }
  2261. if ref.base=NR_EVAL_STACK_BASE then
  2262. exit;
  2263. opc:=loadstoreopcref(size,false,ref,finishandval);
  2264. if ref.refaddr=addr_got_tls then
  2265. list.concat(taicpu.op_const(opc,ref.offset))
  2266. else
  2267. list.concat(taicpu.op_ref(opc,ref));
  2268. { avoid problems with getting the size of an open array etc }
  2269. if wasmAlwayInMem(size) then
  2270. size:=ptruinttype;
  2271. decstack(list,1+extra_slots);
  2272. end;
  2273. procedure thlcgwasm.a_load_reg_stack(list: TAsmList; size: tdef; reg: tregister);
  2274. begin
  2275. list.concat(taicpu.op_reg(a_local_get,reg));
  2276. incstack(list,1);
  2277. end;
  2278. procedure thlcgwasm.a_load_ref_stack(list: TAsmList; size: tdef; const ref: treference; extra_slots: longint);
  2279. var
  2280. opc: tasmop;
  2281. finishandval: tcgint;
  2282. begin
  2283. { fake location that indicates the value is already on the stack? }
  2284. if (ref.base=NR_EVAL_STACK_BASE) then
  2285. exit;
  2286. opc:=loadstoreopcref(size,true,ref,finishandval);
  2287. if ref.refaddr=addr_got_tls then
  2288. list.concat(taicpu.op_const(opc,ref.offset))
  2289. else
  2290. list.concat(taicpu.op_ref(opc,ref));
  2291. { avoid problems with getting the size of an open array etc }
  2292. if wasmAlwayInMem(size) then
  2293. size:=ptruinttype;
  2294. incstack(list,1-extra_slots);
  2295. if finishandval<>-1 then
  2296. a_op_const_stack(list,OP_AND,size,finishandval);
  2297. // there's no cast check in Wasm
  2298. //if ref.checkcast then
  2299. // gen_typecheck(list,a_checkcast,size);
  2300. end;
  2301. procedure thlcgwasm.a_load_subsetref_stack(list : TAsmList;size: tdef; const sref: tsubsetreference);
  2302. var
  2303. tmpreg: TRegister;
  2304. begin
  2305. tmpreg:=getintregister(list,size);
  2306. a_load_subsetref_reg(list,size,size,sref,tmpreg);
  2307. a_load_reg_stack(list,size,tmpreg);
  2308. end;
  2309. function thlcgwasm.loadstoreopcref(def: tdef; isload: boolean; const ref: treference; out finishandval: tcgint): tasmop;
  2310. const
  2311. {iisload} {issigned}
  2312. getputmem8 : array [boolean, boolean] of TAsmOp = ((a_i32_store8, a_i32_store8), (a_i32_load8_u, a_i32_load8_s));
  2313. getputmem16 : array [boolean, boolean] of TAsmOp = ((a_i32_store16, a_i32_store16), (a_i32_load16_u ,a_i32_load16_s));
  2314. getputmem32 : array [boolean, boolean] of TAsmOp = ((a_i32_store, a_i32_store), (a_i32_load, a_i32_load));
  2315. getputmem64 : array [boolean, boolean] of TAsmOp = ((a_i64_store, a_i64_store), (a_i64_load, a_i64_load));
  2316. getputmemf32 : array [boolean] of TAsmOp = (a_f32_store, a_f32_load);
  2317. getputmemf64 : array [boolean] of TAsmOp = (a_f64_store, a_f64_load);
  2318. begin
  2319. if assigned(ref.symbol) and (ref.symbol.typ=AT_WASM_GLOBAL) then
  2320. begin
  2321. if isload then
  2322. result:=a_global_get
  2323. else
  2324. result:=a_global_set;
  2325. finishandval:=-1;
  2326. end
  2327. else if (ref.base<>NR_LOCAL_STACK_POINTER_REG) or assigned(ref.symbol) then
  2328. begin
  2329. { -> either a global (static) field, or a regular field. If a regular
  2330. field, then ref.base contains the self pointer, otherwise
  2331. ref.base=NR_NO. In both cases, the symbol contains all other
  2332. information (combined field name and type descriptor) }
  2333. case def.size of
  2334. 1: result := getputmem8[isload, is_signed(def)];
  2335. 2: result := getputmem16[isload, is_signed(def)];
  2336. 4:
  2337. if is_single(def) or ((def.typ=recorddef) and (trecorddef(def).contains_float_field)) then
  2338. result := getputmemf32[isload]
  2339. else
  2340. result := getputmem32[isload, is_signed(def)];
  2341. 8: if is_double(def) or ((def.typ=recorddef) and (trecorddef(def).contains_float_field)) then
  2342. result := getputmemf64[isload]
  2343. else
  2344. result := getputmem64[isload, is_signed(def)];
  2345. else
  2346. Internalerror(2019091501);
  2347. end;
  2348. //result:=getputopc[isload,ref.base=NR_NO];
  2349. finishandval:=-1;
  2350. { erase sign extension for byte/smallint loads }
  2351. if (def2regtyp(def)=R_INTREGISTER) and
  2352. not is_signed(def) and
  2353. (def.typ=orddef) and
  2354. not is_widechar(def) then
  2355. case def.size of
  2356. 1: if (torddef(def).high>127) then
  2357. finishandval:=255;
  2358. 2: if (torddef(def).high>32767) then
  2359. finishandval:=65535;
  2360. end;
  2361. end
  2362. else
  2363. begin
  2364. finishandval:=-1;
  2365. if isload then
  2366. result := a_local_get
  2367. else
  2368. result := a_local_set;
  2369. end;
  2370. end;
  2371. procedure thlcgwasm.resize_stack_int_val(list: TAsmList; fromsize, tosize: tdef; formemstore: boolean);
  2372. var
  2373. fromcgsize, tocgsize: tcgsize;
  2374. begin
  2375. { When storing to an array, field or global variable, make sure the
  2376. static type verification can determine that the stored value fits
  2377. within the boundaries of the declared type (to appease the Dalvik VM).
  2378. Local variables either get their type upgraded in the debug info,
  2379. or have no type information at all }
  2380. if formemstore and
  2381. (tosize.typ=orddef) then
  2382. if (torddef(tosize).ordtype in [u8bit,uchar]) then
  2383. tosize:=s8inttype
  2384. else if torddef(tosize).ordtype=u16bit then
  2385. tosize:=s16inttype;
  2386. fromcgsize:=def_cgsize(fromsize);
  2387. tocgsize:=def_cgsize(tosize);
  2388. if fromcgsize in [OS_S64,OS_64] then
  2389. begin
  2390. if not(tocgsize in [OS_S64,OS_64]) then
  2391. begin
  2392. { truncate }
  2393. list.concat(taicpu.op_none(a_i32_wrap_i64));
  2394. case tocgsize of
  2395. OS_8:
  2396. a_op_const_stack(list,OP_AND,s32inttype,255);
  2397. OS_S8:
  2398. list.concat(taicpu.op_none(a_i32_extend8_s));
  2399. OS_16:
  2400. a_op_const_stack(list,OP_AND,s32inttype,65535);
  2401. OS_S16:
  2402. list.concat(taicpu.op_none(a_i32_extend16_s));
  2403. OS_32,OS_S32:
  2404. ;
  2405. else
  2406. internalerror(2021012201);
  2407. end;
  2408. end;
  2409. end
  2410. else if tocgsize in [OS_S64,OS_64] then
  2411. begin
  2412. { extend }
  2413. case fromcgsize of
  2414. OS_8:
  2415. begin
  2416. a_op_const_stack(list,OP_AND,s32inttype,255);
  2417. list.concat(taicpu.op_none(a_i64_extend_i32_u));
  2418. end;
  2419. OS_S8:
  2420. begin
  2421. list.concat(taicpu.op_none(a_i64_extend_i32_u));
  2422. list.concat(taicpu.op_none(a_i64_extend8_s));
  2423. end;
  2424. OS_16:
  2425. begin
  2426. a_op_const_stack(list,OP_AND,s32inttype,65535);
  2427. list.concat(taicpu.op_none(a_i64_extend_i32_u));
  2428. end;
  2429. OS_S16:
  2430. begin
  2431. list.concat(taicpu.op_none(a_i64_extend_i32_u));
  2432. list.concat(taicpu.op_none(a_i64_extend16_s));
  2433. end;
  2434. OS_32:
  2435. list.concat(taicpu.op_none(a_i64_extend_i32_u));
  2436. OS_S32:
  2437. list.concat(taicpu.op_none(a_i64_extend_i32_s));
  2438. OS_64,OS_S64:
  2439. ;
  2440. else
  2441. internalerror(2021010301);
  2442. end;
  2443. end
  2444. else
  2445. begin
  2446. if tcgsize2size[fromcgsize]<tcgsize2size[tocgsize] then
  2447. begin
  2448. { extend }
  2449. case fromcgsize of
  2450. OS_8:
  2451. a_op_const_stack(list,OP_AND,s32inttype,255);
  2452. OS_S8:
  2453. begin
  2454. list.concat(taicpu.op_none(a_i32_extend8_s));
  2455. if tocgsize=OS_16 then
  2456. a_op_const_stack(list,OP_AND,s32inttype,65535);
  2457. end;
  2458. OS_16:
  2459. a_op_const_stack(list,OP_AND,s32inttype,65535);
  2460. OS_S16:
  2461. list.concat(taicpu.op_none(a_i32_extend16_s));
  2462. OS_32,OS_S32:
  2463. ;
  2464. else
  2465. internalerror(2021010302);
  2466. end;
  2467. end
  2468. else if tcgsize2size[fromcgsize]>=tcgsize2size[tocgsize] then
  2469. begin
  2470. { truncate }
  2471. case tocgsize of
  2472. OS_8:
  2473. a_op_const_stack(list,OP_AND,s32inttype,255);
  2474. OS_S8:
  2475. list.concat(taicpu.op_none(a_i32_extend8_s));
  2476. OS_16:
  2477. a_op_const_stack(list,OP_AND,s32inttype,65535);
  2478. OS_S16:
  2479. list.concat(taicpu.op_none(a_i32_extend16_s));
  2480. OS_32,OS_S32:
  2481. ;
  2482. else
  2483. internalerror(2021010302);
  2484. end;
  2485. end;
  2486. end;
  2487. end;
  2488. procedure thlcgwasm.maybe_resize_stack_para_val(list: TAsmList; retdef: tdef; callside: boolean);
  2489. var
  2490. convsize: tdef;
  2491. begin
  2492. if (retdef.typ=orddef) then
  2493. begin
  2494. if (torddef(retdef).ordtype in [u8bit,u16bit,uchar]) and
  2495. (torddef(retdef).high>=(1 shl (retdef.size*8-1))) then
  2496. begin
  2497. convsize:=nil;
  2498. if callside then
  2499. if torddef(retdef).ordtype in [u8bit,uchar] then
  2500. convsize:=s8inttype
  2501. else
  2502. convsize:=s16inttype
  2503. else if torddef(retdef).ordtype in [u8bit,uchar] then
  2504. convsize:=u8inttype
  2505. else
  2506. convsize:=u16inttype;
  2507. if assigned(convsize) then
  2508. resize_stack_int_val(list,s32inttype,convsize,false);
  2509. end;
  2510. end;
  2511. end;
  2512. procedure thlcgwasm.g_adjust_stack_after_call(list: TAsmList; pd: tabstractprocdef);
  2513. var
  2514. totalremovesize: longint;
  2515. realresdef: tdef;
  2516. ft: TWasmFuncType;
  2517. begin
  2518. if pd.typ=procvardef then
  2519. ft:=tcpuprocvardef(pd).create_functype
  2520. else
  2521. ft:=tcpuprocdef(pd).create_functype;
  2522. totalremovesize:=Length(ft.params)-Length(ft.results);
  2523. { remove parameters from internal evaluation stack counter (in case of
  2524. e.g. no parameters and a result, it can also increase) }
  2525. if totalremovesize>0 then
  2526. decstack(list,totalremovesize)
  2527. else if totalremovesize<0 then
  2528. incstack(list,-totalremovesize);
  2529. ft.free;
  2530. end;
  2531. procedure thlcgwasm.g_fingerprint(list: TAsmList);
  2532. begin
  2533. list.concat(taicpu.op_const(a_i64_const,Random(high(int64))));
  2534. list.concat(taicpu.op_const(a_i64_const,Random(high(int64))));
  2535. list.concat(taicpu.op_const(a_i64_const,Random(high(int64))));
  2536. list.concat(taicpu.op_const(a_i64_const,Random(high(int64))));
  2537. list.concat(taicpu.op_none(a_drop));
  2538. list.concat(taicpu.op_none(a_drop));
  2539. list.concat(taicpu.op_none(a_drop));
  2540. list.concat(taicpu.op_none(a_drop));
  2541. end;
  2542. procedure thlcgwasm.resizestackfpuval(list: TAsmList; fromsize, tosize: tcgsize);
  2543. begin
  2544. if (fromsize=OS_F32) and
  2545. (tosize=OS_F64) then
  2546. begin
  2547. list.concat(taicpu.op_none(a_f64_promote_f32));
  2548. end
  2549. else if (fromsize=OS_F64) and
  2550. (tosize=OS_F32) then
  2551. begin
  2552. list.concat(taicpu.op_none(a_f32_demote_f64));
  2553. end;
  2554. end;
  2555. procedure create_hlcodegen_cpu;
  2556. begin
  2557. hlcg:=thlcgwasm.create;
  2558. create_codegen;
  2559. end;
  2560. initialization
  2561. chlcgobj:=thlcgwasm;
  2562. create_hlcodegen:=@create_hlcodegen_cpu;
  2563. end.