OSDN Git Service

PR c++/9367
[pf3gnuchains/gcc-fork.git] / gcc / java / builtins.c
1 /* Built-in and inline functions for gcj
2    Copyright (C) 2001, 2003
3    Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
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)
10 any later version.
11
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.
16
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.  
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 "coretypes.h"
31 #include "tm.h"
32 #include "tree.h"
33 #include "ggc.h"
34 #include "flags.h"
35 #include "langhooks.h"
36 #include "java-tree.h"
37
38 enum builtin_type 
39 {
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
63   BT_LAST
64 };
65
66 static tree max_builtin (tree, tree);
67 static tree min_builtin (tree, tree);
68 static tree abs_builtin (tree, tree);
69 static tree cos_builtin (tree, tree);
70 static tree sin_builtin (tree, tree);
71 static tree sqrt_builtin (tree, tree);
72
73 static tree java_build_function_call_expr (tree, tree);
74 static void define_builtin (enum built_in_function, const char *,
75                             enum built_in_class, tree, int, int);
76 static tree define_builtin_type (int, int, int, int, int);
77
78 \f
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 \f
118 /* Internal functions which implement various builtin conversions.  */
119
120 static tree
121 max_builtin (tree method_return_type, tree method_arguments)
122 {
123   return build (MAX_EXPR, method_return_type,
124                 TREE_VALUE (method_arguments),
125                 TREE_VALUE (TREE_CHAIN (method_arguments)));
126 }
127
128 static tree
129 min_builtin (tree method_return_type, tree 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 (tree method_return_type, tree method_arguments)
138 {
139   return build1 (ABS_EXPR, method_return_type,
140                  TREE_VALUE (method_arguments));
141 }
142
143 /* Mostly copied from ../builtins.c.  */
144 static tree
145 java_build_function_call_expr (tree fn, tree arglist)
146 {
147   tree call_expr;
148
149   call_expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
150   call_expr = build (CALL_EXPR, TREE_TYPE (TREE_TYPE (fn)),
151                      call_expr, arglist);
152   TREE_SIDE_EFFECTS (call_expr) = 1;
153   return call_expr;
154 }
155
156 static tree
157 cos_builtin (tree method_return_type ATTRIBUTE_UNUSED, tree method_arguments)
158 {
159   /* FIXME: this assumes that jdouble and double are the same.  */
160   tree fn = built_in_decls[BUILT_IN_COS];
161   if (fn == NULL_TREE)
162     return NULL_TREE;
163   return java_build_function_call_expr (fn, method_arguments);
164 }
165
166 static tree
167 sin_builtin (tree method_return_type ATTRIBUTE_UNUSED, tree method_arguments)
168 {
169   /* FIXME: this assumes that jdouble and double are the same.  */
170   tree fn = built_in_decls[BUILT_IN_SIN];
171   if (fn == NULL_TREE)
172     return NULL_TREE;
173   return java_build_function_call_expr (fn, method_arguments);
174 }
175
176 static tree
177 sqrt_builtin (tree method_return_type ATTRIBUTE_UNUSED, tree method_arguments)
178 {
179   /* FIXME: this assumes that jdouble and double are the same.  */
180   tree fn = built_in_decls[BUILT_IN_SQRT];
181   if (fn == NULL_TREE)
182     return NULL_TREE;
183   return java_build_function_call_expr (fn, method_arguments);
184 }
185
186 \f
187
188 /* Define a single builtin.  */
189 static void
190 define_builtin (enum built_in_function val,
191                 const char *name,
192                 enum built_in_class class,
193                 tree type,
194                 int fallback_p,
195                 int implicit)
196 {
197   tree decl;
198
199   if (! name || ! type)
200     return;
201
202   if (strncmp (name, "__builtin_", strlen ("__builtin_")) != 0)
203     abort ();
204   decl = build_decl (FUNCTION_DECL, get_identifier (name), type);
205   DECL_EXTERNAL (decl) = 1;
206   TREE_PUBLIC (decl) = 1;
207   if (fallback_p)
208     SET_DECL_ASSEMBLER_NAME (decl,
209                              get_identifier (name + strlen ("__builtin_")));
210   make_decl_rtl (decl, NULL);
211   pushdecl (decl);
212   DECL_BUILT_IN_CLASS (decl) = class;
213   DECL_FUNCTION_CODE (decl) = val;
214   built_in_decls[val] = decl;
215   if (implicit)
216     implicit_built_in_decls[val] = decl;
217 }
218
219 /* Compute the type for a builtin.  */
220 static tree
221 define_builtin_type (int ret, int arg1, int arg2, int arg3, int arg4)
222 {
223   tree args;
224
225   if (builtin_types[ret] == NULL_TREE)
226     return NULL_TREE;
227
228   args = void_list_node;
229
230   if (arg4 != -1)
231     {
232       if (builtin_types[arg4] == NULL_TREE)
233         return NULL_TREE;
234       args = tree_cons (NULL_TREE, builtin_types[arg4], args);
235     }
236   if (arg3 != -1)
237     {
238       if (builtin_types[arg3] == NULL_TREE)
239         return NULL_TREE;
240       args = tree_cons (NULL_TREE, builtin_types[arg3], args);
241     }
242   if (arg2 != -1)
243     {
244       if (builtin_types[arg2] == NULL_TREE)
245         return NULL_TREE;
246       args = tree_cons (NULL_TREE, builtin_types[arg2], args);
247     }
248   if (arg1 != -1)
249     {
250       if (builtin_types[arg1] == NULL_TREE)
251         return NULL_TREE;
252       args = tree_cons (NULL_TREE, builtin_types[arg1], args);
253     }
254   
255   return build_function_type (builtin_types[ret], args);
256 }
257
258 \f
259
260 /* Initialize the builtins.  */
261 void
262 initialize_builtins (void)
263 {
264   int i;
265
266   for (i = 0; java_builtins[i].creator != NULL; ++i)
267     {
268       tree klass_id = get_identifier (java_builtins[i].class_name.s);
269       tree m = get_identifier (java_builtins[i].method_name.s);
270
271       java_builtins[i].class_name.t = klass_id;
272       java_builtins[i].method_name.t = m;
273     }
274
275   void_list_node = end_params_node;
276
277   /* Work around C-specific junk in builtin-types.def.  */
278 #define intmax_type_node NULL_TREE
279 #define c_size_type_node NULL_TREE
280 #define const_string_type_node NULL_TREE
281 #define va_list_ref_type_node NULL_TREE
282 #define va_list_arg_type_node NULL_TREE
283 #define flag_isoc99 0
284
285 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE)                                       \
286   builtin_types[(int) ENUM] = VALUE;
287 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN)               \
288   builtin_types[(int) ENUM]                             \
289     = define_builtin_type (RETURN, -1, -1, -1, -1);
290 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1)                         \
291   builtin_types[(int) ENUM]                                             \
292     = define_builtin_type (RETURN, ARG1, -1, -1, -1);
293 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2)   \
294   builtin_types[(int) ENUM]                             \
295     = define_builtin_type (RETURN, ARG1, ARG2, -1, -1);
296 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3)              \
297   builtin_types[(int) ENUM]                                              \
298     = define_builtin_type (RETURN, ARG1, ARG2, ARG3, -1);
299 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4)       \
300   builtin_types[(int) ENUM]                                             \
301     = define_builtin_type (RETURN, ARG1, ARG2, ARG3, ARG4);
302 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN)                   \
303   builtin_types[(int) ENUM] = NULL_TREE;
304 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1)             \
305    builtin_types[(int) ENUM] = NULL_TREE;
306 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2)       \
307    builtin_types[(int) ENUM] = NULL_TREE;
308 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
309    builtin_types[(int) ENUM] = NULL_TREE;
310 #define DEF_POINTER_TYPE(ENUM, TYPE)                    \
311   builtin_types[(int) ENUM] = NULL_TREE;
312
313 #include "builtin-types.def"
314
315 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, \
316                     FALLBACK_P, NONANSI_P, ATTRS, IMPLICIT) \
317   define_builtin (ENUM, NAME, CLASS, builtin_types[TYPE], FALLBACK_P, IMPLICIT);
318 #include "builtins.def"
319 }
320
321 /* If the call matches a builtin, return the
322    appropriate builtin expression instead.  */
323 tree
324 check_for_builtin (tree method, tree call)
325 {
326   if (! flag_emit_class_files && optimize && TREE_CODE (call) == CALL_EXPR)
327     {
328       int i;
329       tree method_arguments = TREE_OPERAND (call, 1);
330       tree method_class = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method)));
331       tree method_name = DECL_NAME (method);
332       tree method_return_type = TREE_TYPE (TREE_TYPE (method));
333
334       for (i = 0; java_builtins[i].creator != NULL; ++i)
335         {
336           if (method_class == java_builtins[i].class_name.t
337               && method_name == java_builtins[i].method_name.t)
338             {
339               return (*java_builtins[i].creator) (method_return_type,
340                                                   method_arguments);
341             }
342         }
343     }
344   return call;
345 }
346
347 #include "gt-java-builtins.h"