yosys-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
extract.cc File Reference
#include "kernel/register.h"
#include "kernel/sigtools.h"
#include "kernel/log.h"
#include "libs/subcircuit/subcircuit.h"
#include <algorithm>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
+ Include dependency graph for extract.cc:

Go to the source code of this file.

Data Structures

class  SubCircuitSolver
 
struct  bit_ref_t
 
struct  ExtractPass
 

Macros

#define param_bool(_n)   if (param == _n) return value.as_bool();
 
#define param_int(_n)   if (param == _n) return value.as_int();
 

Functions

bool module2graph (SubCircuit::Graph &graph, RTLIL::Module *mod, bool constports, RTLIL::Design *sel=NULL, int max_fanout=-1, std::set< std::pair< RTLIL::IdString, RTLIL::IdString >> *split=NULL)
 
RTLIL::Cellreplace (RTLIL::Module *needle, RTLIL::Module *haystack, SubCircuit::Solver::Result &match)
 
bool compareSortNeedleList (RTLIL::Module *left, RTLIL::Module *right)
 

Variables

ExtractPass ExtractPass
 

Macro Definition Documentation

#define param_bool (   _n)    if (param == _n) return value.as_bool();
#define param_int (   _n)    if (param == _n) return value.as_int();

Function Documentation

bool compareSortNeedleList ( RTLIL::Module left,
RTLIL::Module right 
)

Definition at line 341 of file extract.cc.

342 {
343  int left_idx = 0, right_idx = 0;
344  if (left->attributes.count("\\extract_order") > 0)
345  left_idx = left->attributes.at("\\extract_order").as_int();
346  if (right->attributes.count("\\extract_order") > 0)
347  right_idx = right->attributes.at("\\extract_order").as_int();
348  if (left_idx != right_idx)
349  return left_idx < right_idx;
350  return left->name < right->name;
351 }
RTLIL::IdString name
Definition: rtlil.h:599

+ Here is the caller graph for this function:

bool module2graph ( SubCircuit::Graph graph,
RTLIL::Module mod,
bool  constports,
RTLIL::Design sel = NULL,
int  max_fanout = -1,
std::set< std::pair< RTLIL::IdString, RTLIL::IdString >> *  split = NULL 
)

Definition at line 152 of file extract.cc.

154 {
155  SigMap sigmap(mod);
156  std::map<RTLIL::SigBit, bit_ref_t> sig_bit_ref;
157 
158  if (sel && !sel->selected(mod)) {
159  log(" Skipping module %s as it is not selected.\n", id2cstr(mod->name));
160  return false;
161  }
162 
163  if (mod->processes.size() > 0) {
164  log(" Skipping module %s as it contains unprocessed processes.\n", id2cstr(mod->name));
165  return false;
166  }
167 
168  if (constports) {
169  graph.createNode("$const$0", "$const$0", NULL, true);
170  graph.createNode("$const$1", "$const$1", NULL, true);
171  graph.createNode("$const$x", "$const$x", NULL, true);
172  graph.createNode("$const$z", "$const$z", NULL, true);
173  graph.createPort("$const$0", "\\Y", 1);
174  graph.createPort("$const$1", "\\Y", 1);
175  graph.createPort("$const$x", "\\Y", 1);
176  graph.createPort("$const$z", "\\Y", 1);
177  graph.markExtern("$const$0", "\\Y", 0);
178  graph.markExtern("$const$1", "\\Y", 0);
179  graph.markExtern("$const$x", "\\Y", 0);
180  graph.markExtern("$const$z", "\\Y", 0);
181  }
182 
183  std::map<std::pair<RTLIL::Wire*, int>, int> sig_use_count;
184  if (max_fanout > 0)
185  for (auto &cell_it : mod->cells_)
186  {
187  RTLIL::Cell *cell = cell_it.second;
188  if (!sel || sel->selected(mod, cell))
189  for (auto &conn : cell->connections()) {
190  RTLIL::SigSpec conn_sig = conn.second;
191  sigmap.apply(conn_sig);
192  for (auto &bit : conn_sig)
193  if (bit.wire != NULL)
194  sig_use_count[std::pair<RTLIL::Wire*, int>(bit.wire, bit.offset)]++;
195  }
196  }
197 
198  // create graph nodes from cells
199  for (auto &cell_it : mod->cells_)
200  {
201  RTLIL::Cell *cell = cell_it.second;
202  if (sel && !sel->selected(mod, cell))
203  continue;
204 
205  std::string type = cell->type.str();
206  if (sel == NULL && type.substr(0, 2) == "\\$")
207  type = type.substr(1);
208  graph.createNode(cell->name.str(), type, (void*)cell);
209 
210  for (auto &conn : cell->connections())
211  {
212  graph.createPort(cell->name.str(), conn.first.str(), conn.second.size());
213 
214  if (split && split->count(std::pair<RTLIL::IdString, RTLIL::IdString>(cell->type, conn.first)) > 0)
215  continue;
216 
217  RTLIL::SigSpec conn_sig = conn.second;
218  sigmap.apply(conn_sig);
219 
220  for (int i = 0; i < conn_sig.size(); i++)
221  {
222  auto &bit = conn_sig[i];
223 
224  if (bit.wire == NULL) {
225  if (constports) {
226  std::string node = "$const$x";
227  if (bit == RTLIL::State::S0) node = "$const$0";
228  if (bit == RTLIL::State::S1) node = "$const$1";
229  if (bit == RTLIL::State::Sz) node = "$const$z";
230  graph.createConnection(cell->name.str(), conn.first.str(), i, node, "\\Y", 0);
231  } else
232  graph.createConstant(cell->name.str(), conn.first.str(), i, int(bit.data));
233  continue;
234  }
235 
236  if (max_fanout > 0 && sig_use_count[std::pair<RTLIL::Wire*, int>(bit.wire, bit.offset)] > max_fanout)
237  continue;
238 
239  if (sel && !sel->selected(mod, bit.wire))
240  continue;
241 
242  if (sig_bit_ref.count(bit) == 0) {
243  bit_ref_t &bit_ref = sig_bit_ref[bit];
244  bit_ref.cell = cell->name.str();
245  bit_ref.port = conn.first.str();
246  bit_ref.bit = i;
247  }
248 
249  bit_ref_t &bit_ref = sig_bit_ref[bit];
250  graph.createConnection(bit_ref.cell, bit_ref.port, bit_ref.bit, cell->name.str(), conn.first.str(), i);
251  }
252  }
253  }
254 
255  // mark external signals (used in non-selected cells)
256  for (auto &cell_it : mod->cells_)
257  {
258  RTLIL::Cell *cell = cell_it.second;
259  if (sel && !sel->selected(mod, cell))
260  for (auto &conn : cell->connections())
261  {
262  RTLIL::SigSpec conn_sig = conn.second;
263  sigmap.apply(conn_sig);
264 
265  for (auto &bit : conn_sig)
266  if (sig_bit_ref.count(bit) != 0) {
267  bit_ref_t &bit_ref = sig_bit_ref[bit];
268  graph.markExtern(bit_ref.cell, bit_ref.port, bit_ref.bit);
269  }
270  }
271  }
272 
273  // mark external signals (used in module ports)
274  for (auto &wire_it : mod->wires_)
275  {
276  RTLIL::Wire *wire = wire_it.second;
277  if (wire->port_id > 0)
278  {
279  RTLIL::SigSpec conn_sig(wire);
280  sigmap.apply(conn_sig);
281 
282  for (auto &bit : conn_sig)
283  if (sig_bit_ref.count(bit) != 0) {
284  bit_ref_t &bit_ref = sig_bit_ref[bit];
285  graph.markExtern(bit_ref.cell, bit_ref.port, bit_ref.bit);
286  }
287  }
288  }
289 
290  // graph.print();
291  return true;
292 }
bool selected(T1 *module) const
Definition: rtlil.h:551
std::string str() const
Definition: rtlil.h:182
RTLIL::Wire * wire(RTLIL::IdString id)
Definition: rtlil.h:637
std::map< RTLIL::IdString, RTLIL::Wire * > wires_
Definition: rtlil.h:595
RTLIL::IdString name
Definition: rtlil.h:853
int port_id
Definition: rtlil.h:826
RTLIL::IdString type
Definition: rtlil.h:854
int size() const
Definition: rtlil.h:1019
void createNode(std::string nodeId, std::string typeId, void *userData=NULL, bool shared=false)
Definition: subcircuit.cc:107
std::string cell
Definition: extract.cc:148
std::string port
Definition: extract.cc:148
void createConstant(std::string toNodeId, std::string toPortId, int toBit, int constValue)
Definition: subcircuit.cc:209
RTLIL::IdString name
Definition: rtlil.h:599
void markExtern(std::string nodeId, std::string portId, int bit=-1)
Definition: subcircuit.cc:244
static const char * id2cstr(const RTLIL::IdString &str)
Definition: rtlil.h:267
void createConnection(std::string fromNodeId, std::string fromPortId, int fromBit, std::string toNodeId, std::string toPortId, int toBit, int width=1)
Definition: subcircuit.cc:144
std::map< RTLIL::IdString, RTLIL::Process * > processes
Definition: rtlil.h:602
int bit
Definition: extract.cc:149
#define NULL
std::map< RTLIL::IdString, RTLIL::Cell * > cells_
Definition: rtlil.h:596
void log(const char *format,...)
Definition: log.cc:180
void createPort(std::string nodeId, std::string portId, int width=1, int minWidth=-1)
Definition: subcircuit.cc:120
const std::map< RTLIL::IdString, RTLIL::SigSpec > & connections() const
Definition: rtlil.cc:1814

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

RTLIL::Cell* replace ( RTLIL::Module needle,
RTLIL::Module haystack,
SubCircuit::Solver::Result match 
)

Definition at line 294 of file extract.cc.

295 {
296  SigMap sigmap(needle);
298 
299  // create new cell
300  RTLIL::Cell *cell = haystack->addCell(stringf("$extract$%s$%d", needle->name.c_str(), autoidx++), needle->name);
301 
302  // create cell ports
303  for (auto &it : needle->wires_) {
304  RTLIL::Wire *wire = it.second;
305  if (wire->port_id > 0) {
306  for (int i = 0; i < wire->width; i++)
307  sig2port.insert(sigmap(RTLIL::SigSpec(wire, i)), std::pair<RTLIL::IdString, int>(wire->name, i));
308  cell->setPort(wire->name, RTLIL::SigSpec(RTLIL::State::Sz, wire->width));
309  }
310  }
311 
312  // delete replaced cells and connect new ports
313  for (auto &it : match.mappings)
314  {
315  auto &mapping = it.second;
316  RTLIL::Cell *needle_cell = (RTLIL::Cell*)mapping.needleUserData;
317  RTLIL::Cell *haystack_cell = (RTLIL::Cell*)mapping.haystackUserData;
318 
319  if (needle_cell == NULL)
320  continue;
321 
322  for (auto &conn : needle_cell->connections()) {
323  RTLIL::SigSpec sig = sigmap(conn.second);
324  if (mapping.portMapping.count(conn.first.str()) > 0 && sig2port.has(sigmap(sig))) {
325  for (int i = 0; i < sig.size(); i++)
326  for (auto &port : sig2port.find(sig[i])) {
327  RTLIL::SigSpec bitsig = haystack_cell->getPort(mapping.portMapping[conn.first.str()]).extract(i, 1);
328  RTLIL::SigSpec new_sig = cell->getPort(port.first);
329  new_sig.replace(port.second, bitsig);
330  cell->setPort(port.first, new_sig);
331  }
332  }
333  }
334 
335  haystack->remove(haystack_cell);
336  }
337 
338  return cell;
339 }
const char * c_str() const
Definition: rtlil.h:178
std::string stringf(const char *fmt,...)
Definition: yosys.cc:58
bool has(RTLIL::SigSpec sig)
Definition: sigtools.h:203
RTLIL::Cell * addCell(RTLIL::IdString name, RTLIL::IdString type)
Definition: rtlil.cc:1353
std::map< RTLIL::IdString, RTLIL::Wire * > wires_
Definition: rtlil.h:595
void setPort(RTLIL::IdString portname, RTLIL::SigSpec signal)
Definition: rtlil.cc:1789
int width
Definition: rtlil.h:826
int port_id
Definition: rtlil.h:826
int size() const
Definition: rtlil.h:1019
const RTLIL::SigSpec & getPort(RTLIL::IdString portname) const
Definition: rtlil.cc:1809
RTLIL::IdString name
Definition: rtlil.h:599
RTLIL::IdString name
Definition: rtlil.h:825
void replace(const RTLIL::SigSpec &pattern, const RTLIL::SigSpec &with)
Definition: rtlil.cc:2297
#define NULL
void remove(const std::set< RTLIL::Wire * > &wires)
Definition: rtlil.cc:1158
const std::map< RTLIL::IdString, RTLIL::SigSpec > & connections() const
Definition: rtlil.cc:1814
std::map< std::string, ResultNodeMapping > mappings
Definition: subcircuit.h:102
void find(RTLIL::SigSpec sig, std::set< T > &result)
Definition: sigtools.h:187
void insert(RTLIL::SigSpec sig, T data)
Definition: sigtools.h:152
YOSYS_NAMESPACE_BEGIN int autoidx
Definition: yosys.cc:51

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Variable Documentation