Skip to content

Commit 85c0dc0

Browse files
committed
Refactor LookupSpecial nodes
1 parent 3a53a9d commit 85c0dc0

12 files changed

+131
-78
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/call/special/CallBinaryMethodNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
import com.oracle.graal.python.builtins.objects.function.PKeyword;
4848
import com.oracle.graal.python.builtins.objects.method.PBuiltinMethod;
4949
import com.oracle.graal.python.nodes.call.CallNode;
50-
import com.oracle.graal.python.nodes.call.special.LookupSpecialBaseNode.BoundDescriptor;
50+
import com.oracle.graal.python.nodes.call.special.MaybeBindDescriptor.BoundDescriptor;
5151
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
5252
import com.oracle.graal.python.nodes.function.builtins.PythonQuaternaryBuiltinNode;
5353
import com.oracle.graal.python.nodes.function.builtins.PythonTernaryBuiltinNode;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/call/special/CallQuaternaryMethodNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
import com.oracle.graal.python.builtins.objects.function.PKeyword;
4545
import com.oracle.graal.python.builtins.objects.method.PBuiltinMethod;
4646
import com.oracle.graal.python.nodes.call.CallNode;
47-
import com.oracle.graal.python.nodes.call.special.LookupSpecialBaseNode.BoundDescriptor;
47+
import com.oracle.graal.python.nodes.call.special.MaybeBindDescriptor.BoundDescriptor;
4848
import com.oracle.graal.python.nodes.function.builtins.PythonQuaternaryBuiltinNode;
4949
import com.oracle.truffle.api.RootCallTarget;
5050
import com.oracle.truffle.api.dsl.Cached;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/call/special/CallTernaryMethodNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
import com.oracle.graal.python.builtins.objects.function.PKeyword;
4747
import com.oracle.graal.python.builtins.objects.method.PBuiltinMethod;
4848
import com.oracle.graal.python.nodes.call.CallNode;
49-
import com.oracle.graal.python.nodes.call.special.LookupSpecialBaseNode.BoundDescriptor;
49+
import com.oracle.graal.python.nodes.call.special.MaybeBindDescriptor.BoundDescriptor;
5050
import com.oracle.graal.python.nodes.function.builtins.PythonQuaternaryBuiltinNode;
5151
import com.oracle.graal.python.nodes.function.builtins.PythonTernaryBuiltinNode;
5252
import com.oracle.graal.python.runtime.PythonContext;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/call/special/CallUnaryMethodNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
import com.oracle.graal.python.builtins.objects.function.PKeyword;
4848
import com.oracle.graal.python.builtins.objects.method.PBuiltinMethod;
4949
import com.oracle.graal.python.nodes.call.CallNode;
50-
import com.oracle.graal.python.nodes.call.special.LookupSpecialBaseNode.BoundDescriptor;
50+
import com.oracle.graal.python.nodes.call.special.MaybeBindDescriptor.BoundDescriptor;
5151
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
5252
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
5353
import com.oracle.graal.python.runtime.PythonContext;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/call/special/CallVarargsMethodNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
import com.oracle.graal.python.builtins.objects.function.PKeyword;
4646
import com.oracle.graal.python.builtins.objects.method.PBuiltinMethod;
4747
import com.oracle.graal.python.nodes.call.CallNode;
48-
import com.oracle.graal.python.nodes.call.special.LookupSpecialBaseNode.BoundDescriptor;
48+
import com.oracle.graal.python.nodes.call.special.MaybeBindDescriptor.BoundDescriptor;
4949
import com.oracle.graal.python.nodes.function.builtins.PythonVarargsBuiltinNode;
5050
import com.oracle.graal.python.nodes.function.builtins.PythonVarargsBuiltinNode.VarargsBuiltinDirectInvocationNotSupported;
5151
import com.oracle.graal.python.util.PythonUtils;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/call/special/LookupAndCallBinaryNode.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -403,19 +403,19 @@ Object callObjectGeneric(VirtualFrame frame, Object left, Object right,
403403
@SuppressWarnings("unused") @Cached("left.getClass()") Class<?> cachedLeftClass,
404404
@SuppressWarnings("unused") @Cached("right.getClass()") Class<?> cachedRightClass,
405405
@Cached GetClassNode getClassNode,
406-
@Cached("create(name)") LookupSpecialMethodSlotNode getattr) {
406+
@Cached("create(name)") LookupSpecialBaseNode getattr) {
407407
return doCallObject(frame, left, right, getClassNode, getattr);
408408
}
409409

410410
@Specialization(guards = "!isReversible()", replaces = "callObjectGeneric")
411411
@Megamorphic
412412
Object callObjectMegamorphic(VirtualFrame frame, Object left, Object right,
413413
@Cached GetClassNode getClassNode,
414-
@Cached("create(name)") LookupSpecialMethodSlotNode getattr) {
414+
@Cached("create(name)") LookupSpecialBaseNode getattr) {
415415
return doCallObject(frame, left, right, getClassNode, getattr);
416416
}
417417

418-
private Object doCallObject(VirtualFrame frame, Object left, Object right, GetClassNode getClassNode, LookupSpecialMethodSlotNode getattr) {
418+
private Object doCallObject(VirtualFrame frame, Object left, Object right, GetClassNode getClassNode, LookupSpecialBaseNode getattr) {
419419
Object leftClass = getClassNode.execute(left);
420420
Object leftCallable;
421421
try {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/call/special/LookupAndCallTernaryNode.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ protected boolean isReversible() {
102102
Object callObject(VirtualFrame frame, Object arg1, int arg2, Object arg3,
103103
@SuppressWarnings("unused") @Cached("arg1.getClass()") Class<?> cachedArg1Class,
104104
@Cached GetClassNode getClassNode,
105-
@Cached("create(name)") LookupSpecialMethodSlotNode getattr) {
105+
@Cached("create(name)") LookupSpecialBaseNode getattr) {
106106
Object klass = getClassNode.execute(arg1);
107107
return dispatchNode.execute(frame, getattr.execute(frame, klass, arg1), arg1, arg2, arg3);
108108
}
@@ -111,7 +111,7 @@ Object callObject(VirtualFrame frame, Object arg1, int arg2, Object arg3,
111111
Object callObject(VirtualFrame frame, Object arg1, Object arg2, Object arg3,
112112
@SuppressWarnings("unused") @Cached("arg1.getClass()") Class<?> cachedArg1Class,
113113
@Cached GetClassNode getClassNode,
114-
@Cached("create(name)") LookupSpecialMethodSlotNode getattr) {
114+
@Cached("create(name)") LookupSpecialBaseNode getattr) {
115115
Object klass = getClassNode.execute(arg1);
116116
return dispatchNode.execute(frame, getattr.execute(frame, klass, arg1), arg1, arg2, arg3);
117117
}
@@ -120,7 +120,7 @@ Object callObject(VirtualFrame frame, Object arg1, Object arg2, Object arg3,
120120
@Megamorphic
121121
Object callObjectMegamorphic(VirtualFrame frame, Object arg1, Object arg2, Object arg3,
122122
@Cached GetClassNode getClassNode,
123-
@Cached("create(name)") LookupSpecialMethodSlotNode getattr) {
123+
@Cached("create(name)") LookupSpecialBaseNode getattr) {
124124
Object klass = getClassNode.execute(arg1);
125125
return dispatchNode.execute(frame, getattr.execute(frame, klass, arg1), arg1, arg2, arg3);
126126
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/call/special/LookupAndCallUnaryNode.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ static Object callObject(VirtualFrame frame, PNone receiver,
217217
Object callObjectGeneric(VirtualFrame frame, Object receiver,
218218
@SuppressWarnings("unused") @Cached("receiver.getClass()") Class<?> cachedClass,
219219
@Cached GetClassNode getClassNode,
220-
@Cached("create(name)") LookupSpecialMethodSlotNode getattr,
220+
@Cached("create(name)") LookupSpecialBaseNode getattr,
221221
@Cached CallUnaryMethodNode dispatchNode) {
222222
return doCallObject(frame, receiver, getClassNode, getattr, dispatchNode);
223223
}
@@ -226,7 +226,7 @@ Object callObjectGeneric(VirtualFrame frame, Object receiver,
226226
@Megamorphic
227227
Object callObjectMegamorphic(VirtualFrame frame, Object receiver,
228228
@Cached GetClassNode getClassNode,
229-
@Cached("create(name)") LookupSpecialMethodSlotNode getattr,
229+
@Cached("create(name)") LookupSpecialBaseNode getattr,
230230
@Cached CallUnaryMethodNode dispatchNode) {
231231
return doCallObject(frame, receiver, getClassNode, getattr, dispatchNode);
232232
}
@@ -235,7 +235,7 @@ protected Class<?> getObjectClass(Object object) {
235235
return object.getClass();
236236
}
237237

238-
private Object doCallObject(VirtualFrame frame, Object receiver, GetClassNode getClassNode, LookupSpecialMethodSlotNode getattr, CallUnaryMethodNode dispatchNode) {
238+
private Object doCallObject(VirtualFrame frame, Object receiver, GetClassNode getClassNode, LookupSpecialBaseNode getattr, CallUnaryMethodNode dispatchNode) {
239239
Object attr = getattr.execute(frame, getClassNode.execute(receiver), receiver);
240240
if (attr == PNone.NO_VALUE) {
241241
if (handlerFactory != null) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/call/special/LookupSpecialBaseNode.java

Lines changed: 7 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -40,61 +40,19 @@
4040
*/
4141
package com.oracle.graal.python.nodes.call.special;
4242

43-
import com.oracle.graal.python.builtins.objects.PNone;
44-
import com.oracle.graal.python.builtins.objects.function.BuiltinMethodDescriptor;
45-
import com.oracle.graal.python.builtins.objects.function.PBuiltinFunction;
46-
import com.oracle.graal.python.builtins.objects.function.PFunction;
47-
import com.oracle.graal.python.nodes.SpecialMethodNames;
48-
import com.oracle.graal.python.nodes.attributes.LookupInMROBaseNode;
49-
import com.oracle.graal.python.nodes.attributes.LookupInheritedAttributeNode;
50-
import com.oracle.graal.python.nodes.call.CallNode;
51-
import com.oracle.truffle.api.CompilerDirectives;
43+
import com.oracle.graal.python.builtins.objects.type.SpecialMethodSlot;
5244
import com.oracle.truffle.api.frame.VirtualFrame;
5345
import com.oracle.truffle.api.nodes.Node;
54-
import com.oracle.truffle.api.profiles.ValueProfile;
5546

5647
public abstract class LookupSpecialBaseNode extends Node {
57-
@Child LookupInMROBaseNode lookupNode; // this should be initialized by the subclass
58-
@Child private LookupInheritedAttributeNode lookupGet;
59-
@Child private CallNode callGet;
60-
private final ValueProfile lookupResProfile = ValueProfile.createClassProfile();
48+
public abstract Object execute(VirtualFrame frame, Object type, Object receiver);
6149

62-
public final Object execute(VirtualFrame frame, Object type, Object receiver) {
63-
Object descriptor = lookupResProfile.profile(lookupNode.execute(type));
64-
if (descriptor == PNone.NO_VALUE || descriptor instanceof PBuiltinFunction || descriptor instanceof PFunction || descriptor instanceof BuiltinMethodDescriptor) {
65-
// Return unbound to avoid constructing the bound object
66-
return descriptor;
50+
public static LookupSpecialBaseNode create(String key) {
51+
SpecialMethodSlot slot = SpecialMethodSlot.findSpecialSlot(key);
52+
if (slot != null) {
53+
return LookupSpecialMethodSlotNode.create(slot);
6754
}
68-
// Acts as a profile
69-
Object getMethod = ensureLookupGet().execute(descriptor);
70-
if (getMethod != PNone.NO_VALUE) {
71-
return new BoundDescriptor(ensureCallGet().execute(frame, getMethod, descriptor, receiver, type));
72-
}
73-
// CPython considers non-descriptors already bound
74-
return new BoundDescriptor(descriptor);
75-
}
76-
77-
private LookupInheritedAttributeNode ensureLookupGet() {
78-
if (lookupGet == null) {
79-
CompilerDirectives.transferToInterpreterAndInvalidate();
80-
lookupGet = insert(LookupInheritedAttributeNode.create(SpecialMethodNames.__GET__));
81-
}
82-
return lookupGet;
83-
}
84-
85-
private CallNode ensureCallGet() {
86-
if (callGet == null) {
87-
CompilerDirectives.transferToInterpreterAndInvalidate();
88-
callGet = insert(CallNode.create());
89-
}
90-
return callGet;
55+
return LookupSpecialMethodNode.create(key);
9156
}
9257

93-
public static class BoundDescriptor {
94-
public final Object descriptor;
95-
96-
public BoundDescriptor(Object descriptor) {
97-
this.descriptor = descriptor;
98-
}
99-
}
10058
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/call/special/LookupSpecialMethodNode.java

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
import com.oracle.graal.python.nodes.attributes.LookupAttributeInMRONode;
4848
import com.oracle.graal.python.nodes.attributes.LookupInheritedAttributeNode;
4949
import com.oracle.graal.python.nodes.call.CallNode;
50-
import com.oracle.graal.python.nodes.call.special.LookupSpecialMethodNodeFactory.DynamicNodeGen;
5150
import com.oracle.truffle.api.dsl.Cached;
5251
import com.oracle.truffle.api.dsl.GenerateUncached;
5352
import com.oracle.truffle.api.dsl.Specialization;
@@ -62,13 +61,22 @@
6261
* differentiate it from the unbound case. {@link CallUnaryMethodNode} and other method calling
6362
* nodes handle this wrapper.
6463
*/
65-
public final class LookupSpecialMethodNode extends LookupSpecialBaseNode {
66-
LookupSpecialMethodNode(String name) {
67-
this.lookupNode = LookupAttributeInMRONode.create(name);
64+
public abstract class LookupSpecialMethodNode extends LookupSpecialBaseNode {
65+
protected final String name;
66+
67+
public LookupSpecialMethodNode(String name) {
68+
this.name = name;
6869
}
6970

7071
public static LookupSpecialMethodNode create(String name) {
71-
return new LookupSpecialMethodNode(name);
72+
return LookupSpecialMethodNodeGen.create(name);
73+
}
74+
75+
@Specialization
76+
Object lookup(VirtualFrame frame, Object type, Object receiver,
77+
@Cached(parameters = "name") LookupAttributeInMRONode lookupMethod,
78+
@Cached MaybeBindDescriptor bind) {
79+
return bind.execute(frame, lookupMethod.execute(type), receiver);
7280
}
7381

7482
@GenerateUncached
@@ -77,11 +85,11 @@ public abstract static class Dynamic extends Node {
7785
public abstract Object execute(Frame frame, Object type, String name, Object receiver);
7886

7987
public static Dynamic create() {
80-
return DynamicNodeGen.create();
88+
return LookupSpecialMethodNodeGen.DynamicNodeGen.create();
8189
}
8290

8391
public static Dynamic getUncached() {
84-
return DynamicNodeGen.getUncached();
92+
return LookupSpecialMethodNodeGen.DynamicNodeGen.getUncached();
8593
}
8694

8795
@Specialization
@@ -96,10 +104,10 @@ Object lookup(VirtualFrame frame, Object type, String name, Object receiver,
96104
}
97105
Object getMethod = lookupGet.execute(descriptor, SpecialMethodNames.__GET__);
98106
if (getMethod != PNone.NO_VALUE) {
99-
return new BoundDescriptor(callGet.execute(frame, getMethod, descriptor, receiver, type));
107+
return new MaybeBindDescriptor.BoundDescriptor(callGet.execute(frame, getMethod, descriptor, receiver, type));
100108
}
101109
// CPython considers non-descriptors already bound
102-
return new BoundDescriptor(descriptor);
110+
return new MaybeBindDescriptor.BoundDescriptor(descriptor);
103111
}
104112
}
105113
}

0 commit comments

Comments
 (0)