hlcgcpu.pas 89 KB

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