pass metadata: some more rough work on dumping the parameters and attributes
[yosys.git] / backends / metadata / metadata.cc
1 /*
2 * yosys -- Yosys Open SYnthesis Suite
3 *
4 * Copyright (C) 2012 Aki "lethalbit" Van Ness <aki@yosyshq.com> <aki@lethalbit.net>
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 // MYAU - Metadata for Yosys-Assisted Utilities
21
22 #include "kernel/rtlil.h"
23 #include "kernel/register.h"
24 #include "kernel/sigtools.h"
25 #include "kernel/celltypes.h"
26 #include "kernel/cellaigs.h"
27 #include "kernel/log.h"
28 #include <string>
29 #include <unordered_map>
30 #include <vector>
31
32 USING_YOSYS_NAMESPACE
33 PRIVATE_NAMESPACE_BEGIN
34
35
36 struct MetadataWriter
37 {
38 private:
39 std::ostream &f;
40 bool _use_selection;
41 std::unordered_map<std::string, std::vector<Cell*>> _cells{};
42
43 // XXX(aki): this was pulled from the json backend, needs to be pulled
44 // out possibly into some sort of utilities file, or integrated into rtlil.h
45 // directly
46 string get_string(string str)
47 {
48 string newstr = "\"";
49 for (char c : str) {
50 if (c == '\\')
51 newstr += c;
52 newstr += c;
53 }
54 return newstr + "\"";
55 }
56
57 // XXX(aki): I know this is far from ideal but i'm out of spoons and cant focus so
58 // it'll have to do for now,
59 void coalesce_cells(Module* mod)
60 {
61 for (auto cell : mod->cells()) {
62 const auto cell_type = get_string(RTLIL::unescape_id(cell->type));
63
64 if (_cells.find(cell_type) == _cells.end())
65 _cells.emplace(cell_type, std::vector<Cell*>());
66
67 _cells.at(cell_type).push_back(cell);
68 }
69 }
70
71 public:
72 MetadataWriter(std::ostream &f, bool use_selection) noexcept: f(f), _use_selection(use_selection) { }
73
74 void write_metadata(Design *design)
75 {
76 log_assert(design != nullptr);
77
78 design->sort();
79
80 f << stringf("{\n");
81 f << stringf(" \"generator\": %s,\n", get_string(yosys_version_str).c_str());
82 // XXX(aki): Replace this with a proper version info eventually:tm:
83 f << stringf(" \"version\": \"0.0.0\",\n");
84 f << stringf(" \"modules\": [\n");
85
86 bool first{true};
87 for (auto mod : _use_selection ? design->selected_modules() : design->modules()) {
88 if (!first)
89 f << stringf(",\n");
90 write_module(mod);
91
92 f << stringf("\n");
93
94 first = false;
95 }
96
97 f << stringf(" ]\n");
98 f << stringf("}\n");
99 }
100
101 void write_module(Module* mod)
102 {
103 log_assert(mod != nullptr);
104
105 coalesce_cells(mod);
106
107 f << stringf(" {\n");
108 f << stringf(" \"name\": %s,\n", get_string(RTLIL::unescape_id(mod->name)).c_str());
109 f << stringf(" \"cell_sorts\": [\n");
110
111 bool first_sort{true};
112 for (auto& sort : _cells) {
113 if (!first_sort)
114 f << stringf(",\n");
115
116 write_cell_sort(sort);
117
118 first_sort = false;
119 }
120 f << stringf("\n");
121
122 f << stringf(" ],\n");
123 f << stringf(" \"connections\": [\n");
124
125 f << stringf(" ]\n");
126 f << stringf(" }");
127 }
128
129
130 void write_cell_sort(std::pair<const std::string, std::vector<Cell*>>& sort)
131 {
132 const auto port_cell = sort.second.front();
133
134 f << stringf(" {\n");
135 f << stringf(" \"type\": %s,\n", sort.first.c_str());
136 f << stringf(" \"ports\": [\n");
137
138 bool first_port{true};
139 for (auto con : port_cell->connections()) {
140 if (!first_port)
141 f << stringf(",\n");
142
143 f << stringf(" {\n");
144 f << stringf(" \"name\": %s,\n", get_string(RTLIL::unescape_id(con.first)).c_str());
145 f << stringf(" \"direction\": \"");
146 if (port_cell->input(con.first))
147 f << stringf("i");
148 if (port_cell->input(con.first))
149 f << stringf("o");
150 f << stringf("\",\n");
151 if (con.second.size() == 1)
152 f << stringf(" \"range\": [0, 0]\n");
153 else
154 f << stringf(" \"range\": [%d, %d]\n", con.second.size(), 0);
155 f << stringf(" }");
156
157 first_port = false;
158 }
159 f << stringf("\n");
160
161 f << stringf(" ],\n \"cells\": [\n");
162 bool first_cell{true};
163 for (auto& cell : sort.second) {
164 if (!first_cell)
165 f << stringf(",\n");
166
167 write_cell(cell);
168
169 first_cell = false;
170 }
171 f << stringf("\n");
172 f << stringf(" ]\n");
173 f << stringf(" }");
174 }
175
176 void write_cell(Cell* cell)
177 {
178 log_assert(cell != nullptr);
179
180 f << stringf(" {\n");
181 f << stringf(" \"name\": %s,\n", get_string(RTLIL::unescape_id(cell->name)).c_str());
182 f << stringf(" \"attributes\": {\n");
183
184 bool first_attr{true};
185 for (auto& attr : cell->attributes) {
186 if (!first_attr)
187 f << stringf(",\n");
188 const auto attr_val = attr.second;
189 if (!attr_val.empty())
190 f << stringf(" %s: \"%s\"\n", get_string(RTLIL::unescape_id(attr.first)).c_str(), attr_val.decode_string().c_str());
191 else
192 f << stringf(" %s: true\n", get_string(RTLIL::unescape_id(attr.first)).c_str());
193
194 first_attr = false;
195 }
196
197 f << stringf(" },\n");
198 f << stringf(" \"parameters\": {\n");
199
200 bool first_param{true};
201 for (auto& param : cell->parameters) {
202 if (!first_param)
203 f << stringf(",\n");
204 const auto param_val = param.second;
205 if (!param_val.empty())
206 f << stringf(" %s: \"%s\"\n", get_string(RTLIL::unescape_id(param.first)).c_str(), param_val.decode_string().c_str());
207 else
208 f << stringf(" %s: true\n", get_string(RTLIL::unescape_id(param.first)).c_str());
209
210 first_param = false;
211 }
212
213 f << stringf(" },\n");
214 f << stringf(" }");
215 }
216 };
217
218 struct MetadataBackend : public Backend {
219 MetadataBackend() : Backend("metadata", "generate design metadata") { }
220 void help() override
221 {
222 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
223 log("\n");
224 log(" metadata [options] [selection]\n");
225 log("\n");
226 log("Write a JSON metadata for the current design\n");
227 log("\n");
228 log("\n");
229 }
230
231 void execute(std::ostream *&f, std::string filename, std::vector<std::string> args, RTLIL::Design *design) override
232 {
233 size_t argidx{1};
234 extra_args(f, filename, args, argidx);
235
236 log_header(design, "Executing metadata backend.\n");
237
238 MetadataWriter metadata_writier(*f, false);
239 metadata_writier.write_metadata(design);
240 }
241
242 } MetadataBackend;
243
244
245 struct MetadataPass : public Pass {
246 MetadataPass() : Pass("metadata", "write design metadata") { }
247
248 void help() override
249 {
250 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
251 log("\n");
252 log(" metadata [options] [selection]\n");
253 log("\n");
254 log("Write a JSON metadata for the current design\n");
255 log("\n");
256 log(" -o <filename>\n");
257 log(" write to the specified file.\n");
258 log("\n");
259 log("See 'help write_metadata' for a description of the JSON format used.\n");
260 log("\n");
261 }
262 void execute(std::vector<std::string> args, RTLIL::Design *design) override
263 {
264 std::string filename{};
265
266 size_t argidx;
267 for (argidx = 1; argidx < args.size(); argidx++)
268 {
269 if (args[argidx] == "-o" && argidx+1 < args.size()) {
270 filename = args[++argidx];
271 continue;
272 }
273 break;
274 }
275 extra_args(args, argidx, design);
276
277 std::ostream *f;
278 std::stringstream buf;
279
280 if (!filename.empty()) {
281 rewrite_filename(filename);
282 std::ofstream *ff = new std::ofstream;
283 ff->open(filename.c_str(), std::ofstream::trunc);
284 if (ff->fail()) {
285 delete ff;
286 log_error("Can't open file `%s' for writing: %s\n", filename.c_str(), strerror(errno));
287 }
288 f = ff;
289 } else {
290 f = &buf;
291 }
292
293
294 MetadataWriter metadata_writier(*f, false);
295 metadata_writier.write_metadata(design);
296
297 if (!filename.empty()) {
298 delete f;
299 } else {
300 log("%s", buf.str().c_str());
301 }
302 }
303
304 } MetadataPass;
305
306 PRIVATE_NAMESPACE_END