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 *,
76 /* Functions of this type are used to inline a given call. Such a
77 function should either return an expression, if the call is to be
78 inlined, or NULL_TREE if a real call should be emitted. Arguments
79 are method return type and arguments to call. */
80 typedef tree builtin_creator_function PARAMS ((tree, tree));
82 /* Hold a char*, before initialization, or a tree, after
84 union string_or_tree GTY(())
86 const char * GTY ((tag ("0"))) s;
87 tree GTY ((tag ("1"))) t;
90 /* Used to hold a single builtin record. */
91 struct builtin_record GTY(())
93 union string_or_tree GTY ((desc ("1"))) class_name;
94 union string_or_tree GTY ((desc ("1"))) method_name;
95 builtin_creator_function * GTY((skip (""))) creator;
96 enum built_in_function builtin_code;
99 static GTY(()) struct builtin_record java_builtins[] =
101 { { "java.lang.Math" }, { "min" }, min_builtin, 0 },
102 { { "java.lang.Math" }, { "max" }, max_builtin, 0 },
103 { { "java.lang.Math" }, { "abs" }, abs_builtin, 0 },
104 { { "java.lang.Math" }, { "atan" }, NULL, BUILT_IN_ATAN },
105 { { "java.lang.Math" }, { "atan2" }, NULL, BUILT_IN_ATAN2 },
106 { { "java.lang.Math" }, { "cos" }, NULL, BUILT_IN_COS },
107 { { "java.lang.Math" }, { "exp" }, NULL, BUILT_IN_EXP },
108 { { "java.lang.Math" }, { "log" }, NULL, BUILT_IN_LOG },
109 { { "java.lang.Math" }, { "pow" }, NULL, BUILT_IN_POW },
110 { { "java.lang.Math" }, { "sin" }, NULL, BUILT_IN_SIN },
111 { { "java.lang.Math" }, { "sqrt" }, NULL, BUILT_IN_SQRT },
112 { { "java.lang.Math" }, { "tan" }, NULL, BUILT_IN_TAN },
113 { { NULL }, { NULL }, NULL, END_BUILTINS }
117 /* Internal functions which implement various builtin conversions. */
120 max_builtin (tree method_return_type, tree method_arguments)
122 return fold (build (MAX_EXPR, method_return_type,
123 TREE_VALUE (method_arguments),
124 TREE_VALUE (TREE_CHAIN (method_arguments))));
128 min_builtin (tree method_return_type, tree method_arguments)
130 return fold (build (MIN_EXPR, method_return_type,
131 TREE_VALUE (method_arguments),
132 TREE_VALUE (TREE_CHAIN (method_arguments))));
136 abs_builtin (tree method_return_type, tree method_arguments)
138 return fold (build1 (ABS_EXPR, method_return_type,
139 TREE_VALUE (method_arguments)));
142 /* Mostly copied from ../builtins.c. */
144 java_build_function_call_expr (tree fn, tree arglist)
148 call_expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
149 call_expr = build (CALL_EXPR, TREE_TYPE (TREE_TYPE (fn)),
151 TREE_SIDE_EFFECTS (call_expr) = 1;
152 return fold (call_expr);
157 /* Define a single builtin. */
159 define_builtin (enum built_in_function val,
166 decl = build_decl (FUNCTION_DECL, get_identifier (name), type);
167 DECL_EXTERNAL (decl) = 1;
168 TREE_PUBLIC (decl) = 1;
169 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (libname));
170 make_decl_rtl (decl, NULL);
172 DECL_BUILT_IN_CLASS (decl) = BUILT_IN_NORMAL;
173 DECL_FUNCTION_CODE (decl) = val;
175 implicit_built_in_decls[val] = decl;
176 built_in_decls[val] = decl;
181 /* Initialize the builtins. */
183 initialize_builtins (void)
185 tree double_ftype_double, double_ftype_double_double;
186 tree float_ftype_float, float_ftype_float_float;
190 for (i = 0; java_builtins[i].builtin_code != END_BUILTINS; ++i)
192 tree klass_id = get_identifier (java_builtins[i].class_name.s);
193 tree m = get_identifier (java_builtins[i].method_name.s);
195 java_builtins[i].class_name.t = klass_id;
196 java_builtins[i].method_name.t = m;
199 void_list_node = end_params_node;
201 t = tree_cons (NULL_TREE, float_type_node, end_params_node);
202 float_ftype_float = build_function_type (float_type_node, t);
203 t = tree_cons (NULL_TREE, float_type_node, t);
204 float_ftype_float_float = build_function_type (float_type_node, t);
206 t = tree_cons (NULL_TREE, double_type_node, end_params_node);
207 double_ftype_double = build_function_type (double_type_node, t);
208 t = tree_cons (NULL_TREE, double_type_node, t);
209 double_ftype_double_double = build_function_type (double_type_node, t);
211 define_builtin (BUILT_IN_FMOD, "__builtin_fmod",
212 double_ftype_double_double, "fmod");
213 define_builtin (BUILT_IN_FMODF, "__builtin_fmodf",
214 float_ftype_float_float, "fmodf");
216 define_builtin (BUILT_IN_ATAN, "__builtin_atan",
217 double_ftype_double, "_ZN4java4lang4Math4atanEd");
218 define_builtin (BUILT_IN_ATAN2, "__builtin_atan2",
219 double_ftype_double_double, "_ZN4java4lang4Math5atan2Edd");
220 define_builtin (BUILT_IN_COS, "__builtin_cos",
221 double_ftype_double, "_ZN4java4lang4Math3cosEd");
222 define_builtin (BUILT_IN_EXP, "__builtin_exp",
223 double_ftype_double, "_ZN4java4lang4Math3expEd");
224 define_builtin (BUILT_IN_LOG, "__builtin_log",
225 double_ftype_double, "_ZN4java4lang4Math3logEd");
226 define_builtin (BUILT_IN_POW, "__builtin_pow",
227 double_ftype_double_double, "_ZN4java4lang4Math3powEdd");
228 define_builtin (BUILT_IN_SIN, "__builtin_sin",
229 double_ftype_double, "_ZN4java4lang4Math3sinEd");
230 define_builtin (BUILT_IN_SQRT, "__builtin_sqrt",
231 double_ftype_double, "_ZN4java4lang4Math4sqrtEd");
232 define_builtin (BUILT_IN_TAN, "__builtin_tan",
233 double_ftype_double, "_ZN4java4lang4Math3tanEd");
236 /* If the call matches a builtin, return the
237 appropriate builtin expression instead. */
239 check_for_builtin (tree method, tree call)
241 if (! flag_emit_class_files && optimize && TREE_CODE (call) == CALL_EXPR)
244 tree method_arguments = TREE_OPERAND (call, 1);
245 tree method_class = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method)));
246 tree method_name = DECL_NAME (method);
247 tree method_return_type = TREE_TYPE (TREE_TYPE (method));
249 for (i = 0; java_builtins[i].builtin_code != END_BUILTINS; ++i)
251 if (method_class == java_builtins[i].class_name.t
252 && method_name == java_builtins[i].method_name.t)
256 if (java_builtins[i].creator != NULL)
257 return (*java_builtins[i].creator) (method_return_type,
259 fn = built_in_decls[java_builtins[i].builtin_code];
262 return java_build_function_call_expr (fn, method_arguments);
269 #include "gt-java-builtins.h"