njvminl.pas 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  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_copy: tnode; override;
  30. function first_assigned: tnode; override;
  31. function first_get_frame: tnode; override;
  32. function first_box: tnode; override;
  33. function first_unbox: tnode; override;
  34. function first_setlength_array: tnode;
  35. public
  36. { typecheck override to intercept handling }
  37. function pass_typecheck: tnode; override;
  38. { first pass override
  39. so that the code generator will actually generate
  40. these nodes.
  41. }
  42. function first_sqr_real: tnode; override;
  43. function first_trunc_real: tnode; override;
  44. function first_new: tnode; override;
  45. function first_IncludeExclude: tnode; override;
  46. function first_setlength: tnode; override;
  47. function first_length: tnode; override;
  48. procedure second_length; override;
  49. procedure second_sqr_real; override;
  50. procedure second_trunc_real; override;
  51. procedure second_new; override;
  52. procedure second_setlength; override;
  53. protected
  54. procedure load_fpu_location;
  55. end;
  56. implementation
  57. uses
  58. cutils,globals,verbose,globtype,constexp,fmodule,compinnr,
  59. aasmbase,aasmtai,aasmdata,aasmcpu,
  60. symtype,symconst,symdef,symsym,symcpu,symtable,jvmdef,
  61. defutil,
  62. nadd,nbas,ncon,ncnv,nmat,nmem,ncal,nld,nflw,nutils,
  63. cgbase,pass_1,pass_2,
  64. cpuinfo,ncgutil,
  65. cgutils,hlcgobj,hlcgcpu;
  66. {*****************************************************************************
  67. tjvminlinenode
  68. *****************************************************************************}
  69. function tjvminlinenode.typecheck_length(var handled: boolean): tnode;
  70. begin
  71. result:=nil;
  72. typecheckpass(left);
  73. if is_open_array(left.resultdef) or
  74. is_dynamic_array(left.resultdef) or
  75. is_array_of_const(left.resultdef) then
  76. begin
  77. resultdef:=s32inttype;
  78. handled:=true;
  79. end;
  80. end;
  81. function tjvminlinenode.typecheck_high(var handled: boolean): tnode;
  82. begin
  83. result:=nil;
  84. typecheckpass(left);
  85. if is_dynamic_array(left.resultdef) or
  86. is_open_array(left.resultdef) or
  87. is_array_of_const(left.resultdef) then
  88. begin
  89. { replace with pred(length(arr)) }
  90. result:=cinlinenode.create(in_pred_x,false,
  91. cinlinenode.create(in_length_x,false,left));
  92. left:=nil;
  93. handled:=true;
  94. end;
  95. end;
  96. function tjvminlinenode.typecheck_new(var handled: boolean): tnode;
  97. var
  98. para: tcallparanode;
  99. elemdef: tdef;
  100. begin
  101. result:=nil;
  102. { normally never exists; used by the JVM backend to create new
  103. arrays because it requires special opcodes }
  104. tcallparanode(left).get_paratype;
  105. if is_dynamic_array(left.resultdef) then
  106. begin
  107. para:=tcallparanode(left);
  108. { need at least one extra parameter in addition to the
  109. array }
  110. if not assigned(para.right) then
  111. internalerror(2011012206);
  112. elemdef:=tarraydef(left.resultdef).elementdef;
  113. while elemdef.typ=arraydef do
  114. begin
  115. { if we have less length specifiers than dimensions, make
  116. the last array an array of length 0 }
  117. if not assigned(para.right) then
  118. begin
  119. para.right:=ccallparanode.create(
  120. cordconstnode.create(0,s32inttype,false),nil);
  121. tcallparanode(para.right).get_paratype;
  122. break;
  123. end
  124. else
  125. begin
  126. inserttypeconv(tcallparanode(para.right).left,s32inttype);
  127. tcallparanode(para.right).get_paratype;
  128. end;
  129. para:=tcallparanode(para.right);
  130. elemdef:=tarraydef(elemdef).elementdef;
  131. end;
  132. resultdef:=left.resultdef;
  133. handled:=true;
  134. end;
  135. end;
  136. function tjvminlinenode.first_copy: tnode;
  137. var
  138. ppn: tcallparanode;
  139. arr, len, start: tnode;
  140. eledef: tdef;
  141. counter, ndims: longint;
  142. finaltype: char;
  143. begin
  144. if is_dynamic_array(resultdef) then
  145. begin
  146. ppn:=tcallparanode(left);
  147. counter:=1;
  148. while assigned(ppn.right) do
  149. begin
  150. inc(counter);
  151. ppn:=tcallparanode(ppn.right);
  152. end;
  153. if (counter=3) then
  154. begin
  155. len:=tcallparanode(left).left;
  156. tcallparanode(left).left:=nil;
  157. start:=tcallparanode(tcallparanode(left).right).left;
  158. tcallparanode(tcallparanode(left).right).left:=nil;
  159. { free the original start/len paras and remove them }
  160. ppn:=tcallparanode(left);
  161. left:=tcallparanode(tcallparanode(left).right).right;
  162. tcallparanode(ppn.right).right:=nil;
  163. ppn.free;
  164. end
  165. else
  166. begin
  167. { use special -1,-1 argument to copy the whole array }
  168. len:=genintconstnode(-1);
  169. start:=genintconstnode(-1);
  170. end;
  171. { currently there is one parameter left: the array itself }
  172. arr:=tcallparanode(left).left;
  173. tcallparanode(left).left:=nil;
  174. { in case it's a dynamic array of static arrays, get the dimensions
  175. of the static array components }
  176. eledef:=tarraydef(resultdef).elementdef;
  177. ndims:=1;
  178. while (eledef.typ=arraydef) and
  179. not is_dynamic_array(eledef) do
  180. begin
  181. inc(ndims);
  182. eledef:=tarraydef(eledef).elementdef;
  183. end;
  184. { get the final element kind }
  185. finaltype:=jvmarrtype_setlength(eledef);
  186. { construct the call to
  187. fpc_dynarray_copy(src: JLObject; start, len: longint; ndim: longint; eletype: jchar) }
  188. result:=ccallnode.createintern('FPC_DYNARRAY_COPY',
  189. ccallparanode.create(cordconstnode.create(ord(finaltype),cwidechartype,false),
  190. ccallparanode.create(genintconstnode(ndims),
  191. ccallparanode.create(len,
  192. ccallparanode.create(start,
  193. ccallparanode.create(ctypeconvnode.create_explicit(arr,java_jlobject),nil)
  194. )
  195. )
  196. )
  197. )
  198. );
  199. inserttypeconv_explicit(result,resultdef);
  200. end
  201. else
  202. result:=inherited first_copy;
  203. end;
  204. function tjvminlinenode.first_assigned: tnode;
  205. begin
  206. { on the JVM target, empty arrays can also be <> nil but have length 0
  207. instead. Since assigned(dynarray) is only used to determine whether
  208. the length is <> 0 on other targets, replace this expression here }
  209. if is_dynamic_array(tcallparanode(left).left.resultdef) then
  210. begin
  211. result:=caddnode.create(unequaln,cinlinenode.create(
  212. in_length_x,false,tcallparanode(left).left),genintconstnode(0));
  213. tcallparanode(left).left:=nil;
  214. end
  215. else
  216. result:=inherited;
  217. end;
  218. function tjvminlinenode.first_get_frame: tnode;
  219. begin
  220. { no frame pointer on the JVM target }
  221. result:=cnilnode.create;
  222. end;
  223. function tjvminlinenode.first_box: tnode;
  224. var
  225. boxdef,
  226. boxparadef: tdef;
  227. begin
  228. { get class wrapper type }
  229. jvmgetboxtype(left.resultdef,boxdef,boxparadef,true);
  230. { created wrapped instance }
  231. inserttypeconv_explicit(tcallparanode(left).left,boxparadef);
  232. result:=ccallnode.createinternmethod(
  233. cloadvmtaddrnode.create(ctypenode.create(tobjectdef(boxdef))),'CREATE',left);
  234. { reused }
  235. left:=nil;
  236. end;
  237. function tjvminlinenode.first_unbox: tnode;
  238. var
  239. val: tnode;
  240. boxdef,
  241. boxparadef: tdef;
  242. begin
  243. jvmgetboxtype(resultdef,boxdef,boxparadef,true);
  244. val:=tcallparanode(tcallparanode(left).right).left;
  245. tcallparanode(tcallparanode(left).right).left:=nil;
  246. { typecast to the boxing type }
  247. val:=ctypeconvnode.create_explicit(val,boxdef);
  248. { call the unboxing method }
  249. val:=ccallnode.createinternmethod(val,jvmgetunboxmethod(resultdef),nil);
  250. { add type conversion for shortint -> byte etc }
  251. inserttypeconv_explicit(val,resultdef);
  252. result:=val;
  253. end;
  254. function tjvminlinenode.pass_typecheck: tnode;
  255. var
  256. handled: boolean;
  257. begin
  258. result:=nil;
  259. handled:=false;
  260. case inlinenumber of
  261. in_length_x:
  262. begin
  263. result:=typecheck_length(handled);
  264. end;
  265. in_high_x:
  266. begin
  267. result:=typecheck_high(handled);
  268. end;
  269. in_new_x:
  270. begin
  271. result:=typecheck_new(handled);
  272. end;
  273. in_sizeof_x:
  274. begin
  275. { can't get the size of the data of a class/object }
  276. if left.resultdef.typ in [objectdef,classrefdef] then
  277. Message(parser_e_illegal_expression);
  278. end;
  279. else
  280. ;
  281. end;
  282. if not handled then
  283. result:=inherited pass_typecheck;
  284. end;
  285. (*
  286. function tjvminlinenode.first_sqrt_real : tnode;
  287. begin
  288. if (current_settings.cputype >= cpu_PPC970) then
  289. begin
  290. expectloc:=LOC_FPUREGISTER;
  291. first_sqrt_real := nil;
  292. end
  293. else
  294. result:=inherited first_sqrt_real;
  295. end;
  296. *)
  297. function tjvminlinenode.first_sqr_real : tnode;
  298. begin
  299. expectloc:=LOC_FPUREGISTER;
  300. first_sqr_real:=nil;
  301. end;
  302. function tjvminlinenode.first_trunc_real : tnode;
  303. begin
  304. expectloc:=LOC_REGISTER;
  305. first_trunc_real:=nil;
  306. end;
  307. function tjvminlinenode.first_new: tnode;
  308. begin
  309. { skip the array; it's a type node }
  310. tcallparanode(tcallparanode(left).right).firstcallparan;
  311. expectloc:=LOC_REGISTER;
  312. result:=nil;
  313. end;
  314. function tjvminlinenode.first_IncludeExclude: tnode;
  315. var
  316. setpara: tnode;
  317. valuepara: tcallparanode;
  318. seteledef: tdef;
  319. procname: string[6];
  320. begin
  321. setpara:=tcallparanode(left).left;
  322. tcallparanode(left).left:=nil;
  323. valuepara:=tcallparanode(tcallparanode(left).right);
  324. tcallparanode(left).right:=nil;
  325. seteledef:=tsetdef(setpara.resultdef).elementdef;
  326. setpara:=caddrnode.create_internal(setpara);
  327. include(taddrnode(setpara).addrnodeflags,anf_typedaddr);
  328. if seteledef.typ=enumdef then
  329. begin
  330. inserttypeconv_explicit(setpara,java_juenumset);
  331. inserttypeconv_explicit(valuepara.left,tcpuenumdef(tenumdef(seteledef).getbasedef).classdef);
  332. end
  333. else
  334. begin
  335. inserttypeconv_explicit(setpara,java_jubitset);
  336. inserttypeconv_explicit(valuepara.left,s32inttype);
  337. end;
  338. if inlinenumber=in_include_x_y then
  339. procname:='ADD'
  340. else
  341. procname:='REMOVE';
  342. result:=ccallnode.createinternmethod(setpara,procname,valuepara);
  343. end;
  344. function tjvminlinenode.first_setlength_array: tnode;
  345. var
  346. assignmenttarget,
  347. ppn,
  348. newparas: tnode;
  349. newnode: tnode;
  350. eledef,
  351. objarraydef: tdef;
  352. ndims: longint;
  353. finaltype: char;
  354. setlenroutine: string;
  355. lefttemp: ttempcreatenode;
  356. newblock: tblocknode;
  357. newstatement: tstatementnode;
  358. begin
  359. { first parameter is the array, the rest are the dimensions }
  360. newparas:=tcallparanode(left).right;
  361. tcallparanode(left).right:=nil;
  362. { count the number of specified dimensions, and determine the type of
  363. the final one }
  364. ppn:=newparas;
  365. eledef:=tarraydef(left.resultdef).elementdef;
  366. { ppn already points to the first dimension }
  367. ndims:=1;
  368. while assigned(tcallparanode(ppn).right) do
  369. begin
  370. inc(ndims);
  371. eledef:=tarraydef(eledef).elementdef;
  372. ppn:=tcallparanode(ppn).right;
  373. end;
  374. { in case it's a dynamic array of static arrays, we must also allocate
  375. the static arrays! }
  376. while (eledef.typ=arraydef) and
  377. not is_dynamic_array(eledef) do
  378. begin
  379. inc(ndims);
  380. tcallparanode(ppn).right:=
  381. ccallparanode.create(
  382. genintconstnode(tarraydef(eledef).elecount),nil);
  383. ppn:=tcallparanode(ppn).right;
  384. eledef:=tarraydef(eledef).elementdef;
  385. end;
  386. { prepend type parameter for the array }
  387. newparas:=ccallparanode.create(ctypenode.create(left.resultdef),newparas);
  388. ttypenode(tcallparanode(newparas).left).allowed:=true;
  389. { node to create the new array }
  390. newnode:=cinlinenode.create(in_new_x,false,newparas);
  391. { Common parameters for setlength helper }
  392. { start with org (save assignmenttarget itself to assign the result back to) }
  393. { store left into a temp in case it may contain a function call
  394. (which must not be evaluated twice) }
  395. newblock:=nil;
  396. newstatement:=nil;
  397. lefttemp:=maybereplacewithtempref(tcallparanode(left).left,newblock,newstatement,tcallparanode(left).left.resultdef.size,false);
  398. if assigned(lefttemp) then
  399. begin
  400. assignmenttarget:=ctemprefnode.create(lefttemp);
  401. typecheckpass(tnode(assignmenttarget));
  402. end
  403. else
  404. assignmenttarget:=tcallparanode(left).left.getcopy;
  405. newparas:=left;
  406. left:=nil;
  407. finaltype:=jvmarrtype_setlength(eledef);
  408. { since the setlength prototypes require certain types, insert
  409. explicit type conversions where necessary }
  410. objarraydef:=nil;
  411. if (ndims>1) then
  412. begin
  413. { expects array of JLObject }
  414. setlenroutine:='FPC_SETLENGTH_DYNARR_MULTIDIM';
  415. objarraydef:=search_system_type('TJOBJECTARRAY').typedef
  416. end
  417. else
  418. begin
  419. case finaltype of
  420. 'R':
  421. begin
  422. { expects array of FpcBaseRecord}
  423. setlenroutine:='FPC_SETLENGTH_DYNARR_JRECORD';
  424. objarraydef:=search_system_type('TJRECORDARRAY').typedef;
  425. end;
  426. 'T':
  427. begin
  428. { expects array of ShortstringClass}
  429. setlenroutine:='FPC_SETLENGTH_DYNARR_JSHORTSTRING';
  430. objarraydef:=search_system_type('TSHORTSTRINGARRAY').typedef;
  431. end;
  432. else
  433. begin
  434. { expects JLObject }
  435. setlenroutine:='FPC_SETLENGTH_DYNARR_GENERIC';
  436. objarraydef:=java_jlobject;
  437. end
  438. end;
  439. end;
  440. tcallparanode(newparas).left:=ctypeconvnode.create_explicit(tcallparanode(newparas).left,objarraydef);
  441. newnode:=ctypeconvnode.create_explicit(newnode,objarraydef);
  442. { prepend new }
  443. newparas:=ccallparanode.create(newnode,newparas);
  444. { prepend deepcopy }
  445. newparas:=ccallparanode.create(cordconstnode.create(0,pasbool1type,false),newparas);
  446. { call the right setlenght helper }
  447. if ndims>1 then
  448. begin
  449. { create proper parameters, from right to left:
  450. eletype=finaltype, ndim=ndims, deepcopy=false, new=newnode,
  451. assignmenttarget=tcallparanode(left).left }
  452. { prepend ndim }
  453. newparas:=ccallparanode.create(cordconstnode.create(ndims,s32inttype,false),newparas);
  454. { prepend eletype }
  455. newparas:=ccallparanode.create(cordconstnode.create(ord(finaltype),cwidechartype,false),newparas);
  456. end
  457. else
  458. begin
  459. { create proper parameters, from right to left:
  460. deepcopy=false, new=newnode, assignmenttarget=tcallparnode(left).left
  461. -> already done in common part above }
  462. end;
  463. result:=ccallnode.createintern(setlenroutine,newparas);
  464. { assign result back to org (no call-by-reference for Java) }
  465. result:=cassignmentnode.create(assignmenttarget,
  466. ctypeconvnode.create_explicit(result,assignmenttarget.resultdef));
  467. if assigned(lefttemp) then
  468. begin
  469. addstatement(newstatement,result);
  470. addstatement(newstatement,ctempdeletenode.create(lefttemp));
  471. result:=newblock;
  472. end;
  473. end;
  474. function tjvminlinenode.first_setlength: tnode;
  475. begin
  476. { reverse the parameter order so we can process them more easily }
  477. reverseparameters(tcallparanode(left));
  478. { treat setlength(x,0) specially: used to init uninitialised locations }
  479. if not is_shortstring(left.resultdef) and
  480. not assigned(tcallparanode(tcallparanode(left).right).right) and
  481. is_constintnode(tcallparanode(tcallparanode(left).right).left) and
  482. (tordconstnode(tcallparanode(tcallparanode(left).right).left).value=0) then
  483. begin
  484. result:=nil;
  485. expectloc:=LOC_VOID;
  486. exit;
  487. end;
  488. { strings are handled the same as on other platforms }
  489. if left.resultdef.typ=stringdef then
  490. begin
  491. reverseparameters(tcallparanode(left));
  492. result:=inherited first_setlength;
  493. exit;
  494. end;
  495. case left.resultdef.typ of
  496. arraydef:
  497. result:=first_setlength_array;
  498. else
  499. internalerror(2011031204);
  500. end;
  501. end;
  502. function tjvminlinenode.first_length: tnode;
  503. var
  504. newblock: tblocknode;
  505. newstatement: tstatementnode;
  506. lefttemp,
  507. lentemp: ttempcreatenode;
  508. ifcond,
  509. stringtemp,
  510. stringnonnull,
  511. stringnull: tnode;
  512. psym: tsym;
  513. stringclass: tdef;
  514. begin
  515. if is_wide_or_unicode_string(left.resultdef) or
  516. is_ansistring(left.resultdef) then
  517. begin
  518. { if assigned(stringclass(left)) then
  519. lentemp:=stringclass(left).length()
  520. else
  521. lentemp:=0;
  522. --> return lentemp
  523. }
  524. if is_ansistring(left.resultdef) then
  525. stringclass:=java_ansistring
  526. else
  527. stringclass:=java_jlstring;
  528. newblock:=internalstatements(newstatement);
  529. { store left into a temp since it may contain a function call
  530. (which must not be evaluated twice) }
  531. if node_complexity(left)>4 then
  532. begin
  533. lefttemp:=ctempcreatenode.create_value(stringclass,stringclass.size,tt_persistent,true,ctypeconvnode.create_explicit(left,stringclass));
  534. addstatement(newstatement,lefttemp);
  535. stringtemp:=ctemprefnode.create(lefttemp)
  536. end
  537. else
  538. begin
  539. lefttemp:=nil;
  540. stringtemp:=ctypeconvnode.create_explicit(left,stringclass);
  541. end;
  542. left:=nil;
  543. lentemp:=ctempcreatenode.create(s32inttype,s32inttype.size,tt_persistent,true);
  544. addstatement(newstatement,lentemp);
  545. { if-condition: assigned(stringclass(stringvar))? }
  546. ifcond:=cinlinenode.create(in_assigned_x,false,
  547. ccallparanode.create(stringtemp.getcopy,nil));
  548. { then-path: call length() method }
  549. psym:=search_struct_member(tabstractrecorddef(stringclass),'LENGTH');
  550. if not assigned(psym) or
  551. (psym.typ<>procsym) then
  552. internalerror(2011031403);
  553. stringnonnull:=cassignmentnode.create(
  554. ctemprefnode.create(lentemp),
  555. ccallnode.create(nil,tprocsym(psym),psym.owner,stringtemp,[],nil));
  556. { else-path: length is 0 }
  557. stringnull:=cassignmentnode.create(
  558. ctemprefnode.create(lentemp),
  559. genintconstnode(0));
  560. { complete if-statement }
  561. addstatement(newstatement,cifnode.create(ifcond,stringnonnull,stringnull));
  562. { free lefttemp }
  563. if assigned(lefttemp) then
  564. addstatement(newstatement,ctempdeletenode.create(lefttemp));
  565. { return len temp }
  566. addstatement(newstatement,ctempdeletenode.create_normal_temp(lentemp));
  567. addstatement(newstatement,ctemprefnode.create(lentemp));
  568. result:=newblock;
  569. end
  570. else if is_shortstring(left.resultdef) then
  571. begin
  572. psym:=search_struct_member(tabstractrecorddef(java_shortstring),'LENGTH');
  573. if not assigned(psym) or
  574. (psym.typ<>procsym) then
  575. internalerror(2011052402);
  576. result:=
  577. ccallnode.create(nil,tprocsym(psym),psym.owner,
  578. ctypeconvnode.create_explicit(caddrnode.create_internal(left),java_shortstring),[],nil);
  579. { reused }
  580. left:=nil;
  581. end
  582. { should be no other string types }
  583. else if left.resultdef.typ=stringdef then
  584. internalerror(2011052403)
  585. else
  586. result:=inherited first_length;
  587. end;
  588. procedure tjvminlinenode.second_length;
  589. begin
  590. if is_dynamic_array(left.resultdef) or
  591. is_open_array(left.resultdef) or
  592. is_array_of_const(left.resultdef) then
  593. begin
  594. location_reset(location,LOC_REGISTER,OS_S32);
  595. location.register:=hlcg.getintregister(current_asmdata.CurrAsmList,s32inttype);
  596. secondpass(left);
  597. thlcgjvm(hlcg).g_getarraylen(current_asmdata.CurrAsmList,left.location);
  598. thlcgjvm(hlcg).a_load_stack_reg(current_asmdata.CurrAsmList,resultdef,location.register);
  599. end
  600. else
  601. internalerror(2011012004);
  602. end;
  603. (*
  604. function tjvminlinenode.first_round_real : tnode;
  605. begin
  606. if (current_settings.cputype >= cpu_PPC970) then
  607. begin
  608. expectloc:=LOC_REFERENCE;
  609. first_round_real := nil;
  610. end
  611. else
  612. result:=inherited first_round_real;
  613. end;
  614. *)
  615. { load the FPU value on the evaluation stack }
  616. procedure tjvminlinenode.load_fpu_location;
  617. begin
  618. secondpass(left);
  619. thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  620. end;
  621. (*
  622. procedure tjvminlinenode.second_sqrt_real;
  623. begin
  624. if (current_settings.cputype < cpu_PPC970) then
  625. internalerror(2007020910);
  626. location.loc:=LOC_FPUREGISTER;
  627. load_fpu_location;
  628. case left.location.size of
  629. OS_F32:
  630. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FSQRTS,location.register,
  631. left.location.register));
  632. OS_F64:
  633. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FSQRT,location.register,
  634. left.location.register));
  635. else
  636. inherited;
  637. end;
  638. end;
  639. *)
  640. procedure tjvminlinenode.second_sqr_real;
  641. begin
  642. load_fpu_location;
  643. location_reset(location,LOC_FPUREGISTER,location.size);
  644. location.register:=hlcg.getfpuregister(current_asmdata.CurrAsmList,resultdef);
  645. case left.location.size of
  646. OS_F32:
  647. begin
  648. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_dup));
  649. thlcgjvm(hlcg).incstack(current_asmdata.CurrAsmList,1);
  650. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_fmul));
  651. thlcgjvm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  652. end;
  653. OS_F64:
  654. begin
  655. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_dup2));
  656. thlcgjvm(hlcg).incstack(current_asmdata.CurrAsmList,2);
  657. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_dmul));
  658. thlcgjvm(hlcg).decstack(current_asmdata.CurrAsmList,2);
  659. end;
  660. else
  661. internalerror(2011010804);
  662. end;
  663. thlcgjvm(hlcg).a_load_stack_reg(current_asmdata.CurrAsmList,resultdef,location.register);
  664. end;
  665. procedure tjvminlinenode.second_trunc_real;
  666. begin
  667. load_fpu_location;
  668. location_reset(location,LOC_REGISTER,left.location.size);
  669. location.register:=hlcg.getintregister(current_asmdata.CurrAsmList,resultdef);
  670. case left.location.size of
  671. OS_F32:
  672. begin
  673. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_f2l));
  674. { 32 bit float -> 64 bit int: +1 stack slot }
  675. thlcgjvm(hlcg).incstack(current_asmdata.CurrAsmList,1);
  676. end;
  677. OS_F64:
  678. begin
  679. { 64 bit float -> 64 bit int: same number of stack slots }
  680. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_d2l));
  681. end;
  682. else
  683. internalerror(2011010805);
  684. end;
  685. thlcgjvm(hlcg).a_load_stack_reg(current_asmdata.CurrAsmList,resultdef,location.register);
  686. end;
  687. procedure tjvminlinenode.second_new;
  688. var
  689. arr: tnode;
  690. hp: tcallparanode;
  691. paracount: longint;
  692. begin
  693. hp:=tcallparanode(left);
  694. { we don't second pass this one, it's only a type node }
  695. arr:=hp.left;
  696. if not is_dynamic_array(arr.resultdef) then
  697. internalerror(2011012204);
  698. hp:=tcallparanode(hp.right);
  699. if not assigned(hp) then
  700. internalerror(2011012205);
  701. paracount:=0;
  702. { put all the dimensions on the stack }
  703. repeat
  704. inc(paracount);
  705. secondpass(hp.left);
  706. thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,hp.left.resultdef,hp.left.location);
  707. hp:=tcallparanode(hp.right);
  708. until not assigned(hp);
  709. { create the array }
  710. thlcgjvm(hlcg).g_newarray(current_asmdata.CurrAsmList,arr.resultdef,paracount);
  711. location_reset(location,LOC_REGISTER,OS_ADDR);
  712. location.register:=hlcg.getaddressregister(current_asmdata.CurrAsmList,resultdef);
  713. thlcgjvm(hlcg).a_load_stack_reg(current_asmdata.CurrAsmList,arr.resultdef,location.register);
  714. end;
  715. procedure tjvminlinenode.second_setlength;
  716. var
  717. target: tnode;
  718. lenpara: tnode;
  719. emptystr: ansichar;
  720. tmpreg: tregister;
  721. begin
  722. target:=tcallparanode(left).left;
  723. lenpara:=tcallparanode(tcallparanode(left).right).left;
  724. if assigned(tcallparanode(tcallparanode(left).right).right) or
  725. not is_constintnode(lenpara) or
  726. (tordconstnode(lenpara).value<>0) then
  727. internalerror(2011031801);
  728. secondpass(target);
  729. { can't directly load from stack to destination, because if target is
  730. a reference then its address must be placed on the stack before the
  731. value }
  732. tmpreg:=hlcg.getaddressregister(current_asmdata.CurrAsmList,target.resultdef);
  733. if is_wide_or_unicode_string(target.resultdef) then
  734. begin
  735. emptystr:=#0;
  736. current_asmdata.CurrAsmList.concat(taicpu.op_string(a_ldc,0,@emptystr));
  737. thlcgjvm(hlcg).incstack(current_asmdata.CurrAsmList,1);
  738. end
  739. else if is_ansistring(target.resultdef) then
  740. thlcgjvm(hlcg).a_load_const_stack(current_asmdata.CurrAsmList,java_jlobject,0,R_ADDRESSREGISTER)
  741. else if is_dynamic_array(target.resultdef) then
  742. begin
  743. thlcgjvm(hlcg).a_load_const_stack(current_asmdata.CurrAsmList,s32inttype,0,R_INTREGISTER);
  744. thlcgjvm(hlcg).g_newarray(current_asmdata.CurrAsmList,target.resultdef,1);
  745. end
  746. else
  747. internalerror(2011031401);
  748. thlcgjvm(hlcg).a_load_stack_reg(current_asmdata.CurrAsmList,target.resultdef,tmpreg);
  749. thlcgjvm(hlcg).a_load_reg_loc(current_asmdata.CurrAsmList,target.resultdef,target.resultdef,tmpreg,target.location);
  750. end;
  751. begin
  752. cinlinenode:=tjvminlinenode;
  753. end.