use of jdk.vm.ci.meta.ResolvedJavaType 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.ResolvedJavaType in project graal by oracle.
the class StrengthenStampsPhase method strengthenStamp.
private Stamp strengthenStamp(ValueNode node, JavaTypeProfile typeProfile) {
ObjectStamp oldStamp = (ObjectStamp) node.stamp(NodeView.DEFAULT);
HostedType oldType = toHosted(oldStamp.type());
if (oldStamp.alwaysNull()) {
/* We cannot make that more precise. */
return oldStamp;
}
boolean nonNull = oldStamp.nonNull() || typeProfile.getNullSeen() == TriState.FALSE;
ProfiledType[] exactTypes = typeProfile.getTypes();
if (exactTypes.length == 1) {
ResolvedJavaType exactType = exactTypes[0].getType();
assert oldType == null || oldType.isAssignableFrom(exactType);
if (!oldStamp.isExactType() || !exactType.equals(oldType) || nonNull != oldStamp.nonNull()) {
TypeReference typeRef = TypeReference.createExactTrusted(toTarget(exactType));
return nonNull ? StampFactory.objectNonNull(typeRef) : StampFactory.object(typeRef);
} else {
return oldStamp;
}
}
if (exactTypes.length == 0) {
if (!nonNull) {
return StampFactory.alwaysNull();
} else {
/*
* The code after the node is unreachable. We just insert a always-failing guard
* after the node and let dead code elimination remove everything after the node.
*/
StructuredGraph graph = node.graph();
FixedWithNextNode insertionPoint;
if (node instanceof ParameterNode) {
/* The whole method is unreachable. */
insertionPoint = graph.start();
} else if (node instanceof InvokeWithExceptionNode) {
/* The invoked method never returns normally (but can throw an exception). */
insertionPoint = ((InvokeWithExceptionNode) node).next();
} else {
insertionPoint = (FixedWithNextNode) node;
}
graph.addAfterFixed(insertionPoint, graph.add(new FixedGuardNode(LogicConstantNode.forBoolean(true, graph), DeoptimizationReason.UnreachedCode, DeoptimizationAction.None, true)));
return oldStamp;
}
}
ResolvedJavaType baseType;
if (oldStamp.isExactType()) {
/* Base type cannot be more precise. */
baseType = oldType;
} else {
assert exactTypes.length > 1;
assert oldType == null || oldType.isAssignableFrom(exactTypes[0].getType());
baseType = exactTypes[0].getType();
for (int i = 1; i < exactTypes.length; i++) {
assert oldType == null || oldType.isAssignableFrom(exactTypes[i].getType());
baseType = baseType.findLeastCommonAncestor(exactTypes[i].getType());
}
if (oldType != null && !oldType.isAssignableFrom(baseType)) {
/*
* When the original stamp is an interface type, we do not want to weaken that type
* with the common base class of all implementation types (which could even be
* java.lang.Object).
*/
baseType = oldType;
}
}
if (!baseType.equals(oldType) || nonNull != oldStamp.nonNull()) {
TypeReference typeRef = TypeReference.createTrustedWithoutAssumptions(toTarget(baseType));
return nonNull ? StampFactory.objectNonNull(typeRef) : StampFactory.object(typeRef);
}
return oldStamp;
}
use of jdk.vm.ci.meta.ResolvedJavaType in project graal by oracle.
the class AssertTypeStateNode method alwaysHolds.
@Override
protected boolean alwaysHolds(boolean reportError) {
if (getInput().isConstant()) {
if (getInput().asJavaConstant().isNull()) {
if (typeState.getNullSeen() == TriState.FALSE) {
if (reportError) {
throw shouldNotReachHere("Null constant not compatible with type state: " + this + " : " + getTypeState());
}
} else {
return true;
}
} else {
Set<ResolvedJavaType> ourTypes = new HashSet<>();
addAllTypes(ourTypes, typeState);
if (!ourTypes.contains(StampTool.typeOrNull(getInput()))) {
if (reportError) {
throw shouldNotReachHere("Constant object not compatible with type state: " + this + " : " + getTypeState() + ", " + StampTool.typeOrNull(getInput()));
}
} else {
return true;
}
}
}
if (getInput() instanceof AssertTypeStateNode) {
JavaTypeProfile inputTypeState = ((AssertTypeStateNode) getInput()).getTypeState();
Set<ResolvedJavaType> inputTypes = new HashSet<>();
addAllTypes(inputTypes, inputTypeState);
Set<ResolvedJavaType> ourTypes = new HashSet<>();
addAllTypes(ourTypes, typeState);
if (ourTypes.containsAll(inputTypes)) {
/* Another node is already checking the same or a stronger type state. */
return true;
}
}
return false;
}
use of jdk.vm.ci.meta.ResolvedJavaType in project graal by oracle.
the class PEReadEliminationClosure method processUnsafeStore.
private boolean processUnsafeStore(RawStoreNode store, PEReadEliminationBlockState state, GraphEffectList effects) {
ResolvedJavaType type = StampTool.typeOrNull(store.object());
if (type != null && type.isArray()) {
JavaKind accessKind = store.accessKind();
JavaKind componentKind = type.getComponentType().getJavaKind();
LocationIdentity location = NamedLocationIdentity.getArrayLocation(componentKind);
if (store.offset().isConstant()) {
long offset = store.offset().asJavaConstant().asLong();
boolean overflowAccess = isOverflowAccess(accessKind, componentKind);
int index = overflowAccess ? -1 : VirtualArrayNode.entryIndexForOffset(tool.getArrayOffsetProvider(), offset, accessKind, type.getComponentType(), Integer.MAX_VALUE);
return processStore(store, store.object(), location, index, accessKind, overflowAccess, store.value(), state, effects);
} else {
processIdentity(state, location);
}
} else {
state.killReadCache();
}
return false;
}
use of jdk.vm.ci.meta.ResolvedJavaType in project graal by oracle.
the class SubstrateGraphBuilderPlugins method registerArraysPlugins.
private static void registerArraysPlugins(InvocationPlugins plugins, boolean analysis) {
Registration r = new Registration(plugins, Arrays.class).setAllowOverwrite(true);
r.register2("copyOf", Object[].class, int.class, new InvocationPlugin() {
@Override
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode original, ValueNode newLength) {
if (analysis) {
b.addPush(JavaKind.Object, new AnalysisArraysCopyOfNode(b.getInvokeReturnStamp(b.getAssumptions()).getTrustedStamp(), original, newLength));
} else {
/* Get the class from the original node. */
GetClassNode originalArrayType = b.add(new GetClassNode(original.stamp(NodeView.DEFAULT), b.nullCheckedValue(original)));
ValueNode originalLength = b.add(ArrayLengthNode.create(original, b.getConstantReflection()));
Stamp stamp = b.getInvokeReturnStamp(b.getAssumptions()).getTrustedStamp().join(original.stamp(NodeView.DEFAULT));
b.addPush(JavaKind.Object, new SubstrateArraysCopyOfNode(stamp, original, originalLength, newLength, originalArrayType));
}
return true;
}
});
r.register3("copyOf", Object[].class, int.class, Class.class, new InvocationPlugin() {
@Override
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode original, ValueNode newLength, ValueNode newArrayType) {
if (analysis) {
/*
* If the new array type comes from a GetClassNode or is a constant we can infer
* the concrete type of the new array, otherwise we conservatively assume that
* the new array can have any of the instantiated array types.
*/
b.addPush(JavaKind.Object, new AnalysisArraysCopyOfNode(b.getInvokeReturnStamp(b.getAssumptions()).getTrustedStamp(), original, newLength, newArrayType));
} else {
Stamp stamp;
if (newArrayType.isConstant()) {
ResolvedJavaType newType = b.getConstantReflection().asJavaType(newArrayType.asConstant());
stamp = StampFactory.objectNonNull(TypeReference.createExactTrusted(newType));
} else {
stamp = b.getInvokeReturnStamp(b.getAssumptions()).getTrustedStamp();
}
ValueNode originalLength = b.add(ArrayLengthNode.create(original, b.getConstantReflection()));
b.addPush(JavaKind.Object, new SubstrateArraysCopyOfNode(stamp, original, originalLength, newLength, newArrayType));
}
return true;
}
});
}
Aggregations