blob: 92b6a62cc6011cec7792e38fe4bc1dd6d85b9673 [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 Lattnera92f6962002-10-01 22:38:41 +000028#include "Support/Statistic.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<>
Chris Lattnera92f6962002-10-01 22:38:41 +000035BytesWritten("bytecodewriter", "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 Lattner6e6026b2002-11-20 18:36:02 +000061 outputSymbolTable(M->getSymbolTable());
Chris Lattner00950542001-06-06 20:29:01 +000062}
63
Vikram S. Advea7dac3d2002-07-14 23:07:51 +000064// Helper function for outputConstants().
65// Writes out all the constants in the plane Plane starting at entry StartNo.
66//
67void BytecodeWriter::outputConstantsInPlane(const std::vector<const Value*>
68 &Plane, unsigned StartNo) {
69 unsigned ValNo = StartNo;
70
71 // Scan through and ignore function arguments...
72 for (; ValNo < Plane.size() && isa<Argument>(Plane[ValNo]); ValNo++)
73 /*empty*/;
74
75 unsigned NC = ValNo; // Number of constants
76 for (; NC < Plane.size() &&
77 (isa<Constant>(Plane[NC]) || isa<Type>(Plane[NC])); NC++)
78 /*empty*/;
79 NC -= ValNo; // Convert from index into count
80 if (NC == 0) return; // Skip empty type planes...
81
82 // Output type header: [num entries][type id number]
83 //
84 output_vbr(NC, Out);
85
86 // Output the Type ID Number...
87 int Slot = Table.getValSlot(Plane.front()->getType());
88 assert (Slot != -1 && "Type in constant pool but not in function!!");
89 output_vbr((unsigned)Slot, Out);
90
91 //cerr << "Emitting " << NC << " constants of type '"
92 // << Plane.front()->getType()->getName() << "' = Slot #" << Slot << "\n";
93
94 for (unsigned i = ValNo; i < ValNo+NC; ++i) {
95 const Value *V = Plane[i];
96 if (const Constant *CPV = dyn_cast<Constant>(V)) {
97 //cerr << "Serializing value: <" << V->getType() << ">: " << V << ":"
98 // << Out.size() << "\n";
99 outputConstant(CPV);
100 } else {
101 outputType(cast<const Type>(V));
102 }
103 }
104}
105
Chris Lattner79df7c02002-03-26 18:01:55 +0000106void BytecodeWriter::outputConstants(bool isFunction) {
Chris Lattnere8fdde12001-09-07 16:39:41 +0000107 BytecodeBlock CPool(BytecodeFormat::ConstantPool, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000108
109 unsigned NumPlanes = Table.getNumPlanes();
Vikram S. Advea7dac3d2002-07-14 23:07:51 +0000110
111 // Write the type plane for types first because earlier planes
112 // (e.g. for a primitive type like float) may have constants constructed
113 // using types coming later (e.g., via getelementptr from a pointer type).
114 // The type plane is needed before types can be fwd or bkwd referenced.
115 if (!isFunction) {
116 const std::vector<const Value*> &Plane = Table.getPlane(Type::TypeTyID);
117 assert(!Plane.empty() && "No types at all?");
118 unsigned ValNo = Type::FirstDerivedTyID; // Start at the derived types...
119 outputConstantsInPlane(Plane, ValNo); // Write out the types
120 }
121
Chris Lattner6463e0d2002-10-14 03:34:17 +0000122 for (unsigned pno = 0; pno != NumPlanes; pno++) {
Chris Lattner697954c2002-01-20 22:54:45 +0000123 const std::vector<const Value*> &Plane = Table.getPlane(pno);
Chris Lattner6463e0d2002-10-14 03:34:17 +0000124 if (!Plane.empty()) { // Skip empty type planes...
125 unsigned ValNo = 0;
126 if (isFunction) // Don't reemit module constants
127 ValNo = Table.getModuleLevel(pno);
128 else if (pno == Type::TypeTyID) // If type plane wasn't written out above
129 continue;
130
131 outputConstantsInPlane(Plane, ValNo); // Write out constants in the plane
132 }
Chris Lattner00950542001-06-06 20:29:01 +0000133 }
Chris Lattner00950542001-06-06 20:29:01 +0000134}
135
136void BytecodeWriter::outputModuleInfoBlock(const Module *M) {
137 BytecodeBlock ModuleInfoBlock(BytecodeFormat::ModuleGlobalInfo, Out);
138
Chris Lattner70cc3392001-09-10 07:58:01 +0000139 // Output the types for the global variables in the module...
140 for (Module::const_giterator I = M->gbegin(), End = M->gend(); I != End;++I) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000141 int Slot = Table.getValSlot(I->getType());
Chris Lattner70cc3392001-09-10 07:58:01 +0000142 assert(Slot != -1 && "Module global vars is broken!");
Chris Lattnerd70684f2001-09-18 04:01:05 +0000143
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000144 // Fields: bit0 = isConstant, bit1 = hasInitializer, bit2=InternalLinkage,
145 // bit3+ = slot#
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000146 unsigned oSlot = ((unsigned)Slot << 3) | (I->hasInternalLinkage() << 2) |
147 (I->hasInitializer() << 1) | I->isConstant();
Chris Lattnerd70684f2001-09-18 04:01:05 +0000148 output_vbr(oSlot, Out);
149
Chris Lattner1b98c5c2001-10-13 06:48:38 +0000150 // If we have an initializer, output it now.
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000151 if (I->hasInitializer()) {
152 Slot = Table.getValSlot((Value*)I->getInitializer());
Chris Lattnerd70684f2001-09-18 04:01:05 +0000153 assert(Slot != -1 && "No slot for global var initializer!");
154 output_vbr((unsigned)Slot, Out);
155 }
Chris Lattner70cc3392001-09-10 07:58:01 +0000156 }
157 output_vbr((unsigned)Table.getValSlot(Type::VoidTy), Out);
158
Chris Lattnerb5794002002-04-07 22:49:37 +0000159 // Output the types of the functions in this module...
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000160 for (Module::const_iterator I = M->begin(), End = M->end(); I != End; ++I) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000161 int Slot = Table.getValSlot(I->getType());
Chris Lattner00950542001-06-06 20:29:01 +0000162 assert(Slot != -1 && "Module const pool is broken!");
163 assert(Slot >= Type::FirstDerivedTyID && "Derived type not in range!");
164 output_vbr((unsigned)Slot, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000165 }
166 output_vbr((unsigned)Table.getValSlot(Type::VoidTy), Out);
Chris Lattner70cc3392001-09-10 07:58:01 +0000167
168
Chris Lattner00950542001-06-06 20:29:01 +0000169 align32(Out);
170}
171
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000172void BytecodeWriter::processMethod(const Function *F) {
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000173 BytecodeBlock FunctionBlock(BytecodeFormat::Function, Out);
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000174 output_vbr((unsigned)F->hasInternalLinkage(), Out);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000175 // Only output the constant pool and other goodies if needed...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000176 if (!F->isExternal()) {
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000177
Chris Lattnerb5794002002-04-07 22:49:37 +0000178 // Get slot information about the function...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000179 Table.incorporateFunction(F);
Chris Lattner00950542001-06-06 20:29:01 +0000180
Chris Lattnerb5794002002-04-07 22:49:37 +0000181 // Output information about the constants in the function...
Chris Lattnere8fdde12001-09-07 16:39:41 +0000182 outputConstants(true);
Chris Lattner00950542001-06-06 20:29:01 +0000183
Chris Lattnere8fdde12001-09-07 16:39:41 +0000184 // Output basic block nodes...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000185 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
186 processBasicBlock(*I);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000187
Chris Lattnerb5794002002-04-07 22:49:37 +0000188 // If needed, output the symbol table for the function...
Chris Lattner6e6026b2002-11-20 18:36:02 +0000189 outputSymbolTable(F->getSymbolTable());
Chris Lattnere8fdde12001-09-07 16:39:41 +0000190
Chris Lattnerb5794002002-04-07 22:49:37 +0000191 Table.purgeFunction();
Chris Lattnere8fdde12001-09-07 16:39:41 +0000192 }
Chris Lattner00950542001-06-06 20:29:01 +0000193}
194
195
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000196void BytecodeWriter::processBasicBlock(const BasicBlock &BB) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000197 BytecodeBlock FunctionBlock(BytecodeFormat::BasicBlock, Out);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000198 // Process all the instructions in the bb...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000199 for(BasicBlock::const_iterator I = BB.begin(), E = BB.end(); I != E; ++I)
200 processInstruction(*I);
Chris Lattner00950542001-06-06 20:29:01 +0000201}
202
203void BytecodeWriter::outputSymbolTable(const SymbolTable &MST) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000204 BytecodeBlock FunctionBlock(BytecodeFormat::SymbolTable, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000205
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000206 for (SymbolTable::const_iterator TI = MST.begin(); TI != MST.end(); ++TI) {
Chris Lattner00950542001-06-06 20:29:01 +0000207 SymbolTable::type_const_iterator I = MST.type_begin(TI->first);
208 SymbolTable::type_const_iterator End = MST.type_end(TI->first);
209 int Slot;
210
211 if (I == End) continue; // Don't mess with an absent type...
212
213 // Symtab block header: [num entries][type id number]
214 output_vbr(MST.type_size(TI->first), Out);
215
216 Slot = Table.getValSlot(TI->first);
217 assert(Slot != -1 && "Type in symtab, but not in table!");
218 output_vbr((unsigned)Slot, Out);
219
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000220 for (; I != End; ++I) {
Chris Lattner00950542001-06-06 20:29:01 +0000221 // Symtab entry: [def slot #][name]
222 Slot = Table.getValSlot(I->second);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000223 assert(Slot != -1 && "Value in symtab but has no slot number!!");
Chris Lattner00950542001-06-06 20:29:01 +0000224 output_vbr((unsigned)Slot, Out);
225 output(I->first, Out, false); // Don't force alignment...
226 }
227 }
228}
229
Anand Shuklaeea60fc2002-06-25 20:44:04 +0000230void WriteBytecodeToFile(const Module *C, std::ostream &Out) {
Chris Lattnere8fdde12001-09-07 16:39:41 +0000231 assert(C && "You can't write a null module!!");
Chris Lattner00950542001-06-06 20:29:01 +0000232
Chris Lattner697954c2002-01-20 22:54:45 +0000233 std::deque<unsigned char> Buffer;
Chris Lattner00950542001-06-06 20:29:01 +0000234
235 // This object populates buffer for us...
236 BytecodeWriter BCW(Buffer, C);
237
Chris Lattnerce6ef112002-07-26 18:40:14 +0000238 // Keep track of how much we've written...
239 BytesWritten += Buffer.size();
240
Chris Lattnere8fdde12001-09-07 16:39:41 +0000241 // Okay, write the deque out to the ostream now... the deque is not
242 // sequential in memory, however, so write out as much as possible in big
243 // chunks, until we're done.
244 //
Chris Lattner697954c2002-01-20 22:54:45 +0000245 std::deque<unsigned char>::const_iterator I = Buffer.begin(),E = Buffer.end();
Chris Lattnere8fdde12001-09-07 16:39:41 +0000246 while (I != E) { // Loop until it's all written
247 // Scan to see how big this chunk is...
248 const unsigned char *ChunkPtr = &*I;
249 const unsigned char *LastPtr = ChunkPtr;
250 while (I != E) {
251 const unsigned char *ThisPtr = &*++I;
Chris Lattner5cb17412001-11-04 21:32:41 +0000252 if (LastPtr+1 != ThisPtr) { // Advanced by more than a byte of memory?
253 ++LastPtr;
254 break;
255 }
Chris Lattnere8fdde12001-09-07 16:39:41 +0000256 LastPtr = ThisPtr;
257 }
258
259 // Write out the chunk...
Chris Lattner697954c2002-01-20 22:54:45 +0000260 Out.write((char*)ChunkPtr, LastPtr-ChunkPtr);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000261 }
262
Chris Lattner00950542001-06-06 20:29:01 +0000263 Out.flush();
264}