Updated backpatching during object deserialization to support "smart"
[oota-llvm.git] / include / llvm / Bitcode / Deserialize.h
1 //=- Deserialize.h - Generic Object Deserialization from Bitcode --*- C++ -*-=//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Ted Kremenek and is distributed under the
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the interface for generic object deserialization from
11 // LLVM bitcode.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_BITCODE_SERIALIZE_INPUT
16 #define LLVM_BITCODE_SERIALIZE_INPUT
17
18 #include "llvm/Bitcode/BitstreamReader.h"
19 #include "llvm/Bitcode/Serialization.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/Support/Allocator.h"
23 #include "llvm/Support/DataTypes.h"
24 #include <vector>
25
26 namespace llvm {
27   
28 class Deserializer {  
29   BitstreamReader& Stream;
30   SmallVector<uint64_t,10> Record;
31   unsigned RecIdx;
32   BumpPtrAllocator Allocator;
33   
34   struct PtrIdInfo {
35     static inline unsigned getEmptyKey() { return ~((unsigned) 0x0); }
36     static inline unsigned getTombstoneKey() { return getEmptyKey()-1; }
37     static inline unsigned getHashValue(unsigned X) { return X; }
38     static inline bool isEqual(unsigned X, unsigned Y) { return X == Y; }
39     static inline bool isPod() { return true; }
40   };
41   
42   struct BPatchNode {
43     BPatchNode* const Next;
44     uintptr_t& PtrRef;
45     BPatchNode(BPatchNode* n, void*& pref) 
46       : Next(n), PtrRef(reinterpret_cast<uintptr_t&>(pref)) {
47         PtrRef = 0;
48       }
49   };
50   
51   struct BPatchEntry {
52     BPatchNode* Head;
53     void* Ptr;    
54     BPatchEntry() : Head(NULL), Ptr(NULL) {}
55     static inline bool isPod() { return true; }
56   };  
57   
58   typedef llvm::DenseMap<unsigned,BPatchEntry,PtrIdInfo,BPatchEntry> MapTy;
59
60   MapTy BPatchMap;  
61   
62 public:
63   Deserializer(BitstreamReader& stream);
64   ~Deserializer();
65
66   uint64_t ReadInt();
67   bool ReadBool() {
68     return ReadInt() ? true : false;
69   }
70
71   template <typename T>
72   inline T& Read(T& X) {
73     SerializeTrait<T>::Read(*this,X);
74     return X;
75   }
76   
77   template <typename T>
78   inline T ReadVal() {
79     return SerializeTrait<T>::ReadVal(*this);
80   }
81
82   template <typename T>
83   inline T* Materialize() {
84     return SerializeTrait<T>::Materialize(*this);
85   }
86   
87   char* ReadCStr(char* cstr = NULL, unsigned MaxLen=0, bool isNullTerm=true);
88   void ReadCStr(std::vector<char>& buff, bool isNullTerm=false);
89
90   template <typename T>
91   inline T* ReadOwnedPtr() {    
92     unsigned PtrId = ReadInt();
93
94     if (PtrId == 0)
95       return NULL;
96     
97     T* x = SerializeTrait<T>::Materialize(*this);
98     RegisterPtr(PtrId,x);
99     return x;
100   }
101   
102   void ReadPtr(void*& PtrRef);  
103   void RegisterPtr(unsigned PtrId, void* Ptr);
104
105
106   void BackpatchPointers();
107 private:
108   void ReadRecord();  
109   bool inRecord();
110 };
111     
112 } // end namespace llvm
113
114 #endif