HashTable.cpp 2.5 KB

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