hlcgcpu.pas 71 KB

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