Skip to content

Commit 291d303

Browse files
committed
Finally get rid of interface 'PLenSupplier'.
1 parent 38e885e commit 291d303

34 files changed

+503
-226
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/ArrayModuleBuiltins.java

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,15 @@
3737
import com.oracle.graal.python.builtins.PythonBuiltins;
3838
import com.oracle.graal.python.builtins.objects.PNone;
3939
import com.oracle.graal.python.builtins.objects.array.PArray;
40+
import com.oracle.graal.python.builtins.objects.common.SequenceNodes;
4041
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes.CastToByteNode;
4142
import com.oracle.graal.python.builtins.objects.range.PRange;
4243
import com.oracle.graal.python.builtins.objects.type.PythonClass;
4344
import com.oracle.graal.python.nodes.control.GetIteratorNode;
4445
import com.oracle.graal.python.nodes.control.GetNextNode;
45-
import com.oracle.graal.python.nodes.expression.CastToBooleanNode;
4646
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
4747
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
4848
import com.oracle.graal.python.runtime.exception.PException;
49-
import com.oracle.graal.python.runtime.exception.PythonErrorType;
5049
import com.oracle.graal.python.runtime.sequence.PSequence;
5150
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
5251
import com.oracle.truffle.api.dsl.Cached;
@@ -109,6 +108,10 @@ protected boolean isIntArray(String typeCode) {
109108
return typeCode.charAt(0) == 'i';
110109
}
111110

111+
protected boolean isLongArray(String typeCode) {
112+
return typeCode.charAt(0) == 'l';
113+
}
114+
112115
protected boolean isByteArray(String typeCode) {
113116
return typeCode.charAt(0) == 'b';
114117
}
@@ -122,10 +125,11 @@ PArray arrayByteInitializer(PythonClass cls, @SuppressWarnings("unused") String
122125
@Cached("createCast()") CastToByteNode castToByteNode,
123126
@Cached("create()") GetIteratorNode getIterator,
124127
@Cached("create()") GetNextNode next,
125-
@Cached("createBinaryProfile()") ConditionProfile errorProfile) {
128+
@Cached("createBinaryProfile()") ConditionProfile errorProfile,
129+
@Cached("create()") SequenceNodes.LenNode lenNode) {
126130
Object iter = getIterator.executeWith(initializer);
127131
int i = 0;
128-
byte[] byteArray = new byte[initializer.len()];
132+
byte[] byteArray = new byte[lenNode.execute(initializer)];
129133

130134
while (true) {
131135
Object nextValue;
@@ -145,11 +149,12 @@ PArray arrayByteInitializer(PythonClass cls, @SuppressWarnings("unused") String
145149
PArray arrayIntInitializer(PythonClass cls, @SuppressWarnings("unused") String typeCode, PSequence initializer,
146150
@Cached("create()") GetIteratorNode getIterator,
147151
@Cached("create()") GetNextNode next,
148-
@Cached("createBinaryProfile()") ConditionProfile errorProfile) {
152+
@Cached("createBinaryProfile()") ConditionProfile errorProfile,
153+
@Cached("create()") SequenceNodes.LenNode lenNode) {
149154
Object iter = getIterator.executeWith(initializer);
150155
int i = 0;
151156

152-
int[] intArray = new int[initializer.len()];
157+
int[] intArray = new int[lenNode.execute(initializer)];
153158

154159
while (true) {
155160
Object nextValue;
@@ -169,15 +174,45 @@ PArray arrayIntInitializer(PythonClass cls, @SuppressWarnings("unused") String t
169174
return factory().createArray(cls, intArray);
170175
}
171176

177+
@Specialization(guards = "isLongArray(typeCode)")
178+
PArray arrayLongInitializer(PythonClass cls, @SuppressWarnings("unused") String typeCode, PSequence initializer,
179+
@Cached("create()") GetIteratorNode getIterator,
180+
@Cached("create()") GetNextNode next,
181+
@Cached("createBinaryProfile()") ConditionProfile errorProfile,
182+
@Cached("create()") SequenceNodes.LenNode lenNode) {
183+
Object iter = getIterator.executeWith(initializer);
184+
int i = 0;
185+
186+
long[] longArray = new long[lenNode.execute(initializer)];
187+
188+
while (true) {
189+
Object nextValue;
190+
try {
191+
nextValue = next.execute(iter);
192+
} catch (PException e) {
193+
e.expectStopIteration(getCore(), errorProfile);
194+
break;
195+
}
196+
if (nextValue instanceof Long) {
197+
longArray[i++] = (long) nextValue;
198+
} else {
199+
throw raise(ValueError, "integer argument expected, got %p", nextValue);
200+
}
201+
}
202+
203+
return factory().createArray(cls, longArray);
204+
}
205+
172206
@Specialization(guards = "isDoubleArray(typeCode)")
173207
PArray arrayDoubleInitializer(PythonClass cls, @SuppressWarnings("unused") String typeCode, PSequence initializer,
174208
@Cached("create()") GetIteratorNode getIterator,
175209
@Cached("create()") GetNextNode next,
176-
@Cached("createBinaryProfile()") ConditionProfile errorProfile) {
210+
@Cached("createBinaryProfile()") ConditionProfile errorProfile,
211+
@Cached("create()") SequenceNodes.LenNode lenNode) {
177212
Object iter = getIterator.executeWith(initializer);
178213
int i = 0;
179214

180-
double[] doubleArray = new double[initializer.len()];
215+
double[] doubleArray = new double[lenNode.execute(initializer)];
181216

182217
while (true) {
183218
Object nextValue;
@@ -202,6 +237,10 @@ PArray arrayDoubleInitializer(PythonClass cls, @SuppressWarnings("unused") Strin
202237
@Specialization
203238
@TruffleBoundary
204239
PArray arrayWithObjectInitializer(@SuppressWarnings("unused") PythonClass cls, @SuppressWarnings("unused") String typeCode, Object initializer) {
240+
if (!(isIntArray(typeCode) || isByteArray(typeCode) || isDoubleArray(typeCode))) {
241+
// TODO implement support for typecodes: b, B, u, h, H, i, I, l, L, q, Q, f or d
242+
throw raise(ValueError, "bad typecode (must be i, d, b, or l)");
243+
}
205244
throw new RuntimeException("Unsupported initializer " + initializer);
206245
}
207246

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@
8585
import com.oracle.graal.python.builtins.objects.bytes.PBytes;
8686
import com.oracle.graal.python.builtins.objects.cell.PCell;
8787
import com.oracle.graal.python.builtins.objects.code.PCode;
88+
import com.oracle.graal.python.builtins.objects.common.HashingCollectionNodes;
89+
import com.oracle.graal.python.builtins.objects.common.PHashingCollection;
90+
import com.oracle.graal.python.builtins.objects.common.SequenceNodes;
8891
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
8992
import com.oracle.graal.python.builtins.objects.dict.PDict;
9093
import com.oracle.graal.python.builtins.objects.function.Arity;
@@ -144,6 +147,7 @@
144147
import com.oracle.graal.python.runtime.PythonParser.ParserMode;
145148
import com.oracle.graal.python.runtime.exception.PException;
146149
import com.oracle.graal.python.runtime.exception.PythonErrorType;
150+
import com.oracle.graal.python.runtime.sequence.PSequence;
147151
import com.oracle.graal.python.runtime.sequence.storage.SequenceStorage;
148152
import com.oracle.truffle.api.CompilerDirectives;
149153
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
@@ -698,6 +702,8 @@ public abstract static class IdNode extends PythonBuiltinNode {
698702

699703
@Child private ReadAttributeFromObjectNode readId = null;
700704
@Child private WriteAttributeToObjectNode writeId = null;
705+
@Child private SequenceNodes.LenNode lenNode = null;
706+
@Child private HashingCollectionNodes.LenNode setLenNode = null;
701707

702708
@SuppressWarnings("unused")
703709
@Specialization
@@ -761,11 +767,11 @@ Object doEmptyFrozenSet(@SuppressWarnings("unused") PFrozenSet value) {
761767
}
762768

763769
protected boolean isEmptyImmutableBuiltin(Object object) {
764-
return (object instanceof PTuple && PGuards.isEmpty((PTuple) object)) ||
770+
return (object instanceof PTuple && isEmpty((PTuple) object)) ||
765771
(object instanceof String && PGuards.isEmpty((String) object)) ||
766-
(object instanceof PString && PGuards.isEmpty((PString) object)) ||
767-
(object instanceof PBytes && PGuards.isEmpty((PBytes) object)) ||
768-
(object instanceof PFrozenSet && PGuards.isEmpty((PFrozenSet) object));
772+
(object instanceof PString && isEmpty((PString) object)) ||
773+
(object instanceof PBytes && isEmpty((PBytes) object)) ||
774+
(object instanceof PFrozenSet && isEmpty((PFrozenSet) object));
769775
}
770776

771777
/**
@@ -789,6 +795,22 @@ Object doId(Object obj) {
789795
return obj.hashCode();
790796
}
791797

798+
protected boolean isEmpty(PSequence s) {
799+
if (lenNode == null) {
800+
CompilerDirectives.transferToInterpreterAndInvalidate();
801+
lenNode = insert(SequenceNodes.LenNode.create());
802+
}
803+
return lenNode.execute(s) == 0;
804+
}
805+
806+
protected boolean isEmpty(PHashingCollection s) {
807+
if (setLenNode == null) {
808+
CompilerDirectives.transferToInterpreterAndInvalidate();
809+
setLenNode = insert(HashingCollectionNodes.LenNode.create());
810+
}
811+
return setLenNode.execute(s) == 0;
812+
}
813+
792814
private Object getId(PythonObject obj) {
793815
if (readId == null) {
794816
CompilerDirectives.transferToInterpreterAndInvalidate();
@@ -815,6 +837,7 @@ public abstract static class IsInstanceNode extends PythonBuiltinNode {
815837
@Child private LookupAndCallBinaryNode instanceCheckNode = LookupAndCallBinaryNode.create(__INSTANCECHECK__);
816838
@Child private CastToBooleanNode castToBooleanNode = CastToBooleanNode.createIfTrueNode();
817839
@Child private TypeBuiltins.InstanceCheckNode typeInstanceCheckNode = TypeBuiltins.InstanceCheckNode.create();
840+
@Child private SequenceStorageNodes.LenNode lenNode;
818841

819842
public static IsInstanceNode create() {
820843
return BuiltinFunctionsFactory.IsInstanceNodeFactory.create(null);
@@ -834,10 +857,10 @@ public boolean isInstance(Object instance, PythonClass cls,
834857
return instanceClass == cls || isSubtypeNode.execute(instanceClass, cls) || isInstanceCheckInternal(instance, cls);
835858
}
836859

837-
@Specialization(guards = "clsTuple.len() == cachedLen", limit = "getVariableArgumentInlineCacheLimit()")
860+
@Specialization(guards = "getLength(clsTuple) == cachedLen", limit = "getVariableArgumentInlineCacheLimit()")
838861
@ExplodeLoop
839862
public boolean isInstanceTupleConstantLen(Object instance, PTuple clsTuple,
840-
@Cached("clsTuple.len()") int cachedLen,
863+
@Cached("getLength(clsTuple)") int cachedLen,
841864
@Cached("create()") IsInstanceNode isInstanceNode) {
842865
Object[] array = clsTuple.getArray();
843866
for (int i = 0; i < cachedLen; i++) {
@@ -864,6 +887,14 @@ public boolean isInstance(Object instance, PTuple clsTuple,
864887
public boolean isInstance(Object instance, Object cls) {
865888
return isInstanceCheckInternal(instance, cls) || typeInstanceCheckNode.executeWith(cls, instance);
866889
}
890+
891+
protected int getLength(PTuple t) {
892+
if (lenNode == null) {
893+
CompilerDirectives.transferToInterpreterAndInvalidate();
894+
lenNode = insert(SequenceStorageNodes.LenNode.create());
895+
}
896+
return lenNode.execute(t.getSequenceStorage());
897+
}
867898
}
868899

869900
// issubclass(class, classinfo)
@@ -873,6 +904,7 @@ public abstract static class IsSubClassNode extends PythonBuiltinNode {
873904
@Child private LookupAndCallBinaryNode subclassCheckNode = LookupAndCallBinaryNode.create(__SUBCLASSCHECK__);
874905
@Child private CastToBooleanNode castToBooleanNode = CastToBooleanNode.createIfTrueNode();
875906
@Child private IsSubtypeNode isSubtypeNode = IsSubtypeNode.create();
907+
@Child private SequenceStorageNodes.LenNode lenNode;
876908

877909
public static IsSubClassNode create() {
878910
return BuiltinFunctionsFactory.IsSubClassNodeFactory.create(null);
@@ -885,10 +917,10 @@ private boolean isInstanceCheckInternal(Object derived, Object cls) {
885917

886918
public abstract boolean executeWith(Object derived, Object cls);
887919

888-
@Specialization(guards = "clsTuple.len() == cachedLen", limit = "getVariableArgumentInlineCacheLimit()")
920+
@Specialization(guards = "getLength(clsTuple) == cachedLen", limit = "getVariableArgumentInlineCacheLimit()")
889921
@ExplodeLoop
890922
public boolean isSubclassTupleConstantLen(Object derived, PTuple clsTuple,
891-
@Cached("clsTuple.len()") int cachedLen,
923+
@Cached("getLength(clsTuple)") int cachedLen,
892924
@Cached("create()") IsSubClassNode isSubclassNode) {
893925
Object[] array = clsTuple.getArray();
894926
for (int i = 0; i < cachedLen; i++) {
@@ -915,6 +947,14 @@ public boolean isSubclass(Object derived, PTuple clsTuple,
915947
public boolean isSubclass(Object derived, Object cls) {
916948
return isInstanceCheckInternal(derived, cls) || isSubtypeNode.execute(derived, cls);
917949
}
950+
951+
protected int getLength(PTuple t) {
952+
if (lenNode == null) {
953+
CompilerDirectives.transferToInterpreterAndInvalidate();
954+
lenNode = insert(SequenceStorageNodes.LenNode.create());
955+
}
956+
return lenNode.execute(t.getSequenceStorage());
957+
}
918958
}
919959

920960
// iter(object[, sentinel])
@@ -1098,17 +1138,18 @@ public abstract static class OrdNode extends PythonBuiltinNode {
10981138
@Specialization
10991139
public int ord(String chr) {
11001140
if (chr.length() != 1) {
1101-
raise(TypeError, "ord() expected a character, but string of length %d found", chr.length());
1141+
throw raise(TypeError, "ord() expected a character, but string of length %d found", chr.length());
11021142
}
1103-
11041143
return chr.charAt(0);
11051144
}
11061145

11071146
@Specialization
11081147
public int ord(PBytes chr,
1148+
@Cached("create()") SequenceStorageNodes.LenNode lenNode,
11091149
@Cached("create()") SequenceStorageNodes.GetItemNode getItemNode) {
1110-
if (chr.len() != 1) {
1111-
raise(TypeError, "ord() expected a character, but string of length %d found", chr.len());
1150+
int len = lenNode.execute(chr.getSequenceStorage());
1151+
if (len != 1) {
1152+
throw raise(TypeError, "ord() expected a character, but string of length %d found", len);
11121153
}
11131154

11141155
return (byte) getItemNode.execute(chr.getSequenceStorage(), 0);
@@ -1122,11 +1163,12 @@ public abstract static class PrintNode extends PythonBuiltinNode {
11221163
@SuppressWarnings("unused")
11231164
@Specialization
11241165
public Object print(PTuple values, String sep, String end, Object file, boolean flush,
1166+
@Cached("create()") SequenceStorageNodes.LenNode lenNode,
11251167
@Cached("createNotNormalized()") SequenceStorageNodes.GetItemNode getItemNode,
11261168
@Cached("create(__STR__)") LookupAndCallUnaryNode callStr) {
11271169
try {
11281170
PythonContext context = getContext();
1129-
if (values.len() == 0) {
1171+
if (lenNode.execute(values.getSequenceStorage()) == 0) {
11301172
write(context, end);
11311173
} else {
11321174
SequenceStorage store = values.getSequenceStorage();

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/CodecsModuleBuiltins.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -229,12 +229,14 @@ protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFa
229229
@Builtin(name = "__truffle_encode", fixedNumOfPositionalArgs = 1, keywordArguments = {"encoding", "errors"})
230230
@GenerateNodeFactory
231231
public abstract static class CodecsEncodeNode extends PythonBuiltinNode {
232+
@Child private SequenceStorageNodes.LenNode lenNode;
233+
232234
@Specialization(guards = "isString(str)")
233235
Object encode(Object str, @SuppressWarnings("unused") PNone encoding, @SuppressWarnings("unused") PNone errors,
234236
@Cached("createClassProfile()") ValueProfile strTypeProfile) {
235237
Object profiledStr = strTypeProfile.profile(str);
236238
PBytes bytes = encodeString(profiledStr.toString(), "utf-8", "strict");
237-
return factory().createTuple(new Object[]{bytes, bytes.len()});
239+
return factory().createTuple(new Object[]{bytes, getLength(bytes)});
238240
}
239241

240242
@Specialization(guards = {"isString(str)", "isString(encoding)"})
@@ -244,7 +246,7 @@ Object encode(Object str, Object encoding, @SuppressWarnings("unused") PNone err
244246
Object profiledStr = strTypeProfile.profile(str);
245247
Object profiledEncoding = encodingTypeProfile.profile(encoding);
246248
PBytes bytes = encodeString(profiledStr.toString(), profiledEncoding.toString(), "strict");
247-
return factory().createTuple(new Object[]{bytes, bytes.len()});
249+
return factory().createTuple(new Object[]{bytes, getLength(bytes)});
248250
}
249251

250252
@Specialization(guards = {"isString(str)", "isString(errors)"})
@@ -254,7 +256,7 @@ Object encode(Object str, @SuppressWarnings("unused") PNone encoding, Object err
254256
Object profiledStr = strTypeProfile.profile(str);
255257
Object profiledErrors = errorsTypeProfile.profile(errors);
256258
PBytes bytes = encodeString(profiledStr.toString(), "utf-8", profiledErrors.toString());
257-
return factory().createTuple(new Object[]{bytes, bytes.len()});
259+
return factory().createTuple(new Object[]{bytes, getLength(bytes)});
258260
}
259261

260262
@Specialization(guards = {"isString(str)", "isString(encoding)", "isString(errors)"})
@@ -266,7 +268,7 @@ Object encode(Object str, Object encoding, Object errors,
266268
Object profiledEncoding = encodingTypeProfile.profile(encoding);
267269
Object profiledErrors = errorsTypeProfile.profile(errors);
268270
PBytes bytes = encodeString(profiledStr.toString(), profiledEncoding.toString(), profiledErrors.toString());
269-
return factory().createTuple(new Object[]{bytes, bytes.len()});
271+
return factory().createTuple(new Object[]{bytes, getLength(bytes)});
270272
}
271273

272274
@Fallback
@@ -308,6 +310,14 @@ private PBytes encodeString(String self, String encoding, String errors) {
308310
throw raise(UnicodeEncodeError, "%s", e.getMessage());
309311
}
310312
}
313+
314+
private int getLength(PBytes b) {
315+
if (lenNode == null) {
316+
CompilerDirectives.transferToInterpreterAndInvalidate();
317+
lenNode = insert(SequenceStorageNodes.LenNode.create());
318+
}
319+
return lenNode.execute(b.getSequenceStorage());
320+
}
311321
}
312322

313323
// _codecs.decode(obj, encoding='utf-8', errors='strict')
@@ -359,7 +369,7 @@ private ByteBuffer getBytesBuffer(PIBytesLike bytesLike) {
359369
toByteArrayNode = insert(SequenceStorageNodes.ToByteArrayNode.create(false));
360370
}
361371
byte[] barr = toByteArrayNode.execute(bytesLike.getSequenceStorage());
362-
return ByteBuffer.wrap(barr, 0, bytesLike.len());
372+
return ByteBuffer.wrap(barr, 0, barr.length);
363373
}
364374

365375
@TruffleBoundary

0 commit comments

Comments
 (0)