blob: cfabc9cd1ad82b149bf6835f495d57ef7b8b0bb9 [file] [log] [blame]
Eugene Zelenko4fcfc192017-06-30 23:06:03 +00001//===- HashTable.cpp - PDB Hash Table -------------------------------------===//
Zachary Turner11036a92017-01-19 23:31:24 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000010#include "llvm/DebugInfo/PDB/Native/HashTable.h"
Zachary Turner11036a92017-01-19 23:31:24 +000011#include "llvm/ADT/Optional.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000012#include "llvm/DebugInfo/PDB/Native/RawError.h"
Eugene Zelenko4fcfc192017-06-30 23:06:03 +000013#include "llvm/Support/BinaryStreamReader.h"
14#include "llvm/Support/BinaryStreamWriter.h"
15#include "llvm/Support/Error.h"
16#include "llvm/Support/MathExtras.h"
17#include <algorithm>
18#include <cassert>
19#include <cstdint>
20#include <utility>
Zachary Turnera332fa32017-01-19 23:44:14 +000021
Zachary Turner11036a92017-01-19 23:31:24 +000022using namespace llvm;
23using namespace llvm::pdb;
24
Zachary Turnerebf03f62018-03-15 17:38:26 +000025Error llvm::pdb::readSparseBitVector(BinaryStreamReader &Stream,
Zachary Turner11036a92017-01-19 23:31:24 +000026 SparseBitVector<> &V) {
27 uint32_t NumWords;
Zachary Turner695ed562017-02-28 00:04:07 +000028 if (auto EC = Stream.readInteger(NumWords))
Zachary Turner11036a92017-01-19 23:31:24 +000029 return joinErrors(
30 std::move(EC),
31 make_error<RawError>(raw_error_code::corrupt_file,
32 "Expected hash table number of words"));
33
34 for (uint32_t I = 0; I != NumWords; ++I) {
35 uint32_t Word;
Zachary Turner695ed562017-02-28 00:04:07 +000036 if (auto EC = Stream.readInteger(Word))
Zachary Turner11036a92017-01-19 23:31:24 +000037 return joinErrors(std::move(EC),
38 make_error<RawError>(raw_error_code::corrupt_file,
39 "Expected hash table word"));
40 for (unsigned Idx = 0; Idx < 32; ++Idx)
41 if (Word & (1U << Idx))
42 V.set((I * 32) + Idx);
43 }
44 return Error::success();
45}
46
Zachary Turnerebf03f62018-03-15 17:38:26 +000047Error llvm::pdb::writeSparseBitVector(BinaryStreamWriter &Writer,
Zachary Turner11036a92017-01-19 23:31:24 +000048 SparseBitVector<> &Vec) {
Zachary Turneredbcbe02018-03-15 22:31:00 +000049 constexpr int BitsPerWord = 8 * sizeof(uint32_t);
50
Zachary Turner11036a92017-01-19 23:31:24 +000051 int ReqBits = Vec.find_last() + 1;
Zachary Turneredbcbe02018-03-15 22:31:00 +000052 uint32_t ReqWords = alignTo(ReqBits, BitsPerWord) / BitsPerWord;
53 if (auto EC = Writer.writeInteger(ReqWords))
Zachary Turner11036a92017-01-19 23:31:24 +000054 return joinErrors(
55 std::move(EC),
56 make_error<RawError>(raw_error_code::corrupt_file,
57 "Could not write linear map number of words"));
58
59 uint32_t Idx = 0;
Zachary Turneredbcbe02018-03-15 22:31:00 +000060 for (uint32_t I = 0; I != ReqWords; ++I) {
Zachary Turner11036a92017-01-19 23:31:24 +000061 uint32_t Word = 0;
62 for (uint32_t WordIdx = 0; WordIdx < 32; ++WordIdx, ++Idx) {
63 if (Vec.test(Idx))
64 Word |= (1 << WordIdx);
65 }
Zachary Turner695ed562017-02-28 00:04:07 +000066 if (auto EC = Writer.writeInteger(Word))
Zachary Turner11036a92017-01-19 23:31:24 +000067 return joinErrors(std::move(EC), make_error<RawError>(
68 raw_error_code::corrupt_file,
69 "Could not write linear map word"));
70 }
71 return Error::success();
72}