hlcgcpu.pas 67 KB

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