1 //===-- Writer.cpp - Library for writing LLVM bytecode files --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This library implements the functionality defined in llvm/Bytecode/Writer.h
12 // Note that this file uses an unusual technique of outputting all the bytecode
13 // to a deque of unsigned char, then copies the deque to an ostream. The
14 // reason for this is that we must do "seeking" in the stream to do back-
15 // patching, and some very important ostreams that we want to support (like
16 // pipes) do not support seeking. :( :( :(
18 // The choice of the deque data structure is influenced by the extremely fast
19 // "append" speed, plus the free "seek"/replace in the middle of the stream. I
20 // didn't use a vector because the stream could end up very large and copying
21 // the whole thing to reallocate would be kinda silly.
23 //===----------------------------------------------------------------------===//
25 #include "WriterInternals.h"
26 #include "llvm/Bytecode/WriteBytecodePass.h"
27 #include "llvm/Constants.h"
28 #include "llvm/DerivedTypes.h"
29 #include "llvm/Module.h"
30 #include "llvm/SymbolTable.h"
31 #include "Support/STLExtras.h"
32 #include "Support/Statistic.h"
37 static RegisterPass<WriteBytecodePass> X("emitbytecode", "Bytecode Writer");
40 BytesWritten("bytecodewriter", "Number of bytecode bytes written");
42 BytecodeWriter::BytecodeWriter(std::deque<unsigned char> &o, const Module *M)
45 // Emit the signature...
46 static const unsigned char *Sig = (const unsigned char*)"llvm";
47 output_data(Sig, Sig+4, Out);
49 // Emit the top level CLASS block.
50 BytecodeBlock ModuleBlock(BytecodeFormat::Module, Out);
52 bool isBigEndian = M->getEndianness() == Module::BigEndian;
53 bool hasLongPointers = M->getPointerSize() == Module::Pointer64;
54 bool hasNoEndianness = M->getEndianness() == Module::AnyEndianness;
55 bool hasNoPointerSize = M->getPointerSize() == Module::AnyPointerSize;
57 // Output the version identifier... we are currently on bytecode version #2,
58 // which corresponds to LLVM v1.3.
59 unsigned Version = (2 << 4) | isBigEndian | (hasLongPointers << 1) |
60 (hasNoEndianness << 2) | (hasNoPointerSize << 3);
61 output_vbr(Version, Out);
65 BytecodeBlock CPool(BytecodeFormat::GlobalTypePlane, Out);
67 // Write the type plane for types first because earlier planes (e.g. for a
68 // primitive type like float) may have constants constructed using types
69 // coming later (e.g., via getelementptr from a pointer type). The type
70 // plane is needed before types can be fwd or bkwd referenced.
71 const std::vector<const Value*> &Plane = Table.getPlane(Type::TypeTyID);
72 assert(!Plane.empty() && "No types at all?");
73 unsigned ValNo = Type::FirstDerivedTyID; // Start at the derived types...
74 outputConstantsInPlane(Plane, ValNo); // Write out the types
77 // The ModuleInfoBlock follows directly after the type information
78 outputModuleInfoBlock(M);
80 // Output module level constants, used for global variable initializers
81 outputConstants(false);
83 // Do the whole module now! Process each function at a time...
84 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
87 // If needed, output the symbol table for the module...
88 outputSymbolTable(M->getSymbolTable());
91 // Helper function for outputConstants().
92 // Writes out all the constants in the plane Plane starting at entry StartNo.
94 void BytecodeWriter::outputConstantsInPlane(const std::vector<const Value*>
95 &Plane, unsigned StartNo) {
96 unsigned ValNo = StartNo;
98 // Scan through and ignore function arguments, global values, and constant
100 for (; ValNo < Plane.size() &&
101 (isa<Argument>(Plane[ValNo]) || isa<GlobalValue>(Plane[ValNo]) ||
102 (isa<ConstantArray>(Plane[ValNo]) &&
103 cast<ConstantArray>(Plane[ValNo])->isString())); ValNo++)
106 unsigned NC = ValNo; // Number of constants
107 for (; NC < Plane.size() &&
108 (isa<Constant>(Plane[NC]) || isa<Type>(Plane[NC])); NC++)
110 NC -= ValNo; // Convert from index into count
111 if (NC == 0) return; // Skip empty type planes...
113 // FIXME: Most slabs only have 1 or 2 entries! We should encode this much
116 // Output type header: [num entries][type id number]
120 // Output the Type ID Number...
121 int Slot = Table.getSlot(Plane.front()->getType());
122 assert (Slot != -1 && "Type in constant pool but not in function!!");
123 output_vbr((unsigned)Slot, Out);
125 //cerr << "Emitting " << NC << " constants of type '"
126 // << Plane.front()->getType()->getName() << "' = Slot #" << Slot << "\n";
128 for (unsigned i = ValNo; i < ValNo+NC; ++i) {
129 const Value *V = Plane[i];
130 if (const Constant *CPV = dyn_cast<Constant>(V)) {
131 //cerr << "Serializing value: <" << V->getType() << ">: " << V << ":"
132 // << Out.size() << "\n";
135 outputType(cast<Type>(V));
140 static inline bool hasNullValue(unsigned TyID) {
141 return TyID != Type::LabelTyID && TyID != Type::TypeTyID &&
142 TyID != Type::VoidTyID;
145 void BytecodeWriter::outputConstants(bool isFunction) {
146 BytecodeBlock CPool(BytecodeFormat::ConstantPool, Out,
147 true /* Elide block if empty */);
149 unsigned NumPlanes = Table.getNumPlanes();
151 // Output the type plane before any constants!
152 if (isFunction && NumPlanes > Type::TypeTyID) {
153 const std::vector<const Value*> &Plane = Table.getPlane(Type::TypeTyID);
154 if (!Plane.empty()) { // Skip empty type planes...
155 unsigned ValNo = Table.getModuleLevel(Type::TypeTyID);
156 outputConstantsInPlane(Plane, ValNo);
160 // Output module-level string constants before any other constants.x
162 outputConstantStrings();
164 for (unsigned pno = 0; pno != NumPlanes; pno++)
165 if (pno != Type::TypeTyID) { // Type plane handled above.
166 const std::vector<const Value*> &Plane = Table.getPlane(pno);
167 if (!Plane.empty()) { // Skip empty type planes...
169 if (isFunction) // Don't re-emit module constants
170 ValNo += Table.getModuleLevel(pno);
172 if (hasNullValue(pno)) {
173 // Skip zero initializer
178 // Write out constants in the plane
179 outputConstantsInPlane(Plane, ValNo);
184 static unsigned getEncodedLinkage(const GlobalValue *GV) {
185 switch (GV->getLinkage()) {
186 default: assert(0 && "Invalid linkage!");
187 case GlobalValue::ExternalLinkage: return 0;
188 case GlobalValue::WeakLinkage: return 1;
189 case GlobalValue::AppendingLinkage: return 2;
190 case GlobalValue::InternalLinkage: return 3;
191 case GlobalValue::LinkOnceLinkage: return 4;
195 void BytecodeWriter::outputModuleInfoBlock(const Module *M) {
196 BytecodeBlock ModuleInfoBlock(BytecodeFormat::ModuleGlobalInfo, Out);
198 // Output the types for the global variables in the module...
199 for (Module::const_giterator I = M->gbegin(), End = M->gend(); I != End;++I) {
200 int Slot = Table.getSlot(I->getType());
201 assert(Slot != -1 && "Module global vars is broken!");
203 // Fields: bit0 = isConstant, bit1 = hasInitializer, bit2-4=Linkage,
204 // bit5+ = Slot # for type
205 unsigned oSlot = ((unsigned)Slot << 5) | (getEncodedLinkage(I) << 2) |
206 (I->hasInitializer() << 1) | I->isConstant();
207 output_vbr(oSlot, Out);
209 // If we have an initializer, output it now.
210 if (I->hasInitializer()) {
211 Slot = Table.getSlot((Value*)I->getInitializer());
212 assert(Slot != -1 && "No slot for global var initializer!");
213 output_vbr((unsigned)Slot, Out);
216 output_vbr((unsigned)Table.getSlot(Type::VoidTy), Out);
218 // Output the types of the functions in this module...
219 for (Module::const_iterator I = M->begin(), End = M->end(); I != End; ++I) {
220 int Slot = Table.getSlot(I->getType());
221 assert(Slot != -1 && "Module const pool is broken!");
222 assert(Slot >= Type::FirstDerivedTyID && "Derived type not in range!");
223 output_vbr((unsigned)Slot, Out);
225 output_vbr((unsigned)Table.getSlot(Type::VoidTy), Out);
228 void BytecodeWriter::outputInstructions(const Function *F) {
229 BytecodeBlock ILBlock(BytecodeFormat::InstructionList, Out);
230 for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
231 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I)
232 outputInstruction(*I);
235 void BytecodeWriter::outputFunction(const Function *F) {
236 BytecodeBlock FunctionBlock(BytecodeFormat::Function, Out);
237 output_vbr(getEncodedLinkage(F), Out);
239 // If this is an external function, there is nothing else to emit!
240 if (F->isExternal()) return;
242 // Get slot information about the function...
243 Table.incorporateFunction(F);
245 if (Table.getCompactionTable().empty()) {
246 // Output information about the constants in the function if the compaction
247 // table is not being used.
248 outputConstants(true);
250 // Otherwise, emit the compaction table.
251 outputCompactionTable();
254 // Output all of the instructions in the body of the function
255 outputInstructions(F);
257 // If needed, output the symbol table for the function...
258 outputSymbolTable(F->getSymbolTable());
260 Table.purgeFunction();
263 void BytecodeWriter::outputCompactionTablePlane(unsigned PlaneNo,
264 const std::vector<const Value*> &Plane,
266 unsigned End = Table.getModuleLevel(PlaneNo);
267 if (Plane.empty() || StartNo == End || End == 0) return; // Nothing to emit
268 assert(StartNo < End && "Cannot emit negative range!");
269 assert(StartNo < Plane.size() && End <= Plane.size());
271 // Do not emit the null initializer!
272 if (PlaneNo != Type::TypeTyID) ++StartNo;
274 // Figure out which encoding to use. By far the most common case we have is
275 // to emit 0-2 entries in a compaction table plane.
276 switch (End-StartNo) {
277 case 0: // Avoid emitting two vbr's if possible.
280 output_vbr((PlaneNo << 2) | End-StartNo, Out);
283 // Output the number of things.
284 output_vbr((unsigned(End-StartNo) << 2) | 3, Out);
285 output_vbr(PlaneNo, Out); // Emit the type plane this is
289 for (unsigned i = StartNo; i != End; ++i)
290 output_vbr(Table.getGlobalSlot(Plane[i]), Out);
293 void BytecodeWriter::outputCompactionTable() {
294 BytecodeBlock CTB(BytecodeFormat::CompactionTable, Out, true/*ElideIfEmpty*/);
295 const std::vector<std::vector<const Value*> > &CT =Table.getCompactionTable();
297 // First thing is first, emit the type compaction table if there is one.
298 if (CT.size() > Type::TypeTyID)
299 outputCompactionTablePlane(Type::TypeTyID, CT[Type::TypeTyID],
300 Type::FirstDerivedTyID);
302 for (unsigned i = 0, e = CT.size(); i != e; ++i)
303 if (i != Type::TypeTyID)
304 outputCompactionTablePlane(i, CT[i], 0);
307 void BytecodeWriter::outputSymbolTable(const SymbolTable &MST) {
308 // Do not output the Bytecode block for an empty symbol table, it just wastes
310 if ( MST.isEmpty() ) return;
312 BytecodeBlock SymTabBlock(BytecodeFormat::SymbolTable, Out,
313 true/* ElideIfEmpty*/);
315 //Symtab block header: [num entries][type id number]
316 output_vbr(MST.num_types(), Out);
317 output_vbr((unsigned)Table.getSlot(Type::TypeTy), Out);
318 for (SymbolTable::type_const_iterator TI = MST.type_begin(),
319 TE = MST.type_end(); TI != TE; ++TI ) {
320 //Symtab entry:[def slot #][name]
321 output_vbr((unsigned)Table.getSlot(TI->second), Out);
322 output(TI->first, Out, /*align=*/false);
325 // Now do each of the type planes in order.
326 for (SymbolTable::plane_const_iterator PI = MST.plane_begin(),
327 PE = MST.plane_end(); PI != PE; ++PI) {
328 SymbolTable::value_const_iterator I = MST.value_begin(PI->first);
329 SymbolTable::value_const_iterator End = MST.value_end(PI->first);
332 if (I == End) continue; // Don't mess with an absent type...
334 // Symtab block header: [num entries][type id number]
335 output_vbr(MST.type_size(PI->first), Out);
337 Slot = Table.getSlot(PI->first);
338 assert(Slot != -1 && "Type in symtab, but not in table!");
339 output_vbr((unsigned)Slot, Out);
341 for (; I != End; ++I) {
342 // Symtab entry: [def slot #][name]
343 const Value *V = I->second;
345 Slot = Table.getSlot(I->second);
346 assert(Slot != -1 && "Value in symtab but has no slot number!!");
347 output_vbr((unsigned)Slot, Out);
348 output(I->first, Out, false); // Don't force alignment...
353 void llvm::WriteBytecodeToFile(const Module *C, std::ostream &Out) {
354 assert(C && "You can't write a null module!!");
356 std::deque<unsigned char> Buffer;
358 // This object populates buffer for us...
359 BytecodeWriter BCW(Buffer, C);
361 // Keep track of how much we've written...
362 BytesWritten += Buffer.size();
364 // Okay, write the deque out to the ostream now... the deque is not
365 // sequential in memory, however, so write out as much as possible in big
366 // chunks, until we're done.
368 std::deque<unsigned char>::const_iterator I = Buffer.begin(),E = Buffer.end();
369 while (I != E) { // Loop until it's all written
370 // Scan to see how big this chunk is...
371 const unsigned char *ChunkPtr = &*I;
372 const unsigned char *LastPtr = ChunkPtr;
374 const unsigned char *ThisPtr = &*++I;
375 if (LastPtr+1 != ThisPtr) { // Advanced by more than a byte of memory?
382 // Write out the chunk...
383 Out.write((char*)ChunkPtr, LastPtr-ChunkPtr);