ncpuinl.pas 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Generates ARM inline 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 ncpuinl;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,ninl,ncginl;
  22. type
  23. taarch64inlinenode = class(tcgInlineNode)
  24. function first_abs_real: tnode; override;
  25. function first_sqr_real: tnode; override;
  26. function first_sqrt_real: tnode; override;
  27. function first_round_real: tnode; override;
  28. function first_trunc_real: tnode; override;
  29. function first_fma : tnode; override;
  30. procedure second_abs_real; override;
  31. procedure second_sqr_real; override;
  32. procedure second_sqrt_real; override;
  33. procedure second_abs_long; override;
  34. procedure second_round_real; override;
  35. procedure second_trunc_real; override;
  36. procedure second_get_frame; override;
  37. procedure second_fma; override;
  38. private
  39. procedure load_fpu_location;
  40. end;
  41. implementation
  42. uses
  43. globtype,verbose,globals,
  44. cpuinfo, defutil,symdef,aasmdata,aasmcpu,
  45. cgbase,cgutils,pass_1,pass_2,
  46. ncal,
  47. cpubase,ncgutil,cgobj,cgcpu, hlcgobj;
  48. {*****************************************************************************
  49. taarch64inlinenode
  50. *****************************************************************************}
  51. procedure taarch64inlinenode.load_fpu_location;
  52. begin
  53. secondpass(left);
  54. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  55. location_copy(location,left.location);
  56. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,location.size);
  57. location.loc:=LOC_MMREGISTER;
  58. end;
  59. function taarch64inlinenode.first_abs_real : tnode;
  60. begin
  61. expectloc:=LOC_MMREGISTER;
  62. result:=nil;
  63. end;
  64. function taarch64inlinenode.first_sqr_real : tnode;
  65. begin
  66. expectloc:=LOC_MMREGISTER;
  67. result:=nil;
  68. end;
  69. function taarch64inlinenode.first_sqrt_real : tnode;
  70. begin
  71. expectloc:=LOC_MMREGISTER;
  72. result:=nil;
  73. end;
  74. function taarch64inlinenode.first_round_real: tnode;
  75. begin
  76. expectloc:=LOC_MMREGISTER;
  77. result:=nil;
  78. end;
  79. function taarch64inlinenode.first_trunc_real: tnode;
  80. begin
  81. expectloc:=LOC_MMREGISTER;
  82. result:=nil;
  83. end;
  84. function taarch64inlinenode.first_fma : tnode;
  85. begin
  86. if ((is_double(resultdef)) or (is_single(resultdef))) then
  87. begin
  88. expectloc:=LOC_MMREGISTER;
  89. Result:=nil;
  90. end
  91. else
  92. Result:=inherited first_fma;
  93. end;
  94. procedure taarch64inlinenode.second_abs_real;
  95. begin
  96. load_fpu_location;
  97. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FABS,location.register,left.location.register));
  98. cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  99. end;
  100. procedure taarch64inlinenode.second_sqr_real;
  101. begin
  102. load_fpu_location;
  103. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_FMUL,location.register,left.location.register,left.location.register));
  104. cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  105. end;
  106. procedure taarch64inlinenode.second_sqrt_real;
  107. begin
  108. load_fpu_location;
  109. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FSQRT,location.register,left.location.register));
  110. cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  111. end;
  112. procedure taarch64inlinenode.second_abs_long;
  113. var
  114. opsize : tcgsize;
  115. begin
  116. secondpass(left);
  117. opsize:=def_cgsize(left.resultdef);
  118. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  119. location:=left.location;
  120. location.register:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  121. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg(A_NEG,location.register,left.location.register),PF_S));
  122. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg_cond(A_CSEL,location.register,location.register,left.location.register,C_GE));
  123. end;
  124. procedure taarch64inlinenode.second_round_real;
  125. var
  126. hreg: tregister;
  127. begin
  128. secondpass(left);
  129. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  130. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  131. location.register:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  132. hreg:=cg.getmmregister(current_asmdata.CurrAsmList,left.location.size);
  133. { round as floating point using current rounding mode }
  134. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FRINTX,hreg,left.location.register));
  135. { convert to signed integer rounding towards zero (there's no "round to
  136. integer using current rounding mode") }
  137. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FCVTZS,location.register,hreg));
  138. cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  139. end;
  140. procedure taarch64inlinenode.second_trunc_real;
  141. begin
  142. secondpass(left);
  143. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  144. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  145. location.register:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  146. { convert to signed integer rounding towards zero }
  147. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FCVTZS,location.register,left.location.register));
  148. end;
  149. procedure taarch64inlinenode.second_get_frame;
  150. begin
  151. location_reset(location,LOC_CREGISTER,OS_ADDR);
  152. { this routine is used to get the frame pointer for backtracing
  153. purposes. current_procinfo.framepointer is set to SP because that one
  154. is used to access temps. On most platforms these two frame pointers
  155. are the same, but not on AArch64. }
  156. location.register:=NR_FRAME_POINTER_REG;
  157. end;
  158. procedure taarch64inlinenode.second_fma;
  159. const
  160. op : array[false..true,false..true] of TAsmOp =
  161. { positive product }
  162. (
  163. { positive third operand }
  164. (A_FMADD,
  165. { negative third operand }
  166. A_FNMSUB),
  167. { negative product }
  168. { positive third operand }
  169. (A_FMSUB,
  170. A_FNMADD)
  171. );
  172. var
  173. paraarray : array[1..3] of tnode;
  174. i : integer;
  175. negop3,
  176. negproduct : boolean;
  177. begin
  178. negop3:=false;
  179. negproduct:=false;
  180. paraarray[1]:=tcallparanode(tcallparanode(tcallparanode(parameters).nextpara).nextpara).paravalue;
  181. paraarray[2]:=tcallparanode(tcallparanode(parameters).nextpara).paravalue;
  182. paraarray[3]:=tcallparanode(parameters).paravalue;
  183. { check if a neg. node can be removed
  184. this is possible because changing the sign of
  185. a floating point number does not affect its absolute
  186. value in any way
  187. }
  188. if paraarray[1].nodetype=unaryminusn then
  189. begin
  190. paraarray[1]:=tunarynode(paraarray[1]).left;
  191. { do not release the unused unary minus node, it is kept and release together with the other nodes,
  192. only no code is generated for it }
  193. negproduct:=not(negproduct);
  194. end;
  195. if paraarray[2].nodetype=unaryminusn then
  196. begin
  197. paraarray[2]:=tunarynode(paraarray[2]).left;
  198. { do not release the unused unary minus node, it is kept and release together with the other nodes,
  199. only no code is generated for it }
  200. negproduct:=not(negproduct);
  201. end;
  202. if paraarray[3].nodetype=unaryminusn then
  203. begin
  204. paraarray[3]:=tunarynode(paraarray[3]).left;
  205. { do not release the unused unary minus node, it is kept and release together with the other nodes,
  206. only no code is generated for it }
  207. negop3:=true;
  208. end;
  209. for i:=1 to 3 do
  210. secondpass(paraarray[i]);
  211. { no memory operand is allowed }
  212. for i:=1 to 3 do
  213. begin
  214. if not(paraarray[i].location.loc in [LOC_MMREGISTER,LOC_CMMREGISTER]) then
  215. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,paraarray[i].location,paraarray[i].resultdef,true);
  216. end;
  217. location_reset(location,LOC_MMREGISTER,paraarray[1].location.size);
  218. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,location.size);
  219. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg_reg(op[negproduct,negop3],
  220. location.register,paraarray[1].location.register,paraarray[2].location.register,paraarray[3].location.register));
  221. cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  222. end;
  223. begin
  224. cinlinenode:=taarch64inlinenode;
  225. end.