blob: 145004b716dabdeb9c68a5488e263935965f4c66 [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 Lattner70cc3392001-09-10 07:58:01 +000024#include "llvm/GlobalVariable.h"
Chris Lattner79df7c02002-03-26 18:01:55 +000025#include "llvm/Function.h"
Chris Lattner00950542001-06-06 20:29:01 +000026#include "llvm/BasicBlock.h"
Chris Lattner00950542001-06-06 20:29:01 +000027#include "llvm/SymbolTable.h"
28#include "llvm/DerivedTypes.h"
Chris Lattnerf60dc882001-11-29 16:32:16 +000029#include "Support/STLExtras.h"
Chris Lattner00950542001-06-06 20:29:01 +000030#include <string.h>
31#include <algorithm>
32
Chris Lattner697954c2002-01-20 22:54:45 +000033BytecodeWriter::BytecodeWriter(std::deque<unsigned char> &o, const Module *M)
Chris Lattner00950542001-06-06 20:29:01 +000034 : Out(o), Table(M, false) {
35
36 outputSignature();
37
38 // Emit the top level CLASS block.
39 BytecodeBlock ModuleBlock(BytecodeFormat::Module, Out);
40
Chris Lattnere8fdde12001-09-07 16:39:41 +000041 // Output the ID of first "derived" type:
Chris Lattner00950542001-06-06 20:29:01 +000042 output_vbr((unsigned)Type::FirstDerivedTyID, Out);
43 align32(Out);
44
Chris Lattnerb5794002002-04-07 22:49:37 +000045 // Output module level constants, including types used by the function protos
Chris Lattnere8fdde12001-09-07 16:39:41 +000046 outputConstants(false);
Chris Lattner00950542001-06-06 20:29:01 +000047
Chris Lattnere8fdde12001-09-07 16:39:41 +000048 // The ModuleInfoBlock follows directly after the Module constant pool
49 outputModuleInfoBlock(M);
50
Chris Lattnerb5794002002-04-07 22:49:37 +000051 // Do the whole module now! Process each function at a time...
Chris Lattnere8fdde12001-09-07 16:39:41 +000052 for_each(M->begin(), M->end(),
53 bind_obj(this, &BytecodeWriter::processMethod));
54
55 // If needed, output the symbol table for the module...
Chris Lattner00950542001-06-06 20:29:01 +000056 if (M->hasSymbolTable())
57 outputSymbolTable(*M->getSymbolTable());
58}
59
Chris Lattner79df7c02002-03-26 18:01:55 +000060void BytecodeWriter::outputConstants(bool isFunction) {
Chris Lattnere8fdde12001-09-07 16:39:41 +000061 BytecodeBlock CPool(BytecodeFormat::ConstantPool, Out);
Chris Lattner00950542001-06-06 20:29:01 +000062
63 unsigned NumPlanes = Table.getNumPlanes();
Chris Lattner00950542001-06-06 20:29:01 +000064 for (unsigned pno = 0; pno < NumPlanes; pno++) {
Chris Lattner697954c2002-01-20 22:54:45 +000065 const std::vector<const Value*> &Plane = Table.getPlane(pno);
Chris Lattnere8fdde12001-09-07 16:39:41 +000066 if (Plane.empty()) continue; // Skip empty type planes...
Chris Lattner00950542001-06-06 20:29:01 +000067
Chris Lattnere8fdde12001-09-07 16:39:41 +000068 unsigned ValNo = 0;
Chris Lattner79df7c02002-03-26 18:01:55 +000069 if (isFunction) // Don't reemit module constants
Chris Lattnere8fdde12001-09-07 16:39:41 +000070 ValNo = Table.getModuleLevel(pno);
71 else if (pno == Type::TypeTyID)
72 ValNo = Type::FirstDerivedTyID; // Start emitting at the derived types...
Chris Lattner00950542001-06-06 20:29:01 +000073
Chris Lattnerb5794002002-04-07 22:49:37 +000074 // Scan through and ignore function arguments...
Chris Lattner73e21422002-04-09 19:48:49 +000075 for (; ValNo < Plane.size() && isa<Argument>(Plane[ValNo]); ValNo++)
Chris Lattnere8fdde12001-09-07 16:39:41 +000076 /*empty*/;
Chris Lattner00950542001-06-06 20:29:01 +000077
Chris Lattnere8fdde12001-09-07 16:39:41 +000078 unsigned NC = ValNo; // Number of constants
79 for (; NC < Plane.size() &&
Chris Lattner31bcdb82002-04-28 19:55:58 +000080 (isa<Constant>(Plane[NC]) || isa<Type>(Plane[NC])); NC++)
81 /*empty*/;
Chris Lattnere8fdde12001-09-07 16:39:41 +000082 NC -= ValNo; // Convert from index into count
83 if (NC == 0) continue; // Skip empty type planes...
Chris Lattner00950542001-06-06 20:29:01 +000084
85 // Output type header: [num entries][type id number]
86 //
Chris Lattnere8fdde12001-09-07 16:39:41 +000087 output_vbr(NC, Out);
Chris Lattner00950542001-06-06 20:29:01 +000088
89 // Output the Type ID Number...
90 int Slot = Table.getValSlot(Plane.front()->getType());
Chris Lattnerb5794002002-04-07 22:49:37 +000091 assert (Slot != -1 && "Type in constant pool but not in function!!");
Chris Lattner00950542001-06-06 20:29:01 +000092 output_vbr((unsigned)Slot, Out);
93
Chris Lattner697954c2002-01-20 22:54:45 +000094 //cerr << "Emitting " << NC << " constants of type '"
95 // << Plane.front()->getType()->getName() << "' = Slot #" << Slot << "\n";
Chris Lattner00950542001-06-06 20:29:01 +000096
Chris Lattnere8fdde12001-09-07 16:39:41 +000097 for (unsigned i = ValNo; i < ValNo+NC; ++i) {
98 const Value *V = Plane[i];
Chris Lattnere9bb2df2001-12-03 22:26:30 +000099 if (const Constant *CPV = dyn_cast<Constant>(V)) {
Chris Lattnerccc25962002-04-18 18:14:56 +0000100 //cerr << "Serializing value: <" << V->getType() << ">: " << V << ":"
Chris Lattner00950542001-06-06 20:29:01 +0000101 // << Out.size() << "\n";
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000102 outputConstant(CPV);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000103 } else {
Chris Lattner1b98c5c2001-10-13 06:48:38 +0000104 outputType(cast<const Type>(V));
Chris Lattner00950542001-06-06 20:29:01 +0000105 }
106 }
107 }
Chris Lattner00950542001-06-06 20:29:01 +0000108}
109
110void BytecodeWriter::outputModuleInfoBlock(const Module *M) {
111 BytecodeBlock ModuleInfoBlock(BytecodeFormat::ModuleGlobalInfo, Out);
112
Chris Lattner70cc3392001-09-10 07:58:01 +0000113 // Output the types for the global variables in the module...
114 for (Module::const_giterator I = M->gbegin(), End = M->gend(); I != End;++I) {
Chris Lattnerd70684f2001-09-18 04:01:05 +0000115 const GlobalVariable *GV = *I;
116 int Slot = Table.getValSlot(GV->getType());
Chris Lattner70cc3392001-09-10 07:58:01 +0000117 assert(Slot != -1 && "Module global vars is broken!");
Chris Lattnerd70684f2001-09-18 04:01:05 +0000118
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000119 // Fields: bit0 = isConstant, bit1 = hasInitializer, bit2=InternalLinkage,
120 // bit3+ = slot#
121 unsigned oSlot = ((unsigned)Slot << 3) | (GV->hasInternalLinkage() << 2) |
122 (GV->hasInitializer() << 1) | GV->isConstant();
Chris Lattnerd70684f2001-09-18 04:01:05 +0000123 output_vbr(oSlot, Out);
124
Chris Lattner1b98c5c2001-10-13 06:48:38 +0000125 // If we have an initializer, output it now.
Chris Lattnerd70684f2001-09-18 04:01:05 +0000126 if (GV->hasInitializer()) {
Chris Lattner31bcdb82002-04-28 19:55:58 +0000127 Slot = Table.getValSlot((Value*)GV->getInitializer());
Chris Lattnerd70684f2001-09-18 04:01:05 +0000128 assert(Slot != -1 && "No slot for global var initializer!");
129 output_vbr((unsigned)Slot, Out);
130 }
Chris Lattner70cc3392001-09-10 07:58:01 +0000131 }
132 output_vbr((unsigned)Table.getValSlot(Type::VoidTy), Out);
133
Chris Lattnerb5794002002-04-07 22:49:37 +0000134 // Output the types of the functions in this module...
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000135 for (Module::const_iterator I = M->begin(), End = M->end(); I != End; ++I) {
Chris Lattner00950542001-06-06 20:29:01 +0000136 int Slot = Table.getValSlot((*I)->getType());
137 assert(Slot != -1 && "Module const pool is broken!");
138 assert(Slot >= Type::FirstDerivedTyID && "Derived type not in range!");
139 output_vbr((unsigned)Slot, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000140 }
141 output_vbr((unsigned)Table.getValSlot(Type::VoidTy), Out);
Chris Lattner70cc3392001-09-10 07:58:01 +0000142
143
Chris Lattner00950542001-06-06 20:29:01 +0000144 align32(Out);
145}
146
Chris Lattner79df7c02002-03-26 18:01:55 +0000147void BytecodeWriter::processMethod(const Function *M) {
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000148 BytecodeBlock FunctionBlock(BytecodeFormat::Function, Out);
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000149 output_vbr((unsigned)M->hasInternalLinkage(), Out);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000150 // Only output the constant pool and other goodies if needed...
151 if (!M->isExternal()) {
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000152
Chris Lattnerb5794002002-04-07 22:49:37 +0000153 // Get slot information about the function...
154 Table.incorporateFunction(M);
Chris Lattner00950542001-06-06 20:29:01 +0000155
Chris Lattnerb5794002002-04-07 22:49:37 +0000156 // Output information about the constants in the function...
Chris Lattnere8fdde12001-09-07 16:39:41 +0000157 outputConstants(true);
Chris Lattner00950542001-06-06 20:29:01 +0000158
Chris Lattnere8fdde12001-09-07 16:39:41 +0000159 // Output basic block nodes...
160 for_each(M->begin(), M->end(),
161 bind_obj(this, &BytecodeWriter::processBasicBlock));
162
Chris Lattnerb5794002002-04-07 22:49:37 +0000163 // If needed, output the symbol table for the function...
Chris Lattnere8fdde12001-09-07 16:39:41 +0000164 if (M->hasSymbolTable())
165 outputSymbolTable(*M->getSymbolTable());
166
Chris Lattnerb5794002002-04-07 22:49:37 +0000167 Table.purgeFunction();
Chris Lattnere8fdde12001-09-07 16:39:41 +0000168 }
Chris Lattner00950542001-06-06 20:29:01 +0000169}
170
171
Chris Lattnere8fdde12001-09-07 16:39:41 +0000172void BytecodeWriter::processBasicBlock(const BasicBlock *BB) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000173 BytecodeBlock FunctionBlock(BytecodeFormat::BasicBlock, Out);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000174 // Process all the instructions in the bb...
175 for_each(BB->begin(), BB->end(),
176 bind_obj(this, &BytecodeWriter::processInstruction));
Chris Lattner00950542001-06-06 20:29:01 +0000177}
178
179void BytecodeWriter::outputSymbolTable(const SymbolTable &MST) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000180 BytecodeBlock FunctionBlock(BytecodeFormat::SymbolTable, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000181
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000182 for (SymbolTable::const_iterator TI = MST.begin(); TI != MST.end(); ++TI) {
Chris Lattner00950542001-06-06 20:29:01 +0000183 SymbolTable::type_const_iterator I = MST.type_begin(TI->first);
184 SymbolTable::type_const_iterator End = MST.type_end(TI->first);
185 int Slot;
186
187 if (I == End) continue; // Don't mess with an absent type...
188
189 // Symtab block header: [num entries][type id number]
190 output_vbr(MST.type_size(TI->first), Out);
191
192 Slot = Table.getValSlot(TI->first);
193 assert(Slot != -1 && "Type in symtab, but not in table!");
194 output_vbr((unsigned)Slot, Out);
195
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000196 for (; I != End; ++I) {
Chris Lattner00950542001-06-06 20:29:01 +0000197 // Symtab entry: [def slot #][name]
198 Slot = Table.getValSlot(I->second);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000199 assert(Slot != -1 && "Value in symtab but has no slot number!!");
Chris Lattner00950542001-06-06 20:29:01 +0000200 output_vbr((unsigned)Slot, Out);
201 output(I->first, Out, false); // Don't force alignment...
202 }
203 }
204}
205
206void WriteBytecodeToFile(const Module *C, ostream &Out) {
Chris Lattnere8fdde12001-09-07 16:39:41 +0000207 assert(C && "You can't write a null module!!");
Chris Lattner00950542001-06-06 20:29:01 +0000208
Chris Lattner697954c2002-01-20 22:54:45 +0000209 std::deque<unsigned char> Buffer;
Chris Lattner00950542001-06-06 20:29:01 +0000210
211 // This object populates buffer for us...
212 BytecodeWriter BCW(Buffer, C);
213
Chris Lattnere8fdde12001-09-07 16:39:41 +0000214 // Okay, write the deque out to the ostream now... the deque is not
215 // sequential in memory, however, so write out as much as possible in big
216 // chunks, until we're done.
217 //
Chris Lattner697954c2002-01-20 22:54:45 +0000218 std::deque<unsigned char>::const_iterator I = Buffer.begin(),E = Buffer.end();
Chris Lattnere8fdde12001-09-07 16:39:41 +0000219 while (I != E) { // Loop until it's all written
220 // Scan to see how big this chunk is...
221 const unsigned char *ChunkPtr = &*I;
222 const unsigned char *LastPtr = ChunkPtr;
223 while (I != E) {
224 const unsigned char *ThisPtr = &*++I;
Chris Lattner5cb17412001-11-04 21:32:41 +0000225 if (LastPtr+1 != ThisPtr) { // Advanced by more than a byte of memory?
226 ++LastPtr;
227 break;
228 }
Chris Lattnere8fdde12001-09-07 16:39:41 +0000229 LastPtr = ThisPtr;
230 }
231
232 // Write out the chunk...
Chris Lattner697954c2002-01-20 22:54:45 +0000233 Out.write((char*)ChunkPtr, LastPtr-ChunkPtr);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000234 }
235
Chris Lattner00950542001-06-06 20:29:01 +0000236 Out.flush();
237}