hlcgcpu.pas 100 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533
  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;
  27. type
  28. { thlcgwasm }
  29. thlcgwasm = class(thlcgobj)
  30. private
  31. fevalstackheight,
  32. fmaxevalstackheight: longint;
  33. public
  34. blocks: integer;
  35. constructor create;
  36. procedure incblock;
  37. procedure decblock;
  38. procedure incstack(list : TAsmList;slots: longint);
  39. procedure decstack(list : TAsmList;slots: longint);
  40. class function def2regtyp(def: tdef): tregistertype; override;
  41. procedure a_load_const_cgpara(list : TAsmList;tosize : tdef;a : tcgint;const cgpara : TCGPara);override;
  42. function a_call_name(list : TAsmList;pd : tprocdef;const s : TSymStr; const paras: array of pcgpara; forceresdef: tdef; weak: boolean): tcgpara;override;
  43. function a_call_name_inherited(list : TAsmList;pd : tprocdef;const s : TSymStr; const paras: array of pcgpara): tcgpara;override;
  44. function a_call_reg(list: TAsmList; pd: tabstractprocdef; reg: tregister; const paras: array of pcgpara): tcgpara; override;
  45. { move instructions - a_load_FROM_TO }
  46. procedure a_load_const_reg(list : TAsmList;tosize : tdef;a : tcgint;register : tregister);override;
  47. procedure a_load_const_ref(list : TAsmList;tosize : tdef;a : tcgint;const ref : treference);override;
  48. procedure a_load_reg_ref(list : TAsmList;fromsize, tosize : tdef;register : tregister;const ref : treference);override;
  49. procedure a_load_reg_reg(list : TAsmList;fromsize, tosize : tdef;reg1,reg2 : tregister);override;
  50. procedure a_load_ref_reg(list : TAsmList;fromsize, tosize : tdef;const ref : treference;register : tregister);override;
  51. procedure a_load_ref_ref(list : TAsmList;fromsize, tosize : tdef;const sref : treference;const dref : treference);override;
  52. procedure a_loadaddr_ref_reg(list : TAsmList;fromsize, tosize : tdef;const ref : treference;r : tregister);override;
  53. { basic arithmetic operations }
  54. procedure a_op_const_reg(list: TAsmList; Op: TOpCG; size: tdef; a: tcgint; reg: TRegister); override;
  55. procedure a_op_const_reg_reg(list: TAsmList; op: TOpCg; size: tdef; a: tcgint; src, dst: tregister); override;
  56. procedure a_op_const_ref(list: TAsmList; Op: TOpCG; size: tdef; a: tcgint; const ref: TReference); override;
  57. procedure a_op_ref_reg(list: TAsmList; Op: TOpCG; size: tdef; const ref: TReference; reg: TRegister); override;
  58. procedure a_op_reg_reg_reg(list: TAsmList; op: TOpCg; size: tdef; src1, src2, dst: tregister); override;
  59. procedure a_op_reg_reg(list: TAsmList; Op: TOpCG; size: tdef; reg1, reg2: TRegister); override;
  60. procedure a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tdef; a: tcgint; src, dst: tregister;setflags : boolean;var ovloc : tlocation); override;
  61. procedure a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tdef; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation); override;
  62. procedure a_cmp_const_ref_label(list: TAsmList; size: tdef; cmp_op: topcmp; a: tcgint; const ref: treference; l: tasmlabel); override;
  63. procedure a_cmp_const_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; a: tcgint; reg: tregister; l: tasmlabel); override;
  64. procedure a_cmp_ref_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; const ref: treference; reg: tregister; l: tasmlabel); override;
  65. procedure a_cmp_reg_ref_label(list: TAsmList; size: tdef; cmp_op: topcmp; reg: tregister; const ref: treference; l: tasmlabel); override;
  66. procedure a_cmp_reg_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; reg1, reg2: tregister; l: tasmlabel); override;
  67. procedure a_jmp_always(list : TAsmList;l: tasmlabel); override;
  68. procedure g_concatcopy(list : TAsmList;size: tdef; const source,dest : treference);override;
  69. procedure g_copyshortstring(list : TAsmList;const source,dest : treference;strdef:tstringdef);override;
  70. procedure a_loadfpu_ref_ref(list: TAsmList; fromsize, tosize: tdef; const ref1, ref2: treference); override;
  71. procedure a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; reg: tregister); override;
  72. procedure a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tdef; reg: tregister; const ref: treference); override;
  73. procedure a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tdef; reg1, reg2: tregister); override;
  74. procedure g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean); override;
  75. procedure g_proc_exit(list : TAsmList;parasize:longint;nostackframe:boolean); override;
  76. procedure gen_load_return_value(list:TAsmList);override;
  77. procedure record_generated_code_for_procdef(pd: tprocdef; code, data: TAsmList); override;
  78. procedure g_incrrefcount(list : TAsmList;t: tdef; const ref: treference);override;
  79. procedure g_array_rtti_helper(list: TAsmList; t: tdef; const ref: treference; const highloc: tlocation; const name: string); override;
  80. procedure g_initialize(list : TAsmList;t : tdef;const ref : treference);override;
  81. procedure g_finalize(list : TAsmList;t : tdef;const ref : treference);override;
  82. procedure g_overflowcheck(list: TAsmList; const Loc: tlocation; def: tdef); override;
  83. procedure g_overflowCheck_loc(List:TAsmList;const Loc:TLocation;def:TDef;var ovloc : tlocation); override;
  84. procedure location_get_data_ref(list:TAsmList;def: tdef; const l:tlocation;var ref:treference;loadref:boolean; alignment: longint);override;
  85. procedure maybe_change_load_node_reg(list: TAsmList; var n: tnode; reload: boolean); override;
  86. procedure g_copyvaluepara_openarray(list: TAsmList; const ref: treference; const lenloc: tlocation; arrdef: tarraydef; destreg: tregister); override;
  87. procedure g_releasevaluepara_openarray(list: TAsmList; arrdef: tarraydef; const l: tlocation); override;
  88. procedure gen_initialize_code(list: TAsmList); override;
  89. procedure gen_entry_code(list: TAsmList); override;
  90. procedure gen_exit_code(list: TAsmList); override;
  91. { unimplemented/unnecessary routines }
  92. procedure a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; srcsize, dstsize: tdef; src, dst: tregister); override;
  93. procedure a_loadmm_loc_reg(list: TAsmList; fromsize, tosize: tdef; const loc: tlocation; const reg: tregister; shuffle: pmmshuffle); override;
  94. procedure a_loadmm_reg_reg(list: TAsmList; fromsize, tosize: tdef; reg1, reg2: tregister; shuffle: pmmshuffle); override;
  95. procedure a_loadmm_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; reg: tregister; shuffle: pmmshuffle); override;
  96. procedure a_loadmm_reg_ref(list: TAsmList; fromsize, tosize: tdef; reg: tregister; const ref: treference; shuffle: pmmshuffle); override;
  97. procedure a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size: tdef; src, dst: tregister; shuffle: pmmshuffle); override;
  98. procedure a_loadmm_intreg_reg(list: TAsmList; fromsize, tosize: tdef; intreg, mmreg: tregister; shuffle: pmmshuffle); override;
  99. procedure a_loadmm_reg_intreg(list: TAsmList; fromsize, tosize: tdef; mmreg, intreg: tregister; shuffle: pmmshuffle); override;
  100. procedure g_stackpointer_alloc(list: TAsmList; size: longint); override;
  101. procedure g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint); override;
  102. procedure g_adjust_self_value(list: TAsmList; procdef: tprocdef; ioffset: aint); override;
  103. procedure g_local_unwind(list: TAsmList; l: TAsmLabel); override;
  104. { Wasm-specific routines }
  105. procedure a_load_stack_reg(list : TAsmList;size: tdef;reg: tregister);
  106. { extra_slots are the slots that are used by the reference, and that
  107. will be removed by the store operation }
  108. procedure a_load_stack_ref(list : TAsmList;size: tdef;const ref: treference;extra_slots: longint);
  109. procedure a_load_reg_stack(list : TAsmList;size: tdef;reg: tregister);
  110. { extra_slots are the slots that are used by the reference, and that
  111. will be removed by the load operation }
  112. procedure a_load_ref_stack(list : TAsmList;size: tdef;const ref: treference;extra_slots: longint);
  113. procedure a_load_const_stack(list : TAsmList;size: tdef;a :tcgint; typ: TRegisterType);
  114. procedure a_load_stack_loc(list : TAsmList;size: tdef;const loc: tlocation);
  115. procedure a_load_loc_stack(list : TAsmList;size: tdef;const loc: tlocation);
  116. procedure a_loadfpu_const_stack(list : TAsmList;size: tdef;a :double);
  117. procedure a_op_stack(list : TAsmList;op: topcg; size: tdef; trunc32: boolean);
  118. procedure a_op_const_stack(list : TAsmList;op: topcg; size: tdef;a : tcgint);
  119. procedure a_op_reg_stack(list : TAsmList;op: topcg; size: tdef;reg: tregister);
  120. procedure a_op_ref_stack(list : TAsmList;op: topcg; size: tdef;const ref: treference);
  121. procedure a_op_loc_stack(list : TAsmList;op: topcg; size: tdef;const loc: tlocation);
  122. procedure g_reference_loc(list: TAsmList; def: tdef; const fromloc: tlocation; out toloc: tlocation); override;
  123. { assumes that initdim dimensions have already been pushed on the
  124. evaluation stack, and creates a new array of type arrdef with these
  125. dimensions }
  126. procedure g_newarray(list : TAsmList; arrdef: tdef; initdim: longint);
  127. { gets the length of the array whose reference is stored in arrloc,
  128. and puts it on the evaluation stack }
  129. procedure g_getarraylen(list : TAsmList; const arrloc: tlocation);
  130. { this routine expects that all values are already massaged into the
  131. required form (sign bits xor'ed for gt/lt comparisons for OS_32/OS_64,
  132. see http://stackoverflow.com/questions/4068973/c-performing-signed-comparison-in-unsigned-variables-without-casting ) }
  133. procedure a_cmp_stack_label(list : TAsmlist; size: tdef; cmp_op: topcmp; lab: tasmlabel);
  134. { these 2 routines perform the massaging expected by the previous one }
  135. procedure maybe_adjust_cmp_stackval(list : TAsmlist; size: tdef; cmp_op: topcmp);
  136. function maybe_adjust_cmp_constval(size: tdef; cmp_op: topcmp; a: tcgint): tcgint;
  137. { truncate/sign extend after performing operations on values < 32 bit
  138. that may have overflowed outside the range }
  139. procedure maybe_adjust_op_result(list: TAsmList; op: TOpCg; size: tdef);
  140. { performs sign/zero extension as required }
  141. procedure resize_stack_int_val(list: TAsmList;fromsize,tosize: tdef; formemstore: boolean);
  142. { 8/16 bit unsigned parameters and return values must be sign-extended on
  143. the producer side, because the JVM does not support unsigned variants;
  144. then they have to be zero-extended again on the consumer side }
  145. procedure maybe_resize_stack_para_val(list: TAsmList; retdef: tdef; callside: boolean);
  146. { adjust the stack height after a call based on the specified number of
  147. slots used for parameters and the provided resultdef }
  148. procedure g_adjust_stack_after_call(list: TAsmList; pd: tabstractprocdef; paraheight: longint; forceresdef: tdef);
  149. property maxevalstackheight: longint read fmaxevalstackheight;
  150. procedure gen_initialize_fields_code(list:TAsmList);
  151. procedure gen_typecheck(list: TAsmList; checkop: tasmop; checkdef: tdef);
  152. protected
  153. procedure a_load_const_stack_intern(list : TAsmList;size : tdef;a : tcgint; typ: TRegisterType; legalize_const: boolean);
  154. function get_enum_init_val_ref(def: tdef; out ref: treference): boolean;
  155. procedure allocate_implicit_structs_for_st_with_base_ref(list: TAsmList; st: tsymtable; const ref: treference; allocvartyp: tsymtyp);
  156. procedure allocate_enum_with_base_ref(list: TAsmList; vs: tabstractvarsym; const initref: treference; destbaseref: treference);
  157. procedure allocate_implicit_struct_with_base_ref(list: TAsmList; vs: tabstractvarsym; ref: treference);
  158. procedure gen_load_uninitialized_function_result(list: TAsmList; pd: tprocdef; resdef: tdef; const resloc: tcgpara); override;
  159. procedure g_copyvalueparas(p: TObject; arg: pointer); override;
  160. procedure inittempvariables(list:TAsmList);override;
  161. function g_call_system_proc_intern(list: TAsmList; pd: tprocdef; const paras: array of pcgpara; forceresdef: tdef): tcgpara; override;
  162. { in case of an array, the array base address and index have to be
  163. put on the evaluation stack before the stored value; similarly, for
  164. fields the self pointer has to be loaded first. Also checks whether
  165. the reference is valid. If dup is true, the necessary values are stored
  166. twice. Returns how many stack slots have been consumed, disregarding
  167. the "dup". }
  168. function prepare_stack_for_ref(list: TAsmList; const ref: treference; dup: boolean): longint;
  169. { return the load/store opcode to load/store from/to ref; if the result
  170. has to be and'ed after a load to get the final value, that constant
  171. is returned in finishandval (otherwise that value is set to -1) }
  172. function loadstoreopcref(def: tdef; isload: boolean; const ref: treference; out finishandval: tcgint): tasmop;
  173. { return the load/store opcode to load/store from/to reg; if the result
  174. has to be and'ed after a load to get the final value, that constant
  175. is returned in finishandval (otherwise that value is set to -1) }
  176. function loadstoreopc(def: tdef; isload, isarray: boolean; out finishandval: tcgint): tasmop;
  177. procedure resizestackfpuval(list: TAsmList; fromsize, tosize: tcgsize);
  178. { in case of an OS_32 OP_DIV, we have to use an OS_S64 OP_IDIV because the
  179. JVM does not support unsigned divisions }
  180. procedure maybepreparedivu32(list: TAsmList; var op: topcg; size: tdef; out isdivu32: boolean);
  181. { common implementation of a_call_* }
  182. function a_call_name_intern(list : TAsmList;pd : tprocdef;const s : TSymStr; forceresdef: tdef; inheritedcall: boolean): tcgpara;
  183. { concatcopy helpers }
  184. procedure concatcopy_normal_array(list: TAsmList; size: tdef; const source, dest: treference);
  185. procedure concatcopy_record(list: TAsmList; size: tdef; const source, dest: treference);
  186. procedure concatcopy_set(list: TAsmList; size: tdef; const source, dest: treference);
  187. procedure concatcopy_shortstring(list: TAsmList; size: tdef; const source, dest: treference);
  188. procedure paravarsym_set_initialloc_to_paraloc(vs: tparavarsym); override;
  189. end;
  190. implementation
  191. uses
  192. verbose,cutils,globals,fmodule,constexp,
  193. defutil,
  194. aasmtai,aasmcpu,
  195. symtable,symcpu, wasmdef,
  196. procinfo,cpuinfo,cgcpu,tgobj;
  197. const
  198. TOpCG2IAsmOp : array[topcg] of TAsmOp=(
  199. A_None, {OP_NONE}
  200. A_None, {OP_MOVE, replaced operation with direct load }
  201. a_i32_add, {OP_ADD, simple addition }
  202. a_i32_and, {OP_AND, simple logical and }
  203. a_i32_div_u, {OP_DIV, simple unsigned division }
  204. a_i32_div_s, {OP_IDIV, simple signed division }
  205. a_i32_mul, {OP_IMUL, simple signed multiply }
  206. a_i32_mul, {OP_MUL, simple unsigned multiply }
  207. A_None, {OP_NEG, simple negate } // neg = xor + 1
  208. A_None, {OP_NOT, simple logical not } // not = xor - 1
  209. a_i32_or, {OP_OR, simple logical or }
  210. a_i32_shr_s, {OP_SAR, arithmetic shift-right }
  211. a_i32_shl, {OP_SHL, logical shift left }
  212. a_i32_shr_u, {OP_SHR, logical shift right }
  213. a_i32_sub, {OP_SUB, simple subtraction }
  214. a_i32_xor, {OP_XOR, simple exclusive or }
  215. a_i32_rotl, {OP_ROL, rotate left }
  216. a_i32_rotr {OP_ROR rotate right }
  217. );
  218. TOpCG2LAsmOp : array[topcg] of TAsmOp=(
  219. A_None, {OP_NONE}
  220. a_i64_load, {OP_MOVE, replaced operation with direct load }
  221. a_i64_add, {OP_ADD, simple addition }
  222. a_i64_and, {OP_AND, simple logical and }
  223. a_i64_div_u, {OP_DIV, simple unsigned division }
  224. a_i64_div_s, {OP_IDIV, simple signed division }
  225. a_i64_mul, {OP_IMUL, simple signed multiply }
  226. a_i64_mul, {OP_MUL, simple unsigned multiply }
  227. A_None, {OP_NEG, simple negate } // neg = xor + 1
  228. A_None, {OP_NOT, simple logical not } // not = xor - 1
  229. a_i64_or, {OP_OR, simple logical or }
  230. a_i64_shr_s, {OP_SAR, arithmetic shift-right }
  231. a_i64_shl, {OP_SHL, logical shift left }
  232. a_i64_shr_u, {OP_SHR, logical shift right }
  233. a_i64_sub, {OP_SUB, simple subtraction }
  234. a_i64_xor, {OP_XOR, simple exclusive or }
  235. a_i64_rotl, {OP_ROL, rotate left }
  236. a_i64_rotr {OP_ROR rotate right }
  237. );
  238. constructor thlcgwasm.create;
  239. begin
  240. fevalstackheight:=0;
  241. fmaxevalstackheight:=0;
  242. end;
  243. procedure thlcgwasm.incblock;
  244. begin
  245. inc(blocks);
  246. end;
  247. procedure thlcgwasm.decblock;
  248. begin
  249. dec(blocks);
  250. if blocks<0 then Internalerror(2019091807); // out of block
  251. end;
  252. procedure thlcgwasm.incstack(list: TAsmList; slots: longint);
  253. begin
  254. if slots=0 then
  255. exit;
  256. inc(fevalstackheight,slots);
  257. if (fevalstackheight>fmaxevalstackheight) then
  258. fmaxevalstackheight:=fevalstackheight;
  259. if cs_asm_regalloc in current_settings.globalswitches then
  260. list.concat(tai_comment.Create(strpnew(' allocated '+tostr(slots)+', stack height = '+tostr(fevalstackheight))));
  261. end;
  262. procedure thlcgwasm.decstack(list: TAsmList;slots: longint);
  263. begin
  264. if slots=0 then
  265. exit;
  266. dec(fevalstackheight,slots);
  267. if (fevalstackheight<0) and
  268. not(cs_no_regalloc in current_settings.globalswitches) then
  269. internalerror(2010120501);
  270. if cs_asm_regalloc in current_settings.globalswitches then
  271. list.concat(tai_comment.Create(strpnew(' freed '+tostr(slots)+', stack height = '+tostr(fevalstackheight))));
  272. end;
  273. class function thlcgwasm.def2regtyp(def: tdef): tregistertype;
  274. begin
  275. case def.typ of
  276. { records (including files) and enums are implemented via classes }
  277. recorddef,
  278. filedef,
  279. enumdef,
  280. setdef:
  281. result:=R_ADDRESSREGISTER;
  282. { shortstrings are implemented via classes }
  283. else if is_shortstring(def) or
  284. { voiddef can only be typecasted into (implicit) pointers }
  285. is_void(def) then
  286. result:=R_ADDRESSREGISTER
  287. else
  288. result:=inherited;
  289. end;
  290. end;
  291. procedure thlcgwasm.a_load_const_cgpara(list: TAsmList; tosize: tdef; a: tcgint; const cgpara: TCGPara);
  292. begin
  293. tosize:=get_para_push_size(tosize);
  294. if tosize=s8inttype then
  295. a:=shortint(a)
  296. else if tosize=s16inttype then
  297. a:=smallint(a);
  298. inherited a_load_const_cgpara(list, tosize, a, cgpara);
  299. end;
  300. function thlcgwasm.a_call_name(list: TAsmList; pd: tprocdef; const s: TSymStr; const paras: array of pcgpara; forceresdef: tdef; weak: boolean): tcgpara;
  301. begin
  302. result:=a_call_name_intern(list,pd,s,forceresdef,false);
  303. end;
  304. function thlcgwasm.a_call_name_inherited(list: TAsmList; pd: tprocdef; const s: TSymStr; const paras: array of pcgpara): tcgpara;
  305. begin
  306. result:=a_call_name_intern(list,pd,s,nil,true);
  307. end;
  308. function thlcgwasm.a_call_reg(list: TAsmList; pd: tabstractprocdef; reg: tregister; const paras: array of pcgpara): tcgpara;
  309. begin
  310. internalerror(2012042824);
  311. result.init;
  312. end;
  313. procedure thlcgwasm.a_load_const_stack_intern(list : TAsmList;size : tdef;a : tcgint; typ: TRegisterType; legalize_const: boolean);
  314. begin
  315. if legalize_const and
  316. (typ=R_INTREGISTER) and
  317. (size.typ=orddef) then
  318. begin
  319. { uses specific byte/short array store instructions, and the Dalvik
  320. VM does not like it if we store values outside the range }
  321. case torddef(size).ordtype of
  322. u8bit:
  323. a:=shortint(a);
  324. u16bit:
  325. a:=smallint(a);
  326. else
  327. ;
  328. end;
  329. end;
  330. a_load_const_stack(list,size,a,typ);
  331. end;
  332. procedure thlcgwasm.a_load_const_stack(list : TAsmList;size : tdef;a : tcgint; typ: TRegisterType);
  333. begin
  334. case typ of
  335. R_INTREGISTER:
  336. begin
  337. case def_cgsize(size) of
  338. OS_8,OS_16,OS_32,
  339. OS_S8,OS_S16,OS_S32:
  340. begin
  341. { convert cardinals to longints }
  342. list.concat(taicpu.op_const(a_i32_const, a));
  343. end;
  344. OS_64,OS_S64:
  345. begin
  346. list.concat(taicpu.op_const(a_i64_const, a));
  347. incstack(list,1); // why?
  348. end;
  349. else
  350. internalerror(2010110702);
  351. end;
  352. end;
  353. R_ADDRESSREGISTER:
  354. begin
  355. if a<>0 then
  356. internalerror(2010110701);
  357. list.concat(taicpu.op_none(a_none));
  358. end;
  359. else
  360. internalerror(2010110703);
  361. end;
  362. incstack(list,1);
  363. end;
  364. procedure thlcgwasm.a_load_stack_loc(list: TAsmList; size: tdef; const loc: tlocation);
  365. begin
  366. case loc.loc of
  367. LOC_REGISTER,LOC_CREGISTER,
  368. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  369. a_load_stack_reg(list,size,loc.register);
  370. LOC_REFERENCE:
  371. a_load_stack_ref(list,size,loc.reference,prepare_stack_for_ref(list,loc.reference,false));
  372. else
  373. internalerror(2011020501);
  374. end;
  375. end;
  376. procedure thlcgwasm.a_load_loc_stack(list: TAsmList;size: tdef;const loc: tlocation);
  377. begin
  378. case loc.loc of
  379. LOC_REGISTER,LOC_CREGISTER,
  380. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  381. a_load_reg_stack(list,size,loc.register);
  382. LOC_REFERENCE,LOC_CREFERENCE:
  383. a_load_ref_stack(list,size,loc.reference,prepare_stack_for_ref(list,loc.reference,false));
  384. LOC_CONSTANT:
  385. a_load_const_stack(list,size,loc.value,def2regtyp(size));
  386. else
  387. internalerror(2011010401);
  388. end;
  389. end;
  390. procedure thlcgwasm.a_loadfpu_const_stack(list: TAsmList; size: tdef; a: double);
  391. begin
  392. case tfloatdef(size).floattype of
  393. s32real:
  394. begin
  395. list.concat(taicpu.op_single(a_f32_const, a));
  396. incstack(list,1);
  397. end;
  398. s64real:
  399. begin
  400. list.concat(taicpu.op_double(a_f64_const,a));
  401. incstack(list,2);
  402. end
  403. else
  404. internalerror(2011010501);
  405. end;
  406. end;
  407. procedure thlcgwasm.a_op_stack(list: TAsmList; op: topcg; size: tdef; trunc32: boolean);
  408. var
  409. cgsize: tcgsize;
  410. begin
  411. if not trunc32 then
  412. cgsize:=def_cgsize(size)
  413. else
  414. begin
  415. resize_stack_int_val(list,u32inttype,s64inttype,false);
  416. cgsize:=OS_S64;
  417. end;
  418. case cgsize of
  419. OS_8,OS_S8,
  420. OS_16,OS_S16,
  421. OS_32,OS_S32:
  422. begin
  423. { not = xor 1 for boolean, xor -1 for the rest}
  424. if op=OP_NOT then
  425. begin
  426. if not is_pasbool(size) then
  427. a_load_const_stack(list,s32inttype,high(cardinal),R_INTREGISTER)
  428. else
  429. a_load_const_stack(list,size,1,R_INTREGISTER);
  430. op:=OP_XOR;
  431. end;
  432. if TOpCG2IAsmOp[op]=A_None then
  433. internalerror(2010120532);
  434. list.concat(taicpu.op_none(TOpCG2IAsmOp[op]));
  435. maybe_adjust_op_result(list,op,size);
  436. if op<>OP_NEG then
  437. decstack(list,1);
  438. end;
  439. OS_64,OS_S64:
  440. begin
  441. { unsigned 64 bit division must be done via a helper }
  442. if op=OP_DIV then
  443. internalerror(2010120530);
  444. { not = xor 1 for boolean, xor -1 for the rest}
  445. if op=OP_NOT then
  446. begin
  447. if not is_pasbool(size) then
  448. a_load_const_stack(list,s64inttype,-1,R_INTREGISTER)
  449. else
  450. a_load_const_stack(list,s64inttype,1,R_INTREGISTER);
  451. op:=OP_XOR;
  452. end;
  453. if TOpCG2LAsmOp[op]=A_None then
  454. internalerror(2010120533);
  455. list.concat(taicpu.op_none(TOpCG2LAsmOp[op]));
  456. case op of
  457. OP_NOT,
  458. OP_NEG:
  459. ;
  460. { the second argument here is an int rather than a long }
  461. OP_SHL,OP_SHR,OP_SAR:
  462. decstack(list,1);
  463. else
  464. decstack(list,2);
  465. end;
  466. end;
  467. else
  468. internalerror(2010120531);
  469. end;
  470. if trunc32 then
  471. begin
  472. list.concat(taicpu.op_none(a_i32_trunc_s_f32)); // todo: there are several truncs
  473. decstack(list,1);
  474. end;
  475. end;
  476. procedure thlcgwasm.a_op_const_stack(list: TAsmList;op: topcg;size: tdef;a: tcgint);
  477. var
  478. trunc32: boolean;
  479. begin
  480. maybepreparedivu32(list,op,size,trunc32);
  481. case op of
  482. OP_NEG,OP_NOT:
  483. internalerror(2011010801);
  484. OP_SHL,OP_SHR,OP_SAR:
  485. { the second argument here is an int rather than a long }
  486. a_load_const_stack(list,s32inttype,a,R_INTREGISTER);
  487. else
  488. a_load_const_stack(list,size,a,R_INTREGISTER);
  489. end;
  490. a_op_stack(list,op,size,trunc32);
  491. end;
  492. procedure thlcgwasm.a_op_reg_stack(list: TAsmList; op: topcg; size: tdef; reg: tregister);
  493. var
  494. trunc32: boolean;
  495. begin
  496. maybepreparedivu32(list,op,size,trunc32);
  497. case op of
  498. OP_SHL,OP_SHR,OP_SAR:
  499. if not is_64bitint(size) then
  500. a_load_reg_stack(list,size,reg)
  501. else
  502. begin
  503. { the second argument here is an int rather than a long }
  504. if getsubreg(reg)=R_SUBQ then
  505. internalerror(2011010802);
  506. a_load_reg_stack(list,s32inttype,reg)
  507. end
  508. else
  509. a_load_reg_stack(list,size,reg);
  510. end;
  511. a_op_stack(list,op,size,trunc32);
  512. end;
  513. procedure thlcgwasm.a_op_ref_stack(list: TAsmList; op: topcg; size: tdef; const ref: treference);
  514. var
  515. trunc32: boolean;
  516. begin
  517. { ref must not be the stack top, because that may indicate an error
  518. (it means that we will perform an operation of the stack top onto
  519. itself, so that means the two values have been loaded manually prior
  520. to calling this routine, instead of letting this routine load one of
  521. them; if something like that is needed, call a_op_stack() directly) }
  522. if ref.base=NR_EVAL_STACK_BASE then
  523. internalerror(2010121102);
  524. maybepreparedivu32(list,op,size,trunc32);
  525. case op of
  526. OP_SHL,OP_SHR,OP_SAR:
  527. begin
  528. if not is_64bitint(size) then
  529. a_load_ref_stack(list,size,ref,prepare_stack_for_ref(list,ref,false))
  530. else
  531. a_load_ref_stack(list,s32inttype,ref,prepare_stack_for_ref(list,ref,false));
  532. end;
  533. else
  534. a_load_ref_stack(list,size,ref,prepare_stack_for_ref(list,ref,false));
  535. end;
  536. a_op_stack(list,op,size,trunc32);
  537. end;
  538. procedure thlcgwasm.a_op_loc_stack(list: TAsmList; op: topcg; size: tdef; const loc: tlocation);
  539. begin
  540. case loc.loc of
  541. LOC_REGISTER,LOC_CREGISTER:
  542. a_op_reg_stack(list,op,size,loc.register);
  543. LOC_REFERENCE,LOC_CREFERENCE:
  544. a_op_ref_stack(list,op,size,loc.reference);
  545. LOC_CONSTANT:
  546. a_op_const_stack(list,op,size,loc.value);
  547. else
  548. internalerror(2011011415)
  549. end;
  550. end;
  551. procedure thlcgwasm.g_reference_loc(list: TAsmList; def: tdef; const fromloc: tlocation; out toloc: tlocation);
  552. begin
  553. case fromloc.loc of
  554. LOC_CREFERENCE,
  555. LOC_REFERENCE:
  556. begin
  557. toloc:=fromloc;
  558. if (fromloc.reference.base<>NR_NO) and
  559. (fromloc.reference.base<>current_procinfo.framepointer) and
  560. (fromloc.reference.base<>NR_STACK_POINTER_REG) then
  561. g_allocload_reg_reg(list,voidpointertype,fromloc.reference.base,toloc.reference.base,R_ADDRESSREGISTER);
  562. end;
  563. else
  564. inherited;
  565. end;
  566. end;
  567. procedure thlcgwasm.g_newarray(list: TAsmList; arrdef: tdef; initdim: longint);
  568. var
  569. recref,
  570. enuminitref: treference;
  571. elemdef: tdef;
  572. i: longint;
  573. mangledname: string;
  574. opc: tasmop;
  575. primitivetype: boolean;
  576. begin
  577. internalerror(2019083001); // arrays are note yet supported
  578. (*
  579. elemdef:=arrdef;
  580. if initdim>1 then
  581. begin
  582. { multianewarray typedesc ndim }
  583. { todo: WASM
  584. list.concat(taicpu.op_sym_const(a_multianewarray,
  585. current_asmdata.RefAsmSymbol(jvmarrtype(elemdef,primitivetype),AT_METADATA),initdim));
  586. }
  587. { has to be a multi-dimensional array type }
  588. if primitivetype then
  589. internalerror(2011012207);
  590. end
  591. else
  592. begin
  593. { for primitive types:
  594. newarray typedesc
  595. for reference types:
  596. anewarray typedesc
  597. }
  598. { get the type of the elements of the array we are creating }
  599. elemdef:=tarraydef(arrdef).elementdef;
  600. { todo: WASM. Todo: array data structures needs to be stored in Memory
  601. mangledname:=jvmarrtype(elemdef,primitivetype);
  602. if primitivetype then
  603. opc:=a_newarray
  604. else
  605. opc:=a_anewarray;
  606. list.concat(taicpu.op_sym(opc,current_asmdata.RefAsmSymbol(mangledname,AT_METADATA)));
  607. }
  608. end;
  609. { all dimensions are removed from the stack, an array reference is
  610. added }
  611. decstack(list,initdim-1);
  612. { in case of an array of records, sets or shortstrings, initialise }
  613. elemdef:=tarraydef(arrdef).elementdef;
  614. for i:=1 to pred(initdim) do
  615. elemdef:=tarraydef(elemdef).elementdef;
  616. if (elemdef.typ in [recorddef,setdef]) or
  617. ((elemdef.typ=enumdef) and
  618. get_enum_init_val_ref(elemdef,enuminitref)) or
  619. is_shortstring(elemdef) or
  620. ((elemdef.typ=procvardef) and
  621. not tprocvardef(elemdef).is_addressonly) or
  622. is_ansistring(elemdef) or
  623. is_wide_or_unicode_string(elemdef) or
  624. is_dynamic_array(elemdef) then
  625. begin
  626. { duplicate array instance }
  627. list.concat(taicpu.op_none(a_dup));
  628. incstack(list,1);
  629. a_load_const_stack(list,s32inttype,initdim-1,R_INTREGISTER);
  630. case elemdef.typ of
  631. arraydef:
  632. g_call_system_proc(list,'fpc_initialize_array_dynarr',[],nil);
  633. recorddef,setdef,procvardef:
  634. begin
  635. tg.gethltemp(list,elemdef,elemdef.size,tt_persistent,recref);
  636. a_load_ref_stack(list,elemdef,recref,prepare_stack_for_ref(list,recref,false));
  637. case elemdef.typ of
  638. recorddef:
  639. g_call_system_proc(list,'fpc_initialize_array_record',[],nil);
  640. setdef:
  641. begin
  642. if tsetdef(elemdef).elementdef.typ=enumdef then
  643. g_call_system_proc(list,'fpc_initialize_array_enumset',[],nil)
  644. else
  645. g_call_system_proc(list,'fpc_initialize_array_bitset',[],nil)
  646. end;
  647. procvardef:
  648. g_call_system_proc(list,'fpc_initialize_array_procvar',[],nil);
  649. else
  650. internalerror(2019051025);
  651. end;
  652. tg.ungettemp(list,recref);
  653. end;
  654. enumdef:
  655. begin
  656. a_load_ref_stack(list,java_jlobject,enuminitref,prepare_stack_for_ref(list,enuminitref,false));
  657. g_call_system_proc(list,'fpc_initialize_array_object',[],nil);
  658. end;
  659. stringdef:
  660. begin
  661. case tstringdef(elemdef).stringtype of
  662. st_shortstring:
  663. begin
  664. a_load_const_stack_intern(list,u8inttype,tstringdef(elemdef).len,R_INTREGISTER,true);
  665. g_call_system_proc(list,'fpc_initialize_array_shortstring',[],nil);
  666. end;
  667. st_ansistring:
  668. g_call_system_proc(list,'fpc_initialize_array_ansistring',[],nil);
  669. st_unicodestring,
  670. st_widestring:
  671. g_call_system_proc(list,'fpc_initialize_array_unicodestring',[],nil);
  672. else
  673. internalerror(2011081801);
  674. end;
  675. end;
  676. else
  677. internalerror(2011081801);
  678. end;
  679. end;
  680. *)
  681. end;
  682. procedure thlcgwasm.g_getarraylen(list: TAsmList; const arrloc: tlocation);
  683. var
  684. nillab,endlab: tasmlabel;
  685. begin
  686. internalerror(2019083001); // arrays are note yet supported
  687. (*
  688. { inline because we have to use the arraylength opcode, which
  689. cannot be represented directly in Pascal. Even though the JVM
  690. supports allocated arrays with length=0, we still also have to
  691. check for nil pointers because even if FPC always generates
  692. allocated empty arrays under all circumstances, external Java
  693. code could pass in nil pointers.
  694. Note that this means that assigned(arr) can be different from
  695. length(arr)<>0 for dynamic arrays when targeting the JVM.
  696. }
  697. current_asmdata.getjumplabel(nillab);
  698. current_asmdata.getjumplabel(endlab);
  699. { if assigned(arr) ... }
  700. a_load_loc_stack(list,java_jlobject,arrloc);
  701. list.concat(taicpu.op_none(a_dup));
  702. incstack(list,1);
  703. list.concat(taicpu.op_sym(a_ifnull,nillab));
  704. decstack(list,1);
  705. { ... then result:=arraylength(arr) ... }
  706. list.concat(taicpu.op_none(a_arraylength));
  707. a_jmp_always(list,endlab);
  708. { ... else result:=0 }
  709. a_label(list,nillab);
  710. list.concat(taicpu.op_none(a_pop));
  711. decstack(list,1);
  712. list.concat(taicpu.op_none(a_iconst_0));
  713. incstack(list,1);
  714. a_label(list,endlab);
  715. *)
  716. end;
  717. procedure thlcgwasm.a_cmp_stack_label(list: TAsmlist; size: tdef; cmp_op: topcmp; lab: tasmlabel);
  718. const
  719. opcmp32: array[topcmp] of tasmop = (
  720. A_None, { OC_NONE, }
  721. a_i32_eq, { OC_EQ, equality comparison }
  722. a_i32_gt_s, { OC_GT, greater than (signed) }
  723. a_i32_lt_s, { OC_LT, less than (signed) }
  724. a_i32_ge_s, { OC_GTE, greater or equal than (signed) }
  725. a_i32_le_s, { OC_LTE, less or equal than (signed) }
  726. a_i32_ne, { OC_NE, not equal }
  727. a_i32_le_u, { OC_BE, less or equal than (unsigned) }
  728. a_i32_lt_u, { OC_B, less than (unsigned) }
  729. a_i32_ge_u, { OC_AE, greater or equal than (unsigned) }
  730. a_i32_gt_u { OC_A greater than (unsigned) }
  731. );
  732. const
  733. opcmp64: array[TOpCmp] of TAsmOp = (A_None,
  734. a_i64_eq, // OC_EQ
  735. a_i64_gt_s, a_i64_lt_s, // OC_GT, OC_LT
  736. a_i64_ge_s, a_i64_le_s, // OC_GTE, OC_LTE
  737. a_i64_ne, // OC_NE
  738. a_i64_le_u, a_i64_lt_u, // OC_BE, OC_B
  739. a_i64_ge_u, a_i64_gt_u // OC_AE, OC_A
  740. );
  741. var
  742. cgsize: tcgsize;
  743. begin
  744. // WASM doesn't have compare+jump (to label) operation
  745. // thus even though this is a_cmp_stack_label()
  746. // label operrand is ommited
  747. //
  748. // todo: it should NOT be ommitted when we're leaving a block
  749. // (i.e. Exit or break or continue operators)
  750. case def2regtyp(size) of
  751. R_INTREGISTER:
  752. begin
  753. cgsize:=def_cgsize(size);
  754. case cgsize of
  755. OS_S8,OS_8,
  756. OS_16,OS_S16,
  757. OS_S32,OS_32:
  758. begin
  759. //list.concat(taicpu.op_sym(opcmp32[cmp_op],lab));
  760. list.concat(taicpu.op_none(opcmp32[cmp_op]));
  761. decstack(list,2);
  762. end;
  763. OS_64,OS_S64:
  764. begin
  765. //list.concat(taicpu.op_none(a_lcmp));
  766. //decstack(list,3);
  767. //list.concat(taicpu.op_sym(opcmp64[cmp_op],lab));
  768. list.concat(taicpu.op_none(opcmp64[cmp_op]));
  769. decstack(list,2);
  770. end;
  771. else
  772. internalerror(2010120538);
  773. end;
  774. end;
  775. R_ADDRESSREGISTER:
  776. begin
  777. case cmp_op of
  778. OC_EQ:
  779. //list.concat(taicpu.op_sym(a_i64_eq,lab));
  780. list.concat(taicpu.op_none(a_i32_eq));
  781. OC_NE:
  782. //list.concat(taicpu.op_sym(a_i64_ne,lab));
  783. list.concat(taicpu.op_none(a_i32_ne));
  784. else
  785. internalerror(2010120537);
  786. end;
  787. decstack(list,2);
  788. end;
  789. else
  790. internalerror(2010120538);
  791. end;
  792. end;
  793. procedure thlcgwasm.maybe_adjust_cmp_stackval(list: TAsmlist; size: tdef; cmp_op: topcmp);
  794. begin
  795. { use cmp_op because eventually that's what indicates the
  796. signed/unsigned character of the operation, not the size... }
  797. if (cmp_op in [OC_EQ,OC_NE,OC_LT,OC_LTE,OC_GT,OC_GTE]) or
  798. (def2regtyp(size)<>R_INTREGISTER) then
  799. exit;
  800. { http://stackoverflow.com/questions/4068973/c-performing-signed-comparison-in-unsigned-variables-without-casting }
  801. case def_cgsize(size) of
  802. OS_32,OS_S32:
  803. a_op_const_stack(list,OP_XOR,size,cardinal($80000000));
  804. OS_64,OS_S64:
  805. a_op_const_stack(list,OP_XOR,size,tcgint($8000000000000000));
  806. else
  807. ;
  808. end;
  809. end;
  810. function thlcgwasm.maybe_adjust_cmp_constval(size: tdef; cmp_op: topcmp; a: tcgint): tcgint;
  811. begin
  812. result:=a;
  813. { use cmp_op because eventually that's what indicates the
  814. signed/unsigned character of the operation, not the size... }
  815. if (cmp_op in [OC_EQ,OC_NE,OC_LT,OC_LTE,OC_GT,OC_GTE]) or
  816. (def2regtyp(size)<>R_INTREGISTER) then
  817. exit;
  818. case def_cgsize(size) of
  819. OS_32,OS_S32:
  820. result:=a xor cardinal($80000000);
  821. OS_64,OS_S64:
  822. {$push}{$r-}
  823. result:=a xor tcgint($8000000000000000);
  824. {$pop}
  825. else
  826. ;
  827. end;
  828. end;
  829. procedure thlcgwasm.maybe_adjust_op_result(list: TAsmList; op: TOpCg; size: tdef);
  830. const
  831. overflowops = [OP_MUL,OP_SHL,OP_ADD,OP_SUB,OP_NOT,OP_NEG];
  832. begin
  833. if (op in overflowops) and
  834. (def_cgsize(size) in [OS_8,OS_S8,OS_16,OS_S16]) then
  835. resize_stack_int_val(list,s32inttype,size,false);
  836. end;
  837. procedure thlcgwasm.gen_load_uninitialized_function_result(list: TAsmList; pd: tprocdef; resdef: tdef; const resloc: tcgpara);
  838. begin
  839. { constructors don't return anything in Java }
  840. if pd.proctypeoption=potype_constructor then
  841. exit;
  842. { must return a value of the correct type on the evaluation stack }
  843. case def2regtyp(resdef) of
  844. R_INTREGISTER,
  845. R_ADDRESSREGISTER:
  846. a_load_const_cgpara(list,resdef,0,resloc);
  847. R_FPUREGISTER:
  848. case tfloatdef(resdef).floattype of
  849. s32real:
  850. begin
  851. list.concat(taicpu.op_single(a_f32_const, 0));
  852. incstack(list,1);
  853. end;
  854. s64real:
  855. begin
  856. list.concat(taicpu.op_double(a_f64_const, 0));
  857. incstack(list,2);
  858. end;
  859. else
  860. internalerror(2011010302);
  861. end
  862. else
  863. internalerror(2011010301);
  864. end;
  865. end;
  866. procedure thlcgwasm.g_copyvalueparas(p: TObject; arg: pointer);
  867. var
  868. list: tasmlist;
  869. tmpref: treference;
  870. begin
  871. { zero-extend < 32 bit primitive types (FPC can zero-extend when calling,
  872. but that doesn't help when we're called from Java code or indirectly
  873. as a procvar -- exceptions: widechar (Java-specific type) and ordinal
  874. types whose upper bound does not set the sign bit }
  875. if (tsym(p).typ=paravarsym) and
  876. (tparavarsym(p).varspez in [vs_value,vs_const]) and
  877. (tparavarsym(p).vardef.typ=orddef) and
  878. not is_pasbool(tparavarsym(p).vardef) and
  879. not is_widechar(tparavarsym(p).vardef) and
  880. (tparavarsym(p).vardef.size<4) and
  881. not is_signed(tparavarsym(p).vardef) and
  882. (torddef(tparavarsym(p).vardef).high>=(1 shl (tparavarsym(p).vardef.size*8-1))) then
  883. begin
  884. list:=TAsmList(arg);
  885. { store value in new location to keep Android verifier happy }
  886. tg.gethltemp(list,tparavarsym(p).vardef,tparavarsym(p).vardef.size,tt_persistent,tmpref);
  887. a_load_loc_stack(list,tparavarsym(p).vardef,tparavarsym(p).initialloc);
  888. a_op_const_stack(list,OP_AND,tparavarsym(p).vardef,(1 shl (tparavarsym(p).vardef.size*8))-1);
  889. a_load_stack_ref(list,tparavarsym(p).vardef,tmpref,prepare_stack_for_ref(list,tmpref,false));
  890. location_reset_ref(tparavarsym(p).localloc,LOC_REFERENCE,def_cgsize(tparavarsym(p).vardef),4,tmpref.volatility);
  891. tparavarsym(p).localloc.reference:=tmpref;
  892. end;
  893. inherited g_copyvalueparas(p, arg);
  894. end;
  895. procedure thlcgwasm.inittempvariables(list: TAsmList);
  896. begin
  897. { these are automatically initialised when allocated if necessary }
  898. end;
  899. function thlcgwasm.g_call_system_proc_intern(list: TAsmList; pd: tprocdef; const paras: array of pcgpara; forceresdef: tdef): tcgpara;
  900. begin
  901. result:=inherited;
  902. pd.init_paraloc_info(callerside);
  903. g_adjust_stack_after_call(list,pd,pd.callerargareasize,forceresdef);
  904. end;
  905. function thlcgwasm.prepare_stack_for_ref(list: TAsmList; const ref: treference; dup: boolean): longint;
  906. var
  907. href: treference;
  908. begin
  909. result:=0;
  910. { fake location that indicates the value is already on the stack? }
  911. if (ref.base=NR_EVAL_STACK_BASE) or (ref.islocal) then
  912. exit;
  913. // setting up memory offset
  914. if assigned(ref.symbol) then
  915. list.Concat(taicpu.op_sym(a_get_global, ref.symbol))
  916. else if ref.index <> NR_NO then // array access
  917. begin
  918. // it's just faster to sum two of those together
  919. list.Concat(taicpu.op_reg(a_get_local, ref.base));
  920. list.Concat(taicpu.op_reg(a_get_local, ref.index));
  921. list.Concat(taicpu.op_none(a_i32_add));
  922. exit;
  923. end
  924. else
  925. list.Concat(taicpu.op_const(a_i32_const, 0)); //todo: this should not be 0, this should be reference to a global "memory"
  926. if (ref.base<>NR_NO) then
  927. begin
  928. if (ref.base<>NR_STACK_POINTER_REG) then
  929. begin
  930. { regular field -> load self on the stack }
  931. a_load_reg_stack(list,voidpointertype,ref.base);
  932. if dup then
  933. begin
  934. internalerror(2019083002);
  935. //todo: add duplicate
  936. //list.concat(taicpu.op_none(a_dup));
  937. incstack(list,1);
  938. end;
  939. { field name/type encoded in symbol, no index/offset }
  940. result:=1;
  941. end
  942. else
  943. begin
  944. { local variable -> offset encoded in opcode and nothing to
  945. do here, except for checking that it's a valid reference }
  946. if assigned(ref.symbol) then
  947. internalerror(2010120523);
  948. end;
  949. end
  950. else
  951. begin
  952. { static field -> nothing to do here, except for validity check }
  953. {if not assigned(ref.symbol) or
  954. (ref.offset<>0) then
  955. begin
  956. internalerror(2010120525);
  957. end;}
  958. end;
  959. end;
  960. procedure thlcgwasm.a_load_const_reg(list: TAsmList; tosize: tdef; a: tcgint; register: tregister);
  961. begin
  962. a_load_const_stack(list,tosize,a,def2regtyp(tosize));
  963. a_load_stack_reg(list,tosize,register);
  964. end;
  965. procedure thlcgwasm.a_load_const_ref(list: TAsmList; tosize: tdef; a: tcgint; const ref: treference);
  966. var
  967. extra_slots: longint;
  968. begin
  969. extra_slots:=prepare_stack_for_ref(list,ref,false);
  970. a_load_const_stack_intern(list,tosize,a,def2regtyp(tosize),assigned(ref.symbol));
  971. a_load_stack_ref(list,tosize,ref,extra_slots);
  972. end;
  973. procedure thlcgwasm.a_load_reg_ref(list: TAsmList; fromsize, tosize: tdef; register: tregister; const ref: treference);
  974. var
  975. extra_slots: longint;
  976. begin
  977. extra_slots:=prepare_stack_for_ref(list,ref,false);
  978. a_load_reg_stack(list,fromsize,register);
  979. if def2regtyp(fromsize)=R_INTREGISTER then
  980. resize_stack_int_val(list,fromsize,tosize,assigned(ref.symbol));
  981. a_load_stack_ref(list,tosize,ref,extra_slots);
  982. end;
  983. procedure thlcgwasm.a_load_reg_reg(list: TAsmList; fromsize, tosize: tdef; reg1, reg2: tregister);
  984. begin
  985. a_load_reg_stack(list,fromsize,reg1);
  986. if def2regtyp(fromsize)=R_INTREGISTER then
  987. resize_stack_int_val(list,fromsize,tosize,false);
  988. a_load_stack_reg(list,tosize,reg2);
  989. end;
  990. procedure thlcgwasm.a_load_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; register: tregister);
  991. var
  992. extra_slots: longint;
  993. begin
  994. extra_slots:=prepare_stack_for_ref(list,ref,false);
  995. a_load_ref_stack(list,fromsize,ref,extra_slots);
  996. if def2regtyp(fromsize)=R_INTREGISTER then
  997. resize_stack_int_val(list,fromsize,tosize,false);
  998. a_load_stack_reg(list,tosize,register);
  999. end;
  1000. procedure thlcgwasm.a_load_ref_ref(list: TAsmList; fromsize, tosize: tdef; const sref: treference; const dref: treference);
  1001. var
  1002. extra_sslots,
  1003. extra_dslots: longint;
  1004. begin
  1005. { make sure the destination reference is on top, since in the end the
  1006. order has to be "destref, value" -> first create "destref, sourceref" }
  1007. extra_dslots:=prepare_stack_for_ref(list,dref,false);
  1008. extra_sslots:=prepare_stack_for_ref(list,sref,false);
  1009. a_load_ref_stack(list,fromsize,sref,extra_sslots);
  1010. if def2regtyp(fromsize)=R_INTREGISTER then
  1011. resize_stack_int_val(list,fromsize,tosize,assigned(dref.symbol));
  1012. a_load_stack_ref(list,tosize,dref,extra_dslots);
  1013. end;
  1014. procedure thlcgwasm.a_loadaddr_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; r: tregister);
  1015. begin
  1016. { only allowed for types that are not implicit pointers in Pascal (in
  1017. that case, ref contains a pointer to the actual data and we simply
  1018. return that pointer) }
  1019. if not wasmimplicitpointertype(fromsize) then
  1020. internalerror(2010120534);
  1021. a_load_ref_reg(list,ptruinttype,ptruinttype,ref,r);
  1022. end;
  1023. procedure thlcgwasm.a_op_const_reg(list: TAsmList; Op: TOpCG; size: tdef; a: tcgint; reg: TRegister);
  1024. begin
  1025. a_op_const_reg_reg(list,op,size,a,reg,reg);
  1026. end;
  1027. procedure thlcgwasm.a_op_const_reg_reg(list: TAsmList; op: TOpCg; size: tdef; a: tcgint; src, dst: tregister);
  1028. begin
  1029. a_load_reg_stack(list,size,src);
  1030. a_op_const_stack(list,op,size,a);
  1031. a_load_stack_reg(list,size,dst);
  1032. end;
  1033. procedure thlcgwasm.a_op_const_ref(list: TAsmList; Op: TOpCG; size: tdef; a: tcgint; const ref: TReference);
  1034. var
  1035. extra_slots: longint;
  1036. begin
  1037. extra_slots:=prepare_stack_for_ref(list,ref,true);
  1038. { TODO, here or in peepholeopt: use iinc when possible }
  1039. a_load_ref_stack(list,size,ref,extra_slots);
  1040. a_op_const_stack(list,op,size,a);
  1041. { for android verifier }
  1042. if (def2regtyp(size)=R_INTREGISTER) and
  1043. (assigned(ref.symbol)) then
  1044. resize_stack_int_val(list,size,size,true);
  1045. a_load_stack_ref(list,size,ref,extra_slots);
  1046. end;
  1047. procedure thlcgwasm.a_op_ref_reg(list: TAsmList; Op: TOpCG; size: tdef; const ref: TReference; reg: TRegister);
  1048. begin
  1049. if not(op in [OP_NOT,OP_NEG]) then
  1050. a_load_reg_stack(list,size,reg);
  1051. a_op_ref_stack(list,op,size,ref);
  1052. a_load_stack_reg(list,size,reg);
  1053. end;
  1054. procedure thlcgwasm.a_op_reg_reg_reg(list: TAsmList; op: TOpCg; size: tdef; src1, src2, dst: tregister);
  1055. begin
  1056. if not(op in [OP_NOT,OP_NEG]) then
  1057. a_load_reg_stack(list,size,src2);
  1058. a_op_reg_stack(list,op,size,src1);
  1059. a_load_stack_reg(list,size,dst);
  1060. end;
  1061. procedure thlcgwasm.a_op_reg_reg(list: TAsmList; Op: TOpCG; size: tdef; reg1, reg2: TRegister);
  1062. begin
  1063. a_op_reg_reg_reg(list,op,size,reg1,reg2,reg2);
  1064. end;
  1065. procedure thlcgwasm.a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tdef; a: tcgint; src, dst: tregister; setflags: boolean; var ovloc: tlocation);
  1066. var
  1067. tmpreg: tregister;
  1068. begin
  1069. if not setflags then
  1070. begin
  1071. inherited;
  1072. exit;
  1073. end;
  1074. tmpreg:=getintregister(list,size);
  1075. a_load_const_reg(list,size,a,tmpreg);
  1076. a_op_reg_reg_reg_checkoverflow(list,op,size,tmpreg,src,dst,true,ovloc);
  1077. end;
  1078. procedure thlcgwasm.a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tdef; src1, src2, dst: tregister; setflags: boolean; var ovloc: tlocation);
  1079. var
  1080. orgsrc1, orgsrc2: tregister;
  1081. docheck: boolean;
  1082. lab: tasmlabel;
  1083. begin
  1084. if not setflags then
  1085. begin
  1086. inherited;
  1087. exit;
  1088. end;
  1089. { anything else cannot overflow }
  1090. docheck:=size.size in [4,8];
  1091. if docheck then
  1092. begin
  1093. orgsrc1:=src1;
  1094. orgsrc2:=src2;
  1095. if src1=dst then
  1096. begin
  1097. orgsrc1:=getintregister(list,size);
  1098. a_load_reg_reg(list,size,size,src1,orgsrc1);
  1099. end;
  1100. if src2=dst then
  1101. begin
  1102. orgsrc2:=getintregister(list,size);
  1103. a_load_reg_reg(list,size,size,src2,orgsrc2);
  1104. end;
  1105. end;
  1106. a_op_reg_reg_reg(list,op,size,src1,src2,dst);
  1107. if docheck then
  1108. begin
  1109. { * signed overflow for addition iff
  1110. - src1 and src2 are negative and result is positive (excep in case of
  1111. subtraction, then sign of src1 has to be inverted)
  1112. - src1 and src2 are positive and result is negative
  1113. -> Simplified boolean equivalent (in terms of sign bits):
  1114. not(src1 xor src2) and (src1 xor dst)
  1115. for subtraction, multiplication: invert src1 sign bit
  1116. for division: handle separately (div by zero, low(inttype) div -1),
  1117. not supported by this code
  1118. * unsigned overflow iff carry out, aka dst < src1 or dst < src2
  1119. }
  1120. location_reset(ovloc,LOC_REGISTER,OS_S32);
  1121. { not pasbool8, because then we'd still have to convert the integer to
  1122. a boolean via branches for Dalvik}
  1123. ovloc.register:=getintregister(list,s32inttype);
  1124. if not ((size.typ=pointerdef) or
  1125. ((size.typ=orddef) and
  1126. (torddef(size).ordtype in [u64bit,u16bit,u32bit,u8bit,uchar,
  1127. pasbool1,pasbool8,pasbool16,pasbool32,pasbool64]))) then
  1128. begin
  1129. a_load_reg_stack(list,size,src1);
  1130. if op in [OP_SUB,OP_IMUL] then
  1131. a_op_stack(list,OP_NOT,size,false);
  1132. a_op_reg_stack(list,OP_XOR,size,src2);
  1133. a_op_stack(list,OP_NOT,size,false);
  1134. a_load_reg_stack(list,size,src1);
  1135. a_op_reg_stack(list,OP_XOR,size,dst);
  1136. a_op_stack(list,OP_AND,size,false);
  1137. a_op_const_stack(list,OP_SHR,size,(size.size*8)-1);
  1138. if size.size=8 then
  1139. begin
  1140. //todo: any operands needed?
  1141. list.concat(taicpu.op_none(a_i32_wrap_i64));
  1142. decstack(list,1);
  1143. end;
  1144. end
  1145. else
  1146. begin
  1147. a_load_const_stack(list,s32inttype,0,R_INTREGISTER);
  1148. current_asmdata.getjumplabel(lab);
  1149. { can be optimized by removing duplicate xor'ing to convert dst from
  1150. signed to unsigned quadrant }
  1151. a_cmp_reg_reg_label(list,size,OC_B,dst,src1,lab);
  1152. a_cmp_reg_reg_label(list,size,OC_B,dst,src2,lab);
  1153. a_op_const_stack(list,OP_XOR,s32inttype,1);
  1154. a_label(list,lab);
  1155. end;
  1156. a_load_stack_reg(list,s32inttype,ovloc.register);
  1157. end
  1158. else
  1159. ovloc.loc:=LOC_VOID;
  1160. end;
  1161. procedure thlcgwasm.a_cmp_const_ref_label(list: TAsmList; size: tdef; cmp_op: topcmp; a: tcgint; const ref: treference; l: tasmlabel);
  1162. begin
  1163. if ref.base<>NR_EVAL_STACK_BASE then
  1164. a_load_ref_stack(list,size,ref,prepare_stack_for_ref(list,ref,false));
  1165. maybe_adjust_cmp_stackval(list,size,cmp_op);
  1166. a_load_const_stack(list,size,maybe_adjust_cmp_constval(size,cmp_op,a),def2regtyp(size));
  1167. a_cmp_stack_label(list,size,cmp_op,l);
  1168. end;
  1169. procedure thlcgwasm.a_cmp_const_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; a: tcgint; reg: tregister; l: tasmlabel);
  1170. begin
  1171. a_load_reg_stack(list,size,reg);
  1172. maybe_adjust_cmp_stackval(list,size,cmp_op);
  1173. a_load_const_stack(list,size,maybe_adjust_cmp_constval(size,cmp_op,a),def2regtyp(size));
  1174. a_cmp_stack_label(list,size,cmp_op,l);
  1175. end;
  1176. procedure thlcgwasm.a_cmp_ref_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; const ref: treference; reg: tregister; l: tasmlabel);
  1177. begin
  1178. a_load_reg_stack(list,size,reg);
  1179. maybe_adjust_cmp_stackval(list,size,cmp_op);
  1180. if ref.base<>NR_EVAL_STACK_BASE then
  1181. a_load_ref_stack(list,size,ref,prepare_stack_for_ref(list,ref,false))
  1182. else begin
  1183. // todo: need a swap operation?
  1184. //list.concat(taicpu.op_none(a_swap));
  1185. Internalerror(2019083003);
  1186. end;
  1187. maybe_adjust_cmp_stackval(list,size,cmp_op);
  1188. a_cmp_stack_label(list,size,cmp_op,l);
  1189. end;
  1190. procedure thlcgwasm.a_cmp_reg_ref_label(list: TAsmList; size: tdef; cmp_op: topcmp; reg: tregister; const ref: treference; l: tasmlabel);
  1191. begin
  1192. if ref.base<>NR_EVAL_STACK_BASE then
  1193. a_load_ref_stack(list,size,ref,prepare_stack_for_ref(list,ref,false));
  1194. maybe_adjust_cmp_stackval(list,size,cmp_op);
  1195. a_load_reg_stack(list,size,reg);
  1196. maybe_adjust_cmp_stackval(list,size,cmp_op);
  1197. a_cmp_stack_label(list,size,cmp_op,l);
  1198. end;
  1199. procedure thlcgwasm.a_cmp_reg_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; reg1, reg2: tregister; l: tasmlabel);
  1200. begin
  1201. a_load_reg_stack(list,size,reg2);
  1202. maybe_adjust_cmp_stackval(list,size,cmp_op);
  1203. a_load_reg_stack(list,size,reg1);
  1204. maybe_adjust_cmp_stackval(list,size,cmp_op);
  1205. a_cmp_stack_label(list,size,cmp_op,l);
  1206. end;
  1207. procedure thlcgwasm.a_jmp_always(list: TAsmList; l: tasmlabel);
  1208. begin
  1209. //todo: for proper jumping it's necessary to check
  1210. // all active blocks (if, block, loops)
  1211. // and jump to the proper one.
  1212. //list.concat(taicpu.op_const(a_i32_const, 0));
  1213. if l = current_procinfo.CurrBreakLabel then begin
  1214. // todo: this should be moved to node generator pass2
  1215. list.concat(taicpu.op_const(a_i32_const, 0));
  1216. list.concat(taicpu.op_const(a_br,1))
  1217. end else if l = current_procinfo.CurrContinueLabel then begin
  1218. list.concat(taicpu.op_const(a_i32_const, 0));
  1219. list.concat(taicpu.op_const(a_br,0))
  1220. end else
  1221. //Internalerror(2019091806); // unexpected jump
  1222. end;
  1223. procedure thlcgwasm.concatcopy_normal_array(list: TAsmList; size: tdef; const source, dest: treference);
  1224. var
  1225. procname: string;
  1226. eledef: tdef;
  1227. ndim: longint;
  1228. adddefaultlenparas: boolean;
  1229. begin
  1230. { load copy helper parameters on the stack }
  1231. a_load_ref_stack(list,ptruinttype,source,prepare_stack_for_ref(list,source,false));
  1232. a_load_ref_stack(list,ptruinttype,dest,prepare_stack_for_ref(list,dest,false));
  1233. { call copy helper }
  1234. eledef:=tarraydef(size).elementdef;
  1235. ndim:=1;
  1236. adddefaultlenparas:=true;
  1237. case eledef.typ of
  1238. orddef:
  1239. begin
  1240. case torddef(eledef).ordtype of
  1241. pasbool1,pasbool8,s8bit,u8bit,bool8bit,uchar,
  1242. s16bit,u16bit,bool16bit,pasbool16,
  1243. uwidechar,
  1244. s32bit,u32bit,bool32bit,pasbool32,
  1245. s64bit,u64bit,bool64bit,pasbool64,scurrency:
  1246. procname:='FPC_COPY_SHALLOW_ARRAY'
  1247. else
  1248. internalerror(2011020504);
  1249. end;
  1250. end;
  1251. arraydef:
  1252. begin
  1253. { call fpc_setlength_dynarr_multidim with deepcopy=true, and extra
  1254. parameters }
  1255. while (eledef.typ=arraydef) and
  1256. not is_dynamic_array(eledef) do
  1257. begin
  1258. eledef:=tarraydef(eledef).elementdef;
  1259. inc(ndim)
  1260. end;
  1261. if (ndim=1) then
  1262. procname:='FPC_COPY_SHALLOW_ARRAY'
  1263. else
  1264. begin
  1265. { deepcopy=true }
  1266. a_load_const_stack(list,pasbool1type,1,R_INTREGISTER);
  1267. { ndim }
  1268. a_load_const_stack(list,s32inttype,ndim,R_INTREGISTER);
  1269. { eletype }
  1270. { todo: WASM
  1271. a_load_const_stack(list,cwidechartype,ord(jvmarrtype_setlength(eledef)),R_INTREGISTER);
  1272. }
  1273. adddefaultlenparas:=false;
  1274. procname:='FPC_SETLENGTH_DYNARR_MULTIDIM';
  1275. end;
  1276. end;
  1277. recorddef:
  1278. procname:='FPC_COPY_JRECORD_ARRAY';
  1279. procvardef:
  1280. if tprocvardef(eledef).is_addressonly then
  1281. procname:='FPC_COPY_SHALLOW_ARRAY'
  1282. else
  1283. procname:='FPC_COPY_JPROCVAR_ARRAY';
  1284. setdef:
  1285. if tsetdef(eledef).elementdef.typ=enumdef then
  1286. procname:='FPC_COPY_JENUMSET_ARRAY'
  1287. else
  1288. procname:='FPC_COPY_JBITSET_ARRAY';
  1289. floatdef:
  1290. procname:='FPC_COPY_SHALLOW_ARRAY';
  1291. stringdef:
  1292. if is_shortstring(eledef) then
  1293. procname:='FPC_COPY_JSHORTSTRING_ARRAY'
  1294. else
  1295. procname:='FPC_COPY_SHALLOW_ARRAY';
  1296. variantdef:
  1297. begin
  1298. {$ifndef nounsupported}
  1299. procname:='FPC_COPY_SHALLOW_ARRAY';
  1300. {$else}
  1301. { todo: make a deep copy via clone... }
  1302. internalerror(2011020505);
  1303. {$endif}
  1304. end;
  1305. else
  1306. procname:='FPC_COPY_SHALLOW_ARRAY';
  1307. end;
  1308. if adddefaultlenparas then
  1309. begin
  1310. { -1, -1 means "copy entire array" }
  1311. a_load_const_stack(list,s32inttype,-1,R_INTREGISTER);
  1312. a_load_const_stack(list,s32inttype,-1,R_INTREGISTER);
  1313. end;
  1314. g_call_system_proc(list,procname,[],nil);
  1315. if ndim<>1 then
  1316. begin
  1317. { pop return value, must be the same as dest }
  1318. //list.concat(taicpu.op_none(a_pop));
  1319. Internalerror(2019083001); // no support for arrays
  1320. decstack(list,1);
  1321. end;
  1322. end;
  1323. procedure thlcgwasm.concatcopy_record(list: TAsmList; size: tdef; const source, dest: treference);
  1324. var
  1325. srsym: tsym;
  1326. pd: tprocdef;
  1327. begin
  1328. { self }
  1329. a_load_ref_stack(list,size,source,prepare_stack_for_ref(list,source,false));
  1330. { result }
  1331. a_load_ref_stack(list,size,dest,prepare_stack_for_ref(list,dest,false));
  1332. { call fpcDeepCopy helper }
  1333. srsym:=search_struct_member(tabstractrecorddef(size),'FPCDEEPCOPY');
  1334. if not assigned(srsym) or
  1335. (srsym.typ<>procsym) then
  1336. Message1(cg_f_unknown_compilerproc,size.typename+'.fpcDeepCopy');
  1337. pd:=tprocdef(tprocsym(srsym).procdeflist[0]);
  1338. a_call_name(list,pd,pd.mangledname,[],nil,false);
  1339. { both parameters are removed, no function result }
  1340. decstack(list,2);
  1341. end;
  1342. procedure thlcgwasm.concatcopy_set(list: TAsmList; size: tdef; const source, dest: treference);
  1343. begin
  1344. a_load_ref_stack(list,size,source,prepare_stack_for_ref(list,source,false));
  1345. a_load_ref_stack(list,size,dest,prepare_stack_for_ref(list,dest,false));
  1346. { call set copy helper }
  1347. if tsetdef(size).elementdef.typ=enumdef then
  1348. g_call_system_proc(list,'fpc_enumset_copy',[],nil)
  1349. else
  1350. g_call_system_proc(list,'fpc_bitset_copy',[],nil);
  1351. end;
  1352. procedure thlcgwasm.concatcopy_shortstring(list: TAsmList; size: tdef; const source, dest: treference);
  1353. var
  1354. srsym: tsym;
  1355. pd: tprocdef;
  1356. begin
  1357. { self }
  1358. a_load_ref_stack(list,size,source,prepare_stack_for_ref(list,source,false));
  1359. { result }
  1360. a_load_ref_stack(list,size,dest,prepare_stack_for_ref(list,dest,false));
  1361. { call fpcDeepCopy helper }
  1362. srsym:=search_struct_member(java_shortstring,'FPCDEEPCOPY');
  1363. if not assigned(srsym) or
  1364. (srsym.typ<>procsym) then
  1365. Message1(cg_f_unknown_compilerproc,'ShortstringClass.FpcDeepCopy');
  1366. pd:=tprocdef(tprocsym(srsym).procdeflist[0]);
  1367. a_call_name(list,pd,pd.mangledname,[],nil,false);
  1368. { both parameters are removed, no function result }
  1369. decstack(list,2);
  1370. end;
  1371. procedure thlcgwasm.paravarsym_set_initialloc_to_paraloc(vs: tparavarsym);
  1372. begin
  1373. // mark all callee side parameters AS local!
  1374. inherited paravarsym_set_initialloc_to_paraloc(vs);
  1375. if (vs.initialloc.loc = LOC_REFERENCE) then
  1376. vs.initialloc.reference.islocal := true;
  1377. end;
  1378. procedure thlcgwasm.g_concatcopy(list: TAsmList; size: tdef; const source, dest: treference);
  1379. var
  1380. handled: boolean;
  1381. begin
  1382. handled:=false;
  1383. case size.typ of
  1384. arraydef:
  1385. begin
  1386. if not is_dynamic_array(size) then
  1387. begin
  1388. concatcopy_normal_array(list,size,source,dest);
  1389. handled:=true;
  1390. end;
  1391. end;
  1392. recorddef:
  1393. begin
  1394. concatcopy_record(list,size,source,dest);
  1395. handled:=true;
  1396. end;
  1397. setdef:
  1398. begin
  1399. concatcopy_set(list,size,source,dest);
  1400. handled:=true;
  1401. end;
  1402. stringdef:
  1403. begin
  1404. if is_shortstring(size) then
  1405. begin
  1406. concatcopy_shortstring(list,size,source,dest);
  1407. handled:=true;
  1408. end;
  1409. end;
  1410. procvardef:
  1411. begin
  1412. if not tprocvardef(size).is_addressonly then
  1413. begin
  1414. concatcopy_record(list,tcpuprocvardef(size).classdef,source,dest);
  1415. handled:=true;
  1416. end;
  1417. end;
  1418. else
  1419. ;
  1420. end;
  1421. if not handled then
  1422. inherited;
  1423. end;
  1424. procedure thlcgwasm.g_copyshortstring(list: TAsmList; const source, dest: treference; strdef: tstringdef);
  1425. begin
  1426. concatcopy_shortstring(list,strdef,source,dest);
  1427. end;
  1428. procedure thlcgwasm.a_loadfpu_ref_ref(list: TAsmList; fromsize, tosize: tdef; const ref1, ref2: treference);
  1429. var
  1430. dstack_slots: longint;
  1431. begin
  1432. dstack_slots:=prepare_stack_for_ref(list,ref2,false);
  1433. a_load_ref_stack(list,fromsize,ref1,prepare_stack_for_ref(list,ref1,false));
  1434. resizestackfpuval(list,def_cgsize(fromsize),def_cgsize(tosize));
  1435. a_load_stack_ref(list,tosize,ref2,dstack_slots);
  1436. end;
  1437. procedure thlcgwasm.a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; reg: tregister);
  1438. begin
  1439. a_load_ref_stack(list,fromsize,ref,prepare_stack_for_ref(list,ref,false));
  1440. resizestackfpuval(list,def_cgsize(fromsize),def_cgsize(tosize));
  1441. a_load_stack_reg(list,tosize,reg);
  1442. end;
  1443. procedure thlcgwasm.a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tdef; reg: tregister; const ref: treference);
  1444. var
  1445. dstack_slots: longint;
  1446. begin
  1447. dstack_slots:=prepare_stack_for_ref(list,ref,false);
  1448. a_load_reg_stack(list,fromsize,reg);
  1449. resizestackfpuval(list,def_cgsize(fromsize),def_cgsize(tosize));
  1450. a_load_stack_ref(list,tosize,ref,dstack_slots);
  1451. end;
  1452. procedure thlcgwasm.a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tdef; reg1, reg2: tregister);
  1453. begin
  1454. a_load_reg_stack(list,fromsize,reg1);
  1455. resizestackfpuval(list,def_cgsize(fromsize),def_cgsize(tosize));
  1456. a_load_stack_reg(list,tosize,reg2);
  1457. end;
  1458. procedure thlcgwasm.g_proc_entry(list: TAsmList; localsize: longint; nostackframe: boolean);
  1459. begin
  1460. { the localsize is based on tg.lasttemp -> already in terms of stack
  1461. slots rather than bytes }
  1462. list.concat(tai_directive.Create(asd_jlimit,'locals '+tostr(localsize)));
  1463. { we insert the unit initialisation code afterwards in the proginit code,
  1464. and it uses one stack slot }
  1465. if (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1466. fmaxevalstackheight:=max(1,fmaxevalstackheight);
  1467. list.concat(tai_directive.Create(asd_jlimit,'stack '+tostr(fmaxevalstackheight)));
  1468. end;
  1469. procedure thlcgwasm.g_proc_exit(list: TAsmList; parasize: longint; nostackframe: boolean);
  1470. begin
  1471. list.concat(taicpu.op_none(a_return));
  1472. end;
  1473. procedure thlcgwasm.gen_load_return_value(list: TAsmList);
  1474. begin
  1475. { constructors don't return anything in the jvm }
  1476. if current_procinfo.procdef.proctypeoption in [potype_constructor,potype_class_constructor] then
  1477. exit;
  1478. inherited gen_load_return_value(list);
  1479. end;
  1480. procedure thlcgwasm.record_generated_code_for_procdef(pd: tprocdef; code, data: TAsmList);
  1481. begin
  1482. { add something to the al_procedures list as well, because if all al_*
  1483. lists are empty, the assembler writer isn't called }
  1484. if not code.empty and
  1485. current_asmdata.asmlists[al_procedures].empty then
  1486. current_asmdata.asmlists[al_procedures].concat(tai_align.Create(4));
  1487. tcpuprocdef(pd).exprasmlist:=TAsmList.create;
  1488. tcpuprocdef(pd).exprasmlist.concatlist(code);
  1489. if assigned(data) and
  1490. not data.empty then
  1491. internalerror(2010122801);
  1492. end;
  1493. procedure thlcgwasm.g_incrrefcount(list: TAsmList; t: tdef; const ref: treference);
  1494. begin
  1495. // do nothing
  1496. end;
  1497. procedure thlcgwasm.g_array_rtti_helper(list: TAsmList; t: tdef; const ref: treference; const highloc: tlocation; const name: string);
  1498. var
  1499. normaldim: longint;
  1500. eleref: treference;
  1501. begin
  1502. { only in case of initialisation, we have to set all elements to "empty" }
  1503. if name<>'fpc_initialize_array' then
  1504. exit;
  1505. { put array on the stack }
  1506. a_load_ref_stack(list,ptruinttype,ref,prepare_stack_for_ref(list,ref,false));
  1507. { in case it's an open array whose elements are regular arrays, put the
  1508. dimension of the regular arrays on the stack (otherwise pass 0) }
  1509. normaldim:=0;
  1510. while (t.typ=arraydef) and
  1511. not is_dynamic_array(t) do
  1512. begin
  1513. inc(normaldim);
  1514. t:=tarraydef(t).elementdef;
  1515. end;
  1516. a_load_const_stack(list,s32inttype,normaldim,R_INTREGISTER);
  1517. { highloc is invalid, the length is part of the array in Java }
  1518. if is_wide_or_unicode_string(t) then
  1519. g_call_system_proc(list,'fpc_initialize_array_unicodestring',[],nil)
  1520. else if is_ansistring(t) then
  1521. g_call_system_proc(list,'fpc_initialize_array_ansistring',[],nil)
  1522. else if is_dynamic_array(t) then
  1523. g_call_system_proc(list,'fpc_initialize_array_dynarr',[],nil)
  1524. else if is_record(t) or
  1525. (t.typ=setdef) then
  1526. begin
  1527. tg.gethltemp(list,t,t.size,tt_persistent,eleref);
  1528. a_load_ref_stack(list,t,eleref,prepare_stack_for_ref(list,eleref,false));
  1529. if is_record(t) then
  1530. g_call_system_proc(list,'fpc_initialize_array_record',[],nil)
  1531. else if tsetdef(t).elementdef.typ=enumdef then
  1532. g_call_system_proc(list,'fpc_initialize_array_enumset',[],nil)
  1533. else
  1534. g_call_system_proc(list,'fpc_initialize_array_bitset',[],nil);
  1535. tg.ungettemp(list,eleref);
  1536. end
  1537. else if (t.typ=enumdef) then
  1538. begin
  1539. if get_enum_init_val_ref(t,eleref) then
  1540. begin
  1541. a_load_ref_stack(list,ptruinttype,eleref,prepare_stack_for_ref(list,eleref,false));
  1542. g_call_system_proc(list,'fpc_initialize_array_object',[],nil);
  1543. end;
  1544. end
  1545. else
  1546. internalerror(2011031901);
  1547. end;
  1548. procedure thlcgwasm.g_initialize(list: TAsmList; t: tdef; const ref: treference);
  1549. var
  1550. dummyloc: tlocation;
  1551. sym: tsym;
  1552. pd: tprocdef;
  1553. begin
  1554. if (t.typ=arraydef) and
  1555. not is_dynamic_array(t) then
  1556. begin
  1557. dummyloc.loc:=LOC_INVALID;
  1558. g_array_rtti_helper(list,tarraydef(t).elementdef,ref,dummyloc,'fpc_initialize_array')
  1559. end
  1560. else if is_record(t) then
  1561. begin
  1562. { call the fpcInitializeRec method }
  1563. sym:=tsym(trecorddef(t).symtable.find('FPCINITIALIZEREC'));
  1564. if assigned(sym) and
  1565. (sym.typ=procsym) then
  1566. begin
  1567. if tprocsym(sym).procdeflist.Count<>1 then
  1568. internalerror(2011071713);
  1569. pd:=tprocdef(tprocsym(sym).procdeflist[0]);
  1570. end
  1571. else
  1572. internalerror(2013113008);
  1573. a_load_ref_stack(list,ptruinttype,ref,prepare_stack_for_ref(list,ref,false));
  1574. a_call_name(list,pd,pd.mangledname,[],nil,false);
  1575. { parameter removed, no result }
  1576. decstack(list,1);
  1577. end
  1578. else
  1579. a_load_const_ref(list,t,0,ref);
  1580. end;
  1581. procedure thlcgwasm.g_finalize(list: TAsmList; t: tdef; const ref: treference);
  1582. begin
  1583. // do nothing
  1584. end;
  1585. procedure thlcgwasm.g_overflowcheck(list: TAsmList; const Loc: tlocation; def: tdef);
  1586. begin
  1587. { not possible, need the original operands }
  1588. internalerror(2012102101);
  1589. end;
  1590. procedure thlcgwasm.g_overflowCheck_loc(List: TAsmList; const Loc: TLocation; def: TDef; var ovloc: tlocation);
  1591. var
  1592. hl : tasmlabel;
  1593. begin
  1594. if not(cs_check_overflow in current_settings.localswitches) then
  1595. exit;
  1596. current_asmdata.getjumplabel(hl);
  1597. a_cmp_const_loc_label(list,s32inttype,OC_EQ,0,ovloc,hl);
  1598. g_call_system_proc(list,'fpc_overflow',[],nil);
  1599. a_label(list,hl);
  1600. end;
  1601. procedure thlcgwasm.location_get_data_ref(list: TAsmList; def: tdef; const l: tlocation; var ref: treference; loadref: boolean; alignment: longint);
  1602. var
  1603. tmploc: tlocation;
  1604. begin
  1605. { This routine is a combination of a generalised a_loadaddr_ref_reg()
  1606. that also works for addresses in registers (in case loadref is false)
  1607. and of a_load_ref_reg (in case loadref is true). It is used for
  1608. a) getting the address of managed var/out parameters
  1609. b) getting to the actual data of value types that are passed by
  1610. reference by the compiler (and then get a local copy at the caller
  1611. side). Normally, depending on whether this reference is passed in a
  1612. register or reference, we either need a reference with that register
  1613. as base or load the address in that reference and use that as a new
  1614. base.
  1615. Since the JVM cannot take the address of anything, all
  1616. "pass-by-reference" value parameters (which are always aggregate types)
  1617. are already simply the implicit pointer to the data (since arrays,
  1618. records, etc are already internally implicit pointers). This means
  1619. that if "loadref" is true, we must simply return this implicit pointer.
  1620. If it is false, we are supposed the take the address of this implicit
  1621. pointer, which is not possible.
  1622. However, managed types are also implicit pointers in Pascal, so in that
  1623. case "taking the address" again consists of simply returning the
  1624. implicit pointer/current value (in case of a var/out parameter, this
  1625. value is stored inside an array).
  1626. }
  1627. if not loadref then
  1628. begin
  1629. if not is_managed_type(def) then
  1630. internalerror(2011020601);
  1631. tmploc:=l;
  1632. end
  1633. else
  1634. begin
  1635. if not wasmimplicitpointertype(def) then
  1636. begin
  1637. { passed by reference in array of single element; l contains the
  1638. base address of the array }
  1639. location_reset_ref(tmploc,LOC_REFERENCE,OS_ADDR,4,ref.volatility);
  1640. cgutils.reference_reset_base(tmploc.reference,getaddressregister(list,ptruinttype),0,tmploc.reference.temppos,4,ref.volatility);
  1641. a_load_loc_reg(list,ptruinttype,ptruinttype,l,tmploc.reference.base);
  1642. end
  1643. else
  1644. tmploc:=l;
  1645. end;
  1646. case tmploc.loc of
  1647. LOC_REGISTER,
  1648. LOC_CREGISTER :
  1649. begin
  1650. { the implicit pointer is in a register and has to be in a
  1651. reference -> create a reference and put it there }
  1652. location_force_mem(list,tmploc,ptruinttype);
  1653. ref:=tmploc.reference;
  1654. end;
  1655. LOC_REFERENCE,
  1656. LOC_CREFERENCE :
  1657. begin
  1658. ref:=tmploc.reference;
  1659. end;
  1660. else
  1661. internalerror(2011020603);
  1662. end;
  1663. end;
  1664. procedure thlcgwasm.maybe_change_load_node_reg(list: TAsmList; var n: tnode; reload: boolean);
  1665. begin
  1666. { don't do anything, all registers become stack locations anyway }
  1667. end;
  1668. procedure thlcgwasm.g_copyvaluepara_openarray(list: TAsmList; const ref: treference; const lenloc: tlocation; arrdef: tarraydef; destreg: tregister);
  1669. var
  1670. localref: treference;
  1671. arrloc: tlocation;
  1672. stackslots: longint;
  1673. begin
  1674. { temporary reference for passing to concatcopy }
  1675. tg.gethltemp(list,ptruinttype,ptruinttype.size,tt_persistent,localref);
  1676. stackslots:=prepare_stack_for_ref(list,localref,false);
  1677. { create the local copy of the array (lenloc is invalid, get length
  1678. directly from the array) }
  1679. location_reset_ref(arrloc,LOC_REFERENCE,OS_ADDR,sizeof(pint),ref.volatility);
  1680. arrloc.reference:=ref;
  1681. g_getarraylen(list,arrloc);
  1682. g_newarray(list,arrdef,1);
  1683. a_load_stack_ref(list,ptruinttype,localref,stackslots);
  1684. { copy the source array to the destination }
  1685. g_concatcopy(list,arrdef,ref,localref);
  1686. { and put the array pointer in the register as expected by the caller }
  1687. a_load_ref_reg(list,ptruinttype,ptruinttype,localref,destreg);
  1688. end;
  1689. procedure thlcgwasm.g_releasevaluepara_openarray(list: TAsmList; arrdef: tarraydef; const l: tlocation);
  1690. begin
  1691. // do nothing, long live garbage collection!
  1692. end;
  1693. procedure thlcgwasm.gen_initialize_code(list: TAsmList);
  1694. var
  1695. ref: treference;
  1696. begin
  1697. { create globals with wrapped types such as arrays/records }
  1698. case current_procinfo.procdef.proctypeoption of
  1699. potype_unitinit:
  1700. begin
  1701. cgutils.reference_reset_base(ref,NR_NO,0,ctempposinvalid,1,[]);
  1702. if assigned(current_module.globalsymtable) then
  1703. allocate_implicit_structs_for_st_with_base_ref(list,current_module.globalsymtable,ref,staticvarsym);
  1704. allocate_implicit_structs_for_st_with_base_ref(list,current_module.localsymtable,ref,staticvarsym);
  1705. end;
  1706. potype_class_constructor:
  1707. begin
  1708. { also initialise local variables, if any }
  1709. inherited;
  1710. { initialise class fields }
  1711. cgutils.reference_reset_base(ref,NR_NO,0,ctempposinvalid,1,[]);
  1712. allocate_implicit_structs_for_st_with_base_ref(list,tabstractrecorddef(current_procinfo.procdef.owner.defowner).symtable,ref,staticvarsym);
  1713. end
  1714. else
  1715. inherited
  1716. end;
  1717. end;
  1718. procedure thlcgwasm.gen_entry_code(list: TAsmList);
  1719. begin
  1720. list.concat(Tai_force_line.Create);
  1721. end;
  1722. procedure thlcgwasm.gen_exit_code(list: TAsmList);
  1723. begin
  1724. { nothing }
  1725. end;
  1726. procedure thlcgwasm.a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; srcsize, dstsize: tdef; src, dst: tregister);
  1727. begin
  1728. internalerror(2012090201);
  1729. end;
  1730. procedure thlcgwasm.a_loadmm_loc_reg(list: TAsmList; fromsize, tosize: tdef; const loc: tlocation; const reg: tregister; shuffle: pmmshuffle);
  1731. begin
  1732. internalerror(2012090202);
  1733. end;
  1734. procedure thlcgwasm.a_loadmm_reg_reg(list: TAsmList; fromsize, tosize: tdef; reg1, reg2: tregister; shuffle: pmmshuffle);
  1735. begin
  1736. internalerror(2012060130);
  1737. end;
  1738. procedure thlcgwasm.a_loadmm_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; reg: tregister; shuffle: pmmshuffle);
  1739. begin
  1740. internalerror(2012060131);
  1741. end;
  1742. procedure thlcgwasm.a_loadmm_reg_ref(list: TAsmList; fromsize, tosize: tdef; reg: tregister; const ref: treference; shuffle: pmmshuffle);
  1743. begin
  1744. internalerror(2012060132);
  1745. end;
  1746. procedure thlcgwasm.a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size: tdef; src, dst: tregister; shuffle: pmmshuffle);
  1747. begin
  1748. internalerror(2012060133);
  1749. end;
  1750. procedure thlcgwasm.a_loadmm_intreg_reg(list: TAsmList; fromsize, tosize: tdef; intreg, mmreg: tregister; shuffle: pmmshuffle);
  1751. begin
  1752. internalerror(2012060134);
  1753. end;
  1754. procedure thlcgwasm.a_loadmm_reg_intreg(list: TAsmList; fromsize, tosize: tdef; mmreg, intreg: tregister; shuffle: pmmshuffle);
  1755. begin
  1756. internalerror(2012060135);
  1757. end;
  1758. procedure thlcgwasm.g_stackpointer_alloc(list: TAsmList; size: longint);
  1759. begin
  1760. internalerror(2012090203);
  1761. end;
  1762. procedure thlcgwasm.g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);
  1763. begin
  1764. internalerror(2012090204);
  1765. end;
  1766. procedure thlcgwasm.g_adjust_self_value(list: TAsmList; procdef: tprocdef; ioffset: aint);
  1767. begin
  1768. internalerror(2012090205);
  1769. end;
  1770. procedure thlcgwasm.g_local_unwind(list: TAsmList; l: TAsmLabel);
  1771. begin
  1772. internalerror(2012090206);
  1773. end;
  1774. procedure thlcgwasm.a_load_stack_reg(list: TAsmList; size: tdef; reg: tregister);
  1775. var
  1776. opc: tasmop;
  1777. finishandval: tcgint;
  1778. begin
  1779. opc:=loadstoreopc(size,false,false,finishandval);
  1780. list.concat(taicpu.op_reg(opc,reg));
  1781. { avoid problems with getting the size of an open array etc }
  1782. if wasmimplicitpointertype(size) then
  1783. size:=ptruinttype;
  1784. decstack(list,1);
  1785. end;
  1786. procedure thlcgwasm.a_load_stack_ref(list: TAsmList; size: tdef; const ref: treference; extra_slots: longint);
  1787. var
  1788. opc: tasmop;
  1789. finishandval: tcgint;
  1790. begin
  1791. { fake location that indicates the value has to remain on the stack }
  1792. if ref.base=NR_EVAL_STACK_BASE then
  1793. exit;
  1794. opc:=loadstoreopcref(size,false,ref,finishandval);
  1795. list.concat(taicpu.op_ref(opc,ref));
  1796. { avoid problems with getting the size of an open array etc }
  1797. if wasmimplicitpointertype(size) then
  1798. size:=ptruinttype;
  1799. decstack(list,1+extra_slots);
  1800. end;
  1801. procedure thlcgwasm.a_load_reg_stack(list: TAsmList; size: tdef; reg: tregister);
  1802. var
  1803. opc: tasmop;
  1804. finishandval: tcgint;
  1805. begin
  1806. opc:=loadstoreopc(size,true,false,finishandval);
  1807. list.concat(taicpu.op_reg(opc,reg));
  1808. { avoid problems with getting the size of an open array etc }
  1809. if wasmimplicitpointertype(size) then
  1810. size:=ptruinttype;
  1811. incstack(list,1);
  1812. if finishandval<>-1 then
  1813. a_op_const_stack(list,OP_AND,size,finishandval);
  1814. end;
  1815. procedure thlcgwasm.a_load_ref_stack(list: TAsmList; size: tdef; const ref: treference; extra_slots: longint);
  1816. var
  1817. opc: tasmop;
  1818. finishandval: tcgint;
  1819. begin
  1820. { fake location that indicates the value is already on the stack? }
  1821. if (ref.base=NR_EVAL_STACK_BASE) then
  1822. exit;
  1823. opc:=loadstoreopcref(size,true,ref,finishandval);
  1824. list.concat(taicpu.op_ref(opc,ref));
  1825. { avoid problems with getting the size of an open array etc }
  1826. if wasmimplicitpointertype(size) then
  1827. size:=ptruinttype;
  1828. incstack(list,1-extra_slots);
  1829. if finishandval<>-1 then
  1830. a_op_const_stack(list,OP_AND,size,finishandval);
  1831. // there's no cast check in Wasm
  1832. //if ref.checkcast then
  1833. // gen_typecheck(list,a_checkcast,size);
  1834. end;
  1835. function thlcgwasm.loadstoreopcref(def: tdef; isload: boolean; const ref: treference; out finishandval: tcgint): tasmop;
  1836. const
  1837. {iisload} {issigned}
  1838. getputmem8 : array [boolean, boolean] of TAsmOp = ((a_i32_store8, a_i32_store8), (a_i32_load8_u, a_i32_load8_s));
  1839. getputmem16 : array [boolean, boolean] of TAsmOp = ((a_i32_store16, a_i32_store16), (a_i32_load16_u ,a_i32_load16_s));
  1840. getputmem32 : array [boolean, boolean] of TAsmOp = ((a_i32_store, a_i32_store), (a_i32_load, a_i32_load));
  1841. getputmem64 : array [boolean, boolean] of TAsmOp = ((a_i64_store, a_i64_store), (a_i64_load, a_i64_load));
  1842. getputmemf32 : array [boolean] of TAsmOp = (a_f32_store, a_f32_load);
  1843. getputmemf64 : array [boolean] of TAsmOp = (a_f64_store, a_f64_load);
  1844. begin
  1845. if (not ref.islocal) or assigned(ref.symbol) then
  1846. begin
  1847. { -> either a global (static) field, or a regular field. If a regular
  1848. field, then ref.base contains the self pointer, otherwise
  1849. ref.base=NR_NO. In both cases, the symbol contains all other
  1850. information (combined field name and type descriptor) }
  1851. case def.size of
  1852. 1: result := getputmem8[isload, is_signed(def)];
  1853. 2: result := getputmem16[isload, is_signed(def)];
  1854. 4:
  1855. if is_single(def) then
  1856. result := getputmemf32[isload]
  1857. else
  1858. result := getputmem32[isload, is_signed(def)];
  1859. 8: if is_double(def) then
  1860. result := getputmemf64[isload]
  1861. else
  1862. result := getputmem64[isload, is_signed(def)];
  1863. else
  1864. Internalerror(201909150001);
  1865. end;
  1866. //result:=getputopc[isload,ref.base=NR_NO];
  1867. finishandval:=-1;
  1868. { erase sign extension for byte/smallint loads }
  1869. if (def2regtyp(def)=R_INTREGISTER) and
  1870. not is_signed(def) and
  1871. (def.typ=orddef) and
  1872. not is_widechar(def) then
  1873. case def.size of
  1874. 1: if (torddef(def).high>127) then
  1875. finishandval:=255;
  1876. 2: if (torddef(def).high>32767) then
  1877. finishandval:=65535;
  1878. end;
  1879. end
  1880. else
  1881. result:=loadstoreopc(def,isload,false,finishandval);
  1882. end;
  1883. function thlcgwasm.loadstoreopc(def: tdef; isload, isarray: boolean; out finishandval: tcgint): tasmop;
  1884. var
  1885. size: longint;
  1886. begin
  1887. finishandval:=-1;
  1888. if isload then result := a_get_local
  1889. else result := a_set_local;
  1890. {case def2regtyp(def) of
  1891. R_INTREGISTER:
  1892. begin
  1893. size:=def.size;
  1894. case size of
  1895. 1,2,3,4:
  1896. if isload then
  1897. result:=a_i32_load
  1898. else
  1899. result:=a_i32_store;
  1900. 8:
  1901. if isload then
  1902. result:=a_i64_load
  1903. else
  1904. result:=a_i64_store;
  1905. else
  1906. internalerror(2011032814);
  1907. end;
  1908. end;
  1909. R_ADDRESSREGISTER:
  1910. if isload then
  1911. result:=a_i32_load
  1912. else
  1913. result:=a_i32_store;
  1914. R_FPUREGISTER:
  1915. begin
  1916. case tfloatdef(def).floattype of
  1917. s32real:
  1918. if isload then
  1919. result:=a_f32_load
  1920. else
  1921. result:=a_f32_store;
  1922. s64real:
  1923. if isload then
  1924. result:=a_f32_load
  1925. else
  1926. result:=a_f32_store
  1927. else
  1928. internalerror(2010120504);
  1929. end
  1930. end
  1931. else
  1932. internalerror(2010120502);
  1933. end;}
  1934. end;
  1935. procedure thlcgwasm.resize_stack_int_val(list: TAsmList; fromsize, tosize: tdef; formemstore: boolean);
  1936. var
  1937. fromcgsize, tocgsize: tcgsize;
  1938. begin
  1939. { When storing to an array, field or global variable, make sure the
  1940. static type verification can determine that the stored value fits
  1941. within the boundaries of the declared type (to appease the Dalvik VM).
  1942. Local variables either get their type upgraded in the debug info,
  1943. or have no type information at all }
  1944. if formemstore and
  1945. (tosize.typ=orddef) then
  1946. if (torddef(tosize).ordtype in [u8bit,uchar]) then
  1947. tosize:=s8inttype
  1948. else if torddef(tosize).ordtype=u16bit then
  1949. tosize:=s16inttype;
  1950. fromcgsize:=def_cgsize(fromsize);
  1951. tocgsize:=def_cgsize(tosize);
  1952. if fromcgsize in [OS_S64,OS_64] then
  1953. begin
  1954. if not(tocgsize in [OS_S64,OS_64]) then
  1955. begin
  1956. { truncate }
  1957. list.concat(taicpu.op_none(a_i32_wrap_i64));
  1958. decstack(list,1);
  1959. end;
  1960. end
  1961. else if tocgsize in [OS_S64,OS_64] then
  1962. begin
  1963. { extend }
  1964. if tocgsize = OS_S64 then
  1965. list.concat(taicpu.op_none(a_i64_extend_s_i32))
  1966. else
  1967. list.concat(taicpu.op_none(a_i64_extend_u_i32));
  1968. incstack(list,1);
  1969. { if it was an unsigned 32 bit value, remove sign extension }
  1970. if fromcgsize=OS_32 then
  1971. a_op_const_stack(list,OP_AND,s64inttype,cardinal($ffffffff));
  1972. end;
  1973. { Conversions between 32 and 64 bit types have been completely handled
  1974. above. We still may have to truncate or sign extend in case the
  1975. destination type is smaller that the source type, or has a different
  1976. sign. In case the destination is a widechar and the source is not, we
  1977. also have to insert a conversion to widechar.
  1978. }
  1979. if (not(fromcgsize in [OS_S64,OS_64,OS_32,OS_S32]) or
  1980. not(tocgsize in [OS_S64,OS_64,OS_32,OS_S32])) and
  1981. ((tcgsize2size[fromcgsize]>tcgsize2size[tocgsize]) or
  1982. ((tcgsize2size[fromcgsize]=tcgsize2size[tocgsize]) and
  1983. (fromcgsize<>tocgsize)) or
  1984. { needs to mask out the sign in the top 16 bits }
  1985. ((fromcgsize=OS_S8) and
  1986. (tocgsize=OS_16)) or
  1987. ((tosize=cwidechartype) and
  1988. (fromsize<>cwidechartype))) then
  1989. case tocgsize of
  1990. OS_8:
  1991. //todo: conversion
  1992. //a_op_const_stack(list,OP_AND,s32inttype,255);
  1993. ;
  1994. OS_S8:
  1995. //todo: conversion
  1996. //list.concat(taicpu.op_none(a_i2b));
  1997. ;
  1998. OS_16:
  1999. //todo: conversion
  2000. //if (tosize.typ=orddef) and
  2001. // (torddef(tosize).ordtype=uwidechar) then
  2002. // list.concat(taicpu.op_none(a_i2c))
  2003. //else
  2004. // a_op_const_stack(list,OP_AND,s32inttype,65535);
  2005. ;
  2006. OS_S16:
  2007. //todo: conversion
  2008. //list.concat(taicpu.op_none(a_i2s));
  2009. ;
  2010. else
  2011. ;
  2012. end;
  2013. end;
  2014. procedure thlcgwasm.maybe_resize_stack_para_val(list: TAsmList; retdef: tdef; callside: boolean);
  2015. var
  2016. convsize: tdef;
  2017. begin
  2018. if (retdef.typ=orddef) then
  2019. begin
  2020. if (torddef(retdef).ordtype in [u8bit,u16bit,uchar]) and
  2021. (torddef(retdef).high>=(1 shl (retdef.size*8-1))) then
  2022. begin
  2023. convsize:=nil;
  2024. if callside then
  2025. if torddef(retdef).ordtype in [u8bit,uchar] then
  2026. convsize:=s8inttype
  2027. else
  2028. convsize:=s16inttype
  2029. else if torddef(retdef).ordtype in [u8bit,uchar] then
  2030. convsize:=u8inttype
  2031. else
  2032. convsize:=u16inttype;
  2033. if assigned(convsize) then
  2034. resize_stack_int_val(list,s32inttype,convsize,false);
  2035. end;
  2036. end;
  2037. end;
  2038. procedure thlcgwasm.g_adjust_stack_after_call(list: TAsmList; pd: tabstractprocdef; paraheight: longint; forceresdef: tdef);
  2039. var
  2040. totalremovesize: longint;
  2041. realresdef: tdef;
  2042. begin
  2043. if not assigned(forceresdef) then
  2044. realresdef:=pd.returndef
  2045. else
  2046. realresdef:=forceresdef;
  2047. { a constructor doesn't actually return a value in the jvm }
  2048. if (tabstractprocdef(pd).proctypeoption=potype_constructor) then
  2049. totalremovesize:=paraheight
  2050. else
  2051. { even a byte takes up a full stackslot -> align size to multiple of 4 }
  2052. totalremovesize:=paraheight-(align(realresdef.size,4) shr 2);
  2053. { remove parameters from internal evaluation stack counter (in case of
  2054. e.g. no parameters and a result, it can also increase) }
  2055. if totalremovesize>0 then
  2056. decstack(list,totalremovesize)
  2057. else if totalremovesize<0 then
  2058. incstack(list,-totalremovesize);
  2059. end;
  2060. procedure thlcgwasm.allocate_implicit_struct_with_base_ref(list: TAsmList; vs: tabstractvarsym; ref: treference);
  2061. var
  2062. tmpref: treference;
  2063. begin
  2064. ref.symbol:=current_asmdata.RefAsmSymbol(vs.mangledname,AT_DATA);
  2065. tg.gethltemp(list,vs.vardef,vs.vardef.size,tt_persistent,tmpref);
  2066. { only copy the reference, not the actual data }
  2067. a_load_ref_ref(list,ptruinttype,ptruinttype,tmpref,ref);
  2068. { remains live since there's still a reference to the created
  2069. entity }
  2070. tg.ungettemp(list,tmpref);
  2071. end;
  2072. procedure thlcgwasm.allocate_enum_with_base_ref(list: TAsmList; vs: tabstractvarsym; const initref: treference; destbaseref: treference);
  2073. begin
  2074. destbaseref.symbol:=current_asmdata.RefAsmSymbol(vs.mangledname,AT_DATA);
  2075. { only copy the reference, not the actual data }
  2076. a_load_ref_ref(list,ptruinttype,ptruinttype,initref,destbaseref);
  2077. end;
  2078. function thlcgwasm.get_enum_init_val_ref(def: tdef; out ref: treference): boolean;
  2079. var
  2080. sym: tstaticvarsym;
  2081. begin
  2082. result:=false;
  2083. sym:=tstaticvarsym(tcpuenumdef(tenumdef(def).getbasedef).classdef.symtable.Find('__FPC_ZERO_INITIALIZER'));
  2084. { no enum with ordinal value 0 -> exit }
  2085. if not assigned(sym) then
  2086. exit;
  2087. reference_reset_symbol(ref,current_asmdata.RefAsmSymbol(sym.mangledname,AT_DATA),0,4,[]);
  2088. result:=true;
  2089. end;
  2090. procedure thlcgwasm.allocate_implicit_structs_for_st_with_base_ref(list: TAsmList; st: tsymtable; const ref: treference; allocvartyp: tsymtyp);
  2091. var
  2092. vs: tabstractvarsym;
  2093. def: tdef;
  2094. i: longint;
  2095. initref: treference;
  2096. begin
  2097. for i:=0 to st.symlist.count-1 do
  2098. begin
  2099. if (tsym(st.symlist[i]).typ<>allocvartyp) then
  2100. continue;
  2101. vs:=tabstractvarsym(st.symlist[i]);
  2102. if sp_static in vs.symoptions then
  2103. continue;
  2104. { vo_is_external and vo_has_local_copy means a staticvarsym that is
  2105. alias for a constsym, whose sole purpose is for allocating and
  2106. intialising the constant }
  2107. if [vo_is_external,vo_has_local_copy]*vs.varoptions=[vo_is_external] then
  2108. continue;
  2109. { threadvar innitializations are handled at the node tree level }
  2110. if vo_is_thread_var in vs.varoptions then
  2111. begin
  2112. { nothing }
  2113. end
  2114. else if wasmimplicitpointertype(vs.vardef) then
  2115. allocate_implicit_struct_with_base_ref(list,vs,ref)
  2116. { enums are class instances in Java, while they are ordinals in
  2117. Pascal. When they are initialized with enum(0), such as in
  2118. constructors or global variables, initialize them with the
  2119. enum instance for 0 if it exists (if not, it remains nil since
  2120. there is no valid enum value in it) }
  2121. else if (vs.vardef.typ=enumdef) and
  2122. ((vs.typ<>fieldvarsym) or
  2123. (tdef(vs.owner.defowner).typ<>objectdef) or
  2124. (ts_jvm_enum_field_init in current_settings.targetswitches)) and
  2125. get_enum_init_val_ref(vs.vardef,initref) then
  2126. allocate_enum_with_base_ref(list,vs,initref,ref);
  2127. end;
  2128. { process symtables of routines part of this symtable (for local typed
  2129. constants) }
  2130. if allocvartyp=staticvarsym then
  2131. begin
  2132. for i:=0 to st.deflist.count-1 do
  2133. begin
  2134. def:=tdef(st.deflist[i]);
  2135. { the unit symtable also contains the methods of classes defined
  2136. in that unit -> skip them when processing the unit itself.
  2137. Localst is not assigned for the main program code.
  2138. Localst can be the same as st in case of unit init code. }
  2139. if (def.typ<>procdef) or
  2140. (def.owner<>st) or
  2141. not assigned(tprocdef(def).localst) or
  2142. (tprocdef(def).localst=st) then
  2143. continue;
  2144. allocate_implicit_structs_for_st_with_base_ref(list,tprocdef(def).localst,ref,allocvartyp);
  2145. end;
  2146. end;
  2147. end;
  2148. procedure thlcgwasm.gen_initialize_fields_code(list: TAsmList);
  2149. var
  2150. sym: tsym;
  2151. selfpara: tparavarsym;
  2152. selfreg: tregister;
  2153. ref: treference;
  2154. obj: tabstractrecorddef;
  2155. i: longint;
  2156. needinit: boolean;
  2157. begin
  2158. obj:=tabstractrecorddef(current_procinfo.procdef.owner.defowner);
  2159. { check whether there are any fields that need initialisation }
  2160. needinit:=false;
  2161. for i:=0 to obj.symtable.symlist.count-1 do
  2162. begin
  2163. sym:=tsym(obj.symtable.symlist[i]);
  2164. if (sym.typ=fieldvarsym) and
  2165. not(sp_static in sym.symoptions) and
  2166. (wasmimplicitpointertype(tfieldvarsym(sym).vardef) or
  2167. ((tfieldvarsym(sym).vardef.typ=enumdef) and
  2168. get_enum_init_val_ref(tfieldvarsym(sym).vardef,ref))) then
  2169. begin
  2170. needinit:=true;
  2171. break;
  2172. end;
  2173. end;
  2174. if not needinit then
  2175. exit;
  2176. selfpara:=tparavarsym(current_procinfo.procdef.parast.find('self'));
  2177. if not assigned(selfpara) then
  2178. internalerror(2011033001);
  2179. selfreg:=getaddressregister(list,selfpara.vardef);
  2180. a_load_loc_reg(list,obj,obj,selfpara.localloc,selfreg);
  2181. cgutils.reference_reset_base(ref,selfreg,0,ctempposinvalid,1,[]);
  2182. allocate_implicit_structs_for_st_with_base_ref(list,obj.symtable,ref,fieldvarsym);
  2183. end;
  2184. procedure thlcgwasm.gen_typecheck(list: TAsmList; checkop: tasmop; checkdef: tdef);
  2185. begin
  2186. { replace special types with their equivalent class type }
  2187. if (checkdef.typ=pointerdef) and
  2188. wasmimplicitpointertype(tpointerdef(checkdef).pointeddef) then
  2189. checkdef:=tpointerdef(checkdef).pointeddef;
  2190. if (checkdef=voidpointertype) or
  2191. (checkdef.typ=formaldef) then
  2192. checkdef:=ptruinttype
  2193. else if checkdef.typ=enumdef then
  2194. checkdef:=tcpuenumdef(checkdef).classdef
  2195. else if checkdef.typ=setdef then
  2196. begin
  2197. if tsetdef(checkdef).elementdef.typ=enumdef then
  2198. checkdef:=java_juenumset
  2199. else
  2200. checkdef:=java_jubitset;
  2201. end
  2202. else if checkdef.typ=procvardef then
  2203. checkdef:=tcpuprocvardef(checkdef).classdef
  2204. else if is_wide_or_unicode_string(checkdef) then
  2205. checkdef:=java_jlstring
  2206. else if is_ansistring(checkdef) then
  2207. checkdef:=java_ansistring
  2208. else if is_shortstring(checkdef) then
  2209. checkdef:=java_shortstring;
  2210. if checkdef.typ in [objectdef,recorddef] then
  2211. list.concat(taicpu.op_sym(checkop,current_asmdata.RefAsmSymbol(tabstractrecorddef(checkdef).jvm_full_typename(true),AT_METADATA)))
  2212. else if checkdef.typ=classrefdef then
  2213. list.concat(taicpu.op_sym(checkop,current_asmdata.RefAsmSymbol('java/lang/Class',AT_METADATA)))
  2214. { todo: WASM
  2215. else
  2216. list.concat(taicpu.op_sym(checkop,current_asmdata.RefAsmSymbol(jvmencodetype(checkdef,false),AT_METADATA)));
  2217. }
  2218. end;
  2219. procedure thlcgwasm.resizestackfpuval(list: TAsmList; fromsize, tosize: tcgsize);
  2220. begin
  2221. if (fromsize=OS_F32) and
  2222. (tosize=OS_F64) then
  2223. begin
  2224. list.concat(taicpu.op_none(a_f64_promote_f32));
  2225. incstack(list,1);
  2226. end
  2227. else if (fromsize=OS_F64) and
  2228. (tosize=OS_F32) then
  2229. begin
  2230. list.concat(taicpu.op_none(a_f32_demote_f64));
  2231. decstack(list,1);
  2232. end;
  2233. end;
  2234. procedure thlcgwasm.maybepreparedivu32(list: TAsmList; var op: topcg; size: tdef; out isdivu32: boolean);
  2235. begin
  2236. if (op=OP_DIV) and
  2237. (def_cgsize(size)=OS_32) then
  2238. begin
  2239. { needs zero-extension to 64 bit, because the JVM only supports
  2240. signed divisions }
  2241. resize_stack_int_val(list,u32inttype,s64inttype,false);
  2242. op:=OP_IDIV;
  2243. isdivu32:=true;
  2244. end
  2245. else
  2246. isdivu32:=false;
  2247. end;
  2248. function thlcgwasm.a_call_name_intern(list: TAsmList; pd: tprocdef; const s: TSymStr; forceresdef: tdef; inheritedcall: boolean): tcgpara;
  2249. var
  2250. opc: tasmop;
  2251. begin
  2252. {
  2253. invoke types:
  2254. * invokeinterface: call method from an interface (must also specify
  2255. number of parameters in terms of stack slot count!)
  2256. * invokespecial: invoke a constructor, method in a superclass,
  2257. or private instance method
  2258. * invokestatic: invoke a class method (private or not)
  2259. * invokevirtual: invoke a regular method
  2260. }
  2261. case pd.owner.symtabletype of
  2262. globalsymtable,
  2263. staticsymtable,
  2264. localsymtable:
  2265. { regular and nested procedures are turned into static methods }
  2266. opc:=a_call;
  2267. objectsymtable:
  2268. begin
  2269. Internalerror(2019083004); // no support for symbol table calls
  2270. opc:=a_call_indirect; // todo: need a reference to a (funccall) table
  2271. {case tobjectdef(pd.owner.defowner).objecttype of
  2272. odt_javaclass:
  2273. begin
  2274. if (po_classmethod in pd.procoptions) or
  2275. (pd.proctypeoption=potype_operator) then
  2276. opc:=a_invokestatic
  2277. else if (pd.visibility=vis_strictprivate) or
  2278. (pd.proctypeoption=potype_constructor) or
  2279. inheritedcall then
  2280. opc:=a_invokespecial
  2281. else
  2282. opc:=a_invokevirtual;
  2283. end;
  2284. odt_interfacejava:
  2285. { static interface methods are not allowed }
  2286. opc:=a_invokeinterface;
  2287. else
  2288. internalerror(2010122601);
  2289. end;
  2290. }
  2291. end;
  2292. recordsymtable:
  2293. begin
  2294. if (po_staticmethod in pd.procoptions) or
  2295. (pd.proctypeoption=potype_operator) then
  2296. opc:=a_call
  2297. else if (pd.visibility=vis_strictprivate) or
  2298. (pd.proctypeoption=potype_constructor) or
  2299. inheritedcall then
  2300. opc:=a_call
  2301. else
  2302. opc:=a_call_indirect;
  2303. end
  2304. else
  2305. internalerror(2010122602);
  2306. end;
  2307. if (opc<>a_call_indirect) then
  2308. list.concat(taicpu.op_sym(opc,current_asmdata.RefAsmSymbol(s,AT_FUNCTION)))
  2309. else
  2310. begin
  2311. pd.init_paraloc_info(calleeside);
  2312. list.concat(taicpu.op_sym_const(opc,current_asmdata.RefAsmSymbol(s,AT_FUNCTION),pd.calleeargareasize));
  2313. end;
  2314. result:=get_call_result_cgpara(pd,forceresdef);
  2315. end;
  2316. procedure create_hlcodegen_cpu;
  2317. begin
  2318. hlcg:=thlcgwasm.create;
  2319. create_codegen;
  2320. end;
  2321. initialization
  2322. chlcgobj:=thlcgwasm;
  2323. create_hlcodegen:=@create_hlcodegen_cpu;
  2324. end.