Skip to content

Commit bfd6e6a

Browse files
committed
don't store frameDescriptor in PFunction objects, keep call target in PCode
1 parent 5253618 commit bfd6e6a

File tree

10 files changed

+69
-115
lines changed

10 files changed

+69
-115
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1521,22 +1521,22 @@ public PZip zip(PythonClass cls, Object[] args,
15211521
public abstract static class FunctionNode extends PythonBuiltinNode {
15221522
@Specialization
15231523
public PFunction function(PythonClass cls, PCode code, PDict globals, String name, @SuppressWarnings("unused") PNone defaultArgs, @SuppressWarnings("unused") PNone closure) {
1524-
return factory().createFunction(name, cls.getName(), code.getArity(), code.getRootCallTarget(), code.getFrameDescriptor(), globals, null);
1524+
return factory().createFunction(name, cls.getName(), code.getArity(), code.getRootCallTarget(), globals, null);
15251525
}
15261526

15271527
@Specialization
15281528
public PFunction function(PythonClass cls, PCode code, PDict globals, String name, @SuppressWarnings("unused") PNone defaultArgs, PTuple closure) {
1529-
return factory().createFunction(name, cls.getName(), code.getArity(), code.getRootCallTarget(), code.getFrameDescriptor(), globals, (PCell[]) closure.getArray());
1529+
return factory().createFunction(name, cls.getName(), code.getArity(), code.getRootCallTarget(), globals, (PCell[]) closure.getArray());
15301530
}
15311531

15321532
@Specialization
15331533
public PFunction function(PythonClass cls, PCode code, PDict globals, String name, PTuple defaultArgs, @SuppressWarnings("unused") PNone closure) {
1534-
return factory().createFunction(name, cls.getName(), code.getArity(), code.getRootCallTarget(), code.getFrameDescriptor(), globals, defaultArgs.getArray(), null);
1534+
return factory().createFunction(name, cls.getName(), code.getArity(), code.getRootCallTarget(), globals, defaultArgs.getArray(), null);
15351535
}
15361536

15371537
@Specialization
15381538
public PFunction function(PythonClass cls, PCode code, PDict globals, String name, PTuple defaultArgs, PTuple closure) {
1539-
return factory().createFunction(name, cls.getName(), code.getArity(), code.getRootCallTarget(), code.getFrameDescriptor(), globals, defaultArgs.getArray(), (PCell[]) closure.getArray());
1539+
return factory().createFunction(name, cls.getName(), code.getArity(), code.getRootCallTarget(), globals, defaultArgs.getArray(), (PCell[]) closure.getArray());
15401540
}
15411541

15421542
@Fallback

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -552,8 +552,7 @@ private byte[] getByteArray(PIBytesLike pByteArray) {
552552

553553
@TruffleBoundary
554554
private static Object evalExpression(PCode code, PythonObject globals, PythonObject locals, PCell[] closure) {
555-
RootNode root = code.getRootNode();
556-
return evalNode(root, globals, locals, closure);
555+
return evalNode(code.getRootCallTarget(), globals, locals, closure);
557556
}
558557

559558
@TruffleBoundary
@@ -567,20 +566,19 @@ private Object evalExpression(String expression, PythonObject globals, PythonObj
567566
}
568567
PythonParser parser = getCore().getParser();
569568
Source source = PythonLanguage.newSource(getContext(), expression, name);
570-
RootNode parsed = (RootNode) parser.parse(ParserMode.Eval, getCore(), source, callerFrame);
569+
RootCallTarget parsed = Truffle.getRuntime().createCallTarget((RootNode) parser.parse(ParserMode.Eval, getCore(), source, callerFrame));
571570
return evalNode(parsed, globals, locals, closure);
572571
}
573572

574573
/**
575574
* @param locals TODO: support the locals dictionary in execution
576575
*/
577576
@TruffleBoundary
578-
private static Object evalNode(RootNode root, PythonObject globals, PythonObject locals, PCell[] closure) {
577+
private static Object evalNode(RootCallTarget callTarget, PythonObject globals, PythonObject locals, PCell[] closure) {
579578
Object[] args = PArguments.create();
580579
PArguments.setGlobals(args, globals);
581580
PArguments.setClosure(args, closure);
582581
// TODO: cache code and CallTargets and use Direct/IndirectCallNode
583-
RootCallTarget callTarget = Truffle.getRuntime().createCallTarget(root);
584582
return callTarget.call(args);
585583
}
586584
}
@@ -1472,7 +1470,7 @@ public boolean visit(Node node) {
14721470

14731471
String name = func.getName();
14741472
builtinFunc = factory().createFunction(name, func.getEnclosingClassName(), arity.createWithSelf(name), Truffle.getRuntime().createCallTarget(func.getFunctionRootNode()),
1475-
func.getFrameDescriptor(), func.getGlobals(), func.getClosure());
1473+
func.getGlobals(), func.getClosure());
14761474
}
14771475

14781476
PythonObject globals = func.getGlobals();

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

Lines changed: 31 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,17 @@
6060
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
6161
import com.oracle.truffle.api.RootCallTarget;
6262
import com.oracle.truffle.api.Truffle;
63-
import com.oracle.truffle.api.frame.FrameDescriptor;
6463
import com.oracle.truffle.api.nodes.Node;
6564
import com.oracle.truffle.api.nodes.NodeUtil;
6665
import com.oracle.truffle.api.nodes.RootNode;
6766
import com.oracle.truffle.api.source.SourceSection;
6867

69-
public class PCode extends PythonBuiltinObject {
68+
public final class PCode extends PythonBuiltinObject {
7069
private final long FLAG_POS_GENERATOR = 5;
7170
private final long FLAG_POS_VAR_ARGS = 2;
7271
private final long FLAG_POS_VAR_KW_ARGS = 3;
7372

74-
private final RootNode rootNode;
73+
private final RootCallTarget callTarget;
7574
private final PythonCore core;
7675

7776
// number of arguments (not including keyword only arguments, * or ** args)
@@ -111,12 +110,11 @@ public class PCode extends PythonBuiltinObject {
111110

112111
// internal cache for keyword names
113112
private Arity.KeywordName[] keywordNames;
114-
// internal cache for the FrameDescriptor
115-
private FrameDescriptor frameDescriptor;
116113

117114
public PCode(LazyPythonClass cls, RootNode rootNode, PythonCore core) {
118115
super(cls);
119-
this.rootNode = rootNode;
116+
assert rootNode != null;
117+
this.callTarget = Truffle.getRuntime().createCallTarget(rootNode);
120118
this.core = core;
121119
}
122120

@@ -127,7 +125,7 @@ public PCode(LazyPythonClass cls, int argcount, int kwonlyargcount,
127125
String filename, String name, int firstlineno,
128126
byte[] lnotab) {
129127
super(cls);
130-
this.rootNode = null;
128+
this.callTarget = null;
131129
this.core = null;
132130

133131
this.argcount = argcount;
@@ -234,7 +232,7 @@ private static int extractStackSize(RootNode rootNode) {
234232
private void extractArgStats() {
235233
// 0x20 - generator
236234
this.flags = 0;
237-
RootNode funcRootNode = rootNode;
235+
RootNode funcRootNode = getRootNode();
238236
if (funcRootNode instanceof GeneratorFunctionRootNode) {
239237
flags |= (1 << FLAG_POS_GENERATOR);
240238
funcRootNode = ((GeneratorFunctionRootNode) funcRootNode).getFunctionRootNode();
@@ -249,8 +247,8 @@ private void extractArgStats() {
249247
flags |= (1 << FLAG_POS_VAR_KW_ARGS);
250248
}
251249

252-
this.freevars = extractFreeVars(rootNode);
253-
this.cellvars = extractCellVars(rootNode);
250+
this.freevars = extractFreeVars(getRootNode());
251+
this.cellvars = extractCellVars(getRootNode());
254252
Set<String> freeVarsSet = asSet((String[]) freevars);
255253
Set<String> cellVarsSet = asSet((String[]) cellvars);
256254

@@ -277,7 +275,7 @@ private void extractArgStats() {
277275
}
278276

279277
Set<String> varnamesSet = new HashSet<>();
280-
for (Object identifier : rootNode.getFrameDescriptor().getIdentifiers()) {
278+
for (Object identifier : getRootNode().getFrameDescriptor().getIdentifiers()) {
281279
if (identifier instanceof String) {
282280
String varName = (String) identifier;
283281

@@ -296,18 +294,22 @@ private void extractArgStats() {
296294
}
297295

298296
public RootNode getRootNode() {
299-
return rootNode;
297+
return callTarget == null ? null : callTarget.getRootNode();
298+
}
299+
300+
private boolean hasRootNode() {
301+
return getRootNode() != null;
300302
}
301303

302304
public Object[] getFreeVars() {
303-
if (freevars == null && rootNode != null) {
305+
if (freevars == null && hasRootNode()) {
304306
extractArgStats();
305307
}
306308
return freevars;
307309
}
308310

309311
public Object[] getCellVars() {
310-
if (freevars == null && rootNode != null) {
312+
if (freevars == null && hasRootNode()) {
311313
extractArgStats();
312314
}
313315
return cellvars;
@@ -318,63 +320,63 @@ public void setFilename(String filename) {
318320
}
319321

320322
public String getFilename() {
321-
if (filename == null && rootNode != null) {
322-
filename = extractFileName(rootNode);
323+
if (filename == null && hasRootNode()) {
324+
filename = extractFileName(getRootNode());
323325
}
324326
return filename;
325327
}
326328

327329
public int getFirstLineNo() {
328-
if (firstlineno == -1 && rootNode != null) {
329-
firstlineno = extractFirstLineno(rootNode);
330+
if (firstlineno == -1 && hasRootNode()) {
331+
firstlineno = extractFirstLineno(getRootNode());
330332
}
331333
return firstlineno;
332334
}
333335

334336
public String getName() {
335-
if (name == null && rootNode != null) {
336-
name = extractName(rootNode);
337+
if (name == null && hasRootNode()) {
338+
name = extractName(getRootNode());
337339
}
338340
return name;
339341
}
340342

341343
public int getArgcount() {
342-
if (argcount == -1 && rootNode != null) {
344+
if (argcount == -1 && hasRootNode()) {
343345
extractArgStats();
344346
}
345347
return argcount;
346348
}
347349

348350
public int getKwonlyargcount() {
349-
if (kwonlyargcount == -1 && rootNode != null) {
351+
if (kwonlyargcount == -1 && hasRootNode()) {
350352
extractArgStats();
351353
}
352354
return kwonlyargcount;
353355
}
354356

355357
public int getNlocals() {
356-
if (nlocals == -1 && rootNode != null) {
358+
if (nlocals == -1 && hasRootNode()) {
357359
extractArgStats();
358360
}
359361
return nlocals;
360362
}
361363

362364
public int getStacksize() {
363-
if (stacksize == -1 && rootNode != null) {
364-
stacksize = extractStackSize(rootNode);
365+
if (stacksize == -1 && hasRootNode()) {
366+
stacksize = extractStackSize(getRootNode());
365367
}
366368
return stacksize;
367369
}
368370

369371
public int getFlags() {
370-
if (flags == -1 && rootNode != null) {
372+
if (flags == -1 && hasRootNode()) {
371373
extractArgStats();
372374
}
373375
return flags;
374376
}
375377

376378
public Object[] getVarnames() {
377-
if (varnames == null && rootNode != null) {
379+
if (varnames == null && hasRootNode()) {
378380
extractArgStats();
379381
}
380382
return varnames;
@@ -397,7 +399,7 @@ public byte[] getLnotab() {
397399
}
398400

399401
private Arity.KeywordName[] getKeywordNames() {
400-
if (keywordNames == null && rootNode != null) {
402+
if (keywordNames == null && hasRootNode()) {
401403
extractArgStats();
402404
}
403405
return keywordNames;
@@ -431,31 +433,7 @@ public Arity getArity() {
431433
return new Arity(this.getName(), this.getMinNumOfPositionalArgs(), this.getMaxNumOfPositionalArgs(), this.takesVarKeywordArgs(), this.takesVarArgs(), this.getKeywordNames());
432434
}
433435

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
455436
public RootCallTarget getRootCallTarget() {
456-
if (rootNode != null) {
457-
return Truffle.getRuntime().createCallTarget(rootNode);
458-
}
459-
return null;
437+
return callTarget;
460438
}
461439
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/function/PFunction.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import com.oracle.truffle.api.CompilerAsserts;
3737
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
3838
import com.oracle.truffle.api.RootCallTarget;
39-
import com.oracle.truffle.api.frame.FrameDescriptor;
4039
import com.oracle.truffle.api.nodes.RootNode;
4140
import com.oracle.truffle.api.object.DynamicObject;
4241

@@ -46,26 +45,24 @@ public class PFunction extends PythonObject implements PythonCallable {
4645
private final String enclosingClassName;
4746
private final Arity arity;
4847
private final RootCallTarget callTarget;
49-
private final FrameDescriptor frameDescriptor;
5048
private final PythonObject globals;
5149
private final PCell[] closure;
5250
private final boolean isStatic;
5351
private PCode code;
5452
private Object[] defaults;
5553

56-
public PFunction(LazyPythonClass clazz, String name, String enclosingClassName, Arity arity, RootCallTarget callTarget, FrameDescriptor frameDescriptor, PythonObject globals, PCell[] closure) {
57-
this(clazz, name, enclosingClassName, arity, callTarget, frameDescriptor, globals, null, closure);
54+
public PFunction(LazyPythonClass clazz, String name, String enclosingClassName, Arity arity, RootCallTarget callTarget, PythonObject globals, PCell[] closure) {
55+
this(clazz, name, enclosingClassName, arity, callTarget, globals, null, closure);
5856
}
5957

60-
public PFunction(LazyPythonClass clazz, String name, String enclosingClassName, Arity arity, RootCallTarget callTarget, FrameDescriptor frameDescriptor, PythonObject globals, Object[] defaults,
58+
public PFunction(LazyPythonClass clazz, String name, String enclosingClassName, Arity arity, RootCallTarget callTarget, PythonObject globals, Object[] defaults,
6159
PCell[] closure) {
6260
super(clazz);
6361
this.name = name;
6462
this.isStatic = name.equals(SpecialMethodNames.__NEW__);
6563
this.enclosingClassName = enclosingClassName;
6664
this.arity = arity;
6765
this.callTarget = callTarget;
68-
this.frameDescriptor = frameDescriptor;
6966
this.globals = globals;
7067
this.defaults = defaults;
7168
this.closure = closure;
@@ -87,11 +84,6 @@ public RootCallTarget getCallTarget() {
8784
return callTarget;
8885
}
8986

90-
@Override
91-
public FrameDescriptor getFrameDescriptor() {
92-
return frameDescriptor;
93-
}
94-
9587
@Override
9688
public PythonObject getGlobals() {
9789
return globals;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/function/PGeneratorFunction.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,15 @@
2929
import com.oracle.graal.python.builtins.objects.object.PythonObject;
3030
import com.oracle.graal.python.builtins.objects.type.LazyPythonClass;
3131
import com.oracle.truffle.api.RootCallTarget;
32-
import com.oracle.truffle.api.frame.FrameDescriptor;
3332

3433
public final class PGeneratorFunction extends PFunction {
3534

36-
public static PGeneratorFunction create(LazyPythonClass clazz, String name, String enclosingClassName, Arity arity, RootCallTarget callTarget,
37-
FrameDescriptor frameDescriptor, PythonObject globals, PCell[] closure) {
38-
return new PGeneratorFunction(clazz, name, enclosingClassName, arity, callTarget, frameDescriptor, globals, closure);
35+
public static PGeneratorFunction create(LazyPythonClass clazz, String name, String enclosingClassName, Arity arity, RootCallTarget callTarget, PythonObject globals, PCell[] closure) {
36+
return new PGeneratorFunction(clazz, name, enclosingClassName, arity, callTarget, globals, closure);
3937
}
4038

41-
public PGeneratorFunction(LazyPythonClass clazz, String name, String enclosingClassName, Arity arity, RootCallTarget callTarget,
42-
FrameDescriptor frameDescriptor, PythonObject globals, PCell[] closure) {
43-
super(clazz, name, enclosingClassName, arity, callTarget, frameDescriptor, globals, closure);
39+
public PGeneratorFunction(LazyPythonClass clazz, String name, String enclosingClassName, Arity arity, RootCallTarget callTarget, PythonObject globals, PCell[] closure) {
40+
super(clazz, name, enclosingClassName, arity, callTarget, globals, closure);
4441
}
4542

4643
@Override

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/method/PMethod.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import com.oracle.graal.python.builtins.objects.object.PythonObject;
3535
import com.oracle.graal.python.builtins.objects.type.LazyPythonClass;
3636
import com.oracle.truffle.api.RootCallTarget;
37-
import com.oracle.truffle.api.frame.FrameDescriptor;
3837

3938
public final class PMethod extends PythonBuiltinObject implements PythonCallable {
4039

@@ -85,11 +84,6 @@ public RootCallTarget getCallTarget() {
8584
return callTarget;
8685
}
8786

88-
@Override
89-
public FrameDescriptor getFrameDescriptor() {
90-
return function.getFrameDescriptor();
91-
}
92-
9387
@Override
9488
public Arity getArity() {
9589
return function.getArity();

0 commit comments

Comments
 (0)