Skip to content

Commit 95baaa3

Browse files
committed
EnsureNode is converted to DSL node
1 parent edc7f95 commit 95baaa3

File tree

3 files changed

+38
-18
lines changed

3 files changed

+38
-18
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.oracle.truffle.api.CompilerDirectives;
1313
import com.oracle.truffle.api.exception.AbstractTruffleException;
1414
import com.oracle.truffle.api.profiles.ConditionProfile;
15+
import com.oracle.truffle.api.profiles.InlinedConditionProfile;
1516
import org.truffleruby.RubyContext;
1617
import org.truffleruby.RubyLanguage;
1718
import org.truffleruby.core.kernel.KernelNodes;
@@ -85,6 +86,7 @@ public static Object getExceptionObject(AbstractTruffleException exception) {
8586
}
8687
}
8788

89+
// TODO remove when TryNode is refactored
8890
public static Object getExceptionObject(AbstractTruffleException exception,
8991
ConditionProfile raiseExceptionProfile) {
9092
assert !(exception instanceof KillException)
@@ -96,6 +98,17 @@ public static Object getExceptionObject(AbstractTruffleException exception,
9698
}
9799
}
98100

101+
public static Object getExceptionObject(Node node, AbstractTruffleException exception,
102+
InlinedConditionProfile raiseExceptionProfile) {
103+
assert !(exception instanceof KillException)
104+
: "KillException should not be used as an exception object: " + exception;
105+
if (raiseExceptionProfile.profile(node, exception instanceof RaiseException)) {
106+
return ((RaiseException) exception).getException();
107+
} else {
108+
return exception;
109+
}
110+
}
111+
99112
@TruffleBoundary
100113
public static String getMessage(Throwable throwable) {
101114
return throwable.getMessage();

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

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,47 +9,53 @@
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;
1416
import com.oracle.truffle.api.nodes.ControlFlowException;
15-
import com.oracle.truffle.api.profiles.ConditionProfile;
17+
import com.oracle.truffle.api.profiles.InlinedBranchProfile;
18+
import com.oracle.truffle.api.profiles.InlinedConditionProfile;
1619
import org.truffleruby.core.exception.ExceptionOperations;
1720
import org.truffleruby.language.RubyContextSourceNode;
1821
import org.truffleruby.language.RubyNode;
1922

2023
import com.oracle.truffle.api.frame.VirtualFrame;
21-
import com.oracle.truffle.api.profiles.BranchProfile;
2224
import org.truffleruby.language.control.KillException;
2325
import org.truffleruby.language.threadlocal.ThreadLocalGlobals;
2426

25-
public class EnsureNode extends RubyContextSourceNode {
27+
public abstract class EnsureNode extends RubyContextSourceNode {
2628

2729
@Child private RubyNode tryPart;
2830
@Child private RubyNode ensurePart;
2931

30-
private final BranchProfile killExceptionProfile = BranchProfile.create();
31-
private final BranchProfile guestExceptionProfile = BranchProfile.create();
32-
private final BranchProfile controlFlowExceptionProfile = BranchProfile.create();
33-
private final ConditionProfile raiseExceptionProfile = ConditionProfile.create();
34-
3532
public EnsureNode(RubyNode tryPart, RubyNode ensurePart) {
3633
this.tryPart = tryPart;
3734
this.ensurePart = ensurePart;
3835
}
3936

37+
4038
@Override
41-
public Object execute(VirtualFrame frame) {
39+
public final Object execute(VirtualFrame frame) {
4240
return executeCommon(frame, false);
4341
}
4442

4543
@Override
46-
public void doExecuteVoid(VirtualFrame frame) {
44+
public final void doExecuteVoid(VirtualFrame frame) {
4745
executeCommon(frame, true);
4846
}
4947

48+
protected abstract Object executeCommon(VirtualFrame frame, boolean executeVoid);
49+
50+
5051
/** Based on {@link InteropLibrary#throwException(Object)}'s {@code TryCatchNode}. It only runs code in ensure for
5152
* guest exceptions (AbstractTruffleException), ControlFlowException or no exception. */
52-
public Object executeCommon(VirtualFrame frame, boolean executeVoid) {
53+
@Specialization
54+
protected Object ensure(VirtualFrame frame, boolean executeVoid,
55+
@Cached InlinedBranchProfile killExceptionProfile,
56+
@Cached InlinedBranchProfile guestExceptionProfile,
57+
@Cached InlinedBranchProfile controlFlowExceptionProfile,
58+
@Cached InlinedConditionProfile raiseExceptionProfile) {
5359
Object value = nil;
5460
RuntimeException rethrowException = null;
5561
AbstractTruffleException guestException = null;
@@ -61,22 +67,22 @@ public Object executeCommon(VirtualFrame frame, boolean executeVoid) {
6167
value = tryPart.execute(frame);
6268
}
6369
} catch (KillException e) { // an AbstractTruffleException but must not set $!
64-
killExceptionProfile.enter();
70+
killExceptionProfile.enter(this);
6571
rethrowException = e;
6672
} catch (AbstractTruffleException e) {
67-
guestExceptionProfile.enter();
73+
guestExceptionProfile.enter(this);
6874
guestException = e;
6975
rethrowException = e;
7076
} catch (ControlFlowException e) {
71-
controlFlowExceptionProfile.enter();
77+
controlFlowExceptionProfile.enter(this);
7278
rethrowException = e;
7379
}
7480

7581
ThreadLocalGlobals threadLocalGlobals = null;
7682
Object previousException = null;
7783

7884
if (guestException != null) {
79-
var exceptionObject = ExceptionOperations.getExceptionObject(guestException, raiseExceptionProfile);
85+
var exceptionObject = ExceptionOperations.getExceptionObject(this, guestException, raiseExceptionProfile);
8086
threadLocalGlobals = getLanguage().getCurrentThread().threadLocalGlobals;
8187
previousException = threadLocalGlobals.getLastException();
8288
threadLocalGlobals.setLastException(exceptionObject);
@@ -96,9 +102,10 @@ public Object executeCommon(VirtualFrame frame, boolean executeVoid) {
96102
}
97103
}
98104

105+
99106
@Override
100107
public RubyNode cloneUninitialized() {
101-
var copy = new EnsureNode(
108+
var copy = EnsureNodeGen.create(
102109
tryPart.cloneUninitialized(),
103110
ensurePart.cloneUninitialized());
104111
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
@@ -94,7 +94,7 @@
9494
import org.truffleruby.language.defined.DefinedNode;
9595
import org.truffleruby.language.defined.DefinedWrapperNode;
9696
import org.truffleruby.language.dispatch.RubyCallNodeParameters;
97-
import org.truffleruby.language.exceptions.EnsureNode;
97+
import org.truffleruby.language.exceptions.EnsureNodeGen;
9898
import org.truffleruby.language.exceptions.RescueStandardErrorNode;
9999
import org.truffleruby.language.exceptions.RescueClassesNode;
100100
import org.truffleruby.language.exceptions.RescueNode;
@@ -1435,7 +1435,7 @@ public RubyNode visitEncodingNode(EncodingParseNode node) {
14351435
public RubyNode visitEnsureNode(EnsureParseNode node) {
14361436
final RubyNode tryPart = node.getBodyNode().accept(this);
14371437
final RubyNode ensurePart = node.getEnsureNode().accept(this);
1438-
final RubyNode ret = new EnsureNode(tryPart, ensurePart);
1438+
final RubyNode ret = EnsureNodeGen.create(tryPart, ensurePart);
14391439
ret.unsafeSetSourceSection(node.getPosition());
14401440
return addNewlineIfNeeded(node, ret);
14411441
}

0 commit comments

Comments
 (0)