Skip to content

Commit c8f715b

Browse files
committed
Move 'CastToByteNode' to separate file.
1 parent ab28c74 commit c8f715b

File tree

8 files changed

+161
-107
lines changed

8 files changed

+161
-107
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@
3838
import com.oracle.graal.python.builtins.objects.PNone;
3939
import com.oracle.graal.python.builtins.objects.array.PArray;
4040
import com.oracle.graal.python.builtins.objects.common.SequenceNodes;
41-
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes.CastToByteNode;
4241
import com.oracle.graal.python.builtins.objects.range.PRange;
4342
import com.oracle.graal.python.builtins.objects.type.LazyPythonClass;
4443
import com.oracle.graal.python.nodes.control.GetIteratorNode;
4544
import com.oracle.graal.python.nodes.control.GetNextNode;
4645
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
4746
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
4847
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
48+
import com.oracle.graal.python.nodes.util.CastToByteNode;
4949
import com.oracle.graal.python.runtime.exception.PException;
5050
import com.oracle.graal.python.runtime.sequence.PSequence;
5151
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@
9393
import com.oracle.graal.python.builtins.objects.common.HashingStorageNodes;
9494
import com.oracle.graal.python.builtins.objects.common.PHashingCollection;
9595
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
96-
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes.CastToByteNode;
9796
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes.NoGeneralizationNode;
9897
import com.oracle.graal.python.builtins.objects.complex.PComplex;
9998
import com.oracle.graal.python.builtins.objects.dict.PDict;
@@ -155,6 +154,7 @@
155154
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
156155
import com.oracle.graal.python.nodes.subscript.SliceLiteralNode;
157156
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
157+
import com.oracle.graal.python.nodes.util.CastToByteNode;
158158
import com.oracle.graal.python.nodes.util.CastToDoubleNode;
159159
import com.oracle.graal.python.nodes.util.CastToIndexNode;
160160
import com.oracle.graal.python.nodes.util.CastToStringNode;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@
9696
import com.oracle.graal.python.builtins.objects.code.PCode;
9797
import com.oracle.graal.python.builtins.objects.common.HashingCollectionNodes;
9898
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
99-
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes.CastToByteNode;
10099
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes.NormalizeIndexNode;
101100
import com.oracle.graal.python.builtins.objects.complex.PComplex;
102101
import com.oracle.graal.python.builtins.objects.dict.PDict;
@@ -152,6 +151,7 @@
152151
import com.oracle.graal.python.nodes.subscript.SliceLiteralNode;
153152
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
154153
import com.oracle.graal.python.nodes.truffle.PythonTypes;
154+
import com.oracle.graal.python.nodes.util.CastToByteNode;
155155
import com.oracle.graal.python.nodes.util.CastToIndexNode;
156156
import com.oracle.graal.python.runtime.PythonContext;
157157
import com.oracle.graal.python.runtime.PythonCore;

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
7676
import com.oracle.graal.python.nodes.object.GetLazyClassNode;
7777
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
78+
import com.oracle.graal.python.nodes.util.CastToByteNode;
7879
import com.oracle.graal.python.runtime.sequence.PSequence;
7980
import com.oracle.graal.python.runtime.sequence.storage.ByteSequenceStorage;
8081
import com.oracle.graal.python.runtime.sequence.storage.IntSequenceStorage;
@@ -358,7 +359,7 @@ public PByteArray append(PByteArray byteArray, Object arg,
358359
}
359360

360361
protected static SequenceStorageNodes.AppendNode createAppend() {
361-
return SequenceStorageNodes.AppendNode.create(() -> NoGeneralizationNode.create("byte must be in range(0, 256)"));
362+
return SequenceStorageNodes.AppendNode.create(() -> NoGeneralizationNode.create(CastToByteNode.INVALID_BYTE_VALUE));
362363
}
363364
}
364365

@@ -376,7 +377,7 @@ public PNone doGeneric(PByteArray byteArray, Object source,
376377
}
377378

378379
protected static SequenceStorageNodes.ExtendNode createExtend() {
379-
return SequenceStorageNodes.ExtendNode.create(() -> NoGeneralizationNode.create("byte must be in range(0, 256)"));
380+
return SequenceStorageNodes.ExtendNode.create(() -> NoGeneralizationNode.create(CastToByteNode.INVALID_BYTE_VALUE));
380381
}
381382

382383
protected boolean isPSequenceWithStorage(Object source) {

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -57,6 +57,7 @@
5757
import com.oracle.graal.python.nodes.control.GetIteratorNode;
5858
import com.oracle.graal.python.nodes.control.GetNextNode;
5959
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
60+
import com.oracle.graal.python.nodes.util.CastToByteNode;
6061
import com.oracle.graal.python.runtime.exception.PException;
6162
import com.oracle.graal.python.runtime.sequence.PSequence;
6263
import com.oracle.graal.python.runtime.sequence.storage.ByteSequenceStorage;
@@ -286,7 +287,7 @@ public static FindNode create() {
286287
public static class FromSequenceStorageNode extends Node {
287288

288289
@Node.Child private SequenceStorageNodes.GetItemNode getItemNode;
289-
@Node.Child private SequenceStorageNodes.CastToByteNode castToByteNode;
290+
@Node.Child private CastToByteNode castToByteNode;
290291
@Node.Child private SequenceStorageNodes.LenNode lenNode;
291292

292293
public byte[] execute(SequenceStorage storage) {
@@ -307,10 +308,10 @@ private SequenceStorageNodes.GetItemNode getGetItemNode() {
307308
return getItemNode;
308309
}
309310

310-
private SequenceStorageNodes.CastToByteNode getCastToByteNode() {
311+
private CastToByteNode getCastToByteNode() {
311312
if (castToByteNode == null) {
312313
CompilerDirectives.transferToInterpreterAndInvalidate();
313-
castToByteNode = insert(SequenceStorageNodes.CastToByteNode.create());
314+
castToByteNode = insert(CastToByteNode.create());
314315
}
315316
return castToByteNode;
316317
}
@@ -355,7 +356,7 @@ public abstract static class FromIteratorNode extends PNodeWithContext {
355356
public SequenceStorageNodes.AppendNode getAppendByteNode() {
356357
if (appendByteNode == null) {
357358
CompilerDirectives.transferToInterpreterAndInvalidate();
358-
appendByteNode = insert(SequenceStorageNodes.AppendNode.create(() -> SequenceStorageNodes.NoGeneralizationNode.create("byte must be in range(0, 256)")));
359+
appendByteNode = insert(SequenceStorageNodes.AppendNode.create(() -> SequenceStorageNodes.NoGeneralizationNode.create(CastToByteNode.INVALID_BYTE_VALUE)));
359360
}
360361
return appendByteNode;
361362
}

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

Lines changed: 2 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, Oracle and/or its affiliates.
2+
* Copyright (c) 2018, 2019, Oracle and/or its affiliates.
33
* Copyright (c) 2013, Regents of the University of California
44
*
55
* All rights reserved.
@@ -48,15 +48,13 @@
4848

4949
import java.util.Arrays;
5050
import java.util.function.BiFunction;
51-
import java.util.function.Function;
5251
import java.util.function.Supplier;
5352

5453
import com.oracle.graal.python.PythonLanguage;
5554
import com.oracle.graal.python.builtins.objects.cext.CExtNodes;
5655
import com.oracle.graal.python.builtins.objects.cext.CExtNodes.PCallCapiFunction;
5756
import com.oracle.graal.python.builtins.objects.cext.NativeCAPISymbols;
5857
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodesFactory.AppendNodeGen;
59-
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodesFactory.CastToByteNodeGen;
6058
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodesFactory.CmpNodeGen;
6159
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodesFactory.ConcatBaseNodeGen;
6260
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodesFactory.ConcatNodeGen;
@@ -105,6 +103,7 @@
105103
import com.oracle.graal.python.nodes.expression.CastToBooleanNode;
106104
import com.oracle.graal.python.nodes.object.GetLazyClassNode;
107105
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
106+
import com.oracle.graal.python.nodes.util.CastToByteNode;
108107
import com.oracle.graal.python.nodes.util.CastToIndexNode;
109108
import com.oracle.graal.python.runtime.exception.PException;
110109
import com.oracle.graal.python.runtime.exception.PythonErrorType;
@@ -1177,97 +1176,6 @@ public static StorageToNativeNode create() {
11771176
}
11781177
}
11791178

1180-
public abstract static class CastToByteNode extends PNodeWithContext {
1181-
1182-
private final Function<Object, Byte> rangeErrorHandler;
1183-
private final Function<Object, Byte> typeErrorHandler;
1184-
1185-
protected CastToByteNode(Function<Object, Byte> rangeErrorHandler, Function<Object, Byte> typeErrorHandler) {
1186-
this.rangeErrorHandler = rangeErrorHandler;
1187-
this.typeErrorHandler = typeErrorHandler;
1188-
}
1189-
1190-
public abstract byte execute(Object val);
1191-
1192-
@Specialization
1193-
protected byte doByte(byte value) {
1194-
return value;
1195-
}
1196-
1197-
@Specialization(rewriteOn = ArithmeticException.class)
1198-
protected byte doInt(int value) {
1199-
return PInt.byteValueExact(value);
1200-
}
1201-
1202-
@Specialization(replaces = "doInt")
1203-
protected byte doIntOvf(int value) {
1204-
try {
1205-
return PInt.byteValueExact(value);
1206-
} catch (ArithmeticException e) {
1207-
return handleRangeError(value);
1208-
}
1209-
}
1210-
1211-
@Specialization(rewriteOn = ArithmeticException.class)
1212-
protected byte doLong(long value) {
1213-
return PInt.byteValueExact(value);
1214-
}
1215-
1216-
@Specialization(replaces = "doLong")
1217-
protected byte doLongOvf(long value) {
1218-
try {
1219-
return PInt.byteValueExact(value);
1220-
} catch (ArithmeticException e) {
1221-
return handleRangeError(value);
1222-
}
1223-
}
1224-
1225-
@Specialization(rewriteOn = ArithmeticException.class)
1226-
protected byte doPInt(PInt value) {
1227-
return PInt.byteValueExact(value.longValueExact());
1228-
}
1229-
1230-
@Specialization(replaces = "doPInt")
1231-
protected byte doPIntOvf(PInt value) {
1232-
try {
1233-
return PInt.byteValueExact(value.longValueExact());
1234-
} catch (ArithmeticException e) {
1235-
return handleRangeError(value);
1236-
}
1237-
}
1238-
1239-
@Specialization
1240-
protected byte doBoolean(boolean value) {
1241-
return value ? (byte) 1 : (byte) 0;
1242-
}
1243-
1244-
@Fallback
1245-
protected byte doGeneric(@SuppressWarnings("unused") Object val) {
1246-
if (typeErrorHandler != null) {
1247-
return typeErrorHandler.apply(val);
1248-
} else {
1249-
throw raise(TypeError, "an integer is required (got type %p)", val);
1250-
}
1251-
}
1252-
1253-
private byte handleRangeError(Object val) {
1254-
if (rangeErrorHandler != null) {
1255-
return rangeErrorHandler.apply(val);
1256-
} else {
1257-
throw raise(ValueError, "byte must be in range(0, 256)");
1258-
}
1259-
}
1260-
1261-
public static CastToByteNode create() {
1262-
return CastToByteNodeGen.create(null, null);
1263-
}
1264-
1265-
public static CastToByteNode create(Function<Object, Byte> rangeErrorHandler, Function<Object, Byte> typeErrorHandler) {
1266-
return CastToByteNodeGen.create(rangeErrorHandler, typeErrorHandler);
1267-
}
1268-
1269-
}
1270-
12711179
protected abstract static class BinCmpOp {
12721180
protected abstract boolean cmp(int l, int r);
12731181

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/*
2+
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
package com.oracle.graal.python.nodes.util;
42+
43+
import static com.oracle.graal.python.runtime.exception.PythonErrorType.TypeError;
44+
import static com.oracle.graal.python.runtime.exception.PythonErrorType.ValueError;
45+
46+
import java.util.function.Function;
47+
48+
import com.oracle.graal.python.builtins.objects.ints.PInt;
49+
import com.oracle.graal.python.nodes.PNodeWithContext;
50+
import com.oracle.truffle.api.dsl.Fallback;
51+
import com.oracle.truffle.api.dsl.Specialization;
52+
53+
public abstract class CastToByteNode extends PNodeWithContext {
54+
public static final String INVALID_BYTE_VALUE = "byte must be in range(0, 256)";
55+
56+
private final Function<Object, Byte> rangeErrorHandler;
57+
private final Function<Object, Byte> typeErrorHandler;
58+
59+
protected CastToByteNode(Function<Object, Byte> rangeErrorHandler, Function<Object, Byte> typeErrorHandler) {
60+
this.rangeErrorHandler = rangeErrorHandler;
61+
this.typeErrorHandler = typeErrorHandler;
62+
}
63+
64+
public abstract byte execute(Object val);
65+
66+
@Specialization
67+
protected byte doByte(byte value) {
68+
return value;
69+
}
70+
71+
@Specialization(rewriteOn = ArithmeticException.class)
72+
protected byte doInt(int value) {
73+
return PInt.byteValueExact(value);
74+
}
75+
76+
@Specialization(replaces = "doInt")
77+
protected byte doIntOvf(int value) {
78+
try {
79+
return PInt.byteValueExact(value);
80+
} catch (ArithmeticException e) {
81+
return handleRangeError(value);
82+
}
83+
}
84+
85+
@Specialization(rewriteOn = ArithmeticException.class)
86+
protected byte doLong(long value) {
87+
return PInt.byteValueExact(value);
88+
}
89+
90+
@Specialization(replaces = "doLong")
91+
protected byte doLongOvf(long value) {
92+
try {
93+
return PInt.byteValueExact(value);
94+
} catch (ArithmeticException e) {
95+
return handleRangeError(value);
96+
}
97+
}
98+
99+
@Specialization(rewriteOn = ArithmeticException.class)
100+
protected byte doPInt(PInt value) {
101+
return PInt.byteValueExact(value.longValueExact());
102+
}
103+
104+
@Specialization(replaces = "doPInt")
105+
protected byte doPIntOvf(PInt value) {
106+
try {
107+
return PInt.byteValueExact(value.longValueExact());
108+
} catch (ArithmeticException e) {
109+
return handleRangeError(value);
110+
}
111+
}
112+
113+
@Specialization
114+
protected byte doBoolean(boolean value) {
115+
return value ? (byte) 1 : (byte) 0;
116+
}
117+
118+
@Fallback
119+
protected byte doGeneric(@SuppressWarnings("unused") Object val) {
120+
if (typeErrorHandler != null) {
121+
return typeErrorHandler.apply(val);
122+
} else {
123+
throw raise(TypeError, "an integer is required (got type %p)", val);
124+
}
125+
}
126+
127+
private byte handleRangeError(Object val) {
128+
if (rangeErrorHandler != null) {
129+
return rangeErrorHandler.apply(val);
130+
} else {
131+
throw raise(ValueError, INVALID_BYTE_VALUE);
132+
}
133+
}
134+
135+
public static CastToByteNode create() {
136+
return CastToByteNodeGen.create(null, null);
137+
}
138+
139+
public static CastToByteNode create(Function<Object, Byte> rangeErrorHandler, Function<Object, Byte> typeErrorHandler) {
140+
return CastToByteNodeGen.create(rangeErrorHandler, typeErrorHandler);
141+
}
142+
143+
}

0 commit comments

Comments
 (0)