OSDN Git Service

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