Changeset 4213 for icGREP


Ignore:
Timestamp:
Oct 5, 2014, 1:42:07 PM (5 years ago)
Author:
nmedfort
Message:

Created internal 'string pool' for Call, CharClass?, Var and Assign pablo classes.

Location:
icGREP/icgrep-devel/icgrep
Files:
1 added
10 edited

Legend:

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

    r4212 r4213  
    6060                    Name * name = dyn_cast<Name>(*j);
    6161                    assert (name);
    62                     CharClass * cc = makeCharClass(name->getName());
     62                    CharClass * cc = mCG.createCharClass(name->getName());
    6363                    PabloE * sym = marker ? makeAnd(marker, cc) : cc;
    6464                    if (++j != seq->end()) {
     
    6767                        continue;
    6868                    }
    69                     mCG.push_back(makeAssign(seq->getName(), sym));
     69                    mCG.push_back(mCG.createAssign(seq->getName(), sym));
    7070                    break;
    7171                }
     
    7878{   
    7979    //Add the new mapping to the list of pablo statements:
    80     mCG.push_back(makeAssign(varname, expr->pablo_expr));
     80    mCG.push_back(mCG.createAssign(varname, expr->pablo_expr));
    8181
    8282    //Add the new mapping to the common expression map:
     
    8484    Expression* mapped_value = new Expression();
    8585    mapped_value->expr_string = varname;
    86     mapped_value->pablo_expr = makeVar(varname);
     86    mapped_value->pablo_expr = mCG.createVar(varname);
    8787    return mCommon_Expression_Map.insert(std::make_pair(key_value, mapped_value)).first->second;
    8888}
     
    383383
    384384inline Var * CC_Compiler::getBasisVar(const int i) const {
    385     return makeVar(mBasisPattern + std::to_string((mEncoding.getBits() - 1) - i));
     385    return mCG.createVar(mBasisPattern + std::to_string((mEncoding.getBits() - 1) - i));
    386386}
    387387
  • icGREP/icgrep-devel/icgrep/pablo/codegenstate.cpp

    r4207 r4213  
    66
    77#include <pablo/codegenstate.h>
     8
     9namespace pablo {
     10
     11String * CodeGenState::getString(const std::string string) {
     12    auto f = mStringMap.find(string);
     13    String * result;
     14    if (f == mStringMap.end()) {
     15        result = makeString(string);
     16        mStringMap.insert(std::make_pair(*result, result));
     17    }
     18    else {
     19        result = f->second;
     20    }
     21    return result;
     22}
     23
     24Call * CodeGenState::createCall(const std::string name) {
     25    return makeCall(getString(name));
     26}
     27
     28Var * CodeGenState::createVar(const std::string name) {
     29    return makeVar(getString(name));
     30}
     31
     32Var * CodeGenState::createVar(const Assign * assign) {
     33    return makeVar(assign);
     34}
     35
     36CharClass * CodeGenState::createCharClass(const std::string name) {
     37    return makeCharClass(getString(name));
     38}
     39
     40Assign *CodeGenState::createAssign(const std::string name, PabloE * expr) {
     41    return makeAssign(getString(name), expr);
     42}
     43
     44}
  • icGREP/icgrep-devel/icgrep/pablo/codegenstate.h

    r4211 r4213  
    99
    1010#include <pablo/pe_pabloe.h>
     11#include <pablo/pe_string.h>
    1112#include <pablo/pe_advance.h>
    1213#include <pablo/pe_all.h>
     
    5455        return mAll[value];
    5556    }
     57
     58    Call * createCall(const std::string name);
     59
     60    Var * createVar(const Assign * assign);
     61
     62    Var * createVar(const std::string name);
     63
     64    inline PabloE * createVarIfAssign(PabloE * const input) {
     65        return isa<Assign>(input) ? createVar(cast<const Assign>(input)) : input;
     66    }
     67
     68    CharClass * createCharClass(const std::string name);
     69    Assign * createAssign(const std::string name, PabloE * expr);
    5670
    5771//    PabloE * createAdvance(PabloE * expr);
     
    124138//    };
    125139
     140//    template<typename... Args>
     141//    struct ExpressionMap {
     142//        typedef std::tuple<PabloE::ClassTypeId, Args...> Key;
     143
     144//        inline PabloE * find(const PabloE::ClassTypeId type, Args... args) {
     145//            auto key = std::make_tuple(type, args...);
     146//            auto itr = _map.find(key);
     147//            if (itr == _map.end()) {
     148//                _map.insert(std::make_pair(std::move(key), inst));
     149//                return nullptr;
     150//            }
     151//            return itr->second;
     152//        }
     153
     154//        inline PabloE * find(const PabloE::ClassTypeId type, Args... args) {
     155//            auto key = std::make_tuple(type, args...);
     156//            auto itr = _map.find(key);
     157//            if (itr == _map.end()) {
     158//                _map.insert(std::make_pair(std::move(key), inst));
     159//                return nullptr;
     160//            }
     161//            return itr->second;
     162//        }
     163
     164//    private:
     165//        std::unordered_map<Key, PabloE *> _map;
     166//    };
     167
     168
    126169
    127170    inline void push_back(PabloE * expr) {
     
    137180    }
    138181
     182protected:
     183
     184    String * getString(const std::string string);
     185
    139186private:   
    140     SymbolGenerator &               mSymbolGenerator;
    141     CodeGenState * const            mPredecessor;
    142     const std::array<All *, 2>      mAll;
     187    SymbolGenerator &                           mSymbolGenerator;
     188    CodeGenState * const                        mPredecessor;
     189    const std::array<All *, 2>                  mAll;
     190    std::unordered_map<std::string, String *>   mStringMap;
    143191
    144192
    145 
    146     std::list<PabloE *>             mExpressions;
     193    std::list<PabloE *>                         mExpressions;
    147194};
    148195
  • icGREP/icgrep-devel/icgrep/pablo/pe_call.h

    r4209 r4213  
    22#define PE_CALL_H
    33
    4 #include "pe_pabloe.h"
    5 #include <string>
     4#include <pablo/pe_pabloe.h>
     5#include <pablo/pe_string.h>
    66
    77namespace pablo {
    88
    99class Call : public PabloE {
    10     friend Call * makeCall(const std::string callee);
     10    friend Call * makeCall(const String *);
    1111public:
    1212    static inline bool classof(const PabloE * e) {
     
    1919
    2020    }
    21 
    2221    inline const std::string & getCallee() const {
    23         return mCallee;
     22        return *mCallee;
    2423    }
    2524protected:
    26     Call(const std::string callee)
     25    Call(const String * callee)
    2726    : PabloE(ClassTypeId::Call)
    2827    , mCallee(callee) {
     
    3029    }
    3130private:
    32     const std::string mCallee;
     31    const String * const mCallee;
    3332};
    3433
    35 inline Call * makeCall(const std::string callee) {
     34inline Call * makeCall(const String * callee) {
    3635    return new Call(callee);
    3736}
  • icGREP/icgrep-devel/icgrep/pablo/pe_charclass.h

    r4209 r4213  
    88#define PE_CHARCLASS_H
    99
    10 #include "pe_pabloe.h"
    11 #include <string>
     10#include <pablo/pe_pabloe.h>
     11#include <pablo/pe_string.h>
    1212
    1313namespace pablo {
    1414
    1515class CharClass : public PabloE {
    16     friend CharClass * makeCharClass(const std::string cc);
     16    friend CharClass * makeCharClass(const String *);
    1717public:
    1818    static inline bool classof(const PabloE * e) {
     
    2626    }
    2727    inline const std::string & getCharClass() const {
    28         return mCharClass;
     28        return *mCharClass;
    2929    }
    3030protected:
    31     CharClass(const std::string charClass)
     31    CharClass(const String * cc)
    3232    : PabloE(ClassTypeId::CharClass)
    33     , mCharClass(charClass)
     33    , mCharClass(cc)
    3434    {
    3535
    3636    }
    3737private:
    38     const std::string mCharClass;
     38    const String * const mCharClass;
    3939};
    4040
    41 inline CharClass * makeCharClass(const std::string cc) {
     41inline CharClass * makeCharClass(const String * cc) {
    4242    return new CharClass(cc);
    4343}
  • icGREP/icgrep-devel/icgrep/pablo/pe_pabloe.h

    r4200 r4213  
    3333        , If
    3434        , While
     35        , String
    3536    };
    3637    inline ClassTypeId getClassTypeId() const {
  • icGREP/icgrep-devel/icgrep/pablo/pe_var.h

    r4211 r4213  
    1010#include <pablo/pe_pabloe.h>
    1111#include <pablo/ps_assign.h>
    12 #include <string>
     12#include <pablo/pe_string.h>
    1313
    1414namespace pablo {
    1515
    1616class Var : public PabloE {
    17     friend Var * makeVar(const std::string);
     17    friend Var * makeVar(const String *);
    1818public:
    1919    static inline bool classof(const PabloE * e) {
     
    2727    }
    2828    inline const std::string & getName() const {
    29         return mVar;
     29        return *mVar;
    3030    }
    3131protected:
    32     Var(const std::string var)
     32    Var(const String * var)
    3333    : PabloE(ClassTypeId::Var)
    3434    , mVar(var)
     
    3737    }
    3838private:
    39     const std::string mVar;
     39    const String * const mVar;
    4040};
    4141
    42 inline Var * makeVar(const std::string var) {
     42inline Var * makeVar(const String * var) {
    4343    return new Var(var);
    4444}
    4545
    4646inline Var * makeVar(const Assign * assign) {
    47     return makeVar(assign->getName());
     47    return makeVar(assign->mName);
    4848}
    4949
  • icGREP/icgrep-devel/icgrep/pablo/ps_assign.h

    r4211 r4213  
    88#define PS_SETMARKER_H
    99
    10 #include "pe_pabloe.h"
    11 #include <string>
     10#include <pablo/pe_pabloe.h>
     11#include <pablo/pe_string.h>
    1212
    1313namespace pablo {
    1414
     15class Var;
     16
    1517class Assign : public PabloE {
    16     friend Assign * makeAssign(const std::string, PabloE *);
     18    friend Assign * makeAssign(const String *, PabloE *);
     19    friend Var * makeVar(const Assign *);
    1720public:
    1821    static inline bool classof(const PabloE * e) {
     
    2730
    2831    inline const std::string & getName() const {
    29         return mName;
     32        return *mName;
    3033    }
    3134
     
    3437    }
    3538protected:
    36     Assign(const std::string name, PabloE * expr)
     39    Assign(const String * name, PabloE * expr)
    3740    : PabloE(ClassTypeId::Assign)
    3841    , mName(name)
     
    4245    }
    4346private:
    44     const std::string   mName;
    45     PabloE * const      mExpr;
     47    const String * const    mName;
     48    PabloE * const          mExpr;
    4649};
    4750
    48 inline Assign * makeAssign(const std::string marker, PabloE * expr) {
     51inline Assign * makeAssign(const String * marker, PabloE * expr) {
    4952    return new Assign(marker, expr);
    5053}
  • icGREP/icgrep-devel/icgrep/re/re_compiler.cpp

    r4212 r4213  
    4343
    4444RE_Compiler::RE_Compiler(CodeGenState & baseCG, std::map<std::string, std::string> name_map)
    45 : mBaseCG(baseCG)
     45: mCG(baseCG)
    4646, m_name_map(name_map)
    4747{
     
    4949}
    5050
    51 inline PabloE * makeVarIfAssign(PabloE * const input) {
    52     return isa<Assign>(input) ? makeVar(cast<Assign>(input)) : input;
    53 }
    54 
    5551void RE_Compiler::compile(RE * re) {
    5652
    5753    if (hasUnicode(re)) {
    5854        //Set the 'internal.initial' bit stream for the utf-8 multi-byte encoding.
    59         std::string gs_initial = mBaseCG.symgen("initial");
     55        std::string gs_initial = mCG.symgen("initial");
    6056        m_name_map.insert(make_pair("initial", gs_initial));
    61         PabloE * u8single = makeVar(m_name_map.find("UTF8-SingleByte")->second);
    62         PabloE * u8pfx2 = makeVar(m_name_map.find("UTF8-Prefix2")->second);
    63         PabloE * u8pfx3 = makeVar(m_name_map.find("UTF8-Prefix3")->second);
    64         PabloE * u8pfx4 = makeVar(m_name_map.find("UTF8-Prefix4")->second);
     57        PabloE * u8single = mCG.createVar(m_name_map.find("UTF8-SingleByte")->second);
     58        PabloE * u8pfx2 = mCG.createVar(m_name_map.find("UTF8-Prefix2")->second);
     59        PabloE * u8pfx3 = mCG.createVar(m_name_map.find("UTF8-Prefix3")->second);
     60        PabloE * u8pfx4 = mCG.createVar(m_name_map.find("UTF8-Prefix4")->second);
    6561        PabloE * u8pfx = makeOr(makeOr(u8pfx2, u8pfx3), u8pfx4);
    66         mBaseCG.push_back(makeAssign(gs_initial, makeOr(u8pfx, u8single)));
     62        mCG.push_back(mCG.createAssign(gs_initial, makeOr(u8pfx, u8single)));
    6763
    6864        //Set the 'internal.nonfinal' bit stream for the utf-8 multi-byte encoding.
    69         std::string gs_nonfinal = mBaseCG.symgen("nonfinal");
     65        std::string gs_nonfinal = mCG.symgen("nonfinal");
    7066        m_name_map.insert(make_pair("nonfinal", gs_nonfinal));
    7167        //#define USE_IF_FOR_NONFINAL
     
    7672        PabloE * u8scope42 = makeAdvance(u8pfx4);
    7773        PabloE * u8scope43 = makeAdvance(u8scope42);
    78         PabloE * assign_non_final = makeAssign(gs_nonfinal, makeOr(makeOr(u8pfx, u8scope32), makeOr(u8scope42, u8scope43)));
     74        PabloE * assign_non_final = mCG.createAssign(gs_nonfinal, makeOr(makeOr(u8pfx, u8scope32), makeOr(u8scope42, u8scope43)));
    7975        #ifdef USE_IF_FOR_NONFINAL
    8076        std::list<PabloE *> * if_body = new std::list<PabloE *> ();
    8177        if_body->push_back(assign_non_final);
    82         cg.push_back(makeIf(u8pfx, *if_body));
     78        mCG.push_back(makeIf(u8pfx, *if_body));
    8379        #else
    84         mBaseCG.push_back(assign_non_final);
     80        mCG.push_back(assign_non_final);
    8581        #endif
    8682    }
    8783
    88     PabloE * start_marker = mBaseCG.createAll(1);
    89     mBaseCG.push_back(start_marker);
    90     PabloE * result = process(re, start_marker, mBaseCG);
     84    PabloE * start_marker = mCG.createAll(1);
     85    mCG.push_back(start_marker);
     86    PabloE * result = process(re, start_marker, mCG);
    9187
    9288    //These three lines are specifically for grep.
    93     Assign * final = makeAssign(mBaseCG.symgen("marker"), makeAnd(makeMatchStar(makeVarIfAssign(result),
    94         makeNot(makeVar(m_name_map.find("LineFeed")->second))), makeVar(m_name_map.find("LineFeed")->second)));
    95     mBaseCG.push_back(final);
     89    Assign * final = mCG.createAssign(mCG.symgen("marker"), makeAnd(makeMatchStar(mCG.createVarIfAssign(result),
     90        makeNot(mCG.createVar(m_name_map.find("LineFeed")->second))), mCG.createVar(m_name_map.find("LineFeed")->second)));
     91    mCG.push_back(final);
    9692}
    9793
     
    110106    }
    111107    else if (isa<Start>(re)) {
    112         target = makeAnd(makeVarIfAssign(target), makeNot(makeAdvance(makeNot(makeCharClass(m_name_map.find("LineFeed")->second)))));
     108        target = makeAnd(cg.createVarIfAssign(target), makeNot(makeAdvance(makeNot(cg.createCharClass(m_name_map.find("LineFeed")->second)))));
    113109        cg.push_back(target);
    114110    }
    115111    else if (isa<End>(re)) {
    116         target = makeAnd(makeVarIfAssign(target), makeCharClass(m_name_map.find("LineFeed")->second));
     112        target = makeAnd(cg.createVarIfAssign(target), cg.createCharClass(m_name_map.find("LineFeed")->second));
    117113        cg.push_back(target);
    118114    }
     
    122118
    123119inline PabloE * RE_Compiler::process(Name * name, PabloE * target, CodeGenState & cg) {
    124     PabloE * markerExpr = makeVarIfAssign(target);
     120    PabloE * markerExpr = cg.createVarIfAssign(target);
    125121    if (name->getType() != Name::Type::FixedLength) {
    126122        // Move the markers forward through any nonfinal UTF-8 bytes to the final position of each character.
    127         markerExpr = makeAnd(markerExpr, makeCharClass(m_name_map.find("initial")->second));
    128         markerExpr = makeScanThru(markerExpr, makeCharClass(m_name_map.find("nonfinal")->second));
     123        markerExpr = makeAnd(markerExpr, cg.createCharClass(m_name_map.find("initial")->second));
     124        markerExpr = makeScanThru(markerExpr, cg.createCharClass(m_name_map.find("nonfinal")->second));
    129125    }
    130126    PabloE * cc = nullptr;
    131127    if (name->getType() == Name::Type::UnicodeCategory) {
    132         cc = makeCall(name->getName());
     128        cc = cg.createCall(name->getName());
    133129    }
    134130    else {
    135         cc = makeCharClass(name->getName());
     131        cc = cg.createCharClass(name->getName());
    136132    }
    137133    if (name->isNegated()) {
    138         cc = makeNot(makeOr(makeOr(cc, makeCharClass(m_name_map.find("LineFeed")->second)),
    139                                 makeCharClass(m_name_map.find("nonfinal")->second)));
    140     }
    141     target = makeAssign(cg.symgen("marker"), makeAdvance(makeAnd(cc, markerExpr)));
     134        cc = makeNot(makeOr(makeOr(cc, cg.createCharClass(m_name_map.find("LineFeed")->second)),
     135                                cg.createCharClass(m_name_map.find("nonfinal")->second)));
     136    }
     137    target = cg.createAssign(cg.symgen("marker"), makeAdvance(makeAnd(cc, markerExpr)));
    142138    cg.push_back(target);
    143139    return target;
     
    159155        auto i = alt->begin();
    160156        PabloE * const base = target;
    161         target = makeVarIfAssign(process(*i, target, cg));
     157        target = cg.createVarIfAssign(process(*i, target, cg));
    162158        while (++i != alt->end()) {
    163             PabloE * other = makeVarIfAssign(process(*i, base, cg));
     159            PabloE * other = cg.createVarIfAssign(process(*i, base, cg));
    164160            target = makeOr(target, other);
    165161        }
     
    183179        target = process(repeated, target, cg);
    184180    }
     181
     182    target = cg.createVarIfAssign(target);
     183
    185184    if (isa<Name>(repeated)) {
    186185        Name * rep_name = dyn_cast<Name>(repeated);
     
    188187        PabloE * ccExpr;
    189188        if (rep_name->getType() == Name::Type::UnicodeCategory) {
    190             ccExpr = makeCall(rep_name->getName());
     189            ccExpr = cg.createCall(rep_name->getName());
    191190        }
    192191        else {
    193             ccExpr = makeCharClass(rep_name->getName());
     192            ccExpr = cg.createCharClass(rep_name->getName());
    194193        }
    195194
    196195        if (rep_name->isNegated()) {
    197             ccExpr = makeNot(makeOr(makeOr(ccExpr, makeCharClass(m_name_map.find("LineFeed")->second)), makeCharClass(m_name_map.find("nonfinal")->second)));
     196            ccExpr = makeNot(makeOr(makeOr(ccExpr, cg.createCharClass(m_name_map.find("LineFeed")->second)), cg.createCharClass(m_name_map.find("nonfinal")->second)));
    198197        }
    199198
    200199        if (rep_name->getType() == Name::Type::FixedLength) {
    201             target = makeMatchStar(makeVarIfAssign(target), ccExpr);
     200            target = makeMatchStar(target, ccExpr);
    202201        }
    203202        else { // Name::Unicode and Name::UnicodeCategory
    204             target = makeAnd(makeMatchStar(makeVarIfAssign(target), makeOr(makeCharClass(m_name_map.find("nonfinal")->second), ccExpr)), makeCharClass(m_name_map.find("initial")->second));
     203            target = makeAnd(makeMatchStar(target, makeOr(cg.createCharClass(m_name_map.find("nonfinal")->second), ccExpr)), cg.createCharClass(m_name_map.find("initial")->second));
    205204        }
    206205    }
    207206    else {
    208207
    209         Assign * while_test = makeAssign(cg.symgen("while_test"), makeVarIfAssign(target));
    210         Assign * while_accum = makeAssign(cg.symgen("while_accum"), makeVarIfAssign(target));
     208        Assign * while_test = cg.createAssign(cg.symgen("while_test"), target);
     209        Assign * while_accum = cg.createAssign(cg.symgen("while_accum"), target);
    211210
    212211        CodeGenState wt(cg);
    213212
    214         PabloE * accum = makeVarIfAssign(process(repeated, while_test, wt));
     213        PabloE * accum = cg.createVarIfAssign(process(repeated, while_test, wt));
    215214
    216215        cg.push_back(while_test);
    217216        cg.push_back(while_accum);
    218217
    219         Var * var_while_test = makeVar(while_accum);
    220 
    221         wt.push_back(makeAssign(while_test->getName(), makeAnd(accum, makeNot(var_while_test))));
    222 
    223         target = makeAssign(while_accum->getName(), makeOr(var_while_test, accum));
     218        Var * var_while_test = cg.createVar(while_accum);
     219
     220        wt.push_back(cg.createAssign(while_test->getName(), makeAnd(accum, makeNot(var_while_test))));
     221
     222        target = cg.createAssign(while_accum->getName(), makeOr(var_while_test, accum));
    224223
    225224        wt.push_back(target);
    226         cg.push_back(makeWhile(makeVar(while_test), wt.expressions()));
     225        cg.push_back(makeWhile(cg.createVar(while_test), wt.expressions()));
    227226    }   
    228227    return target;
     
    236235    while (ub-- != 0) {
    237236        PabloE * alt = process(repeated, target, cg);
    238         target = makeOr(makeVarIfAssign(target), makeVarIfAssign(alt));
     237        target = makeOr(cg.createVarIfAssign(target), cg.createVarIfAssign(alt));
    239238    }
    240239    cg.push_back(target);
  • icGREP/icgrep-devel/icgrep/re/re_compiler.h

    r4212 r4213  
    3434    static bool hasUnicode(const RE *re);
    3535
    36     pablo::CodeGenState &                      mBaseCG;
     36    pablo::CodeGenState &                      mCG;
    3737    std::map<std::string, std::string>  m_name_map;
    3838};
Note: See TracChangeset for help on using the changeset viewer.