103db533f0f049001cd2670f1f408c0500472e29
[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 << "{\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 << " \"version\": \"0.0.0\",\n";
84 f << " \"modules\": [\n";
85
86 bool first{true};
87 for (auto mod : _use_selection ? design->selected_modules() : design->modules()) {
88 if (!first)
89 f << ",\n";
90 write_module(mod);
91
92 f << "\n";
93
94 first = false;
95 }
96
97 f << " ]\n";
98 f << "}\n";
99 }
100
101 void write_module(Module* mod)
102 {
103 log_assert(mod != nullptr);
104
105 coalesce_cells(mod);
106
107 f << " {\n";
108 f << stringf(" \"name\": %s,\n", get_string(RTLIL::unescape_id(mod->name)).c_str());
109 f << " \"cell_sorts\": [\n";
110
111 bool first_sort{true};
112 for (auto& sort : _cells) {
113 if (!first_sort)
114 f << ",\n";
115
116 write_cell_sort(sort);
117
118 first_sort = false;
119 }
120 f << "\n";
121
122 f << " ],\n";
123 f << " \"connections\": [\n";
124
125 f << " ]\n";
126 f << " }";
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 << " {\n";
135 f << stringf(" \"type\": %s,\n", sort.first.c_str());
136 f << " \"ports\": [\n";
137
138 bool first_port{true};
139 for (auto con : port_cell->connections()) {
140 if (!first_port)
141 f << ",\n";
142
143 f << " {\n";
144 f << stringf(" \"name\": %s,\n", get_string(RTLIL::unescape_id(con.first)).c_str());
145 f << " \"direction\": \"";
146 if (port_cell->input(con.first))
147 f << "i";
148 if (port_cell->input(con.first))
149 f << "o";
150 f << "\",\n";
151 if (con.second.size() == 1)
152 f << " \"range\": [0, 0]\n";
153 else
154 f << stringf(" \"range\": [%d, %d]\n", con.second.size(), 0);
155 f << " }";
156
157 first_port = false;
158 }
159 f << "\n";
160
161 f << " ],\n \"cells\": [\n";
162 bool first_cell{true};
163 for (auto& cell : sort.second) {
164 if (!first_cell)
165 f << ",\n";
166
167 write_cell(cell);
168
169 first_cell = false;
170 }
171 f << "\n";
172 f << " ]\n";
173 f << " }";
174 }
175
176 void write_param_val(const Const& v)
177 {
178 if ((v.flags & RTLIL::ConstFlags::CONST_FLAG_STRING) == RTLIL::ConstFlags::CONST_FLAG_STRING) {
179 const auto str = v.decode_string();
180
181
182 f << get_string(str);
183 } else if ((v.flags & RTLIL::ConstFlags::CONST_FLAG_SIGNED) == RTLIL::ConstFlags::CONST_FLAG_SIGNED) {
184 f << stringf("\"%dsd %d\"", v.size(), v.as_int());
185 } else if ((v.flags & RTLIL::ConstFlags::CONST_FLAG_REAL) == RTLIL::ConstFlags::CONST_FLAG_REAL) {
186
187 } else {
188 f << get_string(v.as_string());
189 }
190 }
191
192 void write_cell(Cell* cell)
193 {
194 log_assert(cell != nullptr);
195
196 f << " {\n";
197 f << stringf(" \"name\": %s,\n", get_string(RTLIL::unescape_id(cell->name)).c_str());
198 f << " \"attributes\": {\n";
199
200 bool first_attr{true};
201 for (auto& attr : cell->attributes) {
202 if (!first_attr)
203 f << stringf(",\n");
204 const auto attr_val = attr.second;
205 if (!attr_val.empty()) {
206 f << stringf(" %s: ", get_string(RTLIL::unescape_id(attr.first)).c_str());
207 write_param_val(attr_val);
208 } else {
209 f << stringf(" %s: true", get_string(RTLIL::unescape_id(attr.first)).c_str());
210 }
211
212 first_attr = false;
213 }
214 f << "\n";
215
216 f << " },\n";
217 f << " \"parameters\": {\n";
218
219 bool first_param{true};
220 for (auto& param : cell->parameters) {
221 if (!first_param)
222 f << stringf(",\n");
223 const auto param_val = param.second;
224 if (!param_val.empty()) {
225 f << stringf(" %s: ", get_string(RTLIL::unescape_id(param.first)).c_str());
226 write_param_val(param_val);
227 } else {
228 f << stringf(" %s: true", get_string(RTLIL::unescape_id(param.first)).c_str());
229 }
230
231 first_param = false;
232 }
233 f << "\n";
234
235 f << " },\n";
236 f << " }";
237 }
238 };
239
240 struct MetadataBackend : public Backend {
241 MetadataBackend() : Backend("metadata", "generate design metadata") { }
242 void help() override
243 {
244 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
245 log("\n");
246 log(" metadata [options] [selection]\n");
247 log("\n");
248 log("Write a JSON metadata for the current design\n");
249 log("\n");
250 log("\n");
251 }
252
253 void execute(std::ostream *&f, std::string filename, std::vector<std::string> args, RTLIL::Design *design) override
254 {
255 size_t argidx{1};
256 extra_args(f, filename, args, argidx);
257
258 log_header(design, "Executing metadata backend.\n");
259
260 MetadataWriter metadata_writier(*f, false);
261 metadata_writier.write_metadata(design);
262 }
263
264 } MetadataBackend;
265
266
267 struct MetadataPass : public Pass {
268 MetadataPass() : Pass("metadata", "write design metadata") { }
269
270 void help() override
271 {
272 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
273 log("\n");
274 log(" metadata [options] [selection]\n");
275 log("\n");
276 log("Write a JSON metadata for the current design\n");
277 log("\n");
278 log(" -o <filename>\n");
279 log(" write to the specified file.\n");
280 log("\n");
281 log("See 'help write_metadata' for a description of the JSON format used.\n");
282 log("\n");
283 }
284 void execute(std::vector<std::string> args, RTLIL::Design *design) override
285 {
286 std::string filename{};
287
288 size_t argidx;
289 for (argidx = 1; argidx < args.size(); argidx++)
290 {
291 if (args[argidx] == "-o" && argidx+1 < args.size()) {
292 filename = args[++argidx];
293 continue;
294 }
295 break;
296 }
297 extra_args(args, argidx, design);
298
299 std::ostream *f;
300 std::stringstream buf;
301
302 if (!filename.empty()) {
303 rewrite_filename(filename);
304 std::ofstream *ff = new std::ofstream;
305 ff->open(filename.c_str(), std::ofstream::trunc);
306 if (ff->fail()) {
307 delete ff;
308 log_error("Can't open file `%s' for writing: %s\n", filename.c_str(), strerror(errno));
309 }
310 f = ff;
311 } else {
312 f = &buf;
313 }
314
315
316 MetadataWriter metadata_writier(*f, false);
317 metadata_writier.write_metadata(design);
318
319 if (!filename.empty()) {
320 delete f;
321 } else {
322 log("%s", buf.str().c_str());
323 }
324 }
325
326 } MetadataPass;
327
328 PRIVATE_NAMESPACE_END