hlcgcpu.pas 99 KB

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