source: icGREP/icgrep-devel/icgrep/re/re_compiler.cpp @ 4631

Last change on this file since 4631 was 4631, checked in by nmedfort, 4 years ago

Fix for SCX and updated property objects.

File size: 23.8 KB
Line 
1/*
2 *  Copyright (c) 2014 International Characters.
3 *  This software is licensed to the public under the Open Software License 3.0.
4 *  icgrep is a trademark of International Characters.
5 */
6
7#include "re_compiler.h"
8//Regular Expressions
9#include <re/re_name.h>
10#include <re/re_any.h>
11#include <re/re_start.h>
12#include <re/re_end.h>
13#include <re/re_alt.h>
14#include <re/re_cc.h>
15#include <re/re_seq.h>
16#include <re/re_rep.h>
17#include <re/re_diff.h>
18#include <re/re_intersect.h>
19#include <re/re_assertion.h>
20#include <re/re_analysis.h>
21#include <cc/cc_namemap.hpp>
22#include <pablo/codegenstate.h>
23#include <resolve_properties.h>
24#include <assert.h>
25#include <stdexcept>
26
27#include "llvm/Support/CommandLine.h"
28static cl::OptionCategory fREcompilationOptions("Regex Compilation Options",
29                                      "These options control the compilation of regular expressions to Pablo.");
30
31static cl::opt<bool> DisableLog2BoundedRepetition("disable-log2-bounded-repetition", cl::init(false),
32                     cl::desc("disable log2 optimizations for bounded repetition of bytes"), cl::cat(fREcompilationOptions));
33static cl::opt<int> IfInsertionGap("if-insertion-gap", cl::init(3), cl::desc("minimum number of nonempty elements between inserted if short-circuit tests"), cl::cat(fREcompilationOptions));
34static cl::opt<bool> DisableMatchStar("disable-matchstar", cl::init(false),
35                     cl::desc("disable MatchStar optimization"), cl::cat(fREcompilationOptions));
36static cl::opt<bool> DisableUnicodeMatchStar("disable-unicode-matchstar", cl::init(false),
37                     cl::desc("disable Unicode MatchStar optimization"), cl::cat(fREcompilationOptions));
38static cl::opt<bool> DisableUnicodeLineBreak("disable-unicode-linebreak", cl::init(false),
39                     cl::desc("disable Unicode line breaks - use LF only"), cl::cat(fREcompilationOptions));
40static cl::opt<bool> DisablePregeneratedUnicode("disable-pregenerated-unicode", cl::init(false),
41                     cl::desc("disable use of pregenerated Unicode character class sets"), cl::cat(fREcompilationOptions));
42
43using namespace pablo;
44
45namespace re {
46
47bool IsPregeneratedUnicodeEnabled() {
48    return !DisablePregeneratedUnicode;
49}
50
51RE_Compiler::RE_Compiler(cc::CC_Compiler & ccCompiler)
52: mCCCompiler(ccCompiler)
53, mLineFeed(nullptr)
54, mCRLF(nullptr)
55, mUnicodeLineBreak(nullptr)
56, mInitial(nullptr)
57, mNonFinal(nullptr)
58, mFinal(nullptr)
59, mWhileTest(nullptr)
60, mStarDepth(0)
61, mPB(ccCompiler.getBuilder().getPabloBlock(), ccCompiler.getBuilder())
62, mUCDCompiler(ccCompiler)
63{
64
65}
66
67   
68MarkerType RE_Compiler::AdvanceMarker(MarkerType m, MarkerPosition newpos, PabloBuilder & pb) {
69    if (m.pos == newpos) return m;
70    PabloAST * a = m.stream;
71    if (m.pos == FinalMatchByte) {
72        // Must advance at least to InitialPostPositionByte
73        a = pb.createAdvance(a, 1, "adv");
74    }
75    // Now at InitialPostPositionByte; is a further advance needed?
76    if (newpos == FinalPostPositionByte) {
77        // Must advance through nonfinal bytes
78        a = pb.createScanThru(pb.createAnd(mInitial, a), mNonFinal, "scanToFinal");
79    }
80    return {newpos, a};
81}
82
83void RE_Compiler::AlignMarkers(MarkerType & m1, MarkerType & m2, PabloBuilder & pb) {
84    if (m1.pos < m2.pos) {
85        m1 = AdvanceMarker(m1, m2.pos, pb); 
86    }
87    else if (m2.pos < m1.pos) {
88        m2 = AdvanceMarker(m2, m1.pos, pb); 
89    }
90}
91
92#define UNICODE_LINE_BREAK (!DisableUnicodeLineBreak)
93
94
95void RE_Compiler::initializeRequiredStreams() {
96
97    Assign * LF = mPB.createAssign("LF", mCCCompiler.compileCC(makeCC(0x0A)));
98    mLineFeed = LF;
99    PabloAST * CR = mCCCompiler.compileCC(makeCC(0x0D));
100    PabloAST * LF_VT_FF_CR = mCCCompiler.compileCC(makeCC(0x0A, 0x0D));
101
102    PabloBuilder crb = PabloBuilder::Create(mPB);
103    PabloAST * cr1 = crb.createAdvance(CR, 1, "cr1");
104    Assign * acrlf = crb.createAssign("crlf", crb.createAnd(cr1, LF));
105    mPB.createIf(CR, {acrlf}, crb);
106    mCRLF = acrlf;
107
108    PabloAST * u8pfx = mCCCompiler.compileCC(makeCC(0xC0, 0xFF));
109    PabloBuilder it = PabloBuilder::Create(mPB);
110    PabloAST * u8pfx2 = mCCCompiler.compileCC(makeCC(0xC2, 0xDF), it);
111    PabloAST * u8pfx3 = mCCCompiler.compileCC(makeCC(0xE0, 0xEF), it);
112    PabloAST * u8pfx4 = mCCCompiler.compileCC(makeCC(0xF0, 0xF4), it);
113    Assign * u8suffix = it.createAssign("u8suffix", mCCCompiler.compileCC(makeCC(0x80, 0xBF)));
114   
115    //
116    // Two-byte sequences
117    PabloBuilder it2 = PabloBuilder::Create(it);
118    Assign * u8scope22 = it2.createAssign("u8scope22", it2.createAdvance(u8pfx2, 1));
119    Assign * NEL = it2.createAssign("NEL", it2.createAnd(it2.createAdvance(mCCCompiler.compileCC(makeCC(0xC2), it2), 1), mCCCompiler.compileCC(makeCC(0x85), it2)));
120    it.createIf(u8pfx2, {u8scope22, NEL}, it2);
121   
122    //
123    // Three-byte sequences
124    PabloBuilder it3 = PabloBuilder::Create(it);
125    Assign * u8scope32 = it3.createAssign("u8scope32", it3.createAdvance(u8pfx3, 1));
126    PabloAST * u8scope33 = it3.createAdvance(u8pfx3, 2);
127    Assign * u8scope3X = it3.createAssign("u8scope3X", it3.createOr(u8scope32, u8scope33));
128    PabloAST * E2_80 = it3.createAnd(it3.createAdvance(mCCCompiler.compileCC(makeCC(0xE2), it3), 1), mCCCompiler.compileCC(makeCC(0x80), it3));
129    Assign * LS_PS = it3.createAssign("LS_PS", it3.createAnd(it3.createAdvance(E2_80, 1), mCCCompiler.compileCC(makeCC(0xA8,0xA9), it3)));
130    PabloAST * E0_invalid = it3.createAnd(it3.createAdvance(mCCCompiler.compileCC(makeCC(0xE0), it3), 1), mCCCompiler.compileCC(makeCC(0x80, 0x9F), it3));
131    PabloAST * ED_invalid = it3.createAnd(it3.createAdvance(mCCCompiler.compileCC(makeCC(0xED), it3), 1), mCCCompiler.compileCC(makeCC(0xA0, 0xBF), it3));
132    Assign * EX_invalid = it3.createAssign("EX_invalid", it3.createOr(E0_invalid, ED_invalid));
133    it.createIf(u8pfx3, {u8scope32, u8scope3X, LS_PS, EX_invalid}, it3);
134 
135    //
136    // Four-byte sequences
137    PabloBuilder it4 = PabloBuilder::Create(it);
138    PabloAST * u8scope42 = it4.createAdvance(u8pfx4, 1, "u8scope42");
139    PabloAST * u8scope43 = it4.createAdvance(u8scope42, 1, "u8scope43");
140    PabloAST * u8scope44 = it4.createAdvance(u8scope43, 1, "u8scope44");
141    Assign * u8scope4nonfinal = it4.createAssign("u8scope4nonfinal", it4.createOr(u8scope42, u8scope43));
142    Assign * u8scope4X = it4.createAssign("u8scope4X", it4.createOr(u8scope4nonfinal, u8scope44));
143    PabloAST * F0_invalid = it4.createAnd(it4.createAdvance(mCCCompiler.compileCC(makeCC(0xF0), it4), 1), mCCCompiler.compileCC(makeCC(0x80, 0x8F), it4));
144    PabloAST * F4_invalid = it4.createAnd(it4.createAdvance(mCCCompiler.compileCC(makeCC(0xF4), it4), 1), mCCCompiler.compileCC(makeCC(0x90, 0xBF), it4));
145    Assign * FX_invalid = it4.createAssign("FX_invalid", it4.createOr(F0_invalid, F4_invalid));
146    it.createIf(u8pfx4, {u8scope4nonfinal, u8scope4X, FX_invalid}, it4);
147
148    //
149    // Invalid cases
150    PabloAST * anyscope = it.createOr(u8scope22, it.createOr(u8scope3X, u8scope4X));
151    PabloAST * legalpfx = it.createOr(it.createOr(u8pfx2, u8pfx3), u8pfx4);
152    //  Any scope that does not have a suffix byte, and any suffix byte that is not in
153    //  a scope is a mismatch, i.e., invalid UTF-8.
154    PabloAST * mismatch = it.createXor(anyscope, u8suffix);
155    //
156    PabloAST * EF_invalid = it.createOr(EX_invalid, FX_invalid);
157    PabloAST * pfx_invalid = it.createXor(u8pfx, legalpfx);
158    Assign * u8invalid = it.createAssign("u8invalid", it.createOr(pfx_invalid, it.createOr(mismatch, EF_invalid)));
159    Assign * u8valid = it.createAssign("u8valid", it.createNot(u8invalid));
160    //
161    //
162   
163    Assign * valid_pfx = it.createAssign("valid_pfx", it.createAnd(u8pfx, u8valid));
164    mNonFinal = it.createAssign("nonfinal", it.createAnd(it.createOr(it.createOr(u8pfx, u8scope32), u8scope4nonfinal), u8valid));
165   
166    Assign * NEL_LS_PS = it.createAssign("NEL_LS_PS", it.createOr(NEL, LS_PS));
167    mPB.createIf(u8pfx, {u8invalid, valid_pfx, mNonFinal, NEL_LS_PS}, it);
168   
169    PabloAST * LB_chars = mPB.createOr(LF_VT_FF_CR, NEL_LS_PS);
170    PabloAST * u8single = mPB.createAnd(mCCCompiler.compileCC(makeCC(0x00, 0x7F)), mPB.createNot(u8invalid));
171    mInitial = mPB.createOr(u8single, valid_pfx, "initial");
172    mFinal = mPB.createNot(mPB.createOr(mNonFinal, u8invalid), "final");
173    mUnicodeLineBreak = mPB.createAnd(LB_chars, mPB.createNot(mCRLF));  // count the CR, but not CRLF
174}
175
176void RE_Compiler::finalizeMatchResult(MarkerType match_result) {
177    //These three lines are specifically for grep.
178    PabloAST * lb = UNICODE_LINE_BREAK ? mUnicodeLineBreak : mLineFeed;
179    PabloAST * v = markerVar(match_result);
180    mPB.createAssign("matches", mPB.createAnd(mPB.createMatchStar(v, mPB.createNot(lb)), lb), 0);
181    mPB.createAssign("lf", mPB.createAnd(lb, mPB.createNot(mCRLF)), 1);
182}
183
184MarkerType RE_Compiler::compile(RE * re, PabloBuilder & pb) {
185    return process(re, makeMarker(FinalPostPositionByte, pb.createOnes()), pb);
186}
187
188PabloAST * RE_Compiler::nextUnicodePosition(MarkerType m, PabloBuilder & pb) {
189    if (markerPos(m) == FinalPostPositionByte) {
190        return markerVar(m);
191    }
192    else if (markerPos(m) == InitialPostPositionByte) {
193        return pb.createScanThru(pb.createAnd(mInitial, markerVar(m)), mNonFinal);
194    }
195    else {
196        return pb.createScanThru(pb.createAnd(mInitial, pb.createAdvance(markerVar(m), 1)), mNonFinal);
197    }
198}
199
200MarkerType RE_Compiler::process(RE * re, MarkerType marker, PabloBuilder & pb) {
201    if (Name * name = dyn_cast<Name>(re)) {
202        return process(name, marker, pb);
203    }
204    else if (Seq* seq = dyn_cast<Seq>(re)) {
205        return process(seq, marker, pb);
206    }
207    else if (Alt * alt = dyn_cast<Alt>(re)) {
208        return process(alt, marker, pb);
209    }
210    else if (Rep * rep = dyn_cast<Rep>(re)) {
211        return process(rep, marker, pb);
212    }
213    else if (Assertion * a = dyn_cast<Assertion>(re)) {
214        return process(a, marker, pb);
215    }
216    else if (isa<Any>(re)) {
217        PabloAST * nextPos = nextUnicodePosition(marker, pb);
218        PabloAST * dot = pb.createNot(UNICODE_LINE_BREAK ? pb.createOr(mUnicodeLineBreak, mCRLF) : mLineFeed);
219        return makeMarker(FinalMatchByte, pb.createAnd(nextPos, dot, "dot"));
220    }
221    else if (Diff * diff = dyn_cast<Diff>(re)) {
222        return process(diff, marker, pb);
223    }
224    else if (Intersect * ix = dyn_cast<Intersect>(re)) {
225        return process(ix, marker, pb);
226    }
227    else if (isa<Start>(re)) {
228        MarkerType m = AdvanceMarker(marker, InitialPostPositionByte, pb);
229        if (UNICODE_LINE_BREAK) {
230            PabloAST * line_end = mPB.createOr(mUnicodeLineBreak, mCRLF);
231            PabloAST * sol = pb.createNot(pb.createOr(pb.createAdvance(pb.createNot(line_end), 1), mCRLF));
232            return makeMarker(InitialPostPositionByte, pb.createAnd(markerVar(m), sol, "sol"));
233        }
234        else {
235            PabloAST * sol = pb.createNot(pb.createAdvance(pb.createNot(mLineFeed), 1));
236            return makeMarker(FinalPostPositionByte, pb.createAnd(markerVar(m), sol, "sol"));
237        }
238    }
239    else if (isa<End>(re)) {
240        if (UNICODE_LINE_BREAK) {
241            PabloAST * nextPos = markerVar(AdvanceMarker(marker, FinalPostPositionByte, pb));
242            return makeMarker(FinalPostPositionByte, pb.createAnd(nextPos, mUnicodeLineBreak, "end"));
243        }
244        PabloAST * nextPos = markerVar(AdvanceMarker(marker, InitialPostPositionByte, pb));  // For LF match
245        return makeMarker(FinalPostPositionByte, pb.createAnd(nextPos, mLineFeed, "eol"));
246    }
247    return marker;
248}
249
250MarkerType RE_Compiler::process(Name * name, MarkerType marker, PabloBuilder & pb) {
251    MarkerType nextPos;
252    if (markerPos(marker) == FinalPostPositionByte) nextPos = marker;
253    else if (name->getType() == Name::Type::Byte) {
254        nextPos = AdvanceMarker(marker, InitialPostPositionByte, pb);
255    }
256    else {
257        nextPos = AdvanceMarker(marker, FinalPostPositionByte, pb);
258    }
259    return makeMarker(FinalMatchByte, pb.createAnd(markerVar(nextPos), getNamedCharacterClassStream(name, pb), "m"));
260}
261
262PabloAST * RE_Compiler::getNamedCharacterClassStream(Name * name, PabloBuilder & pb) {
263    PabloAST * var = name->getCompiled();
264    if (LLVM_LIKELY(var != nullptr)) {
265        return var;
266    }
267    else if (name->getDefinition() != nullptr) {
268        MarkerType m = compile(name->getDefinition(), pb);
269        assert(markerPos(m) == FinalMatchByte);
270        var = markerVar(m);
271    }
272    else if (name->getType() == Name::Type::UnicodeProperty) {
273        if (IsPregeneratedUnicodeEnabled()) {
274            var = mPB.createCall(name->getName());
275        }
276        else {
277            var = mUCDCompiler.generateWithDefaultIfHierarchy(resolveUnicodeSet(name), pb);
278        }
279    }
280    else {
281        throw std::runtime_error("Unresolved name " + name->getName());
282    }
283    var = pb.createAnd(var, pb.createNot(UNICODE_LINE_BREAK ? mUnicodeLineBreak : mLineFeed));
284    name->setCompiled(var);
285    return var;
286}
287
288
289MarkerType RE_Compiler::process(Seq * seq, MarkerType marker, PabloBuilder & pb) {
290    // if-hierarchies are not inserted within unbounded repetitions
291    if (mStarDepth > 0) {
292        for (RE * re : *seq) {
293            marker = process(re, marker, pb);
294        }
295        return marker;
296    }
297    else {
298        return processSeqTail(seq->begin(), seq->end(), 0, marker, pb);
299    }
300}
301
302MarkerType RE_Compiler::processSeqTail(Seq::iterator current, Seq::iterator end, int matchLenSoFar, MarkerType marker, PabloBuilder & pb) {
303    if (current == end) return marker;
304    if (matchLenSoFar < IfInsertionGap) {
305        RE * r = *current;
306        marker = process(r, marker, pb);
307        current++;
308        return processSeqTail(current, end, matchLenSoFar + minMatchLength(r), marker, pb);
309    }
310    else {
311        PabloBuilder nested = PabloBuilder::Create(pb);
312        MarkerType m1 = processSeqTail(current, end, 0, marker, nested);
313        Assign * m1a = nested.createAssign("m", markerVar(m1));
314        pb.createIf(markerVar(marker), {m1a}, nested);
315        return makeMarker(m1.pos, m1a);
316    }
317}
318
319MarkerType RE_Compiler::process(Alt * alt, MarkerType marker, PabloBuilder & pb) {
320    std::vector<PabloAST *>  accum = {pb.createZeroes(), pb.createZeroes(), pb.createZeroes()};
321    MarkerType const base = marker;
322    // The following may be useful to force a common Advance rather than separate
323    // Advances in each alternative.
324    // MarkerType const base = makeMarker(InitialPostPositionByte, postPositionVar(marker, pb), pb);
325    for (RE * re : *alt) {
326        MarkerType rslt = process(re, base, pb);
327        MarkerPosition p = markerPos(rslt);
328        accum[p] = pb.createOr(accum[p], markerVar(rslt), "alt");
329    }
330    if (isa<Zeroes>(accum[InitialPostPositionByte]) && isa<Zeroes>(accum[FinalPostPositionByte])) {
331        return makeMarker(FinalMatchByte, accum[FinalMatchByte]);
332    }
333    PabloAST * combine = pb.createOr(accum[InitialPostPositionByte], pb.createAdvance(accum[FinalMatchByte], 1), "alt");
334    if (isa<Zeroes>(accum[FinalPostPositionByte])) {
335        return makeMarker(InitialPostPositionByte, combine);
336    }
337    combine = pb.createOr(pb.createScanThru(pb.createAnd(mInitial, combine), mNonFinal), accum[FinalPostPositionByte], "alt");
338    return makeMarker(FinalPostPositionByte, combine);
339}
340
341MarkerType RE_Compiler::process(Assertion * a, MarkerType marker, PabloBuilder & pb) {
342    RE * asserted = a->getAsserted();
343    if (a->getKind() == Assertion::Kind::Lookbehind) {
344        MarkerType m = marker;
345        MarkerType lookback = compile(asserted, pb);
346        AlignMarkers(m, lookback, pb);
347        PabloAST * lb = markerVar(lookback);
348        if (a->getSense() == Assertion::Sense::Negative) {
349            lb = pb.createNot(lb);
350        }
351        return makeMarker(markerPos(m), pb.createAnd(markerVar(marker), lb, "lookback"));
352    }
353    else if (isUnicodeUnitLength(asserted)) {
354        MarkerType lookahead = compile(asserted, pb);
355        assert(markerPos(lookahead) == FinalMatchByte);
356        PabloAST * la = markerVar(lookahead);
357        if (a->getSense() == Assertion::Sense::Negative) {
358            la = pb.createNot(la);
359        }
360        MarkerType fbyte = AdvanceMarker(marker, FinalPostPositionByte, pb);
361        return makeMarker(FinalPostPositionByte, pb.createAnd(markerVar(fbyte), la, "lookahead"));
362    }
363    else {
364        throw std::runtime_error("Unsupported lookahead assertion.");
365    }
366}
367
368MarkerType RE_Compiler::process(Diff * diff, MarkerType marker, PabloBuilder & pb) {
369    RE * lh = diff->getLH();
370    RE * rh = diff->getRH();
371    if (isUnicodeUnitLength(lh) && isUnicodeUnitLength(rh)) {
372        MarkerType t1 = process(lh, marker, pb);
373        MarkerType t2 = process(rh, marker, pb);
374        AlignMarkers(t1, t2, pb);
375        return makeMarker(markerPos(t1), pb.createAnd(markerVar(t1), pb.createNot(markerVar(t2)), "diff"));
376    }
377    throw std::runtime_error("Unsupported Diff operands: " + Printer_RE::PrintRE(diff));
378}
379
380MarkerType RE_Compiler::process(Intersect * x, MarkerType marker, PabloBuilder & pb) {
381    RE * lh = x->getLH();
382    RE * rh = x->getRH();
383    if (isUnicodeUnitLength(lh) && isUnicodeUnitLength(rh)) {
384        MarkerType t1 = process(lh, marker, pb);
385        MarkerType t2 = process(rh, marker, pb);
386        AlignMarkers(t1, t2, pb);
387        return makeMarker(markerPos(t1), pb.createAnd(markerVar(t1), markerVar(t2), "intersect"));
388    }
389    throw std::runtime_error("Unsupported Intersect operands: " + Printer_RE::PrintRE(x));
390}
391
392MarkerType RE_Compiler::process(Rep * rep, MarkerType marker, PabloBuilder & pb) {
393    int lb = rep->getLB();
394    int ub = rep->getUB();
395    if (lb > 0) {
396        marker = processLowerBound(rep->getRE(), lb, marker, pb);
397    }
398    if (ub == Rep::UNBOUNDED_REP) {
399        return processUnboundedRep(rep->getRE(), marker, pb);
400    }
401    else if (ub == lb) { // if (rep->getUB() != Rep::UNBOUNDED_REP)
402        return marker;
403    }
404    else { // if (rep->getUB() != Rep::UNBOUNDED_REP)
405        return processBoundedRep(rep->getRE(), ub - lb, marker, pb);
406    }
407}
408
409/*
410   Given a stream |repeated| marking positions associated with matches to an item
411   of length |repeated_lgth|, compute a stream marking |repeat_count| consecutive
412   occurrences of such items.
413*/
414
415inline PabloAST * RE_Compiler::consecutive1(PabloAST * repeated, int repeated_lgth, int repeat_count, PabloBuilder & pb) {
416        int i = repeated_lgth;
417        int total_lgth = repeat_count * repeated_lgth;
418        PabloAST * consecutive_i = repeated;
419        while (i * 2 <= total_lgth) {
420            PabloAST * v = consecutive_i;
421            PabloAST * v2 =  pb.createAdvance(v, i);
422            i *= 2;
423            consecutive_i = pb.createAnd(v, v2, "at" + std::to_string(i) + "inarow");
424        }       
425        if (i < total_lgth) {
426            PabloAST * v = consecutive_i;
427            consecutive_i = pb.createAnd(v, pb.createAdvance(v, total_lgth - i), "at" + std::to_string(total_lgth) + "inarow");
428        }
429        return consecutive_i;
430}
431
432inline PabloAST * RE_Compiler::reachable(PabloAST *repeated, int repeated_lgth, int repeat_count, PabloBuilder & pb) {
433        int i = repeated_lgth;
434        int total_lgth = repeat_count * repeated_lgth;
435        if (repeat_count == 0) {
436            return repeated;
437        }
438        PabloAST * reachable_i = pb.createOr(repeated, pb.createAdvance(repeated, 1), "within1");
439        while (i * 2 < total_lgth) {
440            PabloAST * v = reachable_i;
441            PabloAST * v2 =  pb.createAdvance(v, i);
442            i *= 2;
443            reachable_i = pb.createOr(v, v2, "within" + std::to_string(i));
444        }       
445        if (i < total_lgth) {
446            PabloAST * v = reachable_i;
447            reachable_i = pb.createOr(v, pb.createAdvance(v, total_lgth - i), "within" + std::to_string(total_lgth));
448        }
449        return reachable_i;
450}
451
452MarkerType RE_Compiler::processLowerBound(RE * repeated, int lb, MarkerType marker, PabloBuilder & pb) {
453    if (isByteLength(repeated) && !DisableLog2BoundedRepetition) {
454        PabloAST * cc = markerVar(compile(repeated, pb));
455        PabloAST * cc_lb = consecutive1(cc, 1, lb, pb);
456        PabloAST * marker_fwd = pb.createAdvance(markerVar(marker), markerPos(marker) == FinalMatchByte ? lb : lb-1);
457        return makeMarker(FinalMatchByte, pb.createAnd(marker_fwd, cc_lb, "lowerbound"));
458    }
459    // Fall through to general case.
460    while (lb-- != 0) {
461        marker = process(repeated, marker, pb);
462    }
463    return marker;
464}
465
466MarkerType RE_Compiler::processBoundedRep(RE * repeated, int ub, MarkerType marker, PabloBuilder & pb) {
467    if (isByteLength(repeated) && ub > 1 && !DisableLog2BoundedRepetition) {
468        // log2 upper bound for fixed length (=1) class
469        // Create a mask of positions reachable within ub from current marker.
470        // Use matchstar, then apply filter.
471        PabloAST * match = markerVar(AdvanceMarker(marker, InitialPostPositionByte, pb));
472        PabloAST * upperLimitMask = reachable(match, 1, ub, pb);
473        PabloAST * cursor = markerVar(AdvanceMarker(marker, InitialPostPositionByte, pb));
474        PabloAST * rep_class_var = markerVar(compile(repeated, pb));
475        return makeMarker(InitialPostPositionByte, pb.createAnd(pb.createMatchStar(cursor, rep_class_var), upperLimitMask, "bounded"));
476    }
477    // Fall through to general case.
478    while (ub-- != 0) {
479        MarkerType a = process(repeated, marker, pb);
480        MarkerType m = marker;
481        AlignMarkers(a, m, pb);
482        marker = makeMarker(markerPos(a), pb.createOr(markerVar(a), markerVar(m), "m"));
483    }
484    return marker;
485}
486
487MarkerType RE_Compiler::processUnboundedRep(RE * repeated, MarkerType marker, PabloBuilder & pb) {
488    // always use PostPosition markers for unbounded repetition.
489    PabloAST * base = markerVar(AdvanceMarker(marker, InitialPostPositionByte, pb));
490   
491    if (isByteLength(repeated)  && !DisableMatchStar) {
492        PabloAST * cc = markerVar(compile(repeated, pb)); 
493        return makeMarker(InitialPostPositionByte, pb.createMatchStar(base, cc, "unbounded"));
494    }
495    else if (isUnicodeUnitLength(repeated) && !DisableMatchStar && !DisableUnicodeMatchStar) {
496        PabloAST * cc = markerVar(compile(repeated, pb));
497        return makeMarker(FinalPostPositionByte, pb.createAnd(pb.createMatchStar(base, pb.createOr(mNonFinal, cc)), mFinal, "unbounded"));
498    }
499    else if (mStarDepth > 0){
500       
501        PabloBuilder * outerb = pb.getParent();
502       
503        Assign * starPending = outerb->createAssign("pending", outerb->createZeroes());
504        Assign * starAccum = outerb->createAssign("accum", outerb->createZeroes());
505       
506        mStarDepth++;
507        PabloAST * m1 = pb.createOr(base, starPending);
508        PabloAST * m2 = pb.createOr(base, starAccum);
509        PabloAST * loopComputation = markerVar(AdvanceMarker(process(repeated, makeMarker(InitialPostPositionByte, m1), pb), InitialPostPositionByte, pb));
510        Next * nextPending = pb.createNext(starPending, pb.createAnd(loopComputation, pb.createNot(m2)));
511        Next * nextStarAccum = pb.createNext(starAccum, pb.createOr(loopComputation, m2));
512        mWhileTest = pb.createOr(mWhileTest, nextPending);
513        mStarDepth--;
514       
515        return makeMarker(InitialPostPositionByte, pb.createAssign("unbounded", pb.createOr(base, nextStarAccum)));
516    }   
517    else {
518        Assign * whileTest = pb.createAssign("test", base);
519        Assign * whilePending = pb.createAssign("pending", base);
520        Assign * whileAccum = pb.createAssign("accum", base);
521        mWhileTest = pb.createZeroes();
522
523        PabloBuilder wb = PabloBuilder::Create(pb);
524        mStarDepth++;
525
526        PabloAST * loopComputation = markerVar(AdvanceMarker(process(repeated, makeMarker(InitialPostPositionByte, whilePending), wb), InitialPostPositionByte, wb));
527        Next * nextWhilePending = wb.createNext(whilePending, wb.createAnd(loopComputation, wb.createNot(whileAccum)));
528        Next * nextWhileAccum = wb.createNext(whileAccum, wb.createOr(loopComputation, whileAccum));
529        Next * nextWhileTest = wb.createNext(whileTest, wb.createOr(mWhileTest, nextWhilePending));
530        pb.createWhile(nextWhileTest, wb);
531        mStarDepth--;
532
533        return makeMarker(InitialPostPositionByte, pb.createAssign("unbounded", nextWhileAccum));
534    }   
535} // end of namespace re
536}
Note: See TracBrowser for help on using the repository browser.