hlcgcpu.pas 70 KB

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