1 //==- Serialize.cpp - Generic Object Serialization to Bitcode ----*- C++ -*-==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the internal methods used for object serialization.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Bitcode/Serialize.h"
17 #ifdef DEBUG_BACKPATCH
18 #include "llvm/Support/Streams.h"
23 Serializer::Serializer(BitstreamWriter
& stream
)
24 : Stream(stream
), BlockLevel(0) {}
26 Serializer::~Serializer() {
30 while (BlockLevel
> 0)
36 void Serializer::EmitRecord() {
37 assert(Record
.size() > 0 && "Cannot emit empty record.");
38 Stream
.EmitRecord(8,Record
);
42 void Serializer::EnterBlock(unsigned BlockID
,unsigned CodeLen
) {
44 Stream
.EnterSubblock(BlockID
,CodeLen
);
48 void Serializer::ExitBlock() {
49 assert (BlockLevel
> 0);
55 void Serializer::EmitInt(uint64_t X
) {
56 assert (BlockLevel
> 0);
60 void Serializer::EmitSInt(int64_t X
) {
64 EmitInt((-X
<< 1) | 1);
67 void Serializer::EmitCStr(const char* s
, const char* end
) {
68 Record
.push_back(end
- s
);
76 void Serializer::EmitCStr(const char* s
) {
77 EmitCStr(s
,s
+strlen(s
));
80 SerializedPtrID
Serializer::getPtrId(const void* ptr
) {
84 MapTy::iterator I
= PtrMap
.find(ptr
);
86 if (I
== PtrMap
.end()) {
87 unsigned id
= PtrMap
.size()+1;
88 #ifdef DEBUG_BACKPATCH
89 llvm::cerr
<< "Registered PTR: " << ptr
<< " => " << id
<< "\n";
94 else return I
->second
;
97 bool Serializer::isRegistered(const void* ptr
) const {
98 MapTy::const_iterator I
= PtrMap
.find(ptr
);
99 return I
!= PtrMap
.end();
103 #define INT_EMIT(TYPE)\
104 void SerializeTrait<TYPE>::Emit(Serializer&S, TYPE X) { S.EmitInt(X); }
107 INT_EMIT(unsigned char)
108 INT_EMIT(unsigned short)
109 INT_EMIT(unsigned int)
110 INT_EMIT(unsigned long)
112 #define SINT_EMIT(TYPE)\
113 void SerializeTrait<TYPE>::Emit(Serializer&S, TYPE X) { S.EmitSInt(X); }
115 SINT_EMIT(signed char)
116 SINT_EMIT(signed short)
117 SINT_EMIT(signed int)
118 SINT_EMIT(signed long)