Skip to content

Commit 3906ce8

Browse files
committed
emulation: extracted IsMappingNode
- new convenience HasInheritedAttributeNode
1 parent be81138 commit 3906ce8

File tree

4 files changed

+139
-27
lines changed

4 files changed

+139
-27
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright (c) 2018, Oracle and/or its affiliates.
3+
*
4+
* The Universal Permissive License (UPL), Version 1.0
5+
*
6+
* Subject to the condition set forth below, permission is hereby granted to any
7+
* person obtaining a copy of this software, associated documentation and/or data
8+
* (collectively the "Software"), free of charge and under any and all copyright
9+
* rights in the Software, and any and all patent rights owned or freely
10+
* licensable by each licensor hereunder covering either (i) the unmodified
11+
* Software as contributed to or provided by such licensor, or (ii) the Larger
12+
* Works (as defined below), to deal in both
13+
*
14+
* (a) the Software, and
15+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
16+
* one is included with the Software (each a "Larger Work" to which the
17+
* Software is contributed by such licensors),
18+
*
19+
* without restriction, including without limitation the rights to copy, create
20+
* derivative works of, display, perform, and distribute the Software and make,
21+
* use, sell, offer for sale, import, export, have made, and have sold the
22+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
23+
* either these or other terms.
24+
*
25+
* This license is subject to the following condition:
26+
*
27+
* The above copyright notice and either this complete permission notice or at a
28+
* minimum a reference to the UPL must be included in all copies or substantial
29+
* portions of the Software.
30+
*
31+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
32+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
33+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
34+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
35+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
36+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
37+
* SOFTWARE.
38+
*/
39+
package com.oracle.graal.python.nodes.attributes;
40+
41+
import com.oracle.graal.python.builtins.objects.PNone;
42+
import com.oracle.truffle.api.nodes.Node;
43+
44+
public class HasInheritedAttributeNode extends Node {
45+
private final String attribute;
46+
@Child private LookupInheritedAttributeNode lookupInheritedAttributeNode = LookupInheritedAttributeNode.create();
47+
48+
private HasInheritedAttributeNode(String attribute) {
49+
this.attribute = attribute;
50+
}
51+
52+
public boolean execute(Object object) {
53+
return lookupInheritedAttributeNode.execute(object, attribute) != PNone.NO_VALUE;
54+
}
55+
56+
public static HasInheritedAttributeNode create(String attribute) {
57+
return new HasInheritedAttributeNode(attribute);
58+
}
59+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright (c) 2018, Oracle and/or its affiliates.
3+
*
4+
* The Universal Permissive License (UPL), Version 1.0
5+
*
6+
* Subject to the condition set forth below, permission is hereby granted to any
7+
* person obtaining a copy of this software, associated documentation and/or data
8+
* (collectively the "Software"), free of charge and under any and all copyright
9+
* rights in the Software, and any and all patent rights owned or freely
10+
* licensable by each licensor hereunder covering either (i) the unmodified
11+
* Software as contributed to or provided by such licensor, or (ii) the Larger
12+
* Works (as defined below), to deal in both
13+
*
14+
* (a) the Software, and
15+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
16+
* one is included with the Software (each a "Larger Work" to which the
17+
* Software is contributed by such licensors),
18+
*
19+
* without restriction, including without limitation the rights to copy, create
20+
* derivative works of, display, perform, and distribute the Software and make,
21+
* use, sell, offer for sale, import, export, have made, and have sold the
22+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
23+
* either these or other terms.
24+
*
25+
* This license is subject to the following condition:
26+
*
27+
* The above copyright notice and either this complete permission notice or at a
28+
* minimum a reference to the UPL must be included in all copies or substantial
29+
* portions of the Software.
30+
*
31+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
32+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
33+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
34+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
35+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
36+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
37+
* SOFTWARE.
38+
*/
39+
package com.oracle.graal.python.nodes.datamodel;
40+
41+
import static com.oracle.graal.python.nodes.SpecialMethodNames.ITEMS;
42+
import static com.oracle.graal.python.nodes.SpecialMethodNames.KEYS;
43+
import static com.oracle.graal.python.nodes.SpecialMethodNames.VALUES;
44+
45+
import com.oracle.graal.python.nodes.attributes.HasInheritedAttributeNode;
46+
import com.oracle.truffle.api.dsl.Specialization;
47+
import com.oracle.truffle.api.profiles.ConditionProfile;
48+
49+
public abstract class IsMappingNode extends PDataModelEmulationNode {
50+
@Child private HasInheritedAttributeNode hasKeysNode = HasInheritedAttributeNode.create(KEYS);
51+
@Child private HasInheritedAttributeNode hasItemsNode = HasInheritedAttributeNode.create(ITEMS);
52+
@Child private HasInheritedAttributeNode hasValuesNode = HasInheritedAttributeNode.create(VALUES);
53+
@Child private IsSequenceNode isSequence = IsSequenceNode.create();
54+
55+
private final ConditionProfile profile = ConditionProfile.createBinaryProfile();
56+
57+
@Specialization
58+
public boolean isMapping(Object object) {
59+
if (isSequence.execute(object)) {
60+
return profile.profile((hasKeysNode.execute(object)) &&
61+
(hasItemsNode.execute(object)) &&
62+
(hasValuesNode.execute(object)));
63+
}
64+
return false;
65+
}
66+
67+
public static IsMappingNode create() {
68+
return IsMappingNodeGen.create();
69+
}
70+
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/datamodel/IsSequenceNode.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,21 @@
4141
import static com.oracle.graal.python.nodes.SpecialMethodNames.__GETITEM__;
4242
import static com.oracle.graal.python.nodes.SpecialMethodNames.__LEN__;
4343

44-
import com.oracle.graal.python.builtins.objects.PNone;
45-
import com.oracle.graal.python.nodes.attributes.LookupInheritedAttributeNode;
44+
import com.oracle.graal.python.nodes.attributes.HasInheritedAttributeNode;
4645
import com.oracle.truffle.api.dsl.Specialization;
4746
import com.oracle.truffle.api.profiles.ConditionProfile;
4847

4948
public abstract class IsSequenceNode extends PDataModelEmulationNode {
50-
@Child private LookupInheritedAttributeNode getGetItemNode = LookupInheritedAttributeNode.create();
51-
@Child private LookupInheritedAttributeNode getLenNode = LookupInheritedAttributeNode.create();
49+
@Child private HasInheritedAttributeNode hasGetItemNode = HasInheritedAttributeNode.create(__LEN__);
50+
@Child private HasInheritedAttributeNode hasLenNode = HasInheritedAttributeNode.create(__GETITEM__);
51+
5252
private final ConditionProfile lenProfile = ConditionProfile.createBinaryProfile();
5353
private final ConditionProfile getItemProfile = ConditionProfile.createBinaryProfile();
5454

5555
@Specialization
5656
public boolean isSequence(Object object) {
57-
Object len = getLenNode.execute(object, __LEN__);
58-
if (lenProfile.profile(len != PNone.NO_VALUE)) {
59-
return getItemProfile.profile(getGetItemNode.execute(object, __GETITEM__) != PNone.NO_VALUE);
57+
if (lenProfile.profile(hasLenNode.execute(object))) {
58+
return getItemProfile.profile(hasGetItemNode.execute(object));
6059
}
6160
return false;
6261
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/interop/PythonMessageResolution.java

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import com.oracle.graal.python.nodes.attributes.SetAttributeNode;
6262
import com.oracle.graal.python.nodes.call.CallDispatchNode;
6363
import com.oracle.graal.python.nodes.call.special.LookupAndCallUnaryNode;
64+
import com.oracle.graal.python.nodes.datamodel.IsMappingNode;
6465
import com.oracle.graal.python.nodes.datamodel.IsSequenceNode;
6566
import com.oracle.graal.python.nodes.expression.CastToListNode;
6667
import com.oracle.graal.python.nodes.interop.PForeignToPTypeNode;
@@ -120,23 +121,6 @@ public boolean execute(Object object) {
120121
}
121122
}
122123

123-
private static final class IsMapping extends Node {
124-
@Child private LookupInheritedAttributeNode getKeysNode = LookupInheritedAttributeNode.create();
125-
@Child private LookupInheritedAttributeNode getItemsNode = LookupInheritedAttributeNode.create();
126-
@Child private LookupInheritedAttributeNode getValuesNode = LookupInheritedAttributeNode.create();
127-
@Child private IsSequenceNode isSequence = IsSequenceNode.create();
128-
final ConditionProfile profile = ConditionProfile.createBinaryProfile();
129-
130-
public boolean execute(Object object) {
131-
if (isSequence.execute(object)) {
132-
return profile.profile((getKeysNode.execute(object, SpecialMethodNames.KEYS) != PNone.NO_VALUE) &&
133-
(getItemsNode.execute(object, SpecialMethodNames.ITEMS) != PNone.NO_VALUE) &&
134-
(getValuesNode.execute(object, SpecialMethodNames.VALUES) != PNone.NO_VALUE));
135-
}
136-
return false;
137-
}
138-
}
139-
140124
private abstract static class KeyForForcedAccess extends Node {
141125
final ConditionProfile profile = ConditionProfile.createBinaryProfile();
142126
final char prefix;
@@ -212,7 +196,7 @@ public Object execute(Object object, Object key) {
212196
}
213197

214198
private static final class KeysNode extends Node {
215-
@Child private IsMapping isMapping = new IsMapping();
199+
@Child private IsMappingNode isMapping = IsMappingNode.create();
216200
@Child private LookupAndCallUnaryNode keysNode = LookupAndCallUnaryNode.create(SpecialMethodNames.KEYS);
217201
@Child private CastToListNode castToList = CastToListNode.create();
218202
@Child private PythonObjectFactory factory = PythonObjectFactory.create();
@@ -311,7 +295,7 @@ Object access(Object object) {
311295
abstract static class WriteNode extends Node {
312296
@Child private SetItemNode setItemNode = SetItemNode.create();
313297
@Child private SetAttributeNode writeNode = SetAttributeNode.create();
314-
@Child private IsMapping isMapping = new IsMapping();
298+
@Child private IsMappingNode isMapping = IsMappingNode.create();
315299
@Child private HasSetItem hasSetItem = new HasSetItem();
316300
@Child private KeyForAttributeAccess getAttributeKey = new KeyForAttributeAccess();
317301
@Child private KeyForItemAccess getItemKey = new KeyForItemAccess();
@@ -355,7 +339,7 @@ public Object access(Object object, Object field, Object value) {
355339
abstract static class PRemoveNode extends Node {
356340
@Child private DeleteItemNode delItemNode = DeleteItemNode.create();
357341
@Child private DeleteAttributeNode delNode = DeleteAttributeNode.create();
358-
@Child private IsMapping isMapping = new IsMapping();
342+
@Child private IsMappingNode isMapping = IsMappingNode.create();
359343
@Child private HasDelItem hasDelItem = new HasDelItem();
360344
@Child private KeyForAttributeAccess getAttributeKey = new KeyForAttributeAccess();
361345
@Child private KeyForItemAccess getItemKey = new KeyForItemAccess();

0 commit comments

Comments
 (0)