blob: 334622dfa239a58e668fa26a021ee78f7f1147cf [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"
23#include "llvm/Module.h"
Chris Lattner00950542001-06-06 20:29:01 +000024#include "llvm/SymbolTable.h"
25#include "llvm/DerivedTypes.h"
Chris Lattnerf60dc882001-11-29 16:32:16 +000026#include "Support/STLExtras.h"
Chris Lattner00950542001-06-06 20:29:01 +000027#include <string.h>
28#include <algorithm>
29
Chris Lattner697954c2002-01-20 22:54:45 +000030BytecodeWriter::BytecodeWriter(std::deque<unsigned char> &o, const Module *M)
Chris Lattner00950542001-06-06 20:29:01 +000031 : Out(o), Table(M, false) {
32
33 outputSignature();
34
35 // Emit the top level CLASS block.
36 BytecodeBlock ModuleBlock(BytecodeFormat::Module, Out);
37
Chris Lattnere8fdde12001-09-07 16:39:41 +000038 // Output the ID of first "derived" type:
Chris Lattner00950542001-06-06 20:29:01 +000039 output_vbr((unsigned)Type::FirstDerivedTyID, Out);
40 align32(Out);
41
Chris Lattnerb5794002002-04-07 22:49:37 +000042 // Output module level constants, including types used by the function protos
Chris Lattnere8fdde12001-09-07 16:39:41 +000043 outputConstants(false);
Chris Lattner00950542001-06-06 20:29:01 +000044
Chris Lattnere8fdde12001-09-07 16:39:41 +000045 // The ModuleInfoBlock follows directly after the Module constant pool
46 outputModuleInfoBlock(M);
47
Chris Lattnerb5794002002-04-07 22:49:37 +000048 // Do the whole module now! Process each function at a time...
Chris Lattner0b12b5f2002-06-25 16:13:21 +000049 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
50 processMethod(I);
Chris Lattnere8fdde12001-09-07 16:39:41 +000051
52 // If needed, output the symbol table for the module...
Chris Lattner00950542001-06-06 20:29:01 +000053 if (M->hasSymbolTable())
54 outputSymbolTable(*M->getSymbolTable());
55}
56
Vikram S. Advea7dac3d2002-07-14 23:07:51 +000057// Helper function for outputConstants().
58// Writes out all the constants in the plane Plane starting at entry StartNo.
59//
60void BytecodeWriter::outputConstantsInPlane(const std::vector<const Value*>
61 &Plane, unsigned StartNo) {
62 unsigned ValNo = StartNo;
63
64 // Scan through and ignore function arguments...
65 for (; ValNo < Plane.size() && isa<Argument>(Plane[ValNo]); ValNo++)
66 /*empty*/;
67
68 unsigned NC = ValNo; // Number of constants
69 for (; NC < Plane.size() &&
70 (isa<Constant>(Plane[NC]) || isa<Type>(Plane[NC])); NC++)
71 /*empty*/;
72 NC -= ValNo; // Convert from index into count
73 if (NC == 0) return; // Skip empty type planes...
74
75 // Output type header: [num entries][type id number]
76 //
77 output_vbr(NC, Out);
78
79 // Output the Type ID Number...
80 int Slot = Table.getValSlot(Plane.front()->getType());
81 assert (Slot != -1 && "Type in constant pool but not in function!!");
82 output_vbr((unsigned)Slot, Out);
83
84 //cerr << "Emitting " << NC << " constants of type '"
85 // << Plane.front()->getType()->getName() << "' = Slot #" << Slot << "\n";
86
87 for (unsigned i = ValNo; i < ValNo+NC; ++i) {
88 const Value *V = Plane[i];
89 if (const Constant *CPV = dyn_cast<Constant>(V)) {
90 //cerr << "Serializing value: <" << V->getType() << ">: " << V << ":"
91 // << Out.size() << "\n";
92 outputConstant(CPV);
93 } else {
94 outputType(cast<const Type>(V));
95 }
96 }
97}
98
Chris Lattner79df7c02002-03-26 18:01:55 +000099void BytecodeWriter::outputConstants(bool isFunction) {
Chris Lattnere8fdde12001-09-07 16:39:41 +0000100 BytecodeBlock CPool(BytecodeFormat::ConstantPool, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000101
102 unsigned NumPlanes = Table.getNumPlanes();
Vikram S. Advea7dac3d2002-07-14 23:07:51 +0000103
104 // Write the type plane for types first because earlier planes
105 // (e.g. for a primitive type like float) may have constants constructed
106 // using types coming later (e.g., via getelementptr from a pointer type).
107 // The type plane is needed before types can be fwd or bkwd referenced.
108 if (!isFunction) {
109 const std::vector<const Value*> &Plane = Table.getPlane(Type::TypeTyID);
110 assert(!Plane.empty() && "No types at all?");
111 unsigned ValNo = Type::FirstDerivedTyID; // Start at the derived types...
112 outputConstantsInPlane(Plane, ValNo); // Write out the types
113 }
114
Chris Lattner00950542001-06-06 20:29:01 +0000115 for (unsigned pno = 0; pno < NumPlanes; pno++) {
Chris Lattner697954c2002-01-20 22:54:45 +0000116 const std::vector<const Value*> &Plane = Table.getPlane(pno);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000117 if (Plane.empty()) continue; // Skip empty type planes...
Vikram S. Advea7dac3d2002-07-14 23:07:51 +0000118
Chris Lattnere8fdde12001-09-07 16:39:41 +0000119 unsigned ValNo = 0;
Chris Lattner79df7c02002-03-26 18:01:55 +0000120 if (isFunction) // Don't reemit module constants
Chris Lattnere8fdde12001-09-07 16:39:41 +0000121 ValNo = Table.getModuleLevel(pno);
122 else if (pno == Type::TypeTyID)
Vikram S. Advea7dac3d2002-07-14 23:07:51 +0000123 continue; // Type plane was written out above
Chris Lattner00950542001-06-06 20:29:01 +0000124
Vikram S. Advea7dac3d2002-07-14 23:07:51 +0000125 outputConstantsInPlane(Plane, ValNo); // Write out constants in the plane
Chris Lattner00950542001-06-06 20:29:01 +0000126 }
Chris Lattner00950542001-06-06 20:29:01 +0000127}
128
129void BytecodeWriter::outputModuleInfoBlock(const Module *M) {
130 BytecodeBlock ModuleInfoBlock(BytecodeFormat::ModuleGlobalInfo, Out);
131
Chris Lattner70cc3392001-09-10 07:58:01 +0000132 // Output the types for the global variables in the module...
133 for (Module::const_giterator I = M->gbegin(), End = M->gend(); I != End;++I) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000134 int Slot = Table.getValSlot(I->getType());
Chris Lattner70cc3392001-09-10 07:58:01 +0000135 assert(Slot != -1 && "Module global vars is broken!");
Chris Lattnerd70684f2001-09-18 04:01:05 +0000136
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000137 // Fields: bit0 = isConstant, bit1 = hasInitializer, bit2=InternalLinkage,
138 // bit3+ = slot#
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000139 unsigned oSlot = ((unsigned)Slot << 3) | (I->hasInternalLinkage() << 2) |
140 (I->hasInitializer() << 1) | I->isConstant();
Chris Lattnerd70684f2001-09-18 04:01:05 +0000141 output_vbr(oSlot, Out);
142
Chris Lattner1b98c5c2001-10-13 06:48:38 +0000143 // If we have an initializer, output it now.
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000144 if (I->hasInitializer()) {
145 Slot = Table.getValSlot((Value*)I->getInitializer());
Chris Lattnerd70684f2001-09-18 04:01:05 +0000146 assert(Slot != -1 && "No slot for global var initializer!");
147 output_vbr((unsigned)Slot, Out);
148 }
Chris Lattner70cc3392001-09-10 07:58:01 +0000149 }
150 output_vbr((unsigned)Table.getValSlot(Type::VoidTy), Out);
151
Chris Lattnerb5794002002-04-07 22:49:37 +0000152 // Output the types of the functions in this module...
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000153 for (Module::const_iterator I = M->begin(), End = M->end(); I != End; ++I) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000154 int Slot = Table.getValSlot(I->getType());
Chris Lattner00950542001-06-06 20:29:01 +0000155 assert(Slot != -1 && "Module const pool is broken!");
156 assert(Slot >= Type::FirstDerivedTyID && "Derived type not in range!");
157 output_vbr((unsigned)Slot, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000158 }
159 output_vbr((unsigned)Table.getValSlot(Type::VoidTy), Out);
Chris Lattner70cc3392001-09-10 07:58:01 +0000160
161
Chris Lattner00950542001-06-06 20:29:01 +0000162 align32(Out);
163}
164
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000165void BytecodeWriter::processMethod(const Function *F) {
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000166 BytecodeBlock FunctionBlock(BytecodeFormat::Function, Out);
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000167 output_vbr((unsigned)F->hasInternalLinkage(), Out);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000168 // Only output the constant pool and other goodies if needed...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000169 if (!F->isExternal()) {
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000170
Chris Lattnerb5794002002-04-07 22:49:37 +0000171 // Get slot information about the function...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000172 Table.incorporateFunction(F);
Chris Lattner00950542001-06-06 20:29:01 +0000173
Chris Lattnerb5794002002-04-07 22:49:37 +0000174 // Output information about the constants in the function...
Chris Lattnere8fdde12001-09-07 16:39:41 +0000175 outputConstants(true);
Chris Lattner00950542001-06-06 20:29:01 +0000176
Chris Lattnere8fdde12001-09-07 16:39:41 +0000177 // Output basic block nodes...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000178 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
179 processBasicBlock(*I);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000180
Chris Lattnerb5794002002-04-07 22:49:37 +0000181 // If needed, output the symbol table for the function...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000182 if (F->hasSymbolTable())
183 outputSymbolTable(*F->getSymbolTable());
Chris Lattnere8fdde12001-09-07 16:39:41 +0000184
Chris Lattnerb5794002002-04-07 22:49:37 +0000185 Table.purgeFunction();
Chris Lattnere8fdde12001-09-07 16:39:41 +0000186 }
Chris Lattner00950542001-06-06 20:29:01 +0000187}
188
189
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000190void BytecodeWriter::processBasicBlock(const BasicBlock &BB) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000191 BytecodeBlock FunctionBlock(BytecodeFormat::BasicBlock, Out);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000192 // Process all the instructions in the bb...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000193 for(BasicBlock::const_iterator I = BB.begin(), E = BB.end(); I != E; ++I)
194 processInstruction(*I);
Chris Lattner00950542001-06-06 20:29:01 +0000195}
196
197void BytecodeWriter::outputSymbolTable(const SymbolTable &MST) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000198 BytecodeBlock FunctionBlock(BytecodeFormat::SymbolTable, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000199
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000200 for (SymbolTable::const_iterator TI = MST.begin(); TI != MST.end(); ++TI) {
Chris Lattner00950542001-06-06 20:29:01 +0000201 SymbolTable::type_const_iterator I = MST.type_begin(TI->first);
202 SymbolTable::type_const_iterator End = MST.type_end(TI->first);
203 int Slot;
204
205 if (I == End) continue; // Don't mess with an absent type...
206
207 // Symtab block header: [num entries][type id number]
208 output_vbr(MST.type_size(TI->first), Out);
209
210 Slot = Table.getValSlot(TI->first);
211 assert(Slot != -1 && "Type in symtab, but not in table!");
212 output_vbr((unsigned)Slot, Out);
213
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000214 for (; I != End; ++I) {
Chris Lattner00950542001-06-06 20:29:01 +0000215 // Symtab entry: [def slot #][name]
216 Slot = Table.getValSlot(I->second);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000217 assert(Slot != -1 && "Value in symtab but has no slot number!!");
Chris Lattner00950542001-06-06 20:29:01 +0000218 output_vbr((unsigned)Slot, Out);
219 output(I->first, Out, false); // Don't force alignment...
220 }
221 }
222}
223
Anand Shuklaeea60fc2002-06-25 20:44:04 +0000224void WriteBytecodeToFile(const Module *C, std::ostream &Out) {
Chris Lattnere8fdde12001-09-07 16:39:41 +0000225 assert(C && "You can't write a null module!!");
Chris Lattner00950542001-06-06 20:29:01 +0000226
Chris Lattner697954c2002-01-20 22:54:45 +0000227 std::deque<unsigned char> Buffer;
Chris Lattner00950542001-06-06 20:29:01 +0000228
229 // This object populates buffer for us...
230 BytecodeWriter BCW(Buffer, C);
231
Chris Lattnere8fdde12001-09-07 16:39:41 +0000232 // Okay, write the deque out to the ostream now... the deque is not
233 // sequential in memory, however, so write out as much as possible in big
234 // chunks, until we're done.
235 //
Chris Lattner697954c2002-01-20 22:54:45 +0000236 std::deque<unsigned char>::const_iterator I = Buffer.begin(),E = Buffer.end();
Chris Lattnere8fdde12001-09-07 16:39:41 +0000237 while (I != E) { // Loop until it's all written
238 // Scan to see how big this chunk is...
239 const unsigned char *ChunkPtr = &*I;
240 const unsigned char *LastPtr = ChunkPtr;
241 while (I != E) {
242 const unsigned char *ThisPtr = &*++I;
Chris Lattner5cb17412001-11-04 21:32:41 +0000243 if (LastPtr+1 != ThisPtr) { // Advanced by more than a byte of memory?
244 ++LastPtr;
245 break;
246 }
Chris Lattnere8fdde12001-09-07 16:39:41 +0000247 LastPtr = ThisPtr;
248 }
249
250 // Write out the chunk...
Chris Lattner697954c2002-01-20 22:54:45 +0000251 Out.write((char*)ChunkPtr, LastPtr-ChunkPtr);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000252 }
253
Chris Lattner00950542001-06-06 20:29:01 +0000254 Out.flush();
255}