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

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

Embed carry data in compiled LLVM module; eliminate passing of carry data pointers/size

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