spu-c.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /* Copyright (C) 2006-2015 Free Software Foundation, Inc.
  2. This file is free software; you can redistribute it and/or modify it under
  3. the terms of the GNU General Public License as published by the Free
  4. Software Foundation; either version 3 of the License, or (at your option)
  5. any later version.
  6. This file is distributed in the hope that it will be useful, but WITHOUT
  7. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  8. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  9. for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with GCC; see the file COPYING3. If not see
  12. <http://www.gnu.org/licenses/>. */
  13. #include "config.h"
  14. #include "system.h"
  15. #include "coretypes.h"
  16. #include "tm.h"
  17. #include "cpplib.h"
  18. #include "hash-set.h"
  19. #include "machmode.h"
  20. #include "vec.h"
  21. #include "double-int.h"
  22. #include "input.h"
  23. #include "alias.h"
  24. #include "symtab.h"
  25. #include "wide-int.h"
  26. #include "inchash.h"
  27. #include "tree.h"
  28. #include "stringpool.h"
  29. #include "c-family/c-common.h"
  30. #include "c-family/c-pragma.h"
  31. #include "tm_p.h"
  32. #include "langhooks.h"
  33. #include "target.h"
  34. /* Keep the vector keywords handy for fast comparisons. */
  35. static GTY(()) tree __vector_keyword;
  36. static GTY(()) tree vector_keyword;
  37. static cpp_hashnode *
  38. spu_categorize_keyword (const cpp_token *tok)
  39. {
  40. if (tok->type == CPP_NAME)
  41. {
  42. cpp_hashnode *ident = tok->val.node.node;
  43. if (ident == C_CPP_HASHNODE (vector_keyword)
  44. || ident == C_CPP_HASHNODE (__vector_keyword))
  45. return C_CPP_HASHNODE (__vector_keyword);
  46. else
  47. return ident;
  48. }
  49. return 0;
  50. }
  51. /* Called to decide whether a conditional macro should be expanded.
  52. Since we have exactly one such macro (i.e, 'vector'), we do not
  53. need to examine the 'tok' parameter. */
  54. static cpp_hashnode *
  55. spu_macro_to_expand (cpp_reader *pfile, const cpp_token *tok)
  56. {
  57. cpp_hashnode *expand_this = tok->val.node.node;
  58. cpp_hashnode *ident;
  59. ident = spu_categorize_keyword (tok);
  60. if (ident == C_CPP_HASHNODE (__vector_keyword))
  61. {
  62. tok = cpp_peek_token (pfile, 0);
  63. ident = spu_categorize_keyword (tok);
  64. if (ident)
  65. {
  66. enum rid rid_code = (enum rid)(ident->rid_code);
  67. if (ident->type == NT_MACRO)
  68. {
  69. (void) cpp_get_token (pfile);
  70. tok = cpp_peek_token (pfile, 0);
  71. ident = spu_categorize_keyword (tok);
  72. if (ident)
  73. rid_code = (enum rid)(ident->rid_code);
  74. }
  75. if (rid_code == RID_UNSIGNED || rid_code == RID_LONG
  76. || rid_code == RID_SHORT || rid_code == RID_SIGNED
  77. || rid_code == RID_INT || rid_code == RID_CHAR
  78. || rid_code == RID_FLOAT || rid_code == RID_DOUBLE)
  79. expand_this = C_CPP_HASHNODE (__vector_keyword);
  80. }
  81. }
  82. return expand_this;
  83. }
  84. /* target hook for resolve_overloaded_builtin(). Returns a function call
  85. RTX if we can resolve the overloaded builtin */
  86. tree
  87. spu_resolve_overloaded_builtin (location_t loc, tree fndecl, void *passed_args)
  88. {
  89. #define SCALAR_TYPE_P(t) (INTEGRAL_TYPE_P (t) \
  90. || SCALAR_FLOAT_TYPE_P (t) \
  91. || POINTER_TYPE_P (t))
  92. vec<tree, va_gc> *fnargs = static_cast <vec<tree, va_gc> *> (passed_args);
  93. unsigned int nargs = vec_safe_length (fnargs);
  94. int new_fcode, fcode = DECL_FUNCTION_CODE (fndecl);
  95. struct spu_builtin_description *desc;
  96. tree match = NULL_TREE;
  97. /* The vector types are not available if the backend is not initialized. */
  98. gcc_assert (!flag_preprocess_only);
  99. desc = &spu_builtins[fcode];
  100. if (desc->type != B_OVERLOAD)
  101. return NULL_TREE;
  102. /* Compare the signature of each internal builtin function with the
  103. function arguments until a match is found. */
  104. for (new_fcode = fcode + 1; spu_builtins[new_fcode].type == B_INTERNAL;
  105. new_fcode++)
  106. {
  107. tree decl = targetm.builtin_decl (new_fcode, true);
  108. tree params = TYPE_ARG_TYPES (TREE_TYPE (decl));
  109. tree param;
  110. bool all_scalar;
  111. unsigned int p;
  112. /* Check whether all parameters are scalar. */
  113. all_scalar = true;
  114. for (param = params; param != void_list_node; param = TREE_CHAIN (param))
  115. if (!SCALAR_TYPE_P (TREE_VALUE (param)))
  116. all_scalar = false;
  117. for (param = params, p = 0;
  118. param != void_list_node;
  119. param = TREE_CHAIN (param), p++)
  120. {
  121. tree var, arg_type, param_type = TREE_VALUE (param);
  122. if (p >= nargs)
  123. {
  124. error ("insufficient arguments to overloaded function %s",
  125. desc->name);
  126. return error_mark_node;
  127. }
  128. var = (*fnargs)[p];
  129. if (TREE_CODE (var) == NON_LVALUE_EXPR)
  130. var = TREE_OPERAND (var, 0);
  131. if (TREE_CODE (var) == ERROR_MARK)
  132. return NULL_TREE; /* Let somebody else deal with the problem. */
  133. arg_type = TREE_TYPE (var);
  134. /* The intrinsics spec does not specify precisely how to
  135. resolve generic intrinsics. We require an exact match
  136. for vector types and let C do it's usual parameter type
  137. checking/promotions for scalar arguments, except for the
  138. first argument of intrinsics which don't have a vector
  139. parameter. */
  140. if ((!SCALAR_TYPE_P (param_type)
  141. || !SCALAR_TYPE_P (arg_type)
  142. || (all_scalar && p == 0))
  143. && !lang_hooks.types_compatible_p (param_type, arg_type))
  144. break;
  145. }
  146. if (param == void_list_node)
  147. {
  148. if (p != nargs)
  149. {
  150. error ("too many arguments to overloaded function %s",
  151. desc->name);
  152. return error_mark_node;
  153. }
  154. match = decl;
  155. break;
  156. }
  157. }
  158. if (match == NULL_TREE)
  159. {
  160. error ("parameter list does not match a valid signature for %s()",
  161. desc->name);
  162. return error_mark_node;
  163. }
  164. return build_function_call_vec (loc, vNULL, match, fnargs, NULL);
  165. #undef SCALAR_TYPE_P
  166. }
  167. void
  168. spu_cpu_cpp_builtins (struct cpp_reader *pfile)
  169. {
  170. cpp_define (pfile, "__SPU__");
  171. cpp_assert (pfile, "cpu=spu");
  172. cpp_assert (pfile, "machine=spu");
  173. if (spu_arch == PROCESSOR_CELLEDP)
  174. cpp_define (pfile, "__SPU_EDP__");
  175. if (cpp_get_options (pfile)->lang != CLK_ASM)
  176. cpp_define (pfile, "__vector=__attribute__((__spu_vector__))");
  177. switch (spu_ea_model)
  178. {
  179. case 32:
  180. cpp_define (pfile, "__EA32__");
  181. break;
  182. case 64:
  183. cpp_define (pfile, "__EA64__");
  184. break;
  185. default:
  186. gcc_unreachable ();
  187. }
  188. if (!flag_iso && cpp_get_options (pfile)->lang != CLK_ASM)
  189. {
  190. /* Define this when supporting context-sensitive keywords. */
  191. cpp_define (pfile, "__VECTOR_KEYWORD_SUPPORTED__");
  192. cpp_define (pfile, "vector=vector");
  193. /* Initialize vector keywords. */
  194. __vector_keyword = get_identifier ("__vector");
  195. C_CPP_HASHNODE (__vector_keyword)->flags |= NODE_CONDITIONAL;
  196. vector_keyword = get_identifier ("vector");
  197. C_CPP_HASHNODE (vector_keyword)->flags |= NODE_CONDITIONAL;
  198. /* Enable context-sensitive macros. */
  199. cpp_get_callbacks (pfile)->macro_to_expand = spu_macro_to_expand;
  200. }
  201. }
  202. void
  203. spu_c_common_override_options (void)
  204. {
  205. if (!TARGET_STD_MAIN)
  206. {
  207. /* Don't give warnings about the main() function. */
  208. warn_main = 0;
  209. }
  210. }