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