hlcgcpu.pas 75 KB

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