93
93
import com .oracle .truffle .api .dsl .Cached .Shared ;
94
94
import com .oracle .truffle .api .dsl .GenerateNodeFactory ;
95
95
import com .oracle .truffle .api .dsl .Idempotent ;
96
+ import com .oracle .truffle .api .dsl .ImportStatic ;
96
97
import com .oracle .truffle .api .dsl .NeverDefault ;
97
98
import com .oracle .truffle .api .dsl .NodeFactory ;
98
99
import com .oracle .truffle .api .dsl .ReportPolymorphism ;
@@ -128,29 +129,25 @@ protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFa
128
129
public void initialize (Python3Core core ) {
129
130
addBuiltinConstant ("_with_tregex" , core .getContext ().getLanguage ().getEngineOption (PythonOptions .WithTRegex ));
130
131
addBuiltinConstant ("_with_sre" , core .getContext ().getLanguage ().getEngineOption (PythonOptions .TRegexUsesSREFallback ));
131
- addBuiltinConstant ("_METHOD_SEARCH" , PythonMethod .search );
132
- addBuiltinConstant ("_METHOD_MATCH" , PythonMethod .match );
133
- addBuiltinConstant ("_METHOD_FULLMATCH" , PythonMethod .fullmatch );
132
+ addBuiltinConstant ("_METHOD_SEARCH" , PythonMethod .Search );
133
+ addBuiltinConstant ("_METHOD_MATCH" , PythonMethod .Match );
134
+ addBuiltinConstant ("_METHOD_FULLMATCH" , PythonMethod .FullMatch );
134
135
super .initialize (core );
135
136
}
136
137
137
138
public enum PythonMethod {
138
- search (tsLiteral ("search" )),
139
- match (tsLiteral ("match" )),
140
- fullmatch (tsLiteral ("fullmatch" ));
139
+ Search (tsLiteral ("search" )),
140
+ Match (tsLiteral ("match" )),
141
+ FullMatch (tsLiteral ("fullmatch" ));
141
142
142
- private final TruffleString name ;
143
+ public static final int PYTHON_METHOD_COUNT = PythonMethod . values (). length ;
143
144
144
- private static final PythonMethod [] VALUES = PythonMethod . values () ;
145
+ private final TruffleString name ;
145
146
146
147
PythonMethod (TruffleString name ) {
147
148
this .name = name ;
148
149
}
149
150
150
- public static PythonMethod fromOrdinal (int ordinal ) {
151
- return VALUES [ordinal ];
152
- }
153
-
154
151
public TruffleString getMethodName () {
155
152
return name ;
156
153
}
@@ -301,19 +298,19 @@ private boolean calculateLocaleSensitive() {
301
298
public Object getRegexp (PythonMethod method , boolean mustAdvance ) {
302
299
assert !isLocaleSensitive ();
303
300
switch (method ) {
304
- case search :
301
+ case Search :
305
302
if (mustAdvance ) {
306
303
return mustAdvanceSearchRegexp ;
307
304
} else {
308
305
return searchRegexp ;
309
306
}
310
- case match :
307
+ case Match :
311
308
if (mustAdvance ) {
312
309
return mustAdvanceMatchRegexp ;
313
310
} else {
314
311
return matchRegexp ;
315
312
}
316
- case fullmatch :
313
+ case FullMatch :
317
314
if (mustAdvance ) {
318
315
return mustAdvanceFullMatchRegexp ;
319
316
} else {
@@ -333,21 +330,21 @@ public Object getLocaleSensitiveRegexp(PythonMethod method, boolean mustAdvance,
333
330
private void setRegexp (PythonMethod method , boolean mustAdvance , Object regexp ) {
334
331
assert !isLocaleSensitive ();
335
332
switch (method ) {
336
- case search :
333
+ case Search :
337
334
if (mustAdvance ) {
338
335
mustAdvanceSearchRegexp = regexp ;
339
336
} else {
340
337
searchRegexp = regexp ;
341
338
}
342
339
break ;
343
- case match :
340
+ case Match :
344
341
if (mustAdvance ) {
345
342
mustAdvanceMatchRegexp = regexp ;
346
343
} else {
347
344
matchRegexp = regexp ;
348
345
}
349
346
break ;
350
- case fullmatch :
347
+ case FullMatch :
351
348
if (mustAdvance ) {
352
349
mustAdvanceFullMatchRegexp = regexp ;
353
350
} else {
@@ -476,12 +473,14 @@ Object call(VirtualFrame frame, Object pattern, Object flags,
476
473
@ Builtin (name = "tregex_compile" , minNumOfPositionalArgs = 3 )
477
474
@ TypeSystemReference (PythonArithmeticTypes .class )
478
475
@ GenerateNodeFactory
476
+ @ ImportStatic (PythonMethod .class )
479
477
abstract static class TRegexCompile extends PythonTernaryBuiltinNode {
480
478
481
479
private static final TruffleString T__GETLOCALE = tsLiteral ("_getlocale" );
482
480
483
- // limit of 2 specializations to allow inlining of both a must_advance=False and a
484
- // must_advance=True version in re builtins like sub, split, findall
481
+ // limit of 6 specializations = 3 Python methods * 2 values of mustAdvance
482
+ protected static final int SPECIALIZATION_LIMIT = 2 * PythonMethod .PYTHON_METHOD_COUNT ;
483
+
485
484
@ Specialization (guards = {"tRegexCache == cachedTRegexCache" , "method == cachedMethod" , "mustAdvance == cachedMustAdvance" , "!cachedTRegexCache.isLocaleSensitive()" }, limit = "2" )
486
485
Object cached (TRegexCache tRegexCache , PythonMethod method , boolean mustAdvance ,
487
486
@ Cached ("tRegexCache" ) TRegexCache cachedTRegexCache ,
@@ -495,8 +494,7 @@ protected Object getCompiledRegex(TRegexCache tRegexCache, PythonMethod method,
495
494
return localeNonSensitive (tRegexCache , method , mustAdvance , method , mustAdvance );
496
495
}
497
496
498
- // limit of 6 specializations = 3 Python methods * 2 values of mustAdvance
499
- @ Specialization (guards = {"method == cachedMethod" , "mustAdvance == cachedMustAdvance" , "!tRegexCache.isLocaleSensitive()" }, limit = "6" )
497
+ @ Specialization (guards = {"method == cachedMethod" , "mustAdvance == cachedMustAdvance" , "!tRegexCache.isLocaleSensitive()" }, limit = "SPECIALIZATION_LIMIT" )
500
498
Object localeNonSensitive (TRegexCache tRegexCache , PythonMethod method , boolean mustAdvance ,
501
499
@ Cached ("method" ) PythonMethod cachedMethod ,
502
500
@ Cached ("mustAdvance" ) boolean cachedMustAdvance ) {
@@ -508,8 +506,7 @@ Object localeNonSensitive(TRegexCache tRegexCache, PythonMethod method, boolean
508
506
}
509
507
}
510
508
511
- // limit of 6 specializations = 3 Python methods * 2 values of mustAdvance
512
- @ Specialization (guards = {"method == cachedMethod" , "mustAdvance == cachedMustAdvance" , "tRegexCache.isLocaleSensitive()" }, limit = "6" )
509
+ @ Specialization (guards = {"method == cachedMethod" , "mustAdvance == cachedMustAdvance" , "tRegexCache.isLocaleSensitive()" }, limit = "SPECIALIZATION_LIMIT" )
513
510
Object localeSensitive (TRegexCache tRegexCache , PythonMethod method , boolean mustAdvance ,
514
511
@ Cached ("method" ) PythonMethod cachedMethod ,
515
512
@ Cached ("mustAdvance" ) boolean cachedMustAdvance ,
@@ -603,6 +600,7 @@ protected Object lookupMatchConstructor() {
603
600
@ Builtin (name = "tregex_search" , minNumOfPositionalArgs = 6 )
604
601
@ TypeSystemReference (PythonArithmeticTypes .class )
605
602
@ GenerateNodeFactory
603
+ @ ImportStatic (PythonMethod .class )
606
604
abstract static class TRegexSearch extends PythonSenaryBuiltinNode {
607
605
private static final TruffleString T__PATTERN__TREGEX_CACHE = tsLiteral ("_Pattern__tregex_cache" );
608
606
protected static final TruffleString T__PATTERN__FALLBACK_COMPILE = tsLiteral ("_Pattern__fallback_compile" );
@@ -665,7 +663,7 @@ protected Object doCached(VirtualFrame frame, Object pattern, Object input, Obje
665
663
}
666
664
667
665
@ Specialization (guards = {"tRegexCompileNode.execute(frame, getTRegexCache(readCacheNode, pattern), method, mustAdvance) == compiledRegex" , "method == cachedMethod" ,
668
- "mustAdvance == cachedMustAdvance" , "!tRegexCache.isLocaleSensitive()" }, limit = "1" )
666
+ "mustAdvance == cachedMustAdvance" , "!tRegexCache.isLocaleSensitive()" }, limit = "1" , replaces = "doCached" )
669
667
@ SuppressWarnings ("truffle-static-method" )
670
668
protected Object doCachedRegex (VirtualFrame frame , Object pattern , Object input , Object posArg , Object endPosArg , PythonMethod method , boolean mustAdvance ,
671
669
@ Bind ("this" ) Node inliningTarget ,
@@ -690,8 +688,7 @@ protected Object doCachedRegex(VirtualFrame frame, Object pattern, Object input,
690
688
tRegexCallExec , createMatchFromTRegexResultNode );
691
689
}
692
690
693
- // limit of 3 specializations = 3 Python methods
694
- @ Specialization (guards = "method == cachedMethod" , limit = "3" , replaces = {"doCached" , "doCachedRegex" })
691
+ @ Specialization (guards = "method == cachedMethod" , limit = "PYTHON_METHOD_COUNT" , replaces = {"doCached" , "doCachedRegex" })
695
692
@ SuppressWarnings ("truffle-static-method" )
696
693
@ ReportPolymorphism .Megamorphic
697
694
protected Object doDynamic (VirtualFrame frame , Object pattern , Object input , Object posArg , Object endPosArg , PythonMethod method , boolean mustAdvance ,
0 commit comments