LLVM  14.0.0git
DataExtractor.cpp
Go to the documentation of this file.
1 //===-- DataExtractor.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 
10 #include "llvm/Support/Errc.h"
12 #include "llvm/Support/LEB128.h"
14 
15 using namespace llvm;
16 
17 bool DataExtractor::prepareRead(uint64_t Offset, uint64_t Size,
18  Error *E) const {
20  return true;
21  if (E) {
22  if (Offset <= Data.size())
25  "unexpected end of data at offset 0x%zx while reading [0x%" PRIx64
26  ", 0x%" PRIx64 ")",
27  Data.size(), Offset, Offset + Size);
28  else
30  "offset 0x%" PRIx64
31  " is beyond the end of data at 0x%zx",
32  Offset, Data.size());
33  }
34  return false;
35 }
36 
37 static bool isError(Error *E) { return E && *E; }
38 
39 template <typename T>
40 T DataExtractor::getU(uint64_t *offset_ptr, Error *Err) const {
41  ErrorAsOutParameter ErrAsOut(Err);
42  T val = 0;
43  if (isError(Err))
44  return val;
45 
46  uint64_t offset = *offset_ptr;
47  if (!prepareRead(offset, sizeof(T), Err))
48  return val;
49  std::memcpy(&val, &Data.data()[offset], sizeof(val));
50  if (sys::IsLittleEndianHost != IsLittleEndian)
52 
53  // Advance the offset
54  *offset_ptr += sizeof(val);
55  return val;
56 }
57 
58 template <typename T>
59 T *DataExtractor::getUs(uint64_t *offset_ptr, T *dst, uint32_t count,
60  Error *Err) const {
61  ErrorAsOutParameter ErrAsOut(Err);
62  if (isError(Err))
63  return nullptr;
64 
65  uint64_t offset = *offset_ptr;
66 
67  if (!prepareRead(offset, sizeof(*dst) * count, Err))
68  return nullptr;
69  for (T *value_ptr = dst, *end = dst + count; value_ptr != end;
70  ++value_ptr, offset += sizeof(*dst))
71  *value_ptr = getU<T>(offset_ptr, Err);
72  // Advance the offset
73  *offset_ptr = offset;
74  // Return a non-NULL pointer to the converted data as an indicator of
75  // success
76  return dst;
77 }
78 
79 uint8_t DataExtractor::getU8(uint64_t *offset_ptr, llvm::Error *Err) const {
80  return getU<uint8_t>(offset_ptr, Err);
81 }
82 
83 uint8_t *DataExtractor::getU8(uint64_t *offset_ptr, uint8_t *dst,
84  uint32_t count) const {
85  return getUs<uint8_t>(offset_ptr, dst, count, nullptr);
86 }
87 
88 uint8_t *DataExtractor::getU8(Cursor &C, uint8_t *Dst, uint32_t Count) const {
89  return getUs<uint8_t>(&C.Offset, Dst, Count, &C.Err);
90 }
91 
93  return getU<uint16_t>(offset_ptr, Err);
94 }
95 
97  uint32_t count) const {
98  return getUs<uint16_t>(offset_ptr, dst, count, nullptr);
99 }
100 
101 uint32_t DataExtractor::getU24(uint64_t *OffsetPtr, Error *Err) const {
102  uint24_t ExtractedVal = getU<uint24_t>(OffsetPtr, Err);
103  // The 3 bytes are in the correct byte order for the host.
104  return ExtractedVal.getAsUint32(sys::IsLittleEndianHost);
105 }
106 
108  return getU<uint32_t>(offset_ptr, Err);
109 }
110 
112  uint32_t count) const {
113  return getUs<uint32_t>(offset_ptr, dst, count, nullptr);
114 }
115 
117  return getU<uint64_t>(offset_ptr, Err);
118 }
119 
121  uint32_t count) const {
122  return getUs<uint64_t>(offset_ptr, dst, count, nullptr);
123 }
124 
126  llvm::Error *Err) const {
127  switch (byte_size) {
128  case 1:
129  return getU8(offset_ptr, Err);
130  case 2:
131  return getU16(offset_ptr, Err);
132  case 4:
133  return getU32(offset_ptr, Err);
134  case 8:
135  return getU64(offset_ptr, Err);
136  }
137  llvm_unreachable("getUnsigned unhandled case!");
138 }
139 
140 int64_t
141 DataExtractor::getSigned(uint64_t *offset_ptr, uint32_t byte_size) const {
142  switch (byte_size) {
143  case 1:
144  return (int8_t)getU8(offset_ptr);
145  case 2:
146  return (int16_t)getU16(offset_ptr);
147  case 4:
148  return (int32_t)getU32(offset_ptr);
149  case 8:
150  return (int64_t)getU64(offset_ptr);
151  }
152  llvm_unreachable("getSigned unhandled case!");
153 }
154 
156  ErrorAsOutParameter ErrAsOut(Err);
157  if (isError(Err))
158  return StringRef();
159 
160  uint64_t Start = *OffsetPtr;
161  StringRef::size_type Pos = Data.find('\0', Start);
162  if (Pos != StringRef::npos) {
163  *OffsetPtr = Pos + 1;
164  return StringRef(Data.data() + Start, Pos - Start);
165  }
166  if (Err)
168  "no null terminated string at offset 0x%" PRIx64,
169  Start);
170  return StringRef();
171 }
172 
174  uint64_t Length,
175  StringRef TrimChars) const {
176  StringRef Bytes(getBytes(OffsetPtr, Length));
177  return Bytes.trim(TrimChars);
178 }
179 
181  Error *Err) const {
182  ErrorAsOutParameter ErrAsOut(Err);
183  if (isError(Err))
184  return StringRef();
185 
186  if (!prepareRead(*OffsetPtr, Length, Err))
187  return StringRef();
188 
189  StringRef Result = Data.substr(*OffsetPtr, Length);
190  *OffsetPtr += Length;
191  return Result;
192 }
193 
194 template <typename T>
195 static T getLEB128(StringRef Data, uint64_t *OffsetPtr, Error *Err,
196  T (&Decoder)(const uint8_t *p, unsigned *n,
197  const uint8_t *end, const char **error)) {
198  ArrayRef<uint8_t> Bytes = arrayRefFromStringRef(Data);
199  assert(*OffsetPtr <= Bytes.size());
200  ErrorAsOutParameter ErrAsOut(Err);
201  if (isError(Err))
202  return T();
203 
204  const char *error;
205  unsigned bytes_read;
206  T result =
207  Decoder(Bytes.data() + *OffsetPtr, &bytes_read, Bytes.end(), &error);
208  if (error) {
209  if (Err)
211  "unable to decode LEB128 at offset 0x%8.8" PRIx64
212  ": %s",
213  *OffsetPtr, error);
214  return T();
215  }
216  *OffsetPtr += bytes_read;
217  return result;
218 }
219 
221  return getLEB128(Data, offset_ptr, Err, decodeULEB128);
222 }
223 
224 int64_t DataExtractor::getSLEB128(uint64_t *offset_ptr, Error *Err) const {
225  return getLEB128(Data, offset_ptr, Err, decodeSLEB128);
226 }
227 
228 void DataExtractor::skip(Cursor &C, uint64_t Length) const {
229  ErrorAsOutParameter ErrAsOut(&C.Err);
230  if (isError(&C.Err))
231  return;
232 
233  if (prepareRead(C.Offset, Length, &C.Err))
234  C.Offset += Length;
235 }
llvm::Check::Size
@ Size
Definition: FileCheck.h:73
isError
static bool isError(Error *E)
Definition: DataExtractor.cpp:37
llvm::errc::invalid_argument
@ invalid_argument
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AllocatorList.h:23
llvm::sys::swapByteOrder
void swapByteOrder(T &Value)
Definition: SwapByteOrder.h:158
llvm::DataExtractor::getSigned
int64_t getSigned(uint64_t *offset_ptr, uint32_t size) const
Extract an signed integer of size byte_size from *offset_ptr.
Definition: DataExtractor.cpp:141
llvm::Uint24
An auxiliary type to facilitate extraction of 3-byte entities.
Definition: DataExtractor.h:19
T
llvm::StringRef::npos
static constexpr size_t npos
Definition: StringRef.h:59
llvm::StringRef::find
LLVM_NODISCARD size_t find(char C, size_t From=0) const
Search for the first character C in the string.
Definition: StringRef.h:314
ErrorHandling.h
SwapByteOrder.h
Errc.h
llvm::sys::path::end
const_iterator end(StringRef path)
Get end iterator over path.
Definition: Path.cpp:236
error
#define error(X)
Definition: SymbolRecordMapping.cpp:14
Offset
uint64_t Offset
Definition: ELFObjHandler.cpp:80
llvm::DataExtractor::getU64
uint64_t getU64(uint64_t *offset_ptr, Error *Err=nullptr) const
Extract a uint64_t value from *offset_ptr.
Definition: DataExtractor.cpp:116
llvm::DataExtractor::skip
void skip(Cursor &C, uint64_t Length) const
Advance the Cursor position by the given number of bytes.
Definition: DataExtractor.cpp:228
llvm::Data
@ Data
Definition: SIMachineScheduler.h:55
p
the resulting code requires compare and branches when and if * p
Definition: README.txt:396
size_t
llvm::ArrayRef::data
const T * data() const
Definition: ArrayRef.h:160
result
It looks like we only need to define PPCfmarto for these because according to these instructions perform RTO on fma s result
Definition: README_P9.txt:256
llvm::StringRef::substr
LLVM_NODISCARD StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:610
llvm::DataExtractor::getULEB128
uint64_t getULEB128(uint64_t *offset_ptr, llvm::Error *Err=nullptr) const
Extract a unsigned LEB128 value from *offset_ptr.
Definition: DataExtractor.cpp:220
llvm::decodeULEB128
uint64_t decodeULEB128(const uint8_t *p, unsigned *n=nullptr, const uint8_t *end=nullptr, const char **error=nullptr)
Utility function to decode a ULEB128 value.
Definition: LEB128.h:128
llvm::DataExtractor::getBytes
StringRef getBytes(uint64_t *OffsetPtr, uint64_t Length, Error *Err=nullptr) const
Extract a fixed number of bytes from the specified offset.
Definition: DataExtractor.cpp:180
llvm::DataExtractor::getFixedLengthString
StringRef getFixedLengthString(uint64_t *OffsetPtr, uint64_t Length, StringRef TrimChars={"\0", 1}) const
Extract a fixed length string from *OffsetPtr and consume Length bytes.
Definition: DataExtractor.cpp:173
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
getLEB128
static T getLEB128(StringRef Data, uint64_t *OffsetPtr, Error *Err, T(&Decoder)(const uint8_t *p, unsigned *n, const uint8_t *end, const char **error))
Definition: DataExtractor.cpp:195
llvm::ErrorAsOutParameter
Helper for Errors used as out-parameters.
Definition: Error.h:1098
llvm::errc::illegal_byte_sequence
@ illegal_byte_sequence
llvm::StringRef::trim
LLVM_NODISCARD StringRef trim(char Char) const
Return string with consecutive Char characters starting from the left and right removed.
Definition: StringRef.h:869
val
The initial backend is deliberately restricted to z10 We should add support for later architectures at some point If an asm ties an i32 r result to an i64 the input will be treated as an leaving the upper bits uninitialised For i64 store i32 val
Definition: README.txt:15
llvm::count
auto count(R &&Range, const E &Element)
Wrapper function around std::count to count the number of times an element Element occurs in the give...
Definition: STLExtras.h:1732
uint64_t
llvm::DataExtractor::isValidOffsetForDataOfSize
bool isValidOffsetForDataOfSize(uint64_t offset, uint64_t length) const
Test the availability of length bytes of data from offset.
Definition: DataExtractor.h:672
LEB128.h
llvm::Uint24::getAsUint32
uint32_t getAsUint32(bool IsLittleEndian) const
Definition: DataExtractor.h:27
llvm::DataExtractor::getUnsigned
uint64_t getUnsigned(uint64_t *offset_ptr, uint32_t byte_size, Error *Err=nullptr) const
Extract an unsigned integer of size byte_size from *offset_ptr.
Definition: DataExtractor.cpp:125
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
memcpy
<%struct.s * > cast struct s *S to sbyte *< sbyte * > sbyte uint cast struct s *agg result to sbyte *< sbyte * > sbyte uint cast struct s *memtmp to sbyte *< sbyte * > sbyte uint ret void llc ends up issuing two memcpy or custom lower memcpy(of small size) to be ldmia/stmia. I think option 2 is better but the current register allocator cannot allocate a chunk of registers at a time. A feasible temporary solution is to use specific physical registers at the lowering time for small(<
llvm::DataExtractor::Cursor
A class representing a position in a DataExtractor, as well as any error encountered during extractio...
Definition: DataExtractor.h:54
llvm::DataExtractor::getU32
uint32_t getU32(uint64_t *offset_ptr, Error *Err=nullptr) const
Extract a uint32_t value from *offset_ptr.
Definition: DataExtractor.cpp:107
llvm::ArrayRef< uint8_t >
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:134
uint32_t
llvm::DataExtractor::getU24
uint32_t getU24(uint64_t *OffsetPtr, Error *Err=nullptr) const
Extract a 24-bit unsigned value from *offset_ptr and return it in a uint32_t.
Definition: DataExtractor.cpp:101
llvm::DataExtractor::getU8
uint8_t getU8(uint64_t *offset_ptr, Error *Err=nullptr) const
Extract a uint8_t value from *offset_ptr.
Definition: DataExtractor.cpp:79
llvm::StringRef::size
constexpr LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:156
llvm::createStringError
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1240
uint16_t
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
DataExtractor.h
llvm::DataExtractor::getCStrRef
StringRef getCStrRef(uint64_t *OffsetPtr, Error *Err=nullptr) const
Extract a C string from *offset_ptr.
Definition: DataExtractor.cpp:155
llvm::decodeSLEB128
int64_t decodeSLEB128(const uint8_t *p, unsigned *n=nullptr, const uint8_t *end=nullptr, const char **error=nullptr)
Utility function to decode a SLEB128 value.
Definition: LEB128.h:161
llvm::sys::IsLittleEndianHost
static const bool IsLittleEndianHost
Definition: SwapByteOrder.h:101
llvm::ArrayRef::size
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:163
llvm::StringRef::data
const LLVM_NODISCARD char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:148
n
The same transformation can work with an even modulo with the addition of a and shrink the compare RHS by the same amount Unless the target supports that transformation probably isn t worthwhile The transformation can also easily be made to work with non zero equality for n
Definition: README.txt:685
llvm::ArrayRef::end
iterator end() const
Definition: ArrayRef.h:152
llvm::DataExtractor::getU16
uint16_t getU16(uint64_t *offset_ptr, Error *Err=nullptr) const
Extract a uint16_t value from *offset_ptr.
Definition: DataExtractor.cpp:92
llvm::DataExtractor::getSLEB128
int64_t getSLEB128(uint64_t *OffsetPtr, Error *Err=nullptr) const
Extract a signed LEB128 value from *offset_ptr.
Definition: DataExtractor.cpp:224