hlcgcpu.pas 112 KB

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