LLVM  14.0.0git
NamedStreamMap.cpp
Go to the documentation of this file.
1 //===- NamedStreamMap.cpp - PDB Named Stream Map --------------------------===//
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/ADT/StringMap.h"
11 #include "llvm/ADT/StringRef.h"
19 #include "llvm/Support/Endian.h"
20 #include "llvm/Support/Error.h"
21 #include <algorithm>
22 #include <cassert>
23 #include <cstdint>
24 #include <tuple>
25 
26 using namespace llvm;
27 using namespace llvm::pdb;
28 
30 
32  // In the reference implementation, this uses
33  // HASH Hasher<ULONG*, USHORT*>::hashPbCb(PB pb, size_t cb, ULONG ulMod).
34  // Here, the type HASH is a typedef of unsigned short.
35  // ** It is not a bug that we truncate the result of hashStringV1, in fact
36  // it is a bug if we do not! **
37  // See NMTNI::hash() in the reference implementation.
38  return static_cast<uint16_t>(hashStringV1(S));
39 }
40 
42  return NS->getString(Offset);
43 }
44 
46  return NS->appendStringData(S);
47 }
48 
49 NamedStreamMap::NamedStreamMap() : HashTraits(*this), OffsetIndexMap(1) {}
50 
52  uint32_t StringBufferSize;
53  if (auto EC = Stream.readInteger(StringBufferSize))
54  return joinErrors(std::move(EC),
55  make_error<RawError>(raw_error_code::corrupt_file,
56  "Expected string buffer size"));
57 
58  StringRef Buffer;
59  if (auto EC = Stream.readFixedString(Buffer, StringBufferSize))
60  return EC;
61  NamesBuffer.assign(Buffer.begin(), Buffer.end());
62 
63  return OffsetIndexMap.load(Stream);
64 }
65 
67  // The first field is the number of bytes of string data.
68  if (auto EC = Writer.writeInteger<uint32_t>(NamesBuffer.size()))
69  return EC;
70 
71  // Then the actual string data.
72  StringRef Data(NamesBuffer.data(), NamesBuffer.size());
73  if (auto EC = Writer.writeFixedString(Data))
74  return EC;
75 
76  // And finally the Offset Index map.
77  if (auto EC = OffsetIndexMap.commit(Writer))
78  return EC;
79 
80  return Error::success();
81 }
82 
84  return sizeof(uint32_t) // String data size
85  + NamesBuffer.size() // String data
86  + OffsetIndexMap.calculateSerializedLength(); // Offset Index Map
87 }
88 
89 uint32_t NamedStreamMap::size() const { return OffsetIndexMap.size(); }
90 
92  assert(NamesBuffer.size() > Offset);
93  return StringRef(NamesBuffer.data() + Offset);
94 }
95 
97  return hashStringV1(getString(Offset));
98 }
99 
100 bool NamedStreamMap::get(StringRef Stream, uint32_t &StreamNo) const {
101  auto Iter = OffsetIndexMap.find_as(Stream, HashTraits);
102  if (Iter == OffsetIndexMap.end())
103  return false;
104  StreamNo = (*Iter).second;
105  return true;
106 }
107 
109  StringMap<uint32_t> Result;
110  for (const auto &Entry : OffsetIndexMap) {
111  StringRef Stream(NamesBuffer.data() + Entry.first);
112  Result.try_emplace(Stream, Entry.second);
113  }
114  return Result;
115 }
116 
118  uint32_t Offset = NamesBuffer.size();
119  llvm::append_range(NamesBuffer, S);
120  NamesBuffer.push_back('\0');
121  return Offset;
122 }
123 
124 void NamedStreamMap::set(StringRef Stream, uint32_t StreamNo) {
125  OffsetIndexMap.set_as(Stream, support::ulittle32_t(StreamNo), HashTraits);
126 }
llvm::pdb::NamedStreamMap::set
void set(StringRef Stream, uint32_t StreamNo)
Definition: NamedStreamMap.cpp:124
BinaryStreamReader.h
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AllocatorList.h:23
llvm::BinaryStreamWriter::writeInteger
Error writeInteger(T Value)
Write the integer Value to the underlying stream in the specified endianness.
Definition: BinaryStreamWriter.h:64
llvm::pdb::NamedStreamMapTraits::storageKeyToLookupKey
StringRef storageKeyToLookupKey(uint32_t Offset) const
Definition: NamedStreamMap.cpp:41
llvm::support::detail::packed_endian_specific_integral
Definition: Endian.h:206
llvm::BinaryStreamWriter::writeFixedString
Error writeFixedString(StringRef Str)
Write the string Str to the underlying stream without a null terminator.
Definition: BinaryStreamWriter.cpp:56
llvm::pdb::NamedStreamMap::size
uint32_t size() const
Definition: NamedStreamMap.cpp:89
StringRef.h
llvm::pdb::NamedStreamMap::appendStringData
uint32_t appendStringData(StringRef S)
Definition: NamedStreamMap.cpp:117
NamedStreamMap.h
llvm::BinaryStreamWriter
Provides write only access to a subclass of WritableBinaryStream.
Definition: BinaryStreamWriter.h:31
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition: Error.h:331
llvm::pdb::NamedStreamMap::entries
StringMap< uint32_t > entries() const
Definition: NamedStreamMap.cpp:108
Error.h
HashTable.h
Offset
uint64_t Offset
Definition: ELFObjHandler.cpp:81
llvm::pdb::NamedStreamMap::calculateSerializedLength
uint32_t calculateSerializedLength() const
Definition: NamedStreamMap.cpp:83
RawError.h
llvm::Data
@ Data
Definition: SIMachineScheduler.h:55
llvm::pdb::NamedStreamMapTraits::lookupKeyToStorageKey
uint32_t lookupKeyToStorageKey(StringRef S)
Definition: NamedStreamMap.cpp:45
llvm::pdb::NamedStreamMap::NamedStreamMap
NamedStreamMap()
Definition: NamedStreamMap.cpp:49
llvm::pdb::raw_error_code::corrupt_file
@ corrupt_file
llvm::pdb::NamedStreamMapTraits::NamedStreamMapTraits
NamedStreamMapTraits(NamedStreamMap &NS)
Definition: NamedStreamMap.cpp:29
llvm::BinaryStreamReader::readInteger
Error readInteger(T &Dest)
Read an integer of the specified endianness into Dest and update the stream's offset.
Definition: BinaryStreamReader.h:75
llvm::pdb::NamedStreamMap::getString
StringRef getString(uint32_t Offset) const
Definition: NamedStreamMap.cpp:91
StringMap.h
llvm::StringMap< uint32_t >
llvm::pdb
Definition: ConcreteSymbolEnumerator.h:20
llvm::BinaryStreamReader
Provides read only access to a subclass of BinaryStream.
Definition: BinaryStreamReader.h:31
llvm::StringRef::end
iterator end() const
Definition: StringRef.h:129
llvm::pdb::NamedStreamMap::commit
Error commit(BinaryStreamWriter &Writer) const
Definition: NamedStreamMap.cpp:66
llvm::pdb::NamedStreamMap::hashString
uint32_t hashString(uint32_t Offset) const
Definition: NamedStreamMap.cpp:96
llvm::pdb::NamedStreamMap::load
Error load(BinaryStreamReader &Stream)
Definition: NamedStreamMap.cpp:51
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::pdb::NamedStreamMap::get
bool get(StringRef Stream, uint32_t &StreamNo) const
Definition: NamedStreamMap.cpp:100
llvm::joinErrors
Error joinErrors(Error E1, Error E2)
Concatenate errors.
Definition: Error.h:428
llvm::pdb::NamedStreamMap
Definition: NamedStreamMap.h:37
llvm::pdb::NamedStreamMapTraits::NS
NamedStreamMap * NS
Definition: NamedStreamMap.h:29
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
iterator_range.h
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
this
Analysis the ScalarEvolution expression for r is this
Definition: README.txt:8
BinaryStreamRef.h
uint32_t
llvm::append_range
void append_range(Container &C, Range &&R)
Wrapper function to append a range to a container.
Definition: STLExtras.h:1797
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
uint16_t
llvm::BinaryStreamReader::readFixedString
Error readFixedString(StringRef &Dest, uint32_t Length)
Read a Length byte string into Dest.
Definition: BinaryStreamReader.cpp:122
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
llvm::pdb::NamedStreamMapTraits::hashLookupKey
uint16_t hashLookupKey(StringRef S) const
Definition: NamedStreamMap.cpp:31
Hash.h
BinaryStreamWriter.h
Endian.h
llvm::StringRef::begin
iterator begin() const
Definition: StringRef.h:127
llvm::pdb::hashStringV1
uint32_t hashStringV1(StringRef Str)
Definition: Hash.cpp:20