hlcgcpu.pas 75 KB

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