hlcgcpu.pas 91 KB

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