blob: 3091384722b773a8c9ba1044774cc75cbf0868f9 [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"
Chris Lattnere9bb2df2001-12-03 22:26:30 +000030#include "llvm/ConstantVals.h"
Chris Lattner00950542001-06-06 20:29:01 +000031#include "llvm/SymbolTable.h"
32#include "llvm/DerivedTypes.h"
Chris Lattnerf60dc882001-11-29 16:32:16 +000033#include "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
Chris Lattnere8fdde12001-09-07 16:39:41 +000064void BytecodeWriter::outputConstants(bool isMethod) {
65 BytecodeBlock CPool(BytecodeFormat::ConstantPool, Out);
Chris Lattner00950542001-06-06 20:29:01 +000066
67 unsigned NumPlanes = Table.getNumPlanes();
Chris Lattner00950542001-06-06 20:29:01 +000068 for (unsigned pno = 0; pno < NumPlanes; pno++) {
69 const vector<const Value*> &Plane = Table.getPlane(pno);
Chris Lattnere8fdde12001-09-07 16:39:41 +000070 if (Plane.empty()) continue; // Skip empty type planes...
Chris Lattner00950542001-06-06 20:29:01 +000071
Chris Lattnere8fdde12001-09-07 16:39:41 +000072 unsigned ValNo = 0;
73 if (isMethod) // Don't reemit module constants
74 ValNo = Table.getModuleLevel(pno);
75 else if (pno == Type::TypeTyID)
76 ValNo = Type::FirstDerivedTyID; // Start emitting at the derived types...
Chris Lattner00950542001-06-06 20:29:01 +000077
Chris Lattnere8fdde12001-09-07 16:39:41 +000078 // Scan through and ignore method arguments...
Chris Lattner1d87bcf2001-10-01 20:11:19 +000079 for (; ValNo < Plane.size() && isa<MethodArgument>(Plane[ValNo]); ValNo++)
Chris Lattnere8fdde12001-09-07 16:39:41 +000080 /*empty*/;
Chris Lattner00950542001-06-06 20:29:01 +000081
Chris Lattnere8fdde12001-09-07 16:39:41 +000082 unsigned NC = ValNo; // Number of constants
83 for (; NC < Plane.size() &&
Chris Lattnere9bb2df2001-12-03 22:26:30 +000084 (isa<Constant>(Plane[NC]) ||
Chris Lattnercfe26c92001-10-01 18:26:53 +000085 isa<Type>(Plane[NC])); NC++) /*empty*/;
Chris Lattnere8fdde12001-09-07 16:39:41 +000086 NC -= ValNo; // Convert from index into count
87 if (NC == 0) continue; // Skip empty type planes...
Chris Lattner00950542001-06-06 20:29:01 +000088
89 // Output type header: [num entries][type id number]
90 //
Chris Lattnere8fdde12001-09-07 16:39:41 +000091 output_vbr(NC, Out);
Chris Lattner00950542001-06-06 20:29:01 +000092
93 // Output the Type ID Number...
94 int Slot = Table.getValSlot(Plane.front()->getType());
95 assert (Slot != -1 && "Type in constant pool but not in method!!");
96 output_vbr((unsigned)Slot, Out);
97
Chris Lattnere8fdde12001-09-07 16:39:41 +000098 //cout << "Emitting " << NC << " constants of type '"
99 // << Plane.front()->getType()->getName() << "' = Slot #" << Slot << endl;
Chris Lattner00950542001-06-06 20:29:01 +0000100
Chris Lattnere8fdde12001-09-07 16:39:41 +0000101 for (unsigned i = ValNo; i < ValNo+NC; ++i) {
102 const Value *V = Plane[i];
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000103 if (const Constant *CPV = dyn_cast<Constant>(V)) {
Chris Lattner00950542001-06-06 20:29:01 +0000104 //cerr << "Serializing value: <" << V->getType() << ">: "
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000105 // << ((const Constant*)V)->getStrValue() << ":"
Chris Lattner00950542001-06-06 20:29:01 +0000106 // << Out.size() << "\n";
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000107 outputConstant(CPV);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000108 } else {
Chris Lattner1b98c5c2001-10-13 06:48:38 +0000109 outputType(cast<const Type>(V));
Chris Lattner00950542001-06-06 20:29:01 +0000110 }
111 }
112 }
Chris Lattner00950542001-06-06 20:29:01 +0000113}
114
115void BytecodeWriter::outputModuleInfoBlock(const Module *M) {
116 BytecodeBlock ModuleInfoBlock(BytecodeFormat::ModuleGlobalInfo, Out);
117
Chris Lattner70cc3392001-09-10 07:58:01 +0000118 // Output the types for the global variables in the module...
119 for (Module::const_giterator I = M->gbegin(), End = M->gend(); I != End;++I) {
Chris Lattnerd70684f2001-09-18 04:01:05 +0000120 const GlobalVariable *GV = *I;
121 int Slot = Table.getValSlot(GV->getType());
Chris Lattner70cc3392001-09-10 07:58:01 +0000122 assert(Slot != -1 && "Module global vars is broken!");
Chris Lattnerd70684f2001-09-18 04:01:05 +0000123
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000124 // Fields: bit0 = isConstant, bit1 = hasInitializer, bit2=InternalLinkage,
125 // bit3+ = slot#
126 unsigned oSlot = ((unsigned)Slot << 3) | (GV->hasInternalLinkage() << 2) |
127 (GV->hasInitializer() << 1) | GV->isConstant();
Chris Lattnerd70684f2001-09-18 04:01:05 +0000128 output_vbr(oSlot, Out);
129
Chris Lattner1b98c5c2001-10-13 06:48:38 +0000130 // If we have an initializer, output it now.
Chris Lattnerd70684f2001-09-18 04:01:05 +0000131 if (GV->hasInitializer()) {
132 Slot = Table.getValSlot(GV->getInitializer());
133 assert(Slot != -1 && "No slot for global var initializer!");
134 output_vbr((unsigned)Slot, Out);
135 }
Chris Lattner70cc3392001-09-10 07:58:01 +0000136 }
137 output_vbr((unsigned)Table.getValSlot(Type::VoidTy), Out);
138
139 // Output the types of the methods in this module...
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000140 for (Module::const_iterator I = M->begin(), End = M->end(); I != End; ++I) {
Chris Lattner00950542001-06-06 20:29:01 +0000141 int Slot = Table.getValSlot((*I)->getType());
142 assert(Slot != -1 && "Module const pool is broken!");
143 assert(Slot >= Type::FirstDerivedTyID && "Derived type not in range!");
144 output_vbr((unsigned)Slot, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000145 }
146 output_vbr((unsigned)Table.getValSlot(Type::VoidTy), Out);
Chris Lattner70cc3392001-09-10 07:58:01 +0000147
148
Chris Lattner00950542001-06-06 20:29:01 +0000149 align32(Out);
150}
151
Chris Lattnere8fdde12001-09-07 16:39:41 +0000152void BytecodeWriter::processMethod(const Method *M) {
Chris Lattner00950542001-06-06 20:29:01 +0000153 BytecodeBlock MethodBlock(BytecodeFormat::Method, Out);
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000154 output_vbr((unsigned)M->hasInternalLinkage(), Out);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000155 // Only output the constant pool and other goodies if needed...
156 if (!M->isExternal()) {
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000157
Chris Lattnere8fdde12001-09-07 16:39:41 +0000158 // Get slot information about the method...
159 Table.incorporateMethod(M);
Chris Lattner00950542001-06-06 20:29:01 +0000160
Chris Lattnere8fdde12001-09-07 16:39:41 +0000161 // Output information about the constants in the method...
162 outputConstants(true);
Chris Lattner00950542001-06-06 20:29:01 +0000163
Chris Lattnere8fdde12001-09-07 16:39:41 +0000164 // Output basic block nodes...
165 for_each(M->begin(), M->end(),
166 bind_obj(this, &BytecodeWriter::processBasicBlock));
167
168 // If needed, output the symbol table for the method...
169 if (M->hasSymbolTable())
170 outputSymbolTable(*M->getSymbolTable());
171
172 Table.purgeMethod();
173 }
Chris Lattner00950542001-06-06 20:29:01 +0000174}
175
176
Chris Lattnere8fdde12001-09-07 16:39:41 +0000177void BytecodeWriter::processBasicBlock(const BasicBlock *BB) {
Chris Lattner00950542001-06-06 20:29:01 +0000178 BytecodeBlock MethodBlock(BytecodeFormat::BasicBlock, Out);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000179 // Process all the instructions in the bb...
180 for_each(BB->begin(), BB->end(),
181 bind_obj(this, &BytecodeWriter::processInstruction));
Chris Lattner00950542001-06-06 20:29:01 +0000182}
183
184void BytecodeWriter::outputSymbolTable(const SymbolTable &MST) {
185 BytecodeBlock MethodBlock(BytecodeFormat::SymbolTable, Out);
186
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000187 for (SymbolTable::const_iterator TI = MST.begin(); TI != MST.end(); ++TI) {
Chris Lattner00950542001-06-06 20:29:01 +0000188 SymbolTable::type_const_iterator I = MST.type_begin(TI->first);
189 SymbolTable::type_const_iterator End = MST.type_end(TI->first);
190 int Slot;
191
192 if (I == End) continue; // Don't mess with an absent type...
193
194 // Symtab block header: [num entries][type id number]
195 output_vbr(MST.type_size(TI->first), Out);
196
197 Slot = Table.getValSlot(TI->first);
198 assert(Slot != -1 && "Type in symtab, but not in table!");
199 output_vbr((unsigned)Slot, Out);
200
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000201 for (; I != End; ++I) {
Chris Lattner00950542001-06-06 20:29:01 +0000202 // Symtab entry: [def slot #][name]
203 Slot = Table.getValSlot(I->second);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000204 assert(Slot != -1 && "Value in symtab but has no slot number!!");
Chris Lattner00950542001-06-06 20:29:01 +0000205 output_vbr((unsigned)Slot, Out);
206 output(I->first, Out, false); // Don't force alignment...
207 }
208 }
209}
210
211void WriteBytecodeToFile(const Module *C, ostream &Out) {
Chris Lattnere8fdde12001-09-07 16:39:41 +0000212 assert(C && "You can't write a null module!!");
Chris Lattner00950542001-06-06 20:29:01 +0000213
Chris Lattnere8fdde12001-09-07 16:39:41 +0000214 deque<unsigned char> Buffer;
Chris Lattner00950542001-06-06 20:29:01 +0000215
216 // This object populates buffer for us...
217 BytecodeWriter BCW(Buffer, C);
218
Chris Lattnere8fdde12001-09-07 16:39:41 +0000219 // Okay, write the deque out to the ostream now... the deque is not
220 // sequential in memory, however, so write out as much as possible in big
221 // chunks, until we're done.
222 //
223 deque<unsigned char>::const_iterator I = Buffer.begin(), E = Buffer.end();
224 while (I != E) { // Loop until it's all written
225 // Scan to see how big this chunk is...
226 const unsigned char *ChunkPtr = &*I;
227 const unsigned char *LastPtr = ChunkPtr;
228 while (I != E) {
229 const unsigned char *ThisPtr = &*++I;
Chris Lattner5cb17412001-11-04 21:32:41 +0000230 if (LastPtr+1 != ThisPtr) { // Advanced by more than a byte of memory?
231 ++LastPtr;
232 break;
233 }
Chris Lattnere8fdde12001-09-07 16:39:41 +0000234 LastPtr = ThisPtr;
235 }
236
237 // Write out the chunk...
Chris Lattner5cb17412001-11-04 21:32:41 +0000238 Out.write(ChunkPtr, LastPtr-ChunkPtr);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000239 }
240
Chris Lattner00950542001-06-06 20:29:01 +0000241 Out.flush();
242}