41
41
package com .oracle .graal .python .builtins .objects .cext ;
42
42
43
43
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 ;
44
45
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 ;
46
46
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 ;
47
48
import static com .oracle .graal .python .builtins .objects .cext .NativeMemberNames .NB_POW ;
48
49
import static com .oracle .graal .python .builtins .objects .cext .NativeMemberNames .NB_TRUE_DIVIDE ;
49
50
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__ ;
50
53
import static com .oracle .graal .python .nodes .SpecialMethodNames .__INDEX__ ;
51
- import static com .oracle .graal .python .nodes .SpecialMethodNames .__POW__ ;
52
54
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__ ;
54
56
import static com .oracle .graal .python .nodes .SpecialMethodNames .__TRUEDIV__ ;
55
57
56
58
import com .oracle .graal .python .builtins .objects .cext .CExtNodes .ToSulongNode ;
59
+ import com .oracle .graal .python .builtins .objects .cext .PyNumberMethodsWrapperMRFactory .ReadMethodNodeGen ;
57
60
import com .oracle .graal .python .builtins .objects .type .PythonClass ;
61
+ import com .oracle .graal .python .nodes .PNodeWithContext ;
58
62
import com .oracle .graal .python .nodes .attributes .LookupAttributeInMRONode ;
59
63
import com .oracle .truffle .api .CompilerDirectives ;
64
+ import com .oracle .truffle .api .dsl .Cached ;
65
+ import com .oracle .truffle .api .dsl .Specialization ;
60
66
import com .oracle .truffle .api .interop .MessageResolution ;
61
67
import com .oracle .truffle .api .interop .Resolve ;
62
68
import com .oracle .truffle .api .interop .UnknownIdentifierException ;
@@ -67,73 +73,65 @@ public class PyNumberMethodsWrapperMR {
67
73
68
74
@ Resolve (message = "READ" )
69
75
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 ();
75
77
@ Child private ToSulongNode toSulongNode ;
76
78
77
79
public Object access (PyNumberMethodsWrapper object , String key ) {
78
80
// translate key to attribute name
79
81
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 = {"eq(cachedKey, key)" })
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 ) {
81
111
switch (key ) {
82
112
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__ );
89
116
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__ );
96
118
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__ );
103
120
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__ );
110
122
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__ );
117
124
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__ );
124
126
default :
125
127
// TODO extend list
126
128
throw UnknownIdentifierException .raise (key );
127
129
}
128
- return getToSulongNode ().execute (result );
129
130
}
130
131
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 );
137
134
}
138
135
}
136
+
139
137
}
0 commit comments