hlcgcpu.pas 51 KB

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