76
76
import com .oracle .truffle .api .source .SourceSection ;
77
77
78
78
public final class PCode extends PythonBuiltinObject {
79
- private final long FLAG_POS_GENERATOR = 5 ;
80
- private final long FLAG_POS_VAR_ARGS = 2 ;
81
- private final long FLAG_POS_VAR_KW_ARGS = 3 ;
79
+ private static final String [] EMPTY_STRINGS = new String [0 ];
80
+ private final static long FLAG_POS_GENERATOR = 5 ;
81
+ private final static long FLAG_POS_VAR_ARGS = 2 ;
82
+ private final static long FLAG_POS_VAR_KW_ARGS = 3 ;
82
83
83
84
private final RootCallTarget callTarget ;
84
85
private final Arity arity ;
@@ -149,8 +150,8 @@ public PCode(LazyPythonClass cls, int argcount, int kwonlyargcount,
149
150
// Derive a new call target from the code string, if we can
150
151
FrameDescriptor frameDescriptor = new FrameDescriptor ();
151
152
MaterializedFrame frame = Truffle .getRuntime ().createMaterializedFrame (new Object [0 ], frameDescriptor );
152
- for (int i = 0 ; i < cellvars .length ; i ++) {
153
- Object ident = cellvars [i ];
153
+ for (int i = 0 ; i < freevars .length ; i ++) {
154
+ Object ident = freevars [i ];
154
155
FrameSlot slot = frameDescriptor .addFrameSlot (ident );
155
156
frameDescriptor .setFrameSlotKind (slot , FrameSlotKind .Object );
156
157
frame .setObject (slot , new PCell ());
@@ -185,8 +186,8 @@ public PCode(LazyPythonClass cls, int argcount, int kwonlyargcount,
185
186
}
186
187
187
188
@ TruffleBoundary
188
- private static Set <String > asSet (String [] values ) {
189
- return (values != null ) ? new HashSet <>(Arrays .asList (values )) : new HashSet <>();
189
+ private static Set <Object > asSet (Object [] objects ) {
190
+ return (objects != null ) ? new HashSet <>(Arrays .asList (objects )) : new HashSet <>();
190
191
}
191
192
192
193
private static String [] extractFreeVars (RootNode rootNode ) {
@@ -197,7 +198,7 @@ private static String[] extractFreeVars(RootNode rootNode) {
197
198
} else if (rootNode instanceof ModuleRootNode ) {
198
199
return ((ModuleRootNode ) rootNode ).getFreeVars ();
199
200
} else {
200
- return null ;
201
+ return EMPTY_STRINGS ;
201
202
}
202
203
}
203
204
@@ -206,10 +207,8 @@ private static String[] extractCellVars(RootNode rootNode) {
206
207
return ((FunctionRootNode ) rootNode ).getCellVars ();
207
208
} else if (rootNode instanceof GeneratorFunctionRootNode ) {
208
209
return ((GeneratorFunctionRootNode ) rootNode ).getCellVars ();
209
- } else if (rootNode instanceof ModuleRootNode ) {
210
- return new String [0 ];
211
210
} else {
212
- return null ;
211
+ return EMPTY_STRINGS ;
213
212
}
214
213
}
215
214
@@ -221,7 +220,7 @@ private static String extractFileName(RootNode rootNode) {
221
220
} else if (funcRootNode instanceof ModuleRootNode ) {
222
221
return funcRootNode .getName ();
223
222
} else {
224
- return null ;
223
+ return "<unknown source>" ;
225
224
}
226
225
}
227
226
@@ -272,34 +271,15 @@ private static int extractStackSize(RootNode rootNode) {
272
271
}
273
272
274
273
@ TruffleBoundary
275
- private void extractArgStats () {
276
- // 0x20 - generator
277
- this .flags = 0 ;
278
- RootNode funcRootNode = getRootNode ();
279
- if (funcRootNode instanceof GeneratorFunctionRootNode ) {
280
- flags |= (1 << FLAG_POS_GENERATOR );
281
- funcRootNode = ((GeneratorFunctionRootNode ) funcRootNode ).getFunctionRootNode ();
282
- }
283
-
284
- // 0x04 - *arguments
285
- if (NodeUtil .findAllNodeInstances (funcRootNode , ReadVarArgsNode .class ).size () == 1 ) {
286
- flags |= (1 << FLAG_POS_VAR_ARGS );
287
- }
288
- // 0x08 - **keywords
289
- if (NodeUtil .findAllNodeInstances (funcRootNode , ReadVarKeywordsNode .class ).size () == 1 ) {
290
- flags |= (1 << FLAG_POS_VAR_KW_ARGS );
291
- }
292
-
293
- this .freevars = extractFreeVars (getRootNode ());
294
- this .cellvars = extractCellVars (getRootNode ());
295
- Set <String > freeVarsSet = asSet ((String []) freevars );
296
- Set <String > cellVarsSet = asSet ((String []) cellvars );
274
+ private static Object [] extractVarnames (RootNode rootNode , String [] parameterIds , String [] keywordNames , Object [] freeVars , Object [] cellVars ) {
275
+ Set <Object > freeVarsSet = asSet (freeVars );
276
+ Set <Object > cellVarsSet = asSet (cellVars );
297
277
298
278
ArrayList <String > varNameList = new ArrayList <>(); // must be ordered!
299
- varNameList .addAll (Arrays .asList (arity . getParameterIds () ));
300
- varNameList .addAll (Arrays .asList (arity . getKeywordNames () ));
279
+ varNameList .addAll (Arrays .asList (parameterIds ));
280
+ varNameList .addAll (Arrays .asList (keywordNames ));
301
281
302
- for (Object identifier : getRootNode () .getFrameDescriptor ().getIdentifiers ()) {
282
+ for (Object identifier : rootNode .getFrameDescriptor ().getIdentifiers ()) {
303
283
if (identifier instanceof String ) {
304
284
String varName = (String ) identifier ;
305
285
@@ -315,8 +295,29 @@ private void extractArgStats() {
315
295
}
316
296
}
317
297
318
- this .varnames = varNameList .toArray ();
319
- this .nlocals = varNameList .size ();
298
+ return varNameList .toArray ();
299
+ }
300
+
301
+ @ TruffleBoundary
302
+ private static int extractFlags (RootNode rootNode ) {
303
+ // 0x20 - generator
304
+ int flags = 0 ;
305
+ RootNode funcRootNode = rootNode ;
306
+ if (funcRootNode instanceof GeneratorFunctionRootNode ) {
307
+ flags |= (1 << FLAG_POS_GENERATOR );
308
+ funcRootNode = ((GeneratorFunctionRootNode ) funcRootNode ).getFunctionRootNode ();
309
+ }
310
+
311
+ // 0x04 - *arguments
312
+ if (NodeUtil .findFirstNodeInstance (funcRootNode , ReadVarArgsNode .class ) != null ) {
313
+ flags |= (1 << FLAG_POS_VAR_ARGS );
314
+ }
315
+ // 0x08 - **keywords
316
+ if (NodeUtil .findFirstNodeInstance (funcRootNode , ReadVarKeywordsNode .class ) != null ) {
317
+ flags |= (1 << FLAG_POS_VAR_KW_ARGS );
318
+ }
319
+
320
+ return flags ;
320
321
}
321
322
322
323
@ TruffleBoundary
@@ -338,14 +339,14 @@ public RootNode getRootNode() {
338
339
339
340
public Object [] getFreeVars () {
340
341
if (freevars == null ) {
341
- extractArgStats ( );
342
+ freevars = extractFreeVars ( getRootNode () );
342
343
}
343
344
return freevars ;
344
345
}
345
346
346
347
public Object [] getCellVars () {
347
- if (freevars == null ) {
348
- extractArgStats ( );
348
+ if (cellvars == null ) {
349
+ cellvars = extractCellVars ( getRootNode () );
349
350
}
350
351
return cellvars ;
351
352
}
@@ -385,7 +386,7 @@ public int getKwonlyargcount() {
385
386
386
387
public int getNlocals () {
387
388
if (nlocals == -1 ) {
388
- extractArgStats () ;
389
+ nlocals = getVarnames (). length ;
389
390
}
390
391
return nlocals ;
391
392
}
@@ -399,14 +400,14 @@ public int getStacksize() {
399
400
400
401
public int getFlags () {
401
402
if (flags == -1 ) {
402
- extractArgStats ( );
403
+ flags = extractFlags ( getRootNode () );
403
404
}
404
405
return flags ;
405
406
}
406
407
407
408
public Object [] getVarnames () {
408
409
if (varnames == null ) {
409
- extractArgStats ( );
410
+ varnames = extractVarnames ( getRootNode (), getArity (). getParameterIds (), getArity (). getKeywordNames (), getFreeVars (), getCellVars () );
410
411
}
411
412
return varnames ;
412
413
}
0 commit comments