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

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

Added optional CMake command -DDISABLE_PREGENERATED_UCD_FUNCTIONS.

File size: 24.7 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#include <UCD/resolve_properties.h>
25#ifndef DISABLE_PREGENERATED_UCD_FUNCTIONS
26#include <UCD/precompiled_properties.h>
27#endif
28#include <assert.h>
29#include <stdexcept>
30
31#include "llvm/Support/CommandLine.h"
32static cl::OptionCategory fREcompilationOptions("Regex Compilation Options",
33                                      "These options control the compilation of regular expressions to Pablo.");
34
35static cl::opt<bool> DisableLog2BoundedRepetition("disable-log2-bounded-repetition", cl::init(false),
36                     cl::desc("disable log2 optimizations for bounded repetition of bytes"), cl::cat(fREcompilationOptions));
37static 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));
38static cl::opt<bool> DisableMatchStar("disable-matchstar", cl::init(false),
39                     cl::desc("disable MatchStar optimization"), cl::cat(fREcompilationOptions));
40static cl::opt<bool> DisableUnicodeMatchStar("disable-unicode-matchstar", cl::init(false),
41                     cl::desc("disable Unicode MatchStar optimization"), cl::cat(fREcompilationOptions));
42static cl::opt<bool> DisableUnicodeLineBreak("disable-unicode-linebreak", cl::init(false),
43                     cl::desc("disable Unicode line breaks - use LF only"), cl::cat(fREcompilationOptions));
44#ifndef DISABLE_PREGENERATED_UCD_FUNCTIONS
45static cl::opt<bool> DisablePregeneratedUnicode("disable-pregenerated-unicode", cl::init(false),
46                     cl::desc("disable use of pregenerated Unicode character class sets"), cl::cat(fREcompilationOptions));
47#endif
48using namespace pablo;
49
50namespace re {
51
52RE_Compiler::RE_Compiler(pablo::PabloFunction & function, 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, mFunction(function)
65{
66
67}
68   
69MarkerType RE_Compiler::AdvanceMarker(MarkerType m, MarkerPosition newpos, PabloBuilder & pb) {
70    if (m.pos == newpos) return m;
71    PabloAST * a = m.stream;
72    if (m.pos == FinalMatchByte) {
73        // Must advance at least to InitialPostPositionByte
74        a = pb.createAdvance(a, 1, "adv");
75    }
76    // Now at InitialPostPositionByte; is a further advance needed?
77    if (newpos == FinalPostPositionByte) {
78        // Must advance through nonfinal bytes
79        a = pb.createScanThru(pb.createAnd(mInitial, a), mNonFinal, "scanToFinal");
80    }
81    return {newpos, a};
82}
83
84void RE_Compiler::AlignMarkers(MarkerType & m1, MarkerType & m2, PabloBuilder & pb) {
85    if (m1.pos < m2.pos) {
86        m1 = AdvanceMarker(m1, m2.pos, pb); 
87    }
88    else if (m2.pos < m1.pos) {
89        m2 = AdvanceMarker(m2, m1.pos, pb); 
90    }
91}
92
93#define UNICODE_LINE_BREAK (!DisableUnicodeLineBreak)
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    mFunction.setResult(0, mPB.createAssign("matches", mPB.createAnd(mPB.createMatchStar(v, mPB.createNot(lb)), lb)));
181    mFunction.setResult(1, mPB.createAssign("lf", mPB.createAnd(lb, mPB.createNot(mCRLF))));
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) {
253        nextPos = marker;
254    }
255    else if (name->getType() == Name::Type::Byte) {
256        nextPos = AdvanceMarker(marker, InitialPostPositionByte, pb);
257    }
258    else {
259        nextPos = AdvanceMarker(marker, FinalPostPositionByte, pb);
260    }
261    return makeMarker(FinalMatchByte, pb.createAnd(markerVar(nextPos), getNamedCharacterClassStream(name, pb), "m"));
262}
263
264PabloAST * RE_Compiler::getNamedCharacterClassStream(Name * name, PabloBuilder & pb) {
265    PabloAST * var = name->getCompiled();
266    if (LLVM_LIKELY(var != nullptr)) {
267        return var;
268    }
269    else if (name->getDefinition() != nullptr) {
270        MarkerType m = compile(name->getDefinition(), pb);
271        assert(markerPos(m) == FinalMatchByte);
272        var = markerVar(m);
273    }
274    else if (name->getType() == Name::Type::UnicodeProperty) {
275        #ifndef DISABLE_PREGENERATED_UCD_FUNCTIONS
276        if (DisablePregeneratedUnicode) {
277        #endif
278            UCD::UCDCompiler ucdCompiler(mCCCompiler);
279            var = ucdCompiler.generateWithDefaultIfHierarchy(UCD::resolveUnicodeSet(name), pb);
280        #ifndef DISABLE_PREGENERATED_UCD_FUNCTIONS
281        } else {
282            const UCD::ExternalProperty & ep = UCD::resolveExternalProperty(name->getFunctionName());
283            var = pb.createCall(Prototype::Create(name->getFunctionName(), std::get<1>(ep), std::get<2>(ep), std::get<3>(ep), std::get<0>(ep)), mCCCompiler.getBasisBits());
284        }
285        #endif
286    }
287    else {
288        throw std::runtime_error("Unresolved name " + name->getName());
289    }
290    var = pb.createAnd(var, pb.createNot(UNICODE_LINE_BREAK ? mUnicodeLineBreak : mLineFeed));
291    name->setCompiled(var);
292    return var;
293}
294
295MarkerType RE_Compiler::process(Seq * seq, MarkerType marker, PabloBuilder & pb) {
296    // if-hierarchies are not inserted within unbounded repetitions
297    if (mStarDepth > 0) {
298        for (RE * re : *seq) {
299            marker = process(re, marker, pb);
300        }
301        return marker;
302    }
303    else {
304        return processSeqTail(seq->begin(), seq->end(), 0, marker, pb);
305    }
306}
307
308MarkerType RE_Compiler::processSeqTail(Seq::iterator current, Seq::iterator end, int matchLenSoFar, MarkerType marker, PabloBuilder & pb) {
309    if (current == end) return marker;
310    if (matchLenSoFar < IfInsertionGap) {
311        RE * r = *current;
312        marker = process(r, marker, pb);
313        current++;
314        return processSeqTail(current, end, matchLenSoFar + minMatchLength(r), marker, pb);
315    }
316    else {
317        PabloBuilder nested = PabloBuilder::Create(pb);
318        MarkerType m1 = processSeqTail(current, end, 0, marker, nested);
319        Assign * m1a = nested.createAssign("m", markerVar(m1));
320        pb.createIf(markerVar(marker), {m1a}, nested);
321        return makeMarker(m1.pos, m1a);
322    }
323}
324
325MarkerType RE_Compiler::process(Alt * alt, MarkerType marker, PabloBuilder & pb) {
326    std::vector<PabloAST *>  accum = {pb.createZeroes(), pb.createZeroes(), pb.createZeroes()};
327    MarkerType const base = marker;
328    // The following may be useful to force a common Advance rather than separate
329    // Advances in each alternative.
330    // MarkerType const base = makeMarker(InitialPostPositionByte, postPositionVar(marker, pb), pb);
331    for (RE * re : *alt) {
332        MarkerType rslt = process(re, base, pb);
333        MarkerPosition p = markerPos(rslt);
334        accum[p] = pb.createOr(accum[p], markerVar(rslt), "alt");
335    }
336    if (isa<Zeroes>(accum[InitialPostPositionByte]) && isa<Zeroes>(accum[FinalPostPositionByte])) {
337        return makeMarker(FinalMatchByte, accum[FinalMatchByte]);
338    }
339    PabloAST * combine = pb.createOr(accum[InitialPostPositionByte], pb.createAdvance(accum[FinalMatchByte], 1), "alt");
340    if (isa<Zeroes>(accum[FinalPostPositionByte])) {
341        return makeMarker(InitialPostPositionByte, combine);
342    }
343    combine = pb.createOr(pb.createScanThru(pb.createAnd(mInitial, combine), mNonFinal), accum[FinalPostPositionByte], "alt");
344    return makeMarker(FinalPostPositionByte, combine);
345}
346
347MarkerType RE_Compiler::process(Assertion * a, MarkerType marker, PabloBuilder & pb) {
348    RE * asserted = a->getAsserted();
349    if (a->getKind() == Assertion::Kind::Lookbehind) {
350        MarkerType m = marker;
351        MarkerType lookback = compile(asserted, pb);
352        AlignMarkers(m, lookback, pb);
353        PabloAST * lb = markerVar(lookback);
354        if (a->getSense() == Assertion::Sense::Negative) {
355            lb = pb.createNot(lb);
356        }
357        return makeMarker(markerPos(m), pb.createAnd(markerVar(marker), lb, "lookback"));
358    }
359    else if (isUnicodeUnitLength(asserted)) {
360        MarkerType lookahead = compile(asserted, pb);
361        assert(markerPos(lookahead) == FinalMatchByte);
362        PabloAST * la = markerVar(lookahead);
363        if (a->getSense() == Assertion::Sense::Negative) {
364            la = pb.createNot(la);
365        }
366        MarkerType fbyte = AdvanceMarker(marker, FinalPostPositionByte, pb);
367        return makeMarker(FinalPostPositionByte, pb.createAnd(markerVar(fbyte), la, "lookahead"));
368    }
369    else {
370        throw std::runtime_error("Unsupported lookahead assertion.");
371    }
372}
373
374MarkerType RE_Compiler::process(Diff * diff, MarkerType marker, PabloBuilder & pb) {
375    RE * lh = diff->getLH();
376    RE * rh = diff->getRH();
377    if (isUnicodeUnitLength(lh) && isUnicodeUnitLength(rh)) {
378        MarkerType t1 = process(lh, marker, pb);
379        MarkerType t2 = process(rh, marker, pb);
380        AlignMarkers(t1, t2, pb);
381        return makeMarker(markerPos(t1), pb.createAnd(markerVar(t1), pb.createNot(markerVar(t2)), "diff"));
382    }
383    throw std::runtime_error("Unsupported Diff operands: " + Printer_RE::PrintRE(diff));
384}
385
386MarkerType RE_Compiler::process(Intersect * x, MarkerType marker, PabloBuilder & pb) {
387    RE * lh = x->getLH();
388    RE * rh = x->getRH();
389    if (isUnicodeUnitLength(lh) && isUnicodeUnitLength(rh)) {
390        MarkerType t1 = process(lh, marker, pb);
391        MarkerType t2 = process(rh, marker, pb);
392        AlignMarkers(t1, t2, pb);
393        return makeMarker(markerPos(t1), pb.createAnd(markerVar(t1), markerVar(t2), "intersect"));
394    }
395    throw std::runtime_error("Unsupported Intersect operands: " + Printer_RE::PrintRE(x));
396}
397
398MarkerType RE_Compiler::process(Rep * rep, MarkerType marker, PabloBuilder & pb) {
399    int lb = rep->getLB();
400    int ub = rep->getUB();
401    if (lb > 0) {
402        marker = processLowerBound(rep->getRE(), lb, marker, pb);
403    }
404    if (ub == Rep::UNBOUNDED_REP) {
405        return processUnboundedRep(rep->getRE(), marker, pb);
406    }
407    else if (ub == lb) { // if (rep->getUB() != Rep::UNBOUNDED_REP)
408        return marker;
409    }
410    else { // if (rep->getUB() != Rep::UNBOUNDED_REP)
411        return processBoundedRep(rep->getRE(), ub - lb, marker, pb);
412    }
413}
414
415/*
416   Given a stream |repeated| marking positions associated with matches to an item
417   of length |repeated_lgth|, compute a stream marking |repeat_count| consecutive
418   occurrences of such items.
419*/
420
421inline PabloAST * RE_Compiler::consecutive1(PabloAST * repeated, int repeated_lgth, int repeat_count, PabloBuilder & pb) {
422        int i = repeated_lgth;
423        int total_lgth = repeat_count * repeated_lgth;
424        PabloAST * consecutive_i = repeated;
425        while (i * 2 <= total_lgth) {
426            PabloAST * v = consecutive_i;
427            PabloAST * v2 =  pb.createAdvance(v, i);
428            i *= 2;
429            consecutive_i = pb.createAnd(v, v2, "at" + std::to_string(i) + "inarow");
430        }       
431        if (i < total_lgth) {
432            PabloAST * v = consecutive_i;
433            consecutive_i = pb.createAnd(v, pb.createAdvance(v, total_lgth - i), "at" + std::to_string(total_lgth) + "inarow");
434        }
435        return consecutive_i;
436}
437
438inline PabloAST * RE_Compiler::reachable(PabloAST *repeated, int repeated_lgth, int repeat_count, PabloBuilder & pb) {
439        int i = repeated_lgth;
440        int total_lgth = repeat_count * repeated_lgth;
441        if (repeat_count == 0) {
442            return repeated;
443        }
444        PabloAST * reachable_i = pb.createOr(repeated, pb.createAdvance(repeated, 1), "within1");
445        while (i * 2 < total_lgth) {
446            PabloAST * v = reachable_i;
447            PabloAST * v2 =  pb.createAdvance(v, i);
448            i *= 2;
449            reachable_i = pb.createOr(v, v2, "within" + std::to_string(i));
450        }       
451        if (i < total_lgth) {
452            PabloAST * v = reachable_i;
453            reachable_i = pb.createOr(v, pb.createAdvance(v, total_lgth - i), "within" + std::to_string(total_lgth));
454        }
455        return reachable_i;
456}
457
458MarkerType RE_Compiler::processLowerBound(RE * repeated, int lb, MarkerType marker, PabloBuilder & pb) {
459    if (isByteLength(repeated) && !DisableLog2BoundedRepetition) {
460        PabloAST * cc = markerVar(compile(repeated, pb));
461        PabloAST * cc_lb = consecutive1(cc, 1, lb, pb);
462        PabloAST * marker_fwd = pb.createAdvance(markerVar(marker), markerPos(marker) == FinalMatchByte ? lb : lb-1);
463        return makeMarker(FinalMatchByte, pb.createAnd(marker_fwd, cc_lb, "lowerbound"));
464    }
465    // Fall through to general case.
466    while (lb-- != 0) {
467        marker = process(repeated, marker, pb);
468    }
469    return marker;
470}
471
472MarkerType RE_Compiler::processBoundedRep(RE * repeated, int ub, MarkerType marker, PabloBuilder & pb) {
473    if (isByteLength(repeated) && ub > 1 && !DisableLog2BoundedRepetition) {
474        // log2 upper bound for fixed length (=1) class
475        // Create a mask of positions reachable within ub from current marker.
476        // Use matchstar, then apply filter.
477        PabloAST * match = markerVar(AdvanceMarker(marker, InitialPostPositionByte, pb));
478        PabloAST * upperLimitMask = reachable(match, 1, ub, pb);
479        PabloAST * cursor = markerVar(AdvanceMarker(marker, InitialPostPositionByte, pb));
480        PabloAST * rep_class_var = markerVar(compile(repeated, pb));
481        return makeMarker(InitialPostPositionByte, pb.createAnd(pb.createMatchStar(cursor, rep_class_var), upperLimitMask, "bounded"));
482    }
483    // Fall through to general case.
484    while (ub-- != 0) {
485        MarkerType a = process(repeated, marker, pb);
486        MarkerType m = marker;
487        AlignMarkers(a, m, pb);
488        marker = makeMarker(markerPos(a), pb.createOr(markerVar(a), markerVar(m), "m"));
489    }
490    return marker;
491}
492
493MarkerType RE_Compiler::processUnboundedRep(RE * repeated, MarkerType marker, PabloBuilder & pb) {
494    // always use PostPosition markers for unbounded repetition.
495    PabloAST * base = markerVar(AdvanceMarker(marker, InitialPostPositionByte, pb));
496   
497    if (isByteLength(repeated)  && !DisableMatchStar) {
498        PabloAST * cc = markerVar(compile(repeated, pb)); 
499        return makeMarker(InitialPostPositionByte, pb.createMatchStar(base, cc, "unbounded"));
500    }
501    else if (isUnicodeUnitLength(repeated) && !DisableMatchStar && !DisableUnicodeMatchStar) {
502        PabloAST * cc = markerVar(compile(repeated, pb));
503        return makeMarker(FinalPostPositionByte, pb.createAnd(pb.createMatchStar(base, pb.createOr(mNonFinal, cc)), mFinal, "unbounded"));
504    }
505    else if (mStarDepth > 0){
506       
507        PabloBuilder * outerb = pb.getParent();
508       
509        Assign * starPending = outerb->createAssign("pending", outerb->createZeroes());
510        Assign * starAccum = outerb->createAssign("accum", outerb->createZeroes());
511       
512        mStarDepth++;
513        PabloAST * m1 = pb.createOr(base, starPending);
514        PabloAST * m2 = pb.createOr(base, starAccum);
515        PabloAST * loopComputation = markerVar(AdvanceMarker(process(repeated, makeMarker(InitialPostPositionByte, m1), pb), InitialPostPositionByte, pb));
516        Next * nextPending = pb.createNext(starPending, pb.createAnd(loopComputation, pb.createNot(m2)));
517        Next * nextStarAccum = pb.createNext(starAccum, pb.createOr(loopComputation, m2));
518        mWhileTest = pb.createOr(mWhileTest, nextPending);
519        mLoopVariants.push_back(nextPending);
520        mLoopVariants.push_back(nextStarAccum);
521        mStarDepth--;
522       
523        return makeMarker(InitialPostPositionByte, pb.createAssign("unbounded", pb.createOr(base, nextStarAccum)));
524    }   
525    else {
526        Assign * whileTest = pb.createAssign("test", base);
527        Assign * whilePending = pb.createAssign("pending", base);
528        Assign * whileAccum = pb.createAssign("accum", base);
529        mWhileTest = pb.createZeroes();
530
531        PabloBuilder wb = PabloBuilder::Create(pb);
532        mStarDepth++;
533
534        PabloAST * loopComputation = markerVar(AdvanceMarker(process(repeated, makeMarker(InitialPostPositionByte, whilePending), wb), InitialPostPositionByte, wb));
535        Next * nextWhilePending = wb.createNext(whilePending, wb.createAnd(loopComputation, wb.createNot(whileAccum)));
536        Next * nextWhileAccum = wb.createNext(whileAccum, wb.createOr(loopComputation, whileAccum));
537        Next * nextWhileTest = wb.createNext(whileTest, wb.createOr(mWhileTest, nextWhilePending));
538        mLoopVariants.push_back(nextWhilePending);
539        mLoopVariants.push_back(nextWhileAccum);
540        mLoopVariants.push_back(nextWhileTest);
541        pb.createWhile(nextWhileTest, mLoopVariants, wb);
542        mStarDepth--;
543        mLoopVariants.clear();
544        return makeMarker(InitialPostPositionByte, pb.createAssign("unbounded", nextWhileAccum));
545    }   
546} // end of namespace re
547}
Note: See TracBrowser for help on using the repository browser.