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"
27 // In GCC 4.8 std::regex is not working correctlty, in order to make features
28 // using regular expressions to work replacement regex library is used
29 #if defined(__GNUC__) && !defined( __clang__) && ( __GNUC__ == 4 && __GNUC_MINOR__ <= 8)
30 #include <boost/xpressive/xpressive.hpp>
31 #define YS_REGEX_TYPE boost::xpressive::sregex
32 #define YS_REGEX_NS boost::xpressive
33 #define YS_REGEX_COMPILE(param) boost::xpressive::sregex::compile(param, \
34 boost::xpressive::regex_constants::nosubs | \
35 boost::xpressive::regex_constants::optimize)
38 #define YS_REGEX_TYPE std::regex
39 #define YS_REGEX_NS std
40 #define YS_REGEX_COMPILE(param) std::regex(param, \
41 std::regex_constants::nosubs | \
42 std::regex_constants::optimize | \
43 std::regex_constants::egrep)
47 # include <sys/time.h>
48 # include <sys/resource.h>
52 // At least this is not in MSVC++ 2013.
53 # define __PRETTY_FUNCTION__ __FUNCTION__
56 // from libs/sha1/sha1.h
61 #define S__LINE__sub2(x) #x
62 #define S__LINE__sub1(x) S__LINE__sub2(x)
63 #define S__LINE__ S__LINE__sub1(__LINE__)
65 struct log_cmd_error_exception
{ };
67 extern std::vector
<FILE*> log_files
;
68 extern std::vector
<std::ostream
*> log_streams
;
69 extern std::map
<std::string
, std::set
<std::string
>> log_hdump
;
70 extern std::vector
<YS_REGEX_TYPE
> log_warn_regexes
, log_nowarn_regexes
, log_werror_regexes
;
71 extern std::set
<std::string
> log_warnings
, log_experimentals
, log_experimentals_ignored
;
72 extern int log_warnings_count
;
73 extern int log_warnings_count_noexpect
;
74 extern bool log_expect_no_warnings
;
75 extern bool log_hdump_all
;
76 extern FILE *log_errfile
;
77 extern SHA1
*log_hasher
;
80 extern bool log_error_stderr
;
81 extern bool log_cmd_error_throw
;
82 extern bool log_quiet_warnings
;
83 extern int log_verbose_level
;
84 extern string log_last_error
;
85 extern void (*log_error_atexit
)();
87 extern int log_make_debug
;
88 extern int log_force_debug
;
89 extern int log_debug_suppressed
;
91 void logv(const char *format
, va_list ap
);
92 void logv_header(RTLIL::Design
*design
, const char *format
, va_list ap
);
93 void logv_warning(const char *format
, va_list ap
);
94 void logv_warning_noprefix(const char *format
, va_list ap
);
95 YS_NORETURN
void logv_error(const char *format
, va_list ap
) YS_ATTRIBUTE(noreturn
);
97 void log(const char *format
, ...) YS_ATTRIBUTE(format(printf
, 1, 2));
98 void log_header(RTLIL::Design
*design
, const char *format
, ...) YS_ATTRIBUTE(format(printf
, 2, 3));
99 void log_warning(const char *format
, ...) YS_ATTRIBUTE(format(printf
, 1, 2));
100 void log_experimental(const char *format
, ...) YS_ATTRIBUTE(format(printf
, 1, 2));
102 // Log with filename to report a problem in a source file.
103 void log_file_warning(const std::string
&filename
, int lineno
, const char *format
, ...) YS_ATTRIBUTE(format(printf
, 3, 4));
104 void log_file_info(const std::string
&filename
, int lineno
, const char *format
, ...) YS_ATTRIBUTE(format(printf
, 3, 4));
106 void log_warning_noprefix(const char *format
, ...) YS_ATTRIBUTE(format(printf
, 1, 2));
107 YS_NORETURN
void log_error(const char *format
, ...) YS_ATTRIBUTE(format(printf
, 1, 2), noreturn
);
108 void log_file_error(const string
&filename
, int lineno
, const char *format
, ...) YS_ATTRIBUTE(format(printf
, 3, 4), noreturn
);
109 YS_NORETURN
void log_cmd_error(const char *format
, ...) YS_ATTRIBUTE(format(printf
, 1, 2), noreturn
);
112 static inline bool ys_debug(int n
= 0) { if (log_force_debug
) return true; log_debug_suppressed
+= n
; return false; }
113 # define log_debug(...) do { if (ys_debug(1)) log(__VA_ARGS__); } while (0)
115 static inline bool ys_debug(int = 0) { return false; }
116 # define log_debug(_fmt, ...) do { } while (0)
119 static inline void log_suppressed() {
120 if (log_debug_suppressed
&& !log_make_debug
) {
121 log("<suppressed ~%d debug messages>\n", log_debug_suppressed
);
122 log_debug_suppressed
= 0;
126 struct LogMakeDebugHdl
{
128 LogMakeDebugHdl(bool start_on
= false) {
154 void log_backtrace(const char *prefix
, int levels
);
155 void log_reset_stack();
158 struct LogExpectedItem
160 LogExpectedItem(std::string pattern
, int expected
) :
161 expected_count(expected
),
172 extern std::vector
<std::pair
<YS_REGEX_TYPE
,LogExpectedItem
>> log_expect_log
, log_expect_warning
, log_expect_error
;
173 void log_check_expected();
175 const char *log_signal(const RTLIL::SigSpec
&sig
, bool autoint
= true);
176 const char *log_const(const RTLIL::Const
&value
, bool autoint
= true);
177 const char *log_id(RTLIL::IdString id
);
179 template<typename T
> static inline const char *log_id(T
*obj
, const char *nullstr
= nullptr) {
180 if (nullstr
&& obj
== nullptr)
182 return log_id(obj
->name
);
185 void log_module(RTLIL::Module
*module
, std::string indent
= "");
186 void log_cell(RTLIL::Cell
*cell
, std::string indent
= "");
187 void log_wire(RTLIL::Wire
*wire
, std::string indent
= "");
190 static inline void log_assert_worker(bool cond
, const char *expr
, const char *file
, int line
) {
191 if (!cond
) log_error("Assert `%s' failed in %s:%d.\n", expr
, file
, line
);
193 # define log_assert(_assert_expr_) YOSYS_NAMESPACE_PREFIX log_assert_worker(_assert_expr_, #_assert_expr_, __FILE__, __LINE__)
195 # define log_assert(_assert_expr_)
198 #define log_abort() YOSYS_NAMESPACE_PREFIX log_error("Abort in %s:%d.\n", __FILE__, __LINE__)
199 #define log_ping() YOSYS_NAMESPACE_PREFIX log("-- %s:%d %s --\n", __FILE__, __LINE__, __PRETTY_FUNCTION__)
202 // ---------------------------------------------------
203 // This is the magic behind the code coverage counters
204 // ---------------------------------------------------
206 #if defined(YOSYS_ENABLE_COVER) && (defined(__linux__) || defined(__FreeBSD__))
208 #define cover(_id) do { \
209 static CoverData __d __attribute__((section("yosys_cover_list"), aligned(1), used)) = { __FILE__, __FUNCTION__, _id, __LINE__, 0 }; \
214 const char *file
, *func
, *id
;
216 } YS_ATTRIBUTE(packed
);
218 // this two symbols are created by the linker for the "yosys_cover_list" ELF section
219 extern "C" struct CoverData __start_yosys_cover_list
[];
220 extern "C" struct CoverData __stop_yosys_cover_list
[];
222 extern dict
<std::string
, std::pair
<std::string
, int>> extra_coverage_data
;
224 void cover_extra(std::string parent
, std::string id
, bool increment
= true);
225 dict
<std::string
, std::pair
<std::string
, int>> get_coverage_data();
227 #define cover_list(_id, ...) do { cover(_id); \
228 std::string r = cover_list_worker(_id, __VA_ARGS__); \
229 log_assert(r.empty()); \
232 static inline std::string
cover_list_worker(std::string
, std::string last
) {
236 template<typename
... T
>
237 std::string
cover_list_worker(std::string prefix
, std::string first
, T
... rest
) {
238 std::string selected
= cover_list_worker(prefix
, rest
...);
239 cover_extra(prefix
, prefix
+ "." + first
, first
== selected
);
240 return first
== selected
? "" : selected
;
244 # define cover(...) do { } while (0)
245 # define cover_list(...) do { } while (0)
249 // ------------------------------------------------------------
250 // everything below this line are utilities for troubleshooting
251 // ------------------------------------------------------------
253 // simple timer for performance measurements
254 // toggle the '#if 1' to get a baseline for the performance penalty added by the measurement
255 struct PerformanceTimer
264 static int64_t query() {
267 # elif defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0)
269 clock_gettime(CLOCK_PROCESS_CPUTIME_ID
, &ts
);
270 return int64_t(ts
.tv_sec
)*1000000000 + ts
.tv_nsec
;
271 # elif defined(RUSAGE_SELF)
272 struct rusage rusage
;
274 if (getrusage(RUSAGE_SELF
, &rusage
) == -1) {
275 log_cmd_error("getrusage failed!\n");
278 t
= 1000000000ULL * (int64_t) rusage
.ru_utime
.tv_sec
+ (int64_t) rusage
.ru_utime
.tv_usec
* 1000ULL;
279 t
+= 1000000000ULL * (int64_t) rusage
.ru_stime
.tv_sec
+ (int64_t) rusage
.ru_stime
.tv_usec
* 1000ULL;
282 # error "Don't know how to measure per-process CPU time. Need alternative method (times()/clocks()/gettimeofday()?)."
299 return total_ns
* 1e-9f
;
302 static int64_t query() { return 0; }
306 float sec() const { return 0; }
310 // simple API for quickly dumping values when debugging
312 static inline void log_dump_val_worker(short v
) { log("%d", v
); }
313 static inline void log_dump_val_worker(unsigned short v
) { log("%u", v
); }
314 static inline void log_dump_val_worker(int v
) { log("%d", v
); }
315 static inline void log_dump_val_worker(unsigned int v
) { log("%u", v
); }
316 static inline void log_dump_val_worker(long int v
) { log("%ld", v
); }
317 static inline void log_dump_val_worker(unsigned long int v
) { log("%lu", v
); }
319 static inline void log_dump_val_worker(long long int v
) { log("%lld", v
); }
320 static inline void log_dump_val_worker(unsigned long long int v
) { log("%lld", v
); }
322 static inline void log_dump_val_worker(char c
) { log(c
>= 32 && c
< 127 ? "'%c'" : "'\\x%02x'", c
); }
323 static inline void log_dump_val_worker(unsigned char c
) { log(c
>= 32 && c
< 127 ? "'%c'" : "'\\x%02x'", c
); }
324 static inline void log_dump_val_worker(bool v
) { log("%s", v
? "true" : "false"); }
325 static inline void log_dump_val_worker(double v
) { log("%f", v
); }
326 static inline void log_dump_val_worker(char *v
) { log("%s", v
); }
327 static inline void log_dump_val_worker(const char *v
) { log("%s", v
); }
328 static inline void log_dump_val_worker(std::string v
) { log("%s", v
.c_str()); }
329 static inline void log_dump_val_worker(PerformanceTimer p
) { log("%f seconds", p
.sec()); }
330 static inline void log_dump_args_worker(const char *p
YS_ATTRIBUTE(unused
)) { log_assert(*p
== 0); }
331 void log_dump_val_worker(RTLIL::IdString v
);
332 void log_dump_val_worker(RTLIL::SigSpec v
);
333 void log_dump_val_worker(RTLIL::State v
);
335 template<typename K
, typename T
, typename OPS
>
336 static inline void log_dump_val_worker(dict
<K
, T
, OPS
> &v
) {
340 log(first
? " " : ", ");
341 log_dump_val_worker(it
.first
);
343 log_dump_val_worker(it
.second
);
349 template<typename K
, typename OPS
>
350 static inline void log_dump_val_worker(pool
<K
, OPS
> &v
) {
354 log(first
? " " : ", ");
355 log_dump_val_worker(it
);
362 static inline void log_dump_val_worker(T
*ptr
) { log("%p", ptr
); }
364 template<typename T
, typename
... Args
>
365 void log_dump_args_worker(const char *p
, T first
, Args
... args
)
367 int next_p_state
= 0;
368 const char *next_p
= p
;
369 while (*next_p
&& (next_p_state
!= 0 || *next_p
!= ',')) {
373 while (*next_p
== '\\' && *(next_p
+ 1))
375 } while (*next_p
&& *next_p
!= '"');
376 if (*next_p
== '\'') {
383 if (*next_p
== '(' || *next_p
== '[' || *next_p
== '{')
385 if ((*next_p
== ')' || *next_p
== ']' || *next_p
== '}') && next_p_state
> 0)
389 log("\n\t%.*s => ", int(next_p
- p
), p
);
392 while (*next_p
== ' ' || *next_p
== '\t' || *next_p
== '\r' || *next_p
== '\n')
394 log_dump_val_worker(first
);
395 log_dump_args_worker(next_p
, args
...);
398 #define log_dump(...) do { \
399 log("DEBUG DUMP IN %s AT %s:%d:", __PRETTY_FUNCTION__, __FILE__, __LINE__); \
400 log_dump_args_worker(#__VA_ARGS__, __VA_ARGS__); \