blob: 4eade8827583b2e10cfcac152d74d10db808d014 [file] [log] [blame]
Chris Lattner00950542001-06-06 20:29:01 +00001//===-- Writer.cpp - Library for writing VM bytecode files -------*- C++ -*--=//
2//
3// This library implements the functionality defined in llvm/Bytecode/Writer.h
4//
Chris Lattner00950542001-06-06 20:29:01 +00005// Note that this file uses an unusual technique of outputting all the bytecode
Chris Lattnere8fdde12001-09-07 16:39:41 +00006// to a deque of unsigned char's, then copies the deque to an ostream. The
Chris Lattner00950542001-06-06 20:29:01 +00007// reason for this is that we must do "seeking" in the stream to do back-
8// patching, and some very important ostreams that we want to support (like
9// pipes) do not support seeking. :( :( :(
10//
Chris Lattnere8fdde12001-09-07 16:39:41 +000011// The choice of the deque data structure is influenced by the extremely fast
12// "append" speed, plus the free "seek"/replace in the middle of the stream. I
13// didn't use a vector because the stream could end up very large and copying
14// the whole thing to reallocate would be kinda silly.
Chris Lattner00950542001-06-06 20:29:01 +000015//
16// Note that the performance of this library is not terribly important, because
17// it shouldn't be used by JIT type applications... so it is not a huge focus
18// at least. :)
19//
20//===----------------------------------------------------------------------===//
21
22#include "WriterInternals.h"
Chris Lattner635cd932002-07-23 19:56:44 +000023#include "llvm/Bytecode/WriteBytecodePass.h"
Chris Lattner00950542001-06-06 20:29:01 +000024#include "llvm/Module.h"
Chris Lattner00950542001-06-06 20:29:01 +000025#include "llvm/SymbolTable.h"
26#include "llvm/DerivedTypes.h"
Chris Lattnerf60dc882001-11-29 16:32:16 +000027#include "Support/STLExtras.h"
Chris Lattnerce6ef112002-07-26 18:40:14 +000028#include "Support/StatisticReporter.h"
Chris Lattner00950542001-06-06 20:29:01 +000029#include <string.h>
30#include <algorithm>
31
Chris Lattner635cd932002-07-23 19:56:44 +000032static RegisterPass<WriteBytecodePass> X("emitbytecode", "Bytecode Writer");
33
Chris Lattnerce6ef112002-07-26 18:40:14 +000034static Statistic<>
35BytesWritten("bytecodewriter\t- Number of bytecode bytes written");
Chris Lattner635cd932002-07-23 19:56:44 +000036
37
Chris Lattner697954c2002-01-20 22:54:45 +000038BytecodeWriter::BytecodeWriter(std::deque<unsigned char> &o, const Module *M)
Chris Lattner00950542001-06-06 20:29:01 +000039 : Out(o), Table(M, false) {
40
41 outputSignature();
42
43 // Emit the top level CLASS block.
44 BytecodeBlock ModuleBlock(BytecodeFormat::Module, Out);
45
Chris Lattnere8fdde12001-09-07 16:39:41 +000046 // Output the ID of first "derived" type:
Chris Lattner00950542001-06-06 20:29:01 +000047 output_vbr((unsigned)Type::FirstDerivedTyID, Out);
48 align32(Out);
49
Chris Lattnerb5794002002-04-07 22:49:37 +000050 // Output module level constants, including types used by the function protos
Chris Lattnere8fdde12001-09-07 16:39:41 +000051 outputConstants(false);
Chris Lattner00950542001-06-06 20:29:01 +000052
Chris Lattnere8fdde12001-09-07 16:39:41 +000053 // The ModuleInfoBlock follows directly after the Module constant pool
54 outputModuleInfoBlock(M);
55
Chris Lattnerb5794002002-04-07 22:49:37 +000056 // Do the whole module now! Process each function at a time...
Chris Lattner0b12b5f2002-06-25 16:13:21 +000057 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
58 processMethod(I);
Chris Lattnere8fdde12001-09-07 16:39:41 +000059
60 // If needed, output the symbol table for the module...
Chris Lattner00950542001-06-06 20:29:01 +000061 if (M->hasSymbolTable())
62 outputSymbolTable(*M->getSymbolTable());
63}
64
Vikram S. Advea7dac3d2002-07-14 23:07:51 +000065// Helper function for outputConstants().
66// Writes out all the constants in the plane Plane starting at entry StartNo.
67//
68void BytecodeWriter::outputConstantsInPlane(const std::vector<const Value*>
69 &Plane, unsigned StartNo) {
70 unsigned ValNo = StartNo;
71
72 // Scan through and ignore function arguments...
73 for (; ValNo < Plane.size() && isa<Argument>(Plane[ValNo]); ValNo++)
74 /*empty*/;
75
76 unsigned NC = ValNo; // Number of constants
77 for (; NC < Plane.size() &&
78 (isa<Constant>(Plane[NC]) || isa<Type>(Plane[NC])); NC++)
79 /*empty*/;
80 NC -= ValNo; // Convert from index into count
81 if (NC == 0) return; // Skip empty type planes...
82
83 // Output type header: [num entries][type id number]
84 //
85 output_vbr(NC, Out);
86
87 // Output the Type ID Number...
88 int Slot = Table.getValSlot(Plane.front()->getType());
89 assert (Slot != -1 && "Type in constant pool but not in function!!");
90 output_vbr((unsigned)Slot, Out);
91
92 //cerr << "Emitting " << NC << " constants of type '"
93 // << Plane.front()->getType()->getName() << "' = Slot #" << Slot << "\n";
94
95 for (unsigned i = ValNo; i < ValNo+NC; ++i) {
96 const Value *V = Plane[i];
97 if (const Constant *CPV = dyn_cast<Constant>(V)) {
98 //cerr << "Serializing value: <" << V->getType() << ">: " << V << ":"
99 // << Out.size() << "\n";
100 outputConstant(CPV);
101 } else {
102 outputType(cast<const Type>(V));
103 }
104 }
105}
106
Chris Lattner79df7c02002-03-26 18:01:55 +0000107void BytecodeWriter::outputConstants(bool isFunction) {
Chris Lattnere8fdde12001-09-07 16:39:41 +0000108 BytecodeBlock CPool(BytecodeFormat::ConstantPool, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000109
110 unsigned NumPlanes = Table.getNumPlanes();
Vikram S. Advea7dac3d2002-07-14 23:07:51 +0000111
112 // Write the type plane for types first because earlier planes
113 // (e.g. for a primitive type like float) may have constants constructed
114 // using types coming later (e.g., via getelementptr from a pointer type).
115 // The type plane is needed before types can be fwd or bkwd referenced.
116 if (!isFunction) {
117 const std::vector<const Value*> &Plane = Table.getPlane(Type::TypeTyID);
118 assert(!Plane.empty() && "No types at all?");
119 unsigned ValNo = Type::FirstDerivedTyID; // Start at the derived types...
120 outputConstantsInPlane(Plane, ValNo); // Write out the types
121 }
122
Chris Lattner00950542001-06-06 20:29:01 +0000123 for (unsigned pno = 0; pno < NumPlanes; pno++) {
Chris Lattner697954c2002-01-20 22:54:45 +0000124 const std::vector<const Value*> &Plane = Table.getPlane(pno);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000125 if (Plane.empty()) continue; // Skip empty type planes...
Vikram S. Advea7dac3d2002-07-14 23:07:51 +0000126
Chris Lattnere8fdde12001-09-07 16:39:41 +0000127 unsigned ValNo = 0;
Chris Lattner79df7c02002-03-26 18:01:55 +0000128 if (isFunction) // Don't reemit module constants
Chris Lattnere8fdde12001-09-07 16:39:41 +0000129 ValNo = Table.getModuleLevel(pno);
130 else if (pno == Type::TypeTyID)
Vikram S. Advea7dac3d2002-07-14 23:07:51 +0000131 continue; // Type plane was written out above
Chris Lattner00950542001-06-06 20:29:01 +0000132
Vikram S. Advea7dac3d2002-07-14 23:07:51 +0000133 outputConstantsInPlane(Plane, ValNo); // Write out constants in the plane
Chris Lattner00950542001-06-06 20:29:01 +0000134 }
Chris Lattner00950542001-06-06 20:29:01 +0000135}
136
137void BytecodeWriter::outputModuleInfoBlock(const Module *M) {
138 BytecodeBlock ModuleInfoBlock(BytecodeFormat::ModuleGlobalInfo, Out);
139
Chris Lattner70cc3392001-09-10 07:58:01 +0000140 // Output the types for the global variables in the module...
141 for (Module::const_giterator I = M->gbegin(), End = M->gend(); I != End;++I) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000142 int Slot = Table.getValSlot(I->getType());
Chris Lattner70cc3392001-09-10 07:58:01 +0000143 assert(Slot != -1 && "Module global vars is broken!");
Chris Lattnerd70684f2001-09-18 04:01:05 +0000144
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000145 // Fields: bit0 = isConstant, bit1 = hasInitializer, bit2=InternalLinkage,
146 // bit3+ = slot#
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000147 unsigned oSlot = ((unsigned)Slot << 3) | (I->hasInternalLinkage() << 2) |
148 (I->hasInitializer() << 1) | I->isConstant();
Chris Lattnerd70684f2001-09-18 04:01:05 +0000149 output_vbr(oSlot, Out);
150
Chris Lattner1b98c5c2001-10-13 06:48:38 +0000151 // If we have an initializer, output it now.
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000152 if (I->hasInitializer()) {
153 Slot = Table.getValSlot((Value*)I->getInitializer());
Chris Lattnerd70684f2001-09-18 04:01:05 +0000154 assert(Slot != -1 && "No slot for global var initializer!");
155 output_vbr((unsigned)Slot, Out);
156 }
Chris Lattner70cc3392001-09-10 07:58:01 +0000157 }
158 output_vbr((unsigned)Table.getValSlot(Type::VoidTy), Out);
159
Chris Lattnerb5794002002-04-07 22:49:37 +0000160 // Output the types of the functions in this module...
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000161 for (Module::const_iterator I = M->begin(), End = M->end(); I != End; ++I) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000162 int Slot = Table.getValSlot(I->getType());
Chris Lattner00950542001-06-06 20:29:01 +0000163 assert(Slot != -1 && "Module const pool is broken!");
164 assert(Slot >= Type::FirstDerivedTyID && "Derived type not in range!");
165 output_vbr((unsigned)Slot, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000166 }
167 output_vbr((unsigned)Table.getValSlot(Type::VoidTy), Out);
Chris Lattner70cc3392001-09-10 07:58:01 +0000168
169
Chris Lattner00950542001-06-06 20:29:01 +0000170 align32(Out);
171}
172
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000173void BytecodeWriter::processMethod(const Function *F) {
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000174 BytecodeBlock FunctionBlock(BytecodeFormat::Function, Out);
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000175 output_vbr((unsigned)F->hasInternalLinkage(), Out);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000176 // Only output the constant pool and other goodies if needed...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000177 if (!F->isExternal()) {
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000178
Chris Lattnerb5794002002-04-07 22:49:37 +0000179 // Get slot information about the function...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000180 Table.incorporateFunction(F);
Chris Lattner00950542001-06-06 20:29:01 +0000181
Chris Lattnerb5794002002-04-07 22:49:37 +0000182 // Output information about the constants in the function...
Chris Lattnere8fdde12001-09-07 16:39:41 +0000183 outputConstants(true);
Chris Lattner00950542001-06-06 20:29:01 +0000184
Chris Lattnere8fdde12001-09-07 16:39:41 +0000185 // Output basic block nodes...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000186 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
187 processBasicBlock(*I);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000188
Chris Lattnerb5794002002-04-07 22:49:37 +0000189 // If needed, output the symbol table for the function...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000190 if (F->hasSymbolTable())
191 outputSymbolTable(*F->getSymbolTable());
Chris Lattnere8fdde12001-09-07 16:39:41 +0000192
Chris Lattnerb5794002002-04-07 22:49:37 +0000193 Table.purgeFunction();
Chris Lattnere8fdde12001-09-07 16:39:41 +0000194 }
Chris Lattner00950542001-06-06 20:29:01 +0000195}
196
197
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000198void BytecodeWriter::processBasicBlock(const BasicBlock &BB) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000199 BytecodeBlock FunctionBlock(BytecodeFormat::BasicBlock, Out);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000200 // Process all the instructions in the bb...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000201 for(BasicBlock::const_iterator I = BB.begin(), E = BB.end(); I != E; ++I)
202 processInstruction(*I);
Chris Lattner00950542001-06-06 20:29:01 +0000203}
204
205void BytecodeWriter::outputSymbolTable(const SymbolTable &MST) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000206 BytecodeBlock FunctionBlock(BytecodeFormat::SymbolTable, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000207
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000208 for (SymbolTable::const_iterator TI = MST.begin(); TI != MST.end(); ++TI) {
Chris Lattner00950542001-06-06 20:29:01 +0000209 SymbolTable::type_const_iterator I = MST.type_begin(TI->first);
210 SymbolTable::type_const_iterator End = MST.type_end(TI->first);
211 int Slot;
212
213 if (I == End) continue; // Don't mess with an absent type...
214
215 // Symtab block header: [num entries][type id number]
216 output_vbr(MST.type_size(TI->first), Out);
217
218 Slot = Table.getValSlot(TI->first);
219 assert(Slot != -1 && "Type in symtab, but not in table!");
220 output_vbr((unsigned)Slot, Out);
221
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000222 for (; I != End; ++I) {
Chris Lattner00950542001-06-06 20:29:01 +0000223 // Symtab entry: [def slot #][name]
224 Slot = Table.getValSlot(I->second);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000225 assert(Slot != -1 && "Value in symtab but has no slot number!!");
Chris Lattner00950542001-06-06 20:29:01 +0000226 output_vbr((unsigned)Slot, Out);
227 output(I->first, Out, false); // Don't force alignment...
228 }
229 }
230}
231
Anand Shuklaeea60fc2002-06-25 20:44:04 +0000232void WriteBytecodeToFile(const Module *C, std::ostream &Out) {
Chris Lattnere8fdde12001-09-07 16:39:41 +0000233 assert(C && "You can't write a null module!!");
Chris Lattner00950542001-06-06 20:29:01 +0000234
Chris Lattner697954c2002-01-20 22:54:45 +0000235 std::deque<unsigned char> Buffer;
Chris Lattner00950542001-06-06 20:29:01 +0000236
237 // This object populates buffer for us...
238 BytecodeWriter BCW(Buffer, C);
239
Chris Lattnerce6ef112002-07-26 18:40:14 +0000240 // Keep track of how much we've written...
241 BytesWritten += Buffer.size();
242
Chris Lattnere8fdde12001-09-07 16:39:41 +0000243 // Okay, write the deque out to the ostream now... the deque is not
244 // sequential in memory, however, so write out as much as possible in big
245 // chunks, until we're done.
246 //
Chris Lattner697954c2002-01-20 22:54:45 +0000247 std::deque<unsigned char>::const_iterator I = Buffer.begin(),E = Buffer.end();
Chris Lattnere8fdde12001-09-07 16:39:41 +0000248 while (I != E) { // Loop until it's all written
249 // Scan to see how big this chunk is...
250 const unsigned char *ChunkPtr = &*I;
251 const unsigned char *LastPtr = ChunkPtr;
252 while (I != E) {
253 const unsigned char *ThisPtr = &*++I;
Chris Lattner5cb17412001-11-04 21:32:41 +0000254 if (LastPtr+1 != ThisPtr) { // Advanced by more than a byte of memory?
255 ++LastPtr;
256 break;
257 }
Chris Lattnere8fdde12001-09-07 16:39:41 +0000258 LastPtr = ThisPtr;
259 }
260
261 // Write out the chunk...
Chris Lattner697954c2002-01-20 22:54:45 +0000262 Out.write((char*)ChunkPtr, LastPtr-ChunkPtr);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000263 }
264
Chris Lattner00950542001-06-06 20:29:01 +0000265 Out.flush();
266}