Ignore:
Timestamp:
Jan 6, 2018, 5:03:47 PM (14 months ago)
Author:
nmedfort
Message:

Bug fixes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • icGREP/icgrep-devel/icgrep/re/re_compiler.cpp

    r5816 r5821  
    5858using MarkerType = RE_Compiler::MarkerType;
    5959
    60 PabloAST * RE_Compiler::compile(RE * re, PabloAST * const initialCursors) {
     60PabloAST * RE_Compiler::compile(RE * const re, PabloAST * const initialCursors) {
    6161    const auto markers = initialCursors ? compile(re, initialCursors, mPB) : compile(re, mPB);
    6262    return markerVar(AdvanceMarker(markers, FinalPostPositionUnit, mPB));
    6363}
    6464
    65 inline MarkerType RE_Compiler::compile(RE * re, PabloAST * const cursors, PabloBuilder & pb) {
     65inline MarkerType RE_Compiler::compile(RE * const re, PabloAST * const cursors, PabloBuilder & pb) {
    6666    return process(re, makeMarker(FinalMatchUnit, cursors), pb);
    6767}
    6868
    69 inline MarkerType RE_Compiler::compile(RE * re, PabloBuilder & pb) {
     69inline MarkerType RE_Compiler::compile(RE * const re, PabloBuilder & pb) {
    7070    return process(re, makeMarker(FinalPostPositionUnit, pb.createOnes()), pb);
    7171}
    7272   
    73 MarkerType RE_Compiler::process(RE * re, MarkerType marker, PabloBuilder & pb) {
     73MarkerType RE_Compiler::process(RE * const re, MarkerType marker, PabloBuilder & pb) {
    7474    if (isa<Name>(re)) {
    7575        return compileName(cast<Name>(re), marker, pb);
     
    105105}
    106106
    107 MarkerType RE_Compiler::compileCC(CC * cc, MarkerType marker, PabloBuilder & pb) {
     107MarkerType RE_Compiler::compileCC(CC * const cc, MarkerType marker, PabloBuilder & pb) {
    108108    PabloAST * nextPos = markerVar(marker);
    109109    const cc::Alphabet * a = cc->getAlphabet();
     
    132132}
    133133
    134 inline MarkerType RE_Compiler::compileName(Name * name, MarkerType marker, PabloBuilder & pb) {
    135     const std::string nameString = name->getName();
     134inline MarkerType RE_Compiler::compileName(Name * const name, MarkerType marker, PabloBuilder & pb) {
     135    const auto & nameString = name->getName();
    136136    if (nameString == ".") {
    137137        return compileAny(marker, pb);
     
    159159}
    160160
    161 inline MarkerType RE_Compiler::compileName(Name * name, PabloBuilder & pb) {
     161inline MarkerType RE_Compiler::compileName(Name * const name, PabloBuilder & pb) {
    162162    MarkerType m;
    163163    if (LLVM_LIKELY(mCompiledName->get(name, m))) {
     
    171171}
    172172
    173 MarkerType RE_Compiler::compileSeq(Seq * seq, MarkerType marker, PabloBuilder & pb) {
     173MarkerType RE_Compiler::compileSeq(Seq * const seq, MarkerType marker, PabloBuilder & pb) {
    174174    // if-hierarchies are not inserted within unbounded repetitions
    175175    if (mStarDepth > 0) {
     
    179179        return marker;
    180180    } else {
    181         return compileSeqTail(seq->begin(), seq->end(), 0, marker, pb);
    182     }
    183 }
    184 
    185 MarkerType RE_Compiler::compileSeqTail(Seq::iterator current, Seq::iterator end, int matchLenSoFar, MarkerType marker, PabloBuilder & pb) {
     181        return compileSeqTail(seq->cbegin(), seq->cend(), 0, marker, pb);
     182    }
     183}
     184
     185MarkerType RE_Compiler::compileSeqTail(Seq::const_iterator current, const Seq::const_iterator end, const int matchLenSoFar, MarkerType marker, PabloBuilder & pb) {
    186186    if (current == end) {
    187187        return marker;
     
    189189        RE * r = *current;
    190190        marker = process(r, marker, pb);
    191         current++;
    192         return compileSeqTail(current, end, matchLenSoFar + minMatchLength(r), marker, pb);
     191        return compileSeqTail(++current, end, matchLenSoFar + minMatchLength(r), marker, pb);
    193192    } else {
    194193        Var * m = pb.createVar("m", pb.createZeroes());
     
    204203}
    205204
    206 MarkerType RE_Compiler::compileAlt(Alt * alt, const MarkerType base, PabloBuilder & pb) {
     205MarkerType RE_Compiler::compileAlt(Alt * const alt, const MarkerType base, PabloBuilder & pb) {
    207206    std::vector<PabloAST *>  accum(3, pb.createZeroes());
    208207    // The following may be useful to force a common Advance rather than separate
     
    225224}
    226225
    227 MarkerType RE_Compiler::compileAssertion(Assertion * a, MarkerType marker, PabloBuilder & pb) {
     226MarkerType RE_Compiler::compileAssertion(Assertion * const a, MarkerType marker, PabloBuilder & pb) {
    228227    RE * asserted = a->getAsserted();
    229228    if (a->getKind() == Assertion::Kind::Lookbehind) {
     
    261260}
    262261
    263 inline bool alignedUnicodeLength(const RE * lh, const RE * rh) {
     262inline bool alignedUnicodeLength(const RE * const lh, const RE * const rh) {
    264263    const auto lhl = getUnicodeUnitLengthRange(lh);
    265264    const auto rhl = getUnicodeUnitLengthRange(rh);
     
    268267
    269268MarkerType RE_Compiler::compileDiff(Diff * diff, MarkerType marker, PabloBuilder & pb) {
    270     RE * lh = diff->getLH();
    271     RE * rh = diff->getRH();
     269    RE * const lh = diff->getLH();
     270    RE * const rh = diff->getRH();
    272271    if (alignedUnicodeLength(lh, rh)) {
    273272        MarkerType t1 = process(lh, marker, pb);
     
    279278}
    280279
    281 MarkerType RE_Compiler::compileIntersect(Intersect * x, MarkerType marker, PabloBuilder & pb) {
    282     RE * lh = x->getLH();
    283     RE * rh = x->getRH();
     280MarkerType RE_Compiler::compileIntersect(Intersect * const x, MarkerType marker, PabloBuilder & pb) {
     281    RE * const lh = x->getLH();
     282    RE * const rh = x->getRH();
    284283    if (alignedUnicodeLength(lh, rh)) {
    285284        MarkerType t1 = process(lh, marker, pb);
     
    291290}
    292291
    293 MarkerType RE_Compiler::compileRep(Rep * rep, MarkerType marker, PabloBuilder & pb) {
    294     int lb = rep->getLB();
    295     int ub = rep->getUB();
     292MarkerType RE_Compiler::compileRep(Rep * const rep, MarkerType marker, PabloBuilder & pb) {
     293    const auto lb = rep->getLB();
     294    const auto ub = rep->getUB();
    296295    if (lb > 0) {
    297296        marker = processLowerBound(rep->getRE(), lb, marker, IfInsertionGap, pb);
     
    310309*/
    311310   
    312 PabloAST * RE_Compiler::consecutive_matches(PabloAST * repeated_j, int j, int repeat_count, PabloAST * indexStream, PabloBuilder & pb) {
    313     if (j == repeat_count) return repeated_j;
    314     int i = std::min(j, repeat_count - j);
    315     int k = j + i;
     311PabloAST * RE_Compiler::consecutive_matches(PabloAST * const repeated_j, const int j, const int repeat_count, PabloAST * const indexStream, PabloBuilder & pb) {
     312    if (j == repeat_count) {
     313        return repeated_j;
     314    }
     315    const int i = std::min(j, repeat_count - j);
     316    const int k = j + i;
    316317    if (/*j > IfInsertionGap*/ false) {
    317318        Var * repeated = pb.createVar("repeated", pb.createZeroes());
     
    319320        NameMap nestedMap(mCompiledName);
    320321        mCompiledName = &nestedMap;
    321        
    322         PabloAST * adv_i = nullptr;
    323         if (indexStream == nullptr) adv_i = nested.createAdvance(repeated_j, i);
    324         else adv_i = nested.createIndexedAdvance(repeated_j, indexStream, i);
     322        PabloAST * adv_i = nested.createIndexedAdvance(repeated_j, indexStream, i);
    325323        PabloAST * repeated_k = nested.createAnd(repeated_j, adv_i, "at" + std::to_string(k) + "of" + std::to_string(repeat_count));
    326324        nested.createAssign(repeated, consecutive_matches(repeated_k, k, repeat_count, indexStream, nested));
     
    328326        mCompiledName = nestedMap.getParent();
    329327        return repeated;
    330     }
    331     else {
    332         PabloAST * adv_i = nullptr;
    333         if (indexStream == nullptr) adv_i = pb.createAdvance(repeated_j, i);
    334         else adv_i = pb.createIndexedAdvance(repeated_j, indexStream, i);
     328    } else {
     329        PabloAST * adv_i = pb.createIndexedAdvance(repeated_j, indexStream, i);
    335330        PabloAST * repeated_k = pb.createAnd(repeated_j, adv_i, "at" + std::to_string(k) + "of" + std::to_string(repeat_count));
    336331        return consecutive_matches(repeated_k, k, repeat_count, indexStream, pb);
     
    339334
    340335
    341 inline PabloAST * RE_Compiler::reachable(PabloAST * repeated, int length, int repeat_count, PabloAST * indexStream, PabloBuilder & pb) {
    342     int i = length;
    343     int total_lgth = repeat_count * length;
     336inline PabloAST * RE_Compiler::reachable(PabloAST *  const repeated, const int length, const int repeat_count, PabloAST * const indexStream, PabloBuilder & pb) {
    344337    if (repeat_count == 0) {
    345338        return repeated;
    346     }
    347     PabloAST * reachable = nullptr;
    348     if (indexStream == nullptr) reachable = pb.createOr(repeated, pb.createAdvance(repeated, 1), "within1");
    349     else reachable = pb.createOr(repeated, pb.createIndexedAdvance(repeated, indexStream, 1), "within1");
    350     while ((i * 2) < total_lgth) {
    351         PabloAST * v = reachable;
    352         PabloAST * v2 = nullptr;
    353         if (indexStream == nullptr) v2 = pb.createAdvance(v, i);
    354         else v2 = pb.createIndexedAdvance(v, indexStream, i);
     339    }   
     340    const int total_length = repeat_count * length;
     341    PabloAST * const v2 = pb.createIndexedAdvance(repeated, indexStream, 1);
     342    PabloAST * reachable = pb.createOr(repeated, v2, "within1");
     343    int i = length;
     344    while ((i * 2) < total_length) {
     345        PabloAST * const extension = pb.createIndexedAdvance(reachable, indexStream, i);
    355346        i *= 2;
    356         reachable = pb.createOr(v, v2, "within" + std::to_string(i));
    357     }
    358     if (LLVM_LIKELY(i < total_lgth)) {
    359         PabloAST * v = reachable;
    360         PabloAST * v2 = nullptr;
    361         if (indexStream == nullptr) v2 = pb.createAdvance(v, total_lgth - i);
    362         else v2 = pb.createIndexedAdvance(v, indexStream, total_lgth - i);
    363         reachable = pb.createOr(v, v2, "within" + std::to_string(total_lgth));
     347        reachable = pb.createOr(reachable, extension, "within" + std::to_string(i));
     348    }
     349    if (LLVM_LIKELY(i < total_length)) {
     350        PabloAST * const extension = pb.createIndexedAdvance(reachable, indexStream, total_length - i);
     351        reachable = pb.createOr(reachable, extension, "within" + std::to_string(total_length));
    364352    }
    365353    return reachable;
    366354}
    367355
    368 MarkerType RE_Compiler::processLowerBound(RE * repeated, int lb, MarkerType marker, int ifGroupSize, PabloBuilder & pb) {
     356MarkerType RE_Compiler::processLowerBound(RE * const repeated, const int lb, MarkerType marker, const int ifGroupSize, PabloBuilder & pb) {
    369357    if (LLVM_UNLIKELY(lb == 0)) {
    370358        return marker;
     
    374362    //
    375363    // A bounded repetition with an upper bound of at least 2.
    376     if (!AlgorithmOptionIsSet(DisableLog2BoundedRepetition)) {
     364    if (LLVM_LIKELY(!AlgorithmOptionIsSet(DisableLog2BoundedRepetition))) {
    377365        // Check for a regular expression that satisfies on of the special conditions that
    378366        // allow implementation using the log2 technique.
     
    411399    }
    412400    // Fall through to general case.  Process the first item and insert the rest into an if-structure.
    413     auto group = ifGroupSize < lb ? ifGroupSize : lb;
     401    const auto group = ifGroupSize < lb ? ifGroupSize : lb;
    414402    for (auto i = 0; i < group; i++) {
    415403        marker = process(repeated, marker, pb);
     
    429417}
    430418   
    431 MarkerType RE_Compiler::processBoundedRep(RE * repeated, int ub, MarkerType marker, int ifGroupSize,  PabloBuilder & pb) {
     419MarkerType RE_Compiler::processBoundedRep(RE * const repeated, const int ub, MarkerType marker, const int ifGroupSize,  PabloBuilder & pb) {
    432420    if (LLVM_UNLIKELY(ub == 0)) {
    433421        return marker;
     
    435423    //
    436424    // A bounded repetition with an upper bound of at least 2.
    437     if (!AlgorithmOptionIsSet(DisableLog2BoundedRepetition) && (ub > 1)) {
     425    if ((ub > 1) && LLVM_LIKELY(!AlgorithmOptionIsSet(DisableLog2BoundedRepetition))) {
    438426        // Check for a regular expression that satisfies on of the special conditions that
    439427        // allow implementation using the log2 technique.
     
    474462    }
    475463    // Fall through to general case.  Process the first item and insert the rest into an if-structure.
    476     auto group = ifGroupSize < ub ? ifGroupSize : ub;
     464    const auto group = ifGroupSize < ub ? ifGroupSize : ub;
    477465    for (auto i = 0; i < group; i++) {
    478466        MarkerType a = process(repeated, marker, pb);
     
    484472        return marker;
    485473    }
    486     Var * m1a = pb.createVar("m", pb.createZeroes());
     474    Var * const m1a = pb.createVar("m", pb.createZeroes());
    487475    PabloBuilder nested = PabloBuilder::Create(pb);
    488476    NameMap nestedMap(mCompiledName);
     
    492480    pb.createIf(markerVar(marker), nested);
    493481    mCompiledName = nestedMap.getParent();
    494     return makeMarker(m1.pos, m1a);
    495 }
    496 
    497 MarkerType RE_Compiler::processUnboundedRep(RE * repeated, MarkerType marker, PabloBuilder & pb) {
     482    return makeMarker(markerPos(m1), m1a);
     483}
     484
     485MarkerType RE_Compiler::processUnboundedRep(RE * const repeated, MarkerType marker, PabloBuilder & pb) {
    498486    // always use PostPosition markers for unbounded repetition.
    499487    PabloAST * base = markerVar(AdvanceMarker(marker, InitialPostPositionUnit, pb));
    500     if (isByteLength(repeated)  && !AlgorithmOptionIsSet(DisableMatchStar)) {
    501         PabloAST * mask = markerVar(compile(repeated, pb));
    502         PabloAST * nonFinal = mNonFinal;
     488    if (isByteLength(repeated) && LLVM_LIKELY(!AlgorithmOptionIsSet(DisableMatchStar))) {
     489        PabloAST * mask = pb.createOr(markerVar(compile(repeated, pb)), mNonFinal);
    503490        // The post position character may land on the initial byte of a multi-byte character. Combine them with the masked range.
    504         PabloAST * unbounded = pb.createMatchStar(base, pb.createOr(mask, nonFinal), "unbounded");
     491        PabloAST * unbounded = pb.createMatchStar(base, mask, "unbounded");
    505492        return makeMarker(FinalPostPositionUnit, unbounded);
    506     } else if (isUnicodeUnitLength(repeated) && !AlgorithmOptionIsSet(DisableMatchStar) && !AlgorithmOptionIsSet(DisableUnicodeMatchStar)) {
    507         PabloAST * cc = markerVar(compile(repeated, pb));
    508         PabloAST * mstar = nullptr;
    509         PabloAST * nonFinal = mNonFinal;
    510         cc = pb.createOr(cc, nonFinal);
    511         mstar = pb.createMatchStar(base, cc);
    512         PabloAST * final = mFinal;
    513         return makeMarker(FinalPostPositionUnit, pb.createAnd(mstar, final, "unbounded"));
     493    } else if (isUnicodeUnitLength(repeated) && LLVM_LIKELY(!AlgorithmOptionIsSet(DisableMatchStar) && !AlgorithmOptionIsSet(DisableUnicodeMatchStar))) {
     494        PabloAST * mask = pb.createOr(markerVar(compile(repeated, pb)), mNonFinal);
     495        PabloAST * mstar = pb.createMatchStar(base, mask);
     496        return makeMarker(FinalPostPositionUnit, pb.createAnd(mstar, mFinal, "unbounded"));
    514497    } else if (mStarDepth > 0){
    515498        PabloBuilder * const outer = pb.getParent();
     
    550533
    551534inline MarkerType RE_Compiler::compileStart(MarkerType marker, pablo::PabloBuilder & pb) {
    552     PabloAST * sol = pb.createNot(pb.createAdvance(pb.createNot(mLineBreak), 1));
     535    PabloAST * const sol = pb.createNot(pb.createAdvance(pb.createNot(mLineBreak), 1));
    553536    MarkerType m = AdvanceMarker(marker, InitialPostPositionUnit, pb);
    554537    return makeMarker(InitialPostPositionUnit, pb.createAnd(markerVar(m), sol, "sol"));
Note: See TracChangeset for help on using the changeset viewer.