yosys-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
hierarchy.cc File Reference
#include "kernel/yosys.h"
#include <stdlib.h>
#include <stdio.h>
#include <set>
#include <unistd.h>
+ Include dependency graph for hierarchy.cc:

Go to the source code of this file.

Data Structures

struct  generate_port_decl_t
 
struct  HierarchyPass
 

Functions

void generate (RTLIL::Design *design, const std::vector< std::string > &celltypes, const std::vector< generate_port_decl_t > &portdecls)
 
bool expand_module (RTLIL::Design *design, RTLIL::Module *module, bool flag_check, std::vector< std::string > &libdirs)
 
void hierarchy_worker (RTLIL::Design *design, std::set< RTLIL::Module * > &used, RTLIL::Module *mod, int indent)
 
void hierarchy (RTLIL::Design *design, RTLIL::Module *top, bool purge_lib, bool first_pass)
 
bool set_keep_assert (std::map< RTLIL::Module *, bool > &cache, RTLIL::Module *mod)
 

Variables

HierarchyPass HierarchyPass
 

Function Documentation

bool expand_module ( RTLIL::Design design,
RTLIL::Module module,
bool  flag_check,
std::vector< std::string > &  libdirs 
)

Definition at line 141 of file hierarchy.cc.

142 {
143  bool did_something = false;
144  std::map<RTLIL::Cell*, std::pair<int, int>> array_cells;
145  std::string filename;
146 
147  for (auto &cell_it : module->cells_)
148  {
149  RTLIL::Cell *cell = cell_it.second;
150 
151  if (cell->type.substr(0, 7) == "$array:") {
152  int pos_idx = cell->type.str().find_first_of(':');
153  int pos_num = cell->type.str().find_first_of(':', pos_idx + 1);
154  int pos_type = cell->type.str().find_first_of(':', pos_num + 1);
155  int idx = atoi(cell->type.str().substr(pos_idx + 1, pos_num).c_str());
156  int num = atoi(cell->type.str().substr(pos_num + 1, pos_type).c_str());
157  array_cells[cell] = std::pair<int, int>(idx, num);
158  cell->type = cell->type.str().substr(pos_type + 1);
159  }
160 
161  if (design->modules_.count(cell->type) == 0)
162  {
163  if (design->modules_.count("$abstract" + cell->type.str()))
164  {
165  cell->type = design->modules_.at("$abstract" + cell->type.str())->derive(design, cell->parameters);
166  cell->parameters.clear();
167  did_something = true;
168  continue;
169  }
170 
171  if (cell->type[0] == '$')
172  continue;
173 
174  for (auto &dir : libdirs)
175  {
176  filename = dir + "/" + RTLIL::unescape_id(cell->type) + ".v";
177  if (check_file_exists(filename)) {
178  std::vector<std::string> args;
179  args.push_back(filename);
180  Frontend::frontend_call(design, NULL, filename, "verilog");
181  goto loaded_module;
182  }
183 
184  filename = dir + "/" + RTLIL::unescape_id(cell->type) + ".il";
185  if (check_file_exists(filename)) {
186  std::vector<std::string> args;
187  args.push_back(filename);
188  Frontend::frontend_call(design, NULL, filename, "ilang");
189  goto loaded_module;
190  }
191  }
192 
193  if (flag_check && cell->type[0] != '$')
194  log_error("Module `%s' referenced in module `%s' in cell `%s' is not part of the design.\n",
195  cell->type.c_str(), module->name.c_str(), cell->name.c_str());
196  continue;
197 
198  loaded_module:
199  if (design->modules_.count(cell->type) == 0)
200  log_error("File `%s' from libdir does not declare module `%s'.\n", filename.c_str(), cell->type.c_str());
201  did_something = true;
202  }
203 
204  if (cell->parameters.size() == 0)
205  continue;
206 
207  if (design->modules_.at(cell->type)->get_bool_attribute("\\blackbox"))
208  continue;
209 
210  RTLIL::Module *mod = design->modules_[cell->type];
211  cell->type = mod->derive(design, cell->parameters);
212  cell->parameters.clear();
213  did_something = true;
214  }
215 
216  for (auto &it : array_cells)
217  {
218  RTLIL::Cell *cell = it.first;
219  int idx = it.second.first, num = it.second.second;
220 
221  if (design->modules_.count(cell->type) == 0)
222  log_error("Array cell `%s.%s' of unknown type `%s'.\n", RTLIL::id2cstr(module->name), RTLIL::id2cstr(cell->name), RTLIL::id2cstr(cell->type));
223 
224  RTLIL::Module *mod = design->modules_[cell->type];
225 
226  for (auto &conn : cell->connections_) {
227  int conn_size = conn.second.size();
228  RTLIL::IdString portname = conn.first;
229  if (portname.substr(0, 1) == "$") {
230  int port_id = atoi(portname.substr(1).c_str());
231  for (auto &wire_it : mod->wires_)
232  if (wire_it.second->port_id == port_id) {
233  portname = wire_it.first;
234  break;
235  }
236  }
237  if (mod->wires_.count(portname) == 0)
238  log_error("Array cell `%s.%s' connects to unknown port `%s'.\n", RTLIL::id2cstr(module->name), RTLIL::id2cstr(cell->name), RTLIL::id2cstr(conn.first));
239  int port_size = mod->wires_.at(portname)->width;
240  if (conn_size == port_size)
241  continue;
242  if (conn_size != port_size*num)
243  log_error("Array cell `%s.%s' has invalid port vs. signal size for port `%s'.\n", RTLIL::id2cstr(module->name), RTLIL::id2cstr(cell->name), RTLIL::id2cstr(conn.first));
244  conn.second = conn.second.extract(port_size*idx, port_size);
245  }
246  }
247 
248  return did_something;
249 }
const char * c_str() const
Definition: rtlil.h:178
std::string str() const
Definition: rtlil.h:182
std::map< RTLIL::IdString, RTLIL::Wire * > wires_
Definition: rtlil.h:595
static std::string unescape_id(std::string str)
Definition: rtlil.h:257
static void frontend_call(RTLIL::Design *design, std::istream *f, std::string filename, std::string command)
Definition: register.cc:375
RTLIL::IdString name
Definition: rtlil.h:853
static std::string idx(std::string str)
Definition: test_autotb.cc:57
void log_error(const char *format,...)
Definition: log.cc:204
RTLIL::IdString type
Definition: rtlil.h:854
std::map< RTLIL::IdString, RTLIL::Const > parameters
Definition: rtlil.h:856
virtual RTLIL::IdString derive(RTLIL::Design *design, std::map< RTLIL::IdString, RTLIL::Const > parameters)
Definition: rtlil.cc:467
USING_YOSYS_NAMESPACE PRIVATE_NAMESPACE_BEGIN bool did_something
Definition: opt_const.cc:32
RTLIL::IdString name
Definition: rtlil.h:599
static const char * id2cstr(const RTLIL::IdString &str)
Definition: rtlil.h:267
bool check_file_exists(std::string filename, bool is_exec)
Definition: yosys.cc:302
std::string substr(size_t pos=0, size_t len=std::string::npos) const
Definition: rtlil.h:208
std::map< RTLIL::IdString, RTLIL::Module * > modules_
Definition: rtlil.h:507
#define NULL
std::map< RTLIL::IdString, RTLIL::Cell * > cells_
Definition: rtlil.h:596
std::map< RTLIL::IdString, RTLIL::SigSpec > connections_
Definition: rtlil.h:855

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void generate ( RTLIL::Design design,
const std::vector< std::string > &  celltypes,
const std::vector< generate_port_decl_t > &  portdecls 
)

Definition at line 39 of file hierarchy.cc.

40 {
41  std::set<RTLIL::IdString> found_celltypes;
42 
43  for (auto i1 : design->modules_)
44  for (auto i2 : i1.second->cells_)
45  {
46  RTLIL::Cell *cell = i2.second;
47  if (design->has(cell->type))
48  continue;
49  if (cell->type.substr(0, 1) == "$" && cell->type.substr(0, 3) != "$__")
50  continue;
51  for (auto &pattern : celltypes)
52  if (patmatch(pattern.c_str(), RTLIL::unescape_id(cell->type).c_str()))
53  found_celltypes.insert(cell->type);
54  }
55 
56  for (auto &celltype : found_celltypes)
57  {
58  std::set<RTLIL::IdString> portnames;
59  std::set<RTLIL::IdString> parameters;
60  std::map<RTLIL::IdString, int> portwidths;
61  log("Generate module for cell type %s:\n", celltype.c_str());
62 
63  for (auto i1 : design->modules_)
64  for (auto i2 : i1.second->cells_)
65  if (i2.second->type == celltype) {
66  for (auto &conn : i2.second->connections()) {
67  if (conn.first[0] != '$')
68  portnames.insert(conn.first);
69  portwidths[conn.first] = std::max(portwidths[conn.first], conn.second.size());
70  }
71  for (auto &para : i2.second->parameters)
72  parameters.insert(para.first);
73  }
74 
75  for (auto &decl : portdecls)
76  if (decl.index > 0)
77  portnames.insert(decl.portname);
78 
79  std::set<int> indices;
80  for (int i = 0; i < int(portnames.size()); i++)
81  indices.insert(i+1);
82 
83  std::vector<generate_port_decl_t> ports(portnames.size());
84 
85  for (auto &decl : portdecls)
86  if (decl.index > 0) {
87  portwidths[decl.portname] = std::max(portwidths[decl.portname], 1);
88  portwidths[decl.portname] = std::max(portwidths[decl.portname], portwidths[stringf("$%d", decl.index)]);
89  log(" port %d: %s [%d:0] %s\n", decl.index, decl.input ? decl.output ? "inout" : "input" : "output", portwidths[decl.portname]-1, RTLIL::id2cstr(decl.portname));
90  if (indices.count(decl.index) > ports.size())
91  log_error("Port index (%d) exceeds number of found ports (%d).\n", decl.index, int(ports.size()));
92  if (indices.count(decl.index) == 0)
93  log_error("Conflict on port index %d.\n", decl.index);
94  indices.erase(decl.index);
95  portnames.erase(decl.portname);
96  ports[decl.index-1] = decl;
97  }
98 
99  while (portnames.size() > 0) {
100  RTLIL::IdString portname = *portnames.begin();
101  for (auto &decl : portdecls)
102  if (decl.index == 0 && patmatch(decl.portname.c_str(), RTLIL::unescape_id(portname).c_str())) {
103  generate_port_decl_t d = decl;
104  d.portname = portname;
105  d.index = *indices.begin();
106  log_assert(!indices.empty());
107  indices.erase(d.index);
108  ports[d.index-1] = d;
109  portwidths[d.portname] = std::max(portwidths[d.portname], 1);
110  log(" port %d: %s [%d:0] %s\n", d.index, d.input ? d.output ? "inout" : "input" : "output", portwidths[d.portname]-1, RTLIL::id2cstr(d.portname));
111  goto found_matching_decl;
112  }
113  log_error("Can't match port %s.\n", RTLIL::id2cstr(portname));
114  found_matching_decl:;
115  portnames.erase(portname);
116  }
117 
118  log_assert(indices.empty());
119 
120  RTLIL::Module *mod = new RTLIL::Module;
121  mod->name = celltype;
122  mod->attributes["\\blackbox"] = RTLIL::Const(1);
123  design->add(mod);
124 
125  for (auto &decl : ports) {
126  RTLIL::Wire *wire = mod->addWire(decl.portname, portwidths.at(decl.portname));
127  wire->port_id = decl.index;
128  wire->port_input = decl.input;
129  wire->port_output = decl.output;
130  }
131 
132  mod->fixup_ports();
133 
134  for (auto &para : parameters)
135  log(" ignoring parameter %s.\n", RTLIL::id2cstr(para));
136 
137  log(" module %s created.\n", RTLIL::id2cstr(mod->name));
138  }
139 }
std::string stringf(const char *fmt,...)
Definition: yosys.cc:58
void add(RTLIL::Module *module)
Definition: rtlil.cc:259
static std::string unescape_id(std::string str)
Definition: rtlil.h:257
bool port_input
Definition: rtlil.h:827
RTLIL::IdString portname
Definition: hierarchy.cc:35
void log_error(const char *format,...)
Definition: log.cc:204
int port_id
Definition: rtlil.h:826
RTLIL::IdString type
Definition: rtlil.h:854
bool port_output
Definition: rtlil.h:827
bool patmatch(const char *pattern, const char *string)
Definition: yosys.cc:144
#define log_assert(_assert_expr_)
Definition: log.h:85
RTLIL::IdString name
Definition: rtlil.h:599
static const char * id2cstr(const RTLIL::IdString &str)
Definition: rtlil.h:267
std::string substr(size_t pos=0, size_t len=std::string::npos) const
Definition: rtlil.h:208
bool has(RTLIL::IdString id) const
Definition: rtlil.h:519
std::map< RTLIL::IdString, RTLIL::Module * > modules_
Definition: rtlil.h:507
void log(const char *format,...)
Definition: log.cc:180

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void hierarchy ( RTLIL::Design design,
RTLIL::Module top,
bool  purge_lib,
bool  first_pass 
)

Definition at line 275 of file hierarchy.cc.

276 {
277  std::set<RTLIL::Module*> used;
278  hierarchy_worker(design, used, top, 0);
279 
280  std::vector<RTLIL::Module*> del_modules;
281  for (auto &it : design->modules_)
282  if (used.count(it.second) == 0)
283  del_modules.push_back(it.second);
284 
285  for (auto mod : del_modules) {
286  if (first_pass && mod->name.substr(0, 9) == "$abstract")
287  continue;
288  if (!purge_lib && mod->get_bool_attribute("\\blackbox"))
289  continue;
290  log("Removing unused module `%s'.\n", mod->name.c_str());
291  design->modules_.erase(mod->name);
292  delete mod;
293  }
294 
295  log("Removed %d unused modules.\n", GetSize(del_modules));
296 }
void hierarchy_worker(RTLIL::Design *design, std::set< RTLIL::Module * > &used, RTLIL::Module *mod, int indent)
Definition: hierarchy.cc:251
int GetSize(RTLIL::Wire *wire)
Definition: yosys.cc:334
std::map< RTLIL::IdString, RTLIL::Module * > modules_
Definition: rtlil.h:507
void log(const char *format,...)
Definition: log.cc:180

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void hierarchy_worker ( RTLIL::Design design,
std::set< RTLIL::Module * > &  used,
RTLIL::Module mod,
int  indent 
)

Definition at line 251 of file hierarchy.cc.

252 {
253  if (used.count(mod) > 0)
254  return;
255 
256  if (indent == 0)
257  log("Top module: %s\n", mod->name.c_str());
258  else
259  log("Used module: %*s%s\n", indent, "", mod->name.c_str());
260  used.insert(mod);
261 
262  for (auto cell : mod->cells()) {
263  std::string celltype = cell->type.str();
264  if (celltype.substr(0, 7) == "$array:") {
265  int pos_idx = celltype.find_first_of(':');
266  int pos_num = celltype.find_first_of(':', pos_idx + 1);
267  int pos_type = celltype.find_first_of(':', pos_num + 1);
268  celltype = celltype.substr(pos_type + 1);
269  }
270  if (design->module(celltype))
271  hierarchy_worker(design, used, design->module(celltype), indent+4);
272  }
273 }
const char * c_str() const
Definition: rtlil.h:178
void hierarchy_worker(RTLIL::Design *design, std::set< RTLIL::Module * > &used, RTLIL::Module *mod, int indent)
Definition: hierarchy.cc:251
RTLIL::IdString name
Definition: rtlil.h:599
RTLIL::Module * module(RTLIL::IdString name)
Definition: rtlil.cc:254
RTLIL::ObjRange< RTLIL::Cell * > cells()
Definition: rtlil.h:641
void log(const char *format,...)
Definition: log.cc:180

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

bool set_keep_assert ( std::map< RTLIL::Module *, bool > &  cache,
RTLIL::Module mod 
)

Definition at line 298 of file hierarchy.cc.

299 {
300  if (cache.count(mod) == 0)
301  for (auto c : mod->cells()) {
302  RTLIL::Module *m = mod->design->module(c->type);
303  if ((m != nullptr && set_keep_assert(cache, m)) || c->type == "$assert")
304  return cache[mod] = true;
305  }
306  return cache[mod];
307 }
bool set_keep_assert(std::map< RTLIL::Module *, bool > &cache, RTLIL::Module *mod)
Definition: hierarchy.cc:298
RTLIL::Module * module(RTLIL::IdString name)
Definition: rtlil.cc:254
RTLIL::ObjRange< RTLIL::Cell * > cells()
Definition: rtlil.h:641
RTLIL::Design * design
Definition: rtlil.h:589

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Variable Documentation