Skip to content

Commit 890585e

Browse files
committed
change scope parsing to single-pass-and-fixup (instead of two-pass), make tree translation less stateful, store scopes in parse tree
1 parent 8058d71 commit 890585e

File tree

15 files changed

+463
-443
lines changed

15 files changed

+463
-443
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727

2828
import java.math.BigInteger;
2929
import java.util.List;
30-
import java.util.Set;
3130

3231
import com.oracle.graal.python.PythonLanguage;
3332
import com.oracle.graal.python.builtins.objects.complex.PComplex;
@@ -274,7 +273,7 @@ public PNode createListLiteral(PNode[] values) {
274273
return ListLiteralNode.create(values);
275274
}
276275

277-
public PNode createSetLiteral(Set<PNode> values) {
276+
public PNode createSetLiteral(List<PNode> values) {
278277
PNode[] convertedValues = values.toArray(new PNode[values.size()]);
279278
return new SetLiteralNode(convertedValues);
280279
}
@@ -524,7 +523,7 @@ public PNode createSetAttribute(PNode object, String key, PNode rhs) {
524523
return SetAttributeNode.create(key, object, rhs);
525524
}
526525

527-
public PNode createDestructuringAssignment(PNode rhs, List<ReadNode> slots, int starredIndex, PNode[] assignments) {
526+
public PNode createDestructuringAssignment(PNode rhs, ReadNode[] slots, int starredIndex, PNode[] assignments) {
528527
return DestructuringAssignmentNode.create(rhs, slots, starredIndex, assignments);
529528
}
530529

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@
4545
@ImportStatic({PGuards.class, PythonOptions.class, SpecialMethodNames.class, SpecialAttributeNames.class, BuiltinNames.class})
4646
@GenerateWrapper
4747
public abstract class PNode extends PBaseNode implements InstrumentableNode {
48+
49+
public static final PNode[] EMPTY_ARRAY = new PNode[0];
50+
4851
@CompilationFinal private SourceSection sourceSection;
4952
@CompilationFinal private boolean isStmt = false;
5053
@CompilationFinal private boolean isRoot = false;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/frame/DestructuringAssignmentNode.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import static com.oracle.graal.python.runtime.exception.PythonErrorType.SyntaxError;
3333

3434
import java.util.Arrays;
35-
import java.util.List;
3635

3736
import com.oracle.graal.python.builtins.modules.BuiltinFunctions;
3837
import com.oracle.graal.python.builtins.modules.BuiltinFunctionsFactory;
@@ -68,18 +67,18 @@ public final class DestructuringAssignmentNode extends PNode implements WriteNod
6867
private final ConditionProfile errorProfile2 = ConditionProfile.createBinaryProfile();
6968
private final int starredIndex;
7069

71-
public DestructuringAssignmentNode(PNode rhs, List<ReadNode> slots, int starredIndex, PNode[] assignments) {
70+
public DestructuringAssignmentNode(PNode rhs, ReadNode[] slots, int starredIndex, PNode[] assignments) {
7271
this.rhs = rhs;
7372
this.starredIndex = starredIndex;
7473
this.assignments = assignments;
75-
this.slots = new WriteNode[slots.size()];
76-
for (int i = 0; i < slots.size(); i++) {
77-
this.slots[i] = (WriteNode) slots.get(i).makeWriteNode(null);
74+
this.slots = new WriteNode[slots.length];
75+
for (int i = 0; i < slots.length; i++) {
76+
this.slots[i] = (WriteNode) slots[i].makeWriteNode(null);
7877
}
7978
this.lenNode = starredIndex == -1 ? null : BuiltinFunctionsFactory.LenNodeFactory.create();
8079
}
8180

82-
public static PNode create(PNode rhs, List<ReadNode> slots, int starredIndex, PNode[] assignments) {
81+
public static PNode create(PNode rhs, ReadNode[] slots, int starredIndex, PNode[] assignments) {
8382
return new DestructuringAssignmentNode(rhs, slots, starredIndex, assignments);
8483
}
8584

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/frame/FrameSlotIDs.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,4 @@
2727

2828
public abstract class FrameSlotIDs {
2929
public static final String RETURN_SLOT_ID = "<return_val>";
30-
public static final String LIST_COMPREHENSION_SLOT_ID = "<list_comp_val>";
3130
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/function/BuiltinFunctionRootNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public Object execute(VirtualFrame frame) {
145145
}
146146

147147
public BuiltinFunctionRootNode(PythonLanguage language, Builtin builtin, NodeFactory<? extends PythonBuiltinBaseNode> factory, boolean declaresExplicitSelf) {
148-
super(language, null);
148+
super(language);
149149
this.builtin = builtin;
150150
this.factory = factory;
151151
this.declaresExplicitSelf = declaresExplicitSelf;

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ public class AssignmentTranslator extends Python3BaseVisitor<PNode> {
5959

6060
private final NodeFactory factory;
6161
private final TranslationEnvironment environment;
62-
private final PythonBaseTreeTranslator<?> translator;
62+
private final PythonTreeTranslator translator;
6363
private final PythonCore core;
6464

65-
public AssignmentTranslator(PythonCore core, TranslationEnvironment environment, PythonBaseTreeTranslator<?> translator) {
65+
public AssignmentTranslator(PythonCore core, TranslationEnvironment environment, PythonTreeTranslator translator) {
6666
this.core = core;
6767
this.factory = core.getLanguage().getNodeFactory();
6868
this.environment = environment;
@@ -103,37 +103,37 @@ private PNode createAssignment(PNode lhs, PNode rhs) {
103103
}
104104

105105
private PNode createDestructuringAssignment(PNode[] leftHandSides, PNode rhs) {
106-
List<PNode> statements = new ArrayList<>();
107-
List<ReadNode> temps = new ArrayList<>();
106+
PNode[] statements = new PNode[leftHandSides.length];
107+
ReadNode[] temps = new ReadNode[leftHandSides.length];
108108
int starredIndex = -1;
109109
for (int i = 0; i < leftHandSides.length; i++) {
110110
ReadNode tempRead = environment.makeTempLocalVariable();
111-
temps.add(tempRead);
111+
temps[i] = tempRead;
112112
if (leftHandSides[i] instanceof StarredExpressionNode) {
113113
if (starredIndex != -1) {
114114
throw core.raise(SyntaxError, "two starred expressions in assignment");
115115
}
116116
starredIndex = i;
117-
statements.add(createAssignment(((StarredExpressionNode) leftHandSides[i]).getValue(), (PNode) tempRead));
117+
statements[i] = createAssignment(((StarredExpressionNode) leftHandSides[i]).getValue(), (PNode) tempRead);
118118
} else {
119-
statements.add(createAssignment(leftHandSides[i], (PNode) tempRead));
119+
statements[i] = createAssignment(leftHandSides[i], (PNode) tempRead);
120120
}
121121
}
122-
return factory.createDestructuringAssignment(rhs, temps, starredIndex, statements.toArray(new PNode[0]));
122+
return factory.createDestructuringAssignment(rhs, temps, starredIndex, statements);
123123
}
124124

125125
private PNode createMultiAssignment(List<NormassignContext> normassign, PNode mostRhs, PNode mostLhs) {
126126
ReadNode tmp = environment.makeTempLocalVariable();
127127
PNode tmpWrite = tmp.makeWriteNode(mostRhs);
128-
List<PNode> assignments = new ArrayList<>();
129-
assignments.add(tmpWrite);
130-
assignments.add(createAssignment(mostLhs, (PNode) tmp));
128+
PNode[] assignments = new PNode[normassign.size() + 1];
129+
assignments[0] = tmpWrite;
130+
assignments[1] = createAssignment(mostLhs, (PNode) tmp);
131131
for (int i = 0; i < normassign.size() - 1; i++) {
132132
NormassignContext normassignContext = normassign.get(i);
133133
if (normassignContext.yield_expr() != null) {
134134
throw core.raise(SyntaxError, "assignment to yield expression not possible");
135135
}
136-
assignments.add(createAssignment(normassignContext.accept(this), (PNode) tmp));
136+
assignments[i + 2] = createAssignment(normassignContext.accept(this), (PNode) tmp);
137137
}
138138
return factory.createBlock(assignments);
139139
}

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

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

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

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,19 @@
2727

2828
import static com.oracle.graal.python.runtime.exception.PythonErrorType.SyntaxError;
2929

30-
import java.util.function.Consumer;
31-
3230
import org.antlr.v4.runtime.CharStreams;
3331
import org.antlr.v4.runtime.ParserRuleContext;
3432

3533
import com.oracle.graal.python.builtins.objects.exception.PBaseException;
3634
import com.oracle.graal.python.builtins.objects.type.PythonClass;
37-
import com.oracle.graal.python.parser.ScopeTranslator.ScopeTranslatorFactory;
3835
import com.oracle.graal.python.parser.antlr.Builder;
3936
import com.oracle.graal.python.parser.antlr.Python3Parser;
4037
import com.oracle.graal.python.runtime.PythonCore;
4138
import com.oracle.graal.python.runtime.PythonParser;
4239
import com.oracle.graal.python.runtime.exception.PException;
4340
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
4441
import com.oracle.truffle.api.frame.Frame;
42+
import com.oracle.truffle.api.frame.FrameDescriptor;
4543
import com.oracle.truffle.api.nodes.Node;
4644
import com.oracle.truffle.api.source.Source;
4745
import com.oracle.truffle.api.source.SourceSection;
@@ -57,6 +55,7 @@ private static Python3Parser getPython3Parser(String string) {
5755
@Override
5856
@TruffleBoundary
5957
public Node parse(ParserMode mode, PythonCore core, Source source, Frame currentFrame) {
58+
// ANTLR parsing
6059
Python3Parser parser = getPython3Parser(source.getCharacters().toString());
6160
ParserRuleContext input;
6261
try {
@@ -91,19 +90,19 @@ public Node parse(ParserMode mode, PythonCore core, Source source, Frame current
9190
throw handleParserError(core, source, e);
9291
}
9392
}
93+
94+
// prepare scope translator
9495
TranslationEnvironment environment = new TranslationEnvironment(core.getLanguage());
95-
Node result;
96-
Consumer<TranslationEnvironment> environmentConsumer = (env) -> env.setFreeVarsInRootScope(currentFrame);
97-
if (mode == ParserMode.InlineEvaluation) {
98-
ScopeTranslatorFactory scopeTranslator = (env, trackCells) -> new InlineScopeTranslator<>(core, env, currentFrame.getFrameDescriptor(), trackCells);
99-
ScopeTranslator.accept(input, environment, scopeTranslator, environmentConsumer);
100-
result = new PythonInlineTreeTranslator(core, source.getName(), input, environment, source).getTranslationResult();
101-
} else {
102-
ScopeTranslatorFactory scopeTranslator = (env, trackCells) -> new ScopeTranslator<>(core, env, source.isInteractive(), trackCells);
103-
ScopeTranslator.accept(input, environment, scopeTranslator, environmentConsumer);
104-
result = new PythonTreeTranslator(core, source.getName(), input, environment, source).getTranslationResult();
105-
}
106-
return result;
96+
FrameDescriptor inlineLocals = mode == ParserMode.InlineEvaluation ? currentFrame.getFrameDescriptor() : null;
97+
ScopeTranslator<Object> defineScopes = new ScopeTranslator<>(core, environment, source.isInteractive(), inlineLocals);
98+
// first pass of the scope translator -> define the scopes
99+
input.accept(defineScopes);
100+
// create frame slots for cell and free vars
101+
defineScopes.setFreeVarsInRootScope(currentFrame);
102+
defineScopes.createFrameSlotsForCellAndFreeVars();
103+
104+
// create Truffle ASTs
105+
return PythonTreeTranslator.translate(core, source.getName(), input, environment, source, mode == ParserMode.InlineEvaluation);
107106
}
108107

109108
@Override

0 commit comments

Comments
 (0)