hlcgcpu.pas 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191
  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. function get_enum_init_val_ref(def: tdef; out ref: treference): boolean;
  121. procedure allocate_implicit_structs_for_st_with_base_ref(list: TAsmList; st: tsymtable; const ref: treference; allocvartyp: tsymtyp);
  122. procedure allocate_enum_with_base_ref(list: TAsmList; vs: tabstractvarsym; const initref: treference; destbaseref: treference);
  123. procedure allocate_implicit_struct_with_base_ref(list: TAsmList; vs: tabstractvarsym; ref: treference);
  124. procedure gen_load_uninitialized_function_result(list: TAsmList; pd: tprocdef; resdef: tdef; const resloc: tcgpara); override;
  125. procedure g_copyvalueparas(p: TObject; arg: pointer); override;
  126. procedure inittempvariables(list:TAsmList);override;
  127. { in case of an array, the array base address and index have to be
  128. put on the evaluation stack before the stored value; similarly, for
  129. fields the self pointer has to be loaded first. Also checks whether
  130. the reference is valid. If dup is true, the necessary values are stored
  131. twice. Returns how many stack slots have been consumed, disregarding
  132. the "dup". }
  133. function prepare_stack_for_ref(list: TAsmList; const ref: treference; dup: boolean): longint;
  134. { return the load/store opcode to load/store from/to ref; if the result
  135. has to be and'ed after a load to get the final value, that constant
  136. is returned in finishandval (otherwise that value is set to -1) }
  137. function loadstoreopcref(def: tdef; isload: boolean; const ref: treference; out finishandval: aint): tasmop;
  138. { return the load/store opcode to load/store from/to reg; if the result
  139. has to be and'ed after a load to get the final value, that constant
  140. is returned in finishandval (otherwise that value is set to -1) }
  141. function loadstoreopc(def: tdef; isload, isarray: boolean; out finishandval: aint): tasmop;
  142. procedure resizestackfpuval(list: TAsmList; fromsize, tosize: tcgsize);
  143. { in case of an OS_32 OP_DIV, we have to use an OS_S64 OP_IDIV because the
  144. JVM does not support unsigned divisions }
  145. procedure maybepreparedivu32(list: TAsmList; var op: topcg; size: tdef; out isdivu32: boolean);
  146. { common implementation of a_call_* }
  147. procedure a_call_name_intern(list : TAsmList;pd : tprocdef;const s : TSymStr; inheritedcall: boolean);
  148. { concatcopy helpers }
  149. procedure concatcopy_normal_array(list: TAsmList; size: tdef; const source, dest: treference);
  150. procedure concatcopy_record(list: TAsmList; size: tdef; const source, dest: treference);
  151. procedure concatcopy_set(list: TAsmList; size: tdef; const source, dest: treference);
  152. procedure concatcopy_shortstring(list: TAsmList; size: tdef; const source, dest: treference);
  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,
  542. enuminitref: treference;
  543. elemdef: tdef;
  544. i: longint;
  545. mangledname: string;
  546. opc: tasmop;
  547. parasize: longint;
  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, sets 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 in [recorddef,setdef]) or
  584. ((elemdef.typ=enumdef) and
  585. get_enum_init_val_ref(elemdef,enuminitref)) or
  586. is_shortstring(elemdef) or
  587. ((elemdef.typ=procvardef) and
  588. not tprocvardef(elemdef).is_addressonly) or
  589. is_ansistring(elemdef) or
  590. is_wide_or_unicode_string(elemdef) or
  591. is_dynamic_array(elemdef) then
  592. begin
  593. { duplicate array instance }
  594. list.concat(taicpu.op_none(a_dup));
  595. incstack(list,1);
  596. a_load_const_stack(list,s32inttype,initdim-1,R_INTREGISTER);
  597. parasize:=2;
  598. case elemdef.typ of
  599. arraydef:
  600. g_call_system_proc(list,'fpc_initialize_array_dynarr');
  601. recorddef,setdef,procvardef:
  602. begin
  603. tg.gethltemp(list,elemdef,elemdef.size,tt_persistent,recref);
  604. a_load_ref_stack(list,elemdef,recref,prepare_stack_for_ref(list,recref,false));
  605. inc(parasize);
  606. case elemdef.typ of
  607. recorddef:
  608. g_call_system_proc(list,'fpc_initialize_array_record');
  609. setdef:
  610. begin
  611. if tsetdef(elemdef).elementdef.typ=enumdef then
  612. g_call_system_proc(list,'fpc_initialize_array_enumset')
  613. else
  614. g_call_system_proc(list,'fpc_initialize_array_bitset')
  615. end;
  616. procvardef:
  617. g_call_system_proc(list,'fpc_initialize_array_procvar');
  618. end;
  619. tg.ungettemp(list,recref);
  620. end;
  621. enumdef:
  622. begin
  623. inc(parasize);
  624. a_load_ref_stack(list,java_jlobject,enuminitref,prepare_stack_for_ref(list,enuminitref,false));
  625. g_call_system_proc(list,'fpc_initialize_array_object');
  626. end;
  627. stringdef:
  628. begin
  629. case tstringdef(elemdef).stringtype of
  630. st_shortstring:
  631. begin
  632. inc(parasize);
  633. a_load_const_stack(list,u8inttype,tstringdef(elemdef).len,R_INTREGISTER);
  634. g_call_system_proc(list,'fpc_initialize_array_shortstring');
  635. end;
  636. st_ansistring:
  637. g_call_system_proc(list,'fpc_initialize_array_ansistring');
  638. st_unicodestring,
  639. st_widestring:
  640. g_call_system_proc(list,'fpc_initialize_array_unicodestring');
  641. else
  642. internalerror(2011081801);
  643. end;
  644. end;
  645. else
  646. internalerror(2011081801);
  647. end;
  648. decstack(list,parasize);
  649. end;
  650. end;
  651. procedure thlcgjvm.g_getarraylen(list: TAsmList; const arrloc: tlocation);
  652. var
  653. nillab,endlab: tasmlabel;
  654. begin
  655. { inline because we have to use the arraylength opcode, which
  656. cannot be represented directly in Pascal. Even though the JVM
  657. supports allocated arrays with length=0, we still also have to
  658. check for nil pointers because even if FPC always generates
  659. allocated empty arrays under all circumstances, external Java
  660. code could pass in nil pointers.
  661. Note that this means that assigned(arr) can be different from
  662. length(arr)<>0 for dynamic arrays when targeting the JVM.
  663. }
  664. current_asmdata.getjumplabel(nillab);
  665. current_asmdata.getjumplabel(endlab);
  666. { if assigned(arr) ... }
  667. a_load_loc_stack(list,java_jlobject,arrloc);
  668. list.concat(taicpu.op_none(a_dup));
  669. incstack(list,1);
  670. list.concat(taicpu.op_sym(a_ifnull,nillab));
  671. decstack(list,1);
  672. { ... then result:=arraylength(arr) ... }
  673. list.concat(taicpu.op_none(a_arraylength));
  674. a_jmp_always(list,endlab);
  675. { ... else result:=0 }
  676. a_label(list,nillab);
  677. list.concat(taicpu.op_none(a_pop));
  678. decstack(list,1);
  679. list.concat(taicpu.op_none(a_iconst_0));
  680. incstack(list,1);
  681. a_label(list,endlab);
  682. end;
  683. procedure thlcgjvm.a_cmp_stack_label(list: TAsmlist; size: tdef; cmp_op: topcmp; lab: tasmlabel);
  684. const
  685. opcmp2icmp: array[topcmp] of tasmop = (A_None,
  686. a_if_icmpeq,a_if_icmpgt,a_if_icmplt,a_if_icmpge,a_if_icmple,
  687. a_if_icmpne,a_if_icmple,a_if_icmplt,a_if_icmpge,a_if_icmpgt);
  688. var
  689. cgsize: tcgsize;
  690. begin
  691. case def2regtyp(size) of
  692. R_INTREGISTER:
  693. begin
  694. cgsize:=def_cgsize(size);
  695. case cgsize of
  696. OS_S8,OS_8,
  697. OS_16,OS_S16,
  698. OS_S32,OS_32:
  699. begin
  700. list.concat(taicpu.op_sym(opcmp2icmp[cmp_op],lab));
  701. decstack(list,2);
  702. end;
  703. OS_64,OS_S64:
  704. begin
  705. list.concat(taicpu.op_none(a_lcmp));
  706. decstack(list,3);
  707. list.concat(taicpu.op_sym(opcmp2if[cmp_op],lab));
  708. decstack(list,1);
  709. end;
  710. else
  711. internalerror(2010120538);
  712. end;
  713. end;
  714. R_ADDRESSREGISTER:
  715. begin
  716. case cmp_op of
  717. OC_EQ:
  718. list.concat(taicpu.op_sym(a_if_acmpeq,lab));
  719. OC_NE:
  720. list.concat(taicpu.op_sym(a_if_acmpne,lab));
  721. else
  722. internalerror(2010120537);
  723. end;
  724. decstack(list,2);
  725. end;
  726. else
  727. internalerror(2010120538);
  728. end;
  729. end;
  730. procedure thlcgjvm.maybe_adjust_cmp_stackval(list: TAsmlist; size: tdef; cmp_op: topcmp);
  731. begin
  732. { use cmp_op because eventually that's what indicates the
  733. signed/unsigned character of the operation, not the size... }
  734. if (cmp_op in [OC_EQ,OC_NE,OC_LT,OC_LTE,OC_GT,OC_GTE]) or
  735. (def2regtyp(size)<>R_INTREGISTER) then
  736. exit;
  737. { http://stackoverflow.com/questions/4068973/c-performing-signed-comparison-in-unsigned-variables-without-casting }
  738. case def_cgsize(size) of
  739. OS_32,OS_S32:
  740. a_op_const_stack(list,OP_XOR,size,cardinal($80000000));
  741. OS_64,OS_S64:
  742. a_op_const_stack(list,OP_XOR,size,aint($8000000000000000));
  743. end;
  744. end;
  745. function thlcgjvm.maybe_adjust_cmp_constval(size: tdef; cmp_op: topcmp; a: aint): aint;
  746. begin
  747. result:=a;
  748. { use cmp_op because eventually that's what indicates the
  749. signed/unsigned character of the operation, not the size... }
  750. if (cmp_op in [OC_EQ,OC_NE,OC_LT,OC_LTE,OC_GT,OC_GTE]) or
  751. (def2regtyp(size)<>R_INTREGISTER) then
  752. exit;
  753. case def_cgsize(size) of
  754. OS_32,OS_S32:
  755. result:=a xor cardinal($80000000);
  756. OS_64,OS_S64:
  757. result:=a xor aint($8000000000000000);
  758. end;
  759. end;
  760. procedure thlcgjvm.maybe_adjust_op_result(list: TAsmList; op: TOpCg; size: tdef);
  761. const
  762. overflowops = [OP_MUL,OP_SHL,OP_ADD,OP_SUB,OP_NOT,OP_NEG];
  763. begin
  764. if (op in overflowops) and
  765. (def_cgsize(size) in [OS_8,OS_S8,OS_16,OS_S16]) then
  766. resize_stack_int_val(list,OS_S32,def_cgsize(size),false);
  767. end;
  768. procedure thlcgjvm.gen_load_uninitialized_function_result(list: TAsmList; pd: tprocdef; resdef: tdef; const resloc: tcgpara);
  769. begin
  770. { constructors don't return anything in Java }
  771. if pd.proctypeoption=potype_constructor then
  772. exit;
  773. { must return a value of the correct type on the evaluation stack }
  774. case def2regtyp(resdef) of
  775. R_INTREGISTER,
  776. R_ADDRESSREGISTER:
  777. a_load_const_cgpara(list,resdef,0,resloc);
  778. R_FPUREGISTER:
  779. case tfloatdef(resdef).floattype of
  780. s32real:
  781. begin
  782. list.concat(taicpu.op_none(a_fconst_0));
  783. incstack(list,1);
  784. end;
  785. s64real:
  786. begin
  787. list.concat(taicpu.op_none(a_dconst_0));
  788. incstack(list,2);
  789. end;
  790. else
  791. internalerror(2011010302);
  792. end
  793. else
  794. internalerror(2011010301);
  795. end;
  796. end;
  797. procedure thlcgjvm.g_copyvalueparas(p: TObject; arg: pointer);
  798. var
  799. list: tasmlist;
  800. begin
  801. { zero-extend < 32 bit primitive types (FPC can zero-extend when calling,
  802. but that doesn't help when we're called from Java code or indirectly
  803. as a procvar -- exceptions: widechar (Java-specific type) and ordinal
  804. types whose upper bound does not set the sign bit }
  805. if (tsym(p).typ=paravarsym) and
  806. (tparavarsym(p).varspez=vs_value) and
  807. (tparavarsym(p).vardef.typ=orddef) and
  808. not is_pasbool(tparavarsym(p).vardef) and
  809. not is_widechar(tparavarsym(p).vardef) and
  810. (tparavarsym(p).vardef.size<4) and
  811. not is_signed(tparavarsym(p).vardef) and
  812. (torddef(tparavarsym(p).vardef).high>=(1 shl ((tparavarsym(p).vardef.size-1)*8))) then
  813. begin
  814. list:=TAsmList(arg);
  815. a_op_const_loc(list,OP_AND,tparavarsym(p).vardef,(1 shl (tparavarsym(p).vardef.size*8))-1,tparavarsym(p).initialloc);
  816. end;
  817. inherited g_copyvalueparas(p, arg);
  818. end;
  819. procedure thlcgjvm.inittempvariables(list: TAsmList);
  820. begin
  821. { these are automatically initialised when allocated if necessary }
  822. end;
  823. function thlcgjvm.prepare_stack_for_ref(list: TAsmList; const ref: treference; dup: boolean): longint;
  824. var
  825. href: treference;
  826. begin
  827. result:=0;
  828. { fake location that indicates the value is already on the stack? }
  829. if (ref.base=NR_EVAL_STACK_BASE) then
  830. exit;
  831. if ref.arrayreftype=art_none then
  832. begin
  833. { non-array accesses cannot have an index reg }
  834. if ref.index<>NR_NO then
  835. internalerror(2010120509);
  836. if (ref.base<>NR_NO) then
  837. begin
  838. if (ref.base<>NR_STACK_POINTER_REG) then
  839. begin
  840. { regular field -> load self on the stack }
  841. a_load_reg_stack(list,voidpointertype,ref.base);
  842. if dup then
  843. begin
  844. list.concat(taicpu.op_none(a_dup));
  845. incstack(list,1);
  846. end;
  847. { field name/type encoded in symbol, no index/offset }
  848. if not assigned(ref.symbol) or
  849. (ref.offset<>0) then
  850. internalerror(2010120524);
  851. result:=1;
  852. end
  853. else
  854. begin
  855. { local variable -> offset encoded in opcode and nothing to
  856. do here, except for checking that it's a valid reference }
  857. if assigned(ref.symbol) then
  858. internalerror(2010120523);
  859. end;
  860. end
  861. else
  862. begin
  863. { static field -> nothing to do here, except for validity check }
  864. if not assigned(ref.symbol) or
  865. (ref.offset<>0) then
  866. internalerror(2010120525);
  867. end;
  868. end
  869. else
  870. begin
  871. { arrays have implicit dereference -> pointer to array must have been
  872. loaded into base reg }
  873. if (ref.base=NR_NO) or
  874. (ref.base=NR_STACK_POINTER_REG) then
  875. internalerror(2010120511);
  876. if assigned(ref.symbol) then
  877. internalerror(2010120512);
  878. { stack: ... -> ..., arrayref, index }
  879. { load array base address }
  880. a_load_reg_stack(list,voidpointertype,ref.base);
  881. { index can either be in a register, or located in a simple memory
  882. location (since we have to load it anyway) }
  883. case ref.arrayreftype of
  884. art_indexreg:
  885. begin
  886. if ref.index=NR_NO then
  887. internalerror(2010120513);
  888. { all array indices in Java are 32 bit ints }
  889. a_load_reg_stack(list,s32inttype,ref.index);
  890. end;
  891. art_indexref:
  892. begin
  893. reference_reset_base(href,ref.indexbase,ref.indexoffset,4);
  894. href.symbol:=ref.indexsymbol;
  895. a_load_ref_stack(list,s32inttype,href,prepare_stack_for_ref(list,href,false));
  896. end;
  897. art_indexconst:
  898. begin
  899. a_load_const_stack(list,s32inttype,ref.indexoffset,R_INTREGISTER);
  900. end;
  901. else
  902. internalerror(2011012001);
  903. end;
  904. { adjustment of the index }
  905. if ref.offset<>0 then
  906. a_op_const_stack(list,OP_ADD,s32inttype,ref.offset);
  907. if dup then
  908. begin
  909. list.concat(taicpu.op_none(a_dup2));
  910. incstack(list,2);
  911. end;
  912. result:=2;
  913. end;
  914. end;
  915. procedure thlcgjvm.a_load_const_reg(list: TAsmList; tosize: tdef; a: aint; register: tregister);
  916. begin
  917. a_load_const_stack(list,tosize,a,def2regtyp(tosize));
  918. a_load_stack_reg(list,tosize,register);
  919. end;
  920. procedure thlcgjvm.a_load_const_ref(list: TAsmList; tosize: tdef; a: aint; const ref: treference);
  921. var
  922. extra_slots: longint;
  923. begin
  924. extra_slots:=prepare_stack_for_ref(list,ref,false);
  925. a_load_const_stack(list,tosize,a,def2regtyp(tosize));
  926. a_load_stack_ref(list,tosize,ref,extra_slots);
  927. end;
  928. procedure thlcgjvm.a_load_reg_ref(list: TAsmList; fromsize, tosize: tdef; register: tregister; const ref: treference);
  929. var
  930. extra_slots: longint;
  931. begin
  932. extra_slots:=prepare_stack_for_ref(list,ref,false);
  933. a_load_reg_stack(list,fromsize,register);
  934. a_load_stack_ref(list,tosize,ref,extra_slots);
  935. end;
  936. procedure thlcgjvm.a_load_reg_reg(list: TAsmList; fromsize, tosize: tdef; reg1, reg2: tregister);
  937. begin
  938. a_load_reg_stack(list,fromsize,reg1);
  939. if def2regtyp(fromsize)=R_INTREGISTER then
  940. resize_stack_int_val(list,def_cgsize(fromsize),def_cgsize(tosize),false);
  941. a_load_stack_reg(list,tosize,reg2);
  942. end;
  943. procedure thlcgjvm.a_load_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; register: tregister);
  944. var
  945. extra_slots: longint;
  946. begin
  947. extra_slots:=prepare_stack_for_ref(list,ref,false);
  948. a_load_ref_stack(list,fromsize,ref,extra_slots);
  949. if def2regtyp(fromsize)=R_INTREGISTER then
  950. resize_stack_int_val(list,def_cgsize(fromsize),def_cgsize(tosize),false);
  951. a_load_stack_reg(list,tosize,register);
  952. end;
  953. procedure thlcgjvm.a_load_ref_ref(list: TAsmList; fromsize, tosize: tdef; const sref: treference; const dref: treference);
  954. var
  955. extra_sslots,
  956. extra_dslots: longint;
  957. begin
  958. { make sure the destination reference is on top, since in the end the
  959. order has to be "destref, value" -> first create "destref, sourceref" }
  960. extra_dslots:=prepare_stack_for_ref(list,dref,false);
  961. extra_sslots:=prepare_stack_for_ref(list,sref,false);
  962. a_load_ref_stack(list,fromsize,sref,extra_sslots);
  963. if def2regtyp(fromsize)=R_INTREGISTER then
  964. resize_stack_int_val(list,def_cgsize(fromsize),def_cgsize(tosize),dref.arrayreftype<>art_none);
  965. a_load_stack_ref(list,tosize,dref,extra_dslots);
  966. end;
  967. procedure thlcgjvm.a_loadaddr_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; r: tregister);
  968. begin
  969. { only allowed for types that are not implicit pointers in Pascal (in
  970. that case, ref contains a pointer to the actual data and we simply
  971. return that pointer) }
  972. if not jvmimplicitpointertype(fromsize) then
  973. internalerror(2010120534);
  974. a_load_ref_reg(list,java_jlobject,java_jlobject,ref,r);
  975. end;
  976. procedure thlcgjvm.a_op_const_reg(list: TAsmList; Op: TOpCG; size: tdef; a: Aint; reg: TRegister);
  977. begin
  978. a_op_const_reg_reg(list,op,size,a,reg,reg);
  979. end;
  980. procedure thlcgjvm.a_op_const_reg_reg(list: TAsmList; op: TOpCg; size: tdef; a: aint; src, dst: tregister);
  981. begin
  982. a_load_reg_stack(list,size,src);
  983. a_op_const_stack(list,op,size,a);
  984. a_load_stack_reg(list,size,dst);
  985. end;
  986. procedure thlcgjvm.a_op_const_ref(list: TAsmList; Op: TOpCG; size: tdef; a: Aint; const ref: TReference);
  987. var
  988. extra_slots: longint;
  989. begin
  990. extra_slots:=prepare_stack_for_ref(list,ref,true);
  991. { TODO, here or in peepholeopt: use iinc when possible }
  992. a_load_ref_stack(list,size,ref,extra_slots);
  993. a_op_const_stack(list,op,size,a);
  994. a_load_stack_ref(list,size,ref,extra_slots);
  995. end;
  996. procedure thlcgjvm.a_op_ref_reg(list: TAsmList; Op: TOpCG; size: tdef; const ref: TReference; reg: TRegister);
  997. begin
  998. a_load_reg_stack(list,size,reg);
  999. a_op_ref_stack(list,op,size,ref);
  1000. a_load_stack_reg(list,size,reg);
  1001. end;
  1002. procedure thlcgjvm.a_op_reg_reg_reg(list: TAsmList; op: TOpCg; size: tdef; src1, src2, dst: tregister);
  1003. begin
  1004. a_load_reg_stack(list,size,src2);
  1005. a_op_reg_stack(list,op,size,src1);
  1006. a_load_stack_reg(list,size,dst);
  1007. end;
  1008. procedure thlcgjvm.a_op_reg_reg(list: TAsmList; Op: TOpCG; size: tdef; reg1, reg2: TRegister);
  1009. begin
  1010. a_op_reg_reg_reg(list,op,size,reg1,reg2,reg2);
  1011. end;
  1012. procedure thlcgjvm.a_cmp_const_ref_label(list: TAsmList; size: tdef; cmp_op: topcmp; a: aint; const ref: treference; l: tasmlabel);
  1013. begin
  1014. if ref.base<>NR_EVAL_STACK_BASE then
  1015. a_load_ref_stack(list,size,ref,prepare_stack_for_ref(list,ref,false));
  1016. maybe_adjust_cmp_stackval(list,size,cmp_op);
  1017. a_load_const_stack(list,size,maybe_adjust_cmp_constval(size,cmp_op,a),def2regtyp(size));
  1018. a_cmp_stack_label(list,size,cmp_op,l);
  1019. end;
  1020. procedure thlcgjvm.a_cmp_const_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; a: aint; reg: tregister; l: tasmlabel);
  1021. begin
  1022. a_load_reg_stack(list,size,reg);
  1023. maybe_adjust_cmp_stackval(list,size,cmp_op);
  1024. a_load_const_stack(list,size,maybe_adjust_cmp_constval(size,cmp_op,a),def2regtyp(size));
  1025. a_cmp_stack_label(list,size,cmp_op,l);
  1026. end;
  1027. procedure thlcgjvm.a_cmp_ref_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; const ref: treference; reg: tregister; l: tasmlabel);
  1028. begin
  1029. a_load_reg_stack(list,size,reg);
  1030. maybe_adjust_cmp_stackval(list,size,cmp_op);
  1031. if ref.base<>NR_EVAL_STACK_BASE then
  1032. a_load_ref_stack(list,size,ref,prepare_stack_for_ref(list,ref,false))
  1033. else
  1034. list.concat(taicpu.op_none(a_swap));
  1035. maybe_adjust_cmp_stackval(list,size,cmp_op);
  1036. a_cmp_stack_label(list,size,cmp_op,l);
  1037. end;
  1038. procedure thlcgjvm.a_cmp_reg_ref_label(list: TAsmList; size: tdef; cmp_op: topcmp; reg: tregister; const ref: treference; l: tasmlabel);
  1039. begin
  1040. if ref.base<>NR_EVAL_STACK_BASE then
  1041. a_load_ref_stack(list,size,ref,prepare_stack_for_ref(list,ref,false));
  1042. maybe_adjust_cmp_stackval(list,size,cmp_op);
  1043. a_load_reg_stack(list,size,reg);
  1044. maybe_adjust_cmp_stackval(list,size,cmp_op);
  1045. a_cmp_stack_label(list,size,cmp_op,l);
  1046. end;
  1047. procedure thlcgjvm.a_cmp_reg_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; reg1, reg2: tregister; l: tasmlabel);
  1048. begin
  1049. a_load_reg_stack(list,size,reg2);
  1050. maybe_adjust_cmp_stackval(list,size,cmp_op);
  1051. a_load_reg_stack(list,size,reg1);
  1052. maybe_adjust_cmp_stackval(list,size,cmp_op);
  1053. a_cmp_stack_label(list,size,cmp_op,l);
  1054. end;
  1055. procedure thlcgjvm.a_jmp_always(list: TAsmList; l: tasmlabel);
  1056. begin
  1057. list.concat(taicpu.op_sym(a_goto,current_asmdata.RefAsmSymbol(l.name)));
  1058. end;
  1059. procedure thlcgjvm.concatcopy_normal_array(list: TAsmList; size: tdef; const source, dest: treference);
  1060. var
  1061. procname: string;
  1062. eledef: tdef;
  1063. ndim: longint;
  1064. adddefaultlenparas: boolean;
  1065. begin
  1066. { load copy helper parameters on the stack }
  1067. a_load_ref_stack(list,java_jlobject,source,prepare_stack_for_ref(list,source,false));
  1068. a_load_ref_stack(list,java_jlobject,dest,prepare_stack_for_ref(list,dest,false));
  1069. { call copy helper }
  1070. eledef:=tarraydef(size).elementdef;
  1071. ndim:=1;
  1072. adddefaultlenparas:=true;
  1073. case eledef.typ of
  1074. orddef:
  1075. begin
  1076. case torddef(eledef).ordtype of
  1077. pasbool8,s8bit,u8bit,bool8bit,uchar,
  1078. s16bit,u16bit,bool16bit,pasbool16,
  1079. uwidechar,
  1080. s32bit,u32bit,bool32bit,pasbool32,
  1081. s64bit,u64bit,bool64bit,pasbool64,scurrency:
  1082. procname:='FPC_COPY_SHALLOW_ARRAY'
  1083. else
  1084. internalerror(2011020504);
  1085. end;
  1086. end;
  1087. arraydef:
  1088. begin
  1089. { call fpc_setlength_dynarr_multidim with deepcopy=true, and extra
  1090. parameters }
  1091. while (eledef.typ=arraydef) and
  1092. not is_dynamic_array(eledef) do
  1093. begin
  1094. eledef:=tarraydef(eledef).elementdef;
  1095. inc(ndim)
  1096. end;
  1097. if (ndim=1) then
  1098. procname:='FPC_COPY_SHALLOW_ARRAY'
  1099. else
  1100. begin
  1101. { deepcopy=true }
  1102. a_load_const_stack(list,pasbool8type,1,R_INTREGISTER);
  1103. { ndim }
  1104. a_load_const_stack(list,s32inttype,ndim,R_INTREGISTER);
  1105. { eletype }
  1106. a_load_const_stack(list,cwidechartype,ord(jvmarrtype_setlength(eledef)),R_INTREGISTER);
  1107. adddefaultlenparas:=false;
  1108. procname:='FPC_SETLENGTH_DYNARR_MULTIDIM';
  1109. end;
  1110. end;
  1111. recorddef:
  1112. procname:='FPC_COPY_JRECORD_ARRAY';
  1113. procvardef:
  1114. if tprocvardef(eledef).is_addressonly then
  1115. procname:='FPC_COPY_SHALLOW_ARRAY'
  1116. else
  1117. procname:='FPC_COPY_JPROCVAR_ARRAY';
  1118. setdef:
  1119. if tsetdef(eledef).elementdef.typ=enumdef then
  1120. procname:='FPC_COPY_JENUMSET_ARRAY'
  1121. else
  1122. procname:='FPC_COPY_JBITSET_ARRAY';
  1123. floatdef:
  1124. procname:='FPC_COPY_SHALLOW_ARRAY';
  1125. stringdef:
  1126. if is_shortstring(eledef) then
  1127. procname:='FPC_COPY_JSHORTSTRING_ARRAY'
  1128. else
  1129. procname:='FPC_COPY_SHALLOW_ARRAY';
  1130. variantdef:
  1131. begin
  1132. {$ifndef nounsupported}
  1133. procname:='FPC_COPY_SHALLOW_ARRAY';
  1134. {$else}
  1135. { todo: make a deep copy via clone... }
  1136. internalerror(2011020505);
  1137. {$endif}
  1138. end;
  1139. else
  1140. procname:='FPC_COPY_SHALLOW_ARRAY';
  1141. end;
  1142. if adddefaultlenparas then
  1143. begin
  1144. { -1, -1 means "copy entire array" }
  1145. a_load_const_stack(list,s32inttype,-1,R_INTREGISTER);
  1146. a_load_const_stack(list,s32inttype,-1,R_INTREGISTER);
  1147. end;
  1148. g_call_system_proc(list,procname);
  1149. if ndim=1 then
  1150. begin
  1151. decstack(list,2);
  1152. if adddefaultlenparas then
  1153. decstack(list,2);
  1154. end
  1155. else
  1156. begin
  1157. decstack(list,4);
  1158. { pop return value, must be the same as dest }
  1159. list.concat(taicpu.op_none(a_pop));
  1160. decstack(list,1);
  1161. end;
  1162. end;
  1163. procedure thlcgjvm.concatcopy_record(list: TAsmList; size: tdef; const source, dest: treference);
  1164. var
  1165. srsym: tsym;
  1166. pd: tprocdef;
  1167. begin
  1168. { self }
  1169. a_load_ref_stack(list,size,source,prepare_stack_for_ref(list,source,false));
  1170. { result }
  1171. a_load_ref_stack(list,size,dest,prepare_stack_for_ref(list,dest,false));
  1172. { call fpcDeepCopy helper }
  1173. srsym:=search_struct_member(tabstractrecorddef(size),'FPCDEEPCOPY');
  1174. if not assigned(srsym) or
  1175. (srsym.typ<>procsym) then
  1176. Message1(cg_f_unknown_compilerproc,size.typename+'.fpcDeepCopy');
  1177. pd:=tprocdef(tprocsym(srsym).procdeflist[0]);
  1178. a_call_name(list,pd,pd.mangledname,false);
  1179. { both parameters are removed, no function result }
  1180. decstack(list,2);
  1181. end;
  1182. procedure thlcgjvm.concatcopy_set(list: TAsmList; size: tdef; const source, dest: treference);
  1183. begin
  1184. a_load_ref_stack(list,size,source,prepare_stack_for_ref(list,source,false));
  1185. a_load_ref_stack(list,size,dest,prepare_stack_for_ref(list,dest,false));
  1186. { call set copy helper }
  1187. if tsetdef(size).elementdef.typ=enumdef then
  1188. g_call_system_proc(list,'fpc_enumset_copy')
  1189. else
  1190. g_call_system_proc(list,'fpc_bitset_copy');
  1191. { both parameters are removed, no function result }
  1192. decstack(list,2);
  1193. end;
  1194. procedure thlcgjvm.concatcopy_shortstring(list: TAsmList; size: tdef; const source, dest: treference);
  1195. var
  1196. srsym: tsym;
  1197. pd: tprocdef;
  1198. begin
  1199. { self }
  1200. a_load_ref_stack(list,size,source,prepare_stack_for_ref(list,source,false));
  1201. { result }
  1202. a_load_ref_stack(list,size,dest,prepare_stack_for_ref(list,dest,false));
  1203. { call fpcDeepCopy helper }
  1204. srsym:=search_struct_member(java_shortstring,'FPCDEEPCOPY');
  1205. if not assigned(srsym) or
  1206. (srsym.typ<>procsym) then
  1207. Message1(cg_f_unknown_compilerproc,'ShortstringClass.FpcDeepCopy');
  1208. pd:=tprocdef(tprocsym(srsym).procdeflist[0]);
  1209. a_call_name(list,pd,pd.mangledname,false);
  1210. { both parameters are removed, no function result }
  1211. decstack(list,2);
  1212. end;
  1213. procedure thlcgjvm.g_concatcopy(list: TAsmList; size: tdef; const source, dest: treference);
  1214. var
  1215. handled: boolean;
  1216. begin
  1217. handled:=false;
  1218. case size.typ of
  1219. arraydef:
  1220. begin
  1221. if not is_dynamic_array(size) then
  1222. begin
  1223. concatcopy_normal_array(list,size,source,dest);
  1224. handled:=true;
  1225. end;
  1226. end;
  1227. recorddef:
  1228. begin
  1229. concatcopy_record(list,size,source,dest);
  1230. handled:=true;
  1231. end;
  1232. setdef:
  1233. begin
  1234. concatcopy_set(list,size,source,dest);
  1235. handled:=true;
  1236. end;
  1237. stringdef:
  1238. begin
  1239. if is_shortstring(size) then
  1240. begin
  1241. concatcopy_shortstring(list,size,source,dest);
  1242. handled:=true;
  1243. end;
  1244. end;
  1245. procvardef:
  1246. begin
  1247. if not tprocvardef(size).is_addressonly then
  1248. begin
  1249. concatcopy_record(list,tprocvardef(size).classdef,source,dest);
  1250. handled:=true;
  1251. end;
  1252. end;
  1253. end;
  1254. if not handled then
  1255. inherited;
  1256. end;
  1257. procedure thlcgjvm.g_copyshortstring(list: TAsmList; const source, dest: treference; strdef: tstringdef);
  1258. begin
  1259. concatcopy_shortstring(list,strdef,source,dest);
  1260. end;
  1261. procedure thlcgjvm.a_loadfpu_ref_ref(list: TAsmList; fromsize, tosize: tdef; const ref1, ref2: treference);
  1262. var
  1263. dstack_slots: longint;
  1264. begin
  1265. dstack_slots:=prepare_stack_for_ref(list,ref2,false);
  1266. a_load_ref_stack(list,fromsize,ref1,prepare_stack_for_ref(list,ref1,false));
  1267. resizestackfpuval(list,def_cgsize(fromsize),def_cgsize(tosize));
  1268. a_load_stack_ref(list,tosize,ref2,dstack_slots);
  1269. end;
  1270. procedure thlcgjvm.a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; reg: tregister);
  1271. begin
  1272. a_load_ref_stack(list,fromsize,ref,prepare_stack_for_ref(list,ref,false));
  1273. resizestackfpuval(list,def_cgsize(fromsize),def_cgsize(tosize));
  1274. a_load_stack_reg(list,tosize,reg);
  1275. end;
  1276. procedure thlcgjvm.a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tdef; reg: tregister; const ref: treference);
  1277. var
  1278. dstack_slots: longint;
  1279. begin
  1280. dstack_slots:=prepare_stack_for_ref(list,ref,false);
  1281. a_load_reg_stack(list,fromsize,reg);
  1282. resizestackfpuval(list,def_cgsize(fromsize),def_cgsize(tosize));
  1283. a_load_stack_ref(list,tosize,ref,dstack_slots);
  1284. end;
  1285. procedure thlcgjvm.a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tdef; reg1, reg2: tregister);
  1286. begin
  1287. a_load_reg_stack(list,fromsize,reg1);
  1288. resizestackfpuval(list,def_cgsize(fromsize),def_cgsize(tosize));
  1289. a_load_stack_reg(list,tosize,reg2);
  1290. end;
  1291. procedure thlcgjvm.g_proc_entry(list: TAsmList; localsize: longint; nostackframe: boolean);
  1292. begin
  1293. { the localsize is based on tg.lasttemp -> already in terms of stack
  1294. slots rather than bytes }
  1295. list.concat(tai_directive.Create(asd_jlimit,'locals '+tostr(localsize)));
  1296. { we insert the unit initialisation code afterwards in the proginit code,
  1297. and it uses one stack slot }
  1298. if (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1299. fmaxevalstackheight:=max(1,fmaxevalstackheight);
  1300. list.concat(tai_directive.Create(asd_jlimit,'stack '+tostr(fmaxevalstackheight)));
  1301. end;
  1302. procedure thlcgjvm.g_proc_exit(list: TAsmList; parasize: longint; nostackframe: boolean);
  1303. var
  1304. retdef: tdef;
  1305. opc: tasmop;
  1306. begin
  1307. if current_procinfo.procdef.proctypeoption in [potype_constructor,potype_class_constructor] then
  1308. retdef:=voidtype
  1309. else
  1310. retdef:=current_procinfo.procdef.returndef;
  1311. case retdef.typ of
  1312. orddef:
  1313. case torddef(retdef).ordtype of
  1314. uvoid:
  1315. opc:=a_return;
  1316. s64bit,
  1317. u64bit,
  1318. scurrency:
  1319. opc:=a_lreturn;
  1320. else
  1321. opc:=a_ireturn;
  1322. end;
  1323. setdef:
  1324. opc:=a_areturn;
  1325. floatdef:
  1326. case tfloatdef(retdef).floattype of
  1327. s32real:
  1328. opc:=a_freturn;
  1329. s64real:
  1330. opc:=a_dreturn;
  1331. else
  1332. internalerror(2011010213);
  1333. end;
  1334. else
  1335. opc:=a_areturn;
  1336. end;
  1337. list.concat(taicpu.op_none(opc));
  1338. end;
  1339. procedure thlcgjvm.gen_load_return_value(list: TAsmList);
  1340. begin
  1341. { constructors don't return anything in the jvm }
  1342. if current_procinfo.procdef.proctypeoption in [potype_constructor,potype_class_constructor] then
  1343. exit;
  1344. inherited gen_load_return_value(list);
  1345. end;
  1346. procedure thlcgjvm.record_generated_code_for_procdef(pd: tprocdef; code, data: TAsmList);
  1347. begin
  1348. { add something to the al_procedures list as well, because if all al_*
  1349. lists are empty, the assembler writer isn't called }
  1350. if not code.empty and
  1351. current_asmdata.asmlists[al_procedures].empty then
  1352. current_asmdata.asmlists[al_procedures].concat(tai_align.Create(4));
  1353. pd.exprasmlist:=TAsmList.create;
  1354. pd.exprasmlist.concatlist(code);
  1355. if assigned(data) and
  1356. not data.empty then
  1357. internalerror(2010122801);
  1358. end;
  1359. procedure thlcgjvm.g_incrrefcount(list: TAsmList; t: tdef; const ref: treference);
  1360. begin
  1361. // do nothing
  1362. end;
  1363. procedure thlcgjvm.g_decrrefcount(list: TAsmList; t: tdef; const ref: treference);
  1364. begin
  1365. // do nothing
  1366. end;
  1367. procedure thlcgjvm.g_array_rtti_helper(list: TAsmList; t: tdef; const ref: treference; const highloc: tlocation; const name: string);
  1368. var
  1369. normaldim: longint;
  1370. eleref: treference;
  1371. begin
  1372. { only in case of initialisation, we have to set all elements to "empty" }
  1373. if name<>'FPC_INITIALIZE_ARRAY' then
  1374. exit;
  1375. { put array on the stack }
  1376. a_load_ref_stack(list,java_jlobject,ref,prepare_stack_for_ref(list,ref,false));
  1377. { in case it's an open array whose elements are regular arrays, put the
  1378. dimension of the regular arrays on the stack (otherwise pass 0) }
  1379. normaldim:=0;
  1380. while (t.typ=arraydef) and
  1381. not is_dynamic_array(t) do
  1382. begin
  1383. inc(normaldim);
  1384. t:=tarraydef(t).elementdef;
  1385. end;
  1386. a_load_const_stack(list,s32inttype,normaldim,R_INTREGISTER);
  1387. { highloc is invalid, the length is part of the array in Java }
  1388. if is_wide_or_unicode_string(t) then
  1389. g_call_system_proc(list,'fpc_initialize_array_unicodestring')
  1390. else if is_ansistring(t) then
  1391. g_call_system_proc(list,'fpc_initialize_array_ansistring')
  1392. else if is_dynamic_array(t) then
  1393. g_call_system_proc(list,'fpc_initialize_array_dynarr')
  1394. else if is_record(t) or
  1395. (t.typ=setdef) then
  1396. begin
  1397. tg.gethltemp(list,t,t.size,tt_persistent,eleref);
  1398. a_load_ref_stack(list,t,eleref,prepare_stack_for_ref(list,eleref,false));
  1399. if is_record(t) then
  1400. g_call_system_proc(list,'fpc_initialize_array_record')
  1401. else if tsetdef(t).elementdef.typ=enumdef then
  1402. g_call_system_proc(list,'fpc_initialize_array_enumset')
  1403. else
  1404. g_call_system_proc(list,'fpc_initialize_array_bitset');
  1405. tg.ungettemp(list,eleref);
  1406. end
  1407. else if (t.typ=enumdef) then
  1408. begin
  1409. if get_enum_init_val_ref(t,eleref) then
  1410. begin
  1411. a_load_ref_stack(list,java_jlobject,eleref,prepare_stack_for_ref(list,eleref,false));
  1412. g_call_system_proc(list,'fpc_initialize_array_object');
  1413. end;
  1414. end
  1415. else
  1416. internalerror(2011031901);
  1417. end;
  1418. procedure thlcgjvm.g_initialize(list: TAsmList; t: tdef; const ref: treference);
  1419. var
  1420. dummyloc: tlocation;
  1421. sym: tsym;
  1422. pd: tprocdef;
  1423. begin
  1424. if (t.typ=arraydef) and
  1425. not is_dynamic_array(t) then
  1426. begin
  1427. dummyloc.loc:=LOC_INVALID;
  1428. g_array_rtti_helper(list,tarraydef(t).elementdef,ref,dummyloc,'FPC_INITIALIZE_ARRAY')
  1429. end
  1430. else if is_record(t) then
  1431. begin
  1432. { call the fpcInitializeRec method }
  1433. sym:=tsym(trecorddef(t).symtable.find('FPCINITIALIZEREC'));
  1434. if assigned(sym) and
  1435. (sym.typ=procsym) then
  1436. begin
  1437. if tprocsym(sym).procdeflist.Count<>1 then
  1438. internalerror(2011071713);
  1439. pd:=tprocdef(tprocsym(sym).procdeflist[0]);
  1440. end;
  1441. a_load_ref_stack(list,java_jlobject,ref,prepare_stack_for_ref(list,ref,false));
  1442. a_call_name(list,pd,pd.mangledname,false);
  1443. { parameter removed, no result }
  1444. decstack(list,1);
  1445. end
  1446. else
  1447. a_load_const_ref(list,t,0,ref);
  1448. end;
  1449. procedure thlcgjvm.g_finalize(list: TAsmList; t: tdef; const ref: treference);
  1450. begin
  1451. // do nothing
  1452. end;
  1453. procedure thlcgjvm.location_get_data_ref(list: TAsmList; def: tdef; const l: tlocation; var ref: treference; loadref: boolean; alignment: longint);
  1454. var
  1455. tmploc: tlocation;
  1456. begin
  1457. { This routine is a combination of a generalised a_loadaddr_ref_reg()
  1458. that also works for addresses in registers (in case loadref is false)
  1459. and of a_load_ref_reg (in case loadref is true). It is used for
  1460. a) getting the address of managed var/out parameters
  1461. b) getting to the actual data of value types that are passed by
  1462. reference by the compiler (and then get a local copy at the caller
  1463. side). Normally, depending on whether this reference is passed in a
  1464. register or reference, we either need a reference with that register
  1465. as base or load the address in that reference and use that as a new
  1466. base.
  1467. Since the JVM cannot take the address of anything, all
  1468. "pass-by-reference" value parameters (which are always aggregate types)
  1469. are already simply the implicit pointer to the data (since arrays,
  1470. records, etc are already internally implicit pointers). This means
  1471. that if "loadref" is true, we must simply return this implicit pointer.
  1472. If it is false, we are supposed the take the address of this implicit
  1473. pointer, which is not possible.
  1474. However, managed types are also implicit pointers in Pascal, so in that
  1475. case "taking the address" again consists of simply returning the
  1476. implicit pointer/current value (in case of a var/out parameter, this
  1477. value is stored inside an array).
  1478. }
  1479. if not loadref then
  1480. begin
  1481. if not is_managed_type(def) then
  1482. internalerror(2011020601);
  1483. tmploc:=l;
  1484. end
  1485. else
  1486. begin
  1487. if not jvmimplicitpointertype(def) then
  1488. begin
  1489. { passed by reference in array of single element; l contains the
  1490. base address of the array }
  1491. location_reset_ref(tmploc,LOC_REFERENCE,OS_ADDR,4);
  1492. reference_reset_base(tmploc.reference,getaddressregister(list,java_jlobject),0,4);
  1493. tmploc.reference.arrayreftype:=art_indexconst;
  1494. tmploc.reference.indexoffset:=0;
  1495. a_load_loc_reg(list,java_jlobject,java_jlobject,l,tmploc.reference.base);
  1496. end
  1497. else
  1498. tmploc:=l;
  1499. end;
  1500. case tmploc.loc of
  1501. LOC_REGISTER,
  1502. LOC_CREGISTER :
  1503. begin
  1504. { the implicit pointer is in a register and has to be in a
  1505. reference -> create a reference and put it there }
  1506. location_force_mem(list,tmploc,java_jlobject);
  1507. ref:=tmploc.reference;
  1508. end;
  1509. LOC_REFERENCE,
  1510. LOC_CREFERENCE :
  1511. begin
  1512. ref:=tmploc.reference;
  1513. end;
  1514. else
  1515. internalerror(2011020603);
  1516. end;
  1517. end;
  1518. procedure thlcgjvm.g_copyvaluepara_openarray(list: TAsmList; const ref: treference; const lenloc: tlocation; arrdef: tarraydef; destreg: tregister);
  1519. var
  1520. localref: treference;
  1521. arrloc: tlocation;
  1522. stackslots: longint;
  1523. begin
  1524. { temporary reference for passing to concatcopy }
  1525. tg.gethltemp(list,java_jlobject,java_jlobject.size,tt_persistent,localref);
  1526. stackslots:=prepare_stack_for_ref(list,localref,false);
  1527. { create the local copy of the array (lenloc is invalid, get length
  1528. directly from the array) }
  1529. location_reset_ref(arrloc,LOC_REFERENCE,OS_ADDR,sizeof(pint));
  1530. arrloc.reference:=ref;
  1531. g_getarraylen(list,arrloc);
  1532. g_newarray(list,arrdef,1);
  1533. a_load_stack_ref(list,java_jlobject,localref,stackslots);
  1534. { copy the source array to the destination }
  1535. g_concatcopy(list,arrdef,ref,localref);
  1536. { and put the array pointer in the register as expected by the caller }
  1537. a_load_ref_reg(list,java_jlobject,java_jlobject,localref,destreg);
  1538. end;
  1539. procedure thlcgjvm.g_releasevaluepara_openarray(list: TAsmList; arrdef: tarraydef; const l: tlocation);
  1540. begin
  1541. // do nothing, long live garbage collection!
  1542. end;
  1543. procedure thlcgjvm.gen_initialize_code(list: TAsmList);
  1544. var
  1545. ref: treference;
  1546. begin
  1547. { create globals with wrapped types such as arrays/records }
  1548. case current_procinfo.procdef.proctypeoption of
  1549. potype_unitinit:
  1550. begin
  1551. reference_reset_base(ref,NR_NO,0,1);
  1552. if assigned(current_module.globalsymtable) then
  1553. allocate_implicit_structs_for_st_with_base_ref(list,current_module.globalsymtable,ref,staticvarsym);
  1554. allocate_implicit_structs_for_st_with_base_ref(list,current_module.localsymtable,ref,staticvarsym);
  1555. end;
  1556. potype_class_constructor:
  1557. begin
  1558. { also initialise local variables, if any }
  1559. inherited;
  1560. { initialise class fields }
  1561. reference_reset_base(ref,NR_NO,0,1);
  1562. allocate_implicit_structs_for_st_with_base_ref(list,tabstractrecorddef(current_procinfo.procdef.owner.defowner).symtable,ref,staticvarsym);
  1563. end
  1564. else
  1565. inherited
  1566. end;
  1567. end;
  1568. procedure thlcgjvm.gen_entry_code(list: TAsmList);
  1569. begin
  1570. list.concat(Tai_force_line.Create);
  1571. end;
  1572. procedure thlcgjvm.gen_exit_code(list: TAsmList);
  1573. begin
  1574. { nothing }
  1575. end;
  1576. procedure thlcgjvm.a_load_stack_reg(list: TAsmList; size: tdef; reg: tregister);
  1577. var
  1578. opc: tasmop;
  1579. finishandval: aint;
  1580. begin
  1581. opc:=loadstoreopc(size,false,false,finishandval);
  1582. list.concat(taicpu.op_reg(opc,reg));
  1583. { avoid problems with getting the size of an open array etc }
  1584. if jvmimplicitpointertype(size) then
  1585. size:=java_jlobject;
  1586. decstack(list,1+ord(size.size>4));
  1587. end;
  1588. procedure thlcgjvm.a_load_stack_ref(list: TAsmList; size: tdef; const ref: treference; extra_slots: longint);
  1589. var
  1590. opc: tasmop;
  1591. finishandval: aint;
  1592. begin
  1593. { fake location that indicates the value has to remain on the stack }
  1594. if ref.base=NR_EVAL_STACK_BASE then
  1595. exit;
  1596. opc:=loadstoreopcref(size,false,ref,finishandval);
  1597. if ref.arrayreftype=art_none then
  1598. list.concat(taicpu.op_ref(opc,ref))
  1599. else
  1600. list.concat(taicpu.op_none(opc));
  1601. { avoid problems with getting the size of an open array etc }
  1602. if jvmimplicitpointertype(size) then
  1603. size:=java_jlobject;
  1604. decstack(list,1+ord(size.size>4)+extra_slots);
  1605. end;
  1606. procedure thlcgjvm.a_load_reg_stack(list: TAsmList; size: tdef; reg: tregister);
  1607. var
  1608. opc: tasmop;
  1609. finishandval: aint;
  1610. begin
  1611. opc:=loadstoreopc(size,true,false,finishandval);
  1612. list.concat(taicpu.op_reg(opc,reg));
  1613. { avoid problems with getting the size of an open array etc }
  1614. if jvmimplicitpointertype(size) then
  1615. size:=java_jlobject;
  1616. incstack(list,1+ord(size.size>4));
  1617. if finishandval<>-1 then
  1618. a_op_const_stack(list,OP_AND,size,finishandval);
  1619. end;
  1620. procedure thlcgjvm.a_load_ref_stack(list: TAsmList; size: tdef; const ref: treference; extra_slots: longint);
  1621. var
  1622. opc: tasmop;
  1623. finishandval: aint;
  1624. begin
  1625. { fake location that indicates the value is already on the stack? }
  1626. if (ref.base=NR_EVAL_STACK_BASE) then
  1627. exit;
  1628. opc:=loadstoreopcref(size,true,ref,finishandval);
  1629. if ref.arrayreftype=art_none then
  1630. list.concat(taicpu.op_ref(opc,ref))
  1631. else
  1632. list.concat(taicpu.op_none(opc));
  1633. { avoid problems with getting the size of an open array etc }
  1634. if jvmimplicitpointertype(size) then
  1635. size:=java_jlobject;
  1636. incstack(list,1+ord(size.size>4)-extra_slots);
  1637. if finishandval<>-1 then
  1638. a_op_const_stack(list,OP_AND,size,finishandval);
  1639. end;
  1640. function thlcgjvm.loadstoreopcref(def: tdef; isload: boolean; const ref: treference; out finishandval: aint): tasmop;
  1641. const
  1642. { isload static }
  1643. getputopc: array[boolean,boolean] of tasmop =
  1644. ((a_putfield,a_putstatic),
  1645. (a_getfield,a_getstatic));
  1646. begin
  1647. if assigned(ref.symbol) then
  1648. begin
  1649. { -> either a global (static) field, or a regular field. If a regular
  1650. field, then ref.base contains the self pointer, otherwise
  1651. ref.base=NR_NO. In both cases, the symbol contains all other
  1652. information (combined field name and type descriptor) }
  1653. result:=getputopc[isload,ref.base=NR_NO];
  1654. finishandval:=-1;
  1655. { erase sign extension for byte/smallint loads }
  1656. if (def2regtyp(def)=R_INTREGISTER) and
  1657. not is_signed(def) then
  1658. case def.size of
  1659. 1: finishandval:=255;
  1660. 2: finishandval:=65535;
  1661. end;
  1662. end
  1663. else
  1664. result:=loadstoreopc(def,isload,ref.arrayreftype<>art_none,finishandval);
  1665. end;
  1666. function thlcgjvm.loadstoreopc(def: tdef; isload, isarray: boolean; out finishandval: aint): tasmop;
  1667. var
  1668. size: longint;
  1669. begin
  1670. finishandval:=-1;
  1671. case def2regtyp(def) of
  1672. R_INTREGISTER:
  1673. begin
  1674. size:=def.size;
  1675. if not isarray then
  1676. begin
  1677. case size of
  1678. 1,2,3,4:
  1679. if isload then
  1680. result:=a_iload
  1681. else
  1682. result:=a_istore;
  1683. 8:
  1684. if isload then
  1685. result:=a_lload
  1686. else
  1687. result:=a_lstore;
  1688. else
  1689. internalerror(2011032814);
  1690. end;
  1691. end
  1692. { array }
  1693. else if isload then
  1694. begin
  1695. case size of
  1696. 1:
  1697. begin
  1698. result:=a_baload;
  1699. if not is_signed(def) then
  1700. finishandval:=255;
  1701. end;
  1702. 2:
  1703. begin
  1704. if is_widechar(def) then
  1705. result:=a_caload
  1706. else
  1707. begin
  1708. result:=a_saload;
  1709. { if we'd treat arrays of word as "array of widechar" we
  1710. could use a_caload, but that would make for even more
  1711. awkward interfacing with external Java code }
  1712. if not is_signed(def) then
  1713. finishandval:=65535;
  1714. end;
  1715. end;
  1716. 4: result:=a_iaload;
  1717. 8: result:=a_laload;
  1718. else
  1719. internalerror(2010120503);
  1720. end
  1721. end
  1722. else
  1723. begin
  1724. case size of
  1725. 1: result:=a_bastore;
  1726. 2: if not is_widechar(def) then
  1727. result:=a_sastore
  1728. else
  1729. result:=a_castore;
  1730. 4: result:=a_iastore;
  1731. 8: result:=a_lastore;
  1732. else
  1733. internalerror(2010120508);
  1734. end
  1735. end
  1736. end;
  1737. R_ADDRESSREGISTER:
  1738. if not isarray then
  1739. if isload then
  1740. result:=a_aload
  1741. else
  1742. result:=a_astore
  1743. else if isload then
  1744. result:=a_aaload
  1745. else
  1746. result:=a_aastore;
  1747. R_FPUREGISTER:
  1748. begin
  1749. case tfloatdef(def).floattype of
  1750. s32real:
  1751. if not isarray then
  1752. if isload then
  1753. result:=a_fload
  1754. else
  1755. result:=a_fstore
  1756. else if isload then
  1757. result:=a_faload
  1758. else
  1759. result:=a_fastore;
  1760. s64real:
  1761. if not isarray then
  1762. if isload then
  1763. result:=a_dload
  1764. else
  1765. result:=a_dstore
  1766. else if isload then
  1767. result:=a_daload
  1768. else
  1769. result:=a_dastore;
  1770. else
  1771. internalerror(2010120504);
  1772. end
  1773. end
  1774. else
  1775. internalerror(2010120502);
  1776. end;
  1777. end;
  1778. procedure thlcgjvm.resize_stack_int_val(list: TAsmList; fromsize, tosize: tcgsize; forarraystore: boolean);
  1779. begin
  1780. if fromsize in [OS_S64,OS_64] then
  1781. begin
  1782. if not(tosize in [OS_S64,OS_64]) then
  1783. begin
  1784. { truncate }
  1785. list.concat(taicpu.op_none(a_l2i));
  1786. decstack(list,1);
  1787. end;
  1788. end
  1789. else if tosize in [OS_S64,OS_64] then
  1790. begin
  1791. { extend }
  1792. list.concat(taicpu.op_none(a_i2l));
  1793. incstack(list,1);
  1794. { if it was an unsigned 32 bit value, remove sign extension }
  1795. if fromsize=OS_32 then
  1796. a_op_const_stack(list,OP_AND,s64inttype,cardinal($ffffffff));
  1797. end;
  1798. { if the value is immediately stored to an array afterwards, the store
  1799. instruction will properly truncate the value; otherwise we may need
  1800. additional truncation, except for 64/32 bit conversions, which are
  1801. already handled above }
  1802. if not forarraystore and
  1803. (not(fromsize in [OS_S64,OS_64,OS_32,OS_S32]) or
  1804. not(tosize in [OS_S64,OS_64,OS_32,OS_S32])) and
  1805. (tcgsize2size[fromsize]>tcgsize2size[tosize]) or
  1806. ((tcgsize2size[fromsize]=tcgsize2size[tosize]) and
  1807. (fromsize<>tosize)) or
  1808. { needs to mask out the sign in the top 16 bits }
  1809. ((fromsize=OS_S8) and
  1810. (tosize=OS_16)) then
  1811. case tosize of
  1812. OS_8:
  1813. a_op_const_stack(list,OP_AND,s32inttype,255);
  1814. OS_S8:
  1815. list.concat(taicpu.op_none(a_i2b));
  1816. OS_16:
  1817. list.concat(taicpu.op_none(a_i2c));
  1818. OS_S16:
  1819. list.concat(taicpu.op_none(a_i2s));
  1820. end;
  1821. end;
  1822. procedure thlcgjvm.allocate_implicit_struct_with_base_ref(list: TAsmList; vs: tabstractvarsym; ref: treference);
  1823. var
  1824. tmpref: treference;
  1825. begin
  1826. ref.symbol:=current_asmdata.RefAsmSymbol(vs.mangledname);
  1827. tg.gethltemp(list,vs.vardef,vs.vardef.size,tt_persistent,tmpref);
  1828. { only copy the reference, not the actual data }
  1829. a_load_ref_ref(list,java_jlobject,java_jlobject,tmpref,ref);
  1830. { remains live since there's still a reference to the created
  1831. entity }
  1832. tg.ungettemp(list,tmpref);
  1833. end;
  1834. procedure thlcgjvm.allocate_enum_with_base_ref(list: TAsmList; vs: tabstractvarsym; const initref: treference; destbaseref: treference);
  1835. begin
  1836. destbaseref.symbol:=current_asmdata.RefAsmSymbol(vs.mangledname);
  1837. { only copy the reference, not the actual data }
  1838. a_load_ref_ref(list,java_jlobject,java_jlobject,initref,destbaseref);
  1839. end;
  1840. function thlcgjvm.get_enum_init_val_ref(def: tdef; out ref: treference): boolean;
  1841. var
  1842. sym: tstaticvarsym;
  1843. begin
  1844. result:=false;
  1845. sym:=tstaticvarsym(tenumdef(def).getbasedef.classdef.symtable.Find('__FPC_ZERO_INITIALIZER'));
  1846. { no enum with ordinal value 0 -> exit }
  1847. if not assigned(sym) then
  1848. exit;
  1849. reference_reset_symbol(ref,current_asmdata.RefAsmSymbol(sym.mangledname),0,4);
  1850. result:=true;
  1851. end;
  1852. procedure thlcgjvm.allocate_implicit_structs_for_st_with_base_ref(list: TAsmList; st: tsymtable; const ref: treference; allocvartyp: tsymtyp);
  1853. var
  1854. vs: tabstractvarsym;
  1855. def: tdef;
  1856. i: longint;
  1857. initref: treference;
  1858. begin
  1859. for i:=0 to st.symlist.count-1 do
  1860. begin
  1861. if (tsym(st.symlist[i]).typ<>allocvartyp) then
  1862. continue;
  1863. vs:=tabstractvarsym(st.symlist[i]);
  1864. if sp_static in vs.symoptions then
  1865. continue;
  1866. { vo_is_external and vo_has_local_copy means a staticvarsym that is
  1867. alias for a constsym, whose sole purpose is for allocating and
  1868. intialising the constant }
  1869. if [vo_is_external,vo_has_local_copy]*vs.varoptions=[vo_is_external] then
  1870. continue;
  1871. if jvmimplicitpointertype(vs.vardef) then
  1872. allocate_implicit_struct_with_base_ref(list,vs,ref)
  1873. { enums are class instances in Java, while they are ordinals in
  1874. Pascal. When they are initialized with enum(0), such as in
  1875. constructors or global variables, initialize them with the
  1876. enum instance for 0 if it exists (if not, it remains nil since
  1877. there is no valid enum value in it) }
  1878. else if (vs.vardef.typ=enumdef) and
  1879. get_enum_init_val_ref(vs.vardef,initref) then
  1880. allocate_enum_with_base_ref(list,vs,initref,ref);
  1881. end;
  1882. { process symtables of routines part of this symtable (for local typed
  1883. constants) }
  1884. if allocvartyp=staticvarsym then
  1885. begin
  1886. for i:=0 to st.deflist.count-1 do
  1887. begin
  1888. def:=tdef(st.deflist[i]);
  1889. { the unit symtable also contains the methods of classes defined
  1890. in that unit -> skip them when processing the unit itself.
  1891. Localst is not assigned for the main program code.
  1892. Localst can be the same as st in case of unit init code. }
  1893. if (def.typ<>procdef) or
  1894. (def.owner<>st) or
  1895. not assigned(tprocdef(def).localst) or
  1896. (tprocdef(def).localst=st) then
  1897. continue;
  1898. allocate_implicit_structs_for_st_with_base_ref(list,tprocdef(def).localst,ref,allocvartyp);
  1899. end;
  1900. end;
  1901. end;
  1902. procedure thlcgjvm.gen_initialize_fields_code(list: TAsmList);
  1903. var
  1904. sym: tsym;
  1905. selfpara: tparavarsym;
  1906. selfreg: tregister;
  1907. ref: treference;
  1908. obj: tabstractrecorddef;
  1909. i: longint;
  1910. needinit: boolean;
  1911. begin
  1912. obj:=tabstractrecorddef(current_procinfo.procdef.owner.defowner);
  1913. { check whether there are any fields that need initialisation }
  1914. needinit:=false;
  1915. for i:=0 to obj.symtable.symlist.count-1 do
  1916. begin
  1917. sym:=tsym(obj.symtable.symlist[i]);
  1918. if (sym.typ=fieldvarsym) and
  1919. (jvmimplicitpointertype(tfieldvarsym(sym).vardef) or
  1920. ((tfieldvarsym(sym).vardef.typ=enumdef) and
  1921. get_enum_init_val_ref(tfieldvarsym(sym).vardef,ref))) then
  1922. begin
  1923. needinit:=true;
  1924. break;
  1925. end;
  1926. end;
  1927. if not needinit then
  1928. exit;
  1929. selfpara:=tparavarsym(current_procinfo.procdef.parast.find('self'));
  1930. if not assigned(selfpara) then
  1931. internalerror(2011033001);
  1932. selfreg:=getaddressregister(list,selfpara.vardef);
  1933. a_load_loc_reg(list,obj,obj,selfpara.localloc,selfreg);
  1934. reference_reset_base(ref,selfreg,0,1);
  1935. allocate_implicit_structs_for_st_with_base_ref(list,obj.symtable,ref,fieldvarsym);
  1936. end;
  1937. procedure thlcgjvm.resizestackfpuval(list: TAsmList; fromsize, tosize: tcgsize);
  1938. begin
  1939. if (fromsize=OS_F32) and
  1940. (tosize=OS_F64) then
  1941. begin
  1942. list.concat(taicpu.op_none(a_f2d));
  1943. incstack(list,1);
  1944. end
  1945. else if (fromsize=OS_F64) and
  1946. (tosize=OS_F32) then
  1947. begin
  1948. list.concat(taicpu.op_none(a_d2f));
  1949. decstack(list,1);
  1950. end;
  1951. end;
  1952. procedure thlcgjvm.maybepreparedivu32(list: TAsmList; var op: topcg; size: tdef; out isdivu32: boolean);
  1953. begin
  1954. if (op=OP_DIV) and
  1955. (def_cgsize(size)=OS_32) then
  1956. begin
  1957. { needs zero-extension to 64 bit, because the JVM only supports
  1958. signed divisions }
  1959. resize_stack_int_val(list,OS_32,OS_S64,false);
  1960. op:=OP_IDIV;
  1961. isdivu32:=true;
  1962. end
  1963. else
  1964. isdivu32:=false;
  1965. end;
  1966. procedure thlcgjvm.a_call_name_intern(list: TAsmList; pd: tprocdef; const s: TSymStr; inheritedcall: boolean);
  1967. var
  1968. opc: tasmop;
  1969. begin
  1970. {
  1971. invoke types:
  1972. * invokeinterface: call method from an interface (must also specify
  1973. number of parameters in terms of stack slot count!)
  1974. * invokespecial: invoke a constructor, method in a superclass,
  1975. or private instance method
  1976. * invokestatic: invoke a class method (private or not)
  1977. * invokevirtual: invoke a regular method
  1978. }
  1979. case pd.owner.symtabletype of
  1980. globalsymtable,
  1981. staticsymtable,
  1982. localsymtable:
  1983. { regular and nested procedures are turned into static methods }
  1984. opc:=a_invokestatic;
  1985. objectsymtable:
  1986. begin
  1987. case tobjectdef(pd.owner.defowner).objecttype of
  1988. odt_javaclass:
  1989. begin
  1990. if (po_classmethod in pd.procoptions) then
  1991. opc:=a_invokestatic
  1992. else if (pd.visibility=vis_strictprivate) or
  1993. (pd.proctypeoption=potype_constructor) or
  1994. inheritedcall then
  1995. opc:=a_invokespecial
  1996. else
  1997. opc:=a_invokevirtual;
  1998. end;
  1999. odt_interfacejava:
  2000. { static interface methods are not allowed }
  2001. opc:=a_invokeinterface;
  2002. else
  2003. internalerror(2010122601);
  2004. end;
  2005. end;
  2006. recordsymtable:
  2007. begin
  2008. if (po_staticmethod in pd.procoptions) then
  2009. opc:=a_invokestatic
  2010. else if (pd.visibility=vis_strictprivate) or
  2011. (pd.proctypeoption=potype_constructor) or
  2012. inheritedcall then
  2013. opc:=a_invokespecial
  2014. else
  2015. opc:=a_invokevirtual;
  2016. end
  2017. else
  2018. internalerror(2010122602);
  2019. end;
  2020. if (opc<>a_invokeinterface) then
  2021. list.concat(taicpu.op_sym(opc,current_asmdata.RefAsmSymbol(s)))
  2022. else
  2023. begin
  2024. pd.init_paraloc_info(calleeside);
  2025. list.concat(taicpu.op_sym_const(opc,current_asmdata.RefAsmSymbol(s),pd.calleeargareasize));
  2026. end;
  2027. end;
  2028. procedure create_hlcodegen;
  2029. begin
  2030. hlcg:=thlcgjvm.create;
  2031. create_codegen;
  2032. end;
  2033. end.