hlcgcpu.pas 85 KB

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