hlcgcpu.pas 77 KB

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