Add __STDC_FORMAT_MACROS before <inttypes.h> as per @mithro
[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()
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 ~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.substr(0, 1) == "-")
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)
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.substr(0, 1) == "-")
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.substr(0, 2) == "<<") {
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.substr(indent, eot_marker.size()) == eot_marker)
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());
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 = readsome(*ff, reinterpret_cast<char*>(magic), 3);
502 if (n == 3 && magic[0] == 0x1f && magic[1] == 0x8b) {
503 #ifdef YOSYS_ENABLE_ZLIB
504 log("Found gzip magic in file `%s', decompressing using zlib.\n", filename.c_str());
505 if (magic[2] != 8)
506 log_cmd_error("gzip file `%s' uses unsupported compression type %02x\n",
507 filename.c_str(), unsigned(magic[2]));
508 delete ff;
509 std::stringstream *df = new std::stringstream();
510 decompress_gzip(filename, *df);
511 f = df;
512 #else
513 log_cmd_error("File `%s' is a gzip file, but Yosys is compiled without zlib.\n", filename.c_str());
514 #endif
515 } else {
516 ff->clear();
517 ff->seekg(0, std::ios::beg);
518 }
519 }
520 }
521 if (f == NULL)
522 log_cmd_error("Can't open input file `%s' for reading: %s\n", filename.c_str(), strerror(errno));
523
524 for (size_t i = argidx+1; i < args.size(); i++)
525 if (args[i].substr(0, 1) == "-")
526 cmd_error(args, i, "Found option, expected arguments.");
527
528 if (argidx+1 < args.size()) {
529 if (next_args.empty())
530 next_args.insert(next_args.end(), args.begin(), args.begin()+argidx);
531 next_args.insert(next_args.end(), args.begin()+argidx+1, args.end());
532 args.erase(args.begin()+argidx+1, args.end());
533 }
534 }
535
536 if (f == NULL)
537 cmd_error(args, argidx, "No filename given.");
538
539 if (called_with_fp)
540 args.push_back(filename);
541 args[0] = pass_name;
542 // cmd_log_args(args);
543 }
544
545 void Frontend::frontend_call(RTLIL::Design *design, std::istream *f, std::string filename, std::string command)
546 {
547 std::vector<std::string> args;
548 char *s = strdup(command.c_str());
549 for (char *p = strtok(s, " \t\r\n"); p; p = strtok(NULL, " \t\r\n"))
550 args.push_back(p);
551 free(s);
552 frontend_call(design, f, filename, args);
553 }
554
555 void Frontend::frontend_call(RTLIL::Design *design, std::istream *f, std::string filename, std::vector<std::string> args)
556 {
557 if (args.size() == 0)
558 return;
559 if (frontend_register.count(args[0]) == 0)
560 log_cmd_error("No such frontend: %s\n", args[0].c_str());
561
562 if (f != NULL) {
563 auto state = frontend_register[args[0]]->pre_execute();
564 frontend_register[args[0]]->execute(f, filename, args, design);
565 frontend_register[args[0]]->post_execute(state);
566 } else if (filename == "-") {
567 std::istream *f_cin = &std::cin;
568 auto state = frontend_register[args[0]]->pre_execute();
569 frontend_register[args[0]]->execute(f_cin, "<stdin>", args, design);
570 frontend_register[args[0]]->post_execute(state);
571 } else {
572 if (!filename.empty())
573 args.push_back(filename);
574 frontend_register[args[0]]->execute(args, design);
575 }
576 }
577
578 Backend::Backend(std::string name, std::string short_help) :
579 Pass(name.rfind("=", 0) == 0 ? name.substr(1) : "write_" + name, short_help),
580 backend_name(name.rfind("=", 0) == 0 ? name.substr(1) : name)
581 {
582 }
583
584 void Backend::run_register()
585 {
586 log_assert(pass_register.count(pass_name) == 0);
587 pass_register[pass_name] = this;
588
589 log_assert(backend_register.count(backend_name) == 0);
590 backend_register[backend_name] = this;
591 }
592
593 Backend::~Backend()
594 {
595 }
596
597 void Backend::execute(std::vector<std::string> args, RTLIL::Design *design)
598 {
599 std::ostream *f = NULL;
600 auto state = pre_execute();
601 execute(f, std::string(), args, design);
602 post_execute(state);
603 if (f != &std::cout)
604 delete f;
605 }
606
607 void Backend::extra_args(std::ostream *&f, std::string &filename, std::vector<std::string> args, size_t argidx)
608 {
609 bool called_with_fp = f != NULL;
610
611 for (; argidx < args.size(); argidx++)
612 {
613 std::string arg = args[argidx];
614
615 if (arg.substr(0, 1) == "-" && arg != "-")
616 cmd_error(args, argidx, "Unknown option or option in arguments.");
617 if (f != NULL)
618 cmd_error(args, argidx, "Extra filename argument in direct file mode.");
619
620 if (arg == "-") {
621 filename = "<stdout>";
622 f = &std::cout;
623 continue;
624 }
625
626 filename = arg;
627 rewrite_filename(filename);
628 if (filename.size() > 3 && filename.substr(filename.size()-3) == ".gz") {
629 #ifdef YOSYS_ENABLE_ZLIB
630 gzip_ostream *gf = new gzip_ostream;
631 if (!gf->open(filename)) {
632 delete gf;
633 log_cmd_error("Can't open output file `%s' for writing: %s\n", filename.c_str(), strerror(errno));
634 }
635 yosys_output_files.insert(filename);
636 f = gf;
637 #else
638 log_cmd_error("Yosys is compiled without zlib support, unable to write gzip output.\n");
639 #endif
640 } else {
641 std::ofstream *ff = new std::ofstream;
642 ff->open(filename.c_str(), std::ofstream::trunc);
643 yosys_output_files.insert(filename);
644 if (ff->fail()) {
645 delete ff;
646 log_cmd_error("Can't open output file `%s' for writing: %s\n", filename.c_str(), strerror(errno));
647 }
648 f = ff;
649 }
650 }
651
652 if (called_with_fp)
653 args.push_back(filename);
654 args[0] = pass_name;
655 // cmd_log_args(args);
656
657 if (f == NULL) {
658 filename = "<stdout>";
659 f = &std::cout;
660 }
661 }
662
663 void Backend::backend_call(RTLIL::Design *design, std::ostream *f, std::string filename, std::string command)
664 {
665 std::vector<std::string> args;
666 char *s = strdup(command.c_str());
667 for (char *p = strtok(s, " \t\r\n"); p; p = strtok(NULL, " \t\r\n"))
668 args.push_back(p);
669 free(s);
670 backend_call(design, f, filename, args);
671 }
672
673 void Backend::backend_call(RTLIL::Design *design, std::ostream *f, std::string filename, std::vector<std::string> args)
674 {
675 if (args.size() == 0)
676 return;
677 if (backend_register.count(args[0]) == 0)
678 log_cmd_error("No such backend: %s\n", args[0].c_str());
679
680 size_t orig_sel_stack_pos = design->selection_stack.size();
681
682 if (f != NULL) {
683 auto state = backend_register[args[0]]->pre_execute();
684 backend_register[args[0]]->execute(f, filename, args, design);
685 backend_register[args[0]]->post_execute(state);
686 } else if (filename == "-") {
687 std::ostream *f_cout = &std::cout;
688 auto state = backend_register[args[0]]->pre_execute();
689 backend_register[args[0]]->execute(f_cout, "<stdout>", args, design);
690 backend_register[args[0]]->post_execute(state);
691 } else {
692 if (!filename.empty())
693 args.push_back(filename);
694 backend_register[args[0]]->execute(args, design);
695 }
696
697 while (design->selection_stack.size() > orig_sel_stack_pos)
698 design->selection_stack.pop_back();
699 }
700
701 static struct CellHelpMessages {
702 dict<string, string> cell_help, cell_code;
703 CellHelpMessages() {
704 #include "techlibs/common/simlib_help.inc"
705 #include "techlibs/common/simcells_help.inc"
706 cell_help.sort();
707 cell_code.sort();
708 }
709 } cell_help_messages;
710
711 struct HelpPass : public Pass {
712 HelpPass() : Pass("help", "display help messages") { }
713 void help() YS_OVERRIDE
714 {
715 log("\n");
716 log(" help ................ list all commands\n");
717 log(" help <command> ...... print help message for given command\n");
718 log(" help -all ........... print complete command reference\n");
719 log("\n");
720 log(" help -cells .......... list all cell types\n");
721 log(" help <celltype> ..... print help message for given cell type\n");
722 log(" help <celltype>+ .... print verilog code for given cell type\n");
723 log("\n");
724 }
725 void escape_tex(std::string &tex)
726 {
727 for (size_t pos = 0; (pos = tex.find('_', pos)) != std::string::npos; pos += 2)
728 tex.replace(pos, 1, "\\_");
729 for (size_t pos = 0; (pos = tex.find('$', pos)) != std::string::npos; pos += 2)
730 tex.replace(pos, 1, "\\$");
731 }
732 void write_tex(FILE *f, std::string cmd, std::string title, std::string text)
733 {
734 size_t begin = text.find_first_not_of("\n"), end = text.find_last_not_of("\n");
735 if (begin != std::string::npos && end != std::string::npos && begin < end)
736 text = text.substr(begin, end-begin+1);
737 std::string cmd_unescaped = cmd;
738 escape_tex(cmd);
739 escape_tex(title);
740 fprintf(f, "\\section{%s -- %s}\n", cmd.c_str(), title.c_str());
741 fprintf(f, "\\label{cmd:%s}\n", cmd_unescaped.c_str());
742 fprintf(f, "\\begin{lstlisting}[numbers=left,frame=single]\n");
743 fprintf(f, "%s\n\\end{lstlisting}\n\n", text.c_str());
744 }
745 void escape_html(std::string &html)
746 {
747 size_t pos = 0;
748 while ((pos = html.find_first_of("<>&", pos)) != std::string::npos)
749 switch (html[pos]) {
750 case '<':
751 html.replace(pos, 1, "&lt;");
752 pos += 4;
753 break;
754 case '>':
755 html.replace(pos, 1, "&gt;");
756 pos += 4;
757 break;
758 case '&':
759 html.replace(pos, 1, "&amp;");
760 pos += 5;
761 break;
762 }
763 }
764 void write_html(FILE *idxf, std::string cmd, std::string title, std::string text)
765 {
766 FILE *f = fopen(stringf("cmd_%s.in", cmd.c_str()).c_str(), "wt");
767 fprintf(idxf, "<li><a href=\"cmd_%s.html\"> ", cmd.c_str());
768
769 escape_html(cmd);
770 escape_html(title);
771 escape_html(text);
772
773 fprintf(idxf, "%s</a> <span>%s</span></a>\n", cmd.c_str(), title.c_str());
774
775 fprintf(f, "@cmd_header %s@\n", cmd.c_str());
776 fprintf(f, "<h1>%s - %s</h1>\n", cmd.c_str(), title.c_str());
777 fprintf(f, "<pre>%s</pre>\n", text.c_str());
778 fprintf(f, "@footer@\n");
779
780 fclose(f);
781 }
782 void execute(std::vector<std::string> args, RTLIL::Design*) YS_OVERRIDE
783 {
784 if (args.size() == 1) {
785 log("\n");
786 for (auto &it : pass_register)
787 log(" %-20s %s\n", it.first.c_str(), it.second->short_help.c_str());
788 log("\n");
789 log("Type 'help <command>' for more information on a command.\n");
790 log("Type 'help -cells' for a list of all cell types.\n");
791 log("\n");
792 return;
793 }
794
795 if (args.size() == 2) {
796 if (args[1] == "-all") {
797 for (auto &it : pass_register) {
798 log("\n\n");
799 log("%s -- %s\n", it.first.c_str(), it.second->short_help.c_str());
800 for (size_t i = 0; i < it.first.size() + it.second->short_help.size() + 6; i++)
801 log("=");
802 log("\n");
803 it.second->help();
804 }
805 }
806 else if (args[1] == "-cells") {
807 log("\n");
808 for (auto &it : cell_help_messages.cell_help) {
809 string line = split_tokens(it.second, "\n").at(0);
810 string cell_name = next_token(line);
811 log(" %-15s %s\n", cell_name.c_str(), line.c_str());
812 }
813 log("\n");
814 log("Type 'help <cell_type>' for more information on a cell type.\n");
815 log("\n");
816 return;
817 }
818 // this option is undocumented as it is for internal use only
819 else if (args[1] == "-write-tex-command-reference-manual") {
820 FILE *f = fopen("command-reference-manual.tex", "wt");
821 fprintf(f, "%% Generated using the yosys 'help -write-tex-command-reference-manual' command.\n\n");
822 for (auto &it : pass_register) {
823 std::ostringstream buf;
824 log_streams.push_back(&buf);
825 it.second->help();
826 log_streams.pop_back();
827 write_tex(f, it.first, it.second->short_help, buf.str());
828 }
829 fclose(f);
830 }
831 // this option is undocumented as it is for internal use only
832 else if (args[1] == "-write-web-command-reference-manual") {
833 FILE *f = fopen("templates/cmd_index.in", "wt");
834 for (auto &it : pass_register) {
835 std::ostringstream buf;
836 log_streams.push_back(&buf);
837 it.second->help();
838 log_streams.pop_back();
839 write_html(f, it.first, it.second->short_help, buf.str());
840 }
841 fclose(f);
842 }
843 else if (pass_register.count(args[1])) {
844 pass_register.at(args[1])->help();
845 }
846 else if (cell_help_messages.cell_help.count(args[1])) {
847 log("%s", cell_help_messages.cell_help.at(args[1]).c_str());
848 log("Run 'help %s+' to display the Verilog model for this cell type.\n", args[1].c_str());
849 log("\n");
850 }
851 else if (cell_help_messages.cell_code.count(args[1])) {
852 log("\n");
853 log("%s", cell_help_messages.cell_code.at(args[1]).c_str());
854 }
855 else
856 log("No such command or cell type: %s\n", args[1].c_str());
857 return;
858 }
859
860 help();
861 }
862 } HelpPass;
863
864 struct EchoPass : public Pass {
865 EchoPass() : Pass("echo", "turning echoing back of commands on and off") { }
866 void help() YS_OVERRIDE
867 {
868 log("\n");
869 log(" echo on\n");
870 log("\n");
871 log("Print all commands to log before executing them.\n");
872 log("\n");
873 log("\n");
874 log(" echo off\n");
875 log("\n");
876 log("Do not print all commands to log before executing them. (default)\n");
877 log("\n");
878 }
879 void execute(std::vector<std::string> args, RTLIL::Design*) YS_OVERRIDE
880 {
881 if (args.size() > 2)
882 cmd_error(args, 2, "Unexpected argument.");
883
884 if (args.size() == 2) {
885 if (args[1] == "on")
886 echo_mode = true;
887 else if (args[1] == "off")
888 echo_mode = false;
889 else
890 cmd_error(args, 1, "Unexpected argument.");
891 }
892
893 log("echo %s\n", echo_mode ? "on" : "off");
894 }
895 } EchoPass;
896
897 SatSolver *yosys_satsolver_list;
898 SatSolver *yosys_satsolver;
899
900 struct MinisatSatSolver : public SatSolver {
901 MinisatSatSolver() : SatSolver("minisat") {
902 yosys_satsolver = this;
903 }
904 ezSAT *create() YS_OVERRIDE {
905 return new ezMiniSAT();
906 }
907 } MinisatSatSolver;
908
909 YOSYS_NAMESPACE_END