Search in sources :

Example 1 with LLVMParserRuntime

use of com.oracle.truffle.llvm.parser.LLVMParserRuntime in project graal by oracle.

the class ParserDriver method parseBinary.

/**
 * Parses a binary (bitcode with optional meta information from an ELF, Mach-O object file).
 */
private LLVMParserResult parseBinary(BinaryParserResult binaryParserResult, TruffleFile file) {
    ModelModule module = new ModelModule();
    Source source = binaryParserResult.getSource();
    LLVMScanner.parseBitcode(binaryParserResult.getBitcode(), module, source);
    TargetDataLayout layout = module.getTargetDataLayout();
    DataLayout targetDataLayout = new DataLayout(layout.getDataLayout());
    verifyBitcodeSource(source, targetDataLayout, getTargetTriple(module));
    NodeFactory nodeFactory = context.getLanguage().getActiveConfiguration().createNodeFactory(language, targetDataLayout);
    // Create a new public file scope to be returned inside sulong library.
    LLVMScope publicFileScope = new LLVMScope();
    LLVMScope fileScope = new LLVMScope();
    LLVMParserRuntime runtime = new LLVMParserRuntime(fileScope, publicFileScope, nodeFactory, bitcodeID, file, binaryParserResult.getLibraryName(), getSourceFilesWithChecksums(context.getEnv(), module), binaryParserResult.getLocator());
    LLVMParser parser = new LLVMParser(source, runtime);
    LLVMParserResult result = parser.parse(module, targetDataLayout);
    createDebugInfo(module, new LLVMSymbolReadResolver(runtime, null, GetStackSpaceFactory.createAllocaFactory(), targetDataLayout, false));
    return result;
}
Also used : ModelModule(com.oracle.truffle.llvm.parser.model.ModelModule) DataLayout(com.oracle.truffle.llvm.runtime.datalayout.DataLayout) TargetDataLayout(com.oracle.truffle.llvm.parser.model.target.TargetDataLayout) TargetDataLayout(com.oracle.truffle.llvm.parser.model.target.TargetDataLayout) CommonNodeFactory(com.oracle.truffle.llvm.runtime.CommonNodeFactory) NodeFactory(com.oracle.truffle.llvm.runtime.NodeFactory) LLVMParser(com.oracle.truffle.llvm.parser.LLVMParser) LLVMSymbolReadResolver(com.oracle.truffle.llvm.parser.nodes.LLVMSymbolReadResolver) LLVMScope(com.oracle.truffle.llvm.runtime.LLVMScope) LLVMParserResult(com.oracle.truffle.llvm.parser.LLVMParserResult) LLVMParserRuntime(com.oracle.truffle.llvm.parser.LLVMParserRuntime) Source(com.oracle.truffle.api.source.Source)

Example 2 with LLVMParserRuntime

use of com.oracle.truffle.llvm.parser.LLVMParserRuntime in project graal by oracle.

the class InitializeGlobalNode method createGlobalVariableInitializer.

private static StaticInitsNode createGlobalVariableInitializer(LLVMParserResult parserResult, Object moduleName) {
    LLVMParserRuntime runtime = parserResult.getRuntime();
    GetStackSpaceFactory stackFactory = GetStackSpaceFactory.createAllocaFactory();
    List<GlobalVariable> globals = parserResult.getDefinedGlobals();
    DataLayout dataLayout = parserResult.getDataLayout();
    LLVMStatementNode initNode;
    int totalSize = 0;
    try {
        int[] sizes = new int[globals.size()];
        int nonEmptyGlobals = 0;
        for (int i = 0; i < sizes.length; i++) {
            GlobalVariable global = globals.get(i);
            if (global == null || global.getValue() == null) {
                continue;
            }
            long size = globals.get(i).getType().getPointeeType().getSize(dataLayout);
            if (size > Integer.MAX_VALUE || (totalSize + size) > Integer.MAX_VALUE) {
                throw new TypeOverflowException("globals section > 2GB is not supported");
            }
            if (size == 0) {
                continue;
            }
            sizes[i] = (int) size;
            totalSize += (int) size;
            nonEmptyGlobals++;
        }
        int[] bufferOffsets = new int[nonEmptyGlobals];
        LLVMGlobal[] descriptors = new LLVMGlobal[nonEmptyGlobals];
        Buffer buffer = new Buffer(totalSize, runtime, dataLayout);
        int globalIndex = 0;
        totalSize = 0;
        for (int i = 0; i < sizes.length; i++) {
            if (sizes[i] == 0) {
                continue;
            }
            GlobalVariable global = globals.get(i);
            /*
                 * For fetching the address of the global that we want to initialize, we must use
                 * the file scope because we are initializing the globals of the current file.
                 */
            descriptors[globalIndex] = runtime.getFileScope().getGlobalVariable(global.getName());
            assert descriptors[globalIndex] != null;
            bufferOffsets[globalIndex] = totalSize;
            global.getValue().addToBuffer(buffer, runtime, dataLayout, stackFactory);
            totalSize += sizes[i];
            globalIndex++;
        }
        initNode = buffer.createNode(bufferOffsets, descriptors);
    } catch (TypeOverflowException e) {
        initNode = Type.handleOverflowStatement(e);
    }
    return StaticInitsNodeGen.create(new LLVMStatementNode[] { initNode }, "global variable initializers", moduleName);
}
Also used : DataLayout(com.oracle.truffle.llvm.runtime.datalayout.DataLayout) ByteBuffer(java.nio.ByteBuffer) TypeOverflowException(com.oracle.truffle.llvm.runtime.types.Type.TypeOverflowException) LLVMGlobal(com.oracle.truffle.llvm.runtime.global.LLVMGlobal) LLVMParserRuntime(com.oracle.truffle.llvm.parser.LLVMParserRuntime) GetStackSpaceFactory(com.oracle.truffle.llvm.runtime.GetStackSpaceFactory) GlobalVariable(com.oracle.truffle.llvm.parser.model.symbols.globals.GlobalVariable) LLVMStatementNode(com.oracle.truffle.llvm.runtime.nodes.api.LLVMStatementNode)

Aggregations

LLVMParserRuntime (com.oracle.truffle.llvm.parser.LLVMParserRuntime)2 DataLayout (com.oracle.truffle.llvm.runtime.datalayout.DataLayout)2 Source (com.oracle.truffle.api.source.Source)1 LLVMParser (com.oracle.truffle.llvm.parser.LLVMParser)1 LLVMParserResult (com.oracle.truffle.llvm.parser.LLVMParserResult)1 ModelModule (com.oracle.truffle.llvm.parser.model.ModelModule)1 GlobalVariable (com.oracle.truffle.llvm.parser.model.symbols.globals.GlobalVariable)1 TargetDataLayout (com.oracle.truffle.llvm.parser.model.target.TargetDataLayout)1 LLVMSymbolReadResolver (com.oracle.truffle.llvm.parser.nodes.LLVMSymbolReadResolver)1 CommonNodeFactory (com.oracle.truffle.llvm.runtime.CommonNodeFactory)1 GetStackSpaceFactory (com.oracle.truffle.llvm.runtime.GetStackSpaceFactory)1 LLVMScope (com.oracle.truffle.llvm.runtime.LLVMScope)1 NodeFactory (com.oracle.truffle.llvm.runtime.NodeFactory)1 LLVMGlobal (com.oracle.truffle.llvm.runtime.global.LLVMGlobal)1 LLVMStatementNode (com.oracle.truffle.llvm.runtime.nodes.api.LLVMStatementNode)1 TypeOverflowException (com.oracle.truffle.llvm.runtime.types.Type.TypeOverflowException)1 ByteBuffer (java.nio.ByteBuffer)1