70
70
import com .oracle .graal .python .builtins .objects .common .SequenceNodes .GetObjectArrayNode ;
71
71
import com .oracle .graal .python .builtins .objects .common .SequenceNodesFactory .GetObjectArrayNodeGen ;
72
72
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 ;
74
74
import com .oracle .graal .python .builtins .objects .iterator .PStringIterator ;
75
75
import com .oracle .graal .python .builtins .objects .list .ListBuiltins .ListReverseNode ;
76
76
import com .oracle .graal .python .builtins .objects .list .PList ;
77
+ import com .oracle .graal .python .builtins .objects .object .PythonObjectLibrary ;
77
78
import com .oracle .graal .python .builtins .objects .slice .PSlice ;
78
79
import com .oracle .graal .python .builtins .objects .slice .PSlice .SliceInfo ;
79
80
import com .oracle .graal .python .builtins .objects .str .StringNodes .CastToJavaStringCheckedNode ;
105
106
import com .oracle .graal .python .nodes .util .CastToJavaStringNodeGen ;
106
107
import com .oracle .graal .python .runtime .ExecutionContext .IndirectCallContext ;
107
108
import com .oracle .graal .python .runtime .PythonContext ;
109
+ import com .oracle .graal .python .runtime .PythonOptions ;
108
110
import com .oracle .graal .python .runtime .exception .PException ;
109
111
import com .oracle .graal .python .runtime .formatting .StringFormatter ;
110
112
import com .oracle .truffle .api .CompilerDirectives ;
118
120
import com .oracle .truffle .api .dsl .Specialization ;
119
121
import com .oracle .truffle .api .dsl .TypeSystemReference ;
120
122
import com .oracle .truffle .api .frame .VirtualFrame ;
123
+ import com .oracle .truffle .api .library .CachedLibrary ;
121
124
import com .oracle .truffle .api .profiles .ConditionProfile ;
122
125
123
126
@ CoreFunctions (extendClasses = PythonBuiltinClassType .PString )
@@ -642,33 +645,24 @@ protected String getErrorMessage() {
642
645
@ TypeSystemReference (PythonArithmeticTypes .class )
643
646
abstract static class FindBaseNode extends PythonBuiltinNode {
644
647
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 ;
655
649
656
- private CastToIndexNode getEndNode () {
657
- if (endNode == null ) {
650
+ private PythonObjectLibrary getLibrary () {
651
+ if (lib == null ) {
658
652
CompilerDirectives .transferToInterpreterAndInvalidate ();
659
- endNode = insert (CastToIndexNode . createOverflow ( ));
653
+ lib = insert (PythonObjectLibrary . getFactory (). createDispatched ( PythonOptions . getCallSiteInlineCacheMaxDepth () ));
660
654
}
661
- return endNode ;
655
+ return lib ;
662
656
}
663
657
664
658
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 );
666
660
return tmpSlice .computeIndices (length );
667
661
}
668
662
669
663
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 ) );
672
666
return PSlice .computeIndices (start , end , 1 , length );
673
667
}
674
668
@@ -1019,14 +1013,14 @@ PList doStringMaxsplit(String self, @SuppressWarnings("unused") PNone sep, int m
1019
1013
return splitfields (self , maxsplit , appendNode );
1020
1014
}
1021
1015
1022
- @ Specialization (replaces = {"doStringWhitespace" , "doStringSep" , "doStringSepMaxsplit" , "doStringMaxsplit" })
1016
+ @ Specialization (replaces = {"doStringWhitespace" , "doStringSep" , "doStringSepMaxsplit" , "doStringMaxsplit" }, limit = "getCallSiteInlineCacheMaxDepth()" )
1023
1017
Object doGeneric (VirtualFrame frame , Object self , Object sep , Object maxsplit ,
1024
1018
@ Cached CastToJavaStringCheckedNode castSelfNode ,
1025
- @ Cached CastToIndexNode castMaxsplitNode ,
1019
+ @ CachedLibrary ( "maxsplit" ) PythonObjectLibrary lib ,
1026
1020
@ Cached CastToJavaStringCheckedNode castSepNode ,
1027
- @ Shared ( "appendNode" ) @ Cached AppendNode appendNode ) {
1021
+ @ Cached AppendNode appendNode ) {
1028
1022
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 ) );
1030
1024
if (PGuards .isPNone (sep )) {
1031
1025
return splitfields (selfStr , imaxsplit , appendNode );
1032
1026
} else {
@@ -1152,15 +1146,15 @@ PList doStringMaxsplit(VirtualFrame frame, String self, @SuppressWarnings("unuse
1152
1146
return rsplitfields (frame , self , maxsplit , appendNode , reverseNode );
1153
1147
}
1154
1148
1155
- @ Specialization (replaces = {"doStringWhitespace" , "doStringSep" , "doStringSepMaxsplit" , "doStringMaxsplit" })
1149
+ @ Specialization (replaces = {"doStringWhitespace" , "doStringSep" , "doStringSepMaxsplit" , "doStringMaxsplit" }, limit = "getCallSiteInlineCacheMaxDepth()" )
1156
1150
Object doGeneric (VirtualFrame frame , Object self , Object sep , Object maxsplit ,
1157
1151
@ Cached CastToJavaStringCheckedNode castSelfNode ,
1158
- @ Cached CastToIndexNode castMaxsplitNode ,
1152
+ @ CachedLibrary ( "maxsplit" ) PythonObjectLibrary lib ,
1159
1153
@ Cached CastToJavaStringCheckedNode castSepNode ,
1160
- @ Shared ( "appendNode" ) @ Cached AppendNode appendNode ,
1161
- @ Shared ( "reverseNode" ) @ Cached ListReverseNode reverseNode ) {
1154
+ @ Cached AppendNode appendNode ,
1155
+ @ Cached ListReverseNode reverseNode ) {
1162
1156
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 ) );
1164
1158
if (PGuards .isPNone (sep )) {
1165
1159
return rsplitfields (frame , selfStr , imaxsplit , appendNode , reverseNode );
1166
1160
} else {
@@ -1304,18 +1298,18 @@ static String doReplace(String self, String old, String with, int maxCount) {
1304
1298
return sb .toString ();
1305
1299
}
1306
1300
1307
- @ Specialization
1301
+ @ Specialization ( limit = "getCallSiteInlineCacheMaxDepth()" )
1308
1302
static String doGeneric (VirtualFrame frame , Object self , Object old , Object with , Object maxCount ,
1309
1303
@ Cached CastToJavaStringCheckedNode castSelfNode ,
1310
- @ Cached CastToIndexNode castToIndexNode ) {
1304
+ @ CachedLibrary ( "maxCount" ) PythonObjectLibrary lib ) {
1311
1305
1312
1306
String selfStr = castSelfNode .cast (self , INVALID_RECEIVER , "replace" , self );
1313
1307
String oldStr = castSelfNode .cast (old , "replace() argument 1 must be str, not %p" , "replace" , old );
1314
1308
String withStr = castSelfNode .cast (with , "replace() argument 2 must be str, not %p" , "replace" , with );
1315
1309
if (PGuards .isPNone (maxCount )) {
1316
1310
return doReplace (selfStr , oldStr , withStr , PNone .NO_VALUE );
1317
1311
}
1318
- int iMaxCount = castToIndexNode . execute ( frame , maxCount );
1312
+ int iMaxCount = lib . asIndexWithState ( maxCount , PArguments . getThreadState ( frame ) );
1319
1313
return doReplace (selfStr , oldStr , withStr , iMaxCount );
1320
1314
}
1321
1315
}
@@ -1434,13 +1428,12 @@ int doStringStringStartEnd(String self, String substr, int start, int end,
1434
1428
int doGeneric (VirtualFrame frame , Object self , Object sub , Object start , Object end ,
1435
1429
@ Cached CastToJavaStringCheckedNode castSelfNode ,
1436
1430
@ Cached CastToJavaStringCheckedNode castSubNode ,
1437
- @ Cached CastToIndexNode castStartNode ,
1438
- @ Cached CastToIndexNode castEndNode ,
1431
+ @ CachedLibrary (limit = "getCallSiteInlineCacheMaxDepth()" ) PythonObjectLibrary lib ,
1439
1432
@ Cached ("createBinaryProfile()" ) ConditionProfile errorProfile ) {
1440
1433
String selfStr = castSelfNode .cast (self , INVALID_RECEIVER , "index" , self );
1441
1434
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 ) );
1444
1437
return indexOf (selfStr , subStr , istart , iend , errorProfile );
1445
1438
}
1446
1439
@@ -1537,18 +1530,18 @@ String doStringInt(String left, int right) {
1537
1530
return repeatString (left , right );
1538
1531
}
1539
1532
1540
- @ Specialization
1533
+ @ Specialization ( limit = "1" )
1541
1534
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 ));
1544
1537
}
1545
1538
1546
1539
@ Specialization
1547
1540
String doStringObject (VirtualFrame frame , String left , Object right ,
1548
- @ Shared ("castToIndexNode" ) @ Cached ( "createOverflow ()" ) CastToIndexNode castToIndexNode ,
1541
+ @ Shared ("castToIndexNode" ) @ CachedLibrary ( limit = "getCallSiteInlineCacheMaxDepth ()" ) PythonObjectLibrary lib ,
1549
1542
@ Cached IsBuiltinClassProfile typeErrorProfile ) {
1550
1543
try {
1551
- return doStringInt (left , castToIndexNode . execute ( frame , right ));
1544
+ return doStringInt (left , lib . asIndexWithState ( right , PArguments . getThreadState ( frame ) ));
1552
1545
} catch (PException e ) {
1553
1546
e .expect (PythonBuiltinClassType .OverflowError , typeErrorProfile );
1554
1547
throw raise (MemoryError );
@@ -1558,10 +1551,10 @@ String doStringObject(VirtualFrame frame, String left, Object right,
1558
1551
@ Specialization
1559
1552
Object doGeneric (VirtualFrame frame , Object self , Object times ,
1560
1553
@ Cached CastToJavaStringCheckedNode castSelfNode ,
1561
- @ Shared ("castToIndexNode" ) @ Cached ( "createOverflow ()" ) CastToIndexNode castToIndexNode ,
1554
+ @ Shared ("castToIndexNode" ) @ CachedLibrary ( limit = "getCallSiteInlineCacheMaxDepth ()" ) PythonObjectLibrary lib ,
1562
1555
@ Cached IsBuiltinClassProfile typeErrorProfile ) {
1563
1556
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 );
1565
1558
}
1566
1559
1567
1560
@ TruffleBoundary
@@ -1891,22 +1884,11 @@ abstract static class ZFillNode extends PythonBinaryBuiltinNode {
1891
1884
1892
1885
public abstract String executeObject (VirtualFrame frame , String self , Object x );
1893
1886
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()" )
1906
1888
static String doGeneric (VirtualFrame frame , Object self , Object width ,
1907
1889
@ 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 ) ));
1910
1892
1911
1893
}
1912
1894
@@ -2025,24 +2007,24 @@ String doStringIntString(String self, int width, String fill) {
2025
2007
2026
2008
@ Specialization
2027
2009
String doStringObjectObject (VirtualFrame frame , String self , Object width , Object fill ,
2028
- @ Shared ("castToIndexNode" ) @ Cached CastToIndexNode castToIndexNode ,
2010
+ @ Shared ("castToIndexNode" ) @ CachedLibrary ( limit = "getCallSiteInlineCacheMaxDepth()" ) PythonObjectLibrary lib ,
2029
2011
@ Shared ("castFillNode" ) @ Cached CastToJavaStringCheckedNode castFillNode ,
2030
2012
@ Shared ("errorProfile" ) @ Cached ("createBinaryProfile()" ) ConditionProfile errorProfile ) {
2031
2013
String fillStr = PGuards .isNoValue (fill ) ? " " : castFillNode .cast (fill , "" , fill );
2032
2014
if (errorProfile .profile (fillStr .codePointCount (0 , fillStr .length ()) != 1 )) {
2033
2015
throw raise (TypeError , "The fill character must be exactly one character long" );
2034
2016
}
2035
- return make (self , castToIndexNode . execute ( frame , width ), fillStr );
2017
+ return make (self , lib . asIndexWithState ( width , PArguments . getThreadState ( frame ) ), fillStr );
2036
2018
}
2037
2019
2038
2020
@ Specialization
2039
2021
String doGeneric (VirtualFrame frame , Object self , Object width , Object fill ,
2040
2022
@ Cached CastToJavaStringCheckedNode castSelfNode ,
2041
- @ Shared ("castToIndexNode" ) @ Cached CastToIndexNode castToIndexNode ,
2023
+ @ Shared ("castToIndexNode" ) @ CachedLibrary ( limit = "getCallSiteInlineCacheMaxDepth()" ) PythonObjectLibrary lib ,
2042
2024
@ Shared ("castFillNode" ) @ Cached CastToJavaStringCheckedNode castFillNode ,
2043
2025
@ Shared ("errorProfile" ) @ Cached ("createBinaryProfile()" ) ConditionProfile errorProfile ) {
2044
2026
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 );
2046
2028
}
2047
2029
2048
2030
@ TruffleBoundary
@@ -2140,33 +2122,20 @@ public String doString(String primary, PSlice slice) {
2140
2122
}
2141
2123
}
2142
2124
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 ));
2145
2129
try {
2146
- int index = idx ;
2147
-
2148
- if (idx < 0 ) {
2130
+ if (index < 0 ) {
2149
2131
index += primary .length ();
2150
2132
}
2151
-
2152
2133
return charAtToString (primary , index );
2153
2134
} catch (StringIndexOutOfBoundsException | ArithmeticException e ) {
2154
2135
throw raise (IndexError , "IndexError: string index out of range" );
2155
2136
}
2156
2137
}
2157
2138
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
-
2170
2139
@ SuppressWarnings ("unused" )
2171
2140
@ Fallback
2172
2141
Object doGeneric (Object self , Object other ) {
0 commit comments