hlcgcpu.pas 80 KB

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