BitcodeWriter.cpp revision 7337ab9e929a4a787f90d37ee0ed8f4cc27170ab
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" 26using 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. 30enum { 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 50static 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 68static 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 91static 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. 104static 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. 127static 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 250static 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 264static 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. 275static 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 406static 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 561static 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. 583static 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. 596static 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 Vals.push_back(GetEncodedCastOpcode(I.getOpcode())); 606 Vals.push_back(VE.getTypeID(I.getType())); 607 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); 608 Vals.push_back(VE.getValueID(I.getOperand(0))); 609 } else { 610 assert(isa<BinaryOperator>(I) && "Unknown instruction!"); 611 Code = bitc::FUNC_CODE_INST_BINOP; 612 Vals.push_back(GetEncodedBinaryOpcode(I.getOpcode())); 613 Vals.push_back(VE.getTypeID(I.getType())); 614 Vals.push_back(VE.getValueID(I.getOperand(0))); 615 Vals.push_back(VE.getValueID(I.getOperand(1))); 616 } 617 break; 618 619 case Instruction::GetElementPtr: 620 Code = bitc::FUNC_CODE_INST_GEP; 621 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) 622 PushValueAndType(I.getOperand(i), InstID, Vals, VE); 623 break; 624 case Instruction::Select: 625 Code = bitc::FUNC_CODE_INST_SELECT; 626 Vals.push_back(VE.getTypeID(I.getType())); 627 Vals.push_back(VE.getValueID(I.getOperand(0))); 628 Vals.push_back(VE.getValueID(I.getOperand(1))); 629 Vals.push_back(VE.getValueID(I.getOperand(2))); 630 break; 631 case Instruction::ExtractElement: 632 Code = bitc::FUNC_CODE_INST_EXTRACTELT; 633 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); 634 Vals.push_back(VE.getValueID(I.getOperand(0))); 635 Vals.push_back(VE.getValueID(I.getOperand(1))); 636 break; 637 case Instruction::InsertElement: 638 Code = bitc::FUNC_CODE_INST_INSERTELT; 639 Vals.push_back(VE.getTypeID(I.getType())); 640 Vals.push_back(VE.getValueID(I.getOperand(0))); 641 Vals.push_back(VE.getValueID(I.getOperand(1))); 642 Vals.push_back(VE.getValueID(I.getOperand(2))); 643 break; 644 case Instruction::ShuffleVector: 645 Code = bitc::FUNC_CODE_INST_SHUFFLEVEC; 646 Vals.push_back(VE.getTypeID(I.getType())); 647 Vals.push_back(VE.getValueID(I.getOperand(0))); 648 Vals.push_back(VE.getValueID(I.getOperand(1))); 649 Vals.push_back(VE.getValueID(I.getOperand(2))); 650 break; 651 case Instruction::ICmp: 652 case Instruction::FCmp: 653 Code = bitc::FUNC_CODE_INST_CMP; 654 PushValueAndType(I.getOperand(0), InstID, Vals, VE); 655 Vals.push_back(VE.getValueID(I.getOperand(1))); 656 Vals.push_back(cast<CmpInst>(I).getPredicate()); 657 break; 658 659 case Instruction::Ret: 660 Code = bitc::FUNC_CODE_INST_RET; 661 if (I.getNumOperands()) 662 PushValueAndType(I.getOperand(0), InstID, Vals, VE); 663 break; 664 case Instruction::Br: 665 Code = bitc::FUNC_CODE_INST_BR; 666 Vals.push_back(VE.getValueID(I.getOperand(0))); 667 if (cast<BranchInst>(I).isConditional()) { 668 Vals.push_back(VE.getValueID(I.getOperand(1))); 669 Vals.push_back(VE.getValueID(I.getOperand(2))); 670 } 671 break; 672 case Instruction::Switch: 673 Code = bitc::FUNC_CODE_INST_SWITCH; 674 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); 675 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) 676 Vals.push_back(VE.getValueID(I.getOperand(i))); 677 break; 678 case Instruction::Invoke: { 679 Code = bitc::FUNC_CODE_INST_INVOKE; 680 Vals.push_back(cast<InvokeInst>(I).getCallingConv()); 681 Vals.push_back(VE.getValueID(I.getOperand(1))); // normal dest 682 Vals.push_back(VE.getValueID(I.getOperand(2))); // unwind dest 683 PushValueAndType(I.getOperand(0), InstID, Vals, VE); // callee 684 685 // Emit value #'s for the fixed parameters. 686 const PointerType *PTy = cast<PointerType>(I.getOperand(0)->getType()); 687 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); 688 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) 689 Vals.push_back(VE.getValueID(I.getOperand(i+3))); // fixed param. 690 691 // Emit type/value pairs for varargs params. 692 if (FTy->isVarArg()) { 693 for (unsigned i = 3+FTy->getNumParams(), e = I.getNumOperands(); 694 i != e; ++i) 695 PushValueAndType(I.getOperand(i), InstID, Vals, VE); // vararg 696 } 697 break; 698 } 699 case Instruction::Unwind: 700 Code = bitc::FUNC_CODE_INST_UNWIND; 701 break; 702 case Instruction::Unreachable: 703 Code = bitc::FUNC_CODE_INST_UNREACHABLE; 704 break; 705 706 case Instruction::PHI: 707 Code = bitc::FUNC_CODE_INST_PHI; 708 Vals.push_back(VE.getTypeID(I.getType())); 709 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) 710 Vals.push_back(VE.getValueID(I.getOperand(i))); 711 break; 712 713 case Instruction::Malloc: 714 Code = bitc::FUNC_CODE_INST_MALLOC; 715 Vals.push_back(VE.getTypeID(I.getType())); 716 Vals.push_back(VE.getValueID(I.getOperand(0))); // size. 717 Vals.push_back(Log2_32(cast<MallocInst>(I).getAlignment())+1); 718 break; 719 720 case Instruction::Free: 721 Code = bitc::FUNC_CODE_INST_FREE; 722 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); 723 Vals.push_back(VE.getValueID(I.getOperand(0))); 724 break; 725 726 case Instruction::Alloca: 727 Code = bitc::FUNC_CODE_INST_ALLOCA; 728 Vals.push_back(VE.getTypeID(I.getType())); 729 Vals.push_back(VE.getValueID(I.getOperand(0))); // size. 730 Vals.push_back(Log2_32(cast<AllocaInst>(I).getAlignment())+1); 731 break; 732 733 case Instruction::Load: 734 Code = bitc::FUNC_CODE_INST_LOAD; 735 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE)) // ptr 736 AbbrevToUse = FUNCTION_INST_LOAD_ABBREV; 737 738 Vals.push_back(Log2_32(cast<LoadInst>(I).getAlignment())+1); 739 Vals.push_back(cast<LoadInst>(I).isVolatile()); 740 break; 741 case Instruction::Store: 742 Code = bitc::FUNC_CODE_INST_STORE; 743 Vals.push_back(VE.getTypeID(I.getOperand(1)->getType())); // Pointer 744 Vals.push_back(VE.getValueID(I.getOperand(0))); // val. 745 Vals.push_back(VE.getValueID(I.getOperand(1))); // ptr. 746 Vals.push_back(Log2_32(cast<StoreInst>(I).getAlignment())+1); 747 Vals.push_back(cast<StoreInst>(I).isVolatile()); 748 break; 749 case Instruction::Call: { 750 Code = bitc::FUNC_CODE_INST_CALL; 751 Vals.push_back((cast<CallInst>(I).getCallingConv() << 1) | 752 cast<CallInst>(I).isTailCall()); 753 PushValueAndType(I.getOperand(0), InstID, Vals, VE); // Callee 754 755 // Emit value #'s for the fixed parameters. 756 const PointerType *PTy = cast<PointerType>(I.getOperand(0)->getType()); 757 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); 758 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) 759 Vals.push_back(VE.getValueID(I.getOperand(i+1))); // fixed param. 760 761 // Emit type/value pairs for varargs params. 762 if (FTy->isVarArg()) { 763 unsigned NumVarargs = I.getNumOperands()-1-FTy->getNumParams(); 764 for (unsigned i = I.getNumOperands()-NumVarargs, e = I.getNumOperands(); 765 i != e; ++i) 766 PushValueAndType(I.getOperand(i), InstID, Vals, VE); // varargs 767 } 768 break; 769 } 770 case Instruction::VAArg: 771 Code = bitc::FUNC_CODE_INST_VAARG; 772 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); // valistty 773 Vals.push_back(VE.getValueID(I.getOperand(0))); // valist. 774 Vals.push_back(VE.getTypeID(I.getType())); // restype. 775 break; 776 } 777 778 Stream.EmitRecord(Code, Vals, AbbrevToUse); 779 Vals.clear(); 780} 781 782// Emit names for globals/functions etc. 783static void WriteValueSymbolTable(const ValueSymbolTable &VST, 784 const ValueEnumerator &VE, 785 BitstreamWriter &Stream) { 786 if (VST.empty()) return; 787 Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4); 788 789 // FIXME: Set up the abbrev, we know how many values there are! 790 // FIXME: We know if the type names can use 7-bit ascii. 791 SmallVector<unsigned, 64> NameVals; 792 793 for (ValueSymbolTable::const_iterator SI = VST.begin(), SE = VST.end(); 794 SI != SE; ++SI) { 795 796 const ValueName &Name = *SI; 797 798 // Figure out the encoding to use for the name. 799 bool is7Bit = true; 800 bool isChar6 = true; 801 for (const char *C = Name.getKeyData(), *E = C+Name.getKeyLength(); 802 C != E; ++C) { 803 if (isChar6) 804 isChar6 = BitCodeAbbrevOp::isChar6(*C); 805 if ((unsigned char)*C & 128) { 806 is7Bit = false; 807 break; // don't bother scanning the rest. 808 } 809 } 810 811 unsigned AbbrevToUse = VST_ENTRY_8_ABBREV; 812 813 // VST_ENTRY: [valueid, namechar x N] 814 // VST_BBENTRY: [bbid, namechar x N] 815 unsigned Code; 816 if (isa<BasicBlock>(SI->getValue())) { 817 Code = bitc::VST_CODE_BBENTRY; 818 if (isChar6) 819 AbbrevToUse = VST_BBENTRY_6_ABBREV; 820 } else { 821 Code = bitc::VST_CODE_ENTRY; 822 if (isChar6) 823 AbbrevToUse = VST_ENTRY_6_ABBREV; 824 else if (is7Bit) 825 AbbrevToUse = VST_ENTRY_7_ABBREV; 826 } 827 828 NameVals.push_back(VE.getValueID(SI->getValue())); 829 for (const char *P = Name.getKeyData(), 830 *E = Name.getKeyData()+Name.getKeyLength(); P != E; ++P) 831 NameVals.push_back((unsigned char)*P); 832 833 // Emit the finished record. 834 Stream.EmitRecord(Code, NameVals, AbbrevToUse); 835 NameVals.clear(); 836 } 837 Stream.ExitBlock(); 838} 839 840/// WriteFunction - Emit a function body to the module stream. 841static void WriteFunction(const Function &F, ValueEnumerator &VE, 842 BitstreamWriter &Stream) { 843 Stream.EnterSubblock(bitc::FUNCTION_BLOCK_ID, 3); 844 VE.incorporateFunction(F); 845 846 SmallVector<unsigned, 64> Vals; 847 848 // Emit the number of basic blocks, so the reader can create them ahead of 849 // time. 850 Vals.push_back(VE.getBasicBlocks().size()); 851 Stream.EmitRecord(bitc::FUNC_CODE_DECLAREBLOCKS, Vals); 852 Vals.clear(); 853 854 // FIXME: Function attributes? 855 856 // If there are function-local constants, emit them now. 857 unsigned CstStart, CstEnd; 858 VE.getFunctionConstantRange(CstStart, CstEnd); 859 WriteConstants(CstStart, CstEnd, VE, Stream, false); 860 861 // Keep a running idea of what the instruction ID is. 862 unsigned InstID = CstEnd; 863 864 // Finally, emit all the instructions, in order. 865 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) 866 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); 867 I != E; ++I) { 868 WriteInstruction(*I, InstID, VE, Stream, Vals); 869 if (I->getType() != Type::VoidTy) 870 ++InstID; 871 } 872 873 // Emit names for all the instructions etc. 874 WriteValueSymbolTable(F.getValueSymbolTable(), VE, Stream); 875 876 VE.purgeFunction(); 877 Stream.ExitBlock(); 878} 879 880/// WriteTypeSymbolTable - Emit a block for the specified type symtab. 881static void WriteTypeSymbolTable(const TypeSymbolTable &TST, 882 const ValueEnumerator &VE, 883 BitstreamWriter &Stream) { 884 if (TST.empty()) return; 885 886 Stream.EnterSubblock(bitc::TYPE_SYMTAB_BLOCK_ID, 3); 887 888 // 7-bit fixed width VST_CODE_ENTRY strings. 889 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 890 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY)); 891 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 892 Log2_32_Ceil(VE.getTypes().size()+1))); 893 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 894 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); 895 unsigned V7Abbrev = Stream.EmitAbbrev(Abbv); 896 897 SmallVector<unsigned, 64> NameVals; 898 899 for (TypeSymbolTable::const_iterator TI = TST.begin(), TE = TST.end(); 900 TI != TE; ++TI) { 901 // TST_ENTRY: [typeid, namechar x N] 902 NameVals.push_back(VE.getTypeID(TI->second)); 903 904 const std::string &Str = TI->first; 905 bool is7Bit = true; 906 for (unsigned i = 0, e = Str.size(); i != e; ++i) { 907 NameVals.push_back((unsigned char)Str[i]); 908 if (Str[i] & 128) 909 is7Bit = false; 910 } 911 912 // Emit the finished record. 913 Stream.EmitRecord(bitc::VST_CODE_ENTRY, NameVals, is7Bit ? V7Abbrev : 0); 914 NameVals.clear(); 915 } 916 917 Stream.ExitBlock(); 918} 919 920// Emit blockinfo, which defines the standard abbreviations etc. 921static void WriteBlockInfo(const ValueEnumerator &VE, BitstreamWriter &Stream) { 922 // We only want to emit block info records for blocks that have multiple 923 // instances: CONSTANTS_BLOCK, FUNCTION_BLOCK and VALUE_SYMTAB_BLOCK. Other 924 // blocks can defined their abbrevs inline. 925 Stream.EnterBlockInfoBlock(2); 926 927 { // 8-bit fixed-width VST_ENTRY/VST_BBENTRY strings. 928 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 929 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); 930 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 931 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 932 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); 933 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, 934 Abbv) != VST_ENTRY_8_ABBREV) 935 assert(0 && "Unexpected abbrev ordering!"); 936 } 937 938 { // 7-bit fixed width VST_ENTRY strings. 939 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 940 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY)); 941 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 942 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 943 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); 944 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, 945 Abbv) != VST_ENTRY_7_ABBREV) 946 assert(0 && "Unexpected abbrev ordering!"); 947 } 948 { // 6-bit char6 VST_ENTRY strings. 949 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 950 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY)); 951 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 952 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 953 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6)); 954 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, 955 Abbv) != VST_ENTRY_6_ABBREV) 956 assert(0 && "Unexpected abbrev ordering!"); 957 } 958 { // 6-bit char6 VST_BBENTRY strings. 959 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 960 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_BBENTRY)); 961 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 962 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 963 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6)); 964 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, 965 Abbv) != VST_BBENTRY_6_ABBREV) 966 assert(0 && "Unexpected abbrev ordering!"); 967 } 968 969 970 971 { // SETTYPE abbrev for CONSTANTS_BLOCK. 972 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 973 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_SETTYPE)); 974 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 975 Log2_32_Ceil(VE.getTypes().size()+1))); 976 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, 977 Abbv) != CONSTANTS_SETTYPE_ABBREV) 978 assert(0 && "Unexpected abbrev ordering!"); 979 } 980 981 { // INTEGER abbrev for CONSTANTS_BLOCK. 982 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 983 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_INTEGER)); 984 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 985 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, 986 Abbv) != CONSTANTS_INTEGER_ABBREV) 987 assert(0 && "Unexpected abbrev ordering!"); 988 } 989 990 { // CE_CAST abbrev for CONSTANTS_BLOCK. 991 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 992 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CE_CAST)); 993 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // cast opc 994 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // typeid 995 Log2_32_Ceil(VE.getTypes().size()+1))); 996 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id 997 998 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, 999 Abbv) != CONSTANTS_CE_CAST_Abbrev) 1000 assert(0 && "Unexpected abbrev ordering!"); 1001 } 1002 { // NULL abbrev for CONSTANTS_BLOCK. 1003 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1004 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_NULL)); 1005 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, 1006 Abbv) != CONSTANTS_NULL_Abbrev) 1007 assert(0 && "Unexpected abbrev ordering!"); 1008 } 1009 1010 // FIXME: This should only use space for first class types! 1011 1012 { // INST_LOAD abbrev for FUNCTION_BLOCK. 1013 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1014 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_LOAD)); 1015 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Ptr 1016 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // Align 1017 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // volatile 1018 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, 1019 Abbv) != FUNCTION_INST_LOAD_ABBREV) 1020 assert(0 && "Unexpected abbrev ordering!"); 1021 } 1022 1023 Stream.ExitBlock(); 1024} 1025 1026 1027/// WriteModule - Emit the specified module to the bitstream. 1028static void WriteModule(const Module *M, BitstreamWriter &Stream) { 1029 Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3); 1030 1031 // Emit the version number if it is non-zero. 1032 if (CurVersion) { 1033 SmallVector<unsigned, 1> Vals; 1034 Vals.push_back(CurVersion); 1035 Stream.EmitRecord(bitc::MODULE_CODE_VERSION, Vals); 1036 } 1037 1038 // Analyze the module, enumerating globals, functions, etc. 1039 ValueEnumerator VE(M); 1040 1041 // Emit blockinfo, which defines the standard abbreviations etc. 1042 WriteBlockInfo(VE, Stream); 1043 1044 // Emit information about parameter attributes. 1045 WriteParamAttrTable(VE, Stream); 1046 1047 // Emit information describing all of the types in the module. 1048 WriteTypeTable(VE, Stream); 1049 1050 // Emit top-level description of module, including target triple, inline asm, 1051 // descriptors for global variables, and function prototype info. 1052 WriteModuleInfo(M, VE, Stream); 1053 1054 // Emit constants. 1055 WriteModuleConstants(VE, Stream); 1056 1057 // If we have any aggregate values in the value table, purge them - these can 1058 // only be used to initialize global variables. Doing so makes the value 1059 // namespace smaller for code in functions. 1060 int NumNonAggregates = VE.PurgeAggregateValues(); 1061 if (NumNonAggregates != -1) { 1062 SmallVector<unsigned, 1> Vals; 1063 Vals.push_back(NumNonAggregates); 1064 Stream.EmitRecord(bitc::MODULE_CODE_PURGEVALS, Vals); 1065 } 1066 1067 // Emit function bodies. 1068 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) 1069 if (!I->isDeclaration()) 1070 WriteFunction(*I, VE, Stream); 1071 1072 // Emit the type symbol table information. 1073 WriteTypeSymbolTable(M->getTypeSymbolTable(), VE, Stream); 1074 1075 // Emit names for globals/functions etc. 1076 WriteValueSymbolTable(M->getValueSymbolTable(), VE, Stream); 1077 1078 Stream.ExitBlock(); 1079} 1080 1081 1082/// WriteBitcodeToFile - Write the specified module to the specified output 1083/// stream. 1084void llvm::WriteBitcodeToFile(const Module *M, std::ostream &Out) { 1085 std::vector<unsigned char> Buffer; 1086 BitstreamWriter Stream(Buffer); 1087 1088 Buffer.reserve(256*1024); 1089 1090 // Emit the file header. 1091 Stream.Emit((unsigned)'B', 8); 1092 Stream.Emit((unsigned)'C', 8); 1093 Stream.Emit(0x0, 4); 1094 Stream.Emit(0xC, 4); 1095 Stream.Emit(0xE, 4); 1096 Stream.Emit(0xD, 4); 1097 1098 // Emit the module. 1099 WriteModule(M, Stream); 1100 1101 // Write the generated bitstream to "Out". 1102 Out.write((char*)&Buffer.front(), Buffer.size()); 1103} 1104