njvminl.pas 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. {
  2. Copyright (c) 1998-2011 by Florian Klaempfl and Jonas Maebe
  3. Generate JVM inline 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. }
  17. unit njvminl;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cpubase,
  22. node,ninl,ncginl;
  23. type
  24. tjvminlinenode = class(tcginlinenode)
  25. protected
  26. function typecheck_length(var handled: boolean): tnode;
  27. function typecheck_high(var handled: boolean): tnode;
  28. function typecheck_new(var handled: boolean): tnode;
  29. function first_setlength_array: tnode;
  30. function first_setlength_string: tnode;
  31. public
  32. { typecheck override to intercept handling }
  33. function pass_typecheck: tnode; override;
  34. { first pass override
  35. so that the code generator will actually generate
  36. these nodes.
  37. }
  38. (*
  39. function first_sqrt_real: tnode; override;
  40. *)
  41. function first_sqr_real: tnode; override;
  42. function first_trunc_real: tnode; override;
  43. (*
  44. function first_round_real: tnode; override;
  45. *)
  46. function first_new: tnode; override;
  47. function first_setlength: tnode; override;
  48. function first_length: tnode; override;
  49. procedure second_length; override;
  50. (*
  51. procedure second_sqrt_real; override;
  52. procedure second_abs_real; override;
  53. *)
  54. procedure second_sqr_real; override;
  55. procedure second_trunc_real; override;
  56. (*
  57. procedure second_round_real; override;
  58. *)
  59. procedure second_new; override;
  60. procedure second_setlength; override;
  61. protected
  62. procedure load_fpu_location;
  63. end;
  64. implementation
  65. uses
  66. cutils,globals,verbose,globtype,constexp,
  67. aasmbase,aasmtai,aasmdata,aasmcpu,
  68. symtype,symconst,symdef,symsym,symtable,jvmdef,
  69. defutil,
  70. nbas,ncon,ncnv,ncal,nld,nflw,nutils,
  71. cgbase,pass_1,pass_2,
  72. cpuinfo,ncgutil,
  73. cgutils,hlcgobj,hlcgcpu;
  74. {*****************************************************************************
  75. tjvminlinenode
  76. *****************************************************************************}
  77. function tjvminlinenode.typecheck_length(var handled: boolean): tnode;
  78. begin
  79. typecheckpass(left);
  80. if is_dynamic_array(left.resultdef) or
  81. is_open_array(left.resultdef) or
  82. is_wide_or_unicode_string(left.resultdef) then
  83. begin
  84. resultdef:=s32inttype;
  85. result:=nil;
  86. handled:=true;
  87. end;
  88. end;
  89. function tjvminlinenode.typecheck_high(var handled: boolean): tnode;
  90. begin
  91. typecheckpass(left);
  92. if is_dynamic_array(left.resultdef) or
  93. is_open_array(left.resultdef) then
  94. begin
  95. { replace with pred(length(arr)) }
  96. result:=cinlinenode.create(in_pred_x,false,
  97. cinlinenode.create(in_length_x,false,left));
  98. left:=nil;
  99. handled:=true;
  100. end;
  101. end;
  102. function tjvminlinenode.typecheck_new(var handled: boolean): tnode;
  103. var
  104. para: tcallparanode;
  105. elemdef: tdef;
  106. begin
  107. { normally never exists; used by the JVM backend to create new
  108. arrays because it requires special opcodes }
  109. tcallparanode(left).get_paratype;
  110. if is_dynamic_array(left.resultdef) then
  111. begin
  112. para:=tcallparanode(left);
  113. { need at least one extra parameter in addition to the
  114. array }
  115. if not assigned(para.right) then
  116. internalerror(2011012206);
  117. elemdef:=tarraydef(left.resultdef).elementdef;
  118. while elemdef.typ=arraydef do
  119. begin
  120. { if we have less length specifiers than dimensions, make
  121. the last array an array of length 0 }
  122. if not assigned(para.right) then
  123. begin
  124. para.right:=ccallparanode.create(
  125. cordconstnode.create(0,s32inttype,false),nil);
  126. tcallparanode(para.right).get_paratype;
  127. break;
  128. end
  129. else
  130. begin
  131. inserttypeconv(tcallparanode(para.right).left,s32inttype);
  132. tcallparanode(para.right).get_paratype;
  133. end;
  134. para:=tcallparanode(para.right);
  135. elemdef:=tarraydef(elemdef).elementdef;
  136. end;
  137. result:=nil;
  138. resultdef:=left.resultdef;
  139. handled:=true;
  140. end;
  141. end;
  142. function tjvminlinenode.pass_typecheck: tnode;
  143. var
  144. handled: boolean;
  145. begin
  146. handled:=false;
  147. case inlinenumber of
  148. in_length_x:
  149. begin
  150. result:=typecheck_length(handled);
  151. end;
  152. in_high_x:
  153. begin
  154. result:=typecheck_high(handled);
  155. end;
  156. in_new_x:
  157. begin
  158. result:=typecheck_new(handled);
  159. end;
  160. end;
  161. if not handled then
  162. result:=inherited pass_typecheck;
  163. end;
  164. (*
  165. function tjvminlinenode.first_sqrt_real : tnode;
  166. begin
  167. if (current_settings.cputype >= cpu_PPC970) then
  168. begin
  169. expectloc:=LOC_FPUREGISTER;
  170. first_sqrt_real := nil;
  171. end
  172. else
  173. result:=inherited first_sqrt_real;
  174. end;
  175. *)
  176. function tjvminlinenode.first_sqr_real : tnode;
  177. begin
  178. expectloc:=LOC_FPUREGISTER;
  179. first_sqr_real:=nil;
  180. end;
  181. function tjvminlinenode.first_trunc_real : tnode;
  182. begin
  183. expectloc:=LOC_REGISTER;
  184. first_trunc_real:=nil;
  185. end;
  186. function tjvminlinenode.first_new: tnode;
  187. begin
  188. { skip the array; it's a type node }
  189. tcallparanode(tcallparanode(left).right).firstcallparan;
  190. expectloc:=LOC_REGISTER;
  191. result:=nil;
  192. end;
  193. function tjvminlinenode.first_setlength_array: tnode;
  194. var
  195. assignmenttarget,
  196. ppn,
  197. newparas: tnode;
  198. newnode: tnode;
  199. eledef,
  200. objarraydef: tdef;
  201. ndims: longint;
  202. finaltype: char;
  203. setlenroutine: string;
  204. lefttemp: ttempcreatenode;
  205. newblock: tblocknode;
  206. newstatement: tstatementnode;
  207. primitive: boolean;
  208. begin
  209. { first parameter is the array, the rest are the dimensions }
  210. newparas:=tcallparanode(left).right;
  211. tcallparanode(left).right:=nil;
  212. { count the number of specified dimensions, and determine the type of
  213. the final one }
  214. ppn:=newparas;
  215. eledef:=tarraydef(left.resultdef).elementdef;
  216. { ppn already points to the first dimension }
  217. ndims:=1;
  218. while assigned(tcallparanode(ppn).right) do
  219. begin
  220. inc(ndims);
  221. eledef:=tarraydef(eledef).elementdef;
  222. ppn:=tcallparanode(ppn).right;
  223. end;
  224. { in case it's a dynamic array of static arrays, we must also allocate
  225. the static arrays! }
  226. while (eledef.typ=arraydef) and
  227. not is_dynamic_array(eledef) do
  228. begin
  229. inc(ndims);
  230. tcallparanode(ppn).right:=
  231. ccallparanode.create(
  232. genintconstnode(tarraydef(eledef).elecount),nil);
  233. ppn:=tcallparanode(ppn).right;
  234. eledef:=tarraydef(eledef).elementdef;
  235. end;
  236. { prepend type parameter for the array }
  237. newparas:=ccallparanode.create(ctypenode.create(left.resultdef),newparas);
  238. ttypenode(tcallparanode(newparas).left).allowed:=true;
  239. { node to create the new array }
  240. newnode:=cinlinenode.create(in_new_x,false,newparas);
  241. { Common parameters for setlength helper }
  242. { start with org (save assignmenttarget itself to assign the result back to) }
  243. { store left into a temp in case it may contain a function call
  244. (which must not be evaluated twice) }
  245. lefttemp:=maybereplacewithtempref(tcallparanode(left).left,tcallparanode(left).left.resultdef.size,false);
  246. if assigned(lefttemp) then
  247. begin
  248. newblock:=internalstatements(newstatement);
  249. addstatement(newstatement,lefttemp);
  250. assignmenttarget:=ctemprefnode.create(lefttemp);
  251. typecheckpass(tnode(assignmenttarget));
  252. end
  253. else
  254. assignmenttarget:=tcallparanode(left).left.getcopy;
  255. newparas:=left;
  256. left:=nil;
  257. { if more than 1 dimension, or if 1 dimention of a non-primitive type,
  258. typecast to generic array of tobject }
  259. setlenroutine:=jvmarrtype(eledef,primitive);
  260. if (ndims>1) or
  261. not primitive then
  262. begin
  263. objarraydef:=search_system_type('TJOBJECTARRAY').typedef;
  264. tcallparanode(newparas).left:=ctypeconvnode.create_explicit(tcallparanode(newparas).left,objarraydef);
  265. newnode:=ctypeconvnode.create_explicit(newnode,objarraydef);
  266. end;
  267. { prepend new }
  268. newparas:=ccallparanode.create(newnode,newparas);
  269. { prepend deepcopy }
  270. newparas:=ccallparanode.create(cordconstnode.create(0,pasbool8type,false),newparas);
  271. { call the right setlenght helper }
  272. if ndims>1 then
  273. begin
  274. finaltype:=jvmarrtype_setlength(eledef);
  275. setlenroutine:='FPC_SETLENGTH_DYNARR_MULTIDIM';
  276. { create proper parameters, from right to left:
  277. eletype=finaltype, ndim=ndims, deepcopy=false, new=newnode,
  278. assignmenttarget=tcallparanode(left).left }
  279. { prepend ndim }
  280. newparas:=ccallparanode.create(cordconstnode.create(ndims,s32inttype,false),newparas);
  281. { prepend eletype }
  282. newparas:=ccallparanode.create(cordconstnode.create(ord(finaltype),cwidechartype,false),newparas);
  283. end
  284. else
  285. begin
  286. if not primitive then
  287. setlenroutine:='OBJECT'
  288. else
  289. uppervar(setlenroutine);
  290. setlenroutine:='FPC_SETLENGTH_DYNARR_J'+setlenroutine;
  291. { create proper parameters, from right to left:
  292. deepcopy=false, new=newnode, assignmenttarget=tcallparnode(left).left
  293. -> already done in common part above }
  294. end;
  295. result:=ccallnode.createintern(setlenroutine,newparas);
  296. { assign result back to org (no call-by-reference for Java) }
  297. result:=cassignmentnode.create(assignmenttarget,
  298. ctypeconvnode.create_explicit(result,assignmenttarget.resultdef));
  299. if assigned(lefttemp) then
  300. begin
  301. addstatement(newstatement,result);
  302. addstatement(newstatement,ctempdeletenode.create(lefttemp));
  303. result:=newblock;
  304. end;
  305. end;
  306. function tjvminlinenode.first_setlength_string: tnode;
  307. var
  308. newblock: tblocknode;
  309. newstatement: tstatementnode;
  310. lefttemp: ttempcreatenode;
  311. assignmenttarget: tnode;
  312. begin
  313. if is_wide_or_unicode_string(left.resultdef) then
  314. begin
  315. { store left into a temp in case it may contain a function call
  316. (which must not be evaluated twice) }
  317. lefttemp:=maybereplacewithtempref(tcallparanode(left).left,tcallparanode(left).left.resultdef.size,false);
  318. if assigned(lefttemp) then
  319. begin
  320. newblock:=internalstatements(newstatement);
  321. addstatement(newstatement,lefttemp);
  322. assignmenttarget:=ctemprefnode.create(lefttemp);
  323. typecheckpass(tnode(assignmenttarget));
  324. end
  325. else
  326. assignmenttarget:=tcallparanode(left).left.getcopy;
  327. { back to original order for the call }
  328. left:=reverseparameters(tcallparanode(left));
  329. result:=cassignmentnode.create(assignmenttarget,
  330. ccallnode.createintern('fpc_unicodestr_setlength',left));
  331. if assigned(lefttemp) then
  332. begin
  333. addstatement(newstatement,result);
  334. addstatement(newstatement,ctempdeletenode.create(lefttemp));
  335. result:=newblock;
  336. end;
  337. left:=nil;
  338. end
  339. else
  340. internalerror(2011031405);
  341. end;
  342. function tjvminlinenode.first_setlength: tnode;
  343. begin
  344. { reverse the parameter order so we can process them more easily }
  345. left:=reverseparameters(tcallparanode(left));
  346. { treat setlength(x,0) specially: used to init uninitialised locations }
  347. if not assigned(tcallparanode(tcallparanode(left).right).right) and
  348. is_constintnode(tcallparanode(tcallparanode(left).right).left) and
  349. (tordconstnode(tcallparanode(tcallparanode(left).right).left).value=0) then
  350. begin
  351. result:=nil;
  352. expectloc:=LOC_VOID;
  353. exit;
  354. end;
  355. case left.resultdef.typ of
  356. arraydef:
  357. result:=first_setlength_array;
  358. stringdef:
  359. result:=first_setlength_string;
  360. else
  361. internalerror(2011031204);
  362. end;
  363. end;
  364. function tjvminlinenode.first_length: tnode;
  365. var
  366. newblock: tblocknode;
  367. newstatement: tstatementnode;
  368. lentemp: ttempcreatenode;
  369. ifcond,
  370. stringnonnull,
  371. stringnull: tnode;
  372. psym: tsym;
  373. begin
  374. if is_wide_or_unicode_string(left.resultdef) then
  375. begin
  376. { if assigned(JLString(left)) then
  377. lentemp:=JLString(left).length()
  378. else
  379. lentemp:=0;
  380. --> return lentemp
  381. }
  382. newblock:=internalstatements(newstatement);
  383. lentemp:=ctempcreatenode.create(s32inttype,s32inttype.size,tt_persistent,true);
  384. addstatement(newstatement,lentemp);
  385. { if-condition }
  386. ifcond:=cinlinenode.create(in_assigned_x,false,
  387. ccallparanode.create(ctypeconvnode.create_explicit(left.getcopy,java_jlstring),nil));
  388. { then-path (reuse left, since last use) }
  389. psym:=search_struct_member(java_jlstring,'LENGTH');
  390. if not assigned(psym) or
  391. (psym.typ<>procsym) then
  392. internalerror(2011031403);
  393. stringnonnull:=cassignmentnode.create(
  394. ctemprefnode.create(lentemp),
  395. ccallnode.create(nil,tprocsym(psym),psym.owner,
  396. ctypeconvnode.create_explicit(left,java_jlstring),[]));
  397. left:=nil;
  398. { else-path}
  399. stringnull:=cassignmentnode.create(
  400. ctemprefnode.create(lentemp),
  401. genintconstnode(0));
  402. { complete if-statement }
  403. addstatement(newstatement,cifnode.create(ifcond,stringnonnull,stringnull));
  404. { return temp }
  405. addstatement(newstatement,ctempdeletenode.create_normal_temp(lentemp));
  406. addstatement(newstatement,ctemprefnode.create(lentemp));
  407. result:=newblock;
  408. end
  409. else
  410. result:=inherited first_length;
  411. end;
  412. procedure tjvminlinenode.second_length;
  413. begin
  414. if is_dynamic_array(left.resultdef) or
  415. is_open_array(left.resultdef) then
  416. begin
  417. location_reset(location,LOC_REGISTER,OS_S32);
  418. location.register:=hlcg.getintregister(current_asmdata.CurrAsmList,s32inttype);
  419. secondpass(left);
  420. thlcgjvm(hlcg).g_getarraylen(current_asmdata.CurrAsmList,left.location);
  421. thlcgjvm(hlcg).a_load_stack_reg(current_asmdata.CurrAsmList,resultdef,location.register);
  422. end
  423. else
  424. internalerror(2011012004);
  425. end;
  426. (*
  427. function tjvminlinenode.first_round_real : tnode;
  428. begin
  429. if (current_settings.cputype >= cpu_PPC970) then
  430. begin
  431. expectloc:=LOC_REFERENCE;
  432. first_round_real := nil;
  433. end
  434. else
  435. result:=inherited first_round_real;
  436. end;
  437. *)
  438. { load the FPU value on the evaluation stack }
  439. procedure tjvminlinenode.load_fpu_location;
  440. begin
  441. secondpass(left);
  442. thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  443. end;
  444. (*
  445. procedure tjvminlinenode.second_sqrt_real;
  446. begin
  447. if (current_settings.cputype < cpu_PPC970) then
  448. internalerror(2007020910);
  449. location.loc:=LOC_FPUREGISTER;
  450. load_fpu_location;
  451. case left.location.size of
  452. OS_F32:
  453. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FSQRTS,location.register,
  454. left.location.register));
  455. OS_F64:
  456. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FSQRT,location.register,
  457. left.location.register));
  458. else
  459. inherited;
  460. end;
  461. end;
  462. *)
  463. procedure tjvminlinenode.second_sqr_real;
  464. begin
  465. load_fpu_location;
  466. location_reset(location,LOC_FPUREGISTER,location.size);
  467. location.register:=hlcg.getfpuregister(current_asmdata.CurrAsmList,resultdef);
  468. case left.location.size of
  469. OS_F32:
  470. begin
  471. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_dup));
  472. thlcgjvm(hlcg).incstack(current_asmdata.CurrAsmList,1);
  473. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_fmul));
  474. thlcgjvm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  475. end;
  476. OS_F64:
  477. begin
  478. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_dup2));
  479. thlcgjvm(hlcg).incstack(current_asmdata.CurrAsmList,2);
  480. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_dmul));
  481. thlcgjvm(hlcg).decstack(current_asmdata.CurrAsmList,2);
  482. end;
  483. else
  484. internalerror(2011010804);
  485. end;
  486. thlcgjvm(hlcg).a_load_stack_reg(current_asmdata.CurrAsmList,resultdef,location.register);
  487. end;
  488. procedure tjvminlinenode.second_trunc_real;
  489. begin
  490. load_fpu_location;
  491. location_reset(location,LOC_REGISTER,left.location.size);
  492. location.register:=hlcg.getintregister(current_asmdata.CurrAsmList,resultdef);
  493. case left.location.size of
  494. OS_F32:
  495. begin
  496. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_f2l));
  497. { 32 bit float -> 64 bit int: +1 stack slot }
  498. thlcgjvm(hlcg).incstack(current_asmdata.CurrAsmList,1);
  499. end;
  500. OS_F64:
  501. begin
  502. { 64 bit float -> 64 bit int: same number of stack slots }
  503. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_d2l));
  504. end;
  505. else
  506. internalerror(2011010805);
  507. end;
  508. thlcgjvm(hlcg).a_load_stack_reg(current_asmdata.CurrAsmList,resultdef,location.register);
  509. end;
  510. procedure tjvminlinenode.second_new;
  511. var
  512. arr: tnode;
  513. hp: tcallparanode;
  514. paracount: longint;
  515. begin
  516. hp:=tcallparanode(left);
  517. { we don't second pass this one, it's only a type node }
  518. arr:=hp.left;
  519. if not is_dynamic_array(arr.resultdef) then
  520. internalerror(2011012204);
  521. hp:=tcallparanode(hp.right);
  522. if not assigned(hp) then
  523. internalerror(2011012205);
  524. paracount:=0;
  525. { put all the dimensions on the stack }
  526. repeat
  527. inc(paracount);
  528. secondpass(hp.left);
  529. thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,hp.left.resultdef,hp.left.location);
  530. hp:=tcallparanode(hp.right);
  531. until not assigned(hp);
  532. { create the array }
  533. thlcgjvm(hlcg).g_newarray(current_asmdata.CurrAsmList,arr.resultdef,paracount);
  534. location_reset(location,LOC_REGISTER,OS_ADDR);
  535. location.register:=hlcg.getaddressregister(current_asmdata.CurrAsmList,resultdef);
  536. thlcgjvm(hlcg).a_load_stack_reg(current_asmdata.CurrAsmList,arr.resultdef,location.register);
  537. end;
  538. procedure tjvminlinenode.second_setlength;
  539. var
  540. target: tnode;
  541. lenpara: tnode;
  542. emptystr: ansichar;
  543. begin
  544. target:=tcallparanode(left).left;
  545. lenpara:=tcallparanode(tcallparanode(left).right).left;
  546. if assigned(tcallparanode(tcallparanode(left).right).right) or
  547. not is_constintnode(lenpara) or
  548. (tordconstnode(lenpara).value<>0) then
  549. internalerror(2011031801);
  550. secondpass(target);
  551. if is_wide_or_unicode_string(target.resultdef) then
  552. begin
  553. emptystr:=#0;
  554. current_asmdata.CurrAsmList.concat(taicpu.op_string(a_ldc,0,@emptystr));
  555. thlcgjvm(hlcg).incstack(current_asmdata.CurrAsmList,1);
  556. end
  557. else if is_dynamic_array(target.resultdef) then
  558. begin
  559. thlcgjvm(hlcg).a_load_const_stack(current_asmdata.CurrAsmList,s32inttype,0,R_INTREGISTER);
  560. thlcgjvm(hlcg).g_newarray(current_asmdata.CurrAsmList,target.resultdef,1);
  561. end
  562. else
  563. internalerror(2011031401);
  564. thlcgjvm(hlcg).a_load_stack_loc(current_asmdata.CurrAsmList,target.resultdef,target.location);
  565. end;
  566. begin
  567. cinlinenode:=tjvminlinenode;
  568. end.