yosys-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
rtlil.cc
Go to the documentation of this file.
1 /*
2  * yosys -- Yosys Open SYnthesis Suite
3  *
4  * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
5  *
6  * Permission to use, copy, modify, and/or distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  *
18  */
19 
20 #include "kernel/yosys.h"
21 #include "kernel/macc.h"
24 
25 #include <string.h>
26 #include <algorithm>
27 
29 
32 std::vector<char*> RTLIL::IdString::global_id_storage_;
33 std::map<char*, int, RTLIL::IdString::char_ptr_cmp> RTLIL::IdString::global_id_index_;
35 
37 {
39 }
40 
41 RTLIL::Const::Const(std::string str)
42 {
44  for (int i = str.size()-1; i >= 0; i--) {
45  unsigned char ch = str[i];
46  for (int j = 0; j < 8; j++) {
47  bits.push_back((ch & 1) != 0 ? RTLIL::S1 : RTLIL::S0);
48  ch = ch >> 1;
49  }
50  }
51 }
52 
53 RTLIL::Const::Const(int val, int width)
54 {
55  flags = RTLIL::CONST_FLAG_NONE;
56  for (int i = 0; i < width; i++) {
57  bits.push_back((val & 1) != 0 ? RTLIL::S1 : RTLIL::S0);
58  val = val >> 1;
59  }
60 }
61 
63 {
64  flags = RTLIL::CONST_FLAG_NONE;
65  for (int i = 0; i < width; i++)
66  bits.push_back(bit);
67 }
68 
69 RTLIL::Const::Const(const std::vector<bool> &bits)
70 {
71  flags = RTLIL::CONST_FLAG_NONE;
72  for (auto b : bits)
73  this->bits.push_back(b ? RTLIL::S1 : RTLIL::S0);
74 }
75 
76 bool RTLIL::Const::operator <(const RTLIL::Const &other) const
77 {
78  if (bits.size() != other.bits.size())
79  return bits.size() < other.bits.size();
80  for (size_t i = 0; i < bits.size(); i++)
81  if (bits[i] != other.bits[i])
82  return bits[i] < other.bits[i];
83  return false;
84 }
85 
86 bool RTLIL::Const::operator ==(const RTLIL::Const &other) const
87 {
88  return bits == other.bits;
89 }
90 
91 bool RTLIL::Const::operator !=(const RTLIL::Const &other) const
92 {
93  return bits != other.bits;
94 }
95 
97 {
98  for (size_t i = 0; i < bits.size(); i++)
99  if (bits[i] == RTLIL::S1)
100  return true;
101  return false;
102 }
103 
104 int RTLIL::Const::as_int(bool is_signed) const
105 {
106  int32_t ret = 0;
107  for (size_t i = 0; i < bits.size() && i < 32; i++)
108  if (bits[i] == RTLIL::S1)
109  ret |= 1 << i;
110  if (is_signed && bits.back() == RTLIL::S1)
111  for (size_t i = bits.size(); i < 32; i++)
112  ret |= 1 << i;
113  return ret;
114 }
115 
116 std::string RTLIL::Const::as_string() const
117 {
118  std::string ret;
119  for (size_t i = bits.size(); i > 0; i--)
120  switch (bits[i-1]) {
121  case S0: ret += "0"; break;
122  case S1: ret += "1"; break;
123  case Sx: ret += "x"; break;
124  case Sz: ret += "z"; break;
125  case Sa: ret += "-"; break;
126  case Sm: ret += "m"; break;
127  }
128  return ret;
129 }
130 
131 std::string RTLIL::Const::decode_string() const
132 {
133  std::string string;
134  std::vector <char> string_chars;
135  for (int i = 0; i < int (bits.size()); i += 8) {
136  char ch = 0;
137  for (int j = 0; j < 8 && i + j < int (bits.size()); j++)
138  if (bits[i + j] == RTLIL::State::S1)
139  ch |= 1 << j;
140  if (ch != 0)
141  string_chars.push_back(ch);
142  }
143  for (int i = int (string_chars.size()) - 1; i >= 0; i--)
144  string += string_chars[i];
145  return string;
146 }
147 
149 {
150  if (full_selection)
151  return true;
152  if (selected_modules.count(mod_name) > 0)
153  return true;
154  if (selected_members.count(mod_name) > 0)
155  return true;
156  return false;
157 }
158 
160 {
161  if (full_selection)
162  return true;
163  if (selected_modules.count(mod_name) > 0)
164  return true;
165  return false;
166 }
167 
169 {
170  if (full_selection)
171  return true;
172  if (selected_modules.count(mod_name) > 0)
173  return true;
174  if (selected_members.count(mod_name) > 0)
175  if (selected_members.at(mod_name).count(memb_name) > 0)
176  return true;
177  return false;
178 }
179 
181 {
182  if (full_selection) {
183  selected_modules.clear();
184  selected_members.clear();
185  return;
186  }
187 
188  std::vector<RTLIL::IdString> del_list, add_list;
189 
190  del_list.clear();
191  for (auto mod_name : selected_modules) {
192  if (design->modules_.count(mod_name) == 0)
193  del_list.push_back(mod_name);
194  selected_members.erase(mod_name);
195  }
196  for (auto mod_name : del_list)
197  selected_modules.erase(mod_name);
198 
199  del_list.clear();
200  for (auto &it : selected_members)
201  if (design->modules_.count(it.first) == 0)
202  del_list.push_back(it.first);
203  for (auto mod_name : del_list)
204  selected_members.erase(mod_name);
205 
206  for (auto &it : selected_members) {
207  del_list.clear();
208  for (auto memb_name : it.second)
209  if (design->modules_[it.first]->count_id(memb_name) == 0)
210  del_list.push_back(memb_name);
211  for (auto memb_name : del_list)
212  it.second.erase(memb_name);
213  }
214 
215  del_list.clear();
216  add_list.clear();
217  for (auto &it : selected_members)
218  if (it.second.size() == 0)
219  del_list.push_back(it.first);
220  else if (it.second.size() == design->modules_[it.first]->wires_.size() + design->modules_[it.first]->memories.size() +
221  design->modules_[it.first]->cells_.size() + design->modules_[it.first]->processes.size())
222  add_list.push_back(it.first);
223  for (auto mod_name : del_list)
224  selected_members.erase(mod_name);
225  for (auto mod_name : add_list) {
226  selected_members.erase(mod_name);
227  selected_modules.insert(mod_name);
228  }
229 
230  if (selected_modules.size() == design->modules_.size()) {
231  full_selection = true;
232  selected_modules.clear();
233  selected_members.clear();
234  }
235 }
236 
238 {
239  refcount_modules_ = 0;
240  selection_stack.push_back(RTLIL::Selection());
241 }
242 
244 {
245  for (auto it = modules_.begin(); it != modules_.end(); it++)
246  delete it->second;
247 }
248 
250 {
251  return RTLIL::ObjRange<RTLIL::Module*>(&modules_, &refcount_modules_);
252 }
253 
255 {
256  return modules_.count(name) ? modules_.at(name) : NULL;
257 }
258 
260 {
261  log_assert(modules_.count(module->name) == 0);
262  log_assert(refcount_modules_ == 0);
263  modules_[module->name] = module;
264  module->design = this;
265 
266  for (auto mon : monitors)
267  mon->notify_module_add(module);
268 }
269 
271 {
272  log_assert(modules_.count(name) == 0);
273  log_assert(refcount_modules_ == 0);
274 
276  modules_[name] = module;
277  module->design = this;
278  module->name = name;
279 
280  for (auto mon : monitors)
281  mon->notify_module_add(module);
282 
283  return module;
284 }
285 
286 void RTLIL::Design::scratchpad_unset(std::string varname)
287 {
288  scratchpad.erase(varname);
289 }
290 
291 void RTLIL::Design::scratchpad_set_int(std::string varname, int value)
292 {
293  scratchpad[varname] = stringf("%d", value);
294 }
295 
296 void RTLIL::Design::scratchpad_set_bool(std::string varname, bool value)
297 {
298  scratchpad[varname] = value ? "true" : "false";
299 }
300 
301 void RTLIL::Design::scratchpad_set_string(std::string varname, std::string value)
302 {
303  scratchpad[varname] = value;
304 }
305 
306 int RTLIL::Design::scratchpad_get_int(std::string varname, int default_value) const
307 {
308  if (scratchpad.count(varname) == 0)
309  return default_value;
310 
311  std::string str = scratchpad.at(varname);
312 
313  if (str == "0" || str == "false")
314  return 0;
315 
316  if (str == "1" || str == "true")
317  return 1;
318 
319  char *endptr = nullptr;
320  long int parsed_value = strtol(str.c_str(), &endptr, 10);
321  return *endptr ? default_value : parsed_value;
322 }
323 
324 bool RTLIL::Design::scratchpad_get_bool(std::string varname, bool default_value) const
325 {
326  if (scratchpad.count(varname) == 0)
327  return default_value;
328 
329  std::string str = scratchpad.at(varname);
330 
331  if (str == "0" || str == "false")
332  return false;
333 
334  if (str == "1" || str == "true")
335  return true;
336 
337  return default_value;
338 }
339 
340 std::string RTLIL::Design::scratchpad_get_string(std::string varname, std::string default_value) const
341 {
342  if (scratchpad.count(varname) == 0)
343  return default_value;
344  return scratchpad.at(varname);
345 }
346 
348 {
349  for (auto mon : monitors)
350  mon->notify_module_del(module);
351 
352  log_assert(modules_.at(module->name) == module);
353  modules_.erase(module->name);
354  delete module;
355 }
356 
358 {
359 #ifndef NDEBUG
360  for (auto &it : modules_) {
361  log_assert(this == it.second->design);
362  log_assert(it.first == it.second->name);
363  log_assert(!it.first.empty());
364  it.second->check();
365  }
366 #endif
367 }
368 
370 {
371  for (auto &it : modules_)
372  it.second->optimize();
373  for (auto &it : selection_stack)
374  it.optimize(this);
375  for (auto &it : selection_vars)
376  it.second.optimize(this);
377 }
378 
380 {
381  if (!selected_active_module.empty() && mod_name != selected_active_module)
382  return false;
383  if (selection_stack.size() == 0)
384  return true;
385  return selection_stack.back().selected_module(mod_name);
386 }
387 
389 {
390  if (!selected_active_module.empty() && mod_name != selected_active_module)
391  return false;
392  if (selection_stack.size() == 0)
393  return true;
394  return selection_stack.back().selected_whole_module(mod_name);
395 }
396 
398 {
399  if (!selected_active_module.empty() && mod_name != selected_active_module)
400  return false;
401  if (selection_stack.size() == 0)
402  return true;
403  return selection_stack.back().selected_member(mod_name, memb_name);
404 }
405 
407 {
408  return selected_module(mod->name);
409 }
410 
412 {
413  return selected_whole_module(mod->name);
414 }
415 
416 std::vector<RTLIL::Module*> RTLIL::Design::selected_modules() const
417 {
418  std::vector<RTLIL::Module*> result;
419  result.reserve(modules_.size());
420  for (auto &it : modules_)
421  if (selected_module(it.first))
422  result.push_back(it.second);
423  return result;
424 }
425 
426 std::vector<RTLIL::Module*> RTLIL::Design::selected_whole_modules() const
427 {
428  std::vector<RTLIL::Module*> result;
429  result.reserve(modules_.size());
430  for (auto &it : modules_)
431  if (selected_whole_module(it.first))
432  result.push_back(it.second);
433  return result;
434 }
435 
436 std::vector<RTLIL::Module*> RTLIL::Design::selected_whole_modules_warn() const
437 {
438  std::vector<RTLIL::Module*> result;
439  result.reserve(modules_.size());
440  for (auto &it : modules_)
441  if (selected_whole_module(it.first))
442  result.push_back(it.second);
443  else if (selected_module(it.first))
444  log_warning("Ignoring partially selected module %s.\n", log_id(it.first));
445  return result;
446 }
447 
449 {
450  design = nullptr;
451  refcount_wires_ = 0;
452  refcount_cells_ = 0;
453 }
454 
456 {
457  for (auto it = wires_.begin(); it != wires_.end(); it++)
458  delete it->second;
459  for (auto it = memories.begin(); it != memories.end(); it++)
460  delete it->second;
461  for (auto it = cells_.begin(); it != cells_.end(); it++)
462  delete it->second;
463  for (auto it = processes.begin(); it != processes.end(); it++)
464  delete it->second;
465 }
466 
467 RTLIL::IdString RTLIL::Module::derive(RTLIL::Design*, std::map<RTLIL::IdString, RTLIL::Const>)
468 {
469  log_error("Module `%s' is used with parameters but is not parametric!\n", id2cstr(name));
470 }
471 
473 {
474  return wires_.count(id) + memories.count(id) + cells_.count(id) + processes.count(id);
475 }
476 
477 #ifndef NDEBUG
478 namespace {
479  struct InternalCellChecker
480  {
482  RTLIL::Cell *cell;
483  std::set<RTLIL::IdString> expected_params, expected_ports;
484 
485  InternalCellChecker(RTLIL::Module *module, RTLIL::Cell *cell) : module(module), cell(cell) { }
486 
487  void error(int linenr)
488  {
489  std::stringstream buf;
490  ILANG_BACKEND::dump_cell(buf, " ", cell);
491 
492  log_error("Found error in internal cell %s%s%s (%s) at %s:%d:\n%s",
493  module ? module->name.c_str() : "", module ? "." : "",
494  cell->name.c_str(), cell->type.c_str(), __FILE__, linenr, buf.str().c_str());
495  }
496 
497  int param(const char *name)
498  {
499  if (cell->parameters.count(name) == 0)
500  error(__LINE__);
501  expected_params.insert(name);
502  return cell->parameters.at(name).as_int();
503  }
504 
505  int param_bool(const char *name)
506  {
507  int v = param(name);
508  if (cell->parameters.at(name).bits.size() > 32)
509  error(__LINE__);
510  if (v != 0 && v != 1)
511  error(__LINE__);
512  return v;
513  }
514 
515  void param_bits(const char *name, int width)
516  {
517  param(name);
518  if (int(cell->parameters.at(name).bits.size()) != width)
519  error(__LINE__);
520  }
521 
522  void port(const char *name, int width)
523  {
524  if (!cell->hasPort(name))
525  error(__LINE__);
526  if (cell->getPort(name).size() != width)
527  error(__LINE__);
528  expected_ports.insert(name);
529  }
530 
531  void check_expected(bool check_matched_sign = true)
532  {
533  for (auto &para : cell->parameters)
534  if (expected_params.count(para.first) == 0)
535  error(__LINE__);
536  for (auto &conn : cell->connections())
537  if (expected_ports.count(conn.first) == 0)
538  error(__LINE__);
539 
540  if (expected_params.count("\\A_SIGNED") != 0 && expected_params.count("\\B_SIGNED") && check_matched_sign) {
541  bool a_is_signed = param("\\A_SIGNED") != 0;
542  bool b_is_signed = param("\\B_SIGNED") != 0;
543  if (a_is_signed != b_is_signed)
544  error(__LINE__);
545  }
546  }
547 
548  void check_gate(const char *ports)
549  {
550  if (cell->parameters.size() != 0)
551  error(__LINE__);
552 
553  for (const char *p = ports; *p; p++) {
554  char portname[3] = { '\\', *p, 0 };
555  if (!cell->hasPort(portname))
556  error(__LINE__);
557  if (cell->getPort(portname).size() != 1)
558  error(__LINE__);
559  }
560 
561  for (auto &conn : cell->connections()) {
562  if (conn.first.size() != 2 || conn.first[0] != '\\')
563  error(__LINE__);
564  if (strchr(ports, conn.first[1]) == NULL)
565  error(__LINE__);
566  }
567  }
568 
569  void check()
570  {
571  if (cell->type.substr(0, 1) != "$" || cell->type.substr(0, 3) == "$__" || cell->type.substr(0, 8) == "$paramod" ||
572  cell->type.substr(0, 9) == "$verific$" || cell->type.substr(0, 7) == "$array:" || cell->type.substr(0, 8) == "$extern:")
573  return;
574 
575  if (cell->type.in("$not", "$pos", "$neg")) {
576  param_bool("\\A_SIGNED");
577  port("\\A", param("\\A_WIDTH"));
578  port("\\Y", param("\\Y_WIDTH"));
579  check_expected();
580  return;
581  }
582 
583  if (cell->type.in("$and", "$or", "$xor", "$xnor")) {
584  param_bool("\\A_SIGNED");
585  param_bool("\\B_SIGNED");
586  port("\\A", param("\\A_WIDTH"));
587  port("\\B", param("\\B_WIDTH"));
588  port("\\Y", param("\\Y_WIDTH"));
589  check_expected();
590  return;
591  }
592 
593  if (cell->type.in("$reduce_and", "$reduce_or", "$reduce_xor", "$reduce_xnor", "$reduce_bool")) {
594  param_bool("\\A_SIGNED");
595  port("\\A", param("\\A_WIDTH"));
596  port("\\Y", param("\\Y_WIDTH"));
597  check_expected();
598  return;
599  }
600 
601  if (cell->type.in("$shl", "$shr", "$sshl", "$sshr", "$shift", "$shiftx")) {
602  param_bool("\\A_SIGNED");
603  param_bool("\\B_SIGNED");
604  port("\\A", param("\\A_WIDTH"));
605  port("\\B", param("\\B_WIDTH"));
606  port("\\Y", param("\\Y_WIDTH"));
607  check_expected(false);
608  return;
609  }
610 
611  if (cell->type.in("$lt", "$le", "$eq", "$ne", "$eqx", "$nex", "$ge", "$gt")) {
612  param_bool("\\A_SIGNED");
613  param_bool("\\B_SIGNED");
614  port("\\A", param("\\A_WIDTH"));
615  port("\\B", param("\\B_WIDTH"));
616  port("\\Y", param("\\Y_WIDTH"));
617  check_expected();
618  return;
619  }
620 
621  if (cell->type.in("$add", "$sub", "$mul", "$div", "$mod", "$pow")) {
622  param_bool("\\A_SIGNED");
623  param_bool("\\B_SIGNED");
624  port("\\A", param("\\A_WIDTH"));
625  port("\\B", param("\\B_WIDTH"));
626  port("\\Y", param("\\Y_WIDTH"));
627  check_expected(cell->type != "$pow");
628  return;
629  }
630 
631  if (cell->type == "$fa") {
632  port("\\A", param("\\WIDTH"));
633  port("\\B", param("\\WIDTH"));
634  port("\\C", param("\\WIDTH"));
635  port("\\X", param("\\WIDTH"));
636  port("\\Y", param("\\WIDTH"));
637  check_expected();
638  return;
639  }
640 
641  if (cell->type == "$lcu") {
642  port("\\P", param("\\WIDTH"));
643  port("\\G", param("\\WIDTH"));
644  port("\\CI", 1);
645  port("\\CO", param("\\WIDTH"));
646  check_expected();
647  return;
648  }
649 
650  if (cell->type == "$alu") {
651  param_bool("\\A_SIGNED");
652  param_bool("\\B_SIGNED");
653  port("\\A", param("\\A_WIDTH"));
654  port("\\B", param("\\B_WIDTH"));
655  port("\\CI", 1);
656  port("\\BI", 1);
657  port("\\X", param("\\Y_WIDTH"));
658  port("\\Y", param("\\Y_WIDTH"));
659  port("\\CO", param("\\Y_WIDTH"));
660  check_expected();
661  return;
662  }
663 
664  if (cell->type == "$macc") {
665  param("\\CONFIG");
666  param("\\CONFIG_WIDTH");
667  port("\\A", param("\\A_WIDTH"));
668  port("\\B", param("\\B_WIDTH"));
669  port("\\Y", param("\\Y_WIDTH"));
670  check_expected();
671  Macc().from_cell(cell);
672  return;
673  }
674 
675  if (cell->type == "$logic_not") {
676  param_bool("\\A_SIGNED");
677  port("\\A", param("\\A_WIDTH"));
678  port("\\Y", param("\\Y_WIDTH"));
679  check_expected();
680  return;
681  }
682 
683  if (cell->type == "$logic_and" || cell->type == "$logic_or") {
684  param_bool("\\A_SIGNED");
685  param_bool("\\B_SIGNED");
686  port("\\A", param("\\A_WIDTH"));
687  port("\\B", param("\\B_WIDTH"));
688  port("\\Y", param("\\Y_WIDTH"));
689  check_expected(false);
690  return;
691  }
692 
693  if (cell->type == "$slice") {
694  param("\\OFFSET");
695  port("\\A", param("\\A_WIDTH"));
696  port("\\Y", param("\\Y_WIDTH"));
697  if (param("\\OFFSET") + param("\\Y_WIDTH") > param("\\A_WIDTH"))
698  error(__LINE__);
699  check_expected();
700  return;
701  }
702 
703  if (cell->type == "$concat") {
704  port("\\A", param("\\A_WIDTH"));
705  port("\\B", param("\\B_WIDTH"));
706  port("\\Y", param("\\A_WIDTH") + param("\\B_WIDTH"));
707  check_expected();
708  return;
709  }
710 
711  if (cell->type == "$mux") {
712  port("\\A", param("\\WIDTH"));
713  port("\\B", param("\\WIDTH"));
714  port("\\S", 1);
715  port("\\Y", param("\\WIDTH"));
716  check_expected();
717  return;
718  }
719 
720  if (cell->type == "$pmux") {
721  port("\\A", param("\\WIDTH"));
722  port("\\B", param("\\WIDTH") * param("\\S_WIDTH"));
723  port("\\S", param("\\S_WIDTH"));
724  port("\\Y", param("\\WIDTH"));
725  check_expected();
726  return;
727  }
728 
729  if (cell->type == "$lut") {
730  param("\\LUT");
731  port("\\A", param("\\WIDTH"));
732  port("\\Y", 1);
733  check_expected();
734  return;
735  }
736 
737  if (cell->type == "$sr") {
738  param_bool("\\SET_POLARITY");
739  param_bool("\\CLR_POLARITY");
740  port("\\SET", param("\\WIDTH"));
741  port("\\CLR", param("\\WIDTH"));
742  port("\\Q", param("\\WIDTH"));
743  check_expected();
744  return;
745  }
746 
747  if (cell->type == "$dff") {
748  param_bool("\\CLK_POLARITY");
749  port("\\CLK", 1);
750  port("\\D", param("\\WIDTH"));
751  port("\\Q", param("\\WIDTH"));
752  check_expected();
753  return;
754  }
755 
756  if (cell->type == "$dffe") {
757  param_bool("\\CLK_POLARITY");
758  param_bool("\\EN_POLARITY");
759  port("\\CLK", 1);
760  port("\\EN", 1);
761  port("\\D", param("\\WIDTH"));
762  port("\\Q", param("\\WIDTH"));
763  check_expected();
764  return;
765  }
766 
767  if (cell->type == "$dffsr") {
768  param_bool("\\CLK_POLARITY");
769  param_bool("\\SET_POLARITY");
770  param_bool("\\CLR_POLARITY");
771  port("\\CLK", 1);
772  port("\\SET", param("\\WIDTH"));
773  port("\\CLR", param("\\WIDTH"));
774  port("\\D", param("\\WIDTH"));
775  port("\\Q", param("\\WIDTH"));
776  check_expected();
777  return;
778  }
779 
780  if (cell->type == "$adff") {
781  param_bool("\\CLK_POLARITY");
782  param_bool("\\ARST_POLARITY");
783  param_bits("\\ARST_VALUE", param("\\WIDTH"));
784  port("\\CLK", 1);
785  port("\\ARST", 1);
786  port("\\D", param("\\WIDTH"));
787  port("\\Q", param("\\WIDTH"));
788  check_expected();
789  return;
790  }
791 
792  if (cell->type == "$dlatch") {
793  param_bool("\\EN_POLARITY");
794  port("\\EN", 1);
795  port("\\D", param("\\WIDTH"));
796  port("\\Q", param("\\WIDTH"));
797  check_expected();
798  return;
799  }
800 
801  if (cell->type == "$dlatchsr") {
802  param_bool("\\EN_POLARITY");
803  param_bool("\\SET_POLARITY");
804  param_bool("\\CLR_POLARITY");
805  port("\\EN", 1);
806  port("\\SET", param("\\WIDTH"));
807  port("\\CLR", param("\\WIDTH"));
808  port("\\D", param("\\WIDTH"));
809  port("\\Q", param("\\WIDTH"));
810  check_expected();
811  return;
812  }
813 
814  if (cell->type == "$fsm") {
815  param("\\NAME");
816  param_bool("\\CLK_POLARITY");
817  param_bool("\\ARST_POLARITY");
818  param("\\STATE_BITS");
819  param("\\STATE_NUM");
820  param("\\STATE_NUM_LOG2");
821  param("\\STATE_RST");
822  param_bits("\\STATE_TABLE", param("\\STATE_BITS") * param("\\STATE_NUM"));
823  param("\\TRANS_NUM");
824  param_bits("\\TRANS_TABLE", param("\\TRANS_NUM") * (2*param("\\STATE_NUM_LOG2") + param("\\CTRL_IN_WIDTH") + param("\\CTRL_OUT_WIDTH")));
825  port("\\CLK", 1);
826  port("\\ARST", 1);
827  port("\\CTRL_IN", param("\\CTRL_IN_WIDTH"));
828  port("\\CTRL_OUT", param("\\CTRL_OUT_WIDTH"));
829  check_expected();
830  return;
831  }
832 
833  if (cell->type == "$memrd") {
834  param("\\MEMID");
835  param_bool("\\CLK_ENABLE");
836  param_bool("\\CLK_POLARITY");
837  param_bool("\\TRANSPARENT");
838  port("\\CLK", 1);
839  port("\\ADDR", param("\\ABITS"));
840  port("\\DATA", param("\\WIDTH"));
841  check_expected();
842  return;
843  }
844 
845  if (cell->type == "$memwr") {
846  param("\\MEMID");
847  param_bool("\\CLK_ENABLE");
848  param_bool("\\CLK_POLARITY");
849  param("\\PRIORITY");
850  port("\\CLK", 1);
851  port("\\EN", param("\\WIDTH"));
852  port("\\ADDR", param("\\ABITS"));
853  port("\\DATA", param("\\WIDTH"));
854  check_expected();
855  return;
856  }
857 
858  if (cell->type == "$mem") {
859  param("\\MEMID");
860  param("\\SIZE");
861  param("\\OFFSET");
862  param_bits("\\RD_CLK_ENABLE", param("\\RD_PORTS"));
863  param_bits("\\RD_CLK_POLARITY", param("\\RD_PORTS"));
864  param_bits("\\RD_TRANSPARENT", param("\\RD_PORTS"));
865  param_bits("\\WR_CLK_ENABLE", param("\\WR_PORTS"));
866  param_bits("\\WR_CLK_POLARITY", param("\\WR_PORTS"));
867  port("\\RD_CLK", param("\\RD_PORTS"));
868  port("\\RD_ADDR", param("\\RD_PORTS") * param("\\ABITS"));
869  port("\\RD_DATA", param("\\RD_PORTS") * param("\\WIDTH"));
870  port("\\WR_CLK", param("\\WR_PORTS"));
871  port("\\WR_EN", param("\\WR_PORTS") * param("\\WIDTH"));
872  port("\\WR_ADDR", param("\\WR_PORTS") * param("\\ABITS"));
873  port("\\WR_DATA", param("\\WR_PORTS") * param("\\WIDTH"));
874  check_expected();
875  return;
876  }
877 
878  if (cell->type == "$assert") {
879  port("\\A", 1);
880  port("\\EN", 1);
881  check_expected();
882  return;
883  }
884 
885  if (cell->type == "$_BUF_") { check_gate("AY"); return; }
886  if (cell->type == "$_NOT_") { check_gate("AY"); return; }
887  if (cell->type == "$_AND_") { check_gate("ABY"); return; }
888  if (cell->type == "$_NAND_") { check_gate("ABY"); return; }
889  if (cell->type == "$_OR_") { check_gate("ABY"); return; }
890  if (cell->type == "$_NOR_") { check_gate("ABY"); return; }
891  if (cell->type == "$_XOR_") { check_gate("ABY"); return; }
892  if (cell->type == "$_XNOR_") { check_gate("ABY"); return; }
893  if (cell->type == "$_MUX_") { check_gate("ABSY"); return; }
894  if (cell->type == "$_AOI3_") { check_gate("ABCY"); return; }
895  if (cell->type == "$_OAI3_") { check_gate("ABCY"); return; }
896  if (cell->type == "$_AOI4_") { check_gate("ABCDY"); return; }
897  if (cell->type == "$_OAI4_") { check_gate("ABCDY"); return; }
898 
899  if (cell->type == "$_SR_NN_") { check_gate("SRQ"); return; }
900  if (cell->type == "$_SR_NP_") { check_gate("SRQ"); return; }
901  if (cell->type == "$_SR_PN_") { check_gate("SRQ"); return; }
902  if (cell->type == "$_SR_PP_") { check_gate("SRQ"); return; }
903 
904  if (cell->type == "$_DFF_N_") { check_gate("DQC"); return; }
905  if (cell->type == "$_DFF_P_") { check_gate("DQC"); return; }
906 
907  if (cell->type == "$_DFFE_NN_") { check_gate("DQCE"); return; }
908  if (cell->type == "$_DFFE_NP_") { check_gate("DQCE"); return; }
909  if (cell->type == "$_DFFE_PN_") { check_gate("DQCE"); return; }
910  if (cell->type == "$_DFFE_PP_") { check_gate("DQCE"); return; }
911 
912  if (cell->type == "$_DFF_NN0_") { check_gate("DQCR"); return; }
913  if (cell->type == "$_DFF_NN1_") { check_gate("DQCR"); return; }
914  if (cell->type == "$_DFF_NP0_") { check_gate("DQCR"); return; }
915  if (cell->type == "$_DFF_NP1_") { check_gate("DQCR"); return; }
916  if (cell->type == "$_DFF_PN0_") { check_gate("DQCR"); return; }
917  if (cell->type == "$_DFF_PN1_") { check_gate("DQCR"); return; }
918  if (cell->type == "$_DFF_PP0_") { check_gate("DQCR"); return; }
919  if (cell->type == "$_DFF_PP1_") { check_gate("DQCR"); return; }
920 
921  if (cell->type == "$_DFFSR_NNN_") { check_gate("CSRDQ"); return; }
922  if (cell->type == "$_DFFSR_NNP_") { check_gate("CSRDQ"); return; }
923  if (cell->type == "$_DFFSR_NPN_") { check_gate("CSRDQ"); return; }
924  if (cell->type == "$_DFFSR_NPP_") { check_gate("CSRDQ"); return; }
925  if (cell->type == "$_DFFSR_PNN_") { check_gate("CSRDQ"); return; }
926  if (cell->type == "$_DFFSR_PNP_") { check_gate("CSRDQ"); return; }
927  if (cell->type == "$_DFFSR_PPN_") { check_gate("CSRDQ"); return; }
928  if (cell->type == "$_DFFSR_PPP_") { check_gate("CSRDQ"); return; }
929 
930  if (cell->type == "$_DLATCH_N_") { check_gate("EDQ"); return; }
931  if (cell->type == "$_DLATCH_P_") { check_gate("EDQ"); return; }
932 
933  if (cell->type == "$_DLATCHSR_NNN_") { check_gate("ESRDQ"); return; }
934  if (cell->type == "$_DLATCHSR_NNP_") { check_gate("ESRDQ"); return; }
935  if (cell->type == "$_DLATCHSR_NPN_") { check_gate("ESRDQ"); return; }
936  if (cell->type == "$_DLATCHSR_NPP_") { check_gate("ESRDQ"); return; }
937  if (cell->type == "$_DLATCHSR_PNN_") { check_gate("ESRDQ"); return; }
938  if (cell->type == "$_DLATCHSR_PNP_") { check_gate("ESRDQ"); return; }
939  if (cell->type == "$_DLATCHSR_PPN_") { check_gate("ESRDQ"); return; }
940  if (cell->type == "$_DLATCHSR_PPP_") { check_gate("ESRDQ"); return; }
941 
942  error(__LINE__);
943  }
944  };
945 }
946 #endif
947 
949 {
950 #ifndef NDEBUG
951  std::vector<bool> ports_declared;
952  for (auto &it : wires_) {
953  log_assert(this == it.second->module);
954  log_assert(it.first == it.second->name);
955  log_assert(!it.first.empty());
956  log_assert(it.second->width >= 0);
957  log_assert(it.second->port_id >= 0);
958  for (auto &it2 : it.second->attributes)
959  log_assert(!it2.first.empty());
960  if (it.second->port_id) {
961  log_assert(GetSize(ports) >= it.second->port_id);
962  log_assert(ports.at(it.second->port_id-1) == it.first);
963  log_assert(it.second->port_input || it.second->port_output);
964  if (GetSize(ports_declared) < it.second->port_id)
965  ports_declared.resize(it.second->port_id);
966  log_assert(ports_declared[it.second->port_id-1] == false);
967  ports_declared[it.second->port_id-1] = true;
968  } else
969  log_assert(!it.second->port_input && !it.second->port_output);
970  }
971  for (auto port_declared : ports_declared)
972  log_assert(port_declared == true);
973  log_assert(GetSize(ports) == GetSize(ports_declared));
974 
975  for (auto &it : memories) {
976  log_assert(it.first == it.second->name);
977  log_assert(!it.first.empty());
978  log_assert(it.second->width >= 0);
979  log_assert(it.second->size >= 0);
980  for (auto &it2 : it.second->attributes)
981  log_assert(!it2.first.empty());
982  }
983 
984  for (auto &it : cells_) {
985  log_assert(this == it.second->module);
986  log_assert(it.first == it.second->name);
987  log_assert(!it.first.empty());
988  log_assert(!it.second->type.empty());
989  for (auto &it2 : it.second->connections()) {
990  log_assert(!it2.first.empty());
991  it2.second.check();
992  }
993  for (auto &it2 : it.second->attributes)
994  log_assert(!it2.first.empty());
995  for (auto &it2 : it.second->parameters)
996  log_assert(!it2.first.empty());
997  InternalCellChecker checker(this, it.second);
998  checker.check();
999  }
1000 
1001  for (auto &it : processes) {
1002  log_assert(it.first == it.second->name);
1003  log_assert(!it.first.empty());
1004  // FIXME: More checks here..
1005  }
1006 
1007  for (auto &it : connections_) {
1008  log_assert(it.first.size() == it.second.size());
1009  it.first.check();
1010  it.second.check();
1011  }
1012 
1013  for (auto &it : attributes)
1014  log_assert(!it.first.empty());
1015 #endif
1016 }
1017 
1019 {
1020 }
1021 
1023 {
1024  log_assert(new_mod->refcount_wires_ == 0);
1025  log_assert(new_mod->refcount_cells_ == 0);
1026 
1027  new_mod->connections_ = connections_;
1028  new_mod->attributes = attributes;
1029 
1030  for (auto &it : wires_)
1031  new_mod->addWire(it.first, it.second);
1032 
1033  for (auto &it : memories)
1034  new_mod->memories[it.first] = new RTLIL::Memory(*it.second);
1035 
1036  for (auto &it : cells_)
1037  new_mod->addCell(it.first, it.second);
1038 
1039  for (auto &it : processes)
1040  new_mod->processes[it.first] = it.second->clone();
1041 
1042  struct RewriteSigSpecWorker
1043  {
1044  RTLIL::Module *mod;
1045  void operator()(RTLIL::SigSpec &sig)
1046  {
1047  std::vector<RTLIL::SigChunk> chunks = sig.chunks();
1048  for (auto &c : chunks)
1049  if (c.wire != NULL)
1050  c.wire = mod->wires_.at(c.wire->name);
1051  sig = chunks;
1052  }
1053  };
1054 
1055  RewriteSigSpecWorker rewriteSigSpecWorker;
1056  rewriteSigSpecWorker.mod = new_mod;
1057  new_mod->rewrite_sigspecs(rewriteSigSpecWorker);
1058  new_mod->fixup_ports();
1059 }
1060 
1062 {
1063  RTLIL::Module *new_mod = new RTLIL::Module;
1064  new_mod->name = name;
1065  cloneInto(new_mod);
1066  return new_mod;
1067 }
1068 
1070 {
1071  return !memories.empty();
1072 }
1073 
1075 {
1076  return !processes.empty();
1077 }
1078 
1080 {
1081  if (!memories.empty())
1082  log_warning("Ignoring module %s because it contains memories (run 'memory' command first).\n", log_id(this));
1083  return !memories.empty();
1084 }
1085 
1087 {
1088  if (!processes.empty())
1089  log_warning("Ignoring module %s because it contains processes (run 'proc' command first).\n", log_id(this));
1090  return !processes.empty();
1091 }
1092 
1093 std::vector<RTLIL::Wire*> RTLIL::Module::selected_wires() const
1094 {
1095  std::vector<RTLIL::Wire*> result;
1096  result.reserve(wires_.size());
1097  for (auto &it : wires_)
1098  if (design->selected(this, it.second))
1099  result.push_back(it.second);
1100  return result;
1101 }
1102 
1103 std::vector<RTLIL::Cell*> RTLIL::Module::selected_cells() const
1104 {
1105  std::vector<RTLIL::Cell*> result;
1106  result.reserve(wires_.size());
1107  for (auto &it : cells_)
1108  if (design->selected(this, it.second))
1109  result.push_back(it.second);
1110  return result;
1111 }
1112 
1114 {
1115  log_assert(!wire->name.empty());
1116  log_assert(count_id(wire->name) == 0);
1117  log_assert(refcount_wires_ == 0);
1118  wires_[wire->name] = wire;
1119  wire->module = this;
1120 }
1121 
1123 {
1124  log_assert(!cell->name.empty());
1125  log_assert(count_id(cell->name) == 0);
1126  log_assert(refcount_cells_ == 0);
1127  cells_[cell->name] = cell;
1128  cell->module = this;
1129 }
1130 
1131 namespace {
1132  struct DeleteWireWorker
1133  {
1135  const std::set<RTLIL::Wire*> *wires_p;
1136 
1137  void operator()(RTLIL::SigSpec &sig) {
1138  std::vector<RTLIL::SigChunk> chunks = sig;
1139  for (auto &c : chunks)
1140  if (c.wire != NULL && wires_p->count(c.wire)) {
1141  c.wire = module->addWire(NEW_ID, c.width);
1142  c.offset = 0;
1143  }
1144  sig = chunks;
1145  }
1146  };
1147 }
1148 
1149 #if 0
1151 {
1152  std::setPort<RTLIL::Wire*> wires_;
1153  wires_.insert(wire);
1154  remove(wires_);
1155 }
1156 #endif
1157 
1158 void RTLIL::Module::remove(const std::set<RTLIL::Wire*> &wires)
1159 {
1160  log_assert(refcount_wires_ == 0);
1161 
1162  DeleteWireWorker delete_wire_worker;
1163  delete_wire_worker.module = this;
1164  delete_wire_worker.wires_p = &wires;
1165  rewrite_sigspecs(delete_wire_worker);
1166 
1167  for (auto &it : wires) {
1168  log_assert(wires_.count(it->name) != 0);
1169  wires_.erase(it->name);
1170  delete it;
1171  }
1172 }
1173 
1175 {
1176  while (!cell->connections_.empty())
1177  cell->unsetPort(cell->connections_.begin()->first);
1178 
1179  log_assert(cells_.count(cell->name) != 0);
1180  log_assert(refcount_cells_ == 0);
1181  cells_.erase(cell->name);
1182  delete cell;
1183 }
1184 
1186 {
1187  log_assert(wires_[wire->name] == wire);
1188  log_assert(refcount_wires_ == 0);
1189  wires_.erase(wire->name);
1190  wire->name = new_name;
1191  add(wire);
1192 }
1193 
1195 {
1196  log_assert(cells_[cell->name] == cell);
1197  log_assert(refcount_wires_ == 0);
1198  cells_.erase(cell->name);
1199  cell->name = new_name;
1200  add(cell);
1201 }
1202 
1204 {
1205  log_assert(count_id(old_name) != 0);
1206  if (wires_.count(old_name))
1207  rename(wires_.at(old_name), new_name);
1208  else if (cells_.count(old_name))
1209  rename(cells_.at(old_name), new_name);
1210  else
1211  log_abort();
1212 }
1213 
1215 {
1216  log_assert(wires_[w1->name] == w1);
1217  log_assert(wires_[w2->name] == w2);
1218  log_assert(refcount_wires_ == 0);
1219 
1220  wires_.erase(w1->name);
1221  wires_.erase(w2->name);
1222 
1223  std::swap(w1->name, w2->name);
1224 
1225  wires_[w1->name] = w1;
1226  wires_[w2->name] = w2;
1227 }
1228 
1230 {
1231  log_assert(cells_[c1->name] == c1);
1232  log_assert(cells_[c2->name] == c2);
1233  log_assert(refcount_cells_ == 0);
1234 
1235  cells_.erase(c1->name);
1236  cells_.erase(c2->name);
1237 
1238  std::swap(c1->name, c2->name);
1239 
1240  cells_[c1->name] = c1;
1241  cells_[c2->name] = c2;
1242 }
1243 
1245 {
1246  int index = 0;
1247  return uniquify(name, index);
1248 }
1249 
1251 {
1252  if (index == 0) {
1253  if (count_id(name) == 0)
1254  return name;
1255  index++;
1256  }
1257 
1258  while (1) {
1259  RTLIL::IdString new_name = stringf("%s_%d", name.c_str(), index);
1260  if (count_id(new_name) == 0)
1261  return new_name;
1262  index++;
1263  }
1264 }
1265 
1266 static bool fixup_ports_compare(const RTLIL::Wire *a, const RTLIL::Wire *b)
1267 {
1268  if (a->port_id && !b->port_id)
1269  return true;
1270  if (!a->port_id && b->port_id)
1271  return false;
1272 
1273  if (a->port_id == b->port_id)
1274  return a->name < b->name;
1275  return a->port_id < b->port_id;
1276 }
1277 
1279 {
1280  for (auto mon : monitors)
1281  mon->notify_connect(this, conn);
1282 
1283  if (design)
1284  for (auto mon : design->monitors)
1285  mon->notify_connect(this, conn);
1286 
1287  connections_.push_back(conn);
1288 }
1289 
1291 {
1292  connect(RTLIL::SigSig(lhs, rhs));
1293 }
1294 
1295 void RTLIL::Module::new_connections(const std::vector<RTLIL::SigSig> &new_conn)
1296 {
1297  for (auto mon : monitors)
1298  mon->notify_connect(this, new_conn);
1299 
1300  if (design)
1301  for (auto mon : design->monitors)
1302  mon->notify_connect(this, new_conn);
1303 
1304  connections_ = new_conn;
1305 }
1306 
1307 const std::vector<RTLIL::SigSig> &RTLIL::Module::connections() const
1308 {
1309  return connections_;
1310 }
1311 
1313 {
1314  std::vector<RTLIL::Wire*> all_ports;
1315 
1316  for (auto &w : wires_)
1317  if (w.second->port_input || w.second->port_output)
1318  all_ports.push_back(w.second);
1319  else
1320  w.second->port_id = 0;
1321 
1322  std::sort(all_ports.begin(), all_ports.end(), fixup_ports_compare);
1323 
1324  ports.clear();
1325  for (size_t i = 0; i < all_ports.size(); i++) {
1326  ports.push_back(all_ports[i]->name);
1327  all_ports[i]->port_id = i+1;
1328  }
1329 }
1330 
1332 {
1333  RTLIL::Wire *wire = new RTLIL::Wire;
1334  wire->name = name;
1335  wire->width = width;
1336  add(wire);
1337  return wire;
1338 }
1339 
1341 {
1342  RTLIL::Wire *wire = addWire(name);
1343  wire->width = other->width;
1344  wire->start_offset = other->start_offset;
1345  wire->port_id = other->port_id;
1346  wire->port_input = other->port_input;
1347  wire->port_output = other->port_output;
1348  wire->upto = other->upto;
1349  wire->attributes = other->attributes;
1350  return wire;
1351 }
1352 
1354 {
1355  RTLIL::Cell *cell = new RTLIL::Cell;
1356  cell->name = name;
1357  cell->type = type;
1358  add(cell);
1359  return cell;
1360 }
1361 
1363 {
1364  RTLIL::Cell *cell = addCell(name, other->type);
1365  cell->connections_ = other->connections_;
1366  cell->parameters = other->parameters;
1367  cell->attributes = other->attributes;
1368  return cell;
1369 }
1370 
1371 #define DEF_METHOD(_func, _y_size, _type) \
1372  RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_y, bool is_signed) { \
1373  RTLIL::Cell *cell = addCell(name, _type); \
1374  cell->parameters["\\A_SIGNED"] = is_signed; \
1375  cell->parameters["\\A_WIDTH"] = sig_a.size(); \
1376  cell->parameters["\\Y_WIDTH"] = sig_y.size(); \
1377  cell->setPort("\\A", sig_a); \
1378  cell->setPort("\\Y", sig_y); \
1379  return cell; \
1380  } \
1381  RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, bool is_signed) { \
1382  RTLIL::SigSpec sig_y = addWire(NEW_ID, _y_size); \
1383  add ## _func(name, sig_a, sig_y, is_signed); \
1384  return sig_y; \
1385  }
1386 DEF_METHOD(Not, sig_a.size(), "$not")
1387 DEF_METHOD(Pos, sig_a.size(), "$pos")
1388 DEF_METHOD(Neg, sig_a.size(), "$neg")
1389 DEF_METHOD(ReduceAnd, 1, "$reduce_and")
1390 DEF_METHOD(ReduceOr, 1, "$reduce_or")
1391 DEF_METHOD(ReduceXor, 1, "$reduce_xor")
1392 DEF_METHOD(ReduceXnor, 1, "$reduce_xnor")
1393 DEF_METHOD(ReduceBool, 1, "$reduce_bool")
1394 DEF_METHOD(LogicNot, 1, "$logic_not")
1395 #undef DEF_METHOD
1396 
1397 #define DEF_METHOD(_func, _y_size, _type) \
1398  RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, RTLIL::SigSpec sig_y, bool is_signed) { \
1399  RTLIL::Cell *cell = addCell(name, _type); \
1400  cell->parameters["\\A_SIGNED"] = is_signed; \
1401  cell->parameters["\\B_SIGNED"] = is_signed; \
1402  cell->parameters["\\A_WIDTH"] = sig_a.size(); \
1403  cell->parameters["\\B_WIDTH"] = sig_b.size(); \
1404  cell->parameters["\\Y_WIDTH"] = sig_y.size(); \
1405  cell->setPort("\\A", sig_a); \
1406  cell->setPort("\\B", sig_b); \
1407  cell->setPort("\\Y", sig_y); \
1408  return cell; \
1409  } \
1410  RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, bool is_signed) { \
1411  RTLIL::SigSpec sig_y = addWire(NEW_ID, _y_size); \
1412  add ## _func(name, sig_a, sig_b, sig_y, is_signed); \
1413  return sig_y; \
1414  }
1415 DEF_METHOD(And, std::max(sig_a.size(), sig_b.size()), "$and")
1416 DEF_METHOD(Or, std::max(sig_a.size(), sig_b.size()), "$or")
1417 DEF_METHOD(Xor, std::max(sig_a.size(), sig_b.size()), "$xor")
1418 DEF_METHOD(Xnor, std::max(sig_a.size(), sig_b.size()), "$xnor")
1419 DEF_METHOD(Shl, sig_a.size(), "$shl")
1420 DEF_METHOD(Shr, sig_a.size(), "$shr")
1421 DEF_METHOD(Sshl, sig_a.size(), "$sshl")
1422 DEF_METHOD(Sshr, sig_a.size(), "$sshr")
1423 DEF_METHOD(Shift, sig_a.size(), "$shift")
1424 DEF_METHOD(Shiftx, sig_a.size(), "$shiftx")
1425 DEF_METHOD(Lt, 1, "$lt")
1426 DEF_METHOD(Le, 1, "$le")
1427 DEF_METHOD(Eq, 1, "$eq")
1428 DEF_METHOD(Ne, 1, "$ne")
1429 DEF_METHOD(Eqx, 1, "$eqx")
1430 DEF_METHOD(Nex, 1, "$nex")
1431 DEF_METHOD(Ge, 1, "$ge")
1432 DEF_METHOD(Gt, 1, "$gt")
1433 DEF_METHOD(Add, std::max(sig_a.size(), sig_b.size()), "$add")
1434 DEF_METHOD(Sub, std::max(sig_a.size(), sig_b.size()), "$sub")
1435 DEF_METHOD(Mul, std::max(sig_a.size(), sig_b.size()), "$mul")
1436 DEF_METHOD(Div, std::max(sig_a.size(), sig_b.size()), "$div")
1437 DEF_METHOD(Mod, std::max(sig_a.size(), sig_b.size()), "$mod")
1438 DEF_METHOD(LogicAnd, 1, "$logic_and")
1439 DEF_METHOD(LogicOr, 1, "$logic_or")
1440 #undef DEF_METHOD
1441 
1442 #define DEF_METHOD(_func, _type, _pmux) \
1443  RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, RTLIL::SigSpec sig_s, RTLIL::SigSpec sig_y) { \
1444  RTLIL::Cell *cell = addCell(name, _type); \
1445  cell->parameters["\\WIDTH"] = sig_a.size(); \
1446  if (_pmux) cell->parameters["\\S_WIDTH"] = sig_s.size(); \
1447  cell->setPort("\\A", sig_a); \
1448  cell->setPort("\\B", sig_b); \
1449  cell->setPort("\\S", sig_s); \
1450  cell->setPort("\\Y", sig_y); \
1451  return cell; \
1452  } \
1453  RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, RTLIL::SigSpec sig_s) { \
1454  RTLIL::SigSpec sig_y = addWire(NEW_ID, sig_a.size()); \
1455  add ## _func(name, sig_a, sig_b, sig_s, sig_y); \
1456  return sig_y; \
1457  }
1458 DEF_METHOD(Mux, "$mux", 0)
1459 DEF_METHOD(Pmux, "$pmux", 1)
1460 #undef DEF_METHOD
1461 
1462 #define DEF_METHOD_2(_func, _type, _P1, _P2) \
1463  RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2) { \
1464  RTLIL::Cell *cell = addCell(name, _type); \
1465  cell->setPort("\\" #_P1, sig1); \
1466  cell->setPort("\\" #_P2, sig2); \
1467  return cell; \
1468  } \
1469  RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1) { \
1470  RTLIL::SigBit sig2 = addWire(NEW_ID); \
1471  add ## _func(name, sig1, sig2); \
1472  return sig2; \
1473  }
1474 #define DEF_METHOD_3(_func, _type, _P1, _P2, _P3) \
1475  RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3) { \
1476  RTLIL::Cell *cell = addCell(name, _type); \
1477  cell->setPort("\\" #_P1, sig1); \
1478  cell->setPort("\\" #_P2, sig2); \
1479  cell->setPort("\\" #_P3, sig3); \
1480  return cell; \
1481  } \
1482  RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2) { \
1483  RTLIL::SigBit sig3 = addWire(NEW_ID); \
1484  add ## _func(name, sig1, sig2, sig3); \
1485  return sig3; \
1486  }
1487 #define DEF_METHOD_4(_func, _type, _P1, _P2, _P3, _P4) \
1488  RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3, RTLIL::SigBit sig4) { \
1489  RTLIL::Cell *cell = addCell(name, _type); \
1490  cell->setPort("\\" #_P1, sig1); \
1491  cell->setPort("\\" #_P2, sig2); \
1492  cell->setPort("\\" #_P3, sig3); \
1493  cell->setPort("\\" #_P4, sig4); \
1494  return cell; \
1495  } \
1496  RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3) { \
1497  RTLIL::SigBit sig4 = addWire(NEW_ID); \
1498  add ## _func(name, sig1, sig2, sig3, sig4); \
1499  return sig4; \
1500  }
1501 #define DEF_METHOD_5(_func, _type, _P1, _P2, _P3, _P4, _P5) \
1502  RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3, RTLIL::SigBit sig4, RTLIL::SigBit sig5) { \
1503  RTLIL::Cell *cell = addCell(name, _type); \
1504  cell->setPort("\\" #_P1, sig1); \
1505  cell->setPort("\\" #_P2, sig2); \
1506  cell->setPort("\\" #_P3, sig3); \
1507  cell->setPort("\\" #_P4, sig4); \
1508  cell->setPort("\\" #_P5, sig5); \
1509  return cell; \
1510  } \
1511  RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3, RTLIL::SigBit sig4) { \
1512  RTLIL::SigBit sig5 = addWire(NEW_ID); \
1513  add ## _func(name, sig1, sig2, sig3, sig4, sig5); \
1514  return sig5; \
1515  }
1516 DEF_METHOD_2(NotGate, "$_NOT_", A, Y)
1517 DEF_METHOD_3(AndGate, "$_AND_", A, B, Y)
1518 DEF_METHOD_3(NandGate, "$_NAND_", A, B, Y)
1519 DEF_METHOD_3(OrGate, "$_OR_", A, B, Y)
1520 DEF_METHOD_3(NorGate, "$_NOR_", A, B, Y)
1521 DEF_METHOD_3(XorGate, "$_XOR_", A, B, Y)
1522 DEF_METHOD_3(XnorGate, "$_XNOR_", A, B, Y)
1523 DEF_METHOD_4(MuxGate, "$_MUX_", A, B, S, Y)
1524 DEF_METHOD_4(Aoi3Gate, "$_AOI3_", A, B, C, Y)
1525 DEF_METHOD_4(Oai3Gate, "$_OAI3_", A, B, C, Y)
1526 DEF_METHOD_5(Aoi4Gate, "$_AOI4_", A, B, C, D, Y)
1527 DEF_METHOD_5(Oai4Gate, "$_OAI4_", A, B, C, D, Y)
1528 #undef DEF_METHOD_2
1529 #undef DEF_METHOD_3
1530 #undef DEF_METHOD_4
1531 #undef DEF_METHOD_5
1532 
1533 RTLIL::Cell* RTLIL::Module::addPow(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, RTLIL::SigSpec sig_y, bool a_signed, bool b_signed)
1534 {
1535  RTLIL::Cell *cell = addCell(name, "$pow");
1536  cell->parameters["\\A_SIGNED"] = a_signed;
1537  cell->parameters["\\B_SIGNED"] = b_signed;
1538  cell->parameters["\\A_WIDTH"] = sig_a.size();
1539  cell->parameters["\\B_WIDTH"] = sig_b.size();
1540  cell->parameters["\\Y_WIDTH"] = sig_y.size();
1541  cell->setPort("\\A", sig_a);
1542  cell->setPort("\\B", sig_b);
1543  cell->setPort("\\Y", sig_y);
1544  return cell;
1545 }
1546 
1548 {
1549  RTLIL::Cell *cell = addCell(name, "$slice");
1550  cell->parameters["\\A_WIDTH"] = sig_a.size();
1551  cell->parameters["\\Y_WIDTH"] = sig_y.size();
1552  cell->parameters["\\OFFSET"] = offset;
1553  cell->setPort("\\A", sig_a);
1554  cell->setPort("\\Y", sig_y);
1555  return cell;
1556 }
1557 
1559 {
1560  RTLIL::Cell *cell = addCell(name, "$concat");
1561  cell->parameters["\\A_WIDTH"] = sig_a.size();
1562  cell->parameters["\\B_WIDTH"] = sig_b.size();
1563  cell->setPort("\\A", sig_a);
1564  cell->setPort("\\B", sig_b);
1565  cell->setPort("\\Y", sig_y);
1566  return cell;
1567 }
1568 
1570 {
1571  RTLIL::Cell *cell = addCell(name, "$lut");
1572  cell->parameters["\\LUT"] = lut;
1573  cell->parameters["\\WIDTH"] = sig_i.size();
1574  cell->setPort("\\A", sig_i);
1575  cell->setPort("\\Y", sig_o);
1576  return cell;
1577 }
1578 
1580 {
1581  RTLIL::Cell *cell = addCell(name, "$assert");
1582  cell->setPort("\\A", sig_a);
1583  cell->setPort("\\EN", sig_en);
1584  return cell;
1585 }
1586 
1587 RTLIL::Cell* RTLIL::Module::addSr(RTLIL::IdString name, RTLIL::SigSpec sig_set, RTLIL::SigSpec sig_clr, RTLIL::SigSpec sig_q, bool set_polarity, bool clr_polarity)
1588 {
1589  RTLIL::Cell *cell = addCell(name, "$sr");
1590  cell->parameters["\\SET_POLARITY"] = set_polarity;
1591  cell->parameters["\\CLR_POLARITY"] = clr_polarity;
1592  cell->parameters["\\WIDTH"] = sig_q.size();
1593  cell->setPort("\\SET", sig_set);
1594  cell->setPort("\\CLR", sig_clr);
1595  cell->setPort("\\Q", sig_q);
1596  return cell;
1597 }
1598 
1600 {
1601  RTLIL::Cell *cell = addCell(name, "$dff");
1602  cell->parameters["\\CLK_POLARITY"] = clk_polarity;
1603  cell->parameters["\\WIDTH"] = sig_q.size();
1604  cell->setPort("\\CLK", sig_clk);
1605  cell->setPort("\\D", sig_d);
1606  cell->setPort("\\Q", sig_q);
1607  return cell;
1608 }
1609 
1611 {
1612  RTLIL::Cell *cell = addCell(name, "$dffe");
1613  cell->parameters["\\CLK_POLARITY"] = clk_polarity;
1614  cell->parameters["\\EN_POLARITY"] = en_polarity;
1615  cell->parameters["\\WIDTH"] = sig_q.size();
1616  cell->setPort("\\CLK", sig_clk);
1617  cell->setPort("\\EN", sig_en);
1618  cell->setPort("\\D", sig_d);
1619  cell->setPort("\\Q", sig_q);
1620  return cell;
1621 }
1622 
1624  RTLIL::SigSpec sig_d, RTLIL::SigSpec sig_q, bool clk_polarity, bool set_polarity, bool clr_polarity)
1625 {
1626  RTLIL::Cell *cell = addCell(name, "$dffsr");
1627  cell->parameters["\\CLK_POLARITY"] = clk_polarity;
1628  cell->parameters["\\SET_POLARITY"] = set_polarity;
1629  cell->parameters["\\CLR_POLARITY"] = clr_polarity;
1630  cell->parameters["\\WIDTH"] = sig_q.size();
1631  cell->setPort("\\CLK", sig_clk);
1632  cell->setPort("\\SET", sig_set);
1633  cell->setPort("\\CLR", sig_clr);
1634  cell->setPort("\\D", sig_d);
1635  cell->setPort("\\Q", sig_q);
1636  return cell;
1637 }
1638 
1640  RTLIL::Const arst_value, bool clk_polarity, bool arst_polarity)
1641 {
1642  RTLIL::Cell *cell = addCell(name, "$adff");
1643  cell->parameters["\\CLK_POLARITY"] = clk_polarity;
1644  cell->parameters["\\ARST_POLARITY"] = arst_polarity;
1645  cell->parameters["\\ARST_VALUE"] = arst_value;
1646  cell->parameters["\\WIDTH"] = sig_q.size();
1647  cell->setPort("\\CLK", sig_clk);
1648  cell->setPort("\\ARST", sig_arst);
1649  cell->setPort("\\D", sig_d);
1650  cell->setPort("\\Q", sig_q);
1651  return cell;
1652 }
1653 
1655 {
1656  RTLIL::Cell *cell = addCell(name, "$dlatch");
1657  cell->parameters["\\EN_POLARITY"] = en_polarity;
1658  cell->parameters["\\WIDTH"] = sig_q.size();
1659  cell->setPort("\\EN", sig_en);
1660  cell->setPort("\\D", sig_d);
1661  cell->setPort("\\Q", sig_q);
1662  return cell;
1663 }
1664 
1666  RTLIL::SigSpec sig_d, RTLIL::SigSpec sig_q, bool en_polarity, bool set_polarity, bool clr_polarity)
1667 {
1668  RTLIL::Cell *cell = addCell(name, "$dlatchsr");
1669  cell->parameters["\\EN_POLARITY"] = en_polarity;
1670  cell->parameters["\\SET_POLARITY"] = set_polarity;
1671  cell->parameters["\\CLR_POLARITY"] = clr_polarity;
1672  cell->parameters["\\WIDTH"] = sig_q.size();
1673  cell->setPort("\\EN", sig_en);
1674  cell->setPort("\\SET", sig_set);
1675  cell->setPort("\\CLR", sig_clr);
1676  cell->setPort("\\D", sig_d);
1677  cell->setPort("\\Q", sig_q);
1678  return cell;
1679 }
1680 
1682 {
1683  RTLIL::Cell *cell = addCell(name, stringf("$_DFF_%c_", clk_polarity ? 'P' : 'N'));
1684  cell->setPort("\\C", sig_clk);
1685  cell->setPort("\\D", sig_d);
1686  cell->setPort("\\Q", sig_q);
1687  return cell;
1688 }
1689 
1691 {
1692  RTLIL::Cell *cell = addCell(name, stringf("$_DFFE_%c%c_", clk_polarity ? 'P' : 'N', en_polarity ? 'P' : 'N'));
1693  cell->setPort("\\C", sig_clk);
1694  cell->setPort("\\E", sig_en);
1695  cell->setPort("\\D", sig_d);
1696  cell->setPort("\\Q", sig_q);
1697  return cell;
1698 }
1699 
1701  RTLIL::SigSpec sig_d, RTLIL::SigSpec sig_q, bool clk_polarity, bool set_polarity, bool clr_polarity)
1702 {
1703  RTLIL::Cell *cell = addCell(name, stringf("$_DFFSR_%c%c%c_", clk_polarity ? 'P' : 'N', set_polarity ? 'P' : 'N', clr_polarity ? 'P' : 'N'));
1704  cell->setPort("\\C", sig_clk);
1705  cell->setPort("\\S", sig_set);
1706  cell->setPort("\\R", sig_clr);
1707  cell->setPort("\\D", sig_d);
1708  cell->setPort("\\Q", sig_q);
1709  return cell;
1710 }
1711 
1713  bool arst_value, bool clk_polarity, bool arst_polarity)
1714 {
1715  RTLIL::Cell *cell = addCell(name, stringf("$_DFF_%c%c%c_", clk_polarity ? 'P' : 'N', arst_polarity ? 'P' : 'N', arst_value ? '1' : '0'));
1716  cell->setPort("\\C", sig_clk);
1717  cell->setPort("\\R", sig_arst);
1718  cell->setPort("\\D", sig_d);
1719  cell->setPort("\\Q", sig_q);
1720  return cell;
1721 }
1722 
1724 {
1725  RTLIL::Cell *cell = addCell(name, stringf("$_DLATCH_%c_", en_polarity ? 'P' : 'N'));
1726  cell->setPort("\\E", sig_en);
1727  cell->setPort("\\D", sig_d);
1728  cell->setPort("\\Q", sig_q);
1729  return cell;
1730 }
1731 
1733  RTLIL::SigSpec sig_d, RTLIL::SigSpec sig_q, bool en_polarity, bool set_polarity, bool clr_polarity)
1734 {
1735  RTLIL::Cell *cell = addCell(name, stringf("$_DLATCHSR_%c%c%c_", en_polarity ? 'P' : 'N', set_polarity ? 'P' : 'N', clr_polarity ? 'P' : 'N'));
1736  cell->setPort("\\E", sig_en);
1737  cell->setPort("\\S", sig_set);
1738  cell->setPort("\\R", sig_clr);
1739  cell->setPort("\\D", sig_d);
1740  cell->setPort("\\Q", sig_q);
1741  return cell;
1742 }
1743 
1744 
1746 {
1747  module = nullptr;
1748  width = 1;
1749  start_offset = 0;
1750  port_id = 0;
1751  port_input = false;
1752  port_output = false;
1753  upto = false;
1754 }
1755 
1757 {
1758  width = 1;
1759  size = 0;
1760 }
1761 
1763 {
1764 }
1765 
1767 {
1768  return connections_.count(portname) != 0;
1769 }
1770 
1772 {
1773  RTLIL::SigSpec signal;
1774  auto conn_it = connections_.find(portname);
1775 
1776  if (conn_it != connections_.end())
1777  {
1778  for (auto mon : module->monitors)
1779  mon->notify_connect(this, conn_it->first, conn_it->second, signal);
1780 
1781  if (module->design)
1782  for (auto mon : module->design->monitors)
1783  mon->notify_connect(this, conn_it->first, conn_it->second, signal);
1784 
1785  connections_.erase(conn_it);
1786  }
1787 }
1788 
1790 {
1791  auto conn_it = connections_.find(portname);
1792 
1793  if (conn_it == connections_.end()) {
1794  connections_[portname] = RTLIL::SigSpec();
1795  conn_it = connections_.find(portname);
1796  log_assert(conn_it != connections_.end());
1797  }
1798 
1799  for (auto mon : module->monitors)
1800  mon->notify_connect(this, conn_it->first, conn_it->second, signal);
1801 
1802  if (module->design)
1803  for (auto mon : module->design->monitors)
1804  mon->notify_connect(this, conn_it->first, conn_it->second, signal);
1805 
1806  conn_it->second = signal;
1807 }
1808 
1810 {
1811  return connections_.at(portname);
1812 }
1813 
1814 const std::map<RTLIL::IdString, RTLIL::SigSpec> &RTLIL::Cell::connections() const
1815 {
1816  return connections_;
1817 }
1818 
1820 {
1821  return parameters.count(paramname) != 0;
1822 }
1823 
1825 {
1826  parameters.erase(paramname);
1827 }
1828 
1830 {
1831  parameters[paramname] = value;
1832 }
1833 
1835 {
1836  return parameters.at(paramname);
1837 }
1838 
1840 {
1841 #ifndef NDEBUG
1842  InternalCellChecker checker(NULL, this);
1843  checker.check();
1844 #endif
1845 }
1846 
1847 void RTLIL::Cell::fixup_parameters(bool set_a_signed, bool set_b_signed)
1848 {
1849  if (type.substr(0, 1) != "$" || type.substr(0, 2) == "$_" || type.substr(0, 8) == "$paramod" ||
1850  type.substr(0, 9) == "$verific$" || type.substr(0, 7) == "$array:" || type.substr(0, 8) == "$extern:")
1851  return;
1852 
1853  if (type == "$mux" || type == "$pmux") {
1854  parameters["\\WIDTH"] = GetSize(connections_["\\Y"]);
1855  if (type == "$pmux")
1856  parameters["\\S_WIDTH"] = GetSize(connections_["\\S"]);
1857  check();
1858  return;
1859  }
1860 
1861  if (type == "$lut") {
1862  parameters["\\WIDTH"] = GetSize(connections_["\\A"]);
1863  return;
1864  }
1865 
1866  if (type == "$fa") {
1867  parameters["\\WIDTH"] = GetSize(connections_["\\Y"]);
1868  return;
1869  }
1870 
1871  if (type == "$lcu") {
1872  parameters["\\WIDTH"] = GetSize(connections_["\\CO"]);
1873  return;
1874  }
1875 
1876  bool signedness_ab = !type.in("$slice", "$concat", "$macc");
1877 
1878  if (connections_.count("\\A")) {
1879  if (signedness_ab) {
1880  if (set_a_signed)
1881  parameters["\\A_SIGNED"] = true;
1882  else if (parameters.count("\\A_SIGNED") == 0)
1883  parameters["\\A_SIGNED"] = false;
1884  }
1885  parameters["\\A_WIDTH"] = GetSize(connections_["\\A"]);
1886  }
1887 
1888  if (connections_.count("\\B")) {
1889  if (signedness_ab) {
1890  if (set_b_signed)
1891  parameters["\\B_SIGNED"] = true;
1892  else if (parameters.count("\\B_SIGNED") == 0)
1893  parameters["\\B_SIGNED"] = false;
1894  }
1895  parameters["\\B_WIDTH"] = GetSize(connections_["\\B"]);
1896  }
1897 
1898  if (connections_.count("\\Y"))
1899  parameters["\\Y_WIDTH"] = GetSize(connections_["\\Y"]);
1900 
1901  check();
1902 }
1903 
1905 {
1906  wire = NULL;
1907  width = 0;
1908  offset = 0;
1909 }
1910 
1912 {
1913  wire = NULL;
1914  data = value.bits;
1915  width = GetSize(data);
1916  offset = 0;
1917 }
1918 
1920 {
1921  log_assert(wire != nullptr);
1922  this->wire = wire;
1923  this->width = wire->width;
1924  this->offset = 0;
1925 }
1926 
1927 RTLIL::SigChunk::SigChunk(RTLIL::Wire *wire, int offset, int width)
1928 {
1929  log_assert(wire != nullptr);
1930  this->wire = wire;
1931  this->width = width;
1932  this->offset = offset;
1933 }
1934 
1935 RTLIL::SigChunk::SigChunk(const std::string &str)
1936 {
1937  wire = NULL;
1938  data = RTLIL::Const(str).bits;
1939  width = GetSize(data);
1940  offset = 0;
1941 }
1942 
1943 RTLIL::SigChunk::SigChunk(int val, int width)
1944 {
1945  wire = NULL;
1946  data = RTLIL::Const(val, width).bits;
1947  this->width = GetSize(data);
1948  offset = 0;
1949 }
1950 
1952 {
1953  wire = NULL;
1954  data = RTLIL::Const(bit, width).bits;
1955  this->width = GetSize(data);
1956  offset = 0;
1957 }
1958 
1960 {
1961  wire = bit.wire;
1962  offset = 0;
1963  if (wire == NULL)
1964  data = RTLIL::Const(bit.data).bits;
1965  else
1966  offset = bit.offset;
1967  width = 1;
1968 }
1969 
1970 RTLIL::SigChunk RTLIL::SigChunk::extract(int offset, int length) const
1971 {
1972  RTLIL::SigChunk ret;
1973  if (wire) {
1974  ret.wire = wire;
1975  ret.offset = this->offset + offset;
1976  ret.width = length;
1977  } else {
1978  for (int i = 0; i < length; i++)
1979  ret.data.push_back(data[offset+i]);
1980  ret.width = length;
1981  }
1982  return ret;
1983 }
1984 
1986 {
1987  if (wire && other.wire)
1988  if (wire->name != other.wire->name)
1989  return wire->name < other.wire->name;
1990 
1991  if (wire != other.wire)
1992  return wire < other.wire;
1993 
1994  if (offset != other.offset)
1995  return offset < other.offset;
1996 
1997  if (width != other.width)
1998  return width < other.width;
1999 
2000  return data < other.data;
2001 }
2002 
2004 {
2005  return wire == other.wire && width == other.width && offset == other.offset && data == other.data;
2006 }
2007 
2009 {
2010  if (*this == other)
2011  return false;
2012  return true;
2013 }
2014 
2016 {
2017  width_ = 0;
2018  hash_ = 0;
2019 }
2020 
2022 {
2023  *this = other;
2024 }
2025 
2026 RTLIL::SigSpec::SigSpec(std::initializer_list<RTLIL::SigSpec> parts)
2027 {
2028  cover("kernel.rtlil.sigspec.init.list");
2029 
2030  width_ = 0;
2031  hash_ = 0;
2032 
2033  std::vector<RTLIL::SigSpec> parts_vec(parts.begin(), parts.end());
2034  for (auto it = parts_vec.rbegin(); it != parts_vec.rend(); it++)
2035  append(*it);
2036 }
2037 
2039 {
2040  cover("kernel.rtlil.sigspec.assign");
2041 
2042  width_ = other.width_;
2043  hash_ = other.hash_;
2044  chunks_ = other.chunks_;
2045  bits_.clear();
2046 
2047  if (!other.bits_.empty())
2048  {
2049  RTLIL::SigChunk *last = NULL;
2050  int last_end_offset = 0;
2051 
2052  for (auto &bit : other.bits_) {
2053  if (last && bit.wire == last->wire) {
2054  if (bit.wire == NULL) {
2055  last->data.push_back(bit.data);
2056  last->width++;
2057  continue;
2058  } else if (last_end_offset == bit.offset) {
2059  last_end_offset++;
2060  last->width++;
2061  continue;
2062  }
2063  }
2064  chunks_.push_back(bit);
2065  last = &chunks_.back();
2066  last_end_offset = bit.offset + 1;
2067  }
2068 
2069  check();
2070  }
2071 
2072  return *this;
2073 }
2074 
2076 {
2077  cover("kernel.rtlil.sigspec.init.const");
2078 
2079  chunks_.push_back(RTLIL::SigChunk(value));
2080  width_ = chunks_.back().width;
2081  hash_ = 0;
2082  check();
2083 }
2084 
2086 {
2087  cover("kernel.rtlil.sigspec.init.chunk");
2088 
2089  chunks_.push_back(chunk);
2090  width_ = chunks_.back().width;
2091  hash_ = 0;
2092  check();
2093 }
2094 
2096 {
2097  cover("kernel.rtlil.sigspec.init.wire");
2098 
2099  chunks_.push_back(RTLIL::SigChunk(wire));
2100  width_ = chunks_.back().width;
2101  hash_ = 0;
2102  check();
2103 }
2104 
2105 RTLIL::SigSpec::SigSpec(RTLIL::Wire *wire, int offset, int width)
2106 {
2107  cover("kernel.rtlil.sigspec.init.wire_part");
2108 
2109  chunks_.push_back(RTLIL::SigChunk(wire, offset, width));
2110  width_ = chunks_.back().width;
2111  hash_ = 0;
2112  check();
2113 }
2114 
2115 RTLIL::SigSpec::SigSpec(const std::string &str)
2116 {
2117  cover("kernel.rtlil.sigspec.init.str");
2118 
2119  chunks_.push_back(RTLIL::SigChunk(str));
2120  width_ = chunks_.back().width;
2121  hash_ = 0;
2122  check();
2123 }
2124 
2125 RTLIL::SigSpec::SigSpec(int val, int width)
2126 {
2127  cover("kernel.rtlil.sigspec.init.int");
2128 
2129  chunks_.push_back(RTLIL::SigChunk(val, width));
2130  width_ = width;
2131  hash_ = 0;
2132  check();
2133 }
2134 
2136 {
2137  cover("kernel.rtlil.sigspec.init.state");
2138 
2139  chunks_.push_back(RTLIL::SigChunk(bit, width));
2140  width_ = width;
2141  hash_ = 0;
2142  check();
2143 }
2144 
2146 {
2147  cover("kernel.rtlil.sigspec.init.bit");
2148 
2149  if (bit.wire == NULL)
2150  chunks_.push_back(RTLIL::SigChunk(bit.data, width));
2151  else
2152  for (int i = 0; i < width; i++)
2153  chunks_.push_back(bit);
2154  width_ = width;
2155  hash_ = 0;
2156  check();
2157 }
2158 
2159 RTLIL::SigSpec::SigSpec(std::vector<RTLIL::SigChunk> chunks)
2160 {
2161  cover("kernel.rtlil.sigspec.init.stdvec_chunks");
2162 
2163  width_ = 0;
2164  hash_ = 0;
2165  for (auto &c : chunks)
2166  append(c);
2167  check();
2168 }
2169 
2170 RTLIL::SigSpec::SigSpec(std::vector<RTLIL::SigBit> bits)
2171 {
2172  cover("kernel.rtlil.sigspec.init.stdvec_bits");
2173 
2174  width_ = 0;
2175  hash_ = 0;
2176  for (auto &bit : bits)
2177  append_bit(bit);
2178  check();
2179 }
2180 
2181 RTLIL::SigSpec::SigSpec(std::set<RTLIL::SigBit> bits)
2182 {
2183  cover("kernel.rtlil.sigspec.init.stdset_bits");
2184 
2185  width_ = 0;
2186  hash_ = 0;
2187  for (auto &bit : bits)
2188  append_bit(bit);
2189  check();
2190 }
2191 
2193 {
2194  cover("kernel.rtlil.sigspec.init.bool");
2195 
2196  width_ = 0;
2197  hash_ = 0;
2198  append_bit(bit);
2199  check();
2200 }
2201 
2203 {
2204  RTLIL::SigSpec *that = (RTLIL::SigSpec*)this;
2205 
2206  if (that->bits_.empty())
2207  return;
2208 
2209  cover("kernel.rtlil.sigspec.convert.pack");
2210  log_assert(that->chunks_.empty());
2211 
2212  std::vector<RTLIL::SigBit> old_bits;
2213  old_bits.swap(that->bits_);
2214 
2215  RTLIL::SigChunk *last = NULL;
2216  int last_end_offset = 0;
2217 
2218  for (auto &bit : old_bits) {
2219  if (last && bit.wire == last->wire) {
2220  if (bit.wire == NULL) {
2221  last->data.push_back(bit.data);
2222  last->width++;
2223  continue;
2224  } else if (last_end_offset == bit.offset) {
2225  last_end_offset++;
2226  last->width++;
2227  continue;
2228  }
2229  }
2230  that->chunks_.push_back(bit);
2231  last = &that->chunks_.back();
2232  last_end_offset = bit.offset + 1;
2233  }
2234 
2235  check();
2236 }
2237 
2239 {
2240  RTLIL::SigSpec *that = (RTLIL::SigSpec*)this;
2241 
2242  if (that->chunks_.empty())
2243  return;
2244 
2245  cover("kernel.rtlil.sigspec.convert.unpack");
2246  log_assert(that->bits_.empty());
2247 
2248  that->bits_.reserve(that->width_);
2249  for (auto &c : that->chunks_)
2250  for (int i = 0; i < c.width; i++)
2251  that->bits_.push_back(RTLIL::SigBit(c, i));
2252 
2253  that->chunks_.clear();
2254  that->hash_ = 0;
2255 }
2256 
2257 #define DJB2(_hash, _value) (_hash) = (((_hash) << 5) + (_hash)) + (_value)
2258 
2260 {
2261  RTLIL::SigSpec *that = (RTLIL::SigSpec*)this;
2262 
2263  if (that->hash_ != 0)
2264  return;
2265 
2266  cover("kernel.rtlil.sigspec.hash");
2267  that->pack();
2268 
2269  that->hash_ = 5381;
2270  for (auto &c : that->chunks_)
2271  if (c.wire == NULL) {
2272  for (auto &v : c.data)
2273  DJB2(that->hash_, v);
2274  } else {
2275  DJB2(that->hash_, c.wire->name.index_);
2276  DJB2(that->hash_, c.offset);
2277  DJB2(that->hash_, c.width);
2278  }
2279 
2280  if (that->hash_ == 0)
2281  that->hash_ = 1;
2282 }
2283 
2285 {
2286  unpack();
2287  cover("kernel.rtlil.sigspec.sort");
2288  std::sort(bits_.begin(), bits_.end());
2289 }
2290 
2292 {
2293  cover("kernel.rtlil.sigspec.sort_and_unify");
2294  *this = this->to_sigbit_set();
2295 }
2296 
2297 void RTLIL::SigSpec::replace(const RTLIL::SigSpec &pattern, const RTLIL::SigSpec &with)
2298 {
2299  replace(pattern, with, this);
2300 }
2301 
2302 void RTLIL::SigSpec::replace(const RTLIL::SigSpec &pattern, const RTLIL::SigSpec &with, RTLIL::SigSpec *other) const
2303 {
2304  log_assert(pattern.width_ == with.width_);
2305 
2306  pattern.unpack();
2307  with.unpack();
2308 
2309  std::map<RTLIL::SigBit, RTLIL::SigBit> rules;
2310 
2311  for (int i = 0; i < GetSize(pattern.bits_); i++)
2312  if (pattern.bits_[i].wire != NULL)
2313  rules[pattern.bits_[i]] = with.bits_[i];
2314 
2315  replace(rules, other);
2316 }
2317 
2318 void RTLIL::SigSpec::replace(const std::map<RTLIL::SigBit, RTLIL::SigBit> &rules)
2319 {
2320  replace(rules, this);
2321 }
2322 
2323 void RTLIL::SigSpec::replace(const std::map<RTLIL::SigBit, RTLIL::SigBit> &rules, RTLIL::SigSpec *other) const
2324 {
2325  cover("kernel.rtlil.sigspec.replace");
2326 
2327  log_assert(other != NULL);
2328  log_assert(width_ == other->width_);
2329 
2330  unpack();
2331  other->unpack();
2332 
2333  for (int i = 0; i < GetSize(bits_); i++) {
2334  auto it = rules.find(bits_[i]);
2335  if (it != rules.end())
2336  other->bits_[i] = it->second;
2337  }
2338 
2339  other->check();
2340 }
2341 
2343 {
2344  remove2(pattern, NULL);
2345 }
2346 
2347 void RTLIL::SigSpec::remove(const RTLIL::SigSpec &pattern, RTLIL::SigSpec *other) const
2348 {
2349  RTLIL::SigSpec tmp = *this;
2350  tmp.remove2(pattern, other);
2351 }
2352 
2354 {
2355  std::set<RTLIL::SigBit> pattern_bits = pattern.to_sigbit_set();
2356  remove2(pattern_bits, other);
2357 }
2358 
2359 void RTLIL::SigSpec::remove(const std::set<RTLIL::SigBit> &pattern)
2360 {
2361  remove2(pattern, NULL);
2362 }
2363 
2364 void RTLIL::SigSpec::remove(const std::set<RTLIL::SigBit> &pattern, RTLIL::SigSpec *other) const
2365 {
2366  RTLIL::SigSpec tmp = *this;
2367  tmp.remove2(pattern, other);
2368 }
2369 
2370 void RTLIL::SigSpec::remove2(const std::set<RTLIL::SigBit> &pattern, RTLIL::SigSpec *other)
2371 {
2372  if (other)
2373  cover("kernel.rtlil.sigspec.remove_other");
2374  else
2375  cover("kernel.rtlil.sigspec.remove");
2376 
2377  unpack();
2378 
2379  if (other != NULL) {
2380  log_assert(width_ == other->width_);
2381  other->unpack();
2382  }
2383 
2384  std::vector<RTLIL::SigBit> new_bits, new_other_bits;
2385 
2386  new_bits.resize(GetSize(bits_));
2387  if (other != NULL)
2388  new_other_bits.resize(GetSize(bits_));
2389 
2390  int k = 0;
2391  for (int i = 0; i < GetSize(bits_); i++) {
2392  if (bits_[i].wire != NULL && pattern.count(bits_[i]))
2393  continue;
2394  if (other != NULL)
2395  new_other_bits[k] = other->bits_[i];
2396  new_bits[k++] = bits_[i];
2397  }
2398 
2399  new_bits.resize(k);
2400  if (other != NULL)
2401  new_other_bits.resize(k);
2402 
2403  bits_.swap(new_bits);
2404  width_ = GetSize(bits_);
2405 
2406  if (other != NULL) {
2407  other->bits_.swap(new_other_bits);
2408  other->width_ = GetSize(other->bits_);
2409  }
2410 
2411  check();
2412 }
2413 
2415 {
2416  std::set<RTLIL::SigBit> pattern_bits = pattern.to_sigbit_set();
2417  return extract(pattern_bits, other);
2418 }
2419 
2420 RTLIL::SigSpec RTLIL::SigSpec::extract(const std::set<RTLIL::SigBit> &pattern, const RTLIL::SigSpec *other) const
2421 {
2422  if (other)
2423  cover("kernel.rtlil.sigspec.extract_other");
2424  else
2425  cover("kernel.rtlil.sigspec.extract");
2426 
2427  log_assert(other == NULL || width_ == other->width_);
2428 
2429  std::vector<RTLIL::SigBit> bits_match = to_sigbit_vector();
2430  RTLIL::SigSpec ret;
2431 
2432  if (other) {
2433  std::vector<RTLIL::SigBit> bits_other = other->to_sigbit_vector();
2434  for (int i = 0; i < width_; i++)
2435  if (bits_match[i].wire && pattern.count(bits_match[i]))
2436  ret.append_bit(bits_other[i]);
2437  } else {
2438  for (int i = 0; i < width_; i++)
2439  if (bits_match[i].wire && pattern.count(bits_match[i]))
2440  ret.append_bit(bits_match[i]);
2441  }
2442 
2443  ret.check();
2444  return ret;
2445 }
2446 
2447 void RTLIL::SigSpec::replace(int offset, const RTLIL::SigSpec &with)
2448 {
2449  cover("kernel.rtlil.sigspec.replace_pos");
2450 
2451  unpack();
2452  with.unpack();
2453 
2454  log_assert(offset >= 0);
2455  log_assert(with.width_ >= 0);
2456  log_assert(offset+with.width_ <= width_);
2457 
2458  for (int i = 0; i < with.width_; i++)
2459  bits_.at(offset + i) = with.bits_.at(i);
2460 
2461  check();
2462 }
2463 
2465 {
2466  if (packed())
2467  {
2468  cover("kernel.rtlil.sigspec.remove_const.packed");
2469 
2470  std::vector<RTLIL::SigChunk> new_chunks;
2471  new_chunks.reserve(GetSize(chunks_));
2472 
2473  width_ = 0;
2474  for (auto &chunk : chunks_)
2475  if (chunk.wire != NULL) {
2476  new_chunks.push_back(chunk);
2477  width_ += chunk.width;
2478  }
2479 
2480  chunks_.swap(new_chunks);
2481  }
2482  else
2483  {
2484  cover("kernel.rtlil.sigspec.remove_const.unpacked");
2485 
2486  std::vector<RTLIL::SigBit> new_bits;
2487  new_bits.reserve(width_);
2488 
2489  for (auto &bit : bits_)
2490  if (bit.wire != NULL)
2491  new_bits.push_back(bit);
2492 
2493  bits_.swap(new_bits);
2494  width_ = bits_.size();
2495  }
2496 
2497  check();
2498 }
2499 
2500 void RTLIL::SigSpec::remove(int offset, int length)
2501 {
2502  cover("kernel.rtlil.sigspec.remove_pos");
2503 
2504  unpack();
2505 
2506  log_assert(offset >= 0);
2507  log_assert(length >= 0);
2508  log_assert(offset + length <= width_);
2509 
2510  bits_.erase(bits_.begin() + offset, bits_.begin() + offset + length);
2511  width_ = bits_.size();
2512 
2513  check();
2514 }
2515 
2516 RTLIL::SigSpec RTLIL::SigSpec::extract(int offset, int length) const
2517 {
2518  unpack();
2519  cover("kernel.rtlil.sigspec.extract_pos");
2520  return std::vector<RTLIL::SigBit>(bits_.begin() + offset, bits_.begin() + offset + length);
2521 }
2522 
2524 {
2525  if (signal.width_ == 0)
2526  return;
2527 
2528  if (width_ == 0) {
2529  *this = signal;
2530  return;
2531  }
2532 
2533  cover("kernel.rtlil.sigspec.append");
2534 
2535  if (packed() != signal.packed()) {
2536  pack();
2537  signal.pack();
2538  }
2539 
2540  if (packed())
2541  for (auto &other_c : signal.chunks_)
2542  {
2543  auto &my_last_c = chunks_.back();
2544  if (my_last_c.wire == NULL && other_c.wire == NULL) {
2545  auto &this_data = my_last_c.data;
2546  auto &other_data = other_c.data;
2547  this_data.insert(this_data.end(), other_data.begin(), other_data.end());
2548  my_last_c.width += other_c.width;
2549  } else
2550  if (my_last_c.wire == other_c.wire && my_last_c.offset + my_last_c.width == other_c.offset) {
2551  my_last_c.width += other_c.width;
2552  } else
2553  chunks_.push_back(other_c);
2554  }
2555  else
2556  bits_.insert(bits_.end(), signal.bits_.begin(), signal.bits_.end());
2557 
2558  width_ += signal.width_;
2559  check();
2560 }
2561 
2563 {
2564  if (packed())
2565  {
2566  cover("kernel.rtlil.sigspec.append_bit.packed");
2567 
2568  if (chunks_.size() == 0)
2569  chunks_.push_back(bit);
2570  else
2571  if (bit.wire == NULL)
2572  if (chunks_.back().wire == NULL) {
2573  chunks_.back().data.push_back(bit.data);
2574  chunks_.back().width++;
2575  } else
2576  chunks_.push_back(bit);
2577  else
2578  if (chunks_.back().wire == bit.wire && chunks_.back().offset + chunks_.back().width == bit.offset)
2579  chunks_.back().width++;
2580  else
2581  chunks_.push_back(bit);
2582  }
2583  else
2584  {
2585  cover("kernel.rtlil.sigspec.append_bit.unpacked");
2586  bits_.push_back(bit);
2587  }
2588 
2589  width_++;
2590  check();
2591 }
2592 
2593 void RTLIL::SigSpec::extend(int width, bool is_signed)
2594 {
2595  cover("kernel.rtlil.sigspec.extend");
2596 
2597  pack();
2598 
2599  if (width_ > width)
2600  remove(width, width_ - width);
2601 
2602  if (width_ < width) {
2603  RTLIL::SigSpec padding = width_ > 0 ? extract(width_ - 1, 1) : RTLIL::SigSpec(RTLIL::State::S0);
2604  if (!is_signed && padding != RTLIL::SigSpec(RTLIL::State::Sx) && padding != RTLIL::SigSpec(RTLIL::State::Sz) &&
2606  padding = RTLIL::SigSpec(RTLIL::State::S0);
2607  while (width_ < width)
2608  append(padding);
2609  }
2610 }
2611 
2612 void RTLIL::SigSpec::extend_u0(int width, bool is_signed)
2613 {
2614  cover("kernel.rtlil.sigspec.extend_u0");
2615 
2616  pack();
2617 
2618  if (width_ > width)
2619  remove(width, width_ - width);
2620 
2621  if (width_ < width) {
2622  RTLIL::SigSpec padding = width_ > 0 ? extract(width_ - 1, 1) : RTLIL::SigSpec(RTLIL::State::S0);
2623  if (!is_signed)
2624  padding = RTLIL::SigSpec(RTLIL::State::S0);
2625  while (width_ < width)
2626  append(padding);
2627  }
2628 
2629 }
2630 
2632 {
2633  cover("kernel.rtlil.sigspec.repeat");
2634 
2635  RTLIL::SigSpec sig;
2636  for (int i = 0; i < num; i++)
2637  sig.append(*this);
2638  return sig;
2639 }
2640 
2641 #ifndef NDEBUG
2643 {
2644  if (width_ > 64)
2645  {
2646  cover("kernel.rtlil.sigspec.check.skip");
2647  }
2648  else if (packed())
2649  {
2650  cover("kernel.rtlil.sigspec.check.packed");
2651 
2652  int w = 0;
2653  for (size_t i = 0; i < chunks_.size(); i++) {
2654  const RTLIL::SigChunk chunk = chunks_[i];
2655  if (chunk.wire == NULL) {
2656  if (i > 0)
2657  log_assert(chunks_[i-1].wire != NULL);
2658  log_assert(chunk.offset == 0);
2659  log_assert(chunk.data.size() == (size_t)chunk.width);
2660  } else {
2661  if (i > 0 && chunks_[i-1].wire == chunk.wire)
2662  log_assert(chunk.offset != chunks_[i-1].offset + chunks_[i-1].width);
2663  log_assert(chunk.offset >= 0);
2664  log_assert(chunk.width >= 0);
2665  log_assert(chunk.offset + chunk.width <= chunk.wire->width);
2666  log_assert(chunk.data.size() == 0);
2667  }
2668  w += chunk.width;
2669  }
2670  log_assert(w == width_);
2671  log_assert(bits_.empty());
2672  }
2673  else
2674  {
2675  cover("kernel.rtlil.sigspec.check.unpacked");
2676 
2677  log_assert(width_ == GetSize(bits_));
2678  log_assert(chunks_.empty());
2679  }
2680 }
2681 #endif
2682 
2684 {
2685  cover("kernel.rtlil.sigspec.comp_lt");
2686 
2687  if (this == &other)
2688  return false;
2689 
2690  if (width_ != other.width_)
2691  return width_ < other.width_;
2692 
2693  pack();
2694  other.pack();
2695 
2696  if (chunks_.size() != other.chunks_.size())
2697  return chunks_.size() < other.chunks_.size();
2698 
2699  hash();
2700  other.hash();
2701 
2702  if (hash_ != other.hash_)
2703  return hash_ < other.hash_;
2704 
2705  for (size_t i = 0; i < chunks_.size(); i++)
2706  if (chunks_[i] != other.chunks_[i]) {
2707  cover("kernel.rtlil.sigspec.comp_lt.hash_collision");
2708  return chunks_[i] < other.chunks_[i];
2709  }
2710 
2711  cover("kernel.rtlil.sigspec.comp_lt.equal");
2712  return false;
2713 }
2714 
2716 {
2717  cover("kernel.rtlil.sigspec.comp_eq");
2718 
2719  if (this == &other)
2720  return true;
2721 
2722  if (width_ != other.width_)
2723  return false;
2724 
2725  pack();
2726  other.pack();
2727 
2728  if (chunks_.size() != chunks_.size())
2729  return false;
2730 
2731  hash();
2732  other.hash();
2733 
2734  if (hash_ != other.hash_)
2735  return false;
2736 
2737  for (size_t i = 0; i < chunks_.size(); i++)
2738  if (chunks_[i] != other.chunks_[i]) {
2739  cover("kernel.rtlil.sigspec.comp_eq.hash_collision");
2740  return false;
2741  }
2742 
2743  cover("kernel.rtlil.sigspec.comp_eq.equal");
2744  return true;
2745 }
2746 
2748 {
2749  cover("kernel.rtlil.sigspec.is_wire");
2750 
2751  pack();
2752  return GetSize(chunks_) == 1 && chunks_[0].wire && chunks_[0].wire->width == width_;
2753 }
2754 
2756 {
2757  cover("kernel.rtlil.sigspec.is_chunk");
2758 
2759  pack();
2760  return GetSize(chunks_) == 1;
2761 }
2762 
2764 {
2765  cover("kernel.rtlil.sigspec.is_fully_const");
2766 
2767  pack();
2768  for (auto it = chunks_.begin(); it != chunks_.end(); it++)
2769  if (it->width > 0 && it->wire != NULL)
2770  return false;
2771  return true;
2772 }
2773 
2775 {
2776  cover("kernel.rtlil.sigspec.is_fully_def");
2777 
2778  pack();
2779  for (auto it = chunks_.begin(); it != chunks_.end(); it++) {
2780  if (it->width > 0 && it->wire != NULL)
2781  return false;
2782  for (size_t i = 0; i < it->data.size(); i++)
2783  if (it->data[i] != RTLIL::State::S0 && it->data[i] != RTLIL::State::S1)
2784  return false;
2785  }
2786  return true;
2787 }
2788 
2790 {
2791  cover("kernel.rtlil.sigspec.is_fully_undef");
2792 
2793  pack();
2794  for (auto it = chunks_.begin(); it != chunks_.end(); it++) {
2795  if (it->width > 0 && it->wire != NULL)
2796  return false;
2797  for (size_t i = 0; i < it->data.size(); i++)
2798  if (it->data[i] != RTLIL::State::Sx && it->data[i] != RTLIL::State::Sz)
2799  return false;
2800  }
2801  return true;
2802 }
2803 
2805 {
2806  cover("kernel.rtlil.sigspec.has_marked_bits");
2807 
2808  pack();
2809  for (auto it = chunks_.begin(); it != chunks_.end(); it++)
2810  if (it->width > 0 && it->wire == NULL) {
2811  for (size_t i = 0; i < it->data.size(); i++)
2812  if (it->data[i] == RTLIL::State::Sm)
2813  return true;
2814  }
2815  return false;
2816 }
2817 
2819 {
2820  cover("kernel.rtlil.sigspec.as_bool");
2821 
2822  pack();
2823  log_assert(is_fully_const() && GetSize(chunks_) <= 1);
2824  if (width_)
2825  return RTLIL::Const(chunks_[0].data).as_bool();
2826  return false;
2827 }
2828 
2829 int RTLIL::SigSpec::as_int(bool is_signed) const
2830 {
2831  cover("kernel.rtlil.sigspec.as_int");
2832 
2833  pack();
2834  log_assert(is_fully_const() && GetSize(chunks_) <= 1);
2835  if (width_)
2836  return RTLIL::Const(chunks_[0].data).as_int(is_signed);
2837  return 0;
2838 }
2839 
2840 std::string RTLIL::SigSpec::as_string() const
2841 {
2842  cover("kernel.rtlil.sigspec.as_string");
2843 
2844  pack();
2845  std::string str;
2846  for (size_t i = chunks_.size(); i > 0; i--) {
2847  const RTLIL::SigChunk &chunk = chunks_[i-1];
2848  if (chunk.wire != NULL)
2849  for (int j = 0; j < chunk.width; j++)
2850  str += "?";
2851  else
2852  str += RTLIL::Const(chunk.data).as_string();
2853  }
2854  return str;
2855 }
2856 
2858 {
2859  cover("kernel.rtlil.sigspec.as_const");
2860 
2861  pack();
2862  log_assert(is_fully_const() && GetSize(chunks_) <= 1);
2863  if (width_)
2864  return chunks_[0].data;
2865  return RTLIL::Const();
2866 }
2867 
2869 {
2870  cover("kernel.rtlil.sigspec.as_wire");
2871 
2872  pack();
2873  log_assert(is_wire());
2874  return chunks_[0].wire;
2875 }
2876 
2878 {
2879  cover("kernel.rtlil.sigspec.as_chunk");
2880 
2881  pack();
2882  log_assert(is_chunk());
2883  return chunks_[0];
2884 }
2885 
2886 bool RTLIL::SigSpec::match(std::string pattern) const
2887 {
2888  cover("kernel.rtlil.sigspec.match");
2889 
2890  pack();
2891  std::string str = as_string();
2892  log_assert(pattern.size() == str.size());
2893 
2894  for (size_t i = 0; i < pattern.size(); i++) {
2895  if (pattern[i] == ' ')
2896  continue;
2897  if (pattern[i] == '*') {
2898  if (str[i] != 'z' && str[i] != 'x')
2899  return false;
2900  continue;
2901  }
2902  if (pattern[i] != str[i])
2903  return false;
2904  }
2905 
2906  return true;
2907 }
2908 
2909 std::set<RTLIL::SigBit> RTLIL::SigSpec::to_sigbit_set() const
2910 {
2911  cover("kernel.rtlil.sigspec.to_sigbit_set");
2912 
2913  pack();
2914  std::set<RTLIL::SigBit> sigbits;
2915  for (auto &c : chunks_)
2916  for (int i = 0; i < c.width; i++)
2917  sigbits.insert(RTLIL::SigBit(c, i));
2918  return sigbits;
2919 }
2920 
2921 std::vector<RTLIL::SigBit> RTLIL::SigSpec::to_sigbit_vector() const
2922 {
2923  cover("kernel.rtlil.sigspec.to_sigbit_vector");
2924 
2925  unpack();
2926  return bits_;
2927 }
2928 
2929 std::map<RTLIL::SigBit, RTLIL::SigBit> RTLIL::SigSpec::to_sigbit_map(const RTLIL::SigSpec &other) const
2930 {
2931  cover("kernel.rtlil.sigspec.to_sigbit_map");
2932 
2933  unpack();
2934  other.unpack();
2935 
2936  log_assert(width_ == other.width_);
2937 
2938  std::map<RTLIL::SigBit, RTLIL::SigBit> new_map;
2939  for (int i = 0; i < width_; i++)
2940  new_map[bits_[i]] = other.bits_[i];
2941 
2942  return new_map;
2943 }
2944 
2946 {
2947  cover("kernel.rtlil.sigspec.to_single_sigbit");
2948 
2949  pack();
2950  log_assert(width_ == 1);
2951  for (auto &c : chunks_)
2952  if (c.width)
2953  return RTLIL::SigBit(c);
2954  log_abort();
2955 }
2956 
2957 static void sigspec_parse_split(std::vector<std::string> &tokens, const std::string &text, char sep)
2958 {
2959  size_t start = 0, end = 0;
2960  while ((end = text.find(sep, start)) != std::string::npos) {
2961  tokens.push_back(text.substr(start, end - start));
2962  start = end + 1;
2963  }
2964  tokens.push_back(text.substr(start));
2965 }
2966 
2968 {
2969  return 0;
2970 }
2971 
2973 {
2974  cover("kernel.rtlil.sigspec.parse");
2975 
2976  std::vector<std::string> tokens;
2977  sigspec_parse_split(tokens, str, ',');
2978 
2979  sig = RTLIL::SigSpec();
2980  for (int tokidx = int(tokens.size())-1; tokidx >= 0; tokidx--)
2981  {
2982  std::string netname = tokens[tokidx];
2983  std::string indices;
2984 
2985  if (netname.size() == 0)
2986  continue;
2987 
2988  if (('0' <= netname[0] && netname[0] <= '9') || netname[0] == '\'') {
2989  cover("kernel.rtlil.sigspec.parse.const");
2991  AST::AstNode *ast = VERILOG_FRONTEND::const2ast(netname);
2992  if (ast == NULL)
2993  return false;
2994  sig.append(RTLIL::Const(ast->bits));
2995  delete ast;
2996  continue;
2997  }
2998 
2999  if (module == NULL)
3000  return false;
3001 
3002  cover("kernel.rtlil.sigspec.parse.net");
3003 
3004  if (netname[0] != '$' && netname[0] != '\\')
3005  netname = "\\" + netname;
3006 
3007  if (module->wires_.count(netname) == 0) {
3008  size_t indices_pos = netname.size()-1;
3009  if (indices_pos > 2 && netname[indices_pos] == ']')
3010  {
3011  indices_pos--;
3012  while (indices_pos > 0 && ('0' <= netname[indices_pos] && netname[indices_pos] <= '9')) indices_pos--;
3013  if (indices_pos > 0 && netname[indices_pos] == ':') {
3014  indices_pos--;
3015  while (indices_pos > 0 && ('0' <= netname[indices_pos] && netname[indices_pos] <= '9')) indices_pos--;
3016  }
3017  if (indices_pos > 0 && netname[indices_pos] == '[') {
3018  indices = netname.substr(indices_pos);
3019  netname = netname.substr(0, indices_pos);
3020  }
3021  }
3022  }
3023 
3024  if (module->wires_.count(netname) == 0)
3025  return false;
3026 
3027  RTLIL::Wire *wire = module->wires_.at(netname);
3028  if (!indices.empty()) {
3029  std::vector<std::string> index_tokens;
3030  sigspec_parse_split(index_tokens, indices.substr(1, indices.size()-2), ':');
3031  if (index_tokens.size() == 1) {
3032  cover("kernel.rtlil.sigspec.parse.bit_sel");
3033  int a = atoi(index_tokens.at(0).c_str());
3034  if (a < 0 || a >= wire->width)
3035  return false;
3036  sig.append(RTLIL::SigSpec(wire, a));
3037  } else {
3038  cover("kernel.rtlil.sigspec.parse.part_sel");
3039  int a = atoi(index_tokens.at(0).c_str());
3040  int b = atoi(index_tokens.at(1).c_str());
3041  if (a > b) {
3042  int tmp = a;
3043  a = b, b = tmp;
3044  }
3045  if (a < 0 || a >= wire->width)
3046  return false;
3047  if (b < 0 || b >= wire->width)
3048  return false;
3049  sig.append(RTLIL::SigSpec(wire, a, b-a+1));
3050  }
3051  } else
3052  sig.append(wire);
3053  }
3054 
3055  return true;
3056 }
3057 
3059 {
3060  if (str.empty() || str[0] != '@')
3061  return parse(sig, module, str);
3062 
3063  cover("kernel.rtlil.sigspec.parse.sel");
3064 
3065  str = RTLIL::escape_id(str.substr(1));
3066  if (design->selection_vars.count(str) == 0)
3067  return false;
3068 
3069  sig = RTLIL::SigSpec();
3070  RTLIL::Selection &sel = design->selection_vars.at(str);
3071  for (auto &it : module->wires_)
3072  if (sel.selected_member(module->name, it.first))
3073  sig.append(it.second);
3074 
3075  return true;
3076 }
3077 
3079 {
3080  if (str == "0") {
3081  cover("kernel.rtlil.sigspec.parse.rhs_zeros");
3083  return true;
3084  }
3085 
3086  if (str == "~0") {
3087  cover("kernel.rtlil.sigspec.parse.rhs_ones");
3089  return true;
3090  }
3091 
3092  if (lhs.chunks_.size() == 1) {
3093  char *p = (char*)str.c_str(), *endptr;
3094  long int val = strtol(p, &endptr, 10);
3095  if (endptr && endptr != p && *endptr == 0) {
3096  sig = RTLIL::SigSpec(val, lhs.width_);
3097  cover("kernel.rtlil.sigspec.parse.rhs_dec");
3098  return true;
3099  }
3100  }
3101 
3102  return parse(sig, module, str);
3103 }
3104 
3106 {
3107  for (auto it = switches.begin(); it != switches.end(); it++)
3108  delete *it;
3109 }
3110 
3112 {
3113  RTLIL::CaseRule *new_caserule = new RTLIL::CaseRule;
3114  new_caserule->compare = compare;
3115  new_caserule->actions = actions;
3116  for (auto &it : switches)
3117  new_caserule->switches.push_back(it->clone());
3118  return new_caserule;
3119 }
3120 
3122 {
3123  for (auto it = cases.begin(); it != cases.end(); it++)
3124  delete *it;
3125 }
3126 
3128 {
3129  RTLIL::SwitchRule *new_switchrule = new RTLIL::SwitchRule;
3130  new_switchrule->signal = signal;
3131  new_switchrule->attributes = attributes;
3132  for (auto &it : cases)
3133  new_switchrule->cases.push_back(it->clone());
3134  return new_switchrule;
3135 
3136 }
3137 
3139 {
3140  RTLIL::SyncRule *new_syncrule = new RTLIL::SyncRule;
3141  new_syncrule->type = type;
3142  new_syncrule->signal = signal;
3143  new_syncrule->actions = actions;
3144  return new_syncrule;
3145 }
3146 
3148 {
3149  for (auto it = syncs.begin(); it != syncs.end(); it++)
3150  delete *it;
3151 }
3152 
3154 {
3155  RTLIL::Process *new_proc = new RTLIL::Process;
3156 
3157  new_proc->name = name;
3158  new_proc->attributes = attributes;
3159 
3160  RTLIL::CaseRule *rc_ptr = root_case.clone();
3161  new_proc->root_case = *rc_ptr;
3162  rc_ptr->switches.clear();
3163  delete rc_ptr;
3164 
3165  for (auto &it : syncs)
3166  new_proc->syncs.push_back(it->clone());
3167 
3168  return new_proc;
3169 }
3170 
3172 
bool selected_module(RTLIL::IdString mod_name) const
Definition: rtlil.cc:148
void fixup_parameters(bool set_a_signed=false, bool set_b_signed=false)
Definition: rtlil.cc:1847
const char * c_str() const
Definition: rtlil.h:178
virtual void optimize()
Definition: rtlil.cc:1018
RTLIL::Wire * wire
Definition: rtlil.h:907
bool as_bool() const
Definition: rtlil.cc:96
bool is_fully_def() const
Definition: rtlil.cc:2774
void cloneInto(RTLIL::Module *new_mod) const
Definition: rtlil.cc:1022
void dump_cell(std::ostream &f, std::string indent, const RTLIL::Cell *cell)
bool is_fully_undef() const
Definition: rtlil.cc:2789
static void append(const vec< T > &from, vec< T > &to)
Definition: Alg.h:79
void unsetPort(RTLIL::IdString portname)
Definition: rtlil.cc:1771
RTLIL::Cell * addSlice(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_y, RTLIL::Const offset)
Definition: rtlil.cc:1547
#define cover(...)
Definition: log.h:131
void scratchpad_set_int(std::string varname, int value)
Definition: rtlil.cc:291
void sort()
Definition: rtlil.cc:2284
bool selected_whole_module(RTLIL::IdString mod_name) const
Definition: rtlil.cc:159
std::string stringf(const char *fmt,...)
Definition: yosys.cc:58
RTLIL::State data
Definition: rtlil.h:909
void sort(T *array, int size, LessThan lt)
Definition: Sort.h:57
Definition: macc.h:27
void new_connections(const std::vector< RTLIL::SigSig > &new_conn)
Definition: rtlil.cc:1295
void log_warning(const char *format,...)
Definition: log.cc:196
#define DEF_METHOD_3(_func, _type, _P1, _P2, _P3)
bool selected_module(RTLIL::IdString mod_name) const
Definition: rtlil.cc:379
RTLIL::Cell * addCell(RTLIL::IdString name, RTLIL::IdString type)
Definition: rtlil.cc:1353
void add(RTLIL::Module *module)
Definition: rtlil.cc:259
bool selected_member(RTLIL::IdString mod_name, RTLIL::IdString memb_name) const
Definition: rtlil.cc:168
std::set< RTLIL::Monitor * > monitors
Definition: rtlil.h:590
static bool parse_sel(RTLIL::SigSpec &sig, RTLIL::Design *design, RTLIL::Module *module, std::string str)
Definition: rtlil.cc:3058
bool clk_polarity
Definition: abc.cc:98
int flags
Definition: rtlil.h:437
void setParam(RTLIL::IdString paramname, RTLIL::Const value)
Definition: rtlil.cc:1829
RTLIL::Cell * addDffGate(RTLIL::IdString name, RTLIL::SigSpec sig_clk, RTLIL::SigSpec sig_d, RTLIL::SigSpec sig_q, bool clk_polarity=true)
Definition: rtlil.cc:1681
const std::vector< RTLIL::SigSig > & connections() const
Definition: rtlil.cc:1307
#define YOSYS_NAMESPACE_END
Definition: yosys.h:100
RTLIL::Const as_const() const
Definition: rtlil.cc:2857
#define DJB2(_hash, _value)
Definition: rtlil.cc:2257
RTLIL::SyncType type
Definition: rtlil.h:1144
std::map< RTLIL::IdString, RTLIL::Wire * > wires_
Definition: rtlil.h:595
bool as_bool() const
Definition: rtlil.cc:2818
void rewrite_sigspecs(T functor)
Definition: rtlil.h:1166
std::set< RTLIL::SigBit > to_sigbit_set() const
Definition: rtlil.cc:2909
void scratchpad_unset(std::string varname)
Definition: rtlil.cc:286
RTLIL::SigChunk extract(int offset, int length) const
Definition: rtlil.cc:1970
unsigned long hash_
Definition: rtlil.h:965
RTLIL_ATTRIBUTE_MEMBERS std::vector< RTLIL::CaseRule * > cases
Definition: rtlil.h:1134
void unpack() const
Definition: rtlil.cc:2238
RTLIL::IdString name
Definition: rtlil.h:853
bool upto
Definition: rtlil.h:827
RTLIL::Cell * addDlatchGate(RTLIL::IdString name, RTLIL::SigSpec sig_en, RTLIL::SigSpec sig_d, RTLIL::SigSpec sig_q, bool en_polarity=true)
Definition: rtlil.cc:1723
void setPort(RTLIL::IdString portname, RTLIL::SigSpec signal)
Definition: rtlil.cc:1789
bool port_input
Definition: rtlil.h:827
RTLIL::SyncRule * clone() const
Definition: rtlil.cc:3138
int width
Definition: rtlil.h:826
void check()
Definition: rtlil.cc:357
std::map< RTLIL::IdString, RTLIL::Memory * > memories
Definition: rtlil.h:601
RTLIL::Cell * addLut(RTLIL::IdString name, RTLIL::SigSpec sig_i, RTLIL::SigSpec sig_o, RTLIL::Const lut)
Definition: rtlil.cc:1569
void unsetParam(RTLIL::IdString paramname)
Definition: rtlil.cc:1824
std::vector< RTLIL::SigBit > bits_
Definition: rtlil.h:967
void log_error(const char *format,...)
Definition: log.cc:204
RTLIL::Module * module
Definition: abc.cc:94
void swap_names(RTLIL::Wire *w1, RTLIL::Wire *w2)
Definition: rtlil.cc:1214
int port_id
Definition: rtlil.h:826
RTLIL::IdString type
Definition: rtlil.h:854
std::string scratchpad_get_string(std::string varname, std::string default_value=std::string()) const
Definition: rtlil.cc:340
std::map< RTLIL::IdString, RTLIL::Const > parameters
Definition: rtlil.h:856
void extend_u0(int width, bool is_signed=false)
Definition: rtlil.cc:2612
RTLIL::SigSpec signal
Definition: rtlil.h:1145
int size() const
Definition: rtlil.h:1019
void remove(const RTLIL::SigSpec &pattern)
Definition: rtlil.cc:2342
void hash() const
Definition: rtlil.cc:2259
#define log_abort()
Definition: log.h:84
#define DEF_METHOD_5(_func, _type, _P1, _P2, _P3, _P4, _P5)
#define DEF_METHOD(_func, _y_size, _type)
Definition: rtlil.cc:1397
bool operator<(const RTLIL::SigSpec &other) const
Definition: rtlil.cc:2683
std::vector< RTLIL::SigSpec > compare
Definition: rtlil.h:1119
virtual void check()
Definition: rtlil.cc:948
std::vector< RTLIL::Module * > selected_whole_modules_warn() const
Definition: rtlil.cc:436
bool selected_member(RTLIL::IdString mod_name, RTLIL::IdString memb_name) const
Definition: rtlil.cc:397
virtual RTLIL::IdString derive(RTLIL::Design *design, std::map< RTLIL::IdString, RTLIL::Const > parameters)
Definition: rtlil.cc:467
bool has_processes_warn() const
Definition: rtlil.cc:1086
std::vector< RTLIL::SigSig > connections_
Definition: rtlil.h:597
int offset
Definition: rtlil.h:910
bool operator<(const RTLIL::SigChunk &other) const
Definition: rtlil.cc:1985
bool empty() const
Definition: rtlil.h:219
int scratchpad_get_int(std::string varname, int default_value=0) const
Definition: rtlil.cc:306
static std::string escape_id(std::string str)
Definition: rtlil.h:251
bool port_output
Definition: rtlil.h:827
bool packed() const
Definition: rtlil.h:973
bool match(std::string pattern) const
Definition: rtlil.cc:2886
RTLIL_ATTRIBUTE_MEMBERS Module()
Definition: rtlil.cc:448
RTLIL::Wire * wire
Definition: rtlil.h:885
#define DEF_METHOD_4(_func, _type, _P1, _P2, _P3, _P4)
RTLIL::Cell * addDlatch(RTLIL::IdString name, RTLIL::SigSpec sig_en, RTLIL::SigSpec sig_d, RTLIL::SigSpec sig_q, bool en_polarity=true)
Definition: rtlil.cc:1654
RTLIL::Cell * addDff(RTLIL::IdString name, RTLIL::SigSpec sig_clk, RTLIL::SigSpec sig_d, RTLIL::SigSpec sig_q, bool clk_polarity=true)
Definition: rtlil.cc:1599
void connect(const RTLIL::SigSig &conn)
Definition: rtlil.cc:1278
void append_bit(const RTLIL::SigBit &bit)
Definition: rtlil.cc:2562
void add(RTLIL::Wire *wire)
Definition: rtlil.cc:1113
bool operator!=(const RTLIL::SigChunk &other) const
Definition: rtlil.cc:2008
bool is_chunk() const
Definition: rtlil.cc:2755
std::map< RTLIL::IdString, RTLIL::Selection > selection_vars
Definition: rtlil.h:510
RTLIL::SigSpec signal
Definition: rtlil.h:1132
int(* get_line_num)()
Definition: ast.cc:51
std::vector< RTLIL::SigChunk > chunks_
Definition: rtlil.h:966
std::string as_string() const
Definition: rtlil.cc:116
static std::vector< int > global_free_idx_list_
Definition: rtlil.h:97
RTLIL_ATTRIBUTE_MEMBERS bool hasPort(RTLIL::IdString portname) const
Definition: rtlil.cc:1766
const RTLIL::SigSpec & getPort(RTLIL::IdString portname) const
Definition: rtlil.cc:1809
bool is_wire() const
Definition: rtlil.cc:2747
#define DEF_METHOD_2(_func, _type, _P1, _P2)
int GetSize(RTLIL::Wire *wire)
Definition: yosys.cc:334
std::vector< RTLIL::Wire * > selected_wires() const
Definition: rtlil.cc:1093
void optimize()
Definition: rtlil.cc:369
RTLIL::Cell * addAssert(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_en)
Definition: rtlil.cc:1579
const BigUnsigned & check(const BigUnsigned &x)
Definition: testsuite.cc:25
std::string decode_string() const
Definition: rtlil.cc:131
void fixup_ports()
Definition: rtlil.cc:1312
#define log_assert(_assert_expr_)
Definition: log.h:85
bool is_fully_const() const
Definition: rtlil.cc:2763
RTLIL::Wire * addWire(RTLIL::IdString name, int width=1)
Definition: rtlil.cc:1331
RTLIL::IdString name
Definition: rtlil.h:599
#define NEW_ID
Definition: yosys.h:166
RTLIL::SigChunk as_chunk() const
Definition: rtlil.cc:2877
bool selected_whole_module(RTLIL::IdString mod_name) const
Definition: rtlil.cc:388
std::vector< RTLIL::Cell * > selected_cells() const
Definition: rtlil.cc:1103
const RTLIL::SigSpec & operator=(const RTLIL::SigSpec &other)
Definition: rtlil.cc:2038
static uint32_t hash(uint32_t x)
Definition: Map.h:38
RTLIL::Cell * addConcat(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, RTLIL::SigSpec sig_y)
Definition: rtlil.cc:1558
RTLIL::Cell * addDlatchsr(RTLIL::IdString name, RTLIL::SigSpec sig_en, RTLIL::SigSpec sig_set, RTLIL::SigSpec sig_clr, RTLIL::SigSpec sig_d, RTLIL::SigSpec sig_q, bool en_polarity=true, bool set_polarity=true, bool clr_polarity=true)
Definition: rtlil.cc:1665
int refcount_wires_
Definition: rtlil.h:592
static std::vector< char * > global_id_storage_
Definition: rtlil.h:95
bool scratchpad_get_bool(std::string varname, bool default_value=false) const
Definition: rtlil.cc:324
bool operator<(const RTLIL::Const &other) const
Definition: rtlil.cc:76
void from_cell(RTLIL::Cell *cell)
Definition: macc.h:100
#define param_bool(_n)
RTLIL::Cell * addAdff(RTLIL::IdString name, RTLIL::SigSpec sig_clk, RTLIL::SigSpec sig_arst, RTLIL::SigSpec sig_d, RTLIL::SigSpec sig_q, RTLIL::Const arst_value, bool clk_polarity=true, bool arst_polarity=true)
Definition: rtlil.cc:1639
RTLIL::Process * clone() const
Definition: rtlil.cc:3153
void remove2(const RTLIL::SigSpec &pattern, RTLIL::SigSpec *other)
Definition: rtlil.cc:2353
RTLIL::SigBit to_single_sigbit() const
Definition: rtlil.cc:2945
static std::map< char *, int, char_ptr_cmp > global_id_index_
Definition: rtlil.h:96
bool operator==(const RTLIL::SigChunk &other) const
Definition: rtlil.cc:2003
RTLIL::IdString name
Definition: rtlil.h:825
std::vector< RTLIL::SigSig > actions
Definition: rtlil.h:1146
static const char * id2cstr(const RTLIL::IdString &str)
Definition: rtlil.h:267
RTLIL::CaseRule * clone() const
Definition: rtlil.cc:3111
int as_int(bool is_signed=false) const
Definition: rtlil.cc:104
void remove_const()
Definition: rtlil.cc:2464
RTLIL::Cell * addSr(RTLIL::IdString name, RTLIL::SigSpec sig_set, RTLIL::SigSpec sig_clr, RTLIL::SigSpec sig_q, bool set_polarity=true, bool clr_polarity=true)
Definition: rtlil.cc:1587
std::vector< RTLIL::Module * > selected_whole_modules() const
Definition: rtlil.cc:426
RTLIL::IdString name
Definition: rtlil.h:1154
RTLIL::Module * addModule(RTLIL::IdString name)
Definition: rtlil.cc:270
RTLIL::Module * module(RTLIL::IdString name)
Definition: rtlil.cc:254
std::map< RTLIL::IdString, RTLIL::Process * > processes
Definition: rtlil.h:602
RTLIL::Cell * 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=true, bool set_polarity=true, bool clr_polarity=true)
Definition: rtlil.cc:1623
void check()
Definition: rtlil.cc:1839
virtual size_t count_id(RTLIL::IdString id)
Definition: rtlil.cc:472
RTLIL::ObjRange< RTLIL::Module * > modules()
Definition: rtlil.cc:249
bool has_memories() const
Definition: rtlil.cc:1069
std::map< RTLIL::IdString, RTLIL::Module * > modules_
Definition: rtlil.h:507
void replace(const RTLIL::SigSpec &pattern, const RTLIL::SigSpec &with)
Definition: rtlil.cc:2297
void sort_and_unify()
Definition: rtlil.cc:2291
#define NULL
#define YOSYS_NAMESPACE_BEGIN
Definition: yosys.h:99
USING_YOSYS_NAMESPACE static PRIVATE_NAMESPACE_BEGIN std::string netname(std::set< std::string > &conntypes_code, std::set< std::string > &celltypes_code, std::set< std::string > &constcells_code, RTLIL::SigSpec sig)
Definition: intersynth.cc:30
int as_int(bool is_signed=false) const
Definition: rtlil.cc:2829
void remove(const std::set< RTLIL::Wire * > &wires)
Definition: rtlil.cc:1158
RTLIL::Module * module
Definition: rtlil.h:824
static int sigspec_parse_get_dummy_line_num()
Definition: rtlil.cc:2967
RTLIL::IdString uniquify(RTLIL::IdString name)
Definition: rtlil.cc:1244
RTLIL::Cell * 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=true, bool set_polarity=true, bool clr_polarity=true)
Definition: rtlil.cc:1700
std::vector< RTLIL::SyncRule * > syncs
Definition: rtlil.h:1157
RTLIL::SwitchRule * clone() const
Definition: rtlil.cc:3127
const RTLIL::Const & getParam(RTLIL::IdString paramname) const
Definition: rtlil.cc:1834
static bool fixup_ports_compare(const RTLIL::Wire *a, const RTLIL::Wire *b)
Definition: rtlil.cc:1266
RTLIL::Cell * replace(RTLIL::Module *needle, RTLIL::Module *haystack, SubCircuit::Solver::Result &match)
Definition: extract.cc:294
RTLIL::SigSpec extract(const RTLIL::SigSpec &pattern, const RTLIL::SigSpec *other=NULL) const
Definition: rtlil.cc:2414
RTLIL::SigSpec repeat(int num) const
Definition: rtlil.cc:2631
void check() const
Definition: rtlil.cc:2642
void scratchpad_set_bool(std::string varname, bool value)
Definition: rtlil.cc:296
std::vector< RTLIL::State > bits
Definition: rtlil.h:438
std::string as_string() const
Definition: rtlil.cc:2840
void append(const RTLIL::SigSpec &signal)
Definition: rtlil.cc:2523
std::vector< RTLIL::Module * > selected_modules() const
Definition: rtlil.cc:416
bool operator==(const RTLIL::Const &other) const
Definition: rtlil.cc:86
State
Definition: rtlil.h:29
bool hasParam(RTLIL::IdString paramname) const
Definition: rtlil.cc:1819
static bool parse_rhs(const RTLIL::SigSpec &lhs, RTLIL::SigSpec &sig, RTLIL::Module *module, std::string str)
Definition: rtlil.cc:3078
virtual ~Module()
Definition: rtlil.cc:455
void extend(int width, bool is_signed=false)
Definition: rtlil.cc:2593
int start_offset
Definition: rtlil.h:826
void optimize(RTLIL::Design *design)
Definition: rtlil.cc:180
std::map< RTLIL::IdString, RTLIL::SigSpec > connections_
Definition: rtlil.h:855
RTLIL::Cell * addDlatchsrGate(RTLIL::IdString name, RTLIL::SigSpec sig_en, RTLIL::SigSpec sig_set, RTLIL::SigSpec sig_clr, RTLIL::SigSpec sig_d, RTLIL::SigSpec sig_q, bool en_polarity=true, bool set_polarity=true, bool clr_polarity=true)
Definition: rtlil.cc:1732
AST::AstNode * const2ast(std::string code, char case_type=0, bool warn_z=false)
Definition: const2ast.cc:135
static struct RTLIL::IdString::destruct_guard_t destruct_guard
Definition: rtlil.cc:30
bool has_memories_warn() const
Definition: rtlil.cc:1079
bool operator!=(const RTLIL::Const &other) const
Definition: rtlil.cc:91
std::vector< RTLIL::State > bits
Definition: ast.h:157
int width_
Definition: rtlil.h:964
std::vector< RTLIL::SigSig > actions
Definition: rtlil.h:1120
RTLIL::Design * design
Definition: rtlil.h:589
std::set< RTLIL::Monitor * > monitors
Definition: rtlil.h:503
void remove(RTLIL::Module *module)
Definition: rtlil.cc:347
RTLIL::Cell * addDffe(RTLIL::IdString name, RTLIL::SigSpec sig_clk, RTLIL::SigSpec sig_en, RTLIL::SigSpec sig_d, RTLIL::SigSpec sig_q, bool clk_polarity=true, bool en_polarity=true)
Definition: rtlil.cc:1610
void pack() const
Definition: rtlil.cc:2202
RTLIL::Wire * as_wire() const
Definition: rtlil.cc:2868
std::map< RTLIL::SigBit, RTLIL::SigBit > to_sigbit_map(const RTLIL::SigSpec &other) const
Definition: rtlil.cc:2929
std::vector< RTLIL::SwitchRule * > switches
Definition: rtlil.h:1121
std::pair< SigSpec, SigSpec > SigSig
Definition: rtlil.h:71
const std::map< RTLIL::IdString, RTLIL::SigSpec > & connections() const
Definition: rtlil.cc:1814
RTLIL::Cell * addDffeGate(RTLIL::IdString name, RTLIL::SigSpec sig_clk, RTLIL::SigSpec sig_en, RTLIL::SigSpec sig_d, RTLIL::SigSpec sig_q, bool clk_polarity=true, bool en_polarity=true)
Definition: rtlil.cc:1690
bool has_marked_bits() const
Definition: rtlil.cc:2804
std::vector< RTLIL::State > data
Definition: rtlil.h:886
const char * log_id(RTLIL::IdString str)
Definition: log.cc:283
RTLIL::Module * module
Definition: rtlil.h:852
static bool parse(RTLIL::SigSpec &sig, RTLIL::Module *module, std::string str)
Definition: rtlil.cc:2972
bool operator==(const RTLIL::SigSpec &other) const
Definition: rtlil.cc:2715
std::vector< RTLIL::SigBit > to_sigbit_vector() const
Definition: rtlil.cc:2921
RTLIL::Cell * addPow(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, RTLIL::SigSpec sig_y, bool a_signed=false, bool b_signed=false)
void rename(RTLIL::Wire *wire, RTLIL::IdString new_name)
Definition: rtlil.cc:1185
int refcount_cells_
Definition: rtlil.h:593
bool has_processes() const
Definition: rtlil.cc:1074
const std::vector< RTLIL::SigChunk > & chunks() const
Definition: rtlil.h:1016
static std::vector< int > global_refcount_storage_
Definition: rtlil.h:94
void scratchpad_set_string(std::string varname, std::string value)
Definition: rtlil.cc:301
RTLIL_ATTRIBUTE_MEMBERS RTLIL::CaseRule root_case
Definition: rtlil.h:1156
RTLIL::Cell * addAdffGate(RTLIL::IdString name, RTLIL::SigSpec sig_clk, RTLIL::SigSpec sig_arst, RTLIL::SigSpec sig_d, RTLIL::SigSpec sig_q, bool arst_value=false, bool clk_polarity=true, bool arst_polarity=true)
Definition: rtlil.cc:1712
virtual RTLIL::Module * clone() const
Definition: rtlil.cc:1061
static void sigspec_parse_split(std::vector< std::string > &tokens, const std::string &text, char sep)
Definition: rtlil.cc:2957