hlcgcpu.pas 88 KB

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