hlcgcpu.pas 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118
  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. { use cmp_op because eventually that's what indicates the
  699. signed/unsigned character of the operation, not the size... }
  700. if (cmp_op in [OC_EQ,OC_NE,OC_LT,OC_LTE,OC_GT,OC_GTE]) or
  701. (def2regtyp(size)<>R_INTREGISTER) then
  702. exit;
  703. { http://stackoverflow.com/questions/4068973/c-performing-signed-comparison-in-unsigned-variables-without-casting }
  704. case def_cgsize(size) of
  705. OS_32,OS_S32:
  706. a_op_const_stack(list,OP_XOR,size,cardinal($80000000));
  707. OS_64,OS_S64:
  708. a_op_const_stack(list,OP_XOR,size,aint($8000000000000000));
  709. end;
  710. end;
  711. function thlcgjvm.maybe_adjust_cmp_constval(size: tdef; cmp_op: topcmp; a: aint): aint;
  712. begin
  713. result:=a;
  714. { use cmp_op because eventually that's what indicates the
  715. signed/unsigned character of the operation, not the size... }
  716. if (cmp_op in [OC_EQ,OC_NE,OC_LT,OC_LTE,OC_GT,OC_GTE]) or
  717. (def2regtyp(size)<>R_INTREGISTER) then
  718. exit;
  719. case def_cgsize(size) of
  720. OS_32,OS_S32:
  721. result:=a xor cardinal($80000000);
  722. OS_64,OS_S64:
  723. result:=a xor aint($8000000000000000);
  724. end;
  725. end;
  726. procedure thlcgjvm.maybe_adjust_op_result(list: TAsmList; op: TOpCg; size: tdef);
  727. const
  728. overflowops = [OP_MUL,OP_SHL,OP_ADD,OP_SUB,OP_NOT,OP_NEG];
  729. begin
  730. if (op in overflowops) and
  731. (def_cgsize(size) in [OS_8,OS_S8,OS_16,OS_S16]) then
  732. resize_stack_int_val(list,OS_S32,def_cgsize(size),false);
  733. end;
  734. procedure thlcgjvm.gen_load_uninitialized_function_result(list: TAsmList; pd: tprocdef; resdef: tdef; const resloc: tcgpara);
  735. begin
  736. { constructors don't return anything in Java }
  737. if pd.proctypeoption=potype_constructor then
  738. exit;
  739. { must return a value of the correct type on the evaluation stack }
  740. case def2regtyp(resdef) of
  741. R_INTREGISTER,
  742. R_ADDRESSREGISTER:
  743. a_load_const_cgpara(list,resdef,0,resloc);
  744. R_FPUREGISTER:
  745. case tfloatdef(resdef).floattype of
  746. s32real:
  747. list.concat(taicpu.op_none(a_fconst_0));
  748. s64real:
  749. list.concat(taicpu.op_none(a_dconst_0));
  750. else
  751. internalerror(2011010302);
  752. end
  753. else
  754. internalerror(2011010301);
  755. end;
  756. end;
  757. procedure thlcgjvm.g_copyvalueparas(p: TObject; arg: pointer);
  758. var
  759. list: tasmlist;
  760. begin
  761. { zero-extend < 32 bit primitive types (FPC can zero-extend when calling,
  762. but that doesn't help when we're called from Java code or indirectly
  763. as a procvar -- exceptions: widechar (Java-specific type) and ordinal
  764. types whose upper bound does not set the sign bit }
  765. if (tsym(p).typ=paravarsym) and
  766. (tparavarsym(p).varspez=vs_value) and
  767. (tparavarsym(p).vardef.typ=orddef) and
  768. not is_pasbool(tparavarsym(p).vardef) and
  769. not is_widechar(tparavarsym(p).vardef) and
  770. (tparavarsym(p).vardef.size<4) and
  771. not is_signed(tparavarsym(p).vardef) and
  772. (torddef(tparavarsym(p).vardef).high>=(1 shl ((tparavarsym(p).vardef.size-1)*8))) then
  773. begin
  774. list:=TAsmList(arg);
  775. a_op_const_loc(list,OP_AND,tparavarsym(p).vardef,(1 shl (tparavarsym(p).vardef.size*8))-1,tparavarsym(p).initialloc);
  776. end;
  777. inherited g_copyvalueparas(p, arg);
  778. end;
  779. procedure thlcgjvm.inittempvariables(list: TAsmList);
  780. begin
  781. { these are automatically initialised when allocated if necessary }
  782. end;
  783. function thlcgjvm.prepare_stack_for_ref(list: TAsmList; const ref: treference; dup: boolean): longint;
  784. var
  785. href: treference;
  786. begin
  787. result:=0;
  788. { fake location that indicates the value is already on the stack? }
  789. if (ref.base=NR_EVAL_STACK_BASE) then
  790. exit;
  791. if ref.arrayreftype=art_none then
  792. begin
  793. { non-array accesses cannot have an index reg }
  794. if ref.index<>NR_NO then
  795. internalerror(2010120509);
  796. if (ref.base<>NR_NO) then
  797. begin
  798. if (ref.base<>NR_STACK_POINTER_REG) then
  799. begin
  800. { regular field -> load self on the stack }
  801. a_load_reg_stack(list,voidpointertype,ref.base);
  802. if dup then
  803. begin
  804. list.concat(taicpu.op_none(a_dup));
  805. incstack(list,1);
  806. end;
  807. { field name/type encoded in symbol, no index/offset }
  808. if not assigned(ref.symbol) or
  809. (ref.offset<>0) then
  810. internalerror(2010120524);
  811. result:=1;
  812. end
  813. else
  814. begin
  815. { local variable -> offset encoded in opcode and nothing to
  816. do here, except for checking that it's a valid reference }
  817. if assigned(ref.symbol) then
  818. internalerror(2010120523);
  819. end;
  820. end
  821. else
  822. begin
  823. { static field -> nothing to do here, except for validity check }
  824. if not assigned(ref.symbol) or
  825. (ref.offset<>0) then
  826. internalerror(2010120525);
  827. end;
  828. end
  829. else
  830. begin
  831. { arrays have implicit dereference -> pointer to array must have been
  832. loaded into base reg }
  833. if (ref.base=NR_NO) or
  834. (ref.base=NR_STACK_POINTER_REG) then
  835. internalerror(2010120511);
  836. if assigned(ref.symbol) then
  837. internalerror(2010120512);
  838. { stack: ... -> ..., arrayref, index }
  839. { load array base address }
  840. a_load_reg_stack(list,voidpointertype,ref.base);
  841. { index can either be in a register, or located in a simple memory
  842. location (since we have to load it anyway) }
  843. case ref.arrayreftype of
  844. art_indexreg:
  845. begin
  846. if ref.index=NR_NO then
  847. internalerror(2010120513);
  848. { all array indices in Java are 32 bit ints }
  849. a_load_reg_stack(list,s32inttype,ref.index);
  850. end;
  851. art_indexref:
  852. begin
  853. reference_reset_base(href,ref.indexbase,ref.indexoffset,4);
  854. href.symbol:=ref.indexsymbol;
  855. a_load_ref_stack(list,s32inttype,href,prepare_stack_for_ref(list,href,false));
  856. end;
  857. art_indexconst:
  858. begin
  859. a_load_const_stack(list,s32inttype,ref.indexoffset,R_INTREGISTER);
  860. end;
  861. else
  862. internalerror(2011012001);
  863. end;
  864. { adjustment of the index }
  865. if ref.offset<>0 then
  866. a_op_const_stack(list,OP_ADD,s32inttype,ref.offset);
  867. if dup then
  868. begin
  869. list.concat(taicpu.op_none(a_dup2));
  870. incstack(list,2);
  871. end;
  872. result:=2;
  873. end;
  874. end;
  875. procedure thlcgjvm.a_load_const_reg(list: TAsmList; tosize: tdef; a: aint; register: tregister);
  876. begin
  877. a_load_const_stack(list,tosize,a,def2regtyp(tosize));
  878. a_load_stack_reg(list,tosize,register);
  879. end;
  880. procedure thlcgjvm.a_load_const_ref(list: TAsmList; tosize: tdef; a: aint; const ref: treference);
  881. var
  882. extra_slots: longint;
  883. begin
  884. extra_slots:=prepare_stack_for_ref(list,ref,false);
  885. a_load_const_stack(list,tosize,a,def2regtyp(tosize));
  886. a_load_stack_ref(list,tosize,ref,extra_slots);
  887. end;
  888. procedure thlcgjvm.a_load_reg_ref(list: TAsmList; fromsize, tosize: tdef; register: tregister; const ref: treference);
  889. var
  890. extra_slots: longint;
  891. begin
  892. extra_slots:=prepare_stack_for_ref(list,ref,false);
  893. a_load_reg_stack(list,fromsize,register);
  894. a_load_stack_ref(list,tosize,ref,extra_slots);
  895. end;
  896. procedure thlcgjvm.a_load_reg_reg(list: TAsmList; fromsize, tosize: tdef; reg1, reg2: tregister);
  897. begin
  898. a_load_reg_stack(list,fromsize,reg1);
  899. if def2regtyp(fromsize)=R_INTREGISTER then
  900. resize_stack_int_val(list,def_cgsize(fromsize),def_cgsize(tosize),false);
  901. a_load_stack_reg(list,tosize,reg2);
  902. end;
  903. procedure thlcgjvm.a_load_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; register: tregister);
  904. var
  905. extra_slots: longint;
  906. begin
  907. extra_slots:=prepare_stack_for_ref(list,ref,false);
  908. a_load_ref_stack(list,fromsize,ref,extra_slots);
  909. if def2regtyp(fromsize)=R_INTREGISTER then
  910. resize_stack_int_val(list,def_cgsize(fromsize),def_cgsize(tosize),false);
  911. a_load_stack_reg(list,tosize,register);
  912. end;
  913. procedure thlcgjvm.a_load_ref_ref(list: TAsmList; fromsize, tosize: tdef; const sref: treference; const dref: treference);
  914. var
  915. extra_sslots,
  916. extra_dslots: longint;
  917. begin
  918. { make sure the destination reference is on top, since in the end the
  919. order has to be "destref, value" -> first create "destref, sourceref" }
  920. extra_dslots:=prepare_stack_for_ref(list,dref,false);
  921. extra_sslots:=prepare_stack_for_ref(list,sref,false);
  922. a_load_ref_stack(list,fromsize,sref,extra_sslots);
  923. if def2regtyp(fromsize)=R_INTREGISTER then
  924. resize_stack_int_val(list,def_cgsize(fromsize),def_cgsize(tosize),dref.arrayreftype<>art_none);
  925. a_load_stack_ref(list,tosize,dref,extra_dslots);
  926. end;
  927. procedure thlcgjvm.a_loadaddr_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; r: tregister);
  928. begin
  929. { only allowed for types that are not implicit pointers in Pascal (in
  930. that case, ref contains a pointer to the actual data and we simply
  931. return that pointer) }
  932. if not jvmimplicitpointertype(fromsize) then
  933. internalerror(2010120534);
  934. a_load_ref_reg(list,java_jlobject,java_jlobject,ref,r);
  935. end;
  936. procedure thlcgjvm.a_op_const_reg(list: TAsmList; Op: TOpCG; size: tdef; a: Aint; reg: TRegister);
  937. begin
  938. a_op_const_reg_reg(list,op,size,a,reg,reg);
  939. end;
  940. procedure thlcgjvm.a_op_const_reg_reg(list: TAsmList; op: TOpCg; size: tdef; a: aint; src, dst: tregister);
  941. begin
  942. a_load_reg_stack(list,size,src);
  943. a_op_const_stack(list,op,size,a);
  944. a_load_stack_reg(list,size,dst);
  945. end;
  946. procedure thlcgjvm.a_op_const_ref(list: TAsmList; Op: TOpCG; size: tdef; a: Aint; const ref: TReference);
  947. var
  948. extra_slots: longint;
  949. begin
  950. extra_slots:=prepare_stack_for_ref(list,ref,true);
  951. { TODO, here or in peepholeopt: use iinc when possible }
  952. a_load_ref_stack(list,size,ref,extra_slots);
  953. a_op_const_stack(list,op,size,a);
  954. a_load_stack_ref(list,size,ref,extra_slots);
  955. end;
  956. procedure thlcgjvm.a_op_ref_reg(list: TAsmList; Op: TOpCG; size: tdef; const ref: TReference; reg: TRegister);
  957. begin
  958. a_load_reg_stack(list,size,reg);
  959. a_op_ref_stack(list,op,size,ref);
  960. a_load_stack_reg(list,size,reg);
  961. end;
  962. procedure thlcgjvm.a_op_reg_reg_reg(list: TAsmList; op: TOpCg; size: tdef; src1, src2, dst: tregister);
  963. begin
  964. a_load_reg_stack(list,size,src2);
  965. a_op_reg_stack(list,op,size,src1);
  966. a_load_stack_reg(list,size,dst);
  967. end;
  968. procedure thlcgjvm.a_op_reg_reg(list: TAsmList; Op: TOpCG; size: tdef; reg1, reg2: TRegister);
  969. begin
  970. a_op_reg_reg_reg(list,op,size,reg1,reg2,reg2);
  971. end;
  972. procedure thlcgjvm.a_cmp_const_ref_label(list: TAsmList; size: tdef; cmp_op: topcmp; a: aint; const ref: treference; l: tasmlabel);
  973. begin
  974. if ref.base<>NR_EVAL_STACK_BASE then
  975. a_load_ref_stack(list,size,ref,prepare_stack_for_ref(list,ref,false));
  976. maybe_adjust_cmp_stackval(list,size,cmp_op);
  977. a_load_const_stack(list,size,maybe_adjust_cmp_constval(size,cmp_op,a),def2regtyp(size));
  978. a_cmp_stack_label(list,size,cmp_op,l);
  979. end;
  980. procedure thlcgjvm.a_cmp_const_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; a: aint; reg: tregister; l: tasmlabel);
  981. begin
  982. a_load_reg_stack(list,size,reg);
  983. maybe_adjust_cmp_stackval(list,size,cmp_op);
  984. a_load_const_stack(list,size,maybe_adjust_cmp_constval(size,cmp_op,a),def2regtyp(size));
  985. a_cmp_stack_label(list,size,cmp_op,l);
  986. end;
  987. procedure thlcgjvm.a_cmp_ref_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; const ref: treference; reg: tregister; l: tasmlabel);
  988. begin
  989. a_load_reg_stack(list,size,reg);
  990. maybe_adjust_cmp_stackval(list,size,cmp_op);
  991. if ref.base<>NR_EVAL_STACK_BASE then
  992. a_load_ref_stack(list,size,ref,prepare_stack_for_ref(list,ref,false))
  993. else
  994. list.concat(taicpu.op_none(a_swap));
  995. maybe_adjust_cmp_stackval(list,size,cmp_op);
  996. a_cmp_stack_label(list,size,cmp_op,l);
  997. end;
  998. procedure thlcgjvm.a_cmp_reg_ref_label(list: TAsmList; size: tdef; cmp_op: topcmp; reg: tregister; const ref: treference; l: tasmlabel);
  999. begin
  1000. if ref.base<>NR_EVAL_STACK_BASE then
  1001. a_load_ref_stack(list,size,ref,prepare_stack_for_ref(list,ref,false));
  1002. maybe_adjust_cmp_stackval(list,size,cmp_op);
  1003. a_load_reg_stack(list,size,reg);
  1004. maybe_adjust_cmp_stackval(list,size,cmp_op);
  1005. a_cmp_stack_label(list,size,cmp_op,l);
  1006. end;
  1007. procedure thlcgjvm.a_cmp_reg_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; reg1, reg2: tregister; l: tasmlabel);
  1008. begin
  1009. a_load_reg_stack(list,size,reg2);
  1010. maybe_adjust_cmp_stackval(list,size,cmp_op);
  1011. a_load_reg_stack(list,size,reg1);
  1012. maybe_adjust_cmp_stackval(list,size,cmp_op);
  1013. a_cmp_stack_label(list,size,cmp_op,l);
  1014. end;
  1015. procedure thlcgjvm.a_jmp_always(list: TAsmList; l: tasmlabel);
  1016. begin
  1017. list.concat(taicpu.op_sym(a_goto,current_asmdata.RefAsmSymbol(l.name)));
  1018. end;
  1019. procedure thlcgjvm.concatcopy_normal_array(list: TAsmList; size: tdef; const source, dest: treference);
  1020. var
  1021. procname: string;
  1022. eledef: tdef;
  1023. ndim: longint;
  1024. adddefaultlenparas: boolean;
  1025. begin
  1026. { load copy helper parameters on the stack }
  1027. a_load_ref_stack(list,java_jlobject,source,prepare_stack_for_ref(list,source,false));
  1028. a_load_ref_stack(list,java_jlobject,dest,prepare_stack_for_ref(list,dest,false));
  1029. { call copy helper }
  1030. eledef:=tarraydef(size).elementdef;
  1031. ndim:=1;
  1032. adddefaultlenparas:=true;
  1033. case eledef.typ of
  1034. orddef:
  1035. begin
  1036. case torddef(eledef).ordtype of
  1037. pasbool8,s8bit,u8bit,bool8bit,uchar,
  1038. s16bit,u16bit,bool16bit,pasbool16,
  1039. uwidechar,
  1040. s32bit,u32bit,bool32bit,pasbool32,
  1041. s64bit,u64bit,bool64bit,pasbool64,scurrency:
  1042. procname:='FPC_COPY_SHALLOW_ARRAY'
  1043. else
  1044. internalerror(2011020504);
  1045. end;
  1046. end;
  1047. arraydef:
  1048. begin
  1049. { call fpc_setlength_dynarr_multidim with deepcopy=true, and extra
  1050. parameters }
  1051. while (eledef.typ=arraydef) and
  1052. not is_dynamic_array(eledef) do
  1053. begin
  1054. eledef:=tarraydef(eledef).elementdef;
  1055. inc(ndim)
  1056. end;
  1057. if (ndim=1) then
  1058. procname:='FPC_COPY_SHALLOW_ARRAY'
  1059. else
  1060. begin
  1061. { deepcopy=true }
  1062. a_load_const_stack(list,pasbool8type,1,R_INTREGISTER);
  1063. { ndim }
  1064. a_load_const_stack(list,s32inttype,ndim,R_INTREGISTER);
  1065. { eletype }
  1066. a_load_const_stack(list,cwidechartype,ord(jvmarrtype_setlength(eledef)),R_INTREGISTER);
  1067. adddefaultlenparas:=false;
  1068. procname:='FPC_SETLENGTH_DYNARR_MULTIDIM';
  1069. end;
  1070. end;
  1071. recorddef:
  1072. procname:='FPC_COPY_JRECORD_ARRAY';
  1073. procvardef:
  1074. if tprocvardef(eledef).is_addressonly then
  1075. procname:='FPC_COPY_SHALLOW_ARRAY'
  1076. else
  1077. procname:='FPC_COPY_JPROCVAR_ARRAY';
  1078. setdef:
  1079. if tsetdef(eledef).elementdef.typ=enumdef then
  1080. procname:='FPC_COPY_JENUMSET_ARRAY'
  1081. else
  1082. procname:='FPC_COPY_JBITSET_ARRAY';
  1083. floatdef:
  1084. procname:='FPC_COPY_SHALLOW_ARRAY';
  1085. stringdef:
  1086. if is_shortstring(eledef) then
  1087. procname:='FPC_COPY_JSHORTSTRING_ARRAY'
  1088. else
  1089. procname:='FPC_COPY_SHALLOW_ARRAY';
  1090. variantdef:
  1091. begin
  1092. {$ifndef nounsupported}
  1093. procname:='FPC_COPY_SHALLOW_ARRAY';
  1094. {$else}
  1095. { todo: make a deep copy via clone... }
  1096. internalerror(2011020505);
  1097. {$endif}
  1098. end;
  1099. else
  1100. procname:='FPC_COPY_SHALLOW_ARRAY';
  1101. end;
  1102. if adddefaultlenparas then
  1103. begin
  1104. { -1, -1 means "copy entire array" }
  1105. a_load_const_stack(list,s32inttype,-1,R_INTREGISTER);
  1106. a_load_const_stack(list,s32inttype,-1,R_INTREGISTER);
  1107. end;
  1108. g_call_system_proc(list,procname);
  1109. if ndim=1 then
  1110. begin
  1111. decstack(list,2);
  1112. if adddefaultlenparas then
  1113. decstack(list,2);
  1114. end
  1115. else
  1116. begin
  1117. decstack(list,4);
  1118. { pop return value, must be the same as dest }
  1119. list.concat(taicpu.op_none(a_pop));
  1120. decstack(list,1);
  1121. end;
  1122. end;
  1123. procedure thlcgjvm.concatcopy_record(list: TAsmList; size: tdef; const source, dest: treference);
  1124. var
  1125. srsym: tsym;
  1126. pd: tprocdef;
  1127. begin
  1128. { self }
  1129. a_load_ref_stack(list,size,source,prepare_stack_for_ref(list,source,false));
  1130. { result }
  1131. a_load_ref_stack(list,size,dest,prepare_stack_for_ref(list,dest,false));
  1132. { call fpcDeepCopy helper }
  1133. srsym:=search_struct_member(tabstractrecorddef(size),'FPCDEEPCOPY');
  1134. if not assigned(srsym) or
  1135. (srsym.typ<>procsym) then
  1136. Message1(cg_f_unknown_compilerproc,size.typename+'.fpcDeepCopy');
  1137. pd:=tprocdef(tprocsym(srsym).procdeflist[0]);
  1138. a_call_name(list,pd,pd.mangledname,false);
  1139. { both parameters are removed, no function result }
  1140. decstack(list,2);
  1141. end;
  1142. procedure thlcgjvm.concatcopy_set(list: TAsmList; size: tdef; const source, dest: treference);
  1143. begin
  1144. a_load_ref_stack(list,size,source,prepare_stack_for_ref(list,source,false));
  1145. a_load_ref_stack(list,size,dest,prepare_stack_for_ref(list,dest,false));
  1146. { call set copy helper }
  1147. if tsetdef(size).elementdef.typ=enumdef then
  1148. g_call_system_proc(list,'fpc_enumset_copy')
  1149. else
  1150. g_call_system_proc(list,'fpc_bitset_copy');
  1151. { both parameters are removed, no function result }
  1152. decstack(list,2);
  1153. end;
  1154. procedure thlcgjvm.concatcopy_shortstring(list: TAsmList; size: tdef; const source, dest: treference);
  1155. var
  1156. srsym: tsym;
  1157. pd: tprocdef;
  1158. begin
  1159. { self }
  1160. a_load_ref_stack(list,size,source,prepare_stack_for_ref(list,source,false));
  1161. { result }
  1162. a_load_ref_stack(list,size,dest,prepare_stack_for_ref(list,dest,false));
  1163. { call fpcDeepCopy helper }
  1164. srsym:=search_struct_member(java_shortstring,'FPCDEEPCOPY');
  1165. if not assigned(srsym) or
  1166. (srsym.typ<>procsym) then
  1167. Message1(cg_f_unknown_compilerproc,'ShortstringClass.FpcDeepCopy');
  1168. pd:=tprocdef(tprocsym(srsym).procdeflist[0]);
  1169. a_call_name(list,pd,pd.mangledname,false);
  1170. { both parameters are removed, no function result }
  1171. decstack(list,2);
  1172. end;
  1173. procedure thlcgjvm.g_concatcopy(list: TAsmList; size: tdef; const source, dest: treference);
  1174. var
  1175. handled: boolean;
  1176. begin
  1177. handled:=false;
  1178. case size.typ of
  1179. arraydef:
  1180. begin
  1181. if not is_dynamic_array(size) then
  1182. begin
  1183. concatcopy_normal_array(list,size,source,dest);
  1184. handled:=true;
  1185. end;
  1186. end;
  1187. recorddef:
  1188. begin
  1189. concatcopy_record(list,size,source,dest);
  1190. handled:=true;
  1191. end;
  1192. setdef:
  1193. begin
  1194. concatcopy_set(list,size,source,dest);
  1195. handled:=true;
  1196. end;
  1197. stringdef:
  1198. begin
  1199. if is_shortstring(size) then
  1200. begin
  1201. concatcopy_shortstring(list,size,source,dest);
  1202. handled:=true;
  1203. end;
  1204. end;
  1205. procvardef:
  1206. begin
  1207. if not tprocvardef(size).is_addressonly then
  1208. begin
  1209. concatcopy_record(list,tprocvardef(size).classdef,source,dest);
  1210. handled:=true;
  1211. end;
  1212. end;
  1213. end;
  1214. if not handled then
  1215. inherited;
  1216. end;
  1217. procedure thlcgjvm.g_copyshortstring(list: TAsmList; const source, dest: treference; strdef: tstringdef);
  1218. begin
  1219. concatcopy_shortstring(list,strdef,source,dest);
  1220. end;
  1221. procedure thlcgjvm.a_loadfpu_ref_ref(list: TAsmList; fromsize, tosize: tdef; const ref1, ref2: treference);
  1222. var
  1223. dstack_slots: longint;
  1224. begin
  1225. dstack_slots:=prepare_stack_for_ref(list,ref2,false);
  1226. a_load_ref_stack(list,fromsize,ref1,prepare_stack_for_ref(list,ref1,false));
  1227. resizestackfpuval(list,def_cgsize(fromsize),def_cgsize(tosize));
  1228. a_load_stack_ref(list,tosize,ref2,dstack_slots);
  1229. end;
  1230. procedure thlcgjvm.a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; reg: tregister);
  1231. begin
  1232. a_load_ref_stack(list,fromsize,ref,prepare_stack_for_ref(list,ref,false));
  1233. resizestackfpuval(list,def_cgsize(fromsize),def_cgsize(tosize));
  1234. a_load_stack_reg(list,tosize,reg);
  1235. end;
  1236. procedure thlcgjvm.a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tdef; reg: tregister; const ref: treference);
  1237. var
  1238. dstack_slots: longint;
  1239. begin
  1240. dstack_slots:=prepare_stack_for_ref(list,ref,false);
  1241. a_load_reg_stack(list,fromsize,reg);
  1242. resizestackfpuval(list,def_cgsize(fromsize),def_cgsize(tosize));
  1243. a_load_stack_ref(list,tosize,ref,dstack_slots);
  1244. end;
  1245. procedure thlcgjvm.a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tdef; reg1, reg2: tregister);
  1246. begin
  1247. a_load_reg_stack(list,fromsize,reg1);
  1248. resizestackfpuval(list,def_cgsize(fromsize),def_cgsize(tosize));
  1249. a_load_stack_reg(list,tosize,reg2);
  1250. end;
  1251. procedure thlcgjvm.g_proc_entry(list: TAsmList; localsize: longint; nostackframe: boolean);
  1252. begin
  1253. { the localsize is based on tg.lasttemp -> already in terms of stack
  1254. slots rather than bytes }
  1255. list.concat(tai_directive.Create(asd_jlimit,'locals '+tostr(localsize)));
  1256. { we insert the unit initialisation code afterwards in the proginit code,
  1257. and it uses one stack slot }
  1258. if (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1259. fmaxevalstackheight:=max(1,fmaxevalstackheight);
  1260. list.concat(tai_directive.Create(asd_jlimit,'stack '+tostr(fmaxevalstackheight)));
  1261. end;
  1262. procedure thlcgjvm.g_proc_exit(list: TAsmList; parasize: longint; nostackframe: boolean);
  1263. var
  1264. retdef: tdef;
  1265. opc: tasmop;
  1266. begin
  1267. if current_procinfo.procdef.proctypeoption in [potype_constructor,potype_class_constructor] then
  1268. retdef:=voidtype
  1269. else
  1270. retdef:=current_procinfo.procdef.returndef;
  1271. case retdef.typ of
  1272. orddef:
  1273. case torddef(retdef).ordtype of
  1274. uvoid:
  1275. opc:=a_return;
  1276. s64bit,
  1277. u64bit,
  1278. scurrency:
  1279. opc:=a_lreturn;
  1280. else
  1281. opc:=a_ireturn;
  1282. end;
  1283. setdef:
  1284. opc:=a_areturn;
  1285. floatdef:
  1286. case tfloatdef(retdef).floattype of
  1287. s32real:
  1288. opc:=a_freturn;
  1289. s64real:
  1290. opc:=a_dreturn;
  1291. else
  1292. internalerror(2011010213);
  1293. end;
  1294. else
  1295. opc:=a_areturn;
  1296. end;
  1297. list.concat(taicpu.op_none(opc));
  1298. end;
  1299. procedure thlcgjvm.gen_load_return_value(list: TAsmList);
  1300. begin
  1301. { constructors don't return anything in the jvm }
  1302. if current_procinfo.procdef.proctypeoption in [potype_constructor,potype_class_constructor] then
  1303. exit;
  1304. inherited gen_load_return_value(list);
  1305. end;
  1306. procedure thlcgjvm.record_generated_code_for_procdef(pd: tprocdef; code, data: TAsmList);
  1307. begin
  1308. { add something to the al_procedures list as well, because if all al_*
  1309. lists are empty, the assembler writer isn't called }
  1310. if not code.empty and
  1311. current_asmdata.asmlists[al_procedures].empty then
  1312. current_asmdata.asmlists[al_procedures].concat(tai_align.Create(4));
  1313. pd.exprasmlist:=TAsmList.create;
  1314. pd.exprasmlist.concatlist(code);
  1315. if assigned(data) and
  1316. not data.empty then
  1317. internalerror(2010122801);
  1318. end;
  1319. procedure thlcgjvm.g_incrrefcount(list: TAsmList; t: tdef; const ref: treference);
  1320. begin
  1321. // do nothing
  1322. end;
  1323. procedure thlcgjvm.g_decrrefcount(list: TAsmList; t: tdef; const ref: treference);
  1324. begin
  1325. // do nothing
  1326. end;
  1327. procedure thlcgjvm.g_array_rtti_helper(list: TAsmList; t: tdef; const ref: treference; const highloc: tlocation; const name: string);
  1328. var
  1329. normaldim: longint;
  1330. eleref: treference;
  1331. begin
  1332. { only in case of initialisation, we have to set all elements to "empty" }
  1333. if name<>'FPC_INITIALIZE_ARRAY' then
  1334. exit;
  1335. { put array on the stack }
  1336. a_load_ref_stack(list,java_jlobject,ref,prepare_stack_for_ref(list,ref,false));
  1337. { in case it's an open array whose elements are regular arrays, put the
  1338. dimension of the regular arrays on the stack (otherwise pass 0) }
  1339. normaldim:=0;
  1340. while (t.typ=arraydef) and
  1341. not is_dynamic_array(t) do
  1342. begin
  1343. inc(normaldim);
  1344. t:=tarraydef(t).elementdef;
  1345. end;
  1346. a_load_const_stack(list,s32inttype,normaldim,R_INTREGISTER);
  1347. { highloc is invalid, the length is part of the array in Java }
  1348. if is_wide_or_unicode_string(t) then
  1349. g_call_system_proc(list,'fpc_initialize_array_unicodestring')
  1350. else if is_ansistring(t) then
  1351. g_call_system_proc(list,'fpc_initialize_array_ansistring')
  1352. else if is_dynamic_array(t) then
  1353. g_call_system_proc(list,'fpc_initialize_array_dynarr')
  1354. else if is_record(t) or
  1355. (t.typ=setdef) then
  1356. begin
  1357. tg.gethltemp(list,t,t.size,tt_persistent,eleref);
  1358. a_load_ref_stack(list,t,eleref,prepare_stack_for_ref(list,eleref,false));
  1359. if is_record(t) then
  1360. g_call_system_proc(list,'fpc_initialize_array_record')
  1361. else if tsetdef(t).elementdef.typ=enumdef then
  1362. g_call_system_proc(list,'fpc_initialize_array_enumset')
  1363. else
  1364. g_call_system_proc(list,'fpc_initialize_array_bitset');
  1365. tg.ungettemp(list,eleref);
  1366. end
  1367. else
  1368. internalerror(2011031901);
  1369. end;
  1370. procedure thlcgjvm.g_initialize(list: TAsmList; t: tdef; const ref: treference);
  1371. var
  1372. dummyloc: tlocation;
  1373. sym: tsym;
  1374. pd: tprocdef;
  1375. begin
  1376. if (t.typ=arraydef) and
  1377. not is_dynamic_array(t) then
  1378. begin
  1379. dummyloc.loc:=LOC_INVALID;
  1380. g_array_rtti_helper(list,tarraydef(t).elementdef,ref,dummyloc,'FPC_INITIALIZE_ARRAY')
  1381. end
  1382. else if is_record(t) then
  1383. begin
  1384. { call the fpcInitializeRec method }
  1385. sym:=tsym(trecorddef(t).symtable.find('FPCINITIALIZEREC'));
  1386. if assigned(sym) and
  1387. (sym.typ=procsym) then
  1388. begin
  1389. if tprocsym(sym).procdeflist.Count<>1 then
  1390. internalerror(2011071713);
  1391. pd:=tprocdef(tprocsym(sym).procdeflist[0]);
  1392. end;
  1393. a_load_ref_stack(list,java_jlobject,ref,prepare_stack_for_ref(list,ref,false));
  1394. a_call_name(list,pd,pd.mangledname,false);
  1395. { parameter removed, no result }
  1396. decstack(list,1);
  1397. end
  1398. else
  1399. a_load_const_ref(list,t,0,ref);
  1400. end;
  1401. procedure thlcgjvm.g_finalize(list: TAsmList; t: tdef; const ref: treference);
  1402. begin
  1403. // do nothing
  1404. end;
  1405. procedure thlcgjvm.location_get_data_ref(list: TAsmList; def: tdef; const l: tlocation; var ref: treference; loadref: boolean; alignment: longint);
  1406. var
  1407. tmploc: tlocation;
  1408. begin
  1409. { This routine is a combination of a generalised a_loadaddr_ref_reg()
  1410. that also works for addresses in registers (in case loadref is false)
  1411. and of a_load_ref_reg (in case loadref is true). It is used for
  1412. a) getting the address of managed var/out parameters
  1413. b) getting to the actual data of value types that are passed by
  1414. reference by the compiler (and then get a local copy at the caller
  1415. side). Normally, depending on whether this reference is passed in a
  1416. register or reference, we either need a reference with that register
  1417. as base or load the address in that reference and use that as a new
  1418. base.
  1419. Since the JVM cannot take the address of anything, all
  1420. "pass-by-reference" value parameters (which are always aggregate types)
  1421. are already simply the implicit pointer to the data (since arrays,
  1422. records, etc are already internally implicit pointers). This means
  1423. that if "loadref" is true, we must simply return this implicit pointer.
  1424. If it is false, we are supposed the take the address of this implicit
  1425. pointer, which is not possible.
  1426. However, managed types are also implicit pointers in Pascal, so in that
  1427. case "taking the address" again consists of simply returning the
  1428. implicit pointer/current value (in case of a var/out parameter, this
  1429. value is stored inside an array).
  1430. }
  1431. if not loadref then
  1432. begin
  1433. if not is_managed_type(def) then
  1434. internalerror(2011020601);
  1435. tmploc:=l;
  1436. end
  1437. else
  1438. begin
  1439. if not jvmimplicitpointertype(def) then
  1440. begin
  1441. { passed by reference in array of single element; l contains the
  1442. base address of the array }
  1443. location_reset_ref(tmploc,LOC_REFERENCE,OS_ADDR,4);
  1444. reference_reset_base(tmploc.reference,getaddressregister(list,java_jlobject),0,4);
  1445. tmploc.reference.arrayreftype:=art_indexconst;
  1446. tmploc.reference.indexoffset:=0;
  1447. a_load_loc_reg(list,java_jlobject,java_jlobject,l,tmploc.reference.base);
  1448. end
  1449. else
  1450. tmploc:=l;
  1451. end;
  1452. case tmploc.loc of
  1453. LOC_REGISTER,
  1454. LOC_CREGISTER :
  1455. begin
  1456. { the implicit pointer is in a register and has to be in a
  1457. reference -> create a reference and put it there }
  1458. location_force_mem(list,tmploc,java_jlobject);
  1459. ref:=tmploc.reference;
  1460. end;
  1461. LOC_REFERENCE,
  1462. LOC_CREFERENCE :
  1463. begin
  1464. ref:=tmploc.reference;
  1465. end;
  1466. else
  1467. internalerror(2011020603);
  1468. end;
  1469. end;
  1470. procedure thlcgjvm.g_copyvaluepara_openarray(list: TAsmList; const ref: treference; const lenloc: tlocation; arrdef: tarraydef; destreg: tregister);
  1471. var
  1472. localref: treference;
  1473. arrloc: tlocation;
  1474. stackslots: longint;
  1475. begin
  1476. { temporary reference for passing to concatcopy }
  1477. tg.gethltemp(list,java_jlobject,java_jlobject.size,tt_persistent,localref);
  1478. stackslots:=prepare_stack_for_ref(list,localref,false);
  1479. { create the local copy of the array (lenloc is invalid, get length
  1480. directly from the array) }
  1481. location_reset_ref(arrloc,LOC_REFERENCE,OS_ADDR,sizeof(pint));
  1482. arrloc.reference:=ref;
  1483. g_getarraylen(list,arrloc);
  1484. g_newarray(list,arrdef,1);
  1485. a_load_stack_ref(list,java_jlobject,localref,stackslots);
  1486. { copy the source array to the destination }
  1487. g_concatcopy(list,arrdef,ref,localref);
  1488. { and put the array pointer in the register as expected by the caller }
  1489. a_load_ref_reg(list,java_jlobject,java_jlobject,localref,destreg);
  1490. end;
  1491. procedure thlcgjvm.g_releasevaluepara_openarray(list: TAsmList; arrdef: tarraydef; const l: tlocation);
  1492. begin
  1493. // do nothing, long live garbage collection!
  1494. end;
  1495. procedure thlcgjvm.gen_initialize_code(list: TAsmList);
  1496. var
  1497. ref: treference;
  1498. begin
  1499. { create globals with wrapped types such as arrays/records }
  1500. case current_procinfo.procdef.proctypeoption of
  1501. potype_unitinit:
  1502. begin
  1503. reference_reset_base(ref,NR_NO,0,1);
  1504. if assigned(current_module.globalsymtable) then
  1505. allocate_implicit_structs_for_st_with_base_ref(list,current_module.globalsymtable,ref,staticvarsym);
  1506. allocate_implicit_structs_for_st_with_base_ref(list,current_module.localsymtable,ref,staticvarsym);
  1507. end;
  1508. potype_class_constructor:
  1509. begin
  1510. { also initialise local variables, if any }
  1511. inherited;
  1512. { initialise class fields }
  1513. reference_reset_base(ref,NR_NO,0,1);
  1514. allocate_implicit_structs_for_st_with_base_ref(list,tabstractrecorddef(current_procinfo.procdef.owner.defowner).symtable,ref,staticvarsym);
  1515. end
  1516. else
  1517. inherited
  1518. end;
  1519. end;
  1520. procedure thlcgjvm.gen_entry_code(list: TAsmList);
  1521. begin
  1522. list.concat(Tai_force_line.Create);
  1523. end;
  1524. procedure thlcgjvm.gen_exit_code(list: TAsmList);
  1525. begin
  1526. { nothing }
  1527. end;
  1528. procedure thlcgjvm.a_load_stack_reg(list: TAsmList; size: tdef; reg: tregister);
  1529. var
  1530. opc: tasmop;
  1531. finishandval: aint;
  1532. begin
  1533. opc:=loadstoreopc(size,false,false,finishandval);
  1534. list.concat(taicpu.op_reg(opc,reg));
  1535. { avoid problems with getting the size of an open array etc }
  1536. if jvmimplicitpointertype(size) then
  1537. size:=java_jlobject;
  1538. decstack(list,1+ord(size.size>4));
  1539. end;
  1540. procedure thlcgjvm.a_load_stack_ref(list: TAsmList; size: tdef; const ref: treference; extra_slots: longint);
  1541. var
  1542. opc: tasmop;
  1543. finishandval: aint;
  1544. begin
  1545. { fake location that indicates the value has to remain on the stack }
  1546. if ref.base=NR_EVAL_STACK_BASE then
  1547. exit;
  1548. opc:=loadstoreopcref(size,false,ref,finishandval);
  1549. if ref.arrayreftype=art_none then
  1550. list.concat(taicpu.op_ref(opc,ref))
  1551. else
  1552. list.concat(taicpu.op_none(opc));
  1553. { avoid problems with getting the size of an open array etc }
  1554. if jvmimplicitpointertype(size) then
  1555. size:=java_jlobject;
  1556. decstack(list,1+ord(size.size>4)+extra_slots);
  1557. end;
  1558. procedure thlcgjvm.a_load_reg_stack(list: TAsmList; size: tdef; reg: tregister);
  1559. var
  1560. opc: tasmop;
  1561. finishandval: aint;
  1562. begin
  1563. opc:=loadstoreopc(size,true,false,finishandval);
  1564. list.concat(taicpu.op_reg(opc,reg));
  1565. { avoid problems with getting the size of an open array etc }
  1566. if jvmimplicitpointertype(size) then
  1567. size:=java_jlobject;
  1568. incstack(list,1+ord(size.size>4));
  1569. if finishandval<>-1 then
  1570. a_op_const_stack(list,OP_AND,size,finishandval);
  1571. end;
  1572. procedure thlcgjvm.a_load_ref_stack(list: TAsmList; size: tdef; const ref: treference; extra_slots: longint);
  1573. var
  1574. opc: tasmop;
  1575. finishandval: aint;
  1576. begin
  1577. { fake location that indicates the value is already on the stack? }
  1578. if (ref.base=NR_EVAL_STACK_BASE) then
  1579. exit;
  1580. opc:=loadstoreopcref(size,true,ref,finishandval);
  1581. if ref.arrayreftype=art_none then
  1582. list.concat(taicpu.op_ref(opc,ref))
  1583. else
  1584. list.concat(taicpu.op_none(opc));
  1585. { avoid problems with getting the size of an open array etc }
  1586. if jvmimplicitpointertype(size) then
  1587. size:=java_jlobject;
  1588. incstack(list,1+ord(size.size>4)-extra_slots);
  1589. if finishandval<>-1 then
  1590. a_op_const_stack(list,OP_AND,size,finishandval);
  1591. end;
  1592. function thlcgjvm.loadstoreopcref(def: tdef; isload: boolean; const ref: treference; out finishandval: aint): tasmop;
  1593. const
  1594. { isload static }
  1595. getputopc: array[boolean,boolean] of tasmop =
  1596. ((a_putfield,a_putstatic),
  1597. (a_getfield,a_getstatic));
  1598. begin
  1599. if assigned(ref.symbol) then
  1600. begin
  1601. { -> either a global (static) field, or a regular field. If a regular
  1602. field, then ref.base contains the self pointer, otherwise
  1603. ref.base=NR_NO. In both cases, the symbol contains all other
  1604. information (combined field name and type descriptor) }
  1605. result:=getputopc[isload,ref.base=NR_NO];
  1606. finishandval:=-1;
  1607. { erase sign extension for byte/smallint loads }
  1608. if (def2regtyp(def)=R_INTREGISTER) and
  1609. not is_signed(def) then
  1610. case def.size of
  1611. 1: finishandval:=255;
  1612. 2: finishandval:=65535;
  1613. end;
  1614. end
  1615. else
  1616. result:=loadstoreopc(def,isload,ref.arrayreftype<>art_none,finishandval);
  1617. end;
  1618. function thlcgjvm.loadstoreopc(def: tdef; isload, isarray: boolean; out finishandval: aint): tasmop;
  1619. var
  1620. size: longint;
  1621. begin
  1622. finishandval:=-1;
  1623. case def2regtyp(def) of
  1624. R_INTREGISTER:
  1625. begin
  1626. size:=def.size;
  1627. if not isarray then
  1628. begin
  1629. case size of
  1630. 1,2,3,4:
  1631. if isload then
  1632. result:=a_iload
  1633. else
  1634. result:=a_istore;
  1635. 8:
  1636. if isload then
  1637. result:=a_lload
  1638. else
  1639. result:=a_lstore;
  1640. else
  1641. internalerror(2011032814);
  1642. end;
  1643. end
  1644. { array }
  1645. else if isload then
  1646. begin
  1647. case size of
  1648. 1:
  1649. begin
  1650. result:=a_baload;
  1651. if not is_signed(def) then
  1652. finishandval:=255;
  1653. end;
  1654. 2:
  1655. begin
  1656. if is_widechar(def) then
  1657. result:=a_caload
  1658. else
  1659. begin
  1660. result:=a_saload;
  1661. { if we'd treat arrays of word as "array of widechar" we
  1662. could use a_caload, but that would make for even more
  1663. awkward interfacing with external Java code }
  1664. if not is_signed(def) then
  1665. finishandval:=65535;
  1666. end;
  1667. end;
  1668. 4: result:=a_iaload;
  1669. 8: result:=a_laload;
  1670. else
  1671. internalerror(2010120503);
  1672. end
  1673. end
  1674. else
  1675. begin
  1676. case size of
  1677. 1: result:=a_bastore;
  1678. 2: if not is_widechar(def) then
  1679. result:=a_sastore
  1680. else
  1681. result:=a_castore;
  1682. 4: result:=a_iastore;
  1683. 8: result:=a_lastore;
  1684. else
  1685. internalerror(2010120508);
  1686. end
  1687. end
  1688. end;
  1689. R_ADDRESSREGISTER:
  1690. if not isarray then
  1691. if isload then
  1692. result:=a_aload
  1693. else
  1694. result:=a_astore
  1695. else if isload then
  1696. result:=a_aaload
  1697. else
  1698. result:=a_aastore;
  1699. R_FPUREGISTER:
  1700. begin
  1701. case tfloatdef(def).floattype of
  1702. s32real:
  1703. if not isarray then
  1704. if isload then
  1705. result:=a_fload
  1706. else
  1707. result:=a_fstore
  1708. else if isload then
  1709. result:=a_faload
  1710. else
  1711. result:=a_fastore;
  1712. s64real:
  1713. if not isarray then
  1714. if isload then
  1715. result:=a_dload
  1716. else
  1717. result:=a_dstore
  1718. else if isload then
  1719. result:=a_daload
  1720. else
  1721. result:=a_dastore;
  1722. else
  1723. internalerror(2010120504);
  1724. end
  1725. end
  1726. else
  1727. internalerror(2010120502);
  1728. end;
  1729. end;
  1730. procedure thlcgjvm.resize_stack_int_val(list: TAsmList; fromsize, tosize: tcgsize; forarraystore: boolean);
  1731. begin
  1732. if fromsize in [OS_S64,OS_64] then
  1733. begin
  1734. if not(tosize in [OS_S64,OS_64]) then
  1735. begin
  1736. { truncate }
  1737. list.concat(taicpu.op_none(a_l2i));
  1738. decstack(list,1);
  1739. end;
  1740. end
  1741. else if tosize in [OS_S64,OS_64] then
  1742. begin
  1743. { extend }
  1744. list.concat(taicpu.op_none(a_i2l));
  1745. incstack(list,1);
  1746. { if it was an unsigned 32 bit value, remove sign extension }
  1747. if fromsize=OS_32 then
  1748. a_op_const_stack(list,OP_AND,s64inttype,cardinal($ffffffff));
  1749. end;
  1750. { if the value is immediately stored to an array afterwards, the store
  1751. instruction will properly truncate the value; otherwise we may need
  1752. additional truncation, except for 64/32 bit conversions, which are
  1753. already handled above }
  1754. if not forarraystore and
  1755. (not(fromsize in [OS_S64,OS_64,OS_32,OS_S32]) or
  1756. not(tosize in [OS_S64,OS_64,OS_32,OS_S32])) and
  1757. (tcgsize2size[fromsize]>tcgsize2size[tosize]) or
  1758. ((tcgsize2size[fromsize]=tcgsize2size[tosize]) and
  1759. (fromsize<>tosize)) or
  1760. { needs to mask out the sign in the top 16 bits }
  1761. ((fromsize=OS_S8) and
  1762. (tosize=OS_16)) then
  1763. case tosize of
  1764. OS_8:
  1765. a_op_const_stack(list,OP_AND,s32inttype,255);
  1766. OS_S8:
  1767. list.concat(taicpu.op_none(a_i2b));
  1768. OS_16:
  1769. list.concat(taicpu.op_none(a_i2c));
  1770. OS_S16:
  1771. list.concat(taicpu.op_none(a_i2s));
  1772. end;
  1773. end;
  1774. procedure thlcgjvm.allocate_implicit_struct_with_base_ref(list: TAsmList; vs: tabstractvarsym; ref: treference);
  1775. var
  1776. tmpref: treference;
  1777. begin
  1778. ref.symbol:=current_asmdata.RefAsmSymbol(vs.mangledname);
  1779. tg.gethltemp(list,vs.vardef,vs.vardef.size,tt_persistent,tmpref);
  1780. { only copy the reference, not the actual data }
  1781. a_load_ref_ref(list,java_jlobject,java_jlobject,tmpref,ref);
  1782. { remains live since there's still a reference to the created
  1783. entity }
  1784. tg.ungettemp(list,tmpref);
  1785. end;
  1786. procedure thlcgjvm.allocate_implicit_structs_for_st_with_base_ref(list: TAsmList; st: tsymtable; const ref: treference; allocvartyp: tsymtyp);
  1787. var
  1788. vs: tabstractvarsym;
  1789. def: tdef;
  1790. i: longint;
  1791. begin
  1792. for i:=0 to st.symlist.count-1 do
  1793. begin
  1794. if (tsym(st.symlist[i]).typ<>allocvartyp) then
  1795. continue;
  1796. vs:=tabstractvarsym(st.symlist[i]);
  1797. if sp_static in vs.symoptions then
  1798. continue;
  1799. { vo_is_external and vo_has_local_copy means a staticvarsym that is
  1800. alias for a constsym, whose sole purpose is for allocating and
  1801. intialising the constant }
  1802. if [vo_is_external,vo_has_local_copy]*vs.varoptions=[vo_is_external] then
  1803. continue;
  1804. if not jvmimplicitpointertype(vs.vardef) then
  1805. continue;
  1806. allocate_implicit_struct_with_base_ref(list,vs,ref);
  1807. end;
  1808. { process symtables of routines part of this symtable (for local typed
  1809. constants) }
  1810. if allocvartyp=staticvarsym then
  1811. begin
  1812. for i:=0 to st.deflist.count-1 do
  1813. begin
  1814. def:=tdef(st.deflist[i]);
  1815. { the unit symtable also contains the methods of classes defined
  1816. in that unit -> skip them when processing the unit itself.
  1817. Localst is not assigned for the main program code.
  1818. Localst can be the same as st in case of unit init code. }
  1819. if (def.typ<>procdef) or
  1820. (def.owner<>st) or
  1821. not assigned(tprocdef(def).localst) or
  1822. (tprocdef(def).localst=st) then
  1823. continue;
  1824. allocate_implicit_structs_for_st_with_base_ref(list,tprocdef(def).localst,ref,allocvartyp);
  1825. end;
  1826. end;
  1827. end;
  1828. procedure thlcgjvm.gen_initialize_fields_code(list: TAsmList);
  1829. var
  1830. selfpara: tparavarsym;
  1831. selfreg: tregister;
  1832. ref: treference;
  1833. obj: tabstractrecorddef;
  1834. i: longint;
  1835. needinit: boolean;
  1836. begin
  1837. obj:=tabstractrecorddef(current_procinfo.procdef.owner.defowner);
  1838. { check whether there are any fields that need initialisation }
  1839. needinit:=false;
  1840. for i:=0 to obj.symtable.symlist.count-1 do
  1841. if (tsym(obj.symtable.symlist[i]).typ=fieldvarsym) and
  1842. jvmimplicitpointertype(tfieldvarsym(obj.symtable.symlist[i]).vardef) then
  1843. begin
  1844. needinit:=true;
  1845. break;
  1846. end;
  1847. if not needinit then
  1848. exit;
  1849. selfpara:=tparavarsym(current_procinfo.procdef.parast.find('self'));
  1850. if not assigned(selfpara) then
  1851. internalerror(2011033001);
  1852. selfreg:=getaddressregister(list,selfpara.vardef);
  1853. a_load_loc_reg(list,obj,obj,selfpara.localloc,selfreg);
  1854. reference_reset_base(ref,selfreg,0,1);
  1855. allocate_implicit_structs_for_st_with_base_ref(list,obj.symtable,ref,fieldvarsym);
  1856. end;
  1857. procedure thlcgjvm.resizestackfpuval(list: TAsmList; fromsize, tosize: tcgsize);
  1858. begin
  1859. if (fromsize=OS_F32) and
  1860. (tosize=OS_F64) then
  1861. begin
  1862. list.concat(taicpu.op_none(a_f2d));
  1863. incstack(list,1);
  1864. end
  1865. else if (fromsize=OS_F64) and
  1866. (tosize=OS_F32) then
  1867. begin
  1868. list.concat(taicpu.op_none(a_d2f));
  1869. decstack(list,1);
  1870. end;
  1871. end;
  1872. procedure thlcgjvm.maybepreparedivu32(list: TAsmList; var op: topcg; size: tdef; out isdivu32: boolean);
  1873. begin
  1874. if (op=OP_DIV) and
  1875. (def_cgsize(size)=OS_32) then
  1876. begin
  1877. { needs zero-extension to 64 bit, because the JVM only supports
  1878. signed divisions }
  1879. resize_stack_int_val(list,OS_32,OS_S64,false);
  1880. op:=OP_IDIV;
  1881. isdivu32:=true;
  1882. end
  1883. else
  1884. isdivu32:=false;
  1885. end;
  1886. procedure thlcgjvm.a_call_name_intern(list: TAsmList; pd: tprocdef; const s: TSymStr; inheritedcall: boolean);
  1887. var
  1888. opc: tasmop;
  1889. begin
  1890. {
  1891. invoke types:
  1892. * invokeinterface: call method from an interface (must also specify
  1893. number of parameters in terms of stack slot count!)
  1894. * invokespecial: invoke a constructor, method in a superclass,
  1895. or private instance method
  1896. * invokestatic: invoke a class method (private or not)
  1897. * invokevirtual: invoke a regular method
  1898. }
  1899. case pd.owner.symtabletype of
  1900. globalsymtable,
  1901. staticsymtable,
  1902. localsymtable:
  1903. { regular and nested procedures are turned into static methods }
  1904. opc:=a_invokestatic;
  1905. objectsymtable:
  1906. begin
  1907. case tobjectdef(pd.owner.defowner).objecttype of
  1908. odt_javaclass:
  1909. begin
  1910. if (po_classmethod in pd.procoptions) then
  1911. opc:=a_invokestatic
  1912. else if (pd.visibility=vis_private) or
  1913. (pd.proctypeoption=potype_constructor) or
  1914. inheritedcall then
  1915. opc:=a_invokespecial
  1916. else
  1917. opc:=a_invokevirtual;
  1918. end;
  1919. odt_interfacejava:
  1920. { static interface methods are not allowed }
  1921. opc:=a_invokeinterface;
  1922. else
  1923. internalerror(2010122601);
  1924. end;
  1925. end;
  1926. recordsymtable:
  1927. begin
  1928. if (po_staticmethod in pd.procoptions) then
  1929. opc:=a_invokestatic
  1930. else if (pd.visibility=vis_private) or
  1931. (pd.proctypeoption=potype_constructor) or
  1932. inheritedcall then
  1933. opc:=a_invokespecial
  1934. else
  1935. opc:=a_invokevirtual;
  1936. end
  1937. else
  1938. internalerror(2010122602);
  1939. end;
  1940. if (opc<>a_invokeinterface) then
  1941. list.concat(taicpu.op_sym(opc,current_asmdata.RefAsmSymbol(s)))
  1942. else
  1943. begin
  1944. pd.init_paraloc_info(calleeside);
  1945. list.concat(taicpu.op_sym_const(opc,current_asmdata.RefAsmSymbol(s),pd.calleeargareasize));
  1946. end;
  1947. end;
  1948. procedure thlcgjvm.g_call_system_proc(list: TAsmList; const procname: string);
  1949. var
  1950. srsym: tsym;
  1951. pd: tprocdef;
  1952. begin
  1953. srsym:=tsym(systemunit.find(procname));
  1954. if not assigned(srsym) or
  1955. (srsym.typ<>procsym) then
  1956. Message1(cg_f_unknown_compilerproc,procname);
  1957. pd:=tprocdef(tprocsym(srsym).procdeflist[0]);
  1958. a_call_name(list,pd,pd.mangledname,false);
  1959. end;
  1960. procedure create_hlcodegen;
  1961. begin
  1962. hlcg:=thlcgjvm.create;
  1963. create_codegen;
  1964. end;
  1965. end.