Skip to content

Commit 42ee3ab

Browse files
committed
pass globals explicitly into Read/Write/DeleteGlobalNode
1 parent 842c34e commit 42ee3ab

File tree

5 files changed

+148
-87
lines changed

5 files changed

+148
-87
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/frame/DeleteGlobalNode.java

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242

4343
import com.oracle.graal.python.PythonLanguage;
4444
import com.oracle.graal.python.builtins.objects.PNone;
45-
import com.oracle.graal.python.builtins.objects.function.PArguments;
45+
import com.oracle.graal.python.builtins.objects.dict.PDict;
46+
import com.oracle.graal.python.builtins.objects.module.PythonModule;
4647
import com.oracle.graal.python.nodes.attributes.DeleteAttributeNode;
4748
import com.oracle.graal.python.nodes.statement.StatementNode;
4849
import com.oracle.graal.python.nodes.subscript.DeleteItemNode;
@@ -63,35 +64,40 @@ public static DeleteGlobalNode create(String attributeId) {
6364
return DeleteGlobalNodeGen.create(attributeId);
6465
}
6566

66-
public abstract Object execute(VirtualFrame frame, Object value);
67+
@Override
68+
public final void executeVoid(VirtualFrame frame) {
69+
executeWithGlobals(frame, getGlobals(frame));
70+
}
71+
72+
public abstract Object executeWithGlobals(VirtualFrame frame, Object globals);
6773

68-
@Specialization(guards = {"getGlobals(frame) == cachedGlobals", "isDict(cachedGlobals)"}, assumptions = "singleContextAssumption", limit = "1")
69-
Object deleteDictCached(VirtualFrame frame,
70-
@Cached(value = "getGlobals(frame)", weak = true) Object cachedGlobals,
74+
@Specialization(guards = {"globals == cachedGlobals"}, assumptions = "singleContextAssumption", limit = "1")
75+
Object deleteDictCached(VirtualFrame frame, @SuppressWarnings("unused") PDict globals,
76+
@Cached(value = "globals", weak = true) PDict cachedGlobals,
7177
@Cached DeleteItemNode deleteNode) {
7278
deleteNode.executeWith(frame, cachedGlobals, attributeId);
7379
return PNone.NONE;
7480
}
7581

76-
@Specialization(guards = "isDict(getGlobals(frame))", replaces = "deleteDictCached")
77-
Object deleteDict(VirtualFrame frame,
82+
@Specialization(replaces = "deleteDictCached")
83+
Object deleteDict(VirtualFrame frame, PDict globals,
7884
@Cached DeleteItemNode deleteNode) {
79-
deleteNode.executeWith(frame, PArguments.getGlobals(frame), attributeId);
85+
deleteNode.executeWith(frame, globals, attributeId);
8086
return PNone.NONE;
8187
}
8288

83-
@Specialization(guards = {"getGlobals(frame) == cachedGlobals", "isModule(cachedGlobals)"}, assumptions = "singleContextAssumption", limit = "1")
84-
Object deleteModuleCached(VirtualFrame frame,
85-
@Cached(value = "getGlobals(frame)", weak = true) Object cachedGlobals,
89+
@Specialization(guards = {"globals == cachedGlobals"}, assumptions = "singleContextAssumption", limit = "1")
90+
Object deleteModuleCached(VirtualFrame frame, @SuppressWarnings("unused") PythonModule globals,
91+
@Cached(value = "globals", weak = true) PythonModule cachedGlobals,
8692
@Cached DeleteAttributeNode storeNode) {
8793
storeNode.execute(frame, cachedGlobals, attributeId);
8894
return PNone.NONE;
8995
}
9096

91-
@Specialization(guards = "isModule(getGlobals(frame))", replaces = "deleteModuleCached")
92-
Object deleteModule(VirtualFrame frame,
97+
@Specialization(replaces = "deleteModuleCached")
98+
Object deleteModule(VirtualFrame frame, PythonModule globals,
9399
@Cached DeleteAttributeNode storeNode) {
94-
storeNode.execute(frame, PArguments.getGlobals(frame), attributeId);
100+
storeNode.execute(frame, globals, attributeId);
95101
return PNone.NONE;
96102
}
97103

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/frame/GlobalNode.java

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
4444
import com.oracle.graal.python.builtins.objects.dict.PDict;
4545
import com.oracle.graal.python.builtins.objects.function.PArguments;
46-
import com.oracle.graal.python.builtins.objects.module.PythonModule;
4746
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
4847
import com.oracle.truffle.api.frame.VirtualFrame;
4948

@@ -52,19 +51,8 @@ default Object getGlobals(VirtualFrame frame) {
5251
return PArguments.getGlobals(frame);
5352
}
5453

55-
default boolean isModule(Object globals) {
56-
return globals instanceof PythonModule;
57-
}
58-
59-
default boolean isBuiltinDict(Object globals, IsBuiltinClassProfile profile) {
60-
if (globals instanceof PDict) {
61-
return profile.profileObject(globals, PythonBuiltinClassType.PDict);
62-
}
63-
return false;
64-
}
65-
66-
default boolean isDict(Object globals) {
67-
return globals instanceof PDict;
54+
default boolean isBuiltinDict(PDict globals, IsBuiltinClassProfile profile) {
55+
return profile.profileObject(globals, PythonBuiltinClassType.PDict);
6856
}
6957

7058
public String getAttributeId();

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/frame/ReadGlobalOrBuiltinNode.java

Lines changed: 66 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import com.oracle.graal.python.builtins.objects.common.HashingStorage;
3434
import com.oracle.graal.python.builtins.objects.common.HashingStorageLibrary;
3535
import com.oracle.graal.python.builtins.objects.dict.PDict;
36-
import com.oracle.graal.python.builtins.objects.function.PArguments;
3736
import com.oracle.graal.python.builtins.objects.module.PythonModule;
3837
import com.oracle.graal.python.nodes.BuiltinNames;
3938
import com.oracle.graal.python.nodes.ErrorMessages;
@@ -60,13 +59,57 @@
6059
import com.oracle.truffle.api.library.CachedLibrary;
6160
import com.oracle.truffle.api.nodes.Node;
6261
import com.oracle.truffle.api.nodes.NodeInfo;
62+
import com.oracle.truffle.api.nodes.UnexpectedResultException;
6363
import com.oracle.truffle.api.profiles.ConditionProfile;
6464

6565
@NodeInfo(shortName = "read_global")
6666
public abstract class ReadGlobalOrBuiltinNode extends ExpressionNode implements ReadNode, GlobalNode {
6767
@CompilationFinal private boolean wasReadFromModule = false;
6868
@Child private ReadBuiltinNode readFromBuiltinsNode;
6969

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+
70113
protected final String attributeId;
71114

72115
protected ReadGlobalOrBuiltinNode(String attributeId) {
@@ -82,70 +125,57 @@ public StatementNode makeWriteNode(ExpressionNode rhs) {
82125
return WriteGlobalNode.create(attributeId, rhs);
83126
}
84127

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,
87130
@Shared("readFromModule") @Cached ReadAttributeFromObjectNode readFromModuleNode,
88-
@Cached(value = "getGlobals(frame)", weak = true) Object cachedGlobals) {
131+
@Cached(value = "globals", weak = true) PythonModule cachedGlobals) {
89132
Object result = readFromModuleNode.execute(cachedGlobals, attributeId);
90133
return returnGlobalOrBuiltin(result);
91134
}
92135

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) {
97139
Object result = readFromModuleNode.execute(globals, attributeId);
98140
return returnGlobalOrBuiltin(result);
99141
}
100142

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,
109148
@CachedLibrary("cachedStorage") HashingStorageLibrary hlib,
110149
@SuppressWarnings("unused") @Cached IsBuiltinClassProfile builtinProfile) {
111150
Object result = hlib.getItem(cachedStorage, attributeId);
112151
return returnGlobalOrBuiltin(result == null ? PNone.NO_VALUE : result);
113152
}
114153

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",
124155
"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,
128159
@Cached @SuppressWarnings("unused") IsBuiltinClassProfile builtinProfile) {
129-
Object result = hlib.getItem(getDictStorage(cachedGlobals), attributeId);
160+
Object result = hlib.getItem(cachedGlobals.getDictStorage(), attributeId);
130161
return returnGlobalOrBuiltin(result == null ? PNone.NO_VALUE : result);
131162
}
132163

133164
@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,
137167
@CachedLibrary("storage") HashingStorageLibrary hlib,
138168
@Cached @SuppressWarnings("unused") IsBuiltinClassProfile builtinProfile) {
139169
Object result = hlib.getItem(storage, attributeId);
140170
return returnGlobalOrBuiltin(result == null ? PNone.NO_VALUE : result);
141171
}
142172

143-
@Specialization(guards = "isDict(getGlobals(frame))")
144-
protected Object readGlobalDictGeneric(VirtualFrame frame,
173+
@Specialization
174+
protected Object readGlobalDictGeneric(VirtualFrame frame, PDict globals,
145175
@Cached GetItemNode getItemNode,
146176
@Cached IsBuiltinClassProfile errorProfile) {
147177
try {
148-
Object result = getItemNode.execute(frame, PArguments.getGlobals(frame), attributeId);
178+
Object result = getItemNode.execute(frame, globals, attributeId);
149179
return returnGlobalOrBuiltin(result);
150180
} catch (PException e) {
151181
e.expect(KeyError, errorProfile);
@@ -169,6 +199,7 @@ private Object returnGlobalOrBuiltin(Object result) {
169199
}
170200
}
171201

202+
@Override
172203
public String getAttributeId() {
173204
return attributeId;
174205
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/frame/WriteGlobalNode.java

Lines changed: 60 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -42,73 +42,110 @@
4242

4343
import com.oracle.graal.python.builtins.objects.common.HashingCollectionNodes;
4444
import com.oracle.graal.python.builtins.objects.dict.PDict;
45-
import com.oracle.graal.python.builtins.objects.function.PArguments;
45+
import com.oracle.graal.python.builtins.objects.module.PythonModule;
4646
import com.oracle.graal.python.nodes.attributes.WriteAttributeToObjectNode;
4747
import com.oracle.graal.python.nodes.expression.ExpressionNode;
4848
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
4949
import com.oracle.graal.python.nodes.statement.StatementNode;
5050
import com.oracle.graal.python.nodes.subscript.SetItemNode;
5151
import com.oracle.truffle.api.dsl.Cached;
5252
import com.oracle.truffle.api.dsl.Cached.Shared;
53-
import com.oracle.truffle.api.dsl.NodeChild;
5453
import com.oracle.truffle.api.dsl.Specialization;
5554
import com.oracle.truffle.api.frame.VirtualFrame;
5655

57-
@NodeChild(value = "rhs", type = ExpressionNode.class)
5856
public abstract class WriteGlobalNode extends StatementNode implements GlobalNode, WriteNode {
5957
protected final String attributeId;
6058
@Child protected IsBuiltinClassProfile builtinProfile = IsBuiltinClassProfile.create();
59+
@Child private ExpressionNode rhs;
6160

62-
WriteGlobalNode(String attributeId) {
61+
WriteGlobalNode(String attributeId, ExpressionNode rhs) {
6362
this.attributeId = attributeId;
63+
this.rhs = rhs;
6464
}
6565

6666
public static WriteGlobalNode create(String attributeId) {
67-
return create(attributeId, null);
67+
return WriteGlobalNodeGen.create(attributeId, null);
6868
}
6969

7070
public static WriteGlobalNode create(String attributeId, ExpressionNode rhs) {
7171
return WriteGlobalNodeGen.create(attributeId, rhs);
7272
}
7373

74-
private static PDict getGlobalsDict(VirtualFrame frame) {
75-
return (PDict) PArguments.getGlobals(frame);
74+
@Override
75+
public final void executeVoid(VirtualFrame frame) {
76+
executeWithGlobals(frame, getGlobals(frame));
77+
}
78+
79+
@Override
80+
public final void executeBoolean(VirtualFrame frame, boolean value) {
81+
executeObjectWithGlobals(frame, getGlobals(frame), value);
82+
}
83+
84+
@Override
85+
public final void executeInt(VirtualFrame frame, int value) {
86+
executeObjectWithGlobals(frame, getGlobals(frame), value);
87+
}
88+
89+
@Override
90+
public final void executeLong(VirtualFrame frame, long value) {
91+
executeObjectWithGlobals(frame, getGlobals(frame), value);
92+
}
93+
94+
@Override
95+
public final void executeDouble(VirtualFrame frame, double value) {
96+
executeObjectWithGlobals(frame, getGlobals(frame), value);
97+
}
98+
99+
@Override
100+
public final void executeObject(VirtualFrame frame, Object value) {
101+
executeObjectWithGlobals(frame, getGlobals(frame), value);
76102
}
77103

78-
@Specialization(guards = {"getGlobals(frame) == cachedGlobals", "isBuiltinDict(cachedGlobals, builtinProfile)"}, assumptions = "singleContextAssumption()", limit = "1")
79-
void writeDictObjectCached(VirtualFrame frame, Object value,
80-
@Cached(value = "getGlobals(frame)", weak = true) Object cachedGlobals,
104+
public final void executeWithGlobals(VirtualFrame frame, Object globals) {
105+
executeObjectWithGlobals(frame, globals, getRhs().execute(frame));
106+
}
107+
108+
public abstract void executeObjectWithGlobals(VirtualFrame frame, Object globals, Object value);
109+
110+
@Specialization(guards = {"globals == cachedGlobals", "isBuiltinDict(cachedGlobals, builtinProfile)"}, assumptions = "singleContextAssumption()", limit = "1")
111+
void writeDictObjectCached(VirtualFrame frame, @SuppressWarnings("unused") PDict globals, Object value,
112+
@Cached(value = "globals", weak = true) PDict cachedGlobals,
81113
@Shared("setItemDict") @Cached HashingCollectionNodes.SetItemNode storeNode) {
82-
storeNode.execute(frame, (PDict) cachedGlobals, attributeId, value);
114+
storeNode.execute(frame, cachedGlobals, attributeId, value);
83115
}
84116

85-
@Specialization(replaces = "writeDictObjectCached", guards = "isBuiltinDict(getGlobals(frame), builtinProfile)")
86-
void writeDictObject(VirtualFrame frame, Object value,
117+
@Specialization(replaces = "writeDictObjectCached", guards = "isBuiltinDict(globals, builtinProfile)")
118+
void writeDictObject(VirtualFrame frame, PDict globals, Object value,
87119
@Shared("setItemDict") @Cached HashingCollectionNodes.SetItemNode storeNode) {
88-
storeNode.execute(frame, getGlobalsDict(frame), attributeId, value);
120+
storeNode.execute(frame, globals, attributeId, value);
89121
}
90122

91-
@Specialization(replaces = {"writeDictObject", "writeDictObjectCached"}, guards = "isDict(getGlobals(frame))")
92-
void writeGenericDict(VirtualFrame frame, Object value,
123+
@Specialization(replaces = {"writeDictObject", "writeDictObjectCached"})
124+
void writeGenericDict(VirtualFrame frame, PDict globals, Object value,
93125
@Cached SetItemNode storeNode) {
94-
storeNode.executeWith(frame, PArguments.getGlobals(frame), attributeId, value);
126+
storeNode.executeWith(frame, globals, attributeId, value);
95127
}
96128

97-
@Specialization(guards = {"getGlobals(frame) == cachedGlobals", "isModule(cachedGlobals)"}, assumptions = "singleContextAssumption()", limit = "1")
98-
void writeModuleCached(@SuppressWarnings("unused") VirtualFrame frame, Object value,
99-
@Cached(value = "getGlobals(frame)", weak = true) Object cachedGlobals,
129+
@Specialization(guards = {"globals == cachedGlobals"}, assumptions = "singleContextAssumption()", limit = "1")
130+
void writeModuleCached(@SuppressWarnings("unused") PythonModule globals, Object value,
131+
@Cached(value = "globals", weak = true) PythonModule cachedGlobals,
100132
@Shared("write") @Cached WriteAttributeToObjectNode write) {
101133
write.execute(cachedGlobals, attributeId, value);
102134
}
103135

104-
@Specialization(guards = "isModule(getGlobals(frame))", replaces = "writeModuleCached")
105-
void writeModule(VirtualFrame frame, Object value,
136+
@Specialization(replaces = "writeModuleCached")
137+
void writeModule(PythonModule globals, Object value,
106138
@Shared("write") @Cached WriteAttributeToObjectNode write) {
107-
write.execute(PArguments.getGlobals(frame), attributeId, value);
139+
write.execute(globals, attributeId, value);
108140
}
109141

110142
@Override
111143
public String getAttributeId() {
112144
return attributeId;
113145
}
146+
147+
@Override
148+
public final ExpressionNode getRhs() {
149+
return rhs;
150+
}
114151
}

0 commit comments

Comments
 (0)