shave another 150K off of kc++, by using a 7-bit encoding for BB names
[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 was developed by Chris Lattner and is distributed under
6 // the University of Illinois Open Source 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/Instructions.h"
21 #include "llvm/Module.h"
22 #include "llvm/ParameterAttributes.h"
23 #include "llvm/TypeSymbolTable.h"
24 #include "llvm/ValueSymbolTable.h"
25 #include "llvm/Support/MathExtras.h"
26 using namespace llvm;
27
28 /// These are manifest constants used by the bitcode writer. They do not need to
29 /// be kept in sync with the reader, but need to be consistent within this file.
30 enum {
31   CurVersion = 0,
32   
33   // VALUE_SYMTAB_BLOCK abbrev id's.
34   VST_ENTRY_8_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
35   VST_ENTRY_7_ABBREV,
36   VST_BBENTRY_7_ABBREV
37   
38 };
39
40
41 static unsigned GetEncodedCastOpcode(unsigned Opcode) {
42   switch (Opcode) {
43   default: assert(0 && "Unknown cast instruction!");
44   case Instruction::Trunc   : return bitc::CAST_TRUNC;
45   case Instruction::ZExt    : return bitc::CAST_ZEXT;
46   case Instruction::SExt    : return bitc::CAST_SEXT;
47   case Instruction::FPToUI  : return bitc::CAST_FPTOUI;
48   case Instruction::FPToSI  : return bitc::CAST_FPTOSI;
49   case Instruction::UIToFP  : return bitc::CAST_UITOFP;
50   case Instruction::SIToFP  : return bitc::CAST_SITOFP;
51   case Instruction::FPTrunc : return bitc::CAST_FPTRUNC;
52   case Instruction::FPExt   : return bitc::CAST_FPEXT;
53   case Instruction::PtrToInt: return bitc::CAST_PTRTOINT;
54   case Instruction::IntToPtr: return bitc::CAST_INTTOPTR;
55   case Instruction::BitCast : return bitc::CAST_BITCAST;
56   }
57 }
58
59 static unsigned GetEncodedBinaryOpcode(unsigned Opcode) {
60   switch (Opcode) {
61   default: assert(0 && "Unknown binary instruction!");
62   case Instruction::Add:  return bitc::BINOP_ADD;
63   case Instruction::Sub:  return bitc::BINOP_SUB;
64   case Instruction::Mul:  return bitc::BINOP_MUL;
65   case Instruction::UDiv: return bitc::BINOP_UDIV;
66   case Instruction::FDiv:
67   case Instruction::SDiv: return bitc::BINOP_SDIV;
68   case Instruction::URem: return bitc::BINOP_UREM;
69   case Instruction::FRem:
70   case Instruction::SRem: return bitc::BINOP_SREM;
71   case Instruction::Shl:  return bitc::BINOP_SHL;
72   case Instruction::LShr: return bitc::BINOP_LSHR;
73   case Instruction::AShr: return bitc::BINOP_ASHR;
74   case Instruction::And:  return bitc::BINOP_AND;
75   case Instruction::Or:   return bitc::BINOP_OR;
76   case Instruction::Xor:  return bitc::BINOP_XOR;
77   }
78 }
79
80
81
82 static void WriteStringRecord(unsigned Code, const std::string &Str, 
83                               unsigned AbbrevToUse, BitstreamWriter &Stream) {
84   SmallVector<unsigned, 64> Vals;
85   
86   // Code: [strchar x N]
87   for (unsigned i = 0, e = Str.size(); i != e; ++i)
88     Vals.push_back(Str[i]);
89     
90   // Emit the finished record.
91   Stream.EmitRecord(Code, Vals, AbbrevToUse);
92 }
93
94 // Emit information about parameter attributes.
95 static void WriteParamAttrTable(const ValueEnumerator &VE, 
96                                 BitstreamWriter &Stream) {
97   const std::vector<const ParamAttrsList*> &Attrs = VE.getParamAttrs();
98   if (Attrs.empty()) return;
99   
100   Stream.EnterSubblock(bitc::PARAMATTR_BLOCK_ID, 3);
101
102   SmallVector<uint64_t, 64> Record;
103   for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
104     const ParamAttrsList *A = Attrs[i];
105     for (unsigned op = 0, e = A->size(); op != e; ++op) {
106       Record.push_back(A->getParamIndex(op));
107       Record.push_back(A->getParamAttrsAtIndex(op));
108     }
109     
110     Stream.EmitRecord(bitc::PARAMATTR_CODE_ENTRY, Record);
111     Record.clear();
112   }
113   
114   Stream.ExitBlock();
115 }
116
117 /// WriteTypeTable - Write out the type table for a module.
118 static void WriteTypeTable(const ValueEnumerator &VE, BitstreamWriter &Stream) {
119   const ValueEnumerator::TypeList &TypeList = VE.getTypes();
120   
121   Stream.EnterSubblock(bitc::TYPE_BLOCK_ID, 4 /*count from # abbrevs */);
122   SmallVector<uint64_t, 64> TypeVals;
123   
124   // FIXME: Set up abbrevs now that we know the width of the type fields, etc.
125   
126   // Emit an entry count so the reader can reserve space.
127   TypeVals.push_back(TypeList.size());
128   Stream.EmitRecord(bitc::TYPE_CODE_NUMENTRY, TypeVals);
129   TypeVals.clear();
130   
131   // Loop over all of the types, emitting each in turn.
132   for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
133     const Type *T = TypeList[i].first;
134     int AbbrevToUse = 0;
135     unsigned Code = 0;
136     
137     switch (T->getTypeID()) {
138     case Type::PackedStructTyID: // FIXME: Delete Type::PackedStructTyID.
139     default: assert(0 && "Unknown type!");
140     case Type::VoidTyID:   Code = bitc::TYPE_CODE_VOID;   break;
141     case Type::FloatTyID:  Code = bitc::TYPE_CODE_FLOAT;  break;
142     case Type::DoubleTyID: Code = bitc::TYPE_CODE_DOUBLE; break;
143     case Type::LabelTyID:  Code = bitc::TYPE_CODE_LABEL;  break;
144     case Type::OpaqueTyID: Code = bitc::TYPE_CODE_OPAQUE; break;
145     case Type::IntegerTyID:
146       // INTEGER: [width]
147       Code = bitc::TYPE_CODE_INTEGER;
148       TypeVals.push_back(cast<IntegerType>(T)->getBitWidth());
149       break;
150     case Type::PointerTyID:
151       // POINTER: [pointee type]
152       Code = bitc::TYPE_CODE_POINTER;
153       TypeVals.push_back(VE.getTypeID(cast<PointerType>(T)->getElementType()));
154       break;
155
156     case Type::FunctionTyID: {
157       const FunctionType *FT = cast<FunctionType>(T);
158       // FUNCTION: [isvararg, attrid, #pararms, paramty x N]
159       Code = bitc::TYPE_CODE_FUNCTION;
160       TypeVals.push_back(FT->isVarArg());
161       TypeVals.push_back(VE.getParamAttrID(FT->getParamAttrs()));
162       TypeVals.push_back(VE.getTypeID(FT->getReturnType()));
163       for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i)
164         TypeVals.push_back(VE.getTypeID(FT->getParamType(i)));
165       break;
166     }
167     case Type::StructTyID: {
168       const StructType *ST = cast<StructType>(T);
169       // STRUCT: [ispacked, #elts, eltty x N]
170       Code = bitc::TYPE_CODE_STRUCT;
171       TypeVals.push_back(ST->isPacked());
172       // Output all of the element types.
173       for (StructType::element_iterator I = ST->element_begin(),
174            E = ST->element_end(); I != E; ++I)
175         TypeVals.push_back(VE.getTypeID(*I));
176       break;
177     }
178     case Type::ArrayTyID: {
179       const ArrayType *AT = cast<ArrayType>(T);
180       // ARRAY: [numelts, eltty]
181       Code = bitc::TYPE_CODE_ARRAY;
182       TypeVals.push_back(AT->getNumElements());
183       TypeVals.push_back(VE.getTypeID(AT->getElementType()));
184       break;
185     }
186     case Type::VectorTyID: {
187       const VectorType *VT = cast<VectorType>(T);
188       // VECTOR [numelts, eltty]
189       Code = bitc::TYPE_CODE_VECTOR;
190       TypeVals.push_back(VT->getNumElements());
191       TypeVals.push_back(VE.getTypeID(VT->getElementType()));
192       break;
193     }
194     }
195
196     // Emit the finished record.
197     Stream.EmitRecord(Code, TypeVals, AbbrevToUse);
198     TypeVals.clear();
199   }
200   
201   Stream.ExitBlock();
202 }
203
204 static unsigned getEncodedLinkage(const GlobalValue *GV) {
205   switch (GV->getLinkage()) {
206   default: assert(0 && "Invalid linkage!");
207   case GlobalValue::ExternalLinkage:     return 0;
208   case GlobalValue::WeakLinkage:         return 1;
209   case GlobalValue::AppendingLinkage:    return 2;
210   case GlobalValue::InternalLinkage:     return 3;
211   case GlobalValue::LinkOnceLinkage:     return 4;
212   case GlobalValue::DLLImportLinkage:    return 5;
213   case GlobalValue::DLLExportLinkage:    return 6;
214   case GlobalValue::ExternalWeakLinkage: return 7;
215   }
216 }
217
218 static unsigned getEncodedVisibility(const GlobalValue *GV) {
219   switch (GV->getVisibility()) {
220   default: assert(0 && "Invalid visibility!");
221   case GlobalValue::DefaultVisibility:   return 0;
222   case GlobalValue::HiddenVisibility:    return 1;
223   case GlobalValue::ProtectedVisibility: return 2;
224   }
225 }
226
227 // Emit top-level description of module, including target triple, inline asm,
228 // descriptors for global variables, and function prototype info.
229 static void WriteModuleInfo(const Module *M, const ValueEnumerator &VE,
230                             BitstreamWriter &Stream) {
231   // Emit the list of dependent libraries for the Module.
232   for (Module::lib_iterator I = M->lib_begin(), E = M->lib_end(); I != E; ++I)
233     WriteStringRecord(bitc::MODULE_CODE_DEPLIB, *I, 0/*TODO*/, Stream);
234
235   // Emit various pieces of data attached to a module.
236   if (!M->getTargetTriple().empty())
237     WriteStringRecord(bitc::MODULE_CODE_TRIPLE, M->getTargetTriple(),
238                       0/*TODO*/, Stream);
239   if (!M->getDataLayout().empty())
240     WriteStringRecord(bitc::MODULE_CODE_DATALAYOUT, M->getDataLayout(),
241                       0/*TODO*/, Stream);
242   if (!M->getModuleInlineAsm().empty())
243     WriteStringRecord(bitc::MODULE_CODE_ASM, M->getModuleInlineAsm(),
244                       0/*TODO*/, Stream);
245
246   // Emit information about sections, computing how many there are.  Also
247   // compute the maximum alignment value.
248   std::map<std::string, unsigned> SectionMap;
249   unsigned MaxAlignment = 0;
250   unsigned MaxGlobalType = 0;
251   for (Module::const_global_iterator GV = M->global_begin(),E = M->global_end();
252        GV != E; ++GV) {
253     MaxAlignment = std::max(MaxAlignment, GV->getAlignment());
254     MaxGlobalType = std::max(MaxGlobalType, VE.getTypeID(GV->getType()));
255     
256     if (!GV->hasSection()) continue;
257     // Give section names unique ID's.
258     unsigned &Entry = SectionMap[GV->getSection()];
259     if (Entry != 0) continue;
260     WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, GV->getSection(),
261                       0/*TODO*/, Stream);
262     Entry = SectionMap.size();
263   }
264   for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
265     MaxAlignment = std::max(MaxAlignment, F->getAlignment());
266     if (!F->hasSection()) continue;
267     // Give section names unique ID's.
268     unsigned &Entry = SectionMap[F->getSection()];
269     if (Entry != 0) continue;
270     WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, F->getSection(),
271                       0/*TODO*/, Stream);
272     Entry = SectionMap.size();
273   }
274   
275   // Emit abbrev for globals, now that we know # sections and max alignment.
276   unsigned SimpleGVarAbbrev = 0;
277   if (!M->global_empty()) { 
278     // Add an abbrev for common globals with no visibility or thread localness.
279     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
280     Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_GLOBALVAR));
281     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
282                               Log2_32_Ceil(MaxGlobalType+1)));
283     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));      // Constant.
284     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));        // Initializer.
285     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3));      // Linkage.
286     if (MaxAlignment == 0)                                      // Alignment.
287       Abbv->Add(BitCodeAbbrevOp(0));
288     else {
289       unsigned MaxEncAlignment = Log2_32(MaxAlignment)+1;
290       Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
291                                Log2_32_Ceil(MaxEncAlignment+1)));
292     }
293     if (SectionMap.empty())                                    // Section.
294       Abbv->Add(BitCodeAbbrevOp(0));
295     else
296       Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
297                                Log2_32_Ceil(SectionMap.size()+1)));
298     // Don't bother emitting vis + thread local.
299     SimpleGVarAbbrev = Stream.EmitAbbrev(Abbv);
300   }
301   
302   // Emit the global variable information.
303   SmallVector<unsigned, 64> Vals;
304   for (Module::const_global_iterator GV = M->global_begin(),E = M->global_end();
305        GV != E; ++GV) {
306     unsigned AbbrevToUse = 0;
307
308     // GLOBALVAR: [type, isconst, initid, 
309     //             linkage, alignment, section, visibility, threadlocal]
310     Vals.push_back(VE.getTypeID(GV->getType()));
311     Vals.push_back(GV->isConstant());
312     Vals.push_back(GV->isDeclaration() ? 0 :
313                    (VE.getValueID(GV->getInitializer()) + 1));
314     Vals.push_back(getEncodedLinkage(GV));
315     Vals.push_back(Log2_32(GV->getAlignment())+1);
316     Vals.push_back(GV->hasSection() ? SectionMap[GV->getSection()] : 0);
317     if (GV->isThreadLocal() || 
318         GV->getVisibility() != GlobalValue::DefaultVisibility) {
319       Vals.push_back(getEncodedVisibility(GV));
320       Vals.push_back(GV->isThreadLocal());
321     } else {
322       AbbrevToUse = SimpleGVarAbbrev;
323     }
324     
325     Stream.EmitRecord(bitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse);
326     Vals.clear();
327   }
328
329   // Emit the function proto information.
330   for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
331     // FUNCTION:  [type, callingconv, isproto, linkage, alignment, section,
332     //             visibility]
333     Vals.push_back(VE.getTypeID(F->getType()));
334     Vals.push_back(F->getCallingConv());
335     Vals.push_back(F->isDeclaration());
336     Vals.push_back(getEncodedLinkage(F));
337     Vals.push_back(Log2_32(F->getAlignment())+1);
338     Vals.push_back(F->hasSection() ? SectionMap[F->getSection()] : 0);
339     Vals.push_back(getEncodedVisibility(F));
340     
341     unsigned AbbrevToUse = 0;
342     Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse);
343     Vals.clear();
344   }
345   
346   
347   // Emit the alias information.
348   for (Module::const_alias_iterator AI = M->alias_begin(), E = M->alias_end();
349        AI != E; ++AI) {
350     Vals.push_back(VE.getTypeID(AI->getType()));
351     Vals.push_back(VE.getValueID(AI->getAliasee()));
352     Vals.push_back(getEncodedLinkage(AI));
353     unsigned AbbrevToUse = 0;
354     Stream.EmitRecord(bitc::MODULE_CODE_ALIAS, Vals, AbbrevToUse);
355     Vals.clear();
356   }
357 }
358
359
360 static void WriteConstants(unsigned FirstVal, unsigned LastVal,
361                            const ValueEnumerator &VE,
362                            BitstreamWriter &Stream) {
363   if (FirstVal == LastVal) return;
364   
365   Stream.EnterSubblock(bitc::CONSTANTS_BLOCK_ID, 2);
366
367   // FIXME: Install and use abbrevs to reduce size.  Install them globally so
368   // they don't need to be reemitted for each function body.
369   
370   SmallVector<uint64_t, 64> Record;
371
372   const ValueEnumerator::ValueList &Vals = VE.getValues();
373   const Type *LastTy = 0;
374   for (unsigned i = FirstVal; i != LastVal; ++i) {
375     const Value *V = Vals[i].first;
376     // If we need to switch types, do so now.
377     if (V->getType() != LastTy) {
378       LastTy = V->getType();
379       Record.push_back(VE.getTypeID(LastTy));
380       Stream.EmitRecord(bitc::CST_CODE_SETTYPE, Record);
381       Record.clear();
382     }
383     
384     if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
385       assert(0 && IA && "FIXME: Inline asm writing unimp!");
386       continue;
387     }
388     const Constant *C = cast<Constant>(V);
389     unsigned Code = -1U;
390     unsigned AbbrevToUse = 0;
391     if (C->isNullValue()) {
392       Code = bitc::CST_CODE_NULL;
393     } else if (isa<UndefValue>(C)) {
394       Code = bitc::CST_CODE_UNDEF;
395     } else if (const ConstantInt *IV = dyn_cast<ConstantInt>(C)) {
396       if (IV->getBitWidth() <= 64) {
397         int64_t V = IV->getSExtValue();
398         if (V >= 0)
399           Record.push_back(V << 1);
400         else
401           Record.push_back((-V << 1) | 1);
402         Code = bitc::CST_CODE_INTEGER;
403       } else {                             // Wide integers, > 64 bits in size.
404         // We have an arbitrary precision integer value to write whose 
405         // bit width is > 64. However, in canonical unsigned integer 
406         // format it is likely that the high bits are going to be zero.
407         // So, we only write the number of active words.
408         unsigned NWords = IV->getValue().getActiveWords(); 
409         const uint64_t *RawWords = IV->getValue().getRawData();
410         for (unsigned i = 0; i != NWords; ++i) {
411           int64_t V = RawWords[i];
412           if (V >= 0)
413             Record.push_back(V << 1);
414           else
415             Record.push_back((-V << 1) | 1);
416         }
417         Code = bitc::CST_CODE_WIDE_INTEGER;
418       }
419     } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
420       Code = bitc::CST_CODE_FLOAT;
421       if (CFP->getType() == Type::FloatTy) {
422         Record.push_back(FloatToBits((float)CFP->getValue()));
423       } else {
424         assert (CFP->getType() == Type::DoubleTy && "Unknown FP type!");
425         Record.push_back(DoubleToBits((double)CFP->getValue()));
426       }
427     } else if (isa<ConstantArray>(C) || isa<ConstantStruct>(V) ||
428                isa<ConstantVector>(V)) {
429       Code = bitc::CST_CODE_AGGREGATE;
430       for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
431         Record.push_back(VE.getValueID(C->getOperand(i)));
432     } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
433       switch (CE->getOpcode()) {
434       default:
435         if (Instruction::isCast(CE->getOpcode())) {
436           Code = bitc::CST_CODE_CE_CAST;
437           Record.push_back(GetEncodedCastOpcode(CE->getOpcode()));
438           Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
439           Record.push_back(VE.getValueID(C->getOperand(0)));
440         } else {
441           assert(CE->getNumOperands() == 2 && "Unknown constant expr!");
442           Code = bitc::CST_CODE_CE_BINOP;
443           Record.push_back(GetEncodedBinaryOpcode(CE->getOpcode()));
444           Record.push_back(VE.getValueID(C->getOperand(0)));
445           Record.push_back(VE.getValueID(C->getOperand(1)));
446         }
447         break;
448       case Instruction::GetElementPtr:
449         Code = bitc::CST_CODE_CE_GEP;
450         for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) {
451           Record.push_back(VE.getTypeID(C->getOperand(i)->getType()));
452           Record.push_back(VE.getValueID(C->getOperand(i)));
453         }
454         break;
455       case Instruction::Select:
456         Code = bitc::CST_CODE_CE_SELECT;
457         Record.push_back(VE.getValueID(C->getOperand(0)));
458         Record.push_back(VE.getValueID(C->getOperand(1)));
459         Record.push_back(VE.getValueID(C->getOperand(2)));
460         break;
461       case Instruction::ExtractElement:
462         Code = bitc::CST_CODE_CE_EXTRACTELT;
463         Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
464         Record.push_back(VE.getValueID(C->getOperand(0)));
465         Record.push_back(VE.getValueID(C->getOperand(1)));
466         break;
467       case Instruction::InsertElement:
468         Code = bitc::CST_CODE_CE_INSERTELT;
469         Record.push_back(VE.getValueID(C->getOperand(0)));
470         Record.push_back(VE.getValueID(C->getOperand(1)));
471         Record.push_back(VE.getValueID(C->getOperand(2)));
472         break;
473       case Instruction::ShuffleVector:
474         Code = bitc::CST_CODE_CE_SHUFFLEVEC;
475         Record.push_back(VE.getValueID(C->getOperand(0)));
476         Record.push_back(VE.getValueID(C->getOperand(1)));
477         Record.push_back(VE.getValueID(C->getOperand(2)));
478         break;
479       case Instruction::ICmp:
480       case Instruction::FCmp:
481         Code = bitc::CST_CODE_CE_CMP;
482         Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
483         Record.push_back(VE.getValueID(C->getOperand(0)));
484         Record.push_back(VE.getValueID(C->getOperand(1)));
485         Record.push_back(CE->getPredicate());
486         break;
487       }
488     } else {
489       assert(0 && "Unknown constant!");
490     }
491     Stream.EmitRecord(Code, Record, AbbrevToUse);
492     Record.clear();
493   }
494
495   Stream.ExitBlock();
496 }
497
498 static void WriteModuleConstants(const ValueEnumerator &VE,
499                                  BitstreamWriter &Stream) {
500   const ValueEnumerator::ValueList &Vals = VE.getValues();
501   
502   // Find the first constant to emit, which is the first non-globalvalue value.
503   // We know globalvalues have been emitted by WriteModuleInfo.
504   for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
505     if (!isa<GlobalValue>(Vals[i].first)) {
506       WriteConstants(i, Vals.size(), VE, Stream);
507       return;
508     }
509   }
510 }
511
512 /// WriteInstruction - Emit an instruction to the specified stream.
513 static void WriteInstruction(const Instruction &I, ValueEnumerator &VE, 
514                              BitstreamWriter &Stream,
515                              SmallVector<unsigned, 64> &Vals) {
516   unsigned Code = 0;
517   unsigned AbbrevToUse = 0;
518   switch (I.getOpcode()) {
519   default:
520     if (Instruction::isCast(I.getOpcode())) {
521       Code = bitc::FUNC_CODE_INST_CAST;
522       Vals.push_back(GetEncodedCastOpcode(I.getOpcode()));
523       Vals.push_back(VE.getTypeID(I.getType()));
524       Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
525       Vals.push_back(VE.getValueID(I.getOperand(0)));
526     } else {
527       assert(isa<BinaryOperator>(I) && "Unknown instruction!");
528       Code = bitc::FUNC_CODE_INST_BINOP;
529       Vals.push_back(GetEncodedBinaryOpcode(I.getOpcode()));
530       Vals.push_back(VE.getTypeID(I.getType()));
531       Vals.push_back(VE.getValueID(I.getOperand(0)));
532       Vals.push_back(VE.getValueID(I.getOperand(1)));
533     }
534     break;
535
536   case Instruction::GetElementPtr:
537     Code = bitc::FUNC_CODE_INST_GEP;
538     for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
539       Vals.push_back(VE.getTypeID(I.getOperand(i)->getType()));
540       Vals.push_back(VE.getValueID(I.getOperand(i)));
541     }
542     break;
543   case Instruction::Select:
544     Code = bitc::FUNC_CODE_INST_SELECT;
545     Vals.push_back(VE.getTypeID(I.getType()));
546     Vals.push_back(VE.getValueID(I.getOperand(0)));
547     Vals.push_back(VE.getValueID(I.getOperand(1)));
548     Vals.push_back(VE.getValueID(I.getOperand(2)));
549     break;
550   case Instruction::ExtractElement:
551     Code = bitc::FUNC_CODE_INST_EXTRACTELT;
552     Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
553     Vals.push_back(VE.getValueID(I.getOperand(0)));
554     Vals.push_back(VE.getValueID(I.getOperand(1)));
555     break;
556   case Instruction::InsertElement:
557     Code = bitc::FUNC_CODE_INST_INSERTELT;
558     Vals.push_back(VE.getTypeID(I.getType()));
559     Vals.push_back(VE.getValueID(I.getOperand(0)));
560     Vals.push_back(VE.getValueID(I.getOperand(1)));
561     Vals.push_back(VE.getValueID(I.getOperand(2)));
562     break;
563   case Instruction::ShuffleVector:
564     Code = bitc::FUNC_CODE_INST_SHUFFLEVEC;
565     Vals.push_back(VE.getTypeID(I.getType()));
566     Vals.push_back(VE.getValueID(I.getOperand(0)));
567     Vals.push_back(VE.getValueID(I.getOperand(1)));
568     Vals.push_back(VE.getValueID(I.getOperand(2)));
569     break;
570   case Instruction::ICmp:
571   case Instruction::FCmp:
572     Code = bitc::FUNC_CODE_INST_CMP;
573     Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
574     Vals.push_back(VE.getValueID(I.getOperand(0)));
575     Vals.push_back(VE.getValueID(I.getOperand(1)));
576     Vals.push_back(cast<CmpInst>(I).getPredicate());
577     break;
578
579   case Instruction::Ret:
580     Code = bitc::FUNC_CODE_INST_RET;
581     if (I.getNumOperands()) {
582       Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
583       Vals.push_back(VE.getValueID(I.getOperand(0)));
584     }
585     break;
586   case Instruction::Br:
587     Code = bitc::FUNC_CODE_INST_BR;
588     Vals.push_back(VE.getValueID(I.getOperand(0)));
589     if (cast<BranchInst>(I).isConditional()) {
590       Vals.push_back(VE.getValueID(I.getOperand(1)));
591       Vals.push_back(VE.getValueID(I.getOperand(2)));
592     }
593     break;
594   case Instruction::Switch:
595     Code = bitc::FUNC_CODE_INST_SWITCH;
596     Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
597     for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
598       Vals.push_back(VE.getValueID(I.getOperand(i)));
599     break;
600   case Instruction::Invoke: {
601     Code = bitc::FUNC_CODE_INST_INVOKE;
602     Vals.push_back(cast<InvokeInst>(I).getCallingConv());
603     Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
604     Vals.push_back(VE.getValueID(I.getOperand(0)));  // callee
605     Vals.push_back(VE.getValueID(I.getOperand(1)));  // normal
606     Vals.push_back(VE.getValueID(I.getOperand(2)));  // unwind
607     
608     // Emit value #'s for the fixed parameters.
609     const PointerType *PTy = cast<PointerType>(I.getOperand(0)->getType());
610     const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
611     for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
612       Vals.push_back(VE.getValueID(I.getOperand(i+3)));  // fixed param.
613
614     // Emit type/value pairs for varargs params.
615     if (FTy->isVarArg()) {
616       unsigned NumVarargs = I.getNumOperands()-3-FTy->getNumParams();
617       for (unsigned i = I.getNumOperands()-NumVarargs, e = I.getNumOperands();
618            i != e; ++i) {
619         Vals.push_back(VE.getTypeID(I.getOperand(i)->getType()));
620         Vals.push_back(VE.getValueID(I.getOperand(i)));
621       }
622     }
623     break;
624   }
625   case Instruction::Unwind:
626     Code = bitc::FUNC_CODE_INST_UNWIND;
627     break;
628   case Instruction::Unreachable:
629     Code = bitc::FUNC_CODE_INST_UNREACHABLE;
630     break;
631   
632   case Instruction::PHI:
633     Code = bitc::FUNC_CODE_INST_PHI;
634     Vals.push_back(VE.getTypeID(I.getType()));
635     for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
636       Vals.push_back(VE.getValueID(I.getOperand(i)));
637     break;
638     
639   case Instruction::Malloc:
640     Code = bitc::FUNC_CODE_INST_MALLOC;
641     Vals.push_back(VE.getTypeID(I.getType()));
642     Vals.push_back(VE.getValueID(I.getOperand(0))); // size.
643     Vals.push_back(Log2_32(cast<MallocInst>(I).getAlignment())+1);
644     break;
645     
646   case Instruction::Free:
647     Code = bitc::FUNC_CODE_INST_FREE;
648     Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
649     Vals.push_back(VE.getValueID(I.getOperand(0)));
650     break;
651     
652   case Instruction::Alloca:
653     Code = bitc::FUNC_CODE_INST_ALLOCA;
654     Vals.push_back(VE.getTypeID(I.getType()));
655     Vals.push_back(VE.getValueID(I.getOperand(0))); // size.
656     Vals.push_back(Log2_32(cast<AllocaInst>(I).getAlignment())+1);
657     break;
658     
659   case Instruction::Load:
660     Code = bitc::FUNC_CODE_INST_LOAD;
661     Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
662     Vals.push_back(VE.getValueID(I.getOperand(0))); // ptr.
663     Vals.push_back(Log2_32(cast<LoadInst>(I).getAlignment())+1);
664     Vals.push_back(cast<LoadInst>(I).isVolatile());
665     break;
666   case Instruction::Store:
667     Code = bitc::FUNC_CODE_INST_STORE;
668     Vals.push_back(VE.getTypeID(I.getOperand(1)->getType()));   // Pointer
669     Vals.push_back(VE.getValueID(I.getOperand(0))); // val.
670     Vals.push_back(VE.getValueID(I.getOperand(1))); // ptr.
671     Vals.push_back(Log2_32(cast<StoreInst>(I).getAlignment())+1);
672     Vals.push_back(cast<StoreInst>(I).isVolatile());
673     break;
674   case Instruction::Call: {
675     Code = bitc::FUNC_CODE_INST_CALL;
676     Vals.push_back((cast<CallInst>(I).getCallingConv() << 1) |
677                    cast<CallInst>(I).isTailCall());
678     Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
679     Vals.push_back(VE.getValueID(I.getOperand(0)));  // callee
680     
681     // Emit value #'s for the fixed parameters.
682     const PointerType *PTy = cast<PointerType>(I.getOperand(0)->getType());
683     const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
684     for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
685       Vals.push_back(VE.getValueID(I.getOperand(i+1)));  // fixed param.
686       
687     // Emit type/value pairs for varargs params.
688     if (FTy->isVarArg()) {
689       unsigned NumVarargs = I.getNumOperands()-1-FTy->getNumParams();
690       for (unsigned i = I.getNumOperands()-NumVarargs, e = I.getNumOperands();
691            i != e; ++i) {
692         Vals.push_back(VE.getTypeID(I.getOperand(i)->getType()));
693         Vals.push_back(VE.getValueID(I.getOperand(i)));
694       }
695     }
696     break;
697   }
698   case Instruction::VAArg:
699     Code = bitc::FUNC_CODE_INST_VAARG;
700     Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));   // valistty
701     Vals.push_back(VE.getValueID(I.getOperand(0))); // valist.
702     Vals.push_back(VE.getTypeID(I.getType())); // restype.
703     break;
704   }
705   
706   Stream.EmitRecord(Code, Vals, AbbrevToUse);
707   Vals.clear();
708 }
709
710 // Emit names for globals/functions etc.
711 static void WriteValueSymbolTable(const ValueSymbolTable &VST,
712                                   const ValueEnumerator &VE,
713                                   BitstreamWriter &Stream) {
714   if (VST.empty()) return;
715   Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 3);
716
717   { // 8-bit fixed width VST_ENTRY strings.
718     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
719     Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
720     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
721     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
722     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
723     if (Stream.EmitAbbrev(Abbv) != VST_ENTRY_8_ABBREV)
724       assert(0 && "Unexpected abbrev ordering!");
725   }
726   
727   { // 7-bit fixed width VST_ENTRY strings.
728     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
729     Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
730     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
731     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
732     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
733     if (Stream.EmitAbbrev(Abbv) != VST_ENTRY_7_ABBREV)
734       assert(0 && "Unexpected abbrev ordering!");
735   }
736   { // 7-bit fixed width VST_BBENTRY strings.
737     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
738     Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_BBENTRY));
739     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
740     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
741     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
742     if (Stream.EmitAbbrev(Abbv) != VST_BBENTRY_7_ABBREV)
743       assert(0 && "Unexpected abbrev ordering!");
744   }
745   
746   
747   // FIXME: Set up the abbrev, we know how many values there are!
748   // FIXME: We know if the type names can use 7-bit ascii.
749   SmallVector<unsigned, 64> NameVals;
750   
751   for (ValueSymbolTable::const_iterator SI = VST.begin(), SE = VST.end();
752        SI != SE; ++SI) {
753     
754     const ValueName &Name = *SI;
755     
756     // Figure out the encoding to use for the name.
757     bool is7Bit = true;
758     for (unsigned i = 0, e = Name.getKeyLength(); i != e; ++i)
759       if ((unsigned char)Name.getKeyData()[i] & 128) {
760         is7Bit = false;
761         break;
762       }
763     
764     
765     unsigned AbbrevToUse = 0;
766     
767     // VST_ENTRY:   [valueid, namelen, namechar x N]
768     // VST_BBENTRY: [bbid, namelen, namechar x N]
769     unsigned Code;
770     if (isa<BasicBlock>(SI->getValue())) {
771       Code = bitc::VST_CODE_BBENTRY;
772       if (is7Bit) AbbrevToUse = VST_BBENTRY_7_ABBREV;
773     } else {
774       Code = bitc::VST_CODE_ENTRY;
775       AbbrevToUse = is7Bit ? VST_ENTRY_7_ABBREV : VST_ENTRY_8_ABBREV;
776     }
777     
778     NameVals.push_back(VE.getValueID(SI->getValue()));
779     for (const char *P = Name.getKeyData(),
780          *E = Name.getKeyData()+Name.getKeyLength(); P != E; ++P)
781       NameVals.push_back((unsigned char)*P);
782     
783     // Emit the finished record.
784     Stream.EmitRecord(Code, NameVals, AbbrevToUse);
785     NameVals.clear();
786   }
787   Stream.ExitBlock();
788 }
789
790 /// WriteFunction - Emit a function body to the module stream.
791 static void WriteFunction(const Function &F, ValueEnumerator &VE, 
792                           BitstreamWriter &Stream) {
793   Stream.EnterSubblock(bitc::FUNCTION_BLOCK_ID, 3);
794   VE.incorporateFunction(F);
795
796   SmallVector<unsigned, 64> Vals;
797   
798   // Emit the number of basic blocks, so the reader can create them ahead of
799   // time.
800   Vals.push_back(VE.getBasicBlocks().size());
801   Stream.EmitRecord(bitc::FUNC_CODE_DECLAREBLOCKS, Vals);
802   Vals.clear();
803   
804   // FIXME: Function attributes?
805   
806   // If there are function-local constants, emit them now.
807   unsigned CstStart, CstEnd;
808   VE.getFunctionConstantRange(CstStart, CstEnd);
809   WriteConstants(CstStart, CstEnd, VE, Stream);
810   
811   // Finally, emit all the instructions, in order.
812   for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
813     for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
814       WriteInstruction(*I, VE, Stream, Vals);
815   
816   // Emit names for all the instructions etc.
817   WriteValueSymbolTable(F.getValueSymbolTable(), VE, Stream);
818     
819   VE.purgeFunction();
820   Stream.ExitBlock();
821 }
822
823 /// WriteTypeSymbolTable - Emit a block for the specified type symtab.
824 static void WriteTypeSymbolTable(const TypeSymbolTable &TST,
825                                  const ValueEnumerator &VE,
826                                  BitstreamWriter &Stream) {
827   if (TST.empty()) return;
828   
829   Stream.EnterSubblock(bitc::TYPE_SYMTAB_BLOCK_ID, 3);
830   
831   // FIXME: Set up the abbrev, we know how many types there are!
832   // FIXME: We know if the type names can use 7-bit ascii.
833   
834   SmallVector<unsigned, 64> NameVals;
835   
836   for (TypeSymbolTable::const_iterator TI = TST.begin(), TE = TST.end(); 
837        TI != TE; ++TI) {
838     unsigned AbbrevToUse = 0;
839     
840     // TST_ENTRY: [typeid, namelen, namechar x N]
841     NameVals.push_back(VE.getTypeID(TI->second));
842     
843     const std::string &Str = TI->first;
844     for (unsigned i = 0, e = Str.size(); i != e; ++i)
845       NameVals.push_back(Str[i]);
846     
847     // Emit the finished record.
848     Stream.EmitRecord(bitc::VST_CODE_ENTRY, NameVals, AbbrevToUse);
849     NameVals.clear();
850   }
851   
852   Stream.ExitBlock();
853 }
854
855
856 /// WriteModule - Emit the specified module to the bitstream.
857 static void WriteModule(const Module *M, BitstreamWriter &Stream) {
858   Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3);
859   
860   // Emit the version number if it is non-zero.
861   if (CurVersion) {
862     SmallVector<unsigned, 1> Vals;
863     Vals.push_back(CurVersion);
864     Stream.EmitRecord(bitc::MODULE_CODE_VERSION, Vals);
865   }
866   
867   // Analyze the module, enumerating globals, functions, etc.
868   ValueEnumerator VE(M);
869   
870   // Emit information about parameter attributes.
871   WriteParamAttrTable(VE, Stream);
872   
873   // Emit information describing all of the types in the module.
874   WriteTypeTable(VE, Stream);
875   
876   // Emit top-level description of module, including target triple, inline asm,
877   // descriptors for global variables, and function prototype info.
878   WriteModuleInfo(M, VE, Stream);
879   
880   // Emit constants.
881   WriteModuleConstants(VE, Stream);
882   
883   // If we have any aggregate values in the value table, purge them - these can
884   // only be used to initialize global variables.  Doing so makes the value
885   // namespace smaller for code in functions.
886   int NumNonAggregates = VE.PurgeAggregateValues();
887   if (NumNonAggregates != -1) {
888     SmallVector<unsigned, 1> Vals;
889     Vals.push_back(NumNonAggregates);
890     Stream.EmitRecord(bitc::MODULE_CODE_PURGEVALS, Vals);
891   }
892   
893   // Emit function bodies.
894   for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
895     if (!I->isDeclaration())
896       WriteFunction(*I, VE, Stream);
897   
898   // Emit the type symbol table information.
899   WriteTypeSymbolTable(M->getTypeSymbolTable(), VE, Stream);
900   
901   // Emit names for globals/functions etc.
902   WriteValueSymbolTable(M->getValueSymbolTable(), VE, Stream);
903   
904   Stream.ExitBlock();
905 }
906
907 // Emit blockinfo, which defines the standard abbreviations etc.
908 static void WriteBlockInfo(BitstreamWriter &Stream) {
909   // We only want to emit block info records for blocks that have multiple
910   // instances: CONSTANTS_BLOCK, FUNCTION_BLOCK and VALUE_SYMTAB_BLOCK.  Other
911   // blocks can defined their abbrevs inline.
912   Stream.EnterSubblock(bitc::BLOCKINFO_BLOCK_ID, 2);
913
914 #if 0
915   // Configure TYPE_SYMTAB_BLOCK's.
916
917   // Add an abbrev for VST_ENTRY where the characters each fit in 7 bits.
918   BitCodeAbbrev *Abbv = new BitCodeAbbrev();
919   Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
920   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8); // Value ID
921   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Linkage.
922   
923   xxx = Stream.EmitAbbrev(Abbv);
924 #endif
925   Stream.ExitBlock();
926 }
927
928
929 /// WriteBitcodeToFile - Write the specified module to the specified output
930 /// stream.
931 void llvm::WriteBitcodeToFile(const Module *M, std::ostream &Out) {
932   std::vector<unsigned char> Buffer;
933   BitstreamWriter Stream(Buffer);
934   
935   Buffer.reserve(256*1024);
936   
937   // Emit the file header.
938   Stream.Emit((unsigned)'B', 8);
939   Stream.Emit((unsigned)'C', 8);
940   Stream.Emit(0x0, 4);
941   Stream.Emit(0xC, 4);
942   Stream.Emit(0xE, 4);
943   Stream.Emit(0xD, 4);
944
945   // Emit blockinfo, which defines the standard abbreviations etc.
946   WriteBlockInfo(Stream);
947   
948   // Emit the module.
949   WriteModule(M, Stream);
950   
951   // Write the generated bitstream to "Out".
952   Out.write((char*)&Buffer.front(), Buffer.size());
953 }