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