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