OSDN Git Service

2005-09-18 James A. Morrison <phython@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / gcc / java / builtins.c
1 /* Built-in and inline functions for gcj
2    Copyright (C) 2001, 2003, 2004, 2005
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, 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, 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
39 static tree max_builtin (tree, tree);
40 static tree min_builtin (tree, tree);
41 static tree abs_builtin (tree, tree);
42
43 static tree java_build_function_call_expr (tree, tree);
44
45 \f
46
47 /* Functions of this type are used to inline a given call.  Such a
48    function should either return an expression, if the call is to be
49    inlined, or NULL_TREE if a real call should be emitted.  Arguments
50    are method return type and arguments to call.  */
51 typedef tree builtin_creator_function (tree, tree);
52
53 /* Hold a char*, before initialization, or a tree, after
54    initialization.  */
55 union string_or_tree GTY(())
56 {
57   const char * GTY ((tag ("0"))) s;
58   tree GTY ((tag ("1"))) t;
59 };
60
61 /* Used to hold a single builtin record.  */
62 struct builtin_record GTY(())
63 {
64   union string_or_tree GTY ((desc ("1"))) class_name;
65   union string_or_tree GTY ((desc ("1"))) method_name;
66   builtin_creator_function * GTY((skip)) creator;
67   enum built_in_function builtin_code;
68 };
69
70 static GTY(()) struct builtin_record java_builtins[] =
71 {
72   { { "java.lang.Math" }, { "min" }, min_builtin, 0 },
73   { { "java.lang.Math" }, { "max" }, max_builtin, 0 },
74   { { "java.lang.Math" }, { "abs" }, abs_builtin, 0 },
75   { { "java.lang.Math" }, { "acos" }, NULL, BUILT_IN_ACOS },
76   { { "java.lang.Math" }, { "asin" }, NULL, BUILT_IN_ASIN },
77   { { "java.lang.Math" }, { "atan" }, NULL, BUILT_IN_ATAN },
78   { { "java.lang.Math" }, { "atan2" }, NULL, BUILT_IN_ATAN2 },
79   { { "java.lang.Math" }, { "ceil" }, NULL, BUILT_IN_CEIL },
80   { { "java.lang.Math" }, { "cos" }, NULL, BUILT_IN_COS },
81   { { "java.lang.Math" }, { "exp" }, NULL, BUILT_IN_EXP },
82   { { "java.lang.Math" }, { "floor" }, NULL, BUILT_IN_FLOOR },
83   { { "java.lang.Math" }, { "log" }, NULL, BUILT_IN_LOG },
84   { { "java.lang.Math" }, { "pow" }, NULL, BUILT_IN_POW },
85   { { "java.lang.Math" }, { "sin" }, NULL, BUILT_IN_SIN },
86   { { "java.lang.Math" }, { "sqrt" }, NULL, BUILT_IN_SQRT },
87   { { "java.lang.Math" }, { "tan" }, NULL, BUILT_IN_TAN },
88   { { NULL }, { NULL }, NULL, END_BUILTINS }
89 };
90
91 \f
92 /* Internal functions which implement various builtin conversions.  */
93
94 static tree
95 max_builtin (tree method_return_type, tree method_arguments)
96 {
97   return fold_build2 (MAX_EXPR, method_return_type,
98                       TREE_VALUE (method_arguments),
99                       TREE_VALUE (TREE_CHAIN (method_arguments)));
100 }
101
102 static tree
103 min_builtin (tree method_return_type, tree method_arguments)
104 {
105   return fold_build2 (MIN_EXPR, method_return_type,
106                       TREE_VALUE (method_arguments),
107                       TREE_VALUE (TREE_CHAIN (method_arguments)));
108 }
109
110 static tree
111 abs_builtin (tree method_return_type, tree method_arguments)
112 {
113   return fold_build1 (ABS_EXPR, method_return_type,
114                       TREE_VALUE (method_arguments));
115 }
116
117 /* Mostly copied from ../builtins.c.  */
118 static tree
119 java_build_function_call_expr (tree fn, tree arglist)
120 {
121   tree call_expr;
122
123   call_expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
124   return fold_build3 (CALL_EXPR, TREE_TYPE (TREE_TYPE (fn)),
125                       call_expr, arglist, NULL_TREE);
126 }
127
128 \f
129
130 #define BUILTIN_NOTHROW 1
131 #define BUILTIN_CONST 2
132 /* Define a single builtin.  */
133 static void
134 define_builtin (enum built_in_function val,
135                 const char *name,
136                 tree type,
137                 const char *libname,
138                 int flags)
139 {
140   tree decl;
141
142   decl = build_decl (FUNCTION_DECL, get_identifier (name), type);
143   DECL_EXTERNAL (decl) = 1;
144   TREE_PUBLIC (decl) = 1;
145   SET_DECL_ASSEMBLER_NAME (decl, get_identifier (libname));
146   pushdecl (decl);
147   DECL_BUILT_IN_CLASS (decl) = BUILT_IN_NORMAL;
148   DECL_FUNCTION_CODE (decl) = val;
149   if (flags & BUILTIN_NOTHROW)
150     TREE_NOTHROW (decl) = 1;
151   if (flags & BUILTIN_CONST)
152     TREE_READONLY (decl) = 1;
153
154   implicit_built_in_decls[val] = decl;
155   built_in_decls[val] = decl;
156 }
157
158 \f
159
160 /* Initialize the builtins.  */
161 void
162 initialize_builtins (void)
163 {
164   tree double_ftype_double, double_ftype_double_double;
165   tree float_ftype_float, float_ftype_float_float;
166   tree boolean_ftype_boolean_boolean;
167   tree t;
168   int i;
169
170   for (i = 0; java_builtins[i].builtin_code != END_BUILTINS; ++i)
171     {
172       tree klass_id = get_identifier (java_builtins[i].class_name.s);
173       tree m = get_identifier (java_builtins[i].method_name.s);
174
175       java_builtins[i].class_name.t = klass_id;
176       java_builtins[i].method_name.t = m;
177     }
178
179   void_list_node = end_params_node;
180
181   t = tree_cons (NULL_TREE, float_type_node, end_params_node);
182   float_ftype_float = build_function_type (float_type_node, t);
183   t = tree_cons (NULL_TREE, float_type_node, t);
184   float_ftype_float_float = build_function_type (float_type_node, t);
185
186   t = tree_cons (NULL_TREE, double_type_node, end_params_node);
187   double_ftype_double = build_function_type (double_type_node, t);
188   t = tree_cons (NULL_TREE, double_type_node, t);
189   double_ftype_double_double = build_function_type (double_type_node, t);
190
191   define_builtin (BUILT_IN_FMOD, "__builtin_fmod",
192                   double_ftype_double_double, "fmod", BUILTIN_CONST);
193   define_builtin (BUILT_IN_FMODF, "__builtin_fmodf",
194                   float_ftype_float_float, "fmodf", BUILTIN_CONST);
195
196   define_builtin (BUILT_IN_ACOS, "__builtin_acos",
197                   double_ftype_double, "_ZN4java4lang4Math4acosEd",
198                   BUILTIN_CONST);
199   define_builtin (BUILT_IN_ASIN, "__builtin_asin",
200                   double_ftype_double, "_ZN4java4lang4Math4asinEd",
201                   BUILTIN_CONST);
202   define_builtin (BUILT_IN_ATAN, "__builtin_atan",
203                   double_ftype_double, "_ZN4java4lang4Math4atanEd",
204                   BUILTIN_CONST);
205   define_builtin (BUILT_IN_ATAN2, "__builtin_atan2",
206                   double_ftype_double_double, "_ZN4java4lang4Math5atan2Edd",
207                   BUILTIN_CONST);
208   define_builtin (BUILT_IN_CEIL, "__builtin_ceil",
209                   double_ftype_double, "_ZN4java4lang4Math4ceilEd",
210                   BUILTIN_CONST);
211   define_builtin (BUILT_IN_COS, "__builtin_cos",
212                   double_ftype_double, "_ZN4java4lang4Math3cosEd",
213                   BUILTIN_CONST);
214   define_builtin (BUILT_IN_EXP, "__builtin_exp",
215                   double_ftype_double, "_ZN4java4lang4Math3expEd",
216                   BUILTIN_CONST);
217   define_builtin (BUILT_IN_FLOOR, "__builtin_floor",
218                   double_ftype_double, "_ZN4java4lang4Math5floorEd",
219                   BUILTIN_CONST);
220   define_builtin (BUILT_IN_LOG, "__builtin_log",
221                   double_ftype_double, "_ZN4java4lang4Math3logEd",
222                   BUILTIN_CONST);
223   define_builtin (BUILT_IN_POW, "__builtin_pow",
224                   double_ftype_double_double, "_ZN4java4lang4Math3powEdd",
225                   BUILTIN_CONST);
226   define_builtin (BUILT_IN_SIN, "__builtin_sin",
227                   double_ftype_double, "_ZN4java4lang4Math3sinEd",
228                   BUILTIN_CONST);
229   define_builtin (BUILT_IN_SQRT, "__builtin_sqrt",
230                   double_ftype_double, "_ZN4java4lang4Math4sqrtEd",
231                   BUILTIN_CONST);
232   define_builtin (BUILT_IN_TAN, "__builtin_tan",
233                   double_ftype_double, "_ZN4java4lang4Math3tanEd",
234                   BUILTIN_CONST);
235   
236   t = tree_cons (NULL_TREE, boolean_type_node, end_params_node);
237   t = tree_cons (NULL_TREE, boolean_type_node, t);
238   boolean_ftype_boolean_boolean = build_function_type (boolean_type_node, t);
239   define_builtin (BUILT_IN_EXPECT, "__builtin_expect", 
240                   boolean_ftype_boolean_boolean,
241                   "__builtin_expect",
242                   BUILTIN_CONST | BUILTIN_NOTHROW);
243                   
244   build_common_builtin_nodes ();
245 }
246
247 /* If the call matches a builtin, return the
248    appropriate builtin expression instead.  */
249 tree
250 check_for_builtin (tree method, tree call)
251 {
252   if (! flag_emit_class_files && optimize && TREE_CODE (call) == CALL_EXPR)
253     {
254       int i;
255       tree method_arguments = TREE_OPERAND (call, 1);
256       tree method_class = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method)));
257       tree method_name = DECL_NAME (method);
258       tree method_return_type = TREE_TYPE (TREE_TYPE (method));
259
260       for (i = 0; java_builtins[i].builtin_code != END_BUILTINS; ++i)
261         {
262           if (method_class == java_builtins[i].class_name.t
263               && method_name == java_builtins[i].method_name.t)
264             {
265               tree fn;
266
267               if (java_builtins[i].creator != NULL)
268                 return (*java_builtins[i].creator) (method_return_type,
269                                                     method_arguments);
270               fn = built_in_decls[java_builtins[i].builtin_code];
271               if (fn == NULL_TREE)
272                 return NULL_TREE;
273               return java_build_function_call_expr (fn, method_arguments);
274             }
275         }
276     }
277   return call;
278 }
279
280 #include "gt-java-builtins.h"