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_SHL,OP_SHR,OP_SAR:
  486. if not is_64bitint(size) then
  487. a_load_reg_stack(list,size,reg)
  488. else
  489. begin
  490. { the second argument here is an int rather than a long }
  491. if getsubreg(reg)=R_SUBQ then
  492. internalerror(2011010802);
  493. a_load_reg_stack(list,s32inttype,reg)
  494. end
  495. else
  496. a_load_reg_stack(list,size,reg);
  497. end;
  498. a_op_stack(list,op,size,trunc32);
  499. end;
  500. procedure thlcgjvm.a_op_ref_stack(list: TAsmList; op: topcg; size: tdef; const ref: treference);
  501. var
  502. trunc32: boolean;
  503. begin
  504. { ref must not be the stack top, because that may indicate an error
  505. (it means that we will perform an operation of the stack top onto
  506. itself, so that means the two values have been loaded manually prior
  507. to calling this routine, instead of letting this routine load one of
  508. them; if something like that is needed, call a_op_stack() directly) }
  509. if ref.base=NR_EVAL_STACK_BASE then
  510. internalerror(2010121102);
  511. maybepreparedivu32(list,op,size,trunc32);
  512. case op of
  513. OP_SHL,OP_SHR,OP_SAR:
  514. begin
  515. if not is_64bitint(size) then
  516. a_load_ref_stack(list,size,ref,prepare_stack_for_ref(list,ref,false))
  517. else
  518. a_load_ref_stack(list,s32inttype,ref,prepare_stack_for_ref(list,ref,false));
  519. end;
  520. else
  521. a_load_ref_stack(list,size,ref,prepare_stack_for_ref(list,ref,false));
  522. end;
  523. a_op_stack(list,op,size,trunc32);
  524. end;
  525. procedure thlcgjvm.a_op_loc_stack(list: TAsmList; op: topcg; size: tdef; const loc: tlocation);
  526. begin
  527. case loc.loc of
  528. LOC_REGISTER,LOC_CREGISTER:
  529. a_op_reg_stack(list,op,size,loc.register);
  530. LOC_REFERENCE,LOC_CREFERENCE:
  531. a_op_ref_stack(list,op,size,loc.reference);
  532. LOC_CONSTANT:
  533. a_op_const_stack(list,op,size,loc.value);
  534. else
  535. internalerror(2011011415)
  536. end;
  537. end;
  538. procedure thlcgjvm.g_reference_loc(list: TAsmList; def: tdef; const fromloc: tlocation; out toloc: tlocation);
  539. begin
  540. case fromloc.loc of
  541. LOC_CREFERENCE,
  542. LOC_REFERENCE:
  543. begin
  544. toloc:=fromloc;
  545. if (fromloc.reference.base<>NR_NO) and
  546. (fromloc.reference.base<>current_procinfo.framepointer) and
  547. (fromloc.reference.base<>NR_STACK_POINTER_REG) then
  548. g_allocload_reg_reg(list,voidpointertype,fromloc.reference.base,toloc.reference.base,R_ADDRESSREGISTER);
  549. case fromloc.reference.arrayreftype of
  550. art_indexreg:
  551. begin
  552. { all array indices in Java are 32 bit ints }
  553. g_allocload_reg_reg(list,s32inttype,fromloc.reference.index,toloc.reference.index,R_INTREGISTER);
  554. end;
  555. art_indexref:
  556. begin
  557. { base register of the address of the index -> pointer }
  558. if (fromloc.reference.indexbase<>NR_NO) and
  559. (fromloc.reference.indexbase<>NR_STACK_POINTER_REG) then
  560. g_allocload_reg_reg(list,voidpointertype,fromloc.reference.indexbase,toloc.reference.indexbase,R_ADDRESSREGISTER);
  561. end;
  562. end;
  563. end;
  564. else
  565. inherited;
  566. end;
  567. end;
  568. procedure thlcgjvm.g_newarray(list: TAsmList; arrdef: tdef; initdim: longint);
  569. var
  570. recref,
  571. enuminitref: treference;
  572. elemdef: tdef;
  573. i: longint;
  574. mangledname: string;
  575. opc: tasmop;
  576. primitivetype: boolean;
  577. begin
  578. elemdef:=arrdef;
  579. if initdim>1 then
  580. begin
  581. { multianewarray typedesc ndim }
  582. list.concat(taicpu.op_sym_const(a_multianewarray,
  583. current_asmdata.RefAsmSymbol(jvmarrtype(elemdef,primitivetype)),initdim));
  584. { has to be a multi-dimensional array type }
  585. if primitivetype then
  586. internalerror(2011012207);
  587. end
  588. else
  589. begin
  590. { for primitive types:
  591. newarray typedesc
  592. for reference types:
  593. anewarray typedesc
  594. }
  595. { get the type of the elements of the array we are creating }
  596. elemdef:=tarraydef(arrdef).elementdef;
  597. mangledname:=jvmarrtype(elemdef,primitivetype);
  598. if primitivetype then
  599. opc:=a_newarray
  600. else
  601. opc:=a_anewarray;
  602. list.concat(taicpu.op_sym(opc,current_asmdata.RefAsmSymbol(mangledname)));
  603. end;
  604. { all dimensions are removed from the stack, an array reference is
  605. added }
  606. decstack(list,initdim-1);
  607. { in case of an array of records, sets or shortstrings, initialise }
  608. elemdef:=tarraydef(arrdef).elementdef;
  609. for i:=1 to pred(initdim) do
  610. elemdef:=tarraydef(elemdef).elementdef;
  611. if (elemdef.typ in [recorddef,setdef]) or
  612. ((elemdef.typ=enumdef) and
  613. get_enum_init_val_ref(elemdef,enuminitref)) or
  614. is_shortstring(elemdef) or
  615. ((elemdef.typ=procvardef) and
  616. not tprocvardef(elemdef).is_addressonly) or
  617. is_ansistring(elemdef) or
  618. is_wide_or_unicode_string(elemdef) or
  619. is_dynamic_array(elemdef) then
  620. begin
  621. { duplicate array instance }
  622. list.concat(taicpu.op_none(a_dup));
  623. incstack(list,1);
  624. a_load_const_stack(list,s32inttype,initdim-1,R_INTREGISTER);
  625. case elemdef.typ of
  626. arraydef:
  627. g_call_system_proc(list,'fpc_initialize_array_dynarr',nil);
  628. recorddef,setdef,procvardef:
  629. begin
  630. tg.gethltemp(list,elemdef,elemdef.size,tt_persistent,recref);
  631. a_load_ref_stack(list,elemdef,recref,prepare_stack_for_ref(list,recref,false));
  632. case elemdef.typ of
  633. recorddef:
  634. g_call_system_proc(list,'fpc_initialize_array_record',nil);
  635. setdef:
  636. begin
  637. if tsetdef(elemdef).elementdef.typ=enumdef then
  638. g_call_system_proc(list,'fpc_initialize_array_enumset',nil)
  639. else
  640. g_call_system_proc(list,'fpc_initialize_array_bitset',nil)
  641. end;
  642. procvardef:
  643. g_call_system_proc(list,'fpc_initialize_array_procvar',nil);
  644. end;
  645. tg.ungettemp(list,recref);
  646. end;
  647. enumdef:
  648. begin
  649. a_load_ref_stack(list,java_jlobject,enuminitref,prepare_stack_for_ref(list,enuminitref,false));
  650. g_call_system_proc(list,'fpc_initialize_array_object',nil);
  651. end;
  652. stringdef:
  653. begin
  654. case tstringdef(elemdef).stringtype of
  655. st_shortstring:
  656. begin
  657. a_load_const_stack_intern(list,u8inttype,tstringdef(elemdef).len,R_INTREGISTER,true);
  658. g_call_system_proc(list,'fpc_initialize_array_shortstring',nil);
  659. end;
  660. st_ansistring:
  661. g_call_system_proc(list,'fpc_initialize_array_ansistring',nil);
  662. st_unicodestring,
  663. st_widestring:
  664. g_call_system_proc(list,'fpc_initialize_array_unicodestring',nil);
  665. else
  666. internalerror(2011081801);
  667. end;
  668. end;
  669. else
  670. internalerror(2011081801);
  671. end;
  672. end;
  673. end;
  674. procedure thlcgjvm.g_getarraylen(list: TAsmList; const arrloc: tlocation);
  675. var
  676. nillab,endlab: tasmlabel;
  677. begin
  678. { inline because we have to use the arraylength opcode, which
  679. cannot be represented directly in Pascal. Even though the JVM
  680. supports allocated arrays with length=0, we still also have to
  681. check for nil pointers because even if FPC always generates
  682. allocated empty arrays under all circumstances, external Java
  683. code could pass in nil pointers.
  684. Note that this means that assigned(arr) can be different from
  685. length(arr)<>0 for dynamic arrays when targeting the JVM.
  686. }
  687. current_asmdata.getjumplabel(nillab);
  688. current_asmdata.getjumplabel(endlab);
  689. { if assigned(arr) ... }
  690. a_load_loc_stack(list,java_jlobject,arrloc);
  691. list.concat(taicpu.op_none(a_dup));
  692. incstack(list,1);
  693. list.concat(taicpu.op_sym(a_ifnull,nillab));
  694. decstack(list,1);
  695. { ... then result:=arraylength(arr) ... }
  696. list.concat(taicpu.op_none(a_arraylength));
  697. a_jmp_always(list,endlab);
  698. { ... else result:=0 }
  699. a_label(list,nillab);
  700. list.concat(taicpu.op_none(a_pop));
  701. decstack(list,1);
  702. list.concat(taicpu.op_none(a_iconst_0));
  703. incstack(list,1);
  704. a_label(list,endlab);
  705. end;
  706. procedure thlcgjvm.a_cmp_stack_label(list: TAsmlist; size: tdef; cmp_op: topcmp; lab: tasmlabel);
  707. const
  708. opcmp2icmp: array[topcmp] of tasmop = (A_None,
  709. a_if_icmpeq,a_if_icmpgt,a_if_icmplt,a_if_icmpge,a_if_icmple,
  710. a_if_icmpne,a_if_icmple,a_if_icmplt,a_if_icmpge,a_if_icmpgt);
  711. var
  712. cgsize: tcgsize;
  713. begin
  714. case def2regtyp(size) of
  715. R_INTREGISTER:
  716. begin
  717. cgsize:=def_cgsize(size);
  718. case cgsize of
  719. OS_S8,OS_8,
  720. OS_16,OS_S16,
  721. OS_S32,OS_32:
  722. begin
  723. list.concat(taicpu.op_sym(opcmp2icmp[cmp_op],lab));
  724. decstack(list,2);
  725. end;
  726. OS_64,OS_S64:
  727. begin
  728. list.concat(taicpu.op_none(a_lcmp));
  729. decstack(list,3);
  730. list.concat(taicpu.op_sym(opcmp2if[cmp_op],lab));
  731. decstack(list,1);
  732. end;
  733. else
  734. internalerror(2010120538);
  735. end;
  736. end;
  737. R_ADDRESSREGISTER:
  738. begin
  739. case cmp_op of
  740. OC_EQ:
  741. list.concat(taicpu.op_sym(a_if_acmpeq,lab));
  742. OC_NE:
  743. list.concat(taicpu.op_sym(a_if_acmpne,lab));
  744. else
  745. internalerror(2010120537);
  746. end;
  747. decstack(list,2);
  748. end;
  749. else
  750. internalerror(2010120538);
  751. end;
  752. end;
  753. procedure thlcgjvm.maybe_adjust_cmp_stackval(list: TAsmlist; size: tdef; cmp_op: topcmp);
  754. begin
  755. { use cmp_op because eventually that's what indicates the
  756. signed/unsigned character of the operation, not the size... }
  757. if (cmp_op in [OC_EQ,OC_NE,OC_LT,OC_LTE,OC_GT,OC_GTE]) or
  758. (def2regtyp(size)<>R_INTREGISTER) then
  759. exit;
  760. { http://stackoverflow.com/questions/4068973/c-performing-signed-comparison-in-unsigned-variables-without-casting }
  761. case def_cgsize(size) of
  762. OS_32,OS_S32:
  763. a_op_const_stack(list,OP_XOR,size,cardinal($80000000));
  764. OS_64,OS_S64:
  765. a_op_const_stack(list,OP_XOR,size,tcgint($8000000000000000));
  766. end;
  767. end;
  768. function thlcgjvm.maybe_adjust_cmp_constval(size: tdef; cmp_op: topcmp; a: tcgint): tcgint;
  769. begin
  770. result:=a;
  771. { use cmp_op because eventually that's what indicates the
  772. signed/unsigned character of the operation, not the size... }
  773. if (cmp_op in [OC_EQ,OC_NE,OC_LT,OC_LTE,OC_GT,OC_GTE]) or
  774. (def2regtyp(size)<>R_INTREGISTER) then
  775. exit;
  776. case def_cgsize(size) of
  777. OS_32,OS_S32:
  778. result:=a xor cardinal($80000000);
  779. OS_64,OS_S64:
  780. result:=a xor tcgint($8000000000000000);
  781. end;
  782. end;
  783. procedure thlcgjvm.maybe_adjust_op_result(list: TAsmList; op: TOpCg; size: tdef);
  784. const
  785. overflowops = [OP_MUL,OP_SHL,OP_ADD,OP_SUB,OP_NOT,OP_NEG];
  786. begin
  787. if ((op in overflowops) or
  788. (current_settings.cputype=cpu_dalvik)) and
  789. (def_cgsize(size) in [OS_8,OS_S8,OS_16,OS_S16]) then
  790. resize_stack_int_val(list,s32inttype,size,false);
  791. end;
  792. procedure thlcgjvm.gen_load_uninitialized_function_result(list: TAsmList; pd: tprocdef; resdef: tdef; const resloc: tcgpara);
  793. begin
  794. { constructors don't return anything in Java }
  795. if pd.proctypeoption=potype_constructor then
  796. exit;
  797. { must return a value of the correct type on the evaluation stack }
  798. case def2regtyp(resdef) of
  799. R_INTREGISTER,
  800. R_ADDRESSREGISTER:
  801. a_load_const_cgpara(list,resdef,0,resloc);
  802. R_FPUREGISTER:
  803. case tfloatdef(resdef).floattype of
  804. s32real:
  805. begin
  806. list.concat(taicpu.op_none(a_fconst_0));
  807. incstack(list,1);
  808. end;
  809. s64real:
  810. begin
  811. list.concat(taicpu.op_none(a_dconst_0));
  812. incstack(list,2);
  813. end;
  814. else
  815. internalerror(2011010302);
  816. end
  817. else
  818. internalerror(2011010301);
  819. end;
  820. end;
  821. procedure thlcgjvm.g_copyvalueparas(p: TObject; arg: pointer);
  822. var
  823. list: tasmlist;
  824. tmpref: treference;
  825. begin
  826. { zero-extend < 32 bit primitive types (FPC can zero-extend when calling,
  827. but that doesn't help when we're called from Java code or indirectly
  828. as a procvar -- exceptions: widechar (Java-specific type) and ordinal
  829. types whose upper bound does not set the sign bit }
  830. if (tsym(p).typ=paravarsym) and
  831. (tparavarsym(p).varspez in [vs_value,vs_const]) and
  832. (tparavarsym(p).vardef.typ=orddef) and
  833. not is_pasbool(tparavarsym(p).vardef) and
  834. not is_widechar(tparavarsym(p).vardef) and
  835. (tparavarsym(p).vardef.size<4) and
  836. not is_signed(tparavarsym(p).vardef) and
  837. (torddef(tparavarsym(p).vardef).high>=(1 shl (tparavarsym(p).vardef.size*8-1))) then
  838. begin
  839. list:=TAsmList(arg);
  840. { store value in new location to keep Android verifier happy }
  841. tg.gethltemp(list,tparavarsym(p).vardef,tparavarsym(p).vardef.size,tt_persistent,tmpref);
  842. a_load_loc_stack(list,tparavarsym(p).vardef,tparavarsym(p).initialloc);
  843. a_op_const_stack(list,OP_AND,tparavarsym(p).vardef,(1 shl (tparavarsym(p).vardef.size*8))-1);
  844. a_load_stack_ref(list,tparavarsym(p).vardef,tmpref,prepare_stack_for_ref(list,tmpref,false));
  845. location_reset_ref(tparavarsym(p).localloc,LOC_REFERENCE,def_cgsize(tparavarsym(p).vardef),4);
  846. tparavarsym(p).localloc.reference:=tmpref;
  847. end;
  848. inherited g_copyvalueparas(p, arg);
  849. end;
  850. procedure thlcgjvm.inittempvariables(list: TAsmList);
  851. begin
  852. { these are automatically initialised when allocated if necessary }
  853. end;
  854. function thlcgjvm.g_call_system_proc_intern(list: TAsmList; pd: tprocdef; forceresdef: tdef): tcgpara;
  855. begin
  856. result:=inherited;
  857. pd.init_paraloc_info(callerside);
  858. g_adjust_stack_after_call(list,pd,pd.callerargareasize,forceresdef);
  859. end;
  860. function thlcgjvm.prepare_stack_for_ref(list: TAsmList; const ref: treference; dup: boolean): longint;
  861. var
  862. href: treference;
  863. begin
  864. result:=0;
  865. { fake location that indicates the value is already on the stack? }
  866. if (ref.base=NR_EVAL_STACK_BASE) then
  867. exit;
  868. if ref.arrayreftype=art_none then
  869. begin
  870. { non-array accesses cannot have an index reg }
  871. if ref.index<>NR_NO then
  872. internalerror(2010120509);
  873. if (ref.base<>NR_NO) then
  874. begin
  875. if (ref.base<>NR_STACK_POINTER_REG) then
  876. begin
  877. { regular field -> load self on the stack }
  878. a_load_reg_stack(list,voidpointertype,ref.base);
  879. if dup then
  880. begin
  881. list.concat(taicpu.op_none(a_dup));
  882. incstack(list,1);
  883. end;
  884. { field name/type encoded in symbol, no index/offset }
  885. if not assigned(ref.symbol) or
  886. (ref.offset<>0) then
  887. internalerror(2010120524);
  888. result:=1;
  889. end
  890. else
  891. begin
  892. { local variable -> offset encoded in opcode and nothing to
  893. do here, except for checking that it's a valid reference }
  894. if assigned(ref.symbol) then
  895. internalerror(2010120523);
  896. end;
  897. end
  898. else
  899. begin
  900. { static field -> nothing to do here, except for validity check }
  901. if not assigned(ref.symbol) or
  902. (ref.offset<>0) then
  903. internalerror(2010120525);
  904. end;
  905. end
  906. else
  907. begin
  908. { arrays have implicit dereference -> pointer to array must have been
  909. loaded into base reg }
  910. if (ref.base=NR_NO) or
  911. (ref.base=NR_STACK_POINTER_REG) then
  912. internalerror(2010120511);
  913. if assigned(ref.symbol) then
  914. internalerror(2010120512);
  915. { stack: ... -> ..., arrayref, index }
  916. { load array base address }
  917. a_load_reg_stack(list,voidpointertype,ref.base);
  918. { index can either be in a register, or located in a simple memory
  919. location (since we have to load it anyway) }
  920. case ref.arrayreftype of
  921. art_indexreg:
  922. begin
  923. if ref.index=NR_NO then
  924. internalerror(2010120513);
  925. { all array indices in Java are 32 bit ints }
  926. a_load_reg_stack(list,s32inttype,ref.index);
  927. end;
  928. art_indexref:
  929. begin
  930. reference_reset_base(href,ref.indexbase,ref.indexoffset,4);
  931. href.symbol:=ref.indexsymbol;
  932. a_load_ref_stack(list,s32inttype,href,prepare_stack_for_ref(list,href,false));
  933. end;
  934. art_indexconst:
  935. begin
  936. a_load_const_stack(list,s32inttype,ref.indexoffset,R_INTREGISTER);
  937. end;
  938. else
  939. internalerror(2011012001);
  940. end;
  941. { adjustment of the index }
  942. if ref.offset<>0 then
  943. a_op_const_stack(list,OP_ADD,s32inttype,ref.offset);
  944. if dup then
  945. begin
  946. list.concat(taicpu.op_none(a_dup2));
  947. incstack(list,2);
  948. end;
  949. result:=2;
  950. end;
  951. end;
  952. procedure thlcgjvm.a_load_const_reg(list: TAsmList; tosize: tdef; a: tcgint; register: tregister);
  953. begin
  954. a_load_const_stack(list,tosize,a,def2regtyp(tosize));
  955. a_load_stack_reg(list,tosize,register);
  956. end;
  957. procedure thlcgjvm.a_load_const_ref(list: TAsmList; tosize: tdef; a: tcgint; const ref: treference);
  958. var
  959. extra_slots: longint;
  960. begin
  961. extra_slots:=prepare_stack_for_ref(list,ref,false);
  962. a_load_const_stack_intern(list,tosize,a,def2regtyp(tosize),(ref.arrayreftype<>art_none) or assigned(ref.symbol));
  963. a_load_stack_ref(list,tosize,ref,extra_slots);
  964. end;
  965. procedure thlcgjvm.a_load_reg_ref(list: TAsmList; fromsize, tosize: tdef; register: tregister; const ref: treference);
  966. var
  967. extra_slots: longint;
  968. begin
  969. extra_slots:=prepare_stack_for_ref(list,ref,false);
  970. a_load_reg_stack(list,fromsize,register);
  971. if def2regtyp(fromsize)=R_INTREGISTER then
  972. resize_stack_int_val(list,fromsize,tosize,(ref.arrayreftype<>art_none) or assigned(ref.symbol));
  973. a_load_stack_ref(list,tosize,ref,extra_slots);
  974. end;
  975. procedure thlcgjvm.a_load_reg_reg(list: TAsmList; fromsize, tosize: tdef; reg1, reg2: tregister);
  976. begin
  977. a_load_reg_stack(list,fromsize,reg1);
  978. if def2regtyp(fromsize)=R_INTREGISTER then
  979. resize_stack_int_val(list,fromsize,tosize,false);
  980. a_load_stack_reg(list,tosize,reg2);
  981. end;
  982. procedure thlcgjvm.a_load_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; register: tregister);
  983. var
  984. extra_slots: longint;
  985. begin
  986. extra_slots:=prepare_stack_for_ref(list,ref,false);
  987. a_load_ref_stack(list,fromsize,ref,extra_slots);
  988. if def2regtyp(fromsize)=R_INTREGISTER then
  989. resize_stack_int_val(list,fromsize,tosize,false);
  990. a_load_stack_reg(list,tosize,register);
  991. end;
  992. procedure thlcgjvm.a_load_ref_ref(list: TAsmList; fromsize, tosize: tdef; const sref: treference; const dref: treference);
  993. var
  994. extra_sslots,
  995. extra_dslots: longint;
  996. begin
  997. { make sure the destination reference is on top, since in the end the
  998. order has to be "destref, value" -> first create "destref, sourceref" }
  999. extra_dslots:=prepare_stack_for_ref(list,dref,false);
  1000. extra_sslots:=prepare_stack_for_ref(list,sref,false);
  1001. a_load_ref_stack(list,fromsize,sref,extra_sslots);
  1002. if def2regtyp(fromsize)=R_INTREGISTER then
  1003. resize_stack_int_val(list,fromsize,tosize,(dref.arrayreftype<>art_none) or assigned(dref.symbol));
  1004. a_load_stack_ref(list,tosize,dref,extra_dslots);
  1005. end;
  1006. procedure thlcgjvm.a_loadaddr_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; r: tregister);
  1007. begin
  1008. { only allowed for types that are not implicit pointers in Pascal (in
  1009. that case, ref contains a pointer to the actual data and we simply
  1010. return that pointer) }
  1011. if not jvmimplicitpointertype(fromsize) then
  1012. internalerror(2010120534);
  1013. a_load_ref_reg(list,java_jlobject,java_jlobject,ref,r);
  1014. end;
  1015. procedure thlcgjvm.a_op_const_reg(list: TAsmList; Op: TOpCG; size: tdef; a: tcgint; reg: TRegister);
  1016. begin
  1017. a_op_const_reg_reg(list,op,size,a,reg,reg);
  1018. end;
  1019. procedure thlcgjvm.a_op_const_reg_reg(list: TAsmList; op: TOpCg; size: tdef; a: tcgint; src, dst: tregister);
  1020. begin
  1021. a_load_reg_stack(list,size,src);
  1022. a_op_const_stack(list,op,size,a);
  1023. a_load_stack_reg(list,size,dst);
  1024. end;
  1025. procedure thlcgjvm.a_op_const_ref(list: TAsmList; Op: TOpCG; size: tdef; a: tcgint; const ref: TReference);
  1026. var
  1027. extra_slots: longint;
  1028. begin
  1029. extra_slots:=prepare_stack_for_ref(list,ref,true);
  1030. { TODO, here or in peepholeopt: use iinc when possible }
  1031. a_load_ref_stack(list,size,ref,extra_slots);
  1032. a_op_const_stack(list,op,size,a);
  1033. { for android verifier }
  1034. if (def2regtyp(size)=R_INTREGISTER) and
  1035. ((ref.arrayreftype<>art_none) or
  1036. assigned(ref.symbol)) then
  1037. resize_stack_int_val(list,size,size,true);
  1038. a_load_stack_ref(list,size,ref,extra_slots);
  1039. end;
  1040. procedure thlcgjvm.a_op_ref_reg(list: TAsmList; Op: TOpCG; size: tdef; const ref: TReference; reg: TRegister);
  1041. begin
  1042. if not(op in [OP_NOT,OP_NEG]) then
  1043. a_load_reg_stack(list,size,reg);
  1044. a_op_ref_stack(list,op,size,ref);
  1045. a_load_stack_reg(list,size,reg);
  1046. end;
  1047. procedure thlcgjvm.a_op_reg_reg_reg(list: TAsmList; op: TOpCg; size: tdef; src1, src2, dst: tregister);
  1048. begin
  1049. if not(op in [OP_NOT,OP_NEG]) then
  1050. a_load_reg_stack(list,size,src2);
  1051. a_op_reg_stack(list,op,size,src1);
  1052. a_load_stack_reg(list,size,dst);
  1053. end;
  1054. procedure thlcgjvm.a_op_reg_reg(list: TAsmList; Op: TOpCG; size: tdef; reg1, reg2: TRegister);
  1055. begin
  1056. a_op_reg_reg_reg(list,op,size,reg1,reg2,reg2);
  1057. end;
  1058. procedure thlcgjvm.a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tdef; a: tcgint; src, dst: tregister; setflags: boolean; var ovloc: tlocation);
  1059. var
  1060. tmpreg: tregister;
  1061. begin
  1062. if not setflags then
  1063. begin
  1064. inherited;
  1065. exit;
  1066. end;
  1067. tmpreg:=getintregister(list,size);
  1068. a_load_const_reg(list,size,a,tmpreg);
  1069. a_op_reg_reg_reg_checkoverflow(list,op,size,tmpreg,src,dst,true,ovloc);
  1070. end;
  1071. procedure thlcgjvm.a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tdef; src1, src2, dst: tregister; setflags: boolean; var ovloc: tlocation);
  1072. var
  1073. orgsrc1, orgsrc2: tregister;
  1074. docheck: boolean;
  1075. lab: tasmlabel;
  1076. begin
  1077. if not setflags then
  1078. begin
  1079. inherited;
  1080. exit;
  1081. end;
  1082. { anything else cannot overflow }
  1083. docheck:=size.size in [4,8];
  1084. if docheck then
  1085. begin
  1086. orgsrc1:=src1;
  1087. orgsrc2:=src2;
  1088. if src1=dst then
  1089. begin
  1090. orgsrc1:=getintregister(list,size);
  1091. a_load_reg_reg(list,size,size,src1,orgsrc1);
  1092. end;
  1093. if src2=dst then
  1094. begin
  1095. orgsrc2:=getintregister(list,size);
  1096. a_load_reg_reg(list,size,size,src2,orgsrc2);
  1097. end;
  1098. end;
  1099. a_op_reg_reg_reg(list,op,size,src1,src2,dst);
  1100. if docheck then
  1101. begin
  1102. { * signed overflow for addition iff
  1103. - src1 and src2 are negative and result is positive (excep in case of
  1104. subtraction, then sign of src1 has to be inverted)
  1105. - src1 and src2 are positive and result is negative
  1106. -> Simplified boolean equivalent (in terms of sign bits):
  1107. not(src1 xor src2) and (src1 xor dst)
  1108. for subtraction, multiplication: invert src1 sign bit
  1109. for division: handle separately (div by zero, low(inttype) div -1),
  1110. not supported by this code
  1111. * unsigned overflow iff carry out, aka dst < src1 or dst < src2
  1112. }
  1113. location_reset(ovloc,LOC_REGISTER,OS_S32);
  1114. { not pasbool8, because then we'd still have to convert the integer to
  1115. a boolean via branches for Dalvik}
  1116. ovloc.register:=getintregister(list,s32inttype);
  1117. if not ((size.typ=pointerdef) or
  1118. ((size.typ=orddef) and
  1119. (torddef(size).ordtype in [u64bit,u16bit,u32bit,u8bit,uchar,
  1120. pasbool8,pasbool16,pasbool32,pasbool64]))) then
  1121. begin
  1122. a_load_reg_stack(list,size,src1);
  1123. if op in [OP_SUB,OP_IMUL] then
  1124. a_op_stack(list,OP_NOT,size,false);
  1125. a_op_reg_stack(list,OP_XOR,size,src2);
  1126. a_op_stack(list,OP_NOT,size,false);
  1127. a_load_reg_stack(list,size,src1);
  1128. a_op_reg_stack(list,OP_XOR,size,dst);
  1129. a_op_stack(list,OP_AND,size,false);
  1130. a_op_const_stack(list,OP_SHR,size,(size.size*8)-1);
  1131. if size.size=8 then
  1132. begin
  1133. list.concat(taicpu.op_none(a_l2i));
  1134. decstack(list,1);
  1135. end;
  1136. end
  1137. else
  1138. begin
  1139. a_load_const_stack(list,s32inttype,0,R_INTREGISTER);
  1140. current_asmdata.getjumplabel(lab);
  1141. { can be optimized by removing duplicate xor'ing to convert dst from
  1142. signed to unsigned quadrant }
  1143. a_cmp_reg_reg_label(list,size,OC_B,dst,src1,lab);
  1144. a_cmp_reg_reg_label(list,size,OC_B,dst,src2,lab);
  1145. a_op_const_stack(list,OP_XOR,s32inttype,1);
  1146. a_label(list,lab);
  1147. end;
  1148. a_load_stack_reg(list,s32inttype,ovloc.register);
  1149. end
  1150. else
  1151. ovloc.loc:=LOC_VOID;
  1152. end;
  1153. procedure thlcgjvm.a_cmp_const_ref_label(list: TAsmList; size: tdef; cmp_op: topcmp; a: tcgint; const ref: treference; l: tasmlabel);
  1154. begin
  1155. if ref.base<>NR_EVAL_STACK_BASE then
  1156. a_load_ref_stack(list,size,ref,prepare_stack_for_ref(list,ref,false));
  1157. maybe_adjust_cmp_stackval(list,size,cmp_op);
  1158. a_load_const_stack(list,size,maybe_adjust_cmp_constval(size,cmp_op,a),def2regtyp(size));
  1159. a_cmp_stack_label(list,size,cmp_op,l);
  1160. end;
  1161. procedure thlcgjvm.a_cmp_const_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; a: tcgint; reg: tregister; l: tasmlabel);
  1162. begin
  1163. a_load_reg_stack(list,size,reg);
  1164. maybe_adjust_cmp_stackval(list,size,cmp_op);
  1165. a_load_const_stack(list,size,maybe_adjust_cmp_constval(size,cmp_op,a),def2regtyp(size));
  1166. a_cmp_stack_label(list,size,cmp_op,l);
  1167. end;
  1168. procedure thlcgjvm.a_cmp_ref_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; const ref: treference; reg: tregister; l: tasmlabel);
  1169. begin
  1170. a_load_reg_stack(list,size,reg);
  1171. maybe_adjust_cmp_stackval(list,size,cmp_op);
  1172. if ref.base<>NR_EVAL_STACK_BASE then
  1173. a_load_ref_stack(list,size,ref,prepare_stack_for_ref(list,ref,false))
  1174. else
  1175. list.concat(taicpu.op_none(a_swap));
  1176. maybe_adjust_cmp_stackval(list,size,cmp_op);
  1177. a_cmp_stack_label(list,size,cmp_op,l);
  1178. end;
  1179. procedure thlcgjvm.a_cmp_reg_ref_label(list: TAsmList; size: tdef; cmp_op: topcmp; reg: tregister; const ref: treference; l: tasmlabel);
  1180. begin
  1181. if ref.base<>NR_EVAL_STACK_BASE then
  1182. a_load_ref_stack(list,size,ref,prepare_stack_for_ref(list,ref,false));
  1183. maybe_adjust_cmp_stackval(list,size,cmp_op);
  1184. a_load_reg_stack(list,size,reg);
  1185. maybe_adjust_cmp_stackval(list,size,cmp_op);
  1186. a_cmp_stack_label(list,size,cmp_op,l);
  1187. end;
  1188. procedure thlcgjvm.a_cmp_reg_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; reg1, reg2: tregister; l: tasmlabel);
  1189. begin
  1190. a_load_reg_stack(list,size,reg2);
  1191. maybe_adjust_cmp_stackval(list,size,cmp_op);
  1192. a_load_reg_stack(list,size,reg1);
  1193. maybe_adjust_cmp_stackval(list,size,cmp_op);
  1194. a_cmp_stack_label(list,size,cmp_op,l);
  1195. end;
  1196. procedure thlcgjvm.a_jmp_always(list: TAsmList; l: tasmlabel);
  1197. begin
  1198. list.concat(taicpu.op_sym(a_goto,current_asmdata.RefAsmSymbol(l.name)));
  1199. end;
  1200. procedure thlcgjvm.concatcopy_normal_array(list: TAsmList; size: tdef; const source, dest: treference);
  1201. var
  1202. procname: string;
  1203. eledef: tdef;
  1204. ndim: longint;
  1205. adddefaultlenparas: boolean;
  1206. begin
  1207. { load copy helper parameters on the stack }
  1208. a_load_ref_stack(list,java_jlobject,source,prepare_stack_for_ref(list,source,false));
  1209. a_load_ref_stack(list,java_jlobject,dest,prepare_stack_for_ref(list,dest,false));
  1210. { call copy helper }
  1211. eledef:=tarraydef(size).elementdef;
  1212. ndim:=1;
  1213. adddefaultlenparas:=true;
  1214. case eledef.typ of
  1215. orddef:
  1216. begin
  1217. case torddef(eledef).ordtype of
  1218. pasbool8,s8bit,u8bit,bool8bit,uchar,
  1219. s16bit,u16bit,bool16bit,pasbool16,
  1220. uwidechar,
  1221. s32bit,u32bit,bool32bit,pasbool32,
  1222. s64bit,u64bit,bool64bit,pasbool64,scurrency:
  1223. procname:='FPC_COPY_SHALLOW_ARRAY'
  1224. else
  1225. internalerror(2011020504);
  1226. end;
  1227. end;
  1228. arraydef:
  1229. begin
  1230. { call fpc_setlength_dynarr_multidim with deepcopy=true, and extra
  1231. parameters }
  1232. while (eledef.typ=arraydef) and
  1233. not is_dynamic_array(eledef) do
  1234. begin
  1235. eledef:=tarraydef(eledef).elementdef;
  1236. inc(ndim)
  1237. end;
  1238. if (ndim=1) then
  1239. procname:='FPC_COPY_SHALLOW_ARRAY'
  1240. else
  1241. begin
  1242. { deepcopy=true }
  1243. a_load_const_stack(list,pasbool8type,1,R_INTREGISTER);
  1244. { ndim }
  1245. a_load_const_stack(list,s32inttype,ndim,R_INTREGISTER);
  1246. { eletype }
  1247. a_load_const_stack(list,cwidechartype,ord(jvmarrtype_setlength(eledef)),R_INTREGISTER);
  1248. adddefaultlenparas:=false;
  1249. procname:='FPC_SETLENGTH_DYNARR_MULTIDIM';
  1250. end;
  1251. end;
  1252. recorddef:
  1253. procname:='FPC_COPY_JRECORD_ARRAY';
  1254. procvardef:
  1255. if tprocvardef(eledef).is_addressonly then
  1256. procname:='FPC_COPY_SHALLOW_ARRAY'
  1257. else
  1258. procname:='FPC_COPY_JPROCVAR_ARRAY';
  1259. setdef:
  1260. if tsetdef(eledef).elementdef.typ=enumdef then
  1261. procname:='FPC_COPY_JENUMSET_ARRAY'
  1262. else
  1263. procname:='FPC_COPY_JBITSET_ARRAY';
  1264. floatdef:
  1265. procname:='FPC_COPY_SHALLOW_ARRAY';
  1266. stringdef:
  1267. if is_shortstring(eledef) then
  1268. procname:='FPC_COPY_JSHORTSTRING_ARRAY'
  1269. else
  1270. procname:='FPC_COPY_SHALLOW_ARRAY';
  1271. variantdef:
  1272. begin
  1273. {$ifndef nounsupported}
  1274. procname:='FPC_COPY_SHALLOW_ARRAY';
  1275. {$else}
  1276. { todo: make a deep copy via clone... }
  1277. internalerror(2011020505);
  1278. {$endif}
  1279. end;
  1280. else
  1281. procname:='FPC_COPY_SHALLOW_ARRAY';
  1282. end;
  1283. if adddefaultlenparas then
  1284. begin
  1285. { -1, -1 means "copy entire array" }
  1286. a_load_const_stack(list,s32inttype,-1,R_INTREGISTER);
  1287. a_load_const_stack(list,s32inttype,-1,R_INTREGISTER);
  1288. end;
  1289. g_call_system_proc(list,procname,nil);
  1290. if ndim<>1 then
  1291. begin
  1292. { pop return value, must be the same as dest }
  1293. list.concat(taicpu.op_none(a_pop));
  1294. decstack(list,1);
  1295. end;
  1296. end;
  1297. procedure thlcgjvm.concatcopy_record(list: TAsmList; size: tdef; const source, dest: treference);
  1298. var
  1299. srsym: tsym;
  1300. pd: tprocdef;
  1301. begin
  1302. { self }
  1303. a_load_ref_stack(list,size,source,prepare_stack_for_ref(list,source,false));
  1304. { result }
  1305. a_load_ref_stack(list,size,dest,prepare_stack_for_ref(list,dest,false));
  1306. { call fpcDeepCopy helper }
  1307. srsym:=search_struct_member(tabstractrecorddef(size),'FPCDEEPCOPY');
  1308. if not assigned(srsym) or
  1309. (srsym.typ<>procsym) then
  1310. Message1(cg_f_unknown_compilerproc,size.typename+'.fpcDeepCopy');
  1311. pd:=tprocdef(tprocsym(srsym).procdeflist[0]);
  1312. a_call_name(list,pd,pd.mangledname,nil,false);
  1313. { both parameters are removed, no function result }
  1314. decstack(list,2);
  1315. end;
  1316. procedure thlcgjvm.concatcopy_set(list: TAsmList; size: tdef; const source, dest: treference);
  1317. begin
  1318. a_load_ref_stack(list,size,source,prepare_stack_for_ref(list,source,false));
  1319. a_load_ref_stack(list,size,dest,prepare_stack_for_ref(list,dest,false));
  1320. { call set copy helper }
  1321. if tsetdef(size).elementdef.typ=enumdef then
  1322. g_call_system_proc(list,'fpc_enumset_copy',nil)
  1323. else
  1324. g_call_system_proc(list,'fpc_bitset_copy',nil);
  1325. end;
  1326. procedure thlcgjvm.concatcopy_shortstring(list: TAsmList; size: tdef; const source, dest: treference);
  1327. var
  1328. srsym: tsym;
  1329. pd: tprocdef;
  1330. begin
  1331. { self }
  1332. a_load_ref_stack(list,size,source,prepare_stack_for_ref(list,source,false));
  1333. { result }
  1334. a_load_ref_stack(list,size,dest,prepare_stack_for_ref(list,dest,false));
  1335. { call fpcDeepCopy helper }
  1336. srsym:=search_struct_member(java_shortstring,'FPCDEEPCOPY');
  1337. if not assigned(srsym) or
  1338. (srsym.typ<>procsym) then
  1339. Message1(cg_f_unknown_compilerproc,'ShortstringClass.FpcDeepCopy');
  1340. pd:=tprocdef(tprocsym(srsym).procdeflist[0]);
  1341. a_call_name(list,pd,pd.mangledname,nil,false);
  1342. { both parameters are removed, no function result }
  1343. decstack(list,2);
  1344. end;
  1345. procedure thlcgjvm.g_concatcopy(list: TAsmList; size: tdef; const source, dest: treference);
  1346. var
  1347. handled: boolean;
  1348. begin
  1349. handled:=false;
  1350. case size.typ of
  1351. arraydef:
  1352. begin
  1353. if not is_dynamic_array(size) then
  1354. begin
  1355. concatcopy_normal_array(list,size,source,dest);
  1356. handled:=true;
  1357. end;
  1358. end;
  1359. recorddef:
  1360. begin
  1361. concatcopy_record(list,size,source,dest);
  1362. handled:=true;
  1363. end;
  1364. setdef:
  1365. begin
  1366. concatcopy_set(list,size,source,dest);
  1367. handled:=true;
  1368. end;
  1369. stringdef:
  1370. begin
  1371. if is_shortstring(size) then
  1372. begin
  1373. concatcopy_shortstring(list,size,source,dest);
  1374. handled:=true;
  1375. end;
  1376. end;
  1377. procvardef:
  1378. begin
  1379. if not tprocvardef(size).is_addressonly then
  1380. begin
  1381. concatcopy_record(list,tprocvardef(size).classdef,source,dest);
  1382. handled:=true;
  1383. end;
  1384. end;
  1385. end;
  1386. if not handled then
  1387. inherited;
  1388. end;
  1389. procedure thlcgjvm.g_copyshortstring(list: TAsmList; const source, dest: treference; strdef: tstringdef);
  1390. begin
  1391. concatcopy_shortstring(list,strdef,source,dest);
  1392. end;
  1393. procedure thlcgjvm.a_loadfpu_ref_ref(list: TAsmList; fromsize, tosize: tdef; const ref1, ref2: treference);
  1394. var
  1395. dstack_slots: longint;
  1396. begin
  1397. dstack_slots:=prepare_stack_for_ref(list,ref2,false);
  1398. a_load_ref_stack(list,fromsize,ref1,prepare_stack_for_ref(list,ref1,false));
  1399. resizestackfpuval(list,def_cgsize(fromsize),def_cgsize(tosize));
  1400. a_load_stack_ref(list,tosize,ref2,dstack_slots);
  1401. end;
  1402. procedure thlcgjvm.a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; reg: tregister);
  1403. begin
  1404. a_load_ref_stack(list,fromsize,ref,prepare_stack_for_ref(list,ref,false));
  1405. resizestackfpuval(list,def_cgsize(fromsize),def_cgsize(tosize));
  1406. a_load_stack_reg(list,tosize,reg);
  1407. end;
  1408. procedure thlcgjvm.a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tdef; reg: tregister; const ref: treference);
  1409. var
  1410. dstack_slots: longint;
  1411. begin
  1412. dstack_slots:=prepare_stack_for_ref(list,ref,false);
  1413. a_load_reg_stack(list,fromsize,reg);
  1414. resizestackfpuval(list,def_cgsize(fromsize),def_cgsize(tosize));
  1415. a_load_stack_ref(list,tosize,ref,dstack_slots);
  1416. end;
  1417. procedure thlcgjvm.a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tdef; reg1, reg2: tregister);
  1418. begin
  1419. a_load_reg_stack(list,fromsize,reg1);
  1420. resizestackfpuval(list,def_cgsize(fromsize),def_cgsize(tosize));
  1421. a_load_stack_reg(list,tosize,reg2);
  1422. end;
  1423. procedure thlcgjvm.g_proc_entry(list: TAsmList; localsize: longint; nostackframe: boolean);
  1424. begin
  1425. { the localsize is based on tg.lasttemp -> already in terms of stack
  1426. slots rather than bytes }
  1427. list.concat(tai_directive.Create(asd_jlimit,'locals '+tostr(localsize)));
  1428. { we insert the unit initialisation code afterwards in the proginit code,
  1429. and it uses one stack slot }
  1430. if (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1431. fmaxevalstackheight:=max(1,fmaxevalstackheight);
  1432. list.concat(tai_directive.Create(asd_jlimit,'stack '+tostr(fmaxevalstackheight)));
  1433. end;
  1434. procedure thlcgjvm.g_proc_exit(list: TAsmList; parasize: longint; nostackframe: boolean);
  1435. var
  1436. retdef: tdef;
  1437. opc: tasmop;
  1438. begin
  1439. if current_procinfo.procdef.proctypeoption in [potype_constructor,potype_class_constructor] then
  1440. retdef:=voidtype
  1441. else
  1442. retdef:=current_procinfo.procdef.returndef;
  1443. case retdef.typ of
  1444. orddef:
  1445. case torddef(retdef).ordtype of
  1446. uvoid:
  1447. opc:=a_return;
  1448. s64bit,
  1449. u64bit,
  1450. scurrency:
  1451. opc:=a_lreturn;
  1452. else
  1453. opc:=a_ireturn;
  1454. end;
  1455. setdef:
  1456. opc:=a_areturn;
  1457. floatdef:
  1458. case tfloatdef(retdef).floattype of
  1459. s32real:
  1460. opc:=a_freturn;
  1461. s64real:
  1462. opc:=a_dreturn;
  1463. else
  1464. internalerror(2011010213);
  1465. end;
  1466. else
  1467. opc:=a_areturn;
  1468. end;
  1469. list.concat(taicpu.op_none(opc));
  1470. end;
  1471. procedure thlcgjvm.gen_load_return_value(list: TAsmList);
  1472. begin
  1473. { constructors don't return anything in the jvm }
  1474. if current_procinfo.procdef.proctypeoption in [potype_constructor,potype_class_constructor] then
  1475. exit;
  1476. inherited gen_load_return_value(list);
  1477. end;
  1478. procedure thlcgjvm.record_generated_code_for_procdef(pd: tprocdef; code, data: TAsmList);
  1479. begin
  1480. { add something to the al_procedures list as well, because if all al_*
  1481. lists are empty, the assembler writer isn't called }
  1482. if not code.empty and
  1483. current_asmdata.asmlists[al_procedures].empty then
  1484. current_asmdata.asmlists[al_procedures].concat(tai_align.Create(4));
  1485. pd.exprasmlist:=TAsmList.create;
  1486. pd.exprasmlist.concatlist(code);
  1487. if assigned(data) and
  1488. not data.empty then
  1489. internalerror(2010122801);
  1490. end;
  1491. procedure thlcgjvm.g_incrrefcount(list: TAsmList; t: tdef; const ref: treference);
  1492. begin
  1493. // do nothing
  1494. end;
  1495. procedure thlcgjvm.g_array_rtti_helper(list: TAsmList; t: tdef; const ref: treference; const highloc: tlocation; const name: string);
  1496. var
  1497. normaldim: longint;
  1498. eleref: treference;
  1499. begin
  1500. { only in case of initialisation, we have to set all elements to "empty" }
  1501. if name<>'fpc_initialize_array' then
  1502. exit;
  1503. { put array on the stack }
  1504. a_load_ref_stack(list,java_jlobject,ref,prepare_stack_for_ref(list,ref,false));
  1505. { in case it's an open array whose elements are regular arrays, put the
  1506. dimension of the regular arrays on the stack (otherwise pass 0) }
  1507. normaldim:=0;
  1508. while (t.typ=arraydef) and
  1509. not is_dynamic_array(t) do
  1510. begin
  1511. inc(normaldim);
  1512. t:=tarraydef(t).elementdef;
  1513. end;
  1514. a_load_const_stack(list,s32inttype,normaldim,R_INTREGISTER);
  1515. { highloc is invalid, the length is part of the array in Java }
  1516. if is_wide_or_unicode_string(t) then
  1517. g_call_system_proc(list,'fpc_initialize_array_unicodestring',nil)
  1518. else if is_ansistring(t) then
  1519. g_call_system_proc(list,'fpc_initialize_array_ansistring',nil)
  1520. else if is_dynamic_array(t) then
  1521. g_call_system_proc(list,'fpc_initialize_array_dynarr',nil)
  1522. else if is_record(t) or
  1523. (t.typ=setdef) then
  1524. begin
  1525. tg.gethltemp(list,t,t.size,tt_persistent,eleref);
  1526. a_load_ref_stack(list,t,eleref,prepare_stack_for_ref(list,eleref,false));
  1527. if is_record(t) then
  1528. g_call_system_proc(list,'fpc_initialize_array_record',nil)
  1529. else if tsetdef(t).elementdef.typ=enumdef then
  1530. g_call_system_proc(list,'fpc_initialize_array_enumset',nil)
  1531. else
  1532. g_call_system_proc(list,'fpc_initialize_array_bitset',nil);
  1533. tg.ungettemp(list,eleref);
  1534. end
  1535. else if (t.typ=enumdef) then
  1536. begin
  1537. if get_enum_init_val_ref(t,eleref) then
  1538. begin
  1539. a_load_ref_stack(list,java_jlobject,eleref,prepare_stack_for_ref(list,eleref,false));
  1540. g_call_system_proc(list,'fpc_initialize_array_object',nil);
  1541. end;
  1542. end
  1543. else
  1544. internalerror(2011031901);
  1545. end;
  1546. procedure thlcgjvm.g_initialize(list: TAsmList; t: tdef; const ref: treference);
  1547. var
  1548. dummyloc: tlocation;
  1549. sym: tsym;
  1550. pd: tprocdef;
  1551. begin
  1552. if (t.typ=arraydef) and
  1553. not is_dynamic_array(t) then
  1554. begin
  1555. dummyloc.loc:=LOC_INVALID;
  1556. g_array_rtti_helper(list,tarraydef(t).elementdef,ref,dummyloc,'fpc_initialize_array')
  1557. end
  1558. else if is_record(t) then
  1559. begin
  1560. { call the fpcInitializeRec method }
  1561. sym:=tsym(trecorddef(t).symtable.find('FPCINITIALIZEREC'));
  1562. if assigned(sym) and
  1563. (sym.typ=procsym) then
  1564. begin
  1565. if tprocsym(sym).procdeflist.Count<>1 then
  1566. internalerror(2011071713);
  1567. pd:=tprocdef(tprocsym(sym).procdeflist[0]);
  1568. end
  1569. else
  1570. internalerror(2013113008);
  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.