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

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

Moved resolveProperty responsibilities out of RE_Parser but kept expansion of Name objects with definitions in it.

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