LLVM 24.0.0git
RecordSerialization.cpp
Go to the documentation of this file.
1//===-- RecordSerialization.cpp -------------------------------------------===//
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// Utilities for serializing and deserializing CodeView records.
10//
11//===----------------------------------------------------------------------===//
12
14#include "llvm/ADT/APInt.h"
15#include "llvm/ADT/APSInt.h"
20
21using namespace llvm;
22using namespace llvm::codeview;
23using namespace llvm::support;
24
25/// Reinterpret a byte array as an array of characters. Does not interpret as
26/// a C string, as StringRef has several helpers (split) that make that easy.
28 return StringRef(reinterpret_cast<const char *>(LeafData.data()),
29 LeafData.size());
30}
31
35
37 // Used to avoid overload ambiguity on APInt constructor.
38 bool FalseVal = false;
39 uint16_t Short;
40 if (auto EC = Reader.readInteger(Short))
41 return EC;
42
43 if (Short < LF_NUMERIC) {
44 Num = APSInt(APInt(/*numBits=*/16, Short, /*isSigned=*/false),
45 /*isUnsigned=*/true);
46 return Error::success();
47 }
48
49 switch (Short) {
50 case LF_CHAR: {
51 int8_t N;
52 if (auto EC = Reader.readInteger(N))
53 return EC;
54 Num = APSInt(APInt(8, N, true), false);
55 return Error::success();
56 }
57 case LF_SHORT: {
58 int16_t N;
59 if (auto EC = Reader.readInteger(N))
60 return EC;
61 Num = APSInt(APInt(16, N, true), false);
62 return Error::success();
63 }
64 case LF_USHORT: {
65 uint16_t N;
66 if (auto EC = Reader.readInteger(N))
67 return EC;
68 Num = APSInt(APInt(16, N, false), true);
69 return Error::success();
70 }
71 case LF_LONG: {
72 int32_t N;
73 if (auto EC = Reader.readInteger(N))
74 return EC;
75 Num = APSInt(APInt(32, N, true), false);
76 return Error::success();
77 }
78 case LF_ULONG: {
79 uint32_t N;
80 if (auto EC = Reader.readInteger(N))
81 return EC;
82 Num = APSInt(APInt(32, N, FalseVal), true);
83 return Error::success();
84 }
85 case LF_QUADWORD: {
86 int64_t N;
87 if (auto EC = Reader.readInteger(N))
88 return EC;
89 Num = APSInt(APInt(64, N, true), false);
90 return Error::success();
91 }
92 case LF_UQUADWORD: {
93 uint64_t N;
94 if (auto EC = Reader.readInteger(N))
95 return EC;
96 Num = APSInt(APInt(64, N, false), true);
97 return Error::success();
98 }
99 case LF_OCTWORD: {
100 uint64_t Lo = 0, Hi = 0;
101 if (auto EC = Reader.readInteger(Lo))
102 return EC;
103 if (auto EC = Reader.readInteger(Hi))
104 return EC;
105 // APInt expects the low words first and each word in native endian. Since
106 // we know that the integer was encoded in little endian, there's no need to
107 // swap the words.
108 uint64_t Words[2] = {Lo, Hi};
109 Num = APSInt(APInt(128, Words), /*isUnsigned=*/false);
110 return Error::success();
111 }
112 case LF_UOCTWORD: {
113 uint64_t Lo = 0, Hi = 0;
114 if (auto EC = Reader.readInteger(Lo))
115 return EC;
116 if (auto EC = Reader.readInteger(Hi))
117 return EC;
118 uint64_t Words[2] = {Lo, Hi};
119 Num = APSInt(APInt(128, Words), /*isUnsigned=*/true);
120 return Error::success();
121 }
122 }
124 "Buffer contains invalid APSInt type");
125}
126
128 ArrayRef<uint8_t> Bytes(Data.bytes_begin(), Data.bytes_end());
130 BinaryStreamReader SR(S);
131 auto EC = consume(SR, Num);
132 Data = Data.take_back(SR.bytesRemaining());
133 return EC;
134}
135
136/// Decode a numeric leaf value that is known to be a uint64_t.
138 uint64_t &Num) {
139 APSInt N;
140 if (auto EC = consume(Reader, N))
141 return EC;
142 if (N.isSigned() || !N.isIntN(64))
144 "Data is not a numeric value!");
145 Num = N.getLimitedValue();
146 return Error::success();
147}
148
150 return Reader.readInteger(Item);
151}
152
154 ArrayRef<uint8_t> Bytes(Data.bytes_begin(), Data.bytes_end());
156 BinaryStreamReader SR(S);
157 auto EC = consume(SR, Item);
158 Data = Data.take_back(SR.bytesRemaining());
159 return EC;
160}
161
163 return Reader.readInteger(Item);
164}
165
167 if (Reader.empty())
169 "Null terminated string buffer is empty!");
170
171 return Reader.readCString(Item);
172}
173
This file implements a class to represent arbitrary precision integral constant values and operations...
This file implements the APSInt class, which is a simple class that represents an arbitrary sized int...
Class for arbitrary precision integers.
Definition APInt.h:78
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
size_t size() const
Get the array size.
Definition ArrayRef.h:141
const T * data() const
Definition ArrayRef.h:138
An implementation of BinaryStream which holds its entire data set in a single contiguous buffer.
Provides read only access to a subclass of BinaryStream.
LLVM_ABI Error readCString(StringRef &Dest)
Read a null terminated string from Dest.
Error readInteger(T &Dest)
Read an integer of the specified endianness into Dest and update the stream's offset.
BinaryStreamRef is to BinaryStream what ArrayRef is to an Array.
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
Tagged union holding either a T or a Error.
Definition Error.h:485
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition StringRef.h:728
LLVM_ABI Error consume_numeric(BinaryStreamReader &Reader, uint64_t &Value)
Decodes a numeric leaf value that is known to be a particular type.
Error consume(BinaryStreamReader &Reader)
LLVM_ABI StringRef getBytesAsCString(ArrayRef< uint8_t > LeafData)
LLVM_ABI StringRef getBytesAsCharacters(ArrayRef< uint8_t > LeafData)
Reinterpret a byte array as an array of characters.
LLVM_ABI Expected< CVSymbol > readSymbolFromStream(BinaryStreamRef Stream, uint32_t Offset)
Expected< CVRecord< Kind > > readCVRecordFromStream(BinaryStreamRef Stream, uint32_t Offset)
Read a complete record from a stream at a random offset.
Definition CVRecord.h:92
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:577
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
Definition Error.h:340
#define N