Skip to content
Permalink
Browse files
[Truffle] Add unary and binary core method nodes for expressivity.
* This is an experiment, it might prove difficult to handle some cases of ArgumentError, etc.
  • Loading branch information
eregon committed Dec 2, 2014
1 parent b21373e commit bcfa7238ab7ee8814f75195698617f92cfe46781
@@ -31,7 +31,7 @@
public abstract class BasicObjectNodes {

@CoreMethod(names = "!")
public abstract static class NotNode extends CoreMethodNode {
public abstract static class NotNode extends UnaryCoreMethodNode {

public NotNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
@@ -41,10 +41,8 @@ public NotNode(NotNode prev) {
super(prev);
}

@CreateCast("arguments") public RubyNode[] createCast(RubyNode[] arguments) {
return new RubyNode[] {
BooleanCastNodeFactory.create(getContext(), getSourceSection(), arguments[0])
};
@CreateCast("operand") public RubyNode createCast(RubyNode operand) {
return BooleanCastNodeFactory.create(getContext(), getSourceSection(), operand);
}

@Specialization
@@ -149,7 +147,7 @@ protected boolean isSmallFixnum(long fixnum) {
}

@CoreMethod(names = {"equal?", "=="}, required = 1)
public abstract static class ReferenceEqualNode extends CoreMethodNode {
public abstract static class ReferenceEqualNode extends BinaryCoreMethodNode {

public ReferenceEqualNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
@@ -170,17 +168,17 @@ public ReferenceEqualNode(ReferenceEqualNode prev) {
return a == b;
}

@Specialization(guards = {"isNotRubyBasicObject(arguments[0])", "isNotRubyBasicObject(arguments[1])", "notSameClass"})
@Specialization(guards = {"isNotRubyBasicObject(left)", "isNotRubyBasicObject(right)", "notSameClass"})
public boolean equal(Object a, Object b) {
return false;
}

@Specialization(guards = "isNotRubyBasicObject(arguments[0])")
@Specialization(guards = "isNotRubyBasicObject(left)")
public boolean equal(Object a, RubyBasicObject b) {
return false;
}

@Specialization(guards = "isNotRubyBasicObject(arguments[1])")
@Specialization(guards = "isNotRubyBasicObject(right)")
public boolean equal(RubyBasicObject a, Object b) {
return false;
}
@@ -37,8 +37,8 @@

public abstract class CoreMethodNodeManager {

public static void addCoreMethodNodes(RubyClass rubyObjectClass, List<? extends NodeFactory<? extends CoreMethodNode>> nodeFactories) {
for (NodeFactory<? extends CoreMethodNode> nodeFactory : nodeFactories) {
public static void addCoreMethodNodes(RubyClass rubyObjectClass, List<? extends NodeFactory<? extends RubyNode>> nodeFactories) {
for (NodeFactory<? extends RubyNode> nodeFactory : nodeFactories) {
final GeneratedBy generatedBy = nodeFactory.getClass().getAnnotation(GeneratedBy.class);
final Class<?> nodeClass = generatedBy.value();
final CoreClass classAnnotation = nodeClass.getEnclosingClass().getAnnotation(CoreClass.class);
@@ -164,7 +164,18 @@ private static RubyRootNode makeGenericMethod(RubyContext context, MethodDetails
argumentsNodes.add(new ReadBlockNode(context, sourceSection, UndefinedPlaceholder.INSTANCE));
}

final RubyNode methodNode = methodDetails.getNodeFactory().createNode(context, sourceSection, argumentsNodes.toArray(new RubyNode[argumentsNodes.size()]));
final RubyNode methodNode;
List<List<Class<?>>> signatures = methodDetails.getNodeFactory().getNodeSignatures();
if (signatures.size() < 1 || signatures.get(0).get(2) == RubyNode[].class) {
methodNode = methodDetails.getNodeFactory().createNode(context, sourceSection, argumentsNodes.toArray(new RubyNode[argumentsNodes.size()]));
} else {
Object[] args = new Object[2 + argumentsNodes.size()];
args[0] = context;
args[1] = sourceSection;
System.arraycopy(argumentsNodes.toArray(new RubyNode[argumentsNodes.size()]), 0, args, 2, argumentsNodes.size());
methodNode = methodDetails.getNodeFactory().createNode(args);
}

final CheckArityNode checkArity = new CheckArityNode(context, sourceSection, arity);
final RubyNode block = SequenceNode.sequence(context, sourceSection, checkArity, methodNode);
final ExceptionTranslatingNode exceptionTranslatingNode = new ExceptionTranslatingNode(context, sourceSection, block);

0 comments on commit bcfa723

Please sign in to comment.