hlcgcpu.pas 115 KB

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