Skip to content

Commit 7427120

Browse files
committed
Refactor UnlessNode
1 parent 8cd0272 commit 7427120

File tree

2 files changed

+15
-22
lines changed

2 files changed

+15
-22
lines changed

src/main/java/org/truffleruby/language/control/UnlessNode.java

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,30 @@
99
*/
1010
package org.truffleruby.language.control;
1111

12-
import com.oracle.truffle.api.profiles.CountingConditionProfile;
12+
import com.oracle.truffle.api.dsl.Cached;
13+
import com.oracle.truffle.api.dsl.Specialization;
14+
import com.oracle.truffle.api.profiles.InlinedCountingConditionProfile;
1315
import org.truffleruby.core.cast.BooleanCastNode;
14-
import org.truffleruby.core.cast.BooleanCastNodeGen;
1516
import org.truffleruby.language.RubyContextSourceNode;
1617
import org.truffleruby.language.RubyNode;
1718

1819
import com.oracle.truffle.api.frame.VirtualFrame;
1920

20-
public class UnlessNode extends RubyContextSourceNode {
21+
public abstract class UnlessNode extends RubyContextSourceNode {
2122

22-
@Child private BooleanCastNode condition;
23+
@Child private RubyNode condition;
2324
@Child private RubyNode thenBody;
2425

25-
private final CountingConditionProfile conditionProfile = CountingConditionProfile.create();
26-
2726
public UnlessNode(RubyNode condition, RubyNode thenBody) {
28-
this.condition = BooleanCastNodeGen.create(condition);
29-
this.thenBody = thenBody;
30-
}
31-
32-
public UnlessNode(BooleanCastNode condition, RubyNode thenBody) {
3327
this.condition = condition;
3428
this.thenBody = thenBody;
3529
}
3630

37-
@Override
38-
public Object execute(VirtualFrame frame) {
39-
if (!conditionProfile.profile(condition.execute(frame))) {
31+
@Specialization
32+
protected Object doUnless(VirtualFrame frame,
33+
@Cached InlinedCountingConditionProfile conditionProfile,
34+
@Cached BooleanCastNode booleanCastNode) {
35+
if (!conditionProfile.profile(this, booleanCastNode.execute(condition.execute(frame)))) {
4036
return thenBody.execute(frame);
4137
} else {
4238
return nil;
@@ -55,16 +51,13 @@ public RubyNode subsumeFollowing(RubyNode following) {
5551

5652
@Override
5753
public RubyNode simplifyAsTailExpression() {
58-
return new UnlessNode(condition, thenBody.simplifyAsTailExpression()).copySourceSection(this);
54+
return UnlessNodeGen.create(condition, thenBody.simplifyAsTailExpression()).copySourceSection(this);
5955
}
6056

61-
private RubyNode getConditionBeforeCasting() {
62-
return condition.getValueNode();
63-
}
6457

6558
public RubyNode cloneUninitialized() {
66-
var copy = new UnlessNode(
67-
getConditionBeforeCasting().cloneUninitialized(),
59+
var copy = UnlessNodeGen.create(
60+
condition.cloneUninitialized(),
6861
thenBody.cloneUninitialized());
6962
return copy.copyFlags(this);
7063
}

src/main/java/org/truffleruby/parser/BodyTranslator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
import org.truffleruby.language.control.RedoNode;
9090
import org.truffleruby.language.control.RetryNode;
9191
import org.truffleruby.language.control.ReturnID;
92-
import org.truffleruby.language.control.UnlessNode;
92+
import org.truffleruby.language.control.UnlessNodeGen;
9393
import org.truffleruby.language.control.WhileNode;
9494
import org.truffleruby.language.defined.DefinedNode;
9595
import org.truffleruby.language.defined.DefinedWrapperNode;
@@ -1816,7 +1816,7 @@ public RubyNode visitIfNode(IfParseNode node) {
18161816
ret.unsafeSetSourceSection(sourceSection);
18171817
} else if (elseBody != null) {
18181818
final RubyNode elseBodyTranslated = elseBody.accept(this);
1819-
ret = new UnlessNode(condition, elseBodyTranslated);
1819+
ret = UnlessNodeGen.create(condition, elseBodyTranslated);
18201820
ret.unsafeSetSourceSection(sourceSection);
18211821
} else {
18221822
ret = sequence(sourceSection, Arrays.asList(condition, new NilLiteralNode(true)));

0 commit comments

Comments
 (0)