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