mirror of
https://github.com/YosysHQ/yosys
synced 2025-04-07 01:54:10 +00:00
Added "cover" command
This commit is contained in:
parent
e589289df7
commit
2f54345cff
2
Makefile
2
Makefile
|
@ -23,7 +23,7 @@ TARGETS = yosys yosys-config
|
||||||
|
|
||||||
all: top-all
|
all: top-all
|
||||||
|
|
||||||
CXXFLAGS = -Wall -Wextra -ggdb -I"$(shell pwd)" -MD -D_YOSYS_ -fPIC -I${DESTDIR}/include
|
CXXFLAGS = -Wall -Wextra -ggdb -I"$(shell pwd)" -MD -DYOSYS_SRC='"$(shell pwd)"' -D_YOSYS_ -fPIC -I${DESTDIR}/include
|
||||||
LDFLAGS = -L${DESTDIR}/lib
|
LDFLAGS = -L${DESTDIR}/lib
|
||||||
LDLIBS = -lstdc++ -lreadline -lm -ldl
|
LDLIBS = -lstdc++ -lreadline -lm -ldl
|
||||||
QMAKE = qmake-qt4
|
QMAKE = qmake-qt4
|
||||||
|
|
|
@ -768,16 +768,9 @@ int main(int argc, char **argv)
|
||||||
|
|
||||||
log("<writing coverage file \"%s\">\n", filename_buffer);
|
log("<writing coverage file \"%s\">\n", filename_buffer);
|
||||||
|
|
||||||
std::map<std::string, std::pair<std::string, int>> coverage_data;
|
for (auto &it : get_coverage_data())
|
||||||
for (CoverData *p = __start_yosys_cover_list; p != __stop_yosys_cover_list; p++) {
|
fprintf(f, "%-60s %10d %s\n", it.second.first.c_str(), it.second.second, it.first.c_str());
|
||||||
if (coverage_data.count(p->id))
|
|
||||||
log("WARNING: found duplicate coverage id \"%s\".\n", p->id);
|
|
||||||
coverage_data[p->id].first = stringf("%s:%d:%s", p->file, p->line, p->func);
|
|
||||||
coverage_data[p->id].second += p->counter;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (auto &it : coverage_data)
|
|
||||||
fprintf(f, "%-40s %10d %s\n", it.second.first.c_str(), it.second.second, it.first.c_str());
|
|
||||||
fclose(f);
|
fclose(f);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
41
kernel/log.h
41
kernel/log.h
|
@ -21,7 +21,10 @@
|
||||||
#define LOG_H
|
#define LOG_H
|
||||||
|
|
||||||
#include "kernel/rtlil.h"
|
#include "kernel/rtlil.h"
|
||||||
|
#include "kernel/register.h"
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
#include <sys/resource.h>
|
#include <sys/resource.h>
|
||||||
|
@ -68,22 +71,46 @@ void log_cell(RTLIL::Cell *cell, std::string indent = "");
|
||||||
// This is the magic behind the code coverage counters
|
// This is the magic behind the code coverage counters
|
||||||
// ---------------------------------------------------
|
// ---------------------------------------------------
|
||||||
|
|
||||||
|
#ifndef NDEBUG
|
||||||
|
|
||||||
|
#define cover(_id) do { \
|
||||||
|
static CoverData __d __attribute__((section("yosys_cover_list"), aligned(1))) = { __FILE__, __FUNCTION__, _id, __LINE__, 0 }; \
|
||||||
|
__d.counter++; \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
struct CoverData {
|
struct CoverData {
|
||||||
const char *file, *func, *id;
|
const char *file, *func, *id;
|
||||||
int line, counter;
|
int line, counter;
|
||||||
} __attribute__ ((packed));
|
} __attribute__ ((packed));
|
||||||
|
|
||||||
// this two symbols are created by the linker for the "yosys_cover_list" ELF section
|
// this two symbols are created by the linker for the "yosys_cover_list" ELF section
|
||||||
#ifndef NDEBUG
|
|
||||||
extern "C" struct CoverData __start_yosys_cover_list[];
|
extern "C" struct CoverData __start_yosys_cover_list[];
|
||||||
extern "C" struct CoverData __stop_yosys_cover_list[];
|
extern "C" struct CoverData __stop_yosys_cover_list[];
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifndef NDEBUG
|
static inline std::map<std::string, std::pair<std::string, int>> get_coverage_data()
|
||||||
# define cover(_id) do { \
|
{
|
||||||
static CoverData __d __attribute__((section("yosys_cover_list"), aligned(1))) = { __FILE__, __FUNCTION__, _id, __LINE__, 0 }; \
|
std::map<std::string, std::pair<std::string, int>> coverage_data;
|
||||||
__d.counter++; \
|
|
||||||
} while (0)
|
for (auto &it : REGISTER_INTERN::pass_register) {
|
||||||
|
std::string key = stringf("passes.%s", it.first.c_str());
|
||||||
|
coverage_data[key].first = stringf("%s:%d:%s", __FILE__, __LINE__, __FUNCTION__);
|
||||||
|
coverage_data[key].second += it.second->call_counter;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (CoverData *p = __start_yosys_cover_list; p != __stop_yosys_cover_list; p++) {
|
||||||
|
if (coverage_data.count(p->id))
|
||||||
|
log("WARNING: found duplicate coverage id \"%s\".\n", p->id);
|
||||||
|
coverage_data[p->id].first = stringf("%s:%d:%s", p->file, p->line, p->func);
|
||||||
|
coverage_data[p->id].second += p->counter;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto &it : coverage_data)
|
||||||
|
if (!it.second.first.compare(0, strlen(YOSYS_SRC "/"), YOSYS_SRC "/"))
|
||||||
|
it.second.first = it.second.first.substr(strlen(YOSYS_SRC "/"));
|
||||||
|
|
||||||
|
return coverage_data;
|
||||||
|
}
|
||||||
|
|
||||||
#else
|
#else
|
||||||
# define cover(_id) do { } while (0)
|
# define cover(_id) do { } while (0)
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -32,9 +32,7 @@ using namespace REGISTER_INTERN;
|
||||||
namespace REGISTER_INTERN
|
namespace REGISTER_INTERN
|
||||||
{
|
{
|
||||||
bool echo_mode = false;
|
bool echo_mode = false;
|
||||||
int raw_register_count = 0;
|
Pass *first_queued_pass;
|
||||||
bool raw_register_done = false;
|
|
||||||
Pass *raw_register_array[MAX_REG_COUNT];
|
|
||||||
|
|
||||||
std::map<std::string, Frontend*> frontend_register;
|
std::map<std::string, Frontend*> frontend_register;
|
||||||
std::map<std::string, Pass*> pass_register;
|
std::map<std::string, Pass*> pass_register;
|
||||||
|
@ -45,9 +43,9 @@ std::vector<std::string> Frontend::next_args;
|
||||||
|
|
||||||
Pass::Pass(std::string name, std::string short_help) : pass_name(name), short_help(short_help)
|
Pass::Pass(std::string name, std::string short_help) : pass_name(name), short_help(short_help)
|
||||||
{
|
{
|
||||||
assert(!raw_register_done);
|
next_queued_pass = first_queued_pass;
|
||||||
assert(raw_register_count < MAX_REG_COUNT);
|
first_queued_pass = this;
|
||||||
raw_register_array[raw_register_count++] = this;
|
call_counter = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Pass::run_register()
|
void Pass::run_register()
|
||||||
|
@ -58,11 +56,10 @@ void Pass::run_register()
|
||||||
|
|
||||||
void Pass::init_register()
|
void Pass::init_register()
|
||||||
{
|
{
|
||||||
if (raw_register_done)
|
while (first_queued_pass) {
|
||||||
done_register();
|
first_queued_pass->run_register();
|
||||||
while (raw_register_count > 0)
|
first_queued_pass = first_queued_pass->next_queued_pass;
|
||||||
raw_register_array[--raw_register_count]->run_register();
|
}
|
||||||
raw_register_done = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Pass::done_register()
|
void Pass::done_register()
|
||||||
|
@ -70,7 +67,7 @@ void Pass::done_register()
|
||||||
frontend_register.clear();
|
frontend_register.clear();
|
||||||
pass_register.clear();
|
pass_register.clear();
|
||||||
backend_register.clear();
|
backend_register.clear();
|
||||||
raw_register_done = false;
|
assert(first_queued_pass == NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
Pass::~Pass()
|
Pass::~Pass()
|
||||||
|
@ -191,6 +188,7 @@ void Pass::call(RTLIL::Design *design, std::vector<std::string> args)
|
||||||
log_cmd_error("No such command: %s (type 'help' for a command overview)\n", args[0].c_str());
|
log_cmd_error("No such command: %s (type 'help' for a command overview)\n", args[0].c_str());
|
||||||
|
|
||||||
size_t orig_sel_stack_pos = design->selection_stack.size();
|
size_t orig_sel_stack_pos = design->selection_stack.size();
|
||||||
|
pass_register[args[0]]->call_counter++;
|
||||||
pass_register[args[0]]->execute(args, design);
|
pass_register[args[0]]->execute(args, design);
|
||||||
while (design->selection_stack.size() > orig_sel_stack_pos)
|
while (design->selection_stack.size() > orig_sel_stack_pos)
|
||||||
design->selection_stack.pop_back();
|
design->selection_stack.pop_back();
|
||||||
|
@ -271,6 +269,7 @@ void Frontend::execute(std::vector<std::string> args, RTLIL::Design *design)
|
||||||
do {
|
do {
|
||||||
FILE *f = NULL;
|
FILE *f = NULL;
|
||||||
next_args.clear();
|
next_args.clear();
|
||||||
|
call_counter++;
|
||||||
execute(f, std::string(), args, design);
|
execute(f, std::string(), args, design);
|
||||||
args = next_args;
|
args = next_args;
|
||||||
fclose(f);
|
fclose(f);
|
||||||
|
@ -334,9 +333,11 @@ void Frontend::frontend_call(RTLIL::Design *design, FILE *f, std::string filenam
|
||||||
log_cmd_error("No such frontend: %s\n", args[0].c_str());
|
log_cmd_error("No such frontend: %s\n", args[0].c_str());
|
||||||
|
|
||||||
if (f != NULL) {
|
if (f != NULL) {
|
||||||
|
frontend_register[args[0]]->call_counter++;
|
||||||
frontend_register[args[0]]->execute(f, filename, args, design);
|
frontend_register[args[0]]->execute(f, filename, args, design);
|
||||||
} else if (filename == "-") {
|
} else if (filename == "-") {
|
||||||
FILE *f_stdin = stdin; // workaround for OpenBSD 'stdin' implementation
|
FILE *f_stdin = stdin; // workaround for OpenBSD 'stdin' implementation
|
||||||
|
frontend_register[args[0]]->call_counter++;
|
||||||
frontend_register[args[0]]->execute(f_stdin, "<stdin>", args, design);
|
frontend_register[args[0]]->execute(f_stdin, "<stdin>", args, design);
|
||||||
} else {
|
} else {
|
||||||
if (!filename.empty())
|
if (!filename.empty())
|
||||||
|
@ -367,6 +368,7 @@ Backend::~Backend()
|
||||||
void Backend::execute(std::vector<std::string> args, RTLIL::Design *design)
|
void Backend::execute(std::vector<std::string> args, RTLIL::Design *design)
|
||||||
{
|
{
|
||||||
FILE *f = NULL;
|
FILE *f = NULL;
|
||||||
|
call_counter++;
|
||||||
execute(f, std::string(), args, design);
|
execute(f, std::string(), args, design);
|
||||||
if (f != stdout)
|
if (f != stdout)
|
||||||
fclose(f);
|
fclose(f);
|
||||||
|
@ -428,9 +430,11 @@ void Backend::backend_call(RTLIL::Design *design, FILE *f, std::string filename,
|
||||||
size_t orig_sel_stack_pos = design->selection_stack.size();
|
size_t orig_sel_stack_pos = design->selection_stack.size();
|
||||||
|
|
||||||
if (f != NULL) {
|
if (f != NULL) {
|
||||||
|
backend_register[args[0]]->call_counter++;
|
||||||
backend_register[args[0]]->execute(f, filename, args, design);
|
backend_register[args[0]]->execute(f, filename, args, design);
|
||||||
} else if (filename == "-") {
|
} else if (filename == "-") {
|
||||||
FILE *f_stdout = stdout; // workaround for OpenBSD 'stdout' implementation
|
FILE *f_stdout = stdout; // workaround for OpenBSD 'stdout' implementation
|
||||||
|
backend_register[args[0]]->call_counter++;
|
||||||
backend_register[args[0]]->execute(f_stdout, "<stdout>", args, design);
|
backend_register[args[0]]->execute(f_stdout, "<stdout>", args, design);
|
||||||
} else {
|
} else {
|
||||||
if (!filename.empty())
|
if (!filename.empty())
|
||||||
|
|
|
@ -47,9 +47,11 @@ extern std::vector<RTLIL::Design*> pushed_designs;
|
||||||
struct Pass
|
struct Pass
|
||||||
{
|
{
|
||||||
std::string pass_name, short_help;
|
std::string pass_name, short_help;
|
||||||
|
int call_counter;
|
||||||
|
|
||||||
Pass(std::string name, std::string short_help = "** document me **");
|
Pass(std::string name, std::string short_help = "** document me **");
|
||||||
virtual void run_register();
|
|
||||||
virtual ~Pass();
|
virtual ~Pass();
|
||||||
|
|
||||||
virtual void help();
|
virtual void help();
|
||||||
virtual void execute(std::vector<std::string> args, RTLIL::Design *design) = 0;
|
virtual void execute(std::vector<std::string> args, RTLIL::Design *design) = 0;
|
||||||
|
|
||||||
|
@ -66,6 +68,8 @@ struct Pass
|
||||||
static void call_on_module(RTLIL::Design *design, RTLIL::Module *module, std::string command);
|
static void call_on_module(RTLIL::Design *design, RTLIL::Module *module, std::string command);
|
||||||
static void call_on_module(RTLIL::Design *design, RTLIL::Module *module, std::vector<std::string> args);
|
static void call_on_module(RTLIL::Design *design, RTLIL::Module *module, std::vector<std::string> args);
|
||||||
|
|
||||||
|
Pass *next_queued_pass;
|
||||||
|
virtual void run_register();
|
||||||
static void init_register();
|
static void init_register();
|
||||||
static void done_register();
|
static void done_register();
|
||||||
};
|
};
|
||||||
|
@ -105,9 +109,6 @@ struct Backend : Pass
|
||||||
extern void handle_extra_select_args(Pass *pass, std::vector<std::string> args, size_t argidx, size_t args_size, RTLIL::Design *design);
|
extern void handle_extra_select_args(Pass *pass, std::vector<std::string> args, size_t argidx, size_t args_size, RTLIL::Design *design);
|
||||||
|
|
||||||
namespace REGISTER_INTERN {
|
namespace REGISTER_INTERN {
|
||||||
extern int raw_register_count;
|
|
||||||
extern bool raw_register_done;
|
|
||||||
extern Pass *raw_register_array[];
|
|
||||||
extern std::map<std::string, Pass*> pass_register;
|
extern std::map<std::string, Pass*> pass_register;
|
||||||
extern std::map<std::string, Frontend*> frontend_register;
|
extern std::map<std::string, Frontend*> frontend_register;
|
||||||
extern std::map<std::string, Backend*> backend_register;
|
extern std::map<std::string, Backend*> backend_register;
|
||||||
|
|
|
@ -17,4 +17,5 @@ OBJS += passes/cmds/scc.o
|
||||||
OBJS += passes/cmds/log.o
|
OBJS += passes/cmds/log.o
|
||||||
OBJS += passes/cmds/tee.o
|
OBJS += passes/cmds/tee.o
|
||||||
OBJS += passes/cmds/connwrappers.o
|
OBJS += passes/cmds/connwrappers.o
|
||||||
|
OBJS += passes/cmds/cover.o
|
||||||
|
|
||||||
|
|
115
passes/cmds/cover.cc
Normal file
115
passes/cmds/cover.cc
Normal file
|
@ -0,0 +1,115 @@
|
||||||
|
/*
|
||||||
|
* yosys -- Yosys Open SYnthesis Suite
|
||||||
|
*
|
||||||
|
* Copyright (C) 2014 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/register.h"
|
||||||
|
#include "kernel/rtlil.h"
|
||||||
|
#include "kernel/log.h"
|
||||||
|
|
||||||
|
struct CoverPass : public Pass {
|
||||||
|
CoverPass() : Pass("cover", "print code coverage counters") { }
|
||||||
|
virtual void help()
|
||||||
|
{
|
||||||
|
// |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
|
||||||
|
log("\n");
|
||||||
|
log(" cover [-q] [-o logfile|-a logfile]\n");
|
||||||
|
log("\n");
|
||||||
|
log("Print the code coverage counters collected using the cover() macro in the Yosys\n");
|
||||||
|
log("C++ code. This is useful to figure out what parts of Yosys are utilized by a\n");
|
||||||
|
log("test bench.\n");
|
||||||
|
log("\n");
|
||||||
|
log(" -q\n");
|
||||||
|
log(" Do not print output to the normal destination (console and/or log file)\n");
|
||||||
|
log("\n");
|
||||||
|
log(" -o logfile\n");
|
||||||
|
log(" Write output to this file, truncate if exists.\n");
|
||||||
|
log("\n");
|
||||||
|
log(" -a logfile\n");
|
||||||
|
log(" Write output to this file, append if exists.\n");
|
||||||
|
log("\n");
|
||||||
|
log("\n");
|
||||||
|
log("It is also possible to instruct Yosys to print the coverage counters to a file\n");
|
||||||
|
log("using environment variables.\n");
|
||||||
|
log("\n");
|
||||||
|
log(" YOSYS_COVER_DIR=\"{dir-name}\" yosys {args}n");
|
||||||
|
log("\n");
|
||||||
|
log(" This will create a file (with an auto-generated name) in this\n");
|
||||||
|
log(" directory and wire the coverage counters to it.\n");
|
||||||
|
log("\n");
|
||||||
|
log(" YOSYS_COVER_FILE=\"{file-name}\" yosys {args}n");
|
||||||
|
log("\n");
|
||||||
|
log(" This will append the coverage counters to the specified file.\n");
|
||||||
|
log("\n");
|
||||||
|
log("\n");
|
||||||
|
log("Hint: Use the following AWK command to consolidate Yosys coverage files:\n");
|
||||||
|
log("\n");
|
||||||
|
log(" gawk '{ p[$3] = $1; c[$3] += $2; } END { for (i in p)\n");
|
||||||
|
log(" printf \"%%-60s %%10d %%s\\n\", p[i], c[i], i; }' {files} | sort -k3\n");
|
||||||
|
log("\n");
|
||||||
|
}
|
||||||
|
virtual void execute(std::vector<std::string> args, RTLIL::Design *design)
|
||||||
|
{
|
||||||
|
std::vector<FILE*> out_files;
|
||||||
|
bool do_log = true;
|
||||||
|
|
||||||
|
size_t argidx;
|
||||||
|
for (argidx = 1; argidx < args.size(); argidx++)
|
||||||
|
{
|
||||||
|
if (args[argidx] == "-q") {
|
||||||
|
do_log = false;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if ((args[argidx] == "-o" || args[argidx] == "-a") && argidx+1 < args.size()) {
|
||||||
|
const char *open_mode = args[argidx] == "-o" ? "w" : "a+";
|
||||||
|
FILE *f = fopen(args[++argidx].c_str(), open_mode);
|
||||||
|
if (f == NULL) {
|
||||||
|
for (auto f : out_files)
|
||||||
|
fclose(f);
|
||||||
|
log_cmd_error("Can't create file %s.\n", args[argidx].c_str());
|
||||||
|
}
|
||||||
|
out_files.push_back(f);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
extra_args(args, argidx, design);
|
||||||
|
|
||||||
|
if (do_log) {
|
||||||
|
log_header("Printing code coverage counters.\n");
|
||||||
|
log("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifndef NDEBUG
|
||||||
|
for (auto &it : get_coverage_data()) {
|
||||||
|
for (auto f : out_files)
|
||||||
|
fprintf(f, "%-60s %10d %s\n", it.second.first.c_str(), it.second.second, it.first.c_str());
|
||||||
|
if (do_log)
|
||||||
|
log("%-60s %10d %s\n", it.second.first.c_str(), it.second.second, it.first.c_str());
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
for (auto f : out_files)
|
||||||
|
fclose(f);
|
||||||
|
|
||||||
|
log_cmd_error("Coverage counters are only available in debug builds of Yosys.");
|
||||||
|
#endif
|
||||||
|
|
||||||
|
for (auto f : out_files)
|
||||||
|
fclose(f);
|
||||||
|
}
|
||||||
|
} CoverPass;
|
||||||
|
|
|
@ -56,7 +56,7 @@ struct TeePass : public Pass {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if ((args[argidx] == "-o" || args[argidx] == "-a") && argidx+1 < args.size()) {
|
if ((args[argidx] == "-o" || args[argidx] == "-a") && argidx+1 < args.size()) {
|
||||||
const char *open_mode = args[argidx] == "-o" ? "wt" : "at";
|
const char *open_mode = args[argidx] == "-o" ? "w" : "a+";
|
||||||
FILE *f = fopen(args[++argidx].c_str(), open_mode);
|
FILE *f = fopen(args[++argidx].c_str(), open_mode);
|
||||||
if (f == NULL) {
|
if (f == NULL) {
|
||||||
for (auto cf : files_to_close)
|
for (auto cf : files_to_close)
|
||||||
|
|
Loading…
Reference in a new issue