spu-c.c revision 1.10 1 /* Copyright (C) 2006-2019 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 #define IN_TARGET_CODE 1
18
19 #include "config.h"
20 #include "system.h"
21 #include "coretypes.h"
22 #include "target.h"
23 #include "c-family/c-common.h"
24 #include "stringpool.h"
25 #include "langhooks.h"
26
27
29 /* Keep the vector keywords handy for fast comparisons. */
30 static GTY(()) tree __vector_keyword;
31 static GTY(()) tree vector_keyword;
32
33 static cpp_hashnode *
34 spu_categorize_keyword (const cpp_token *tok)
35 {
36 if (tok->type == CPP_NAME)
37 {
38 cpp_hashnode *ident = tok->val.node.node;
39
40 if (ident == C_CPP_HASHNODE (vector_keyword)
41 || ident == C_CPP_HASHNODE (__vector_keyword))
42 return C_CPP_HASHNODE (__vector_keyword);
43 else
44 return ident;
45 }
46 return 0;
47 }
48
49 /* Called to decide whether a conditional macro should be expanded.
50 Since we have exactly one such macro (i.e, 'vector'), we do not
51 need to examine the 'tok' parameter. */
52
53 static cpp_hashnode *
54 spu_macro_to_expand (cpp_reader *pfile, const cpp_token *tok)
55 {
56 cpp_hashnode *expand_this = tok->val.node.node;
57 cpp_hashnode *ident;
58
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
65 if (ident)
66 {
67 enum rid rid_code = (enum rid)(ident->rid_code);
68 if (cpp_macro_p (ident))
69 {
70 (void) cpp_get_token (pfile);
71 tok = cpp_peek_token (pfile, 0);
72 ident = spu_categorize_keyword (tok);
73 if (ident)
74 rid_code = (enum rid)(ident->rid_code);
75 }
76
77 if (rid_code == RID_UNSIGNED || rid_code == RID_LONG
78 || rid_code == RID_SHORT || rid_code == RID_SIGNED
79 || rid_code == RID_INT || rid_code == RID_CHAR
80 || rid_code == RID_FLOAT || rid_code == RID_DOUBLE)
81 expand_this = C_CPP_HASHNODE (__vector_keyword);
82 }
83 }
84 return expand_this;
85 }
86
87 /* target hook for resolve_overloaded_builtin(). Returns a function call
88 RTX if we can resolve the overloaded builtin */
89 tree
90 spu_resolve_overloaded_builtin (location_t loc, tree fndecl, void *passed_args)
91 {
92 #define SCALAR_TYPE_P(t) (INTEGRAL_TYPE_P (t) \
93 || SCALAR_FLOAT_TYPE_P (t) \
94 || POINTER_TYPE_P (t))
95 vec<tree, va_gc> *fnargs = static_cast <vec<tree, va_gc> *> (passed_args);
96 unsigned int nargs = vec_safe_length (fnargs);
97 int new_fcode, fcode = DECL_FUNCTION_CODE (fndecl);
98 struct spu_builtin_description *desc;
99 tree match = NULL_TREE;
100
101 /* The vector types are not available if the backend is not initialized. */
102 gcc_assert (!flag_preprocess_only);
103
104 desc = &spu_builtins[fcode];
105 if (desc->type != B_OVERLOAD)
106 return NULL_TREE;
107
108 /* Compare the signature of each internal builtin function with the
109 function arguments until a match is found. */
110
111 for (new_fcode = fcode + 1; spu_builtins[new_fcode].type == B_INTERNAL;
112 new_fcode++)
113 {
114 tree decl = targetm.builtin_decl (new_fcode, true);
115 tree params = TYPE_ARG_TYPES (TREE_TYPE (decl));
116 tree param;
117 bool all_scalar;
118 unsigned int p;
119
120 /* Check whether all parameters are scalar. */
121 all_scalar = true;
122 for (param = params; param != void_list_node; param = TREE_CHAIN (param))
123 if (!SCALAR_TYPE_P (TREE_VALUE (param)))
124 all_scalar = false;
125
126 for (param = params, p = 0;
127 param != void_list_node;
128 param = TREE_CHAIN (param), p++)
129 {
130 tree var, arg_type, param_type = TREE_VALUE (param);
131
132 if (p >= nargs)
133 {
134 error ("insufficient arguments to overloaded function %s",
135 desc->name);
136 return error_mark_node;
137 }
138
139 var = (*fnargs)[p];
140
141 if (TREE_CODE (var) == NON_LVALUE_EXPR)
142 var = TREE_OPERAND (var, 0);
143
144 if (TREE_CODE (var) == ERROR_MARK)
145 return NULL_TREE; /* Let somebody else deal with the problem. */
146
147 arg_type = TREE_TYPE (var);
148
149 /* The intrinsics spec does not specify precisely how to
150 resolve generic intrinsics. We require an exact match
151 for vector types and let C do it's usual parameter type
152 checking/promotions for scalar arguments, except for the
153 first argument of intrinsics which don't have a vector
154 parameter. */
155 if ((!SCALAR_TYPE_P (param_type)
156 || !SCALAR_TYPE_P (arg_type)
157 || (all_scalar && p == 0))
158 && !lang_hooks.types_compatible_p (param_type, arg_type))
159 break;
160 }
161 if (param == void_list_node)
162 {
163 if (p != nargs)
164 {
165 error ("too many arguments to overloaded function %s",
166 desc->name);
167 return error_mark_node;
168 }
169
170 match = decl;
171 break;
172 }
173 }
174
175 if (match == NULL_TREE)
176 {
177 error ("parameter list does not match a valid signature for %s()",
178 desc->name);
179 return error_mark_node;
180 }
181
182 return build_function_call_vec (loc, vNULL, match, fnargs, NULL);
183 #undef SCALAR_TYPE_P
184 }
185
186
187 void
188 spu_cpu_cpp_builtins (struct cpp_reader *pfile)
189 {
190 cpp_define (pfile, "__SPU__");
191 cpp_assert (pfile, "cpu=spu");
192 cpp_assert (pfile, "machine=spu");
193 if (spu_arch == PROCESSOR_CELLEDP)
194 cpp_define (pfile, "__SPU_EDP__");
195 if (cpp_get_options (pfile)->lang != CLK_ASM)
196 cpp_define (pfile, "__vector=__attribute__((__spu_vector__))");
197 switch (spu_ea_model)
198 {
199 case 32:
200 cpp_define (pfile, "__EA32__");
201 break;
202 case 64:
203 cpp_define (pfile, "__EA64__");
204 break;
205 default:
206 gcc_unreachable ();
207 }
208
209 if (!flag_iso && cpp_get_options (pfile)->lang != CLK_ASM)
210 {
211 /* Define this when supporting context-sensitive keywords. */
212 cpp_define (pfile, "__VECTOR_KEYWORD_SUPPORTED__");
213 cpp_define (pfile, "vector=vector");
214
215 /* Initialize vector keywords. */
216 __vector_keyword = get_identifier ("__vector");
217 C_CPP_HASHNODE (__vector_keyword)->flags |= NODE_CONDITIONAL;
218 vector_keyword = get_identifier ("vector");
219 C_CPP_HASHNODE (vector_keyword)->flags |= NODE_CONDITIONAL;
220
221 /* Enable context-sensitive macros. */
222 cpp_get_callbacks (pfile)->macro_to_expand = spu_macro_to_expand;
223 }
224 }
225
226 void
227 spu_c_common_override_options (void)
228 {
229 if (!TARGET_STD_MAIN)
230 {
231 /* Don't give warnings about the main() function. */
232 warn_main = 0;
233 }
234 }
235