njvmadd.pas 20 KB

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