Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 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++) |
Chris Lattner | 7fc9fe3 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 70 | if (Plane[vn]->isConstant()) |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 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]; |
Chris Lattner | 7fc9fe3 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 88 | if (const ConstPoolVal *CPV = V->castConstant()) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 89 | //cerr << "Serializing value: <" << V->getType() << ">: " |
| 90 | // << ((const ConstPoolVal*)V)->getStrValue() << ":" |
| 91 | // << Out.size() << "\n"; |
Chris Lattner | 7fc9fe3 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 92 | outputConstant(CPV); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | delete CPool; // End bytecode block section! |
| 98 | |
Chris Lattner | 7fc9fe3 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 99 | if (!isMethod) // The ModuleInfoBlock follows directly after the c-pool |
Chris Lattner | e545981 | 2001-07-14 06:10:49 +0000 | [diff] [blame] | 100 | outputModuleInfoBlock(CP.getParentV()->castModuleAsserting()); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 101 | |
| 102 | return false; |
| 103 | } |
| 104 | |
| 105 | void BytecodeWriter::outputModuleInfoBlock(const Module *M) { |
| 106 | BytecodeBlock ModuleInfoBlock(BytecodeFormat::ModuleGlobalInfo, Out); |
| 107 | |
| 108 | // Output the types of the methods in this class |
Chris Lattner | 7fc9fe3 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 109 | for (Module::const_iterator I = M->begin(), End = M->end(); I != End; ++I) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 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); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 114 | } |
| 115 | output_vbr((unsigned)Table.getValSlot(Type::VoidTy), Out); |
| 116 | align32(Out); |
| 117 | } |
| 118 | |
| 119 | bool BytecodeWriter::processMethod(const Method *M) { |
| 120 | BytecodeBlock MethodBlock(BytecodeFormat::Method, Out); |
| 121 | |
| 122 | Table.incorporateMethod(M); |
| 123 | |
| 124 | if (ModuleAnalyzer::processMethod(M)) return true; |
| 125 | |
| 126 | // If needed, output the symbol table for the method... |
| 127 | if (M->hasSymbolTable()) |
| 128 | outputSymbolTable(*M->getSymbolTable()); |
| 129 | |
| 130 | Table.purgeMethod(); |
| 131 | return false; |
| 132 | } |
| 133 | |
| 134 | |
| 135 | bool BytecodeWriter::processBasicBlock(const BasicBlock *BB) { |
| 136 | BytecodeBlock MethodBlock(BytecodeFormat::BasicBlock, Out); |
| 137 | return ModuleAnalyzer::processBasicBlock(BB); |
| 138 | } |
| 139 | |
| 140 | void BytecodeWriter::outputSymbolTable(const SymbolTable &MST) { |
| 141 | BytecodeBlock MethodBlock(BytecodeFormat::SymbolTable, Out); |
| 142 | |
Chris Lattner | 7fc9fe3 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 143 | for (SymbolTable::const_iterator TI = MST.begin(); TI != MST.end(); ++TI) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 144 | SymbolTable::type_const_iterator I = MST.type_begin(TI->first); |
| 145 | SymbolTable::type_const_iterator End = MST.type_end(TI->first); |
| 146 | int Slot; |
| 147 | |
| 148 | if (I == End) continue; // Don't mess with an absent type... |
| 149 | |
| 150 | // Symtab block header: [num entries][type id number] |
| 151 | output_vbr(MST.type_size(TI->first), Out); |
| 152 | |
| 153 | Slot = Table.getValSlot(TI->first); |
| 154 | assert(Slot != -1 && "Type in symtab, but not in table!"); |
| 155 | output_vbr((unsigned)Slot, Out); |
| 156 | |
Chris Lattner | 7fc9fe3 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 157 | for (; I != End; ++I) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 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... |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | void WriteBytecodeToFile(const Module *C, ostream &Out) { |
| 168 | assert(C && "You can't write a null class!!"); |
| 169 | |
| 170 | vector<unsigned char> Buffer; |
| 171 | |
| 172 | // This object populates buffer for us... |
| 173 | BytecodeWriter BCW(Buffer, C); |
| 174 | |
| 175 | // Okay, write the vector out to the ostream now... |
| 176 | Out.write(&Buffer[0], Buffer.size()); |
| 177 | Out.flush(); |
| 178 | } |