Initial revision
[oota-llvm.git] / lib / Bytecode / Writer / Writer.cpp
1 //===-- 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
9 // to a vector of unsigned char's, then copies the vector to an ostream.  The
10 // 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 //
14 // The choice of the vector data structure is influenced by the extremely fast
15 // "append" speed, plus the free "seek"/replace in the middle of the stream.
16 //
17 // Note that the performance of this library is not terribly important, because
18 // it shouldn't be used by JIT type applications... so it is not a huge focus
19 // at least.  :)
20 //
21 //===----------------------------------------------------------------------===//
22
23 #include "WriterInternals.h"
24 #include "llvm/Module.h"
25 #include "llvm/Method.h"
26 #include "llvm/BasicBlock.h"
27 #include "llvm/ConstPoolVals.h"
28 #include "llvm/SymbolTable.h"
29 #include "llvm/DerivedTypes.h"
30 #include <string.h>
31 #include <algorithm>
32
33 BytecodeWriter::BytecodeWriter(vector<unsigned char> &o, const Module *M) 
34   : Out(o), Table(M, false) {
35
36   outputSignature();
37
38   // Emit the top level CLASS block.
39   BytecodeBlock ModuleBlock(BytecodeFormat::Module, Out);
40
41   // Output largest ID of first "primitive" type:
42   output_vbr((unsigned)Type::FirstDerivedTyID, Out);
43   align32(Out);
44
45   // Do the whole module now!
46   processModule(M);
47
48   // If needed, output the symbol table for the class...
49   if (M->hasSymbolTable())
50     outputSymbolTable(*M->getSymbolTable());
51 }
52
53 // TODO: REMOVE
54 #include "llvm/Assembly/Writer.h"
55
56 bool BytecodeWriter::processConstPool(const ConstantPool &CP, bool isMethod) {
57   BytecodeBlock *CPool = new BytecodeBlock(BytecodeFormat::ConstantPool, Out);
58
59   unsigned NumPlanes = Table.getNumPlanes();
60
61   for (unsigned pno = 0; pno < NumPlanes; pno++) {
62     const vector<const Value*> &Plane = Table.getPlane(pno);
63     if (Plane.empty()) continue;          // Skip empty type planes...
64
65     unsigned ValNo = 0;   // Don't reemit module constants
66     if (isMethod) ValNo = Table.getModuleLevel(pno);
67     
68     unsigned NumConstants = 0;
69     for (unsigned vn = ValNo; vn < Plane.size(); vn++)
70       if (Plane[vn]->getValueType() == Value::ConstantVal)
71         NumConstants++;
72
73     if (NumConstants == 0) continue;  // Skip empty type planes...
74
75     // Output type header: [num entries][type id number]
76     //
77     output_vbr(NumConstants, Out);
78
79     // Output the Type ID Number...
80     int Slot = Table.getValSlot(Plane.front()->getType());
81     assert (Slot != -1 && "Type in constant pool but not in method!!");
82     output_vbr((unsigned)Slot, Out);
83
84     //cerr << "NC: " << NumConstants << " Slot = " << hex << Slot << endl;
85
86     for (; ValNo < Plane.size(); ValNo++) {
87       const Value *V = Plane[ValNo];
88       if (V->getValueType() == Value::ConstantVal) {
89         //cerr << "Serializing value: <" << V->getType() << ">: " 
90         //     << ((const ConstPoolVal*)V)->getStrValue() << ":" 
91         //     << Out.size() << "\n";
92         outputConstant((const ConstPoolVal*)V);
93       }
94     }
95   }
96
97   delete CPool;  // End bytecode block section!
98
99   if (!isMethod) { // The ModuleInfoBlock follows directly after the c-pool
100     assert(CP.getParent()->getValueType() == Value::ModuleVal);
101     outputModuleInfoBlock((const Module*)CP.getParent());
102   }
103
104   return false;
105 }
106
107 void BytecodeWriter::outputModuleInfoBlock(const Module *M) {
108   BytecodeBlock ModuleInfoBlock(BytecodeFormat::ModuleGlobalInfo, Out);
109   
110   // Output the types of the methods in this class
111   Module::MethodListType::const_iterator I = M->getMethodList().begin();
112   while (I != M->getMethodList().end()) {
113     int Slot = Table.getValSlot((*I)->getType());
114     assert(Slot != -1 && "Module const pool is broken!");
115     assert(Slot >= Type::FirstDerivedTyID && "Derived type not in range!");
116     output_vbr((unsigned)Slot, Out);
117     I++;
118   }
119   output_vbr((unsigned)Table.getValSlot(Type::VoidTy), Out);
120   align32(Out);
121 }
122
123 bool BytecodeWriter::processMethod(const Method *M) {
124   BytecodeBlock MethodBlock(BytecodeFormat::Method, Out);
125
126   Table.incorporateMethod(M);
127
128   if (ModuleAnalyzer::processMethod(M)) return true;
129   
130   // If needed, output the symbol table for the method...
131   if (M->hasSymbolTable())
132     outputSymbolTable(*M->getSymbolTable());
133
134   Table.purgeMethod();
135   return false;
136 }
137
138
139 bool BytecodeWriter::processBasicBlock(const BasicBlock *BB) {
140   BytecodeBlock MethodBlock(BytecodeFormat::BasicBlock, Out);
141   return ModuleAnalyzer::processBasicBlock(BB);
142 }
143
144 void BytecodeWriter::outputSymbolTable(const SymbolTable &MST) {
145   BytecodeBlock MethodBlock(BytecodeFormat::SymbolTable, Out);
146
147   for (SymbolTable::const_iterator TI = MST.begin(); TI != MST.end(); TI++) {
148     SymbolTable::type_const_iterator I = MST.type_begin(TI->first);
149     SymbolTable::type_const_iterator End = MST.type_end(TI->first);
150     int Slot;
151     
152     if (I == End) continue;  // Don't mess with an absent type...
153
154     // Symtab block header: [num entries][type id number]
155     output_vbr(MST.type_size(TI->first), Out);
156
157     Slot = Table.getValSlot(TI->first);
158     assert(Slot != -1 && "Type in symtab, but not in table!");
159     output_vbr((unsigned)Slot, Out);
160
161     for (; I != End; I++) {
162       // Symtab entry: [def slot #][name]
163       Slot = Table.getValSlot(I->second);
164       assert (Slot != -1 && "Value in symtab but not in method!!");
165       output_vbr((unsigned)Slot, Out);
166       output(I->first, Out, false); // Don't force alignment...
167     }
168   }
169 }
170
171 void WriteBytecodeToFile(const Module *C, ostream &Out) {
172   assert(C && "You can't write a null class!!");
173
174   vector<unsigned char> Buffer;
175
176   // This object populates buffer for us...
177   BytecodeWriter BCW(Buffer, C);
178
179   // Okay, write the vector out to the ostream now...
180   Out.write(&Buffer[0], Buffer.size());
181   Out.flush();
182 }