1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
//==- 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"
#include "llvm/ADT/DenseMap.h"
namespace llvm {
class Serializer {
BitstreamWriter& Stream;
SmallVector<uint64_t,10> Record;
unsigned BlockLevel;
typedef DenseMap<const void*,unsigned> MapTy;
MapTy PtrMap;
public:
explicit Serializer(BitstreamWriter& stream);
~Serializer();
//==------------------------------------------------==//
// Template-based dispatch to emit arbitrary types.
//==------------------------------------------------==//
template <typename T>
inline void Emit(const T& X) { SerializeTrait<T>::Emit(*this,X); }
//==------------------------------------------------==//
// Methods to emit primitive types.
//==------------------------------------------------==//
void EmitInt(uint64_t X);
void EmitSInt(int64_t X);
inline void EmitBool(bool X) { EmitInt(X); }
void EmitCStr(const char* beg, const char* end);
void EmitCStr(const char* cstr);
void EmitPtr(const void* ptr) { EmitInt(getPtrId(ptr)); }
template <typename T>
inline void EmitRef(const T& ref) { EmitPtr(&ref); }
template <typename T>
inline void EmitOwnedPtr(T* ptr) {
EmitPtr(ptr);
if (ptr) SerializeTrait<T>::Emit(*this,*ptr);
}
//==------------------------------------------------==//
// Batch emission of pointers.
//==------------------------------------------------==//
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);
}
template <typename T>
void BatchEmitOwnedPtrs(unsigned NumPtrs, T* const * Ptrs) {
for (unsigned i = 0; i < NumPtrs; ++i)
EmitPtr(Ptrs[i]);
for (unsigned i = 0; i < NumPtrs; ++i)
if (Ptrs[i]) SerializeTrait<T>::Emit(*this,*Ptrs[i]);
}
template <typename T1, typename T2>
void BatchEmitOwnedPtrs(unsigned NumT1Ptrs, T1* const * Ptrs, T2* p2) {
for (unsigned i = 0; i < NumT1Ptrs; ++i)
EmitPtr(Ptrs[i]);
EmitPtr(p2);
for (unsigned i = 0; i < NumT1Ptrs; ++i)
if (Ptrs[i]) SerializeTrait<T1>::Emit(*this,*Ptrs[i]);
if (p2) SerializeTrait<T2>::Emit(*this,*p2);
}
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);
}
//==------------------------------------------------==//
// Emitter Functors
//==------------------------------------------------==//
template <typename T>
struct Emitter0 {
Serializer& S;
Emitter0(Serializer& s) : S(s) {}
void operator()(const T& x) const {
SerializeTrait<T>::Emit(S,x);
}
};
template <typename T, typename Arg1>
struct Emitter1 {
Serializer& S;
Arg1 A1;
Emitter1(Serializer& s, Arg1 a1) : S(s), A1(a1) {}
void operator()(const T& x) const {
SerializeTrait<T>::Emit(S,x,A1);
}
};
template <typename T, typename Arg1, typename Arg2>
struct Emitter2 {
Serializer& S;
Arg1 A1;
Arg2 A2;
Emitter2(Serializer& s, Arg1 a1, Arg2 a2) : S(s), A1(a1), A2(a2) {}
void operator()(const T& x) const {
SerializeTrait<T>::Emit(S,x,A1,A2);
}
};
template <typename T>
Emitter0<T> MakeEmitter() {
return Emitter0<T>(*this);
}
template <typename T, typename Arg1>
Emitter1<T,Arg1> MakeEmitter(Arg1 a1) {
return Emitter1<T,Arg1>(*this,a1);
}
template <typename T, typename Arg1, typename Arg2>
Emitter2<T,Arg1,Arg2> MakeEmitter(Arg1 a1, Arg2 a2) {
return Emitter2<T,Arg1,Arg2>(*this,a1,a2);
}
//==------------------------------------------------==//
// Misc. query and block/record manipulation methods.
//==------------------------------------------------==//
bool isRegistered(const void* p) const;
void FlushRecord() { if (inRecord()) EmitRecord(); }
void EnterBlock(unsigned BlockID = 8, unsigned CodeLen = 3);
void ExitBlock();
private:
void EmitRecord();
inline bool inRecord() { return Record.size() > 0; }
SerializedPtrID getPtrId(const void* ptr);
};
} // end namespace llvm
#endif
|