hlcgcpu.pas 98 KB

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