Skip to content

Commit

Permalink
[Truffle] Remove last instances of CoreLibrary.isTruthy.
Browse files Browse the repository at this point in the history
* Introduce PredicateDispatchHeadNode which is simply
  BooleanCastNode(DispatchHeadNode)
  • Loading branch information
eregon committed Dec 4, 2014
1 parent 1c409cc commit 45a50d1
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 67 deletions.
Expand Up @@ -13,21 +13,21 @@
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.nodes.UnexpectedResultException;
import org.jruby.truffle.nodes.RubyNode;
import org.jruby.truffle.nodes.dispatch.DispatchHeadNode;
import org.jruby.truffle.nodes.dispatch.PredicateDispatchHeadNode;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.core.RubyArray;

public class WhenSplatNode extends RubyNode {

@Child protected RubyNode readCaseExpression;
@Child protected RubyNode splat;
@Child protected DispatchHeadNode dispatchCaseEqual;
@Child protected PredicateDispatchHeadNode dispatchCaseEqual;

public WhenSplatNode(RubyContext context, SourceSection sourceSection, RubyNode readCaseExpression, RubyNode splat) {
super(context, sourceSection);
this.readCaseExpression = readCaseExpression;
this.splat = splat;
dispatchCaseEqual = new DispatchHeadNode(context);
dispatchCaseEqual = new PredicateDispatchHeadNode(context);
}

@Override
Expand All @@ -45,7 +45,7 @@ public boolean executeBoolean(VirtualFrame frame) {
}

for (Object value : array.slowToArray()) {
if (dispatchCaseEqual.callIsTruthy(frame, caseExpression, "===", null, value)) {
if (dispatchCaseEqual.call(frame, caseExpression, "===", null, value)) {
return true;
}
}
Expand Down
Expand Up @@ -27,6 +27,7 @@
import org.jruby.truffle.nodes.RubyRootNode;
import org.jruby.truffle.nodes.dispatch.Dispatch;
import org.jruby.truffle.nodes.dispatch.DispatchHeadNode;
import org.jruby.truffle.nodes.dispatch.PredicateDispatchHeadNode;
import org.jruby.truffle.nodes.methods.arguments.MissingArgumentBehaviour;
import org.jruby.truffle.nodes.methods.arguments.ReadPreArgumentNode;
import org.jruby.truffle.nodes.methods.locals.ReadLevelVariableNodeFactory;
Expand Down Expand Up @@ -430,11 +431,11 @@ public RubyArray orObject(RubyArray a, RubyArray b) {
@CoreMethod(names = {"==", "eql?"}, required = 1)
public abstract static class EqualNode extends ArrayCoreMethodNode {

@Child protected DispatchHeadNode equals;
@Child protected PredicateDispatchHeadNode equals;

public EqualNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
equals = new DispatchHeadNode(context);
equals = new PredicateDispatchHeadNode(context);
}

public EqualNode(EqualNode prev) {
Expand Down Expand Up @@ -503,7 +504,7 @@ public boolean equal(VirtualFrame frame, RubyArray a, RubyArray b) {
final Object[] bs = b.slowToArray();

for (int n = 0; n < a.getSize(); n++) {
if (!equals.callIsTruthy(frame, as[n], "==", null, bs[n])) {
if (!equals.call(frame, as[n], "==", null, bs[n])) {
return false;
}
}
Expand Down
Expand Up @@ -11,8 +11,6 @@

import java.util.*;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.ExactMath;
import com.oracle.truffle.api.source.*;
import com.oracle.truffle.api.dsl.*;
import com.oracle.truffle.api.frame.VirtualFrame;
Expand All @@ -21,6 +19,7 @@
import org.jruby.truffle.nodes.cast.BooleanCastNodeFactory;
import org.jruby.truffle.nodes.dispatch.Dispatch;
import org.jruby.truffle.nodes.dispatch.DispatchHeadNode;
import org.jruby.truffle.nodes.dispatch.PredicateDispatchHeadNode;
import org.jruby.truffle.nodes.yield.YieldDispatchHeadNode;
import org.jruby.truffle.runtime.*;
import org.jruby.truffle.runtime.control.RaiseException;
Expand Down Expand Up @@ -55,11 +54,11 @@ public boolean not(boolean value) {
@CoreMethod(names = "!=", required = 1)
public abstract static class NotEqualNode extends CoreMethodNode {

@Child protected DispatchHeadNode equalNode;
@Child protected PredicateDispatchHeadNode equalNode;

public NotEqualNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
equalNode = new DispatchHeadNode(context);
equalNode = new PredicateDispatchHeadNode(context);
}

public NotEqualNode(NotEqualNode prev) {
Expand All @@ -69,7 +68,7 @@ public NotEqualNode(NotEqualNode prev) {

@Specialization
public boolean equal(VirtualFrame frame, Object a, Object b) {
return !equalNode.callIsTruthy(frame, a, "==", null, b);
return !equalNode.call(frame, a, "==", null, b);
}

}
Expand Down
32 changes: 16 additions & 16 deletions core/src/main/java/org/jruby/truffle/nodes/core/HashNodes.java
Expand Up @@ -18,9 +18,9 @@
import com.oracle.truffle.api.frame.*;
import com.oracle.truffle.api.utilities.BranchProfile;
import org.jruby.runtime.Visibility;
import org.jruby.truffle.nodes.RubyNode;
import org.jruby.truffle.nodes.RubyRootNode;
import org.jruby.truffle.nodes.dispatch.DispatchHeadNode;
import org.jruby.truffle.nodes.dispatch.PredicateDispatchHeadNode;
import org.jruby.truffle.nodes.yield.YieldDispatchHeadNode;
import org.jruby.truffle.runtime.*;
import org.jruby.truffle.runtime.core.*;
Expand All @@ -33,11 +33,11 @@ public abstract class HashNodes {
@CoreMethod(names = "==", required = 1)
public abstract static class EqualNode extends HashCoreMethodNode {

@Child protected DispatchHeadNode equalNode;
@Child protected PredicateDispatchHeadNode equalNode;

public EqualNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
equalNode = new DispatchHeadNode(context);
equalNode = new PredicateDispatchHeadNode(context);
}

public EqualNode(EqualNode prev) {
Expand Down Expand Up @@ -69,7 +69,7 @@ public boolean equalObjectArray(VirtualFrame frame, RubyHash a, RubyHash b) {
}

for (int n = 0; n < aSize * 2; n++) {
if (!equalNode.callIsTruthy(frame, aStore[n], "==", null, bStore[n])) {
if (!equalNode.call(frame, aStore[n], "==", null, bStore[n])) {
return false;
}
}
Expand Down Expand Up @@ -199,15 +199,15 @@ public RubyHash constructObjectLinkedMapMap(Object[] args) {
@CoreMethod(names = "[]", required = 1)
public abstract static class GetIndexNode extends HashCoreMethodNode {

@Child protected DispatchHeadNode eqlNode;
@Child protected PredicateDispatchHeadNode eqlNode;
@Child protected YieldDispatchHeadNode yield;

private final BranchProfile notInHashProfile = new BranchProfile();
private final BranchProfile useDefaultProfile = new BranchProfile();

public GetIndexNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
eqlNode = new DispatchHeadNode(context);
eqlNode = new PredicateDispatchHeadNode(context);
yield = new YieldDispatchHeadNode(context);
}

Expand Down Expand Up @@ -237,7 +237,7 @@ public Object getObjectArray(VirtualFrame frame, RubyHash hash, Object key) {
final int size = hash.getStoreSize();

for (int n = 0; n < RubyHash.HASHES_SMALL; n++) {
if (n < size && eqlNode.callIsTruthy(frame, store[n * 2], "eql?", null, key)) {
if (n < size && eqlNode.call(frame, store[n * 2], "eql?", null, key)) {
return store[n * 2 + 1];
}
}
Expand Down Expand Up @@ -285,15 +285,15 @@ public Object getObjectLinkedHashMap(VirtualFrame frame, RubyHash hash, Object k
@CoreMethod(names = "[]=", required = 2)
public abstract static class SetIndexNode extends HashCoreMethodNode {

@Child protected DispatchHeadNode eqlNode;
@Child protected PredicateDispatchHeadNode eqlNode;

private final BranchProfile considerExtendProfile = new BranchProfile();
private final BranchProfile extendProfile = new BranchProfile();
private final BranchProfile transitionToLinkedHashMapProfile = new BranchProfile();

public SetIndexNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
eqlNode = new DispatchHeadNode(context);
eqlNode = new PredicateDispatchHeadNode(context);
}

public SetIndexNode(SetIndexNode prev) {
Expand All @@ -320,7 +320,7 @@ public Object setObjectArray(VirtualFrame frame, RubyHash hash, Object key, Obje
final int size = hash.getStoreSize();

for (int n = 0; n < RubyHash.HASHES_SMALL; n++) {
if (n < size && eqlNode.callIsTruthy(frame, store[n * 2], "eql?", null, key)) {
if (n < size && eqlNode.call(frame, store[n * 2], "eql?", null, key)) {
store[n * 2 + 1] = value;
return value;
}
Expand Down Expand Up @@ -723,11 +723,11 @@ public RubyString inspectObjectLinkedHashMap(VirtualFrame frame, RubyHash hash)
@CoreMethod(names = "key?", required = 1)
public abstract static class KeyNode extends HashCoreMethodNode {

@Child protected DispatchHeadNode eqlNode;
@Child protected PredicateDispatchHeadNode eqlNode;

public KeyNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
eqlNode = new DispatchHeadNode(context);
eqlNode = new PredicateDispatchHeadNode(context);
}

public KeyNode(KeyNode prev) {
Expand All @@ -748,7 +748,7 @@ public boolean keyObjectArray(VirtualFrame frame, RubyHash hash, Object key) {
final Object[] store = (Object[]) hash.getStore();

for (int n = 0; n < store.length; n += 2) {
if (n < size && eqlNode.callIsTruthy(frame, store[n], "eql?", null, key)) {
if (n < size && eqlNode.call(frame, store[n], "eql?", null, key)) {
return true;
}
}
Expand Down Expand Up @@ -896,7 +896,7 @@ public RubyArray mapObjectLinkedHashMap(VirtualFrame frame, RubyHash hash, RubyP
@CoreMethod(names = "merge", required = 1)
public abstract static class MergeNode extends HashCoreMethodNode {

@Child protected DispatchHeadNode eqlNode;
@Child protected PredicateDispatchHeadNode eqlNode;

private final BranchProfile nothingFromFirstProfile = new BranchProfile();
private final BranchProfile considerNothingFromSecondProfile = new BranchProfile();
Expand All @@ -908,7 +908,7 @@ public abstract static class MergeNode extends HashCoreMethodNode {

public MergeNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
eqlNode = new DispatchHeadNode(context);
eqlNode = new PredicateDispatchHeadNode(context);
}

public MergeNode(MergeNode prev) {
Expand Down Expand Up @@ -944,7 +944,7 @@ public RubyHash mergeObjectArrayObjectArray(VirtualFrame frame, RubyHash hash, R

for (int b = 0; b < RubyHash.HASHES_SMALL; b++) {
if (b < storeBSize) {
if (eqlNode.callIsTruthy(frame, storeA[a * 2], "eql?", null, storeB[b * 2])) {
if (eqlNode.call(frame, storeA[a * 2], "eql?", null, storeB[b * 2])) {
merge = false;
break;
}
Expand Down
13 changes: 7 additions & 6 deletions core/src/main/java/org/jruby/truffle/nodes/core/KernelNodes.java
Expand Up @@ -26,6 +26,7 @@
import org.jruby.truffle.nodes.control.*;
import org.jruby.truffle.nodes.dispatch.Dispatch;
import org.jruby.truffle.nodes.dispatch.DispatchHeadNode;
import org.jruby.truffle.nodes.dispatch.PredicateDispatchHeadNode;
import org.jruby.truffle.nodes.literal.*;
import org.jruby.truffle.nodes.yield.*;
import org.jruby.truffle.runtime.*;
Expand All @@ -50,7 +51,7 @@ public abstract class KernelNodes {
public abstract static class SameOrEqualNode extends CoreMethodNode {

@Child protected BasicObjectNodes.ReferenceEqualNode referenceEqualNode;
@Child protected DispatchHeadNode equalNode;
@Child protected PredicateDispatchHeadNode equalNode;

public SameOrEqualNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
Expand All @@ -71,9 +72,9 @@ protected boolean areSame(VirtualFrame frame, Object left, Object right) {
protected boolean areEqual(VirtualFrame frame, Object left, Object right) {
if (equalNode == null) {
CompilerDirectives.transferToInterpreterAndInvalidate();
equalNode = insert(new DispatchHeadNode(getContext()));
equalNode = insert(new PredicateDispatchHeadNode(getContext()));
}
return equalNode.callIsTruthy(frame, left, "==", null, right);
return equalNode.call(frame, left, "==", null, right);
}

public abstract boolean executeSameOrEqual(VirtualFrame frame, Object a, Object b);
Expand Down Expand Up @@ -108,11 +109,11 @@ public RubyNilClass equal(Object other) {
@CoreMethod(names = "!~", required = 1)
public abstract static class NotMatchNode extends CoreMethodNode {

@Child protected DispatchHeadNode matchNode;
@Child protected PredicateDispatchHeadNode matchNode;

public NotMatchNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
matchNode = new DispatchHeadNode(context);
matchNode = new PredicateDispatchHeadNode(context);
}

public NotMatchNode(NotMatchNode prev) {
Expand All @@ -122,7 +123,7 @@ public NotMatchNode(NotMatchNode prev) {

@Specialization
public boolean notMatch(VirtualFrame frame, Object self, Object other) {
return !matchNode.callIsTruthy(frame, self, "=~", null, other);
return !matchNode.call(frame, self, "=~", null, other);
}

}
Expand Down
19 changes: 10 additions & 9 deletions core/src/main/java/org/jruby/truffle/nodes/core/RangeNodes.java
Expand Up @@ -16,6 +16,7 @@
import com.oracle.truffle.api.utilities.*;
import org.jruby.truffle.nodes.RubyRootNode;
import org.jruby.truffle.nodes.dispatch.DispatchHeadNode;
import org.jruby.truffle.nodes.dispatch.PredicateDispatchHeadNode;
import org.jruby.truffle.runtime.*;
import org.jruby.truffle.runtime.core.*;
import org.jruby.truffle.runtime.core.RubyArray;
Expand Down Expand Up @@ -207,15 +208,15 @@ public Object each(RubyRange.ObjectRange range) {
@CoreMethod(names = {"include?", "==="}, optional = 1, lowerFixnumSelf = true, lowerFixnumParameters = 0)
public abstract static class IncludeNode extends CoreMethodNode {

@Child protected DispatchHeadNode callLess;
@Child protected DispatchHeadNode callGreater;
@Child protected DispatchHeadNode callGreaterEqual;
@Child protected PredicateDispatchHeadNode callLess;
@Child protected PredicateDispatchHeadNode callGreater;
@Child protected PredicateDispatchHeadNode callGreaterEqual;

public IncludeNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
callLess = new DispatchHeadNode(context);
callGreater = new DispatchHeadNode(context);
callGreaterEqual = new DispatchHeadNode(context);
callLess = new PredicateDispatchHeadNode(context);
callGreater = new PredicateDispatchHeadNode(context);
callGreaterEqual = new PredicateDispatchHeadNode(context);
}

public IncludeNode(IncludeNode prev) {
Expand All @@ -234,16 +235,16 @@ public boolean include(RubyRange.IntegerFixnumRange range, int value) {
public boolean include(VirtualFrame frame, RubyRange.ObjectRange range, Object value) {
notDesignedForCompilation();

if (callLess.callIsTruthy(frame, value, "<", null, range.getBegin())) {
if (callLess.call(frame, value, "<", null, range.getBegin())) {
return false;
}

if (range.doesExcludeEnd()) {
if (callGreaterEqual.callIsTruthy(frame, value, ">=", null, range.getEnd())) {
if (callGreaterEqual.call(frame, value, ">=", null, range.getEnd())) {
return false;
}
} else {
if (callGreater.callIsTruthy(frame, value, ">", null, range.getEnd())) {
if (callGreater.call(frame, value, ">", null, range.getEnd())) {
return false;
}
}
Expand Down
Expand Up @@ -68,15 +68,6 @@ public Object call(
Dispatch.DispatchAction.CALL_METHOD);
}

public boolean callIsTruthy(
VirtualFrame frame,
Object receiverObject,
Object methodName,
RubyProc blockObject,
Object... argumentsObjects) {
return context.getCoreLibrary().isTruthy(call(frame, receiverObject, methodName, blockObject, argumentsObjects));
}

public double callFloat(
VirtualFrame frame,
Object receiverObject,
Expand Down
@@ -0,0 +1,38 @@
/*
* 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
*/
package org.jruby.truffle.nodes.dispatch;

import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.nodes.Node;
import org.jruby.truffle.nodes.cast.BooleanCastNode;
import org.jruby.truffle.nodes.cast.BooleanCastNodeFactory;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.core.RubyProc;

public class PredicateDispatchHeadNode extends Node {

@Child protected DispatchHeadNode dispatchNode;
@Child protected BooleanCastNode booleanCastNode;

public PredicateDispatchHeadNode(RubyContext context) {
dispatchNode = new DispatchHeadNode(context);
booleanCastNode = BooleanCastNodeFactory.create(context, getSourceSection(), null);
}

public boolean call(
VirtualFrame frame,
Object receiverObject,
Object methodName,
RubyProc blockObject,
Object... argumentsObjects) {
return booleanCastNode.executeBoolean(frame,
dispatchNode.call(frame, receiverObject, methodName, blockObject, argumentsObjects));
}
}

0 comments on commit 45a50d1

Please sign in to comment.