2
0

njvmmat.pas 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. {
  2. Copyright (c) 1998-2011 by Florian Klaempfl and Jonas Maebe
  3. Generate JVM code for math nodes
  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 njvmmat;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,nmat,ncgmat,ncghlmat;
  22. type
  23. tjvmmoddivnode = class(tmoddivnode)
  24. protected
  25. function use_moddiv64bitint_helper: boolean; override;
  26. public
  27. procedure pass_generate_code;override;
  28. end;
  29. tjvmshlshrnode = class(tshlshrnode)
  30. procedure pass_generate_code;override;
  31. end;
  32. tjvmnotnode = class(tcghlnotnode)
  33. end;
  34. tjvmunaryminusnode = class(tcgunaryminusnode)
  35. procedure second_float;override;
  36. end;
  37. implementation
  38. uses
  39. globtype,systems,constexp,
  40. cutils,verbose,globals,compinnr,
  41. symconst,symdef,
  42. aasmbase,aasmcpu,aasmtai,aasmdata,
  43. defutil,
  44. cgbase,cgobj,pass_2,procinfo,
  45. ncon,
  46. cpubase,
  47. hlcgobj,hlcgcpu,cgutils;
  48. {*****************************************************************************
  49. tjvmmoddivnode
  50. *****************************************************************************}
  51. function tjvmmoddivnode.use_moddiv64bitint_helper: boolean;
  52. begin
  53. result:=
  54. (left.resultdef.typ=orddef) and
  55. (right.resultdef.typ=orddef) and
  56. ((torddef(left.resultdef).ordtype=u64bit) or
  57. (torddef(right.resultdef).ordtype=u64bit));
  58. end;
  59. procedure tjvmmoddivnode.pass_generate_code;
  60. var
  61. tmpreg: tregister;
  62. lab: tasmlabel;
  63. ovloc: tlocation;
  64. op: topcg;
  65. isu32int: boolean;
  66. begin
  67. secondpass(left);
  68. secondpass(right);
  69. location_reset(location,LOC_REGISTER,left.location.size);
  70. location.register:=hlcg.getintregister(current_asmdata.CurrAsmList,resultdef);
  71. if nodetype=divn then
  72. begin
  73. thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  74. if is_signed(resultdef) then
  75. op:=OP_IDIV
  76. else
  77. op:=OP_DIV;
  78. thlcgjvm(hlcg).a_op_loc_stack(current_asmdata.CurrAsmList,op,right.resultdef,right.location)
  79. end
  80. else
  81. begin
  82. { must be handled via a helper }
  83. if torddef(resultdef).ordtype=u64bit then
  84. internalerror(2011010416);
  85. if (torddef(resultdef).ordtype<>u32bit) then
  86. begin
  87. isu32int:=false;
  88. thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  89. thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,right.resultdef,right.location);
  90. end
  91. else
  92. begin
  93. isu32int:=true;
  94. if left.location.loc=LOC_CONSTANT then
  95. thlcgjvm(hlcg).a_load_const_stack(current_asmdata.CurrAsmList,s64inttype,left.location.value,R_INTREGISTER)
  96. else
  97. begin
  98. thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  99. thlcgjvm(hlcg).resize_stack_int_val(current_asmdata.CurrAsmList,u32inttype,s64inttype,false);
  100. end;
  101. if right.location.loc=LOC_CONSTANT then
  102. thlcgjvm(hlcg).a_load_const_stack(current_asmdata.CurrAsmList,s64inttype,right.location.value,R_INTREGISTER)
  103. else
  104. begin
  105. thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,right.resultdef,right.location);
  106. thlcgjvm(hlcg).resize_stack_int_val(current_asmdata.CurrAsmList,u32inttype,s64inttype,false);
  107. end;
  108. end;
  109. if isu32int or
  110. (torddef(resultdef).ordtype=s64bit) then
  111. begin
  112. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_lrem));
  113. thlcgjvm(hlcg).decstack(current_asmdata.CurrAsmList,2);
  114. end
  115. else
  116. begin
  117. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_irem));
  118. thlcgjvm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  119. end;
  120. if isu32int then
  121. thlcgjvm(hlcg).resize_stack_int_val(current_asmdata.CurrAsmList,s64inttype,u32inttype,false);
  122. end;
  123. thlcgjvm(hlcg).a_load_stack_reg(current_asmdata.CurrAsmList,resultdef,location.register);
  124. if (cs_check_overflow in current_settings.localswitches) and
  125. is_signed(resultdef) then
  126. begin
  127. { the JVM raises an exception for integer div-iby-zero -> only
  128. overflow in case left is low(inttype) and right is -1 ->
  129. check by adding high(inttype) to left and and'ing with right
  130. -> result is -1 only in case above conditions are fulfilled)
  131. }
  132. tmpreg:=hlcg.getintregister(current_asmdata.CurrAsmList,resultdef);
  133. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,resultdef,true);
  134. hlcg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_ADD,resultdef,torddef(resultdef).high,right.location.register,tmpreg);
  135. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,resultdef,true);
  136. hlcg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_AND,resultdef,left.location.register,tmpreg);
  137. current_asmdata.getjumplabel(lab);
  138. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,resultdef,OC_NE,-1,tmpreg,lab);
  139. hlcg.g_call_system_proc(current_asmdata.CurrAsmList,'fpc_overflow',[],nil);
  140. hlcg.a_label(current_asmdata.CurrAsmList,lab);
  141. end;
  142. end;
  143. {*****************************************************************************
  144. tjvmshlshrnode
  145. *****************************************************************************}
  146. procedure tjvmshlshrnode.pass_generate_code;
  147. var
  148. op : topcg;
  149. begin
  150. secondpass(left);
  151. secondpass(right);
  152. location_reset(location,LOC_REGISTER,left.location.size);
  153. location.register:=hlcg.getintregister(current_asmdata.CurrAsmList,resultdef);
  154. thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  155. if nodetype=shln then
  156. op:=OP_SHL
  157. else
  158. op:=OP_SHR;
  159. thlcgjvm(hlcg).a_op_loc_stack(current_asmdata.CurrAsmList,op,resultdef,right.location);
  160. thlcgjvm(hlcg).a_load_stack_reg(current_asmdata.CurrAsmList,resultdef,location.register);
  161. end;
  162. {*****************************************************************************
  163. tjvmunaryminustnode
  164. *****************************************************************************}
  165. procedure tjvmunaryminusnode.second_float;
  166. var
  167. opc: tasmop;
  168. begin
  169. secondpass(left);
  170. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  171. location.register:=hlcg.getfpuregister(current_asmdata.CurrAsmList,resultdef);
  172. thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  173. if (tfloatdef(left.resultdef).floattype=s32real) then
  174. opc:=a_fneg
  175. else
  176. opc:=a_dneg;
  177. current_asmdata.CurrAsmList.concat(taicpu.op_none(opc));
  178. thlcgjvm(hlcg).a_load_stack_reg(current_asmdata.CurrAsmList,resultdef,location.register);
  179. end;
  180. begin
  181. cmoddivnode:=tjvmmoddivnode;
  182. cshlshrnode:=tjvmshlshrnode;
  183. cnotnode:=tjvmnotnode;
  184. cunaryminusnode:=tjvmunaryminusnode;
  185. end.