1 //===-- Writer.cpp - Library for writing VM bytecode files -------*- C++ -*--=//
3 // This library implements the functionality defined in llvm/Bytecode/Writer.h
5 // This library uses the Analysis library to figure out offsets for
6 // variables in the method tables...
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. :( :( :(
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.
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
21 //===----------------------------------------------------------------------===//
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"
33 BytecodeWriter::BytecodeWriter(vector<unsigned char> &o, const Module *M)
34 : Out(o), Table(M, false) {
38 // Emit the top level CLASS block.
39 BytecodeBlock ModuleBlock(BytecodeFormat::Module, Out);
41 // Output largest ID of first "primitive" type:
42 output_vbr((unsigned)Type::FirstDerivedTyID, Out);
45 // Do the whole module now!
48 // If needed, output the symbol table for the class...
49 if (M->hasSymbolTable())
50 outputSymbolTable(*M->getSymbolTable());
54 #include "llvm/Assembly/Writer.h"
56 bool BytecodeWriter::processConstPool(const ConstantPool &CP, bool isMethod) {
57 BytecodeBlock *CPool = new BytecodeBlock(BytecodeFormat::ConstantPool, Out);
59 unsigned NumPlanes = Table.getNumPlanes();
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...
65 unsigned ValNo = 0; // Don't reemit module constants
66 if (isMethod) ValNo = Table.getModuleLevel(pno);
68 unsigned NumConstants = 0;
69 for (unsigned vn = ValNo; vn < Plane.size(); vn++)
70 if (Plane[vn]->isConstant())
73 if (NumConstants == 0) continue; // Skip empty type planes...
75 // Output type header: [num entries][type id number]
77 output_vbr(NumConstants, Out);
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);
84 //cerr << "NC: " << NumConstants << " Slot = " << hex << Slot << endl;
86 for (; ValNo < Plane.size(); ValNo++) {
87 const Value *V = Plane[ValNo];
88 if (const ConstPoolVal *CPV = V->castConstant()) {
89 //cerr << "Serializing value: <" << V->getType() << ">: "
90 // << ((const ConstPoolVal*)V)->getStrValue() << ":"
91 // << Out.size() << "\n";
97 delete CPool; // End bytecode block section!
99 if (!isMethod) // The ModuleInfoBlock follows directly after the c-pool
100 outputModuleInfoBlock(CP.getParentV()->castModuleAsserting());
105 void BytecodeWriter::outputModuleInfoBlock(const Module *M) {
106 BytecodeBlock ModuleInfoBlock(BytecodeFormat::ModuleGlobalInfo, Out);
108 // Output the types of the methods in this class
109 for (Module::const_iterator I = M->begin(), End = M->end(); I != End; ++I) {
110 int Slot = Table.getValSlot((*I)->getType());
111 assert(Slot != -1 && "Module const pool is broken!");
112 assert(Slot >= Type::FirstDerivedTyID && "Derived type not in range!");
113 output_vbr((unsigned)Slot, Out);
115 output_vbr((unsigned)Table.getValSlot(Type::VoidTy), Out);
119 bool BytecodeWriter::processMethod(const Method *M) {
120 BytecodeBlock MethodBlock(BytecodeFormat::Method, Out);
122 Table.incorporateMethod(M);
124 if (ModuleAnalyzer::processMethod(M)) return true;
126 // If needed, output the symbol table for the method...
127 if (M->hasSymbolTable())
128 outputSymbolTable(*M->getSymbolTable());
135 bool BytecodeWriter::processBasicBlock(const BasicBlock *BB) {
136 BytecodeBlock MethodBlock(BytecodeFormat::BasicBlock, Out);
137 return ModuleAnalyzer::processBasicBlock(BB);
140 void BytecodeWriter::outputSymbolTable(const SymbolTable &MST) {
141 BytecodeBlock MethodBlock(BytecodeFormat::SymbolTable, Out);
143 for (SymbolTable::const_iterator TI = MST.begin(); TI != MST.end(); ++TI) {
144 SymbolTable::type_const_iterator I = MST.type_begin(TI->first);
145 SymbolTable::type_const_iterator End = MST.type_end(TI->first);
148 if (I == End) continue; // Don't mess with an absent type...
150 // Symtab block header: [num entries][type id number]
151 output_vbr(MST.type_size(TI->first), Out);
153 Slot = Table.getValSlot(TI->first);
154 assert(Slot != -1 && "Type in symtab, but not in table!");
155 output_vbr((unsigned)Slot, Out);
157 for (; I != End; ++I) {
158 // Symtab entry: [def slot #][name]
159 Slot = Table.getValSlot(I->second);
160 assert (Slot != -1 && "Value in symtab but not in method!!");
161 output_vbr((unsigned)Slot, Out);
162 output(I->first, Out, false); // Don't force alignment...
167 void WriteBytecodeToFile(const Module *C, ostream &Out) {
168 assert(C && "You can't write a null class!!");
170 vector<unsigned char> Buffer;
172 // This object populates buffer for us...
173 BytecodeWriter BCW(Buffer, C);
175 // Okay, write the vector out to the ostream now...
176 Out.write(&Buffer[0], Buffer.size());