hlcgcpu.pas 70 KB

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