43
43
import java .util .ArrayList ;
44
44
import java .util .Arrays ;
45
45
import java .util .HashSet ;
46
- import java .util .List ;
47
46
import java .util .Set ;
48
47
49
48
import com .oracle .graal .python .PythonLanguage ;
50
49
import com .oracle .graal .python .builtins .objects .function .Signature ;
51
50
import com .oracle .graal .python .builtins .objects .object .PythonBuiltinObject ;
52
51
import com .oracle .graal .python .builtins .objects .type .LazyPythonClass ;
53
52
import com .oracle .graal .python .nodes .ModuleRootNode ;
53
+ import com .oracle .graal .python .nodes .PClosureFunctionRootNode ;
54
+ import com .oracle .graal .python .nodes .PClosureRootNode ;
54
55
import com .oracle .graal .python .nodes .PRootNode ;
55
56
import com .oracle .graal .python .nodes .argument .ReadVarArgsNode ;
56
57
import com .oracle .graal .python .nodes .argument .ReadVarKeywordsNode ;
57
- import com .oracle .graal .python .nodes .frame .DeleteGlobalNode ;
58
58
import com .oracle .graal .python .nodes .frame .FrameSlotIDs ;
59
59
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 ;
63
60
import com .oracle .graal .python .nodes .generator .GeneratorFunctionRootNode ;
64
61
import com .oracle .graal .python .nodes .literal .SimpleLiteralNode ;
65
62
import com .oracle .truffle .api .CompilerDirectives ;
@@ -96,7 +93,7 @@ public final class PCode extends PythonBuiltinObject {
96
93
private byte [] codestring ;
97
94
// tuple of constants used in the bytecode
98
95
private Object [] constants ;
99
- // tuple containing the literals used by the bytecode
96
+ // tuple containing the literals (builtins/globals) used by the bytecode
100
97
private Object [] names ;
101
98
// is a tuple containing the names of the local variables (starting with the argument names)
102
99
private Object [] varnames ;
@@ -112,8 +109,6 @@ public final class PCode extends PythonBuiltinObject {
112
109
private Object [] freevars ;
113
110
// tuple of names of cell variables (referenced by containing scopes)
114
111
private Object [] cellvars ;
115
- // is a tuple containing the names of the global variables accessed from this code object
116
- private Object [] globalAndBuiltinVarNames ;
117
112
118
113
public PCode (LazyPythonClass cls , RootCallTarget callTarget ) {
119
114
super (cls );
@@ -155,29 +150,23 @@ private static Set<Object> asSet(Object[] objects) {
155
150
}
156
151
157
152
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 ();
164
155
} else {
165
156
return EMPTY_STRINGS ;
166
157
}
167
158
}
168
159
169
160
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 ();
174
163
} else {
175
164
return EMPTY_STRINGS ;
176
165
}
177
166
}
178
167
179
168
private static String extractFileName (RootNode rootNode ) {
180
- RootNode funcRootNode = (rootNode instanceof GeneratorFunctionRootNode ) ? (( GeneratorFunctionRootNode ) rootNode ). getFunctionRootNode () : rootNode ;
169
+ RootNode funcRootNode = rootNodeForExtraction (rootNode ) ;
181
170
SourceSection src ;
182
171
if (funcRootNode instanceof PRootNode ) {
183
172
src = ((PRootNode ) funcRootNode ).getSourceSection ();
@@ -200,7 +189,7 @@ private static String extractFileName(RootNode rootNode) {
200
189
201
190
@ TruffleBoundary
202
191
private static int extractFirstLineno (RootNode rootNode ) {
203
- RootNode funcRootNode = (rootNode instanceof GeneratorFunctionRootNode ) ? (( GeneratorFunctionRootNode ) rootNode ). getFunctionRootNode () : rootNode ;
192
+ RootNode funcRootNode = rootNodeForExtraction (rootNode ) ;
204
193
SourceSection sourceSection = funcRootNode .getSourceSection ();
205
194
if (sourceSection != null ) {
206
195
return sourceSection .getStartLine ();
@@ -209,15 +198,7 @@ private static int extractFirstLineno(RootNode rootNode) {
209
198
}
210
199
211
200
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 ();
221
202
}
222
203
223
204
private static int extractStackSize (RootNode rootNode ) {
@@ -254,26 +235,16 @@ private static Object[] extractVarnames(RootNode rootNode, String[] parameterIds
254
235
255
236
@ TruffleBoundary
256
237
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 ();
258
239
}
259
240
260
241
@ 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
+ }
275
245
276
- return varNameList .toArray ();
246
+ private static RootNode rootNodeForExtraction (RootNode rootNode ) {
247
+ return (rootNode instanceof GeneratorFunctionRootNode ) ? ((GeneratorFunctionRootNode ) rootNode ).getFunctionRootNode () : rootNode ;
277
248
}
278
249
279
250
@ TruffleBoundary
@@ -405,13 +376,6 @@ public byte[] getCodestring() {
405
376
return codestring ;
406
377
}
407
378
408
- public Object [] getGlobalAndBuiltinVarNames () {
409
- if (globalAndBuiltinVarNames == null ) {
410
- this .globalAndBuiltinVarNames = extractGlobalAndBuiltinVarnames (getRootNode ());
411
- }
412
- return globalAndBuiltinVarNames ;
413
- }
414
-
415
379
public Object [] getConstants () {
416
380
if (constants == null ) {
417
381
constants = extractConstants (getRootNode ());
@@ -420,6 +384,9 @@ public Object[] getConstants() {
420
384
}
421
385
422
386
public Object [] getNames () {
387
+ if (names == null ) {
388
+ names = extractNames (getRootNode ());
389
+ }
423
390
return names ;
424
391
}
425
392
0 commit comments