njvmadd.pas 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. {
  2. Copyright (c) 2000-2011 by Florian Klaempfl and Jonas Maebe
  3. Code generation for add nodes on the JVM
  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 njvmadd;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cgbase,
  22. node,ncgadd,cpubase;
  23. type
  24. { tjvmaddnode }
  25. tjvmaddnode = class(tcgaddnode)
  26. function pass_1: tnode;override;
  27. protected
  28. function first_addstring: tnode; override;
  29. function jvm_first_addset: tnode;
  30. function cmpnode2signedtopcmp: TOpCmp;
  31. procedure second_generic_compare;
  32. procedure pass_left_right;override;
  33. procedure second_addfloat;override;
  34. procedure second_cmpfloat;override;
  35. procedure second_cmpboolean;override;
  36. procedure second_cmp64bit;override;
  37. procedure second_add64bit; override;
  38. procedure second_cmpordinal;override;
  39. end;
  40. implementation
  41. uses
  42. systems,
  43. cutils,verbose,constexp,globtype,
  44. symconst,symtable,symdef,
  45. paramgr,procinfo,pass_1,
  46. aasmtai,aasmdata,aasmcpu,defutil,
  47. hlcgobj,hlcgcpu,cgutils,
  48. cpupara,
  49. nbas,ncon,nset,nadd,ncal,ncnv,nld,nmat,nmem,
  50. njvmcon,
  51. cgobj;
  52. {*****************************************************************************
  53. tjvmaddnode
  54. *****************************************************************************}
  55. function tjvmaddnode.pass_1: tnode;
  56. begin
  57. { special handling for enums: they're classes in the JVM -> get their
  58. ordinal value to compare them (do before calling inherited pass_1,
  59. because pass_1 will convert enum constants from ordinals into class
  60. instances) }
  61. if (left.resultdef.typ=enumdef) and
  62. (right.resultdef.typ=enumdef) then
  63. begin
  64. { enums can only be compared at this stage (add/sub is only allowed
  65. in constant expressions) }
  66. if not is_boolean(resultdef) then
  67. internalerror(2011062603);
  68. inserttypeconv_explicit(left,s32inttype);
  69. inserttypeconv_explicit(right,s32inttype);
  70. end;
  71. { special handling for sets: all sets are JUBitSet/JUEnumSet on the JVM
  72. target to ease interoperability with Java code }
  73. if left.resultdef.typ=setdef then
  74. begin
  75. result:=jvm_first_addset;
  76. exit;
  77. end;
  78. result:=inherited pass_1;
  79. if expectloc=LOC_FLAGS then
  80. expectloc:=LOC_JUMP;
  81. end;
  82. function tjvmaddnode.first_addstring: tnode;
  83. var
  84. cmpfuncname: string;
  85. begin
  86. { when we get here, we are sure that both the left and the right }
  87. { node are both strings of the same stringtype (JM) }
  88. case nodetype of
  89. addn:
  90. begin
  91. if is_shortstring(resultdef) then
  92. begin
  93. result:=inherited;
  94. exit;
  95. end;
  96. { unicode/ansistring operations use functions rather than
  97. procedures for efficiency reasons (were also implemented before
  98. var-parameters were supported; may go to procedures for
  99. maintenance reasons though }
  100. if (left.nodetype=stringconstn) and (tstringconstnode(left).len=0) then
  101. begin
  102. result:=right;
  103. left.free;
  104. left:=nil;
  105. right:=nil;
  106. exit;
  107. end;
  108. if (right.nodetype=stringconstn) and (tstringconstnode(right).len=0) then
  109. begin
  110. result:=left;
  111. left:=nil;
  112. right.free;
  113. right:=nil;
  114. exit;
  115. end;
  116. { create the call to the concat routine both strings as arguments }
  117. result:=ccallnode.createintern('fpc_'+
  118. tstringdef(resultdef).stringtypname+'_concat',
  119. ccallparanode.create(right,
  120. ccallparanode.create(left,nil)));
  121. { we reused the arguments }
  122. left := nil;
  123. right := nil;
  124. end;
  125. ltn,lten,gtn,gten,equaln,unequaln :
  126. begin
  127. { call compare routine }
  128. cmpfuncname := 'fpc_'+tstringdef(left.resultdef).stringtypname+'_compare';
  129. { for equality checks use optimized version }
  130. if nodetype in [equaln,unequaln] then
  131. cmpfuncname := cmpfuncname + '_equal';
  132. result := ccallnode.createintern(cmpfuncname,
  133. ccallparanode.create(right,ccallparanode.create(left,nil)));
  134. { and compare its result with 0 according to the original operator }
  135. result := caddnode.create(nodetype,result,
  136. cordconstnode.create(0,s32inttype,false));
  137. left := nil;
  138. right := nil;
  139. end;
  140. else
  141. internalerror(2011031401);
  142. end;
  143. end;
  144. function tjvmaddnode.jvm_first_addset: tnode;
  145. procedure call_set_helper_paras(const n : string; isenum: boolean; paras: tcallparanode);
  146. var
  147. block: tblocknode;
  148. stat: tstatementnode;
  149. temp: ttempcreatenode;
  150. begin
  151. result:=ccallnode.createinternmethod(left,'CLONE',nil);
  152. if isenum then
  153. inserttypeconv_explicit(result,java_juenumset)
  154. else
  155. inserttypeconv_explicit(result,java_jubitset);
  156. if isenum then
  157. begin
  158. { all enum instance methods return a boolean, while we are
  159. interested in the resulting set }
  160. block:=internalstatements(stat);
  161. temp:=ctempcreatenode.create(java_juenumset,4,tt_persistent,true);
  162. addstatement(stat,temp);
  163. addstatement(stat,cassignmentnode.create(
  164. ctemprefnode.create(temp),result));
  165. addstatement(stat,ccallnode.createinternmethod(
  166. ctemprefnode.create(temp),n,paras));
  167. addstatement(stat,ctempdeletenode.create_normal_temp(temp));
  168. addstatement(stat,ctemprefnode.create(temp));
  169. result:=block;
  170. end
  171. else
  172. result:=ccallnode.createinternmethod(result,n,paras);
  173. end;
  174. procedure call_set_helper(const n: string; isenum: boolean);
  175. begin
  176. call_set_helper_paras(n,isenum,ccallparanode.create(right,nil));
  177. end;
  178. var
  179. procname: string;
  180. tmpn: tnode;
  181. paras: tcallparanode;
  182. isenum: boolean;
  183. begin
  184. isenum:=
  185. (assigned(tsetdef(left.resultdef).elementdef) and
  186. (tsetdef(left.resultdef).elementdef.typ=enumdef)) or
  187. ((right.nodetype=setelementn) and
  188. (tsetelementnode(right).left.resultdef.typ=enumdef)) or
  189. ((right.resultdef.typ=setdef) and
  190. assigned(tsetdef(right.resultdef).elementdef) and
  191. (tsetdef(right.resultdef).elementdef.typ=enumdef));
  192. { don't destroy optimization opportunity }
  193. if not((nodetype=addn) and
  194. (right.nodetype=setelementn) and
  195. is_emptyset(left)) then
  196. begin
  197. left:=caddrnode.create_internal(left);
  198. include(left.flags,nf_typedaddr);
  199. if isenum then
  200. begin
  201. inserttypeconv_explicit(left,java_juenumset);
  202. if right.resultdef.typ=setdef then
  203. begin
  204. right:=caddrnode.create_internal(right);
  205. include(right.flags,nf_typedaddr);
  206. inserttypeconv_explicit(right,java_juenumset);
  207. end;
  208. end
  209. else
  210. begin
  211. inserttypeconv_explicit(left,java_jubitset);
  212. if right.resultdef.typ=setdef then
  213. begin
  214. right:=caddrnode.create_internal(right);
  215. include(right.flags,nf_typedaddr);
  216. inserttypeconv_explicit(right,java_jubitset);
  217. end;
  218. end;
  219. end
  220. else
  221. tjvmsetconstnode(left).setconsttype:=sct_notransform;
  222. firstpass(left);
  223. firstpass(right);
  224. case nodetype of
  225. equaln,unequaln,lten,gten:
  226. begin
  227. case nodetype of
  228. equaln,unequaln:
  229. procname:='EQUALS';
  230. lten,gten:
  231. begin
  232. { (left <= right) = (right >= left) }
  233. if nodetype=lten then
  234. begin
  235. tmpn:=left;
  236. left:=right;
  237. right:=tmpn;
  238. end;
  239. procname:='CONTAINSALL'
  240. end;
  241. end;
  242. result:=ccallnode.createinternmethod(left,procname,ccallparanode.create(right,nil));
  243. { for an unequaln, we have to negate the result of equals }
  244. if nodetype=unequaln then
  245. result:=cnotnode.create(result);
  246. end;
  247. addn:
  248. begin
  249. { optimize first loading of a set }
  250. if (right.nodetype=setelementn) and
  251. is_emptyset(left) then
  252. begin
  253. paras:=nil;
  254. procname:='OF';
  255. if isenum then
  256. begin
  257. inserttypeconv_explicit(tsetelementnode(right).left,tenumdef(tsetelementnode(right).left.resultdef).getbasedef.classdef);
  258. result:=cloadvmtaddrnode.create(ctypenode.create(java_juenumset));
  259. end
  260. else
  261. begin
  262. { for boolean, char, etc }
  263. inserttypeconv_explicit(tsetelementnode(right).left,s32inttype);
  264. result:=cloadvmtaddrnode.create(ctypenode.create(java_jubitset));
  265. end;
  266. paras:=ccallparanode.create(tsetelementnode(right).left,nil);
  267. tsetelementnode(right).left:=nil;
  268. if assigned(tsetelementnode(right).right) then
  269. begin
  270. procname:='RANGE';
  271. if isenum then
  272. begin
  273. inserttypeconv_explicit(tsetelementnode(right).right,tenumdef(tsetelementnode(right).right.resultdef).getbasedef.classdef);
  274. end
  275. else
  276. begin
  277. inserttypeconv_explicit(tsetelementnode(right).right,s32inttype);
  278. end;
  279. paras:=ccallparanode.create(tsetelementnode(right).right,paras);
  280. tsetelementnode(right).right:=nil;
  281. end;
  282. right.free;
  283. result:=ccallnode.createinternmethod(result,procname,paras)
  284. end
  285. else
  286. begin
  287. if right.nodetype=setelementn then
  288. begin
  289. paras:=nil;
  290. { get a copy of left to add to }
  291. procname:='ADD';
  292. if isenum then
  293. begin
  294. inserttypeconv_explicit(tsetelementnode(right).left,tenumdef(tsetelementnode(right).left.resultdef).getbasedef.classdef);
  295. end
  296. else
  297. begin
  298. { for boolean, char, etc }
  299. inserttypeconv_explicit(tsetelementnode(right).left,s32inttype);
  300. end;
  301. paras:=ccallparanode.create(tsetelementnode(right).left,paras);
  302. tsetelementnode(right).left:=nil;
  303. if assigned(tsetelementnode(right).right) then
  304. begin
  305. procname:='ADDALL';
  306. { create a set containing the range via the class
  307. factory method, then add all of its elements }
  308. if isenum then
  309. begin
  310. inserttypeconv_explicit(tsetelementnode(right).right,tenumdef(tsetelementnode(right).right.resultdef).getbasedef.classdef);
  311. tmpn:=cloadvmtaddrnode.create(ctypenode.create(java_juenumset));
  312. end
  313. else
  314. begin
  315. inserttypeconv_explicit(tsetelementnode(right).right,s32inttype);
  316. tmpn:=cloadvmtaddrnode.create(ctypenode.create(java_jubitset));
  317. end;
  318. paras:=ccallparanode.create(ccallnode.createinternmethod(tmpn,'RANGE',ccallparanode.create(tsetelementnode(right).right,paras)),nil);
  319. tsetelementnode(right).right:=nil;
  320. end;
  321. call_set_helper_paras(procname,isenum,paras);
  322. end
  323. else
  324. call_set_helper('ADDALL',isenum)
  325. end
  326. end;
  327. subn:
  328. call_set_helper('REMOVEALL',isenum);
  329. symdifn:
  330. if isenum then
  331. begin
  332. { "s1 xor s2" is the same as "(s1 + s2) - (s1 * s2)"
  333. -> call helper to prevent double evaluations }
  334. result:=ccallnode.createintern('fpc_enumset_symdif',
  335. ccallparanode.create(right,ccallparanode.create(left,nil)));
  336. left:=nil;
  337. right:=nil;
  338. end
  339. else
  340. call_set_helper('SYMDIF',isenum);
  341. muln:
  342. call_set_helper('RETAINALL',isenum)
  343. else
  344. internalerror(2011062807);
  345. end;
  346. { convert helper result back to original set type for further expression
  347. evaluation }
  348. if not is_boolean(resultdef) then
  349. begin
  350. inserttypeconv_explicit(result,getpointerdef(resultdef));
  351. result:=cderefnode.create(result);
  352. end;
  353. { left and right are reused as parameters }
  354. left:=nil;
  355. right:=nil;
  356. end;
  357. function tjvmaddnode.cmpnode2signedtopcmp: TOpCmp;
  358. begin
  359. case nodetype of
  360. gtn: result:=OC_GT;
  361. gten: result:=OC_GTE;
  362. ltn: result:=OC_LT;
  363. lten: result:=OC_LTE;
  364. equaln: result:=OC_EQ;
  365. unequaln: result:=OC_NE;
  366. else
  367. internalerror(2011010412);
  368. end;
  369. end;
  370. procedure tjvmaddnode.second_generic_compare;
  371. var
  372. cmpop: TOpCmp;
  373. begin
  374. pass_left_right;
  375. { swap the operands to make it easier for the optimizer to optimize
  376. the operand stack slot reloading in case both are in a register }
  377. if (left.location.loc in [LOC_REGISTER,LOC_CREGISTER]) and
  378. (right.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  379. swapleftright;
  380. cmpop:=cmpnode2signedtopcmp;
  381. if (nf_swapped in flags) then
  382. cmpop:=swap_opcmp(cmpop);
  383. location_reset(location,LOC_JUMP,OS_NO);
  384. if left.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  385. hlcg.a_cmp_loc_reg_label(current_asmdata.CurrAsmList,left.resultdef,cmpop,right.location,left.location.register,current_procinfo.CurrTrueLabel)
  386. else case right.location.loc of
  387. LOC_REGISTER,LOC_CREGISTER:
  388. hlcg.a_cmp_reg_loc_label(current_asmdata.CurrAsmList,left.resultdef,cmpop,right.location.register,left.location,current_procinfo.CurrTrueLabel);
  389. LOC_REFERENCE,LOC_CREFERENCE:
  390. hlcg.a_cmp_ref_loc_label(current_asmdata.CurrAsmList,left.resultdef,cmpop,right.location.reference,left.location,current_procinfo.CurrTrueLabel);
  391. LOC_CONSTANT:
  392. hlcg.a_cmp_const_loc_label(current_asmdata.CurrAsmList,left.resultdef,cmpop,right.location.value,left.location,current_procinfo.CurrTrueLabel);
  393. else
  394. internalerror(2011010413);
  395. end;
  396. hlcg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  397. end;
  398. procedure tjvmaddnode.pass_left_right;
  399. begin
  400. swapleftright;
  401. inherited pass_left_right;
  402. end;
  403. procedure tjvmaddnode.second_addfloat;
  404. var
  405. op : TAsmOp;
  406. commutative : boolean;
  407. begin
  408. pass_left_right;
  409. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  410. location.register:=hlcg.getfpuregister(current_asmdata.CurrAsmList,resultdef);
  411. commutative:=false;
  412. case nodetype of
  413. addn :
  414. begin
  415. if location.size=OS_F64 then
  416. op:=a_dadd
  417. else
  418. op:=a_fadd;
  419. commutative:=true;
  420. end;
  421. muln :
  422. begin
  423. if location.size=OS_F64 then
  424. op:=a_dmul
  425. else
  426. op:=a_fmul;
  427. commutative:=true;
  428. end;
  429. subn :
  430. begin
  431. if location.size=OS_F64 then
  432. op:=a_dsub
  433. else
  434. op:=a_fsub;
  435. end;
  436. slashn :
  437. begin
  438. if location.size=OS_F64 then
  439. op:=a_ddiv
  440. else
  441. op:=a_fdiv;
  442. end;
  443. else
  444. internalerror(2011010402);
  445. end;
  446. { swap the operands to make it easier for the optimizer to optimize
  447. the operand stack slot reloading (non-commutative operations must
  448. always be in the correct order though) }
  449. if (commutative and
  450. (left.location.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER]) and
  451. (right.location.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER])) or
  452. (not commutative and
  453. (nf_swapped in flags)) then
  454. swapleftright;
  455. thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  456. thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,right.resultdef,right.location);
  457. current_asmdata.CurrAsmList.concat(taicpu.op_none(op));
  458. thlcgjvm(hlcg).decstack(current_asmdata.CurrAsmList,1+ord(location.size=OS_F64));
  459. { could be optimized in the future by keeping the results on the stack,
  460. if we add code to swap the operands when necessary (a_swap for
  461. singles, store/load/load for doubles since there is no swap for
  462. 2-slot elements -- also adjust expectloc in that case! }
  463. thlcgjvm(hlcg).a_load_stack_reg(current_asmdata.CurrAsmList,resultdef,location.register);
  464. end;
  465. procedure tjvmaddnode.second_cmpfloat;
  466. var
  467. op : tasmop;
  468. cmpop: TOpCmp;
  469. begin
  470. pass_left_right;
  471. { swap the operands to make it easier for the optimizer to optimize
  472. the operand stack slot reloading in case both are in a register }
  473. if (left.location.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER]) and
  474. (right.location.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER]) then
  475. swapleftright;
  476. cmpop:=cmpnode2signedtopcmp;
  477. if (nf_swapped in flags) then
  478. cmpop:=swap_opcmp(cmpop);
  479. location_reset(location,LOC_JUMP,OS_NO);
  480. thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  481. thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,right.resultdef,right.location);
  482. { compares two floating point values and puts 1/0/-1 on stack depending
  483. on whether value1 >/=/< value2 }
  484. if left.location.size=OS_F64 then
  485. { make sure that comparisons with NaNs always return false for </> }
  486. if nodetype in [ltn,lten] then
  487. op:=a_dcmpg
  488. else
  489. op:=a_dcmpl
  490. else if nodetype in [ltn,lten] then
  491. op:=a_fcmpg
  492. else
  493. op:=a_fcmpl;
  494. current_asmdata.CurrAsmList.concat(taicpu.op_none(op));
  495. thlcgjvm(hlcg).decstack(current_asmdata.CurrAsmList,(1+ord(left.location.size=OS_F64))*2-1);
  496. current_asmdata.CurrAsmList.concat(taicpu.op_sym(opcmp2if[cmpop],current_procinfo.CurrTrueLabel));
  497. thlcgjvm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  498. hlcg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  499. end;
  500. procedure tjvmaddnode.second_cmpboolean;
  501. begin
  502. second_generic_compare;
  503. end;
  504. procedure tjvmaddnode.second_cmp64bit;
  505. begin
  506. second_generic_compare;
  507. end;
  508. procedure tjvmaddnode.second_add64bit;
  509. begin
  510. second_opordinal;
  511. end;
  512. procedure tjvmaddnode.second_cmpordinal;
  513. begin
  514. second_generic_compare;
  515. end;
  516. begin
  517. caddnode:=tjvmaddnode;
  518. end.