hlcgcpu.pas 98 KB

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