hlcgcpu.pas 85 KB

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