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

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

Preliminary changes to inclusion of UCD Compiler into the RE Compiler.

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