hlcgcpu.pas 69 KB

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