hlcgcpu.pas 102 KB

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