hlcgcpu.pas 81 KB

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