hlcgcpu.pas 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885
  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. adddefaultlenparas: boolean;
  958. begin
  959. { load copy helper parameters on the stack }
  960. a_load_ref_stack(list,java_jlobject,source,prepare_stack_for_ref(list,source,false));
  961. a_load_ref_stack(list,java_jlobject,dest,prepare_stack_for_ref(list,dest,false));
  962. { call copy helper }
  963. eledef:=tarraydef(size).elementdef;
  964. ndim:=1;
  965. adddefaultlenparas:=true;
  966. case eledef.typ of
  967. orddef:
  968. begin
  969. case torddef(eledef).ordtype of
  970. pasbool8,s8bit,u8bit,bool8bit,uchar,
  971. s16bit,u16bit,bool16bit,pasbool16,
  972. uwidechar,
  973. s32bit,u32bit,bool32bit,pasbool32,
  974. s64bit,u64bit,bool64bit,pasbool64,scurrency:
  975. procname:='FPC_COPY_SHALLOW_ARRAY'
  976. else
  977. internalerror(2011020504);
  978. end;
  979. end;
  980. arraydef:
  981. begin
  982. { call fpc_setlength_dynarr_multidim with deepcopy=true, and extra
  983. parameters }
  984. while (eledef.typ=arraydef) and
  985. not is_dynamic_array(eledef) do
  986. begin
  987. eledef:=tarraydef(eledef).elementdef;
  988. inc(ndim)
  989. end;
  990. if (ndim=1) then
  991. procname:='FPC_COPY_SHALLOW_ARRAY'
  992. else
  993. begin
  994. { deepcopy=true }
  995. a_load_const_stack(list,pasbool8type,1,R_INTREGISTER);
  996. { ndim }
  997. a_load_const_stack(list,s32inttype,ndim,R_INTREGISTER);
  998. { eletype }
  999. a_load_const_stack(list,cwidechartype,ord(jvmarrtype_setlength(eledef)),R_INTREGISTER);
  1000. adddefaultlenparas:=false;
  1001. procname:='FPC_SETLENGTH_DYNARR_MULTIDIM';
  1002. end;
  1003. end;
  1004. recorddef:
  1005. procname:='FPC_COPY_JRECORD_ARRAY';
  1006. floatdef,
  1007. stringdef:
  1008. procname:='FPC_COPY_SHALLOW_ARRAY';
  1009. setdef,
  1010. variantdef:
  1011. begin
  1012. { todo: make a deep copy via clone... }
  1013. internalerror(2011020505);
  1014. end;
  1015. else
  1016. procname:='FPC_COPY_SHALLOW_ARRAY';
  1017. end;
  1018. if adddefaultlenparas then
  1019. begin
  1020. { -1, -1 means "copy entire array" }
  1021. a_load_const_stack(list,s32inttype,-1,R_INTREGISTER);
  1022. a_load_const_stack(list,s32inttype,-1,R_INTREGISTER);
  1023. end;
  1024. g_call_system_proc(list,procname);
  1025. if ndim=1 then
  1026. begin
  1027. decstack(list,2);
  1028. if adddefaultlenparas then
  1029. decstack(list,2);
  1030. end
  1031. else
  1032. begin
  1033. decstack(list,4);
  1034. { pop return value, must be the same as dest }
  1035. list.concat(taicpu.op_none(a_pop));
  1036. decstack(list,1);
  1037. end;
  1038. end;
  1039. procedure thlcgjvm.concatcopy_record(list: TAsmList; size: tdef; const source, dest: treference);
  1040. var
  1041. srsym: tsym;
  1042. pd: tprocdef;
  1043. begin
  1044. { self }
  1045. a_load_ref_stack(list,size,source,prepare_stack_for_ref(list,source,false));
  1046. { result }
  1047. a_load_ref_stack(list,size,dest,prepare_stack_for_ref(list,dest,false));
  1048. { call fpcDeepCopy helper }
  1049. srsym:=search_struct_member(tabstractrecorddef(size),'FPCDEEPCOPY');
  1050. if not assigned(srsym) or
  1051. (srsym.typ<>procsym) then
  1052. Message1(cg_f_unknown_compilerproc,'FpcRecordBaseType.fpcDeepCopy');
  1053. pd:=tprocdef(tprocsym(srsym).procdeflist[0]);
  1054. a_call_name(list,pd,pd.mangledname,false);
  1055. { both parameters are removed, no function result }
  1056. decstack(list,2);
  1057. end;
  1058. procedure thlcgjvm.g_concatcopy(list: TAsmList; size: tdef; const source, dest: treference);
  1059. var
  1060. handled: boolean;
  1061. begin
  1062. handled:=false;
  1063. case size.typ of
  1064. arraydef:
  1065. begin
  1066. if not is_dynamic_array(size) then
  1067. begin
  1068. concatcopy_normal_array(list,size,source,dest);
  1069. handled:=true;
  1070. end;
  1071. end;
  1072. recorddef:
  1073. begin
  1074. concatcopy_record(list,size,source,dest);
  1075. handled:=true;
  1076. end;
  1077. end;
  1078. if not handled then
  1079. inherited;
  1080. end;
  1081. procedure thlcgjvm.a_loadfpu_ref_ref(list: TAsmList; fromsize, tosize: tdef; const ref1, ref2: treference);
  1082. var
  1083. dstack_slots: longint;
  1084. begin
  1085. dstack_slots:=prepare_stack_for_ref(list,ref2,false);
  1086. a_load_ref_stack(list,fromsize,ref1,prepare_stack_for_ref(list,ref1,false));
  1087. resizestackfpuval(list,def_cgsize(fromsize),def_cgsize(tosize));
  1088. a_load_stack_ref(list,tosize,ref2,dstack_slots);
  1089. end;
  1090. procedure thlcgjvm.a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; reg: tregister);
  1091. begin
  1092. a_load_ref_stack(list,fromsize,ref,prepare_stack_for_ref(list,ref,false));
  1093. resizestackfpuval(list,def_cgsize(fromsize),def_cgsize(tosize));
  1094. a_load_stack_reg(list,tosize,reg);
  1095. end;
  1096. procedure thlcgjvm.a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tdef; reg: tregister; const ref: treference);
  1097. var
  1098. dstack_slots: longint;
  1099. begin
  1100. dstack_slots:=prepare_stack_for_ref(list,ref,false);
  1101. a_load_reg_stack(list,fromsize,reg);
  1102. resizestackfpuval(list,def_cgsize(fromsize),def_cgsize(tosize));
  1103. a_load_stack_ref(list,tosize,ref,dstack_slots);
  1104. end;
  1105. procedure thlcgjvm.a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tdef; reg1, reg2: tregister);
  1106. begin
  1107. a_load_reg_stack(list,fromsize,reg1);
  1108. resizestackfpuval(list,def_cgsize(fromsize),def_cgsize(tosize));
  1109. a_load_stack_reg(list,tosize,reg2);
  1110. end;
  1111. procedure thlcgjvm.g_proc_entry(list: TAsmList; localsize: longint; nostackframe: boolean);
  1112. begin
  1113. { the localsize is based on tg.lasttemp -> already in terms of stack
  1114. slots rather than bytes }
  1115. list.concat(tai_directive.Create(asd_jlimit,'locals '+tostr(localsize)));
  1116. list.concat(tai_directive.Create(asd_jlimit,'stack '+tostr(fmaxevalstackheight)));
  1117. end;
  1118. procedure thlcgjvm.g_proc_exit(list: TAsmList; parasize: longint; nostackframe: boolean);
  1119. var
  1120. retdef: tdef;
  1121. opc: tasmop;
  1122. begin
  1123. if current_procinfo.procdef.proctypeoption in [potype_constructor,potype_class_constructor] then
  1124. retdef:=voidtype
  1125. else
  1126. retdef:=current_procinfo.procdef.returndef;
  1127. case retdef.typ of
  1128. orddef:
  1129. case torddef(retdef).ordtype of
  1130. uvoid:
  1131. opc:=a_return;
  1132. s64bit,
  1133. u64bit,
  1134. scurrency:
  1135. opc:=a_lreturn;
  1136. else
  1137. opc:=a_ireturn;
  1138. end;
  1139. floatdef:
  1140. case tfloatdef(retdef).floattype of
  1141. s32real:
  1142. opc:=a_freturn;
  1143. s64real:
  1144. opc:=a_dreturn;
  1145. else
  1146. internalerror(2011010213);
  1147. end;
  1148. else
  1149. opc:=a_areturn;
  1150. end;
  1151. list.concat(taicpu.op_none(opc));
  1152. end;
  1153. procedure thlcgjvm.gen_load_return_value(list: TAsmList);
  1154. begin
  1155. { constructors don't return anything in the jvm }
  1156. if current_procinfo.procdef.proctypeoption in [potype_constructor,potype_class_constructor] then
  1157. exit;
  1158. inherited gen_load_return_value(list);
  1159. end;
  1160. procedure thlcgjvm.record_generated_code_for_procdef(pd: tprocdef; code, data: TAsmList);
  1161. begin
  1162. { add something to the al_procedures list as well, because if all al_*
  1163. lists are empty, the assembler writer isn't called }
  1164. if not code.empty and
  1165. current_asmdata.asmlists[al_procedures].empty then
  1166. current_asmdata.asmlists[al_procedures].concat(tai_align.Create(4));
  1167. pd.exprasmlist:=TAsmList.create;
  1168. pd.exprasmlist.concatlist(code);
  1169. if assigned(data) and
  1170. not data.empty then
  1171. internalerror(2010122801);
  1172. end;
  1173. procedure thlcgjvm.g_incrrefcount(list: TAsmList; t: tdef; const ref: treference);
  1174. begin
  1175. // do nothing
  1176. end;
  1177. procedure thlcgjvm.g_decrrefcount(list: TAsmList; t: tdef; const ref: treference);
  1178. begin
  1179. // do nothing
  1180. end;
  1181. procedure thlcgjvm.g_array_rtti_helper(list: TAsmList; t: tdef; const ref: treference; const highloc: tlocation; const name: string);
  1182. var
  1183. normaldim: longint;
  1184. recref: treference;
  1185. begin
  1186. { only in case of initialisation, we have to set all elements to "empty" }
  1187. if name<>'FPC_INITIALIZE_ARRAY' then
  1188. exit;
  1189. { put array on the stack }
  1190. a_load_ref_stack(list,java_jlobject,ref,prepare_stack_for_ref(list,ref,false));
  1191. { in case it's an open array whose elements are regular arrays, put the
  1192. dimension of the regular arrays on the stack (otherwise pass 0) }
  1193. normaldim:=0;
  1194. while (t.typ=arraydef) and
  1195. not is_dynamic_array(t) do
  1196. begin
  1197. inc(normaldim);
  1198. t:=tarraydef(t).elementdef;
  1199. end;
  1200. a_load_const_stack(list,s32inttype,normaldim,R_INTREGISTER);
  1201. { highloc is invalid, the length is part of the array in Java }
  1202. if is_wide_or_unicode_string(t) then
  1203. g_call_system_proc(list,'fpc_initialize_array_unicodestring')
  1204. else if is_dynamic_array(t) then
  1205. g_call_system_proc(list,'fpc_initialize_array_dynarr')
  1206. else if is_record(t) then
  1207. begin
  1208. tg.gethltemp(list,t,t.size,tt_persistent,recref);
  1209. a_load_ref_stack(list,t,recref,prepare_stack_for_ref(list,recref,false));
  1210. g_call_system_proc(list,'fpc_initialize_array_record');
  1211. tg.ungettemp(list,recref);
  1212. end
  1213. else
  1214. internalerror(2011031901);
  1215. end;
  1216. procedure thlcgjvm.g_initialize(list: TAsmList; t: tdef; const ref: treference);
  1217. var
  1218. dummyloc: tlocation;
  1219. recref: treference;
  1220. begin
  1221. if (t.typ=arraydef) and
  1222. not is_dynamic_array(t) then
  1223. begin
  1224. dummyloc.loc:=LOC_INVALID;
  1225. g_array_rtti_helper(list,tarraydef(t).elementdef,ref,dummyloc,'FPC_INITIALIZE_ARRAY')
  1226. end
  1227. else if is_record(t) then
  1228. begin
  1229. { create a new, empty record and replace the contents of the old one
  1230. with those of the new one (in the future we can generate a dedicate
  1231. initialization helper) }
  1232. tg.gethltemp(list,t,t.size,tt_persistent,recref);
  1233. g_concatcopy(list,t,recref,ref);
  1234. tg.ungettemp(list,recref);
  1235. end
  1236. else
  1237. a_load_const_ref(list,t,0,ref);
  1238. end;
  1239. procedure thlcgjvm.g_finalize(list: TAsmList; t: tdef; const ref: treference);
  1240. begin
  1241. // do nothing
  1242. end;
  1243. procedure thlcgjvm.location_get_data_ref(list: TAsmList; def: tdef; const l: tlocation; var ref: treference; loadref: boolean; alignment: longint);
  1244. var
  1245. tmploc: tlocation;
  1246. begin
  1247. { This routine is a combination of a generalised a_loadaddr_ref_reg()
  1248. that also works for addresses in registers (in case loadref is false)
  1249. and of a_load_ref_reg (in case loadref is true). It is used for
  1250. a) getting the address of managed types
  1251. b) getting to the actual data of value types that are passed by
  1252. reference by the compiler (and then get a local copy at the caller
  1253. side). Normally, depending on whether this reference is passed in a
  1254. register or reference, we either need a reference with that register
  1255. as base or load the address in that reference and use that as a new
  1256. base.
  1257. Since the JVM cannot take the address of anything, all
  1258. "pass-by-reference" value parameters (which are always aggregate types)
  1259. are already simply the implicit pointer to the data (since arrays,
  1260. records, etc are already internally implicit pointers). This means
  1261. that if "loadref" is true, we must simply return this implicit pointer.
  1262. If it is false, we are supposed the take the address of this implicit
  1263. pointer, which is not possible.
  1264. However, managed types are also implicit pointers in Pascal, so in that
  1265. case "taking the address" again consists of simply returning the
  1266. implicit pointer/current value.
  1267. }
  1268. if not loadref then
  1269. begin
  1270. if not is_managed_type(def) then
  1271. internalerror(2011020601);
  1272. end
  1273. else
  1274. begin
  1275. if not jvmimplicitpointertype(def) then
  1276. internalerror(2011020602);
  1277. end;
  1278. case l.loc of
  1279. LOC_REGISTER,
  1280. LOC_CREGISTER :
  1281. begin
  1282. { the implicit pointer is in a register and has to be in a
  1283. reference -> create a reference and put it there }
  1284. tmploc:=l;
  1285. location_force_mem(list,tmploc,java_jlobject);
  1286. ref:=tmploc.reference;
  1287. end;
  1288. LOC_REFERENCE,
  1289. LOC_CREFERENCE :
  1290. begin
  1291. ref:=l.reference;
  1292. end;
  1293. else
  1294. internalerror(2011020603);
  1295. end;
  1296. end;
  1297. procedure thlcgjvm.g_copyvaluepara_openarray(list: TAsmList; const ref: treference; const lenloc: tlocation; arrdef: tarraydef; destreg: tregister);
  1298. var
  1299. localref: treference;
  1300. arrloc: tlocation;
  1301. stackslots: longint;
  1302. begin
  1303. { temporary reference for passing to concatcopy }
  1304. tg.gethltemp(list,java_jlobject,java_jlobject.size,tt_persistent,localref);
  1305. stackslots:=prepare_stack_for_ref(list,localref,false);
  1306. { create the local copy of the array (lenloc is invalid, get length
  1307. directly from the array) }
  1308. location_reset_ref(arrloc,LOC_REFERENCE,OS_ADDR,sizeof(pint));
  1309. arrloc.reference:=ref;
  1310. g_getarraylen(list,arrloc);
  1311. g_newarray(list,arrdef,1);
  1312. a_load_stack_ref(list,java_jlobject,localref,stackslots);
  1313. { copy the source array to the destination }
  1314. g_concatcopy(list,arrdef,ref,localref);
  1315. { and put the array pointer in the register as expected by the caller }
  1316. a_load_ref_reg(list,java_jlobject,java_jlobject,localref,destreg);
  1317. end;
  1318. procedure thlcgjvm.g_releasevaluepara_openarray(list: TAsmList; arrdef: tarraydef; const l: tlocation);
  1319. begin
  1320. // do nothing, long live garbage collection!
  1321. end;
  1322. procedure thlcgjvm.gen_initialize_code(list: TAsmList);
  1323. var
  1324. ref: treference;
  1325. begin
  1326. { create globals with wrapped types such as arrays/records }
  1327. case current_procinfo.procdef.proctypeoption of
  1328. potype_unitinit:
  1329. begin
  1330. reference_reset_base(ref,NR_NO,0,1);
  1331. if assigned(current_module.globalsymtable) then
  1332. allocate_implicit_structs_for_st_with_base_ref(list,current_module.globalsymtable,ref,staticvarsym);
  1333. allocate_implicit_structs_for_st_with_base_ref(list,current_module.localsymtable,ref,staticvarsym);
  1334. end;
  1335. potype_class_constructor:
  1336. begin
  1337. { also initialise local variables, if any }
  1338. inherited;
  1339. { initialise class fields }
  1340. reference_reset_base(ref,NR_NO,0,1);
  1341. allocate_implicit_structs_for_st_with_base_ref(list,tabstractrecorddef(current_procinfo.procdef.owner.defowner).symtable,ref,staticvarsym);
  1342. end
  1343. else
  1344. inherited
  1345. end;
  1346. end;
  1347. procedure thlcgjvm.a_load_stack_reg(list: TAsmList; size: tdef; reg: tregister);
  1348. var
  1349. opc: tasmop;
  1350. finishandval: aint;
  1351. begin
  1352. opc:=loadstoreopc(size,false,false,finishandval);
  1353. list.concat(taicpu.op_reg(opc,reg));
  1354. { avoid problems with getting the size of an open array etc }
  1355. if jvmimplicitpointertype(size) then
  1356. size:=java_jlobject;
  1357. decstack(list,1+ord(size.size>4));
  1358. end;
  1359. procedure thlcgjvm.a_load_stack_ref(list: TAsmList; size: tdef; const ref: treference; extra_slots: longint);
  1360. var
  1361. opc: tasmop;
  1362. finishandval: aint;
  1363. begin
  1364. { fake location that indicates the value has to remain on the stack }
  1365. if ref.base=NR_EVAL_STACK_BASE then
  1366. exit;
  1367. opc:=loadstoreopcref(size,false,ref,finishandval);
  1368. if ref.arrayreftype=art_none then
  1369. list.concat(taicpu.op_ref(opc,ref))
  1370. else
  1371. list.concat(taicpu.op_none(opc));
  1372. { avoid problems with getting the size of an open array etc }
  1373. if jvmimplicitpointertype(size) then
  1374. size:=java_jlobject;
  1375. decstack(list,1+ord(size.size>4)+extra_slots);
  1376. end;
  1377. procedure thlcgjvm.a_load_reg_stack(list: TAsmList; size: tdef; reg: tregister);
  1378. var
  1379. opc: tasmop;
  1380. finishandval: aint;
  1381. begin
  1382. opc:=loadstoreopc(size,true,false,finishandval);
  1383. list.concat(taicpu.op_reg(opc,reg));
  1384. if finishandval<>-1 then
  1385. a_op_const_stack(list,OP_AND,size,finishandval);
  1386. { avoid problems with getting the size of an open array etc }
  1387. if jvmimplicitpointertype(size) then
  1388. size:=java_jlobject;
  1389. incstack(list,1+ord(size.size>4));
  1390. end;
  1391. procedure thlcgjvm.a_load_ref_stack(list: TAsmList; size: tdef; const ref: treference; extra_slots: longint);
  1392. var
  1393. opc: tasmop;
  1394. finishandval: aint;
  1395. begin
  1396. { fake location that indicates the value is already on the stack? }
  1397. if (ref.base=NR_EVAL_STACK_BASE) then
  1398. exit;
  1399. opc:=loadstoreopcref(size,true,ref,finishandval);
  1400. if ref.arrayreftype=art_none then
  1401. list.concat(taicpu.op_ref(opc,ref))
  1402. else
  1403. list.concat(taicpu.op_none(opc));
  1404. if finishandval<>-1 then
  1405. a_op_const_stack(list,OP_AND,size,finishandval);
  1406. { avoid problems with getting the size of an open array etc }
  1407. if jvmimplicitpointertype(size) then
  1408. size:=java_jlobject;
  1409. incstack(list,1+ord(size.size>4)-extra_slots);
  1410. end;
  1411. function thlcgjvm.loadstoreopcref(def: tdef; isload: boolean; const ref: treference; out finishandval: aint): tasmop;
  1412. const
  1413. { isload static }
  1414. getputopc: array[boolean,boolean] of tasmop =
  1415. ((a_putfield,a_putstatic),
  1416. (a_getfield,a_getstatic));
  1417. begin
  1418. if assigned(ref.symbol) then
  1419. begin
  1420. finishandval:=-1;
  1421. { -> either a global (static) field, or a regular field. If a regular
  1422. field, then ref.base contains the self pointer, otherwise
  1423. ref.base=NR_NO. In both cases, the symbol contains all other
  1424. information (combined field name and type descriptor) }
  1425. result:=getputopc[isload,ref.base=NR_NO];
  1426. end
  1427. else
  1428. result:=loadstoreopc(def,isload,ref.arrayreftype<>art_none,finishandval);
  1429. end;
  1430. function thlcgjvm.loadstoreopc(def: tdef; isload, isarray: boolean; out finishandval: aint): tasmop;
  1431. var
  1432. size: longint;
  1433. begin
  1434. finishandval:=-1;
  1435. case def2regtyp(def) of
  1436. R_INTREGISTER:
  1437. begin
  1438. size:=def.size;
  1439. if not isarray then
  1440. begin
  1441. case size of
  1442. 1,2,3,4:
  1443. if isload then
  1444. result:=a_iload
  1445. else
  1446. result:=a_istore;
  1447. 8:
  1448. if isload then
  1449. result:=a_lload
  1450. else
  1451. result:=a_lstore;
  1452. else
  1453. internalerror(2011032814);
  1454. end;
  1455. end
  1456. { array }
  1457. else if isload then
  1458. begin
  1459. case size of
  1460. 1:
  1461. begin
  1462. result:=a_baload;
  1463. if not is_signed(def) then
  1464. finishandval:=255;
  1465. end;
  1466. 2:
  1467. begin
  1468. if is_widechar(def) then
  1469. result:=a_caload
  1470. else
  1471. begin
  1472. result:=a_saload;
  1473. { if we'd treat arrays of word as "array of widechar" we
  1474. could use a_caload, but that would make for even more
  1475. awkward interfacing with external Java code }
  1476. if not is_signed(def) then
  1477. finishandval:=65535;
  1478. end;
  1479. end;
  1480. 4: result:=a_iaload;
  1481. 8: result:=a_laload;
  1482. else
  1483. internalerror(2010120503);
  1484. end
  1485. end
  1486. else
  1487. begin
  1488. case size of
  1489. 1: result:=a_bastore;
  1490. 2: if not is_widechar(def) then
  1491. result:=a_sastore
  1492. else
  1493. result:=a_castore;
  1494. 4: result:=a_iastore;
  1495. 8: result:=a_lastore;
  1496. else
  1497. internalerror(2010120508);
  1498. end
  1499. end
  1500. end;
  1501. R_ADDRESSREGISTER:
  1502. if not isarray then
  1503. if isload then
  1504. result:=a_aload
  1505. else
  1506. result:=a_astore
  1507. else if isload then
  1508. result:=a_aaload
  1509. else
  1510. result:=a_aastore;
  1511. R_FPUREGISTER:
  1512. begin
  1513. case tfloatdef(def).floattype of
  1514. s32real:
  1515. if not isarray then
  1516. if isload then
  1517. result:=a_fload
  1518. else
  1519. result:=a_fstore
  1520. else if isload then
  1521. result:=a_faload
  1522. else
  1523. result:=a_fastore;
  1524. s64real:
  1525. if not isarray then
  1526. if isload then
  1527. result:=a_dload
  1528. else
  1529. result:=a_dstore
  1530. else if isload then
  1531. result:=a_daload
  1532. else
  1533. result:=a_dastore;
  1534. else
  1535. internalerror(2010120504);
  1536. end
  1537. end
  1538. else
  1539. internalerror(2010120502);
  1540. end;
  1541. end;
  1542. procedure thlcgjvm.resize_stack_int_val(list: TAsmList; fromsize, tosize: tcgsize; forarraystore: boolean);
  1543. begin
  1544. if fromsize in [OS_S64,OS_64] then
  1545. begin
  1546. if not(tosize in [OS_S64,OS_64]) then
  1547. begin
  1548. { truncate }
  1549. list.concat(taicpu.op_none(a_l2i));
  1550. decstack(list,1);
  1551. end;
  1552. end
  1553. else if tosize in [OS_S64,OS_64] then
  1554. begin
  1555. { extend }
  1556. list.concat(taicpu.op_none(a_i2l));
  1557. incstack(list,1);
  1558. { if it was an unsigned 32 bit value, remove sign extension }
  1559. if fromsize=OS_32 then
  1560. a_op_const_stack(list,OP_AND,s64inttype,cardinal($ffffffff));
  1561. end;
  1562. { if the value is immediately stored to an array afterwards, the store
  1563. instruction will properly truncate the value; otherwise we may need
  1564. additional truncation, except for 64/32 bit conversions, which are
  1565. already handled above }
  1566. if not forarraystore and
  1567. (not(fromsize in [OS_S64,OS_64,OS_32,OS_S32]) or
  1568. not(tosize in [OS_S64,OS_64,OS_32,OS_S32])) and
  1569. (tcgsize2size[fromsize]>tcgsize2size[tosize]) or
  1570. ((tcgsize2size[fromsize]=tcgsize2size[tosize]) and
  1571. (fromsize<>tosize)) or
  1572. { needs to mask out the sign in the top 16 bits }
  1573. ((fromsize=OS_S8) and
  1574. (tosize=OS_16)) then
  1575. case tosize of
  1576. OS_8:
  1577. a_op_const_stack(list,OP_AND,s32inttype,255);
  1578. OS_S8:
  1579. list.concat(taicpu.op_none(a_i2b));
  1580. OS_16:
  1581. list.concat(taicpu.op_none(a_i2c));
  1582. OS_S16:
  1583. list.concat(taicpu.op_none(a_i2s));
  1584. end;
  1585. end;
  1586. procedure thlcgjvm.allocate_implicit_struct_with_base_ref(list: TAsmList; vs: tabstractvarsym; ref: treference);
  1587. var
  1588. tmpref: treference;
  1589. begin
  1590. ref.symbol:=current_asmdata.RefAsmSymbol(vs.mangledname);
  1591. tg.gethltemp(list,vs.vardef,vs.vardef.size,tt_persistent,tmpref);
  1592. { only copy the reference, not the actual data }
  1593. a_load_ref_ref(list,java_jlobject,java_jlobject,tmpref,ref);
  1594. { remains live since there's still a reference to the created
  1595. entity }
  1596. tg.ungettemp(list,tmpref);
  1597. end;
  1598. procedure thlcgjvm.allocate_implicit_structs_for_st_with_base_ref(list: TAsmList; st: tsymtable; const ref: treference; allocvartyp: tsymtyp);
  1599. var
  1600. vs: tabstractvarsym;
  1601. i: longint;
  1602. begin
  1603. for i:=0 to st.symlist.count-1 do
  1604. begin
  1605. if (tsym(st.symlist[i]).typ<>allocvartyp) then
  1606. continue;
  1607. vs:=tabstractvarsym(st.symlist[i]);
  1608. if sp_internal in vs.symoptions then
  1609. continue;
  1610. if not jvmimplicitpointertype(vs.vardef) then
  1611. continue;
  1612. allocate_implicit_struct_with_base_ref(list,vs,ref);
  1613. end;
  1614. end;
  1615. procedure thlcgjvm.gen_initialize_fields_code(list: TAsmList);
  1616. var
  1617. selfpara: tparavarsym;
  1618. selfreg: tregister;
  1619. ref: treference;
  1620. obj: tabstractrecorddef;
  1621. i: longint;
  1622. needinit: boolean;
  1623. begin
  1624. obj:=tabstractrecorddef(current_procinfo.procdef.owner.defowner);
  1625. { check whether there are any fields that need initialisation }
  1626. needinit:=false;
  1627. for i:=0 to obj.symtable.symlist.count-1 do
  1628. if (tsym(obj.symtable.symlist[i]).typ=fieldvarsym) and
  1629. jvmimplicitpointertype(tfieldvarsym(obj.symtable.symlist[i]).vardef) then
  1630. begin
  1631. needinit:=true;
  1632. break;
  1633. end;
  1634. if not needinit then
  1635. exit;
  1636. selfpara:=tparavarsym(current_procinfo.procdef.parast.find('self'));
  1637. if not assigned(selfpara) then
  1638. internalerror(2011033001);
  1639. selfreg:=getaddressregister(list,selfpara.vardef);
  1640. a_load_loc_reg(list,obj,obj,selfpara.localloc,selfreg);
  1641. reference_reset_base(ref,selfreg,0,1);
  1642. allocate_implicit_structs_for_st_with_base_ref(list,obj.symtable,ref,fieldvarsym);
  1643. end;
  1644. procedure thlcgjvm.resizestackfpuval(list: TAsmList; fromsize, tosize: tcgsize);
  1645. begin
  1646. if (fromsize=OS_F32) and
  1647. (tosize=OS_F64) then
  1648. begin
  1649. list.concat(taicpu.op_none(a_f2d));
  1650. incstack(list,1);
  1651. end
  1652. else if (fromsize=OS_F64) and
  1653. (tosize=OS_F32) then
  1654. begin
  1655. list.concat(taicpu.op_none(a_d2f));
  1656. decstack(list,1);
  1657. end;
  1658. end;
  1659. procedure thlcgjvm.maybepreparedivu32(list: TAsmList; var op: topcg; size: tdef; out isdivu32: boolean);
  1660. begin
  1661. if (op=OP_DIV) and
  1662. (def_cgsize(size)=OS_32) then
  1663. begin
  1664. { needs zero-extension to 64 bit, because the JVM only supports
  1665. signed divisions }
  1666. resize_stack_int_val(list,OS_32,OS_S64,false);
  1667. op:=OP_IDIV;
  1668. isdivu32:=true;
  1669. end
  1670. else
  1671. isdivu32:=false;
  1672. end;
  1673. procedure thlcgjvm.a_call_name_intern(list: TAsmList; pd: tprocdef; const s: string; inheritedcall: boolean);
  1674. var
  1675. opc: tasmop;
  1676. begin
  1677. {
  1678. invoke types:
  1679. * invokeinterface: call method from an interface
  1680. * invokespecial: invoke a constructor, method in a superclass,
  1681. or private instance method
  1682. * invokestatic: invoke a class method (private or not)
  1683. * invokevirtual: invoke a regular method
  1684. }
  1685. case pd.owner.symtabletype of
  1686. globalsymtable,
  1687. staticsymtable,
  1688. localsymtable:
  1689. { regular and nested procedures are turned into static methods }
  1690. opc:=a_invokestatic;
  1691. objectsymtable:
  1692. begin
  1693. case tobjectdef(pd.owner.defowner).objecttype of
  1694. odt_javaclass:
  1695. begin
  1696. if (po_staticmethod in pd.procoptions) then
  1697. opc:=a_invokestatic
  1698. else if (pd.visibility=vis_private) or
  1699. (pd.proctypeoption=potype_constructor) or
  1700. inheritedcall then
  1701. opc:=a_invokespecial
  1702. else
  1703. opc:=a_invokevirtual;
  1704. end;
  1705. odt_interfacejava:
  1706. { static interface methods are not allowed }
  1707. opc:=a_invokeinterface;
  1708. else
  1709. internalerror(2010122601);
  1710. end;
  1711. end;
  1712. recordsymtable:
  1713. begin
  1714. if (po_staticmethod in pd.procoptions) then
  1715. opc:=a_invokestatic
  1716. else if (pd.visibility=vis_private) or
  1717. (pd.proctypeoption=potype_constructor) or
  1718. inheritedcall then
  1719. opc:=a_invokespecial
  1720. else
  1721. opc:=a_invokevirtual;
  1722. end
  1723. else
  1724. internalerror(2010122602);
  1725. end;
  1726. list.concat(taicpu.op_sym(opc,current_asmdata.RefAsmSymbol(s)));
  1727. end;
  1728. procedure thlcgjvm.g_call_system_proc(list: TAsmList; const procname: string);
  1729. var
  1730. srsym: tsym;
  1731. pd: tprocdef;
  1732. begin
  1733. srsym:=tsym(systemunit.find(procname));
  1734. if not assigned(srsym) or
  1735. (srsym.typ<>procsym) then
  1736. Message1(cg_f_unknown_compilerproc,procname);
  1737. pd:=tprocdef(tprocsym(srsym).procdeflist[0]);
  1738. a_call_name(list,pd,pd.mangledname,false);
  1739. end;
  1740. procedure create_hlcodegen;
  1741. begin
  1742. hlcg:=thlcgjvm.create;
  1743. create_codegen;
  1744. end;
  1745. end.