Package com.oracle.truffle.api.nodes
Class EncapsulatingNodeReference
java.lang.Object
com.oracle.truffle.api.nodes.EncapsulatingNodeReference
Thread local reference class to remember the current encapsulating node of an interpreter on the
stack. The current encapsulating node is used whenever the code transitions from cached to
uncached cases. This typically happens when the maximum limit for an inline cache was reached and
the uncached version of a node will be used behind a
CompilerDirectives.TruffleBoundary
. To correctly
produce exception stack traces it is necessary to remember the node that transitioned to the
uncached case. Uncached calls to CallTarget
will automatically use the value of the
encapsulating node. Truffle DSL produces such transitions automatically if Truffle libraries are
used and they transition to uncached.
Usage example for pushing and restoring the current node to the stack.
EncapsulatingNodeReference encapsulating = EncapsulatingNodeReference.getCurrent(); Node encapsulatingNode = encapsulating.set(currentNode); try { // follow uncached path } finally { encapsulating.set(encapsulatingNode); }
The current encapsulating node can also be useful when constructing guest exceptions in slow-paths and the location of the error needs to be found out reliably. Usage example:
final class LanguageException extends AbstractTruffleException { private LanguageException(Node locationNode) { super(locationNode); } static LanguageException create(Node locationNode) { CompilerAsserts.partialEvaluationConstant(locationNode); if (locationNode == null || !locationNode.isAdoptable()) { return new LanguageException(EncapsulatingNodeReference.getCurrent().get()); } else { return new LanguageException(locationNode); } } // ... }Note that
Node.isAdoptable()
is a way to find out whether a node was used in an uncached
scenario or not.- Since:
- 20.2
-
Method Summary
Modifier and TypeMethodDescriptionget()
Returns the current encapsulating node for the current thread.static EncapsulatingNodeReference
Returns the encapsulating node reference of thecurrent
thread.Sets the current encapsulating node for the current thread and returns its previous value.
-
Method Details
-
set
Sets the current encapsulating node for the current thread and returns its previous value. The passed node may benull
. The set node must beadoptable
and be adopted by aRootNode
otherwise anAssertionError
is thrown. This method must only be used from the thread it wasrequested
for. This method is safe to be called from compiled code paths.- Since:
- 20.2
-
get
Returns the current encapsulating node for the current thread. Returned node may benull
. If the returned node is non-null then it is guaranteed to beadoptable
and adopted by aRootNode
. This method must only be used from the thread it wasrequested
for. This method is safe to be called from compiled code paths.- Since:
- 20.2
-
getCurrent
Returns the encapsulating node reference of thecurrent
thread. The returned value is nevernull
. This method is safe to be called from compiled code paths.- Since:
- 20.2
-