Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jruby/jruby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 2d142dab68c5
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 68a1d95258e9
Choose a head ref
  • 2 commits
  • 3 files changed
  • 1 contributor

Commits on Jul 13, 2015

  1. [Truffle] Refactor RangeLiteralNode.

    * Create lowered ranges (IntegerFixnumRange) directly.
    eregon committed Jul 13, 2015
    Copy the full SHA
    a347363 View commit details
  2. [Truffle] Remove the lowering of ranges from FixnumLowerNode.

    * It is done eagerly at creation time now.
    eregon committed Jul 13, 2015
    Copy the full SHA
    68a1d95 View commit details
Original file line number Diff line number Diff line change
@@ -29,7 +29,6 @@ public class FixnumLowerNode extends RubyNode {
@CompilationFinal private boolean hasSeenUndefined = false;

@CompilationFinal private boolean hasNeededToLowerLongFixnum = false;
@CompilationFinal private boolean hasNeededToLowerLongFixnumRange = false;

public FixnumLowerNode(RubyNode child) {
super(child.getContext(), child.getEncapsulatingSourceSection());
@@ -57,11 +56,7 @@ public Object execute(VirtualFrame frame) {
}

if (hasSeenLongRange && value instanceof RubyRange.LongFixnumRange) {
if (canLower((RubyRange.LongFixnumRange) value)) {
return lower((RubyRange.LongFixnumRange) value);
} else {
return value;
}
return value;
}

if (hasSeenUndefined && value instanceof NotProvided) {
@@ -91,11 +86,7 @@ public Object execute(VirtualFrame frame) {

if (value instanceof RubyRange.LongFixnumRange) {
hasSeenLongRange = true;
if (canLower((RubyRange.LongFixnumRange) value)) {
return lower((RubyRange.LongFixnumRange) value);
} else {
return value;
}
return value;
}

if (value instanceof NotProvided) {
@@ -124,9 +115,6 @@ public int executeInteger(VirtualFrame frame) throws UnexpectedResultException {
if (e.getResult() instanceof Long && canLower((long) e.getResult())) {
hasNeededToLowerLongFixnum = true;
return lower((long) e.getResult());
} else if (e.getResult() instanceof RubyRange.LongFixnumRange && canLower((RubyRange.LongFixnumRange) e.getResult())) {
hasNeededToLowerLongFixnumRange = true;
throw new UnexpectedResultException(lower((RubyRange.LongFixnumRange) e.getResult()));
} else {
throw e;
}
@@ -138,39 +126,6 @@ public long executeLong(VirtualFrame frame) throws UnexpectedResultException {
throw new RuntimeException();
}

@Override
public RubyRange.IntegerFixnumRange executeIntegerFixnumRange(VirtualFrame frame) throws UnexpectedResultException {
try {
if (hasNeededToLowerLongFixnumRange) {
final RubyRange.LongFixnumRange range = super.executeLongFixnumRange(frame);

if (canLower(range)) {
return lower(range);
} else {
throw new UnexpectedResultException(range);
}
} else {
return super.executeIntegerFixnumRange(frame);
}
} catch (UnexpectedResultException e) {
if (e.getResult() instanceof Long && canLower((long) e.getResult())) {
hasNeededToLowerLongFixnum = true;
throw new UnexpectedResultException(lower((long) e.getResult()));
} else if (e.getResult() instanceof RubyRange.LongFixnumRange && canLower((RubyRange.LongFixnumRange) e.getResult())) {
hasNeededToLowerLongFixnumRange = true;
return lower((RubyRange.LongFixnumRange) e.getResult());
} else {
throw e;
}
}
}

@Override
public RubyRange.LongFixnumRange executeLongFixnumRange(VirtualFrame frame) throws UnexpectedResultException {
throw new RuntimeException();

}

@Override
public NotProvided executeNotProvided(VirtualFrame frame) throws UnexpectedResultException {
try {
@@ -179,9 +134,6 @@ public NotProvided executeNotProvided(VirtualFrame frame) throws UnexpectedResul
if (e.getResult() instanceof Long && canLower((long) e.getResult())) {
hasNeededToLowerLongFixnum = true;
throw new UnexpectedResultException(lower((long) e.getResult()));
} else if (e.getResult() instanceof RubyRange.LongFixnumRange && canLower((RubyRange.LongFixnumRange) e.getResult())) {
hasNeededToLowerLongFixnumRange = true;
throw new UnexpectedResultException(e.getResult());
} else {
throw e;
}
@@ -197,13 +149,4 @@ private static int lower(long value) {
return (int) value;
}

private static boolean canLower(RubyRange.LongFixnumRange range) {
return canLower(range.getBegin()) && canLower(range.getEnd());
}

private static RubyRange.IntegerFixnumRange lower(RubyRange.LongFixnumRange range) {
assert canLower(range);
return new RubyRange.IntegerFixnumRange(range.getContext().getCoreLibrary().getRangeClass(), lower(range.getBegin()), lower(range.getEnd()), range.doesExcludeEnd());
}

}
Original file line number Diff line number Diff line change
@@ -9,31 +9,27 @@
*/
package org.jruby.truffle.nodes.literal;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.dsl.NodeChild;
import com.oracle.truffle.api.dsl.NodeChildren;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.source.SourceSection;
import com.oracle.truffle.api.utilities.BranchProfile;
import org.jruby.truffle.nodes.RubyGuards;
import org.jruby.truffle.nodes.RubyNode;
import org.jruby.truffle.nodes.dispatch.CallDispatchHeadNode;
import org.jruby.truffle.nodes.dispatch.DispatchHeadNodeFactory;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.control.RaiseException;
import org.jruby.truffle.runtime.core.CoreLibrary;
import org.jruby.truffle.runtime.core.RubyRange;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.dsl.NodeChild;
import com.oracle.truffle.api.dsl.NodeChildren;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.source.SourceSection;

@NodeChildren({@NodeChild("begin"), @NodeChild("end")})
public abstract class RangeLiteralNode extends RubyNode {

private final boolean excludeEnd;

private final BranchProfile beginIntegerProfile = BranchProfile.create();
private final BranchProfile beginLongProfile = BranchProfile.create();
private final BranchProfile endIntegerProfile = BranchProfile.create();
private final BranchProfile endLongProfile = BranchProfile.create();
private final BranchProfile objectProfile = BranchProfile.create();

@Child private CallDispatchHeadNode cmpNode;

public RangeLiteralNode(RubyContext context, SourceSection sourceSection, boolean excludeEnd) {
@@ -42,62 +38,28 @@ public RangeLiteralNode(RubyContext context, SourceSection sourceSection, boolea
}

@Specialization
public RubyRange.IntegerFixnumRange doRange(int begin, int end) {
public RubyRange.IntegerFixnumRange intRange(int begin, int end) {
return new RubyRange.IntegerFixnumRange(getContext().getCoreLibrary().getRangeClass(), begin, end, excludeEnd);
}

@Specialization
public RubyRange.LongFixnumRange doRange(int begin, long end) {
return new RubyRange.LongFixnumRange(getContext().getCoreLibrary().getRangeClass(), begin, end, excludeEnd);
}

@Specialization
public RubyRange.LongFixnumRange doRange(long begin, int end) {
return new RubyRange.LongFixnumRange(getContext().getCoreLibrary().getRangeClass(), begin, end, excludeEnd);
@Specialization(guards = { "fitsIntoInteger(begin)", "fitsIntoInteger(end)" })
public RubyRange.IntegerFixnumRange longFittingIntRange(long begin, long end) {
return new RubyRange.IntegerFixnumRange(getContext().getCoreLibrary().getRangeClass(), (int) begin, (int) end, excludeEnd);
}

@Specialization
public RubyRange.LongFixnumRange doRange(long begin, long end) {
@Specialization(guards = "!fitsIntoInteger(begin) || !fitsIntoInteger(end)")
public RubyRange.LongFixnumRange longRange(long begin, long end) {
return new RubyRange.LongFixnumRange(getContext().getCoreLibrary().getRangeClass(), begin, end, excludeEnd);
}

@Specialization
@Specialization(guards = { "!isIntOrLong(begin) || !isIntOrLong(end)" })
public Object doRange(VirtualFrame frame, Object begin, Object end) {
if (begin instanceof Integer) {
beginIntegerProfile.enter();

if (end instanceof Integer) {
endIntegerProfile.enter();
return doRange((int) begin, (int) end);
}

if (end instanceof Long) {
endLongProfile.enter();
return doRange((int) begin, (long) end);
}
} else if (begin instanceof Long) {
beginLongProfile.enter();

if (end instanceof Integer) {
endIntegerProfile.enter();
return doRange((long) begin, (int) end);
}

if (end instanceof Long) {
endLongProfile.enter();
return doRange((long) begin, (long) end);
}
}

objectProfile.enter();

if (cmpNode == null) {
CompilerDirectives.transferToInterpreter();
cmpNode = insert(DispatchHeadNodeFactory.createMethodCall(getContext()));
}

final Object cmpResult;

try {
cmpResult = cmpNode.call(frame, begin, "<=>", null, end);
} catch (RaiseException e) {
@@ -111,4 +73,12 @@ public Object doRange(VirtualFrame frame, Object begin, Object end) {
return new RubyRange.ObjectRange(getContext().getCoreLibrary().getRangeClass(), begin, end, excludeEnd);
}

protected boolean fitsIntoInteger(long value) {
return CoreLibrary.fitsIntoInteger(value);
}

protected boolean isIntOrLong(Object value) {
return RubyGuards.isInteger(value) || RubyGuards.isLong(value);
}

}
Original file line number Diff line number Diff line change
@@ -69,6 +69,7 @@ public static class LongFixnumRange extends RubyRange {

public LongFixnumRange(RubyClass rangeClass, long begin, long end, boolean excludeEnd) {
super(rangeClass);
assert !CoreLibrary.fitsIntoInteger(begin) || !CoreLibrary.fitsIntoInteger(end);
this.begin = begin;
this.end = end;
this.excludeEnd = excludeEnd;