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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
|
//===-- Writer.cpp - Library for writing LLVM bytecode files --------------===//
//
// The LLVM Compiler Infrastructure
//
// This file was developed by the LLVM research group and is distributed under
// the University of Illinois Open Source License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This library implements the functionality defined in llvm/Bytecode/Writer.h
//
// Note that this file uses an unusual technique of outputting all the bytecode
// to a deque of unsigned char, then copies the deque to an ostream. The
// reason for this is that we must do "seeking" in the stream to do back-
// patching, and some very important ostreams that we want to support (like
// pipes) do not support seeking. :( :( :(
//
// The choice of the deque data structure is influenced by the extremely fast
// "append" speed, plus the free "seek"/replace in the middle of the stream. I
// didn't use a vector because the stream could end up very large and copying
// the whole thing to reallocate would be kinda silly.
//
//===----------------------------------------------------------------------===//
#include "WriterInternals.h"
#include "llvm/Bytecode/WriteBytecodePass.h"
#include "llvm/Constants.h"
#include "llvm/DerivedTypes.h"
#include "llvm/Module.h"
#include "llvm/SymbolTable.h"
#include "Support/STLExtras.h"
#include "Support/Statistic.h"
#include "Support/Debug.h"
#include <cstring>
#include <algorithm>
using namespace llvm;
static RegisterPass<WriteBytecodePass> X("emitbytecode", "Bytecode Writer");
static Statistic<>
BytesWritten("bytecodewriter", "Number of bytecode bytes written");
static Statistic<>
ConstantTotalBytes("bytecodewriter", "Bytes of constants total");
static Statistic<>
FunctionConstantTotalBytes("bytecodewriter", "Bytes of function constants total");
static Statistic<>
ConstantPlaneHeaderBytes("bytecodewriter", "Constant plane header bytes");
static Statistic<>
InstructionBytes("bytecodewriter", "Bytes of bytes of instructions");
static Statistic<>
SymTabBytes("bytecodewriter", "Bytes of symbol table");
static Statistic<>
ModuleInfoBytes("bytecodewriter", "Bytes of module info");
BytecodeWriter::BytecodeWriter(std::deque<unsigned char> &o, const Module *M)
: Out(o), Table(M, true) {
// Emit the signature...
static const unsigned char *Sig = (const unsigned char*)"llvm";
output_data(Sig, Sig+4, Out);
// Emit the top level CLASS block.
BytecodeBlock ModuleBlock(BytecodeFormat::Module, Out);
bool isBigEndian = M->getEndianness() == Module::BigEndian;
bool hasLongPointers = M->getPointerSize() == Module::Pointer64;
bool hasNoEndianness = M->getEndianness() == Module::AnyEndianness;
bool hasNoPointerSize = M->getPointerSize() == Module::AnyPointerSize;
// Output the version identifier... we are currently on bytecode version #1,
// which corresponds to LLVM v1.2.
unsigned Version = (1 << 4) | isBigEndian | (hasLongPointers << 1) |
(hasNoEndianness << 2) | (hasNoPointerSize << 3);
output_vbr(Version, Out);
align32(Out);
{
BytecodeBlock CPool(BytecodeFormat::GlobalTypePlane, Out);
// Write the type plane for types first because earlier planes (e.g. for a
// primitive type like float) may have constants constructed using types
// coming later (e.g., via getelementptr from a pointer type). The type
// plane is needed before types can be fwd or bkwd referenced.
const std::vector<const Value*> &Plane = Table.getPlane(Type::TypeTyID);
assert(!Plane.empty() && "No types at all?");
unsigned ValNo = Type::FirstDerivedTyID; // Start at the derived types...
outputConstantsInPlane(Plane, ValNo); // Write out the types
}
DEBUG(for (unsigned i = 0; i != Type::TypeTyID; ++i)
if (Table.getPlane(i).size())
std::cerr << " ModuleLevel["
<< *Type::getPrimitiveType((Type::PrimitiveID)i)
<< "] = " << Table.getPlane(i).size() << "\n");
// The ModuleInfoBlock follows directly after the type information
outputModuleInfoBlock(M);
// Output module level constants, used for global variable initializers
outputConstants(false);
// Do the whole module now! Process each function at a time...
for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
outputFunction(I);
// If needed, output the symbol table for the module...
outputSymbolTable(M->getSymbolTable());
}
// Helper function for outputConstants().
// Writes out all the constants in the plane Plane starting at entry StartNo.
//
void BytecodeWriter::outputConstantsInPlane(const std::vector<const Value*>
&Plane, unsigned StartNo) {
unsigned ValNo = StartNo;
// Scan through and ignore function arguments, global values, and constant
// strings.
for (; ValNo < Plane.size() &&
(isa<Argument>(Plane[ValNo]) || isa<GlobalValue>(Plane[ValNo]) ||
(isa<ConstantArray>(Plane[ValNo]) &&
cast<ConstantArray>(Plane[ValNo])->isString())); ValNo++)
/*empty*/;
unsigned NC = ValNo; // Number of constants
for (; NC < Plane.size() &&
(isa<Constant>(Plane[NC]) || isa<Type>(Plane[NC])); NC++)
/*empty*/;
NC -= ValNo; // Convert from index into count
if (NC == 0) return; // Skip empty type planes...
// FIXME: Most slabs only have 1 or 2 entries! We should encode this much
// more compactly.
ConstantPlaneHeaderBytes -= Out.size();
// Output type header: [num entries][type id number]
//
output_vbr(NC, Out);
// Output the Type ID Number...
int Slot = Table.getSlot(Plane.front()->getType());
assert (Slot != -1 && "Type in constant pool but not in function!!");
output_vbr((unsigned)Slot, Out);
ConstantPlaneHeaderBytes += Out.size();
//cerr << "Emitting " << NC << " constants of type '"
// << Plane.front()->getType()->getName() << "' = Slot #" << Slot << "\n";
for (unsigned i = ValNo; i < ValNo+NC; ++i) {
const Value *V = Plane[i];
if (const Constant *CPV = dyn_cast<Constant>(V)) {
//cerr << "Serializing value: <" << V->getType() << ">: " << V << ":"
// << Out.size() << "\n";
outputConstant(CPV);
} else {
outputType(cast<Type>(V));
}
}
}
static inline bool hasNullValue(unsigned TyID) {
return TyID != Type::LabelTyID && TyID != Type::TypeTyID &&
TyID != Type::VoidTyID;
}
void BytecodeWriter::outputConstants(bool isFunction) {
ConstantTotalBytes -= Out.size();
if (isFunction) FunctionConstantTotalBytes -= Out.size();
BytecodeBlock CPool(BytecodeFormat::ConstantPool, Out,
true /* Elide block if empty */);
unsigned NumPlanes = Table.getNumPlanes();
// Output the type plane before any constants!
if (isFunction && NumPlanes > Type::TypeTyID) {
const std::vector<const Value*> &Plane = Table.getPlane(Type::TypeTyID);
if (!Plane.empty()) { // Skip empty type planes...
unsigned ValNo = Table.getModuleLevel(Type::TypeTyID);
outputConstantsInPlane(Plane, ValNo);
}
}
// Output module-level string constants before any other constants.x
if (!isFunction)
outputConstantStrings();
for (unsigned pno = 0; pno != NumPlanes; pno++)
if (pno != Type::TypeTyID) { // Type plane handled above.
const std::vector<const Value*> &Plane = Table.getPlane(pno);
if (!Plane.empty()) { // Skip empty type planes...
unsigned ValNo = 0;
if (isFunction) // Don't re-emit module constants
ValNo += Table.getModuleLevel(pno);
if (hasNullValue(pno)) {
// Skip zero initializer
if (ValNo == 0)
ValNo = 1;
}
// Write out constants in the plane
outputConstantsInPlane(Plane, ValNo);
}
}
ConstantTotalBytes += Out.size();
if (isFunction) FunctionConstantTotalBytes += Out.size();
}
static unsigned getEncodedLinkage(const GlobalValue *GV) {
switch (GV->getLinkage()) {
default: assert(0 && "Invalid linkage!");
case GlobalValue::ExternalLinkage: return 0;
case GlobalValue::WeakLinkage: return 1;
case GlobalValue::AppendingLinkage: return 2;
case GlobalValue::InternalLinkage: return 3;
case GlobalValue::LinkOnceLinkage: return
|