Ignore:
Timestamp:
Jan 6, 2015, 3:29:34 PM (5 years ago)
Author:
nmedfort
Message:

Some work towards maintaining UseDef? information. Use PabloBlock::Create() instead of the PabloBlock? constructors.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • icGREP/icgrep-devel/icgrep/pablo/codegenstate.h

    r4344 r4404  
    3636namespace pablo {
    3737
    38 class PabloBlock {
     38class PabloBlock : public StatementList {
     39    friend class pablo::PabloAST;
    3940public:
    4041
    41     PabloBlock(SymbolGenerator & symgen)
    42     : mZeroes(new Zeroes())
    43     , mOnes(new Ones())
    44     , mSymbolGenerator(symgen)
    45     , mUnary(nullptr, this)
    46     , mUnaryWithInt(nullptr, this)
    47     , mBinary(nullptr, this)
    48     , mTernary(nullptr, this)
    49     {
    50 
    51     }
    52 
    53     PabloBlock(PabloBlock & cg)
    54     : mZeroes(cg.mZeroes) // inherit the original "Zeroes" variable for simplicity
    55     , mOnes(cg.mOnes) // inherit the original "Ones" variable for simplicity
    56     , mSymbolGenerator(cg.mSymbolGenerator)
    57     , mUnary(&(cg.mUnary), this)
    58     , mUnaryWithInt(&(cg.mUnaryWithInt), this)
    59     , mBinary(&(cg.mBinary), this)
    60     , mTernary(&(cg.mTernary), this)
    61     {
    62 
     42    inline static PabloBlock & Create() {
     43        return *(new PabloBlock());
     44    }
     45
     46    inline static PabloBlock & Create(PabloBlock & predecessor) {
     47        return *(new PabloBlock(&predecessor));
    6348    }
    6449
     
    7358    }
    7459
    75     Call * createCall(const std::string name);
    76 
    77     inline Assign * createAssign(const std::string prefix, PabloAST * expr, const int outputIndex = -1) {
    78         // TODO: should this test whether we've somehow created a var for this prior to
    79         // making the assignment?
    80         Assign * assign = new Assign(mSymbolGenerator.get_ssa(prefix), expr, outputIndex, &mStatements);
    81         mStatements.push_back(assign);
     60    PabloAST * createCall(const std::string name);
     61
     62    Assign * createAssign(const std::string prefix, PabloAST * expr, const int outputIndex = -1)  {
     63        // Note: we cannot just use the findOrMake method to obtain this; an Assign node cannot be considered
     64        // unique until we prove it has no Next node associated with it. But the Assign node must be created
     65        // before the Next node. Should we create a "Constant" flag for this?
     66        Assign * assign = new Assign(mSymbolGenerator->get_ssa(prefix), expr, outputIndex, this);
     67        push_back(assign);
    8268        return assign;
    8369    }
     
    116102    PabloAST * createSel(PabloAST * condition, PabloAST * trueExpr, PabloAST * falseExpr);
    117103
    118     inline If * createIf(PabloAST * condition, std::vector<Assign *> && definedVars, PabloBlock && body) {
    119         If * statement = new If(condition, std::move(definedVars), std::move(body.statements()), &mStatements);
    120         mStatements.push_back(statement);
     104    inline If * createIf(PabloAST * condition, std::vector<Assign *> && definedVars, PabloBlock & body) {
     105        If * statement = new If(condition, std::move(definedVars), body, this);
     106        push_back(statement);
    121107        return statement;
    122108    }
    123109
    124     inline While * createWhile(PabloAST * cond, PabloBlock && body) {
    125         While * statement = new While(cond, std::move(body.statements()), &mStatements);
    126         mStatements.push_back(statement);
     110    inline While * createWhile(PabloAST * cond, PabloBlock & body) {
     111        While * statement = new While(cond, body, this);
     112        push_back(statement);
    127113        return statement;
    128114    }
     
    142128
    143129        template <class Type, typename... Params>
    144         inline Type * findOrMake(const PabloAST::ClassTypeId type, Args... args, Params... params) {
     130        inline std::pair<Type *, bool> findOrMake(const PabloAST::ClassTypeId type, Args... args, Params... params) {
    145131            Key key = std::make_tuple(type, args...);
    146132            PabloAST * const f = find(key);
    147133            if (f) {
    148                 return cast<Type>(f);
     134                return std::make_pair(cast<Type>(f), false);
    149135            }
    150136            Type * const expr = new Type(std::forward<Args>(args)..., std::forward<Params>(params)...);
    151137            insert(std::move(key), expr);
    152             return expr;
     138            return std::make_pair(expr, true);
    153139        }
    154140
    155141
    156142        template <class Functor, typename... Params>
    157         inline PabloAST * findOrCall(const PabloAST::ClassTypeId type, Args... args, Params... params) {
     143        inline std::pair<PabloAST *, bool> findOrCall(const PabloAST::ClassTypeId type, Args... args, Params... params) {
    158144            Key key = std::make_tuple(type, args...);
    159145            PabloAST * const f = find(key);
    160146            if (f) {
    161                 return f;
     147                return std::make_pair(f, false);
    162148            }
    163149            Functor mf(mCurrentBlock);
    164150            PabloAST * const expr = mf(std::forward<Args>(args)..., std::forward<Params>(params)...);
    165151            insert(std::move(key), expr);
    166             return expr;
     152            return std::make_pair(expr, true);
    167153        }
    168154
     
    196182    };
    197183
    198 
    199184    inline StatementList & statements() {
    200         return mStatements;
     185        return *this;
    201186    }
    202187
    203188    inline const StatementList & statements() const {
    204         return mStatements;
    205     }
    206 
     189        return *this;
     190    }
     191protected:
     192    PabloBlock()
     193    : mZeroes(new Zeroes())
     194    , mOnes(new Ones())
     195    , mSymbolGenerator(new SymbolGenerator())
     196    , mUnary(nullptr, this)
     197    , mUnaryWithInt(nullptr, this)
     198    , mBinary(nullptr, this)
     199    , mTernary(nullptr, this)
     200    {
     201
     202    }
     203
     204    PabloBlock(PabloBlock * predecessor)
     205    : mZeroes(predecessor->mZeroes) // inherit the original "Zeroes" variable for simplicity
     206    , mOnes(predecessor->mOnes) // inherit the original "Ones" variable for simplicity
     207    , mSymbolGenerator(predecessor->mSymbolGenerator)
     208    , mUnary(&(predecessor->mUnary), this)
     209    , mUnaryWithInt(&(predecessor->mUnaryWithInt), this)
     210    , mBinary(&(predecessor->mBinary), this)
     211    , mTernary(&(predecessor->mTernary), this)
     212    {
     213
     214    }
     215
     216    void* operator new (std::size_t size) noexcept {
     217        return PabloAST::mAllocator.allocate(size);
     218    }
    207219private:       
    208220    Zeroes * const                                      mZeroes;
    209221    Ones * const                                        mOnes;
    210     SymbolGenerator &                                   mSymbolGenerator;
     222    SymbolGenerator * const                             mSymbolGenerator;
    211223    ExpressionMap<PabloAST *>                           mUnary;
    212224    ExpressionMap<PabloAST *, int>                      mUnaryWithInt;
    213225    ExpressionMap<PabloAST *, PabloAST *>               mBinary;
    214226    ExpressionMap<PabloAST *, PabloAST *, PabloAST *>   mTernary;
    215     StatementList                                       mStatements;
    216227};
    217228
Note: See TracChangeset for help on using the changeset viewer.