Skip to content

Commit

Permalink
[GR-60035] Replace old cast implementation with ToEspressoNode.
Browse files Browse the repository at this point in the history
PullRequest: graal/19640
  • Loading branch information
javeleon committed Dec 20, 2024
2 parents f2f6136 + b0e2b3c commit 34121af
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 223 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.oracle.truffle.api.nodes.NodeInfo;
import com.oracle.truffle.api.profiles.BranchProfile;
import com.oracle.truffle.espresso.meta.EspressoError;
import com.oracle.truffle.espresso.runtime.dispatch.staticobject.EspressoInterop;
import com.oracle.truffle.espresso.runtime.staticobject.StaticObject;

/**
Expand Down Expand Up @@ -102,8 +103,13 @@ int doHost(Integer value) {
@Specialization
int doEspresso(StaticObject value,
@Cached BranchProfile exceptionProfile) throws UnsupportedTypeException {
if (value != null && !StaticObject.isNull(value) && value.getKlass() == getMeta().java_lang_Integer) {
return (int) getMeta().java_lang_Integer_value.get(value);
if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInInt(value)) {
try {
return EspressoInterop.asInt(value);
} catch (UnsupportedMessageException e) {
CompilerDirectives.transferToInterpreterAndInvalidate();
throw EspressoError.shouldNotReachHere("Contract violation: if fitsInInt returns true, asInt must succeed.");
}
}
exceptionProfile.enter();
throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to int"));
Expand Down Expand Up @@ -148,8 +154,13 @@ byte doHost(Byte value) {
@Specialization
byte doEspresso(StaticObject value,
@Cached BranchProfile exceptionProfile) throws UnsupportedTypeException {
if (value != null && !StaticObject.isNull(value) && value.getKlass() == getMeta().java_lang_Byte) {
return (byte) getMeta().java_lang_Byte_value.get(value);
if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInByte(value)) {
try {
return EspressoInterop.asByte(value);
} catch (UnsupportedMessageException e) {
CompilerDirectives.transferToInterpreterAndInvalidate();
throw EspressoError.shouldNotReachHere("Contract violation: if fitsInByte returns true, asByte must succeed.");
}
}
exceptionProfile.enter();
throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to byte"));
Expand Down Expand Up @@ -194,8 +205,13 @@ short doHost(Short value) {
@Specialization
short doEspresso(StaticObject value,
@Cached BranchProfile exceptionProfile) throws UnsupportedTypeException {
if (value != null && !StaticObject.isNull(value) && value.getKlass() == getMeta().java_lang_Short) {
return (short) getMeta().java_lang_Short_value.get(value);
if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInShort(value)) {
try {
return EspressoInterop.asShort(value);
} catch (UnsupportedMessageException e) {
CompilerDirectives.transferToInterpreterAndInvalidate();
throw EspressoError.shouldNotReachHere("Contract violation: if fitsInShort returns true, asShort must succeed.");
}
}
exceptionProfile.enter();
throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to short"));
Expand Down Expand Up @@ -264,7 +280,7 @@ char doForeign(Object value,
throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", s, " to char"));
} catch (UnsupportedMessageException e) {
CompilerDirectives.transferToInterpreterAndInvalidate();
throw EspressoError.shouldNotReachHere("Contract violation: if fitsInInt returns true, asInt must succeed.");
throw EspressoError.shouldNotReachHere("Contract violation: if isString returns true, asString must succeed.");
}
}

Expand Down Expand Up @@ -292,8 +308,13 @@ long doHost(Long value) {
@Specialization
long doEspresso(StaticObject value,
@Cached BranchProfile exceptionProfile) throws UnsupportedTypeException {
if (value != null && !StaticObject.isNull(value) && value.getKlass() == getMeta().java_lang_Long) {
return (long) getMeta().java_lang_Long_value.get(value);
if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInLong(value)) {
try {
return EspressoInterop.asLong(value);
} catch (UnsupportedMessageException e) {
CompilerDirectives.transferToInterpreterAndInvalidate();
throw EspressoError.shouldNotReachHere("Contract violation: if fitsInLong returns true, asLong must succeed.");
}
}
exceptionProfile.enter();
throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to long"));
Expand Down Expand Up @@ -338,8 +359,13 @@ float doHost(Float value) {
@Specialization
float doEspresso(StaticObject value,
@Cached BranchProfile exceptionProfile) throws UnsupportedTypeException {
if (value != null && !StaticObject.isNull(value) && value.getKlass() == getMeta().java_lang_Float) {
return (float) getMeta().java_lang_Float_value.get(value);
if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInFloat(value)) {
try {
return EspressoInterop.asFloat(value);
} catch (UnsupportedMessageException e) {
CompilerDirectives.transferToInterpreterAndInvalidate();
throw EspressoError.shouldNotReachHere("Contract violation: if fitsInFloat returns true, asFloat must succeed.");
}
}
exceptionProfile.enter();
throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to float"));
Expand Down Expand Up @@ -384,8 +410,13 @@ public abstract static class ToDouble extends ToPrimitive {
@Specialization
double doEspresso(StaticObject value,
@Cached BranchProfile exceptionProfile) throws UnsupportedTypeException {
if (value != null && !StaticObject.isNull(value) && value.getKlass() == getMeta().java_lang_Double) {
return (double) getMeta().java_lang_Double_value.get(value);
if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInDouble(value)) {
try {
return EspressoInterop.asDouble(value);
} catch (UnsupportedMessageException e) {
CompilerDirectives.transferToInterpreterAndInvalidate();
throw EspressoError.shouldNotReachHere("Contract violation: if fitsInDouble returns true, asDouble must succeed.");
}
}
exceptionProfile.enter();
throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to double"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import com.oracle.truffle.espresso.nodes.bytecodes.InstanceOf;
import com.oracle.truffle.espresso.runtime.EspressoContext;
import com.oracle.truffle.espresso.runtime.EspressoException;
import com.oracle.truffle.espresso.runtime.dispatch.staticobject.EspressoInterop;
import com.oracle.truffle.espresso.runtime.staticobject.StaticObject;

@NodeInfo(shortName = "Convert to Espresso StaticObject")
Expand Down Expand Up @@ -325,8 +326,33 @@ public abstract static class GenericToReference extends EspressoNode {
@Specialization
public StaticObject doStaticObject(StaticObject value, EspressoType targetType,
@Cached InstanceOf.Dynamic instanceOf) throws UnsupportedTypeException {
if (StaticObject.isNull(value) || instanceOf.execute(value.getKlass(), targetType.getRawType())) {
return value; // pass through, NULL coercion not needed.
Klass rawType = targetType.getRawType();
if (StaticObject.isNull(value) || instanceOf.execute(value.getKlass(), rawType)) {
return value;
}
try {
Meta meta = getMeta();
if (rawType == meta.java_lang_Double && EspressoInterop.fitsInDouble(value)) {
return meta.boxDouble(EspressoInterop.asDouble(value));
}
if (rawType == meta.java_lang_Float && EspressoInterop.fitsInFloat(value)) {
return meta.boxFloat(EspressoInterop.asFloat(value));
}
if (rawType == meta.java_lang_Long && EspressoInterop.fitsInLong(value)) {
return meta.boxLong(EspressoInterop.asLong(value));
}
if (rawType == meta.java_lang_Integer && EspressoInterop.fitsInInt(value)) {
return meta.boxInteger(EspressoInterop.asInt(value));
}
if (rawType == meta.java_lang_Short && EspressoInterop.fitsInShort(value)) {
return meta.boxShort(EspressoInterop.asShort(value));
}
if (rawType == meta.java_lang_Byte && EspressoInterop.fitsInByte(value)) {
return meta.boxByte(EspressoInterop.asByte(value));
}
} catch (UnsupportedMessageException ex) {
CompilerDirectives.transferToInterpreterAndInvalidate();
throw EspressoError.shouldNotReachHere("Contract violation: if fitsIn* returns true, as* must succeed.");
}
throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to ", targetType.getRawType().getTypeAsString()));
}
Expand Down Expand Up @@ -498,6 +524,10 @@ public static StaticObject doGeneric(Object value, EspressoType targetType,
if (result != null) {
return result;
}
// no generic conversion to abstract target types allowed
if (targetType.getRawType().isAbstract()) {
throw UnsupportedTypeException.create(new Object[]{value}, targetType.getRawType().getTypeAsString());
}
if (targetType instanceof ObjectKlass rawType) {
noConverterProfile.enter(node);
checkHasAllFieldsOrThrow(value, rawType, interop, meta);
Expand Down Expand Up @@ -2609,6 +2639,10 @@ static StaticObject doForeignWrapper(Object value,
if (result != null) {
return result;
}
// no generic conversion to abstract target types allowed
if (target.getRawType().isAbstract()) {
throw UnsupportedTypeException.create(new Object[]{value}, target.getRawType().getTypeAsString());
}
noConverterProfile.enter(node);
checkHasAllFieldsOrThrow(value, (ObjectKlass) target.getRawType(), interop, context.getMeta());
return StaticObject.createForeign(EspressoLanguage.get(node), target.getRawType(), value, interop);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,14 @@ public static Meta getMeta() {
}

@ExportMessage
static boolean isBoolean(StaticObject receiver) {
public static boolean isBoolean(StaticObject receiver) {
receiver.checkNotForeign();
assert !isNull(receiver) : "Null espresso object should be dispatched to BaseInterop";
return receiver.getKlass() == receiver.getKlass().getMeta().java_lang_Boolean;
}

@ExportMessage
static boolean asBoolean(StaticObject receiver) throws UnsupportedMessageException {
public static boolean asBoolean(StaticObject receiver) throws UnsupportedMessageException {
receiver.checkNotForeign();
if (!isBoolean(receiver)) {
throw UnsupportedMessageException.create();
Expand All @@ -128,7 +128,7 @@ static boolean isNumber(StaticObject receiver) {
}

@ExportMessage
static boolean fitsInByte(StaticObject receiver) {
public static boolean fitsInByte(StaticObject receiver) {
receiver.checkNotForeign();
if (isNull(receiver)) {
return false;
Expand Down Expand Up @@ -163,7 +163,7 @@ static boolean fitsInByte(StaticObject receiver) {
}

@ExportMessage
static boolean fitsInShort(StaticObject receiver) {
public static boolean fitsInShort(StaticObject receiver) {
receiver.checkNotForeign();
if (isNull(receiver)) {
return false;
Expand Down Expand Up @@ -194,7 +194,7 @@ static boolean fitsInShort(StaticObject receiver) {
}

@ExportMessage
static boolean fitsInInt(StaticObject receiver) {
public static boolean fitsInInt(StaticObject receiver) {
receiver.checkNotForeign();
if (isNull(receiver)) {
return false;
Expand All @@ -221,7 +221,7 @@ static boolean fitsInInt(StaticObject receiver) {
}

@ExportMessage
static boolean fitsInLong(StaticObject receiver) {
public static boolean fitsInLong(StaticObject receiver) {
receiver.checkNotForeign();
if (isNull(receiver)) {
return false;
Expand All @@ -244,7 +244,7 @@ static boolean fitsInLong(StaticObject receiver) {
}

@ExportMessage
static boolean fitsInFloat(StaticObject receiver) {
public static boolean fitsInFloat(StaticObject receiver) {
receiver.checkNotForeign();
if (isNull(receiver)) {
return false;
Expand Down Expand Up @@ -278,7 +278,7 @@ static boolean fitsInFloat(StaticObject receiver) {
}

@ExportMessage
static boolean fitsInDouble(StaticObject receiver) {
public static boolean fitsInDouble(StaticObject receiver) {
receiver.checkNotForeign();
if (isNull(receiver)) {
return false;
Expand Down Expand Up @@ -349,7 +349,7 @@ private static Number readNumberValue(StaticObject receiver) throws UnsupportedM
}

@ExportMessage
static byte asByte(StaticObject receiver) throws UnsupportedMessageException {
public static byte asByte(StaticObject receiver) throws UnsupportedMessageException {
receiver.checkNotForeign();
if (!fitsInByte(receiver)) {
CompilerDirectives.transferToInterpreter();
Expand All @@ -359,7 +359,7 @@ static byte asByte(StaticObject receiver) throws UnsupportedMessageException {
}

@ExportMessage
static short asShort(StaticObject receiver) throws UnsupportedMessageException {
public static short asShort(StaticObject receiver) throws UnsupportedMessageException {
receiver.checkNotForeign();
if (!fitsInShort(receiver)) {
CompilerDirectives.transferToInterpreter();
Expand All @@ -369,7 +369,7 @@ static short asShort(StaticObject receiver) throws UnsupportedMessageException {
}

@ExportMessage
static int asInt(StaticObject receiver) throws UnsupportedMessageException {
public static int asInt(StaticObject receiver) throws UnsupportedMessageException {
receiver.checkNotForeign();
if (!fitsInInt(receiver)) {
CompilerDirectives.transferToInterpreter();
Expand All @@ -379,7 +379,7 @@ static int asInt(StaticObject receiver) throws UnsupportedMessageException {
}

@ExportMessage
static long asLong(StaticObject receiver) throws UnsupportedMessageException {
public static long asLong(StaticObject receiver) throws UnsupportedMessageException {
receiver.checkNotForeign();
if (!fitsInLong(receiver)) {
CompilerDirectives.transferToInterpreter();
Expand All @@ -389,7 +389,7 @@ static long asLong(StaticObject receiver) throws UnsupportedMessageException {
}

@ExportMessage
static float asFloat(StaticObject receiver) throws UnsupportedMessageException {
public static float asFloat(StaticObject receiver) throws UnsupportedMessageException {
receiver.checkNotForeign();
if (!fitsInFloat(receiver)) {
CompilerDirectives.transferToInterpreter();
Expand All @@ -399,7 +399,7 @@ static float asFloat(StaticObject receiver) throws UnsupportedMessageException {
}

@ExportMessage
static double asDouble(StaticObject receiver) throws UnsupportedMessageException {
public static double asDouble(StaticObject receiver) throws UnsupportedMessageException {
receiver.checkNotForeign();
if (!fitsInDouble(receiver)) {
CompilerDirectives.transferToInterpreter();
Expand Down
Loading

0 comments on commit 34121af

Please sign in to comment.