Added Frontend "+/" filename syntax for files from proc_share_dir
[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, "Unkown 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 char *s = strdup(command.c_str()), *sstart = s, *saveptr;
150 s += strspn(s, " \t\r\n");
151 if (*s == 0 || *s == '#') {
152 free(sstart);
153 return;
154 }
155 if (*s == '!') {
156 for (s++; *s == ' ' || *s == '\t'; s++) { }
157 char *p = s + strlen(s) - 1;
158 while (p >= s && (*p == '\r' || *p == '\n'))
159 *(p--) = 0;
160 log_header("Shell command: %s\n", s);
161 int retCode = system(s);
162 if (retCode != 0)
163 log_cmd_error("Shell command returned error code %d.\n", retCode);
164 free(sstart);
165 return;
166 }
167 for (char *p = strtok_r(s, " \t\r\n", &saveptr); p; p = strtok_r(NULL, " \t\r\n", &saveptr)) {
168 std::string str = p;
169 int strsz = str.size();
170 if (str == "#")
171 break;
172 if (strsz > 0 && str[strsz-1] == ';') {
173 int num_semikolon = 0;
174 while (strsz > 0 && str[strsz-1] == ';')
175 strsz--, num_semikolon++;
176 if (strsz > 0)
177 args.push_back(str.substr(0, strsz));
178 call(design, args);
179 args.clear();
180 if (num_semikolon == 2)
181 call(design, "clean");
182 if (num_semikolon == 3)
183 call(design, "clean -purge");
184 } else
185 args.push_back(str);
186 }
187 free(sstart);
188 call(design, args);
189 }
190
191 void Pass::call(RTLIL::Design *design, std::vector<std::string> args)
192 {
193 if (args.size() == 0 || args[0][0] == '#')
194 return;
195
196 if (echo_mode) {
197 log("%s", create_prompt(design, 0));
198 for (size_t i = 0; i < args.size(); i++)
199 log("%s%s", i ? " " : "", args[i].c_str());
200 log("\n");
201 }
202
203 if (pass_register.count(args[0]) == 0)
204 log_cmd_error("No such command: %s (type 'help' for a command overview)\n", args[0].c_str());
205
206 size_t orig_sel_stack_pos = design->selection_stack.size();
207 auto state = pass_register[args[0]]->pre_execute();
208 pass_register[args[0]]->execute(args, design);
209 pass_register[args[0]]->post_execute(state);
210 while (design->selection_stack.size() > orig_sel_stack_pos)
211 design->selection_stack.pop_back();
212
213 design->check();
214 }
215
216 void Pass::call_on_selection(RTLIL::Design *design, const RTLIL::Selection &selection, std::string command)
217 {
218 std::string backup_selected_active_module = design->selected_active_module;
219 design->selected_active_module.clear();
220 design->selection_stack.push_back(selection);
221
222 Pass::call(design, command);
223
224 design->selection_stack.pop_back();
225 design->selected_active_module = backup_selected_active_module;
226 }
227
228 void Pass::call_on_selection(RTLIL::Design *design, const RTLIL::Selection &selection, std::vector<std::string> args)
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, args);
235
236 design->selection_stack.pop_back();
237 design->selected_active_module = backup_selected_active_module;
238 }
239
240 void Pass::call_on_module(RTLIL::Design *design, RTLIL::Module *module, std::string command)
241 {
242 std::string backup_selected_active_module = design->selected_active_module;
243 design->selected_active_module = module->name.str();
244 design->selection_stack.push_back(RTLIL::Selection(false));
245 design->selection_stack.back().select(module);
246
247 Pass::call(design, command);
248
249 design->selection_stack.pop_back();
250 design->selected_active_module = backup_selected_active_module;
251 }
252
253 void Pass::call_on_module(RTLIL::Design *design, RTLIL::Module *module, std::vector<std::string> args)
254 {
255 std::string backup_selected_active_module = design->selected_active_module;
256 design->selected_active_module = module->name.str();
257 design->selection_stack.push_back(RTLIL::Selection(false));
258 design->selection_stack.back().select(module);
259
260 Pass::call(design, args);
261
262 design->selection_stack.pop_back();
263 design->selected_active_module = backup_selected_active_module;
264 }
265
266 Frontend::Frontend(std::string name, std::string short_help) :
267 Pass(name.rfind("=", 0) == 0 ? name.substr(1) : "read_" + name, short_help),
268 frontend_name(name.rfind("=", 0) == 0 ? name.substr(1) : name)
269 {
270 }
271
272 void Frontend::run_register()
273 {
274 log_assert(pass_register.count(pass_name) == 0);
275 pass_register[pass_name] = this;
276
277 log_assert(frontend_register.count(frontend_name) == 0);
278 frontend_register[frontend_name] = this;
279 }
280
281 Frontend::~Frontend()
282 {
283 }
284
285 void Frontend::execute(std::vector<std::string> args, RTLIL::Design *design)
286 {
287 log_assert(next_args.empty());
288 do {
289 FILE *f = NULL;
290 next_args.clear();
291 auto state = pre_execute();
292 execute(f, std::string(), args, design);
293 post_execute(state);
294 args = next_args;
295 fclose(f);
296 } while (!args.empty());
297 }
298
299 FILE *Frontend::current_script_file = NULL;
300 std::string Frontend::last_here_document;
301
302 void Frontend::extra_args(FILE *&f, std::string &filename, std::vector<std::string> args, size_t argidx)
303 {
304 bool called_with_fp = f != NULL;
305
306 next_args.clear();
307 for (; argidx < args.size(); argidx++)
308 {
309 std::string arg = args[argidx];
310
311 if (arg.substr(0, 1) == "-")
312 cmd_error(args, argidx, "Unkown option or option in arguments.");
313 if (f != NULL)
314 cmd_error(args, argidx, "Extra filename argument in direct file mode.");
315
316 filename = arg;
317 if (filename == "<<" && argidx+1 < args.size())
318 filename += args[++argidx];
319 if (filename.substr(0, 2) == "<<") {
320 if (Frontend::current_script_file == NULL)
321 log_error("Unexpected here document '%s' outside of script!\n", filename.c_str());
322 if (filename.size() <= 2)
323 log_error("Missing EOT marker in here document!\n");
324 std::string eot_marker = filename.substr(2);
325 last_here_document.clear();
326 while (1) {
327 std::string buffer;
328 char block[4096];
329 while (1) {
330 if (fgets(block, 4096, Frontend::current_script_file) == NULL)
331 log_error("Unexpected end of file in here document '%s'!\n", filename.c_str());
332 buffer += block;
333 if (buffer.size() > 0 && (buffer[buffer.size() - 1] == '\n' || buffer[buffer.size() - 1] == '\r'))
334 break;
335 }
336 int indent = buffer.find_first_not_of(" \t\r\n");
337 if (buffer.substr(indent, eot_marker.size()) == eot_marker)
338 break;
339 last_here_document += buffer;
340 }
341 f = fmemopen((void*)last_here_document.c_str(), last_here_document.size(), "r");
342 } else {
343 if (filename.substr(0, 2) == "+/")
344 filename = proc_share_dirname() + filename.substr(1);
345 f = fopen(filename.c_str(), "r");
346 }
347 if (f == NULL)
348 log_cmd_error("Can't open input file `%s' for reading: %s\n", filename.c_str(), strerror(errno));
349
350 for (size_t i = argidx+1; i < args.size(); i++)
351 if (args[i].substr(0, 1) == "-")
352 cmd_error(args, i, "Found option, expected arguments.");
353
354 if (argidx+1 < args.size()) {
355 next_args.insert(next_args.begin(), args.begin(), args.begin()+argidx);
356 next_args.insert(next_args.begin()+argidx, args.begin()+argidx+1, args.end());
357 args.erase(args.begin()+argidx+1, args.end());
358 }
359 break;
360 }
361 if (f == NULL)
362 cmd_error(args, argidx, "No filename given.");
363
364 if (called_with_fp)
365 args.push_back(filename);
366 args[0] = pass_name;
367 // cmd_log_args(args);
368 }
369
370 void Frontend::frontend_call(RTLIL::Design *design, FILE *f, std::string filename, std::string command)
371 {
372 std::vector<std::string> args;
373 char *s = strdup(command.c_str());
374 for (char *p = strtok(s, " \t\r\n"); p; p = strtok(NULL, " \t\r\n"))
375 args.push_back(p);
376 free(s);
377 frontend_call(design, f, filename, args);
378 }
379
380 void Frontend::frontend_call(RTLIL::Design *design, FILE *f, std::string filename, std::vector<std::string> args)
381 {
382 if (args.size() == 0)
383 return;
384 if (frontend_register.count(args[0]) == 0)
385 log_cmd_error("No such frontend: %s\n", args[0].c_str());
386
387 if (f != NULL) {
388 auto state = frontend_register[args[0]]->pre_execute();
389 frontend_register[args[0]]->execute(f, filename, args, design);
390 frontend_register[args[0]]->post_execute(state);
391 } else if (filename == "-") {
392 FILE *f_stdin = stdin; // workaround for OpenBSD 'stdin' implementation
393 auto state = frontend_register[args[0]]->pre_execute();
394 frontend_register[args[0]]->execute(f_stdin, "<stdin>", args, design);
395 frontend_register[args[0]]->post_execute(state);
396 } else {
397 if (!filename.empty())
398 args.push_back(filename);
399 frontend_register[args[0]]->execute(args, design);
400 }
401
402 design->check();
403 }
404
405 Backend::Backend(std::string name, std::string short_help) :
406 Pass(name.rfind("=", 0) == 0 ? name.substr(1) : "write_" + name, short_help),
407 backend_name(name.rfind("=", 0) == 0 ? name.substr(1) : name)
408 {
409 }
410
411 void Backend::run_register()
412 {
413 log_assert(pass_register.count(pass_name) == 0);
414 pass_register[pass_name] = this;
415
416 log_assert(backend_register.count(backend_name) == 0);
417 backend_register[backend_name] = this;
418 }
419
420 Backend::~Backend()
421 {
422 }
423
424 void Backend::execute(std::vector<std::string> args, RTLIL::Design *design)
425 {
426 FILE *f = NULL;
427 auto state = pre_execute();
428 execute(f, std::string(), args, design);
429 post_execute(state);
430 if (f != stdout)
431 fclose(f);
432 }
433
434 void Backend::extra_args(FILE *&f, std::string &filename, std::vector<std::string> args, size_t argidx)
435 {
436 bool called_with_fp = f != NULL;
437
438 for (; argidx < args.size(); argidx++)
439 {
440 std::string arg = args[argidx];
441
442 if (arg.substr(0, 1) == "-" && arg != "-")
443 cmd_error(args, argidx, "Unkown option or option in arguments.");
444 if (f != NULL)
445 cmd_error(args, argidx, "Extra filename argument in direct file mode.");
446
447 if (arg == "-") {
448 filename = "<stdout>";
449 f = stdout;
450 continue;
451 }
452
453 filename = arg;
454 f = fopen(filename.c_str(), "w");
455 if (f == NULL)
456 log_cmd_error("Can't open output file `%s' for writing: %s\n", filename.c_str(), strerror(errno));
457 }
458
459 if (called_with_fp)
460 args.push_back(filename);
461 args[0] = pass_name;
462 // cmd_log_args(args);
463
464 if (f == NULL) {
465 filename = "<stdout>";
466 f = stdout;
467 }
468 }
469
470 void Backend::backend_call(RTLIL::Design *design, FILE *f, std::string filename, std::string command)
471 {
472 std::vector<std::string> args;
473 char *s = strdup(command.c_str());
474 for (char *p = strtok(s, " \t\r\n"); p; p = strtok(NULL, " \t\r\n"))
475 args.push_back(p);
476 free(s);
477 backend_call(design, f, filename, args);
478 }
479
480 void Backend::backend_call(RTLIL::Design *design, FILE *f, std::string filename, std::vector<std::string> args)
481 {
482 if (args.size() == 0)
483 return;
484 if (backend_register.count(args[0]) == 0)
485 log_cmd_error("No such backend: %s\n", args[0].c_str());
486
487 size_t orig_sel_stack_pos = design->selection_stack.size();
488
489 if (f != NULL) {
490 auto state = backend_register[args[0]]->pre_execute();
491 backend_register[args[0]]->execute(f, filename, args, design);
492 backend_register[args[0]]->post_execute(state);
493 } else if (filename == "-") {
494 FILE *f_stdout = stdout; // workaround for OpenBSD 'stdout' implementation
495 auto state = backend_register[args[0]]->pre_execute();
496 backend_register[args[0]]->execute(f_stdout, "<stdout>", args, design);
497 backend_register[args[0]]->post_execute(state);
498 } else {
499 if (!filename.empty())
500 args.push_back(filename);
501 backend_register[args[0]]->execute(args, design);
502 }
503
504 while (design->selection_stack.size() > orig_sel_stack_pos)
505 design->selection_stack.pop_back();
506
507 design->check();
508 }
509
510 struct HelpPass : public Pass {
511 HelpPass() : Pass("help", "display help messages") { }
512 virtual void help()
513 {
514 log("\n");
515 log(" help ............. list all commands\n");
516 log(" help <command> ... print help message for given command\n");
517 log(" help -all ........ print complete command reference\n");
518 log("\n");
519 }
520 void escape_tex(std::string &tex)
521 {
522 size_t pos = 0;
523 while ((pos = tex.find('_', pos)) != std::string::npos) {
524 tex.replace(pos, 1, "\\_");
525 pos += 2;
526 }
527 }
528 void write_tex(FILE *f, std::string cmd, std::string title, std::string text)
529 {
530 size_t begin = text.find_first_not_of("\n"), end = text.find_last_not_of("\n");
531 if (begin != std::string::npos && end != std::string::npos && begin < end)
532 text = text.substr(begin, end-begin+1);
533 std::string cmd_unescaped = cmd;
534 escape_tex(cmd);
535 escape_tex(title);
536 fprintf(f, "\\section{%s -- %s}\n", cmd.c_str(), title.c_str());
537 fprintf(f, "\\label{cmd:%s}\n", cmd_unescaped.c_str());
538 fprintf(f, "\\begin{lstlisting}[numbers=left,frame=single]\n");
539 fprintf(f, "%s\n\\end{lstlisting}\n\n", text.c_str());
540 }
541 void escape_html(std::string &html)
542 {
543 size_t pos = 0;
544 while ((pos = html.find_first_of("<>&", pos)) != std::string::npos)
545 switch (html[pos]) {
546 case '<':
547 html.replace(pos, 1, "&lt;");
548 pos += 4;
549 break;
550 case '>':
551 html.replace(pos, 1, "&gt;");
552 pos += 4;
553 break;
554 case '&':
555 html.replace(pos, 1, "&amp;");
556 pos += 5;
557 break;
558 }
559 }
560 void write_html(FILE *idxf, std::string cmd, std::string title, std::string text)
561 {
562 FILE *f = fopen(stringf("cmd_%s.in", cmd.c_str()).c_str(), "wt");
563 fprintf(idxf, "<li><a href=\"cmd_%s.html\"> ", cmd.c_str());
564
565 escape_html(cmd);
566 escape_html(title);
567 escape_html(text);
568
569 fprintf(idxf, "%s</a> <span>%s</span></a>\n", cmd.c_str(), title.c_str());
570
571 fprintf(f, "@cmd_header %s@\n", cmd.c_str());
572 fprintf(f, "<h1>%s - %s</h1>\n", cmd.c_str(), title.c_str());
573 fprintf(f, "<pre>%s</pre>\n", text.c_str());
574 fprintf(f, "@footer@\n");
575
576 fclose(f);
577 }
578 virtual void execute(std::vector<std::string> args, RTLIL::Design*)
579 {
580 if (args.size() == 1) {
581 log("\n");
582 for (auto &it : pass_register)
583 log(" %-20s %s\n", it.first.c_str(), it.second->short_help.c_str());
584 log("\n");
585 log("Type 'help <command>' for more information on a command.\n");
586 log("\n");
587 return;
588 }
589
590 if (args.size() == 2) {
591 if (args[1] == "-all") {
592 for (auto &it : pass_register) {
593 log("\n\n");
594 log("%s -- %s\n", it.first.c_str(), it.second->short_help.c_str());
595 for (size_t i = 0; i < it.first.size() + it.second->short_help.size() + 6; i++)
596 log("=");
597 log("\n");
598 it.second->help();
599 }
600 }
601 // this option is undocumented as it is for internal use only
602 else if (args[1] == "-write-tex-command-reference-manual") {
603 FILE *f = fopen("command-reference-manual.tex", "wt");
604 fprintf(f, "%% Generated using the yosys 'help -write-tex-command-reference-manual' command.\n\n");
605 for (auto &it : pass_register) {
606 size_t memsize;
607 char *memptr;
608 FILE *memf = open_memstream(&memptr, &memsize);
609 log_files.push_back(memf);
610 it.second->help();
611 log_files.pop_back();
612 fclose(memf);
613 write_tex(f, it.first, it.second->short_help, memptr);
614 free(memptr);
615 }
616 fclose(f);
617 }
618 // this option is undocumented as it is for internal use only
619 else if (args[1] == "-write-web-command-reference-manual") {
620 FILE *f = fopen("templates/cmd_index.in", "wt");
621 for (auto &it : pass_register) {
622 size_t memsize;
623 char *memptr;
624 FILE *memf = open_memstream(&memptr, &memsize);
625 log_files.push_back(memf);
626 it.second->help();
627 log_files.pop_back();
628 fclose(memf);
629 write_html(f, it.first, it.second->short_help, memptr);
630 free(memptr);
631 }
632 fclose(f);
633 }
634 else if (pass_register.count(args[1]) == 0)
635 log("No such command: %s\n", args[1].c_str());
636 else
637 pass_register.at(args[1])->help();
638 return;
639 }
640
641 help();
642 }
643 } HelpPass;
644
645 struct EchoPass : public Pass {
646 EchoPass() : Pass("echo", "turning echoing back of commands on and off") { }
647 virtual void help()
648 {
649 log("\n");
650 log(" echo on\n");
651 log("\n");
652 log("Print all commands to log before executing them.\n");
653 log("\n");
654 log("\n");
655 log(" echo off\n");
656 log("\n");
657 log("Do not print all commands to log before executing them. (default)\n");
658 log("\n");
659 }
660 virtual void execute(std::vector<std::string> args, RTLIL::Design*)
661 {
662 if (args.size() > 2)
663 cmd_error(args, 2, "Unexpected argument.");
664
665 if (args.size() == 2) {
666 if (args[1] == "on")
667 echo_mode = true;
668 else if (args[1] == "off")
669 echo_mode = false;
670 else
671 cmd_error(args, 1, "Unexpected argument.");
672 }
673
674 log("echo %s\n", echo_mode ? "on" : "off");
675 }
676 } EchoPass;
677
678 YOSYS_NAMESPACE_END
679