diff options
author | Ted Kremenek <kremenek@apple.com> | 2007-10-24 19:06:40 +0000 |
---|---|---|
committer | Ted Kremenek <kremenek@apple.com> | 2007-10-24 19:06:40 +0000 |
commit | 03a89f80939e85bb8bcc8f0b8b86cfdffdd9585f (patch) | |
tree | 1ce826794cbe38fce1c7bb292dc5bae94731ff18 /include | |
parent | a6c7dce87a6bfd05f132432e6673e256a8ce8469 (diff) | |
download | external_llvm-03a89f80939e85bb8bcc8f0b8b86cfdffdd9585f.zip external_llvm-03a89f80939e85bb8bcc8f0b8b86cfdffdd9585f.tar.gz external_llvm-03a89f80939e85bb8bcc8f0b8b86cfdffdd9585f.tar.bz2 |
Split Serialization.h into separate headers: Serialize.h and
Deserialize.h Serialization.h now includes trait speciailizations for
unsigned long, etc.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@43307 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'include')
-rw-r--r-- | include/llvm/Bitcode/Deserialize.h | 74 | ||||
-rw-r--r-- | include/llvm/Bitcode/Serialization.h | 118 | ||||
-rw-r--r-- | include/llvm/Bitcode/Serialize.h | 48 |
3 files changed, 137 insertions, 103 deletions
diff --git a/include/llvm/Bitcode/Deserialize.h b/include/llvm/Bitcode/Deserialize.h new file mode 100644 index 0000000..6ee09aa --- /dev/null +++ b/include/llvm/Bitcode/Deserialize.h @@ -0,0 +1,74 @@ +//=- 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 <vector> + +namespace llvm { + +class Deserializer { + BitstreamReader& Stream; + SmallVector<uint64_t,10> Record; + unsigned RecIdx; +public: + Deserializer(BitstreamReader& stream); + ~Deserializer(); + + 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); + } + + uint64_t ReadInt(); + bool ReadBool() { return ReadInt() ? true : false; } + + // FIXME: Substitute a better implementation which calculates the minimum + // number of bits needed to serialize the enum. + template <typename EnumT> + EnumT ReadEnum(unsigned MinVal, unsigned MaxVal) { + return static_cast<EnumT>(ReadInt(32)); + } + + char* ReadCStr(char* cstr = NULL, unsigned MaxLen=0, bool isNullTerm=true); + void ReadCStr(std::vector<char>& buff, bool isNullTerm=false); + +private: + void ReadRecord(); + + inline bool inRecord() { + if (Record.size() > 0) { + if (RecIdx >= Record.size()) { + RecIdx = 0; + Record.clear(); + return false; + } + else return true; + } + else return false; + } +}; + +} // end namespace llvm + +#endif diff --git a/include/llvm/Bitcode/Serialization.h b/include/llvm/Bitcode/Serialization.h index eeaac4a..935452c 100644 --- a/include/llvm/Bitcode/Serialization.h +++ b/include/llvm/Bitcode/Serialization.h @@ -1,4 +1,4 @@ - //=- Serialization.h - Generic Object Serialization to Bitcode ---*- C++ -*-=// +//==- Serialization.h - Generic Object Serialization to Bitcode ---*- C++ -*-=// // // The LLVM Compiler Infrastructure // @@ -15,114 +15,26 @@ #ifndef LLVM_BITCODE_SERIALIZE #define LLVM_BITCODE_SERIALIZE -#include "llvm/Bitcode/BitstreamWriter.h" -#include "llvm/Bitcode/BitstreamReader.h" -#include "llvm/ADT/SmallVector.h" -#include <vector> - namespace llvm { +class Serializer; +class Deserializer; template <typename T> struct SerializeTrait; -class Serializer { - BitstreamWriter& Stream; - SmallVector<uint64_t,10> Record; - bool inBlock; -public: - Serializer(BitstreamWriter& stream, unsigned BlockID = 0); - ~Serializer(); - - template <typename T> - inline void Emit(const T& X) { SerializeTrait<T>::Serialize(*this,X); } - - void EmitInt(unsigned X, unsigned bits); - - // FIXME: Substitute a better implementation which calculates the minimum - // number of bits needed to serialize the enum. - void EmitEnum(unsigned X, unsigned MinVal, unsigned MaxVal) { EmitInt(X,32); } - - void EmitCString(const char* cstr); - - void Flush() { if (inRecord()) EmitRecord(); } - -private: - void EmitRecord(); - inline bool inRecord() { return Record.size() > 0; } -}; - - -class Deserializer { - BitstreamReader& Stream; - SmallVector<uint64_t,10> Record; - unsigned RecIdx; -public: - Deserializer(BitstreamReader& stream); - ~Deserializer(); - - template <typename T> - inline T& Read(T& X) { SerializeTrait<T>::Deserialize(*this,X); return X; } - - template <typename T> - inline T* Materialize() { - T* X = SerializeTrait<T>::Instantiate(); - Read(*X); - return X; - } - - uint64_t ReadInt(unsigned bits = 32); - bool ReadBool() { return ReadInt(1) ? true : false; } - - // FIXME: Substitute a better implementation which calculates the minimum - // number of bits needed to serialize the enum. - template <typename EnumT> - EnumT ReadEnum(unsigned MinVal, unsigned MaxVal) { - return static_cast<EnumT>(ReadInt(32)); - } - - char* ReadCString(char* cstr = NULL, unsigned MaxLen=0, bool isNullTerm=true); - void ReadCString(std::vector<char>& buff, bool isNullTerm=false); - -private: - void ReadRecord(); +#define SERIALIZE_INT_TRAIT(TYPE)\ +template <> struct SerializeTrait<TYPE> {\ + static void Emit(Serializer& S, TYPE X);\ + static void Read(Deserializer& S, TYPE& X); }; - inline bool inRecord() { - if (Record.size() > 0) { - if (RecIdx >= Record.size()) { - RecIdx = 0; - Record.clear(); - return false; - } - else return true; - } - else return false; - } -}; - - -template <typename uintty, unsigned Bits> -struct SerializeIntTrait { - static inline void Serialize(Serializer& S, uintty X) { - S.EmitInt(X,Bits); - } - - static inline void Deserialize(Deserializer& S, uintty& X) { - X = (uintty) S.ReadInt(Bits); - } -}; - -template <> struct SerializeTrait<bool> - : public SerializeIntTrait<bool,1> {}; +SERIALIZE_INT_TRAIT(bool) +SERIALIZE_INT_TRAIT(unsigned char) +SERIALIZE_INT_TRAIT(unsigned short) +SERIALIZE_INT_TRAIT(unsigned int) +SERIALIZE_INT_TRAIT(unsigned long) +SERIALIZE_INT_TRAIT(unsigned long long) -template <> struct SerializeTrait<char> - : public SerializeIntTrait<char,8> {}; - -template <> struct SerializeTrait<short> - : public SerializeIntTrait<short,16> {}; - -template <> struct SerializeTrait<unsigned> - : public SerializeIntTrait<unsigned,32> {}; - - +#undef SERIALIZE_INT_TRAIT } // end namespace llvm + #endif diff --git a/include/llvm/Bitcode/Serialize.h b/include/llvm/Bitcode/Serialize.h new file mode 100644 index 0000000..061ccaf --- /dev/null +++ b/include/llvm/Bitcode/Serialize.h @@ -0,0 +1,48 @@ +//==- Serialize.h - Generic Object Serialization to 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 serialization to +// LLVM bitcode. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_BITCODE_SERIALIZE_OUTPUT +#define LLVM_BITCODE_SERIALIZE_OUTPUT + +#include "llvm/Bitcode/Serialization.h" +#include "llvm/Bitcode/BitstreamWriter.h" +#include "llvm/ADT/SmallVector.h" + +namespace llvm { + +class Serializer { + BitstreamWriter& Stream; + SmallVector<uint64_t,10> Record; + bool inBlock; +public: + Serializer(BitstreamWriter& stream, unsigned BlockID = 0); + ~Serializer(); + + template <typename T> + inline void Emit(const T& X) { SerializeTrait<T>::Emit(*this,X); } + + void EmitInt(unsigned X); + void EmitBool(bool X) { EmitInt(X); } + void EmitCStr(const char* beg, const char* end); + void EmitCStr(const char* cstr); + + void Flush() { if (inRecord()) EmitRecord(); } + +private: + void EmitRecord(); + inline bool inRecord() { return Record.size() > 0; } +}; + +} // end namespace llvm +#endif |