hlcgcpu.pas 106 KB

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