LLVM 24.0.0git
CodeViewRecordIO.h
Go to the documentation of this file.
1//===- CodeViewRecordIO.h ---------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_DEBUGINFO_CODEVIEW_CODEVIEWRECORDIO_H
10#define LLVM_DEBUGINFO_CODEVIEW_CODEVIEWRECORDIO_H
11
13#include "llvm/ADT/StringRef.h"
18#include "llvm/Support/Error.h"
19#include <cassert>
20#include <cstdint>
21#include <type_traits>
22
23namespace llvm {
24
25template <typename T> class ArrayRef;
26class APSInt;
27
28namespace codeview {
29class TypeIndex;
30struct GUID;
31
33public:
34 virtual void emitBytes(StringRef Data) = 0;
35 virtual void emitIntValue(uint64_t Value, unsigned Size) = 0;
36 virtual void emitAPSIntValue(const APSInt &Value) = 0;
37 virtual void emitBinaryData(StringRef Data) = 0;
38 virtual void AddComment(const Twine &T) = 0;
39 virtual void AddRawComment(const Twine &T) = 0;
40 virtual bool isVerboseAsm() = 0;
41 virtual std::string getTypeName(TypeIndex TI) = 0;
42 virtual ~CodeViewRecordStreamer() = default;
43};
44
46 uint32_t getCurrentOffset() const {
47 if (isWriting())
48 return Writer->getOffset();
49 else if (isReading())
50 return Reader->getOffset();
51 else
52 return 0;
53 }
54
55public:
56 // deserializes records to structures
57 explicit CodeViewRecordIO(BinaryStreamReader &Reader) : Reader(&Reader) {}
58
59 // serializes records to buffer
60 explicit CodeViewRecordIO(BinaryStreamWriter &Writer) : Writer(&Writer) {}
61
62 // writes records to assembly file using MC library interface
64 : Streamer(&Streamer) {}
65
66 LLVM_ABI Error beginRecord(std::optional<uint32_t> MaxLength);
68
69 LLVM_ABI Error mapInteger(TypeIndex &TypeInd, const Twine &Comment = "");
70
71 bool isStreaming() const {
72 return (Streamer != nullptr) && (Reader == nullptr) && (Writer == nullptr);
73 }
74 bool isReading() const {
75 return (Reader != nullptr) && (Streamer == nullptr) && (Writer == nullptr);
76 }
77 bool isWriting() const {
78 return (Writer != nullptr) && (Streamer == nullptr) && (Reader == nullptr);
79 }
80
82
83 template <typename T> Error mapObject(T &Value) {
84 if (isStreaming()) {
85 StringRef BytesSR =
86 StringRef((reinterpret_cast<const char *>(&Value)), sizeof(Value));
87 Streamer->emitBytes(BytesSR);
88 incrStreamedLen(sizeof(T));
89 return Error::success();
90 }
91
92 if (isWriting())
93 return Writer->writeObject(Value);
94
95 const T *ValuePtr;
96 if (auto EC = Reader->readObject(ValuePtr))
97 return EC;
98 Value = *ValuePtr;
99 return Error::success();
100 }
101
102 template <typename T>
103 Error mapWriteInteger(const T &Value, const Twine &Comment = "") {
104 if (isStreaming()) {
105 emitComment(Comment);
106 Streamer->emitIntValue(Value, sizeof(T));
107 incrStreamedLen(sizeof(T));
108 return Error::success();
109 }
110
111 return Writer->writeInteger(Value);
112 }
113
114 template <typename T> Error mapInteger(T &Value, const Twine &Comment = "") {
115 if (isReading())
116 return Reader->readInteger(Value);
117 return mapWriteInteger(Value, Comment);
118 }
119
120 LLVM_ABI Error mapWriteInt128(const APSInt &Value, const Twine &Comment = "");
121
122 template <typename Trunc, typename T>
123 Error mapTruncInteger(T &Value, const Twine &Comment = "") {
124 static_assert(sizeof(Trunc) <= sizeof(T));
125
126 Trunc Truncated = static_cast<Trunc>(Value);
127 auto EC = mapInteger(Truncated, Comment);
128 if (!EC && isReading())
129 Value = Truncated;
130
131 return EC;
132 }
133
134 template <typename T> Error mapEnum(T &Value, const Twine &Comment = "") {
135 if (!isStreaming() && sizeof(Value) > maxFieldLength())
137
138 using U = std::underlying_type_t<T>;
139 U X;
140
141 if (isWriting() || isStreaming())
142 X = static_cast<U>(Value);
143
144 if (auto EC = mapInteger(X, Comment))
145 return EC;
146
147 if (isReading())
148 Value = static_cast<T>(X);
149
150 return Error::success();
151 }
152
153 LLVM_ABI Error mapEncodedInteger(int64_t &Value, const Twine &Comment = "");
154 LLVM_ABI Error mapEncodedInteger(uint64_t &Value, const Twine &Comment = "");
155 LLVM_ABI Error mapEncodedInteger(APSInt &Value, const Twine &Comment = "");
156 LLVM_ABI Error mapStringZ(StringRef &Value, const Twine &Comment = "");
157 LLVM_ABI Error mapGuid(GUID &Guid, const Twine &Comment = "");
158
159 LLVM_ABI Error mapStringZVectorZ(std::vector<StringRef> &Value,
160 const Twine &Comment = "");
161
162 template <typename SizeType, typename T, typename ElementMapper>
163 Error mapVectorN(T &Items, const ElementMapper &Mapper,
164 const Twine &Comment = "") {
165 SizeType Size;
166 if (isStreaming()) {
167 Size = static_cast<SizeType>(Items.size());
168 emitComment(Comment);
169 Streamer->emitIntValue(Size, sizeof(Size));
170 incrStreamedLen(sizeof(Size)); // add 1 for the delimiter
171
172 for (auto &X : Items) {
173 if (auto EC = Mapper(*this, X))
174 return EC;
175 }
176 } else if (isWriting()) {
177 Size = static_cast<SizeType>(Items.size());
178 if (auto EC = Writer->writeInteger(Size))
179 return EC;
180
181 for (auto &X : Items) {
182 if (auto EC = Mapper(*this, X))
183 return EC;
184 }
185 } else {
186 if (auto EC = Reader->readInteger(Size))
187 return EC;
188 for (SizeType I = 0; I < Size; ++I) {
189 typename T::value_type Item;
190 if (auto EC = Mapper(*this, Item))
191 return EC;
192 Items.push_back(Item);
193 }
194 }
195
196 return Error::success();
197 }
198
199 template <typename T, typename ElementMapper>
200 Error mapVectorTail(T &Items, const ElementMapper &Mapper,
201 const Twine &Comment = "") {
202 emitComment(Comment);
203 if (isStreaming() || isWriting()) {
204 for (auto &Item : Items) {
205 if (auto EC = Mapper(*this, Item))
206 return EC;
207 }
208 } else {
209 typename T::value_type Field;
210 // Stop when we run out of bytes or we hit record padding bytes.
211 while (!Reader->empty() && Reader->peek() < 0xf0 /* LF_PAD0 */) {
212 if (auto EC = Mapper(*this, Field))
213 return EC;
214 Items.push_back(Field);
215 }
216 }
217 return Error::success();
218 }
219
221 const Twine &Comment = "");
222 LLVM_ABI Error mapByteVectorTail(std::vector<uint8_t> &Bytes,
223 const Twine &Comment = "");
224
227
229 if (isStreaming())
230 return StreamedLen;
231 return 0;
232 }
233
234 void emitRawComment(const Twine &T) {
235 if (isStreaming() && Streamer->isVerboseAsm())
236 Streamer->AddRawComment(T);
237 }
238
239private:
240 Error mapWriteEncodedSignedInteger(const APSInt &Value,
241 const Twine &Comment = "");
242 Error mapWriteEncodedUnsignedInteger(const APSInt &Value,
243 const Twine &Comment = "");
244
245 void incrStreamedLen(const uint64_t &Len) {
246 if (isStreaming())
247 StreamedLen += Len;
248 }
249
250 void resetStreamedLen() {
251 if (isStreaming())
252 StreamedLen = 4; // The record prefix is 4 bytes long
253 }
254
255 void emitComment(const Twine &Comment) {
256 if (isStreaming() && Streamer->isVerboseAsm()) {
257 Twine TComment(Comment);
258 if (!TComment.isTriviallyEmpty())
259 Streamer->AddComment(TComment);
260 }
261 }
262
263 struct RecordLimit {
264 uint32_t BeginOffset;
265 std::optional<uint32_t> MaxLength;
266
267 std::optional<uint32_t> bytesRemaining(uint32_t CurrentOffset) const {
268 if (!MaxLength)
269 return std::nullopt;
270 assert(CurrentOffset >= BeginOffset);
271
272 uint32_t BytesUsed = CurrentOffset - BeginOffset;
273 if (BytesUsed >= *MaxLength)
274 return 0;
275 return *MaxLength - BytesUsed;
276 }
277 };
278
279 SmallVector<RecordLimit, 2> Limits;
280
281 BinaryStreamReader *Reader = nullptr;
282 BinaryStreamWriter *Writer = nullptr;
283 CodeViewRecordStreamer *Streamer = nullptr;
284 uint64_t StreamedLen = 0;
285};
286
287} // end namespace codeview
288} // end namespace llvm
289
290#endif // LLVM_DEBUGINFO_CODEVIEW_CODEVIEWRECORDIO_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t uint64_t
#define X(NUM, ENUM, NAME)
Definition ELF.h:665
#define LLVM_ABI
Definition Compiler.h:211
#define I(x, y, z)
Definition MD5.cpp:57
#define T
OptimizedStructLayoutField Field
This file defines the SmallVector class.
std::unique_ptr< MCStreamer > && Streamer
An arbitrary precision integer that knows its signedness.
Definition APSInt.h:24
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
Provides read only access to a subclass of BinaryStream.
Provides write only access to a subclass of WritableBinaryStream.
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
LLVM Value Representation.
Definition Value.h:75
Error mapVectorTail(T &Items, const ElementMapper &Mapper, const Twine &Comment="")
CodeViewRecordIO(BinaryStreamWriter &Writer)
LLVM_ABI Error padToAlignment(uint32_t Align)
Error mapVectorN(T &Items, const ElementMapper &Mapper, const Twine &Comment="")
Error mapInteger(T &Value, const Twine &Comment="")
LLVM_ABI Error mapInteger(TypeIndex &TypeInd, const Twine &Comment="")
CodeViewRecordIO(BinaryStreamReader &Reader)
LLVM_ABI Error mapGuid(GUID &Guid, const Twine &Comment="")
LLVM_ABI Error mapStringZVectorZ(std::vector< StringRef > &Value, const Twine &Comment="")
LLVM_ABI Error mapStringZ(StringRef &Value, const Twine &Comment="")
LLVM_ABI Error mapWriteInt128(const APSInt &Value, const Twine &Comment="")
Error mapWriteInteger(const T &Value, const Twine &Comment="")
Error mapEnum(T &Value, const Twine &Comment="")
LLVM_ABI Error mapEncodedInteger(int64_t &Value, const Twine &Comment="")
Error mapTruncInteger(T &Value, const Twine &Comment="")
LLVM_ABI Error beginRecord(std::optional< uint32_t > MaxLength)
LLVM_ABI Error mapByteVectorTail(ArrayRef< uint8_t > &Bytes, const Twine &Comment="")
CodeViewRecordIO(CodeViewRecordStreamer &Streamer)
LLVM_ABI uint32_t maxFieldLength() const
virtual void emitBytes(StringRef Data)=0
virtual void emitAPSIntValue(const APSInt &Value)=0
virtual void AddComment(const Twine &T)=0
virtual void emitIntValue(uint64_t Value, unsigned Size)=0
virtual void emitBinaryData(StringRef Data)=0
virtual std::string getTypeName(TypeIndex TI)=0
virtual void AddRawComment(const Twine &T)=0
A 32-bit type reference.
Definition TypeIndex.h:97
This is an optimization pass for GlobalISel generic memory operations.
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
Definition Error.h:340
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
This represents the 'GUID' type from windows.h.
Definition GUID.h:22