43
43
import com .oracle .graal .python .PythonLanguage ;
44
44
import com .oracle .graal .python .builtins .PythonBuiltinClassType ;
45
45
import com .oracle .graal .python .builtins .objects .PNone ;
46
- import com .oracle .graal .python .builtins .objects .type .PythonAbstractClass ;
47
46
import com .oracle .graal .python .builtins .objects .type .PythonClass ;
48
47
import com .oracle .graal .python .builtins .objects .type .TypeNodes ;
49
48
import com .oracle .graal .python .builtins .objects .type .TypeNodes .GetMroStorageNode ;
57
56
import com .oracle .truffle .api .CompilerAsserts ;
58
57
import com .oracle .truffle .api .CompilerDirectives ;
59
58
import com .oracle .truffle .api .CompilerDirectives .CompilationFinal ;
60
- import com .oracle .truffle .api .CompilerDirectives .TruffleBoundary ;
61
59
import com .oracle .truffle .api .TruffleLanguage .ContextReference ;
62
60
import com .oracle .truffle .api .dsl .Cached ;
61
+ import com .oracle .truffle .api .dsl .CachedContext ;
62
+ import com .oracle .truffle .api .dsl .GenerateUncached ;
63
63
import com .oracle .truffle .api .dsl .ImportStatic ;
64
64
import com .oracle .truffle .api .dsl .ReportPolymorphism ;
65
65
import com .oracle .truffle .api .dsl .Specialization ;
68
68
@ ImportStatic (PythonOptions .class )
69
69
@ ReportPolymorphism
70
70
public abstract class LookupAttributeInMRONode extends PNodeWithContext {
71
+ @ GenerateUncached
71
72
public abstract static class Dynamic extends PNodeWithContext {
72
- private static final DynamicUncached UNCACHED = new DynamicUncached ();
73
-
74
73
public abstract Object execute (Object klass , Object key );
75
74
76
- public static LookupAttributeInMRONode .Dynamic create () {
77
- return LookupAttributeInMRONodeGen .DynamicCachedNodeGen .create ();
78
- }
79
-
80
- public static LookupAttributeInMRONode .Dynamic getUncached () {
81
- return UNCACHED ;
82
- }
83
- }
84
-
85
- abstract static class DynamicCached extends Dynamic {
86
- @ CompilationFinal private ContextReference <PythonContext > contextRef ;
87
-
88
75
protected static boolean compareStrings (String key , String cachedKey ) {
89
76
return cachedKey .equals (key );
90
77
}
@@ -97,37 +84,25 @@ protected static Object lookupConstantMRO(Object klass, @SuppressWarnings("unuse
97
84
}
98
85
99
86
@ Specialization (replaces = "lookupConstantMRO" )
100
- protected Object lookup (PythonBuiltinClassType klass , Object key ) {
101
- if (contextRef == null ) {
102
- CompilerDirectives .transferToInterpreterAndInvalidate ();
103
- contextRef = lookupContextReference (PythonLanguage .class );
104
- }
105
- return findAttr (contextRef .get ().getCore (), klass , key );
87
+ protected Object lookup (PythonBuiltinClassType klass , Object key ,
88
+ @ CachedContext (PythonLanguage .class ) PythonContext ctx ,
89
+ @ Cached ReadAttributeFromDynamicObjectNode readAttrNode ) {
90
+ return findAttr (ctx .getCore (), klass , key , readAttrNode );
106
91
}
107
92
108
93
@ Specialization (replaces = "lookupConstantMRO" )
109
94
protected static Object lookup (Object klass , Object key ,
110
- @ Cached ( "create()" ) GetMroStorageNode getMroNode ,
111
- @ Cached ("createForceType()" ) ReadAttributeFromObjectNode readAttrNode ) {
95
+ @ Cached GetMroStorageNode getMroNode ,
96
+ @ Cached (value = "createForceType()" , uncached = "getUncachedForceType ()" ) ReadAttributeFromObjectNode readAttrNode ) {
112
97
return lookupSlow (klass , key , getMroNode , readAttrNode , false );
113
98
}
114
- }
115
99
116
- static class DynamicUncached extends Dynamic {
117
- private final ReadAttributeFromObjectNode readAttrNode = ReadAttributeFromObjectNode . getUncachedForceType ();
118
- private final GetMroStorageNode getMroNode = GetMroStorageNode . getUncached ();
100
+ public static LookupAttributeInMRONode . Dynamic create () {
101
+ return LookupAttributeInMRONodeGen . DynamicNodeGen . create ();
102
+ }
119
103
120
- @ TruffleBoundary
121
- @ Override
122
- public Object execute (Object klass , Object key ) {
123
- if (klass instanceof PythonBuiltinClassType ) {
124
- return findAttr (PythonLanguage .getCore (), (PythonBuiltinClassType ) klass , key );
125
- } else if (klass instanceof PythonAbstractClass ) {
126
- return lookupSlow (klass , key , getMroNode , readAttrNode , false );
127
- } else {
128
- CompilerDirectives .transferToInterpreter ();
129
- throw new RuntimeException ("not implemented: lookup inherited attribute from non-PythonClass" );
130
- }
104
+ public static LookupAttributeInMRONode .Dynamic getUncached () {
105
+ return LookupAttributeInMRONodeGen .DynamicNodeGen .getUncached ();
131
106
}
132
107
}
133
108
@@ -170,17 +145,21 @@ public static LookupAttributeInMRONode createForLookupOfUnmanagedClasses(String
170
145
*/
171
146
public abstract Object execute (Object klass );
172
147
173
- @ TruffleBoundary
174
148
protected static Object findAttr (PythonCore core , PythonBuiltinClassType klass , Object key ) {
149
+ return findAttr (core , klass , key , ReadAttributeFromDynamicObjectNode .getUncached ());
150
+ }
151
+
152
+ protected static Object findAttr (PythonCore core , PythonBuiltinClassType klass , Object key , ReadAttributeFromDynamicObjectNode readAttrNode ) {
175
153
PythonBuiltinClassType current = klass ;
176
- while (current != PythonBuiltinClassType .PythonObject ) {
177
- Object value = ReadAttributeFromDynamicObjectNode .getUncached ().execute (core .lookupType (current ).getStorage (), key );
154
+ Object value = PNone .NO_VALUE ;
155
+ while (current != null ) {
156
+ value = readAttrNode .execute (core .lookupType (current ), key );
178
157
if (value != PNone .NO_VALUE ) {
179
158
return value ;
180
159
}
181
160
current = current .getBase ();
182
161
}
183
- return ReadAttributeFromDynamicObjectNode . getUncached (). execute ( core . lookupType ( current ). getStorage (), key ) ;
162
+ return value ;
184
163
}
185
164
186
165
@ Specialization (guards = {"klass == cachedKlass" }, limit = "getAttributeAccessInlineCacheMaxDepth()" , assumptions = "singleContextAssumption()" )
@@ -190,9 +169,25 @@ protected static Object lookupPBCTCached(@SuppressWarnings("unused") PythonBuilt
190
169
return cachedValue ;
191
170
}
192
171
172
+ protected static boolean canCache (Object value ) {
173
+ return value instanceof Long ||
174
+ value instanceof Integer ||
175
+ value instanceof Boolean ||
176
+ value instanceof Double ||
177
+ value instanceof PNone ;
178
+ }
179
+
180
+ @ Specialization (guards = {"klass == cachedKlass" , "canCache(cachedValue)" }, limit = "getAttributeAccessInlineCacheMaxDepth()" )
181
+ protected static Object lookupPBCTCachedMulti (@ SuppressWarnings ("unused" ) PythonBuiltinClassType klass ,
182
+ @ Cached ("klass" ) @ SuppressWarnings ("unused" ) PythonBuiltinClassType cachedKlass ,
183
+ @ Cached ("findAttr(getCore(), cachedKlass, key)" ) Object cachedValue ) {
184
+ return cachedValue ;
185
+ }
186
+
193
187
@ Specialization (replaces = "lookupPBCTCached" )
194
- protected Object lookupPBCTGeneric (PythonBuiltinClassType klass ) {
195
- return findAttr (getCore (), klass , key );
188
+ protected Object lookupPBCTGeneric (PythonBuiltinClassType klass ,
189
+ @ Cached ReadAttributeFromDynamicObjectNode readAttrNode ) {
190
+ return findAttr (getCore (), klass , key , readAttrNode );
196
191
}
197
192
198
193
static final class PythonClassAssumptionPair {
0 commit comments