hlcgcpu.pas 79 KB

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