Search in sources :

Example 21 with PointerType

use of com.oracle.truffle.llvm.runtime.types.PointerType in project sulong by graalvm.

the class AsmFactory method getOperandAddress.

private LLVMExpressionNode getOperandAddress(Type type, AsmOperand operand) {
    if (operand instanceof AsmRegisterOperand) {
        AsmRegisterOperand op = (AsmRegisterOperand) operand;
        FrameSlot frame = getRegisterSlot(op.getBaseRegister());
        if (type instanceof PointerType) {
            return LLVMAddressReadNodeGen.create(frame);
        } else {
            throw new AsmParseException("not a pointer");
        }
    } else if (operand instanceof AsmArgumentOperand) {
        AsmArgumentOperand op = (AsmArgumentOperand) operand;
        Argument info = argInfo.get(op.getIndex());
        FrameSlot frame = getArgumentSlot(op.getIndex(), type);
        if (info.isMemory()) {
            return LLVMAddressReadNodeGen.create(frame);
        } else {
            throw new AsmParseException("not a pointer");
        }
    } else if (operand instanceof AsmMemoryOperand) {
        AsmMemoryOperand op = (AsmMemoryOperand) operand;
        int displacement = op.getDisplacement();
        AsmOperand base = op.getBase();
        AsmOperand offset = op.getOffset();
        int shift = op.getShift();
        LLVMExpressionNode baseAddress;
        assert op.getSegment() == null || op.getSegment().equals("%fs");
        LLVMExpressionNode segment = null;
        if (op.getSegment() != null) {
            segment = new LLVMAMD64GetTlsNode();
        }
        if (base != null) {
            baseAddress = getOperandLoad(new PointerType(type), base);
        } else if (offset != null) {
            LLVMExpressionNode offsetNode = getOperandLoad(null, offset);
            if (op.getSegment() != null) {
                return LLVMAMD64AddressOffsetComputationNodeGen.create(displacement, shift, segment, offsetNode);
            } else {
                return LLVMAMD64AddressNoBaseOffsetComputationNodeGen.create(displacement, shift, offsetNode);
            }
        } else if (op.getSegment() != null) {
            return LLVMAMD64AddressDisplacementComputationNodeGen.create(displacement, segment);
        } else {
            if (type instanceof PrimitiveType) {
                switch(((PrimitiveType) type).getPrimitiveKind()) {
                    case I16:
                        return LLVMAMD64I16NodeGen.create((short) displacement);
                    case I32:
                        return LLVMAMD64I32NodeGen.create(displacement);
                    case I64:
                        return LLVMAMD64I64NodeGen.create(displacement);
                    default:
                        throw new AsmParseException("unknown type: " + type);
                }
            } else if (type instanceof PointerType) {
                return LLVMAMD64I64NodeGen.create(displacement);
            } else {
                throw new AsmParseException("invalid type: " + type);
            }
        }
        LLVMExpressionNode address;
        if (offset == null) {
            address = LLVMAMD64AddressDisplacementComputationNodeGen.create(displacement, baseAddress);
        } else {
            LLVMExpressionNode offsetNode = getOperandLoad(null, offset);
            address = LLVMAMD64AddressOffsetComputationNodeGen.create(displacement, shift, baseAddress, offsetNode);
        }
        if (op.getSegment() != null) {
            address = LLVMAMD64AddressSegmentComputationNodeGen.create(address, segment);
        }
        return address;
    } else {
        throw new AsmParseException("unsupported operand: " + operand);
    }
}
Also used : FrameSlot(com.oracle.truffle.api.frame.FrameSlot) LLVMAMD64GetTlsNode(com.oracle.truffle.llvm.nodes.asm.support.LLVMAMD64GetTlsNode) LLVMExpressionNode(com.oracle.truffle.llvm.runtime.nodes.api.LLVMExpressionNode) PointerType(com.oracle.truffle.llvm.runtime.types.PointerType) PrimitiveType(com.oracle.truffle.llvm.runtime.types.PrimitiveType)

Example 22 with PointerType

use of com.oracle.truffle.llvm.runtime.types.PointerType in project sulong by graalvm.

the class AsmFactory method getArguments.

private void getArguments() {
    LLVMStoreNode[] writeNodes = null;
    LLVMExpressionNode[] valueNodes = null;
    if (retType instanceof StructureType) {
        writeNodes = new LLVMStoreNode[retTypes.length];
        valueNodes = new LLVMExpressionNode[retTypes.length];
    }
    Set<String> todoRegisters = new HashSet<>(registers);
    for (Argument arg : argInfo) {
        // output register
        if (arg.isOutput()) {
            FrameSlot slot = null;
            if (arg.isRegister()) {
                slot = getRegisterSlot(arg.getRegister());
                LLVMExpressionNode register = LLVMAMD64ReadRegisterNodeGen.create(slot);
                if (retType instanceof StructureType) {
                    assert retTypes[arg.getOutIndex()] == arg.getType();
                    if (arg.getType() instanceof PointerType) {
                        valueNodes[arg.getOutIndex()] = LLVMToAddressNodeGen.create(register);
                        writeNodes[arg.getOutIndex()] = LLVMAddressStoreNodeGen.create(null, null);
                    } else {
                        PrimitiveKind primitiveKind = getPrimitiveKind(arg);
                        switch(primitiveKind) {
                            case I8:
                                valueNodes[arg.getOutIndex()] = LLVMToI8NoZeroExtNodeGen.create(register);
                                writeNodes[arg.getOutIndex()] = LLVMI8StoreNodeGen.create(null, null);
                                break;
                            case I16:
                                valueNodes[arg.getOutIndex()] = LLVMToI16NoZeroExtNodeGen.create(register);
                                writeNodes[arg.getOutIndex()] = LLVMI16StoreNodeGen.create(null, null);
                                break;
                            case I32:
                                valueNodes[arg.getOutIndex()] = LLVMToI32NoZeroExtNodeGen.create(register);
                                writeNodes[arg.getOutIndex()] = LLVMI32StoreNodeGen.create(null, null);
                                break;
                            case I64:
                                valueNodes[arg.getOutIndex()] = register;
                                writeNodes[arg.getOutIndex()] = LLVMI64StoreNodeGen.create(null, null);
                                break;
                            default:
                                throw new AsmParseException("invalid operand size: " + arg.getType());
                        }
                    }
                } else {
                    result = castResult(register);
                }
            } else {
                assert arg.isMemory();
                slot = getArgumentSlot(arg.getIndex(), argTypes[arg.getOutIndex()]);
                LLVMExpressionNode argnode = LLVMArgNodeGen.create(arg.getOutIndex());
                arguments.add(LLVMWriteAddressNodeGen.create(argnode, slot, sourceLocation));
            }
        }
        // input register
        if (arg.isInput()) {
            FrameSlot slot = null;
            if (arg.isRegister()) {
                String reg = arg.isAnonymous() ? arg.getRegister() : AsmRegisterOperand.getBaseRegister(arg.getRegister());
                slot = getRegisterSlot(reg);
                todoRegisters.remove(reg);
                LLVMExpressionNode argnode = LLVMArgNodeGen.create(arg.getInIndex());
                if (argTypes[arg.getInIndex()] instanceof PointerType) {
                    arguments.add(LLVMWriteAddressNodeGen.create(argnode, slot, null));
                } else {
                    LLVMExpressionNode node = LLVMToI64NoZeroExtNodeGen.create(argnode);
                    arguments.add(LLVMWriteI64NodeGen.create(node, slot, null));
                }
            }
            slot = getArgumentSlot(arg.getIndex(), argTypes[arg.getInIndex()]);
            LLVMExpressionNode argnode = LLVMArgNodeGen.create(arg.getInIndex());
            if (arg.getType() instanceof PrimitiveType) {
                LLVMExpressionNode node = LLVMToI64NoZeroExtNodeGen.create(argnode);
                arguments.add(LLVMWriteI64NodeGen.create(node, slot, null));
            } else if (arg.getType() instanceof PointerType) {
                arguments.add(LLVMWriteAddressNodeGen.create(argnode, slot, null));
            } else {
                throw new AsmParseException("invalid operand size: " + arg.getType());
            }
        }
    }
    if (retType instanceof StructureType) {
        LLVMExpressionNode addrArg = LLVMArgNodeGen.create(1);
        FrameSlot slot = frameDescriptor.addFrameSlot("returnValue", null, FrameSlotKind.Object);
        LLVMWriteAddressNode writeAddr = LLVMWriteAddressNodeGen.create(addrArg, slot, null);
        statements.add(writeAddr);
        LLVMExpressionNode addr = LLVMAddressReadNodeGen.create(slot);
        this.result = StructLiteralNodeGen.create(retOffsets, writeNodes, valueNodes, addr);
    }
    // rsp is initialized to stack pointer; ignore it here
    todoRegisters.remove("rsp");
    // initialize registers
    for (String register : todoRegisters) {
        if (register.startsWith("$")) {
            continue;
        }
        LLVMExpressionNode node = LLVMAMD64I64NodeGen.create(0);
        FrameSlot slot = getRegisterSlot(register);
        arguments.add(LLVMWriteI64NodeGen.create(node, slot, null));
    }
    // initialize flags
    LLVMExpressionNode zero = LLVMAMD64I1NodeGen.create(false);
    arguments.add(LLVMWriteI1NodeGen.create(zero, getFlagSlot(LLVMAMD64Flags.CF), sourceLocation));
    arguments.add(LLVMWriteI1NodeGen.create(zero, getFlagSlot(LLVMAMD64Flags.PF), sourceLocation));
    arguments.add(LLVMWriteI1NodeGen.create(zero, getFlagSlot(LLVMAMD64Flags.AF), sourceLocation));
    arguments.add(LLVMWriteI1NodeGen.create(zero, getFlagSlot(LLVMAMD64Flags.ZF), sourceLocation));
    arguments.add(LLVMWriteI1NodeGen.create(zero, getFlagSlot(LLVMAMD64Flags.SF), sourceLocation));
    arguments.add(LLVMWriteI1NodeGen.create(zero, getFlagSlot(LLVMAMD64Flags.OF), sourceLocation));
    // copy stack pointer
    LLVMExpressionNode stackPointer = LLVMArgNodeGen.create(0);
    FrameSlot stackSlot = frameDescriptor.addFrameSlot(LLVMStack.FRAME_ID);
    stackSlot.setKind(FrameSlotKind.Object);
    arguments.add(LLVMWriteAddressNodeGen.create(stackPointer, frameDescriptor.findFrameSlot(LLVMStack.FRAME_ID), sourceLocation));
    arguments.add(LLVMWriteAddressNodeGen.create(stackPointer, getRegisterSlot("rsp"), null));
    assert retType instanceof VoidType || retType != null;
}
Also used : VoidType(com.oracle.truffle.llvm.runtime.types.VoidType) FrameSlot(com.oracle.truffle.api.frame.FrameSlot) PointerType(com.oracle.truffle.llvm.runtime.types.PointerType) PrimitiveKind(com.oracle.truffle.llvm.runtime.types.PrimitiveType.PrimitiveKind) LLVMWriteAddressNode(com.oracle.truffle.llvm.nodes.vars.LLVMWriteNode.LLVMWriteAddressNode) StructureType(com.oracle.truffle.llvm.runtime.types.StructureType) LLVMExpressionNode(com.oracle.truffle.llvm.runtime.nodes.api.LLVMExpressionNode) LLVMStoreNode(com.oracle.truffle.llvm.runtime.nodes.api.LLVMStoreNode) PrimitiveType(com.oracle.truffle.llvm.runtime.types.PrimitiveType) HashSet(java.util.HashSet)

Example 23 with PointerType

use of com.oracle.truffle.llvm.runtime.types.PointerType in project sulong by graalvm.

the class Function method createFunctionCall.

private void createFunctionCall(long[] args) {
    int i = 0;
    final AttributesCodeEntry paramAttr = paramAttributes.getCodeEntry(args[i++]);
    final long ccinfo = args[i++];
    if (((ccinfo >> CALL_HAS_FMF_SHIFT) & 1) != 0) {
        // fast math flags
        i++;
    }
    FunctionType functionType = null;
    if (((ccinfo >> CALL_HAS_EXPLICITTYPE_SHIFT) & 1) != 0) {
        functionType = (FunctionType) types.get(args[i++]);
    }
    int callee = getIndex(args[i++]);
    Type calleeType;
    if (scope.isValueForwardRef(callee)) {
        calleeType = types.get(args[i++]);
    } else {
        calleeType = scope.getValueType(callee);
    }
    if (functionType == null) {
        if (calleeType instanceof FunctionType) {
            functionType = (FunctionType) calleeType;
        } else {
            functionType = (FunctionType) ((PointerType) calleeType).getPointeeType();
        }
    }
    int[] arguments = new int[args.length - i];
    int skipped = 0;
    int j = 0;
    while (j < functionType.getArgumentTypes().length && i < args.length) {
        arguments[j++] = getIndex(args[i++]);
    }
    while (i < args.length) {
        int index = getIndex(args[i++]);
        arguments[j++] = index;
        if (scope.isValueForwardRef(index)) {
            i++;
            skipped++;
        }
    }
    if (skipped > 0) {
        arguments = Arrays.copyOf(arguments, arguments.length - skipped);
    }
    final Type returnType = functionType.getReturnType();
    if (returnType == VoidType.INSTANCE) {
        emit(VoidCallInstruction.fromSymbols(scope, callee, arguments, paramAttr));
    } else {
        emit(CallInstruction.fromSymbols(scope, returnType, callee, arguments, paramAttr));
    }
}
Also used : PrimitiveType(com.oracle.truffle.llvm.runtime.types.PrimitiveType) VectorType(com.oracle.truffle.llvm.runtime.types.VectorType) 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) PointerType(com.oracle.truffle.llvm.runtime.types.PointerType) FunctionType(com.oracle.truffle.llvm.runtime.types.FunctionType) VoidType(com.oracle.truffle.llvm.runtime.types.VoidType) AttributesCodeEntry(com.oracle.truffle.llvm.parser.model.attributes.AttributesCodeEntry) FunctionType(com.oracle.truffle.llvm.runtime.types.FunctionType) PointerType(com.oracle.truffle.llvm.runtime.types.PointerType)

Example 24 with PointerType

use of com.oracle.truffle.llvm.runtime.types.PointerType in project sulong by graalvm.

the class Function method createCompareExchange.

private void createCompareExchange(long[] args, FunctionRecord record) {
    int i = 0;
    final Type ptrType;
    final int ptr = getIndex(args[i]);
    if (scope.isValueForwardRef(ptr)) {
        ptrType = types.get(args[++i]);
    } else {
        ptrType = scope.getValueType(ptr);
    }
    final int cmp = getIndex(args[++i]);
    if (record == FunctionRecord.CMPXCHG && scope.isValueForwardRef(cmp)) {
        // type of cmp
        ++i;
    }
    final int replace = getIndex(args[++i]);
    final boolean isVolatile = args[++i] != 0;
    final long successOrdering = args[++i];
    final long synchronizationScope = args[++i];
    final long failureOrdering = i < args.length - 1 ? args[++i] : -1L;
    final boolean addExtractValue = i >= args.length - 1;
    final boolean isWeak = addExtractValue || (args[++i] != 0);
    final Type type = findCmpxchgResultType(((PointerType) ptrType).getPointeeType());
    emit(CompareExchangeInstruction.fromSymbols(scope.getSymbols(), type, ptr, cmp, replace, isVolatile, successOrdering, synchronizationScope, failureOrdering, isWeak));
    if (addExtractValue) {
        // in older llvm versions cmpxchg just returned the new value at the pointer, to emulate
        // this we have to add an extractelvalue instruction. llvm does the same thing
        createExtractValue(new long[] { 1, 0 });
        // register the implicit index
        implicitIndices.add(scope.getNextValueIndex() - 1);
    }
}
Also used : PrimitiveType(com.oracle.truffle.llvm.runtime.types.PrimitiveType) VectorType(com.oracle.truffle.llvm.runtime.types.VectorType) 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) PointerType(com.oracle.truffle.llvm.runtime.types.PointerType) FunctionType(com.oracle.truffle.llvm.runtime.types.FunctionType) VoidType(com.oracle.truffle.llvm.runtime.types.VoidType)

Example 25 with PointerType

use of com.oracle.truffle.llvm.runtime.types.PointerType in project sulong by graalvm.

the class Function method createLoadAtomic.

private void createLoadAtomic(long[] args) {
    int i = 0;
    final int src = getIndex(args[i++]);
    final Type srcType;
    if (scope.isValueForwardRef(src)) {
        srcType = types.get(args[i++]);
    } else {
        srcType = scope.getValueType(src);
    }
    final Type opType;
    if (i + LOADATOMIC_ARGS_EXPECTED_AFTER_TYPE == args.length) {
        opType = types.get(args[i++]);
    } else {
        opType = ((PointerType) srcType).getPointeeType();
    }
    final int align = getAlign(args[i++]);
    final boolean isVolatile = args[i++] != 0;
    final long atomicOrdering = args[i++];
    final long synchronizationScope = args[i];
    emit(LoadInstruction.fromSymbols(scope.getSymbols(), opType, src, align, isVolatile, atomicOrdering, synchronizationScope));
}
Also used : PrimitiveType(com.oracle.truffle.llvm.runtime.types.PrimitiveType) VectorType(com.oracle.truffle.llvm.runtime.types.VectorType) 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) PointerType(com.oracle.truffle.llvm.runtime.types.PointerType) FunctionType(com.oracle.truffle.llvm.runtime.types.FunctionType) VoidType(com.oracle.truffle.llvm.runtime.types.VoidType)

Aggregations

PointerType (com.oracle.truffle.llvm.runtime.types.PointerType)37 PrimitiveType (com.oracle.truffle.llvm.runtime.types.PrimitiveType)31 Type (com.oracle.truffle.llvm.runtime.types.Type)29 StructureType (com.oracle.truffle.llvm.runtime.types.StructureType)27 FunctionType (com.oracle.truffle.llvm.runtime.types.FunctionType)24 ArrayType (com.oracle.truffle.llvm.runtime.types.ArrayType)20 LLVMExpressionNode (com.oracle.truffle.llvm.runtime.nodes.api.LLVMExpressionNode)18 AggregateType (com.oracle.truffle.llvm.runtime.types.AggregateType)18 VoidType (com.oracle.truffle.llvm.runtime.types.VoidType)18 VectorType (com.oracle.truffle.llvm.runtime.types.VectorType)14 FrameSlot (com.oracle.truffle.api.frame.FrameSlot)9 LLVMArithmeticInstructionType (com.oracle.truffle.llvm.parser.instructions.LLVMArithmeticInstructionType)9 LLVMConversionType (com.oracle.truffle.llvm.parser.instructions.LLVMConversionType)9 SymbolImpl (com.oracle.truffle.llvm.parser.model.SymbolImpl)6 ArrayList (java.util.ArrayList)5 AttributesGroup (com.oracle.truffle.llvm.parser.model.attributes.AttributesGroup)4 LLVMSourceLocation (com.oracle.truffle.llvm.runtime.debug.scope.LLVMSourceLocation)4 PrimitiveKind (com.oracle.truffle.llvm.runtime.types.PrimitiveType.PrimitiveKind)4 VariableBitWidthType (com.oracle.truffle.llvm.runtime.types.VariableBitWidthType)4 AttributesCodeEntry (com.oracle.truffle.llvm.parser.model.attributes.AttributesCodeEntry)3