blob: ffe67a32f50126c65da3efa2a793a61b41f86615 [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
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 Lattnercfe26c92001-10-01 18:26:53 +000084 (isa<ConstPoolVal>(Plane[NC]) ||
85 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 Lattner1d87bcf2001-10-01 20:11:19 +0000103 if (const ConstPoolVal *CPV = dyn_cast<ConstPoolVal>(V)) {
Chris Lattner00950542001-06-06 20:29:01 +0000104 //cerr << "Serializing value: <" << V->getType() << ">: "
105 // << ((const ConstPoolVal*)V)->getStrValue() << ":"
106 // << 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
124 // Fields: bit0 = isConstant, bit1 = hasInitializer, bit2+ = slot#
125 unsigned oSlot = ((unsigned)Slot << 2) | (GV->hasInitializer() << 1) |
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000126 isa<ConstPoolVal>(GV);
Chris Lattnerd70684f2001-09-18 04:01:05 +0000127 output_vbr(oSlot, Out);
128
Chris Lattner1b98c5c2001-10-13 06:48:38 +0000129 // If we have an initializer, output it now.
Chris Lattnerd70684f2001-09-18 04:01:05 +0000130 if (GV->hasInitializer()) {
131 Slot = Table.getValSlot(GV->getInitializer());
132 assert(Slot != -1 && "No slot for global var initializer!");
133 output_vbr((unsigned)Slot, Out);
134 }
Chris Lattner70cc3392001-09-10 07:58:01 +0000135 }
136 output_vbr((unsigned)Table.getValSlot(Type::VoidTy), Out);
137
138 // Output the types of the methods in this module...
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000139 for (Module::const_iterator I = M->begin(), End = M->end(); I != End; ++I) {
Chris Lattner00950542001-06-06 20:29:01 +0000140 int Slot = Table.getValSlot((*I)->getType());
141 assert(Slot != -1 && "Module const pool is broken!");
142 assert(Slot >= Type::FirstDerivedTyID && "Derived type not in range!");
143 output_vbr((unsigned)Slot, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000144 }
145 output_vbr((unsigned)Table.getValSlot(Type::VoidTy), Out);
Chris Lattner70cc3392001-09-10 07:58:01 +0000146
147
Chris Lattner00950542001-06-06 20:29:01 +0000148 align32(Out);
149}
150
Chris Lattnere8fdde12001-09-07 16:39:41 +0000151void BytecodeWriter::processMethod(const Method *M) {
Chris Lattner00950542001-06-06 20:29:01 +0000152 BytecodeBlock MethodBlock(BytecodeFormat::Method, Out);
153
Chris Lattnere8fdde12001-09-07 16:39:41 +0000154 // Only output the constant pool and other goodies if needed...
155 if (!M->isExternal()) {
156 // Get slot information about the method...
157 Table.incorporateMethod(M);
Chris Lattner00950542001-06-06 20:29:01 +0000158
Chris Lattnere8fdde12001-09-07 16:39:41 +0000159 // Output information about the constants in the method...
160 outputConstants(true);
Chris Lattner00950542001-06-06 20:29:01 +0000161
Chris Lattnere8fdde12001-09-07 16:39:41 +0000162 // Output basic block nodes...
163 for_each(M->begin(), M->end(),
164 bind_obj(this, &BytecodeWriter::processBasicBlock));
165
166 // If needed, output the symbol table for the method...
167 if (M->hasSymbolTable())
168 outputSymbolTable(*M->getSymbolTable());
169
170 Table.purgeMethod();
171 }
Chris Lattner00950542001-06-06 20:29:01 +0000172}
173
174
Chris Lattnere8fdde12001-09-07 16:39:41 +0000175void BytecodeWriter::processBasicBlock(const BasicBlock *BB) {
Chris Lattner00950542001-06-06 20:29:01 +0000176 BytecodeBlock MethodBlock(BytecodeFormat::BasicBlock, Out);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000177 // Process all the instructions in the bb...
178 for_each(BB->begin(), BB->end(),
179 bind_obj(this, &BytecodeWriter::processInstruction));
Chris Lattner00950542001-06-06 20:29:01 +0000180}
181
182void BytecodeWriter::outputSymbolTable(const SymbolTable &MST) {
183 BytecodeBlock MethodBlock(BytecodeFormat::SymbolTable, Out);
184
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000185 for (SymbolTable::const_iterator TI = MST.begin(); TI != MST.end(); ++TI) {
Chris Lattner00950542001-06-06 20:29:01 +0000186 SymbolTable::type_const_iterator I = MST.type_begin(TI->first);
187 SymbolTable::type_const_iterator End = MST.type_end(TI->first);
188 int Slot;
189
190 if (I == End) continue; // Don't mess with an absent type...
191
192 // Symtab block header: [num entries][type id number]
193 output_vbr(MST.type_size(TI->first), Out);
194
195 Slot = Table.getValSlot(TI->first);
196 assert(Slot != -1 && "Type in symtab, but not in table!");
197 output_vbr((unsigned)Slot, Out);
198
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000199 for (; I != End; ++I) {
Chris Lattner00950542001-06-06 20:29:01 +0000200 // Symtab entry: [def slot #][name]
201 Slot = Table.getValSlot(I->second);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000202 assert(Slot != -1 && "Value in symtab but has no slot number!!");
Chris Lattner00950542001-06-06 20:29:01 +0000203 output_vbr((unsigned)Slot, Out);
204 output(I->first, Out, false); // Don't force alignment...
205 }
206 }
207}
208
209void WriteBytecodeToFile(const Module *C, ostream &Out) {
Chris Lattnere8fdde12001-09-07 16:39:41 +0000210 assert(C && "You can't write a null module!!");
Chris Lattner00950542001-06-06 20:29:01 +0000211
Chris Lattnere8fdde12001-09-07 16:39:41 +0000212 deque<unsigned char> Buffer;
Chris Lattner00950542001-06-06 20:29:01 +0000213
214 // This object populates buffer for us...
215 BytecodeWriter BCW(Buffer, C);
216
Chris Lattnere8fdde12001-09-07 16:39:41 +0000217 // Okay, write the deque out to the ostream now... the deque is not
218 // sequential in memory, however, so write out as much as possible in big
219 // chunks, until we're done.
220 //
221 deque<unsigned char>::const_iterator I = Buffer.begin(), E = Buffer.end();
222 while (I != E) { // Loop until it's all written
223 // Scan to see how big this chunk is...
224 const unsigned char *ChunkPtr = &*I;
225 const unsigned char *LastPtr = ChunkPtr;
226 while (I != E) {
227 const unsigned char *ThisPtr = &*++I;
228 if (LastPtr+1 != ThisPtr) break;// Advanced by more than a byte of memory?
229 LastPtr = ThisPtr;
230 }
231
232 // Write out the chunk...
233 Out.write(ChunkPtr, LastPtr-ChunkPtr+(I != E));
234 }
235
Chris Lattner00950542001-06-06 20:29:01 +0000236 Out.flush();
237}