60
60
import com .oracle .truffle .api .CompilerDirectives .TruffleBoundary ;
61
61
import com .oracle .truffle .api .RootCallTarget ;
62
62
import com .oracle .truffle .api .Truffle ;
63
- import com .oracle .truffle .api .frame .FrameDescriptor ;
64
63
import com .oracle .truffle .api .nodes .Node ;
65
64
import com .oracle .truffle .api .nodes .NodeUtil ;
66
65
import com .oracle .truffle .api .nodes .RootNode ;
67
66
import com .oracle .truffle .api .source .SourceSection ;
68
67
69
- public class PCode extends PythonBuiltinObject {
68
+ public final class PCode extends PythonBuiltinObject {
70
69
private final long FLAG_POS_GENERATOR = 5 ;
71
70
private final long FLAG_POS_VAR_ARGS = 2 ;
72
71
private final long FLAG_POS_VAR_KW_ARGS = 3 ;
73
72
74
- private final RootNode rootNode ;
73
+ private final RootCallTarget callTarget ;
75
74
private final PythonCore core ;
76
75
77
76
// number of arguments (not including keyword only arguments, * or ** args)
@@ -111,12 +110,11 @@ public class PCode extends PythonBuiltinObject {
111
110
112
111
// internal cache for keyword names
113
112
private Arity .KeywordName [] keywordNames ;
114
- // internal cache for the FrameDescriptor
115
- private FrameDescriptor frameDescriptor ;
116
113
117
114
public PCode (LazyPythonClass cls , RootNode rootNode , PythonCore core ) {
118
115
super (cls );
119
- this .rootNode = rootNode ;
116
+ assert rootNode != null ;
117
+ this .callTarget = Truffle .getRuntime ().createCallTarget (rootNode );
120
118
this .core = core ;
121
119
}
122
120
@@ -127,7 +125,7 @@ public PCode(LazyPythonClass cls, int argcount, int kwonlyargcount,
127
125
String filename , String name , int firstlineno ,
128
126
byte [] lnotab ) {
129
127
super (cls );
130
- this .rootNode = null ;
128
+ this .callTarget = null ;
131
129
this .core = null ;
132
130
133
131
this .argcount = argcount ;
@@ -234,7 +232,7 @@ private static int extractStackSize(RootNode rootNode) {
234
232
private void extractArgStats () {
235
233
// 0x20 - generator
236
234
this .flags = 0 ;
237
- RootNode funcRootNode = rootNode ;
235
+ RootNode funcRootNode = getRootNode () ;
238
236
if (funcRootNode instanceof GeneratorFunctionRootNode ) {
239
237
flags |= (1 << FLAG_POS_GENERATOR );
240
238
funcRootNode = ((GeneratorFunctionRootNode ) funcRootNode ).getFunctionRootNode ();
@@ -249,8 +247,8 @@ private void extractArgStats() {
249
247
flags |= (1 << FLAG_POS_VAR_KW_ARGS );
250
248
}
251
249
252
- this .freevars = extractFreeVars (rootNode );
253
- this .cellvars = extractCellVars (rootNode );
250
+ this .freevars = extractFreeVars (getRootNode () );
251
+ this .cellvars = extractCellVars (getRootNode () );
254
252
Set <String > freeVarsSet = asSet ((String []) freevars );
255
253
Set <String > cellVarsSet = asSet ((String []) cellvars );
256
254
@@ -277,7 +275,7 @@ private void extractArgStats() {
277
275
}
278
276
279
277
Set <String > varnamesSet = new HashSet <>();
280
- for (Object identifier : rootNode .getFrameDescriptor ().getIdentifiers ()) {
278
+ for (Object identifier : getRootNode () .getFrameDescriptor ().getIdentifiers ()) {
281
279
if (identifier instanceof String ) {
282
280
String varName = (String ) identifier ;
283
281
@@ -296,18 +294,22 @@ private void extractArgStats() {
296
294
}
297
295
298
296
public RootNode getRootNode () {
299
- return rootNode ;
297
+ return callTarget == null ? null : callTarget .getRootNode ();
298
+ }
299
+
300
+ private boolean hasRootNode () {
301
+ return getRootNode () != null ;
300
302
}
301
303
302
304
public Object [] getFreeVars () {
303
- if (freevars == null && rootNode != null ) {
305
+ if (freevars == null && hasRootNode () ) {
304
306
extractArgStats ();
305
307
}
306
308
return freevars ;
307
309
}
308
310
309
311
public Object [] getCellVars () {
310
- if (freevars == null && rootNode != null ) {
312
+ if (freevars == null && hasRootNode () ) {
311
313
extractArgStats ();
312
314
}
313
315
return cellvars ;
@@ -318,63 +320,63 @@ public void setFilename(String filename) {
318
320
}
319
321
320
322
public String getFilename () {
321
- if (filename == null && rootNode != null ) {
322
- filename = extractFileName (rootNode );
323
+ if (filename == null && hasRootNode () ) {
324
+ filename = extractFileName (getRootNode () );
323
325
}
324
326
return filename ;
325
327
}
326
328
327
329
public int getFirstLineNo () {
328
- if (firstlineno == -1 && rootNode != null ) {
329
- firstlineno = extractFirstLineno (rootNode );
330
+ if (firstlineno == -1 && hasRootNode () ) {
331
+ firstlineno = extractFirstLineno (getRootNode () );
330
332
}
331
333
return firstlineno ;
332
334
}
333
335
334
336
public String getName () {
335
- if (name == null && rootNode != null ) {
336
- name = extractName (rootNode );
337
+ if (name == null && hasRootNode () ) {
338
+ name = extractName (getRootNode () );
337
339
}
338
340
return name ;
339
341
}
340
342
341
343
public int getArgcount () {
342
- if (argcount == -1 && rootNode != null ) {
344
+ if (argcount == -1 && hasRootNode () ) {
343
345
extractArgStats ();
344
346
}
345
347
return argcount ;
346
348
}
347
349
348
350
public int getKwonlyargcount () {
349
- if (kwonlyargcount == -1 && rootNode != null ) {
351
+ if (kwonlyargcount == -1 && hasRootNode () ) {
350
352
extractArgStats ();
351
353
}
352
354
return kwonlyargcount ;
353
355
}
354
356
355
357
public int getNlocals () {
356
- if (nlocals == -1 && rootNode != null ) {
358
+ if (nlocals == -1 && hasRootNode () ) {
357
359
extractArgStats ();
358
360
}
359
361
return nlocals ;
360
362
}
361
363
362
364
public int getStacksize () {
363
- if (stacksize == -1 && rootNode != null ) {
364
- stacksize = extractStackSize (rootNode );
365
+ if (stacksize == -1 && hasRootNode () ) {
366
+ stacksize = extractStackSize (getRootNode () );
365
367
}
366
368
return stacksize ;
367
369
}
368
370
369
371
public int getFlags () {
370
- if (flags == -1 && rootNode != null ) {
372
+ if (flags == -1 && hasRootNode () ) {
371
373
extractArgStats ();
372
374
}
373
375
return flags ;
374
376
}
375
377
376
378
public Object [] getVarnames () {
377
- if (varnames == null && rootNode != null ) {
379
+ if (varnames == null && hasRootNode () ) {
378
380
extractArgStats ();
379
381
}
380
382
return varnames ;
@@ -397,7 +399,7 @@ public byte[] getLnotab() {
397
399
}
398
400
399
401
private Arity .KeywordName [] getKeywordNames () {
400
- if (keywordNames == null && rootNode != null ) {
402
+ if (keywordNames == null && hasRootNode () ) {
401
403
extractArgStats ();
402
404
}
403
405
return keywordNames ;
@@ -431,31 +433,7 @@ public Arity getArity() {
431
433
return new Arity (this .getName (), this .getMinNumOfPositionalArgs (), this .getMaxNumOfPositionalArgs (), this .takesVarKeywordArgs (), this .takesVarArgs (), this .getKeywordNames ());
432
434
}
433
435
434
- @ TruffleBoundary
435
- private FrameDescriptor createFrameDescriptor () {
436
- FrameDescriptor fd = new FrameDescriptor ();
437
- for (Object identifier : varnames ) {
438
- fd .addFrameSlot (identifier );
439
- }
440
- return fd ;
441
- }
442
-
443
- public FrameDescriptor getFrameDescriptor () {
444
- if (frameDescriptor == null ) {
445
- if (rootNode != null ) {
446
- frameDescriptor = rootNode .getFrameDescriptor ();
447
- } else {
448
- frameDescriptor = createFrameDescriptor ();
449
- }
450
- }
451
- return frameDescriptor ;
452
- }
453
-
454
- @ TruffleBoundary
455
436
public RootCallTarget getRootCallTarget () {
456
- if (rootNode != null ) {
457
- return Truffle .getRuntime ().createCallTarget (rootNode );
458
- }
459
- return null ;
437
+ return callTarget ;
460
438
}
461
439
}
0 commit comments