hlcgcpu.pas 72 KB

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