2 * yosys -- Yosys Open SYnthesis Suite
4 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
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.
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.
20 #include "kernel/compatibility.h"
21 #include "kernel/register.h"
22 #include "kernel/sigtools.h"
23 #include "kernel/log.h"
29 #include "passes/techmap/stdcells.inc"
32 extern void simplemap_get_mappers(std::map
<std::string
, void(*)(RTLIL::Module
*, RTLIL::Cell
*)> &mappers
);
34 static void apply_prefix(std::string prefix
, std::string
&id
)
37 id
= prefix
+ "." + id
.substr(1);
39 id
= "$techmap" + prefix
+ "." + id
;
42 static void apply_prefix(std::string prefix
, RTLIL::SigSpec
&sig
, RTLIL::Module
*module
)
44 std::vector
<RTLIL::SigChunk
> chunks
= sig
;
45 for (auto &chunk
: chunks
)
46 if (chunk
.wire
!= NULL
) {
47 std::string wire_name
= chunk
.wire
->name
;
48 apply_prefix(prefix
, wire_name
);
49 assert(module
->wires
.count(wire_name
) > 0);
50 chunk
.wire
= module
->wires
[wire_name
];
57 std::map
<std::string
, void(*)(RTLIL::Module
*, RTLIL::Cell
*)> simplemap_mappers
;
58 std::map
<std::pair
<RTLIL::IdString
, std::map
<RTLIL::IdString
, RTLIL::Const
>>, RTLIL::Module
*> techmap_cache
;
59 std::map
<RTLIL::Module
*, bool> techmap_do_cache
;
61 struct TechmapWireData
{
66 typedef std::map
<std::string
, std::vector
<TechmapWireData
>> TechmapWires
;
68 TechmapWires
techmap_find_special_wires(RTLIL::Module
*module
)
75 for (auto &it
: module
->wires
) {
76 const char *p
= it
.first
.c_str();
80 const char *q
= strrchr(p
+1, '.');
83 if (!strncmp(p
, "_TECHMAP_", 9)) {
84 TechmapWireData record
;
85 record
.wire
= it
.second
;
86 record
.value
= it
.second
;
87 result
[p
].push_back(record
);
88 it
.second
->attributes
["\\keep"] = RTLIL::Const(1);
89 it
.second
->attributes
["\\_techmap_special_"] = RTLIL::Const(1);
93 if (!result
.empty()) {
94 SigMap
sigmap(module
);
95 for (auto &it1
: result
)
96 for (auto &it2
: it1
.second
)
97 sigmap
.apply(it2
.value
);
103 void techmap_module_worker(RTLIL::Design
*design
, RTLIL::Module
*module
, RTLIL::Cell
*cell
, RTLIL::Module
*tpl
, bool flatten_mode
)
105 log("Mapping `%s.%s' using `%s'.\n", RTLIL::id2cstr(module
->name
), RTLIL::id2cstr(cell
->name
), RTLIL::id2cstr(tpl
->name
));
107 if (tpl
->memories
.size() != 0)
108 log_error("Technology map yielded memories -> this is not supported.\n");
110 if (tpl
->processes
.size() != 0) {
111 log("Technology map yielded processes:\n");
112 for (auto &it
: tpl
->processes
)
113 log(" %s",RTLIL::id2cstr(it
.first
));
114 log_error("Technology map yielded processes -> this is not supported.\n");
117 // erase from namespace first for _TECHMAP_REPLACE_ to work
118 module
->cells
.erase(cell
->name
);
119 std::string orig_cell_name
;
122 for (auto &it
: tpl
->cells
)
123 if (it
.first
== "\\_TECHMAP_REPLACE_") {
124 orig_cell_name
= cell
->name
;
125 cell
->name
= stringf("$techmap%d", RTLIL::autoidx
++) + cell
->name
;
129 std::map
<RTLIL::IdString
, RTLIL::IdString
> positional_ports
;
131 for (auto &it
: tpl
->wires
) {
132 if (it
.second
->port_id
> 0)
133 positional_ports
[stringf("$%d", it
.second
->port_id
)] = it
.first
;
134 RTLIL::Wire
*w
= new RTLIL::Wire(*it
.second
);
135 apply_prefix(cell
->name
, w
->name
);
136 w
->port_input
= false;
137 w
->port_output
= false;
139 if (it
.second
->get_bool_attribute("\\_techmap_special_"))
140 w
->attributes
.clear();
142 design
->select(module
, w
);
145 SigMap port_signal_map
;
147 for (auto &it
: cell
->connections
) {
148 RTLIL::IdString portname
= it
.first
;
149 if (positional_ports
.count(portname
) > 0)
150 portname
= positional_ports
.at(portname
);
151 if (tpl
->wires
.count(portname
) == 0 || tpl
->wires
.at(portname
)->port_id
== 0) {
152 if (portname
.substr(0, 1) == "$")
153 log_error("Can't map port `%s' of cell `%s' to template `%s'!\n", portname
.c_str(), cell
->name
.c_str(), tpl
->name
.c_str());
156 RTLIL::Wire
*w
= tpl
->wires
.at(portname
);
158 if (w
->port_output
) {
160 c
.second
= RTLIL::SigSpec(w
);
161 apply_prefix(cell
->name
, c
.second
, module
);
163 c
.first
= RTLIL::SigSpec(w
);
164 c
.second
= it
.second
;
165 apply_prefix(cell
->name
, c
.first
, module
);
167 if (c
.second
.size() > c
.first
.size())
168 c
.second
.remove(c
.first
.size(), c
.second
.size() - c
.first
.size());
169 if (c
.second
.size() < c
.first
.size())
170 c
.second
.append(RTLIL::SigSpec(RTLIL::State::S0
, c
.first
.size() - c
.second
.size()));
171 assert(c
.first
.size() == c
.second
.size());
173 // more conservative approach:
174 // connect internal and external wires
175 module
->connections
.push_back(c
);
177 // approach that yields nicer outputs:
178 // replace internal wires that are connected to external wires
180 port_signal_map
.add(c
.second
, c
.first
);
182 port_signal_map
.add(c
.first
, c
.second
);
186 for (auto &it
: tpl
->cells
) {
187 RTLIL::Cell
*c
= new RTLIL::Cell(*it
.second
);
188 if (!flatten_mode
&& c
->type
.substr(0, 2) == "\\$")
189 c
->type
= c
->type
.substr(1);
190 if (!flatten_mode
&& c
->name
== "\\_TECHMAP_REPLACE_")
191 c
->name
= orig_cell_name
;
193 apply_prefix(cell
->name
, c
->name
);
194 for (auto &it2
: c
->connections
) {
195 apply_prefix(cell
->name
, it2
.second
, module
);
196 port_signal_map
.apply(it2
.second
);
199 design
->select(module
, c
);
202 for (auto &it
: tpl
->connections
) {
203 RTLIL::SigSig c
= it
;
204 apply_prefix(cell
->name
, c
.first
, module
);
205 apply_prefix(cell
->name
, c
.second
, module
);
206 port_signal_map
.apply(c
.first
);
207 port_signal_map
.apply(c
.second
);
208 module
->connections
.push_back(c
);
214 bool techmap_module(RTLIL::Design
*design
, RTLIL::Module
*module
, RTLIL::Design
*map
, std::set
<RTLIL::Cell
*> &handled_cells
,
215 const std::map
<RTLIL::IdString
, std::set
<RTLIL::IdString
>> &celltypeMap
, bool flatten_mode
)
217 if (!design
->selected(module
))
220 bool log_continue
= false;
221 bool did_something
= false;
222 std::vector
<std::string
> cell_names
;
224 SigMap
sigmap(module
);
225 for (auto &cell_it
: module
->cells
)
226 cell_names
.push_back(cell_it
.first
);
228 for (auto &cell_name
: cell_names
)
230 if (module
->cells
.count(cell_name
) == 0)
233 RTLIL::Cell
*cell
= module
->cells
[cell_name
];
235 if (!design
->selected(module
, cell
) || handled_cells
.count(cell
) > 0)
238 if (celltypeMap
.count(cell
->type
) == 0)
241 for (auto &tpl_name
: celltypeMap
.at(cell
->type
))
243 std::string derived_name
= tpl_name
;
244 RTLIL::Module
*tpl
= map
->modules
[tpl_name
];
245 std::map
<RTLIL::IdString
, RTLIL::Const
> parameters
= cell
->parameters
;
247 if (tpl
->get_bool_attribute("\\blackbox"))
252 if (tpl
->get_bool_attribute("\\techmap_simplemap")) {
253 log("Mapping %s.%s (%s) with simplemap.\n", RTLIL::id2cstr(module
->name
), RTLIL::id2cstr(cell
->name
), RTLIL::id2cstr(cell
->type
));
254 if (simplemap_mappers
.count(cell
->type
) == 0)
255 log_error("No simplemap mapper for cell type %s found!\n", RTLIL::id2cstr(cell
->type
));
256 simplemap_mappers
.at(cell
->type
)(module
, cell
);
257 module
->cells
.erase(cell
->name
);
260 did_something
= true;
264 for (auto conn
: cell
->connections
) {
265 if (conn
.first
.substr(0, 1) == "$")
267 if (tpl
->wires
.count(conn
.first
) > 0 && tpl
->wires
.at(conn
.first
)->port_id
> 0)
269 if (!conn
.second
.is_fully_const() || parameters
.count(conn
.first
) > 0 || tpl
->avail_parameters
.count(conn
.first
) == 0)
271 parameters
[conn
.first
] = conn
.second
.as_const();
279 if (tpl
->avail_parameters
.count("\\_TECHMAP_CELLTYPE_") != 0)
280 parameters
["\\_TECHMAP_CELLTYPE_"] = RTLIL::unescape_id(cell
->type
);
282 for (auto conn
: cell
->connections
) {
283 if (tpl
->avail_parameters
.count(stringf("\\_TECHMAP_CONSTMSK_%s_", RTLIL::id2cstr(conn
.first
))) != 0) {
284 std::vector
<RTLIL::SigBit
> v
= sigmap(conn
.second
).to_sigbit_vector();
286 bit
= RTLIL::SigBit(bit
.wire
== NULL
? RTLIL::State::S1
: RTLIL::State::S0
);
287 parameters
[stringf("\\_TECHMAP_CONSTMSK_%s_", RTLIL::id2cstr(conn
.first
))] = RTLIL::SigSpec(v
).as_const();
289 if (tpl
->avail_parameters
.count(stringf("\\_TECHMAP_CONSTVAL_%s_", RTLIL::id2cstr(conn
.first
))) != 0) {
290 std::vector
<RTLIL::SigBit
> v
= sigmap(conn
.second
).to_sigbit_vector();
292 if (bit
.wire
!= NULL
)
293 bit
= RTLIL::SigBit(RTLIL::State::Sx
);
294 parameters
[stringf("\\_TECHMAP_CONSTVAL_%s_", RTLIL::id2cstr(conn
.first
))] = RTLIL::SigSpec(v
).as_const();
298 int unique_bit_id_counter
= 0;
299 std::map
<RTLIL::SigBit
, int> unique_bit_id
;
300 unique_bit_id
[RTLIL::State::S0
] = unique_bit_id_counter
++;
301 unique_bit_id
[RTLIL::State::S1
] = unique_bit_id_counter
++;
302 unique_bit_id
[RTLIL::State::Sx
] = unique_bit_id_counter
++;
303 unique_bit_id
[RTLIL::State::Sz
] = unique_bit_id_counter
++;
305 for (auto conn
: cell
->connections
)
306 if (tpl
->avail_parameters
.count(stringf("\\_TECHMAP_CONNMAP_%s_", RTLIL::id2cstr(conn
.first
))) != 0) {
307 for (auto &bit
: sigmap(conn
.second
).to_sigbit_vector())
308 if (unique_bit_id
.count(bit
) == 0)
309 unique_bit_id
[bit
] = unique_bit_id_counter
++;
313 for (int i
= 0; i
< 32; i
++)
314 if (((unique_bit_id_counter
-1) & (1 << i
)) != 0)
316 if (tpl
->avail_parameters
.count("\\_TECHMAP_BITS_CONNMAP_"))
317 parameters
["\\_TECHMAP_BITS_CONNMAP_"] = bits
;
319 for (auto conn
: cell
->connections
)
320 if (tpl
->avail_parameters
.count(stringf("\\_TECHMAP_CONNMAP_%s_", RTLIL::id2cstr(conn
.first
))) != 0) {
322 for (auto &bit
: sigmap(conn
.second
).to_sigbit_vector()) {
323 RTLIL::Const
chunk(unique_bit_id
.at(bit
), bits
);
324 value
.bits
.insert(value
.bits
.end(), chunk
.bits
.begin(), chunk
.bits
.end());
326 parameters
[stringf("\\_TECHMAP_CONNMAP_%s_", RTLIL::id2cstr(conn
.first
))] = value
;
330 std::pair
<RTLIL::IdString
, std::map
<RTLIL::IdString
, RTLIL::Const
>> key(tpl_name
, parameters
);
331 if (techmap_cache
.count(key
) > 0) {
332 tpl
= techmap_cache
[key
];
334 if (cell
->parameters
.size() != 0) {
335 derived_name
= tpl
->derive(map
, parameters
);
336 tpl
= map
->modules
[derived_name
];
339 techmap_cache
[key
] = tpl
;
343 techmap_do_cache
[tpl
] = true;
345 if (techmap_do_cache
.count(tpl
) == 0)
347 bool keep_running
= true;
348 techmap_do_cache
[tpl
] = true;
350 std::set
<std::string
> techmap_wire_names
;
354 TechmapWires twd
= techmap_find_special_wires(tpl
);
355 keep_running
= false;
358 techmap_wire_names
.insert(it
.first
);
360 for (auto &it
: twd
["_TECHMAP_FAIL_"]) {
361 RTLIL::SigSpec value
= it
.value
;
362 if (value
.is_fully_const() && value
.as_bool()) {
363 log("Not using module `%s' from techmap as it contains a %s marker wire with non-zero value %s.\n",
364 derived_name
.c_str(), RTLIL::id2cstr(it
.wire
->name
), log_signal(value
));
365 techmap_do_cache
[tpl
] = false;
369 if (!techmap_do_cache
[tpl
])
374 if (it
.first
.substr(0, 12) != "_TECHMAP_DO_" || it
.second
.empty())
377 auto &data
= it
.second
.front();
379 if (!data
.value
.is_fully_const())
380 log_error("Techmap yielded config wire %s with non-const value %s.\n", RTLIL::id2cstr(data
.wire
->name
), log_signal(data
.value
));
382 techmap_wire_names
.erase(it
.first
);
383 tpl
->wires
.erase(data
.wire
->name
);
385 const char *p
= data
.wire
->name
.c_str();
386 const char *q
= strrchr(p
+1, '.');
389 assert(!strncmp(q
, "_TECHMAP_DO_", 12));
390 std::string new_name
= data
.wire
->name
.substr(0, q
-p
) + "_TECHMAP_DONE_" + data
.wire
->name
.substr(q
-p
+12);
391 while (tpl
->wires
.count(new_name
))
393 data
.wire
->name
= new_name
;
396 std::string cmd_string
= data
.value
.as_const().decode_string();
397 Pass::call_on_module(map
, tpl
, cmd_string
);
404 TechmapWires twd
= techmap_find_special_wires(tpl
);
405 for (auto &it
: twd
) {
406 if (it
.first
!= "_TECHMAP_FAIL_" && it
.first
.substr(0, 12) != "_TECHMAP_DO_" && it
.first
.substr(0, 14) != "_TECHMAP_DONE_")
407 log_error("Techmap yielded unknown config wire %s.\n", it
.first
.c_str());
408 if (techmap_do_cache
[tpl
])
409 for (auto &it2
: it
.second
)
410 if (!it2
.value
.is_fully_const())
411 log_error("Techmap yielded config wire %s with non-const value %s.\n", RTLIL::id2cstr(it2
.wire
->name
), log_signal(it2
.value
));
412 techmap_wire_names
.erase(it
.first
);
415 for (auto &it
: techmap_wire_names
)
416 log_error("Techmap special wire %s disappeared. This is considered a fatal error.\n", RTLIL::id2cstr(it
));
419 if (techmap_do_cache
.at(tpl
) == false)
423 log_header("Continuing TECHMAP pass.\n");
424 log_continue
= false;
427 techmap_module_worker(design
, module
, cell
, tpl
, flatten_mode
);
428 did_something
= true;
433 handled_cells
.insert(cell
);
437 log_header("Continuing TECHMAP pass.\n");
438 log_continue
= false;
441 return did_something
;
445 struct TechmapPass
: public Pass
{
446 TechmapPass() : Pass("techmap", "generic technology mapper") { }
449 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
451 log(" techmap [-map filename] [selection]\n");
453 log("This pass implements a very simple technology mapper that replaces cells in\n");
454 log("the design with implementations given in form of a verilog or ilang source\n");
457 log(" -map filename\n");
458 log(" the library of cell implementations to be used.\n");
459 log(" without this parameter a builtin library is used that\n");
460 log(" transforms the internal RTL cells to the internal gate\n");
463 log(" -share_map filename\n");
464 log(" like -map, but look for the file in the share directory (where the\n");
465 log(" yosys data files are). this is mainly used internally when techmap\n");
466 log(" is called from other commands.\n");
468 log(" -max_iter <number>\n");
469 log(" only run the specified number of iterations.\n");
471 log(" -D <define>, -I <incdir>\n");
472 log(" this options are passed as-is to the verilog frontend for loading the\n");
473 log(" map file. Note that the verilog frontend is also called with the\n");
474 log(" '-ignore_redef' option set.\n");
476 log("When a module in the map file has the 'techmap_celltype' attribute set, it will\n");
477 log("match cells with a type that match the text value of this attribute. Otherwise\n");
478 log("the module name will be used to match the cell.\n");
480 log("When a module in the map file has the 'techmap_simplemap' attribute set, techmap\n");
481 log("will use 'simplemap' (see 'help simplemap') to map cells matching the module.\n");
483 log("All wires in the modules from the map file matching the pattern _TECHMAP_*\n");
484 log("or *._TECHMAP_* are special wires that are used to pass instructions from\n");
485 log("the mapping module to the techmap command. At the moment the following special\n");
486 log("wires are supported:\n");
488 log(" _TECHMAP_FAIL_\n");
489 log(" When this wire is set to a non-zero constant value, techmap will not\n");
490 log(" use this module and instead try the next module with a matching\n");
491 log(" 'techmap_celltype' attribute.\n");
493 log(" When such a wire exists but does not have a constant value after all\n");
494 log(" _TECHMAP_DO_* commands have been executed, an error is generated.\n");
496 log(" _TECHMAP_DO_*\n");
497 log(" This wires are evaluated in alphabetical order. The constant text value\n");
498 log(" of this wire is a yosys command (or sequence of commands) that is run\n");
499 log(" by techmap on the module. A common use case is to run 'proc' on modules\n");
500 log(" that are written using always-statements.\n");
502 log(" When such a wire has a non-constant value at the time it is to be\n");
503 log(" evaluated, an error is produced. That means it is possible for such a\n");
504 log(" wire to start out as non-constant and evaluate to a constant value\n");
505 log(" during processing of other _TECHMAP_DO_* commands.\n");
507 log("In addition to this special wires, techmap also supports special parameters in\n");
508 log("modules in the map file:\n");
510 log(" _TECHMAP_CELLTYPE_\n");
511 log(" When a parameter with this name exists, it will be set to the type name\n");
512 log(" of the cell that matches the module.\n");
514 log(" _TECHMAP_CONSTMSK_<port-name>_\n");
515 log(" _TECHMAP_CONSTVAL_<port-name>_\n");
516 log(" When this pair of parameters is available in a module for a port, then\n");
517 log(" former has a 1-bit for each constant input bit and the latter has the\n");
518 log(" value for this bit. The unused bits of the latter are set to undef (x).\n");
520 log(" _TECHMAP_BITS_CONNMAP_\n");
521 log(" _TECHMAP_CONNMAP_<port-name>_\n");
522 log(" For an N-bit port, the _TECHMAP_CONNMAP_<port-name>_ parameter, if it\n");
523 log(" exists, will be set to an N*_TECHMAP_BITS_CONNMAP_ bit vector containing\n");
524 log(" N words (of _TECHMAP_BITS_CONNMAP_ bits each) that assign each single\n");
525 log(" bit driver a unique id. The values 0-3 are reserved for 0, 1, x, and z.\n");
526 log(" This can be used to detect shorted inputs.\n");
528 log("When a module in the map file has a parameter where the according cell in the\n");
529 log("design has a port, the module from the map file is only used if the port in\n");
530 log("the design is connected to a constant value. The parameter is then set to the\n");
531 log("constant value.\n");
533 log("A cell with the name _TECHMAP_REPLACE_ in the map file will inherit the name\n");
534 log("of the cell that is beeing replaced.\n");
536 log("See 'help extract' for a pass that does the opposite thing.\n");
538 log("See 'help flatten' for a pass that does flatten the design (which is\n");
539 log("esentially techmap but using the design itself as map library).\n");
542 virtual void execute(std::vector
<std::string
> args
, RTLIL::Design
*design
)
544 log_header("Executing TECHMAP pass (map to technology primitives).\n");
547 std::vector
<std::string
> map_files
;
548 std::string verilog_frontend
= "verilog -ignore_redef";
552 std::string proc_share_path
= proc_share_dirname();
553 for (argidx
= 1; argidx
< args
.size(); argidx
++) {
554 if (args
[argidx
] == "-map" && argidx
+1 < args
.size()) {
555 map_files
.push_back(args
[++argidx
]);
558 if (args
[argidx
] == "-share_map" && argidx
+1 < args
.size()) {
559 map_files
.push_back(proc_share_path
+ args
[++argidx
]);
562 if (args
[argidx
] == "-max_iter" && argidx
+1 < args
.size()) {
563 max_iter
= atoi(args
[++argidx
].c_str());
566 if (args
[argidx
] == "-D" && argidx
+1 < args
.size()) {
567 verilog_frontend
+= " -D " + args
[++argidx
];
570 if (args
[argidx
] == "-I" && argidx
+1 < args
.size()) {
571 verilog_frontend
+= " -I " + args
[++argidx
];
576 extra_args(args
, argidx
, design
);
578 TechmapWorker worker
;
579 simplemap_get_mappers(worker
.simplemap_mappers
);
581 RTLIL::Design
*map
= new RTLIL::Design
;
582 if (map_files
.empty()) {
583 FILE *f
= fmemopen(stdcells_code
, strlen(stdcells_code
), "rt");
584 Frontend::frontend_call(map
, f
, "<stdcells.v>", verilog_frontend
);
587 for (auto &fn
: map_files
) {
588 FILE *f
= fopen(fn
.c_str(), "rt");
590 log_cmd_error("Can't open map file `%s'\n", fn
.c_str());
591 Frontend::frontend_call(map
, f
, fn
, (fn
.size() > 3 && fn
.substr(fn
.size()-3) == ".il") ? "ilang" : verilog_frontend
);
595 std::map
<RTLIL::IdString
, RTLIL::Module
*> modules_new
;
596 for (auto &it
: map
->modules
) {
597 if (it
.first
.substr(0, 2) == "\\$")
598 it
.second
->name
= it
.first
.substr(1);
599 modules_new
[it
.second
->name
] = it
.second
;
601 map
->modules
.swap(modules_new
);
603 std::map
<RTLIL::IdString
, std::set
<RTLIL::IdString
>> celltypeMap
;
604 for (auto &it
: map
->modules
) {
605 if (it
.second
->attributes
.count("\\techmap_celltype") && !it
.second
->attributes
.at("\\techmap_celltype").bits
.empty()) {
606 char *p
= strdup(it
.second
->attributes
.at("\\techmap_celltype").decode_string().c_str());
607 for (char *q
= strtok(p
, " \t\r\n"); q
; q
= strtok(NULL
, " \t\r\n"))
608 celltypeMap
[RTLIL::escape_id(q
)].insert(it
.first
);
611 celltypeMap
[it
.first
].insert(it
.first
);
614 bool did_something
= true;
615 std::set
<RTLIL::Cell
*> handled_cells
;
616 while (did_something
) {
617 did_something
= false;
618 for (auto &mod_it
: design
->modules
)
619 if (worker
.techmap_module(design
, mod_it
.second
, map
, handled_cells
, celltypeMap
, false))
620 did_something
= true;
623 if (max_iter
> 0 && --max_iter
== 0)
627 log("No more expansions possible.\n");
634 struct FlattenPass
: public Pass
{
635 FlattenPass() : Pass("flatten", "flatten design") { }
638 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
640 log(" flatten [selection]\n");
642 log("This pass flattens the design by replacing cells by their implementation. This\n");
643 log("pass is very simmilar to the 'techmap' pass. The only difference is that this\n");
644 log("pass is using the current design as mapping library.\n");
647 virtual void execute(std::vector
<std::string
> args
, RTLIL::Design
*design
)
649 log_header("Executing FLATTEN pass (flatten design).\n");
652 extra_args(args
, 1, design
);
654 TechmapWorker worker
;
656 std::map
<RTLIL::IdString
, std::set
<RTLIL::IdString
>> celltypeMap
;
657 for (auto &it
: design
->modules
)
658 celltypeMap
[it
.first
].insert(it
.first
);
660 RTLIL::Module
*top_mod
= NULL
;
661 if (design
->full_selection())
662 for (auto &mod_it
: design
->modules
)
663 if (mod_it
.second
->get_bool_attribute("\\top"))
664 top_mod
= mod_it
.second
;
666 bool did_something
= true;
667 std::set
<RTLIL::Cell
*> handled_cells
;
668 while (did_something
) {
669 did_something
= false;
670 if (top_mod
!= NULL
) {
671 if (worker
.techmap_module(design
, top_mod
, design
, handled_cells
, celltypeMap
, true))
672 did_something
= true;
674 for (auto &mod_it
: design
->modules
)
675 if (worker
.techmap_module(design
, mod_it
.second
, design
, handled_cells
, celltypeMap
, true))
676 did_something
= true;
680 log("No more expansions possible.\n");
682 if (top_mod
!= NULL
) {
683 std::map
<RTLIL::IdString
, RTLIL::Module
*> new_modules
;
684 for (auto &mod_it
: design
->modules
)
685 if (mod_it
.second
== top_mod
|| mod_it
.second
->get_bool_attribute("\\blackbox")) {
686 new_modules
[mod_it
.first
] = mod_it
.second
;
688 log("Deleting now unused module %s.\n", RTLIL::id2cstr(mod_it
.first
));
689 delete mod_it
.second
;
691 design
->modules
.swap(new_modules
);