Skip to content

Commit db287d1

Browse files
committed
move arity to PRootNodes, thus allowing us to exchange function code objects and recover correct code objects from PFrames
1 parent afeb38e commit db287d1

28 files changed

+348
-300
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_functions.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,9 @@ def test_constructor():
105105
import types
106106
func_copy = types.FunctionType(f.__code__, f.__globals__, f.__name__, f.__defaults__, f.__closure__)
107107

108-
assert func_copy(1, 2) == (1, 2, 10, (), {})
109-
assert func_copy(1, 2, 3) == (1, 2, 3, (), {})
110-
assert func_copy(1, 2, 3, 4, 5, x=2) == (1, 2, 3, (4, 5), {'x': 2})
108+
assert func_copy(1, 2) == (1, 2, 10, (), {}), func_copy(1, 2)
109+
assert func_copy(1, 2, 3) == (1, 2, 3, (), {}), func_copy(1, 2, 3)
110+
assert func_copy(1, 2, 3, 4, 5, x=2) == (1, 2, 3, (4, 5), {'x': 2}), func_copy(1, 2, 3, 4, 5, x=2)
111111

112112

113113
def test_inner_function_with_defaults():

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/Python3Core.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@
168168
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
169169
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
170170
import com.oracle.truffle.api.RootCallTarget;
171+
import com.oracle.truffle.api.Truffle;
171172
import com.oracle.truffle.api.TruffleFile;
172173
import com.oracle.truffle.api.TruffleLanguage.Env;
173174
import com.oracle.truffle.api.TruffleOptions;
@@ -629,7 +630,7 @@ private Source getSource(String basename, String prefix) {
629630

630631
private void loadFile(String s, String prefix) {
631632
Source source = getSource(s, prefix);
632-
Supplier<PCode> getCode = () -> factory.createCode((RootNode) getParser().parse(ParserMode.File, this, source, null));
633+
Supplier<PCode> getCode = () -> factory.createCode(Truffle.getRuntime().createCallTarget((RootNode) getParser().parse(ParserMode.File, this, source, null)));
633634
RootCallTarget callTarget = getLanguage().cacheCode(source.getName(), getCode).getRootCallTarget();
634635
PythonModule mod = lookupBuiltinModule(s);
635636
if (mod == null) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/PythonBuiltins.java

Lines changed: 2 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,14 @@
2828
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__DOC__;
2929
import static com.oracle.graal.python.nodes.SpecialMethodNames.__NEW__;
3030

31-
import java.util.Arrays;
3231
import java.util.Collections;
3332
import java.util.HashMap;
3433
import java.util.List;
3534
import java.util.Map;
3635
import java.util.Map.Entry;
3736
import java.util.function.BiConsumer;
38-
import java.util.logging.Level;
3937

40-
import com.oracle.graal.python.PythonLanguage;
4138
import com.oracle.graal.python.builtins.objects.PNone;
42-
import com.oracle.graal.python.builtins.objects.function.Arity;
4339
import com.oracle.graal.python.builtins.objects.function.PBuiltinFunction;
4440
import com.oracle.graal.python.builtins.objects.type.PythonBuiltinClass;
4541
import com.oracle.graal.python.nodes.function.BuiltinFunctionRootNode;
@@ -81,14 +77,14 @@ public void initialize(PythonCore core) {
8177
Object builtinDoc = builtin.doc().isEmpty() ? PNone.NONE : builtin.doc();
8278
if (builtin.constructsClass().length > 0) {
8379
assert !builtin.isGetter() && !builtin.isSetter() && !builtin.isClassmethod() && !builtin.isStaticmethod();
84-
PBuiltinFunction newFunc = core.factory().createBuiltinFunction(__NEW__, null, createArity(factory, builtin, declaresExplicitSelf), numDefaults(builtin), callTarget);
80+
PBuiltinFunction newFunc = core.factory().createBuiltinFunction(__NEW__, null, numDefaults(builtin), callTarget);
8581
for (PythonBuiltinClassType type : builtin.constructsClass()) {
8682
PythonBuiltinClass builtinClass = core.lookupType(type);
8783
builtinClass.setAttributeUnsafe(__NEW__, newFunc);
8884
builtinClass.setAttribute(__DOC__, builtinDoc);
8985
}
9086
} else {
91-
PBuiltinFunction function = core.factory().createBuiltinFunction(builtin.name(), null, createArity(factory, builtin, declaresExplicitSelf), numDefaults(builtin), callTarget);
87+
PBuiltinFunction function = core.factory().createBuiltinFunction(builtin.name(), null, numDefaults(builtin), callTarget);
9288
function.setAttribute(__DOC__, builtinDoc);
9389
BoundBuiltinCallable<?> callable = function;
9490
if (builtin.isGetter() || builtin.isSetter()) {
@@ -134,53 +130,6 @@ private static int numDefaults(Builtin builtin) {
134130
return maxNumPosArgs - builtin.minNumOfPositionalArgs();
135131
}
136132

137-
/**
138-
* Should return an Arity compatible with {@link BuiltinFunctionRootNode}.createArgumentsList
139-
*/
140-
private static Arity createArity(NodeFactory<? extends PythonBuiltinBaseNode> factory, Builtin builtin, boolean declaresExplicitSelf) {
141-
String[] parameterNames = builtin.parameterNames();
142-
int maxNumPosArgs = Math.max(builtin.minNumOfPositionalArgs(), parameterNames.length);
143-
144-
if (builtin.maxNumOfPositionalArgs() >= 0) {
145-
maxNumPosArgs = builtin.maxNumOfPositionalArgs();
146-
assert parameterNames.length == 0 : "either give all parameter names explicitly, or define the max number: " + builtin.name() + " - " + String.join(",", builtin.parameterNames()) +
147-
" vs " + builtin.maxNumOfPositionalArgs() + " - " + factory.toString();
148-
}
149-
150-
if (!declaresExplicitSelf) {
151-
// if we don't take the explicit self, we still need to accept it by arity
152-
maxNumPosArgs++;
153-
} else if (builtin.constructsClass().length > 0 && maxNumPosArgs == 0) {
154-
// we have this convention to always declare the cls argument without setting the num
155-
// args
156-
maxNumPosArgs = 1;
157-
}
158-
159-
if (maxNumPosArgs > 0) {
160-
if (parameterNames.length == 0) {
161-
PythonLanguage.getLogger().log(Level.FINEST, "missing parameter names for builtin " + factory);
162-
parameterNames = new String[maxNumPosArgs];
163-
parameterNames[0] = builtin.constructsClass().length > 0 ? "$cls" : "$self";
164-
for (int i = 1, p = 'a'; i < parameterNames.length; i++, p++) {
165-
parameterNames[i] = Character.toString((char) p);
166-
}
167-
} else {
168-
if (declaresExplicitSelf) {
169-
assert parameterNames.length == maxNumPosArgs : "not enough parameter ids on " + factory;
170-
} else {
171-
// we don't declare the "self" as a parameter id unless it's explicit
172-
assert parameterNames.length + 1 == maxNumPosArgs : "not enough parameter ids on " + factory;
173-
parameterNames = Arrays.copyOf(parameterNames, parameterNames.length + 1);
174-
System.arraycopy(parameterNames, 0, parameterNames, 1, parameterNames.length - 1);
175-
parameterNames[0] = builtin.constructsClass().length > 0 ? "$cls" : "$self";
176-
}
177-
}
178-
}
179-
180-
return new Arity(builtin.takesVarKeywordArgs(), (builtin.takesVarArgs() || builtin.varArgsMarker()) ? parameterNames.length : -1, builtin.varArgsMarker(), parameterNames,
181-
builtin.keywordOnlyNames());
182-
}
183-
184133
private void setBuiltinFunction(String name, BoundBuiltinCallable<?> function) {
185134
builtinFunctions.put(name, function);
186135
}

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
@@ -1716,24 +1716,24 @@ public abstract static class FunctionNode extends PythonBuiltinNode {
17161716

17171717
@Specialization
17181718
public PFunction function(LazyPythonClass cls, PCode code, PDict globals, String name, @SuppressWarnings("unused") PNone defaultArgs, @SuppressWarnings("unused") PNone closure) {
1719-
return factory().createFunction(name, getTypeName(cls), code.getArity(), code.getRootCallTarget(), globals, null);
1719+
return factory().createFunction(name, getTypeName(cls), code.getRootCallTarget(), globals, null);
17201720
}
17211721

17221722
@Specialization
17231723
public PFunction function(LazyPythonClass cls, PCode code, PDict globals, String name, @SuppressWarnings("unused") PNone defaultArgs, PTuple closure) {
1724-
return factory().createFunction(name, getTypeName(cls), code.getArity(), code.getRootCallTarget(), globals, (PCell[]) closure.getArray());
1724+
return factory().createFunction(name, getTypeName(cls), code.getRootCallTarget(), globals, (PCell[]) closure.getArray());
17251725
}
17261726

17271727
@Specialization
17281728
public PFunction function(LazyPythonClass cls, PCode code, PDict globals, String name, PTuple defaultArgs, @SuppressWarnings("unused") PNone closure) {
17291729
// TODO split defaults of positional args from kwDefaults
1730-
return factory().createFunction(name, getTypeName(cls), code.getArity(), code.getRootCallTarget(), globals, defaultArgs.getArray(), null, null);
1730+
return factory().createFunction(name, getTypeName(cls), code.getRootCallTarget(), globals, defaultArgs.getArray(), null, null);
17311731
}
17321732

17331733
@Specialization
17341734
public PFunction function(LazyPythonClass cls, PCode code, PDict globals, String name, PTuple defaultArgs, PTuple closure) {
17351735
// TODO split defaults of positional args from kwDefaults
1736-
return factory().createFunction(name, getTypeName(cls), code.getArity(), code.getRootCallTarget(), globals, defaultArgs.getArray(), null, (PCell[]) closure.getArray());
1736+
return factory().createFunction(name, getTypeName(cls), code.getRootCallTarget(), globals, defaultArgs.getArray(), null, (PCell[]) closure.getArray());
17371737
}
17381738

17391739
@Fallback

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

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,7 @@ PCode compile(String expression, String filename, String mode, Object kwFlags, O
795795
} else {
796796
throw raise(ValueError, "compile() mode must be 'exec', 'eval' or 'single'");
797797
}
798-
Supplier<PCode> createCode = () -> factory().createCode((RootNode) getCore().getParser().parse(pm, getCore(), source, null));
798+
Supplier<PCode> createCode = () -> factory().createCode(Truffle.getRuntime().createCallTarget((RootNode) getCore().getParser().parse(pm, getCore(), source, null)));
799799
if (getCore().isInitialized()) {
800800
return createCode.get();
801801
} else {
@@ -1752,25 +1752,25 @@ public boolean visit(Node node) {
17521752
* parameter reads).
17531753
*/
17541754
FunctionRootNode functionRootNode = (FunctionRootNode) func.getFunctionRootNode();
1755-
if (!functionRootNode.isRewritten()) {
1756-
functionRootNode.setRewritten();
1757-
func.getFunctionRootNode().accept(new NodeVisitor() {
1758-
public boolean visit(Node node) {
1759-
if (node instanceof ReadVarArgsNode) {
1760-
ReadVarArgsNode varArgsNode = (ReadVarArgsNode) node;
1761-
node.replace(ReadVarArgsNode.create(varArgsNode.getIndex() + 1, varArgsNode.isBuiltin()));
1762-
} else if (node instanceof ReadIndexedArgumentNode) {
1763-
node.replace(ReadIndexedArgumentNode.create(((ReadIndexedArgumentNode) node).getIndex() + 1));
1764-
} else if (node instanceof PythonCallNode) {
1765-
node.replace(((PythonCallNode) node).asSpecialCall());
1766-
}
1767-
return true;
1755+
assert !functionRootNode.isRewritten() : "a function cannot be annotated as builtin twice";
1756+
functionRootNode.setRewritten();
1757+
functionRootNode = functionRootNode.copyWithNewArity(arity.createWithSelf());
1758+
functionRootNode.accept(new NodeVisitor() {
1759+
public boolean visit(Node node) {
1760+
if (node instanceof ReadVarArgsNode) {
1761+
ReadVarArgsNode varArgsNode = (ReadVarArgsNode) node;
1762+
node.replace(ReadVarArgsNode.create(varArgsNode.getIndex() + 1, varArgsNode.isBuiltin()));
1763+
} else if (node instanceof ReadIndexedArgumentNode) {
1764+
node.replace(ReadIndexedArgumentNode.create(((ReadIndexedArgumentNode) node).getIndex() + 1));
1765+
} else if (node instanceof PythonCallNode) {
1766+
node.replace(((PythonCallNode) node).asSpecialCall());
17681767
}
1769-
});
1770-
}
1768+
return true;
1769+
}
1770+
});
17711771

17721772
String name = func.getName();
1773-
builtinFunc = factory().createFunction(name, func.getEnclosingClassName(), arity.createWithSelf(), Truffle.getRuntime().createCallTarget(func.getFunctionRootNode()),
1773+
builtinFunc = factory().createFunction(name, func.getEnclosingClassName(), Truffle.getRuntime().createCallTarget(functionRootNode),
17741774
func.getGlobals(), func.getDefaults(), func.getKwDefaults(), func.getClosure());
17751775
}
17761776

0 commit comments

Comments
 (0)