hlcgcpu.pas 82 KB

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