hlcgcpu.pas 64 KB

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