OSDN Git Service

* builtins.c (java_build_function_call_expr): Renamed from
[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_POINTER_TYPE(NAME, TYPE) NAME,
50 #include "builtin-types.def"
51 #undef DEF_PRIMITIVE_TYPE
52 #undef DEF_FUNCTION_TYPE_0
53 #undef DEF_FUNCTION_TYPE_1
54 #undef DEF_FUNCTION_TYPE_2
55 #undef DEF_FUNCTION_TYPE_3
56 #undef DEF_FUNCTION_TYPE_4
57 #undef DEF_FUNCTION_TYPE_VAR_0
58 #undef DEF_FUNCTION_TYPE_VAR_1
59 #undef DEF_FUNCTION_TYPE_VAR_2
60 #undef DEF_POINTER_TYPE
61   BT_LAST
62 };
63
64 static tree max_builtin (tree, tree);
65 static tree min_builtin (tree, tree);
66 static tree abs_builtin (tree, tree);
67 static tree cos_builtin (tree, tree);
68 static tree sin_builtin (tree, tree);
69 static tree sqrt_builtin (tree, tree);
70
71 static tree java_build_function_call_expr (tree, tree);
72 static void define_builtin (enum built_in_function, const char *,
73                             enum built_in_class, tree, int);
74 static tree define_builtin_type (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 GTY(())
87 {
88   const char * GTY ((tag ("0"))) s;
89   tree GTY ((tag ("1"))) t;
90 };
91
92 /* Used to hold a single builtin record.  */
93 struct builtin_record GTY(())
94 {
95   union string_or_tree GTY ((desc ("1"))) class_name;
96   union string_or_tree GTY ((desc ("1"))) method_name;
97   builtin_creator_function * GTY((skip (""))) creator;
98 };
99
100 static GTY(()) 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 (tree method_return_type, tree method_arguments)
120 {
121   return build (MAX_EXPR, method_return_type,
122                 TREE_VALUE (method_arguments),
123                 TREE_VALUE (TREE_CHAIN (method_arguments)));
124 }
125
126 static tree
127 min_builtin (tree method_return_type, tree method_arguments)
128 {
129   return build (MIN_EXPR, method_return_type,
130                 TREE_VALUE (method_arguments),
131                 TREE_VALUE (TREE_CHAIN (method_arguments)));
132 }
133
134 static tree
135 abs_builtin (tree method_return_type, tree method_arguments)
136 {
137   return build1 (ABS_EXPR, method_return_type,
138                  TREE_VALUE (method_arguments));
139 }
140
141 /* Mostly copied from ../builtins.c.  */
142 static tree
143 java_build_function_call_expr (tree fn, tree arglist)
144 {
145   tree call_expr;
146
147   call_expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
148   call_expr = build (CALL_EXPR, TREE_TYPE (TREE_TYPE (fn)),
149                      call_expr, arglist);
150   TREE_SIDE_EFFECTS (call_expr) = 1;
151   return call_expr;
152 }
153
154 static tree
155 cos_builtin (tree method_return_type ATTRIBUTE_UNUSED, tree method_arguments)
156 {
157   /* FIXME: this assumes that jdouble and double are the same.  */
158   tree fn = built_in_decls[BUILT_IN_COS];
159   if (fn == NULL_TREE)
160     return NULL_TREE;
161   return java_build_function_call_expr (fn, method_arguments);
162 }
163
164 static tree
165 sin_builtin (tree method_return_type ATTRIBUTE_UNUSED, tree method_arguments)
166 {
167   /* FIXME: this assumes that jdouble and double are the same.  */
168   tree fn = built_in_decls[BUILT_IN_SIN];
169   if (fn == NULL_TREE)
170     return NULL_TREE;
171   return java_build_function_call_expr (fn, method_arguments);
172 }
173
174 static tree
175 sqrt_builtin (tree method_return_type ATTRIBUTE_UNUSED, tree method_arguments)
176 {
177   /* FIXME: this assumes that jdouble and double are the same.  */
178   tree fn = built_in_decls[BUILT_IN_SQRT];
179   if (fn == NULL_TREE)
180     return NULL_TREE;
181   return java_build_function_call_expr (fn, method_arguments);
182 }
183
184 \f
185
186 /* Define a single builtin.  */
187 static void
188 define_builtin (enum built_in_function val,
189                 const char *name,
190                 enum built_in_class class,
191                 tree type,
192                 int fallback_p)
193 {
194   tree decl;
195
196   if (! name || ! type)
197     return;
198
199   if (strncmp (name, "__builtin_", strlen ("__builtin_")) != 0)
200     abort ();
201   decl = build_decl (FUNCTION_DECL, get_identifier (name), type);
202   DECL_EXTERNAL (decl) = 1;
203   TREE_PUBLIC (decl) = 1;
204   if (fallback_p)
205     SET_DECL_ASSEMBLER_NAME (decl,
206                              get_identifier (name + strlen ("__builtin_")));
207   make_decl_rtl (decl, NULL);
208   pushdecl (decl);
209   DECL_BUILT_IN_CLASS (decl) = class;
210   DECL_FUNCTION_CODE (decl) = val;
211   built_in_decls[val] = decl;
212 }
213
214 /* Compute the type for a builtin.  */
215 static tree
216 define_builtin_type (int ret, int arg1, int arg2, int arg3, int arg4)
217 {
218   tree args;
219
220   if (builtin_types[ret] == NULL_TREE)
221     return NULL_TREE;
222
223   args = void_list_node;
224
225   if (arg4 != -1)
226     {
227       if (builtin_types[arg4] == NULL_TREE)
228         return NULL_TREE;
229       args = tree_cons (NULL_TREE, builtin_types[arg4], args);
230     }
231   if (arg3 != -1)
232     {
233       if (builtin_types[arg3] == NULL_TREE)
234         return NULL_TREE;
235       args = tree_cons (NULL_TREE, builtin_types[arg3], args);
236     }
237   if (arg2 != -1)
238     {
239       if (builtin_types[arg2] == NULL_TREE)
240         return NULL_TREE;
241       args = tree_cons (NULL_TREE, builtin_types[arg2], args);
242     }
243   if (arg1 != -1)
244     {
245       if (builtin_types[arg1] == NULL_TREE)
246         return NULL_TREE;
247       args = tree_cons (NULL_TREE, builtin_types[arg1], args);
248     }
249   
250   return build_function_type (builtin_types[ret], args);
251 }
252
253 \f
254
255 /* Initialize the builtins.  */
256 void
257 initialize_builtins (void)
258 {
259   int i;
260
261   for (i = 0; java_builtins[i].creator != NULL; ++i)
262     {
263       tree klass_id = get_identifier (java_builtins[i].class_name.s);
264       tree m = get_identifier (java_builtins[i].method_name.s);
265
266       java_builtins[i].class_name.t = klass_id;
267       java_builtins[i].method_name.t = m;
268     }
269
270   void_list_node = end_params_node;
271
272   /* Work around C-specific junk in builtin-types.def.  */
273 #define intmax_type_node NULL_TREE
274 #define c_size_type_node NULL_TREE
275 #define const_string_type_node NULL_TREE
276 #define va_list_ref_type_node NULL_TREE
277 #define va_list_arg_type_node NULL_TREE
278 #define flag_isoc99 0
279
280 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE)                                       \
281   builtin_types[(int) ENUM] = VALUE;
282 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN)               \
283   builtin_types[(int) ENUM]                             \
284     = define_builtin_type (RETURN, -1, -1, -1, -1);
285 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1)                         \
286   builtin_types[(int) ENUM]                                             \
287     = define_builtin_type (RETURN, ARG1, -1, -1, -1);
288 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2)   \
289   builtin_types[(int) ENUM]                             \
290     = define_builtin_type (RETURN, ARG1, ARG2, -1, -1);
291 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3)              \
292   builtin_types[(int) ENUM]                                              \
293     = define_builtin_type (RETURN, ARG1, ARG2, ARG3, -1);
294 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4)       \
295   builtin_types[(int) ENUM]                                             \
296     = define_builtin_type (RETURN, ARG1, ARG2, ARG3, ARG4);
297 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN)                                 \
298   builtin_types[(int) ENUM] = NULL_TREE;
299 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1)                      \
300    builtin_types[(int) ENUM] = NULL_TREE;
301 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2)       \
302    builtin_types[(int) ENUM] = NULL_TREE;
303 #define DEF_POINTER_TYPE(ENUM, TYPE)                    \
304   builtin_types[(int) ENUM] = NULL_TREE;
305
306 #include "builtin-types.def"
307
308 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, \
309                     FALLBACK_P, NONANSI_P, ATTRS) \
310   define_builtin (ENUM, NAME, CLASS, builtin_types[TYPE], FALLBACK_P);
311 #include "builtins.def"
312 }
313
314 /* If the call matches a builtin, return the
315    appropriate builtin expression instead.  */
316 tree
317 check_for_builtin (tree method, tree call)
318 {
319   if (! flag_emit_class_files && optimize && TREE_CODE (call) == CALL_EXPR)
320     {
321       int i;
322       tree method_arguments = TREE_OPERAND (call, 1);
323       tree method_class = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method)));
324       tree method_name = DECL_NAME (method);
325       tree method_return_type = TREE_TYPE (TREE_TYPE (method));
326
327       for (i = 0; java_builtins[i].creator != NULL; ++i)
328         {
329           if (method_class == java_builtins[i].class_name.t
330               && method_name == java_builtins[i].method_name.t)
331             {
332               return (*java_builtins[i].creator) (method_return_type,
333                                                   method_arguments);
334             }
335         }
336     }
337   return call;
338 }
339
340 #include "gt-java-builtins.h"