hlcgcpu.pas 83 KB

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