njvminl.pas 29 KB

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