njvmadd.pas 12 KB

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