njvmadd.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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 cmpnode2signedtopcmp: TOpCmp;
  30. procedure second_generic_compare;
  31. procedure pass_left_right;override;
  32. procedure second_addfloat;override;
  33. procedure second_cmpfloat;override;
  34. procedure second_cmpboolean;override;
  35. procedure second_cmpsmallset;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,
  44. symconst,symtable,symdef,
  45. paramgr,procinfo,
  46. aasmtai,aasmdata,aasmcpu,defutil,
  47. hlcgobj,hlcgcpu,cgutils,
  48. cpupara,
  49. ncon,nset,nadd,ncal,ncnv,
  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. result:=inherited pass_1;
  71. if expectloc=LOC_FLAGS then
  72. expectloc:=LOC_JUMP;
  73. end;
  74. function tjvmaddnode.first_addstring: tnode;
  75. var
  76. cmpfuncname: string;
  77. begin
  78. { when we get here, we are sure that both the left and the right }
  79. { node are both strings of the same stringtype (JM) }
  80. case nodetype of
  81. addn:
  82. begin
  83. if is_shortstring(resultdef) then
  84. begin
  85. result:=inherited;
  86. exit;
  87. end;
  88. { unicode/ansistring operations use functions rather than
  89. procedures for efficiency reasons (were also implemented before
  90. var-parameters were supported; may go to procedures for
  91. maintenance reasons though }
  92. if (left.nodetype=stringconstn) and (tstringconstnode(left).len=0) then
  93. begin
  94. result:=right;
  95. left.free;
  96. left:=nil;
  97. right:=nil;
  98. exit;
  99. end;
  100. if (right.nodetype=stringconstn) and (tstringconstnode(right).len=0) then
  101. begin
  102. result:=left;
  103. left:=nil;
  104. right.free;
  105. right:=nil;
  106. exit;
  107. end;
  108. { create the call to the concat routine both strings as arguments }
  109. result:=ccallnode.createintern('fpc_'+
  110. tstringdef(resultdef).stringtypname+'_concat',
  111. ccallparanode.create(right,
  112. ccallparanode.create(left,nil)));
  113. { we reused the arguments }
  114. left := nil;
  115. right := nil;
  116. end;
  117. ltn,lten,gtn,gten,equaln,unequaln :
  118. begin
  119. { call compare routine }
  120. cmpfuncname := 'fpc_'+tstringdef(left.resultdef).stringtypname+'_compare';
  121. { for equality checks use optimized version }
  122. if nodetype in [equaln,unequaln] then
  123. cmpfuncname := cmpfuncname + '_equal';
  124. result := ccallnode.createintern(cmpfuncname,
  125. ccallparanode.create(right,ccallparanode.create(left,nil)));
  126. { and compare its result with 0 according to the original operator }
  127. result := caddnode.create(nodetype,result,
  128. cordconstnode.create(0,s32inttype,false));
  129. left := nil;
  130. right := nil;
  131. end;
  132. else
  133. internalerror(2011031401);
  134. end;
  135. end;
  136. function tjvmaddnode.cmpnode2signedtopcmp: TOpCmp;
  137. begin
  138. case nodetype of
  139. gtn: result:=OC_GT;
  140. gten: result:=OC_GTE;
  141. ltn: result:=OC_LT;
  142. lten: result:=OC_LTE;
  143. equaln: result:=OC_EQ;
  144. unequaln: result:=OC_NE;
  145. else
  146. internalerror(2011010412);
  147. end;
  148. end;
  149. procedure tjvmaddnode.second_generic_compare;
  150. var
  151. cmpop: TOpCmp;
  152. begin
  153. pass_left_right;
  154. { swap the operands to make it easier for the optimizer to optimize
  155. the operand stack slot reloading in case both are in a register }
  156. if (left.location.loc in [LOC_REGISTER,LOC_CREGISTER]) and
  157. (right.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  158. swapleftright;
  159. cmpop:=cmpnode2signedtopcmp;
  160. if (nf_swapped in flags) then
  161. cmpop:=swap_opcmp(cmpop);
  162. location_reset(location,LOC_JUMP,OS_NO);
  163. if left.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  164. hlcg.a_cmp_loc_reg_label(current_asmdata.CurrAsmList,left.resultdef,cmpop,right.location,left.location.register,current_procinfo.CurrTrueLabel)
  165. else case right.location.loc of
  166. LOC_REGISTER,LOC_CREGISTER:
  167. hlcg.a_cmp_reg_loc_label(current_asmdata.CurrAsmList,left.resultdef,cmpop,right.location.register,left.location,current_procinfo.CurrTrueLabel);
  168. LOC_REFERENCE,LOC_CREFERENCE:
  169. hlcg.a_cmp_ref_loc_label(current_asmdata.CurrAsmList,left.resultdef,cmpop,right.location.reference,left.location,current_procinfo.CurrTrueLabel);
  170. LOC_CONSTANT:
  171. hlcg.a_cmp_const_loc_label(current_asmdata.CurrAsmList,left.resultdef,cmpop,right.location.value,left.location,current_procinfo.CurrTrueLabel);
  172. else
  173. internalerror(2011010413);
  174. end;
  175. hlcg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  176. end;
  177. procedure tjvmaddnode.pass_left_right;
  178. begin
  179. swapleftright;
  180. inherited pass_left_right;
  181. end;
  182. procedure tjvmaddnode.second_addfloat;
  183. var
  184. op : TAsmOp;
  185. commutative : boolean;
  186. begin
  187. pass_left_right;
  188. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  189. location.register:=hlcg.getfpuregister(current_asmdata.CurrAsmList,resultdef);
  190. commutative:=false;
  191. case nodetype of
  192. addn :
  193. begin
  194. if location.size=OS_F64 then
  195. op:=a_dadd
  196. else
  197. op:=a_fadd;
  198. commutative:=true;
  199. end;
  200. muln :
  201. begin
  202. if location.size=OS_F64 then
  203. op:=a_dmul
  204. else
  205. op:=a_fmul;
  206. commutative:=true;
  207. end;
  208. subn :
  209. begin
  210. if location.size=OS_F64 then
  211. op:=a_dsub
  212. else
  213. op:=a_fsub;
  214. end;
  215. slashn :
  216. begin
  217. if location.size=OS_F64 then
  218. op:=a_ddiv
  219. else
  220. op:=a_fdiv;
  221. end;
  222. else
  223. internalerror(2011010402);
  224. end;
  225. { swap the operands to make it easier for the optimizer to optimize
  226. the operand stack slot reloading (non-commutative operations must
  227. always be in the correct order though) }
  228. if (commutative and
  229. (left.location.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER]) and
  230. (right.location.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER])) or
  231. (not commutative and
  232. (nf_swapped in flags)) then
  233. swapleftright;
  234. thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  235. thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,right.resultdef,right.location);
  236. current_asmdata.CurrAsmList.concat(taicpu.op_none(op));
  237. thlcgjvm(hlcg).decstack(current_asmdata.CurrAsmList,1+ord(location.size=OS_F64));
  238. { could be optimized in the future by keeping the results on the stack,
  239. if we add code to swap the operands when necessary (a_swap for
  240. singles, store/load/load for doubles since there is no swap for
  241. 2-slot elements -- also adjust expectloc in that case! }
  242. thlcgjvm(hlcg).a_load_stack_reg(current_asmdata.CurrAsmList,resultdef,location.register);
  243. end;
  244. procedure tjvmaddnode.second_cmpfloat;
  245. var
  246. op : tasmop;
  247. cmpop: TOpCmp;
  248. begin
  249. pass_left_right;
  250. { swap the operands to make it easier for the optimizer to optimize
  251. the operand stack slot reloading in case both are in a register }
  252. if (left.location.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER]) and
  253. (right.location.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER]) then
  254. swapleftright;
  255. cmpop:=cmpnode2signedtopcmp;
  256. if (nf_swapped in flags) then
  257. cmpop:=swap_opcmp(cmpop);
  258. location_reset(location,LOC_JUMP,OS_NO);
  259. thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  260. thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,right.resultdef,right.location);
  261. { compares two floating point values and puts 1/0/-1 on stack depending
  262. on whether value1 >/=/< value2 }
  263. if left.location.size=OS_F64 then
  264. { make sure that comparisons with NaNs always return false for </> }
  265. if nodetype in [ltn,lten] then
  266. op:=a_dcmpg
  267. else
  268. op:=a_dcmpl
  269. else if nodetype in [ltn,lten] then
  270. op:=a_fcmpg
  271. else
  272. op:=a_fcmpl;
  273. current_asmdata.CurrAsmList.concat(taicpu.op_none(op));
  274. thlcgjvm(hlcg).decstack(current_asmdata.CurrAsmList,(1+ord(left.location.size=OS_F64))*2-1);
  275. current_asmdata.CurrAsmList.concat(taicpu.op_sym(opcmp2if[cmpop],current_procinfo.CurrTrueLabel));
  276. thlcgjvm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  277. hlcg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  278. end;
  279. procedure tjvmaddnode.second_cmpboolean;
  280. begin
  281. second_generic_compare;
  282. end;
  283. procedure tjvmaddnode.second_cmpsmallset;
  284. begin
  285. if (nodetype in [equaln,unequaln]) then
  286. begin
  287. second_generic_compare;
  288. exit;
  289. end;
  290. case nodetype of
  291. lten,gten:
  292. begin
  293. pass_left_right;
  294. If (not(nf_swapped in flags) and
  295. (nodetype=lten)) or
  296. ((nf_swapped in flags) and
  297. (nodetype=gten)) then
  298. swapleftright;
  299. location_reset(location,LOC_JUMP,OS_NO);
  300. // now we have to check whether left >= right:
  301. // (right and not(left)=0)
  302. thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  303. thlcgjvm(hlcg).a_op_reg_stack(current_asmdata.CurrAsmList,OP_NOT,left.resultdef,NR_NO);
  304. thlcgjvm(hlcg).a_op_loc_stack(current_asmdata.CurrAsmList,OP_AND,right.resultdef,right.location);
  305. current_asmdata.CurrAsmList.concat(taicpu.op_sym(a_ifeq,current_procinfo.CurrTrueLabel));
  306. thlcgjvm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  307. hlcg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  308. end;
  309. else
  310. internalerror(2011010414);
  311. end;
  312. end;
  313. procedure tjvmaddnode.second_cmp64bit;
  314. begin
  315. second_generic_compare;
  316. end;
  317. procedure tjvmaddnode.second_add64bit;
  318. begin
  319. second_opordinal;
  320. end;
  321. procedure tjvmaddnode.second_cmpordinal;
  322. begin
  323. second_generic_compare;
  324. end;
  325. begin
  326. caddnode:=tjvmaddnode;
  327. end.