hlcgcpu.pas 105 KB

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