81
81
import com .oracle .graal .python .builtins .objects .common .SequenceStorageNodesFactory .DeleteSliceNodeGen ;
82
82
import com .oracle .graal .python .builtins .objects .common .SequenceStorageNodesFactory .EnsureCapacityNodeGen ;
83
83
import com .oracle .graal .python .builtins .objects .common .SequenceStorageNodesFactory .ExtendNodeGen ;
84
+ import com .oracle .graal .python .builtins .objects .common .SequenceStorageNodesFactory .GetElementTypeNodeGen ;
84
85
import com .oracle .graal .python .builtins .objects .common .SequenceStorageNodesFactory .GetItemNodeGen ;
85
86
import com .oracle .graal .python .builtins .objects .common .SequenceStorageNodesFactory .GetItemScalarNodeGen ;
86
87
import com .oracle .graal .python .builtins .objects .common .SequenceStorageNodesFactory .GetItemSliceNodeGen ;
88
+ import com .oracle .graal .python .builtins .objects .common .SequenceStorageNodesFactory .ItemIndexNodeGen ;
87
89
import com .oracle .graal .python .builtins .objects .common .SequenceStorageNodesFactory .LenNodeGen ;
88
90
import com .oracle .graal .python .builtins .objects .common .SequenceStorageNodesFactory .ListGeneralizationNodeGen ;
89
91
import com .oracle .graal .python .builtins .objects .common .SequenceStorageNodesFactory .NoGeneralizationNodeGen ;
104
106
import com .oracle .graal .python .builtins .objects .str .PString ;
105
107
import com .oracle .graal .python .builtins .objects .tuple .PTuple ;
106
108
import com .oracle .graal .python .nodes .PBaseNode ;
109
+ import com .oracle .graal .python .nodes .SpecialMethodNames ;
107
110
import com .oracle .graal .python .nodes .builtins .ListNodes ;
108
111
import com .oracle .graal .python .nodes .control .GetIteratorNode ;
109
112
import com .oracle .graal .python .nodes .control .GetNextNode ;
@@ -152,6 +155,8 @@ public abstract class SequenceStorageNodes {
152
155
153
156
abstract static class SequenceStorageBaseNode extends PBaseNode {
154
157
158
+ @ Child private GetElementType getElementTypeNode ;
159
+
155
160
protected static final int DEFAULT_CAPACITY = 8 ;
156
161
157
162
protected static final int MAX_SEQUENCE_STORAGES = 12 ;
@@ -163,7 +168,7 @@ protected static boolean isByteStorage(NativeSequenceStorage store) {
163
168
/**
164
169
* Tests if {@code left} has the same element type as {@code right}.
165
170
*/
166
- protected static boolean compatible (SequenceStorage left , NativeSequenceStorage right ) {
171
+ protected boolean compatible (SequenceStorage left , NativeSequenceStorage right ) {
167
172
switch (right .getElementType ()) {
168
173
case Boolean :
169
174
return left instanceof BoolSequenceStorage ;
@@ -187,9 +192,9 @@ protected static boolean compatible(SequenceStorage left, NativeSequenceStorage
187
192
/**
188
193
* Tests if each element of {@code rhs} can be assign to {@code lhs} without casting.
189
194
*/
190
- protected static boolean compatibleAssign (SequenceStorage lhs , SequenceStorage rhs ) {
191
- ListStorageType rhsType = rhs . getElementType ();
192
- switch (lhs . getElementType ()) {
195
+ protected boolean compatibleAssign (SequenceStorage lhs , SequenceStorage rhs ) {
196
+ ListStorageType rhsType = getElementType (rhs );
197
+ switch (getElementType (lhs )) {
193
198
case Boolean :
194
199
return rhsType == Boolean || rhsType == Uninitialized ;
195
200
case Byte :
@@ -218,9 +223,9 @@ protected static boolean compatibleAssign(SequenceStorage lhs, SequenceStorage r
218
223
/**
219
224
* Tests if elements of {@code rhs} can be assign to {@code lhs} with casting.
220
225
*/
221
- protected static boolean compatibleDataType (SequenceStorage lhs , SequenceStorage rhs ) {
222
- ListStorageType rhsType = rhs . getElementType ();
223
- switch (lhs . getElementType ()) {
226
+ protected boolean compatibleDataType (SequenceStorage lhs , SequenceStorage rhs ) {
227
+ ListStorageType rhsType = getElementType (rhs );
228
+ switch (getElementType (lhs )) {
224
229
case Boolean :
225
230
case Byte :
226
231
case Int :
@@ -252,44 +257,52 @@ protected boolean isEmpty(SequenceStorage left) {
252
257
return left instanceof EmptySequenceStorage || left .length () == 0 ;
253
258
}
254
259
255
- protected static boolean isBoolean (SequenceStorage s ) {
256
- return s .getElementType () == ListStorageType .Boolean ;
260
+ private ListStorageType getElementType (SequenceStorage s ) {
261
+ if (getElementTypeNode == null ) {
262
+ CompilerDirectives .transferToInterpreterAndInvalidate ();
263
+ getElementTypeNode = insert (GetElementTypeNodeGen .create ());
264
+ }
265
+ return getElementTypeNode .execute (s );
266
+ }
267
+
268
+ protected boolean isBoolean (SequenceStorage s ) {
269
+ return getElementType (s ) == ListStorageType .Boolean ;
257
270
}
258
271
259
- protected static boolean isByte (SequenceStorage s ) {
260
- return s . getElementType () == ListStorageType .Byte ;
272
+ protected boolean isByte (SequenceStorage s ) {
273
+ return getElementType (s ) == ListStorageType .Byte ;
261
274
}
262
275
263
- protected static boolean isByteLike (SequenceStorage s ) {
276
+ protected boolean isByteLike (SequenceStorage s ) {
264
277
return isByte (s ) || isInt (s ) || isLong (s );
265
278
}
266
279
267
- protected static boolean isChar (SequenceStorage s ) {
268
- return s . getElementType () == ListStorageType .Char ;
280
+ protected boolean isChar (SequenceStorage s ) {
281
+ return getElementType (s ) == ListStorageType .Char ;
269
282
}
270
283
271
- protected static boolean isInt (SequenceStorage s ) {
272
- return s . getElementType () == ListStorageType .Int ;
284
+ protected boolean isInt (SequenceStorage s ) {
285
+ return getElementType (s ) == ListStorageType .Int ;
273
286
}
274
287
275
- protected static boolean isLong (SequenceStorage s ) {
276
- return s . getElementType () == ListStorageType .Long ;
288
+ protected boolean isLong (SequenceStorage s ) {
289
+ return getElementType (s ) == ListStorageType .Long ;
277
290
}
278
291
279
- protected static boolean isDouble (SequenceStorage s ) {
280
- return s . getElementType () == ListStorageType .Double ;
292
+ protected boolean isDouble (SequenceStorage s ) {
293
+ return getElementType (s ) == ListStorageType .Double ;
281
294
}
282
295
283
- protected static boolean isObject (SequenceStorage s ) {
284
- return s . getElementType () == ListStorageType .Generic ;
296
+ protected boolean isObject (SequenceStorage s ) {
297
+ return getElementType (s ) == ListStorageType .Generic ;
285
298
}
286
299
287
- protected static boolean isTuple (SequenceStorage s ) {
288
- return s . getElementType () == ListStorageType .Tuple ;
300
+ protected boolean isTuple (SequenceStorage s ) {
301
+ return getElementType (s ) == ListStorageType .Tuple ;
289
302
}
290
303
291
- protected static boolean isList (SequenceStorage s ) {
292
- return s . getElementType () == ListStorageType .List ;
304
+ protected boolean isList (SequenceStorage s ) {
305
+ return getElementType (s ) == ListStorageType .List ;
293
306
}
294
307
295
308
protected static boolean hasStorage (Object source ) {
@@ -478,6 +491,8 @@ public static GetItemScalarNode create() {
478
491
479
492
public abstract byte executeByte (SequenceStorage s , int idx );
480
493
494
+ public abstract char executeChar (SequenceStorage s , int idx );
495
+
481
496
public abstract int executeInt (SequenceStorage s , int idx );
482
497
483
498
public abstract long executeLong (SequenceStorage s , int idx );
@@ -2362,10 +2377,6 @@ SequenceStorage doGeneric(@SuppressWarnings("unused") SequenceStorage s, @Suppre
2362
2377
throw raise (TypeError , errorMessage );
2363
2378
}
2364
2379
2365
- protected static boolean compatibleAssign (SequenceStorage lhs , SequenceStorage rhs ) {
2366
- return SequenceStorageBaseNode .compatibleAssign (lhs , rhs );
2367
- }
2368
-
2369
2380
public static NoGeneralizationNode create (String invalidItemErrorMessage ) {
2370
2381
return NoGeneralizationNodeGen .create (invalidItemErrorMessage );
2371
2382
}
@@ -2503,10 +2514,6 @@ private ValueProfile getSelfProfile() {
2503
2514
return selfProfile ;
2504
2515
}
2505
2516
2506
- protected static boolean compatibleAssign (SequenceStorage lhs , SequenceStorage rhs ) {
2507
- return SequenceStorageBaseNode .compatibleAssign (lhs , rhs );
2508
- }
2509
-
2510
2517
public static ListGeneralizationNode create () {
2511
2518
return ListGeneralizationNodeGen .create ();
2512
2519
}
@@ -2885,4 +2892,131 @@ protected static DeleteSliceNode create() {
2885
2892
return DeleteSliceNodeGen .create ();
2886
2893
}
2887
2894
}
2895
+
2896
+ abstract static class GetElementType extends PBaseNode {
2897
+
2898
+ public abstract ListStorageType execute (SequenceStorage s );
2899
+
2900
+ @ Specialization (limit = "cacheLimit()" , guards = {"s.getClass() == cachedClass" })
2901
+ ListStorageType doCached (SequenceStorage s ,
2902
+ @ Cached ("s.getClass()" ) Class <? extends SequenceStorage > cachedClass ) {
2903
+ return cachedClass .cast (s ).getElementType ();
2904
+ }
2905
+
2906
+ protected static int cacheLimit () {
2907
+ return SequenceStorageBaseNode .MAX_SEQUENCE_STORAGES ;
2908
+ }
2909
+ }
2910
+
2911
+ @ ImportStatic (SpecialMethodNames .class )
2912
+ public abstract static class ItemIndexNode extends SequenceStorageBaseNode {
2913
+
2914
+ @ Child private GetItemScalarNode getItemNode ;
2915
+ @ Child private LenNode lenNode ;
2916
+
2917
+ public abstract int execute (SequenceStorage s , Object item , int start , int end );
2918
+
2919
+ public abstract int execute (SequenceStorage s , boolean item , int start , int end );
2920
+
2921
+ public abstract int execute (SequenceStorage s , char item , int start , int end );
2922
+
2923
+ public abstract int execute (SequenceStorage s , int item , int start , int end );
2924
+
2925
+ public abstract int execute (SequenceStorage s , long item , int start , int end );
2926
+
2927
+ public abstract int execute (SequenceStorage s , double item , int start , int end );
2928
+
2929
+ @ Specialization (guards = "isBoolean(s)" )
2930
+ int doBoolean (SequenceStorage s , boolean item , int start , int end ) {
2931
+ for (int i = start ; i < getLength (s , end ); i ++) {
2932
+ if (getItemNode ().executeBoolean (s , i ) == item ) {
2933
+ return i ;
2934
+ }
2935
+ }
2936
+ return -1 ;
2937
+ }
2938
+
2939
+ @ Specialization (guards = "isByte(s)" )
2940
+ int doByte (SequenceStorage s , byte item , int start , int end ) {
2941
+ for (int i = start ; i < getLength (s , end ); i ++) {
2942
+ if (getItemNode ().executeByte (s , i ) == item ) {
2943
+ return i ;
2944
+ }
2945
+ }
2946
+ return -1 ;
2947
+ }
2948
+
2949
+ @ Specialization (guards = "isChar(s)" )
2950
+ int doChar (SequenceStorage s , char item , int start , int end ) {
2951
+ for (int i = start ; i < getLength (s , end ); i ++) {
2952
+ if (getItemNode ().executeChar (s , i ) == item ) {
2953
+ return i ;
2954
+ }
2955
+ }
2956
+ return -1 ;
2957
+ }
2958
+
2959
+ @ Specialization (guards = "isInt(s)" )
2960
+ int doInt (SequenceStorage s , int item , int start , int end ) {
2961
+ for (int i = start ; i < getLength (s , end ); i ++) {
2962
+ if (getItemNode ().executeInt (s , i ) == item ) {
2963
+ return i ;
2964
+ }
2965
+ }
2966
+ return -1 ;
2967
+ }
2968
+
2969
+ @ Specialization (guards = "isLong(s)" )
2970
+ int doLong (SequenceStorage s , long item , int start , int end ) {
2971
+ for (int i = start ; i < getLength (s , end ); i ++) {
2972
+ if (getItemNode ().executeLong (s , i ) == item ) {
2973
+ return i ;
2974
+ }
2975
+ }
2976
+ return -1 ;
2977
+ }
2978
+
2979
+ @ Specialization (guards = "isDouble(s)" )
2980
+ int doDouble (SequenceStorage s , double item , int start , int end ) {
2981
+ for (int i = start ; i < getLength (s , end ); i ++) {
2982
+ if (getItemNode ().executeDouble (s , i ) == item ) {
2983
+ return i ;
2984
+ }
2985
+ }
2986
+ return -1 ;
2987
+ }
2988
+
2989
+ @ Specialization
2990
+ int doGeneric (SequenceStorage s , Object item , int start , int end ,
2991
+ @ Cached ("createIfTrueNode()" ) CastToBooleanNode castToBooleanNode ,
2992
+ @ Cached ("create(__EQ__, __EQ__, __EQ__)" ) BinaryComparisonNode eqNode ) {
2993
+ for (int i = start ; i < getLength (s , end ); i ++) {
2994
+ Object object = getItemNode ().execute (s , i );
2995
+ if (castToBooleanNode .executeWith (eqNode .executeWith (object , item ))) {
2996
+ return i ;
2997
+ }
2998
+ }
2999
+ return -1 ;
3000
+ }
3001
+
3002
+ private GetItemScalarNode getItemNode () {
3003
+ if (getItemNode == null ) {
3004
+ CompilerDirectives .transferToInterpreterAndInvalidate ();
3005
+ getItemNode = insert (GetItemScalarNode .create ());
3006
+ }
3007
+ return getItemNode ;
3008
+ }
3009
+
3010
+ private int getLength (SequenceStorage s , int end ) {
3011
+ if (lenNode == null ) {
3012
+ CompilerDirectives .transferToInterpreterAndInvalidate ();
3013
+ lenNode = insert (LenNode .create ());
3014
+ }
3015
+ return Math .min (lenNode .execute (s ), end );
3016
+ }
3017
+
3018
+ public static ItemIndexNode create () {
3019
+ return ItemIndexNodeGen .create ();
3020
+ }
3021
+ }
2888
3022
}
0 commit comments