BitcodeWriter.cpp revision 7a263ea8598c03c8522d66ea8db5113d6b4d640e
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"
26using 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.
30enum {
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
41static 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
59static 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
82static 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.
95static 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.
118static 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
204static 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
218static 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.
229static 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
360static 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
498static 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.
513static 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.
711static 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  // FIXME: Set up the abbrev, we know how many values there are!
718  // FIXME: We know if the type names can use 7-bit ascii.
719  SmallVector<unsigned, 64> NameVals;
720
721  for (ValueSymbolTable::const_iterator SI = VST.begin(), SE = VST.end();
722       SI != SE; ++SI) {
723
724    const ValueName &Name = *SI;
725
726    // Figure out the encoding to use for the name.
727    bool is7Bit = true;
728    for (unsigned i = 0, e = Name.getKeyLength(); i != e; ++i)
729      if ((unsigned char)Name.getKeyData()[i] & 128) {
730        is7Bit = false;
731        break;
732      }
733
734
735    unsigned AbbrevToUse = VST_ENTRY_8_ABBREV;
736
737    // VST_ENTRY:   [valueid, namechar x N]
738    // VST_BBENTRY: [bbid, namechar x N]
739    unsigned Code;
740    if (isa<BasicBlock>(SI->getValue())) {
741      Code = bitc::VST_CODE_BBENTRY;
742      if (is7Bit) AbbrevToUse = VST_BBENTRY_7_ABBREV;
743    } else {
744      Code = bitc::VST_CODE_ENTRY;
745      if (is7Bit) AbbrevToUse = VST_ENTRY_7_ABBREV;
746    }
747
748    NameVals.push_back(VE.getValueID(SI->getValue()));
749    for (const char *P = Name.getKeyData(),
750         *E = Name.getKeyData()+Name.getKeyLength(); P != E; ++P)
751      NameVals.push_back((unsigned char)*P);
752
753    // Emit the finished record.
754    Stream.EmitRecord(Code, NameVals, AbbrevToUse);
755    NameVals.clear();
756  }
757  Stream.ExitBlock();
758}
759
760/// WriteFunction - Emit a function body to the module stream.
761static void WriteFunction(const Function &F, ValueEnumerator &VE,
762                          BitstreamWriter &Stream) {
763  Stream.EnterSubblock(bitc::FUNCTION_BLOCK_ID, 3);
764  VE.incorporateFunction(F);
765
766  SmallVector<unsigned, 64> Vals;
767
768  // Emit the number of basic blocks, so the reader can create them ahead of
769  // time.
770  Vals.push_back(VE.getBasicBlocks().size());
771  Stream.EmitRecord(bitc::FUNC_CODE_DECLAREBLOCKS, Vals);
772  Vals.clear();
773
774  // FIXME: Function attributes?
775
776  // If there are function-local constants, emit them now.
777  unsigned CstStart, CstEnd;
778  VE.getFunctionConstantRange(CstStart, CstEnd);
779  WriteConstants(CstStart, CstEnd, VE, Stream);
780
781  // Finally, emit all the instructions, in order.
782  for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
783    for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
784      WriteInstruction(*I, VE, Stream, Vals);
785
786  // Emit names for all the instructions etc.
787  WriteValueSymbolTable(F.getValueSymbolTable(), VE, Stream);
788
789  VE.purgeFunction();
790  Stream.ExitBlock();
791}
792
793/// WriteTypeSymbolTable - Emit a block for the specified type symtab.
794static void WriteTypeSymbolTable(const TypeSymbolTable &TST,
795                                 const ValueEnumerator &VE,
796                                 BitstreamWriter &Stream) {
797  if (TST.empty()) return;
798
799  Stream.EnterSubblock(bitc::TYPE_SYMTAB_BLOCK_ID, 3);
800
801  // 7-bit fixed width VST_CODE_ENTRY strings.
802  BitCodeAbbrev *Abbv = new BitCodeAbbrev();
803  Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
804  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
805                            Log2_32_Ceil(VE.getTypes().size()+1)));
806  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
807  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
808  unsigned V7Abbrev = Stream.EmitAbbrev(Abbv);
809
810  SmallVector<unsigned, 64> NameVals;
811
812  for (TypeSymbolTable::const_iterator TI = TST.begin(), TE = TST.end();
813       TI != TE; ++TI) {
814    // TST_ENTRY: [typeid, namechar x N]
815    NameVals.push_back(VE.getTypeID(TI->second));
816
817    const std::string &Str = TI->first;
818    bool is7Bit = true;
819    for (unsigned i = 0, e = Str.size(); i != e; ++i) {
820      NameVals.push_back((unsigned char)Str[i]);
821      if (Str[i] & 128)
822        is7Bit = false;
823    }
824
825    // Emit the finished record.
826    Stream.EmitRecord(bitc::VST_CODE_ENTRY, NameVals, is7Bit ? V7Abbrev : 0);
827    NameVals.clear();
828  }
829
830  Stream.ExitBlock();
831}
832
833
834/// WriteModule - Emit the specified module to the bitstream.
835static void WriteModule(const Module *M, BitstreamWriter &Stream) {
836  Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3);
837
838  // Emit the version number if it is non-zero.
839  if (CurVersion) {
840    SmallVector<unsigned, 1> Vals;
841    Vals.push_back(CurVersion);
842    Stream.EmitRecord(bitc::MODULE_CODE_VERSION, Vals);
843  }
844
845  // Analyze the module, enumerating globals, functions, etc.
846  ValueEnumerator VE(M);
847
848  // Emit information about parameter attributes.
849  WriteParamAttrTable(VE, Stream);
850
851  // Emit information describing all of the types in the module.
852  WriteTypeTable(VE, Stream);
853
854  // Emit top-level description of module, including target triple, inline asm,
855  // descriptors for global variables, and function prototype info.
856  WriteModuleInfo(M, VE, Stream);
857
858  // Emit constants.
859  WriteModuleConstants(VE, Stream);
860
861  // If we have any aggregate values in the value table, purge them - these can
862  // only be used to initialize global variables.  Doing so makes the value
863  // namespace smaller for code in functions.
864  int NumNonAggregates = VE.PurgeAggregateValues();
865  if (NumNonAggregates != -1) {
866    SmallVector<unsigned, 1> Vals;
867    Vals.push_back(NumNonAggregates);
868    Stream.EmitRecord(bitc::MODULE_CODE_PURGEVALS, Vals);
869  }
870
871  // Emit function bodies.
872  for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
873    if (!I->isDeclaration())
874      WriteFunction(*I, VE, Stream);
875
876  // Emit the type symbol table information.
877  WriteTypeSymbolTable(M->getTypeSymbolTable(), VE, Stream);
878
879  // Emit names for globals/functions etc.
880  WriteValueSymbolTable(M->getValueSymbolTable(), VE, Stream);
881
882  Stream.ExitBlock();
883}
884
885// Emit blockinfo, which defines the standard abbreviations etc.
886static void WriteBlockInfo(BitstreamWriter &Stream) {
887  // We only want to emit block info records for blocks that have multiple
888  // instances: CONSTANTS_BLOCK, FUNCTION_BLOCK and VALUE_SYMTAB_BLOCK.  Other
889  // blocks can defined their abbrevs inline.
890  Stream.EnterBlockInfoBlock(2);
891
892  { // 8-bit fixed-width VST_ENTRY/VST_BBENTRY strings.
893    BitCodeAbbrev *Abbv = new BitCodeAbbrev();
894    Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3));
895    Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
896    Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
897    Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
898    if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
899                                   Abbv) != VST_ENTRY_8_ABBREV)
900      assert(0 && "Unexpected abbrev ordering!");
901  }
902
903  { // 7-bit fixed width VST_ENTRY strings.
904    BitCodeAbbrev *Abbv = new BitCodeAbbrev();
905    Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
906    Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
907    Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
908    Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
909    if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
910                                   Abbv) != VST_ENTRY_7_ABBREV)
911      assert(0 && "Unexpected abbrev ordering!");
912  }
913  { // 7-bit fixed width VST_BBENTRY strings.
914    BitCodeAbbrev *Abbv = new BitCodeAbbrev();
915    Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_BBENTRY));
916    Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
917    Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
918    Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
919    if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
920                                   Abbv) != VST_BBENTRY_7_ABBREV)
921      assert(0 && "Unexpected abbrev ordering!");
922  }
923
924  Stream.ExitBlock();
925}
926
927
928/// WriteBitcodeToFile - Write the specified module to the specified output
929/// stream.
930void llvm::WriteBitcodeToFile(const Module *M, std::ostream &Out) {
931  std::vector<unsigned char> Buffer;
932  BitstreamWriter Stream(Buffer);
933
934  Buffer.reserve(256*1024);
935
936  // Emit the file header.
937  Stream.Emit((unsigned)'B', 8);
938  Stream.Emit((unsigned)'C', 8);
939  Stream.Emit(0x0, 4);
940  Stream.Emit(0xC, 4);
941  Stream.Emit(0xE, 4);
942  Stream.Emit(0xD, 4);
943
944  // Emit blockinfo, which defines the standard abbreviations etc.
945  WriteBlockInfo(Stream);
946
947  // Emit the module.
948  WriteModule(M, Stream);
949
950  // Write the generated bitstream to "Out".
951  Out.write((char*)&Buffer.front(), Buffer.size());
952}
953