Skip to content

Commit 1b83c7c

Browse files
committed
OrAssignConstantNode is converted to DSL node
1 parent 538a6a2 commit 1b83c7c

File tree

2 files changed

+16
-24
lines changed

2 files changed

+16
-24
lines changed

src/main/java/org/truffleruby/language/constants/OrAssignConstantNode.java

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
*/
1010
package org.truffleruby.language.constants;
1111

12-
import com.oracle.truffle.api.CompilerDirectives;
12+
import com.oracle.truffle.api.dsl.Cached;
13+
import com.oracle.truffle.api.dsl.Specialization;
1314
import com.oracle.truffle.api.frame.VirtualFrame;
14-
import com.oracle.truffle.api.profiles.ConditionProfile;
15+
import com.oracle.truffle.api.profiles.InlinedConditionProfile;
1516
import org.truffleruby.core.cast.BooleanCastNode;
16-
import org.truffleruby.core.cast.BooleanCastNodeGen;
1717
import org.truffleruby.core.module.RubyModule;
1818
import org.truffleruby.language.RubyConstant;
1919
import org.truffleruby.language.RubyContextSourceNode;
@@ -26,25 +26,25 @@
2626
* We need a separate class for this because we need to check if the constant is defined. Doing so will evaluate the
2727
* module part, which will be evaluated again when assigning the constant. If evaluating the module part has any
2828
* side-effect, this is incorrect and differs from MRI semantics. */
29-
public class OrAssignConstantNode extends RubyContextSourceNode {
29+
public abstract class OrAssignConstantNode extends RubyContextSourceNode {
3030

3131
@Child protected ReadConstantNode readConstant;
3232
@Child protected WriteConstantNode writeConstant;
33-
@Child private BooleanCastNode cast;
3433

35-
private final ConditionProfile triviallyUndefined = ConditionProfile.create();
36-
private final ConditionProfile defined = ConditionProfile.create();
3734
private final RunTwiceBranchProfile writeTwiceProfile = new RunTwiceBranchProfile();
3835

3936
public OrAssignConstantNode(ReadConstantNode readConstant, WriteConstantNode writeConstant) {
4037
this.readConstant = readConstant;
4138
this.writeConstant = writeConstant;
4239
}
4340

44-
@Override
45-
public Object execute(VirtualFrame frame) {
46-
47-
if (triviallyUndefined.profile(readConstant.isModuleTriviallyUndefined(frame, getLanguage(), getContext()))) {
41+
@Specialization
42+
protected Object doOrAssignConstant(VirtualFrame frame,
43+
@Cached BooleanCastNode booleanCastNode,
44+
@Cached InlinedConditionProfile defined,
45+
@Cached InlinedConditionProfile triviallyUndefined) {
46+
if (triviallyUndefined.profile(this,
47+
readConstant.isModuleTriviallyUndefined(frame, getLanguage(), getContext()))) {
4848
// It might not be defined because of autoloaded constants (maybe other reasons?),
4949
// simply attempt writing (which will trigger autoloads if required).
5050
// Since we didn't evaluate the module part yet, no side-effects can occur.
@@ -62,31 +62,23 @@ public Object execute(VirtualFrame frame) {
6262
// Next we check if the constant itself is defined, and if it is, we get its value.
6363
final RubyConstant constant = readConstant.getConstantIfDefined(module);
6464

65-
final boolean isDefined = defined.profile(constant != null);
65+
final boolean isDefined = defined.profile(this, constant != null);
6666

6767
final Object value = isDefined
6868
? readConstant.getConstant(module, constant)
6969
: null;
7070

7171
// Write if the constant is undefined or if its value is falsy.
72-
if (!isDefined || !castToBoolean(value)) {
72+
if (!isDefined || !booleanCastNode.execute(this, value)) {
7373
writeTwiceProfile.enter();
7474
return writeConstant.execute(frame, module);
7575
} else {
7676
return value;
7777
}
7878
}
7979

80-
private boolean castToBoolean(final Object value) {
81-
if (cast == null) {
82-
CompilerDirectives.transferToInterpreterAndInvalidate();
83-
cast = insert(BooleanCastNodeGen.create(null));
84-
}
85-
return cast.execute(value);
86-
}
87-
8880
public RubyNode cloneUninitialized() {
89-
var copy = new OrAssignConstantNode(
81+
var copy = OrAssignConstantNodeGen.create(
9082
(ReadConstantNode) readConstant.cloneUninitialized(),
9183
(WriteConstantNode) writeConstant.cloneUninitialized());
9284
return copy.copyFlags(this);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
import org.truffleruby.language.SourceIndexLength;
6565
import org.truffleruby.language.arguments.ArgumentsDescriptor;
6666
import org.truffleruby.language.arguments.EmptyArgumentsDescriptor;
67-
import org.truffleruby.language.constants.OrAssignConstantNode;
67+
import org.truffleruby.language.constants.OrAssignConstantNodeGen;
6868
import org.truffleruby.language.constants.ReadConstantNode;
6969
import org.truffleruby.language.constants.ReadConstantWithDynamicScopeNode;
7070
import org.truffleruby.language.constants.ReadConstantWithLexicalScopeNode;
@@ -2232,7 +2232,7 @@ public RubyNode visitOpAsgnConstDeclNode(OpAsgnConstDeclParseNode node) {
22322232
}
22332233

22342234
case "||": {
2235-
return new OrAssignConstantNode(lhs, (WriteConstantNode) rhs);
2235+
return OrAssignConstantNodeGen.create(lhs, (WriteConstantNode) rhs);
22362236
}
22372237

22382238
default: {

0 commit comments

Comments
 (0)