Skip to content

Commit a9fdac0

Browse files
committed
[GR-64248] Bytecode DSL improvements.
PullRequest: graalpython/4024
2 parents 4fe48b3 + 5d8751a commit a9fdac0

File tree

7 files changed

+142
-126
lines changed

7 files changed

+142
-126
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/array/ArrayBuiltins.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@
5757

5858
import com.oracle.graal.python.PythonLanguage;
5959
import com.oracle.graal.python.annotations.ArgumentClinic;
60+
import com.oracle.graal.python.annotations.Builtin;
6061
import com.oracle.graal.python.annotations.Slot;
6162
import com.oracle.graal.python.annotations.Slot.SlotKind;
6263
import com.oracle.graal.python.annotations.Slot.SlotSignature;
63-
import com.oracle.graal.python.annotations.Builtin;
6464
import com.oracle.graal.python.builtins.CoreFunctions;
6565
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
6666
import com.oracle.graal.python.builtins.PythonBuiltins;
@@ -72,7 +72,8 @@
7272
import com.oracle.graal.python.builtins.objects.buffer.PythonBufferAccessLibrary;
7373
import com.oracle.graal.python.builtins.objects.bytes.PBytes;
7474
import com.oracle.graal.python.builtins.objects.bytes.PBytesLike;
75-
import com.oracle.graal.python.builtins.objects.common.IndexNodes.NormalizeIndexNode;
75+
import com.oracle.graal.python.builtins.objects.common.IndexNodes.NormalizeIndexWithBoundsCheckNode;
76+
import com.oracle.graal.python.builtins.objects.common.IndexNodes.NormalizeIndexWithoutBoundsCheckNode;
7677
import com.oracle.graal.python.builtins.objects.common.SequenceNodes;
7778
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
7879
import com.oracle.graal.python.builtins.objects.function.PKeyword;
@@ -760,21 +761,21 @@ abstract static class SetSubscriptNode extends MpAssSubscriptBuiltinNode {
760761
static void setitem(VirtualFrame frame, PArray self, Object idx, Object value,
761762
@Bind Node inliningTarget,
762763
@Exclusive @Cached PyNumberIndexNode indexNode,
763-
@Shared @Cached("forArrayAssign()") NormalizeIndexNode normalizeIndexNode,
764+
@Shared @Cached NormalizeIndexWithBoundsCheckNode normalizeIndexNode,
764765
@Cached ArrayNodes.PutValueNode putValueNode) {
765-
int index = normalizeIndexNode.execute(indexNode.execute(frame, inliningTarget, idx), self.getLength());
766+
int index = normalizeIndexNode.execute(indexNode.execute(frame, inliningTarget, idx), self.getLength(), ErrorMessages.ARRAY_ASSIGN_OUT_OF_BOUNDS);
766767
putValueNode.execute(frame, inliningTarget, self, index, value);
767768
}
768769

769770
@Specialization(guards = {"!isPSlice(idx)", "isNoValue(value)"})
770771
static void delitem(VirtualFrame frame, PArray self, Object idx, @SuppressWarnings("unused") Object value,
771772
@Bind Node inliningTarget,
772773
@Exclusive @Cached PyNumberIndexNode indexNode,
773-
@Shared @Cached("forArrayAssign()") NormalizeIndexNode normalizeIndexNode,
774+
@Shared @Cached NormalizeIndexWithBoundsCheckNode normalizeIndexNode,
774775
@Exclusive @Cached DeleteArraySliceNode deleteSliceNode,
775776
@Exclusive @Cached PRaiseNode raiseNode) {
776777
self.checkCanResize(inliningTarget, raiseNode);
777-
int index = normalizeIndexNode.execute(indexNode.execute(frame, inliningTarget, idx), self.getLength());
778+
int index = normalizeIndexNode.execute(indexNode.execute(frame, inliningTarget, idx), self.getLength(), ErrorMessages.ARRAY_ASSIGN_OUT_OF_BOUNDS);
778779
deleteSliceNode.execute(inliningTarget, self, index, 1);
779780
}
780781

@@ -1124,12 +1125,12 @@ abstract static class InsertNode extends PythonTernaryClinicBuiltinNode {
11241125
@Specialization
11251126
static Object insert(VirtualFrame frame, PArray self, int inputIndex, Object value,
11261127
@Bind Node inliningTarget,
1127-
@Cached("create(false)") NormalizeIndexNode normalizeIndexNode,
1128+
@Cached NormalizeIndexWithoutBoundsCheckNode normalizeIndexNode,
11281129
@Cached ArrayNodes.CheckValueNode checkValueNode,
11291130
@Cached ArrayNodes.PutValueNode putValueNode,
11301131
@Cached ArrayNodes.ShiftNode shiftNode,
11311132
@Cached PRaiseNode raiseNode) {
1132-
int index = normalizeIndexNode.execute(inputIndex, self.getLength());
1133+
int index = normalizeIndexNode.execute(inputIndex, self.getLength(), ErrorMessages.INDEX_OUT_OF_RANGE);
11331134
if (index > self.getLength()) {
11341135
index = self.getLength();
11351136
} else if (index < 0) {
@@ -1179,14 +1180,14 @@ abstract static class PopNode extends PythonBinaryClinicBuiltinNode {
11791180
@Specialization
11801181
static Object pop(PArray self, int inputIndex,
11811182
@Bind Node inliningTarget,
1182-
@Cached("forPop()") NormalizeIndexNode normalizeIndexNode,
1183+
@Cached NormalizeIndexWithBoundsCheckNode normalizeIndexNode,
11831184
@Cached ArrayNodes.GetValueNode getValueNode,
11841185
@Cached DeleteArraySliceNode deleteSliceNode,
11851186
@Cached PRaiseNode raiseNode) {
11861187
if (self.getLength() == 0) {
11871188
throw raiseNode.raise(inliningTarget, IndexError, ErrorMessages.POP_FROM_EMPTY_ARRAY);
11881189
}
1189-
int index = normalizeIndexNode.execute(inputIndex, self.getLength());
1190+
int index = normalizeIndexNode.execute(inputIndex, self.getLength(), ErrorMessages.POP_INDEX_OUT_OF_RANGE);
11901191
Object value = getValueNode.execute(inliningTarget, self, index);
11911192
self.checkCanResize(inliningTarget, raiseNode);
11921193
deleteSliceNode.execute(inliningTarget, self, index, 1);

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
import com.oracle.graal.python.builtins.objects.bytes.BytesNodes.GetBytesStorage;
5959
import com.oracle.graal.python.builtins.objects.bytes.BytesNodes.HexStringToBytesNode;
6060
import com.oracle.graal.python.builtins.objects.common.IndexNodes.NormalizeIndexNode;
61+
import com.oracle.graal.python.builtins.objects.common.IndexNodes.NormalizeIndexWithBoundsCheckNode;
6162
import com.oracle.graal.python.builtins.objects.common.SequenceNodes;
6263
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
6364
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes.GetInternalByteArrayNode;
@@ -229,18 +230,18 @@ abstract static class SetItemNode extends SqAssItemBuiltinNode {
229230
@Specialization(guards = "!isNoValue(value)")
230231
static void set(PByteArray self, int index, Object value,
231232
@Bind Node inliningTarget,
232-
@Shared @Cached("forBytearray()") NormalizeIndexNode normalizeIndexNode,
233+
@Shared @Cached NormalizeIndexWithBoundsCheckNode normalizeIndexNode,
233234
@Cached SequenceStorageNodes.SetItemScalarNode setItemNode) {
234-
index = normalizeIndexNode.execute(index, self.getSequenceStorage().length());
235+
index = normalizeIndexNode.execute(index, self.getSequenceStorage().length(), ErrorMessages.BYTEARRAY_OUT_OF_BOUNDS);
235236
setItemNode.execute(inliningTarget, self.getSequenceStorage(), index, value);
236237
}
237238

238239
@Specialization(guards = "isNoValue(value)")
239240
static void del(PByteArray self, int index, @SuppressWarnings("unused") Object value,
240241
@Bind Node inliningTarget,
241-
@Shared @Cached("forBytearray()") NormalizeIndexNode normalizeIndexNode,
242+
@Shared @Cached NormalizeIndexWithBoundsCheckNode normalizeIndexNode,
242243
@Cached SequenceStorageNodes.DeleteItemNode deleteItemNode) {
243-
index = normalizeIndexNode.execute(index, self.getSequenceStorage().length());
244+
index = normalizeIndexNode.execute(index, self.getSequenceStorage().length(), ErrorMessages.BYTEARRAY_OUT_OF_BOUNDS);
244245
deleteItemNode.execute(inliningTarget, self.getSequenceStorage(), index);
245246
}
246247
}
@@ -254,12 +255,12 @@ static void set(VirtualFrame frame, PByteArray self, Object indexObj, Object val
254255
@Bind Node inliningTarget,
255256
@Cached PyIndexCheckNode indexCheckNode,
256257
@Cached PyNumberAsSizeNode asSizeNode,
257-
@Cached("forBytearray()") NormalizeIndexNode normalizeIndexNode,
258+
@Cached NormalizeIndexWithBoundsCheckNode normalizeIndexNode,
258259
@Cached SequenceStorageNodes.SetItemScalarNode setItemNode,
259260
@Cached @Exclusive PRaiseNode raiseNode) {
260261
if (indexCheckNode.execute(inliningTarget, indexObj)) {
261262
int index = asSizeNode.executeExact(frame, inliningTarget, indexObj);
262-
index = normalizeIndexNode.execute(index, self.getSequenceStorage().length());
263+
index = normalizeIndexNode.execute(index, self.getSequenceStorage().length(), ErrorMessages.BYTEARRAY_OUT_OF_BOUNDS);
263264
setItemNode.execute(inliningTarget, self.getSequenceStorage(), index, value);
264265
} else {
265266
throw raiseNode.raise(inliningTarget, TypeError, ErrorMessages.OBJ_INDEX_MUST_BE_INT_OR_SLICES, "bytearray", indexObj);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/common/IndexNodes.java

Lines changed: 22 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -106,16 +106,6 @@ public static NormalizeIndexNode create(TruffleString errorMessage) {
106106
return new NormalizeIndexNode(errorMessage, true);
107107
}
108108

109-
@NeverDefault
110-
public static NormalizeIndexNode create(boolean boundsCheck) {
111-
return new NormalizeIndexNode(ErrorMessages.INDEX_OUT_OF_RANGE, boundsCheck);
112-
}
113-
114-
@NeverDefault
115-
public static NormalizeIndexNode create(TruffleString errorMessage, boolean boundsCheck) {
116-
return new NormalizeIndexNode(errorMessage, boundsCheck);
117-
}
118-
119109
@NeverDefault
120110
public static NormalizeIndexNode forList() {
121111
return create(ErrorMessages.LIST_INDEX_OUT_OF_RANGE);
@@ -130,31 +120,6 @@ public static NormalizeIndexNode forListAssign() {
130120
public static NormalizeIndexNode forTuple() {
131121
return create(ErrorMessages.TUPLE_OUT_OF_BOUNDS);
132122
}
133-
134-
@NeverDefault
135-
public static NormalizeIndexNode forArray() {
136-
return create(ErrorMessages.ARRAY_OUT_OF_BOUNDS);
137-
}
138-
139-
@NeverDefault
140-
public static NormalizeIndexNode forArrayAssign() {
141-
return create(ErrorMessages.ARRAY_ASSIGN_OUT_OF_BOUNDS);
142-
}
143-
144-
@NeverDefault
145-
public static NormalizeIndexNode forPop() {
146-
return create(ErrorMessages.POP_INDEX_OUT_OF_RANGE);
147-
}
148-
149-
@NeverDefault
150-
public static NormalizeIndexNode forRange() {
151-
return create(ErrorMessages.RANGE_OUT_OF_BOUNDS);
152-
}
153-
154-
@NeverDefault
155-
public static NormalizeIndexNode forBytearray() {
156-
return create(ErrorMessages.BYTEARRAY_OUT_OF_BOUNDS);
157-
}
158123
}
159124

160125
public abstract static class NormalizeIndexCustomMessageNode extends Node {
@@ -177,17 +142,22 @@ public static NormalizeIndexCustomMessageNode create() {
177142
public static NormalizeIndexCustomMessageNode getUncached() {
178143
return NormalizeIndexWithBoundsCheckNodeGen.getUncached();
179144
}
180-
181-
@NeverDefault
182-
public static NormalizeIndexCustomMessageNode getUncachedWithoutBoundsCheck() {
183-
return NormalizeIndexWithoutBoundsCheckNodeGen.getUncached();
184-
}
185145
}
186146

187147
@GenerateUncached
188148
@GenerateInline(false) // footprint reduction 28 -> 10
189149
// not inlined because it is always created dynamically by NormalizeIndexNode
190-
abstract static class NormalizeIndexWithBoundsCheckNode extends NormalizeIndexCustomMessageNode {
150+
public abstract static class NormalizeIndexWithBoundsCheckNode extends NormalizeIndexCustomMessageNode {
151+
152+
@NeverDefault
153+
public static NormalizeIndexWithBoundsCheckNode create() {
154+
return NormalizeIndexWithBoundsCheckNodeGen.create();
155+
}
156+
157+
@NeverDefault
158+
public static NormalizeIndexWithBoundsCheckNode getUncached() {
159+
return NormalizeIndexWithBoundsCheckNodeGen.getUncached();
160+
}
191161

192162
@Specialization
193163
static int doInt(int index, int length, TruffleString errorMessage,
@@ -272,7 +242,17 @@ static long doLongLong(long lIndex, long length, TruffleString errorMessage,
272242
@GenerateUncached
273243
@GenerateInline(false) // footprint reduction 24 -> 6
274244
// not inlined because it is always created dynamically by NormalizeIndexNode
275-
abstract static class NormalizeIndexWithoutBoundsCheckNode extends NormalizeIndexCustomMessageNode {
245+
public abstract static class NormalizeIndexWithoutBoundsCheckNode extends NormalizeIndexCustomMessageNode {
246+
247+
@NeverDefault
248+
public static NormalizeIndexWithoutBoundsCheckNode create() {
249+
return NormalizeIndexWithoutBoundsCheckNodeGen.create();
250+
}
251+
252+
@NeverDefault
253+
public static NormalizeIndexWithoutBoundsCheckNode getUncached() {
254+
return NormalizeIndexWithoutBoundsCheckNodeGen.getUncached();
255+
}
276256

277257
@Specialization
278258
static int doInt(int index, int length, @SuppressWarnings("unused") TruffleString errorMessage,

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

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
import com.oracle.graal.python.nodes.interop.PForeignToPTypeNode;
3535
import com.oracle.graal.python.runtime.GilNode;
3636
import com.oracle.graal.python.runtime.exception.PException;
37-
import com.oracle.graal.python.runtime.sequence.PSequence;
37+
import com.oracle.graal.python.runtime.sequence.PSequenceWithStorage;
3838
import com.oracle.graal.python.runtime.sequence.storage.ArrayBasedSequenceStorage;
3939
import com.oracle.graal.python.runtime.sequence.storage.SequenceStorage;
4040
import com.oracle.graal.python.util.OverflowException;
@@ -57,9 +57,8 @@
5757

5858
@SuppressWarnings("truffle-abstract-export")
5959
@ExportLibrary(InteropLibrary.class)
60-
public final class PList extends PSequence {
60+
public final class PList extends PSequenceWithStorage {
6161
private final ListOrigin origin;
62-
private SequenceStorage store;
6362

6463
public PList(Object cls, Shape instanceShape, SequenceStorage store) {
6564
super(builtinClassToType(cls), instanceShape);
@@ -73,16 +72,6 @@ public PList(Object cls, Shape instanceShape, SequenceStorage store, ListOrigin
7372
this.store = store;
7473
}
7574

76-
@Override
77-
public SequenceStorage getSequenceStorage() {
78-
return store;
79-
}
80-
81-
@Override
82-
public void setSequenceStorage(SequenceStorage newStorage) {
83-
this.store = newStorage;
84-
}
85-
8675
@Override
8776
public String toString() {
8877
CompilerAsserts.neverPartOfCompilation();

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/tuple/PTuple.java

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@
2727

2828
import static com.oracle.graal.python.util.PythonUtils.builtinClassToType;
2929

30-
import com.oracle.graal.python.runtime.sequence.PSequence;
31-
import com.oracle.graal.python.runtime.sequence.storage.MroSequenceStorage;
30+
import com.oracle.graal.python.runtime.sequence.PSequenceWithStorage;
3231
import com.oracle.graal.python.runtime.sequence.storage.ObjectSequenceStorage;
3332
import com.oracle.graal.python.runtime.sequence.storage.SequenceStorage;
3433
import com.oracle.truffle.api.CompilerAsserts;
@@ -41,9 +40,8 @@
4140

4241
@SuppressWarnings("truffle-abstract-export")
4342
@ExportLibrary(InteropLibrary.class)
44-
public final class PTuple extends PSequence {
43+
public final class PTuple extends PSequenceWithStorage {
4544

46-
private SequenceStorage store;
4745
private long hash = -1;
4846

4947
public PTuple(Object cls, Shape instanceShape, Object[] elements) {
@@ -62,17 +60,6 @@ public String toString() {
6260
return String.format("tuple(%s)", store);
6361
}
6462

65-
@Override
66-
public SequenceStorage getSequenceStorage() {
67-
return store;
68-
}
69-
70-
@Override
71-
public void setSequenceStorage(SequenceStorage store) {
72-
assert !(this.store instanceof MroSequenceStorage) : "attempting to replace MroSequenceStorage";
73-
this.store = store;
74-
}
75-
7663
public long getHash() {
7764
return hash;
7865
}

0 commit comments

Comments
 (0)