Skip to content

Commit 61640e3

Browse files
committed
Improve function generation.
1 parent c5f4bc4 commit 61640e3

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

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

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import com.oracle.graal.python.builtins.objects.object.PythonObject;
3535
import com.oracle.graal.python.builtins.objects.type.LazyPythonClass;
3636
import com.oracle.graal.python.nodes.SpecialMethodNames;
37+
import com.oracle.graal.python.nodes.attributes.WriteAttributeToDynamicObjectNode;
3738
import com.oracle.graal.python.nodes.generator.GeneratorFunctionRootNode;
3839
import com.oracle.truffle.api.Assumption;
3940
import com.oracle.truffle.api.CompilerAsserts;
@@ -49,8 +50,8 @@ public class PFunction extends PythonObject {
4950
private static final Object[] EMPTY_DEFAULTS = new Object[0];
5051
private final String name;
5152
private final String enclosingClassName;
52-
private final Assumption codeStableAssumption = Truffle.getRuntime().createAssumption("function code unchanged for " + getQualifiedName());
53-
private final Assumption defaultsStableAssumption = Truffle.getRuntime().createAssumption("function defaults unchanged " + getQualifiedName());
53+
private final Assumption codeStableAssumption = Truffle.getRuntime().createAssumption();
54+
private final Assumption defaultsStableAssumption = Truffle.getRuntime().createAssumption();
5455
private final PythonObject globals;
5556
private final PCell[] closure;
5657
private final boolean isStatic;
@@ -64,6 +65,11 @@ public PFunction(LazyPythonClass clazz, String name, String enclosingClassName,
6465

6566
public PFunction(LazyPythonClass clazz, String name, String enclosingClassName, RootCallTarget callTarget, PythonObject globals, Object[] defaultValues, PKeyword[] kwDefaultValues,
6667
PCell[] closure) {
68+
this(clazz, name, enclosingClassName, callTarget, globals, defaultValues, kwDefaultValues, closure, null);
69+
}
70+
71+
public PFunction(LazyPythonClass clazz, String name, String enclosingClassName, RootCallTarget callTarget, PythonObject globals, Object[] defaultValues, PKeyword[] kwDefaultValues,
72+
PCell[] closure, WriteAttributeToDynamicObjectNode writeAttrNode) {
6773
super(clazz);
6874
this.name = name;
6975
this.code = new PCode(PythonBuiltinClassType.PCode, callTarget);
@@ -73,13 +79,17 @@ public PFunction(LazyPythonClass clazz, String name, String enclosingClassName,
7379
this.defaultValues = defaultValues == null ? EMPTY_DEFAULTS : defaultValues;
7480
this.kwDefaultValues = kwDefaultValues == null ? PKeyword.EMPTY_KEYWORDS : kwDefaultValues;
7581
this.closure = closure;
76-
addDefaultConstants(this.getStorage(), name, enclosingClassName);
82+
addDefaultConstants(writeAttrNode, getStorage(), name, enclosingClassName);
7783
}
7884

79-
@TruffleBoundary
80-
private static void addDefaultConstants(DynamicObject storage, String name, String enclosingClassName) {
81-
storage.define(__NAME__, name);
82-
storage.define(__QUALNAME__, enclosingClassName != null ? enclosingClassName + "." + name : name);
85+
private static void addDefaultConstants(WriteAttributeToDynamicObjectNode writeAttrNode, DynamicObject storage, String name, String enclosingClassName) {
86+
if (writeAttrNode != null) {
87+
writeAttrNode.execute(storage, __NAME__, name);
88+
writeAttrNode.execute(storage, __QUALNAME__, enclosingClassName != null ? enclosingClassName + "." + name : name);
89+
} else {
90+
storage.define(__NAME__, name);
91+
storage.define(__QUALNAME__, enclosingClassName != null ? enclosingClassName + "." + name : name);
92+
}
8393
}
8494

8595
public boolean isStatic() {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/function/FunctionDefinitionNode.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import com.oracle.graal.python.builtins.objects.function.PFunction;
3232
import com.oracle.graal.python.builtins.objects.function.PKeyword;
3333
import com.oracle.graal.python.nodes.SpecialAttributeNames;
34+
import com.oracle.graal.python.nodes.attributes.WriteAttributeToDynamicObjectNode;
3435
import com.oracle.graal.python.nodes.attributes.WriteAttributeToObjectNode;
3536
import com.oracle.graal.python.nodes.expression.ExpressionNode;
3637
import com.oracle.graal.python.parser.DefinitionCellSlots;
@@ -53,6 +54,8 @@ public class FunctionDefinitionNode extends ExpressionDefinitionNode {
5354
@Children protected KwDefaultExpressionNode[] kwDefaults;
5455
@Child private ExpressionNode doc;
5556
@Child private WriteAttributeToObjectNode writeDocNode = WriteAttributeToObjectNode.create();
57+
@Child private WriteAttributeToDynamicObjectNode writeNameNode = WriteAttributeToDynamicObjectNode.create();
58+
@Child private PythonObjectFactory factory = PythonObjectFactory.create();
5659

5760
public FunctionDefinitionNode(String functionName, String enclosingClassName, ExpressionNode doc, ExpressionNode[] defaults, KwDefaultExpressionNode[] kwDefaults,
5861
RootCallTarget callTarget,
@@ -74,7 +77,7 @@ protected PythonContext getContext() {
7477
}
7578

7679
protected PythonObjectFactory factory() {
77-
return getContext().getCore().factory();
80+
return factory;
7881
}
7982

8083
private static boolean noNullElements(Object[] array) {
@@ -91,7 +94,7 @@ public Object execute(VirtualFrame frame) {
9194
Object[] defaultValues = computeDefaultValues(frame);
9295
PKeyword[] kwDefaultValues = computeKwDefaultValues(frame);
9396
PCell[] closure = getClosureFromGeneratorOrFunctionLocals(frame);
94-
return withDocString(frame, factory().createFunction(functionName, enclosingClassName, callTarget, PArguments.getGlobals(frame), defaultValues, kwDefaultValues, closure));
97+
return withDocString(frame, factory().createFunction(functionName, enclosingClassName, callTarget, PArguments.getGlobals(frame), defaultValues, kwDefaultValues, closure, writeNameNode));
9598
}
9699

97100
@ExplodeLoop

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/object/PythonObjectFactory.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@
113113
import com.oracle.graal.python.builtins.objects.type.PythonAbstractClass;
114114
import com.oracle.graal.python.builtins.objects.type.PythonClass;
115115
import com.oracle.graal.python.builtins.objects.zipimporter.PZipImporter;
116+
import com.oracle.graal.python.nodes.attributes.WriteAttributeToDynamicObjectNode;
116117
import com.oracle.graal.python.parser.ExecutionCellSlots;
117118
import com.oracle.graal.python.runtime.PythonContext;
118119
import com.oracle.graal.python.runtime.sequence.storage.ByteSequenceStorage;
@@ -363,6 +364,11 @@ public PFunction createFunction(String name, String enclosingClassName, RootCall
363364
return trace(new PFunction(PythonBuiltinClassType.PFunction, name, enclosingClassName, callTarget, globals, defaultValues, kwDefaultValues, closure));
364365
}
365366

367+
public PFunction createFunction(String name, String enclosingClassName, RootCallTarget callTarget, PythonObject globals, Object[] defaultValues, PKeyword[] kwDefaultValues,
368+
PCell[] closure, WriteAttributeToDynamicObjectNode writeAttrNode) {
369+
return trace(new PFunction(PythonBuiltinClassType.PFunction, name, enclosingClassName, callTarget, globals, defaultValues, kwDefaultValues, closure, writeAttrNode));
370+
}
371+
366372
public PBuiltinFunction createBuiltinFunction(String name, LazyPythonClass type, int numDefaults, RootCallTarget callTarget) {
367373
return trace(new PBuiltinFunction(PythonBuiltinClassType.PBuiltinFunction, name, type, numDefaults, callTarget));
368374
}

0 commit comments

Comments
 (0)