use of com.oracle.truffle.api.nodes.Node in project graal by oracle.
the class SLGraalRuntimeBuiltin method findCallsTo.
/**
* Finds all {@link DirectCallNode} instances calling a certain original {@link CallTarget} in a
* given {@link RootNode}.
*/
@TruffleBoundary
protected static final Set<DirectCallNode> findCallsTo(RootNode root, OptimizedCallTarget originalCallTarget) {
final Set<DirectCallNode> allCallNodes = new HashSet<>();
root.accept(new NodeVisitor() {
@Override
public boolean visit(Node node) {
if (node instanceof DirectCallNode) {
DirectCallNode callNode = (DirectCallNode) node;
if (callNode.getCallTarget() == originalCallTarget || callNode.getClonedCallTarget() == originalCallTarget) {
allCallNodes.add(callNode);
}
}
return true;
}
});
return allCallNodes;
}
use of com.oracle.truffle.api.nodes.Node in project graal by oracle.
the class SLInstrumentTest method testLexicalScopes.
@Test
public void testLexicalScopes() throws Exception {
String code = "function test(n) {\n" + // 2
" a = 1;\n" + " if (a > 0) {\n" + " b = 10;\n" + // 5
" println(b);\n" + " }\n" + " if (a == 1) {\n" + " b = 20;\n" + " a = 0;\n" + // 10
" c = 1;\n" + " if (b > 0) {\n" + " a = 4;\n" + " b = 5;\n" + " c = 6;\n" + // 15
" d = 7;\n" + " println(d);\n" + " }\n" + " }\n" + " println(b);\n" + // 20
" println(a);\n" + "}\n" + "function main() {\n" + " test(\"n_n\");\n" + "}";
Source source = Source.newBuilder("sl", code, "testing").build();
List<Throwable> throwables;
try (Engine engine = Engine.newBuilder().out(new java.io.OutputStream() {
// null output stream
@Override
public void write(int b) throws IOException {
}
}).build()) {
Instrument envInstr = engine.getInstruments().get("testEnvironmentHandlerInstrument");
TruffleInstrument.Env env = envInstr.lookup(Environment.class).env;
throwables = new ArrayList<>();
env.getInstrumenter().attachExecutionEventListener(SourceSectionFilter.newBuilder().lineIn(1, source.getLineCount()).build(), new ExecutionEventListener() {
@Override
public void onEnter(EventContext context, VirtualFrame frame) {
Node node = context.getInstrumentedNode();
Iterable<Scope> lexicalScopes = env.findLocalScopes(node, null);
Iterable<Scope> dynamicScopes = env.findLocalScopes(node, frame);
try {
verifyLexicalScopes(lexicalScopes, dynamicScopes, context.getInstrumentedSourceSection().getStartLine(), frame.materialize());
} catch (ThreadDeath t) {
throw t;
} catch (Throwable t) {
CompilerDirectives.transferToInterpreter();
PrintStream lsErr = System.err;
lsErr.println("Line = " + context.getInstrumentedSourceSection().getStartLine());
lsErr.println("Node = " + node + ", class = " + node.getClass().getName());
t.printStackTrace(lsErr);
throwables.add(t);
}
}
@Override
public void onReturnValue(EventContext context, VirtualFrame frame, Object result) {
}
@Override
public void onReturnExceptional(EventContext context, VirtualFrame frame, Throwable exception) {
}
});
Context.newBuilder().engine(engine).build().eval(source);
}
assertTrue(throwables.toString(), throwables.isEmpty());
}
use of com.oracle.truffle.api.nodes.Node in project graal by oracle.
the class SLLexicalScope method getParentBlock.
private static SLBlockNode getParentBlock(Node node) {
SLBlockNode block;
Node parent = node.getParent();
// Find a nearest block node.
while (parent != null && !(parent instanceof SLBlockNode)) {
parent = parent.getParent();
}
if (parent != null) {
block = (SLBlockNode) parent;
} else {
block = null;
}
return block;
}
use of com.oracle.truffle.api.nodes.Node in project graal by oracle.
the class SLLexicalScope method findChildrenBlock.
private static SLBlockNode findChildrenBlock(Node node) {
SLBlockNode[] blockPtr = new SLBlockNode[1];
node.accept(new NodeVisitor() {
@Override
public boolean visit(Node n) {
if (n instanceof SLBlockNode) {
blockPtr[0] = (SLBlockNode) n;
return false;
} else {
return true;
}
}
});
return blockPtr[0];
}
use of com.oracle.truffle.api.nodes.Node in project graal by oracle.
the class SLLexicalScope method collectVars.
private Map<String, FrameSlot> collectVars(Node varsBlock, Node currentNode) {
// Variables are slot-based.
// To collect declared variables, traverse the block's AST and find slots associated
// with SLWriteLocalVariableNode. The traversal stops when we hit the current node.
Map<String, FrameSlot> slots = new LinkedHashMap<>(4);
NodeUtil.forEachChild(varsBlock, new NodeVisitor() {
@Override
public boolean visit(Node node) {
if (node == currentNode) {
return false;
}
// Do not enter any nested blocks.
if (!(node instanceof SLBlockNode)) {
boolean all = NodeUtil.forEachChild(node, this);
if (!all) {
return false;
}
}
// Write to a variable is a declaration unless it exists already in a parent scope.
if (node instanceof SLWriteLocalVariableNode) {
SLWriteLocalVariableNode wn = (SLWriteLocalVariableNode) node;
String name = Objects.toString(wn.getSlot().getIdentifier());
if (!hasParentVar(name)) {
slots.put(name, wn.getSlot());
}
}
return true;
}
});
return slots;
}
Aggregations