OSDN Git Service

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