OSDN Git Service

* target-def.h: Remove usage of OBJECT_FORMAT_ROSE.
[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                             tree, const char *);
73
74 \f
75
76 /* Functions of this type are used to inline a given call.  Such a
77    function should either return an expression, if the call is to be
78    inlined, or NULL_TREE if a real call should be emitted.  Arguments
79    are method return type and arguments to call.  */
80 typedef tree builtin_creator_function PARAMS ((tree, tree));
81
82 /* Hold a char*, before initialization, or a tree, after
83    initialization.  */
84 union string_or_tree GTY(())
85 {
86   const char * GTY ((tag ("0"))) s;
87   tree GTY ((tag ("1"))) t;
88 };
89
90 /* Used to hold a single builtin record.  */
91 struct builtin_record GTY(())
92 {
93   union string_or_tree GTY ((desc ("1"))) class_name;
94   union string_or_tree GTY ((desc ("1"))) method_name;
95   builtin_creator_function * GTY((skip (""))) creator;
96   enum built_in_function builtin_code;
97 };
98
99 static GTY(()) struct builtin_record java_builtins[] =
100 {
101   { { "java.lang.Math" }, { "min" }, min_builtin, 0 },
102   { { "java.lang.Math" }, { "max" }, max_builtin, 0 },
103   { { "java.lang.Math" }, { "abs" }, abs_builtin, 0 },
104   { { "java.lang.Math" }, { "atan" }, NULL, BUILT_IN_ATAN },
105   { { "java.lang.Math" }, { "atan2" }, NULL, BUILT_IN_ATAN2 },
106   { { "java.lang.Math" }, { "cos" }, NULL, BUILT_IN_COS },
107   { { "java.lang.Math" }, { "exp" }, NULL, BUILT_IN_EXP },
108   { { "java.lang.Math" }, { "log" }, NULL, BUILT_IN_LOG },
109   { { "java.lang.Math" }, { "pow" }, NULL, BUILT_IN_POW },
110   { { "java.lang.Math" }, { "sin" }, NULL, BUILT_IN_SIN },
111   { { "java.lang.Math" }, { "sqrt" }, NULL, BUILT_IN_SQRT },
112   { { "java.lang.Math" }, { "tan" }, NULL, BUILT_IN_TAN },
113   { { NULL }, { NULL }, NULL, END_BUILTINS }
114 };
115
116 \f
117 /* Internal functions which implement various builtin conversions.  */
118
119 static tree
120 max_builtin (tree method_return_type, tree method_arguments)
121 {
122   return fold (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 (tree method_return_type, tree method_arguments)
129 {
130   return fold (build (MIN_EXPR, method_return_type,
131                       TREE_VALUE (method_arguments),
132                       TREE_VALUE (TREE_CHAIN (method_arguments))));
133 }
134
135 static tree
136 abs_builtin (tree method_return_type, tree method_arguments)
137 {
138   return fold (build1 (ABS_EXPR, method_return_type,
139                        TREE_VALUE (method_arguments)));
140 }
141
142 /* Mostly copied from ../builtins.c.  */
143 static tree
144 java_build_function_call_expr (tree fn, tree arglist)
145 {
146   tree call_expr;
147
148   call_expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
149   call_expr = build (CALL_EXPR, TREE_TYPE (TREE_TYPE (fn)),
150                      call_expr, arglist);
151   TREE_SIDE_EFFECTS (call_expr) = 1;
152   return fold (call_expr);
153 }
154
155 \f
156
157 /* Define a single builtin.  */
158 static void
159 define_builtin (enum built_in_function val,
160                 const char *name,
161                 tree type,
162                 const char *libname)
163 {
164   tree decl;
165
166   decl = build_decl (FUNCTION_DECL, get_identifier (name), type);
167   DECL_EXTERNAL (decl) = 1;
168   TREE_PUBLIC (decl) = 1;
169   SET_DECL_ASSEMBLER_NAME (decl, get_identifier (libname));
170   make_decl_rtl (decl, NULL);
171   pushdecl (decl);
172   DECL_BUILT_IN_CLASS (decl) = BUILT_IN_NORMAL;
173   DECL_FUNCTION_CODE (decl) = val;
174
175   implicit_built_in_decls[val] = decl;
176   built_in_decls[val] = decl;
177 }
178
179 \f
180
181 /* Initialize the builtins.  */
182 void
183 initialize_builtins (void)
184 {
185   tree double_ftype_double, double_ftype_double_double;
186   tree float_ftype_float, float_ftype_float_float;
187   tree t;
188   int i;
189
190   for (i = 0; java_builtins[i].builtin_code != END_BUILTINS; ++i)
191     {
192       tree klass_id = get_identifier (java_builtins[i].class_name.s);
193       tree m = get_identifier (java_builtins[i].method_name.s);
194
195       java_builtins[i].class_name.t = klass_id;
196       java_builtins[i].method_name.t = m;
197     }
198
199   void_list_node = end_params_node;
200
201   t = tree_cons (NULL_TREE, float_type_node, end_params_node);
202   float_ftype_float = build_function_type (float_type_node, t);
203   t = tree_cons (NULL_TREE, float_type_node, t);
204   float_ftype_float_float = build_function_type (float_type_node, t);
205
206   t = tree_cons (NULL_TREE, double_type_node, end_params_node);
207   double_ftype_double = build_function_type (double_type_node, t);
208   t = tree_cons (NULL_TREE, double_type_node, t);
209   double_ftype_double_double = build_function_type (double_type_node, t);
210
211   define_builtin (BUILT_IN_FMOD, "__builtin_fmod",
212                   double_ftype_double_double, "fmod");
213   define_builtin (BUILT_IN_FMODF, "__builtin_fmodf",
214                   float_ftype_float_float, "fmodf");
215
216   define_builtin (BUILT_IN_ATAN, "__builtin_atan",
217                   double_ftype_double, "_ZN4java4lang4Math4atanEd");
218   define_builtin (BUILT_IN_ATAN2, "__builtin_atan2",
219                   double_ftype_double_double, "_ZN4java4lang4Math5atan2Edd");
220   define_builtin (BUILT_IN_COS, "__builtin_cos",
221                   double_ftype_double, "_ZN4java4lang4Math3cosEd");
222   define_builtin (BUILT_IN_EXP, "__builtin_exp",
223                   double_ftype_double, "_ZN4java4lang4Math3expEd");
224   define_builtin (BUILT_IN_LOG, "__builtin_log",
225                   double_ftype_double, "_ZN4java4lang4Math3logEd");
226   define_builtin (BUILT_IN_POW, "__builtin_pow",
227                   double_ftype_double_double, "_ZN4java4lang4Math3powEdd");
228   define_builtin (BUILT_IN_SIN, "__builtin_sin",
229                   double_ftype_double, "_ZN4java4lang4Math3sinEd");
230   define_builtin (BUILT_IN_SQRT, "__builtin_sqrt",
231                   double_ftype_double, "_ZN4java4lang4Math4sqrtEd");
232   define_builtin (BUILT_IN_TAN, "__builtin_tan",
233                   double_ftype_double, "_ZN4java4lang4Math3tanEd");
234 }
235
236 /* If the call matches a builtin, return the
237    appropriate builtin expression instead.  */
238 tree
239 check_for_builtin (tree method, tree call)
240 {
241   if (! flag_emit_class_files && optimize && TREE_CODE (call) == CALL_EXPR)
242     {
243       int i;
244       tree method_arguments = TREE_OPERAND (call, 1);
245       tree method_class = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method)));
246       tree method_name = DECL_NAME (method);
247       tree method_return_type = TREE_TYPE (TREE_TYPE (method));
248
249       for (i = 0; java_builtins[i].builtin_code != END_BUILTINS; ++i)
250         {
251           if (method_class == java_builtins[i].class_name.t
252               && method_name == java_builtins[i].method_name.t)
253             {
254               tree fn;
255
256               if (java_builtins[i].creator != NULL)
257                 return (*java_builtins[i].creator) (method_return_type,
258                                                     method_arguments);
259               fn = built_in_decls[java_builtins[i].builtin_code];
260               if (fn == NULL_TREE)
261                 return NULL_TREE;
262               return java_build_function_call_expr (fn, method_arguments);
263             }
264         }
265     }
266   return call;
267 }
268
269 #include "gt-java-builtins.h"