njvminl.pas 30 KB

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