hlcgcpu.pas 91 KB

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