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