yosys/kernel/driver.cc

348 lines
12 KiB
C++
Raw Normal View History

2013-01-05 04:13:26 -06:00
/*
* yosys -- Yosys Open SYnthesis Suite
*
* Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
*/
#include "kernel/yosys.h"
2013-01-05 04:13:26 -06:00
#include <readline/readline.h>
#include <readline/history.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <libgen.h>
#include <dlfcn.h>
#include <limits.h>
#include <errno.h>
2013-01-05 04:13:26 -06:00
USING_YOSYS_NAMESPACE
2013-01-05 04:13:26 -06:00
int main(int argc, char **argv)
{
std::string frontend_command = "auto";
std::string backend_command = "auto";
std::vector<std::string> passes_commands;
std::vector<void*> loaded_modules;
std::string output_filename = "";
2013-01-05 04:13:26 -06:00
std::string scriptfile = "";
bool scriptfile_tcl = false;
2013-01-05 04:13:26 -06:00
bool got_output_filename = false;
2014-07-30 17:53:21 -05:00
bool print_banner = true;
2014-08-01 11:42:10 -05:00
bool print_stats = true;
2014-07-30 18:05:27 -05:00
bool call_abort = false;
2013-01-05 04:13:26 -06:00
int history_offset = 0;
std::string history_file;
if (getenv("HOME") != NULL) {
history_file = stringf("%s/.yosys_history", getenv("HOME"));
read_history(history_file.c_str());
history_offset = where_history();
}
2013-01-05 04:13:26 -06:00
int opt;
2014-08-01 11:42:10 -05:00
while ((opt = getopt(argc, argv, "AQTVSm:f:Hh:b:o:p:l:qv:ts:c:")) != -1)
2013-01-05 04:13:26 -06:00
{
switch (opt)
{
2014-07-30 18:05:27 -05:00
case 'A':
call_abort = true;
break;
2014-07-30 17:53:21 -05:00
case 'Q':
print_banner = false;
break;
2014-08-01 11:42:10 -05:00
case 'T':
print_stats = false;
break;
case 'V':
printf("%s\n", yosys_version_str);
exit(0);
case 'S':
passes_commands.push_back("hierarchy");
passes_commands.push_back("proc");
passes_commands.push_back("opt");
passes_commands.push_back("memory");
passes_commands.push_back("opt");
passes_commands.push_back("techmap");
passes_commands.push_back("opt");
break;
case 'm':
loaded_modules.push_back(dlopen(optarg, RTLD_LAZY|RTLD_GLOBAL));
if (loaded_modules.back() == NULL) {
fprintf(stderr, "Can't load module `%s': %s\n", optarg, dlerror());
exit(1);
}
break;
2013-01-05 04:13:26 -06:00
case 'f':
frontend_command = optarg;
break;
2014-01-30 05:32:59 -06:00
case 'H':
passes_commands.push_back("help");
break;
2014-01-29 04:10:39 -06:00
case 'h':
passes_commands.push_back(stringf("help %s", optarg));
break;
2013-01-05 04:13:26 -06:00
case 'b':
backend_command = optarg;
break;
case 'p':
passes_commands.push_back(optarg);
break;
case 'o':
output_filename = optarg;
got_output_filename = true;
break;
case 'l':
log_files.push_back(fopen(optarg, "wt"));
if (log_files.back() == NULL) {
fprintf(stderr, "Can't open log file `%s' for writing!\n", optarg);
exit(1);
}
break;
case 'q':
log_errfile = stderr;
break;
case 'v':
log_errfile = stderr;
log_verbose_level = atoi(optarg);
break;
2013-01-05 04:13:26 -06:00
case 't':
log_time = true;
break;
case 's':
scriptfile = optarg;
scriptfile_tcl = false;
break;
case 'c':
scriptfile = optarg;
scriptfile_tcl = true;
2013-01-05 04:13:26 -06:00
break;
default:
2013-02-28 06:13:56 -06:00
fprintf(stderr, "\n");
2014-08-01 11:42:10 -05:00
fprintf(stderr, "Usage: %s [-V -S -Q -T -q] [-v <level>[-t] [-l <logfile>] [-o <outfile>] [-f <frontend>] [-h cmd] \\\n", argv[0]);
fprintf(stderr, " %*s[{-s|-c} <scriptfile>] [-p <pass> [-p ..]] [-b <backend>] [-m <module_file>] [<infile> [..]]\n", int(strlen(argv[0])+1), "");
2013-02-28 06:13:56 -06:00
fprintf(stderr, "\n");
2014-07-30 17:53:21 -05:00
fprintf(stderr, " -Q\n");
fprintf(stderr, " suppress printing of banner (copyright, disclaimer, version)\n");
fprintf(stderr, "\n");
2014-08-01 11:42:10 -05:00
fprintf(stderr, " -T\n");
fprintf(stderr, " suppress printing of footer (log hash, version, timing statistics)\n");
fprintf(stderr, "\n");
2013-02-28 06:13:56 -06:00
fprintf(stderr, " -q\n");
fprintf(stderr, " quiet operation. only write error messages to console\n");
fprintf(stderr, "\n");
fprintf(stderr, " -v <level>\n");
fprintf(stderr, " print log headers up to level <level> to the console. (implies -q)\n");
fprintf(stderr, "\n");
2013-02-28 06:13:56 -06:00
fprintf(stderr, " -t\n");
fprintf(stderr, " annotate all log messages with a time stamp\n");
fprintf(stderr, "\n");
fprintf(stderr, " -l logfile\n");
fprintf(stderr, " write log messages to the specified file\n");
fprintf(stderr, "\n");
fprintf(stderr, " -o outfile\n");
fprintf(stderr, " write the design to the specified file on exit\n");
fprintf(stderr, "\n");
fprintf(stderr, " -b backend\n");
fprintf(stderr, " use this backend for the output file specified on the command line\n");
fprintf(stderr, "\n");
fprintf(stderr, " -f backend\n");
fprintf(stderr, " use the specified front for the input files on the command line\n");
fprintf(stderr, "\n");
2014-01-30 05:32:59 -06:00
fprintf(stderr, " -H\n");
fprintf(stderr, " print the command list\n");
fprintf(stderr, "\n");
2014-01-29 04:10:39 -06:00
fprintf(stderr, " -h command\n");
fprintf(stderr, " print the help message for the specified command\n");
fprintf(stderr, "\n");
2013-02-28 06:13:56 -06:00
fprintf(stderr, " -s scriptfile\n");
fprintf(stderr, " execute the commands in the script file\n");
fprintf(stderr, "\n");
fprintf(stderr, " -c tcl_scriptfile\n");
2013-05-02 08:27:01 -05:00
fprintf(stderr, " execute the commands in the tcl script file (see 'help tcl' for details)\n");
fprintf(stderr, "\n");
2013-02-28 06:13:56 -06:00
fprintf(stderr, " -p command\n");
fprintf(stderr, " execute the commands\n");
fprintf(stderr, "\n");
fprintf(stderr, " -m module_file\n");
fprintf(stderr, " load the specified module (aka plugin)\n");
fprintf(stderr, "\n");
2014-07-30 18:05:27 -05:00
fprintf(stderr, " -A\n");
fprintf(stderr, " will call abort() at the end of the script. useful for debugging\n");
fprintf(stderr, "\n");
fprintf(stderr, " -V\n");
fprintf(stderr, " print version information and exit\n");
fprintf(stderr, "\n");
fprintf(stderr, "The option -S is an alias for the following options that perform a simple\n");
2013-11-22 07:53:57 -06:00
fprintf(stderr, "transformation of the input to a gate-level netlist.\n");
fprintf(stderr, "\n");
2013-11-22 07:53:57 -06:00
fprintf(stderr, " -p hierarchy -p proc -p opt -p memory -p opt -p techmap -p opt\n");
fprintf(stderr, "\n");
2013-02-28 06:13:56 -06:00
fprintf(stderr, "For more complex synthesis jobs it is recommended to use the read_* and write_*\n");
fprintf(stderr, "commands in a script file instead of specifying input and output files on the\n");
fprintf(stderr, "command line.\n");
fprintf(stderr, "\n");
fprintf(stderr, "When no commands, script files or input files are specified on the command\n");
2013-02-28 06:13:56 -06:00
fprintf(stderr, "line, yosys automatically enters the interactive command mode. Use the 'help'\n");
fprintf(stderr, "command to get information on the individual commands.\n");
fprintf(stderr, "\n");
2013-01-05 04:13:26 -06:00
exit(1);
}
}
if (log_errfile == NULL)
log_files.push_back(stderr);
2014-07-30 17:53:21 -05:00
if (print_banner) {
log("\n");
log(" /-----------------------------------------------------------------------------\\\n");
log(" | |\n");
log(" | yosys -- Yosys Open SYnthesis Suite |\n");
log(" | |\n");
log(" | Copyright (C) 2012 Clifford Wolf <clifford@clifford.at> |\n");
log(" | |\n");
log(" | Permission to use, copy, modify, and/or distribute this software for any |\n");
log(" | purpose with or without fee is hereby granted, provided that the above |\n");
log(" | copyright notice and this permission notice appear in all copies. |\n");
log(" | |\n");
log(" | THE SOFTWARE IS PROVIDED \"AS IS\" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |\n");
log(" | WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |\n");
log(" | MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |\n");
log(" | ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |\n");
log(" | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |\n");
log(" | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |\n");
log(" | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |\n");
log(" | |\n");
log(" \\-----------------------------------------------------------------------------/\n");
log("\n");
log(" %s\n", yosys_version_str);
log("\n");
}
2013-02-28 06:13:56 -06:00
yosys_setup();
if (optind == argc && passes_commands.size() == 0 && scriptfile.empty()) {
2013-01-05 04:13:26 -06:00
if (!got_output_filename)
backend_command = "";
2013-06-08 18:04:23 -05:00
shell(yosys_design);
2013-01-05 04:13:26 -06:00
}
while (optind < argc)
2014-07-21 06:28:12 -05:00
run_frontend(argv[optind++], frontend_command, yosys_design, output_filename == "-" ? &backend_command : NULL, NULL);
2013-01-05 04:13:26 -06:00
if (!scriptfile.empty()) {
if (scriptfile_tcl) {
#ifdef YOSYS_ENABLE_TCL
if (Tcl_EvalFile(yosys_get_tcl_interp(), scriptfile.c_str()) != TCL_OK)
log_error("TCL interpreter returned an error: %s\n", Tcl_GetStringResult(yosys_get_tcl_interp()));
#else
log_error("Can't exectue TCL script: this version of yosys is not built with TCL support enabled.\n");
#endif
} else
2014-07-21 06:28:12 -05:00
run_frontend(scriptfile, "script", yosys_design, output_filename == "-" ? &backend_command : NULL, NULL);
}
2013-01-05 04:13:26 -06:00
for (auto it = passes_commands.begin(); it != passes_commands.end(); it++)
2013-06-08 18:04:23 -05:00
run_pass(*it, yosys_design);
2013-01-05 04:13:26 -06:00
if (!backend_command.empty())
2013-06-08 18:04:23 -05:00
run_backend(output_filename, backend_command, yosys_design);
2013-01-05 04:13:26 -06:00
2013-06-08 18:04:23 -05:00
delete yosys_design;
yosys_design = NULL;
2014-07-25 05:22:37 -05:00
#ifdef COVER_ACTIVE
if (getenv("YOSYS_COVER_DIR") || getenv("YOSYS_COVER_FILE"))
{
char filename_buffer[4096];
FILE *f;
if (getenv("YOSYS_COVER_DIR")) {
snprintf(filename_buffer, 4096, "%s/yosys_cover_%d_XXXXXX.txt", getenv("YOSYS_COVER_DIR"), getpid());
f = fdopen(mkstemps(filename_buffer, 4), "w");
} else {
snprintf(filename_buffer, 4096, "%s", getenv("YOSYS_COVER_FILE"));
f = fopen(filename_buffer, "a+");
}
if (f == NULL)
log_error("Can't create coverage file `%s'.\n", filename_buffer);
log("<writing coverage file \"%s\">\n", filename_buffer);
2014-07-24 08:06:45 -05:00
for (auto &it : get_coverage_data())
fprintf(f, "%-60s %10d %s\n", it.second.first.c_str(), it.second.second, it.first.c_str());
fclose(f);
}
#endif
2014-08-01 11:42:10 -05:00
if (print_stats)
{
struct rusage ru_buffer;
getrusage(RUSAGE_SELF, &ru_buffer);
log("\nEnd of script. Logfile hash: xxxxxxxxxx, CPU: user %.2fs system %.2fs\n",
ru_buffer.ru_utime.tv_sec + 1e-6 * ru_buffer.ru_utime.tv_usec,
ru_buffer.ru_stime.tv_sec + 1e-6 * ru_buffer.ru_stime.tv_usec);
log("%s\nTime spent:", yosys_version_str);
int64_t total_ns = 0;
std::set<std::tuple<int64_t, int, std::string>> timedat;
for (auto &it : pass_register)
if (it.second->call_counter) {
total_ns += it.second->runtime_ns + 1;
timedat.insert(make_tuple(it.second->runtime_ns + 1, it.second->call_counter, it.first));
}
int out_count = 0;
for (auto it = timedat.rbegin(); it != timedat.rend() && out_count < 4; it++, out_count++) {
if (out_count >= 2 && (std::get<0>(*it) < 1000000000 || int(100*std::get<0>(*it) / total_ns) < 20)) {
log(", ...");
break;
}
log("%s %d%% %dx %s (%d sec)", out_count ? "," : "", int(100*std::get<0>(*it) / total_ns),
std::get<1>(*it), std::get<2>(*it).c_str(), int(std::get<0>(*it) / 1000000000));
}
log("%s\n", out_count ? "" : " no commands executed");
}
2014-07-30 18:05:27 -05:00
if (call_abort)
abort();
2013-01-05 04:13:26 -06:00
if (!history_file.empty()) {
if (history_offset > 0) {
history_truncate_file(history_file.c_str(), 100);
append_history(where_history() - history_offset, history_file.c_str());
} else
write_history(history_file.c_str());
}
clear_history();
HIST_ENTRY **hist_list = history_list();
if (hist_list != NULL)
free(hist_list);
yosys_shutdown();
for (auto mod : loaded_modules)
dlclose(mod);
2013-01-05 04:13:26 -06:00
return 0;
}