hlcgcpu.pas 97 KB

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