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 default: assert(0 && "Unknown type!");
187 case Type::VoidTyID: Code = bitc::TYPE_CODE_VOID; break;
188 case Type::FloatTyID: Code = bitc::TYPE_CODE_FLOAT; break;
189 case Type::DoubleTyID: Code = bitc::TYPE_CODE_DOUBLE; break;
190 case Type::X86_FP80TyID: Code = bitc::TYPE_CODE_X86_FP80; break;
191 case Type::FP128TyID: Code = bitc::TYPE_CODE_FP128; break;
192 case Type::PPC_FP128TyID: Code = bitc::TYPE_CODE_PPC_FP128; break;
193 case Type::LabelTyID: Code = bitc::TYPE_CODE_LABEL; break;
194 case Type::OpaqueTyID: Code = bitc::TYPE_CODE_OPAQUE; break;
195 case Type::IntegerTyID:
197 Code = bitc::TYPE_CODE_INTEGER;
198 TypeVals.push_back(cast<IntegerType>(T)->getBitWidth());
200 case Type::PointerTyID:
201 // POINTER: [pointee type]
202 Code = bitc::TYPE_CODE_POINTER;
203 TypeVals.push_back(VE.getTypeID(cast<PointerType>(T)->getElementType()));
204 AbbrevToUse = PtrAbbrev;
207 case Type::FunctionTyID: {
208 const FunctionType *FT = cast<FunctionType>(T);
209 // FUNCTION: [isvararg, attrid, retty, paramty x N]
210 Code = bitc::TYPE_CODE_FUNCTION;
211 TypeVals.push_back(FT->isVarArg());
212 TypeVals.push_back(VE.getParamAttrID(FT->getParamAttrs()));
213 TypeVals.push_back(VE.getTypeID(FT->getReturnType()));
214 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i)
215 TypeVals.push_back(VE.getTypeID(FT->getParamType(i)));
216 AbbrevToUse = FunctionAbbrev;
219 case Type::StructTyID: {
220 const StructType *ST = cast<StructType>(T);
221 // STRUCT: [ispacked, eltty x N]
222 Code = bitc::TYPE_CODE_STRUCT;
223 TypeVals.push_back(ST->isPacked());
224 // Output all of the element types.
225 for (StructType::element_iterator I = ST->element_begin(),
226 E = ST->element_end(); I != E; ++I)
227 TypeVals.push_back(VE.getTypeID(*I));
228 AbbrevToUse = StructAbbrev;
231 case Type::ArrayTyID: {
232 const ArrayType *AT = cast<ArrayType>(T);
233 // ARRAY: [numelts, eltty]
234 Code = bitc::TYPE_CODE_ARRAY;
235 TypeVals.push_back(AT->getNumElements());
236 TypeVals.push_back(VE.getTypeID(AT->getElementType()));
237 AbbrevToUse = ArrayAbbrev;
240 case Type::VectorTyID: {
241 const VectorType *VT = cast<VectorType>(T);
242 // VECTOR [numelts, eltty]
243 Code = bitc::TYPE_CODE_VECTOR;
244 TypeVals.push_back(VT->getNumElements());
245 TypeVals.push_back(VE.getTypeID(VT->getElementType()));
250 // Emit the finished record.
251 Stream.EmitRecord(Code, TypeVals, AbbrevToUse);
258 static unsigned getEncodedLinkage(const GlobalValue *GV) {
259 switch (GV->getLinkage()) {
260 default: assert(0 && "Invalid linkage!");
261 case GlobalValue::GhostLinkage: // Map ghost linkage onto external.
262 case GlobalValue::ExternalLinkage: return 0;
263 case GlobalValue::WeakLinkage: return 1;
264 case GlobalValue::AppendingLinkage: return 2;
265 case GlobalValue::InternalLinkage: return 3;
266 case GlobalValue::LinkOnceLinkage: return 4;
267 case GlobalValue::DLLImportLinkage: return 5;
268 case GlobalValue::DLLExportLinkage: return 6;
269 case GlobalValue::ExternalWeakLinkage: return 7;
273 static unsigned getEncodedVisibility(const GlobalValue *GV) {
274 switch (GV->getVisibility()) {
275 default: assert(0 && "Invalid visibility!");
276 case GlobalValue::DefaultVisibility: return 0;
277 case GlobalValue::HiddenVisibility: return 1;
278 case GlobalValue::ProtectedVisibility: return 2;
282 // Emit top-level description of module, including target triple, inline asm,
283 // descriptors for global variables, and function prototype info.
284 static void WriteModuleInfo(const Module *M, const ValueEnumerator &VE,
285 BitstreamWriter &Stream) {
286 // Emit the list of dependent libraries for the Module.
287 for (Module::lib_iterator I = M->lib_begin(), E = M->lib_end(); I != E; ++I)
288 WriteStringRecord(bitc::MODULE_CODE_DEPLIB, *I, 0/*TODO*/, Stream);
290 // Emit various pieces of data attached to a module.
291 if (!M->getTargetTriple().empty())
292 WriteStringRecord(bitc::MODULE_CODE_TRIPLE, M->getTargetTriple(),
294 if (!M->getDataLayout().empty())
295 WriteStringRecord(bitc::MODULE_CODE_DATALAYOUT, M->getDataLayout(),
297 if (!M->getModuleInlineAsm().empty())
298 WriteStringRecord(bitc::MODULE_CODE_ASM, M->getModuleInlineAsm(),
301 // Emit information about sections, computing how many there are. Also
302 // compute the maximum alignment value.
303 std::map<std::string, unsigned> SectionMap;
304 unsigned MaxAlignment = 0;
305 unsigned MaxGlobalType = 0;
306 for (Module::const_global_iterator GV = M->global_begin(),E = M->global_end();
308 MaxAlignment = std::max(MaxAlignment, GV->getAlignment());
309 MaxGlobalType = std::max(MaxGlobalType, VE.getTypeID(GV->getType()));
311 if (!GV->hasSection()) continue;
312 // Give section names unique ID's.
313 unsigned &Entry = SectionMap[GV->getSection()];
314 if (Entry != 0) continue;
315 WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, GV->getSection(),
317 Entry = SectionMap.size();
319 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
320 MaxAlignment = std::max(MaxAlignment, F->getAlignment());
321 if (!F->hasSection()) continue;
322 // Give section names unique ID's.
323 unsigned &Entry = SectionMap[F->getSection()];
324 if (Entry != 0) continue;
325 WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, F->getSection(),
327 Entry = SectionMap.size();
330 // Emit abbrev for globals, now that we know # sections and max alignment.
331 unsigned SimpleGVarAbbrev = 0;
332 if (!M->global_empty()) {
333 // Add an abbrev for common globals with no visibility or thread localness.
334 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
335 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_GLOBALVAR));
336 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
337 Log2_32_Ceil(MaxGlobalType+1)));
338 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Constant.
339 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Initializer.
340 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Linkage.
341 if (MaxAlignment == 0) // Alignment.
342 Abbv->Add(BitCodeAbbrevOp(0));
344 unsigned MaxEncAlignment = Log2_32(MaxAlignment)+1;
345 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
346 Log2_32_Ceil(MaxEncAlignment+1)));
348 if (SectionMap.empty()) // Section.
349 Abbv->Add(BitCodeAbbrevOp(0));
351 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
352 Log2_32_Ceil(SectionMap.size()+1)));
353 // Don't bother emitting vis + thread local.
354 SimpleGVarAbbrev = Stream.EmitAbbrev(Abbv);
357 // Emit the global variable information.
358 SmallVector<unsigned, 64> Vals;
359 for (Module::const_global_iterator GV = M->global_begin(),E = M->global_end();
361 unsigned AbbrevToUse = 0;
363 // GLOBALVAR: [type, isconst, initid,
364 // linkage, alignment, section, visibility, threadlocal]
365 Vals.push_back(VE.getTypeID(GV->getType()));
366 Vals.push_back(GV->isConstant());
367 Vals.push_back(GV->isDeclaration() ? 0 :
368 (VE.getValueID(GV->getInitializer()) + 1));
369 Vals.push_back(getEncodedLinkage(GV));
370 Vals.push_back(Log2_32(GV->getAlignment())+1);
371 Vals.push_back(GV->hasSection() ? SectionMap[GV->getSection()] : 0);
372 if (GV->isThreadLocal() ||
373 GV->getVisibility() != GlobalValue::DefaultVisibility) {
374 Vals.push_back(getEncodedVisibility(GV));
375 Vals.push_back(GV->isThreadLocal());
377 AbbrevToUse = SimpleGVarAbbrev;
380 Stream.EmitRecord(bitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse);
384 // Emit the function proto information.
385 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
386 // FUNCTION: [type, callingconv, isproto, linkage, alignment, section,
388 Vals.push_back(VE.getTypeID(F->getType()));
389 Vals.push_back(F->getCallingConv());
390 Vals.push_back(F->isDeclaration());
391 Vals.push_back(getEncodedLinkage(F));
393 // Note: we emit the param attr ID number for the function type of this
394 // function. In the future, we intend for attrs to be properties of
395 // functions, instead of on the type. This is to support this future work.
396 Vals.push_back(VE.getParamAttrID(F->getFunctionType()->getParamAttrs()));
398 Vals.push_back(Log2_32(F->getAlignment())+1);
399 Vals.push_back(F->hasSection() ? SectionMap[F->getSection()] : 0);
400 Vals.push_back(getEncodedVisibility(F));
402 unsigned AbbrevToUse = 0;
403 Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse);
408 // Emit the alias information.
409 for (Module::const_alias_iterator AI = M->alias_begin(), E = M->alias_end();
411 Vals.push_back(VE.getTypeID(AI->getType()));
412 Vals.push_back(VE.getValueID(AI->getAliasee()));
413 Vals.push_back(getEncodedLinkage(AI));
414 unsigned AbbrevToUse = 0;
415 Stream.EmitRecord(bitc::MODULE_CODE_ALIAS, Vals, AbbrevToUse);
421 static void WriteConstants(unsigned FirstVal, unsigned LastVal,
422 const ValueEnumerator &VE,
423 BitstreamWriter &Stream, bool isGlobal) {
424 if (FirstVal == LastVal) return;
426 Stream.EnterSubblock(bitc::CONSTANTS_BLOCK_ID, 4);
428 unsigned AggregateAbbrev = 0;
429 unsigned String8Abbrev = 0;
430 unsigned CString7Abbrev = 0;
431 unsigned CString6Abbrev = 0;
432 // If this is a constant pool for the module, emit module-specific abbrevs.
434 // Abbrev for CST_CODE_AGGREGATE.
435 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
436 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_AGGREGATE));
437 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
438 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, Log2_32_Ceil(LastVal+1)));
439 AggregateAbbrev = Stream.EmitAbbrev(Abbv);
441 // Abbrev for CST_CODE_STRING.
442 Abbv = new BitCodeAbbrev();
443 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_STRING));
444 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
445 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
446 String8Abbrev = Stream.EmitAbbrev(Abbv);
447 // Abbrev for CST_CODE_CSTRING.
448 Abbv = new BitCodeAbbrev();
449 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
450 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
451 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
452 CString7Abbrev = Stream.EmitAbbrev(Abbv);
453 // Abbrev for CST_CODE_CSTRING.
454 Abbv = new BitCodeAbbrev();
455 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
456 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
457 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
458 CString6Abbrev = Stream.EmitAbbrev(Abbv);
461 SmallVector<uint64_t, 64> Record;
463 const ValueEnumerator::ValueList &Vals = VE.getValues();
464 const Type *LastTy = 0;
465 for (unsigned i = FirstVal; i != LastVal; ++i) {
466 const Value *V = Vals[i].first;
467 // If we need to switch types, do so now.
468 if (V->getType() != LastTy) {
469 LastTy = V->getType();
470 Record.push_back(VE.getTypeID(LastTy));
471 Stream.EmitRecord(bitc::CST_CODE_SETTYPE, Record,
472 CONSTANTS_SETTYPE_ABBREV);
476 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
477 Record.push_back(unsigned(IA->hasSideEffects()));
479 // Add the asm string.
480 const std::string &AsmStr = IA->getAsmString();
481 Record.push_back(AsmStr.size());
482 for (unsigned i = 0, e = AsmStr.size(); i != e; ++i)
483 Record.push_back(AsmStr[i]);
485 // Add the constraint string.
486 const std::string &ConstraintStr = IA->getConstraintString();
487 Record.push_back(ConstraintStr.size());
488 for (unsigned i = 0, e = ConstraintStr.size(); i != e; ++i)
489 Record.push_back(ConstraintStr[i]);
490 Stream.EmitRecord(bitc::CST_CODE_INLINEASM, Record);
494 const Constant *C = cast<Constant>(V);
496 unsigned AbbrevToUse = 0;
497 if (C->isNullValue()) {
498 Code = bitc::CST_CODE_NULL;
499 } else if (isa<UndefValue>(C)) {
500 Code = bitc::CST_CODE_UNDEF;
501 } else if (const ConstantInt *IV = dyn_cast<ConstantInt>(C)) {
502 if (IV->getBitWidth() <= 64) {
503 int64_t V = IV->getSExtValue();
505 Record.push_back(V << 1);
507 Record.push_back((-V << 1) | 1);
508 Code = bitc::CST_CODE_INTEGER;
509 AbbrevToUse = CONSTANTS_INTEGER_ABBREV;
510 } else { // Wide integers, > 64 bits in size.
511 // We have an arbitrary precision integer value to write whose
512 // bit width is > 64. However, in canonical unsigned integer
513 // format it is likely that the high bits are going to be zero.
514 // So, we only write the number of active words.
515 unsigned NWords = IV->getValue().getActiveWords();
516 const uint64_t *RawWords = IV->getValue().getRawData();
517 for (unsigned i = 0; i != NWords; ++i) {
518 int64_t V = RawWords[i];
520 Record.push_back(V << 1);
522 Record.push_back((-V << 1) | 1);
524 Code = bitc::CST_CODE_WIDE_INTEGER;
526 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
527 Code = bitc::CST_CODE_FLOAT;
528 const Type *Ty = CFP->getType();
529 if (Ty == Type::FloatTy) {
530 Record.push_back(FloatToBits((float)CFP->getValue()));
531 } else if (Ty == Type::DoubleTy) {
532 Record.push_back(DoubleToBits((double)CFP->getValue()));
533 // FIXME: make long double constants work.
534 } else if (Ty == Type::X86_FP80Ty ||
535 Ty == Type::FP128Ty || Ty == Type::PPC_FP128Ty) {
536 assert (0 && "Long double constants not handled yet.");
538 assert (0 && "Unknown FP type!");
540 } else if (isa<ConstantArray>(C) && cast<ConstantArray>(C)->isString()) {
541 // Emit constant strings specially.
542 unsigned NumOps = C->getNumOperands();
543 // If this is a null-terminated string, use the denser CSTRING encoding.
544 if (C->getOperand(NumOps-1)->isNullValue()) {
545 Code = bitc::CST_CODE_CSTRING;
546 --NumOps; // Don't encode the null, which isn't allowed by char6.
548 Code = bitc::CST_CODE_STRING;
549 AbbrevToUse = String8Abbrev;
551 bool isCStr7 = Code == bitc::CST_CODE_CSTRING;
552 bool isCStrChar6 = Code == bitc::CST_CODE_CSTRING;
553 for (unsigned i = 0; i != NumOps; ++i) {
554 unsigned char V = cast<ConstantInt>(C->getOperand(i))->getZExtValue();
556 isCStr7 &= (V & 128) == 0;
558 isCStrChar6 = BitCodeAbbrevOp::isChar6(V);
562 AbbrevToUse = CString6Abbrev;
564 AbbrevToUse = CString7Abbrev;
565 } else if (isa<ConstantArray>(C) || isa<ConstantStruct>(V) ||
566 isa<ConstantVector>(V)) {
567 Code = bitc::CST_CODE_AGGREGATE;
568 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
569 Record.push_back(VE.getValueID(C->getOperand(i)));
570 AbbrevToUse = AggregateAbbrev;
571 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
572 switch (CE->getOpcode()) {
574 if (Instruction::isCast(CE->getOpcode())) {
575 Code = bitc::CST_CODE_CE_CAST;
576 Record.push_back(GetEncodedCastOpcode(CE->getOpcode()));
577 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
578 Record.push_back(VE.getValueID(C->getOperand(0)));
579 AbbrevToUse = CONSTANTS_CE_CAST_Abbrev;
581 assert(CE->getNumOperands() == 2 && "Unknown constant expr!");
582 Code = bitc::CST_CODE_CE_BINOP;
583 Record.push_back(GetEncodedBinaryOpcode(CE->getOpcode()));
584 Record.push_back(VE.getValueID(C->getOperand(0)));
585 Record.push_back(VE.getValueID(C->getOperand(1)));
588 case Instruction::GetElementPtr:
589 Code = bitc::CST_CODE_CE_GEP;
590 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) {
591 Record.push_back(VE.getTypeID(C->getOperand(i)->getType()));
592 Record.push_back(VE.getValueID(C->getOperand(i)));
595 case Instruction::Select:
596 Code = bitc::CST_CODE_CE_SELECT;
597 Record.push_back(VE.getValueID(C->getOperand(0)));
598 Record.push_back(VE.getValueID(C->getOperand(1)));
599 Record.push_back(VE.getValueID(C->getOperand(2)));
601 case Instruction::ExtractElement:
602 Code = bitc::CST_CODE_CE_EXTRACTELT;
603 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
604 Record.push_back(VE.getValueID(C->getOperand(0)));
605 Record.push_back(VE.getValueID(C->getOperand(1)));
607 case Instruction::InsertElement:
608 Code = bitc::CST_CODE_CE_INSERTELT;
609 Record.push_back(VE.getValueID(C->getOperand(0)));
610 Record.push_back(VE.getValueID(C->getOperand(1)));
611 Record.push_back(VE.getValueID(C->getOperand(2)));
613 case Instruction::ShuffleVector:
614 Code = bitc::CST_CODE_CE_SHUFFLEVEC;
615 Record.push_back(VE.getValueID(C->getOperand(0)));
616 Record.push_back(VE.getValueID(C->getOperand(1)));
617 Record.push_back(VE.getValueID(C->getOperand(2)));
619 case Instruction::ICmp:
620 case Instruction::FCmp:
621 Code = bitc::CST_CODE_CE_CMP;
622 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
623 Record.push_back(VE.getValueID(C->getOperand(0)));
624 Record.push_back(VE.getValueID(C->getOperand(1)));
625 Record.push_back(CE->getPredicate());
629 assert(0 && "Unknown constant!");
631 Stream.EmitRecord(Code, Record, AbbrevToUse);
638 static void WriteModuleConstants(const ValueEnumerator &VE,
639 BitstreamWriter &Stream) {
640 const ValueEnumerator::ValueList &Vals = VE.getValues();
642 // Find the first constant to emit, which is the first non-globalvalue value.
643 // We know globalvalues have been emitted by WriteModuleInfo.
644 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
645 if (!isa<GlobalValue>(Vals[i].first)) {
646 WriteConstants(i, Vals.size(), VE, Stream, true);
652 /// PushValueAndType - The file has to encode both the value and type id for
653 /// many values, because we need to know what type to create for forward
654 /// references. However, most operands are not forward references, so this type
655 /// field is not needed.
657 /// This function adds V's value ID to Vals. If the value ID is higher than the
658 /// instruction ID, then it is a forward reference, and it also includes the
660 static bool PushValueAndType(Value *V, unsigned InstID,
661 SmallVector<unsigned, 64> &Vals,
662 ValueEnumerator &VE) {
663 unsigned ValID = VE.getValueID(V);
664 Vals.push_back(ValID);
665 if (ValID >= InstID) {
666 Vals.push_back(VE.getTypeID(V->getType()));
672 /// WriteInstruction - Emit an instruction to the specified stream.
673 static void WriteInstruction(const Instruction &I, unsigned InstID,
674 ValueEnumerator &VE, BitstreamWriter &Stream,
675 SmallVector<unsigned, 64> &Vals) {
677 unsigned AbbrevToUse = 0;
678 switch (I.getOpcode()) {
680 if (Instruction::isCast(I.getOpcode())) {
681 Code = bitc::FUNC_CODE_INST_CAST;
682 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE))
683 AbbrevToUse = FUNCTION_INST_CAST_ABBREV;
684 Vals.push_back(VE.getTypeID(I.getType()));
685 Vals.push_back(GetEncodedCastOpcode(I.getOpcode()));
687 assert(isa<BinaryOperator>(I) && "Unknown instruction!");
688 Code = bitc::FUNC_CODE_INST_BINOP;
689 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE))
690 AbbrevToUse = FUNCTION_INST_BINOP_ABBREV;
691 Vals.push_back(VE.getValueID(I.getOperand(1)));
692 Vals.push_back(GetEncodedBinaryOpcode(I.getOpcode()));
696 case Instruction::GetElementPtr:
697 Code = bitc::FUNC_CODE_INST_GEP;
698 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
699 PushValueAndType(I.getOperand(i), InstID, Vals, VE);
701 case Instruction::Select:
702 Code = bitc::FUNC_CODE_INST_SELECT;
703 PushValueAndType(I.getOperand(1), InstID, Vals, VE);
704 Vals.push_back(VE.getValueID(I.getOperand(2)));
705 Vals.push_back(VE.getValueID(I.getOperand(0)));
707 case Instruction::ExtractElement:
708 Code = bitc::FUNC_CODE_INST_EXTRACTELT;
709 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
710 Vals.push_back(VE.getValueID(I.getOperand(1)));
712 case Instruction::InsertElement:
713 Code = bitc::FUNC_CODE_INST_INSERTELT;
714 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
715 Vals.push_back(VE.getValueID(I.getOperand(1)));
716 Vals.push_back(VE.getValueID(I.getOperand(2)));
718 case Instruction::ShuffleVector:
719 Code = bitc::FUNC_CODE_INST_SHUFFLEVEC;
720 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
721 Vals.push_back(VE.getValueID(I.getOperand(1)));
722 Vals.push_back(VE.getValueID(I.getOperand(2)));
724 case Instruction::ICmp:
725 case Instruction::FCmp:
726 Code = bitc::FUNC_CODE_INST_CMP;
727 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
728 Vals.push_back(VE.getValueID(I.getOperand(1)));
729 Vals.push_back(cast<CmpInst>(I).getPredicate());
732 case Instruction::Ret:
733 Code = bitc::FUNC_CODE_INST_RET;
734 if (!I.getNumOperands())
735 AbbrevToUse = FUNCTION_INST_RET_VOID_ABBREV;
736 else if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE))
737 AbbrevToUse = FUNCTION_INST_RET_VAL_ABBREV;
739 case Instruction::Br:
740 Code = bitc::FUNC_CODE_INST_BR;
741 Vals.push_back(VE.getValueID(I.getOperand(0)));
742 if (cast<BranchInst>(I).isConditional()) {
743 Vals.push_back(VE.getValueID(I.getOperand(1)));
744 Vals.push_back(VE.getValueID(I.getOperand(2)));
747 case Instruction::Switch:
748 Code = bitc::FUNC_CODE_INST_SWITCH;
749 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
750 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
751 Vals.push_back(VE.getValueID(I.getOperand(i)));
753 case Instruction::Invoke: {
754 const PointerType *PTy = cast<PointerType>(I.getOperand(0)->getType());
755 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
756 Code = bitc::FUNC_CODE_INST_INVOKE;
758 // Note: we emit the param attr ID number for the function type of this
759 // function. In the future, we intend for attrs to be properties of
760 // functions, instead of on the type. This is to support this future work.
761 Vals.push_back(VE.getParamAttrID(FTy->getParamAttrs()));
763 Vals.push_back(cast<InvokeInst>(I).getCallingConv());
764 Vals.push_back(VE.getValueID(I.getOperand(1))); // normal dest
765 Vals.push_back(VE.getValueID(I.getOperand(2))); // unwind dest
766 PushValueAndType(I.getOperand(0), InstID, Vals, VE); // callee
768 // Emit value #'s for the fixed parameters.
769 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
770 Vals.push_back(VE.getValueID(I.getOperand(i+3))); // fixed param.
772 // Emit type/value pairs for varargs params.
773 if (FTy->isVarArg()) {
774 for (unsigned i = 3+FTy->getNumParams(), e = I.getNumOperands();
776 PushValueAndType(I.getOperand(i), InstID, Vals, VE); // vararg
780 case Instruction::Unwind:
781 Code = bitc::FUNC_CODE_INST_UNWIND;
783 case Instruction::Unreachable:
784 Code = bitc::FUNC_CODE_INST_UNREACHABLE;
785 AbbrevToUse = FUNCTION_INST_UNREACHABLE_ABBREV;
788 case Instruction::PHI:
789 Code = bitc::FUNC_CODE_INST_PHI;
790 Vals.push_back(VE.getTypeID(I.getType()));
791 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
792 Vals.push_back(VE.getValueID(I.getOperand(i)));
795 case Instruction::Malloc:
796 Code = bitc::FUNC_CODE_INST_MALLOC;
797 Vals.push_back(VE.getTypeID(I.getType()));
798 Vals.push_back(VE.getValueID(I.getOperand(0))); // size.
799 Vals.push_back(Log2_32(cast<MallocInst>(I).getAlignment())+1);
802 case Instruction::Free:
803 Code = bitc::FUNC_CODE_INST_FREE;
804 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
807 case Instruction::Alloca:
808 Code = bitc::FUNC_CODE_INST_ALLOCA;
809 Vals.push_back(VE.getTypeID(I.getType()));
810 Vals.push_back(VE.getValueID(I.getOperand(0))); // size.
811 Vals.push_back(Log2_32(cast<AllocaInst>(I).getAlignment())+1);
814 case Instruction::Load:
815 Code = bitc::FUNC_CODE_INST_LOAD;
816 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE)) // ptr
817 AbbrevToUse = FUNCTION_INST_LOAD_ABBREV;
819 Vals.push_back(Log2_32(cast<LoadInst>(I).getAlignment())+1);
820 Vals.push_back(cast<LoadInst>(I).isVolatile());
822 case Instruction::Store:
823 Code = bitc::FUNC_CODE_INST_STORE;
824 PushValueAndType(I.getOperand(0), InstID, Vals, VE); // val.
825 Vals.push_back(VE.getValueID(I.getOperand(1))); // ptr.
826 Vals.push_back(Log2_32(cast<StoreInst>(I).getAlignment())+1);
827 Vals.push_back(cast<StoreInst>(I).isVolatile());
829 case Instruction::Call: {
830 const PointerType *PTy = cast<PointerType>(I.getOperand(0)->getType());
831 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
833 Code = bitc::FUNC_CODE_INST_CALL;
835 // Note: we emit the param attr ID number for the function type of this
836 // function. In the future, we intend for attrs to be properties of
837 // functions, instead of on the type. This is to support this future work.
838 Vals.push_back(VE.getParamAttrID(FTy->getParamAttrs()));
840 Vals.push_back((cast<CallInst>(I).getCallingConv() << 1) |
841 unsigned(cast<CallInst>(I).isTailCall()));
842 PushValueAndType(I.getOperand(0), InstID, Vals, VE); // Callee
844 // Emit value #'s for the fixed parameters.
845 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
846 Vals.push_back(VE.getValueID(I.getOperand(i+1))); // fixed param.
848 // Emit type/value pairs for varargs params.
849 if (FTy->isVarArg()) {
850 unsigned NumVarargs = I.getNumOperands()-1-FTy->getNumParams();
851 for (unsigned i = I.getNumOperands()-NumVarargs, e = I.getNumOperands();
853 PushValueAndType(I.getOperand(i), InstID, Vals, VE); // varargs
857 case Instruction::VAArg:
858 Code = bitc::FUNC_CODE_INST_VAARG;
859 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); // valistty
860 Vals.push_back(VE.getValueID(I.getOperand(0))); // valist.
861 Vals.push_back(VE.getTypeID(I.getType())); // restype.
865 Stream.EmitRecord(Code, Vals, AbbrevToUse);
869 // Emit names for globals/functions etc.
870 static void WriteValueSymbolTable(const ValueSymbolTable &VST,
871 const ValueEnumerator &VE,
872 BitstreamWriter &Stream) {
873 if (VST.empty()) return;
874 Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4);
876 // FIXME: Set up the abbrev, we know how many values there are!
877 // FIXME: We know if the type names can use 7-bit ascii.
878 SmallVector<unsigned, 64> NameVals;
880 for (ValueSymbolTable::const_iterator SI = VST.begin(), SE = VST.end();
883 const ValueName &Name = *SI;
885 // Figure out the encoding to use for the name.
888 for (const char *C = Name.getKeyData(), *E = C+Name.getKeyLength();
891 isChar6 = BitCodeAbbrevOp::isChar6(*C);
892 if ((unsigned char)*C & 128) {
894 break; // don't bother scanning the rest.
898 unsigned AbbrevToUse = VST_ENTRY_8_ABBREV;
900 // VST_ENTRY: [valueid, namechar x N]
901 // VST_BBENTRY: [bbid, namechar x N]
903 if (isa<BasicBlock>(SI->getValue())) {
904 Code = bitc::VST_CODE_BBENTRY;
906 AbbrevToUse = VST_BBENTRY_6_ABBREV;
908 Code = bitc::VST_CODE_ENTRY;
910 AbbrevToUse = VST_ENTRY_6_ABBREV;
912 AbbrevToUse = VST_ENTRY_7_ABBREV;
915 NameVals.push_back(VE.getValueID(SI->getValue()));
916 for (const char *P = Name.getKeyData(),
917 *E = Name.getKeyData()+Name.getKeyLength(); P != E; ++P)
918 NameVals.push_back((unsigned char)*P);
920 // Emit the finished record.
921 Stream.EmitRecord(Code, NameVals, AbbrevToUse);
927 /// WriteFunction - Emit a function body to the module stream.
928 static void WriteFunction(const Function &F, ValueEnumerator &VE,
929 BitstreamWriter &Stream) {
930 Stream.EnterSubblock(bitc::FUNCTION_BLOCK_ID, 4);
931 VE.incorporateFunction(F);
933 SmallVector<unsigned, 64> Vals;
935 // Emit the number of basic blocks, so the reader can create them ahead of
937 Vals.push_back(VE.getBasicBlocks().size());
938 Stream.EmitRecord(bitc::FUNC_CODE_DECLAREBLOCKS, Vals);
941 // If there are function-local constants, emit them now.
942 unsigned CstStart, CstEnd;
943 VE.getFunctionConstantRange(CstStart, CstEnd);
944 WriteConstants(CstStart, CstEnd, VE, Stream, false);
946 // Keep a running idea of what the instruction ID is.
947 unsigned InstID = CstEnd;
949 // Finally, emit all the instructions, in order.
950 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
951 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
953 WriteInstruction(*I, InstID, VE, Stream, Vals);
954 if (I->getType() != Type::VoidTy)
958 // Emit names for all the instructions etc.
959 WriteValueSymbolTable(F.getValueSymbolTable(), VE, Stream);
965 /// WriteTypeSymbolTable - Emit a block for the specified type symtab.
966 static void WriteTypeSymbolTable(const TypeSymbolTable &TST,
967 const ValueEnumerator &VE,
968 BitstreamWriter &Stream) {
969 if (TST.empty()) return;
971 Stream.EnterSubblock(bitc::TYPE_SYMTAB_BLOCK_ID, 3);
973 // 7-bit fixed width VST_CODE_ENTRY strings.
974 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
975 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
976 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
977 Log2_32_Ceil(VE.getTypes().size()+1)));
978 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
979 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
980 unsigned V7Abbrev = Stream.EmitAbbrev(Abbv);
982 SmallVector<unsigned, 64> NameVals;
984 for (TypeSymbolTable::const_iterator TI = TST.begin(), TE = TST.end();
986 // TST_ENTRY: [typeid, namechar x N]
987 NameVals.push_back(VE.getTypeID(TI->second));
989 const std::string &Str = TI->first;
991 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
992 NameVals.push_back((unsigned char)Str[i]);
997 // Emit the finished record.
998 Stream.EmitRecord(bitc::VST_CODE_ENTRY, NameVals, is7Bit ? V7Abbrev : 0);
1005 // Emit blockinfo, which defines the standard abbreviations etc.
1006 static void WriteBlockInfo(const ValueEnumerator &VE, BitstreamWriter &Stream) {
1007 // We only want to emit block info records for blocks that have multiple
1008 // instances: CONSTANTS_BLOCK, FUNCTION_BLOCK and VALUE_SYMTAB_BLOCK. Other
1009 // blocks can defined their abbrevs inline.
1010 Stream.EnterBlockInfoBlock(2);
1012 { // 8-bit fixed-width VST_ENTRY/VST_BBENTRY strings.
1013 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1014 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3));
1015 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1016 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1017 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
1018 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
1019 Abbv) != VST_ENTRY_8_ABBREV)
1020 assert(0 && "Unexpected abbrev ordering!");
1023 { // 7-bit fixed width VST_ENTRY strings.
1024 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1025 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
1026 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1027 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1028 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
1029 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
1030 Abbv) != VST_ENTRY_7_ABBREV)
1031 assert(0 && "Unexpected abbrev ordering!");
1033 { // 6-bit char6 VST_ENTRY strings.
1034 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1035 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
1036 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1037 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1038 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
1039 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
1040 Abbv) != VST_ENTRY_6_ABBREV)
1041 assert(0 && "Unexpected abbrev ordering!");
1043 { // 6-bit char6 VST_BBENTRY strings.
1044 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1045 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_BBENTRY));
1046 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1047 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1048 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
1049 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
1050 Abbv) != VST_BBENTRY_6_ABBREV)
1051 assert(0 && "Unexpected abbrev ordering!");
1056 { // SETTYPE abbrev for CONSTANTS_BLOCK.
1057 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1058 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_SETTYPE));
1059 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
1060 Log2_32_Ceil(VE.getTypes().size()+1)));
1061 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
1062 Abbv) != CONSTANTS_SETTYPE_ABBREV)
1063 assert(0 && "Unexpected abbrev ordering!");
1066 { // INTEGER abbrev for CONSTANTS_BLOCK.
1067 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1068 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_INTEGER));
1069 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1070 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
1071 Abbv) != CONSTANTS_INTEGER_ABBREV)
1072 assert(0 && "Unexpected abbrev ordering!");
1075 { // CE_CAST abbrev for CONSTANTS_BLOCK.
1076 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1077 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CE_CAST));
1078 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // cast opc
1079 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // typeid
1080 Log2_32_Ceil(VE.getTypes().size()+1)));
1081 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
1083 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
1084 Abbv) != CONSTANTS_CE_CAST_Abbrev)
1085 assert(0 && "Unexpected abbrev ordering!");
1087 { // NULL abbrev for CONSTANTS_BLOCK.
1088 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1089 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_NULL));
1090 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
1091 Abbv) != CONSTANTS_NULL_Abbrev)
1092 assert(0 && "Unexpected abbrev ordering!");
1095 // FIXME: This should only use space for first class types!
1097 { // INST_LOAD abbrev for FUNCTION_BLOCK.
1098 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1099 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_LOAD));
1100 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Ptr
1101 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // Align
1102 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // volatile
1103 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1104 Abbv) != FUNCTION_INST_LOAD_ABBREV)
1105 assert(0 && "Unexpected abbrev ordering!");
1107 { // INST_BINOP abbrev for FUNCTION_BLOCK.
1108 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1109 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP));
1110 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
1111 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS
1112 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
1113 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1114 Abbv) != FUNCTION_INST_BINOP_ABBREV)
1115 assert(0 && "Unexpected abbrev ordering!");
1117 { // INST_CAST abbrev for FUNCTION_BLOCK.
1118 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1119 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_CAST));
1120 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // OpVal
1121 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
1122 Log2_32_Ceil(VE.getTypes().size()+1)));
1123 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
1124 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1125 Abbv) != FUNCTION_INST_CAST_ABBREV)
1126 assert(0 && "Unexpected abbrev ordering!");
1129 { // INST_RET abbrev for FUNCTION_BLOCK.
1130 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1131 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET));
1132 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1133 Abbv) != FUNCTION_INST_RET_VOID_ABBREV)
1134 assert(0 && "Unexpected abbrev ordering!");
1136 { // INST_RET abbrev for FUNCTION_BLOCK.
1137 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1138 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET));
1139 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ValID
1140 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1141 Abbv) != FUNCTION_INST_RET_VAL_ABBREV)
1142 assert(0 && "Unexpected abbrev ordering!");
1144 { // INST_UNREACHABLE abbrev for FUNCTION_BLOCK.
1145 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1146 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_UNREACHABLE));
1147 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1148 Abbv) != FUNCTION_INST_UNREACHABLE_ABBREV)
1149 assert(0 && "Unexpected abbrev ordering!");
1156 /// WriteModule - Emit the specified module to the bitstream.
1157 static void WriteModule(const Module *M, BitstreamWriter &Stream) {
1158 Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3);
1160 // Emit the version number if it is non-zero.
1162 SmallVector<unsigned, 1> Vals;
1163 Vals.push_back(CurVersion);
1164 Stream.EmitRecord(bitc::MODULE_CODE_VERSION, Vals);
1167 // Analyze the module, enumerating globals, functions, etc.
1168 ValueEnumerator VE(M);
1170 // Emit blockinfo, which defines the standard abbreviations etc.
1171 WriteBlockInfo(VE, Stream);
1173 // Emit information about parameter attributes.
1174 WriteParamAttrTable(VE, Stream);
1176 // Emit information describing all of the types in the module.
1177 WriteTypeTable(VE, Stream);
1179 // Emit top-level description of module, including target triple, inline asm,
1180 // descriptors for global variables, and function prototype info.
1181 WriteModuleInfo(M, VE, Stream);
1184 WriteModuleConstants(VE, Stream);
1186 // If we have any aggregate values in the value table, purge them - these can
1187 // only be used to initialize global variables. Doing so makes the value
1188 // namespace smaller for code in functions.
1189 int NumNonAggregates = VE.PurgeAggregateValues();
1190 if (NumNonAggregates != -1) {
1191 SmallVector<unsigned, 1> Vals;
1192 Vals.push_back(NumNonAggregates);
1193 Stream.EmitRecord(bitc::MODULE_CODE_PURGEVALS, Vals);
1196 // Emit function bodies.
1197 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
1198 if (!I->isDeclaration())
1199 WriteFunction(*I, VE, Stream);
1201 // Emit the type symbol table information.
1202 WriteTypeSymbolTable(M->getTypeSymbolTable(), VE, Stream);
1204 // Emit names for globals/functions etc.
1205 WriteValueSymbolTable(M->getValueSymbolTable(), VE, Stream);
1211 /// WriteBitcodeToFile - Write the specified module to the specified output
1213 void llvm::WriteBitcodeToFile(const Module *M, std::ostream &Out) {
1214 std::vector<unsigned char> Buffer;
1215 BitstreamWriter Stream(Buffer);
1217 Buffer.reserve(256*1024);
1219 // Emit the file header.
1220 Stream.Emit((unsigned)'B', 8);
1221 Stream.Emit((unsigned)'C', 8);
1222 Stream.Emit(0x0, 4);
1223 Stream.Emit(0xC, 4);
1224 Stream.Emit(0xE, 4);
1225 Stream.Emit(0xD, 4);
1228 WriteModule(M, Stream);
1230 // Write the generated bitstream to "Out".
1231 Out.write((char*)&Buffer.front(), Buffer.size());
1233 // Make sure it hits disk now.