blob: 04a0ca4624f2807740b48bfe83dca0c5c1f2fc17 [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//
5// This library uses the Analysis library to figure out offsets for
6// variables in the method tables...
7//
8// Note that this file uses an unusual technique of outputting all the bytecode
Chris Lattnere8fdde12001-09-07 16:39:41 +00009// to a deque of unsigned char's, then copies the deque to an ostream. The
Chris Lattner00950542001-06-06 20:29:01 +000010// reason for this is that we must do "seeking" in the stream to do back-
11// patching, and some very important ostreams that we want to support (like
12// pipes) do not support seeking. :( :( :(
13//
Chris Lattnere8fdde12001-09-07 16:39:41 +000014// The choice of the deque data structure is influenced by the extremely fast
15// "append" speed, plus the free "seek"/replace in the middle of the stream. I
16// didn't use a vector because the stream could end up very large and copying
17// the whole thing to reallocate would be kinda silly.
Chris Lattner00950542001-06-06 20:29:01 +000018//
19// Note that the performance of this library is not terribly important, because
20// it shouldn't be used by JIT type applications... so it is not a huge focus
21// at least. :)
22//
23//===----------------------------------------------------------------------===//
24
25#include "WriterInternals.h"
26#include "llvm/Module.h"
Chris Lattner70cc3392001-09-10 07:58:01 +000027#include "llvm/GlobalVariable.h"
Chris Lattner00950542001-06-06 20:29:01 +000028#include "llvm/Method.h"
29#include "llvm/BasicBlock.h"
30#include "llvm/ConstPoolVals.h"
31#include "llvm/SymbolTable.h"
32#include "llvm/DerivedTypes.h"
Chris Lattnere8fdde12001-09-07 16:39:41 +000033#include "llvm/Support/STLExtras.h"
Chris Lattner00950542001-06-06 20:29:01 +000034#include <string.h>
35#include <algorithm>
36
Chris Lattnere8fdde12001-09-07 16:39:41 +000037BytecodeWriter::BytecodeWriter(deque<unsigned char> &o, const Module *M)
Chris Lattner00950542001-06-06 20:29:01 +000038 : Out(o), Table(M, false) {
39
40 outputSignature();
41
42 // Emit the top level CLASS block.
43 BytecodeBlock ModuleBlock(BytecodeFormat::Module, Out);
44
Chris Lattnere8fdde12001-09-07 16:39:41 +000045 // Output the ID of first "derived" type:
Chris Lattner00950542001-06-06 20:29:01 +000046 output_vbr((unsigned)Type::FirstDerivedTyID, Out);
47 align32(Out);
48
Chris Lattnere8fdde12001-09-07 16:39:41 +000049 // Output module level constants, including types used by the method protos
50 outputConstants(false);
Chris Lattner00950542001-06-06 20:29:01 +000051
Chris Lattnere8fdde12001-09-07 16:39:41 +000052 // The ModuleInfoBlock follows directly after the Module constant pool
53 outputModuleInfoBlock(M);
54
55 // Do the whole module now! Process each method at a time...
56 for_each(M->begin(), M->end(),
57 bind_obj(this, &BytecodeWriter::processMethod));
58
59 // If needed, output the symbol table for the module...
Chris Lattner00950542001-06-06 20:29:01 +000060 if (M->hasSymbolTable())
61 outputSymbolTable(*M->getSymbolTable());
62}
63
64// TODO: REMOVE
65#include "llvm/Assembly/Writer.h"
66
Chris Lattnere8fdde12001-09-07 16:39:41 +000067void BytecodeWriter::outputConstants(bool isMethod) {
68 BytecodeBlock CPool(BytecodeFormat::ConstantPool, Out);
Chris Lattner00950542001-06-06 20:29:01 +000069
70 unsigned NumPlanes = Table.getNumPlanes();
Chris Lattner00950542001-06-06 20:29:01 +000071 for (unsigned pno = 0; pno < NumPlanes; pno++) {
72 const vector<const Value*> &Plane = Table.getPlane(pno);
Chris Lattnere8fdde12001-09-07 16:39:41 +000073 if (Plane.empty()) continue; // Skip empty type planes...
Chris Lattner00950542001-06-06 20:29:01 +000074
Chris Lattnere8fdde12001-09-07 16:39:41 +000075 unsigned ValNo = 0;
76 if (isMethod) // Don't reemit module constants
77 ValNo = Table.getModuleLevel(pno);
78 else if (pno == Type::TypeTyID)
79 ValNo = Type::FirstDerivedTyID; // Start emitting at the derived types...
Chris Lattner00950542001-06-06 20:29:01 +000080
Chris Lattnere8fdde12001-09-07 16:39:41 +000081 // Scan through and ignore method arguments...
82 for (; ValNo < Plane.size() && Plane[ValNo]->isMethodArgument(); ValNo++)
83 /*empty*/;
Chris Lattner00950542001-06-06 20:29:01 +000084
Chris Lattnere8fdde12001-09-07 16:39:41 +000085 unsigned NC = ValNo; // Number of constants
86 for (; NC < Plane.size() &&
87 (Plane[NC]->isConstant() || Plane[NC]->isType()); NC++) /*empty*/;
88 NC -= ValNo; // Convert from index into count
89 if (NC == 0) continue; // Skip empty type planes...
Chris Lattner00950542001-06-06 20:29:01 +000090
91 // Output type header: [num entries][type id number]
92 //
Chris Lattnere8fdde12001-09-07 16:39:41 +000093 output_vbr(NC, Out);
Chris Lattner00950542001-06-06 20:29:01 +000094
95 // Output the Type ID Number...
96 int Slot = Table.getValSlot(Plane.front()->getType());
97 assert (Slot != -1 && "Type in constant pool but not in method!!");
98 output_vbr((unsigned)Slot, Out);
99
Chris Lattnere8fdde12001-09-07 16:39:41 +0000100 //cout << "Emitting " << NC << " constants of type '"
101 // << Plane.front()->getType()->getName() << "' = Slot #" << Slot << endl;
Chris Lattner00950542001-06-06 20:29:01 +0000102
Chris Lattnere8fdde12001-09-07 16:39:41 +0000103 for (unsigned i = ValNo; i < ValNo+NC; ++i) {
104 const Value *V = Plane[i];
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000105 if (const ConstPoolVal *CPV = V->castConstant()) {
Chris Lattner00950542001-06-06 20:29:01 +0000106 //cerr << "Serializing value: <" << V->getType() << ">: "
107 // << ((const ConstPoolVal*)V)->getStrValue() << ":"
108 // << Out.size() << "\n";
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000109 outputConstant(CPV);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000110 } else {
Chris Lattner9636a912001-10-01 16:18:37 +0000111 const Type *Ty = cast<const Type>(V);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000112 outputType(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000113 }
114 }
115 }
Chris Lattner00950542001-06-06 20:29:01 +0000116}
117
118void BytecodeWriter::outputModuleInfoBlock(const Module *M) {
119 BytecodeBlock ModuleInfoBlock(BytecodeFormat::ModuleGlobalInfo, Out);
120
Chris Lattner70cc3392001-09-10 07:58:01 +0000121 // Output the types for the global variables in the module...
122 for (Module::const_giterator I = M->gbegin(), End = M->gend(); I != End;++I) {
Chris Lattnerd70684f2001-09-18 04:01:05 +0000123 const GlobalVariable *GV = *I;
124 int Slot = Table.getValSlot(GV->getType());
Chris Lattner70cc3392001-09-10 07:58:01 +0000125 assert(Slot != -1 && "Module global vars is broken!");
Chris Lattnerd70684f2001-09-18 04:01:05 +0000126
127 // Fields: bit0 = isConstant, bit1 = hasInitializer, bit2+ = slot#
128 unsigned oSlot = ((unsigned)Slot << 2) | (GV->hasInitializer() << 1) |
129 GV->isConstant();
130 output_vbr(oSlot, Out);
131
132 // If we have an initialized, output it now.
133 if (GV->hasInitializer()) {
134 Slot = Table.getValSlot(GV->getInitializer());
135 assert(Slot != -1 && "No slot for global var initializer!");
136 output_vbr((unsigned)Slot, Out);
137 }
Chris Lattner70cc3392001-09-10 07:58:01 +0000138 }
139 output_vbr((unsigned)Table.getValSlot(Type::VoidTy), Out);
140
141 // Output the types of the methods in this module...
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000142 for (Module::const_iterator I = M->begin(), End = M->end(); I != End; ++I) {
Chris Lattner00950542001-06-06 20:29:01 +0000143 int Slot = Table.getValSlot((*I)->getType());
144 assert(Slot != -1 && "Module const pool is broken!");
145 assert(Slot >= Type::FirstDerivedTyID && "Derived type not in range!");
146 output_vbr((unsigned)Slot, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000147 }
148 output_vbr((unsigned)Table.getValSlot(Type::VoidTy), Out);
Chris Lattner70cc3392001-09-10 07:58:01 +0000149
150
Chris Lattner00950542001-06-06 20:29:01 +0000151 align32(Out);
152}
153
Chris Lattnere8fdde12001-09-07 16:39:41 +0000154void BytecodeWriter::processMethod(const Method *M) {
Chris Lattner00950542001-06-06 20:29:01 +0000155 BytecodeBlock MethodBlock(BytecodeFormat::Method, Out);
156
Chris Lattnere8fdde12001-09-07 16:39:41 +0000157 // Only output the constant pool and other goodies if needed...
158 if (!M->isExternal()) {
159 // Get slot information about the method...
160 Table.incorporateMethod(M);
Chris Lattner00950542001-06-06 20:29:01 +0000161
Chris Lattnere8fdde12001-09-07 16:39:41 +0000162 // Output information about the constants in the method...
163 outputConstants(true);
Chris Lattner00950542001-06-06 20:29:01 +0000164
Chris Lattnere8fdde12001-09-07 16:39:41 +0000165 // Output basic block nodes...
166 for_each(M->begin(), M->end(),
167 bind_obj(this, &BytecodeWriter::processBasicBlock));
168
169 // If needed, output the symbol table for the method...
170 if (M->hasSymbolTable())
171 outputSymbolTable(*M->getSymbolTable());
172
173 Table.purgeMethod();
174 }
Chris Lattner00950542001-06-06 20:29:01 +0000175}
176
177
Chris Lattnere8fdde12001-09-07 16:39:41 +0000178void BytecodeWriter::processBasicBlock(const BasicBlock *BB) {
Chris Lattner00950542001-06-06 20:29:01 +0000179 BytecodeBlock MethodBlock(BytecodeFormat::BasicBlock, Out);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000180 // Process all the instructions in the bb...
181 for_each(BB->begin(), BB->end(),
182 bind_obj(this, &BytecodeWriter::processInstruction));
Chris Lattner00950542001-06-06 20:29:01 +0000183}
184
185void BytecodeWriter::outputSymbolTable(const SymbolTable &MST) {
186 BytecodeBlock MethodBlock(BytecodeFormat::SymbolTable, Out);
187
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000188 for (SymbolTable::const_iterator TI = MST.begin(); TI != MST.end(); ++TI) {
Chris Lattner00950542001-06-06 20:29:01 +0000189 SymbolTable::type_const_iterator I = MST.type_begin(TI->first);
190 SymbolTable::type_const_iterator End = MST.type_end(TI->first);
191 int Slot;
192
193 if (I == End) continue; // Don't mess with an absent type...
194
195 // Symtab block header: [num entries][type id number]
196 output_vbr(MST.type_size(TI->first), Out);
197
198 Slot = Table.getValSlot(TI->first);
199 assert(Slot != -1 && "Type in symtab, but not in table!");
200 output_vbr((unsigned)Slot, Out);
201
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000202 for (; I != End; ++I) {
Chris Lattner00950542001-06-06 20:29:01 +0000203 // Symtab entry: [def slot #][name]
204 Slot = Table.getValSlot(I->second);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000205 assert(Slot != -1 && "Value in symtab but has no slot number!!");
Chris Lattner00950542001-06-06 20:29:01 +0000206 output_vbr((unsigned)Slot, Out);
207 output(I->first, Out, false); // Don't force alignment...
208 }
209 }
210}
211
212void WriteBytecodeToFile(const Module *C, ostream &Out) {
Chris Lattnere8fdde12001-09-07 16:39:41 +0000213 assert(C && "You can't write a null module!!");
Chris Lattner00950542001-06-06 20:29:01 +0000214
Chris Lattnere8fdde12001-09-07 16:39:41 +0000215 deque<unsigned char> Buffer;
Chris Lattner00950542001-06-06 20:29:01 +0000216
217 // This object populates buffer for us...
218 BytecodeWriter BCW(Buffer, C);
219
Chris Lattnere8fdde12001-09-07 16:39:41 +0000220 // Okay, write the deque out to the ostream now... the deque is not
221 // sequential in memory, however, so write out as much as possible in big
222 // chunks, until we're done.
223 //
224 deque<unsigned char>::const_iterator I = Buffer.begin(), E = Buffer.end();
225 while (I != E) { // Loop until it's all written
226 // Scan to see how big this chunk is...
227 const unsigned char *ChunkPtr = &*I;
228 const unsigned char *LastPtr = ChunkPtr;
229 while (I != E) {
230 const unsigned char *ThisPtr = &*++I;
231 if (LastPtr+1 != ThisPtr) break;// Advanced by more than a byte of memory?
232 LastPtr = ThisPtr;
233 }
234
235 // Write out the chunk...
236 Out.write(ChunkPtr, LastPtr-ChunkPtr+(I != E));
237 }
238
Chris Lattner00950542001-06-06 20:29:01 +0000239 Out.flush();
240}