Ignore:
Timestamp:
Oct 4, 2014, 5:14:27 PM (5 years ago)
Author:
nmedfort
Message:

Removed 'newsym' from CodeGenState? and replaced with an Assign target system in RE_Compiler

Location:
icGREP/icgrep-devel/icgrep
Files:
11 edited

Legend:

Unmodified
Added
Removed
  • icGREP/icgrep-devel/icgrep/cc/cc_compiler.cpp

    r4210 r4211  
    1010//Pablo Expressions
    1111#include <pablo/codegenstate.h>
    12 #include <pablo/pe_advance.h>
    13 #include <pablo/pe_all.h>
    14 #include <pablo/pe_and.h>
    15 #include <pablo/pe_call.h>
    16 #include <pablo/pe_charclass.h>
    17 #include <pablo/pe_matchstar.h>
    18 #include <pablo/pe_not.h>
    19 #include <pablo/pe_or.h>
    20 #include <pablo/pe_pabloe.h>
    21 #include <pablo/pe_scanthru.h>
    22 #include <pablo/pe_sel.h>
    23 #include <pablo/pe_var.h>
    24 #include <pablo/pe_xor.h>
    25 #include <pablo/ps_assign.h>
    26 #include <pablo/ps_if.h>
    27 #include <pablo/ps_while.h>
    2812#include <re/re_alt.h>
    2913#include <re/re_cc.h>
     
    5135, mEncoding(encoding)
    5236, mGenSymPattern(gensym_pattern)
    53 , mGenSymCounter(0)
    5437, mBasisPattern(basis_pattern)
    5538{
    5639    for (int i = 0; i < mEncoding.getBits(); i++)
    5740    {
    58         std::string b_pattern = bit_var((mEncoding.getBits() - 1) - i);
     41        Var * basisVar = getBasisVar(i);
    5942        Expression* expr = new Expression();
    60         expr->expr_string  =  b_pattern;
    61         expr->pablo_expr = make_bitv(i);
    62         add_predefined(b_pattern, expr);
     43        expr->expr_string  =  basisVar->getName();
     44        expr->pablo_expr = basisVar;
     45        mCommon_Expression_Map.insert(make_pair(basisVar->getName(), expr));
    6346    }
    6447}
    6548
    6649void CC_Compiler::compile(const REMap & re_map) {
    67     process_re_map(re_map);
    68     for (auto i =  re_map.rbegin(); i != re_map.rend(); ++i) {
     50    for (auto i =  re_map.cbegin(); i != re_map.cend(); ++i) {
     51        process_re(i->second);
     52    }
     53    for (auto i =  re_map.cbegin(); i != re_map.cend(); ++i) {
    6954        //This is specifically for the utf8 multibyte character classes.
    7055        if (Seq * seq = dyn_cast<Seq>(i->second)) {
    7156            if (seq->getType() == Seq::Type::Byte) {
     57                Assign * assignment = nullptr;
    7258                auto j = seq->begin();
    7359                while (true) {
    7460                    Name * name = dyn_cast<Name>(*j);
    7561                    assert (name);
    76                     CharClass * cc_mask = makeCharClass(name->getName());
     62                    CharClass * cc = makeCharClass(name->getName());
     63                    PabloE * sym = assignment ? makeAnd(makeVar(assignment->getName()), cc) : cc;
    7764                    if (++j != seq->end()) {
    78                         mCG.push_back(makeAssign(mCG.symgen("marker"), makeAdvance(cc_mask)));
     65                        assignment = makeAssign(mCG.symgen("marker"), makeAdvance(sym));
     66                        mCG.push_back(assignment);
     67                        continue;
    7968                    }
    80                     else {
    81                         mCG.push_back(makeAssign(seq->getName(), cc_mask));
    82                         break;
    83                     }
     69                    mCG.push_back(makeAssign(seq->getName(), sym));
     70                    break;
    8471                }
    8572            }
     
    8875}
    8976
    90 inline void CC_Compiler::add_predefined(std::string key_value, Expression* mapped_value) {
    91     mCommon_Expression_Map.insert(make_pair(key_value, mapped_value));
    92 }
    93 
    94 Expression* CC_Compiler::add_assignment(std::string varname, Expression* expr)
     77Expression * CC_Compiler::add_assignment(std::string varname, Expression* expr)
    9578{   
    9679    //Add the new mapping to the list of pablo statements:
     
    10285    mapped_value->expr_string = varname;
    10386    mapped_value->pablo_expr = makeVar(varname);
    104 
    105     std::pair<MapIterator, bool> ret = mCommon_Expression_Map.insert(make_pair(key_value, mapped_value));
    106 
    107     return ret.first->second;
    108 }
    109 
    110 Expression* CC_Compiler::expr_to_variable(Expression * expr) {
     87    return mCommon_Expression_Map.insert(std::make_pair(key_value, mapped_value)).first->second;
     88}
     89
     90Expression * CC_Compiler::expr_to_variable(Expression * expr) {
    11191    MapIterator itr = mCommon_Expression_Map.find(expr->expr_string);
    11292    if (itr != mCommon_Expression_Map.end()) {
     
    11494    }
    11595    else {
    116         return add_assignment(mGenSymPattern + std::to_string(++mGenSymCounter), expr);
    117     }
    118 }
    119 
    120 void CC_Compiler::process_re_map(const REMap & re_map) {
    121     for (auto it =  re_map.crbegin(); it != re_map.crend(); ++it) {
    122         process_re(it->second);
     96        return add_assignment(mCG.symgen(mGenSymPattern), expr);
    12397    }
    12498}
    12599
    126100void CC_Compiler::process_re(const RE * re) {
    127     if (const Alt* re_alt = dyn_cast<const Alt>(re)) {
    128         for (const RE * re : *re_alt) {
     101    if (const Alt * alt = dyn_cast<const Alt>(re)) {
     102        for (const RE * re : *alt) {
    129103            process_re(re);
    130104        }
    131105    }
    132     else if (const CC* re_cc = dyn_cast<const CC>(re)) {
    133         cc2pablos(re_cc);
     106    else if (const CC * cc = dyn_cast<const CC>(re)) {
     107        process(cc);
    134108    }
    135109    else if (const Rep* re_rep = dyn_cast<const Rep>(re)) {
     
    143117}
    144118
     119void CC_Compiler::process(const CC * cc)
     120{
     121    add_assignment(cc->getName(), expr2pabloe(charset_expr(cc)));
     122}
     123
     124
    145125PabloE* CC_Compiler::bit_pattern_expr(int pattern, int selected_bits)
    146126{
    147     if (selected_bits == 0) return makeAll(1);
     127    if (selected_bits == 0) {
     128        return mCG.createAll(1);
     129    }
    148130
    149131    std::vector<PabloE*> bit_terms;
     
    157139            if ((pattern & test_bit) == 0)
    158140            {
    159                 bit_terms.push_back(makeNot(make_bitv(bit_no)));
     141                bit_terms.push_back(makeNot(getBasisVar(bit_no)));
    160142            }
    161143            else
    162144            {
    163                 bit_terms.push_back(make_bitv(bit_no));
     145                bit_terms.push_back(getBasisVar(bit_no));
    164146            }
    165147        }
    166148        else
    167149        {
    168             bit_terms.push_back(makeAll(1));
     150            bit_terms.push_back(mCG.createAll(1));
    169151        }
    170152        selected_bits &= ~test_bit;
     
    216198    PabloE* hi_test = LE_Range(diff_count - 1, n2 & mask1);
    217199
    218     return makeAnd(common, makeSel(make_bitv(diff_count - 1), hi_test, lo_test));
     200    return makeAnd(common, makeSel(getBasisVar(diff_count - 1), hi_test, lo_test));
    219201}
    220202
     
    222204    if (N == 0)
    223205    {
    224         return makeAll(1); //Return a true literal.
     206        return mCG.createAll(1); //Return a true literal.
    225207    }
    226208    else if (((N % 2) == 0) && ((n >> (N - 2)) == 0))
    227209    {
    228         return makeOr(makeOr(make_bitv(N - 1), make_bitv(N - 2)), GE_Range(N - 2, n));
     210        return makeOr(makeOr(getBasisVar(N - 1), getBasisVar(N - 2)), GE_Range(N - 2, n));
    229211    }
    230212    else if (((N % 2) == 0) && ((n >> (N - 2)) == 3))
    231213    {
    232         return makeAnd(makeAnd(make_bitv(N - 1), make_bitv(N - 2)), GE_Range(N - 2, n - (3 << (N - 2))));
     214        return makeAnd(makeAnd(getBasisVar(N - 1), getBasisVar(N - 2)), GE_Range(N - 2, n - (3 << (N - 2))));
    233215    }
    234216    else if (N >= 1)
     
    244226              the value of GE_range(N-1), lo_range) is required.
    245227            */
    246             return makeOr(make_bitv(N-1), lo_range);
     228            return makeOr(getBasisVar(N - 1), lo_range);
    247229        }
    248230        else
     
    252234              in the target for >= and GE_range(N-1, lo_bits) must also be true.
    253235            */
    254             return makeAnd(make_bitv(N-1), lo_range);
     236            return makeAnd(getBasisVar(N - 1), lo_range);
    255237        }
    256238    }
     
    265247    */
    266248    if ((n + 1) == (1 << N)) {
    267         return makeAll(1); //True.
     249        return mCG.createAll(1); //True.
    268250    }
    269251    else {
     
    274256PabloE* CC_Compiler::charset_expr(const CC * cc) {
    275257    if (cc->empty()) {
    276         return makeAll(0);
     258        return mCG.createAll(0);
    277259    }
    278260    if (cc->size() > 2) {
     
    301283                hi |= (mask ^ (mask - 1));
    302284                PabloE * expr = make_range(lo, hi);
    303                 PabloE * bit0 = make_bitv(0);
     285                PabloE * bit0 = getBasisVar(0);
    304286                if ((lo & 1) == 0) {
    305287                    bit0 = makeNot(bit0);
     
    326308    throw std::runtime_error(std::string("Invalid Character Set Range: [") + std::to_string(lo) + "," + std::to_string(hi) + "]");
    327309}
    328 
    329310
    330311Expression* CC_Compiler::expr2pabloe(PabloE* expr) {
     
    341322        {
    342323            retExpr->expr_string = "All(1)";
    343             retExpr->pablo_expr = makeAll(1);
    344324        }
    345325        else if (all->getValue() == 0)
    346326        {
    347327            retExpr->expr_string = "All(0)";
    348             retExpr->pablo_expr = makeAll(0);
    349328        }
    350329    }
    351330    else if (Var * var = dyn_cast<Var>(expr))
    352331    {
    353             retExpr->expr_string = var->getVar();
    354             retExpr->pablo_expr = makeVar(var->getVar());
     332            retExpr->expr_string = var->getName();
    355333    }
    356334    else if (Not * pe_not = dyn_cast<Not>(expr))
     
    358336        Expression* ret = expr_to_variable(expr2pabloe(pe_not->getExpr()));
    359337        retExpr->expr_string =  "~" + ret->expr_string;
    360         retExpr->pablo_expr = makeNot(ret->pablo_expr);
    361338    }
    362339    else if(Or * pe_or = dyn_cast<Or>(expr))
     
    365342        Expression* ret2 = expr_to_variable(expr2pabloe(pe_or->getExpr2()));
    366343        retExpr->expr_string = "(" + ret1->expr_string + "|" + ret2->expr_string + ")";
    367         retExpr->pablo_expr = makeOr(ret1->pablo_expr, ret2->pablo_expr);
    368344    }
    369345    else if (Xor * pe_xor = dyn_cast<Xor>(expr))
     
    372348        Expression* ret2 = expr_to_variable(expr2pabloe(pe_xor->getExpr2()));
    373349        retExpr->expr_string = "(" + ret1->expr_string + "^" + ret2->expr_string + ")";
    374         retExpr->pablo_expr = makeXor(ret1->pablo_expr, ret2->pablo_expr);
    375350    }
    376351    else if (And * pe_and = dyn_cast<And>(expr))
     
    381356            Expression* ret2 = expr_to_variable(expr2pabloe(pe_and->getExpr2()));
    382357            retExpr->expr_string = "(" + ret2->expr_string + "&~" + ret1->expr_string + ")";
    383             retExpr->pablo_expr = makeAnd(ret2->pablo_expr, makeNot(ret1->pablo_expr));
    384358        }
    385359        else if (Not * pe_not = dyn_cast<Not>(pe_and->getExpr2()))
     
    388362            Expression* ret2 = expr_to_variable(expr2pabloe(pe_not->getExpr()));
    389363            retExpr->expr_string = "(" + ret1->expr_string  + "&~" + ret2->expr_string + ")";
    390             retExpr->pablo_expr = makeAnd(ret1->pablo_expr, makeNot(ret2->pablo_expr));
    391364        }
    392365        else
     
    395368            Expression* ret2 = expr_to_variable(expr2pabloe(pe_and->getExpr2()));
    396369            retExpr->expr_string = "(" + ret1->expr_string + "&" + ret2->expr_string + ")";
    397             retExpr->pablo_expr = makeAnd(ret1->pablo_expr, ret2->pablo_expr);
    398370        }
    399371    }
     
    404376        Expression* ret_false = expr_to_variable(expr2pabloe(pe_sel->getF_expr()));
    405377        retExpr->expr_string = "((" + ret_sel->expr_string + "&" + ret_true->expr_string + ")|(~("
    406             + ret_sel->expr_string + ")&" + ret_false->expr_string + ")";
    407         retExpr->pablo_expr = makeSel(ret_sel->pablo_expr, ret_true->pablo_expr, ret_false->pablo_expr);
    408     }
    409 
     378            + ret_sel->expr_string + ")&" + ret_false->expr_string + ")";       
     379    }
     380    retExpr->pablo_expr = expr;
    410381    return retExpr;
    411382}
    412383
    413 void CC_Compiler::cc2pablos(const CC * cc)
    414 {
    415     add_assignment(cc->getName(), expr2pabloe(charset_expr(cc)));
    416 }
    417 
    418 std::string CC_Compiler::bit_var(int n)
    419 {
    420     return  mBasisPattern + std::to_string(n);
    421 }
    422 
    423 PabloE* CC_Compiler::make_bitv(int n)
    424 {
    425     return makeVar(bit_var((mEncoding.getBits() - 1) - n));
     384inline Var * CC_Compiler::getBasisVar(const int i) const {
     385    return makeVar(mBasisPattern + std::to_string((mEncoding.getBits() - 1) - i));
    426386}
    427387
  • icGREP/icgrep-devel/icgrep/cc/cc_compiler.h

    r4210 r4211  
    1515#include <pablo/pe_pabloe.h>
    1616#include <re/re_cc.h>
    17 
    1817
    1918namespace cc {
     
    4039
    4140private:
    42     void process_re_map(const REMap &re_map);
    4341    void process_re(const re::RE *re);
    44     std::string bit_var(int n);
    45     pablo::PabloE * make_bitv(int n);
     42    pablo::Var * getBasisVar(const int n) const;
    4643    pablo::PabloE * bit_pattern_expr(int pattern, int selected_bits);
    4744    pablo::PabloE * char_test_expr(const re::CodePointType ch);
     
    5249    pablo::PabloE * charset_expr(const re::CC *cc);
    5350    Expression* expr2pabloe(pablo::PabloE * expr);
    54     void cc2pablos(const re::CC *cc);
     51    void process(const re::CC *cc);
    5552
    56     void add_predefined(std::string key_value, Expression *mapped_value);
    57     Expression* add_assignment(std::string value, Expression* expr);
     53    Expression * add(std::string key_value, Expression *mapped_value);
     54    Expression * add_assignment(std::string value, Expression* expr);
    5855    Expression* expr_to_variable(Expression* cgo);
    5956
    6057    pablo::CodeGenState &       mCG;
    6158    const Encoding              mEncoding;
     59    const std::string           mGenSymPattern;
    6260    const std::string           mBasisPattern;
    63     const std::string           mGenSymPattern;
    64     int                         mGenSymCounter;
    6561    ExpressionMap               mCommon_Expression_Map;
    6662};
  • icGREP/icgrep-devel/icgrep/llvm_gen.cpp

    r4210 r4211  
    423423    //Generate the IR instructions for the function.
    424424    Generate_PabloStatements(cg_state.expressions());
    425     SetReturnMarker(cg_state.newsym, 0);
     425
     426    Assign * final = dyn_cast<Assign>(cg_state.expressions().back());
     427    assert (final);
     428
     429    SetReturnMarker(final->getName(), 0);
    426430    SetReturnMarker(m_name_map.find("LineFeed")->second, 1);
    427431
     
    846850{
    847851    IRBuilder<> b(mBasicBlock);
    848 
    849     if (mMarkerMap.find(name) == mMarkerMap.end())
    850     {
     852    auto itr = mMarkerMap.find(name);
     853    if (itr == mMarkerMap.end()) {
    851854        Value* ptr = b.CreateAlloca(mXi64Vect);
    852         Value* void_1 = b.CreateStore(mConst_Aggregate_Xi64_0, ptr);
    853         mMarkerMap.insert(make_pair(name, ptr));
    854     }
    855     std::map<std::string, Value*>::iterator itGet = mMarkerMap.find(name);
    856 
    857     return itGet->second;
     855        b.CreateStore(mConst_Aggregate_Xi64_0, ptr);
     856        itr = mMarkerMap.insert(make_pair(name, ptr)).first;
     857    }
     858    return itr->second;
    858859}
    859860
     
    885886        IRBuilder<> b(mBasicBlock);
    886887
    887         b.CreateStore(Generate_PabloE(assign->getExpr()), GetMarker(assign->getM()));
    888 
    889         retVal = assign->getM();
     888        b.CreateStore(Generate_PabloE(assign->getExpr()), GetMarker(assign->getName()));
     889
     890        retVal = assign->getName();
    890891    }
    891892    else if (If * ifstmt = dyn_cast<If>(stmt))
     
    10341035        IRBuilder<> b(mBasicBlock);
    10351036
    1036         Value* var_value = b.CreateLoad(GetMarker(var->getVar()), false, var->getVar());
     1037        Value* var_value = b.CreateLoad(GetMarker(var->getName()), false, var->getName());
    10371038
    10381039        retVal = var_value;
  • icGREP/icgrep-devel/icgrep/pablo/codegenstate.h

    r4210 r4211  
    2828#include <vector>
    2929#include <string>
     30#include <array>
    3031#include <pablo/symbol_generator.h>
    3132
     
    3738    : mSymbolGenerator(symgen)
    3839    , mPredecessor(nullptr)
     40    , mAll{{makeAll(0), makeAll(1)}}
    3941    {
    4042
     
    4446    : mSymbolGenerator(cg.mSymbolGenerator)
    4547    , mPredecessor(&cg)
     48    , mAll(cg.mAll)    // inherit the original "All" variables for simplicity
    4649    {
    4750
    4851    }
    4952
     53    inline All * createAll(const bool value) const {
     54        return mAll[value];
     55    }
    5056
    51 //    PabloE * createAll(const bool value);
    5257//    PabloE * createAdvance(PabloE * expr);
    5358//    PabloE * createNot(PabloE * expr);
     
    132137    }
    133138
    134     std::string newsym;
    135 private:
    136     std::list<PabloE *>     mExpressions;
    137     SymbolGenerator &       mSymbolGenerator;
    138     CodeGenState * const    mPredecessor;
     139private:   
     140    SymbolGenerator &               mSymbolGenerator;
     141    CodeGenState * const            mPredecessor;
     142    const std::array<All *, 2>      mAll;
     143
     144
     145
     146    std::list<PabloE *>             mExpressions;
    139147};
    140148
  • icGREP/icgrep-devel/icgrep/pablo/pe_all.h

    r4209 r4211  
    1414class All : public PabloE {
    1515    friend All * makeAll(bool value);
     16    friend struct CodeGenState;
    1617public:
    1718    static inline bool classof(const PabloE * e) {
  • icGREP/icgrep-devel/icgrep/pablo/pe_pabloe.cpp

    r4200 r4211  
    3939        else if (const Var * var1 = dyn_cast<const Var>(expr1)) {
    4040            if (const Var * var2 = cast<const Var>(expr2)) {
    41                 return (var1->getVar() == var2->getVar());
     41                return (var1->getName() == var2->getName());
    4242            }
    4343        }
  • icGREP/icgrep-devel/icgrep/pablo/pe_var.h

    r4209 r4211  
    88#define PE_VAR_H
    99
    10 #include "pe_pabloe.h"
     10#include <pablo/pe_pabloe.h>
     11#include <pablo/ps_assign.h>
    1112#include <string>
    1213
     
    2526
    2627    }
    27     inline const std::string & getVar() const {
     28    inline const std::string & getName() const {
    2829        return mVar;
    2930    }
     
    4344}
    4445
     46inline Var * makeVar(const Assign * assign) {
     47    return makeVar(assign->getName());
     48}
     49
    4550}
    4651
  • icGREP/icgrep-devel/icgrep/pablo/printer_pablos.cpp

    r4210 r4211  
    4848
    4949    strOut = strOut.substr(0, strOut.length() - 1);
    50     strOut += "],";
    51 
    52     //Print the name of the variable that holds the match result for the overall expression so far.
    53     strOut += "'" + cg_state.newsym + "'";
     50    strOut += "]";
    5451
    5552    return strOut;
     
    7774    if (const Assign * an = dyn_cast<const Assign>(stmt))
    7875    {
    79         retVal = "Assign('" + an->getM() + "', " + ShowPabloE(an->getExpr()) + "),";
     76        retVal = "Assign('" + an->getName() + "', " + ShowPabloE(an->getExpr()) + "),";
    8077    }
    8178    else if (const If * ifstmt = dyn_cast<const If>(stmt))
     
    105102    else if (const Var * pablo_var = dyn_cast<const Var>(expr))
    106103    {
    107         retVal = "Var '" + pablo_var->getVar() + "' ";
     104        retVal = "Var '" + pablo_var->getName() + "' ";
    108105    }
    109106    else if (const And * pablo_and = dyn_cast<const And>(expr))
  • icGREP/icgrep-devel/icgrep/pablo/ps_assign.h

    r4209 r4211  
    2626    }
    2727
    28     inline const std::string & getM() const {
    29         return mM;
     28    inline const std::string & getName() const {
     29        return mName;
    3030    }
    3131
     
    3434    }
    3535protected:
    36     Assign(const std::string m, PabloE * expr)
     36    Assign(const std::string name, PabloE * expr)
    3737    : PabloE(ClassTypeId::Assign)
    38     , mM(m)
     38    , mName(name)
    3939    , mExpr(expr)
    4040    {
     
    4242    }
    4343private:
    44     const std::string   mM;
     44    const std::string   mName;
    4545    PabloE * const      mExpr;
    4646};
  • icGREP/icgrep-devel/icgrep/re/re_compiler.cpp

    r4210 r4211  
    5151void RE_Compiler::compile(RE * re) {
    5252
    53     std::string gs_m0 = mBaseCG.symgen("start_marker");
    54     mBaseCG.push_back(makeAssign(gs_m0, makeAll(1)));
    55 
    5653    if (hasUnicode(re)) {
    57         mBaseCG.newsym = gs_m0;
    5854        //Set the 'internal.initial' bit stream for the utf-8 multi-byte encoding.
    5955        std::string gs_initial = mBaseCG.symgen("internal.initial");
     
    6561        PabloE * u8pfx = makeOr(makeOr(u8pfx2, u8pfx3), u8pfx4);
    6662        mBaseCG.push_back(makeAssign(gs_initial, makeOr(u8pfx, u8single)));
    67         mBaseCG.newsym = gs_initial;
    6863
    6964        //Set the 'internal.nonfinal' bit stream for the utf-8 multi-byte encoding.
    70         mBaseCG.newsym = gs_m0;
    7165        std::string gs_nonfinal = mBaseCG.symgen("internal.nonfinal");
    7266        m_name_map.insert(make_pair("internal.nonfinal", gs_nonfinal));
     
    8680        mBaseCG.push_back(assign_non_final);
    8781        #endif
    88         mBaseCG.newsym = gs_nonfinal;
    89     }
    90 
    91     mBaseCG.newsym = gs_m0;
    92     process(re, mBaseCG);
     82    }
     83
     84    Assign * start_marker = makeAssign(mBaseCG.symgen("start_marker"), mBaseCG.createAll(1));
     85    mBaseCG.push_back(start_marker);
     86    Assign * result = process(re, start_marker, mBaseCG);
    9387
    9488    //These three lines are specifically for grep.
    95     std::string gs_retVal = mBaseCG.symgen("marker");
    96     mBaseCG.push_back(makeAssign(gs_retVal, makeAnd(makeMatchStar(makeVar(mBaseCG.newsym),
    97         makeNot(makeVar(m_name_map.find("LineFeed")->second))), makeVar(m_name_map.find("LineFeed")->second))));
    98     mBaseCG.newsym = gs_retVal;
    99 }
    100 
    101 void RE_Compiler::process(RE * re, CodeGenState & cg) {
     89    Assign * final = makeAssign(mBaseCG.symgen("marker"), makeAnd(makeMatchStar(makeVar(result),
     90        makeNot(makeVar(m_name_map.find("LineFeed")->second))), makeVar(m_name_map.find("LineFeed")->second)));
     91    mBaseCG.push_back(final);
     92}
     93
     94Assign * RE_Compiler::process(RE * re, Assign * target, CodeGenState & cg) {
    10295    if (Name * name = dyn_cast<Name>(re)) {
    103         process(name, cg);
     96        target = process(name, target, cg);
    10497    }
    10598    else if (Seq* seq = dyn_cast<Seq>(re)) {
    106         process(seq, cg);
     99        target = process(seq, target, cg);
    107100    }
    108101    else if (Alt * alt = dyn_cast<Alt>(re)) {
    109         process(alt, cg);
     102        target = process(alt, target, cg);
    110103    }
    111104    else if (Rep * rep = dyn_cast<Rep>(re)) {
    112         process(rep, cg);
     105        target = process(rep, target, cg);
    113106    }
    114107    else if (isa<Start>(re)) {
    115         std::string gs_retVal = cg.symgen("sol");
    116         cg.push_back(makeAssign(gs_retVal, makeAnd(makeVar(cg.newsym), makeNot(makeAdvance(makeNot(makeCharClass(m_name_map.find("LineFeed")->second)))))));
    117         cg.newsym = gs_retVal;
     108        target = makeAssign(cg.symgen("sol"), makeAnd(makeVar(target), makeNot(makeAdvance(makeNot(makeCharClass(m_name_map.find("LineFeed")->second))))));
     109        cg.push_back(target);
    118110    }
    119111    else if (isa<End>(re)) {
    120         std::string gs_retVal = cg.symgen("eol");
    121         cg.push_back(makeAssign(gs_retVal, makeAnd(makeVar(cg.newsym), makeCharClass(m_name_map.find("LineFeed")->second))));
    122         cg.newsym = gs_retVal;
    123     }
    124 }
    125 
    126 inline void RE_Compiler::process(Name * name, CodeGenState & cg) {
    127     std::string gs_retVal = cg.symgen("marker");
    128     PabloE * markerExpr = makeVar(cg.newsym);
     112        target = makeAssign(cg.symgen("eol"), makeAnd(makeVar(target), makeCharClass(m_name_map.find("LineFeed")->second)));
     113        cg.push_back(target);
     114    }
     115
     116    return target;
     117}
     118
     119inline Assign * RE_Compiler::process(Name * name, Assign * target, CodeGenState & cg) {
     120    PabloE * markerExpr = makeVar(target);
    129121    if (name->getType() != Name::Type::FixedLength) {
    130122        // Move the markers forward through any nonfinal UTF-8 bytes to the final position of each character.
    131123        markerExpr = makeAnd(markerExpr, makeCharClass(m_name_map.find("internal.initial")->second));
    132         markerExpr = new ScanThru(markerExpr, makeCharClass(m_name_map.find("internal.nonfinal")->second));
     124        markerExpr = makeScanThru(markerExpr, makeCharClass(m_name_map.find("internal.nonfinal")->second));
    133125    }
    134126    PabloE * ccExpr;
     
    143135                                makeCharClass(m_name_map.find("internal.nonfinal")->second)));
    144136    }
    145     cg.push_back(makeAssign(gs_retVal, makeAdvance(makeAnd(ccExpr, markerExpr))));
    146     cg.newsym = gs_retVal;
    147 }
    148 
    149 inline void RE_Compiler::process(Seq * seq, CodeGenState & cg) {
     137    target = makeAssign(cg.symgen("marker"), makeAdvance(makeAnd(ccExpr, markerExpr)));
     138    cg.push_back(target);
     139    return target;
     140}
     141
     142inline Assign * RE_Compiler::process(Seq * seq, Assign * target, CodeGenState & cg) {
    150143    for (RE * re : *seq) {
    151         process(re, cg);
    152     }
    153 }
    154 
    155 inline void RE_Compiler::process(Alt * alt, CodeGenState & cg) {
     144        target = process(re, target, cg);
     145    }
     146    cg.push_back(target);
     147    return target;
     148}
     149
     150inline Assign * RE_Compiler::process(Alt * alt, Assign * target, CodeGenState & cg) {
    156151    if (alt->empty()) {
    157         std::string gs_retVal = cg.symgen("always_fail_marker");
    158         cg.push_back(makeAssign(gs_retVal, makeAll(0)));
    159         cg.newsym = gs_retVal;
     152        target = makeAssign(cg.symgen("always_fail"), cg.createAll(0));
     153        cg.push_back(target);
    160154    }
    161155    else {
    162156        auto i = alt->begin();
    163         const std::string startsym = cg.newsym;
    164         process(*i, cg);
     157        Assign * const base = target;
     158        target = process(*i, target, cg);
    165159        while (++i != alt->end()) {
    166             std::string alt1 = cg.newsym;
    167             cg.newsym = startsym;
    168             process(*i, cg);
    169             std::string newsym = cg.symgen("alt");
    170             cg.push_back(makeAssign(newsym, makeOr(makeVar(alt1), makeVar(cg.newsym))));
    171             cg.newsym = newsym;
    172         }
    173     }
    174 }
    175 
    176 inline void RE_Compiler::process(Rep * rep, CodeGenState & cg) {
     160            Var * alt1 = makeVar(process(*i, base, cg));
     161            Var * alt2 = makeVar(target);
     162            target = makeAssign(cg.symgen("alt"), makeOr(alt2, alt1));
     163            cg.push_back(target);
     164        }
     165    }
     166    return target;
     167}
     168
     169inline Assign * RE_Compiler::process(Rep * rep, Assign * target, CodeGenState & cg) {
    177170    if (rep->getUB() == Rep::UNBOUNDED_REP) {
    178         processUnboundedRep(rep->getRE(), rep->getLB(), cg);
     171        target = processUnboundedRep(rep->getRE(), rep->getLB(), target, cg);
    179172    }
    180173    else { // if (rep->getUB() != Rep::UNBOUNDED_REP)
    181         processBoundedRep(rep->getRE(), rep->getLB(), rep->getUB(), cg);
    182     }
    183 }
    184 
    185 inline void RE_Compiler::processUnboundedRep(RE * repeated, int lb, CodeGenState & cg) {
     174        target = processBoundedRep(rep->getRE(), rep->getLB(), rep->getUB(), target, cg);
     175    }
     176    cg.push_back(target);
     177    return target;
     178}
     179
     180inline Assign * RE_Compiler::processUnboundedRep(RE * repeated, int lb, Assign * target, CodeGenState & cg) {
    186181    while (lb-- != 0) {
    187         process(repeated, cg);
     182        target = process(repeated, target, cg);
    188183    }
    189184    if (isa<Name>(repeated)) {
    190185        Name * rep_name = dyn_cast<Name>(repeated);
    191         std::string gs_retVal = cg.symgen("marker");
    192 
    193         PabloE* ccExpr;
     186
     187        PabloE * ccExpr;
    194188        if (rep_name->getType() == Name::Type::UnicodeCategory) {
    195189            ccExpr = makeCall(rep_name->getName());
     
    202196            ccExpr = makeNot(makeOr(makeOr(ccExpr, makeCharClass(m_name_map.find("LineFeed")->second)), makeCharClass(m_name_map.find("internal.nonfinal")->second)));
    203197        }
     198
     199
     200        std::string marker = cg.symgen("marker");
    204201        if (rep_name->getType() == Name::Type::FixedLength) {
    205             cg.push_back(makeAssign(gs_retVal, makeMatchStar(makeVar(cg.newsym), ccExpr)));
     202            target = makeAssign(marker, makeMatchStar(makeVar(target), ccExpr));
    206203        }
    207204        else { // Name::Unicode and Name::UnicodeCategory
    208             cg.push_back(makeAssign(gs_retVal,
    209                 makeAnd(makeMatchStar(makeVar(cg.newsym),
     205            target = makeAssign(marker,
     206                makeAnd(makeMatchStar(makeVar(target),
    210207                        makeOr(makeCharClass(m_name_map.find("internal.nonfinal")->second), ccExpr)),
    211                                makeCharClass(m_name_map.find("internal.initial")->second))));
    212         }
    213         cg.newsym = gs_retVal;
     208                               makeCharClass(m_name_map.find("internal.initial")->second)));
     209        }
    214210    }
    215211    else {
    216       std::string while_test = cg.symgen("while_test");
    217       std::string while_accum = cg.symgen("while_accum");
    218 
    219       CodeGenState wt(cg);
    220 
    221       wt.newsym = while_test;
    222       process(repeated, wt);
    223 
    224       cg.push_back(makeAssign(while_test, makeVar(cg.newsym)));
    225       cg.push_back(makeAssign(while_accum, makeVar(cg.newsym)));
    226 
    227       wt.push_back(makeAssign(while_test, makeAnd(makeVar(wt.newsym), makeNot(makeVar(while_accum)))));
    228       wt.push_back(makeAssign(while_accum, makeOr(makeVar(while_accum), makeVar(wt.newsym))));
    229 
    230       cg.push_back(makeWhile(makeVar(while_test), wt.expressions()));
    231       cg.newsym = while_accum;
    232     }
    233 }
    234 
    235 inline void RE_Compiler::processBoundedRep(RE * repeated, int lb, int ub, CodeGenState & cg) {
     212
     213        Assign * while_test = makeAssign(cg.symgen("while_test"), makeVar(target));
     214        Assign * while_accum = makeAssign(cg.symgen("while_accum"), makeVar(target));
     215
     216        CodeGenState wt(cg);
     217
     218        Var * accum = makeVar(process(repeated, while_test, wt));
     219
     220        cg.push_back(while_test);
     221        cg.push_back(while_accum);
     222
     223        Var * var_while_test = makeVar(while_accum);
     224
     225        wt.push_back(makeAssign(while_test->getName(), makeAnd(accum, makeNot(var_while_test))));
     226
     227        target = makeAssign(while_accum->getName(), makeOr(var_while_test, accum));
     228
     229        wt.push_back(target);
     230        cg.push_back(makeWhile(makeVar(while_test), wt.expressions()));
     231    }   
     232    return target;
     233}
     234
     235inline Assign * RE_Compiler::processBoundedRep(RE * repeated, int lb, int ub, Assign * target, CodeGenState & cg) {
    236236    ub -= lb;
    237237    while(lb-- != 0) {
    238         process(repeated, cg);
    239     }
    240     if (ub > 0) {
    241          std::string oldsym = cg.newsym;
    242          process(repeated, cg);
    243          processBoundedRep(repeated, 0, ub - 1, cg);
    244          std::string altsym = cg.symgen("alt");
    245          cg.push_back(makeAssign(altsym, makeOr(makeVar(oldsym), makeVar(cg.newsym))));
    246          cg.newsym = altsym;
    247     }
     238        target = process(repeated, target, cg);
     239    }
     240    while (ub-- != 0) {
     241        Assign * alt = process(repeated, target, cg);
     242        target = makeAssign(cg.symgen("alt"), makeOr(makeVar(target), makeVar(alt)));
     243        cg.push_back(target);
     244    }
     245    return target;
    248246}
    249247
  • icGREP/icgrep-devel/icgrep/re/re_compiler.h

    r4210 r4211  
    2424
    2525private:
    26     void process(RE * re, pablo::CodeGenState & cg_state);
    27     void process(Alt * alt, pablo::CodeGenState & cg_state);
    28     void process(Seq * seq, pablo::CodeGenState & cg_state);
    29     void process(Rep * rep, pablo::CodeGenState & cg_state);
    30     void processUnboundedRep(RE * repeated, int lb, pablo::CodeGenState & cg_state);
    31     void processBoundedRep(RE * repeated, int lb, int ub, pablo::CodeGenState & cg_state);
    32     void process(Name * name, pablo::CodeGenState & cg_state);
     26    pablo::Assign * process(RE * re, pablo::Assign * target, pablo::CodeGenState & cg_state);
     27    pablo::Assign * process(Alt * alt, pablo::Assign * target, pablo::CodeGenState & cg_state);
     28    pablo::Assign * process(Seq * seq, pablo::Assign * target, pablo::CodeGenState & cg_state);
     29    pablo::Assign * process(Rep * rep, pablo::Assign * target, pablo::CodeGenState & cg_state);
     30    pablo::Assign * processUnboundedRep(RE * repeated, int lb, pablo::Assign * target, pablo::CodeGenState & cg_state);
     31    pablo::Assign * processBoundedRep(RE * repeated, int lb, int ub, pablo::Assign * target, pablo::CodeGenState & cg_state);
     32    pablo::Assign * process(Name * name, pablo::Assign * target, pablo::CodeGenState & cg_state);
    3333
    3434    static bool hasUnicode(const RE *re);
    3535
    36     pablo::CodeGenState &               mBaseCG;
     36    pablo::CodeGenState &                      mBaseCG;
    3737    std::map<std::string, std::string>  m_name_map;
    3838};
Note: See TracChangeset for help on using the changeset viewer.