Skip to content

Commit 04435cf

Browse files
committed
push code down from PNode into StatementNode or ExpressionNode
1 parent bf014e6 commit 04435cf

File tree

19 files changed

+128
-132
lines changed

19 files changed

+128
-132
lines changed

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/grammar/TestParserTranslator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ <T> T parseAs(String src, Class<? extends T> klass) {
154154
}
155155

156156
Object literalAs(String src, Class<? extends PNode> klass) {
157-
PNode firstChild = parseAs(src, klass);
157+
ExpressionNode firstChild = (ExpressionNode) parseAs(src, klass);
158158
return firstChild.execute(null);
159159
}
160160

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/PythonLanguage.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import com.oracle.graal.python.nodes.PNode;
4848
import com.oracle.graal.python.nodes.call.InvokeNode;
4949
import com.oracle.graal.python.nodes.control.TopLevelExceptionHandler;
50+
import com.oracle.graal.python.nodes.expression.ExpressionNode;
5051
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
5152
import com.oracle.graal.python.nodes.object.GetClassNode;
5253
import com.oracle.graal.python.parser.PythonParserImpl;
@@ -81,7 +82,8 @@
8182
import com.oracle.truffle.api.source.Source.SourceBuilder;
8283

8384
@TruffleLanguage.Registration(id = PythonLanguage.ID, name = PythonLanguage.NAME, version = PythonLanguage.VERSION, mimeType = PythonLanguage.MIME_TYPE, interactive = true, internal = false, contextPolicy = TruffleLanguage.ContextPolicy.SHARED)
84-
@ProvidedTags({StandardTags.CallTag.class, StandardTags.StatementTag.class, StandardTags.RootTag.class, StandardTags.TryBlockTag.class, DebuggerTags.AlwaysHalt.class})
85+
@ProvidedTags({StandardTags.CallTag.class, StandardTags.StatementTag.class, StandardTags.RootTag.class, StandardTags.TryBlockTag.class, StandardTags.ExpressionTag.class,
86+
DebuggerTags.AlwaysHalt.class})
8587
public final class PythonLanguage extends TruffleLanguage<PythonContext> {
8688
public static final String ID = "python";
8789
public static final String NAME = "Python";
@@ -250,7 +252,7 @@ protected ExecutableNode parse(InlineParsingRequest request) throws Exception {
250252
final ExecutableNode executableNode = new ExecutableNode(this) {
251253
private final ContextReference<PythonContext> contextRef = getContextReference();
252254
@CompilationFinal private volatile PythonContext cachedContext;
253-
@Child private PNode expression;
255+
@Child private ExpressionNode expression;
254256

255257
@Override
256258
public Object execute(VirtualFrame frame) {
@@ -280,17 +282,17 @@ private void parseAndCache(PythonContext context) {
280282

281283
@TruffleBoundary
282284
private Object parseAndEval(PythonContext context, MaterializedFrame frame) {
283-
PNode fragment = parseInline(source, context, frame);
285+
ExpressionNode fragment = parseInline(source, context, frame);
284286
return fragment.execute(frame);
285287
}
286288
};
287289
return executableNode;
288290
}
289291

290292
@TruffleBoundary
291-
protected static PNode parseInline(Source code, PythonContext context, MaterializedFrame lexicalContextFrame) {
293+
protected static ExpressionNode parseInline(Source code, PythonContext context, MaterializedFrame lexicalContextFrame) {
292294
PythonCore pythonCore = context.getCore();
293-
return (PNode) pythonCore.getParser().parse(ParserMode.InlineEvaluation, pythonCore, code, lexicalContextFrame);
295+
return (ExpressionNode) pythonCore.getParser().parse(ParserMode.InlineEvaluation, pythonCore, code, lexicalContextFrame);
294296
}
295297

296298
@Override

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@
2626

2727
package com.oracle.graal.python.nodes;
2828

29+
import com.oracle.graal.python.nodes.expression.ExpressionNode;
2930
import com.oracle.truffle.api.frame.*;
3031

31-
public final class GraalPythonTranslationErrorNode extends PNode {
32+
public final class GraalPythonTranslationErrorNode extends ExpressionNode {
3233

3334
public static final String MESSAGE = "GraalPythonTranslationError";
3435

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

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

28-
import com.oracle.graal.python.nodes.statement.TryExceptNode;
2928
import com.oracle.graal.python.nodes.truffle.PythonTypes;
3029
import com.oracle.graal.python.runtime.PythonOptions;
3130
import com.oracle.truffle.api.CompilerAsserts;
3231
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
3332
import com.oracle.truffle.api.dsl.ImportStatic;
3433
import com.oracle.truffle.api.dsl.TypeSystemReference;
35-
import com.oracle.truffle.api.frame.VirtualFrame;
36-
import com.oracle.truffle.api.instrumentation.GenerateWrapper;
3734
import com.oracle.truffle.api.instrumentation.InstrumentableNode;
38-
import com.oracle.truffle.api.instrumentation.ProbeNode;
3935
import com.oracle.truffle.api.instrumentation.StandardTags;
4036
import com.oracle.truffle.api.instrumentation.Tag;
41-
import com.oracle.truffle.api.nodes.UnexpectedResultException;
4237
import com.oracle.truffle.api.source.SourceSection;
4338

4439
@TypeSystemReference(PythonTypes.class)
4540
@ImportStatic({PGuards.class, PythonOptions.class, SpecialMethodNames.class, SpecialAttributeNames.class, BuiltinNames.class})
46-
@GenerateWrapper
4741
public abstract class PNode extends PNodeWithContext implements InstrumentableNode {
4842

4943
public static final PNode[] EMPTY_ARRAY = new PNode[0];
5044

5145
@CompilationFinal private SourceSection sourceSection;
52-
@CompilationFinal private boolean isStmt = false;
5346
@CompilationFinal private boolean isRoot = false;
54-
@CompilationFinal private boolean isTryBlock = false;
5547

5648
@Override
5749
public String toString() {
@@ -68,44 +60,6 @@ public SourceSection getSourceSection() {
6860
return this.sourceSection;
6961
}
7062

71-
public abstract Object execute(VirtualFrame frame);
72-
73-
public int executeInt(VirtualFrame frame) throws UnexpectedResultException {
74-
Object value = execute(frame);
75-
if (value instanceof Integer) {
76-
return (int) value;
77-
}
78-
throw new UnexpectedResultException(value);
79-
}
80-
81-
public long executeLong(VirtualFrame frame) throws UnexpectedResultException {
82-
Object value = execute(frame);
83-
if (value instanceof Long) {
84-
return (long) value;
85-
}
86-
throw new UnexpectedResultException(value);
87-
}
88-
89-
public double executeDouble(VirtualFrame frame) throws UnexpectedResultException {
90-
Object o = execute(frame);
91-
if (o instanceof Double) {
92-
return (double) o;
93-
}
94-
throw new UnexpectedResultException(o);
95-
}
96-
97-
public boolean executeBoolean(VirtualFrame frame) throws UnexpectedResultException {
98-
Object o = execute(frame);
99-
if (o instanceof Boolean) {
100-
return (boolean) o;
101-
}
102-
throw new UnexpectedResultException(o);
103-
}
104-
105-
public boolean hasSideEffectAsAnExpression() {
106-
return false;
107-
}
108-
10963
public void clearSourceSection() {
11064
this.sourceSection = null;
11165
}
@@ -115,50 +69,18 @@ public void assignSourceSection(SourceSection source) {
11569
}
11670

11771
public boolean hasTag(Class<? extends Tag> tag) {
118-
return (isStmt && tag == StandardTags.StatementTag.class) || (isRoot && tag == StandardTags.RootTag.class) || (isTryBlock && tag == StandardTags.TryBlockTag.class);
119-
}
120-
121-
public WrapperNode createWrapper(ProbeNode probeNode) {
122-
return new PNodeWrapper(this, probeNode);
72+
return isRoot && tag == StandardTags.RootTag.class;
12373
}
12474

12575
public boolean isInstrumentable() {
12676
return getSourceSection() != null;
12777
}
12878

129-
public boolean isStatement() {
130-
return isStmt;
131-
}
132-
133-
public void markAsStatement() {
134-
isStmt = true;
135-
}
136-
13779
public void markAsRoot() {
13880
isRoot = true;
13981
}
14082

14183
public boolean isRoot() {
14284
return isRoot;
14385
}
144-
145-
public void markAsTryBlock() {
146-
isTryBlock = true;
147-
}
148-
149-
public boolean isTryBlock() {
150-
return isTryBlock;
151-
}
152-
153-
public Object getNodeObject() {
154-
if (isTryBlock) {
155-
if (this.getParent() instanceof TryExceptNode) {
156-
return this.getParent();
157-
} else if (this.getParent() instanceof PNodeWrapper) {
158-
assert this.getParent().getParent() instanceof TryExceptNode;
159-
return this.getParent().getParent();
160-
}
161-
}
162-
return null;
163-
}
16486
}

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,6 @@ public static void clearSourceSections(PNode node) {
8686
}
8787

8888
public static <T extends PNode> T replace(PNode oldNode, T node) {
89-
if (oldNode.isStatement()) {
90-
node.markAsStatement();
91-
}
9289
node.assignSourceSection(oldNode.getSourceSection());
9390
return oldNode.replace(node);
9491
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/argument/positional/PositionalArgumentsNode.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
*/
2626
package com.oracle.graal.python.nodes.argument.positional;
2727

28-
import com.oracle.graal.python.nodes.PNode;
2928
import com.oracle.graal.python.nodes.expression.ExpressionNode;
3029
import com.oracle.truffle.api.CompilerAsserts;
3130
import com.oracle.truffle.api.frame.VirtualFrame;
@@ -72,7 +71,7 @@ public static Object[] prependArgument(Object primary, Object[] arguments, int a
7271
}
7372

7473
@ExplodeLoop
75-
public static Object[] evaluateArguments(VirtualFrame frame, PNode[] arguments) {
74+
public static Object[] evaluateArguments(VirtualFrame frame, ExpressionNode[] arguments) {
7675
CompilerAsserts.compilationConstant(arguments);
7776
Object[] values = new Object[arguments.length];
7877
for (int i = 0; i < arguments.length; i++) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/cell/WriteCellVarNode.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
package com.oracle.graal.python.nodes.cell;
2727

2828
import com.oracle.graal.python.builtins.objects.cell.PCell;
29-
import com.oracle.graal.python.nodes.PNode;
29+
import com.oracle.graal.python.nodes.expression.ExpressionNode;
3030
import com.oracle.graal.python.nodes.statement.StatementNode;
3131
import com.oracle.truffle.api.dsl.Specialization;
3232
import com.oracle.truffle.api.frame.FrameSlot;
@@ -37,9 +37,9 @@
3737
public abstract class WriteCellVarNode extends StatementNode {
3838
private int cellIndex = -1;
3939
private final CellSupplier cellSupplier;
40-
@Child private PNode readNode;
40+
@Child private ExpressionNode readNode;
4141

42-
WriteCellVarNode(PNode readNode, CellSupplier cellSupplier, String identifier) {
42+
WriteCellVarNode(ExpressionNode readNode, CellSupplier cellSupplier, String identifier) {
4343
this.readNode = readNode;
4444
this.cellSupplier = cellSupplier;
4545

@@ -53,7 +53,7 @@ public abstract class WriteCellVarNode extends StatementNode {
5353
}
5454
}
5555

56-
public static WriteCellVarNode create(PNode readNode, CellSupplier cellSupplier, String identifier) {
56+
public static WriteCellVarNode create(ExpressionNode readNode, CellSupplier cellSupplier, String identifier) {
5757
return WriteCellVarNodeGen.create(readNode, cellSupplier, identifier);
5858
}
5959

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/classes/DeleteClassAttributeNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,6 @@ void deleteFromLocals(@SuppressWarnings("unused") VirtualFrame frame,
9393
@Specialization
9494
void delete(VirtualFrame frame) {
9595
// delete attribute actual attribute
96-
deleteNsItem.execute(frame);
96+
deleteNsItem.executeVoid(frame);
9797
}
9898
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ public StatementNode getCatchPart() {
5353
@Override
5454
public void executeVoid(VirtualFrame frame) {
5555
try {
56-
tryPart.execute(frame);
56+
tryPart.executeVoid(frame);
5757
} catch (PException ex) {
5858
ex.expectStopIteration(getCore(), errorProfile);
59-
catchPart.execute(frame);
59+
catchPart.executeVoid(frame);
6060
}
6161
}
6262
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/expression/ExpressionNode.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,63 @@
33
import com.oracle.graal.python.nodes.PNode;
44
import com.oracle.graal.python.nodes.statement.StatementNode;
55
import com.oracle.truffle.api.frame.VirtualFrame;
6+
import com.oracle.truffle.api.instrumentation.GenerateWrapper;
7+
import com.oracle.truffle.api.instrumentation.ProbeNode;
8+
import com.oracle.truffle.api.instrumentation.StandardTags.ExpressionTag;
9+
import com.oracle.truffle.api.instrumentation.Tag;
610
import com.oracle.truffle.api.nodes.NodeCost;
11+
import com.oracle.truffle.api.nodes.UnexpectedResultException;
712

13+
@GenerateWrapper
814
public abstract class ExpressionNode extends PNode {
15+
16+
public abstract Object execute(VirtualFrame frame);
17+
18+
public int executeInt(VirtualFrame frame) throws UnexpectedResultException {
19+
Object value = execute(frame);
20+
if (value instanceof Integer) {
21+
return (int) value;
22+
}
23+
throw new UnexpectedResultException(value);
24+
}
25+
26+
public long executeLong(VirtualFrame frame) throws UnexpectedResultException {
27+
Object value = execute(frame);
28+
if (value instanceof Long) {
29+
return (long) value;
30+
}
31+
throw new UnexpectedResultException(value);
32+
}
33+
34+
public double executeDouble(VirtualFrame frame) throws UnexpectedResultException {
35+
Object o = execute(frame);
36+
if (o instanceof Double) {
37+
return (double) o;
38+
}
39+
throw new UnexpectedResultException(o);
40+
}
41+
42+
public boolean executeBoolean(VirtualFrame frame) throws UnexpectedResultException {
43+
Object o = execute(frame);
44+
if (o instanceof Boolean) {
45+
return (boolean) o;
46+
}
47+
throw new UnexpectedResultException(o);
48+
}
49+
50+
public boolean hasSideEffectAsAnExpression() {
51+
return false;
52+
}
53+
54+
public WrapperNode createWrapper(ProbeNode probe) {
55+
return new ExpressionNodeWrapper(this, probe);
56+
}
57+
58+
@Override
59+
public boolean hasTag(Class<? extends Tag> tag) {
60+
return tag == ExpressionTag.class || super.hasTag(tag);
61+
}
62+
963
public static final class ExpressionStatementNode extends StatementNode {
1064
@Child private ExpressionNode node;
1165

0 commit comments

Comments
 (0)