Merge pull request #1622 from YosysHQ/clifford/onpassreg
[yosys.git] / kernel / register.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/yosys.h"
21 #include "kernel/satgen.h"
22
23 #include <string.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <errno.h>
27
28 #ifdef YOSYS_ENABLE_ZLIB
29 #include <zlib.h>
30
31 PRIVATE_NAMESPACE_BEGIN
32 #define GZ_BUFFER_SIZE 8192
33 void decompress_gzip(const std::string &filename, std::stringstream &out)
34 {
35 char buffer[GZ_BUFFER_SIZE];
36 int bytes_read;
37 gzFile gzf = gzopen(filename.c_str(), "rb");
38 while(!gzeof(gzf)) {
39 bytes_read = gzread(gzf, reinterpret_cast<void *>(buffer), GZ_BUFFER_SIZE);
40 out.write(buffer, bytes_read);
41 }
42 gzclose(gzf);
43 }
44
45 /*
46 An output stream that uses a stringbuf to buffer data internally,
47 using zlib to write gzip-compressed data every time the stream is flushed.
48 */
49 class gzip_ostream : public std::ostream {
50 public:
51 gzip_ostream() : std::ostream(nullptr)
52 {
53 rdbuf(&outbuf);
54 }
55 bool open(const std::string &filename)
56 {
57 return outbuf.open(filename);
58 }
59 private:
60 class gzip_streambuf : public std::stringbuf {
61 public:
62 gzip_streambuf() { };
63 bool open(const std::string &filename)
64 {
65 gzf = gzopen(filename.c_str(), "wb");
66 return gzf != nullptr;
67 }
68 virtual int sync() override
69 {
70 gzwrite(gzf, reinterpret_cast<const void *>(str().c_str()), unsigned(str().size()));
71 str("");
72 return 0;
73 }
74 virtual ~gzip_streambuf()
75 {
76 sync();
77 gzclose(gzf);
78 }
79 private:
80 gzFile gzf = nullptr;
81 } outbuf;
82 };
83 PRIVATE_NAMESPACE_END
84
85 #endif
86
87 YOSYS_NAMESPACE_BEGIN
88
89 #define MAX_REG_COUNT 1000
90
91 bool echo_mode = false;
92 Pass *first_queued_pass;
93 Pass *current_pass;
94
95 std::map<std::string, Frontend*> frontend_register;
96 std::map<std::string, Pass*> pass_register;
97 std::map<std::string, Backend*> backend_register;
98
99 std::vector<std::string> Frontend::next_args;
100
101 Pass::Pass(std::string name, std::string short_help) : pass_name(name), short_help(short_help)
102 {
103 next_queued_pass = first_queued_pass;
104 first_queued_pass = this;
105 call_counter = 0;
106 runtime_ns = 0;
107 }
108
109 void Pass::run_register()
110 {
111 log_assert(pass_register.count(pass_name) == 0);
112 pass_register[pass_name] = this;
113 }
114
115 void Pass::init_register()
116 {
117 vector<Pass*> added_passes;
118 while (first_queued_pass) {
119 added_passes.push_back(first_queued_pass);
120 first_queued_pass->run_register();
121 first_queued_pass = first_queued_pass->next_queued_pass;
122 }
123 for (auto added_pass : added_passes)
124 added_pass->on_register();
125 }
126
127 void Pass::done_register()
128 {
129 for (auto &it : pass_register)
130 it.second->on_shutdown();
131
132 frontend_register.clear();
133 pass_register.clear();
134 backend_register.clear();
135 log_assert(first_queued_pass == NULL);
136 }
137
138 void Pass::on_register()
139 {
140 }
141
142 void Pass::on_shutdown()
143 {
144 }
145
146 Pass::~Pass()
147 {
148 }
149
150 Pass::pre_post_exec_state_t Pass::pre_execute()
151 {
152 pre_post_exec_state_t state;
153 call_counter++;
154 state.begin_ns = PerformanceTimer::query();
155 state.parent_pass = current_pass;
156 current_pass = this;
157 clear_flags();
158 return state;
159 }
160
161 void Pass::post_execute(Pass::pre_post_exec_state_t state)
162 {
163 IdString::checkpoint();
164 log_suppressed();
165
166 int64_t time_ns = PerformanceTimer::query() - state.begin_ns;
167 runtime_ns += time_ns;
168 current_pass = state.parent_pass;
169 if (current_pass)
170 current_pass->runtime_ns -= time_ns;
171 }
172
173 void Pass::help()
174 {
175 log("\n");
176 log("No help message for command `%s'.\n", pass_name.c_str());
177 log("\n");
178 }
179
180 void Pass::clear_flags()
181 {
182 }
183
184 void Pass::cmd_log_args(const std::vector<std::string> &args)
185 {
186 if (args.size() <= 1)
187 return;
188 log("Full command line:");
189 for (size_t i = 0; i < args.size(); i++)
190 log(" %s", args[i].c_str());
191 log("\n");
192 }
193
194 void Pass::cmd_error(const std::vector<std::string> &args, size_t argidx, std::string msg)
195 {
196 std::string command_text;
197 int error_pos = 0;
198
199 for (size_t i = 0; i < args.size(); i++) {
200 if (i < argidx)
201 error_pos += args[i].size() + 1;
202 command_text = command_text + (command_text.empty() ? "" : " ") + args[i];
203 }
204
205 log("\nSyntax error in command `%s':\n", command_text.c_str());
206 help();
207
208 log_cmd_error("Command syntax error: %s\n> %s\n> %*s^\n",
209 msg.c_str(), command_text.c_str(), error_pos, "");
210 }
211
212 void Pass::extra_args(std::vector<std::string> args, size_t argidx, RTLIL::Design *design, bool select)
213 {
214 for (; argidx < args.size(); argidx++)
215 {
216 std::string arg = args[argidx];
217
218 if (arg.compare(0, 1, "-") == 0)
219 cmd_error(args, argidx, "Unknown option or option in arguments.");
220
221 if (!select)
222 cmd_error(args, argidx, "Extra argument.");
223
224 handle_extra_select_args(this, args, argidx, args.size(), design);
225 break;
226 }
227 // cmd_log_args(args);
228 }
229
230 void Pass::call(RTLIL::Design *design, std::string command)
231 {
232 std::vector<std::string> args;
233
234 std::string cmd_buf = command;
235 std::string tok = next_token(cmd_buf, " \t\r\n", true);
236
237 if (tok.empty())
238 return;
239
240 if (tok[0] == '!') {
241 cmd_buf = command.substr(command.find('!') + 1);
242 while (!cmd_buf.empty() && (cmd_buf.back() == ' ' || cmd_buf.back() == '\t' ||
243 cmd_buf.back() == '\r' || cmd_buf.back() == '\n'))
244 cmd_buf.resize(cmd_buf.size()-1);
245 log_header(design, "Shell command: %s\n", cmd_buf.c_str());
246 int retCode = run_command(cmd_buf);
247 if (retCode != 0)
248 log_cmd_error("Shell command returned error code %d.\n", retCode);
249 return;
250 }
251
252 while (!tok.empty()) {
253 if (tok[0] == '#') {
254 int stop;
255 for (stop = 0; stop < GetSize(cmd_buf); stop++)
256 if (cmd_buf[stop] == '\r' || cmd_buf[stop] == '\n')
257 break;
258 cmd_buf = cmd_buf.substr(stop);
259 } else
260 if (tok.back() == ';') {
261 int num_semikolon = 0;
262 while (!tok.empty() && tok.back() == ';')
263 tok.resize(tok.size()-1), num_semikolon++;
264 if (!tok.empty())
265 args.push_back(tok);
266 call(design, args);
267 args.clear();
268 if (num_semikolon == 2)
269 call(design, "clean");
270 if (num_semikolon == 3)
271 call(design, "clean -purge");
272 } else
273 args.push_back(tok);
274 bool found_nl = false;
275 for (auto c : cmd_buf) {
276 if (c == ' ' || c == '\t')
277 continue;
278 if (c == '\r' || c == '\n')
279 found_nl = true;
280 break;
281 }
282 if (found_nl) {
283 call(design, args);
284 args.clear();
285 }
286 tok = next_token(cmd_buf, " \t\r\n", true);
287 }
288
289 call(design, args);
290 }
291
292 void Pass::call(RTLIL::Design *design, std::vector<std::string> args)
293 {
294 if (args.size() == 0 || args[0][0] == '#' || args[0][0] == ':')
295 return;
296
297 if (echo_mode) {
298 log("%s", create_prompt(design, 0));
299 for (size_t i = 0; i < args.size(); i++)
300 log("%s%s", i ? " " : "", args[i].c_str());
301 log("\n");
302 }
303
304 if (pass_register.count(args[0]) == 0)
305 log_cmd_error("No such command: %s (type 'help' for a command overview)\n", args[0].c_str());
306
307 size_t orig_sel_stack_pos = design->selection_stack.size();
308 auto state = pass_register[args[0]]->pre_execute();
309 pass_register[args[0]]->execute(args, design);
310 pass_register[args[0]]->post_execute(state);
311 while (design->selection_stack.size() > orig_sel_stack_pos)
312 design->selection_stack.pop_back();
313 }
314
315 void Pass::call_on_selection(RTLIL::Design *design, const RTLIL::Selection &selection, std::string command)
316 {
317 std::string backup_selected_active_module = design->selected_active_module;
318 design->selected_active_module.clear();
319 design->selection_stack.push_back(selection);
320
321 Pass::call(design, command);
322
323 design->selection_stack.pop_back();
324 design->selected_active_module = backup_selected_active_module;
325 }
326
327 void Pass::call_on_selection(RTLIL::Design *design, const RTLIL::Selection &selection, std::vector<std::string> args)
328 {
329 std::string backup_selected_active_module = design->selected_active_module;
330 design->selected_active_module.clear();
331 design->selection_stack.push_back(selection);
332
333 Pass::call(design, args);
334
335 design->selection_stack.pop_back();
336 design->selected_active_module = backup_selected_active_module;
337 }
338
339 void Pass::call_on_module(RTLIL::Design *design, RTLIL::Module *module, std::string command)
340 {
341 std::string backup_selected_active_module = design->selected_active_module;
342 design->selected_active_module = module->name.str();
343 design->selection_stack.push_back(RTLIL::Selection(false));
344 design->selection_stack.back().select(module);
345
346 Pass::call(design, command);
347
348 design->selection_stack.pop_back();
349 design->selected_active_module = backup_selected_active_module;
350 }
351
352 void Pass::call_on_module(RTLIL::Design *design, RTLIL::Module *module, std::vector<std::string> args)
353 {
354 std::string backup_selected_active_module = design->selected_active_module;
355 design->selected_active_module = module->name.str();
356 design->selection_stack.push_back(RTLIL::Selection(false));
357 design->selection_stack.back().select(module);
358
359 Pass::call(design, args);
360
361 design->selection_stack.pop_back();
362 design->selected_active_module = backup_selected_active_module;
363 }
364
365 bool ScriptPass::check_label(std::string label, std::string info)
366 {
367 if (active_design == nullptr) {
368 log("\n");
369 if (info.empty())
370 log(" %s:\n", label.c_str());
371 else
372 log(" %s: %s\n", label.c_str(), info.c_str());
373 return true;
374 } else {
375 if (!active_run_from.empty() && active_run_from == active_run_to) {
376 block_active = (label == active_run_from);
377 } else {
378 if (label == active_run_from)
379 block_active = true;
380 if (label == active_run_to)
381 block_active = false;
382 }
383 return block_active;
384 }
385 }
386
387 void ScriptPass::run(std::string command, std::string info)
388 {
389 if (active_design == nullptr) {
390 if (info.empty())
391 log(" %s\n", command.c_str());
392 else
393 log(" %s %s\n", command.c_str(), info.c_str());
394 } else {
395 Pass::call(active_design, command);
396 active_design->check();
397 }
398 }
399
400 void ScriptPass::run_script(RTLIL::Design *design, std::string run_from, std::string run_to)
401 {
402 help_mode = false;
403 active_design = design;
404 block_active = run_from.empty();
405 active_run_from = run_from;
406 active_run_to = run_to;
407 script();
408 }
409
410 void ScriptPass::help_script()
411 {
412 clear_flags();
413 help_mode = true;
414 active_design = nullptr;
415 block_active = true;
416 active_run_from.clear();
417 active_run_to.clear();
418 script();
419 }
420
421 Frontend::Frontend(std::string name, std::string short_help) :
422 Pass(name.rfind("=", 0) == 0 ? name.substr(1) : "read_" + name, short_help),
423 frontend_name(name.rfind("=", 0) == 0 ? name.substr(1) : name)
424 {
425 }
426
427 void Frontend::run_register()
428 {
429 log_assert(pass_register.count(pass_name) == 0);
430 pass_register[pass_name] = this;
431
432 log_assert(frontend_register.count(frontend_name) == 0);
433 frontend_register[frontend_name] = this;
434 }
435
436 Frontend::~Frontend()
437 {
438 }
439
440 void Frontend::execute(std::vector<std::string> args, RTLIL::Design *design)
441 {
442 log_assert(next_args.empty());
443 do {
444 std::istream *f = NULL;
445 next_args.clear();
446 auto state = pre_execute();
447 execute(f, std::string(), args, design);
448 post_execute(state);
449 args = next_args;
450 delete f;
451 } while (!args.empty());
452 }
453
454 FILE *Frontend::current_script_file = NULL;
455 std::string Frontend::last_here_document;
456
457 void Frontend::extra_args(std::istream *&f, std::string &filename, std::vector<std::string> args, size_t argidx, bool bin_input)
458 {
459 bool called_with_fp = f != NULL;
460
461 next_args.clear();
462
463 if (argidx < args.size())
464 {
465 std::string arg = args[argidx];
466
467 if (arg.compare(0, 1, "-") == 0)
468 cmd_error(args, argidx, "Unknown option or option in arguments.");
469 if (f != NULL)
470 cmd_error(args, argidx, "Extra filename argument in direct file mode.");
471
472 filename = arg;
473 if (filename == "<<" && argidx+1 < args.size())
474 filename += args[++argidx];
475 if (filename.compare(0, 2, "<<") == 0) {
476 if (Frontend::current_script_file == NULL)
477 log_error("Unexpected here document '%s' outside of script!\n", filename.c_str());
478 if (filename.size() <= 2)
479 log_error("Missing EOT marker in here document!\n");
480 std::string eot_marker = filename.substr(2);
481 last_here_document.clear();
482 while (1) {
483 std::string buffer;
484 char block[4096];
485 while (1) {
486 if (fgets(block, 4096, Frontend::current_script_file) == NULL)
487 log_error("Unexpected end of file in here document '%s'!\n", filename.c_str());
488 buffer += block;
489 if (buffer.size() > 0 && (buffer[buffer.size() - 1] == '\n' || buffer[buffer.size() - 1] == '\r'))
490 break;
491 }
492 size_t indent = buffer.find_first_not_of(" \t\r\n");
493 if (indent != std::string::npos && buffer.compare(indent, eot_marker.size(), eot_marker) == 0)
494 break;
495 last_here_document += buffer;
496 }
497 f = new std::istringstream(last_here_document);
498 } else {
499 rewrite_filename(filename);
500 vector<string> filenames = glob_filename(filename);
501 filename = filenames.front();
502 if (GetSize(filenames) > 1) {
503 next_args.insert(next_args.end(), args.begin(), args.begin()+argidx);
504 next_args.insert(next_args.end(), filenames.begin()+1, filenames.end());
505 }
506 std::ifstream *ff = new std::ifstream;
507 ff->open(filename.c_str(), bin_input ? std::ifstream::binary : std::ifstream::in);
508 yosys_input_files.insert(filename);
509 if (ff->fail())
510 delete ff;
511 else
512 f = ff;
513 if (f != NULL) {
514 // Check for gzip magic
515 unsigned char magic[3];
516 int n = 0;
517 while (n < 3)
518 {
519 int c = ff->get();
520 if (c != EOF) {
521 magic[n] = (unsigned char) c;
522 }
523 n++;
524 }
525 if (n == 3 && magic[0] == 0x1f && magic[1] == 0x8b) {
526 #ifdef YOSYS_ENABLE_ZLIB
527 log("Found gzip magic in file `%s', decompressing using zlib.\n", filename.c_str());
528 if (magic[2] != 8)
529 log_cmd_error("gzip file `%s' uses unsupported compression type %02x\n",
530 filename.c_str(), unsigned(magic[2]));
531 delete ff;
532 std::stringstream *df = new std::stringstream();
533 decompress_gzip(filename, *df);
534 f = df;
535 #else
536 log_cmd_error("File `%s' is a gzip file, but Yosys is compiled without zlib.\n", filename.c_str());
537 #endif
538 } else {
539 ff->clear();
540 ff->seekg(0, std::ios::beg);
541 }
542 }
543 }
544 if (f == NULL)
545 log_cmd_error("Can't open input file `%s' for reading: %s\n", filename.c_str(), strerror(errno));
546
547 for (size_t i = argidx+1; i < args.size(); i++)
548 if (args[i].compare(0, 1, "-") == 0)
549 cmd_error(args, i, "Found option, expected arguments.");
550
551 if (argidx+1 < args.size()) {
552 if (next_args.empty())
553 next_args.insert(next_args.end(), args.begin(), args.begin()+argidx);
554 next_args.insert(next_args.end(), args.begin()+argidx+1, args.end());
555 args.erase(args.begin()+argidx+1, args.end());
556 }
557 }
558
559 if (f == NULL)
560 cmd_error(args, argidx, "No filename given.");
561
562 if (called_with_fp)
563 args.push_back(filename);
564 args[0] = pass_name;
565 // cmd_log_args(args);
566 }
567
568 void Frontend::frontend_call(RTLIL::Design *design, std::istream *f, std::string filename, std::string command)
569 {
570 std::vector<std::string> args;
571 char *s = strdup(command.c_str());
572 for (char *p = strtok(s, " \t\r\n"); p; p = strtok(NULL, " \t\r\n"))
573 args.push_back(p);
574 free(s);
575 frontend_call(design, f, filename, args);
576 }
577
578 void Frontend::frontend_call(RTLIL::Design *design, std::istream *f, std::string filename, std::vector<std::string> args)
579 {
580 if (args.size() == 0)
581 return;
582 if (frontend_register.count(args[0]) == 0)
583 log_cmd_error("No such frontend: %s\n", args[0].c_str());
584
585 if (f != NULL) {
586 auto state = frontend_register[args[0]]->pre_execute();
587 frontend_register[args[0]]->execute(f, filename, args, design);
588 frontend_register[args[0]]->post_execute(state);
589 } else if (filename == "-") {
590 std::istream *f_cin = &std::cin;
591 auto state = frontend_register[args[0]]->pre_execute();
592 frontend_register[args[0]]->execute(f_cin, "<stdin>", args, design);
593 frontend_register[args[0]]->post_execute(state);
594 } else {
595 if (!filename.empty())
596 args.push_back(filename);
597 frontend_register[args[0]]->execute(args, design);
598 }
599 }
600
601 Backend::Backend(std::string name, std::string short_help) :
602 Pass(name.rfind("=", 0) == 0 ? name.substr(1) : "write_" + name, short_help),
603 backend_name(name.rfind("=", 0) == 0 ? name.substr(1) : name)
604 {
605 }
606
607 void Backend::run_register()
608 {
609 log_assert(pass_register.count(pass_name) == 0);
610 pass_register[pass_name] = this;
611
612 log_assert(backend_register.count(backend_name) == 0);
613 backend_register[backend_name] = this;
614 }
615
616 Backend::~Backend()
617 {
618 }
619
620 void Backend::execute(std::vector<std::string> args, RTLIL::Design *design)
621 {
622 std::ostream *f = NULL;
623 auto state = pre_execute();
624 execute(f, std::string(), args, design);
625 post_execute(state);
626 if (f != &std::cout)
627 delete f;
628 }
629
630 void Backend::extra_args(std::ostream *&f, std::string &filename, std::vector<std::string> args, size_t argidx, bool bin_output)
631 {
632 bool called_with_fp = f != NULL;
633
634 for (; argidx < args.size(); argidx++)
635 {
636 std::string arg = args[argidx];
637
638 if (arg.compare(0, 1, "-") == 0 && arg != "-")
639 cmd_error(args, argidx, "Unknown option or option in arguments.");
640 if (f != NULL)
641 cmd_error(args, argidx, "Extra filename argument in direct file mode.");
642
643 if (arg == "-") {
644 filename = "<stdout>";
645 f = &std::cout;
646 continue;
647 }
648
649 filename = arg;
650 rewrite_filename(filename);
651 if (filename.size() > 3 && filename.compare(filename.size()-3, std::string::npos, ".gz") == 0) {
652 #ifdef YOSYS_ENABLE_ZLIB
653 gzip_ostream *gf = new gzip_ostream;
654 if (!gf->open(filename)) {
655 delete gf;
656 log_cmd_error("Can't open output file `%s' for writing: %s\n", filename.c_str(), strerror(errno));
657 }
658 yosys_output_files.insert(filename);
659 f = gf;
660 #else
661 log_cmd_error("Yosys is compiled without zlib support, unable to write gzip output.\n");
662 #endif
663 } else {
664 std::ofstream *ff = new std::ofstream;
665 ff->open(filename.c_str(), bin_output ? (std::ofstream::trunc | std::ofstream::binary) : std::ofstream::trunc);
666 yosys_output_files.insert(filename);
667 if (ff->fail()) {
668 delete ff;
669 log_cmd_error("Can't open output file `%s' for writing: %s\n", filename.c_str(), strerror(errno));
670 }
671 f = ff;
672 }
673 }
674
675 if (called_with_fp)
676 args.push_back(filename);
677 args[0] = pass_name;
678 // cmd_log_args(args);
679
680 if (f == NULL) {
681 filename = "<stdout>";
682 f = &std::cout;
683 }
684 }
685
686 void Backend::backend_call(RTLIL::Design *design, std::ostream *f, std::string filename, std::string command)
687 {
688 std::vector<std::string> args;
689 char *s = strdup(command.c_str());
690 for (char *p = strtok(s, " \t\r\n"); p; p = strtok(NULL, " \t\r\n"))
691 args.push_back(p);
692 free(s);
693 backend_call(design, f, filename, args);
694 }
695
696 void Backend::backend_call(RTLIL::Design *design, std::ostream *f, std::string filename, std::vector<std::string> args)
697 {
698 if (args.size() == 0)
699 return;
700 if (backend_register.count(args[0]) == 0)
701 log_cmd_error("No such backend: %s\n", args[0].c_str());
702
703 size_t orig_sel_stack_pos = design->selection_stack.size();
704
705 if (f != NULL) {
706 auto state = backend_register[args[0]]->pre_execute();
707 backend_register[args[0]]->execute(f, filename, args, design);
708 backend_register[args[0]]->post_execute(state);
709 } else if (filename == "-") {
710 std::ostream *f_cout = &std::cout;
711 auto state = backend_register[args[0]]->pre_execute();
712 backend_register[args[0]]->execute(f_cout, "<stdout>", args, design);
713 backend_register[args[0]]->post_execute(state);
714 } else {
715 if (!filename.empty())
716 args.push_back(filename);
717 backend_register[args[0]]->execute(args, design);
718 }
719
720 while (design->selection_stack.size() > orig_sel_stack_pos)
721 design->selection_stack.pop_back();
722 }
723
724 static struct CellHelpMessages {
725 dict<string, string> cell_help, cell_code;
726 CellHelpMessages() {
727 #include "techlibs/common/simlib_help.inc"
728 #include "techlibs/common/simcells_help.inc"
729 cell_help.sort();
730 cell_code.sort();
731 }
732 } cell_help_messages;
733
734 struct HelpPass : public Pass {
735 HelpPass() : Pass("help", "display help messages") { }
736 void help() YS_OVERRIDE
737 {
738 log("\n");
739 log(" help ................ list all commands\n");
740 log(" help <command> ...... print help message for given command\n");
741 log(" help -all ........... print complete command reference\n");
742 log("\n");
743 log(" help -cells .......... list all cell types\n");
744 log(" help <celltype> ..... print help message for given cell type\n");
745 log(" help <celltype>+ .... print verilog code for given cell type\n");
746 log("\n");
747 }
748 void escape_tex(std::string &tex)
749 {
750 for (size_t pos = 0; (pos = tex.find('_', pos)) != std::string::npos; pos += 2)
751 tex.replace(pos, 1, "\\_");
752 for (size_t pos = 0; (pos = tex.find('$', pos)) != std::string::npos; pos += 2)
753 tex.replace(pos, 1, "\\$");
754 }
755 void write_tex(FILE *f, std::string cmd, std::string title, std::string text)
756 {
757 size_t begin = text.find_first_not_of("\n"), end = text.find_last_not_of("\n");
758 if (begin != std::string::npos && end != std::string::npos && begin < end)
759 text = text.substr(begin, end-begin+1);
760 std::string cmd_unescaped = cmd;
761 escape_tex(cmd);
762 escape_tex(title);
763 fprintf(f, "\\section{%s -- %s}\n", cmd.c_str(), title.c_str());
764 fprintf(f, "\\label{cmd:%s}\n", cmd_unescaped.c_str());
765 fprintf(f, "\\begin{lstlisting}[numbers=left,frame=single]\n");
766 fprintf(f, "%s\n\\end{lstlisting}\n\n", text.c_str());
767 }
768 void escape_html(std::string &html)
769 {
770 size_t pos = 0;
771 while ((pos = html.find_first_of("<>&", pos)) != std::string::npos)
772 switch (html[pos]) {
773 case '<':
774 html.replace(pos, 1, "&lt;");
775 pos += 4;
776 break;
777 case '>':
778 html.replace(pos, 1, "&gt;");
779 pos += 4;
780 break;
781 case '&':
782 html.replace(pos, 1, "&amp;");
783 pos += 5;
784 break;
785 }
786 }
787 void write_html(FILE *idxf, std::string cmd, std::string title, std::string text)
788 {
789 FILE *f = fopen(stringf("cmd_%s.in", cmd.c_str()).c_str(), "wt");
790 fprintf(idxf, "<li><a href=\"cmd_%s.html\"> ", cmd.c_str());
791
792 escape_html(cmd);
793 escape_html(title);
794 escape_html(text);
795
796 fprintf(idxf, "%s</a> <span>%s</span></a>\n", cmd.c_str(), title.c_str());
797
798 fprintf(f, "@cmd_header %s@\n", cmd.c_str());
799 fprintf(f, "<h1>%s - %s</h1>\n", cmd.c_str(), title.c_str());
800 fprintf(f, "<pre>%s</pre>\n", text.c_str());
801 fprintf(f, "@footer@\n");
802
803 fclose(f);
804 }
805 void execute(std::vector<std::string> args, RTLIL::Design*) YS_OVERRIDE
806 {
807 if (args.size() == 1) {
808 log("\n");
809 for (auto &it : pass_register)
810 log(" %-20s %s\n", it.first.c_str(), it.second->short_help.c_str());
811 log("\n");
812 log("Type 'help <command>' for more information on a command.\n");
813 log("Type 'help -cells' for a list of all cell types.\n");
814 log("\n");
815 return;
816 }
817
818 if (args.size() == 2) {
819 if (args[1] == "-all") {
820 for (auto &it : pass_register) {
821 log("\n\n");
822 log("%s -- %s\n", it.first.c_str(), it.second->short_help.c_str());
823 for (size_t i = 0; i < it.first.size() + it.second->short_help.size() + 6; i++)
824 log("=");
825 log("\n");
826 it.second->help();
827 }
828 }
829 else if (args[1] == "-cells") {
830 log("\n");
831 for (auto &it : cell_help_messages.cell_help) {
832 string line = split_tokens(it.second, "\n").at(0);
833 string cell_name = next_token(line);
834 log(" %-15s %s\n", cell_name.c_str(), line.c_str());
835 }
836 log("\n");
837 log("Type 'help <cell_type>' for more information on a cell type.\n");
838 log("\n");
839 return;
840 }
841 // this option is undocumented as it is for internal use only
842 else if (args[1] == "-write-tex-command-reference-manual") {
843 FILE *f = fopen("command-reference-manual.tex", "wt");
844 fprintf(f, "%% Generated using the yosys 'help -write-tex-command-reference-manual' command.\n\n");
845 for (auto &it : pass_register) {
846 std::ostringstream buf;
847 log_streams.push_back(&buf);
848 it.second->help();
849 log_streams.pop_back();
850 write_tex(f, it.first, it.second->short_help, buf.str());
851 }
852 fclose(f);
853 }
854 // this option is undocumented as it is for internal use only
855 else if (args[1] == "-write-web-command-reference-manual") {
856 FILE *f = fopen("templates/cmd_index.in", "wt");
857 for (auto &it : pass_register) {
858 std::ostringstream buf;
859 log_streams.push_back(&buf);
860 it.second->help();
861 log_streams.pop_back();
862 write_html(f, it.first, it.second->short_help, buf.str());
863 }
864 fclose(f);
865 }
866 else if (pass_register.count(args[1])) {
867 pass_register.at(args[1])->help();
868 }
869 else if (cell_help_messages.cell_help.count(args[1])) {
870 log("%s", cell_help_messages.cell_help.at(args[1]).c_str());
871 log("Run 'help %s+' to display the Verilog model for this cell type.\n", args[1].c_str());
872 log("\n");
873 }
874 else if (cell_help_messages.cell_code.count(args[1])) {
875 log("\n");
876 log("%s", cell_help_messages.cell_code.at(args[1]).c_str());
877 }
878 else
879 log("No such command or cell type: %s\n", args[1].c_str());
880 return;
881 }
882
883 help();
884 }
885 } HelpPass;
886
887 struct EchoPass : public Pass {
888 EchoPass() : Pass("echo", "turning echoing back of commands on and off") { }
889 void help() YS_OVERRIDE
890 {
891 log("\n");
892 log(" echo on\n");
893 log("\n");
894 log("Print all commands to log before executing them.\n");
895 log("\n");
896 log("\n");
897 log(" echo off\n");
898 log("\n");
899 log("Do not print all commands to log before executing them. (default)\n");
900 log("\n");
901 }
902 void execute(std::vector<std::string> args, RTLIL::Design*) YS_OVERRIDE
903 {
904 if (args.size() > 2)
905 cmd_error(args, 2, "Unexpected argument.");
906
907 if (args.size() == 2) {
908 if (args[1] == "on")
909 echo_mode = true;
910 else if (args[1] == "off")
911 echo_mode = false;
912 else
913 cmd_error(args, 1, "Unexpected argument.");
914 }
915
916 log("echo %s\n", echo_mode ? "on" : "off");
917 }
918 } EchoPass;
919
920 SatSolver *yosys_satsolver_list;
921 SatSolver *yosys_satsolver;
922
923 struct MinisatSatSolver : public SatSolver {
924 MinisatSatSolver() : SatSolver("minisat") {
925 yosys_satsolver = this;
926 }
927 ezSAT *create() YS_OVERRIDE {
928 return new ezMiniSAT();
929 }
930 } MinisatSatSolver;
931
932 YOSYS_NAMESPACE_END