hlcgcpu.pas 66 KB

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