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 |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 9 | // to a deque of unsigned char's, then copies the deque to an ostream. The |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 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 | // |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 14 | // 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 Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 18 | // |
| 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 Lattner | 70cc339 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 27 | #include "llvm/GlobalVariable.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 28 | #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 Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 33 | #include "llvm/Support/STLExtras.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 34 | #include <string.h> |
| 35 | #include <algorithm> |
| 36 | |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 37 | BytecodeWriter::BytecodeWriter(deque<unsigned char> &o, const Module *M) |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 38 | : Out(o), Table(M, false) { |
| 39 | |
| 40 | outputSignature(); |
| 41 | |
| 42 | // Emit the top level CLASS block. |
| 43 | BytecodeBlock ModuleBlock(BytecodeFormat::Module, Out); |
| 44 | |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 45 | // Output the ID of first "derived" type: |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 46 | output_vbr((unsigned)Type::FirstDerivedTyID, Out); |
| 47 | align32(Out); |
| 48 | |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 49 | // Output module level constants, including types used by the method protos |
| 50 | outputConstants(false); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 51 | |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 52 | // 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 Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 60 | if (M->hasSymbolTable()) |
| 61 | outputSymbolTable(*M->getSymbolTable()); |
| 62 | } |
| 63 | |
| 64 | // TODO: REMOVE |
| 65 | #include "llvm/Assembly/Writer.h" |
| 66 | |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 67 | void BytecodeWriter::outputConstants(bool isMethod) { |
| 68 | BytecodeBlock CPool(BytecodeFormat::ConstantPool, Out); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 69 | |
| 70 | unsigned NumPlanes = Table.getNumPlanes(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 71 | for (unsigned pno = 0; pno < NumPlanes; pno++) { |
| 72 | const vector<const Value*> &Plane = Table.getPlane(pno); |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 73 | if (Plane.empty()) continue; // Skip empty type planes... |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 74 | |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 75 | unsigned ValNo = 0; |
| 76 | if (isMethod) // Don't reemit module constants |
| 77 | ValNo = Table.getModuleLevel(pno); |
| 78 | else if (pno == Type::TypeTyID) |
| 79 | ValNo = Type::FirstDerivedTyID; // Start emitting at the derived types... |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 80 | |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 81 | // Scan through and ignore method arguments... |
Chris Lattner | 1d87bcf | 2001-10-01 20:11:19 +0000 | [diff] [blame^] | 82 | for (; ValNo < Plane.size() && isa<MethodArgument>(Plane[ValNo]); ValNo++) |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 83 | /*empty*/; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 84 | |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 85 | unsigned NC = ValNo; // Number of constants |
| 86 | for (; NC < Plane.size() && |
Chris Lattner | cfe26c9 | 2001-10-01 18:26:53 +0000 | [diff] [blame] | 87 | (isa<ConstPoolVal>(Plane[NC]) || |
| 88 | isa<Type>(Plane[NC])); NC++) /*empty*/; |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 89 | NC -= ValNo; // Convert from index into count |
| 90 | if (NC == 0) continue; // Skip empty type planes... |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 91 | |
| 92 | // Output type header: [num entries][type id number] |
| 93 | // |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 94 | output_vbr(NC, Out); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 95 | |
| 96 | // Output the Type ID Number... |
| 97 | int Slot = Table.getValSlot(Plane.front()->getType()); |
| 98 | assert (Slot != -1 && "Type in constant pool but not in method!!"); |
| 99 | output_vbr((unsigned)Slot, Out); |
| 100 | |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 101 | //cout << "Emitting " << NC << " constants of type '" |
| 102 | // << Plane.front()->getType()->getName() << "' = Slot #" << Slot << endl; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 103 | |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 104 | for (unsigned i = ValNo; i < ValNo+NC; ++i) { |
| 105 | const Value *V = Plane[i]; |
Chris Lattner | 1d87bcf | 2001-10-01 20:11:19 +0000 | [diff] [blame^] | 106 | if (const ConstPoolVal *CPV = dyn_cast<ConstPoolVal>(V)) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 107 | //cerr << "Serializing value: <" << V->getType() << ">: " |
| 108 | // << ((const ConstPoolVal*)V)->getStrValue() << ":" |
| 109 | // << Out.size() << "\n"; |
Chris Lattner | 7fc9fe3 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 110 | outputConstant(CPV); |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 111 | } else { |
Chris Lattner | 9636a91 | 2001-10-01 16:18:37 +0000 | [diff] [blame] | 112 | const Type *Ty = cast<const Type>(V); |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 113 | outputType(Ty); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | void BytecodeWriter::outputModuleInfoBlock(const Module *M) { |
| 120 | BytecodeBlock ModuleInfoBlock(BytecodeFormat::ModuleGlobalInfo, Out); |
| 121 | |
Chris Lattner | 70cc339 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 122 | // Output the types for the global variables in the module... |
| 123 | for (Module::const_giterator I = M->gbegin(), End = M->gend(); I != End;++I) { |
Chris Lattner | d70684f | 2001-09-18 04:01:05 +0000 | [diff] [blame] | 124 | const GlobalVariable *GV = *I; |
| 125 | int Slot = Table.getValSlot(GV->getType()); |
Chris Lattner | 70cc339 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 126 | assert(Slot != -1 && "Module global vars is broken!"); |
Chris Lattner | d70684f | 2001-09-18 04:01:05 +0000 | [diff] [blame] | 127 | |
| 128 | // Fields: bit0 = isConstant, bit1 = hasInitializer, bit2+ = slot# |
| 129 | unsigned oSlot = ((unsigned)Slot << 2) | (GV->hasInitializer() << 1) | |
Chris Lattner | 1d87bcf | 2001-10-01 20:11:19 +0000 | [diff] [blame^] | 130 | isa<ConstPoolVal>(GV); |
Chris Lattner | d70684f | 2001-09-18 04:01:05 +0000 | [diff] [blame] | 131 | output_vbr(oSlot, Out); |
| 132 | |
| 133 | // If we have an initialized, output it now. |
| 134 | if (GV->hasInitializer()) { |
| 135 | Slot = Table.getValSlot(GV->getInitializer()); |
| 136 | assert(Slot != -1 && "No slot for global var initializer!"); |
| 137 | output_vbr((unsigned)Slot, Out); |
| 138 | } |
Chris Lattner | 70cc339 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 139 | } |
| 140 | output_vbr((unsigned)Table.getValSlot(Type::VoidTy), Out); |
| 141 | |
| 142 | // Output the types of the methods in this module... |
Chris Lattner | 7fc9fe3 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 143 | 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] | 144 | int Slot = Table.getValSlot((*I)->getType()); |
| 145 | assert(Slot != -1 && "Module const pool is broken!"); |
| 146 | assert(Slot >= Type::FirstDerivedTyID && "Derived type not in range!"); |
| 147 | output_vbr((unsigned)Slot, Out); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 148 | } |
| 149 | output_vbr((unsigned)Table.getValSlot(Type::VoidTy), Out); |
Chris Lattner | 70cc339 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 150 | |
| 151 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 152 | align32(Out); |
| 153 | } |
| 154 | |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 155 | void BytecodeWriter::processMethod(const Method *M) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 156 | BytecodeBlock MethodBlock(BytecodeFormat::Method, Out); |
| 157 | |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 158 | // Only output the constant pool and other goodies if needed... |
| 159 | if (!M->isExternal()) { |
| 160 | // Get slot information about the method... |
| 161 | Table.incorporateMethod(M); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 162 | |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 163 | // Output information about the constants in the method... |
| 164 | outputConstants(true); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 165 | |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 166 | // Output basic block nodes... |
| 167 | for_each(M->begin(), M->end(), |
| 168 | bind_obj(this, &BytecodeWriter::processBasicBlock)); |
| 169 | |
| 170 | // If needed, output the symbol table for the method... |
| 171 | if (M->hasSymbolTable()) |
| 172 | outputSymbolTable(*M->getSymbolTable()); |
| 173 | |
| 174 | Table.purgeMethod(); |
| 175 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 179 | void BytecodeWriter::processBasicBlock(const BasicBlock *BB) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 180 | BytecodeBlock MethodBlock(BytecodeFormat::BasicBlock, Out); |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 181 | // Process all the instructions in the bb... |
| 182 | for_each(BB->begin(), BB->end(), |
| 183 | bind_obj(this, &BytecodeWriter::processInstruction)); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | void BytecodeWriter::outputSymbolTable(const SymbolTable &MST) { |
| 187 | BytecodeBlock MethodBlock(BytecodeFormat::SymbolTable, Out); |
| 188 | |
Chris Lattner | 7fc9fe3 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 189 | for (SymbolTable::const_iterator TI = MST.begin(); TI != MST.end(); ++TI) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 190 | SymbolTable::type_const_iterator I = MST.type_begin(TI->first); |
| 191 | SymbolTable::type_const_iterator End = MST.type_end(TI->first); |
| 192 | int Slot; |
| 193 | |
| 194 | if (I == End) continue; // Don't mess with an absent type... |
| 195 | |
| 196 | // Symtab block header: [num entries][type id number] |
| 197 | output_vbr(MST.type_size(TI->first), Out); |
| 198 | |
| 199 | Slot = Table.getValSlot(TI->first); |
| 200 | assert(Slot != -1 && "Type in symtab, but not in table!"); |
| 201 | output_vbr((unsigned)Slot, Out); |
| 202 | |
Chris Lattner | 7fc9fe3 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 203 | for (; I != End; ++I) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 204 | // Symtab entry: [def slot #][name] |
| 205 | Slot = Table.getValSlot(I->second); |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 206 | assert(Slot != -1 && "Value in symtab but has no slot number!!"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 207 | output_vbr((unsigned)Slot, Out); |
| 208 | output(I->first, Out, false); // Don't force alignment... |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | void WriteBytecodeToFile(const Module *C, ostream &Out) { |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 214 | assert(C && "You can't write a null module!!"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 215 | |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 216 | deque<unsigned char> Buffer; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 217 | |
| 218 | // This object populates buffer for us... |
| 219 | BytecodeWriter BCW(Buffer, C); |
| 220 | |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 221 | // Okay, write the deque out to the ostream now... the deque is not |
| 222 | // sequential in memory, however, so write out as much as possible in big |
| 223 | // chunks, until we're done. |
| 224 | // |
| 225 | deque<unsigned char>::const_iterator I = Buffer.begin(), E = Buffer.end(); |
| 226 | while (I != E) { // Loop until it's all written |
| 227 | // Scan to see how big this chunk is... |
| 228 | const unsigned char *ChunkPtr = &*I; |
| 229 | const unsigned char *LastPtr = ChunkPtr; |
| 230 | while (I != E) { |
| 231 | const unsigned char *ThisPtr = &*++I; |
| 232 | if (LastPtr+1 != ThisPtr) break;// Advanced by more than a byte of memory? |
| 233 | LastPtr = ThisPtr; |
| 234 | } |
| 235 | |
| 236 | // Write out the chunk... |
| 237 | Out.write(ChunkPtr, LastPtr-ChunkPtr+(I != E)); |
| 238 | } |
| 239 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 240 | Out.flush(); |
| 241 | } |