Skip to content

Commit dc32f35

Browse files
committed
[PDB] Move PDB functions to a separate file.
We are going to use the hash functions from TPI streams. Differential Revision: http://reviews.llvm.org/D21142 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@272223 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 1ead239 commit dc32f35

File tree

4 files changed

+101
-59
lines changed

4 files changed

+101
-59
lines changed

include/llvm/DebugInfo/PDB/Raw/Hash.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===- Hash.h - PDB hash functions ------------------------------*- C++ -*-===//
2+
//
3+
// The LLVM Compiler Infrastructure
4+
//
5+
// This file is distributed under the University of Illinois Open Source
6+
// License. See LICENSE.TXT for details.
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
#ifndef LLVM_DEBUGINFO_PDB_RAW_HASH_H
11+
#define LLVM_DEBUGINFO_PDB_RAW_HASH_H
12+
13+
#include "llvm/ADT/StringRef.h"
14+
15+
namespace llvm {
16+
namespace pdb {
17+
uint32_t HashStringV1(StringRef Str);
18+
uint32_t HashStringV2(StringRef Str);
19+
}
20+
}
21+
22+
#endif

lib/DebugInfo/PDB/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ endif()
3030
add_pdb_impl_folder(Raw
3131
Raw/DbiStream.cpp
3232
Raw/EnumTables.cpp
33+
Raw/Hash.cpp
3334
Raw/IndexedStreamData.cpp
3435
Raw/InfoStream.cpp
3536
Raw/MappedBlockStream.cpp

lib/DebugInfo/PDB/Raw/Hash.cpp

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
//===- Hash.cpp - PDB Hash Functions --------------------------------------===//
2+
//
3+
// The LLVM Compiler Infrastructure
4+
//
5+
// This file is distributed under the University of Illinois Open Source
6+
// License. See LICENSE.TXT for details.
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
#include "llvm/DebugInfo/PDB/Raw/Hash.h"
11+
12+
#include "llvm/ADT/ArrayRef.h"
13+
#include "llvm/Support/Endian.h"
14+
15+
using namespace llvm;
16+
using namespace llvm::support;
17+
18+
// Corresponds to `Hasher::lhashPbCb` in PDB/include/misc.h.
19+
// Used for name hash table and TPI/IPI hashes.
20+
uint32_t pdb::HashStringV1(StringRef Str) {
21+
uint32_t Result = 0;
22+
uint32_t Size = Str.size();
23+
24+
ArrayRef<ulittle32_t> Longs(reinterpret_cast<const ulittle32_t *>(Str.data()),
25+
Size / 4);
26+
27+
for (auto Value : Longs)
28+
Result ^= Value;
29+
30+
const uint8_t *Remainder = reinterpret_cast<const uint8_t *>(Longs.end());
31+
uint32_t RemainderSize = Size % 4;
32+
33+
// Maximum of 3 bytes left. Hash a 2 byte word if possible, then hash the
34+
// possibly remaining 1 byte.
35+
if (RemainderSize >= 2) {
36+
uint16_t Value = *reinterpret_cast<const ulittle16_t *>(Remainder);
37+
Result ^= static_cast<uint32_t>(Value);
38+
Remainder += 2;
39+
RemainderSize -= 2;
40+
}
41+
42+
// hash possible odd byte
43+
if (RemainderSize == 1) {
44+
Result ^= *(Remainder++);
45+
}
46+
47+
const uint32_t toLowerMask = 0x20202020;
48+
Result |= toLowerMask;
49+
Result ^= (Result >> 11);
50+
51+
return Result ^ (Result >> 16);
52+
}
53+
54+
// Corresponds to `HasherV2::HashULONG` in PDB/include/misc.h.
55+
// Used for name hash table.
56+
uint32_t pdb::HashStringV2(StringRef Str) {
57+
uint32_t Hash = 0xb170a1bf;
58+
59+
ArrayRef<char> Buffer(Str.begin(), Str.end());
60+
61+
ArrayRef<ulittle32_t> Items(
62+
reinterpret_cast<const ulittle32_t *>(Buffer.data()),
63+
Buffer.size() / sizeof(ulittle32_t));
64+
for (ulittle32_t Item : Items) {
65+
Hash += Item;
66+
Hash += (Hash << 10);
67+
Hash ^= (Hash >> 6);
68+
}
69+
Buffer = Buffer.slice(Items.size() * sizeof(ulittle32_t));
70+
for (uint8_t Item : Buffer) {
71+
Hash += Item;
72+
Hash += (Hash << 10);
73+
Hash ^= (Hash >> 6);
74+
}
75+
76+
return Hash * 1664525L + 1013904223L;
77+
}

lib/DebugInfo/PDB/Raw/NameHashTable.cpp

Lines changed: 1 addition & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -11,72 +11,14 @@
1111

1212
#include "llvm/ADT/ArrayRef.h"
1313
#include "llvm/DebugInfo/CodeView/StreamReader.h"
14+
#include "llvm/DebugInfo/PDB/Raw/Hash.h"
1415
#include "llvm/DebugInfo/PDB/Raw/RawError.h"
1516
#include "llvm/Support/Endian.h"
1617

1718
using namespace llvm;
1819
using namespace llvm::support;
1920
using namespace llvm::pdb;
2021

21-
// Corresponds to `Hasher::lhashPbCb` in PDB/include/misc.h.
22-
static inline uint32_t HashStringV1(StringRef Str) {
23-
uint32_t Result = 0;
24-
uint32_t Size = Str.size();
25-
26-
ArrayRef<ulittle32_t> Longs(reinterpret_cast<const ulittle32_t *>(Str.data()),
27-
Size / 4);
28-
29-
for (auto Value : Longs)
30-
Result ^= Value;
31-
32-
const uint8_t *Remainder = reinterpret_cast<const uint8_t *>(Longs.end());
33-
uint32_t RemainderSize = Size - Longs.size() * 4;
34-
35-
// Maximum of 3 bytes left. Hash a 2 byte word if possible, then hash the
36-
// possibly remaining 1 byte.
37-
if (RemainderSize >= 2) {
38-
uint16_t Value = *reinterpret_cast<const ulittle16_t *>(Remainder);
39-
Result ^= static_cast<uint32_t>(Value);
40-
Remainder += 2;
41-
RemainderSize -= 2;
42-
}
43-
44-
// hash possible odd byte
45-
if (RemainderSize == 1) {
46-
Result ^= *(Remainder++);
47-
}
48-
49-
const uint32_t toLowerMask = 0x20202020;
50-
Result |= toLowerMask;
51-
Result ^= (Result >> 11);
52-
53-
return Result ^ (Result >> 16);
54-
}
55-
56-
// Corresponds to `HasherV2::HashULONG` in PDB/include/misc.h.
57-
static inline uint32_t HashStringV2(StringRef Str) {
58-
uint32_t Hash = 0xb170a1bf;
59-
60-
ArrayRef<char> Buffer(Str.begin(), Str.end());
61-
62-
ArrayRef<ulittle32_t> Items(
63-
reinterpret_cast<const ulittle32_t *>(Buffer.data()),
64-
Buffer.size() / sizeof(ulittle32_t));
65-
for (ulittle32_t Item : Items) {
66-
Hash += Item;
67-
Hash += (Hash << 10);
68-
Hash ^= (Hash >> 6);
69-
}
70-
Buffer = Buffer.slice(Items.size() * sizeof(ulittle32_t));
71-
for (uint8_t Item : Buffer) {
72-
Hash += Item;
73-
Hash += (Hash << 10);
74-
Hash ^= (Hash >> 6);
75-
}
76-
77-
return Hash * 1664525L + 1013904223L;
78-
}
79-
8022
NameHashTable::NameHashTable() : Signature(0), HashVersion(0), NameCount(0) {}
8123

8224
Error NameHashTable::load(codeview::StreamReader &Stream) {

0 commit comments

Comments
 (0)