Skip to content

Commit 9c5a6df

Browse files
committed
TryNode converted to DSL node
1 parent 95baaa3 commit 9c5a6df

File tree

3 files changed

+23
-34
lines changed

3 files changed

+23
-34
lines changed

src/main/java/org/truffleruby/core/exception/ExceptionOperations.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
import com.oracle.truffle.api.CompilerDirectives;
1313
import com.oracle.truffle.api.exception.AbstractTruffleException;
14-
import com.oracle.truffle.api.profiles.ConditionProfile;
1514
import com.oracle.truffle.api.profiles.InlinedConditionProfile;
1615
import org.truffleruby.RubyContext;
1716
import org.truffleruby.RubyLanguage;
@@ -86,18 +85,6 @@ public static Object getExceptionObject(AbstractTruffleException exception) {
8685
}
8786
}
8887

89-
// TODO remove when TryNode is refactored
90-
public static Object getExceptionObject(AbstractTruffleException exception,
91-
ConditionProfile raiseExceptionProfile) {
92-
assert !(exception instanceof KillException)
93-
: "KillException should not be used as an exception object: " + exception;
94-
if (raiseExceptionProfile.profile(exception instanceof RaiseException)) {
95-
return ((RaiseException) exception).getException();
96-
} else {
97-
return exception;
98-
}
99-
}
100-
10188
public static Object getExceptionObject(Node node, AbstractTruffleException exception,
10289
InlinedConditionProfile raiseExceptionProfile) {
10390
assert !(exception instanceof KillException)

src/main/java/org/truffleruby/language/exceptions/TryNode.java

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

12+
import com.oracle.truffle.api.dsl.Cached;
13+
import com.oracle.truffle.api.dsl.Specialization;
1214
import com.oracle.truffle.api.exception.AbstractTruffleException;
1315
import com.oracle.truffle.api.interop.InteropLibrary;
14-
import com.oracle.truffle.api.profiles.ConditionProfile;
1516
import com.oracle.truffle.api.CompilerDirectives;
1617
import com.oracle.truffle.api.TruffleSafepoint;
18+
import com.oracle.truffle.api.profiles.InlinedBranchProfile;
19+
import com.oracle.truffle.api.profiles.InlinedConditionProfile;
1720
import org.truffleruby.core.exception.ExceptionOperations;
1821
import org.truffleruby.language.RubyContextSourceNode;
1922
import org.truffleruby.language.RubyNode;
@@ -26,24 +29,17 @@
2629
import com.oracle.truffle.api.frame.VirtualFrame;
2730
import com.oracle.truffle.api.nodes.ExplodeLoop;
2831
import com.oracle.truffle.api.nodes.ExplodeLoop.LoopExplosionKind;
29-
import com.oracle.truffle.api.profiles.BranchProfile;
3032
import org.truffleruby.language.methods.TranslateExceptionNode;
3133
import org.truffleruby.language.threadlocal.ThreadLocalGlobals;
3234

33-
public class TryNode extends RubyContextSourceNode {
35+
public abstract class TryNode extends RubyContextSourceNode {
3436

3537
@Child private RubyNode tryPart;
3638
@Children private final RescueNode[] rescueParts;
3739
@Child private RubyNode elsePart;
3840
@Child private TranslateExceptionNode translateExceptionNode;
3941
private final boolean canOmitBacktrace;
4042

41-
private final BranchProfile noExceptionProfile = BranchProfile.create();
42-
private final BranchProfile killExceptionProfile = BranchProfile.create();
43-
private final BranchProfile guestExceptionProfile = BranchProfile.create();
44-
private final BranchProfile retryProfile = BranchProfile.create();
45-
private final ConditionProfile raiseExceptionProfile = ConditionProfile.create();
46-
4743
public TryNode(
4844
RubyNode tryPart,
4945
RescueNode[] rescueParts,
@@ -56,23 +52,28 @@ public TryNode(
5652
}
5753

5854
/** Based on {@link InteropLibrary#throwException(Object)}'s {@code TryCatchNode} */
59-
@Override
60-
public Object execute(VirtualFrame frame) {
55+
@Specialization
56+
protected Object doTry(VirtualFrame frame,
57+
@Cached InlinedBranchProfile noExceptionProfile,
58+
@Cached InlinedBranchProfile killExceptionProfile,
59+
@Cached InlinedBranchProfile guestExceptionProfile,
60+
@Cached InlinedBranchProfile retryProfile,
61+
@Cached InlinedConditionProfile raiseExceptionProfile) {
6162
while (true) {
6263
Object result;
6364

6465
try {
6566
result = tryPart.execute(frame);
66-
noExceptionProfile.enter();
67+
noExceptionProfile.enter(this);
6768
} catch (KillException e) { // an AbstractTruffleException but must not set $! and cannot be rescue'd
68-
killExceptionProfile.enter();
69+
killExceptionProfile.enter(this);
6970
throw e;
7071
} catch (AbstractTruffleException exception) {
71-
guestExceptionProfile.enter();
72+
guestExceptionProfile.enter(this);
7273
try {
73-
return handleException(frame, exception);
74+
return handleException(frame, exception, raiseExceptionProfile);
7475
} catch (RetryException e) {
75-
retryProfile.enter();
76+
retryProfile.enter(this);
7677
TruffleSafepoint.poll(this);
7778
continue;
7879
}
@@ -93,8 +94,9 @@ public Object execute(VirtualFrame frame) {
9394
}
9495

9596
@ExplodeLoop(kind = LoopExplosionKind.FULL_UNROLL_UNTIL_RETURN)
96-
private Object handleException(VirtualFrame frame, AbstractTruffleException exception) {
97-
final Object exceptionObject = ExceptionOperations.getExceptionObject(exception, raiseExceptionProfile);
97+
private Object handleException(VirtualFrame frame, AbstractTruffleException exception,
98+
InlinedConditionProfile raiseExceptionProfile) {
99+
final Object exceptionObject = ExceptionOperations.getExceptionObject(this, exception, raiseExceptionProfile);
98100

99101
for (RescueNode rescue : rescueParts) {
100102
if (rescue.canHandle(frame, exceptionObject)) {
@@ -142,7 +144,7 @@ private void printBacktraceOnRescue(RescueNode rescue, AbstractTruffleException
142144

143145
@Override
144146
public RubyNode cloneUninitialized() {
145-
var copy = new TryNode(
147+
var copy = TryNodeGen.create(
146148
tryPart.cloneUninitialized(),
147149
cloneUninitialized(rescueParts),
148150
cloneUninitialized(elsePart),

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@
9999
import org.truffleruby.language.exceptions.RescueClassesNode;
100100
import org.truffleruby.language.exceptions.RescueNode;
101101
import org.truffleruby.language.exceptions.RescueSplatNode;
102-
import org.truffleruby.language.exceptions.TryNode;
102+
import org.truffleruby.language.exceptions.TryNodeGen;
103103
import org.truffleruby.language.globals.AliasGlobalVarNode;
104104
import org.truffleruby.language.globals.ReadGlobalVariableNodeGen;
105105
import org.truffleruby.language.globals.ReadMatchReferenceNodes;
@@ -2669,7 +2669,7 @@ public RubyNode visitRescueNode(RescueParseNode node) {
26692669
elsePart = node.getElseNode().accept(this);
26702670
}
26712671

2672-
final RubyNode ret = new TryNode(
2672+
final RubyNode ret = TryNodeGen.create(
26732673
tryPart,
26742674
rescueNodes.toArray(EMPTY_RESCUE_NODE_ARRAY),
26752675
elsePart,

0 commit comments

Comments
 (0)