1 /* Built-in and inline functions for gcj
2 Copyright (C) 2001, 2003, 2004, 2005, 2006, 2007, 2009, 2010
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 3, 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.
21 Java and all Java-based marks are trademarks or registered trademarks
22 of Sun Microsystems, Inc. in the United States and other countries.
23 The Free Software Foundation is independent of Sun Microsystems, Inc. */
25 /* Written by Tom Tromey <tromey@redhat.com>. */
29 #include "coretypes.h"
34 #include "langhooks.h"
35 #include "java-tree.h"
39 #include "insn-codes.h"
43 static tree max_builtin (tree, tree);
44 static tree min_builtin (tree, tree);
45 static tree abs_builtin (tree, tree);
46 static tree convert_real (tree, tree);
48 static tree java_build_function_call_expr (tree, tree);
50 static tree putObject_builtin (tree, tree);
51 static tree compareAndSwapInt_builtin (tree, tree);
52 static tree compareAndSwapLong_builtin (tree, tree);
53 static tree compareAndSwapObject_builtin (tree, tree);
54 static tree putVolatile_builtin (tree, tree);
55 static tree getVolatile_builtin (tree, tree);
56 static tree VMSupportsCS8_builtin (tree, tree);
59 /* Functions of this type are used to inline a given call. Such a
60 function should either return an expression, if the call is to be
61 inlined, or NULL_TREE if a real call should be emitted. Arguments
62 are method return type and the original CALL_EXPR containing the
63 arguments to the call. */
64 typedef tree builtin_creator_function (tree, tree);
66 /* Hold a char*, before initialization, or a tree, after
68 union GTY(()) string_or_tree {
69 const char * GTY ((tag ("0"))) s;
70 tree GTY ((tag ("1"))) t;
73 /* Used to hold a single builtin record. */
74 struct GTY(()) builtin_record {
75 union string_or_tree GTY ((desc ("1"))) class_name;
76 union string_or_tree GTY ((desc ("1"))) method_name;
77 builtin_creator_function * GTY((skip)) creator;
78 enum built_in_function builtin_code;
81 static GTY(()) struct builtin_record java_builtins[] =
83 { { "java.lang.Math" }, { "min" }, min_builtin, (enum built_in_function) 0 },
84 { { "java.lang.Math" }, { "max" }, max_builtin, (enum built_in_function) 0 },
85 { { "java.lang.Math" }, { "abs" }, abs_builtin, (enum built_in_function) 0 },
86 { { "java.lang.Math" }, { "acos" }, NULL, BUILT_IN_ACOS },
87 { { "java.lang.Math" }, { "asin" }, NULL, BUILT_IN_ASIN },
88 { { "java.lang.Math" }, { "atan" }, NULL, BUILT_IN_ATAN },
89 { { "java.lang.Math" }, { "atan2" }, NULL, BUILT_IN_ATAN2 },
90 { { "java.lang.Math" }, { "ceil" }, NULL, BUILT_IN_CEIL },
91 { { "java.lang.Math" }, { "cos" }, NULL, BUILT_IN_COS },
92 { { "java.lang.Math" }, { "exp" }, NULL, BUILT_IN_EXP },
93 { { "java.lang.Math" }, { "floor" }, NULL, BUILT_IN_FLOOR },
94 { { "java.lang.Math" }, { "log" }, NULL, BUILT_IN_LOG },
95 { { "java.lang.Math" }, { "pow" }, NULL, BUILT_IN_POW },
96 { { "java.lang.Math" }, { "sin" }, NULL, BUILT_IN_SIN },
97 { { "java.lang.Math" }, { "sqrt" }, NULL, BUILT_IN_SQRT },
98 { { "java.lang.Math" }, { "tan" }, NULL, BUILT_IN_TAN },
99 { { "java.lang.Float" }, { "intBitsToFloat" }, convert_real,
100 (enum built_in_function) 0 },
101 { { "java.lang.Double" }, { "longBitsToDouble" }, convert_real,
102 (enum built_in_function) 0 },
103 { { "java.lang.Float" }, { "floatToRawIntBits" }, convert_real,
104 (enum built_in_function) 0 },
105 { { "java.lang.Double" }, { "doubleToRawLongBits" }, convert_real,
106 (enum built_in_function) 0 },
107 { { "sun.misc.Unsafe" }, { "putInt" }, putObject_builtin,
108 (enum built_in_function) 0},
109 { { "sun.misc.Unsafe" }, { "putLong" }, putObject_builtin,
110 (enum built_in_function) 0},
111 { { "sun.misc.Unsafe" }, { "putObject" }, putObject_builtin,
112 (enum built_in_function) 0},
113 { { "sun.misc.Unsafe" }, { "compareAndSwapInt" },
114 compareAndSwapInt_builtin, (enum built_in_function) 0},
115 { { "sun.misc.Unsafe" }, { "compareAndSwapLong" },
116 compareAndSwapLong_builtin, (enum built_in_function) 0},
117 { { "sun.misc.Unsafe" }, { "compareAndSwapObject" },
118 compareAndSwapObject_builtin, (enum built_in_function) 0},
119 { { "sun.misc.Unsafe" }, { "putOrderedInt" }, putVolatile_builtin,
120 (enum built_in_function) 0},
121 { { "sun.misc.Unsafe" }, { "putOrderedLong" }, putVolatile_builtin,
122 (enum built_in_function) 0},
123 { { "sun.misc.Unsafe" }, { "putOrderedObject" }, putVolatile_builtin,
124 (enum built_in_function) 0},
125 { { "sun.misc.Unsafe" }, { "putIntVolatile" }, putVolatile_builtin,
126 (enum built_in_function) 0},
127 { { "sun.misc.Unsafe" }, { "putLongVolatile" }, putVolatile_builtin,
128 (enum built_in_function) 0},
129 { { "sun.misc.Unsafe" }, { "putObjectVolatile" }, putVolatile_builtin,
130 (enum built_in_function) 0},
131 { { "sun.misc.Unsafe" }, { "getObjectVolatile" }, getVolatile_builtin,
132 (enum built_in_function) 0},
133 { { "sun.misc.Unsafe" }, { "getIntVolatile" }, getVolatile_builtin,
134 (enum built_in_function) 0},
135 { { "sun.misc.Unsafe" }, { "getLongVolatile" }, getVolatile_builtin, (enum built_in_function) 0},
136 { { "sun.misc.Unsafe" }, { "getLong" }, getVolatile_builtin,
137 (enum built_in_function) 0},
138 { { "java.util.concurrent.atomic.AtomicLong" }, { "VMSupportsCS8" },
139 VMSupportsCS8_builtin, (enum built_in_function) 0},
140 { { NULL }, { NULL }, NULL, END_BUILTINS }
144 /* Internal functions which implement various builtin conversions. */
147 max_builtin (tree method_return_type, tree orig_call)
149 /* MAX_EXPR does not handle -0.0 in the Java style. */
150 if (TREE_CODE (method_return_type) == REAL_TYPE)
152 return fold_build2 (MAX_EXPR, method_return_type,
153 CALL_EXPR_ARG (orig_call, 0),
154 CALL_EXPR_ARG (orig_call, 1));
158 min_builtin (tree method_return_type, tree orig_call)
160 /* MIN_EXPR does not handle -0.0 in the Java style. */
161 if (TREE_CODE (method_return_type) == REAL_TYPE)
163 return fold_build2 (MIN_EXPR, method_return_type,
164 CALL_EXPR_ARG (orig_call, 0),
165 CALL_EXPR_ARG (orig_call, 1));
169 abs_builtin (tree method_return_type, tree orig_call)
171 return fold_build1 (ABS_EXPR, method_return_type,
172 CALL_EXPR_ARG (orig_call, 0));
175 /* Construct a new call to FN using the arguments from ORIG_CALL. */
178 java_build_function_call_expr (tree fn, tree orig_call)
180 int nargs = call_expr_nargs (orig_call);
183 /* Although we could handle the 0-3 argument cases using the general
184 logic in the default case, splitting them out permits folding to
185 be performed without constructing a temporary CALL_EXPR. */
187 return build_call_expr (fn, 0);
189 return build_call_expr (fn, 1, CALL_EXPR_ARG (orig_call, 0));
191 return build_call_expr (fn, 2,
192 CALL_EXPR_ARG (orig_call, 0),
193 CALL_EXPR_ARG (orig_call, 1));
195 return build_call_expr (fn, 3,
196 CALL_EXPR_ARG (orig_call, 0),
197 CALL_EXPR_ARG (orig_call, 1),
198 CALL_EXPR_ARG (orig_call, 2));
201 tree fntype = TREE_TYPE (fn);
202 fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fn);
203 return fold (build_call_array (TREE_TYPE (fntype),
204 fn, nargs, CALL_EXPR_ARGP (orig_call)));
210 convert_real (tree method_return_type, tree orig_call)
212 return build1 (VIEW_CONVERT_EXPR, method_return_type,
213 CALL_EXPR_ARG (orig_call, 0));
218 /* Provide builtin support for atomic operations. These are
219 documented at length in libjava/sun/misc/Unsafe.java. */
221 /* FIXME. There are still a few things wrong with this logic. In
222 particular, atomic writes of multi-word integers are not truly
223 atomic: this requires more work.
225 In general, double-word compare-and-swap cannot portably be
226 implemented, so we need some kind of fallback for 32-bit machines.
231 /* Macros to unmarshal arguments from a CALL_EXPR into a few
232 variables. We also convert the offset arg from a long to an
233 integer that is the same size as a pointer. */
235 #define UNMARSHAL3(METHOD_CALL) \
236 tree this_arg, obj_arg, offset_arg; \
239 tree orig_method_call = METHOD_CALL; \
240 this_arg = CALL_EXPR_ARG (orig_method_call, 0); \
241 obj_arg = CALL_EXPR_ARG (orig_method_call, 1); \
242 offset_arg = fold_convert (java_type_for_size (POINTER_SIZE, 0), \
243 CALL_EXPR_ARG (orig_method_call, 2)); \
247 #define UNMARSHAL4(METHOD_CALL) \
248 tree value_type, this_arg, obj_arg, offset_arg, value_arg; \
251 tree orig_method_call = METHOD_CALL; \
252 this_arg = CALL_EXPR_ARG (orig_method_call, 0); \
253 obj_arg = CALL_EXPR_ARG (orig_method_call, 1); \
254 offset_arg = fold_convert (java_type_for_size (POINTER_SIZE, 0), \
255 CALL_EXPR_ARG (orig_method_call, 2)); \
256 value_arg = CALL_EXPR_ARG (orig_method_call, 3); \
257 value_type = TREE_TYPE (value_arg); \
261 #define UNMARSHAL5(METHOD_CALL) \
262 tree value_type, this_arg, obj_arg, offset_arg, expected_arg, value_arg; \
265 tree orig_method_call = METHOD_CALL; \
266 this_arg = CALL_EXPR_ARG (orig_method_call, 0); \
267 obj_arg = CALL_EXPR_ARG (orig_method_call, 1); \
268 offset_arg = fold_convert (java_type_for_size (POINTER_SIZE, 0), \
269 CALL_EXPR_ARG (orig_method_call, 2)); \
270 expected_arg = CALL_EXPR_ARG (orig_method_call, 3); \
271 value_arg = CALL_EXPR_ARG (orig_method_call, 4); \
272 value_type = TREE_TYPE (value_arg); \
276 /* Add an address to an offset, forming a sum. */
279 build_addr_sum (tree type, tree addr, tree offset)
281 tree ptr_type = build_pointer_type (type);
282 return fold_build2 (POINTER_PLUS_EXPR,
284 fold_convert (ptr_type, addr),
285 fold_convert (sizetype, offset));
288 /* Make sure that this-arg is non-NULL. This is a security check. */
291 build_check_this (tree stmt, tree this_arg)
293 return build2 (COMPOUND_EXPR, TREE_TYPE (stmt),
294 java_check_reference (this_arg, 1), stmt);
297 /* Now the builtins. These correspond to the primitive functions in
298 libjava/sun/misc/natUnsafe.cc. */
301 putObject_builtin (tree method_return_type ATTRIBUTE_UNUSED,
305 UNMARSHAL4 (orig_call);
307 addr = build_addr_sum (value_type, obj_arg, offset_arg);
308 stmt = fold_build2 (MODIFY_EXPR, value_type,
309 build_java_indirect_ref (value_type, addr,
310 flag_check_references),
313 return build_check_this (stmt, this_arg);
317 compareAndSwapInt_builtin (tree method_return_type ATTRIBUTE_UNUSED,
320 enum machine_mode mode = TYPE_MODE (int_type_node);
321 if (sync_compare_and_swap[mode] != CODE_FOR_nothing
322 || flag_use_atomic_builtins)
325 UNMARSHAL5 (orig_call);
326 (void) value_type; /* Avoid set but not used warning. */
328 addr = build_addr_sum (int_type_node, obj_arg, offset_arg);
329 stmt = build_call_expr (built_in_decls[BUILT_IN_BOOL_COMPARE_AND_SWAP_4],
330 3, addr, expected_arg, value_arg);
332 return build_check_this (stmt, this_arg);
338 compareAndSwapLong_builtin (tree method_return_type ATTRIBUTE_UNUSED,
341 enum machine_mode mode = TYPE_MODE (long_type_node);
342 if (sync_compare_and_swap[mode] != CODE_FOR_nothing
343 || (GET_MODE_SIZE (mode) <= GET_MODE_SIZE (word_mode)
344 && flag_use_atomic_builtins))
345 /* We don't trust flag_use_atomic_builtins for multi-word
346 compareAndSwap. Some machines such as ARM have atomic libfuncs
347 but not the multi-word versions. */
350 UNMARSHAL5 (orig_call);
351 (void) value_type; /* Avoid set but not used warning. */
353 addr = build_addr_sum (long_type_node, obj_arg, offset_arg);
354 stmt = build_call_expr (built_in_decls[BUILT_IN_BOOL_COMPARE_AND_SWAP_8],
355 3, addr, expected_arg, value_arg);
357 return build_check_this (stmt, this_arg);
362 compareAndSwapObject_builtin (tree method_return_type ATTRIBUTE_UNUSED,
365 enum machine_mode mode = TYPE_MODE (ptr_type_node);
366 if (sync_compare_and_swap[mode] != CODE_FOR_nothing
367 || flag_use_atomic_builtins)
372 UNMARSHAL5 (orig_call);
373 builtin = (POINTER_SIZE == 32
374 ? BUILT_IN_BOOL_COMPARE_AND_SWAP_4
375 : BUILT_IN_BOOL_COMPARE_AND_SWAP_8);
377 addr = build_addr_sum (value_type, obj_arg, offset_arg);
378 stmt = build_call_expr (built_in_decls[builtin],
379 3, addr, expected_arg, value_arg);
381 return build_check_this (stmt, this_arg);
387 putVolatile_builtin (tree method_return_type ATTRIBUTE_UNUSED,
390 tree addr, stmt, modify_stmt;
391 UNMARSHAL4 (orig_call);
393 addr = build_addr_sum (value_type, obj_arg, offset_arg);
395 = fold_convert (build_pointer_type (build_type_variant (value_type, 0, 1)),
398 stmt = build_call_expr (built_in_decls[BUILT_IN_SYNCHRONIZE], 0);
399 modify_stmt = fold_build2 (MODIFY_EXPR, value_type,
400 build_java_indirect_ref (value_type, addr,
401 flag_check_references),
403 stmt = build2 (COMPOUND_EXPR, TREE_TYPE (modify_stmt),
406 return build_check_this (stmt, this_arg);
410 getVolatile_builtin (tree method_return_type ATTRIBUTE_UNUSED,
413 tree addr, stmt, modify_stmt, tmp;
414 UNMARSHAL3 (orig_call);
415 (void) this_arg; /* Avoid set but not used warning. */
417 addr = build_addr_sum (method_return_type, obj_arg, offset_arg);
419 = fold_convert (build_pointer_type (build_type_variant
420 (method_return_type, 0, 1)), addr);
422 stmt = build_call_expr (built_in_decls[BUILT_IN_SYNCHRONIZE], 0);
424 tmp = build_decl (BUILTINS_LOCATION, VAR_DECL, NULL, method_return_type);
425 DECL_IGNORED_P (tmp) = 1;
426 DECL_ARTIFICIAL (tmp) = 1;
429 modify_stmt = fold_build2 (MODIFY_EXPR, method_return_type,
431 build_java_indirect_ref (method_return_type, addr,
432 flag_check_references));
434 stmt = build2 (COMPOUND_EXPR, void_type_node, modify_stmt, stmt);
435 stmt = build2 (COMPOUND_EXPR, method_return_type, stmt, tmp);
441 VMSupportsCS8_builtin (tree method_return_type,
442 tree orig_call ATTRIBUTE_UNUSED)
444 enum machine_mode mode = TYPE_MODE (long_type_node);
445 gcc_assert (method_return_type == boolean_type_node);
446 if (sync_compare_and_swap[mode] != CODE_FOR_nothing)
447 return boolean_true_node;
449 return boolean_false_node;
454 #define BUILTIN_NOTHROW 1
455 #define BUILTIN_CONST 2
456 /* Define a single builtin. */
458 define_builtin (enum built_in_function val,
466 decl = build_decl (BUILTINS_LOCATION,
467 FUNCTION_DECL, get_identifier (name), type);
468 DECL_EXTERNAL (decl) = 1;
469 TREE_PUBLIC (decl) = 1;
470 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (libname));
472 DECL_BUILT_IN_CLASS (decl) = BUILT_IN_NORMAL;
473 DECL_FUNCTION_CODE (decl) = val;
474 if (flags & BUILTIN_NOTHROW)
475 TREE_NOTHROW (decl) = 1;
476 if (flags & BUILTIN_CONST)
477 TREE_READONLY (decl) = 1;
479 implicit_built_in_decls[val] = decl;
480 built_in_decls[val] = decl;
485 /* Initialize the builtins. */
487 initialize_builtins (void)
489 tree double_ftype_double, double_ftype_double_double;
490 tree float_ftype_float_float;
491 tree boolean_ftype_boolean_boolean;
495 for (i = 0; java_builtins[i].builtin_code != END_BUILTINS; ++i)
497 tree klass_id = get_identifier (java_builtins[i].class_name.s);
498 tree m = get_identifier (java_builtins[i].method_name.s);
500 java_builtins[i].class_name.t = klass_id;
501 java_builtins[i].method_name.t = m;
504 void_list_node = end_params_node;
506 t = tree_cons (NULL_TREE, float_type_node, end_params_node);
507 t = tree_cons (NULL_TREE, float_type_node, t);
508 float_ftype_float_float = build_function_type (float_type_node, t);
510 t = tree_cons (NULL_TREE, double_type_node, end_params_node);
511 double_ftype_double = build_function_type (double_type_node, t);
512 t = tree_cons (NULL_TREE, double_type_node, t);
513 double_ftype_double_double = build_function_type (double_type_node, t);
515 define_builtin (BUILT_IN_FMOD, "__builtin_fmod",
516 double_ftype_double_double, "fmod", BUILTIN_CONST);
517 define_builtin (BUILT_IN_FMODF, "__builtin_fmodf",
518 float_ftype_float_float, "fmodf", BUILTIN_CONST);
520 define_builtin (BUILT_IN_ACOS, "__builtin_acos",
521 double_ftype_double, "_ZN4java4lang4Math4acosEJdd",
523 define_builtin (BUILT_IN_ASIN, "__builtin_asin",
524 double_ftype_double, "_ZN4java4lang4Math4asinEJdd",
526 define_builtin (BUILT_IN_ATAN, "__builtin_atan",
527 double_ftype_double, "_ZN4java4lang4Math4atanEJdd",
529 define_builtin (BUILT_IN_ATAN2, "__builtin_atan2",
530 double_ftype_double_double, "_ZN4java4lang4Math5atan2EJddd",
532 define_builtin (BUILT_IN_CEIL, "__builtin_ceil",
533 double_ftype_double, "_ZN4java4lang4Math4ceilEJdd",
535 define_builtin (BUILT_IN_COS, "__builtin_cos",
536 double_ftype_double, "_ZN4java4lang4Math3cosEJdd",
538 define_builtin (BUILT_IN_EXP, "__builtin_exp",
539 double_ftype_double, "_ZN4java4lang4Math3expEJdd",
541 define_builtin (BUILT_IN_FLOOR, "__builtin_floor",
542 double_ftype_double, "_ZN4java4lang4Math5floorEJdd",
544 define_builtin (BUILT_IN_LOG, "__builtin_log",
545 double_ftype_double, "_ZN4java4lang4Math3logEJdd",
547 define_builtin (BUILT_IN_POW, "__builtin_pow",
548 double_ftype_double_double, "_ZN4java4lang4Math3powEJddd",
550 define_builtin (BUILT_IN_SIN, "__builtin_sin",
551 double_ftype_double, "_ZN4java4lang4Math3sinEJdd",
553 define_builtin (BUILT_IN_SQRT, "__builtin_sqrt",
554 double_ftype_double, "_ZN4java4lang4Math4sqrtEJdd",
556 define_builtin (BUILT_IN_TAN, "__builtin_tan",
557 double_ftype_double, "_ZN4java4lang4Math3tanEJdd",
560 t = tree_cons (NULL_TREE, boolean_type_node, end_params_node);
561 t = tree_cons (NULL_TREE, boolean_type_node, t);
562 boolean_ftype_boolean_boolean = build_function_type (boolean_type_node, t);
563 define_builtin (BUILT_IN_EXPECT, "__builtin_expect",
564 boolean_ftype_boolean_boolean,
566 BUILTIN_CONST | BUILTIN_NOTHROW);
567 define_builtin (BUILT_IN_BOOL_COMPARE_AND_SWAP_4,
568 "__sync_bool_compare_and_swap_4",
569 build_function_type_list (boolean_type_node,
571 build_pointer_type (int_type_node),
572 int_type_node, NULL_TREE),
573 "__sync_bool_compare_and_swap_4", 0);
574 define_builtin (BUILT_IN_BOOL_COMPARE_AND_SWAP_8,
575 "__sync_bool_compare_and_swap_8",
576 build_function_type_list (boolean_type_node,
578 build_pointer_type (long_type_node),
579 int_type_node, NULL_TREE),
580 "__sync_bool_compare_and_swap_8", 0);
581 define_builtin (BUILT_IN_SYNCHRONIZE, "__sync_synchronize",
582 build_function_type (void_type_node, void_list_node),
583 "__sync_synchronize", BUILTIN_NOTHROW);
585 define_builtin (BUILT_IN_RETURN_ADDRESS, "__builtin_return_address",
586 build_function_type_list (ptr_type_node, int_type_node, NULL_TREE),
587 "__builtin_return_address", BUILTIN_NOTHROW);
589 build_common_builtin_nodes ();
592 /* If the call matches a builtin, return the
593 appropriate builtin expression instead. */
595 check_for_builtin (tree method, tree call)
597 if (optimize && TREE_CODE (call) == CALL_EXPR)
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));
604 for (i = 0; java_builtins[i].builtin_code != END_BUILTINS; ++i)
606 if (method_class == java_builtins[i].class_name.t
607 && method_name == java_builtins[i].method_name.t)
611 if (java_builtins[i].creator != NULL)
614 = (*java_builtins[i].creator) (method_return_type, call);
615 return result == NULL_TREE ? call : result;
618 /* Builtin functions emit a direct call which is incompatible
620 if (flag_indirect_dispatch)
622 fn = built_in_decls[java_builtins[i].builtin_code];
625 return java_build_function_call_expr (fn, call);
632 #include "gt-java-builtins.h"