Skip to content

Commit

Permalink
Showing 10 changed files with 42 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -34,14 +34,6 @@
@CoreClass(name = "Bignum")
public abstract class BignumNodes {

public static final BigInteger LONG_MAX = BigInteger.valueOf(Long.MAX_VALUE);
public static final BigInteger LONG_MIN = BigInteger.valueOf(Long.MIN_VALUE);

public static DynamicObject createRubyBignum(DynamicObject rubyClass, BigInteger value) {
assert value.compareTo(LONG_MIN) < 0 || value.compareTo(LONG_MAX) > 0 : String.format("%s not in Bignum range", value);
return Layouts.BIGNUM.createBignum(Layouts.CLASS.getInstanceFactory(rubyClass), value);
}

public static abstract class BignumCoreMethodNode extends CoreMethodArrayArgumentsNode {

@Child private FixnumOrBignumNode fixnumOrBignum;
Original file line number Diff line number Diff line change
@@ -165,7 +165,7 @@ private DynamicObject divMod(BigInteger a, BigInteger b) {
}

public DynamicObject create(BigInteger value) {
return BignumNodes.createRubyBignum(getContext().getCoreLibrary().getBignumClass(), value);
return Layouts.BIGNUM.createBignum(getContext().getCoreLibrary().getBignumFactory(), value);
}

@Override
Original file line number Diff line number Diff line change
@@ -438,7 +438,7 @@ public Object mod(long a, DynamicObject b) {

if (mod < 0 && Layouts.BIGNUM.getValue(b).compareTo(BigInteger.ZERO) > 0 || mod > 0 && Layouts.BIGNUM.getValue(b).compareTo(BigInteger.ZERO) < 0) {
adjustProfile.enter();
return BignumNodes.createRubyBignum(getContext().getCoreLibrary().getBignumClass(), BigInteger.valueOf(mod).add(Layouts.BIGNUM.getValue(b)));
return Layouts.BIGNUM.createBignum(getContext().getCoreLibrary().getBignumFactory(), BigInteger.valueOf(mod).add(Layouts.BIGNUM.getValue(b)));
}

return mod;
@@ -1051,7 +1051,7 @@ public long absInBounds(long n) {
@Specialization(contains = "absInBounds")
public Object abs(long n) {
if (n == Long.MIN_VALUE) {
return BignumNodes.createRubyBignum(getContext().getCoreLibrary().getBignumClass(), BigInteger.valueOf(n).abs());
return Layouts.BIGNUM.createBignum(getContext().getCoreLibrary().getBignumFactory(), BigInteger.valueOf(n).abs());
}
return (n < 0) ? -n : n;
}
Original file line number Diff line number Diff line change
@@ -14,7 +14,6 @@
import com.oracle.truffle.api.object.DynamicObject;
import com.oracle.truffle.api.source.SourceSection;
import com.oracle.truffle.api.utilities.ConditionProfile;
import org.jruby.truffle.nodes.core.BignumNodes;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.layouts.Layouts;

@@ -43,7 +42,7 @@ public DynamicObject pow(DynamicObject a, long b) {
return null; // Primitive failure
} else {
// TODO CS 15-Feb-15 what about this cast?
return BignumNodes.createRubyBignum(getContext().getCoreLibrary().getBignumClass(), Layouts.BIGNUM.getValue(a).pow((int) b));
return Layouts.BIGNUM.createBignum(getContext().getCoreLibrary().getBignumFactory(), Layouts.BIGNUM.getValue(a).pow((int) b));
}
}

Original file line number Diff line number Diff line change
@@ -10,15 +10,20 @@
package org.jruby.truffle.nodes.rubinius;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.object.DynamicObject;
import com.oracle.truffle.api.source.SourceSection;
import com.oracle.truffle.api.utilities.ConditionProfile;
import org.jruby.truffle.nodes.objects.IsTaintedNode;
import org.jruby.truffle.nodes.objects.IsTaintedNodeGen;
import org.jruby.truffle.nodes.objects.TaintNode;
import org.jruby.truffle.nodes.objects.TaintNodeGen;
import org.jruby.truffle.nodes.objectstorage.ReadHeadObjectFieldNode;
import org.jruby.truffle.nodes.objectstorage.WriteHeadObjectFieldNode;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.layouts.Layouts;
import org.jruby.truffle.runtime.object.ObjectIDOperations;

/**
@@ -29,6 +34,9 @@ public abstract class ObjectPrimitiveNodes {
@RubiniusPrimitive(name = "object_id")
public abstract static class ObjectIDPrimitiveNode extends RubiniusPrimitiveNode {

@Child private ReadHeadObjectFieldNode readObjectIdNode;
@Child private WriteHeadObjectFieldNode writeObjectIdNode;

public ObjectIDPrimitiveNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}
@@ -60,20 +68,10 @@ public long objectIDSmallFixnumOverflow(long value) throws ArithmeticException {
return ObjectIDOperations.smallFixnumToIDOverflow(value);
}

/* TODO: Ideally we would have this instead of the code below to speculate better. [GRAAL-903]
@Specialization(guards = "isSmallFixnum")
public long objectIDSmallFixnum(long value) {
return ObjectIDOperations.smallFixnumToID(value);
}
@Specialization(guards = "!isSmallFixnum")
public Object objectIDLargeFixnum(long value) {
return ObjectIDOperations.largeFixnumToID(getContext(), value);
} */

@Specialization
public Object objectID(long value) {
if (isSmallFixnum(value)) {
public Object objectID(long value,
@Cached("createCountingProfile()") ConditionProfile smallProfile) {
if (smallProfile.profile(isSmallFixnum(value))) {
return ObjectIDOperations.smallFixnumToID(value);
} else {
return ObjectIDOperations.largeFixnumToID(getContext(), value);
@@ -86,9 +84,26 @@ public Object objectID(double value) {
}

@Specialization
public long objectID(DynamicObject object) {
// TODO: CS 22-Mar-15 need to write this using nodes
return ObjectIDOperations.verySlowGetObjectID(object);
public Object objectID(DynamicObject object) {
if (readObjectIdNode == null) {
CompilerDirectives.transferToInterpreter();
readObjectIdNode = insert(new ReadHeadObjectFieldNode(Layouts.OBJECT_ID_IDENTIFIER));
}

final Object id = readObjectIdNode.execute(object);

if (id == nil()) {
if (writeObjectIdNode == null) {
CompilerDirectives.transferToInterpreter();
writeObjectIdNode = insert(new WriteHeadObjectFieldNode(Layouts.OBJECT_ID_IDENTIFIER));
}

final Long newId = getContext().getNextObjectID();
writeObjectIdNode.execute(object, newId);
return newId;
}

return id;
}

protected boolean isSmallFixnum(long fixnum) {
Original file line number Diff line number Diff line change
@@ -18,8 +18,8 @@
import org.jruby.RubyNumeric;
import org.jruby.RubyRandom;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.truffle.nodes.core.BignumNodes;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.layouts.Layouts;

import java.math.BigInteger;

@@ -86,7 +86,7 @@ public RandomizerGenSeedPrimitiveNode(RubyContext context, SourceSection sourceS
@Specialization
public DynamicObject randomizerGenSeed(DynamicObject random) {
BigInteger integer = RandomPrimitiveHelper.randomSeed(getContext().getRuntime());
return BignumNodes.createRubyBignum(getContext().getCoreLibrary().getBignumClass(), integer);
return Layouts.BIGNUM.createBignum(getContext().getCoreLibrary().getBignumFactory(), integer);
}
}

Original file line number Diff line number Diff line change
@@ -37,7 +37,6 @@
import org.jruby.truffle.nodes.RubyNode;
import org.jruby.truffle.nodes.RubyRootNode;
import org.jruby.truffle.nodes.control.SequenceNode;
import org.jruby.truffle.nodes.core.BignumNodes;
import org.jruby.truffle.nodes.core.LoadRequiredLibrariesNode;
import org.jruby.truffle.nodes.core.SetTopLevelBindingNode;
import org.jruby.truffle.nodes.dispatch.CallDispatchHeadNode;
@@ -492,8 +491,7 @@ public Object toTruffle(IRubyObject object) {
return ((org.jruby.RubyFloat) object).getDoubleValue();
} else if (object instanceof org.jruby.RubyBignum) {
final BigInteger value = ((org.jruby.RubyBignum) object).getBigIntegerValue();

return BignumNodes.createRubyBignum(coreLibrary.getBignumClass(), value);
return Layouts.BIGNUM.createBignum(coreLibrary.getBignumFactory(), value);
} else if (object instanceof org.jruby.RubyString) {
return toTruffle((org.jruby.RubyString) object);
} else if (object instanceof org.jruby.RubySymbol) {
Original file line number Diff line number Diff line change
@@ -13,7 +13,6 @@
import com.oracle.truffle.api.ExactMath;
import com.oracle.truffle.api.object.DynamicObject;
import com.oracle.truffle.api.object.Property;
import org.jruby.truffle.nodes.core.BignumNodes;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.layouts.Layouts;

@@ -71,13 +70,13 @@ public static long smallFixnumToID(long fixnum) {
public static DynamicObject largeFixnumToID(RubyContext context, long fixnum) {
assert !isSmallFixnum(fixnum);
BigInteger big = unsignedBigInteger(fixnum);
return BignumNodes.createRubyBignum(context.getCoreLibrary().getBignumClass(), big.or(LARGE_FIXNUM_FLAG));
return Layouts.BIGNUM.createBignum(context.getCoreLibrary().getBignumFactory(), big.or(LARGE_FIXNUM_FLAG));
}

public static DynamicObject floatToID(RubyContext context, double value) {
long bits = Double.doubleToRawLongBits(value);
BigInteger big = unsignedBigInteger(bits);
return BignumNodes.createRubyBignum(context.getCoreLibrary().getBignumClass(), big.or(FLOAT_FLAG));
return Layouts.BIGNUM.createBignum(context.getCoreLibrary().getBignumFactory(), big.or(FLOAT_FLAG));
}

// ID => primitive
Original file line number Diff line number Diff line change
@@ -43,7 +43,6 @@
import jnr.posix.FileStat;
import org.jcodings.specific.UTF8Encoding;
import org.jruby.RubyString;
import org.jruby.truffle.nodes.core.BignumNodes;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.layouts.Layouts;
import org.jruby.util.StringSupport;
@@ -164,7 +163,7 @@ public static void load(RubiniusConfiguration configuration, RubyContext context
}

protected static DynamicObject newBignum(RubyContext context, String value) {
return BignumNodes.createRubyBignum(context.getCoreLibrary().getBignumClass(), new BigInteger(value));
return Layouts.BIGNUM.createBignum(context.getCoreLibrary().getBignumFactory(), new BigInteger(value));
}

protected static DynamicObject string(RubyContext context, String value) {
Original file line number Diff line number Diff line change
@@ -317,7 +317,7 @@ public RubyNode visitBignumNode(org.jruby.ast.BignumNode node) {
final RubyNode ret;

if (value.bitLength() >= 64) {
ret = new LiteralNode(context, sourceSection, BignumNodes.createRubyBignum(context.getCoreLibrary().getBignumClass(), node.getValue()));
ret = new LiteralNode(context, sourceSection, Layouts.BIGNUM.createBignum(context.getCoreLibrary().getBignumFactory(), node.getValue()));
} else {
ret = new FixnumLiteralNode.LongFixnumLiteralNode(context, sourceSection, value.longValue());
}

0 comments on commit b71bf50

Please sign in to comment.