OSDN Git Service

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