hlcgcpu.pas 71 KB

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