X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=include%2Fllvm%2FBitcode%2FDeserialize.h;h=3266038a3d98d6ee99cbc8fe16b6294f82676128;hb=43afbce8bae74629162779f5a14bde172156840f;hp=0398e434ca49ebcd0ba6bc8e8881a09b95892a42;hpb=0a6d98e10e3b5080861bc1e06c2053d7941f319a;p=oota-llvm.git diff --git a/include/llvm/Bitcode/Deserialize.h b/include/llvm/Bitcode/Deserialize.h index 0398e434ca4..3266038a3d9 100644 --- a/include/llvm/Bitcode/Deserialize.h +++ b/include/llvm/Bitcode/Deserialize.h @@ -2,8 +2,8 @@ // // 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 is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// // @@ -20,81 +20,125 @@ #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/SmallVector.h" #include "llvm/Support/Allocator.h" -#include "llvm/Support/DataTypes.h" +#include "llvm/System/DataTypes.h" #include namespace llvm { + +struct BPNode { + BPNode* Next; + uintptr_t& PtrRef; -class Deserializer { + BPNode(BPNode* n, uintptr_t& pref) + : Next(n), PtrRef(pref) { + PtrRef = 0; + } +}; - //===----------------------------------------------------------===// - // Internal type definitions. - //===----------------------------------------------------------===// +struct BPEntry { + union { BPNode* Head; void* Ptr; }; + BPEntry() : Head(NULL) {} + void SetPtr(BPNode*& FreeList, void* P); +}; + +class BPKey { + unsigned Raw; +public: + BPKey(SerializedPtrID PtrId) : Raw(PtrId << 1) { assert (PtrId > 0); } + BPKey(unsigned code, unsigned) : Raw(code) {} - struct BPNode { - BPNode* Next; - uintptr_t& PtrRef; - - BPNode(BPNode* n, uintptr_t& pref) - : Next(n), PtrRef(pref) { - PtrRef = 0; - } - }; + void MarkFinal() { Raw |= 0x1; } + bool hasFinalPtr() const { return Raw & 0x1 ? true : false; } + SerializedPtrID getID() const { return Raw >> 1; } - struct BPEntry { - union { BPNode* Head; void* Ptr; }; - - BPEntry() : Head(NULL) {} - - static inline bool isPod() { return true; } - - void SetPtr(BPNode*& FreeList, void* P); - }; + 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; } - 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; } - }; + static bool isEqual(const BPKey& K1, const BPKey& K2) { + return (K1.Raw ^ K2.Raw) & ~0x1 ? false : true; + } +}; + +template <> +struct isPodLike { static const bool value = true; }; +template <> +struct isPodLike { static const bool value = true; }; +class Deserializer { + + //===----------------------------------------------------------===// + // Internal type definitions. + //===----------------------------------------------------------===// + + typedef llvm::DenseMap MapTy; + //===----------------------------------------------------------===// + // Publicly visible types. + //===----------------------------------------------------------===// + +public: + struct Location { + uint64_t BitNo; + unsigned BlockID; + unsigned NumWords; + + Location(uint64_t bit, unsigned bid, unsigned words) + : BitNo(bit), BlockID(bid), NumWords(words) {} + + Location() : BitNo(0), BlockID(0), NumWords(0) {} + + Location& operator=(Location& RHS) { + BitNo = RHS.BitNo; + BlockID = RHS.BlockID; + NumWords = RHS.NumWords; + return *this; + } + + bool operator==(const Location& RHS) const { return BitNo == RHS.BitNo; } + bool operator!=(const Location& RHS) const { return BitNo != RHS.BitNo; } + + bool contains(const Location& RHS) const { + if (RHS.BitNo < BitNo) + return false; + + if ((RHS.BitNo - BitNo) >> 5 < NumWords) + return true; + + return false; + } + }; + //===----------------------------------------------------------===// // Internal data members. //===----------------------------------------------------------===// - - BitstreamReader& Stream; - SmallVector Record; + +private: + BitstreamCursor Stream; + SmallVector Record; unsigned RecIdx; BumpPtrAllocator Allocator; BPNode* FreeList; - MapTy BPatchMap; - + MapTy BPatchMap; + llvm::SmallVector BlockStack; + unsigned AbbrevNo; + unsigned RecordCode; + uint64_t StreamStart; + //===----------------------------------------------------------===// // Public Interface. //===----------------------------------------------------------===// - + public: Deserializer(BitstreamReader& stream); ~Deserializer(); uint64_t ReadInt(); + int64_t ReadSInt(); + SerializedPtrID ReadPtrID() { return (SerializedPtrID) ReadInt(); } + + bool ReadBool() { return ReadInt() ? true : false; } @@ -106,83 +150,367 @@ public: } template - inline T* Materialize() { - return SerializeTrait::Materialize(*this); + inline T* Create() { + return SerializeTrait::Create(*this); } - + char* ReadCStr(char* cstr = NULL, unsigned MaxLen=0, bool isNullTerm=true); - void ReadCStr(std::vector& buff, bool isNullTerm=false); + void ReadCStr(std::vector& buff, bool isNullTerm=false, unsigned Idx=0); template - inline T* ReadOwnedPtr() { - unsigned PtrId = ReadInt(); + inline T* ReadOwnedPtr(bool AutoRegister = true) { + SerializedPtrID PtrID = ReadPtrID(); - if (PtrId == 0) + if (!PtrID) return NULL; - - T* x = SerializeTrait::Materialize(*this); - RegisterPtr(PtrId,x); + + T* x = SerializeTrait::Create(*this); + + if (AutoRegister) + RegisterPtr(PtrID,x); + return x; } - + + template + inline T* ReadOwnedPtr(Arg1& arg1, bool AutoRegister = true) { + SerializedPtrID PtrID = ReadPtrID(); + + if (!PtrID) + return NULL; + + T* x = SerializeTrait::Create(*this, arg1); + + if (AutoRegister) + RegisterPtr(PtrID,x); + + return x; + } + template - inline void ReadOwnedPtr(T*& Ptr) { - Ptr = ReadOwnedPtr(); + inline void ReadOwnedPtr(T*& Ptr, bool AutoRegister = true) { + Ptr = ReadOwnedPtr(AutoRegister); } - + + template + void BatchReadOwnedPtrs(T1*& P1, T2*& P2, + bool A1=true, bool A2=true) { + + SerializedPtrID ID1 = ReadPtrID(); + SerializedPtrID ID2 = ReadPtrID(); + + P1 = (ID1) ? SerializeTrait::Create(*this) : NULL; + if (ID1 && A1) RegisterPtr(ID1,P1); + + P2 = (ID2) ? SerializeTrait::Create(*this) : NULL; + if (ID2 && A2) RegisterPtr(ID2,P2); + } + + template + void BatchReadOwnedPtrs(T1*& P1, T2*& P2, Arg1& arg1, + bool A1=true, bool A2=true) { + + SerializedPtrID ID1 = ReadPtrID(); + SerializedPtrID ID2 = ReadPtrID(); + + P1 = (ID1) ? SerializeTrait::Create(*this, arg1) : NULL; + if (ID1 && A1) RegisterPtr(ID1,P1); + + P2 = (ID2) ? SerializeTrait::Create(*this, arg1) : NULL; + if (ID2 && A2) RegisterPtr(ID2,P2); + } + + template + void BatchReadOwnedPtrs(T1*& P1, T2*& P2, T3*& P3, + bool A1=true, bool A2=true, bool A3=true) { + + SerializedPtrID ID1 = ReadPtrID(); + SerializedPtrID ID2 = ReadPtrID(); + SerializedPtrID ID3 = ReadPtrID(); + + P1 = (ID1) ? SerializeTrait::Create(*this) : NULL; + if (ID1 && A1) RegisterPtr(ID1,P1); + + P2 = (ID2) ? SerializeTrait::Create(*this) : NULL; + if (ID2 && A2) RegisterPtr(ID2,P2); + + P3 = (ID3) ? SerializeTrait::Create(*this) : NULL; + if (ID3 && A3) RegisterPtr(ID3,P3); + } + + template + void BatchReadOwnedPtrs(T1*& P1, T2*& P2, T3*& P3, Arg1& arg1, + bool A1=true, bool A2=true, bool A3=true) { + + SerializedPtrID ID1 = ReadPtrID(); + SerializedPtrID ID2 = ReadPtrID(); + SerializedPtrID ID3 = ReadPtrID(); + + P1 = (ID1) ? SerializeTrait::Create(*this, arg1) : NULL; + if (ID1 && A1) RegisterPtr(ID1,P1); + + P2 = (ID2) ? SerializeTrait::Create(*this, arg1) : NULL; + if (ID2 && A2) RegisterPtr(ID2,P2); + + P3 = (ID3) ? SerializeTrait::Create(*this, arg1) : NULL; + if (ID3 && A3) RegisterPtr(ID3,P3); + } + template - void ReadPtr(T*& PtrRef) { - ReadUIntPtr(reinterpret_cast(PtrRef)); + void BatchReadOwnedPtrs(unsigned NumPtrs, T** Ptrs, bool AutoRegister=true) { + llvm::SmallVector BatchIDVec; + + for (unsigned i = 0; i < NumPtrs; ++i) + BatchIDVec.push_back(ReadPtrID()); + + for (unsigned i = 0; i < NumPtrs; ++i) { + SerializedPtrID& PtrID = BatchIDVec[i]; + + T* p = PtrID ? SerializeTrait::Create(*this) : NULL; + + if (PtrID && AutoRegister) + RegisterPtr(PtrID,p); + + Ptrs[i] = p; + } } - + + template + void BatchReadOwnedPtrs(unsigned NumPtrs, T** Ptrs, Arg1& arg1, + bool AutoRegister=true) { + + llvm::SmallVector BatchIDVec; + + for (unsigned i = 0; i < NumPtrs; ++i) + BatchIDVec.push_back(ReadPtrID()); + + for (unsigned i = 0; i < NumPtrs; ++i) { + SerializedPtrID& PtrID = BatchIDVec[i]; + + T* p = PtrID ? SerializeTrait::Create(*this, arg1) : NULL; + + if (PtrID && AutoRegister) + RegisterPtr(PtrID,p); + + Ptrs[i] = p; + } + } + + template + void BatchReadOwnedPtrs(unsigned NumT1Ptrs, T1** Ptrs, T2*& P2, + bool A1=true, bool A2=true) { + + llvm::SmallVector BatchIDVec; + + for (unsigned i = 0; i < NumT1Ptrs; ++i) + BatchIDVec.push_back(ReadPtrID()); + + SerializedPtrID ID2 = ReadPtrID(); + + for (unsigned i = 0; i < NumT1Ptrs; ++i) { + SerializedPtrID& PtrID = BatchIDVec[i]; + + T1* p = PtrID ? SerializeTrait::Create(*this) : NULL; + + if (PtrID && A1) + RegisterPtr(PtrID,p); + + Ptrs[i] = p; + } + + P2 = (ID2) ? SerializeTrait::Create(*this) : NULL; + if (ID2 && A2) RegisterPtr(ID2,P2); + } + + template + void BatchReadOwnedPtrs(unsigned NumT1Ptrs, T1** Ptrs, T2*& P2, Arg1& arg1, + bool A1=true, bool A2=true) { + + llvm::SmallVector BatchIDVec; + + for (unsigned i = 0; i < NumT1Ptrs; ++i) + BatchIDVec.push_back(ReadPtrID()); + + SerializedPtrID ID2 = ReadPtrID(); + + for (unsigned i = 0; i < NumT1Ptrs; ++i) { + SerializedPtrID& PtrID = BatchIDVec[i]; + + T1* p = PtrID ? SerializeTrait::Create(*this, arg1) : NULL; + + if (PtrID && A1) + RegisterPtr(PtrID,p); + + Ptrs[i] = p; + } + + P2 = (ID2) ? SerializeTrait::Create(*this, arg1) : NULL; + if (ID2 && A2) RegisterPtr(ID2,P2); + } + + template + void BatchReadOwnedPtrs(unsigned NumT1Ptrs, T1** Ptrs, + T2*& P2, T3*& P3, + bool A1=true, bool A2=true, bool A3=true) { + + llvm::SmallVector BatchIDVec; + + for (unsigned i = 0; i < NumT1Ptrs; ++i) + BatchIDVec.push_back(ReadPtrID()); + + SerializedPtrID ID2 = ReadPtrID(); + SerializedPtrID ID3 = ReadPtrID(); + + for (unsigned i = 0; i < NumT1Ptrs; ++i) { + SerializedPtrID& PtrID = BatchIDVec[i]; + + T1* p = PtrID ? SerializeTrait::Create(*this) : NULL; + + if (PtrID && A1) + RegisterPtr(PtrID,p); + + Ptrs[i] = p; + } + + P2 = (ID2) ? SerializeTrait::Create(*this) : NULL; + if (ID2 && A2) RegisterPtr(ID2,P2); + + P3 = (ID3) ? SerializeTrait::Create(*this) : NULL; + if (ID3 && A3) RegisterPtr(ID3,P3); + } + + template + void BatchReadOwnedPtrs(unsigned NumT1Ptrs, T1** Ptrs, + T2*& P2, T3*& P3, Arg1& arg1, + bool A1=true, bool A2=true, bool A3=true) { + + llvm::SmallVector BatchIDVec; + + for (unsigned i = 0; i < NumT1Ptrs; ++i) + BatchIDVec.push_back(ReadPtrID()); + + SerializedPtrID ID2 = ReadPtrID(); + SerializedPtrID ID3 = ReadPtrID(); + + for (unsigned i = 0; i < NumT1Ptrs; ++i) { + SerializedPtrID& PtrID = BatchIDVec[i]; + + T1* p = PtrID ? SerializeTrait::Create(*this, arg1) : NULL; + + if (PtrID && A1) + RegisterPtr(PtrID,p); + + Ptrs[i] = p; + } + + P2 = (ID2) ? SerializeTrait::Create(*this, arg1) : NULL; + if (ID2 && A2) RegisterPtr(ID2,P2); + + P3 = (ID3) ? SerializeTrait::Create(*this, arg1) : NULL; + if (ID3 && A3) RegisterPtr(ID3,P3); + } + template - void ReadPtr(const T*& PtrRef) { - ReadPtr(const_cast(PtrRef)); - } + void ReadPtr(T*& PtrRef, bool AllowBackpatch = true) { + ReadUIntPtr(reinterpret_cast(PtrRef), AllowBackpatch); + } + + template + void ReadPtr(const T*& PtrRef, bool AllowBackpatch = true) { + ReadPtr(const_cast(PtrRef), AllowBackpatch); + } + + + template + void ReadPtr(T*& PtrRef, const SerializedPtrID& PtrID, + bool AllowBackpatch = true) { + ReadUIntPtr(reinterpret_cast(PtrRef), PtrID, AllowBackpatch); + } + + template + void ReadPtr(const T*& PtrRef, const SerializedPtrID& PtrID, + bool AllowBackpatch = true) { + + ReadPtr(const_cast(PtrRef), PtrID, AllowBackpatch); + } + + template + T* ReadPtr() { T* x = 0; ReadPtr(x,false); return x; } + + void ReadUIntPtr(uintptr_t& PtrRef, const SerializedPtrID& PtrID, + bool AllowBackpatch = true); + + void ReadUIntPtr(uintptr_t& PtrRef, bool AllowBackpatch = true) { + ReadUIntPtr(PtrRef,ReadPtrID(),AllowBackpatch); + } - void ReadUIntPtr(uintptr_t& PtrRef); - template T& ReadRef() { T* p = reinterpret_cast(ReadInternalRefPtr()); return *p; } - void RegisterPtr(unsigned PtrId, const void* Ptr); - + void RegisterPtr(const SerializedPtrID& PtrID, const void* Ptr); + void RegisterPtr(const void* Ptr) { - RegisterPtr(ReadInt(),Ptr); + RegisterPtr(ReadPtrID(),Ptr); } - + + template + void RegisterRef(const T& x) { + RegisterPtr(&x); + } + + template + void RegisterRef(const SerializedPtrID& PtrID, const T& x) { + RegisterPtr(PtrID,&x); + } + + Location getCurrentBlockLocation(); + unsigned getCurrentBlockID(); + unsigned getAbbrevNo(); + + bool FinishedBlock(Location BlockLoc); + bool JumpTo(const Location& BlockLoc); + void Rewind(); + bool AtEnd(); + bool inRecord(); + void SkipBlock(); + bool SkipToBlock(unsigned BlockID); + + unsigned getRecordCode(); + + BitstreamCursor &getStream() { return Stream; } private: - void ReadRecord(); - bool inRecord(); + bool AdvanceStream(); + 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(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(P)); } }; - + } // end namespace llvm #endif