Skip to content

Commit ad2dce5

Browse files
timfelcosminbasca
authored andcommitted
properly implement co_names and cleanup PCode
1 parent fd4e38f commit ad2dce5

File tree

6 files changed

+27
-95
lines changed

6 files changed

+27
-95
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/GraalPythonModuleBuiltins.java

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -40,55 +40,18 @@
4040
*/
4141
package com.oracle.graal.python.builtins.modules;
4242

43-
import java.util.Arrays;
43+
import java.util.ArrayList;
4444
import java.util.List;
4545

46-
import com.oracle.graal.python.builtins.Builtin;
4746
import com.oracle.graal.python.builtins.CoreFunctions;
4847
import com.oracle.graal.python.builtins.PythonBuiltins;
49-
import com.oracle.graal.python.builtins.modules.BuiltinFunctions.GlobalsNode;
50-
import com.oracle.graal.python.builtins.objects.code.PCode;
51-
import com.oracle.graal.python.nodes.SpecialMethodNames;
52-
import com.oracle.graal.python.nodes.call.special.LookupAndCallBinaryNode;
53-
import com.oracle.graal.python.nodes.expression.CoerceToBooleanNode;
5448
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
55-
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
56-
import com.oracle.truffle.api.dsl.Cached;
57-
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
58-
import com.oracle.truffle.api.dsl.ImportStatic;
5949
import com.oracle.truffle.api.dsl.NodeFactory;
60-
import com.oracle.truffle.api.dsl.Specialization;
61-
import com.oracle.truffle.api.frame.VirtualFrame;
6250

6351
@CoreFunctions(defineModule = "__graalpython__")
6452
public class GraalPythonModuleBuiltins extends PythonBuiltins {
6553
@Override
6654
protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFactories() {
67-
return GraalPythonModuleBuiltinsFactory.getFactories();
68-
}
69-
70-
@Builtin(name = "current_global_code_variables", minNumOfPositionalArgs = 1, maxNumOfPositionalArgs = 1)
71-
@GenerateNodeFactory
72-
@ImportStatic(SpecialMethodNames.class)
73-
abstract static class GetCodeGlobalVarsNode extends PythonUnaryBuiltinNode {
74-
@Specialization
75-
protected Object get(VirtualFrame frame, PCode code,
76-
@Cached GlobalsNode globalsNode,
77-
@Cached("createIfTrueNode()") CoerceToBooleanNode isTrue,
78-
@Cached("create(__CONTAINS__)") LookupAndCallBinaryNode containsNode) {
79-
Object[] varNames = code.getGlobalAndBuiltinVarNames();
80-
if (varNames != null) {
81-
int i = 0;
82-
Object[] globalNames = new Object[varNames.length];
83-
Object globals = globalsNode.execute(frame);
84-
for (Object name : varNames) {
85-
if (isTrue.executeBoolean(frame, containsNode.executeObject(frame, globals, name))) {
86-
globalNames[i++] = name;
87-
}
88-
}
89-
return factory().createTuple(Arrays.copyOf(globalNames, i));
90-
}
91-
return factory().createEmptyTuple();
92-
}
55+
return new ArrayList<>();
9356
}
9457
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/code/CodeBuiltins.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,6 @@ public abstract static class GetConstsNode extends PythonBuiltinNode {
184184
protected Object get(PCode self) {
185185
Object[] constants = self.getConstants();
186186
if (constants == null) {
187-
// TODO: this is for the moment undefined (see co_code)
188187
constants = new Object[0];
189188
}
190189
return factory().createTuple(constants);
@@ -198,7 +197,6 @@ public abstract static class GetNamesNode extends PythonBuiltinNode {
198197
protected Object get(PCode self) {
199198
Object[] names = self.getNames();
200199
if (names == null) {
201-
// TODO: this is for the moment undefined (see co_code)
202200
names = new Object[0];
203201
}
204202
return factory().createTuple(names);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/code/PCode.java

Lines changed: 19 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -43,23 +43,20 @@
4343
import java.util.ArrayList;
4444
import java.util.Arrays;
4545
import java.util.HashSet;
46-
import java.util.List;
4746
import java.util.Set;
4847

4948
import com.oracle.graal.python.PythonLanguage;
5049
import com.oracle.graal.python.builtins.objects.function.Signature;
5150
import com.oracle.graal.python.builtins.objects.object.PythonBuiltinObject;
5251
import com.oracle.graal.python.builtins.objects.type.LazyPythonClass;
5352
import com.oracle.graal.python.nodes.ModuleRootNode;
53+
import com.oracle.graal.python.nodes.PClosureFunctionRootNode;
54+
import com.oracle.graal.python.nodes.PClosureRootNode;
5455
import com.oracle.graal.python.nodes.PRootNode;
5556
import com.oracle.graal.python.nodes.argument.ReadVarArgsNode;
5657
import com.oracle.graal.python.nodes.argument.ReadVarKeywordsNode;
57-
import com.oracle.graal.python.nodes.frame.DeleteGlobalNode;
5858
import com.oracle.graal.python.nodes.frame.FrameSlotIDs;
5959
import com.oracle.graal.python.nodes.frame.GlobalNode;
60-
import com.oracle.graal.python.nodes.frame.ReadGlobalOrBuiltinNode;
61-
import com.oracle.graal.python.nodes.frame.WriteGlobalNode;
62-
import com.oracle.graal.python.nodes.function.FunctionRootNode;
6360
import com.oracle.graal.python.nodes.generator.GeneratorFunctionRootNode;
6461
import com.oracle.graal.python.nodes.literal.SimpleLiteralNode;
6562
import com.oracle.truffle.api.CompilerDirectives;
@@ -96,7 +93,7 @@ public final class PCode extends PythonBuiltinObject {
9693
private byte[] codestring;
9794
// tuple of constants used in the bytecode
9895
private Object[] constants;
99-
// tuple containing the literals used by the bytecode
96+
// tuple containing the literals (builtins/globals) used by the bytecode
10097
private Object[] names;
10198
// is a tuple containing the names of the local variables (starting with the argument names)
10299
private Object[] varnames;
@@ -112,8 +109,6 @@ public final class PCode extends PythonBuiltinObject {
112109
private Object[] freevars;
113110
// tuple of names of cell variables (referenced by containing scopes)
114111
private Object[] cellvars;
115-
// is a tuple containing the names of the global variables accessed from this code object
116-
private Object[] globalAndBuiltinVarNames;
117112

118113
public PCode(LazyPythonClass cls, RootCallTarget callTarget) {
119114
super(cls);
@@ -155,29 +150,23 @@ private static Set<Object> asSet(Object[] objects) {
155150
}
156151

157152
private static String[] extractFreeVars(RootNode rootNode) {
158-
if (rootNode instanceof FunctionRootNode) {
159-
return ((FunctionRootNode) rootNode).getFreeVars();
160-
} else if (rootNode instanceof GeneratorFunctionRootNode) {
161-
return ((GeneratorFunctionRootNode) rootNode).getFreeVars();
162-
} else if (rootNode instanceof ModuleRootNode) {
163-
return ((ModuleRootNode) rootNode).getFreeVars();
153+
if (rootNode instanceof PClosureRootNode) {
154+
return ((PClosureRootNode) rootNode).getFreeVars();
164155
} else {
165156
return EMPTY_STRINGS;
166157
}
167158
}
168159

169160
private static String[] extractCellVars(RootNode rootNode) {
170-
if (rootNode instanceof FunctionRootNode) {
171-
return ((FunctionRootNode) rootNode).getCellVars();
172-
} else if (rootNode instanceof GeneratorFunctionRootNode) {
173-
return ((GeneratorFunctionRootNode) rootNode).getCellVars();
161+
if (rootNode instanceof PClosureFunctionRootNode) {
162+
return ((PClosureFunctionRootNode) rootNode).getCellVars();
174163
} else {
175164
return EMPTY_STRINGS;
176165
}
177166
}
178167

179168
private static String extractFileName(RootNode rootNode) {
180-
RootNode funcRootNode = (rootNode instanceof GeneratorFunctionRootNode) ? ((GeneratorFunctionRootNode) rootNode).getFunctionRootNode() : rootNode;
169+
RootNode funcRootNode = rootNodeForExtraction(rootNode);
181170
SourceSection src;
182171
if (funcRootNode instanceof PRootNode) {
183172
src = ((PRootNode) funcRootNode).getSourceSection();
@@ -200,7 +189,7 @@ private static String extractFileName(RootNode rootNode) {
200189

201190
@TruffleBoundary
202191
private static int extractFirstLineno(RootNode rootNode) {
203-
RootNode funcRootNode = (rootNode instanceof GeneratorFunctionRootNode) ? ((GeneratorFunctionRootNode) rootNode).getFunctionRootNode() : rootNode;
192+
RootNode funcRootNode = rootNodeForExtraction(rootNode);
204193
SourceSection sourceSection = funcRootNode.getSourceSection();
205194
if (sourceSection != null) {
206195
return sourceSection.getStartLine();
@@ -209,15 +198,7 @@ private static int extractFirstLineno(RootNode rootNode) {
209198
}
210199

211200
private static String extractName(RootNode rootNode) {
212-
String name;
213-
if (rootNode instanceof ModuleRootNode) {
214-
name = rootNode.getName();
215-
} else if (rootNode instanceof FunctionRootNode) {
216-
name = ((FunctionRootNode) rootNode).getName();
217-
} else {
218-
name = rootNode.getName();
219-
}
220-
return name;
201+
return rootNode.getName();
221202
}
222203

223204
private static int extractStackSize(RootNode rootNode) {
@@ -254,26 +235,16 @@ private static Object[] extractVarnames(RootNode rootNode, String[] parameterIds
254235

255236
@TruffleBoundary
256237
private static Object[] extractConstants(RootNode rootNode) {
257-
return NodeUtil.findAllNodeInstances(rootNode, SimpleLiteralNode.class).stream().map((n) -> n.getValue()).toArray();
238+
return NodeUtil.findAllNodeInstances(rootNodeForExtraction(rootNode), SimpleLiteralNode.class).stream().map((n) -> n.getValue()).toArray();
258239
}
259240

260241
@TruffleBoundary
261-
private static Object[] extractGlobalAndBuiltinVarnames(RootNode rootNode) {
262-
RootNode funcRootNode = (rootNode instanceof GeneratorFunctionRootNode) ? ((GeneratorFunctionRootNode) rootNode).getFunctionRootNode() : rootNode;
263-
Set<Object> varNameList = new HashSet<>();
264-
265-
List<GlobalNode> globalNodes = NodeUtil.findAllNodeInstances(funcRootNode, GlobalNode.class);
266-
for (GlobalNode node : globalNodes) {
267-
if (node instanceof ReadGlobalOrBuiltinNode) {
268-
varNameList.add(((ReadGlobalOrBuiltinNode) node).getAttributeId());
269-
} else if (node instanceof WriteGlobalNode) {
270-
varNameList.add(((WriteGlobalNode) node).getAttributeId());
271-
} else if (node instanceof DeleteGlobalNode) {
272-
varNameList.add(((DeleteGlobalNode) node).getAttributeId());
273-
}
274-
}
242+
private static Object[] extractNames(RootNode rootNode) {
243+
return NodeUtil.findAllNodeInstances(rootNodeForExtraction(rootNode), GlobalNode.class).stream().map((n) -> n.getAttributeId()).distinct().toArray();
244+
}
275245

276-
return varNameList.toArray();
246+
private static RootNode rootNodeForExtraction(RootNode rootNode) {
247+
return (rootNode instanceof GeneratorFunctionRootNode) ? ((GeneratorFunctionRootNode) rootNode).getFunctionRootNode() : rootNode;
277248
}
278249

279250
@TruffleBoundary
@@ -405,13 +376,6 @@ public byte[] getCodestring() {
405376
return codestring;
406377
}
407378

408-
public Object[] getGlobalAndBuiltinVarNames() {
409-
if (globalAndBuiltinVarNames == null) {
410-
this.globalAndBuiltinVarNames = extractGlobalAndBuiltinVarnames(getRootNode());
411-
}
412-
return globalAndBuiltinVarNames;
413-
}
414-
415379
public Object[] getConstants() {
416380
if (constants == null) {
417381
constants = extractConstants(getRootNode());
@@ -420,6 +384,9 @@ public Object[] getConstants() {
420384
}
421385

422386
public Object[] getNames() {
387+
if (names == null) {
388+
names = extractNames(getRootNode());
389+
}
423390
return names;
424391
}
425392

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ Object deleteModule(VirtualFrame frame,
9595
return PNone.NONE;
9696
}
9797

98-
public Object getAttributeId() {
98+
@Override
99+
public String getAttributeId() {
99100
return attributeId;
100101
}
101102
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,6 @@ default boolean isBuiltinDict(Object globals, IsBuiltinClassProfile profile) {
6666
default boolean isDict(Object globals) {
6767
return globals instanceof PDict;
6868
}
69+
70+
public String getAttributeId();
6971
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,8 @@ void writeModule(VirtualFrame frame, Object value,
209209
storeNode.executeVoid(frame, PArguments.getGlobals(frame), value);
210210
}
211211

212-
public Object getAttributeId() {
212+
@Override
213+
public String getAttributeId() {
213214
return attributeId;
214215
}
215216
}

0 commit comments

Comments
 (0)