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