-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathsct_assert.h
350 lines (301 loc) · 13 KB
/
sct_assert.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/******************************************************************************
* Copyright (c) 2020-2023, Intel Corporation. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception.
*
*****************************************************************************/
/**
* SystemC temporal assertions.
* SCT_ASSERT and SCT_ASSERT_LOOP macros definition.
*
* Author: Mikhail Moiseev
*/
#ifndef SCT_ASSERT_H
#define SCT_ASSERT_H
#ifdef SCT_TLM_MODE
#define SCT_ASSERT_OFF 1
#endif
#if !defined(__SC_TOOL__) && !defined(SCT_ASSERT_OFF)
#include "sct_property.h"
#endif
#include "systemc.h"
#include <cassert>
namespace sc_core {
/// Get string representation for argument
#define SCT_ONE_(X) #X
#define SCT_ONE(X) SCT_ONE_(X)
/// Get string concatenation for arguments
#define SCT_TWO_(X,Y) X##Y
#define SCT_TWO(X,Y) SCT_TWO_(X,Y)
/// Macro argument number overload
#ifndef SCT_GET_MACRO
#define SCT_GET_MACRO(_1,_2,_3,_4,NAME,...) NAME
#endif
/// Remove brackets from given argument
#define SCT_ARGS__(...) __VA_ARGS__
#define SCT_ARGS_(X) X
#define SCT_ARGS(X) SCT_ARGS_(SCT_ARGS__ X)
//=============================================================================
// Immediate assertions
/// Immediate assertion, checked at SC simulation and SVA generated
/// \param msg -- error message, added into generated SVA
#ifdef __SC_TOOL__
inline void sct_assert(bool expr) {}
inline void sct_assert(bool expr, const char* msg) {}
#else
#ifdef NDEBUG
#define sct_assert1(expr) \
if (!(expr)) {\
std::cout << std::endl << sc_time_stamp() << \
", Error : sct_assert violation " << \
std::string(__FILE__) << ":" << \
std::to_string(__LINE__) << std::endl;\
}
#define sct_assert2(expr, msg) \
if (!(expr)) {\
std::cout << std::endl << sc_time_stamp() << \
", Error : sct_assert violation " << \
std::string(__FILE__) << ":" << \
std::to_string(__LINE__) << " " << msg << std::endl;\
}
#else
#define sct_assert1(expr) assert(expr)
#define sct_assert2(expr, msg) assert(expr && msg)
#endif
#define sct_assert(...) SCT_GET_MACRO(__VA_ARGS__, , ,\
sct_assert2, sct_assert1)(__VA_ARGS__)
#endif
//=============================================================================
// Temporal assertions
/// Time parameter
#define SCT_TIME1(X) (X)
#define SCT_TIME2(X,Y) (X,Y)
#define SCT_TIME(...) SCT_GET_MACRO(__VA_ARGS__, , ,\
SCT_TIME2, SCT_TIME1)(__VA_ARGS__)
/// Temporal assertion in module scope
#ifdef __SC_TOOL__
struct sct_property_mod
{
explicit sct_property_mod() {}
template<class T1, class T2, class RT>
explicit sct_property_mod(bool lexpr, RT rexpr, sc_event_finder& event,
const char* name, T1 lotime, T2 hitime,
unsigned stable) {}
template<class T1, class RT>
explicit sct_property_mod(bool lexpr, RT rexpr, sc_event_finder& event,
const char* name, T1 time, unsigned stable) {}
template<class T1, class T2, class RT>
explicit sct_property_mod(bool lexpr, RT rexpr, sc_port_base& event,
const char* name, T1 lotime, T2 hitime,
unsigned stable) {}
template<class T1, class RT>
explicit sct_property_mod(bool lexpr, RT rexpr, sc_port_base& event,
const char* name, T1 time, unsigned stable) {}
};
/// __SC_TOOL_CLANG__ defined for Clang only, SVC target build without it
#ifdef __SC_TOOL_CLANG__
#define SCT_ASSERT4(LE, TIMES, RE, EVENT)\
sct_property_mod SCT_TWO(sctTmpVar,__LINE__){\
static_cast<bool>(LE), static_cast<bool>(RE), EVENT,\
"sctAssertLine" SCT_ONE(__LINE__), SCT_ARGS(TIMES), 0};
#define SCT_ASSERT4_STABLE(LE, TIMES, RE, EVENT)\
sct_property_mod SCT_TWO(sctTmpVar,__LINE__){\
static_cast<bool>(LE), RE, EVENT,\
"sctAssertLine" SCT_ONE(__LINE__), SCT_ARGS(TIMES), 1};
#define SCT_ASSERT4_ROSE(LE, TIMES, RE, EVENT)\
sct_property_mod SCT_TWO(sctTmpVar,__LINE__){\
static_cast<bool>(LE), RE, EVENT,\
"sctAssertLine" SCT_ONE(__LINE__), SCT_ARGS(TIMES), 2};
#define SCT_ASSERT4_FELL(LE, TIMES, RE, EVENT)\
sct_property_mod SCT_TWO(sctTmpVar,__LINE__){\
static_cast<bool>(LE), RE, EVENT,\
"sctAssertLine" SCT_ONE(__LINE__), SCT_ARGS(TIMES), 3};
#else
// No parameter passed, LE/RE can use not-bound port/not-allocated pointers
#define SCT_ASSERT4(LE, TIMES, RE, EVENT)\
sct_property_mod SCT_TWO(sctTmpVar,__LINE__);
#define SCT_ASSERT4_STABLE(LE, TIMES, RE, EVENT)\
sct_property_mod SCT_TWO(sctTmpVar,__LINE__);
#define SCT_ASSERT4_ROSE(LE, TIMES, RE, EVENT)\
sct_property_mod SCT_TWO(sctTmpVar,__LINE__);
#define SCT_ASSERT4_FELL(LE, TIMES, RE, EVENT)\
sct_property_mod SCT_TWO(sctTmpVar,__LINE__);
#endif
#else
#define SCT_ASSERT4(LE, TIMES, RE, EVENT)\
sct_property_base* SCT_TWO(sctTmpVar,__LINE__) =\
sct_property_storage::getProperty(\
[&]()->bool{return ( LE );},\
[&]()->bool{return ( RE );},\
&EVENT,\
[&]()->sct_time{return (sct_time(SCT_ARGS(TIMES)));},\
std::string(__FILE__)+":"+std::to_string(__LINE__)\
);
#define SCT_ASSERT4_STABLE(LE, TIMES, RE, EVENT)\
sct_property_base* SCT_TWO(sctTmpVar,__LINE__) =\
sct_property_storage::getPropertyStable(\
[&]()->bool{return ( LE );},\
[&]()->decltype(RE){return ( RE );},\
&EVENT,\
[&]()->sct_time{return (sct_time(SCT_ARGS(TIMES)));},\
std::string(__FILE__)+":"+std::to_string(__LINE__),\
stStable\
);
#define SCT_ASSERT4_ROSE(LE, TIMES, RE, EVENT)\
sct_property_base* SCT_TWO(sctTmpVar,__LINE__) =\
sct_property_storage::getPropertyStable(\
[&]()->bool{return ( LE );},\
[&]()->decltype(RE){return ( RE );},\
&EVENT,\
[&]()->sct_time{return (sct_time(SCT_ARGS(TIMES)));},\
std::string(__FILE__)+":"+std::to_string(__LINE__),\
stRose\
);
#define SCT_ASSERT4_FELL(LE, TIMES, RE, EVENT)\
sct_property_base* SCT_TWO(sctTmpVar,__LINE__) =\
sct_property_storage::getPropertyStable(\
[&]()->bool{return ( LE );},\
[&]()->decltype(RE){return ( RE );},\
&EVENT,\
[&]()->sct_time{return (sct_time(SCT_ARGS(TIMES)));},\
std::string(__FILE__)+":"+std::to_string(__LINE__),\
stFell\
);
#endif
/// Temporal assertion in clock thread process body
#ifdef __SC_TOOL__
void sct_assert_in_proc_start() {}
template<class T1, class T2>
void sct_assert_in_proc_func(bool lhs, bool rhs,\
const char* name, T1 lotime, T2 hitime) {}
template<class T1>
void sct_assert_in_proc_func(bool lhs, bool rhs,\
const char* name, T1 time) {}
#define SCT_ASSERT4_TH(LE, TIMES, RE, EVENT) {\
sct_assert_in_proc_start();\
sct_assert_in_proc_func(\
LE, RE, "sctAssertLine" SCT_ONE(__LINE__),\
SCT_ARGS(TIMES)\
);}
#define SCT_ASSERT_LOOPN(LE, TIMES, RE, EVENT, ...) SCT_ASSERT4_TH(LE, TIMES, RE, EVENT)
#else
#define SCT_ASSERT4_TH(LE, TIMES, RE, EVENT) {\
sct_property_storage::getProperty(\
[&]()->bool{return ( LE );},\
[&]()->bool{return ( RE );},\
&EVENT,\
[&]()->sct_time{return (sct_time(SCT_ARGS(TIMES)));},\
std::string(__FILE__)+":"+std::to_string(__LINE__),\
true\
);}
/// Provide comma separated iteration variables to capture them by value
#define SCT_ITER1(ARG1) ARG1
#define SCT_ITER2(ARG1, ARG2) ARG1, ARG2
#define SCT_ITER3(ARG1, ARG2, ARG3) ARG1, ARG2, ARG3
#define SCT_ITER4(ARG1, ARG2, ARG3, ARG4) ARG1, ARG2, ARG3, ARG4
#define SCT_ITER_STR(...) SCT_GET_MACRO(__VA_ARGS__, SCT_ITER4, SCT_ITER3,\
SCT_ITER2, SCT_ITER1)(__VA_ARGS__)
/// Take variables by value, required for loop counter variable
#define SCT_ASSERT_LOOPN(LE, TIMES, RE, EVENT, ...) {\
sct_property_storage::getProperty(\
[&, SCT_ITER_STR(__VA_ARGS__)]()->bool{return ( LE );},\
[&, SCT_ITER_STR(__VA_ARGS__)]()->bool{return ( RE );},\
&EVENT,\
[&]()->sct_time{return (sct_time(SCT_ARGS(TIMES)));},\
std::string(__FILE__)+":"+std::to_string(__LINE__),\
__VA_ARGS__\
);}
#endif
/// Immediate assertion activated by event in module scope
#define SCT_ASSERT2(RE, EVENT) \
SCT_ASSERT4(true, SCT_TIME(0), RE, EVENT);
#define SCT_ASSERT2_TH(RE, EVENT) \
SCT_ASSERT4_TH(true, SCT_TIME(0), RE, EVENT);
#define SCT_ASSERT1(ARG1) \
SCT_ASSERT with 1 argument not supported;
#define SCT_ASSERT1_STABLE(ARG1) \
SCT_ASSERT_STABLE/ROSE/FELL with 1 argument not supported;
#define SCT_ASSERT2_STABLE(ARG1, ARG2) \
SCT_ASSERT_STABLE/ROSE/FELL with 2 arguments not supported;
#define SCT_ASSERT3(ARG1, ARG2, ARG3) \
SCT_ASSERT with 3 arguments not supported;
#define SCT_ASSERT3_STABLE(ARG1, ARG2, ARG3) \
SCT_ASSERT_STABLE/ROSE/FELL with 3 arguments not supported;
/// Disable SCT_ASSERT and others for HLS tools which not support it
#ifdef SCT_ASSERT_OFF
#define SCT_ASSERT(...) ;
#define SCT_ASSERT_THREAD(...) ;
#define SCT_ASSERT_LOOP(...) ;
#define SCT_ASSERT_STABLE(...) ;
#define SCT_ASSERT_ROSE(...) ;
#define SCT_ASSERT_FELL(...) ;
#else
#define SCT_ASSERT(...) SCT_GET_MACRO(__VA_ARGS__, SCT_ASSERT4, SCT_ASSERT3,\
SCT_ASSERT2, SCT_ASSERT1)(__VA_ARGS__)
#define SCT_ASSERT_THREAD(...) SCT_GET_MACRO(__VA_ARGS__, SCT_ASSERT4_TH, SCT_ASSERT3,\
SCT_ASSERT2_TH, SCT_ASSERT1)(__VA_ARGS__)
#define SCT_ASSERT_LOOP(...) SCT_ASSERT_LOOPN(__VA_ARGS__)
#define SCT_ASSERT_STABLE(...) SCT_GET_MACRO(__VA_ARGS__, \
SCT_ASSERT4_STABLE, SCT_ASSERT3_STABLE,\
SCT_ASSERT2_STABLE, SCT_ASSERT1_STABLE)(__VA_ARGS__)
#define SCT_ASSERT_ROSE(...) SCT_GET_MACRO(__VA_ARGS__, \
SCT_ASSERT4_ROSE, SCT_ASSERT3_STABLE,\
SCT_ASSERT2_STABLE, SCT_ASSERT1_STABLE)(__VA_ARGS__)
#define SCT_ASSERT_FELL(...) SCT_GET_MACRO(__VA_ARGS__, \
SCT_ASSERT4_FELL, SCT_ASSERT3_STABLE,\
SCT_ASSERT2_STABLE, SCT_ASSERT1_STABLE)(__VA_ARGS__)
#endif
//=============================================================================
// Special assertions of ISCS tool, used for tool testing
/// Latch assertion, assert that given variable, signal or port is latch or not
/// depends on second parameter
/// \param var -- variable, signal or port
/// \param latch -- assert latch if true, or not latch otherwise
template <typename T>
inline void sct_assert_latch(T& var, bool latch = true) {}
/// Check given expression is true in constant propagation analysis
#ifdef __SC_TOOL__
inline void sct_assert_const(bool expr) {
assert(expr);
}
#else
#define sct_assert_const(X) sc_assert(X)
#endif
/// Check current block level with given one
inline void sct_assert_level(unsigned level) {}
/// Check value is unknown
template <typename T>
inline void sct_assert_unknown(T v) {}
/// Check if @v is defined if @b is true or not else
template <typename T>
inline void sct_assert_defined(T& v, bool b = true) {}
/// Check if @v is read if @b is true or not else
template <typename T>
inline void sct_assert_read(T& v, bool b = true) {}
/// Check if @v is read not defined if @b is true or not else
template <typename T>
inline void sct_assert_register(T& v, bool b = true) {}
/// Check if @v is array some element of which defined at least at on path
template <typename T>
inline void sct_assert_array_defined(T& v, bool b = true) {}
//=============================================================================
// Assert that FOR/WHILE loop has at least one iteration
// Used for loop with wait() inside and non-determinable number of iterations
// which is placed in another loop without wait()
//
// Usage:
// SCT_ALIVE_LOOP(for(...){...});
// SCT_ALIVE_LOOP(while(...){...});
//
// Usage example (N is non-determinable value):
// for (int j = 0; j < M; j++) {
// SCT_ALIVE_LOOP(
// for (int i = 0; i < N; i++) {
// wait();
// });
// }
//
inline void sct_alive_loop() {}
#define SCT_ALIVE_LOOP(X) {sct_alive_loop(); X}
} // namespace sc_core
#endif /* SCT_ASSERT_H */