blob: 2f427770d9083aca56340bc5b87720aefb6161f5 [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 Lattnere9bb2df2001-12-03 22:26:30 +000027#include "llvm/ConstantVals.h"
Chris Lattner00950542001-06-06 20:29:01 +000028#include "llvm/SymbolTable.h"
29#include "llvm/DerivedTypes.h"
Chris Lattnerf60dc882001-11-29 16:32:16 +000030#include "Support/STLExtras.h"
Chris Lattner00950542001-06-06 20:29:01 +000031#include <string.h>
32#include <algorithm>
33
Chris Lattner697954c2002-01-20 22:54:45 +000034BytecodeWriter::BytecodeWriter(std::deque<unsigned char> &o, const Module *M)
Chris Lattner00950542001-06-06 20:29:01 +000035 : Out(o), Table(M, false) {
36
37 outputSignature();
38
39 // Emit the top level CLASS block.
40 BytecodeBlock ModuleBlock(BytecodeFormat::Module, Out);
41
Chris Lattnere8fdde12001-09-07 16:39:41 +000042 // Output the ID of first "derived" type:
Chris Lattner00950542001-06-06 20:29:01 +000043 output_vbr((unsigned)Type::FirstDerivedTyID, Out);
44 align32(Out);
45
Chris Lattnerb5794002002-04-07 22:49:37 +000046 // Output module level constants, including types used by the function protos
Chris Lattnere8fdde12001-09-07 16:39:41 +000047 outputConstants(false);
Chris Lattner00950542001-06-06 20:29:01 +000048
Chris Lattnere8fdde12001-09-07 16:39:41 +000049 // The ModuleInfoBlock follows directly after the Module constant pool
50 outputModuleInfoBlock(M);
51
Chris Lattnerb5794002002-04-07 22:49:37 +000052 // Do the whole module now! Process each function at a time...
Chris Lattnere8fdde12001-09-07 16:39:41 +000053 for_each(M->begin(), M->end(),
54 bind_obj(this, &BytecodeWriter::processMethod));
55
56 // If needed, output the symbol table for the module...
Chris Lattner00950542001-06-06 20:29:01 +000057 if (M->hasSymbolTable())
58 outputSymbolTable(*M->getSymbolTable());
59}
60
Chris Lattner79df7c02002-03-26 18:01:55 +000061void BytecodeWriter::outputConstants(bool isFunction) {
Chris Lattnere8fdde12001-09-07 16:39:41 +000062 BytecodeBlock CPool(BytecodeFormat::ConstantPool, Out);
Chris Lattner00950542001-06-06 20:29:01 +000063
64 unsigned NumPlanes = Table.getNumPlanes();
Chris Lattner00950542001-06-06 20:29:01 +000065 for (unsigned pno = 0; pno < NumPlanes; pno++) {
Chris Lattner697954c2002-01-20 22:54:45 +000066 const std::vector<const Value*> &Plane = Table.getPlane(pno);
Chris Lattnere8fdde12001-09-07 16:39:41 +000067 if (Plane.empty()) continue; // Skip empty type planes...
Chris Lattner00950542001-06-06 20:29:01 +000068
Chris Lattnere8fdde12001-09-07 16:39:41 +000069 unsigned ValNo = 0;
Chris Lattner79df7c02002-03-26 18:01:55 +000070 if (isFunction) // Don't reemit module constants
Chris Lattnere8fdde12001-09-07 16:39:41 +000071 ValNo = Table.getModuleLevel(pno);
72 else if (pno == Type::TypeTyID)
73 ValNo = Type::FirstDerivedTyID; // Start emitting at the derived types...
Chris Lattner00950542001-06-06 20:29:01 +000074
Chris Lattnerb5794002002-04-07 22:49:37 +000075 // Scan through and ignore function arguments...
Chris Lattner73e21422002-04-09 19:48:49 +000076 for (; ValNo < Plane.size() && isa<Argument>(Plane[ValNo]); ValNo++)
Chris Lattnere8fdde12001-09-07 16:39:41 +000077 /*empty*/;
Chris Lattner00950542001-06-06 20:29:01 +000078
Chris Lattnere8fdde12001-09-07 16:39:41 +000079 unsigned NC = ValNo; // Number of constants
80 for (; NC < Plane.size() &&
Chris Lattnere9bb2df2001-12-03 22:26:30 +000081 (isa<Constant>(Plane[NC]) ||
Chris Lattnercfe26c92001-10-01 18:26:53 +000082 isa<Type>(Plane[NC])); NC++) /*empty*/;
Chris Lattnere8fdde12001-09-07 16:39:41 +000083 NC -= ValNo; // Convert from index into count
84 if (NC == 0) continue; // Skip empty type planes...
Chris Lattner00950542001-06-06 20:29:01 +000085
86 // Output type header: [num entries][type id number]
87 //
Chris Lattnere8fdde12001-09-07 16:39:41 +000088 output_vbr(NC, Out);
Chris Lattner00950542001-06-06 20:29:01 +000089
90 // Output the Type ID Number...
91 int Slot = Table.getValSlot(Plane.front()->getType());
Chris Lattnerb5794002002-04-07 22:49:37 +000092 assert (Slot != -1 && "Type in constant pool but not in function!!");
Chris Lattner00950542001-06-06 20:29:01 +000093 output_vbr((unsigned)Slot, Out);
94
Chris Lattner697954c2002-01-20 22:54:45 +000095 //cerr << "Emitting " << NC << " constants of type '"
96 // << Plane.front()->getType()->getName() << "' = Slot #" << Slot << "\n";
Chris Lattner00950542001-06-06 20:29:01 +000097
Chris Lattnere8fdde12001-09-07 16:39:41 +000098 for (unsigned i = ValNo; i < ValNo+NC; ++i) {
99 const Value *V = Plane[i];
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000100 if (const Constant *CPV = dyn_cast<Constant>(V)) {
Chris Lattnerccc25962002-04-18 18:14:56 +0000101 //cerr << "Serializing value: <" << V->getType() << ">: " << V << ":"
Chris Lattner00950542001-06-06 20:29:01 +0000102 // << Out.size() << "\n";
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000103 outputConstant(CPV);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000104 } else {
Chris Lattner1b98c5c2001-10-13 06:48:38 +0000105 outputType(cast<const Type>(V));
Chris Lattner00950542001-06-06 20:29:01 +0000106 }
107 }
108 }
Chris Lattner00950542001-06-06 20:29:01 +0000109}
110
111void BytecodeWriter::outputModuleInfoBlock(const Module *M) {
112 BytecodeBlock ModuleInfoBlock(BytecodeFormat::ModuleGlobalInfo, Out);
113
Chris Lattner70cc3392001-09-10 07:58:01 +0000114 // Output the types for the global variables in the module...
115 for (Module::const_giterator I = M->gbegin(), End = M->gend(); I != End;++I) {
Chris Lattnerd70684f2001-09-18 04:01:05 +0000116 const GlobalVariable *GV = *I;
117 int Slot = Table.getValSlot(GV->getType());
Chris Lattner70cc3392001-09-10 07:58:01 +0000118 assert(Slot != -1 && "Module global vars is broken!");
Chris Lattnerd70684f2001-09-18 04:01:05 +0000119
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000120 // Fields: bit0 = isConstant, bit1 = hasInitializer, bit2=InternalLinkage,
121 // bit3+ = slot#
122 unsigned oSlot = ((unsigned)Slot << 3) | (GV->hasInternalLinkage() << 2) |
123 (GV->hasInitializer() << 1) | GV->isConstant();
Chris Lattnerd70684f2001-09-18 04:01:05 +0000124 output_vbr(oSlot, Out);
125
Chris Lattner1b98c5c2001-10-13 06:48:38 +0000126 // If we have an initializer, output it now.
Chris Lattnerd70684f2001-09-18 04:01:05 +0000127 if (GV->hasInitializer()) {
128 Slot = Table.getValSlot(GV->getInitializer());
129 assert(Slot != -1 && "No slot for global var initializer!");
130 output_vbr((unsigned)Slot, Out);
131 }
Chris Lattner70cc3392001-09-10 07:58:01 +0000132 }
133 output_vbr((unsigned)Table.getValSlot(Type::VoidTy), Out);
134
Chris Lattnerb5794002002-04-07 22:49:37 +0000135 // Output the types of the functions in this module...
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000136 for (Module::const_iterator I = M->begin(), End = M->end(); I != End; ++I) {
Chris Lattner00950542001-06-06 20:29:01 +0000137 int Slot = Table.getValSlot((*I)->getType());
138 assert(Slot != -1 && "Module const pool is broken!");
139 assert(Slot >= Type::FirstDerivedTyID && "Derived type not in range!");
140 output_vbr((unsigned)Slot, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000141 }
142 output_vbr((unsigned)Table.getValSlot(Type::VoidTy), Out);
Chris Lattner70cc3392001-09-10 07:58:01 +0000143
144
Chris Lattner00950542001-06-06 20:29:01 +0000145 align32(Out);
146}
147
Chris Lattner79df7c02002-03-26 18:01:55 +0000148void BytecodeWriter::processMethod(const Function *M) {
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000149 BytecodeBlock FunctionBlock(BytecodeFormat::Function, Out);
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000150 output_vbr((unsigned)M->hasInternalLinkage(), Out);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000151 // Only output the constant pool and other goodies if needed...
152 if (!M->isExternal()) {
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000153
Chris Lattnerb5794002002-04-07 22:49:37 +0000154 // Get slot information about the function...
155 Table.incorporateFunction(M);
Chris Lattner00950542001-06-06 20:29:01 +0000156
Chris Lattnerb5794002002-04-07 22:49:37 +0000157 // Output information about the constants in the function...
Chris Lattnere8fdde12001-09-07 16:39:41 +0000158 outputConstants(true);
Chris Lattner00950542001-06-06 20:29:01 +0000159
Chris Lattnere8fdde12001-09-07 16:39:41 +0000160 // Output basic block nodes...
161 for_each(M->begin(), M->end(),
162 bind_obj(this, &BytecodeWriter::processBasicBlock));
163
Chris Lattnerb5794002002-04-07 22:49:37 +0000164 // If needed, output the symbol table for the function...
Chris Lattnere8fdde12001-09-07 16:39:41 +0000165 if (M->hasSymbolTable())
166 outputSymbolTable(*M->getSymbolTable());
167
Chris Lattnerb5794002002-04-07 22:49:37 +0000168 Table.purgeFunction();
Chris Lattnere8fdde12001-09-07 16:39:41 +0000169 }
Chris Lattner00950542001-06-06 20:29:01 +0000170}
171
172
Chris Lattnere8fdde12001-09-07 16:39:41 +0000173void BytecodeWriter::processBasicBlock(const BasicBlock *BB) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000174 BytecodeBlock FunctionBlock(BytecodeFormat::BasicBlock, Out);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000175 // Process all the instructions in the bb...
176 for_each(BB->begin(), BB->end(),
177 bind_obj(this, &BytecodeWriter::processInstruction));
Chris Lattner00950542001-06-06 20:29:01 +0000178}
179
180void BytecodeWriter::outputSymbolTable(const SymbolTable &MST) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000181 BytecodeBlock FunctionBlock(BytecodeFormat::SymbolTable, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000182
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000183 for (SymbolTable::const_iterator TI = MST.begin(); TI != MST.end(); ++TI) {
Chris Lattner00950542001-06-06 20:29:01 +0000184 SymbolTable::type_const_iterator I = MST.type_begin(TI->first);
185 SymbolTable::type_const_iterator End = MST.type_end(TI->first);
186 int Slot;
187
188 if (I == End) continue; // Don't mess with an absent type...
189
190 // Symtab block header: [num entries][type id number]
191 output_vbr(MST.type_size(TI->first), Out);
192
193 Slot = Table.getValSlot(TI->first);
194 assert(Slot != -1 && "Type in symtab, but not in table!");
195 output_vbr((unsigned)Slot, Out);
196
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000197 for (; I != End; ++I) {
Chris Lattner00950542001-06-06 20:29:01 +0000198 // Symtab entry: [def slot #][name]
199 Slot = Table.getValSlot(I->second);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000200 assert(Slot != -1 && "Value in symtab but has no slot number!!");
Chris Lattner00950542001-06-06 20:29:01 +0000201 output_vbr((unsigned)Slot, Out);
202 output(I->first, Out, false); // Don't force alignment...
203 }
204 }
205}
206
207void WriteBytecodeToFile(const Module *C, ostream &Out) {
Chris Lattnere8fdde12001-09-07 16:39:41 +0000208 assert(C && "You can't write a null module!!");
Chris Lattner00950542001-06-06 20:29:01 +0000209
Chris Lattner697954c2002-01-20 22:54:45 +0000210 std::deque<unsigned char> Buffer;
Chris Lattner00950542001-06-06 20:29:01 +0000211
212 // This object populates buffer for us...
213 BytecodeWriter BCW(Buffer, C);
214
Chris Lattnere8fdde12001-09-07 16:39:41 +0000215 // Okay, write the deque out to the ostream now... the deque is not
216 // sequential in memory, however, so write out as much as possible in big
217 // chunks, until we're done.
218 //
Chris Lattner697954c2002-01-20 22:54:45 +0000219 std::deque<unsigned char>::const_iterator I = Buffer.begin(),E = Buffer.end();
Chris Lattnere8fdde12001-09-07 16:39:41 +0000220 while (I != E) { // Loop until it's all written
221 // Scan to see how big this chunk is...
222 const unsigned char *ChunkPtr = &*I;
223 const unsigned char *LastPtr = ChunkPtr;
224 while (I != E) {
225 const unsigned char *ThisPtr = &*++I;
Chris Lattner5cb17412001-11-04 21:32:41 +0000226 if (LastPtr+1 != ThisPtr) { // Advanced by more than a byte of memory?
227 ++LastPtr;
228 break;
229 }
Chris Lattnere8fdde12001-09-07 16:39:41 +0000230 LastPtr = ThisPtr;
231 }
232
233 // Write out the chunk...
Chris Lattner697954c2002-01-20 22:54:45 +0000234 Out.write((char*)ChunkPtr, LastPtr-ChunkPtr);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000235 }
236
Chris Lattner00950542001-06-06 20:29:01 +0000237 Out.flush();
238}