hlcgcpu.pas 52 KB

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