njvmcnv.pas 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  1. {
  2. Copyright (c) 1998-2011 by Florian Klaempfl and Jonas Maebe
  3. Generate JVM code for type converting nodes
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************}
  16. unit njvmcnv;
  17. {$i fpcdefs.inc}
  18. interface
  19. uses
  20. node,ncnv,ncgcnv,
  21. symtype;
  22. type
  23. tjvmtypeconvnode = class(tcgtypeconvnode)
  24. function typecheck_dynarray_to_openarray: tnode; override;
  25. function typecheck_string_to_chararray: tnode; override;
  26. function pass_1: tnode; override;
  27. procedure second_int_to_int;override;
  28. { procedure second_string_to_string;override; }
  29. { procedure second_cstring_to_pchar;override; }
  30. { procedure second_string_to_chararray;override; }
  31. { procedure second_array_to_pointer;override; }
  32. function first_int_to_real: tnode; override;
  33. { procedure second_pointer_to_array;override; }
  34. { procedure second_chararray_to_string;override; }
  35. { procedure second_char_to_string;override; }
  36. procedure second_int_to_real;override;
  37. { procedure second_real_to_real;override; }
  38. { procedure second_cord_to_pointer;override; }
  39. { procedure second_proc_to_procvar;override; }
  40. procedure second_bool_to_int;override;
  41. procedure second_int_to_bool;override;
  42. { procedure second_load_smallset;override; }
  43. { procedure second_ansistring_to_pchar;override; }
  44. { procedure second_pchar_to_string;override; }
  45. { procedure second_class_to_intf;override; }
  46. { procedure second_char_to_char;override; }
  47. function target_specific_explicit_typeconv: boolean; override;
  48. function target_specific_general_typeconv: boolean; override;
  49. protected
  50. function do_target_specific_explicit_typeconv(check_only: boolean; out resnode: tnode): boolean;
  51. end;
  52. tjvmasnode = class(tcgasnode)
  53. protected
  54. function target_specific_typecheck: boolean;override;
  55. public
  56. function pass_1 : tnode;override;
  57. procedure pass_generate_code; override;
  58. end;
  59. tjvmisnode = class(tisnode)
  60. protected
  61. function target_specific_typecheck: boolean;override;
  62. public
  63. function pass_1 : tnode;override;
  64. procedure pass_generate_code; override;
  65. end;
  66. implementation
  67. uses
  68. verbose,globals,globtype,
  69. symconst,symdef,symsym,symtable,aasmbase,aasmdata,
  70. defutil,defcmp,jvmdef,
  71. cgbase,cgutils,pass_1,pass_2,
  72. nbas,ncon,ncal,nld,nmem,procinfo,
  73. nutils,
  74. cpubase,aasmcpu,
  75. tgobj,hlcgobj,hlcgcpu;
  76. {*****************************************************************************
  77. TypeCheckTypeConv
  78. *****************************************************************************}
  79. function tjvmtypeconvnode.typecheck_dynarray_to_openarray: tnode;
  80. begin
  81. { all arrays are equal in Java }
  82. left.resultdef:=resultdef;
  83. result:=left;
  84. left:=nil;
  85. end;
  86. function tjvmtypeconvnode.typecheck_string_to_chararray: tnode;
  87. var
  88. newblock: tblocknode;
  89. newstat: tstatementnode;
  90. restemp: ttempcreatenode;
  91. chartype: string;
  92. begin
  93. if (left.nodetype = stringconstn) and
  94. (tstringconstnode(left).cst_type=cst_conststring) then
  95. inserttypeconv(left,cunicodestringtype);
  96. { even constant strings have to be handled via a helper }
  97. if is_widechar(tarraydef(resultdef).elementdef) then
  98. chartype:='widechar'
  99. else
  100. chartype:='char';
  101. newblock:=internalstatements(newstat);
  102. restemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  103. addstatement(newstat,restemp);
  104. addstatement(newstat,ccallnode.createintern('fpc_'+tstringdef(left.resultdef).stringtypname+
  105. '_to_'+chartype+'array',ccallparanode.create(left,ccallparanode.create(
  106. ctemprefnode.create(restemp),nil))));
  107. addstatement(newstat,ctempdeletenode.create_normal_temp(restemp));
  108. addstatement(newstat,ctemprefnode.create(restemp));
  109. result:=newblock;
  110. left:=nil;
  111. end;
  112. {*****************************************************************************
  113. FirstTypeConv
  114. *****************************************************************************}
  115. function tjvmtypeconvnode.first_int_to_real: tnode;
  116. begin
  117. if not is_64bitint(left.resultdef) then
  118. if is_signed(left.resultdef) or
  119. (left.resultdef.size<4) then
  120. inserttypeconv(left,s32inttype)
  121. else
  122. inserttypeconv(left,u32inttype);
  123. firstpass(left);
  124. result := nil;
  125. expectloc:=LOC_FPUREGISTER;
  126. end;
  127. function tjvmtypeconvnode.pass_1: tnode;
  128. begin
  129. if (nf_explicit in flags) then
  130. begin
  131. do_target_specific_explicit_typeconv(false,result);
  132. if assigned(result) then
  133. exit;
  134. end;
  135. result:=inherited pass_1;
  136. end;
  137. {*****************************************************************************
  138. SecondTypeConv
  139. *****************************************************************************}
  140. procedure tjvmtypeconvnode.second_int_to_int;
  141. var
  142. ressize,
  143. leftsize : longint;
  144. begin
  145. { insert range check if not explicit conversion }
  146. if not(nf_explicit in flags) then
  147. hlcg.g_rangecheck(current_asmdata.CurrAsmList,left.location,left.resultdef,resultdef);
  148. { is the result size smaller? when typecasting from void
  149. we always reuse the current location, because there is
  150. nothing that we can load in a register }
  151. ressize:=resultdef.size;
  152. leftsize :=left.resultdef.size;
  153. if ((ressize<>leftsize) or
  154. ((location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) and
  155. (location.reference.arrayreftype<>art_none) and
  156. (is_widechar(left.resultdef)<>is_widechar(resultdef))) or
  157. is_bitpacked_access(left)) and
  158. not is_void(left.resultdef) then
  159. begin
  160. location_copy(location,left.location);
  161. { reuse a loc_reference when the newsize is smaller than
  162. than the original, except
  163. a) for arrays (they use different load instructions for
  164. differently sized data types)
  165. b) when going from 8 to 4 bytes, because these are different
  166. data types
  167. -- note that this is different from other targets, and will
  168. break stuff like passing byte(shortintvar) to a var-parameter;
  169. although that may be "fixed" again because we have to use
  170. copy-in/copy-out to emulate var-parameters anyway... }
  171. if (location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) and
  172. (location.reference.arrayreftype=art_none) and
  173. (ressize<leftsize) and
  174. (leftsize<=4) then
  175. begin
  176. location.size:=def_cgsize(resultdef);
  177. { no adjustment of the ffset even though Java is big endian,
  178. because the load instruction will remain the same }
  179. end
  180. else
  181. hlcg.location_force_reg(current_asmdata.CurrAsmList,location,left.resultdef,resultdef,false);
  182. end
  183. else
  184. begin
  185. location_copy(location,left.location);
  186. location.size:=def_cgsize(resultdef);
  187. if (ressize < sizeof(aint)) and
  188. (location.loc in [LOC_REGISTER,LOC_CREGISTER]) and
  189. (def_cgsize(left.resultdef)<>def_cgsize(resultdef)) then
  190. begin
  191. location.register:=hlcg.getintregister(current_asmdata.CurrAsmList,resultdef);
  192. location.loc:=LOC_REGISTER;
  193. hlcg.a_load_reg_reg(current_asmdata.CurrAsmList,left.resultdef,resultdef,left.location.register,location.register);
  194. end;
  195. end;
  196. end;
  197. procedure tjvmtypeconvnode.second_int_to_real;
  198. var
  199. srcsize, ressize: longint;
  200. procedure convertsignedstackloc;
  201. begin
  202. case srcsize of
  203. 4:
  204. case ressize of
  205. 4:
  206. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_i2f));
  207. 8:
  208. begin
  209. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_i2d));
  210. thlcgjvm(hlcg).incstack(current_asmdata.CurrAsmList,1);
  211. end;
  212. else
  213. internalerror(2011010601);
  214. end;
  215. 8:
  216. case ressize of
  217. 4:
  218. begin
  219. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_l2f));
  220. thlcgjvm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  221. end;
  222. 8:
  223. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_l2d));
  224. else
  225. internalerror(2011010602);
  226. end;
  227. else
  228. internalerror(2011010603);
  229. end;
  230. end;
  231. var
  232. signeddef : tdef;
  233. l1 : tasmlabel;
  234. begin
  235. srcsize:=left.resultdef.size;
  236. ressize:=resultdef.size;
  237. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  238. location.register:=hlcg.getfpuregister(current_asmdata.CurrAsmList,resultdef);
  239. { first always convert as if it's a signed number }
  240. thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  241. convertsignedstackloc;
  242. if not is_signed(left.resultdef) then
  243. begin
  244. { if it was unsigned, add high(cardinal)+1/high(qword)+1 in case
  245. the signed interpretation is < 0 }
  246. current_asmdata.getjumplabel(l1);
  247. if srcsize=4 then
  248. signeddef:=s32inttype
  249. else
  250. signeddef:=s64inttype;
  251. hlcg.a_cmp_const_loc_label(current_asmdata.CurrAsmList,signeddef,OC_GTE,0,left.location,l1);
  252. if srcsize=4 then
  253. thlcgjvm(hlcg).a_loadfpu_const_stack(current_asmdata.CurrAsmList,resultdef,4294967296.0)
  254. else
  255. thlcgjvm(hlcg).a_loadfpu_const_stack(current_asmdata.CurrAsmList,resultdef,18446744073709551616.0);
  256. if ressize=4 then
  257. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_fadd))
  258. else
  259. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_dadd));
  260. hlcg.a_label(current_asmdata.CurrAsmList,l1);
  261. end;
  262. thlcgjvm(hlcg).a_load_stack_reg(current_asmdata.CurrAsmList,resultdef,location.register);
  263. end;
  264. procedure tjvmtypeconvnode.second_bool_to_int;
  265. var
  266. newsize: tcgsize;
  267. oldTrueLabel,oldFalseLabel : tasmlabel;
  268. begin
  269. oldTrueLabel:=current_procinfo.CurrTrueLabel;
  270. oldFalseLabel:=current_procinfo.CurrFalseLabel;
  271. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  272. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  273. secondpass(left);
  274. location_copy(location,left.location);
  275. newsize:=def_cgsize(resultdef);
  276. { byte(bytebool) or word(wordbool) or longint(longbool) must be }
  277. { accepted for var parameters and assignments, and must not }
  278. { change the ordinal value or value location. }
  279. { htypechk.valid_for_assign ensures that such locations with a }
  280. { size<sizeof(register) cannot be LOC_CREGISTER (they otherwise }
  281. { could be in case of a plain assignment), and LOC_REGISTER can }
  282. { never be an assignment target. The remaining LOC_REGISTER/ }
  283. { LOC_CREGISTER locations do have to be sign/zero-extended. }
  284. { -- Note: this does not work for Java and 2/4 byte sized
  285. values, because bytebool/wordbool are signed and
  286. are stored in 4 byte locations -> will result in
  287. "byte" with the value high(cardinal); see remark
  288. in second_int_to_int above regarding consequences }
  289. if not(nf_explicit in flags) or
  290. (location.loc in [LOC_FLAGS,LOC_JUMP]) or
  291. ((newsize<>left.location.size) and
  292. ((left.resultdef.size<>resultdef.size) or
  293. not(left.resultdef.size in [4,8]))
  294. ) then
  295. hlcg.location_force_reg(current_asmdata.CurrAsmList,location,left.resultdef,resultdef,true)
  296. else
  297. { may differ in sign, e.g. bytebool -> byte }
  298. location.size:=newsize;
  299. current_procinfo.CurrTrueLabel:=oldTrueLabel;
  300. current_procinfo.CurrFalseLabel:=oldFalseLabel;
  301. end;
  302. procedure tjvmtypeconvnode.second_int_to_bool;
  303. var
  304. hlabel1,hlabel2,oldTrueLabel,oldFalseLabel : tasmlabel;
  305. newsize : tcgsize;
  306. begin
  307. oldTrueLabel:=current_procinfo.CurrTrueLabel;
  308. oldFalseLabel:=current_procinfo.CurrFalseLabel;
  309. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  310. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  311. secondpass(left);
  312. if codegenerror then
  313. exit;
  314. { Explicit typecasts from any ordinal type to a boolean type }
  315. { must not change the ordinal value }
  316. if (nf_explicit in flags) and
  317. not(left.location.loc in [LOC_FLAGS,LOC_JUMP]) then
  318. begin
  319. location_copy(location,left.location);
  320. newsize:=def_cgsize(resultdef);
  321. { change of size? change sign only if location is LOC_(C)REGISTER? Then we have to sign/zero-extend }
  322. if (tcgsize2size[newsize]<>tcgsize2size[left.location.size]) or
  323. ((newsize<>left.location.size) and (location.loc in [LOC_REGISTER,LOC_CREGISTER])) then
  324. hlcg.location_force_reg(current_asmdata.CurrAsmList,location,left.resultdef,resultdef,true)
  325. else
  326. location.size:=newsize;
  327. current_procinfo.CurrTrueLabel:=oldTrueLabel;
  328. current_procinfo.CurrFalseLabel:=oldFalseLabel;
  329. exit;
  330. end;
  331. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  332. location.register:=hlcg.getintregister(current_asmdata.CurrAsmList,resultdef);
  333. current_asmdata.getjumplabel(hlabel2);
  334. case left.location.loc of
  335. LOC_CREFERENCE,LOC_REFERENCE,LOC_REGISTER,LOC_CREGISTER:
  336. begin
  337. current_asmdata.getjumplabel(hlabel1);
  338. hlcg.a_cmp_const_loc_label(current_asmdata.CurrAsmList,left.resultdef,OC_EQ,0,left.location,hlabel1);
  339. end;
  340. LOC_JUMP :
  341. begin
  342. hlabel1:=current_procinfo.CurrFalseLabel;
  343. hlcg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrTrueLabel);
  344. end;
  345. else
  346. internalerror(10062);
  347. end;
  348. if not(is_cbool(resultdef)) then
  349. thlcgjvm(hlcg).a_load_const_stack(current_asmdata.CurrAsmList,resultdef,1,R_INTREGISTER)
  350. else
  351. thlcgjvm(hlcg).a_load_const_stack(current_asmdata.CurrAsmList,resultdef,-1,R_INTREGISTER);
  352. { we jump over the next constant load -> they don't appear on the
  353. stack simulataneously }
  354. thlcgjvm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  355. hlcg.a_jmp_always(current_asmdata.CurrAsmList,hlabel2);
  356. hlcg.a_label(current_asmdata.CurrAsmList,hlabel1);
  357. thlcgjvm(hlcg).a_load_const_stack(current_asmdata.CurrAsmList,resultdef,0,R_INTREGISTER);
  358. hlcg.a_label(current_asmdata.CurrAsmList,hlabel2);
  359. thlcgjvm(hlcg).a_load_stack_reg(current_asmdata.CurrAsmList,resultdef,location.register);
  360. current_procinfo.CurrTrueLabel:=oldTrueLabel;
  361. current_procinfo.CurrFalseLabel:=oldFalseLabel;
  362. end;
  363. procedure get_most_nested_types(var fromdef, todef: tdef);
  364. begin
  365. while is_dynamic_array(fromdef) and
  366. is_dynamic_array(todef) do
  367. begin
  368. fromdef:=tarraydef(fromdef).elementdef;
  369. todef:=tarraydef(todef).elementdef;
  370. end;
  371. end;
  372. function tjvmtypeconvnode.do_target_specific_explicit_typeconv(check_only: boolean; out resnode: tnode): boolean;
  373. { handle explicit typecast from int to to real or vice versa }
  374. function int_real_explicit_typecast(fdef: tfloatdef; const singlemethod, doublemethod: string): tnode;
  375. var
  376. csym: ttypesym;
  377. psym: tsym;
  378. begin
  379. { use the float/double to raw bits methods to get the bit pattern }
  380. if fdef.floattype=s32real then
  381. begin
  382. csym:=search_system_type('JLFLOAT');
  383. psym:=search_struct_member(tobjectdef(csym.typedef),singlemethod);
  384. end
  385. else
  386. begin
  387. csym:=search_system_type('JLDOUBLE');
  388. psym:=search_struct_member(tobjectdef(csym.typedef),doublemethod);
  389. end;
  390. if not assigned(psym) or
  391. (psym.typ<>procsym) then
  392. internalerror(2011012901);
  393. { call the (static class) method to get the raw bits }
  394. result:=ccallnode.create(ccallparanode.create(left,nil),
  395. tprocsym(psym),psym.owner,
  396. cloadvmtaddrnode.create(ctypenode.create(csym.typedef)),[]);
  397. { convert the result to the result type of this type conversion node }
  398. inserttypeconv_explicit(result,resultdef);
  399. { left is reused }
  400. left:=nil;
  401. end;
  402. var
  403. fromclasscompatible,
  404. toclasscompatible: boolean;
  405. fromdef,
  406. todef,
  407. jlclass: tdef;
  408. fromarrtype,
  409. toarrtype: char;
  410. begin
  411. resnode:=nil;
  412. { This routine is only called for explicit typeconversions of same-sized
  413. entities that aren't handled by normal type conversions -> bit pattern
  414. reinterpretations. In the JVM, many of these also need special
  415. handling because of the type safety. }
  416. { don't allow conversions between object-based and non-object-based
  417. types }
  418. fromclasscompatible:=
  419. (left.resultdef.typ=objectdef) or
  420. is_dynamic_array(left.resultdef) or
  421. ((left.resultdef.typ in [recorddef,stringdef]) and
  422. (resultdef.typ=objectdef));
  423. toclasscompatible:=
  424. (resultdef.typ=objectdef) or
  425. is_dynamic_array(resultdef) or
  426. ((resultdef.typ in [recorddef,stringdef]) and
  427. (left.resultdef.typ=objectdef));
  428. if fromclasscompatible and toclasscompatible then
  429. begin
  430. { we need an as-node to check the validity of the conversion (since
  431. it wasn't handled by another type conversion, we know it can't
  432. have been valid normally)
  433. Exceptions: (most nested) destination is
  434. * java.lang.Object, since everything is compatible with that type
  435. * related to source
  436. * a primitive that are represented by the same type in Java
  437. (e.g., byte and shortint) }
  438. fromdef:=left.resultdef;
  439. todef:=resultdef;
  440. get_most_nested_types(fromdef,todef);
  441. fromarrtype:=jvmarrtype_setlength(fromdef);
  442. toarrtype:=jvmarrtype_setlength(todef);
  443. if (compare_defs(fromdef,todef,nothingn)<te_equal) and
  444. not fromdef.is_related(todef) and
  445. (((fromdef.typ in [objectdef,recorddef,stringdef]) and
  446. not is_dynamic_array(fromdef)) or
  447. (todef<>java_jlobject)) and
  448. ((fromarrtype in ['A','R']) or
  449. (fromarrtype<>toarrtype)) then
  450. begin
  451. if not check_only and
  452. not assignment_side then
  453. begin
  454. resnode:=ctypenode.create(resultdef);
  455. if resultdef.typ=objectdef then
  456. resnode:=cloadvmtaddrnode.create(resnode);
  457. resnode:=casnode.create(left,resnode);
  458. left:=nil;
  459. end
  460. end
  461. { typecasting from a child to a parent type on the assignment side
  462. will (rightly) mess up the type safety verification of the JVM }
  463. else if assignment_side and
  464. (compare_defs(fromdef,todef,nothingn)<te_equal) then
  465. CGMessage(type_e_no_managed_assign_generic_typecast);
  466. result:=true;
  467. exit;
  468. end;
  469. { from classrefdef to JLClass and JLObject and back }
  470. if (left.resultdef.typ=classrefdef) or
  471. (resultdef.typ=classrefdef) then
  472. begin
  473. if (left.resultdef.typ=classrefdef) and
  474. (resultdef.typ=classrefdef) then
  475. begin
  476. if not tclassrefdef(left.resultdef).pointeddef.is_related(resultdef) and
  477. not tclassrefdef(resultdef).pointeddef.is_related(left.resultdef) then
  478. CGMessage2(type_e_illegal_type_conversion,left.resultdef.typename,resultdef.typename);
  479. end
  480. else
  481. begin
  482. jlclass:=search_system_type('JLCLASS').typedef;
  483. if (left.resultdef<>jlclass) and
  484. (left.resultdef<>java_jlobject) and
  485. (resultdef<>jlclass) and
  486. (resultdef<>java_jlobject) then
  487. CGMessage2(type_e_illegal_type_conversion,left.resultdef.typename,resultdef.typename);
  488. end;
  489. result:=true;
  490. exit;
  491. end;
  492. { don't allow conversions between different classes of primitive types,
  493. except for a few special cases }
  494. { float to int/enum explicit type conversion: get the bits }
  495. if (convtype<>tc_int_2_real) and
  496. (left.resultdef.typ=floatdef) and
  497. (is_integer(resultdef) or
  498. (resultdef.typ=enumdef)) then
  499. begin
  500. if not check_only then
  501. resnode:=int_real_explicit_typecast(tfloatdef(left.resultdef),'FLOATTORAWINTBITS','DOUBLETORAWLONGBITS');
  502. result:=true;
  503. exit;
  504. end;
  505. { int to float explicit type conversion: also use the bits }
  506. if (convtype<>tc_int_2_real) and
  507. (is_integer(left.resultdef) or
  508. (left.resultdef.typ=enumdef)) and
  509. (resultdef.typ=floatdef) then
  510. begin
  511. if not check_only then
  512. resnode:=int_real_explicit_typecast(tfloatdef(resultdef),'INTBITSTOFLOAT','LONGBITSTODOUBLE');
  513. result:=true;
  514. exit;
  515. end;
  516. { nothing special required when going between ordinals and enums }
  517. if (left.resultdef.typ in [orddef,enumdef]) and
  518. (resultdef.typ in [orddef,enumdef]) then
  519. begin
  520. result:=false;
  521. exit;
  522. end;
  523. {ifndef nounsupported}
  524. result:=false;
  525. exit;
  526. {endif}
  527. { Todo:
  528. * int to set and vice versa
  529. * set to float and vice versa (via int) (maybe)
  530. * regular array of primitive to primitive and vice versa (maybe)
  531. * packed record to primitive and vice versa (maybe)
  532. Definitely not:
  533. * unpacked record to anything and vice versa (no alignment rules
  534. for Java)
  535. }
  536. { anything not explicitly handled is a problem }
  537. CGMessage2(type_e_illegal_type_conversion,left.resultdef.typename,resultdef.typename);
  538. end;
  539. function tjvmtypeconvnode.target_specific_explicit_typeconv: boolean;
  540. var
  541. dummyres: tnode;
  542. begin
  543. result:=do_target_specific_explicit_typeconv(true,dummyres);
  544. end;
  545. function tjvmtypeconvnode.target_specific_general_typeconv: boolean;
  546. begin
  547. result:=false;
  548. { deal with explicit typecasts between records and classes (for
  549. FpcBaseRecordType) }
  550. if ((left.resultdef.typ=recorddef) and
  551. (resultdef.typ=objectdef) and
  552. left.resultdef.is_related(resultdef)) or
  553. ((left.resultdef.typ=objectdef) and
  554. (resultdef.typ=recorddef) and
  555. resultdef.is_related(left.resultdef)) and
  556. (nf_explicit in flags) then
  557. begin
  558. convtype:=tc_equal;
  559. result:=true;
  560. exit;
  561. end;
  562. {$ifndef nounsupported}
  563. if ((left.resultdef.typ=procvardef) and
  564. ((resultdef=methodpointertype) or
  565. (resultdef=search_system_type('TMETHOD').typedef))) or
  566. ((resultdef.typ=procvardef) and
  567. ((left.resultdef=methodpointertype) or
  568. (left.resultdef=search_system_type('TMETHOD').typedef))) then
  569. begin
  570. convtype:=tc_equal;
  571. result:=true;
  572. exit;
  573. end;
  574. {$endif}
  575. end;
  576. {*****************************************************************************
  577. AsNode and IsNode common helpers
  578. *****************************************************************************}
  579. function asis_target_specific_typecheck(node: tasisnode): boolean;
  580. var
  581. fromelt, toelt: tdef;
  582. realfromdef,
  583. realtodef: tdef;
  584. function isrecordconv(var res: boolean): boolean;
  585. begin
  586. if is_record(realtodef) then
  587. result:=
  588. (realfromdef=java_jlobject) or
  589. (realfromdef=java_fpcbaserecordtype)
  590. else if is_record(realfromdef) then
  591. result:=
  592. (realtodef=java_jlobject) or
  593. (realtodef=java_fpcbaserecordtype)
  594. else
  595. end;
  596. function isstringconv(var res: boolean): boolean;
  597. begin
  598. if is_wide_or_unicode_string(realtodef) then
  599. result:=
  600. (realfromdef=java_jlobject) or
  601. (realfromdef=java_jlstring)
  602. else if is_wide_or_unicode_string(realfromdef) then
  603. result:=
  604. (realtodef=java_jlobject) or
  605. (realtodef=java_jlstring)
  606. else
  607. end;
  608. begin
  609. realfromdef:=maybe_find_real_class_definition(node.left.resultdef,false);
  610. realtodef:=node.right.resultdef;
  611. if realtodef.typ=classrefdef then
  612. realtodef:=tclassrefdef(realtodef).pointeddef;
  613. realtodef:=maybe_find_real_class_definition(realtodef,false);
  614. if not isrecordconv(result) and
  615. not isstringconv(result) then
  616. { dynamic arrays can be converted to java.lang.Object and vice versa }
  617. if realtodef=java_jlobject then
  618. { dynamic array to java.lang.Object }
  619. result:=is_dynamic_array(realfromdef)
  620. else if is_dynamic_array(realtodef) then
  621. begin
  622. { <x> to dynamic array: only if possibly valid }
  623. fromelt:=node.left.resultdef;
  624. toelt:=realtodef;
  625. get_most_nested_types(fromelt,toelt);
  626. { final levels must be convertable:
  627. a) from array (dynamic or not) to java.lang.Object or vice versa,
  628. or
  629. b) the same primitive/class type
  630. }
  631. if not isrecordconv(result) then
  632. result:=
  633. (compare_defs(fromelt,toelt,node.left.nodetype) in [te_exact,te_equal]) or
  634. (((fromelt.typ=objectdef) or
  635. (fromelt.typ=arraydef)) and
  636. ((toelt.typ=objectdef) or
  637. (toelt.typ=arraydef)));
  638. end
  639. else
  640. begin
  641. if (node.right.resultdef.typ<>classrefdef) then
  642. result:=false
  643. else
  644. result:=true;
  645. end;
  646. if result then
  647. if node.nodetype=asn then
  648. begin
  649. if realtodef.typ<>classrefdef then
  650. node.resultdef:=realtodef
  651. else
  652. node.resultdef:=tclassrefdef(realtodef).pointeddef
  653. end
  654. else
  655. node.resultdef:=pasbool8type;
  656. end;
  657. function asis_pass_1(node: tasisnode; const methodname: string): tnode;
  658. var
  659. ps: tsym;
  660. call: tnode;
  661. jlclass: tobjectdef;
  662. begin
  663. result:=nil;
  664. firstpass(node.left);
  665. if not(node.right.nodetype in [typen,loadvmtaddrn]) then
  666. begin
  667. if (node.nodetype=isn) or
  668. not assigned(tasnode(node).call) then
  669. begin
  670. if not is_javaclassref(node.right.resultdef) then
  671. internalerror(2011041920);
  672. firstpass(node.right);
  673. jlclass:=tobjectdef(search_system_type('JLCLASS').typedef);
  674. ps:=search_struct_member(jlclass,methodname);
  675. if not assigned(ps) or
  676. (ps.typ<>procsym) then
  677. internalerror(2011041910);
  678. call:=ccallnode.create(ccallparanode.create(node.left,nil),tprocsym(ps),ps.owner,ctypeconvnode.create_explicit(node.right,jlclass),[]);
  679. node.left:=nil;
  680. node.right:=nil;
  681. firstpass(call);
  682. if codegenerror then
  683. exit;
  684. if node.nodetype=isn then
  685. result:=call
  686. else
  687. begin
  688. tasnode(node).call:=call;
  689. node.expectloc:=call.expectloc;
  690. end;
  691. end;
  692. end
  693. else
  694. begin
  695. node.expectloc:=LOC_REGISTER;
  696. result:=nil;
  697. end;
  698. end;
  699. function asis_generate_code(node: tasisnode; opcode: tasmop): boolean;
  700. var
  701. checkdef: tdef;
  702. begin
  703. if (node.nodetype=asn) and
  704. assigned(tasnode(node).call) then
  705. begin
  706. result:=false;
  707. exit;
  708. end;
  709. result:=true;
  710. secondpass(node.left);
  711. thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,node.left.resultdef,node.left.location);
  712. location_freetemp(current_asmdata.CurrAsmList,node.left.location);
  713. { Perform a checkcast instruction, which will raise an exception in case
  714. the actual type does not match/inherit from the expected type.
  715. Object types need the full type name (package+class name), arrays only
  716. the array definition }
  717. if node.nodetype=asn then
  718. checkdef:=node.resultdef
  719. else if node.right.resultdef.typ=classrefdef then
  720. checkdef:=tclassrefdef(node.right.resultdef).pointeddef
  721. else
  722. checkdef:=node.right.resultdef;
  723. if checkdef.typ in [objectdef,recorddef] then
  724. current_asmdata.CurrAsmList.concat(taicpu.op_sym(opcode,current_asmdata.RefAsmSymbol(tabstractrecorddef(checkdef).jvm_full_typename(true))))
  725. else
  726. current_asmdata.CurrAsmList.concat(taicpu.op_sym(opcode,current_asmdata.RefAsmSymbol(jvmencodetype(checkdef))));
  727. location_reset(node.location,LOC_REGISTER,OS_ADDR);
  728. node.location.register:=hlcg.getaddressregister(current_asmdata.CurrAsmList,node.resultdef);
  729. thlcgjvm(hlcg).a_load_stack_reg(current_asmdata.CurrAsmList,node.resultdef,node.location.register);
  730. end;
  731. {*****************************************************************************
  732. TJVMAsNode
  733. *****************************************************************************}
  734. function tjvmasnode.target_specific_typecheck: boolean;
  735. begin
  736. result:=asis_target_specific_typecheck(self);
  737. end;
  738. function tjvmasnode.pass_1: tnode;
  739. begin
  740. result:=asis_pass_1(self,'CAST');
  741. end;
  742. procedure tjvmasnode.pass_generate_code;
  743. begin
  744. if not asis_generate_code(self,a_checkcast) then
  745. inherited;
  746. end;
  747. {*****************************************************************************
  748. TJVMIsNode
  749. *****************************************************************************}
  750. function tjvmisnode.target_specific_typecheck: boolean;
  751. begin
  752. result:=asis_target_specific_typecheck(self);
  753. end;
  754. function tjvmisnode.pass_1: tnode;
  755. begin
  756. result:=asis_pass_1(self,'ISINSTANCE');
  757. end;
  758. procedure tjvmisnode.pass_generate_code;
  759. begin
  760. if not asis_generate_code(self,a_instanceof) then
  761. inherited;
  762. end;
  763. begin
  764. ctypeconvnode:=tjvmtypeconvnode;
  765. casnode:=tjvmasnode;
  766. cisnode:=tjvmisnode;
  767. end.