LLVM  15.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 
11 #include "llvm/ADT/StringMap.h"
12 #include "llvm/ADT/StringRef.h"
18 #include "llvm/Support/Endian.h"
19 #include "llvm/Support/Error.h"
20 #include <algorithm>
21 #include <cassert>
22 #include <cstdint>
23 
24 using namespace llvm;
25 using namespace llvm::pdb;
26 
28 
30  // In the reference implementation, this uses
31  // HASH Hasher<ULONG*, USHORT*>::hashPbCb(PB pb, size_t cb, ULONG ulMod).
32  // Here, the type HASH is a typedef of unsigned short.
33  // ** It is not a bug that we truncate the result of hashStringV1, in fact
34  // it is a bug if we do not! **
35  // See NMTNI::hash() in the reference implementation.
36  return static_cast<uint16_t>(hashStringV1(S));
37 }
38 
40  return NS->getString(Offset);
41 }
42 
44  return NS->appendStringData(S);
45 }
46 
47 NamedStreamMap::NamedStreamMap() : HashTraits(*this), OffsetIndexMap(1) {}
48 
50  uint32_t StringBufferSize;
51  if (auto EC = Stream.readInteger(StringBufferSize))
52  return joinErrors(std::move(EC),
53  make_error<RawError>(raw_error_code::corrupt_file,
54  "Expected string buffer size"));
55 
56  StringRef Buffer;
57  if (auto EC = Stream.readFixedString(Buffer, StringBufferSize))
58  return EC;
59  NamesBuffer.assign(Buffer.begin(), Buffer.end());
60 
61  return OffsetIndexMap.load(Stream);
62 }
63 
65  // The first field is the number of bytes of string data.
66  if (auto EC = Writer.writeInteger<uint32_t>(NamesBuffer.size()))
67  return EC;
68 
69  // Then the actual string data.
70  StringRef Data(NamesBuffer.data(), NamesBuffer.size());
71  if (auto EC = Writer.writeFixedString(Data))
72  return EC;
73 
74  // And finally the Offset Index map.
75  if (auto EC = OffsetIndexMap.commit(Writer))
76  return EC;
77 
78  return Error::success();
79 }
80 
82  return sizeof(uint32_t) // String data size
83  + NamesBuffer.size() // String data
84  + OffsetIndexMap.calculateSerializedLength(); // Offset Index Map
85 }
86 
87 uint32_t NamedStreamMap::size() const { return OffsetIndexMap.size(); }
88 
90  assert(NamesBuffer.size() > Offset);
91  return StringRef(NamesBuffer.data() + Offset);
92 }
93 
95  return hashStringV1(getString(Offset));
96 }
97 
98 bool NamedStreamMap::get(StringRef Stream, uint32_t &StreamNo) const {
99  auto Iter = OffsetIndexMap.find_as(Stream, HashTraits);
100  if (Iter == OffsetIndexMap.end())
101  return false;
102  StreamNo = (*Iter).second;
103  return true;
104 }
105 
107  StringMap<uint32_t> Result;
108  for (const auto &Entry : OffsetIndexMap) {
109  StringRef Stream(NamesBuffer.data() + Entry.first);
110  Result.try_emplace(Stream, Entry.second);
111  }
112  return Result;
113 }
114 
116  uint32_t Offset = NamesBuffer.size();
117  llvm::append_range(NamesBuffer, S);
118  NamesBuffer.push_back('\0');
119  return Offset;
120 }
121 
122 void NamedStreamMap::set(StringRef Stream, uint32_t StreamNo) {
123  OffsetIndexMap.set_as(Stream, support::ulittle32_t(StreamNo), HashTraits);
124 }
llvm::pdb::NamedStreamMap::set
void set(StringRef Stream, uint32_t StreamNo)
Definition: NamedStreamMap.cpp:122
BinaryStreamReader.h
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::BinaryStreamWriter::writeInteger
Error writeInteger(T Value)
Write the integer Value to the underlying stream in the specified endianness.
Definition: BinaryStreamWriter.h:58
llvm::pdb::NamedStreamMapTraits::storageKeyToLookupKey
StringRef storageKeyToLookupKey(uint32_t Offset) const
Definition: NamedStreamMap.cpp:39
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:87
StringRef.h
llvm::pdb::NamedStreamMap::appendStringData
uint32_t appendStringData(StringRef S)
Definition: NamedStreamMap.cpp:115
NamedStreamMap.h
llvm::BinaryStreamWriter
Provides write only access to a subclass of WritableBinaryStream.
Definition: BinaryStreamWriter.h:30
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition: Error.h:329
llvm::pdb::NamedStreamMap::entries
StringMap< uint32_t > entries() const
Definition: NamedStreamMap.cpp:106
Error.h
HashTable.h
llvm::pdb::NamedStreamMap::calculateSerializedLength
uint32_t calculateSerializedLength() const
Definition: NamedStreamMap.cpp:81
RawError.h
llvm::Data
@ Data
Definition: SIMachineScheduler.h:55
llvm::pdb::NamedStreamMapTraits::lookupKeyToStorageKey
uint32_t lookupKeyToStorageKey(StringRef S)
Definition: NamedStreamMap.cpp:43
SparseBitVector.h
llvm::pdb::NamedStreamMap::NamedStreamMap
NamedStreamMap()
Definition: NamedStreamMap.cpp:47
llvm::pdb::raw_error_code::corrupt_file
@ corrupt_file
llvm::pdb::NamedStreamMapTraits::NamedStreamMapTraits
NamedStreamMapTraits(NamedStreamMap &NS)
Definition: NamedStreamMap.cpp:27
llvm::pdb::NamedStreamMap::getString
StringRef getString(uint32_t Offset) const
Definition: NamedStreamMap.cpp:89
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:29
llvm::StringRef::end
iterator end() const
Definition: StringRef.h:130
llvm::pdb::NamedStreamMap::commit
Error commit(BinaryStreamWriter &Writer) const
Definition: NamedStreamMap.cpp:64
llvm::pdb::NamedStreamMap::hashString
uint32_t hashString(uint32_t Offset) const
Definition: NamedStreamMap.cpp:94
llvm::pdb::NamedStreamMap::load
Error load(BinaryStreamReader &Stream)
Definition: NamedStreamMap.cpp:49
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:98
llvm::joinErrors
Error joinErrors(Error E1, Error E2)
Concatenate errors.
Definition: Error.h:426
llvm::pdb::NamedStreamMap
Definition: NamedStreamMap.h:36
llvm::pdb::NamedStreamMapTraits::NS
NamedStreamMap * NS
Definition: NamedStreamMap.h:28
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
this
Analysis the ScalarEvolution expression for r is this
Definition: README.txt:8
uint32_t
llvm::append_range
void append_range(Container &C, Range &&R)
Wrapper function to append a range to a container.
Definition: STLExtras.h:1813
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::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:155
llvm::pdb::NamedStreamMapTraits::hashLookupKey
uint16_t hashLookupKey(StringRef S) const
Definition: NamedStreamMap.cpp:29
Hash.h
BinaryStreamWriter.h
Endian.h
llvm::StringRef::begin
iterator begin() const
Definition: StringRef.h:128
llvm::pdb::hashStringV1
uint32_t hashStringV1(StringRef Str)
Definition: Hash.cpp:20