Chris Lattner | 1499934 | 2004-01-10 19:07:06 +0000 | [diff] [blame] | 1 | //===-- Writer.cpp - Library for writing LLVM bytecode files --------------===// |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 9 | // |
| 10 | // This library implements the functionality defined in llvm/Bytecode/Writer.h |
| 11 | // |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 12 | // Note that this file uses an unusual technique of outputting all the bytecode |
Chris Lattner | abe83ae | 2003-09-15 00:33:20 +0000 | [diff] [blame] | 13 | // to a deque of unsigned char, then copies the deque to an ostream. The |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 14 | // reason for this is that we must do "seeking" in the stream to do back- |
| 15 | // patching, and some very important ostreams that we want to support (like |
| 16 | // pipes) do not support seeking. :( :( :( |
| 17 | // |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 18 | // The choice of the deque data structure is influenced by the extremely fast |
| 19 | // "append" speed, plus the free "seek"/replace in the middle of the stream. I |
| 20 | // didn't use a vector because the stream could end up very large and copying |
| 21 | // the whole thing to reallocate would be kinda silly. |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 22 | // |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 23 | //===----------------------------------------------------------------------===// |
| 24 | |
| 25 | #include "WriterInternals.h" |
Chris Lattner | 635cd93 | 2002-07-23 19:56:44 +0000 | [diff] [blame] | 26 | #include "llvm/Bytecode/WriteBytecodePass.h" |
Chris Lattner | 83bb3d2 | 2004-01-14 23:36:54 +0000 | [diff] [blame] | 27 | #include "llvm/Constants.h" |
| 28 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 29 | #include "llvm/Module.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 30 | #include "llvm/SymbolTable.h" |
Chris Lattner | f60dc88 | 2001-11-29 16:32:16 +0000 | [diff] [blame] | 31 | #include "Support/STLExtras.h" |
Chris Lattner | a92f696 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 32 | #include "Support/Statistic.h" |
Chris Lattner | 32abce6 | 2004-01-10 19:10:01 +0000 | [diff] [blame] | 33 | #include <cstring> |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 34 | #include <algorithm> |
Chris Lattner | 44f549b | 2004-01-10 18:49:43 +0000 | [diff] [blame] | 35 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 36 | |
Chris Lattner | 635cd93 | 2002-07-23 19:56:44 +0000 | [diff] [blame] | 37 | static RegisterPass<WriteBytecodePass> X("emitbytecode", "Bytecode Writer"); |
| 38 | |
Chris Lattner | ce6ef11 | 2002-07-26 18:40:14 +0000 | [diff] [blame] | 39 | static Statistic<> |
Chris Lattner | a92f696 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 40 | BytesWritten("bytecodewriter", "Number of bytecode bytes written"); |
Chris Lattner | 635cd93 | 2002-07-23 19:56:44 +0000 | [diff] [blame] | 41 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 42 | BytecodeWriter::BytecodeWriter(std::deque<unsigned char> &o, const Module *M) |
Reid Spencer | 798ff64 | 2004-05-26 07:37:11 +0000 | [diff] [blame] | 43 | : Out(o), Table(M) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 44 | |
Chris Lattner | 83bb3d2 | 2004-01-14 23:36:54 +0000 | [diff] [blame] | 45 | // Emit the signature... |
| 46 | static const unsigned char *Sig = (const unsigned char*)"llvm"; |
| 47 | output_data(Sig, Sig+4, Out); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 48 | |
| 49 | // Emit the top level CLASS block. |
| 50 | BytecodeBlock ModuleBlock(BytecodeFormat::Module, Out); |
| 51 | |
Chris Lattner | d445c6b | 2003-08-24 13:47:36 +0000 | [diff] [blame] | 52 | bool isBigEndian = M->getEndianness() == Module::BigEndian; |
| 53 | bool hasLongPointers = M->getPointerSize() == Module::Pointer64; |
| 54 | bool hasNoEndianness = M->getEndianness() == Module::AnyEndianness; |
| 55 | bool hasNoPointerSize = M->getPointerSize() == Module::AnyPointerSize; |
Chris Lattner | 186a1f7 | 2003-03-19 20:56:46 +0000 | [diff] [blame] | 56 | |
Chris Lattner | 5fa428f | 2004-04-05 01:27:26 +0000 | [diff] [blame] | 57 | // Output the version identifier... we are currently on bytecode version #2, |
| 58 | // which corresponds to LLVM v1.3. |
Chris Lattner | 036de03 | 2004-06-25 20:52:10 +0000 | [diff] [blame] | 59 | unsigned Version = (2 << 4) | (unsigned)isBigEndian | (hasLongPointers << 1) | |
Chris Lattner | d445c6b | 2003-08-24 13:47:36 +0000 | [diff] [blame] | 60 | (hasNoEndianness << 2) | (hasNoPointerSize << 3); |
Chris Lattner | 186a1f7 | 2003-03-19 20:56:46 +0000 | [diff] [blame] | 61 | output_vbr(Version, Out); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 62 | align32(Out); |
| 63 | |
Reid Spencer | cb3595c | 2004-07-04 11:45:47 +0000 | [diff] [blame] | 64 | // The Global type plane comes first |
Chris Lattner | 186a1f7 | 2003-03-19 20:56:46 +0000 | [diff] [blame] | 65 | { |
Reid Spencer | cb3595c | 2004-07-04 11:45:47 +0000 | [diff] [blame] | 66 | BytecodeBlock CPool(BytecodeFormat::GlobalTypePlane, Out ); |
| 67 | outputTypes(Type::FirstDerivedTyID); |
Chris Lattner | 186a1f7 | 2003-03-19 20:56:46 +0000 | [diff] [blame] | 68 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 69 | |
Chris Lattner | 186a1f7 | 2003-03-19 20:56:46 +0000 | [diff] [blame] | 70 | // The ModuleInfoBlock follows directly after the type information |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 71 | outputModuleInfoBlock(M); |
| 72 | |
Chris Lattner | 186a1f7 | 2003-03-19 20:56:46 +0000 | [diff] [blame] | 73 | // Output module level constants, used for global variable initializers |
| 74 | outputConstants(false); |
| 75 | |
Chris Lattner | b579400 | 2002-04-07 22:49:37 +0000 | [diff] [blame] | 76 | // Do the whole module now! Process each function at a time... |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 77 | for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) |
Chris Lattner | 186a1f7 | 2003-03-19 20:56:46 +0000 | [diff] [blame] | 78 | outputFunction(I); |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 79 | |
| 80 | // If needed, output the symbol table for the module... |
Chris Lattner | 6e6026b | 2002-11-20 18:36:02 +0000 | [diff] [blame] | 81 | outputSymbolTable(M->getSymbolTable()); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Reid Spencer | cb3595c | 2004-07-04 11:45:47 +0000 | [diff] [blame] | 84 | void BytecodeWriter::outputTypes(unsigned TypeNum) |
| 85 | { |
| 86 | // Write the type plane for types first because earlier planes (e.g. for a |
| 87 | // primitive type like float) may have constants constructed using types |
| 88 | // coming later (e.g., via getelementptr from a pointer type). The type |
| 89 | // plane is needed before types can be fwd or bkwd referenced. |
| 90 | const std::vector<const Type*>& Types = Table.getTypes(); |
| 91 | assert(!Types.empty() && "No types at all?"); |
| 92 | assert(TypeNum <= Types.size() && "Invalid TypeNo index"); |
| 93 | |
| 94 | unsigned NumEntries = Types.size() - TypeNum; |
| 95 | |
| 96 | // Output type header: [num entries] |
| 97 | output_vbr(NumEntries, Out); |
| 98 | |
| 99 | for (unsigned i = TypeNum; i < TypeNum+NumEntries; ++i) |
| 100 | outputType(Types[i]); |
| 101 | } |
| 102 | |
Vikram S. Adve | a7dac3d | 2002-07-14 23:07:51 +0000 | [diff] [blame] | 103 | // Helper function for outputConstants(). |
| 104 | // Writes out all the constants in the plane Plane starting at entry StartNo. |
| 105 | // |
| 106 | void BytecodeWriter::outputConstantsInPlane(const std::vector<const Value*> |
| 107 | &Plane, unsigned StartNo) { |
| 108 | unsigned ValNo = StartNo; |
| 109 | |
Chris Lattner | 83bb3d2 | 2004-01-14 23:36:54 +0000 | [diff] [blame] | 110 | // Scan through and ignore function arguments, global values, and constant |
| 111 | // strings. |
| 112 | for (; ValNo < Plane.size() && |
| 113 | (isa<Argument>(Plane[ValNo]) || isa<GlobalValue>(Plane[ValNo]) || |
| 114 | (isa<ConstantArray>(Plane[ValNo]) && |
| 115 | cast<ConstantArray>(Plane[ValNo])->isString())); ValNo++) |
Vikram S. Adve | a7dac3d | 2002-07-14 23:07:51 +0000 | [diff] [blame] | 116 | /*empty*/; |
| 117 | |
| 118 | unsigned NC = ValNo; // Number of constants |
Reid Spencer | cb3595c | 2004-07-04 11:45:47 +0000 | [diff] [blame] | 119 | for (; NC < Plane.size() && (isa<Constant>(Plane[NC])); NC++) |
Vikram S. Adve | a7dac3d | 2002-07-14 23:07:51 +0000 | [diff] [blame] | 120 | /*empty*/; |
| 121 | NC -= ValNo; // Convert from index into count |
| 122 | if (NC == 0) return; // Skip empty type planes... |
| 123 | |
Chris Lattner | d6942d7 | 2004-01-14 16:54:21 +0000 | [diff] [blame] | 124 | // FIXME: Most slabs only have 1 or 2 entries! We should encode this much |
| 125 | // more compactly. |
| 126 | |
Vikram S. Adve | a7dac3d | 2002-07-14 23:07:51 +0000 | [diff] [blame] | 127 | // Output type header: [num entries][type id number] |
| 128 | // |
| 129 | output_vbr(NC, Out); |
| 130 | |
| 131 | // Output the Type ID Number... |
Alkis Evlogimenos | 6059638 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 132 | int Slot = Table.getSlot(Plane.front()->getType()); |
Vikram S. Adve | a7dac3d | 2002-07-14 23:07:51 +0000 | [diff] [blame] | 133 | assert (Slot != -1 && "Type in constant pool but not in function!!"); |
| 134 | output_vbr((unsigned)Slot, Out); |
| 135 | |
Vikram S. Adve | a7dac3d | 2002-07-14 23:07:51 +0000 | [diff] [blame] | 136 | for (unsigned i = ValNo; i < ValNo+NC; ++i) { |
| 137 | const Value *V = Plane[i]; |
Reid Spencer | e0125b6 | 2004-07-18 00:16:21 +0000 | [diff] [blame] | 138 | if (const Constant *C = dyn_cast<Constant>(V)) { |
| 139 | outputConstant(C); |
Vikram S. Adve | a7dac3d | 2002-07-14 23:07:51 +0000 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
Chris Lattner | 80b9734 | 2004-01-17 23:25:43 +0000 | [diff] [blame] | 144 | static inline bool hasNullValue(unsigned TyID) { |
Reid Spencer | cb3595c | 2004-07-04 11:45:47 +0000 | [diff] [blame] | 145 | return TyID != Type::LabelTyID && TyID != Type::VoidTyID; |
Chris Lattner | 80b9734 | 2004-01-17 23:25:43 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 148 | void BytecodeWriter::outputConstants(bool isFunction) { |
Chris Lattner | 0baa0af | 2004-01-15 21:06:57 +0000 | [diff] [blame] | 149 | BytecodeBlock CPool(BytecodeFormat::ConstantPool, Out, |
| 150 | true /* Elide block if empty */); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 151 | |
| 152 | unsigned NumPlanes = Table.getNumPlanes(); |
Chris Lattner | f69315b | 2003-05-22 18:35:38 +0000 | [diff] [blame] | 153 | |
Reid Spencer | e0125b6 | 2004-07-18 00:16:21 +0000 | [diff] [blame] | 154 | if (isFunction) |
| 155 | // Output the type plane before any constants! |
Reid Spencer | cb3595c | 2004-07-04 11:45:47 +0000 | [diff] [blame] | 156 | outputTypes( Table.getModuleTypeLevel() ); |
Reid Spencer | e0125b6 | 2004-07-18 00:16:21 +0000 | [diff] [blame] | 157 | else |
| 158 | // Output module-level string constants before any other constants.x |
Chris Lattner | 83bb3d2 | 2004-01-14 23:36:54 +0000 | [diff] [blame] | 159 | outputConstantStrings(); |
| 160 | |
Reid Spencer | cb3595c | 2004-07-04 11:45:47 +0000 | [diff] [blame] | 161 | for (unsigned pno = 0; pno != NumPlanes; pno++) { |
| 162 | const std::vector<const Value*> &Plane = Table.getPlane(pno); |
| 163 | if (!Plane.empty()) { // Skip empty type planes... |
| 164 | unsigned ValNo = 0; |
| 165 | if (isFunction) // Don't re-emit module constants |
Reid Spencer | 0852c80 | 2004-07-04 11:46:15 +0000 | [diff] [blame] | 166 | ValNo += Table.getModuleLevel(pno); |
Reid Spencer | cb3595c | 2004-07-04 11:45:47 +0000 | [diff] [blame] | 167 | |
| 168 | if (hasNullValue(pno)) { |
Reid Spencer | 0852c80 | 2004-07-04 11:46:15 +0000 | [diff] [blame] | 169 | // Skip zero initializer |
| 170 | if (ValNo == 0) |
| 171 | ValNo = 1; |
Chris Lattner | f69315b | 2003-05-22 18:35:38 +0000 | [diff] [blame] | 172 | } |
Reid Spencer | cb3595c | 2004-07-04 11:45:47 +0000 | [diff] [blame] | 173 | |
| 174 | // Write out constants in the plane |
| 175 | outputConstantsInPlane(Plane, ValNo); |
Chris Lattner | f69315b | 2003-05-22 18:35:38 +0000 | [diff] [blame] | 176 | } |
Reid Spencer | cb3595c | 2004-07-04 11:45:47 +0000 | [diff] [blame] | 177 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Chris Lattner | 6b25242 | 2003-10-16 18:28:50 +0000 | [diff] [blame] | 180 | static unsigned getEncodedLinkage(const GlobalValue *GV) { |
| 181 | switch (GV->getLinkage()) { |
| 182 | default: assert(0 && "Invalid linkage!"); |
| 183 | case GlobalValue::ExternalLinkage: return 0; |
Chris Lattner | 6b25242 | 2003-10-16 18:28:50 +0000 | [diff] [blame] | 184 | case GlobalValue::WeakLinkage: return 1; |
| 185 | case GlobalValue::AppendingLinkage: return 2; |
| 186 | case GlobalValue::InternalLinkage: return 3; |
Chris Lattner | 22482a1 | 2003-10-18 06:30:21 +0000 | [diff] [blame] | 187 | case GlobalValue::LinkOnceLinkage: return 4; |
Chris Lattner | 6b25242 | 2003-10-16 18:28:50 +0000 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 191 | void BytecodeWriter::outputModuleInfoBlock(const Module *M) { |
| 192 | BytecodeBlock ModuleInfoBlock(BytecodeFormat::ModuleGlobalInfo, Out); |
| 193 | |
Chris Lattner | 70cc339 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 194 | // Output the types for the global variables in the module... |
| 195 | for (Module::const_giterator I = M->gbegin(), End = M->gend(); I != End;++I) { |
Alkis Evlogimenos | 6059638 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 196 | int Slot = Table.getSlot(I->getType()); |
Chris Lattner | 70cc339 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 197 | assert(Slot != -1 && "Module global vars is broken!"); |
Chris Lattner | d70684f | 2001-09-18 04:01:05 +0000 | [diff] [blame] | 198 | |
Chris Lattner | 22482a1 | 2003-10-18 06:30:21 +0000 | [diff] [blame] | 199 | // Fields: bit0 = isConstant, bit1 = hasInitializer, bit2-4=Linkage, |
| 200 | // bit5+ = Slot # for type |
| 201 | unsigned oSlot = ((unsigned)Slot << 5) | (getEncodedLinkage(I) << 2) | |
Chris Lattner | 036de03 | 2004-06-25 20:52:10 +0000 | [diff] [blame] | 202 | (I->hasInitializer() << 1) | (unsigned)I->isConstant(); |
Chris Lattner | d70684f | 2001-09-18 04:01:05 +0000 | [diff] [blame] | 203 | output_vbr(oSlot, Out); |
| 204 | |
Chris Lattner | 1b98c5c | 2001-10-13 06:48:38 +0000 | [diff] [blame] | 205 | // If we have an initializer, output it now. |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 206 | if (I->hasInitializer()) { |
Alkis Evlogimenos | 6059638 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 207 | Slot = Table.getSlot((Value*)I->getInitializer()); |
Chris Lattner | d70684f | 2001-09-18 04:01:05 +0000 | [diff] [blame] | 208 | assert(Slot != -1 && "No slot for global var initializer!"); |
| 209 | output_vbr((unsigned)Slot, Out); |
| 210 | } |
Chris Lattner | 70cc339 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 211 | } |
Alkis Evlogimenos | 6059638 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 212 | output_vbr((unsigned)Table.getSlot(Type::VoidTy), Out); |
Chris Lattner | 70cc339 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 213 | |
Chris Lattner | b579400 | 2002-04-07 22:49:37 +0000 | [diff] [blame] | 214 | // Output the types of the functions in this module... |
Chris Lattner | 7fc9fe3 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 215 | for (Module::const_iterator I = M->begin(), End = M->end(); I != End; ++I) { |
Alkis Evlogimenos | 6059638 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 216 | int Slot = Table.getSlot(I->getType()); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 217 | assert(Slot != -1 && "Module const pool is broken!"); |
| 218 | assert(Slot >= Type::FirstDerivedTyID && "Derived type not in range!"); |
| 219 | output_vbr((unsigned)Slot, Out); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 220 | } |
Alkis Evlogimenos | 6059638 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 221 | output_vbr((unsigned)Table.getSlot(Type::VoidTy), Out); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Chris Lattner | cf3e67f | 2004-01-18 21:08:52 +0000 | [diff] [blame] | 224 | void BytecodeWriter::outputInstructions(const Function *F) { |
| 225 | BytecodeBlock ILBlock(BytecodeFormat::InstructionList, Out); |
Chris Lattner | cf3e67f | 2004-01-18 21:08:52 +0000 | [diff] [blame] | 226 | for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB) |
| 227 | for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) |
| 228 | outputInstruction(*I); |
Chris Lattner | cf3e67f | 2004-01-18 21:08:52 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Chris Lattner | 186a1f7 | 2003-03-19 20:56:46 +0000 | [diff] [blame] | 231 | void BytecodeWriter::outputFunction(const Function *F) { |
Chris Lattner | c9aa7df | 2002-03-29 03:51:11 +0000 | [diff] [blame] | 232 | BytecodeBlock FunctionBlock(BytecodeFormat::Function, Out); |
Chris Lattner | 6b25242 | 2003-10-16 18:28:50 +0000 | [diff] [blame] | 233 | output_vbr(getEncodedLinkage(F), Out); |
Chris Lattner | d23b1d3 | 2001-11-26 18:56:10 +0000 | [diff] [blame] | 234 | |
Chris Lattner | cf3e67f | 2004-01-18 21:08:52 +0000 | [diff] [blame] | 235 | // If this is an external function, there is nothing else to emit! |
| 236 | if (F->isExternal()) return; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 237 | |
Chris Lattner | cf3e67f | 2004-01-18 21:08:52 +0000 | [diff] [blame] | 238 | // Get slot information about the function... |
| 239 | Table.incorporateFunction(F); |
| 240 | |
| 241 | if (Table.getCompactionTable().empty()) { |
| 242 | // Output information about the constants in the function if the compaction |
| 243 | // table is not being used. |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 244 | outputConstants(true); |
Chris Lattner | cf3e67f | 2004-01-18 21:08:52 +0000 | [diff] [blame] | 245 | } else { |
| 246 | // Otherwise, emit the compaction table. |
| 247 | outputCompactionTable(); |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 248 | } |
Chris Lattner | cf3e67f | 2004-01-18 21:08:52 +0000 | [diff] [blame] | 249 | |
| 250 | // Output all of the instructions in the body of the function |
| 251 | outputInstructions(F); |
| 252 | |
| 253 | // If needed, output the symbol table for the function... |
| 254 | outputSymbolTable(F->getSymbolTable()); |
| 255 | |
| 256 | Table.purgeFunction(); |
| 257 | } |
| 258 | |
| 259 | void BytecodeWriter::outputCompactionTablePlane(unsigned PlaneNo, |
| 260 | const std::vector<const Value*> &Plane, |
| 261 | unsigned StartNo) { |
| 262 | unsigned End = Table.getModuleLevel(PlaneNo); |
Chris Lattner | 52f86d6 | 2004-01-20 00:54:06 +0000 | [diff] [blame] | 263 | if (Plane.empty() || StartNo == End || End == 0) return; // Nothing to emit |
Chris Lattner | cf3e67f | 2004-01-18 21:08:52 +0000 | [diff] [blame] | 264 | assert(StartNo < End && "Cannot emit negative range!"); |
| 265 | assert(StartNo < Plane.size() && End <= Plane.size()); |
| 266 | |
Chris Lattner | cf3e67f | 2004-01-18 21:08:52 +0000 | [diff] [blame] | 267 | // Do not emit the null initializer! |
Reid Spencer | cb3595c | 2004-07-04 11:45:47 +0000 | [diff] [blame] | 268 | ++StartNo; |
Chris Lattner | cf3e67f | 2004-01-18 21:08:52 +0000 | [diff] [blame] | 269 | |
Chris Lattner | 2410243 | 2004-01-18 22:35:34 +0000 | [diff] [blame] | 270 | // Figure out which encoding to use. By far the most common case we have is |
| 271 | // to emit 0-2 entries in a compaction table plane. |
| 272 | switch (End-StartNo) { |
| 273 | case 0: // Avoid emitting two vbr's if possible. |
| 274 | case 1: |
| 275 | case 2: |
| 276 | output_vbr((PlaneNo << 2) | End-StartNo, Out); |
| 277 | break; |
| 278 | default: |
| 279 | // Output the number of things. |
| 280 | output_vbr((unsigned(End-StartNo) << 2) | 3, Out); |
| 281 | output_vbr(PlaneNo, Out); // Emit the type plane this is |
| 282 | break; |
| 283 | } |
| 284 | |
Chris Lattner | cf3e67f | 2004-01-18 21:08:52 +0000 | [diff] [blame] | 285 | for (unsigned i = StartNo; i != End; ++i) |
| 286 | output_vbr(Table.getGlobalSlot(Plane[i]), Out); |
| 287 | } |
| 288 | |
Reid Spencer | cb3595c | 2004-07-04 11:45:47 +0000 | [diff] [blame] | 289 | void BytecodeWriter::outputCompactionTypes(unsigned StartNo) { |
| 290 | // Get the compaction type table from the slot calculator |
| 291 | const std::vector<const Type*> &CTypes = Table.getCompactionTypes(); |
| 292 | |
| 293 | // The compaction types may have been uncompactified back to the |
| 294 | // global types. If so, we just write an empty table |
| 295 | if (CTypes.size() == 0 ) { |
| 296 | output_vbr(0U, Out); |
| 297 | return; |
| 298 | } |
| 299 | |
| 300 | assert(CTypes.size() >= StartNo && "Invalid compaction types start index"); |
| 301 | |
| 302 | // Determine how many types to write |
| 303 | unsigned NumTypes = CTypes.size() - StartNo; |
| 304 | |
| 305 | // Output the number of types. |
| 306 | output_vbr(NumTypes, Out); |
| 307 | |
| 308 | for (unsigned i = StartNo; i < StartNo+NumTypes; ++i) |
| 309 | output_vbr(Table.getGlobalSlot(CTypes[i]), Out); |
| 310 | } |
| 311 | |
Chris Lattner | cf3e67f | 2004-01-18 21:08:52 +0000 | [diff] [blame] | 312 | void BytecodeWriter::outputCompactionTable() { |
Chris Lattner | cf3e67f | 2004-01-18 21:08:52 +0000 | [diff] [blame] | 313 | BytecodeBlock CTB(BytecodeFormat::CompactionTable, Out, true/*ElideIfEmpty*/); |
| 314 | const std::vector<std::vector<const Value*> > &CT =Table.getCompactionTable(); |
| 315 | |
| 316 | // First thing is first, emit the type compaction table if there is one. |
Reid Spencer | cb3595c | 2004-07-04 11:45:47 +0000 | [diff] [blame] | 317 | outputCompactionTypes(Type::FirstDerivedTyID); |
Chris Lattner | cf3e67f | 2004-01-18 21:08:52 +0000 | [diff] [blame] | 318 | |
| 319 | for (unsigned i = 0, e = CT.size(); i != e; ++i) |
Reid Spencer | cb3595c | 2004-07-04 11:45:47 +0000 | [diff] [blame] | 320 | outputCompactionTablePlane(i, CT[i], 0); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 323 | void BytecodeWriter::outputSymbolTable(const SymbolTable &MST) { |
Chris Lattner | 737d3cd | 2004-01-10 19:56:59 +0000 | [diff] [blame] | 324 | // Do not output the Bytecode block for an empty symbol table, it just wastes |
| 325 | // space! |
Reid Spencer | 6ed81e2 | 2004-05-27 20:18:51 +0000 | [diff] [blame] | 326 | if ( MST.isEmpty() ) return; |
Chris Lattner | 737d3cd | 2004-01-10 19:56:59 +0000 | [diff] [blame] | 327 | |
Chris Lattner | 0baa0af | 2004-01-15 21:06:57 +0000 | [diff] [blame] | 328 | BytecodeBlock SymTabBlock(BytecodeFormat::SymbolTable, Out, |
| 329 | true/* ElideIfEmpty*/); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 330 | |
Reid Spencer | cb3595c | 2004-07-04 11:45:47 +0000 | [diff] [blame] | 331 | //Symtab block header for types: [num entries] |
Reid Spencer | 94f2df2 | 2004-05-25 17:29:59 +0000 | [diff] [blame] | 332 | output_vbr(MST.num_types(), Out); |
Reid Spencer | 94f2df2 | 2004-05-25 17:29:59 +0000 | [diff] [blame] | 333 | for (SymbolTable::type_const_iterator TI = MST.type_begin(), |
| 334 | TE = MST.type_end(); TI != TE; ++TI ) { |
| 335 | //Symtab entry:[def slot #][name] |
| 336 | output_vbr((unsigned)Table.getSlot(TI->second), Out); |
| 337 | output(TI->first, Out, /*align=*/false); |
| 338 | } |
| 339 | |
| 340 | // Now do each of the type planes in order. |
| 341 | for (SymbolTable::plane_const_iterator PI = MST.plane_begin(), |
| 342 | PE = MST.plane_end(); PI != PE; ++PI) { |
| 343 | SymbolTable::value_const_iterator I = MST.value_begin(PI->first); |
| 344 | SymbolTable::value_const_iterator End = MST.value_end(PI->first); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 345 | int Slot; |
| 346 | |
| 347 | if (I == End) continue; // Don't mess with an absent type... |
| 348 | |
| 349 | // Symtab block header: [num entries][type id number] |
Reid Spencer | 94f2df2 | 2004-05-25 17:29:59 +0000 | [diff] [blame] | 350 | output_vbr(MST.type_size(PI->first), Out); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 351 | |
Reid Spencer | 94f2df2 | 2004-05-25 17:29:59 +0000 | [diff] [blame] | 352 | Slot = Table.getSlot(PI->first); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 353 | assert(Slot != -1 && "Type in symtab, but not in table!"); |
| 354 | output_vbr((unsigned)Slot, Out); |
| 355 | |
Chris Lattner | 7fc9fe3 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 356 | for (; I != End; ++I) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 357 | // Symtab entry: [def slot #][name] |
Alkis Evlogimenos | 6059638 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 358 | Slot = Table.getSlot(I->second); |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 359 | assert(Slot != -1 && "Value in symtab but has no slot number!!"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 360 | output_vbr((unsigned)Slot, Out); |
| 361 | output(I->first, Out, false); // Don't force alignment... |
| 362 | } |
| 363 | } |
| 364 | } |
| 365 | |
Chris Lattner | 44f549b | 2004-01-10 18:49:43 +0000 | [diff] [blame] | 366 | void llvm::WriteBytecodeToFile(const Module *C, std::ostream &Out) { |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 367 | assert(C && "You can't write a null module!!"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 368 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 369 | std::deque<unsigned char> Buffer; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 370 | |
| 371 | // This object populates buffer for us... |
| 372 | BytecodeWriter BCW(Buffer, C); |
| 373 | |
Chris Lattner | ce6ef11 | 2002-07-26 18:40:14 +0000 | [diff] [blame] | 374 | // Keep track of how much we've written... |
| 375 | BytesWritten += Buffer.size(); |
| 376 | |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 377 | // Okay, write the deque out to the ostream now... the deque is not |
| 378 | // sequential in memory, however, so write out as much as possible in big |
| 379 | // chunks, until we're done. |
| 380 | // |
Chris Lattner | 036de03 | 2004-06-25 20:52:10 +0000 | [diff] [blame] | 381 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 382 | std::deque<unsigned char>::const_iterator I = Buffer.begin(),E = Buffer.end(); |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 383 | while (I != E) { // Loop until it's all written |
| 384 | // Scan to see how big this chunk is... |
| 385 | const unsigned char *ChunkPtr = &*I; |
| 386 | const unsigned char *LastPtr = ChunkPtr; |
| 387 | while (I != E) { |
| 388 | const unsigned char *ThisPtr = &*++I; |
Chris Lattner | 036de03 | 2004-06-25 20:52:10 +0000 | [diff] [blame] | 389 | if (++LastPtr != ThisPtr) // Advanced by more than a byte of memory? |
Chris Lattner | 5cb1741 | 2001-11-04 21:32:41 +0000 | [diff] [blame] | 390 | break; |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | // Write out the chunk... |
Chris Lattner | d616228 | 2004-06-25 00:35:55 +0000 | [diff] [blame] | 394 | Out.write((char*)ChunkPtr, unsigned(LastPtr-ChunkPtr)); |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 395 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 396 | Out.flush(); |
| 397 | } |
Reid Spencer | e0125b6 | 2004-07-18 00:16:21 +0000 | [diff] [blame] | 398 | |
| 399 | // vim: sw=2 ai |