2007-10-24 19:06:40 +00:00
|
|
|
//==- 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"
|
2007-10-25 00:10:21 +00:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2007-10-24 19:06:40 +00:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
class Serializer {
|
|
|
|
BitstreamWriter& Stream;
|
|
|
|
SmallVector<uint64_t,10> Record;
|
2007-11-05 21:36:35 +00:00
|
|
|
unsigned BlockLevel;
|
2007-10-25 00:10:21 +00:00
|
|
|
|
2007-10-31 18:23:21 +00:00
|
|
|
typedef DenseMap<const void*,unsigned> MapTy;
|
2007-10-25 00:10:21 +00:00
|
|
|
MapTy PtrMap;
|
|
|
|
|
2007-10-24 19:06:40 +00:00
|
|
|
public:
|
2007-11-06 22:22:25 +00:00
|
|
|
Serializer(BitstreamWriter& stream);
|
2007-10-24 19:06:40 +00:00
|
|
|
~Serializer();
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
inline void Emit(const T& X) { SerializeTrait<T>::Emit(*this,X); }
|
|
|
|
|
2007-11-07 18:24:34 +00:00
|
|
|
void EmitInt(uint64_t X);
|
|
|
|
void EmitSInt(int64_t X);
|
|
|
|
|
2007-10-24 19:06:40 +00:00
|
|
|
void EmitBool(bool X) { EmitInt(X); }
|
|
|
|
void EmitCStr(const char* beg, const char* end);
|
2007-10-25 00:10:21 +00:00
|
|
|
void EmitCStr(const char* cstr);
|
|
|
|
|
2007-10-31 18:23:21 +00:00
|
|
|
void EmitPtr(const void* ptr) { EmitInt(getPtrId(ptr)); }
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void EmitRef(const T& ref) { EmitPtr(&ref); }
|
2007-10-25 00:10:21 +00:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void EmitOwnedPtr(T* ptr) {
|
|
|
|
EmitPtr(ptr);
|
2007-10-25 18:42:52 +00:00
|
|
|
if (ptr) SerializeTrait<T>::Emit(*this,*ptr);
|
2007-10-25 00:10:21 +00:00
|
|
|
}
|
2007-11-07 22:30:29 +00:00
|
|
|
|
|
|
|
template <typename T1, typename T2>
|
|
|
|
void BatchEmitOwnedPtrs(T1* p1, T2* p2) {
|
|
|
|
EmitPtr(p1);
|
|
|
|
EmitPtr(p2);
|
|
|
|
if (p1) SerializeTrait<T1>::Emit(*this,*p1);
|
|
|
|
if (p2) SerializeTrait<T2>::Emit(*this,*p2);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T1, typename T2, typename T3>
|
|
|
|
void BatchEmitOwnedPtrs(T1* p1, T2* p2, T3* p3) {
|
|
|
|
EmitPtr(p1);
|
|
|
|
EmitPtr(p2);
|
|
|
|
EmitPtr(p3);
|
|
|
|
if (p1) SerializeTrait<T1>::Emit(*this,*p1);
|
|
|
|
if (p2) SerializeTrait<T2>::Emit(*this,*p2);
|
|
|
|
if (p3) SerializeTrait<T3>::Emit(*this,*p3);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T1, typename T2, typename T3, typename T4>
|
|
|
|
void BatchEmitOwnedPtrs(T1* p1, T2* p2, T3* p3, T4& p4) {
|
|
|
|
EmitPtr(p1);
|
|
|
|
EmitPtr(p2);
|
|
|
|
EmitPtr(p3);
|
|
|
|
EmitPtr(p4);
|
|
|
|
if (p1) SerializeTrait<T1>::Emit(*this,*p1);
|
|
|
|
if (p2) SerializeTrait<T2>::Emit(*this,*p2);
|
|
|
|
if (p3) SerializeTrait<T3>::Emit(*this,*p3);
|
|
|
|
if (p4) SerializeTrait<T4>::Emit(*this,*p4);
|
|
|
|
}
|
2007-10-24 19:06:40 +00:00
|
|
|
|
2007-11-07 23:18:40 +00:00
|
|
|
template <typename T>
|
2007-11-08 00:55:31 +00:00
|
|
|
void BatchEmitOwnedPtrs(unsigned NumPtrs, T* const * Ptrs) {
|
2007-11-07 23:18:40 +00:00
|
|
|
for (unsigned i = 0; i < NumPtrs; ++i)
|
|
|
|
EmitPtr(Ptrs[i]);
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < NumPtrs; ++i)
|
2007-11-07 23:32:06 +00:00
|
|
|
if (Ptrs[i]) SerializeTrait<T>::Emit(*this,*Ptrs[i]);
|
2007-11-07 23:18:40 +00:00
|
|
|
}
|
2007-11-13 00:13:57 +00:00
|
|
|
|
|
|
|
template <typename T1, typename T2, typename T3>
|
|
|
|
void BatchEmitOwnedPtrs(unsigned NumT1Ptrs, T1* const * Ptrs,
|
|
|
|
T2* p2, T3* p3) {
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < NumT1Ptrs; ++i)
|
|
|
|
EmitPtr(Ptrs[i]);
|
|
|
|
|
|
|
|
EmitPtr(p2);
|
|
|
|
EmitPtr(p3);
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < NumT1Ptrs; ++i)
|
|
|
|
if (Ptrs[i]) SerializeTrait<T1>::Emit(*this,*Ptrs[i]);
|
|
|
|
|
|
|
|
if (p2) SerializeTrait<T2>::Emit(*this,*p2);
|
|
|
|
if (p3) SerializeTrait<T3>::Emit(*this,*p3);
|
|
|
|
}
|
2007-11-08 19:50:46 +00:00
|
|
|
|
|
|
|
bool isRegistered(const void* p) const;
|
2007-11-07 23:18:40 +00:00
|
|
|
|
2007-11-08 19:50:46 +00:00
|
|
|
void FlushRecord() { if (inRecord()) EmitRecord(); }
|
2007-11-05 21:36:35 +00:00
|
|
|
void EnterBlock(unsigned BlockID = 8, unsigned CodeLen = 3);
|
|
|
|
void ExitBlock();
|
|
|
|
|
2007-10-24 19:06:40 +00:00
|
|
|
private:
|
|
|
|
void EmitRecord();
|
2007-10-25 00:10:21 +00:00
|
|
|
inline bool inRecord() { return Record.size() > 0; }
|
2007-11-08 19:50:46 +00:00
|
|
|
SerializedPtrID getPtrId(const void* ptr);
|
2007-10-24 19:06:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace llvm
|
|
|
|
#endif
|