hlcgcpu.pas 80 KB

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