3
0
Fork 0
mirror of https://github.com/YosysHQ/yosys synced 2025-06-06 06:03:23 +00:00

Added "cover" command

This commit is contained in:
Clifford Wolf 2014-07-24 15:06:45 +02:00
parent e589289df7
commit 2f54345cff
8 changed files with 175 additions and 34 deletions

View file

@ -768,16 +768,9 @@ int main(int argc, char **argv)
log("<writing coverage file \"%s\">\n", filename_buffer);
std::map<std::string, std::pair<std::string, int>> coverage_data;
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 : get_coverage_data())
fprintf(f, "%-60s %10d %s\n", it.second.first.c_str(), it.second.second, it.first.c_str());
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);
}
#endif

View file

@ -21,7 +21,10 @@
#define LOG_H
#include "kernel/rtlil.h"
#include "kernel/register.h"
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <sys/time.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
// ---------------------------------------------------
#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 {
const char *file, *func, *id;
int line, counter;
} __attribute__ ((packed));
// 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 __stop_yosys_cover_list[];
#endif
#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)
static inline std::map<std::string, std::pair<std::string, int>> get_coverage_data()
{
std::map<std::string, std::pair<std::string, int>> coverage_data;
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
# define cover(_id) do { } while (0)
#endif

View file

@ -32,9 +32,7 @@ using namespace REGISTER_INTERN;
namespace REGISTER_INTERN
{
bool echo_mode = false;
int raw_register_count = 0;
bool raw_register_done = false;
Pass *raw_register_array[MAX_REG_COUNT];
Pass *first_queued_pass;
std::map<std::string, Frontend*> frontend_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)
{
assert(!raw_register_done);
assert(raw_register_count < MAX_REG_COUNT);
raw_register_array[raw_register_count++] = this;
next_queued_pass = first_queued_pass;
first_queued_pass = this;
call_counter = 0;
}
void Pass::run_register()
@ -58,11 +56,10 @@ void Pass::run_register()
void Pass::init_register()
{
if (raw_register_done)
done_register();
while (raw_register_count > 0)
raw_register_array[--raw_register_count]->run_register();
raw_register_done = true;
while (first_queued_pass) {
first_queued_pass->run_register();
first_queued_pass = first_queued_pass->next_queued_pass;
}
}
void Pass::done_register()
@ -70,7 +67,7 @@ void Pass::done_register()
frontend_register.clear();
pass_register.clear();
backend_register.clear();
raw_register_done = false;
assert(first_queued_pass == NULL);
}
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());
size_t orig_sel_stack_pos = design->selection_stack.size();
pass_register[args[0]]->call_counter++;
pass_register[args[0]]->execute(args, design);
while (design->selection_stack.size() > orig_sel_stack_pos)
design->selection_stack.pop_back();
@ -271,6 +269,7 @@ void Frontend::execute(std::vector<std::string> args, RTLIL::Design *design)
do {
FILE *f = NULL;
next_args.clear();
call_counter++;
execute(f, std::string(), args, design);
args = next_args;
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());
if (f != NULL) {
frontend_register[args[0]]->call_counter++;
frontend_register[args[0]]->execute(f, filename, args, design);
} else if (filename == "-") {
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);
} else {
if (!filename.empty())
@ -367,6 +368,7 @@ Backend::~Backend()
void Backend::execute(std::vector<std::string> args, RTLIL::Design *design)
{
FILE *f = NULL;
call_counter++;
execute(f, std::string(), args, design);
if (f != stdout)
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();
if (f != NULL) {
backend_register[args[0]]->call_counter++;
backend_register[args[0]]->execute(f, filename, args, design);
} else if (filename == "-") {
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);
} else {
if (!filename.empty())

View file

@ -47,9 +47,11 @@ extern std::vector<RTLIL::Design*> pushed_designs;
struct Pass
{
std::string pass_name, short_help;
int call_counter;
Pass(std::string name, std::string short_help = "** document me **");
virtual void run_register();
virtual ~Pass();
virtual void help();
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::vector<std::string> args);
Pass *next_queued_pass;
virtual void run_register();
static void init_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);
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, Frontend*> frontend_register;
extern std::map<std::string, Backend*> backend_register;