Search in sources :

Example 1 with Scope

use of com.oracle.truffle.api.Scope in project graal by oracle.

the class DebugStackFrame method getScope.

/**
 * Get the current inner-most scope. The scope remain valid as long as the current stack frame
 * remains valid.
 * <p>
 * Use {@link DebuggerSession#getTopScope(java.lang.String)} to get scopes with global validity.
 * <p>
 * This method is not thread-safe and will throw an {@link IllegalStateException} if called on
 * another thread than it was created with.
 *
 * @return the scope, or <code>null</code> when no language is associated with this frame
 *         location, or when no local scope exists.
 * @since 0.26
 */
public DebugScope getScope() {
    verifyValidState(false);
    SuspendedContext context = getContext();
    RootNode root = findCurrentRoot();
    if (root == null) {
        return null;
    }
    Node node;
    if (currentFrame == null) {
        node = context.getInstrumentedNode();
    } else {
        node = currentFrame.getCallNode();
    }
    if (node.getRootNode().getLanguageInfo() == null) {
        // no language, no scopes
        return null;
    }
    Debugger debugger = event.getSession().getDebugger();
    MaterializedFrame frame = findTruffleFrame();
    Iterable<Scope> scopes = debugger.getEnv().findLocalScopes(node, frame);
    Iterator<Scope> it = scopes.iterator();
    if (!it.hasNext()) {
        return null;
    }
    return new DebugScope(it.next(), it, debugger, event, frame, root);
}
Also used : RootNode(com.oracle.truffle.api.nodes.RootNode) Scope(com.oracle.truffle.api.Scope) Node(com.oracle.truffle.api.nodes.Node) RootNode(com.oracle.truffle.api.nodes.RootNode) MaterializedFrame(com.oracle.truffle.api.frame.MaterializedFrame)

Example 2 with Scope

use of com.oracle.truffle.api.Scope in project graal by oracle.

the class SLInstrumentTest method verifyLexicalScopes.

@CompilerDirectives.TruffleBoundary
private static void verifyLexicalScopes(Iterable<Scope> lexicalScopes, Iterable<Scope> dynamicScopes, int line, MaterializedFrame frame) {
    int depth = 0;
    switch(line) {
        case 1:
            break;
        case 2:
            for (Scope ls : lexicalScopes) {
                // Test that ls.getNode() returns the current root node:
                checkRootNode(ls, "test", frame);
                TruffleObject arguments = (TruffleObject) ls.getArguments();
                checkVars(arguments, "n", null);
                TruffleObject variables = (TruffleObject) ls.getVariables();
                checkVars(variables, "n", null);
                depth++;
            }
            assertEquals("LexicalScope depth", 1, depth);
            depth = 0;
            for (Scope ls : dynamicScopes) {
                // Test that ls.getNode() returns the current root node:
                checkRootNode(ls, "test", frame);
                TruffleObject arguments = (TruffleObject) ls.getArguments();
                checkVars(arguments, "n", "n_n");
                TruffleObject variables = (TruffleObject) ls.getVariables();
                checkVars(variables, "n", "n_n");
                depth++;
            }
            assertEquals("DynamicScope depth", 1, depth);
            break;
        case 3:
        case 7:
        case 19:
        case 20:
            for (Scope ls : lexicalScopes) {
                checkRootNode(ls, "test", frame);
                TruffleObject arguments = (TruffleObject) ls.getArguments();
                checkVars(arguments, "n", null);
                TruffleObject variables = (TruffleObject) ls.getVariables();
                checkVars(variables, "n", null, "a", null);
                depth++;
            }
            assertEquals("LexicalScope depth", 1, depth);
            depth = 0;
            for (Scope ls : dynamicScopes) {
                checkRootNode(ls, "test", frame);
                TruffleObject arguments = (TruffleObject) ls.getArguments();
                checkVars(arguments, "n", "n_n");
                TruffleObject variables = (TruffleObject) ls.getVariables();
                long aVal = (line < 19) ? 1L : 4L;
                checkVars(variables, "n", "n_n", "a", aVal);
                depth++;
            }
            assertEquals("DynamicScope depth", 1, depth);
            break;
        case 4:
        case 8:
            for (Scope ls : lexicalScopes) {
                if (depth == 0) {
                    checkBlock(ls);
                    TruffleObject variables = (TruffleObject) ls.getVariables();
                    checkVars(variables);
                    assertNull(ls.getArguments());
                } else {
                    checkRootNode(ls, "test", frame);
                    TruffleObject variables = (TruffleObject) ls.getVariables();
                    checkVars(variables, "n", null, "a", null);
                    TruffleObject arguments = (TruffleObject) ls.getArguments();
                    checkVars(arguments, "n", null);
                }
                depth++;
            }
            assertEquals("LexicalScope depth", 2, depth);
            depth = 0;
            for (Scope ls : dynamicScopes) {
                if (depth == 0) {
                    checkBlock(ls);
                    TruffleObject variables = (TruffleObject) ls.getVariables();
                    checkVars(variables);
                    assertNull(ls.getArguments());
                } else {
                    checkRootNode(ls, "test", frame);
                    TruffleObject variables = (TruffleObject) ls.getVariables();
                    checkVars(variables, "n", "n_n", "a", 1L);
                    TruffleObject arguments = (TruffleObject) ls.getArguments();
                    checkVars(arguments, "n", "n_n");
                }
                depth++;
            }
            assertEquals("DynamicScope depth", 2, depth);
            break;
        case 5:
        case 9:
        case 10:
            for (Scope ls : lexicalScopes) {
                if (depth == 0) {
                    checkBlock(ls);
                    TruffleObject variables = (TruffleObject) ls.getVariables();
                    checkVars(variables, "b", null);
                    assertNull(ls.getArguments());
                } else {
                    checkRootNode(ls, "test", frame);
                    TruffleObject variables = (TruffleObject) ls.getVariables();
                    checkVars(variables, "n", null, "a", null);
                    TruffleObject arguments = (TruffleObject) ls.getArguments();
                    checkVars(arguments, "n", null);
                }
                depth++;
            }
            assertEquals("LexicalScope depth", 2, depth);
            depth = 0;
            for (Scope ls : dynamicScopes) {
                if (depth == 0) {
                    checkBlock(ls);
                    TruffleObject variables = (TruffleObject) ls.getVariables();
                    long bVal = (line == 5) ? 10L : 20L;
                    checkVars(variables, "b", bVal);
                    assertNull(ls.getArguments());
                } else {
                    checkRootNode(ls, "test", frame);
                    TruffleObject variables = (TruffleObject) ls.getVariables();
                    long aVal = (line == 10) ? 0L : 1L;
                    checkVars(variables, "n", "n_n", "a", aVal);
                    TruffleObject arguments = (TruffleObject) ls.getArguments();
                    checkVars(arguments, "n", "n_n");
                }
                depth++;
            }
            assertEquals("DynamicScope depth", 2, depth);
            break;
        case 11:
            for (Scope ls : lexicalScopes) {
                if (depth == 0) {
                    checkBlock(ls);
                    TruffleObject variables = (TruffleObject) ls.getVariables();
                    checkVars(variables, "b", null, "c", null);
                    assertNull(ls.getArguments());
                } else {
                    checkRootNode(ls, "test", frame);
                }
                depth++;
            }
            assertEquals("LexicalScope depth", 2, depth);
            depth = 0;
            for (Scope ls : dynamicScopes) {
                if (depth == 0) {
                    checkBlock(ls);
                    TruffleObject variables = (TruffleObject) ls.getVariables();
                    checkVars(variables, "b", 20L, "c", 1L);
                    assertNull(ls.getArguments());
                } else {
                    checkRootNode(ls, "test", frame);
                }
                depth++;
            }
            assertEquals("DynamicScope depth", 2, depth);
            break;
        case 12:
        case 13:
        case 14:
        case 15:
            for (Scope ls : lexicalScopes) {
                if (depth == 0) {
                    checkBlock(ls);
                    TruffleObject variables = (TruffleObject) ls.getVariables();
                    checkVars(variables);
                    assertNull(ls.getArguments());
                } else if (depth == 1) {
                    checkBlock(ls);
                    TruffleObject variables = (TruffleObject) ls.getVariables();
                    checkVars(variables, "b", null, "c", null);
                    assertNull(ls.getArguments());
                } else {
                    checkRootNode(ls, "test", frame);
                    TruffleObject variables = (TruffleObject) ls.getVariables();
                    checkVars(variables, "n", null, "a", null);
                }
                depth++;
            }
            assertEquals("LexicalScope depth", 3, depth);
            depth = 0;
            for (Scope ls : dynamicScopes) {
                if (depth == 0) {
                    checkBlock(ls);
                    TruffleObject variables = (TruffleObject) ls.getVariables();
                    checkVars(variables);
                    assertNull(ls.getArguments());
                } else if (depth == 1) {
                    checkBlock(ls);
                    TruffleObject variables = (TruffleObject) ls.getVariables();
                    long bVal = (line < 14) ? 20L : 5L;
                    long cVal = (line < 15) ? 1L : 6L;
                    checkVars(variables, "b", bVal, "c", cVal);
                    assertNull(ls.getArguments());
                } else {
                    checkRootNode(ls, "test", frame);
                    TruffleObject variables = (TruffleObject) ls.getVariables();
                    long aVal = (line == 12) ? 0L : 4L;
                    checkVars(variables, "n", "n_n", "a", aVal);
                }
                depth++;
            }
            assertEquals("DynamicScope depth", 3, depth);
            break;
        case 16:
            for (Scope ls : lexicalScopes) {
                if (depth == 0) {
                    checkBlock(ls);
                    TruffleObject variables = (TruffleObject) ls.getVariables();
                    checkVars(variables, "d", null);
                    assertNull(ls.getArguments());
                } else if (depth == 1) {
                    checkBlock(ls);
                    TruffleObject variables = (TruffleObject) ls.getVariables();
                    checkVars(variables, "b", null, "c", null);
                    assertNull(ls.getArguments());
                } else {
                    checkRootNode(ls, "test", frame);
                    TruffleObject variables = (TruffleObject) ls.getVariables();
                    checkVars(variables, "n", null, "a", null);
                }
                depth++;
            }
            assertEquals("LexicalScope depth", 3, depth);
            depth = 0;
            for (Scope ls : dynamicScopes) {
                if (depth == 0) {
                    checkBlock(ls);
                    TruffleObject variables = (TruffleObject) ls.getVariables();
                    checkVars(variables, "d", 7L);
                    assertNull(ls.getArguments());
                } else if (depth == 1) {
                    checkBlock(ls);
                    TruffleObject variables = (TruffleObject) ls.getVariables();
                    checkVars(variables, "b", 5L, "c", 6L);
                    assertNull(ls.getArguments());
                } else {
                    checkRootNode(ls, "test", frame);
                    TruffleObject variables = (TruffleObject) ls.getVariables();
                    checkVars(variables, "n", "n_n", "a", 4L);
                }
                depth++;
            }
            assertEquals("DynamicScope depth", 3, depth);
            break;
        case 22:
        case 23:
            for (Scope ls : lexicalScopes) {
                checkRootNode(ls, "main", frame);
                TruffleObject arguments = (TruffleObject) ls.getArguments();
                checkVars(arguments);
                TruffleObject variables = (TruffleObject) ls.getVariables();
                checkVars(variables);
                depth++;
            }
            assertEquals("LexicalScope depth", 1, depth);
            depth = 0;
            for (Scope ls : dynamicScopes) {
                checkRootNode(ls, "main", frame);
                TruffleObject arguments = (TruffleObject) ls.getArguments();
                checkVars(arguments);
                TruffleObject variables = (TruffleObject) ls.getVariables();
                checkVars(variables);
                depth++;
            }
            assertEquals("DynamicScope depth", 1, depth);
            break;
        default:
            fail("Untested line: " + line);
            break;
    }
}
Also used : Scope(com.oracle.truffle.api.Scope) TruffleObject(com.oracle.truffle.api.interop.TruffleObject)

Example 3 with Scope

use of com.oracle.truffle.api.Scope in project graal by oracle.

the class PolyglotLanguageContext method lookupGuest.

Object lookupGuest(String symbolName) {
    ensureInitialized(null);
    Iterable<?> topScopes = VMAccessor.instrumentAccess().findTopScopes(env);
    for (Object topScope : topScopes) {
        Scope scope = (Scope) topScope;
        TruffleObject variables = (TruffleObject) scope.getVariables();
        int symbolInfo = ForeignAccess.sendKeyInfo(keyInfoNode, variables, symbolName);
        if (KeyInfo.isExisting(symbolInfo) && KeyInfo.isReadable(symbolInfo)) {
            try {
                return ForeignAccess.sendRead(readNode, variables, symbolName);
            } catch (InteropException ex) {
                throw new AssertionError(symbolName, ex);
            }
        }
    }
    return null;
}
Also used : Scope(com.oracle.truffle.api.Scope) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) InteropException(com.oracle.truffle.api.interop.InteropException) TruffleObject(com.oracle.truffle.api.interop.TruffleObject)

Example 4 with Scope

use of com.oracle.truffle.api.Scope in project graal by oracle.

the class SLLanguage method findLocalScopes.

@Override
public Iterable<Scope> findLocalScopes(SLContext context, Node node, Frame frame) {
    final SLLexicalScope scope = SLLexicalScope.createScope(node);
    return new Iterable<Scope>() {

        @Override
        public Iterator<Scope> iterator() {
            return new Iterator<Scope>() {

                private SLLexicalScope previousScope;

                private SLLexicalScope nextScope = scope;

                @Override
                public boolean hasNext() {
                    if (nextScope == null) {
                        nextScope = previousScope.findParent();
                    }
                    return nextScope != null;
                }

                @Override
                public Scope next() {
                    if (!hasNext()) {
                        throw new NoSuchElementException();
                    }
                    Scope vscope = Scope.newBuilder(nextScope.getName(), nextScope.getVariables(frame)).node(nextScope.getNode()).arguments(nextScope.getArguments(frame)).build();
                    previousScope = nextScope;
                    nextScope = null;
                    return vscope;
                }
            };
        }
    };
}
Also used : SLLexicalScope(com.oracle.truffle.sl.nodes.local.SLLexicalScope) Scope(com.oracle.truffle.api.Scope) Iterator(java.util.Iterator) SLLexicalScope(com.oracle.truffle.sl.nodes.local.SLLexicalScope) NoSuchElementException(java.util.NoSuchElementException)

Example 5 with Scope

use of com.oracle.truffle.api.Scope in project graal by oracle.

the class DebuggerSessionSnippets method getTopScope.

/**
 * Returns a language top scope. The top scopes have global validity and unlike
 * {@link DebugStackFrame#getScope()} have no relation to the suspended location.
 *
 * @since 0.30
 */
public DebugScope getTopScope(String languageId) {
    LanguageInfo info = debugger.getEnv().getLanguages().get(languageId);
    if (info == null) {
        return null;
    }
    Iterable<Scope> scopes = debugger.getEnv().findTopScopes(languageId);
    Iterator<Scope> it = scopes.iterator();
    if (!it.hasNext()) {
        return null;
    }
    return new DebugScope(it.next(), it, debugger, info);
}
Also used : LanguageInfo(com.oracle.truffle.api.nodes.LanguageInfo) Scope(com.oracle.truffle.api.Scope)

Aggregations

Scope (com.oracle.truffle.api.Scope)6 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)2 RootNode (com.oracle.truffle.api.nodes.RootNode)2 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)1 MaterializedFrame (com.oracle.truffle.api.frame.MaterializedFrame)1 InteropException (com.oracle.truffle.api.interop.InteropException)1 LanguageInfo (com.oracle.truffle.api.nodes.LanguageInfo)1 Node (com.oracle.truffle.api.nodes.Node)1 SourceSection (com.oracle.truffle.api.source.SourceSection)1 LLVMSourceContext (com.oracle.truffle.llvm.runtime.debug.LLVMSourceContext)1 LLVMNode (com.oracle.truffle.llvm.runtime.nodes.api.LLVMNode)1 SLLexicalScope (com.oracle.truffle.sl.nodes.local.SLLexicalScope)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 NoSuchElementException (java.util.NoSuchElementException)1