hlcgcpu.pas 75 KB

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