Merge pull request #1928 from YosysHQ/eddie/design_delete
[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 if (pass_register[args[0]]->experimental_flag)
308 log_experimental("%s", args[0].c_str());
309
310 size_t orig_sel_stack_pos = design->selection_stack.size();
311 auto state = pass_register[args[0]]->pre_execute();
312 pass_register[args[0]]->execute(args, design);
313 pass_register[args[0]]->post_execute(state);
314 while (design->selection_stack.size() > orig_sel_stack_pos)
315 design->selection_stack.pop_back();
316 }
317
318 void Pass::call_on_selection(RTLIL::Design *design, const RTLIL::Selection &selection, std::string command)
319 {
320 std::string backup_selected_active_module = design->selected_active_module;
321 design->selected_active_module.clear();
322 design->selection_stack.push_back(selection);
323
324 Pass::call(design, command);
325
326 design->selection_stack.pop_back();
327 design->selected_active_module = backup_selected_active_module;
328 }
329
330 void Pass::call_on_selection(RTLIL::Design *design, const RTLIL::Selection &selection, std::vector<std::string> args)
331 {
332 std::string backup_selected_active_module = design->selected_active_module;
333 design->selected_active_module.clear();
334 design->selection_stack.push_back(selection);
335
336 Pass::call(design, args);
337
338 design->selection_stack.pop_back();
339 design->selected_active_module = backup_selected_active_module;
340 }
341
342 void Pass::call_on_module(RTLIL::Design *design, RTLIL::Module *module, std::string command)
343 {
344 std::string backup_selected_active_module = design->selected_active_module;
345 design->selected_active_module = module->name.str();
346 design->selection_stack.push_back(RTLIL::Selection(false));
347 design->selection_stack.back().select(module);
348
349 Pass::call(design, command);
350
351 design->selection_stack.pop_back();
352 design->selected_active_module = backup_selected_active_module;
353 }
354
355 void Pass::call_on_module(RTLIL::Design *design, RTLIL::Module *module, std::vector<std::string> args)
356 {
357 std::string backup_selected_active_module = design->selected_active_module;
358 design->selected_active_module = module->name.str();
359 design->selection_stack.push_back(RTLIL::Selection(false));
360 design->selection_stack.back().select(module);
361
362 Pass::call(design, args);
363
364 design->selection_stack.pop_back();
365 design->selected_active_module = backup_selected_active_module;
366 }
367
368 bool ScriptPass::check_label(std::string label, std::string info)
369 {
370 if (active_design == nullptr) {
371 log("\n");
372 if (info.empty())
373 log(" %s:\n", label.c_str());
374 else
375 log(" %s: %s\n", label.c_str(), info.c_str());
376 return true;
377 } else {
378 if (!active_run_from.empty() && active_run_from == active_run_to) {
379 block_active = (label == active_run_from);
380 } else {
381 if (label == active_run_from)
382 block_active = true;
383 if (label == active_run_to)
384 block_active = false;
385 }
386 return block_active;
387 }
388 }
389
390 void ScriptPass::run(std::string command, std::string info)
391 {
392 if (active_design == nullptr) {
393 if (info.empty())
394 log(" %s\n", command.c_str());
395 else
396 log(" %s %s\n", command.c_str(), info.c_str());
397 } else {
398 Pass::call(active_design, command);
399 active_design->check();
400 }
401 }
402
403 void ScriptPass::run_nocheck(std::string command, std::string info)
404 {
405 if (active_design == nullptr) {
406 if (info.empty())
407 log(" %s\n", command.c_str());
408 else
409 log(" %s %s\n", command.c_str(), info.c_str());
410 } else {
411 Pass::call(active_design, command);
412 }
413 }
414
415 void ScriptPass::run_script(RTLIL::Design *design, std::string run_from, std::string run_to)
416 {
417 help_mode = false;
418 active_design = design;
419 block_active = run_from.empty();
420 active_run_from = run_from;
421 active_run_to = run_to;
422 script();
423 }
424
425 void ScriptPass::help_script()
426 {
427 clear_flags();
428 help_mode = true;
429 active_design = nullptr;
430 block_active = true;
431 active_run_from.clear();
432 active_run_to.clear();
433 script();
434 }
435
436 Frontend::Frontend(std::string name, std::string short_help) :
437 Pass(name.rfind("=", 0) == 0 ? name.substr(1) : "read_" + name, short_help),
438 frontend_name(name.rfind("=", 0) == 0 ? name.substr(1) : name)
439 {
440 }
441
442 void Frontend::run_register()
443 {
444 log_assert(pass_register.count(pass_name) == 0);
445 pass_register[pass_name] = this;
446
447 log_assert(frontend_register.count(frontend_name) == 0);
448 frontend_register[frontend_name] = this;
449 }
450
451 Frontend::~Frontend()
452 {
453 }
454
455 void Frontend::execute(std::vector<std::string> args, RTLIL::Design *design)
456 {
457 log_assert(next_args.empty());
458 do {
459 std::istream *f = NULL;
460 next_args.clear();
461 auto state = pre_execute();
462 execute(f, std::string(), args, design);
463 post_execute(state);
464 args = next_args;
465 delete f;
466 } while (!args.empty());
467 }
468
469 FILE *Frontend::current_script_file = NULL;
470 std::string Frontend::last_here_document;
471
472 void Frontend::extra_args(std::istream *&f, std::string &filename, std::vector<std::string> args, size_t argidx, bool bin_input)
473 {
474 bool called_with_fp = f != NULL;
475
476 next_args.clear();
477
478 if (argidx < args.size())
479 {
480 std::string arg = args[argidx];
481
482 if (arg.compare(0, 1, "-") == 0)
483 cmd_error(args, argidx, "Unknown option or option in arguments.");
484 if (f != NULL)
485 cmd_error(args, argidx, "Extra filename argument in direct file mode.");
486
487 filename = arg;
488 //Accommodate heredocs with EOT marker spaced out from "<<", e.g. "<< EOT" vs. "<<EOT"
489 if (filename == "<<" && argidx+1 < args.size())
490 filename += args[++argidx];
491 if (filename.compare(0, 2, "<<") == 0) {
492 if (filename.size() <= 2)
493 log_error("Missing EOT marker in here document!\n");
494 std::string eot_marker = filename.substr(2);
495 if (Frontend::current_script_file == nullptr)
496 filename = "<stdin>";
497 last_here_document.clear();
498 while (1) {
499 std::string buffer;
500 char block[4096];
501 while (1) {
502 if (fgets(block, 4096, Frontend::current_script_file == nullptr? stdin : Frontend::current_script_file) == nullptr)
503 log_error("Unexpected end of file in here document '%s'!\n", filename.c_str());
504 buffer += block;
505 if (buffer.size() > 0 && (buffer[buffer.size() - 1] == '\n' || buffer[buffer.size() - 1] == '\r'))
506 break;
507 }
508 size_t indent = buffer.find_first_not_of(" \t\r\n");
509 if (indent != std::string::npos && buffer.compare(indent, eot_marker.size(), eot_marker) == 0)
510 break;
511 last_here_document += buffer;
512 }
513 f = new std::istringstream(last_here_document);
514 } else {
515 rewrite_filename(filename);
516 vector<string> filenames = glob_filename(filename);
517 filename = filenames.front();
518 if (GetSize(filenames) > 1) {
519 next_args.insert(next_args.end(), args.begin(), args.begin()+argidx);
520 next_args.insert(next_args.end(), filenames.begin()+1, filenames.end());
521 }
522 std::ifstream *ff = new std::ifstream;
523 ff->open(filename.c_str(), bin_input ? std::ifstream::binary : std::ifstream::in);
524 yosys_input_files.insert(filename);
525 if (ff->fail())
526 delete ff;
527 else
528 f = ff;
529 if (f != NULL) {
530 // Check for gzip magic
531 unsigned char magic[3];
532 int n = 0;
533 while (n < 3)
534 {
535 int c = ff->get();
536 if (c != EOF) {
537 magic[n] = (unsigned char) c;
538 }
539 n++;
540 }
541 if (n == 3 && magic[0] == 0x1f && magic[1] == 0x8b) {
542 #ifdef YOSYS_ENABLE_ZLIB
543 log("Found gzip magic in file `%s', decompressing using zlib.\n", filename.c_str());
544 if (magic[2] != 8)
545 log_cmd_error("gzip file `%s' uses unsupported compression type %02x\n",
546 filename.c_str(), unsigned(magic[2]));
547 delete ff;
548 std::stringstream *df = new std::stringstream();
549 decompress_gzip(filename, *df);
550 f = df;
551 #else
552 log_cmd_error("File `%s' is a gzip file, but Yosys is compiled without zlib.\n", filename.c_str());
553 #endif
554 } else {
555 ff->clear();
556 ff->seekg(0, std::ios::beg);
557 }
558 }
559 }
560 if (f == NULL)
561 log_cmd_error("Can't open input file `%s' for reading: %s\n", filename.c_str(), strerror(errno));
562
563 for (size_t i = argidx+1; i < args.size(); i++)
564 if (args[i].compare(0, 1, "-") == 0)
565 cmd_error(args, i, "Found option, expected arguments.");
566
567 if (argidx+1 < args.size()) {
568 if (next_args.empty())
569 next_args.insert(next_args.end(), args.begin(), args.begin()+argidx);
570 next_args.insert(next_args.end(), args.begin()+argidx+1, args.end());
571 args.erase(args.begin()+argidx+1, args.end());
572 }
573 }
574
575 if (f == NULL)
576 cmd_error(args, argidx, "No filename given.");
577
578 if (called_with_fp)
579 args.push_back(filename);
580 args[0] = pass_name;
581 // cmd_log_args(args);
582 }
583
584 void Frontend::frontend_call(RTLIL::Design *design, std::istream *f, std::string filename, std::string command)
585 {
586 std::vector<std::string> args;
587 char *s = strdup(command.c_str());
588 for (char *p = strtok(s, " \t\r\n"); p; p = strtok(NULL, " \t\r\n"))
589 args.push_back(p);
590 free(s);
591 frontend_call(design, f, filename, args);
592 }
593
594 void Frontend::frontend_call(RTLIL::Design *design, std::istream *f, std::string filename, std::vector<std::string> args)
595 {
596 if (args.size() == 0)
597 return;
598 if (frontend_register.count(args[0]) == 0)
599 log_cmd_error("No such frontend: %s\n", args[0].c_str());
600
601 if (f != NULL) {
602 auto state = frontend_register[args[0]]->pre_execute();
603 frontend_register[args[0]]->execute(f, filename, args, design);
604 frontend_register[args[0]]->post_execute(state);
605 } else if (filename == "-") {
606 std::istream *f_cin = &std::cin;
607 auto state = frontend_register[args[0]]->pre_execute();
608 frontend_register[args[0]]->execute(f_cin, "<stdin>", args, design);
609 frontend_register[args[0]]->post_execute(state);
610 } else {
611 if (!filename.empty())
612 args.push_back(filename);
613 frontend_register[args[0]]->execute(args, design);
614 }
615 }
616
617 Backend::Backend(std::string name, std::string short_help) :
618 Pass(name.rfind("=", 0) == 0 ? name.substr(1) : "write_" + name, short_help),
619 backend_name(name.rfind("=", 0) == 0 ? name.substr(1) : name)
620 {
621 }
622
623 void Backend::run_register()
624 {
625 log_assert(pass_register.count(pass_name) == 0);
626 pass_register[pass_name] = this;
627
628 log_assert(backend_register.count(backend_name) == 0);
629 backend_register[backend_name] = this;
630 }
631
632 Backend::~Backend()
633 {
634 }
635
636 void Backend::execute(std::vector<std::string> args, RTLIL::Design *design)
637 {
638 std::ostream *f = NULL;
639 auto state = pre_execute();
640 execute(f, std::string(), args, design);
641 post_execute(state);
642 if (f != &std::cout)
643 delete f;
644 }
645
646 void Backend::extra_args(std::ostream *&f, std::string &filename, std::vector<std::string> args, size_t argidx, bool bin_output)
647 {
648 bool called_with_fp = f != NULL;
649
650 for (; argidx < args.size(); argidx++)
651 {
652 std::string arg = args[argidx];
653
654 if (arg.compare(0, 1, "-") == 0 && arg != "-")
655 cmd_error(args, argidx, "Unknown option or option in arguments.");
656 if (f != NULL)
657 cmd_error(args, argidx, "Extra filename argument in direct file mode.");
658
659 if (arg == "-") {
660 filename = "<stdout>";
661 f = &std::cout;
662 continue;
663 }
664
665 filename = arg;
666 rewrite_filename(filename);
667 if (filename.size() > 3 && filename.compare(filename.size()-3, std::string::npos, ".gz") == 0) {
668 #ifdef YOSYS_ENABLE_ZLIB
669 gzip_ostream *gf = new gzip_ostream;
670 if (!gf->open(filename)) {
671 delete gf;
672 log_cmd_error("Can't open output file `%s' for writing: %s\n", filename.c_str(), strerror(errno));
673 }
674 yosys_output_files.insert(filename);
675 f = gf;
676 #else
677 log_cmd_error("Yosys is compiled without zlib support, unable to write gzip output.\n");
678 #endif
679 } else {
680 std::ofstream *ff = new std::ofstream;
681 ff->open(filename.c_str(), bin_output ? (std::ofstream::trunc | std::ofstream::binary) : std::ofstream::trunc);
682 yosys_output_files.insert(filename);
683 if (ff->fail()) {
684 delete ff;
685 log_cmd_error("Can't open output file `%s' for writing: %s\n", filename.c_str(), strerror(errno));
686 }
687 f = ff;
688 }
689 }
690
691 if (called_with_fp)
692 args.push_back(filename);
693 args[0] = pass_name;
694 // cmd_log_args(args);
695
696 if (f == NULL) {
697 filename = "<stdout>";
698 f = &std::cout;
699 }
700 }
701
702 void Backend::backend_call(RTLIL::Design *design, std::ostream *f, std::string filename, std::string command)
703 {
704 std::vector<std::string> args;
705 char *s = strdup(command.c_str());
706 for (char *p = strtok(s, " \t\r\n"); p; p = strtok(NULL, " \t\r\n"))
707 args.push_back(p);
708 free(s);
709 backend_call(design, f, filename, args);
710 }
711
712 void Backend::backend_call(RTLIL::Design *design, std::ostream *f, std::string filename, std::vector<std::string> args)
713 {
714 if (args.size() == 0)
715 return;
716 if (backend_register.count(args[0]) == 0)
717 log_cmd_error("No such backend: %s\n", args[0].c_str());
718
719 size_t orig_sel_stack_pos = design->selection_stack.size();
720
721 if (f != NULL) {
722 auto state = backend_register[args[0]]->pre_execute();
723 backend_register[args[0]]->execute(f, filename, args, design);
724 backend_register[args[0]]->post_execute(state);
725 } else if (filename == "-") {
726 std::ostream *f_cout = &std::cout;
727 auto state = backend_register[args[0]]->pre_execute();
728 backend_register[args[0]]->execute(f_cout, "<stdout>", args, design);
729 backend_register[args[0]]->post_execute(state);
730 } else {
731 if (!filename.empty())
732 args.push_back(filename);
733 backend_register[args[0]]->execute(args, design);
734 }
735
736 while (design->selection_stack.size() > orig_sel_stack_pos)
737 design->selection_stack.pop_back();
738 }
739
740 static struct CellHelpMessages {
741 dict<string, string> cell_help, cell_code;
742 CellHelpMessages() {
743 #include "techlibs/common/simlib_help.inc"
744 #include "techlibs/common/simcells_help.inc"
745 cell_help.sort();
746 cell_code.sort();
747 }
748 } cell_help_messages;
749
750 struct HelpPass : public Pass {
751 HelpPass() : Pass("help", "display help messages") { }
752 void help() YS_OVERRIDE
753 {
754 log("\n");
755 log(" help ................ list all commands\n");
756 log(" help <command> ...... print help message for given command\n");
757 log(" help -all ........... print complete command reference\n");
758 log("\n");
759 log(" help -cells .......... list all cell types\n");
760 log(" help <celltype> ..... print help message for given cell type\n");
761 log(" help <celltype>+ .... print verilog code for given cell type\n");
762 log("\n");
763 }
764 void escape_tex(std::string &tex)
765 {
766 for (size_t pos = 0; (pos = tex.find('_', pos)) != std::string::npos; pos += 2)
767 tex.replace(pos, 1, "\\_");
768 for (size_t pos = 0; (pos = tex.find('$', pos)) != std::string::npos; pos += 2)
769 tex.replace(pos, 1, "\\$");
770 }
771 void write_tex(FILE *f, std::string cmd, std::string title, std::string text)
772 {
773 size_t begin = text.find_first_not_of("\n"), end = text.find_last_not_of("\n");
774 if (begin != std::string::npos && end != std::string::npos && begin < end)
775 text = text.substr(begin, end-begin+1);
776 std::string cmd_unescaped = cmd;
777 escape_tex(cmd);
778 escape_tex(title);
779 fprintf(f, "\\section{%s -- %s}\n", cmd.c_str(), title.c_str());
780 fprintf(f, "\\label{cmd:%s}\n", cmd_unescaped.c_str());
781 fprintf(f, "\\begin{lstlisting}[numbers=left,frame=single]\n");
782 fprintf(f, "%s\n\\end{lstlisting}\n\n", text.c_str());
783 }
784 void escape_html(std::string &html)
785 {
786 size_t pos = 0;
787 while ((pos = html.find_first_of("<>&", pos)) != std::string::npos)
788 switch (html[pos]) {
789 case '<':
790 html.replace(pos, 1, "&lt;");
791 pos += 4;
792 break;
793 case '>':
794 html.replace(pos, 1, "&gt;");
795 pos += 4;
796 break;
797 case '&':
798 html.replace(pos, 1, "&amp;");
799 pos += 5;
800 break;
801 }
802 }
803 void write_html(FILE *idxf, std::string cmd, std::string title, std::string text)
804 {
805 FILE *f = fopen(stringf("cmd_%s.in", cmd.c_str()).c_str(), "wt");
806 fprintf(idxf, "<li><a href=\"cmd_%s.html\"> ", cmd.c_str());
807
808 escape_html(cmd);
809 escape_html(title);
810 escape_html(text);
811
812 fprintf(idxf, "%s</a> <span>%s</span></a>\n", cmd.c_str(), title.c_str());
813
814 fprintf(f, "@cmd_header %s@\n", cmd.c_str());
815 fprintf(f, "<h1>%s - %s</h1>\n", cmd.c_str(), title.c_str());
816 fprintf(f, "<pre>%s</pre>\n", text.c_str());
817 fprintf(f, "@footer@\n");
818
819 fclose(f);
820 }
821 void execute(std::vector<std::string> args, RTLIL::Design*) YS_OVERRIDE
822 {
823 if (args.size() == 1) {
824 log("\n");
825 for (auto &it : pass_register)
826 log(" %-20s %s\n", it.first.c_str(), it.second->short_help.c_str());
827 log("\n");
828 log("Type 'help <command>' for more information on a command.\n");
829 log("Type 'help -cells' for a list of all cell types.\n");
830 log("\n");
831 return;
832 }
833
834 if (args.size() == 2) {
835 if (args[1] == "-all") {
836 for (auto &it : pass_register) {
837 log("\n\n");
838 log("%s -- %s\n", it.first.c_str(), it.second->short_help.c_str());
839 for (size_t i = 0; i < it.first.size() + it.second->short_help.size() + 6; i++)
840 log("=");
841 log("\n");
842 it.second->help();
843 if (it.second->experimental_flag) {
844 log("\n");
845 log("WARNING: THE '%s' COMMAND IS EXPERIMENTAL.\n", it.first.c_str());
846 log("\n");
847 }
848 }
849 }
850 else if (args[1] == "-cells") {
851 log("\n");
852 for (auto &it : cell_help_messages.cell_help) {
853 string line = split_tokens(it.second, "\n").at(0);
854 string cell_name = next_token(line);
855 log(" %-15s %s\n", cell_name.c_str(), line.c_str());
856 }
857 log("\n");
858 log("Type 'help <cell_type>' for more information on a cell type.\n");
859 log("\n");
860 return;
861 }
862 // this option is undocumented as it is for internal use only
863 else if (args[1] == "-write-tex-command-reference-manual") {
864 FILE *f = fopen("command-reference-manual.tex", "wt");
865 fprintf(f, "%% Generated using the yosys 'help -write-tex-command-reference-manual' command.\n\n");
866 for (auto &it : pass_register) {
867 std::ostringstream buf;
868 log_streams.push_back(&buf);
869 it.second->help();
870 if (it.second->experimental_flag) {
871 log("\n");
872 log("WARNING: THE '%s' COMMAND IS EXPERIMENTAL.\n", it.first.c_str());
873 log("\n");
874 }
875 log_streams.pop_back();
876 write_tex(f, it.first, it.second->short_help, buf.str());
877 }
878 fclose(f);
879 }
880 // this option is undocumented as it is for internal use only
881 else if (args[1] == "-write-web-command-reference-manual") {
882 FILE *f = fopen("templates/cmd_index.in", "wt");
883 for (auto &it : pass_register) {
884 std::ostringstream buf;
885 log_streams.push_back(&buf);
886 it.second->help();
887 if (it.second->experimental_flag) {
888 log("\n");
889 log("WARNING: THE '%s' COMMAND IS EXPERIMENTAL.\n", it.first.c_str());
890 log("\n");
891 }
892 log_streams.pop_back();
893 write_html(f, it.first, it.second->short_help, buf.str());
894 }
895 fclose(f);
896 }
897 else if (pass_register.count(args[1])) {
898 pass_register.at(args[1])->help();
899 if (pass_register.at(args[1])->experimental_flag) {
900 log("\n");
901 log("WARNING: THE '%s' COMMAND IS EXPERIMENTAL.\n", args[1].c_str());
902 log("\n");
903 }
904 }
905 else if (cell_help_messages.cell_help.count(args[1])) {
906 log("%s", cell_help_messages.cell_help.at(args[1]).c_str());
907 log("Run 'help %s+' to display the Verilog model for this cell type.\n", args[1].c_str());
908 log("\n");
909 }
910 else if (cell_help_messages.cell_code.count(args[1])) {
911 log("\n");
912 log("%s", cell_help_messages.cell_code.at(args[1]).c_str());
913 }
914 else
915 log("No such command or cell type: %s\n", args[1].c_str());
916 return;
917 }
918
919 help();
920 }
921 } HelpPass;
922
923 struct EchoPass : public Pass {
924 EchoPass() : Pass("echo", "turning echoing back of commands on and off") { }
925 void help() YS_OVERRIDE
926 {
927 log("\n");
928 log(" echo on\n");
929 log("\n");
930 log("Print all commands to log before executing them.\n");
931 log("\n");
932 log("\n");
933 log(" echo off\n");
934 log("\n");
935 log("Do not print all commands to log before executing them. (default)\n");
936 log("\n");
937 }
938 void execute(std::vector<std::string> args, RTLIL::Design*) YS_OVERRIDE
939 {
940 if (args.size() > 2)
941 cmd_error(args, 2, "Unexpected argument.");
942
943 if (args.size() == 2) {
944 if (args[1] == "on")
945 echo_mode = true;
946 else if (args[1] == "off")
947 echo_mode = false;
948 else
949 cmd_error(args, 1, "Unexpected argument.");
950 }
951
952 log("echo %s\n", echo_mode ? "on" : "off");
953 }
954 } EchoPass;
955
956 SatSolver *yosys_satsolver_list;
957 SatSolver *yosys_satsolver;
958
959 struct MinisatSatSolver : public SatSolver {
960 MinisatSatSolver() : SatSolver("minisat") {
961 yosys_satsolver = this;
962 }
963 ezSAT *create() YS_OVERRIDE {
964 return new ezMiniSAT();
965 }
966 } MinisatSatSolver;
967
968 YOSYS_NAMESPACE_END