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::list
<std::string
> string_buf
;
48 int string_buf_size
= 0;
50 static struct timeval initial_tv
= { 0, 0 };
51 static bool next_print_log
= false;
52 static int log_newline_count
= 0;
54 #if defined(_WIN32) && !defined(__MINGW32__)
55 // this will get time information and return it in timeval, simulating gettimeofday()
56 int gettimeofday(struct timeval
*tv
, struct timezone
*tz
)
58 LARGE_INTEGER counter
;
61 QueryPerformanceFrequency(&freq
);
62 QueryPerformanceCounter(&counter
);
64 counter
.QuadPart
*= 1000000;
65 counter
.QuadPart
/= freq
.QuadPart
;
67 tv
->tv_sec
= counter
.QuadPart
/ 1000000;
68 tv
->tv_usec
= counter
.QuadPart
% 1000000;
74 void logv(const char *format
, va_list ap
)
76 while (format
[0] == '\n' && format
[1] != 0) {
81 std::string str
= vstringf(format
, ap
);
86 size_t nnl_pos
= str
.find_last_not_of('\n');
87 if (nnl_pos
== std::string::npos
)
88 log_newline_count
+= GetSize(str
);
90 log_newline_count
= GetSize(str
) - nnl_pos
- 1;
93 log_hasher
->update(str
);
99 if (next_print_log
|| initial_tv
.tv_sec
== 0) {
100 next_print_log
= false;
102 gettimeofday(&tv
, NULL
);
103 if (initial_tv
.tv_sec
== 0)
105 if (tv
.tv_usec
< initial_tv
.tv_usec
) {
107 tv
.tv_usec
+= 1000000;
109 tv
.tv_sec
-= initial_tv
.tv_sec
;
110 tv
.tv_usec
-= initial_tv
.tv_usec
;
111 time_str
+= stringf("[%05d.%06d] ", int(tv
.tv_sec
), int(tv
.tv_usec
));
114 if (format
[0] && format
[strlen(format
)-1] == '\n')
115 next_print_log
= true;
117 for (auto f
: log_files
)
118 fputs(time_str
.c_str(), f
);
120 for (auto f
: log_streams
)
124 for (auto f
: log_files
)
125 fputs(str
.c_str(), f
);
127 for (auto f
: log_streams
)
131 void logv_header(const char *format
, va_list ap
)
133 bool pop_errfile
= false;
136 if (header_count
.size() > 0)
137 header_count
.back()++;
139 if (int(header_count
.size()) <= log_verbose_level
&& log_errfile
!= NULL
) {
140 log_files
.push_back(log_errfile
);
144 for (int c
: header_count
)
151 log_files
.pop_back();
154 void logv_error(const char *format
, va_list ap
)
156 if (log_errfile
!= NULL
)
157 log_files
.push_back(log_errfile
);
165 void log(const char *format
, ...)
168 va_start(ap
, format
);
173 void log_header(const char *format
, ...)
176 va_start(ap
, format
);
177 logv_header(format
, ap
);
181 void log_error(const char *format
, ...)
184 va_start(ap
, format
);
185 logv_error(format
, ap
);
188 void log_cmd_error(const char *format
, ...)
191 va_start(ap
, format
);
193 if (log_cmd_error_throw
) {
197 throw log_cmd_error_expection();
200 logv_error(format
, ap
);
205 while (log_newline_count
< 2)
211 header_count
.push_back(0);
216 header_count
.pop_back();
222 void log_reset_stack()
224 while (header_count
.size() > 1)
225 header_count
.pop_back();
233 for (auto f
: log_files
)
236 for (auto f
: log_streams
)
240 void log_dump_val_worker(RTLIL::SigSpec v
) {
241 log("%s", log_signal(v
));
244 const char *log_signal(const RTLIL::SigSpec
&sig
, bool autoint
)
246 std::stringstream buf
;
247 ILANG_BACKEND::dump_sigspec(buf
, sig
, autoint
);
249 if (string_buf_size
< 100)
252 string_buf
.pop_front();
253 string_buf
.push_back(buf
.str());
255 return string_buf
.back().c_str();
258 const char *log_id(RTLIL::IdString str
)
260 const char *p
= str
.c_str();
261 log_assert(RTLIL::IdString::global_refcount_storage_
[str
.index_
] > 1);
262 if (p
[0] == '\\' && p
[1] != '$' && p
[1] != 0)
267 void log_cell(RTLIL::Cell
*cell
, std::string indent
)
269 std::stringstream buf
;
270 ILANG_BACKEND::dump_cell(buf
, indent
, cell
);
271 log("%s", buf
.str().c_str());
274 // ---------------------------------------------------
275 // This is the magic behind the code coverage counters
276 // ---------------------------------------------------
279 std::map
<std::string
, std::pair
<std::string
, int>> extra_coverage_data
;
281 void cover_extra(std::string parent
, std::string id
, bool increment
) {
282 if (extra_coverage_data
.count(id
) == 0) {
283 for (CoverData
*p
= __start_yosys_cover_list
; p
!= __stop_yosys_cover_list
; p
++)
285 extra_coverage_data
[id
].first
= stringf("%s:%d:%s", p
->file
, p
->line
, p
->func
);
286 log_assert(extra_coverage_data
.count(id
));
289 extra_coverage_data
[id
].second
++;
292 std::map
<std::string
, std::pair
<std::string
, int>> get_coverage_data()
294 std::map
<std::string
, std::pair
<std::string
, int>> coverage_data
;
296 for (auto &it
: pass_register
) {
297 std::string key
= stringf("passes.%s", it
.first
.c_str());
298 coverage_data
[key
].first
= stringf("%s:%d:%s", __FILE__
, __LINE__
, __FUNCTION__
);
299 coverage_data
[key
].second
+= it
.second
->call_counter
;
302 for (auto &it
: extra_coverage_data
) {
303 if (coverage_data
.count(it
.first
))
304 log("WARNING: found duplicate coverage id \"%s\".\n", it
.first
.c_str());
305 coverage_data
[it
.first
].first
= it
.second
.first
;
306 coverage_data
[it
.first
].second
+= it
.second
.second
;
309 for (CoverData
*p
= __start_yosys_cover_list
; p
!= __stop_yosys_cover_list
; p
++) {
310 if (coverage_data
.count(p
->id
))
311 log("WARNING: found duplicate coverage id \"%s\".\n", p
->id
);
312 coverage_data
[p
->id
].first
= stringf("%s:%d:%s", p
->file
, p
->line
, p
->func
);
313 coverage_data
[p
->id
].second
+= p
->counter
;
316 for (auto &it
: coverage_data
)
317 if (!it
.second
.first
.compare(0, strlen(YOSYS_SRC
"/"), YOSYS_SRC
"/"))
318 it
.second
.first
= it
.second
.first
.substr(strlen(YOSYS_SRC
"/"));
320 return coverage_data
;