Search in sources :

Example 6 with SymbolImpl

use of com.oracle.truffle.llvm.parser.model.SymbolImpl in project sulong by graalvm.

the class LLVMBitcodeInstructionVisitor method visit.

@Override
public void visit(InvokeInstruction call) {
    final Type targetType = call.getType();
    int argumentCount = getArgumentCount(call.getArgumentCount(), targetType);
    final LLVMExpressionNode[] argNodes = new LLVMExpressionNode[argumentCount];
    final Type[] argTypes = new Type[argumentCount];
    int argIndex = 0;
    argNodes[argIndex] = nodeFactory.createFrameRead(runtime, PointerType.VOID, getStackSlot());
    argTypes[argIndex] = new PointerType(null);
    argIndex++;
    if (targetType instanceof StructureType) {
        argTypes[argIndex] = new PointerType(targetType);
        argNodes[argIndex] = nodeFactory.createAlloca(runtime, targetType);
        argIndex++;
    }
    for (int i = 0; argIndex < argumentCount; i++, argIndex++) {
        argNodes[argIndex] = symbols.resolve(call.getArgument(i));
        argTypes[argIndex] = call.getArgument(i).getType();
        final AttributesGroup paramAttr = call.getParameterAttributesGroup(i);
        if (isByValue(paramAttr)) {
            argNodes[argIndex] = capsuleAddressByValue(argNodes[argIndex], argTypes[argIndex], paramAttr);
        }
    }
    final SymbolImpl target = call.getCallTarget();
    int regularIndex = call.normalSuccessor().getBlockIndex();
    int unwindIndex = call.unwindSuccessor().getBlockIndex();
    List<FrameSlot> normalTo = new ArrayList<>();
    List<FrameSlot> unwindTo = new ArrayList<>();
    List<Type> normalType = new ArrayList<>();
    List<Type> unwindType = new ArrayList<>();
    List<LLVMExpressionNode> normalValue = new ArrayList<>();
    List<LLVMExpressionNode> unwindValue = new ArrayList<>();
    if (blockPhis != null) {
        for (Phi phi : blockPhis) {
            FrameSlot slot = getSlot(phi.getPhiValue().getName());
            LLVMExpressionNode value = symbols.resolve(phi.getValue());
            if (call.normalSuccessor() == phi.getBlock()) {
                normalTo.add(slot);
                normalType.add(phi.getValue().getType());
                normalValue.add(value);
            } else {
                unwindTo.add(slot);
                unwindType.add(phi.getValue().getType());
                unwindValue.add(value);
            }
        }
    }
    LLVMExpressionNode normalPhi = nodeFactory.createPhi(runtime, normalValue.toArray(new LLVMExpressionNode[normalValue.size()]), normalTo.toArray(new FrameSlot[normalTo.size()]), normalType.toArray(Type.EMPTY_ARRAY));
    LLVMExpressionNode unwindPhi = nodeFactory.createPhi(runtime, unwindValue.toArray(new LLVMExpressionNode[unwindValue.size()]), unwindTo.toArray(new FrameSlot[unwindTo.size()]), unwindType.toArray(Type.EMPTY_ARRAY));
    final LLVMSourceLocation source = sourceFunction.getSourceLocation(call);
    LLVMExpressionNode function = nodeFactory.createLLVMBuiltin(runtime, target, argNodes, argCount, null);
    if (function == null) {
        function = symbols.resolve(target);
    }
    LLVMControlFlowNode result = nodeFactory.createFunctionInvoke(runtime, getSlot(call.getName()), function, argNodes, new FunctionType(targetType, argTypes, false), regularIndex, unwindIndex, normalPhi, unwindPhi, source);
    setControlFlowNode(result);
}
Also used : LLVMControlFlowNode(com.oracle.truffle.llvm.runtime.nodes.api.LLVMControlFlowNode) FrameSlot(com.oracle.truffle.api.frame.FrameSlot) FunctionType(com.oracle.truffle.llvm.runtime.types.FunctionType) ArrayList(java.util.ArrayList) PointerType(com.oracle.truffle.llvm.runtime.types.PointerType) LLVMSourceLocation(com.oracle.truffle.llvm.runtime.debug.scope.LLVMSourceLocation) Phi(com.oracle.truffle.llvm.parser.LLVMPhiManager.Phi) SymbolImpl(com.oracle.truffle.llvm.parser.model.SymbolImpl) LLVMConversionType(com.oracle.truffle.llvm.parser.instructions.LLVMConversionType) PrimitiveType(com.oracle.truffle.llvm.runtime.types.PrimitiveType) StructureType(com.oracle.truffle.llvm.runtime.types.StructureType) ArrayType(com.oracle.truffle.llvm.runtime.types.ArrayType) AggregateType(com.oracle.truffle.llvm.runtime.types.AggregateType) LLVMArithmeticInstructionType(com.oracle.truffle.llvm.parser.instructions.LLVMArithmeticInstructionType) Type(com.oracle.truffle.llvm.runtime.types.Type) PointerType(com.oracle.truffle.llvm.runtime.types.PointerType) FunctionType(com.oracle.truffle.llvm.runtime.types.FunctionType) AttributesGroup(com.oracle.truffle.llvm.parser.model.attributes.AttributesGroup) StructureType(com.oracle.truffle.llvm.runtime.types.StructureType) LLVMExpressionNode(com.oracle.truffle.llvm.runtime.nodes.api.LLVMExpressionNode)

Example 7 with SymbolImpl

use of com.oracle.truffle.llvm.parser.model.SymbolImpl in project sulong by graalvm.

the class LLVMBitcodeInstructionVisitor method visit.

@Override
public void visit(VoidCallInstruction call) {
    // stackpointer
    final int argumentCount = call.getArgumentCount() + 1;
    final LLVMExpressionNode[] args = new LLVMExpressionNode[argumentCount];
    final Type[] argsType = new Type[argumentCount];
    int argIndex = 0;
    args[argIndex] = nodeFactory.createFrameRead(runtime, PointerType.VOID, getStackSlot());
    argsType[argIndex] = new PointerType(null);
    argIndex++;
    for (int i = 0; i < call.getArgumentCount(); i++) {
        args[argIndex] = symbols.resolve(call.getArgument(i));
        argsType[argIndex] = call.getArgument(i).getType();
        final AttributesGroup paramAttr = call.getParameterAttributesGroup(i);
        if (isByValue(paramAttr)) {
            args[argIndex] = capsuleAddressByValue(args[argIndex], argsType[argIndex], paramAttr);
        }
        argIndex++;
    }
    final LLVMSourceLocation source = sourceFunction.getSourceLocation(call);
    final SymbolImpl target = call.getCallTarget();
    LLVMExpressionNode node = nodeFactory.createLLVMBuiltin(runtime, target, args, argCount, source);
    if (node == null) {
        if (target instanceof InlineAsmConstant) {
            final InlineAsmConstant inlineAsmConstant = (InlineAsmConstant) target;
            node = createInlineAssemblerNode(inlineAsmConstant, args, argsType, call.getType(), source);
        } else {
            final LLVMExpressionNode function = symbols.resolve(target);
            final FunctionType functionType = new FunctionType(call.getType(), argsType, false);
            node = nodeFactory.createFunctionCall(runtime, function, args, functionType, source);
        }
    }
    addInstruction(node);
}
Also used : SymbolImpl(com.oracle.truffle.llvm.parser.model.SymbolImpl) LLVMConversionType(com.oracle.truffle.llvm.parser.instructions.LLVMConversionType) PrimitiveType(com.oracle.truffle.llvm.runtime.types.PrimitiveType) StructureType(com.oracle.truffle.llvm.runtime.types.StructureType) ArrayType(com.oracle.truffle.llvm.runtime.types.ArrayType) AggregateType(com.oracle.truffle.llvm.runtime.types.AggregateType) LLVMArithmeticInstructionType(com.oracle.truffle.llvm.parser.instructions.LLVMArithmeticInstructionType) Type(com.oracle.truffle.llvm.runtime.types.Type) PointerType(com.oracle.truffle.llvm.runtime.types.PointerType) FunctionType(com.oracle.truffle.llvm.runtime.types.FunctionType) AttributesGroup(com.oracle.truffle.llvm.parser.model.attributes.AttributesGroup) FunctionType(com.oracle.truffle.llvm.runtime.types.FunctionType) LLVMExpressionNode(com.oracle.truffle.llvm.runtime.nodes.api.LLVMExpressionNode) PointerType(com.oracle.truffle.llvm.runtime.types.PointerType) LLVMSourceLocation(com.oracle.truffle.llvm.runtime.debug.scope.LLVMSourceLocation) InlineAsmConstant(com.oracle.truffle.llvm.parser.model.symbols.constants.InlineAsmConstant)

Example 8 with SymbolImpl

use of com.oracle.truffle.llvm.parser.model.SymbolImpl in project sulong by graalvm.

the class LLVMBitcodeInstructionVisitor method visit.

@Override
public void visit(AllocateInstruction allocate) {
    final Type type = allocate.getPointeeType();
    int alignment;
    if (allocate.getAlign() == 0) {
        alignment = runtime.getContext().getByteAlignment(type);
    } else {
        alignment = 1 << (allocate.getAlign() - 1);
    }
    if (alignment == 0) {
        alignment = LLVMStack.NO_ALIGNMENT_REQUIREMENTS;
    }
    final SymbolImpl count = allocate.getCount();
    final LLVMExpressionNode result;
    if (count instanceof NullConstant) {
        result = nodeFactory.createAlloca(runtime, type, alignment);
    } else if (count instanceof IntegerConstant) {
        long numElements = (int) ((IntegerConstant) count).getValue();
        if (numElements == 1) {
            result = nodeFactory.createAlloca(runtime, type, alignment);
        } else {
            assert numElements == (int) numElements;
            ArrayType arrayType = new ArrayType(type, (int) numElements);
            result = nodeFactory.createAlloca(runtime, arrayType, alignment);
        }
    } else {
        LLVMExpressionNode num = symbols.resolve(count);
        result = nodeFactory.createAllocaArray(runtime, type, num, alignment);
    }
    // we never want to step on allocations, only to actual assignments in the source
    createFrameWrite(result, allocate, null);
}
Also used : ArrayType(com.oracle.truffle.llvm.runtime.types.ArrayType) SymbolImpl(com.oracle.truffle.llvm.parser.model.SymbolImpl) LLVMConversionType(com.oracle.truffle.llvm.parser.instructions.LLVMConversionType) PrimitiveType(com.oracle.truffle.llvm.runtime.types.PrimitiveType) StructureType(com.oracle.truffle.llvm.runtime.types.StructureType) ArrayType(com.oracle.truffle.llvm.runtime.types.ArrayType) AggregateType(com.oracle.truffle.llvm.runtime.types.AggregateType) LLVMArithmeticInstructionType(com.oracle.truffle.llvm.parser.instructions.LLVMArithmeticInstructionType) Type(com.oracle.truffle.llvm.runtime.types.Type) PointerType(com.oracle.truffle.llvm.runtime.types.PointerType) FunctionType(com.oracle.truffle.llvm.runtime.types.FunctionType) LLVMExpressionNode(com.oracle.truffle.llvm.runtime.nodes.api.LLVMExpressionNode) NullConstant(com.oracle.truffle.llvm.parser.model.symbols.constants.NullConstant) IntegerConstant(com.oracle.truffle.llvm.parser.model.symbols.constants.integer.IntegerConstant)

Example 9 with SymbolImpl

use of com.oracle.truffle.llvm.parser.model.SymbolImpl in project sulong by graalvm.

the class LLVMParserRuntime method resolveStructor.

private LLVMExpressionNode[] resolveStructor(GlobalValueSymbol globalVar, Comparator<Pair<Integer, ?>> priorityComparator) {
    if (!(globalVar.getValue() instanceof ArrayConstant)) {
        // array globals of length 0 may be initialized with scalar null
        return LLVMExpressionNode.NO_EXPRESSIONS;
    }
    final Object globalVariableDescriptor = scope.getGlobalVariable(globalVar.getName());
    final ArrayConstant arrayConstant = (ArrayConstant) globalVar.getValue();
    final int elemCount = arrayConstant.getElementCount();
    final StructureType elementType = (StructureType) arrayConstant.getType().getElementType();
    final int structSize = getContext().getByteSize(elementType);
    final FunctionType functionType = (FunctionType) ((PointerType) elementType.getElementType(1)).getPointeeType();
    final int indexedTypeLength = getContext().getByteAlignment(functionType);
    final ArrayList<Pair<Integer, LLVMExpressionNode>> structors = new ArrayList<>(elemCount);
    for (int i = 0; i < elemCount; i++) {
        final LLVMExpressionNode globalVarAddress = nodeFactory.createLiteral(this, globalVariableDescriptor, new PointerType(globalVar.getType()));
        final LLVMExpressionNode iNode = nodeFactory.createLiteral(this, i, PrimitiveType.I32);
        final LLVMExpressionNode structPointer = nodeFactory.createTypedElementPointer(this, globalVarAddress, iNode, structSize, elementType);
        final LLVMExpressionNode loadedStruct = nodeFactory.createLoad(this, elementType, structPointer);
        final LLVMExpressionNode oneLiteralNode = nodeFactory.createLiteral(this, 1, PrimitiveType.I32);
        final LLVMExpressionNode functionLoadTarget = nodeFactory.createTypedElementPointer(this, loadedStruct, oneLiteralNode, indexedTypeLength, functionType);
        final LLVMExpressionNode loadedFunction = nodeFactory.createLoad(this, functionType, functionLoadTarget);
        final LLVMExpressionNode[] argNodes = new LLVMExpressionNode[] { nodeFactory.createFrameRead(this, PointerType.VOID, rootFrame.findFrameSlot(LLVMStack.FRAME_ID)) };
        final LLVMExpressionNode functionCall = nodeFactory.createFunctionCall(this, loadedFunction, argNodes, functionType, null);
        final StructureConstant structorDefinition = (StructureConstant) arrayConstant.getElement(i);
        final SymbolImpl prioritySymbol = structorDefinition.getElement(0);
        final Integer priority = LLVMSymbolReadResolver.evaluateIntegerConstant(prioritySymbol);
        structors.add(new Pair<>(priority != null ? priority : LEAST_CONSTRUCTOR_PRIORITY, functionCall));
    }
    return structors.stream().sorted(priorityComparator).map(Pair::getSecond).toArray(LLVMExpressionNode[]::new);
}
Also used : FunctionType(com.oracle.truffle.llvm.runtime.types.FunctionType) ArrayList(java.util.ArrayList) PointerType(com.oracle.truffle.llvm.runtime.types.PointerType) SymbolImpl(com.oracle.truffle.llvm.parser.model.SymbolImpl) StructureConstant(com.oracle.truffle.llvm.parser.model.symbols.constants.aggregate.StructureConstant) StructureType(com.oracle.truffle.llvm.runtime.types.StructureType) LLVMExpressionNode(com.oracle.truffle.llvm.runtime.nodes.api.LLVMExpressionNode) ArrayConstant(com.oracle.truffle.llvm.parser.model.symbols.constants.aggregate.ArrayConstant) Pair(com.oracle.truffle.llvm.parser.util.Pair)

Example 10 with SymbolImpl

use of com.oracle.truffle.llvm.parser.model.SymbolImpl in project sulong by graalvm.

the class LLVMParserRuntime method registerFunctions.

private void registerFunctions(ModelModule model) {
    for (FunctionDefinition function : model.getDefinedFunctions()) {
        registerFunction(function, model);
    }
    for (Map.Entry<GlobalAlias, SymbolImpl> entry : aliases.entrySet()) {
        GlobalAlias alias = entry.getKey();
        SymbolImpl value = entry.getValue();
        if (value instanceof FunctionDefinition) {
            registerFunctionAlias(alias, (FunctionDefinition) value);
        }
    }
}
Also used : SymbolImpl(com.oracle.truffle.llvm.parser.model.SymbolImpl) FunctionDefinition(com.oracle.truffle.llvm.parser.model.functions.FunctionDefinition) GlobalAlias(com.oracle.truffle.llvm.parser.model.symbols.globals.GlobalAlias) Map(java.util.Map)

Aggregations

SymbolImpl (com.oracle.truffle.llvm.parser.model.SymbolImpl)17 PointerType (com.oracle.truffle.llvm.runtime.types.PointerType)8 Type (com.oracle.truffle.llvm.runtime.types.Type)8 LLVMExpressionNode (com.oracle.truffle.llvm.runtime.nodes.api.LLVMExpressionNode)7 FunctionType (com.oracle.truffle.llvm.runtime.types.FunctionType)7 StructureType (com.oracle.truffle.llvm.runtime.types.StructureType)7 LLVMArithmeticInstructionType (com.oracle.truffle.llvm.parser.instructions.LLVMArithmeticInstructionType)6 LLVMConversionType (com.oracle.truffle.llvm.parser.instructions.LLVMConversionType)6 AggregateType (com.oracle.truffle.llvm.runtime.types.AggregateType)6 ArrayType (com.oracle.truffle.llvm.runtime.types.ArrayType)6 PrimitiveType (com.oracle.truffle.llvm.runtime.types.PrimitiveType)6 AttributesGroup (com.oracle.truffle.llvm.parser.model.attributes.AttributesGroup)4 LLVMSourceLocation (com.oracle.truffle.llvm.runtime.debug.scope.LLVMSourceLocation)4 FrameSlot (com.oracle.truffle.api.frame.FrameSlot)3 NullConstant (com.oracle.truffle.llvm.parser.model.symbols.constants.NullConstant)3 IntegerConstant (com.oracle.truffle.llvm.parser.model.symbols.constants.integer.IntegerConstant)3 LLVMSourceSymbol (com.oracle.truffle.llvm.runtime.debug.LLVMSourceSymbol)3 MetaType (com.oracle.truffle.llvm.runtime.types.MetaType)3 VoidType (com.oracle.truffle.llvm.runtime.types.VoidType)3 ArrayList (java.util.ArrayList)3