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