21
21
// / \brief State access interface
22
22
23
23
#include < cinttypes>
24
+ #include < cstdarg>
24
25
#include < cstdint>
25
26
#include < type_traits>
26
27
#include < utility>
27
28
28
- #include " dump .h"
29
+ #include " assert-printf .h"
29
30
#include " i-prefer-shadow-state.h"
30
31
#include " meta.h"
31
32
#include " poor-type-name.h"
@@ -47,7 +48,7 @@ using i_state_access_fast_addr_t = typename i_state_access_fast_addr<STATE_ACCES
47
48
uint64_t read_##REG() const { \
48
49
if constexpr (!is_an_i_prefer_shadow_state_v<DERIVED>) { \
49
50
const auto val = derived ().do_read_ ##REG (); \
50
- DSA_PRINTF (" %s::read_" #REG " () = %" PRIu64 " (0x%" PRIx64 " )\n " , get_name (), val, val); \
51
+ dsa_printf (" %s::read_" #REG " () = %" PRIu64 " (0x%" PRIx64 " )\n " , get_name (), val, val); \
51
52
return val; \
52
53
} else { \
53
54
return prefer_read_shadow_state (shadow_state_what::REG); \
@@ -58,7 +59,7 @@ using i_state_access_fast_addr_t = typename i_state_access_fast_addr<STATE_ACCES
58
59
void write_##REG(uint64_t val) const { \
59
60
if constexpr (!is_an_i_prefer_shadow_state_v<DERIVED>) { \
60
61
derived ().do_write_ ##REG (val); \
61
- DSA_PRINTF (" %s::write_" #REG " (%" PRIu64 " (0x%" PRIx64 " ))\n " , get_name (), val, val); \
62
+ dsa_printf (" %s::write_" #REG " (%" PRIu64 " (0x%" PRIx64 " ))\n " , get_name (), val, val); \
62
63
} else { \
63
64
prefer_write_shadow_state (shadow_state_what::REG, val); \
64
65
} \
@@ -104,24 +105,35 @@ class i_state_access { // CRTP
104
105
uint64_t prefer_read_shadow_state (shadow_state_what what) const {
105
106
const auto val = derived ().read_shadow_state (what);
106
107
[[maybe_unused]] const auto *const what_name = shadow_state_get_what_name (what);
107
- DSA_PRINTF (" %s::read_shadow_state(%s) = %" PRIu64 " (0x%" PRIx64 " )\n " , get_name (), what_name, val, val);
108
+ dsa_printf (" %s::read_shadow_state(%s) = %" PRIu64 " (0x%" PRIx64 " )\n " , get_name (), what_name, val, val);
108
109
return val;
109
110
}
110
111
111
112
void prefer_write_shadow_state (shadow_state_what what, uint64_t val) const {
112
113
derived ().write_shadow_state (what, val);
113
114
[[maybe_unused]] const auto *const what_name = shadow_state_get_what_name (what);
114
- DSA_PRINTF (" %s::write_shadow_state(%s, %" PRIu64 " (0x%" PRIx64 " ))\n " , get_name (), what_name, val, val);
115
+ dsa_printf (" %s::write_shadow_state(%s, %" PRIu64 " (0x%" PRIx64 " ))\n " , get_name (), what_name, val, val);
115
116
}
116
117
117
118
public:
118
119
using fast_addr = i_state_access_fast_addr_t <DERIVED>;
119
120
121
+ // / \brief Works as vprintf if we are dumping state accesses, otherwise does nothing
122
+ static void dsa_vprintf ([[maybe_unused]] const char *fmt, [[maybe_unused]] va_list ap) {
123
+ #ifdef DUMP_STATE_ACCESS
124
+ d_vprintf (fmt, ap);
125
+ #endif
126
+ }
127
+
120
128
// / \brief Works as printf if we are dumping state accesses, otherwise does nothing
121
- template <size_t N, typename ... ARGS>
122
- static void DSA_PRINTF ([[maybe_unused]] const char (&fmt)[N], [[maybe_unused]] ARGS... args) {
129
+ // Better to use C-style variadic function that checks for format!
130
+ // NOLINTNEXTLINE(cert-dcl50-cpp)
131
+ __attribute__ ((__format__(__printf__, 1 , 2 ))) static void dsa_printf ([[maybe_unused]] const char *fmt, ...) {
123
132
#ifdef DUMP_STATE_ACCESS
124
- D_PRINTF (fmt, args...);
133
+ va_list ap;
134
+ va_start (ap, fmt);
135
+ dsa_vprintf (fmt, ap);
136
+ va_end (ap);
125
137
#endif
126
138
}
127
139
@@ -131,7 +143,7 @@ class i_state_access { // CRTP
131
143
uint64_t read_x (int i) const {
132
144
if constexpr (!is_an_i_prefer_shadow_state_v<DERIVED>) {
133
145
const auto val = derived ().do_read_x (i);
134
- DSA_PRINTF (" %s::read_x(%d) = %" PRIu64 " (0x%" PRIx64 " )\n " , get_name (), i, val, val);
146
+ dsa_printf (" %s::read_x(%d) = %" PRIu64 " (0x%" PRIx64 " )\n " , get_name (), i, val, val);
135
147
return val;
136
148
} else {
137
149
return prefer_read_shadow_state (shadow_state_get_what (shadow_state_what::x0, i));
@@ -146,7 +158,7 @@ class i_state_access { // CRTP
146
158
void write_x (int i, uint64_t val) const {
147
159
if constexpr (!is_an_i_prefer_shadow_state_v<DERIVED>) {
148
160
derived ().do_write_x (i, val);
149
- DSA_PRINTF (" %s::write_x(%d, %" PRIu64 " (0x%" PRIx64 " ))\n " , get_name (), i, val, val);
161
+ dsa_printf (" %s::write_x(%d, %" PRIu64 " (0x%" PRIx64 " ))\n " , get_name (), i, val, val);
150
162
} else {
151
163
prefer_write_shadow_state (shadow_state_get_what (shadow_state_what::x0, i), val);
152
164
}
@@ -158,7 +170,7 @@ class i_state_access { // CRTP
158
170
uint64_t read_f (int i) const {
159
171
if constexpr (!is_an_i_prefer_shadow_state_v<DERIVED>) {
160
172
const auto val = derived ().do_read_f (i);
161
- DSA_PRINTF (" %s::read_f(%d) = %" PRIu64 " (0x%" PRIx64 " )\n " , get_name (), i, val, val);
173
+ dsa_printf (" %s::read_f(%d) = %" PRIu64 " (0x%" PRIx64 " )\n " , get_name (), i, val, val);
162
174
return val;
163
175
} else {
164
176
return prefer_read_shadow_state (shadow_state_get_what (shadow_state_what::f0, i));
@@ -171,7 +183,7 @@ class i_state_access { // CRTP
171
183
void write_f (int i, uint64_t val) const {
172
184
if constexpr (!is_an_i_prefer_shadow_state_v<DERIVED>) {
173
185
derived ().do_write_f (i, val);
174
- DSA_PRINTF (" %s::write_f(%d, %" PRIu64 " (%" PRIx64 " ))\n " , get_name (), i, val, val);
186
+ dsa_printf (" %s::write_f(%d, %" PRIu64 " (%" PRIx64 " ))\n " , get_name (), i, val, val);
175
187
} else {
176
188
prefer_write_shadow_state (shadow_state_get_what (shadow_state_what::f0, i), val);
177
189
}
@@ -269,7 +281,7 @@ class i_state_access { // CRTP
269
281
// / \param index Index of PMA
270
282
address_range &read_pma (uint64_t index) const {
271
283
auto &ar = derived ().do_read_pma (index );
272
- DSA_PRINTF (" %s::read_address_range(%" PRIu64 " ) = {%s, 0x%" PRIx64 " , 0x%" PRIx64 " }\n " , get_name (), index ,
284
+ dsa_printf (" %s::read_address_range(%" PRIu64 " ) = {%s, 0x%" PRIx64 " , 0x%" PRIx64 " }\n " , get_name (), index ,
273
285
pmas_get_DID_name (ar.get_driver_id ()), ar.get_start (), ar.get_length ());
274
286
return ar;
275
287
}
@@ -281,8 +293,8 @@ class i_state_access { // CRTP
281
293
fast_addr get_faddr (uint64_t paddr, uint64_t pma_index) const {
282
294
const auto val = derived ().do_get_faddr (paddr, pma_index);
283
295
[[maybe_unused]] const auto fast_addr_name = std::is_same_v<fast_addr, uint64_t > ? " phys_addr" : " fast_addr" ;
284
- DSA_PRINTF (" %s::get_faddr(%" PRIu64 " (0x%" PRIx64 " )) = %s{%" PRIu64 " (0x%" PRIx64 " )}\n " , get_name (), paddr,
285
- paddr, fast_addr_name, val, val);
296
+ dsa_printf (" %s::get_faddr(%" PRIu64 " (0x%" PRIx64 " )) = %s{%" PRIu64 " (0x%" PRIx64 " )}\n " , get_name (), paddr,
297
+ paddr, fast_addr_name, static_cast < uint64_t >( val), static_cast < uint64_t >( val) );
286
298
return val;
287
299
}
288
300
@@ -331,9 +343,9 @@ class i_state_access { // CRTP
331
343
static_assert (std::is_integral_v<T> && sizeof (T) <= sizeof (uint64_t ), " unsupported type" );
332
344
derived ().template do_read_memory_word <T, A>(faddr, pma_index, pval);
333
345
[[maybe_unused]] const auto fast_addr_name = std::is_same_v<fast_addr, uint64_t > ? " phys_addr" : " fast_addr" ;
334
- DSA_PRINTF (" %s::read_memory_word<%s,%s>(%s{0x%" PRIx64 " }, %" PRIu64 " ) = %" PRIu64 " (0x%" PRIx64 " )\n " ,
335
- get_name (), poor_type_name_v<T>, poor_type_name_v<A>, fast_addr_name, faddr, pma_index ,
336
- static_cast <uint64_t >(*pval), static_cast <uint64_t >(*pval));
346
+ dsa_printf (" %s::read_memory_word<%s,%s>(%s{0x%" PRIx64 " }, %" PRIu64 " ) = %" PRIu64 " (0x%" PRIx64 " )\n " ,
347
+ get_name (), poor_type_name_v<T>, poor_type_name_v<A>, fast_addr_name, static_cast < uint64_t >( faddr) ,
348
+ pma_index, static_cast <uint64_t >(*pval), static_cast <uint64_t >(*pval));
337
349
}
338
350
339
351
// / \brief Writes a word to memory.
@@ -348,9 +360,9 @@ class i_state_access { // CRTP
348
360
static_assert (std::is_integral_v<T> && sizeof (T) <= sizeof (uint64_t ), " unsupported type" );
349
361
derived ().template do_write_memory_word <T, A>(faddr, pma_index, val);
350
362
[[maybe_unused]] const auto fast_addr_name = std::is_same_v<fast_addr, uint64_t > ? " phys_addr" : " fast_addr" ;
351
- DSA_PRINTF (" %s::write_memory_word<%s,%s>(%s{0x%" PRIx64 " }, %" PRIu64 " , %" PRIu64 " (0x%" PRIx64 " ))\n " ,
352
- get_name (), poor_type_name_v<T>, poor_type_name_v<A>, fast_addr_name, faddr, pma_index ,
353
- static_cast <uint64_t >(val), static_cast <uint64_t >(val));
363
+ dsa_printf (" %s::write_memory_word<%s,%s>(%s{0x%" PRIx64 " }, %" PRIu64 " , %" PRIu64 " (0x%" PRIx64 " ))\n " ,
364
+ get_name (), poor_type_name_v<T>, poor_type_name_v<A>, fast_addr_name, static_cast < uint64_t >( faddr) ,
365
+ pma_index, static_cast <uint64_t >(val), static_cast <uint64_t >(val));
354
366
}
355
367
356
368
// / \brief Reads TLB's vaddr_page
@@ -360,7 +372,7 @@ class i_state_access { // CRTP
360
372
template <TLB_set_index SET>
361
373
uint64_t read_tlb_vaddr_page (uint64_t slot_index) const {
362
374
const auto val = derived ().template do_read_tlb_vaddr_page <SET>(slot_index);
363
- DSA_PRINTF (" %s::read_tlb_vaddr_page<%" PRIu64 " >(%" PRIu64 " ) = 0x%" PRIx64 " \n " , get_name (), SET, slot_index,
375
+ dsa_printf (" %s::read_tlb_vaddr_page<%" PRIu64 " >(%" PRIu64 " ) = 0x%" PRIx64 " \n " , get_name (), SET, slot_index,
364
376
val);
365
377
return val;
366
378
}
@@ -373,8 +385,8 @@ class i_state_access { // CRTP
373
385
fast_addr read_tlb_vp_offset (uint64_t slot_index) const {
374
386
[[maybe_unused]] const auto fast_addr_name = std::is_same_v<fast_addr, uint64_t > ? " phys_addr" : " fast_addr" ;
375
387
const auto val = derived ().template do_read_tlb_vp_offset <SET>(slot_index);
376
- DSA_PRINTF (" %s::read_tlb_vp_offset<%" PRIu64 " >(%" PRIu64 " ) = %s{0x%" PRIx64 " }\n " , get_name (), SET,
377
- slot_index, fast_addr_name, val);
388
+ dsa_printf (" %s::read_tlb_vp_offset<%" PRIu64 " >(%" PRIu64 " ) = %s{0x%" PRIx64 " }\n " , get_name (), SET,
389
+ slot_index, fast_addr_name, static_cast < uint64_t >( val) );
378
390
return val;
379
391
}
380
392
@@ -385,7 +397,7 @@ class i_state_access { // CRTP
385
397
template <TLB_set_index SET>
386
398
uint64_t read_tlb_pma_index (uint64_t slot_index) const {
387
399
const auto val = derived ().template do_read_tlb_pma_index <SET>(slot_index);
388
- DSA_PRINTF (" %s::read_tlb_pma_index<%" PRIu64 " >(%" PRIu64 " ) = %" PRIu64 " (0x%" PRIx64 " )\n " , get_name (), SET,
400
+ dsa_printf (" %s::read_tlb_pma_index<%" PRIu64 " >(%" PRIu64 " ) = %" PRIu64 " (0x%" PRIx64 " )\n " , get_name (), SET,
389
401
slot_index, val, val);
390
402
return val;
391
403
}
@@ -402,8 +414,8 @@ class i_state_access { // CRTP
402
414
void write_tlb (uint64_t slot_index, uint64_t vaddr_page, fast_addr vp_offset, uint64_t pma_index) const {
403
415
derived ().template do_write_tlb <SET>(slot_index, vaddr_page, vp_offset, pma_index);
404
416
[[maybe_unused]] const auto fast_addr_name = std::is_same_v<fast_addr, uint64_t > ? " phys_addr" : " fast_addr" ;
405
- DSA_PRINTF (" %s::write_tlb<%" PRIu64 " >(%" PRIu64 " , 0x%" PRIx64 " , %s{0x%" PRIx64 " }, %" PRIu64 " )\n " ,
406
- get_name (), SET, slot_index, vaddr_page, fast_addr_name, vp_offset, pma_index);
417
+ dsa_printf (" %s::write_tlb<%" PRIu64 " >(%" PRIu64 " , 0x%" PRIx64 " , %s{0x%" PRIx64 " }, %" PRIu64 " )\n " ,
418
+ get_name (), SET, slot_index, vaddr_page, fast_addr_name, static_cast < uint64_t >( vp_offset) , pma_index);
407
419
}
408
420
409
421
// / \brief Marks a page as dirty
0 commit comments