hlcgcpu.pas 72 KB

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