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 int log_verbose_level
;
46 std::vector
<int> header_count
;
47 std::set
<RTLIL::IdString
> log_id_cache
;
48 std::list
<std::string
> string_buf
;
49 int string_buf_size
= 0;
51 static struct timeval initial_tv
= { 0, 0 };
52 static bool next_print_log
= false;
53 static int log_newline_count
= 0;
55 #if defined(_WIN32) && !defined(__MINGW32__)
56 // this will get time information and return it in timeval, simulating gettimeofday()
57 int gettimeofday(struct timeval
*tv
, struct timezone
*tz
)
59 LARGE_INTEGER counter
;
62 QueryPerformanceFrequency(&freq
);
63 QueryPerformanceCounter(&counter
);
65 counter
.QuadPart
*= 1000000;
66 counter
.QuadPart
/= freq
.QuadPart
;
68 tv
->tv_sec
= long(counter
.QuadPart
/ 1000000);
69 tv
->tv_usec
= counter
.QuadPart
% 1000000;
75 void logv(const char *format
, va_list ap
)
77 while (format
[0] == '\n' && format
[1] != 0) {
82 std::string str
= vstringf(format
, ap
);
87 size_t nnl_pos
= str
.find_last_not_of('\n');
88 if (nnl_pos
== std::string::npos
)
89 log_newline_count
+= GetSize(str
);
91 log_newline_count
= GetSize(str
) - nnl_pos
- 1;
94 log_hasher
->update(str
);
100 if (next_print_log
|| initial_tv
.tv_sec
== 0) {
101 next_print_log
= false;
103 gettimeofday(&tv
, NULL
);
104 if (initial_tv
.tv_sec
== 0)
106 if (tv
.tv_usec
< initial_tv
.tv_usec
) {
108 tv
.tv_usec
+= 1000000;
110 tv
.tv_sec
-= initial_tv
.tv_sec
;
111 tv
.tv_usec
-= initial_tv
.tv_usec
;
112 time_str
+= stringf("[%05d.%06d] ", int(tv
.tv_sec
), int(tv
.tv_usec
));
115 if (format
[0] && format
[strlen(format
)-1] == '\n')
116 next_print_log
= true;
118 for (auto f
: log_files
)
119 fputs(time_str
.c_str(), f
);
121 for (auto f
: log_streams
)
125 for (auto f
: log_files
)
126 fputs(str
.c_str(), f
);
128 for (auto f
: log_streams
)
132 void logv_header(const char *format
, va_list ap
)
134 bool pop_errfile
= false;
137 if (header_count
.size() > 0)
138 header_count
.back()++;
140 if (int(header_count
.size()) <= log_verbose_level
&& log_errfile
!= NULL
) {
141 log_files
.push_back(log_errfile
);
145 for (int c
: header_count
)
152 log_files
.pop_back();
155 void logv_error(const char *format
, va_list ap
)
157 if (log_errfile
!= NULL
)
158 log_files
.push_back(log_errfile
);
166 void log(const char *format
, ...)
169 va_start(ap
, format
);
174 void log_header(const char *format
, ...)
177 va_start(ap
, format
);
178 logv_header(format
, ap
);
182 void log_error(const char *format
, ...)
185 va_start(ap
, format
);
186 logv_error(format
, ap
);
189 void log_cmd_error(const char *format
, ...)
192 va_start(ap
, format
);
194 if (log_cmd_error_throw
) {
198 throw log_cmd_error_exception();
201 logv_error(format
, ap
);
206 while (log_newline_count
< 2)
212 header_count
.push_back(0);
217 header_count
.pop_back();
218 log_id_cache
.clear();
224 void log_reset_stack()
226 while (header_count
.size() > 1)
227 header_count
.pop_back();
228 log_id_cache
.clear();
236 for (auto f
: log_files
)
239 for (auto f
: log_streams
)
243 void log_dump_val_worker(RTLIL::SigSpec v
) {
244 log("%s", log_signal(v
));
247 const char *log_signal(const RTLIL::SigSpec
&sig
, bool autoint
)
249 std::stringstream buf
;
250 ILANG_BACKEND::dump_sigspec(buf
, sig
, autoint
);
252 if (string_buf_size
< 100)
255 string_buf
.pop_front();
256 string_buf
.push_back(buf
.str());
258 return string_buf
.back().c_str();
261 const char *log_id(RTLIL::IdString str
)
263 log_id_cache
.insert(str
);
264 const char *p
= str
.c_str();
265 if (p
[0] == '\\' && p
[1] != '$' && p
[1] != 0)
270 void log_cell(RTLIL::Cell
*cell
, std::string indent
)
272 std::stringstream buf
;
273 ILANG_BACKEND::dump_cell(buf
, indent
, cell
);
274 log("%s", buf
.str().c_str());
277 // ---------------------------------------------------
278 // This is the magic behind the code coverage counters
279 // ---------------------------------------------------
280 #ifdef YOSYS_ENABLE_COVER
282 std::map
<std::string
, std::pair
<std::string
, int>> extra_coverage_data
;
284 void cover_extra(std::string parent
, std::string id
, bool increment
) {
285 if (extra_coverage_data
.count(id
) == 0) {
286 for (CoverData
*p
= __start_yosys_cover_list
; p
!= __stop_yosys_cover_list
; p
++)
288 extra_coverage_data
[id
].first
= stringf("%s:%d:%s", p
->file
, p
->line
, p
->func
);
289 log_assert(extra_coverage_data
.count(id
));
292 extra_coverage_data
[id
].second
++;
295 std::map
<std::string
, std::pair
<std::string
, int>> get_coverage_data()
297 std::map
<std::string
, std::pair
<std::string
, int>> coverage_data
;
299 for (auto &it
: pass_register
) {
300 std::string key
= stringf("passes.%s", it
.first
.c_str());
301 coverage_data
[key
].first
= stringf("%s:%d:%s", __FILE__
, __LINE__
, __FUNCTION__
);
302 coverage_data
[key
].second
+= it
.second
->call_counter
;
305 for (auto &it
: extra_coverage_data
) {
306 if (coverage_data
.count(it
.first
))
307 log("WARNING: found duplicate coverage id \"%s\".\n", it
.first
.c_str());
308 coverage_data
[it
.first
].first
= it
.second
.first
;
309 coverage_data
[it
.first
].second
+= it
.second
.second
;
312 for (CoverData
*p
= __start_yosys_cover_list
; p
!= __stop_yosys_cover_list
; p
++) {
313 if (coverage_data
.count(p
->id
))
314 log("WARNING: found duplicate coverage id \"%s\".\n", p
->id
);
315 coverage_data
[p
->id
].first
= stringf("%s:%d:%s", p
->file
, p
->line
, p
->func
);
316 coverage_data
[p
->id
].second
+= p
->counter
;
319 for (auto &it
: coverage_data
)
320 if (!it
.second
.first
.compare(0, strlen(YOSYS_SRC
"/"), YOSYS_SRC
"/"))
321 it
.second
.first
= it
.second
.first
.substr(strlen(YOSYS_SRC
"/"));
323 return coverage_data
;