hlcgcpu.pas 75 KB

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