Search in sources :

Example 1 with LLVMIVarBit

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

the class LLVMAllocationValueProvider method readBigInteger.

@Override
@TruffleBoundary
public Object readBigInteger(long bitOffset, int bitSize, boolean signed) {
    if (!canRead(bitOffset, bitSize)) {
        return unavailable(bitOffset, bitSize);
    }
    // the most common cases are byte-aligned integers
    if (isByteAligned(bitOffset)) {
        final long byteOffset = bitOffset / Byte.SIZE;
        final long address = baseAddress.increment(byteOffset).getVal();
        if (signed) {
            switch(bitSize) {
                case LLVMDebugTypeConstants.BYTE_SIZE:
                    return BigInteger.valueOf(memory.getI8(address));
                case LLVMDebugTypeConstants.SHORT_SIZE:
                    return BigInteger.valueOf(memory.getI16(address));
                case LLVMDebugTypeConstants.INTEGER_SIZE:
                    return BigInteger.valueOf(memory.getI32(address));
                case LLVMDebugTypeConstants.LONG_SIZE:
                    return BigInteger.valueOf(memory.getI64(address));
            }
        } else {
            switch(bitSize) {
                case LLVMDebugTypeConstants.BYTE_SIZE:
                    return BigInteger.valueOf(Byte.toUnsignedInt(memory.getI8(address)));
                case LLVMDebugTypeConstants.SHORT_SIZE:
                    return BigInteger.valueOf(Short.toUnsignedInt(memory.getI16(address)));
                case LLVMDebugTypeConstants.INTEGER_SIZE:
                    return BigInteger.valueOf(Integer.toUnsignedLong(memory.getI32(address)));
            }
        }
    }
    final int paddingBefore = (int) (bitOffset % Byte.SIZE);
    int totalBitSize = bitSize + paddingBefore;
    int paddingAfter = totalBitSize % Byte.SIZE;
    if (paddingAfter != 0) {
        paddingAfter = Byte.SIZE - paddingAfter;
    }
    totalBitSize += paddingAfter;
    LLVMIVarBit var = memory.getIVarBit(baseAddress.increment(bitOffset / Byte.SIZE), totalBitSize);
    if (paddingAfter != 0) {
        var = var.leftShift(LLVMIVarBit.fromInt(Integer.SIZE, paddingAfter));
    }
    final int totalPadding = paddingBefore + paddingAfter;
    final LLVMIVarBit shiftRight = LLVMIVarBit.fromInt(Integer.SIZE, totalPadding);
    if (totalPadding != 0) {
        var = signed ? var.arithmeticRightShift(shiftRight) : var.logicalRightShift(shiftRight);
    }
    return signed ? var.asBigInteger() : var.asUnsignedBigInteger();
}
Also used : LLVMIVarBit(com.oracle.truffle.llvm.runtime.LLVMIVarBit) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Aggregations

TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)1 LLVMIVarBit (com.oracle.truffle.llvm.runtime.LLVMIVarBit)1