hlcgcpu.pas 97 KB

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