njvminl.pas 31 KB

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