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