hlcgcpu.pas 98 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497
  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_i64_eq));
  768. OC_NE:
  769. //list.concat(taicpu.op_sym(a_i64_ne,lab));
  770. list.concat(taicpu.op_none(a_i64_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) 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,java_jlobject,java_jlobject,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. list.concat(taicpu.op_sym(a_br,current_asmdata.RefAsmSymbol(l.name,AT_METADATA)));
  1197. end;
  1198. procedure thlcgwasm.concatcopy_normal_array(list: TAsmList; size: tdef; const source, dest: treference);
  1199. var
  1200. procname: string;
  1201. eledef: tdef;
  1202. ndim: longint;
  1203. adddefaultlenparas: boolean;
  1204. begin
  1205. { load copy helper parameters on the stack }
  1206. a_load_ref_stack(list,java_jlobject,source,prepare_stack_for_ref(list,source,false));
  1207. a_load_ref_stack(list,java_jlobject,dest,prepare_stack_for_ref(list,dest,false));
  1208. { call copy helper }
  1209. eledef:=tarraydef(size).elementdef;
  1210. ndim:=1;
  1211. adddefaultlenparas:=true;
  1212. case eledef.typ of
  1213. orddef:
  1214. begin
  1215. case torddef(eledef).ordtype of
  1216. pasbool1,pasbool8,s8bit,u8bit,bool8bit,uchar,
  1217. s16bit,u16bit,bool16bit,pasbool16,
  1218. uwidechar,
  1219. s32bit,u32bit,bool32bit,pasbool32,
  1220. s64bit,u64bit,bool64bit,pasbool64,scurrency:
  1221. procname:='FPC_COPY_SHALLOW_ARRAY'
  1222. else
  1223. internalerror(2011020504);
  1224. end;
  1225. end;
  1226. arraydef:
  1227. begin
  1228. { call fpc_setlength_dynarr_multidim with deepcopy=true, and extra
  1229. parameters }
  1230. while (eledef.typ=arraydef) and
  1231. not is_dynamic_array(eledef) do
  1232. begin
  1233. eledef:=tarraydef(eledef).elementdef;
  1234. inc(ndim)
  1235. end;
  1236. if (ndim=1) then
  1237. procname:='FPC_COPY_SHALLOW_ARRAY'
  1238. else
  1239. begin
  1240. { deepcopy=true }
  1241. a_load_const_stack(list,pasbool1type,1,R_INTREGISTER);
  1242. { ndim }
  1243. a_load_const_stack(list,s32inttype,ndim,R_INTREGISTER);
  1244. { eletype }
  1245. { todo: WASM
  1246. a_load_const_stack(list,cwidechartype,ord(jvmarrtype_setlength(eledef)),R_INTREGISTER);
  1247. }
  1248. adddefaultlenparas:=false;
  1249. procname:='FPC_SETLENGTH_DYNARR_MULTIDIM';
  1250. end;
  1251. end;
  1252. recorddef:
  1253. procname:='FPC_COPY_JRECORD_ARRAY';
  1254. procvardef:
  1255. if tprocvardef(eledef).is_addressonly then
  1256. procname:='FPC_COPY_SHALLOW_ARRAY'
  1257. else
  1258. procname:='FPC_COPY_JPROCVAR_ARRAY';
  1259. setdef:
  1260. if tsetdef(eledef).elementdef.typ=enumdef then
  1261. procname:='FPC_COPY_JENUMSET_ARRAY'
  1262. else
  1263. procname:='FPC_COPY_JBITSET_ARRAY';
  1264. floatdef:
  1265. procname:='FPC_COPY_SHALLOW_ARRAY';
  1266. stringdef:
  1267. if is_shortstring(eledef) then
  1268. procname:='FPC_COPY_JSHORTSTRING_ARRAY'
  1269. else
  1270. procname:='FPC_COPY_SHALLOW_ARRAY';
  1271. variantdef:
  1272. begin
  1273. {$ifndef nounsupported}
  1274. procname:='FPC_COPY_SHALLOW_ARRAY';
  1275. {$else}
  1276. { todo: make a deep copy via clone... }
  1277. internalerror(2011020505);
  1278. {$endif}
  1279. end;
  1280. else
  1281. procname:='FPC_COPY_SHALLOW_ARRAY';
  1282. end;
  1283. if adddefaultlenparas then
  1284. begin
  1285. { -1, -1 means "copy entire array" }
  1286. a_load_const_stack(list,s32inttype,-1,R_INTREGISTER);
  1287. a_load_const_stack(list,s32inttype,-1,R_INTREGISTER);
  1288. end;
  1289. g_call_system_proc(list,procname,[],nil);
  1290. if ndim<>1 then
  1291. begin
  1292. { pop return value, must be the same as dest }
  1293. //list.concat(taicpu.op_none(a_pop));
  1294. Internalerror(2019083001); // no support for arrays
  1295. decstack(list,1);
  1296. end;
  1297. end;
  1298. procedure thlcgwasm.concatcopy_record(list: TAsmList; size: tdef; const source, dest: treference);
  1299. var
  1300. srsym: tsym;
  1301. pd: tprocdef;
  1302. begin
  1303. { self }
  1304. a_load_ref_stack(list,size,source,prepare_stack_for_ref(list,source,false));
  1305. { result }
  1306. a_load_ref_stack(list,size,dest,prepare_stack_for_ref(list,dest,false));
  1307. { call fpcDeepCopy helper }
  1308. srsym:=search_struct_member(tabstractrecorddef(size),'FPCDEEPCOPY');
  1309. if not assigned(srsym) or
  1310. (srsym.typ<>procsym) then
  1311. Message1(cg_f_unknown_compilerproc,size.typename+'.fpcDeepCopy');
  1312. pd:=tprocdef(tprocsym(srsym).procdeflist[0]);
  1313. a_call_name(list,pd,pd.mangledname,[],nil,false);
  1314. { both parameters are removed, no function result }
  1315. decstack(list,2);
  1316. end;
  1317. procedure thlcgwasm.concatcopy_set(list: TAsmList; size: tdef; const source, dest: treference);
  1318. begin
  1319. a_load_ref_stack(list,size,source,prepare_stack_for_ref(list,source,false));
  1320. a_load_ref_stack(list,size,dest,prepare_stack_for_ref(list,dest,false));
  1321. { call set copy helper }
  1322. if tsetdef(size).elementdef.typ=enumdef then
  1323. g_call_system_proc(list,'fpc_enumset_copy',[],nil)
  1324. else
  1325. g_call_system_proc(list,'fpc_bitset_copy',[],nil);
  1326. end;
  1327. procedure thlcgwasm.concatcopy_shortstring(list: TAsmList; size: tdef; const source, dest: treference);
  1328. var
  1329. srsym: tsym;
  1330. pd: tprocdef;
  1331. begin
  1332. { self }
  1333. a_load_ref_stack(list,size,source,prepare_stack_for_ref(list,source,false));
  1334. { result }
  1335. a_load_ref_stack(list,size,dest,prepare_stack_for_ref(list,dest,false));
  1336. { call fpcDeepCopy helper }
  1337. srsym:=search_struct_member(java_shortstring,'FPCDEEPCOPY');
  1338. if not assigned(srsym) or
  1339. (srsym.typ<>procsym) then
  1340. Message1(cg_f_unknown_compilerproc,'ShortstringClass.FpcDeepCopy');
  1341. pd:=tprocdef(tprocsym(srsym).procdeflist[0]);
  1342. a_call_name(list,pd,pd.mangledname,[],nil,false);
  1343. { both parameters are removed, no function result }
  1344. decstack(list,2);
  1345. end;
  1346. procedure thlcgwasm.g_concatcopy(list: TAsmList; size: tdef; const source, dest: treference);
  1347. var
  1348. handled: boolean;
  1349. begin
  1350. handled:=false;
  1351. case size.typ of
  1352. arraydef:
  1353. begin
  1354. if not is_dynamic_array(size) then
  1355. begin
  1356. concatcopy_normal_array(list,size,source,dest);
  1357. handled:=true;
  1358. end;
  1359. end;
  1360. recorddef:
  1361. begin
  1362. concatcopy_record(list,size,source,dest);
  1363. handled:=true;
  1364. end;
  1365. setdef:
  1366. begin
  1367. concatcopy_set(list,size,source,dest);
  1368. handled:=true;
  1369. end;
  1370. stringdef:
  1371. begin
  1372. if is_shortstring(size) then
  1373. begin
  1374. concatcopy_shortstring(list,size,source,dest);
  1375. handled:=true;
  1376. end;
  1377. end;
  1378. procvardef:
  1379. begin
  1380. if not tprocvardef(size).is_addressonly then
  1381. begin
  1382. concatcopy_record(list,tcpuprocvardef(size).classdef,source,dest);
  1383. handled:=true;
  1384. end;
  1385. end;
  1386. else
  1387. ;
  1388. end;
  1389. if not handled then
  1390. inherited;
  1391. end;
  1392. procedure thlcgwasm.g_copyshortstring(list: TAsmList; const source, dest: treference; strdef: tstringdef);
  1393. begin
  1394. concatcopy_shortstring(list,strdef,source,dest);
  1395. end;
  1396. procedure thlcgwasm.a_loadfpu_ref_ref(list: TAsmList; fromsize, tosize: tdef; const ref1, ref2: treference);
  1397. var
  1398. dstack_slots: longint;
  1399. begin
  1400. dstack_slots:=prepare_stack_for_ref(list,ref2,false);
  1401. a_load_ref_stack(list,fromsize,ref1,prepare_stack_for_ref(list,ref1,false));
  1402. resizestackfpuval(list,def_cgsize(fromsize),def_cgsize(tosize));
  1403. a_load_stack_ref(list,tosize,ref2,dstack_slots);
  1404. end;
  1405. procedure thlcgwasm.a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; reg: tregister);
  1406. begin
  1407. a_load_ref_stack(list,fromsize,ref,prepare_stack_for_ref(list,ref,false));
  1408. resizestackfpuval(list,def_cgsize(fromsize),def_cgsize(tosize));
  1409. a_load_stack_reg(list,tosize,reg);
  1410. end;
  1411. procedure thlcgwasm.a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tdef; reg: tregister; const ref: treference);
  1412. var
  1413. dstack_slots: longint;
  1414. begin
  1415. dstack_slots:=prepare_stack_for_ref(list,ref,false);
  1416. a_load_reg_stack(list,fromsize,reg);
  1417. resizestackfpuval(list,def_cgsize(fromsize),def_cgsize(tosize));
  1418. a_load_stack_ref(list,tosize,ref,dstack_slots);
  1419. end;
  1420. procedure thlcgwasm.a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tdef; reg1, reg2: tregister);
  1421. begin
  1422. a_load_reg_stack(list,fromsize,reg1);
  1423. resizestackfpuval(list,def_cgsize(fromsize),def_cgsize(tosize));
  1424. a_load_stack_reg(list,tosize,reg2);
  1425. end;
  1426. procedure thlcgwasm.g_proc_entry(list: TAsmList; localsize: longint; nostackframe: boolean);
  1427. begin
  1428. { the localsize is based on tg.lasttemp -> already in terms of stack
  1429. slots rather than bytes }
  1430. list.concat(tai_directive.Create(asd_jlimit,'locals '+tostr(localsize)));
  1431. { we insert the unit initialisation code afterwards in the proginit code,
  1432. and it uses one stack slot }
  1433. if (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1434. fmaxevalstackheight:=max(1,fmaxevalstackheight);
  1435. list.concat(tai_directive.Create(asd_jlimit,'stack '+tostr(fmaxevalstackheight)));
  1436. end;
  1437. procedure thlcgwasm.g_proc_exit(list: TAsmList; parasize: longint; nostackframe: boolean);
  1438. begin
  1439. list.concat(taicpu.op_none(a_return));
  1440. end;
  1441. procedure thlcgwasm.gen_load_return_value(list: TAsmList);
  1442. begin
  1443. { constructors don't return anything in the jvm }
  1444. if current_procinfo.procdef.proctypeoption in [potype_constructor,potype_class_constructor] then
  1445. exit;
  1446. inherited gen_load_return_value(list);
  1447. end;
  1448. procedure thlcgwasm.record_generated_code_for_procdef(pd: tprocdef; code, data: TAsmList);
  1449. begin
  1450. { add something to the al_procedures list as well, because if all al_*
  1451. lists are empty, the assembler writer isn't called }
  1452. if not code.empty and
  1453. current_asmdata.asmlists[al_procedures].empty then
  1454. current_asmdata.asmlists[al_procedures].concat(tai_align.Create(4));
  1455. tcpuprocdef(pd).exprasmlist:=TAsmList.create;
  1456. tcpuprocdef(pd).exprasmlist.concatlist(code);
  1457. if assigned(data) and
  1458. not data.empty then
  1459. internalerror(2010122801);
  1460. end;
  1461. procedure thlcgwasm.g_incrrefcount(list: TAsmList; t: tdef; const ref: treference);
  1462. begin
  1463. // do nothing
  1464. end;
  1465. procedure thlcgwasm.g_array_rtti_helper(list: TAsmList; t: tdef; const ref: treference; const highloc: tlocation; const name: string);
  1466. var
  1467. normaldim: longint;
  1468. eleref: treference;
  1469. begin
  1470. { only in case of initialisation, we have to set all elements to "empty" }
  1471. if name<>'fpc_initialize_array' then
  1472. exit;
  1473. { put array on the stack }
  1474. a_load_ref_stack(list,java_jlobject,ref,prepare_stack_for_ref(list,ref,false));
  1475. { in case it's an open array whose elements are regular arrays, put the
  1476. dimension of the regular arrays on the stack (otherwise pass 0) }
  1477. normaldim:=0;
  1478. while (t.typ=arraydef) and
  1479. not is_dynamic_array(t) do
  1480. begin
  1481. inc(normaldim);
  1482. t:=tarraydef(t).elementdef;
  1483. end;
  1484. a_load_const_stack(list,s32inttype,normaldim,R_INTREGISTER);
  1485. { highloc is invalid, the length is part of the array in Java }
  1486. if is_wide_or_unicode_string(t) then
  1487. g_call_system_proc(list,'fpc_initialize_array_unicodestring',[],nil)
  1488. else if is_ansistring(t) then
  1489. g_call_system_proc(list,'fpc_initialize_array_ansistring',[],nil)
  1490. else if is_dynamic_array(t) then
  1491. g_call_system_proc(list,'fpc_initialize_array_dynarr',[],nil)
  1492. else if is_record(t) or
  1493. (t.typ=setdef) then
  1494. begin
  1495. tg.gethltemp(list,t,t.size,tt_persistent,eleref);
  1496. a_load_ref_stack(list,t,eleref,prepare_stack_for_ref(list,eleref,false));
  1497. if is_record(t) then
  1498. g_call_system_proc(list,'fpc_initialize_array_record',[],nil)
  1499. else if tsetdef(t).elementdef.typ=enumdef then
  1500. g_call_system_proc(list,'fpc_initialize_array_enumset',[],nil)
  1501. else
  1502. g_call_system_proc(list,'fpc_initialize_array_bitset',[],nil);
  1503. tg.ungettemp(list,eleref);
  1504. end
  1505. else if (t.typ=enumdef) then
  1506. begin
  1507. if get_enum_init_val_ref(t,eleref) then
  1508. begin
  1509. a_load_ref_stack(list,java_jlobject,eleref,prepare_stack_for_ref(list,eleref,false));
  1510. g_call_system_proc(list,'fpc_initialize_array_object',[],nil);
  1511. end;
  1512. end
  1513. else
  1514. internalerror(2011031901);
  1515. end;
  1516. procedure thlcgwasm.g_initialize(list: TAsmList; t: tdef; const ref: treference);
  1517. var
  1518. dummyloc: tlocation;
  1519. sym: tsym;
  1520. pd: tprocdef;
  1521. begin
  1522. if (t.typ=arraydef) and
  1523. not is_dynamic_array(t) then
  1524. begin
  1525. dummyloc.loc:=LOC_INVALID;
  1526. g_array_rtti_helper(list,tarraydef(t).elementdef,ref,dummyloc,'fpc_initialize_array')
  1527. end
  1528. else if is_record(t) then
  1529. begin
  1530. { call the fpcInitializeRec method }
  1531. sym:=tsym(trecorddef(t).symtable.find('FPCINITIALIZEREC'));
  1532. if assigned(sym) and
  1533. (sym.typ=procsym) then
  1534. begin
  1535. if tprocsym(sym).procdeflist.Count<>1 then
  1536. internalerror(2011071713);
  1537. pd:=tprocdef(tprocsym(sym).procdeflist[0]);
  1538. end
  1539. else
  1540. internalerror(2013113008);
  1541. a_load_ref_stack(list,java_jlobject,ref,prepare_stack_for_ref(list,ref,false));
  1542. a_call_name(list,pd,pd.mangledname,[],nil,false);
  1543. { parameter removed, no result }
  1544. decstack(list,1);
  1545. end
  1546. else
  1547. a_load_const_ref(list,t,0,ref);
  1548. end;
  1549. procedure thlcgwasm.g_finalize(list: TAsmList; t: tdef; const ref: treference);
  1550. begin
  1551. // do nothing
  1552. end;
  1553. procedure thlcgwasm.g_overflowcheck(list: TAsmList; const Loc: tlocation; def: tdef);
  1554. begin
  1555. { not possible, need the original operands }
  1556. internalerror(2012102101);
  1557. end;
  1558. procedure thlcgwasm.g_overflowCheck_loc(List: TAsmList; const Loc: TLocation; def: TDef; var ovloc: tlocation);
  1559. var
  1560. hl : tasmlabel;
  1561. begin
  1562. if not(cs_check_overflow in current_settings.localswitches) then
  1563. exit;
  1564. current_asmdata.getjumplabel(hl);
  1565. a_cmp_const_loc_label(list,s32inttype,OC_EQ,0,ovloc,hl);
  1566. g_call_system_proc(list,'fpc_overflow',[],nil);
  1567. a_label(list,hl);
  1568. end;
  1569. procedure thlcgwasm.location_get_data_ref(list: TAsmList; def: tdef; const l: tlocation; var ref: treference; loadref: boolean; alignment: longint);
  1570. var
  1571. tmploc: tlocation;
  1572. begin
  1573. { This routine is a combination of a generalised a_loadaddr_ref_reg()
  1574. that also works for addresses in registers (in case loadref is false)
  1575. and of a_load_ref_reg (in case loadref is true). It is used for
  1576. a) getting the address of managed var/out parameters
  1577. b) getting to the actual data of value types that are passed by
  1578. reference by the compiler (and then get a local copy at the caller
  1579. side). Normally, depending on whether this reference is passed in a
  1580. register or reference, we either need a reference with that register
  1581. as base or load the address in that reference and use that as a new
  1582. base.
  1583. Since the JVM cannot take the address of anything, all
  1584. "pass-by-reference" value parameters (which are always aggregate types)
  1585. are already simply the implicit pointer to the data (since arrays,
  1586. records, etc are already internally implicit pointers). This means
  1587. that if "loadref" is true, we must simply return this implicit pointer.
  1588. If it is false, we are supposed the take the address of this implicit
  1589. pointer, which is not possible.
  1590. However, managed types are also implicit pointers in Pascal, so in that
  1591. case "taking the address" again consists of simply returning the
  1592. implicit pointer/current value (in case of a var/out parameter, this
  1593. value is stored inside an array).
  1594. }
  1595. if not loadref then
  1596. begin
  1597. if not is_managed_type(def) then
  1598. internalerror(2011020601);
  1599. tmploc:=l;
  1600. end
  1601. else
  1602. begin
  1603. if not wasmimplicitpointertype(def) then
  1604. begin
  1605. { passed by reference in array of single element; l contains the
  1606. base address of the array }
  1607. location_reset_ref(tmploc,LOC_REFERENCE,OS_ADDR,4,ref.volatility);
  1608. cgutils.reference_reset_base(tmploc.reference,getaddressregister(list,java_jlobject),0,tmploc.reference.temppos,4,ref.volatility);
  1609. a_load_loc_reg(list,java_jlobject,java_jlobject,l,tmploc.reference.base);
  1610. end
  1611. else
  1612. tmploc:=l;
  1613. end;
  1614. case tmploc.loc of
  1615. LOC_REGISTER,
  1616. LOC_CREGISTER :
  1617. begin
  1618. { the implicit pointer is in a register and has to be in a
  1619. reference -> create a reference and put it there }
  1620. location_force_mem(list,tmploc,java_jlobject);
  1621. ref:=tmploc.reference;
  1622. end;
  1623. LOC_REFERENCE,
  1624. LOC_CREFERENCE :
  1625. begin
  1626. ref:=tmploc.reference;
  1627. end;
  1628. else
  1629. internalerror(2011020603);
  1630. end;
  1631. end;
  1632. procedure thlcgwasm.maybe_change_load_node_reg(list: TAsmList; var n: tnode; reload: boolean);
  1633. begin
  1634. { don't do anything, all registers become stack locations anyway }
  1635. end;
  1636. procedure thlcgwasm.g_copyvaluepara_openarray(list: TAsmList; const ref: treference; const lenloc: tlocation; arrdef: tarraydef; destreg: tregister);
  1637. var
  1638. localref: treference;
  1639. arrloc: tlocation;
  1640. stackslots: longint;
  1641. begin
  1642. { temporary reference for passing to concatcopy }
  1643. tg.gethltemp(list,java_jlobject,java_jlobject.size,tt_persistent,localref);
  1644. stackslots:=prepare_stack_for_ref(list,localref,false);
  1645. { create the local copy of the array (lenloc is invalid, get length
  1646. directly from the array) }
  1647. location_reset_ref(arrloc,LOC_REFERENCE,OS_ADDR,sizeof(pint),ref.volatility);
  1648. arrloc.reference:=ref;
  1649. g_getarraylen(list,arrloc);
  1650. g_newarray(list,arrdef,1);
  1651. a_load_stack_ref(list,java_jlobject,localref,stackslots);
  1652. { copy the source array to the destination }
  1653. g_concatcopy(list,arrdef,ref,localref);
  1654. { and put the array pointer in the register as expected by the caller }
  1655. a_load_ref_reg(list,java_jlobject,java_jlobject,localref,destreg);
  1656. end;
  1657. procedure thlcgwasm.g_releasevaluepara_openarray(list: TAsmList; arrdef: tarraydef; const l: tlocation);
  1658. begin
  1659. // do nothing, long live garbage collection!
  1660. end;
  1661. procedure thlcgwasm.gen_initialize_code(list: TAsmList);
  1662. var
  1663. ref: treference;
  1664. begin
  1665. { create globals with wrapped types such as arrays/records }
  1666. case current_procinfo.procdef.proctypeoption of
  1667. potype_unitinit:
  1668. begin
  1669. cgutils.reference_reset_base(ref,NR_NO,0,ctempposinvalid,1,[]);
  1670. if assigned(current_module.globalsymtable) then
  1671. allocate_implicit_structs_for_st_with_base_ref(list,current_module.globalsymtable,ref,staticvarsym);
  1672. allocate_implicit_structs_for_st_with_base_ref(list,current_module.localsymtable,ref,staticvarsym);
  1673. end;
  1674. potype_class_constructor:
  1675. begin
  1676. { also initialise local variables, if any }
  1677. inherited;
  1678. { initialise class fields }
  1679. cgutils.reference_reset_base(ref,NR_NO,0,ctempposinvalid,1,[]);
  1680. allocate_implicit_structs_for_st_with_base_ref(list,tabstractrecorddef(current_procinfo.procdef.owner.defowner).symtable,ref,staticvarsym);
  1681. end
  1682. else
  1683. inherited
  1684. end;
  1685. end;
  1686. procedure thlcgwasm.gen_entry_code(list: TAsmList);
  1687. begin
  1688. list.concat(Tai_force_line.Create);
  1689. end;
  1690. procedure thlcgwasm.gen_exit_code(list: TAsmList);
  1691. begin
  1692. { nothing }
  1693. end;
  1694. procedure thlcgwasm.a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; srcsize, dstsize: tdef; src, dst: tregister);
  1695. begin
  1696. internalerror(2012090201);
  1697. end;
  1698. procedure thlcgwasm.a_loadmm_loc_reg(list: TAsmList; fromsize, tosize: tdef; const loc: tlocation; const reg: tregister; shuffle: pmmshuffle);
  1699. begin
  1700. internalerror(2012090202);
  1701. end;
  1702. procedure thlcgwasm.a_loadmm_reg_reg(list: TAsmList; fromsize, tosize: tdef; reg1, reg2: tregister; shuffle: pmmshuffle);
  1703. begin
  1704. internalerror(2012060130);
  1705. end;
  1706. procedure thlcgwasm.a_loadmm_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; reg: tregister; shuffle: pmmshuffle);
  1707. begin
  1708. internalerror(2012060131);
  1709. end;
  1710. procedure thlcgwasm.a_loadmm_reg_ref(list: TAsmList; fromsize, tosize: tdef; reg: tregister; const ref: treference; shuffle: pmmshuffle);
  1711. begin
  1712. internalerror(2012060132);
  1713. end;
  1714. procedure thlcgwasm.a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size: tdef; src, dst: tregister; shuffle: pmmshuffle);
  1715. begin
  1716. internalerror(2012060133);
  1717. end;
  1718. procedure thlcgwasm.a_loadmm_intreg_reg(list: TAsmList; fromsize, tosize: tdef; intreg, mmreg: tregister; shuffle: pmmshuffle);
  1719. begin
  1720. internalerror(2012060134);
  1721. end;
  1722. procedure thlcgwasm.a_loadmm_reg_intreg(list: TAsmList; fromsize, tosize: tdef; mmreg, intreg: tregister; shuffle: pmmshuffle);
  1723. begin
  1724. internalerror(2012060135);
  1725. end;
  1726. procedure thlcgwasm.g_stackpointer_alloc(list: TAsmList; size: longint);
  1727. begin
  1728. internalerror(2012090203);
  1729. end;
  1730. procedure thlcgwasm.g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);
  1731. begin
  1732. internalerror(2012090204);
  1733. end;
  1734. procedure thlcgwasm.g_adjust_self_value(list: TAsmList; procdef: tprocdef; ioffset: aint);
  1735. begin
  1736. internalerror(2012090205);
  1737. end;
  1738. procedure thlcgwasm.g_local_unwind(list: TAsmList; l: TAsmLabel);
  1739. begin
  1740. internalerror(2012090206);
  1741. end;
  1742. procedure thlcgwasm.a_load_stack_reg(list: TAsmList; size: tdef; reg: tregister);
  1743. var
  1744. opc: tasmop;
  1745. finishandval: tcgint;
  1746. begin
  1747. opc:=loadstoreopc(size,false,false,finishandval);
  1748. list.concat(taicpu.op_reg(opc,reg));
  1749. { avoid problems with getting the size of an open array etc }
  1750. if wasmimplicitpointertype(size) then
  1751. size:=ptruinttype;
  1752. decstack(list,1);
  1753. end;
  1754. procedure thlcgwasm.a_load_stack_ref(list: TAsmList; size: tdef; const ref: treference; extra_slots: longint);
  1755. var
  1756. opc: tasmop;
  1757. finishandval: tcgint;
  1758. begin
  1759. { fake location that indicates the value has to remain on the stack }
  1760. if ref.base=NR_EVAL_STACK_BASE then
  1761. exit;
  1762. opc:=loadstoreopcref(size,false,ref,finishandval);
  1763. list.concat(taicpu.op_ref(opc,ref));
  1764. { avoid problems with getting the size of an open array etc }
  1765. if wasmimplicitpointertype(size) then
  1766. size:=ptruinttype;
  1767. decstack(list,1+extra_slots);
  1768. end;
  1769. procedure thlcgwasm.a_load_reg_stack(list: TAsmList; size: tdef; reg: tregister);
  1770. var
  1771. opc: tasmop;
  1772. finishandval: tcgint;
  1773. begin
  1774. opc:=loadstoreopc(size,true,false,finishandval);
  1775. list.concat(taicpu.op_reg(opc,reg));
  1776. { avoid problems with getting the size of an open array etc }
  1777. if wasmimplicitpointertype(size) then
  1778. size:=ptruinttype;
  1779. incstack(list,1);
  1780. if finishandval<>-1 then
  1781. a_op_const_stack(list,OP_AND,size,finishandval);
  1782. end;
  1783. procedure thlcgwasm.a_load_ref_stack(list: TAsmList; size: tdef; const ref: treference; extra_slots: longint);
  1784. var
  1785. opc: tasmop;
  1786. finishandval: tcgint;
  1787. begin
  1788. { fake location that indicates the value is already on the stack? }
  1789. if (ref.base=NR_EVAL_STACK_BASE) then
  1790. exit;
  1791. opc:=loadstoreopcref(size,true,ref,finishandval);
  1792. list.concat(taicpu.op_ref(opc,ref));
  1793. { avoid problems with getting the size of an open array etc }
  1794. if wasmimplicitpointertype(size) then
  1795. size:=ptruinttype;
  1796. incstack(list,1-extra_slots);
  1797. if finishandval<>-1 then
  1798. a_op_const_stack(list,OP_AND,size,finishandval);
  1799. // there's no cast check in Wasm
  1800. //if ref.checkcast then
  1801. // gen_typecheck(list,a_checkcast,size);
  1802. end;
  1803. function thlcgwasm.loadstoreopcref(def: tdef; isload: boolean; const ref: treference; out finishandval: tcgint): tasmop;
  1804. const
  1805. {iisload} {issigned}
  1806. getputmem8 : array [boolean, boolean] of TAsmOp = ((a_i32_store8, a_i32_store8), (a_i32_load8_u, a_i32_load8_s));
  1807. getputmem16 : array [boolean, boolean] of TAsmOp = ((a_i32_store16, a_i32_store16), (a_i32_load16_u ,a_i32_load16_s));
  1808. getputmem32 : array [boolean, boolean] of TAsmOp = ((a_i32_store, a_i32_store), (a_i32_load, a_i32_load));
  1809. getputmem64 : array [boolean, boolean] of TAsmOp = ((a_i64_store, a_i64_store), (a_i64_load, a_i64_load));
  1810. getputmemf32 : array [boolean] of TAsmOp = (a_f32_store, a_f32_load);
  1811. getputmemf64 : array [boolean] of TAsmOp = (a_f64_store, a_f64_load);
  1812. begin
  1813. if (not ref.islocal) or assigned(ref.symbol) then
  1814. begin
  1815. { -> either a global (static) field, or a regular field. If a regular
  1816. field, then ref.base contains the self pointer, otherwise
  1817. ref.base=NR_NO. In both cases, the symbol contains all other
  1818. information (combined field name and type descriptor) }
  1819. case def.size of
  1820. 1: result := getputmem8[isload, is_signed(def)];
  1821. 2: result := getputmem16[isload, is_signed(def)];
  1822. 4:
  1823. if is_single(def) then
  1824. result := getputmemf32[isload]
  1825. else
  1826. result := getputmem32[isload, is_signed(def)];
  1827. 8: if is_double(def) then
  1828. result := getputmemf64[isload]
  1829. else
  1830. result := getputmem64[isload, is_signed(def)];
  1831. else
  1832. Internalerror(201909150001);
  1833. end;
  1834. //result:=getputopc[isload,ref.base=NR_NO];
  1835. finishandval:=-1;
  1836. { erase sign extension for byte/smallint loads }
  1837. if (def2regtyp(def)=R_INTREGISTER) and
  1838. not is_signed(def) and
  1839. (def.typ=orddef) and
  1840. not is_widechar(def) then
  1841. case def.size of
  1842. 1: if (torddef(def).high>127) then
  1843. finishandval:=255;
  1844. 2: if (torddef(def).high>32767) then
  1845. finishandval:=65535;
  1846. end;
  1847. end
  1848. else
  1849. result:=loadstoreopc(def,isload,false,finishandval);
  1850. end;
  1851. function thlcgwasm.loadstoreopc(def: tdef; isload, isarray: boolean; out finishandval: tcgint): tasmop;
  1852. var
  1853. size: longint;
  1854. begin
  1855. finishandval:=-1;
  1856. if isload then result := a_get_local
  1857. else result := a_set_local;
  1858. {case def2regtyp(def) of
  1859. R_INTREGISTER:
  1860. begin
  1861. size:=def.size;
  1862. case size of
  1863. 1,2,3,4:
  1864. if isload then
  1865. result:=a_i32_load
  1866. else
  1867. result:=a_i32_store;
  1868. 8:
  1869. if isload then
  1870. result:=a_i64_load
  1871. else
  1872. result:=a_i64_store;
  1873. else
  1874. internalerror(2011032814);
  1875. end;
  1876. end;
  1877. R_ADDRESSREGISTER:
  1878. if isload then
  1879. result:=a_i32_load
  1880. else
  1881. result:=a_i32_store;
  1882. R_FPUREGISTER:
  1883. begin
  1884. case tfloatdef(def).floattype of
  1885. s32real:
  1886. if isload then
  1887. result:=a_f32_load
  1888. else
  1889. result:=a_f32_store;
  1890. s64real:
  1891. if isload then
  1892. result:=a_f32_load
  1893. else
  1894. result:=a_f32_store
  1895. else
  1896. internalerror(2010120504);
  1897. end
  1898. end
  1899. else
  1900. internalerror(2010120502);
  1901. end;}
  1902. end;
  1903. procedure thlcgwasm.resize_stack_int_val(list: TAsmList; fromsize, tosize: tdef; formemstore: boolean);
  1904. var
  1905. fromcgsize, tocgsize: tcgsize;
  1906. begin
  1907. { When storing to an array, field or global variable, make sure the
  1908. static type verification can determine that the stored value fits
  1909. within the boundaries of the declared type (to appease the Dalvik VM).
  1910. Local variables either get their type upgraded in the debug info,
  1911. or have no type information at all }
  1912. if formemstore and
  1913. (tosize.typ=orddef) then
  1914. if (torddef(tosize).ordtype in [u8bit,uchar]) then
  1915. tosize:=s8inttype
  1916. else if torddef(tosize).ordtype=u16bit then
  1917. tosize:=s16inttype;
  1918. fromcgsize:=def_cgsize(fromsize);
  1919. tocgsize:=def_cgsize(tosize);
  1920. if fromcgsize in [OS_S64,OS_64] then
  1921. begin
  1922. if not(tocgsize in [OS_S64,OS_64]) then
  1923. begin
  1924. { truncate }
  1925. list.concat(taicpu.op_none(a_i32_wrap_i64));
  1926. decstack(list,1);
  1927. end;
  1928. end
  1929. else if tocgsize in [OS_S64,OS_64] then
  1930. begin
  1931. { extend }
  1932. if tocgsize = OS_S64 then
  1933. list.concat(taicpu.op_none(a_i64_extend_s_i32))
  1934. else
  1935. list.concat(taicpu.op_none(a_i64_extend_u_i32));
  1936. incstack(list,1);
  1937. { if it was an unsigned 32 bit value, remove sign extension }
  1938. if fromcgsize=OS_32 then
  1939. a_op_const_stack(list,OP_AND,s64inttype,cardinal($ffffffff));
  1940. end;
  1941. { Conversions between 32 and 64 bit types have been completely handled
  1942. above. We still may have to truncate or sign extend in case the
  1943. destination type is smaller that the source type, or has a different
  1944. sign. In case the destination is a widechar and the source is not, we
  1945. also have to insert a conversion to widechar.
  1946. }
  1947. if (not(fromcgsize in [OS_S64,OS_64,OS_32,OS_S32]) or
  1948. not(tocgsize in [OS_S64,OS_64,OS_32,OS_S32])) and
  1949. ((tcgsize2size[fromcgsize]>tcgsize2size[tocgsize]) or
  1950. ((tcgsize2size[fromcgsize]=tcgsize2size[tocgsize]) and
  1951. (fromcgsize<>tocgsize)) or
  1952. { needs to mask out the sign in the top 16 bits }
  1953. ((fromcgsize=OS_S8) and
  1954. (tocgsize=OS_16)) or
  1955. ((tosize=cwidechartype) and
  1956. (fromsize<>cwidechartype))) then
  1957. case tocgsize of
  1958. OS_8:
  1959. //todo: conversion
  1960. //a_op_const_stack(list,OP_AND,s32inttype,255);
  1961. ;
  1962. OS_S8:
  1963. //todo: conversion
  1964. //list.concat(taicpu.op_none(a_i2b));
  1965. ;
  1966. OS_16:
  1967. //todo: conversion
  1968. //if (tosize.typ=orddef) and
  1969. // (torddef(tosize).ordtype=uwidechar) then
  1970. // list.concat(taicpu.op_none(a_i2c))
  1971. //else
  1972. // a_op_const_stack(list,OP_AND,s32inttype,65535);
  1973. ;
  1974. OS_S16:
  1975. //todo: conversion
  1976. //list.concat(taicpu.op_none(a_i2s));
  1977. ;
  1978. else
  1979. ;
  1980. end;
  1981. end;
  1982. procedure thlcgwasm.maybe_resize_stack_para_val(list: TAsmList; retdef: tdef; callside: boolean);
  1983. var
  1984. convsize: tdef;
  1985. begin
  1986. if (retdef.typ=orddef) then
  1987. begin
  1988. if (torddef(retdef).ordtype in [u8bit,u16bit,uchar]) and
  1989. (torddef(retdef).high>=(1 shl (retdef.size*8-1))) then
  1990. begin
  1991. convsize:=nil;
  1992. if callside then
  1993. if torddef(retdef).ordtype in [u8bit,uchar] then
  1994. convsize:=s8inttype
  1995. else
  1996. convsize:=s16inttype
  1997. else if torddef(retdef).ordtype in [u8bit,uchar] then
  1998. convsize:=u8inttype
  1999. else
  2000. convsize:=u16inttype;
  2001. if assigned(convsize) then
  2002. resize_stack_int_val(list,s32inttype,convsize,false);
  2003. end;
  2004. end;
  2005. end;
  2006. procedure thlcgwasm.g_adjust_stack_after_call(list: TAsmList; pd: tabstractprocdef; paraheight: longint; forceresdef: tdef);
  2007. var
  2008. totalremovesize: longint;
  2009. realresdef: tdef;
  2010. begin
  2011. if not assigned(forceresdef) then
  2012. realresdef:=pd.returndef
  2013. else
  2014. realresdef:=forceresdef;
  2015. { a constructor doesn't actually return a value in the jvm }
  2016. if (tabstractprocdef(pd).proctypeoption=potype_constructor) then
  2017. totalremovesize:=paraheight
  2018. else
  2019. { even a byte takes up a full stackslot -> align size to multiple of 4 }
  2020. totalremovesize:=paraheight-(align(realresdef.size,4) shr 2);
  2021. { remove parameters from internal evaluation stack counter (in case of
  2022. e.g. no parameters and a result, it can also increase) }
  2023. if totalremovesize>0 then
  2024. decstack(list,totalremovesize)
  2025. else if totalremovesize<0 then
  2026. incstack(list,-totalremovesize);
  2027. end;
  2028. procedure thlcgwasm.allocate_implicit_struct_with_base_ref(list: TAsmList; vs: tabstractvarsym; ref: treference);
  2029. var
  2030. tmpref: treference;
  2031. begin
  2032. ref.symbol:=current_asmdata.RefAsmSymbol(vs.mangledname,AT_DATA);
  2033. tg.gethltemp(list,vs.vardef,vs.vardef.size,tt_persistent,tmpref);
  2034. { only copy the reference, not the actual data }
  2035. a_load_ref_ref(list,ptruinttype,java_jlobject,tmpref,ref);
  2036. { remains live since there's still a reference to the created
  2037. entity }
  2038. tg.ungettemp(list,tmpref);
  2039. end;
  2040. procedure thlcgwasm.allocate_enum_with_base_ref(list: TAsmList; vs: tabstractvarsym; const initref: treference; destbaseref: treference);
  2041. begin
  2042. destbaseref.symbol:=current_asmdata.RefAsmSymbol(vs.mangledname,AT_DATA);
  2043. { only copy the reference, not the actual data }
  2044. a_load_ref_ref(list,ptruinttype,java_jlobject,initref,destbaseref);
  2045. end;
  2046. function thlcgwasm.get_enum_init_val_ref(def: tdef; out ref: treference): boolean;
  2047. var
  2048. sym: tstaticvarsym;
  2049. begin
  2050. result:=false;
  2051. sym:=tstaticvarsym(tcpuenumdef(tenumdef(def).getbasedef).classdef.symtable.Find('__FPC_ZERO_INITIALIZER'));
  2052. { no enum with ordinal value 0 -> exit }
  2053. if not assigned(sym) then
  2054. exit;
  2055. reference_reset_symbol(ref,current_asmdata.RefAsmSymbol(sym.mangledname,AT_DATA),0,4,[]);
  2056. result:=true;
  2057. end;
  2058. procedure thlcgwasm.allocate_implicit_structs_for_st_with_base_ref(list: TAsmList; st: tsymtable; const ref: treference; allocvartyp: tsymtyp);
  2059. var
  2060. vs: tabstractvarsym;
  2061. def: tdef;
  2062. i: longint;
  2063. initref: treference;
  2064. begin
  2065. for i:=0 to st.symlist.count-1 do
  2066. begin
  2067. if (tsym(st.symlist[i]).typ<>allocvartyp) then
  2068. continue;
  2069. vs:=tabstractvarsym(st.symlist[i]);
  2070. if sp_static in vs.symoptions then
  2071. continue;
  2072. { vo_is_external and vo_has_local_copy means a staticvarsym that is
  2073. alias for a constsym, whose sole purpose is for allocating and
  2074. intialising the constant }
  2075. if [vo_is_external,vo_has_local_copy]*vs.varoptions=[vo_is_external] then
  2076. continue;
  2077. { threadvar innitializations are handled at the node tree level }
  2078. if vo_is_thread_var in vs.varoptions then
  2079. begin
  2080. { nothing }
  2081. end
  2082. else if wasmimplicitpointertype(vs.vardef) then
  2083. allocate_implicit_struct_with_base_ref(list,vs,ref)
  2084. { enums are class instances in Java, while they are ordinals in
  2085. Pascal. When they are initialized with enum(0), such as in
  2086. constructors or global variables, initialize them with the
  2087. enum instance for 0 if it exists (if not, it remains nil since
  2088. there is no valid enum value in it) }
  2089. else if (vs.vardef.typ=enumdef) and
  2090. ((vs.typ<>fieldvarsym) or
  2091. (tdef(vs.owner.defowner).typ<>objectdef) or
  2092. (ts_jvm_enum_field_init in current_settings.targetswitches)) and
  2093. get_enum_init_val_ref(vs.vardef,initref) then
  2094. allocate_enum_with_base_ref(list,vs,initref,ref);
  2095. end;
  2096. { process symtables of routines part of this symtable (for local typed
  2097. constants) }
  2098. if allocvartyp=staticvarsym then
  2099. begin
  2100. for i:=0 to st.deflist.count-1 do
  2101. begin
  2102. def:=tdef(st.deflist[i]);
  2103. { the unit symtable also contains the methods of classes defined
  2104. in that unit -> skip them when processing the unit itself.
  2105. Localst is not assigned for the main program code.
  2106. Localst can be the same as st in case of unit init code. }
  2107. if (def.typ<>procdef) or
  2108. (def.owner<>st) or
  2109. not assigned(tprocdef(def).localst) or
  2110. (tprocdef(def).localst=st) then
  2111. continue;
  2112. allocate_implicit_structs_for_st_with_base_ref(list,tprocdef(def).localst,ref,allocvartyp);
  2113. end;
  2114. end;
  2115. end;
  2116. procedure thlcgwasm.gen_initialize_fields_code(list: TAsmList);
  2117. var
  2118. sym: tsym;
  2119. selfpara: tparavarsym;
  2120. selfreg: tregister;
  2121. ref: treference;
  2122. obj: tabstractrecorddef;
  2123. i: longint;
  2124. needinit: boolean;
  2125. begin
  2126. obj:=tabstractrecorddef(current_procinfo.procdef.owner.defowner);
  2127. { check whether there are any fields that need initialisation }
  2128. needinit:=false;
  2129. for i:=0 to obj.symtable.symlist.count-1 do
  2130. begin
  2131. sym:=tsym(obj.symtable.symlist[i]);
  2132. if (sym.typ=fieldvarsym) and
  2133. not(sp_static in sym.symoptions) and
  2134. (wasmimplicitpointertype(tfieldvarsym(sym).vardef) or
  2135. ((tfieldvarsym(sym).vardef.typ=enumdef) and
  2136. get_enum_init_val_ref(tfieldvarsym(sym).vardef,ref))) then
  2137. begin
  2138. needinit:=true;
  2139. break;
  2140. end;
  2141. end;
  2142. if not needinit then
  2143. exit;
  2144. selfpara:=tparavarsym(current_procinfo.procdef.parast.find('self'));
  2145. if not assigned(selfpara) then
  2146. internalerror(2011033001);
  2147. selfreg:=getaddressregister(list,selfpara.vardef);
  2148. a_load_loc_reg(list,obj,obj,selfpara.localloc,selfreg);
  2149. cgutils.reference_reset_base(ref,selfreg,0,ctempposinvalid,1,[]);
  2150. allocate_implicit_structs_for_st_with_base_ref(list,obj.symtable,ref,fieldvarsym);
  2151. end;
  2152. procedure thlcgwasm.gen_typecheck(list: TAsmList; checkop: tasmop; checkdef: tdef);
  2153. begin
  2154. { replace special types with their equivalent class type }
  2155. if (checkdef.typ=pointerdef) and
  2156. wasmimplicitpointertype(tpointerdef(checkdef).pointeddef) then
  2157. checkdef:=tpointerdef(checkdef).pointeddef;
  2158. if (checkdef=voidpointertype) or
  2159. (checkdef.typ=formaldef) then
  2160. checkdef:=java_jlobject
  2161. else if checkdef.typ=enumdef then
  2162. checkdef:=tcpuenumdef(checkdef).classdef
  2163. else if checkdef.typ=setdef then
  2164. begin
  2165. if tsetdef(checkdef).elementdef.typ=enumdef then
  2166. checkdef:=java_juenumset
  2167. else
  2168. checkdef:=java_jubitset;
  2169. end
  2170. else if checkdef.typ=procvardef then
  2171. checkdef:=tcpuprocvardef(checkdef).classdef
  2172. else if is_wide_or_unicode_string(checkdef) then
  2173. checkdef:=java_jlstring
  2174. else if is_ansistring(checkdef) then
  2175. checkdef:=java_ansistring
  2176. else if is_shortstring(checkdef) then
  2177. checkdef:=java_shortstring;
  2178. if checkdef.typ in [objectdef,recorddef] then
  2179. list.concat(taicpu.op_sym(checkop,current_asmdata.RefAsmSymbol(tabstractrecorddef(checkdef).jvm_full_typename(true),AT_METADATA)))
  2180. else if checkdef.typ=classrefdef then
  2181. list.concat(taicpu.op_sym(checkop,current_asmdata.RefAsmSymbol('java/lang/Class',AT_METADATA)))
  2182. { todo: WASM
  2183. else
  2184. list.concat(taicpu.op_sym(checkop,current_asmdata.RefAsmSymbol(jvmencodetype(checkdef,false),AT_METADATA)));
  2185. }
  2186. end;
  2187. procedure thlcgwasm.resizestackfpuval(list: TAsmList; fromsize, tosize: tcgsize);
  2188. begin
  2189. if (fromsize=OS_F32) and
  2190. (tosize=OS_F64) then
  2191. begin
  2192. list.concat(taicpu.op_none(a_f64_promote_f32));
  2193. incstack(list,1);
  2194. end
  2195. else if (fromsize=OS_F64) and
  2196. (tosize=OS_F32) then
  2197. begin
  2198. list.concat(taicpu.op_none(a_f32_demote_f64));
  2199. decstack(list,1);
  2200. end;
  2201. end;
  2202. procedure thlcgwasm.maybepreparedivu32(list: TAsmList; var op: topcg; size: tdef; out isdivu32: boolean);
  2203. begin
  2204. if (op=OP_DIV) and
  2205. (def_cgsize(size)=OS_32) then
  2206. begin
  2207. { needs zero-extension to 64 bit, because the JVM only supports
  2208. signed divisions }
  2209. resize_stack_int_val(list,u32inttype,s64inttype,false);
  2210. op:=OP_IDIV;
  2211. isdivu32:=true;
  2212. end
  2213. else
  2214. isdivu32:=false;
  2215. end;
  2216. function thlcgwasm.a_call_name_intern(list: TAsmList; pd: tprocdef; const s: TSymStr; forceresdef: tdef; inheritedcall: boolean): tcgpara;
  2217. var
  2218. opc: tasmop;
  2219. begin
  2220. {
  2221. invoke types:
  2222. * invokeinterface: call method from an interface (must also specify
  2223. number of parameters in terms of stack slot count!)
  2224. * invokespecial: invoke a constructor, method in a superclass,
  2225. or private instance method
  2226. * invokestatic: invoke a class method (private or not)
  2227. * invokevirtual: invoke a regular method
  2228. }
  2229. case pd.owner.symtabletype of
  2230. globalsymtable,
  2231. staticsymtable,
  2232. localsymtable:
  2233. { regular and nested procedures are turned into static methods }
  2234. opc:=a_call;
  2235. objectsymtable:
  2236. begin
  2237. Internalerror(2019083004); // no support for symbol table calls
  2238. opc:=a_call_indirect; // todo: need a reference to a (funccall) table
  2239. {case tobjectdef(pd.owner.defowner).objecttype of
  2240. odt_javaclass:
  2241. begin
  2242. if (po_classmethod in pd.procoptions) or
  2243. (pd.proctypeoption=potype_operator) then
  2244. opc:=a_invokestatic
  2245. else if (pd.visibility=vis_strictprivate) or
  2246. (pd.proctypeoption=potype_constructor) or
  2247. inheritedcall then
  2248. opc:=a_invokespecial
  2249. else
  2250. opc:=a_invokevirtual;
  2251. end;
  2252. odt_interfacejava:
  2253. { static interface methods are not allowed }
  2254. opc:=a_invokeinterface;
  2255. else
  2256. internalerror(2010122601);
  2257. end;
  2258. }
  2259. end;
  2260. recordsymtable:
  2261. begin
  2262. if (po_staticmethod in pd.procoptions) or
  2263. (pd.proctypeoption=potype_operator) then
  2264. opc:=a_call
  2265. else if (pd.visibility=vis_strictprivate) or
  2266. (pd.proctypeoption=potype_constructor) or
  2267. inheritedcall then
  2268. opc:=a_call
  2269. else
  2270. opc:=a_call_indirect;
  2271. end
  2272. else
  2273. internalerror(2010122602);
  2274. end;
  2275. if (opc<>a_call_indirect) then
  2276. list.concat(taicpu.op_sym(opc,current_asmdata.RefAsmSymbol(s,AT_FUNCTION)))
  2277. else
  2278. begin
  2279. pd.init_paraloc_info(calleeside);
  2280. list.concat(taicpu.op_sym_const(opc,current_asmdata.RefAsmSymbol(s,AT_FUNCTION),pd.calleeargareasize));
  2281. end;
  2282. result:=get_call_result_cgpara(pd,forceresdef);
  2283. end;
  2284. procedure create_hlcodegen_cpu;
  2285. begin
  2286. hlcg:=thlcgwasm.create;
  2287. create_codegen;
  2288. end;
  2289. initialization
  2290. chlcgobj:=thlcgwasm;
  2291. create_hlcodegen:=@create_hlcodegen_cpu;
  2292. end.