Skip to content

Commit 88f64d0

Browse files
committed
PCode: extract co_filename, co_name, co_firstlineno, co_freevars, co_cellvars from RootNode on initialization
1 parent 12a3a15 commit 88f64d0

File tree

4 files changed

+215
-91
lines changed

4 files changed

+215
-91
lines changed

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1452,25 +1452,25 @@ Object call() {
14521452
public abstract static class CodeTypeNode extends PythonBuiltinNode {
14531453
@Specialization
14541454
Object call(PythonClass cls, int argcount, int kwonlyargcount, int nlocals, int stacksize,
1455-
int flags, String codestring, Object constants, Object names, Object varnames,
1456-
String filename, String name, int firstlineno, Object lnotab, Object freevars,
1457-
Object cellvars) {
1455+
int flags, String codestring, Object constants, Object names, PTuple varnames,
1456+
String filename, String name, int firstlineno, Object lnotab, PTuple freevars,
1457+
PTuple cellvars) {
14581458
return factory().createCode(cls, argcount, kwonlyargcount, nlocals, stacksize,
1459-
flags, codestring, constants, names, varnames,
1460-
filename, name, firstlineno, lnotab, freevars,
1461-
cellvars);
1459+
flags, codestring, constants, names, varnames.getArray(),
1460+
filename, name, firstlineno, lnotab, freevars.getArray(),
1461+
cellvars.getArray());
14621462
}
14631463

14641464
@Specialization
14651465
@TruffleBoundary
14661466
Object call(PythonClass cls, int argcount, int kwonlyargcount, int nlocals, int stacksize,
1467-
int flags, PBytes codestring, Object constants, Object names, Object varnames,
1468-
PString filename, PString name, int firstlineno, Object lnotab, Object freevars,
1469-
Object cellvars) {
1467+
int flags, PBytes codestring, Object constants, Object names, PTuple varnames,
1468+
PString filename, PString name, int firstlineno, Object lnotab, PTuple freevars,
1469+
PTuple cellvars) {
14701470
return factory().createCode(cls, argcount, kwonlyargcount, nlocals, stacksize,
1471-
flags, new String(codestring.getInternalByteArray()), constants, names, varnames,
1472-
filename.getValue(), name.getValue(), firstlineno, lnotab, freevars,
1473-
cellvars);
1471+
flags, new String(codestring.getInternalByteArray()), constants, names, varnames.getArray(),
1472+
filename.getValue(), name.getValue(), firstlineno, lnotab, freevars.getArray(),
1473+
cellvars.getArray());
14741474
}
14751475

14761476
@SuppressWarnings("unused")

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

Lines changed: 115 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,13 @@
3232
import com.oracle.graal.python.builtins.CoreFunctions;
3333
import com.oracle.graal.python.builtins.PythonBuiltins;
3434
import com.oracle.graal.python.builtins.objects.PNone;
35-
import com.oracle.graal.python.nodes.ModuleRootNode;
36-
import com.oracle.graal.python.nodes.function.FunctionRootNode;
35+
import com.oracle.graal.python.builtins.objects.PNotImplemented;
3736
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
3837
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
39-
import com.oracle.graal.python.nodes.generator.GeneratorFunctionRootNode;
4038
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
4139
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
4240
import com.oracle.truffle.api.dsl.NodeFactory;
4341
import com.oracle.truffle.api.dsl.Specialization;
44-
import com.oracle.truffle.api.nodes.RootNode;
45-
import com.oracle.truffle.api.source.SourceSection;
4642

4743
@CoreFunctions(extendClasses = PCode.class)
4844
public class CodeBuiltins extends PythonBuiltins {
@@ -54,96 +50,153 @@ protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFa
5450

5551
@Builtin(name = "co_freevars", fixedNumOfArguments = 1, isGetter = true)
5652
@GenerateNodeFactory
57-
public abstract static class FreeVarsNode extends PythonBuiltinNode {
53+
public abstract static class GetFreeVarsNode extends PythonBuiltinNode {
5854
@Specialization
59-
protected Object doIt(PCode self) {
60-
RootNode rootNode = self.getRootNode();
61-
if (rootNode instanceof FunctionRootNode) {
62-
return factory().createTuple(((FunctionRootNode) rootNode).getFreeVars());
63-
} else if (rootNode instanceof GeneratorFunctionRootNode) {
64-
return factory().createTuple(((GeneratorFunctionRootNode) rootNode).getFreeVars());
65-
} else {
66-
return PNone.NONE;
55+
protected Object get(PCode self) {
56+
Object[] freeVars = self.getFreeVars();
57+
if (freeVars != null) {
58+
return factory().createTuple(freeVars);
6759
}
60+
return PNone.NONE;
6861
}
6962
}
7063

7164
@Builtin(name = "co_cellvars", fixedNumOfArguments = 1, isGetter = true)
7265
@GenerateNodeFactory
73-
public abstract static class CellVarsNode extends PythonBuiltinNode {
66+
public abstract static class GetCellVarsNode extends PythonBuiltinNode {
7467
@Specialization
75-
protected Object doIt(PCode self) {
76-
RootNode rootNode = self.getRootNode();
77-
if (rootNode instanceof FunctionRootNode) {
78-
return factory().createTuple(((FunctionRootNode) rootNode).getCellVars());
79-
} else if (rootNode instanceof GeneratorFunctionRootNode) {
80-
return factory().createTuple(((GeneratorFunctionRootNode) rootNode).getCellVars());
81-
} else {
82-
return PNone.NONE;
68+
protected Object get(PCode self) {
69+
Object[] cellVars = self.getCellVars();
70+
if (cellVars != null) {
71+
return factory().createTuple(cellVars);
8372
}
73+
return PNone.NONE;
8474
}
8575
}
8676

8777
@Builtin(name = "co_filename", fixedNumOfArguments = 1, isGetter = true)
8878
@GenerateNodeFactory
89-
public abstract static class FilenameNode extends PythonBuiltinNode {
79+
public abstract static class GetFilenameNode extends PythonBuiltinNode {
9080
@Specialization
91-
protected Object doIt(PCode self) {
92-
RootNode rootNode = self.getRootNode();
93-
if (rootNode == null) {
94-
return self.getFilename();
95-
}
96-
SourceSection src = rootNode.getSourceSection();
97-
if (src != null) {
98-
return src.getSource().getName();
99-
} else if (rootNode instanceof ModuleRootNode) {
100-
return ((ModuleRootNode) rootNode).getName();
101-
} else {
102-
return PNone.NONE;
81+
protected Object get(PCode self) {
82+
String filename = self.getFilename();
83+
if (filename != null) {
84+
return filename;
10385
}
86+
return PNone.NONE;
10487
}
10588
}
10689

10790
@Builtin(name = "co_firstlineno", fixedNumOfArguments = 1, isGetter = true)
10891
@GenerateNodeFactory
109-
public abstract static class LinenoNode extends PythonBuiltinNode {
92+
public abstract static class GetLinenoNode extends PythonBuiltinNode {
11093
@Specialization
111-
@TruffleBoundary
112-
protected Object doIt(PCode self) {
113-
RootNode rootNode = self.getRootNode();
114-
if (rootNode == null) {
115-
return self.getFirstLineNo();
116-
}
117-
SourceSection sourceSection = rootNode.getSourceSection();
118-
if (sourceSection == null) {
119-
return 1;
120-
} else {
121-
return sourceSection.getStartLine();
122-
}
94+
protected Object get(PCode self) {
95+
return self.getFirstLineNo();
12396
}
12497
}
12598

12699
@Builtin(name = "co_name", fixedNumOfArguments = 1, isGetter = true)
127100
@GenerateNodeFactory
128-
public abstract static class NameNode extends PythonBuiltinNode {
101+
public abstract static class GetNameNode extends PythonBuiltinNode {
129102
@Specialization
130103
@TruffleBoundary
131-
protected Object doIt(PCode self) {
132-
RootNode rootNode = self.getRootNode();
133-
String name;
134-
if (rootNode instanceof ModuleRootNode) {
135-
name = "<module>";
136-
} else if (rootNode instanceof FunctionRootNode) {
137-
name = ((FunctionRootNode) rootNode).getFunctionName();
138-
} else if (rootNode == null) {
139-
name = self.getName();
140-
} else {
141-
name = rootNode.getName();
142-
}
104+
protected Object get(PCode self) {
105+
String name = self.getName();
143106
if (name != null) {
144107
return name;
145108
}
146109
return PNone.NONE;
147110
}
148111
}
112+
113+
@Builtin(name = "co_argcount", fixedNumOfArguments = 1, isGetter = true)
114+
@GenerateNodeFactory
115+
public abstract static class GetArgCountNode extends PythonBuiltinNode {
116+
@Specialization
117+
protected Object get(PCode self) {
118+
return PNotImplemented.NOT_IMPLEMENTED;
119+
}
120+
}
121+
122+
@Builtin(name = "co_kwonlyargcount", fixedNumOfArguments = 1, isGetter = true)
123+
@GenerateNodeFactory
124+
public abstract static class GetKnownlyArgCountNode extends PythonBuiltinNode {
125+
@Specialization
126+
protected Object get(PCode self) {
127+
return PNotImplemented.NOT_IMPLEMENTED;
128+
}
129+
}
130+
131+
@Builtin(name = "co_nlocals", fixedNumOfArguments = 1, isGetter = true)
132+
@GenerateNodeFactory
133+
public abstract static class GetNLocalsNode extends PythonBuiltinNode {
134+
@Specialization
135+
protected Object get(PCode self) {
136+
return PNotImplemented.NOT_IMPLEMENTED;
137+
}
138+
}
139+
140+
@Builtin(name = "co_stacksize", fixedNumOfArguments = 1, isGetter = true)
141+
@GenerateNodeFactory
142+
public abstract static class GetStackSizeNode extends PythonBuiltinNode {
143+
@Specialization
144+
protected Object get(PCode self) {
145+
return PNotImplemented.NOT_IMPLEMENTED;
146+
}
147+
}
148+
149+
@Builtin(name = "co_flags", fixedNumOfArguments = 1, isGetter = true)
150+
@GenerateNodeFactory
151+
public abstract static class GetFlagsNode extends PythonBuiltinNode {
152+
@Specialization
153+
protected Object get(PCode self) {
154+
return PNotImplemented.NOT_IMPLEMENTED;
155+
}
156+
}
157+
158+
@Builtin(name = "co_code", fixedNumOfArguments = 1, isGetter = true)
159+
@GenerateNodeFactory
160+
public abstract static class GetCodeNode extends PythonBuiltinNode {
161+
@Specialization
162+
protected Object get(PCode self) {
163+
return PNotImplemented.NOT_IMPLEMENTED;
164+
}
165+
}
166+
167+
@Builtin(name = "co_consts", fixedNumOfArguments = 1, isGetter = true)
168+
@GenerateNodeFactory
169+
public abstract static class GetConstsNode extends PythonBuiltinNode {
170+
@Specialization
171+
protected Object get(PCode self) {
172+
return PNotImplemented.NOT_IMPLEMENTED;
173+
}
174+
}
175+
176+
@Builtin(name = "co_names", fixedNumOfArguments = 1, isGetter = true)
177+
@GenerateNodeFactory
178+
public abstract static class GetNamesNode extends PythonBuiltinNode {
179+
@Specialization
180+
protected Object get(PCode self) {
181+
return PNotImplemented.NOT_IMPLEMENTED;
182+
}
183+
}
184+
185+
@Builtin(name = "co_varnames", fixedNumOfArguments = 1, isGetter = true)
186+
@GenerateNodeFactory
187+
public abstract static class GetVarNamesNode extends PythonBuiltinNode {
188+
@Specialization
189+
protected Object get(PCode self) {
190+
return PNotImplemented.NOT_IMPLEMENTED;
191+
}
192+
}
193+
194+
@Builtin(name = "co_lnotab", fixedNumOfArguments = 1, isGetter = true)
195+
@GenerateNodeFactory
196+
public abstract static class GetLNoTabNode extends PythonBuiltinNode {
197+
@Specialization
198+
protected Object get(PCode self) {
199+
return PNotImplemented.NOT_IMPLEMENTED;
200+
}
201+
}
149202
}

0 commit comments

Comments
 (0)