2
0

hlcgcpu.pas 100 KB

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