forked from google/swiftshader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Print.hpp
464 lines (432 loc) · 14.7 KB
/
Print.hpp
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
// Copyright 2019 The SwiftShader Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef rr_Print_hpp
#define rr_Print_hpp
#ifdef ENABLE_RR_PRINT
# include "Reactor.hpp"
# include <string>
# include <vector>
namespace rr {
// PrintValue holds the printf format and value(s) for a single argument
// to Print(). A single argument can be expanded into multiple printf
// values - for example a Float4 will expand to "%f %f %f %f" and four
// scalar values.
// The PrintValue constructor accepts the following:
// * Reactor LValues, RValues, Pointers.
// * Standard Plain-Old-Value types (int, float, bool, etc)
// * Custom types that specialize the PrintValue::Ty template struct.
// * Static arrays in the form T[N] where T can be any of the above.
class PrintValue
{
public:
// Ty is a template that can be specialized for printing type T.
// Each specialization must expose:
// * A 'static std::string fmt(const T& v)' method that provides the
// printf format specifier.
// * A 'static std::vector<rr::Value*> val(const T& v)' method that
// returns all the printf format values.
template<typename T>
struct Ty
{
// static std::string fmt(const T& v);
// static std::vector<rr::Value*> val(const T& v);
};
// returns the printf values for all the values in the given array.
template<typename T>
static std::vector<Value *> val(const T *list, int count)
{
std::vector<Value *> values;
values.reserve(count);
for(int i = 0; i < count; i++)
{
auto v = val(list[i]);
values.insert(values.end(), v.begin(), v.end());
}
return values;
}
// fmt returns the comma-delimited list of printf format strings for
// every element in the provided list, all enclosed in square brackets.
template<typename T>
static std::string fmt(const T *list, int count)
{
std::string out = "[";
for(int i = 0; i < count; i++)
{
if(i > 0) { out += ", "; }
out += fmt(list[i]);
}
return out + "]";
}
static std::string addr(const void *ptr)
{
char buf[32];
snprintf(buf, sizeof(buf), "%p", ptr);
return buf;
}
const std::string format;
const std::vector<Value *> values;
// Constructs a PrintValue for the given value.
template<typename T>
PrintValue(const T &v)
: format(fmt(v))
, values(val(v))
{}
// Constructs a PrintValue for the given static array.
template<typename T, int N>
PrintValue(const T (&v)[N])
: format(fmt(&v[0], N))
, values(val(&v[0], N))
{}
// Constructs a PrintValue for the given array starting at arr of length
// len.
template<typename T>
PrintValue(const T *arr, int len)
: format(fmt(arr, len))
, values(val(arr, len))
{}
// PrintValue constructors for plain-old-data values.
PrintValue(bool v)
: format(v ? "true" : "false")
{}
PrintValue(int8_t v)
: format(std::to_string(v))
{}
PrintValue(uint8_t v)
: format(std::to_string(v))
{}
PrintValue(int16_t v)
: format(std::to_string(v))
{}
PrintValue(uint16_t v)
: format(std::to_string(v))
{}
PrintValue(int32_t v)
: format(std::to_string(v))
{}
PrintValue(uint32_t v)
: format(std::to_string(v))
{}
PrintValue(int64_t v)
: format(std::to_string(v))
{}
PrintValue(uint64_t v)
: format(std::to_string(v))
{}
PrintValue(float v)
: format(std::to_string(v))
{}
PrintValue(double v)
: format(std::to_string(v))
{}
PrintValue(const char *v)
: format(v)
{}
template<typename T>
PrintValue(T *v)
: format(addr(v))
{}
// vals is a helper to build composite value lists.
// vals returns the full, sequential list of printf argument values used
// to print all the provided variadic values.
// vals() is intended to be used by implementations of
// PrintValue::Ty<>::vals() to help declare aggregate types.
// For example, if you were declaring a PrintValue::Ty<> specialization
// for a custom Mat4x4 matrix formed from four Vector4 values, you'd
// write:
//
// namespace rr
// {
// template <> struct PrintValue::Ty<Mat4x4>
// {
// static std::string fmt(const Mat4x4& v)
// {
// return "[a: <%f, %f, %f, %f>,"
// " b: <%f, %f, %f, %f>,"
// " c: <%f, %f, %f, %f>,"
// " d: <%f, %f, %f, %f>]";
// }
// static std::vector<rr::Value*> val(const Mat4x4& v)
// {
// return PrintValue::vals(v.a, v.b, v.c, v.d);
// }
// };
// }
template<typename... ARGS>
static std::vector<Value *> vals(ARGS... v)
{
std::vector<std::vector<Value *>> lists = { val(v)... };
std::vector<Value *> joined;
for(const auto &list : lists)
{
joined.insert(joined.end(), list.begin(), list.end());
}
return joined;
}
// returns the printf format specifier for the given type via the
// PrintValue::Ty<T> specialization.
template<typename T>
static std::string fmt(const T &v)
{
return Ty<T>::fmt(v);
}
// returns the printf value for the given type with a
// PrintValue::Ty<T> specialization.
template<typename T>
static std::vector<Value *> val(const T &v)
{
return Ty<T>::val(v);
}
};
// PrintValue::Ty<T> specializations for basic types.
template<>
struct PrintValue::Ty<const char *>
{
static std::string fmt(const char *v) { return "%s"; }
static std::vector<Value *> val(const char *v);
};
template<>
struct PrintValue::Ty<std::string>
{
static std::string fmt(const std::string &v) { return PrintValue::Ty<const char *>::fmt(v.c_str()); }
static std::vector<Value *> val(const std::string &v) { return PrintValue::Ty<const char *>::val(v.c_str()); }
};
// PrintValue::Ty<T> specializations for standard Reactor types.
template<>
struct PrintValue::Ty<Bool>
{
static std::string fmt(const RValue<Bool> &v) { return "%s"; }
static std::vector<Value *> val(const RValue<Bool> &v);
};
template<>
struct PrintValue::Ty<Byte>
{
static std::string fmt(const RValue<Byte> &v) { return "%d"; }
static std::vector<Value *> val(const RValue<Byte> &v);
};
template<>
struct PrintValue::Ty<Byte4>
{
static std::string fmt(const RValue<Byte4> &v) { return "[%d, %d, %d, %d]"; }
static std::vector<Value *> val(const RValue<Byte4> &v);
};
template<>
struct PrintValue::Ty<Int>
{
static std::string fmt(const RValue<Int> &v) { return "%d"; }
static std::vector<Value *> val(const RValue<Int> &v);
};
template<>
struct PrintValue::Ty<Int2>
{
static std::string fmt(const RValue<Int2> &v) { return "[%d, %d]"; }
static std::vector<Value *> val(const RValue<Int2> &v);
};
template<>
struct PrintValue::Ty<Int4>
{
static std::string fmt(const RValue<Int4> &v) { return "[%d, %d, %d, %d]"; }
static std::vector<Value *> val(const RValue<Int4> &v);
};
template<>
struct PrintValue::Ty<UInt>
{
static std::string fmt(const RValue<UInt> &v) { return "%u"; }
static std::vector<Value *> val(const RValue<UInt> &v);
};
template<>
struct PrintValue::Ty<UInt2>
{
static std::string fmt(const RValue<UInt2> &v) { return "[%u, %u]"; }
static std::vector<Value *> val(const RValue<UInt2> &v);
};
template<>
struct PrintValue::Ty<UInt4>
{
static std::string fmt(const RValue<UInt4> &v) { return "[%u, %u, %u, %u]"; }
static std::vector<Value *> val(const RValue<UInt4> &v);
};
template<>
struct PrintValue::Ty<Short>
{
static std::string fmt(const RValue<Short> &v) { return "%d"; }
static std::vector<Value *> val(const RValue<Short> &v);
};
template<>
struct PrintValue::Ty<Short4>
{
static std::string fmt(const RValue<Short4> &v) { return "[%d, %d, %d, %d]"; }
static std::vector<Value *> val(const RValue<Short4> &v);
};
template<>
struct PrintValue::Ty<UShort>
{
static std::string fmt(const RValue<UShort> &v) { return "%u"; }
static std::vector<Value *> val(const RValue<UShort> &v);
};
template<>
struct PrintValue::Ty<UShort4>
{
static std::string fmt(const RValue<UShort4> &v) { return "[%u, %u, %u, %u]"; }
static std::vector<Value *> val(const RValue<UShort4> &v);
};
template<>
struct PrintValue::Ty<Float>
{
static std::string fmt(const RValue<Float> &v) { return "%f"; }
static std::vector<Value *> val(const RValue<Float> &v);
};
template<>
struct PrintValue::Ty<Float4>
{
static std::string fmt(const RValue<Float4> &v) { return "[%f, %f, %f, %f]"; }
static std::vector<Value *> val(const RValue<Float4> &v);
};
template<>
struct PrintValue::Ty<Long>
{
static std::string fmt(const RValue<Long> &v) { return "%lld"; }
static std::vector<Value *> val(const RValue<Long> &v) { return { v.value() }; }
};
template<typename T>
struct PrintValue::Ty<Pointer<T>>
{
static std::string fmt(const RValue<Pointer<T>> &v) { return "%p"; }
static std::vector<Value *> val(const RValue<Pointer<T>> &v) { return { v.value() }; }
};
template<typename T>
struct PrintValue::Ty<Reference<T>>
{
static std::string fmt(const Reference<T> &v) { return PrintValue::Ty<T>::fmt(v); }
static std::vector<Value *> val(const Reference<T> &v) { return PrintValue::Ty<T>::val(v); }
};
template<typename T>
struct PrintValue::Ty<RValue<T>>
{
static std::string fmt(const RValue<T> &v) { return PrintValue::Ty<T>::fmt(v); }
static std::vector<Value *> val(const RValue<T> &v) { return PrintValue::Ty<T>::val(v); }
};
// VPrintf emits a call to printf() using vals[0] as the format string,
// and vals[1..n] as the args.
void VPrintf(const std::vector<Value *> &vals);
// Printv emits a call to printf() using the function, file and line,
// message and optional values.
// See Printv below.
void Printv(const char *function, const char *file, int line, const char *msg, std::initializer_list<PrintValue> vals);
// Printv emits a call to printf() using the provided message and optional
// values.
// Printf replaces any bracketed indices in the message with string
// representations of the corresponding value in vals.
// For example:
// Printv("{0} and {1}", "red", "green");
// Would print the string:
// "red and green"
// Arguments can be indexed in any order.
// Invalid indices are not substituted.
inline void Printv(const char *msg, std::initializer_list<PrintValue> vals)
{
Printv(nullptr, nullptr, 0, msg, vals);
}
// Print is a wrapper over Printv that wraps the variadic arguments into an
// initializer_list before calling Printv.
template<typename... ARGS>
void Print(const char *msg, const ARGS &... vals)
{
Printv(msg, { vals... });
}
// Print is a wrapper over Printv that wraps the variadic arguments into an
// initializer_list before calling Printv.
template<typename... ARGS>
void Print(const char *function, const char *file, int line, const char *msg, const ARGS &... vals)
{
Printv(function, file, line, msg, { vals... });
}
// RR_LOG is a macro that calls Print(), automatically populating the
// function, file and line parameters and appending a newline to the string.
//
// RR_LOG() is intended to be used for debugging JIT compiled code, and is
// not intended for production use.
# if defined(_WIN32)
# define RR_LOG(msg, ...) Print(__FUNCSIG__, __FILE__, static_cast<int>(__LINE__), msg "\n", ##__VA_ARGS__)
# else
# define RR_LOG(msg, ...) Print(__PRETTY_FUNCTION__, __FILE__, static_cast<int>(__LINE__), msg "\n", ##__VA_ARGS__)
# endif
// Macro magic to perform variadic dispatch.
// See: https://renenyffenegger.ch/notes/development/languages/C-C-plus-plus/preprocessor/macros/__VA_ARGS__/count-arguments
// Note, this doesn't attempt to use the ##__VA_ARGS__ trick to handle 0
# define RR_MSVC_EXPAND_BUG(X) X // Helper macro to force expanding __VA_ARGS__ to satisfy MSVC compiler.
# define RR_GET_NTH_ARG(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, N, ...) N
# define RR_COUNT_ARGUMENTS(...) RR_MSVC_EXPAND_BUG(RR_GET_NTH_ARG(__VA_ARGS__, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0))
static_assert(1 == RR_COUNT_ARGUMENTS(a), "RR_COUNT_ARGUMENTS broken"); // Sanity checks.
static_assert(2 == RR_COUNT_ARGUMENTS(a, b), "RR_COUNT_ARGUMENTS broken");
static_assert(3 == RR_COUNT_ARGUMENTS(a, b, c), "RR_COUNT_ARGUMENTS broken");
// RR_WATCH_FMT(...) resolves to a string literal that lists all the
// arguments by name. This string can be passed to LOG() to print each of
// the arguments with their name and value.
//
// RR_WATCH_FMT(...) uses the RR_COUNT_ARGUMENTS helper macro to delegate to a
// corresponding RR_WATCH_FMT_n specialization macro below.
# define RR_WATCH_CONCAT(a, b) a##b
# define RR_WATCH_CONCAT2(a, b) RR_WATCH_CONCAT(a, b)
# define RR_WATCH_FMT(...) RR_MSVC_EXPAND_BUG(RR_WATCH_CONCAT2(RR_WATCH_FMT_, RR_COUNT_ARGUMENTS(__VA_ARGS__))(__VA_ARGS__))
# define RR_WATCH_FMT_1(_1) "\n " # _1 ": {0}"
# define RR_WATCH_FMT_2(_1, _2) \
RR_WATCH_FMT_1(_1) \
"\n " #_2 ": {1}"
# define RR_WATCH_FMT_3(_1, _2, _3) \
RR_WATCH_FMT_2(_1, _2) \
"\n " #_3 ": {2}"
# define RR_WATCH_FMT_4(_1, _2, _3, _4) \
RR_WATCH_FMT_3(_1, _2, _3) \
"\n " #_4 ": {3}"
# define RR_WATCH_FMT_5(_1, _2, _3, _4, _5) \
RR_WATCH_FMT_4(_1, _2, _3, _4) \
"\n " #_5 ": {4}"
# define RR_WATCH_FMT_6(_1, _2, _3, _4, _5, _6) \
RR_WATCH_FMT_5(_1, _2, _3, _4, _5) \
"\n " #_6 ": {5}"
# define RR_WATCH_FMT_7(_1, _2, _3, _4, _5, _6, _7) \
RR_WATCH_FMT_6(_1, _2, _3, _4, _5, _6) \
"\n " #_7 ": {6}"
# define RR_WATCH_FMT_8(_1, _2, _3, _4, _5, _6, _7, _8) \
RR_WATCH_FMT_7(_1, _2, _3, _4, _5, _6, _7) \
"\n " #_8 ": {7}"
# define RR_WATCH_FMT_9(_1, _2, _3, _4, _5, _6, _7, _8, _9) \
RR_WATCH_FMT_8(_1, _2, _3, _4, _5, _6, _7, _8) \
"\n " #_9 ": {8}"
# define RR_WATCH_FMT_10(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10) \
RR_WATCH_FMT_9(_1, _2, _3, _4, _5, _6, _7, _8, _9) \
"\n " #_10 ": {9}"
# define RR_WATCH_FMT_11(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11) \
RR_WATCH_FMT_10(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10) \
"\n " #_11 ": {10}"
# define RR_WATCH_FMT_12(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12) \
RR_WATCH_FMT_11(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11) \
"\n " #_12 ": {11}"
// RR_WATCH() is a helper that prints the name and value of all the supplied
// arguments.
// For example, if you had the Int and bool variables 'foo' and 'bar' that
// you want to print, you can simply write:
// RR_WATCH(foo, bar)
// When this JIT compiled code is executed, it will print the string
// "foo: 1, bar: true" to stdout.
//
// RR_WATCH() is intended to be used for debugging JIT compiled code, and
// is not intended for production use.
# define RR_WATCH(...) RR_LOG(RR_WATCH_FMT(__VA_ARGS__), __VA_ARGS__)
} // namespace rr
# define RR_PRINT_ONLY(x) x
#else
# define RR_PRINT_ONLY(x)
#endif // ENABLE_RR_PRINT
#endif // rr_Print_hpp