hlcgcpu.pas 89 KB

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