njvmadd.pas 12 KB

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