Added proper TECHMAP_FAIL support and added support for the celltype attribute in...
[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 = 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)
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.");
77
78 if (tpl->processes.size() != 0)
79 log_error("Technology map yielded processes -> this is not supported.");
80
81 for (auto &it : tpl->wires) {
82 RTLIL::Wire *w = new RTLIL::Wire(*it.second);
83 apply_prefix(cell->name, w->name);
84 w->port_input = false;
85 w->port_output = false;
86 w->port_id = 0;
87 module->wires[w->name] = w;
88 design->select(module, w);
89 }
90
91 for (auto &it : tpl->cells) {
92 RTLIL::Cell *c = new RTLIL::Cell(*it.second);
93 if (c->type.substr(0, 2) == "\\$")
94 c->type = c->type.substr(1);
95 apply_prefix(cell->name, c->name);
96 for (auto &it2 : c->connections)
97 apply_prefix(cell->name, it2.second, module);
98 module->cells[c->name] = c;
99 design->select(module, c);
100 }
101
102 for (auto &it : tpl->connections) {
103 RTLIL::SigSig c = it;
104 apply_prefix(cell->name, c.first, module);
105 apply_prefix(cell->name, c.second, module);
106 module->connections.push_back(c);
107 }
108
109 for (auto &it : cell->connections) {
110 assert(tpl->wires.count(it.first));
111 assert(tpl->wires[it.first]->port_id > 0);
112 RTLIL::Wire *w = tpl->wires[it.first];
113 RTLIL::SigSig c;
114 if (w->port_output) {
115 c.first = it.second;
116 c.second = RTLIL::SigSpec(w);
117 apply_prefix(cell->name, c.second, module);
118 } else {
119 c.first = RTLIL::SigSpec(w);
120 c.second = it.second;
121 apply_prefix(cell->name, c.first, module);
122 }
123 if (c.second.width > c.first.width)
124 c.second.remove(c.first.width, c.second.width - c.first.width);
125 if (c.second.width < c.first.width)
126 c.second.append(RTLIL::SigSpec(RTLIL::State::S0, c.first.width - c.second.width));
127 assert(c.first.width == c.second.width);
128 module->connections.push_back(c);
129 }
130
131 module->cells.erase(cell->name);
132 delete cell;
133 }
134
135 static bool techmap_module(RTLIL::Design *design, RTLIL::Module *module, RTLIL::Design *map, std::set<RTLIL::Cell*> &handled_cells,
136 const std::map<RTLIL::IdString, std::set<RTLIL::IdString>> &celltypeMap)
137 {
138 if (!design->selected(module))
139 return false;
140
141 bool did_something = false;
142
143 std::vector<std::string> cell_names;
144
145 for (auto &cell_it : module->cells)
146 cell_names.push_back(cell_it.first);
147
148 for (auto &cell_name : cell_names)
149 {
150 if (module->cells.count(cell_name) == 0)
151 continue;
152
153 RTLIL::Cell *cell = module->cells[cell_name];
154
155 if (!design->selected(module, cell) || handled_cells.count(cell) > 0)
156 continue;
157
158 if (celltypeMap.count(cell->type) == 0)
159 continue;
160
161 for (auto &tpl_name : celltypeMap.at(cell->type))
162 {
163 RTLIL::Module *tpl = map->modules[tpl_name];
164 std::pair<RTLIL::IdString, std::map<RTLIL::IdString, RTLIL::Const>> key(cell->type, cell->parameters);
165 std::string derived_name = cell->type;
166
167 if (techmap_cache.count(key) > 0) {
168 tpl = techmap_cache[key];
169 } else {
170 if (cell->parameters.size() != 0) {
171 derived_name = tpl->derive(map, cell->parameters);
172 tpl = map->modules[derived_name];
173 log_header("Continuing TECHMAP pass.\n");
174 }
175 techmap_cache[key] = tpl;
176 }
177
178 if (techmap_fail_check(tpl)) {
179 log("Not using module `%s' from techmap as it contains a TECHMAP_FAIL marker wire.\n", derived_name.c_str());
180 continue;
181 }
182
183 techmap_module_worker(design, module, cell, tpl);
184 did_something = true;
185 cell = NULL;
186 break;
187 }
188
189 handled_cells.insert(cell);
190 }
191
192 return did_something;
193 }
194
195 struct TechmapPass : public Pass {
196 TechmapPass() : Pass("techmap", "simple technology mapper") { }
197 virtual void help()
198 {
199 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
200 log("\n");
201 log(" techmap [-map filename] [selection]\n");
202 log("\n");
203 log("This pass implements a very simple technology mapper that replaces cells in\n");
204 log("the design with implementations given in form of a verilog or ilang source\n");
205 log("file.\n");
206 log("\n");
207 log(" -map filename\n");
208 log(" the library of cell implementations to be used.\n");
209 log(" without this parameter a builtin library is used that\n");
210 log(" transforms the internal RTL cells to the internal gate\n");
211 log(" library.\n");
212 log("\n");
213 log("When a module in the map file has the 'celltype' attribute set, it will match\n");
214 log("cells with a type that match the text value of this attribute.\n");
215 log("\n");
216 log("When a module in the map file contains a wire with the name 'TECHMAP_FAIL' (or\n");
217 log("one matching '*.TECHMAP_FAIL') then no substitution will be performed. The\n");
218 log("module in the map file are tried in alphabetical order.\n");
219 log("\n");
220 log("See 'help extract' for a pass that does the opposite thing.\n");
221 log("\n");
222 }
223 virtual void execute(std::vector<std::string> args, RTLIL::Design *design)
224 {
225 log_header("Executing TECHMAP pass (map to technology primitives).\n");
226 log_push();
227
228 std::string filename;
229
230 size_t argidx;
231 for (argidx = 1; argidx < args.size(); argidx++) {
232 if (args[argidx] == "-map" && argidx+1 < args.size()) {
233 filename = args[++argidx];
234 continue;
235 }
236 break;
237 }
238 extra_args(args, argidx, design);
239
240 FILE *f = filename.empty() ? fmemopen(stdcells_code, strlen(stdcells_code), "rt") : fopen(filename.c_str(), "rt");
241 if (f == NULL)
242 log_cmd_error("Can't open map file `%s'\n", filename.c_str());
243
244 RTLIL::Design *map = new RTLIL::Design;
245 Frontend::frontend_call(map, f, filename.empty() ? "<stdcells.v>" : filename,
246 (filename.size() > 3 && filename.substr(filename.size()-3) == ".il") ? "ilang" : "verilog");
247
248 fclose(f);
249
250 std::map<RTLIL::IdString, RTLIL::Module*> modules_new;
251 for (auto &it : map->modules) {
252 if (it.first.substr(0, 2) == "\\$")
253 it.second->name = it.first.substr(1);
254 modules_new[it.second->name] = it.second;
255 }
256 map->modules.swap(modules_new);
257
258 std::map<RTLIL::IdString, std::set<RTLIL::IdString>> celltypeMap;
259 for (auto &it : map->modules) {
260 if (it.second->attributes.count("\\celltype") && !it.second->attributes.at("\\celltype").str.empty()) {
261 celltypeMap[RTLIL::escape_id(it.second->attributes.at("\\celltype").str)].insert(it.first);
262 } else
263 celltypeMap[it.first].insert(it.first);
264 }
265
266 bool did_something = true;
267 std::set<RTLIL::Cell*> handled_cells;
268 while (did_something) {
269 did_something = false;
270 for (auto &mod_it : design->modules)
271 if (techmap_module(design, mod_it.second, map, handled_cells, celltypeMap))
272 did_something = true;
273 }
274
275 log("No more expansions possible.\n");
276 techmap_cache.clear();
277 techmap_fail_cache.clear();
278 delete map;
279 log_pop();
280 }
281 } TechmapPass;
282