blob: 395386d6621a8bb015135a9863d0c2a72d582c5c [file] [log] [blame]
Chris Lattner14999342004-01-10 19:07:06 +00001//===-- Writer.cpp - Library for writing LLVM bytecode files --------------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
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 Lattner00950542001-06-06 20:29:01 +00009//
10// This library implements the functionality defined in llvm/Bytecode/Writer.h
11//
Chris Lattner00950542001-06-06 20:29:01 +000012// Note that this file uses an unusual technique of outputting all the bytecode
Chris Lattnerabe83ae2003-09-15 00:33:20 +000013// to a deque of unsigned char, then copies the deque to an ostream. The
Chris Lattner00950542001-06-06 20:29:01 +000014// 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 Lattnere8fdde12001-09-07 16:39:41 +000018// 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 Lattner00950542001-06-06 20:29:01 +000022//
Chris Lattner00950542001-06-06 20:29:01 +000023//===----------------------------------------------------------------------===//
24
25#include "WriterInternals.h"
Chris Lattner635cd932002-07-23 19:56:44 +000026#include "llvm/Bytecode/WriteBytecodePass.h"
Chris Lattner83bb3d22004-01-14 23:36:54 +000027#include "llvm/Constants.h"
28#include "llvm/DerivedTypes.h"
Chris Lattner00950542001-06-06 20:29:01 +000029#include "llvm/Module.h"
Chris Lattner00950542001-06-06 20:29:01 +000030#include "llvm/SymbolTable.h"
Chris Lattnerf60dc882001-11-29 16:32:16 +000031#include "Support/STLExtras.h"
Chris Lattnera92f6962002-10-01 22:38:41 +000032#include "Support/Statistic.h"
Chris Lattner32abce62004-01-10 19:10:01 +000033#include <cstring>
Chris Lattner00950542001-06-06 20:29:01 +000034#include <algorithm>
Chris Lattner44f549b2004-01-10 18:49:43 +000035using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000036
Chris Lattner635cd932002-07-23 19:56:44 +000037static RegisterPass<WriteBytecodePass> X("emitbytecode", "Bytecode Writer");
38
Chris Lattnerce6ef112002-07-26 18:40:14 +000039static Statistic<>
Chris Lattnera92f6962002-10-01 22:38:41 +000040BytesWritten("bytecodewriter", "Number of bytecode bytes written");
Chris Lattner635cd932002-07-23 19:56:44 +000041
Chris Lattner697954c2002-01-20 22:54:45 +000042BytecodeWriter::BytecodeWriter(std::deque<unsigned char> &o, const Module *M)
Reid Spencer798ff642004-05-26 07:37:11 +000043 : Out(o), Table(M) {
Chris Lattner00950542001-06-06 20:29:01 +000044
Chris Lattner83bb3d22004-01-14 23:36:54 +000045 // Emit the signature...
46 static const unsigned char *Sig = (const unsigned char*)"llvm";
47 output_data(Sig, Sig+4, Out);
Chris Lattner00950542001-06-06 20:29:01 +000048
49 // Emit the top level CLASS block.
50 BytecodeBlock ModuleBlock(BytecodeFormat::Module, Out);
51
Chris Lattnerd445c6b2003-08-24 13:47:36 +000052 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 Lattner186a1f72003-03-19 20:56:46 +000056
Chris Lattner5fa428f2004-04-05 01:27:26 +000057 // Output the version identifier... we are currently on bytecode version #2,
58 // which corresponds to LLVM v1.3.
Chris Lattner036de032004-06-25 20:52:10 +000059 unsigned Version = (2 << 4) | (unsigned)isBigEndian | (hasLongPointers << 1) |
Chris Lattnerd445c6b2003-08-24 13:47:36 +000060 (hasNoEndianness << 2) | (hasNoPointerSize << 3);
Chris Lattner186a1f72003-03-19 20:56:46 +000061 output_vbr(Version, Out);
Chris Lattner00950542001-06-06 20:29:01 +000062 align32(Out);
63
Reid Spencercb3595c2004-07-04 11:45:47 +000064 // The Global type plane comes first
Chris Lattner186a1f72003-03-19 20:56:46 +000065 {
Reid Spencercb3595c2004-07-04 11:45:47 +000066 BytecodeBlock CPool(BytecodeFormat::GlobalTypePlane, Out );
67 outputTypes(Type::FirstDerivedTyID);
Chris Lattner186a1f72003-03-19 20:56:46 +000068 }
Chris Lattner00950542001-06-06 20:29:01 +000069
Chris Lattner186a1f72003-03-19 20:56:46 +000070 // The ModuleInfoBlock follows directly after the type information
Chris Lattnere8fdde12001-09-07 16:39:41 +000071 outputModuleInfoBlock(M);
72
Chris Lattner186a1f72003-03-19 20:56:46 +000073 // Output module level constants, used for global variable initializers
74 outputConstants(false);
75
Chris Lattnerb5794002002-04-07 22:49:37 +000076 // Do the whole module now! Process each function at a time...
Chris Lattner0b12b5f2002-06-25 16:13:21 +000077 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
Chris Lattner186a1f72003-03-19 20:56:46 +000078 outputFunction(I);
Chris Lattnere8fdde12001-09-07 16:39:41 +000079
80 // If needed, output the symbol table for the module...
Chris Lattner6e6026b2002-11-20 18:36:02 +000081 outputSymbolTable(M->getSymbolTable());
Chris Lattner00950542001-06-06 20:29:01 +000082}
83
Reid Spencercb3595c2004-07-04 11:45:47 +000084void 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. Advea7dac3d2002-07-14 23:07:51 +0000103// Helper function for outputConstants().
104// Writes out all the constants in the plane Plane starting at entry StartNo.
105//
106void BytecodeWriter::outputConstantsInPlane(const std::vector<const Value*>
107 &Plane, unsigned StartNo) {
108 unsigned ValNo = StartNo;
109
Chris Lattner83bb3d22004-01-14 23:36:54 +0000110 // 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. Advea7dac3d2002-07-14 23:07:51 +0000116 /*empty*/;
117
118 unsigned NC = ValNo; // Number of constants
Reid Spencercb3595c2004-07-04 11:45:47 +0000119 for (; NC < Plane.size() && (isa<Constant>(Plane[NC])); NC++)
Vikram S. Advea7dac3d2002-07-14 23:07:51 +0000120 /*empty*/;
121 NC -= ValNo; // Convert from index into count
122 if (NC == 0) return; // Skip empty type planes...
123
Chris Lattnerd6942d72004-01-14 16:54:21 +0000124 // FIXME: Most slabs only have 1 or 2 entries! We should encode this much
125 // more compactly.
126
Vikram S. Advea7dac3d2002-07-14 23:07:51 +0000127 // Output type header: [num entries][type id number]
128 //
129 output_vbr(NC, Out);
130
131 // Output the Type ID Number...
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000132 int Slot = Table.getSlot(Plane.front()->getType());
Vikram S. Advea7dac3d2002-07-14 23:07:51 +0000133 assert (Slot != -1 && "Type in constant pool but not in function!!");
134 output_vbr((unsigned)Slot, Out);
135
Vikram S. Advea7dac3d2002-07-14 23:07:51 +0000136 for (unsigned i = ValNo; i < ValNo+NC; ++i) {
137 const Value *V = Plane[i];
Reid Spencere0125b62004-07-18 00:16:21 +0000138 if (const Constant *C = dyn_cast<Constant>(V)) {
139 outputConstant(C);
Vikram S. Advea7dac3d2002-07-14 23:07:51 +0000140 }
141 }
142}
143
Chris Lattner80b97342004-01-17 23:25:43 +0000144static inline bool hasNullValue(unsigned TyID) {
Reid Spencercb3595c2004-07-04 11:45:47 +0000145 return TyID != Type::LabelTyID && TyID != Type::VoidTyID;
Chris Lattner80b97342004-01-17 23:25:43 +0000146}
147
Chris Lattner79df7c02002-03-26 18:01:55 +0000148void BytecodeWriter::outputConstants(bool isFunction) {
Chris Lattner0baa0af2004-01-15 21:06:57 +0000149 BytecodeBlock CPool(BytecodeFormat::ConstantPool, Out,
150 true /* Elide block if empty */);
Chris Lattner00950542001-06-06 20:29:01 +0000151
152 unsigned NumPlanes = Table.getNumPlanes();
Chris Lattnerf69315b2003-05-22 18:35:38 +0000153
Reid Spencere0125b62004-07-18 00:16:21 +0000154 if (isFunction)
155 // Output the type plane before any constants!
Reid Spencercb3595c2004-07-04 11:45:47 +0000156 outputTypes( Table.getModuleTypeLevel() );
Reid Spencere0125b62004-07-18 00:16:21 +0000157 else
158 // Output module-level string constants before any other constants.x
Chris Lattner83bb3d22004-01-14 23:36:54 +0000159 outputConstantStrings();
160
Reid Spencercb3595c2004-07-04 11:45:47 +0000161 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 Spencer0852c802004-07-04 11:46:15 +0000166 ValNo += Table.getModuleLevel(pno);
Reid Spencercb3595c2004-07-04 11:45:47 +0000167
168 if (hasNullValue(pno)) {
Reid Spencer0852c802004-07-04 11:46:15 +0000169 // Skip zero initializer
170 if (ValNo == 0)
171 ValNo = 1;
Chris Lattnerf69315b2003-05-22 18:35:38 +0000172 }
Reid Spencercb3595c2004-07-04 11:45:47 +0000173
174 // Write out constants in the plane
175 outputConstantsInPlane(Plane, ValNo);
Chris Lattnerf69315b2003-05-22 18:35:38 +0000176 }
Reid Spencercb3595c2004-07-04 11:45:47 +0000177 }
Chris Lattner00950542001-06-06 20:29:01 +0000178}
179
Chris Lattner6b252422003-10-16 18:28:50 +0000180static unsigned getEncodedLinkage(const GlobalValue *GV) {
181 switch (GV->getLinkage()) {
182 default: assert(0 && "Invalid linkage!");
183 case GlobalValue::ExternalLinkage: return 0;
Chris Lattner6b252422003-10-16 18:28:50 +0000184 case GlobalValue::WeakLinkage: return 1;
185 case GlobalValue::AppendingLinkage: return 2;
186 case GlobalValue::InternalLinkage: return 3;
Chris Lattner22482a12003-10-18 06:30:21 +0000187 case GlobalValue::LinkOnceLinkage: return 4;
Chris Lattner6b252422003-10-16 18:28:50 +0000188 }
189}
190
Chris Lattner00950542001-06-06 20:29:01 +0000191void BytecodeWriter::outputModuleInfoBlock(const Module *M) {
192 BytecodeBlock ModuleInfoBlock(BytecodeFormat::ModuleGlobalInfo, Out);
193
Chris Lattner70cc3392001-09-10 07:58:01 +0000194 // 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 Evlogimenos60596382003-10-17 02:02:40 +0000196 int Slot = Table.getSlot(I->getType());
Chris Lattner70cc3392001-09-10 07:58:01 +0000197 assert(Slot != -1 && "Module global vars is broken!");
Chris Lattnerd70684f2001-09-18 04:01:05 +0000198
Chris Lattner22482a12003-10-18 06:30:21 +0000199 // Fields: bit0 = isConstant, bit1 = hasInitializer, bit2-4=Linkage,
200 // bit5+ = Slot # for type
201 unsigned oSlot = ((unsigned)Slot << 5) | (getEncodedLinkage(I) << 2) |
Chris Lattner036de032004-06-25 20:52:10 +0000202 (I->hasInitializer() << 1) | (unsigned)I->isConstant();
Chris Lattnerd70684f2001-09-18 04:01:05 +0000203 output_vbr(oSlot, Out);
204
Chris Lattner1b98c5c2001-10-13 06:48:38 +0000205 // If we have an initializer, output it now.
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000206 if (I->hasInitializer()) {
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000207 Slot = Table.getSlot((Value*)I->getInitializer());
Chris Lattnerd70684f2001-09-18 04:01:05 +0000208 assert(Slot != -1 && "No slot for global var initializer!");
209 output_vbr((unsigned)Slot, Out);
210 }
Chris Lattner70cc3392001-09-10 07:58:01 +0000211 }
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000212 output_vbr((unsigned)Table.getSlot(Type::VoidTy), Out);
Chris Lattner70cc3392001-09-10 07:58:01 +0000213
Chris Lattnerb5794002002-04-07 22:49:37 +0000214 // Output the types of the functions in this module...
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000215 for (Module::const_iterator I = M->begin(), End = M->end(); I != End; ++I) {
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000216 int Slot = Table.getSlot(I->getType());
Chris Lattner00950542001-06-06 20:29:01 +0000217 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 Lattner00950542001-06-06 20:29:01 +0000220 }
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000221 output_vbr((unsigned)Table.getSlot(Type::VoidTy), Out);
Chris Lattner00950542001-06-06 20:29:01 +0000222}
223
Chris Lattnercf3e67f2004-01-18 21:08:52 +0000224void BytecodeWriter::outputInstructions(const Function *F) {
225 BytecodeBlock ILBlock(BytecodeFormat::InstructionList, Out);
Chris Lattnercf3e67f2004-01-18 21:08:52 +0000226 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 Lattnercf3e67f2004-01-18 21:08:52 +0000229}
230
Chris Lattner186a1f72003-03-19 20:56:46 +0000231void BytecodeWriter::outputFunction(const Function *F) {
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000232 BytecodeBlock FunctionBlock(BytecodeFormat::Function, Out);
Chris Lattner6b252422003-10-16 18:28:50 +0000233 output_vbr(getEncodedLinkage(F), Out);
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000234
Chris Lattnercf3e67f2004-01-18 21:08:52 +0000235 // If this is an external function, there is nothing else to emit!
236 if (F->isExternal()) return;
Chris Lattner00950542001-06-06 20:29:01 +0000237
Chris Lattnercf3e67f2004-01-18 21:08:52 +0000238 // 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 Lattnere8fdde12001-09-07 16:39:41 +0000244 outputConstants(true);
Chris Lattnercf3e67f2004-01-18 21:08:52 +0000245 } else {
246 // Otherwise, emit the compaction table.
247 outputCompactionTable();
Chris Lattnere8fdde12001-09-07 16:39:41 +0000248 }
Chris Lattnercf3e67f2004-01-18 21:08:52 +0000249
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
259void BytecodeWriter::outputCompactionTablePlane(unsigned PlaneNo,
260 const std::vector<const Value*> &Plane,
261 unsigned StartNo) {
262 unsigned End = Table.getModuleLevel(PlaneNo);
Chris Lattner52f86d62004-01-20 00:54:06 +0000263 if (Plane.empty() || StartNo == End || End == 0) return; // Nothing to emit
Chris Lattnercf3e67f2004-01-18 21:08:52 +0000264 assert(StartNo < End && "Cannot emit negative range!");
265 assert(StartNo < Plane.size() && End <= Plane.size());
266
Chris Lattnercf3e67f2004-01-18 21:08:52 +0000267 // Do not emit the null initializer!
Reid Spencercb3595c2004-07-04 11:45:47 +0000268 ++StartNo;
Chris Lattnercf3e67f2004-01-18 21:08:52 +0000269
Chris Lattner24102432004-01-18 22:35:34 +0000270 // 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 Lattnercf3e67f2004-01-18 21:08:52 +0000285 for (unsigned i = StartNo; i != End; ++i)
286 output_vbr(Table.getGlobalSlot(Plane[i]), Out);
287}
288
Reid Spencercb3595c2004-07-04 11:45:47 +0000289void 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 Lattnercf3e67f2004-01-18 21:08:52 +0000312void BytecodeWriter::outputCompactionTable() {
Chris Lattnercf3e67f2004-01-18 21:08:52 +0000313 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 Spencercb3595c2004-07-04 11:45:47 +0000317 outputCompactionTypes(Type::FirstDerivedTyID);
Chris Lattnercf3e67f2004-01-18 21:08:52 +0000318
319 for (unsigned i = 0, e = CT.size(); i != e; ++i)
Reid Spencercb3595c2004-07-04 11:45:47 +0000320 outputCompactionTablePlane(i, CT[i], 0);
Chris Lattner00950542001-06-06 20:29:01 +0000321}
322
Chris Lattner00950542001-06-06 20:29:01 +0000323void BytecodeWriter::outputSymbolTable(const SymbolTable &MST) {
Chris Lattner737d3cd2004-01-10 19:56:59 +0000324 // Do not output the Bytecode block for an empty symbol table, it just wastes
325 // space!
Reid Spencer6ed81e22004-05-27 20:18:51 +0000326 if ( MST.isEmpty() ) return;
Chris Lattner737d3cd2004-01-10 19:56:59 +0000327
Chris Lattner0baa0af2004-01-15 21:06:57 +0000328 BytecodeBlock SymTabBlock(BytecodeFormat::SymbolTable, Out,
329 true/* ElideIfEmpty*/);
Chris Lattner00950542001-06-06 20:29:01 +0000330
Reid Spencercb3595c2004-07-04 11:45:47 +0000331 //Symtab block header for types: [num entries]
Reid Spencer94f2df22004-05-25 17:29:59 +0000332 output_vbr(MST.num_types(), Out);
Reid Spencer94f2df22004-05-25 17:29:59 +0000333 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 Lattner00950542001-06-06 20:29:01 +0000345 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 Spencer94f2df22004-05-25 17:29:59 +0000350 output_vbr(MST.type_size(PI->first), Out);
Chris Lattner00950542001-06-06 20:29:01 +0000351
Reid Spencer94f2df22004-05-25 17:29:59 +0000352 Slot = Table.getSlot(PI->first);
Chris Lattner00950542001-06-06 20:29:01 +0000353 assert(Slot != -1 && "Type in symtab, but not in table!");
354 output_vbr((unsigned)Slot, Out);
355
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000356 for (; I != End; ++I) {
Chris Lattner00950542001-06-06 20:29:01 +0000357 // Symtab entry: [def slot #][name]
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000358 Slot = Table.getSlot(I->second);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000359 assert(Slot != -1 && "Value in symtab but has no slot number!!");
Chris Lattner00950542001-06-06 20:29:01 +0000360 output_vbr((unsigned)Slot, Out);
361 output(I->first, Out, false); // Don't force alignment...
362 }
363 }
364}
365
Chris Lattner44f549b2004-01-10 18:49:43 +0000366void llvm::WriteBytecodeToFile(const Module *C, std::ostream &Out) {
Chris Lattnere8fdde12001-09-07 16:39:41 +0000367 assert(C && "You can't write a null module!!");
Chris Lattner00950542001-06-06 20:29:01 +0000368
Chris Lattner697954c2002-01-20 22:54:45 +0000369 std::deque<unsigned char> Buffer;
Chris Lattner00950542001-06-06 20:29:01 +0000370
371 // This object populates buffer for us...
372 BytecodeWriter BCW(Buffer, C);
373
Chris Lattnerce6ef112002-07-26 18:40:14 +0000374 // Keep track of how much we've written...
375 BytesWritten += Buffer.size();
376
Chris Lattnere8fdde12001-09-07 16:39:41 +0000377 // 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 Lattner036de032004-06-25 20:52:10 +0000381
Chris Lattner697954c2002-01-20 22:54:45 +0000382 std::deque<unsigned char>::const_iterator I = Buffer.begin(),E = Buffer.end();
Chris Lattnere8fdde12001-09-07 16:39:41 +0000383 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 Lattner036de032004-06-25 20:52:10 +0000389 if (++LastPtr != ThisPtr) // Advanced by more than a byte of memory?
Chris Lattner5cb17412001-11-04 21:32:41 +0000390 break;
Chris Lattnere8fdde12001-09-07 16:39:41 +0000391 }
392
393 // Write out the chunk...
Chris Lattnerd6162282004-06-25 00:35:55 +0000394 Out.write((char*)ChunkPtr, unsigned(LastPtr-ChunkPtr));
Chris Lattnere8fdde12001-09-07 16:39:41 +0000395 }
Chris Lattner00950542001-06-06 20:29:01 +0000396 Out.flush();
397}
Reid Spencere0125b62004-07-18 00:16:21 +0000398
399// vim: sw=2 ai