1 //===--- Bitcode/Writer/BitcodeWriter.cpp - Bitcode Writer ----------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // Bitcode writer implementation.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Bitcode/ReaderWriter.h"
15 #include "llvm/Bitcode/BitstreamWriter.h"
16 #include "llvm/Bitcode/LLVMBitCodes.h"
17 #include "ValueEnumerator.h"
18 #include "llvm/Constants.h"
19 #include "llvm/DerivedTypes.h"
20 #include "llvm/InlineAsm.h"
21 #include "llvm/Instructions.h"
22 #include "llvm/Module.h"
23 #include "llvm/ParameterAttributes.h"
24 #include "llvm/TypeSymbolTable.h"
25 #include "llvm/ValueSymbolTable.h"
26 #include "llvm/Support/MathExtras.h"
29 /// These are manifest constants used by the bitcode writer. They do not need to
30 /// be kept in sync with the reader, but need to be consistent within this file.
34 // VALUE_SYMTAB_BLOCK abbrev id's.
35 VST_ENTRY_8_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
40 // CONSTANTS_BLOCK abbrev id's.
41 CONSTANTS_SETTYPE_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
42 CONSTANTS_INTEGER_ABBREV,
43 CONSTANTS_CE_CAST_Abbrev,
44 CONSTANTS_NULL_Abbrev,
46 // FUNCTION_BLOCK abbrev id's.
47 FUNCTION_INST_LOAD_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
48 FUNCTION_INST_BINOP_ABBREV,
49 FUNCTION_INST_CAST_ABBREV,
50 FUNCTION_INST_RET_VOID_ABBREV,
51 FUNCTION_INST_RET_VAL_ABBREV,
52 FUNCTION_INST_UNREACHABLE_ABBREV
56 static unsigned GetEncodedCastOpcode(unsigned Opcode) {
58 default: assert(0 && "Unknown cast instruction!");
59 case Instruction::Trunc : return bitc::CAST_TRUNC;
60 case Instruction::ZExt : return bitc::CAST_ZEXT;
61 case Instruction::SExt : return bitc::CAST_SEXT;
62 case Instruction::FPToUI : return bitc::CAST_FPTOUI;
63 case Instruction::FPToSI : return bitc::CAST_FPTOSI;
64 case Instruction::UIToFP : return bitc::CAST_UITOFP;
65 case Instruction::SIToFP : return bitc::CAST_SITOFP;
66 case Instruction::FPTrunc : return bitc::CAST_FPTRUNC;
67 case Instruction::FPExt : return bitc::CAST_FPEXT;
68 case Instruction::PtrToInt: return bitc::CAST_PTRTOINT;
69 case Instruction::IntToPtr: return bitc::CAST_INTTOPTR;
70 case Instruction::BitCast : return bitc::CAST_BITCAST;
74 static unsigned GetEncodedBinaryOpcode(unsigned Opcode) {
76 default: assert(0 && "Unknown binary instruction!");
77 case Instruction::Add: return bitc::BINOP_ADD;
78 case Instruction::Sub: return bitc::BINOP_SUB;
79 case Instruction::Mul: return bitc::BINOP_MUL;
80 case Instruction::UDiv: return bitc::BINOP_UDIV;
81 case Instruction::FDiv:
82 case Instruction::SDiv: return bitc::BINOP_SDIV;
83 case Instruction::URem: return bitc::BINOP_UREM;
84 case Instruction::FRem:
85 case Instruction::SRem: return bitc::BINOP_SREM;
86 case Instruction::Shl: return bitc::BINOP_SHL;
87 case Instruction::LShr: return bitc::BINOP_LSHR;
88 case Instruction::AShr: return bitc::BINOP_ASHR;
89 case Instruction::And: return bitc::BINOP_AND;
90 case Instruction::Or: return bitc::BINOP_OR;
91 case Instruction::Xor: return bitc::BINOP_XOR;
97 static void WriteStringRecord(unsigned Code, const std::string &Str,
98 unsigned AbbrevToUse, BitstreamWriter &Stream) {
99 SmallVector<unsigned, 64> Vals;
101 // Code: [strchar x N]
102 for (unsigned i = 0, e = Str.size(); i != e; ++i)
103 Vals.push_back(Str[i]);
105 // Emit the finished record.
106 Stream.EmitRecord(Code, Vals, AbbrevToUse);
109 // Emit information about parameter attributes.
110 static void WriteParamAttrTable(const ValueEnumerator &VE,
111 BitstreamWriter &Stream) {
112 const std::vector<const ParamAttrsList*> &Attrs = VE.getParamAttrs();
113 if (Attrs.empty()) return;
115 Stream.EnterSubblock(bitc::PARAMATTR_BLOCK_ID, 3);
117 SmallVector<uint64_t, 64> Record;
118 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
119 const ParamAttrsList *A = Attrs[i];
120 for (unsigned op = 0, e = A->size(); op != e; ++op) {
121 Record.push_back(A->getParamIndex(op));
122 Record.push_back(A->getParamAttrsAtIndex(op));
125 Stream.EmitRecord(bitc::PARAMATTR_CODE_ENTRY, Record);
132 /// WriteTypeTable - Write out the type table for a module.
133 static void WriteTypeTable(const ValueEnumerator &VE, BitstreamWriter &Stream) {
134 const ValueEnumerator::TypeList &TypeList = VE.getTypes();
136 Stream.EnterSubblock(bitc::TYPE_BLOCK_ID, 4 /*count from # abbrevs */);
137 SmallVector<uint64_t, 64> TypeVals;
139 // Abbrev for TYPE_CODE_POINTER.
140 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
141 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_POINTER));
142 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
143 Log2_32_Ceil(VE.getTypes().size()+1)));
144 unsigned PtrAbbrev = Stream.EmitAbbrev(Abbv);
146 // Abbrev for TYPE_CODE_FUNCTION.
147 Abbv = new BitCodeAbbrev();
148 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_FUNCTION));
149 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isvararg
150 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
151 Log2_32_Ceil(VE.getParamAttrs().size()+1)));
152 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
153 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
154 Log2_32_Ceil(VE.getTypes().size()+1)));
155 unsigned FunctionAbbrev = Stream.EmitAbbrev(Abbv);
157 // Abbrev for TYPE_CODE_STRUCT.
158 Abbv = new BitCodeAbbrev();
159 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT));
160 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked
161 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
162 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
163 Log2_32_Ceil(VE.getTypes().size()+1)));
164 unsigned StructAbbrev = Stream.EmitAbbrev(Abbv);
166 // Abbrev for TYPE_CODE_ARRAY.
167 Abbv = new BitCodeAbbrev();
168 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_ARRAY));
169 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // size
170 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
171 Log2_32_Ceil(VE.getTypes().size()+1)));
172 unsigned ArrayAbbrev = Stream.EmitAbbrev(Abbv);
174 // Emit an entry count so the reader can reserve space.
175 TypeVals.push_back(TypeList.size());
176 Stream.EmitRecord(bitc::TYPE_CODE_NUMENTRY, TypeVals);
179 // Loop over all of the types, emitting each in turn.
180 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
181 const Type *T = TypeList[i].first;
185 switch (T->getTypeID()) {
186 case Type::PackedStructTyID: // FIXME: Delete Type::PackedStructTyID.
187 default: assert(0 && "Unknown type!");
188 case Type::VoidTyID: Code = bitc::TYPE_CODE_VOID; break;
189 case Type::FloatTyID: Code = bitc::TYPE_CODE_FLOAT; break;
190 case Type::DoubleTyID: Code = bitc::TYPE_CODE_DOUBLE; break;
191 case Type::LabelTyID: Code = bitc::TYPE_CODE_LABEL; break;
192 case Type::OpaqueTyID: Code = bitc::TYPE_CODE_OPAQUE; break;
193 case Type::IntegerTyID:
195 Code = bitc::TYPE_CODE_INTEGER;
196 TypeVals.push_back(cast<IntegerType>(T)->getBitWidth());
198 case Type::PointerTyID:
199 // POINTER: [pointee type]
200 Code = bitc::TYPE_CODE_POINTER;
201 TypeVals.push_back(VE.getTypeID(cast<PointerType>(T)->getElementType()));
202 AbbrevToUse = PtrAbbrev;
205 case Type::FunctionTyID: {
206 const FunctionType *FT = cast<FunctionType>(T);
207 // FUNCTION: [isvararg, attrid, retty, paramty x N]
208 Code = bitc::TYPE_CODE_FUNCTION;
209 TypeVals.push_back(FT->isVarArg());
210 TypeVals.push_back(VE.getParamAttrID(FT->getParamAttrs()));
211 TypeVals.push_back(VE.getTypeID(FT->getReturnType()));
212 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i)
213 TypeVals.push_back(VE.getTypeID(FT->getParamType(i)));
214 AbbrevToUse = FunctionAbbrev;
217 case Type::StructTyID: {
218 const StructType *ST = cast<StructType>(T);
219 // STRUCT: [ispacked, eltty x N]
220 Code = bitc::TYPE_CODE_STRUCT;
221 TypeVals.push_back(ST->isPacked());
222 // Output all of the element types.
223 for (StructType::element_iterator I = ST->element_begin(),
224 E = ST->element_end(); I != E; ++I)
225 TypeVals.push_back(VE.getTypeID(*I));
226 AbbrevToUse = StructAbbrev;
229 case Type::ArrayTyID: {
230 const ArrayType *AT = cast<ArrayType>(T);
231 // ARRAY: [numelts, eltty]
232 Code = bitc::TYPE_CODE_ARRAY;
233 TypeVals.push_back(AT->getNumElements());
234 TypeVals.push_back(VE.getTypeID(AT->getElementType()));
235 AbbrevToUse = ArrayAbbrev;
238 case Type::VectorTyID: {
239 const VectorType *VT = cast<VectorType>(T);
240 // VECTOR [numelts, eltty]
241 Code = bitc::TYPE_CODE_VECTOR;
242 TypeVals.push_back(VT->getNumElements());
243 TypeVals.push_back(VE.getTypeID(VT->getElementType()));
248 // Emit the finished record.
249 Stream.EmitRecord(Code, TypeVals, AbbrevToUse);
256 static unsigned getEncodedLinkage(const GlobalValue *GV) {
257 switch (GV->getLinkage()) {
258 default: assert(0 && "Invalid linkage!");
259 case GlobalValue::GhostLinkage: // Map ghost linkage onto external.
260 case GlobalValue::ExternalLinkage: return 0;
261 case GlobalValue::WeakLinkage: return 1;
262 case GlobalValue::AppendingLinkage: return 2;
263 case GlobalValue::InternalLinkage: return 3;
264 case GlobalValue::LinkOnceLinkage: return 4;
265 case GlobalValue::DLLImportLinkage: return 5;
266 case GlobalValue::DLLExportLinkage: return 6;
267 case GlobalValue::ExternalWeakLinkage: return 7;
271 static unsigned getEncodedVisibility(const GlobalValue *GV) {
272 switch (GV->getVisibility()) {
273 default: assert(0 && "Invalid visibility!");
274 case GlobalValue::DefaultVisibility: return 0;
275 case GlobalValue::HiddenVisibility: return 1;
276 case GlobalValue::ProtectedVisibility: return 2;
280 // Emit top-level description of module, including target triple, inline asm,
281 // descriptors for global variables, and function prototype info.
282 static void WriteModuleInfo(const Module *M, const ValueEnumerator &VE,
283 BitstreamWriter &Stream) {
284 // Emit the list of dependent libraries for the Module.
285 for (Module::lib_iterator I = M->lib_begin(), E = M->lib_end(); I != E; ++I)
286 WriteStringRecord(bitc::MODULE_CODE_DEPLIB, *I, 0/*TODO*/, Stream);
288 // Emit various pieces of data attached to a module.
289 if (!M->getTargetTriple().empty())
290 WriteStringRecord(bitc::MODULE_CODE_TRIPLE, M->getTargetTriple(),
292 if (!M->getDataLayout().empty())
293 WriteStringRecord(bitc::MODULE_CODE_DATALAYOUT, M->getDataLayout(),
295 if (!M->getModuleInlineAsm().empty())
296 WriteStringRecord(bitc::MODULE_CODE_ASM, M->getModuleInlineAsm(),
299 // Emit information about sections, computing how many there are. Also
300 // compute the maximum alignment value.
301 std::map<std::string, unsigned> SectionMap;
302 unsigned MaxAlignment = 0;
303 unsigned MaxGlobalType = 0;
304 for (Module::const_global_iterator GV = M->global_begin(),E = M->global_end();
306 MaxAlignment = std::max(MaxAlignment, GV->getAlignment());
307 MaxGlobalType = std::max(MaxGlobalType, VE.getTypeID(GV->getType()));
309 if (!GV->hasSection()) continue;
310 // Give section names unique ID's.
311 unsigned &Entry = SectionMap[GV->getSection()];
312 if (Entry != 0) continue;
313 WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, GV->getSection(),
315 Entry = SectionMap.size();
317 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
318 MaxAlignment = std::max(MaxAlignment, F->getAlignment());
319 if (!F->hasSection()) continue;
320 // Give section names unique ID's.
321 unsigned &Entry = SectionMap[F->getSection()];
322 if (Entry != 0) continue;
323 WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, F->getSection(),
325 Entry = SectionMap.size();
328 // Emit abbrev for globals, now that we know # sections and max alignment.
329 unsigned SimpleGVarAbbrev = 0;
330 if (!M->global_empty()) {
331 // Add an abbrev for common globals with no visibility or thread localness.
332 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
333 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_GLOBALVAR));
334 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
335 Log2_32_Ceil(MaxGlobalType+1)));
336 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Constant.
337 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Initializer.
338 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Linkage.
339 if (MaxAlignment == 0) // Alignment.
340 Abbv->Add(BitCodeAbbrevOp(0));
342 unsigned MaxEncAlignment = Log2_32(MaxAlignment)+1;
343 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
344 Log2_32_Ceil(MaxEncAlignment+1)));
346 if (SectionMap.empty()) // Section.
347 Abbv->Add(BitCodeAbbrevOp(0));
349 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
350 Log2_32_Ceil(SectionMap.size()+1)));
351 // Don't bother emitting vis + thread local.
352 SimpleGVarAbbrev = Stream.EmitAbbrev(Abbv);
355 // Emit the global variable information.
356 SmallVector<unsigned, 64> Vals;
357 for (Module::const_global_iterator GV = M->global_begin(),E = M->global_end();
359 unsigned AbbrevToUse = 0;
361 // GLOBALVAR: [type, isconst, initid,
362 // linkage, alignment, section, visibility, threadlocal]
363 Vals.push_back(VE.getTypeID(GV->getType()));
364 Vals.push_back(GV->isConstant());
365 Vals.push_back(GV->isDeclaration() ? 0 :
366 (VE.getValueID(GV->getInitializer()) + 1));
367 Vals.push_back(getEncodedLinkage(GV));
368 Vals.push_back(Log2_32(GV->getAlignment())+1);
369 Vals.push_back(GV->hasSection() ? SectionMap[GV->getSection()] : 0);
370 if (GV->isThreadLocal() ||
371 GV->getVisibility() != GlobalValue::DefaultVisibility) {
372 Vals.push_back(getEncodedVisibility(GV));
373 Vals.push_back(GV->isThreadLocal());
375 AbbrevToUse = SimpleGVarAbbrev;
378 Stream.EmitRecord(bitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse);
382 // Emit the function proto information.
383 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
384 // FUNCTION: [type, callingconv, isproto, linkage, alignment, section,
386 Vals.push_back(VE.getTypeID(F->getType()));
387 Vals.push_back(F->getCallingConv());
388 Vals.push_back(F->isDeclaration());
389 Vals.push_back(getEncodedLinkage(F));
391 // Note: we emit the param attr ID number for the function type of this
392 // function. In the future, we intend for attrs to be properties of
393 // functions, instead of on the type. This is to support this future work.
394 Vals.push_back(VE.getParamAttrID(F->getFunctionType()->getParamAttrs()));
396 Vals.push_back(Log2_32(F->getAlignment())+1);
397 Vals.push_back(F->hasSection() ? SectionMap[F->getSection()] : 0);
398 Vals.push_back(getEncodedVisibility(F));
400 unsigned AbbrevToUse = 0;
401 Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse);
406 // Emit the alias information.
407 for (Module::const_alias_iterator AI = M->alias_begin(), E = M->alias_end();
409 Vals.push_back(VE.getTypeID(AI->getType()));
410 Vals.push_back(VE.getValueID(AI->getAliasee()));
411 Vals.push_back(getEncodedLinkage(AI));
412 unsigned AbbrevToUse = 0;
413 Stream.EmitRecord(bitc::MODULE_CODE_ALIAS, Vals, AbbrevToUse);
419 static void WriteConstants(unsigned FirstVal, unsigned LastVal,
420 const ValueEnumerator &VE,
421 BitstreamWriter &Stream, bool isGlobal) {
422 if (FirstVal == LastVal) return;
424 Stream.EnterSubblock(bitc::CONSTANTS_BLOCK_ID, 4);
426 unsigned AggregateAbbrev = 0;
427 unsigned String8Abbrev = 0;
428 unsigned CString7Abbrev = 0;
429 unsigned CString6Abbrev = 0;
430 // If this is a constant pool for the module, emit module-specific abbrevs.
432 // Abbrev for CST_CODE_AGGREGATE.
433 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
434 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_AGGREGATE));
435 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
436 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, Log2_32_Ceil(LastVal+1)));
437 AggregateAbbrev = Stream.EmitAbbrev(Abbv);
439 // Abbrev for CST_CODE_STRING.
440 Abbv = new BitCodeAbbrev();
441 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_STRING));
442 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
443 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
444 String8Abbrev = Stream.EmitAbbrev(Abbv);
445 // Abbrev for CST_CODE_CSTRING.
446 Abbv = new BitCodeAbbrev();
447 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
448 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
449 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
450 CString7Abbrev = Stream.EmitAbbrev(Abbv);
451 // Abbrev for CST_CODE_CSTRING.
452 Abbv = new BitCodeAbbrev();
453 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
454 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
455 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
456 CString6Abbrev = Stream.EmitAbbrev(Abbv);
459 SmallVector<uint64_t, 64> Record;
461 const ValueEnumerator::ValueList &Vals = VE.getValues();
462 const Type *LastTy = 0;
463 for (unsigned i = FirstVal; i != LastVal; ++i) {
464 const Value *V = Vals[i].first;
465 // If we need to switch types, do so now.
466 if (V->getType() != LastTy) {
467 LastTy = V->getType();
468 Record.push_back(VE.getTypeID(LastTy));
469 Stream.EmitRecord(bitc::CST_CODE_SETTYPE, Record,
470 CONSTANTS_SETTYPE_ABBREV);
474 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
475 Record.push_back(unsigned(IA->hasSideEffects()));
477 // Add the asm string.
478 const std::string &AsmStr = IA->getAsmString();
479 Record.push_back(AsmStr.size());
480 for (unsigned i = 0, e = AsmStr.size(); i != e; ++i)
481 Record.push_back(AsmStr[i]);
483 // Add the constraint string.
484 const std::string &ConstraintStr = IA->getConstraintString();
485 Record.push_back(ConstraintStr.size());
486 for (unsigned i = 0, e = ConstraintStr.size(); i != e; ++i)
487 Record.push_back(ConstraintStr[i]);
488 Stream.EmitRecord(bitc::CST_CODE_INLINEASM, Record);
492 const Constant *C = cast<Constant>(V);
494 unsigned AbbrevToUse = 0;
495 if (C->isNullValue()) {
496 Code = bitc::CST_CODE_NULL;
497 } else if (isa<UndefValue>(C)) {
498 Code = bitc::CST_CODE_UNDEF;
499 } else if (const ConstantInt *IV = dyn_cast<ConstantInt>(C)) {
500 if (IV->getBitWidth() <= 64) {
501 int64_t V = IV->getSExtValue();
503 Record.push_back(V << 1);
505 Record.push_back((-V << 1) | 1);
506 Code = bitc::CST_CODE_INTEGER;
507 AbbrevToUse = CONSTANTS_INTEGER_ABBREV;
508 } else { // Wide integers, > 64 bits in size.
509 // We have an arbitrary precision integer value to write whose
510 // bit width is > 64. However, in canonical unsigned integer
511 // format it is likely that the high bits are going to be zero.
512 // So, we only write the number of active words.
513 unsigned NWords = IV->getValue().getActiveWords();
514 const uint64_t *RawWords = IV->getValue().getRawData();
515 for (unsigned i = 0; i != NWords; ++i) {
516 int64_t V = RawWords[i];
518 Record.push_back(V << 1);
520 Record.push_back((-V << 1) | 1);
522 Code = bitc::CST_CODE_WIDE_INTEGER;
524 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
525 Code = bitc::CST_CODE_FLOAT;
526 if (CFP->getType() == Type::FloatTy) {
527 Record.push_back(FloatToBits((float)CFP->getValue()));
529 assert (CFP->getType() == Type::DoubleTy && "Unknown FP type!");
530 Record.push_back(DoubleToBits((double)CFP->getValue()));
532 } else if (isa<ConstantArray>(C) && cast<ConstantArray>(C)->isString()) {
533 // Emit constant strings specially.
534 unsigned NumOps = C->getNumOperands();
535 // If this is a null-terminated string, use the denser CSTRING encoding.
536 if (C->getOperand(NumOps-1)->isNullValue()) {
537 Code = bitc::CST_CODE_CSTRING;
538 --NumOps; // Don't encode the null, which isn't allowed by char6.
540 Code = bitc::CST_CODE_STRING;
541 AbbrevToUse = String8Abbrev;
543 bool isCStr7 = Code == bitc::CST_CODE_CSTRING;
544 bool isCStrChar6 = Code == bitc::CST_CODE_CSTRING;
545 for (unsigned i = 0; i != NumOps; ++i) {
546 unsigned char V = cast<ConstantInt>(C->getOperand(i))->getZExtValue();
548 isCStr7 &= (V & 128) == 0;
550 isCStrChar6 = BitCodeAbbrevOp::isChar6(V);
554 AbbrevToUse = CString6Abbrev;
556 AbbrevToUse = CString7Abbrev;
557 } else if (isa<ConstantArray>(C) || isa<ConstantStruct>(V) ||
558 isa<ConstantVector>(V)) {
559 Code = bitc::CST_CODE_AGGREGATE;
560 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
561 Record.push_back(VE.getValueID(C->getOperand(i)));
562 AbbrevToUse = AggregateAbbrev;
563 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
564 switch (CE->getOpcode()) {
566 if (Instruction::isCast(CE->getOpcode())) {
567 Code = bitc::CST_CODE_CE_CAST;
568 Record.push_back(GetEncodedCastOpcode(CE->getOpcode()));
569 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
570 Record.push_back(VE.getValueID(C->getOperand(0)));
571 AbbrevToUse = CONSTANTS_CE_CAST_Abbrev;
573 assert(CE->getNumOperands() == 2 && "Unknown constant expr!");
574 Code = bitc::CST_CODE_CE_BINOP;
575 Record.push_back(GetEncodedBinaryOpcode(CE->getOpcode()));
576 Record.push_back(VE.getValueID(C->getOperand(0)));
577 Record.push_back(VE.getValueID(C->getOperand(1)));
580 case Instruction::GetElementPtr:
581 Code = bitc::CST_CODE_CE_GEP;
582 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) {
583 Record.push_back(VE.getTypeID(C->getOperand(i)->getType()));
584 Record.push_back(VE.getValueID(C->getOperand(i)));
587 case Instruction::Select:
588 Code = bitc::CST_CODE_CE_SELECT;
589 Record.push_back(VE.getValueID(C->getOperand(0)));
590 Record.push_back(VE.getValueID(C->getOperand(1)));
591 Record.push_back(VE.getValueID(C->getOperand(2)));
593 case Instruction::ExtractElement:
594 Code = bitc::CST_CODE_CE_EXTRACTELT;
595 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
596 Record.push_back(VE.getValueID(C->getOperand(0)));
597 Record.push_back(VE.getValueID(C->getOperand(1)));
599 case Instruction::InsertElement:
600 Code = bitc::CST_CODE_CE_INSERTELT;
601 Record.push_back(VE.getValueID(C->getOperand(0)));
602 Record.push_back(VE.getValueID(C->getOperand(1)));
603 Record.push_back(VE.getValueID(C->getOperand(2)));
605 case Instruction::ShuffleVector:
606 Code = bitc::CST_CODE_CE_SHUFFLEVEC;
607 Record.push_back(VE.getValueID(C->getOperand(0)));
608 Record.push_back(VE.getValueID(C->getOperand(1)));
609 Record.push_back(VE.getValueID(C->getOperand(2)));
611 case Instruction::ICmp:
612 case Instruction::FCmp:
613 Code = bitc::CST_CODE_CE_CMP;
614 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
615 Record.push_back(VE.getValueID(C->getOperand(0)));
616 Record.push_back(VE.getValueID(C->getOperand(1)));
617 Record.push_back(CE->getPredicate());
621 assert(0 && "Unknown constant!");
623 Stream.EmitRecord(Code, Record, AbbrevToUse);
630 static void WriteModuleConstants(const ValueEnumerator &VE,
631 BitstreamWriter &Stream) {
632 const ValueEnumerator::ValueList &Vals = VE.getValues();
634 // Find the first constant to emit, which is the first non-globalvalue value.
635 // We know globalvalues have been emitted by WriteModuleInfo.
636 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
637 if (!isa<GlobalValue>(Vals[i].first)) {
638 WriteConstants(i, Vals.size(), VE, Stream, true);
644 /// PushValueAndType - The file has to encode both the value and type id for
645 /// many values, because we need to know what type to create for forward
646 /// references. However, most operands are not forward references, so this type
647 /// field is not needed.
649 /// This function adds V's value ID to Vals. If the value ID is higher than the
650 /// instruction ID, then it is a forward reference, and it also includes the
652 static bool PushValueAndType(Value *V, unsigned InstID,
653 SmallVector<unsigned, 64> &Vals,
654 ValueEnumerator &VE) {
655 unsigned ValID = VE.getValueID(V);
656 Vals.push_back(ValID);
657 if (ValID >= InstID) {
658 Vals.push_back(VE.getTypeID(V->getType()));
664 /// WriteInstruction - Emit an instruction to the specified stream.
665 static void WriteInstruction(const Instruction &I, unsigned InstID,
666 ValueEnumerator &VE, BitstreamWriter &Stream,
667 SmallVector<unsigned, 64> &Vals) {
669 unsigned AbbrevToUse = 0;
670 switch (I.getOpcode()) {
672 if (Instruction::isCast(I.getOpcode())) {
673 Code = bitc::FUNC_CODE_INST_CAST;
674 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE))
675 AbbrevToUse = FUNCTION_INST_CAST_ABBREV;
676 Vals.push_back(VE.getTypeID(I.getType()));
677 Vals.push_back(GetEncodedCastOpcode(I.getOpcode()));
679 assert(isa<BinaryOperator>(I) && "Unknown instruction!");
680 Code = bitc::FUNC_CODE_INST_BINOP;
681 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE))
682 AbbrevToUse = FUNCTION_INST_BINOP_ABBREV;
683 Vals.push_back(VE.getValueID(I.getOperand(1)));
684 Vals.push_back(GetEncodedBinaryOpcode(I.getOpcode()));
688 case Instruction::GetElementPtr:
689 Code = bitc::FUNC_CODE_INST_GEP;
690 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
691 PushValueAndType(I.getOperand(i), InstID, Vals, VE);
693 case Instruction::Select:
694 Code = bitc::FUNC_CODE_INST_SELECT;
695 PushValueAndType(I.getOperand(1), InstID, Vals, VE);
696 Vals.push_back(VE.getValueID(I.getOperand(2)));
697 Vals.push_back(VE.getValueID(I.getOperand(0)));
699 case Instruction::ExtractElement:
700 Code = bitc::FUNC_CODE_INST_EXTRACTELT;
701 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
702 Vals.push_back(VE.getValueID(I.getOperand(1)));
704 case Instruction::InsertElement:
705 Code = bitc::FUNC_CODE_INST_INSERTELT;
706 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
707 Vals.push_back(VE.getValueID(I.getOperand(1)));
708 Vals.push_back(VE.getValueID(I.getOperand(2)));
710 case Instruction::ShuffleVector:
711 Code = bitc::FUNC_CODE_INST_SHUFFLEVEC;
712 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
713 Vals.push_back(VE.getValueID(I.getOperand(1)));
714 Vals.push_back(VE.getValueID(I.getOperand(2)));
716 case Instruction::ICmp:
717 case Instruction::FCmp:
718 Code = bitc::FUNC_CODE_INST_CMP;
719 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
720 Vals.push_back(VE.getValueID(I.getOperand(1)));
721 Vals.push_back(cast<CmpInst>(I).getPredicate());
724 case Instruction::Ret:
725 Code = bitc::FUNC_CODE_INST_RET;
726 if (!I.getNumOperands())
727 AbbrevToUse = FUNCTION_INST_RET_VOID_ABBREV;
728 else if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE))
729 AbbrevToUse = FUNCTION_INST_RET_VAL_ABBREV;
731 case Instruction::Br:
732 Code = bitc::FUNC_CODE_INST_BR;
733 Vals.push_back(VE.getValueID(I.getOperand(0)));
734 if (cast<BranchInst>(I).isConditional()) {
735 Vals.push_back(VE.getValueID(I.getOperand(1)));
736 Vals.push_back(VE.getValueID(I.getOperand(2)));
739 case Instruction::Switch:
740 Code = bitc::FUNC_CODE_INST_SWITCH;
741 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
742 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
743 Vals.push_back(VE.getValueID(I.getOperand(i)));
745 case Instruction::Invoke: {
746 const PointerType *PTy = cast<PointerType>(I.getOperand(0)->getType());
747 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
748 Code = bitc::FUNC_CODE_INST_INVOKE;
750 // Note: we emit the param attr ID number for the function type of this
751 // function. In the future, we intend for attrs to be properties of
752 // functions, instead of on the type. This is to support this future work.
753 Vals.push_back(VE.getParamAttrID(FTy->getParamAttrs()));
755 Vals.push_back(cast<InvokeInst>(I).getCallingConv());
756 Vals.push_back(VE.getValueID(I.getOperand(1))); // normal dest
757 Vals.push_back(VE.getValueID(I.getOperand(2))); // unwind dest
758 PushValueAndType(I.getOperand(0), InstID, Vals, VE); // callee
760 // Emit value #'s for the fixed parameters.
761 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
762 Vals.push_back(VE.getValueID(I.getOperand(i+3))); // fixed param.
764 // Emit type/value pairs for varargs params.
765 if (FTy->isVarArg()) {
766 for (unsigned i = 3+FTy->getNumParams(), e = I.getNumOperands();
768 PushValueAndType(I.getOperand(i), InstID, Vals, VE); // vararg
772 case Instruction::Unwind:
773 Code = bitc::FUNC_CODE_INST_UNWIND;
775 case Instruction::Unreachable:
776 Code = bitc::FUNC_CODE_INST_UNREACHABLE;
777 AbbrevToUse = FUNCTION_INST_UNREACHABLE_ABBREV;
780 case Instruction::PHI:
781 Code = bitc::FUNC_CODE_INST_PHI;
782 Vals.push_back(VE.getTypeID(I.getType()));
783 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
784 Vals.push_back(VE.getValueID(I.getOperand(i)));
787 case Instruction::Malloc:
788 Code = bitc::FUNC_CODE_INST_MALLOC;
789 Vals.push_back(VE.getTypeID(I.getType()));
790 Vals.push_back(VE.getValueID(I.getOperand(0))); // size.
791 Vals.push_back(Log2_32(cast<MallocInst>(I).getAlignment())+1);
794 case Instruction::Free:
795 Code = bitc::FUNC_CODE_INST_FREE;
796 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
799 case Instruction::Alloca:
800 Code = bitc::FUNC_CODE_INST_ALLOCA;
801 Vals.push_back(VE.getTypeID(I.getType()));
802 Vals.push_back(VE.getValueID(I.getOperand(0))); // size.
803 Vals.push_back(Log2_32(cast<AllocaInst>(I).getAlignment())+1);
806 case Instruction::Load:
807 Code = bitc::FUNC_CODE_INST_LOAD;
808 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE)) // ptr
809 AbbrevToUse = FUNCTION_INST_LOAD_ABBREV;
811 Vals.push_back(Log2_32(cast<LoadInst>(I).getAlignment())+1);
812 Vals.push_back(cast<LoadInst>(I).isVolatile());
814 case Instruction::Store:
815 Code = bitc::FUNC_CODE_INST_STORE;
816 PushValueAndType(I.getOperand(0), InstID, Vals, VE); // val.
817 Vals.push_back(VE.getValueID(I.getOperand(1))); // ptr.
818 Vals.push_back(Log2_32(cast<StoreInst>(I).getAlignment())+1);
819 Vals.push_back(cast<StoreInst>(I).isVolatile());
821 case Instruction::Call: {
822 const PointerType *PTy = cast<PointerType>(I.getOperand(0)->getType());
823 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
825 Code = bitc::FUNC_CODE_INST_CALL;
827 // Note: we emit the param attr ID number for the function type of this
828 // function. In the future, we intend for attrs to be properties of
829 // functions, instead of on the type. This is to support this future work.
830 Vals.push_back(VE.getParamAttrID(FTy->getParamAttrs()));
832 Vals.push_back((cast<CallInst>(I).getCallingConv() << 1) |
833 unsigned(cast<CallInst>(I).isTailCall()));
834 PushValueAndType(I.getOperand(0), InstID, Vals, VE); // Callee
836 // Emit value #'s for the fixed parameters.
837 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
838 Vals.push_back(VE.getValueID(I.getOperand(i+1))); // fixed param.
840 // Emit type/value pairs for varargs params.
841 if (FTy->isVarArg()) {
842 unsigned NumVarargs = I.getNumOperands()-1-FTy->getNumParams();
843 for (unsigned i = I.getNumOperands()-NumVarargs, e = I.getNumOperands();
845 PushValueAndType(I.getOperand(i), InstID, Vals, VE); // varargs
849 case Instruction::VAArg:
850 Code = bitc::FUNC_CODE_INST_VAARG;
851 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); // valistty
852 Vals.push_back(VE.getValueID(I.getOperand(0))); // valist.
853 Vals.push_back(VE.getTypeID(I.getType())); // restype.
857 Stream.EmitRecord(Code, Vals, AbbrevToUse);
861 // Emit names for globals/functions etc.
862 static void WriteValueSymbolTable(const ValueSymbolTable &VST,
863 const ValueEnumerator &VE,
864 BitstreamWriter &Stream) {
865 if (VST.empty()) return;
866 Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4);
868 // FIXME: Set up the abbrev, we know how many values there are!
869 // FIXME: We know if the type names can use 7-bit ascii.
870 SmallVector<unsigned, 64> NameVals;
872 for (ValueSymbolTable::const_iterator SI = VST.begin(), SE = VST.end();
875 const ValueName &Name = *SI;
877 // Figure out the encoding to use for the name.
880 for (const char *C = Name.getKeyData(), *E = C+Name.getKeyLength();
883 isChar6 = BitCodeAbbrevOp::isChar6(*C);
884 if ((unsigned char)*C & 128) {
886 break; // don't bother scanning the rest.
890 unsigned AbbrevToUse = VST_ENTRY_8_ABBREV;
892 // VST_ENTRY: [valueid, namechar x N]
893 // VST_BBENTRY: [bbid, namechar x N]
895 if (isa<BasicBlock>(SI->getValue())) {
896 Code = bitc::VST_CODE_BBENTRY;
898 AbbrevToUse = VST_BBENTRY_6_ABBREV;
900 Code = bitc::VST_CODE_ENTRY;
902 AbbrevToUse = VST_ENTRY_6_ABBREV;
904 AbbrevToUse = VST_ENTRY_7_ABBREV;
907 NameVals.push_back(VE.getValueID(SI->getValue()));
908 for (const char *P = Name.getKeyData(),
909 *E = Name.getKeyData()+Name.getKeyLength(); P != E; ++P)
910 NameVals.push_back((unsigned char)*P);
912 // Emit the finished record.
913 Stream.EmitRecord(Code, NameVals, AbbrevToUse);
919 /// WriteFunction - Emit a function body to the module stream.
920 static void WriteFunction(const Function &F, ValueEnumerator &VE,
921 BitstreamWriter &Stream) {
922 Stream.EnterSubblock(bitc::FUNCTION_BLOCK_ID, 4);
923 VE.incorporateFunction(F);
925 SmallVector<unsigned, 64> Vals;
927 // Emit the number of basic blocks, so the reader can create them ahead of
929 Vals.push_back(VE.getBasicBlocks().size());
930 Stream.EmitRecord(bitc::FUNC_CODE_DECLAREBLOCKS, Vals);
933 // If there are function-local constants, emit them now.
934 unsigned CstStart, CstEnd;
935 VE.getFunctionConstantRange(CstStart, CstEnd);
936 WriteConstants(CstStart, CstEnd, VE, Stream, false);
938 // Keep a running idea of what the instruction ID is.
939 unsigned InstID = CstEnd;
941 // Finally, emit all the instructions, in order.
942 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
943 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
945 WriteInstruction(*I, InstID, VE, Stream, Vals);
946 if (I->getType() != Type::VoidTy)
950 // Emit names for all the instructions etc.
951 WriteValueSymbolTable(F.getValueSymbolTable(), VE, Stream);
957 /// WriteTypeSymbolTable - Emit a block for the specified type symtab.
958 static void WriteTypeSymbolTable(const TypeSymbolTable &TST,
959 const ValueEnumerator &VE,
960 BitstreamWriter &Stream) {
961 if (TST.empty()) return;
963 Stream.EnterSubblock(bitc::TYPE_SYMTAB_BLOCK_ID, 3);
965 // 7-bit fixed width VST_CODE_ENTRY strings.
966 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
967 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
968 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
969 Log2_32_Ceil(VE.getTypes().size()+1)));
970 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
971 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
972 unsigned V7Abbrev = Stream.EmitAbbrev(Abbv);
974 SmallVector<unsigned, 64> NameVals;
976 for (TypeSymbolTable::const_iterator TI = TST.begin(), TE = TST.end();
978 // TST_ENTRY: [typeid, namechar x N]
979 NameVals.push_back(VE.getTypeID(TI->second));
981 const std::string &Str = TI->first;
983 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
984 NameVals.push_back((unsigned char)Str[i]);
989 // Emit the finished record.
990 Stream.EmitRecord(bitc::VST_CODE_ENTRY, NameVals, is7Bit ? V7Abbrev : 0);
997 // Emit blockinfo, which defines the standard abbreviations etc.
998 static void WriteBlockInfo(const ValueEnumerator &VE, BitstreamWriter &Stream) {
999 // We only want to emit block info records for blocks that have multiple
1000 // instances: CONSTANTS_BLOCK, FUNCTION_BLOCK and VALUE_SYMTAB_BLOCK. Other
1001 // blocks can defined their abbrevs inline.
1002 Stream.EnterBlockInfoBlock(2);
1004 { // 8-bit fixed-width VST_ENTRY/VST_BBENTRY strings.
1005 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1006 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3));
1007 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1008 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1009 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
1010 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
1011 Abbv) != VST_ENTRY_8_ABBREV)
1012 assert(0 && "Unexpected abbrev ordering!");
1015 { // 7-bit fixed width VST_ENTRY strings.
1016 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1017 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
1018 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1019 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1020 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
1021 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
1022 Abbv) != VST_ENTRY_7_ABBREV)
1023 assert(0 && "Unexpected abbrev ordering!");
1025 { // 6-bit char6 VST_ENTRY strings.
1026 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1027 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
1028 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1029 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1030 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
1031 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
1032 Abbv) != VST_ENTRY_6_ABBREV)
1033 assert(0 && "Unexpected abbrev ordering!");
1035 { // 6-bit char6 VST_BBENTRY strings.
1036 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1037 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_BBENTRY));
1038 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1039 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1040 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
1041 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
1042 Abbv) != VST_BBENTRY_6_ABBREV)
1043 assert(0 && "Unexpected abbrev ordering!");
1048 { // SETTYPE abbrev for CONSTANTS_BLOCK.
1049 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1050 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_SETTYPE));
1051 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
1052 Log2_32_Ceil(VE.getTypes().size()+1)));
1053 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
1054 Abbv) != CONSTANTS_SETTYPE_ABBREV)
1055 assert(0 && "Unexpected abbrev ordering!");
1058 { // INTEGER abbrev for CONSTANTS_BLOCK.
1059 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1060 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_INTEGER));
1061 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1062 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
1063 Abbv) != CONSTANTS_INTEGER_ABBREV)
1064 assert(0 && "Unexpected abbrev ordering!");
1067 { // CE_CAST abbrev for CONSTANTS_BLOCK.
1068 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1069 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CE_CAST));
1070 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // cast opc
1071 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // typeid
1072 Log2_32_Ceil(VE.getTypes().size()+1)));
1073 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
1075 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
1076 Abbv) != CONSTANTS_CE_CAST_Abbrev)
1077 assert(0 && "Unexpected abbrev ordering!");
1079 { // NULL abbrev for CONSTANTS_BLOCK.
1080 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1081 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_NULL));
1082 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
1083 Abbv) != CONSTANTS_NULL_Abbrev)
1084 assert(0 && "Unexpected abbrev ordering!");
1087 // FIXME: This should only use space for first class types!
1089 { // INST_LOAD abbrev for FUNCTION_BLOCK.
1090 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1091 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_LOAD));
1092 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Ptr
1093 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // Align
1094 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // volatile
1095 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1096 Abbv) != FUNCTION_INST_LOAD_ABBREV)
1097 assert(0 && "Unexpected abbrev ordering!");
1099 { // INST_BINOP abbrev for FUNCTION_BLOCK.
1100 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1101 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP));
1102 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
1103 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS
1104 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
1105 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1106 Abbv) != FUNCTION_INST_BINOP_ABBREV)
1107 assert(0 && "Unexpected abbrev ordering!");
1109 { // INST_CAST abbrev for FUNCTION_BLOCK.
1110 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1111 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_CAST));
1112 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // OpVal
1113 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
1114 Log2_32_Ceil(VE.getTypes().size()+1)));
1115 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
1116 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1117 Abbv) != FUNCTION_INST_CAST_ABBREV)
1118 assert(0 && "Unexpected abbrev ordering!");
1121 { // INST_RET abbrev for FUNCTION_BLOCK.
1122 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1123 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET));
1124 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1125 Abbv) != FUNCTION_INST_RET_VOID_ABBREV)
1126 assert(0 && "Unexpected abbrev ordering!");
1128 { // INST_RET abbrev for FUNCTION_BLOCK.
1129 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1130 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET));
1131 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ValID
1132 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1133 Abbv) != FUNCTION_INST_RET_VAL_ABBREV)
1134 assert(0 && "Unexpected abbrev ordering!");
1136 { // INST_UNREACHABLE abbrev for FUNCTION_BLOCK.
1137 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1138 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_UNREACHABLE));
1139 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1140 Abbv) != FUNCTION_INST_UNREACHABLE_ABBREV)
1141 assert(0 && "Unexpected abbrev ordering!");
1148 /// WriteModule - Emit the specified module to the bitstream.
1149 static void WriteModule(const Module *M, BitstreamWriter &Stream) {
1150 Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3);
1152 // Emit the version number if it is non-zero.
1154 SmallVector<unsigned, 1> Vals;
1155 Vals.push_back(CurVersion);
1156 Stream.EmitRecord(bitc::MODULE_CODE_VERSION, Vals);
1159 // Analyze the module, enumerating globals, functions, etc.
1160 ValueEnumerator VE(M);
1162 // Emit blockinfo, which defines the standard abbreviations etc.
1163 WriteBlockInfo(VE, Stream);
1165 // Emit information about parameter attributes.
1166 WriteParamAttrTable(VE, Stream);
1168 // Emit information describing all of the types in the module.
1169 WriteTypeTable(VE, Stream);
1171 // Emit top-level description of module, including target triple, inline asm,
1172 // descriptors for global variables, and function prototype info.
1173 WriteModuleInfo(M, VE, Stream);
1176 WriteModuleConstants(VE, Stream);
1178 // If we have any aggregate values in the value table, purge them - these can
1179 // only be used to initialize global variables. Doing so makes the value
1180 // namespace smaller for code in functions.
1181 int NumNonAggregates = VE.PurgeAggregateValues();
1182 if (NumNonAggregates != -1) {
1183 SmallVector<unsigned, 1> Vals;
1184 Vals.push_back(NumNonAggregates);
1185 Stream.EmitRecord(bitc::MODULE_CODE_PURGEVALS, Vals);
1188 // Emit function bodies.
1189 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
1190 if (!I->isDeclaration())
1191 WriteFunction(*I, VE, Stream);
1193 // Emit the type symbol table information.
1194 WriteTypeSymbolTable(M->getTypeSymbolTable(), VE, Stream);
1196 // Emit names for globals/functions etc.
1197 WriteValueSymbolTable(M->getValueSymbolTable(), VE, Stream);
1203 /// WriteBitcodeToFile - Write the specified module to the specified output
1205 void llvm::WriteBitcodeToFile(const Module *M, std::ostream &Out) {
1206 std::vector<unsigned char> Buffer;
1207 BitstreamWriter Stream(Buffer);
1209 Buffer.reserve(256*1024);
1211 // Emit the file header.
1212 Stream.Emit((unsigned)'B', 8);
1213 Stream.Emit((unsigned)'C', 8);
1214 Stream.Emit(0x0, 4);
1215 Stream.Emit(0xC, 4);
1216 Stream.Emit(0xE, 4);
1217 Stream.Emit(0xD, 4);
1220 WriteModule(M, Stream);
1222 // Write the generated bitstream to "Out".
1223 Out.write((char*)&Buffer.front(), Buffer.size());
1225 // Make sure it hits disk now.