Skip to content

Commit

Permalink
[Truffle] All Fixnum specs passing!
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisseaton committed Jan 12, 2015
1 parent 2a87593 commit 47d566e
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 33 deletions.
87 changes: 62 additions & 25 deletions core/src/main/java/org/jruby/truffle/nodes/core/FixnumNodes.java
Expand Up @@ -17,7 +17,9 @@
import com.oracle.truffle.api.source.SourceSection;
import com.oracle.truffle.api.utilities.BranchProfile;
import com.oracle.truffle.api.utilities.ConditionProfile;
import org.jruby.truffle.nodes.RubyNode;
import org.jruby.truffle.nodes.dispatch.CallDispatchHeadNode;
import org.jruby.truffle.nodes.dispatch.DispatchHeadNode;
import org.jruby.truffle.nodes.dispatch.DispatchHeadNodeFactory;
import org.jruby.truffle.nodes.methods.UnsupportedOperationBehavior;
import org.jruby.truffle.runtime.RubyContext;
Expand All @@ -34,6 +36,8 @@
@CoreClass(name = "Fixnum")
public abstract class FixnumNodes {

private static final int BITS = 64;

@CoreMethod(names = "-@")
public abstract static class NegNode extends CoreMethodNode {

Expand Down Expand Up @@ -1275,21 +1279,26 @@ public Object bitXOr(long a, RubyBignum b) {
}
}

@CoreMethod(names = "<<", required = 1, lowerFixnumParameters = 0)
@CoreMethod(names = "<<", required = 1)
public abstract static class LeftShiftNode extends BignumNodes.BignumCoreMethodNode {

@Child private CallDispatchHeadNode fallbackCallNode;

public LeftShiftNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}

public LeftShiftNode(LeftShiftNode prev) {
super(prev);
fallbackCallNode = prev.fallbackCallNode;
}

protected Object lower(RubyBignum value) {
return fixnumOrBignum(value);
}

public abstract Object executeLeftShift(VirtualFrame frame, Object a, Object b);

@Specialization(guards = {"isPositive(arguments[1])", "canShiftIntoInt"})
public int leftShift(int a, int b) {
return a << b;
Expand Down Expand Up @@ -1337,6 +1346,16 @@ public long leftShiftNeg(long a, int b) {
}
}

@Specialization(guards = {"!isInteger(arguments[1])", "!isLong(arguments[1])"})
public Object leftShiftFallback(VirtualFrame frame, Object a, Object b) {
if (fallbackCallNode == null) {
CompilerDirectives.transferToInterpreter();
fallbackCallNode = insert(DispatchHeadNodeFactory.createMethodCall(getContext(), true));
}

return fallbackCallNode.call(frame, a, "left_shift_fallback", null, b);
}

static boolean canShiftIntoInt(int a, int b) {
return Integer.numberOfLeadingZeros(a) - b > 0;
}
Expand All @@ -1359,46 +1378,65 @@ static boolean isStrictlyNegative(int value) {

}

@CoreMethod(names = ">>", required = 1, lowerFixnumParameters = 0)
@CoreMethod(names = ">>", required = 1)
public abstract static class RightShiftNode extends CoreMethodNode {

@Child private CallDispatchHeadNode toInt;
@Child private CallDispatchHeadNode fallbackCallNode;
@Child private LeftShiftNode leftShiftNode;

public RightShiftNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
toInt = DispatchHeadNodeFactory.createMethodCall(context);
}

public RightShiftNode(RightShiftNode prev) {
super(prev);
toInt = prev.toInt;
fallbackCallNode = prev.fallbackCallNode;
leftShiftNode = prev.leftShiftNode;
}

protected abstract Object executeRightShift(VirtualFrame frame, Object a, Object b);

@Specialization
public int rightShift(int a, int b) {
public Object rightShift(VirtualFrame frame, int a, int b) {
if (b > 0) {
return a >> b;
} else {
if (-b >= Long.SIZE) {
return 0;
if (b >= BITS - 1) {
if (a < 0) {
return -1;
} else {
return 0;
}
} else {
return a << -b;
return a >> b;
}
} else {
if (leftShiftNode == null) {
CompilerDirectives.transferToInterpreter();
leftShiftNode = insert(FixnumNodesFactory.LeftShiftNodeFactory.create(getContext(), getSourceSection(), new RubyNode[]{null, null}));
}

return leftShiftNode.executeLeftShift(frame, a, -b);
}
}

@Specialization
public long rightShift(long a, int b) {
public Object rightShift(VirtualFrame frame, long a, int b) {
if (b > 0) {
return a >> b;
} else {
if (-b >= Long.SIZE) {
return 0;
if (b >= BITS - 1) {
if (a < 0) {
return -1;
} else {
return 0;
}

This comment has been minimized.

Copy link
@thomaswue

thomaswue Jan 13, 2015

Contributor

This could benefit from branch profiling. I expect those two cases to be rare?

This comment has been minimized.

Copy link
@chrisseaton

chrisseaton Jan 13, 2015

Author Contributor

Yes they're rare. I think rather than branch profiling I'm going to try to put them in separate specialisations with guards, which has the same effect as branch profiles, plus the advantages of them going into a priority order, and might be easier to read.

This comment has been minimized.

Copy link
@thomaswue

thomaswue Jan 13, 2015

Contributor

OK, sounds good.

} else {
return a << -b;
return a >> b;
}
} else {
if (leftShiftNode == null) {
CompilerDirectives.transferToInterpreter();
leftShiftNode = insert(FixnumNodesFactory.LeftShiftNodeFactory.create(getContext(), getSourceSection(), new RubyNode[]{null, null}));
}

return leftShiftNode.executeLeftShift(frame, a, -b);
}
}

Expand All @@ -1412,15 +1450,14 @@ public int rightShift(long a, RubyBignum b) {
return 0;
}

@Specialization(guards = {"!isInteger(arguments[1])", "!isLong(arguments[1])", "!isRubyBignum(arguments[1])"})
public Object rightShift(VirtualFrame frame, Object a, Object b) {
final Object coerced = toInt.call(frame, b, "to_int", null);

if (coerced instanceof Integer || coerced instanceof Long || coerced instanceof RubyBignum) {
return executeRightShift(frame, a, coerced);
} else {
throw new UnsupportedOperationException();
@Specialization(guards = {"!isInteger(arguments[1])", "!isLong(arguments[1])"})
public Object rightShiftFallback(VirtualFrame frame, Object a, Object b) {
if (fallbackCallNode == null) {
CompilerDirectives.transferToInterpreter();
fallbackCallNode = insert(DispatchHeadNodeFactory.createMethodCall(getContext(), true));
}

return fallbackCallNode.call(frame, a, "right_shift_fallback", null, b);
}

}
Expand Down
1 change: 1 addition & 0 deletions core/src/main/ruby/jruby/truffle/core.rb
Expand Up @@ -14,6 +14,7 @@
require_relative 'core/thread'
require_relative 'core/module'
require_relative 'core/hash'
require_relative 'core/fixnum'

require_relative 'core/rubinius/api/compat/type'

Expand Down
63 changes: 63 additions & 0 deletions core/src/main/ruby/jruby/truffle/core/fixnum.rb
@@ -0,0 +1,63 @@
# Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved. This
# code is released under a tri EPL/GPL/LGPL license. You can use it,
# redistribute it and/or modify it under the terms of the:
#
# Eclipse Public License version 1.0
# GNU General Public License version 2
# GNU Lesser General Public License version 2.1

# Copyright (c) 2007-2014, Evan Phoenix and contributors
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# * Neither the name of Rubinius nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

class Fixnum

def left_shift_fallback(other)
# Fallback from Rubinius' Fixnum#<<, after the primitive call

other = Rubinius::Type.coerce_to other, Integer, :to_int
unless other.kind_of? Fixnum
raise RangeError, "argument is out of range for a Fixnum"
end

self << other
end

private :left_shift_fallback

def right_shift_fallback(other)
# Fallback from Rubinius' Fixnum#>>, after the primitive call

other = Rubinius::Type.coerce_to other, Integer, :to_int
unless other.kind_of? Fixnum
raise RangeError, "argument is out of range for a Fixnum"
end

self >> other
end

private :right_shift_fallback

end
3 changes: 3 additions & 0 deletions core/src/main/ruby/jruby/truffle/core/rubinius/README.md
Expand Up @@ -4,6 +4,9 @@ of Rubinius. This code was written by Evan Phoenix, Brian Shirai, et al.

https://github.com/rubinius/rubinius

Some files in the parent directory contain small snippets of code from the same
repository. These are individually annotated.

`api` contains code from `rubinius-core-api`, the Rubinius API implementation
written in Ruby, in some cases modified. We have taken files from commit
8d01207061518355da9b53274fe8766ecf85fdfe. This code was written by Evan Phoenix,
Expand Down
2 changes: 0 additions & 2 deletions spec/truffle/tags/core/fixnum/left_shift_tags.txt

This file was deleted.

6 changes: 0 additions & 6 deletions spec/truffle/tags/core/fixnum/right_shift_tags.txt

This file was deleted.

1 comment on commit 47d566e

@nirvdrum
Copy link
Contributor

Choose a reason for hiding this comment

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

A glorious day!

Please sign in to comment.