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