hlcgcpu.pas 103 KB

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