hlcgcpu.pas 100 KB

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