Chris Lattner | cf3056d | 2003-10-13 03:32:08 +0000 | [diff] [blame^] | 1 | //===-- Writer.cpp - Library for writing VM bytecode files ----------------===// |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 2 | // |
| 3 | // This library implements the functionality defined in llvm/Bytecode/Writer.h |
| 4 | // |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 5 | // 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] | 6 | // 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] | 7 | // reason for this is that we must do "seeking" in the stream to do back- |
| 8 | // patching, and some very important ostreams that we want to support (like |
| 9 | // pipes) do not support seeking. :( :( :( |
| 10 | // |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 11 | // The choice of the deque data structure is influenced by the extremely fast |
| 12 | // "append" speed, plus the free "seek"/replace in the middle of the stream. I |
| 13 | // didn't use a vector because the stream could end up very large and copying |
| 14 | // the whole thing to reallocate would be kinda silly. |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 15 | // |
| 16 | // Note that the performance of this library is not terribly important, because |
| 17 | // it shouldn't be used by JIT type applications... so it is not a huge focus |
| 18 | // at least. :) |
| 19 | // |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | |
| 22 | #include "WriterInternals.h" |
Chris Lattner | 635cd93 | 2002-07-23 19:56:44 +0000 | [diff] [blame] | 23 | #include "llvm/Bytecode/WriteBytecodePass.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 24 | #include "llvm/Module.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 25 | #include "llvm/SymbolTable.h" |
| 26 | #include "llvm/DerivedTypes.h" |
Chris Lattner | f60dc88 | 2001-11-29 16:32:16 +0000 | [diff] [blame] | 27 | #include "Support/STLExtras.h" |
Chris Lattner | a92f696 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 28 | #include "Support/Statistic.h" |
John Criswell | 7a73b80 | 2003-06-30 21:59:07 +0000 | [diff] [blame] | 29 | #include "Config/string.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 30 | #include <algorithm> |
| 31 | |
Chris Lattner | 635cd93 | 2002-07-23 19:56:44 +0000 | [diff] [blame] | 32 | static RegisterPass<WriteBytecodePass> X("emitbytecode", "Bytecode Writer"); |
| 33 | |
Chris Lattner | ce6ef11 | 2002-07-26 18:40:14 +0000 | [diff] [blame] | 34 | static Statistic<> |
Chris Lattner | a92f696 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 35 | BytesWritten("bytecodewriter", "Number of bytecode bytes written"); |
Chris Lattner | 635cd93 | 2002-07-23 19:56:44 +0000 | [diff] [blame] | 36 | |
| 37 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 38 | BytecodeWriter::BytecodeWriter(std::deque<unsigned char> &o, const Module *M) |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 39 | : Out(o), Table(M, false) { |
| 40 | |
| 41 | outputSignature(); |
| 42 | |
| 43 | // Emit the top level CLASS block. |
| 44 | BytecodeBlock ModuleBlock(BytecodeFormat::Module, Out); |
| 45 | |
Chris Lattner | d445c6b | 2003-08-24 13:47:36 +0000 | [diff] [blame] | 46 | bool isBigEndian = M->getEndianness() == Module::BigEndian; |
| 47 | bool hasLongPointers = M->getPointerSize() == Module::Pointer64; |
| 48 | bool hasNoEndianness = M->getEndianness() == Module::AnyEndianness; |
| 49 | bool hasNoPointerSize = M->getPointerSize() == Module::AnyPointerSize; |
Chris Lattner | 186a1f7 | 2003-03-19 20:56:46 +0000 | [diff] [blame] | 50 | |
Chris Lattner | e3869c8 | 2003-04-16 21:16:05 +0000 | [diff] [blame] | 51 | // Output the version identifier... we are currently on bytecode version #2 |
Chris Lattner | d445c6b | 2003-08-24 13:47:36 +0000 | [diff] [blame] | 52 | unsigned Version = (2 << 4) | isBigEndian | (hasLongPointers << 1) | |
| 53 | (hasNoEndianness << 2) | (hasNoPointerSize << 3); |
Chris Lattner | 186a1f7 | 2003-03-19 20:56:46 +0000 | [diff] [blame] | 54 | output_vbr(Version, Out); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 55 | align32(Out); |
| 56 | |
Chris Lattner | 186a1f7 | 2003-03-19 20:56:46 +0000 | [diff] [blame] | 57 | { |
| 58 | BytecodeBlock CPool(BytecodeFormat::GlobalTypePlane, Out); |
| 59 | |
| 60 | // Write the type plane for types first because earlier planes (e.g. for a |
| 61 | // primitive type like float) may have constants constructed using types |
| 62 | // coming later (e.g., via getelementptr from a pointer type). The type |
| 63 | // plane is needed before types can be fwd or bkwd referenced. |
| 64 | const std::vector<const Value*> &Plane = Table.getPlane(Type::TypeTyID); |
| 65 | assert(!Plane.empty() && "No types at all?"); |
| 66 | unsigned ValNo = Type::FirstDerivedTyID; // Start at the derived types... |
| 67 | outputConstantsInPlane(Plane, ValNo); // Write out the types |
| 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 | |
Vikram S. Adve | a7dac3d | 2002-07-14 23:07:51 +0000 | [diff] [blame] | 84 | // Helper function for outputConstants(). |
| 85 | // Writes out all the constants in the plane Plane starting at entry StartNo. |
| 86 | // |
| 87 | void BytecodeWriter::outputConstantsInPlane(const std::vector<const Value*> |
| 88 | &Plane, unsigned StartNo) { |
| 89 | unsigned ValNo = StartNo; |
| 90 | |
Chris Lattner | 186a1f7 | 2003-03-19 20:56:46 +0000 | [diff] [blame] | 91 | // Scan through and ignore function arguments/global values... |
| 92 | for (; ValNo < Plane.size() && (isa<Argument>(Plane[ValNo]) || |
| 93 | isa<GlobalValue>(Plane[ValNo])); ValNo++) |
Vikram S. Adve | a7dac3d | 2002-07-14 23:07:51 +0000 | [diff] [blame] | 94 | /*empty*/; |
| 95 | |
| 96 | unsigned NC = ValNo; // Number of constants |
| 97 | for (; NC < Plane.size() && |
| 98 | (isa<Constant>(Plane[NC]) || isa<Type>(Plane[NC])); NC++) |
| 99 | /*empty*/; |
| 100 | NC -= ValNo; // Convert from index into count |
| 101 | if (NC == 0) return; // Skip empty type planes... |
| 102 | |
| 103 | // Output type header: [num entries][type id number] |
| 104 | // |
| 105 | output_vbr(NC, Out); |
| 106 | |
| 107 | // Output the Type ID Number... |
| 108 | int Slot = Table.getValSlot(Plane.front()->getType()); |
| 109 | assert (Slot != -1 && "Type in constant pool but not in function!!"); |
| 110 | output_vbr((unsigned)Slot, Out); |
| 111 | |
| 112 | //cerr << "Emitting " << NC << " constants of type '" |
| 113 | // << Plane.front()->getType()->getName() << "' = Slot #" << Slot << "\n"; |
| 114 | |
| 115 | for (unsigned i = ValNo; i < ValNo+NC; ++i) { |
| 116 | const Value *V = Plane[i]; |
| 117 | if (const Constant *CPV = dyn_cast<Constant>(V)) { |
| 118 | //cerr << "Serializing value: <" << V->getType() << ">: " << V << ":" |
| 119 | // << Out.size() << "\n"; |
| 120 | outputConstant(CPV); |
| 121 | } else { |
Chris Lattner | 186a1f7 | 2003-03-19 20:56:46 +0000 | [diff] [blame] | 122 | outputType(cast<Type>(V)); |
Vikram S. Adve | a7dac3d | 2002-07-14 23:07:51 +0000 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 127 | void BytecodeWriter::outputConstants(bool isFunction) { |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 128 | BytecodeBlock CPool(BytecodeFormat::ConstantPool, Out); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 129 | |
| 130 | unsigned NumPlanes = Table.getNumPlanes(); |
Chris Lattner | f69315b | 2003-05-22 18:35:38 +0000 | [diff] [blame] | 131 | |
| 132 | // Output the type plane before any constants! |
| 133 | if (isFunction && NumPlanes > Type::TypeTyID) { |
| 134 | const std::vector<const Value*> &Plane = Table.getPlane(Type::TypeTyID); |
Chris Lattner | 186a1f7 | 2003-03-19 20:56:46 +0000 | [diff] [blame] | 135 | if (!Plane.empty()) { // Skip empty type planes... |
Chris Lattner | f69315b | 2003-05-22 18:35:38 +0000 | [diff] [blame] | 136 | unsigned ValNo = Table.getModuleLevel(Type::TypeTyID); |
| 137 | outputConstantsInPlane(Plane, ValNo); |
Chris Lattner | 6463e0d | 2002-10-14 03:34:17 +0000 | [diff] [blame] | 138 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 139 | } |
Chris Lattner | f69315b | 2003-05-22 18:35:38 +0000 | [diff] [blame] | 140 | |
| 141 | for (unsigned pno = 0; pno != NumPlanes; pno++) |
| 142 | if (pno != Type::TypeTyID) { // Type plane handled above. |
| 143 | const std::vector<const Value*> &Plane = Table.getPlane(pno); |
| 144 | if (!Plane.empty()) { // Skip empty type planes... |
| 145 | unsigned ValNo = 0; |
Misha Brukman | 37f92e2 | 2003-09-11 22:34:13 +0000 | [diff] [blame] | 146 | if (isFunction) // Don't re-emit module constants |
Chris Lattner | f69315b | 2003-05-22 18:35:38 +0000 | [diff] [blame] | 147 | ValNo += Table.getModuleLevel(pno); |
| 148 | |
| 149 | if (pno >= Type::FirstDerivedTyID) { |
| 150 | // Skip zero initializer |
| 151 | if (ValNo == 0) |
| 152 | ValNo = 1; |
| 153 | } |
| 154 | |
| 155 | // Write out constants in the plane |
| 156 | outputConstantsInPlane(Plane, ValNo); |
| 157 | } |
| 158 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | void BytecodeWriter::outputModuleInfoBlock(const Module *M) { |
| 162 | BytecodeBlock ModuleInfoBlock(BytecodeFormat::ModuleGlobalInfo, Out); |
| 163 | |
Chris Lattner | 70cc339 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 164 | // Output the types for the global variables in the module... |
| 165 | for (Module::const_giterator I = M->gbegin(), End = M->gend(); I != End;++I) { |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 166 | int Slot = Table.getValSlot(I->getType()); |
Chris Lattner | 70cc339 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 167 | assert(Slot != -1 && "Module global vars is broken!"); |
Chris Lattner | d70684f | 2001-09-18 04:01:05 +0000 | [diff] [blame] | 168 | |
Chris Lattner | e3869c8 | 2003-04-16 21:16:05 +0000 | [diff] [blame] | 169 | // Fields: bit0 = isConstant, bit1 = hasInitializer, bit2,3=Linkage, |
| 170 | // bit4+ = Slot # for type |
| 171 | unsigned oSlot = ((unsigned)Slot << 4) | ((unsigned)I->getLinkage() << 2) | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 172 | (I->hasInitializer() << 1) | I->isConstant(); |
Chris Lattner | d70684f | 2001-09-18 04:01:05 +0000 | [diff] [blame] | 173 | output_vbr(oSlot, Out); |
| 174 | |
Chris Lattner | 1b98c5c | 2001-10-13 06:48:38 +0000 | [diff] [blame] | 175 | // If we have an initializer, output it now. |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 176 | if (I->hasInitializer()) { |
| 177 | Slot = Table.getValSlot((Value*)I->getInitializer()); |
Chris Lattner | d70684f | 2001-09-18 04:01:05 +0000 | [diff] [blame] | 178 | assert(Slot != -1 && "No slot for global var initializer!"); |
| 179 | output_vbr((unsigned)Slot, Out); |
| 180 | } |
Chris Lattner | 70cc339 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 181 | } |
| 182 | output_vbr((unsigned)Table.getValSlot(Type::VoidTy), Out); |
| 183 | |
Chris Lattner | b579400 | 2002-04-07 22:49:37 +0000 | [diff] [blame] | 184 | // Output the types of the functions in this module... |
Chris Lattner | 7fc9fe3 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 185 | for (Module::const_iterator I = M->begin(), End = M->end(); I != End; ++I) { |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 186 | int Slot = Table.getValSlot(I->getType()); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 187 | assert(Slot != -1 && "Module const pool is broken!"); |
| 188 | assert(Slot >= Type::FirstDerivedTyID && "Derived type not in range!"); |
| 189 | output_vbr((unsigned)Slot, Out); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 190 | } |
| 191 | output_vbr((unsigned)Table.getValSlot(Type::VoidTy), Out); |
Chris Lattner | 70cc339 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 192 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 193 | align32(Out); |
| 194 | } |
| 195 | |
Chris Lattner | 186a1f7 | 2003-03-19 20:56:46 +0000 | [diff] [blame] | 196 | void BytecodeWriter::outputFunction(const Function *F) { |
Chris Lattner | c9aa7df | 2002-03-29 03:51:11 +0000 | [diff] [blame] | 197 | BytecodeBlock FunctionBlock(BytecodeFormat::Function, Out); |
Chris Lattner | e3869c8 | 2003-04-16 21:16:05 +0000 | [diff] [blame] | 198 | output_vbr((unsigned)F->getLinkage(), Out); |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 199 | // Only output the constant pool and other goodies if needed... |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 200 | if (!F->isExternal()) { |
Chris Lattner | d23b1d3 | 2001-11-26 18:56:10 +0000 | [diff] [blame] | 201 | |
Chris Lattner | b579400 | 2002-04-07 22:49:37 +0000 | [diff] [blame] | 202 | // Get slot information about the function... |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 203 | Table.incorporateFunction(F); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 204 | |
Chris Lattner | b579400 | 2002-04-07 22:49:37 +0000 | [diff] [blame] | 205 | // Output information about the constants in the function... |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 206 | outputConstants(true); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 207 | |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 208 | // Output basic block nodes... |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 209 | for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I) |
| 210 | processBasicBlock(*I); |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 211 | |
Chris Lattner | b579400 | 2002-04-07 22:49:37 +0000 | [diff] [blame] | 212 | // If needed, output the symbol table for the function... |
Chris Lattner | 6e6026b | 2002-11-20 18:36:02 +0000 | [diff] [blame] | 213 | outputSymbolTable(F->getSymbolTable()); |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 214 | |
Chris Lattner | b579400 | 2002-04-07 22:49:37 +0000 | [diff] [blame] | 215 | Table.purgeFunction(); |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 216 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 220 | void BytecodeWriter::processBasicBlock(const BasicBlock &BB) { |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 221 | BytecodeBlock FunctionBlock(BytecodeFormat::BasicBlock, Out); |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 222 | // Process all the instructions in the bb... |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 223 | for(BasicBlock::const_iterator I = BB.begin(), E = BB.end(); I != E; ++I) |
| 224 | processInstruction(*I); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | void BytecodeWriter::outputSymbolTable(const SymbolTable &MST) { |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 228 | BytecodeBlock FunctionBlock(BytecodeFormat::SymbolTable, Out); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 229 | |
Chris Lattner | 7fc9fe3 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 230 | for (SymbolTable::const_iterator TI = MST.begin(); TI != MST.end(); ++TI) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 231 | SymbolTable::type_const_iterator I = MST.type_begin(TI->first); |
| 232 | SymbolTable::type_const_iterator End = MST.type_end(TI->first); |
| 233 | int Slot; |
| 234 | |
| 235 | if (I == End) continue; // Don't mess with an absent type... |
| 236 | |
| 237 | // Symtab block header: [num entries][type id number] |
| 238 | output_vbr(MST.type_size(TI->first), Out); |
| 239 | |
| 240 | Slot = Table.getValSlot(TI->first); |
| 241 | assert(Slot != -1 && "Type in symtab, but not in table!"); |
| 242 | output_vbr((unsigned)Slot, Out); |
| 243 | |
Chris Lattner | 7fc9fe3 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 244 | for (; I != End; ++I) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 245 | // Symtab entry: [def slot #][name] |
| 246 | Slot = Table.getValSlot(I->second); |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 247 | assert(Slot != -1 && "Value in symtab but has no slot number!!"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 248 | output_vbr((unsigned)Slot, Out); |
| 249 | output(I->first, Out, false); // Don't force alignment... |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | |
Anand Shukla | eea60fc | 2002-06-25 20:44:04 +0000 | [diff] [blame] | 254 | void WriteBytecodeToFile(const Module *C, std::ostream &Out) { |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 255 | assert(C && "You can't write a null module!!"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 256 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 257 | std::deque<unsigned char> Buffer; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 258 | |
| 259 | // This object populates buffer for us... |
| 260 | BytecodeWriter BCW(Buffer, C); |
| 261 | |
Chris Lattner | ce6ef11 | 2002-07-26 18:40:14 +0000 | [diff] [blame] | 262 | // Keep track of how much we've written... |
| 263 | BytesWritten += Buffer.size(); |
| 264 | |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 265 | // Okay, write the deque out to the ostream now... the deque is not |
| 266 | // sequential in memory, however, so write out as much as possible in big |
| 267 | // chunks, until we're done. |
| 268 | // |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 269 | std::deque<unsigned char>::const_iterator I = Buffer.begin(),E = Buffer.end(); |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 270 | while (I != E) { // Loop until it's all written |
| 271 | // Scan to see how big this chunk is... |
| 272 | const unsigned char *ChunkPtr = &*I; |
| 273 | const unsigned char *LastPtr = ChunkPtr; |
| 274 | while (I != E) { |
| 275 | const unsigned char *ThisPtr = &*++I; |
Chris Lattner | 5cb1741 | 2001-11-04 21:32:41 +0000 | [diff] [blame] | 276 | if (LastPtr+1 != ThisPtr) { // Advanced by more than a byte of memory? |
| 277 | ++LastPtr; |
| 278 | break; |
| 279 | } |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 280 | LastPtr = ThisPtr; |
| 281 | } |
| 282 | |
| 283 | // Write out the chunk... |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 284 | Out.write((char*)ChunkPtr, LastPtr-ChunkPtr); |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 287 | Out.flush(); |
| 288 | } |