Small cleanups. No functionality change intended!
[oota-llvm.git] / lib / Bitcode / Writer / BitcodeWriter.cpp
1 //===--- Bitcode/Writer/BitcodeWriter.cpp - Bitcode Writer ----------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Bitcode writer implementation.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Bitcode/ReaderWriter.h"
15 #include "llvm/Bitcode/BitstreamWriter.h"
16 #include "llvm/Bitcode/LLVMBitCodes.h"
17 #include "ValueEnumerator.h"
18 #include "llvm/Constants.h"
19 #include "llvm/DerivedTypes.h"
20 #include "llvm/InlineAsm.h"
21 #include "llvm/Instructions.h"
22 #include "llvm/Module.h"
23 #include "llvm/TypeSymbolTable.h"
24 #include "llvm/ValueSymbolTable.h"
25 #include "llvm/Support/MathExtras.h"
26 #include "llvm/Support/Streams.h"
27 #include "llvm/Support/raw_ostream.h"
28 #include "llvm/System/Program.h"
29 using namespace llvm;
30
31 /// These are manifest constants used by the bitcode writer. They do not need to
32 /// be kept in sync with the reader, but need to be consistent within this file.
33 enum {
34   CurVersion = 0,
35   
36   // VALUE_SYMTAB_BLOCK abbrev id's.
37   VST_ENTRY_8_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
38   VST_ENTRY_7_ABBREV,
39   VST_ENTRY_6_ABBREV,
40   VST_BBENTRY_6_ABBREV,
41   
42   // CONSTANTS_BLOCK abbrev id's.
43   CONSTANTS_SETTYPE_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
44   CONSTANTS_INTEGER_ABBREV,
45   CONSTANTS_CE_CAST_Abbrev,
46   CONSTANTS_NULL_Abbrev,
47   
48   // FUNCTION_BLOCK abbrev id's.
49   FUNCTION_INST_LOAD_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
50   FUNCTION_INST_BINOP_ABBREV,
51   FUNCTION_INST_CAST_ABBREV,
52   FUNCTION_INST_RET_VOID_ABBREV,
53   FUNCTION_INST_RET_VAL_ABBREV,
54   FUNCTION_INST_UNREACHABLE_ABBREV
55 };
56
57
58 static unsigned GetEncodedCastOpcode(unsigned Opcode) {
59   switch (Opcode) {
60   default: assert(0 && "Unknown cast instruction!");
61   case Instruction::Trunc   : return bitc::CAST_TRUNC;
62   case Instruction::ZExt    : return bitc::CAST_ZEXT;
63   case Instruction::SExt    : return bitc::CAST_SEXT;
64   case Instruction::FPToUI  : return bitc::CAST_FPTOUI;
65   case Instruction::FPToSI  : return bitc::CAST_FPTOSI;
66   case Instruction::UIToFP  : return bitc::CAST_UITOFP;
67   case Instruction::SIToFP  : return bitc::CAST_SITOFP;
68   case Instruction::FPTrunc : return bitc::CAST_FPTRUNC;
69   case Instruction::FPExt   : return bitc::CAST_FPEXT;
70   case Instruction::PtrToInt: return bitc::CAST_PTRTOINT;
71   case Instruction::IntToPtr: return bitc::CAST_INTTOPTR;
72   case Instruction::BitCast : return bitc::CAST_BITCAST;
73   }
74 }
75
76 static unsigned GetEncodedBinaryOpcode(unsigned Opcode) {
77   switch (Opcode) {
78   default: assert(0 && "Unknown binary instruction!");
79   case Instruction::Add:  return bitc::BINOP_ADD;
80   case Instruction::Sub:  return bitc::BINOP_SUB;
81   case Instruction::Mul:  return bitc::BINOP_MUL;
82   case Instruction::UDiv: return bitc::BINOP_UDIV;
83   case Instruction::FDiv:
84   case Instruction::SDiv: return bitc::BINOP_SDIV;
85   case Instruction::URem: return bitc::BINOP_UREM;
86   case Instruction::FRem:
87   case Instruction::SRem: return bitc::BINOP_SREM;
88   case Instruction::Shl:  return bitc::BINOP_SHL;
89   case Instruction::LShr: return bitc::BINOP_LSHR;
90   case Instruction::AShr: return bitc::BINOP_ASHR;
91   case Instruction::And:  return bitc::BINOP_AND;
92   case Instruction::Or:   return bitc::BINOP_OR;
93   case Instruction::Xor:  return bitc::BINOP_XOR;
94   }
95 }
96
97
98
99 static void WriteStringRecord(unsigned Code, const std::string &Str, 
100                               unsigned AbbrevToUse, BitstreamWriter &Stream) {
101   SmallVector<unsigned, 64> Vals;
102   
103   // Code: [strchar x N]
104   for (unsigned i = 0, e = Str.size(); i != e; ++i)
105     Vals.push_back(Str[i]);
106     
107   // Emit the finished record.
108   Stream.EmitRecord(Code, Vals, AbbrevToUse);
109 }
110
111 // Emit information about parameter attributes.
112 static void WriteAttributeTable(const ValueEnumerator &VE, 
113                                 BitstreamWriter &Stream) {
114   const std::vector<AttrListPtr> &Attrs = VE.getAttributes();
115   if (Attrs.empty()) return;
116   
117   Stream.EnterSubblock(bitc::PARAMATTR_BLOCK_ID, 3);
118
119   SmallVector<uint64_t, 64> Record;
120   for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
121     const AttrListPtr &A = Attrs[i];
122     for (unsigned i = 0, e = A.getNumSlots(); i != e; ++i) {
123       const AttributeWithIndex &PAWI = A.getSlot(i);
124       Record.push_back(PAWI.Index);
125       Record.push_back(PAWI.Attrs);
126     }
127     
128     Stream.EmitRecord(bitc::PARAMATTR_CODE_ENTRY, Record);
129     Record.clear();
130   }
131   
132   Stream.ExitBlock();
133 }
134
135 /// WriteTypeTable - Write out the type table for a module.
136 static void WriteTypeTable(const ValueEnumerator &VE, BitstreamWriter &Stream) {
137   const ValueEnumerator::TypeList &TypeList = VE.getTypes();
138   
139   Stream.EnterSubblock(bitc::TYPE_BLOCK_ID, 4 /*count from # abbrevs */);
140   SmallVector<uint64_t, 64> TypeVals;
141   
142   // Abbrev for TYPE_CODE_POINTER.
143   BitCodeAbbrev *Abbv = new BitCodeAbbrev();
144   Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_POINTER));
145   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
146                             Log2_32_Ceil(VE.getTypes().size()+1)));
147   Abbv->Add(BitCodeAbbrevOp(0));  // Addrspace = 0
148   unsigned PtrAbbrev = Stream.EmitAbbrev(Abbv);
149   
150   // Abbrev for TYPE_CODE_FUNCTION.
151   Abbv = new BitCodeAbbrev();
152   Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_FUNCTION));
153   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));  // isvararg
154   Abbv->Add(BitCodeAbbrevOp(0));  // FIXME: DEAD value, remove in LLVM 3.0
155   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
156   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
157                             Log2_32_Ceil(VE.getTypes().size()+1)));
158   unsigned FunctionAbbrev = Stream.EmitAbbrev(Abbv);
159   
160   // Abbrev for TYPE_CODE_STRUCT.
161   Abbv = new BitCodeAbbrev();
162   Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT));
163   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));  // ispacked
164   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
165   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
166                             Log2_32_Ceil(VE.getTypes().size()+1)));
167   unsigned StructAbbrev = Stream.EmitAbbrev(Abbv);
168  
169   // Abbrev for TYPE_CODE_ARRAY.
170   Abbv = new BitCodeAbbrev();
171   Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_ARRAY));
172   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));   // size
173   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
174                             Log2_32_Ceil(VE.getTypes().size()+1)));
175   unsigned ArrayAbbrev = Stream.EmitAbbrev(Abbv);
176   
177   // Emit an entry count so the reader can reserve space.
178   TypeVals.push_back(TypeList.size());
179   Stream.EmitRecord(bitc::TYPE_CODE_NUMENTRY, TypeVals);
180   TypeVals.clear();
181   
182   // Loop over all of the types, emitting each in turn.
183   for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
184     const Type *T = TypeList[i].first;
185     int AbbrevToUse = 0;
186     unsigned Code = 0;
187     
188     switch (T->getTypeID()) {
189     default: assert(0 && "Unknown type!");
190     case Type::VoidTyID:   Code = bitc::TYPE_CODE_VOID;   break;
191     case Type::FloatTyID:  Code = bitc::TYPE_CODE_FLOAT;  break;
192     case Type::DoubleTyID: Code = bitc::TYPE_CODE_DOUBLE; break;
193     case Type::X86_FP80TyID: Code = bitc::TYPE_CODE_X86_FP80; break;
194     case Type::FP128TyID: Code = bitc::TYPE_CODE_FP128; break;
195     case Type::PPC_FP128TyID: Code = bitc::TYPE_CODE_PPC_FP128; break;
196     case Type::LabelTyID:  Code = bitc::TYPE_CODE_LABEL;  break;
197     case Type::OpaqueTyID: Code = bitc::TYPE_CODE_OPAQUE; break;
198     case Type::IntegerTyID:
199       // INTEGER: [width]
200       Code = bitc::TYPE_CODE_INTEGER;
201       TypeVals.push_back(cast<IntegerType>(T)->getBitWidth());
202       break;
203     case Type::PointerTyID: {
204       const PointerType *PTy = cast<PointerType>(T);
205       // POINTER: [pointee type, address space]
206       Code = bitc::TYPE_CODE_POINTER;
207       TypeVals.push_back(VE.getTypeID(PTy->getElementType()));
208       unsigned AddressSpace = PTy->getAddressSpace();
209       TypeVals.push_back(AddressSpace);
210       if (AddressSpace == 0) AbbrevToUse = PtrAbbrev;
211       break;
212     }
213     case Type::FunctionTyID: {
214       const FunctionType *FT = cast<FunctionType>(T);
215       // FUNCTION: [isvararg, attrid, retty, paramty x N]
216       Code = bitc::TYPE_CODE_FUNCTION;
217       TypeVals.push_back(FT->isVarArg());
218       TypeVals.push_back(0);  // FIXME: DEAD: remove in llvm 3.0
219       TypeVals.push_back(VE.getTypeID(FT->getReturnType()));
220       for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i)
221         TypeVals.push_back(VE.getTypeID(FT->getParamType(i)));
222       AbbrevToUse = FunctionAbbrev;
223       break;
224     }
225     case Type::StructTyID: {
226       const StructType *ST = cast<StructType>(T);
227       // STRUCT: [ispacked, eltty x N]
228       Code = bitc::TYPE_CODE_STRUCT;
229       TypeVals.push_back(ST->isPacked());
230       // Output all of the element types.
231       for (StructType::element_iterator I = ST->element_begin(),
232            E = ST->element_end(); I != E; ++I)
233         TypeVals.push_back(VE.getTypeID(*I));
234       AbbrevToUse = StructAbbrev;
235       break;
236     }
237     case Type::ArrayTyID: {
238       const ArrayType *AT = cast<ArrayType>(T);
239       // ARRAY: [numelts, eltty]
240       Code = bitc::TYPE_CODE_ARRAY;
241       TypeVals.push_back(AT->getNumElements());
242       TypeVals.push_back(VE.getTypeID(AT->getElementType()));
243       AbbrevToUse = ArrayAbbrev;
244       break;
245     }
246     case Type::VectorTyID: {
247       const VectorType *VT = cast<VectorType>(T);
248       // VECTOR [numelts, eltty]
249       Code = bitc::TYPE_CODE_VECTOR;
250       TypeVals.push_back(VT->getNumElements());
251       TypeVals.push_back(VE.getTypeID(VT->getElementType()));
252       break;
253     }
254     }
255
256     // Emit the finished record.
257     Stream.EmitRecord(Code, TypeVals, AbbrevToUse);
258     TypeVals.clear();
259   }
260   
261   Stream.ExitBlock();
262 }
263
264 static unsigned getEncodedLinkage(const GlobalValue *GV) {
265   switch (GV->getLinkage()) {
266   default: assert(0 && "Invalid linkage!");
267   case GlobalValue::GhostLinkage:  // Map ghost linkage onto external.
268   case GlobalValue::ExternalLinkage:     return 0;
269   case GlobalValue::WeakLinkage:         return 1;
270   case GlobalValue::AppendingLinkage:    return 2;
271   case GlobalValue::InternalLinkage:     return 3;
272   case GlobalValue::LinkOnceLinkage:     return 4;
273   case GlobalValue::DLLImportLinkage:    return 5;
274   case GlobalValue::DLLExportLinkage:    return 6;
275   case GlobalValue::ExternalWeakLinkage: return 7;
276   case GlobalValue::CommonLinkage:       return 8;
277   }
278 }
279
280 static unsigned getEncodedVisibility(const GlobalValue *GV) {
281   switch (GV->getVisibility()) {
282   default: assert(0 && "Invalid visibility!");
283   case GlobalValue::DefaultVisibility:   return 0;
284   case GlobalValue::HiddenVisibility:    return 1;
285   case GlobalValue::ProtectedVisibility: return 2;
286   }
287 }
288
289 // Emit top-level description of module, including target triple, inline asm,
290 // descriptors for global variables, and function prototype info.
291 static void WriteModuleInfo(const Module *M, const ValueEnumerator &VE,
292                             BitstreamWriter &Stream) {
293   // Emit the list of dependent libraries for the Module.
294   for (Module::lib_iterator I = M->lib_begin(), E = M->lib_end(); I != E; ++I)
295     WriteStringRecord(bitc::MODULE_CODE_DEPLIB, *I, 0/*TODO*/, Stream);
296
297   // Emit various pieces of data attached to a module.
298   if (!M->getTargetTriple().empty())
299     WriteStringRecord(bitc::MODULE_CODE_TRIPLE, M->getTargetTriple(),
300                       0/*TODO*/, Stream);
301   if (!M->getDataLayout().empty())
302     WriteStringRecord(bitc::MODULE_CODE_DATALAYOUT, M->getDataLayout(),
303                       0/*TODO*/, Stream);
304   if (!M->getModuleInlineAsm().empty())
305     WriteStringRecord(bitc::MODULE_CODE_ASM, M->getModuleInlineAsm(),
306                       0/*TODO*/, Stream);
307
308   // Emit information about sections and GC, computing how many there are. Also
309   // compute the maximum alignment value.
310   std::map<std::string, unsigned> SectionMap;
311   std::map<std::string, unsigned> GCMap;
312   unsigned MaxAlignment = 0;
313   unsigned MaxGlobalType = 0;
314   for (Module::const_global_iterator GV = M->global_begin(),E = M->global_end();
315        GV != E; ++GV) {
316     MaxAlignment = std::max(MaxAlignment, GV->getAlignment());
317     MaxGlobalType = std::max(MaxGlobalType, VE.getTypeID(GV->getType()));
318     
319     if (!GV->hasSection()) continue;
320     // Give section names unique ID's.
321     unsigned &Entry = SectionMap[GV->getSection()];
322     if (Entry != 0) continue;
323     WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, GV->getSection(),
324                       0/*TODO*/, Stream);
325     Entry = SectionMap.size();
326   }
327   for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
328     MaxAlignment = std::max(MaxAlignment, F->getAlignment());
329     if (F->hasSection()) {
330       // Give section names unique ID's.
331       unsigned &Entry = SectionMap[F->getSection()];
332       if (!Entry) {
333         WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, F->getSection(),
334                           0/*TODO*/, Stream);
335         Entry = SectionMap.size();
336       }
337     }
338     if (F->hasGC()) {
339       // Same for GC names.
340       unsigned &Entry = GCMap[F->getGC()];
341       if (!Entry) {
342         WriteStringRecord(bitc::MODULE_CODE_GCNAME, F->getGC(),
343                           0/*TODO*/, Stream);
344         Entry = GCMap.size();
345       }
346     }
347   }
348   
349   // Emit abbrev for globals, now that we know # sections and max alignment.
350   unsigned SimpleGVarAbbrev = 0;
351   if (!M->global_empty()) { 
352     // Add an abbrev for common globals with no visibility or thread localness.
353     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
354     Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_GLOBALVAR));
355     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
356                               Log2_32_Ceil(MaxGlobalType+1)));
357     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));      // Constant.
358     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));        // Initializer.
359     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4));      // Linkage.
360     if (MaxAlignment == 0)                                      // Alignment.
361       Abbv->Add(BitCodeAbbrevOp(0));
362     else {
363       unsigned MaxEncAlignment = Log2_32(MaxAlignment)+1;
364       Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
365                                Log2_32_Ceil(MaxEncAlignment+1)));
366     }
367     if (SectionMap.empty())                                    // Section.
368       Abbv->Add(BitCodeAbbrevOp(0));
369     else
370       Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
371                                Log2_32_Ceil(SectionMap.size()+1)));
372     // Don't bother emitting vis + thread local.
373     SimpleGVarAbbrev = Stream.EmitAbbrev(Abbv);
374   }
375   
376   // Emit the global variable information.
377   SmallVector<unsigned, 64> Vals;
378   for (Module::const_global_iterator GV = M->global_begin(),E = M->global_end();
379        GV != E; ++GV) {
380     unsigned AbbrevToUse = 0;
381
382     // GLOBALVAR: [type, isconst, initid, 
383     //             linkage, alignment, section, visibility, threadlocal]
384     Vals.push_back(VE.getTypeID(GV->getType()));
385     Vals.push_back(GV->isConstant());
386     Vals.push_back(GV->isDeclaration() ? 0 :
387                    (VE.getValueID(GV->getInitializer()) + 1));
388     Vals.push_back(getEncodedLinkage(GV));
389     Vals.push_back(Log2_32(GV->getAlignment())+1);
390     Vals.push_back(GV->hasSection() ? SectionMap[GV->getSection()] : 0);
391     if (GV->isThreadLocal() || 
392         GV->getVisibility() != GlobalValue::DefaultVisibility) {
393       Vals.push_back(getEncodedVisibility(GV));
394       Vals.push_back(GV->isThreadLocal());
395     } else {
396       AbbrevToUse = SimpleGVarAbbrev;
397     }
398     
399     Stream.EmitRecord(bitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse);
400     Vals.clear();
401   }
402
403   // Emit the function proto information.
404   for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
405     // FUNCTION:  [type, callingconv, isproto, paramattr,
406     //             linkage, alignment, section, visibility, gc]
407     Vals.push_back(VE.getTypeID(F->getType()));
408     Vals.push_back(F->getCallingConv());
409     Vals.push_back(F->isDeclaration());
410     Vals.push_back(getEncodedLinkage(F));
411     Vals.push_back(VE.getAttributeID(F->getAttributes()));
412     Vals.push_back(Log2_32(F->getAlignment())+1);
413     Vals.push_back(F->hasSection() ? SectionMap[F->getSection()] : 0);
414     Vals.push_back(getEncodedVisibility(F));
415     Vals.push_back(F->hasGC() ? GCMap[F->getGC()] : 0);
416     
417     unsigned AbbrevToUse = 0;
418     Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse);
419     Vals.clear();
420   }
421   
422   
423   // Emit the alias information.
424   for (Module::const_alias_iterator AI = M->alias_begin(), E = M->alias_end();
425        AI != E; ++AI) {
426     Vals.push_back(VE.getTypeID(AI->getType()));
427     Vals.push_back(VE.getValueID(AI->getAliasee()));
428     Vals.push_back(getEncodedLinkage(AI));
429     Vals.push_back(getEncodedVisibility(AI));
430     unsigned AbbrevToUse = 0;
431     Stream.EmitRecord(bitc::MODULE_CODE_ALIAS, Vals, AbbrevToUse);
432     Vals.clear();
433   }
434 }
435
436
437 static void WriteConstants(unsigned FirstVal, unsigned LastVal,
438                            const ValueEnumerator &VE,
439                            BitstreamWriter &Stream, bool isGlobal) {
440   if (FirstVal == LastVal) return;
441   
442   Stream.EnterSubblock(bitc::CONSTANTS_BLOCK_ID, 4);
443
444   unsigned AggregateAbbrev = 0;
445   unsigned String8Abbrev = 0;
446   unsigned CString7Abbrev = 0;
447   unsigned CString6Abbrev = 0;
448   // If this is a constant pool for the module, emit module-specific abbrevs.
449   if (isGlobal) {
450     // Abbrev for CST_CODE_AGGREGATE.
451     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
452     Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_AGGREGATE));
453     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
454     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, Log2_32_Ceil(LastVal+1)));
455     AggregateAbbrev = Stream.EmitAbbrev(Abbv);
456
457     // Abbrev for CST_CODE_STRING.
458     Abbv = new BitCodeAbbrev();
459     Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_STRING));
460     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
461     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
462     String8Abbrev = Stream.EmitAbbrev(Abbv);
463     // Abbrev for CST_CODE_CSTRING.
464     Abbv = new BitCodeAbbrev();
465     Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
466     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
467     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
468     CString7Abbrev = Stream.EmitAbbrev(Abbv);
469     // Abbrev for CST_CODE_CSTRING.
470     Abbv = new BitCodeAbbrev();
471     Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
472     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
473     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
474     CString6Abbrev = Stream.EmitAbbrev(Abbv);
475   }  
476   
477   SmallVector<uint64_t, 64> Record;
478
479   const ValueEnumerator::ValueList &Vals = VE.getValues();
480   const Type *LastTy = 0;
481   for (unsigned i = FirstVal; i != LastVal; ++i) {
482     const Value *V = Vals[i].first;
483     // If we need to switch types, do so now.
484     if (V->getType() != LastTy) {
485       LastTy = V->getType();
486       Record.push_back(VE.getTypeID(LastTy));
487       Stream.EmitRecord(bitc::CST_CODE_SETTYPE, Record,
488                         CONSTANTS_SETTYPE_ABBREV);
489       Record.clear();
490     }
491     
492     if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
493       Record.push_back(unsigned(IA->hasSideEffects()));
494       
495       // Add the asm string.
496       const std::string &AsmStr = IA->getAsmString();
497       Record.push_back(AsmStr.size());
498       for (unsigned i = 0, e = AsmStr.size(); i != e; ++i)
499         Record.push_back(AsmStr[i]);
500       
501       // Add the constraint string.
502       const std::string &ConstraintStr = IA->getConstraintString();
503       Record.push_back(ConstraintStr.size());
504       for (unsigned i = 0, e = ConstraintStr.size(); i != e; ++i)
505         Record.push_back(ConstraintStr[i]);
506       Stream.EmitRecord(bitc::CST_CODE_INLINEASM, Record);
507       Record.clear();
508       continue;
509     }
510     const Constant *C = cast<Constant>(V);
511     unsigned Code = -1U;
512     unsigned AbbrevToUse = 0;
513     if (C->isNullValue()) {
514       Code = bitc::CST_CODE_NULL;
515     } else if (isa<UndefValue>(C)) {
516       Code = bitc::CST_CODE_UNDEF;
517     } else if (const ConstantInt *IV = dyn_cast<ConstantInt>(C)) {
518       if (IV->getBitWidth() <= 64) {
519         int64_t V = IV->getSExtValue();
520         if (V >= 0)
521           Record.push_back(V << 1);
522         else
523           Record.push_back((-V << 1) | 1);
524         Code = bitc::CST_CODE_INTEGER;
525         AbbrevToUse = CONSTANTS_INTEGER_ABBREV;
526       } else {                             // Wide integers, > 64 bits in size.
527         // We have an arbitrary precision integer value to write whose 
528         // bit width is > 64. However, in canonical unsigned integer 
529         // format it is likely that the high bits are going to be zero.
530         // So, we only write the number of active words.
531         unsigned NWords = IV->getValue().getActiveWords(); 
532         const uint64_t *RawWords = IV->getValue().getRawData();
533         for (unsigned i = 0; i != NWords; ++i) {
534           int64_t V = RawWords[i];
535           if (V >= 0)
536             Record.push_back(V << 1);
537           else
538             Record.push_back((-V << 1) | 1);
539         }
540         Code = bitc::CST_CODE_WIDE_INTEGER;
541       }
542     } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
543       Code = bitc::CST_CODE_FLOAT;
544       const Type *Ty = CFP->getType();
545       if (Ty == Type::FloatTy || Ty == Type::DoubleTy) {
546         Record.push_back(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
547       } else if (Ty == Type::X86_FP80Ty) {
548         // api needed to prevent premature destruction
549         APInt api = CFP->getValueAPF().bitcastToAPInt();
550         const uint64_t *p = api.getRawData();
551         Record.push_back(p[0]);
552         Record.push_back((uint16_t)p[1]);
553       } else if (Ty == Type::FP128Ty || Ty == Type::PPC_FP128Ty) {
554         APInt api = CFP->getValueAPF().bitcastToAPInt();
555         const uint64_t *p = api.getRawData();
556         Record.push_back(p[0]);
557         Record.push_back(p[1]);
558       } else {
559         assert (0 && "Unknown FP type!");
560       }
561     } else if (isa<ConstantArray>(C) && cast<ConstantArray>(C)->isString()) {
562       // Emit constant strings specially.
563       unsigned NumOps = C->getNumOperands();
564       // If this is a null-terminated string, use the denser CSTRING encoding.
565       if (C->getOperand(NumOps-1)->isNullValue()) {
566         Code = bitc::CST_CODE_CSTRING;
567         --NumOps;  // Don't encode the null, which isn't allowed by char6.
568       } else {
569         Code = bitc::CST_CODE_STRING;
570         AbbrevToUse = String8Abbrev;
571       }
572       bool isCStr7 = Code == bitc::CST_CODE_CSTRING;
573       bool isCStrChar6 = Code == bitc::CST_CODE_CSTRING;
574       for (unsigned i = 0; i != NumOps; ++i) {
575         unsigned char V = cast<ConstantInt>(C->getOperand(i))->getZExtValue();
576         Record.push_back(V);
577         isCStr7 &= (V & 128) == 0;
578         if (isCStrChar6) 
579           isCStrChar6 = BitCodeAbbrevOp::isChar6(V);
580       }
581       
582       if (isCStrChar6)
583         AbbrevToUse = CString6Abbrev;
584       else if (isCStr7)
585         AbbrevToUse = CString7Abbrev;
586     } else if (isa<ConstantArray>(C) || isa<ConstantStruct>(V) ||
587                isa<ConstantVector>(V)) {
588       Code = bitc::CST_CODE_AGGREGATE;
589       for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
590         Record.push_back(VE.getValueID(C->getOperand(i)));
591       AbbrevToUse = AggregateAbbrev;
592     } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
593       switch (CE->getOpcode()) {
594       default:
595         if (Instruction::isCast(CE->getOpcode())) {
596           Code = bitc::CST_CODE_CE_CAST;
597           Record.push_back(GetEncodedCastOpcode(CE->getOpcode()));
598           Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
599           Record.push_back(VE.getValueID(C->getOperand(0)));
600           AbbrevToUse = CONSTANTS_CE_CAST_Abbrev;
601         } else {
602           assert(CE->getNumOperands() == 2 && "Unknown constant expr!");
603           Code = bitc::CST_CODE_CE_BINOP;
604           Record.push_back(GetEncodedBinaryOpcode(CE->getOpcode()));
605           Record.push_back(VE.getValueID(C->getOperand(0)));
606           Record.push_back(VE.getValueID(C->getOperand(1)));
607         }
608         break;
609       case Instruction::GetElementPtr:
610         Code = bitc::CST_CODE_CE_GEP;
611         for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) {
612           Record.push_back(VE.getTypeID(C->getOperand(i)->getType()));
613           Record.push_back(VE.getValueID(C->getOperand(i)));
614         }
615         break;
616       case Instruction::Select:
617         Code = bitc::CST_CODE_CE_SELECT;
618         Record.push_back(VE.getValueID(C->getOperand(0)));
619         Record.push_back(VE.getValueID(C->getOperand(1)));
620         Record.push_back(VE.getValueID(C->getOperand(2)));
621         break;
622       case Instruction::ExtractElement:
623         Code = bitc::CST_CODE_CE_EXTRACTELT;
624         Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
625         Record.push_back(VE.getValueID(C->getOperand(0)));
626         Record.push_back(VE.getValueID(C->getOperand(1)));
627         break;
628       case Instruction::InsertElement:
629         Code = bitc::CST_CODE_CE_INSERTELT;
630         Record.push_back(VE.getValueID(C->getOperand(0)));
631         Record.push_back(VE.getValueID(C->getOperand(1)));
632         Record.push_back(VE.getValueID(C->getOperand(2)));
633         break;
634       case Instruction::ShuffleVector:
635         Code = bitc::CST_CODE_CE_SHUFFLEVEC;
636         Record.push_back(VE.getValueID(C->getOperand(0)));
637         Record.push_back(VE.getValueID(C->getOperand(1)));
638         Record.push_back(VE.getValueID(C->getOperand(2)));
639         break;
640       case Instruction::ICmp:
641       case Instruction::FCmp:
642       case Instruction::VICmp:
643       case Instruction::VFCmp:
644         if (isa<VectorType>(C->getOperand(0)->getType())
645             && (CE->getOpcode() == Instruction::ICmp
646                 || CE->getOpcode() == Instruction::FCmp)) {
647           // compare returning vector of Int1Ty
648           assert(0 && "Unsupported constant!");
649         } else {
650           Code = bitc::CST_CODE_CE_CMP;
651         }
652         Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
653         Record.push_back(VE.getValueID(C->getOperand(0)));
654         Record.push_back(VE.getValueID(C->getOperand(1)));
655         Record.push_back(CE->getPredicate());
656         break;
657       }
658     } else {
659       assert(0 && "Unknown constant!");
660     }
661     Stream.EmitRecord(Code, Record, AbbrevToUse);
662     Record.clear();
663   }
664
665   Stream.ExitBlock();
666 }
667
668 static void WriteModuleConstants(const ValueEnumerator &VE,
669                                  BitstreamWriter &Stream) {
670   const ValueEnumerator::ValueList &Vals = VE.getValues();
671   
672   // Find the first constant to emit, which is the first non-globalvalue value.
673   // We know globalvalues have been emitted by WriteModuleInfo.
674   for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
675     if (!isa<GlobalValue>(Vals[i].first)) {
676       WriteConstants(i, Vals.size(), VE, Stream, true);
677       return;
678     }
679   }
680 }
681
682 /// PushValueAndType - The file has to encode both the value and type id for
683 /// many values, because we need to know what type to create for forward
684 /// references.  However, most operands are not forward references, so this type
685 /// field is not needed.
686 ///
687 /// This function adds V's value ID to Vals.  If the value ID is higher than the
688 /// instruction ID, then it is a forward reference, and it also includes the
689 /// type ID.
690 static bool PushValueAndType(Value *V, unsigned InstID,
691                              SmallVector<unsigned, 64> &Vals, 
692                              ValueEnumerator &VE) {
693   unsigned ValID = VE.getValueID(V);
694   Vals.push_back(ValID);
695   if (ValID >= InstID) {
696     Vals.push_back(VE.getTypeID(V->getType()));
697     return true;
698   }
699   return false;
700 }
701
702 /// WriteInstruction - Emit an instruction to the specified stream.
703 static void WriteInstruction(const Instruction &I, unsigned InstID,
704                              ValueEnumerator &VE, BitstreamWriter &Stream,
705                              SmallVector<unsigned, 64> &Vals) {
706   unsigned Code = 0;
707   unsigned AbbrevToUse = 0;
708   switch (I.getOpcode()) {
709   default:
710     if (Instruction::isCast(I.getOpcode())) {
711       Code = bitc::FUNC_CODE_INST_CAST;
712       if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE))
713         AbbrevToUse = FUNCTION_INST_CAST_ABBREV;
714       Vals.push_back(VE.getTypeID(I.getType()));
715       Vals.push_back(GetEncodedCastOpcode(I.getOpcode()));
716     } else {
717       assert(isa<BinaryOperator>(I) && "Unknown instruction!");
718       Code = bitc::FUNC_CODE_INST_BINOP;
719       if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE))
720         AbbrevToUse = FUNCTION_INST_BINOP_ABBREV;
721       Vals.push_back(VE.getValueID(I.getOperand(1)));
722       Vals.push_back(GetEncodedBinaryOpcode(I.getOpcode()));
723     }
724     break;
725
726   case Instruction::GetElementPtr:
727     Code = bitc::FUNC_CODE_INST_GEP;
728     for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
729       PushValueAndType(I.getOperand(i), InstID, Vals, VE);
730     break;
731   case Instruction::ExtractValue: {
732     Code = bitc::FUNC_CODE_INST_EXTRACTVAL;
733     PushValueAndType(I.getOperand(0), InstID, Vals, VE);
734     const ExtractValueInst *EVI = cast<ExtractValueInst>(&I);
735     for (const unsigned *i = EVI->idx_begin(), *e = EVI->idx_end(); i != e; ++i)
736       Vals.push_back(*i);
737     break;
738   }
739   case Instruction::InsertValue: {
740     Code = bitc::FUNC_CODE_INST_INSERTVAL;
741     PushValueAndType(I.getOperand(0), InstID, Vals, VE);
742     PushValueAndType(I.getOperand(1), InstID, Vals, VE);
743     const InsertValueInst *IVI = cast<InsertValueInst>(&I);
744     for (const unsigned *i = IVI->idx_begin(), *e = IVI->idx_end(); i != e; ++i)
745       Vals.push_back(*i);
746     break;
747   }
748   case Instruction::Select:
749     Code = bitc::FUNC_CODE_INST_VSELECT;
750     PushValueAndType(I.getOperand(1), InstID, Vals, VE);
751     Vals.push_back(VE.getValueID(I.getOperand(2)));
752     PushValueAndType(I.getOperand(0), InstID, Vals, VE);
753     break;
754   case Instruction::ExtractElement:
755     Code = bitc::FUNC_CODE_INST_EXTRACTELT;
756     PushValueAndType(I.getOperand(0), InstID, Vals, VE);
757     Vals.push_back(VE.getValueID(I.getOperand(1)));
758     break;
759   case Instruction::InsertElement:
760     Code = bitc::FUNC_CODE_INST_INSERTELT;
761     PushValueAndType(I.getOperand(0), InstID, Vals, VE);
762     Vals.push_back(VE.getValueID(I.getOperand(1)));
763     Vals.push_back(VE.getValueID(I.getOperand(2)));
764     break;
765   case Instruction::ShuffleVector:
766     Code = bitc::FUNC_CODE_INST_SHUFFLEVEC;
767     PushValueAndType(I.getOperand(0), InstID, Vals, VE);
768     Vals.push_back(VE.getValueID(I.getOperand(1)));
769     Vals.push_back(VE.getValueID(I.getOperand(2)));
770     break;
771   case Instruction::ICmp:
772   case Instruction::FCmp:
773   case Instruction::VICmp:
774   case Instruction::VFCmp:
775     if (I.getOpcode() == Instruction::ICmp
776         || I.getOpcode() == Instruction::FCmp) {
777       // compare returning Int1Ty or vector of Int1Ty
778       Code = bitc::FUNC_CODE_INST_CMP2;
779     } else {
780       Code = bitc::FUNC_CODE_INST_CMP;
781     }
782     PushValueAndType(I.getOperand(0), InstID, Vals, VE);
783     Vals.push_back(VE.getValueID(I.getOperand(1)));
784     Vals.push_back(cast<CmpInst>(I).getPredicate());
785     break;
786
787   case Instruction::Ret: 
788     {
789       Code = bitc::FUNC_CODE_INST_RET;
790       unsigned NumOperands = I.getNumOperands();
791       if (NumOperands == 0)
792         AbbrevToUse = FUNCTION_INST_RET_VOID_ABBREV;
793       else if (NumOperands == 1) {
794         if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE))
795           AbbrevToUse = FUNCTION_INST_RET_VAL_ABBREV;
796       } else {
797         for (unsigned i = 0, e = NumOperands; i != e; ++i)
798           PushValueAndType(I.getOperand(i), InstID, Vals, VE);
799       }
800     }
801     break;
802   case Instruction::Br:
803     Code = bitc::FUNC_CODE_INST_BR;
804     Vals.push_back(VE.getValueID(I.getOperand(0)));
805     if (cast<BranchInst>(I).isConditional()) {
806       Vals.push_back(VE.getValueID(I.getOperand(1)));
807       Vals.push_back(VE.getValueID(I.getOperand(2)));
808     }
809     break;
810   case Instruction::Switch:
811     Code = bitc::FUNC_CODE_INST_SWITCH;
812     Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
813     for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
814       Vals.push_back(VE.getValueID(I.getOperand(i)));
815     break;
816   case Instruction::Invoke: {
817     const PointerType *PTy = cast<PointerType>(I.getOperand(0)->getType());
818     const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
819     Code = bitc::FUNC_CODE_INST_INVOKE;
820     
821     const InvokeInst *II = cast<InvokeInst>(&I);
822     Vals.push_back(VE.getAttributeID(II->getAttributes()));
823     Vals.push_back(II->getCallingConv());
824     Vals.push_back(VE.getValueID(I.getOperand(1)));      // normal dest
825     Vals.push_back(VE.getValueID(I.getOperand(2)));      // unwind dest
826     PushValueAndType(I.getOperand(0), InstID, Vals, VE); // callee
827     
828     // Emit value #'s for the fixed parameters.
829     for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
830       Vals.push_back(VE.getValueID(I.getOperand(i+3)));  // fixed param.
831
832     // Emit type/value pairs for varargs params.
833     if (FTy->isVarArg()) {
834       for (unsigned i = 3+FTy->getNumParams(), e = I.getNumOperands();
835            i != e; ++i)
836         PushValueAndType(I.getOperand(i), InstID, Vals, VE); // vararg
837     }
838     break;
839   }
840   case Instruction::Unwind:
841     Code = bitc::FUNC_CODE_INST_UNWIND;
842     break;
843   case Instruction::Unreachable:
844     Code = bitc::FUNC_CODE_INST_UNREACHABLE;
845     AbbrevToUse = FUNCTION_INST_UNREACHABLE_ABBREV;
846     break;
847   
848   case Instruction::PHI:
849     Code = bitc::FUNC_CODE_INST_PHI;
850     Vals.push_back(VE.getTypeID(I.getType()));
851     for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
852       Vals.push_back(VE.getValueID(I.getOperand(i)));
853     break;
854     
855   case Instruction::Malloc:
856     Code = bitc::FUNC_CODE_INST_MALLOC;
857     Vals.push_back(VE.getTypeID(I.getType()));
858     Vals.push_back(VE.getValueID(I.getOperand(0))); // size.
859     Vals.push_back(Log2_32(cast<MallocInst>(I).getAlignment())+1);
860     break;
861     
862   case Instruction::Free:
863     Code = bitc::FUNC_CODE_INST_FREE;
864     PushValueAndType(I.getOperand(0), InstID, Vals, VE);
865     break;
866     
867   case Instruction::Alloca:
868     Code = bitc::FUNC_CODE_INST_ALLOCA;
869     Vals.push_back(VE.getTypeID(I.getType()));
870     Vals.push_back(VE.getValueID(I.getOperand(0))); // size.
871     Vals.push_back(Log2_32(cast<AllocaInst>(I).getAlignment())+1);
872     break;
873     
874   case Instruction::Load:
875     Code = bitc::FUNC_CODE_INST_LOAD;
876     if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE))  // ptr
877       AbbrevToUse = FUNCTION_INST_LOAD_ABBREV;
878       
879     Vals.push_back(Log2_32(cast<LoadInst>(I).getAlignment())+1);
880     Vals.push_back(cast<LoadInst>(I).isVolatile());
881     break;
882   case Instruction::Store:
883     Code = bitc::FUNC_CODE_INST_STORE2;
884     PushValueAndType(I.getOperand(1), InstID, Vals, VE);  // ptrty + ptr
885     Vals.push_back(VE.getValueID(I.getOperand(0)));       // val.
886     Vals.push_back(Log2_32(cast<StoreInst>(I).getAlignment())+1);
887     Vals.push_back(cast<StoreInst>(I).isVolatile());
888     break;
889   case Instruction::Call: {
890     const PointerType *PTy = cast<PointerType>(I.getOperand(0)->getType());
891     const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
892
893     Code = bitc::FUNC_CODE_INST_CALL;
894     
895     const CallInst *CI = cast<CallInst>(&I);
896     Vals.push_back(VE.getAttributeID(CI->getAttributes()));
897     Vals.push_back((CI->getCallingConv() << 1) | unsigned(CI->isTailCall()));
898     PushValueAndType(CI->getOperand(0), InstID, Vals, VE);  // Callee
899     
900     // Emit value #'s for the fixed parameters.
901     for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
902       Vals.push_back(VE.getValueID(I.getOperand(i+1)));  // fixed param.
903       
904     // Emit type/value pairs for varargs params.
905     if (FTy->isVarArg()) {
906       unsigned NumVarargs = I.getNumOperands()-1-FTy->getNumParams();
907       for (unsigned i = I.getNumOperands()-NumVarargs, e = I.getNumOperands();
908            i != e; ++i)
909         PushValueAndType(I.getOperand(i), InstID, Vals, VE);  // varargs
910     }
911     break;
912   }
913   case Instruction::VAArg:
914     Code = bitc::FUNC_CODE_INST_VAARG;
915     Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));   // valistty
916     Vals.push_back(VE.getValueID(I.getOperand(0))); // valist.
917     Vals.push_back(VE.getTypeID(I.getType())); // restype.
918     break;
919   }
920   
921   Stream.EmitRecord(Code, Vals, AbbrevToUse);
922   Vals.clear();
923 }
924
925 // Emit names for globals/functions etc.
926 static void WriteValueSymbolTable(const ValueSymbolTable &VST,
927                                   const ValueEnumerator &VE,
928                                   BitstreamWriter &Stream) {
929   if (VST.empty()) return;
930   Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4);
931
932   // FIXME: Set up the abbrev, we know how many values there are!
933   // FIXME: We know if the type names can use 7-bit ascii.
934   SmallVector<unsigned, 64> NameVals;
935   
936   for (ValueSymbolTable::const_iterator SI = VST.begin(), SE = VST.end();
937        SI != SE; ++SI) {
938     
939     const ValueName &Name = *SI;
940     
941     // Figure out the encoding to use for the name.
942     bool is7Bit = true;
943     bool isChar6 = true;
944     for (const char *C = Name.getKeyData(), *E = C+Name.getKeyLength();
945          C != E; ++C) {
946       if (isChar6) 
947         isChar6 = BitCodeAbbrevOp::isChar6(*C);
948       if ((unsigned char)*C & 128) {
949         is7Bit = false;
950         break;  // don't bother scanning the rest.
951       }
952     }
953     
954     unsigned AbbrevToUse = VST_ENTRY_8_ABBREV;
955     
956     // VST_ENTRY:   [valueid, namechar x N]
957     // VST_BBENTRY: [bbid, namechar x N]
958     unsigned Code;
959     if (isa<BasicBlock>(SI->getValue())) {
960       Code = bitc::VST_CODE_BBENTRY;
961       if (isChar6)
962         AbbrevToUse = VST_BBENTRY_6_ABBREV;
963     } else {
964       Code = bitc::VST_CODE_ENTRY;
965       if (isChar6)
966         AbbrevToUse = VST_ENTRY_6_ABBREV;
967       else if (is7Bit)
968         AbbrevToUse = VST_ENTRY_7_ABBREV;
969     }
970     
971     NameVals.push_back(VE.getValueID(SI->getValue()));
972     for (const char *P = Name.getKeyData(),
973          *E = Name.getKeyData()+Name.getKeyLength(); P != E; ++P)
974       NameVals.push_back((unsigned char)*P);
975     
976     // Emit the finished record.
977     Stream.EmitRecord(Code, NameVals, AbbrevToUse);
978     NameVals.clear();
979   }
980   Stream.ExitBlock();
981 }
982
983 /// WriteFunction - Emit a function body to the module stream.
984 static void WriteFunction(const Function &F, ValueEnumerator &VE, 
985                           BitstreamWriter &Stream) {
986   Stream.EnterSubblock(bitc::FUNCTION_BLOCK_ID, 4);
987   VE.incorporateFunction(F);
988
989   SmallVector<unsigned, 64> Vals;
990   
991   // Emit the number of basic blocks, so the reader can create them ahead of
992   // time.
993   Vals.push_back(VE.getBasicBlocks().size());
994   Stream.EmitRecord(bitc::FUNC_CODE_DECLAREBLOCKS, Vals);
995   Vals.clear();
996   
997   // If there are function-local constants, emit them now.
998   unsigned CstStart, CstEnd;
999   VE.getFunctionConstantRange(CstStart, CstEnd);
1000   WriteConstants(CstStart, CstEnd, VE, Stream, false);
1001   
1002   // Keep a running idea of what the instruction ID is. 
1003   unsigned InstID = CstEnd;
1004   
1005   // Finally, emit all the instructions, in order.
1006   for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
1007     for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
1008          I != E; ++I) {
1009       WriteInstruction(*I, InstID, VE, Stream, Vals);
1010       if (I->getType() != Type::VoidTy)
1011         ++InstID;
1012     }
1013   
1014   // Emit names for all the instructions etc.
1015   WriteValueSymbolTable(F.getValueSymbolTable(), VE, Stream);
1016     
1017   VE.purgeFunction();
1018   Stream.ExitBlock();
1019 }
1020
1021 /// WriteTypeSymbolTable - Emit a block for the specified type symtab.
1022 static void WriteTypeSymbolTable(const TypeSymbolTable &TST,
1023                                  const ValueEnumerator &VE,
1024                                  BitstreamWriter &Stream) {
1025   if (TST.empty()) return;
1026   
1027   Stream.EnterSubblock(bitc::TYPE_SYMTAB_BLOCK_ID, 3);
1028   
1029   // 7-bit fixed width VST_CODE_ENTRY strings.
1030   BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1031   Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
1032   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
1033                             Log2_32_Ceil(VE.getTypes().size()+1)));
1034   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1035   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
1036   unsigned V7Abbrev = Stream.EmitAbbrev(Abbv);
1037   
1038   SmallVector<unsigned, 64> NameVals;
1039   
1040   for (TypeSymbolTable::const_iterator TI = TST.begin(), TE = TST.end(); 
1041        TI != TE; ++TI) {
1042     // TST_ENTRY: [typeid, namechar x N]
1043     NameVals.push_back(VE.getTypeID(TI->second));
1044     
1045     const std::string &Str = TI->first;
1046     bool is7Bit = true;
1047     for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1048       NameVals.push_back((unsigned char)Str[i]);
1049       if (Str[i] & 128)
1050         is7Bit = false;
1051     }
1052     
1053     // Emit the finished record.
1054     Stream.EmitRecord(bitc::VST_CODE_ENTRY, NameVals, is7Bit ? V7Abbrev : 0);
1055     NameVals.clear();
1056   }
1057   
1058   Stream.ExitBlock();
1059 }
1060
1061 // Emit blockinfo, which defines the standard abbreviations etc.
1062 static void WriteBlockInfo(const ValueEnumerator &VE, BitstreamWriter &Stream) {
1063   // We only want to emit block info records for blocks that have multiple
1064   // instances: CONSTANTS_BLOCK, FUNCTION_BLOCK and VALUE_SYMTAB_BLOCK.  Other
1065   // blocks can defined their abbrevs inline.
1066   Stream.EnterBlockInfoBlock(2);
1067   
1068   { // 8-bit fixed-width VST_ENTRY/VST_BBENTRY strings.
1069     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1070     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3));
1071     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1072     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1073     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
1074     if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, 
1075                                    Abbv) != VST_ENTRY_8_ABBREV)
1076       assert(0 && "Unexpected abbrev ordering!");
1077   }
1078   
1079   { // 7-bit fixed width VST_ENTRY strings.
1080     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1081     Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
1082     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1083     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1084     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
1085     if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
1086                                    Abbv) != VST_ENTRY_7_ABBREV)
1087       assert(0 && "Unexpected abbrev ordering!");
1088   }
1089   { // 6-bit char6 VST_ENTRY strings.
1090     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1091     Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
1092     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1093     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1094     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
1095     if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
1096                                    Abbv) != VST_ENTRY_6_ABBREV)
1097       assert(0 && "Unexpected abbrev ordering!");
1098   }
1099   { // 6-bit char6 VST_BBENTRY strings.
1100     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1101     Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_BBENTRY));
1102     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1103     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1104     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
1105     if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
1106                                    Abbv) != VST_BBENTRY_6_ABBREV)
1107       assert(0 && "Unexpected abbrev ordering!");
1108   }
1109   
1110   
1111   
1112   { // SETTYPE abbrev for CONSTANTS_BLOCK.
1113     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1114     Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_SETTYPE));
1115     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
1116                               Log2_32_Ceil(VE.getTypes().size()+1)));
1117     if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
1118                                    Abbv) != CONSTANTS_SETTYPE_ABBREV)
1119       assert(0 && "Unexpected abbrev ordering!");
1120   }
1121   
1122   { // INTEGER abbrev for CONSTANTS_BLOCK.
1123     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1124     Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_INTEGER));
1125     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1126     if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
1127                                    Abbv) != CONSTANTS_INTEGER_ABBREV)
1128       assert(0 && "Unexpected abbrev ordering!");
1129   }
1130   
1131   { // CE_CAST abbrev for CONSTANTS_BLOCK.
1132     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1133     Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CE_CAST));
1134     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4));  // cast opc
1135     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,       // typeid
1136                               Log2_32_Ceil(VE.getTypes().size()+1)));
1137     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));    // value id
1138
1139     if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
1140                                    Abbv) != CONSTANTS_CE_CAST_Abbrev)
1141       assert(0 && "Unexpected abbrev ordering!");
1142   }
1143   { // NULL abbrev for CONSTANTS_BLOCK.
1144     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1145     Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_NULL));
1146     if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
1147                                    Abbv) != CONSTANTS_NULL_Abbrev)
1148       assert(0 && "Unexpected abbrev ordering!");
1149   }
1150   
1151   // FIXME: This should only use space for first class types!
1152  
1153   { // INST_LOAD abbrev for FUNCTION_BLOCK.
1154     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1155     Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_LOAD));
1156     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Ptr
1157     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // Align
1158     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // volatile
1159     if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1160                                    Abbv) != FUNCTION_INST_LOAD_ABBREV)
1161       assert(0 && "Unexpected abbrev ordering!");
1162   }
1163   { // INST_BINOP abbrev for FUNCTION_BLOCK.
1164     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1165     Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP));
1166     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
1167     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS
1168     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
1169     if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1170                                    Abbv) != FUNCTION_INST_BINOP_ABBREV)
1171       assert(0 && "Unexpected abbrev ordering!");
1172   }
1173   { // INST_CAST abbrev for FUNCTION_BLOCK.
1174     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1175     Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_CAST));
1176     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));    // OpVal
1177     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,       // dest ty
1178                               Log2_32_Ceil(VE.getTypes().size()+1)));
1179     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4));  // opc
1180     if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1181                                    Abbv) != FUNCTION_INST_CAST_ABBREV)
1182       assert(0 && "Unexpected abbrev ordering!");
1183   }
1184   
1185   { // INST_RET abbrev for FUNCTION_BLOCK.
1186     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1187     Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET));
1188     if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1189                                    Abbv) != FUNCTION_INST_RET_VOID_ABBREV)
1190       assert(0 && "Unexpected abbrev ordering!");
1191   }
1192   { // INST_RET abbrev for FUNCTION_BLOCK.
1193     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1194     Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET));
1195     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ValID
1196     if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1197                                    Abbv) != FUNCTION_INST_RET_VAL_ABBREV)
1198       assert(0 && "Unexpected abbrev ordering!");
1199   }
1200   { // INST_UNREACHABLE abbrev for FUNCTION_BLOCK.
1201     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1202     Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_UNREACHABLE));
1203     if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1204                                    Abbv) != FUNCTION_INST_UNREACHABLE_ABBREV)
1205       assert(0 && "Unexpected abbrev ordering!");
1206   }
1207   
1208   Stream.ExitBlock();
1209 }
1210
1211
1212 /// WriteModule - Emit the specified module to the bitstream.
1213 static void WriteModule(const Module *M, BitstreamWriter &Stream) {
1214   Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3);
1215   
1216   // Emit the version number if it is non-zero.
1217   if (CurVersion) {
1218     SmallVector<unsigned, 1> Vals;
1219     Vals.push_back(CurVersion);
1220     Stream.EmitRecord(bitc::MODULE_CODE_VERSION, Vals);
1221   }
1222   
1223   // Analyze the module, enumerating globals, functions, etc.
1224   ValueEnumerator VE(M);
1225
1226   // Emit blockinfo, which defines the standard abbreviations etc.
1227   WriteBlockInfo(VE, Stream);
1228   
1229   // Emit information about parameter attributes.
1230   WriteAttributeTable(VE, Stream);
1231   
1232   // Emit information describing all of the types in the module.
1233   WriteTypeTable(VE, Stream);
1234   
1235   // Emit top-level description of module, including target triple, inline asm,
1236   // descriptors for global variables, and function prototype info.
1237   WriteModuleInfo(M, VE, Stream);
1238   
1239   // Emit constants.
1240   WriteModuleConstants(VE, Stream);
1241   
1242   // If we have any aggregate values in the value table, purge them - these can
1243   // only be used to initialize global variables.  Doing so makes the value
1244   // namespace smaller for code in functions.
1245   int NumNonAggregates = VE.PurgeAggregateValues();
1246   if (NumNonAggregates != -1) {
1247     SmallVector<unsigned, 1> Vals;
1248     Vals.push_back(NumNonAggregates);
1249     Stream.EmitRecord(bitc::MODULE_CODE_PURGEVALS, Vals);
1250   }
1251   
1252   // Emit function bodies.
1253   for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
1254     if (!I->isDeclaration())
1255       WriteFunction(*I, VE, Stream);
1256   
1257   // Emit the type symbol table information.
1258   WriteTypeSymbolTable(M->getTypeSymbolTable(), VE, Stream);
1259   
1260   // Emit names for globals/functions etc.
1261   WriteValueSymbolTable(M->getValueSymbolTable(), VE, Stream);
1262   
1263   Stream.ExitBlock();
1264 }
1265
1266 /// EmitDarwinBCHeader - If generating a bc file on darwin, we have to emit a
1267 /// header and trailer to make it compatible with the system archiver.  To do
1268 /// this we emit the following header, and then emit a trailer that pads the
1269 /// file out to be a multiple of 16 bytes.
1270 /// 
1271 /// struct bc_header {
1272 ///   uint32_t Magic;         // 0x0B17C0DE
1273 ///   uint32_t Version;       // Version, currently always 0.
1274 ///   uint32_t BitcodeOffset; // Offset to traditional bitcode file.
1275 ///   uint32_t BitcodeSize;   // Size of traditional bitcode file.
1276 ///   uint32_t CPUType;       // CPU specifier.
1277 ///   ... potentially more later ...
1278 /// };
1279 enum {
1280   DarwinBCSizeFieldOffset = 3*4, // Offset to bitcode_size.
1281   DarwinBCHeaderSize = 5*4
1282 };
1283
1284 static void EmitDarwinBCHeader(BitstreamWriter &Stream,
1285                                const std::string &TT) {
1286   unsigned CPUType = ~0U;
1287   
1288   // Match x86_64-*, i[3-9]86-*, powerpc-*, powerpc64-*.  The CPUType is a
1289   // magic number from /usr/include/mach/machine.h.  It is ok to reproduce the
1290   // specific constants here because they are implicitly part of the Darwin ABI.
1291   enum {
1292     DARWIN_CPU_ARCH_ABI64      = 0x01000000,
1293     DARWIN_CPU_TYPE_X86        = 7,
1294     DARWIN_CPU_TYPE_POWERPC    = 18
1295   };
1296   
1297   if (TT.find("x86_64-") == 0)
1298     CPUType = DARWIN_CPU_TYPE_X86 | DARWIN_CPU_ARCH_ABI64;
1299   else if (TT.size() >= 5 && TT[0] == 'i' && TT[2] == '8' && TT[3] == '6' &&
1300            TT[4] == '-' && TT[1] - '3' < 6)
1301     CPUType = DARWIN_CPU_TYPE_X86;
1302   else if (TT.find("powerpc-") == 0)
1303     CPUType = DARWIN_CPU_TYPE_POWERPC;
1304   else if (TT.find("powerpc64-") == 0)
1305     CPUType = DARWIN_CPU_TYPE_POWERPC | DARWIN_CPU_ARCH_ABI64;
1306   
1307   // Traditional Bitcode starts after header.
1308   unsigned BCOffset = DarwinBCHeaderSize;
1309   
1310   Stream.Emit(0x0B17C0DE, 32);
1311   Stream.Emit(0         , 32);  // Version.
1312   Stream.Emit(BCOffset  , 32);
1313   Stream.Emit(0         , 32);  // Filled in later.
1314   Stream.Emit(CPUType   , 32);
1315 }
1316
1317 /// EmitDarwinBCTrailer - Emit the darwin epilog after the bitcode file and
1318 /// finalize the header.
1319 static void EmitDarwinBCTrailer(BitstreamWriter &Stream, unsigned BufferSize) {
1320   // Update the size field in the header.
1321   Stream.BackpatchWord(DarwinBCSizeFieldOffset, BufferSize-DarwinBCHeaderSize);
1322   
1323   // If the file is not a multiple of 16 bytes, insert dummy padding.
1324   while (BufferSize & 15) {
1325     Stream.Emit(0, 8);
1326     ++BufferSize;
1327   }
1328 }
1329
1330
1331 /// WriteBitcodeToFile - Write the specified module to the specified output
1332 /// stream.
1333 void llvm::WriteBitcodeToFile(const Module *M, std::ostream &Out) {
1334   raw_os_ostream RawOut(Out);
1335   // If writing to stdout, set binary mode.
1336   if (llvm::cout == Out)
1337     sys::Program::ChangeStdoutToBinary();
1338   WriteBitcodeToFile(M, RawOut);
1339 }
1340
1341 /// WriteBitcodeToFile - Write the specified module to the specified output
1342 /// stream.
1343 void llvm::WriteBitcodeToFile(const Module *M, raw_ostream &Out) {
1344   std::vector<unsigned char> Buffer;
1345   BitstreamWriter Stream(Buffer);
1346   
1347   Buffer.reserve(256*1024);
1348   
1349   // If this is darwin, emit a file header and trailer if needed.
1350   bool isDarwin = M->getTargetTriple().find("-darwin") != std::string::npos;
1351   if (isDarwin)
1352     EmitDarwinBCHeader(Stream, M->getTargetTriple());
1353   
1354   // Emit the file header.
1355   Stream.Emit((unsigned)'B', 8);
1356   Stream.Emit((unsigned)'C', 8);
1357   Stream.Emit(0x0, 4);
1358   Stream.Emit(0xC, 4);
1359   Stream.Emit(0xE, 4);
1360   Stream.Emit(0xD, 4);
1361
1362   // Emit the module.
1363   WriteModule(M, Stream);
1364
1365   if (isDarwin)
1366     EmitDarwinBCTrailer(Stream, Buffer.size());
1367
1368   
1369   // If writing to stdout, set binary mode.
1370   if (&llvm::outs() == &Out)
1371     sys::Program::ChangeStdoutToBinary();
1372
1373   // Write the generated bitstream to "Out".
1374   Out.write((char*)&Buffer.front(), Buffer.size());
1375   
1376   // Make sure it hits disk now.
1377   Out.flush();
1378 }