* call.c: Fix comment formatting.
[official-gcc.git] / gcc / java / builtins.c
blobeba84161363f94893f47e6fbb2d031c9df6a60e8
1 /* Built-in and inline functions for gcj
2 Copyright (C) 2001
3 Free Software Foundation, Inc.
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.
22 Java and all Java-based marks are trademarks or registered trademarks
23 of Sun Microsystems, Inc. in the United States and other countries.
24 The Free Software Foundation is independent of Sun Microsystems, Inc. */
26 /* Written by Tom Tromey <tromey@redhat.com>. */
28 #include "config.h"
29 #include "system.h"
30 #include "tree.h"
31 #include "ggc.h"
32 #include "flags.h"
33 #include "langhooks.h"
34 #include "java-tree.h"
36 enum builtin_type
38 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
39 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
40 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
41 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
42 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
43 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
44 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
45 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
46 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
47 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
48 #include "builtin-types.def"
49 #undef DEF_PRIMITIVE_TYPE
50 #undef DEF_FUNCTION_TYPE_0
51 #undef DEF_FUNCTION_TYPE_1
52 #undef DEF_FUNCTION_TYPE_2
53 #undef DEF_FUNCTION_TYPE_3
54 #undef DEF_FUNCTION_TYPE_4
55 #undef DEF_FUNCTION_TYPE_VAR_0
56 #undef DEF_FUNCTION_TYPE_VAR_1
57 #undef DEF_FUNCTION_TYPE_VAR_2
58 #undef DEF_POINTER_TYPE
59 BT_LAST
62 static tree max_builtin PARAMS ((tree, tree));
63 static tree min_builtin PARAMS ((tree, tree));
64 static tree abs_builtin PARAMS ((tree, tree));
65 static tree cos_builtin PARAMS ((tree, tree));
66 static tree sin_builtin PARAMS ((tree, tree));
67 static tree sqrt_builtin PARAMS ((tree, tree));
69 static tree build_function_call_expr PARAMS ((tree, tree));
70 static void define_builtin PARAMS ((enum built_in_function,
71 const char *,
72 enum built_in_class,
73 tree, int));
74 static tree define_builtin_type PARAMS ((int, int, int, int, int));
78 /* Functions of this type are used to inline a given call. Such a
79 function should either return an expression, if the call is to be
80 inlined, or NULL_TREE if a real call should be emitted. Arguments
81 are method return type and arguments to call. */
82 typedef tree builtin_creator_function PARAMS ((tree, tree));
84 /* Hold a char*, before initialization, or a tree, after
85 initialization. */
86 union string_or_tree GTY(())
88 const char * GTY ((tag ("0"))) s;
89 tree GTY ((tag ("1"))) t;
92 /* Used to hold a single builtin record. */
93 struct builtin_record GTY(())
95 union string_or_tree GTY ((desc ("1"))) class_name;
96 union string_or_tree GTY ((desc ("1"))) method_name;
97 builtin_creator_function * GTY((skip (""))) creator;
100 static GTY(()) struct builtin_record java_builtins[] =
102 { { "java.lang.Math" }, { "min" }, min_builtin },
103 { { "java.lang.Math" }, { "max" }, max_builtin },
104 { { "java.lang.Math" }, { "abs" }, abs_builtin },
105 { { "java.lang.Math" }, { "cos" }, cos_builtin },
106 { { "java.lang.Math" }, { "sin" }, sin_builtin },
107 { { "java.lang.Math" }, { "sqrt" }, sqrt_builtin },
108 { { NULL }, { NULL }, NULL }
111 /* This is only used transiently, so we don't mark it as roots for the
112 GC. */
113 static tree builtin_types[(int) BT_LAST];
116 /* Internal functions which implement various builtin conversions. */
118 static tree
119 max_builtin (method_return_type, method_arguments)
120 tree method_return_type, method_arguments;
122 return build (MAX_EXPR, method_return_type,
123 TREE_VALUE (method_arguments),
124 TREE_VALUE (TREE_CHAIN (method_arguments)));
127 static tree
128 min_builtin (method_return_type, method_arguments)
129 tree method_return_type, method_arguments;
131 return build (MIN_EXPR, method_return_type,
132 TREE_VALUE (method_arguments),
133 TREE_VALUE (TREE_CHAIN (method_arguments)));
136 static tree
137 abs_builtin (method_return_type, method_arguments)
138 tree method_return_type, method_arguments;
140 return build1 (ABS_EXPR, method_return_type,
141 TREE_VALUE (method_arguments));
144 /* Mostly copied from ../builtins.c. */
145 static tree
146 build_function_call_expr (tree fn, tree arglist)
148 tree call_expr;
150 call_expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
151 call_expr = build (CALL_EXPR, TREE_TYPE (TREE_TYPE (fn)),
152 call_expr, arglist);
153 TREE_SIDE_EFFECTS (call_expr) = 1;
154 return call_expr;
157 static tree
158 cos_builtin (method_return_type, method_arguments)
159 tree method_return_type ATTRIBUTE_UNUSED, method_arguments;
161 /* FIXME: this assumes that jdouble and double are the same. */
162 tree fn = built_in_decls[BUILT_IN_COS];
163 if (fn == NULL_TREE)
164 return NULL_TREE;
165 return build_function_call_expr (fn, method_arguments);
168 static tree
169 sin_builtin (method_return_type, method_arguments)
170 tree method_return_type ATTRIBUTE_UNUSED, method_arguments;
172 /* FIXME: this assumes that jdouble and double are the same. */
173 tree fn = built_in_decls[BUILT_IN_SIN];
174 if (fn == NULL_TREE)
175 return NULL_TREE;
176 return build_function_call_expr (fn, method_arguments);
179 static tree
180 sqrt_builtin (method_return_type, method_arguments)
181 tree method_return_type ATTRIBUTE_UNUSED, method_arguments;
183 /* FIXME: this assumes that jdouble and double are the same. */
184 tree fn = built_in_decls[BUILT_IN_SQRT];
185 if (fn == NULL_TREE)
186 return NULL_TREE;
187 return build_function_call_expr (fn, method_arguments);
192 /* Define a single builtin. */
193 static void
194 define_builtin (val, name, class, type, fallback_p)
195 enum built_in_function val;
196 const char *name;
197 enum built_in_class class;
198 tree type;
199 int fallback_p;
201 tree decl;
203 if (! name || ! type)
204 return;
206 if (strncmp (name, "__builtin_", strlen ("__builtin_")) != 0)
207 abort ();
208 decl = build_decl (FUNCTION_DECL, get_identifier (name), type);
209 DECL_EXTERNAL (decl) = 1;
210 TREE_PUBLIC (decl) = 1;
211 if (fallback_p)
212 SET_DECL_ASSEMBLER_NAME (decl,
213 get_identifier (name + strlen ("__builtin_")));
214 make_decl_rtl (decl, NULL);
215 pushdecl (decl);
216 DECL_BUILT_IN_CLASS (decl) = class;
217 DECL_FUNCTION_CODE (decl) = val;
218 built_in_decls[val] = decl;
221 /* Compute the type for a builtin. */
222 static tree
223 define_builtin_type (ret, arg1, arg2, arg3, arg4)
224 int ret, arg1, arg2, arg3, arg4;
226 tree args;
228 if (builtin_types[ret] == NULL_TREE)
229 return NULL_TREE;
231 args = void_list_node;
233 if (arg4 != -1)
235 if (builtin_types[arg4] == NULL_TREE)
236 return NULL_TREE;
237 args = tree_cons (NULL_TREE, builtin_types[arg4], args);
239 if (arg3 != -1)
241 if (builtin_types[arg3] == NULL_TREE)
242 return NULL_TREE;
243 args = tree_cons (NULL_TREE, builtin_types[arg3], args);
245 if (arg2 != -1)
247 if (builtin_types[arg2] == NULL_TREE)
248 return NULL_TREE;
249 args = tree_cons (NULL_TREE, builtin_types[arg2], args);
251 if (arg1 != -1)
253 if (builtin_types[arg1] == NULL_TREE)
254 return NULL_TREE;
255 args = tree_cons (NULL_TREE, builtin_types[arg1], args);
258 return build_function_type (builtin_types[ret], args);
263 /* Initialize the builtins. */
264 void
265 initialize_builtins ()
267 int i;
269 for (i = 0; java_builtins[i].creator != NULL; ++i)
271 tree klass_id = get_identifier (java_builtins[i].class_name.s);
272 tree m = get_identifier (java_builtins[i].method_name.s);
274 java_builtins[i].class_name.t = klass_id;
275 java_builtins[i].method_name.t = m;
278 void_list_node = end_params_node;
280 /* Work around C-specific junk in builtin-types.def. */
281 #define intmax_type_node NULL_TREE
282 #define c_size_type_node NULL_TREE
283 #define const_string_type_node NULL_TREE
284 #define va_list_ref_type_node NULL_TREE
285 #define va_list_arg_type_node NULL_TREE
286 #define flag_isoc99 0
288 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
289 builtin_types[(int) ENUM] = VALUE;
290 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
291 builtin_types[(int) ENUM] \
292 = define_builtin_type (RETURN, -1, -1, -1, -1);
293 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
294 builtin_types[(int) ENUM] \
295 = define_builtin_type (RETURN, ARG1, -1, -1, -1);
296 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
297 builtin_types[(int) ENUM] \
298 = define_builtin_type (RETURN, ARG1, ARG2, -1, -1);
299 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
300 builtin_types[(int) ENUM] \
301 = define_builtin_type (RETURN, ARG1, ARG2, ARG3, -1);
302 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
303 builtin_types[(int) ENUM] \
304 = define_builtin_type (RETURN, ARG1, ARG2, ARG3, ARG4);
305 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
306 builtin_types[(int) ENUM] = NULL_TREE;
307 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
308 builtin_types[(int) ENUM] = NULL_TREE;
309 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
310 builtin_types[(int) ENUM] = NULL_TREE;
311 #define DEF_POINTER_TYPE(ENUM, TYPE) \
312 builtin_types[(int) ENUM] = NULL_TREE;
314 #include "builtin-types.def"
316 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, \
317 FALLBACK_P, NONANSI_P, ATTRS) \
318 define_builtin (ENUM, NAME, CLASS, builtin_types[TYPE], FALLBACK_P);
319 #include "builtins.def"
322 /* If the call matches a builtin, return the
323 appropriate builtin expression instead. */
324 tree
325 check_for_builtin (method, call)
326 tree method;
327 tree call;
329 if (! flag_emit_class_files && optimize && TREE_CODE (call) == CALL_EXPR)
331 int i;
332 tree method_arguments = TREE_OPERAND (call, 1);
333 tree method_class = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method)));
334 tree method_name = DECL_NAME (method);
335 tree method_return_type = TREE_TYPE (TREE_TYPE (method));
337 for (i = 0; java_builtins[i].creator != NULL; ++i)
339 if (method_class == java_builtins[i].class_name.t
340 && method_name == java_builtins[i].method_name.t)
342 return (*java_builtins[i].creator) (method_return_type,
343 method_arguments);
347 return call;
350 #include "gt-java-builtins.h"