Skip to content

Commit 01b4cb3

Browse files
committed
Pass function object as frame argument
1 parent fb10459 commit 01b4cb3

File tree

7 files changed

+23
-55
lines changed

7 files changed

+23
-55
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/cext/PythonCextCEvalBuiltins.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import static com.oracle.graal.python.builtins.objects.cext.capi.transitions.ArgDescriptor.PyThreadState;
5353
import static com.oracle.graal.python.builtins.objects.cext.capi.transitions.ArgDescriptor.Void;
5454

55+
import com.oracle.graal.python.PythonLanguage;
5556
import com.oracle.graal.python.builtins.modules.cext.PythonCextBuiltins.CApi11BuiltinNode;
5657
import com.oracle.graal.python.builtins.modules.cext.PythonCextBuiltins.CApiBuiltin;
5758
import com.oracle.graal.python.builtins.modules.cext.PythonCextBuiltins.CApiNullaryBuiltinNode;
@@ -66,6 +67,7 @@
6667
import com.oracle.graal.python.builtins.objects.common.SequenceNodes;
6768
import com.oracle.graal.python.builtins.objects.frame.PFrame;
6869
import com.oracle.graal.python.builtins.objects.function.PArguments;
70+
import com.oracle.graal.python.builtins.objects.function.PFunction;
6971
import com.oracle.graal.python.builtins.objects.function.PKeyword;
7072
import com.oracle.graal.python.builtins.objects.function.Signature;
7173
import com.oracle.graal.python.builtins.objects.module.PythonModule;
@@ -81,6 +83,7 @@
8183
import com.oracle.graal.python.nodes.util.CastToTruffleStringNode;
8284
import com.oracle.graal.python.runtime.GilNode;
8385
import com.oracle.graal.python.runtime.PythonContext;
86+
import com.oracle.graal.python.runtime.object.PFactory;
8487
import com.oracle.truffle.api.RootCallTarget;
8588
import com.oracle.truffle.api.TruffleLogger;
8689
import com.oracle.truffle.api.dsl.Bind;
@@ -151,10 +154,11 @@ Object getFrame(
151154
@CApiBuiltin(ret = PyObjectTransfer, args = {PyObject, PyObject, PyObject, PyObjectConstPtr, Int, PyObjectConstPtr, Int, PyObjectConstPtr, Int, PyObject, PyObject}, call = Ignored)
152155
abstract static class GraalPyPrivate_Eval_EvalCodeEx extends CApi11BuiltinNode {
153156
@Specialization
154-
static Object doGeneric(PCode code, Object globals, Object locals,
157+
static Object doGeneric(PCode code, PythonObject globals, Object locals,
155158
Object argumentArrayPtr, int argumentCount, Object kwsPtr, int kwsCount, Object defaultValueArrayPtr, int defaultValueCount,
156159
Object kwdefaultsWrapper, Object closureObj,
157160
@Bind Node inliningTarget,
161+
@Bind PythonLanguage language,
158162
@Cached PRaiseNode raiseNode,
159163
@Cached CStructAccess.ReadObjectNode readNode,
160164
@Cached PythonCextBuiltins.CastKwargsNode castKwargsNode,
@@ -185,21 +189,18 @@ static Object doGeneric(PCode code, Object globals, Object locals,
185189
// prepare Python frame arguments
186190
Object[] userArguments = readNode.readPyObjectArray(argumentArrayPtr, argumentCount);
187191
Signature signature = getSignatureNode.execute(inliningTarget, code);
192+
PFunction function = PFactory.createFunction(language, code.getName(), code, globals, closure);
188193
Object[] pArguments = createArgumentsNode.execute(inliningTarget, code, userArguments, keywords, signature, null, null, defaults, kwdefaults, false);
189194

190195
// set custom locals
191196
if (!(locals instanceof PNone)) {
192197
PArguments.setSpecialArgument(pArguments, locals);
193198
}
194-
PArguments.setClosure(pArguments, closure);
199+
PArguments.setFunctionObject(pArguments, function);
195200
// TODO(fa): set builtins in globals
196201
// PythonModule builtins = getContext().getBuiltins();
197202
// setBuiltinsInGlobals(globals, setBuiltins, builtins, lib);
198-
if (globals instanceof PythonObject) {
199-
PArguments.setGlobals(pArguments, (PythonObject) globals);
200-
} else {
201-
// TODO(fa): raise appropriate exception
202-
}
203+
PArguments.setGlobals(pArguments, globals);
203204

204205
RootCallTarget rootCallTarget = getCallTargetNode.execute(inliningTarget, code);
205206
return invoke.execute(null, inliningTarget, rootCallTarget, pArguments);

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

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
*/
2626
package com.oracle.graal.python.builtins.objects.function;
2727

28-
import com.oracle.graal.python.builtins.objects.cell.PCell;
2928
import com.oracle.graal.python.builtins.objects.frame.PFrame;
3029
import com.oracle.graal.python.builtins.objects.object.PythonObject;
3130
import com.oracle.graal.python.runtime.PythonOptions;
@@ -45,7 +44,7 @@
4544
* +-------------------+
4645
* INDEX_GLOBALS_ARGUMENT -> | PythonObject |
4746
* +-------------------+
48-
* INDEX_CLOSURE -> | PCell[] |
47+
* INDEX_FUNCTION_OBJECT -> | PFunction |
4948
* +-------------------+
5049
* INDEX_CALLER_FRAME_INFO -> | PFrame.Reference |
5150
* +-------------------+
@@ -64,7 +63,7 @@ public final class PArguments {
6463
private static final int INDEX_GENERATOR_FRAME = 0;
6564
private static final int INDEX_SPECIAL_ARGUMENT = 1;
6665
private static final int INDEX_GLOBALS_ARGUMENT = 2;
67-
private static final int INDEX_CLOSURE = 3;
66+
private static final int INDEX_FUNCTION_OBJECT = 3;
6867
private static final int INDEX_CALLER_FRAME_INFO = 4;
6968
private static final int INDEX_CURRENT_FRAME_INFO = 5;
7069
private static final int INDEX_CURRENT_EXCEPTION = 6;
@@ -199,16 +198,12 @@ public static void setExceptionUnchecked(Object[] arguments, Object exc) {
199198
arguments[INDEX_CURRENT_EXCEPTION] = exc;
200199
}
201200

202-
public static void setClosure(Object[] arguments, PCell[] closure) {
203-
arguments[INDEX_CLOSURE] = closure;
201+
public static PFunction getFunctionObject(Object[] arguments) {
202+
return (PFunction) arguments[INDEX_FUNCTION_OBJECT];
204203
}
205204

206-
public static PCell[] getClosure(Object[] arguments) {
207-
return (PCell[]) arguments[INDEX_CLOSURE];
208-
}
209-
210-
public static PCell[] getClosure(Frame frame) {
211-
return getClosure(frame.getArguments());
205+
public static void setFunctionObject(Object[] arguments, PFunction function) {
206+
arguments[INDEX_FUNCTION_OBJECT] = function;
212207
}
213208

214209
public static void setArgument(Object[] arguments, int index, Object value) {
@@ -248,18 +243,6 @@ public static void setGeneratorFrame(Object[] arguments, MaterializedFrame gener
248243
arguments[INDEX_GENERATOR_FRAME] = generatorFrame;
249244
}
250245

251-
/**
252-
* This should be used only in GeneratorFunctionRootNode, later the slot is overwritten with
253-
* generator frame
254-
*/
255-
public static PFunction getGeneratorFunction(Object[] arguments) {
256-
return (PFunction) arguments[INDEX_GENERATOR_FRAME];
257-
}
258-
259-
public static void setGeneratorFunction(Object[] arguments, PFunction generatorFunction) {
260-
arguments[INDEX_GENERATOR_FRAME] = generatorFunction;
261-
}
262-
263246
/**
264247
* Synchronizes the arguments array of a Truffle frame with a {@link PFrame}. Copies only those
265248
* arguments that are necessary to be synchronized between the two.
@@ -271,7 +254,7 @@ public static void synchronizeArgs(Frame frameToMaterialize, PFrame escapedFrame
271254
// copy only some carefully picked internal arguments
272255
setSpecialArgument(copiedArgs, getSpecialArgument(arguments));
273256
setGlobals(copiedArgs, getGlobals(arguments));
274-
setClosure(copiedArgs, getClosure(arguments));
257+
setFunctionObject(copiedArgs, getFunctionObject(arguments));
275258

276259
escapedFrame.setArguments(copiedArgs);
277260
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/bytecode/PBytecodeGeneratorFunctionRootNode.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,7 @@ public Object execute(VirtualFrame frame) {
8080
Object[] arguments = frame.getArguments();
8181

8282
PythonLanguage language = PythonLanguage.get(this);
83-
// This is passed from the dispatch node
84-
PFunction generatorFunction = PArguments.getGeneratorFunction(arguments);
83+
PFunction generatorFunction = PArguments.getFunctionObject(arguments);
8584
assert generatorFunction != null;
8685
if (rootNode.getCodeUnit().isGenerator()) {
8786
// if CO_ITERABLE_COROUTINE was explicitly set (likely by types.coroutine), we have to

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/bytecode/PBytecodeRootNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5677,7 +5677,7 @@ private void initCell(Frame localFrame, int i) {
56775677
@ExplodeLoop
56785678
private void initFreeVars(Frame localFrame, Object[] originalArgs) {
56795679
if (freevars.length > 0) {
5680-
PCell[] closure = PArguments.getClosure(originalArgs);
5680+
PCell[] closure = PArguments.getFunctionObject(originalArgs).getClosure();
56815681
for (int i = 0; i < freevars.length; i++) {
56825682
localFrame.setObject(freeoffset + i, closure[i]);
56835683
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/bytecode_dsl/PBytecodeDSLGeneratorFunctionRootNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public Object execute(VirtualFrame frame) {
7272
Object[] arguments = frame.getArguments();
7373

7474
// This is passed from InvokeNode node
75-
PFunction generatorFunction = PArguments.getGeneratorFunction(arguments);
75+
PFunction generatorFunction = PArguments.getFunctionObject(arguments);
7676
assert generatorFunction != null;
7777
if (rootNode.getCodeUnit().isGenerator()) {
7878
// if CO_ITERABLE_COROUTINE was explicitly set (likely by types.coroutine), we have to

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/bytecode_dsl/PBytecodeDSLRootNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2490,7 +2490,7 @@ public static void doClearLocal(VirtualFrame frame, LocalAccessor localAccessor,
24902490
public static final class LoadClosure {
24912491
@Specialization
24922492
public static PCell[] doLoadClosure(VirtualFrame frame) {
2493-
return PArguments.getClosure(frame);
2493+
return PArguments.getFunctionObject(frame.getArguments()).getClosure();
24942494
}
24952495
}
24962496

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/call/CallDispatchers.java

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@
5050
import com.oracle.graal.python.builtins.objects.module.PythonModule;
5151
import com.oracle.graal.python.nodes.PNodeWithContext;
5252
import com.oracle.graal.python.nodes.argument.CreateArgumentsNode;
53-
import com.oracle.graal.python.nodes.bytecode.PBytecodeGeneratorFunctionRootNode;
54-
import com.oracle.graal.python.nodes.bytecode_dsl.PBytecodeDSLGeneratorFunctionRootNode;
5553
import com.oracle.graal.python.nodes.function.BuiltinFunctionRootNode;
5654
import com.oracle.graal.python.runtime.ExecutionContext;
5755
import com.oracle.graal.python.runtime.ExecutionContext.IndirectCalleeContext;
@@ -81,10 +79,6 @@
8179

8280
public class CallDispatchers {
8381

84-
private static boolean isGeneratorFunction(RootCallTarget callTarget) {
85-
return callTarget.getRootNode() instanceof PBytecodeGeneratorFunctionRootNode || callTarget.getRootNode() instanceof PBytecodeDSLGeneratorFunctionRootNode;
86-
}
87-
8882
@NeverDefault
8983
public static DirectCallNode createDirectCallNodeFor(PBuiltinFunction callee) {
9084
DirectCallNode callNode = Truffle.getRuntime().createDirectCallNode(callee.getCallTarget());
@@ -352,11 +346,7 @@ static Object doDirect(VirtualFrame frame, Node inliningTarget, DirectCallNode c
352346
@Cached SimpleDirectInvokeNode invoke) {
353347
assert callee.getCallTarget() == callNode.getCallTarget();
354348
PArguments.setGlobals(arguments, callee.getGlobals());
355-
PArguments.setClosure(arguments, callee.getClosure());
356-
RootCallTarget callTarget = (RootCallTarget) callNode.getCurrentCallTarget();
357-
if (isGeneratorFunction(callTarget)) {
358-
PArguments.setGeneratorFunction(arguments, callee);
359-
}
349+
PArguments.setFunctionObject(arguments, callee);
360350
return invoke.execute(frame, inliningTarget, callNode, arguments);
361351
}
362352
}
@@ -373,15 +363,10 @@ public abstract static class FunctionIndirectInvokeNode extends Node {
373363

374364
@Specialization
375365
static Object doDirect(VirtualFrame frame, Node inliningTarget, PFunction callee, Object[] arguments,
376-
@Cached SimpleIndirectInvokeNode invoke,
377-
@Cached InlinedConditionProfile generatorProfile) {
366+
@Cached SimpleIndirectInvokeNode invoke) {
378367
PArguments.setGlobals(arguments, callee.getGlobals());
379-
PArguments.setClosure(arguments, callee.getClosure());
380-
RootCallTarget callTarget = callee.getCallTarget();
381-
if (generatorProfile.profile(inliningTarget, isGeneratorFunction(callTarget))) {
382-
PArguments.setGeneratorFunction(arguments, callee);
383-
}
384-
return invoke.execute(frame, inliningTarget, callTarget, arguments);
368+
PArguments.setFunctionObject(arguments, callee);
369+
return invoke.execute(frame, inliningTarget, callee.getCallTarget(), arguments);
385370
}
386371
}
387372

0 commit comments

Comments
 (0)