HashTable.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. //===- HashTable.cpp - PDB Hash Table -------------------------------------===//
  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. #include "llvm/DebugInfo/PDB/Native/HashTable.h"
  9. #include "llvm/ADT/Optional.h"
  10. #include "llvm/DebugInfo/PDB/Native/RawError.h"
  11. #include "llvm/Support/BinaryStreamReader.h"
  12. #include "llvm/Support/BinaryStreamWriter.h"
  13. #include "llvm/Support/Error.h"
  14. #include "llvm/Support/MathExtras.h"
  15. #include <algorithm>
  16. #include <cassert>
  17. #include <cstdint>
  18. #include <utility>
  19. using namespace llvm;
  20. using namespace llvm::pdb;
  21. Error llvm::pdb::readSparseBitVector(BinaryStreamReader &Stream,
  22. SparseBitVector<> &V) {
  23. uint32_t NumWords;
  24. if (auto EC = Stream.readInteger(NumWords))
  25. return joinErrors(
  26. std::move(EC),
  27. make_error<RawError>(raw_error_code::corrupt_file,
  28. "Expected hash table number of words"));
  29. for (uint32_t I = 0; I != NumWords; ++I) {
  30. uint32_t Word;
  31. if (auto EC = Stream.readInteger(Word))
  32. return joinErrors(std::move(EC),
  33. make_error<RawError>(raw_error_code::corrupt_file,
  34. "Expected hash table word"));
  35. for (unsigned Idx = 0; Idx < 32; ++Idx)
  36. if (Word & (1U << Idx))
  37. V.set((I * 32) + Idx);
  38. }
  39. return Error::success();
  40. }
  41. Error llvm::pdb::writeSparseBitVector(BinaryStreamWriter &Writer,
  42. SparseBitVector<> &Vec) {
  43. constexpr int BitsPerWord = 8 * sizeof(uint32_t);
  44. int ReqBits = Vec.find_last() + 1;
  45. uint32_t ReqWords = alignTo(ReqBits, BitsPerWord) / BitsPerWord;
  46. if (auto EC = Writer.writeInteger(ReqWords))
  47. return joinErrors(
  48. std::move(EC),
  49. make_error<RawError>(raw_error_code::corrupt_file,
  50. "Could not write linear map number of words"));
  51. uint32_t Idx = 0;
  52. for (uint32_t I = 0; I != ReqWords; ++I) {
  53. uint32_t Word = 0;
  54. for (uint32_t WordIdx = 0; WordIdx < 32; ++WordIdx, ++Idx) {
  55. if (Vec.test(Idx))
  56. Word |= (1 << WordIdx);
  57. }
  58. if (auto EC = Writer.writeInteger(Word))
  59. return joinErrors(std::move(EC), make_error<RawError>(
  60. raw_error_code::corrupt_file,
  61. "Could not write linear map word"));
  62. }
  63. return Error::success();
  64. }