Move muxpack from passes/techmap to passes/opt
[yosys.git] / kernel / rtlil.h
index e71a5fcebbd394cee56f1478fce648cd1aaee901..8509670ff4b2580361c8cb3a01555a718a01e441 100644 (file)
@@ -1,4 +1,4 @@
-/*
+/* -*- c++ -*-
  *  yosys -- Yosys Open SYnthesis Suite
  *
  *  Copyright (C) 2012  Clifford Wolf <clifford@clifford.at>
@@ -50,7 +50,7 @@ namespace RTLIL
                CONST_FLAG_NONE   = 0,
                CONST_FLAG_STRING = 1,
                CONST_FLAG_SIGNED = 2,  // only used for parameters
-               CONST_FLAG_REAL   = 4   // unused -- to be used for parameters
+               CONST_FLAG_REAL   = 4   // only used for parameters
        };
 
        struct Const;
@@ -76,6 +76,9 @@ namespace RTLIL
 
        struct IdString
        {
+               #undef YOSYS_XTRACE_GET_PUT
+               #undef YOSYS_SORT_ID_FREE_LIST
+
                // the global id string cache
 
                static struct destruct_guard_t {
@@ -89,9 +92,43 @@ namespace RTLIL
                static dict<char*, int, hash_cstr_ops> global_id_index_;
                static std::vector<int> global_free_idx_list_;
 
+               static int last_created_idx_ptr_;
+               static int last_created_idx_[8];
+
+               static inline void xtrace_db_dump()
+               {
+               #ifdef YOSYS_XTRACE_GET_PUT
+                       for (int idx = 0; idx < GetSize(global_id_storage_); idx++)
+                       {
+                               if (global_id_storage_.at(idx) == nullptr)
+                                       log("#X# DB-DUMP index %d: FREE\n", idx);
+                               else
+                                       log("#X# DB-DUMP index %d: '%s' (ref %d)\n", idx, global_id_storage_.at(idx), global_refcount_storage_.at(idx));
+                       }
+               #endif
+               }
+
+               static inline void checkpoint()
+               {
+                       last_created_idx_ptr_ = 0;
+                       for (int i = 0; i < 8; i++) {
+                               if (last_created_idx_[i])
+                                       put_reference(last_created_idx_[i]);
+                               last_created_idx_[i] = 0;
+                       }
+               #ifdef YOSYS_SORT_ID_FREE_LIST
+                       std::sort(global_free_idx_list_.begin(), global_free_idx_list_.end(), std::greater<int>());
+               #endif
+               }
+
                static inline int get_reference(int idx)
                {
                        global_refcount_storage_.at(idx)++;
+               #ifdef YOSYS_XTRACE_GET_PUT
+                       if (yosys_xtrace) {
+                               log("#X# GET-BY-INDEX '%s' (index %d, refcount %d)\n", global_id_storage_.at(idx), idx, global_refcount_storage_.at(idx));
+                       }
+               #endif
                        return idx;
                }
 
@@ -107,6 +144,11 @@ namespace RTLIL
                        auto it = global_id_index_.find((char*)p);
                        if (it != global_id_index_.end()) {
                                global_refcount_storage_.at(it->second)++;
+               #ifdef YOSYS_XTRACE_GET_PUT
+                               if (yosys_xtrace) {
+                                       log("#X# GET-BY-NAME '%s' (index %d, refcount %d)\n", global_id_storage_.at(it->second), it->second, global_refcount_storage_.at(it->second));
+                               }
+               #endif
                                return it->second;
                        }
 
@@ -124,16 +166,22 @@ namespace RTLIL
                        global_refcount_storage_.at(idx)++;
 
                        // Avoid Create->Delete->Create pattern
-                       static IdString last_created_id;
-                       put_reference(last_created_id.index_);
-                       last_created_id.index_ = idx;
-                       get_reference(last_created_id.index_);
+                       if (last_created_idx_[last_created_idx_ptr_])
+                               put_reference(last_created_idx_[last_created_idx_ptr_]);
+                       last_created_idx_[last_created_idx_ptr_] = idx;
+                       get_reference(last_created_idx_[last_created_idx_ptr_]);
+                       last_created_idx_ptr_ = (last_created_idx_ptr_ + 1) & 7;
 
                        if (yosys_xtrace) {
                                log("#X# New IdString '%s' with index %d.\n", p, idx);
                                log_backtrace("-X- ", yosys_xtrace-1);
                        }
 
+               #ifdef YOSYS_XTRACE_GET_PUT
+                       if (yosys_xtrace) {
+                               log("#X# GET-BY-NAME '%s' (index %d, refcount %d)\n", global_id_storage_.at(idx), idx, global_refcount_storage_.at(idx));
+                       }
+               #endif
                        return idx;
                }
 
@@ -144,6 +192,12 @@ namespace RTLIL
                        if (!destruct_guard.ok)
                                return;
 
+               #ifdef YOSYS_XTRACE_GET_PUT
+                       if (yosys_xtrace) {
+                               log("#X# PUT '%s' (index %d, refcount %d)\n", global_id_storage_.at(idx), idx, global_refcount_storage_.at(idx));
+                       }
+               #endif
+
                        log_assert(global_refcount_storage_.at(idx) > 0);
 
                        if (--global_refcount_storage_.at(idx) != 0)
@@ -463,6 +517,8 @@ struct RTLIL::Const
        Const(RTLIL::State bit, int width = 1);
        Const(const std::vector<RTLIL::State> &bits) : bits(bits) { flags = CONST_FLAG_NONE; }
        Const(const std::vector<bool> &bits);
+       Const(const RTLIL::Const &c);
+       RTLIL::Const &operator =(const RTLIL::Const &other) = default;
 
        bool operator <(const RTLIL::Const &other) const;
        bool operator ==(const RTLIL::Const &other) const;
@@ -492,6 +548,14 @@ struct RTLIL::Const
                return ret;
        }
 
+       void extu(int width) {
+               bits.resize(width, RTLIL::State::S0);
+       }
+
+       void exts(int width) {
+               bits.resize(width, bits.empty() ? RTLIL::State::Sx : bits.back());
+       }
+
        inline unsigned int hash() const {
                unsigned int h = mkhash_init;
                for (auto b : bits)
@@ -504,9 +568,13 @@ struct RTLIL::AttrObject
 {
        dict<RTLIL::IdString, RTLIL::Const> attributes;
 
-       void set_bool_attribute(RTLIL::IdString id);
+       void set_bool_attribute(RTLIL::IdString id, bool value=true);
        bool get_bool_attribute(RTLIL::IdString id) const;
 
+       bool get_blackbox_attribute(bool ignore_wb=false) const {
+               return get_bool_attribute("\\blackbox") || (!ignore_wb && get_bool_attribute("\\whitebox"));
+       }
+
        void set_strpool_attribute(RTLIL::IdString id, const pool<string> &data);
        void add_strpool_attribute(RTLIL::IdString id, const pool<string> &data);
        pool<string> get_strpool_attribute(RTLIL::IdString id) const;
@@ -529,8 +597,11 @@ struct RTLIL::SigChunk
        SigChunk(int val, int width = 32);
        SigChunk(RTLIL::State bit, int width = 1);
        SigChunk(RTLIL::SigBit bit);
+       SigChunk(const RTLIL::SigChunk &sigchunk);
+       RTLIL::SigChunk &operator =(const RTLIL::SigChunk &other) = default;
 
        RTLIL::SigChunk extract(int offset, int length) const;
+       inline int size() const { return width; }
 
        bool operator <(const RTLIL::SigChunk &other) const;
        bool operator ==(const RTLIL::SigChunk &other) const;
@@ -553,6 +624,8 @@ struct RTLIL::SigBit
        SigBit(const RTLIL::SigChunk &chunk);
        SigBit(const RTLIL::SigChunk &chunk, int index);
        SigBit(const RTLIL::SigSpec &sig);
+       SigBit(const RTLIL::SigBit &sigbit);
+       RTLIL::SigBit &operator =(const RTLIL::SigBit &other) = default;
 
        bool operator <(const RTLIL::SigBit &other) const;
        bool operator ==(const RTLIL::SigBit &other) const;
@@ -874,13 +947,13 @@ struct RTLIL::Design
                }
        }
 
-#ifdef WITH_PYTHON
-       static std::map<unsigned int, RTLIL::Design*> *get_all_designs(void);
-#endif
 
        std::vector<RTLIL::Module*> selected_modules() const;
        std::vector<RTLIL::Module*> selected_whole_modules() const;
        std::vector<RTLIL::Module*> selected_whole_modules_warn() const;
+#ifdef WITH_PYTHON
+       static std::map<unsigned int, RTLIL::Design*> *get_all_designs(void);
+#endif
 };
 
 struct RTLIL::Module : public RTLIL::AttrObject
@@ -911,11 +984,14 @@ public:
        Module();
        virtual ~Module();
        virtual RTLIL::IdString derive(RTLIL::Design *design, dict<RTLIL::IdString, RTLIL::Const> parameters, bool mayfail = false);
+       virtual RTLIL::IdString derive(RTLIL::Design *design, dict<RTLIL::IdString, RTLIL::Const> parameters, dict<RTLIL::IdString, RTLIL::Module*> interfaces, dict<RTLIL::IdString, RTLIL::IdString> modports, bool mayfail = false);
        virtual size_t count_id(RTLIL::IdString id);
+       virtual void reprocess_module(RTLIL::Design *design, dict<RTLIL::IdString, RTLIL::Module *> local_interfaces);
 
        virtual void sort();
        virtual void check();
        virtual void optimize();
+       virtual void makeblackbox();
 
        void connect(const RTLIL::SigSig &conn);
        void connect(const RTLIL::SigSpec &lhs, const RTLIL::SigSpec &rhs);
@@ -926,6 +1002,7 @@ public:
        void fixup_ports();
 
        template<typename T> void rewrite_sigspecs(T &functor);
+       template<typename T> void rewrite_sigspecs2(T &functor);
        void cloneInto(RTLIL::Module *new_mod) const;
        virtual RTLIL::Module *clone() const;
 
@@ -1175,6 +1252,10 @@ struct RTLIL::Memory : public RTLIL::AttrObject
 
        RTLIL::IdString name;
        int width, start_offset, size;
+#ifdef WITH_PYTHON
+       ~Memory();
+       static std::map<unsigned int, RTLIL::Memory*> *get_all_memorys(void);
+#endif
 };
 
 struct RTLIL::Cell : public RTLIL::AttrObject
@@ -1227,6 +1308,7 @@ public:
        }
 
        template<typename T> void rewrite_sigspecs(T &functor);
+       template<typename T> void rewrite_sigspecs2(T &functor);
 
 #ifdef WITH_PYTHON
        static std::map<unsigned int, RTLIL::Cell*> *get_all_cells(void);
@@ -1242,7 +1324,10 @@ struct RTLIL::CaseRule
        ~CaseRule();
        void optimize();
 
+       bool empty() const;
+
        template<typename T> void rewrite_sigspecs(T &functor);
+       template<typename T> void rewrite_sigspecs2(T &functor);
        RTLIL::CaseRule *clone() const;
 };
 
@@ -1253,7 +1338,10 @@ struct RTLIL::SwitchRule : public RTLIL::AttrObject
 
        ~SwitchRule();
 
+       bool empty() const;
+
        template<typename T> void rewrite_sigspecs(T &functor);
+       template<typename T> void rewrite_sigspecs2(T &functor);
        RTLIL::SwitchRule *clone() const;
 };
 
@@ -1264,6 +1352,7 @@ struct RTLIL::SyncRule
        std::vector<RTLIL::SigSig> actions;
 
        template<typename T> void rewrite_sigspecs(T &functor);
+       template<typename T> void rewrite_sigspecs2(T &functor);
        RTLIL::SyncRule *clone() const;
 };
 
@@ -1276,6 +1365,7 @@ struct RTLIL::Process : public RTLIL::AttrObject
        ~Process();
 
        template<typename T> void rewrite_sigspecs(T &functor);
+       template<typename T> void rewrite_sigspecs2(T &functor);
        RTLIL::Process *clone() const;
 };
 
@@ -1287,13 +1377,14 @@ inline RTLIL::SigBit::SigBit(RTLIL::Wire *wire) : wire(wire), offset(0) { log_as
 inline RTLIL::SigBit::SigBit(RTLIL::Wire *wire, int offset) : wire(wire), offset(offset) { log_assert(wire != nullptr); }
 inline RTLIL::SigBit::SigBit(const RTLIL::SigChunk &chunk) : wire(chunk.wire) { log_assert(chunk.width == 1); if (wire) offset = chunk.offset; else data = chunk.data[0]; }
 inline RTLIL::SigBit::SigBit(const RTLIL::SigChunk &chunk, int index) : wire(chunk.wire) { if (wire) offset = chunk.offset + index; else data = chunk.data[index]; }
+inline RTLIL::SigBit::SigBit(const RTLIL::SigBit &sigbit) : wire(sigbit.wire), data(sigbit.data){if(wire) offset = sigbit.offset;}
 
 inline bool RTLIL::SigBit::operator<(const RTLIL::SigBit &other) const {
        if (wire == other.wire)
                return wire ? (offset < other.offset) : (data < other.data);
        if (wire != nullptr && other.wire != nullptr)
                return wire->name < other.wire->name;
-       return wire < other.wire;
+       return (wire != nullptr) < (other.wire != nullptr);
 }
 
 inline bool RTLIL::SigBit::operator==(const RTLIL::SigBit &other) const {
@@ -1336,12 +1427,30 @@ void RTLIL::Module::rewrite_sigspecs(T &functor)
        }
 }
 
+template<typename T>
+void RTLIL::Module::rewrite_sigspecs2(T &functor)
+{
+       for (auto &it : cells_)
+               it.second->rewrite_sigspecs2(functor);
+       for (auto &it : processes)
+               it.second->rewrite_sigspecs2(functor);
+       for (auto &it : connections_) {
+               functor(it.first, it.second);
+       }
+}
+
 template<typename T>
 void RTLIL::Cell::rewrite_sigspecs(T &functor) {
        for (auto &it : connections_)
                functor(it.second);
 }
 
+template<typename T>
+void RTLIL::Cell::rewrite_sigspecs2(T &functor) {
+       for (auto &it : connections_)
+               functor(it.second);
+}
+
 template<typename T>
 void RTLIL::CaseRule::rewrite_sigspecs(T &functor) {
        for (auto &it : compare)
@@ -1354,6 +1463,17 @@ void RTLIL::CaseRule::rewrite_sigspecs(T &functor) {
                it->rewrite_sigspecs(functor);
 }
 
+template<typename T>
+void RTLIL::CaseRule::rewrite_sigspecs2(T &functor) {
+       for (auto &it : compare)
+               functor(it);
+       for (auto &it : actions) {
+               functor(it.first, it.second);
+       }
+       for (auto it : switches)
+               it->rewrite_sigspecs2(functor);
+}
+
 template<typename T>
 void RTLIL::SwitchRule::rewrite_sigspecs(T &functor)
 {
@@ -1362,6 +1482,14 @@ void RTLIL::SwitchRule::rewrite_sigspecs(T &functor)
                it->rewrite_sigspecs(functor);
 }
 
+template<typename T>
+void RTLIL::SwitchRule::rewrite_sigspecs2(T &functor)
+{
+       functor(signal);
+       for (auto it : cases)
+               it->rewrite_sigspecs2(functor);
+}
+
 template<typename T>
 void RTLIL::SyncRule::rewrite_sigspecs(T &functor)
 {
@@ -1372,6 +1500,15 @@ void RTLIL::SyncRule::rewrite_sigspecs(T &functor)
        }
 }
 
+template<typename T>
+void RTLIL::SyncRule::rewrite_sigspecs2(T &functor)
+{
+       functor(signal);
+       for (auto &it : actions) {
+               functor(it.first, it.second);
+       }
+}
+
 template<typename T>
 void RTLIL::Process::rewrite_sigspecs(T &functor)
 {
@@ -1380,6 +1517,14 @@ void RTLIL::Process::rewrite_sigspecs(T &functor)
                it->rewrite_sigspecs(functor);
 }
 
+template<typename T>
+void RTLIL::Process::rewrite_sigspecs2(T &functor)
+{
+       root_case.rewrite_sigspecs2(functor);
+       for (auto it : syncs)
+               it->rewrite_sigspecs2(functor);
+}
+
 YOSYS_NAMESPACE_END
 
 #endif