Added "cover" command
authorClifford Wolf <clifford@clifford.at>
Thu, 24 Jul 2014 13:06:45 +0000 (15:06 +0200)
committerClifford Wolf <clifford@clifford.at>
Thu, 24 Jul 2014 14:14:19 +0000 (16:14 +0200)
Makefile
kernel/driver.cc
kernel/log.h
kernel/register.cc
kernel/register.h
passes/cmds/Makefile.inc
passes/cmds/cover.cc [new file with mode: 0644]
passes/cmds/tee.cc

index 10bd9407ef41f7cd0f0597bc84ce25056748dd1b..272161ef4ce2c225977375bdc473b8390ff4ca54 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -23,7 +23,7 @@ TARGETS = yosys yosys-config
 
 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
 LDLIBS = -lstdc++ -lreadline -lm -ldl
 QMAKE = qmake-qt4
index 64165737eac6338a9859e77f6a21379cdaf849a2..a4556fb1b162305dbe00a3f1419f1e4f6023fb55 100644 (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
index 804175909c3e4bd266814c741a4593625a6c86f2..0e8d0827200c1de8388719e18495b6480202d4f2 100644 (file)
 #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
index 84051948fa6270ea91e362ca7b12c37b4fe9c460..e7ad7ef0504c3a4c14b97d5a1ccf42cb1c4ab518 100644 (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())
index b07c461778d04a61e6cd42e894e9717637d594c7..fd073cbe7deb7473f28a19290e26f8c63d18fd69 100644 (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;
index 3a4b2da714015c7ea3a6b41ee60e90a844124c08..6ae4495a8153ee896832e957ae6a236027f6cc40 100644 (file)
@@ -17,4 +17,5 @@ OBJS += passes/cmds/scc.o
 OBJS += passes/cmds/log.o
 OBJS += passes/cmds/tee.o
 OBJS += passes/cmds/connwrappers.o
+OBJS += passes/cmds/cover.o
 
diff --git a/passes/cmds/cover.cc b/passes/cmds/cover.cc
new file mode 100644 (file)
index 0000000..ebbdc7c
--- /dev/null
@@ -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;
+
index dd95919544711fe37bb3286b0b4e9eca642bb374..e2f69e5990b14085211bfe4055d5178df81da6be 100644 (file)
@@ -56,7 +56,7 @@ struct TeePass : public Pass {
                                continue;
                        }
                        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);
                                if (f == NULL) {
                                        for (auto cf : files_to_close)