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