hlcgcpu.pas 47 KB

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