hlcgcpu.pas 110 KB

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