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"
21 #include "libs/sha1/sha1.h"
22 #include "backends/ilang/ilang_backend.h"
24 #if !defined(_WIN32) || defined(__MINGW32__)
25 # include <sys/time.h>
41 std::vector
<FILE*> log_files
;
42 std::vector
<std::ostream
*> log_streams
;
43 FILE *log_errfile
= NULL
;
44 SHA1
*log_hasher
= NULL
;
46 bool log_time
= false;
47 bool log_cmd_error_throw
= false;
48 bool log_quiet_warnings
= false;
49 int log_verbose_level
;
51 std::vector
<int> header_count
;
52 std::set
<RTLIL::IdString
> log_id_cache
;
53 std::list
<std::string
> string_buf
;
54 int string_buf_size
= 0;
56 static struct timeval initial_tv
= { 0, 0 };
57 static bool next_print_log
= false;
58 static int log_newline_count
= 0;
60 #if defined(_WIN32) && !defined(__MINGW32__)
61 // this will get time information and return it in timeval, simulating gettimeofday()
62 int gettimeofday(struct timeval
*tv
, struct timezone
*tz
)
64 LARGE_INTEGER counter
;
67 QueryPerformanceFrequency(&freq
);
68 QueryPerformanceCounter(&counter
);
70 counter
.QuadPart
*= 1000000;
71 counter
.QuadPart
/= freq
.QuadPart
;
73 tv
->tv_sec
= long(counter
.QuadPart
/ 1000000);
74 tv
->tv_usec
= counter
.QuadPart
% 1000000;
80 void logv(const char *format
, va_list ap
)
82 while (format
[0] == '\n' && format
[1] != 0) {
87 std::string str
= vstringf(format
, ap
);
92 size_t nnl_pos
= str
.find_last_not_of('\n');
93 if (nnl_pos
== std::string::npos
)
94 log_newline_count
+= GetSize(str
);
96 log_newline_count
= GetSize(str
) - nnl_pos
- 1;
99 log_hasher
->update(str
);
103 std::string time_str
;
105 if (next_print_log
|| initial_tv
.tv_sec
== 0) {
106 next_print_log
= false;
108 gettimeofday(&tv
, NULL
);
109 if (initial_tv
.tv_sec
== 0)
111 if (tv
.tv_usec
< initial_tv
.tv_usec
) {
113 tv
.tv_usec
+= 1000000;
115 tv
.tv_sec
-= initial_tv
.tv_sec
;
116 tv
.tv_usec
-= initial_tv
.tv_usec
;
117 time_str
+= stringf("[%05d.%06d] ", int(tv
.tv_sec
), int(tv
.tv_usec
));
120 if (format
[0] && format
[strlen(format
)-1] == '\n')
121 next_print_log
= true;
123 for (auto f
: log_files
)
124 fputs(time_str
.c_str(), f
);
126 for (auto f
: log_streams
)
130 for (auto f
: log_files
)
131 fputs(str
.c_str(), f
);
133 for (auto f
: log_streams
)
137 void logv_header(const char *format
, va_list ap
)
139 bool pop_errfile
= false;
142 if (header_count
.size() > 0)
143 header_count
.back()++;
145 if (int(header_count
.size()) <= log_verbose_level
&& log_errfile
!= NULL
) {
146 log_files
.push_back(log_errfile
);
150 for (int c
: header_count
)
157 log_files
.pop_back();
160 void logv_warning(const char *format
, va_list ap
)
162 if (log_errfile
!= NULL
&& !log_quiet_warnings
)
163 log_files
.push_back(log_errfile
);
169 if (log_errfile
!= NULL
&& !log_quiet_warnings
)
170 log_files
.pop_back();
173 void logv_error(const char *format
, va_list ap
)
175 if (log_errfile
!= NULL
)
176 log_files
.push_back(log_errfile
);
184 void log(const char *format
, ...)
187 va_start(ap
, format
);
192 void log_header(const char *format
, ...)
195 va_start(ap
, format
);
196 logv_header(format
, ap
);
200 void log_warning(const char *format
, ...)
203 va_start(ap
, format
);
204 logv_warning(format
, ap
);
208 void log_error(const char *format
, ...)
211 va_start(ap
, format
);
212 logv_error(format
, ap
);
215 void log_cmd_error(const char *format
, ...)
218 va_start(ap
, format
);
220 if (log_cmd_error_throw
) {
224 throw log_cmd_error_exception();
227 logv_error(format
, ap
);
232 while (log_newline_count
< 2)
238 header_count
.push_back(0);
243 header_count
.pop_back();
244 log_id_cache
.clear();
250 void log_backtrace(const char *prefix
, int levels
)
253 if (levels
<= 0) return;
258 if ((p
= __builtin_extract_return_addr(__builtin_return_address(0))) && dladdr(p
, &dli
)) {
259 log("%sframe #1: %p %s(%p) %s(%p)\n", prefix
, p
, dli
.dli_fname
, dli
.dli_fbase
, dli
.dli_sname
, dli
.dli_saddr
);
261 log("%sframe #1: ---\n", prefix
);
265 if (levels
<= 1) return;
267 if ((p
= __builtin_extract_return_addr(__builtin_return_address(1))) && dladdr(p
, &dli
)) {
268 log("%sframe #2: %p %s(%p) %s(%p)\n", prefix
, p
, dli
.dli_fname
, dli
.dli_fbase
, dli
.dli_sname
, dli
.dli_saddr
);
270 log("%sframe #2: ---\n", prefix
);
274 if (levels
<= 2) return;
276 if ((p
= __builtin_extract_return_addr(__builtin_return_address(2))) && dladdr(p
, &dli
)) {
277 log("%sframe #3: %p %s(%p) %s(%p)\n", prefix
, p
, dli
.dli_fname
, dli
.dli_fbase
, dli
.dli_sname
, dli
.dli_saddr
);
279 log("%sframe #3: ---\n", prefix
);
283 if (levels
<= 3) return;
285 if ((p
= __builtin_extract_return_addr(__builtin_return_address(3))) && dladdr(p
, &dli
)) {
286 log("%sframe #4: %p %s(%p) %s(%p)\n", prefix
, p
, dli
.dli_fname
, dli
.dli_fbase
, dli
.dli_sname
, dli
.dli_saddr
);
288 log("%sframe #4: ---\n", prefix
);
292 if (levels
<= 4) return;
294 if ((p
= __builtin_extract_return_addr(__builtin_return_address(4))) && dladdr(p
, &dli
)) {
295 log("%sframe #5: %p %s(%p) %s(%p)\n", prefix
, p
, dli
.dli_fname
, dli
.dli_fbase
, dli
.dli_sname
, dli
.dli_saddr
);
297 log("%sframe #5: ---\n", prefix
);
301 if (levels
<= 5) return;
303 if ((p
= __builtin_extract_return_addr(__builtin_return_address(5))) && dladdr(p
, &dli
)) {
304 log("%sframe #6: %p %s(%p) %s(%p)\n", prefix
, p
, dli
.dli_fname
, dli
.dli_fbase
, dli
.dli_sname
, dli
.dli_saddr
);
306 log("%sframe #6: ---\n", prefix
);
310 if (levels
<= 6) return;
312 if ((p
= __builtin_extract_return_addr(__builtin_return_address(6))) && dladdr(p
, &dli
)) {
313 log("%sframe #7: %p %s(%p) %s(%p)\n", prefix
, p
, dli
.dli_fname
, dli
.dli_fbase
, dli
.dli_sname
, dli
.dli_saddr
);
315 log("%sframe #7: ---\n", prefix
);
319 if (levels
<= 7) return;
321 if ((p
= __builtin_extract_return_addr(__builtin_return_address(7))) && dladdr(p
, &dli
)) {
322 log("%sframe #8: %p %s(%p) %s(%p)\n", prefix
, p
, dli
.dli_fname
, dli
.dli_fbase
, dli
.dli_sname
, dli
.dli_saddr
);
324 log("%sframe #8: ---\n", prefix
);
328 if (levels
<= 8) return;
330 if ((p
= __builtin_extract_return_addr(__builtin_return_address(8))) && dladdr(p
, &dli
)) {
331 log("%sframe #9: %p %s(%p) %s(%p)\n", prefix
, p
, dli
.dli_fname
, dli
.dli_fbase
, dli
.dli_sname
, dli
.dli_saddr
);
333 log("%sframe #9: ---\n", prefix
);
337 if (levels
<= 9) return;
341 void log_reset_stack()
343 while (header_count
.size() > 1)
344 header_count
.pop_back();
345 log_id_cache
.clear();
353 for (auto f
: log_files
)
356 for (auto f
: log_streams
)
360 void log_dump_val_worker(RTLIL::SigSpec v
) {
361 log("%s", log_signal(v
));
364 const char *log_signal(const RTLIL::SigSpec
&sig
, bool autoint
)
366 std::stringstream buf
;
367 ILANG_BACKEND::dump_sigspec(buf
, sig
, autoint
);
369 if (string_buf_size
< 100)
372 string_buf
.pop_front();
373 string_buf
.push_back(buf
.str());
375 return string_buf
.back().c_str();
378 const char *log_id(RTLIL::IdString str
)
380 log_id_cache
.insert(str
);
381 const char *p
= str
.c_str();
382 if (p
[0] == '\\' && p
[1] != '$' && p
[1] != 0)
387 void log_cell(RTLIL::Cell
*cell
, std::string indent
)
389 std::stringstream buf
;
390 ILANG_BACKEND::dump_cell(buf
, indent
, cell
);
391 log("%s", buf
.str().c_str());
394 // ---------------------------------------------------
395 // This is the magic behind the code coverage counters
396 // ---------------------------------------------------
397 #ifdef YOSYS_ENABLE_COVER
399 dict
<std::string
, std::pair
<std::string
, int>> extra_coverage_data
;
401 void cover_extra(std::string parent
, std::string id
, bool increment
) {
402 if (extra_coverage_data
.count(id
) == 0) {
403 for (CoverData
*p
= __start_yosys_cover_list
; p
!= __stop_yosys_cover_list
; p
++)
405 extra_coverage_data
[id
].first
= stringf("%s:%d:%s", p
->file
, p
->line
, p
->func
);
406 log_assert(extra_coverage_data
.count(id
));
409 extra_coverage_data
[id
].second
++;
412 dict
<std::string
, std::pair
<std::string
, int>> get_coverage_data()
414 dict
<std::string
, std::pair
<std::string
, int>> coverage_data
;
416 for (auto &it
: pass_register
) {
417 std::string key
= stringf("passes.%s", it
.first
.c_str());
418 coverage_data
[key
].first
= stringf("%s:%d:%s", __FILE__
, __LINE__
, __FUNCTION__
);
419 coverage_data
[key
].second
+= it
.second
->call_counter
;
422 for (auto &it
: extra_coverage_data
) {
423 if (coverage_data
.count(it
.first
))
424 log_warning("found duplicate coverage id \"%s\".\n", it
.first
.c_str());
425 coverage_data
[it
.first
].first
= it
.second
.first
;
426 coverage_data
[it
.first
].second
+= it
.second
.second
;
429 for (CoverData
*p
= __start_yosys_cover_list
; p
!= __stop_yosys_cover_list
; p
++) {
430 if (coverage_data
.count(p
->id
))
431 log_warning("found duplicate coverage id \"%s\".\n", p
->id
);
432 coverage_data
[p
->id
].first
= stringf("%s:%d:%s", p
->file
, p
->line
, p
->func
);
433 coverage_data
[p
->id
].second
+= p
->counter
;
436 for (auto &it
: coverage_data
)
437 if (!it
.second
.first
.compare(0, strlen(YOSYS_SRC
"/"), YOSYS_SRC
"/"))
438 it
.second
.first
= it
.second
.first
.substr(strlen(YOSYS_SRC
"/"));
440 return coverage_data
;