BitcodeWriter.cpp revision d711dec946b6408791ca59eb98e363ef04bbd4aa
1//===--- Bitcode/Writer/BitcodeWriter.cpp - Bitcode Writer ----------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// Bitcode writer implementation. 11// 12//===----------------------------------------------------------------------===// 13 14#include "ReaderWriter_3_2.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/Operator.h" 24#include "llvm/ValueSymbolTable.h" 25#include "llvm/ADT/Triple.h" 26#include "llvm/Support/CommandLine.h" 27#include "llvm/Support/ErrorHandling.h" 28#include "llvm/Support/MathExtras.h" 29#include "llvm/Support/raw_ostream.h" 30#include "llvm/Support/Program.h" 31#include <cctype> 32#include <map> 33using namespace llvm; 34 35static cl::opt<bool> 36EnablePreserveUseListOrdering("enable-bc-uselist-preserve", 37 cl::desc("Turn on experimental support for " 38 "use-list order preservation."), 39 cl::init(false), cl::Hidden); 40 41/// These are manifest constants used by the bitcode writer. They do not need to 42/// be kept in sync with the reader, but need to be consistent within this file. 43enum { 44 CurVersion = 0, 45 46 // VALUE_SYMTAB_BLOCK abbrev id's. 47 VST_ENTRY_8_ABBREV = bitc::FIRST_APPLICATION_ABBREV, 48 VST_ENTRY_7_ABBREV, 49 VST_ENTRY_6_ABBREV, 50 VST_BBENTRY_6_ABBREV, 51 52 // CONSTANTS_BLOCK abbrev id's. 53 CONSTANTS_SETTYPE_ABBREV = bitc::FIRST_APPLICATION_ABBREV, 54 CONSTANTS_INTEGER_ABBREV, 55 CONSTANTS_CE_CAST_Abbrev, 56 CONSTANTS_NULL_Abbrev, 57 58 // FUNCTION_BLOCK abbrev id's. 59 FUNCTION_INST_LOAD_ABBREV = bitc::FIRST_APPLICATION_ABBREV, 60 FUNCTION_INST_BINOP_ABBREV, 61 FUNCTION_INST_BINOP_FLAGS_ABBREV, 62 FUNCTION_INST_CAST_ABBREV, 63 FUNCTION_INST_RET_VOID_ABBREV, 64 FUNCTION_INST_RET_VAL_ABBREV, 65 FUNCTION_INST_UNREACHABLE_ABBREV, 66 67 // SwitchInst Magic 68 SWITCH_INST_MAGIC = 0x4B5 // May 2012 => 1205 => Hex 69}; 70 71static unsigned GetEncodedCastOpcode(unsigned Opcode) { 72 switch (Opcode) { 73 default: llvm_unreachable("Unknown cast instruction!"); 74 case Instruction::Trunc : return bitc::CAST_TRUNC; 75 case Instruction::ZExt : return bitc::CAST_ZEXT; 76 case Instruction::SExt : return bitc::CAST_SEXT; 77 case Instruction::FPToUI : return bitc::CAST_FPTOUI; 78 case Instruction::FPToSI : return bitc::CAST_FPTOSI; 79 case Instruction::UIToFP : return bitc::CAST_UITOFP; 80 case Instruction::SIToFP : return bitc::CAST_SITOFP; 81 case Instruction::FPTrunc : return bitc::CAST_FPTRUNC; 82 case Instruction::FPExt : return bitc::CAST_FPEXT; 83 case Instruction::PtrToInt: return bitc::CAST_PTRTOINT; 84 case Instruction::IntToPtr: return bitc::CAST_INTTOPTR; 85 case Instruction::BitCast : return bitc::CAST_BITCAST; 86 } 87} 88 89static unsigned GetEncodedBinaryOpcode(unsigned Opcode) { 90 switch (Opcode) { 91 default: llvm_unreachable("Unknown binary instruction!"); 92 case Instruction::Add: 93 case Instruction::FAdd: return bitc::BINOP_ADD; 94 case Instruction::Sub: 95 case Instruction::FSub: return bitc::BINOP_SUB; 96 case Instruction::Mul: 97 case Instruction::FMul: return bitc::BINOP_MUL; 98 case Instruction::UDiv: return bitc::BINOP_UDIV; 99 case Instruction::FDiv: 100 case Instruction::SDiv: return bitc::BINOP_SDIV; 101 case Instruction::URem: return bitc::BINOP_UREM; 102 case Instruction::FRem: 103 case Instruction::SRem: return bitc::BINOP_SREM; 104 case Instruction::Shl: return bitc::BINOP_SHL; 105 case Instruction::LShr: return bitc::BINOP_LSHR; 106 case Instruction::AShr: return bitc::BINOP_ASHR; 107 case Instruction::And: return bitc::BINOP_AND; 108 case Instruction::Or: return bitc::BINOP_OR; 109 case Instruction::Xor: return bitc::BINOP_XOR; 110 } 111} 112 113static unsigned GetEncodedRMWOperation(AtomicRMWInst::BinOp Op) { 114 switch (Op) { 115 default: llvm_unreachable("Unknown RMW operation!"); 116 case AtomicRMWInst::Xchg: return bitc::RMW_XCHG; 117 case AtomicRMWInst::Add: return bitc::RMW_ADD; 118 case AtomicRMWInst::Sub: return bitc::RMW_SUB; 119 case AtomicRMWInst::And: return bitc::RMW_AND; 120 case AtomicRMWInst::Nand: return bitc::RMW_NAND; 121 case AtomicRMWInst::Or: return bitc::RMW_OR; 122 case AtomicRMWInst::Xor: return bitc::RMW_XOR; 123 case AtomicRMWInst::Max: return bitc::RMW_MAX; 124 case AtomicRMWInst::Min: return bitc::RMW_MIN; 125 case AtomicRMWInst::UMax: return bitc::RMW_UMAX; 126 case AtomicRMWInst::UMin: return bitc::RMW_UMIN; 127 } 128} 129 130static unsigned GetEncodedOrdering(AtomicOrdering Ordering) { 131 switch (Ordering) { 132 case NotAtomic: return bitc::ORDERING_NOTATOMIC; 133 case Unordered: return bitc::ORDERING_UNORDERED; 134 case Monotonic: return bitc::ORDERING_MONOTONIC; 135 case Acquire: return bitc::ORDERING_ACQUIRE; 136 case Release: return bitc::ORDERING_RELEASE; 137 case AcquireRelease: return bitc::ORDERING_ACQREL; 138 case SequentiallyConsistent: return bitc::ORDERING_SEQCST; 139 } 140 llvm_unreachable("Invalid ordering"); 141} 142 143static unsigned GetEncodedSynchScope(SynchronizationScope SynchScope) { 144 switch (SynchScope) { 145 case SingleThread: return bitc::SYNCHSCOPE_SINGLETHREAD; 146 case CrossThread: return bitc::SYNCHSCOPE_CROSSTHREAD; 147 } 148 llvm_unreachable("Invalid synch scope"); 149} 150 151static void WriteStringRecord(unsigned Code, StringRef Str, 152 unsigned AbbrevToUse, BitstreamWriter &Stream) { 153 SmallVector<unsigned, 64> Vals; 154 155 // Code: [strchar x N] 156 for (unsigned i = 0, e = Str.size(); i != e; ++i) { 157 if (AbbrevToUse && !BitCodeAbbrevOp::isChar6(Str[i])) 158 AbbrevToUse = 0; 159 Vals.push_back(Str[i]); 160 } 161 162 // Emit the finished record. 163 Stream.EmitRecord(Code, Vals, AbbrevToUse); 164} 165 166// Emit information about parameter attributes. 167static void WriteAttributeTable(const llvm_3_2::ValueEnumerator &VE, 168 BitstreamWriter &Stream) { 169 const std::vector<AttrListPtr> &Attrs = VE.getAttributes(); 170 if (Attrs.empty()) return; 171 172 Stream.EnterSubblock(bitc::PARAMATTR_BLOCK_ID, 3); 173 174 SmallVector<uint64_t, 64> Record; 175 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) { 176 const AttrListPtr &A = Attrs[i]; 177 for (unsigned i = 0, e = A.getNumSlots(); i != e; ++i) { 178 const AttributeWithIndex &PAWI = A.getSlot(i); 179 Record.push_back(PAWI.Index); 180 Record.push_back(Attribute::encodeLLVMAttributesForBitcode(PAWI.Attrs)); 181 } 182 183 Stream.EmitRecord(bitc::PARAMATTR_CODE_ENTRY, Record); 184 Record.clear(); 185 } 186 187 Stream.ExitBlock(); 188} 189 190/// WriteTypeTable - Write out the type table for a module. 191static void WriteTypeTable(const llvm_3_2::ValueEnumerator &VE, 192 BitstreamWriter &Stream) { 193 const llvm_3_2::ValueEnumerator::TypeList &TypeList = VE.getTypes(); 194 195 Stream.EnterSubblock(bitc::TYPE_BLOCK_ID_NEW, 4 /*count from # abbrevs */); 196 SmallVector<uint64_t, 64> TypeVals; 197 198 uint64_t NumBits = Log2_32_Ceil(VE.getTypes().size()+1); 199 200 // Abbrev for TYPE_CODE_POINTER. 201 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 202 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_POINTER)); 203 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits)); 204 Abbv->Add(BitCodeAbbrevOp(0)); // Addrspace = 0 205 unsigned PtrAbbrev = Stream.EmitAbbrev(Abbv); 206 207 // Abbrev for TYPE_CODE_FUNCTION. 208 Abbv = new BitCodeAbbrev(); 209 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_FUNCTION)); 210 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isvararg 211 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 212 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits)); 213 214 unsigned FunctionAbbrev = Stream.EmitAbbrev(Abbv); 215 216 // Abbrev for TYPE_CODE_STRUCT_ANON. 217 Abbv = new BitCodeAbbrev(); 218 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_ANON)); 219 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked 220 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 221 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits)); 222 223 unsigned StructAnonAbbrev = Stream.EmitAbbrev(Abbv); 224 225 // Abbrev for TYPE_CODE_STRUCT_NAME. 226 Abbv = new BitCodeAbbrev(); 227 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_NAME)); 228 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 229 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6)); 230 unsigned StructNameAbbrev = Stream.EmitAbbrev(Abbv); 231 232 // Abbrev for TYPE_CODE_STRUCT_NAMED. 233 Abbv = new BitCodeAbbrev(); 234 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_NAMED)); 235 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked 236 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 237 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits)); 238 239 unsigned StructNamedAbbrev = Stream.EmitAbbrev(Abbv); 240 241 // Abbrev for TYPE_CODE_ARRAY. 242 Abbv = new BitCodeAbbrev(); 243 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_ARRAY)); 244 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // size 245 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits)); 246 247 unsigned ArrayAbbrev = Stream.EmitAbbrev(Abbv); 248 249 // Emit an entry count so the reader can reserve space. 250 TypeVals.push_back(TypeList.size()); 251 Stream.EmitRecord(bitc::TYPE_CODE_NUMENTRY, TypeVals); 252 TypeVals.clear(); 253 254 // Loop over all of the types, emitting each in turn. 255 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) { 256 Type *T = TypeList[i]; 257 int AbbrevToUse = 0; 258 unsigned Code = 0; 259 260 switch (T->getTypeID()) { 261 default: llvm_unreachable("Unknown type!"); 262 case Type::VoidTyID: Code = bitc::TYPE_CODE_VOID; break; 263 case Type::HalfTyID: Code = bitc::TYPE_CODE_HALF; break; 264 case Type::FloatTyID: Code = bitc::TYPE_CODE_FLOAT; break; 265 case Type::DoubleTyID: Code = bitc::TYPE_CODE_DOUBLE; break; 266 case Type::X86_FP80TyID: Code = bitc::TYPE_CODE_X86_FP80; break; 267 case Type::FP128TyID: Code = bitc::TYPE_CODE_FP128; break; 268 case Type::PPC_FP128TyID: Code = bitc::TYPE_CODE_PPC_FP128; break; 269 case Type::LabelTyID: Code = bitc::TYPE_CODE_LABEL; break; 270 case Type::MetadataTyID: Code = bitc::TYPE_CODE_METADATA; break; 271 case Type::X86_MMXTyID: Code = bitc::TYPE_CODE_X86_MMX; break; 272 case Type::IntegerTyID: 273 // INTEGER: [width] 274 Code = bitc::TYPE_CODE_INTEGER; 275 TypeVals.push_back(cast<IntegerType>(T)->getBitWidth()); 276 break; 277 case Type::PointerTyID: { 278 PointerType *PTy = cast<PointerType>(T); 279 // POINTER: [pointee type, address space] 280 Code = bitc::TYPE_CODE_POINTER; 281 TypeVals.push_back(VE.getTypeID(PTy->getElementType())); 282 unsigned AddressSpace = PTy->getAddressSpace(); 283 TypeVals.push_back(AddressSpace); 284 if (AddressSpace == 0) AbbrevToUse = PtrAbbrev; 285 break; 286 } 287 case Type::FunctionTyID: { 288 FunctionType *FT = cast<FunctionType>(T); 289 // FUNCTION: [isvararg, retty, paramty x N] 290 Code = bitc::TYPE_CODE_FUNCTION; 291 TypeVals.push_back(FT->isVarArg()); 292 TypeVals.push_back(VE.getTypeID(FT->getReturnType())); 293 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) 294 TypeVals.push_back(VE.getTypeID(FT->getParamType(i))); 295 AbbrevToUse = FunctionAbbrev; 296 break; 297 } 298 case Type::StructTyID: { 299 StructType *ST = cast<StructType>(T); 300 // STRUCT: [ispacked, eltty x N] 301 TypeVals.push_back(ST->isPacked()); 302 // Output all of the element types. 303 for (StructType::element_iterator I = ST->element_begin(), 304 E = ST->element_end(); I != E; ++I) 305 TypeVals.push_back(VE.getTypeID(*I)); 306 307 if (ST->isLiteral()) { 308 Code = bitc::TYPE_CODE_STRUCT_ANON; 309 AbbrevToUse = StructAnonAbbrev; 310 } else { 311 if (ST->isOpaque()) { 312 Code = bitc::TYPE_CODE_OPAQUE; 313 } else { 314 Code = bitc::TYPE_CODE_STRUCT_NAMED; 315 AbbrevToUse = StructNamedAbbrev; 316 } 317 318 // Emit the name if it is present. 319 if (!ST->getName().empty()) 320 WriteStringRecord(bitc::TYPE_CODE_STRUCT_NAME, ST->getName(), 321 StructNameAbbrev, Stream); 322 } 323 break; 324 } 325 case Type::ArrayTyID: { 326 ArrayType *AT = cast<ArrayType>(T); 327 // ARRAY: [numelts, eltty] 328 Code = bitc::TYPE_CODE_ARRAY; 329 TypeVals.push_back(AT->getNumElements()); 330 TypeVals.push_back(VE.getTypeID(AT->getElementType())); 331 AbbrevToUse = ArrayAbbrev; 332 break; 333 } 334 case Type::VectorTyID: { 335 VectorType *VT = cast<VectorType>(T); 336 // VECTOR [numelts, eltty] 337 Code = bitc::TYPE_CODE_VECTOR; 338 TypeVals.push_back(VT->getNumElements()); 339 TypeVals.push_back(VE.getTypeID(VT->getElementType())); 340 break; 341 } 342 } 343 344 // Emit the finished record. 345 Stream.EmitRecord(Code, TypeVals, AbbrevToUse); 346 TypeVals.clear(); 347 } 348 349 Stream.ExitBlock(); 350} 351 352static unsigned getEncodedLinkage(const GlobalValue *GV) { 353 switch (GV->getLinkage()) { 354 case GlobalValue::ExternalLinkage: return 0; 355 case GlobalValue::WeakAnyLinkage: return 1; 356 case GlobalValue::AppendingLinkage: return 2; 357 case GlobalValue::InternalLinkage: return 3; 358 case GlobalValue::LinkOnceAnyLinkage: return 4; 359 case GlobalValue::DLLImportLinkage: return 5; 360 case GlobalValue::DLLExportLinkage: return 6; 361 case GlobalValue::ExternalWeakLinkage: return 7; 362 case GlobalValue::CommonLinkage: return 8; 363 case GlobalValue::PrivateLinkage: return 9; 364 case GlobalValue::WeakODRLinkage: return 10; 365 case GlobalValue::LinkOnceODRLinkage: return 11; 366 case GlobalValue::AvailableExternallyLinkage: return 12; 367 case GlobalValue::LinkerPrivateLinkage: return 13; 368 case GlobalValue::LinkerPrivateWeakLinkage: return 14; 369 case GlobalValue::LinkOnceODRAutoHideLinkage: return 15; 370 } 371 llvm_unreachable("Invalid linkage"); 372} 373 374static unsigned getEncodedVisibility(const GlobalValue *GV) { 375 switch (GV->getVisibility()) { 376 case GlobalValue::DefaultVisibility: return 0; 377 case GlobalValue::HiddenVisibility: return 1; 378 case GlobalValue::ProtectedVisibility: return 2; 379 } 380 llvm_unreachable("Invalid visibility"); 381} 382 383static unsigned getEncodedThreadLocalMode(const GlobalVariable *GV) { 384 switch (GV->getThreadLocalMode()) { 385 case GlobalVariable::NotThreadLocal: return 0; 386 case GlobalVariable::GeneralDynamicTLSModel: return 1; 387 case GlobalVariable::LocalDynamicTLSModel: return 2; 388 case GlobalVariable::InitialExecTLSModel: return 3; 389 case GlobalVariable::LocalExecTLSModel: return 4; 390 } 391 llvm_unreachable("Invalid TLS model"); 392} 393 394// Emit top-level description of module, including target triple, inline asm, 395// descriptors for global variables, and function prototype info. 396static void WriteModuleInfo(const Module *M, 397 const llvm_3_2::ValueEnumerator &VE, 398 BitstreamWriter &Stream) { 399 // Emit the list of dependent libraries for the Module. 400 for (Module::lib_iterator I = M->lib_begin(), E = M->lib_end(); I != E; ++I) 401 WriteStringRecord(bitc::MODULE_CODE_DEPLIB, *I, 0/*TODO*/, Stream); 402 403 // Emit various pieces of data attached to a module. 404 if (!M->getTargetTriple().empty()) 405 WriteStringRecord(bitc::MODULE_CODE_TRIPLE, M->getTargetTriple(), 406 0/*TODO*/, Stream); 407 if (!M->getDataLayout().empty()) 408 WriteStringRecord(bitc::MODULE_CODE_DATALAYOUT, M->getDataLayout(), 409 0/*TODO*/, Stream); 410 if (!M->getModuleInlineAsm().empty()) 411 WriteStringRecord(bitc::MODULE_CODE_ASM, M->getModuleInlineAsm(), 412 0/*TODO*/, Stream); 413 414 // Emit information about sections and GC, computing how many there are. Also 415 // compute the maximum alignment value. 416 std::map<std::string, unsigned> SectionMap; 417 std::map<std::string, unsigned> GCMap; 418 unsigned MaxAlignment = 0; 419 unsigned MaxGlobalType = 0; 420 for (Module::const_global_iterator GV = M->global_begin(),E = M->global_end(); 421 GV != E; ++GV) { 422 MaxAlignment = std::max(MaxAlignment, GV->getAlignment()); 423 MaxGlobalType = std::max(MaxGlobalType, VE.getTypeID(GV->getType())); 424 if (GV->hasSection()) { 425 // Give section names unique ID's. 426 unsigned &Entry = SectionMap[GV->getSection()]; 427 if (!Entry) { 428 WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, GV->getSection(), 429 0/*TODO*/, Stream); 430 Entry = SectionMap.size(); 431 } 432 } 433 } 434 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) { 435 MaxAlignment = std::max(MaxAlignment, F->getAlignment()); 436 if (F->hasSection()) { 437 // Give section names unique ID's. 438 unsigned &Entry = SectionMap[F->getSection()]; 439 if (!Entry) { 440 WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, F->getSection(), 441 0/*TODO*/, Stream); 442 Entry = SectionMap.size(); 443 } 444 } 445 if (F->hasGC()) { 446 // Same for GC names. 447 unsigned &Entry = GCMap[F->getGC()]; 448 if (!Entry) { 449 WriteStringRecord(bitc::MODULE_CODE_GCNAME, F->getGC(), 450 0/*TODO*/, Stream); 451 Entry = GCMap.size(); 452 } 453 } 454 } 455 456 // Emit abbrev for globals, now that we know # sections and max alignment. 457 unsigned SimpleGVarAbbrev = 0; 458 if (!M->global_empty()) { 459 // Add an abbrev for common globals with no visibility or thread localness. 460 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 461 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_GLOBALVAR)); 462 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 463 Log2_32_Ceil(MaxGlobalType+1))); 464 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Constant. 465 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Initializer. 466 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // Linkage. 467 if (MaxAlignment == 0) // Alignment. 468 Abbv->Add(BitCodeAbbrevOp(0)); 469 else { 470 unsigned MaxEncAlignment = Log2_32(MaxAlignment)+1; 471 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 472 Log2_32_Ceil(MaxEncAlignment+1))); 473 } 474 if (SectionMap.empty()) // Section. 475 Abbv->Add(BitCodeAbbrevOp(0)); 476 else 477 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 478 Log2_32_Ceil(SectionMap.size()+1))); 479 // Don't bother emitting vis + thread local. 480 SimpleGVarAbbrev = Stream.EmitAbbrev(Abbv); 481 } 482 483 // Emit the global variable information. 484 SmallVector<unsigned, 64> Vals; 485 for (Module::const_global_iterator GV = M->global_begin(),E = M->global_end(); 486 GV != E; ++GV) { 487 unsigned AbbrevToUse = 0; 488 489 // GLOBALVAR: [type, isconst, initid, 490 // linkage, alignment, section, visibility, threadlocal, 491 // unnamed_addr] 492 Vals.push_back(VE.getTypeID(GV->getType())); 493 Vals.push_back(GV->isConstant()); 494 Vals.push_back(GV->isDeclaration() ? 0 : 495 (VE.getValueID(GV->getInitializer()) + 1)); 496 Vals.push_back(getEncodedLinkage(GV)); 497 Vals.push_back(Log2_32(GV->getAlignment())+1); 498 Vals.push_back(GV->hasSection() ? SectionMap[GV->getSection()] : 0); 499 if (GV->isThreadLocal() || 500 GV->getVisibility() != GlobalValue::DefaultVisibility || 501 GV->hasUnnamedAddr()) { 502 Vals.push_back(getEncodedVisibility(GV)); 503 Vals.push_back(getEncodedThreadLocalMode(GV)); 504 Vals.push_back(GV->hasUnnamedAddr()); 505 } else { 506 AbbrevToUse = SimpleGVarAbbrev; 507 } 508 509 Stream.EmitRecord(bitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse); 510 Vals.clear(); 511 } 512 513 // Emit the function proto information. 514 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) { 515 // FUNCTION: [type, callingconv, isproto, linkage, paramattrs, alignment, 516 // section, visibility, gc, unnamed_addr] 517 Vals.push_back(VE.getTypeID(F->getType())); 518 Vals.push_back(F->getCallingConv()); 519 Vals.push_back(F->isDeclaration()); 520 Vals.push_back(getEncodedLinkage(F)); 521 Vals.push_back(VE.getAttributeID(F->getAttributes())); 522 Vals.push_back(Log2_32(F->getAlignment())+1); 523 Vals.push_back(F->hasSection() ? SectionMap[F->getSection()] : 0); 524 Vals.push_back(getEncodedVisibility(F)); 525 Vals.push_back(F->hasGC() ? GCMap[F->getGC()] : 0); 526 Vals.push_back(F->hasUnnamedAddr()); 527 528 unsigned AbbrevToUse = 0; 529 Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse); 530 Vals.clear(); 531 } 532 533 // Emit the alias information. 534 for (Module::const_alias_iterator AI = M->alias_begin(), E = M->alias_end(); 535 AI != E; ++AI) { 536 // ALIAS: [alias type, aliasee val#, linkage, visibility] 537 Vals.push_back(VE.getTypeID(AI->getType())); 538 Vals.push_back(VE.getValueID(AI->getAliasee())); 539 Vals.push_back(getEncodedLinkage(AI)); 540 Vals.push_back(getEncodedVisibility(AI)); 541 unsigned AbbrevToUse = 0; 542 Stream.EmitRecord(bitc::MODULE_CODE_ALIAS, Vals, AbbrevToUse); 543 Vals.clear(); 544 } 545} 546 547static uint64_t GetOptimizationFlags(const Value *V) { 548 uint64_t Flags = 0; 549 550 if (const OverflowingBinaryOperator *OBO = 551 dyn_cast<OverflowingBinaryOperator>(V)) { 552 if (OBO->hasNoSignedWrap()) 553 Flags |= 1 << bitc::OBO_NO_SIGNED_WRAP; 554 if (OBO->hasNoUnsignedWrap()) 555 Flags |= 1 << bitc::OBO_NO_UNSIGNED_WRAP; 556 } else if (const PossiblyExactOperator *PEO = 557 dyn_cast<PossiblyExactOperator>(V)) { 558 if (PEO->isExact()) 559 Flags |= 1 << bitc::PEO_EXACT; 560 } 561 562 return Flags; 563} 564 565static void WriteMDNode(const MDNode *N, 566 const llvm_3_2::ValueEnumerator &VE, 567 BitstreamWriter &Stream, 568 SmallVector<uint64_t, 64> &Record) { 569 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { 570 if (N->getOperand(i)) { 571 Record.push_back(VE.getTypeID(N->getOperand(i)->getType())); 572 Record.push_back(VE.getValueID(N->getOperand(i))); 573 } else { 574 Record.push_back(VE.getTypeID(Type::getVoidTy(N->getContext()))); 575 Record.push_back(0); 576 } 577 } 578 unsigned MDCode = N->isFunctionLocal() ? bitc::METADATA_FN_NODE : 579 bitc::METADATA_NODE; 580 Stream.EmitRecord(MDCode, Record, 0); 581 Record.clear(); 582} 583 584static void WriteModuleMetadata(const Module *M, 585 const llvm_3_2::ValueEnumerator &VE, 586 BitstreamWriter &Stream) { 587 const llvm_3_2::ValueEnumerator::ValueList &Vals = VE.getMDValues(); 588 bool StartedMetadataBlock = false; 589 unsigned MDSAbbrev = 0; 590 SmallVector<uint64_t, 64> Record; 591 for (unsigned i = 0, e = Vals.size(); i != e; ++i) { 592 593 if (const MDNode *N = dyn_cast<MDNode>(Vals[i].first)) { 594 if (!N->isFunctionLocal() || !N->getFunction()) { 595 if (!StartedMetadataBlock) { 596 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3); 597 StartedMetadataBlock = true; 598 } 599 WriteMDNode(N, VE, Stream, Record); 600 } 601 } else if (const MDString *MDS = dyn_cast<MDString>(Vals[i].first)) { 602 if (!StartedMetadataBlock) { 603 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3); 604 605 // Abbrev for METADATA_STRING. 606 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 607 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_STRING)); 608 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 609 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); 610 MDSAbbrev = Stream.EmitAbbrev(Abbv); 611 StartedMetadataBlock = true; 612 } 613 614 // Code: [strchar x N] 615 Record.append(MDS->begin(), MDS->end()); 616 617 // Emit the finished record. 618 Stream.EmitRecord(bitc::METADATA_STRING, Record, MDSAbbrev); 619 Record.clear(); 620 } 621 } 622 623 // Write named metadata. 624 for (Module::const_named_metadata_iterator I = M->named_metadata_begin(), 625 E = M->named_metadata_end(); I != E; ++I) { 626 const NamedMDNode *NMD = I; 627 if (!StartedMetadataBlock) { 628 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3); 629 StartedMetadataBlock = true; 630 } 631 632 // Write name. 633 StringRef Str = NMD->getName(); 634 for (unsigned i = 0, e = Str.size(); i != e; ++i) 635 Record.push_back(Str[i]); 636 Stream.EmitRecord(bitc::METADATA_NAME, Record, 0/*TODO*/); 637 Record.clear(); 638 639 // Write named metadata operands. 640 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) 641 Record.push_back(VE.getValueID(NMD->getOperand(i))); 642 Stream.EmitRecord(bitc::METADATA_NAMED_NODE, Record, 0); 643 Record.clear(); 644 } 645 646 if (StartedMetadataBlock) 647 Stream.ExitBlock(); 648} 649 650static void WriteFunctionLocalMetadata(const Function &F, 651 const llvm_3_2::ValueEnumerator &VE, 652 BitstreamWriter &Stream) { 653 bool StartedMetadataBlock = false; 654 SmallVector<uint64_t, 64> Record; 655 const SmallVector<const MDNode *, 8> &Vals = VE.getFunctionLocalMDValues(); 656 for (unsigned i = 0, e = Vals.size(); i != e; ++i) 657 if (const MDNode *N = Vals[i]) 658 if (N->isFunctionLocal() && N->getFunction() == &F) { 659 if (!StartedMetadataBlock) { 660 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3); 661 StartedMetadataBlock = true; 662 } 663 WriteMDNode(N, VE, Stream, Record); 664 } 665 666 if (StartedMetadataBlock) 667 Stream.ExitBlock(); 668} 669 670static void WriteMetadataAttachment(const Function &F, 671 const llvm_3_2::ValueEnumerator &VE, 672 BitstreamWriter &Stream) { 673 Stream.EnterSubblock(bitc::METADATA_ATTACHMENT_ID, 3); 674 675 SmallVector<uint64_t, 64> Record; 676 677 // Write metadata attachments 678 // METADATA_ATTACHMENT - [m x [value, [n x [id, mdnode]]] 679 SmallVector<std::pair<unsigned, MDNode*>, 4> MDs; 680 681 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) 682 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); 683 I != E; ++I) { 684 MDs.clear(); 685 I->getAllMetadataOtherThanDebugLoc(MDs); 686 687 // If no metadata, ignore instruction. 688 if (MDs.empty()) continue; 689 690 Record.push_back(VE.getInstructionID(I)); 691 692 for (unsigned i = 0, e = MDs.size(); i != e; ++i) { 693 Record.push_back(MDs[i].first); 694 Record.push_back(VE.getValueID(MDs[i].second)); 695 } 696 Stream.EmitRecord(bitc::METADATA_ATTACHMENT, Record, 0); 697 Record.clear(); 698 } 699 700 Stream.ExitBlock(); 701} 702 703static void WriteModuleMetadataStore(const Module *M, BitstreamWriter &Stream) { 704 SmallVector<uint64_t, 64> Record; 705 706 // Write metadata kinds 707 // METADATA_KIND - [n x [id, name]] 708 SmallVector<StringRef, 4> Names; 709 M->getMDKindNames(Names); 710 711 if (Names.empty()) return; 712 713 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3); 714 715 for (unsigned MDKindID = 0, e = Names.size(); MDKindID != e; ++MDKindID) { 716 Record.push_back(MDKindID); 717 StringRef KName = Names[MDKindID]; 718 Record.append(KName.begin(), KName.end()); 719 720 Stream.EmitRecord(bitc::METADATA_KIND, Record, 0); 721 Record.clear(); 722 } 723 724 Stream.ExitBlock(); 725} 726 727static void EmitAPInt(SmallVectorImpl<uint64_t> &Vals, 728 unsigned &Code, unsigned &AbbrevToUse, const APInt &Val, 729 bool EmitSizeForWideNumbers = false 730 ) { 731 if (Val.getBitWidth() <= 64) { 732 uint64_t V = Val.getSExtValue(); 733 if ((int64_t)V >= 0) 734 Vals.push_back(V << 1); 735 else 736 Vals.push_back((-V << 1) | 1); 737 Code = bitc::CST_CODE_INTEGER; 738 AbbrevToUse = CONSTANTS_INTEGER_ABBREV; 739 } else { 740 // Wide integers, > 64 bits in size. 741 // We have an arbitrary precision integer value to write whose 742 // bit width is > 64. However, in canonical unsigned integer 743 // format it is likely that the high bits are going to be zero. 744 // So, we only write the number of active words. 745 unsigned NWords = Val.getActiveWords(); 746 747 if (EmitSizeForWideNumbers) 748 Vals.push_back(NWords); 749 750 const uint64_t *RawWords = Val.getRawData(); 751 for (unsigned i = 0; i != NWords; ++i) { 752 int64_t V = RawWords[i]; 753 if (V >= 0) 754 Vals.push_back(V << 1); 755 else 756 Vals.push_back((-V << 1) | 1); 757 } 758 Code = bitc::CST_CODE_WIDE_INTEGER; 759 } 760} 761 762static void WriteConstants(unsigned FirstVal, unsigned LastVal, 763 const llvm_3_2::ValueEnumerator &VE, 764 BitstreamWriter &Stream, bool isGlobal) { 765 if (FirstVal == LastVal) return; 766 767 Stream.EnterSubblock(bitc::CONSTANTS_BLOCK_ID, 4); 768 769 unsigned AggregateAbbrev = 0; 770 unsigned String8Abbrev = 0; 771 unsigned CString7Abbrev = 0; 772 unsigned CString6Abbrev = 0; 773 // If this is a constant pool for the module, emit module-specific abbrevs. 774 if (isGlobal) { 775 // Abbrev for CST_CODE_AGGREGATE. 776 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 777 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_AGGREGATE)); 778 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 779 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, Log2_32_Ceil(LastVal+1))); 780 AggregateAbbrev = Stream.EmitAbbrev(Abbv); 781 782 // Abbrev for CST_CODE_STRING. 783 Abbv = new BitCodeAbbrev(); 784 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_STRING)); 785 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 786 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); 787 String8Abbrev = Stream.EmitAbbrev(Abbv); 788 // Abbrev for CST_CODE_CSTRING. 789 Abbv = new BitCodeAbbrev(); 790 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING)); 791 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 792 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); 793 CString7Abbrev = Stream.EmitAbbrev(Abbv); 794 // Abbrev for CST_CODE_CSTRING. 795 Abbv = new BitCodeAbbrev(); 796 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING)); 797 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 798 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6)); 799 CString6Abbrev = Stream.EmitAbbrev(Abbv); 800 } 801 802 SmallVector<uint64_t, 64> Record; 803 804 const llvm_3_2::ValueEnumerator::ValueList &Vals = VE.getValues(); 805 Type *LastTy = 0; 806 for (unsigned i = FirstVal; i != LastVal; ++i) { 807 const Value *V = Vals[i].first; 808 // If we need to switch types, do so now. 809 if (V->getType() != LastTy) { 810 LastTy = V->getType(); 811 Record.push_back(VE.getTypeID(LastTy)); 812 Stream.EmitRecord(bitc::CST_CODE_SETTYPE, Record, 813 CONSTANTS_SETTYPE_ABBREV); 814 Record.clear(); 815 } 816 817 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) { 818 Record.push_back(unsigned(IA->hasSideEffects()) | 819 unsigned(IA->isAlignStack()) << 1 | 820 unsigned(IA->getDialect()&1) << 2); 821 822 // Add the asm string. 823 const std::string &AsmStr = IA->getAsmString(); 824 Record.push_back(AsmStr.size()); 825 for (unsigned i = 0, e = AsmStr.size(); i != e; ++i) 826 Record.push_back(AsmStr[i]); 827 828 // Add the constraint string. 829 const std::string &ConstraintStr = IA->getConstraintString(); 830 Record.push_back(ConstraintStr.size()); 831 for (unsigned i = 0, e = ConstraintStr.size(); i != e; ++i) 832 Record.push_back(ConstraintStr[i]); 833 Stream.EmitRecord(bitc::CST_CODE_INLINEASM, Record); 834 Record.clear(); 835 continue; 836 } 837 const Constant *C = cast<Constant>(V); 838 unsigned Code = -1U; 839 unsigned AbbrevToUse = 0; 840 if (C->isNullValue()) { 841 Code = bitc::CST_CODE_NULL; 842 } else if (isa<UndefValue>(C)) { 843 Code = bitc::CST_CODE_UNDEF; 844 } else if (const ConstantInt *IV = dyn_cast<ConstantInt>(C)) { 845 EmitAPInt(Record, Code, AbbrevToUse, IV->getValue()); 846 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) { 847 Code = bitc::CST_CODE_FLOAT; 848 Type *Ty = CFP->getType(); 849 if (Ty->isHalfTy() || Ty->isFloatTy() || Ty->isDoubleTy()) { 850 Record.push_back(CFP->getValueAPF().bitcastToAPInt().getZExtValue()); 851 } else if (Ty->isX86_FP80Ty()) { 852 // api needed to prevent premature destruction 853 // bits are not in the same order as a normal i80 APInt, compensate. 854 APInt api = CFP->getValueAPF().bitcastToAPInt(); 855 const uint64_t *p = api.getRawData(); 856 Record.push_back((p[1] << 48) | (p[0] >> 16)); 857 Record.push_back(p[0] & 0xffffLL); 858 } else if (Ty->isFP128Ty() || Ty->isPPC_FP128Ty()) { 859 APInt api = CFP->getValueAPF().bitcastToAPInt(); 860 const uint64_t *p = api.getRawData(); 861 Record.push_back(p[0]); 862 Record.push_back(p[1]); 863 } else { 864 assert (0 && "Unknown FP type!"); 865 } 866 } else if (isa<ConstantDataSequential>(C) && 867 cast<ConstantDataSequential>(C)->isString()) { 868 const ConstantDataSequential *Str = cast<ConstantDataSequential>(C); 869 // Emit constant strings specially. 870 unsigned NumElts = Str->getNumElements(); 871 // If this is a null-terminated string, use the denser CSTRING encoding. 872 if (Str->isCString()) { 873 Code = bitc::CST_CODE_CSTRING; 874 --NumElts; // Don't encode the null, which isn't allowed by char6. 875 } else { 876 Code = bitc::CST_CODE_STRING; 877 AbbrevToUse = String8Abbrev; 878 } 879 bool isCStr7 = Code == bitc::CST_CODE_CSTRING; 880 bool isCStrChar6 = Code == bitc::CST_CODE_CSTRING; 881 for (unsigned i = 0; i != NumElts; ++i) { 882 unsigned char V = Str->getElementAsInteger(i); 883 Record.push_back(V); 884 isCStr7 &= (V & 128) == 0; 885 if (isCStrChar6) 886 isCStrChar6 = BitCodeAbbrevOp::isChar6(V); 887 } 888 889 if (isCStrChar6) 890 AbbrevToUse = CString6Abbrev; 891 else if (isCStr7) 892 AbbrevToUse = CString7Abbrev; 893 } else if (const ConstantDataSequential *CDS = 894 dyn_cast<ConstantDataSequential>(C)) { 895 Code = bitc::CST_CODE_DATA; 896 Type *EltTy = CDS->getType()->getElementType(); 897 if (isa<IntegerType>(EltTy)) { 898 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) 899 Record.push_back(CDS->getElementAsInteger(i)); 900 } else if (EltTy->isFloatTy()) { 901 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) { 902 union { float F; uint32_t I; }; 903 F = CDS->getElementAsFloat(i); 904 Record.push_back(I); 905 } 906 } else { 907 assert(EltTy->isDoubleTy() && "Unknown ConstantData element type"); 908 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) { 909 union { double F; uint64_t I; }; 910 F = CDS->getElementAsDouble(i); 911 Record.push_back(I); 912 } 913 } 914 } else if (isa<ConstantArray>(C) || isa<ConstantStruct>(C) || 915 isa<ConstantVector>(C)) { 916 Code = bitc::CST_CODE_AGGREGATE; 917 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) 918 Record.push_back(VE.getValueID(C->getOperand(i))); 919 AbbrevToUse = AggregateAbbrev; 920 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { 921 switch (CE->getOpcode()) { 922 default: 923 if (Instruction::isCast(CE->getOpcode())) { 924 Code = bitc::CST_CODE_CE_CAST; 925 Record.push_back(GetEncodedCastOpcode(CE->getOpcode())); 926 Record.push_back(VE.getTypeID(C->getOperand(0)->getType())); 927 Record.push_back(VE.getValueID(C->getOperand(0))); 928 AbbrevToUse = CONSTANTS_CE_CAST_Abbrev; 929 } else { 930 assert(CE->getNumOperands() == 2 && "Unknown constant expr!"); 931 Code = bitc::CST_CODE_CE_BINOP; 932 Record.push_back(GetEncodedBinaryOpcode(CE->getOpcode())); 933 Record.push_back(VE.getValueID(C->getOperand(0))); 934 Record.push_back(VE.getValueID(C->getOperand(1))); 935 uint64_t Flags = GetOptimizationFlags(CE); 936 if (Flags != 0) 937 Record.push_back(Flags); 938 } 939 break; 940 case Instruction::GetElementPtr: 941 Code = bitc::CST_CODE_CE_GEP; 942 if (cast<GEPOperator>(C)->isInBounds()) 943 Code = bitc::CST_CODE_CE_INBOUNDS_GEP; 944 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) { 945 Record.push_back(VE.getTypeID(C->getOperand(i)->getType())); 946 Record.push_back(VE.getValueID(C->getOperand(i))); 947 } 948 break; 949 case Instruction::Select: 950 Code = bitc::CST_CODE_CE_SELECT; 951 Record.push_back(VE.getValueID(C->getOperand(0))); 952 Record.push_back(VE.getValueID(C->getOperand(1))); 953 Record.push_back(VE.getValueID(C->getOperand(2))); 954 break; 955 case Instruction::ExtractElement: 956 Code = bitc::CST_CODE_CE_EXTRACTELT; 957 Record.push_back(VE.getTypeID(C->getOperand(0)->getType())); 958 Record.push_back(VE.getValueID(C->getOperand(0))); 959 Record.push_back(VE.getValueID(C->getOperand(1))); 960 break; 961 case Instruction::InsertElement: 962 Code = bitc::CST_CODE_CE_INSERTELT; 963 Record.push_back(VE.getValueID(C->getOperand(0))); 964 Record.push_back(VE.getValueID(C->getOperand(1))); 965 Record.push_back(VE.getValueID(C->getOperand(2))); 966 break; 967 case Instruction::ShuffleVector: 968 // If the return type and argument types are the same, this is a 969 // standard shufflevector instruction. If the types are different, 970 // then the shuffle is widening or truncating the input vectors, and 971 // the argument type must also be encoded. 972 if (C->getType() == C->getOperand(0)->getType()) { 973 Code = bitc::CST_CODE_CE_SHUFFLEVEC; 974 } else { 975 Code = bitc::CST_CODE_CE_SHUFVEC_EX; 976 Record.push_back(VE.getTypeID(C->getOperand(0)->getType())); 977 } 978 Record.push_back(VE.getValueID(C->getOperand(0))); 979 Record.push_back(VE.getValueID(C->getOperand(1))); 980 Record.push_back(VE.getValueID(C->getOperand(2))); 981 break; 982 case Instruction::ICmp: 983 case Instruction::FCmp: 984 Code = bitc::CST_CODE_CE_CMP; 985 Record.push_back(VE.getTypeID(C->getOperand(0)->getType())); 986 Record.push_back(VE.getValueID(C->getOperand(0))); 987 Record.push_back(VE.getValueID(C->getOperand(1))); 988 Record.push_back(CE->getPredicate()); 989 break; 990 } 991 } else if (const BlockAddress *BA = dyn_cast<BlockAddress>(C)) { 992 Code = bitc::CST_CODE_BLOCKADDRESS; 993 Record.push_back(VE.getTypeID(BA->getFunction()->getType())); 994 Record.push_back(VE.getValueID(BA->getFunction())); 995 Record.push_back(VE.getGlobalBasicBlockID(BA->getBasicBlock())); 996 } else { 997#ifndef NDEBUG 998 C->dump(); 999#endif 1000 llvm_unreachable("Unknown constant!"); 1001 } 1002 Stream.EmitRecord(Code, Record, AbbrevToUse); 1003 Record.clear(); 1004 } 1005 1006 Stream.ExitBlock(); 1007} 1008 1009static void WriteModuleConstants(const llvm_3_2::ValueEnumerator &VE, 1010 BitstreamWriter &Stream) { 1011 const llvm_3_2::ValueEnumerator::ValueList &Vals = VE.getValues(); 1012 1013 // Find the first constant to emit, which is the first non-globalvalue value. 1014 // We know globalvalues have been emitted by WriteModuleInfo. 1015 for (unsigned i = 0, e = Vals.size(); i != e; ++i) { 1016 if (!isa<GlobalValue>(Vals[i].first)) { 1017 WriteConstants(i, Vals.size(), VE, Stream, true); 1018 return; 1019 } 1020 } 1021} 1022 1023/// PushValueAndType - The file has to encode both the value and type id for 1024/// many values, because we need to know what type to create for forward 1025/// references. However, most operands are not forward references, so this type 1026/// field is not needed. 1027/// 1028/// This function adds V's value ID to Vals. If the value ID is higher than the 1029/// instruction ID, then it is a forward reference, and it also includes the 1030/// type ID. 1031static bool PushValueAndType(const Value *V, unsigned InstID, 1032 SmallVector<unsigned, 64> &Vals, 1033 llvm_3_2::ValueEnumerator &VE) { 1034 unsigned ValID = VE.getValueID(V); 1035 Vals.push_back(ValID); 1036 if (ValID >= InstID) { 1037 Vals.push_back(VE.getTypeID(V->getType())); 1038 return true; 1039 } 1040 return false; 1041} 1042 1043/// WriteInstruction - Emit an instruction to the specified stream. 1044static void WriteInstruction(const Instruction &I, unsigned InstID, 1045 llvm_3_2::ValueEnumerator &VE, 1046 BitstreamWriter &Stream, 1047 SmallVector<unsigned, 64> &Vals) { 1048 unsigned Code = 0; 1049 unsigned AbbrevToUse = 0; 1050 VE.setInstructionID(&I); 1051 switch (I.getOpcode()) { 1052 default: 1053 if (Instruction::isCast(I.getOpcode())) { 1054 Code = bitc::FUNC_CODE_INST_CAST; 1055 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE)) 1056 AbbrevToUse = FUNCTION_INST_CAST_ABBREV; 1057 Vals.push_back(VE.getTypeID(I.getType())); 1058 Vals.push_back(GetEncodedCastOpcode(I.getOpcode())); 1059 } else { 1060 assert(isa<BinaryOperator>(I) && "Unknown instruction!"); 1061 Code = bitc::FUNC_CODE_INST_BINOP; 1062 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE)) 1063 AbbrevToUse = FUNCTION_INST_BINOP_ABBREV; 1064 Vals.push_back(VE.getValueID(I.getOperand(1))); 1065 Vals.push_back(GetEncodedBinaryOpcode(I.getOpcode())); 1066 uint64_t Flags = GetOptimizationFlags(&I); 1067 if (Flags != 0) { 1068 if (AbbrevToUse == FUNCTION_INST_BINOP_ABBREV) 1069 AbbrevToUse = FUNCTION_INST_BINOP_FLAGS_ABBREV; 1070 Vals.push_back(Flags); 1071 } 1072 } 1073 break; 1074 1075 case Instruction::GetElementPtr: 1076 Code = bitc::FUNC_CODE_INST_GEP; 1077 if (cast<GEPOperator>(&I)->isInBounds()) 1078 Code = bitc::FUNC_CODE_INST_INBOUNDS_GEP; 1079 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) 1080 PushValueAndType(I.getOperand(i), InstID, Vals, VE); 1081 break; 1082 case Instruction::ExtractValue: { 1083 Code = bitc::FUNC_CODE_INST_EXTRACTVAL; 1084 PushValueAndType(I.getOperand(0), InstID, Vals, VE); 1085 const ExtractValueInst *EVI = cast<ExtractValueInst>(&I); 1086 for (const unsigned *i = EVI->idx_begin(), *e = EVI->idx_end(); i != e; ++i) 1087 Vals.push_back(*i); 1088 break; 1089 } 1090 case Instruction::InsertValue: { 1091 Code = bitc::FUNC_CODE_INST_INSERTVAL; 1092 PushValueAndType(I.getOperand(0), InstID, Vals, VE); 1093 PushValueAndType(I.getOperand(1), InstID, Vals, VE); 1094 const InsertValueInst *IVI = cast<InsertValueInst>(&I); 1095 for (const unsigned *i = IVI->idx_begin(), *e = IVI->idx_end(); i != e; ++i) 1096 Vals.push_back(*i); 1097 break; 1098 } 1099 case Instruction::Select: 1100 Code = bitc::FUNC_CODE_INST_VSELECT; 1101 PushValueAndType(I.getOperand(1), InstID, Vals, VE); 1102 Vals.push_back(VE.getValueID(I.getOperand(2))); 1103 PushValueAndType(I.getOperand(0), InstID, Vals, VE); 1104 break; 1105 case Instruction::ExtractElement: 1106 Code = bitc::FUNC_CODE_INST_EXTRACTELT; 1107 PushValueAndType(I.getOperand(0), InstID, Vals, VE); 1108 Vals.push_back(VE.getValueID(I.getOperand(1))); 1109 break; 1110 case Instruction::InsertElement: 1111 Code = bitc::FUNC_CODE_INST_INSERTELT; 1112 PushValueAndType(I.getOperand(0), InstID, Vals, VE); 1113 Vals.push_back(VE.getValueID(I.getOperand(1))); 1114 Vals.push_back(VE.getValueID(I.getOperand(2))); 1115 break; 1116 case Instruction::ShuffleVector: 1117 Code = bitc::FUNC_CODE_INST_SHUFFLEVEC; 1118 PushValueAndType(I.getOperand(0), InstID, Vals, VE); 1119 Vals.push_back(VE.getValueID(I.getOperand(1))); 1120 Vals.push_back(VE.getValueID(I.getOperand(2))); 1121 break; 1122 case Instruction::ICmp: 1123 case Instruction::FCmp: 1124 // compare returning Int1Ty or vector of Int1Ty 1125 Code = bitc::FUNC_CODE_INST_CMP2; 1126 PushValueAndType(I.getOperand(0), InstID, Vals, VE); 1127 Vals.push_back(VE.getValueID(I.getOperand(1))); 1128 Vals.push_back(cast<CmpInst>(I).getPredicate()); 1129 break; 1130 1131 case Instruction::Ret: 1132 { 1133 Code = bitc::FUNC_CODE_INST_RET; 1134 unsigned NumOperands = I.getNumOperands(); 1135 if (NumOperands == 0) 1136 AbbrevToUse = FUNCTION_INST_RET_VOID_ABBREV; 1137 else if (NumOperands == 1) { 1138 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE)) 1139 AbbrevToUse = FUNCTION_INST_RET_VAL_ABBREV; 1140 } else { 1141 for (unsigned i = 0, e = NumOperands; i != e; ++i) 1142 PushValueAndType(I.getOperand(i), InstID, Vals, VE); 1143 } 1144 } 1145 break; 1146 case Instruction::Br: 1147 { 1148 Code = bitc::FUNC_CODE_INST_BR; 1149 BranchInst &II = cast<BranchInst>(I); 1150 Vals.push_back(VE.getValueID(II.getSuccessor(0))); 1151 if (II.isConditional()) { 1152 Vals.push_back(VE.getValueID(II.getSuccessor(1))); 1153 Vals.push_back(VE.getValueID(II.getCondition())); 1154 } 1155 } 1156 break; 1157 case Instruction::Switch: 1158 { 1159 // Redefine Vals, since here we need to use 64 bit values 1160 // explicitly to store large APInt numbers. 1161 SmallVector<uint64_t, 128> Vals64; 1162 1163 Code = bitc::FUNC_CODE_INST_SWITCH; 1164 SwitchInst &SI = cast<SwitchInst>(I); 1165 1166 uint32_t SwitchRecordHeader = SI.hash() | (SWITCH_INST_MAGIC << 16); 1167 Vals64.push_back(SwitchRecordHeader); 1168 1169 Vals64.push_back(VE.getTypeID(SI.getCondition()->getType())); 1170 Vals64.push_back(VE.getValueID(SI.getCondition())); 1171 Vals64.push_back(VE.getValueID(SI.getDefaultDest())); 1172 Vals64.push_back(SI.getNumCases()); 1173 for (SwitchInst::CaseIt i = SI.case_begin(), e = SI.case_end(); 1174 i != e; ++i) { 1175 IntegersSubset& CaseRanges = i.getCaseValueEx(); 1176 unsigned Code, Abbrev; // will unused. 1177 1178 if (CaseRanges.isSingleNumber()) { 1179 Vals64.push_back(1/*NumItems = 1*/); 1180 Vals64.push_back(true/*IsSingleNumber = true*/); 1181 EmitAPInt(Vals64, Code, Abbrev, CaseRanges.getSingleNumber(0), true); 1182 } else { 1183 1184 Vals64.push_back(CaseRanges.getNumItems()); 1185 1186 if (CaseRanges.isSingleNumbersOnly()) { 1187 for (unsigned ri = 0, rn = CaseRanges.getNumItems(); 1188 ri != rn; ++ri) { 1189 1190 Vals64.push_back(true/*IsSingleNumber = true*/); 1191 1192 EmitAPInt(Vals64, Code, Abbrev, 1193 CaseRanges.getSingleNumber(ri), true); 1194 } 1195 } else 1196 for (unsigned ri = 0, rn = CaseRanges.getNumItems(); 1197 ri != rn; ++ri) { 1198 IntegersSubset::Range r = CaseRanges.getItem(ri); 1199 bool IsSingleNumber = CaseRanges.isSingleNumber(ri); 1200 1201 Vals64.push_back(IsSingleNumber); 1202 1203 EmitAPInt(Vals64, Code, Abbrev, r.getLow(), true); 1204 if (!IsSingleNumber) 1205 EmitAPInt(Vals64, Code, Abbrev, r.getHigh(), true); 1206 } 1207 } 1208 Vals64.push_back(VE.getValueID(i.getCaseSuccessor())); 1209 } 1210 1211 Stream.EmitRecord(Code, Vals64, AbbrevToUse); 1212 1213 // Also do expected action - clear external Vals collection: 1214 Vals.clear(); 1215 return; 1216 } 1217 break; 1218 case Instruction::IndirectBr: 1219 Code = bitc::FUNC_CODE_INST_INDIRECTBR; 1220 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); 1221 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) 1222 Vals.push_back(VE.getValueID(I.getOperand(i))); 1223 break; 1224 1225 case Instruction::Invoke: { 1226 const InvokeInst *II = cast<InvokeInst>(&I); 1227 const Value *Callee(II->getCalledValue()); 1228 PointerType *PTy = cast<PointerType>(Callee->getType()); 1229 FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); 1230 Code = bitc::FUNC_CODE_INST_INVOKE; 1231 1232 Vals.push_back(VE.getAttributeID(II->getAttributes())); 1233 Vals.push_back(II->getCallingConv()); 1234 Vals.push_back(VE.getValueID(II->getNormalDest())); 1235 Vals.push_back(VE.getValueID(II->getUnwindDest())); 1236 PushValueAndType(Callee, InstID, Vals, VE); 1237 1238 // Emit value #'s for the fixed parameters. 1239 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) 1240 Vals.push_back(VE.getValueID(I.getOperand(i))); // fixed param. 1241 1242 // Emit type/value pairs for varargs params. 1243 if (FTy->isVarArg()) { 1244 for (unsigned i = FTy->getNumParams(), e = I.getNumOperands()-3; 1245 i != e; ++i) 1246 PushValueAndType(I.getOperand(i), InstID, Vals, VE); // vararg 1247 } 1248 break; 1249 } 1250 case Instruction::Resume: 1251 Code = bitc::FUNC_CODE_INST_RESUME; 1252 PushValueAndType(I.getOperand(0), InstID, Vals, VE); 1253 break; 1254 case Instruction::Unreachable: 1255 Code = bitc::FUNC_CODE_INST_UNREACHABLE; 1256 AbbrevToUse = FUNCTION_INST_UNREACHABLE_ABBREV; 1257 break; 1258 1259 case Instruction::PHI: { 1260 const PHINode &PN = cast<PHINode>(I); 1261 Code = bitc::FUNC_CODE_INST_PHI; 1262 Vals.push_back(VE.getTypeID(PN.getType())); 1263 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) { 1264 Vals.push_back(VE.getValueID(PN.getIncomingValue(i))); 1265 Vals.push_back(VE.getValueID(PN.getIncomingBlock(i))); 1266 } 1267 break; 1268 } 1269 1270 case Instruction::LandingPad: { 1271 const LandingPadInst &LP = cast<LandingPadInst>(I); 1272 Code = bitc::FUNC_CODE_INST_LANDINGPAD; 1273 Vals.push_back(VE.getTypeID(LP.getType())); 1274 PushValueAndType(LP.getPersonalityFn(), InstID, Vals, VE); 1275 Vals.push_back(LP.isCleanup()); 1276 Vals.push_back(LP.getNumClauses()); 1277 for (unsigned I = 0, E = LP.getNumClauses(); I != E; ++I) { 1278 if (LP.isCatch(I)) 1279 Vals.push_back(LandingPadInst::Catch); 1280 else 1281 Vals.push_back(LandingPadInst::Filter); 1282 PushValueAndType(LP.getClause(I), InstID, Vals, VE); 1283 } 1284 break; 1285 } 1286 1287 case Instruction::Alloca: 1288 Code = bitc::FUNC_CODE_INST_ALLOCA; 1289 Vals.push_back(VE.getTypeID(I.getType())); 1290 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); 1291 Vals.push_back(VE.getValueID(I.getOperand(0))); // size. 1292 Vals.push_back(Log2_32(cast<AllocaInst>(I).getAlignment())+1); 1293 break; 1294 1295 case Instruction::Load: 1296 if (cast<LoadInst>(I).isAtomic()) { 1297 Code = bitc::FUNC_CODE_INST_LOADATOMIC; 1298 PushValueAndType(I.getOperand(0), InstID, Vals, VE); 1299 } else { 1300 Code = bitc::FUNC_CODE_INST_LOAD; 1301 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE)) // ptr 1302 AbbrevToUse = FUNCTION_INST_LOAD_ABBREV; 1303 } 1304 Vals.push_back(Log2_32(cast<LoadInst>(I).getAlignment())+1); 1305 Vals.push_back(cast<LoadInst>(I).isVolatile()); 1306 if (cast<LoadInst>(I).isAtomic()) { 1307 Vals.push_back(GetEncodedOrdering(cast<LoadInst>(I).getOrdering())); 1308 Vals.push_back(GetEncodedSynchScope(cast<LoadInst>(I).getSynchScope())); 1309 } 1310 break; 1311 case Instruction::Store: 1312 if (cast<StoreInst>(I).isAtomic()) 1313 Code = bitc::FUNC_CODE_INST_STOREATOMIC; 1314 else 1315 Code = bitc::FUNC_CODE_INST_STORE; 1316 PushValueAndType(I.getOperand(1), InstID, Vals, VE); // ptrty + ptr 1317 Vals.push_back(VE.getValueID(I.getOperand(0))); // val. 1318 Vals.push_back(Log2_32(cast<StoreInst>(I).getAlignment())+1); 1319 Vals.push_back(cast<StoreInst>(I).isVolatile()); 1320 if (cast<StoreInst>(I).isAtomic()) { 1321 Vals.push_back(GetEncodedOrdering(cast<StoreInst>(I).getOrdering())); 1322 Vals.push_back(GetEncodedSynchScope(cast<StoreInst>(I).getSynchScope())); 1323 } 1324 break; 1325 case Instruction::AtomicCmpXchg: 1326 Code = bitc::FUNC_CODE_INST_CMPXCHG; 1327 PushValueAndType(I.getOperand(0), InstID, Vals, VE); // ptrty + ptr 1328 Vals.push_back(VE.getValueID(I.getOperand(1))); // cmp. 1329 Vals.push_back(VE.getValueID(I.getOperand(2))); // newval. 1330 Vals.push_back(cast<AtomicCmpXchgInst>(I).isVolatile()); 1331 Vals.push_back(GetEncodedOrdering( 1332 cast<AtomicCmpXchgInst>(I).getOrdering())); 1333 Vals.push_back(GetEncodedSynchScope( 1334 cast<AtomicCmpXchgInst>(I).getSynchScope())); 1335 break; 1336 case Instruction::AtomicRMW: 1337 Code = bitc::FUNC_CODE_INST_ATOMICRMW; 1338 PushValueAndType(I.getOperand(0), InstID, Vals, VE); // ptrty + ptr 1339 Vals.push_back(VE.getValueID(I.getOperand(1))); // val. 1340 Vals.push_back(GetEncodedRMWOperation( 1341 cast<AtomicRMWInst>(I).getOperation())); 1342 Vals.push_back(cast<AtomicRMWInst>(I).isVolatile()); 1343 Vals.push_back(GetEncodedOrdering(cast<AtomicRMWInst>(I).getOrdering())); 1344 Vals.push_back(GetEncodedSynchScope( 1345 cast<AtomicRMWInst>(I).getSynchScope())); 1346 break; 1347 case Instruction::Fence: 1348 Code = bitc::FUNC_CODE_INST_FENCE; 1349 Vals.push_back(GetEncodedOrdering(cast<FenceInst>(I).getOrdering())); 1350 Vals.push_back(GetEncodedSynchScope(cast<FenceInst>(I).getSynchScope())); 1351 break; 1352 case Instruction::Call: { 1353 const CallInst &CI = cast<CallInst>(I); 1354 PointerType *PTy = cast<PointerType>(CI.getCalledValue()->getType()); 1355 FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); 1356 1357 Code = bitc::FUNC_CODE_INST_CALL; 1358 1359 Vals.push_back(VE.getAttributeID(CI.getAttributes())); 1360 Vals.push_back((CI.getCallingConv() << 1) | unsigned(CI.isTailCall())); 1361 PushValueAndType(CI.getCalledValue(), InstID, Vals, VE); // Callee 1362 1363 // Emit value #'s for the fixed parameters. 1364 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) 1365 Vals.push_back(VE.getValueID(CI.getArgOperand(i))); // fixed param. 1366 1367 // Emit type/value pairs for varargs params. 1368 if (FTy->isVarArg()) { 1369 for (unsigned i = FTy->getNumParams(), e = CI.getNumArgOperands(); 1370 i != e; ++i) 1371 PushValueAndType(CI.getArgOperand(i), InstID, Vals, VE); // varargs 1372 } 1373 break; 1374 } 1375 case Instruction::VAArg: 1376 Code = bitc::FUNC_CODE_INST_VAARG; 1377 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); // valistty 1378 Vals.push_back(VE.getValueID(I.getOperand(0))); // valist. 1379 Vals.push_back(VE.getTypeID(I.getType())); // restype. 1380 break; 1381 } 1382 1383 Stream.EmitRecord(Code, Vals, AbbrevToUse); 1384 Vals.clear(); 1385} 1386 1387// Emit names for globals/functions etc. 1388static void WriteValueSymbolTable(const ValueSymbolTable &VST, 1389 const llvm_3_2::ValueEnumerator &VE, 1390 BitstreamWriter &Stream) { 1391 if (VST.empty()) return; 1392 Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4); 1393 1394 // FIXME: Set up the abbrev, we know how many values there are! 1395 // FIXME: We know if the type names can use 7-bit ascii. 1396 SmallVector<unsigned, 64> NameVals; 1397 1398 for (ValueSymbolTable::const_iterator SI = VST.begin(), SE = VST.end(); 1399 SI != SE; ++SI) { 1400 1401 const ValueName &Name = *SI; 1402 1403 // Figure out the encoding to use for the name. 1404 bool is7Bit = true; 1405 bool isChar6 = true; 1406 for (const char *C = Name.getKeyData(), *E = C+Name.getKeyLength(); 1407 C != E; ++C) { 1408 if (isChar6) 1409 isChar6 = BitCodeAbbrevOp::isChar6(*C); 1410 if ((unsigned char)*C & 128) { 1411 is7Bit = false; 1412 break; // don't bother scanning the rest. 1413 } 1414 } 1415 1416 unsigned AbbrevToUse = VST_ENTRY_8_ABBREV; 1417 1418 // VST_ENTRY: [valueid, namechar x N] 1419 // VST_BBENTRY: [bbid, namechar x N] 1420 unsigned Code; 1421 if (isa<BasicBlock>(SI->getValue())) { 1422 Code = bitc::VST_CODE_BBENTRY; 1423 if (isChar6) 1424 AbbrevToUse = VST_BBENTRY_6_ABBREV; 1425 } else { 1426 Code = bitc::VST_CODE_ENTRY; 1427 if (isChar6) 1428 AbbrevToUse = VST_ENTRY_6_ABBREV; 1429 else if (is7Bit) 1430 AbbrevToUse = VST_ENTRY_7_ABBREV; 1431 } 1432 1433 NameVals.push_back(VE.getValueID(SI->getValue())); 1434 for (const char *P = Name.getKeyData(), 1435 *E = Name.getKeyData()+Name.getKeyLength(); P != E; ++P) 1436 NameVals.push_back((unsigned char)*P); 1437 1438 // Emit the finished record. 1439 Stream.EmitRecord(Code, NameVals, AbbrevToUse); 1440 NameVals.clear(); 1441 } 1442 Stream.ExitBlock(); 1443} 1444 1445/// WriteFunction - Emit a function body to the module stream. 1446static void WriteFunction(const Function &F, llvm_3_2::ValueEnumerator &VE, 1447 BitstreamWriter &Stream) { 1448 Stream.EnterSubblock(bitc::FUNCTION_BLOCK_ID, 4); 1449 VE.incorporateFunction(F); 1450 1451 SmallVector<unsigned, 64> Vals; 1452 1453 // Emit the number of basic blocks, so the reader can create them ahead of 1454 // time. 1455 Vals.push_back(VE.getBasicBlocks().size()); 1456 Stream.EmitRecord(bitc::FUNC_CODE_DECLAREBLOCKS, Vals); 1457 Vals.clear(); 1458 1459 // If there are function-local constants, emit them now. 1460 unsigned CstStart, CstEnd; 1461 VE.getFunctionConstantRange(CstStart, CstEnd); 1462 WriteConstants(CstStart, CstEnd, VE, Stream, false); 1463 1464 // If there is function-local metadata, emit it now. 1465 WriteFunctionLocalMetadata(F, VE, Stream); 1466 1467 // Keep a running idea of what the instruction ID is. 1468 unsigned InstID = CstEnd; 1469 1470 bool NeedsMetadataAttachment = false; 1471 1472 DebugLoc LastDL; 1473 1474 // Finally, emit all the instructions, in order. 1475 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) 1476 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); 1477 I != E; ++I) { 1478 WriteInstruction(*I, InstID, VE, Stream, Vals); 1479 1480 if (!I->getType()->isVoidTy()) 1481 ++InstID; 1482 1483 // If the instruction has metadata, write a metadata attachment later. 1484 NeedsMetadataAttachment |= I->hasMetadataOtherThanDebugLoc(); 1485 1486 // If the instruction has a debug location, emit it. 1487 DebugLoc DL = I->getDebugLoc(); 1488 if (DL.isUnknown()) { 1489 // nothing todo. 1490 } else if (DL == LastDL) { 1491 // Just repeat the same debug loc as last time. 1492 Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC_AGAIN, Vals); 1493 } else { 1494 MDNode *Scope, *IA; 1495 DL.getScopeAndInlinedAt(Scope, IA, I->getContext()); 1496 1497 Vals.push_back(DL.getLine()); 1498 Vals.push_back(DL.getCol()); 1499 Vals.push_back(Scope ? VE.getValueID(Scope)+1 : 0); 1500 Vals.push_back(IA ? VE.getValueID(IA)+1 : 0); 1501 Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC, Vals); 1502 Vals.clear(); 1503 1504 LastDL = DL; 1505 } 1506 } 1507 1508 // Emit names for all the instructions etc. 1509 WriteValueSymbolTable(F.getValueSymbolTable(), VE, Stream); 1510 1511 if (NeedsMetadataAttachment) 1512 WriteMetadataAttachment(F, VE, Stream); 1513 VE.purgeFunction(); 1514 Stream.ExitBlock(); 1515} 1516 1517// Emit blockinfo, which defines the standard abbreviations etc. 1518static void WriteBlockInfo(const llvm_3_2::ValueEnumerator &VE, 1519 BitstreamWriter &Stream) { 1520 // We only want to emit block info records for blocks that have multiple 1521 // instances: CONSTANTS_BLOCK, FUNCTION_BLOCK and VALUE_SYMTAB_BLOCK. Other 1522 // blocks can defined their abbrevs inline. 1523 Stream.EnterBlockInfoBlock(2); 1524 1525 { // 8-bit fixed-width VST_ENTRY/VST_BBENTRY strings. 1526 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1527 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); 1528 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 1529 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 1530 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); 1531 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, 1532 Abbv) != VST_ENTRY_8_ABBREV) 1533 llvm_unreachable("Unexpected abbrev ordering!"); 1534 } 1535 1536 { // 7-bit fixed width VST_ENTRY strings. 1537 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1538 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY)); 1539 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 1540 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 1541 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); 1542 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, 1543 Abbv) != VST_ENTRY_7_ABBREV) 1544 llvm_unreachable("Unexpected abbrev ordering!"); 1545 } 1546 { // 6-bit char6 VST_ENTRY strings. 1547 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1548 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY)); 1549 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 1550 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 1551 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6)); 1552 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, 1553 Abbv) != VST_ENTRY_6_ABBREV) 1554 llvm_unreachable("Unexpected abbrev ordering!"); 1555 } 1556 { // 6-bit char6 VST_BBENTRY strings. 1557 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1558 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_BBENTRY)); 1559 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 1560 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 1561 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6)); 1562 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, 1563 Abbv) != VST_BBENTRY_6_ABBREV) 1564 llvm_unreachable("Unexpected abbrev ordering!"); 1565 } 1566 1567 1568 1569 { // SETTYPE abbrev for CONSTANTS_BLOCK. 1570 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1571 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_SETTYPE)); 1572 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1573 Log2_32_Ceil(VE.getTypes().size()+1))); 1574 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, 1575 Abbv) != CONSTANTS_SETTYPE_ABBREV) 1576 llvm_unreachable("Unexpected abbrev ordering!"); 1577 } 1578 1579 { // INTEGER abbrev for CONSTANTS_BLOCK. 1580 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1581 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_INTEGER)); 1582 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 1583 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, 1584 Abbv) != CONSTANTS_INTEGER_ABBREV) 1585 llvm_unreachable("Unexpected abbrev ordering!"); 1586 } 1587 1588 { // CE_CAST abbrev for CONSTANTS_BLOCK. 1589 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1590 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CE_CAST)); 1591 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // cast opc 1592 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // typeid 1593 Log2_32_Ceil(VE.getTypes().size()+1))); 1594 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id 1595 1596 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, 1597 Abbv) != CONSTANTS_CE_CAST_Abbrev) 1598 llvm_unreachable("Unexpected abbrev ordering!"); 1599 } 1600 { // NULL abbrev for CONSTANTS_BLOCK. 1601 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1602 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_NULL)); 1603 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, 1604 Abbv) != CONSTANTS_NULL_Abbrev) 1605 llvm_unreachable("Unexpected abbrev ordering!"); 1606 } 1607 1608 // FIXME: This should only use space for first class types! 1609 1610 { // INST_LOAD abbrev for FUNCTION_BLOCK. 1611 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1612 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_LOAD)); 1613 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Ptr 1614 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // Align 1615 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // volatile 1616 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, 1617 Abbv) != FUNCTION_INST_LOAD_ABBREV) 1618 llvm_unreachable("Unexpected abbrev ordering!"); 1619 } 1620 { // INST_BINOP abbrev for FUNCTION_BLOCK. 1621 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1622 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP)); 1623 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS 1624 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS 1625 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc 1626 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, 1627 Abbv) != FUNCTION_INST_BINOP_ABBREV) 1628 llvm_unreachable("Unexpected abbrev ordering!"); 1629 } 1630 { // INST_BINOP_FLAGS abbrev for FUNCTION_BLOCK. 1631 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1632 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP)); 1633 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS 1634 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS 1635 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc 1636 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); // flags 1637 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, 1638 Abbv) != FUNCTION_INST_BINOP_FLAGS_ABBREV) 1639 llvm_unreachable("Unexpected abbrev ordering!"); 1640 } 1641 { // INST_CAST abbrev for FUNCTION_BLOCK. 1642 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1643 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_CAST)); 1644 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // OpVal 1645 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty 1646 Log2_32_Ceil(VE.getTypes().size()+1))); 1647 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc 1648 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, 1649 Abbv) != FUNCTION_INST_CAST_ABBREV) 1650 llvm_unreachable("Unexpected abbrev ordering!"); 1651 } 1652 1653 { // INST_RET abbrev for FUNCTION_BLOCK. 1654 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1655 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET)); 1656 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, 1657 Abbv) != FUNCTION_INST_RET_VOID_ABBREV) 1658 llvm_unreachable("Unexpected abbrev ordering!"); 1659 } 1660 { // INST_RET abbrev for FUNCTION_BLOCK. 1661 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1662 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET)); 1663 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ValID 1664 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, 1665 Abbv) != FUNCTION_INST_RET_VAL_ABBREV) 1666 llvm_unreachable("Unexpected abbrev ordering!"); 1667 } 1668 { // INST_UNREACHABLE abbrev for FUNCTION_BLOCK. 1669 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1670 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_UNREACHABLE)); 1671 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, 1672 Abbv) != FUNCTION_INST_UNREACHABLE_ABBREV) 1673 llvm_unreachable("Unexpected abbrev ordering!"); 1674 } 1675 1676 Stream.ExitBlock(); 1677} 1678 1679// Sort the Users based on the order in which the reader parses the bitcode 1680// file. 1681static bool bitcodereader_order(const User *lhs, const User *rhs) { 1682 // TODO: Implement. 1683 return true; 1684} 1685 1686static void WriteUseList(const Value *V, const llvm_3_2::ValueEnumerator &VE, 1687 BitstreamWriter &Stream) { 1688 1689 // One or zero uses can't get out of order. 1690 if (V->use_empty() || V->hasNUses(1)) 1691 return; 1692 1693 // Make a copy of the in-memory use-list for sorting. 1694 unsigned UseListSize = std::distance(V->use_begin(), V->use_end()); 1695 SmallVector<const User*, 8> UseList; 1696 UseList.reserve(UseListSize); 1697 for (Value::const_use_iterator I = V->use_begin(), E = V->use_end(); 1698 I != E; ++I) { 1699 const User *U = *I; 1700 UseList.push_back(U); 1701 } 1702 1703 // Sort the copy based on the order read by the BitcodeReader. 1704 std::sort(UseList.begin(), UseList.end(), bitcodereader_order); 1705 1706 // TODO: Generate a diff between the BitcodeWriter in-memory use-list and the 1707 // sorted list (i.e., the expected BitcodeReader in-memory use-list). 1708 1709 // TODO: Emit the USELIST_CODE_ENTRYs. 1710} 1711 1712static void WriteFunctionUseList(const Function *F, 1713 llvm_3_2::ValueEnumerator &VE, 1714 BitstreamWriter &Stream) { 1715 VE.incorporateFunction(*F); 1716 1717 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end(); 1718 AI != AE; ++AI) 1719 WriteUseList(AI, VE, Stream); 1720 for (Function::const_iterator BB = F->begin(), FE = F->end(); BB != FE; 1721 ++BB) { 1722 WriteUseList(BB, VE, Stream); 1723 for (BasicBlock::const_iterator II = BB->begin(), IE = BB->end(); II != IE; 1724 ++II) { 1725 WriteUseList(II, VE, Stream); 1726 for (User::const_op_iterator OI = II->op_begin(), E = II->op_end(); 1727 OI != E; ++OI) { 1728 if ((isa<Constant>(*OI) && !isa<GlobalValue>(*OI)) || 1729 isa<InlineAsm>(*OI)) 1730 WriteUseList(*OI, VE, Stream); 1731 } 1732 } 1733 } 1734 VE.purgeFunction(); 1735} 1736 1737// Emit use-lists. 1738static void WriteModuleUseLists(const Module *M, llvm_3_2::ValueEnumerator &VE, 1739 BitstreamWriter &Stream) { 1740 Stream.EnterSubblock(bitc::USELIST_BLOCK_ID, 3); 1741 1742 // XXX: this modifies the module, but in a way that should never change the 1743 // behavior of any pass or codegen in LLVM. The problem is that GVs may 1744 // contain entries in the use_list that do not exist in the Module and are 1745 // not stored in the .bc file. 1746 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end(); 1747 I != E; ++I) 1748 I->removeDeadConstantUsers(); 1749 1750 // Write the global variables. 1751 for (Module::const_global_iterator GI = M->global_begin(), 1752 GE = M->global_end(); GI != GE; ++GI) { 1753 WriteUseList(GI, VE, Stream); 1754 1755 // Write the global variable initializers. 1756 if (GI->hasInitializer()) 1757 WriteUseList(GI->getInitializer(), VE, Stream); 1758 } 1759 1760 // Write the functions. 1761 for (Module::const_iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI) { 1762 WriteUseList(FI, VE, Stream); 1763 if (!FI->isDeclaration()) 1764 WriteFunctionUseList(FI, VE, Stream); 1765 } 1766 1767 // Write the aliases. 1768 for (Module::const_alias_iterator AI = M->alias_begin(), AE = M->alias_end(); 1769 AI != AE; ++AI) { 1770 WriteUseList(AI, VE, Stream); 1771 WriteUseList(AI->getAliasee(), VE, Stream); 1772 } 1773 1774 Stream.ExitBlock(); 1775} 1776 1777/// WriteModule - Emit the specified module to the bitstream. 1778static void WriteModule(const Module *M, BitstreamWriter &Stream) { 1779 Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3); 1780 1781 // Emit the version number if it is non-zero. 1782 if (CurVersion) { 1783 SmallVector<unsigned, 1> Vals; 1784 Vals.push_back(CurVersion); 1785 Stream.EmitRecord(bitc::MODULE_CODE_VERSION, Vals); 1786 } 1787 1788 // Analyze the module, enumerating globals, functions, etc. 1789 llvm_3_2::ValueEnumerator VE(M); 1790 1791 // Emit blockinfo, which defines the standard abbreviations etc. 1792 WriteBlockInfo(VE, Stream); 1793 1794 // Emit information about parameter attributes. 1795 WriteAttributeTable(VE, Stream); 1796 1797 // Emit information describing all of the types in the module. 1798 WriteTypeTable(VE, Stream); 1799 1800 // Emit top-level description of module, including target triple, inline asm, 1801 // descriptors for global variables, and function prototype info. 1802 WriteModuleInfo(M, VE, Stream); 1803 1804 // Emit constants. 1805 WriteModuleConstants(VE, Stream); 1806 1807 // Emit metadata. 1808 WriteModuleMetadata(M, VE, Stream); 1809 1810 // Emit metadata. 1811 WriteModuleMetadataStore(M, Stream); 1812 1813 // Emit names for globals/functions etc. 1814 WriteValueSymbolTable(M->getValueSymbolTable(), VE, Stream); 1815 1816 // Emit use-lists. 1817 if (EnablePreserveUseListOrdering) 1818 WriteModuleUseLists(M, VE, Stream); 1819 1820 // Emit function bodies. 1821 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) 1822 if (!F->isDeclaration()) 1823 WriteFunction(*F, VE, Stream); 1824 1825 Stream.ExitBlock(); 1826} 1827 1828/// EmitDarwinBCHeader - If generating a bc file on darwin, we have to emit a 1829/// header and trailer to make it compatible with the system archiver. To do 1830/// this we emit the following header, and then emit a trailer that pads the 1831/// file out to be a multiple of 16 bytes. 1832/// 1833/// struct bc_header { 1834/// uint32_t Magic; // 0x0B17C0DE 1835/// uint32_t Version; // Version, currently always 0. 1836/// uint32_t BitcodeOffset; // Offset to traditional bitcode file. 1837/// uint32_t BitcodeSize; // Size of traditional bitcode file. 1838/// uint32_t CPUType; // CPU specifier. 1839/// ... potentially more later ... 1840/// }; 1841enum { 1842 DarwinBCSizeFieldOffset = 3*4, // Offset to bitcode_size. 1843 DarwinBCHeaderSize = 5*4 1844}; 1845 1846static void WriteInt32ToBuffer(uint32_t Value, SmallVectorImpl<char> &Buffer, 1847 uint32_t &Position) { 1848 Buffer[Position + 0] = (unsigned char) (Value >> 0); 1849 Buffer[Position + 1] = (unsigned char) (Value >> 8); 1850 Buffer[Position + 2] = (unsigned char) (Value >> 16); 1851 Buffer[Position + 3] = (unsigned char) (Value >> 24); 1852 Position += 4; 1853} 1854 1855static void EmitDarwinBCHeaderAndTrailer(SmallVectorImpl<char> &Buffer, 1856 const Triple &TT) { 1857 unsigned CPUType = ~0U; 1858 1859 // Match x86_64-*, i[3-9]86-*, powerpc-*, powerpc64-*, arm-*, thumb-*, 1860 // armv[0-9]-*, thumbv[0-9]-*, armv5te-*, or armv6t2-*. The CPUType is a magic 1861 // number from /usr/include/mach/machine.h. It is ok to reproduce the 1862 // specific constants here because they are implicitly part of the Darwin ABI. 1863 enum { 1864 DARWIN_CPU_ARCH_ABI64 = 0x01000000, 1865 DARWIN_CPU_TYPE_X86 = 7, 1866 DARWIN_CPU_TYPE_ARM = 12, 1867 DARWIN_CPU_TYPE_POWERPC = 18 1868 }; 1869 1870 Triple::ArchType Arch = TT.getArch(); 1871 if (Arch == Triple::x86_64) 1872 CPUType = DARWIN_CPU_TYPE_X86 | DARWIN_CPU_ARCH_ABI64; 1873 else if (Arch == Triple::x86) 1874 CPUType = DARWIN_CPU_TYPE_X86; 1875 else if (Arch == Triple::ppc) 1876 CPUType = DARWIN_CPU_TYPE_POWERPC; 1877 else if (Arch == Triple::ppc64) 1878 CPUType = DARWIN_CPU_TYPE_POWERPC | DARWIN_CPU_ARCH_ABI64; 1879 else if (Arch == Triple::arm || Arch == Triple::thumb) 1880 CPUType = DARWIN_CPU_TYPE_ARM; 1881 1882 // Traditional Bitcode starts after header. 1883 assert(Buffer.size() >= DarwinBCHeaderSize && 1884 "Expected header size to be reserved"); 1885 unsigned BCOffset = DarwinBCHeaderSize; 1886 unsigned BCSize = Buffer.size()-DarwinBCHeaderSize; 1887 1888 // Write the magic and version. 1889 unsigned Position = 0; 1890 WriteInt32ToBuffer(0x0B17C0DE , Buffer, Position); 1891 WriteInt32ToBuffer(0 , Buffer, Position); // Version. 1892 WriteInt32ToBuffer(BCOffset , Buffer, Position); 1893 WriteInt32ToBuffer(BCSize , Buffer, Position); 1894 WriteInt32ToBuffer(CPUType , Buffer, Position); 1895 1896 // If the file is not a multiple of 16 bytes, insert dummy padding. 1897 while (Buffer.size() & 15) 1898 Buffer.push_back(0); 1899} 1900 1901/// WriteBitcodeToFile - Write the specified module to the specified output 1902/// stream. 1903void llvm_3_2::WriteBitcodeToFile(const Module *M, raw_ostream &Out) { 1904 SmallVector<char, 1024> Buffer; 1905 Buffer.reserve(256*1024); 1906 1907 // If this is darwin or another generic macho target, reserve space for the 1908 // header. 1909 Triple TT(M->getTargetTriple()); 1910 if (TT.isOSDarwin()) 1911 Buffer.insert(Buffer.begin(), DarwinBCHeaderSize, 0); 1912 1913 // Emit the module into the buffer. 1914 { 1915 BitstreamWriter Stream(Buffer); 1916 1917 // Emit the file header. 1918 Stream.Emit((unsigned)'B', 8); 1919 Stream.Emit((unsigned)'C', 8); 1920 Stream.Emit(0x0, 4); 1921 Stream.Emit(0xC, 4); 1922 Stream.Emit(0xE, 4); 1923 Stream.Emit(0xD, 4); 1924 1925 // Emit the module. 1926 WriteModule(M, Stream); 1927 } 1928 1929 if (TT.isOSDarwin()) 1930 EmitDarwinBCHeaderAndTrailer(Buffer, TT); 1931 1932 // Write the generated bitstream to "Out". 1933 Out.write((char*)&Buffer.front(), Buffer.size()); 1934} 1935