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