njvmmat.pas 7.4 KB

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