hlcgcpu.pas 89 KB

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