OSDN Git Service

2007-01-24 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, 2006, 2007
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 #include <stdarg.h>
38 #include "convert.h"
39 #include "rtl.h"
40 #include "insn-codes.h"
41 #include "expr.h"
42 #include "optabs.h"
43
44 static tree max_builtin (tree, tree);
45 static tree min_builtin (tree, tree);
46 static tree abs_builtin (tree, tree);
47 static tree convert_real (tree, tree);
48
49 static tree java_build_function_call_expr (tree, tree);
50
51 static tree putObject_builtin (tree, tree);
52 static tree compareAndSwapInt_builtin (tree, tree);
53 static tree compareAndSwapLong_builtin (tree, tree);
54 static tree compareAndSwapObject_builtin (tree, tree);
55 static tree putVolatile_builtin (tree, tree);
56 static tree getVolatile_builtin (tree, tree);
57 static tree VMSupportsCS8_builtin (tree, tree);
58 \f
59
60 /* Functions of this type are used to inline a given call.  Such a
61    function should either return an expression, if the call is to be
62    inlined, or NULL_TREE if a real call should be emitted.  Arguments
63    are method return type and arguments to call.  */
64 typedef tree builtin_creator_function (tree, tree);
65
66 /* Hold a char*, before initialization, or a tree, after
67    initialization.  */
68 union string_or_tree GTY(())
69 {
70   const char * GTY ((tag ("0"))) s;
71   tree GTY ((tag ("1"))) t;
72 };
73
74 /* Used to hold a single builtin record.  */
75 struct builtin_record GTY(())
76 {
77   union string_or_tree GTY ((desc ("1"))) class_name;
78   union string_or_tree GTY ((desc ("1"))) method_name;
79   builtin_creator_function * GTY((skip)) creator;
80   enum built_in_function builtin_code;
81 };
82
83 static GTY(()) struct builtin_record java_builtins[] =
84 {
85   { { "java.lang.Math" }, { "min" }, min_builtin, 0 },
86   { { "java.lang.Math" }, { "max" }, max_builtin, 0 },
87   { { "java.lang.Math" }, { "abs" }, abs_builtin, 0 },
88   { { "java.lang.Math" }, { "acos" }, NULL, BUILT_IN_ACOS },
89   { { "java.lang.Math" }, { "asin" }, NULL, BUILT_IN_ASIN },
90   { { "java.lang.Math" }, { "atan" }, NULL, BUILT_IN_ATAN },
91   { { "java.lang.Math" }, { "atan2" }, NULL, BUILT_IN_ATAN2 },
92   { { "java.lang.Math" }, { "ceil" }, NULL, BUILT_IN_CEIL },
93   { { "java.lang.Math" }, { "cos" }, NULL, BUILT_IN_COS },
94   { { "java.lang.Math" }, { "exp" }, NULL, BUILT_IN_EXP },
95   { { "java.lang.Math" }, { "floor" }, NULL, BUILT_IN_FLOOR },
96   { { "java.lang.Math" }, { "log" }, NULL, BUILT_IN_LOG },
97   { { "java.lang.Math" }, { "pow" }, NULL, BUILT_IN_POW },
98   { { "java.lang.Math" }, { "sin" }, NULL, BUILT_IN_SIN },
99   { { "java.lang.Math" }, { "sqrt" }, NULL, BUILT_IN_SQRT },
100   { { "java.lang.Math" }, { "tan" }, NULL, BUILT_IN_TAN },
101   { { "java.lang.Float" }, { "intBitsToFloat" }, convert_real, 0 },
102   { { "java.lang.Double" }, { "longBitsToDouble" }, convert_real, 0 },
103   { { "java.lang.Float" }, { "floatToRawIntBits" }, convert_real, 0 },
104   { { "java.lang.Double" }, { "doubleToRawLongBits" }, convert_real, 0 },
105   { { "sun.misc.Unsafe" }, { "putInt" }, putObject_builtin, 0},
106   { { "sun.misc.Unsafe" }, { "putLong" }, putObject_builtin, 0},
107   { { "sun.misc.Unsafe" }, { "putObject" }, putObject_builtin, 0},
108   { { "sun.misc.Unsafe" }, { "compareAndSwapInt" }, 
109     compareAndSwapInt_builtin, 0},
110   { { "sun.misc.Unsafe" }, { "compareAndSwapLong" }, 
111     compareAndSwapLong_builtin, 0},
112   { { "sun.misc.Unsafe" }, { "compareAndSwapObject" }, 
113     compareAndSwapObject_builtin, 0},
114   { { "sun.misc.Unsafe" }, { "putOrderedInt" }, putVolatile_builtin, 0},
115   { { "sun.misc.Unsafe" }, { "putOrderedLong" }, putVolatile_builtin, 0},
116   { { "sun.misc.Unsafe" }, { "putOrderedObject" }, putVolatile_builtin, 0},
117   { { "sun.misc.Unsafe" }, { "putIntVolatile" }, putVolatile_builtin, 0},
118   { { "sun.misc.Unsafe" }, { "putLongVolatile" }, putVolatile_builtin, 0},
119   { { "sun.misc.Unsafe" }, { "putObjectVolatile" }, putVolatile_builtin, 0},
120   { { "sun.misc.Unsafe" }, { "getObjectVolatile" }, getVolatile_builtin, 0},
121   { { "sun.misc.Unsafe" }, { "getIntVolatile" }, getVolatile_builtin, 0},
122   { { "sun.misc.Unsafe" }, { "getLongVolatile" }, getVolatile_builtin, 0},
123   { { "sun.misc.Unsafe" }, { "getLong" }, getVolatile_builtin, 0},
124   { { "java.util.concurrent.atomic.AtomicLong" }, { "VMSupportsCS8" }, 
125     VMSupportsCS8_builtin, 0},
126   { { NULL }, { NULL }, NULL, END_BUILTINS }
127 };
128
129 \f
130 /* Internal functions which implement various builtin conversions.  */
131
132 static tree
133 max_builtin (tree method_return_type, tree method_arguments)
134 {
135   /* MAX_EXPR does not handle -0.0 in the Java style.  */
136   if (TREE_CODE (method_return_type) == REAL_TYPE)
137     return NULL_TREE;
138   return fold_build2 (MAX_EXPR, method_return_type,
139                       TREE_VALUE (method_arguments),
140                       TREE_VALUE (TREE_CHAIN (method_arguments)));
141 }
142
143 static tree
144 min_builtin (tree method_return_type, tree method_arguments)
145 {
146   /* MIN_EXPR does not handle -0.0 in the Java style.  */
147   if (TREE_CODE (method_return_type) == REAL_TYPE)
148     return NULL_TREE;
149   return fold_build2 (MIN_EXPR, method_return_type,
150                       TREE_VALUE (method_arguments),
151                       TREE_VALUE (TREE_CHAIN (method_arguments)));
152 }
153
154 static tree
155 abs_builtin (tree method_return_type, tree method_arguments)
156 {
157   return fold_build1 (ABS_EXPR, method_return_type,
158                       TREE_VALUE (method_arguments));
159 }
160
161 /* Mostly copied from ../builtins.c.  */
162 static tree
163 java_build_function_call_expr (tree fn, tree arglist)
164 {
165   tree call_expr;
166
167   call_expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
168   return fold_build3 (CALL_EXPR, TREE_TYPE (TREE_TYPE (fn)),
169                       call_expr, arglist, NULL_TREE);
170 }
171
172 static tree
173 convert_real (tree method_return_type, tree method_arguments)
174 {
175   return build1 (VIEW_CONVERT_EXPR, method_return_type,
176                  TREE_VALUE (method_arguments));
177 }
178
179 \f
180
181 /* Provide builtin support for atomic operations.  These are
182    documented at length in libjava/sun/misc/Unsafe.java.  */
183
184 /* FIXME.  There are still a few things wrong with this logic.  In
185    particular, atomic writes of multi-word integers are not truly
186    atomic: this requires more work.
187
188    In general, double-word compare-and-swap cannot portably be
189    implemented, so we need some kind of fallback for 32-bit machines.
190
191 */
192
193
194 /* Macros to unmarshal arguments from a TREE_LIST into a few
195    variables.  We also convert the offset arg from a long to an
196    integer that is the same size as a pointer.  */
197
198 #define UNMARSHAL3(METHOD_ARGUMENTS)                                    \
199 tree this_arg, obj_arg, offset_arg;                                     \
200 do                                                                      \
201 {                                                                       \
202   tree chain = METHOD_ARGUMENTS;                                        \
203   this_arg = TREE_VALUE (chain);                                        \
204   chain = TREE_CHAIN (chain);                                           \
205   obj_arg = TREE_VALUE (chain);                                         \
206   chain = TREE_CHAIN (chain);                                           \
207   offset_arg = fold_convert (java_type_for_size (POINTER_SIZE, 0),      \
208                              TREE_VALUE (chain));                       \
209 }                                                                       \
210 while (0)
211
212 #define UNMARSHAL4(METHOD_ARGUMENTS)                                    \
213 tree value_type, this_arg, obj_arg, offset_arg, value_arg;              \
214 do                                                                      \
215 {                                                                       \
216   tree chain = METHOD_ARGUMENTS;                                        \
217   this_arg = TREE_VALUE (chain);                                        \
218   chain = TREE_CHAIN (chain);                                           \
219   obj_arg = TREE_VALUE (chain);                                         \
220   chain = TREE_CHAIN (chain);                                           \
221   offset_arg = fold_convert (java_type_for_size (POINTER_SIZE, 0),      \
222                              TREE_VALUE (chain));                       \
223   chain = TREE_CHAIN (chain);                                           \
224   value_arg = TREE_VALUE (chain);                                       \
225   value_type = TREE_TYPE (value_arg);                                   \
226 }                                                                       \
227 while (0)
228
229 #define UNMARSHAL5(METHOD_ARGUMENTS)                                    \
230 tree value_type, this_arg, obj_arg, offset_arg, expected_arg, value_arg; \
231 do                                                                      \
232 {                                                                       \
233   tree chain = METHOD_ARGUMENTS;                                        \
234   this_arg = TREE_VALUE (chain);                                        \
235   chain = TREE_CHAIN (chain);                                           \
236   obj_arg = TREE_VALUE (chain);                                         \
237   chain = TREE_CHAIN (chain);                                           \
238   offset_arg = fold_convert (java_type_for_size (POINTER_SIZE, 0),      \
239                              TREE_VALUE (chain));                       \
240   chain = TREE_CHAIN (chain);                                           \
241   expected_arg = TREE_VALUE (chain);                                    \
242   chain = TREE_CHAIN (chain);                                           \
243   value_arg = TREE_VALUE (chain);                                       \
244   value_type = TREE_TYPE (value_arg);                                   \
245 }                                                                       \
246 while (0)
247
248 /* Construct an arglist from a call.  */
249
250 static tree
251 build_arglist_for_builtin (tree arg, ...)
252 {
253   va_list ap;
254   tree nextarg;
255   tree newarglist = build_tree_list (NULL_TREE, arg);
256
257   va_start(ap, arg);
258   while ((nextarg = va_arg(ap, tree)))
259     newarglist = tree_cons (NULL_TREE, nextarg, newarglist);
260
261   return nreverse (newarglist);
262 }
263
264 /* Add an address to an offset, forming a sum.  */
265
266 static tree
267 build_addr_sum (tree type, tree addr, tree offset)
268 {
269   tree ptr_type = build_pointer_type (type);
270   return  fold_build2 (PLUS_EXPR, 
271                        ptr_type, 
272                        fold_convert (ptr_type, addr), offset);
273 }
274
275 /* Make sure that this-arg is non-NULL.  This is a security check.  */
276
277 static tree
278 build_check_this (tree stmt, tree this_arg)
279 {
280   return build2 (COMPOUND_EXPR, TREE_TYPE (stmt), 
281                  java_check_reference (this_arg, 1), stmt);
282 }
283
284 /* Now the builtins.  These correspond to the primitive functions in
285    libjava/sun/misc/natUnsafe.cc.  */
286
287 static tree
288 putObject_builtin (tree method_return_type ATTRIBUTE_UNUSED, 
289                    tree method_arguments)
290 {
291   tree addr, stmt;
292   UNMARSHAL4 (method_arguments);
293
294   addr = build_addr_sum (value_type, obj_arg, offset_arg);
295   stmt = fold_build2 (MODIFY_EXPR, value_type,
296                       build_java_indirect_ref (value_type, addr,
297                                                flag_check_references),
298                       value_arg);
299
300   return build_check_this (stmt, this_arg);
301 }
302
303 static tree
304 compareAndSwapInt_builtin (tree method_return_type ATTRIBUTE_UNUSED,
305                            tree method_arguments)
306 {
307   enum machine_mode mode = TYPE_MODE (int_type_node);
308   if (sync_compare_and_swap_cc[mode] != CODE_FOR_nothing 
309       || sync_compare_and_swap[mode] != CODE_FOR_nothing)
310     {
311       tree newarglist, addr, stmt;
312       UNMARSHAL5 (method_arguments);
313
314       addr = build_addr_sum (int_type_node, obj_arg, offset_arg);
315
316       newarglist 
317         = build_arglist_for_builtin (addr, expected_arg, value_arg, NULL_TREE);
318       stmt = (build_function_call_expr
319               (built_in_decls[BUILT_IN_BOOL_COMPARE_AND_SWAP_4],
320                newarglist));
321
322       return build_check_this (stmt, this_arg);
323     }
324   return NULL_TREE;
325 }
326
327 static tree
328 compareAndSwapLong_builtin (tree method_return_type ATTRIBUTE_UNUSED,
329                             tree method_arguments)
330 {
331   enum machine_mode mode = TYPE_MODE (long_type_node);
332   if (sync_compare_and_swap_cc[mode] != CODE_FOR_nothing 
333       || sync_compare_and_swap[mode] != CODE_FOR_nothing)
334     {
335       tree newarglist, addr, stmt;
336       UNMARSHAL5 (method_arguments);
337
338       addr = build_addr_sum (long_type_node, obj_arg, offset_arg);
339
340       newarglist 
341         = build_arglist_for_builtin (addr, expected_arg, value_arg, NULL_TREE);
342       stmt = (build_function_call_expr
343               (built_in_decls[BUILT_IN_BOOL_COMPARE_AND_SWAP_8],
344                newarglist));
345
346       return build_check_this (stmt, this_arg);
347     }
348   return NULL_TREE;
349 }
350 static tree
351 compareAndSwapObject_builtin (tree method_return_type ATTRIBUTE_UNUSED, 
352                               tree method_arguments)
353 {
354   enum machine_mode mode = TYPE_MODE (ptr_type_node);
355   if (sync_compare_and_swap_cc[mode] != CODE_FOR_nothing 
356       || sync_compare_and_swap[mode] != CODE_FOR_nothing)
357   {
358     tree newarglist, addr, stmt;
359     int builtin;
360
361     UNMARSHAL5 (method_arguments);
362     builtin = (POINTER_SIZE == 32 
363                ? BUILT_IN_BOOL_COMPARE_AND_SWAP_4 
364                : BUILT_IN_BOOL_COMPARE_AND_SWAP_8);
365
366     addr = build_addr_sum (value_type, obj_arg, offset_arg);
367
368     newarglist 
369       = build_arglist_for_builtin (addr, expected_arg, value_arg, NULL_TREE);
370     stmt = (build_function_call_expr
371             (built_in_decls[builtin],
372              newarglist));
373
374     return build_check_this (stmt, this_arg);
375   }
376   return NULL_TREE;
377 }
378
379 static tree
380 putVolatile_builtin (tree method_return_type ATTRIBUTE_UNUSED, 
381                      tree method_arguments)
382 {
383   tree newarglist, addr, stmt, modify_stmt;
384   UNMARSHAL4 (method_arguments);
385   
386   addr = build_addr_sum (value_type, obj_arg, offset_arg);
387   addr 
388     = fold_convert (build_pointer_type (build_type_variant (value_type, 0, 1)),
389                     addr);
390   
391   newarglist = NULL_TREE;
392   stmt = (build_function_call_expr
393           (built_in_decls[BUILT_IN_SYNCHRONIZE],
394            newarglist));
395   modify_stmt = fold_build2 (MODIFY_EXPR, value_type,
396                              build_java_indirect_ref (value_type, addr,
397                                                       flag_check_references),
398                              value_arg);
399   stmt = build2 (COMPOUND_EXPR, TREE_TYPE (modify_stmt), 
400                  stmt, modify_stmt);
401
402   return build_check_this (stmt, this_arg);
403 }
404
405 static tree
406 getVolatile_builtin (tree method_return_type ATTRIBUTE_UNUSED, 
407                      tree method_arguments)
408 {
409   tree newarglist, addr, stmt, modify_stmt, tmp;
410   UNMARSHAL3 (method_arguments);
411   
412   addr = build_addr_sum (method_return_type, obj_arg, offset_arg);
413   addr 
414     = fold_convert (build_pointer_type (build_type_variant 
415                                         (method_return_type, 0, 1)), addr);
416   
417   newarglist = NULL_TREE;
418   stmt = (build_function_call_expr
419           (built_in_decls[BUILT_IN_SYNCHRONIZE],
420            newarglist));
421   
422   tmp = build_decl (VAR_DECL, NULL, method_return_type);
423   DECL_IGNORED_P (tmp) = 1;
424   DECL_ARTIFICIAL (tmp) = 1;
425   pushdecl (tmp);
426
427   modify_stmt = fold_build2 (MODIFY_EXPR, method_return_type,
428                              tmp,
429                              build_java_indirect_ref (method_return_type, addr,
430                                                       flag_check_references));
431
432   stmt = build2 (COMPOUND_EXPR, void_type_node, modify_stmt, stmt);
433   stmt = build2 (COMPOUND_EXPR, method_return_type, stmt, tmp);
434   
435   return stmt;
436 }
437
438 static tree
439 VMSupportsCS8_builtin (tree method_return_type, 
440                        tree method_arguments ATTRIBUTE_UNUSED)
441 {
442   enum machine_mode mode = TYPE_MODE (long_type_node);
443   gcc_assert (method_return_type == boolean_type_node);
444   if (sync_compare_and_swap_cc[mode] != CODE_FOR_nothing 
445       || sync_compare_and_swap[mode] != CODE_FOR_nothing)
446     return boolean_true_node;
447   else
448     return boolean_false_node;
449 }  
450
451 \f
452
453 #define BUILTIN_NOTHROW 1
454 #define BUILTIN_CONST 2
455 /* Define a single builtin.  */
456 static void
457 define_builtin (enum built_in_function val,
458                 const char *name,
459                 tree type,
460                 const char *libname,
461                 int flags)
462 {
463   tree decl;
464
465   decl = build_decl (FUNCTION_DECL, get_identifier (name), type);
466   DECL_EXTERNAL (decl) = 1;
467   TREE_PUBLIC (decl) = 1;
468   SET_DECL_ASSEMBLER_NAME (decl, get_identifier (libname));
469   pushdecl (decl);
470   DECL_BUILT_IN_CLASS (decl) = BUILT_IN_NORMAL;
471   DECL_FUNCTION_CODE (decl) = val;
472   if (flags & BUILTIN_NOTHROW)
473     TREE_NOTHROW (decl) = 1;
474   if (flags & BUILTIN_CONST)
475     TREE_READONLY (decl) = 1;
476
477   implicit_built_in_decls[val] = decl;
478   built_in_decls[val] = decl;
479 }
480
481 \f
482
483 /* Initialize the builtins.  */
484 void
485 initialize_builtins (void)
486 {
487   tree double_ftype_double, double_ftype_double_double;
488   tree float_ftype_float, float_ftype_float_float;
489   tree boolean_ftype_boolean_boolean;
490   tree t;
491   int i;
492
493   for (i = 0; java_builtins[i].builtin_code != END_BUILTINS; ++i)
494     {
495       tree klass_id = get_identifier (java_builtins[i].class_name.s);
496       tree m = get_identifier (java_builtins[i].method_name.s);
497
498       java_builtins[i].class_name.t = klass_id;
499       java_builtins[i].method_name.t = m;
500     }
501
502   void_list_node = end_params_node;
503
504   t = tree_cons (NULL_TREE, float_type_node, end_params_node);
505   float_ftype_float = build_function_type (float_type_node, t);
506   t = tree_cons (NULL_TREE, float_type_node, t);
507   float_ftype_float_float = build_function_type (float_type_node, t);
508
509   t = tree_cons (NULL_TREE, double_type_node, end_params_node);
510   double_ftype_double = build_function_type (double_type_node, t);
511   t = tree_cons (NULL_TREE, double_type_node, t);
512   double_ftype_double_double = build_function_type (double_type_node, t);
513
514   define_builtin (BUILT_IN_FMOD, "__builtin_fmod",
515                   double_ftype_double_double, "fmod", BUILTIN_CONST);
516   define_builtin (BUILT_IN_FMODF, "__builtin_fmodf",
517                   float_ftype_float_float, "fmodf", BUILTIN_CONST);
518
519   define_builtin (BUILT_IN_ACOS, "__builtin_acos",
520                   double_ftype_double, "_ZN4java4lang4Math4acosEJdd",
521                   BUILTIN_CONST);
522   define_builtin (BUILT_IN_ASIN, "__builtin_asin",
523                   double_ftype_double, "_ZN4java4lang4Math4asinEJdd",
524                   BUILTIN_CONST);
525   define_builtin (BUILT_IN_ATAN, "__builtin_atan",
526                   double_ftype_double, "_ZN4java4lang4Math4atanEJdd",
527                   BUILTIN_CONST);
528   define_builtin (BUILT_IN_ATAN2, "__builtin_atan2",
529                   double_ftype_double_double, "_ZN4java4lang4Math5atan2EJddd",
530                   BUILTIN_CONST);
531   define_builtin (BUILT_IN_CEIL, "__builtin_ceil",
532                   double_ftype_double, "_ZN4java4lang4Math4ceilEJdd",
533                   BUILTIN_CONST);
534   define_builtin (BUILT_IN_COS, "__builtin_cos",
535                   double_ftype_double, "_ZN4java4lang4Math3cosEJdd",
536                   BUILTIN_CONST);
537   define_builtin (BUILT_IN_EXP, "__builtin_exp",
538                   double_ftype_double, "_ZN4java4lang4Math3expEJdd",
539                   BUILTIN_CONST);
540   define_builtin (BUILT_IN_FLOOR, "__builtin_floor",
541                   double_ftype_double, "_ZN4java4lang4Math5floorEJdd",
542                   BUILTIN_CONST);
543   define_builtin (BUILT_IN_LOG, "__builtin_log",
544                   double_ftype_double, "_ZN4java4lang4Math3logEJdd",
545                   BUILTIN_CONST);
546   define_builtin (BUILT_IN_POW, "__builtin_pow",
547                   double_ftype_double_double, "_ZN4java4lang4Math3powEJddd",
548                   BUILTIN_CONST);
549   define_builtin (BUILT_IN_SIN, "__builtin_sin",
550                   double_ftype_double, "_ZN4java4lang4Math3sinEJdd",
551                   BUILTIN_CONST);
552   define_builtin (BUILT_IN_SQRT, "__builtin_sqrt",
553                   double_ftype_double, "_ZN4java4lang4Math4sqrtEJdd",
554                   BUILTIN_CONST);
555   define_builtin (BUILT_IN_TAN, "__builtin_tan",
556                   double_ftype_double, "_ZN4java4lang4Math3tanEJdd",
557                   BUILTIN_CONST);
558   
559   t = tree_cons (NULL_TREE, boolean_type_node, end_params_node);
560   t = tree_cons (NULL_TREE, boolean_type_node, t);
561   boolean_ftype_boolean_boolean = build_function_type (boolean_type_node, t);
562   define_builtin (BUILT_IN_EXPECT, "__builtin_expect", 
563                   boolean_ftype_boolean_boolean,
564                   "__builtin_expect",
565                   BUILTIN_CONST | BUILTIN_NOTHROW);
566   define_builtin (BUILT_IN_BOOL_COMPARE_AND_SWAP_4, 
567                   "__sync_bool_compare_and_swap_4",
568                   build_function_type_list (boolean_type_node,
569                                             int_type_node, 
570                                             build_pointer_type (int_type_node),
571                                             int_type_node, NULL_TREE), 
572                   "__sync_bool_compare_and_swap_4", 0);
573   define_builtin (BUILT_IN_BOOL_COMPARE_AND_SWAP_8, 
574                   "__sync_bool_compare_and_swap_8",
575                   build_function_type_list (boolean_type_node,
576                                             long_type_node, 
577                                             build_pointer_type (long_type_node),
578                                             int_type_node, NULL_TREE), 
579                   "__sync_bool_compare_and_swap_8", 0);
580   define_builtin (BUILT_IN_SYNCHRONIZE, "__sync_synchronize",
581                   build_function_type (void_type_node, void_list_node),
582                   "__sync_synchronize", BUILTIN_NOTHROW);
583   
584   define_builtin (BUILT_IN_RETURN_ADDRESS, "__builtin_return_address",
585                   build_function_type_list (ptr_type_node, int_type_node, NULL_TREE),
586                   "__builtin_return_address", BUILTIN_NOTHROW);
587
588   build_common_builtin_nodes ();
589 }
590
591 /* If the call matches a builtin, return the
592    appropriate builtin expression instead.  */
593 tree
594 check_for_builtin (tree method, tree call)
595 {
596   if (optimize && TREE_CODE (call) == CALL_EXPR)
597     {
598       int i;
599       tree method_arguments = TREE_OPERAND (call, 1);
600       tree method_class = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method)));
601       tree method_name = DECL_NAME (method);
602       tree method_return_type = TREE_TYPE (TREE_TYPE (method));
603
604       for (i = 0; java_builtins[i].builtin_code != END_BUILTINS; ++i)
605         {
606           if (method_class == java_builtins[i].class_name.t
607               && method_name == java_builtins[i].method_name.t)
608             {
609               tree fn;
610
611               if (java_builtins[i].creator != NULL)
612                 {
613                   tree result
614                     = (*java_builtins[i].creator) (method_return_type,
615                                                    method_arguments);
616                   return result == NULL_TREE ? call : result;
617                 }
618
619               /* Builtin functions emit a direct call which is incompatible
620                  with the BC-ABI.  */
621               if (flag_indirect_dispatch)
622                 return call;
623               fn = built_in_decls[java_builtins[i].builtin_code];
624               if (fn == NULL_TREE)
625                 return call;
626               return java_build_function_call_expr (fn, method_arguments);
627             }
628         }
629     }
630   return call;
631 }
632
633 #include "gt-java-builtins.h"