Skip to content

Commit

Permalink
Reduce code duplication and fix warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
fniephaus committed Dec 9, 2023
1 parent 37e2359 commit 508ec3b
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ protected static final NativeObject doString(final Node node, final String value

@Specialization
protected static final NativeObject doTruffleString(final Node node, final TruffleString value,
@Cached final TruffleString.ToJavaStringNode toJavaString,
@Cached(inline = false) final TruffleString.ToJavaStringNode toJavaString,
@Shared("wideStringProfile") @Cached final InlinedConditionProfile wideStringProfile) {
return doString(node, toJavaString.execute(value), wideStringProfile);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ public static DoItRootNode create(final SqueakImageContext image, final SqueakLa
return DoItRootNodeGen.create(image, language, closure);
}

public abstract Object execute(VirtualFrame frame);

@Specialization
protected final Object doIt(final VirtualFrame frame,
@Bind("this") final Node node,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import de.hpi.swa.trufflesqueak.model.layout.ObjectLayouts.ASSOCIATION;
import de.hpi.swa.trufflesqueak.nodes.AbstractNode;
import de.hpi.swa.trufflesqueak.nodes.accessing.SqueakObjectAt0Node;
import de.hpi.swa.trufflesqueak.nodes.bytecodes.PushBytecodesFactory.PushLiteralVariableNodeFactory.PushLiteralVariableReadonlyNodeGen;
import de.hpi.swa.trufflesqueak.nodes.bytecodes.PushBytecodesFactory.PushLiteralVariableNodeFactory.PushLiteralVariableWritableNodeGen;
import de.hpi.swa.trufflesqueak.nodes.bytecodes.PushBytecodesFactory.PushReceiverVariableNodeGen;
import de.hpi.swa.trufflesqueak.nodes.bytecodes.PushBytecodesFactory.PushRemoteTempNodeGen;
Expand Down Expand Up @@ -372,7 +373,7 @@ public String toString() {
}

@NodeInfo(cost = NodeCost.NONE)
public abstract static class PushLiteralVariableNode extends AbstractPushNode {
public abstract static class PushLiteralVariableNode extends AbstractInstrumentableBytecodeNode {
private static final String[] READONLY_CLASSES = {"ClassBinding", "ReadOnlyVariableBinding"};
protected final Object literal;

Expand All @@ -381,12 +382,12 @@ private PushLiteralVariableNode(final CompiledCodeObject code, final int index,
this.literal = literal;
}

public static final AbstractPushNode create(final CompiledCodeObject code, final int index, final int numBytecodes, final int literalIndex) {
public static final AbstractInstrumentableBytecodeNode create(final CompiledCodeObject code, final int index, final int numBytecodes, final int literalIndex) {
final Object literal = code.getLiteral(literalIndex);
if (literal instanceof final AbstractSqueakObjectWithClassAndHash l) {
final String squeakClassName = l.getSqueakClassName();
if (ArrayUtils.containsEqual(READONLY_CLASSES, squeakClassName)) {
return new PushLiteralVariableReadonlyNode(code, index, numBytecodes, literal);
return PushLiteralVariableReadonlyNodeGen.create(code, index, numBytecodes, literal);
}
}
return PushLiteralVariableWritableNodeGen.create(code, index, numBytecodes, literal);
Expand Down Expand Up @@ -414,16 +415,17 @@ protected final void doPushLiteralVariable(final VirtualFrame frame,
}
}

private static final class PushLiteralVariableReadonlyNode extends PushLiteralVariableNode {
protected abstract static class PushLiteralVariableReadonlyNode extends PushLiteralVariableNode {
private final Object pushValue;

protected PushLiteralVariableReadonlyNode(final CompiledCodeObject code, final int index, final int numBytecodes, final Object literal) {
super(code, index, numBytecodes, literal);
pushValue = getPushValue(literal);
}

@Override
public void executeVoid(final VirtualFrame frame) {
@Specialization
protected final void doPushLiteralVariable(final VirtualFrame frame,
@Cached final FrameStackPushNode pushNode) {
assert pushValue == getPushValue(literal) : "value of binding changed unexpectedly";
pushNode.execute(frame, pushValue);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.Cached.Shared;
import com.oracle.truffle.api.dsl.Fallback;
import com.oracle.truffle.api.dsl.GenerateCached;
import com.oracle.truffle.api.dsl.GenerateInline;
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
import com.oracle.truffle.api.dsl.NodeFactory;
import com.oracle.truffle.api.dsl.Specialization;
Expand All @@ -24,6 +26,7 @@
import de.hpi.swa.trufflesqueak.model.AbstractSqueakObject;
import de.hpi.swa.trufflesqueak.model.LargeIntegerObject;
import de.hpi.swa.trufflesqueak.model.NativeObject;
import de.hpi.swa.trufflesqueak.nodes.AbstractNode;
import de.hpi.swa.trufflesqueak.nodes.primitives.AbstractPrimitiveFactoryHolder;
import de.hpi.swa.trufflesqueak.nodes.primitives.AbstractPrimitiveNode;
import de.hpi.swa.trufflesqueak.nodes.primitives.PrimitiveFallbacks.BinaryPrimitiveFallback;
Expand Down Expand Up @@ -424,39 +427,49 @@ protected static final long calculateHash(final long initialHash, final byte[] b
@SqueakPrimitive(names = "primitiveStringHash")
/* Byte(Array|String|Symbol)>>#hashWithInitialHash: */
public abstract static class PrimStringHash2Node extends AbstractPrimStringHashNode implements BinaryPrimitiveFallback {
@Specialization(guards = {"string.isByteType()"})
protected static final long doNativeObject(final NativeObject string, final long initialHash) {
return calculateHash(initialHash, string.getByteStorage());
}

@Specialization
protected static final long doLargeInteger(final LargeIntegerObject largeInteger, final long initialHash) {
return calculateHash(initialHash, largeInteger.getBytes());
}

@Specialization(guards = {"isLongMinValue(value)"})
protected static final long doLongMinValue(@SuppressWarnings("unused") final long value, final long initialHash) {
return calculateHash(initialHash, LargeIntegerObject.getLongMinOverflowResultBytes());
protected static final long doStringHash(final Object receiver, final long initialHash,
@Bind("this") final Node node,
@Cached final GetHashBytesNode getHashBytesNode) {
return calculateHash(initialHash, getHashBytesNode.execute(node, receiver));
}
}

@GenerateNodeFactory
@SqueakPrimitive(names = "primitiveStringHash")
/* (Byte(Array|String|Symbol) class|MiscPrimitivePluginTest)>>#hashBytes:startingWith: */
public abstract static class PrimStringHash3Node extends AbstractPrimStringHashNode implements TernaryPrimitiveFallback {
@Specialization(guards = {"string.isByteType()"})
protected static final long doNativeObject(@SuppressWarnings("unused") final Object receiver, final NativeObject string, final long initialHash) {
return calculateHash(initialHash, string.getByteStorage());
@Specialization
protected static final long doStringHash(@SuppressWarnings("unused") final Object receiver, final Object target, final long initialHash,
@Bind("this") final Node node,
@Cached final GetHashBytesNode getHashBytesNode) {
return calculateHash(initialHash, getHashBytesNode.execute(node, target));
}
}

@GenerateInline
@GenerateCached(false)
protected abstract static class GetHashBytesNode extends AbstractNode {
protected abstract byte[] execute(Node inliningTarget, Object value);

@Specialization(guards = {"value.isByteType()"})
protected static final byte[] doNativeObject(final NativeObject value) {
return value.getByteStorage();
}

@Specialization
protected static final long doLargeInteger(@SuppressWarnings("unused") final Object receiver, final LargeIntegerObject largeInteger, final long initialHash) {
return calculateHash(initialHash, largeInteger.getBytes());
protected static final byte[] doLargeInteger(final LargeIntegerObject value) {
return value.getBytes();
}

@Specialization(guards = {"isLongMinValue(value)"})
protected static final long doLongMinValue(@SuppressWarnings("unused") final Object receiver, @SuppressWarnings("unused") final long value, final long initialHash) {
return calculateHash(initialHash, LargeIntegerObject.getLongMinOverflowResultBytes());
protected static final byte[] doLongMinValue(@SuppressWarnings("unused") final long value) {
return LargeIntegerObject.getLongMinOverflowResultBytes();
}

@Fallback
protected static final byte[] doFallback(@SuppressWarnings("unused") final Object value) {
throw PrimitiveFailed.GENERIC_ERROR;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
import com.oracle.truffle.api.dsl.Bind;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.Cached.Shared;
import com.oracle.truffle.api.dsl.Fallback;
import com.oracle.truffle.api.dsl.GenerateCached;
import com.oracle.truffle.api.dsl.GenerateInline;
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
import com.oracle.truffle.api.dsl.ImportStatic;
import com.oracle.truffle.api.dsl.NodeFactory;
Expand All @@ -29,6 +32,7 @@
import de.hpi.swa.trufflesqueak.model.FloatObject;
import de.hpi.swa.trufflesqueak.model.LargeIntegerObject;
import de.hpi.swa.trufflesqueak.model.PointersObject;
import de.hpi.swa.trufflesqueak.nodes.AbstractNode;
import de.hpi.swa.trufflesqueak.nodes.SqueakGuards;
import de.hpi.swa.trufflesqueak.nodes.accessing.AbstractPointersObjectNodes.AbstractPointersObjectWriteNode;
import de.hpi.swa.trufflesqueak.nodes.accessing.FloatObjectNodes.AsFloatObjectIfNessaryNode;
Expand Down Expand Up @@ -721,23 +725,17 @@ protected static final Object doLargeInteger(final LargeIntegerObject lhs, final
@SqueakPrimitive(indices = 38)
protected abstract static class PrimFloatAtNode extends AbstractArithmeticPrimitiveNode implements BinaryPrimitiveFallback {
@Specialization(guards = "index == 1")
protected static final long doDoubleHigh(final double receiver, @SuppressWarnings("unused") final long index) {
return Integer.toUnsignedLong((int) (Double.doubleToRawLongBits(receiver) >> 32));
}

@Specialization(guards = "index == 2")
protected static final long doDoubleLow(final double receiver, @SuppressWarnings("unused") final long index) {
return Integer.toUnsignedLong((int) Double.doubleToRawLongBits(receiver));
}

@Specialization(guards = "index == 1")
protected static final long doFloatHigh(final FloatObject receiver, final long index) {
return doDoubleHigh(receiver.getValue(), index);
protected static final long doHigh(final Object receiver, @SuppressWarnings("unused") final long index,
@Bind("this") final Node node,
@Shared("toDoubleNode") @Cached final ToDoubleNode toDoubleNode) {
return Integer.toUnsignedLong((int) (Double.doubleToRawLongBits(toDoubleNode.execute(node, receiver)) >> 32));
}

@Specialization(guards = "index == 2")
protected static final long doFloatLow(final FloatObject receiver, final long index) {
return doDoubleLow(receiver.getValue(), index);
protected static final long doLow(final Object receiver, @SuppressWarnings("unused") final long index,
@Bind("this") final Node node,
@Shared("toDoubleNode") @Cached final ToDoubleNode toDoubleNode) {
return Integer.toUnsignedLong((int) Double.doubleToRawLongBits(toDoubleNode.execute(node, receiver)));
}

@SuppressWarnings("unused")
Expand Down Expand Up @@ -1651,6 +1649,27 @@ protected static final long exponentNonZero(final double receiver, final Inlined
}
}

@GenerateInline
@GenerateCached(false)
protected abstract static class ToDoubleNode extends AbstractNode {
protected abstract double execute(Node inliningTarget, Object value);

@Specialization
protected static final double doDouble(final double value) {
return value;
}

@Specialization
protected static final double doFloatObject(final FloatObject value) {
return value.getValue();
}

@Fallback
protected static final double doFallback(@SuppressWarnings("unused") final Object value) {
throw PrimitiveFailed.BAD_RECEIVER;
}
}

@Override
public List<? extends NodeFactory<? extends AbstractPrimitiveNode>> getFactories() {
return ArithmeticPrimitivesFactory.getFactories();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ protected static final Object basicAtPut(final AbstractSqueakObject receiver, fi
@SqueakPrimitive(indices = 60)
protected abstract static class PrimBasicAt2Node extends AbstractBasicAtOrAtPutNode implements BinaryPrimitiveFallback {
@Specialization
protected final Object doSqueakObject(final Object receiver, final long index,
protected static final Object doSqueakObject(final Object receiver, final long index,
@Bind("this") final Node node,
@Cached final SqueakObjectSizeNode sizeNode,
@Cached final SqueakObjectInstSizeNode instSizeNode,
Expand All @@ -98,7 +98,7 @@ protected final Object doSqueakObject(final Object receiver, final long index,
@SqueakPrimitive(indices = 60)
protected abstract static class PrimBasicAt3Node extends AbstractBasicAtOrAtPutNode implements TernaryPrimitiveFallback {
@Specialization
protected final Object doSqueakObject(@SuppressWarnings("unused") final Object receiver, final Object target, final long index,
protected static final Object doSqueakObject(@SuppressWarnings("unused") final Object receiver, final Object target, final long index,
@Bind("this") final Node node,
@Cached final SqueakObjectSizeNode sizeNode,
@Cached final SqueakObjectInstSizeNode instSizeNode,
Expand All @@ -113,7 +113,7 @@ protected final Object doSqueakObject(@SuppressWarnings("unused") final Object r
@SqueakPrimitive(indices = 61)
protected abstract static class PrimBasicAtPut3Node extends AbstractBasicAtOrAtPutNode implements TernaryPrimitiveFallback {
@Specialization
protected final Object doSqueakObject(final AbstractSqueakObject receiver, final long index, final Object value,
protected static final Object doSqueakObject(final AbstractSqueakObject receiver, final long index, final Object value,
@Bind("this") final Node node,
@Cached final SqueakObjectSizeNode sizeNode,
@Cached final SqueakObjectInstSizeNode instSizeNode,
Expand All @@ -128,7 +128,7 @@ protected final Object doSqueakObject(final AbstractSqueakObject receiver, final
@SqueakPrimitive(indices = 61)
protected abstract static class PrimBasicAtPut4Node extends AbstractBasicAtOrAtPutNode implements QuaternaryPrimitiveFallback {
@Specialization
protected final Object doSqueakObject(@SuppressWarnings("unused") final Object receiver, final AbstractSqueakObject target, final long index, final Object value,
protected static final Object doSqueakObject(@SuppressWarnings("unused") final Object receiver, final AbstractSqueakObject target, final long index, final Object value,
@Bind("this") final Node node,
@Cached final SqueakObjectSizeNode sizeNode,
@Cached final SqueakObjectInstSizeNode instSizeNode,
Expand Down

0 comments on commit 508ec3b

Please sign in to comment.