njvmadd.pas 19 KB

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