Skip to content

Commit

Permalink
[Truffle] Make a couple of helper nodes subclasses of RubyNode.
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisseaton committed Dec 29, 2014
1 parent d7e2d71 commit b542641
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 26 deletions.
Expand Up @@ -28,7 +28,7 @@ public static abstract class BignumCoreMethodNode extends CoreMethodNode {

public BignumCoreMethodNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
fixnumOrBignum = new FixnumOrBignumNode(context);
fixnumOrBignum = new FixnumOrBignumNode(context, sourceSection);
}

public BignumCoreMethodNode(BignumCoreMethodNode prev) {
Expand Down Expand Up @@ -255,12 +255,12 @@ public abstract static class DivModNode extends CoreMethodNode {

public DivModNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
divModNode = new GeneralDivModNode(context);
divModNode = new GeneralDivModNode(context, sourceSection);
}

public DivModNode(DivModNode prev) {
super(prev);
divModNode = new GeneralDivModNode(getContext());
divModNode = prev.divModNode;
}

@Specialization
Expand Down
Expand Up @@ -552,12 +552,12 @@ public abstract static class DivModNode extends CoreMethodNode {

public DivModNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
divModNode = new GeneralDivModNode(context);
divModNode = new GeneralDivModNode(context, sourceSection);
}

public DivModNode(DivModNode prev) {
super(prev);
divModNode = new GeneralDivModNode(getContext());
divModNode = prev.divModNode;
}

@Specialization
Expand Down
Expand Up @@ -10,22 +10,23 @@
package org.jruby.truffle.nodes.core;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.source.SourceSection;
import com.oracle.truffle.api.utilities.BranchProfile;
import org.jruby.truffle.nodes.RubyNode;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.core.RubyBignum;

import java.math.BigDecimal;
import java.math.BigInteger;

public class FixnumOrBignumNode extends Node {
public class FixnumOrBignumNode extends RubyNode {

public FixnumOrBignumNode(RubyContext context) {
this.context = context;
public FixnumOrBignumNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}

private final RubyContext context;

private final BranchProfile lowerProfile = BranchProfile.create();
private final BranchProfile integerFromBignumProfile = BranchProfile.create();
private final BranchProfile longFromBignumProfile = BranchProfile.create();
Expand Down Expand Up @@ -69,12 +70,16 @@ public Object fixnumOrBignum(double value) {

bignumProfile.enter();

return new RubyBignum(context.getCoreLibrary().getBignumClass(), doubleToBigInteger(value));
return new RubyBignum(getContext().getCoreLibrary().getBignumClass(), doubleToBigInteger(value));
}

@CompilerDirectives.TruffleBoundary
private static BigInteger doubleToBigInteger(double value) {
return new BigDecimal(value).toBigInteger();
}

public Object execute(VirtualFrame frame) {
throw new UnsupportedOperationException();
}

}
Expand Up @@ -563,7 +563,7 @@ public abstract static class RoundNode extends CoreMethodNode {

public RoundNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
fixnumOrBignum = new FixnumOrBignumNode(context);
fixnumOrBignum = new FixnumOrBignumNode(context, sourceSection);
}

public RoundNode(RoundNode prev) {
Expand Down Expand Up @@ -617,7 +617,7 @@ public abstract static class ToINode extends CoreMethodNode {

public ToINode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
fixnumOrBignum = new FixnumOrBignumNode(context);
fixnumOrBignum = new FixnumOrBignumNode(context, sourceSection);
}

public ToINode(ToINode prev) {
Expand Down
Expand Up @@ -10,17 +10,18 @@
package org.jruby.truffle.nodes.core;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.source.SourceSection;
import com.oracle.truffle.api.utilities.BranchProfile;
import org.jruby.truffle.nodes.RubyNode;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.core.RubyArray;
import org.jruby.truffle.runtime.core.RubyBignum;

import java.math.BigInteger;

public class GeneralDivModNode extends Node {

private final RubyContext context;
public class GeneralDivModNode extends RubyNode {

@Child protected FixnumOrBignumNode fixnumOrBignumQuotient;
@Child protected FixnumOrBignumNode fixnumOrBignumRemainder;
Expand All @@ -31,11 +32,10 @@ public class GeneralDivModNode extends Node {
private final BranchProfile useFixnumPairProfile = BranchProfile.create();
private final BranchProfile useObjectPairProfile = BranchProfile.create();

public GeneralDivModNode(RubyContext context) {
assert context != null;
this.context = context;
fixnumOrBignumQuotient = new FixnumOrBignumNode(context);
fixnumOrBignumRemainder = new FixnumOrBignumNode(context);
public GeneralDivModNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
fixnumOrBignumQuotient = new FixnumOrBignumNode(context, sourceSection);
fixnumOrBignumRemainder = new FixnumOrBignumNode(context, sourceSection);
}

public RubyArray execute(int a, int b) {
Expand Down Expand Up @@ -109,13 +109,13 @@ private RubyArray divMod(long a, long b) {

if (integerDiv instanceof Long && ((long) integerDiv) >= Integer.MIN_VALUE && ((long) integerDiv) <= Integer.MAX_VALUE && mod >= Integer.MIN_VALUE && mod <= Integer.MAX_VALUE) {
useFixnumPairProfile.enter();
return new RubyArray(context.getCoreLibrary().getArrayClass(), new int[]{(int) (long) integerDiv, (int) mod}, 2);
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), new int[]{(int) (long) integerDiv, (int) mod}, 2);
} else if (integerDiv instanceof Long) {
useObjectPairProfile.enter();
return new RubyArray(context.getCoreLibrary().getArrayClass(), new Object[]{integerDiv, mod}, 2);
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), new Object[]{integerDiv, mod}, 2);
} else {
useObjectPairProfile.enter();
return new RubyArray(context.getCoreLibrary().getArrayClass(), new Object[]{
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), new Object[]{
fixnumOrBignumQuotient.fixnumOrBignum(create((BigInteger) integerDiv)),
mod}, 2);
}
Expand All @@ -136,13 +136,18 @@ private RubyArray divMod(BigInteger a, BigInteger b) {
bigIntegerResults[1] = b.add(bigIntegerResults[1]);
}

return new RubyArray(context.getCoreLibrary().getArrayClass(), new Object[]{
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), new Object[]{
fixnumOrBignumQuotient.fixnumOrBignum(create(bigIntegerResults[0])),
fixnumOrBignumRemainder.fixnumOrBignum(create(bigIntegerResults[1]))}, 2);
}

public RubyBignum create(BigInteger value) {
return new RubyBignum(context.getCoreLibrary().getBignumClass(), value);
return new RubyBignum(getContext().getCoreLibrary().getBignumClass(), value);
}

@Override
public Object execute(VirtualFrame frame) {
throw new UnsupportedOperationException();
}

}

0 comments on commit b542641

Please sign in to comment.