hlcgcpu.pas 84 KB

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