2
0

hlcgcpu.pas 99 KB

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