Skip to content

Commit

Permalink
[Truffle] Move Array guards ro ArrayGuards and use ImportGuards.
Browse files Browse the repository at this point in the history
* Remove the duplicating classes Yielding[Type]CoreMethodNode.
  • Loading branch information
eregon committed Nov 5, 2014
1 parent a15f7c2 commit c886cc7
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 154 deletions.
Expand Up @@ -9,11 +9,11 @@
*/
package org.jruby.truffle.nodes.core;

import com.oracle.truffle.api.*;
import com.oracle.truffle.api.dsl.ImportGuards;
import com.oracle.truffle.api.source.*;
import org.jruby.truffle.runtime.*;
import org.jruby.truffle.runtime.core.RubyArray;

@ImportGuards(ArrayGuards.class)
public abstract class ArrayCoreMethodNode extends CoreMethodNode {

public ArrayCoreMethodNode(RubyContext context, SourceSection sourceSection) {
Expand All @@ -24,64 +24,4 @@ public ArrayCoreMethodNode(ArrayCoreMethodNode prev) {
super(prev);
}

protected boolean isNull(RubyArray array) {
return array.getStore() == null;
}

protected boolean isIntegerFixnum(RubyArray array) {
return array.getStore() instanceof int[];
}

protected boolean isLongFixnum(RubyArray array) {
return array.getStore() instanceof long[];
}

protected boolean isFloat(RubyArray array) {
return array.getStore() instanceof double[];
}

protected boolean isObject(RubyArray array) {
return array.getStore() instanceof Object[];
}

protected boolean isOtherNull(RubyArray array, RubyArray other) {
return other.getStore() == null;
}

protected boolean isOtherIntegerFixnum(RubyArray array, RubyArray other) {
return other.getStore() instanceof int[];
}

protected boolean isOtherLongFixnum(RubyArray array, RubyArray other) {
return other.getStore() instanceof long[];
}

protected boolean isOtherFloat(RubyArray array, RubyArray other) {
return other.getStore() instanceof double[];
}

protected boolean isOtherObject(RubyArray array, RubyArray other) {
return other.getStore() instanceof Object[];
}

protected boolean areBothNull(RubyArray a, RubyArray b) {
return a.getStore() == null && b.getStore() == null;
}

protected boolean areBothIntegerFixnum(RubyArray a, RubyArray b) {
return a.getStore() instanceof int[] && b.getStore() instanceof int[];
}

protected boolean areBothLongFixnum(RubyArray a, RubyArray b) {
return a.getStore() instanceof long[] && b.getStore() instanceof long[];
}

protected boolean areBothFloat(RubyArray a, RubyArray b) {
return a.getStore() instanceof double[] && b.getStore() instanceof double[];
}

protected boolean areBothObject(RubyArray a, RubyArray b) {
return a.getStore() instanceof Object[] && b.getStore() instanceof Object[];
}

}
76 changes: 76 additions & 0 deletions core/src/main/java/org/jruby/truffle/nodes/core/ArrayGuards.java
@@ -0,0 +1,76 @@
/*
* Copyright (c) 2013, 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.core;

import org.jruby.truffle.runtime.core.RubyArray;

public class ArrayGuards {

public static boolean isNull(RubyArray array) {
return array.getStore() == null;
}

public static boolean isIntegerFixnum(RubyArray array) {
return array.getStore() instanceof int[];
}

public static boolean isLongFixnum(RubyArray array) {
return array.getStore() instanceof long[];
}

public static boolean isFloat(RubyArray array) {
return array.getStore() instanceof double[];
}

public static boolean isObject(RubyArray array) {
return array.getStore() instanceof Object[];
}

public static boolean isOtherNull(RubyArray array, RubyArray other) {
return other.getStore() == null;
}

public static boolean isOtherIntegerFixnum(RubyArray array, RubyArray other) {
return other.getStore() instanceof int[];
}

public static boolean isOtherLongFixnum(RubyArray array, RubyArray other) {
return other.getStore() instanceof long[];
}

public static boolean isOtherFloat(RubyArray array, RubyArray other) {
return other.getStore() instanceof double[];
}

public static boolean isOtherObject(RubyArray array, RubyArray other) {
return other.getStore() instanceof Object[];
}

public static boolean areBothNull(RubyArray a, RubyArray b) {
return a.getStore() == null && b.getStore() == null;
}

public static boolean areBothIntegerFixnum(RubyArray a, RubyArray b) {
return a.getStore() instanceof int[] && b.getStore() instanceof int[];
}

public static boolean areBothLongFixnum(RubyArray a, RubyArray b) {
return a.getStore() instanceof long[] && b.getStore() instanceof long[];
}

public static boolean areBothFloat(RubyArray a, RubyArray b) {
return a.getStore() instanceof double[] && b.getStore() instanceof double[];
}

public static boolean areBothObject(RubyArray a, RubyArray b) {
return a.getStore() instanceof Object[] && b.getStore() instanceof Object[];
}

}
31 changes: 21 additions & 10 deletions core/src/main/java/org/jruby/truffle/nodes/core/ArrayNodes.java
Expand Up @@ -11,6 +11,7 @@

import com.oracle.truffle.api.CallTarget;
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.dsl.ImportGuards;
import com.oracle.truffle.api.nodes.ExplodeLoop;
import com.oracle.truffle.api.Truffle;
import com.oracle.truffle.api.dsl.Specialization;
Expand Down Expand Up @@ -893,7 +894,8 @@ public RubyArray setIntegerFixnumRange(RubyArray array, RubyRange.IntegerFixnumR
}

@CoreMethod(names = "all?", needsBlock = true)
public abstract static class AllNode extends YieldingArrayCoreMethodNode {
@ImportGuards(ArrayGuards.class)
public abstract static class AllNode extends YieldingCoreMethodNode {

public AllNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
Expand Down Expand Up @@ -963,7 +965,8 @@ public boolean allObject(VirtualFrame frame, RubyArray array, RubyProc block) {
}

@CoreMethod(names = "any?", needsBlock = true)
public abstract static class AnyNode extends YieldingArrayCoreMethodNode {
@ImportGuards(ArrayGuards.class)
public abstract static class AnyNode extends YieldingCoreMethodNode {

public AnyNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
Expand Down Expand Up @@ -1334,7 +1337,8 @@ public Object dupObject(RubyArray array) {
}

@CoreMethod(names = "each", needsBlock = true)
public abstract static class EachNode extends YieldingArrayCoreMethodNode {
@ImportGuards(ArrayGuards.class)
public abstract static class EachNode extends YieldingCoreMethodNode {

private final BranchProfile breakProfile = new BranchProfile();
private final BranchProfile nextProfile = new BranchProfile();
Expand Down Expand Up @@ -1504,7 +1508,8 @@ public Object eachObject(VirtualFrame frame, RubyArray array, RubyProc block) {
}

@CoreMethod(names = "each_with_index", needsBlock = true)
public abstract static class EachWithIndexNode extends YieldingArrayCoreMethodNode {
@ImportGuards(ArrayGuards.class)
public abstract static class EachWithIndexNode extends YieldingCoreMethodNode {

private final BranchProfile breakProfile = new BranchProfile();
private final BranchProfile nextProfile = new BranchProfile();
Expand Down Expand Up @@ -1581,7 +1586,8 @@ public boolean isEmpty(RubyArray array) {
}

@CoreMethod(names = "find", needsBlock = true)
public abstract static class FindNode extends YieldingArrayCoreMethodNode {
@ImportGuards(ArrayGuards.class)
public abstract static class FindNode extends YieldingCoreMethodNode {

public FindNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
Expand Down Expand Up @@ -1846,7 +1852,8 @@ public RubyArray initialize(RubyArray array, int size, Object defaultValue) {
}

@CoreMethod(names = {"inject", "reduce"}, needsBlock = true, optional = 1)
public abstract static class InjectNode extends YieldingArrayCoreMethodNode {
@ImportGuards(ArrayGuards.class)
public abstract static class InjectNode extends YieldingCoreMethodNode {

@Child protected DispatchHeadNode dispatch;

Expand Down Expand Up @@ -2069,7 +2076,8 @@ public Object last(RubyArray array) {
}

@CoreMethod(names = {"map", "collect"}, needsBlock = true)
public abstract static class MapNode extends YieldingArrayCoreMethodNode {
@ImportGuards(ArrayGuards.class)
public abstract static class MapNode extends YieldingCoreMethodNode {

@Child protected ArrayBuilderNode arrayBuilder;

Expand Down Expand Up @@ -2165,7 +2173,8 @@ public RubyArray mapObject(VirtualFrame frame, RubyArray array, RubyProc block)
}

@CoreMethod(names = {"map!", "collect!"}, needsBlock = true)
public abstract static class MapInPlaceNode extends YieldingArrayCoreMethodNode {
@ImportGuards(ArrayGuards.class)
public abstract static class MapInPlaceNode extends YieldingCoreMethodNode {

@Child protected ArrayBuilderNode arrayBuilder;

Expand Down Expand Up @@ -2863,7 +2872,8 @@ public RubyArray pushObjectObject(RubyArray array, Object value) {
}

@CoreMethod(names = "reject!", needsBlock = true)
public abstract static class RejectInPlaceNode extends YieldingArrayCoreMethodNode {
@ImportGuards(ArrayGuards.class)
public abstract static class RejectInPlaceNode extends YieldingCoreMethodNode {

public RejectInPlaceNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
Expand Down Expand Up @@ -2956,7 +2966,8 @@ public RubyArray replaceObject(RubyArray array, RubyArray other) {
}

@CoreMethod(names = "select", needsBlock = true)
public abstract static class SelectNode extends YieldingArrayCoreMethodNode {
@ImportGuards(ArrayGuards.class)
public abstract static class SelectNode extends YieldingCoreMethodNode {

@Child protected ArrayBuilderNode arrayBuilder;

Expand Down

This file was deleted.

Expand Up @@ -34,4 +34,8 @@ public Object yield(VirtualFrame frame, RubyProc block, Object... arguments) {
return dispatchNode.dispatch(frame, block, arguments);
}

public boolean yieldIsTruthy(VirtualFrame frame, RubyProc block, Object... arguments) {
return getContext().getCoreLibrary().isTruthy(yield(frame, block, arguments));
}

}

This file was deleted.

0 comments on commit c886cc7

Please sign in to comment.