Skip to content

Commit bcfa723

Browse files
committed
[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.
1 parent b21373e commit bcfa723

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

core/src/main/java/org/jruby/truffle/nodes/core/BasicObjectNodes.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
public abstract class BasicObjectNodes {
3232

3333
@CoreMethod(names = "!")
34-
public abstract static class NotNode extends CoreMethodNode {
34+
public abstract static class NotNode extends UnaryCoreMethodNode {
3535

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

44-
@CreateCast("arguments") public RubyNode[] createCast(RubyNode[] arguments) {
45-
return new RubyNode[] {
46-
BooleanCastNodeFactory.create(getContext(), getSourceSection(), arguments[0])
47-
};
44+
@CreateCast("operand") public RubyNode createCast(RubyNode operand) {
45+
return BooleanCastNodeFactory.create(getContext(), getSourceSection(), operand);
4846
}
4947

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

151149
@CoreMethod(names = {"equal?", "=="}, required = 1)
152-
public abstract static class ReferenceEqualNode extends CoreMethodNode {
150+
public abstract static class ReferenceEqualNode extends BinaryCoreMethodNode {
153151

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

173-
@Specialization(guards = {"isNotRubyBasicObject(arguments[0])", "isNotRubyBasicObject(arguments[1])", "notSameClass"})
171+
@Specialization(guards = {"isNotRubyBasicObject(left)", "isNotRubyBasicObject(right)", "notSameClass"})
174172
public boolean equal(Object a, Object b) {
175173
return false;
176174
}
177175

178-
@Specialization(guards = "isNotRubyBasicObject(arguments[0])")
176+
@Specialization(guards = "isNotRubyBasicObject(left)")
179177
public boolean equal(Object a, RubyBasicObject b) {
180178
return false;
181179
}
182180

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

core/src/main/java/org/jruby/truffle/nodes/core/CoreMethodNodeManager.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@
3737

3838
public abstract class CoreMethodNodeManager {
3939

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

167-
final RubyNode methodNode = methodDetails.getNodeFactory().createNode(context, sourceSection, argumentsNodes.toArray(new RubyNode[argumentsNodes.size()]));
167+
final RubyNode methodNode;
168+
List<List<Class<?>>> signatures = methodDetails.getNodeFactory().getNodeSignatures();
169+
if (signatures.size() < 1 || signatures.get(0).get(2) == RubyNode[].class) {
170+
methodNode = methodDetails.getNodeFactory().createNode(context, sourceSection, argumentsNodes.toArray(new RubyNode[argumentsNodes.size()]));
171+
} else {
172+
Object[] args = new Object[2 + argumentsNodes.size()];
173+
args[0] = context;
174+
args[1] = sourceSection;
175+
System.arraycopy(argumentsNodes.toArray(new RubyNode[argumentsNodes.size()]), 0, args, 2, argumentsNodes.size());
176+
methodNode = methodDetails.getNodeFactory().createNode(args);
177+
}
178+
168179
final CheckArityNode checkArity = new CheckArityNode(context, sourceSection, arity);
169180
final RubyNode block = SequenceNode.sequence(context, sourceSection, checkArity, methodNode);
170181
final ExceptionTranslatingNode exceptionTranslatingNode = new ExceptionTranslatingNode(context, sourceSection, block);

0 commit comments

Comments
 (0)