Fixed techmap/flatten for positional module arguments
[yosys.git] / passes / techmap / techmap.cc
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/register.h"
21 #include "kernel/log.h"
22 #include <stdlib.h>
23 #include <assert.h>
24 #include <stdio.h>
25 #include <string.h>
26
27 #include "passes/techmap/stdcells.inc"
28
29 static void apply_prefix(std::string prefix, std::string &id)
30 {
31 if (id[0] == '\\')
32 id = prefix + "." + id.substr(1);
33 else
34 id = "$techmap" + prefix + "." + id;
35 }
36
37 static void apply_prefix(std::string prefix, RTLIL::SigSpec &sig, RTLIL::Module *module)
38 {
39 for (size_t i = 0; i < sig.chunks.size(); i++) {
40 if (sig.chunks[i].wire == NULL)
41 continue;
42 std::string wire_name = sig.chunks[i].wire->name;
43 apply_prefix(prefix, wire_name);
44 assert(module->wires.count(wire_name) > 0);
45 sig.chunks[i].wire = module->wires[wire_name];
46 }
47 }
48
49 std::map<std::pair<RTLIL::IdString, std::map<RTLIL::IdString, RTLIL::Const>>, RTLIL::Module*> techmap_cache;
50 std::map<RTLIL::Module*, bool> techmap_fail_cache;
51
52 static bool techmap_fail_check(RTLIL::Module *module)
53 {
54 if (module == NULL)
55 return false;
56
57 if (techmap_fail_cache.count(module) > 0)
58 return techmap_fail_cache.at(module);
59
60 for (auto &it : module->wires) {
61 std::string name = it.first;
62 if (name == "\\TECHMAP_FAIL")
63 return techmap_fail_cache[module] = true;
64 if (name.size() > 13 && name[0] == '\\' && name.substr(name.size()-13) == ".TECHMAP_FAIL")
65 return techmap_fail_cache[module] = true;
66 }
67
68 return techmap_fail_cache[module] = false;
69 }
70
71 static void techmap_module_worker(RTLIL::Design *design, RTLIL::Module *module, RTLIL::Cell *cell, RTLIL::Module *tpl, bool flatten_mode)
72 {
73 log("Mapping `%s.%s' using `%s'.\n", RTLIL::id2cstr(module->name), RTLIL::id2cstr(cell->name), RTLIL::id2cstr(tpl->name));
74
75 if (tpl->memories.size() != 0)
76 log_error("Technology map yielded memories -> this is not supported.\n");
77
78 if (tpl->processes.size() != 0)
79 log_error("Technology map yielded processes -> this is not supported.\n");
80
81 std::map<RTLIL::IdString, RTLIL::IdString> positional_ports;
82
83 for (auto &it : tpl->wires) {
84 if (it.second->port_id > 0)
85 positional_ports[stringf("$%d", it.second->port_id)] = it.first;
86 RTLIL::Wire *w = new RTLIL::Wire(*it.second);
87 apply_prefix(cell->name, w->name);
88 w->port_input = false;
89 w->port_output = false;
90 w->port_id = 0;
91 module->wires[w->name] = w;
92 design->select(module, w);
93 }
94
95 for (auto &it : tpl->cells) {
96 RTLIL::Cell *c = new RTLIL::Cell(*it.second);
97 if (!flatten_mode && c->type.substr(0, 2) == "\\$")
98 c->type = c->type.substr(1);
99 apply_prefix(cell->name, c->name);
100 for (auto &it2 : c->connections)
101 apply_prefix(cell->name, it2.second, module);
102 module->cells[c->name] = c;
103 design->select(module, c);
104 }
105
106 for (auto &it : tpl->connections) {
107 RTLIL::SigSig c = it;
108 apply_prefix(cell->name, c.first, module);
109 apply_prefix(cell->name, c.second, module);
110 module->connections.push_back(c);
111 }
112
113 for (auto &it : cell->connections) {
114 RTLIL::IdString portname = it.first;
115 if (positional_ports.count(portname) > 0)
116 portname = positional_ports.at(portname);
117 if (tpl->wires.count(portname) == 0 || tpl->wires.at(portname)->port_id == 0) {
118 if (portname.substr(0, 1) == "$")
119 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());
120 continue;
121 }
122 RTLIL::Wire *w = tpl->wires.at(portname);
123 RTLIL::SigSig c;
124 if (w->port_output) {
125 c.first = it.second;
126 c.second = RTLIL::SigSpec(w);
127 apply_prefix(cell->name, c.second, module);
128 } else {
129 c.first = RTLIL::SigSpec(w);
130 c.second = it.second;
131 apply_prefix(cell->name, c.first, module);
132 }
133 if (c.second.width > c.first.width)
134 c.second.remove(c.first.width, c.second.width - c.first.width);
135 if (c.second.width < c.first.width)
136 c.second.append(RTLIL::SigSpec(RTLIL::State::S0, c.first.width - c.second.width));
137 assert(c.first.width == c.second.width);
138 module->connections.push_back(c);
139 }
140
141 module->cells.erase(cell->name);
142 delete cell;
143 }
144
145 static bool techmap_module(RTLIL::Design *design, RTLIL::Module *module, RTLIL::Design *map, std::set<RTLIL::Cell*> &handled_cells,
146 const std::map<RTLIL::IdString, std::set<RTLIL::IdString>> &celltypeMap, bool flatten_mode)
147 {
148 if (!design->selected(module))
149 return false;
150
151 bool did_something = false;
152
153 std::vector<std::string> cell_names;
154
155 for (auto &cell_it : module->cells)
156 cell_names.push_back(cell_it.first);
157
158 for (auto &cell_name : cell_names)
159 {
160 if (module->cells.count(cell_name) == 0)
161 continue;
162
163 RTLIL::Cell *cell = module->cells[cell_name];
164
165 if (!design->selected(module, cell) || handled_cells.count(cell) > 0)
166 continue;
167
168 if (celltypeMap.count(cell->type) == 0)
169 continue;
170
171 for (auto &tpl_name : celltypeMap.at(cell->type))
172 {
173 std::string derived_name = tpl_name;
174 RTLIL::Module *tpl = map->modules[tpl_name];
175 std::map<RTLIL::IdString, RTLIL::Const> parameters = cell->parameters;
176
177 for (auto conn : cell->connections) {
178 if (conn.first.substr(0, 1) == "$")
179 continue;
180 if (tpl->wires.count(conn.first) > 0 && tpl->wires.at(conn.first)->port_id > 0)
181 continue;
182 if (!conn.second.is_fully_const() || parameters.count(conn.first) > 0)
183 goto next_tpl;
184 parameters[conn.first] = conn.second.as_const();
185 }
186
187 if (0) {
188 next_tpl:
189 continue;
190 }
191
192 std::pair<RTLIL::IdString, std::map<RTLIL::IdString, RTLIL::Const>> key(tpl_name, parameters);
193 if (techmap_cache.count(key) > 0) {
194 tpl = techmap_cache[key];
195 } else {
196 if (cell->parameters.size() != 0) {
197 derived_name = tpl->derive(map, parameters);
198 tpl = map->modules[derived_name];
199 log_header("Continuing TECHMAP pass.\n");
200 }
201 techmap_cache[key] = tpl;
202 }
203
204 if (techmap_fail_check(tpl)) {
205 log("Not using module `%s' from techmap as it contains a TECHMAP_FAIL marker wire.\n", derived_name.c_str());
206 continue;
207 }
208
209 techmap_module_worker(design, module, cell, tpl, flatten_mode);
210 did_something = true;
211 cell = NULL;
212 break;
213 }
214
215 handled_cells.insert(cell);
216 }
217
218 return did_something;
219 }
220
221 struct TechmapPass : public Pass {
222 TechmapPass() : Pass("techmap", "simple technology mapper") { }
223 virtual void help()
224 {
225 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
226 log("\n");
227 log(" techmap [-map filename] [selection]\n");
228 log("\n");
229 log("This pass implements a very simple technology mapper that replaces cells in\n");
230 log("the design with implementations given in form of a verilog or ilang source\n");
231 log("file.\n");
232 log("\n");
233 log(" -map filename\n");
234 log(" the library of cell implementations to be used.\n");
235 log(" without this parameter a builtin library is used that\n");
236 log(" transforms the internal RTL cells to the internal gate\n");
237 log(" library.\n");
238 log("\n");
239 log("When a module in the map file has the 'celltype' attribute set, it will match\n");
240 log("cells with a type that match the text value of this attribute.\n");
241 log("\n");
242 log("When a module in the map file contains a wire with the name 'TECHMAP_FAIL' (or\n");
243 log("one matching '*.TECHMAP_FAIL') then no substitution will be performed. The\n");
244 log("modules in the map file are tried in alphabetical order.\n");
245 log("\n");
246 log("When a module in the map file has a parameter where the according cell in the\n");
247 log("design has a port, the module from the map file is only used if the port in\n");
248 log("the design is connected to a constant value. The parameter is then set to the\n");
249 log("constant value.\n");
250 log("\n");
251 log("See 'help extract' for a pass that does the opposite thing.\n");
252 log("\n");
253 log("See 'help flatten' for a pass that does flatten the design (which is\n");
254 log("esentially techmap but using the design itself as map library).\n");
255 log("\n");
256 }
257 virtual void execute(std::vector<std::string> args, RTLIL::Design *design)
258 {
259 log_header("Executing TECHMAP pass (map to technology primitives).\n");
260 log_push();
261
262 std::string filename;
263
264 size_t argidx;
265 for (argidx = 1; argidx < args.size(); argidx++) {
266 if (args[argidx] == "-map" && argidx+1 < args.size()) {
267 filename = args[++argidx];
268 continue;
269 }
270 break;
271 }
272 extra_args(args, argidx, design);
273
274 FILE *f = filename.empty() ? fmemopen(stdcells_code, strlen(stdcells_code), "rt") : fopen(filename.c_str(), "rt");
275 if (f == NULL)
276 log_cmd_error("Can't open map file `%s'\n", filename.c_str());
277
278 RTLIL::Design *map = new RTLIL::Design;
279 Frontend::frontend_call(map, f, filename.empty() ? "<stdcells.v>" : filename,
280 (filename.size() > 3 && filename.substr(filename.size()-3) == ".il") ? "ilang" : "verilog");
281
282 fclose(f);
283
284 std::map<RTLIL::IdString, RTLIL::Module*> modules_new;
285 for (auto &it : map->modules) {
286 if (it.first.substr(0, 2) == "\\$")
287 it.second->name = it.first.substr(1);
288 modules_new[it.second->name] = it.second;
289 }
290 map->modules.swap(modules_new);
291
292 std::map<RTLIL::IdString, std::set<RTLIL::IdString>> celltypeMap;
293 for (auto &it : map->modules) {
294 if (it.second->attributes.count("\\celltype") && !it.second->attributes.at("\\celltype").str.empty()) {
295 celltypeMap[RTLIL::escape_id(it.second->attributes.at("\\celltype").str)].insert(it.first);
296 } else
297 celltypeMap[it.first].insert(it.first);
298 }
299
300 bool did_something = true;
301 std::set<RTLIL::Cell*> handled_cells;
302 while (did_something) {
303 did_something = false;
304 for (auto &mod_it : design->modules)
305 if (techmap_module(design, mod_it.second, map, handled_cells, celltypeMap, false))
306 did_something = true;
307 }
308
309 log("No more expansions possible.\n");
310 techmap_cache.clear();
311 techmap_fail_cache.clear();
312 delete map;
313 log_pop();
314 }
315 } TechmapPass;
316
317 struct FlattenPass : public Pass {
318 FlattenPass() : Pass("flatten", "flatten design") { }
319 virtual void help()
320 {
321 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
322 log("\n");
323 log(" flatten [selection]\n");
324 log("\n");
325 log("This pass flattens the design by replacing cells by their implementation. This\n");
326 log("pass is very simmilar to the 'techmap' pass. The only difference is that this\n");
327 log("pass is using the current design as mapping library.\n");
328 log("\n");
329 }
330 virtual void execute(std::vector<std::string> args, RTLIL::Design *design)
331 {
332 log_header("Executing FLATTEN pass (flatten design).\n");
333 log_push();
334
335 extra_args(args, 1, design);
336
337 std::map<RTLIL::IdString, std::set<RTLIL::IdString>> celltypeMap;
338 for (auto &it : design->modules)
339 celltypeMap[it.first].insert(it.first);
340
341 bool did_something = true;
342 std::set<RTLIL::Cell*> handled_cells;
343 while (did_something) {
344 did_something = false;
345 for (auto &mod_it : design->modules)
346 if (techmap_module(design, mod_it.second, design, handled_cells, celltypeMap, true))
347 did_something = true;
348 }
349
350 log("No more expansions possible.\n");
351 techmap_cache.clear();
352 techmap_fail_cache.clear();
353 log_pop();
354 }
355 } FlattenPass;
356