Added newline support to Pass::call() parser
[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 <string.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <errno.h>
25
26 YOSYS_NAMESPACE_BEGIN
27
28 #define MAX_REG_COUNT 1000
29
30 bool echo_mode = false;
31 Pass *first_queued_pass;
32 Pass *current_pass;
33
34 std::map<std::string, Frontend*> frontend_register;
35 std::map<std::string, Pass*> pass_register;
36 std::map<std::string, Backend*> backend_register;
37
38 std::vector<std::string> Frontend::next_args;
39
40 Pass::Pass(std::string name, std::string short_help) : pass_name(name), short_help(short_help)
41 {
42 next_queued_pass = first_queued_pass;
43 first_queued_pass = this;
44 call_counter = 0;
45 runtime_ns = 0;
46 }
47
48 void Pass::run_register()
49 {
50 log_assert(pass_register.count(pass_name) == 0);
51 pass_register[pass_name] = this;
52 }
53
54 void Pass::init_register()
55 {
56 while (first_queued_pass) {
57 first_queued_pass->run_register();
58 first_queued_pass = first_queued_pass->next_queued_pass;
59 }
60 }
61
62 void Pass::done_register()
63 {
64 frontend_register.clear();
65 pass_register.clear();
66 backend_register.clear();
67 log_assert(first_queued_pass == NULL);
68 }
69
70 Pass::~Pass()
71 {
72 }
73
74 Pass::pre_post_exec_state_t Pass::pre_execute()
75 {
76 pre_post_exec_state_t state;
77 call_counter++;
78 state.begin_ns = PerformanceTimer::query();
79 state.parent_pass = current_pass;
80 current_pass = this;
81 return state;
82 }
83
84 void Pass::post_execute(Pass::pre_post_exec_state_t state)
85 {
86 int64_t time_ns = PerformanceTimer::query() - state.begin_ns;
87 runtime_ns += time_ns;
88 current_pass = state.parent_pass;
89 if (current_pass)
90 current_pass->runtime_ns -= time_ns;
91 }
92
93 void Pass::help()
94 {
95 log("\n");
96 log("No help message for command `%s'.\n", pass_name.c_str());
97 log("\n");
98 }
99
100 void Pass::cmd_log_args(const std::vector<std::string> &args)
101 {
102 if (args.size() <= 1)
103 return;
104 log("Full command line:");
105 for (size_t i = 0; i < args.size(); i++)
106 log(" %s", args[i].c_str());
107 log("\n");
108 }
109
110 void Pass::cmd_error(const std::vector<std::string> &args, size_t argidx, std::string msg)
111 {
112 std::string command_text;
113 int error_pos = 0;
114
115 for (size_t i = 0; i < args.size(); i++) {
116 if (i < argidx)
117 error_pos += args[i].size() + 1;
118 command_text = command_text + (command_text.empty() ? "" : " ") + args[i];
119 }
120
121 log("\nSyntax error in command `%s':\n", command_text.c_str());
122 help();
123
124 log_cmd_error("Command syntax error: %s\n> %s\n> %*s^\n",
125 msg.c_str(), command_text.c_str(), error_pos, "");
126 }
127
128 void Pass::extra_args(std::vector<std::string> args, size_t argidx, RTLIL::Design *design, bool select)
129 {
130 for (; argidx < args.size(); argidx++)
131 {
132 std::string arg = args[argidx];
133
134 if (arg.substr(0, 1) == "-")
135 cmd_error(args, argidx, "Unknown option or option in arguments.");
136
137 if (!select)
138 cmd_error(args, argidx, "Extra argument.");
139
140 handle_extra_select_args(this, args, argidx, args.size(), design);
141 break;
142 }
143 // cmd_log_args(args);
144 }
145
146 void Pass::call(RTLIL::Design *design, std::string command)
147 {
148 std::vector<std::string> args;
149
150 std::string cmd_buf = command;
151 std::string tok = next_token(cmd_buf, " \t\r\n");
152
153 if (tok.empty() || tok[0] == '#')
154 return;
155
156 if (tok[0] == '!') {
157 cmd_buf = command.substr(command.find('!') + 1);
158 while (!cmd_buf.empty() && (cmd_buf.back() == ' ' || cmd_buf.back() == '\t' ||
159 cmd_buf.back() == '\r' || cmd_buf.back() == '\n'))
160 cmd_buf.resize(cmd_buf.size()-1);
161 log_header("Shell command: %s\n", cmd_buf.c_str());
162 int retCode = run_command(cmd_buf);
163 if (retCode != 0)
164 log_cmd_error("Shell command returned error code %d.\n", retCode);
165 return;
166 }
167
168 while (!tok.empty()) {
169 if (tok == "#")
170 break;
171 if (tok.back() == ';') {
172 int num_semikolon = 0;
173 while (!tok.empty() && tok.back() == ';')
174 tok.resize(tok.size()-1), num_semikolon++;
175 if (!tok.empty())
176 args.push_back(tok);
177 call(design, args);
178 args.clear();
179 if (num_semikolon == 2)
180 call(design, "clean");
181 if (num_semikolon == 3)
182 call(design, "clean -purge");
183 } else
184 args.push_back(tok);
185 bool found_nl = false;
186 for (auto c : cmd_buf) {
187 if (c == ' ' || c == '\t')
188 continue;
189 if (c == '\r' || c == '\n')
190 found_nl = true;
191 break;
192 }
193 if (found_nl) {
194 call(design, args);
195 args.clear();
196 }
197 tok = next_token(cmd_buf, " \t\r\n");
198 }
199
200 call(design, args);
201 }
202
203 void Pass::call(RTLIL::Design *design, std::vector<std::string> args)
204 {
205 if (args.size() == 0 || args[0][0] == '#')
206 return;
207
208 if (echo_mode) {
209 log("%s", create_prompt(design, 0));
210 for (size_t i = 0; i < args.size(); i++)
211 log("%s%s", i ? " " : "", args[i].c_str());
212 log("\n");
213 }
214
215 if (pass_register.count(args[0]) == 0)
216 log_cmd_error("No such command: %s (type 'help' for a command overview)\n", args[0].c_str());
217
218 size_t orig_sel_stack_pos = design->selection_stack.size();
219 auto state = pass_register[args[0]]->pre_execute();
220 pass_register[args[0]]->execute(args, design);
221 pass_register[args[0]]->post_execute(state);
222 while (design->selection_stack.size() > orig_sel_stack_pos)
223 design->selection_stack.pop_back();
224
225 design->check();
226 }
227
228 void Pass::call_on_selection(RTLIL::Design *design, const RTLIL::Selection &selection, std::string command)
229 {
230 std::string backup_selected_active_module = design->selected_active_module;
231 design->selected_active_module.clear();
232 design->selection_stack.push_back(selection);
233
234 Pass::call(design, command);
235
236 design->selection_stack.pop_back();
237 design->selected_active_module = backup_selected_active_module;
238 }
239
240 void Pass::call_on_selection(RTLIL::Design *design, const RTLIL::Selection &selection, std::vector<std::string> args)
241 {
242 std::string backup_selected_active_module = design->selected_active_module;
243 design->selected_active_module.clear();
244 design->selection_stack.push_back(selection);
245
246 Pass::call(design, args);
247
248 design->selection_stack.pop_back();
249 design->selected_active_module = backup_selected_active_module;
250 }
251
252 void Pass::call_on_module(RTLIL::Design *design, RTLIL::Module *module, std::string command)
253 {
254 std::string backup_selected_active_module = design->selected_active_module;
255 design->selected_active_module = module->name.str();
256 design->selection_stack.push_back(RTLIL::Selection(false));
257 design->selection_stack.back().select(module);
258
259 Pass::call(design, command);
260
261 design->selection_stack.pop_back();
262 design->selected_active_module = backup_selected_active_module;
263 }
264
265 void Pass::call_on_module(RTLIL::Design *design, RTLIL::Module *module, std::vector<std::string> args)
266 {
267 std::string backup_selected_active_module = design->selected_active_module;
268 design->selected_active_module = module->name.str();
269 design->selection_stack.push_back(RTLIL::Selection(false));
270 design->selection_stack.back().select(module);
271
272 Pass::call(design, args);
273
274 design->selection_stack.pop_back();
275 design->selected_active_module = backup_selected_active_module;
276 }
277
278 Frontend::Frontend(std::string name, std::string short_help) :
279 Pass(name.rfind("=", 0) == 0 ? name.substr(1) : "read_" + name, short_help),
280 frontend_name(name.rfind("=", 0) == 0 ? name.substr(1) : name)
281 {
282 }
283
284 void Frontend::run_register()
285 {
286 log_assert(pass_register.count(pass_name) == 0);
287 pass_register[pass_name] = this;
288
289 log_assert(frontend_register.count(frontend_name) == 0);
290 frontend_register[frontend_name] = this;
291 }
292
293 Frontend::~Frontend()
294 {
295 }
296
297 void Frontend::execute(std::vector<std::string> args, RTLIL::Design *design)
298 {
299 log_assert(next_args.empty());
300 do {
301 std::istream *f = NULL;
302 next_args.clear();
303 auto state = pre_execute();
304 execute(f, std::string(), args, design);
305 post_execute(state);
306 args = next_args;
307 delete f;
308 } while (!args.empty());
309 }
310
311 FILE *Frontend::current_script_file = NULL;
312 std::string Frontend::last_here_document;
313
314 void Frontend::extra_args(std::istream *&f, std::string &filename, std::vector<std::string> args, size_t argidx)
315 {
316 bool called_with_fp = f != NULL;
317
318 next_args.clear();
319 for (; argidx < args.size(); argidx++)
320 {
321 std::string arg = args[argidx];
322
323 if (arg.substr(0, 1) == "-")
324 cmd_error(args, argidx, "Unknown option or option in arguments.");
325 if (f != NULL)
326 cmd_error(args, argidx, "Extra filename argument in direct file mode.");
327
328 filename = arg;
329 if (filename == "<<" && argidx+1 < args.size())
330 filename += args[++argidx];
331 if (filename.substr(0, 2) == "<<") {
332 if (Frontend::current_script_file == NULL)
333 log_error("Unexpected here document '%s' outside of script!\n", filename.c_str());
334 if (filename.size() <= 2)
335 log_error("Missing EOT marker in here document!\n");
336 std::string eot_marker = filename.substr(2);
337 last_here_document.clear();
338 while (1) {
339 std::string buffer;
340 char block[4096];
341 while (1) {
342 if (fgets(block, 4096, Frontend::current_script_file) == NULL)
343 log_error("Unexpected end of file in here document '%s'!\n", filename.c_str());
344 buffer += block;
345 if (buffer.size() > 0 && (buffer[buffer.size() - 1] == '\n' || buffer[buffer.size() - 1] == '\r'))
346 break;
347 }
348 size_t indent = buffer.find_first_not_of(" \t\r\n");
349 if (indent != std::string::npos && buffer.substr(indent, eot_marker.size()) == eot_marker)
350 break;
351 last_here_document += buffer;
352 }
353 f = new std::istringstream(last_here_document);
354 } else {
355 if (filename.substr(0, 2) == "+/")
356 filename = proc_share_dirname() + filename.substr(1);
357 std::ifstream *ff = new std::ifstream;
358 ff->open(filename.c_str());
359 if (ff->fail())
360 delete ff;
361 else
362 f = ff;
363 }
364 if (f == NULL)
365 log_cmd_error("Can't open input file `%s' for reading: %s\n", filename.c_str(), strerror(errno));
366
367 for (size_t i = argidx+1; i < args.size(); i++)
368 if (args[i].substr(0, 1) == "-")
369 cmd_error(args, i, "Found option, expected arguments.");
370
371 if (argidx+1 < args.size()) {
372 next_args.insert(next_args.begin(), args.begin(), args.begin()+argidx);
373 next_args.insert(next_args.begin()+argidx, args.begin()+argidx+1, args.end());
374 args.erase(args.begin()+argidx+1, args.end());
375 }
376 break;
377 }
378 if (f == NULL)
379 cmd_error(args, argidx, "No filename given.");
380
381 if (called_with_fp)
382 args.push_back(filename);
383 args[0] = pass_name;
384 // cmd_log_args(args);
385 }
386
387 void Frontend::frontend_call(RTLIL::Design *design, std::istream *f, std::string filename, std::string command)
388 {
389 std::vector<std::string> args;
390 char *s = strdup(command.c_str());
391 for (char *p = strtok(s, " \t\r\n"); p; p = strtok(NULL, " \t\r\n"))
392 args.push_back(p);
393 free(s);
394 frontend_call(design, f, filename, args);
395 }
396
397 void Frontend::frontend_call(RTLIL::Design *design, std::istream *f, std::string filename, std::vector<std::string> args)
398 {
399 if (args.size() == 0)
400 return;
401 if (frontend_register.count(args[0]) == 0)
402 log_cmd_error("No such frontend: %s\n", args[0].c_str());
403
404 if (f != NULL) {
405 auto state = frontend_register[args[0]]->pre_execute();
406 frontend_register[args[0]]->execute(f, filename, args, design);
407 frontend_register[args[0]]->post_execute(state);
408 } else if (filename == "-") {
409 std::istream *f_cin = &std::cin;
410 auto state = frontend_register[args[0]]->pre_execute();
411 frontend_register[args[0]]->execute(f_cin, "<stdin>", args, design);
412 frontend_register[args[0]]->post_execute(state);
413 } else {
414 if (!filename.empty())
415 args.push_back(filename);
416 frontend_register[args[0]]->execute(args, design);
417 }
418
419 design->check();
420 }
421
422 Backend::Backend(std::string name, std::string short_help) :
423 Pass(name.rfind("=", 0) == 0 ? name.substr(1) : "write_" + name, short_help),
424 backend_name(name.rfind("=", 0) == 0 ? name.substr(1) : name)
425 {
426 }
427
428 void Backend::run_register()
429 {
430 log_assert(pass_register.count(pass_name) == 0);
431 pass_register[pass_name] = this;
432
433 log_assert(backend_register.count(backend_name) == 0);
434 backend_register[backend_name] = this;
435 }
436
437 Backend::~Backend()
438 {
439 }
440
441 void Backend::execute(std::vector<std::string> args, RTLIL::Design *design)
442 {
443 std::ostream *f = NULL;
444 auto state = pre_execute();
445 execute(f, std::string(), args, design);
446 post_execute(state);
447 if (f != &std::cout)
448 delete f;
449 }
450
451 void Backend::extra_args(std::ostream *&f, std::string &filename, std::vector<std::string> args, size_t argidx)
452 {
453 bool called_with_fp = f != NULL;
454
455 for (; argidx < args.size(); argidx++)
456 {
457 std::string arg = args[argidx];
458
459 if (arg.substr(0, 1) == "-" && arg != "-")
460 cmd_error(args, argidx, "Unknown option or option in arguments.");
461 if (f != NULL)
462 cmd_error(args, argidx, "Extra filename argument in direct file mode.");
463
464 if (arg == "-") {
465 filename = "<stdout>";
466 f = &std::cout;
467 continue;
468 }
469
470 filename = arg;
471 std::ofstream *ff = new std::ofstream;
472 ff->open(filename.c_str(), std::ofstream::trunc);
473 if (ff->fail()) {
474 delete ff;
475 log_cmd_error("Can't open output file `%s' for writing: %s\n", filename.c_str(), strerror(errno));
476 }
477 f = ff;
478 }
479
480 if (called_with_fp)
481 args.push_back(filename);
482 args[0] = pass_name;
483 // cmd_log_args(args);
484
485 if (f == NULL) {
486 filename = "<stdout>";
487 f = &std::cout;
488 }
489 }
490
491 void Backend::backend_call(RTLIL::Design *design, std::ostream *f, std::string filename, std::string command)
492 {
493 std::vector<std::string> args;
494 char *s = strdup(command.c_str());
495 for (char *p = strtok(s, " \t\r\n"); p; p = strtok(NULL, " \t\r\n"))
496 args.push_back(p);
497 free(s);
498 backend_call(design, f, filename, args);
499 }
500
501 void Backend::backend_call(RTLIL::Design *design, std::ostream *f, std::string filename, std::vector<std::string> args)
502 {
503 if (args.size() == 0)
504 return;
505 if (backend_register.count(args[0]) == 0)
506 log_cmd_error("No such backend: %s\n", args[0].c_str());
507
508 size_t orig_sel_stack_pos = design->selection_stack.size();
509
510 if (f != NULL) {
511 auto state = backend_register[args[0]]->pre_execute();
512 backend_register[args[0]]->execute(f, filename, args, design);
513 backend_register[args[0]]->post_execute(state);
514 } else if (filename == "-") {
515 std::ostream *f_cout = &std::cout;
516 auto state = backend_register[args[0]]->pre_execute();
517 backend_register[args[0]]->execute(f_cout, "<stdout>", args, design);
518 backend_register[args[0]]->post_execute(state);
519 } else {
520 if (!filename.empty())
521 args.push_back(filename);
522 backend_register[args[0]]->execute(args, design);
523 }
524
525 while (design->selection_stack.size() > orig_sel_stack_pos)
526 design->selection_stack.pop_back();
527
528 design->check();
529 }
530
531 struct HelpPass : public Pass {
532 HelpPass() : Pass("help", "display help messages") { }
533 virtual void help()
534 {
535 log("\n");
536 log(" help ............. list all commands\n");
537 log(" help <command> ... print help message for given command\n");
538 log(" help -all ........ print complete command reference\n");
539 log("\n");
540 }
541 void escape_tex(std::string &tex)
542 {
543 size_t pos = 0;
544 while ((pos = tex.find('_', pos)) != std::string::npos) {
545 tex.replace(pos, 1, "\\_");
546 pos += 2;
547 }
548 }
549 void write_tex(FILE *f, std::string cmd, std::string title, std::string text)
550 {
551 size_t begin = text.find_first_not_of("\n"), end = text.find_last_not_of("\n");
552 if (begin != std::string::npos && end != std::string::npos && begin < end)
553 text = text.substr(begin, end-begin+1);
554 std::string cmd_unescaped = cmd;
555 escape_tex(cmd);
556 escape_tex(title);
557 fprintf(f, "\\section{%s -- %s}\n", cmd.c_str(), title.c_str());
558 fprintf(f, "\\label{cmd:%s}\n", cmd_unescaped.c_str());
559 fprintf(f, "\\begin{lstlisting}[numbers=left,frame=single]\n");
560 fprintf(f, "%s\n\\end{lstlisting}\n\n", text.c_str());
561 }
562 void escape_html(std::string &html)
563 {
564 size_t pos = 0;
565 while ((pos = html.find_first_of("<>&", pos)) != std::string::npos)
566 switch (html[pos]) {
567 case '<':
568 html.replace(pos, 1, "&lt;");
569 pos += 4;
570 break;
571 case '>':
572 html.replace(pos, 1, "&gt;");
573 pos += 4;
574 break;
575 case '&':
576 html.replace(pos, 1, "&amp;");
577 pos += 5;
578 break;
579 }
580 }
581 void write_html(FILE *idxf, std::string cmd, std::string title, std::string text)
582 {
583 FILE *f = fopen(stringf("cmd_%s.in", cmd.c_str()).c_str(), "wt");
584 fprintf(idxf, "<li><a href=\"cmd_%s.html\"> ", cmd.c_str());
585
586 escape_html(cmd);
587 escape_html(title);
588 escape_html(text);
589
590 fprintf(idxf, "%s</a> <span>%s</span></a>\n", cmd.c_str(), title.c_str());
591
592 fprintf(f, "@cmd_header %s@\n", cmd.c_str());
593 fprintf(f, "<h1>%s - %s</h1>\n", cmd.c_str(), title.c_str());
594 fprintf(f, "<pre>%s</pre>\n", text.c_str());
595 fprintf(f, "@footer@\n");
596
597 fclose(f);
598 }
599 virtual void execute(std::vector<std::string> args, RTLIL::Design*)
600 {
601 if (args.size() == 1) {
602 log("\n");
603 for (auto &it : pass_register)
604 log(" %-20s %s\n", it.first.c_str(), it.second->short_help.c_str());
605 log("\n");
606 log("Type 'help <command>' for more information on a command.\n");
607 log("\n");
608 return;
609 }
610
611 if (args.size() == 2) {
612 if (args[1] == "-all") {
613 for (auto &it : pass_register) {
614 log("\n\n");
615 log("%s -- %s\n", it.first.c_str(), it.second->short_help.c_str());
616 for (size_t i = 0; i < it.first.size() + it.second->short_help.size() + 6; i++)
617 log("=");
618 log("\n");
619 it.second->help();
620 }
621 }
622 // this option is undocumented as it is for internal use only
623 else if (args[1] == "-write-tex-command-reference-manual") {
624 FILE *f = fopen("command-reference-manual.tex", "wt");
625 fprintf(f, "%% Generated using the yosys 'help -write-tex-command-reference-manual' command.\n\n");
626 for (auto &it : pass_register) {
627 std::ostringstream buf;
628 log_streams.push_back(&buf);
629 it.second->help();
630 log_streams.pop_back();
631 write_tex(f, it.first, it.second->short_help, buf.str());
632 }
633 fclose(f);
634 }
635 // this option is undocumented as it is for internal use only
636 else if (args[1] == "-write-web-command-reference-manual") {
637 FILE *f = fopen("templates/cmd_index.in", "wt");
638 for (auto &it : pass_register) {
639 std::ostringstream buf;
640 log_streams.push_back(&buf);
641 it.second->help();
642 log_streams.pop_back();
643 write_html(f, it.first, it.second->short_help, buf.str());
644 }
645 fclose(f);
646 }
647 else if (pass_register.count(args[1]) == 0)
648 log("No such command: %s\n", args[1].c_str());
649 else
650 pass_register.at(args[1])->help();
651 return;
652 }
653
654 help();
655 }
656 } HelpPass;
657
658 struct EchoPass : public Pass {
659 EchoPass() : Pass("echo", "turning echoing back of commands on and off") { }
660 virtual void help()
661 {
662 log("\n");
663 log(" echo on\n");
664 log("\n");
665 log("Print all commands to log before executing them.\n");
666 log("\n");
667 log("\n");
668 log(" echo off\n");
669 log("\n");
670 log("Do not print all commands to log before executing them. (default)\n");
671 log("\n");
672 }
673 virtual void execute(std::vector<std::string> args, RTLIL::Design*)
674 {
675 if (args.size() > 2)
676 cmd_error(args, 2, "Unexpected argument.");
677
678 if (args.size() == 2) {
679 if (args[1] == "on")
680 echo_mode = true;
681 else if (args[1] == "off")
682 echo_mode = false;
683 else
684 cmd_error(args, 1, "Unexpected argument.");
685 }
686
687 log("echo %s\n", echo_mode ? "on" : "off");
688 }
689 } EchoPass;
690
691 YOSYS_NAMESPACE_END
692