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