njvminl.pas 29 KB

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