njvmadd.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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,compinnr,
  42. symconst,symtable,symdef,symcpu,
  43. paramgr,procinfo,pass_1,
  44. aasmbase,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(taddrnode(left).addrnodeflags,anf_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(taddrnode(right).addrnodeflags,anf_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(taddrnode(right).addrnodeflags,anf_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,cpointerdef.getreusable(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. truelabel,
  315. falselabel: tasmlabel;
  316. cmpop: TOpCmp;
  317. begin
  318. truelabel:=nil;
  319. falselabel:=nil;
  320. pass_left_right;
  321. { swap the operands to make it easier for the optimizer to optimize
  322. the operand stack slot reloading in case both are in a register }
  323. if (left.location.loc in [LOC_REGISTER,LOC_CREGISTER]) and
  324. (right.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  325. swapleftright;
  326. cmpop:=cmpnode2topcmp(unsigned);
  327. if (nf_swapped in flags) then
  328. cmpop:=swap_opcmp(cmpop);
  329. current_asmdata.getjumplabel(truelabel);
  330. current_asmdata.getjumplabel(falselabel);
  331. location_reset_jump(location,truelabel,falselabel);
  332. if left.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  333. hlcg.a_cmp_loc_reg_label(current_asmdata.CurrAsmList,left.resultdef,cmpop,right.location,left.location.register,location.truelabel)
  334. else case right.location.loc of
  335. LOC_REGISTER,LOC_CREGISTER:
  336. hlcg.a_cmp_reg_loc_label(current_asmdata.CurrAsmList,left.resultdef,cmpop,right.location.register,left.location,location.truelabel);
  337. LOC_REFERENCE,LOC_CREFERENCE:
  338. hlcg.a_cmp_ref_loc_label(current_asmdata.CurrAsmList,left.resultdef,cmpop,right.location.reference,left.location,location.truelabel);
  339. LOC_CONSTANT:
  340. hlcg.a_cmp_const_loc_label(current_asmdata.CurrAsmList,left.resultdef,cmpop,right.location.value,left.location,location.truelabel);
  341. else
  342. internalerror(2011010413);
  343. end;
  344. hlcg.a_jmp_always(current_asmdata.CurrAsmList,location.falselabel);
  345. end;
  346. procedure tjvmaddnode.pass_left_right;
  347. begin
  348. if not((nodetype in [orn,andn]) and
  349. is_boolean(left.resultdef)) then
  350. swapleftright;
  351. inherited pass_left_right;
  352. end;
  353. procedure tjvmaddnode.second_addfloat;
  354. var
  355. op : TAsmOp;
  356. commutative : boolean;
  357. begin
  358. pass_left_right;
  359. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  360. location.register:=hlcg.getfpuregister(current_asmdata.CurrAsmList,resultdef);
  361. commutative:=false;
  362. case nodetype of
  363. addn :
  364. begin
  365. if location.size=OS_F64 then
  366. op:=a_dadd
  367. else
  368. op:=a_fadd;
  369. commutative:=true;
  370. end;
  371. muln :
  372. begin
  373. if location.size=OS_F64 then
  374. op:=a_dmul
  375. else
  376. op:=a_fmul;
  377. commutative:=true;
  378. end;
  379. subn :
  380. begin
  381. if location.size=OS_F64 then
  382. op:=a_dsub
  383. else
  384. op:=a_fsub;
  385. end;
  386. slashn :
  387. begin
  388. if location.size=OS_F64 then
  389. op:=a_ddiv
  390. else
  391. op:=a_fdiv;
  392. end;
  393. else
  394. internalerror(2011010402);
  395. end;
  396. { swap the operands to make it easier for the optimizer to optimize
  397. the operand stack slot reloading (non-commutative operations must
  398. always be in the correct order though) }
  399. if (commutative and
  400. (left.location.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER]) and
  401. (right.location.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER])) or
  402. (not commutative and
  403. (nf_swapped in flags)) then
  404. swapleftright;
  405. thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  406. thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,right.resultdef,right.location);
  407. current_asmdata.CurrAsmList.concat(taicpu.op_none(op));
  408. thlcgjvm(hlcg).decstack(current_asmdata.CurrAsmList,1+ord(location.size=OS_F64));
  409. { could be optimized in the future by keeping the results on the stack,
  410. if we add code to swap the operands when necessary (a_swap for
  411. singles, store/load/load for doubles since there is no swap for
  412. 2-slot elements -- also adjust expectloc in that case! }
  413. thlcgjvm(hlcg).a_load_stack_reg(current_asmdata.CurrAsmList,resultdef,location.register);
  414. end;
  415. procedure tjvmaddnode.second_cmpfloat;
  416. var
  417. truelabel,
  418. falselabel: tasmlabel;
  419. op: tasmop;
  420. cmpop: TOpCmp;
  421. begin
  422. truelabel:=nil;
  423. falselabel:=nil;
  424. pass_left_right;
  425. { swap the operands to make it easier for the optimizer to optimize
  426. the operand stack slot reloading in case both are in a register }
  427. if (left.location.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER]) and
  428. (right.location.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER]) then
  429. swapleftright;
  430. cmpop:=cmpnode2topcmp(false);
  431. if (nf_swapped in flags) then
  432. cmpop:=swap_opcmp(cmpop);
  433. current_asmdata.getjumplabel(truelabel);
  434. current_asmdata.getjumplabel(falselabel);
  435. location_reset_jump(location,truelabel,falselabel);
  436. thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  437. thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,right.resultdef,right.location);
  438. { compares two floating point values and puts 1/0/-1 on stack depending
  439. on whether value1 >/=/< value2 }
  440. if left.location.size=OS_F64 then
  441. { make sure that comparisons with NaNs always return false for </> }
  442. if nodetype in [ltn,lten] then
  443. op:=a_dcmpg
  444. else
  445. op:=a_dcmpl
  446. else if nodetype in [ltn,lten] then
  447. op:=a_fcmpg
  448. else
  449. op:=a_fcmpl;
  450. current_asmdata.CurrAsmList.concat(taicpu.op_none(op));
  451. thlcgjvm(hlcg).decstack(current_asmdata.CurrAsmList,(1+ord(left.location.size=OS_F64))*2-1);
  452. current_asmdata.CurrAsmList.concat(taicpu.op_sym(opcmp2if[cmpop],location.truelabel));
  453. thlcgjvm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  454. hlcg.a_jmp_always(current_asmdata.CurrAsmList,location.falselabel);
  455. end;
  456. procedure tjvmaddnode.second_cmpboolean;
  457. begin
  458. second_generic_compare(true);
  459. end;
  460. procedure tjvmaddnode.second_cmp64bit;
  461. begin
  462. second_generic_compare(not is_signed(left.resultdef));
  463. end;
  464. procedure tjvmaddnode.second_add64bit;
  465. begin
  466. second_opordinal;
  467. end;
  468. procedure tjvmaddnode.second_cmpordinal;
  469. begin
  470. second_generic_compare(not is_signed(left.resultdef));
  471. end;
  472. begin
  473. caddnode:=tjvmaddnode;
  474. end.