OSDN Git Service

* builtins.c (check_for_builtin): New function.
[pf3gnuchains/gcc-fork.git] / gcc / java / builtins.c
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
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_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
60 };
61
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));
68
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));
75
76 \f
77
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));
83
84 /* Hold a char*, before initialization, or a tree, after
85    initialization.  */
86 union string_or_tree
87 {
88   const char *s;
89   tree t;
90 };
91
92 /* Used to hold a single builtin record.  */
93 struct builtin_record
94 {
95   union string_or_tree class_name;
96   union string_or_tree method_name;
97   builtin_creator_function *creator;
98 };
99
100 static struct builtin_record java_builtins[] =
101 {
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 }
109 };
110
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];
114
115 \f
116 /* Internal functions which implement various builtin conversions.  */
117
118 static tree
119 max_builtin (method_return_type, method_arguments)
120      tree method_return_type, method_arguments;
121 {
122   return build (MAX_EXPR, method_return_type,
123                 TREE_VALUE (method_arguments),
124                 TREE_VALUE (TREE_CHAIN (method_arguments)));
125 }
126
127 static tree
128 min_builtin (method_return_type, method_arguments)
129      tree method_return_type, method_arguments;
130 {
131   return build (MIN_EXPR, method_return_type,
132                 TREE_VALUE (method_arguments),
133                 TREE_VALUE (TREE_CHAIN (method_arguments)));
134 }
135
136 static tree
137 abs_builtin (method_return_type, method_arguments)
138      tree method_return_type, method_arguments;
139 {
140   return build1 (ABS_EXPR, method_return_type,
141                  TREE_VALUE (method_arguments));
142 }
143
144 /* Mostly copied from ../builtins.c.  */
145 static tree
146 build_function_call_expr (tree fn, tree arglist)
147 {
148   tree call_expr;
149
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;
155 }
156
157 static tree
158 cos_builtin (method_return_type, method_arguments)
159      tree method_return_type, method_arguments;
160 {
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);
166 }
167
168 static tree
169 sin_builtin (method_return_type, method_arguments)
170      tree method_return_type, method_arguments;
171 {
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);
177 }
178
179 static tree
180 sqrt_builtin (method_return_type, method_arguments)
181      tree method_return_type, method_arguments;
182 {
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);
188 }
189
190 \f
191
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;
200 {
201   tree decl;
202
203   if (! name)
204     return;
205
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;
219 }
220
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;
225 {
226   tree args;
227
228   if (builtin_types[ret] == NULL_TREE)
229     return NULL_TREE;
230
231   args = void_list_node;
232
233   if (arg4 != -1)
234     {
235       if (builtin_types[arg4] == NULL_TREE)
236         return NULL_TREE;
237       args = tree_cons (NULL_TREE, builtin_types[arg4], args);
238     }
239   if (arg3 != -1)
240     {
241       if (builtin_types[arg3] == NULL_TREE)
242         return NULL_TREE;
243       args = tree_cons (NULL_TREE, builtin_types[arg3], args);
244     }
245   if (arg2 != -1)
246     {
247       if (builtin_types[arg2] == NULL_TREE)
248         return NULL_TREE;
249       args = tree_cons (NULL_TREE, builtin_types[arg2], args);
250     }
251   if (arg1 != -1)
252     {
253       if (builtin_types[arg1] == NULL_TREE)
254         return NULL_TREE;
255       args = tree_cons (NULL_TREE, builtin_types[arg1], args);
256     }
257   
258   return build_function_type (builtin_types[ret], args);
259 }
260
261 \f
262
263 /* Initialize the builtins.  */
264 void
265 initialize_builtins ()
266 {
267   int i;
268
269   for (i = 0; java_builtins[i].creator != NULL; ++i)
270     {
271       tree klass_id = get_identifier (java_builtins[i].class_name.s);
272       tree m = get_identifier (java_builtins[i].method_name.s);
273
274       java_builtins[i].class_name.t = klass_id;
275       java_builtins[i].method_name.t = m;
276       ggc_add_tree_root (&java_builtins[i].class_name.t, 1);
277       ggc_add_tree_root (&java_builtins[i].method_name.t, 1);
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 traditional_ptr_type_node NULL_TREE
285 #define traditional_cptr_type_node NULL_TREE
286 #define c_size_type_node NULL_TREE
287 #define const_string_type_node NULL_TREE
288 #define traditional_len_type_node NULL_TREE
289 #define va_list_ref_type_node NULL_TREE
290 #define va_list_arg_type_node NULL_TREE
291 #define flag_isoc99 0
292
293 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE)                                       \
294   builtin_types[(int) ENUM] = VALUE;
295 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN)               \
296   builtin_types[(int) ENUM]                             \
297     = define_builtin_type (RETURN, -1, -1, -1, -1);
298 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1)                         \
299   builtin_types[(int) ENUM]                                             \
300     = define_builtin_type (RETURN, ARG1, -1, -1, -1);
301 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2)   \
302   builtin_types[(int) ENUM]                             \
303     = define_builtin_type (RETURN, ARG1, ARG2, -1, -1);
304 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3)              \
305   builtin_types[(int) ENUM]                                              \
306     = define_builtin_type (RETURN, ARG1, ARG2, ARG3, -1);
307 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4)       \
308   builtin_types[(int) ENUM]                                             \
309     = define_builtin_type (RETURN, ARG1, ARG2, ARG3, ARG4);
310 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN)                                 \
311   builtin_types[(int) ENUM] = NULL_TREE;
312 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1)                      \
313    builtin_types[(int) ENUM] = NULL_TREE;
314 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2)       \
315    builtin_types[(int) ENUM] = NULL_TREE;
316 #define DEF_POINTER_TYPE(ENUM, TYPE)                    \
317   builtin_types[(int) ENUM] = NULL_TREE;
318
319 #include "builtin-types.def"
320
321 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, \
322                     FALLBACK_P, NONANSI_P) \
323   define_builtin (ENUM, NAME, CLASS, builtin_types[TYPE], FALLBACK_P);
324 #include "builtins.def"
325 }
326
327 /* If the call matches a builtin, return the
328    appropriate builtin expression instead.  */
329 tree
330 check_for_builtin (method, call)
331      tree method;
332      tree call;
333 {
334   if (! flag_emit_class_files && optimize && TREE_CODE (call) == CALL_EXPR)
335     {
336       int i;
337       tree method_arguments = TREE_OPERAND (call, 1);
338       tree method_class = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method)));
339       tree method_name = DECL_NAME (method);
340       tree method_return_type = TREE_TYPE (TREE_TYPE (method));
341
342       for (i = 0; java_builtins[i].creator != NULL; ++i)
343         {
344           if (method_class == java_builtins[i].class_name.t
345               && method_name == java_builtins[i].method_name.t)
346             {
347               return (*java_builtins[i].creator) (method_return_type,
348                                                   method_arguments);
349             }
350         }
351     }
352   return call;
353 }