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

Last change on this file since 4803 was 4803, checked in by cameron, 4 years ago

Work on character name patterns

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