14fcfc199SEugene Zelenko //===- HashTable.cpp - PDB Hash Table -------------------------------------===//
26b6b8c4fSAdrian McCarthy //
36b6b8c4fSAdrian McCarthy //                     The LLVM Compiler Infrastructure
46b6b8c4fSAdrian McCarthy //
56b6b8c4fSAdrian McCarthy // This file is distributed under the University of Illinois Open Source
66b6b8c4fSAdrian McCarthy // License. See LICENSE.TXT for details.
76b6b8c4fSAdrian McCarthy //
86b6b8c4fSAdrian McCarthy //===----------------------------------------------------------------------===//
96b6b8c4fSAdrian McCarthy 
106b6b8c4fSAdrian McCarthy #include "llvm/DebugInfo/PDB/Native/HashTable.h"
116b6b8c4fSAdrian McCarthy #include "llvm/ADT/Optional.h"
126b6b8c4fSAdrian McCarthy #include "llvm/DebugInfo/PDB/Native/RawError.h"
134fcfc199SEugene Zelenko #include "llvm/Support/BinaryStreamReader.h"
144fcfc199SEugene Zelenko #include "llvm/Support/BinaryStreamWriter.h"
154fcfc199SEugene Zelenko #include "llvm/Support/Error.h"
164fcfc199SEugene Zelenko #include "llvm/Support/MathExtras.h"
174fcfc199SEugene Zelenko #include <algorithm>
184fcfc199SEugene Zelenko #include <cassert>
194fcfc199SEugene Zelenko #include <cstdint>
204fcfc199SEugene Zelenko #include <utility>
216b6b8c4fSAdrian McCarthy 
226b6b8c4fSAdrian McCarthy using namespace llvm;
236b6b8c4fSAdrian McCarthy using namespace llvm::pdb;
246b6b8c4fSAdrian McCarthy 
25ebf03f6cSZachary Turner Error llvm::pdb::readSparseBitVector(BinaryStreamReader &Stream,
266b6b8c4fSAdrian McCarthy                                      SparseBitVector<> &V) {
276b6b8c4fSAdrian McCarthy   uint32_t NumWords;
28695ed56bSZachary Turner   if (auto EC = Stream.readInteger(NumWords))
296b6b8c4fSAdrian McCarthy     return joinErrors(
306b6b8c4fSAdrian McCarthy         std::move(EC),
316b6b8c4fSAdrian McCarthy         make_error<RawError>(raw_error_code::corrupt_file,
326b6b8c4fSAdrian McCarthy                              "Expected hash table number of words"));
336b6b8c4fSAdrian McCarthy 
346b6b8c4fSAdrian McCarthy   for (uint32_t I = 0; I != NumWords; ++I) {
356b6b8c4fSAdrian McCarthy     uint32_t Word;
36695ed56bSZachary Turner     if (auto EC = Stream.readInteger(Word))
376b6b8c4fSAdrian McCarthy       return joinErrors(std::move(EC),
386b6b8c4fSAdrian McCarthy                         make_error<RawError>(raw_error_code::corrupt_file,
396b6b8c4fSAdrian McCarthy                                              "Expected hash table word"));
406b6b8c4fSAdrian McCarthy     for (unsigned Idx = 0; Idx < 32; ++Idx)
416b6b8c4fSAdrian McCarthy       if (Word & (1U << Idx))
426b6b8c4fSAdrian McCarthy         V.set((I * 32) + Idx);
436b6b8c4fSAdrian McCarthy   }
446b6b8c4fSAdrian McCarthy   return Error::success();
456b6b8c4fSAdrian McCarthy }
466b6b8c4fSAdrian McCarthy 
47ebf03f6cSZachary Turner Error llvm::pdb::writeSparseBitVector(BinaryStreamWriter &Writer,
486b6b8c4fSAdrian McCarthy                                       SparseBitVector<> &Vec) {
49*edbcbe0bSZachary Turner   constexpr int BitsPerWord = 8 * sizeof(uint32_t);
50*edbcbe0bSZachary Turner 
516b6b8c4fSAdrian McCarthy   int ReqBits = Vec.find_last() + 1;
52*edbcbe0bSZachary Turner   uint32_t ReqWords = alignTo(ReqBits, BitsPerWord) / BitsPerWord;
53*edbcbe0bSZachary Turner   if (auto EC = Writer.writeInteger(ReqWords))
546b6b8c4fSAdrian McCarthy     return joinErrors(
556b6b8c4fSAdrian McCarthy         std::move(EC),
566b6b8c4fSAdrian McCarthy         make_error<RawError>(raw_error_code::corrupt_file,
576b6b8c4fSAdrian McCarthy                              "Could not write linear map number of words"));
586b6b8c4fSAdrian McCarthy 
596b6b8c4fSAdrian McCarthy   uint32_t Idx = 0;
60*edbcbe0bSZachary Turner   for (uint32_t I = 0; I != ReqWords; ++I) {
616b6b8c4fSAdrian McCarthy     uint32_t Word = 0;
626b6b8c4fSAdrian McCarthy     for (uint32_t WordIdx = 0; WordIdx < 32; ++WordIdx, ++Idx) {
636b6b8c4fSAdrian McCarthy       if (Vec.test(Idx))
646b6b8c4fSAdrian McCarthy         Word |= (1 << WordIdx);
656b6b8c4fSAdrian McCarthy     }
66695ed56bSZachary Turner     if (auto EC = Writer.writeInteger(Word))
676b6b8c4fSAdrian McCarthy       return joinErrors(std::move(EC), make_error<RawError>(
686b6b8c4fSAdrian McCarthy                                            raw_error_code::corrupt_file,
696b6b8c4fSAdrian McCarthy                                            "Could not write linear map word"));
706b6b8c4fSAdrian McCarthy   }
716b6b8c4fSAdrian McCarthy   return Error::success();
726b6b8c4fSAdrian McCarthy }
73