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) |
Chris Lattner | 277bafb | 2004-01-14 02:50:16 +0000 | [diff] [blame] | 43 | : Out(o), Table(M, true) { |
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. |
| 59 | unsigned Version = (2 << 4) | 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 | |
Chris Lattner | 186a1f7 | 2003-03-19 20:56:46 +0000 | [diff] [blame] | 64 | { |
| 65 | BytecodeBlock CPool(BytecodeFormat::GlobalTypePlane, Out); |
| 66 | |
| 67 | // Write the type plane for types first because earlier planes (e.g. for a |
| 68 | // primitive type like float) may have constants constructed using types |
| 69 | // coming later (e.g., via getelementptr from a pointer type). The type |
| 70 | // plane is needed before types can be fwd or bkwd referenced. |
| 71 | const std::vector<const Value*> &Plane = Table.getPlane(Type::TypeTyID); |
| 72 | assert(!Plane.empty() && "No types at all?"); |
| 73 | unsigned ValNo = Type::FirstDerivedTyID; // Start at the derived types... |
| 74 | outputConstantsInPlane(Plane, ValNo); // Write out the types |
| 75 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 76 | |
Chris Lattner | 186a1f7 | 2003-03-19 20:56:46 +0000 | [diff] [blame] | 77 | // The ModuleInfoBlock follows directly after the type information |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 78 | outputModuleInfoBlock(M); |
| 79 | |
Chris Lattner | 186a1f7 | 2003-03-19 20:56:46 +0000 | [diff] [blame] | 80 | // Output module level constants, used for global variable initializers |
| 81 | outputConstants(false); |
| 82 | |
Chris Lattner | b579400 | 2002-04-07 22:49:37 +0000 | [diff] [blame] | 83 | // Do the whole module now! Process each function at a time... |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 84 | 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] | 85 | outputFunction(I); |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 86 | |
| 87 | // If needed, output the symbol table for the module... |
Chris Lattner | 6e6026b | 2002-11-20 18:36:02 +0000 | [diff] [blame] | 88 | outputSymbolTable(M->getSymbolTable()); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Vikram S. Adve | a7dac3d | 2002-07-14 23:07:51 +0000 | [diff] [blame] | 91 | // Helper function for outputConstants(). |
| 92 | // Writes out all the constants in the plane Plane starting at entry StartNo. |
| 93 | // |
| 94 | void BytecodeWriter::outputConstantsInPlane(const std::vector<const Value*> |
| 95 | &Plane, unsigned StartNo) { |
| 96 | unsigned ValNo = StartNo; |
| 97 | |
Chris Lattner | 83bb3d2 | 2004-01-14 23:36:54 +0000 | [diff] [blame] | 98 | // Scan through and ignore function arguments, global values, and constant |
| 99 | // strings. |
| 100 | for (; ValNo < Plane.size() && |
| 101 | (isa<Argument>(Plane[ValNo]) || isa<GlobalValue>(Plane[ValNo]) || |
| 102 | (isa<ConstantArray>(Plane[ValNo]) && |
| 103 | cast<ConstantArray>(Plane[ValNo])->isString())); ValNo++) |
Vikram S. Adve | a7dac3d | 2002-07-14 23:07:51 +0000 | [diff] [blame] | 104 | /*empty*/; |
| 105 | |
| 106 | unsigned NC = ValNo; // Number of constants |
| 107 | for (; NC < Plane.size() && |
| 108 | (isa<Constant>(Plane[NC]) || isa<Type>(Plane[NC])); NC++) |
| 109 | /*empty*/; |
| 110 | NC -= ValNo; // Convert from index into count |
| 111 | if (NC == 0) return; // Skip empty type planes... |
| 112 | |
Chris Lattner | d6942d7 | 2004-01-14 16:54:21 +0000 | [diff] [blame] | 113 | // FIXME: Most slabs only have 1 or 2 entries! We should encode this much |
| 114 | // more compactly. |
| 115 | |
Vikram S. Adve | a7dac3d | 2002-07-14 23:07:51 +0000 | [diff] [blame] | 116 | // Output type header: [num entries][type id number] |
| 117 | // |
| 118 | output_vbr(NC, Out); |
| 119 | |
| 120 | // Output the Type ID Number... |
Alkis Evlogimenos | 6059638 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 121 | int Slot = Table.getSlot(Plane.front()->getType()); |
Vikram S. Adve | a7dac3d | 2002-07-14 23:07:51 +0000 | [diff] [blame] | 122 | assert (Slot != -1 && "Type in constant pool but not in function!!"); |
| 123 | output_vbr((unsigned)Slot, Out); |
| 124 | |
| 125 | //cerr << "Emitting " << NC << " constants of type '" |
| 126 | // << Plane.front()->getType()->getName() << "' = Slot #" << Slot << "\n"; |
| 127 | |
| 128 | for (unsigned i = ValNo; i < ValNo+NC; ++i) { |
| 129 | const Value *V = Plane[i]; |
| 130 | if (const Constant *CPV = dyn_cast<Constant>(V)) { |
| 131 | //cerr << "Serializing value: <" << V->getType() << ">: " << V << ":" |
| 132 | // << Out.size() << "\n"; |
| 133 | outputConstant(CPV); |
| 134 | } else { |
Chris Lattner | 186a1f7 | 2003-03-19 20:56:46 +0000 | [diff] [blame] | 135 | outputType(cast<Type>(V)); |
Vikram S. Adve | a7dac3d | 2002-07-14 23:07:51 +0000 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
Chris Lattner | 80b9734 | 2004-01-17 23:25:43 +0000 | [diff] [blame] | 140 | static inline bool hasNullValue(unsigned TyID) { |
| 141 | return TyID != Type::LabelTyID && TyID != Type::TypeTyID && |
| 142 | TyID != Type::VoidTyID; |
| 143 | } |
| 144 | |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 145 | void BytecodeWriter::outputConstants(bool isFunction) { |
Chris Lattner | 0baa0af | 2004-01-15 21:06:57 +0000 | [diff] [blame] | 146 | BytecodeBlock CPool(BytecodeFormat::ConstantPool, Out, |
| 147 | true /* Elide block if empty */); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 148 | |
| 149 | unsigned NumPlanes = Table.getNumPlanes(); |
Chris Lattner | f69315b | 2003-05-22 18:35:38 +0000 | [diff] [blame] | 150 | |
| 151 | // Output the type plane before any constants! |
| 152 | if (isFunction && NumPlanes > Type::TypeTyID) { |
| 153 | const std::vector<const Value*> &Plane = Table.getPlane(Type::TypeTyID); |
Chris Lattner | 186a1f7 | 2003-03-19 20:56:46 +0000 | [diff] [blame] | 154 | if (!Plane.empty()) { // Skip empty type planes... |
Chris Lattner | f69315b | 2003-05-22 18:35:38 +0000 | [diff] [blame] | 155 | unsigned ValNo = Table.getModuleLevel(Type::TypeTyID); |
| 156 | outputConstantsInPlane(Plane, ValNo); |
Chris Lattner | 6463e0d | 2002-10-14 03:34:17 +0000 | [diff] [blame] | 157 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 158 | } |
Chris Lattner | f69315b | 2003-05-22 18:35:38 +0000 | [diff] [blame] | 159 | |
Chris Lattner | 83bb3d2 | 2004-01-14 23:36:54 +0000 | [diff] [blame] | 160 | // Output module-level string constants before any other constants.x |
| 161 | if (!isFunction) |
| 162 | outputConstantStrings(); |
| 163 | |
Chris Lattner | f69315b | 2003-05-22 18:35:38 +0000 | [diff] [blame] | 164 | for (unsigned pno = 0; pno != NumPlanes; pno++) |
| 165 | if (pno != Type::TypeTyID) { // Type plane handled above. |
| 166 | const std::vector<const Value*> &Plane = Table.getPlane(pno); |
| 167 | if (!Plane.empty()) { // Skip empty type planes... |
| 168 | unsigned ValNo = 0; |
Misha Brukman | 37f92e2 | 2003-09-11 22:34:13 +0000 | [diff] [blame] | 169 | if (isFunction) // Don't re-emit module constants |
Chris Lattner | f69315b | 2003-05-22 18:35:38 +0000 | [diff] [blame] | 170 | ValNo += Table.getModuleLevel(pno); |
| 171 | |
Chris Lattner | 80b9734 | 2004-01-17 23:25:43 +0000 | [diff] [blame] | 172 | if (hasNullValue(pno)) { |
Chris Lattner | f69315b | 2003-05-22 18:35:38 +0000 | [diff] [blame] | 173 | // Skip zero initializer |
| 174 | if (ValNo == 0) |
| 175 | ValNo = 1; |
| 176 | } |
| 177 | |
| 178 | // Write out constants in the plane |
| 179 | outputConstantsInPlane(Plane, ValNo); |
| 180 | } |
| 181 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Chris Lattner | 6b25242 | 2003-10-16 18:28:50 +0000 | [diff] [blame] | 184 | static unsigned getEncodedLinkage(const GlobalValue *GV) { |
| 185 | switch (GV->getLinkage()) { |
| 186 | default: assert(0 && "Invalid linkage!"); |
| 187 | case GlobalValue::ExternalLinkage: return 0; |
Chris Lattner | 6b25242 | 2003-10-16 18:28:50 +0000 | [diff] [blame] | 188 | case GlobalValue::WeakLinkage: return 1; |
| 189 | case GlobalValue::AppendingLinkage: return 2; |
| 190 | case GlobalValue::InternalLinkage: return 3; |
Chris Lattner | 22482a1 | 2003-10-18 06:30:21 +0000 | [diff] [blame] | 191 | case GlobalValue::LinkOnceLinkage: return 4; |
Chris Lattner | 6b25242 | 2003-10-16 18:28:50 +0000 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 195 | void BytecodeWriter::outputModuleInfoBlock(const Module *M) { |
| 196 | BytecodeBlock ModuleInfoBlock(BytecodeFormat::ModuleGlobalInfo, Out); |
| 197 | |
Chris Lattner | 70cc339 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 198 | // Output the types for the global variables in the module... |
| 199 | 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] | 200 | int Slot = Table.getSlot(I->getType()); |
Chris Lattner | 70cc339 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 201 | assert(Slot != -1 && "Module global vars is broken!"); |
Chris Lattner | d70684f | 2001-09-18 04:01:05 +0000 | [diff] [blame] | 202 | |
Chris Lattner | 22482a1 | 2003-10-18 06:30:21 +0000 | [diff] [blame] | 203 | // Fields: bit0 = isConstant, bit1 = hasInitializer, bit2-4=Linkage, |
| 204 | // bit5+ = Slot # for type |
| 205 | unsigned oSlot = ((unsigned)Slot << 5) | (getEncodedLinkage(I) << 2) | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 206 | (I->hasInitializer() << 1) | I->isConstant(); |
Chris Lattner | d70684f | 2001-09-18 04:01:05 +0000 | [diff] [blame] | 207 | output_vbr(oSlot, Out); |
| 208 | |
Chris Lattner | 1b98c5c | 2001-10-13 06:48:38 +0000 | [diff] [blame] | 209 | // If we have an initializer, output it now. |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 210 | if (I->hasInitializer()) { |
Alkis Evlogimenos | 6059638 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 211 | Slot = Table.getSlot((Value*)I->getInitializer()); |
Chris Lattner | d70684f | 2001-09-18 04:01:05 +0000 | [diff] [blame] | 212 | assert(Slot != -1 && "No slot for global var initializer!"); |
| 213 | output_vbr((unsigned)Slot, Out); |
| 214 | } |
Chris Lattner | 70cc339 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 215 | } |
Alkis Evlogimenos | 6059638 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 216 | output_vbr((unsigned)Table.getSlot(Type::VoidTy), Out); |
Chris Lattner | 70cc339 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 217 | |
Chris Lattner | b579400 | 2002-04-07 22:49:37 +0000 | [diff] [blame] | 218 | // Output the types of the functions in this module... |
Chris Lattner | 7fc9fe3 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 219 | 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] | 220 | int Slot = Table.getSlot(I->getType()); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 221 | assert(Slot != -1 && "Module const pool is broken!"); |
| 222 | assert(Slot >= Type::FirstDerivedTyID && "Derived type not in range!"); |
| 223 | output_vbr((unsigned)Slot, Out); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 224 | } |
Alkis Evlogimenos | 6059638 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 225 | output_vbr((unsigned)Table.getSlot(Type::VoidTy), Out); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Chris Lattner | cf3e67f | 2004-01-18 21:08:52 +0000 | [diff] [blame] | 228 | void BytecodeWriter::outputInstructions(const Function *F) { |
| 229 | BytecodeBlock ILBlock(BytecodeFormat::InstructionList, Out); |
Chris Lattner | cf3e67f | 2004-01-18 21:08:52 +0000 | [diff] [blame] | 230 | for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB) |
| 231 | for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) |
| 232 | outputInstruction(*I); |
Chris Lattner | cf3e67f | 2004-01-18 21:08:52 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Chris Lattner | 186a1f7 | 2003-03-19 20:56:46 +0000 | [diff] [blame] | 235 | void BytecodeWriter::outputFunction(const Function *F) { |
Chris Lattner | c9aa7df | 2002-03-29 03:51:11 +0000 | [diff] [blame] | 236 | BytecodeBlock FunctionBlock(BytecodeFormat::Function, Out); |
Chris Lattner | 6b25242 | 2003-10-16 18:28:50 +0000 | [diff] [blame] | 237 | output_vbr(getEncodedLinkage(F), Out); |
Chris Lattner | d23b1d3 | 2001-11-26 18:56:10 +0000 | [diff] [blame] | 238 | |
Chris Lattner | cf3e67f | 2004-01-18 21:08:52 +0000 | [diff] [blame] | 239 | // If this is an external function, there is nothing else to emit! |
| 240 | if (F->isExternal()) return; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 241 | |
Chris Lattner | cf3e67f | 2004-01-18 21:08:52 +0000 | [diff] [blame] | 242 | // Get slot information about the function... |
| 243 | Table.incorporateFunction(F); |
| 244 | |
| 245 | if (Table.getCompactionTable().empty()) { |
| 246 | // Output information about the constants in the function if the compaction |
| 247 | // table is not being used. |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 248 | outputConstants(true); |
Chris Lattner | cf3e67f | 2004-01-18 21:08:52 +0000 | [diff] [blame] | 249 | } else { |
| 250 | // Otherwise, emit the compaction table. |
| 251 | outputCompactionTable(); |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 252 | } |
Chris Lattner | cf3e67f | 2004-01-18 21:08:52 +0000 | [diff] [blame] | 253 | |
| 254 | // Output all of the instructions in the body of the function |
| 255 | outputInstructions(F); |
| 256 | |
| 257 | // If needed, output the symbol table for the function... |
| 258 | outputSymbolTable(F->getSymbolTable()); |
| 259 | |
| 260 | Table.purgeFunction(); |
| 261 | } |
| 262 | |
| 263 | void BytecodeWriter::outputCompactionTablePlane(unsigned PlaneNo, |
| 264 | const std::vector<const Value*> &Plane, |
| 265 | unsigned StartNo) { |
| 266 | unsigned End = Table.getModuleLevel(PlaneNo); |
Chris Lattner | 52f86d6 | 2004-01-20 00:54:06 +0000 | [diff] [blame] | 267 | if (Plane.empty() || StartNo == End || End == 0) return; // Nothing to emit |
Chris Lattner | cf3e67f | 2004-01-18 21:08:52 +0000 | [diff] [blame] | 268 | assert(StartNo < End && "Cannot emit negative range!"); |
| 269 | assert(StartNo < Plane.size() && End <= Plane.size()); |
| 270 | |
Chris Lattner | cf3e67f | 2004-01-18 21:08:52 +0000 | [diff] [blame] | 271 | // Do not emit the null initializer! |
| 272 | if (PlaneNo != Type::TypeTyID) ++StartNo; |
| 273 | |
Chris Lattner | 2410243 | 2004-01-18 22:35:34 +0000 | [diff] [blame] | 274 | // Figure out which encoding to use. By far the most common case we have is |
| 275 | // to emit 0-2 entries in a compaction table plane. |
| 276 | switch (End-StartNo) { |
| 277 | case 0: // Avoid emitting two vbr's if possible. |
| 278 | case 1: |
| 279 | case 2: |
| 280 | output_vbr((PlaneNo << 2) | End-StartNo, Out); |
| 281 | break; |
| 282 | default: |
| 283 | // Output the number of things. |
| 284 | output_vbr((unsigned(End-StartNo) << 2) | 3, Out); |
| 285 | output_vbr(PlaneNo, Out); // Emit the type plane this is |
| 286 | break; |
| 287 | } |
| 288 | |
Chris Lattner | cf3e67f | 2004-01-18 21:08:52 +0000 | [diff] [blame] | 289 | for (unsigned i = StartNo; i != End; ++i) |
| 290 | output_vbr(Table.getGlobalSlot(Plane[i]), Out); |
| 291 | } |
| 292 | |
| 293 | void BytecodeWriter::outputCompactionTable() { |
Chris Lattner | cf3e67f | 2004-01-18 21:08:52 +0000 | [diff] [blame] | 294 | BytecodeBlock CTB(BytecodeFormat::CompactionTable, Out, true/*ElideIfEmpty*/); |
| 295 | const std::vector<std::vector<const Value*> > &CT =Table.getCompactionTable(); |
| 296 | |
| 297 | // First thing is first, emit the type compaction table if there is one. |
| 298 | if (CT.size() > Type::TypeTyID) |
| 299 | outputCompactionTablePlane(Type::TypeTyID, CT[Type::TypeTyID], |
| 300 | Type::FirstDerivedTyID); |
| 301 | |
| 302 | for (unsigned i = 0, e = CT.size(); i != e; ++i) |
| 303 | if (i != Type::TypeTyID) |
| 304 | outputCompactionTablePlane(i, CT[i], 0); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 305 | } |
| 306 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 307 | void BytecodeWriter::outputSymbolTable(const SymbolTable &MST) { |
Chris Lattner | 737d3cd | 2004-01-10 19:56:59 +0000 | [diff] [blame] | 308 | // Do not output the Bytecode block for an empty symbol table, it just wastes |
| 309 | // space! |
| 310 | if (MST.begin() == MST.end()) return; |
| 311 | |
Chris Lattner | 0baa0af | 2004-01-15 21:06:57 +0000 | [diff] [blame] | 312 | BytecodeBlock SymTabBlock(BytecodeFormat::SymbolTable, Out, |
| 313 | true/* ElideIfEmpty*/); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 314 | |
Chris Lattner | 7fc9fe3 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 315 | for (SymbolTable::const_iterator TI = MST.begin(); TI != MST.end(); ++TI) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 316 | SymbolTable::type_const_iterator I = MST.type_begin(TI->first); |
| 317 | SymbolTable::type_const_iterator End = MST.type_end(TI->first); |
| 318 | int Slot; |
| 319 | |
| 320 | if (I == End) continue; // Don't mess with an absent type... |
| 321 | |
| 322 | // Symtab block header: [num entries][type id number] |
| 323 | output_vbr(MST.type_size(TI->first), Out); |
| 324 | |
Alkis Evlogimenos | 6059638 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 325 | Slot = Table.getSlot(TI->first); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 326 | assert(Slot != -1 && "Type in symtab, but not in table!"); |
| 327 | output_vbr((unsigned)Slot, Out); |
| 328 | |
Chris Lattner | 7fc9fe3 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 329 | for (; I != End; ++I) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 330 | // Symtab entry: [def slot #][name] |
Chris Lattner | 52f86d6 | 2004-01-20 00:54:06 +0000 | [diff] [blame] | 331 | const Value *V = I->second; |
| 332 | |
Alkis Evlogimenos | 6059638 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 333 | Slot = Table.getSlot(I->second); |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 334 | assert(Slot != -1 && "Value in symtab but has no slot number!!"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 335 | output_vbr((unsigned)Slot, Out); |
| 336 | output(I->first, Out, false); // Don't force alignment... |
| 337 | } |
| 338 | } |
| 339 | } |
| 340 | |
Chris Lattner | 44f549b | 2004-01-10 18:49:43 +0000 | [diff] [blame] | 341 | void llvm::WriteBytecodeToFile(const Module *C, std::ostream &Out) { |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 342 | assert(C && "You can't write a null module!!"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 343 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 344 | std::deque<unsigned char> Buffer; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 345 | |
| 346 | // This object populates buffer for us... |
| 347 | BytecodeWriter BCW(Buffer, C); |
| 348 | |
Chris Lattner | ce6ef11 | 2002-07-26 18:40:14 +0000 | [diff] [blame] | 349 | // Keep track of how much we've written... |
| 350 | BytesWritten += Buffer.size(); |
| 351 | |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 352 | // Okay, write the deque out to the ostream now... the deque is not |
| 353 | // sequential in memory, however, so write out as much as possible in big |
| 354 | // chunks, until we're done. |
| 355 | // |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 356 | std::deque<unsigned char>::const_iterator I = Buffer.begin(),E = Buffer.end(); |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 357 | while (I != E) { // Loop until it's all written |
| 358 | // Scan to see how big this chunk is... |
| 359 | const unsigned char *ChunkPtr = &*I; |
| 360 | const unsigned char *LastPtr = ChunkPtr; |
| 361 | while (I != E) { |
| 362 | const unsigned char *ThisPtr = &*++I; |
Chris Lattner | 5cb1741 | 2001-11-04 21:32:41 +0000 | [diff] [blame] | 363 | if (LastPtr+1 != ThisPtr) { // Advanced by more than a byte of memory? |
| 364 | ++LastPtr; |
| 365 | break; |
| 366 | } |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 367 | LastPtr = ThisPtr; |
| 368 | } |
| 369 | |
| 370 | // Write out the chunk... |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 371 | Out.write((char*)ChunkPtr, LastPtr-ChunkPtr); |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 372 | } |
| 373 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 374 | Out.flush(); |
| 375 | } |