hlcgcpu.pas 64 KB

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