hlcgcpu.pas 71 KB

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