hlcgcpu.pas 92 KB

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