1 /* Built-in and inline functions for gcj
2 Copyright (C) 2001
3 Free Software Foundation, Inc.
4
5 This file is part of GNU CC.
6
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.
11
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.
16
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.
21
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. */
25
26 /* Written by Tom Tromey <tromey@redhat.com>. */
27
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"
35
36 enum builtin_type
37 {
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_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
48 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
49 #include "builtin-types.def"
50 #undef DEF_PRIMITIVE_TYPE
51 #undef DEF_FUNCTION_TYPE_0
52 #undef DEF_FUNCTION_TYPE_1
53 #undef DEF_FUNCTION_TYPE_2
54 #undef DEF_FUNCTION_TYPE_3
55 #undef DEF_FUNCTION_TYPE_4
56 #undef DEF_FUNCTION_TYPE_VAR_0
57 #undef DEF_FUNCTION_TYPE_VAR_1
58 #undef DEF_FUNCTION_TYPE_VAR_2
59 #undef DEF_FUNCTION_TYPE_VAR_3
60 #undef DEF_POINTER_TYPE
61 BT_LAST
62 };
63
64 static tree max_builtin PARAMS ((tree, tree));
65 static tree min_builtin PARAMS ((tree, tree));
66 static tree abs_builtin PARAMS ((tree, tree));
67 static tree cos_builtin PARAMS ((tree, tree));
68 static tree sin_builtin PARAMS ((tree, tree));
69 static tree sqrt_builtin PARAMS ((tree, tree));
70
71 static tree build_function_call_expr PARAMS ((tree, tree));
72 static void define_builtin PARAMS ((enum built_in_function,
73 const char *,
74 enum built_in_class,
75 tree, int));
76 static tree define_builtin_type PARAMS ((int, int, int, int, int));
77
78
79
80 /* Functions of this type are used to inline a given call. Such a
81 function should either return an expression, if the call is to be
82 inlined, or NULL_TREE if a real call should be emitted. Arguments
83 are method return type and arguments to call. */
84 typedef tree builtin_creator_function PARAMS ((tree, tree));
85
86 /* Hold a char*, before initialization, or a tree, after
87 initialization. */
88 union string_or_tree GTY(())
89 {
90 const char * GTY ((tag ("0"))) s;
91 tree GTY ((tag ("1"))) t;
92 };
93
94 /* Used to hold a single builtin record. */
95 struct builtin_record GTY(())
96 {
97 union string_or_tree GTY ((desc ("1"))) class_name;
98 union string_or_tree GTY ((desc ("1"))) method_name;
99 builtin_creator_function * GTY((skip (""))) creator;
100 };
101
102 static GTY(()) struct builtin_record java_builtins[] =
103 {
104 { { "java.lang.Math" }, { "min" }, min_builtin },
105 { { "java.lang.Math" }, { "max" }, max_builtin },
106 { { "java.lang.Math" }, { "abs" }, abs_builtin },
107 { { "java.lang.Math" }, { "cos" }, cos_builtin },
108 { { "java.lang.Math" }, { "sin" }, sin_builtin },
109 { { "java.lang.Math" }, { "sqrt" }, sqrt_builtin },
110 { { NULL }, { NULL }, NULL }
111 };
112
113 /* This is only used transiently, so we don't mark it as roots for the
114 GC. */
115 static tree builtin_types[(int) BT_LAST];
116
117
118 /* Internal functions which implement various builtin conversions. */
119
120 static tree
max_builtin(method_return_type,method_arguments)121 max_builtin (method_return_type, method_arguments)
122 tree method_return_type, method_arguments;
123 {
124 return build (MAX_EXPR, method_return_type,
125 TREE_VALUE (method_arguments),
126 TREE_VALUE (TREE_CHAIN (method_arguments)));
127 }
128
129 static tree
min_builtin(method_return_type,method_arguments)130 min_builtin (method_return_type, method_arguments)
131 tree method_return_type, method_arguments;
132 {
133 return build (MIN_EXPR, method_return_type,
134 TREE_VALUE (method_arguments),
135 TREE_VALUE (TREE_CHAIN (method_arguments)));
136 }
137
138 static tree
abs_builtin(method_return_type,method_arguments)139 abs_builtin (method_return_type, method_arguments)
140 tree method_return_type, method_arguments;
141 {
142 return build1 (ABS_EXPR, method_return_type,
143 TREE_VALUE (method_arguments));
144 }
145
146 /* Mostly copied from ../builtins.c. */
147 static tree
build_function_call_expr(tree fn,tree arglist)148 build_function_call_expr (tree fn, tree arglist)
149 {
150 tree call_expr;
151
152 call_expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
153 call_expr = build (CALL_EXPR, TREE_TYPE (TREE_TYPE (fn)),
154 call_expr, arglist);
155 TREE_SIDE_EFFECTS (call_expr) = 1;
156 return call_expr;
157 }
158
159 static tree
cos_builtin(method_return_type,method_arguments)160 cos_builtin (method_return_type, method_arguments)
161 tree method_return_type ATTRIBUTE_UNUSED, method_arguments;
162 {
163 /* FIXME: this assumes that jdouble and double are the same. */
164 tree fn = built_in_decls[BUILT_IN_COS];
165 if (fn == NULL_TREE)
166 return NULL_TREE;
167 return build_function_call_expr (fn, method_arguments);
168 }
169
170 static tree
sin_builtin(method_return_type,method_arguments)171 sin_builtin (method_return_type, method_arguments)
172 tree method_return_type ATTRIBUTE_UNUSED, method_arguments;
173 {
174 /* FIXME: this assumes that jdouble and double are the same. */
175 tree fn = built_in_decls[BUILT_IN_SIN];
176 if (fn == NULL_TREE)
177 return NULL_TREE;
178 return build_function_call_expr (fn, method_arguments);
179 }
180
181 static tree
sqrt_builtin(method_return_type,method_arguments)182 sqrt_builtin (method_return_type, method_arguments)
183 tree method_return_type ATTRIBUTE_UNUSED, method_arguments;
184 {
185 /* FIXME: this assumes that jdouble and double are the same. */
186 tree fn = built_in_decls[BUILT_IN_SQRT];
187 if (fn == NULL_TREE)
188 return NULL_TREE;
189 return build_function_call_expr (fn, method_arguments);
190 }
191
192
193
194 /* Define a single builtin. */
195 static void
define_builtin(val,name,class,type,fallback_p)196 define_builtin (val, name, class, type, fallback_p)
197 enum built_in_function val;
198 const char *name;
199 enum built_in_class class;
200 tree type;
201 int fallback_p;
202 {
203 tree decl;
204
205 if (! name || ! type)
206 return;
207
208 if (strncmp (name, "__builtin_", strlen ("__builtin_")) != 0)
209 abort ();
210 decl = build_decl (FUNCTION_DECL, get_identifier (name), type);
211 DECL_EXTERNAL (decl) = 1;
212 TREE_PUBLIC (decl) = 1;
213 if (fallback_p)
214 SET_DECL_ASSEMBLER_NAME (decl,
215 get_identifier (name + strlen ("__builtin_")));
216 make_decl_rtl (decl, NULL);
217 pushdecl (decl);
218 DECL_BUILT_IN_CLASS (decl) = class;
219 DECL_FUNCTION_CODE (decl) = val;
220 built_in_decls[val] = decl;
221 }
222
223 /* Compute the type for a builtin. */
224 static tree
define_builtin_type(ret,arg1,arg2,arg3,arg4)225 define_builtin_type (ret, arg1, arg2, arg3, arg4)
226 int ret, arg1, arg2, arg3, arg4;
227 {
228 tree args;
229
230 if (builtin_types[ret] == NULL_TREE)
231 return NULL_TREE;
232
233 args = void_list_node;
234
235 if (arg4 != -1)
236 {
237 if (builtin_types[arg4] == NULL_TREE)
238 return NULL_TREE;
239 args = tree_cons (NULL_TREE, builtin_types[arg4], args);
240 }
241 if (arg3 != -1)
242 {
243 if (builtin_types[arg3] == NULL_TREE)
244 return NULL_TREE;
245 args = tree_cons (NULL_TREE, builtin_types[arg3], args);
246 }
247 if (arg2 != -1)
248 {
249 if (builtin_types[arg2] == NULL_TREE)
250 return NULL_TREE;
251 args = tree_cons (NULL_TREE, builtin_types[arg2], args);
252 }
253 if (arg1 != -1)
254 {
255 if (builtin_types[arg1] == NULL_TREE)
256 return NULL_TREE;
257 args = tree_cons (NULL_TREE, builtin_types[arg1], args);
258 }
259
260 return build_function_type (builtin_types[ret], args);
261 }
262
263
264
265 /* Initialize the builtins. */
266 void
initialize_builtins()267 initialize_builtins ()
268 {
269 int i;
270
271 for (i = 0; java_builtins[i].creator != NULL; ++i)
272 {
273 tree klass_id = get_identifier (java_builtins[i].class_name.s);
274 tree m = get_identifier (java_builtins[i].method_name.s);
275
276 java_builtins[i].class_name.t = klass_id;
277 java_builtins[i].method_name.t = m;
278 }
279
280 void_list_node = end_params_node;
281
282 /* Work around C-specific junk in builtin-types.def. */
283 #define intmax_type_node NULL_TREE
284 #define c_size_type_node NULL_TREE
285 #define const_string_type_node NULL_TREE
286 #define va_list_ref_type_node NULL_TREE
287 #define va_list_arg_type_node NULL_TREE
288 #define flag_isoc99 0
289
290 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
291 builtin_types[(int) ENUM] = VALUE;
292 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
293 builtin_types[(int) ENUM] \
294 = define_builtin_type (RETURN, -1, -1, -1, -1);
295 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
296 builtin_types[(int) ENUM] \
297 = define_builtin_type (RETURN, ARG1, -1, -1, -1);
298 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
299 builtin_types[(int) ENUM] \
300 = define_builtin_type (RETURN, ARG1, ARG2, -1, -1);
301 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
302 builtin_types[(int) ENUM] \
303 = define_builtin_type (RETURN, ARG1, ARG2, ARG3, -1);
304 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
305 builtin_types[(int) ENUM] \
306 = define_builtin_type (RETURN, ARG1, ARG2, ARG3, ARG4);
307 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
308 builtin_types[(int) ENUM] = NULL_TREE;
309 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
310 builtin_types[(int) ENUM] = NULL_TREE;
311 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
312 builtin_types[(int) ENUM] = NULL_TREE;
313 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
314 builtin_types[(int) ENUM] = NULL_TREE;
315 #define DEF_POINTER_TYPE(ENUM, TYPE) \
316 builtin_types[(int) ENUM] = NULL_TREE;
317
318 #include "builtin-types.def"
319
320 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, \
321 FALLBACK_P, NONANSI_P, ATTRS) \
322 define_builtin (ENUM, NAME, CLASS, builtin_types[TYPE], FALLBACK_P);
323 #include "builtins.def"
324 }
325
326 /* If the call matches a builtin, return the
327 appropriate builtin expression instead. */
328 tree
check_for_builtin(method,call)329 check_for_builtin (method, call)
330 tree method;
331 tree call;
332 {
333 if (! flag_emit_class_files && optimize && TREE_CODE (call) == CALL_EXPR)
334 {
335 int i;
336 tree method_arguments = TREE_OPERAND (call, 1);
337 tree method_class = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method)));
338 tree method_name = DECL_NAME (method);
339 tree method_return_type = TREE_TYPE (TREE_TYPE (method));
340
341 for (i = 0; java_builtins[i].creator != NULL; ++i)
342 {
343 if (method_class == java_builtins[i].class_name.t
344 && method_name == java_builtins[i].method_name.t)
345 {
346 return (*java_builtins[i].creator) (method_return_type,
347 method_arguments);
348 }
349 }
350 }
351 return call;
352 }
353
354 #include "gt-java-builtins.h"
355