njvmmat.pas 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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;
  22. type
  23. tjvmmoddivnode = class(tmoddivnode)
  24. procedure pass_generate_code;override;
  25. end;
  26. tjvmshlshrnode = class(tshlshrnode)
  27. procedure pass_generate_code;override;
  28. end;
  29. tjvmnotnode = class(tcgnotnode)
  30. procedure second_boolean;override;
  31. end;
  32. tjvmunaryminusnode = class(tcgunaryminusnode)
  33. procedure second_float;override;
  34. end;
  35. implementation
  36. uses
  37. globtype,systems,constexp,
  38. cutils,verbose,globals,
  39. symconst,symdef,
  40. aasmbase,aasmcpu,aasmtai,aasmdata,
  41. defutil,
  42. cgbase,cgobj,pass_2,procinfo,
  43. ncon,
  44. cpubase,
  45. hlcgobj,hlcgcpu,cgutils;
  46. {*****************************************************************************
  47. tjvmmoddivnode
  48. *****************************************************************************}
  49. procedure tjvmmoddivnode.pass_generate_code;
  50. var
  51. op: topcg;
  52. isu32int: boolean;
  53. begin
  54. secondpass(left);
  55. secondpass(right);
  56. location_reset(location,LOC_REGISTER,left.location.size);
  57. location.register:=hlcg.getintregister(current_asmdata.CurrAsmList,resultdef);
  58. if nodetype=divn then
  59. begin
  60. { TODO: overflow checking in case of high(longint) or high(int64) div -1 }
  61. thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  62. if is_signed(resultdef) then
  63. op:=OP_IDIV
  64. else
  65. op:=OP_DIV;
  66. thlcgjvm(hlcg).a_op_loc_stack(current_asmdata.CurrAsmList,op,right.resultdef,right.location)
  67. end
  68. else
  69. begin
  70. { must be handled via a helper }
  71. if torddef(resultdef).ordtype=u64bit then
  72. internalerror(2011010416);
  73. if (torddef(resultdef).ordtype<>u32bit) then
  74. begin
  75. isu32int:=false;
  76. thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  77. thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,right.resultdef,right.location);
  78. end
  79. else
  80. begin
  81. isu32int:=true;
  82. if left.location.loc=LOC_CONSTANT then
  83. thlcgjvm(hlcg).a_load_const_stack(current_asmdata.CurrAsmList,s64inttype,left.location.value,R_INTREGISTER)
  84. else
  85. begin
  86. thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  87. thlcgjvm(hlcg).resize_stack_int_val(current_asmdata.CurrAsmList,OS_32,OS_S64,false);
  88. end;
  89. if right.location.loc=LOC_CONSTANT then
  90. thlcgjvm(hlcg).a_load_const_stack(current_asmdata.CurrAsmList,s64inttype,right.location.value,R_INTREGISTER)
  91. else
  92. begin
  93. thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,right.resultdef,right.location);
  94. thlcgjvm(hlcg).resize_stack_int_val(current_asmdata.CurrAsmList,OS_32,OS_S64,false);
  95. end;
  96. end;
  97. if isu32int or
  98. (torddef(resultdef).ordtype=s64bit) then
  99. begin
  100. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_lrem));
  101. thlcgjvm(hlcg).decstack(current_asmdata.CurrAsmList,2);
  102. end
  103. else
  104. begin
  105. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_irem));
  106. thlcgjvm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  107. end;
  108. if isu32int then
  109. thlcgjvm(hlcg).resize_stack_int_val(current_asmdata.CurrAsmList,OS_S64,OS_32,false);
  110. end;
  111. thlcgjvm(hlcg).a_load_stack_reg(current_asmdata.CurrAsmList,resultdef,location.register);
  112. end;
  113. {*****************************************************************************
  114. tjvmshlshrnode
  115. *****************************************************************************}
  116. procedure tjvmshlshrnode.pass_generate_code;
  117. var
  118. op : topcg;
  119. begin
  120. secondpass(left);
  121. secondpass(right);
  122. location_reset(location,LOC_REGISTER,left.location.size);
  123. location.register:=hlcg.getintregister(current_asmdata.CurrAsmList,resultdef);
  124. thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  125. if nodetype=shln then
  126. op:=OP_SHL
  127. else
  128. op:=OP_SHR;
  129. thlcgjvm(hlcg).a_op_loc_stack(current_asmdata.CurrAsmList,op,resultdef,right.location);
  130. thlcgjvm(hlcg).a_load_stack_reg(current_asmdata.CurrAsmList,resultdef,location.register);
  131. end;
  132. {*****************************************************************************
  133. tjvmnotnode
  134. *****************************************************************************}
  135. procedure tjvmnotnode.second_boolean;
  136. var
  137. hl : tasmlabel;
  138. begin
  139. { if the location is LOC_JUMP, we do the secondpass after the
  140. labels are allocated
  141. }
  142. if left.expectloc=LOC_JUMP then
  143. begin
  144. hl:=current_procinfo.CurrTrueLabel;
  145. current_procinfo.CurrTrueLabel:=current_procinfo.CurrFalseLabel;
  146. current_procinfo.CurrFalseLabel:=hl;
  147. secondpass(left);
  148. hlcg.maketojumpbool(current_asmdata.CurrAsmList,left);
  149. hl:=current_procinfo.CurrTrueLabel;
  150. current_procinfo.CurrTrueLabel:=current_procinfo.CurrFalseLabel;
  151. current_procinfo.CurrFalseLabel:=hl;
  152. location.loc:=LOC_JUMP;
  153. end
  154. else
  155. begin
  156. secondpass(left);
  157. case left.location.loc of
  158. LOC_REGISTER, LOC_CREGISTER,
  159. LOC_REFERENCE, LOC_CREFERENCE:
  160. begin
  161. location_reset(location,LOC_REGISTER,left.location.size);
  162. location.register:=hlcg.getintregister(current_asmdata.CurrAsmList,resultdef);
  163. thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  164. thlcgjvm(hlcg).a_op_reg_stack(current_asmdata.CurrAsmList,OP_NOT,left.resultdef,NR_NO);
  165. thlcgjvm(hlcg).a_load_stack_reg(current_asmdata.CurrAsmList,resultdef,location.register);
  166. end;
  167. else
  168. internalerror(2011010417);
  169. end;
  170. end;
  171. end;
  172. procedure tjvmunaryminusnode.second_float;
  173. var
  174. opc: tasmop;
  175. begin
  176. secondpass(left);
  177. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  178. location.register:=hlcg.getfpuregister(current_asmdata.CurrAsmList,resultdef);
  179. thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  180. if (tfloatdef(left.resultdef).floattype=s32real) then
  181. opc:=a_fneg
  182. else
  183. opc:=a_dneg;
  184. current_asmdata.CurrAsmList.concat(taicpu.op_none(opc));
  185. thlcgjvm(hlcg).a_load_stack_reg(current_asmdata.CurrAsmList,resultdef,location.register);
  186. end;
  187. begin
  188. cmoddivnode:=tjvmmoddivnode;
  189. cshlshrnode:=tjvmshlshrnode;
  190. cnotnode:=tjvmnotnode;
  191. cunaryminusnode:=tjvmunaryminusnode;
  192. end.