hlcgcpu.pas 45 KB

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