hlcgcpu.pas 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233
  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. case op of
  314. OP_NOT:
  315. ;
  316. { the second argument here is an int rather than a long }
  317. OP_SHL,OP_SHR,OP_SAR:
  318. decstack(list,1);
  319. else
  320. decstack(list,2);
  321. end;
  322. end;
  323. else
  324. internalerror(2010120531);
  325. end;
  326. if trunc32 then
  327. begin
  328. list.concat(taicpu.op_none(a_l2i));
  329. decstack(list,1);
  330. end;
  331. end;
  332. procedure thlcgjvm.a_op_const_stack(list: TAsmList;op: topcg;size: tdef;a: aint);
  333. var
  334. trunc32: boolean;
  335. begin
  336. { use "integer to (wide)char" narrowing opcode for "and 65535" }
  337. if (op=OP_AND) and
  338. (def_cgsize(size) in [OS_16,OS_S16,OS_32,OS_S32]) and
  339. (a=65535) then
  340. list.concat(taicpu.op_none(a_i2c))
  341. else
  342. begin
  343. maybepreparedivu32(list,op,size,trunc32);
  344. case op of
  345. OP_NEG,OP_NOT:
  346. internalerror(2011010801);
  347. OP_SHL,OP_SHR,OP_SAR:
  348. { the second argument here is an int rather than a long }
  349. a_load_const_stack(list,s32inttype,a,R_INTREGISTER);
  350. else
  351. a_load_const_stack(list,size,a,R_INTREGISTER);
  352. end;
  353. a_op_stack(list,op,size,trunc32);
  354. end;
  355. end;
  356. procedure thlcgjvm.a_op_reg_stack(list: TAsmList; op: topcg; size: tdef; reg: tregister);
  357. var
  358. trunc32: boolean;
  359. begin
  360. maybepreparedivu32(list,op,size,trunc32);
  361. case op of
  362. OP_NEG,OP_NOT:
  363. ;
  364. OP_SHL,OP_SHR,OP_SAR:
  365. if not is_64bitint(size) then
  366. a_load_reg_stack(list,size,reg)
  367. else
  368. begin
  369. { the second argument here is an int rather than a long }
  370. if getsubreg(reg)=R_SUBQ then
  371. internalerror(2011010802);
  372. a_load_reg_stack(list,s32inttype,reg)
  373. end
  374. else
  375. a_load_reg_stack(list,size,reg);
  376. end;
  377. a_op_stack(list,op,size,trunc32);
  378. end;
  379. procedure thlcgjvm.a_op_ref_stack(list: TAsmList; op: topcg; size: tdef; const ref: treference);
  380. var
  381. trunc32: boolean;
  382. begin
  383. { ref must not be the stack top, because that may indicate an error
  384. (it means that we will perform an operation of the stack top onto
  385. itself, so that means the two values have been loaded manually prior
  386. to calling this routine, instead of letting this routine load one of
  387. them; if something like that is needed, call a_op_stack() directly) }
  388. if ref.base=NR_EVAL_STACK_BASE then
  389. internalerror(2010121102);
  390. maybepreparedivu32(list,op,size,trunc32);
  391. if not(op in [OP_NEG,OP_NOT]) then
  392. a_load_ref_stack(list,size,ref,prepare_stack_for_ref(list,ref,false));
  393. a_op_stack(list,op,size,trunc32);
  394. end;
  395. procedure thlcgjvm.a_op_loc_stack(list: TAsmList; op: topcg; size: tdef; const loc: tlocation);
  396. begin
  397. case loc.loc of
  398. LOC_REGISTER,LOC_CREGISTER:
  399. a_op_reg_stack(list,op,size,loc.register);
  400. LOC_REFERENCE,LOC_CREFERENCE:
  401. a_op_ref_stack(list,op,size,loc.reference);
  402. LOC_CONSTANT:
  403. a_op_const_stack(list,op,size,loc.value);
  404. else
  405. internalerror(2011011415)
  406. end;
  407. end;
  408. procedure thlcgjvm.a_cmp_stack_label(list: TAsmlist; size: tdef; cmp_op: topcmp; lab: tasmlabel);
  409. const
  410. opcmp2icmp: array[topcmp] of tasmop = (A_None,
  411. a_if_icmpeq,a_if_icmpgt,a_if_icmplt,a_if_icmpge,a_if_icmple,
  412. a_if_icmpne,a_if_icmple,a_if_icmplt,a_if_icmpge,a_if_icmpgt);
  413. var
  414. cgsize: tcgsize;
  415. begin
  416. case def2regtyp(size) of
  417. R_INTREGISTER:
  418. begin
  419. cgsize:=def_cgsize(size);
  420. case cgsize of
  421. OS_S8,OS_8,
  422. OS_16,OS_S16,
  423. OS_S32,OS_32:
  424. begin
  425. list.concat(taicpu.op_sym(opcmp2icmp[cmp_op],lab));
  426. decstack(list,2);
  427. end;
  428. OS_64,OS_S64:
  429. begin
  430. list.concat(taicpu.op_none(a_lcmp));
  431. decstack(list,3);
  432. list.concat(taicpu.op_sym(opcmp2if[cmp_op],lab));
  433. decstack(list,1);
  434. end;
  435. else
  436. internalerror(2010120538);
  437. end;
  438. end;
  439. R_ADDRESSREGISTER:
  440. begin
  441. case cmp_op of
  442. OC_EQ:
  443. list.concat(taicpu.op_sym(a_if_acmpeq,lab));
  444. OC_NE:
  445. list.concat(taicpu.op_sym(a_if_acmpne,lab));
  446. else
  447. internalerror(2010120537);
  448. end;
  449. decstack(list,2);
  450. end;
  451. else
  452. internalerror(2010120538);
  453. end;
  454. end;
  455. procedure thlcgjvm.maybe_adjust_cmp_stackval(list: TAsmlist; size: tdef; cmp_op: topcmp);
  456. begin
  457. if (cmp_op in [OC_EQ,OC_NE]) or
  458. (def2regtyp(size)<>R_INTREGISTER) then
  459. exit;
  460. { http://stackoverflow.com/questions/4068973/c-performing-signed-comparison-in-unsigned-variables-without-casting }
  461. case def_cgsize(size) of
  462. OS_32:
  463. a_op_const_stack(list,OP_XOR,size,cardinal($80000000));
  464. OS_64:
  465. a_op_const_stack(list,OP_XOR,size,aint($8000000000000000));
  466. end;
  467. end;
  468. function thlcgjvm.maybe_adjust_cmp_constval(size: tdef; cmp_op: topcmp; a: aint): aint;
  469. begin
  470. result:=a;
  471. if (cmp_op in [OC_EQ,OC_NE]) or
  472. (def2regtyp(size)<>R_INTREGISTER) then
  473. exit;
  474. case def_cgsize(size) of
  475. OS_32:
  476. result:=a xor cardinal($80000000);
  477. OS_64:
  478. result:=a xor aint($8000000000000000);
  479. end;
  480. end;
  481. procedure thlcgjvm.maybe_adjust_op_result(list: TAsmList; op: TOpCg; size: tdef);
  482. const
  483. overflowops = [OP_MUL,OP_SHL,OP_ADD,OP_SUB,OP_NOT,OP_NEG];
  484. begin
  485. if (op in overflowops) and
  486. (def_cgsize(size) in [OS_8,OS_S8,OS_16,OS_S16]) then
  487. resize_stack_int_val(list,OS_S32,def_cgsize(size),false);
  488. end;
  489. procedure thlcgjvm.gen_load_uninitialized_function_result(list: TAsmList; pd: tprocdef; resdef: tdef; const resloc: tcgpara);
  490. begin
  491. { constructors don't return anything in Java }
  492. if pd.proctypeoption=potype_constructor then
  493. exit;
  494. { must return a value of the correct type on the evaluation stack }
  495. case def2regtyp(resdef) of
  496. R_INTREGISTER,
  497. R_ADDRESSREGISTER:
  498. a_load_const_cgpara(list,resdef,0,resloc);
  499. R_FPUREGISTER:
  500. case tfloatdef(resdef).floattype of
  501. s32real:
  502. list.concat(taicpu.op_none(a_fconst_0));
  503. s64real:
  504. list.concat(taicpu.op_none(a_dconst_0));
  505. else
  506. internalerror(2011010302);
  507. end
  508. else
  509. internalerror(2011010301);
  510. end;
  511. end;
  512. function thlcgjvm.prepare_stack_for_ref(list: TAsmList; const ref: treference; dup: boolean): longint;
  513. var
  514. href: treference;
  515. begin
  516. result:=0;
  517. { fake location that indicates the value is already on the stack? }
  518. if (ref.base=NR_EVAL_STACK_BASE) then
  519. exit;
  520. if ref.arrayreftype=art_none then
  521. begin
  522. { non-array accesses cannot have an index reg }
  523. if ref.index<>NR_NO then
  524. internalerror(2010120509);
  525. if (ref.base<>NR_NO) then
  526. begin
  527. if (ref.base<>NR_STACK_POINTER_REG) then
  528. begin
  529. { regular field -> load self on the stack }
  530. a_load_reg_stack(list,voidpointertype,ref.base);
  531. if dup then
  532. begin
  533. list.concat(taicpu.op_none(a_dup));
  534. incstack(list,1);
  535. end;
  536. { field name/type encoded in symbol, no index/offset }
  537. if not assigned(ref.symbol) or
  538. (ref.offset<>0) then
  539. internalerror(2010120524);
  540. result:=1;
  541. end
  542. else
  543. begin
  544. { local variable -> offset encoded in opcode and nothing to
  545. do here, except for checking that it's a valid reference }
  546. if assigned(ref.symbol) then
  547. internalerror(2010120523);
  548. end;
  549. end
  550. else
  551. begin
  552. { static field -> nothing to do here, except for validity check }
  553. if not assigned(ref.symbol) or
  554. (ref.offset<>0) then
  555. internalerror(2010120525);
  556. end;
  557. end
  558. else
  559. begin
  560. { arrays have implicit dereference -> pointer to array must have been
  561. loaded into base reg }
  562. if (ref.base=NR_NO) or
  563. (ref.base=NR_STACK_POINTER_REG) then
  564. internalerror(2010120511);
  565. if assigned(ref.symbol) then
  566. internalerror(2010120512);
  567. { stack: ... -> ..., arrayref, index }
  568. { load array base address }
  569. a_load_reg_stack(list,voidpointertype,ref.base);
  570. { index can either be in a register, or located in a simple memory
  571. location (since we have to load it anyway) }
  572. if ref.arrayreftype=art_indexreg then
  573. begin
  574. if ref.index=NR_NO then
  575. internalerror(2010120513);
  576. { all array indices in Java are 32 bit ints }
  577. a_load_reg_stack(list,s32inttype,ref.index);
  578. end
  579. else
  580. begin
  581. reference_reset_base(href,ref.indexbase,ref.indexoffset,4);
  582. href.symbol:=href.indexsymbol;
  583. a_load_ref_stack(list,s32inttype,href,prepare_stack_for_ref(list,href,false));
  584. end;
  585. { adjustment of the index }
  586. if ref.offset<>0 then
  587. a_op_const_stack(list,OP_ADD,s32inttype,ref.offset);
  588. if dup then
  589. begin
  590. list.concat(taicpu.op_none(a_dup2));
  591. incstack(list,2);
  592. end;
  593. result:=2;
  594. end;
  595. end;
  596. procedure thlcgjvm.a_load_const_reg(list: TAsmList; tosize: tdef; a: aint; register: tregister);
  597. begin
  598. a_load_const_stack(list,tosize,a,def2regtyp(tosize));
  599. a_load_stack_reg(list,tosize,register);
  600. end;
  601. procedure thlcgjvm.a_load_const_ref(list: TAsmList; tosize: tdef; a: aint; const ref: treference);
  602. var
  603. extra_slots: longint;
  604. begin
  605. extra_slots:=prepare_stack_for_ref(list,ref,false);
  606. a_load_const_stack(list,tosize,a,def2regtyp(tosize));
  607. a_load_stack_ref(list,tosize,ref,extra_slots);
  608. decstack(list,extra_slots);
  609. end;
  610. procedure thlcgjvm.a_load_reg_ref(list: TAsmList; fromsize, tosize: tdef; register: tregister; const ref: treference);
  611. var
  612. extra_slots: longint;
  613. begin
  614. extra_slots:=prepare_stack_for_ref(list,ref,false);
  615. a_load_reg_stack(list,fromsize,register);
  616. a_load_stack_ref(list,tosize,ref,extra_slots);
  617. decstack(list,extra_slots);
  618. end;
  619. procedure thlcgjvm.a_load_reg_reg(list: TAsmList; fromsize, tosize: tdef; reg1, reg2: tregister);
  620. begin
  621. a_load_reg_stack(list,fromsize,reg1);
  622. if def2regtyp(fromsize)=R_INTREGISTER then
  623. resize_stack_int_val(list,def_cgsize(fromsize),def_cgsize(tosize),false);
  624. a_load_stack_reg(list,tosize,reg2);
  625. end;
  626. procedure thlcgjvm.a_load_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; register: tregister);
  627. var
  628. extra_slots: longint;
  629. begin
  630. extra_slots:=prepare_stack_for_ref(list,ref,false);
  631. a_load_ref_stack(list,fromsize,ref,extra_slots);
  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,register);
  635. end;
  636. procedure thlcgjvm.a_load_ref_ref(list: TAsmList; fromsize, tosize: tdef; const sref: treference; const dref: treference);
  637. var
  638. extra_sslots,
  639. extra_dslots: longint;
  640. begin
  641. { make sure the destination reference is on top, since in the end the
  642. order has to be "destref, value" -> first create "destref, sourceref" }
  643. extra_dslots:=prepare_stack_for_ref(list,dref,false);
  644. extra_sslots:=prepare_stack_for_ref(list,sref,false);
  645. a_load_ref_stack(list,fromsize,sref,extra_sslots);
  646. if def2regtyp(fromsize)=R_INTREGISTER then
  647. resize_stack_int_val(list,def_cgsize(fromsize),def_cgsize(tosize),dref.arrayreftype<>art_none);
  648. a_load_stack_ref(list,tosize,dref,extra_dslots);
  649. end;
  650. procedure thlcgjvm.a_loadaddr_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; r: tregister);
  651. begin
  652. internalerror(2010120534);
  653. end;
  654. procedure thlcgjvm.a_op_const_reg(list: TAsmList; Op: TOpCG; size: tdef; a: Aint; reg: TRegister);
  655. begin
  656. a_op_const_reg_reg(list,op,size,a,reg,reg);
  657. end;
  658. procedure thlcgjvm.a_op_const_reg_reg(list: TAsmList; op: TOpCg; size: tdef; a: aint; src, dst: tregister);
  659. begin
  660. a_load_reg_stack(list,size,src);
  661. a_op_const_stack(list,op,size,a);
  662. a_load_stack_reg(list,size,dst);
  663. end;
  664. procedure thlcgjvm.a_op_const_ref(list: TAsmList; Op: TOpCG; size: tdef; a: Aint; const ref: TReference);
  665. var
  666. extra_slots: longint;
  667. begin
  668. extra_slots:=prepare_stack_for_ref(list,ref,true);
  669. { TODO, here or in peepholeopt: use iinc when possible }
  670. a_load_ref_stack(list,size,ref,extra_slots);
  671. a_op_const_stack(list,op,size,a);
  672. a_load_stack_ref(list,size,ref,extra_slots);
  673. end;
  674. procedure thlcgjvm.a_op_ref_reg(list: TAsmList; Op: TOpCG; size: tdef; const ref: TReference; reg: TRegister);
  675. begin
  676. a_load_reg_stack(list,size,reg);
  677. a_op_ref_stack(list,op,size,ref);
  678. a_load_stack_reg(list,size,reg);
  679. end;
  680. procedure thlcgjvm.a_op_reg_reg_reg(list: TAsmList; op: TOpCg; size: tdef; src1, src2, dst: tregister);
  681. begin
  682. a_load_reg_stack(list,size,src2);
  683. a_op_reg_stack(list,op,size,src1);
  684. a_load_stack_reg(list,size,dst);
  685. end;
  686. procedure thlcgjvm.a_op_reg_reg(list: TAsmList; Op: TOpCG; size: tdef; reg1, reg2: TRegister);
  687. begin
  688. a_op_reg_reg_reg(list,op,size,reg1,reg2,reg2);
  689. end;
  690. procedure thlcgjvm.a_cmp_const_ref_label(list: TAsmList; size: tdef; cmp_op: topcmp; a: aint; const ref: treference; l: tasmlabel);
  691. begin
  692. a_load_const_stack(list,size,maybe_adjust_cmp_constval(size,cmp_op,a),R_INTREGISTER);
  693. if ref.base<>NR_EVAL_STACK_BASE then
  694. a_load_ref_stack(list,size,ref,prepare_stack_for_ref(list,ref,false))
  695. else
  696. list.concat(taicpu.op_none(a_swap));
  697. maybe_adjust_cmp_stackval(list,size,cmp_op);
  698. a_cmp_stack_label(list,size,cmp_op,l);
  699. end;
  700. procedure thlcgjvm.a_cmp_const_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; a: aint; reg: tregister; l: tasmlabel);
  701. begin
  702. a_load_const_stack(list,size,maybe_adjust_cmp_constval(size,cmp_op,a),R_INTREGISTER);
  703. a_load_reg_stack(list,size,reg);
  704. maybe_adjust_cmp_stackval(list,size,cmp_op);
  705. a_cmp_stack_label(list,size,cmp_op,l);
  706. end;
  707. procedure thlcgjvm.a_cmp_ref_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; const ref: treference; reg: tregister; l: tasmlabel);
  708. begin
  709. if ref.base<>NR_EVAL_STACK_BASE then
  710. a_load_ref_stack(list,size,ref,prepare_stack_for_ref(list,ref,false));
  711. maybe_adjust_cmp_stackval(list,size,cmp_op);
  712. a_load_reg_stack(list,size,reg);
  713. maybe_adjust_cmp_stackval(list,size,cmp_op);
  714. a_cmp_stack_label(list,size,cmp_op,l);
  715. end;
  716. procedure thlcgjvm.a_cmp_reg_ref_label(list: TAsmList; size: tdef; cmp_op: topcmp; reg: tregister; const ref: treference; l: tasmlabel);
  717. begin
  718. a_load_reg_stack(list,size,reg);
  719. maybe_adjust_cmp_stackval(list,size,cmp_op);
  720. if ref.base<>NR_EVAL_STACK_BASE then
  721. a_load_ref_stack(list,size,ref,prepare_stack_for_ref(list,ref,false))
  722. else
  723. list.concat(taicpu.op_none(a_swap));
  724. maybe_adjust_cmp_stackval(list,size,cmp_op);
  725. a_cmp_stack_label(list,size,cmp_op,l);
  726. end;
  727. procedure thlcgjvm.a_cmp_reg_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; reg1, reg2: tregister; l: tasmlabel);
  728. begin
  729. a_load_reg_stack(list,size,reg1);
  730. maybe_adjust_cmp_stackval(list,size,cmp_op);
  731. a_load_reg_stack(list,size,reg2);
  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_jmp_always(list: TAsmList; l: tasmlabel);
  736. begin
  737. list.concat(taicpu.op_sym(a_goto,current_asmdata.RefAsmSymbol(l.name)));
  738. end;
  739. procedure thlcgjvm.a_loadfpu_ref_ref(list: TAsmList; fromsize, tosize: tdef; const ref1, ref2: treference);
  740. var
  741. dstack_slots: longint;
  742. begin
  743. dstack_slots:=prepare_stack_for_ref(list,ref2,false);
  744. a_load_ref_stack(list,fromsize,ref1,prepare_stack_for_ref(list,ref1,false));
  745. resizestackfpuval(list,def_cgsize(fromsize),def_cgsize(tosize));
  746. a_load_stack_ref(list,tosize,ref2,dstack_slots);
  747. end;
  748. procedure thlcgjvm.a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; reg: tregister);
  749. begin
  750. a_load_ref_stack(list,fromsize,ref,prepare_stack_for_ref(list,ref,false));
  751. resizestackfpuval(list,def_cgsize(fromsize),def_cgsize(tosize));
  752. a_load_stack_reg(list,tosize,reg);
  753. end;
  754. procedure thlcgjvm.a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tdef; reg: tregister; const ref: treference);
  755. var
  756. dstack_slots: longint;
  757. begin
  758. dstack_slots:=prepare_stack_for_ref(list,ref,false);
  759. a_load_reg_stack(list,fromsize,reg);
  760. resizestackfpuval(list,def_cgsize(fromsize),def_cgsize(tosize));
  761. a_load_stack_ref(list,tosize,ref,dstack_slots);
  762. end;
  763. procedure thlcgjvm.a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tdef; reg1, reg2: tregister);
  764. begin
  765. a_load_reg_stack(list,fromsize,reg1);
  766. resizestackfpuval(list,def_cgsize(fromsize),def_cgsize(tosize));
  767. a_load_stack_reg(list,tosize,reg2);
  768. end;
  769. procedure thlcgjvm.g_proc_entry(list: TAsmList; localsize: longint; nostackframe: boolean);
  770. begin
  771. { the localsize is based on tg.lasttemp -> already in terms of stack
  772. slots rather than bytes }
  773. list.concat(tai_directive.Create(asd_jlimit,'locals '+tostr(localsize)));
  774. list.concat(tai_directive.Create(asd_jlimit,'stack '+tostr(fmaxevalstackheight)));
  775. end;
  776. procedure thlcgjvm.g_proc_exit(list: TAsmList; parasize: longint; nostackframe: boolean);
  777. var
  778. retdef: tdef;
  779. opc: tasmop;
  780. begin
  781. if current_procinfo.procdef.proctypeoption=potype_constructor then
  782. retdef:=voidtype
  783. else
  784. retdef:=current_procinfo.procdef.returndef;
  785. case retdef.typ of
  786. orddef:
  787. case torddef(retdef).ordtype of
  788. uvoid:
  789. opc:=a_return;
  790. s64bit,
  791. u64bit,
  792. scurrency:
  793. opc:=a_lreturn;
  794. else
  795. opc:=a_ireturn;
  796. end;
  797. floatdef:
  798. case tfloatdef(retdef).floattype of
  799. s32real:
  800. opc:=a_freturn;
  801. s64real:
  802. opc:=a_dreturn;
  803. else
  804. internalerror(2011010213);
  805. end;
  806. else
  807. opc:=a_areturn;
  808. end;
  809. list.concat(taicpu.op_none(opc));
  810. end;
  811. procedure thlcgjvm.gen_load_return_value(list: TAsmList);
  812. begin
  813. { constructors don't return anything in the jvm }
  814. if current_procinfo.procdef.proctypeoption=potype_constructor then
  815. exit;
  816. inherited gen_load_return_value(list);
  817. end;
  818. procedure thlcgjvm.record_generated_code_for_procdef(pd: tprocdef; code, data: TAsmList);
  819. begin
  820. { add something to the al_procedures list as well, because if all al_*
  821. lists are empty, the assembler writer isn't called }
  822. if not code.empty and
  823. current_asmdata.asmlists[al_procedures].empty then
  824. current_asmdata.asmlists[al_procedures].concat(tai_align.Create(4));
  825. pd.exprasmlist:=TAsmList.create;
  826. pd.exprasmlist.concatlist(code);
  827. if assigned(data) and
  828. not data.empty then
  829. internalerror(2010122801);
  830. end;
  831. procedure thlcgjvm.a_load_stack_reg(list: TAsmList; size: tdef; reg: tregister);
  832. var
  833. opc: tasmop;
  834. finishandval: aint;
  835. begin
  836. opc:=loadstoreopc(size,false,false,finishandval);
  837. list.concat(taicpu.op_reg(opc,reg));
  838. decstack(list,1+ord(size.size>4));
  839. end;
  840. procedure thlcgjvm.a_load_stack_ref(list: TAsmList; size: tdef; const ref: treference; extra_slots: longint);
  841. var
  842. opc: tasmop;
  843. finishandval: aint;
  844. begin
  845. { fake location that indicates the value has to remain on the stack }
  846. if ref.base=NR_EVAL_STACK_BASE then
  847. exit;
  848. opc:=loadstoreopcref(size,false,ref,finishandval);
  849. if ref.arrayreftype=art_none then
  850. list.concat(taicpu.op_ref(opc,ref))
  851. else
  852. list.concat(taicpu.op_none(opc));
  853. decstack(list,1+ord(size.size>4)+extra_slots);
  854. end;
  855. procedure thlcgjvm.a_load_reg_stack(list: TAsmList; size: tdef; reg: tregister);
  856. var
  857. opc: tasmop;
  858. finishandval: aint;
  859. begin
  860. opc:=loadstoreopc(size,true,false,finishandval);
  861. list.concat(taicpu.op_reg(opc,reg));
  862. if finishandval<>-1 then
  863. a_op_const_stack(list,OP_AND,size,finishandval);
  864. incstack(list,1+ord(size.size>4));
  865. end;
  866. procedure thlcgjvm.a_load_ref_stack(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 is already on the stack? }
  872. if (ref.base=NR_EVAL_STACK_BASE) then
  873. exit;
  874. opc:=loadstoreopcref(size,true,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. if finishandval<>-1 then
  880. a_op_const_stack(list,OP_AND,size,finishandval);
  881. incstack(list,1+ord(size.size>4)-extra_slots);
  882. end;
  883. function thlcgjvm.loadstoreopcref(def: tdef; isload: boolean; const ref: treference; out finishandval: aint): tasmop;
  884. const
  885. { isload static }
  886. getputopc: array[boolean,boolean] of tasmop =
  887. ((a_putfield,a_putstatic),
  888. (a_getfield,a_getstatic));
  889. var
  890. size: aint;
  891. begin
  892. if assigned(ref.symbol) then
  893. begin
  894. finishandval:=-1;
  895. { -> either a global (static) field, or a regular field. If a regular
  896. field, then ref.base contains the self pointer, otherwise
  897. ref.base=NR_NO. In both cases, the symbol contains all other
  898. information (combined field name and type descriptor) }
  899. result:=getputopc[isload,ref.base=NR_NO];
  900. end
  901. else
  902. result:=loadstoreopc(def,isload,ref.arrayreftype<>art_none,finishandval);
  903. end;
  904. function thlcgjvm.loadstoreopc(def: tdef; isload, isarray: boolean; out finishandval: aint): tasmop;
  905. var
  906. size: longint;
  907. begin
  908. finishandval:=-1;
  909. case def2regtyp(def) of
  910. R_INTREGISTER:
  911. begin
  912. size:=def.size;
  913. if not isarray then
  914. begin
  915. case size of
  916. 1,2,3,4:
  917. if isload then
  918. result:=a_iload
  919. else
  920. result:=a_istore;
  921. 8:
  922. if isload then
  923. result:=a_lload
  924. else
  925. result:=a_lstore;
  926. end;
  927. end
  928. { array }
  929. else if isload then
  930. begin
  931. case size of
  932. 1:
  933. begin
  934. result:=a_baload;
  935. if not is_signed(def) then
  936. finishandval:=255;
  937. end;
  938. 2:
  939. begin
  940. if is_widechar(def) then
  941. result:=a_caload
  942. else
  943. begin
  944. result:=a_saload;
  945. { if we'd treat arrays of word as "array of widechar" we
  946. could use a_caload, but that would make for even more
  947. awkward interfacing with external Java code }
  948. if not is_signed(def) then
  949. finishandval:=65535;
  950. end;
  951. end;
  952. 4: result:=a_iaload;
  953. 8: result:=a_laload;
  954. else
  955. internalerror(2010120503);
  956. end
  957. end
  958. else
  959. begin
  960. case size of
  961. 1: result:=a_bastore;
  962. 2: if not is_widechar(def) then
  963. result:=a_sastore
  964. else
  965. result:=a_castore;
  966. 4: result:=a_iastore;
  967. 8: result:=a_lastore;
  968. else
  969. internalerror(2010120508);
  970. end
  971. end
  972. end;
  973. R_ADDRESSREGISTER:
  974. if not isarray then
  975. if isload then
  976. result:=a_aload
  977. else
  978. result:=a_astore
  979. else if isload then
  980. result:=a_aaload
  981. else
  982. result:=a_aastore;
  983. R_FPUREGISTER:
  984. begin
  985. case tfloatdef(def).floattype of
  986. s32real:
  987. if not isarray then
  988. if isload then
  989. result:=a_fload
  990. else
  991. result:=a_fstore
  992. else if isload then
  993. result:=a_faload
  994. else
  995. result:=a_fastore;
  996. s64real:
  997. if not isarray then
  998. if isload then
  999. result:=a_dload
  1000. else
  1001. result:=a_dstore
  1002. else if isload then
  1003. result:=a_daload
  1004. else
  1005. result:=a_dastore;
  1006. else
  1007. internalerror(2010120504);
  1008. end
  1009. end
  1010. else
  1011. internalerror(2010120502);
  1012. end;
  1013. end;
  1014. procedure thlcgjvm.resize_stack_int_val(list: TAsmList; fromsize, tosize: tcgsize; forarraystore: boolean);
  1015. begin
  1016. if fromsize in [OS_S64,OS_64] then
  1017. begin
  1018. if not(tosize in [OS_S64,OS_64]) then
  1019. begin
  1020. { truncate }
  1021. list.concat(taicpu.op_none(a_l2i));
  1022. decstack(list,1);
  1023. end;
  1024. end
  1025. else if tosize in [OS_S64,OS_64] then
  1026. begin
  1027. { extend }
  1028. list.concat(taicpu.op_none(a_i2l));
  1029. incstack(list,1);
  1030. { if it was an unsigned 32 bit value, remove sign extension }
  1031. if fromsize=OS_32 then
  1032. a_op_const_stack(list,OP_AND,s64inttype,cardinal($ffffffff));
  1033. end;
  1034. { if the value is immediately stored to an array afterwards, the store
  1035. instruction will properly truncate the value; otherwise we may need
  1036. additional truncation, except for 64/32 bit conversions, which are
  1037. already handled above }
  1038. if not forarraystore and
  1039. (not(fromsize in [OS_S64,OS_64,OS_32,OS_S32]) or
  1040. not(tosize in [OS_S64,OS_64,OS_32,OS_S32])) and
  1041. (tcgsize2size[fromsize]>tcgsize2size[tosize]) or
  1042. ((tcgsize2size[fromsize]=tcgsize2size[tosize]) and
  1043. (fromsize<>tosize)) or
  1044. { needs to mask out the sign in the top 16 bits }
  1045. ((fromsize=OS_S8) and
  1046. (tosize=OS_16)) then
  1047. case tosize of
  1048. OS_8:
  1049. a_op_const_stack(list,OP_AND,s32inttype,255);
  1050. OS_S8:
  1051. list.concat(taicpu.op_none(a_i2b));
  1052. OS_16:
  1053. list.concat(taicpu.op_none(a_i2c));
  1054. OS_S16:
  1055. list.concat(taicpu.op_none(a_i2s));
  1056. end;
  1057. end;
  1058. procedure thlcgjvm.resizestackfpuval(list: TAsmList; fromsize, tosize: tcgsize);
  1059. begin
  1060. if (fromsize=OS_F32) and
  1061. (tosize=OS_F64) then
  1062. begin
  1063. list.concat(taicpu.op_none(a_f2d));
  1064. incstack(list,1);
  1065. end
  1066. else if (fromsize=OS_F64) and
  1067. (tosize=OS_F32) then
  1068. begin
  1069. list.concat(taicpu.op_none(a_d2f));
  1070. decstack(list,1);
  1071. end;
  1072. end;
  1073. procedure thlcgjvm.maybepreparedivu32(list: TAsmList; var op: topcg; size: tdef; out isdivu32: boolean);
  1074. begin
  1075. if (op=OP_DIV) and
  1076. (def_cgsize(size)=OS_32) then
  1077. begin
  1078. { needs zero-extension to 64 bit, because the JVM only supports
  1079. signed divisions }
  1080. resize_stack_int_val(list,OS_32,OS_S64,false);
  1081. op:=OP_IDIV;
  1082. isdivu32:=true;
  1083. end
  1084. else
  1085. isdivu32:=false;
  1086. end;
  1087. procedure thlcgjvm.a_call_name_intern(list: TAsmList; pd: tprocdef; const s: string; inheritedcall: boolean);
  1088. var
  1089. opc: tasmop;
  1090. begin
  1091. {
  1092. invoke types:
  1093. * invokeinterface: call method from an interface
  1094. * invokespecial: invoke a constructor, method in a superclass,
  1095. or private instance method
  1096. * invokestatic: invoke a class method (private or not)
  1097. * invokevirtual: invoke a regular method
  1098. }
  1099. case pd.owner.symtabletype of
  1100. globalsymtable,
  1101. staticsymtable,
  1102. localsymtable:
  1103. { regular and nested procedures are turned into static methods }
  1104. opc:=a_invokestatic;
  1105. objectsymtable:
  1106. begin
  1107. case tobjectdef(pd.owner.defowner).objecttype of
  1108. odt_javaclass:
  1109. begin
  1110. if (po_staticmethod in pd.procoptions) then
  1111. opc:=a_invokestatic
  1112. else if (pd.visibility=vis_private) or
  1113. (pd.proctypeoption=potype_constructor) or
  1114. inheritedcall then
  1115. opc:=a_invokespecial
  1116. else
  1117. opc:=a_invokevirtual;
  1118. end;
  1119. odt_interfacejava:
  1120. { static interface methods are not allowed }
  1121. opc:=a_invokeinterface;
  1122. else
  1123. internalerror(2010122601);
  1124. end;
  1125. end;
  1126. else
  1127. internalerror(2010122602);
  1128. end;
  1129. list.concat(taicpu.op_sym(opc,current_asmdata.RefAsmSymbol(s)));
  1130. end;
  1131. procedure create_hlcodegen;
  1132. begin
  1133. hlcg:=thlcgjvm.create;
  1134. create_codegen;
  1135. end;
  1136. end.