aboutsummaryrefslogtreecommitdiffstats
path: root/include/llvm/Bitcode/Deserialize.h
blob: 5f8e137559326faeca28403e15ea17d615bb82ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
//=- Deserialize.h - Generic Object Deserialization from Bitcode --*- C++ -*-=//
//
//                     The LLVM Compiler Infrastructure
//
// This file was developed by Ted Kremenek and is distributed under the
// University of Illinois Open Source License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file defines the interface for generic object deserialization from
// LLVM bitcode.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_BITCODE_SERIALIZE_INPUT
#define LLVM_BITCODE_SERIALIZE_INPUT

#include "llvm/Bitcode/BitstreamReader.h"
#include "llvm/Bitcode/Serialization.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/Allocator.h"
#include "llvm/Support/DataTypes.h"
#include <vector>

namespace llvm {
  
class Deserializer {  

  //===----------------------------------------------------------===//
  // Internal type definitions.
  //===----------------------------------------------------------===//
  
  struct BPNode {
    BPNode* Next;
    uintptr_t& PtrRef;
    
    BPNode(BPNode* n, uintptr_t& pref) 
      : Next(n), PtrRef(pref) {
        PtrRef = 0;
      }
  };
  
  struct BPEntry { 
    union { BPNode* Head; void* Ptr; };
    
    BPEntry() : Head(NULL) {}
    
    static inline bool isPod() { return true; }
    
    void SetPtr(BPNode*& FreeList, void* P);    
  };  
  
  class BPKey {
    unsigned Raw;
    
  public:
    BPKey(unsigned PtrId) : Raw(PtrId << 1) { assert (PtrId > 0); }
    BPKey(unsigned code, unsigned) : Raw(code) {}
    
    void MarkFinal() { Raw |= 0x1; }
    bool hasFinalPtr() const { return Raw & 0x1 ? true : false; }
    unsigned getID() const { return Raw >> 1; }
    
    static inline BPKey getEmptyKey() { return BPKey(0,0); }
    static inline BPKey getTombstoneKey() { return BPKey(1,0); }
    static inline unsigned getHashValue(const BPKey& K) { return K.Raw & ~0x1; }

    static bool isEqual(const BPKey& K1, const BPKey& K2) {
      return (K1.Raw ^ K2.Raw) & ~0x1 ? false : true;
    }
    
    static bool isPod() { return true; }
  };
  
  typedef llvm::DenseMap<BPKey,BPEntry,BPKey,BPEntry> MapTy;

  //===----------------------------------------------------------===//
  // Internal data members.
  //===----------------------------------------------------------===//
  
  BitstreamReader& Stream;
  SmallVector<uint64_t,10> Record;
  unsigned RecIdx;
  BumpPtrAllocator Allocator;
  BPNode* FreeList;
  MapTy BPatchMap;  
  
  //===----------------------------------------------------------===//
  // Public Interface.
  //===----------------------------------------------------------===//
  
public:
  Deserializer(BitstreamReader& stream);
  ~Deserializer();

  uint64_t ReadInt();
  int64_t ReadSInt();
  
  bool ReadBool() {
    return ReadInt() ? true : false;
  }

  template <typename T>
  inline T& Read(T& X) {
    SerializeTrait<T>::Read(*this,X);
    return X;
  }

  template <typename T>
  inline T* Materialize() {
    return SerializeTrait<T>::Materialize(*this);
  }
  
  char* ReadCStr(char* cstr = NULL, unsigned MaxLen=0, bool isNullTerm=true);
  void ReadCStr(std::vector<char>& buff, bool isNullTerm=false);

  template <typename T>
  inline T* ReadOwnedPtr(bool AutoRegister = true) {
    unsigned PtrID = ReadInt();    

    if (!PtrID)
      return NULL;
    
    T* x = SerializeTrait<T>::Materialize(*this);

    if (AutoRegister)
      RegisterPtr(PtrID,x);
    
    return x;
  }
  
  template <typename T>
  inline void ReadOwnedPtr(T*& Ptr, bool AutoRegister = true) {
    Ptr = ReadOwnedPtr<T>(AutoRegister);
  }
  
  template <typename T1, typename T2>
  void BatchReadOwnedPtrs(T1*& P1, T2*& P2,
                          bool A1=true, bool A2=true) {

    unsigned ID1 = ReadInt();
    unsigned ID2 = ReadInt();

    P1 = (ID1) ? SerializeTrait<T1>::Materialize(*this) : NULL;
    if (ID1 && A1) RegisterPtr(ID1,P1);

    P2 = (ID2) ? SerializeTrait<T2>::Materialize(*this) : NULL;
    if (ID2 && A2) RegisterPtr(ID2,P2);
  }

  template <typename T1, typename T2, typename T3>
  void BatchReadOwnedPtrs(T1*& P1, T2*& P2, T3*& P3,
                          bool A1=true, bool A2=true, bool A3=true) {
    
    unsigned ID1 = ReadInt();
    unsigned ID2 = ReadInt();
    unsigned ID3 = ReadInt();
    
    P1 = (ID1) ? SerializeTrait<T1>::Materialize(*this) : NULL;
    if (ID1 && A1) RegisterPtr(ID1,P1);    
    
    P2 = (ID2) ? SerializeTrait<T2>::Materialize(*this) : NULL;
    if (ID2 && A2) RegisterPtr(ID2,P2);
    
    P3 = (ID3) ? SerializeTrait<T2>::Materialize(*this) : NULL;
    if (ID3 && A3) RegisterPtr(ID3,P3);
  }
  
  template <typename T>
  void BatchReadOwnedPtrs(unsigned NumPtrs, T** Ptrs) {
    llvm::SmallVector<unsigned,20> PtrIDs;
    PtrIDs.reserve(NumPtrs);
    
    for (unsigned i = 0; i < NumPtrs; ++i)
      PtrIDs.push_back(ReadInt());
    
    for (unsigned i = 0; i < NumPtrs; ++i)
      Ptrs[i] = PtrIDs[i] ? SerializeTrait<T>::Materialize(*this) : NULL;
  }
    
  
  template <typename T>
  void ReadPtr(T*& PtrRef, bool AllowBackpatch = true) {
    ReadUIntPtr(reinterpret_cast<uintptr_t&>(PtrRef), AllowBackpatch);
  }
  
  template <typename T>
  void ReadPtr(const T*& PtrRef, bool AllowBackpatch = true) {
    ReadPtr(const_cast<T*&>(PtrRef), AllowBackpatch);
  }
  
  template <typename T>
  T* ReadPtr() { T* x; ReadPtr<T>(x,false); return x; }

  void ReadUIntPtr(uintptr_t& PtrRef, bool AllowBackpatch = true);
  
  template <typename T>
  T& ReadRef() {
    T* p = reinterpret_cast<T*>(ReadInternalRefPtr());
    return *p;
  }

  void RegisterPtr(unsigned PtrId, const void* Ptr);
  
  void RegisterPtr(const void* Ptr) {
    RegisterPtr(ReadInt(),Ptr);
  }
  
  bool AtEnd();

  bool inRecord();
private:
  void ReadRecord();  
  uintptr_t ReadInternalRefPtr();
  
  static inline bool HasFinalPtr(MapTy::value_type& V) {
    return V.first.hasFinalPtr();
  }
  
  static inline uintptr_t GetFinalPtr(MapTy::value_type& V) {
    return reinterpret_cast<uintptr_t>(V.second.Ptr);
  }
  
  static inline BPNode* GetBPNode(MapTy::value_type& V) {
    return V.second.Head;
  }
    
  static inline void SetBPNode(MapTy::value_type& V, BPNode* N) {
    V.second.Head = N;
  }
  
  void SetPtr(MapTy::value_type& V, const void* P) {
    V.first.MarkFinal();
    V.second.SetPtr(FreeList,const_cast<void*>(P));
  }
};
    
} // end namespace llvm

#endif