1*0b57cec5SDimitry Andric //===- HashTable.cpp - PDB Hash Table -------------------------------------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric
9*0b57cec5SDimitry Andric #include "llvm/DebugInfo/PDB/Native/HashTable.h"
10*0b57cec5SDimitry Andric #include "llvm/ADT/Optional.h"
11*0b57cec5SDimitry Andric #include "llvm/DebugInfo/PDB/Native/RawError.h"
12*0b57cec5SDimitry Andric #include "llvm/Support/BinaryStreamReader.h"
13*0b57cec5SDimitry Andric #include "llvm/Support/BinaryStreamWriter.h"
14*0b57cec5SDimitry Andric #include "llvm/Support/Error.h"
15*0b57cec5SDimitry Andric #include "llvm/Support/MathExtras.h"
16*0b57cec5SDimitry Andric #include <algorithm>
17*0b57cec5SDimitry Andric #include <cassert>
18*0b57cec5SDimitry Andric #include <cstdint>
19*0b57cec5SDimitry Andric #include <utility>
20*0b57cec5SDimitry Andric
21*0b57cec5SDimitry Andric using namespace llvm;
22*0b57cec5SDimitry Andric using namespace llvm::pdb;
23*0b57cec5SDimitry Andric
readSparseBitVector(BinaryStreamReader & Stream,SparseBitVector<> & V)24*0b57cec5SDimitry Andric Error llvm::pdb::readSparseBitVector(BinaryStreamReader &Stream,
25*0b57cec5SDimitry Andric SparseBitVector<> &V) {
26*0b57cec5SDimitry Andric uint32_t NumWords;
27*0b57cec5SDimitry Andric if (auto EC = Stream.readInteger(NumWords))
28*0b57cec5SDimitry Andric return joinErrors(
29*0b57cec5SDimitry Andric std::move(EC),
30*0b57cec5SDimitry Andric make_error<RawError>(raw_error_code::corrupt_file,
31*0b57cec5SDimitry Andric "Expected hash table number of words"));
32*0b57cec5SDimitry Andric
33*0b57cec5SDimitry Andric for (uint32_t I = 0; I != NumWords; ++I) {
34*0b57cec5SDimitry Andric uint32_t Word;
35*0b57cec5SDimitry Andric if (auto EC = Stream.readInteger(Word))
36*0b57cec5SDimitry Andric return joinErrors(std::move(EC),
37*0b57cec5SDimitry Andric make_error<RawError>(raw_error_code::corrupt_file,
38*0b57cec5SDimitry Andric "Expected hash table word"));
39*0b57cec5SDimitry Andric for (unsigned Idx = 0; Idx < 32; ++Idx)
40*0b57cec5SDimitry Andric if (Word & (1U << Idx))
41*0b57cec5SDimitry Andric V.set((I * 32) + Idx);
42*0b57cec5SDimitry Andric }
43*0b57cec5SDimitry Andric return Error::success();
44*0b57cec5SDimitry Andric }
45*0b57cec5SDimitry Andric
writeSparseBitVector(BinaryStreamWriter & Writer,SparseBitVector<> & Vec)46*0b57cec5SDimitry Andric Error llvm::pdb::writeSparseBitVector(BinaryStreamWriter &Writer,
47*0b57cec5SDimitry Andric SparseBitVector<> &Vec) {
48*0b57cec5SDimitry Andric constexpr int BitsPerWord = 8 * sizeof(uint32_t);
49*0b57cec5SDimitry Andric
50*0b57cec5SDimitry Andric int ReqBits = Vec.find_last() + 1;
51*0b57cec5SDimitry Andric uint32_t ReqWords = alignTo(ReqBits, BitsPerWord) / BitsPerWord;
52*0b57cec5SDimitry Andric if (auto EC = Writer.writeInteger(ReqWords))
53*0b57cec5SDimitry Andric return joinErrors(
54*0b57cec5SDimitry Andric std::move(EC),
55*0b57cec5SDimitry Andric make_error<RawError>(raw_error_code::corrupt_file,
56*0b57cec5SDimitry Andric "Could not write linear map number of words"));
57*0b57cec5SDimitry Andric
58*0b57cec5SDimitry Andric uint32_t Idx = 0;
59*0b57cec5SDimitry Andric for (uint32_t I = 0; I != ReqWords; ++I) {
60*0b57cec5SDimitry Andric uint32_t Word = 0;
61*0b57cec5SDimitry Andric for (uint32_t WordIdx = 0; WordIdx < 32; ++WordIdx, ++Idx) {
62*0b57cec5SDimitry Andric if (Vec.test(Idx))
63*0b57cec5SDimitry Andric Word |= (1 << WordIdx);
64*0b57cec5SDimitry Andric }
65*0b57cec5SDimitry Andric if (auto EC = Writer.writeInteger(Word))
66*0b57cec5SDimitry Andric return joinErrors(std::move(EC), make_error<RawError>(
67*0b57cec5SDimitry Andric raw_error_code::corrupt_file,
68*0b57cec5SDimitry Andric "Could not write linear map word"));
69*0b57cec5SDimitry Andric }
70*0b57cec5SDimitry Andric return Error::success();
71*0b57cec5SDimitry Andric }
72