hlcgcpu.pas 89 KB

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