Skip to content

Commit 8b0322a

Browse files
committed
common supper class for Attribute Read Write from / to Object nodes
1 parent 927486a commit 8b0322a

File tree

5 files changed

+91
-97
lines changed

5 files changed

+91
-97
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
package com.oracle.graal.python.nodes.attributes;
42+
43+
import com.oracle.graal.python.builtins.objects.common.DynamicObjectStorage;
44+
import com.oracle.graal.python.builtins.objects.common.PHashingCollection;
45+
import com.oracle.graal.python.builtins.objects.object.PythonObject;
46+
import com.oracle.graal.python.builtins.objects.str.PString;
47+
import com.oracle.graal.python.nodes.PGuards;
48+
import com.oracle.graal.python.nodes.PNodeWithContext;
49+
import com.oracle.graal.python.runtime.PythonOptions;
50+
import com.oracle.truffle.api.dsl.ImportStatic;
51+
import com.oracle.truffle.api.object.HiddenKey;
52+
import com.oracle.truffle.api.object.Location;
53+
import com.oracle.truffle.api.object.Property;
54+
55+
@ImportStatic({PGuards.class, PythonOptions.class})
56+
public abstract class ObjectAttributeNode extends PNodeWithContext {
57+
protected Object attrKey(Object key) {
58+
if (key instanceof PString) {
59+
return ((PString) key).getValue();
60+
} else {
61+
return key;
62+
}
63+
}
64+
65+
protected boolean isDictUnsetOrSameAsStorage(PythonObject object) {
66+
PHashingCollection dict = object.getDict();
67+
if (dict == null) {
68+
return true;
69+
} else {
70+
return dict.getDictStorage() instanceof DynamicObjectStorage.PythonObjectDictStorage;
71+
}
72+
}
73+
74+
protected Location getLocationOrNull(Property prop) {
75+
return prop == null ? null : prop.getLocation();
76+
}
77+
78+
protected static boolean isHiddenKey(Object key) {
79+
return key instanceof HiddenKey;
80+
}
81+
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/attributes/ReadAttributeFromDynamicObjectNode.java

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@
4141
package com.oracle.graal.python.nodes.attributes;
4242

4343
import com.oracle.graal.python.builtins.objects.PNone;
44-
import com.oracle.graal.python.builtins.objects.str.PString;
4544
import com.oracle.graal.python.nodes.PGuards;
46-
import com.oracle.graal.python.nodes.PNodeWithContext;
4745
import com.oracle.graal.python.runtime.PythonOptions;
4846
import com.oracle.truffle.api.Assumption;
4947
import com.oracle.truffle.api.CompilerDirectives;
@@ -52,25 +50,16 @@
5250
import com.oracle.truffle.api.dsl.Specialization;
5351
import com.oracle.truffle.api.object.DynamicObject;
5452
import com.oracle.truffle.api.object.Location;
55-
import com.oracle.truffle.api.object.Property;
5653
import com.oracle.truffle.api.object.Shape;
5754

5855
@ImportStatic({PGuards.class, PythonOptions.class})
59-
public abstract class ReadAttributeFromDynamicObjectNode extends PNodeWithContext {
56+
public abstract class ReadAttributeFromDynamicObjectNode extends ObjectAttributeNode {
6057
public static ReadAttributeFromDynamicObjectNode create() {
6158
return ReadAttributeFromDynamicObjectNodeGen.create();
6259
}
6360

6461
public abstract Object execute(Object object, Object key);
6562

66-
protected static Location getLocationOrNull(Property prop) {
67-
return prop == null ? null : prop.getLocation();
68-
}
69-
70-
protected static boolean isNull(Object value) {
71-
return value == null;
72-
}
73-
7463
protected static Object readFinalValue(DynamicObject object, Location location) {
7564
Object value = location.get(object);
7665
return value == null ? PNone.NO_VALUE : value;
@@ -83,14 +72,6 @@ protected static boolean checkShape(@SuppressWarnings("unused") DynamicObject dy
8372
return cachedObject.getShape() == cachedShape;
8473
}
8574

86-
protected Object attrKey(Object key) {
87-
if (key instanceof PString) {
88-
return ((PString) key).getValue();
89-
} else {
90-
return key;
91-
}
92-
}
93-
9475
private static boolean assertFinal(DynamicObject dynamicObject, Object key, Object cachedValue) {
9576
Object other = dynamicObject.get(key) == null ? PNone.NO_VALUE : dynamicObject.get(key);
9677
return cachedValue == other || cachedValue instanceof Number && other instanceof Number && ((Number) cachedValue).doubleValue() == ((Number) other).doubleValue();
@@ -102,7 +83,7 @@ private static boolean assertFinal(DynamicObject dynamicObject, Object key, Obje
10283
"dynamicObject == cachedDynamicObject",
10384
"checkShape(dynamicObject, cachedDynamicObject, cachedShape)",
10485
"key == cachedKey",
105-
"!isNull(loc)",
86+
"loc != null",
10687
"loc.isAssumedFinal()",
10788
}, //
10889
assumptions = {
@@ -127,7 +108,7 @@ protected Object readDirectFinal(DynamicObject dynamicObject, Object key,
127108
guards = {
128109
"dynamicObject.getShape() == cachedShape",
129110
"key == cachedKey",
130-
"isNull(loc) || !loc.isAssumedFinal()",
111+
"loc == null || !loc.isAssumedFinal()",
131112
}, //
132113
assumptions = {
133114
"layoutAssumption"

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/attributes/ReadAttributeFromObjectNode.java

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,9 @@
4141
package com.oracle.graal.python.nodes.attributes;
4242

4343
import com.oracle.graal.python.builtins.objects.PNone;
44-
import com.oracle.graal.python.builtins.objects.common.DynamicObjectStorage;
4544
import com.oracle.graal.python.builtins.objects.common.HashingStorageNodes;
46-
import com.oracle.graal.python.builtins.objects.common.PHashingCollection;
4745
import com.oracle.graal.python.builtins.objects.object.PythonObject;
48-
import com.oracle.graal.python.builtins.objects.str.PString;
4946
import com.oracle.graal.python.nodes.PGuards;
50-
import com.oracle.graal.python.nodes.PNodeWithContext;
5147
import com.oracle.graal.python.nodes.interop.PForeignToPTypeNode;
5248
import com.oracle.graal.python.runtime.PythonOptions;
5349
import com.oracle.truffle.api.Assumption;
@@ -62,34 +58,13 @@
6258
import com.oracle.truffle.api.nodes.Node;
6359

6460
@ImportStatic({PGuards.class, PythonOptions.class})
65-
public abstract class ReadAttributeFromObjectNode extends PNodeWithContext {
61+
public abstract class ReadAttributeFromObjectNode extends ObjectAttributeNode {
6662
public static ReadAttributeFromObjectNode create() {
6763
return ReadAttributeFromObjectNodeGen.create();
6864
}
6965

7066
public abstract Object execute(Object object, Object key);
7167

72-
protected static boolean isNull(Object value) {
73-
return value == null;
74-
}
75-
76-
protected Object attrKey(Object key) {
77-
if (key instanceof PString) {
78-
return ((PString) key).getValue();
79-
} else {
80-
return key;
81-
}
82-
}
83-
84-
protected boolean isDictUnsetOrSameAsStorage(PythonObject object) {
85-
PHashingCollection dict = object.getDict();
86-
if (dict == null) {
87-
return true;
88-
} else {
89-
return dict.getDictStorage() instanceof DynamicObjectStorage.PythonObjectDictStorage;
90-
}
91-
}
92-
9368
// read from the DynamicObject store
9469
@Specialization(guards = {
9570
"object == cachedObject"

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/attributes/WriteAttributeToDynamicObjectNode.java

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@
4040
*/
4141
package com.oracle.graal.python.nodes.attributes;
4242

43-
import com.oracle.graal.python.builtins.objects.str.PString;
44-
import com.oracle.graal.python.nodes.PNodeWithContext;
4543
import com.oracle.graal.python.runtime.PythonOptions;
4644
import com.oracle.truffle.api.Assumption;
4745
import com.oracle.truffle.api.CompilerAsserts;
@@ -54,11 +52,10 @@
5452
import com.oracle.truffle.api.object.FinalLocationException;
5553
import com.oracle.truffle.api.object.IncompatibleLocationException;
5654
import com.oracle.truffle.api.object.Location;
57-
import com.oracle.truffle.api.object.Property;
5855
import com.oracle.truffle.api.object.Shape;
5956

6057
@ImportStatic(PythonOptions.class)
61-
public abstract class WriteAttributeToDynamicObjectNode extends PNodeWithContext {
58+
public abstract class WriteAttributeToDynamicObjectNode extends ObjectAttributeNode {
6259

6360
public abstract boolean execute(Object primary, Object key, Object value);
6461

@@ -68,18 +65,6 @@ public static WriteAttributeToDynamicObjectNode create() {
6865
return WriteAttributeToDynamicObjectNodeGen.create();
6966
}
7067

71-
protected Location getLocationOrNull(Property prop) {
72-
return prop == null ? null : prop.getLocation();
73-
}
74-
75-
protected Object attrKey(Object key) {
76-
if (key instanceof PString) {
77-
return ((PString) key).getValue();
78-
} else {
79-
return key;
80-
}
81-
}
82-
8368
@SuppressWarnings("unused")
8469
@Specialization(guards = {
8570
"dynamicObject.getShape() == cachedShape",
@@ -93,15 +78,11 @@ protected boolean updateShapeAndWrite(DynamicObject dynamicObject, Object key, O
9378
return nextNode.execute(dynamicObject, key, value);
9479
}
9580

96-
protected static boolean compareKey(Object cachedKey, Object key) {
97-
return cachedKey == key;
98-
}
99-
10081
@SuppressWarnings("unused")
10182
@Specialization(limit = "getIntOption(getContext(), AttributeAccessInlineCacheMaxDepth)", //
10283
guards = {
10384
"dynamicObject.getShape() == cachedShape",
104-
"compareKey(cachedKey, key)",
85+
"cachedKey == key",
10586
"loc != null",
10687
"loc.canSet(value)"
10788
}, //
@@ -129,7 +110,7 @@ protected boolean doDirect(DynamicObject dynamicObject, Object key, Object value
129110
@Specialization(limit = "getIntOption(getContext(), AttributeAccessInlineCacheMaxDepth)", //
130111
guards = {
131112
"dynamicObject.getShape() == cachedShape",
132-
"compareKey(cachedKey, key)",
113+
"cachedKey == key",
133114
"loc == null || !loc.canSet(value)",
134115
"newLoc.canSet(value)"
135116
}, //

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/attributes/WriteAttributeToObjectNode.java

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -41,29 +41,26 @@
4141
package com.oracle.graal.python.nodes.attributes;
4242

4343
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
44-
import com.oracle.graal.python.builtins.objects.common.DynamicObjectStorage;
4544
import com.oracle.graal.python.builtins.objects.common.HashingStorage;
4645
import com.oracle.graal.python.builtins.objects.common.HashingStorageNodes;
4746
import com.oracle.graal.python.builtins.objects.common.PHashingCollection;
4847
import com.oracle.graal.python.builtins.objects.exception.PBaseException;
4948
import com.oracle.graal.python.builtins.objects.function.PFunction;
49+
import com.oracle.graal.python.builtins.objects.method.PMethod;
5050
import com.oracle.graal.python.builtins.objects.module.PythonModule;
5151
import com.oracle.graal.python.builtins.objects.object.PythonObject;
52-
import com.oracle.graal.python.builtins.objects.str.PString;
5352
import com.oracle.graal.python.builtins.objects.type.PythonClass;
54-
import com.oracle.graal.python.nodes.PNodeWithContext;
5553
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
5654
import com.oracle.graal.python.runtime.PythonOptions;
5755
import com.oracle.truffle.api.Assumption;
5856
import com.oracle.truffle.api.dsl.Cached;
5957
import com.oracle.truffle.api.dsl.Fallback;
6058
import com.oracle.truffle.api.dsl.ImportStatic;
6159
import com.oracle.truffle.api.dsl.Specialization;
62-
import com.oracle.truffle.api.object.HiddenKey;
6360
import com.oracle.truffle.api.profiles.ConditionProfile;
6461

6562
@ImportStatic(PythonOptions.class)
66-
public abstract class WriteAttributeToObjectNode extends PNodeWithContext {
63+
public abstract class WriteAttributeToObjectNode extends ObjectAttributeNode {
6764

6865
private final ConditionProfile isClassProfile = ConditionProfile.createBinaryProfile();
6966
private final IsBuiltinClassProfile exactBuiltinInstanceProfile = IsBuiltinClassProfile.create();
@@ -76,16 +73,8 @@ public static WriteAttributeToObjectNode create() {
7673
return WriteAttributeToObjectNodeGen.create();
7774
}
7875

79-
protected Object attrKey(Object key) {
80-
if (key instanceof PString) {
81-
return ((PString) key).getValue();
82-
} else {
83-
return key;
84-
}
85-
}
86-
8776
protected boolean isAttrWritable(PythonObject self) {
88-
if (self instanceof PythonClass || self instanceof PFunction || self instanceof PythonModule || self instanceof PBaseException) {
77+
if (self instanceof PythonClass || self instanceof PFunction || self instanceof PMethod || self instanceof PythonModule || self instanceof PBaseException) {
8978
return true;
9079
}
9180
return !exactBuiltinInstanceProfile.profileIsAnyBuiltinObject(self);
@@ -99,19 +88,6 @@ private void handlePythonClass(PythonObject object, Object key) {
9988
}
10089
}
10190

102-
protected static boolean isHiddenKey(Object key) {
103-
return key instanceof HiddenKey;
104-
}
105-
106-
protected boolean isDictUnsetOrSameAsStorage(PythonObject object) {
107-
PHashingCollection dict = object.getDict();
108-
if (dict == null) {
109-
return true;
110-
} else {
111-
return dict.getDictStorage() instanceof DynamicObjectStorage.PythonObjectDictStorage;
112-
}
113-
}
114-
11591
// write to the DynamicObject
11692
@Specialization(guards = {
11793
"isAttrWritable(object) || isHiddenKey(key)",

0 commit comments

Comments
 (0)