14fcfc199SEugene Zelenko //===- HashTable.cpp - PDB Hash Table -------------------------------------===//
26b6b8c4fSAdrian McCarthy //
3*2946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*2946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
5*2946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66b6b8c4fSAdrian McCarthy //
76b6b8c4fSAdrian McCarthy //===----------------------------------------------------------------------===//
86b6b8c4fSAdrian McCarthy
96b6b8c4fSAdrian McCarthy #include "llvm/DebugInfo/PDB/Native/HashTable.h"
106b6b8c4fSAdrian McCarthy #include "llvm/DebugInfo/PDB/Native/RawError.h"
114fcfc199SEugene Zelenko #include "llvm/Support/BinaryStreamReader.h"
124fcfc199SEugene Zelenko #include "llvm/Support/BinaryStreamWriter.h"
134fcfc199SEugene Zelenko #include "llvm/Support/Error.h"
144fcfc199SEugene Zelenko #include "llvm/Support/MathExtras.h"
154fcfc199SEugene Zelenko #include <cstdint>
164fcfc199SEugene Zelenko #include <utility>
176b6b8c4fSAdrian McCarthy
186b6b8c4fSAdrian McCarthy using namespace llvm;
196b6b8c4fSAdrian McCarthy using namespace llvm::pdb;
206b6b8c4fSAdrian McCarthy
readSparseBitVector(BinaryStreamReader & Stream,SparseBitVector<> & V)21ebf03f6cSZachary Turner Error llvm::pdb::readSparseBitVector(BinaryStreamReader &Stream,
226b6b8c4fSAdrian McCarthy SparseBitVector<> &V) {
236b6b8c4fSAdrian McCarthy uint32_t NumWords;
24695ed56bSZachary Turner if (auto EC = Stream.readInteger(NumWords))
256b6b8c4fSAdrian McCarthy return joinErrors(
266b6b8c4fSAdrian McCarthy std::move(EC),
276b6b8c4fSAdrian McCarthy make_error<RawError>(raw_error_code::corrupt_file,
286b6b8c4fSAdrian McCarthy "Expected hash table number of words"));
296b6b8c4fSAdrian McCarthy
306b6b8c4fSAdrian McCarthy for (uint32_t I = 0; I != NumWords; ++I) {
316b6b8c4fSAdrian McCarthy uint32_t Word;
32695ed56bSZachary Turner if (auto EC = Stream.readInteger(Word))
336b6b8c4fSAdrian McCarthy return joinErrors(std::move(EC),
346b6b8c4fSAdrian McCarthy make_error<RawError>(raw_error_code::corrupt_file,
356b6b8c4fSAdrian McCarthy "Expected hash table word"));
366b6b8c4fSAdrian McCarthy for (unsigned Idx = 0; Idx < 32; ++Idx)
376b6b8c4fSAdrian McCarthy if (Word & (1U << Idx))
386b6b8c4fSAdrian McCarthy V.set((I * 32) + Idx);
396b6b8c4fSAdrian McCarthy }
406b6b8c4fSAdrian McCarthy return Error::success();
416b6b8c4fSAdrian McCarthy }
426b6b8c4fSAdrian McCarthy
writeSparseBitVector(BinaryStreamWriter & Writer,SparseBitVector<> & Vec)43ebf03f6cSZachary Turner Error llvm::pdb::writeSparseBitVector(BinaryStreamWriter &Writer,
446b6b8c4fSAdrian McCarthy SparseBitVector<> &Vec) {
45edbcbe0bSZachary Turner constexpr int BitsPerWord = 8 * sizeof(uint32_t);
46edbcbe0bSZachary Turner
476b6b8c4fSAdrian McCarthy int ReqBits = Vec.find_last() + 1;
48edbcbe0bSZachary Turner uint32_t ReqWords = alignTo(ReqBits, BitsPerWord) / BitsPerWord;
49edbcbe0bSZachary Turner if (auto EC = Writer.writeInteger(ReqWords))
506b6b8c4fSAdrian McCarthy return joinErrors(
516b6b8c4fSAdrian McCarthy std::move(EC),
526b6b8c4fSAdrian McCarthy make_error<RawError>(raw_error_code::corrupt_file,
536b6b8c4fSAdrian McCarthy "Could not write linear map number of words"));
546b6b8c4fSAdrian McCarthy
556b6b8c4fSAdrian McCarthy uint32_t Idx = 0;
56edbcbe0bSZachary Turner for (uint32_t I = 0; I != ReqWords; ++I) {
576b6b8c4fSAdrian McCarthy uint32_t Word = 0;
586b6b8c4fSAdrian McCarthy for (uint32_t WordIdx = 0; WordIdx < 32; ++WordIdx, ++Idx) {
596b6b8c4fSAdrian McCarthy if (Vec.test(Idx))
606b6b8c4fSAdrian McCarthy Word |= (1 << WordIdx);
616b6b8c4fSAdrian McCarthy }
62695ed56bSZachary Turner if (auto EC = Writer.writeInteger(Word))
636b6b8c4fSAdrian McCarthy return joinErrors(std::move(EC), make_error<RawError>(
646b6b8c4fSAdrian McCarthy raw_error_code::corrupt_file,
656b6b8c4fSAdrian McCarthy "Could not write linear map word"));
666b6b8c4fSAdrian McCarthy }
676b6b8c4fSAdrian McCarthy return Error::success();
686b6b8c4fSAdrian McCarthy }
69