blob: 096846145673a1814a3105e27db6989d8ff2624a [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 Lattnereff112c2003-10-18 05:54:48 +000051 // Output the version identifier... we are currently on bytecode version #0
52 unsigned Version = (0 << 4) | isBigEndian | (hasLongPointers << 1) |
Chris Lattnerd445c6b2003-08-24 13:47:36 +000053 (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...
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000108 int Slot = Table.getSlot(Plane.front()->getType());
Vikram S. Advea7dac3d2002-07-14 23:07:51 +0000109 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
Chris Lattner6b252422003-10-16 18:28:50 +0000161static unsigned getEncodedLinkage(const GlobalValue *GV) {
162 switch (GV->getLinkage()) {
163 default: assert(0 && "Invalid linkage!");
164 case GlobalValue::ExternalLinkage: return 0;
165 case GlobalValue::LinkOnceLinkage: return 1;
166 case GlobalValue::WeakLinkage: return 1;
167 case GlobalValue::AppendingLinkage: return 2;
168 case GlobalValue::InternalLinkage: return 3;
169 }
170}
171
Chris Lattner00950542001-06-06 20:29:01 +0000172void BytecodeWriter::outputModuleInfoBlock(const Module *M) {
173 BytecodeBlock ModuleInfoBlock(BytecodeFormat::ModuleGlobalInfo, Out);
174
Chris Lattner70cc3392001-09-10 07:58:01 +0000175 // Output the types for the global variables in the module...
176 for (Module::const_giterator I = M->gbegin(), End = M->gend(); I != End;++I) {
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000177 int Slot = Table.getSlot(I->getType());
Chris Lattner70cc3392001-09-10 07:58:01 +0000178 assert(Slot != -1 && "Module global vars is broken!");
Chris Lattnerd70684f2001-09-18 04:01:05 +0000179
Chris Lattnere3869c82003-04-16 21:16:05 +0000180 // Fields: bit0 = isConstant, bit1 = hasInitializer, bit2,3=Linkage,
181 // bit4+ = Slot # for type
Chris Lattner6b252422003-10-16 18:28:50 +0000182 unsigned oSlot = ((unsigned)Slot << 4) | (getEncodedLinkage(I) << 2) |
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000183 (I->hasInitializer() << 1) | I->isConstant();
Chris Lattnerd70684f2001-09-18 04:01:05 +0000184 output_vbr(oSlot, Out);
185
Chris Lattner1b98c5c2001-10-13 06:48:38 +0000186 // If we have an initializer, output it now.
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000187 if (I->hasInitializer()) {
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000188 Slot = Table.getSlot((Value*)I->getInitializer());
Chris Lattnerd70684f2001-09-18 04:01:05 +0000189 assert(Slot != -1 && "No slot for global var initializer!");
190 output_vbr((unsigned)Slot, Out);
191 }
Chris Lattner70cc3392001-09-10 07:58:01 +0000192 }
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000193 output_vbr((unsigned)Table.getSlot(Type::VoidTy), Out);
Chris Lattner70cc3392001-09-10 07:58:01 +0000194
Chris Lattnerb5794002002-04-07 22:49:37 +0000195 // Output the types of the functions in this module...
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000196 for (Module::const_iterator I = M->begin(), End = M->end(); I != End; ++I) {
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000197 int Slot = Table.getSlot(I->getType());
Chris Lattner00950542001-06-06 20:29:01 +0000198 assert(Slot != -1 && "Module const pool is broken!");
199 assert(Slot >= Type::FirstDerivedTyID && "Derived type not in range!");
200 output_vbr((unsigned)Slot, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000201 }
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000202 output_vbr((unsigned)Table.getSlot(Type::VoidTy), Out);
Chris Lattner70cc3392001-09-10 07:58:01 +0000203
Chris Lattner00950542001-06-06 20:29:01 +0000204 align32(Out);
205}
206
Chris Lattner186a1f72003-03-19 20:56:46 +0000207void BytecodeWriter::outputFunction(const Function *F) {
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000208 BytecodeBlock FunctionBlock(BytecodeFormat::Function, Out);
Chris Lattner6b252422003-10-16 18:28:50 +0000209 output_vbr(getEncodedLinkage(F), Out);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000210 // Only output the constant pool and other goodies if needed...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000211 if (!F->isExternal()) {
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000212
Chris Lattnerb5794002002-04-07 22:49:37 +0000213 // Get slot information about the function...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000214 Table.incorporateFunction(F);
Chris Lattner00950542001-06-06 20:29:01 +0000215
Chris Lattnerb5794002002-04-07 22:49:37 +0000216 // Output information about the constants in the function...
Chris Lattnere8fdde12001-09-07 16:39:41 +0000217 outputConstants(true);
Chris Lattner00950542001-06-06 20:29:01 +0000218
Chris Lattnere8fdde12001-09-07 16:39:41 +0000219 // Output basic block nodes...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000220 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
221 processBasicBlock(*I);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000222
Chris Lattnerb5794002002-04-07 22:49:37 +0000223 // If needed, output the symbol table for the function...
Chris Lattner6e6026b2002-11-20 18:36:02 +0000224 outputSymbolTable(F->getSymbolTable());
Chris Lattnere8fdde12001-09-07 16:39:41 +0000225
Chris Lattnerb5794002002-04-07 22:49:37 +0000226 Table.purgeFunction();
Chris Lattnere8fdde12001-09-07 16:39:41 +0000227 }
Chris Lattner00950542001-06-06 20:29:01 +0000228}
229
230
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000231void BytecodeWriter::processBasicBlock(const BasicBlock &BB) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000232 BytecodeBlock FunctionBlock(BytecodeFormat::BasicBlock, Out);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000233 // Process all the instructions in the bb...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000234 for(BasicBlock::const_iterator I = BB.begin(), E = BB.end(); I != E; ++I)
235 processInstruction(*I);
Chris Lattner00950542001-06-06 20:29:01 +0000236}
237
238void BytecodeWriter::outputSymbolTable(const SymbolTable &MST) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000239 BytecodeBlock FunctionBlock(BytecodeFormat::SymbolTable, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000240
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000241 for (SymbolTable::const_iterator TI = MST.begin(); TI != MST.end(); ++TI) {
Chris Lattner00950542001-06-06 20:29:01 +0000242 SymbolTable::type_const_iterator I = MST.type_begin(TI->first);
243 SymbolTable::type_const_iterator End = MST.type_end(TI->first);
244 int Slot;
245
246 if (I == End) continue; // Don't mess with an absent type...
247
248 // Symtab block header: [num entries][type id number]
249 output_vbr(MST.type_size(TI->first), Out);
250
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000251 Slot = Table.getSlot(TI->first);
Chris Lattner00950542001-06-06 20:29:01 +0000252 assert(Slot != -1 && "Type in symtab, but not in table!");
253 output_vbr((unsigned)Slot, Out);
254
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000255 for (; I != End; ++I) {
Chris Lattner00950542001-06-06 20:29:01 +0000256 // Symtab entry: [def slot #][name]
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000257 Slot = Table.getSlot(I->second);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000258 assert(Slot != -1 && "Value in symtab but has no slot number!!");
Chris Lattner00950542001-06-06 20:29:01 +0000259 output_vbr((unsigned)Slot, Out);
260 output(I->first, Out, false); // Don't force alignment...
261 }
262 }
263}
264
Anand Shuklaeea60fc2002-06-25 20:44:04 +0000265void WriteBytecodeToFile(const Module *C, std::ostream &Out) {
Chris Lattnere8fdde12001-09-07 16:39:41 +0000266 assert(C && "You can't write a null module!!");
Chris Lattner00950542001-06-06 20:29:01 +0000267
Chris Lattner697954c2002-01-20 22:54:45 +0000268 std::deque<unsigned char> Buffer;
Chris Lattner00950542001-06-06 20:29:01 +0000269
270 // This object populates buffer for us...
271 BytecodeWriter BCW(Buffer, C);
272
Chris Lattnerce6ef112002-07-26 18:40:14 +0000273 // Keep track of how much we've written...
274 BytesWritten += Buffer.size();
275
Chris Lattnere8fdde12001-09-07 16:39:41 +0000276 // Okay, write the deque out to the ostream now... the deque is not
277 // sequential in memory, however, so write out as much as possible in big
278 // chunks, until we're done.
279 //
Chris Lattner697954c2002-01-20 22:54:45 +0000280 std::deque<unsigned char>::const_iterator I = Buffer.begin(),E = Buffer.end();
Chris Lattnere8fdde12001-09-07 16:39:41 +0000281 while (I != E) { // Loop until it's all written
282 // Scan to see how big this chunk is...
283 const unsigned char *ChunkPtr = &*I;
284 const unsigned char *LastPtr = ChunkPtr;
285 while (I != E) {
286 const unsigned char *ThisPtr = &*++I;
Chris Lattner5cb17412001-11-04 21:32:41 +0000287 if (LastPtr+1 != ThisPtr) { // Advanced by more than a byte of memory?
288 ++LastPtr;
289 break;
290 }
Chris Lattnere8fdde12001-09-07 16:39:41 +0000291 LastPtr = ThisPtr;
292 }
293
294 // Write out the chunk...
Chris Lattner697954c2002-01-20 22:54:45 +0000295 Out.write((char*)ChunkPtr, LastPtr-ChunkPtr);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000296 }
297
Chris Lattner00950542001-06-06 20:29:01 +0000298 Out.flush();
299}