blob: 413fd293dbd7eb46d0c6168b84a23f9885655697 [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===-- Writer.cpp - Library for writing VM bytecode files ----------------===//
Chris Lattner00950542001-06-06 20:29:01 +00002//
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 Lattnerabe83ae2003-09-15 00:33:20 +00006// to a deque of unsigned char, 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"
John Criswell7a73b802003-06-30 21:59:07 +000029#include "Config/string.h"
Chris Lattner00950542001-06-06 20:29:01 +000030#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 Lattnerd445c6b2003-08-24 13:47:36 +000046 bool isBigEndian = M->getEndianness() == Module::BigEndian;
47 bool hasLongPointers = M->getPointerSize() == Module::Pointer64;
48 bool hasNoEndianness = M->getEndianness() == Module::AnyEndianness;
49 bool hasNoPointerSize = M->getPointerSize() == Module::AnyPointerSize;
Chris Lattner186a1f72003-03-19 20:56:46 +000050
Chris Lattnere3869c82003-04-16 21:16:05 +000051 // Output the version identifier... we are currently on bytecode version #2
Chris Lattnerd445c6b2003-08-24 13:47:36 +000052 unsigned Version = (2 << 4) | isBigEndian | (hasLongPointers << 1) |
53 (hasNoEndianness << 2) | (hasNoPointerSize << 3);
Chris Lattner186a1f72003-03-19 20:56:46 +000054 output_vbr(Version, Out);
Chris Lattner00950542001-06-06 20:29:01 +000055 align32(Out);
56
Chris Lattner186a1f72003-03-19 20:56:46 +000057 {
58 BytecodeBlock CPool(BytecodeFormat::GlobalTypePlane, Out);
59
60 // Write the type plane for types first because earlier planes (e.g. for a
61 // primitive type like float) may have constants constructed using types
62 // coming later (e.g., via getelementptr from a pointer type). The type
63 // plane is needed before types can be fwd or bkwd referenced.
64 const std::vector<const Value*> &Plane = Table.getPlane(Type::TypeTyID);
65 assert(!Plane.empty() && "No types at all?");
66 unsigned ValNo = Type::FirstDerivedTyID; // Start at the derived types...
67 outputConstantsInPlane(Plane, ValNo); // Write out the types
68 }
Chris Lattner00950542001-06-06 20:29:01 +000069
Chris Lattner186a1f72003-03-19 20:56:46 +000070 // The ModuleInfoBlock follows directly after the type information
Chris Lattnere8fdde12001-09-07 16:39:41 +000071 outputModuleInfoBlock(M);
72
Chris Lattner186a1f72003-03-19 20:56:46 +000073 // Output module level constants, used for global variable initializers
74 outputConstants(false);
75
Chris Lattnerb5794002002-04-07 22:49:37 +000076 // Do the whole module now! Process each function at a time...
Chris Lattner0b12b5f2002-06-25 16:13:21 +000077 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
Chris Lattner186a1f72003-03-19 20:56:46 +000078 outputFunction(I);
Chris Lattnere8fdde12001-09-07 16:39:41 +000079
80 // If needed, output the symbol table for the module...
Chris Lattner6e6026b2002-11-20 18:36:02 +000081 outputSymbolTable(M->getSymbolTable());
Chris Lattner00950542001-06-06 20:29:01 +000082}
83
Vikram S. Advea7dac3d2002-07-14 23:07:51 +000084// Helper function for outputConstants().
85// Writes out all the constants in the plane Plane starting at entry StartNo.
86//
87void BytecodeWriter::outputConstantsInPlane(const std::vector<const Value*>
88 &Plane, unsigned StartNo) {
89 unsigned ValNo = StartNo;
90
Chris Lattner186a1f72003-03-19 20:56:46 +000091 // Scan through and ignore function arguments/global values...
92 for (; ValNo < Plane.size() && (isa<Argument>(Plane[ValNo]) ||
93 isa<GlobalValue>(Plane[ValNo])); ValNo++)
Vikram S. Advea7dac3d2002-07-14 23:07:51 +000094 /*empty*/;
95
96 unsigned NC = ValNo; // Number of constants
97 for (; NC < Plane.size() &&
98 (isa<Constant>(Plane[NC]) || isa<Type>(Plane[NC])); NC++)
99 /*empty*/;
100 NC -= ValNo; // Convert from index into count
101 if (NC == 0) return; // Skip empty type planes...
102
103 // Output type header: [num entries][type id number]
104 //
105 output_vbr(NC, Out);
106
107 // Output the Type ID Number...
108 int Slot = Table.getValSlot(Plane.front()->getType());
109 assert (Slot != -1 && "Type in constant pool but not in function!!");
110 output_vbr((unsigned)Slot, Out);
111
112 //cerr << "Emitting " << NC << " constants of type '"
113 // << Plane.front()->getType()->getName() << "' = Slot #" << Slot << "\n";
114
115 for (unsigned i = ValNo; i < ValNo+NC; ++i) {
116 const Value *V = Plane[i];
117 if (const Constant *CPV = dyn_cast<Constant>(V)) {
118 //cerr << "Serializing value: <" << V->getType() << ">: " << V << ":"
119 // << Out.size() << "\n";
120 outputConstant(CPV);
121 } else {
Chris Lattner186a1f72003-03-19 20:56:46 +0000122 outputType(cast<Type>(V));
Vikram S. Advea7dac3d2002-07-14 23:07:51 +0000123 }
124 }
125}
126
Chris Lattner79df7c02002-03-26 18:01:55 +0000127void BytecodeWriter::outputConstants(bool isFunction) {
Chris Lattnere8fdde12001-09-07 16:39:41 +0000128 BytecodeBlock CPool(BytecodeFormat::ConstantPool, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000129
130 unsigned NumPlanes = Table.getNumPlanes();
Chris Lattnerf69315b2003-05-22 18:35:38 +0000131
132 // Output the type plane before any constants!
133 if (isFunction && NumPlanes > Type::TypeTyID) {
134 const std::vector<const Value*> &Plane = Table.getPlane(Type::TypeTyID);
Chris Lattner186a1f72003-03-19 20:56:46 +0000135 if (!Plane.empty()) { // Skip empty type planes...
Chris Lattnerf69315b2003-05-22 18:35:38 +0000136 unsigned ValNo = Table.getModuleLevel(Type::TypeTyID);
137 outputConstantsInPlane(Plane, ValNo);
Chris Lattner6463e0d2002-10-14 03:34:17 +0000138 }
Chris Lattner00950542001-06-06 20:29:01 +0000139 }
Chris Lattnerf69315b2003-05-22 18:35:38 +0000140
141 for (unsigned pno = 0; pno != NumPlanes; pno++)
142 if (pno != Type::TypeTyID) { // Type plane handled above.
143 const std::vector<const Value*> &Plane = Table.getPlane(pno);
144 if (!Plane.empty()) { // Skip empty type planes...
145 unsigned ValNo = 0;
Misha Brukman37f92e22003-09-11 22:34:13 +0000146 if (isFunction) // Don't re-emit module constants
Chris Lattnerf69315b2003-05-22 18:35:38 +0000147 ValNo += Table.getModuleLevel(pno);
148
149 if (pno >= Type::FirstDerivedTyID) {
150 // Skip zero initializer
151 if (ValNo == 0)
152 ValNo = 1;
153 }
154
155 // Write out constants in the plane
156 outputConstantsInPlane(Plane, ValNo);
157 }
158 }
Chris Lattner00950542001-06-06 20:29:01 +0000159}
160
161void BytecodeWriter::outputModuleInfoBlock(const Module *M) {
162 BytecodeBlock ModuleInfoBlock(BytecodeFormat::ModuleGlobalInfo, Out);
163
Chris Lattner70cc3392001-09-10 07:58:01 +0000164 // Output the types for the global variables in the module...
165 for (Module::const_giterator I = M->gbegin(), End = M->gend(); I != End;++I) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000166 int Slot = Table.getValSlot(I->getType());
Chris Lattner70cc3392001-09-10 07:58:01 +0000167 assert(Slot != -1 && "Module global vars is broken!");
Chris Lattnerd70684f2001-09-18 04:01:05 +0000168
Chris Lattnere3869c82003-04-16 21:16:05 +0000169 // Fields: bit0 = isConstant, bit1 = hasInitializer, bit2,3=Linkage,
170 // bit4+ = Slot # for type
171 unsigned oSlot = ((unsigned)Slot << 4) | ((unsigned)I->getLinkage() << 2) |
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000172 (I->hasInitializer() << 1) | I->isConstant();
Chris Lattnerd70684f2001-09-18 04:01:05 +0000173 output_vbr(oSlot, Out);
174
Chris Lattner1b98c5c2001-10-13 06:48:38 +0000175 // If we have an initializer, output it now.
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000176 if (I->hasInitializer()) {
177 Slot = Table.getValSlot((Value*)I->getInitializer());
Chris Lattnerd70684f2001-09-18 04:01:05 +0000178 assert(Slot != -1 && "No slot for global var initializer!");
179 output_vbr((unsigned)Slot, Out);
180 }
Chris Lattner70cc3392001-09-10 07:58:01 +0000181 }
182 output_vbr((unsigned)Table.getValSlot(Type::VoidTy), Out);
183
Chris Lattnerb5794002002-04-07 22:49:37 +0000184 // Output the types of the functions in this module...
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000185 for (Module::const_iterator I = M->begin(), End = M->end(); I != End; ++I) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000186 int Slot = Table.getValSlot(I->getType());
Chris Lattner00950542001-06-06 20:29:01 +0000187 assert(Slot != -1 && "Module const pool is broken!");
188 assert(Slot >= Type::FirstDerivedTyID && "Derived type not in range!");
189 output_vbr((unsigned)Slot, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000190 }
191 output_vbr((unsigned)Table.getValSlot(Type::VoidTy), Out);
Chris Lattner70cc3392001-09-10 07:58:01 +0000192
Chris Lattner00950542001-06-06 20:29:01 +0000193 align32(Out);
194}
195
Chris Lattner186a1f72003-03-19 20:56:46 +0000196void BytecodeWriter::outputFunction(const Function *F) {
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000197 BytecodeBlock FunctionBlock(BytecodeFormat::Function, Out);
Chris Lattnere3869c82003-04-16 21:16:05 +0000198 output_vbr((unsigned)F->getLinkage(), Out);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000199 // Only output the constant pool and other goodies if needed...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000200 if (!F->isExternal()) {
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000201
Chris Lattnerb5794002002-04-07 22:49:37 +0000202 // Get slot information about the function...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000203 Table.incorporateFunction(F);
Chris Lattner00950542001-06-06 20:29:01 +0000204
Chris Lattnerb5794002002-04-07 22:49:37 +0000205 // Output information about the constants in the function...
Chris Lattnere8fdde12001-09-07 16:39:41 +0000206 outputConstants(true);
Chris Lattner00950542001-06-06 20:29:01 +0000207
Chris Lattnere8fdde12001-09-07 16:39:41 +0000208 // Output basic block nodes...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000209 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
210 processBasicBlock(*I);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000211
Chris Lattnerb5794002002-04-07 22:49:37 +0000212 // If needed, output the symbol table for the function...
Chris Lattner6e6026b2002-11-20 18:36:02 +0000213 outputSymbolTable(F->getSymbolTable());
Chris Lattnere8fdde12001-09-07 16:39:41 +0000214
Chris Lattnerb5794002002-04-07 22:49:37 +0000215 Table.purgeFunction();
Chris Lattnere8fdde12001-09-07 16:39:41 +0000216 }
Chris Lattner00950542001-06-06 20:29:01 +0000217}
218
219
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000220void BytecodeWriter::processBasicBlock(const BasicBlock &BB) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000221 BytecodeBlock FunctionBlock(BytecodeFormat::BasicBlock, Out);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000222 // Process all the instructions in the bb...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000223 for(BasicBlock::const_iterator I = BB.begin(), E = BB.end(); I != E; ++I)
224 processInstruction(*I);
Chris Lattner00950542001-06-06 20:29:01 +0000225}
226
227void BytecodeWriter::outputSymbolTable(const SymbolTable &MST) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000228 BytecodeBlock FunctionBlock(BytecodeFormat::SymbolTable, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000229
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000230 for (SymbolTable::const_iterator TI = MST.begin(); TI != MST.end(); ++TI) {
Chris Lattner00950542001-06-06 20:29:01 +0000231 SymbolTable::type_const_iterator I = MST.type_begin(TI->first);
232 SymbolTable::type_const_iterator End = MST.type_end(TI->first);
233 int Slot;
234
235 if (I == End) continue; // Don't mess with an absent type...
236
237 // Symtab block header: [num entries][type id number]
238 output_vbr(MST.type_size(TI->first), Out);
239
240 Slot = Table.getValSlot(TI->first);
241 assert(Slot != -1 && "Type in symtab, but not in table!");
242 output_vbr((unsigned)Slot, Out);
243
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000244 for (; I != End; ++I) {
Chris Lattner00950542001-06-06 20:29:01 +0000245 // Symtab entry: [def slot #][name]
246 Slot = Table.getValSlot(I->second);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000247 assert(Slot != -1 && "Value in symtab but has no slot number!!");
Chris Lattner00950542001-06-06 20:29:01 +0000248 output_vbr((unsigned)Slot, Out);
249 output(I->first, Out, false); // Don't force alignment...
250 }
251 }
252}
253
Anand Shuklaeea60fc2002-06-25 20:44:04 +0000254void WriteBytecodeToFile(const Module *C, std::ostream &Out) {
Chris Lattnere8fdde12001-09-07 16:39:41 +0000255 assert(C && "You can't write a null module!!");
Chris Lattner00950542001-06-06 20:29:01 +0000256
Chris Lattner697954c2002-01-20 22:54:45 +0000257 std::deque<unsigned char> Buffer;
Chris Lattner00950542001-06-06 20:29:01 +0000258
259 // This object populates buffer for us...
260 BytecodeWriter BCW(Buffer, C);
261
Chris Lattnerce6ef112002-07-26 18:40:14 +0000262 // Keep track of how much we've written...
263 BytesWritten += Buffer.size();
264
Chris Lattnere8fdde12001-09-07 16:39:41 +0000265 // Okay, write the deque out to the ostream now... the deque is not
266 // sequential in memory, however, so write out as much as possible in big
267 // chunks, until we're done.
268 //
Chris Lattner697954c2002-01-20 22:54:45 +0000269 std::deque<unsigned char>::const_iterator I = Buffer.begin(),E = Buffer.end();
Chris Lattnere8fdde12001-09-07 16:39:41 +0000270 while (I != E) { // Loop until it's all written
271 // Scan to see how big this chunk is...
272 const unsigned char *ChunkPtr = &*I;
273 const unsigned char *LastPtr = ChunkPtr;
274 while (I != E) {
275 const unsigned char *ThisPtr = &*++I;
Chris Lattner5cb17412001-11-04 21:32:41 +0000276 if (LastPtr+1 != ThisPtr) { // Advanced by more than a byte of memory?
277 ++LastPtr;
278 break;
279 }
Chris Lattnere8fdde12001-09-07 16:39:41 +0000280 LastPtr = ThisPtr;
281 }
282
283 // Write out the chunk...
Chris Lattner697954c2002-01-20 22:54:45 +0000284 Out.write((char*)ChunkPtr, LastPtr-ChunkPtr);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000285 }
286
Chris Lattner00950542001-06-06 20:29:01 +0000287 Out.flush();
288}