njvmadd.pas 12 KB

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