blob: f8d94890f066cf869521252b4226ee29105ea912 [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"
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 Lattner76e38962003-04-22 18:15:10 +000046 bool isBigEndian = M->isBigEndian();
47 bool hasLongPointers = M->has64BitPointers();
Chris Lattner186a1f72003-03-19 20:56:46 +000048
Chris Lattnere3869c82003-04-16 21:16:05 +000049 // Output the version identifier... we are currently on bytecode version #2
50 unsigned Version = (2 << 4) | isBigEndian | (hasLongPointers << 1);
Chris Lattner186a1f72003-03-19 20:56:46 +000051 output_vbr(Version, Out);
Chris Lattner00950542001-06-06 20:29:01 +000052 align32(Out);
53
Chris Lattner186a1f72003-03-19 20:56:46 +000054 {
55 BytecodeBlock CPool(BytecodeFormat::GlobalTypePlane, Out);
56
57 // Write the type plane for types first because earlier planes (e.g. for a
58 // primitive type like float) may have constants constructed using types
59 // coming later (e.g., via getelementptr from a pointer type). The type
60 // plane is needed before types can be fwd or bkwd referenced.
61 const std::vector<const Value*> &Plane = Table.getPlane(Type::TypeTyID);
62 assert(!Plane.empty() && "No types at all?");
63 unsigned ValNo = Type::FirstDerivedTyID; // Start at the derived types...
64 outputConstantsInPlane(Plane, ValNo); // Write out the types
65 }
Chris Lattner00950542001-06-06 20:29:01 +000066
Chris Lattner186a1f72003-03-19 20:56:46 +000067 // The ModuleInfoBlock follows directly after the type information
Chris Lattnere8fdde12001-09-07 16:39:41 +000068 outputModuleInfoBlock(M);
69
Chris Lattner186a1f72003-03-19 20:56:46 +000070 // Output module level constants, used for global variable initializers
71 outputConstants(false);
72
Chris Lattnerb5794002002-04-07 22:49:37 +000073 // Do the whole module now! Process each function at a time...
Chris Lattner0b12b5f2002-06-25 16:13:21 +000074 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
Chris Lattner186a1f72003-03-19 20:56:46 +000075 outputFunction(I);
Chris Lattnere8fdde12001-09-07 16:39:41 +000076
77 // If needed, output the symbol table for the module...
Chris Lattner6e6026b2002-11-20 18:36:02 +000078 outputSymbolTable(M->getSymbolTable());
Chris Lattner00950542001-06-06 20:29:01 +000079}
80
Vikram S. Advea7dac3d2002-07-14 23:07:51 +000081// Helper function for outputConstants().
82// Writes out all the constants in the plane Plane starting at entry StartNo.
83//
84void BytecodeWriter::outputConstantsInPlane(const std::vector<const Value*>
85 &Plane, unsigned StartNo) {
86 unsigned ValNo = StartNo;
87
Chris Lattner186a1f72003-03-19 20:56:46 +000088 // Scan through and ignore function arguments/global values...
89 for (; ValNo < Plane.size() && (isa<Argument>(Plane[ValNo]) ||
90 isa<GlobalValue>(Plane[ValNo])); ValNo++)
Vikram S. Advea7dac3d2002-07-14 23:07:51 +000091 /*empty*/;
92
93 unsigned NC = ValNo; // Number of constants
94 for (; NC < Plane.size() &&
95 (isa<Constant>(Plane[NC]) || isa<Type>(Plane[NC])); NC++)
96 /*empty*/;
97 NC -= ValNo; // Convert from index into count
98 if (NC == 0) return; // Skip empty type planes...
99
100 // Output type header: [num entries][type id number]
101 //
102 output_vbr(NC, Out);
103
104 // Output the Type ID Number...
105 int Slot = Table.getValSlot(Plane.front()->getType());
106 assert (Slot != -1 && "Type in constant pool but not in function!!");
107 output_vbr((unsigned)Slot, Out);
108
109 //cerr << "Emitting " << NC << " constants of type '"
110 // << Plane.front()->getType()->getName() << "' = Slot #" << Slot << "\n";
111
112 for (unsigned i = ValNo; i < ValNo+NC; ++i) {
113 const Value *V = Plane[i];
114 if (const Constant *CPV = dyn_cast<Constant>(V)) {
115 //cerr << "Serializing value: <" << V->getType() << ">: " << V << ":"
116 // << Out.size() << "\n";
117 outputConstant(CPV);
118 } else {
Chris Lattner186a1f72003-03-19 20:56:46 +0000119 outputType(cast<Type>(V));
Vikram S. Advea7dac3d2002-07-14 23:07:51 +0000120 }
121 }
122}
123
Chris Lattner79df7c02002-03-26 18:01:55 +0000124void BytecodeWriter::outputConstants(bool isFunction) {
Chris Lattnere8fdde12001-09-07 16:39:41 +0000125 BytecodeBlock CPool(BytecodeFormat::ConstantPool, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000126
127 unsigned NumPlanes = Table.getNumPlanes();
Chris Lattnerf69315b2003-05-22 18:35:38 +0000128
129 // Output the type plane before any constants!
130 if (isFunction && NumPlanes > Type::TypeTyID) {
131 const std::vector<const Value*> &Plane = Table.getPlane(Type::TypeTyID);
Chris Lattner186a1f72003-03-19 20:56:46 +0000132 if (!Plane.empty()) { // Skip empty type planes...
Chris Lattnerf69315b2003-05-22 18:35:38 +0000133 unsigned ValNo = Table.getModuleLevel(Type::TypeTyID);
134 outputConstantsInPlane(Plane, ValNo);
Chris Lattner6463e0d2002-10-14 03:34:17 +0000135 }
Chris Lattner00950542001-06-06 20:29:01 +0000136 }
Chris Lattnerf69315b2003-05-22 18:35:38 +0000137
138 for (unsigned pno = 0; pno != NumPlanes; pno++)
139 if (pno != Type::TypeTyID) { // Type plane handled above.
140 const std::vector<const Value*> &Plane = Table.getPlane(pno);
141 if (!Plane.empty()) { // Skip empty type planes...
142 unsigned ValNo = 0;
143 if (isFunction) // Don't reemit module constants
144 ValNo += Table.getModuleLevel(pno);
145
146 if (pno >= Type::FirstDerivedTyID) {
147 // Skip zero initializer
148 if (ValNo == 0)
149 ValNo = 1;
150 }
151
152 // Write out constants in the plane
153 outputConstantsInPlane(Plane, ValNo);
154 }
155 }
Chris Lattner00950542001-06-06 20:29:01 +0000156}
157
158void BytecodeWriter::outputModuleInfoBlock(const Module *M) {
159 BytecodeBlock ModuleInfoBlock(BytecodeFormat::ModuleGlobalInfo, Out);
160
Chris Lattner70cc3392001-09-10 07:58:01 +0000161 // Output the types for the global variables in the module...
162 for (Module::const_giterator I = M->gbegin(), End = M->gend(); I != End;++I) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000163 int Slot = Table.getValSlot(I->getType());
Chris Lattner70cc3392001-09-10 07:58:01 +0000164 assert(Slot != -1 && "Module global vars is broken!");
Chris Lattnerd70684f2001-09-18 04:01:05 +0000165
Chris Lattnere3869c82003-04-16 21:16:05 +0000166 // Fields: bit0 = isConstant, bit1 = hasInitializer, bit2,3=Linkage,
167 // bit4+ = Slot # for type
168 unsigned oSlot = ((unsigned)Slot << 4) | ((unsigned)I->getLinkage() << 2) |
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000169 (I->hasInitializer() << 1) | I->isConstant();
Chris Lattnerd70684f2001-09-18 04:01:05 +0000170 output_vbr(oSlot, Out);
171
Chris Lattner1b98c5c2001-10-13 06:48:38 +0000172 // If we have an initializer, output it now.
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000173 if (I->hasInitializer()) {
174 Slot = Table.getValSlot((Value*)I->getInitializer());
Chris Lattnerd70684f2001-09-18 04:01:05 +0000175 assert(Slot != -1 && "No slot for global var initializer!");
176 output_vbr((unsigned)Slot, Out);
177 }
Chris Lattner70cc3392001-09-10 07:58:01 +0000178 }
179 output_vbr((unsigned)Table.getValSlot(Type::VoidTy), Out);
180
Chris Lattnerb5794002002-04-07 22:49:37 +0000181 // Output the types of the functions in this module...
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000182 for (Module::const_iterator I = M->begin(), End = M->end(); I != End; ++I) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000183 int Slot = Table.getValSlot(I->getType());
Chris Lattner00950542001-06-06 20:29:01 +0000184 assert(Slot != -1 && "Module const pool is broken!");
185 assert(Slot >= Type::FirstDerivedTyID && "Derived type not in range!");
186 output_vbr((unsigned)Slot, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000187 }
188 output_vbr((unsigned)Table.getValSlot(Type::VoidTy), Out);
Chris Lattner70cc3392001-09-10 07:58:01 +0000189
Chris Lattner00950542001-06-06 20:29:01 +0000190 align32(Out);
191}
192
Chris Lattner186a1f72003-03-19 20:56:46 +0000193void BytecodeWriter::outputFunction(const Function *F) {
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000194 BytecodeBlock FunctionBlock(BytecodeFormat::Function, Out);
Chris Lattnere3869c82003-04-16 21:16:05 +0000195 output_vbr((unsigned)F->getLinkage(), Out);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000196 // Only output the constant pool and other goodies if needed...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000197 if (!F->isExternal()) {
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000198
Chris Lattnerb5794002002-04-07 22:49:37 +0000199 // Get slot information about the function...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000200 Table.incorporateFunction(F);
Chris Lattner00950542001-06-06 20:29:01 +0000201
Chris Lattnerb5794002002-04-07 22:49:37 +0000202 // Output information about the constants in the function...
Chris Lattnere8fdde12001-09-07 16:39:41 +0000203 outputConstants(true);
Chris Lattner00950542001-06-06 20:29:01 +0000204
Chris Lattnere8fdde12001-09-07 16:39:41 +0000205 // Output basic block nodes...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000206 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
207 processBasicBlock(*I);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000208
Chris Lattnerb5794002002-04-07 22:49:37 +0000209 // If needed, output the symbol table for the function...
Chris Lattner6e6026b2002-11-20 18:36:02 +0000210 outputSymbolTable(F->getSymbolTable());
Chris Lattnere8fdde12001-09-07 16:39:41 +0000211
Chris Lattnerb5794002002-04-07 22:49:37 +0000212 Table.purgeFunction();
Chris Lattnere8fdde12001-09-07 16:39:41 +0000213 }
Chris Lattner00950542001-06-06 20:29:01 +0000214}
215
216
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000217void BytecodeWriter::processBasicBlock(const BasicBlock &BB) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000218 BytecodeBlock FunctionBlock(BytecodeFormat::BasicBlock, Out);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000219 // Process all the instructions in the bb...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000220 for(BasicBlock::const_iterator I = BB.begin(), E = BB.end(); I != E; ++I)
221 processInstruction(*I);
Chris Lattner00950542001-06-06 20:29:01 +0000222}
223
224void BytecodeWriter::outputSymbolTable(const SymbolTable &MST) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000225 BytecodeBlock FunctionBlock(BytecodeFormat::SymbolTable, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000226
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000227 for (SymbolTable::const_iterator TI = MST.begin(); TI != MST.end(); ++TI) {
Chris Lattner00950542001-06-06 20:29:01 +0000228 SymbolTable::type_const_iterator I = MST.type_begin(TI->first);
229 SymbolTable::type_const_iterator End = MST.type_end(TI->first);
230 int Slot;
231
232 if (I == End) continue; // Don't mess with an absent type...
233
234 // Symtab block header: [num entries][type id number]
235 output_vbr(MST.type_size(TI->first), Out);
236
237 Slot = Table.getValSlot(TI->first);
238 assert(Slot != -1 && "Type in symtab, but not in table!");
239 output_vbr((unsigned)Slot, Out);
240
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000241 for (; I != End; ++I) {
Chris Lattner00950542001-06-06 20:29:01 +0000242 // Symtab entry: [def slot #][name]
243 Slot = Table.getValSlot(I->second);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000244 assert(Slot != -1 && "Value in symtab but has no slot number!!");
Chris Lattner00950542001-06-06 20:29:01 +0000245 output_vbr((unsigned)Slot, Out);
246 output(I->first, Out, false); // Don't force alignment...
247 }
248 }
249}
250
Anand Shuklaeea60fc2002-06-25 20:44:04 +0000251void WriteBytecodeToFile(const Module *C, std::ostream &Out) {
Chris Lattnere8fdde12001-09-07 16:39:41 +0000252 assert(C && "You can't write a null module!!");
Chris Lattner00950542001-06-06 20:29:01 +0000253
Chris Lattner697954c2002-01-20 22:54:45 +0000254 std::deque<unsigned char> Buffer;
Chris Lattner00950542001-06-06 20:29:01 +0000255
256 // This object populates buffer for us...
257 BytecodeWriter BCW(Buffer, C);
258
Chris Lattnerce6ef112002-07-26 18:40:14 +0000259 // Keep track of how much we've written...
260 BytesWritten += Buffer.size();
261
Chris Lattnere8fdde12001-09-07 16:39:41 +0000262 // Okay, write the deque out to the ostream now... the deque is not
263 // sequential in memory, however, so write out as much as possible in big
264 // chunks, until we're done.
265 //
Chris Lattner697954c2002-01-20 22:54:45 +0000266 std::deque<unsigned char>::const_iterator I = Buffer.begin(),E = Buffer.end();
Chris Lattnere8fdde12001-09-07 16:39:41 +0000267 while (I != E) { // Loop until it's all written
268 // Scan to see how big this chunk is...
269 const unsigned char *ChunkPtr = &*I;
270 const unsigned char *LastPtr = ChunkPtr;
271 while (I != E) {
272 const unsigned char *ThisPtr = &*++I;
Chris Lattner5cb17412001-11-04 21:32:41 +0000273 if (LastPtr+1 != ThisPtr) { // Advanced by more than a byte of memory?
274 ++LastPtr;
275 break;
276 }
Chris Lattnere8fdde12001-09-07 16:39:41 +0000277 LastPtr = ThisPtr;
278 }
279
280 // Write out the chunk...
Chris Lattner697954c2002-01-20 22:54:45 +0000281 Out.write((char*)ChunkPtr, LastPtr-ChunkPtr);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000282 }
283
Chris Lattner00950542001-06-06 20:29:01 +0000284 Out.flush();
285}