5b18e3d6c3705fdae1bd085905b7ad909ba16c7f
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>
37 std::vector
<FILE*> log_files
;
38 std::vector
<std::ostream
*> log_streams
;
39 FILE *log_errfile
= NULL
;
40 SHA1
*log_hasher
= NULL
;
42 bool log_time
= false;
43 bool log_cmd_error_throw
= false;
44 bool log_quiet_warnings
= false;
45 int log_verbose_level
;
47 std::vector
<int> header_count
;
48 std::set
<RTLIL::IdString
> log_id_cache
;
49 std::list
<std::string
> string_buf
;
50 int string_buf_size
= 0;
52 static struct timeval initial_tv
= { 0, 0 };
53 static bool next_print_log
= false;
54 static int log_newline_count
= 0;
56 #if defined(_WIN32) && !defined(__MINGW32__)
57 // this will get time information and return it in timeval, simulating gettimeofday()
58 int gettimeofday(struct timeval
*tv
, struct timezone
*tz
)
60 LARGE_INTEGER counter
;
63 QueryPerformanceFrequency(&freq
);
64 QueryPerformanceCounter(&counter
);
66 counter
.QuadPart
*= 1000000;
67 counter
.QuadPart
/= freq
.QuadPart
;
69 tv
->tv_sec
= long(counter
.QuadPart
/ 1000000);
70 tv
->tv_usec
= counter
.QuadPart
% 1000000;
76 void logv(const char *format
, va_list ap
)
78 while (format
[0] == '\n' && format
[1] != 0) {
83 std::string str
= vstringf(format
, ap
);
88 size_t nnl_pos
= str
.find_last_not_of('\n');
89 if (nnl_pos
== std::string::npos
)
90 log_newline_count
+= GetSize(str
);
92 log_newline_count
= GetSize(str
) - nnl_pos
- 1;
95 log_hasher
->update(str
);
101 if (next_print_log
|| initial_tv
.tv_sec
== 0) {
102 next_print_log
= false;
104 gettimeofday(&tv
, NULL
);
105 if (initial_tv
.tv_sec
== 0)
107 if (tv
.tv_usec
< initial_tv
.tv_usec
) {
109 tv
.tv_usec
+= 1000000;
111 tv
.tv_sec
-= initial_tv
.tv_sec
;
112 tv
.tv_usec
-= initial_tv
.tv_usec
;
113 time_str
+= stringf("[%05d.%06d] ", int(tv
.tv_sec
), int(tv
.tv_usec
));
116 if (format
[0] && format
[strlen(format
)-1] == '\n')
117 next_print_log
= true;
119 for (auto f
: log_files
)
120 fputs(time_str
.c_str(), f
);
122 for (auto f
: log_streams
)
126 for (auto f
: log_files
)
127 fputs(str
.c_str(), f
);
129 for (auto f
: log_streams
)
133 void logv_header(const char *format
, va_list ap
)
135 bool pop_errfile
= false;
138 if (header_count
.size() > 0)
139 header_count
.back()++;
141 if (int(header_count
.size()) <= log_verbose_level
&& log_errfile
!= NULL
) {
142 log_files
.push_back(log_errfile
);
146 for (int c
: header_count
)
153 log_files
.pop_back();
156 void logv_warning(const char *format
, va_list ap
)
158 if (log_errfile
!= NULL
&& !log_quiet_warnings
)
159 log_files
.push_back(log_errfile
);
165 if (log_errfile
!= NULL
&& !log_quiet_warnings
)
166 log_files
.pop_back();
169 void logv_error(const char *format
, va_list ap
)
171 if (log_errfile
!= NULL
)
172 log_files
.push_back(log_errfile
);
180 void log(const char *format
, ...)
183 va_start(ap
, format
);
188 void log_header(const char *format
, ...)
191 va_start(ap
, format
);
192 logv_header(format
, ap
);
196 void log_warning(const char *format
, ...)
199 va_start(ap
, format
);
200 logv_warning(format
, ap
);
204 void log_error(const char *format
, ...)
207 va_start(ap
, format
);
208 logv_error(format
, ap
);
211 void log_cmd_error(const char *format
, ...)
214 va_start(ap
, format
);
216 if (log_cmd_error_throw
) {
220 throw log_cmd_error_exception();
223 logv_error(format
, ap
);
228 while (log_newline_count
< 2)
234 header_count
.push_back(0);
239 header_count
.pop_back();
240 log_id_cache
.clear();
246 void log_reset_stack()
248 while (header_count
.size() > 1)
249 header_count
.pop_back();
250 log_id_cache
.clear();
258 for (auto f
: log_files
)
261 for (auto f
: log_streams
)
265 void log_dump_val_worker(RTLIL::SigSpec v
) {
266 log("%s", log_signal(v
));
269 const char *log_signal(const RTLIL::SigSpec
&sig
, bool autoint
)
271 std::stringstream buf
;
272 ILANG_BACKEND::dump_sigspec(buf
, sig
, autoint
);
274 if (string_buf_size
< 100)
277 string_buf
.pop_front();
278 string_buf
.push_back(buf
.str());
280 return string_buf
.back().c_str();
283 const char *log_id(RTLIL::IdString str
)
285 log_id_cache
.insert(str
);
286 const char *p
= str
.c_str();
287 if (p
[0] == '\\' && p
[1] != '$' && p
[1] != 0)
292 void log_cell(RTLIL::Cell
*cell
, std::string indent
)
294 std::stringstream buf
;
295 ILANG_BACKEND::dump_cell(buf
, indent
, cell
);
296 log("%s", buf
.str().c_str());
299 // ---------------------------------------------------
300 // This is the magic behind the code coverage counters
301 // ---------------------------------------------------
302 #ifdef YOSYS_ENABLE_COVER
304 dict
<std::string
, std::pair
<std::string
, int>> extra_coverage_data
;
306 void cover_extra(std::string parent
, std::string id
, bool increment
) {
307 if (extra_coverage_data
.count(id
) == 0) {
308 for (CoverData
*p
= __start_yosys_cover_list
; p
!= __stop_yosys_cover_list
; p
++)
310 extra_coverage_data
[id
].first
= stringf("%s:%d:%s", p
->file
, p
->line
, p
->func
);
311 log_assert(extra_coverage_data
.count(id
));
314 extra_coverage_data
[id
].second
++;
317 dict
<std::string
, std::pair
<std::string
, int>> get_coverage_data()
319 dict
<std::string
, std::pair
<std::string
, int>> coverage_data
;
321 for (auto &it
: pass_register
) {
322 std::string key
= stringf("passes.%s", it
.first
.c_str());
323 coverage_data
[key
].first
= stringf("%s:%d:%s", __FILE__
, __LINE__
, __FUNCTION__
);
324 coverage_data
[key
].second
+= it
.second
->call_counter
;
327 for (auto &it
: extra_coverage_data
) {
328 if (coverage_data
.count(it
.first
))
329 log_warning("found duplicate coverage id \"%s\".\n", it
.first
.c_str());
330 coverage_data
[it
.first
].first
= it
.second
.first
;
331 coverage_data
[it
.first
].second
+= it
.second
.second
;
334 for (CoverData
*p
= __start_yosys_cover_list
; p
!= __stop_yosys_cover_list
; p
++) {
335 if (coverage_data
.count(p
->id
))
336 log_warning("found duplicate coverage id \"%s\".\n", p
->id
);
337 coverage_data
[p
->id
].first
= stringf("%s:%d:%s", p
->file
, p
->line
, p
->func
);
338 coverage_data
[p
->id
].second
+= p
->counter
;
341 for (auto &it
: coverage_data
)
342 if (!it
.second
.first
.compare(0, strlen(YOSYS_SRC
"/"), YOSYS_SRC
"/"))
343 it
.second
.first
= it
.second
.first
.substr(strlen(YOSYS_SRC
"/"));
345 return coverage_data
;