hlcgcpu.pas 47 KB

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