hlcgcpu.pas 99 KB

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