This repository has been archived by the owner on Mar 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 176
/
Copy pathRecordLayout.h
529 lines (465 loc) · 18.1 KB
/
RecordLayout.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
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
//===--- RecordLayout.h - Convenience wrappers for bitcode ------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
///
/// \file Convenience wrappers for the LLVM bitcode format and bitstream APIs.
///
/// This allows you to use a sort of DSL to declare and use bitcode abbrevs
/// and records. Example:
///
/// \code
/// using Metadata = BCRecordLayout<
/// METADATA_ID, // ID
/// BCFixed<16>, // Module format major version
/// BCFixed<16>, // Module format minor version
/// BCBlob // misc. version information
/// >;
/// unsigned MetadataAbbrevCode = Metadata::emitAbbrev(Out);
/// Metadata::emitRecord(Out, ScratchRecord, MetadataAbbrevCode,
/// VERSION_MAJOR, VERSION_MINOR, extraData);
/// \endcode
///
/// For details on the bitcode format, see
/// http://llvm.org/docs/BitCodeFormat.html
///
//===----------------------------------------------------------------------===//
#ifndef LLVM_BITCODE_RECORDLAYOUT_H
#define LLVM_BITCODE_RECORDLAYOUT_H
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Bitcode/BitCodes.h"
#include "llvm/Bitcode/BitstreamWriter.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/MathExtras.h"
namespace llvm {
namespace impl {
/// Convenience base for all kinds of bitcode abbreviation fields.
///
/// This just defines common properties queried by the metaprogramming.
template<bool COMPOUND = false>
class BCField {
public:
static const bool IS_COMPOUND = COMPOUND;
/// Asserts that the given data is a valid value for this field.
template<typename T>
static void assertValid(const T &data) {}
/// Converts a raw numeric representation of this value to its preferred
/// type.
template<typename T>
static T convert(T rawValue) {
return rawValue;
}
};
} // end namespace impl
/// Represents a literal operand in a bitcode record.
///
/// The value of a literal operand is the same for all instances of the record,
/// so it is only emitted in the abbreviation definition.
///
/// Note that because this uses a compile-time template, you cannot have a
/// literal operand that is fixed at run-time without dropping down to the
/// raw LLVM APIs.
template<uint64_t Value>
class BCLiteral : public impl::BCField<> {
public:
static void emitOp(llvm::BitCodeAbbrev &abbrev) {
abbrev.Add(llvm::BitCodeAbbrevOp(Value));
}
template<typename T>
static void assertValid(const T &data) {
assert(data == Value && "data value does not match declared literal value");
}
};
/// Represents a fixed-width value in a bitcode record.
///
/// Note that the LLVM bitcode format only supports unsigned values.
template<unsigned Width>
class BCFixed : public impl::BCField<> {
public:
static_assert(Width <= 64, "fixed-width field is too large");
static void emitOp(llvm::BitCodeAbbrev &abbrev) {
abbrev.Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, Width));
}
template<typename T>
static void assertValid(const T &data) {
assert(data >= 0 && "cannot encode signed integers");
assert(llvm::isUInt<Width>(data) &&
"data value does not fit in the given bit width");
}
};
/// Represents a variable-width value in a bitcode record.
///
/// The \p Width parameter should include the continuation bit.
///
/// Note that the LLVM bitcode format only supports unsigned values.
template<unsigned Width>
class BCVBR : public impl::BCField<> {
static_assert(Width >= 2, "width does not have room for continuation bit");
public:
static void emitOp(llvm::BitCodeAbbrev &abbrev) {
abbrev.Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, Width));
}
template<typename T>
static void assertValid(const T &data) {
assert(data >= 0 && "cannot encode signed integers");
}
};
/// Represents a character encoded in LLVM's Char6 encoding.
///
/// This format is suitable for encoding decimal numbers (without signs or
/// exponents) and C identifiers (without dollar signs), but not much else.
///
/// \sa http://llvm.org/docs/BitCodeFormat.html#char6-encoded-value
class BCChar6 : public impl::BCField<> {
public:
static void emitOp(llvm::BitCodeAbbrev &abbrev) {
abbrev.Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Char6));
}
template<typename T>
static void assertValid(const T &data) {
assert(llvm::BitCodeAbbrevOp::isChar6(data) && "invalid Char6 data");
}
template<typename T>
char convert(T rawValue) {
return static_cast<char>(rawValue);
}
};
/// Represents an untyped blob of bytes.
///
/// If present, this must be the last field in a record.
class BCBlob : public impl::BCField<true> {
public:
static void emitOp(llvm::BitCodeAbbrev &abbrev) {
abbrev.Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
}
};
/// Represents an array of some other type.
///
/// If present, this must be the last field in a record.
template<typename Element>
class BCArray : public impl::BCField<true> {
static_assert(!Element::IS_COMPOUND, "arrays can only contain scalar types");
public:
static void emitOp(llvm::BitCodeAbbrev &abbrev) {
abbrev.Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Array));
Element::emitOp(abbrev);
}
};
namespace impl {
/// Attaches the last field to an abbreviation.
///
/// This is the base case for \c emitOps.
///
/// \sa BCRecordLayout::emitAbbrev
template<typename Last>
static void emitOps(llvm::BitCodeAbbrev &abbrev) {
Last::emitOp(abbrev);
}
/// Attaches fields to an abbreviation.
///
/// This is the recursive case for \c emitOps.
///
/// \sa BCRecordLayout::emitAbbrev
template<typename First, typename Next, typename ...Rest>
static void emitOps(llvm::BitCodeAbbrev &abbrev) {
static_assert(!First::IS_COMPOUND,
"arrays and blobs may not appear in the middle of a record");
First::emitOp(abbrev);
emitOps<Next, Rest...>(abbrev);
}
/// Helper class for dealing with a scalar element in the middle of a record.
///
/// \sa BCRecordLayout
template<typename First, typename... Fields>
class BCRecordCoding {
public:
template <typename BufferTy, typename FirstData, typename... Data>
static void emit(llvm::BitstreamWriter &out, BufferTy &buffer,
unsigned abbrCode, FirstData data, Data... rest) {
static_assert(!First::IS_COMPOUND,
"arrays and blobs may not appear in the middle of a record");
First::assertValid(data);
buffer.push_back(data);
BCRecordCoding<Fields...>::emit(out, buffer, abbrCode, rest...);
}
template <typename ElementTy, typename FirstData, typename... Data>
static void read(ArrayRef<ElementTy> buffer,
FirstData &data, Data &&...rest) {
assert(!buffer.empty() && "too few elements in buffer");
data = First::convert(buffer.front());
BCRecordCoding<Fields...>::read(buffer.slice(1),
std::forward<Data>(rest)...);
}
template <typename ElementTy, typename... Data>
static void read(ArrayRef<ElementTy> buffer,
NoneType, Data &&...rest) {
assert(!buffer.empty() && "too few elements in buffer");
BCRecordCoding<Fields...>::read(buffer.slice(1),
std::forward<Data>(rest)...);
}
};
/// Helper class for dealing with a scalar element at the end of a record.
///
/// This has a separate implementation because up until now we've only been
/// \em building the record (into a data buffer), and now we need to hand it
/// off to the BitstreamWriter to be emitted.
///
/// \sa BCRecordLayout
template<typename Last>
class BCRecordCoding<Last> {
public:
template <typename BufferTy, typename LastData>
static void emit(llvm::BitstreamWriter &out, BufferTy &buffer,
unsigned abbrCode, LastData data) {
static_assert(!Last::IS_COMPOUND,
"arrays and blobs need special handling");
Last::assertValid(data);
buffer.push_back(data);
out.EmitRecordWithAbbrev(abbrCode, buffer);
}
template <typename ElementTy, typename LastData>
static void read(ArrayRef<ElementTy> buffer, LastData &data) {
assert(buffer.size() == 1 && "record data does not match layout");
data = Last::convert(buffer.front());
}
template <typename ElementTy>
static void read(ArrayRef<ElementTy> buffer, NoneType) {
assert(buffer.size() == 1 && "record data does not match layout");
(void)buffer;
}
template <typename ElementTy>
static void read(ArrayRef<ElementTy> buffer) = delete;
};
/// Helper class for dealing with an array at the end of a record.
///
/// \sa BCRecordLayout::emitRecord
template<typename EleTy>
class BCRecordCoding<BCArray<EleTy>> {
public:
template <typename BufferTy>
static void emit(llvm::BitstreamWriter &out, BufferTy &buffer,
unsigned abbrCode, StringRef arrayData) {
// FIXME: validate array data.
out.EmitRecordWithArray(abbrCode, buffer, arrayData);
}
template <typename BufferTy, typename ArrayTy>
static void emit(llvm::BitstreamWriter &out, BufferTy &buffer,
unsigned abbrCode, const ArrayTy &arrayData) {
#ifndef NDEBUG
for (auto &item : arrayData)
EleTy::assertValid(item);
#endif
buffer.reserve(buffer.size() + arrayData.size());
std::copy(arrayData.begin(), arrayData.end(),
std::back_inserter(buffer));
out.EmitRecordWithAbbrev(abbrCode, buffer);
}
template <typename BufferTy, typename FirstData, typename ...RestData>
static void emit(llvm::BitstreamWriter &out, BufferTy &buffer,
unsigned abbrCode, FirstData firstData,
RestData... restData) {
std::array<FirstData, 1+sizeof...(restData)> arrayData{ {
firstData,
restData...
} };
emit(out, buffer, abbrCode, arrayData);
}
template <typename BufferTy>
static void emit(llvm::BitstreamWriter &out, BufferTy &buffer,
unsigned abbrCode, NoneType) {
out.EmitRecordWithAbbrev(abbrCode, buffer);
}
template <typename ElementTy>
static void read(ArrayRef<ElementTy> buffer, ArrayRef<ElementTy> &rawData) {
rawData = buffer;
}
template <typename ElementTy, typename ArrayTy>
static void read(ArrayRef<ElementTy> buffer, ArrayTy &array) {
array.append(llvm::map_iterator(buffer.begin(), ElementTy::convert),
llvm::map_iterator(buffer.end(), ElementTy::convert));
}
template <typename ElementTy>
static void read(ArrayRef<ElementTy> buffer, NoneType) {
(void)buffer;
}
template <typename ElementTy>
static void read(ArrayRef<ElementTy> buffer) = delete;
};
/// Helper class for dealing with a blob at the end of a record.
///
/// \sa BCRecordLayout
template<>
class BCRecordCoding<BCBlob> {
public:
template <typename BufferTy>
static void emit(llvm::BitstreamWriter &out, BufferTy &buffer,
unsigned abbrCode, StringRef blobData) {
out.EmitRecordWithBlob(abbrCode, buffer, blobData);
}
template <typename ElementTy>
static void read(ArrayRef<ElementTy> buffer) {
(void)buffer;
}
/// Blob data is not stored in the buffer if you are using the correct
/// accessor; this method should not be used.
template <typename ElementTy, typename DataTy>
static void read(ArrayRef<ElementTy> buffer, DataTy &data) = delete;
};
/// A type trait whose \c type field is the last of its template parameters.
template<typename First, typename ...Rest>
struct last_type {
using type = typename last_type<Rest...>::type;
};
template<typename Last>
struct last_type<Last> {
using type = Last;
};
/// A type trait whose \c value field is \c true if the last type is BCBlob.
template<typename ...Types>
using has_blob = std::is_same<BCBlob, typename last_type<int, Types...>::type>;
/// A type trait whose \c value field is \c true if the given type is a
/// BCArray (of any element kind).
template <typename T>
struct is_array {
private:
template <typename E>
static bool check(BCArray<E> *);
static int check(...);
public:
typedef bool value_type;
static constexpr bool value =
!std::is_same<decltype(check((T*)nullptr)),
decltype(check(false))>::value;
};
/// A type trait whose \c value field is \c true if the last type is a
/// BCArray (of any element kind).
template<typename ...Types>
using has_array = is_array<typename last_type<int, Types...>::type>;
} // end namespace impl
/// Represents a single bitcode record type.
///
/// This class template is meant to be instantiated and then given a name,
/// so that from then on that name can be used
template<typename IDField, typename... Fields>
class BCGenericRecordLayout {
llvm::BitstreamWriter &Out;
public:
/// The abbreviation code used for this record in the current block.
///
/// Note that this is not the same as the semantic record code, which is the
/// first field of the record.
const unsigned AbbrevCode;
/// Create a layout and register it with the given bitstream writer.
explicit BCGenericRecordLayout(llvm::BitstreamWriter &out)
: Out(out), AbbrevCode(emitAbbrev(out)) {}
/// Emit a record to the bitstream writer, using the given buffer for scratch
/// space.
///
/// Note that even fixed arguments must be specified here.
template <typename BufferTy, typename... Data>
void emit(BufferTy &buffer, unsigned recordID, Data... data) const {
emitRecord(Out, buffer, AbbrevCode, recordID, data...);
}
/// Registers this record's layout with the bitstream reader.
///
/// \returns The abbreviation code for the newly-registered record type.
static unsigned emitAbbrev(llvm::BitstreamWriter &out) {
auto *abbrev = new llvm::BitCodeAbbrev();
impl::emitOps<IDField, Fields...>(*abbrev);
return out.EmitAbbrev(abbrev);
}
/// Emit a record identified by \p abbrCode to bitstream reader \p out, using
/// \p buffer for scratch space.
///
/// Note that even fixed arguments must be specified here. Blobs are passed
/// as StringRefs, while arrays can be passed inline, as aggregates, or as
/// pre-encoded StringRef data. Skipped values and empty arrays should use
/// the special Nothing value.
template <typename BufferTy, typename... Data>
static void emitRecord(llvm::BitstreamWriter &out, BufferTy &buffer,
unsigned abbrCode, unsigned recordID, Data... data) {
static_assert(sizeof...(data) <= sizeof...(Fields) ||
impl::has_array<Fields...>::value,
"Too many record elements");
static_assert(sizeof...(data) >= sizeof...(Fields),
"Too few record elements");
buffer.clear();
impl::BCRecordCoding<IDField, Fields...>::emit(out, buffer, abbrCode,
recordID, data...);
}
/// Extract record data from \p buffer into the given data fields.
///
/// Note that even fixed arguments must be specified here. Pass \c Nothing
/// if you don't care about a particular parameter. Blob data is not included
/// in the buffer and should be handled separately by the caller.
template <typename ElementTy, typename... Data>
static void readRecord(ArrayRef<ElementTy> buffer, Data &&... data) {
static_assert(sizeof...(data) <= sizeof...(Fields),
"Too many record elements");
static_assert(sizeof...(Fields) <=
sizeof...(data) + impl::has_blob<Fields...>::value,
"Too few record elements");
return impl::BCRecordCoding<Fields...>::read(buffer,
std::forward<Data>(data)...);
}
/// Extract record data from \p buffer into the given data fields.
///
/// Note that even fixed arguments must be specified here. Pass \c Nothing
/// if you don't care about a particular parameter. Blob data is not included
/// in the buffer and should be handled separately by the caller.
template <typename BufferTy, typename... Data>
static void readRecord(BufferTy &buffer, Data &&... data) {
return readRecord(llvm::makeArrayRef(buffer), std::forward<Data>(data)...);
}
};
/// A record with a fixed record code.
template<unsigned RecordCode, typename... Fields>
class BCRecordLayout : public BCGenericRecordLayout<BCLiteral<RecordCode>,
Fields...> {
using Base = BCGenericRecordLayout<BCLiteral<RecordCode>, Fields...>;
public:
enum : unsigned {
/// The record code associated with this layout.
Code = RecordCode
};
/// Create a layout and register it with the given bitstream writer.
explicit BCRecordLayout(llvm::BitstreamWriter &out) : Base(out) {}
/// Emit a record to the bitstream writer, using the given buffer for scratch
/// space.
///
/// Note that even fixed arguments must be specified here.
template <typename BufferTy, typename... Data>
void emit(BufferTy &buffer, Data... data) const {
Base::emit(buffer, RecordCode, data...);
}
/// Emit a record identified by \p abbrCode to bitstream reader \p out, using
/// \p buffer for scratch space.
///
/// Note that even fixed arguments must be specified here. Currently, arrays
/// and blobs can only be passed as StringRefs.
template <typename BufferTy, typename... Data>
static void emitRecord(llvm::BitstreamWriter &out, BufferTy &buffer,
unsigned abbrCode, Data... data) {
Base::emitRecord(out, buffer, abbrCode, RecordCode, data...);
}
};
/// RAII object to pair entering and exiting a sub-block.
class BCBlockRAII {
llvm::BitstreamWriter &Writer;
public:
BCBlockRAII(llvm::BitstreamWriter &writer, unsigned blockID,
unsigned abbrevLen)
: Writer(writer) {
writer.EnterSubblock(blockID, abbrevLen);
}
~BCBlockRAII() {
Writer.ExitBlock();
}
};
} // end namespace llvm
#endif