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