hlcgcpu.pas 105 KB

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