hlcgcpu.pas 62 KB

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