Skip to content

Commit fbbb711

Browse files
committed
keep flag for YieldNode instead of special handling in GeneratorBlockNode
1 parent 48d6547 commit fbbb711

File tree

7 files changed

+39
-105
lines changed

7 files changed

+39
-105
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/NodeFactory.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@
7676
import com.oracle.graal.python.nodes.function.FunctionRootNode;
7777
import com.oracle.graal.python.nodes.generator.DictConcatNode;
7878
import com.oracle.graal.python.nodes.generator.YieldNode;
79-
import com.oracle.graal.python.nodes.generator.YieldResumeNode;
8079
import com.oracle.graal.python.nodes.literal.BooleanLiteralNode;
8180
import com.oracle.graal.python.nodes.literal.BuiltinsLiteralNode;
8281
import com.oracle.graal.python.nodes.literal.BytesLiteralNode;
@@ -213,7 +212,7 @@ public StatementNode createBreakTarget(PNode forNode) {
213212
}
214213

215214
public PNode createYield(PNode right, FrameSlot returnSlot) {
216-
return createBlock(new YieldNode(createWriteLocal(right, returnSlot)), new YieldResumeNode());
215+
return new YieldNode(createWriteLocal(right, returnSlot));
217216
}
218217

219218
public PNode createIntegerLiteral(int value) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/control/BaseBlockNode.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ protected BaseBlockNode(PNode[] statements) {
3939
assert statements.length > 0;
4040
}
4141

42-
public abstract BaseBlockNode insertNodesBefore(PNode insertBefore, List<PNode> insertees);
43-
4442
protected PNode[] insertStatementsBefore(PNode insertBefore, List<PNode> insertees) {
4543
int insertAt = -1;
4644
for (int i = 0; i < statements.length; i++) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/control/BlockNode.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,8 @@
2525
*/
2626
package com.oracle.graal.python.nodes.control;
2727

28-
import java.util.List;
29-
3028
import com.oracle.graal.python.nodes.EmptyNode;
3129
import com.oracle.graal.python.nodes.PNode;
32-
import com.oracle.graal.python.nodes.generator.YieldNode;
3330
import com.oracle.truffle.api.frame.VirtualFrame;
3431
import com.oracle.truffle.api.nodes.ExplodeLoop;
3532
import com.oracle.truffle.api.nodes.UnexpectedResultException;
@@ -46,17 +43,12 @@ public static PNode create(PNode... statements) {
4643
if (length == 0) {
4744
return EmptyNode.create();
4845
} else if (length == 1) {
49-
return statements[0] instanceof YieldNode ? new BlockNode(statements) : statements[0];
46+
return statements[0];
5047
} else {
5148
return new BlockNode(statements);
5249
}
5350
}
5451

55-
@Override
56-
public BaseBlockNode insertNodesBefore(PNode insertBefore, List<PNode> insertees) {
57-
return new BlockNode(insertStatementsBefore(insertBefore, insertees));
58-
}
59-
6052
@ExplodeLoop
6153
private void executeFirst(VirtualFrame frame) {
6254
for (int i = 0; i < statements.length - 1; i++) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/generator/GeneratorBlockNode.java

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,35 +31,21 @@
3131
import com.oracle.graal.python.nodes.PNode;
3232
import com.oracle.graal.python.nodes.control.BaseBlockNode;
3333
import com.oracle.graal.python.runtime.exception.YieldException;
34-
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
3534
import com.oracle.truffle.api.frame.VirtualFrame;
3635
import com.oracle.truffle.api.nodes.ExplodeLoop;
3736
import com.oracle.truffle.api.profiles.BranchProfile;
3837
import com.oracle.truffle.api.profiles.ConditionProfile;
3938

4039
public final class GeneratorBlockNode extends BaseBlockNode implements GeneratorControlNode {
4140

42-
private static final boolean[] EMPTY = new boolean[0];
43-
4441
@Child private GeneratorAccessNode gen = GeneratorAccessNode.create();
4542

4643
private final ConditionProfile needsUpdateProfile = ConditionProfile.createBinaryProfile();
4744
private final BranchProfile seenYield = BranchProfile.create();
48-
@CompilationFinal(dimensions = 1) private final boolean[] isYield;
4945
private final int indexSlot;
5046

5147
public GeneratorBlockNode(PNode[] statements, int indexSlot) {
5248
super(statements);
53-
boolean[] yields = EMPTY;
54-
for (int i = 0; i < statements.length; i++) {
55-
if (statements[i] instanceof YieldNode) {
56-
if (yields.length == 0) {
57-
yields = new boolean[statements.length];
58-
}
59-
yields[i] = true;
60-
}
61-
}
62-
this.isYield = yields;
6349
this.indexSlot = indexSlot;
6450
}
6551

@@ -71,7 +57,6 @@ public int getIndexSlot() {
7157
return indexSlot;
7258
}
7359

74-
@Override
7560
public GeneratorBlockNode insertNodesBefore(PNode insertBefore, List<PNode> insertees) {
7661
return new GeneratorBlockNode(insertStatementsBefore(insertBefore, insertees), getIndexSlot());
7762
}
@@ -92,12 +77,7 @@ public Object execute(VirtualFrame frame) {
9277
return result;
9378
} catch (YieldException e) {
9479
seenYield.enter();
95-
if (i < isYield.length && isYield[i]) {
96-
// continue after the yield
97-
nextIndex = i + 1;
98-
} else {
99-
nextIndex = i;
100-
}
80+
nextIndex = i;
10181
throw e;
10282
} finally {
10383
if (needsUpdateProfile.profile(nextIndex != startIndex)) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/generator/YieldNode.java

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,26 @@
2525
*/
2626
package com.oracle.graal.python.nodes.generator;
2727

28+
import com.oracle.graal.python.builtins.objects.PNone;
29+
import com.oracle.graal.python.builtins.objects.function.PArguments;
2830
import com.oracle.graal.python.nodes.PNode;
2931
import com.oracle.graal.python.nodes.statement.StatementNode;
32+
import com.oracle.graal.python.runtime.exception.PException;
3033
import com.oracle.graal.python.runtime.exception.YieldException;
34+
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
3135
import com.oracle.truffle.api.frame.VirtualFrame;
36+
import com.oracle.truffle.api.profiles.BranchProfile;
3237

3338
public class YieldNode extends StatementNode implements GeneratorControlNode {
3439

3540
@Child private PNode right;
41+
@Child private GeneratorAccessNode access = GeneratorAccessNode.create();
42+
43+
@CompilationFinal private int flagSlot;
44+
45+
private final BranchProfile gotException = BranchProfile.create();
46+
private final BranchProfile gotValue = BranchProfile.create();
47+
private final BranchProfile gotNothing = BranchProfile.create();
3648

3749
public YieldNode(PNode right) {
3850
this.right = right;
@@ -44,7 +56,28 @@ public PNode getRhs() {
4456

4557
@Override
4658
public Object execute(VirtualFrame frame) {
47-
right.execute(frame);
48-
throw YieldException.INSTANCE;
59+
if (access.isActive(frame, flagSlot)) {
60+
// yield is active -> resume
61+
access.setActive(frame, flagSlot, false);
62+
Object specialArgument = PArguments.getSpecialArgument(frame);
63+
if (specialArgument == null) {
64+
gotNothing.enter();
65+
return PNone.NONE;
66+
} else if (specialArgument instanceof PException) {
67+
gotException.enter();
68+
throw (PException) specialArgument;
69+
} else {
70+
gotValue.enter();
71+
return specialArgument;
72+
}
73+
} else {
74+
right.executeVoid(frame);
75+
access.setActive(frame, flagSlot, true);
76+
throw YieldException.INSTANCE;
77+
}
78+
}
79+
80+
public void setFlagSlot(int slot) {
81+
this.flagSlot = slot;
4982
}
5083
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/generator/YieldResumeNode.java

Lines changed: 0 additions & 69 deletions
This file was deleted.

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/parser/GeneratorTranslator.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ public RootCallTarget translate() {
124124

125125
private void replaceYield(YieldNode yield) {
126126
PNode current = yield;
127+
yield.setFlagSlot(nextActiveFlagSlot());
127128

128129
if (yield.getParent() instanceof GeneratorReturnTargetNode) {
129130
// if this yield is the only thing in the body, we introduce a block

0 commit comments

Comments
 (0)