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/toposort.h"
24 #include "kernel/log.h"
30 #include "passes/techmap/stdcells.inc"
33 extern void simplemap_get_mappers(std::map
<std::string
, void(*)(RTLIL::Module
*, RTLIL::Cell
*)> &mappers
);
35 static void apply_prefix(std::string prefix
, std::string
&id
)
38 id
= prefix
+ "." + id
.substr(1);
40 id
= "$techmap" + prefix
+ "." + id
;
43 static void apply_prefix(std::string prefix
, RTLIL::SigSpec
&sig
, RTLIL::Module
*module
)
45 std::vector
<RTLIL::SigChunk
> chunks
= sig
;
46 for (auto &chunk
: chunks
)
47 if (chunk
.wire
!= NULL
) {
48 std::string wire_name
= chunk
.wire
->name
;
49 apply_prefix(prefix
, wire_name
);
50 assert(module
->wires_
.count(wire_name
) > 0);
51 chunk
.wire
= module
->wires_
[wire_name
];
58 std::map
<std::string
, void(*)(RTLIL::Module
*, RTLIL::Cell
*)> simplemap_mappers
;
59 std::map
<std::pair
<RTLIL::IdString
, std::map
<RTLIL::IdString
, RTLIL::Const
>>, RTLIL::Module
*> techmap_cache
;
60 std::map
<RTLIL::Module
*, bool> techmap_do_cache
;
62 struct TechmapWireData
{
67 typedef std::map
<std::string
, std::vector
<TechmapWireData
>> TechmapWires
;
69 TechmapWires
techmap_find_special_wires(RTLIL::Module
*module
)
76 for (auto &it
: module
->wires_
) {
77 const char *p
= it
.first
.c_str();
81 const char *q
= strrchr(p
+1, '.');
84 if (!strncmp(p
, "_TECHMAP_", 9)) {
85 TechmapWireData record
;
86 record
.wire
= it
.second
;
87 record
.value
= it
.second
;
88 result
[p
].push_back(record
);
89 it
.second
->attributes
["\\keep"] = RTLIL::Const(1);
90 it
.second
->attributes
["\\_techmap_special_"] = RTLIL::Const(1);
94 if (!result
.empty()) {
95 SigMap
sigmap(module
);
96 for (auto &it1
: result
)
97 for (auto &it2
: it1
.second
)
98 sigmap
.apply(it2
.value
);
104 void techmap_module_worker(RTLIL::Design
*design
, RTLIL::Module
*module
, RTLIL::Cell
*cell
, RTLIL::Module
*tpl
, bool flatten_mode
)
106 log("Mapping `%s.%s' using `%s'.\n", RTLIL::id2cstr(module
->name
), RTLIL::id2cstr(cell
->name
), RTLIL::id2cstr(tpl
->name
));
108 if (tpl
->memories
.size() != 0)
109 log_error("Technology map yielded memories -> this is not supported.\n");
111 if (tpl
->processes
.size() != 0) {
112 log("Technology map yielded processes:\n");
113 for (auto &it
: tpl
->processes
)
114 log(" %s",RTLIL::id2cstr(it
.first
));
115 log_error("Technology map yielded processes -> this is not supported.\n");
118 std::string orig_cell_name
;
120 for (auto &it
: tpl
->cells_
)
121 if (it
.first
== "\\_TECHMAP_REPLACE_") {
122 orig_cell_name
= cell
->name
;
123 module
->rename(cell
, stringf("$techmap%d", RTLIL::autoidx
++) + cell
->name
);
127 std::map
<RTLIL::IdString
, RTLIL::IdString
> positional_ports
;
129 for (auto &it
: tpl
->wires_
) {
130 if (it
.second
->port_id
> 0)
131 positional_ports
[stringf("$%d", it
.second
->port_id
)] = it
.first
;
132 std::string w_name
= it
.second
->name
;
133 apply_prefix(cell
->name
, w_name
);
134 RTLIL::Wire
*w
= module
->addWire(w_name
, it
.second
);
135 w
->port_input
= false;
136 w
->port_output
= false;
138 if (it
.second
->get_bool_attribute("\\_techmap_special_"))
139 w
->attributes
.clear();
140 design
->select(module
, w
);
143 SigMap port_signal_map
;
145 for (auto &it
: cell
->connections()) {
146 RTLIL::IdString portname
= it
.first
;
147 if (positional_ports
.count(portname
) > 0)
148 portname
= positional_ports
.at(portname
);
149 if (tpl
->wires_
.count(portname
) == 0 || tpl
->wires_
.at(portname
)->port_id
== 0) {
150 if (portname
.substr(0, 1) == "$")
151 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());
154 RTLIL::Wire
*w
= tpl
->wires_
.at(portname
);
156 if (w
->port_output
) {
158 c
.second
= RTLIL::SigSpec(w
);
159 apply_prefix(cell
->name
, c
.second
, module
);
161 c
.first
= RTLIL::SigSpec(w
);
162 c
.second
= it
.second
;
163 apply_prefix(cell
->name
, c
.first
, module
);
165 if (c
.second
.size() > c
.first
.size())
166 c
.second
.remove(c
.first
.size(), c
.second
.size() - c
.first
.size());
167 if (c
.second
.size() < c
.first
.size())
168 c
.second
.append(RTLIL::SigSpec(RTLIL::State::S0
, c
.first
.size() - c
.second
.size()));
169 assert(c
.first
.size() == c
.second
.size());
171 // more conservative approach:
172 // connect internal and external wires
175 // approach that yields nicer outputs:
176 // replace internal wires that are connected to external wires
178 port_signal_map
.add(c
.second
, c
.first
);
180 port_signal_map
.add(c
.first
, c
.second
);
184 for (auto &it
: tpl
->cells_
)
186 RTLIL::IdString c_name
= it
.second
->name
;
188 if (!flatten_mode
&& c_name
== "\\_TECHMAP_REPLACE_")
189 c_name
= orig_cell_name
;
191 apply_prefix(cell
->name
, c_name
);
193 RTLIL::Cell
*c
= module
->addCell(c_name
, it
.second
);
194 design
->select(module
, c
);
196 if (!flatten_mode
&& c
->type
.substr(0, 2) == "\\$")
197 c
->type
= c
->type
.substr(1);
199 for (auto &it2
: c
->connections_
) {
200 apply_prefix(cell
->name
, it2
.second
, module
);
201 port_signal_map
.apply(it2
.second
);
205 for (auto &it
: tpl
->connections()) {
206 RTLIL::SigSig c
= it
;
207 apply_prefix(cell
->name
, c
.first
, module
);
208 apply_prefix(cell
->name
, c
.second
, module
);
209 port_signal_map
.apply(c
.first
);
210 port_signal_map
.apply(c
.second
);
214 module
->remove(cell
);
217 bool techmap_module(RTLIL::Design
*design
, RTLIL::Module
*module
, RTLIL::Design
*map
, std::set
<RTLIL::Cell
*> &handled_cells
,
218 const std::map
<RTLIL::IdString
, std::set
<RTLIL::IdString
>> &celltypeMap
, bool flatten_mode
)
220 if (!design
->selected(module
))
223 bool log_continue
= false;
224 bool did_something
= false;
226 SigMap
sigmap(module
);
228 TopoSort
<RTLIL::Cell
*> cells
;
229 std::map
<RTLIL::Cell
*, std::set
<RTLIL::SigBit
>> cell_to_inbit
;
230 std::map
<RTLIL::SigBit
, std::set
<RTLIL::Cell
*>> outbit_to_cell
;
232 for (auto cell
: module
->cells())
234 if (!design
->selected(module
, cell
) || handled_cells
.count(cell
) > 0)
237 if (celltypeMap
.count(cell
->type
) == 0)
240 for (auto &conn
: cell
->connections())
242 RTLIL::SigSpec sig
= sigmap(conn
.second
);
248 for (auto &tpl_name
: celltypeMap
.at(cell
->type
)) {
249 RTLIL::Module
*tpl
= map
->modules_
[tpl_name
];
250 RTLIL::Wire
*port
= tpl
->wire(conn
.first
);
251 if (port
&& port
->port_input
)
252 cell_to_inbit
[cell
].insert(sig
.begin(), sig
.end());
253 if (port
&& port
->port_output
)
254 for (auto &bit
: sig
)
255 outbit_to_cell
[bit
].insert(cell
);
262 for (auto &it_right
: cell_to_inbit
)
263 for (auto &it_sigbit
: it_right
.second
)
264 for (auto &it_left
: outbit_to_cell
[it_sigbit
])
265 cells
.edge(it_left
, it_right
.first
);
269 for (auto cell
: cells
.sorted
)
271 log_assert(handled_cells
.count(cell
) == 0);
272 log_assert(cell
== module
->cell(cell
->name
));
274 for (auto &tpl_name
: celltypeMap
.at(cell
->type
))
276 std::string derived_name
= tpl_name
;
277 RTLIL::Module
*tpl
= map
->modules_
[tpl_name
];
278 std::map
<RTLIL::IdString
, RTLIL::Const
> parameters
= cell
->parameters
;
280 if (tpl
->get_bool_attribute("\\blackbox"))
285 if (tpl
->get_bool_attribute("\\techmap_simplemap")) {
286 log("Mapping %s.%s (%s) with simplemap.\n", RTLIL::id2cstr(module
->name
), RTLIL::id2cstr(cell
->name
), RTLIL::id2cstr(cell
->type
));
287 if (simplemap_mappers
.count(cell
->type
) == 0)
288 log_error("No simplemap mapper for cell type %s found!\n", RTLIL::id2cstr(cell
->type
));
289 simplemap_mappers
.at(cell
->type
)(module
, cell
);
290 module
->remove(cell
);
292 did_something
= true;
296 for (auto conn
: cell
->connections()) {
297 if (conn
.first
.substr(0, 1) == "$")
299 if (tpl
->wires_
.count(conn
.first
) > 0 && tpl
->wires_
.at(conn
.first
)->port_id
> 0)
301 if (!conn
.second
.is_fully_const() || parameters
.count(conn
.first
) > 0 || tpl
->avail_parameters
.count(conn
.first
) == 0)
303 parameters
[conn
.first
] = conn
.second
.as_const();
311 if (tpl
->avail_parameters
.count("\\_TECHMAP_CELLTYPE_") != 0)
312 parameters
["\\_TECHMAP_CELLTYPE_"] = RTLIL::unescape_id(cell
->type
);
314 for (auto conn
: cell
->connections()) {
315 if (tpl
->avail_parameters
.count(stringf("\\_TECHMAP_CONSTMSK_%s_", RTLIL::id2cstr(conn
.first
))) != 0) {
316 std::vector
<RTLIL::SigBit
> v
= sigmap(conn
.second
).to_sigbit_vector();
318 bit
= RTLIL::SigBit(bit
.wire
== NULL
? RTLIL::State::S1
: RTLIL::State::S0
);
319 parameters
[stringf("\\_TECHMAP_CONSTMSK_%s_", RTLIL::id2cstr(conn
.first
))] = RTLIL::SigSpec(v
).as_const();
321 if (tpl
->avail_parameters
.count(stringf("\\_TECHMAP_CONSTVAL_%s_", RTLIL::id2cstr(conn
.first
))) != 0) {
322 std::vector
<RTLIL::SigBit
> v
= sigmap(conn
.second
).to_sigbit_vector();
324 if (bit
.wire
!= NULL
)
325 bit
= RTLIL::SigBit(RTLIL::State::Sx
);
326 parameters
[stringf("\\_TECHMAP_CONSTVAL_%s_", RTLIL::id2cstr(conn
.first
))] = RTLIL::SigSpec(v
).as_const();
330 int unique_bit_id_counter
= 0;
331 std::map
<RTLIL::SigBit
, int> unique_bit_id
;
332 unique_bit_id
[RTLIL::State::S0
] = unique_bit_id_counter
++;
333 unique_bit_id
[RTLIL::State::S1
] = unique_bit_id_counter
++;
334 unique_bit_id
[RTLIL::State::Sx
] = unique_bit_id_counter
++;
335 unique_bit_id
[RTLIL::State::Sz
] = unique_bit_id_counter
++;
337 for (auto conn
: cell
->connections())
338 if (tpl
->avail_parameters
.count(stringf("\\_TECHMAP_CONNMAP_%s_", RTLIL::id2cstr(conn
.first
))) != 0) {
339 for (auto &bit
: sigmap(conn
.second
).to_sigbit_vector())
340 if (unique_bit_id
.count(bit
) == 0)
341 unique_bit_id
[bit
] = unique_bit_id_counter
++;
345 for (int i
= 0; i
< 32; i
++)
346 if (((unique_bit_id_counter
-1) & (1 << i
)) != 0)
348 if (tpl
->avail_parameters
.count("\\_TECHMAP_BITS_CONNMAP_"))
349 parameters
["\\_TECHMAP_BITS_CONNMAP_"] = bits
;
351 for (auto conn
: cell
->connections())
352 if (tpl
->avail_parameters
.count(stringf("\\_TECHMAP_CONNMAP_%s_", RTLIL::id2cstr(conn
.first
))) != 0) {
354 for (auto &bit
: sigmap(conn
.second
).to_sigbit_vector()) {
355 RTLIL::Const
chunk(unique_bit_id
.at(bit
), bits
);
356 value
.bits
.insert(value
.bits
.end(), chunk
.bits
.begin(), chunk
.bits
.end());
358 parameters
[stringf("\\_TECHMAP_CONNMAP_%s_", RTLIL::id2cstr(conn
.first
))] = value
;
362 std::pair
<RTLIL::IdString
, std::map
<RTLIL::IdString
, RTLIL::Const
>> key(tpl_name
, parameters
);
363 if (techmap_cache
.count(key
) > 0) {
364 tpl
= techmap_cache
[key
];
366 if (cell
->parameters
.size() != 0) {
367 derived_name
= tpl
->derive(map
, parameters
);
368 tpl
= map
->modules_
[derived_name
];
371 techmap_cache
[key
] = tpl
;
375 techmap_do_cache
[tpl
] = true;
377 if (techmap_do_cache
.count(tpl
) == 0)
379 bool keep_running
= true;
380 techmap_do_cache
[tpl
] = true;
382 std::set
<std::string
> techmap_wire_names
;
386 TechmapWires twd
= techmap_find_special_wires(tpl
);
387 keep_running
= false;
390 techmap_wire_names
.insert(it
.first
);
392 for (auto &it
: twd
["_TECHMAP_FAIL_"]) {
393 RTLIL::SigSpec value
= it
.value
;
394 if (value
.is_fully_const() && value
.as_bool()) {
395 log("Not using module `%s' from techmap as it contains a %s marker wire with non-zero value %s.\n",
396 derived_name
.c_str(), RTLIL::id2cstr(it
.wire
->name
), log_signal(value
));
397 techmap_do_cache
[tpl
] = false;
401 if (!techmap_do_cache
[tpl
])
406 if (it
.first
.substr(0, 12) != "_TECHMAP_DO_" || it
.second
.empty())
409 auto &data
= it
.second
.front();
411 if (!data
.value
.is_fully_const())
412 log_error("Techmap yielded config wire %s with non-const value %s.\n", RTLIL::id2cstr(data
.wire
->name
), log_signal(data
.value
));
414 techmap_wire_names
.erase(it
.first
);
416 const char *p
= data
.wire
->name
.c_str();
417 const char *q
= strrchr(p
+1, '.');
420 assert(!strncmp(q
, "_TECHMAP_DO_", 12));
421 std::string new_name
= data
.wire
->name
.substr(0, q
-p
) + "_TECHMAP_DONE_" + data
.wire
->name
.substr(q
-p
+12);
422 while (tpl
->wires_
.count(new_name
))
424 tpl
->rename(data
.wire
, new_name
);
426 std::string cmd_string
= data
.value
.as_const().decode_string();
427 Pass::call_on_module(map
, tpl
, cmd_string
);
434 TechmapWires twd
= techmap_find_special_wires(tpl
);
435 for (auto &it
: twd
) {
436 if (it
.first
!= "_TECHMAP_FAIL_" && it
.first
.substr(0, 12) != "_TECHMAP_DO_" && it
.first
.substr(0, 14) != "_TECHMAP_DONE_")
437 log_error("Techmap yielded unknown config wire %s.\n", it
.first
.c_str());
438 if (techmap_do_cache
[tpl
])
439 for (auto &it2
: it
.second
)
440 if (!it2
.value
.is_fully_const())
441 log_error("Techmap yielded config wire %s with non-const value %s.\n", RTLIL::id2cstr(it2
.wire
->name
), log_signal(it2
.value
));
442 techmap_wire_names
.erase(it
.first
);
445 for (auto &it
: techmap_wire_names
)
446 log_error("Techmap special wire %s disappeared. This is considered a fatal error.\n", RTLIL::id2cstr(it
));
449 if (techmap_do_cache
.at(tpl
) == false)
453 log_header("Continuing TECHMAP pass.\n");
454 log_continue
= false;
457 techmap_module_worker(design
, module
, cell
, tpl
, flatten_mode
);
458 did_something
= true;
463 handled_cells
.insert(cell
);
467 log_header("Continuing TECHMAP pass.\n");
468 log_continue
= false;
471 return did_something
;
475 struct TechmapPass
: public Pass
{
476 TechmapPass() : Pass("techmap", "generic technology mapper") { }
479 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
481 log(" techmap [-map filename] [selection]\n");
483 log("This pass implements a very simple technology mapper that replaces cells in\n");
484 log("the design with implementations given in form of a verilog or ilang source\n");
487 log(" -map filename\n");
488 log(" the library of cell implementations to be used.\n");
489 log(" without this parameter a builtin library is used that\n");
490 log(" transforms the internal RTL cells to the internal gate\n");
493 log(" -share_map filename\n");
494 log(" like -map, but look for the file in the share directory (where the\n");
495 log(" yosys data files are). this is mainly used internally when techmap\n");
496 log(" is called from other commands.\n");
498 log(" -max_iter <number>\n");
499 log(" only run the specified number of iterations.\n");
501 log(" -D <define>, -I <incdir>\n");
502 log(" this options are passed as-is to the verilog frontend for loading the\n");
503 log(" map file. Note that the verilog frontend is also called with the\n");
504 log(" '-ignore_redef' option set.\n");
506 log("When a module in the map file has the 'techmap_celltype' attribute set, it will\n");
507 log("match cells with a type that match the text value of this attribute. Otherwise\n");
508 log("the module name will be used to match the cell.\n");
510 log("When a module in the map file has the 'techmap_simplemap' attribute set, techmap\n");
511 log("will use 'simplemap' (see 'help simplemap') to map cells matching the module.\n");
513 log("All wires in the modules from the map file matching the pattern _TECHMAP_*\n");
514 log("or *._TECHMAP_* are special wires that are used to pass instructions from\n");
515 log("the mapping module to the techmap command. At the moment the following special\n");
516 log("wires are supported:\n");
518 log(" _TECHMAP_FAIL_\n");
519 log(" When this wire is set to a non-zero constant value, techmap will not\n");
520 log(" use this module and instead try the next module with a matching\n");
521 log(" 'techmap_celltype' attribute.\n");
523 log(" When such a wire exists but does not have a constant value after all\n");
524 log(" _TECHMAP_DO_* commands have been executed, an error is generated.\n");
526 log(" _TECHMAP_DO_*\n");
527 log(" This wires are evaluated in alphabetical order. The constant text value\n");
528 log(" of this wire is a yosys command (or sequence of commands) that is run\n");
529 log(" by techmap on the module. A common use case is to run 'proc' on modules\n");
530 log(" that are written using always-statements.\n");
532 log(" When such a wire has a non-constant value at the time it is to be\n");
533 log(" evaluated, an error is produced. That means it is possible for such a\n");
534 log(" wire to start out as non-constant and evaluate to a constant value\n");
535 log(" during processing of other _TECHMAP_DO_* commands.\n");
537 log("In addition to this special wires, techmap also supports special parameters in\n");
538 log("modules in the map file:\n");
540 log(" _TECHMAP_CELLTYPE_\n");
541 log(" When a parameter with this name exists, it will be set to the type name\n");
542 log(" of the cell that matches the module.\n");
544 log(" _TECHMAP_CONSTMSK_<port-name>_\n");
545 log(" _TECHMAP_CONSTVAL_<port-name>_\n");
546 log(" When this pair of parameters is available in a module for a port, then\n");
547 log(" former has a 1-bit for each constant input bit and the latter has the\n");
548 log(" value for this bit. The unused bits of the latter are set to undef (x).\n");
550 log(" _TECHMAP_BITS_CONNMAP_\n");
551 log(" _TECHMAP_CONNMAP_<port-name>_\n");
552 log(" For an N-bit port, the _TECHMAP_CONNMAP_<port-name>_ parameter, if it\n");
553 log(" exists, will be set to an N*_TECHMAP_BITS_CONNMAP_ bit vector containing\n");
554 log(" N words (of _TECHMAP_BITS_CONNMAP_ bits each) that assign each single\n");
555 log(" bit driver a unique id. The values 0-3 are reserved for 0, 1, x, and z.\n");
556 log(" This can be used to detect shorted inputs.\n");
558 log("When a module in the map file has a parameter where the according cell in the\n");
559 log("design has a port, the module from the map file is only used if the port in\n");
560 log("the design is connected to a constant value. The parameter is then set to the\n");
561 log("constant value.\n");
563 log("A cell with the name _TECHMAP_REPLACE_ in the map file will inherit the name\n");
564 log("of the cell that is beeing replaced.\n");
566 log("See 'help extract' for a pass that does the opposite thing.\n");
568 log("See 'help flatten' for a pass that does flatten the design (which is\n");
569 log("esentially techmap but using the design itself as map library).\n");
572 virtual void execute(std::vector
<std::string
> args
, RTLIL::Design
*design
)
574 log_header("Executing TECHMAP pass (map to technology primitives).\n");
577 std::vector
<std::string
> map_files
;
578 std::string verilog_frontend
= "verilog -ignore_redef";
582 std::string proc_share_path
= proc_share_dirname();
583 for (argidx
= 1; argidx
< args
.size(); argidx
++) {
584 if (args
[argidx
] == "-map" && argidx
+1 < args
.size()) {
585 map_files
.push_back(args
[++argidx
]);
588 if (args
[argidx
] == "-share_map" && argidx
+1 < args
.size()) {
589 map_files
.push_back(proc_share_path
+ args
[++argidx
]);
592 if (args
[argidx
] == "-max_iter" && argidx
+1 < args
.size()) {
593 max_iter
= atoi(args
[++argidx
].c_str());
596 if (args
[argidx
] == "-D" && argidx
+1 < args
.size()) {
597 verilog_frontend
+= " -D " + args
[++argidx
];
600 if (args
[argidx
] == "-I" && argidx
+1 < args
.size()) {
601 verilog_frontend
+= " -I " + args
[++argidx
];
606 extra_args(args
, argidx
, design
);
608 TechmapWorker worker
;
609 simplemap_get_mappers(worker
.simplemap_mappers
);
611 RTLIL::Design
*map
= new RTLIL::Design
;
612 if (map_files
.empty()) {
613 FILE *f
= fmemopen(stdcells_code
, strlen(stdcells_code
), "rt");
614 Frontend::frontend_call(map
, f
, "<stdcells.v>", verilog_frontend
);
617 for (auto &fn
: map_files
) {
618 FILE *f
= fopen(fn
.c_str(), "rt");
620 log_cmd_error("Can't open map file `%s'\n", fn
.c_str());
621 Frontend::frontend_call(map
, f
, fn
, (fn
.size() > 3 && fn
.substr(fn
.size()-3) == ".il") ? "ilang" : verilog_frontend
);
625 std::map
<RTLIL::IdString
, RTLIL::Module
*> modules_new
;
626 for (auto &it
: map
->modules_
) {
627 if (it
.first
.substr(0, 2) == "\\$")
628 it
.second
->name
= it
.first
.substr(1);
629 modules_new
[it
.second
->name
] = it
.second
;
631 map
->modules_
.swap(modules_new
);
633 std::map
<RTLIL::IdString
, std::set
<RTLIL::IdString
>> celltypeMap
;
634 for (auto &it
: map
->modules_
) {
635 if (it
.second
->attributes
.count("\\techmap_celltype") && !it
.second
->attributes
.at("\\techmap_celltype").bits
.empty()) {
636 char *p
= strdup(it
.second
->attributes
.at("\\techmap_celltype").decode_string().c_str());
637 for (char *q
= strtok(p
, " \t\r\n"); q
; q
= strtok(NULL
, " \t\r\n"))
638 celltypeMap
[RTLIL::escape_id(q
)].insert(it
.first
);
641 celltypeMap
[it
.first
].insert(it
.first
);
644 for (auto module
: design
->modules()) {
645 bool did_something
= true;
646 std::set
<RTLIL::Cell
*> handled_cells
;
647 while (did_something
) {
648 did_something
= false;
649 if (worker
.techmap_module(design
, module
, map
, handled_cells
, celltypeMap
, false))
650 did_something
= true;
653 if (max_iter
> 0 && --max_iter
== 0)
658 log("No more expansions possible.\n");
665 struct FlattenPass
: public Pass
{
666 FlattenPass() : Pass("flatten", "flatten design") { }
669 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
671 log(" flatten [selection]\n");
673 log("This pass flattens the design by replacing cells by their implementation. This\n");
674 log("pass is very simmilar to the 'techmap' pass. The only difference is that this\n");
675 log("pass is using the current design as mapping library.\n");
678 virtual void execute(std::vector
<std::string
> args
, RTLIL::Design
*design
)
680 log_header("Executing FLATTEN pass (flatten design).\n");
683 extra_args(args
, 1, design
);
685 TechmapWorker worker
;
687 std::map
<RTLIL::IdString
, std::set
<RTLIL::IdString
>> celltypeMap
;
688 for (auto &it
: design
->modules_
)
689 celltypeMap
[it
.first
].insert(it
.first
);
691 RTLIL::Module
*top_mod
= NULL
;
692 if (design
->full_selection())
693 for (auto mod
: design
->modules())
694 if (mod
->get_bool_attribute("\\top"))
697 bool did_something
= true;
698 std::set
<RTLIL::Cell
*> handled_cells
;
699 while (did_something
) {
700 did_something
= false;
701 if (top_mod
!= NULL
) {
702 if (worker
.techmap_module(design
, top_mod
, design
, handled_cells
, celltypeMap
, true))
703 did_something
= true;
705 for (auto mod
: design
->modules())
706 if (worker
.techmap_module(design
, mod
, design
, handled_cells
, celltypeMap
, true))
707 did_something
= true;
711 log("No more expansions possible.\n");
713 if (top_mod
!= NULL
) {
714 std::map
<RTLIL::IdString
, RTLIL::Module
*> new_modules
;
715 for (auto mod
: design
->modules())
716 if (mod
== top_mod
|| mod
->get_bool_attribute("\\blackbox")) {
717 new_modules
[mod
->name
] = mod
;
719 log("Deleting now unused module %s.\n", log_id(mod
));
722 design
->modules_
.swap(new_modules
);