use of com.oracle.truffle.api.dsl.Specialization in project graal by oracle.
the class ExecuteMethodNode method doVarArgs.
@SuppressWarnings("unused")
@Specialization(guards = { "method.isVarArgs()", "method == cachedMethod" }, limit = "LIMIT")
Object doVarArgs(SingleMethodDesc method, Object obj, Object[] args, Object languageContext, @Cached("method") SingleMethodDesc cachedMethod, @Cached("create()") ToJavaNode toJavaNode, @Cached("createClassProfile()") ValueProfile receiverProfile) {
int parameterCount = cachedMethod.getParameterCount();
int minArity = parameterCount - 1;
if (args.length < minArity) {
throw ArityException.raise(minArity, args.length);
}
Class<?>[] types = cachedMethod.getParameterTypes();
Type[] genericTypes = cachedMethod.getGenericParameterTypes();
Object[] convertedArguments = new Object[args.length];
for (int i = 0; i < minArity; i++) {
convertedArguments[i] = toJavaNode.execute(args[i], types[i], genericTypes[i], languageContext);
}
if (asVarArgs(args, cachedMethod)) {
for (int i = minArity; i < args.length; i++) {
Class<?> expectedType = types[minArity].getComponentType();
Type expectedGenericType = getGenericComponentType(genericTypes[minArity]);
convertedArguments[i] = toJavaNode.execute(args[i], expectedType, expectedGenericType, languageContext);
}
convertedArguments = createVarArgsArray(cachedMethod, convertedArguments, parameterCount);
} else {
convertedArguments[minArity] = toJavaNode.execute(args[minArity], types[minArity], genericTypes[minArity], languageContext);
}
return doInvoke(cachedMethod, receiverProfile.profile(obj), convertedArguments, languageContext);
}
use of com.oracle.truffle.api.dsl.Specialization in project graal by oracle.
the class ExecuteMethodNode method doOverloadedCached.
// Note: checkArgTypes must be evaluated after selectOverload.
@SuppressWarnings("unused")
@ExplodeLoop
@Specialization(guards = { "method == cachedMethod", "checkArgTypes(args, cachedArgTypes, toJavaNode, asVarArgs)" }, limit = "LIMIT")
Object doOverloadedCached(OverloadedMethodDesc method, Object obj, Object[] args, Object languageContext, @Cached("method") OverloadedMethodDesc cachedMethod, @Cached("create()") ToJavaNode toJavaNode, @Cached(value = "createArgTypesArray(args)", dimensions = 1) Type[] cachedArgTypes, @Cached("selectOverload(method, args, languageContext, toJavaNode, cachedArgTypes)") SingleMethodDesc overload, @Cached("asVarArgs(args, overload)") boolean asVarArgs, @Cached("createClassProfile()") ValueProfile receiverProfile) {
assert overload == selectOverload(method, args, languageContext, toJavaNode);
Class<?>[] types = overload.getParameterTypes();
Type[] genericTypes = overload.getGenericParameterTypes();
Object[] convertedArguments = new Object[cachedArgTypes.length];
if (asVarArgs) {
assert overload.isVarArgs();
int parameterCount = overload.getParameterCount();
for (int i = 0; i < cachedArgTypes.length; i++) {
Class<?> expectedType = i < parameterCount - 1 ? types[i] : types[parameterCount - 1].getComponentType();
Type expectedGenericType = i < parameterCount - 1 ? genericTypes[i] : getGenericComponentType(genericTypes[parameterCount - 1]);
convertedArguments[i] = toJavaNode.execute(args[i], expectedType, expectedGenericType, languageContext);
}
convertedArguments = createVarArgsArray(overload, convertedArguments, parameterCount);
} else {
for (int i = 0; i < cachedArgTypes.length; i++) {
convertedArguments[i] = toJavaNode.execute(args[i], types[i], genericTypes[i], languageContext);
}
}
return doInvoke(overload, receiverProfile.profile(obj), convertedArguments, languageContext);
}
use of com.oracle.truffle.api.dsl.Specialization in project sulong by graalvm.
the class LLVMTruffleReadNBytes method interop.
@Specialization
protected Object interop(LLVMTruffleObject objectWithOffset, int n, @Cached("createForeignReadNode()") Node foreignRead, @Cached("createToByteNode()") ForeignToLLVM toLLVM, @Cached("getContextReference()") ContextReference<LLVMContext> ctxRef) {
long offset = objectWithOffset.getOffset();
TruffleObject object = objectWithOffset.getObject();
byte[] chars = new byte[n];
for (int i = 0; i < n; i++) {
Object rawValue;
try {
rawValue = ForeignAccess.sendRead(foreignRead, object, offset + i);
} catch (UnknownIdentifierException | UnsupportedMessageException e) {
CompilerDirectives.transferToInterpreter();
throw new IllegalStateException(e);
}
chars[i] = (byte) toLLVM.executeWithTarget(rawValue);
}
TruffleObject ret = (TruffleObject) ctxRef.get().getEnv().asGuestValue(chars);
return new LLVMTruffleObject(LLVMTypedForeignObject.createUnknown(ret));
}
use of com.oracle.truffle.api.dsl.Specialization in project sulong by graalvm.
the class LLVMTruffleReadBytes method doIntrinsic.
@Specialization
protected Object doIntrinsic(LLVMAddress value, @Cached("getLLVMMemory()") LLVMMemory memory, @Cached("getContextReference()") ContextReference<LLVMContext> ctxRef) {
byte c;
long ptr = value.getVal();
int count = 0;
while ((c = memory.getI8(ptr)) != 0) {
count++;
ptr += Byte.BYTES;
}
byte[] bytes = new byte[count];
count = 0;
ptr = value.getVal();
while ((c = memory.getI8(ptr)) != 0) {
bytes[count++] = c;
ptr += Byte.BYTES;
}
TruffleObject ret = (TruffleObject) ctxRef.get().getEnv().asGuestValue(bytes);
return new LLVMTruffleObject(LLVMTypedForeignObject.createUnknown(ret));
}
use of com.oracle.truffle.api.dsl.Specialization in project sulong by graalvm.
the class LLVMNativeDispatchNode method doCached.
@Specialization(guards = "function.getVal() == cachedFunction.getVal()")
@SuppressWarnings("unused")
protected Object doCached(LLVMAddress function, Object[] arguments, @Cached("getContextReference()") ContextReference<LLVMContext> context, @Cached("function") LLVMAddress cachedFunction, @Cached("identityFunction()") TruffleObject identity, @Cached("dispatchIdentity(identity, cachedFunction.getVal())") TruffleObject nativeFunctionHandle, @Cached("createToNativeNodes()") LLVMNativeConvertNode[] toNative, @Cached("createFromNativeNode()") LLVMNativeConvertNode fromNative, @Cached("nativeCallStatisticsEnabled(context)") boolean statistics) {
Object[] nativeArgs = prepareNativeArguments(arguments, toNative);
Object returnValue;
try (StackPointer save = ((StackPointer) arguments[0]).newFrame()) {
returnValue = LLVMNativeCallUtils.callNativeFunction(statistics, context, nativeCallNode, nativeFunctionHandle, nativeArgs, null);
}
return fromNative.executeConvert(returnValue);
}
Aggregations