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();
}
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();
}
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;
}
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();
}
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);
}
});
}
Aggregations