hlcgcpu.pas 78 KB

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