hlcgcpu.pas 87 KB

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