hlcgcpu.pas 101 KB

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