Merged trunk at revision 161680 into branch.
[official-gcc.git] / gcc / config / spu / spu-c.c
blobce5f92d94db6fa9118df20faee1801a2a3cd4beb
1 /* Copyright (C) 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
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.
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.
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/>. */
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-family/c-common.h"
24 #include "c-family/c-pragma.h"
25 #include "tm_p.h"
26 #include "langhooks.h"
29 /* Keep the vector keywords handy for fast comparisons. */
30 static GTY(()) tree __vector_keyword;
31 static GTY(()) tree vector_keyword;
33 static cpp_hashnode *
34 spu_categorize_keyword (const cpp_token *tok)
36 if (tok->type == CPP_NAME)
38 cpp_hashnode *ident = tok->val.node.node;
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;
46 return 0;
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. */
53 static cpp_hashnode *
54 spu_macro_to_expand (cpp_reader *pfile, const cpp_token *tok)
56 cpp_hashnode *expand_this = tok->val.node.node;
57 cpp_hashnode *ident;
59 ident = spu_categorize_keyword (tok);
60 if (ident == C_CPP_HASHNODE (__vector_keyword))
62 tok = cpp_peek_token (pfile, 0);
63 ident = spu_categorize_keyword (tok);
65 if (ident)
67 enum rid rid_code = (enum rid)(ident->rid_code);
68 if (ident->type == NT_MACRO)
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);
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);
84 return expand_this;
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)
92 #define SCALAR_TYPE_P(t) (INTEGRAL_TYPE_P (t) \
93 || SCALAR_FLOAT_TYPE_P (t) \
94 || POINTER_TYPE_P (t))
95 VEC(tree,gc) *fnargs = (VEC(tree,gc) *) passed_args;
96 unsigned int nargs = VEC_length (tree, fnargs);
97 int new_fcode, fcode = DECL_FUNCTION_CODE (fndecl) - END_BUILTINS;
98 struct spu_builtin_description *desc;
99 tree match = NULL_TREE;
101 /* The vector types are not available if the backend is not initialized. */
102 gcc_assert (!flag_preprocess_only);
104 desc = &spu_builtins[fcode];
105 if (desc->type != B_OVERLOAD)
106 return NULL_TREE;
108 /* Compare the signature of each internal builtin function with the
109 function arguments until a match is found. */
111 for (new_fcode = fcode + 1; spu_builtins[new_fcode].type == B_INTERNAL;
112 new_fcode++)
114 tree decl = spu_builtins[new_fcode].fndecl;
115 tree params = TYPE_ARG_TYPES (TREE_TYPE (decl));
116 tree param;
117 bool all_scalar;
118 unsigned int p;
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;
126 for (param = params, p = 0;
127 param != void_list_node;
128 param = TREE_CHAIN (param), p++)
130 tree var, arg_type, param_type = TREE_VALUE (param);
132 if (p >= nargs)
134 error ("insufficient arguments to overloaded function %s",
135 desc->name);
136 return error_mark_node;
139 var = VEC_index (tree, fnargs, p);
141 if (TREE_CODE (var) == NON_LVALUE_EXPR)
142 var = TREE_OPERAND (var, 0);
144 if (TREE_CODE (var) == ERROR_MARK)
145 return NULL_TREE; /* Let somebody else deal with the problem. */
147 arg_type = TREE_TYPE (var);
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;
161 if (param == void_list_node)
163 if (p != nargs)
165 error ("too many arguments to overloaded function %s",
166 desc->name);
167 return error_mark_node;
170 match = decl;
171 break;
175 if (match == NULL_TREE)
177 error ("parameter list does not match a valid signature for %s()",
178 desc->name);
179 return error_mark_node;
182 return build_function_call_vec (loc, match, fnargs, NULL);
183 #undef SCALAR_TYPE_P
187 void
188 spu_cpu_cpp_builtins (struct cpp_reader *pfile)
190 builtin_define_std ("__SPU__");
191 cpp_assert (pfile, "cpu=spu");
192 cpp_assert (pfile, "machine=spu");
193 if (spu_arch == PROCESSOR_CELLEDP)
194 builtin_define_std ("__SPU_EDP__");
195 builtin_define_std ("__vector=__attribute__((__spu_vector__))");
196 switch (spu_ea_model)
198 case 32:
199 builtin_define_std ("__EA32__");
200 break;
201 case 64:
202 builtin_define_std ("__EA64__");
203 break;
204 default:
205 gcc_unreachable ();
208 if (!flag_iso)
210 /* Define this when supporting context-sensitive keywords. */
211 cpp_define (pfile, "__VECTOR_KEYWORD_SUPPORTED__");
212 cpp_define (pfile, "vector=vector");
214 /* Initialize vector keywords. */
215 __vector_keyword = get_identifier ("__vector");
216 C_CPP_HASHNODE (__vector_keyword)->flags |= NODE_CONDITIONAL;
217 vector_keyword = get_identifier ("vector");
218 C_CPP_HASHNODE (vector_keyword)->flags |= NODE_CONDITIONAL;
220 /* Enable context-sensitive macros. */
221 cpp_get_callbacks (pfile)->macro_to_expand = spu_macro_to_expand;
225 void
226 spu_c_common_override_options (void)
228 if (!TARGET_STD_MAIN)
230 /* Don't give warnings about the main() function. */
231 warn_main = 0;