Skip to content

Commit c312882

Browse files
committed
[GR-33179] Bytecode interpreter preparation
PullRequest: graalpython/2236
2 parents 98a6ed7 + 5d3c2ad commit c312882

35 files changed

+830
-716
lines changed

graalpython/com.oracle.graal.python.test/testData/testFiles/RuntimeFileTests/functions.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,29 +38,29 @@
3838
# SOFTWARE.
3939

4040
@__graalpython__.builtin
41-
def hasattr(obj, key):
41+
def hasattr(module, obj, key):
4242
default = object()
4343
return getattr(obj, key, default) is not default
4444

4545

4646
@__graalpython__.builtin
47-
def any(iterable):
47+
def any(module, iterable):
4848
for i in iterable:
4949
if i:
5050
return True
5151
return False
5252

5353

5454
@__graalpython__.builtin
55-
def all(iterable):
55+
def all(module, iterable):
5656
for i in iterable:
5757
if not i:
5858
return False
5959
return True
6060

6161

6262
@__graalpython__.builtin
63-
def filter(func, iterable):
63+
def filter(module, func, iterable):
6464
result = []
6565
predicate = func if func is not None else lambda a: a
6666
for i in iterable:
@@ -112,7 +112,7 @@ def __contains__(self, x):
112112

113113

114114
@__graalpython__.builtin
115-
def vars(*obj):
115+
def vars(module, *obj):
116116
"""Return a dictionary of all the attributes currently bound in obj. If
117117
called with no argument, return the variables bound in local scope."""
118118
if len(obj) == 0:
@@ -127,7 +127,7 @@ def vars(*obj):
127127

128128

129129
@__graalpython__.builtin
130-
def format(value, format_spec=''):
130+
def format(module, value, format_spec=''):
131131
"""Return value.__format__(format_spec)
132132
133133
format_spec defaults to the empty string.
@@ -137,7 +137,7 @@ def format(value, format_spec=''):
137137

138138

139139
@__graalpython__.builtin
140-
def sorted(iterable, key=None, reverse=False):
140+
def sorted(module, iterable, key=None, reverse=False):
141141
"""Return a new list containing all items from the iterable in ascending order.
142142
143143
A custom key function can be supplied to customize the sort order, and the

graalpython/com.oracle.graal.python.test/testData/testFiles/RuntimeFileTests/functions.scope

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@ Scope: []
55
FreeVars: Empty
66
Scope: sorted
77
Kind: Function
8-
FrameDescriptor: [<return_val>, iterable, key, result, reverse]
8+
FrameDescriptor: [<return_val>, iterable, key, module, result, reverse]
99
CellVars: Empty
1010
FreeVars: Empty
1111
Scope: format
1212
Kind: Function
13-
FrameDescriptor: [<return_val>, format_spec, value]
13+
FrameDescriptor: [<return_val>, format_spec, module, value]
1414
CellVars: Empty
1515
FreeVars: Empty
1616
Scope: vars
1717
Kind: Function
18-
FrameDescriptor: [<return_val>, obj]
18+
FrameDescriptor: [<return_val>, module, obj]
1919
CellVars: Empty
2020
FreeVars: Empty
2121
Scope: map
@@ -50,7 +50,7 @@ Scope: []
5050
FreeVars: Empty
5151
Scope: filter
5252
Kind: Function
53-
FrameDescriptor: [<>temp5, <return_val>, func, i, iterable, predicate, result]
53+
FrameDescriptor: [<>temp6, <return_val>, func, i, iterable, module, predicate, result]
5454
CellVars: Empty
5555
FreeVars: Empty
5656
Scope: <lambda>
@@ -60,16 +60,16 @@ Scope: []
6060
FreeVars: Empty
6161
Scope: all
6262
Kind: Function
63-
FrameDescriptor: [<>temp2, <return_val>, i, iterable]
63+
FrameDescriptor: [<>temp3, <return_val>, i, iterable, module]
6464
CellVars: Empty
6565
FreeVars: Empty
6666
Scope: any
6767
Kind: Function
68-
FrameDescriptor: [<>temp2, <return_val>, i, iterable]
68+
FrameDescriptor: [<>temp3, <return_val>, i, iterable, module]
6969
CellVars: Empty
7070
FreeVars: Empty
7171
Scope: hasattr
7272
Kind: Function
73-
FrameDescriptor: [<return_val>, default, key, obj]
73+
FrameDescriptor: [<return_val>, default, key, module, obj]
7474
CellVars: Empty
7575
FreeVars: Empty

graalpython/com.oracle.graal.python.test/testData/testFiles/RuntimeFileTests/functions.tast

Lines changed: 411 additions & 365 deletions
Large diffs are not rendered by default.

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

Lines changed: 30 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import static com.oracle.graal.python.nodes.BuiltinNames.BYTEARRAY;
3535
import static com.oracle.graal.python.nodes.BuiltinNames.BYTES;
3636
import static com.oracle.graal.python.nodes.BuiltinNames.CLASSMETHOD;
37-
import static com.oracle.graal.python.nodes.BuiltinNames.INSTANCEMETHOD;
3837
import static com.oracle.graal.python.nodes.BuiltinNames.COMPLEX;
3938
import static com.oracle.graal.python.nodes.BuiltinNames.DICT;
4039
import static com.oracle.graal.python.nodes.BuiltinNames.DICT_ITEMITERATOR;
@@ -47,6 +46,7 @@
4746
import static com.oracle.graal.python.nodes.BuiltinNames.FLOAT;
4847
import static com.oracle.graal.python.nodes.BuiltinNames.FROZENSET;
4948
import static com.oracle.graal.python.nodes.BuiltinNames.GETSET_DESCRIPTOR;
49+
import static com.oracle.graal.python.nodes.BuiltinNames.INSTANCEMETHOD;
5050
import static com.oracle.graal.python.nodes.BuiltinNames.INT;
5151
import static com.oracle.graal.python.nodes.BuiltinNames.LIST;
5252
import static com.oracle.graal.python.nodes.BuiltinNames.MAP;
@@ -181,7 +181,6 @@
181181
import com.oracle.graal.python.builtins.objects.type.TypeNodes.GetItemsizeNode;
182182
import com.oracle.graal.python.builtins.objects.type.TypeNodes.GetMroNode;
183183
import com.oracle.graal.python.builtins.objects.type.TypeNodes.GetMroStorageNode;
184-
import com.oracle.graal.python.builtins.objects.type.TypeNodes.GetNameNode;
185184
import com.oracle.graal.python.builtins.objects.type.TypeNodes.IsAcceptableBaseNode;
186185
import com.oracle.graal.python.builtins.objects.type.TypeNodes.IsTypeNode;
187186
import com.oracle.graal.python.lib.CanBeDoubleNode;
@@ -198,6 +197,7 @@
198197
import com.oracle.graal.python.lib.PyObjectIsTrueNode;
199198
import com.oracle.graal.python.lib.PyObjectSizeNode;
200199
import com.oracle.graal.python.lib.PyObjectStrAsObjectNode;
200+
import com.oracle.graal.python.lib.PySliceNew;
201201
import com.oracle.graal.python.nodes.BuiltinNames;
202202
import com.oracle.graal.python.nodes.ErrorMessages;
203203
import com.oracle.graal.python.nodes.PConstructAndRaiseNode;
@@ -239,7 +239,6 @@
239239
import com.oracle.graal.python.nodes.object.GetClassNode;
240240
import com.oracle.graal.python.nodes.object.GetOrCreateDictNode;
241241
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
242-
import com.oracle.graal.python.nodes.subscript.SliceLiteralNode;
243242
import com.oracle.graal.python.nodes.util.CannotCastException;
244243
import com.oracle.graal.python.nodes.util.CastToJavaIntExactNode;
245244
import com.oracle.graal.python.nodes.util.CastToJavaStringNode;
@@ -2028,59 +2027,51 @@ PZip zip(VirtualFrame frame, Object cls, Object[] args,
20282027
@Builtin(name = "function", minNumOfPositionalArgs = 3, maxNumOfPositionalArgs = 6, constructsClass = PythonBuiltinClassType.PFunction, isPublic = false)
20292028
@GenerateNodeFactory
20302029
public abstract static class FunctionNode extends PythonBuiltinNode {
2031-
@Child private GetNameNode getNameNode;
2032-
20332030
@Specialization
2034-
public PFunction function(Object cls, PCode code, PDict globals, String name, @SuppressWarnings("unused") PNone defaultArgs, @SuppressWarnings("unused") PNone closure) {
2035-
return factory().createFunction(name, getTypeName(cls), code, globals, null);
2031+
public PFunction function(@SuppressWarnings("unused") Object cls, PCode code, PDict globals, String name, @SuppressWarnings("unused") PNone defaultArgs,
2032+
@SuppressWarnings("unused") PNone closure) {
2033+
return factory().createFunction(name, code, globals, null);
20362034
}
20372035

20382036
@Specialization
2039-
public PFunction function(Object cls, PCode code, PDict globals, @SuppressWarnings("unused") PNone name, @SuppressWarnings("unused") PNone defaultArgs, PTuple closure,
2037+
public PFunction function(@SuppressWarnings("unused") Object cls, PCode code, PDict globals, @SuppressWarnings("unused") PNone name, @SuppressWarnings("unused") PNone defaultArgs,
2038+
PTuple closure,
20402039
@Shared("getObjectArrayNode") @Cached GetObjectArrayNode getObjectArrayNode) {
2041-
return factory().createFunction("<lambda>", getTypeName(cls), code, globals, PCell.toCellArray(getObjectArrayNode.execute(closure)));
2040+
return factory().createFunction("<lambda>", code, globals, PCell.toCellArray(getObjectArrayNode.execute(closure)));
20422041
}
20432042

20442043
@Specialization
2045-
public PFunction function(Object cls, PCode code, PDict globals, @SuppressWarnings("unused") PNone name, @SuppressWarnings("unused") PNone defaultArgs,
2044+
public PFunction function(@SuppressWarnings("unused") Object cls, PCode code, PDict globals, @SuppressWarnings("unused") PNone name, @SuppressWarnings("unused") PNone defaultArgs,
20462045
@SuppressWarnings("unused") PNone closure,
20472046
@SuppressWarnings("unused") @Shared("getObjectArrayNode") @Cached GetObjectArrayNode getObjectArrayNode) {
2048-
return factory().createFunction("<lambda>", getTypeName(cls), code, globals, null);
2047+
return factory().createFunction("<lambda>", code, globals, null);
20492048
}
20502049

20512050
@Specialization
2052-
public PFunction function(Object cls, PCode code, PDict globals, String name, @SuppressWarnings("unused") PNone defaultArgs, PTuple closure,
2051+
public PFunction function(@SuppressWarnings("unused") Object cls, PCode code, PDict globals, String name, @SuppressWarnings("unused") PNone defaultArgs, PTuple closure,
20532052
@Shared("getObjectArrayNode") @Cached GetObjectArrayNode getObjectArrayNode) {
2054-
return factory().createFunction(name, getTypeName(cls), code, globals, PCell.toCellArray(getObjectArrayNode.execute(closure)));
2053+
return factory().createFunction(name, code, globals, PCell.toCellArray(getObjectArrayNode.execute(closure)));
20552054
}
20562055

20572056
@Specialization
2058-
public PFunction function(Object cls, PCode code, PDict globals, String name, PTuple defaultArgs, @SuppressWarnings("unused") PNone closure,
2057+
public PFunction function(@SuppressWarnings("unused") Object cls, PCode code, PDict globals, String name, PTuple defaultArgs, @SuppressWarnings("unused") PNone closure,
20592058
@Shared("getObjectArrayNode") @Cached GetObjectArrayNode getObjectArrayNode) {
20602059
// TODO split defaults of positional args from kwDefaults
2061-
return factory().createFunction(name, getTypeName(cls), code, globals, getObjectArrayNode.execute(defaultArgs), null, null);
2060+
return factory().createFunction(name, code, globals, getObjectArrayNode.execute(defaultArgs), null, null);
20622061
}
20632062

20642063
@Specialization
2065-
public PFunction function(Object cls, PCode code, PDict globals, String name, PTuple defaultArgs, PTuple closure,
2064+
public PFunction function(@SuppressWarnings("unused") Object cls, PCode code, PDict globals, String name, PTuple defaultArgs, PTuple closure,
20662065
@Shared("getObjectArrayNode") @Cached GetObjectArrayNode getObjectArrayNode) {
20672066
// TODO split defaults of positional args from kwDefaults
2068-
return factory().createFunction(name, getTypeName(cls), code, globals, getObjectArrayNode.execute(defaultArgs), null, PCell.toCellArray(getObjectArrayNode.execute(closure)));
2067+
return factory().createFunction(name, code, globals, getObjectArrayNode.execute(defaultArgs), null, PCell.toCellArray(getObjectArrayNode.execute(closure)));
20692068
}
20702069

20712070
@Fallback
20722071
@SuppressWarnings("unused")
2073-
public PFunction function(Object cls, Object code, Object globals, Object name, Object defaultArgs, Object closure) {
2072+
public PFunction function(@SuppressWarnings("unused") Object cls, Object code, Object globals, Object name, Object defaultArgs, Object closure) {
20742073
throw raise(TypeError, ErrorMessages.FUNC_CONSTRUCTION_NOT_SUPPORTED, cls, code, globals, name, defaultArgs, closure);
20752074
}
2076-
2077-
private String getTypeName(Object typeObj) {
2078-
if (getNameNode == null) {
2079-
CompilerDirectives.transferToInterpreterAndInvalidate();
2080-
getNameNode = insert(TypeNodes.GetNameNode.create());
2081-
}
2082-
return getNameNode.execute(typeObj);
2083-
}
20842075
}
20852076

20862077
// builtin-function(method-def, self, module)
@@ -3253,26 +3244,25 @@ Object doGeneric(@SuppressWarnings("unused") Object clazz, Object get, Object se
32533244
// slice(start, stop[, step])
32543245
@Builtin(name = "slice", minNumOfPositionalArgs = 2, maxNumOfPositionalArgs = 4, constructsClass = PythonBuiltinClassType.PSlice)
32553246
@GenerateNodeFactory
3256-
public abstract static class CreateSliceNode extends PythonBuiltinNode {
3257-
3258-
@Specialization(guards = {"isNoValue(second)", "isNoValue(third)"})
3247+
abstract static class SliceNode extends PythonQuaternaryBuiltinNode {
3248+
@Specialization(guards = {"isNoValue(second)"})
32593249
@SuppressWarnings("unused")
3260-
static Object stop(VirtualFrame frame, Object cls, Object first, Object second, Object third,
3261-
@Cached SliceLiteralNode sliceNode) {
3262-
return sliceNode.execute(frame, PNone.NONE, first, PNone.NONE);
3250+
static Object singleArg(Object cls, Object first, Object second, Object third,
3251+
@Cached PySliceNew sliceNode) {
3252+
return sliceNode.execute(PNone.NONE, first, PNone.NONE);
32633253
}
32643254

3265-
@Specialization(guards = {"!isNoValue(second)", "isNoValue(third)"})
3255+
@Specialization(guards = {"!isNoValue(stop)", "isNoValue(step)"})
32663256
@SuppressWarnings("unused")
3267-
static Object startStop(VirtualFrame frame, Object cls, Object first, Object second, Object third,
3268-
@Cached SliceLiteralNode sliceNode) {
3269-
return sliceNode.execute(frame, first, second, PNone.NONE);
3257+
static Object twoArgs(Object cls, Object start, Object stop, Object step,
3258+
@Cached PySliceNew sliceNode) {
3259+
return sliceNode.execute(start, stop, PNone.NONE);
32703260
}
32713261

3272-
@Specialization(guards = {"!isNoValue(second)", "!isNoValue(third)"})
3273-
static Object slice(VirtualFrame frame, @SuppressWarnings("unused") Object cls, Object first, Object second, Object third,
3274-
@Cached SliceLiteralNode sliceNode) {
3275-
return sliceNode.execute(frame, first, second, third);
3262+
@Fallback
3263+
static Object threeArgs(@SuppressWarnings("unused") Object cls, Object start, Object stop, Object step,
3264+
@Cached PySliceNew sliceNode) {
3265+
return sliceNode.execute(start, stop, step);
32763266
}
32773267
}
32783268

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

Lines changed: 4 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@
7878
import com.oracle.graal.python.builtins.objects.exception.OSErrorEnum;
7979
import com.oracle.graal.python.builtins.objects.exception.OSErrorEnum.ErrorAndMessagePair;
8080
import com.oracle.graal.python.builtins.objects.function.PFunction;
81-
import com.oracle.graal.python.builtins.objects.function.Signature;
8281
import com.oracle.graal.python.builtins.objects.generator.PGenerator;
8382
import com.oracle.graal.python.builtins.objects.list.PList;
8483
import com.oracle.graal.python.builtins.objects.method.PMethod;
@@ -88,10 +87,7 @@
8887
import com.oracle.graal.python.lib.PyObjectCallMethodObjArgs;
8988
import com.oracle.graal.python.lib.PyObjectTypeCheck;
9089
import com.oracle.graal.python.nodes.ErrorMessages;
91-
import com.oracle.graal.python.nodes.argument.ReadIndexedArgumentNode;
92-
import com.oracle.graal.python.nodes.argument.ReadVarArgsNode;
9390
import com.oracle.graal.python.nodes.builtins.FunctionNodes.GetCallTargetNode;
94-
import com.oracle.graal.python.nodes.builtins.FunctionNodes.GetSignatureNode;
9591
import com.oracle.graal.python.nodes.call.CallNode;
9692
import com.oracle.graal.python.nodes.classes.IsSubtypeNode;
9793
import com.oracle.graal.python.nodes.function.FunctionRootNode;
@@ -115,7 +111,6 @@
115111
import com.oracle.truffle.api.CallTarget;
116112
import com.oracle.truffle.api.CompilerDirectives;
117113
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
118-
import com.oracle.truffle.api.RootCallTarget;
119114
import com.oracle.truffle.api.TruffleFile;
120115
import com.oracle.truffle.api.TruffleLanguage.Env;
121116
import com.oracle.truffle.api.TruffleLogger;
@@ -131,9 +126,7 @@
131126
import com.oracle.truffle.api.interop.UnsupportedMessageException;
132127
import com.oracle.truffle.api.library.CachedLibrary;
133128
import com.oracle.truffle.api.nodes.LanguageInfo;
134-
import com.oracle.truffle.api.nodes.Node;
135129
import com.oracle.truffle.api.nodes.NodeUtil;
136-
import com.oracle.truffle.api.nodes.NodeVisitor;
137130
import com.oracle.truffle.api.source.Source;
138131
import com.oracle.truffle.llvm.api.Toolchain;
139132

@@ -448,56 +441,10 @@ public Object doIt(VirtualFrame frame, PFunction func) {
448441

449442
@TruffleBoundary
450443
public synchronized PFunction convertToBuiltin(PFunction func) {
451-
/*
452-
* (tfel): To be compatible with CPython, builtin module functions must be bound to
453-
* their respective builtin module. We ignore that builtin functions should really be
454-
* builtin methods here - it does not hurt if they are normal methods. What does hurt,
455-
* however, is if they are not bound, because then using these functions in class field
456-
* won't work when they are called from an instance of that class due to the implicit
457-
* currying with "self".
458-
*/
459-
Signature signature = GetSignatureNode.getUncached().execute(func);
460-
PFunction builtinFunc;
461-
FunctionRootNode functionRootNode = (FunctionRootNode) CodeNodes.GetCodeRootNode.getUncached().execute(func.getCode());
462-
if (signature.getParameterIds().length > 0 && signature.getParameterIds()[0].equals("self")) {
463-
/*
464-
* If the first parameter is called self, we assume the function does explicitly
465-
* declare the module argument
466-
*/
467-
builtinFunc = func;
468-
functionRootNode.setPythonInternal(true);
469-
} else {
470-
RootCallTarget callTarget = PythonLanguage.get(functionRootNode).createCachedCallTarget(
471-
r -> {
472-
/*
473-
* Otherwise, we create a new function with a signature that
474-
* requires one extra argument in front. We actually modify the
475-
* function's AST here, so the original PFunction cannot be used
476-
* anymore (its signature won't agree with it's indexed
477-
* parameter reads).
478-
*/
479-
assert !functionRootNode.isPythonInternal() : "a function cannot be rewritten as builtin twice";
480-
return functionRootNode.rewriteWithNewSignature(signature.createWithSelf(), new NodeVisitor() {
481-
482-
@Override
483-
public boolean visit(Node node) {
484-
if (node instanceof ReadVarArgsNode) {
485-
node.replace(ReadVarArgsNode.create(((ReadVarArgsNode) node).isBuiltin()));
486-
} else if (node instanceof ReadIndexedArgumentNode) {
487-
node.replace(ReadIndexedArgumentNode.create(((ReadIndexedArgumentNode) node).getIndex() + 1));
488-
}
489-
return true;
490-
}
491-
}, x -> x);
492-
}, functionRootNode);
493-
494-
String name = func.getName();
495-
builtinFunc = factory().createFunction(name, func.getEnclosingClassName(),
496-
factory().createCode(callTarget),
497-
func.getGlobals(), func.getDefaults(), func.getKwDefaults(), func.getClosure());
498-
}
499-
500-
return builtinFunc;
444+
FunctionRootNode rootNode = (FunctionRootNode) CodeNodes.GetCodeRootNode.getUncached().execute(func.getCode());
445+
rootNode.setPythonInternal(true);
446+
func.setBuiltin(true);
447+
return func;
501448
}
502449
}
503450

0 commit comments

Comments
 (0)