Skip to content

Commit 1a98b1d

Browse files
committed
Migrate from canBeIndex
1 parent 015d59e commit 1a98b1d

File tree

6 files changed

+36
-57
lines changed

6 files changed

+36
-57
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/bytes/ByteArrayBuiltins.java

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import com.oracle.graal.python.builtins.objects.type.PythonBuiltinClass;
6363
import com.oracle.graal.python.builtins.objects.type.TypeBuiltins;
6464
import com.oracle.graal.python.builtins.objects.type.TypeNodes;
65+
import com.oracle.graal.python.lib.PyIndexCheckNode;
6566
import com.oracle.graal.python.lib.PyNumberAsSizeNode;
6667
import com.oracle.graal.python.nodes.ErrorMessages;
6768
import com.oracle.graal.python.nodes.SpecialAttributeNames;
@@ -161,19 +162,13 @@ public PNone doInit(Object self, @SuppressWarnings("unused") Object source, @Sup
161162
@Builtin(name = __GETITEM__, minNumOfPositionalArgs = 2)
162163
@GenerateNodeFactory
163164
abstract static class GetitemNode extends PythonBinaryBuiltinNode {
164-
@Specialization(guards = "isPSlice(key) || lib.canBeIndex(key)", limit = "3")
165+
@Specialization(guards = "isPSlice(key) || indexCheckNode.execute(key)", limit = "1")
165166
static Object doSlice(VirtualFrame frame, PBytesLike self, Object key,
166-
@SuppressWarnings("unused") @CachedLibrary("key") PythonObjectLibrary lib,
167+
@SuppressWarnings("unused") @Cached PyIndexCheckNode indexCheckNode,
167168
@Cached("createGetItem()") SequenceStorageNodes.GetItemNode getSequenceItemNode) {
168169
return getSequenceItemNode.execute(frame, self.getSequenceStorage(), key);
169170
}
170171

171-
@SuppressWarnings("unused")
172-
@Specialization
173-
Object none(VirtualFrame frame, PBytesLike self, PNone key) {
174-
return raise(ValueError, ErrorMessages.BYTESLIKE_OBJ_REQUIRED, key);
175-
}
176-
177172
@SuppressWarnings("unused")
178173
@Fallback
179174
Object doSlice(VirtualFrame frame, Object self, Object key) {
@@ -190,9 +185,9 @@ protected static SequenceStorageNodes.GetItemNode createGetItem() {
190185
@ImportStatic(SpecialMethodNames.class)
191186
abstract static class SetItemNode extends PythonTernaryBuiltinNode {
192187

193-
@Specialization(guards = {"!isPSlice(idx)", "lib.canBeIndex(idx)"}, limit = "3")
188+
@Specialization(guards = {"!isPSlice(idx)", "indexCheckNode.execute(idx)"}, limit = "1")
194189
static PNone doItem(VirtualFrame frame, PByteArray self, Object idx, Object value,
195-
@SuppressWarnings("unused") @CachedLibrary("idx") PythonObjectLibrary lib,
190+
@SuppressWarnings("unused") @Cached PyIndexCheckNode indexCheckNode,
196191
@Cached("createSetItem()") SequenceStorageNodes.SetItemNode setItemNode) {
197192
setItemNode.execute(frame, self.getSequenceStorage(), idx, value);
198193
return PNone.NONE;
@@ -414,12 +409,11 @@ public abstract static class RemoveNode extends PythonBinaryBuiltinNode {
414409

415410
private static final String NOT_IN_BYTEARRAY = "value not found in bytearray";
416411

417-
@Specialization(guards = "lib.canBeIndex(value)")
412+
@Specialization
418413
PNone remove(VirtualFrame frame, PByteArray self, Object value,
419414
@Cached BytesNodes.FindNode findNode,
420415
@Cached SequenceStorageNodes.DeleteNode deleteNode,
421-
@Cached SequenceStorageNodes.LenNode lenNode,
422-
@SuppressWarnings("unused") @CachedLibrary(limit = "getCallSiteInlineCacheMaxDepth()") PythonObjectLibrary lib) {
416+
@Cached SequenceStorageNodes.LenNode lenNode) {
423417
self.checkCanResize(this);
424418
SequenceStorage storage = self.getSequenceStorage();
425419
int len = lenNode.execute(storage);
@@ -500,21 +494,15 @@ protected Object doGeneric(Object self, Object idx) {
500494
@GenerateNodeFactory
501495
public abstract static class AppendNode extends PythonBinaryBuiltinNode {
502496

503-
@Specialization(guards = "lib.canBeIndex(arg)", limit = "3")
497+
@Specialization
504498
public PNone append(VirtualFrame frame, PByteArray byteArray, Object arg,
505499
@Cached("createCast()") CastToByteNode toByteNode,
506-
@Cached SequenceStorageNodes.AppendNode appendNode,
507-
@SuppressWarnings("unused") @CachedLibrary("arg") PythonObjectLibrary lib) {
500+
@Cached SequenceStorageNodes.AppendNode appendNode) {
508501
byteArray.checkCanResize(this);
509502
appendNode.execute(byteArray.getSequenceStorage(), toByteNode.execute(frame, arg), BytesLikeNoGeneralizationNode.SUPPLIER);
510503
return PNone.NONE;
511504
}
512505

513-
@Fallback
514-
public Object doError(@SuppressWarnings("unused") Object list, Object arg) {
515-
throw raise(TypeError, ErrorMessages.OBJ_CANNOT_BE_INTERPRETED_AS_INTEGER, arg);
516-
}
517-
518506
protected CastToByteNode createCast() {
519507
return CastToByteNode.create(val -> {
520508
throw raise(TypeError, ErrorMessages.OBJ_CANNOT_BE_INTERPRETED_AS_INTEGER, "bytes");

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/bytes/BytesBuiltins.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,9 +234,9 @@ public Object byteDone(VirtualFrame frame, PBytes self, Object args) {
234234
@Builtin(name = __GETITEM__, minNumOfPositionalArgs = 2)
235235
@GenerateNodeFactory
236236
abstract static class GetitemNode extends PythonBinaryBuiltinNode {
237-
@Specialization(guards = "isPSlice(key) || lib.canBeIndex(key)", limit = "3")
237+
@Specialization(guards = "isPSlice(key) || indexCheckNode.execute(key)", limit = "1")
238238
Object doSlice(VirtualFrame frame, PBytesLike self, Object key,
239-
@SuppressWarnings("unused") @CachedLibrary("key") PythonObjectLibrary lib,
239+
@SuppressWarnings("unused") @Cached PyIndexCheckNode indexCheckNode,
240240
@Cached("createGetItem()") SequenceStorageNodes.GetItemNode getSequenceItemNode) {
241241
return getSequenceItemNode.execute(frame, self.getSequenceStorage(), key);
242242
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/bytes/BytesNodes.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -761,11 +761,12 @@ protected IterableToByteNode(Function<Object, Object> typeErrorHandler) {
761761
this.typeErrorHandler = typeErrorHandler;
762762
}
763763

764-
@Specialization(guards = "!lib.canBeIndex(iterable)")
764+
@Specialization(guards = "!indexCheckNode.execute(iterable)", limit = "1")
765765
public static byte[] bytearray(VirtualFrame frame, Object iterable, int len,
766+
@SuppressWarnings("unused") @Cached PyIndexCheckNode indexCheckNode,
766767
@Cached GetNextNode getNextNode,
767768
@Cached IsBuiltinClassProfile stopIterationProfile,
768-
@Cached CastToByteNode castToByteNode,
769+
@Cached("createCast()") CastToByteNode castToByteNode,
769770
@CachedLibrary(limit = "3") PythonObjectLibrary lib) {
770771
Object it = lib.getIteratorWithFrame(iterable, frame);
771772
byte[] arr = new byte[len < 16 && len > 0 ? len : 16];

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@
121121
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
122122
import com.oracle.graal.python.builtins.objects.type.TypeNodes.GetNameNode;
123123
import com.oracle.graal.python.builtins.objects.type.TypeNodes.IsTypeNode;
124+
import com.oracle.graal.python.lib.CanBeDoubleNode;
124125
import com.oracle.graal.python.lib.PyFloatAsDoubleNode;
125126
import com.oracle.graal.python.lib.PyIndexCheckNode;
126127
import com.oracle.graal.python.lib.PyNumberIndexNode;
@@ -1803,14 +1804,14 @@ Object execute(Object[] arguments,
18031804
@Cached HPyAsContextNode asContextNode,
18041805
@Cached HPyAsPythonObjectNode asPythonObjectNode,
18051806
@Cached PyIndexCheckNode indexCheckNode,
1807+
@Cached CanBeDoubleNode canBeDoubleNode,
18061808
@Cached LookupInheritedAttributeNode.Dynamic lookup,
1807-
@CachedLibrary(limit = "3") PythonObjectLibrary lib,
18081809
@Cached HPyTransformExceptionToNativeNode transformExceptionToNativeNode) throws ArityException {
18091810
checkArity(arguments, 2);
18101811
GraalHPyContext nativeContext = asContextNode.execute(arguments[0]);
18111812
Object receiver = asPythonObjectNode.execute(nativeContext, arguments[1]);
18121813
try {
1813-
return indexCheckNode.execute(receiver) || lib.canBeJavaDouble(receiver) || lookup.execute(receiver, __INT__) != PNone.NO_VALUE;
1814+
return indexCheckNode.execute(receiver) || canBeDoubleNode.execute(receiver) || lookup.execute(receiver, __INT__) != PNone.NO_VALUE;
18141815
} catch (PException e) {
18151816
transformExceptionToNativeNode.execute(nativeContext, e);
18161817
return nativeContext.getNullHandle();

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/list/ListBuiltins.java

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
import com.oracle.graal.python.builtins.objects.str.PString;
8686
import com.oracle.graal.python.builtins.objects.str.StringUtils;
8787
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
88+
import com.oracle.graal.python.lib.PyIndexCheckNode;
8889
import com.oracle.graal.python.nodes.ErrorMessages;
8990
import com.oracle.graal.python.nodes.PGuards;
9091
import com.oracle.graal.python.nodes.attributes.GetAttributeNode;
@@ -335,17 +336,16 @@ protected Object doGeneric(Object self, @SuppressWarnings("unused") Object objec
335336
@GenerateNodeFactory
336337
public abstract static class GetItemNode extends PythonBinaryBuiltinNode {
337338

338-
@Specialization(guards = "lib.canBeIndex(key) || isPSlice(key)")
339+
@Specialization(guards = "indexCheckNode.execute(key) || isPSlice(key)", limit = "1")
339340
protected Object doScalar(VirtualFrame frame, PList self, Object key,
340-
@Cached("createGetItemNode()") SequenceStorageNodes.GetItemNode getItemNode,
341-
@SuppressWarnings("unused") @CachedLibrary(limit = "1") PythonObjectLibrary lib) {
341+
@SuppressWarnings("unused") @Cached PyIndexCheckNode indexCheckNode,
342+
@Cached("createGetItemNode()") SequenceStorageNodes.GetItemNode getItemNode) {
342343
return getItemNode.execute(frame, self.getSequenceStorage(), key);
343344
}
344345

345346
@SuppressWarnings("unused")
346-
@Specialization(guards = {"!lib.canBeIndex(key)", "!isPSlice(key)"})
347-
public Object doListError(VirtualFrame frame, PList primary, Object key,
348-
@CachedLibrary(limit = "1") PythonObjectLibrary lib) {
347+
@Fallback
348+
public Object doListError(VirtualFrame frame, Object self, Object key) {
349349
throw raise(TypeError, ErrorMessages.OBJ_INDEX_MUST_BE_INT_OR_SLICES, "list", key);
350350
}
351351

@@ -356,12 +356,6 @@ protected static SequenceStorageNodes.GetItemNode createGetItemNode() {
356356
protected static GetItemNode create() {
357357
return ListBuiltinsFactory.GetItemNodeFactory.create();
358358
}
359-
360-
@SuppressWarnings("unused")
361-
@Fallback
362-
protected Object doGeneric(Object self, Object objectIdx) {
363-
throw raise(TypeError, ErrorMessages.DESCRIPTOR_REQUIRES_OBJ, "__getitem__", "list", self);
364-
}
365359
}
366360

367361
@Builtin(name = __SETITEM__, minNumOfPositionalArgs = 3)
@@ -370,25 +364,18 @@ public abstract static class SetItemNode extends PythonTernaryBuiltinNode {
370364

371365
private final ConditionProfile generalizedProfile = ConditionProfile.createBinaryProfile();
372366

373-
@Specialization(guards = "lib.canBeIndex(key) || isPSlice(key)")
367+
@Specialization(guards = "indexCheckNode.execute(key) || isPSlice(key)", limit = "1")
374368
public Object doGeneric(VirtualFrame frame, PList primary, Object key, Object value,
375-
@SuppressWarnings("unused") @CachedLibrary(limit = "3") PythonObjectLibrary lib,
369+
@SuppressWarnings("unused") @Cached PyIndexCheckNode indexCheckNode,
376370
@Cached("createSetItem()") SequenceStorageNodes.SetItemNode setItemNode) {
377371
updateStorage(primary, setItemNode.execute(frame, primary.getSequenceStorage(), key, value));
378372
return PNone.NONE;
379373
}
380374

381-
@SuppressWarnings("unused")
382-
@Specialization(guards = {"!lib.canBeIndex(key)", "!isPSlice(key)"})
383-
public Object doListError(VirtualFrame frame, PList primary, Object key, Object value,
384-
@CachedLibrary(limit = "1") PythonObjectLibrary lib) {
385-
throw raise(TypeError, ErrorMessages.OBJ_INDEX_MUST_BE_INT_OR_SLICES, "list", key);
386-
}
387-
388375
@SuppressWarnings("unused")
389376
@Fallback
390-
protected Object doGeneric(Object self, Object objectIdx, Object value) {
391-
throw raise(TypeError, ErrorMessages.DESCRIPTOR_REQUIRES_OBJ, "__setitem__", "list", self);
377+
protected Object doGeneric(Object self, Object key, Object value) {
378+
throw raise(TypeError, ErrorMessages.OBJ_INDEX_MUST_BE_INT_OR_SLICES, "list", key);
392379
}
393380

394381
private void updateStorage(PList primary, SequenceStorage newStorage) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/subscript/GetItemNode.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import com.oracle.graal.python.builtins.objects.list.PList;
3636
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
3737
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
38+
import com.oracle.graal.python.lib.PyIndexCheckNode;
3839
import com.oracle.graal.python.nodes.ErrorMessages;
3940
import com.oracle.graal.python.nodes.PGuards;
4041
import com.oracle.graal.python.nodes.PRaiseNode;
@@ -90,34 +91,35 @@ public StatementNode makeWriteNode(ExpressionNode rhs) {
9091
return SetItemNode.create(getPrimary(), getSlice(), rhs);
9192
}
9293

93-
@Specialization(guards = {"lib.canBeIndex(index) || isPSlice(index)", "isBuiltinList.profileIsAnyBuiltinObject(primary)"})
94+
@Specialization(guards = {"indexCheckNode.execute(index) || isPSlice(index)", "isBuiltinList.profileIsAnyBuiltinObject(primary)"}, limit = "1")
9495
Object doBuiltinList(VirtualFrame frame, PList primary, Object index,
95-
@SuppressWarnings("unused") @CachedLibrary(limit = "3") PythonObjectLibrary lib,
96+
@SuppressWarnings("unused") @Cached PyIndexCheckNode indexCheckNode,
9697
@Cached("createGetItemNodeForList()") SequenceStorageNodes.GetItemNode getItemNode,
9798
@SuppressWarnings("unused") @Cached IsBuiltinClassProfile isBuiltinList) {
9899
return getItemNode.execute(frame, primary.getSequenceStorage(), index);
99100
}
100101

101102
@SuppressWarnings("unused")
102-
@Specialization(guards = {"!lib.canBeIndex(index)", "!isPSlice(index)"})
103+
@Specialization(guards = {"!indexCheckNode.execute(index)", "!isPSlice(index)"}, limit = "1")
103104
public Object doListError(VirtualFrame frame, PList primary, Object index,
105+
@SuppressWarnings("unused") @Cached PyIndexCheckNode indexCheckNode,
104106
@CachedLibrary(limit = "1") PythonObjectLibrary lib,
105107
@Cached PRaiseNode raiseNode) {
106108
throw raiseNode.raise(TypeError, ErrorMessages.OBJ_INDEX_MUST_BE_INT_OR_SLICES, "list", index);
107109
}
108110

109-
@Specialization(guards = {"lib.canBeIndex(index) || isPSlice(index)", "isBuiltinTuple.profileIsAnyBuiltinObject(primary)"})
111+
@Specialization(guards = {"indexCheckNode.execute(index) || isPSlice(index)", "isBuiltinTuple.profileIsAnyBuiltinObject(primary)"}, limit = "1")
110112
Object doBuiltinTuple(VirtualFrame frame, PTuple primary, Object index,
111-
@SuppressWarnings("unused") @CachedLibrary(limit = "3") PythonObjectLibrary lib,
113+
@SuppressWarnings("unused") @Cached PyIndexCheckNode indexCheckNode,
112114
@Cached("createGetItemNodeForTuple()") SequenceStorageNodes.GetItemNode getItemNode,
113115
@SuppressWarnings("unused") @Cached IsBuiltinClassProfile isBuiltinTuple) {
114116
return getItemNode.execute(frame, primary.getSequenceStorage(), index);
115117
}
116118

117119
@SuppressWarnings("unused")
118-
@Specialization(guards = {"!lib.canBeIndex(index)", "!isPSlice(index)"})
120+
@Specialization(guards = {"!indexCheckNode.execute(index)", "!isPSlice(index)"}, limit = "1")
119121
public Object doTupleError(VirtualFrame frame, PTuple primary, Object index,
120-
@CachedLibrary(limit = "1") PythonObjectLibrary lib,
122+
@SuppressWarnings("unused") @Cached PyIndexCheckNode indexCheckNode,
121123
@Cached PRaiseNode raiseNode) {
122124
throw raiseNode.raise(TypeError, ErrorMessages.OBJ_INDEX_MUST_BE_INT_OR_SLICES, "tuple", index);
123125
}

0 commit comments

Comments
 (0)