njvmadd.pas 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. protected
  27. function pass_1: tnode;override;
  28. function cmpnode2signedtopcmp: TOpCmp;
  29. procedure second_generic_compare;
  30. procedure pass_left_right;override;
  31. procedure second_addfloat;override;
  32. procedure second_cmpfloat;override;
  33. procedure second_cmpboolean;override;
  34. procedure second_cmpsmallset;override;
  35. procedure second_cmp64bit;override;
  36. procedure second_cmpordinal;override;
  37. end;
  38. implementation
  39. uses
  40. systems,
  41. cutils,verbose,
  42. paramgr,procinfo,
  43. aasmtai,aasmdata,aasmcpu,defutil,
  44. hlcgobj,hlcgcpu,cgutils,
  45. cpupara,
  46. ncon,nset,nadd,
  47. cgobj;
  48. {*****************************************************************************
  49. tjvmaddnode
  50. *****************************************************************************}
  51. function tjvmaddnode.pass_1: tnode;
  52. begin
  53. result:=inherited pass_1;
  54. if expectloc=LOC_FLAGS then
  55. expectloc:=LOC_JUMP;
  56. end;
  57. function tjvmaddnode.cmpnode2signedtopcmp: TOpCmp;
  58. begin
  59. case nodetype of
  60. gtn: result:=OC_GT;
  61. gten: result:=OC_GTE;
  62. ltn: result:=OC_LT;
  63. lten: result:=OC_LTE;
  64. equaln: result:=OC_EQ;
  65. unequaln: result:=OC_NE;
  66. else
  67. internalerror(2011010412);
  68. end;
  69. end;
  70. procedure tjvmaddnode.second_generic_compare;
  71. var
  72. cmpop: TOpCmp;
  73. begin
  74. pass_left_right;
  75. { swap the operands to make it easier for the optimizer to optimize
  76. the operand stack slot reloading in case both are in a register }
  77. if (left.location.loc in [LOC_REGISTER,LOC_CREGISTER]) and
  78. (right.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  79. swapleftright;
  80. cmpop:=cmpnode2signedtopcmp;
  81. if (nf_swapped in flags) then
  82. cmpop:=swap_opcmp(cmpop);
  83. location_reset(location,LOC_JUMP,OS_NO);
  84. if left.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  85. hlcg.a_cmp_loc_reg_label(current_asmdata.CurrAsmList,left.resultdef,cmpop,right.location,left.location.register,current_procinfo.CurrTrueLabel)
  86. else case right.location.loc of
  87. LOC_REGISTER,LOC_CREGISTER:
  88. hlcg.a_cmp_reg_loc_label(current_asmdata.CurrAsmList,left.resultdef,cmpop,right.location.register,left.location,current_procinfo.CurrTrueLabel);
  89. LOC_REFERENCE,LOC_CREFERENCE:
  90. hlcg.a_cmp_ref_loc_label(current_asmdata.CurrAsmList,left.resultdef,cmpop,right.location.reference,left.location,current_procinfo.CurrTrueLabel);
  91. LOC_CONSTANT:
  92. hlcg.a_cmp_const_loc_label(current_asmdata.CurrAsmList,left.resultdef,cmpop,right.location.value,left.location,current_procinfo.CurrTrueLabel);
  93. else
  94. internalerror(2011010413);
  95. end;
  96. hlcg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  97. end;
  98. procedure tjvmaddnode.pass_left_right;
  99. begin
  100. swapleftright;
  101. inherited pass_left_right;
  102. end;
  103. procedure tjvmaddnode.second_addfloat;
  104. var
  105. op : TAsmOp;
  106. commutative : boolean;
  107. begin
  108. pass_left_right;
  109. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  110. location.register:=hlcg.getfpuregister(current_asmdata.CurrAsmList,resultdef);
  111. commutative:=false;
  112. case nodetype of
  113. addn :
  114. begin
  115. if location.size=OS_F64 then
  116. op:=a_dadd
  117. else
  118. op:=a_fadd;
  119. commutative:=true;
  120. end;
  121. muln :
  122. begin
  123. if location.size=OS_F64 then
  124. op:=a_dmul
  125. else
  126. op:=a_fmul;
  127. commutative:=true;
  128. end;
  129. subn :
  130. begin
  131. if location.size=OS_F64 then
  132. op:=a_dsub
  133. else
  134. op:=a_fsub;
  135. end;
  136. slashn :
  137. begin
  138. if location.size=OS_F64 then
  139. op:=a_ddiv
  140. else
  141. op:=a_fdiv;
  142. end;
  143. else
  144. internalerror(2011010402);
  145. end;
  146. { swap the operands to make it easier for the optimizer to optimize
  147. the operand stack slot reloading (non-commutative operations must
  148. always be in the correct order though) }
  149. if (commutative and
  150. (left.location.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER]) and
  151. (right.location.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER])) or
  152. (not commutative and
  153. (nf_swapped in flags)) then
  154. swapleftright;
  155. thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  156. thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,right.resultdef,right.location);
  157. current_asmdata.CurrAsmList.concat(taicpu.op_none(op));
  158. thlcgjvm(hlcg).decstack(current_asmdata.CurrAsmList,1+ord(location.size=OS_F64));
  159. { could be optimized in the future by keeping the results on the stack,
  160. if we add code to swap the operands when necessary (a_swap for
  161. singles, store/load/load for doubles since there is no swap for
  162. 2-slot elements -- also adjust expectloc in that case! }
  163. thlcgjvm(hlcg).a_load_stack_reg(current_asmdata.CurrAsmList,resultdef,location.register);
  164. end;
  165. procedure tjvmaddnode.second_cmpfloat;
  166. var
  167. op : tasmop;
  168. cmpop: TOpCmp;
  169. begin
  170. pass_left_right;
  171. { swap the operands to make it easier for the optimizer to optimize
  172. the operand stack slot reloading in case both are in a register }
  173. if (left.location.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER]) and
  174. (right.location.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER]) then
  175. swapleftright;
  176. cmpop:=cmpnode2signedtopcmp;
  177. if (nf_swapped in flags) then
  178. cmpop:=swap_opcmp(cmpop);
  179. location_reset(location,LOC_JUMP,OS_NO);
  180. thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  181. thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,right.resultdef,right.location);
  182. { compares two floating point values and puts 1/0/-1 on stack depending
  183. on whether value1 >/=/< value2 }
  184. if left.location.size=OS_F64 then
  185. { make sure that comparisons with NaNs always return false for </> }
  186. if nodetype in [ltn,lten] then
  187. op:=a_dcmpg
  188. else
  189. op:=a_dcmpl
  190. else if nodetype in [ltn,lten] then
  191. op:=a_fcmpg
  192. else
  193. op:=a_fcmpl;
  194. current_asmdata.CurrAsmList.concat(taicpu.op_none(op));
  195. thlcgjvm(hlcg).decstack(current_asmdata.CurrAsmList,(1+ord(left.location.size=OS_F64))*2-1);
  196. current_asmdata.CurrAsmList.concat(taicpu.op_sym(opcmp2if[cmpop],current_procinfo.CurrTrueLabel));
  197. thlcgjvm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  198. hlcg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  199. end;
  200. procedure tjvmaddnode.second_cmpboolean;
  201. begin
  202. second_generic_compare;
  203. end;
  204. procedure tjvmaddnode.second_cmpsmallset;
  205. begin
  206. if (nodetype in [equaln,unequaln]) then
  207. begin
  208. second_generic_compare;
  209. exit;
  210. end;
  211. case nodetype of
  212. lten,gten:
  213. begin
  214. pass_left_right;
  215. If (not(nf_swapped in flags) and
  216. (nodetype=lten)) or
  217. ((nf_swapped in flags) and
  218. (nodetype=gten)) then
  219. swapleftright;
  220. location_reset(location,LOC_JUMP,OS_NO);
  221. // now we have to check whether left >= right:
  222. // (right and not(left)=0)
  223. thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  224. thlcgjvm(hlcg).a_op_reg_stack(current_asmdata.CurrAsmList,OP_NOT,left.resultdef,NR_NO);
  225. thlcgjvm(hlcg).a_op_loc_stack(current_asmdata.CurrAsmList,OP_AND,right.resultdef,right.location);
  226. current_asmdata.CurrAsmList.concat(taicpu.op_sym(a_ifeq,current_procinfo.CurrTrueLabel));
  227. thlcgjvm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  228. hlcg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  229. end;
  230. else
  231. internalerror(2011010414);
  232. end;
  233. end;
  234. procedure tjvmaddnode.second_cmp64bit;
  235. begin
  236. second_generic_compare;
  237. end;
  238. procedure tjvmaddnode.second_cmpordinal;
  239. begin
  240. second_generic_compare;
  241. end;
  242. begin
  243. caddnode:=tjvmaddnode;
  244. end.