Skip to content

Commit

Permalink
Merge JNICallSignature into SimpleSignature.
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-hofer committed Jul 7, 2022
1 parent e4682d8 commit 4ff3399
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 127 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,35 @@
*/
package com.oracle.svm.hosted.code;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

import com.oracle.svm.core.SubstrateUtil;

import jdk.vm.ci.meta.JavaKind;
import jdk.vm.ci.meta.JavaType;
import jdk.vm.ci.meta.MetaAccessProvider;
import jdk.vm.ci.meta.ResolvedJavaType;
import jdk.vm.ci.meta.Signature;

/**
* A straightforward implementation of {@link Signature}.
*/
public class SimpleSignature implements Signature {
public static SimpleSignature fromKinds(JavaKind[] paramKinds, JavaKind returnKind, MetaAccessProvider metaAccess) {
ResolvedJavaType[] paramTypes = new ResolvedJavaType[paramKinds.length];
for (int i = 0; i < paramKinds.length; i++) {
paramTypes[i] = SimpleSignature.resolveType(paramKinds[i], metaAccess);
}
JavaType returnType = SimpleSignature.resolveType(returnKind, metaAccess);
return new SimpleSignature(paramTypes, returnType);
}

private static ResolvedJavaType resolveType(JavaKind kind, MetaAccessProvider metaAccess) {
return metaAccess.lookupJavaType(kind.isObject() ? Object.class : kind.toJavaClass());
}

private final JavaType[] parameterTypes;
private final JavaType returnType;

Expand All @@ -60,4 +79,38 @@ public JavaType getParameterType(int index, ResolvedJavaType accessingClass) {
public JavaType getReturnType(ResolvedJavaType accessingClass) {
return returnType;
}

public String getIdentifier() {
StringBuilder sb = new StringBuilder(1 + parameterTypes.length);
boolean digest = false;
for (JavaType type : parameterTypes) {
if (type.getJavaKind().isPrimitive() || (type instanceof ResolvedJavaType && ((ResolvedJavaType) type).isJavaLangObject())) {
sb.append(type.getJavaKind().getTypeChar());
} else {
sb.append(type.toClassName());
digest = true;
}
}
sb.append('_').append(returnType.getJavaKind().getTypeChar());
return digest ? SubstrateUtil.digest(sb.toString()) : sb.toString();
}

@Override
public boolean equals(Object obj) {
if (this != obj && obj instanceof SimpleSignature) {
var other = (SimpleSignature) obj;
return Arrays.equals(parameterTypes, other.parameterTypes) && Objects.equals(returnType, other.returnType);
}
return (this == obj);
}

@Override
public int hashCode() {
return Arrays.hashCode(parameterTypes) * 31 + Objects.hashCode(returnType);
}

@Override
public String toString() {
return getIdentifier();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@
import com.oracle.svm.hosted.ProgressReporter;
import com.oracle.svm.hosted.code.CEntryPointData;
import com.oracle.svm.hosted.code.FactoryMethodSupport;
import com.oracle.svm.hosted.code.SimpleSignature;
import com.oracle.svm.hosted.config.ConfigurationParserUtils;
import com.oracle.svm.hosted.meta.KnownOffsetsFeature;
import com.oracle.svm.hosted.meta.MaterializedConstantFields;
import com.oracle.svm.hosted.substitute.SubstitutionReflectivityFilter;
import com.oracle.svm.jni.JNIJavaCallTrampolines;
import com.oracle.svm.jni.hosted.JNICallSignature;
import com.oracle.svm.jni.hosted.JNICallTrampolineMethod;
import com.oracle.svm.jni.hosted.JNIFieldAccessorMethod;
import com.oracle.svm.jni.hosted.JNIJavaCallVariantWrapperMethod;
Expand Down Expand Up @@ -110,9 +110,9 @@ static final class JNIJavaCallVariantWrapperGroup {

private boolean sealed = false;
private final Map<String, JNICallTrampolineMethod> trampolineMethods = new ConcurrentHashMap<>();
private final Map<JNICallSignature, JNIJavaCallWrapperMethod> javaCallWrapperMethods = new ConcurrentHashMap<>();
private final Map<JNICallSignature, JNIJavaCallVariantWrapperGroup> callVariantWrappers = new ConcurrentHashMap<>();
private final Map<JNICallSignature, JNIJavaCallVariantWrapperGroup> nonvirtualCallVariantWrappers = new ConcurrentHashMap<>();
private final Map<SimpleSignature, JNIJavaCallWrapperMethod> javaCallWrapperMethods = new ConcurrentHashMap<>();
private final Map<SimpleSignature, JNIJavaCallVariantWrapperGroup> callVariantWrappers = new ConcurrentHashMap<>();
private final Map<SimpleSignature, JNIJavaCallVariantWrapperGroup> nonvirtualCallVariantWrappers = new ConcurrentHashMap<>();

private int loadedConfigurations;

Expand Down Expand Up @@ -327,7 +327,7 @@ private void addMethod(Executable method, DuringAnalysisAccessImpl access) {
newObjectMethod = aFactoryMethod.getWrapped();
}

JNICallSignature compatibleSignature = JNIJavaCallWrapperMethod.getGeneralizedSignatureForTarget(targetMethod, originalMetaAccess);
SimpleSignature compatibleSignature = JNIJavaCallWrapperMethod.getGeneralizedSignatureForTarget(targetMethod, originalMetaAccess);
JNIJavaCallWrapperMethod callWrapperMethod = javaCallWrapperMethods.computeIfAbsent(compatibleSignature,
signature -> factory.create(signature, originalMetaAccess, access.getBigBang().getProviders().getWordTypes()));
access.registerAsRoot(universe.lookup(callWrapperMethod), true);
Expand All @@ -343,7 +343,7 @@ private void addMethod(Executable method, DuringAnalysisAccessImpl access) {
});
}

private JNIJavaCallVariantWrapperGroup createJavaCallVariantWrappers(DuringAnalysisAccessImpl access, JNICallSignature wrapperSignature, boolean nonVirtual) {
private JNIJavaCallVariantWrapperGroup createJavaCallVariantWrappers(DuringAnalysisAccessImpl access, SimpleSignature wrapperSignature, boolean nonVirtual) {
var map = nonVirtual ? nonvirtualCallVariantWrappers : callVariantWrappers;
return map.computeIfAbsent(wrapperSignature, signature -> {
MetaAccessProvider originalMetaAccess = access.getUniverse().getOriginalMetaAccess();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import com.oracle.svm.core.graal.nodes.VaListNextArgNode;
import com.oracle.svm.core.util.VMError;
import com.oracle.svm.hosted.code.EntryPointCallStubMethod;
import com.oracle.svm.hosted.code.SimpleSignature;
import com.oracle.svm.hosted.meta.HostedMetaAccess;
import com.oracle.svm.jni.JNIJavaCallVariantWrappers;

Expand All @@ -87,7 +88,7 @@ public enum CallVariant {
private final CallVariant callVariant;
private final boolean nonVirtual;

public JNIJavaCallVariantWrapperMethod(JNICallSignature callWrapperSignature, CallVariant callVariant, boolean nonVirtual, MetaAccessProvider originalMetaAccess, WordTypes wordTypes) {
public JNIJavaCallVariantWrapperMethod(SimpleSignature callWrapperSignature, CallVariant callVariant, boolean nonVirtual, MetaAccessProvider originalMetaAccess, WordTypes wordTypes) {
super(createName(callWrapperSignature, callVariant, nonVirtual),
originalMetaAccess.lookupJavaType(JNIJavaCallVariantWrappers.class),
createSignature(callWrapperSignature, callVariant, nonVirtual, originalMetaAccess, wordTypes),
Expand All @@ -97,7 +98,7 @@ public JNIJavaCallVariantWrapperMethod(JNICallSignature callWrapperSignature, Ca
this.nonVirtual = nonVirtual;
}

private static String createName(JNICallSignature targetSignature, CallVariant callVariant, boolean nonVirtual) {
private static String createName(SimpleSignature targetSignature, CallVariant callVariant, boolean nonVirtual) {
return "invoke" + targetSignature.getIdentifier() + "_" + callVariant.name() + (nonVirtual ? "_Nonvirtual" : "");
}

Expand Down Expand Up @@ -134,7 +135,7 @@ private static Signature createSignature(Signature callWrapperSignature, CallVar
if (returnType.isObject()) {
returnType = wordKind; // handle
}
return new JNICallSignature(args.toArray(JavaKind[]::new), returnType, originalMetaAccess);
return SimpleSignature.fromKinds(args.toArray(JavaKind[]::new), returnType, originalMetaAccess);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import com.oracle.svm.core.graal.nodes.LoweredDeadEndNode;
import com.oracle.svm.hosted.code.FactoryMethodSupport;
import com.oracle.svm.hosted.code.NonBytecodeStaticMethod;
import com.oracle.svm.hosted.code.SimpleSignature;
import com.oracle.svm.hosted.meta.HostedMetaAccess;
import com.oracle.svm.jni.JNIJavaCallWrappers;
import com.oracle.svm.jni.access.JNIAccessibleMethod;
Expand Down Expand Up @@ -89,7 +90,7 @@ public class JNIJavaCallWrapperMethod extends NonBytecodeStaticMethod {
private static final Constructor<InstantiationException> INSTANTIATION_EXCEPTION_CONSTRUCTOR = ReflectionUtil.lookupConstructor(InstantiationException.class);

public static class Factory {
public JNIJavaCallWrapperMethod create(JNICallSignature targetSignature, MetaAccessProvider originalMetaAccess, WordTypes wordTypes) {
public JNIJavaCallWrapperMethod create(SimpleSignature targetSignature, MetaAccessProvider originalMetaAccess, WordTypes wordTypes) {
return new JNIJavaCallWrapperMethod(targetSignature, originalMetaAccess, wordTypes);
}

Expand All @@ -99,7 +100,7 @@ public boolean canInvokeConstructorOnObject(ResolvedJavaMethod constructor, Meta
}
}

public static JNICallSignature getGeneralizedSignatureForTarget(ResolvedJavaMethod targetMethod, MetaAccessProvider originalMetaAccess) {
public static SimpleSignature getGeneralizedSignatureForTarget(ResolvedJavaMethod targetMethod, MetaAccessProvider originalMetaAccess) {
JavaType[] paramTypes = targetMethod.getSignature().toParameterTypes(null);
// Note: our parameters do not include the receiver, but we can do a type check based on the
// JNIAccessibleMethod object we get from the method id.
Expand All @@ -114,18 +115,18 @@ public static JNICallSignature getGeneralizedSignatureForTarget(ResolvedJavaMeth
// Note: no need to distinguish between object return types for us, the return value must
// match in Java code and we return it as handle anyway.
JavaType returnType = originalMetaAccess.lookupJavaType(returnKind.isObject() ? Object.class : returnKind.toJavaClass());
return new JNICallSignature(paramTypes, returnType);
return new SimpleSignature(paramTypes, returnType);
}

private final Signature targetSignature;

protected JNIJavaCallWrapperMethod(JNICallSignature targetSignature, MetaAccessProvider metaAccess, WordTypes wordTypes) {
protected JNIJavaCallWrapperMethod(SimpleSignature targetSignature, MetaAccessProvider metaAccess, WordTypes wordTypes) {
super("invoke_" + targetSignature.getIdentifier(), metaAccess.lookupJavaType(JNIJavaCallWrappers.class),
createSignature(targetSignature, metaAccess, wordTypes), JNIJavaCallWrappers.getConstantPool(metaAccess));
this.targetSignature = targetSignature;
}

private static JNICallSignature createSignature(Signature targetSignature, MetaAccessProvider originalMetaAccess, WordTypes wordTypes) {
private static SimpleSignature createSignature(Signature targetSignature, MetaAccessProvider originalMetaAccess, WordTypes wordTypes) {
JavaKind wordKind = wordTypes.getWordKind();
int count = targetSignature.getParameterCount(false);
JavaKind[] args = new JavaKind[3 + count];
Expand All @@ -151,12 +152,12 @@ private static JNICallSignature createSignature(Signature targetSignature, MetaA
if (returnKind.isObject()) {
returnKind = wordKind; // handle
}
return new JNICallSignature(args, returnKind, originalMetaAccess);
return SimpleSignature.fromKinds(args, returnKind, originalMetaAccess);
}

@Override
public JNICallSignature getSignature() {
return (JNICallSignature) super.getSignature();
public SimpleSignature getSignature() {
return (SimpleSignature) super.getSignature();
}

@Override
Expand Down

0 comments on commit 4ff3399

Please sign in to comment.