njvminl.pas 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  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_assert: 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,
  59. aasmbase,aasmtai,aasmdata,aasmcpu,
  60. symtype,symconst,symdef,symsym,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. 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_assert: tnode;
  216. var
  217. paras: tcallparanode;
  218. begin
  219. paras:=tcallparanode(tcallparanode(left).right);
  220. paras:=ccallparanode.create(cstringconstnode.createstr(current_module.sourcefiles.get_file_name(current_filepos.fileindex)),paras);
  221. paras:=ccallparanode.create(genintconstnode(fileinfo.line),paras);
  222. result:=cifnode.create(cnotnode.create(tcallparanode(left).left),
  223. ccallnode.createintern('fpc_assert',paras),nil);
  224. tcallparanode(left).left:=nil;
  225. tcallparanode(left).right:=nil;
  226. end;
  227. function tjvminlinenode.first_box: tnode;
  228. var
  229. boxdef,
  230. boxparadef: tdef;
  231. begin
  232. { get class wrapper type }
  233. jvmgetboxtype(left.resultdef,boxdef,boxparadef,true);
  234. { created wrapped instance }
  235. inserttypeconv_explicit(tcallparanode(left).left,boxparadef);
  236. result:=ccallnode.createinternmethod(
  237. cloadvmtaddrnode.create(ctypenode.create(tobjectdef(boxdef))),'CREATE',left);
  238. { reused }
  239. left:=nil;
  240. end;
  241. function tjvminlinenode.first_unbox: tnode;
  242. var
  243. val: tnode;
  244. boxdef,
  245. boxparadef: tdef;
  246. begin
  247. jvmgetboxtype(resultdef,boxdef,boxparadef,true);
  248. val:=tcallparanode(tcallparanode(left).right).left;
  249. tcallparanode(tcallparanode(left).right).left:=nil;
  250. { typecast to the boxing type }
  251. val:=ctypeconvnode.create_explicit(val,boxdef);
  252. { call the unboxing method }
  253. val:=ccallnode.createinternmethod(val,jvmgetunboxmethod(resultdef),nil);
  254. { add type conversion for shortint -> byte etc }
  255. inserttypeconv_explicit(val,resultdef);
  256. result:=val;
  257. end;
  258. function tjvminlinenode.pass_typecheck: tnode;
  259. var
  260. handled: boolean;
  261. begin
  262. handled:=false;
  263. case inlinenumber of
  264. in_length_x:
  265. begin
  266. result:=typecheck_length(handled);
  267. end;
  268. in_high_x:
  269. begin
  270. result:=typecheck_high(handled);
  271. end;
  272. in_new_x:
  273. begin
  274. result:=typecheck_new(handled);
  275. end;
  276. end;
  277. if not handled then
  278. result:=inherited pass_typecheck;
  279. end;
  280. (*
  281. function tjvminlinenode.first_sqrt_real : tnode;
  282. begin
  283. if (current_settings.cputype >= cpu_PPC970) then
  284. begin
  285. expectloc:=LOC_FPUREGISTER;
  286. first_sqrt_real := nil;
  287. end
  288. else
  289. result:=inherited first_sqrt_real;
  290. end;
  291. *)
  292. function tjvminlinenode.first_sqr_real : tnode;
  293. begin
  294. expectloc:=LOC_FPUREGISTER;
  295. first_sqr_real:=nil;
  296. end;
  297. function tjvminlinenode.first_trunc_real : tnode;
  298. begin
  299. expectloc:=LOC_REGISTER;
  300. first_trunc_real:=nil;
  301. end;
  302. function tjvminlinenode.first_new: tnode;
  303. begin
  304. { skip the array; it's a type node }
  305. tcallparanode(tcallparanode(left).right).firstcallparan;
  306. expectloc:=LOC_REGISTER;
  307. result:=nil;
  308. end;
  309. function tjvminlinenode.first_IncludeExclude: tnode;
  310. var
  311. setpara: tnode;
  312. valuepara: tcallparanode;
  313. seteledef: tdef;
  314. procname: string[6];
  315. begin
  316. setpara:=tcallparanode(left).left;
  317. tcallparanode(left).left:=nil;
  318. valuepara:=tcallparanode(tcallparanode(left).right);
  319. tcallparanode(left).right:=nil;
  320. seteledef:=tsetdef(setpara.resultdef).elementdef;
  321. setpara:=caddrnode.create_internal(setpara);
  322. include(setpara.flags,nf_typedaddr);
  323. if seteledef.typ=enumdef then
  324. begin
  325. inserttypeconv_explicit(setpara,java_juenumset);
  326. inserttypeconv_explicit(valuepara.left,tenumdef(seteledef).getbasedef.classdef);
  327. end
  328. else
  329. begin
  330. inserttypeconv_explicit(setpara,java_jubitset);
  331. inserttypeconv_explicit(valuepara.left,s32inttype);
  332. end;
  333. if inlinenumber=in_include_x_y then
  334. procname:='ADD'
  335. else
  336. procname:='REMOVE';
  337. result:=ccallnode.createinternmethod(setpara,procname,valuepara);
  338. end;
  339. function tjvminlinenode.first_setlength_array: tnode;
  340. var
  341. assignmenttarget,
  342. ppn,
  343. newparas: tnode;
  344. newnode: tnode;
  345. eledef,
  346. objarraydef: tdef;
  347. ndims: longint;
  348. finaltype: char;
  349. setlenroutine: string;
  350. lefttemp: ttempcreatenode;
  351. newblock: tblocknode;
  352. newstatement: tstatementnode;
  353. primitive: boolean;
  354. begin
  355. { first parameter is the array, the rest are the dimensions }
  356. newparas:=tcallparanode(left).right;
  357. tcallparanode(left).right:=nil;
  358. { count the number of specified dimensions, and determine the type of
  359. the final one }
  360. ppn:=newparas;
  361. eledef:=tarraydef(left.resultdef).elementdef;
  362. { ppn already points to the first dimension }
  363. ndims:=1;
  364. while assigned(tcallparanode(ppn).right) do
  365. begin
  366. inc(ndims);
  367. eledef:=tarraydef(eledef).elementdef;
  368. ppn:=tcallparanode(ppn).right;
  369. end;
  370. { in case it's a dynamic array of static arrays, we must also allocate
  371. the static arrays! }
  372. while (eledef.typ=arraydef) and
  373. not is_dynamic_array(eledef) do
  374. begin
  375. inc(ndims);
  376. tcallparanode(ppn).right:=
  377. ccallparanode.create(
  378. genintconstnode(tarraydef(eledef).elecount),nil);
  379. ppn:=tcallparanode(ppn).right;
  380. eledef:=tarraydef(eledef).elementdef;
  381. end;
  382. { prepend type parameter for the array }
  383. newparas:=ccallparanode.create(ctypenode.create(left.resultdef),newparas);
  384. ttypenode(tcallparanode(newparas).left).allowed:=true;
  385. { node to create the new array }
  386. newnode:=cinlinenode.create(in_new_x,false,newparas);
  387. { Common parameters for setlength helper }
  388. { start with org (save assignmenttarget itself to assign the result back to) }
  389. { store left into a temp in case it may contain a function call
  390. (which must not be evaluated twice) }
  391. newblock:=nil;
  392. newstatement:=nil;
  393. lefttemp:=maybereplacewithtempref(tcallparanode(left).left,newblock,newstatement,tcallparanode(left).left.resultdef.size,false);
  394. if assigned(lefttemp) then
  395. begin
  396. assignmenttarget:=ctemprefnode.create(lefttemp);
  397. typecheckpass(tnode(assignmenttarget));
  398. end
  399. else
  400. assignmenttarget:=tcallparanode(left).left.getcopy;
  401. newparas:=left;
  402. left:=nil;
  403. finaltype:=jvmarrtype_setlength(eledef);
  404. { since the setlength prototypes require certain types, insert
  405. explicit type conversions where necessary }
  406. objarraydef:=nil;
  407. if (ndims>1) then
  408. begin
  409. { expects array of JLObject }
  410. setlenroutine:='FPC_SETLENGTH_DYNARR_MULTIDIM';
  411. objarraydef:=search_system_type('TJOBJECTARRAY').typedef
  412. end
  413. else
  414. begin
  415. case finaltype of
  416. 'R':
  417. begin
  418. { expects array of FpcBaseRecord}
  419. setlenroutine:='FPC_SETLENGTH_DYNARR_JRECORD';
  420. objarraydef:=search_system_type('TJRECORDARRAY').typedef;
  421. end;
  422. 'T':
  423. begin
  424. { expects array of ShortstringClass}
  425. setlenroutine:='FPC_SETLENGTH_DYNARR_JSHORTSTRING';
  426. objarraydef:=search_system_type('TSHORTSTRINGARRAY').typedef;
  427. end;
  428. else
  429. begin
  430. { expects JLObject }
  431. setlenroutine:='FPC_SETLENGTH_DYNARR_GENERIC';
  432. objarraydef:=java_jlobject;
  433. end
  434. end;
  435. end;
  436. tcallparanode(newparas).left:=ctypeconvnode.create_explicit(tcallparanode(newparas).left,objarraydef);
  437. newnode:=ctypeconvnode.create_explicit(newnode,objarraydef);
  438. { prepend new }
  439. newparas:=ccallparanode.create(newnode,newparas);
  440. { prepend deepcopy }
  441. newparas:=ccallparanode.create(cordconstnode.create(0,pasbool8type,false),newparas);
  442. { call the right setlenght helper }
  443. if ndims>1 then
  444. begin
  445. { create proper parameters, from right to left:
  446. eletype=finaltype, ndim=ndims, deepcopy=false, new=newnode,
  447. assignmenttarget=tcallparanode(left).left }
  448. { prepend ndim }
  449. newparas:=ccallparanode.create(cordconstnode.create(ndims,s32inttype,false),newparas);
  450. { prepend eletype }
  451. newparas:=ccallparanode.create(cordconstnode.create(ord(finaltype),cwidechartype,false),newparas);
  452. end
  453. else
  454. begin
  455. { create proper parameters, from right to left:
  456. deepcopy=false, new=newnode, assignmenttarget=tcallparnode(left).left
  457. -> already done in common part above }
  458. end;
  459. result:=ccallnode.createintern(setlenroutine,newparas);
  460. { assign result back to org (no call-by-reference for Java) }
  461. result:=cassignmentnode.create(assignmenttarget,
  462. ctypeconvnode.create_explicit(result,assignmenttarget.resultdef));
  463. if assigned(lefttemp) then
  464. begin
  465. addstatement(newstatement,result);
  466. addstatement(newstatement,ctempdeletenode.create(lefttemp));
  467. result:=newblock;
  468. end;
  469. end;
  470. function tjvminlinenode.first_setlength: tnode;
  471. begin
  472. { reverse the parameter order so we can process them more easily }
  473. left:=reverseparameters(tcallparanode(left));
  474. { treat setlength(x,0) specially: used to init uninitialised locations }
  475. if not is_shortstring(left.resultdef) and
  476. not assigned(tcallparanode(tcallparanode(left).right).right) and
  477. is_constintnode(tcallparanode(tcallparanode(left).right).left) and
  478. (tordconstnode(tcallparanode(tcallparanode(left).right).left).value=0) then
  479. begin
  480. result:=nil;
  481. expectloc:=LOC_VOID;
  482. exit;
  483. end;
  484. { strings are handled the same as on other platforms }
  485. if left.resultdef.typ=stringdef then
  486. begin
  487. left:=reverseparameters(tcallparanode(left));
  488. result:=inherited first_setlength;
  489. exit;
  490. end;
  491. case left.resultdef.typ of
  492. arraydef:
  493. result:=first_setlength_array;
  494. else
  495. internalerror(2011031204);
  496. end;
  497. end;
  498. function tjvminlinenode.first_length: tnode;
  499. var
  500. newblock: tblocknode;
  501. newstatement: tstatementnode;
  502. lefttemp,
  503. lentemp: ttempcreatenode;
  504. ifcond,
  505. stringtemp,
  506. stringnonnull,
  507. stringnull: tnode;
  508. psym: tsym;
  509. stringclass: tdef;
  510. begin
  511. if is_wide_or_unicode_string(left.resultdef) or
  512. is_ansistring(left.resultdef) then
  513. begin
  514. { if assigned(stringclass(left)) then
  515. lentemp:=stringclass(left).length()
  516. else
  517. lentemp:=0;
  518. --> return lentemp
  519. }
  520. if is_ansistring(left.resultdef) then
  521. stringclass:=java_ansistring
  522. else
  523. stringclass:=java_jlstring;
  524. newblock:=internalstatements(newstatement);
  525. { store left into a temp since it may contain a function call
  526. (which must not be evaluated twice) }
  527. if node_complexity(left)>4 then
  528. begin
  529. lefttemp:=ctempcreatenode.create_value(stringclass,stringclass.size,tt_persistent,true,ctypeconvnode.create_explicit(left,stringclass));
  530. addstatement(newstatement,lefttemp);
  531. stringtemp:=ctemprefnode.create(lefttemp)
  532. end
  533. else
  534. begin
  535. lefttemp:=nil;
  536. stringtemp:=left;
  537. end;
  538. left:=nil;
  539. lentemp:=ctempcreatenode.create(s32inttype,s32inttype.size,tt_persistent,true);
  540. addstatement(newstatement,lentemp);
  541. { if-condition: assigned(stringclass(stringvar))? }
  542. ifcond:=cinlinenode.create(in_assigned_x,false,
  543. ccallparanode.create(stringtemp.getcopy,nil));
  544. { then-path: call length() method }
  545. psym:=search_struct_member(tabstractrecorddef(stringclass),'LENGTH');
  546. if not assigned(psym) or
  547. (psym.typ<>procsym) then
  548. internalerror(2011031403);
  549. stringnonnull:=cassignmentnode.create(
  550. ctemprefnode.create(lentemp),
  551. ccallnode.create(nil,tprocsym(psym),psym.owner,stringtemp,[]));
  552. { else-path: length is 0 }
  553. stringnull:=cassignmentnode.create(
  554. ctemprefnode.create(lentemp),
  555. genintconstnode(0));
  556. { complete if-statement }
  557. addstatement(newstatement,cifnode.create(ifcond,stringnonnull,stringnull));
  558. { free lefttemp }
  559. if assigned(lefttemp) then
  560. addstatement(newstatement,ctempdeletenode.create(lefttemp));
  561. { return len temp }
  562. addstatement(newstatement,ctempdeletenode.create_normal_temp(lentemp));
  563. addstatement(newstatement,ctemprefnode.create(lentemp));
  564. result:=newblock;
  565. end
  566. else if is_shortstring(left.resultdef) then
  567. begin
  568. psym:=search_struct_member(tabstractrecorddef(java_shortstring),'LENGTH');
  569. if not assigned(psym) or
  570. (psym.typ<>procsym) then
  571. internalerror(2011052402);
  572. result:=
  573. ccallnode.create(nil,tprocsym(psym),psym.owner,
  574. ctypeconvnode.create_explicit(caddrnode.create_internal(left),java_shortstring),[]);
  575. { reused }
  576. left:=nil;
  577. end
  578. { should be no other string types }
  579. else if left.resultdef.typ=stringdef then
  580. internalerror(2011052403)
  581. else
  582. result:=inherited first_length;
  583. end;
  584. procedure tjvminlinenode.second_length;
  585. begin
  586. if is_dynamic_array(left.resultdef) or
  587. is_open_array(left.resultdef) then
  588. begin
  589. location_reset(location,LOC_REGISTER,OS_S32);
  590. location.register:=hlcg.getintregister(current_asmdata.CurrAsmList,s32inttype);
  591. secondpass(left);
  592. thlcgjvm(hlcg).g_getarraylen(current_asmdata.CurrAsmList,left.location);
  593. thlcgjvm(hlcg).a_load_stack_reg(current_asmdata.CurrAsmList,resultdef,location.register);
  594. end
  595. else
  596. internalerror(2011012004);
  597. end;
  598. (*
  599. function tjvminlinenode.first_round_real : tnode;
  600. begin
  601. if (current_settings.cputype >= cpu_PPC970) then
  602. begin
  603. expectloc:=LOC_REFERENCE;
  604. first_round_real := nil;
  605. end
  606. else
  607. result:=inherited first_round_real;
  608. end;
  609. *)
  610. { load the FPU value on the evaluation stack }
  611. procedure tjvminlinenode.load_fpu_location;
  612. begin
  613. secondpass(left);
  614. thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  615. end;
  616. (*
  617. procedure tjvminlinenode.second_sqrt_real;
  618. begin
  619. if (current_settings.cputype < cpu_PPC970) then
  620. internalerror(2007020910);
  621. location.loc:=LOC_FPUREGISTER;
  622. load_fpu_location;
  623. case left.location.size of
  624. OS_F32:
  625. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FSQRTS,location.register,
  626. left.location.register));
  627. OS_F64:
  628. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FSQRT,location.register,
  629. left.location.register));
  630. else
  631. inherited;
  632. end;
  633. end;
  634. *)
  635. procedure tjvminlinenode.second_sqr_real;
  636. begin
  637. load_fpu_location;
  638. location_reset(location,LOC_FPUREGISTER,location.size);
  639. location.register:=hlcg.getfpuregister(current_asmdata.CurrAsmList,resultdef);
  640. case left.location.size of
  641. OS_F32:
  642. begin
  643. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_dup));
  644. thlcgjvm(hlcg).incstack(current_asmdata.CurrAsmList,1);
  645. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_fmul));
  646. thlcgjvm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  647. end;
  648. OS_F64:
  649. begin
  650. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_dup2));
  651. thlcgjvm(hlcg).incstack(current_asmdata.CurrAsmList,2);
  652. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_dmul));
  653. thlcgjvm(hlcg).decstack(current_asmdata.CurrAsmList,2);
  654. end;
  655. else
  656. internalerror(2011010804);
  657. end;
  658. thlcgjvm(hlcg).a_load_stack_reg(current_asmdata.CurrAsmList,resultdef,location.register);
  659. end;
  660. procedure tjvminlinenode.second_trunc_real;
  661. begin
  662. load_fpu_location;
  663. location_reset(location,LOC_REGISTER,left.location.size);
  664. location.register:=hlcg.getintregister(current_asmdata.CurrAsmList,resultdef);
  665. case left.location.size of
  666. OS_F32:
  667. begin
  668. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_f2l));
  669. { 32 bit float -> 64 bit int: +1 stack slot }
  670. thlcgjvm(hlcg).incstack(current_asmdata.CurrAsmList,1);
  671. end;
  672. OS_F64:
  673. begin
  674. { 64 bit float -> 64 bit int: same number of stack slots }
  675. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_d2l));
  676. end;
  677. else
  678. internalerror(2011010805);
  679. end;
  680. thlcgjvm(hlcg).a_load_stack_reg(current_asmdata.CurrAsmList,resultdef,location.register);
  681. end;
  682. procedure tjvminlinenode.second_new;
  683. var
  684. arr: tnode;
  685. hp: tcallparanode;
  686. paracount: longint;
  687. begin
  688. hp:=tcallparanode(left);
  689. { we don't second pass this one, it's only a type node }
  690. arr:=hp.left;
  691. if not is_dynamic_array(arr.resultdef) then
  692. internalerror(2011012204);
  693. hp:=tcallparanode(hp.right);
  694. if not assigned(hp) then
  695. internalerror(2011012205);
  696. paracount:=0;
  697. { put all the dimensions on the stack }
  698. repeat
  699. inc(paracount);
  700. secondpass(hp.left);
  701. thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,hp.left.resultdef,hp.left.location);
  702. hp:=tcallparanode(hp.right);
  703. until not assigned(hp);
  704. { create the array }
  705. thlcgjvm(hlcg).g_newarray(current_asmdata.CurrAsmList,arr.resultdef,paracount);
  706. location_reset(location,LOC_REGISTER,OS_ADDR);
  707. location.register:=hlcg.getaddressregister(current_asmdata.CurrAsmList,resultdef);
  708. thlcgjvm(hlcg).a_load_stack_reg(current_asmdata.CurrAsmList,arr.resultdef,location.register);
  709. end;
  710. procedure tjvminlinenode.second_setlength;
  711. var
  712. target: tnode;
  713. lenpara: tnode;
  714. emptystr: ansichar;
  715. tmpreg: tregister;
  716. begin
  717. target:=tcallparanode(left).left;
  718. lenpara:=tcallparanode(tcallparanode(left).right).left;
  719. if assigned(tcallparanode(tcallparanode(left).right).right) or
  720. not is_constintnode(lenpara) or
  721. (tordconstnode(lenpara).value<>0) then
  722. internalerror(2011031801);
  723. secondpass(target);
  724. { can't directly load from stack to destination, because if target is
  725. a reference then its address must be placed on the stack before the
  726. value }
  727. tmpreg:=hlcg.getaddressregister(current_asmdata.CurrAsmList,target.resultdef);
  728. if is_wide_or_unicode_string(target.resultdef) then
  729. begin
  730. emptystr:=#0;
  731. current_asmdata.CurrAsmList.concat(taicpu.op_string(a_ldc,0,@emptystr));
  732. thlcgjvm(hlcg).incstack(current_asmdata.CurrAsmList,1);
  733. end
  734. else if is_ansistring(target.resultdef) then
  735. thlcgjvm(hlcg).a_load_const_stack(current_asmdata.CurrAsmList,java_jlobject,0,R_ADDRESSREGISTER)
  736. else if is_dynamic_array(target.resultdef) then
  737. begin
  738. thlcgjvm(hlcg).a_load_const_stack(current_asmdata.CurrAsmList,s32inttype,0,R_INTREGISTER);
  739. thlcgjvm(hlcg).g_newarray(current_asmdata.CurrAsmList,target.resultdef,1);
  740. end
  741. else
  742. internalerror(2011031401);
  743. thlcgjvm(hlcg).a_load_stack_reg(current_asmdata.CurrAsmList,target.resultdef,tmpreg);
  744. thlcgjvm(hlcg).a_load_reg_loc(current_asmdata.CurrAsmList,target.resultdef,target.resultdef,tmpreg,target.location);
  745. end;
  746. begin
  747. cinlinenode:=tjvminlinenode;
  748. end.