hlcgcpu.pas 70 KB

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