Adding dllimport, dllexport and external weak linkage types.
[oota-llvm.git] / lib / Bytecode / Writer / Writer.cpp
1 //===-- Writer.cpp - Library for writing LLVM bytecode files --------------===//
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 //===----------------------------------------------------------------------===//
9 //
10 // This library implements the functionality defined in llvm/Bytecode/Writer.h
11 //
12 // Note that this file uses an unusual technique of outputting all the bytecode
13 // to a vector of unsigned char, then copies the vector to an ostream.  The
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 //
18 //===----------------------------------------------------------------------===//
19
20 #include "WriterInternals.h"
21 #include "llvm/Bytecode/WriteBytecodePass.h"
22 #include "llvm/CallingConv.h"
23 #include "llvm/Constants.h"
24 #include "llvm/DerivedTypes.h"
25 #include "llvm/InlineAsm.h"
26 #include "llvm/Instructions.h"
27 #include "llvm/Module.h"
28 #include "llvm/SymbolTable.h"
29 #include "llvm/Support/GetElementPtrTypeIterator.h"
30 #include "llvm/Support/Compressor.h"
31 #include "llvm/Support/MathExtras.h"
32 #include "llvm/System/Program.h"
33 #include "llvm/ADT/STLExtras.h"
34 #include "llvm/ADT/Statistic.h"
35 #include <cstring>
36 #include <algorithm>
37 using namespace llvm;
38
39 /// This value needs to be incremented every time the bytecode format changes
40 /// so that the reader can distinguish which format of the bytecode file has
41 /// been written.
42 /// @brief The bytecode version number
43 const unsigned BCVersionNum = 5;
44
45 static RegisterPass<WriteBytecodePass> X("emitbytecode", "Bytecode Writer");
46
47 static Statistic<>
48 BytesWritten("bytecodewriter", "Number of bytecode bytes written");
49
50 //===----------------------------------------------------------------------===//
51 //===                           Output Primitives                          ===//
52 //===----------------------------------------------------------------------===//
53
54 // output - If a position is specified, it must be in the valid portion of the
55 // string... note that this should be inlined always so only the relevant IF
56 // body should be included.
57 inline void BytecodeWriter::output(unsigned i, int pos) {
58   if (pos == -1) { // Be endian clean, little endian is our friend
59     Out.push_back((unsigned char)i);
60     Out.push_back((unsigned char)(i >> 8));
61     Out.push_back((unsigned char)(i >> 16));
62     Out.push_back((unsigned char)(i >> 24));
63   } else {
64     Out[pos  ] = (unsigned char)i;
65     Out[pos+1] = (unsigned char)(i >> 8);
66     Out[pos+2] = (unsigned char)(i >> 16);
67     Out[pos+3] = (unsigned char)(i >> 24);
68   }
69 }
70
71 inline void BytecodeWriter::output(int i) {
72   output((unsigned)i);
73 }
74
75 /// output_vbr - Output an unsigned value, by using the least number of bytes
76 /// possible.  This is useful because many of our "infinite" values are really
77 /// very small most of the time; but can be large a few times.
78 /// Data format used:  If you read a byte with the high bit set, use the low
79 /// seven bits as data and then read another byte.
80 inline void BytecodeWriter::output_vbr(uint64_t i) {
81   while (1) {
82     if (i < 0x80) { // done?
83       Out.push_back((unsigned char)i);   // We know the high bit is clear...
84       return;
85     }
86
87     // Nope, we are bigger than a character, output the next 7 bits and set the
88     // high bit to say that there is more coming...
89     Out.push_back(0x80 | ((unsigned char)i & 0x7F));
90     i >>= 7;  // Shift out 7 bits now...
91   }
92 }
93
94 inline void BytecodeWriter::output_vbr(unsigned i) {
95   while (1) {
96     if (i < 0x80) { // done?
97       Out.push_back((unsigned char)i);   // We know the high bit is clear...
98       return;
99     }
100
101     // Nope, we are bigger than a character, output the next 7 bits and set the
102     // high bit to say that there is more coming...
103     Out.push_back(0x80 | ((unsigned char)i & 0x7F));
104     i >>= 7;  // Shift out 7 bits now...
105   }
106 }
107
108 inline void BytecodeWriter::output_typeid(unsigned i) {
109   if (i <= 0x00FFFFFF)
110     this->output_vbr(i);
111   else {
112     this->output_vbr(0x00FFFFFF);
113     this->output_vbr(i);
114   }
115 }
116
117 inline void BytecodeWriter::output_vbr(int64_t i) {
118   if (i < 0)
119     output_vbr(((uint64_t)(-i) << 1) | 1); // Set low order sign bit...
120   else
121     output_vbr((uint64_t)i << 1);          // Low order bit is clear.
122 }
123
124
125 inline void BytecodeWriter::output_vbr(int i) {
126   if (i < 0)
127     output_vbr(((unsigned)(-i) << 1) | 1); // Set low order sign bit...
128   else
129     output_vbr((unsigned)i << 1);          // Low order bit is clear.
130 }
131
132 inline void BytecodeWriter::output(const std::string &s) {
133   unsigned Len = s.length();
134   output_vbr(Len);             // Strings may have an arbitrary length.
135   Out.insert(Out.end(), s.begin(), s.end());
136 }
137
138 inline void BytecodeWriter::output_data(const void *Ptr, const void *End) {
139   Out.insert(Out.end(), (const unsigned char*)Ptr, (const unsigned char*)End);
140 }
141
142 inline void BytecodeWriter::output_float(float& FloatVal) {
143   /// FIXME: This isn't optimal, it has size problems on some platforms
144   /// where FP is not IEEE.
145   uint32_t i = FloatToBits(FloatVal);
146   Out.push_back( static_cast<unsigned char>( (i      ) & 0xFF));
147   Out.push_back( static_cast<unsigned char>( (i >> 8 ) & 0xFF));
148   Out.push_back( static_cast<unsigned char>( (i >> 16) & 0xFF));
149   Out.push_back( static_cast<unsigned char>( (i >> 24) & 0xFF));
150 }
151
152 inline void BytecodeWriter::output_double(double& DoubleVal) {
153   /// FIXME: This isn't optimal, it has size problems on some platforms
154   /// where FP is not IEEE.
155   uint64_t i = DoubleToBits(DoubleVal);
156   Out.push_back( static_cast<unsigned char>( (i      ) & 0xFF));
157   Out.push_back( static_cast<unsigned char>( (i >> 8 ) & 0xFF));
158   Out.push_back( static_cast<unsigned char>( (i >> 16) & 0xFF));
159   Out.push_back( static_cast<unsigned char>( (i >> 24) & 0xFF));
160   Out.push_back( static_cast<unsigned char>( (i >> 32) & 0xFF));
161   Out.push_back( static_cast<unsigned char>( (i >> 40) & 0xFF));
162   Out.push_back( static_cast<unsigned char>( (i >> 48) & 0xFF));
163   Out.push_back( static_cast<unsigned char>( (i >> 56) & 0xFF));
164 }
165
166 inline BytecodeBlock::BytecodeBlock(unsigned ID, BytecodeWriter &w,
167                                     bool elideIfEmpty, bool hasLongFormat)
168   : Id(ID), Writer(w), ElideIfEmpty(elideIfEmpty), HasLongFormat(hasLongFormat){
169
170   if (HasLongFormat) {
171     w.output(ID);
172     w.output(0U); // For length in long format
173   } else {
174     w.output(0U); /// Place holder for ID and length for this block
175   }
176   Loc = w.size();
177 }
178
179 inline BytecodeBlock::~BytecodeBlock() { // Do backpatch when block goes out
180                                          // of scope...
181   if (Loc == Writer.size() && ElideIfEmpty) {
182     // If the block is empty, and we are allowed to, do not emit the block at
183     // all!
184     Writer.resize(Writer.size()-(HasLongFormat?8:4));
185     return;
186   }
187
188   if (HasLongFormat)
189     Writer.output(unsigned(Writer.size()-Loc), int(Loc-4));
190   else
191     Writer.output(unsigned(Writer.size()-Loc) << 5 | (Id & 0x1F), int(Loc-4));
192 }
193
194 //===----------------------------------------------------------------------===//
195 //===                           Constant Output                            ===//
196 //===----------------------------------------------------------------------===//
197
198 void BytecodeWriter::outputType(const Type *T) {
199   output_vbr((unsigned)T->getTypeID());
200
201   // That's all there is to handling primitive types...
202   if (T->isPrimitiveType()) {
203     return;     // We might do this if we alias a prim type: %x = type int
204   }
205
206   switch (T->getTypeID()) {   // Handle derived types now.
207   case Type::FunctionTyID: {
208     const FunctionType *MT = cast<FunctionType>(T);
209     int Slot = Table.getSlot(MT->getReturnType());
210     assert(Slot != -1 && "Type used but not available!!");
211     output_typeid((unsigned)Slot);
212
213     // Output the number of arguments to function (+1 if varargs):
214     output_vbr((unsigned)MT->getNumParams()+MT->isVarArg());
215
216     // Output all of the arguments...
217     FunctionType::param_iterator I = MT->param_begin();
218     for (; I != MT->param_end(); ++I) {
219       Slot = Table.getSlot(*I);
220       assert(Slot != -1 && "Type used but not available!!");
221       output_typeid((unsigned)Slot);
222     }
223
224     // Terminate list with VoidTy if we are a varargs function...
225     if (MT->isVarArg())
226       output_typeid((unsigned)Type::VoidTyID);
227     break;
228   }
229
230   case Type::ArrayTyID: {
231     const ArrayType *AT = cast<ArrayType>(T);
232     int Slot = Table.getSlot(AT->getElementType());
233     assert(Slot != -1 && "Type used but not available!!");
234     output_typeid((unsigned)Slot);
235     output_vbr(AT->getNumElements());
236     break;
237   }
238
239  case Type::PackedTyID: {
240     const PackedType *PT = cast<PackedType>(T);
241     int Slot = Table.getSlot(PT->getElementType());
242     assert(Slot != -1 && "Type used but not available!!");
243     output_typeid((unsigned)Slot);
244     output_vbr(PT->getNumElements());
245     break;
246   }
247
248
249   case Type::StructTyID: {
250     const StructType *ST = cast<StructType>(T);
251
252     // Output all of the element types...
253     for (StructType::element_iterator I = ST->element_begin(),
254            E = ST->element_end(); I != E; ++I) {
255       int Slot = Table.getSlot(*I);
256       assert(Slot != -1 && "Type used but not available!!");
257       output_typeid((unsigned)Slot);
258     }
259
260     // Terminate list with VoidTy
261     output_typeid((unsigned)Type::VoidTyID);
262     break;
263   }
264
265   case Type::PointerTyID: {
266     const PointerType *PT = cast<PointerType>(T);
267     int Slot = Table.getSlot(PT->getElementType());
268     assert(Slot != -1 && "Type used but not available!!");
269     output_typeid((unsigned)Slot);
270     break;
271   }
272
273   case Type::OpaqueTyID:
274     // No need to emit anything, just the count of opaque types is enough.
275     break;
276
277   default:
278     std::cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
279               << " Type '" << T->getDescription() << "'\n";
280     break;
281   }
282 }
283
284 void BytecodeWriter::outputConstant(const Constant *CPV) {
285   assert((CPV->getType()->isPrimitiveType() || !CPV->isNullValue()) &&
286          "Shouldn't output null constants!");
287
288   // We must check for a ConstantExpr before switching by type because
289   // a ConstantExpr can be of any type, and has no explicit value.
290   //
291   if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) {
292     // FIXME: Encoding of constant exprs could be much more compact!
293     assert(CE->getNumOperands() > 0 && "ConstantExpr with 0 operands");
294     assert(CE->getNumOperands() != 1 || CE->getOpcode() == Instruction::Cast);
295     output_vbr(1+CE->getNumOperands());   // flags as an expr
296     output_vbr(CE->getOpcode());        // flags as an expr
297
298     for (User::const_op_iterator OI = CE->op_begin(); OI != CE->op_end(); ++OI){
299       int Slot = Table.getSlot(*OI);
300       assert(Slot != -1 && "Unknown constant used in ConstantExpr!!");
301       output_vbr((unsigned)Slot);
302       Slot = Table.getSlot((*OI)->getType());
303       output_typeid((unsigned)Slot);
304     }
305     return;
306   } else if (isa<UndefValue>(CPV)) {
307     output_vbr(1U);       // 1 -> UndefValue constant.
308     return;
309   } else {
310     output_vbr(0U);       // flag as not a ConstantExpr
311   }
312
313   switch (CPV->getType()->getTypeID()) {
314   case Type::BoolTyID:    // Boolean Types
315     if (cast<ConstantBool>(CPV)->getValue())
316       output_vbr(1U);
317     else
318       output_vbr(0U);
319     break;
320
321   case Type::UByteTyID:   // Unsigned integer types...
322   case Type::UShortTyID:
323   case Type::UIntTyID:
324   case Type::ULongTyID:
325     output_vbr(cast<ConstantUInt>(CPV)->getValue());
326     break;
327
328   case Type::SByteTyID:   // Signed integer types...
329   case Type::ShortTyID:
330   case Type::IntTyID:
331   case Type::LongTyID:
332     output_vbr(cast<ConstantSInt>(CPV)->getValue());
333     break;
334
335   case Type::ArrayTyID: {
336     const ConstantArray *CPA = cast<ConstantArray>(CPV);
337     assert(!CPA->isString() && "Constant strings should be handled specially!");
338
339     for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i) {
340       int Slot = Table.getSlot(CPA->getOperand(i));
341       assert(Slot != -1 && "Constant used but not available!!");
342       output_vbr((unsigned)Slot);
343     }
344     break;
345   }
346
347   case Type::PackedTyID: {
348     const ConstantPacked *CP = cast<ConstantPacked>(CPV);
349
350     for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) {
351       int Slot = Table.getSlot(CP->getOperand(i));
352       assert(Slot != -1 && "Constant used but not available!!");
353       output_vbr((unsigned)Slot);
354     }
355     break;
356   }
357
358   case Type::StructTyID: {
359     const ConstantStruct *CPS = cast<ConstantStruct>(CPV);
360
361     for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i) {
362       int Slot = Table.getSlot(CPS->getOperand(i));
363       assert(Slot != -1 && "Constant used but not available!!");
364       output_vbr((unsigned)Slot);
365     }
366     break;
367   }
368
369   case Type::PointerTyID:
370     assert(0 && "No non-null, non-constant-expr constants allowed!");
371     abort();
372
373   case Type::FloatTyID: {   // Floating point types...
374     float Tmp = (float)cast<ConstantFP>(CPV)->getValue();
375     output_float(Tmp);
376     break;
377   }
378   case Type::DoubleTyID: {
379     double Tmp = cast<ConstantFP>(CPV)->getValue();
380     output_double(Tmp);
381     break;
382   }
383
384   case Type::VoidTyID:
385   case Type::LabelTyID:
386   default:
387     std::cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
388               << " type '" << *CPV->getType() << "'\n";
389     break;
390   }
391   return;
392 }
393
394 /// outputInlineAsm - InlineAsm's get emitted to the constant pool, so they can
395 /// be shared by multiple uses.
396 void BytecodeWriter::outputInlineAsm(const InlineAsm *IA) {
397   // Output a marker, so we know when we have one one parsing the constant pool.
398   // Note that this encoding is 5 bytes: not very efficient for a marker.  Since
399   // unique inline asms are rare, this should hardly matter.
400   output_vbr(~0U);
401   
402   output(IA->getAsmString());
403   output(IA->getConstraintString());
404   output_vbr(unsigned(IA->hasSideEffects()));
405 }
406
407 void BytecodeWriter::outputConstantStrings() {
408   SlotCalculator::string_iterator I = Table.string_begin();
409   SlotCalculator::string_iterator E = Table.string_end();
410   if (I == E) return;  // No strings to emit
411
412   // If we have != 0 strings to emit, output them now.  Strings are emitted into
413   // the 'void' type plane.
414   output_vbr(unsigned(E-I));
415   output_typeid(Type::VoidTyID);
416
417   // Emit all of the strings.
418   for (I = Table.string_begin(); I != E; ++I) {
419     const ConstantArray *Str = *I;
420     int Slot = Table.getSlot(Str->getType());
421     assert(Slot != -1 && "Constant string of unknown type?");
422     output_typeid((unsigned)Slot);
423
424     // Now that we emitted the type (which indicates the size of the string),
425     // emit all of the characters.
426     std::string Val = Str->getAsString();
427     output_data(Val.c_str(), Val.c_str()+Val.size());
428   }
429 }
430
431 //===----------------------------------------------------------------------===//
432 //===                           Instruction Output                         ===//
433 //===----------------------------------------------------------------------===//
434
435 // outputInstructionFormat0 - Output those weird instructions that have a large
436 // number of operands or have large operands themselves.
437 //
438 // Format: [opcode] [type] [numargs] [arg0] [arg1] ... [arg<numargs-1>]
439 //
440 void BytecodeWriter::outputInstructionFormat0(const Instruction *I,
441                                               unsigned Opcode,
442                                               const SlotCalculator &Table,
443                                               unsigned Type) {
444   // Opcode must have top two bits clear...
445   output_vbr(Opcode << 2);                  // Instruction Opcode ID
446   output_typeid(Type);                      // Result type
447
448   unsigned NumArgs = I->getNumOperands();
449   output_vbr(NumArgs + (isa<CastInst>(I)  ||
450                         isa<VAArgInst>(I) || Opcode == 56 || Opcode == 58));
451
452   if (!isa<GetElementPtrInst>(&I)) {
453     for (unsigned i = 0; i < NumArgs; ++i) {
454       int Slot = Table.getSlot(I->getOperand(i));
455       assert(Slot >= 0 && "No slot number for value!?!?");
456       output_vbr((unsigned)Slot);
457     }
458
459     if (isa<CastInst>(I) || isa<VAArgInst>(I)) {
460       int Slot = Table.getSlot(I->getType());
461       assert(Slot != -1 && "Cast return type unknown?");
462       output_typeid((unsigned)Slot);
463     } else if (Opcode == 56) {  // Invoke escape sequence
464       output_vbr(cast<InvokeInst>(I)->getCallingConv());
465     } else if (Opcode == 58) {  // Call escape sequence
466       output_vbr((cast<CallInst>(I)->getCallingConv() << 1) |
467                  unsigned(cast<CallInst>(I)->isTailCall()));
468     }
469   } else {
470     int Slot = Table.getSlot(I->getOperand(0));
471     assert(Slot >= 0 && "No slot number for value!?!?");
472     output_vbr(unsigned(Slot));
473
474     // We need to encode the type of sequential type indices into their slot #
475     unsigned Idx = 1;
476     for (gep_type_iterator TI = gep_type_begin(I), E = gep_type_end(I);
477          Idx != NumArgs; ++TI, ++Idx) {
478       Slot = Table.getSlot(I->getOperand(Idx));
479       assert(Slot >= 0 && "No slot number for value!?!?");
480
481       if (isa<SequentialType>(*TI)) {
482         unsigned IdxId;
483         switch (I->getOperand(Idx)->getType()->getTypeID()) {
484         default: assert(0 && "Unknown index type!");
485         case Type::UIntTyID:  IdxId = 0; break;
486         case Type::IntTyID:   IdxId = 1; break;
487         case Type::ULongTyID: IdxId = 2; break;
488         case Type::LongTyID:  IdxId = 3; break;
489         }
490         Slot = (Slot << 2) | IdxId;
491       }
492       output_vbr(unsigned(Slot));
493     }
494   }
495 }
496
497
498 // outputInstrVarArgsCall - Output the absurdly annoying varargs function calls.
499 // This are more annoying than most because the signature of the call does not
500 // tell us anything about the types of the arguments in the varargs portion.
501 // Because of this, we encode (as type 0) all of the argument types explicitly
502 // before the argument value.  This really sucks, but you shouldn't be using
503 // varargs functions in your code! *death to printf*!
504 //
505 // Format: [opcode] [type] [numargs] [arg0] [arg1] ... [arg<numargs-1>]
506 //
507 void BytecodeWriter::outputInstrVarArgsCall(const Instruction *I,
508                                             unsigned Opcode,
509                                             const SlotCalculator &Table,
510                                             unsigned Type) {
511   assert(isa<CallInst>(I) || isa<InvokeInst>(I));
512   // Opcode must have top two bits clear...
513   output_vbr(Opcode << 2);                  // Instruction Opcode ID
514   output_typeid(Type);                      // Result type (varargs type)
515
516   const PointerType *PTy = cast<PointerType>(I->getOperand(0)->getType());
517   const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
518   unsigned NumParams = FTy->getNumParams();
519
520   unsigned NumFixedOperands;
521   if (isa<CallInst>(I)) {
522     // Output an operand for the callee and each fixed argument, then two for
523     // each variable argument.
524     NumFixedOperands = 1+NumParams;
525   } else {
526     assert(isa<InvokeInst>(I) && "Not call or invoke??");
527     // Output an operand for the callee and destinations, then two for each
528     // variable argument.
529     NumFixedOperands = 3+NumParams;
530   }
531   output_vbr(2 * I->getNumOperands()-NumFixedOperands +
532              unsigned(Opcode == 56 || Opcode == 58));
533
534   // The type for the function has already been emitted in the type field of the
535   // instruction.  Just emit the slot # now.
536   for (unsigned i = 0; i != NumFixedOperands; ++i) {
537     int Slot = Table.getSlot(I->getOperand(i));
538     assert(Slot >= 0 && "No slot number for value!?!?");
539     output_vbr((unsigned)Slot);
540   }
541
542   for (unsigned i = NumFixedOperands, e = I->getNumOperands(); i != e; ++i) {
543     // Output Arg Type ID
544     int Slot = Table.getSlot(I->getOperand(i)->getType());
545     assert(Slot >= 0 && "No slot number for value!?!?");
546     output_typeid((unsigned)Slot);
547
548     // Output arg ID itself
549     Slot = Table.getSlot(I->getOperand(i));
550     assert(Slot >= 0 && "No slot number for value!?!?");
551     output_vbr((unsigned)Slot);
552   }
553   
554   // If this is the escape sequence for call, emit the tailcall/cc info.
555   if (Opcode == 58) {
556     const CallInst *CI = cast<CallInst>(I);
557     output_vbr((CI->getCallingConv() << 1) | unsigned(CI->isTailCall()));
558   } else if (Opcode == 56) {    // Invoke escape sequence.
559     output_vbr(cast<InvokeInst>(I)->getCallingConv());
560   }
561 }
562
563
564 // outputInstructionFormat1 - Output one operand instructions, knowing that no
565 // operand index is >= 2^12.
566 //
567 inline void BytecodeWriter::outputInstructionFormat1(const Instruction *I,
568                                                      unsigned Opcode,
569                                                      unsigned *Slots,
570                                                      unsigned Type) {
571   // bits   Instruction format:
572   // --------------------------
573   // 01-00: Opcode type, fixed to 1.
574   // 07-02: Opcode
575   // 19-08: Resulting type plane
576   // 31-20: Operand #1 (if set to (2^12-1), then zero operands)
577   //
578   output(1 | (Opcode << 2) | (Type << 8) | (Slots[0] << 20));
579 }
580
581
582 // outputInstructionFormat2 - Output two operand instructions, knowing that no
583 // operand index is >= 2^8.
584 //
585 inline void BytecodeWriter::outputInstructionFormat2(const Instruction *I,
586                                                      unsigned Opcode,
587                                                      unsigned *Slots,
588                                                      unsigned Type) {
589   // bits   Instruction format:
590   // --------------------------
591   // 01-00: Opcode type, fixed to 2.
592   // 07-02: Opcode
593   // 15-08: Resulting type plane
594   // 23-16: Operand #1
595   // 31-24: Operand #2
596   //
597   output(2 | (Opcode << 2) | (Type << 8) | (Slots[0] << 16) | (Slots[1] << 24));
598 }
599
600
601 // outputInstructionFormat3 - Output three operand instructions, knowing that no
602 // operand index is >= 2^6.
603 //
604 inline void BytecodeWriter::outputInstructionFormat3(const Instruction *I,
605                                                      unsigned Opcode,
606                                                      unsigned *Slots,
607                                                      unsigned Type) {
608   // bits   Instruction format:
609   // --------------------------
610   // 01-00: Opcode type, fixed to 3.
611   // 07-02: Opcode
612   // 13-08: Resulting type plane
613   // 19-14: Operand #1
614   // 25-20: Operand #2
615   // 31-26: Operand #3
616   //
617   output(3 | (Opcode << 2) | (Type << 8) |
618           (Slots[0] << 14) | (Slots[1] << 20) | (Slots[2] << 26));
619 }
620
621 void BytecodeWriter::outputInstruction(const Instruction &I) {
622   assert(I.getOpcode() < 56 && "Opcode too big???");
623   unsigned Opcode = I.getOpcode();
624   unsigned NumOperands = I.getNumOperands();
625
626   // Encode 'tail call' as 61, 'volatile load' as 62, and 'volatile store' as
627   // 63.
628   if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
629     if (CI->getCallingConv() == CallingConv::C) {
630       if (CI->isTailCall())
631         Opcode = 61;   // CCC + Tail Call
632       else
633         ;     // Opcode = Instruction::Call
634     } else if (CI->getCallingConv() == CallingConv::Fast) {
635       if (CI->isTailCall())
636         Opcode = 59;    // FastCC + TailCall
637       else
638         Opcode = 60;    // FastCC + Not Tail Call
639     } else {
640       Opcode = 58;      // Call escape sequence.
641     }
642   } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
643     if (II->getCallingConv() == CallingConv::Fast)
644       Opcode = 57;      // FastCC invoke.
645     else if (II->getCallingConv() != CallingConv::C)
646       Opcode = 56;      // Invoke escape sequence.
647
648   } else if (isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) {
649     Opcode = 62;
650   } else if (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile()) {
651     Opcode = 63;
652   }
653
654   // Figure out which type to encode with the instruction.  Typically we want
655   // the type of the first parameter, as opposed to the type of the instruction
656   // (for example, with setcc, we always know it returns bool, but the type of
657   // the first param is actually interesting).  But if we have no arguments
658   // we take the type of the instruction itself.
659   //
660   const Type *Ty;
661   switch (I.getOpcode()) {
662   case Instruction::Select:
663   case Instruction::Malloc:
664   case Instruction::Alloca:
665     Ty = I.getType();  // These ALWAYS want to encode the return type
666     break;
667   case Instruction::Store:
668     Ty = I.getOperand(1)->getType();  // Encode the pointer type...
669     assert(isa<PointerType>(Ty) && "Store to nonpointer type!?!?");
670     break;
671   default:              // Otherwise use the default behavior...
672     Ty = NumOperands ? I.getOperand(0)->getType() : I.getType();
673     break;
674   }
675
676   unsigned Type;
677   int Slot = Table.getSlot(Ty);
678   assert(Slot != -1 && "Type not available!!?!");
679   Type = (unsigned)Slot;
680
681   // Varargs calls and invokes are encoded entirely different from any other
682   // instructions.
683   if (const CallInst *CI = dyn_cast<CallInst>(&I)){
684     const PointerType *Ty =cast<PointerType>(CI->getCalledValue()->getType());
685     if (cast<FunctionType>(Ty->getElementType())->isVarArg()) {
686       outputInstrVarArgsCall(CI, Opcode, Table, Type);
687       return;
688     }
689   } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
690     const PointerType *Ty =cast<PointerType>(II->getCalledValue()->getType());
691     if (cast<FunctionType>(Ty->getElementType())->isVarArg()) {
692       outputInstrVarArgsCall(II, Opcode, Table, Type);
693       return;
694     }
695   }
696
697   if (NumOperands <= 3) {
698     // Make sure that we take the type number into consideration.  We don't want
699     // to overflow the field size for the instruction format we select.
700     //
701     unsigned MaxOpSlot = Type;
702     unsigned Slots[3]; Slots[0] = (1 << 12)-1;   // Marker to signify 0 operands
703
704     for (unsigned i = 0; i != NumOperands; ++i) {
705       int slot = Table.getSlot(I.getOperand(i));
706       assert(slot != -1 && "Broken bytecode!");
707       if (unsigned(slot) > MaxOpSlot) MaxOpSlot = unsigned(slot);
708       Slots[i] = unsigned(slot);
709     }
710
711     // Handle the special cases for various instructions...
712     if (isa<CastInst>(I) || isa<VAArgInst>(I)) {
713       // Cast has to encode the destination type as the second argument in the
714       // packet, or else we won't know what type to cast to!
715       Slots[1] = Table.getSlot(I.getType());
716       assert(Slots[1] != ~0U && "Cast return type unknown?");
717       if (Slots[1] > MaxOpSlot) MaxOpSlot = Slots[1];
718       NumOperands++;
719     } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(&I)) {
720       assert(NumOperands == 1 && "Bogus allocation!");
721       if (AI->getAlignment()) {
722         Slots[1] = Log2_32(AI->getAlignment())+1;
723         if (Slots[1] > MaxOpSlot) MaxOpSlot = Slots[1];
724         NumOperands = 2;
725       }
726     } else if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(&I)) {
727       // We need to encode the type of sequential type indices into their slot #
728       unsigned Idx = 1;
729       for (gep_type_iterator I = gep_type_begin(GEP), E = gep_type_end(GEP);
730            I != E; ++I, ++Idx)
731         if (isa<SequentialType>(*I)) {
732           unsigned IdxId;
733           switch (GEP->getOperand(Idx)->getType()->getTypeID()) {
734           default: assert(0 && "Unknown index type!");
735           case Type::UIntTyID:  IdxId = 0; break;
736           case Type::IntTyID:   IdxId = 1; break;
737           case Type::ULongTyID: IdxId = 2; break;
738           case Type::LongTyID:  IdxId = 3; break;
739           }
740           Slots[Idx] = (Slots[Idx] << 2) | IdxId;
741           if (Slots[Idx] > MaxOpSlot) MaxOpSlot = Slots[Idx];
742         }
743     } else if (Opcode == 58) {
744       // If this is the escape sequence for call, emit the tailcall/cc info.
745       const CallInst &CI = cast<CallInst>(I);
746       ++NumOperands;
747       if (NumOperands <= 3) {
748         Slots[NumOperands-1] =
749           (CI.getCallingConv() << 1)|unsigned(CI.isTailCall());
750         if (Slots[NumOperands-1] > MaxOpSlot)
751           MaxOpSlot = Slots[NumOperands-1];
752       }
753     } else if (Opcode == 56) {
754       // Invoke escape seq has at least 4 operands to encode.
755       ++NumOperands;
756     }
757
758     // Decide which instruction encoding to use.  This is determined primarily
759     // by the number of operands, and secondarily by whether or not the max
760     // operand will fit into the instruction encoding.  More operands == fewer
761     // bits per operand.
762     //
763     switch (NumOperands) {
764     case 0:
765     case 1:
766       if (MaxOpSlot < (1 << 12)-1) { // -1 because we use 4095 to indicate 0 ops
767         outputInstructionFormat1(&I, Opcode, Slots, Type);
768         return;
769       }
770       break;
771
772     case 2:
773       if (MaxOpSlot < (1 << 8)) {
774         outputInstructionFormat2(&I, Opcode, Slots, Type);
775         return;
776       }
777       break;
778
779     case 3:
780       if (MaxOpSlot < (1 << 6)) {
781         outputInstructionFormat3(&I, Opcode, Slots, Type);
782         return;
783       }
784       break;
785     default:
786       break;
787     }
788   }
789
790   // If we weren't handled before here, we either have a large number of
791   // operands or a large operand index that we are referring to.
792   outputInstructionFormat0(&I, Opcode, Table, Type);
793 }
794
795 //===----------------------------------------------------------------------===//
796 //===                              Block Output                            ===//
797 //===----------------------------------------------------------------------===//
798
799 BytecodeWriter::BytecodeWriter(std::vector<unsigned char> &o, const Module *M)
800   : Out(o), Table(M) {
801
802   // Emit the signature...
803   static const unsigned char *Sig = (const unsigned char*)"llvm";
804   output_data(Sig, Sig+4);
805
806   // Emit the top level CLASS block.
807   BytecodeBlock ModuleBlock(BytecodeFormat::ModuleBlockID, *this, false, true);
808
809   bool isBigEndian      = M->getEndianness() == Module::BigEndian;
810   bool hasLongPointers  = M->getPointerSize() == Module::Pointer64;
811   bool hasNoEndianness  = M->getEndianness() == Module::AnyEndianness;
812   bool hasNoPointerSize = M->getPointerSize() == Module::AnyPointerSize;
813
814   // Output the version identifier and other information.
815   unsigned Version = (BCVersionNum << 4) |
816                      (unsigned)isBigEndian | (hasLongPointers << 1) |
817                      (hasNoEndianness << 2) |
818                      (hasNoPointerSize << 3);
819   output_vbr(Version);
820
821   // The Global type plane comes first
822   {
823     BytecodeBlock CPool(BytecodeFormat::GlobalTypePlaneBlockID, *this);
824     outputTypes(Type::FirstDerivedTyID);
825   }
826
827   // The ModuleInfoBlock follows directly after the type information
828   outputModuleInfoBlock(M);
829
830   // Output module level constants, used for global variable initializers
831   outputConstants(false);
832
833   // Do the whole module now! Process each function at a time...
834   for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
835     outputFunction(I);
836
837   // If needed, output the symbol table for the module...
838   outputSymbolTable(M->getSymbolTable());
839 }
840
841 void BytecodeWriter::outputTypes(unsigned TypeNum) {
842   // Write the type plane for types first because earlier planes (e.g. for a
843   // primitive type like float) may have constants constructed using types
844   // coming later (e.g., via getelementptr from a pointer type).  The type
845   // plane is needed before types can be fwd or bkwd referenced.
846   const std::vector<const Type*>& Types = Table.getTypes();
847   assert(!Types.empty() && "No types at all?");
848   assert(TypeNum <= Types.size() && "Invalid TypeNo index");
849
850   unsigned NumEntries = Types.size() - TypeNum;
851
852   // Output type header: [num entries]
853   output_vbr(NumEntries);
854
855   for (unsigned i = TypeNum; i < TypeNum+NumEntries; ++i)
856     outputType(Types[i]);
857 }
858
859 // Helper function for outputConstants().
860 // Writes out all the constants in the plane Plane starting at entry StartNo.
861 //
862 void BytecodeWriter::outputConstantsInPlane(const std::vector<const Value*>
863                                             &Plane, unsigned StartNo) {
864   unsigned ValNo = StartNo;
865
866   // Scan through and ignore function arguments, global values, and constant
867   // strings.
868   for (; ValNo < Plane.size() &&
869          (isa<Argument>(Plane[ValNo]) || isa<GlobalValue>(Plane[ValNo]) ||
870           (isa<ConstantArray>(Plane[ValNo]) &&
871            cast<ConstantArray>(Plane[ValNo])->isString())); ValNo++)
872     /*empty*/;
873
874   unsigned NC = ValNo;              // Number of constants
875   for (; NC < Plane.size() && (isa<Constant>(Plane[NC]) || 
876                                isa<InlineAsm>(Plane[NC])); NC++)
877     /*empty*/;
878   NC -= ValNo;                      // Convert from index into count
879   if (NC == 0) return;              // Skip empty type planes...
880
881   // FIXME: Most slabs only have 1 or 2 entries!  We should encode this much
882   // more compactly.
883
884   // Output type header: [num entries][type id number]
885   //
886   output_vbr(NC);
887
888   // Output the Type ID Number...
889   int Slot = Table.getSlot(Plane.front()->getType());
890   assert (Slot != -1 && "Type in constant pool but not in function!!");
891   output_typeid((unsigned)Slot);
892
893   for (unsigned i = ValNo; i < ValNo+NC; ++i) {
894     const Value *V = Plane[i];
895     if (const Constant *C = dyn_cast<Constant>(V))
896       outputConstant(C);
897     else
898       outputInlineAsm(cast<InlineAsm>(V));
899   }
900 }
901
902 static inline bool hasNullValue(const Type *Ty) {
903   return Ty != Type::LabelTy && Ty != Type::VoidTy && !isa<OpaqueType>(Ty);
904 }
905
906 void BytecodeWriter::outputConstants(bool isFunction) {
907   BytecodeBlock CPool(BytecodeFormat::ConstantPoolBlockID, *this,
908                       true  /* Elide block if empty */);
909
910   unsigned NumPlanes = Table.getNumPlanes();
911
912   if (isFunction)
913     // Output the type plane before any constants!
914     outputTypes(Table.getModuleTypeLevel());
915   else
916     // Output module-level string constants before any other constants.
917     outputConstantStrings();
918
919   for (unsigned pno = 0; pno != NumPlanes; pno++) {
920     const std::vector<const Value*> &Plane = Table.getPlane(pno);
921     if (!Plane.empty()) {              // Skip empty type planes...
922       unsigned ValNo = 0;
923       if (isFunction)                  // Don't re-emit module constants
924         ValNo += Table.getModuleLevel(pno);
925
926       if (hasNullValue(Plane[0]->getType())) {
927         // Skip zero initializer
928         if (ValNo == 0)
929           ValNo = 1;
930       }
931
932       // Write out constants in the plane
933       outputConstantsInPlane(Plane, ValNo);
934     }
935   }
936 }
937
938 static unsigned getEncodedLinkage(const GlobalValue *GV) {
939   switch (GV->getLinkage()) {
940   default: assert(0 && "Invalid linkage!");
941   case GlobalValue::ExternalLinkage:     return 0;
942   case GlobalValue::WeakLinkage:         return 1;
943   case GlobalValue::AppendingLinkage:    return 2;
944   case GlobalValue::InternalLinkage:     return 3;
945   case GlobalValue::LinkOnceLinkage:     return 4;
946   case GlobalValue::DLLImportLinkage:    return 5;
947   case GlobalValue::DLLExportLinkage:    return 6;
948   case GlobalValue::ExternalWeakLinkage: return 7;
949   }
950 }
951
952 void BytecodeWriter::outputModuleInfoBlock(const Module *M) {
953   BytecodeBlock ModuleInfoBlock(BytecodeFormat::ModuleGlobalInfoBlockID, *this);
954
955   // Give numbers to sections as we encounter them.
956   unsigned SectionIDCounter = 0;
957   std::vector<std::string> SectionNames;
958   std::map<std::string, unsigned> SectionID;
959   
960   // Output the types for the global variables in the module...
961   for (Module::const_global_iterator I = M->global_begin(),
962          End = M->global_end(); I != End; ++I) {
963     int Slot = Table.getSlot(I->getType());
964     assert(Slot != -1 && "Module global vars is broken!");
965
966     assert((I->hasInitializer() || !I->hasInternalLinkage()) &&
967            "Global must have an initializer or have external linkage!");
968     
969     // Fields: bit0 = isConstant, bit1 = hasInitializer, bit2-4=Linkage,
970     // bit5+ = Slot # for type.
971     bool HasExtensionWord = (I->getAlignment() != 0) || I->hasSection();
972     
973     // If we need to use the extension byte, set linkage=3(internal) and
974     // initializer = 0 (impossible!).
975     if (!HasExtensionWord) {
976       unsigned oSlot = ((unsigned)Slot << 5) | (getEncodedLinkage(I) << 2) |
977                         (I->hasInitializer() << 1) | (unsigned)I->isConstant();
978       output_vbr(oSlot);
979     } else {  
980       unsigned oSlot = ((unsigned)Slot << 5) | (3 << 2) |
981                         (0 << 1) | (unsigned)I->isConstant();
982       output_vbr(oSlot);
983       
984       // The extension word has this format: bit 0 = has initializer, bit 1-3 =
985       // linkage, bit 4-8 = alignment (log2), bit 9 = has SectionID, 
986       // bits 10+ = future use.
987       unsigned ExtWord = (unsigned)I->hasInitializer() |
988                          (getEncodedLinkage(I) << 1) |
989                          ((Log2_32(I->getAlignment())+1) << 4) |
990                          ((unsigned)I->hasSection() << 9);
991       output_vbr(ExtWord);
992       if (I->hasSection()) {
993         // Give section names unique ID's.
994         unsigned &Entry = SectionID[I->getSection()];
995         if (Entry == 0) {
996           Entry = ++SectionIDCounter;
997           SectionNames.push_back(I->getSection());
998         }
999         output_vbr(Entry);
1000       }
1001     }
1002
1003     // If we have an initializer, output it now.
1004     if (I->hasInitializer()) {
1005       Slot = Table.getSlot((Value*)I->getInitializer());
1006       assert(Slot != -1 && "No slot for global var initializer!");
1007       output_vbr((unsigned)Slot);
1008     }
1009   }
1010   output_typeid((unsigned)Table.getSlot(Type::VoidTy));
1011
1012   // Output the types of the functions in this module.
1013   for (Module::const_iterator I = M->begin(), End = M->end(); I != End; ++I) {
1014     int Slot = Table.getSlot(I->getType());
1015     assert(Slot != -1 && "Module slot calculator is broken!");
1016     assert(Slot >= Type::FirstDerivedTyID && "Derived type not in range!");
1017     assert(((Slot << 6) >> 6) == Slot && "Slot # too big!");
1018     unsigned CC = I->getCallingConv()+1;
1019     unsigned ID = (Slot << 5) | (CC & 15);
1020
1021     if (I->isExternal())   // If external, we don't have an FunctionInfo block.
1022       ID |= 1 << 4;
1023     
1024     if (I->getAlignment() || I->hasSection() || (CC & ~15) != 0 ||
1025         (I->isExternal() && I->hasDLLImportLinkage()) ||
1026         (I->isExternal() && I->hasExternalWeakLinkage())
1027        )
1028       ID |= 1 << 31;       // Do we need an extension word?
1029     
1030     output_vbr(ID);
1031     
1032     if (ID & (1 << 31)) {
1033       // Extension byte: bits 0-4 = alignment, bits 5-9 = top nibble of calling
1034       // convention, bit 10 = hasSectionID., bits 11-12 = external linkage type
1035       unsigned extLinkage = 0;
1036
1037       if (I->isExternal()) {
1038         if (I->hasDLLImportLinkage()) {
1039           extLinkage = 1;
1040         } else if (I->hasExternalWeakLinkage()) {
1041           extLinkage = 2;
1042         }
1043       }
1044
1045       ID = (Log2_32(I->getAlignment())+1) | ((CC >> 4) << 5) | 
1046         (I->hasSection() << 10) |
1047         ((extLinkage & 3) << 11);
1048       output_vbr(ID);
1049       
1050       // Give section names unique ID's.
1051       if (I->hasSection()) {
1052         unsigned &Entry = SectionID[I->getSection()];
1053         if (Entry == 0) {
1054           Entry = ++SectionIDCounter;
1055           SectionNames.push_back(I->getSection());
1056         }
1057         output_vbr(Entry);
1058       }
1059     }
1060   }
1061   output_vbr((unsigned)Table.getSlot(Type::VoidTy) << 5);
1062
1063   // Emit the list of dependent libraries for the Module.
1064   Module::lib_iterator LI = M->lib_begin();
1065   Module::lib_iterator LE = M->lib_end();
1066   output_vbr(unsigned(LE - LI));   // Emit the number of dependent libraries.
1067   for (; LI != LE; ++LI)
1068     output(*LI);
1069
1070   // Output the target triple from the module
1071   output(M->getTargetTriple());
1072   
1073   // Emit the table of section names.
1074   output_vbr((unsigned)SectionNames.size());
1075   for (unsigned i = 0, e = SectionNames.size(); i != e; ++i)
1076     output(SectionNames[i]);
1077   
1078   // Output the inline asm string.
1079   output(M->getModuleInlineAsm());
1080 }
1081
1082 void BytecodeWriter::outputInstructions(const Function *F) {
1083   BytecodeBlock ILBlock(BytecodeFormat::InstructionListBlockID, *this);
1084   for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
1085     for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I)
1086       outputInstruction(*I);
1087 }
1088
1089 void BytecodeWriter::outputFunction(const Function *F) {
1090   // If this is an external function, there is nothing else to emit!
1091   if (F->isExternal()) return;
1092
1093   BytecodeBlock FunctionBlock(BytecodeFormat::FunctionBlockID, *this);
1094   output_vbr(getEncodedLinkage(F));
1095
1096   // Get slot information about the function...
1097   Table.incorporateFunction(F);
1098
1099   if (Table.getCompactionTable().empty()) {
1100     // Output information about the constants in the function if the compaction
1101     // table is not being used.
1102     outputConstants(true);
1103   } else {
1104     // Otherwise, emit the compaction table.
1105     outputCompactionTable();
1106   }
1107
1108   // Output all of the instructions in the body of the function
1109   outputInstructions(F);
1110
1111   // If needed, output the symbol table for the function...
1112   outputSymbolTable(F->getSymbolTable());
1113
1114   Table.purgeFunction();
1115 }
1116
1117 void BytecodeWriter::outputCompactionTablePlane(unsigned PlaneNo,
1118                                          const std::vector<const Value*> &Plane,
1119                                                 unsigned StartNo) {
1120   unsigned End = Table.getModuleLevel(PlaneNo);
1121   if (Plane.empty() || StartNo == End || End == 0) return;   // Nothing to emit
1122   assert(StartNo < End && "Cannot emit negative range!");
1123   assert(StartNo < Plane.size() && End <= Plane.size());
1124
1125   // Do not emit the null initializer!
1126   ++StartNo;
1127
1128   // Figure out which encoding to use.  By far the most common case we have is
1129   // to emit 0-2 entries in a compaction table plane.
1130   switch (End-StartNo) {
1131   case 0:         // Avoid emitting two vbr's if possible.
1132   case 1:
1133   case 2:
1134     output_vbr((PlaneNo << 2) | End-StartNo);
1135     break;
1136   default:
1137     // Output the number of things.
1138     output_vbr((unsigned(End-StartNo) << 2) | 3);
1139     output_typeid(PlaneNo);                 // Emit the type plane this is
1140     break;
1141   }
1142
1143   for (unsigned i = StartNo; i != End; ++i)
1144     output_vbr(Table.getGlobalSlot(Plane[i]));
1145 }
1146
1147 void BytecodeWriter::outputCompactionTypes(unsigned StartNo) {
1148   // Get the compaction type table from the slot calculator
1149   const std::vector<const Type*> &CTypes = Table.getCompactionTypes();
1150
1151   // The compaction types may have been uncompactified back to the
1152   // global types. If so, we just write an empty table
1153   if (CTypes.size() == 0) {
1154     output_vbr(0U);
1155     return;
1156   }
1157
1158   assert(CTypes.size() >= StartNo && "Invalid compaction types start index");
1159
1160   // Determine how many types to write
1161   unsigned NumTypes = CTypes.size() - StartNo;
1162
1163   // Output the number of types.
1164   output_vbr(NumTypes);
1165
1166   for (unsigned i = StartNo; i < StartNo+NumTypes; ++i)
1167     output_typeid(Table.getGlobalSlot(CTypes[i]));
1168 }
1169
1170 void BytecodeWriter::outputCompactionTable() {
1171   // Avoid writing the compaction table at all if there is no content.
1172   if (Table.getCompactionTypes().size() >= Type::FirstDerivedTyID ||
1173       (!Table.CompactionTableIsEmpty())) {
1174     BytecodeBlock CTB(BytecodeFormat::CompactionTableBlockID, *this,
1175                       true/*ElideIfEmpty*/);
1176     const std::vector<std::vector<const Value*> > &CT =
1177       Table.getCompactionTable();
1178
1179     // First things first, emit the type compaction table if there is one.
1180     outputCompactionTypes(Type::FirstDerivedTyID);
1181
1182     for (unsigned i = 0, e = CT.size(); i != e; ++i)
1183       outputCompactionTablePlane(i, CT[i], 0);
1184   }
1185 }
1186
1187 void BytecodeWriter::outputSymbolTable(const SymbolTable &MST) {
1188   // Do not output the Bytecode block for an empty symbol table, it just wastes
1189   // space!
1190   if (MST.isEmpty()) return;
1191
1192   BytecodeBlock SymTabBlock(BytecodeFormat::SymbolTableBlockID, *this,
1193                             true/*ElideIfEmpty*/);
1194
1195   // Write the number of types
1196   output_vbr(MST.num_types());
1197
1198   // Write each of the types
1199   for (SymbolTable::type_const_iterator TI = MST.type_begin(),
1200        TE = MST.type_end(); TI != TE; ++TI) {
1201     // Symtab entry:[def slot #][name]
1202     output_typeid((unsigned)Table.getSlot(TI->second));
1203     output(TI->first);
1204   }
1205
1206   // Now do each of the type planes in order.
1207   for (SymbolTable::plane_const_iterator PI = MST.plane_begin(),
1208        PE = MST.plane_end(); PI != PE;  ++PI) {
1209     SymbolTable::value_const_iterator I = MST.value_begin(PI->first);
1210     SymbolTable::value_const_iterator End = MST.value_end(PI->first);
1211     int Slot;
1212
1213     if (I == End) continue;  // Don't mess with an absent type...
1214
1215     // Write the number of values in this plane
1216     output_vbr((unsigned)PI->second.size());
1217
1218     // Write the slot number of the type for this plane
1219     Slot = Table.getSlot(PI->first);
1220     assert(Slot != -1 && "Type in symtab, but not in table!");
1221     output_typeid((unsigned)Slot);
1222
1223     // Write each of the values in this plane
1224     for (; I != End; ++I) {
1225       // Symtab entry: [def slot #][name]
1226       Slot = Table.getSlot(I->second);
1227       assert(Slot != -1 && "Value in symtab but has no slot number!!");
1228       output_vbr((unsigned)Slot);
1229       output(I->first);
1230     }
1231   }
1232 }
1233
1234 void llvm::WriteBytecodeToFile(const Module *M, std::ostream &Out,
1235                                bool compress) {
1236   assert(M && "You can't write a null module!!");
1237
1238   // Make sure that std::cout is put into binary mode for systems
1239   // that care.
1240   if (&Out == std::cout)
1241     sys::Program::ChangeStdoutToBinary();
1242
1243   // Create a vector of unsigned char for the bytecode output. We
1244   // reserve 256KBytes of space in the vector so that we avoid doing
1245   // lots of little allocations. 256KBytes is sufficient for a large
1246   // proportion of the bytecode files we will encounter. Larger files
1247   // will be automatically doubled in size as needed (std::vector
1248   // behavior).
1249   std::vector<unsigned char> Buffer;
1250   Buffer.reserve(256 * 1024);
1251
1252   // The BytecodeWriter populates Buffer for us.
1253   BytecodeWriter BCW(Buffer, M);
1254
1255   // Keep track of how much we've written
1256   BytesWritten += Buffer.size();
1257
1258   // Determine start and end points of the Buffer
1259   const unsigned char *FirstByte = &Buffer.front();
1260
1261   // If we're supposed to compress this mess ...
1262   if (compress) {
1263
1264     // We signal compression by using an alternate magic number for the
1265     // file. The compressed bytecode file's magic number is "llvc" instead
1266     // of "llvm".
1267     char compressed_magic[4];
1268     compressed_magic[0] = 'l';
1269     compressed_magic[1] = 'l';
1270     compressed_magic[2] = 'v';
1271     compressed_magic[3] = 'c';
1272
1273     Out.write(compressed_magic,4);
1274
1275     // Compress everything after the magic number (which we altered)
1276     uint64_t zipSize = Compressor::compressToStream(
1277       (char*)(FirstByte+4),        // Skip the magic number
1278       Buffer.size()-4,             // Skip the magic number
1279       Out                          // Where to write compressed data
1280     );
1281
1282   } else {
1283
1284     // We're not compressing, so just write the entire block.
1285     Out.write((char*)FirstByte, Buffer.size());
1286   }
1287
1288   // make sure it hits disk now
1289   Out.flush();
1290 }