Skip to content

Commit f5d2a2c

Browse files
committed
IfElseNode is converted to DSL node
1 parent c27c9b3 commit f5d2a2c

File tree

6 files changed

+28
-35
lines changed

6 files changed

+28
-35
lines changed

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

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,37 +9,33 @@
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 IfElseNode extends RubyContextSourceNode {
21+
public abstract class IfElseNode extends RubyContextSourceNode {
2122

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

26-
private final CountingConditionProfile conditionProfile = CountingConditionProfile.create();
27-
2827
public IfElseNode(RubyNode condition, RubyNode thenBody, RubyNode elseBody) {
29-
this.condition = BooleanCastNodeGen.create(condition);
30-
this.thenBody = thenBody;
31-
this.elseBody = elseBody;
32-
}
33-
34-
IfElseNode(BooleanCastNode condition, RubyNode thenBody, RubyNode elseBody) {
3528
this.condition = condition;
3629
this.thenBody = thenBody;
3730
this.elseBody = elseBody;
3831
}
3932

40-
@Override
41-
public Object execute(VirtualFrame frame) {
42-
if (conditionProfile.profile(condition.execute(frame))) {
33+
@Specialization
34+
protected Object doIfElse(VirtualFrame frame,
35+
@Cached InlinedCountingConditionProfile conditionProfile,
36+
@Cached BooleanCastNode booleanCastNode) {
37+
final var conditionAsBoolean = booleanCastNode.execute(condition.execute(frame));
38+
if (conditionProfile.profile(this, conditionAsBoolean)) {
4339
return thenBody.execute(frame);
4440
} else {
4541
return elseBody.execute(frame);
@@ -55,17 +51,13 @@ public boolean isContinuable() {
5551
public RubyNode simplifyAsTailExpression() {
5652
final RubyNode newThen = thenBody.simplifyAsTailExpression();
5753
final RubyNode newElse = elseBody.simplifyAsTailExpression();
58-
return new IfElseNode(condition, newThen, newElse).copySourceSection(this);
59-
}
60-
61-
private RubyNode getConditionBeforeCasting() {
62-
return condition.getValueNode();
54+
return IfElseNodeGen.create(condition, newThen, newElse).copySourceSection(this);
6355
}
6456

6557
@Override
6658
public RubyNode cloneUninitialized() {
67-
var copy = new IfElseNode(
68-
getConditionBeforeCasting().cloneUninitialized(),
59+
var copy = IfElseNodeGen.create(
60+
condition.cloneUninitialized(),
6961
thenBody.cloneUninitialized(),
7062
elseBody.cloneUninitialized());
7163
return copy.copyFlags(this);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public boolean canSubsumeFollowing() {
4848

4949
@Override
5050
public RubyNode subsumeFollowing(RubyNode following) {
51-
return new IfElseNode(condition, thenBody, following).copySourceSection(this);
51+
return IfElseNodeGen.create(condition, thenBody, following).copySourceSection(this);
5252
}
5353

5454
@Override

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public boolean canSubsumeFollowing() {
4646

4747
@Override
4848
public RubyNode subsumeFollowing(RubyNode following) {
49-
return new IfElseNode(condition, following, thenBody).copySourceSection(this);
49+
return IfElseNodeGen.create(condition, following, thenBody).copySourceSection(this);
5050
}
5151

5252
@Override

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
import org.truffleruby.language.control.DynamicReturnNode;
7777
import org.truffleruby.language.control.FrameOnStackNode;
7878
import org.truffleruby.language.control.IfElseNode;
79+
import org.truffleruby.language.control.IfElseNodeGen;
7980
import org.truffleruby.language.control.IfNodeGen;
8081
import org.truffleruby.language.control.InvalidReturnNode;
8182
import org.truffleruby.language.control.LocalReturnNode;
@@ -781,7 +782,7 @@ public RubyNode visitCaseNode(CaseParseNode node) {
781782

782783
// Create the if node
783784
final RubyNode thenNode = translateNodeOrNil(sourceSection, when.getBodyNode());
784-
final IfElseNode ifNode = new IfElseNode(conditionNode, thenNode, elseNode);
785+
final IfElseNode ifNode = IfElseNodeGen.create(conditionNode, thenNode, elseNode);
785786

786787
// This if becomes the else for the next if
787788
elseNode = ifNode;
@@ -802,7 +803,7 @@ public RubyNode visitCaseNode(CaseParseNode node) {
802803

803804
// Create the if node
804805
final RubyNode thenNode = when.getBodyNode().accept(this);
805-
final IfElseNode ifNode = new IfElseNode(conditionNode, thenNode, elseNode);
806+
final IfElseNode ifNode = IfElseNodeGen.create(conditionNode, thenNode, elseNode);
806807

807808
// This if becomes the else for the next if
808809
elseNode = ifNode;
@@ -857,7 +858,7 @@ public RubyNode visitCaseInNode(CaseInParseNode node) {
857858
final RubyNode conditionNode = translator.translatePatternNode(patternNode, readTemp);
858859
// Create the if node
859860
final RubyNode thenNode = translateNodeOrNil(sourceSection, in.getBodyNode());
860-
final IfElseNode ifNode = new IfElseNode(conditionNode, thenNode, elseNode);
861+
final IfElseNode ifNode = IfElseNodeGen.create(conditionNode, thenNode, elseNode);
861862

862863
// This if becomes the else for the next if
863864
elseNode = ifNode;
@@ -1809,7 +1810,7 @@ public RubyNode visitIfNode(IfParseNode node) {
18091810
if (thenBody != null && elseBody != null) {
18101811
final RubyNode thenBodyTranslated = thenBody.accept(this);
18111812
final RubyNode elseBodyTranslated = elseBody.accept(this);
1812-
ret = new IfElseNode(condition, thenBodyTranslated, elseBodyTranslated);
1813+
ret = IfElseNodeGen.create(condition, thenBodyTranslated, elseBodyTranslated);
18131814
ret.unsafeSetSourceSection(sourceSection);
18141815
} else if (thenBody != null) {
18151816
final RubyNode thenBodyTranslated = thenBody.accept(this);

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
import org.truffleruby.language.arguments.ReadPreArgumentNode;
3535
import org.truffleruby.language.arguments.ReadRestArgumentNode;
3636
import org.truffleruby.language.arguments.SaveMethodBlockNode;
37-
import org.truffleruby.language.control.IfElseNode;
37+
import org.truffleruby.language.control.IfElseNodeGen;
3838
import org.truffleruby.language.literal.NilLiteralNode;
3939
import org.truffleruby.language.locals.LocalVariableType;
4040
import org.truffleruby.language.locals.ReadLocalVariableNode;
@@ -217,7 +217,7 @@ public RubyNode translate() {
217217

218218
if (useArray()) {
219219
if (argsNode.getPreCount() == 0 || argsNode.hasRestArg()) {
220-
sequence.add(new IfElseNode(
220+
sequence.add(IfElseNodeGen.create(
221221
new ArrayIsAtLeastAsLargeAsNode(required, loadArray(sourceSection)),
222222
notNilAtLeastAsLarge,
223223
notNilSmaller));
@@ -404,7 +404,7 @@ private RubyNode translateLocalAssignment(SourceIndexLength sourcePosition, Stri
404404

405405
if (useArray()) {
406406
// TODO CS 10-Jan-16 we should really hoist this check, or see if Graal does it for us
407-
readNode = new IfElseNode(
407+
readNode = IfElseNodeGen.create(
408408
new ArrayIsAtLeastAsLargeAsNode(minimum, loadArray(sourceSection)),
409409
ArrayIndexNodes.ReadConstantIndexNode.create(loadArray(sourceSection), index),
410410
defaultValue);
@@ -571,10 +571,10 @@ public RubyNode visitMultipleAsgnNode(MultipleAsgnParseNode node) {
571571
SplatCastNode.NilBehavior.ARRAY_WITH_NIL,
572572
true,
573573
readArgument(sourceSection))),
574-
new IfElseNode(
574+
IfElseNodeGen.create(
575575
new IsNilNode(new ReadLocalVariableNode(LocalVariableType.FRAME_LOCAL, arraySlot)),
576576
nil,
577-
new IfElseNode(
577+
IfElseNodeGen.create(
578578
new ArrayIsAtLeastAsLargeAsNode(
579579
node.getPreCount() + node.getPostCount(),
580580
new ReadLocalVariableNode(LocalVariableType.FRAME_LOCAL, arraySlot)),

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.util.function.Supplier;
1414

1515
import org.truffleruby.RubyLanguage;
16+
import org.truffleruby.language.control.IfElseNodeGen;
1617
import org.truffleruby.language.control.NotNodeGen;
1718
import org.truffleruby.language.locals.FindDeclarationVariableNodes.FrameSlotAndDepth;
1819
import org.truffleruby.language.methods.CachedLazyCallTargetSupplier;
@@ -35,7 +36,6 @@
3536
import org.truffleruby.language.arguments.ShouldDestructureNode;
3637
import org.truffleruby.language.control.AndNode;
3738
import org.truffleruby.language.control.DynamicReturnNode;
38-
import org.truffleruby.language.control.IfElseNode;
3939
import org.truffleruby.language.control.InvalidReturnNode;
4040
import org.truffleruby.language.control.ReturnID;
4141
import org.truffleruby.language.locals.LocalVariableType;
@@ -230,7 +230,7 @@ private RubyNode preludeProc(
230230
new ShouldDestructureNode(arity.acceptsKeywords()),
231231
arrayWasNotNil);
232232

233-
preludeProc = new IfElseNode(
233+
preludeProc = IfElseNodeGen.create(
234234
shouldDestructureAndArrayWasNotNil,
235235
newDestructureArguments,
236236
loadArguments);

0 commit comments

Comments
 (0)