Merge pull request #1576 from YosysHQ/eddie/opt_merge_init
[yosys.git] / kernel / log.h
1 /*
2 * yosys -- Yosys Open SYnthesis Suite
3 *
4 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
5 *
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.
9 *
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.
17 *
18 */
19
20 #include "kernel/yosys.h"
21
22 #ifndef LOG_H
23 #define LOG_H
24
25 #include <time.h>
26 #include <regex>
27
28 #ifndef _WIN32
29 # include <sys/time.h>
30 # include <sys/resource.h>
31 #endif
32
33 #if defined(_MSC_VER)
34 // At least this is not in MSVC++ 2013.
35 # define __PRETTY_FUNCTION__ __FUNCTION__
36 #endif
37
38 // from libs/sha1/sha1.h
39 class SHA1;
40
41 YOSYS_NAMESPACE_BEGIN
42
43 #define S__LINE__sub2(x) #x
44 #define S__LINE__sub1(x) S__LINE__sub2(x)
45 #define S__LINE__ S__LINE__sub1(__LINE__)
46
47 struct log_cmd_error_exception { };
48
49 extern std::vector<FILE*> log_files;
50 extern std::vector<std::ostream*> log_streams;
51 extern std::map<std::string, std::set<std::string>> log_hdump;
52 extern std::vector<std::regex> log_warn_regexes, log_nowarn_regexes, log_werror_regexes;
53 extern std::set<std::string> log_warnings, log_experimentals, log_experimentals_ignored;
54 extern int log_warnings_count;
55 extern bool log_hdump_all;
56 extern FILE *log_errfile;
57 extern SHA1 *log_hasher;
58
59 extern bool log_time;
60 extern bool log_error_stderr;
61 extern bool log_cmd_error_throw;
62 extern bool log_quiet_warnings;
63 extern int log_verbose_level;
64 extern string log_last_error;
65 extern void (*log_error_atexit)();
66
67 extern int log_make_debug;
68 extern int log_force_debug;
69 extern int log_debug_suppressed;
70
71 void logv(const char *format, va_list ap);
72 void logv_header(RTLIL::Design *design, const char *format, va_list ap);
73 void logv_warning(const char *format, va_list ap);
74 void logv_warning_noprefix(const char *format, va_list ap);
75 YS_NORETURN void logv_error(const char *format, va_list ap) YS_ATTRIBUTE(noreturn);
76
77 void log(const char *format, ...) YS_ATTRIBUTE(format(printf, 1, 2));
78 void log_header(RTLIL::Design *design, const char *format, ...) YS_ATTRIBUTE(format(printf, 2, 3));
79 void log_warning(const char *format, ...) YS_ATTRIBUTE(format(printf, 1, 2));
80 void log_experimental(const char *format, ...) YS_ATTRIBUTE(format(printf, 1, 2));
81
82 // Log with filename to report a problem in a source file.
83 void log_file_warning(const std::string &filename, int lineno, const char *format, ...) YS_ATTRIBUTE(format(printf, 3, 4));
84 void log_file_info(const std::string &filename, int lineno, const char *format, ...) YS_ATTRIBUTE(format(printf, 3, 4));
85
86 void log_warning_noprefix(const char *format, ...) YS_ATTRIBUTE(format(printf, 1, 2));
87 YS_NORETURN void log_error(const char *format, ...) YS_ATTRIBUTE(format(printf, 1, 2), noreturn);
88 void log_file_error(const string &filename, int lineno, const char *format, ...) YS_ATTRIBUTE(format(printf, 3, 4), noreturn);
89 YS_NORETURN void log_cmd_error(const char *format, ...) YS_ATTRIBUTE(format(printf, 1, 2), noreturn);
90
91 #ifndef NDEBUG
92 static inline bool ys_debug(int n = 0) { if (log_force_debug) return true; log_debug_suppressed += n; return false; }
93 # define log_debug(...) do { if (ys_debug(1)) log(__VA_ARGS__); } while (0)
94 #else
95 static inline bool ys_debug(int = 0) { return false; }
96 # define log_debug(_fmt, ...) do { } while (0)
97 #endif
98
99 static inline void log_suppressed() {
100 if (log_debug_suppressed && !log_make_debug) {
101 log("<suppressed ~%d debug messages>\n", log_debug_suppressed);
102 log_debug_suppressed = 0;
103 }
104 }
105
106 struct LogMakeDebugHdl {
107 bool status = false;
108 LogMakeDebugHdl(bool start_on = false) {
109 if (start_on)
110 on();
111 }
112 ~LogMakeDebugHdl() {
113 off();
114 }
115 void on() {
116 if (status) return;
117 status=true;
118 log_make_debug++;
119 }
120 void off_silent() {
121 if (!status) return;
122 status=false;
123 log_make_debug--;
124 }
125 void off() {
126 off_silent();
127 }
128 };
129
130 void log_spacer();
131 void log_push();
132 void log_pop();
133
134 void log_backtrace(const char *prefix, int levels);
135 void log_reset_stack();
136 void log_flush();
137
138 const char *log_signal(const RTLIL::SigSpec &sig, bool autoint = true);
139 const char *log_const(const RTLIL::Const &value, bool autoint = true);
140 const char *log_id(RTLIL::IdString id);
141
142 template<typename T> static inline const char *log_id(T *obj, const char *nullstr = nullptr) {
143 if (nullstr && obj == nullptr)
144 return nullstr;
145 return log_id(obj->name);
146 }
147
148 void log_module(RTLIL::Module *module, std::string indent = "");
149 void log_cell(RTLIL::Cell *cell, std::string indent = "");
150 void log_wire(RTLIL::Wire *wire, std::string indent = "");
151
152 #ifndef NDEBUG
153 static inline void log_assert_worker(bool cond, const char *expr, const char *file, int line) {
154 if (!cond) log_error("Assert `%s' failed in %s:%d.\n", expr, file, line);
155 }
156 # define log_assert(_assert_expr_) YOSYS_NAMESPACE_PREFIX log_assert_worker(_assert_expr_, #_assert_expr_, __FILE__, __LINE__)
157 #else
158 # define log_assert(_assert_expr_)
159 #endif
160
161 #define log_abort() YOSYS_NAMESPACE_PREFIX log_error("Abort in %s:%d.\n", __FILE__, __LINE__)
162 #define log_ping() YOSYS_NAMESPACE_PREFIX log("-- %s:%d %s --\n", __FILE__, __LINE__, __PRETTY_FUNCTION__)
163
164
165 // ---------------------------------------------------
166 // This is the magic behind the code coverage counters
167 // ---------------------------------------------------
168
169 #if defined(YOSYS_ENABLE_COVER) && (defined(__linux__) || defined(__FreeBSD__))
170
171 #define cover(_id) do { \
172 static CoverData __d __attribute__((section("yosys_cover_list"), aligned(1), used)) = { __FILE__, __FUNCTION__, _id, __LINE__, 0 }; \
173 __d.counter++; \
174 } while (0)
175
176 struct CoverData {
177 const char *file, *func, *id;
178 int line, counter;
179 } YS_ATTRIBUTE(packed);
180
181 // this two symbols are created by the linker for the "yosys_cover_list" ELF section
182 extern "C" struct CoverData __start_yosys_cover_list[];
183 extern "C" struct CoverData __stop_yosys_cover_list[];
184
185 extern dict<std::string, std::pair<std::string, int>> extra_coverage_data;
186
187 void cover_extra(std::string parent, std::string id, bool increment = true);
188 dict<std::string, std::pair<std::string, int>> get_coverage_data();
189
190 #define cover_list(_id, ...) do { cover(_id); \
191 std::string r = cover_list_worker(_id, __VA_ARGS__); \
192 log_assert(r.empty()); \
193 } while (0)
194
195 static inline std::string cover_list_worker(std::string, std::string last) {
196 return last;
197 }
198
199 template<typename... T>
200 std::string cover_list_worker(std::string prefix, std::string first, T... rest) {
201 std::string selected = cover_list_worker(prefix, rest...);
202 cover_extra(prefix, prefix + "." + first, first == selected);
203 return first == selected ? "" : selected;
204 }
205
206 #else
207 # define cover(...) do { } while (0)
208 # define cover_list(...) do { } while (0)
209 #endif
210
211
212 // ------------------------------------------------------------
213 // everything below this line are utilities for troubleshooting
214 // ------------------------------------------------------------
215
216 // simple timer for performance measurements
217 // toggle the '#if 1' to get a baseline for the performance penalty added by the measurement
218 struct PerformanceTimer
219 {
220 #if 1
221 int64_t total_ns;
222
223 PerformanceTimer() {
224 total_ns = 0;
225 }
226
227 static int64_t query() {
228 # ifdef _WIN32
229 return 0;
230 # elif defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0)
231 struct timespec ts;
232 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
233 return int64_t(ts.tv_sec)*1000000000 + ts.tv_nsec;
234 # elif defined(RUSAGE_SELF)
235 struct rusage rusage;
236 int64_t t;
237 if (getrusage(RUSAGE_SELF, &rusage) == -1) {
238 log_cmd_error("getrusage failed!\n");
239 log_abort();
240 }
241 t = 1000000000ULL * (int64_t) rusage.ru_utime.tv_sec + (int64_t) rusage.ru_utime.tv_usec * 1000ULL;
242 t += 1000000000ULL * (int64_t) rusage.ru_stime.tv_sec + (int64_t) rusage.ru_stime.tv_usec * 1000ULL;
243 return t;
244 # else
245 # error "Don't know how to measure per-process CPU time. Need alternative method (times()/clocks()/gettimeofday()?)."
246 # endif
247 }
248
249 void reset() {
250 total_ns = 0;
251 }
252
253 void begin() {
254 total_ns -= query();
255 }
256
257 void end() {
258 total_ns += query();
259 }
260
261 float sec() const {
262 return total_ns * 1e-9f;
263 }
264 #else
265 static int64_t query() { return 0; }
266 void reset() { }
267 void begin() { }
268 void end() { }
269 float sec() const { return 0; }
270 #endif
271 };
272
273 // simple API for quickly dumping values when debugging
274
275 static inline void log_dump_val_worker(short v) { log("%d", v); }
276 static inline void log_dump_val_worker(unsigned short v) { log("%u", v); }
277 static inline void log_dump_val_worker(int v) { log("%d", v); }
278 static inline void log_dump_val_worker(unsigned int v) { log("%u", v); }
279 static inline void log_dump_val_worker(long int v) { log("%ld", v); }
280 static inline void log_dump_val_worker(unsigned long int v) { log("%lu", v); }
281 #ifndef _WIN32
282 static inline void log_dump_val_worker(long long int v) { log("%lld", v); }
283 static inline void log_dump_val_worker(unsigned long long int v) { log("%lld", v); }
284 #endif
285 static inline void log_dump_val_worker(char c) { log(c >= 32 && c < 127 ? "'%c'" : "'\\x%02x'", c); }
286 static inline void log_dump_val_worker(unsigned char c) { log(c >= 32 && c < 127 ? "'%c'" : "'\\x%02x'", c); }
287 static inline void log_dump_val_worker(bool v) { log("%s", v ? "true" : "false"); }
288 static inline void log_dump_val_worker(double v) { log("%f", v); }
289 static inline void log_dump_val_worker(char *v) { log("%s", v); }
290 static inline void log_dump_val_worker(const char *v) { log("%s", v); }
291 static inline void log_dump_val_worker(std::string v) { log("%s", v.c_str()); }
292 static inline void log_dump_val_worker(PerformanceTimer p) { log("%f seconds", p.sec()); }
293 static inline void log_dump_args_worker(const char *p YS_ATTRIBUTE(unused)) { log_assert(*p == 0); }
294 void log_dump_val_worker(RTLIL::IdString v);
295 void log_dump_val_worker(RTLIL::SigSpec v);
296 void log_dump_val_worker(RTLIL::State v);
297
298 template<typename K, typename T, typename OPS>
299 static inline void log_dump_val_worker(dict<K, T, OPS> &v) {
300 log("{");
301 bool first = true;
302 for (auto &it : v) {
303 log(first ? " " : ", ");
304 log_dump_val_worker(it.first);
305 log(": ");
306 log_dump_val_worker(it.second);
307 first = false;
308 }
309 log(" }");
310 }
311
312 template<typename K, typename OPS>
313 static inline void log_dump_val_worker(pool<K, OPS> &v) {
314 log("{");
315 bool first = true;
316 for (auto &it : v) {
317 log(first ? " " : ", ");
318 log_dump_val_worker(it);
319 first = false;
320 }
321 log(" }");
322 }
323
324 template<typename T>
325 static inline void log_dump_val_worker(T *ptr) { log("%p", ptr); }
326
327 template<typename T, typename ... Args>
328 void log_dump_args_worker(const char *p, T first, Args ... args)
329 {
330 int next_p_state = 0;
331 const char *next_p = p;
332 while (*next_p && (next_p_state != 0 || *next_p != ',')) {
333 if (*next_p == '"')
334 do {
335 next_p++;
336 while (*next_p == '\\' && *(next_p + 1))
337 next_p += 2;
338 } while (*next_p && *next_p != '"');
339 if (*next_p == '\'') {
340 next_p++;
341 if (*next_p == '\\')
342 next_p++;
343 if (*next_p)
344 next_p++;
345 }
346 if (*next_p == '(' || *next_p == '[' || *next_p == '{')
347 next_p_state++;
348 if ((*next_p == ')' || *next_p == ']' || *next_p == '}') && next_p_state > 0)
349 next_p_state--;
350 next_p++;
351 }
352 log("\n\t%.*s => ", int(next_p - p), p);
353 if (*next_p == ',')
354 next_p++;
355 while (*next_p == ' ' || *next_p == '\t' || *next_p == '\r' || *next_p == '\n')
356 next_p++;
357 log_dump_val_worker(first);
358 log_dump_args_worker(next_p, args ...);
359 }
360
361 #define log_dump(...) do { \
362 log("DEBUG DUMP IN %s AT %s:%d:", __PRETTY_FUNCTION__, __FILE__, __LINE__); \
363 log_dump_args_worker(#__VA_ARGS__, __VA_ARGS__); \
364 log("\n"); \
365 } while (0)
366
367 YOSYS_NAMESPACE_END
368
369 #endif