hlcgcpu.pas 115 KB

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