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 int64_t time_ns
= PerformanceTimer::query() - state
.begin_ns
;
90 runtime_ns
+= time_ns
;
91 current_pass
= state
.parent_pass
;
93 current_pass
->runtime_ns
-= time_ns
;
99 log("No help message for command `%s'.\n", pass_name
.c_str());
103 void Pass::clear_flags()
107 void Pass::cmd_log_args(const std::vector
<std::string
> &args
)
109 if (args
.size() <= 1)
111 log("Full command line:");
112 for (size_t i
= 0; i
< args
.size(); i
++)
113 log(" %s", args
[i
].c_str());
117 void Pass::cmd_error(const std::vector
<std::string
> &args
, size_t argidx
, std::string msg
)
119 std::string command_text
;
122 for (size_t i
= 0; i
< args
.size(); i
++) {
124 error_pos
+= args
[i
].size() + 1;
125 command_text
= command_text
+ (command_text
.empty() ? "" : " ") + args
[i
];
128 log("\nSyntax error in command `%s':\n", command_text
.c_str());
131 log_cmd_error("Command syntax error: %s\n> %s\n> %*s^\n",
132 msg
.c_str(), command_text
.c_str(), error_pos
, "");
135 void Pass::extra_args(std::vector
<std::string
> args
, size_t argidx
, RTLIL::Design
*design
, bool select
)
137 for (; argidx
< args
.size(); argidx
++)
139 std::string arg
= args
[argidx
];
141 if (arg
.substr(0, 1) == "-")
142 cmd_error(args
, argidx
, "Unknown option or option in arguments.");
145 cmd_error(args
, argidx
, "Extra argument.");
147 handle_extra_select_args(this, args
, argidx
, args
.size(), design
);
150 // cmd_log_args(args);
153 void Pass::call(RTLIL::Design
*design
, std::string command
)
155 std::vector
<std::string
> args
;
157 std::string cmd_buf
= command
;
158 std::string tok
= next_token(cmd_buf
, " \t\r\n", true);
164 cmd_buf
= command
.substr(command
.find('!') + 1);
165 while (!cmd_buf
.empty() && (cmd_buf
.back() == ' ' || cmd_buf
.back() == '\t' ||
166 cmd_buf
.back() == '\r' || cmd_buf
.back() == '\n'))
167 cmd_buf
.resize(cmd_buf
.size()-1);
168 log_header(design
, "Shell command: %s\n", cmd_buf
.c_str());
169 int retCode
= run_command(cmd_buf
);
171 log_cmd_error("Shell command returned error code %d.\n", retCode
);
175 while (!tok
.empty()) {
178 for (stop
= 0; stop
< GetSize(cmd_buf
); stop
++)
179 if (cmd_buf
[stop
] == '\r' || cmd_buf
[stop
] == '\n')
181 cmd_buf
= cmd_buf
.substr(stop
);
183 if (tok
.back() == ';') {
184 int num_semikolon
= 0;
185 while (!tok
.empty() && tok
.back() == ';')
186 tok
.resize(tok
.size()-1), num_semikolon
++;
191 if (num_semikolon
== 2)
192 call(design
, "clean");
193 if (num_semikolon
== 3)
194 call(design
, "clean -purge");
197 bool found_nl
= false;
198 for (auto c
: cmd_buf
) {
199 if (c
== ' ' || c
== '\t')
201 if (c
== '\r' || c
== '\n')
209 tok
= next_token(cmd_buf
, " \t\r\n", true);
215 void Pass::call(RTLIL::Design
*design
, std::vector
<std::string
> args
)
217 if (args
.size() == 0 || args
[0][0] == '#' || args
[0][0] == ':')
221 log("%s", create_prompt(design
, 0));
222 for (size_t i
= 0; i
< args
.size(); i
++)
223 log("%s%s", i
? " " : "", args
[i
].c_str());
227 if (pass_register
.count(args
[0]) == 0)
228 log_cmd_error("No such command: %s (type 'help' for a command overview)\n", args
[0].c_str());
230 size_t orig_sel_stack_pos
= design
->selection_stack
.size();
231 auto state
= pass_register
[args
[0]]->pre_execute();
232 pass_register
[args
[0]]->execute(args
, design
);
233 pass_register
[args
[0]]->post_execute(state
);
234 while (design
->selection_stack
.size() > orig_sel_stack_pos
)
235 design
->selection_stack
.pop_back();
240 void Pass::call_on_selection(RTLIL::Design
*design
, const RTLIL::Selection
&selection
, std::string command
)
242 std::string backup_selected_active_module
= design
->selected_active_module
;
243 design
->selected_active_module
.clear();
244 design
->selection_stack
.push_back(selection
);
246 Pass::call(design
, command
);
248 design
->selection_stack
.pop_back();
249 design
->selected_active_module
= backup_selected_active_module
;
252 void Pass::call_on_selection(RTLIL::Design
*design
, const RTLIL::Selection
&selection
, std::vector
<std::string
> args
)
254 std::string backup_selected_active_module
= design
->selected_active_module
;
255 design
->selected_active_module
.clear();
256 design
->selection_stack
.push_back(selection
);
258 Pass::call(design
, args
);
260 design
->selection_stack
.pop_back();
261 design
->selected_active_module
= backup_selected_active_module
;
264 void Pass::call_on_module(RTLIL::Design
*design
, RTLIL::Module
*module
, std::string command
)
266 std::string backup_selected_active_module
= design
->selected_active_module
;
267 design
->selected_active_module
= module
->name
.str();
268 design
->selection_stack
.push_back(RTLIL::Selection(false));
269 design
->selection_stack
.back().select(module
);
271 Pass::call(design
, command
);
273 design
->selection_stack
.pop_back();
274 design
->selected_active_module
= backup_selected_active_module
;
277 void Pass::call_on_module(RTLIL::Design
*design
, RTLIL::Module
*module
, std::vector
<std::string
> args
)
279 std::string backup_selected_active_module
= design
->selected_active_module
;
280 design
->selected_active_module
= module
->name
.str();
281 design
->selection_stack
.push_back(RTLIL::Selection(false));
282 design
->selection_stack
.back().select(module
);
284 Pass::call(design
, args
);
286 design
->selection_stack
.pop_back();
287 design
->selected_active_module
= backup_selected_active_module
;
290 bool ScriptPass::check_label(std::string label
, std::string info
)
292 if (active_design
== nullptr) {
295 log(" %s:\n", label
.c_str());
297 log(" %s: %s\n", label
.c_str(), info
.c_str());
300 if (!active_run_from
.empty() && active_run_from
== active_run_to
) {
301 block_active
= (label
== active_run_from
);
303 if (label
== active_run_from
)
305 if (label
== active_run_to
)
306 block_active
= false;
312 void ScriptPass::run(std::string command
, std::string info
)
314 if (active_design
== nullptr) {
316 log(" %s\n", command
.c_str());
318 log(" %s %s\n", command
.c_str(), info
.c_str());
320 Pass::call(active_design
, command
);
323 void ScriptPass::run_script(RTLIL::Design
*design
, std::string run_from
, std::string run_to
)
326 active_design
= design
;
327 block_active
= run_from
.empty();
328 active_run_from
= run_from
;
329 active_run_to
= run_to
;
333 void ScriptPass::help_script()
337 active_design
= nullptr;
339 active_run_from
.clear();
340 active_run_to
.clear();
344 Frontend::Frontend(std::string name
, std::string short_help
) :
345 Pass(name
.rfind("=", 0) == 0 ? name
.substr(1) : "read_" + name
, short_help
),
346 frontend_name(name
.rfind("=", 0) == 0 ? name
.substr(1) : name
)
350 void Frontend::run_register()
352 log_assert(pass_register
.count(pass_name
) == 0);
353 pass_register
[pass_name
] = this;
355 log_assert(frontend_register
.count(frontend_name
) == 0);
356 frontend_register
[frontend_name
] = this;
359 Frontend::~Frontend()
363 void Frontend::execute(std::vector
<std::string
> args
, RTLIL::Design
*design
)
365 log_assert(next_args
.empty());
367 std::istream
*f
= NULL
;
369 auto state
= pre_execute();
370 execute(f
, std::string(), args
, design
);
374 } while (!args
.empty());
377 FILE *Frontend::current_script_file
= NULL
;
378 std::string
Frontend::last_here_document
;
380 void Frontend::extra_args(std::istream
*&f
, std::string
&filename
, std::vector
<std::string
> args
, size_t argidx
)
382 bool called_with_fp
= f
!= NULL
;
386 if (argidx
< args
.size())
388 std::string arg
= args
[argidx
];
390 if (arg
.substr(0, 1) == "-")
391 cmd_error(args
, argidx
, "Unknown option or option in arguments.");
393 cmd_error(args
, argidx
, "Extra filename argument in direct file mode.");
396 if (filename
== "<<" && argidx
+1 < args
.size())
397 filename
+= args
[++argidx
];
398 if (filename
.substr(0, 2) == "<<") {
399 if (Frontend::current_script_file
== NULL
)
400 log_error("Unexpected here document '%s' outside of script!\n", filename
.c_str());
401 if (filename
.size() <= 2)
402 log_error("Missing EOT marker in here document!\n");
403 std::string eot_marker
= filename
.substr(2);
404 last_here_document
.clear();
409 if (fgets(block
, 4096, Frontend::current_script_file
) == NULL
)
410 log_error("Unexpected end of file in here document '%s'!\n", filename
.c_str());
412 if (buffer
.size() > 0 && (buffer
[buffer
.size() - 1] == '\n' || buffer
[buffer
.size() - 1] == '\r'))
415 size_t indent
= buffer
.find_first_not_of(" \t\r\n");
416 if (indent
!= std::string::npos
&& buffer
.substr(indent
, eot_marker
.size()) == eot_marker
)
418 last_here_document
+= buffer
;
420 f
= new std::istringstream(last_here_document
);
422 rewrite_filename(filename
);
423 vector
<string
> filenames
= glob_filename(filename
);
424 filename
= filenames
.front();
425 if (GetSize(filenames
) > 1) {
426 next_args
.insert(next_args
.end(), args
.begin(), args
.begin()+argidx
);
427 next_args
.insert(next_args
.end(), filenames
.begin()+1, filenames
.end());
429 std::ifstream
*ff
= new std::ifstream
;
430 ff
->open(filename
.c_str());
431 yosys_input_files
.insert(filename
);
438 log_cmd_error("Can't open input file `%s' for reading: %s\n", filename
.c_str(), strerror(errno
));
440 for (size_t i
= argidx
+1; i
< args
.size(); i
++)
441 if (args
[i
].substr(0, 1) == "-")
442 cmd_error(args
, i
, "Found option, expected arguments.");
444 if (argidx
+1 < args
.size()) {
445 if (next_args
.empty())
446 next_args
.insert(next_args
.end(), args
.begin(), args
.begin()+argidx
);
447 next_args
.insert(next_args
.end(), args
.begin()+argidx
+1, args
.end());
448 args
.erase(args
.begin()+argidx
+1, args
.end());
453 cmd_error(args
, argidx
, "No filename given.");
456 args
.push_back(filename
);
458 // cmd_log_args(args);
461 void Frontend::frontend_call(RTLIL::Design
*design
, std::istream
*f
, std::string filename
, std::string command
)
463 std::vector
<std::string
> args
;
464 char *s
= strdup(command
.c_str());
465 for (char *p
= strtok(s
, " \t\r\n"); p
; p
= strtok(NULL
, " \t\r\n"))
468 frontend_call(design
, f
, filename
, args
);
471 void Frontend::frontend_call(RTLIL::Design
*design
, std::istream
*f
, std::string filename
, std::vector
<std::string
> args
)
473 if (args
.size() == 0)
475 if (frontend_register
.count(args
[0]) == 0)
476 log_cmd_error("No such frontend: %s\n", args
[0].c_str());
479 auto state
= frontend_register
[args
[0]]->pre_execute();
480 frontend_register
[args
[0]]->execute(f
, filename
, args
, design
);
481 frontend_register
[args
[0]]->post_execute(state
);
482 } else if (filename
== "-") {
483 std::istream
*f_cin
= &std::cin
;
484 auto state
= frontend_register
[args
[0]]->pre_execute();
485 frontend_register
[args
[0]]->execute(f_cin
, "<stdin>", args
, design
);
486 frontend_register
[args
[0]]->post_execute(state
);
488 if (!filename
.empty())
489 args
.push_back(filename
);
490 frontend_register
[args
[0]]->execute(args
, design
);
496 Backend::Backend(std::string name
, std::string short_help
) :
497 Pass(name
.rfind("=", 0) == 0 ? name
.substr(1) : "write_" + name
, short_help
),
498 backend_name(name
.rfind("=", 0) == 0 ? name
.substr(1) : name
)
502 void Backend::run_register()
504 log_assert(pass_register
.count(pass_name
) == 0);
505 pass_register
[pass_name
] = this;
507 log_assert(backend_register
.count(backend_name
) == 0);
508 backend_register
[backend_name
] = this;
515 void Backend::execute(std::vector
<std::string
> args
, RTLIL::Design
*design
)
517 std::ostream
*f
= NULL
;
518 auto state
= pre_execute();
519 execute(f
, std::string(), args
, design
);
525 void Backend::extra_args(std::ostream
*&f
, std::string
&filename
, std::vector
<std::string
> args
, size_t argidx
)
527 bool called_with_fp
= f
!= NULL
;
529 for (; argidx
< args
.size(); argidx
++)
531 std::string arg
= args
[argidx
];
533 if (arg
.substr(0, 1) == "-" && arg
!= "-")
534 cmd_error(args
, argidx
, "Unknown option or option in arguments.");
536 cmd_error(args
, argidx
, "Extra filename argument in direct file mode.");
539 filename
= "<stdout>";
545 std::ofstream
*ff
= new std::ofstream
;
546 ff
->open(filename
.c_str(), std::ofstream::trunc
);
547 yosys_output_files
.insert(filename
);
550 log_cmd_error("Can't open output file `%s' for writing: %s\n", filename
.c_str(), strerror(errno
));
556 args
.push_back(filename
);
558 // cmd_log_args(args);
561 filename
= "<stdout>";
566 void Backend::backend_call(RTLIL::Design
*design
, std::ostream
*f
, std::string filename
, std::string command
)
568 std::vector
<std::string
> args
;
569 char *s
= strdup(command
.c_str());
570 for (char *p
= strtok(s
, " \t\r\n"); p
; p
= strtok(NULL
, " \t\r\n"))
573 backend_call(design
, f
, filename
, args
);
576 void Backend::backend_call(RTLIL::Design
*design
, std::ostream
*f
, std::string filename
, std::vector
<std::string
> args
)
578 if (args
.size() == 0)
580 if (backend_register
.count(args
[0]) == 0)
581 log_cmd_error("No such backend: %s\n", args
[0].c_str());
583 size_t orig_sel_stack_pos
= design
->selection_stack
.size();
586 auto state
= backend_register
[args
[0]]->pre_execute();
587 backend_register
[args
[0]]->execute(f
, filename
, args
, design
);
588 backend_register
[args
[0]]->post_execute(state
);
589 } else if (filename
== "-") {
590 std::ostream
*f_cout
= &std::cout
;
591 auto state
= backend_register
[args
[0]]->pre_execute();
592 backend_register
[args
[0]]->execute(f_cout
, "<stdout>", args
, design
);
593 backend_register
[args
[0]]->post_execute(state
);
595 if (!filename
.empty())
596 args
.push_back(filename
);
597 backend_register
[args
[0]]->execute(args
, design
);
600 while (design
->selection_stack
.size() > orig_sel_stack_pos
)
601 design
->selection_stack
.pop_back();
606 static struct CellHelpMessages
{
607 dict
<string
, string
> cell_help
, cell_code
;
609 #include "techlibs/common/simlib_help.inc"
610 #include "techlibs/common/simcells_help.inc"
614 } cell_help_messages
;
616 struct HelpPass
: public Pass
{
617 HelpPass() : Pass("help", "display help messages") { }
618 void help() YS_OVERRIDE
621 log(" help ................ list all commands\n");
622 log(" help <command> ...... print help message for given command\n");
623 log(" help -all ........... print complete command reference\n");
625 log(" help -cells .......... list all cell types\n");
626 log(" help <celltype> ..... print help message for given cell type\n");
627 log(" help <celltype>+ .... print verilog code for given cell type\n");
630 void escape_tex(std::string
&tex
)
632 for (size_t pos
= 0; (pos
= tex
.find('_', pos
)) != std::string::npos
; pos
+= 2)
633 tex
.replace(pos
, 1, "\\_");
634 for (size_t pos
= 0; (pos
= tex
.find('$', pos
)) != std::string::npos
; pos
+= 2)
635 tex
.replace(pos
, 1, "\\$");
637 void write_tex(FILE *f
, std::string cmd
, std::string title
, std::string text
)
639 size_t begin
= text
.find_first_not_of("\n"), end
= text
.find_last_not_of("\n");
640 if (begin
!= std::string::npos
&& end
!= std::string::npos
&& begin
< end
)
641 text
= text
.substr(begin
, end
-begin
+1);
642 std::string cmd_unescaped
= cmd
;
645 fprintf(f
, "\\section{%s -- %s}\n", cmd
.c_str(), title
.c_str());
646 fprintf(f
, "\\label{cmd:%s}\n", cmd_unescaped
.c_str());
647 fprintf(f
, "\\begin{lstlisting}[numbers=left,frame=single]\n");
648 fprintf(f
, "%s\n\\end{lstlisting}\n\n", text
.c_str());
650 void escape_html(std::string
&html
)
653 while ((pos
= html
.find_first_of("<>&", pos
)) != std::string::npos
)
656 html
.replace(pos
, 1, "<");
660 html
.replace(pos
, 1, ">");
664 html
.replace(pos
, 1, "&");
669 void write_html(FILE *idxf
, std::string cmd
, std::string title
, std::string text
)
671 FILE *f
= fopen(stringf("cmd_%s.in", cmd
.c_str()).c_str(), "wt");
672 fprintf(idxf
, "<li><a href=\"cmd_%s.html\"> ", cmd
.c_str());
678 fprintf(idxf
, "%s</a> <span>%s</span></a>\n", cmd
.c_str(), title
.c_str());
680 fprintf(f
, "@cmd_header %s@\n", cmd
.c_str());
681 fprintf(f
, "<h1>%s - %s</h1>\n", cmd
.c_str(), title
.c_str());
682 fprintf(f
, "<pre>%s</pre>\n", text
.c_str());
683 fprintf(f
, "@footer@\n");
687 void execute(std::vector
<std::string
> args
, RTLIL::Design
*) YS_OVERRIDE
689 if (args
.size() == 1) {
691 for (auto &it
: pass_register
)
692 log(" %-20s %s\n", it
.first
.c_str(), it
.second
->short_help
.c_str());
694 log("Type 'help <command>' for more information on a command.\n");
695 log("Type 'help -cells' for a list of all cell types.\n");
700 if (args
.size() == 2) {
701 if (args
[1] == "-all") {
702 for (auto &it
: pass_register
) {
704 log("%s -- %s\n", it
.first
.c_str(), it
.second
->short_help
.c_str());
705 for (size_t i
= 0; i
< it
.first
.size() + it
.second
->short_help
.size() + 6; i
++)
711 else if (args
[1] == "-cells") {
713 for (auto &it
: cell_help_messages
.cell_help
) {
714 string line
= split_tokens(it
.second
, "\n").at(0);
715 string cell_name
= next_token(line
);
716 log(" %-15s %s\n", cell_name
.c_str(), line
.c_str());
719 log("Type 'help <cell_type>' for more information on a cell type.\n");
723 // this option is undocumented as it is for internal use only
724 else if (args
[1] == "-write-tex-command-reference-manual") {
725 FILE *f
= fopen("command-reference-manual.tex", "wt");
726 fprintf(f
, "%% Generated using the yosys 'help -write-tex-command-reference-manual' command.\n\n");
727 for (auto &it
: pass_register
) {
728 std::ostringstream buf
;
729 log_streams
.push_back(&buf
);
731 log_streams
.pop_back();
732 write_tex(f
, it
.first
, it
.second
->short_help
, buf
.str());
736 // this option is undocumented as it is for internal use only
737 else if (args
[1] == "-write-web-command-reference-manual") {
738 FILE *f
= fopen("templates/cmd_index.in", "wt");
739 for (auto &it
: pass_register
) {
740 std::ostringstream buf
;
741 log_streams
.push_back(&buf
);
743 log_streams
.pop_back();
744 write_html(f
, it
.first
, it
.second
->short_help
, buf
.str());
748 else if (pass_register
.count(args
[1])) {
749 pass_register
.at(args
[1])->help();
751 else if (cell_help_messages
.cell_help
.count(args
[1])) {
752 log("%s", cell_help_messages
.cell_help
.at(args
[1]).c_str());
753 log("Run 'help %s+' to display the Verilog model for this cell type.\n", args
[1].c_str());
756 else if (cell_help_messages
.cell_code
.count(args
[1])) {
758 log("%s", cell_help_messages
.cell_code
.at(args
[1]).c_str());
761 log("No such command or cell type: %s\n", args
[1].c_str());
769 struct EchoPass
: public Pass
{
770 EchoPass() : Pass("echo", "turning echoing back of commands on and off") { }
771 void help() YS_OVERRIDE
776 log("Print all commands to log before executing them.\n");
781 log("Do not print all commands to log before executing them. (default)\n");
784 void execute(std::vector
<std::string
> args
, RTLIL::Design
*) YS_OVERRIDE
787 cmd_error(args
, 2, "Unexpected argument.");
789 if (args
.size() == 2) {
792 else if (args
[1] == "off")
795 cmd_error(args
, 1, "Unexpected argument.");
798 log("echo %s\n", echo_mode
? "on" : "off");
802 SatSolver
*yosys_satsolver_list
;
803 SatSolver
*yosys_satsolver
;
805 struct MinisatSatSolver
: public SatSolver
{
806 MinisatSatSolver() : SatSolver("minisat") {
807 yosys_satsolver
= this;
809 ezSAT
*create() YS_OVERRIDE
{
810 return new ezMiniSAT();