Renamed extend() to extend_xx(), changed most users to extend_u0()
[yosys.git] / kernel / rtlil.cc
index 35cd54b46f1b8ff909fcad759bde205921918e52..0e8078df6db12298bc9fc31b98415429f03e41d0 100644 (file)
@@ -18,6 +18,7 @@
  */
 
 #include "kernel/yosys.h"
+#include "kernel/macc.h"
 #include "frontends/verilog/verilog_frontend.h"
 #include "backends/ilang/ilang_backend.h"
 
@@ -26,6 +27,7 @@
 
 YOSYS_NAMESPACE_BEGIN
 
+RTLIL::IdString::destruct_guard_t RTLIL::IdString::destruct_guard;
 std::vector<int> RTLIL::IdString::global_refcount_storage_;
 std::vector<char*> RTLIL::IdString::global_id_storage_;
 std::map<char*, int, RTLIL::IdString::char_ptr_cmp> RTLIL::IdString::global_id_index_;
@@ -64,6 +66,13 @@ RTLIL::Const::Const(RTLIL::State bit, int width)
                bits.push_back(bit);
 }
 
+RTLIL::Const::Const(const std::vector<bool> &bits)
+{
+       flags = RTLIL::CONST_FLAG_NONE;
+       for (auto b : bits)
+               this->bits.push_back(b ? RTLIL::S1 : RTLIL::S0);
+}
+
 bool RTLIL::Const::operator <(const RTLIL::Const &other) const
 {
        if (bits.size() != other.bits.size())
@@ -432,7 +441,7 @@ std::vector<RTLIL::Module*> RTLIL::Design::selected_whole_modules_warn() const
                if (selected_whole_module(it.first))
                        result.push_back(it.second);
                else if (selected_module(it.first))
-                       log("Warning: Ignoring partially selected module %s.\n", log_id(it.first));
+                       log_warning("Ignoring partially selected module %s.\n", log_id(it.first));
        return result;
 }
 
@@ -563,7 +572,7 @@ namespace {
                                        cell->type.substr(0, 9) == "$verific$" || cell->type.substr(0, 7) == "$array:" || cell->type.substr(0, 8) == "$extern:")
                                return;
 
-                       if (cell->type.in("$not", "$pos", "$bu0", "$neg")) {
+                       if (cell->type.in("$not", "$pos", "$neg")) {
                                param_bool("\\A_SIGNED");
                                port("\\A", param("\\A_WIDTH"));
                                port("\\Y", param("\\Y_WIDTH"));
@@ -619,6 +628,25 @@ namespace {
                                return;
                        }
 
+                       if (cell->type == "$fa") {
+                               port("\\A", param("\\WIDTH"));
+                               port("\\B", param("\\WIDTH"));
+                               port("\\C", param("\\WIDTH"));
+                               port("\\X", param("\\WIDTH"));
+                               port("\\Y", param("\\WIDTH"));
+                               check_expected();
+                               return;
+                       }
+
+                       if (cell->type == "$lcu") {
+                               port("\\P", param("\\WIDTH"));
+                               port("\\G", param("\\WIDTH"));
+                               port("\\CI", 1);
+                               port("\\CO", param("\\WIDTH"));
+                               check_expected();
+                               return;
+                       }
+
                        if (cell->type == "$alu") {
                                param_bool("\\A_SIGNED");
                                param_bool("\\B_SIGNED");
@@ -633,6 +661,17 @@ namespace {
                                return;
                        }
 
+                       if (cell->type == "$macc") {
+                               param("\\CONFIG");
+                               param("\\CONFIG_WIDTH");
+                               port("\\A", param("\\A_WIDTH"));
+                               port("\\B", param("\\B_WIDTH"));
+                               port("\\Y", param("\\Y_WIDTH"));
+                               check_expected();
+                               Macc().from_cell(cell);
+                               return;
+                       }
+
                        if (cell->type == "$logic_not") {
                                param_bool("\\A_SIGNED");
                                port("\\A", param("\\A_WIDTH"));
@@ -714,6 +753,17 @@ namespace {
                                return;
                        }
 
+                       if (cell->type == "$dffe") {
+                               param_bool("\\CLK_POLARITY");
+                               param_bool("\\EN_POLARITY");
+                               port("\\CLK", 1);
+                               port("\\EN", 1);
+                               port("\\D", param("\\WIDTH"));
+                               port("\\Q", param("\\WIDTH"));
+                               check_expected();
+                               return;
+                       }
+
                        if (cell->type == "$dffsr") {
                                param_bool("\\CLK_POLARITY");
                                param_bool("\\SET_POLARITY");
@@ -832,6 +882,7 @@ namespace {
                                return;
                        }
 
+                       if (cell->type == "$_BUF_")  { check_gate("AY"); return; }
                        if (cell->type == "$_NOT_")  { check_gate("AY"); return; }
                        if (cell->type == "$_AND_")  { check_gate("ABY"); return; }
                        if (cell->type == "$_NAND_") { check_gate("ABY"); return; }
@@ -853,6 +904,11 @@ namespace {
                        if (cell->type == "$_DFF_N_") { check_gate("DQC"); return; }
                        if (cell->type == "$_DFF_P_") { check_gate("DQC"); return; }
 
+                       if (cell->type == "$_DFFE_NN_") { check_gate("DQCE"); return; }
+                       if (cell->type == "$_DFFE_NP_") { check_gate("DQCE"); return; }
+                       if (cell->type == "$_DFFE_PN_") { check_gate("DQCE"); return; }
+                       if (cell->type == "$_DFFE_PP_") { check_gate("DQCE"); return; }
+
                        if (cell->type == "$_DFF_NN0_") { check_gate("DQCR"); return; }
                        if (cell->type == "$_DFF_NN1_") { check_gate("DQCR"); return; }
                        if (cell->type == "$_DFF_NP0_") { check_gate("DQCR"); return; }
@@ -902,10 +958,10 @@ void RTLIL::Module::check()
                for (auto &it2 : it.second->attributes)
                        log_assert(!it2.first.empty());
                if (it.second->port_id) {
-                       log_assert(SIZE(ports) >= it.second->port_id);
+                       log_assert(GetSize(ports) >= it.second->port_id);
                        log_assert(ports.at(it.second->port_id-1) == it.first);
                        log_assert(it.second->port_input || it.second->port_output);
-                       if (SIZE(ports_declared) < it.second->port_id)
+                       if (GetSize(ports_declared) < it.second->port_id)
                                ports_declared.resize(it.second->port_id);
                        log_assert(ports_declared[it.second->port_id-1] == false);
                        ports_declared[it.second->port_id-1] = true;
@@ -914,7 +970,7 @@ void RTLIL::Module::check()
        }
        for (auto port_declared : ports_declared)
                log_assert(port_declared == true);
-       log_assert(SIZE(ports) == SIZE(ports_declared));
+       log_assert(GetSize(ports) == GetSize(ports_declared));
 
        for (auto &it : memories) {
                log_assert(it.first == it.second->name);
@@ -1023,14 +1079,14 @@ bool RTLIL::Module::has_processes() const
 bool RTLIL::Module::has_memories_warn() const
 {
        if (!memories.empty())
-               log("Warning: Ignoring module %s because it contains memories (run 'memory' command first).\n", log_id(this));
+               log_warning("Ignoring module %s because it contains memories (run 'memory' command first).\n", log_id(this));
        return !memories.empty();
 }
 
 bool RTLIL::Module::has_processes_warn() const
 {
        if (!processes.empty())
-               log("Warning: Ignoring module %s because it contains processes (run 'proc' command first).\n", log_id(this));
+               log_warning("Ignoring module %s because it contains processes (run 'proc' command first).\n", log_id(this));
        return !processes.empty();
 }
 
@@ -1117,6 +1173,9 @@ void RTLIL::Module::remove(const std::set<RTLIL::Wire*> &wires)
 
 void RTLIL::Module::remove(RTLIL::Cell *cell)
 {
+       while (!cell->connections_.empty())
+               cell->unsetPort(cell->connections_.begin()->first);
+
        log_assert(cells_.count(cell->name) != 0);
        log_assert(refcount_cells_ == 0);
        cells_.erase(cell->name);
@@ -1326,7 +1385,6 @@ RTLIL::Cell *RTLIL::Module::addCell(RTLIL::IdString name, const RTLIL::Cell *oth
        }
 DEF_METHOD(Not,        sig_a.size(), "$not")
 DEF_METHOD(Pos,        sig_a.size(), "$pos")
-DEF_METHOD(Bu0,        sig_a.size(), "$bu0")
 DEF_METHOD(Neg,        sig_a.size(), "$neg")
 DEF_METHOD(ReduceAnd,  1, "$reduce_and")
 DEF_METHOD(ReduceOr,   1, "$reduce_or")
@@ -1538,7 +1596,7 @@ RTLIL::Cell* RTLIL::Module::addSr(RTLIL::IdString name, RTLIL::SigSpec sig_set,
        return cell;
 }
 
-RTLIL::Cell* RTLIL::Module::addDff(RTLIL::IdString name, RTLIL::SigSpec sig_clk, RTLIL::SigSpec sig_d,   RTLIL::SigSpec sig_q, bool clk_polarity)
+RTLIL::Cell* RTLIL::Module::addDff(RTLIL::IdString name, RTLIL::SigSpec sig_clk, RTLIL::SigSpec sig_d, RTLIL::SigSpec sig_q, bool clk_polarity)
 {
        RTLIL::Cell *cell = addCell(name, "$dff");
        cell->parameters["\\CLK_POLARITY"] = clk_polarity;
@@ -1549,6 +1607,19 @@ RTLIL::Cell* RTLIL::Module::addDff(RTLIL::IdString name, RTLIL::SigSpec sig_clk,
        return cell;
 }
 
+RTLIL::Cell* RTLIL::Module::addDffe(RTLIL::IdString name, RTLIL::SigSpec sig_clk, RTLIL::SigSpec sig_en, RTLIL::SigSpec sig_d, RTLIL::SigSpec sig_q, bool clk_polarity, bool en_polarity)
+{
+       RTLIL::Cell *cell = addCell(name, "$dffe");
+       cell->parameters["\\CLK_POLARITY"] = clk_polarity;
+       cell->parameters["\\EN_POLARITY"] = en_polarity;
+       cell->parameters["\\WIDTH"] = sig_q.size();
+       cell->setPort("\\CLK", sig_clk);
+       cell->setPort("\\EN", sig_en);
+       cell->setPort("\\D", sig_d);
+       cell->setPort("\\Q", sig_q);
+       return cell;
+}
+
 RTLIL::Cell* RTLIL::Module::addDffsr(RTLIL::IdString name, RTLIL::SigSpec sig_clk, RTLIL::SigSpec sig_set, RTLIL::SigSpec sig_clr,
                RTLIL::SigSpec sig_d, RTLIL::SigSpec sig_q, bool clk_polarity, bool set_polarity, bool clr_polarity)
 {
@@ -1616,6 +1687,16 @@ RTLIL::Cell* RTLIL::Module::addDffGate(RTLIL::IdString name, RTLIL::SigSpec sig_
        return cell;
 }
 
+RTLIL::Cell* RTLIL::Module::addDffeGate(RTLIL::IdString name, RTLIL::SigSpec sig_clk, RTLIL::SigSpec sig_en, RTLIL::SigSpec sig_d, RTLIL::SigSpec sig_q, bool clk_polarity, bool en_polarity)
+{
+       RTLIL::Cell *cell = addCell(name, stringf("$_DFFE_%c%c_", clk_polarity ? 'P' : 'N', en_polarity ? 'P' : 'N'));
+       cell->setPort("\\C", sig_clk);
+       cell->setPort("\\E", sig_en);
+       cell->setPort("\\D", sig_d);
+       cell->setPort("\\Q", sig_q);
+       return cell;
+}
+
 RTLIL::Cell* RTLIL::Module::addDffsrGate(RTLIL::IdString name, RTLIL::SigSpec sig_clk, RTLIL::SigSpec sig_set, RTLIL::SigSpec sig_clr,
                RTLIL::SigSpec sig_d, RTLIL::SigSpec sig_q, bool clk_polarity, bool set_polarity, bool clr_polarity)
 {
@@ -1737,7 +1818,7 @@ const std::map<RTLIL::IdString, RTLIL::SigSpec> &RTLIL::Cell::connections() cons
 
 bool RTLIL::Cell::hasParam(RTLIL::IdString paramname) const
 {
-       return parameters.count(paramname);
+       return parameters.count(paramname) != 0;
 }
 
 void RTLIL::Cell::unsetParam(RTLIL::IdString paramname)
@@ -1770,19 +1851,29 @@ void RTLIL::Cell::fixup_parameters(bool set_a_signed, bool set_b_signed)
                return;
 
        if (type == "$mux" || type == "$pmux") {
-               parameters["\\WIDTH"] = SIZE(connections_["\\Y"]);
+               parameters["\\WIDTH"] = GetSize(connections_["\\Y"]);
                if (type == "$pmux")
-                       parameters["\\S_WIDTH"] = SIZE(connections_["\\S"]);
+                       parameters["\\S_WIDTH"] = GetSize(connections_["\\S"]);
                check();
                return;
        }
 
        if (type == "$lut") {
-               parameters["\\WIDTH"] = SIZE(connections_["\\A"]);
+               parameters["\\WIDTH"] = GetSize(connections_["\\A"]);
+               return;
+       }
+
+       if (type == "$fa") {
+               parameters["\\WIDTH"] = GetSize(connections_["\\Y"]);
+               return;
+       }
+
+       if (type == "$lcu") {
+               parameters["\\WIDTH"] = GetSize(connections_["\\CO"]);
                return;
        }
 
-       bool signedness_ab = !type.in("$slice", "$concat");
+       bool signedness_ab = !type.in("$slice", "$concat", "$macc");
 
        if (connections_.count("\\A")) {
                if (signedness_ab) {
@@ -1791,7 +1882,7 @@ void RTLIL::Cell::fixup_parameters(bool set_a_signed, bool set_b_signed)
                        else if (parameters.count("\\A_SIGNED") == 0)
                                parameters["\\A_SIGNED"] = false;
                }
-               parameters["\\A_WIDTH"] = SIZE(connections_["\\A"]);
+               parameters["\\A_WIDTH"] = GetSize(connections_["\\A"]);
        }
 
        if (connections_.count("\\B")) {
@@ -1801,11 +1892,11 @@ void RTLIL::Cell::fixup_parameters(bool set_a_signed, bool set_b_signed)
                        else if (parameters.count("\\B_SIGNED") == 0)
                                parameters["\\B_SIGNED"] = false;
                }
-               parameters["\\B_WIDTH"] = SIZE(connections_["\\B"]);
+               parameters["\\B_WIDTH"] = GetSize(connections_["\\B"]);
        }
 
        if (connections_.count("\\Y"))
-               parameters["\\Y_WIDTH"] = SIZE(connections_["\\Y"]);
+               parameters["\\Y_WIDTH"] = GetSize(connections_["\\Y"]);
 
        check();
 }
@@ -1821,7 +1912,7 @@ RTLIL::SigChunk::SigChunk(const RTLIL::Const &value)
 {
        wire = NULL;
        data = value.bits;
-       width = SIZE(data);
+       width = GetSize(data);
        offset = 0;
 }
 
@@ -1845,7 +1936,7 @@ RTLIL::SigChunk::SigChunk(const std::string &str)
 {
        wire = NULL;
        data = RTLIL::Const(str).bits;
-       width = SIZE(data);
+       width = GetSize(data);
        offset = 0;
 }
 
@@ -1853,7 +1944,7 @@ RTLIL::SigChunk::SigChunk(int val, int width)
 {
        wire = NULL;
        data = RTLIL::Const(val, width).bits;
-       this->width = SIZE(data);
+       this->width = GetSize(data);
        offset = 0;
 }
 
@@ -1861,7 +1952,7 @@ RTLIL::SigChunk::SigChunk(RTLIL::State bit, int width)
 {
        wire = NULL;
        data = RTLIL::Const(bit, width).bits;
-       this->width = SIZE(data);
+       this->width = GetSize(data);
        offset = 0;
 }
 
@@ -2098,6 +2189,16 @@ RTLIL::SigSpec::SigSpec(std::set<RTLIL::SigBit> bits)
        check();
 }
 
+RTLIL::SigSpec::SigSpec(bool bit)
+{
+       cover("kernel.rtlil.sigspec.init.bool");
+
+       width_ = 0;
+       hash_ = 0;
+       append_bit(bit);
+       check();
+}
+
 void RTLIL::SigSpec::pack() const
 {
        RTLIL::SigSpec *that = (RTLIL::SigSpec*)this;
@@ -2153,7 +2254,7 @@ void RTLIL::SigSpec::unpack() const
        that->hash_ = 0;
 }
 
-#define DJB2(_hash, _value) do { (_hash) = (((_hash) << 5) + (_hash)) + (_value); } while (0)
+#define DJB2(_hash, _value) (_hash) = (((_hash) << 5) + (_hash)) + (_value)
 
 void RTLIL::SigSpec::hash() const
 {
@@ -2207,7 +2308,7 @@ void RTLIL::SigSpec::replace(const RTLIL::SigSpec &pattern, const RTLIL::SigSpec
 
        std::map<RTLIL::SigBit, RTLIL::SigBit> rules;
 
-       for (int i = 0; i < SIZE(pattern.bits_); i++)
+       for (int i = 0; i < GetSize(pattern.bits_); i++)
                if (pattern.bits_[i].wire != NULL)
                        rules[pattern.bits_[i]] = with.bits_[i];
 
@@ -2229,7 +2330,7 @@ void RTLIL::SigSpec::replace(const std::map<RTLIL::SigBit, RTLIL::SigBit> &rules
        unpack();
        other->unpack();
 
-       for (int i = 0; i < SIZE(bits_); i++) {
+       for (int i = 0; i < GetSize(bits_); i++) {
                auto it = rules.find(bits_[i]);
                if (it != rules.end())
                        other->bits_[i] = it->second;
@@ -2282,12 +2383,12 @@ void RTLIL::SigSpec::remove2(const std::set<RTLIL::SigBit> &pattern, RTLIL::SigS
 
        std::vector<RTLIL::SigBit> new_bits, new_other_bits;
 
-       new_bits.resize(SIZE(bits_));
+       new_bits.resize(GetSize(bits_));
        if (other != NULL)
-               new_other_bits.resize(SIZE(bits_));
+               new_other_bits.resize(GetSize(bits_));
 
        int k = 0;
-       for (int i = 0; i < SIZE(bits_); i++) {
+       for (int i = 0; i < GetSize(bits_); i++) {
                if (bits_[i].wire != NULL && pattern.count(bits_[i]))
                        continue;
                if (other != NULL)
@@ -2300,11 +2401,11 @@ void RTLIL::SigSpec::remove2(const std::set<RTLIL::SigBit> &pattern, RTLIL::SigS
                new_other_bits.resize(k);
 
        bits_.swap(new_bits);
-       width_ = SIZE(bits_);
+       width_ = GetSize(bits_);
 
        if (other != NULL) {
                other->bits_.swap(new_other_bits);
-               other->width_ = SIZE(other->bits_);
+               other->width_ = GetSize(other->bits_);
        }
 
        check();
@@ -2367,7 +2468,7 @@ void RTLIL::SigSpec::remove_const()
                cover("kernel.rtlil.sigspec.remove_const.packed");
 
                std::vector<RTLIL::SigChunk> new_chunks;
-               new_chunks.reserve(SIZE(chunks_));
+               new_chunks.reserve(GetSize(chunks_));
 
                width_ = 0;
                for (auto &chunk : chunks_)
@@ -2489,9 +2590,9 @@ void RTLIL::SigSpec::append_bit(const RTLIL::SigBit &bit)
        check();
 }
 
-void RTLIL::SigSpec::extend(int width, bool is_signed)
+void RTLIL::SigSpec::extend_xx(int width, bool is_signed)
 {
-       cover("kernel.rtlil.sigspec.extend");
+       cover("kernel.rtlil.sigspec.extend_xx");
 
        pack();
 
@@ -2499,10 +2600,9 @@ void RTLIL::SigSpec::extend(int width, bool is_signed)
                remove(width, width_ - width);
        
        if (width_ < width) {
-               RTLIL::SigSpec padding = width_ > 0 ? extract(width_ - 1, 1) : RTLIL::SigSpec(RTLIL::State::S0);
-               if (!is_signed && padding != RTLIL::SigSpec(RTLIL::State::Sx) && padding != RTLIL::SigSpec(RTLIL::State::Sz) &&
-                               padding != RTLIL::SigSpec(RTLIL::State::Sa) && padding != RTLIL::SigSpec(RTLIL::State::Sm))
-                       padding = RTLIL::SigSpec(RTLIL::State::S0);
+               RTLIL::SigBit padding = width_ > 0 ? (*this)[width_ - 1] : RTLIL::State::S0;
+               if (!is_signed && (padding == RTLIL::State::S1 || padding.wire))
+                       padding = RTLIL::State::S0;
                while (width_ < width)
                        append(padding);
        }
@@ -2518,9 +2618,9 @@ void RTLIL::SigSpec::extend_u0(int width, bool is_signed)
                remove(width, width_ - width);
        
        if (width_ < width) {
-               RTLIL::SigSpec padding = width_ > 0 ? extract(width_ - 1, 1) : RTLIL::SigSpec(RTLIL::State::S0);
+               RTLIL::SigBit padding = width_ > 0 ? (*this)[width_ - 1] : RTLIL::State::S0;
                if (!is_signed)
-                       padding = RTLIL::SigSpec(RTLIL::State::S0);
+                       padding = RTLIL::State::S0;
                while (width_ < width)
                        append(padding);
        }
@@ -2573,7 +2673,7 @@ void RTLIL::SigSpec::check() const
        {
                cover("kernel.rtlil.sigspec.check.unpacked");
 
-               log_assert(width_ == SIZE(bits_));
+               log_assert(width_ == GetSize(bits_));
                log_assert(chunks_.empty());
        }
 }
@@ -2648,7 +2748,7 @@ bool RTLIL::SigSpec::is_wire() const
        cover("kernel.rtlil.sigspec.is_wire");
 
        pack();
-       return SIZE(chunks_) == 1 && chunks_[0].wire && chunks_[0].wire->width == width_;
+       return GetSize(chunks_) == 1 && chunks_[0].wire && chunks_[0].wire->width == width_;
 }
 
 bool RTLIL::SigSpec::is_chunk() const
@@ -2656,7 +2756,7 @@ bool RTLIL::SigSpec::is_chunk() const
        cover("kernel.rtlil.sigspec.is_chunk");
 
        pack();
-       return SIZE(chunks_) == 1;
+       return GetSize(chunks_) == 1;
 }
 
 bool RTLIL::SigSpec::is_fully_const() const
@@ -2719,7 +2819,7 @@ bool RTLIL::SigSpec::as_bool() const
        cover("kernel.rtlil.sigspec.as_bool");
 
        pack();
-       log_assert(is_fully_const() && SIZE(chunks_) <= 1);
+       log_assert(is_fully_const() && GetSize(chunks_) <= 1);
        if (width_)
                return RTLIL::Const(chunks_[0].data).as_bool();
        return false;
@@ -2730,7 +2830,7 @@ int RTLIL::SigSpec::as_int(bool is_signed) const
        cover("kernel.rtlil.sigspec.as_int");
 
        pack();
-       log_assert(is_fully_const() && SIZE(chunks_) <= 1);
+       log_assert(is_fully_const() && GetSize(chunks_) <= 1);
        if (width_)
                return RTLIL::Const(chunks_[0].data).as_int(is_signed);
        return 0;
@@ -2758,7 +2858,7 @@ RTLIL::Const RTLIL::SigSpec::as_const() const
        cover("kernel.rtlil.sigspec.as_const");
 
        pack();
-       log_assert(is_fully_const() && SIZE(chunks_) <= 1);
+       log_assert(is_fully_const() && GetSize(chunks_) <= 1);
        if (width_)
                return chunks_[0].data;
        return RTLIL::Const();
@@ -2884,7 +2984,7 @@ bool RTLIL::SigSpec::parse(RTLIL::SigSpec &sig, RTLIL::Module *module, std::stri
                if (netname.size() == 0)
                        continue;
 
-               if ('0' <= netname[0] && netname[0] <= '9') {
+               if (('0' <= netname[0] && netname[0] <= '9') || netname[0] == '\'') {
                        cover("kernel.rtlil.sigspec.parse.const");
                        AST::get_line_num = sigspec_parse_get_dummy_line_num;
                        AST::AstNode *ast = VERILOG_FRONTEND::const2ast(netname);
@@ -2929,7 +3029,10 @@ bool RTLIL::SigSpec::parse(RTLIL::SigSpec &sig, RTLIL::Module *module, std::stri
                        sigspec_parse_split(index_tokens, indices.substr(1, indices.size()-2), ':');
                        if (index_tokens.size() == 1) {
                                cover("kernel.rtlil.sigspec.parse.bit_sel");
-                               sig.append(RTLIL::SigSpec(wire, atoi(index_tokens.at(0).c_str())));
+                               int a = atoi(index_tokens.at(0).c_str());
+                               if (a < 0 || a >= wire->width)
+                                       return false;
+                               sig.append(RTLIL::SigSpec(wire, a));
                        } else {
                                cover("kernel.rtlil.sigspec.parse.part_sel");
                                int a = atoi(index_tokens.at(0).c_str());
@@ -2938,6 +3041,10 @@ bool RTLIL::SigSpec::parse(RTLIL::SigSpec &sig, RTLIL::Module *module, std::stri
                                        int tmp = a;
                                        a = b, b = tmp;
                                }
+                               if (a < 0 || a >= wire->width)
+                                       return false;
+                               if (b < 0 || b >= wire->width)
+                                       return false;
                                sig.append(RTLIL::SigSpec(wire, a, b-a+1));
                        }
                } else
@@ -2983,7 +3090,7 @@ bool RTLIL::SigSpec::parse_rhs(const RTLIL::SigSpec &lhs, RTLIL::SigSpec &sig, R
 
        if (lhs.chunks_.size() == 1) {
                char *p = (char*)str.c_str(), *endptr;
-               long long int val = strtoll(p, &endptr, 10);
+               long int val = strtol(p, &endptr, 10);
                if (endptr && endptr != p && *endptr == 0) {
                        sig = RTLIL::SigSpec(val, lhs.width_);
                        cover("kernel.rtlil.sigspec.parse.rhs_dec");