Skip to content

Commit

Permalink
Extend interop with BigInteger, meta parents, etc
Browse files Browse the repository at this point in the history
  • Loading branch information
fniephaus committed Oct 22, 2024
1 parent 4bb8aa7 commit 4e1c15e
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -413,30 +413,30 @@ protected boolean fitsInLong(@Shared("lib") @CachedLibrary(limit = "LIMIT") fina
}

@ExportMessage
protected boolean fitsInFloat(@Shared("lib") @CachedLibrary(limit = "LIMIT") final InteropLibrary lib) {
protected boolean fitsInBigInteger(@Shared("lib") @CachedLibrary(limit = "LIMIT") final InteropLibrary lib) {
if (wrappedObject instanceof BigInteger) {
return true;
}
if (isNumber()) {
return lib.fitsInFloat(wrappedObject);
return lib.fitsInBigInteger(wrappedObject);
} else {
return false;
}
}

@ExportMessage
protected boolean fitsInDouble(@Shared("lib") @CachedLibrary(limit = "LIMIT") final InteropLibrary lib) {
protected boolean fitsInFloat(@Shared("lib") @CachedLibrary(limit = "LIMIT") final InteropLibrary lib) {
if (isNumber()) {
return lib.fitsInDouble(wrappedObject);
return lib.fitsInFloat(wrappedObject);
} else {
return false;
}
}

@ExportMessage
protected boolean fitsInBigInteger(@Shared("lib") @CachedLibrary(limit = "LIMIT") final InteropLibrary lib) {
if (wrappedObject instanceof BigInteger) {
return true;
}
protected boolean fitsInDouble(@Shared("lib") @CachedLibrary(limit = "LIMIT") final InteropLibrary lib) {
if (isNumber()) {
return lib.fitsInBigInteger(wrappedObject);
return lib.fitsInDouble(wrappedObject);
} else {
return false;
}
Expand Down Expand Up @@ -479,29 +479,29 @@ protected long asLong(@Shared("lib") @CachedLibrary(limit = "LIMIT") final Inter
}

@ExportMessage
protected float asFloat(@Shared("lib") @CachedLibrary(limit = "LIMIT") final InteropLibrary lib) throws UnsupportedMessageException {
if (isNumber()) {
return lib.asFloat(wrappedObject);
protected BigInteger asBigInteger(@Shared("lib") @CachedLibrary(limit = "LIMIT") final InteropLibrary lib) throws UnsupportedMessageException {
if (wrappedObject instanceof final BigInteger w) {
return w;
} else if (isNumber()) {
return lib.asBigInteger(wrappedObject);
} else {
throw UnsupportedMessageException.create();
}
}

@ExportMessage
protected double asDouble(@Shared("lib") @CachedLibrary(limit = "LIMIT") final InteropLibrary lib) throws UnsupportedMessageException {
protected float asFloat(@Shared("lib") @CachedLibrary(limit = "LIMIT") final InteropLibrary lib) throws UnsupportedMessageException {
if (isNumber()) {
return lib.asDouble(wrappedObject);
return lib.asFloat(wrappedObject);
} else {
throw UnsupportedMessageException.create();
}
}

@ExportMessage
protected BigInteger asBigInteger(@Shared("lib") @CachedLibrary(limit = "LIMIT") final InteropLibrary lib) throws UnsupportedMessageException {
if (wrappedObject instanceof final BigInteger w) {
return w;
} else if (isNumber()) {
return lib.asBigInteger(wrappedObject);
protected double asDouble(@Shared("lib") @CachedLibrary(limit = "LIMIT") final InteropLibrary lib) throws UnsupportedMessageException {
if (isNumber()) {
return lib.asDouble(wrappedObject);
} else {
throw UnsupportedMessageException.create();
}
Expand Down Expand Up @@ -864,6 +864,20 @@ protected boolean isMetaInstance(final Object other) throws UnsupportedMessageEx
}
}

@ExportMessage
public boolean hasMetaParents() {
return isMetaObject();
}

@ExportMessage
public Object getMetaParents() throws UnsupportedMessageException {
if (isClass()) {
return wrap(new Object[]{asClass().getSuperclass()});
} else {
throw UnsupportedMessageException.create();
}
}

@ExportMessage
protected boolean hasLanguage() {
return true;
Expand Down Expand Up @@ -915,6 +929,8 @@ private static Object toJavaArgument(final Object argument) {
return lib.asInt(argument);
} else if (lib.fitsInLong(argument)) {
return lib.asLong(argument);
} else if (lib.fitsInBigInteger(argument)) {
return lib.asBigInteger(argument);
} else if (lib.fitsInFloat(argument)) {
return lib.asFloat(argument);
} else if (lib.fitsInDouble(argument)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigInteger;
import java.nio.ByteOrder;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -55,6 +56,7 @@
import de.hpi.swa.trufflesqueak.model.ArrayObject;
import de.hpi.swa.trufflesqueak.model.BooleanObject;
import de.hpi.swa.trufflesqueak.model.ClassObject;
import de.hpi.swa.trufflesqueak.model.LargeIntegerObject;
import de.hpi.swa.trufflesqueak.model.NativeObject;
import de.hpi.swa.trufflesqueak.model.NilObject;
import de.hpi.swa.trufflesqueak.model.PointersObject;
Expand All @@ -65,6 +67,7 @@
import de.hpi.swa.trufflesqueak.nodes.primitives.PrimitiveFallbacks.BinaryPrimitiveFallback;
import de.hpi.swa.trufflesqueak.nodes.primitives.PrimitiveFallbacks.QuaternaryPrimitiveFallback;
import de.hpi.swa.trufflesqueak.nodes.primitives.PrimitiveFallbacks.QuinaryPrimitiveFallback;
import de.hpi.swa.trufflesqueak.nodes.primitives.PrimitiveFallbacks.SenaryPrimitiveFallback;
import de.hpi.swa.trufflesqueak.nodes.primitives.PrimitiveFallbacks.TernaryPrimitiveFallback;
import de.hpi.swa.trufflesqueak.nodes.primitives.PrimitiveFallbacks.UnaryPrimitiveFallback;
import de.hpi.swa.trufflesqueak.nodes.primitives.SqueakPrimitive;
Expand Down Expand Up @@ -539,6 +542,33 @@ protected static final double doAsDouble(@SuppressWarnings("unused") final Objec
}
}

@GenerateNodeFactory
@SqueakPrimitive(names = "primitiveFitsInBigInteger")
protected abstract static class PrimFitsInBigIntegerNode extends AbstractPrimitiveNode {

@Specialization
protected static final boolean doFitsInBigInteger(@SuppressWarnings("unused") final Object receiver, final Object object,
@CachedLibrary(limit = "2") final InteropLibrary lib) {
return BooleanObject.wrap(lib.fitsInBigInteger(object));
}
}

@GenerateNodeFactory
@SqueakPrimitive(names = "primitiveAsBigInteger")
protected abstract static class PrimAsBigIntegerNode extends AbstractPrimitiveNode implements BinaryPrimitiveFallback {

@TruffleBoundary
@Specialization(guards = {"lib.fitsInBigInteger(object)"}, limit = "2")
protected final Object doAsDouble(@SuppressWarnings("unused") final Object receiver, final Object object,
@CachedLibrary("object") final InteropLibrary lib) {
try {
return new LargeIntegerObject(getContext(), lib.asBigInteger(object));
} catch (final UnsupportedMessageException e) {
throw primitiveFailedInInterpreterCapturing(e);
}
}
}

@GenerateNodeFactory
@SqueakPrimitive(names = "primitiveIsExecutable")
protected abstract static class PrimIsExecutableNode extends AbstractPrimitiveNode {
Expand Down Expand Up @@ -1213,6 +1243,21 @@ protected static final long doReadBufferByte(@SuppressWarnings("unused") final O
}
}

@GenerateNodeFactory
@SqueakPrimitive(names = "primitiveReadBuffer")
protected abstract static class PrimReadBufferNode extends AbstractPrimitiveNode implements SenaryPrimitiveFallback {
@Specialization(guards = {"lib.hasBufferElements(object)", "destination.isByteType()"})
protected static final Object doReadBuffer(final Object receiver, final Object object, final long byteOffset, final NativeObject destination, final long destinationOffset, final long length,
@CachedLibrary(limit = "2") final InteropLibrary lib) {
try {
lib.readBuffer(object, byteOffset, destination.getByteStorage(), MiscUtils.toIntExact(destinationOffset), MiscUtils.toIntExact(length));
return receiver;
} catch (final UnsupportedMessageException | InvalidBufferOffsetException e) {
throw primitiveFailedInInterpreterCapturing(e);
}
}
}

@GenerateNodeFactory
@SqueakPrimitive(names = "primitiveWriteBufferByte")
protected abstract static class PrimWriteBufferByteNode extends AbstractPrimitiveNode implements QuaternaryPrimitiveFallback {
Expand Down Expand Up @@ -1657,6 +1702,29 @@ protected static final Object getMetaSimpleName(@SuppressWarnings("unused") fina
}
}

@GenerateNodeFactory
@SqueakPrimitive(names = "primitiveHasMetaParents")
protected abstract static class PrimHasMetaParentsNode extends AbstractPrimitiveNode implements BinaryPrimitiveFallback {
@Specialization(guards = "lib.isMetaObject(object)")
protected static final boolean hasMetaParents(@SuppressWarnings("unused") final Object receiver, final Object object, @CachedLibrary(limit = "2") final InteropLibrary lib) {
return lib.hasMetaParents(object);
}
}

@GenerateNodeFactory
@SqueakPrimitive(names = "primitiveGetMetaParents")
protected abstract static class PrimGetMetaParentsNode extends AbstractPrimitiveNode implements BinaryPrimitiveFallback {
@Specialization(guards = "lib.isMetaObject(object)")
protected static final Object getMetaParents(@SuppressWarnings("unused") final Object receiver, final Object object,
@CachedLibrary(limit = "2") final InteropLibrary lib) {
try {
return lib.getMetaParents(object);
} catch (final UnsupportedMessageException e) {
throw primitiveFailedInInterpreterCapturing(e);
}
}
}

@GenerateNodeFactory
@SqueakPrimitive(names = "primitiveHasSourceLocation")
protected abstract static class PrimHasSourceLocationNode extends AbstractPrimitiveNode {
Expand Down Expand Up @@ -2172,6 +2240,15 @@ protected static final int toJavaInteger(@SuppressWarnings("unused") final Objec
}
}

@GenerateNodeFactory
@SqueakPrimitive(names = "primitiveToJavaBigInteger")
protected abstract static class PrimToJavaBigIntegerNode extends AbstractPrimitiveNode implements BinaryPrimitiveFallback {
@Specialization
protected static final BigInteger toJavaInteger(@SuppressWarnings("unused") final Object receiver, final LargeIntegerObject value) {
return value.getBigInteger();
}
}

@GenerateNodeFactory
@SqueakPrimitive(names = "primitiveToJavaString")
protected abstract static class PrimToJavaStringNode extends AbstractPrimitiveNode implements BinaryPrimitiveFallback {
Expand Down
2 changes: 1 addition & 1 deletion src/image
Submodule image updated 38 files
+3 −0 src/TruffleSqueak-Core.package/Behavior.extension/instance/interopGetMetaParents.st
+3 −0 src/TruffleSqueak-Core.package/Behavior.extension/instance/interopHasMetaParents.st
+2 −0 src/TruffleSqueak-Core.package/Behavior.extension/methodProperties.json
+1 −0 src/TruffleSqueak-Core.package/ForeignObject.class/instance/^equals.st
+1 −0 src/TruffleSqueak-Core.package/ForeignObject.class/instance/adaptToNumber.andSend..st
+1 −0 src/TruffleSqueak-Core.package/ForeignObject.class/instance/asNumber.st
+1 −0 src/TruffleSqueak-Core.package/ForeignObject.class/instance/asSmalltalk.st
+1 −0 src/TruffleSqueak-Core.package/ForeignObject.class/instance/isZero.st
+5 −5 src/TruffleSqueak-Core.package/ForeignObject.class/methodProperties.json
+6 −0 src/TruffleSqueak-Core.package/Interop.class/class/asBigInteger..st
+4 −0 src/TruffleSqueak-Core.package/Interop.class/class/fitsInBigInteger..st
+6 −0 src/TruffleSqueak-Core.package/Interop.class/class/getMetaParents..st
+6 −0 src/TruffleSqueak-Core.package/Interop.class/class/hasMetaParents..st
+8 −0 ...uffleSqueak-Core.package/Interop.class/class/readBuffer.byteOffset.destination.destinationOffset.length..st
+5 −0 src/TruffleSqueak-Core.package/Interop.class/methodProperties.json
+4 −0 src/TruffleSqueak-Core.package/Java.class/class/asBigInteger..st
+1 −1 src/TruffleSqueak-Core.package/Java.class/class/asHostObject..st
+2 −1 src/TruffleSqueak-Core.package/Java.class/methodProperties.json
+3 −0 src/TruffleSqueak-Core.package/LargePositiveInteger.extension/instance/interopAsBigInteger.st
+3 −0 src/TruffleSqueak-Core.package/LargePositiveInteger.extension/instance/interopFitsInBigInteger.st
+6 −0 src/TruffleSqueak-Core.package/LargePositiveInteger.extension/methodProperties.json
+2 −0 src/TruffleSqueak-Core.package/LargePositiveInteger.extension/properties.json
+3 −0 src/TruffleSqueak-Core.package/Object.extension/instance/interopAsBigInteger.st
+3 −0 src/TruffleSqueak-Core.package/Object.extension/instance/interopFitsInBigInteger.st
+3 −0 src/TruffleSqueak-Core.package/Object.extension/instance/interopGetMetaParents.st
+3 −0 src/TruffleSqueak-Core.package/Object.extension/instance/interopHasMetaParents.st
+3 −0 ...fleSqueak-Core.package/Object.extension/instance/interopReadBuffer.destination.destinationOffset.length..st
+5 −0 src/TruffleSqueak-Core.package/Object.extension/methodProperties.json
+1 −0 src/TruffleSqueak-Tests.package/ForeignObjectTest.class/instance/testJSMetadataAPIs.st
+2 −0 src/TruffleSqueak-Tests.package/ForeignObjectTest.class/instance/testJSObject.st
+2 −2 src/TruffleSqueak-Tests.package/ForeignObjectTest.class/methodProperties.json
+4 −0 src/TruffleSqueak-Tests.package/InteropTest.class/instance/testMetadataAPIs.st
+8 −1 src/TruffleSqueak-Tests.package/InteropTest.class/instance/testNumbers.st
+2 −2 src/TruffleSqueak-Tests.package/InteropTest.class/methodProperties.json
+5 −1 src/TruffleSqueak-Tests.package/JavaTest.class/instance/testJavaBuffers.st
+1 −1 src/TruffleSqueak-Tests.package/JavaTest.class/methodProperties.json
+6 −3 src/TruffleSqueak-Tests.package/TruffleSqueakTest.class/instance/testVMIntrospection.st
+1 −1 src/TruffleSqueak-Tests.package/TruffleSqueakTest.class/methodProperties.json

2 comments on commit 4e1c15e

@TruffleSqueak-Bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Performance Report (4e1c15e)

Benchmarks ran on 23.0.1-graal.

Steady (after 100 iterations)

Benchmark Name Min Geomean Median Mean Max Total (ms) Total (min)
Bounce 517 536 520.72 518 520.69 104143 1.74
CD 478 499 484.9 481 484.85 96980 1.62
DeltaBlue 279 455 396.1 394 395.24 79219 1.32
Havlak 1127 1179 1153.56 1152.5 1153.51 230711 3.85
Json 353 371 356.88 354 356.84 71376 1.19
List 311 336 313.17 312 313.12 62633 1.04
Mandelbrot 129 150 131.01 130 130.95 26201 0.44
NBody 243 265 247.87 245 247.81 49574 0.83
Permute 159 173 161.77 161 161.75 32354 0.54
Queens 217 251 220.44 218 220.36 44087 0.73
Richards 1254 1318 1262.64 1257 1262.59 252528 4.21
Sieve 177 210 178.25 177 178.21 35649 0.59
Storage 140 160 143.05 141 142.97 28610 0.48
Towers 178 202 180.1 179 180.05 36020 0.6
5562 6105 5750.43 5719.5 5748.95 1150085 19.17

4e1c15e-2-steady.svg

Warmup (first 100 iterations)

4e1c15e-3-warmup.svg

@TruffleSqueak-Bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Performance Report (4e1c15e)

Benchmarks ran on 23.0.1-graal.

Steady (after 100 iterations)

Benchmark Name Min Geomean Median Mean Max Total (ms) Total (min)
Bounce 522 536 526.43 523 526.41 105286 1.75
CD 476 512 481.36 477 481.3 96271 1.6
DeltaBlue 285 436 395.38 392 394.78 79076 1.32
Havlak 1129 1189 1161.72 1161 1161.68 232345 3.87
Json 333 348 336.54 334 336.5 67308 1.12
List 311 345 313.66 312 313.61 62732 1.05
Mandelbrot 129 150 130.54 130 130.5 26108 0.44
NBody 245 268 248.97 246 248.9 49794 0.83
Permute 156 175 157.98 157 157.95 31596 0.53
Queens 215 253 220.48 217 220.37 44095 0.73
Richards 1269 1336 1277.11 1273 1277.07 255421 4.26
Sieve 177 206 178.3 178 178.27 35660 0.59
Storage 141 161 144.75 143 144.67 28949 0.48
Towers 178 203 181.03 179 180.95 36206 0.6
5566 6118 5754.24 5722 5752.97 1150847 19.18

4e1c15e-2-steady.svg

Warmup (first 100 iterations)

4e1c15e-3-warmup.svg

Please sign in to comment.