hlcgcpu.pas 44 KB

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