Skip to content

Commit 98aa4a2

Browse files
committed
adopt new Frame API
1 parent b605060 commit 98aa4a2

File tree

144 files changed

+1871
-2410
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

144 files changed

+1871
-2410
lines changed

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/parser/BasicTests.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import org.junit.Test;
4848

4949
import com.oracle.graal.python.builtins.objects.str.StringUtils;
50+
import com.oracle.graal.python.nodes.frame.PythonFrame;
5051
import com.oracle.graal.python.runtime.PythonParser;
5152
import com.oracle.truffle.api.Truffle;
5253
import com.oracle.truffle.api.frame.Frame;
@@ -130,9 +131,7 @@ public void assert04() throws Exception {
130131

131132
@Test
132133
public void inline01() throws Exception {
133-
FrameDescriptor fd = new FrameDescriptor(44);
134-
fd.addFrameSlot("a");
135-
fd.addFrameSlot("b");
134+
FrameDescriptor fd = PythonFrame.createTestFrameDescriptor("a", "b");
136135
Frame frame = Truffle.getRuntime().createVirtualFrame(new Object[]{2, 3, null}, fd);
137136
checkTreeResult("a + b", PythonParser.ParserMode.InlineEvaluation, frame);
138137
}

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/parser/ParserTestBase.java

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,15 @@
6565
import com.oracle.graal.python.runtime.PythonParser;
6666
import com.oracle.graal.python.runtime.exception.PException;
6767
import com.oracle.graal.python.test.PythonTests;
68+
import com.oracle.truffle.api.CompilerDirectives;
6869
import com.oracle.truffle.api.TruffleFile;
6970
import com.oracle.truffle.api.frame.Frame;
71+
import com.oracle.truffle.api.frame.VirtualFrame;
7072
import com.oracle.truffle.api.interop.ExceptionType;
7173
import com.oracle.truffle.api.interop.InteropLibrary;
7274
import com.oracle.truffle.api.interop.UnsupportedMessageException;
7375
import com.oracle.truffle.api.nodes.Node;
76+
import com.oracle.truffle.api.nodes.RootNode;
7477
import com.oracle.truffle.api.source.Source;
7578

7679
public class ParserTestBase {
@@ -113,10 +116,28 @@ protected Source createSource(File testFile) throws Exception {
113116

114117
public Node parse(String src, String moduleName, PythonParser.ParserMode mode, Frame fd) {
115118
Source source = Source.newBuilder(PythonLanguage.ID, src, moduleName).build();
119+
Node result = parseInternal(source, mode, fd);
120+
return result;
121+
}
122+
123+
private Node parseInternal(Source source, PythonParser.ParserMode mode, Frame fd) {
116124
PythonParser parser = context.getParser();
117125
Node result = ((PythonParserImpl) parser).parseN(mode, 0, context, source, fd, null);
118126
lastGlobalScope = ((PythonParserImpl) parser).getLastGlobaScope();
119127
lastSST = ((PythonParserImpl) parser).getLastSST();
128+
// ensure that node parent pointers are set:
129+
if (result instanceof RootNode) {
130+
((RootNode) result).getCallTarget();
131+
} else {
132+
new RootNode(null, fd.getFrameDescriptor()) {
133+
@Child private Node adopted = result;
134+
135+
@Override
136+
public Object execute(VirtualFrame frame) {
137+
throw CompilerDirectives.shouldNotReachHere();
138+
}
139+
}.getCallTarget();
140+
}
120141
return result;
121142
}
122143

@@ -125,11 +146,7 @@ public Node parse(String src, String moduleName, PythonParser.ParserMode mode) {
125146
}
126147

127148
public Node parse(Source source, PythonParser.ParserMode mode) {
128-
PythonParser parser = context.getParser();
129-
Node result = ((PythonParserImpl) parser).parseN(mode, 0, context, source, null, null);
130-
lastGlobalScope = ((PythonParserImpl) parser).getLastGlobaScope();
131-
lastSST = ((PythonParserImpl) parser).getLastSST();
132-
return result;
149+
return parseInternal(source, mode, null);
133150
}
134151

135152
protected ScopeInfo getLastGlobalScope() {
@@ -334,7 +351,7 @@ protected void assertDescriptionMatches(String actual, String expected, String s
334351
return; // Only difference is in line separation --> Test passed
335352
}
336353

337-
// There are some diffrerences between expected and actual content --> Test failed
354+
// There are some differences between expected and actual content --> Test failed
338355

339356
assertTrue("Not matching results: " + (someName == null ? "" : someName) + lineSeparator(2) + getContentDifferences(expectedUnified, actualUnified), false);
340357
}

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/parser/ParserTreePrinter.java

Lines changed: 23 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
package com.oracle.graal.python.test.parser;
4343

4444
import java.util.Collection;
45-
import java.util.Set;
4645
import java.util.TreeSet;
4746

4847
import com.oracle.graal.python.builtins.objects.function.Signature;
@@ -61,6 +60,7 @@
6160
import com.oracle.graal.python.nodes.frame.AccessNameNode;
6261
import com.oracle.graal.python.nodes.frame.FrameSlotIDs;
6362
import com.oracle.graal.python.nodes.frame.FrameSlotNode;
63+
import com.oracle.graal.python.nodes.frame.PythonFrame;
6464
import com.oracle.graal.python.nodes.frame.ReadGlobalOrBuiltinNode;
6565
import com.oracle.graal.python.nodes.frame.WriteGlobalNode;
6666
import com.oracle.graal.python.nodes.frame.WriteIdentifierNode;
@@ -82,7 +82,6 @@
8282
import com.oracle.graal.python.parser.GeneratorInfo;
8383
import com.oracle.graal.python.runtime.ExecutionContext;
8484
import com.oracle.truffle.api.frame.FrameDescriptor;
85-
import com.oracle.truffle.api.frame.FrameSlot;
8685
import com.oracle.truffle.api.nodes.Node;
8786
import com.oracle.truffle.api.nodes.NodeVisitor;
8887
import com.oracle.truffle.api.nodes.RootNode;
@@ -109,7 +108,7 @@ public boolean visit(ModuleRootNode module) {
109108
nodeHeader(module, module.getName());
110109
level++;
111110
addSignature(module.getSignature());
112-
addInfoPCloserRootNode(module);
111+
addInfoPClosureRootNode(module);
113112
indent(level);
114113
sb.append("Documentation: ");
115114
add(module.getDoc());
@@ -124,10 +123,10 @@ private void addInfoPClosureFunctionRootNode(PClosureFunctionRootNode node) {
124123
sb.append("CelVars: ");
125124
add(node.getCellVars());
126125
newLine();
127-
addInfoPCloserRootNode(node);
126+
addInfoPClosureRootNode(node);
128127
}
129128

130-
private void addInfoPCloserRootNode(PClosureRootNode node) {
129+
private void addInfoPClosureRootNode(PClosureRootNode node) {
131130
indent(level);
132131
sb.append("FreeVars: ");
133132
add(node.getFreeVars());
@@ -144,7 +143,7 @@ private void addInfoPRootNode(PRootNode node) {
144143
}
145144

146145
private void addInfoRootNode(RootNode node) {
147-
addFrameDescriptor(node.getFrameDescriptor());
146+
addIdentifiers(PythonFrame.getIdentifiersAsString(node.getFrameDescriptor()));
148147
}
149148

150149
private void addFunctionDefinitionNode(FunctionDefinitionNode node) {
@@ -191,16 +190,16 @@ private void addFunctionDefinitionNode(FunctionDefinitionNode node) {
191190
}
192191
indent(level);
193192
sb.append("FreeVarSlots: ");
194-
add(node.getFreeVarDefinitionSlots());
193+
add(node.getFreeVarDefinitions());
195194
newLine();
196-
add(node.getExecutionCellSlots());
195+
add(node.getExecutionCellSlots(), node.getCallTarget().getRootNode().getFrameDescriptor());
197196
visit(node.getFunctionRoot());
198197
}
199198

200199
public boolean visit(GeneratorFunctionDefinitionNode node) {
201200
nodeHeader(node, node.getFunctionName());
202201
level++;
203-
addFrameDescriptor(node.getFrameDescriptor());
202+
addIdentifiers(PythonFrame.getIdentifiersAsString(node.getFrameDescriptor()));
204203
indent(level);
205204
GeneratorInfo generatorInfo = node.getGeneratorInfo();
206205
sb.append("Active Flags: ").append(generatorInfo.getNumOfActiveFlags());
@@ -231,7 +230,7 @@ public boolean visit(FunctionRootNode node) {
231230
sb.append("Name: ").append(node.getName());
232231
newLine();
233232
addInfoPClosureFunctionRootNode(node);
234-
add(node.getExecutionCellSlots());
233+
add(node.getExecutionCellSlots(), node.getFrameDescriptor());
235234
level--;
236235
return true;
237236
}
@@ -350,12 +349,12 @@ public boolean visit(GeneratorExpressionNode node) {
350349
indent(level);
351350
sb.append("Name: ").append(node.getName());
352351
newLine();
353-
addFrameDescriptor(node.getFrameDescriptor());
352+
addIdentifiers(PythonFrame.getIdentifiersAsString(node.getFrameDescriptor()));
354353
indent(level);
355354
sb.append("Enclosing");
356355
newLine();
357356
level++;
358-
addFrameDescriptor(node.getEnclosingFrameDescriptor());
357+
addIdentifiers(node.getEnclosingFrameDescriptor());
359358
level--;
360359
indent(level);
361360
GeneratorInfo generatorInfo = node.getGeneratorInfo();
@@ -485,10 +484,9 @@ private void addSignature(Signature signature) {
485484
level--;
486485
}
487486

488-
private void addFrameDescriptor(FrameDescriptor fd) {
487+
private void addIdentifiers(Collection<? extends Object> identifiers) {
489488
indent(level);
490489
sb.append("FrameDescriptor: ");
491-
Set<Object> identifiers = fd.getIdentifiers();
492490
if (identifiers.isEmpty()) {
493491
sb.append(" Empty");
494492
newLine();
@@ -551,53 +549,39 @@ private void add(Collection<String> array) {
551549
}
552550
}
553551

554-
private void add(FrameSlot[] slots) {
555-
if (slots == null || slots.length == 0) {
556-
sb.append("None");
557-
} else {
558-
boolean first = true;
559-
for (FrameSlot slot : slots) {
560-
if (!first) {
561-
sb.append(", ");
562-
} else {
563-
first = false;
564-
}
565-
sb.append(slot.getIdentifier());
566-
}
567-
}
568-
}
569-
570-
private void add(FrameSlot slot) {
552+
private void add(FrameSlotNode slot) {
553+
FrameDescriptor descriptor = ((Node) slot).getRootNode().getFrameDescriptor();
554+
String full = "[" + slot.getSlotIndex() + "," + slot.getSlotIdentifier(descriptor) + "," + slot.getSlotKind(descriptor) + "]";
571555
if (printTmpSlots) {
572-
sb.append(slot.toString());
556+
sb.append(full);
573557
} else {
574-
Object identifier = slot.getIdentifier();
558+
Object identifier = slot.getSlotIdentifier(descriptor);
575559
if (identifier instanceof String) {
576-
sb.append(slot.toString());
560+
sb.append(full);
577561
} else if (identifier == FrameSlotIDs.RETURN_SLOT_ID) {
578562
sb.append("<return_val>");
579563
} else if (identifier == FrameSlotIDs.FREEVAR__CLASS__) {
580564
sb.append("<>freevar__class__");
581565
} else if (identifier.toString().startsWith("<>temp")) {
582566
sb.append("<>temp");
583567
} else {
584-
sb.append(slot.toString());
568+
sb.append(full);
585569
}
586570
}
587571
}
588572

589-
private void add(ExecutionCellSlots executionCellSlots) {
573+
private void add(ExecutionCellSlots executionCellSlots, FrameDescriptor descriptor) {
590574
indent(level);
591575
sb.append("ExecutionSlots:");
592576
newLine();
593577
level++;
594578
indent(level);
595579
sb.append("FreeVarsSlots: ");
596-
add(executionCellSlots.getFreeVarSlots());
580+
add(PythonFrame.extractSlotNames(descriptor, executionCellSlots.getFreeVarSlots()));
597581
newLine();
598582
indent(level);
599583
sb.append("CellVarsSlots: ");
600-
add(executionCellSlots.getCellVarSlots());
584+
add(PythonFrame.extractSlotNames(descriptor, executionCellSlots.getCellVarSlots()));
601585
newLine();
602586
level--;
603587
}
@@ -708,7 +692,7 @@ public boolean visit(Node node) {
708692
if (node instanceof FrameSlotNode) {
709693
indent(level);
710694
sb.append("Frame: ");
711-
add(((FrameSlotNode) node).getSlot());
695+
add((FrameSlotNode) node);
712696
newLine();
713697
}
714698
if (node instanceof WriteGlobalNode) {

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/parser/SSTSerializationTests.java

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import java.io.FileFilter;
4949
import java.io.OutputStream;
5050
import java.util.Arrays;
51+
import java.util.Collection;
5152
import java.util.HashSet;
5253
import java.util.List;
5354
import java.util.Set;
@@ -62,7 +63,6 @@
6263
import com.oracle.graal.python.runtime.PythonCodeSerializer;
6364
import com.oracle.graal.python.runtime.PythonParser;
6465
import com.oracle.truffle.api.TruffleFile;
65-
import com.oracle.truffle.api.frame.FrameSlot;
6666
import com.oracle.truffle.api.nodes.RootNode;
6767
import com.oracle.truffle.api.source.Source;
6868

@@ -215,6 +215,14 @@ public void classDecoratorTest() throws Exception {
215215
"class foo():pass");
216216
}
217217

218+
@Test
219+
public void classSuperTest() throws Exception {
220+
checkSerialization(
221+
"class Foo():\n" +
222+
" def bar():\n" +
223+
" super()");
224+
}
225+
218226
@Test
219227
public void classDefTest() throws Exception {
220228
checkSerialization("class foo():pass");
@@ -1264,6 +1272,7 @@ public void checkSerialization(Source source) throws Exception {
12641272

12651273
// at first parse the source and obtain the parse result
12661274
RootNode parserResult = (RootNode) parse(source, PythonParser.ParserMode.File);
1275+
parserResult.getCallTarget(); // set parent pointers
12671276
ScopeInfo parserScope = getLastGlobalScope();
12681277
checkScopeSerialization(parserScope);
12691278
Assert.assertNotNull("Parser result is null", parserResult);
@@ -1272,6 +1281,7 @@ public void checkSerialization(Source source) throws Exception {
12721281
Assert.assertNotNull("Serialized data are null", serializeResult);
12731282
// and get the tree from serialized data
12741283
RootNode deserialize = serializer.deserialize(context, serializeResult);
1284+
deserialize.getCallTarget(); // set parent pointers
12751285

12761286
Assert.assertNotNull("Deserialized result is null", parserResult);
12771287
// compare the tree from parser with the tree from serializer
@@ -1330,13 +1340,13 @@ private static void indent(StringBuilder sb, int indent) {
13301340
}
13311341
}
13321342

1333-
private static void printSet(StringBuilder sb, Set<String> set) {
1343+
private static void print(StringBuilder sb, Collection<String> set) {
13341344
if (set == null || set.isEmpty()) {
13351345
sb.append("Empty");
13361346
} else {
13371347
sb.append("[");
13381348
boolean first = true;
1339-
for (String name : set) {
1349+
for (String name : new TreeSet<>(set)) {
13401350
if (first) {
13411351
sb.append(name);
13421352
first = false;
@@ -1348,23 +1358,6 @@ private static void printSet(StringBuilder sb, Set<String> set) {
13481358
}
13491359
}
13501360

1351-
private static void printFrameSlots(StringBuilder sb, FrameSlot[] slots) {
1352-
if (slots.length == 0) {
1353-
sb.append("Empty");
1354-
} else {
1355-
sb.append("[");
1356-
boolean first = true;
1357-
for (FrameSlot slot : slots) {
1358-
if (first) {
1359-
sb.append(slot.getIdentifier());
1360-
first = false;
1361-
} else {
1362-
sb.append(", ").append(slot.getIdentifier());
1363-
}
1364-
}
1365-
}
1366-
}
1367-
13681361
// here we can not use the ScopeInfo.debugPrint(), because we need exclude the temporary
13691362
// variables.
13701363
private static void printScope(ScopeInfo scope, StringBuilder sb, int indent) {
@@ -1373,24 +1366,22 @@ private static void printScope(ScopeInfo scope, StringBuilder sb, int indent) {
13731366
indent(sb, indent + 1);
13741367
sb.append("Kind: ").append(scope.getScopeKind()).append("\n");
13751368
Set<String> names = new HashSet<>();
1376-
scope.getFrameDescriptor().getIdentifiers().forEach((id) -> {
1377-
if (id instanceof String) {
1378-
names.add(id.toString());
1379-
} else if (id == FrameSlotIDs.FREEVAR__CLASS__ || id == FrameSlotIDs.RETURN_SLOT_ID) {
1369+
for (Object id : scope.getFrameIdentifiers()) { // sorted
1370+
if (id instanceof String || id == FrameSlotIDs.FREEVAR__CLASS__ || id == FrameSlotIDs.RETURN_SLOT_ID) {
13801371
names.add(id.toString());
13811372
}
1382-
});
1373+
}
13831374
indent(sb, indent + 1);
13841375
sb.append("FrameDescriptor: ");
1385-
printSet(sb, new TreeSet<>(names));
1376+
print(sb, names);
13861377
sb.append("\n");
13871378
indent(sb, indent + 1);
13881379
sb.append("CellVars: ");
1389-
printFrameSlots(sb, scope.getCellVarSlots());
1380+
print(sb, scope.getCellVars());
13901381
sb.append("\n");
13911382
indent(sb, indent + 1);
13921383
sb.append("FreeVars: ");
1393-
printFrameSlots(sb, scope.getFreeVarSlots());
1384+
print(sb, scope.getFreeVars());
13941385
sb.append("\n");
13951386
ScopeInfo child = scope.getFirstChildScope();
13961387
while (child != null) {

graalpython/com.oracle.graal.python.test/testData/goldenFiles/AwaitAndAsyncTests/asyncFor01.scope

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ Scope: []
55
FreeVars: Empty
66
Scope: f
77
Kind: Function
8-
FrameDescriptor: [<return_val>, i]
8+
FrameDescriptor: [<>temp1, <return_val>, i]
99
CellVars: Empty
1010
FreeVars: Empty

0 commit comments

Comments
 (0)