Skip to content

Commit

Permalink
TRegex: merge executeFindSuccessor into the main DFA loop
Browse files Browse the repository at this point in the history
  • Loading branch information
djoooooe committed Jun 10, 2020
1 parent 158fab3 commit 874207e
Show file tree
Hide file tree
Showing 8 changed files with 333 additions and 352 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ public int inputReadRaw(TRegexExecutorLocals locals, int index, boolean forward)
}

public void inputAdvance(TRegexExecutorLocals locals) {
assert isForward() ? locals.getIndex() < locals.getNextIndex() : locals.getIndex() > locals.getNextIndex();
locals.setIndex(locals.getNextIndex());
}

Expand All @@ -187,7 +186,7 @@ public void inputSkipReverse(TRegexExecutorLocals locals) {
inputSkipIntl(locals, !isForward());
}

private void inputSkipIntl(TRegexExecutorLocals locals, boolean forward) {
protected void inputSkipIntl(TRegexExecutorLocals locals, boolean forward) {
if (getEncoding() == Encodings.UTF_8) {
if (forward) {
int c = inputReadRaw(locals, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,36 +116,30 @@ public AllTransitionsInOneTreeMatcher(int[] ranges, short[] successors, AllTrans
this.leafMatchers = leafMatchers;
}

public int checkMatchTree(TRegexDFAExecutorLocals locals, TRegexDFAExecutorNode executor, DFAStateNode stateNode, int c) {
public int checkMatchTree(int c) {
CompilerAsserts.partialEvaluationConstant(this);
CompilerAsserts.partialEvaluationConstant(stateNode);
return checkMatchTree(locals, executor, stateNode, 0, ranges.length - 1, c);
return checkMatchTree(0, ranges.length - 1, c);
}

/**
* Recursive binary-search through {@code ranges}.
*/
private int checkMatchTree(TRegexDFAExecutorLocals locals, TRegexDFAExecutorNode executor, DFAStateNode stateNode, int fromIndex, int toIndex, int c) {
CompilerAsserts.partialEvaluationConstant(stateNode);
private int checkMatchTree(int fromIndex, int toIndex, int c) {
CompilerAsserts.partialEvaluationConstant(fromIndex);
CompilerAsserts.partialEvaluationConstant(toIndex);
if (fromIndex > toIndex) {
final short successor = successors[fromIndex];
if (successor == -1) {
return successor;
} else if (successor < -1) {
return checkMatchLeaf((successor * -1) - 2, locals, executor, stateNode, c);
} else {
stateNode.successorFound(locals, executor, successor);
if (successor < -1) {
return checkMatchLeaf((successor * -1) - 2, c);
}
return successor;
}
final int mid = (fromIndex + toIndex) >>> 1;
CompilerAsserts.partialEvaluationConstant(mid);
if (c < ranges[mid]) {
return checkMatchTree(locals, executor, stateNode, fromIndex, mid - 1, c);
return checkMatchTree(fromIndex, mid - 1, c);
} else {
return checkMatchTree(locals, executor, stateNode, mid + 1, toIndex, c);
return checkMatchTree(mid + 1, toIndex, c);
}
}

Expand All @@ -155,7 +149,7 @@ private int checkMatchTree(TRegexDFAExecutorLocals locals, TRegexDFAExecutorNode
* converted to bit sets.
*/
@ExplodeLoop
private int checkMatchLeaf(int iLeaf, TRegexDFAExecutorLocals locals, TRegexDFAExecutorNode executor, DFAStateNode stateNode, int c) {
private int checkMatchLeaf(int iLeaf, int c) {
CompilerAsserts.partialEvaluationConstant(iLeaf);
AllTransitionsInOneTreeLeafMatcher leafMatcher = leafMatchers[iLeaf];
int lowByte = BitSets.lowByte(c);
Expand All @@ -164,19 +158,17 @@ private int checkMatchLeaf(int iLeaf, TRegexDFAExecutorLocals locals, TRegexDFAE
if (BitSets.get(leafMatcher.bitSets[i], lowByte)) {
final short successor = leafMatcher.successors[i];
CompilerAsserts.partialEvaluationConstant(successor);
stateNode.successorFound(locals, executor, successor);
return successor;
}
}
return checkMatchLeafSubTree(locals, executor, stateNode, leafMatcher, 0, leafMatcher.ranges.length - 1, c);
return checkMatchLeafSubTree(leafMatcher, 0, leafMatcher.ranges.length - 1, c);
}

/**
* Recursive binary-search through {@code ranges} of a {@link AllTransitionsInOneTreeLeafMatcher
* leaf matcher}.
*/
private static int checkMatchLeafSubTree(TRegexDFAExecutorLocals locals, TRegexDFAExecutorNode executor, DFAStateNode stateNode,
AllTransitionsInOneTreeLeafMatcher leafMatcher, int fromIndex, int toIndex, int c) {
private static int checkMatchLeafSubTree(AllTransitionsInOneTreeLeafMatcher leafMatcher, int fromIndex, int toIndex, int c) {
CompilerAsserts.partialEvaluationConstant(leafMatcher);
CompilerAsserts.partialEvaluationConstant(fromIndex);
CompilerAsserts.partialEvaluationConstant(toIndex);
Expand All @@ -191,16 +183,15 @@ private static int checkMatchLeafSubTree(TRegexDFAExecutorLocals locals, TRegexD
// TODO: move bitset matches here. requires PE intrinsic.
return successor;
} else {
stateNode.successorFound(locals, executor, successor);
return successor;
}
}
final int mid = (fromIndex + toIndex) >>> 1;
CompilerAsserts.partialEvaluationConstant(mid);
if (c < leafMatcher.ranges[mid]) {
return checkMatchLeafSubTree(locals, executor, stateNode, leafMatcher, fromIndex, mid - 1, c);
return checkMatchLeafSubTree(leafMatcher, fromIndex, mid - 1, c);
} else {
return checkMatchLeafSubTree(locals, executor, stateNode, leafMatcher, mid + 1, toIndex, c);
return checkMatchLeafSubTree(leafMatcher, mid + 1, toIndex, c);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,8 @@ public DFAStateNode createNodeSplitCopy(short copyID) {
return new BackwardDFAStateNode(this, copyID);
}

private int getBackwardPrefixStateIndex() {
int getBackwardPrefixStateIndex() {
assert hasBackwardPrefixState();
return getSuccessors().length - 1;
}

@Override
int atEnd(TRegexDFAExecutorLocals locals, TRegexDFAExecutorNode executor) {
super.atEnd(locals, executor);
if (hasBackwardPrefixState() && locals.getIndex() > 0) {
assert locals.getIndex() == locals.getFromIndex();
/*
* We have reached the starting index of the forward matcher, so we have to switch to
* backward-prefix-states. These states will match look-behind assertions only.
*/
locals.setCurMinIndex(0);
return getBackwardPrefixStateIndex();
}
return FS_RESULT_NO_SUCCESSOR;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,4 @@ public final short getId() {
public final short[] getSuccessors() {
return successors;
}

/**
* Calculates this state's successor and returns its ID ({@link DFAStateNode#getId()}) via
* {@link TRegexDFAExecutorLocals#setSuccessorIndex(int)}. This return value is called
* "successor index" and may either be an index of the successors array (between 0 and
* {@link #getSuccessors()}{@code .length}) or {@link #FS_RESULT_NO_SUCCESSOR}.
*
* @param locals a virtual frame as described by {@link TRegexDFAExecutorProperties}.
* @param executor this node's parent {@link TRegexDFAExecutorNode}.
* @param compactString
*/
public abstract void executeFindSuccessor(TRegexDFAExecutorLocals locals, TRegexDFAExecutorNode executor, boolean compactString);
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,37 +61,24 @@ public DFAFindInnerLiteralStateNode(short id, short[] successors, InnerLiteral i
this.prefixMatcher = prefixMatcher;
}

public InnerLiteral getInnerLiteral() {
return innerLiteral;
}

@Override
public DFAAbstractStateNode createNodeSplitCopy(short copyID) {
return new DFAFindInnerLiteralStateNode(copyID, Arrays.copyOf(getSuccessors(), getSuccessors().length), innerLiteral, prefixMatcher);
}

@Override
public void executeFindSuccessor(TRegexDFAExecutorLocals locals, TRegexDFAExecutorNode executor, boolean compactString) {
assert executor.isForward();
while (true) {
if (!executor.inputHasNext(locals)) {
locals.setSuccessorIndex(FS_RESULT_NO_SUCCESSOR);
return;
}
locals.setIndex(indexOfNode.execute(locals.getInput(), locals.getIndex(), executor.getMaxIndex(locals), innerLiteral.getLiteral().content(), innerLiteral.getMaskContent()));
if (locals.getIndex() < 0) {
locals.setSuccessorIndex(FS_RESULT_NO_SUCCESSOR);
return;
}
if (prefixMatcher == null || prefixMatcherMatches(locals, compactString)) {
if (prefixMatcher == null && executor.isSimpleCG()) {
locals.getCGData().results[0] = locals.getIndex();
}
executor.inputIncRaw(locals, innerLiteral.getLiteral().encodedLength());
locals.setSuccessorIndex(0);
return;
}
executor.inputIncRaw(locals);
}
public boolean hasPrefixMatcher() {
return prefixMatcher != null;
}

private boolean prefixMatcherMatches(TRegexDFAExecutorLocals locals, boolean compactString) {
int executeInnerLiteralSearch(TRegexDFAExecutorLocals locals, TRegexDFAExecutorNode executor) {
return indexOfNode.execute(locals.getInput(), locals.getIndex(), executor.getMaxIndex(locals), innerLiteral.getLiteral().content(), innerLiteral.getMaskContent());
}

boolean prefixMatcherMatches(TRegexDFAExecutorLocals locals, boolean compactString) {
Object result = prefixMatcher.execute(locals.toInnerLiteralBackwardLocals(), compactString);
return prefixMatcher.isSimpleCG() ? result != null : (int) result != TRegexDFAExecutorNode.NO_MATCH;
}
Expand All @@ -105,5 +92,4 @@ public JsonValue toJson() {
Json.prop("loopToSelf", false),
Json.prop("transitions", Json.array(Json.obj(Json.prop("matcher", "innerLiteral(" + innerLiteral.getLiteral() + ")"), Json.prop("target", successors[0])))));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -90,28 +90,6 @@ public DFAAbstractStateNode createNodeSplitCopy(short copyID) {
return new DFAInitialStateNode(this);
}

@Override
public void executeFindSuccessor(TRegexDFAExecutorLocals locals, TRegexDFAExecutorNode executor, boolean compactString) {
if (searching) {
locals.setSuccessorIndex(executor.rewindUpTo(locals, 0, getPrefixLength()));
} else {
if (executor.isForward() && locals.getIndex() < locals.getFromIndex()) {
locals.setSuccessorIndex(executor.countUpTo(locals, locals.getFromIndex(), getPrefixLength()));
} else {
locals.setSuccessorIndex(0);
}
}
if (!executor.inputAtBegin(locals)) {
locals.setSuccessorIndex(locals.getSuccessorIndex() + (successors.length / 2));
}
if (genericCG) {
locals.setLastTransition((short) 0);
}
if (executor.recordExecution()) {
executor.getDebugRecorder().setInitialIndex(locals.getIndex());
}
}

@TruffleBoundary
@Override
public JsonValue toJson() {
Expand Down
Loading

0 comments on commit 874207e

Please sign in to comment.