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