hlcgcpu.pas 100 KB

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