hlcgcpu.pas 108 KB

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