Skip to content

Commit cd08f0f

Browse files
committed
most string builtins
1 parent f455e82 commit cd08f0f

File tree

1 file changed

+46
-77
lines changed
  • graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/str

1 file changed

+46
-77
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/str/StringBuiltins.java

Lines changed: 46 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,11 @@
7070
import com.oracle.graal.python.builtins.objects.common.SequenceNodes.GetObjectArrayNode;
7171
import com.oracle.graal.python.builtins.objects.common.SequenceNodesFactory.GetObjectArrayNodeGen;
7272
import com.oracle.graal.python.builtins.objects.dict.PDict;
73-
import com.oracle.graal.python.builtins.objects.ints.PInt;
73+
import com.oracle.graal.python.builtins.objects.function.PArguments;
7474
import com.oracle.graal.python.builtins.objects.iterator.PStringIterator;
7575
import com.oracle.graal.python.builtins.objects.list.ListBuiltins.ListReverseNode;
7676
import com.oracle.graal.python.builtins.objects.list.PList;
77+
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
7778
import com.oracle.graal.python.builtins.objects.slice.PSlice;
7879
import com.oracle.graal.python.builtins.objects.slice.PSlice.SliceInfo;
7980
import com.oracle.graal.python.builtins.objects.str.StringNodes.CastToJavaStringCheckedNode;
@@ -105,6 +106,7 @@
105106
import com.oracle.graal.python.nodes.util.CastToJavaStringNodeGen;
106107
import com.oracle.graal.python.runtime.ExecutionContext.IndirectCallContext;
107108
import com.oracle.graal.python.runtime.PythonContext;
109+
import com.oracle.graal.python.runtime.PythonOptions;
108110
import com.oracle.graal.python.runtime.exception.PException;
109111
import com.oracle.graal.python.runtime.formatting.StringFormatter;
110112
import com.oracle.truffle.api.CompilerDirectives;
@@ -118,6 +120,7 @@
118120
import com.oracle.truffle.api.dsl.Specialization;
119121
import com.oracle.truffle.api.dsl.TypeSystemReference;
120122
import com.oracle.truffle.api.frame.VirtualFrame;
123+
import com.oracle.truffle.api.library.CachedLibrary;
121124
import com.oracle.truffle.api.profiles.ConditionProfile;
122125

123126
@CoreFunctions(extendClasses = PythonBuiltinClassType.PString)
@@ -642,33 +645,24 @@ protected String getErrorMessage() {
642645
@TypeSystemReference(PythonArithmeticTypes.class)
643646
abstract static class FindBaseNode extends PythonBuiltinNode {
644647

645-
@Child private CastToIndexNode startNode;
646-
@Child private CastToIndexNode endNode;
647-
648-
private CastToIndexNode getStartNode() {
649-
if (startNode == null) {
650-
CompilerDirectives.transferToInterpreterAndInvalidate();
651-
startNode = insert(CastToIndexNode.createOverflow());
652-
}
653-
return startNode;
654-
}
648+
@Child private PythonObjectLibrary lib;
655649

656-
private CastToIndexNode getEndNode() {
657-
if (endNode == null) {
650+
private PythonObjectLibrary getLibrary() {
651+
if (lib == null) {
658652
CompilerDirectives.transferToInterpreterAndInvalidate();
659-
endNode = insert(CastToIndexNode.createOverflow());
653+
lib = insert(PythonObjectLibrary.getFactory().createDispatched(PythonOptions.getCallSiteInlineCacheMaxDepth()));
660654
}
661-
return endNode;
655+
return lib;
662656
}
663657

664658
private SliceInfo computeSlice(@SuppressWarnings("unused") VirtualFrame frame, int length, long start, long end) {
665-
PSlice tmpSlice = factory().createSlice(getStartNode().execute(start), getEndNode().execute(end), 1);
659+
PSlice tmpSlice = factory().createSlice(getLibrary().asIndex(start), getLibrary().asIndex(end), 1);
666660
return tmpSlice.computeIndices(length);
667661
}
668662

669663
private SliceInfo computeSlice(VirtualFrame frame, int length, Object startO, Object endO) {
670-
int start = PGuards.isPNone(startO) ? PSlice.MISSING_INDEX : getStartNode().execute(frame, startO);
671-
int end = PGuards.isPNone(endO) ? PSlice.MISSING_INDEX : getEndNode().execute(frame, endO);
664+
int start = PGuards.isPNone(startO) ? PSlice.MISSING_INDEX : getLibrary().asIndexWithState(startO, PArguments.getThreadState(frame));
665+
int end = PGuards.isPNone(endO) ? PSlice.MISSING_INDEX : getLibrary().asIndexWithState(endO, PArguments.getThreadState(frame));
672666
return PSlice.computeIndices(start, end, 1, length);
673667
}
674668

@@ -1019,14 +1013,14 @@ PList doStringMaxsplit(String self, @SuppressWarnings("unused") PNone sep, int m
10191013
return splitfields(self, maxsplit, appendNode);
10201014
}
10211015

1022-
@Specialization(replaces = {"doStringWhitespace", "doStringSep", "doStringSepMaxsplit", "doStringMaxsplit"})
1016+
@Specialization(replaces = {"doStringWhitespace", "doStringSep", "doStringSepMaxsplit", "doStringMaxsplit"}, limit = "getCallSiteInlineCacheMaxDepth()")
10231017
Object doGeneric(VirtualFrame frame, Object self, Object sep, Object maxsplit,
10241018
@Cached CastToJavaStringCheckedNode castSelfNode,
1025-
@Cached CastToIndexNode castMaxsplitNode,
1019+
@CachedLibrary("maxsplit") PythonObjectLibrary lib,
10261020
@Cached CastToJavaStringCheckedNode castSepNode,
1027-
@Shared("appendNode") @Cached AppendNode appendNode) {
1021+
@Cached AppendNode appendNode) {
10281022
String selfStr = castSelfNode.cast(self, INVALID_RECEIVER, "split", self);
1029-
int imaxsplit = PGuards.isPNone(maxsplit) ? -1 : castMaxsplitNode.execute(frame, maxsplit);
1023+
int imaxsplit = PGuards.isPNone(maxsplit) ? -1 : lib.asIndexWithState(maxsplit, PArguments.getThreadState(frame));
10301024
if (PGuards.isPNone(sep)) {
10311025
return splitfields(selfStr, imaxsplit, appendNode);
10321026
} else {
@@ -1152,15 +1146,15 @@ PList doStringMaxsplit(VirtualFrame frame, String self, @SuppressWarnings("unuse
11521146
return rsplitfields(frame, self, maxsplit, appendNode, reverseNode);
11531147
}
11541148

1155-
@Specialization(replaces = {"doStringWhitespace", "doStringSep", "doStringSepMaxsplit", "doStringMaxsplit"})
1149+
@Specialization(replaces = {"doStringWhitespace", "doStringSep", "doStringSepMaxsplit", "doStringMaxsplit"}, limit = "getCallSiteInlineCacheMaxDepth()")
11561150
Object doGeneric(VirtualFrame frame, Object self, Object sep, Object maxsplit,
11571151
@Cached CastToJavaStringCheckedNode castSelfNode,
1158-
@Cached CastToIndexNode castMaxsplitNode,
1152+
@CachedLibrary("maxsplit") PythonObjectLibrary lib,
11591153
@Cached CastToJavaStringCheckedNode castSepNode,
1160-
@Shared("appendNode") @Cached AppendNode appendNode,
1161-
@Shared("reverseNode") @Cached ListReverseNode reverseNode) {
1154+
@Cached AppendNode appendNode,
1155+
@Cached ListReverseNode reverseNode) {
11621156
String selfStr = castSelfNode.cast(self, INVALID_RECEIVER, "rsplit", self);
1163-
int imaxsplit = PGuards.isPNone(maxsplit) ? -1 : castMaxsplitNode.execute(frame, maxsplit);
1157+
int imaxsplit = PGuards.isPNone(maxsplit) ? -1 : lib.asIndexWithState(maxsplit, PArguments.getThreadState(frame));
11641158
if (PGuards.isPNone(sep)) {
11651159
return rsplitfields(frame, selfStr, imaxsplit, appendNode, reverseNode);
11661160
} else {
@@ -1304,18 +1298,18 @@ static String doReplace(String self, String old, String with, int maxCount) {
13041298
return sb.toString();
13051299
}
13061300

1307-
@Specialization
1301+
@Specialization(limit = "getCallSiteInlineCacheMaxDepth()")
13081302
static String doGeneric(VirtualFrame frame, Object self, Object old, Object with, Object maxCount,
13091303
@Cached CastToJavaStringCheckedNode castSelfNode,
1310-
@Cached CastToIndexNode castToIndexNode) {
1304+
@CachedLibrary("maxCount") PythonObjectLibrary lib) {
13111305

13121306
String selfStr = castSelfNode.cast(self, INVALID_RECEIVER, "replace", self);
13131307
String oldStr = castSelfNode.cast(old, "replace() argument 1 must be str, not %p", "replace", old);
13141308
String withStr = castSelfNode.cast(with, "replace() argument 2 must be str, not %p", "replace", with);
13151309
if (PGuards.isPNone(maxCount)) {
13161310
return doReplace(selfStr, oldStr, withStr, PNone.NO_VALUE);
13171311
}
1318-
int iMaxCount = castToIndexNode.execute(frame, maxCount);
1312+
int iMaxCount = lib.asIndexWithState(maxCount, PArguments.getThreadState(frame));
13191313
return doReplace(selfStr, oldStr, withStr, iMaxCount);
13201314
}
13211315
}
@@ -1434,13 +1428,12 @@ int doStringStringStartEnd(String self, String substr, int start, int end,
14341428
int doGeneric(VirtualFrame frame, Object self, Object sub, Object start, Object end,
14351429
@Cached CastToJavaStringCheckedNode castSelfNode,
14361430
@Cached CastToJavaStringCheckedNode castSubNode,
1437-
@Cached CastToIndexNode castStartNode,
1438-
@Cached CastToIndexNode castEndNode,
1431+
@CachedLibrary(limit = "getCallSiteInlineCacheMaxDepth()") PythonObjectLibrary lib,
14391432
@Cached("createBinaryProfile()") ConditionProfile errorProfile) {
14401433
String selfStr = castSelfNode.cast(self, INVALID_RECEIVER, "index", self);
14411434
String subStr = castSubNode.cast(sub, MUST_BE_STR, sub);
1442-
int istart = PGuards.isPNone(start) ? 0 : castStartNode.execute(frame, start);
1443-
int iend = PGuards.isPNone(end) ? selfStr.length() : castEndNode.execute(frame, end);
1435+
int istart = PGuards.isPNone(start) ? 0 : lib.asIndexWithState(start, PArguments.getThreadState(frame));
1436+
int iend = PGuards.isPNone(end) ? selfStr.length() : lib.asIndexWithState(end, PArguments.getThreadState(frame));
14441437
return indexOf(selfStr, subStr, istart, iend, errorProfile);
14451438
}
14461439

@@ -1537,18 +1530,18 @@ String doStringInt(String left, int right) {
15371530
return repeatString(left, right);
15381531
}
15391532

1540-
@Specialization
1533+
@Specialization(limit = "1")
15411534
String doStringLong(String left, long right,
1542-
@Exclusive @Cached("createOverflow()") CastToIndexNode castToIndexNode) {
1543-
return doStringInt(left, castToIndexNode.execute(right));
1535+
@Exclusive @CachedLibrary("right") PythonObjectLibrary lib) {
1536+
return doStringInt(left, lib.asIndex(right));
15441537
}
15451538

15461539
@Specialization
15471540
String doStringObject(VirtualFrame frame, String left, Object right,
1548-
@Shared("castToIndexNode") @Cached("createOverflow()") CastToIndexNode castToIndexNode,
1541+
@Shared("castToIndexNode") @CachedLibrary(limit = "getCallSiteInlineCacheMaxDepth()") PythonObjectLibrary lib,
15491542
@Cached IsBuiltinClassProfile typeErrorProfile) {
15501543
try {
1551-
return doStringInt(left, castToIndexNode.execute(frame, right));
1544+
return doStringInt(left, lib.asIndexWithState(right, PArguments.getThreadState(frame)));
15521545
} catch (PException e) {
15531546
e.expect(PythonBuiltinClassType.OverflowError, typeErrorProfile);
15541547
throw raise(MemoryError);
@@ -1558,10 +1551,10 @@ String doStringObject(VirtualFrame frame, String left, Object right,
15581551
@Specialization
15591552
Object doGeneric(VirtualFrame frame, Object self, Object times,
15601553
@Cached CastToJavaStringCheckedNode castSelfNode,
1561-
@Shared("castToIndexNode") @Cached("createOverflow()") CastToIndexNode castToIndexNode,
1554+
@Shared("castToIndexNode") @CachedLibrary(limit = "getCallSiteInlineCacheMaxDepth()") PythonObjectLibrary lib,
15621555
@Cached IsBuiltinClassProfile typeErrorProfile) {
15631556
String selfStr = castSelfNode.cast(self, INVALID_RECEIVER, "index", self);
1564-
return doStringObject(frame, selfStr, times, castToIndexNode, typeErrorProfile);
1557+
return doStringObject(frame, selfStr, times, lib, typeErrorProfile);
15651558
}
15661559

15671560
@TruffleBoundary
@@ -1891,22 +1884,11 @@ abstract static class ZFillNode extends PythonBinaryBuiltinNode {
18911884

18921885
public abstract String executeObject(VirtualFrame frame, String self, Object x);
18931886

1894-
@Specialization
1895-
static String doStringInt(String self, int width) {
1896-
return zfill(self, width);
1897-
}
1898-
1899-
@Specialization
1900-
static String doStringObject(VirtualFrame frame, String self, Object width,
1901-
@Shared("toIndexNode") @Cached CastToIndexNode toIndexNode) {
1902-
return zfill(self, toIndexNode.execute(frame, width));
1903-
}
1904-
1905-
@Specialization(replaces = {"doStringInt", "doStringObject"})
1887+
@Specialization(limit = "getCallSiteInlineCacheMaxDepth()")
19061888
static String doGeneric(VirtualFrame frame, Object self, Object width,
19071889
@Cached CastToJavaStringCheckedNode castSelfNode,
1908-
@Shared("toIndexNode") @Cached CastToIndexNode toIndexNode) {
1909-
return zfill(castSelfNode.cast(self, INVALID_RECEIVER, "zfill", self), toIndexNode.execute(frame, width));
1890+
@CachedLibrary("width") PythonObjectLibrary lib) {
1891+
return zfill(castSelfNode.cast(self, INVALID_RECEIVER, "zfill", self), lib.asIndexWithState(width, PArguments.getThreadState(frame)));
19101892

19111893
}
19121894

@@ -2025,24 +2007,24 @@ String doStringIntString(String self, int width, String fill) {
20252007

20262008
@Specialization
20272009
String doStringObjectObject(VirtualFrame frame, String self, Object width, Object fill,
2028-
@Shared("castToIndexNode") @Cached CastToIndexNode castToIndexNode,
2010+
@Shared("castToIndexNode") @CachedLibrary(limit = "getCallSiteInlineCacheMaxDepth()") PythonObjectLibrary lib,
20292011
@Shared("castFillNode") @Cached CastToJavaStringCheckedNode castFillNode,
20302012
@Shared("errorProfile") @Cached("createBinaryProfile()") ConditionProfile errorProfile) {
20312013
String fillStr = PGuards.isNoValue(fill) ? " " : castFillNode.cast(fill, "", fill);
20322014
if (errorProfile.profile(fillStr.codePointCount(0, fillStr.length()) != 1)) {
20332015
throw raise(TypeError, "The fill character must be exactly one character long");
20342016
}
2035-
return make(self, castToIndexNode.execute(frame, width), fillStr);
2017+
return make(self, lib.asIndexWithState(width, PArguments.getThreadState(frame)), fillStr);
20362018
}
20372019

20382020
@Specialization
20392021
String doGeneric(VirtualFrame frame, Object self, Object width, Object fill,
20402022
@Cached CastToJavaStringCheckedNode castSelfNode,
2041-
@Shared("castToIndexNode") @Cached CastToIndexNode castToIndexNode,
2023+
@Shared("castToIndexNode") @CachedLibrary(limit = "getCallSiteInlineCacheMaxDepth()") PythonObjectLibrary lib,
20422024
@Shared("castFillNode") @Cached CastToJavaStringCheckedNode castFillNode,
20432025
@Shared("errorProfile") @Cached("createBinaryProfile()") ConditionProfile errorProfile) {
20442026
String selfStr = castSelfNode.cast(self, INVALID_RECEIVER, __ITER__, self);
2045-
return doStringObjectObject(frame, selfStr, width, fill, castToIndexNode, castFillNode, errorProfile);
2027+
return doStringObjectObject(frame, selfStr, width, fill, lib, castFillNode, errorProfile);
20462028
}
20472029

20482030
@TruffleBoundary
@@ -2140,33 +2122,20 @@ public String doString(String primary, PSlice slice) {
21402122
}
21412123
}
21422124

2143-
@Specialization
2144-
public String doString(String primary, int idx) {
2125+
@Specialization(limit = "getCallSiteInlineCacheMaxDepth()")
2126+
public String doString(VirtualFrame frame, String primary, Object idx,
2127+
@CachedLibrary("idx") PythonObjectLibrary lib) {
2128+
int index = lib.asIndexWithState(idx, PArguments.getThreadState(frame));
21452129
try {
2146-
int index = idx;
2147-
2148-
if (idx < 0) {
2130+
if (index < 0) {
21492131
index += primary.length();
21502132
}
2151-
21522133
return charAtToString(primary, index);
21532134
} catch (StringIndexOutOfBoundsException | ArithmeticException e) {
21542135
throw raise(IndexError, "IndexError: string index out of range");
21552136
}
21562137
}
21572138

2158-
@Specialization
2159-
public String doString(@SuppressWarnings("unused") VirtualFrame frame, String primary, long idx,
2160-
@Cached("create()") CastToIndexNode castToIndex) {
2161-
return doString(primary, castToIndex.execute(idx));
2162-
}
2163-
2164-
@Specialization
2165-
public String doString(VirtualFrame frame, String primary, PInt idx,
2166-
@Cached("create()") CastToIndexNode castToIndex) {
2167-
return doString(primary, castToIndex.execute(frame, idx));
2168-
}
2169-
21702139
@SuppressWarnings("unused")
21712140
@Fallback
21722141
Object doGeneric(Object self, Object other) {

0 commit comments

Comments
 (0)