hlcgcpu.pas 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324
  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. symtype,symdef,
  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. procedure a_call_name(list : TAsmList;pd : tprocdef;const s : string; weak: boolean);override;
  37. procedure a_call_name_inherited(list : TAsmList;pd : tprocdef;const s : string);override;
  38. procedure a_load_const_reg(list : TAsmList;tosize : tdef;a : aint;register : tregister);override;
  39. procedure a_load_const_ref(list : TAsmList;tosize : tdef;a : aint;const ref : treference);override;
  40. procedure a_load_reg_ref(list : TAsmList;fromsize, tosize : tdef;register : tregister;const ref : treference);override;
  41. procedure a_load_reg_reg(list : TAsmList;fromsize, tosize : tdef;reg1,reg2 : tregister);override;
  42. procedure a_load_ref_reg(list : TAsmList;fromsize, tosize : tdef;const ref : treference;register : tregister);override;
  43. procedure a_load_ref_ref(list : TAsmList;fromsize, tosize : tdef;const sref : treference;const dref : treference);override;
  44. procedure a_loadaddr_ref_reg(list : TAsmList;fromsize, tosize : tdef;const ref : treference;r : tregister);override;
  45. procedure a_op_const_reg(list: TAsmList; Op: TOpCG; size: tdef; a: Aint; reg: TRegister); override;
  46. procedure a_op_const_reg_reg(list: TAsmList; op: TOpCg; size: tdef; a: aint; src, dst: tregister); override;
  47. procedure a_op_const_ref(list: TAsmList; Op: TOpCG; size: tdef; a: Aint; const ref: TReference); override;
  48. procedure a_op_ref_reg(list: TAsmList; Op: TOpCG; size: tdef; const ref: TReference; reg: TRegister); override;
  49. procedure a_op_reg_reg_reg(list: TAsmList; op: TOpCg; size: tdef; src1, src2, dst: tregister); override;
  50. procedure a_op_reg_reg(list: TAsmList; Op: TOpCG; size: tdef; reg1, reg2: TRegister); override;
  51. procedure a_cmp_const_ref_label(list: TAsmList; size: tdef; cmp_op: topcmp; a: aint; const ref: treference; l: tasmlabel); override;
  52. procedure a_cmp_const_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; a: aint; reg: tregister; l: tasmlabel); override;
  53. procedure a_cmp_ref_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; const ref: treference; reg: tregister; l: tasmlabel); override;
  54. procedure a_cmp_reg_ref_label(list: TAsmList; size: tdef; cmp_op: topcmp; reg: tregister; const ref: treference; l: tasmlabel); override;
  55. procedure a_cmp_reg_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; reg1, reg2: tregister; l: tasmlabel); override;
  56. procedure a_jmp_always(list : TAsmList;l: tasmlabel); override;
  57. procedure a_loadfpu_ref_ref(list: TAsmList; fromsize, tosize: tdef; const ref1, ref2: treference); override;
  58. procedure a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; reg: tregister); override;
  59. procedure a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tdef; reg: tregister; const ref: treference); override;
  60. procedure a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tdef; reg1, reg2: tregister); override;
  61. procedure g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean); override;
  62. procedure g_proc_exit(list : TAsmList;parasize:longint;nostackframe:boolean); override;
  63. procedure gen_load_return_value(list:TAsmList);override;
  64. procedure record_generated_code_for_procdef(pd: tprocdef; code, data: TAsmList); override;
  65. procedure g_incrrefcount(list : TAsmList;t: tdef; const ref: treference);override;
  66. procedure g_decrrefcount(list : TAsmList;t: tdef; const ref: treference);override;
  67. procedure g_initialize(list : TAsmList;t : tdef;const ref : treference);override;
  68. procedure g_finalize(list : TAsmList;t : tdef;const ref : treference);override;
  69. { JVM-specific routines }
  70. procedure a_load_stack_reg(list : TAsmList;size: tdef;reg: tregister);
  71. { extra_slots are the slots that are used by the reference, and that
  72. will be removed by the store operation }
  73. procedure a_load_stack_ref(list : TAsmList;size: tdef;const ref: treference;extra_slots: longint);
  74. procedure a_load_reg_stack(list : TAsmList;size: tdef;reg: tregister);
  75. { extra_slots are the slots that are used by the reference, and that
  76. will be removed by the load operation }
  77. procedure a_load_ref_stack(list : TAsmList;size: tdef;const ref: treference;extra_slots: longint);
  78. procedure a_load_const_stack(list : TAsmList;size: tdef;a :aint; typ: TRegisterType);
  79. procedure a_load_loc_stack(list : TAsmList;size: tdef;const loc: tlocation);
  80. procedure a_loadfpu_const_stack(list : TAsmList;size: tdef;a :double);
  81. procedure a_op_stack(list : TAsmList;op: topcg; size: tdef; trunc32: boolean);
  82. procedure a_op_const_stack(list : TAsmList;op: topcg; size: tdef;a : aint);
  83. procedure a_op_reg_stack(list : TAsmList;op: topcg; size: tdef;reg: tregister);
  84. procedure a_op_ref_stack(list : TAsmList;op: topcg; size: tdef;const ref: treference);
  85. procedure a_op_loc_stack(list : TAsmList;op: topcg; size: tdef;const loc: tlocation);
  86. procedure g_reference_loc(list: TAsmList; def: tdef; const fromloc: tlocation; out toloc: tlocation); override;
  87. { this routine expects that all values are already massaged into the
  88. required form (sign bits xor'ed for gt/lt comparisons for OS_32/OS_64,
  89. see http://stackoverflow.com/questions/4068973/c-performing-signed-comparison-in-unsigned-variables-without-casting ) }
  90. procedure a_cmp_stack_label(list : TAsmlist; size: tdef; cmp_op: topcmp; lab: tasmlabel);
  91. { these 2 routines perform the massaging expected by the previous one }
  92. procedure maybe_adjust_cmp_stackval(list : TAsmlist; size: tdef; cmp_op: topcmp);
  93. function maybe_adjust_cmp_constval(size: tdef; cmp_op: topcmp; a: aint): aint;
  94. { truncate/sign extend after performing operations on values < 32 bit
  95. that may have overflowed outside the range }
  96. procedure maybe_adjust_op_result(list: TAsmList; op: TOpCg; size: tdef);
  97. { performs sign/zero extension as required }
  98. procedure resize_stack_int_val(list: TAsmList;fromsize,tosize: tcgsize; forarraystore: boolean);
  99. property maxevalstackheight: longint read fmaxevalstackheight;
  100. protected
  101. procedure gen_load_uninitialized_function_result(list: TAsmList; pd: tprocdef; resdef: tdef; const resloc: tcgpara); override;
  102. { in case of an array, the array base address and index have to be
  103. put on the evaluation stack before the stored value; similarly, for
  104. fields the self pointer has to be loaded first. Also checks whether
  105. the reference is valid. If dup is true, the necessary values are stored
  106. twice. Returns how many stack slots have been consumed, disregarding
  107. the "dup". }
  108. function prepare_stack_for_ref(list: TAsmList; const ref: treference; dup: boolean): longint;
  109. { return the load/store opcode to load/store from/to ref; if the result
  110. has to be and'ed after a load to get the final value, that constant
  111. is returned in finishandval (otherwise that value is set to -1) }
  112. function loadstoreopcref(def: tdef; isload: boolean; const ref: treference; out finishandval: aint): tasmop;
  113. { return the load/store opcode to load/store from/to reg; if the result
  114. has to be and'ed after a load to get the final value, that constant
  115. is returned in finishandval (otherwise that value is set to -1) }
  116. function loadstoreopc(def: tdef; isload, isarray: boolean; out finishandval: aint): tasmop;
  117. procedure resizestackfpuval(list: TAsmList; fromsize, tosize: tcgsize);
  118. { in case of an OS_32 OP_DIV, we have to use an OS_S64 OP_IDIV because the
  119. JVM does not support unsigned divisions }
  120. procedure maybepreparedivu32(list: TAsmList; var op: topcg; size: tdef; out isdivu32: boolean);
  121. { common implementation of a_call_* }
  122. procedure a_call_name_intern(list : TAsmList;pd : tprocdef;const s : string; inheritedcall: boolean);
  123. end;
  124. procedure create_hlcodegen;
  125. const
  126. opcmp2if: array[topcmp] of tasmop = (A_None,
  127. a_ifeq,a_ifgt,a_iflt,a_ifge,a_ifle,
  128. a_ifne,a_ifle,a_iflt,a_ifge,a_ifgt);
  129. implementation
  130. uses
  131. verbose,cutils,globals,
  132. defutil,
  133. aasmtai,aasmcpu,
  134. symconst,
  135. procinfo,cgcpu;
  136. const
  137. TOpCG2IAsmOp : array[topcg] of TAsmOp=( { not = xor -1 }
  138. 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
  139. );
  140. TOpCG2LAsmOp : array[topcg] of TAsmOp=( { not = xor -1 }
  141. 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
  142. );
  143. constructor thlcgjvm.create;
  144. begin
  145. fevalstackheight:=0;
  146. fmaxevalstackheight:=0;
  147. end;
  148. procedure thlcgjvm.incstack(list: TasmList;slots: longint);
  149. begin
  150. if slots=0 then
  151. exit;
  152. inc(fevalstackheight,slots);
  153. if (fevalstackheight>fmaxevalstackheight) then
  154. fmaxevalstackheight:=fevalstackheight;
  155. if cs_asm_regalloc in current_settings.globalswitches then
  156. list.concat(tai_comment.Create(strpnew('allocated '+tostr(slots)+', stack height = '+tostr(fevalstackheight))));
  157. end;
  158. procedure thlcgjvm.decstack(list: TAsmList;slots: longint);
  159. begin
  160. if slots=0 then
  161. exit;
  162. dec(fevalstackheight,slots);
  163. if (fevalstackheight<0) then
  164. internalerror(2010120501);
  165. if cs_asm_regalloc in current_settings.globalswitches then
  166. list.concat(tai_comment.Create(strpnew(' freed '+tostr(slots)+', stack height = '+tostr(fevalstackheight))));
  167. end;
  168. procedure thlcgjvm.a_call_name(list: TAsmList; pd: tprocdef; const s: string; weak: boolean);
  169. begin
  170. a_call_name_intern(list,pd,s,false);
  171. end;
  172. procedure thlcgjvm.a_call_name_inherited(list: TAsmList; pd: tprocdef; const s: string);
  173. begin
  174. a_call_name_intern(list,pd,s,true);
  175. end;
  176. procedure thlcgjvm.a_load_const_stack(list : TAsmList;size : tdef;a : aint; typ: TRegisterType);
  177. const
  178. int2opc: array[-1..5] of tasmop = (a_iconst_m1,a_iconst_0,a_iconst_1,
  179. a_iconst_2,a_iconst_3,a_iconst_4,a_iconst_5);
  180. begin
  181. case typ of
  182. R_INTREGISTER:
  183. begin
  184. case def_cgsize(size) of
  185. OS_8,OS_16,OS_32,
  186. OS_S8,OS_S16,OS_S32:
  187. begin
  188. { convert cardinals to longints }
  189. a:=longint(a);
  190. if (a>=-1) and
  191. (a<=5) then
  192. list.concat(taicpu.op_none(int2opc[a]))
  193. else if (a>=low(shortint)) and
  194. (a<=high(shortint)) then
  195. list.concat(taicpu.op_const(a_bipush,a))
  196. else if (a>=low(smallint)) and
  197. (a<=high(smallint)) then
  198. list.concat(taicpu.op_const(a_sipush,a))
  199. else
  200. list.concat(taicpu.op_const(a_ldc,a));
  201. end;
  202. OS_64,OS_S64:
  203. begin
  204. case a of
  205. 0:
  206. list.concat(taicpu.op_none(a_lconst_0));
  207. 1:
  208. list.concat(taicpu.op_none(a_lconst_1));
  209. else
  210. list.concat(taicpu.op_const(a_ldc2_w,a));
  211. end;
  212. incstack(list,1);
  213. end;
  214. else
  215. internalerror(2010110702);
  216. end;
  217. end;
  218. R_ADDRESSREGISTER:
  219. begin
  220. if a<>0 then
  221. internalerror(2010110701);
  222. list.concat(taicpu.op_none(a_aconst_null));
  223. end;
  224. else
  225. internalerror(2010110703);
  226. end;
  227. incstack(list,1);
  228. end;
  229. procedure thlcgjvm.a_load_loc_stack(list: TAsmList;size: tdef;const loc: tlocation);
  230. begin
  231. case loc.loc of
  232. LOC_REGISTER,LOC_CREGISTER,
  233. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  234. a_load_reg_stack(list,size,loc.register);
  235. LOC_REFERENCE,LOC_CREFERENCE:
  236. a_load_ref_stack(list,size,loc.reference,prepare_stack_for_ref(list,loc.reference,false));
  237. LOC_CONSTANT:
  238. a_load_const_stack(list,size,loc.value,def2regtyp(size));
  239. else
  240. internalerror(2011010401);
  241. end;
  242. end;
  243. procedure thlcgjvm.a_loadfpu_const_stack(list: TAsmList; size: tdef; a: double);
  244. begin
  245. case tfloatdef(size).floattype of
  246. s32real:
  247. begin
  248. if a=0.0 then
  249. list.concat(taicpu.op_none(a_fconst_0))
  250. else if a=1.0 then
  251. list.concat(taicpu.op_none(a_fconst_1))
  252. else if a=2.0 then
  253. list.concat(taicpu.op_none(a_fconst_2))
  254. else
  255. list.concat(taicpu.op_single(a_ldc,a));
  256. incstack(list,1);
  257. end;
  258. s64real:
  259. begin
  260. if a=0.0 then
  261. list.concat(taicpu.op_none(a_dconst_0))
  262. else if a=1.0 then
  263. list.concat(taicpu.op_none(a_dconst_1))
  264. else
  265. list.concat(taicpu.op_double(a_ldc2_w,a));
  266. incstack(list,2);
  267. end
  268. else
  269. internalerror(2011010501);
  270. end;
  271. end;
  272. procedure thlcgjvm.a_op_stack(list: TAsmList; op: topcg; size: tdef; trunc32: boolean);
  273. var
  274. cgsize: tcgsize;
  275. begin
  276. if not trunc32 then
  277. cgsize:=def_cgsize(size)
  278. else
  279. begin
  280. resize_stack_int_val(list,OS_32,OS_S64,false);
  281. cgsize:=OS_S64;
  282. end;
  283. case cgsize of
  284. OS_8,OS_S8,
  285. OS_16,OS_S16,
  286. OS_32,OS_S32:
  287. begin
  288. { not = xor 1 for boolean, xor -1 for the rest}
  289. if op=OP_NOT then
  290. begin
  291. if not is_pasbool(size) then
  292. a_load_const_stack(list,s32inttype,high(cardinal),R_INTREGISTER)
  293. else
  294. a_load_const_stack(list,size,1,R_INTREGISTER);
  295. op:=OP_XOR;
  296. end;
  297. if TOpCG2IAsmOp[op]=A_None then
  298. internalerror(2010120532);
  299. list.concat(taicpu.op_none(TOpCG2IAsmOp[op]));
  300. maybe_adjust_op_result(list,op,size);
  301. if op<>OP_NEG then
  302. decstack(list,1);
  303. end;
  304. OS_64,OS_S64:
  305. begin
  306. { unsigned 64 bit division must be done via a helper }
  307. if op=OP_DIV then
  308. internalerror(2010120530);
  309. { not = xor -1 }
  310. if op=OP_NOT then
  311. begin
  312. a_load_const_stack(list,s64inttype,-1,R_INTREGISTER);
  313. op:=OP_XOR;
  314. end;
  315. if TOpCG2LAsmOp[op]=A_None then
  316. internalerror(2010120533);
  317. list.concat(taicpu.op_none(TOpCG2LAsmOp[op]));
  318. case op of
  319. OP_NOT:
  320. ;
  321. { the second argument here is an int rather than a long }
  322. OP_SHL,OP_SHR,OP_SAR:
  323. decstack(list,1);
  324. else
  325. decstack(list,2);
  326. end;
  327. end;
  328. else
  329. internalerror(2010120531);
  330. end;
  331. if trunc32 then
  332. begin
  333. list.concat(taicpu.op_none(a_l2i));
  334. decstack(list,1);
  335. end;
  336. end;
  337. procedure thlcgjvm.a_op_const_stack(list: TAsmList;op: topcg;size: tdef;a: aint);
  338. var
  339. trunc32: boolean;
  340. begin
  341. { use "integer to (wide)char" narrowing opcode for "and 65535" }
  342. if (op=OP_AND) and
  343. (def_cgsize(size) in [OS_16,OS_S16,OS_32,OS_S32]) and
  344. (a=65535) then
  345. list.concat(taicpu.op_none(a_i2c))
  346. else
  347. begin
  348. maybepreparedivu32(list,op,size,trunc32);
  349. case op of
  350. OP_NEG,OP_NOT:
  351. internalerror(2011010801);
  352. OP_SHL,OP_SHR,OP_SAR:
  353. { the second argument here is an int rather than a long }
  354. a_load_const_stack(list,s32inttype,a,R_INTREGISTER);
  355. else
  356. a_load_const_stack(list,size,a,R_INTREGISTER);
  357. end;
  358. a_op_stack(list,op,size,trunc32);
  359. end;
  360. end;
  361. procedure thlcgjvm.a_op_reg_stack(list: TAsmList; op: topcg; size: tdef; reg: tregister);
  362. var
  363. trunc32: boolean;
  364. begin
  365. maybepreparedivu32(list,op,size,trunc32);
  366. case op of
  367. OP_NEG,OP_NOT:
  368. ;
  369. OP_SHL,OP_SHR,OP_SAR:
  370. if not is_64bitint(size) then
  371. a_load_reg_stack(list,size,reg)
  372. else
  373. begin
  374. { the second argument here is an int rather than a long }
  375. if getsubreg(reg)=R_SUBQ then
  376. internalerror(2011010802);
  377. a_load_reg_stack(list,s32inttype,reg)
  378. end
  379. else
  380. a_load_reg_stack(list,size,reg);
  381. end;
  382. a_op_stack(list,op,size,trunc32);
  383. end;
  384. procedure thlcgjvm.a_op_ref_stack(list: TAsmList; op: topcg; size: tdef; const ref: treference);
  385. var
  386. trunc32: boolean;
  387. begin
  388. { ref must not be the stack top, because that may indicate an error
  389. (it means that we will perform an operation of the stack top onto
  390. itself, so that means the two values have been loaded manually prior
  391. to calling this routine, instead of letting this routine load one of
  392. them; if something like that is needed, call a_op_stack() directly) }
  393. if ref.base=NR_EVAL_STACK_BASE then
  394. internalerror(2010121102);
  395. maybepreparedivu32(list,op,size,trunc32);
  396. if not(op in [OP_NEG,OP_NOT]) then
  397. a_load_ref_stack(list,size,ref,prepare_stack_for_ref(list,ref,false));
  398. a_op_stack(list,op,size,trunc32);
  399. end;
  400. procedure thlcgjvm.a_op_loc_stack(list: TAsmList; op: topcg; size: tdef; const loc: tlocation);
  401. begin
  402. case loc.loc of
  403. LOC_REGISTER,LOC_CREGISTER:
  404. a_op_reg_stack(list,op,size,loc.register);
  405. LOC_REFERENCE,LOC_CREFERENCE:
  406. a_op_ref_stack(list,op,size,loc.reference);
  407. LOC_CONSTANT:
  408. a_op_const_stack(list,op,size,loc.value);
  409. else
  410. internalerror(2011011415)
  411. end;
  412. end;
  413. procedure thlcgjvm.g_reference_loc(list: TAsmList; def: tdef; const fromloc: tlocation; out toloc: tlocation);
  414. procedure handle_reg_move(regsize: tdef; const fromreg: tregister; out toreg: tregister; regtyp: tregistertype);
  415. begin
  416. case regtyp of
  417. R_INTREGISTER:
  418. toreg:=getintregister(list,regsize);
  419. R_ADDRESSREGISTER:
  420. toreg:=getaddressregister(list,regsize);
  421. R_FPUREGISTER:
  422. toreg:=getfpuregister(list,regsize);
  423. end;
  424. a_load_reg_reg(list,regsize,regsize,fromreg,toreg);
  425. end;
  426. begin
  427. toloc:=fromloc;
  428. case fromloc.loc of
  429. { volatile location, can't get a permanent reference }
  430. LOC_REGISTER,
  431. LOC_FPUREGISTER:
  432. internalerror(2011031406);
  433. LOC_CONSTANT:
  434. { finished }
  435. ;
  436. LOC_CREGISTER:
  437. handle_reg_move(def,fromloc.reference.index,toloc.reference.index,R_INTREGISTER);
  438. LOC_CFPUREGISTER:
  439. handle_reg_move(def,fromloc.reference.index,toloc.reference.index,R_FPUREGISTER);
  440. { although LOC_CREFERENCE cannot be an lvalue, we may want to take a
  441. reference to such a location for multiple reading }
  442. LOC_CREFERENCE,
  443. LOC_REFERENCE:
  444. begin
  445. if (fromloc.reference.base<>NR_NO) and
  446. (fromloc.reference.base<>current_procinfo.framepointer) and
  447. (fromloc.reference.base<>NR_STACK_POINTER_REG) then
  448. handle_reg_move(java_jlobject,fromloc.reference.base,toloc.reference.base,R_ADDRESSREGISTER);
  449. case fromloc.reference.arrayreftype of
  450. art_indexreg:
  451. begin
  452. { all array indices in Java are 32 bit ints }
  453. handle_reg_move(s32inttype,fromloc.reference.index,toloc.reference.index,R_INTREGISTER);
  454. end;
  455. art_indexref:
  456. begin
  457. if (fromloc.reference.indexbase<>NR_NO) and
  458. (fromloc.reference.indexbase<>NR_STACK_POINTER_REG) then
  459. handle_reg_move(s32inttype,fromloc.reference.indexbase,toloc.reference.indexbase,R_ADDRESSREGISTER);
  460. end;
  461. end;
  462. end;
  463. else
  464. internalerror(2011031407);
  465. end;
  466. end;
  467. procedure thlcgjvm.a_cmp_stack_label(list: TAsmlist; size: tdef; cmp_op: topcmp; lab: tasmlabel);
  468. const
  469. opcmp2icmp: array[topcmp] of tasmop = (A_None,
  470. a_if_icmpeq,a_if_icmpgt,a_if_icmplt,a_if_icmpge,a_if_icmple,
  471. a_if_icmpne,a_if_icmple,a_if_icmplt,a_if_icmpge,a_if_icmpgt);
  472. var
  473. cgsize: tcgsize;
  474. begin
  475. case def2regtyp(size) of
  476. R_INTREGISTER:
  477. begin
  478. cgsize:=def_cgsize(size);
  479. case cgsize of
  480. OS_S8,OS_8,
  481. OS_16,OS_S16,
  482. OS_S32,OS_32:
  483. begin
  484. list.concat(taicpu.op_sym(opcmp2icmp[cmp_op],lab));
  485. decstack(list,2);
  486. end;
  487. OS_64,OS_S64:
  488. begin
  489. list.concat(taicpu.op_none(a_lcmp));
  490. decstack(list,3);
  491. list.concat(taicpu.op_sym(opcmp2if[cmp_op],lab));
  492. decstack(list,1);
  493. end;
  494. else
  495. internalerror(2010120538);
  496. end;
  497. end;
  498. R_ADDRESSREGISTER:
  499. begin
  500. case cmp_op of
  501. OC_EQ:
  502. list.concat(taicpu.op_sym(a_if_acmpeq,lab));
  503. OC_NE:
  504. list.concat(taicpu.op_sym(a_if_acmpne,lab));
  505. else
  506. internalerror(2010120537);
  507. end;
  508. decstack(list,2);
  509. end;
  510. else
  511. internalerror(2010120538);
  512. end;
  513. end;
  514. procedure thlcgjvm.maybe_adjust_cmp_stackval(list: TAsmlist; size: tdef; cmp_op: topcmp);
  515. begin
  516. if (cmp_op in [OC_EQ,OC_NE]) or
  517. (def2regtyp(size)<>R_INTREGISTER) then
  518. exit;
  519. { http://stackoverflow.com/questions/4068973/c-performing-signed-comparison-in-unsigned-variables-without-casting }
  520. case def_cgsize(size) of
  521. OS_32:
  522. a_op_const_stack(list,OP_XOR,size,cardinal($80000000));
  523. OS_64:
  524. a_op_const_stack(list,OP_XOR,size,aint($8000000000000000));
  525. end;
  526. end;
  527. function thlcgjvm.maybe_adjust_cmp_constval(size: tdef; cmp_op: topcmp; a: aint): aint;
  528. begin
  529. result:=a;
  530. if (cmp_op in [OC_EQ,OC_NE]) or
  531. (def2regtyp(size)<>R_INTREGISTER) then
  532. exit;
  533. case def_cgsize(size) of
  534. OS_32:
  535. result:=a xor cardinal($80000000);
  536. OS_64:
  537. result:=a xor aint($8000000000000000);
  538. end;
  539. end;
  540. procedure thlcgjvm.maybe_adjust_op_result(list: TAsmList; op: TOpCg; size: tdef);
  541. const
  542. overflowops = [OP_MUL,OP_SHL,OP_ADD,OP_SUB,OP_NOT,OP_NEG];
  543. begin
  544. if (op in overflowops) and
  545. (def_cgsize(size) in [OS_8,OS_S8,OS_16,OS_S16]) then
  546. resize_stack_int_val(list,OS_S32,def_cgsize(size),false);
  547. end;
  548. procedure thlcgjvm.gen_load_uninitialized_function_result(list: TAsmList; pd: tprocdef; resdef: tdef; const resloc: tcgpara);
  549. begin
  550. { constructors don't return anything in Java }
  551. if pd.proctypeoption=potype_constructor then
  552. exit;
  553. { must return a value of the correct type on the evaluation stack }
  554. case def2regtyp(resdef) of
  555. R_INTREGISTER,
  556. R_ADDRESSREGISTER:
  557. a_load_const_cgpara(list,resdef,0,resloc);
  558. R_FPUREGISTER:
  559. case tfloatdef(resdef).floattype of
  560. s32real:
  561. list.concat(taicpu.op_none(a_fconst_0));
  562. s64real:
  563. list.concat(taicpu.op_none(a_dconst_0));
  564. else
  565. internalerror(2011010302);
  566. end
  567. else
  568. internalerror(2011010301);
  569. end;
  570. end;
  571. function thlcgjvm.prepare_stack_for_ref(list: TAsmList; const ref: treference; dup: boolean): longint;
  572. var
  573. href: treference;
  574. begin
  575. result:=0;
  576. { fake location that indicates the value is already on the stack? }
  577. if (ref.base=NR_EVAL_STACK_BASE) then
  578. exit;
  579. if ref.arrayreftype=art_none then
  580. begin
  581. { non-array accesses cannot have an index reg }
  582. if ref.index<>NR_NO then
  583. internalerror(2010120509);
  584. if (ref.base<>NR_NO) then
  585. begin
  586. if (ref.base<>NR_STACK_POINTER_REG) then
  587. begin
  588. { regular field -> load self on the stack }
  589. a_load_reg_stack(list,voidpointertype,ref.base);
  590. if dup then
  591. begin
  592. list.concat(taicpu.op_none(a_dup));
  593. incstack(list,1);
  594. end;
  595. { field name/type encoded in symbol, no index/offset }
  596. if not assigned(ref.symbol) or
  597. (ref.offset<>0) then
  598. internalerror(2010120524);
  599. result:=1;
  600. end
  601. else
  602. begin
  603. { local variable -> offset encoded in opcode and nothing to
  604. do here, except for checking that it's a valid reference }
  605. if assigned(ref.symbol) then
  606. internalerror(2010120523);
  607. end;
  608. end
  609. else
  610. begin
  611. { static field -> nothing to do here, except for validity check }
  612. if not assigned(ref.symbol) or
  613. (ref.offset<>0) then
  614. internalerror(2010120525);
  615. end;
  616. end
  617. else
  618. begin
  619. { arrays have implicit dereference -> pointer to array must have been
  620. loaded into base reg }
  621. if (ref.base=NR_NO) or
  622. (ref.base=NR_STACK_POINTER_REG) then
  623. internalerror(2010120511);
  624. if assigned(ref.symbol) then
  625. internalerror(2010120512);
  626. { stack: ... -> ..., arrayref, index }
  627. { load array base address }
  628. a_load_reg_stack(list,voidpointertype,ref.base);
  629. { index can either be in a register, or located in a simple memory
  630. location (since we have to load it anyway) }
  631. case ref.arrayreftype of
  632. art_indexreg:
  633. begin
  634. if ref.index=NR_NO then
  635. internalerror(2010120513);
  636. { all array indices in Java are 32 bit ints }
  637. a_load_reg_stack(list,s32inttype,ref.index);
  638. end;
  639. art_indexref:
  640. begin
  641. reference_reset_base(href,ref.indexbase,ref.indexoffset,4);
  642. href.symbol:=ref.indexsymbol;
  643. a_load_ref_stack(list,s32inttype,href,prepare_stack_for_ref(list,href,false));
  644. end;
  645. art_indexconst:
  646. begin
  647. a_load_const_stack(list,s32inttype,ref.indexoffset,R_INTREGISTER);
  648. end;
  649. else
  650. internalerror(2011012001);
  651. end;
  652. { adjustment of the index }
  653. if ref.offset<>0 then
  654. a_op_const_stack(list,OP_ADD,s32inttype,ref.offset);
  655. if dup then
  656. begin
  657. list.concat(taicpu.op_none(a_dup2));
  658. incstack(list,2);
  659. end;
  660. result:=2;
  661. end;
  662. end;
  663. procedure thlcgjvm.a_load_const_reg(list: TAsmList; tosize: tdef; a: aint; register: tregister);
  664. begin
  665. a_load_const_stack(list,tosize,a,def2regtyp(tosize));
  666. a_load_stack_reg(list,tosize,register);
  667. end;
  668. procedure thlcgjvm.a_load_const_ref(list: TAsmList; tosize: tdef; a: aint; const ref: treference);
  669. var
  670. extra_slots: longint;
  671. begin
  672. extra_slots:=prepare_stack_for_ref(list,ref,false);
  673. a_load_const_stack(list,tosize,a,def2regtyp(tosize));
  674. a_load_stack_ref(list,tosize,ref,extra_slots);
  675. end;
  676. procedure thlcgjvm.a_load_reg_ref(list: TAsmList; fromsize, tosize: tdef; register: tregister; const ref: treference);
  677. var
  678. extra_slots: longint;
  679. begin
  680. extra_slots:=prepare_stack_for_ref(list,ref,false);
  681. a_load_reg_stack(list,fromsize,register);
  682. a_load_stack_ref(list,tosize,ref,extra_slots);
  683. end;
  684. procedure thlcgjvm.a_load_reg_reg(list: TAsmList; fromsize, tosize: tdef; reg1, reg2: tregister);
  685. begin
  686. a_load_reg_stack(list,fromsize,reg1);
  687. if def2regtyp(fromsize)=R_INTREGISTER then
  688. resize_stack_int_val(list,def_cgsize(fromsize),def_cgsize(tosize),false);
  689. a_load_stack_reg(list,tosize,reg2);
  690. end;
  691. procedure thlcgjvm.a_load_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; register: tregister);
  692. var
  693. extra_slots: longint;
  694. begin
  695. extra_slots:=prepare_stack_for_ref(list,ref,false);
  696. a_load_ref_stack(list,fromsize,ref,extra_slots);
  697. if def2regtyp(fromsize)=R_INTREGISTER then
  698. resize_stack_int_val(list,def_cgsize(fromsize),def_cgsize(tosize),false);
  699. a_load_stack_reg(list,tosize,register);
  700. end;
  701. procedure thlcgjvm.a_load_ref_ref(list: TAsmList; fromsize, tosize: tdef; const sref: treference; const dref: treference);
  702. var
  703. extra_sslots,
  704. extra_dslots: longint;
  705. begin
  706. { make sure the destination reference is on top, since in the end the
  707. order has to be "destref, value" -> first create "destref, sourceref" }
  708. extra_dslots:=prepare_stack_for_ref(list,dref,false);
  709. extra_sslots:=prepare_stack_for_ref(list,sref,false);
  710. a_load_ref_stack(list,fromsize,sref,extra_sslots);
  711. if def2regtyp(fromsize)=R_INTREGISTER then
  712. resize_stack_int_val(list,def_cgsize(fromsize),def_cgsize(tosize),dref.arrayreftype<>art_none);
  713. a_load_stack_ref(list,tosize,dref,extra_dslots);
  714. end;
  715. procedure thlcgjvm.a_loadaddr_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; r: tregister);
  716. begin
  717. internalerror(2010120534);
  718. end;
  719. procedure thlcgjvm.a_op_const_reg(list: TAsmList; Op: TOpCG; size: tdef; a: Aint; reg: TRegister);
  720. begin
  721. a_op_const_reg_reg(list,op,size,a,reg,reg);
  722. end;
  723. procedure thlcgjvm.a_op_const_reg_reg(list: TAsmList; op: TOpCg; size: tdef; a: aint; src, dst: tregister);
  724. begin
  725. a_load_reg_stack(list,size,src);
  726. a_op_const_stack(list,op,size,a);
  727. a_load_stack_reg(list,size,dst);
  728. end;
  729. procedure thlcgjvm.a_op_const_ref(list: TAsmList; Op: TOpCG; size: tdef; a: Aint; const ref: TReference);
  730. var
  731. extra_slots: longint;
  732. begin
  733. extra_slots:=prepare_stack_for_ref(list,ref,true);
  734. { TODO, here or in peepholeopt: use iinc when possible }
  735. a_load_ref_stack(list,size,ref,extra_slots);
  736. a_op_const_stack(list,op,size,a);
  737. a_load_stack_ref(list,size,ref,extra_slots);
  738. end;
  739. procedure thlcgjvm.a_op_ref_reg(list: TAsmList; Op: TOpCG; size: tdef; const ref: TReference; reg: TRegister);
  740. begin
  741. a_load_reg_stack(list,size,reg);
  742. a_op_ref_stack(list,op,size,ref);
  743. a_load_stack_reg(list,size,reg);
  744. end;
  745. procedure thlcgjvm.a_op_reg_reg_reg(list: TAsmList; op: TOpCg; size: tdef; src1, src2, dst: tregister);
  746. begin
  747. a_load_reg_stack(list,size,src2);
  748. a_op_reg_stack(list,op,size,src1);
  749. a_load_stack_reg(list,size,dst);
  750. end;
  751. procedure thlcgjvm.a_op_reg_reg(list: TAsmList; Op: TOpCG; size: tdef; reg1, reg2: TRegister);
  752. begin
  753. a_op_reg_reg_reg(list,op,size,reg1,reg2,reg2);
  754. end;
  755. procedure thlcgjvm.a_cmp_const_ref_label(list: TAsmList; size: tdef; cmp_op: topcmp; a: aint; const ref: treference; l: tasmlabel);
  756. begin
  757. if ref.base<>NR_EVAL_STACK_BASE then
  758. a_load_ref_stack(list,size,ref,prepare_stack_for_ref(list,ref,false));
  759. maybe_adjust_cmp_stackval(list,size,cmp_op);
  760. a_load_const_stack(list,size,maybe_adjust_cmp_constval(size,cmp_op,a),def2regtyp(size));
  761. a_cmp_stack_label(list,size,cmp_op,l);
  762. end;
  763. procedure thlcgjvm.a_cmp_const_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; a: aint; reg: tregister; l: tasmlabel);
  764. begin
  765. a_load_reg_stack(list,size,reg);
  766. maybe_adjust_cmp_stackval(list,size,cmp_op);
  767. a_load_const_stack(list,size,maybe_adjust_cmp_constval(size,cmp_op,a),def2regtyp(size));
  768. a_cmp_stack_label(list,size,cmp_op,l);
  769. end;
  770. procedure thlcgjvm.a_cmp_ref_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; const ref: treference; reg: tregister; l: tasmlabel);
  771. begin
  772. a_load_reg_stack(list,size,reg);
  773. maybe_adjust_cmp_stackval(list,size,cmp_op);
  774. if ref.base<>NR_EVAL_STACK_BASE then
  775. a_load_ref_stack(list,size,ref,prepare_stack_for_ref(list,ref,false))
  776. else
  777. list.concat(taicpu.op_none(a_swap));
  778. maybe_adjust_cmp_stackval(list,size,cmp_op);
  779. a_cmp_stack_label(list,size,cmp_op,l);
  780. end;
  781. procedure thlcgjvm.a_cmp_reg_ref_label(list: TAsmList; size: tdef; cmp_op: topcmp; reg: tregister; const ref: treference; l: tasmlabel);
  782. begin
  783. if ref.base<>NR_EVAL_STACK_BASE then
  784. a_load_ref_stack(list,size,ref,prepare_stack_for_ref(list,ref,false));
  785. maybe_adjust_cmp_stackval(list,size,cmp_op);
  786. a_load_reg_stack(list,size,reg);
  787. maybe_adjust_cmp_stackval(list,size,cmp_op);
  788. a_cmp_stack_label(list,size,cmp_op,l);
  789. end;
  790. procedure thlcgjvm.a_cmp_reg_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; reg1, reg2: tregister; l: tasmlabel);
  791. begin
  792. a_load_reg_stack(list,size,reg2);
  793. maybe_adjust_cmp_stackval(list,size,cmp_op);
  794. a_load_reg_stack(list,size,reg1);
  795. maybe_adjust_cmp_stackval(list,size,cmp_op);
  796. a_cmp_stack_label(list,size,cmp_op,l);
  797. end;
  798. procedure thlcgjvm.a_jmp_always(list: TAsmList; l: tasmlabel);
  799. begin
  800. list.concat(taicpu.op_sym(a_goto,current_asmdata.RefAsmSymbol(l.name)));
  801. end;
  802. procedure thlcgjvm.a_loadfpu_ref_ref(list: TAsmList; fromsize, tosize: tdef; const ref1, ref2: treference);
  803. var
  804. dstack_slots: longint;
  805. begin
  806. dstack_slots:=prepare_stack_for_ref(list,ref2,false);
  807. a_load_ref_stack(list,fromsize,ref1,prepare_stack_for_ref(list,ref1,false));
  808. resizestackfpuval(list,def_cgsize(fromsize),def_cgsize(tosize));
  809. a_load_stack_ref(list,tosize,ref2,dstack_slots);
  810. end;
  811. procedure thlcgjvm.a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; reg: tregister);
  812. begin
  813. a_load_ref_stack(list,fromsize,ref,prepare_stack_for_ref(list,ref,false));
  814. resizestackfpuval(list,def_cgsize(fromsize),def_cgsize(tosize));
  815. a_load_stack_reg(list,tosize,reg);
  816. end;
  817. procedure thlcgjvm.a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tdef; reg: tregister; const ref: treference);
  818. var
  819. dstack_slots: longint;
  820. begin
  821. dstack_slots:=prepare_stack_for_ref(list,ref,false);
  822. a_load_reg_stack(list,fromsize,reg);
  823. resizestackfpuval(list,def_cgsize(fromsize),def_cgsize(tosize));
  824. a_load_stack_ref(list,tosize,ref,dstack_slots);
  825. end;
  826. procedure thlcgjvm.a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tdef; reg1, reg2: tregister);
  827. begin
  828. a_load_reg_stack(list,fromsize,reg1);
  829. resizestackfpuval(list,def_cgsize(fromsize),def_cgsize(tosize));
  830. a_load_stack_reg(list,tosize,reg2);
  831. end;
  832. procedure thlcgjvm.g_proc_entry(list: TAsmList; localsize: longint; nostackframe: boolean);
  833. begin
  834. { the localsize is based on tg.lasttemp -> already in terms of stack
  835. slots rather than bytes }
  836. list.concat(tai_directive.Create(asd_jlimit,'locals '+tostr(localsize)));
  837. list.concat(tai_directive.Create(asd_jlimit,'stack '+tostr(fmaxevalstackheight)));
  838. end;
  839. procedure thlcgjvm.g_proc_exit(list: TAsmList; parasize: longint; nostackframe: boolean);
  840. var
  841. retdef: tdef;
  842. opc: tasmop;
  843. begin
  844. if current_procinfo.procdef.proctypeoption=potype_constructor then
  845. retdef:=voidtype
  846. else
  847. retdef:=current_procinfo.procdef.returndef;
  848. case retdef.typ of
  849. orddef:
  850. case torddef(retdef).ordtype of
  851. uvoid:
  852. opc:=a_return;
  853. s64bit,
  854. u64bit,
  855. scurrency:
  856. opc:=a_lreturn;
  857. else
  858. opc:=a_ireturn;
  859. end;
  860. floatdef:
  861. case tfloatdef(retdef).floattype of
  862. s32real:
  863. opc:=a_freturn;
  864. s64real:
  865. opc:=a_dreturn;
  866. else
  867. internalerror(2011010213);
  868. end;
  869. else
  870. opc:=a_areturn;
  871. end;
  872. list.concat(taicpu.op_none(opc));
  873. end;
  874. procedure thlcgjvm.gen_load_return_value(list: TAsmList);
  875. begin
  876. { constructors don't return anything in the jvm }
  877. if current_procinfo.procdef.proctypeoption=potype_constructor then
  878. exit;
  879. inherited gen_load_return_value(list);
  880. end;
  881. procedure thlcgjvm.record_generated_code_for_procdef(pd: tprocdef; code, data: TAsmList);
  882. begin
  883. { add something to the al_procedures list as well, because if all al_*
  884. lists are empty, the assembler writer isn't called }
  885. if not code.empty and
  886. current_asmdata.asmlists[al_procedures].empty then
  887. current_asmdata.asmlists[al_procedures].concat(tai_align.Create(4));
  888. pd.exprasmlist:=TAsmList.create;
  889. pd.exprasmlist.concatlist(code);
  890. if assigned(data) and
  891. not data.empty then
  892. internalerror(2010122801);
  893. end;
  894. procedure thlcgjvm.g_incrrefcount(list: TAsmList; t: tdef; const ref: treference);
  895. begin
  896. // do nothing
  897. end;
  898. procedure thlcgjvm.g_decrrefcount(list: TAsmList; t: tdef; const ref: treference);
  899. begin
  900. // do nothing
  901. end;
  902. procedure thlcgjvm.g_initialize(list: TAsmList; t: tdef; const ref: treference);
  903. var
  904. extra_slots: longint;
  905. begin
  906. a_load_const_ref(list,t,0,ref);
  907. end;
  908. procedure thlcgjvm.g_finalize(list: TAsmList; t: tdef; const ref: treference);
  909. begin
  910. // do nothing
  911. end;
  912. procedure thlcgjvm.a_load_stack_reg(list: TAsmList; size: tdef; reg: tregister);
  913. var
  914. opc: tasmop;
  915. finishandval: aint;
  916. begin
  917. opc:=loadstoreopc(size,false,false,finishandval);
  918. list.concat(taicpu.op_reg(opc,reg));
  919. decstack(list,1+ord(size.size>4));
  920. end;
  921. procedure thlcgjvm.a_load_stack_ref(list: TAsmList; size: tdef; const ref: treference; extra_slots: longint);
  922. var
  923. opc: tasmop;
  924. finishandval: aint;
  925. begin
  926. { fake location that indicates the value has to remain on the stack }
  927. if ref.base=NR_EVAL_STACK_BASE then
  928. exit;
  929. opc:=loadstoreopcref(size,false,ref,finishandval);
  930. if ref.arrayreftype=art_none then
  931. list.concat(taicpu.op_ref(opc,ref))
  932. else
  933. list.concat(taicpu.op_none(opc));
  934. decstack(list,1+ord(size.size>4)+extra_slots);
  935. end;
  936. procedure thlcgjvm.a_load_reg_stack(list: TAsmList; size: tdef; reg: tregister);
  937. var
  938. opc: tasmop;
  939. finishandval: aint;
  940. begin
  941. opc:=loadstoreopc(size,true,false,finishandval);
  942. list.concat(taicpu.op_reg(opc,reg));
  943. if finishandval<>-1 then
  944. a_op_const_stack(list,OP_AND,size,finishandval);
  945. incstack(list,1+ord(size.size>4));
  946. end;
  947. procedure thlcgjvm.a_load_ref_stack(list: TAsmList; size: tdef; const ref: treference; extra_slots: longint);
  948. var
  949. opc: tasmop;
  950. finishandval: aint;
  951. begin
  952. { fake location that indicates the value is already on the stack? }
  953. if (ref.base=NR_EVAL_STACK_BASE) then
  954. exit;
  955. opc:=loadstoreopcref(size,true,ref,finishandval);
  956. if ref.arrayreftype=art_none then
  957. list.concat(taicpu.op_ref(opc,ref))
  958. else
  959. list.concat(taicpu.op_none(opc));
  960. if finishandval<>-1 then
  961. a_op_const_stack(list,OP_AND,size,finishandval);
  962. incstack(list,1+ord(size.size>4)-extra_slots);
  963. end;
  964. function thlcgjvm.loadstoreopcref(def: tdef; isload: boolean; const ref: treference; out finishandval: aint): tasmop;
  965. const
  966. { isload static }
  967. getputopc: array[boolean,boolean] of tasmop =
  968. ((a_putfield,a_putstatic),
  969. (a_getfield,a_getstatic));
  970. var
  971. size: aint;
  972. begin
  973. if assigned(ref.symbol) then
  974. begin
  975. finishandval:=-1;
  976. { -> either a global (static) field, or a regular field. If a regular
  977. field, then ref.base contains the self pointer, otherwise
  978. ref.base=NR_NO. In both cases, the symbol contains all other
  979. information (combined field name and type descriptor) }
  980. result:=getputopc[isload,ref.base=NR_NO];
  981. end
  982. else
  983. result:=loadstoreopc(def,isload,ref.arrayreftype<>art_none,finishandval);
  984. end;
  985. function thlcgjvm.loadstoreopc(def: tdef; isload, isarray: boolean; out finishandval: aint): tasmop;
  986. var
  987. size: longint;
  988. begin
  989. finishandval:=-1;
  990. case def2regtyp(def) of
  991. R_INTREGISTER:
  992. begin
  993. size:=def.size;
  994. if not isarray then
  995. begin
  996. case size of
  997. 1,2,3,4:
  998. if isload then
  999. result:=a_iload
  1000. else
  1001. result:=a_istore;
  1002. 8:
  1003. if isload then
  1004. result:=a_lload
  1005. else
  1006. result:=a_lstore;
  1007. end;
  1008. end
  1009. { array }
  1010. else if isload then
  1011. begin
  1012. case size of
  1013. 1:
  1014. begin
  1015. result:=a_baload;
  1016. if not is_signed(def) then
  1017. finishandval:=255;
  1018. end;
  1019. 2:
  1020. begin
  1021. if is_widechar(def) then
  1022. result:=a_caload
  1023. else
  1024. begin
  1025. result:=a_saload;
  1026. { if we'd treat arrays of word as "array of widechar" we
  1027. could use a_caload, but that would make for even more
  1028. awkward interfacing with external Java code }
  1029. if not is_signed(def) then
  1030. finishandval:=65535;
  1031. end;
  1032. end;
  1033. 4: result:=a_iaload;
  1034. 8: result:=a_laload;
  1035. else
  1036. internalerror(2010120503);
  1037. end
  1038. end
  1039. else
  1040. begin
  1041. case size of
  1042. 1: result:=a_bastore;
  1043. 2: if not is_widechar(def) then
  1044. result:=a_sastore
  1045. else
  1046. result:=a_castore;
  1047. 4: result:=a_iastore;
  1048. 8: result:=a_lastore;
  1049. else
  1050. internalerror(2010120508);
  1051. end
  1052. end
  1053. end;
  1054. R_ADDRESSREGISTER:
  1055. if not isarray then
  1056. if isload then
  1057. result:=a_aload
  1058. else
  1059. result:=a_astore
  1060. else if isload then
  1061. result:=a_aaload
  1062. else
  1063. result:=a_aastore;
  1064. R_FPUREGISTER:
  1065. begin
  1066. case tfloatdef(def).floattype of
  1067. s32real:
  1068. if not isarray then
  1069. if isload then
  1070. result:=a_fload
  1071. else
  1072. result:=a_fstore
  1073. else if isload then
  1074. result:=a_faload
  1075. else
  1076. result:=a_fastore;
  1077. s64real:
  1078. if not isarray then
  1079. if isload then
  1080. result:=a_dload
  1081. else
  1082. result:=a_dstore
  1083. else if isload then
  1084. result:=a_daload
  1085. else
  1086. result:=a_dastore;
  1087. else
  1088. internalerror(2010120504);
  1089. end
  1090. end
  1091. else
  1092. internalerror(2010120502);
  1093. end;
  1094. end;
  1095. procedure thlcgjvm.resize_stack_int_val(list: TAsmList; fromsize, tosize: tcgsize; forarraystore: boolean);
  1096. begin
  1097. if fromsize in [OS_S64,OS_64] then
  1098. begin
  1099. if not(tosize in [OS_S64,OS_64]) then
  1100. begin
  1101. { truncate }
  1102. list.concat(taicpu.op_none(a_l2i));
  1103. decstack(list,1);
  1104. end;
  1105. end
  1106. else if tosize in [OS_S64,OS_64] then
  1107. begin
  1108. { extend }
  1109. list.concat(taicpu.op_none(a_i2l));
  1110. incstack(list,1);
  1111. { if it was an unsigned 32 bit value, remove sign extension }
  1112. if fromsize=OS_32 then
  1113. a_op_const_stack(list,OP_AND,s64inttype,cardinal($ffffffff));
  1114. end;
  1115. { if the value is immediately stored to an array afterwards, the store
  1116. instruction will properly truncate the value; otherwise we may need
  1117. additional truncation, except for 64/32 bit conversions, which are
  1118. already handled above }
  1119. if not forarraystore and
  1120. (not(fromsize in [OS_S64,OS_64,OS_32,OS_S32]) or
  1121. not(tosize in [OS_S64,OS_64,OS_32,OS_S32])) and
  1122. (tcgsize2size[fromsize]>tcgsize2size[tosize]) or
  1123. ((tcgsize2size[fromsize]=tcgsize2size[tosize]) and
  1124. (fromsize<>tosize)) or
  1125. { needs to mask out the sign in the top 16 bits }
  1126. ((fromsize=OS_S8) and
  1127. (tosize=OS_16)) then
  1128. case tosize of
  1129. OS_8:
  1130. a_op_const_stack(list,OP_AND,s32inttype,255);
  1131. OS_S8:
  1132. list.concat(taicpu.op_none(a_i2b));
  1133. OS_16:
  1134. list.concat(taicpu.op_none(a_i2c));
  1135. OS_S16:
  1136. list.concat(taicpu.op_none(a_i2s));
  1137. end;
  1138. end;
  1139. procedure thlcgjvm.resizestackfpuval(list: TAsmList; fromsize, tosize: tcgsize);
  1140. begin
  1141. if (fromsize=OS_F32) and
  1142. (tosize=OS_F64) then
  1143. begin
  1144. list.concat(taicpu.op_none(a_f2d));
  1145. incstack(list,1);
  1146. end
  1147. else if (fromsize=OS_F64) and
  1148. (tosize=OS_F32) then
  1149. begin
  1150. list.concat(taicpu.op_none(a_d2f));
  1151. decstack(list,1);
  1152. end;
  1153. end;
  1154. procedure thlcgjvm.maybepreparedivu32(list: TAsmList; var op: topcg; size: tdef; out isdivu32: boolean);
  1155. begin
  1156. if (op=OP_DIV) and
  1157. (def_cgsize(size)=OS_32) then
  1158. begin
  1159. { needs zero-extension to 64 bit, because the JVM only supports
  1160. signed divisions }
  1161. resize_stack_int_val(list,OS_32,OS_S64,false);
  1162. op:=OP_IDIV;
  1163. isdivu32:=true;
  1164. end
  1165. else
  1166. isdivu32:=false;
  1167. end;
  1168. procedure thlcgjvm.a_call_name_intern(list: TAsmList; pd: tprocdef; const s: string; inheritedcall: boolean);
  1169. var
  1170. opc: tasmop;
  1171. begin
  1172. {
  1173. invoke types:
  1174. * invokeinterface: call method from an interface
  1175. * invokespecial: invoke a constructor, method in a superclass,
  1176. or private instance method
  1177. * invokestatic: invoke a class method (private or not)
  1178. * invokevirtual: invoke a regular method
  1179. }
  1180. case pd.owner.symtabletype of
  1181. globalsymtable,
  1182. staticsymtable,
  1183. localsymtable:
  1184. { regular and nested procedures are turned into static methods }
  1185. opc:=a_invokestatic;
  1186. objectsymtable:
  1187. begin
  1188. case tobjectdef(pd.owner.defowner).objecttype of
  1189. odt_javaclass:
  1190. begin
  1191. if (po_staticmethod in pd.procoptions) then
  1192. opc:=a_invokestatic
  1193. else if (pd.visibility=vis_private) or
  1194. (pd.proctypeoption=potype_constructor) or
  1195. inheritedcall then
  1196. opc:=a_invokespecial
  1197. else
  1198. opc:=a_invokevirtual;
  1199. end;
  1200. odt_interfacejava:
  1201. { static interface methods are not allowed }
  1202. opc:=a_invokeinterface;
  1203. else
  1204. internalerror(2010122601);
  1205. end;
  1206. end;
  1207. else
  1208. internalerror(2010122602);
  1209. end;
  1210. list.concat(taicpu.op_sym(opc,current_asmdata.RefAsmSymbol(s)));
  1211. end;
  1212. procedure create_hlcodegen;
  1213. begin
  1214. hlcg:=thlcgjvm.create;
  1215. create_codegen;
  1216. end;
  1217. end.