ncpumat.pas 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. {
  2. Copyright (c) 1998-2002, 2014 by Florian Klaempfl and Jonas Maebe
  3. Generate AArch64 assembler 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 ncpumat;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,nmat,ncgmat;
  22. type
  23. taarch64moddivnode = class(tmoddivnode)
  24. function pass_1: tnode; override;
  25. procedure pass_generate_code;override;
  26. end;
  27. taarch64notnode = class(tcgnotnode)
  28. procedure second_boolean;override;
  29. end;
  30. taarch64unaryminusnode = class(tcgunaryminusnode)
  31. procedure second_float; override;
  32. end;
  33. implementation
  34. uses
  35. globtype,systems,constexp,
  36. cutils,verbose,globals,
  37. symconst,symdef,
  38. aasmbase,aasmcpu,aasmtai,aasmdata,
  39. defutil,
  40. cgbase,cgobj,hlcgobj,pass_2,procinfo,
  41. ncon,
  42. cpubase,
  43. ncgutil,cgcpu,cgutils;
  44. {*****************************************************************************
  45. taarch64moddivnode
  46. *****************************************************************************}
  47. function taarch64moddivnode.pass_1: tnode;
  48. begin
  49. result:=inherited pass_1;
  50. if not assigned(result) then
  51. include(current_procinfo.flags,pi_do_call);
  52. end;
  53. procedure taarch64moddivnode.pass_generate_code;
  54. var
  55. op : tasmop;
  56. tmpreg,
  57. numerator,
  58. divider,
  59. resultreg : tregister;
  60. hl : tasmlabel;
  61. overflowloc: tlocation;
  62. begin
  63. secondpass(left);
  64. secondpass(right);
  65. { set result location }
  66. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  67. location.register:=cg.getintregister(current_asmdata.CurrAsmList,def_cgsize(resultdef));
  68. resultreg:=location.register;
  69. { put numerator in register }
  70. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  71. numerator:=left.location.register;
  72. { load divider in a register }
  73. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,true);
  74. divider:=right.location.register;
  75. { start division }
  76. if is_signed(left.resultdef) then
  77. op:=A_SDIV
  78. else
  79. op:=A_UDIV;
  80. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op,location.register,numerator,divider));
  81. { no divide-by-zero detection available in hardware, emulate (if it's a
  82. constant, this will have been detected earlier already) }
  83. if (right.nodetype<>ordconstn) then
  84. begin
  85. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const(A_CMP,
  86. right.location.register,0));
  87. current_asmdata.getjumplabel(hl);
  88. current_asmdata.CurrAsmList.concat(taicpu.op_cond_sym(A_B,C_NE,hl));
  89. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_DIVBYZERO',false);
  90. cg.a_label(current_asmdata.CurrAsmList,hl);
  91. end;
  92. { in case of overflow checking, also check for low(int64) div (-1)
  93. (no hardware support for this either) }
  94. if (cs_check_overflow in current_settings.localswitches) and
  95. is_signed(left.resultdef) and
  96. ((right.nodetype<>ordconstn) or
  97. (tordconstnode(right).value=-1)) then
  98. begin
  99. { num=ffff... and div=8000... <=>
  100. num xor not(div xor 8000...) = 0
  101. (and we have the "eon" operation, which performs "xor not(...)" }
  102. tmpreg:=hlcg.getintregister(current_asmdata.CurrAsmList,left.resultdef);
  103. hlcg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_XOR,left.resultdef,low(int64),left.location.register,tmpreg);
  104. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_EON,
  105. tmpreg,left.location.register,tmpreg));
  106. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const(A_CMP,tmpreg,0));
  107. { now the zero/equal flag is set in case we divided low(int64) by
  108. (-1) }
  109. location_reset(overflowloc,LOC_FLAGS,OS_NO);
  110. overflowloc.resflags:=F_EQ;
  111. cg.g_overflowcheck_loc(current_asmdata.CurrAsmList,location,resultdef,overflowloc);
  112. end;
  113. { in case of modulo, multiply result again by the divider and subtract
  114. from the numerator }
  115. if nodetype=modn then
  116. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg_reg(A_MSUB,resultreg,
  117. resultreg,divider,numerator));
  118. end;
  119. {*****************************************************************************
  120. taarch64notnode
  121. *****************************************************************************}
  122. procedure taarch64notnode.second_boolean;
  123. begin
  124. if not handle_locjump then
  125. begin
  126. secondpass(left);
  127. case left.location.loc of
  128. LOC_FLAGS :
  129. begin
  130. location_copy(location,left.location);
  131. inverse_flags(location.resflags);
  132. end;
  133. LOC_REGISTER, LOC_CREGISTER,
  134. LOC_REFERENCE, LOC_CREFERENCE,
  135. LOC_SUBSETREG, LOC_CSUBSETREG,
  136. LOC_SUBSETREF, LOC_CSUBSETREF:
  137. begin
  138. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  139. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const(A_CMP,
  140. left.location.register,0));
  141. location_reset(location,LOC_FLAGS,OS_NO);
  142. location.resflags:=F_EQ;
  143. end;
  144. else
  145. internalerror(2003042401);
  146. end;
  147. end;
  148. end;
  149. {*****************************************************************************
  150. taarch64unaryminusnode
  151. *****************************************************************************}
  152. procedure taarch64unaryminusnode.second_float;
  153. begin
  154. secondpass(left);
  155. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  156. location_reset(location,LOC_MMREGISTER,def_cgsize(resultdef));
  157. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,location.size);
  158. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FNEG,location.register,left.location.register));
  159. cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  160. end;
  161. begin
  162. cmoddivnode:=taarch64moddivnode;
  163. cnotnode:=taarch64notnode;
  164. cunaryminusnode:=taarch64unaryminusnode;
  165. end.