2
0

hlcgcpu.pas 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229
  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. end;
  609. procedure thlcgjvm.a_load_reg_ref(list: TAsmList; fromsize, tosize: tdef; register: tregister; const ref: treference);
  610. var
  611. extra_slots: longint;
  612. begin
  613. extra_slots:=prepare_stack_for_ref(list,ref,false);
  614. a_load_reg_stack(list,fromsize,register);
  615. a_load_stack_ref(list,tosize,ref,extra_slots);
  616. end;
  617. procedure thlcgjvm.a_load_reg_reg(list: TAsmList; fromsize, tosize: tdef; reg1, reg2: tregister);
  618. begin
  619. a_load_reg_stack(list,fromsize,reg1);
  620. if def2regtyp(fromsize)=R_INTREGISTER then
  621. resize_stack_int_val(list,def_cgsize(fromsize),def_cgsize(tosize),false);
  622. a_load_stack_reg(list,tosize,reg2);
  623. end;
  624. procedure thlcgjvm.a_load_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; register: tregister);
  625. var
  626. extra_slots: longint;
  627. begin
  628. extra_slots:=prepare_stack_for_ref(list,ref,false);
  629. a_load_ref_stack(list,fromsize,ref,extra_slots);
  630. if def2regtyp(fromsize)=R_INTREGISTER then
  631. resize_stack_int_val(list,def_cgsize(fromsize),def_cgsize(tosize),false);
  632. a_load_stack_reg(list,tosize,register);
  633. end;
  634. procedure thlcgjvm.a_load_ref_ref(list: TAsmList; fromsize, tosize: tdef; const sref: treference; const dref: treference);
  635. var
  636. extra_sslots,
  637. extra_dslots: longint;
  638. begin
  639. { make sure the destination reference is on top, since in the end the
  640. order has to be "destref, value" -> first create "destref, sourceref" }
  641. extra_dslots:=prepare_stack_for_ref(list,dref,false);
  642. extra_sslots:=prepare_stack_for_ref(list,sref,false);
  643. a_load_ref_stack(list,fromsize,sref,extra_sslots);
  644. if def2regtyp(fromsize)=R_INTREGISTER then
  645. resize_stack_int_val(list,def_cgsize(fromsize),def_cgsize(tosize),dref.arrayreftype<>art_none);
  646. a_load_stack_ref(list,tosize,dref,extra_dslots);
  647. end;
  648. procedure thlcgjvm.a_loadaddr_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; r: tregister);
  649. begin
  650. internalerror(2010120534);
  651. end;
  652. procedure thlcgjvm.a_op_const_reg(list: TAsmList; Op: TOpCG; size: tdef; a: Aint; reg: TRegister);
  653. begin
  654. a_op_const_reg_reg(list,op,size,a,reg,reg);
  655. end;
  656. procedure thlcgjvm.a_op_const_reg_reg(list: TAsmList; op: TOpCg; size: tdef; a: aint; src, dst: tregister);
  657. begin
  658. a_load_reg_stack(list,size,src);
  659. a_op_const_stack(list,op,size,a);
  660. a_load_stack_reg(list,size,dst);
  661. end;
  662. procedure thlcgjvm.a_op_const_ref(list: TAsmList; Op: TOpCG; size: tdef; a: Aint; const ref: TReference);
  663. var
  664. extra_slots: longint;
  665. begin
  666. extra_slots:=prepare_stack_for_ref(list,ref,true);
  667. { TODO, here or in peepholeopt: use iinc when possible }
  668. a_load_ref_stack(list,size,ref,extra_slots);
  669. a_op_const_stack(list,op,size,a);
  670. a_load_stack_ref(list,size,ref,extra_slots);
  671. end;
  672. procedure thlcgjvm.a_op_ref_reg(list: TAsmList; Op: TOpCG; size: tdef; const ref: TReference; reg: TRegister);
  673. begin
  674. a_load_reg_stack(list,size,reg);
  675. a_op_ref_stack(list,op,size,ref);
  676. a_load_stack_reg(list,size,reg);
  677. end;
  678. procedure thlcgjvm.a_op_reg_reg_reg(list: TAsmList; op: TOpCg; size: tdef; src1, src2, dst: tregister);
  679. begin
  680. a_load_reg_stack(list,size,src2);
  681. a_op_reg_stack(list,op,size,src1);
  682. a_load_stack_reg(list,size,dst);
  683. end;
  684. procedure thlcgjvm.a_op_reg_reg(list: TAsmList; Op: TOpCG; size: tdef; reg1, reg2: TRegister);
  685. begin
  686. a_op_reg_reg_reg(list,op,size,reg1,reg2,reg2);
  687. end;
  688. procedure thlcgjvm.a_cmp_const_ref_label(list: TAsmList; size: tdef; cmp_op: topcmp; a: aint; const ref: treference; l: tasmlabel);
  689. begin
  690. if ref.base<>NR_EVAL_STACK_BASE then
  691. a_load_ref_stack(list,size,ref,prepare_stack_for_ref(list,ref,false));
  692. maybe_adjust_cmp_stackval(list,size,cmp_op);
  693. a_load_const_stack(list,size,maybe_adjust_cmp_constval(size,cmp_op,a),def2regtyp(size));
  694. a_cmp_stack_label(list,size,cmp_op,l);
  695. end;
  696. procedure thlcgjvm.a_cmp_const_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; a: aint; reg: tregister; l: tasmlabel);
  697. begin
  698. a_load_reg_stack(list,size,reg);
  699. maybe_adjust_cmp_stackval(list,size,cmp_op);
  700. a_load_const_stack(list,size,maybe_adjust_cmp_constval(size,cmp_op,a),def2regtyp(size));
  701. a_cmp_stack_label(list,size,cmp_op,l);
  702. end;
  703. procedure thlcgjvm.a_cmp_ref_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; const ref: treference; reg: tregister; l: tasmlabel);
  704. begin
  705. a_load_reg_stack(list,size,reg);
  706. maybe_adjust_cmp_stackval(list,size,cmp_op);
  707. if ref.base<>NR_EVAL_STACK_BASE then
  708. a_load_ref_stack(list,size,ref,prepare_stack_for_ref(list,ref,false))
  709. else
  710. list.concat(taicpu.op_none(a_swap));
  711. maybe_adjust_cmp_stackval(list,size,cmp_op);
  712. a_cmp_stack_label(list,size,cmp_op,l);
  713. end;
  714. procedure thlcgjvm.a_cmp_reg_ref_label(list: TAsmList; size: tdef; cmp_op: topcmp; reg: tregister; const ref: treference; l: tasmlabel);
  715. begin
  716. if ref.base<>NR_EVAL_STACK_BASE then
  717. a_load_ref_stack(list,size,ref,prepare_stack_for_ref(list,ref,false));
  718. maybe_adjust_cmp_stackval(list,size,cmp_op);
  719. a_load_reg_stack(list,size,reg);
  720. maybe_adjust_cmp_stackval(list,size,cmp_op);
  721. a_cmp_stack_label(list,size,cmp_op,l);
  722. end;
  723. procedure thlcgjvm.a_cmp_reg_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; reg1, reg2: tregister; l: tasmlabel);
  724. begin
  725. a_load_reg_stack(list,size,reg2);
  726. maybe_adjust_cmp_stackval(list,size,cmp_op);
  727. a_load_reg_stack(list,size,reg1);
  728. maybe_adjust_cmp_stackval(list,size,cmp_op);
  729. a_cmp_stack_label(list,size,cmp_op,l);
  730. end;
  731. procedure thlcgjvm.a_jmp_always(list: TAsmList; l: tasmlabel);
  732. begin
  733. list.concat(taicpu.op_sym(a_goto,current_asmdata.RefAsmSymbol(l.name)));
  734. end;
  735. procedure thlcgjvm.a_loadfpu_ref_ref(list: TAsmList; fromsize, tosize: tdef; const ref1, ref2: treference);
  736. var
  737. dstack_slots: longint;
  738. begin
  739. dstack_slots:=prepare_stack_for_ref(list,ref2,false);
  740. a_load_ref_stack(list,fromsize,ref1,prepare_stack_for_ref(list,ref1,false));
  741. resizestackfpuval(list,def_cgsize(fromsize),def_cgsize(tosize));
  742. a_load_stack_ref(list,tosize,ref2,dstack_slots);
  743. end;
  744. procedure thlcgjvm.a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; reg: tregister);
  745. begin
  746. a_load_ref_stack(list,fromsize,ref,prepare_stack_for_ref(list,ref,false));
  747. resizestackfpuval(list,def_cgsize(fromsize),def_cgsize(tosize));
  748. a_load_stack_reg(list,tosize,reg);
  749. end;
  750. procedure thlcgjvm.a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tdef; reg: tregister; const ref: treference);
  751. var
  752. dstack_slots: longint;
  753. begin
  754. dstack_slots:=prepare_stack_for_ref(list,ref,false);
  755. a_load_reg_stack(list,fromsize,reg);
  756. resizestackfpuval(list,def_cgsize(fromsize),def_cgsize(tosize));
  757. a_load_stack_ref(list,tosize,ref,dstack_slots);
  758. end;
  759. procedure thlcgjvm.a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tdef; reg1, reg2: tregister);
  760. begin
  761. a_load_reg_stack(list,fromsize,reg1);
  762. resizestackfpuval(list,def_cgsize(fromsize),def_cgsize(tosize));
  763. a_load_stack_reg(list,tosize,reg2);
  764. end;
  765. procedure thlcgjvm.g_proc_entry(list: TAsmList; localsize: longint; nostackframe: boolean);
  766. begin
  767. { the localsize is based on tg.lasttemp -> already in terms of stack
  768. slots rather than bytes }
  769. list.concat(tai_directive.Create(asd_jlimit,'locals '+tostr(localsize)));
  770. list.concat(tai_directive.Create(asd_jlimit,'stack '+tostr(fmaxevalstackheight)));
  771. end;
  772. procedure thlcgjvm.g_proc_exit(list: TAsmList; parasize: longint; nostackframe: boolean);
  773. var
  774. retdef: tdef;
  775. opc: tasmop;
  776. begin
  777. if current_procinfo.procdef.proctypeoption=potype_constructor then
  778. retdef:=voidtype
  779. else
  780. retdef:=current_procinfo.procdef.returndef;
  781. case retdef.typ of
  782. orddef:
  783. case torddef(retdef).ordtype of
  784. uvoid:
  785. opc:=a_return;
  786. s64bit,
  787. u64bit,
  788. scurrency:
  789. opc:=a_lreturn;
  790. else
  791. opc:=a_ireturn;
  792. end;
  793. floatdef:
  794. case tfloatdef(retdef).floattype of
  795. s32real:
  796. opc:=a_freturn;
  797. s64real:
  798. opc:=a_dreturn;
  799. else
  800. internalerror(2011010213);
  801. end;
  802. else
  803. opc:=a_areturn;
  804. end;
  805. list.concat(taicpu.op_none(opc));
  806. end;
  807. procedure thlcgjvm.gen_load_return_value(list: TAsmList);
  808. begin
  809. { constructors don't return anything in the jvm }
  810. if current_procinfo.procdef.proctypeoption=potype_constructor then
  811. exit;
  812. inherited gen_load_return_value(list);
  813. end;
  814. procedure thlcgjvm.record_generated_code_for_procdef(pd: tprocdef; code, data: TAsmList);
  815. begin
  816. { add something to the al_procedures list as well, because if all al_*
  817. lists are empty, the assembler writer isn't called }
  818. if not code.empty and
  819. current_asmdata.asmlists[al_procedures].empty then
  820. current_asmdata.asmlists[al_procedures].concat(tai_align.Create(4));
  821. pd.exprasmlist:=TAsmList.create;
  822. pd.exprasmlist.concatlist(code);
  823. if assigned(data) and
  824. not data.empty then
  825. internalerror(2010122801);
  826. end;
  827. procedure thlcgjvm.a_load_stack_reg(list: TAsmList; size: tdef; reg: tregister);
  828. var
  829. opc: tasmop;
  830. finishandval: aint;
  831. begin
  832. opc:=loadstoreopc(size,false,false,finishandval);
  833. list.concat(taicpu.op_reg(opc,reg));
  834. decstack(list,1+ord(size.size>4));
  835. end;
  836. procedure thlcgjvm.a_load_stack_ref(list: TAsmList; size: tdef; const ref: treference; extra_slots: longint);
  837. var
  838. opc: tasmop;
  839. finishandval: aint;
  840. begin
  841. { fake location that indicates the value has to remain on the stack }
  842. if ref.base=NR_EVAL_STACK_BASE then
  843. exit;
  844. opc:=loadstoreopcref(size,false,ref,finishandval);
  845. if ref.arrayreftype=art_none then
  846. list.concat(taicpu.op_ref(opc,ref))
  847. else
  848. list.concat(taicpu.op_none(opc));
  849. decstack(list,1+ord(size.size>4)+extra_slots);
  850. end;
  851. procedure thlcgjvm.a_load_reg_stack(list: TAsmList; size: tdef; reg: tregister);
  852. var
  853. opc: tasmop;
  854. finishandval: aint;
  855. begin
  856. opc:=loadstoreopc(size,true,false,finishandval);
  857. list.concat(taicpu.op_reg(opc,reg));
  858. if finishandval<>-1 then
  859. a_op_const_stack(list,OP_AND,size,finishandval);
  860. incstack(list,1+ord(size.size>4));
  861. end;
  862. procedure thlcgjvm.a_load_ref_stack(list: TAsmList; size: tdef; const ref: treference; extra_slots: longint);
  863. var
  864. opc: tasmop;
  865. finishandval: aint;
  866. begin
  867. { fake location that indicates the value is already on the stack? }
  868. if (ref.base=NR_EVAL_STACK_BASE) then
  869. exit;
  870. opc:=loadstoreopcref(size,true,ref,finishandval);
  871. if ref.arrayreftype=art_none then
  872. list.concat(taicpu.op_ref(opc,ref))
  873. else
  874. list.concat(taicpu.op_none(opc));
  875. if finishandval<>-1 then
  876. a_op_const_stack(list,OP_AND,size,finishandval);
  877. incstack(list,1+ord(size.size>4)-extra_slots);
  878. end;
  879. function thlcgjvm.loadstoreopcref(def: tdef; isload: boolean; const ref: treference; out finishandval: aint): tasmop;
  880. const
  881. { isload static }
  882. getputopc: array[boolean,boolean] of tasmop =
  883. ((a_putfield,a_putstatic),
  884. (a_getfield,a_getstatic));
  885. var
  886. size: aint;
  887. begin
  888. if assigned(ref.symbol) then
  889. begin
  890. finishandval:=-1;
  891. { -> either a global (static) field, or a regular field. If a regular
  892. field, then ref.base contains the self pointer, otherwise
  893. ref.base=NR_NO. In both cases, the symbol contains all other
  894. information (combined field name and type descriptor) }
  895. result:=getputopc[isload,ref.base=NR_NO];
  896. end
  897. else
  898. result:=loadstoreopc(def,isload,ref.arrayreftype<>art_none,finishandval);
  899. end;
  900. function thlcgjvm.loadstoreopc(def: tdef; isload, isarray: boolean; out finishandval: aint): tasmop;
  901. var
  902. size: longint;
  903. begin
  904. finishandval:=-1;
  905. case def2regtyp(def) of
  906. R_INTREGISTER:
  907. begin
  908. size:=def.size;
  909. if not isarray then
  910. begin
  911. case size of
  912. 1,2,3,4:
  913. if isload then
  914. result:=a_iload
  915. else
  916. result:=a_istore;
  917. 8:
  918. if isload then
  919. result:=a_lload
  920. else
  921. result:=a_lstore;
  922. end;
  923. end
  924. { array }
  925. else if isload then
  926. begin
  927. case size of
  928. 1:
  929. begin
  930. result:=a_baload;
  931. if not is_signed(def) then
  932. finishandval:=255;
  933. end;
  934. 2:
  935. begin
  936. if is_widechar(def) then
  937. result:=a_caload
  938. else
  939. begin
  940. result:=a_saload;
  941. { if we'd treat arrays of word as "array of widechar" we
  942. could use a_caload, but that would make for even more
  943. awkward interfacing with external Java code }
  944. if not is_signed(def) then
  945. finishandval:=65535;
  946. end;
  947. end;
  948. 4: result:=a_iaload;
  949. 8: result:=a_laload;
  950. else
  951. internalerror(2010120503);
  952. end
  953. end
  954. else
  955. begin
  956. case size of
  957. 1: result:=a_bastore;
  958. 2: if not is_widechar(def) then
  959. result:=a_sastore
  960. else
  961. result:=a_castore;
  962. 4: result:=a_iastore;
  963. 8: result:=a_lastore;
  964. else
  965. internalerror(2010120508);
  966. end
  967. end
  968. end;
  969. R_ADDRESSREGISTER:
  970. if not isarray then
  971. if isload then
  972. result:=a_aload
  973. else
  974. result:=a_astore
  975. else if isload then
  976. result:=a_aaload
  977. else
  978. result:=a_aastore;
  979. R_FPUREGISTER:
  980. begin
  981. case tfloatdef(def).floattype of
  982. s32real:
  983. if not isarray then
  984. if isload then
  985. result:=a_fload
  986. else
  987. result:=a_fstore
  988. else if isload then
  989. result:=a_faload
  990. else
  991. result:=a_fastore;
  992. s64real:
  993. if not isarray then
  994. if isload then
  995. result:=a_dload
  996. else
  997. result:=a_dstore
  998. else if isload then
  999. result:=a_daload
  1000. else
  1001. result:=a_dastore;
  1002. else
  1003. internalerror(2010120504);
  1004. end
  1005. end
  1006. else
  1007. internalerror(2010120502);
  1008. end;
  1009. end;
  1010. procedure thlcgjvm.resize_stack_int_val(list: TAsmList; fromsize, tosize: tcgsize; forarraystore: boolean);
  1011. begin
  1012. if fromsize in [OS_S64,OS_64] then
  1013. begin
  1014. if not(tosize in [OS_S64,OS_64]) then
  1015. begin
  1016. { truncate }
  1017. list.concat(taicpu.op_none(a_l2i));
  1018. decstack(list,1);
  1019. end;
  1020. end
  1021. else if tosize in [OS_S64,OS_64] then
  1022. begin
  1023. { extend }
  1024. list.concat(taicpu.op_none(a_i2l));
  1025. incstack(list,1);
  1026. { if it was an unsigned 32 bit value, remove sign extension }
  1027. if fromsize=OS_32 then
  1028. a_op_const_stack(list,OP_AND,s64inttype,cardinal($ffffffff));
  1029. end;
  1030. { if the value is immediately stored to an array afterwards, the store
  1031. instruction will properly truncate the value; otherwise we may need
  1032. additional truncation, except for 64/32 bit conversions, which are
  1033. already handled above }
  1034. if not forarraystore and
  1035. (not(fromsize in [OS_S64,OS_64,OS_32,OS_S32]) or
  1036. not(tosize in [OS_S64,OS_64,OS_32,OS_S32])) and
  1037. (tcgsize2size[fromsize]>tcgsize2size[tosize]) or
  1038. ((tcgsize2size[fromsize]=tcgsize2size[tosize]) and
  1039. (fromsize<>tosize)) or
  1040. { needs to mask out the sign in the top 16 bits }
  1041. ((fromsize=OS_S8) and
  1042. (tosize=OS_16)) then
  1043. case tosize of
  1044. OS_8:
  1045. a_op_const_stack(list,OP_AND,s32inttype,255);
  1046. OS_S8:
  1047. list.concat(taicpu.op_none(a_i2b));
  1048. OS_16:
  1049. list.concat(taicpu.op_none(a_i2c));
  1050. OS_S16:
  1051. list.concat(taicpu.op_none(a_i2s));
  1052. end;
  1053. end;
  1054. procedure thlcgjvm.resizestackfpuval(list: TAsmList; fromsize, tosize: tcgsize);
  1055. begin
  1056. if (fromsize=OS_F32) and
  1057. (tosize=OS_F64) then
  1058. begin
  1059. list.concat(taicpu.op_none(a_f2d));
  1060. incstack(list,1);
  1061. end
  1062. else if (fromsize=OS_F64) and
  1063. (tosize=OS_F32) then
  1064. begin
  1065. list.concat(taicpu.op_none(a_d2f));
  1066. decstack(list,1);
  1067. end;
  1068. end;
  1069. procedure thlcgjvm.maybepreparedivu32(list: TAsmList; var op: topcg; size: tdef; out isdivu32: boolean);
  1070. begin
  1071. if (op=OP_DIV) and
  1072. (def_cgsize(size)=OS_32) then
  1073. begin
  1074. { needs zero-extension to 64 bit, because the JVM only supports
  1075. signed divisions }
  1076. resize_stack_int_val(list,OS_32,OS_S64,false);
  1077. op:=OP_IDIV;
  1078. isdivu32:=true;
  1079. end
  1080. else
  1081. isdivu32:=false;
  1082. end;
  1083. procedure thlcgjvm.a_call_name_intern(list: TAsmList; pd: tprocdef; const s: string; inheritedcall: boolean);
  1084. var
  1085. opc: tasmop;
  1086. begin
  1087. {
  1088. invoke types:
  1089. * invokeinterface: call method from an interface
  1090. * invokespecial: invoke a constructor, method in a superclass,
  1091. or private instance method
  1092. * invokestatic: invoke a class method (private or not)
  1093. * invokevirtual: invoke a regular method
  1094. }
  1095. case pd.owner.symtabletype of
  1096. globalsymtable,
  1097. staticsymtable,
  1098. localsymtable:
  1099. { regular and nested procedures are turned into static methods }
  1100. opc:=a_invokestatic;
  1101. objectsymtable:
  1102. begin
  1103. case tobjectdef(pd.owner.defowner).objecttype of
  1104. odt_javaclass:
  1105. begin
  1106. if (po_staticmethod in pd.procoptions) then
  1107. opc:=a_invokestatic
  1108. else if (pd.visibility=vis_private) or
  1109. (pd.proctypeoption=potype_constructor) or
  1110. inheritedcall then
  1111. opc:=a_invokespecial
  1112. else
  1113. opc:=a_invokevirtual;
  1114. end;
  1115. odt_interfacejava:
  1116. { static interface methods are not allowed }
  1117. opc:=a_invokeinterface;
  1118. else
  1119. internalerror(2010122601);
  1120. end;
  1121. end;
  1122. else
  1123. internalerror(2010122602);
  1124. end;
  1125. list.concat(taicpu.op_sym(opc,current_asmdata.RefAsmSymbol(s)));
  1126. end;
  1127. procedure create_hlcodegen;
  1128. begin
  1129. hlcg:=thlcgjvm.create;
  1130. create_codegen;
  1131. end;
  1132. end.