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 "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/MDNode.h" 23 #include "llvm/Module.h" 24 #include "llvm/TypeSymbolTable.h" 25 #include "llvm/ValueSymbolTable.h" 26 #include "llvm/Support/MathExtras.h" 27 #include "llvm/Support/Streams.h" 28 #include "llvm/Support/raw_ostream.h" 29 #include "llvm/System/Program.h" 30 using namespace llvm; 31 32 /// These are manifest constants used by the bitcode writer. They do not need to 33 /// be kept in sync with the reader, but need to be consistent within this file. 34 enum { 35 CurVersion = 0, 36 37 // VALUE_SYMTAB_BLOCK abbrev id's. 38 VST_ENTRY_8_ABBREV = bitc::FIRST_APPLICATION_ABBREV, 39 VST_ENTRY_7_ABBREV, 40 VST_ENTRY_6_ABBREV, 41 VST_BBENTRY_6_ABBREV, 42 43 // CONSTANTS_BLOCK abbrev id's. 44 CONSTANTS_SETTYPE_ABBREV = bitc::FIRST_APPLICATION_ABBREV, 45 CONSTANTS_INTEGER_ABBREV, 46 CONSTANTS_CE_CAST_Abbrev, 47 CONSTANTS_NULL_Abbrev, 48 49 // FUNCTION_BLOCK abbrev id's. 50 FUNCTION_INST_LOAD_ABBREV = bitc::FIRST_APPLICATION_ABBREV, 51 FUNCTION_INST_BINOP_ABBREV, 52 FUNCTION_INST_CAST_ABBREV, 53 FUNCTION_INST_RET_VOID_ABBREV, 54 FUNCTION_INST_RET_VAL_ABBREV, 55 FUNCTION_INST_UNREACHABLE_ABBREV 56 }; 57 58 59 static unsigned GetEncodedCastOpcode(unsigned Opcode) { 60 switch (Opcode) { 61 default: assert(0 && "Unknown cast instruction!"); 62 case Instruction::Trunc : return bitc::CAST_TRUNC; 63 case Instruction::ZExt : return bitc::CAST_ZEXT; 64 case Instruction::SExt : return bitc::CAST_SEXT; 65 case Instruction::FPToUI : return bitc::CAST_FPTOUI; 66 case Instruction::FPToSI : return bitc::CAST_FPTOSI; 67 case Instruction::UIToFP : return bitc::CAST_UITOFP; 68 case Instruction::SIToFP : return bitc::CAST_SITOFP; 69 case Instruction::FPTrunc : return bitc::CAST_FPTRUNC; 70 case Instruction::FPExt : return bitc::CAST_FPEXT; 71 case Instruction::PtrToInt: return bitc::CAST_PTRTOINT; 72 case Instruction::IntToPtr: return bitc::CAST_INTTOPTR; 73 case Instruction::BitCast : return bitc::CAST_BITCAST; 74 } 75 } 76 77 static unsigned GetEncodedBinaryOpcode(unsigned Opcode) { 78 switch (Opcode) { 79 default: assert(0 && "Unknown binary instruction!"); 80 case Instruction::Add: 81 case Instruction::FAdd: return bitc::BINOP_ADD; 82 case Instruction::Sub: 83 case Instruction::FSub: return bitc::BINOP_SUB; 84 case Instruction::Mul: 85 case Instruction::FMul: return bitc::BINOP_MUL; 86 case Instruction::UDiv: return bitc::BINOP_UDIV; 87 case Instruction::FDiv: 88 case Instruction::SDiv: return bitc::BINOP_SDIV; 89 case Instruction::URem: return bitc::BINOP_UREM; 90 case Instruction::FRem: 91 case Instruction::SRem: return bitc::BINOP_SREM; 92 case Instruction::Shl: return bitc::BINOP_SHL; 93 case Instruction::LShr: return bitc::BINOP_LSHR; 94 case Instruction::AShr: return bitc::BINOP_ASHR; 95 case Instruction::And: return bitc::BINOP_AND; 96 case Instruction::Or: return bitc::BINOP_OR; 97 case Instruction::Xor: return bitc::BINOP_XOR; 98 } 99 } 100 101 102 103 static void WriteStringRecord(unsigned Code, const std::string &Str, 104 unsigned AbbrevToUse, BitstreamWriter &Stream) { 105 SmallVector<unsigned, 64> Vals; 106 107 // Code: [strchar x N] 108 for (unsigned i = 0, e = Str.size(); i != e; ++i) 109 Vals.push_back(Str[i]); 110 111 // Emit the finished record. 112 Stream.EmitRecord(Code, Vals, AbbrevToUse); 113 } 114 115 // Emit information about parameter attributes. 116 static void WriteAttributeTable(const ValueEnumerator &VE, 117 BitstreamWriter &Stream) { 118 const std::vector<AttrListPtr> &Attrs = VE.getAttributes(); 119 if (Attrs.empty()) return; 120 121 Stream.EnterSubblock(bitc::PARAMATTR_BLOCK_ID, 3); 122 123 SmallVector<uint64_t, 64> Record; 124 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) { 125 const AttrListPtr &A = Attrs[i]; 126 for (unsigned i = 0, e = A.getNumSlots(); i != e; ++i) { 127 const AttributeWithIndex &PAWI = A.getSlot(i); 128 Record.push_back(PAWI.Index); 129 130 // FIXME: remove in LLVM 3.0 131 // Store the alignment in the bitcode as a 16-bit raw value instead of a 132 // 5-bit log2 encoded value. Shift the bits above the alignment up by 133 // 11 bits. 134 uint64_t FauxAttr = PAWI.Attrs & 0xffff; 135 if (PAWI.Attrs & Attribute::Alignment) 136 FauxAttr |= (1ull<<16)<<(((PAWI.Attrs & Attribute::Alignment)-1) >> 16); 137 FauxAttr |= (PAWI.Attrs & (0x3FFull << 21)) << 11; 138 139 Record.push_back(FauxAttr); 140 } 141 142 Stream.EmitRecord(bitc::PARAMATTR_CODE_ENTRY, Record); 143 Record.clear(); 144 } 145 146 Stream.ExitBlock(); 147 } 148 149 /// WriteTypeTable - Write out the type table for a module. 150 static void WriteTypeTable(const ValueEnumerator &VE, BitstreamWriter &Stream) { 151 const ValueEnumerator::TypeList &TypeList = VE.getTypes(); 152 153 Stream.EnterSubblock(bitc::TYPE_BLOCK_ID, 4 /*count from # abbrevs */); 154 SmallVector<uint64_t, 64> TypeVals; 155 156 // Abbrev for TYPE_CODE_POINTER. 157 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 158 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_POINTER)); 159 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 160 Log2_32_Ceil(VE.getTypes().size()+1))); 161 Abbv->Add(BitCodeAbbrevOp(0)); // Addrspace = 0 162 unsigned PtrAbbrev = Stream.EmitAbbrev(Abbv); 163 164 // Abbrev for TYPE_CODE_FUNCTION. 165 Abbv = new BitCodeAbbrev(); 166 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_FUNCTION)); 167 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isvararg 168 Abbv->Add(BitCodeAbbrevOp(0)); // FIXME: DEAD value, remove in LLVM 3.0 169 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 170 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 171 Log2_32_Ceil(VE.getTypes().size()+1))); 172 unsigned FunctionAbbrev = Stream.EmitAbbrev(Abbv); 173 174 // Abbrev for TYPE_CODE_STRUCT. 175 Abbv = new BitCodeAbbrev(); 176 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT)); 177 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked 178 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 179 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 180 Log2_32_Ceil(VE.getTypes().size()+1))); 181 unsigned StructAbbrev = Stream.EmitAbbrev(Abbv); 182 183 // Abbrev for TYPE_CODE_ARRAY. 184 Abbv = new BitCodeAbbrev(); 185 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_ARRAY)); 186 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // size 187 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 188 Log2_32_Ceil(VE.getTypes().size()+1))); 189 unsigned ArrayAbbrev = Stream.EmitAbbrev(Abbv); 190 191 // Emit an entry count so the reader can reserve space. 192 TypeVals.push_back(TypeList.size()); 193 Stream.EmitRecord(bitc::TYPE_CODE_NUMENTRY, TypeVals); 194 TypeVals.clear(); 195 196 // Loop over all of the types, emitting each in turn. 197 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) { 198 const Type *T = TypeList[i].first; 199 int AbbrevToUse = 0; 200 unsigned Code = 0; 201 202 switch (T->getTypeID()) { 203 default: assert(0 && "Unknown type!"); 204 case Type::VoidTyID: Code = bitc::TYPE_CODE_VOID; break; 205 case Type::FloatTyID: Code = bitc::TYPE_CODE_FLOAT; break; 206 case Type::DoubleTyID: Code = bitc::TYPE_CODE_DOUBLE; break; 207 case Type::X86_FP80TyID: Code = bitc::TYPE_CODE_X86_FP80; break; 208 case Type::FP128TyID: Code = bitc::TYPE_CODE_FP128; break; 209 case Type::PPC_FP128TyID: Code = bitc::TYPE_CODE_PPC_FP128; break; 210 case Type::LabelTyID: Code = bitc::TYPE_CODE_LABEL; break; 211 case Type::OpaqueTyID: Code = bitc::TYPE_CODE_OPAQUE; break; 212 case Type::MetadataTyID: Code = bitc::TYPE_CODE_METADATA; break; 213 case Type::IntegerTyID: 214 // INTEGER: [width] 215 Code = bitc::TYPE_CODE_INTEGER; 216 TypeVals.push_back(cast<IntegerType>(T)->getBitWidth()); 217 break; 218 case Type::PointerTyID: { 219 const PointerType *PTy = cast<PointerType>(T); 220 // POINTER: [pointee type, address space] 221 Code = bitc::TYPE_CODE_POINTER; 222 TypeVals.push_back(VE.getTypeID(PTy->getElementType())); 223 unsigned AddressSpace = PTy->getAddressSpace(); 224 TypeVals.push_back(AddressSpace); 225 if (AddressSpace == 0) AbbrevToUse = PtrAbbrev; 226 break; 227 } 228 case Type::FunctionTyID: { 229 const FunctionType *FT = cast<FunctionType>(T); 230 // FUNCTION: [isvararg, attrid, retty, paramty x N] 231 Code = bitc::TYPE_CODE_FUNCTION; 232 TypeVals.push_back(FT->isVarArg()); 233 TypeVals.push_back(0); // FIXME: DEAD: remove in llvm 3.0 234 TypeVals.push_back(VE.getTypeID(FT->getReturnType())); 235 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) 236 TypeVals.push_back(VE.getTypeID(FT->getParamType(i))); 237 AbbrevToUse = FunctionAbbrev; 238 break; 239 } 240 case Type::StructTyID: { 241 const StructType *ST = cast<StructType>(T); 242 // STRUCT: [ispacked, eltty x N] 243 Code = bitc::TYPE_CODE_STRUCT; 244 TypeVals.push_back(ST->isPacked()); 245 // Output all of the element types. 246 for (StructType::element_iterator I = ST->element_begin(), 247 E = ST->element_end(); I != E; ++I) 248 TypeVals.push_back(VE.getTypeID(*I)); 249 AbbrevToUse = StructAbbrev; 250 break; 251 } 252 case Type::ArrayTyID: { 253 const ArrayType *AT = cast<ArrayType>(T); 254 // ARRAY: [numelts, eltty] 255 Code = bitc::TYPE_CODE_ARRAY; 256 TypeVals.push_back(AT->getNumElements()); 257 TypeVals.push_back(VE.getTypeID(AT->getElementType())); 258 AbbrevToUse = ArrayAbbrev; 259 break; 260 } 261 case Type::VectorTyID: { 262 const VectorType *VT = cast<VectorType>(T); 263 // VECTOR [numelts, eltty] 264 Code = bitc::TYPE_CODE_VECTOR; 265 TypeVals.push_back(VT->getNumElements()); 266 TypeVals.push_back(VE.getTypeID(VT->getElementType())); 267 break; 268 } 269 } 270 271 // Emit the finished record. 272 Stream.EmitRecord(Code, TypeVals, AbbrevToUse); 273 TypeVals.clear(); 274 } 275 276 Stream.ExitBlock(); 277 } 278 279 static unsigned getEncodedLinkage(const GlobalValue *GV) { 280 switch (GV->getLinkage()) { 281 default: assert(0 && "Invalid linkage!"); 282 case GlobalValue::GhostLinkage: // Map ghost linkage onto external. 283 case GlobalValue::ExternalLinkage: return 0; 284 case GlobalValue::WeakAnyLinkage: return 1; 285 case GlobalValue::AppendingLinkage: return 2; 286 case GlobalValue::InternalLinkage: return 3; 287 case GlobalValue::LinkOnceAnyLinkage: return 4; 288 case GlobalValue::DLLImportLinkage: return 5; 289 case GlobalValue::DLLExportLinkage: return 6; 290 case GlobalValue::ExternalWeakLinkage: return 7; 291 case GlobalValue::CommonLinkage: return 8; 292 case GlobalValue::PrivateLinkage: return 9; 293 case GlobalValue::WeakODRLinkage: return 10; 294 case GlobalValue::LinkOnceODRLinkage: return 11; 295 case GlobalValue::AvailableExternallyLinkage: return 12; 296 } 297 } 298 299 static unsigned getEncodedVisibility(const GlobalValue *GV) { 300 switch (GV->getVisibility()) { 301 default: assert(0 && "Invalid visibility!"); 302 case GlobalValue::DefaultVisibility: return 0; 303 case GlobalValue::HiddenVisibility: return 1; 304 case GlobalValue::ProtectedVisibility: return 2; 305 } 306 } 307 308 // Emit top-level description of module, including target triple, inline asm, 309 // descriptors for global variables, and function prototype info. 310 static void WriteModuleInfo(const Module *M, const ValueEnumerator &VE, 311 BitstreamWriter &Stream) { 312 // Emit the list of dependent libraries for the Module. 313 for (Module::lib_iterator I = M->lib_begin(), E = M->lib_end(); I != E; ++I) 314 WriteStringRecord(bitc::MODULE_CODE_DEPLIB, *I, 0/*TODO*/, Stream); 315 316 // Emit various pieces of data attached to a module. 317 if (!M->getTargetTriple().empty()) 318 WriteStringRecord(bitc::MODULE_CODE_TRIPLE, M->getTargetTriple(), 319 0/*TODO*/, Stream); 320 if (!M->getDataLayout().empty()) 321 WriteStringRecord(bitc::MODULE_CODE_DATALAYOUT, M->getDataLayout(), 322 0/*TODO*/, Stream); 323 if (!M->getModuleInlineAsm().empty()) 324 WriteStringRecord(bitc::MODULE_CODE_ASM, M->getModuleInlineAsm(), 325 0/*TODO*/, Stream); 326 327 // Emit information about sections and GC, computing how many there are. Also 328 // compute the maximum alignment value. 329 std::map<std::string, unsigned> SectionMap; 330 std::map<std::string, unsigned> GCMap; 331 unsigned MaxAlignment = 0; 332 unsigned MaxGlobalType = 0; 333 for (Module::const_global_iterator GV = M->global_begin(),E = M->global_end(); 334 GV != E; ++GV) { 335 MaxAlignment = std::max(MaxAlignment, GV->getAlignment()); 336 MaxGlobalType = std::max(MaxGlobalType, VE.getTypeID(GV->getType())); 337 338 if (!GV->hasSection()) continue; 339 // Give section names unique ID's. 340 unsigned &Entry = SectionMap[GV->getSection()]; 341 if (Entry != 0) continue; 342 WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, GV->getSection(), 343 0/*TODO*/, Stream); 344 Entry = SectionMap.size(); 345 } 346 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) { 347 MaxAlignment = std::max(MaxAlignment, F->getAlignment()); 348 if (F->hasSection()) { 349 // Give section names unique ID's. 350 unsigned &Entry = SectionMap[F->getSection()]; 351 if (!Entry) { 352 WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, F->getSection(), 353 0/*TODO*/, Stream); 354 Entry = SectionMap.size(); 355 } 356 } 357 if (F->hasGC()) { 358 // Same for GC names. 359 unsigned &Entry = GCMap[F->getGC()]; 360 if (!Entry) { 361 WriteStringRecord(bitc::MODULE_CODE_GCNAME, F->getGC(), 362 0/*TODO*/, Stream); 363 Entry = GCMap.size(); 364 } 365 } 366 } 367 368 // Emit abbrev for globals, now that we know # sections and max alignment. 369 unsigned SimpleGVarAbbrev = 0; 370 if (!M->global_empty()) { 371 // Add an abbrev for common globals with no visibility or thread localness. 372 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 373 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_GLOBALVAR)); 374 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 375 Log2_32_Ceil(MaxGlobalType+1))); 376 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Constant. 377 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Initializer. 378 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // Linkage. 379 if (MaxAlignment == 0) // Alignment. 380 Abbv->Add(BitCodeAbbrevOp(0)); 381 else { 382 unsigned MaxEncAlignment = Log2_32(MaxAlignment)+1; 383 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 384 Log2_32_Ceil(MaxEncAlignment+1))); 385 } 386 if (SectionMap.empty()) // Section. 387 Abbv->Add(BitCodeAbbrevOp(0)); 388 else 389 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 390 Log2_32_Ceil(SectionMap.size()+1))); 391 // Don't bother emitting vis + thread local. 392 SimpleGVarAbbrev = Stream.EmitAbbrev(Abbv); 393 } 394 395 // Emit the global variable information. 396 SmallVector<unsigned, 64> Vals; 397 for (Module::const_global_iterator GV = M->global_begin(),E = M->global_end(); 398 GV != E; ++GV) { 399 unsigned AbbrevToUse = 0; 400 401 // GLOBALVAR: [type, isconst, initid, 402 // linkage, alignment, section, visibility, threadlocal] 403 Vals.push_back(VE.getTypeID(GV->getType())); 404 Vals.push_back(GV->isConstant()); 405 Vals.push_back(GV->isDeclaration() ? 0 : 406 (VE.getValueID(GV->getInitializer()) + 1)); 407 Vals.push_back(getEncodedLinkage(GV)); 408 Vals.push_back(Log2_32(GV->getAlignment())+1); 409 Vals.push_back(GV->hasSection() ? SectionMap[GV->getSection()] : 0); 410 if (GV->isThreadLocal() || 411 GV->getVisibility() != GlobalValue::DefaultVisibility) { 412 Vals.push_back(getEncodedVisibility(GV)); 413 Vals.push_back(GV->isThreadLocal()); 414 } else { 415 AbbrevToUse = SimpleGVarAbbrev; 416 } 417 418 Stream.EmitRecord(bitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse); 419 Vals.clear(); 420 } 421 422 // Emit the function proto information. 423 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) { 424 // FUNCTION: [type, callingconv, isproto, paramattr, 425 // linkage, alignment, section, visibility, gc] 426 Vals.push_back(VE.getTypeID(F->getType())); 427 Vals.push_back(F->getCallingConv()); 428 Vals.push_back(F->isDeclaration()); 429 Vals.push_back(getEncodedLinkage(F)); 430 Vals.push_back(VE.getAttributeID(F->getAttributes())); 431 Vals.push_back(Log2_32(F->getAlignment())+1); 432 Vals.push_back(F->hasSection() ? SectionMap[F->getSection()] : 0); 433 Vals.push_back(getEncodedVisibility(F)); 434 Vals.push_back(F->hasGC() ? GCMap[F->getGC()] : 0); 435 436 unsigned AbbrevToUse = 0; 437 Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse); 438 Vals.clear(); 439 } 440 441 442 // Emit the alias information. 443 for (Module::const_alias_iterator AI = M->alias_begin(), E = M->alias_end(); 444 AI != E; ++AI) { 445 Vals.push_back(VE.getTypeID(AI->getType())); 446 Vals.push_back(VE.getValueID(AI->getAliasee())); 447 Vals.push_back(getEncodedLinkage(AI)); 448 Vals.push_back(getEncodedVisibility(AI)); 449 unsigned AbbrevToUse = 0; 450 Stream.EmitRecord(bitc::MODULE_CODE_ALIAS, Vals, AbbrevToUse); 451 Vals.clear(); 452 } 453 } 454 455 456 static void WriteConstants(unsigned FirstVal, unsigned LastVal, 457 const ValueEnumerator &VE, 458 BitstreamWriter &Stream, bool isGlobal) { 459 if (FirstVal == LastVal) return; 460 461 Stream.EnterSubblock(bitc::CONSTANTS_BLOCK_ID, 4); 462 463 unsigned AggregateAbbrev = 0; 464 unsigned String8Abbrev = 0; 465 unsigned CString7Abbrev = 0; 466 unsigned CString6Abbrev = 0; 467 unsigned MDString8Abbrev = 0; 468 unsigned MDString6Abbrev = 0; 469 // If this is a constant pool for the module, emit module-specific abbrevs. 470 if (isGlobal) { 471 // Abbrev for CST_CODE_AGGREGATE. 472 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 473 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_AGGREGATE)); 474 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 475 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, Log2_32_Ceil(LastVal+1))); 476 AggregateAbbrev = Stream.EmitAbbrev(Abbv); 477 478 // Abbrev for CST_CODE_STRING. 479 Abbv = new BitCodeAbbrev(); 480 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_STRING)); 481 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 482 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); 483 String8Abbrev = Stream.EmitAbbrev(Abbv); 484 // Abbrev for CST_CODE_CSTRING. 485 Abbv = new BitCodeAbbrev(); 486 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING)); 487 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 488 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); 489 CString7Abbrev = Stream.EmitAbbrev(Abbv); 490 // Abbrev for CST_CODE_CSTRING. 491 Abbv = new BitCodeAbbrev(); 492 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING)); 493 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 494 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6)); 495 CString6Abbrev = Stream.EmitAbbrev(Abbv); 496 497 // Abbrev for CST_CODE_MDSTRING. 498 Abbv = new BitCodeAbbrev(); 499 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_MDSTRING)); 500 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 501 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); 502 MDString8Abbrev = Stream.EmitAbbrev(Abbv); 503 // Abbrev for CST_CODE_MDSTRING. 504 Abbv = new BitCodeAbbrev(); 505 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_MDSTRING)); 506 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 507 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6)); 508 MDString6Abbrev = Stream.EmitAbbrev(Abbv); 509 } 510 511 SmallVector<uint64_t, 64> Record; 512 513 const ValueEnumerator::ValueList &Vals = VE.getValues(); 514 const Type *LastTy = 0; 515 for (unsigned i = FirstVal; i != LastVal; ++i) { 516 const Value *V = Vals[i].first; 517 // If we need to switch types, do so now. 518 if (V->getType() != LastTy) { 519 LastTy = V->getType(); 520 Record.push_back(VE.getTypeID(LastTy)); 521 Stream.EmitRecord(bitc::CST_CODE_SETTYPE, Record, 522 CONSTANTS_SETTYPE_ABBREV); 523 Record.clear(); 524 } 525 526 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) { 527 Record.push_back(unsigned(IA->hasSideEffects())); 528 529 // Add the asm string. 530 const std::string &AsmStr = IA->getAsmString(); 531 Record.push_back(AsmStr.size()); 532 for (unsigned i = 0, e = AsmStr.size(); i != e; ++i) 533 Record.push_back(AsmStr[i]); 534 535 // Add the constraint string. 536 const std::string &ConstraintStr = IA->getConstraintString(); 537 Record.push_back(ConstraintStr.size()); 538 for (unsigned i = 0, e = ConstraintStr.size(); i != e; ++i) 539 Record.push_back(ConstraintStr[i]); 540 Stream.EmitRecord(bitc::CST_CODE_INLINEASM, Record); 541 Record.clear(); 542 continue; 543 } 544 const Constant *C = cast<Constant>(V); 545 unsigned Code = -1U; 546 unsigned AbbrevToUse = 0; 547 if (C->isNullValue()) { 548 Code = bitc::CST_CODE_NULL; 549 } else if (isa<UndefValue>(C)) { 550 Code = bitc::CST_CODE_UNDEF; 551 } else if (const ConstantInt *IV = dyn_cast<ConstantInt>(C)) { 552 if (IV->getBitWidth() <= 64) { 553 int64_t V = IV->getSExtValue(); 554 if (V >= 0) 555 Record.push_back(V << 1); 556 else 557 Record.push_back((-V << 1) | 1); 558 Code = bitc::CST_CODE_INTEGER; 559 AbbrevToUse = CONSTANTS_INTEGER_ABBREV; 560 } else { // Wide integers, > 64 bits in size. 561 // We have an arbitrary precision integer value to write whose 562 // bit width is > 64. However, in canonical unsigned integer 563 // format it is likely that the high bits are going to be zero. 564 // So, we only write the number of active words. 565 unsigned NWords = IV->getValue().getActiveWords(); 566 const uint64_t *RawWords = IV->getValue().getRawData(); 567 for (unsigned i = 0; i != NWords; ++i) { 568 int64_t V = RawWords[i]; 569 if (V >= 0) 570 Record.push_back(V << 1); 571 else 572 Record.push_back((-V << 1) | 1); 573 } 574 Code = bitc::CST_CODE_WIDE_INTEGER; 575 } 576 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) { 577 Code = bitc::CST_CODE_FLOAT; 578 const Type *Ty = CFP->getType(); 579 if (Ty == Type::FloatTy || Ty == Type::DoubleTy) { 580 Record.push_back(CFP->getValueAPF().bitcastToAPInt().getZExtValue()); 581 } else if (Ty == Type::X86_FP80Ty) { 582 // api needed to prevent premature destruction 583 // bits are not in the same order as a normal i80 APInt, compensate. 584 APInt api = CFP->getValueAPF().bitcastToAPInt(); 585 const uint64_t *p = api.getRawData(); 586 Record.push_back((p[1] << 48) | (p[0] >> 16)); 587 Record.push_back(p[0] & 0xffffLL); 588 } else if (Ty == Type::FP128Ty || Ty == Type::PPC_FP128Ty) { 589 APInt api = CFP->getValueAPF().bitcastToAPInt(); 590 const uint64_t *p = api.getRawData(); 591 Record.push_back(p[0]); 592 Record.push_back(p[1]); 593 } else { 594 assert (0 && "Unknown FP type!"); 595 } 596 } else if (isa<ConstantArray>(C) && cast<ConstantArray>(C)->isString()) { 597 // Emit constant strings specially. 598 unsigned NumOps = C->getNumOperands(); 599 // If this is a null-terminated string, use the denser CSTRING encoding. 600 if (C->getOperand(NumOps-1)->isNullValue()) { 601 Code = bitc::CST_CODE_CSTRING; 602 --NumOps; // Don't encode the null, which isn't allowed by char6. 603 } else { 604 Code = bitc::CST_CODE_STRING; 605 AbbrevToUse = String8Abbrev; 606 } 607 bool isCStr7 = Code == bitc::CST_CODE_CSTRING; 608 bool isCStrChar6 = Code == bitc::CST_CODE_CSTRING; 609 for (unsigned i = 0; i != NumOps; ++i) { 610 unsigned char V = cast<ConstantInt>(C->getOperand(i))->getZExtValue(); 611 Record.push_back(V); 612 isCStr7 &= (V & 128) == 0; 613 if (isCStrChar6) 614 isCStrChar6 = BitCodeAbbrevOp::isChar6(V); 615 } 616 617 if (isCStrChar6) 618 AbbrevToUse = CString6Abbrev; 619 else if (isCStr7) 620 AbbrevToUse = CString7Abbrev; 621 } else if (isa<ConstantArray>(C) || isa<ConstantStruct>(V) || 622 isa<ConstantVector>(V)) { 623 Code = bitc::CST_CODE_AGGREGATE; 624 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) 625 Record.push_back(VE.getValueID(C->getOperand(i))); 626 AbbrevToUse = AggregateAbbrev; 627 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { 628 switch (CE->getOpcode()) { 629 default: 630 if (Instruction::isCast(CE->getOpcode())) { 631 Code = bitc::CST_CODE_CE_CAST; 632 Record.push_back(GetEncodedCastOpcode(CE->getOpcode())); 633 Record.push_back(VE.getTypeID(C->getOperand(0)->getType())); 634 Record.push_back(VE.getValueID(C->getOperand(0))); 635 AbbrevToUse = CONSTANTS_CE_CAST_Abbrev; 636 } else { 637 assert(CE->getNumOperands() == 2 && "Unknown constant expr!"); 638 Code = bitc::CST_CODE_CE_BINOP; 639 Record.push_back(GetEncodedBinaryOpcode(CE->getOpcode())); 640 Record.push_back(VE.getValueID(C->getOperand(0))); 641 Record.push_back(VE.getValueID(C->getOperand(1))); 642 } 643 break; 644 case Instruction::GetElementPtr: 645 Code = bitc::CST_CODE_CE_GEP; 646 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) { 647 Record.push_back(VE.getTypeID(C->getOperand(i)->getType())); 648 Record.push_back(VE.getValueID(C->getOperand(i))); 649 } 650 break; 651 case Instruction::Select: 652 Code = bitc::CST_CODE_CE_SELECT; 653 Record.push_back(VE.getValueID(C->getOperand(0))); 654 Record.push_back(VE.getValueID(C->getOperand(1))); 655 Record.push_back(VE.getValueID(C->getOperand(2))); 656 break; 657 case Instruction::ExtractElement: 658 Code = bitc::CST_CODE_CE_EXTRACTELT; 659 Record.push_back(VE.getTypeID(C->getOperand(0)->getType())); 660 Record.push_back(VE.getValueID(C->getOperand(0))); 661 Record.push_back(VE.getValueID(C->getOperand(1))); 662 break; 663 case Instruction::InsertElement: 664 Code = bitc::CST_CODE_CE_INSERTELT; 665 Record.push_back(VE.getValueID(C->getOperand(0))); 666 Record.push_back(VE.getValueID(C->getOperand(1))); 667 Record.push_back(VE.getValueID(C->getOperand(2))); 668 break; 669 case Instruction::ShuffleVector: 670 // If the return type and argument types are the same, this is a 671 // standard shufflevector instruction. If the types are different, 672 // then the shuffle is widening or truncating the input vectors, and 673 // the argument type must also be encoded. 674 if (C->getType() == C->getOperand(0)->getType()) { 675 Code = bitc::CST_CODE_CE_SHUFFLEVEC; 676 } else { 677 Code = bitc::CST_CODE_CE_SHUFVEC_EX; 678 Record.push_back(VE.getTypeID(C->getOperand(0)->getType())); 679 } 680 Record.push_back(VE.getValueID(C->getOperand(0))); 681 Record.push_back(VE.getValueID(C->getOperand(1))); 682 Record.push_back(VE.getValueID(C->getOperand(2))); 683 break; 684 case Instruction::ICmp: 685 case Instruction::FCmp: 686 Code = bitc::CST_CODE_CE_CMP; 687 Record.push_back(VE.getTypeID(C->getOperand(0)->getType())); 688 Record.push_back(VE.getValueID(C->getOperand(0))); 689 Record.push_back(VE.getValueID(C->getOperand(1))); 690 Record.push_back(CE->getPredicate()); 691 break; 692 } 693 } else if (const MDString *S = dyn_cast<MDString>(C)) { 694 Code = bitc::CST_CODE_MDSTRING; 695 AbbrevToUse = MDString6Abbrev; 696 for (unsigned i = 0, e = S->size(); i != e; ++i) { 697 char V = S->begin()[i]; 698 Record.push_back(V); 699 700 if (!BitCodeAbbrevOp::isChar6(V)) 701 AbbrevToUse = MDString8Abbrev; 702 } 703 } else if (const MDNode *N = dyn_cast<MDNode>(C)) { 704 Code = bitc::CST_CODE_MDNODE; 705 for (unsigned i = 0, e = N->getNumElements(); i != e; ++i) { 706 if (N->getElement(i)) { 707 Record.push_back(VE.getTypeID(N->getElement(i)->getType())); 708 Record.push_back(VE.getValueID(N->getElement(i))); 709 } else { 710 Record.push_back(VE.getTypeID(Type::VoidTy)); 711 Record.push_back(0); 712 } 713 } 714 } else { 715 assert(0 && "Unknown constant!"); 716 } 717 Stream.EmitRecord(Code, Record, AbbrevToUse); 718 Record.clear(); 719 } 720 721 Stream.ExitBlock(); 722 } 723 724 static void WriteModuleConstants(const ValueEnumerator &VE, 725 BitstreamWriter &Stream) { 726 const ValueEnumerator::ValueList &Vals = VE.getValues(); 727 728 // Find the first constant to emit, which is the first non-globalvalue value. 729 // We know globalvalues have been emitted by WriteModuleInfo. 730 for (unsigned i = 0, e = Vals.size(); i != e; ++i) { 731 if (!isa<GlobalValue>(Vals[i].first)) { 732 WriteConstants(i, Vals.size(), VE, Stream, true); 733 return; 734 } 735 } 736 } 737 738 /// PushValueAndType - The file has to encode both the value and type id for 739 /// many values, because we need to know what type to create for forward 740 /// references. However, most operands are not forward references, so this type 741 /// field is not needed. 742 /// 743 /// This function adds V's value ID to Vals. If the value ID is higher than the 744 /// instruction ID, then it is a forward reference, and it also includes the 745 /// type ID. 746 static bool PushValueAndType(const Value *V, unsigned InstID, 747 SmallVector<unsigned, 64> &Vals, 748 ValueEnumerator &VE) { 749 unsigned ValID = VE.getValueID(V); 750 Vals.push_back(ValID); 751 if (ValID >= InstID) { 752 Vals.push_back(VE.getTypeID(V->getType())); 753 return true; 754 } 755 return false; 756 } 757 758 /// WriteInstruction - Emit an instruction to the specified stream. 759 static void WriteInstruction(const Instruction &I, unsigned InstID, 760 ValueEnumerator &VE, BitstreamWriter &Stream, 761 SmallVector<unsigned, 64> &Vals) { 762 unsigned Code = 0; 763 unsigned AbbrevToUse = 0; 764 switch (I.getOpcode()) { 765 default: 766 if (Instruction::isCast(I.getOpcode())) { 767 Code = bitc::FUNC_CODE_INST_CAST; 768 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE)) 769 AbbrevToUse = FUNCTION_INST_CAST_ABBREV; 770 Vals.push_back(VE.getTypeID(I.getType())); 771 Vals.push_back(GetEncodedCastOpcode(I.getOpcode())); 772 } else { 773 assert(isa<BinaryOperator>(I) && "Unknown instruction!"); 774 Code = bitc::FUNC_CODE_INST_BINOP; 775 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE)) 776 AbbrevToUse = FUNCTION_INST_BINOP_ABBREV; 777 Vals.push_back(VE.getValueID(I.getOperand(1))); 778 Vals.push_back(GetEncodedBinaryOpcode(I.getOpcode())); 779 } 780 break; 781 782 case Instruction::GetElementPtr: 783 Code = bitc::FUNC_CODE_INST_GEP; 784 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) 785 PushValueAndType(I.getOperand(i), InstID, Vals, VE); 786 break; 787 case Instruction::ExtractValue: { 788 Code = bitc::FUNC_CODE_INST_EXTRACTVAL; 789 PushValueAndType(I.getOperand(0), InstID, Vals, VE); 790 const ExtractValueInst *EVI = cast<ExtractValueInst>(&I); 791 for (const unsigned *i = EVI->idx_begin(), *e = EVI->idx_end(); i != e; ++i) 792 Vals.push_back(*i); 793 break; 794 } 795 case Instruction::InsertValue: { 796 Code = bitc::FUNC_CODE_INST_INSERTVAL; 797 PushValueAndType(I.getOperand(0), InstID, Vals, VE); 798 PushValueAndType(I.getOperand(1), InstID, Vals, VE); 799 const InsertValueInst *IVI = cast<InsertValueInst>(&I); 800 for (const unsigned *i = IVI->idx_begin(), *e = IVI->idx_end(); i != e; ++i) 801 Vals.push_back(*i); 802 break; 803 } 804 case Instruction::Select: 805 Code = bitc::FUNC_CODE_INST_VSELECT; 806 PushValueAndType(I.getOperand(1), InstID, Vals, VE); 807 Vals.push_back(VE.getValueID(I.getOperand(2))); 808 PushValueAndType(I.getOperand(0), InstID, Vals, VE); 809 break; 810 case Instruction::ExtractElement: 811 Code = bitc::FUNC_CODE_INST_EXTRACTELT; 812 PushValueAndType(I.getOperand(0), InstID, Vals, VE); 813 Vals.push_back(VE.getValueID(I.getOperand(1))); 814 break; 815 case Instruction::InsertElement: 816 Code = bitc::FUNC_CODE_INST_INSERTELT; 817 PushValueAndType(I.getOperand(0), InstID, Vals, VE); 818 Vals.push_back(VE.getValueID(I.getOperand(1))); 819 Vals.push_back(VE.getValueID(I.getOperand(2))); 820 break; 821 case Instruction::ShuffleVector: 822 Code = bitc::FUNC_CODE_INST_SHUFFLEVEC; 823 PushValueAndType(I.getOperand(0), InstID, Vals, VE); 824 Vals.push_back(VE.getValueID(I.getOperand(1))); 825 Vals.push_back(VE.getValueID(I.getOperand(2))); 826 break; 827 case Instruction::ICmp: 828 case Instruction::FCmp: 829 // compare returning Int1Ty or vector of Int1Ty 830 Code = bitc::FUNC_CODE_INST_CMP2; 831 PushValueAndType(I.getOperand(0), InstID, Vals, VE); 832 Vals.push_back(VE.getValueID(I.getOperand(1))); 833 Vals.push_back(cast<CmpInst>(I).getPredicate()); 834 break; 835 836 case Instruction::Ret: 837 { 838 Code = bitc::FUNC_CODE_INST_RET; 839 unsigned NumOperands = I.getNumOperands(); 840 if (NumOperands == 0) 841 AbbrevToUse = FUNCTION_INST_RET_VOID_ABBREV; 842 else if (NumOperands == 1) { 843 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE)) 844 AbbrevToUse = FUNCTION_INST_RET_VAL_ABBREV; 845 } else { 846 for (unsigned i = 0, e = NumOperands; i != e; ++i) 847 PushValueAndType(I.getOperand(i), InstID, Vals, VE); 848 } 849 } 850 break; 851 case Instruction::Br: 852 { 853 Code = bitc::FUNC_CODE_INST_BR; 854 BranchInst &II(cast<BranchInst>(I)); 855 Vals.push_back(VE.getValueID(II.getSuccessor(0))); 856 if (II.isConditional()) { 857 Vals.push_back(VE.getValueID(II.getSuccessor(1))); 858 Vals.push_back(VE.getValueID(II.getCondition())); 859 } 860 } 861 break; 862 case Instruction::Switch: 863 Code = bitc::FUNC_CODE_INST_SWITCH; 864 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); 865 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) 866 Vals.push_back(VE.getValueID(I.getOperand(i))); 867 break; 868 case Instruction::Invoke: { 869 const InvokeInst *II = cast<InvokeInst>(&I); 870 const Value *Callee(II->getCalledValue()); 871 const PointerType *PTy = cast<PointerType>(Callee->getType()); 872 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); 873 Code = bitc::FUNC_CODE_INST_INVOKE; 874 875 Vals.push_back(VE.getAttributeID(II->getAttributes())); 876 Vals.push_back(II->getCallingConv()); 877 Vals.push_back(VE.getValueID(II->getNormalDest())); 878 Vals.push_back(VE.getValueID(II->getUnwindDest())); 879 PushValueAndType(Callee, InstID, Vals, VE); 880 881 // Emit value #'s for the fixed parameters. 882 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) 883 Vals.push_back(VE.getValueID(I.getOperand(i+3))); // fixed param. 884 885 // Emit type/value pairs for varargs params. 886 if (FTy->isVarArg()) { 887 for (unsigned i = 3+FTy->getNumParams(), e = I.getNumOperands(); 888 i != e; ++i) 889 PushValueAndType(I.getOperand(i), InstID, Vals, VE); // vararg 890 } 891 break; 892 } 893 case Instruction::Unwind: 894 Code = bitc::FUNC_CODE_INST_UNWIND; 895 break; 896 case Instruction::Unreachable: 897 Code = bitc::FUNC_CODE_INST_UNREACHABLE; 898 AbbrevToUse = FUNCTION_INST_UNREACHABLE_ABBREV; 899 break; 900 901 case Instruction::PHI: 902 Code = bitc::FUNC_CODE_INST_PHI; 903 Vals.push_back(VE.getTypeID(I.getType())); 904 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) 905 Vals.push_back(VE.getValueID(I.getOperand(i))); 906 break; 907 908 case Instruction::Malloc: 909 Code = bitc::FUNC_CODE_INST_MALLOC; 910 Vals.push_back(VE.getTypeID(I.getType())); 911 Vals.push_back(VE.getValueID(I.getOperand(0))); // size. 912 Vals.push_back(Log2_32(cast<MallocInst>(I).getAlignment())+1); 913 break; 914 915 case Instruction::Free: 916 Code = bitc::FUNC_CODE_INST_FREE; 917 PushValueAndType(I.getOperand(0), InstID, Vals, VE); 918 break; 919 920 case Instruction::Alloca: 921 Code = bitc::FUNC_CODE_INST_ALLOCA; 922 Vals.push_back(VE.getTypeID(I.getType())); 923 Vals.push_back(VE.getValueID(I.getOperand(0))); // size. 924 Vals.push_back(Log2_32(cast<AllocaInst>(I).getAlignment())+1); 925 break; 926 927 case Instruction::Load: 928 Code = bitc::FUNC_CODE_INST_LOAD; 929 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE)) // ptr 930 AbbrevToUse = FUNCTION_INST_LOAD_ABBREV; 931 932 Vals.push_back(Log2_32(cast<LoadInst>(I).getAlignment())+1); 933 Vals.push_back(cast<LoadInst>(I).isVolatile()); 934 break; 935 case Instruction::Store: 936 Code = bitc::FUNC_CODE_INST_STORE2; 937 PushValueAndType(I.getOperand(1), InstID, Vals, VE); // ptrty + ptr 938 Vals.push_back(VE.getValueID(I.getOperand(0))); // val. 939 Vals.push_back(Log2_32(cast<StoreInst>(I).getAlignment())+1); 940 Vals.push_back(cast<StoreInst>(I).isVolatile()); 941 break; 942 case Instruction::Call: { 943 const PointerType *PTy = cast<PointerType>(I.getOperand(0)->getType()); 944 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); 945 946 Code = bitc::FUNC_CODE_INST_CALL; 947 948 const CallInst *CI = cast<CallInst>(&I); 949 Vals.push_back(VE.getAttributeID(CI->getAttributes())); 950 Vals.push_back((CI->getCallingConv() << 1) | unsigned(CI->isTailCall())); 951 PushValueAndType(CI->getOperand(0), InstID, Vals, VE); // Callee 952 953 // Emit value #'s for the fixed parameters. 954 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) 955 Vals.push_back(VE.getValueID(I.getOperand(i+1))); // fixed param. 956 957 // Emit type/value pairs for varargs params. 958 if (FTy->isVarArg()) { 959 unsigned NumVarargs = I.getNumOperands()-1-FTy->getNumParams(); 960 for (unsigned i = I.getNumOperands()-NumVarargs, e = I.getNumOperands(); 961 i != e; ++i) 962 PushValueAndType(I.getOperand(i), InstID, Vals, VE); // varargs 963 } 964 break; 965 } 966 case Instruction::VAArg: 967 Code = bitc::FUNC_CODE_INST_VAARG; 968 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); // valistty 969 Vals.push_back(VE.getValueID(I.getOperand(0))); // valist. 970 Vals.push_back(VE.getTypeID(I.getType())); // restype. 971 break; 972 } 973 974 Stream.EmitRecord(Code, Vals, AbbrevToUse); 975 Vals.clear(); 976 } 977 978 // Emit names for globals/functions etc. 979 static void WriteValueSymbolTable(const ValueSymbolTable &VST, 980 const ValueEnumerator &VE, 981 BitstreamWriter &Stream) { 982 if (VST.empty()) return; 983 Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4); 984 985 // FIXME: Set up the abbrev, we know how many values there are! 986 // FIXME: We know if the type names can use 7-bit ascii. 987 SmallVector<unsigned, 64> NameVals; 988 989 for (ValueSymbolTable::const_iterator SI = VST.begin(), SE = VST.end(); 990 SI != SE; ++SI) { 991 992 const ValueName &Name = *SI; 993 994 // Figure out the encoding to use for the name. 995 bool is7Bit = true; 996 bool isChar6 = true; 997 for (const char *C = Name.getKeyData(), *E = C+Name.getKeyLength(); 998 C != E; ++C) { 999 if (isChar6) 1000 isChar6 = BitCodeAbbrevOp::isChar6(*C); 1001 if ((unsigned char)*C & 128) { 1002 is7Bit = false; 1003 break; // don't bother scanning the rest. 1004 } 1005 } 1006 1007 unsigned AbbrevToUse = VST_ENTRY_8_ABBREV; 1008 1009 // VST_ENTRY: [valueid, namechar x N] 1010 // VST_BBENTRY: [bbid, namechar x N] 1011 unsigned Code; 1012 if (isa<BasicBlock>(SI->getValue())) { 1013 Code = bitc::VST_CODE_BBENTRY; 1014 if (isChar6) 1015 AbbrevToUse = VST_BBENTRY_6_ABBREV; 1016 } else { 1017 Code = bitc::VST_CODE_ENTRY; 1018 if (isChar6) 1019 AbbrevToUse = VST_ENTRY_6_ABBREV; 1020 else if (is7Bit) 1021 AbbrevToUse = VST_ENTRY_7_ABBREV; 1022 } 1023 1024 NameVals.push_back(VE.getValueID(SI->getValue())); 1025 for (const char *P = Name.getKeyData(), 1026 *E = Name.getKeyData()+Name.getKeyLength(); P != E; ++P) 1027 NameVals.push_back((unsigned char)*P); 1028 1029 // Emit the finished record. 1030 Stream.EmitRecord(Code, NameVals, AbbrevToUse); 1031 NameVals.clear(); 1032 } 1033 Stream.ExitBlock(); 1034 } 1035 1036 /// WriteFunction - Emit a function body to the module stream. 1037 static void WriteFunction(const Function &F, ValueEnumerator &VE, 1038 BitstreamWriter &Stream) { 1039 Stream.EnterSubblock(bitc::FUNCTION_BLOCK_ID, 4); 1040 VE.incorporateFunction(F); 1041 1042 SmallVector<unsigned, 64> Vals; 1043 1044 // Emit the number of basic blocks, so the reader can create them ahead of 1045 // time. 1046 Vals.push_back(VE.getBasicBlocks().size()); 1047 Stream.EmitRecord(bitc::FUNC_CODE_DECLAREBLOCKS, Vals); 1048 Vals.clear(); 1049 1050 // If there are function-local constants, emit them now. 1051 unsigned CstStart, CstEnd; 1052 VE.getFunctionConstantRange(CstStart, CstEnd); 1053 WriteConstants(CstStart, CstEnd, VE, Stream, false); 1054 1055 // Keep a running idea of what the instruction ID is. 1056 unsigned InstID = CstEnd; 1057 1058 // Finally, emit all the instructions, in order. 1059 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) 1060 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); 1061 I != E; ++I) { 1062 WriteInstruction(*I, InstID, VE, Stream, Vals); 1063 if (I->getType() != Type::VoidTy) 1064 ++InstID; 1065 } 1066 1067 // Emit names for all the instructions etc. 1068 WriteValueSymbolTable(F.getValueSymbolTable(), VE, Stream); 1069 1070 VE.purgeFunction(); 1071 Stream.ExitBlock(); 1072 } 1073 1074 /// WriteTypeSymbolTable - Emit a block for the specified type symtab. 1075 static void WriteTypeSymbolTable(const TypeSymbolTable &TST, 1076 const ValueEnumerator &VE, 1077 BitstreamWriter &Stream) { 1078 if (TST.empty()) return; 1079 1080 Stream.EnterSubblock(bitc::TYPE_SYMTAB_BLOCK_ID, 3); 1081 1082 // 7-bit fixed width VST_CODE_ENTRY strings. 1083 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1084 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY)); 1085 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1086 Log2_32_Ceil(VE.getTypes().size()+1))); 1087 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 1088 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); 1089 unsigned V7Abbrev = Stream.EmitAbbrev(Abbv); 1090 1091 SmallVector<unsigned, 64> NameVals; 1092 1093 for (TypeSymbolTable::const_iterator TI = TST.begin(), TE = TST.end(); 1094 TI != TE; ++TI) { 1095 // TST_ENTRY: [typeid, namechar x N] 1096 NameVals.push_back(VE.getTypeID(TI->second)); 1097 1098 const std::string &Str = TI->first; 1099 bool is7Bit = true; 1100 for (unsigned i = 0, e = Str.size(); i != e; ++i) { 1101 NameVals.push_back((unsigned char)Str[i]); 1102 if (Str[i] & 128) 1103 is7Bit = false; 1104 } 1105 1106 // Emit the finished record. 1107 Stream.EmitRecord(bitc::VST_CODE_ENTRY, NameVals, is7Bit ? V7Abbrev : 0); 1108 NameVals.clear(); 1109 } 1110 1111 Stream.ExitBlock(); 1112 } 1113 1114 // Emit blockinfo, which defines the standard abbreviations etc. 1115 static void WriteBlockInfo(const ValueEnumerator &VE, BitstreamWriter &Stream) { 1116 // We only want to emit block info records for blocks that have multiple 1117 // instances: CONSTANTS_BLOCK, FUNCTION_BLOCK and VALUE_SYMTAB_BLOCK. Other 1118 // blocks can defined their abbrevs inline. 1119 Stream.EnterBlockInfoBlock(2); 1120 1121 { // 8-bit fixed-width VST_ENTRY/VST_BBENTRY strings. 1122 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1123 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); 1124 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 1125 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 1126 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); 1127 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, 1128 Abbv) != VST_ENTRY_8_ABBREV) 1129 assert(0 && "Unexpected abbrev ordering!"); 1130 } 1131 1132 { // 7-bit fixed width VST_ENTRY strings. 1133 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1134 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY)); 1135 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 1136 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 1137 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); 1138 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, 1139 Abbv) != VST_ENTRY_7_ABBREV) 1140 assert(0 && "Unexpected abbrev ordering!"); 1141 } 1142 { // 6-bit char6 VST_ENTRY strings. 1143 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1144 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY)); 1145 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 1146 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 1147 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6)); 1148 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, 1149 Abbv) != VST_ENTRY_6_ABBREV) 1150 assert(0 && "Unexpected abbrev ordering!"); 1151 } 1152 { // 6-bit char6 VST_BBENTRY strings. 1153 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1154 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_BBENTRY)); 1155 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 1156 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 1157 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6)); 1158 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, 1159 Abbv) != VST_BBENTRY_6_ABBREV) 1160 assert(0 && "Unexpected abbrev ordering!"); 1161 } 1162 1163 1164 1165 { // SETTYPE abbrev for CONSTANTS_BLOCK. 1166 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1167 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_SETTYPE)); 1168 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1169 Log2_32_Ceil(VE.getTypes().size()+1))); 1170 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, 1171 Abbv) != CONSTANTS_SETTYPE_ABBREV) 1172 assert(0 && "Unexpected abbrev ordering!"); 1173 } 1174 1175 { // INTEGER abbrev for CONSTANTS_BLOCK. 1176 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1177 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_INTEGER)); 1178 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 1179 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, 1180 Abbv) != CONSTANTS_INTEGER_ABBREV) 1181 assert(0 && "Unexpected abbrev ordering!"); 1182 } 1183 1184 { // CE_CAST abbrev for CONSTANTS_BLOCK. 1185 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1186 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CE_CAST)); 1187 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // cast opc 1188 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // typeid 1189 Log2_32_Ceil(VE.getTypes().size()+1))); 1190 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id 1191 1192 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, 1193 Abbv) != CONSTANTS_CE_CAST_Abbrev) 1194 assert(0 && "Unexpected abbrev ordering!"); 1195 } 1196 { // NULL abbrev for CONSTANTS_BLOCK. 1197 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1198 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_NULL)); 1199 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, 1200 Abbv) != CONSTANTS_NULL_Abbrev) 1201 assert(0 && "Unexpected abbrev ordering!"); 1202 } 1203 1204 // FIXME: This should only use space for first class types! 1205 1206 { // INST_LOAD abbrev for FUNCTION_BLOCK. 1207 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1208 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_LOAD)); 1209 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Ptr 1210 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // Align 1211 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // volatile 1212 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, 1213 Abbv) != FUNCTION_INST_LOAD_ABBREV) 1214 assert(0 && "Unexpected abbrev ordering!"); 1215 } 1216 { // INST_BINOP abbrev for FUNCTION_BLOCK. 1217 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1218 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP)); 1219 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS 1220 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS 1221 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc 1222 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, 1223 Abbv) != FUNCTION_INST_BINOP_ABBREV) 1224 assert(0 && "Unexpected abbrev ordering!"); 1225 } 1226 { // INST_CAST abbrev for FUNCTION_BLOCK. 1227 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1228 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_CAST)); 1229 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // OpVal 1230 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty 1231 Log2_32_Ceil(VE.getTypes().size()+1))); 1232 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc 1233 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, 1234 Abbv) != FUNCTION_INST_CAST_ABBREV) 1235 assert(0 && "Unexpected abbrev ordering!"); 1236 } 1237 1238 { // INST_RET abbrev for FUNCTION_BLOCK. 1239 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1240 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET)); 1241 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, 1242 Abbv) != FUNCTION_INST_RET_VOID_ABBREV) 1243 assert(0 && "Unexpected abbrev ordering!"); 1244 } 1245 { // INST_RET abbrev for FUNCTION_BLOCK. 1246 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1247 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET)); 1248 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ValID 1249 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, 1250 Abbv) != FUNCTION_INST_RET_VAL_ABBREV) 1251 assert(0 && "Unexpected abbrev ordering!"); 1252 } 1253 { // INST_UNREACHABLE abbrev for FUNCTION_BLOCK. 1254 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1255 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_UNREACHABLE)); 1256 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, 1257 Abbv) != FUNCTION_INST_UNREACHABLE_ABBREV) 1258 assert(0 && "Unexpected abbrev ordering!"); 1259 } 1260 1261 Stream.ExitBlock(); 1262 } 1263 1264 1265 /// WriteModule - Emit the specified module to the bitstream. 1266 static void WriteModule(const Module *M, BitstreamWriter &Stream) { 1267 Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3); 1268 1269 // Emit the version number if it is non-zero. 1270 if (CurVersion) { 1271 SmallVector<unsigned, 1> Vals; 1272 Vals.push_back(CurVersion); 1273 Stream.EmitRecord(bitc::MODULE_CODE_VERSION, Vals); 1274 } 1275 1276 // Analyze the module, enumerating globals, functions, etc. 1277 ValueEnumerator VE(M); 1278 1279 // Emit blockinfo, which defines the standard abbreviations etc. 1280 WriteBlockInfo(VE, Stream); 1281 1282 // Emit information about parameter attributes. 1283 WriteAttributeTable(VE, Stream); 1284 1285 // Emit information describing all of the types in the module. 1286 WriteTypeTable(VE, Stream); 1287 1288 // Emit top-level description of module, including target triple, inline asm, 1289 // descriptors for global variables, and function prototype info. 1290 WriteModuleInfo(M, VE, Stream); 1291 1292 // Emit constants. 1293 WriteModuleConstants(VE, Stream); 1294 1295 // Emit function bodies. 1296 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) 1297 if (!I->isDeclaration()) 1298 WriteFunction(*I, VE, Stream); 1299 1300 // Emit the type symbol table information. 1301 WriteTypeSymbolTable(M->getTypeSymbolTable(), VE, Stream); 1302 1303 // Emit names for globals/functions etc. 1304 WriteValueSymbolTable(M->getValueSymbolTable(), VE, Stream); 1305 1306 Stream.ExitBlock(); 1307 } 1308 1309 /// EmitDarwinBCHeader - If generating a bc file on darwin, we have to emit a 1310 /// header and trailer to make it compatible with the system archiver. To do 1311 /// this we emit the following header, and then emit a trailer that pads the 1312 /// file out to be a multiple of 16 bytes. 1313 /// 1314 /// struct bc_header { 1315 /// uint32_t Magic; // 0x0B17C0DE 1316 /// uint32_t Version; // Version, currently always 0. 1317 /// uint32_t BitcodeOffset; // Offset to traditional bitcode file. 1318 /// uint32_t BitcodeSize; // Size of traditional bitcode file. 1319 /// uint32_t CPUType; // CPU specifier. 1320 /// ... potentially more later ... 1321 /// }; 1322 enum { 1323 DarwinBCSizeFieldOffset = 3*4, // Offset to bitcode_size. 1324 DarwinBCHeaderSize = 5*4 1325 }; 1326 1327 static void EmitDarwinBCHeader(BitstreamWriter &Stream, 1328 const std::string &TT) { 1329 unsigned CPUType = ~0U; 1330 1331 // Match x86_64-*, i[3-9]86-*, powerpc-*, powerpc64-*. The CPUType is a 1332 // magic number from /usr/include/mach/machine.h. It is ok to reproduce the 1333 // specific constants here because they are implicitly part of the Darwin ABI. 1334 enum { 1335 DARWIN_CPU_ARCH_ABI64 = 0x01000000, 1336 DARWIN_CPU_TYPE_X86 = 7, 1337 DARWIN_CPU_TYPE_POWERPC = 18 1338 }; 1339 1340 if (TT.find("x86_64-") == 0) 1341 CPUType = DARWIN_CPU_TYPE_X86 | DARWIN_CPU_ARCH_ABI64; 1342 else if (TT.size() >= 5 && TT[0] == 'i' && TT[2] == '8' && TT[3] == '6' && 1343 TT[4] == '-' && TT[1] - '3' < 6) 1344 CPUType = DARWIN_CPU_TYPE_X86; 1345 else if (TT.find("powerpc-") == 0) 1346 CPUType = DARWIN_CPU_TYPE_POWERPC; 1347 else if (TT.find("powerpc64-") == 0) 1348 CPUType = DARWIN_CPU_TYPE_POWERPC | DARWIN_CPU_ARCH_ABI64; 1349 1350 // Traditional Bitcode starts after header. 1351 unsigned BCOffset = DarwinBCHeaderSize; 1352 1353 Stream.Emit(0x0B17C0DE, 32); 1354 Stream.Emit(0 , 32); // Version. 1355 Stream.Emit(BCOffset , 32); 1356 Stream.Emit(0 , 32); // Filled in later. 1357 Stream.Emit(CPUType , 32); 1358 } 1359 1360 /// EmitDarwinBCTrailer - Emit the darwin epilog after the bitcode file and 1361 /// finalize the header. 1362 static void EmitDarwinBCTrailer(BitstreamWriter &Stream, unsigned BufferSize) { 1363 // Update the size field in the header. 1364 Stream.BackpatchWord(DarwinBCSizeFieldOffset, BufferSize-DarwinBCHeaderSize); 1365 1366 // If the file is not a multiple of 16 bytes, insert dummy padding. 1367 while (BufferSize & 15) { 1368 Stream.Emit(0, 8); 1369 ++BufferSize; 1370 } 1371 } 1372 1373 1374 /// WriteBitcodeToFile - Write the specified module to the specified output 1375 /// stream. 1376 void llvm::WriteBitcodeToFile(const Module *M, std::ostream &Out) { 1377 raw_os_ostream RawOut(Out); 1378 // If writing to stdout, set binary mode. 1379 if (llvm::cout == Out) 1380 sys::Program::ChangeStdoutToBinary(); 1381 WriteBitcodeToFile(M, RawOut); 1382 } 1383 1384 /// WriteBitcodeToFile - Write the specified module to the specified output 1385 /// stream. 1386 void llvm::WriteBitcodeToFile(const Module *M, raw_ostream &Out) { 1387 std::vector<unsigned char> Buffer; 1388 BitstreamWriter Stream(Buffer); 1389 1390 Buffer.reserve(256*1024); 1391 1392 WriteBitcodeToStream( M, Stream ); 1393 1394 // If writing to stdout, set binary mode. 1395 if (&llvm::outs() == &Out) 1396 sys::Program::ChangeStdoutToBinary(); 1397 1398 // Write the generated bitstream to "Out". 1399 Out.write((char*)&Buffer.front(), Buffer.size()); 1400 1401 // Make sure it hits disk now. 1402 Out.flush(); 1403 } 1404 1405 /// WriteBitcodeToStream - Write the specified module to the specified output 1406 /// stream. 1407 void llvm::WriteBitcodeToStream(const Module *M, BitstreamWriter &Stream) { 1408 // If this is darwin, emit a file header and trailer if needed. 1409 bool isDarwin = M->getTargetTriple().find("-darwin") != std::string::npos; 1410 if (isDarwin) 1411 EmitDarwinBCHeader(Stream, M->getTargetTriple()); 1412 1413 // Emit the file header. 1414 Stream.Emit((unsigned)'B', 8); 1415 Stream.Emit((unsigned)'C', 8); 1416 Stream.Emit(0x0, 4); 1417 Stream.Emit(0xC, 4); 1418 Stream.Emit(0xE, 4); 1419 Stream.Emit(0xD, 4); 1420 1421 // Emit the module. 1422 WriteModule(M, Stream); 1423 1424 if (isDarwin) 1425 EmitDarwinBCTrailer(Stream, Stream.getBuffer().size()); 1426 } 1427