njvmadd.pas 8.0 KB

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