Search in sources :

Example 1 with LLVMSourceContext

use of com.oracle.truffle.llvm.runtime.debug.LLVMSourceContext in project sulong by graalvm.

the class LLVMParserRuntime method parse.

public static LLVMParserResult parse(Source source, ExternalLibrary library, BitcodeParserResult parserResult, LLVMLanguage language, LLVMContext context, NodeFactory nodeFactory) {
    ModelModule model = parserResult.getModel();
    TargetDataLayout layout = model.getTargetDataLayout();
    assert layout != null;
    LLVMModelVisitor module = new LLVMModelVisitor();
    model.accept(module);
    DataLayoutConverter.DataSpecConverterImpl targetDataLayout = DataLayoutConverter.getConverter(layout.getDataLayout());
    context.setDataLayoutConverter(targetDataLayout);
    LLVMParserRuntime runtime = new LLVMParserRuntime(source, library, language, context, nodeFactory, module.getAliases());
    runtime.registerFunctions(model);
    LLVMSymbolReadResolver symbolResolver = new LLVMSymbolReadResolver(runtime, runtime.getGlobalFrameDescriptor());
    LLVMExpressionNode[] globals = runtime.createGlobalVariableInitializationNodes(symbolResolver, module.getGlobals());
    RootNode globalVarInits = nodeFactory.createStaticInitsRootNode(runtime, globals);
    RootCallTarget globalVarInitsTarget = Truffle.getRuntime().createCallTarget(globalVarInits);
    LLVMExpressionNode[] deallocs = runtime.getDeallocations();
    RootNode globalVarDeallocs = nodeFactory.createStaticInitsRootNode(runtime, deallocs);
    RootCallTarget globalVarDeallocsTarget = Truffle.getRuntime().createCallTarget(globalVarDeallocs);
    RootCallTarget constructorFunctions = runtime.getConstructors(module.getGlobals());
    RootCallTarget destructorFunctions = runtime.getDestructors(module.getGlobals());
    if (context.getEnv().getOptions().get(SulongEngineOption.ENABLE_LVI)) {
        final LLVMSourceContext sourceContext = context.getSourceContext();
        model.getSourceGlobals().forEach((symbol, irValue) -> {
            final LLVMExpressionNode node = symbolResolver.resolve(irValue);
            final LLVMDebugValue value = nodeFactory.createDebugStaticValue(node);
            sourceContext.registerStatic(symbol, value);
        });
        model.getSourceStaticMembers().forEach(((type, symbol) -> {
            final LLVMExpressionNode node = symbolResolver.resolve(symbol);
            final LLVMDebugValue value = nodeFactory.createDebugStaticValue(node);
            type.setValue(value);
        }));
    }
    RootCallTarget mainFunctionCallTarget = null;
    if (runtime.getScope().functionExists("@main")) {
        LLVMFunctionDescriptor mainDescriptor = runtime.getScope().getFunctionDescriptor("@main");
        LLVMFunctionDescriptor startDescriptor = runtime.getScope().getFunctionDescriptor("@_start");
        RootCallTarget startCallTarget = startDescriptor.getLLVMIRFunction();
        String applicationPath = source.getPath() == null ? "" : source.getPath().toString();
        RootNode globalFunction = nodeFactory.createGlobalRootNode(runtime, startCallTarget, mainDescriptor, applicationPath);
        RootCallTarget globalFunctionRoot = Truffle.getRuntime().createCallTarget(globalFunction);
        RootNode globalRootNode = nodeFactory.createGlobalRootNodeWrapping(runtime, globalFunctionRoot, startDescriptor.getType().getReturnType());
        mainFunctionCallTarget = Truffle.getRuntime().createCallTarget(globalRootNode);
    }
    return new LLVMParserResult(runtime.getScope(), mainFunctionCallTarget, globalVarInitsTarget, globalVarDeallocsTarget, constructorFunctions, destructorFunctions);
}
Also used : GlobalVariable(com.oracle.truffle.llvm.parser.model.symbols.globals.GlobalVariable) DataLayoutConverter(com.oracle.truffle.llvm.runtime.datalayout.DataLayoutConverter) GlobalConstant(com.oracle.truffle.llvm.parser.model.symbols.globals.GlobalConstant) GlobalValueSymbol(com.oracle.truffle.llvm.parser.model.symbols.globals.GlobalValueSymbol) RootCallTarget(com.oracle.truffle.api.RootCallTarget) FunctionDefinition(com.oracle.truffle.llvm.parser.model.functions.FunctionDefinition) Pair(com.oracle.truffle.llvm.parser.util.Pair) Linkage(com.oracle.truffle.llvm.parser.model.enums.Linkage) LLVMContext(com.oracle.truffle.llvm.runtime.LLVMContext) SulongEngineOption(com.oracle.truffle.llvm.runtime.options.SulongEngineOption) LLVMSymbolReadResolver(com.oracle.truffle.llvm.parser.nodes.LLVMSymbolReadResolver) ArrayList(java.util.ArrayList) FrameDescriptor(com.oracle.truffle.api.frame.FrameDescriptor) LLVMFunctionDescriptor(com.oracle.truffle.llvm.runtime.LLVMFunctionDescriptor) Type(com.oracle.truffle.llvm.runtime.types.Type) ModelModule(com.oracle.truffle.llvm.parser.model.ModelModule) LLVMSourceContext(com.oracle.truffle.llvm.runtime.debug.LLVMSourceContext) LLVMLanguage(com.oracle.truffle.llvm.runtime.LLVMLanguage) Map(java.util.Map) RootNode(com.oracle.truffle.api.nodes.RootNode) ArrayConstant(com.oracle.truffle.llvm.parser.model.symbols.constants.aggregate.ArrayConstant) LLVMSourceSymbol(com.oracle.truffle.llvm.runtime.debug.LLVMSourceSymbol) PrimitiveType(com.oracle.truffle.llvm.runtime.types.PrimitiveType) ExternalLibrary(com.oracle.truffle.llvm.runtime.LLVMContext.ExternalLibrary) LLVMExpressionNode(com.oracle.truffle.llvm.runtime.nodes.api.LLVMExpressionNode) LLVMStack(com.oracle.truffle.llvm.runtime.memory.LLVMStack) SymbolImpl(com.oracle.truffle.llvm.parser.model.SymbolImpl) TargetDataLayout(com.oracle.truffle.llvm.parser.model.target.TargetDataLayout) LLVMDebugValue(com.oracle.truffle.llvm.runtime.debug.LLVMDebugValue) PointerType(com.oracle.truffle.llvm.runtime.types.PointerType) LLVMScope(com.oracle.truffle.llvm.runtime.LLVMScope) List(java.util.List) Source(com.oracle.truffle.api.source.Source) StructureType(com.oracle.truffle.llvm.runtime.types.StructureType) StructureConstant(com.oracle.truffle.llvm.parser.model.symbols.constants.aggregate.StructureConstant) GlobalAlias(com.oracle.truffle.llvm.parser.model.symbols.globals.GlobalAlias) FunctionType(com.oracle.truffle.llvm.runtime.types.FunctionType) Truffle(com.oracle.truffle.api.Truffle) ArrayType(com.oracle.truffle.llvm.runtime.types.ArrayType) Comparator(java.util.Comparator) RootNode(com.oracle.truffle.api.nodes.RootNode) TargetDataLayout(com.oracle.truffle.llvm.parser.model.target.TargetDataLayout) DataLayoutConverter(com.oracle.truffle.llvm.runtime.datalayout.DataLayoutConverter) LLVMSymbolReadResolver(com.oracle.truffle.llvm.parser.nodes.LLVMSymbolReadResolver) ModelModule(com.oracle.truffle.llvm.parser.model.ModelModule) LLVMFunctionDescriptor(com.oracle.truffle.llvm.runtime.LLVMFunctionDescriptor) LLVMExpressionNode(com.oracle.truffle.llvm.runtime.nodes.api.LLVMExpressionNode) LLVMDebugValue(com.oracle.truffle.llvm.runtime.debug.LLVMDebugValue) RootCallTarget(com.oracle.truffle.api.RootCallTarget) LLVMSourceContext(com.oracle.truffle.llvm.runtime.debug.LLVMSourceContext)

Example 2 with LLVMSourceContext

use of com.oracle.truffle.llvm.runtime.debug.LLVMSourceContext in project sulong by graalvm.

the class LLVMSourceScope method create.

@TruffleBoundary
public static Iterable<Scope> create(Node node, Frame frame, LLVMContext context) {
    final LLVMSourceContext sourceContext = context.getSourceContext();
    final RootNode rootNode = node.getRootNode();
    LLVMNode llvmNode = findStatementNode(node);
    if (rootNode == null || llvmNode == null) {
        return Collections.singleton(new LLVMSourceScope(sourceContext, node).toScope(frame));
    }
    LLVMSourceLocation scope = llvmNode.getSourceLocation();
    final SourceSection sourceSection = llvmNode.getSourceSection();
    LLVMSourceScope baseScope = new LLVMSourceScope(sourceContext, new LinkedList<>(), rootNode);
    LLVMSourceScope staticScope = null;
    for (boolean isLocalScope = true; isLocalScope && scope != null; scope = scope.getParent()) {
        final LLVMSourceScope next = toScope(scope, sourceContext, rootNode, sourceSection);
        copySymbols(next, baseScope);
        if (scope.getKind() == LLVMSourceLocation.Kind.FUNCTION) {
            baseScope.setName(next.getName());
            if (scope.getCompileUnit() != null) {
                staticScope = toScope(scope.getCompileUnit(), sourceContext, null, sourceSection);
            }
            isLocalScope = false;
        }
    }
    List<Scope> scopeList = new ArrayList<>();
    scopeList.add(baseScope.toScope(frame));
    for (; scope != null; scope = scope.getParent()) {
        // e.g. lambdas are compiled to calls to a method in a locally defined class. We
        // cannot access the locals of the enclosing function since they do not lie on the
        // function's frame. They are still accessible from the calling function's frame, so
        // we can simply ignore this scope here. Also, any variables actually used in the
        // lambda would still be available as the members of the 'this' pointer.
        final LLVMSourceScope next = toScope(scope, sourceContext, null, sourceSection);
        switch(scope.getKind()) {
            case NAMESPACE:
            case FILE:
            case BLOCK:
                if (next.hasSymbols()) {
                    scopeList.add(next.toScope(frame));
                }
                break;
            case COMPILEUNIT:
                if (staticScope == null) {
                    staticScope = next;
                } else {
                    copySymbols(next, staticScope);
                }
                break;
        }
    }
    if (staticScope != null && staticScope.hasSymbols()) {
        scopeList.add(staticScope.toScope(frame));
    }
    return Collections.unmodifiableList(scopeList);
}
Also used : RootNode(com.oracle.truffle.api.nodes.RootNode) Scope(com.oracle.truffle.api.Scope) ArrayList(java.util.ArrayList) SourceSection(com.oracle.truffle.api.source.SourceSection) LLVMNode(com.oracle.truffle.llvm.runtime.nodes.api.LLVMNode) LLVMSourceContext(com.oracle.truffle.llvm.runtime.debug.LLVMSourceContext) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Aggregations

RootNode (com.oracle.truffle.api.nodes.RootNode)2 LLVMSourceContext (com.oracle.truffle.llvm.runtime.debug.LLVMSourceContext)2 ArrayList (java.util.ArrayList)2 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)1 RootCallTarget (com.oracle.truffle.api.RootCallTarget)1 Scope (com.oracle.truffle.api.Scope)1 Truffle (com.oracle.truffle.api.Truffle)1 FrameDescriptor (com.oracle.truffle.api.frame.FrameDescriptor)1 Source (com.oracle.truffle.api.source.Source)1 SourceSection (com.oracle.truffle.api.source.SourceSection)1 ModelModule (com.oracle.truffle.llvm.parser.model.ModelModule)1 SymbolImpl (com.oracle.truffle.llvm.parser.model.SymbolImpl)1 Linkage (com.oracle.truffle.llvm.parser.model.enums.Linkage)1 FunctionDefinition (com.oracle.truffle.llvm.parser.model.functions.FunctionDefinition)1 ArrayConstant (com.oracle.truffle.llvm.parser.model.symbols.constants.aggregate.ArrayConstant)1 StructureConstant (com.oracle.truffle.llvm.parser.model.symbols.constants.aggregate.StructureConstant)1 GlobalAlias (com.oracle.truffle.llvm.parser.model.symbols.globals.GlobalAlias)1 GlobalConstant (com.oracle.truffle.llvm.parser.model.symbols.globals.GlobalConstant)1 GlobalValueSymbol (com.oracle.truffle.llvm.parser.model.symbols.globals.GlobalValueSymbol)1 GlobalVariable (com.oracle.truffle.llvm.parser.model.symbols.globals.GlobalVariable)1