2 * yosys -- Yosys Open SYnthesis Suite
4 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
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.
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.
20 #include "kernel/yosys.h"
21 #include "kernel/satgen.h"
30 #define MAX_REG_COUNT 1000
32 bool echo_mode
= false;
33 Pass
*first_queued_pass
;
36 std::map
<std::string
, Frontend
*> frontend_register
;
37 std::map
<std::string
, Pass
*> pass_register
;
38 std::map
<std::string
, Backend
*> backend_register
;
40 std::vector
<std::string
> Frontend::next_args
;
42 Pass::Pass(std::string name
, std::string short_help
) : pass_name(name
), short_help(short_help
)
44 next_queued_pass
= first_queued_pass
;
45 first_queued_pass
= this;
50 void Pass::run_register()
52 log_assert(pass_register
.count(pass_name
) == 0);
53 pass_register
[pass_name
] = this;
56 void Pass::init_register()
58 while (first_queued_pass
) {
59 first_queued_pass
->run_register();
60 first_queued_pass
= first_queued_pass
->next_queued_pass
;
64 void Pass::done_register()
66 frontend_register
.clear();
67 pass_register
.clear();
68 backend_register
.clear();
69 log_assert(first_queued_pass
== NULL
);
76 Pass::pre_post_exec_state_t
Pass::pre_execute()
78 pre_post_exec_state_t state
;
80 state
.begin_ns
= PerformanceTimer::query();
81 state
.parent_pass
= current_pass
;
87 void Pass::post_execute(Pass::pre_post_exec_state_t state
)
89 IdString::checkpoint();
92 int64_t time_ns
= PerformanceTimer::query() - state
.begin_ns
;
93 runtime_ns
+= time_ns
;
94 current_pass
= state
.parent_pass
;
96 current_pass
->runtime_ns
-= time_ns
;
102 log("No help message for command `%s'.\n", pass_name
.c_str());
106 void Pass::clear_flags()
110 void Pass::cmd_log_args(const std::vector
<std::string
> &args
)
112 if (args
.size() <= 1)
114 log("Full command line:");
115 for (size_t i
= 0; i
< args
.size(); i
++)
116 log(" %s", args
[i
].c_str());
120 void Pass::cmd_error(const std::vector
<std::string
> &args
, size_t argidx
, std::string msg
)
122 std::string command_text
;
125 for (size_t i
= 0; i
< args
.size(); i
++) {
127 error_pos
+= args
[i
].size() + 1;
128 command_text
= command_text
+ (command_text
.empty() ? "" : " ") + args
[i
];
131 log("\nSyntax error in command `%s':\n", command_text
.c_str());
134 log_cmd_error("Command syntax error: %s\n> %s\n> %*s^\n",
135 msg
.c_str(), command_text
.c_str(), error_pos
, "");
138 void Pass::extra_args(std::vector
<std::string
> args
, size_t argidx
, RTLIL::Design
*design
, bool select
)
140 for (; argidx
< args
.size(); argidx
++)
142 std::string arg
= args
[argidx
];
144 if (arg
.substr(0, 1) == "-")
145 cmd_error(args
, argidx
, "Unknown option or option in arguments.");
148 cmd_error(args
, argidx
, "Extra argument.");
150 handle_extra_select_args(this, args
, argidx
, args
.size(), design
);
153 // cmd_log_args(args);
156 void Pass::call(RTLIL::Design
*design
, std::string command
)
158 std::vector
<std::string
> args
;
160 std::string cmd_buf
= command
;
161 std::string tok
= next_token(cmd_buf
, " \t\r\n", true);
167 cmd_buf
= command
.substr(command
.find('!') + 1);
168 while (!cmd_buf
.empty() && (cmd_buf
.back() == ' ' || cmd_buf
.back() == '\t' ||
169 cmd_buf
.back() == '\r' || cmd_buf
.back() == '\n'))
170 cmd_buf
.resize(cmd_buf
.size()-1);
171 log_header(design
, "Shell command: %s\n", cmd_buf
.c_str());
172 int retCode
= run_command(cmd_buf
);
174 log_cmd_error("Shell command returned error code %d.\n", retCode
);
178 while (!tok
.empty()) {
181 for (stop
= 0; stop
< GetSize(cmd_buf
); stop
++)
182 if (cmd_buf
[stop
] == '\r' || cmd_buf
[stop
] == '\n')
184 cmd_buf
= cmd_buf
.substr(stop
);
186 if (tok
.back() == ';') {
187 int num_semikolon
= 0;
188 while (!tok
.empty() && tok
.back() == ';')
189 tok
.resize(tok
.size()-1), num_semikolon
++;
194 if (num_semikolon
== 2)
195 call(design
, "clean");
196 if (num_semikolon
== 3)
197 call(design
, "clean -purge");
200 bool found_nl
= false;
201 for (auto c
: cmd_buf
) {
202 if (c
== ' ' || c
== '\t')
204 if (c
== '\r' || c
== '\n')
212 tok
= next_token(cmd_buf
, " \t\r\n", true);
218 void Pass::call(RTLIL::Design
*design
, std::vector
<std::string
> args
)
220 if (args
.size() == 0 || args
[0][0] == '#' || args
[0][0] == ':')
224 log("%s", create_prompt(design
, 0));
225 for (size_t i
= 0; i
< args
.size(); i
++)
226 log("%s%s", i
? " " : "", args
[i
].c_str());
230 if (pass_register
.count(args
[0]) == 0)
231 log_cmd_error("No such command: %s (type 'help' for a command overview)\n", args
[0].c_str());
233 size_t orig_sel_stack_pos
= design
->selection_stack
.size();
234 auto state
= pass_register
[args
[0]]->pre_execute();
235 pass_register
[args
[0]]->execute(args
, design
);
236 pass_register
[args
[0]]->post_execute(state
);
237 while (design
->selection_stack
.size() > orig_sel_stack_pos
)
238 design
->selection_stack
.pop_back();
243 void Pass::call_on_selection(RTLIL::Design
*design
, const RTLIL::Selection
&selection
, std::string command
)
245 std::string backup_selected_active_module
= design
->selected_active_module
;
246 design
->selected_active_module
.clear();
247 design
->selection_stack
.push_back(selection
);
249 Pass::call(design
, command
);
251 design
->selection_stack
.pop_back();
252 design
->selected_active_module
= backup_selected_active_module
;
255 void Pass::call_on_selection(RTLIL::Design
*design
, const RTLIL::Selection
&selection
, std::vector
<std::string
> args
)
257 std::string backup_selected_active_module
= design
->selected_active_module
;
258 design
->selected_active_module
.clear();
259 design
->selection_stack
.push_back(selection
);
261 Pass::call(design
, args
);
263 design
->selection_stack
.pop_back();
264 design
->selected_active_module
= backup_selected_active_module
;
267 void Pass::call_on_module(RTLIL::Design
*design
, RTLIL::Module
*module
, std::string command
)
269 std::string backup_selected_active_module
= design
->selected_active_module
;
270 design
->selected_active_module
= module
->name
.str();
271 design
->selection_stack
.push_back(RTLIL::Selection(false));
272 design
->selection_stack
.back().select(module
);
274 Pass::call(design
, command
);
276 design
->selection_stack
.pop_back();
277 design
->selected_active_module
= backup_selected_active_module
;
280 void Pass::call_on_module(RTLIL::Design
*design
, RTLIL::Module
*module
, std::vector
<std::string
> args
)
282 std::string backup_selected_active_module
= design
->selected_active_module
;
283 design
->selected_active_module
= module
->name
.str();
284 design
->selection_stack
.push_back(RTLIL::Selection(false));
285 design
->selection_stack
.back().select(module
);
287 Pass::call(design
, args
);
289 design
->selection_stack
.pop_back();
290 design
->selected_active_module
= backup_selected_active_module
;
293 bool ScriptPass::check_label(std::string label
, std::string info
)
295 if (active_design
== nullptr) {
298 log(" %s:\n", label
.c_str());
300 log(" %s: %s\n", label
.c_str(), info
.c_str());
303 if (!active_run_from
.empty() && active_run_from
== active_run_to
) {
304 block_active
= (label
== active_run_from
);
306 if (label
== active_run_from
)
308 if (label
== active_run_to
)
309 block_active
= false;
315 void ScriptPass::run(std::string command
, std::string info
)
317 if (active_design
== nullptr) {
319 log(" %s\n", command
.c_str());
321 log(" %s %s\n", command
.c_str(), info
.c_str());
323 Pass::call(active_design
, command
);
326 void ScriptPass::run_script(RTLIL::Design
*design
, std::string run_from
, std::string run_to
)
329 active_design
= design
;
330 block_active
= run_from
.empty();
331 active_run_from
= run_from
;
332 active_run_to
= run_to
;
336 void ScriptPass::help_script()
340 active_design
= nullptr;
342 active_run_from
.clear();
343 active_run_to
.clear();
347 Frontend::Frontend(std::string name
, std::string short_help
) :
348 Pass(name
.rfind("=", 0) == 0 ? name
.substr(1) : "read_" + name
, short_help
),
349 frontend_name(name
.rfind("=", 0) == 0 ? name
.substr(1) : name
)
353 void Frontend::run_register()
355 log_assert(pass_register
.count(pass_name
) == 0);
356 pass_register
[pass_name
] = this;
358 log_assert(frontend_register
.count(frontend_name
) == 0);
359 frontend_register
[frontend_name
] = this;
362 Frontend::~Frontend()
366 void Frontend::execute(std::vector
<std::string
> args
, RTLIL::Design
*design
)
368 log_assert(next_args
.empty());
370 std::istream
*f
= NULL
;
372 auto state
= pre_execute();
373 execute(f
, std::string(), args
, design
);
377 } while (!args
.empty());
380 FILE *Frontend::current_script_file
= NULL
;
381 std::string
Frontend::last_here_document
;
383 void Frontend::extra_args(std::istream
*&f
, std::string
&filename
, std::vector
<std::string
> args
, size_t argidx
)
385 bool called_with_fp
= f
!= NULL
;
389 if (argidx
< args
.size())
391 std::string arg
= args
[argidx
];
393 if (arg
.substr(0, 1) == "-")
394 cmd_error(args
, argidx
, "Unknown option or option in arguments.");
396 cmd_error(args
, argidx
, "Extra filename argument in direct file mode.");
399 if (filename
== "<<" && argidx
+1 < args
.size())
400 filename
+= args
[++argidx
];
401 if (filename
.substr(0, 2) == "<<") {
402 if (Frontend::current_script_file
== NULL
)
403 log_error("Unexpected here document '%s' outside of script!\n", filename
.c_str());
404 if (filename
.size() <= 2)
405 log_error("Missing EOT marker in here document!\n");
406 std::string eot_marker
= filename
.substr(2);
407 last_here_document
.clear();
412 if (fgets(block
, 4096, Frontend::current_script_file
) == NULL
)
413 log_error("Unexpected end of file in here document '%s'!\n", filename
.c_str());
415 if (buffer
.size() > 0 && (buffer
[buffer
.size() - 1] == '\n' || buffer
[buffer
.size() - 1] == '\r'))
418 size_t indent
= buffer
.find_first_not_of(" \t\r\n");
419 if (indent
!= std::string::npos
&& buffer
.substr(indent
, eot_marker
.size()) == eot_marker
)
421 last_here_document
+= buffer
;
423 f
= new std::istringstream(last_here_document
);
425 rewrite_filename(filename
);
426 vector
<string
> filenames
= glob_filename(filename
);
427 filename
= filenames
.front();
428 if (GetSize(filenames
) > 1) {
429 next_args
.insert(next_args
.end(), args
.begin(), args
.begin()+argidx
);
430 next_args
.insert(next_args
.end(), filenames
.begin()+1, filenames
.end());
432 std::ifstream
*ff
= new std::ifstream
;
433 ff
->open(filename
.c_str());
434 yosys_input_files
.insert(filename
);
441 log_cmd_error("Can't open input file `%s' for reading: %s\n", filename
.c_str(), strerror(errno
));
443 for (size_t i
= argidx
+1; i
< args
.size(); i
++)
444 if (args
[i
].substr(0, 1) == "-")
445 cmd_error(args
, i
, "Found option, expected arguments.");
447 if (argidx
+1 < args
.size()) {
448 if (next_args
.empty())
449 next_args
.insert(next_args
.end(), args
.begin(), args
.begin()+argidx
);
450 next_args
.insert(next_args
.end(), args
.begin()+argidx
+1, args
.end());
451 args
.erase(args
.begin()+argidx
+1, args
.end());
456 cmd_error(args
, argidx
, "No filename given.");
459 args
.push_back(filename
);
461 // cmd_log_args(args);
464 void Frontend::frontend_call(RTLIL::Design
*design
, std::istream
*f
, std::string filename
, std::string command
)
466 std::vector
<std::string
> args
;
467 char *s
= strdup(command
.c_str());
468 for (char *p
= strtok(s
, " \t\r\n"); p
; p
= strtok(NULL
, " \t\r\n"))
471 frontend_call(design
, f
, filename
, args
);
474 void Frontend::frontend_call(RTLIL::Design
*design
, std::istream
*f
, std::string filename
, std::vector
<std::string
> args
)
476 if (args
.size() == 0)
478 if (frontend_register
.count(args
[0]) == 0)
479 log_cmd_error("No such frontend: %s\n", args
[0].c_str());
482 auto state
= frontend_register
[args
[0]]->pre_execute();
483 frontend_register
[args
[0]]->execute(f
, filename
, args
, design
);
484 frontend_register
[args
[0]]->post_execute(state
);
485 } else if (filename
== "-") {
486 std::istream
*f_cin
= &std::cin
;
487 auto state
= frontend_register
[args
[0]]->pre_execute();
488 frontend_register
[args
[0]]->execute(f_cin
, "<stdin>", args
, design
);
489 frontend_register
[args
[0]]->post_execute(state
);
491 if (!filename
.empty())
492 args
.push_back(filename
);
493 frontend_register
[args
[0]]->execute(args
, design
);
499 Backend::Backend(std::string name
, std::string short_help
) :
500 Pass(name
.rfind("=", 0) == 0 ? name
.substr(1) : "write_" + name
, short_help
),
501 backend_name(name
.rfind("=", 0) == 0 ? name
.substr(1) : name
)
505 void Backend::run_register()
507 log_assert(pass_register
.count(pass_name
) == 0);
508 pass_register
[pass_name
] = this;
510 log_assert(backend_register
.count(backend_name
) == 0);
511 backend_register
[backend_name
] = this;
518 void Backend::execute(std::vector
<std::string
> args
, RTLIL::Design
*design
)
520 std::ostream
*f
= NULL
;
521 auto state
= pre_execute();
522 execute(f
, std::string(), args
, design
);
528 void Backend::extra_args(std::ostream
*&f
, std::string
&filename
, std::vector
<std::string
> args
, size_t argidx
)
530 bool called_with_fp
= f
!= NULL
;
532 for (; argidx
< args
.size(); argidx
++)
534 std::string arg
= args
[argidx
];
536 if (arg
.substr(0, 1) == "-" && arg
!= "-")
537 cmd_error(args
, argidx
, "Unknown option or option in arguments.");
539 cmd_error(args
, argidx
, "Extra filename argument in direct file mode.");
542 filename
= "<stdout>";
548 rewrite_filename(filename
);
549 std::ofstream
*ff
= new std::ofstream
;
550 ff
->open(filename
.c_str(), std::ofstream::trunc
);
551 yosys_output_files
.insert(filename
);
554 log_cmd_error("Can't open output file `%s' for writing: %s\n", filename
.c_str(), strerror(errno
));
560 args
.push_back(filename
);
562 // cmd_log_args(args);
565 filename
= "<stdout>";
570 void Backend::backend_call(RTLIL::Design
*design
, std::ostream
*f
, std::string filename
, std::string command
)
572 std::vector
<std::string
> args
;
573 char *s
= strdup(command
.c_str());
574 for (char *p
= strtok(s
, " \t\r\n"); p
; p
= strtok(NULL
, " \t\r\n"))
577 backend_call(design
, f
, filename
, args
);
580 void Backend::backend_call(RTLIL::Design
*design
, std::ostream
*f
, std::string filename
, std::vector
<std::string
> args
)
582 if (args
.size() == 0)
584 if (backend_register
.count(args
[0]) == 0)
585 log_cmd_error("No such backend: %s\n", args
[0].c_str());
587 size_t orig_sel_stack_pos
= design
->selection_stack
.size();
590 auto state
= backend_register
[args
[0]]->pre_execute();
591 backend_register
[args
[0]]->execute(f
, filename
, args
, design
);
592 backend_register
[args
[0]]->post_execute(state
);
593 } else if (filename
== "-") {
594 std::ostream
*f_cout
= &std::cout
;
595 auto state
= backend_register
[args
[0]]->pre_execute();
596 backend_register
[args
[0]]->execute(f_cout
, "<stdout>", args
, design
);
597 backend_register
[args
[0]]->post_execute(state
);
599 if (!filename
.empty())
600 args
.push_back(filename
);
601 backend_register
[args
[0]]->execute(args
, design
);
604 while (design
->selection_stack
.size() > orig_sel_stack_pos
)
605 design
->selection_stack
.pop_back();
610 static struct CellHelpMessages
{
611 dict
<string
, string
> cell_help
, cell_code
;
613 #include "techlibs/common/simlib_help.inc"
614 #include "techlibs/common/simcells_help.inc"
618 } cell_help_messages
;
620 struct HelpPass
: public Pass
{
621 HelpPass() : Pass("help", "display help messages") { }
622 void help() YS_OVERRIDE
625 log(" help ................ list all commands\n");
626 log(" help <command> ...... print help message for given command\n");
627 log(" help -all ........... print complete command reference\n");
629 log(" help -cells .......... list all cell types\n");
630 log(" help <celltype> ..... print help message for given cell type\n");
631 log(" help <celltype>+ .... print verilog code for given cell type\n");
634 void escape_tex(std::string
&tex
)
636 for (size_t pos
= 0; (pos
= tex
.find('_', pos
)) != std::string::npos
; pos
+= 2)
637 tex
.replace(pos
, 1, "\\_");
638 for (size_t pos
= 0; (pos
= tex
.find('$', pos
)) != std::string::npos
; pos
+= 2)
639 tex
.replace(pos
, 1, "\\$");
641 void write_tex(FILE *f
, std::string cmd
, std::string title
, std::string text
)
643 size_t begin
= text
.find_first_not_of("\n"), end
= text
.find_last_not_of("\n");
644 if (begin
!= std::string::npos
&& end
!= std::string::npos
&& begin
< end
)
645 text
= text
.substr(begin
, end
-begin
+1);
646 std::string cmd_unescaped
= cmd
;
649 fprintf(f
, "\\section{%s -- %s}\n", cmd
.c_str(), title
.c_str());
650 fprintf(f
, "\\label{cmd:%s}\n", cmd_unescaped
.c_str());
651 fprintf(f
, "\\begin{lstlisting}[numbers=left,frame=single]\n");
652 fprintf(f
, "%s\n\\end{lstlisting}\n\n", text
.c_str());
654 void escape_html(std::string
&html
)
657 while ((pos
= html
.find_first_of("<>&", pos
)) != std::string::npos
)
660 html
.replace(pos
, 1, "<");
664 html
.replace(pos
, 1, ">");
668 html
.replace(pos
, 1, "&");
673 void write_html(FILE *idxf
, std::string cmd
, std::string title
, std::string text
)
675 FILE *f
= fopen(stringf("cmd_%s.in", cmd
.c_str()).c_str(), "wt");
676 fprintf(idxf
, "<li><a href=\"cmd_%s.html\"> ", cmd
.c_str());
682 fprintf(idxf
, "%s</a> <span>%s</span></a>\n", cmd
.c_str(), title
.c_str());
684 fprintf(f
, "@cmd_header %s@\n", cmd
.c_str());
685 fprintf(f
, "<h1>%s - %s</h1>\n", cmd
.c_str(), title
.c_str());
686 fprintf(f
, "<pre>%s</pre>\n", text
.c_str());
687 fprintf(f
, "@footer@\n");
691 void execute(std::vector
<std::string
> args
, RTLIL::Design
*) YS_OVERRIDE
693 if (args
.size() == 1) {
695 for (auto &it
: pass_register
)
696 log(" %-20s %s\n", it
.first
.c_str(), it
.second
->short_help
.c_str());
698 log("Type 'help <command>' for more information on a command.\n");
699 log("Type 'help -cells' for a list of all cell types.\n");
704 if (args
.size() == 2) {
705 if (args
[1] == "-all") {
706 for (auto &it
: pass_register
) {
708 log("%s -- %s\n", it
.first
.c_str(), it
.second
->short_help
.c_str());
709 for (size_t i
= 0; i
< it
.first
.size() + it
.second
->short_help
.size() + 6; i
++)
715 else if (args
[1] == "-cells") {
717 for (auto &it
: cell_help_messages
.cell_help
) {
718 string line
= split_tokens(it
.second
, "\n").at(0);
719 string cell_name
= next_token(line
);
720 log(" %-15s %s\n", cell_name
.c_str(), line
.c_str());
723 log("Type 'help <cell_type>' for more information on a cell type.\n");
727 // this option is undocumented as it is for internal use only
728 else if (args
[1] == "-write-tex-command-reference-manual") {
729 FILE *f
= fopen("command-reference-manual.tex", "wt");
730 fprintf(f
, "%% Generated using the yosys 'help -write-tex-command-reference-manual' command.\n\n");
731 for (auto &it
: pass_register
) {
732 std::ostringstream buf
;
733 log_streams
.push_back(&buf
);
735 log_streams
.pop_back();
736 write_tex(f
, it
.first
, it
.second
->short_help
, buf
.str());
740 // this option is undocumented as it is for internal use only
741 else if (args
[1] == "-write-web-command-reference-manual") {
742 FILE *f
= fopen("templates/cmd_index.in", "wt");
743 for (auto &it
: pass_register
) {
744 std::ostringstream buf
;
745 log_streams
.push_back(&buf
);
747 log_streams
.pop_back();
748 write_html(f
, it
.first
, it
.second
->short_help
, buf
.str());
752 else if (pass_register
.count(args
[1])) {
753 pass_register
.at(args
[1])->help();
755 else if (cell_help_messages
.cell_help
.count(args
[1])) {
756 log("%s", cell_help_messages
.cell_help
.at(args
[1]).c_str());
757 log("Run 'help %s+' to display the Verilog model for this cell type.\n", args
[1].c_str());
760 else if (cell_help_messages
.cell_code
.count(args
[1])) {
762 log("%s", cell_help_messages
.cell_code
.at(args
[1]).c_str());
765 log("No such command or cell type: %s\n", args
[1].c_str());
773 struct EchoPass
: public Pass
{
774 EchoPass() : Pass("echo", "turning echoing back of commands on and off") { }
775 void help() YS_OVERRIDE
780 log("Print all commands to log before executing them.\n");
785 log("Do not print all commands to log before executing them. (default)\n");
788 void execute(std::vector
<std::string
> args
, RTLIL::Design
*) YS_OVERRIDE
791 cmd_error(args
, 2, "Unexpected argument.");
793 if (args
.size() == 2) {
796 else if (args
[1] == "off")
799 cmd_error(args
, 1, "Unexpected argument.");
802 log("echo %s\n", echo_mode
? "on" : "off");
806 SatSolver
*yosys_satsolver_list
;
807 SatSolver
*yosys_satsolver
;
809 struct MinisatSatSolver
: public SatSolver
{
810 MinisatSatSolver() : SatSolver("minisat") {
811 yosys_satsolver
= this;
813 ezSAT
*create() YS_OVERRIDE
{
814 return new ezMiniSAT();