Chris Lattner | b0a5964 | 2004-01-10 19:07:06 +0000 | [diff] [blame] | 1 | //===-- Writer.cpp - Library for writing LLVM bytecode files --------------===// |
John Criswell | 482202a | 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 | 2f7c963 | 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 | 2f7c963 | 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 | af4c95a | 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 | 2f7c963 | 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 | b97ef9f | 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 | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 22 | // |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 23 | //===----------------------------------------------------------------------===// |
| 24 | |
| 25 | #include "WriterInternals.h" |
Chris Lattner | 36d2c7c | 2002-07-23 19:56:44 +0000 | [diff] [blame] | 26 | #include "llvm/Bytecode/WriteBytecodePass.h" |
Chris Lattner | 6229fbf | 2004-01-14 23:36:54 +0000 | [diff] [blame] | 27 | #include "llvm/Constants.h" |
| 28 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 29 | #include "llvm/Module.h" |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 30 | #include "llvm/SymbolTable.h" |
Chris Lattner | b4c6777 | 2001-11-29 16:32:16 +0000 | [diff] [blame] | 31 | #include "Support/STLExtras.h" |
Chris Lattner | bf3a099 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 32 | #include "Support/Statistic.h" |
Chris Lattner | 0c53031 | 2004-01-10 19:10:01 +0000 | [diff] [blame] | 33 | #include <cstring> |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 34 | #include <algorithm> |
Chris Lattner | dfe0346 | 2004-01-10 18:49:43 +0000 | [diff] [blame] | 35 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 36 | |
Chris Lattner | 36d2c7c | 2002-07-23 19:56:44 +0000 | [diff] [blame] | 37 | static RegisterPass<WriteBytecodePass> X("emitbytecode", "Bytecode Writer"); |
| 38 | |
Chris Lattner | 64eea74 | 2002-07-26 18:40:14 +0000 | [diff] [blame] | 39 | static Statistic<> |
Chris Lattner | bf3a099 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 40 | BytesWritten("bytecodewriter", "Number of bytecode bytes written"); |
Chris Lattner | 9a38c78f | 2004-01-14 16:54:21 +0000 | [diff] [blame] | 41 | static Statistic<> |
| 42 | ConstantTotalBytes("bytecodewriter", "Bytes of constants total"); |
Chris Lattner | 9a38c78f | 2004-01-14 16:54:21 +0000 | [diff] [blame] | 43 | static Statistic<> |
| 44 | ConstantPlaneHeaderBytes("bytecodewriter", "Constant plane header bytes"); |
| 45 | static Statistic<> |
| 46 | InstructionBytes("bytecodewriter", "Bytes of bytes of instructions"); |
| 47 | static Statistic<> |
| 48 | SymTabBytes("bytecodewriter", "Bytes of symbol table"); |
| 49 | static Statistic<> |
| 50 | ModuleInfoBytes("bytecodewriter", "Bytes of module info"); |
Chris Lattner | bc02f4c | 2004-01-18 21:08:52 +0000 | [diff] [blame] | 51 | static Statistic<> |
| 52 | CompactionTableBytes("bytecodewriter", "Bytes of compaction tables"); |
Chris Lattner | 36d2c7c | 2002-07-23 19:56:44 +0000 | [diff] [blame] | 53 | |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 54 | BytecodeWriter::BytecodeWriter(std::deque<unsigned char> &o, const Module *M) |
Chris Lattner | 1f018c2 | 2004-01-14 02:50:16 +0000 | [diff] [blame] | 55 | : Out(o), Table(M, true) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 56 | |
Chris Lattner | 6229fbf | 2004-01-14 23:36:54 +0000 | [diff] [blame] | 57 | // Emit the signature... |
| 58 | static const unsigned char *Sig = (const unsigned char*)"llvm"; |
| 59 | output_data(Sig, Sig+4, Out); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 60 | |
| 61 | // Emit the top level CLASS block. |
| 62 | BytecodeBlock ModuleBlock(BytecodeFormat::Module, Out); |
| 63 | |
Chris Lattner | 55a07ad | 2003-08-24 13:47:36 +0000 | [diff] [blame] | 64 | bool isBigEndian = M->getEndianness() == Module::BigEndian; |
| 65 | bool hasLongPointers = M->getPointerSize() == Module::Pointer64; |
| 66 | bool hasNoEndianness = M->getEndianness() == Module::AnyEndianness; |
| 67 | bool hasNoPointerSize = M->getPointerSize() == Module::AnyPointerSize; |
Chris Lattner | 428bf5a | 2003-03-19 20:56:46 +0000 | [diff] [blame] | 68 | |
Chris Lattner | 9a38c78f | 2004-01-14 16:54:21 +0000 | [diff] [blame] | 69 | // Output the version identifier... we are currently on bytecode version #1, |
| 70 | // which corresponds to LLVM v1.2. |
| 71 | unsigned Version = (1 << 4) | isBigEndian | (hasLongPointers << 1) | |
Chris Lattner | 55a07ad | 2003-08-24 13:47:36 +0000 | [diff] [blame] | 72 | (hasNoEndianness << 2) | (hasNoPointerSize << 3); |
Chris Lattner | 428bf5a | 2003-03-19 20:56:46 +0000 | [diff] [blame] | 73 | output_vbr(Version, Out); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 74 | align32(Out); |
| 75 | |
Chris Lattner | 428bf5a | 2003-03-19 20:56:46 +0000 | [diff] [blame] | 76 | { |
| 77 | BytecodeBlock CPool(BytecodeFormat::GlobalTypePlane, Out); |
| 78 | |
| 79 | // Write the type plane for types first because earlier planes (e.g. for a |
| 80 | // primitive type like float) may have constants constructed using types |
| 81 | // coming later (e.g., via getelementptr from a pointer type). The type |
| 82 | // plane is needed before types can be fwd or bkwd referenced. |
| 83 | const std::vector<const Value*> &Plane = Table.getPlane(Type::TypeTyID); |
| 84 | assert(!Plane.empty() && "No types at all?"); |
| 85 | unsigned ValNo = Type::FirstDerivedTyID; // Start at the derived types... |
| 86 | outputConstantsInPlane(Plane, ValNo); // Write out the types |
| 87 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 88 | |
Chris Lattner | 428bf5a | 2003-03-19 20:56:46 +0000 | [diff] [blame] | 89 | // The ModuleInfoBlock follows directly after the type information |
Chris Lattner | b97ef9f | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 90 | outputModuleInfoBlock(M); |
| 91 | |
Chris Lattner | 428bf5a | 2003-03-19 20:56:46 +0000 | [diff] [blame] | 92 | // Output module level constants, used for global variable initializers |
| 93 | outputConstants(false); |
| 94 | |
Chris Lattner | 6915f8f | 2002-04-07 22:49:37 +0000 | [diff] [blame] | 95 | // Do the whole module now! Process each function at a time... |
Chris Lattner | 7076ff2 | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 96 | for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) |
Chris Lattner | 428bf5a | 2003-03-19 20:56:46 +0000 | [diff] [blame] | 97 | outputFunction(I); |
Chris Lattner | b97ef9f | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 98 | |
| 99 | // If needed, output the symbol table for the module... |
Chris Lattner | 98cf1f5 | 2002-11-20 18:36:02 +0000 | [diff] [blame] | 100 | outputSymbolTable(M->getSymbolTable()); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Vikram S. Adve | c1b6474a | 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 | 6229fbf | 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 | c1b6474a | 2002-07-14 23:07:51 +0000 | [diff] [blame] | 116 | /*empty*/; |
| 117 | |
| 118 | unsigned NC = ValNo; // Number of constants |
| 119 | for (; NC < Plane.size() && |
| 120 | (isa<Constant>(Plane[NC]) || isa<Type>(Plane[NC])); NC++) |
| 121 | /*empty*/; |
| 122 | NC -= ValNo; // Convert from index into count |
| 123 | if (NC == 0) return; // Skip empty type planes... |
| 124 | |
Chris Lattner | 9a38c78f | 2004-01-14 16:54:21 +0000 | [diff] [blame] | 125 | // FIXME: Most slabs only have 1 or 2 entries! We should encode this much |
| 126 | // more compactly. |
| 127 | |
| 128 | ConstantPlaneHeaderBytes -= Out.size(); |
| 129 | |
Vikram S. Adve | c1b6474a | 2002-07-14 23:07:51 +0000 | [diff] [blame] | 130 | // Output type header: [num entries][type id number] |
| 131 | // |
| 132 | output_vbr(NC, Out); |
| 133 | |
| 134 | // Output the Type ID Number... |
Alkis Evlogimenos | 8faf8d9 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 135 | int Slot = Table.getSlot(Plane.front()->getType()); |
Vikram S. Adve | c1b6474a | 2002-07-14 23:07:51 +0000 | [diff] [blame] | 136 | assert (Slot != -1 && "Type in constant pool but not in function!!"); |
| 137 | output_vbr((unsigned)Slot, Out); |
| 138 | |
Chris Lattner | 9a38c78f | 2004-01-14 16:54:21 +0000 | [diff] [blame] | 139 | ConstantPlaneHeaderBytes += Out.size(); |
| 140 | |
| 141 | |
Vikram S. Adve | c1b6474a | 2002-07-14 23:07:51 +0000 | [diff] [blame] | 142 | //cerr << "Emitting " << NC << " constants of type '" |
| 143 | // << Plane.front()->getType()->getName() << "' = Slot #" << Slot << "\n"; |
| 144 | |
| 145 | for (unsigned i = ValNo; i < ValNo+NC; ++i) { |
| 146 | const Value *V = Plane[i]; |
| 147 | if (const Constant *CPV = dyn_cast<Constant>(V)) { |
| 148 | //cerr << "Serializing value: <" << V->getType() << ">: " << V << ":" |
| 149 | // << Out.size() << "\n"; |
| 150 | outputConstant(CPV); |
| 151 | } else { |
Chris Lattner | 428bf5a | 2003-03-19 20:56:46 +0000 | [diff] [blame] | 152 | outputType(cast<Type>(V)); |
Vikram S. Adve | c1b6474a | 2002-07-14 23:07:51 +0000 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
Chris Lattner | 677af4a | 2004-01-17 23:25:43 +0000 | [diff] [blame] | 157 | static inline bool hasNullValue(unsigned TyID) { |
| 158 | return TyID != Type::LabelTyID && TyID != Type::TypeTyID && |
| 159 | TyID != Type::VoidTyID; |
| 160 | } |
| 161 | |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 162 | void BytecodeWriter::outputConstants(bool isFunction) { |
Chris Lattner | 9a38c78f | 2004-01-14 16:54:21 +0000 | [diff] [blame] | 163 | ConstantTotalBytes -= Out.size(); |
Chris Lattner | 4c57267 | 2004-01-15 21:06:57 +0000 | [diff] [blame] | 164 | BytecodeBlock CPool(BytecodeFormat::ConstantPool, Out, |
| 165 | true /* Elide block if empty */); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 166 | |
| 167 | unsigned NumPlanes = Table.getNumPlanes(); |
Chris Lattner | 4a20791 | 2003-05-22 18:35:38 +0000 | [diff] [blame] | 168 | |
| 169 | // Output the type plane before any constants! |
| 170 | if (isFunction && NumPlanes > Type::TypeTyID) { |
| 171 | const std::vector<const Value*> &Plane = Table.getPlane(Type::TypeTyID); |
Chris Lattner | 428bf5a | 2003-03-19 20:56:46 +0000 | [diff] [blame] | 172 | if (!Plane.empty()) { // Skip empty type planes... |
Chris Lattner | 4a20791 | 2003-05-22 18:35:38 +0000 | [diff] [blame] | 173 | unsigned ValNo = Table.getModuleLevel(Type::TypeTyID); |
| 174 | outputConstantsInPlane(Plane, ValNo); |
Chris Lattner | ca1725d | 2002-10-14 03:34:17 +0000 | [diff] [blame] | 175 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 176 | } |
Chris Lattner | 4a20791 | 2003-05-22 18:35:38 +0000 | [diff] [blame] | 177 | |
Chris Lattner | 6229fbf | 2004-01-14 23:36:54 +0000 | [diff] [blame] | 178 | // Output module-level string constants before any other constants.x |
| 179 | if (!isFunction) |
| 180 | outputConstantStrings(); |
| 181 | |
Chris Lattner | 4a20791 | 2003-05-22 18:35:38 +0000 | [diff] [blame] | 182 | for (unsigned pno = 0; pno != NumPlanes; pno++) |
| 183 | if (pno != Type::TypeTyID) { // Type plane handled above. |
| 184 | const std::vector<const Value*> &Plane = Table.getPlane(pno); |
| 185 | if (!Plane.empty()) { // Skip empty type planes... |
| 186 | unsigned ValNo = 0; |
Misha Brukman | acda7df | 2003-09-11 22:34:13 +0000 | [diff] [blame] | 187 | if (isFunction) // Don't re-emit module constants |
Chris Lattner | 4a20791 | 2003-05-22 18:35:38 +0000 | [diff] [blame] | 188 | ValNo += Table.getModuleLevel(pno); |
| 189 | |
Chris Lattner | 677af4a | 2004-01-17 23:25:43 +0000 | [diff] [blame] | 190 | if (hasNullValue(pno)) { |
Chris Lattner | 4a20791 | 2003-05-22 18:35:38 +0000 | [diff] [blame] | 191 | // Skip zero initializer |
| 192 | if (ValNo == 0) |
| 193 | ValNo = 1; |
| 194 | } |
| 195 | |
| 196 | // Write out constants in the plane |
| 197 | outputConstantsInPlane(Plane, ValNo); |
| 198 | } |
| 199 | } |
Chris Lattner | 9a38c78f | 2004-01-14 16:54:21 +0000 | [diff] [blame] | 200 | ConstantTotalBytes += Out.size(); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Chris Lattner | 2d05c60 | 2003-10-16 18:28:50 +0000 | [diff] [blame] | 203 | static unsigned getEncodedLinkage(const GlobalValue *GV) { |
| 204 | switch (GV->getLinkage()) { |
| 205 | default: assert(0 && "Invalid linkage!"); |
| 206 | case GlobalValue::ExternalLinkage: return 0; |
Chris Lattner | 2d05c60 | 2003-10-16 18:28:50 +0000 | [diff] [blame] | 207 | case GlobalValue::WeakLinkage: return 1; |
| 208 | case GlobalValue::AppendingLinkage: return 2; |
| 209 | case GlobalValue::InternalLinkage: return 3; |
Chris Lattner | dc83293 | 2003-10-18 06:30:21 +0000 | [diff] [blame] | 210 | case GlobalValue::LinkOnceLinkage: return 4; |
Chris Lattner | 2d05c60 | 2003-10-16 18:28:50 +0000 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 214 | void BytecodeWriter::outputModuleInfoBlock(const Module *M) { |
Chris Lattner | 9a38c78f | 2004-01-14 16:54:21 +0000 | [diff] [blame] | 215 | ModuleInfoBytes -= Out.size(); |
| 216 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 217 | BytecodeBlock ModuleInfoBlock(BytecodeFormat::ModuleGlobalInfo, Out); |
| 218 | |
Chris Lattner | da97550 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 219 | // Output the types for the global variables in the module... |
| 220 | for (Module::const_giterator I = M->gbegin(), End = M->gend(); I != End;++I) { |
Alkis Evlogimenos | 8faf8d9 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 221 | int Slot = Table.getSlot(I->getType()); |
Chris Lattner | da97550 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 222 | assert(Slot != -1 && "Module global vars is broken!"); |
Chris Lattner | 3779864 | 2001-09-18 04:01:05 +0000 | [diff] [blame] | 223 | |
Chris Lattner | dc83293 | 2003-10-18 06:30:21 +0000 | [diff] [blame] | 224 | // Fields: bit0 = isConstant, bit1 = hasInitializer, bit2-4=Linkage, |
| 225 | // bit5+ = Slot # for type |
| 226 | unsigned oSlot = ((unsigned)Slot << 5) | (getEncodedLinkage(I) << 2) | |
Chris Lattner | 7076ff2 | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 227 | (I->hasInitializer() << 1) | I->isConstant(); |
Chris Lattner | 3779864 | 2001-09-18 04:01:05 +0000 | [diff] [blame] | 228 | output_vbr(oSlot, Out); |
| 229 | |
Chris Lattner | 81125b6 | 2001-10-13 06:48:38 +0000 | [diff] [blame] | 230 | // If we have an initializer, output it now. |
Chris Lattner | 7076ff2 | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 231 | if (I->hasInitializer()) { |
Alkis Evlogimenos | 8faf8d9 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 232 | Slot = Table.getSlot((Value*)I->getInitializer()); |
Chris Lattner | 3779864 | 2001-09-18 04:01:05 +0000 | [diff] [blame] | 233 | assert(Slot != -1 && "No slot for global var initializer!"); |
| 234 | output_vbr((unsigned)Slot, Out); |
| 235 | } |
Chris Lattner | da97550 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 236 | } |
Alkis Evlogimenos | 8faf8d9 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 237 | output_vbr((unsigned)Table.getSlot(Type::VoidTy), Out); |
Chris Lattner | da97550 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 238 | |
Chris Lattner | 6915f8f | 2002-04-07 22:49:37 +0000 | [diff] [blame] | 239 | // Output the types of the functions in this module... |
Chris Lattner | 4cee8d8 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 240 | for (Module::const_iterator I = M->begin(), End = M->end(); I != End; ++I) { |
Alkis Evlogimenos | 8faf8d9 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 241 | int Slot = Table.getSlot(I->getType()); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 242 | assert(Slot != -1 && "Module const pool is broken!"); |
| 243 | assert(Slot >= Type::FirstDerivedTyID && "Derived type not in range!"); |
| 244 | output_vbr((unsigned)Slot, Out); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 245 | } |
Alkis Evlogimenos | 8faf8d9 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 246 | output_vbr((unsigned)Table.getSlot(Type::VoidTy), Out); |
Chris Lattner | da97550 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 247 | |
Chris Lattner | 9a38c78f | 2004-01-14 16:54:21 +0000 | [diff] [blame] | 248 | ModuleInfoBytes += Out.size(); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Chris Lattner | bc02f4c | 2004-01-18 21:08:52 +0000 | [diff] [blame] | 251 | void BytecodeWriter::outputInstructions(const Function *F) { |
| 252 | BytecodeBlock ILBlock(BytecodeFormat::InstructionList, Out); |
| 253 | InstructionBytes -= Out.size(); |
| 254 | for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB) |
| 255 | for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) |
| 256 | outputInstruction(*I); |
| 257 | InstructionBytes += Out.size(); |
| 258 | } |
| 259 | |
Chris Lattner | 428bf5a | 2003-03-19 20:56:46 +0000 | [diff] [blame] | 260 | void BytecodeWriter::outputFunction(const Function *F) { |
Chris Lattner | ff87436 | 2002-03-29 03:51:11 +0000 | [diff] [blame] | 261 | BytecodeBlock FunctionBlock(BytecodeFormat::Function, Out); |
Chris Lattner | 2d05c60 | 2003-10-16 18:28:50 +0000 | [diff] [blame] | 262 | output_vbr(getEncodedLinkage(F), Out); |
Chris Lattner | 2263733 | 2001-11-26 18:56:10 +0000 | [diff] [blame] | 263 | |
Chris Lattner | bc02f4c | 2004-01-18 21:08:52 +0000 | [diff] [blame] | 264 | // If this is an external function, there is nothing else to emit! |
| 265 | if (F->isExternal()) return; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 266 | |
Chris Lattner | bc02f4c | 2004-01-18 21:08:52 +0000 | [diff] [blame] | 267 | // Get slot information about the function... |
| 268 | Table.incorporateFunction(F); |
| 269 | |
| 270 | if (Table.getCompactionTable().empty()) { |
| 271 | // Output information about the constants in the function if the compaction |
| 272 | // table is not being used. |
Chris Lattner | b97ef9f | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 273 | outputConstants(true); |
Chris Lattner | bc02f4c | 2004-01-18 21:08:52 +0000 | [diff] [blame] | 274 | } else { |
| 275 | // Otherwise, emit the compaction table. |
| 276 | outputCompactionTable(); |
Chris Lattner | b97ef9f | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 277 | } |
Chris Lattner | bc02f4c | 2004-01-18 21:08:52 +0000 | [diff] [blame] | 278 | |
| 279 | // Output all of the instructions in the body of the function |
| 280 | outputInstructions(F); |
| 281 | |
| 282 | // If needed, output the symbol table for the function... |
| 283 | outputSymbolTable(F->getSymbolTable()); |
| 284 | |
| 285 | Table.purgeFunction(); |
| 286 | } |
| 287 | |
| 288 | void BytecodeWriter::outputCompactionTablePlane(unsigned PlaneNo, |
| 289 | const std::vector<const Value*> &Plane, |
| 290 | unsigned StartNo) { |
| 291 | unsigned End = Table.getModuleLevel(PlaneNo); |
| 292 | if (StartNo == End || End == 0) return; // Nothing to emit |
| 293 | assert(StartNo < End && "Cannot emit negative range!"); |
| 294 | assert(StartNo < Plane.size() && End <= Plane.size()); |
| 295 | |
| 296 | output_vbr(unsigned(End-StartNo), Out); // Output the number of things. |
| 297 | output_vbr(PlaneNo, Out); // Emit the type plane this is |
| 298 | |
| 299 | // Do not emit the null initializer! |
| 300 | if (PlaneNo != Type::TypeTyID) ++StartNo; |
| 301 | |
| 302 | for (unsigned i = StartNo; i != End; ++i) |
| 303 | output_vbr(Table.getGlobalSlot(Plane[i]), Out); |
| 304 | } |
| 305 | |
| 306 | void BytecodeWriter::outputCompactionTable() { |
| 307 | CompactionTableBytes -= Out.size(); |
| 308 | BytecodeBlock CTB(BytecodeFormat::CompactionTable, Out, true/*ElideIfEmpty*/); |
| 309 | const std::vector<std::vector<const Value*> > &CT =Table.getCompactionTable(); |
| 310 | |
| 311 | // First thing is first, emit the type compaction table if there is one. |
| 312 | if (CT.size() > Type::TypeTyID) |
| 313 | outputCompactionTablePlane(Type::TypeTyID, CT[Type::TypeTyID], |
| 314 | Type::FirstDerivedTyID); |
| 315 | |
| 316 | for (unsigned i = 0, e = CT.size(); i != e; ++i) |
| 317 | if (i != Type::TypeTyID) |
| 318 | outputCompactionTablePlane(i, CT[i], 0); |
| 319 | CompactionTableBytes += Out.size(); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 322 | void BytecodeWriter::outputSymbolTable(const SymbolTable &MST) { |
Chris Lattner | a2bfab8 | 2004-01-10 19:56:59 +0000 | [diff] [blame] | 323 | // Do not output the Bytecode block for an empty symbol table, it just wastes |
| 324 | // space! |
| 325 | if (MST.begin() == MST.end()) return; |
| 326 | |
Chris Lattner | 9a38c78f | 2004-01-14 16:54:21 +0000 | [diff] [blame] | 327 | SymTabBytes -= Out.size(); |
| 328 | |
Chris Lattner | 4c57267 | 2004-01-15 21:06:57 +0000 | [diff] [blame] | 329 | BytecodeBlock SymTabBlock(BytecodeFormat::SymbolTable, Out, |
| 330 | true/* ElideIfEmpty*/); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 331 | |
Chris Lattner | 4cee8d8 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 332 | for (SymbolTable::const_iterator TI = MST.begin(); TI != MST.end(); ++TI) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 333 | SymbolTable::type_const_iterator I = MST.type_begin(TI->first); |
| 334 | SymbolTable::type_const_iterator End = MST.type_end(TI->first); |
| 335 | int Slot; |
| 336 | |
| 337 | if (I == End) continue; // Don't mess with an absent type... |
| 338 | |
| 339 | // Symtab block header: [num entries][type id number] |
| 340 | output_vbr(MST.type_size(TI->first), Out); |
| 341 | |
Alkis Evlogimenos | 8faf8d9 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 342 | Slot = Table.getSlot(TI->first); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 343 | assert(Slot != -1 && "Type in symtab, but not in table!"); |
| 344 | output_vbr((unsigned)Slot, Out); |
| 345 | |
Chris Lattner | 4cee8d8 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 346 | for (; I != End; ++I) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 347 | // Symtab entry: [def slot #][name] |
Alkis Evlogimenos | 8faf8d9 | 2003-10-17 02:02:40 +0000 | [diff] [blame] | 348 | Slot = Table.getSlot(I->second); |
Chris Lattner | b97ef9f | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 349 | assert(Slot != -1 && "Value in symtab but has no slot number!!"); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 350 | output_vbr((unsigned)Slot, Out); |
| 351 | output(I->first, Out, false); // Don't force alignment... |
| 352 | } |
| 353 | } |
Chris Lattner | 9a38c78f | 2004-01-14 16:54:21 +0000 | [diff] [blame] | 354 | |
| 355 | SymTabBytes += Out.size(); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 356 | } |
| 357 | |
Chris Lattner | dfe0346 | 2004-01-10 18:49:43 +0000 | [diff] [blame] | 358 | void llvm::WriteBytecodeToFile(const Module *C, std::ostream &Out) { |
Chris Lattner | b97ef9f | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 359 | assert(C && "You can't write a null module!!"); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 360 | |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 361 | std::deque<unsigned char> Buffer; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 362 | |
| 363 | // This object populates buffer for us... |
| 364 | BytecodeWriter BCW(Buffer, C); |
| 365 | |
Chris Lattner | 64eea74 | 2002-07-26 18:40:14 +0000 | [diff] [blame] | 366 | // Keep track of how much we've written... |
| 367 | BytesWritten += Buffer.size(); |
| 368 | |
Chris Lattner | b97ef9f | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 369 | // Okay, write the deque out to the ostream now... the deque is not |
| 370 | // sequential in memory, however, so write out as much as possible in big |
| 371 | // chunks, until we're done. |
| 372 | // |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 373 | std::deque<unsigned char>::const_iterator I = Buffer.begin(),E = Buffer.end(); |
Chris Lattner | b97ef9f | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 374 | while (I != E) { // Loop until it's all written |
| 375 | // Scan to see how big this chunk is... |
| 376 | const unsigned char *ChunkPtr = &*I; |
| 377 | const unsigned char *LastPtr = ChunkPtr; |
| 378 | while (I != E) { |
| 379 | const unsigned char *ThisPtr = &*++I; |
Chris Lattner | dc194d5 | 2001-11-04 21:32:41 +0000 | [diff] [blame] | 380 | if (LastPtr+1 != ThisPtr) { // Advanced by more than a byte of memory? |
| 381 | ++LastPtr; |
| 382 | break; |
| 383 | } |
Chris Lattner | b97ef9f | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 384 | LastPtr = ThisPtr; |
| 385 | } |
| 386 | |
| 387 | // Write out the chunk... |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 388 | Out.write((char*)ChunkPtr, LastPtr-ChunkPtr); |
Chris Lattner | b97ef9f | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 389 | } |
| 390 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 391 | Out.flush(); |
| 392 | } |