Skip to content

Commit 2892631

Browse files
committed
Add MR for number method 'nb_and'.
1 parent 9b1d19d commit 2892631

File tree

2 files changed

+51
-52
lines changed

2 files changed

+51
-52
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/NativeMemberNames.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ public final class NativeMemberNames {
9595
public static final String BUF_DELEGATE = "buf_delegate";
9696
public static final String BUF_READONLY = "readonly";
9797
public static final String NB_ADD = "nb_add";
98+
public static final String NB_AND = "nb_and";
9899
public static final String NB_INDEX = "nb_index";
99100
public static final String NB_POW = "nb_power";
100101
public static final String NB_TRUE_DIVIDE = "nb_true_divide";

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/PyNumberMethodsWrapperMR.java

Lines changed: 50 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,28 @@
4141
package com.oracle.graal.python.builtins.objects.cext;
4242

4343
import static com.oracle.graal.python.builtins.objects.cext.NativeMemberNames.NB_ADD;
44+
import static com.oracle.graal.python.builtins.objects.cext.NativeMemberNames.NB_AND;
4445
import static com.oracle.graal.python.builtins.objects.cext.NativeMemberNames.NB_INDEX;
45-
import static com.oracle.graal.python.builtins.objects.cext.NativeMemberNames.NB_MULTIPLY;
4646
import static com.oracle.graal.python.builtins.objects.cext.NativeMemberNames.NB_INPLACE_MULTIPLY;
47+
import static com.oracle.graal.python.builtins.objects.cext.NativeMemberNames.NB_MULTIPLY;
4748
import static com.oracle.graal.python.builtins.objects.cext.NativeMemberNames.NB_POW;
4849
import static com.oracle.graal.python.builtins.objects.cext.NativeMemberNames.NB_TRUE_DIVIDE;
4950
import static com.oracle.graal.python.nodes.SpecialMethodNames.__ADD__;
51+
import static com.oracle.graal.python.nodes.SpecialMethodNames.__AND__;
52+
import static com.oracle.graal.python.nodes.SpecialMethodNames.__IMUL__;
5053
import static com.oracle.graal.python.nodes.SpecialMethodNames.__INDEX__;
51-
import static com.oracle.graal.python.nodes.SpecialMethodNames.__POW__;
5254
import static com.oracle.graal.python.nodes.SpecialMethodNames.__MUL__;
53-
import static com.oracle.graal.python.nodes.SpecialMethodNames.__IMUL__;
55+
import static com.oracle.graal.python.nodes.SpecialMethodNames.__POW__;
5456
import static com.oracle.graal.python.nodes.SpecialMethodNames.__TRUEDIV__;
5557

5658
import com.oracle.graal.python.builtins.objects.cext.CExtNodes.ToSulongNode;
59+
import com.oracle.graal.python.builtins.objects.cext.PyNumberMethodsWrapperMRFactory.ReadMethodNodeGen;
5760
import com.oracle.graal.python.builtins.objects.type.PythonClass;
61+
import com.oracle.graal.python.nodes.PNodeWithContext;
5862
import com.oracle.graal.python.nodes.attributes.LookupAttributeInMRONode;
5963
import com.oracle.truffle.api.CompilerDirectives;
64+
import com.oracle.truffle.api.dsl.Cached;
65+
import com.oracle.truffle.api.dsl.Specialization;
6066
import com.oracle.truffle.api.interop.MessageResolution;
6167
import com.oracle.truffle.api.interop.Resolve;
6268
import com.oracle.truffle.api.interop.UnknownIdentifierException;
@@ -67,73 +73,65 @@ public class PyNumberMethodsWrapperMR {
6773

6874
@Resolve(message = "READ")
6975
abstract static class ReadNode extends Node {
70-
@Child private LookupAttributeInMRONode getAddAttributeNode;
71-
@Child private LookupAttributeInMRONode getIndexAttributeNode;
72-
@Child private LookupAttributeInMRONode getPowAttributeNode;
73-
@Child private LookupAttributeInMRONode getMulAttributeNode;
74-
@Child private LookupAttributeInMRONode getTrueDivAttributeNode;
76+
@Child private ReadMethodNode readMethodNode = ReadMethodNodeGen.create();
7577
@Child private ToSulongNode toSulongNode;
7678

7779
public Object access(PyNumberMethodsWrapper object, String key) {
7880
// translate key to attribute name
7981
PythonClass delegate = object.getDelegate();
80-
Object result;
82+
return getToSulongNode().execute(readMethodNode.execute(delegate, key));
83+
}
84+
85+
private ToSulongNode getToSulongNode() {
86+
if (toSulongNode == null) {
87+
CompilerDirectives.transferToInterpreterAndInvalidate();
88+
toSulongNode = insert(ToSulongNode.create());
89+
}
90+
return toSulongNode;
91+
}
92+
}
93+
94+
abstract static class ReadMethodNode extends PNodeWithContext {
95+
96+
public abstract Object execute(PythonClass clazz, String key);
97+
98+
@Specialization(limit = "99", guards = {"key == cachedKey"})
99+
Object getMethod(PythonClass clazz, @SuppressWarnings("unused") String key,
100+
@Cached("key") @SuppressWarnings("unused") String cachedKey,
101+
@Cached("createLookupNode(cachedKey)") LookupAttributeInMRONode lookupNode) {
102+
if (lookupNode != null) {
103+
return lookupNode.execute(clazz);
104+
}
105+
// TODO extend list
106+
CompilerDirectives.transferToInterpreter();
107+
throw UnknownIdentifierException.raise(key);
108+
}
109+
110+
protected LookupAttributeInMRONode createLookupNode(String key) {
81111
switch (key) {
82112
case NB_ADD:
83-
if (getAddAttributeNode == null) {
84-
CompilerDirectives.transferToInterpreterAndInvalidate();
85-
getAddAttributeNode = insert(LookupAttributeInMRONode.create(__ADD__));
86-
}
87-
result = getAddAttributeNode.execute(delegate);
88-
break;
113+
return LookupAttributeInMRONode.create(__ADD__);
114+
case NB_AND:
115+
return LookupAttributeInMRONode.create(__AND__);
89116
case NB_INDEX:
90-
if (getIndexAttributeNode == null) {
91-
CompilerDirectives.transferToInterpreterAndInvalidate();
92-
getIndexAttributeNode = insert(LookupAttributeInMRONode.create(__INDEX__));
93-
}
94-
result = getIndexAttributeNode.execute(delegate);
95-
break;
117+
return LookupAttributeInMRONode.create(__INDEX__);
96118
case NB_POW:
97-
if (getPowAttributeNode == null) {
98-
CompilerDirectives.transferToInterpreterAndInvalidate();
99-
getPowAttributeNode = insert(LookupAttributeInMRONode.create(__POW__));
100-
}
101-
result = getPowAttributeNode.execute(delegate);
102-
break;
119+
return LookupAttributeInMRONode.create(__POW__);
103120
case NB_TRUE_DIVIDE:
104-
if (getTrueDivAttributeNode == null) {
105-
CompilerDirectives.transferToInterpreterAndInvalidate();
106-
getTrueDivAttributeNode = insert(LookupAttributeInMRONode.create(__TRUEDIV__));
107-
}
108-
result = getTrueDivAttributeNode.execute(delegate);
109-
break;
121+
return LookupAttributeInMRONode.create(__TRUEDIV__);
110122
case NB_MULTIPLY:
111-
if (getMulAttributeNode == null) {
112-
CompilerDirectives.transferToInterpreterAndInvalidate();
113-
getMulAttributeNode = insert(LookupAttributeInMRONode.create(__MUL__));
114-
}
115-
result = getMulAttributeNode.execute(delegate);
116-
break;
123+
return LookupAttributeInMRONode.create(__MUL__);
117124
case NB_INPLACE_MULTIPLY:
118-
if (getMulAttributeNode == null) {
119-
CompilerDirectives.transferToInterpreterAndInvalidate();
120-
getMulAttributeNode = insert(LookupAttributeInMRONode.create(__IMUL__));
121-
}
122-
result = getMulAttributeNode.execute(delegate);
123-
break;
125+
return LookupAttributeInMRONode.create(__IMUL__);
124126
default:
125127
// TODO extend list
126128
throw UnknownIdentifierException.raise(key);
127129
}
128-
return getToSulongNode().execute(result);
129130
}
130131

131-
private ToSulongNode getToSulongNode() {
132-
if (toSulongNode == null) {
133-
CompilerDirectives.transferToInterpreterAndInvalidate();
134-
toSulongNode = insert(ToSulongNode.create());
135-
}
136-
return toSulongNode;
132+
protected static boolean eq(String expected, String actual) {
133+
return expected.equals(actual);
137134
}
138135
}
136+
139137
}

0 commit comments

Comments
 (0)