1 /* Built-in and inline functions for gcj
2 Copyright (C) 2001, 2003
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC 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)
12 GCC 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 GCC; 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>. */
30 #include "coretypes.h"
35 #include "langhooks.h"
36 #include "java-tree.h"
40 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
41 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
42 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
43 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
44 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
45 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
46 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
47 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
48 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
49 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
50 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
51 #include "builtin-types.def"
52 #undef DEF_PRIMITIVE_TYPE
53 #undef DEF_FUNCTION_TYPE_0
54 #undef DEF_FUNCTION_TYPE_1
55 #undef DEF_FUNCTION_TYPE_2
56 #undef DEF_FUNCTION_TYPE_3
57 #undef DEF_FUNCTION_TYPE_4
58 #undef DEF_FUNCTION_TYPE_VAR_0
59 #undef DEF_FUNCTION_TYPE_VAR_1
60 #undef DEF_FUNCTION_TYPE_VAR_2
61 #undef DEF_FUNCTION_TYPE_VAR_3
62 #undef DEF_POINTER_TYPE
66 static tree max_builtin (tree, tree);
67 static tree min_builtin (tree, tree);
68 static tree abs_builtin (tree, tree);
70 static tree java_build_function_call_expr (tree, tree);
71 static void define_builtin (enum built_in_function, const char *,
72 enum built_in_class, tree, int, int);
73 static tree define_builtin_type (int, int, int, int, int);
77 /* Functions of this type are used to inline a given call. Such a
78 function should either return an expression, if the call is to be
79 inlined, or NULL_TREE if a real call should be emitted. Arguments
80 are method return type and arguments to call. */
81 typedef tree builtin_creator_function PARAMS ((tree, tree));
83 /* Hold a char*, before initialization, or a tree, after
85 union string_or_tree GTY(())
87 const char * GTY ((tag ("0"))) s;
88 tree GTY ((tag ("1"))) t;
91 /* Used to hold a single builtin record. */
92 struct builtin_record GTY(())
94 union string_or_tree GTY ((desc ("1"))) class_name;
95 union string_or_tree GTY ((desc ("1"))) method_name;
96 builtin_creator_function * GTY((skip (""))) creator;
97 enum built_in_function builtin_code;
100 static GTY(()) struct builtin_record java_builtins[] =
102 { { "java.lang.Math" }, { "min" }, min_builtin, 0 },
103 { { "java.lang.Math" }, { "max" }, max_builtin, 0 },
104 { { "java.lang.Math" }, { "abs" }, abs_builtin, 0 },
105 { { "java.lang.Math" }, { "atan" }, NULL, BUILT_IN_ATAN },
106 { { "java.lang.Math" }, { "atan2" }, NULL, BUILT_IN_ATAN2 },
107 { { "java.lang.Math" }, { "cos" }, NULL, BUILT_IN_COS },
108 { { "java.lang.Math" }, { "exp" }, NULL, BUILT_IN_EXP },
109 { { "java.lang.Math" }, { "log" }, NULL, BUILT_IN_LOG },
110 { { "java.lang.Math" }, { "pow" }, NULL, BUILT_IN_POW },
111 { { "java.lang.Math" }, { "sin" }, NULL, BUILT_IN_SIN },
112 { { "java.lang.Math" }, { "sqrt" }, NULL, BUILT_IN_SQRT },
113 { { "java.lang.Math" }, { "tan" }, NULL, BUILT_IN_TAN },
114 { { NULL }, { NULL }, NULL, END_BUILTINS }
117 /* This is only used transiently, so we don't mark it as roots for the
119 static tree builtin_types[(int) BT_LAST];
122 /* Internal functions which implement various builtin conversions. */
125 max_builtin (tree method_return_type, tree method_arguments)
127 return fold (build (MAX_EXPR, method_return_type,
128 TREE_VALUE (method_arguments),
129 TREE_VALUE (TREE_CHAIN (method_arguments))));
133 min_builtin (tree method_return_type, tree method_arguments)
135 return fold (build (MIN_EXPR, method_return_type,
136 TREE_VALUE (method_arguments),
137 TREE_VALUE (TREE_CHAIN (method_arguments))));
141 abs_builtin (tree method_return_type, tree method_arguments)
143 return fold (build1 (ABS_EXPR, method_return_type,
144 TREE_VALUE (method_arguments)));
147 /* Mostly copied from ../builtins.c. */
149 java_build_function_call_expr (tree fn, tree arglist)
153 call_expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
154 call_expr = build (CALL_EXPR, TREE_TYPE (TREE_TYPE (fn)),
156 TREE_SIDE_EFFECTS (call_expr) = 1;
157 return fold (call_expr);
162 /* Define a single builtin. */
164 define_builtin (enum built_in_function val,
166 enum built_in_class class,
173 if (! name || ! type)
176 if (strncmp (name, "__builtin_", strlen ("__builtin_")) != 0)
178 decl = build_decl (FUNCTION_DECL, get_identifier (name), type);
179 DECL_EXTERNAL (decl) = 1;
180 TREE_PUBLIC (decl) = 1;
182 SET_DECL_ASSEMBLER_NAME (decl,
183 get_identifier (name + strlen ("__builtin_")));
184 make_decl_rtl (decl, NULL);
186 DECL_BUILT_IN_CLASS (decl) = class;
187 DECL_FUNCTION_CODE (decl) = val;
188 built_in_decls[val] = decl;
190 implicit_built_in_decls[val] = decl;
193 /* Compute the type for a builtin. */
195 define_builtin_type (int ret, int arg1, int arg2, int arg3, int arg4)
199 if (builtin_types[ret] == NULL_TREE)
202 args = void_list_node;
206 if (builtin_types[arg4] == NULL_TREE)
208 args = tree_cons (NULL_TREE, builtin_types[arg4], args);
212 if (builtin_types[arg3] == NULL_TREE)
214 args = tree_cons (NULL_TREE, builtin_types[arg3], args);
218 if (builtin_types[arg2] == NULL_TREE)
220 args = tree_cons (NULL_TREE, builtin_types[arg2], args);
224 if (builtin_types[arg1] == NULL_TREE)
226 args = tree_cons (NULL_TREE, builtin_types[arg1], args);
229 return build_function_type (builtin_types[ret], args);
234 /* Initialize the builtins. */
236 initialize_builtins (void)
240 for (i = 0; java_builtins[i].builtin_code != END_BUILTINS; ++i)
242 tree klass_id = get_identifier (java_builtins[i].class_name.s);
243 tree m = get_identifier (java_builtins[i].method_name.s);
245 java_builtins[i].class_name.t = klass_id;
246 java_builtins[i].method_name.t = m;
249 void_list_node = end_params_node;
251 /* Work around C-specific junk in builtin-types.def. */
252 #define intmax_type_node NULL_TREE
253 #define c_size_type_node NULL_TREE
254 #define const_string_type_node NULL_TREE
255 #define va_list_ref_type_node NULL_TREE
256 #define va_list_arg_type_node NULL_TREE
257 #define flag_isoc99 0
259 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
260 builtin_types[(int) ENUM] = VALUE;
261 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
262 builtin_types[(int) ENUM] \
263 = define_builtin_type (RETURN, -1, -1, -1, -1);
264 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
265 builtin_types[(int) ENUM] \
266 = define_builtin_type (RETURN, ARG1, -1, -1, -1);
267 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
268 builtin_types[(int) ENUM] \
269 = define_builtin_type (RETURN, ARG1, ARG2, -1, -1);
270 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
271 builtin_types[(int) ENUM] \
272 = define_builtin_type (RETURN, ARG1, ARG2, ARG3, -1);
273 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
274 builtin_types[(int) ENUM] \
275 = define_builtin_type (RETURN, ARG1, ARG2, ARG3, ARG4);
276 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
277 builtin_types[(int) ENUM] = NULL_TREE;
278 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
279 builtin_types[(int) ENUM] = NULL_TREE;
280 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
281 builtin_types[(int) ENUM] = NULL_TREE;
282 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
283 builtin_types[(int) ENUM] = NULL_TREE;
284 #define DEF_POINTER_TYPE(ENUM, TYPE) \
285 builtin_types[(int) ENUM] = NULL_TREE;
287 #include "builtin-types.def"
289 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, \
290 FALLBACK_P, NONANSI_P, ATTRS, IMPLICIT) \
291 define_builtin (ENUM, NAME, CLASS, builtin_types[TYPE], FALLBACK_P, IMPLICIT);
292 #include "builtins.def"
295 /* If the call matches a builtin, return the
296 appropriate builtin expression instead. */
298 check_for_builtin (tree method, tree call)
300 if (! flag_emit_class_files && optimize && TREE_CODE (call) == CALL_EXPR)
303 tree method_arguments = TREE_OPERAND (call, 1);
304 tree method_class = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method)));
305 tree method_name = DECL_NAME (method);
306 tree method_return_type = TREE_TYPE (TREE_TYPE (method));
308 for (i = 0; java_builtins[i].builtin_code != END_BUILTINS; ++i)
310 if (method_class == java_builtins[i].class_name.t
311 && method_name == java_builtins[i].method_name.t)
315 if (java_builtins[i].creator != NULL)
316 return (*java_builtins[i].creator) (method_return_type,
318 fn = built_in_decls[java_builtins[i].builtin_code];
321 return java_build_function_call_expr (fn, method_arguments);
328 #include "gt-java-builtins.h"