annotate graal/com.oracle.truffle.api/src/com/oracle/truffle/api/CompilerDirectives.java @ 11269:f00c85ee46a2

Truffle: add CompilerDirectives.inInterpreter()
author Andreas Woess <andreas.woess@jku.at>
date Thu, 08 Aug 2013 19:35:32 +0200
parents d2055a110396
children 494b818b527c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9244
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
1 /*
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
4 *
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
7 * published by the Free Software Foundation.
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
8 *
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
13 * accompanied this code).
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
14 *
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
18 *
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
20 * or visit www.oracle.com if you need additional information or have any
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
21 * questions.
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
22 */
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
23 package com.oracle.truffle.api;
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
24
9313
6369d37b37d1 Added annotation CompilerDirectives.CompilationFinal for fields that should be considered final during compilation, but are not final while executing in the Truffle interpreter.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9283
diff changeset
25 import java.lang.annotation.*;
9244
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
26 import java.util.concurrent.*;
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
27
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
28 /**
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
29 * Directives that influence the optimizations of the Truffle compiler. All of the operations have
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
30 * no effect when executed in the Truffle interpreter.
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
31 */
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
32 public class CompilerDirectives {
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
33
9319
1188b7c42196 Changed the behavior of CompilerDirectives.injectBranchProbability and added javadoc to document the new behavior. Introduced probability constants. Removed CompilerDirectives.slowpath().
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9313
diff changeset
34 public static final double LIKELY_PROBABILITY = 0.75;
1188b7c42196 Changed the behavior of CompilerDirectives.injectBranchProbability and added javadoc to document the new behavior. Introduced probability constants. Removed CompilerDirectives.slowpath().
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9313
diff changeset
35 public static final double UNLIKELY_PROBABILITY = 1.0 - LIKELY_PROBABILITY;
1188b7c42196 Changed the behavior of CompilerDirectives.injectBranchProbability and added javadoc to document the new behavior. Introduced probability constants. Removed CompilerDirectives.slowpath().
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9313
diff changeset
36
1188b7c42196 Changed the behavior of CompilerDirectives.injectBranchProbability and added javadoc to document the new behavior. Introduced probability constants. Removed CompilerDirectives.slowpath().
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9313
diff changeset
37 public static final double SLOWPATH_PROBABILITY = 0.0001;
1188b7c42196 Changed the behavior of CompilerDirectives.injectBranchProbability and added javadoc to document the new behavior. Introduced probability constants. Removed CompilerDirectives.slowpath().
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9313
diff changeset
38 public static final double FASTPATH_PROBABILITY = 1.0 - SLOWPATH_PROBABILITY;
9244
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
39
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
40 /**
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
41 * Directive for the compiler to discontinue compilation at this code position and instead
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
42 * insert a transfer to the interpreter.
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
43 */
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
44 public static void transferToInterpreter() {
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
45 }
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
46
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
47 /**
11269
f00c85ee46a2 Truffle: add CompilerDirectives.inInterpreter()
Andreas Woess <andreas.woess@jku.at>
parents: 10687
diff changeset
48 * Returns a boolean value indicating whether the method is executed in the interpreter.
f00c85ee46a2 Truffle: add CompilerDirectives.inInterpreter()
Andreas Woess <andreas.woess@jku.at>
parents: 10687
diff changeset
49 *
f00c85ee46a2 Truffle: add CompilerDirectives.inInterpreter()
Andreas Woess <andreas.woess@jku.at>
parents: 10687
diff changeset
50 * @return {@code true} when executed in the interpreter, {@code false} in compiled code.
f00c85ee46a2 Truffle: add CompilerDirectives.inInterpreter()
Andreas Woess <andreas.woess@jku.at>
parents: 10687
diff changeset
51 */
f00c85ee46a2 Truffle: add CompilerDirectives.inInterpreter()
Andreas Woess <andreas.woess@jku.at>
parents: 10687
diff changeset
52 public static boolean inInterpreter() {
f00c85ee46a2 Truffle: add CompilerDirectives.inInterpreter()
Andreas Woess <andreas.woess@jku.at>
parents: 10687
diff changeset
53 return true;
f00c85ee46a2 Truffle: add CompilerDirectives.inInterpreter()
Andreas Woess <andreas.woess@jku.at>
parents: 10687
diff changeset
54 }
f00c85ee46a2 Truffle: add CompilerDirectives.inInterpreter()
Andreas Woess <andreas.woess@jku.at>
parents: 10687
diff changeset
55
f00c85ee46a2 Truffle: add CompilerDirectives.inInterpreter()
Andreas Woess <andreas.woess@jku.at>
parents: 10687
diff changeset
56 /**
9244
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
57 * Directive for the compiler that the given runnable should only be executed in the interpreter
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
58 * and ignored in the compiled code.
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
59 *
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
60 * @param runnable the closure that should only be executed in the interpreter
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
61 */
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
62 public static void interpreterOnly(Runnable runnable) {
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
63 runnable.run();
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
64 }
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
65
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
66 /**
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
67 * Directive for the compiler that the given callable should only be executed in the
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
68 * interpreter.
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
69 *
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
70 * @param callable the closure that should only be executed in the interpreter
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
71 * @return the result of executing the closure in the interpreter and null in the compiled code
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
72 * @throws Exception If the closure throws an exception when executed in the interpreter.
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
73 */
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
74 public static <T> T interpreterOnly(Callable<T> callable) throws Exception {
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
75 return callable.call();
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
76 }
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
77
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
78 /**
9319
1188b7c42196 Changed the behavior of CompilerDirectives.injectBranchProbability and added javadoc to document the new behavior. Introduced probability constants. Removed CompilerDirectives.slowpath().
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9313
diff changeset
79 * Injects a probability for the given condition into the probability information of the
1188b7c42196 Changed the behavior of CompilerDirectives.injectBranchProbability and added javadoc to document the new behavior. Introduced probability constants. Removed CompilerDirectives.slowpath().
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9313
diff changeset
80 * immediately succeeding branch instruction for the condition. The probability must be a value
1188b7c42196 Changed the behavior of CompilerDirectives.injectBranchProbability and added javadoc to document the new behavior. Introduced probability constants. Removed CompilerDirectives.slowpath().
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9313
diff changeset
81 * between 0.0 and 1.0 (inclusive). The condition should not be a combined condition.
1188b7c42196 Changed the behavior of CompilerDirectives.injectBranchProbability and added javadoc to document the new behavior. Introduced probability constants. Removed CompilerDirectives.slowpath().
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9313
diff changeset
82 *
1188b7c42196 Changed the behavior of CompilerDirectives.injectBranchProbability and added javadoc to document the new behavior. Introduced probability constants. Removed CompilerDirectives.slowpath().
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9313
diff changeset
83 * Example usage immediately before an if statement (it specifies that the likelihood for a to
1188b7c42196 Changed the behavior of CompilerDirectives.injectBranchProbability and added javadoc to document the new behavior. Introduced probability constants. Removed CompilerDirectives.slowpath().
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9313
diff changeset
84 * be greater than b is 90%):
1188b7c42196 Changed the behavior of CompilerDirectives.injectBranchProbability and added javadoc to document the new behavior. Introduced probability constants. Removed CompilerDirectives.slowpath().
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9313
diff changeset
85 *
1188b7c42196 Changed the behavior of CompilerDirectives.injectBranchProbability and added javadoc to document the new behavior. Introduced probability constants. Removed CompilerDirectives.slowpath().
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9313
diff changeset
86 * <code>
1188b7c42196 Changed the behavior of CompilerDirectives.injectBranchProbability and added javadoc to document the new behavior. Introduced probability constants. Removed CompilerDirectives.slowpath().
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9313
diff changeset
87 * if (injectBranchProbability(0.9, a > b)) {
1188b7c42196 Changed the behavior of CompilerDirectives.injectBranchProbability and added javadoc to document the new behavior. Introduced probability constants. Removed CompilerDirectives.slowpath().
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9313
diff changeset
88 * // ...
1188b7c42196 Changed the behavior of CompilerDirectives.injectBranchProbability and added javadoc to document the new behavior. Introduced probability constants. Removed CompilerDirectives.slowpath().
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9313
diff changeset
89 * }
1188b7c42196 Changed the behavior of CompilerDirectives.injectBranchProbability and added javadoc to document the new behavior. Introduced probability constants. Removed CompilerDirectives.slowpath().
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9313
diff changeset
90 * </code>
1188b7c42196 Changed the behavior of CompilerDirectives.injectBranchProbability and added javadoc to document the new behavior. Introduced probability constants. Removed CompilerDirectives.slowpath().
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9313
diff changeset
91 *
1188b7c42196 Changed the behavior of CompilerDirectives.injectBranchProbability and added javadoc to document the new behavior. Introduced probability constants. Removed CompilerDirectives.slowpath().
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9313
diff changeset
92 * Example usage for a combined condition (it specifies that the likelihood for a to be greater
1188b7c42196 Changed the behavior of CompilerDirectives.injectBranchProbability and added javadoc to document the new behavior. Introduced probability constants. Removed CompilerDirectives.slowpath().
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9313
diff changeset
93 * than b is 90% and under the assumption that this is true, the likelihood for a being 0 is
1188b7c42196 Changed the behavior of CompilerDirectives.injectBranchProbability and added javadoc to document the new behavior. Introduced probability constants. Removed CompilerDirectives.slowpath().
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9313
diff changeset
94 * 10%):
1188b7c42196 Changed the behavior of CompilerDirectives.injectBranchProbability and added javadoc to document the new behavior. Introduced probability constants. Removed CompilerDirectives.slowpath().
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9313
diff changeset
95 *
1188b7c42196 Changed the behavior of CompilerDirectives.injectBranchProbability and added javadoc to document the new behavior. Introduced probability constants. Removed CompilerDirectives.slowpath().
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9313
diff changeset
96 * <code>
1188b7c42196 Changed the behavior of CompilerDirectives.injectBranchProbability and added javadoc to document the new behavior. Introduced probability constants. Removed CompilerDirectives.slowpath().
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9313
diff changeset
97 * if (injectBranchProbability(0.9, a > b) && injectBranchProbability(0.1, a == 0)) {
1188b7c42196 Changed the behavior of CompilerDirectives.injectBranchProbability and added javadoc to document the new behavior. Introduced probability constants. Removed CompilerDirectives.slowpath().
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9313
diff changeset
98 * // ...
1188b7c42196 Changed the behavior of CompilerDirectives.injectBranchProbability and added javadoc to document the new behavior. Introduced probability constants. Removed CompilerDirectives.slowpath().
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9313
diff changeset
99 * }
1188b7c42196 Changed the behavior of CompilerDirectives.injectBranchProbability and added javadoc to document the new behavior. Introduced probability constants. Removed CompilerDirectives.slowpath().
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9313
diff changeset
100 * </code>
1188b7c42196 Changed the behavior of CompilerDirectives.injectBranchProbability and added javadoc to document the new behavior. Introduced probability constants. Removed CompilerDirectives.slowpath().
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9313
diff changeset
101 *
1188b7c42196 Changed the behavior of CompilerDirectives.injectBranchProbability and added javadoc to document the new behavior. Introduced probability constants. Removed CompilerDirectives.slowpath().
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9313
diff changeset
102 * There are predefined constants for commonly used probabilities (see
1188b7c42196 Changed the behavior of CompilerDirectives.injectBranchProbability and added javadoc to document the new behavior. Introduced probability constants. Removed CompilerDirectives.slowpath().
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9313
diff changeset
103 * {@link #LIKELY_PROBABILITY} , {@link #UNLIKELY_PROBABILITY}, {@link #SLOWPATH_PROBABILITY},
1188b7c42196 Changed the behavior of CompilerDirectives.injectBranchProbability and added javadoc to document the new behavior. Introduced probability constants. Removed CompilerDirectives.slowpath().
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9313
diff changeset
104 * {@link #FASTPATH_PROBABILITY} ).
9244
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
105 *
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
106 * @param probability the probability value between 0.0 and 1.0 that should be injected
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
107 */
9319
1188b7c42196 Changed the behavior of CompilerDirectives.injectBranchProbability and added javadoc to document the new behavior. Introduced probability constants. Removed CompilerDirectives.slowpath().
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9313
diff changeset
108 public static boolean injectBranchProbability(double probability, boolean condition) {
9244
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
109 assert probability >= 0.0 && probability <= 1.0;
9319
1188b7c42196 Changed the behavior of CompilerDirectives.injectBranchProbability and added javadoc to document the new behavior. Introduced probability constants. Removed CompilerDirectives.slowpath().
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9313
diff changeset
110 return condition;
9244
8f540423a5be Added two new classes to the Truffle API: CompilerDirectives and CompilerAsserts.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
111 }
9282
f5e58a1eca55 Added method CompilerDirectives.bailout to the Truffle API.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9244
diff changeset
112
f5e58a1eca55 Added method CompilerDirectives.bailout to the Truffle API.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9244
diff changeset
113 /**
f5e58a1eca55 Added method CompilerDirectives.bailout to the Truffle API.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9244
diff changeset
114 * Bails out of a compilation (e.g., for guest language features that should never be compiled).
f5e58a1eca55 Added method CompilerDirectives.bailout to the Truffle API.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9244
diff changeset
115 *
f5e58a1eca55 Added method CompilerDirectives.bailout to the Truffle API.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9244
diff changeset
116 * @param reason the reason for the bailout
f5e58a1eca55 Added method CompilerDirectives.bailout to the Truffle API.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9244
diff changeset
117 */
f5e58a1eca55 Added method CompilerDirectives.bailout to the Truffle API.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9244
diff changeset
118 public static void bailout(String reason) {
f5e58a1eca55 Added method CompilerDirectives.bailout to the Truffle API.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9244
diff changeset
119 }
9313
6369d37b37d1 Added annotation CompilerDirectives.CompilationFinal for fields that should be considered final during compilation, but are not final while executing in the Truffle interpreter.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9283
diff changeset
120
6369d37b37d1 Added annotation CompilerDirectives.CompilationFinal for fields that should be considered final during compilation, but are not final while executing in the Truffle interpreter.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9283
diff changeset
121 /**
6369d37b37d1 Added annotation CompilerDirectives.CompilationFinal for fields that should be considered final during compilation, but are not final while executing in the Truffle interpreter.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9283
diff changeset
122 * Marks fields that should be considered final for a Truffle compilation although they are not
6369d37b37d1 Added annotation CompilerDirectives.CompilationFinal for fields that should be considered final during compilation, but are not final while executing in the Truffle interpreter.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9283
diff changeset
123 * final while executing in the interpreter.
6369d37b37d1 Added annotation CompilerDirectives.CompilationFinal for fields that should be considered final during compilation, but are not final while executing in the Truffle interpreter.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9283
diff changeset
124 */
6369d37b37d1 Added annotation CompilerDirectives.CompilationFinal for fields that should be considered final during compilation, but are not final while executing in the Truffle interpreter.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9283
diff changeset
125 @Retention(RetentionPolicy.RUNTIME)
6369d37b37d1 Added annotation CompilerDirectives.CompilationFinal for fields that should be considered final during compilation, but are not final while executing in the Truffle interpreter.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9283
diff changeset
126 @Target({ElementType.FIELD})
6369d37b37d1 Added annotation CompilerDirectives.CompilationFinal for fields that should be considered final during compilation, but are not final while executing in the Truffle interpreter.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9283
diff changeset
127 public @interface CompilationFinal {
6369d37b37d1 Added annotation CompilerDirectives.CompilationFinal for fields that should be considered final during compilation, but are not final while executing in the Truffle interpreter.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9283
diff changeset
128 }
9349
0fccad3ce40d Introduce CompilerDirectives.Unsafe. Mark Frame.getArguments method as unsafe.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9319
diff changeset
129
0fccad3ce40d Introduce CompilerDirectives.Unsafe. Mark Frame.getArguments method as unsafe.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9319
diff changeset
130 /**
0fccad3ce40d Introduce CompilerDirectives.Unsafe. Mark Frame.getArguments method as unsafe.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9319
diff changeset
131 * Marks methods that are considered unsafe. Wrong usage of those methods can lead to unexpected
0fccad3ce40d Introduce CompilerDirectives.Unsafe. Mark Frame.getArguments method as unsafe.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9319
diff changeset
132 * behavior including a crash of the runtime. Therefore, special care should be taken.
0fccad3ce40d Introduce CompilerDirectives.Unsafe. Mark Frame.getArguments method as unsafe.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9319
diff changeset
133 */
0fccad3ce40d Introduce CompilerDirectives.Unsafe. Mark Frame.getArguments method as unsafe.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9319
diff changeset
134 @Retention(RetentionPolicy.RUNTIME)
0fccad3ce40d Introduce CompilerDirectives.Unsafe. Mark Frame.getArguments method as unsafe.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9319
diff changeset
135 @Target({ElementType.METHOD})
0fccad3ce40d Introduce CompilerDirectives.Unsafe. Mark Frame.getArguments method as unsafe.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9319
diff changeset
136 public @interface Unsafe {
0fccad3ce40d Introduce CompilerDirectives.Unsafe. Mark Frame.getArguments method as unsafe.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9319
diff changeset
137 }
10649
ac8b195fd3aa New unsafe cast CompilerDirectives method in Truffle API.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9349
diff changeset
138
ac8b195fd3aa New unsafe cast CompilerDirectives method in Truffle API.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9349
diff changeset
139 /**
ac8b195fd3aa New unsafe cast CompilerDirectives method in Truffle API.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9349
diff changeset
140 * Treats the given value as a value of the given class. The class must evaluate to a constant.
ac8b195fd3aa New unsafe cast CompilerDirectives method in Truffle API.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9349
diff changeset
141 *
ac8b195fd3aa New unsafe cast CompilerDirectives method in Truffle API.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9349
diff changeset
142 * @param value the value that is known to have the specified type
ac8b195fd3aa New unsafe cast CompilerDirectives method in Truffle API.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9349
diff changeset
143 * @param clazz the specified type of the value
ac8b195fd3aa New unsafe cast CompilerDirectives method in Truffle API.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9349
diff changeset
144 * @return the value
ac8b195fd3aa New unsafe cast CompilerDirectives method in Truffle API.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9349
diff changeset
145 */
10650
b6e46324233f Make CompilerDirectives.unsafeCast a generic method.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 10649
diff changeset
146 @SuppressWarnings("unchecked")
10649
ac8b195fd3aa New unsafe cast CompilerDirectives method in Truffle API.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9349
diff changeset
147 @Unsafe
10650
b6e46324233f Make CompilerDirectives.unsafeCast a generic method.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 10649
diff changeset
148 public static <T> T unsafeCast(Object value, Class<T> clazz) {
b6e46324233f Make CompilerDirectives.unsafeCast a generic method.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 10649
diff changeset
149 return (T) value;
10649
ac8b195fd3aa New unsafe cast CompilerDirectives method in Truffle API.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9349
diff changeset
150 }
10687
d2055a110396 Introduce CompilerDirectives.SlowPath annotation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 10650
diff changeset
151
d2055a110396 Introduce CompilerDirectives.SlowPath annotation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 10650
diff changeset
152 /**
d2055a110396 Introduce CompilerDirectives.SlowPath annotation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 10650
diff changeset
153 * Marks methods that are considered slowpath and should therefore not be inlined by default.
d2055a110396 Introduce CompilerDirectives.SlowPath annotation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 10650
diff changeset
154 */
d2055a110396 Introduce CompilerDirectives.SlowPath annotation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 10650
diff changeset
155 @Retention(RetentionPolicy.RUNTIME)
d2055a110396 Introduce CompilerDirectives.SlowPath annotation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 10650
diff changeset
156 @Target({ElementType.METHOD})
d2055a110396 Introduce CompilerDirectives.SlowPath annotation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 10650
diff changeset
157 public @interface SlowPath {
d2055a110396 Introduce CompilerDirectives.SlowPath annotation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 10650
diff changeset
158 }
9283
159ac409c27a Fix new line at end of file.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9282
diff changeset
159 }