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]->getValueType() == Value::ConstantVal)
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 (V->getValueType() == Value::ConstantVal) {
89 //cerr << "Serializing value: <" << V->getType() << ">: "
90 // << ((const ConstPoolVal*)V)->getStrValue() << ":"
91 // << Out.size() << "\n";
92 outputConstant((const ConstPoolVal*)V);
97 delete CPool; // End bytecode block section!
99 if (!isMethod) { // The ModuleInfoBlock follows directly after the c-pool
100 assert(CP.getParent()->getValueType() == Value::ModuleVal);
101 outputModuleInfoBlock((const Module*)CP.getParent());
107 void BytecodeWriter::outputModuleInfoBlock(const Module *M) {
108 BytecodeBlock ModuleInfoBlock(BytecodeFormat::ModuleGlobalInfo, Out);
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);
119 output_vbr((unsigned)Table.getValSlot(Type::VoidTy), Out);
123 bool BytecodeWriter::processMethod(const Method *M) {
124 BytecodeBlock MethodBlock(BytecodeFormat::Method, Out);
126 Table.incorporateMethod(M);
128 if (ModuleAnalyzer::processMethod(M)) return true;
130 // If needed, output the symbol table for the method...
131 if (M->hasSymbolTable())
132 outputSymbolTable(*M->getSymbolTable());
139 bool BytecodeWriter::processBasicBlock(const BasicBlock *BB) {
140 BytecodeBlock MethodBlock(BytecodeFormat::BasicBlock, Out);
141 return ModuleAnalyzer::processBasicBlock(BB);
144 void BytecodeWriter::outputSymbolTable(const SymbolTable &MST) {
145 BytecodeBlock MethodBlock(BytecodeFormat::SymbolTable, Out);
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);
152 if (I == End) continue; // Don't mess with an absent type...
154 // Symtab block header: [num entries][type id number]
155 output_vbr(MST.type_size(TI->first), Out);
157 Slot = Table.getValSlot(TI->first);
158 assert(Slot != -1 && "Type in symtab, but not in table!");
159 output_vbr((unsigned)Slot, Out);
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...
171 void WriteBytecodeToFile(const Module *C, ostream &Out) {
172 assert(C && "You can't write a null class!!");
174 vector<unsigned char> Buffer;
176 // This object populates buffer for us...
177 BytecodeWriter BCW(Buffer, C);
179 // Okay, write the vector out to the ostream now...
180 Out.write(&Buffer[0], Buffer.size());