use of jdk.vm.ci.meta.JavaType in project graal by oracle.
the class GraalCompilerTest method bindArguments.
protected void bindArguments(StructuredGraph graph, Object[] argsToBind) {
ResolvedJavaMethod m = graph.method();
Object receiver = isStatic(m.getModifiers()) ? null : this;
Object[] args = argsWithReceiver(receiver, argsToBind);
JavaType[] parameterTypes = m.toParameterTypes();
assert parameterTypes.length == args.length;
for (ParameterNode param : graph.getNodes(ParameterNode.TYPE)) {
JavaConstant c = getSnippetReflection().forBoxed(parameterTypes[param.index()].getJavaKind(), args[param.index()]);
ConstantNode replacement = ConstantNode.forConstant(c, getMetaAccess(), graph);
param.replaceAtUsages(replacement);
}
}
use of jdk.vm.ci.meta.JavaType in project graal by oracle.
the class GraalCompilerTest method checkArgs.
protected void checkArgs(ResolvedJavaMethod method, Object[] args) {
JavaType[] sig = method.toParameterTypes();
Assert.assertEquals(sig.length, args.length);
for (int i = 0; i < args.length; i++) {
JavaType javaType = sig[i];
JavaKind kind = javaType.getJavaKind();
Object arg = args[i];
if (kind == JavaKind.Object) {
if (arg != null && javaType instanceof ResolvedJavaType) {
ResolvedJavaType resolvedJavaType = (ResolvedJavaType) javaType;
Assert.assertTrue(resolvedJavaType + " from " + getMetaAccess().lookupJavaType(arg.getClass()), resolvedJavaType.isAssignableFrom(getMetaAccess().lookupJavaType(arg.getClass())));
}
} else {
Assert.assertNotNull(arg);
Assert.assertEquals(kind.toBoxedJavaClass(), arg.getClass());
}
}
}
use of jdk.vm.ci.meta.JavaType in project graal by oracle.
the class InjectedAccessorsPlugin method handleField.
private static boolean handleField(GraphBuilderContext b, ResolvedJavaField field, boolean isStatic, ValueNode receiver, boolean isGet, ValueNode value) {
InjectAccessors injectAccesors = field.getAnnotation(InjectAccessors.class);
if (injectAccesors == null) {
return false;
}
Class<?> accessorsClass = injectAccesors.value();
ResolvedJavaType accessorsType = b.getMetaAccess().lookupJavaType(accessorsClass);
String shortName = isGet ? "get" : "set";
String fieldName = field.getName();
String longName = shortName + Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1);
ResolvedJavaMethod foundMethod = null;
for (ResolvedJavaMethod method : accessorsType.getDeclaredMethods()) {
if (method.getName().equals(shortName) || method.getName().equals(longName)) {
if (foundMethod != null) {
error(field, accessorsType, null, "found two methods " + foundMethod.format("%n(%p)") + " and " + method.format("%n(%p)"));
}
foundMethod = method;
}
}
if (foundMethod == null) {
error(field, accessorsType, null, "found no method named " + shortName + " or " + longName);
}
if (!foundMethod.isStatic()) {
error(field, accessorsType, foundMethod, "method is not static");
}
int paramIdx = 0;
if (!isStatic) {
if (foundMethod.getSignature().getParameterCount(false) < paramIdx + 1) {
error(field, accessorsType, foundMethod, "not enough parameters");
}
JavaType actualReceiver = foundMethod.getSignature().getParameterType(paramIdx, null);
ResolvedJavaType expectedReceiver = field.getDeclaringClass();
if (!actualReceiver.equals(expectedReceiver)) {
error(field, accessorsType, foundMethod, "wrong receiver type: expected " + expectedReceiver.toJavaName(true) + ", found " + actualReceiver.toJavaName(true));
}
paramIdx++;
}
JavaType expectedValue = field.getType();
if (isGet) {
JavaType actualValue = foundMethod.getSignature().getReturnType(null);
if (!actualValue.equals(expectedValue)) {
error(field, accessorsType, foundMethod, "wrong return type: expected " + expectedValue.toJavaName(true) + ", found " + actualValue.toJavaName(true));
}
} else {
if (foundMethod.getSignature().getParameterCount(false) < paramIdx + 1) {
error(field, accessorsType, foundMethod, "not enough parameters");
}
JavaType actualValue = foundMethod.getSignature().getParameterType(paramIdx, null);
if (!actualValue.equals(expectedValue)) {
error(field, accessorsType, foundMethod, "wrong value type: expected " + expectedValue.toJavaName(true) + ", found " + actualValue.toJavaName(true));
}
paramIdx++;
}
if (foundMethod.getSignature().getParameterCount(false) != paramIdx) {
error(field, accessorsType, foundMethod, "Wrong number of parameters: expected " + paramIdx + ", found " + foundMethod.getSignature().getParameterCount(false));
}
List<ValueNode> args = new ArrayList<>();
if (!isStatic) {
args.add(receiver);
}
if (!isGet) {
args.add(value);
}
b.handleReplacedInvoke(InvokeKind.Static, foundMethod, args.toArray(new ValueNode[args.size()]), false);
return true;
}
use of jdk.vm.ci.meta.JavaType in project graal by oracle.
the class JNIJavaCallWrapperMethod method createSignature.
private JNISignature createSignature(MetaAccessProvider metaAccess) {
ResolvedJavaType objectHandle = metaAccess.lookupJavaType(JNIObjectHandle.class);
List<JavaType> args = new ArrayList<>();
args.add(metaAccess.lookupJavaType(JNIEnvironment.class));
// this (instance method) or class (static method)
args.add(objectHandle);
if (nonVirtual) {
// class of implementation to invoke
args.add(objectHandle);
}
args.add(metaAccess.lookupJavaType(JNIMethodId.class));
Signature targetSignature = targetMethod.getSignature();
if (callVariant == CallVariant.VARARGS) {
for (JavaType targetArg : targetSignature.toParameterTypes(null)) {
JavaKind kind = targetArg.getJavaKind();
if (kind.isObject()) {
args.add(objectHandle);
} else if (kind == JavaKind.Float) {
// C varargs promote float to double
args.add(metaAccess.lookupJavaType(JavaKind.Double.toJavaClass()));
} else {
args.add(targetArg);
}
}
} else if (callVariant == CallVariant.ARRAY) {
// const jvalue *
args.add(metaAccess.lookupJavaType(JNIValue.class));
} else if (callVariant == CallVariant.VA_LIST) {
// va_list (a pointer of some kind)
args.add(metaAccess.lookupJavaType(WordBase.class));
} else {
throw VMError.shouldNotReachHere();
}
JavaType returnType = targetSignature.getReturnType(null);
if (returnType.getJavaKind().isObject() || targetMethod.isConstructor()) {
// Constructor: returns `this` to implement NewObject
returnType = objectHandle;
}
return new JNISignature(args, returnType);
}
use of jdk.vm.ci.meta.JavaType in project graal by oracle.
the class JNINativeCallWrapperMethod method buildGraph.
@Override
public StructuredGraph buildGraph(DebugContext debug, ResolvedJavaMethod method, HostedProviders providers, Purpose purpose) {
JNIGraphKit kit = new JNIGraphKit(debug, providers, method);
StructuredGraph graph = kit.getGraph();
InvokeWithExceptionNode handleFrame = kit.nativeCallPrologue();
ValueNode callAddress = kit.nativeCallAddress(kit.createObject(linkage));
ValueNode environment = kit.environment();
JavaType javaReturnType = method.getSignature().getReturnType(null);
JavaType[] javaArgumentTypes = method.toParameterTypes();
List<ValueNode> javaArguments = kit.loadArguments(javaArgumentTypes);
List<ValueNode> jniArguments = new ArrayList<>(2 + javaArguments.size());
List<JavaType> jniArgumentTypes = new ArrayList<>(jniArguments.size());
JavaType environmentType = providers.getMetaAccess().lookupJavaType(JNIEnvironment.class);
JavaType objectHandleType = providers.getMetaAccess().lookupJavaType(JNIObjectHandle.class);
jniArguments.add(environment);
jniArgumentTypes.add(environmentType);
if (method.isStatic()) {
JavaConstant clazz = providers.getConstantReflection().asJavaClass(method.getDeclaringClass());
ConstantNode clazzNode = ConstantNode.forConstant(clazz, providers.getMetaAccess(), graph);
ValueNode box = kit.boxObjectInLocalHandle(clazzNode);
jniArguments.add(box);
jniArgumentTypes.add(objectHandleType);
}
for (int i = 0; i < javaArguments.size(); i++) {
ValueNode arg = javaArguments.get(i);
JavaType argType = javaArgumentTypes[i];
if (javaArgumentTypes[i].getJavaKind().isObject()) {
ValueNode obj = javaArguments.get(i);
arg = kit.boxObjectInLocalHandle(obj);
argType = objectHandleType;
}
jniArguments.add(arg);
jniArgumentTypes.add(argType);
}
assert jniArguments.size() == jniArgumentTypes.size();
JavaType jniReturnType = javaReturnType;
if (jniReturnType.getJavaKind().isObject()) {
jniReturnType = objectHandleType;
}
if (getOriginal().isSynchronized()) {
ValueNode monitorObject;
if (method.isStatic()) {
Constant hubConstant = providers.getConstantReflection().asObjectHub(method.getDeclaringClass());
DynamicHub hub = (DynamicHub) SubstrateObjectConstant.asObject(hubConstant);
monitorObject = ConstantNode.forConstant(SubstrateObjectConstant.forObject(hub), providers.getMetaAccess(), graph);
} else {
monitorObject = javaArguments.get(0);
}
MonitorIdNode monitorId = graph.add(new MonitorIdNode(kit.getFrameState().lockDepth(false)));
MonitorEnterNode monitorEnter = kit.append(new MonitorEnterNode(monitorObject, monitorId));
kit.getFrameState().pushLock(monitorEnter.object(), monitorEnter.getMonitorId());
monitorEnter.setStateAfter(kit.getFrameState().create(kit.bci(), monitorEnter));
}
kit.getFrameState().clearLocals();
Signature jniSignature = new JNISignature(jniArgumentTypes, jniReturnType);
ValueNode returnValue = kit.createCFunctionCall(callAddress, method, jniArguments, jniSignature, true, false);
if (getOriginal().isSynchronized()) {
MonitorIdNode monitorId = kit.getFrameState().peekMonitorId();
ValueNode monitorObject = kit.getFrameState().popLock();
MonitorExitNode monitorExit = kit.append(new MonitorExitNode(monitorObject, monitorId, null));
monitorExit.setStateAfter(kit.getFrameState().create(kit.bci(), monitorExit));
}
if (javaReturnType.getJavaKind().isObject()) {
// before destroying handles in epilogue
returnValue = kit.unboxHandle(returnValue);
}
kit.nativeCallEpilogue(handleFrame);
kit.rethrowPendingException();
if (javaReturnType.getJavaKind().isObject()) {
// Just before return to always run the epilogue and never suppress a pending exception
returnValue = castObject(kit, returnValue, (ResolvedJavaType) javaReturnType);
}
kit.createReturn(returnValue, javaReturnType.getJavaKind());
kit.mergeUnwinds();
assert graph.verify();
return graph;
}
Aggregations