Skip to content

Commit 2c98451

Browse files
committed
Move the Primitive.matchdata_fixup_positions logic inside match_in_region
1 parent 78a88ab commit 2c98451

File tree

4 files changed

+131
-112
lines changed

4 files changed

+131
-112
lines changed

lib/truffle/strscan.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,6 @@ def peep(len)
387387
else
388388
start = @fixed_anchor ? 0 : @pos
389389
md = Truffle::RegexpOperations.match_in_region pattern, @string, @pos, @string.bytesize, headonly, start
390-
Primitive.matchdata_fixup_positions(md, start) if md
391390
end
392391

393392
if md

src/main/java/org/truffleruby/core/regexp/MatchDataNodes.java

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import java.util.Iterator;
1414

1515
import com.oracle.truffle.api.TruffleSafepoint;
16+
import com.oracle.truffle.api.dsl.GenerateCached;
17+
import com.oracle.truffle.api.dsl.GenerateInline;
1618
import com.oracle.truffle.api.dsl.NeverDefault;
1719
import com.oracle.truffle.api.dsl.ReportPolymorphism;
1820
import com.oracle.truffle.api.frame.VirtualFrame;
@@ -188,35 +190,35 @@ private static MultiRegion getCharOffsets(RubyMatchData matchData, AbstractTruff
188190
}
189191
}
190192

191-
@TruffleBoundary
192-
private static void fixupMatchDataForStart(RubyMatchData matchData, int startPos) {
193-
assert startPos != 0;
194-
MultiRegion regs = matchData.region;
195-
for (int i = 0; i < regs.getNumRegs(); i++) {
196-
assert regs.getBeg(i) != RubyMatchData.LAZY && regs
197-
.getEnd(i) != RubyMatchData.LAZY : "Group bounds must be computed before fixupMatchDataForStart()";
198-
if (regs.getBeg(i) >= 0) {
199-
regs.setBeg(i, regs.getBeg(i) + startPos);
200-
regs.setEnd(i, regs.getEnd(i) + startPos);
201-
}
202-
}
203-
}
193+
@GenerateInline
194+
@GenerateCached(false)
195+
public abstract static class FixupMatchDataStartNode extends RubyBaseNode {
204196

205-
@Primitive(name = "matchdata_fixup_positions", lowerFixnum = { 1 })
206-
public abstract static class FixupMatchData extends PrimitiveArrayArgumentsNode {
197+
public abstract void execute(Node node, RubyMatchData matchData, int start);
207198

208199
@Specialization
209-
RubyMatchData fixupMatchData(RubyMatchData matchData, int startPos,
210-
@Cached InlinedConditionProfile nonZeroPos,
200+
static void fixupMatchData(Node node, RubyMatchData matchData, int start,
211201
@Cached InlinedConditionProfile lazyProfile,
212202
@CachedLibrary(limit = "getInteropCacheLimit()") InteropLibrary interop) {
213-
if (nonZeroPos.profile(this, startPos != 0)) {
214-
if (lazyProfile.profile(this, matchData.tRegexResult != null)) {
215-
forceLazyMatchData(matchData, interop);
203+
assert start != 0 : "should be checked by caller";
204+
if (lazyProfile.profile(node, matchData.tRegexResult != null)) {
205+
forceLazyMatchData(matchData, interop);
206+
}
207+
fixupMatchDataForStart(matchData, start);
208+
}
209+
210+
@TruffleBoundary
211+
private static void fixupMatchDataForStart(RubyMatchData matchData, int start) {
212+
assert start != 0;
213+
MultiRegion regs = matchData.region;
214+
for (int i = 0; i < regs.getNumRegs(); i++) {
215+
assert regs.getBeg(i) != RubyMatchData.LAZY && regs
216+
.getEnd(i) != RubyMatchData.LAZY : "Group bounds must be computed before fixupMatchDataForStart()";
217+
if (regs.getBeg(i) >= 0) {
218+
regs.setBeg(i, regs.getBeg(i) + start);
219+
regs.setEnd(i, regs.getEnd(i) + start);
216220
}
217-
fixupMatchDataForStart(matchData, startPos);
218221
}
219-
return matchData;
220222
}
221223
}
222224

src/main/java/org/truffleruby/core/regexp/TRegexCache.java

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,46 +28,46 @@
2828

2929
public final class TRegexCache {
3030

31-
// atStart=false
31+
// onlyMatchAtStart=false
3232
private Object usAsciiRegex;
3333
private Object latin1Regex;
3434
private Object utf8Regex;
3535
private Object binaryRegex;
3636

37-
// atStart=true
37+
// onlyMatchAtStart=true
3838
private Object usAsciiRegexAtStart;
3939
private Object latin1RegexAtStart;
4040
private Object utf8RegexAtStart;
4141
private Object binaryRegexAtStart;
4242

43-
public Object getUSASCIIRegex(boolean atStart) {
44-
return atStart ? usAsciiRegexAtStart : usAsciiRegex;
43+
public Object getUSASCIIRegex(boolean onlyMatchAtStart) {
44+
return onlyMatchAtStart ? usAsciiRegexAtStart : usAsciiRegex;
4545
}
4646

47-
public Object getLatin1Regex(boolean atStart) {
48-
return atStart ? latin1RegexAtStart : latin1Regex;
47+
public Object getLatin1Regex(boolean onlyMatchAtStart) {
48+
return onlyMatchAtStart ? latin1RegexAtStart : latin1Regex;
4949
}
5050

51-
public Object getUTF8Regex(boolean atStart) {
52-
return atStart ? utf8RegexAtStart : utf8Regex;
51+
public Object getUTF8Regex(boolean onlyMatchAtStart) {
52+
return onlyMatchAtStart ? utf8RegexAtStart : utf8Regex;
5353
}
5454

55-
public Object getBinaryRegex(boolean atStart) {
56-
return atStart ? binaryRegexAtStart : binaryRegex;
55+
public Object getBinaryRegex(boolean onlyMatchAtStart) {
56+
return onlyMatchAtStart ? binaryRegexAtStart : binaryRegex;
5757
}
5858

5959
@TruffleBoundary
60-
public Object compile(RubyContext context, RubyRegexp regexp, boolean atStart, RubyEncoding encoding,
60+
public Object compile(RubyContext context, RubyRegexp regexp, boolean onlyMatchAtStart, RubyEncoding encoding,
6161
TRegexCompileNode node) {
62-
Object tregex = compileTRegex(context, regexp, atStart, encoding);
62+
Object tregex = compileTRegex(context, regexp, onlyMatchAtStart, encoding);
6363
if (tregex == null) {
6464
tregex = Nil.INSTANCE;
6565
if (context.getOptions().WARN_TRUFFLE_REGEX_COMPILE_FALLBACK) {
6666
node.getWarnOnFallbackNode().call(
6767
context.getCoreLibrary().truffleRegexpOperationsModule,
6868
"warn_fallback_regex",
6969
regexp,
70-
atStart,
70+
onlyMatchAtStart,
7171
encoding);
7272
}
7373
} else if (isBacktracking(tregex)) {
@@ -76,31 +76,31 @@ public Object compile(RubyContext context, RubyRegexp regexp, boolean atStart, R
7676
context.getCoreLibrary().truffleRegexpOperationsModule,
7777
"warn_backtracking",
7878
regexp,
79-
atStart,
79+
onlyMatchAtStart,
8080
encoding);
8181
}
8282
}
8383

8484
if (encoding == Encodings.US_ASCII) {
85-
if (atStart) {
85+
if (onlyMatchAtStart) {
8686
usAsciiRegexAtStart = tregex;
8787
} else {
8888
usAsciiRegex = tregex;
8989
}
9090
} else if (encoding == Encodings.ISO_8859_1) {
91-
if (atStart) {
91+
if (onlyMatchAtStart) {
9292
latin1RegexAtStart = tregex;
9393
} else {
9494
latin1Regex = tregex;
9595
}
9696
} else if (encoding == Encodings.UTF_8) {
97-
if (atStart) {
97+
if (onlyMatchAtStart) {
9898
utf8RegexAtStart = tregex;
9999
} else {
100100
utf8Regex = tregex;
101101
}
102102
} else if (encoding == Encodings.BINARY) {
103-
if (atStart) {
103+
if (onlyMatchAtStart) {
104104
binaryRegexAtStart = tregex;
105105
} else {
106106
binaryRegex = tregex;
@@ -136,7 +136,8 @@ public static String toTRegexEncoding(RubyEncoding encoding) {
136136
}
137137

138138
@TruffleBoundary
139-
private static Object compileTRegex(RubyContext context, RubyRegexp regexp, boolean atStart, RubyEncoding enc) {
139+
private static Object compileTRegex(RubyContext context, RubyRegexp regexp, boolean onlyMatchAtStart,
140+
RubyEncoding enc) {
140141
String tRegexEncoding = TRegexCache.toTRegexEncoding(enc);
141142
if (tRegexEncoding == null) {
142143
return null;
@@ -166,7 +167,7 @@ private static Object compileTRegex(RubyContext context, RubyRegexp regexp, bool
166167
processedRegexpSource = TStringUtils.toJavaStringOrThrow(latin1string, Encodings.ISO_8859_1);
167168
}
168169

169-
String flags = optionsToFlags(regexp.options, atStart);
170+
String flags = optionsToFlags(regexp.options, onlyMatchAtStart);
170171

171172
String ignoreAtomicGroups = context.getOptions().TRUFFLE_REGEX_IGNORE_ATOMIC_GROUPS
172173
? ",IgnoreAtomicGroups=true"
@@ -187,7 +188,7 @@ private static Object compileTRegex(RubyContext context, RubyRegexp regexp, bool
187188
}
188189
}
189190

190-
public static String optionsToFlags(RegexpOptions options, boolean atStart) {
191+
public static String optionsToFlags(RegexpOptions options, boolean onlyMatchAtStart) {
191192
StringBuilder flags = new StringBuilder(4);
192193
if (options.isMultiline()) {
193194
flags.append('m');
@@ -198,7 +199,7 @@ public static String optionsToFlags(RegexpOptions options, boolean atStart) {
198199
if (options.isExtended()) {
199200
flags.append('x');
200201
}
201-
if (atStart) {
202+
if (onlyMatchAtStart) {
202203
flags.append('y');
203204
}
204205
return flags.toString();

0 commit comments

Comments
 (0)