njvmadd.pas 19 KB

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