use of com.oracle.truffle.api.frame.FrameSlot in project graal by oracle.
the class DefaultScope method getVariables.
private static Object getVariables(RootNode root, Frame frame) {
List<? extends FrameSlot> slots;
if (frame == null) {
slots = root.getFrameDescriptor().getSlots();
} else {
slots = frame.getFrameDescriptor().getSlots();
// Filter out slots with null values:
List<FrameSlot> nonNulls = null;
int lastI = 0;
for (int i = 0; i < slots.size(); i++) {
FrameSlot slot = slots.get(i);
if (frame.getValue(slot) == null || isInternal(slot)) {
if (nonNulls == null) {
nonNulls = new ArrayList<>(slots.size());
}
nonNulls.addAll(slots.subList(lastI, i));
lastI = i + 1;
}
}
if (nonNulls != null) {
if (lastI < slots.size()) {
nonNulls.addAll(slots.subList(lastI, slots.size()));
}
slots = nonNulls;
}
}
Map<String, FrameSlot> slotsMap;
if (slots.isEmpty()) {
slotsMap = Collections.emptyMap();
} else if (slots.size() == 1) {
FrameSlot slot = slots.get(0);
slotsMap = Collections.singletonMap(Objects.toString(slot.getIdentifier()), slot);
} else {
slotsMap = new LinkedHashMap<>(slots.size());
for (FrameSlot slot : slots) {
slotsMap.put(Objects.toString(slot.getIdentifier()), slot);
}
}
return new VariablesMapObject(slotsMap, frame);
}
use of com.oracle.truffle.api.frame.FrameSlot in project graal by oracle.
the class SLHelloEqualsWorldBuiltin method change.
@Specialization
@TruffleBoundary
public String change() {
FrameInstance frameInstance = Truffle.getRuntime().getCallerFrame();
Frame frame = frameInstance.getFrame(FrameAccess.READ_WRITE);
FrameSlot slot = frame.getFrameDescriptor().findOrAddFrameSlot("hello");
frame.setObject(slot, "world");
return "world";
}
use of com.oracle.truffle.api.frame.FrameSlot in project graal by oracle.
the class SLNodeFactory method createRead.
/**
* Returns a {@link SLReadLocalVariableNode} if this read is a local variable or a
* {@link SLFunctionLiteralNode} if this read is global. In SL, the only global names are
* functions.
*
* @param nameNode The name of the variable/function being read
* @return either:
* <ul>
* <li>A SLReadLocalVariableNode representing the local variable being read.</li>
* <li>A SLFunctionLiteralNode representing the function definition.</li>
* <li>null if nameNode is null.</li>
* </ul>
*/
public SLExpressionNode createRead(SLExpressionNode nameNode) {
if (nameNode == null) {
return null;
}
String name = ((SLStringLiteralNode) nameNode).executeGeneric(null);
final SLExpressionNode result;
final FrameSlot frameSlot = lexicalScope.locals.get(name);
if (frameSlot != null) {
/* Read of a local variable. */
result = SLReadLocalVariableNodeGen.create(frameSlot);
} else {
/* Read of a global name. In our language, the only global names are functions. */
result = new SLFunctionLiteralNode(language, name);
}
result.setSourceSection(nameNode.getSourceCharIndex(), nameNode.getSourceLength());
return result;
}
use of com.oracle.truffle.api.frame.FrameSlot in project graal by oracle.
the class SLNodeFactory method createAssignment.
/**
* Returns an {@link SLWriteLocalVariableNode} for the given parameters.
*
* @param nameNode The name of the variable being assigned
* @param valueNode The value to be assigned
* @return An SLExpressionNode for the given parameters. null if nameNode or valueNode is null.
*/
public SLExpressionNode createAssignment(SLExpressionNode nameNode, SLExpressionNode valueNode) {
if (nameNode == null || valueNode == null) {
return null;
}
String name = ((SLStringLiteralNode) nameNode).executeGeneric(null);
FrameSlot frameSlot = frameDescriptor.findOrAddFrameSlot(name);
lexicalScope.locals.put(name, frameSlot);
final SLExpressionNode result = SLWriteLocalVariableNodeGen.create(valueNode, frameSlot);
if (valueNode.hasSource()) {
final int start = nameNode.getSourceCharIndex();
final int length = valueNode.getSourceEndIndex() - start;
result.setSourceSection(start, length);
}
return result;
}
use of com.oracle.truffle.api.frame.FrameSlot in project graal by oracle.
the class SLLexicalScope method collectArgs.
private static Map<String, FrameSlot> collectArgs(Node block) {
// Arguments are pushed to frame slots at the beginning of the function block.
// To collect argument slots, search for SLReadArgumentNode inside of
// SLWriteLocalVariableNode.
Map<String, FrameSlot> args = new LinkedHashMap<>(4);
NodeUtil.forEachChild(block, new NodeVisitor() {
// The current write node containing a slot
private SLWriteLocalVariableNode wn;
@Override
public boolean visit(Node node) {
// When there is a write node, search for SLReadArgumentNode among its children:
if (node instanceof SLWriteLocalVariableNode) {
wn = (SLWriteLocalVariableNode) node;
boolean all = NodeUtil.forEachChild(node, this);
wn = null;
return all;
} else if (wn != null && (node instanceof SLReadArgumentNode)) {
FrameSlot slot = wn.getSlot();
String name = Objects.toString(slot.getIdentifier());
assert !args.containsKey(name) : name + " argument exists already.";
args.put(name, slot);
return true;
} else if (wn == null && (node instanceof SLStatementNode)) {
// A different SL node - we're done.
return false;
} else {
return NodeUtil.forEachChild(node, this);
}
}
});
return args;
}
Aggregations