Skip to content

Commit a48474d

Browse files
committed
Style fixes and missing 'replaces' clause in specialization
1 parent 368e980 commit a48474d

File tree

1 file changed

+24
-27
lines changed

1 file changed

+24
-27
lines changed

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

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
import com.oracle.truffle.api.dsl.Cached.Shared;
9494
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
9595
import com.oracle.truffle.api.dsl.Idempotent;
96+
import com.oracle.truffle.api.dsl.ImportStatic;
9697
import com.oracle.truffle.api.dsl.NeverDefault;
9798
import com.oracle.truffle.api.dsl.NodeFactory;
9899
import com.oracle.truffle.api.dsl.ReportPolymorphism;
@@ -128,29 +129,25 @@ protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFa
128129
public void initialize(Python3Core core) {
129130
addBuiltinConstant("_with_tregex", core.getContext().getLanguage().getEngineOption(PythonOptions.WithTRegex));
130131
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);
134135
super.initialize(core);
135136
}
136137

137138
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"));
141142

142-
private final TruffleString name;
143+
public static final int PYTHON_METHOD_COUNT = PythonMethod.values().length;
143144

144-
private static final PythonMethod[] VALUES = PythonMethod.values();
145+
private final TruffleString name;
145146

146147
PythonMethod(TruffleString name) {
147148
this.name = name;
148149
}
149150

150-
public static PythonMethod fromOrdinal(int ordinal) {
151-
return VALUES[ordinal];
152-
}
153-
154151
public TruffleString getMethodName() {
155152
return name;
156153
}
@@ -301,19 +298,19 @@ private boolean calculateLocaleSensitive() {
301298
public Object getRegexp(PythonMethod method, boolean mustAdvance) {
302299
assert !isLocaleSensitive();
303300
switch (method) {
304-
case search:
301+
case Search:
305302
if (mustAdvance) {
306303
return mustAdvanceSearchRegexp;
307304
} else {
308305
return searchRegexp;
309306
}
310-
case match:
307+
case Match:
311308
if (mustAdvance) {
312309
return mustAdvanceMatchRegexp;
313310
} else {
314311
return matchRegexp;
315312
}
316-
case fullmatch:
313+
case FullMatch:
317314
if (mustAdvance) {
318315
return mustAdvanceFullMatchRegexp;
319316
} else {
@@ -333,21 +330,21 @@ public Object getLocaleSensitiveRegexp(PythonMethod method, boolean mustAdvance,
333330
private void setRegexp(PythonMethod method, boolean mustAdvance, Object regexp) {
334331
assert !isLocaleSensitive();
335332
switch (method) {
336-
case search:
333+
case Search:
337334
if (mustAdvance) {
338335
mustAdvanceSearchRegexp = regexp;
339336
} else {
340337
searchRegexp = regexp;
341338
}
342339
break;
343-
case match:
340+
case Match:
344341
if (mustAdvance) {
345342
mustAdvanceMatchRegexp = regexp;
346343
} else {
347344
matchRegexp = regexp;
348345
}
349346
break;
350-
case fullmatch:
347+
case FullMatch:
351348
if (mustAdvance) {
352349
mustAdvanceFullMatchRegexp = regexp;
353350
} else {
@@ -476,12 +473,14 @@ Object call(VirtualFrame frame, Object pattern, Object flags,
476473
@Builtin(name = "tregex_compile", minNumOfPositionalArgs = 3)
477474
@TypeSystemReference(PythonArithmeticTypes.class)
478475
@GenerateNodeFactory
476+
@ImportStatic(PythonMethod.class)
479477
abstract static class TRegexCompile extends PythonTernaryBuiltinNode {
480478

481479
private static final TruffleString T__GETLOCALE = tsLiteral("_getlocale");
482480

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+
485484
@Specialization(guards = {"tRegexCache == cachedTRegexCache", "method == cachedMethod", "mustAdvance == cachedMustAdvance", "!cachedTRegexCache.isLocaleSensitive()"}, limit = "2")
486485
Object cached(TRegexCache tRegexCache, PythonMethod method, boolean mustAdvance,
487486
@Cached("tRegexCache") TRegexCache cachedTRegexCache,
@@ -495,8 +494,7 @@ protected Object getCompiledRegex(TRegexCache tRegexCache, PythonMethod method,
495494
return localeNonSensitive(tRegexCache, method, mustAdvance, method, mustAdvance);
496495
}
497496

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")
500498
Object localeNonSensitive(TRegexCache tRegexCache, PythonMethod method, boolean mustAdvance,
501499
@Cached("method") PythonMethod cachedMethod,
502500
@Cached("mustAdvance") boolean cachedMustAdvance) {
@@ -508,8 +506,7 @@ Object localeNonSensitive(TRegexCache tRegexCache, PythonMethod method, boolean
508506
}
509507
}
510508

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")
513510
Object localeSensitive(TRegexCache tRegexCache, PythonMethod method, boolean mustAdvance,
514511
@Cached("method") PythonMethod cachedMethod,
515512
@Cached("mustAdvance") boolean cachedMustAdvance,
@@ -603,6 +600,7 @@ protected Object lookupMatchConstructor() {
603600
@Builtin(name = "tregex_search", minNumOfPositionalArgs = 6)
604601
@TypeSystemReference(PythonArithmeticTypes.class)
605602
@GenerateNodeFactory
603+
@ImportStatic(PythonMethod.class)
606604
abstract static class TRegexSearch extends PythonSenaryBuiltinNode {
607605
private static final TruffleString T__PATTERN__TREGEX_CACHE = tsLiteral("_Pattern__tregex_cache");
608606
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
665663
}
666664

667665
@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")
669667
@SuppressWarnings("truffle-static-method")
670668
protected Object doCachedRegex(VirtualFrame frame, Object pattern, Object input, Object posArg, Object endPosArg, PythonMethod method, boolean mustAdvance,
671669
@Bind("this") Node inliningTarget,
@@ -690,8 +688,7 @@ protected Object doCachedRegex(VirtualFrame frame, Object pattern, Object input,
690688
tRegexCallExec, createMatchFromTRegexResultNode);
691689
}
692690

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"})
695692
@SuppressWarnings("truffle-static-method")
696693
@ReportPolymorphism.Megamorphic
697694
protected Object doDynamic(VirtualFrame frame, Object pattern, Object input, Object posArg, Object endPosArg, PythonMethod method, boolean mustAdvance,

0 commit comments

Comments
 (0)