hlcgcpu.pas 100 KB

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