Search in sources :

Example 1 with NodeFactory

use of com.oracle.truffle.llvm.runtime.NodeFactory in project graal by oracle.

the class LazyToTruffleConverterImpl method generateCallTarget.

private RootCallTarget generateCallTarget() {
    LLVMContext context = LLVMLanguage.getContext();
    NodeFactory nodeFactory = runtime.getNodeFactory();
    OptionValues options = context.getEnv().getOptions();
    boolean printAST = false;
    if (LLVMContext.printAstEnabled()) {
        String printASTOption = options.get(SulongEngineOption.PRINT_AST_FILTER);
        if (!printASTOption.isEmpty()) {
            String[] regexes = printASTOption.split(",");
            for (String regex : regexes) {
                if (method.getName().matches(regex)) {
                    printAST = true;
                    LLVMContext.printAstLog("========== " + method.getName());
                    break;
                }
            }
        }
    }
    doParse();
    // prepare the phis
    final Map<InstructionBlock, List<Phi>> phis = LLVMPhiManager.getPhis(method);
    LLVMLivenessAnalysisResult liveness = LLVMLivenessAnalysis.computeLiveness(phis, method);
    // setup the frameDescriptor
    FrameDescriptor.Builder builder = FrameDescriptor.newBuilder();
    nodeFactory.addStackSlots(builder);
    UniquesRegion uniquesRegion = new UniquesRegion();
    GetStackSpaceFactory getStackSpaceFactory = GetStackSpaceFactory.createGetUniqueStackSpaceFactory(uniquesRegion);
    LLVMSymbolReadResolver symbols = new LLVMSymbolReadResolver(runtime, builder, getStackSpaceFactory, dataLayout, options.get(SulongEngineOption.LL_DEBUG));
    int exceptionSlot = builder.addSlot(FrameSlotKind.Object, null, null);
    for (FunctionParameter parameter : method.getParameters()) {
        symbols.findOrAddFrameSlot(parameter);
    }
    HashSet<SSAValue> neededForDebug = getDebugValues();
    // create blocks and translate instructions
    boolean initDebugValues = true;
    LLVMRuntimeDebugInformation info = new LLVMRuntimeDebugInformation(method.getBlocks().size());
    LLVMBasicBlockNode[] blockNodes = new LLVMBasicBlockNode[method.getBlocks().size()];
    for (InstructionBlock block : method.getBlocks()) {
        List<Phi> blockPhis = phis.get(block);
        ArrayList<LLVMLivenessAnalysis.NullerInformation> blockNullerInfos = liveness.getNullableWithinBlock()[block.getBlockIndex()];
        LLVMBitcodeInstructionVisitor visitor = new LLVMBitcodeInstructionVisitor(exceptionSlot, uniquesRegion, blockPhis, method.getParameters().size(), symbols, context, blockNullerInfos, neededForDebug, dataLayout, nodeFactory);
        if (initDebugValues) {
            for (SourceVariable variable : method.getSourceFunction().getVariables()) {
                if (variable.hasFragments()) {
                    visitor.initializeAggregateLocalVariable(variable);
                }
            }
            initDebugValues = false;
        }
        for (int i = 0; i < block.getInstructionCount(); i++) {
            visitor.setInstructionIndex(i);
            block.getInstruction(i).accept(visitor);
        }
        LLVMStatementNode[] nodes = visitor.finish();
        info.setBlockDebugInfo(block.getBlockIndex(), visitor.getDebugInfo());
        blockNodes[block.getBlockIndex()] = LLVMBasicBlockNode.createBasicBlockNode(options, nodes, visitor.getControlFlowNode(), block.getBlockIndex(), block.getName());
    }
    for (int j = 0; j < blockNodes.length; j++) {
        int[] nullableBeforeBlock = getNullableFrameSlots(liveness.getFrameSlots(), liveness.getNullableBeforeBlock()[j]);
        int[] nullableAfterBlock = getNullableFrameSlots(liveness.getFrameSlots(), liveness.getNullableAfterBlock()[j]);
        blockNodes[j].setNullableFrameSlots(nullableBeforeBlock, nullableAfterBlock);
    }
    info.setBlocks(blockNodes);
    int loopSuccessorSlot = -1;
    if (options.get(SulongEngineOption.OSR_MODE) == SulongEngineOption.OSRMode.CFG && !options.get(SulongEngineOption.AOTCacheStore)) {
        LLVMControlFlowGraph cfg = new LLVMControlFlowGraph(method.getBlocks().toArray(FunctionDefinition.EMPTY));
        cfg.build();
        if (cfg.isReducible() && cfg.getCFGLoops().size() > 0) {
            loopSuccessorSlot = builder.addSlot(FrameSlotKind.Int, null, null);
            resolveLoops(blockNodes, cfg, loopSuccessorSlot, exceptionSlot, info, options);
        }
    }
    LLVMSourceLocation location = method.getLexicalScope();
    rootFunction.setSourceLocation(LLVMSourceLocation.orDefault(location));
    LLVMStatementNode[] copyArgumentsToFrameArray = copyArgumentsToFrame(symbols).toArray(LLVMStatementNode.NO_STATEMENTS);
    FrameDescriptor frame = builder.build();
    RootNode rootNode = nodeFactory.createFunction(exceptionSlot, blockNodes, uniquesRegion, copyArgumentsToFrameArray, frame, loopSuccessorSlot, info, method.getName(), method.getSourceName(), method.getParameters().size(), source, location, rootFunction);
    method.onAfterParse();
    if (printAST) {
        printCompactTree(rootNode);
        LLVMContext.printAstLog("");
    }
    return rootNode.getCallTarget();
}
Also used : RootNode(com.oracle.truffle.api.nodes.RootNode) OptionValues(org.graalvm.options.OptionValues) LLVMSymbolReadResolver(com.oracle.truffle.llvm.parser.nodes.LLVMSymbolReadResolver) LLVMSourceLocation(com.oracle.truffle.llvm.runtime.debug.scope.LLVMSourceLocation) GetStackSpaceFactory(com.oracle.truffle.llvm.runtime.GetStackSpaceFactory) Phi(com.oracle.truffle.llvm.parser.LLVMPhiManager.Phi) SourceVariable(com.oracle.truffle.llvm.parser.metadata.debuginfo.SourceVariable) LLVMControlFlowGraph(com.oracle.truffle.llvm.parser.util.LLVMControlFlowGraph) LLVMStatementNode(com.oracle.truffle.llvm.runtime.nodes.api.LLVMStatementNode) List(java.util.List) ArrayList(java.util.ArrayList) InstructionBlock(com.oracle.truffle.llvm.parser.model.blocks.InstructionBlock) FunctionParameter(com.oracle.truffle.llvm.parser.model.functions.FunctionParameter) FrameDescriptor(com.oracle.truffle.api.frame.FrameDescriptor) LLVMContext(com.oracle.truffle.llvm.runtime.LLVMContext) LLVMLivenessAnalysisResult(com.oracle.truffle.llvm.parser.LLVMLivenessAnalysis.LLVMLivenessAnalysisResult) LLVMBitcodeInstructionVisitor(com.oracle.truffle.llvm.parser.nodes.LLVMBitcodeInstructionVisitor) SSAValue(com.oracle.truffle.llvm.runtime.types.symbols.SSAValue) UniquesRegion(com.oracle.truffle.llvm.runtime.memory.LLVMStack.UniquesRegion) CommonNodeFactory(com.oracle.truffle.llvm.runtime.CommonNodeFactory) NodeFactory(com.oracle.truffle.llvm.runtime.NodeFactory) LLVMRuntimeDebugInformation(com.oracle.truffle.llvm.parser.nodes.LLVMRuntimeDebugInformation) LLVMBasicBlockNode(com.oracle.truffle.llvm.runtime.nodes.base.LLVMBasicBlockNode)

Example 2 with NodeFactory

use of com.oracle.truffle.llvm.runtime.NodeFactory in project graal by oracle.

the class LazyToTruffleConverterImpl method copyArgumentsToFrame.

/**
 * Copies arguments to the current frame, handling normal "primitives" and byval pointers (e.g.
 * for structs).
 */
private List<LLVMStatementNode> copyArgumentsToFrame(LLVMSymbolReadResolver symbols) {
    NodeFactory nodeFactory = runtime.getNodeFactory();
    List<FunctionParameter> parameters = method.getParameters();
    List<LLVMStatementNode> formalParamInits = new ArrayList<>();
    // There's a struct return type.
    int argIndex = 1;
    if (method.getType().getReturnType() instanceof StructureType) {
        argIndex++;
    }
    for (FunctionParameter parameter : parameters) {
        int slot = symbols.findOrAddFrameSlot(parameter);
        if (parameter.getType() instanceof PointerType && functionParameterHasByValueAttribute(parameter)) {
            // It's a struct passed as a pointer but originally passed by value (because LLVM
            // and/or ABI), treat it as such.
            PointerType pointerType = (PointerType) parameter.getType();
            Type pointeeType = pointerType.getPointeeType();
            GetStackSpaceFactory allocaFactory = GetStackSpaceFactory.createAllocaFactory();
            LLVMExpressionNode allocation = allocaFactory.createGetStackSpace(nodeFactory, pointeeType);
            formalParamInits.add(CommonNodeFactory.createFrameWrite(pointerType, allocation, slot));
            ArrayDeque<Long> indices = new ArrayDeque<>();
            copyStructArgumentsToFrame(formalParamInits, nodeFactory, slot, argIndex++, pointerType, pointeeType, indices);
        } else {
            LLVMExpressionNode parameterNode = nodeFactory.createFunctionArgNode(argIndex++, parameter.getType());
            formalParamInits.add(CommonNodeFactory.createFrameWrite(parameter.getType(), parameterNode, slot));
        }
    }
    return formalParamInits;
}
Also used : ArrayList(java.util.ArrayList) PointerType(com.oracle.truffle.llvm.runtime.types.PointerType) ArrayDeque(java.util.ArrayDeque) GetStackSpaceFactory(com.oracle.truffle.llvm.runtime.GetStackSpaceFactory) StructureType(com.oracle.truffle.llvm.runtime.types.StructureType) ArrayType(com.oracle.truffle.llvm.runtime.types.ArrayType) AggregateType(com.oracle.truffle.llvm.runtime.types.AggregateType) Type(com.oracle.truffle.llvm.runtime.types.Type) LLVMSourceFunctionType(com.oracle.truffle.llvm.runtime.debug.type.LLVMSourceFunctionType) PointerType(com.oracle.truffle.llvm.runtime.types.PointerType) CommonNodeFactory(com.oracle.truffle.llvm.runtime.CommonNodeFactory) NodeFactory(com.oracle.truffle.llvm.runtime.NodeFactory) StructureType(com.oracle.truffle.llvm.runtime.types.StructureType) LLVMStatementNode(com.oracle.truffle.llvm.runtime.nodes.api.LLVMStatementNode) LLVMExpressionNode(com.oracle.truffle.llvm.runtime.nodes.api.LLVMExpressionNode) FunctionParameter(com.oracle.truffle.llvm.parser.model.functions.FunctionParameter)

Example 3 with NodeFactory

use of com.oracle.truffle.llvm.runtime.NodeFactory in project graal by oracle.

the class InitializeModuleNode method createDestructor.

public static RootCallTarget createDestructor(LLVMParserResult parserResult, String moduleName, LLVMLanguage language) {
    LLVMStatementNode[] destructors = createStructor(DESTRUCTORS_VARNAME, parserResult, DESCENDING_PRIORITY);
    if (destructors.length > 0) {
        NodeFactory nodeFactory = parserResult.getRuntime().getNodeFactory();
        FrameDescriptor.Builder builder = FrameDescriptor.newBuilder();
        nodeFactory.addStackSlots(builder);
        FrameDescriptor frameDescriptor = builder.build();
        LLVMStatementRootNode root = new LLVMStatementRootNode(language, StaticInitsNodeGen.create(destructors, "fini", moduleName), frameDescriptor, nodeFactory.createStackAccess());
        return root.getCallTarget();
    } else {
        return null;
    }
}
Also used : FrameDescriptor(com.oracle.truffle.api.frame.FrameDescriptor) CommonNodeFactory(com.oracle.truffle.llvm.runtime.CommonNodeFactory) NodeFactory(com.oracle.truffle.llvm.runtime.NodeFactory) LLVMStatementNode(com.oracle.truffle.llvm.runtime.nodes.api.LLVMStatementNode) LLVMStatementRootNode(com.oracle.truffle.llvm.runtime.nodes.others.LLVMStatementRootNode)

Example 4 with NodeFactory

use of com.oracle.truffle.llvm.runtime.NodeFactory 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)

Aggregations

CommonNodeFactory (com.oracle.truffle.llvm.runtime.CommonNodeFactory)4 NodeFactory (com.oracle.truffle.llvm.runtime.NodeFactory)4 LLVMStatementNode (com.oracle.truffle.llvm.runtime.nodes.api.LLVMStatementNode)3 FrameDescriptor (com.oracle.truffle.api.frame.FrameDescriptor)2 FunctionParameter (com.oracle.truffle.llvm.parser.model.functions.FunctionParameter)2 LLVMSymbolReadResolver (com.oracle.truffle.llvm.parser.nodes.LLVMSymbolReadResolver)2 GetStackSpaceFactory (com.oracle.truffle.llvm.runtime.GetStackSpaceFactory)2 ArrayList (java.util.ArrayList)2 RootNode (com.oracle.truffle.api.nodes.RootNode)1 Source (com.oracle.truffle.api.source.Source)1 LLVMLivenessAnalysisResult (com.oracle.truffle.llvm.parser.LLVMLivenessAnalysis.LLVMLivenessAnalysisResult)1 LLVMParser (com.oracle.truffle.llvm.parser.LLVMParser)1 LLVMParserResult (com.oracle.truffle.llvm.parser.LLVMParserResult)1 LLVMParserRuntime (com.oracle.truffle.llvm.parser.LLVMParserRuntime)1 Phi (com.oracle.truffle.llvm.parser.LLVMPhiManager.Phi)1 SourceVariable (com.oracle.truffle.llvm.parser.metadata.debuginfo.SourceVariable)1 ModelModule (com.oracle.truffle.llvm.parser.model.ModelModule)1 InstructionBlock (com.oracle.truffle.llvm.parser.model.blocks.InstructionBlock)1 TargetDataLayout (com.oracle.truffle.llvm.parser.model.target.TargetDataLayout)1 LLVMBitcodeInstructionVisitor (com.oracle.truffle.llvm.parser.nodes.LLVMBitcodeInstructionVisitor)1