Search in sources :

Example 41 with TruffleBoundary

use of com.oracle.truffle.api.CompilerDirectives.TruffleBoundary in project graal by oracle.

the class Target_com_oracle_truffle_nfi_impl_NFIContext method loadLibrary.

@Substitute
@TruffleBoundary
static long loadLibrary(@SuppressWarnings("unused") long nativeContext, String name, int flags) {
    PointerBase ret = PosixUtils.dlopen(name, flags);
    if (ret.equal(WordFactory.zero())) {
        CompilerDirectives.transferToInterpreter();
        String error = PosixUtils.dlerror();
        throw new UnsatisfiedLinkError(error);
    }
    return ret.rawValue();
}
Also used : PointerBase(org.graalvm.word.PointerBase) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary) Substitute(com.oracle.svm.core.annotate.Substitute)

Example 42 with TruffleBoundary

use of com.oracle.truffle.api.CompilerDirectives.TruffleBoundary in project graal by oracle.

the class TruffleNFISupport method javaStringToUtf8.

@TruffleBoundary
static byte[] javaStringToUtf8(String str) {
    CharsetEncoder encoder = UTF8.newEncoder();
    int sizeEstimate = (int) (str.length() * encoder.averageBytesPerChar()) + 1;
    ByteBuffer retBuffer = ByteBuffer.allocate(sizeEstimate);
    CharBuffer input = CharBuffer.wrap(str);
    while (input.hasRemaining()) {
        CoderResult result = encoder.encode(input, retBuffer, true);
        if (result.isUnderflow()) {
            result = encoder.flush(retBuffer);
        }
        if (result.isUnderflow()) {
            break;
        }
        if (result.isOverflow()) {
            sizeEstimate = 2 * sizeEstimate + 1;
            ByteBuffer newBuffer = ByteBuffer.allocate(sizeEstimate);
            retBuffer.flip();
            newBuffer.put(retBuffer);
            retBuffer = newBuffer;
        } else {
            try {
                result.throwException();
            } catch (CharacterCodingException ex) {
                throw new RuntimeException(ex);
            }
        }
    }
    if (retBuffer.remaining() == 0) {
        ByteBuffer newBuffer = ByteBuffer.allocate(retBuffer.limit() + 1);
        newBuffer.put(retBuffer);
        retBuffer = newBuffer;
    }
    retBuffer.put((byte) 0);
    return retBuffer.array();
}
Also used : CharBuffer(java.nio.CharBuffer) CharacterCodingException(java.nio.charset.CharacterCodingException) CharsetEncoder(java.nio.charset.CharsetEncoder) ByteBuffer(java.nio.ByteBuffer) CoderResult(java.nio.charset.CoderResult) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Example 43 with TruffleBoundary

use of com.oracle.truffle.api.CompilerDirectives.TruffleBoundary in project sulong by graalvm.

the class LLVMSignal method setSignalHandler.

@TruffleBoundary
private static LLVMAddress setSignalHandler(LLVMContext context, Signal signal, LLVMAddress function) {
    int signalId = signal.getNumber();
    LLVMAddress returnFunction = context.getSigDfl();
    try {
        LLVMSignalHandler newSignalHandler = new LLVMSignalHandler(context, signal, function);
        synchronized (registeredSignals) {
            if (registeredSignals.containsKey(signalId)) {
                LLVMSignalHandler currentFunction = registeredSignals.get(signalId);
                if (currentFunction.isRunning()) {
                    returnFunction = currentFunction.getFunction();
                    /*
                         * the new signal handler already manages this signal, so we can safely
                         * deactivate the old one.
                         */
                    currentFunction.setStopped();
                }
            }
            registeredSignals.put(signalId, newSignalHandler);
        }
    } catch (IllegalArgumentException e) {
        System.err.println("could not register signal with id " + signalId + " (" + signal + ")");
        return context.getSigErr();
    }
    return returnFunction;
}
Also used : LLVMAddress(com.oracle.truffle.llvm.runtime.LLVMAddress) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Example 44 with TruffleBoundary

use of com.oracle.truffle.api.CompilerDirectives.TruffleBoundary 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)

Example 45 with TruffleBoundary

use of com.oracle.truffle.api.CompilerDirectives.TruffleBoundary in project sulong by graalvm.

the class DITypeExtractor method setAggregateProperties.

private static void setAggregateProperties(boolean isVector, LLVMSourceArrayLikeType aggregate, long length, LLVMSourceType baseType) {
    aggregate.setBaseType(baseType);
    final String nameFormatString;
    if (length < 0) {
        // this case happens for dynamically allocated arrays
        aggregate.setLength(0);
        nameFormatString = isVector ? "%s<?>" : "%s[?]";
    } else {
        aggregate.setLength(length);
        nameFormatString = isVector ? "%s<%d>" : "%s[%d]";
    }
    aggregate.setName(new Supplier<String>() {

        @Override
        @TruffleBoundary
        public String get() {
            String baseName = baseType.getName();
            if (baseName.contains(" ")) {
                baseName = String.format("(%s)", baseName);
            }
            return String.format(nameFormatString, baseName, length);
        }
    });
}
Also used : TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary) MDString(com.oracle.truffle.llvm.parser.metadata.MDString)

Aggregations

TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)49 RootNode (com.oracle.truffle.api.nodes.RootNode)6 Property (com.oracle.truffle.api.object.Property)6 Specialization (com.oracle.truffle.api.dsl.Specialization)5 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)5 BigInteger (java.math.BigInteger)4 Node (com.oracle.truffle.api.nodes.Node)3 SourceSection (com.oracle.truffle.api.source.SourceSection)3 ByteBuffer (java.nio.ByteBuffer)3 Substitute (com.oracle.svm.core.annotate.Substitute)2 FrameInstance (com.oracle.truffle.api.frame.FrameInstance)2 FrameSlot (com.oracle.truffle.api.frame.FrameSlot)2 NodeVisitor (com.oracle.truffle.api.nodes.NodeVisitor)2 Source (com.oracle.truffle.api.source.Source)2 LLVMContext (com.oracle.truffle.llvm.runtime.LLVMContext)2 SLRootNode (com.oracle.truffle.sl.nodes.SLRootNode)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 Assumption (com.oracle.truffle.api.Assumption)1 RootCallTarget (com.oracle.truffle.api.RootCallTarget)1