1 /* Built-in and inline functions for gcj
2 Copyright (C) 2001, 2003, 2004, 2005, 2006
3 Free Software Foundation, Inc.
5 This file is part of GCC.
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)
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.
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.
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. */
26 /* Written by Tom Tromey <tromey@redhat.com>. */
30 #include "coretypes.h"
35 #include "langhooks.h"
36 #include "java-tree.h"
40 #include "insn-codes.h"
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);
49 static tree java_build_function_call_expr (tree, tree);
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);
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);
66 /* Hold a char*, before initialization, or a tree, after
68 union string_or_tree GTY(())
70 const char * GTY ((tag ("0"))) s;
71 tree GTY ((tag ("1"))) t;
74 /* Used to hold a single builtin record. */
75 struct builtin_record GTY(())
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;
83 static GTY(()) struct builtin_record java_builtins[] =
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 { { NULL }, { NULL }, NULL, END_BUILTINS }
128 /* Internal functions which implement various builtin conversions. */
131 max_builtin (tree method_return_type, tree method_arguments)
133 /* MAX_EXPR does not handle -0.0 in the Java style. */
134 if (TREE_CODE (method_return_type) == REAL_TYPE)
136 return fold_build2 (MAX_EXPR, method_return_type,
137 TREE_VALUE (method_arguments),
138 TREE_VALUE (TREE_CHAIN (method_arguments)));
142 min_builtin (tree method_return_type, tree method_arguments)
144 /* MIN_EXPR does not handle -0.0 in the Java style. */
145 if (TREE_CODE (method_return_type) == REAL_TYPE)
147 return fold_build2 (MIN_EXPR, method_return_type,
148 TREE_VALUE (method_arguments),
149 TREE_VALUE (TREE_CHAIN (method_arguments)));
153 abs_builtin (tree method_return_type, tree method_arguments)
155 return fold_build1 (ABS_EXPR, method_return_type,
156 TREE_VALUE (method_arguments));
159 /* Mostly copied from ../builtins.c. */
161 java_build_function_call_expr (tree fn, tree arglist)
165 call_expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
166 return fold_build3 (CALL_EXPR, TREE_TYPE (TREE_TYPE (fn)),
167 call_expr, arglist, NULL_TREE);
171 convert_real (tree method_return_type, tree method_arguments)
173 return build1 (VIEW_CONVERT_EXPR, method_return_type,
174 TREE_VALUE (method_arguments));
179 /* Provide builtin support for atomic operations. These are
180 documented at length in libjava/sun/misc/Unsafe.java. */
182 /* FIXME. There are still a few things wrong with this logic. In
183 particular, atomic writes of multi-word integers are not truly
184 atomic: this requires more work.
186 In general, double-word compare-and-swap cannot portably be
187 implemented, so we need some kind of fallback for 32-bit machines.
192 /* Macros to unmarshal arguments from a TREE_LIST into a few
193 variables. We also convert the offset arg from a long to an
194 integer that is the same size as a pointer. */
196 #define UNMARSHAL3(METHOD_ARGUMENTS) \
197 tree this_arg, obj_arg, offset_arg; \
200 tree chain = METHOD_ARGUMENTS; \
201 this_arg = TREE_VALUE (chain); \
202 chain = TREE_CHAIN (chain); \
203 obj_arg = TREE_VALUE (chain); \
204 chain = TREE_CHAIN (chain); \
205 offset_arg = fold_convert (java_type_for_size (POINTER_SIZE, 0), \
206 TREE_VALUE (chain)); \
210 #define UNMARSHAL4(METHOD_ARGUMENTS) \
211 tree value_type, this_arg, obj_arg, offset_arg, value_arg; \
214 tree chain = METHOD_ARGUMENTS; \
215 this_arg = TREE_VALUE (chain); \
216 chain = TREE_CHAIN (chain); \
217 obj_arg = TREE_VALUE (chain); \
218 chain = TREE_CHAIN (chain); \
219 offset_arg = fold_convert (java_type_for_size (POINTER_SIZE, 0), \
220 TREE_VALUE (chain)); \
221 chain = TREE_CHAIN (chain); \
222 value_arg = TREE_VALUE (chain); \
223 value_type = TREE_TYPE (value_arg); \
227 #define UNMARSHAL5(METHOD_ARGUMENTS) \
228 tree value_type, this_arg, obj_arg, offset_arg, expected_arg, value_arg; \
231 tree chain = METHOD_ARGUMENTS; \
232 this_arg = TREE_VALUE (chain); \
233 chain = TREE_CHAIN (chain); \
234 obj_arg = TREE_VALUE (chain); \
235 chain = TREE_CHAIN (chain); \
236 offset_arg = fold_convert (java_type_for_size (POINTER_SIZE, 0), \
237 TREE_VALUE (chain)); \
238 chain = TREE_CHAIN (chain); \
239 expected_arg = TREE_VALUE (chain); \
240 chain = TREE_CHAIN (chain); \
241 value_arg = TREE_VALUE (chain); \
242 value_type = TREE_TYPE (value_arg); \
246 /* Construct an arglist from a call. */
249 build_arglist_for_builtin (tree arg, ...)
253 tree newarglist = build_tree_list (NULL_TREE, arg);
256 while ((nextarg = va_arg(ap, tree)))
257 newarglist = tree_cons (NULL_TREE, nextarg, newarglist);
259 return nreverse (newarglist);
262 /* Add an address to an offset, forming a sum. */
265 build_addr_sum (tree type, tree addr, tree offset)
267 tree ptr_type = build_pointer_type (type);
268 return fold_build2 (PLUS_EXPR,
270 fold_convert (ptr_type, addr), offset);
273 /* Make sure that this-arg is non-NULL. This is a security check. */
276 build_check_this (tree stmt, tree this_arg)
278 return build2 (COMPOUND_EXPR, TREE_TYPE (stmt),
279 java_check_reference (this_arg, 1), stmt);
282 /* Now the builtins. These correspond to the primitive functions in
283 libjava/sun/misc/natUnsafe.cc. */
286 putObject_builtin (tree method_return_type ATTRIBUTE_UNUSED,
287 tree method_arguments)
290 UNMARSHAL4 (method_arguments);
292 addr = build_addr_sum (value_type, obj_arg, offset_arg);
293 stmt = fold_build2 (MODIFY_EXPR, value_type,
294 build_java_indirect_ref (value_type, addr,
295 flag_check_references),
298 return build_check_this (stmt, this_arg);
302 compareAndSwapInt_builtin (tree method_return_type ATTRIBUTE_UNUSED,
303 tree method_arguments)
305 enum machine_mode mode = TYPE_MODE (int_type_node);
306 if (sync_compare_and_swap_cc[mode] != CODE_FOR_nothing
307 || sync_compare_and_swap[mode] != CODE_FOR_nothing)
309 tree newarglist, addr, stmt;
310 UNMARSHAL5 (method_arguments);
312 addr = build_addr_sum (int_type_node, obj_arg, offset_arg);
315 = build_arglist_for_builtin (addr, expected_arg, value_arg, NULL_TREE);
316 stmt = (build_function_call_expr
317 (built_in_decls[BUILT_IN_BOOL_COMPARE_AND_SWAP_4],
320 return build_check_this (stmt, this_arg);
326 compareAndSwapLong_builtin (tree method_return_type ATTRIBUTE_UNUSED,
327 tree method_arguments)
329 enum machine_mode mode = TYPE_MODE (long_type_node);
330 if (sync_compare_and_swap_cc[mode] != CODE_FOR_nothing
331 || sync_compare_and_swap[mode] != CODE_FOR_nothing)
333 tree newarglist, addr, stmt;
334 UNMARSHAL5 (method_arguments);
336 addr = build_addr_sum (long_type_node, obj_arg, offset_arg);
339 = build_arglist_for_builtin (addr, expected_arg, value_arg, NULL_TREE);
340 stmt = (build_function_call_expr
341 (built_in_decls[BUILT_IN_BOOL_COMPARE_AND_SWAP_8],
344 return build_check_this (stmt, this_arg);
349 compareAndSwapObject_builtin (tree method_return_type ATTRIBUTE_UNUSED,
350 tree method_arguments)
352 enum machine_mode mode = TYPE_MODE (ptr_type_node);
353 if (sync_compare_and_swap_cc[mode] != CODE_FOR_nothing
354 || sync_compare_and_swap[mode] != CODE_FOR_nothing)
356 tree newarglist, addr, stmt;
359 UNMARSHAL5 (method_arguments);
360 builtin = (POINTER_SIZE == 32
361 ? BUILT_IN_BOOL_COMPARE_AND_SWAP_4
362 : BUILT_IN_BOOL_COMPARE_AND_SWAP_8);
364 addr = build_addr_sum (value_type, obj_arg, offset_arg);
367 = build_arglist_for_builtin (addr, expected_arg, value_arg, NULL_TREE);
368 stmt = (build_function_call_expr
369 (built_in_decls[builtin],
372 return build_check_this (stmt, this_arg);
378 putVolatile_builtin (tree method_return_type ATTRIBUTE_UNUSED,
379 tree method_arguments)
381 tree newarglist, addr, stmt, modify_stmt;
382 UNMARSHAL4 (method_arguments);
384 addr = build_addr_sum (value_type, obj_arg, offset_arg);
386 = fold_convert (build_pointer_type (build_type_variant (value_type, 0, 1)),
389 newarglist = NULL_TREE;
390 stmt = (build_function_call_expr
391 (built_in_decls[BUILT_IN_SYNCHRONIZE],
393 modify_stmt = fold_build2 (MODIFY_EXPR, value_type,
394 build_java_indirect_ref (value_type, addr,
395 flag_check_references),
397 stmt = build2 (COMPOUND_EXPR, TREE_TYPE (modify_stmt),
400 return build_check_this (stmt, this_arg);
404 getVolatile_builtin (tree method_return_type ATTRIBUTE_UNUSED,
405 tree method_arguments)
407 tree newarglist, addr, stmt, modify_stmt, tmp;
408 UNMARSHAL3 (method_arguments);
410 addr = build_addr_sum (method_return_type, obj_arg, offset_arg);
412 = fold_convert (build_pointer_type (build_type_variant
413 (method_return_type, 0, 1)), addr);
415 newarglist = NULL_TREE;
416 stmt = (build_function_call_expr
417 (built_in_decls[BUILT_IN_SYNCHRONIZE],
420 tmp = build_decl (VAR_DECL, NULL, method_return_type);
421 DECL_IGNORED_P (tmp) = 1;
422 DECL_ARTIFICIAL (tmp) = 1;
425 modify_stmt = fold_build2 (MODIFY_EXPR, method_return_type,
427 build_java_indirect_ref (method_return_type, addr,
428 flag_check_references));
430 stmt = build2 (COMPOUND_EXPR, void_type_node, modify_stmt, stmt);
431 stmt = build2 (COMPOUND_EXPR, method_return_type, stmt, tmp);
438 #define BUILTIN_NOTHROW 1
439 #define BUILTIN_CONST 2
440 /* Define a single builtin. */
442 define_builtin (enum built_in_function val,
450 decl = build_decl (FUNCTION_DECL, get_identifier (name), type);
451 DECL_EXTERNAL (decl) = 1;
452 TREE_PUBLIC (decl) = 1;
453 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (libname));
455 DECL_BUILT_IN_CLASS (decl) = BUILT_IN_NORMAL;
456 DECL_FUNCTION_CODE (decl) = val;
457 if (flags & BUILTIN_NOTHROW)
458 TREE_NOTHROW (decl) = 1;
459 if (flags & BUILTIN_CONST)
460 TREE_READONLY (decl) = 1;
462 implicit_built_in_decls[val] = decl;
463 built_in_decls[val] = decl;
468 /* Initialize the builtins. */
470 initialize_builtins (void)
472 tree double_ftype_double, double_ftype_double_double;
473 tree float_ftype_float, float_ftype_float_float;
474 tree boolean_ftype_boolean_boolean;
478 for (i = 0; java_builtins[i].builtin_code != END_BUILTINS; ++i)
480 tree klass_id = get_identifier (java_builtins[i].class_name.s);
481 tree m = get_identifier (java_builtins[i].method_name.s);
483 java_builtins[i].class_name.t = klass_id;
484 java_builtins[i].method_name.t = m;
487 void_list_node = end_params_node;
489 t = tree_cons (NULL_TREE, float_type_node, end_params_node);
490 float_ftype_float = build_function_type (float_type_node, t);
491 t = tree_cons (NULL_TREE, float_type_node, t);
492 float_ftype_float_float = build_function_type (float_type_node, t);
494 t = tree_cons (NULL_TREE, double_type_node, end_params_node);
495 double_ftype_double = build_function_type (double_type_node, t);
496 t = tree_cons (NULL_TREE, double_type_node, t);
497 double_ftype_double_double = build_function_type (double_type_node, t);
499 define_builtin (BUILT_IN_FMOD, "__builtin_fmod",
500 double_ftype_double_double, "fmod", BUILTIN_CONST);
501 define_builtin (BUILT_IN_FMODF, "__builtin_fmodf",
502 float_ftype_float_float, "fmodf", BUILTIN_CONST);
504 define_builtin (BUILT_IN_ACOS, "__builtin_acos",
505 double_ftype_double, "_ZN4java4lang4Math4acosEJdd",
507 define_builtin (BUILT_IN_ASIN, "__builtin_asin",
508 double_ftype_double, "_ZN4java4lang4Math4asinEJdd",
510 define_builtin (BUILT_IN_ATAN, "__builtin_atan",
511 double_ftype_double, "_ZN4java4lang4Math4atanEJdd",
513 define_builtin (BUILT_IN_ATAN2, "__builtin_atan2",
514 double_ftype_double_double, "_ZN4java4lang4Math5atan2EJddd",
516 define_builtin (BUILT_IN_CEIL, "__builtin_ceil",
517 double_ftype_double, "_ZN4java4lang4Math4ceilEJdd",
519 define_builtin (BUILT_IN_COS, "__builtin_cos",
520 double_ftype_double, "_ZN4java4lang4Math3cosEJdd",
522 define_builtin (BUILT_IN_EXP, "__builtin_exp",
523 double_ftype_double, "_ZN4java4lang4Math3expEJdd",
525 define_builtin (BUILT_IN_FLOOR, "__builtin_floor",
526 double_ftype_double, "_ZN4java4lang4Math5floorEJdd",
528 define_builtin (BUILT_IN_LOG, "__builtin_log",
529 double_ftype_double, "_ZN4java4lang4Math3logEJdd",
531 define_builtin (BUILT_IN_POW, "__builtin_pow",
532 double_ftype_double_double, "_ZN4java4lang4Math3powEJddd",
534 define_builtin (BUILT_IN_SIN, "__builtin_sin",
535 double_ftype_double, "_ZN4java4lang4Math3sinEJdd",
537 define_builtin (BUILT_IN_SQRT, "__builtin_sqrt",
538 double_ftype_double, "_ZN4java4lang4Math4sqrtEJdd",
540 define_builtin (BUILT_IN_TAN, "__builtin_tan",
541 double_ftype_double, "_ZN4java4lang4Math3tanEJdd",
544 t = tree_cons (NULL_TREE, boolean_type_node, end_params_node);
545 t = tree_cons (NULL_TREE, boolean_type_node, t);
546 boolean_ftype_boolean_boolean = build_function_type (boolean_type_node, t);
547 define_builtin (BUILT_IN_EXPECT, "__builtin_expect",
548 boolean_ftype_boolean_boolean,
550 BUILTIN_CONST | BUILTIN_NOTHROW);
551 define_builtin (BUILT_IN_BOOL_COMPARE_AND_SWAP_4,
552 "__sync_bool_compare_and_swap_4",
553 build_function_type_list (boolean_type_node,
555 build_pointer_type (int_type_node),
556 int_type_node, NULL_TREE),
557 "__sync_bool_compare_and_swap_4", 0);
558 define_builtin (BUILT_IN_BOOL_COMPARE_AND_SWAP_8,
559 "__sync_bool_compare_and_swap_8",
560 build_function_type_list (boolean_type_node,
562 build_pointer_type (long_type_node),
563 int_type_node, NULL_TREE),
564 "__sync_bool_compare_and_swap_8", 0);
565 define_builtin (BUILT_IN_SYNCHRONIZE, "__sync_synchronize",
566 build_function_type (void_type_node, void_list_node),
567 "__sync_synchronize", BUILTIN_NOTHROW);
569 define_builtin (BUILT_IN_RETURN_ADDRESS, "__builtin_return_address",
570 build_function_type_list (ptr_type_node, int_type_node, NULL_TREE),
571 "__builtin_return_address", BUILTIN_NOTHROW);
573 build_common_builtin_nodes ();
576 /* If the call matches a builtin, return the
577 appropriate builtin expression instead. */
579 check_for_builtin (tree method, tree call)
581 if (! flag_emit_class_files && optimize && TREE_CODE (call) == CALL_EXPR)
584 tree method_arguments = TREE_OPERAND (call, 1);
585 tree method_class = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method)));
586 tree method_name = DECL_NAME (method);
587 tree method_return_type = TREE_TYPE (TREE_TYPE (method));
589 for (i = 0; java_builtins[i].builtin_code != END_BUILTINS; ++i)
591 if (method_class == java_builtins[i].class_name.t
592 && method_name == java_builtins[i].method_name.t)
596 if (java_builtins[i].creator != NULL)
599 = (*java_builtins[i].creator) (method_return_type,
601 return result == NULL_TREE ? call : result;
604 /* Builtin functions emit a direct call which is incompatible
606 if (flag_indirect_dispatch)
608 fn = built_in_decls[java_builtins[i].builtin_code];
611 return java_build_function_call_expr (fn, method_arguments);
618 #include "gt-java-builtins.h"