2
0

hlcgcpu.pas 89 KB

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