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();
91 int64_t time_ns
= PerformanceTimer::query() - state
.begin_ns
;
92 runtime_ns
+= time_ns
;
93 current_pass
= state
.parent_pass
;
95 current_pass
->runtime_ns
-= time_ns
;
101 log("No help message for command `%s'.\n", pass_name
.c_str());
105 void Pass::clear_flags()
109 void Pass::cmd_log_args(const std::vector
<std::string
> &args
)
111 if (args
.size() <= 1)
113 log("Full command line:");
114 for (size_t i
= 0; i
< args
.size(); i
++)
115 log(" %s", args
[i
].c_str());
119 void Pass::cmd_error(const std::vector
<std::string
> &args
, size_t argidx
, std::string msg
)
121 std::string command_text
;
124 for (size_t i
= 0; i
< args
.size(); i
++) {
126 error_pos
+= args
[i
].size() + 1;
127 command_text
= command_text
+ (command_text
.empty() ? "" : " ") + args
[i
];
130 log("\nSyntax error in command `%s':\n", command_text
.c_str());
133 log_cmd_error("Command syntax error: %s\n> %s\n> %*s^\n",
134 msg
.c_str(), command_text
.c_str(), error_pos
, "");
137 void Pass::extra_args(std::vector
<std::string
> args
, size_t argidx
, RTLIL::Design
*design
, bool select
)
139 for (; argidx
< args
.size(); argidx
++)
141 std::string arg
= args
[argidx
];
143 if (arg
.substr(0, 1) == "-")
144 cmd_error(args
, argidx
, "Unknown option or option in arguments.");
147 cmd_error(args
, argidx
, "Extra argument.");
149 handle_extra_select_args(this, args
, argidx
, args
.size(), design
);
152 // cmd_log_args(args);
155 void Pass::call(RTLIL::Design
*design
, std::string command
)
157 std::vector
<std::string
> args
;
159 std::string cmd_buf
= command
;
160 std::string tok
= next_token(cmd_buf
, " \t\r\n", true);
166 cmd_buf
= command
.substr(command
.find('!') + 1);
167 while (!cmd_buf
.empty() && (cmd_buf
.back() == ' ' || cmd_buf
.back() == '\t' ||
168 cmd_buf
.back() == '\r' || cmd_buf
.back() == '\n'))
169 cmd_buf
.resize(cmd_buf
.size()-1);
170 log_header(design
, "Shell command: %s\n", cmd_buf
.c_str());
171 int retCode
= run_command(cmd_buf
);
173 log_cmd_error("Shell command returned error code %d.\n", retCode
);
177 while (!tok
.empty()) {
180 for (stop
= 0; stop
< GetSize(cmd_buf
); stop
++)
181 if (cmd_buf
[stop
] == '\r' || cmd_buf
[stop
] == '\n')
183 cmd_buf
= cmd_buf
.substr(stop
);
185 if (tok
.back() == ';') {
186 int num_semikolon
= 0;
187 while (!tok
.empty() && tok
.back() == ';')
188 tok
.resize(tok
.size()-1), num_semikolon
++;
193 if (num_semikolon
== 2)
194 call(design
, "clean");
195 if (num_semikolon
== 3)
196 call(design
, "clean -purge");
199 bool found_nl
= false;
200 for (auto c
: cmd_buf
) {
201 if (c
== ' ' || c
== '\t')
203 if (c
== '\r' || c
== '\n')
211 tok
= next_token(cmd_buf
, " \t\r\n", true);
217 void Pass::call(RTLIL::Design
*design
, std::vector
<std::string
> args
)
219 if (args
.size() == 0 || args
[0][0] == '#' || args
[0][0] == ':')
223 log("%s", create_prompt(design
, 0));
224 for (size_t i
= 0; i
< args
.size(); i
++)
225 log("%s%s", i
? " " : "", args
[i
].c_str());
229 if (pass_register
.count(args
[0]) == 0)
230 log_cmd_error("No such command: %s (type 'help' for a command overview)\n", args
[0].c_str());
232 size_t orig_sel_stack_pos
= design
->selection_stack
.size();
233 auto state
= pass_register
[args
[0]]->pre_execute();
234 pass_register
[args
[0]]->execute(args
, design
);
235 pass_register
[args
[0]]->post_execute(state
);
236 while (design
->selection_stack
.size() > orig_sel_stack_pos
)
237 design
->selection_stack
.pop_back();
242 void Pass::call_on_selection(RTLIL::Design
*design
, const RTLIL::Selection
&selection
, std::string command
)
244 std::string backup_selected_active_module
= design
->selected_active_module
;
245 design
->selected_active_module
.clear();
246 design
->selection_stack
.push_back(selection
);
248 Pass::call(design
, command
);
250 design
->selection_stack
.pop_back();
251 design
->selected_active_module
= backup_selected_active_module
;
254 void Pass::call_on_selection(RTLIL::Design
*design
, const RTLIL::Selection
&selection
, std::vector
<std::string
> args
)
256 std::string backup_selected_active_module
= design
->selected_active_module
;
257 design
->selected_active_module
.clear();
258 design
->selection_stack
.push_back(selection
);
260 Pass::call(design
, args
);
262 design
->selection_stack
.pop_back();
263 design
->selected_active_module
= backup_selected_active_module
;
266 void Pass::call_on_module(RTLIL::Design
*design
, RTLIL::Module
*module
, std::string command
)
268 std::string backup_selected_active_module
= design
->selected_active_module
;
269 design
->selected_active_module
= module
->name
.str();
270 design
->selection_stack
.push_back(RTLIL::Selection(false));
271 design
->selection_stack
.back().select(module
);
273 Pass::call(design
, command
);
275 design
->selection_stack
.pop_back();
276 design
->selected_active_module
= backup_selected_active_module
;
279 void Pass::call_on_module(RTLIL::Design
*design
, RTLIL::Module
*module
, std::vector
<std::string
> args
)
281 std::string backup_selected_active_module
= design
->selected_active_module
;
282 design
->selected_active_module
= module
->name
.str();
283 design
->selection_stack
.push_back(RTLIL::Selection(false));
284 design
->selection_stack
.back().select(module
);
286 Pass::call(design
, args
);
288 design
->selection_stack
.pop_back();
289 design
->selected_active_module
= backup_selected_active_module
;
292 bool ScriptPass::check_label(std::string label
, std::string info
)
294 if (active_design
== nullptr) {
297 log(" %s:\n", label
.c_str());
299 log(" %s: %s\n", label
.c_str(), info
.c_str());
302 if (!active_run_from
.empty() && active_run_from
== active_run_to
) {
303 block_active
= (label
== active_run_from
);
305 if (label
== active_run_from
)
307 if (label
== active_run_to
)
308 block_active
= false;
314 void ScriptPass::run(std::string command
, std::string info
)
316 if (active_design
== nullptr) {
318 log(" %s\n", command
.c_str());
320 log(" %s %s\n", command
.c_str(), info
.c_str());
322 Pass::call(active_design
, command
);
325 void ScriptPass::run_script(RTLIL::Design
*design
, std::string run_from
, std::string run_to
)
328 active_design
= design
;
329 block_active
= run_from
.empty();
330 active_run_from
= run_from
;
331 active_run_to
= run_to
;
335 void ScriptPass::help_script()
339 active_design
= nullptr;
341 active_run_from
.clear();
342 active_run_to
.clear();
346 Frontend::Frontend(std::string name
, std::string short_help
) :
347 Pass(name
.rfind("=", 0) == 0 ? name
.substr(1) : "read_" + name
, short_help
),
348 frontend_name(name
.rfind("=", 0) == 0 ? name
.substr(1) : name
)
352 void Frontend::run_register()
354 log_assert(pass_register
.count(pass_name
) == 0);
355 pass_register
[pass_name
] = this;
357 log_assert(frontend_register
.count(frontend_name
) == 0);
358 frontend_register
[frontend_name
] = this;
361 Frontend::~Frontend()
365 void Frontend::execute(std::vector
<std::string
> args
, RTLIL::Design
*design
)
367 log_assert(next_args
.empty());
369 std::istream
*f
= NULL
;
371 auto state
= pre_execute();
372 execute(f
, std::string(), args
, design
);
376 } while (!args
.empty());
379 FILE *Frontend::current_script_file
= NULL
;
380 std::string
Frontend::last_here_document
;
382 void Frontend::extra_args(std::istream
*&f
, std::string
&filename
, std::vector
<std::string
> args
, size_t argidx
)
384 bool called_with_fp
= f
!= NULL
;
388 if (argidx
< args
.size())
390 std::string arg
= args
[argidx
];
392 if (arg
.substr(0, 1) == "-")
393 cmd_error(args
, argidx
, "Unknown option or option in arguments.");
395 cmd_error(args
, argidx
, "Extra filename argument in direct file mode.");
398 if (filename
== "<<" && argidx
+1 < args
.size())
399 filename
+= args
[++argidx
];
400 if (filename
.substr(0, 2) == "<<") {
401 if (Frontend::current_script_file
== NULL
)
402 log_error("Unexpected here document '%s' outside of script!\n", filename
.c_str());
403 if (filename
.size() <= 2)
404 log_error("Missing EOT marker in here document!\n");
405 std::string eot_marker
= filename
.substr(2);
406 last_here_document
.clear();
411 if (fgets(block
, 4096, Frontend::current_script_file
) == NULL
)
412 log_error("Unexpected end of file in here document '%s'!\n", filename
.c_str());
414 if (buffer
.size() > 0 && (buffer
[buffer
.size() - 1] == '\n' || buffer
[buffer
.size() - 1] == '\r'))
417 size_t indent
= buffer
.find_first_not_of(" \t\r\n");
418 if (indent
!= std::string::npos
&& buffer
.substr(indent
, eot_marker
.size()) == eot_marker
)
420 last_here_document
+= buffer
;
422 f
= new std::istringstream(last_here_document
);
424 rewrite_filename(filename
);
425 vector
<string
> filenames
= glob_filename(filename
);
426 filename
= filenames
.front();
427 if (GetSize(filenames
) > 1) {
428 next_args
.insert(next_args
.end(), args
.begin(), args
.begin()+argidx
);
429 next_args
.insert(next_args
.end(), filenames
.begin()+1, filenames
.end());
431 std::ifstream
*ff
= new std::ifstream
;
432 ff
->open(filename
.c_str());
433 yosys_input_files
.insert(filename
);
440 log_cmd_error("Can't open input file `%s' for reading: %s\n", filename
.c_str(), strerror(errno
));
442 for (size_t i
= argidx
+1; i
< args
.size(); i
++)
443 if (args
[i
].substr(0, 1) == "-")
444 cmd_error(args
, i
, "Found option, expected arguments.");
446 if (argidx
+1 < args
.size()) {
447 if (next_args
.empty())
448 next_args
.insert(next_args
.end(), args
.begin(), args
.begin()+argidx
);
449 next_args
.insert(next_args
.end(), args
.begin()+argidx
+1, args
.end());
450 args
.erase(args
.begin()+argidx
+1, args
.end());
455 cmd_error(args
, argidx
, "No filename given.");
458 args
.push_back(filename
);
460 // cmd_log_args(args);
463 void Frontend::frontend_call(RTLIL::Design
*design
, std::istream
*f
, std::string filename
, std::string command
)
465 std::vector
<std::string
> args
;
466 char *s
= strdup(command
.c_str());
467 for (char *p
= strtok(s
, " \t\r\n"); p
; p
= strtok(NULL
, " \t\r\n"))
470 frontend_call(design
, f
, filename
, args
);
473 void Frontend::frontend_call(RTLIL::Design
*design
, std::istream
*f
, std::string filename
, std::vector
<std::string
> args
)
475 if (args
.size() == 0)
477 if (frontend_register
.count(args
[0]) == 0)
478 log_cmd_error("No such frontend: %s\n", args
[0].c_str());
481 auto state
= frontend_register
[args
[0]]->pre_execute();
482 frontend_register
[args
[0]]->execute(f
, filename
, args
, design
);
483 frontend_register
[args
[0]]->post_execute(state
);
484 } else if (filename
== "-") {
485 std::istream
*f_cin
= &std::cin
;
486 auto state
= frontend_register
[args
[0]]->pre_execute();
487 frontend_register
[args
[0]]->execute(f_cin
, "<stdin>", args
, design
);
488 frontend_register
[args
[0]]->post_execute(state
);
490 if (!filename
.empty())
491 args
.push_back(filename
);
492 frontend_register
[args
[0]]->execute(args
, design
);
498 Backend::Backend(std::string name
, std::string short_help
) :
499 Pass(name
.rfind("=", 0) == 0 ? name
.substr(1) : "write_" + name
, short_help
),
500 backend_name(name
.rfind("=", 0) == 0 ? name
.substr(1) : name
)
504 void Backend::run_register()
506 log_assert(pass_register
.count(pass_name
) == 0);
507 pass_register
[pass_name
] = this;
509 log_assert(backend_register
.count(backend_name
) == 0);
510 backend_register
[backend_name
] = this;
517 void Backend::execute(std::vector
<std::string
> args
, RTLIL::Design
*design
)
519 std::ostream
*f
= NULL
;
520 auto state
= pre_execute();
521 execute(f
, std::string(), args
, design
);
527 void Backend::extra_args(std::ostream
*&f
, std::string
&filename
, std::vector
<std::string
> args
, size_t argidx
)
529 bool called_with_fp
= f
!= NULL
;
531 for (; argidx
< args
.size(); argidx
++)
533 std::string arg
= args
[argidx
];
535 if (arg
.substr(0, 1) == "-" && arg
!= "-")
536 cmd_error(args
, argidx
, "Unknown option or option in arguments.");
538 cmd_error(args
, argidx
, "Extra filename argument in direct file mode.");
541 filename
= "<stdout>";
547 std::ofstream
*ff
= new std::ofstream
;
548 ff
->open(filename
.c_str(), std::ofstream::trunc
);
549 yosys_output_files
.insert(filename
);
552 log_cmd_error("Can't open output file `%s' for writing: %s\n", filename
.c_str(), strerror(errno
));
558 args
.push_back(filename
);
560 // cmd_log_args(args);
563 filename
= "<stdout>";
568 void Backend::backend_call(RTLIL::Design
*design
, std::ostream
*f
, std::string filename
, std::string command
)
570 std::vector
<std::string
> args
;
571 char *s
= strdup(command
.c_str());
572 for (char *p
= strtok(s
, " \t\r\n"); p
; p
= strtok(NULL
, " \t\r\n"))
575 backend_call(design
, f
, filename
, args
);
578 void Backend::backend_call(RTLIL::Design
*design
, std::ostream
*f
, std::string filename
, std::vector
<std::string
> args
)
580 if (args
.size() == 0)
582 if (backend_register
.count(args
[0]) == 0)
583 log_cmd_error("No such backend: %s\n", args
[0].c_str());
585 size_t orig_sel_stack_pos
= design
->selection_stack
.size();
588 auto state
= backend_register
[args
[0]]->pre_execute();
589 backend_register
[args
[0]]->execute(f
, filename
, args
, design
);
590 backend_register
[args
[0]]->post_execute(state
);
591 } else if (filename
== "-") {
592 std::ostream
*f_cout
= &std::cout
;
593 auto state
= backend_register
[args
[0]]->pre_execute();
594 backend_register
[args
[0]]->execute(f_cout
, "<stdout>", args
, design
);
595 backend_register
[args
[0]]->post_execute(state
);
597 if (!filename
.empty())
598 args
.push_back(filename
);
599 backend_register
[args
[0]]->execute(args
, design
);
602 while (design
->selection_stack
.size() > orig_sel_stack_pos
)
603 design
->selection_stack
.pop_back();
608 static struct CellHelpMessages
{
609 dict
<string
, string
> cell_help
, cell_code
;
611 #include "techlibs/common/simlib_help.inc"
612 #include "techlibs/common/simcells_help.inc"
616 } cell_help_messages
;
618 struct HelpPass
: public Pass
{
619 HelpPass() : Pass("help", "display help messages") { }
620 void help() YS_OVERRIDE
623 log(" help ................ list all commands\n");
624 log(" help <command> ...... print help message for given command\n");
625 log(" help -all ........... print complete command reference\n");
627 log(" help -cells .......... list all cell types\n");
628 log(" help <celltype> ..... print help message for given cell type\n");
629 log(" help <celltype>+ .... print verilog code for given cell type\n");
632 void escape_tex(std::string
&tex
)
634 for (size_t pos
= 0; (pos
= tex
.find('_', pos
)) != std::string::npos
; pos
+= 2)
635 tex
.replace(pos
, 1, "\\_");
636 for (size_t pos
= 0; (pos
= tex
.find('$', pos
)) != std::string::npos
; pos
+= 2)
637 tex
.replace(pos
, 1, "\\$");
639 void write_tex(FILE *f
, std::string cmd
, std::string title
, std::string text
)
641 size_t begin
= text
.find_first_not_of("\n"), end
= text
.find_last_not_of("\n");
642 if (begin
!= std::string::npos
&& end
!= std::string::npos
&& begin
< end
)
643 text
= text
.substr(begin
, end
-begin
+1);
644 std::string cmd_unescaped
= cmd
;
647 fprintf(f
, "\\section{%s -- %s}\n", cmd
.c_str(), title
.c_str());
648 fprintf(f
, "\\label{cmd:%s}\n", cmd_unescaped
.c_str());
649 fprintf(f
, "\\begin{lstlisting}[numbers=left,frame=single]\n");
650 fprintf(f
, "%s\n\\end{lstlisting}\n\n", text
.c_str());
652 void escape_html(std::string
&html
)
655 while ((pos
= html
.find_first_of("<>&", pos
)) != std::string::npos
)
658 html
.replace(pos
, 1, "<");
662 html
.replace(pos
, 1, ">");
666 html
.replace(pos
, 1, "&");
671 void write_html(FILE *idxf
, std::string cmd
, std::string title
, std::string text
)
673 FILE *f
= fopen(stringf("cmd_%s.in", cmd
.c_str()).c_str(), "wt");
674 fprintf(idxf
, "<li><a href=\"cmd_%s.html\"> ", cmd
.c_str());
680 fprintf(idxf
, "%s</a> <span>%s</span></a>\n", cmd
.c_str(), title
.c_str());
682 fprintf(f
, "@cmd_header %s@\n", cmd
.c_str());
683 fprintf(f
, "<h1>%s - %s</h1>\n", cmd
.c_str(), title
.c_str());
684 fprintf(f
, "<pre>%s</pre>\n", text
.c_str());
685 fprintf(f
, "@footer@\n");
689 void execute(std::vector
<std::string
> args
, RTLIL::Design
*) YS_OVERRIDE
691 if (args
.size() == 1) {
693 for (auto &it
: pass_register
)
694 log(" %-20s %s\n", it
.first
.c_str(), it
.second
->short_help
.c_str());
696 log("Type 'help <command>' for more information on a command.\n");
697 log("Type 'help -cells' for a list of all cell types.\n");
702 if (args
.size() == 2) {
703 if (args
[1] == "-all") {
704 for (auto &it
: pass_register
) {
706 log("%s -- %s\n", it
.first
.c_str(), it
.second
->short_help
.c_str());
707 for (size_t i
= 0; i
< it
.first
.size() + it
.second
->short_help
.size() + 6; i
++)
713 else if (args
[1] == "-cells") {
715 for (auto &it
: cell_help_messages
.cell_help
) {
716 string line
= split_tokens(it
.second
, "\n").at(0);
717 string cell_name
= next_token(line
);
718 log(" %-15s %s\n", cell_name
.c_str(), line
.c_str());
721 log("Type 'help <cell_type>' for more information on a cell type.\n");
725 // this option is undocumented as it is for internal use only
726 else if (args
[1] == "-write-tex-command-reference-manual") {
727 FILE *f
= fopen("command-reference-manual.tex", "wt");
728 fprintf(f
, "%% Generated using the yosys 'help -write-tex-command-reference-manual' command.\n\n");
729 for (auto &it
: pass_register
) {
730 std::ostringstream buf
;
731 log_streams
.push_back(&buf
);
733 log_streams
.pop_back();
734 write_tex(f
, it
.first
, it
.second
->short_help
, buf
.str());
738 // this option is undocumented as it is for internal use only
739 else if (args
[1] == "-write-web-command-reference-manual") {
740 FILE *f
= fopen("templates/cmd_index.in", "wt");
741 for (auto &it
: pass_register
) {
742 std::ostringstream buf
;
743 log_streams
.push_back(&buf
);
745 log_streams
.pop_back();
746 write_html(f
, it
.first
, it
.second
->short_help
, buf
.str());
750 else if (pass_register
.count(args
[1])) {
751 pass_register
.at(args
[1])->help();
753 else if (cell_help_messages
.cell_help
.count(args
[1])) {
754 log("%s", cell_help_messages
.cell_help
.at(args
[1]).c_str());
755 log("Run 'help %s+' to display the Verilog model for this cell type.\n", args
[1].c_str());
758 else if (cell_help_messages
.cell_code
.count(args
[1])) {
760 log("%s", cell_help_messages
.cell_code
.at(args
[1]).c_str());
763 log("No such command or cell type: %s\n", args
[1].c_str());
771 struct EchoPass
: public Pass
{
772 EchoPass() : Pass("echo", "turning echoing back of commands on and off") { }
773 void help() YS_OVERRIDE
778 log("Print all commands to log before executing them.\n");
783 log("Do not print all commands to log before executing them. (default)\n");
786 void execute(std::vector
<std::string
> args
, RTLIL::Design
*) YS_OVERRIDE
789 cmd_error(args
, 2, "Unexpected argument.");
791 if (args
.size() == 2) {
794 else if (args
[1] == "off")
797 cmd_error(args
, 1, "Unexpected argument.");
800 log("echo %s\n", echo_mode
? "on" : "off");
804 SatSolver
*yosys_satsolver_list
;
805 SatSolver
*yosys_satsolver
;
807 struct MinisatSatSolver
: public SatSolver
{
808 MinisatSatSolver() : SatSolver("minisat") {
809 yosys_satsolver
= this;
811 ezSAT
*create() YS_OVERRIDE
{
812 return new ezMiniSAT();