hlcgcpu.pas 77 KB

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