Skip to content

Commit 538a6a2

Browse files
committed
AndNode is converted to DSL node
1 parent f5d2a2c commit 538a6a2

File tree

4 files changed

+24
-35
lines changed

4 files changed

+24
-35
lines changed

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

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,51 +9,40 @@
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

18-
import com.oracle.truffle.api.CompilerDirectives;
1919
import com.oracle.truffle.api.frame.VirtualFrame;
2020

21-
public class AndNode extends RubyContextSourceNode {
21+
public abstract class AndNode extends RubyContextSourceNode {
2222

2323
@Child private RubyNode left;
2424
@Child private RubyNode right;
2525

26-
@Child private BooleanCastNode leftCast;
27-
28-
private final CountingConditionProfile conditionProfile = CountingConditionProfile.create();
29-
3026
public AndNode(RubyNode left, RubyNode right) {
3127
this.left = left;
3228
this.right = right;
3329
}
3430

35-
@Override
36-
public Object execute(VirtualFrame frame) {
37-
final Object leftValue = left.execute(frame);
38-
39-
if (conditionProfile.profile(castToBoolean(leftValue))) {
31+
@Specialization
32+
protected Object doAnd(VirtualFrame frame,
33+
@Cached BooleanCastNode leftCast,
34+
@Cached InlinedCountingConditionProfile conditionProfile) {
35+
final var leftValue = left.execute(frame);
36+
if (conditionProfile.profile(this, leftCast.execute(this, leftValue))) {
4037
return right.execute(frame);
4138
} else {
4239
return leftValue;
4340
}
4441
}
4542

46-
private boolean castToBoolean(final Object value) {
47-
if (leftCast == null) {
48-
CompilerDirectives.transferToInterpreterAndInvalidate();
49-
leftCast = insert(BooleanCastNodeGen.create(null));
50-
}
51-
return leftCast.execute(value);
52-
}
53-
5443
@Override
5544
public RubyNode cloneUninitialized() {
56-
var copy = new AndNode(
45+
var copy = AndNodeGen.create(
5746
left.cloneUninitialized(),
5847
right.cloneUninitialized());
5948
return copy.copyFlags(this);

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
import org.truffleruby.language.constants.ReadConstantWithDynamicScopeNode;
7070
import org.truffleruby.language.constants.ReadConstantWithLexicalScopeNode;
7171
import org.truffleruby.language.constants.WriteConstantNode;
72-
import org.truffleruby.language.control.AndNode;
72+
import org.truffleruby.language.control.AndNodeGen;
7373
import org.truffleruby.language.control.BreakID;
7474
import org.truffleruby.language.control.BreakNode;
7575
import org.truffleruby.language.control.DeferredRaiseException;
@@ -340,7 +340,7 @@ public RubyNode visitAndNode(AndParseNode node) {
340340
final RubyNode x = translateNodeOrNil(sourceSection, node.getFirstNode());
341341
final RubyNode y = translateNodeOrNil(sourceSection, node.getSecondNode());
342342

343-
final RubyNode ret = new AndNode(x, y);
343+
final RubyNode ret = AndNodeGen.create(x, y);
344344
ret.unsafeSetSourceSection(sourceSection);
345345
return addNewlineIfNeeded(node, ret);
346346
}
@@ -2209,7 +2209,7 @@ private RubyNode translateOpAsgnAndNode(ParseNode node, RubyNode lhs, RubyNode r
22092209

22102210
final SourceIndexLength sourceSection = node.getPosition();
22112211

2212-
final RubyNode andNode = new AndNode(lhs, rhs);
2212+
final RubyNode andNode = AndNodeGen.create(lhs, rhs);
22132213
andNode.unsafeSetSourceSection(sourceSection);
22142214

22152215
final RubyNode ret = new DefinedWrapperNode(language.coreStrings.ASSIGNMENT, andNode);
@@ -2275,7 +2275,7 @@ public RubyNode visitOpAsgnNode(OpAsgnParseNode node) {
22752275
RubyNode lhs = readMethod.accept(this);
22762276
RubyNode rhs = writeMethod.accept(this);
22772277

2278-
final RubyNode controlNode = isOrOperator ? OrNodeGen.create(lhs, rhs) : new AndNode(lhs, rhs);
2278+
final RubyNode controlNode = isOrOperator ? OrNodeGen.create(lhs, rhs) : AndNodeGen.create(lhs, rhs);
22792279

22802280
final RubyNode ret = new DefinedWrapperNode(
22812281
language.coreStrings.ASSIGNMENT,
@@ -2327,7 +2327,7 @@ public RubyNode visitOpAsgnOrNode(OpAsgnOrParseNode node) {
23272327
// This is needed for class variables. Constants are handled separately in visitOpAsgnConstDeclNode.
23282328
if (node.getFirstNode().needsDefinitionCheck()) {
23292329
RubyNode defined = new DefinedNode(lhs);
2330-
lhs = new AndNode(defined, lhs);
2330+
lhs = AndNodeGen.create(defined, lhs);
23312331
}
23322332

23332333
return translateOpAsgOrNode(node, lhs, rhs);

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.AndNodeGen;
1617
import org.truffleruby.language.control.IfElseNodeGen;
1718
import org.truffleruby.language.control.NotNodeGen;
1819
import org.truffleruby.language.locals.FindDeclarationVariableNodes.FrameSlotAndDepth;
@@ -34,7 +35,6 @@
3435
import org.truffleruby.language.arguments.MissingArgumentBehavior;
3536
import org.truffleruby.language.arguments.ReadPreArgumentNode;
3637
import org.truffleruby.language.arguments.ShouldDestructureNode;
37-
import org.truffleruby.language.control.AndNode;
3838
import org.truffleruby.language.control.DynamicReturnNode;
3939
import org.truffleruby.language.control.InvalidReturnNode;
4040
import org.truffleruby.language.control.ReturnID;
@@ -226,7 +226,7 @@ private RubyNode preludeProc(
226226
new IsNilNode(
227227
new ReadLocalVariableNode(LocalVariableType.FRAME_LOCAL, arraySlot)))));
228228

229-
final RubyNode shouldDestructureAndArrayWasNotNil = new AndNode(
229+
final RubyNode shouldDestructureAndArrayWasNotNil = AndNodeGen.create(
230230
new ShouldDestructureNode(arity.acceptsKeywords()),
231231
arrayWasNotNil);
232232

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import org.truffleruby.core.array.ArraySliceNodeGen;
1818
import org.truffleruby.language.RubyNode;
1919
import org.truffleruby.language.SourceIndexLength;
20-
import org.truffleruby.language.control.AndNode;
20+
import org.truffleruby.language.control.AndNodeGen;
2121
import org.truffleruby.language.control.ExecuteAndReturnTrueNode;
2222
import org.truffleruby.language.control.NotNodeGen;
2323
import org.truffleruby.language.control.RaiseException;
@@ -98,7 +98,7 @@ public RubyNode translatePatternNode(ParseNode patternNode, RubyNode expressionV
9898
condition = NotNodeGen.create(condition);
9999
}
100100

101-
return new AndNode(pattern, condition);
101+
return AndNodeGen.create(pattern, condition);
102102
default:
103103
return createCallNode(patternNode.accept(this), "===", NodeUtil.cloneNode(expressionValue));
104104
}
@@ -141,7 +141,7 @@ public RubyNode visitArrayPatternNode(ArrayPatternParseNode arrayPatternParseNod
141141
ConstParseNode constant = (ConstParseNode) arrayPatternParseNode.getConstant();
142142
RubyNode constVal = constant.accept(this);
143143
var isInstance = createCallNode(constVal, "===", currentValueToMatch);
144-
condition = new AndNode(isInstance, condition);
144+
condition = AndNodeGen.create(isInstance, condition);
145145
}
146146

147147
for (int i = 0; i < preSize; i++) {
@@ -157,7 +157,7 @@ public RubyNode visitArrayPatternNode(ArrayPatternParseNode arrayPatternParseNod
157157
}
158158

159159
var callNode = createCallNode(translatedPatternElement, "===", NodeUtil.cloneNode(exprElement));
160-
condition = new AndNode(condition, callNode);
160+
condition = AndNodeGen.create(condition, callNode);
161161
}
162162

163163
if (restNode != null) {
@@ -178,7 +178,7 @@ public RubyNode visitArrayPatternNode(ArrayPatternParseNode arrayPatternParseNod
178178
currentValueToMatch = prev;
179179
}
180180
var seq = new ExecuteAndReturnTrueNode(restAccept);
181-
condition = new AndNode(condition, seq);
181+
condition = AndNodeGen.create(condition, seq);
182182
}
183183
}
184184

@@ -201,7 +201,7 @@ public RubyNode visitArrayPatternNode(ArrayPatternParseNode arrayPatternParseNod
201201
"===",
202202
NodeUtil.cloneNode(exprElement));
203203

204-
condition = new AndNode(condition, callNode);
204+
condition = AndNodeGen.create(condition, callNode);
205205
}
206206
}
207207

0 commit comments

Comments
 (0)