33
33
import com .oracle .graal .python .builtins .objects .common .HashingStorage ;
34
34
import com .oracle .graal .python .builtins .objects .common .HashingStorageLibrary ;
35
35
import com .oracle .graal .python .builtins .objects .dict .PDict ;
36
- import com .oracle .graal .python .builtins .objects .function .PArguments ;
37
36
import com .oracle .graal .python .builtins .objects .module .PythonModule ;
38
37
import com .oracle .graal .python .nodes .BuiltinNames ;
39
38
import com .oracle .graal .python .nodes .ErrorMessages ;
60
59
import com .oracle .truffle .api .library .CachedLibrary ;
61
60
import com .oracle .truffle .api .nodes .Node ;
62
61
import com .oracle .truffle .api .nodes .NodeInfo ;
62
+ import com .oracle .truffle .api .nodes .UnexpectedResultException ;
63
63
import com .oracle .truffle .api .profiles .ConditionProfile ;
64
64
65
65
@ NodeInfo (shortName = "read_global" )
66
66
public abstract class ReadGlobalOrBuiltinNode extends ExpressionNode implements ReadNode , GlobalNode {
67
67
@ CompilationFinal private boolean wasReadFromModule = false ;
68
68
@ Child private ReadBuiltinNode readFromBuiltinsNode ;
69
69
70
+ @ Override
71
+ public final Object execute (VirtualFrame frame ) {
72
+ return executeWithGlobals (frame , getGlobals (frame ));
73
+ }
74
+
75
+ @ Override
76
+ public final int executeInt (VirtualFrame frame ) throws UnexpectedResultException {
77
+ Object value = execute (frame );
78
+ if (value instanceof Integer ) {
79
+ return (int ) value ;
80
+ }
81
+ throw new UnexpectedResultException (value );
82
+ }
83
+
84
+ @ Override
85
+ public final long executeLong (VirtualFrame frame ) throws UnexpectedResultException {
86
+ Object value = execute (frame );
87
+ if (value instanceof Long ) {
88
+ return (long ) value ;
89
+ }
90
+ throw new UnexpectedResultException (value );
91
+ }
92
+
93
+ @ Override
94
+ public final double executeDouble (VirtualFrame frame ) throws UnexpectedResultException {
95
+ Object o = execute (frame );
96
+ if (o instanceof Double ) {
97
+ return (double ) o ;
98
+ }
99
+ throw new UnexpectedResultException (o );
100
+ }
101
+
102
+ @ Override
103
+ public final boolean executeBoolean (VirtualFrame frame ) throws UnexpectedResultException {
104
+ Object o = execute (frame );
105
+ if (o instanceof Boolean ) {
106
+ return (boolean ) o ;
107
+ }
108
+ throw new UnexpectedResultException (o );
109
+ }
110
+
111
+ public abstract Object executeWithGlobals (VirtualFrame frame , Object globals );
112
+
70
113
protected final String attributeId ;
71
114
72
115
protected ReadGlobalOrBuiltinNode (String attributeId ) {
@@ -82,70 +125,57 @@ public StatementNode makeWriteNode(ExpressionNode rhs) {
82
125
return WriteGlobalNode .create (attributeId , rhs );
83
126
}
84
127
85
- @ Specialization (guards = {"getGlobals(frame) == cachedGlobals" , "isModule(cachedGlobals) " }, assumptions = "singleContextAssumption()" , limit = "1" )
86
- protected Object readGlobalCached (@ SuppressWarnings ("unused" ) VirtualFrame frame ,
128
+ @ Specialization (guards = {"globals == cachedGlobals" }, assumptions = "singleContextAssumption()" , limit = "1" )
129
+ protected Object readGlobalCached (@ SuppressWarnings ("unused" ) PythonModule globals ,
87
130
@ Shared ("readFromModule" ) @ Cached ReadAttributeFromObjectNode readFromModuleNode ,
88
- @ Cached (value = "getGlobals(frame) " , weak = true ) Object cachedGlobals ) {
131
+ @ Cached (value = "globals " , weak = true ) PythonModule cachedGlobals ) {
89
132
Object result = readFromModuleNode .execute (cachedGlobals , attributeId );
90
133
return returnGlobalOrBuiltin (result );
91
134
}
92
135
93
- @ Specialization (guards = "isModule(globals)" , replaces = "readGlobalCached" )
94
- protected Object readGlobal (@ SuppressWarnings ("unused" ) VirtualFrame frame ,
95
- @ Shared ("readFromModule" ) @ Cached ReadAttributeFromObjectNode readFromModuleNode ,
96
- @ Bind ("getGlobals(frame)" ) Object globals ) {
136
+ @ Specialization (replaces = "readGlobalCached" )
137
+ protected Object readGlobal (PythonModule globals ,
138
+ @ Shared ("readFromModule" ) @ Cached ReadAttributeFromObjectNode readFromModuleNode ) {
97
139
Object result = readFromModuleNode .execute (globals , attributeId );
98
140
return returnGlobalOrBuiltin (result );
99
141
}
100
142
101
- protected static HashingStorage getStorage (Object globals ) {
102
- return ((PDict ) globals ).getDictStorage ();
103
- }
104
-
105
- @ Specialization (guards = "isBuiltinDictUnchangedStorage(frame, builtinProfile, cachedGlobals, cachedStorage)" , assumptions = "singleContextAssumption()" , limit = "1" )
106
- protected Object readGlobalBuiltinDictCachedUnchangedStorage (@ SuppressWarnings ("unused" ) VirtualFrame frame ,
107
- @ SuppressWarnings ("unused" ) @ Cached (value = "getGlobals(frame)" , weak = true ) Object cachedGlobals ,
108
- @ Cached (value = "getStorage(getGlobals(frame))" , weak = true ) HashingStorage cachedStorage ,
143
+ @ Specialization (guards = {"globals == cachedGlobals" , "isBuiltinDict(cachedGlobals, builtinProfile)" ,
144
+ "cachedGlobals.getDictStorage() == cachedStorage" }, assumptions = "singleContextAssumption()" , limit = "1" )
145
+ protected Object readGlobalBuiltinDictCachedUnchangedStorage (@ SuppressWarnings ("unused" ) PDict globals ,
146
+ @ SuppressWarnings ("unused" ) @ Cached (value = "globals" , weak = true ) PDict cachedGlobals ,
147
+ @ Cached (value = "globals.getDictStorage()" , weak = true ) HashingStorage cachedStorage ,
109
148
@ CachedLibrary ("cachedStorage" ) HashingStorageLibrary hlib ,
110
149
@ SuppressWarnings ("unused" ) @ Cached IsBuiltinClassProfile builtinProfile ) {
111
150
Object result = hlib .getItem (cachedStorage , attributeId );
112
151
return returnGlobalOrBuiltin (result == null ? PNone .NO_VALUE : result );
113
152
}
114
153
115
- protected final boolean isBuiltinDictUnchangedStorage (VirtualFrame frame , IsBuiltinClassProfile profile , Object cachedGlobals , HashingStorage cachedStorage ) {
116
- return getGlobals (frame ) == cachedGlobals && isBuiltinDict (cachedGlobals , profile ) && getStorage (cachedGlobals ) == cachedStorage ;
117
- }
118
-
119
- protected static HashingStorage getDictStorage (Object cachedGlobals ) {
120
- return ((PDict ) cachedGlobals ).getDictStorage ();
121
- }
122
-
123
- @ Specialization (guards = {"getGlobals(frame) == cachedGlobals" ,
154
+ @ Specialization (guards = {"globals == cachedGlobals" ,
124
155
"isBuiltinDict(cachedGlobals, builtinProfile)" }, assumptions = "singleContextAssumption()" , replaces = "readGlobalBuiltinDictCachedUnchangedStorage" , limit = "1" )
125
- protected Object readGlobalBuiltinDictCached (@ SuppressWarnings ("unused" ) VirtualFrame frame ,
126
- @ Cached (value = "getGlobals(frame) " , weak = true ) Object cachedGlobals ,
127
- @ CachedLibrary (value = "getDictStorage(cachedGlobals )" ) HashingStorageLibrary hlib ,
156
+ protected Object readGlobalBuiltinDictCached (@ SuppressWarnings ("unused" ) PDict globals ,
157
+ @ Cached (value = "globals " , weak = true ) PDict cachedGlobals ,
158
+ @ CachedLibrary (value = "cachedGlobals. getDictStorage()" ) HashingStorageLibrary hlib ,
128
159
@ Cached @ SuppressWarnings ("unused" ) IsBuiltinClassProfile builtinProfile ) {
129
- Object result = hlib .getItem (getDictStorage (cachedGlobals ), attributeId );
160
+ Object result = hlib .getItem (cachedGlobals . getDictStorage (), attributeId );
130
161
return returnGlobalOrBuiltin (result == null ? PNone .NO_VALUE : result );
131
162
}
132
163
133
164
@ Specialization (guards = "isBuiltinDict(globals, builtinProfile)" , replaces = {"readGlobalBuiltinDictCached" , "readGlobalBuiltinDictCachedUnchangedStorage" }, limit = "3" )
134
- protected Object readGlobalBuiltinDict (@ SuppressWarnings ("unused" ) VirtualFrame frame ,
135
- @ SuppressWarnings ("unused" ) @ Bind ("getGlobals(frame)" ) Object globals ,
136
- @ Bind ("getStorage(globals)" ) HashingStorage storage ,
165
+ protected Object readGlobalBuiltinDict (@ SuppressWarnings ("unused" ) PDict globals ,
166
+ @ Bind ("globals.getDictStorage()" ) HashingStorage storage ,
137
167
@ CachedLibrary ("storage" ) HashingStorageLibrary hlib ,
138
168
@ Cached @ SuppressWarnings ("unused" ) IsBuiltinClassProfile builtinProfile ) {
139
169
Object result = hlib .getItem (storage , attributeId );
140
170
return returnGlobalOrBuiltin (result == null ? PNone .NO_VALUE : result );
141
171
}
142
172
143
- @ Specialization ( guards = "isDict(getGlobals(frame))" )
144
- protected Object readGlobalDictGeneric (VirtualFrame frame ,
173
+ @ Specialization
174
+ protected Object readGlobalDictGeneric (VirtualFrame frame , PDict globals ,
145
175
@ Cached GetItemNode getItemNode ,
146
176
@ Cached IsBuiltinClassProfile errorProfile ) {
147
177
try {
148
- Object result = getItemNode .execute (frame , PArguments . getGlobals ( frame ) , attributeId );
178
+ Object result = getItemNode .execute (frame , globals , attributeId );
149
179
return returnGlobalOrBuiltin (result );
150
180
} catch (PException e ) {
151
181
e .expect (KeyError , errorProfile );
@@ -169,6 +199,7 @@ private Object returnGlobalOrBuiltin(Object result) {
169
199
}
170
200
}
171
201
202
+ @ Override
172
203
public String getAttributeId () {
173
204
return attributeId ;
174
205
}
0 commit comments