com.oracle.truffle.sl.nodes.local.SLReadLocalVariableNode Maven / Gradle / Ivy
Show all versions of truffle-sl Show documentation
/*
* Copyright (c) 2012, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
*
* Subject to the condition set forth below, permission is hereby granted to any
* person obtaining a copy of this software, associated documentation and/or
* data (collectively the "Software"), free of charge and under any and all
* copyright rights in the Software, and any and all patent rights owned or
* freely licensable by each licensor hereunder covering either (i) the
* unmodified Software as contributed to or provided by such licensor, or (ii)
* the Larger Works (as defined below), to deal in both
*
* (a) the Software, and
*
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
* one is included with the Software each a "Larger Work" to which the Software
* is contributed by such licensors),
*
* without restriction, including without limitation the rights to copy, create
* derivative works of, display, perform, and distribute the Software and make,
* use, sell, offer for sale, import, export, have made, and have sold the
* Software and the Larger Work(s), and to sublicense the foregoing rights on
* either these or other terms.
*
* This license is subject to the following condition:
*
* The above copyright notice and either this complete permission notice or at a
* minimum a reference to the UPL must be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.oracle.truffle.sl.nodes.local;
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.dsl.NodeField;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.frame.FrameSlot;
import com.oracle.truffle.api.frame.FrameUtil;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.instrumentation.StandardTags.ReadVariableTag;
import com.oracle.truffle.api.instrumentation.Tag;
import com.oracle.truffle.sl.nodes.SLExpressionNode;
import com.oracle.truffle.sl.nodes.interop.NodeObjectDescriptor;
/**
* Node to read a local variable from a function's {@link VirtualFrame frame}. The Truffle frame API
* allows to store primitive values of all Java primitive types, and Object values. This means that
* all SL types that are objects are handled by the {@link #readObject} method.
*
* We use the primitive type only when the same primitive type is uses for all writes. If the local
* variable is type-polymorphic, then the value is always stored as an Object, i.e., primitive
* values are boxed. Even a mixture of {@code long} and {@code boolean} writes leads to both being
* stored boxed.
*/
@NodeField(name = "slot", type = FrameSlot.class)
public abstract class SLReadLocalVariableNode extends SLExpressionNode {
/**
* Returns the descriptor of the accessed local variable. The implementation of this method is
* created by the Truffle DSL based on the {@link NodeField} annotation on the class.
*/
protected abstract FrameSlot getSlot();
@Specialization(guards = "frame.isLong(getSlot())")
protected long readLong(VirtualFrame frame) {
/*
* When the FrameSlotKind is Long, we know that only primitive long values have ever been
* written to the local variable. So we do not need to check that the frame really contains
* a primitive long value.
*/
return FrameUtil.getLongSafe(frame, getSlot());
}
@Specialization(guards = "frame.isBoolean(getSlot())")
protected boolean readBoolean(VirtualFrame frame) {
return FrameUtil.getBooleanSafe(frame, getSlot());
}
@Specialization(replaces = {"readLong", "readBoolean"})
protected Object readObject(VirtualFrame frame) {
if (!frame.isObject(getSlot())) {
/*
* The FrameSlotKind has been set to Object, so from now on all writes to the local
* variable will be Object writes. However, now we are in a frame that still has an old
* non-Object value. This is a slow-path operation: we read the non-Object value, and
* write it immediately as an Object value so that we do not hit this path again
* multiple times for the same variable of the same frame.
*/
CompilerDirectives.transferToInterpreter();
Object result = frame.getValue(getSlot());
frame.setObject(getSlot(), result);
return result;
}
return FrameUtil.getObjectSafe(frame, getSlot());
}
@Override
public boolean hasTag(Class extends Tag> tag) {
return tag == ReadVariableTag.class || super.hasTag(tag);
}
@Override
public Object getNodeObject() {
return NodeObjectDescriptor.readVariable(getSlot().getIdentifier().toString());
}
}