hlcgcpu.pas 106 KB

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