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) && cast<ConstantArray>(C)->isString()) { 488 // Emit constant strings specially. 489 Code = bitc::CST_CODE_STRING; 490 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) 491 Record.push_back(cast<ConstantInt>(C->getOperand(i))->getZExtValue()); 492 493 } else if (isa<ConstantArray>(C) || isa<ConstantStruct>(V) || 494 isa<ConstantVector>(V)) { 495 Code = bitc::CST_CODE_AGGREGATE; 496 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) 497 Record.push_back(VE.getValueID(C->getOperand(i))); 498 AbbrevToUse = AggregateAbbrev; 499 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { 500 switch (CE->getOpcode()) { 501 default: 502 if (Instruction::isCast(CE->getOpcode())) { 503 Code = bitc::CST_CODE_CE_CAST; 504 Record.push_back(GetEncodedCastOpcode(CE->getOpcode())); 505 Record.push_back(VE.getTypeID(C->getOperand(0)->getType())); 506 Record.push_back(VE.getValueID(C->getOperand(0))); 507 AbbrevToUse = CONSTANTS_CE_CAST_Abbrev; 508 } else { 509 assert(CE->getNumOperands() == 2 && "Unknown constant expr!"); 510 Code = bitc::CST_CODE_CE_BINOP; 511 Record.push_back(GetEncodedBinaryOpcode(CE->getOpcode())); 512 Record.push_back(VE.getValueID(C->getOperand(0))); 513 Record.push_back(VE.getValueID(C->getOperand(1))); 514 } 515 break; 516 case Instruction::GetElementPtr: 517 Code = bitc::CST_CODE_CE_GEP; 518 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) { 519 Record.push_back(VE.getTypeID(C->getOperand(i)->getType())); 520 Record.push_back(VE.getValueID(C->getOperand(i))); 521 } 522 AbbrevToUse = GEPAbbrev; 523 break; 524 case Instruction::Select: 525 Code = bitc::CST_CODE_CE_SELECT; 526 Record.push_back(VE.getValueID(C->getOperand(0))); 527 Record.push_back(VE.getValueID(C->getOperand(1))); 528 Record.push_back(VE.getValueID(C->getOperand(2))); 529 break; 530 case Instruction::ExtractElement: 531 Code = bitc::CST_CODE_CE_EXTRACTELT; 532 Record.push_back(VE.getTypeID(C->getOperand(0)->getType())); 533 Record.push_back(VE.getValueID(C->getOperand(0))); 534 Record.push_back(VE.getValueID(C->getOperand(1))); 535 break; 536 case Instruction::InsertElement: 537 Code = bitc::CST_CODE_CE_INSERTELT; 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::ShuffleVector: 543 Code = bitc::CST_CODE_CE_SHUFFLEVEC; 544 Record.push_back(VE.getValueID(C->getOperand(0))); 545 Record.push_back(VE.getValueID(C->getOperand(1))); 546 Record.push_back(VE.getValueID(C->getOperand(2))); 547 break; 548 case Instruction::ICmp: 549 case Instruction::FCmp: 550 Code = bitc::CST_CODE_CE_CMP; 551 Record.push_back(VE.getTypeID(C->getOperand(0)->getType())); 552 Record.push_back(VE.getValueID(C->getOperand(0))); 553 Record.push_back(VE.getValueID(C->getOperand(1))); 554 Record.push_back(CE->getPredicate()); 555 break; 556 } 557 } else { 558 assert(0 && "Unknown constant!"); 559 } 560 Stream.EmitRecord(Code, Record, AbbrevToUse); 561 Record.clear(); 562 } 563 564 Stream.ExitBlock(); 565 } 566 567 static void WriteModuleConstants(const ValueEnumerator &VE, 568 BitstreamWriter &Stream) { 569 const ValueEnumerator::ValueList &Vals = VE.getValues(); 570 571 // Find the first constant to emit, which is the first non-globalvalue value. 572 // We know globalvalues have been emitted by WriteModuleInfo. 573 for (unsigned i = 0, e = Vals.size(); i != e; ++i) { 574 if (!isa<GlobalValue>(Vals[i].first)) { 575 WriteConstants(i, Vals.size(), VE, Stream, true); 576 return; 577 } 578 } 579 } 580 581 /// PushValueAndType - The file has to encode both the value and type id for 582 /// many values, because we need to know what type to create for forward 583 /// references. However, most operands are not forward references, so this type 584 /// field is not needed. 585 /// 586 /// This function adds V's value ID to Vals. If the value ID is higher than the 587 /// instruction ID, then it is a forward reference, and it also includes the 588 /// type ID. 589 static bool PushValueAndType(Value *V, unsigned InstID, 590 SmallVector<unsigned, 64> &Vals, 591 ValueEnumerator &VE) { 592 unsigned ValID = VE.getValueID(V); 593 Vals.push_back(ValID); 594 if (ValID >= InstID) { 595 Vals.push_back(VE.getTypeID(V->getType())); 596 return true; 597 } 598 return false; 599 } 600 601 /// WriteInstruction - Emit an instruction to the specified stream. 602 static void WriteInstruction(const Instruction &I, unsigned InstID, 603 ValueEnumerator &VE, BitstreamWriter &Stream, 604 SmallVector<unsigned, 64> &Vals) { 605 unsigned Code = 0; 606 unsigned AbbrevToUse = 0; 607 switch (I.getOpcode()) { 608 default: 609 if (Instruction::isCast(I.getOpcode())) { 610 Code = bitc::FUNC_CODE_INST_CAST; 611 PushValueAndType(I.getOperand(0), InstID, Vals, VE); 612 Vals.push_back(VE.getTypeID(I.getType())); 613 Vals.push_back(GetEncodedCastOpcode(I.getOpcode())); 614 } else { 615 assert(isa<BinaryOperator>(I) && "Unknown instruction!"); 616 Code = bitc::FUNC_CODE_INST_BINOP; 617 PushValueAndType(I.getOperand(0), InstID, Vals, VE); 618 Vals.push_back(VE.getValueID(I.getOperand(1))); 619 Vals.push_back(GetEncodedBinaryOpcode(I.getOpcode())); 620 } 621 break; 622 623 case Instruction::GetElementPtr: 624 Code = bitc::FUNC_CODE_INST_GEP; 625 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) 626 PushValueAndType(I.getOperand(i), InstID, Vals, VE); 627 break; 628 case Instruction::Select: 629 Code = bitc::FUNC_CODE_INST_SELECT; 630 PushValueAndType(I.getOperand(1), InstID, Vals, VE); 631 Vals.push_back(VE.getValueID(I.getOperand(2))); 632 Vals.push_back(VE.getValueID(I.getOperand(0))); 633 break; 634 case Instruction::ExtractElement: 635 Code = bitc::FUNC_CODE_INST_EXTRACTELT; 636 PushValueAndType(I.getOperand(0), InstID, Vals, VE); 637 Vals.push_back(VE.getValueID(I.getOperand(1))); 638 break; 639 case Instruction::InsertElement: 640 Code = bitc::FUNC_CODE_INST_INSERTELT; 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::ShuffleVector: 646 Code = bitc::FUNC_CODE_INST_SHUFFLEVEC; 647 PushValueAndType(I.getOperand(0), InstID, Vals, VE); 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 PushValueAndType(I.getOperand(0), InstID, Vals, VE); 723 break; 724 725 case Instruction::Alloca: 726 Code = bitc::FUNC_CODE_INST_ALLOCA; 727 Vals.push_back(VE.getTypeID(I.getType())); 728 Vals.push_back(VE.getValueID(I.getOperand(0))); // size. 729 Vals.push_back(Log2_32(cast<AllocaInst>(I).getAlignment())+1); 730 break; 731 732 case Instruction::Load: 733 Code = bitc::FUNC_CODE_INST_LOAD; 734 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE)) // ptr 735 AbbrevToUse = FUNCTION_INST_LOAD_ABBREV; 736 737 Vals.push_back(Log2_32(cast<LoadInst>(I).getAlignment())+1); 738 Vals.push_back(cast<LoadInst>(I).isVolatile()); 739 break; 740 case Instruction::Store: 741 Code = bitc::FUNC_CODE_INST_STORE; 742 PushValueAndType(I.getOperand(0), InstID, Vals, VE); // val. 743 Vals.push_back(VE.getValueID(I.getOperand(1))); // ptr. 744 Vals.push_back(Log2_32(cast<StoreInst>(I).getAlignment())+1); 745 Vals.push_back(cast<StoreInst>(I).isVolatile()); 746 break; 747 case Instruction::Call: { 748 Code = bitc::FUNC_CODE_INST_CALL; 749 Vals.push_back((cast<CallInst>(I).getCallingConv() << 1) | 750 cast<CallInst>(I).isTailCall()); 751 PushValueAndType(I.getOperand(0), InstID, Vals, VE); // Callee 752 753 // Emit value #'s for the fixed parameters. 754 const PointerType *PTy = cast<PointerType>(I.getOperand(0)->getType()); 755 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); 756 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) 757 Vals.push_back(VE.getValueID(I.getOperand(i+1))); // fixed param. 758 759 // Emit type/value pairs for varargs params. 760 if (FTy->isVarArg()) { 761 unsigned NumVarargs = I.getNumOperands()-1-FTy->getNumParams(); 762 for (unsigned i = I.getNumOperands()-NumVarargs, e = I.getNumOperands(); 763 i != e; ++i) 764 PushValueAndType(I.getOperand(i), InstID, Vals, VE); // varargs 765 } 766 break; 767 } 768 case Instruction::VAArg: 769 Code = bitc::FUNC_CODE_INST_VAARG; 770 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); // valistty 771 Vals.push_back(VE.getValueID(I.getOperand(0))); // valist. 772 Vals.push_back(VE.getTypeID(I.getType())); // restype. 773 break; 774 } 775 776 Stream.EmitRecord(Code, Vals, AbbrevToUse); 777 Vals.clear(); 778 } 779 780 // Emit names for globals/functions etc. 781 static void WriteValueSymbolTable(const ValueSymbolTable &VST, 782 const ValueEnumerator &VE, 783 BitstreamWriter &Stream) { 784 if (VST.empty()) return; 785 Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4); 786 787 // FIXME: Set up the abbrev, we know how many values there are! 788 // FIXME: We know if the type names can use 7-bit ascii. 789 SmallVector<unsigned, 64> NameVals; 790 791 for (ValueSymbolTable::const_iterator SI = VST.begin(), SE = VST.end(); 792 SI != SE; ++SI) { 793 794 const ValueName &Name = *SI; 795 796 // Figure out the encoding to use for the name. 797 bool is7Bit = true; 798 bool isChar6 = true; 799 for (const char *C = Name.getKeyData(), *E = C+Name.getKeyLength(); 800 C != E; ++C) { 801 if (isChar6) 802 isChar6 = BitCodeAbbrevOp::isChar6(*C); 803 if ((unsigned char)*C & 128) { 804 is7Bit = false; 805 break; // don't bother scanning the rest. 806 } 807 } 808 809 unsigned AbbrevToUse = VST_ENTRY_8_ABBREV; 810 811 // VST_ENTRY: [valueid, namechar x N] 812 // VST_BBENTRY: [bbid, namechar x N] 813 unsigned Code; 814 if (isa<BasicBlock>(SI->getValue())) { 815 Code = bitc::VST_CODE_BBENTRY; 816 if (isChar6) 817 AbbrevToUse = VST_BBENTRY_6_ABBREV; 818 } else { 819 Code = bitc::VST_CODE_ENTRY; 820 if (isChar6) 821 AbbrevToUse = VST_ENTRY_6_ABBREV; 822 else if (is7Bit) 823 AbbrevToUse = VST_ENTRY_7_ABBREV; 824 } 825 826 NameVals.push_back(VE.getValueID(SI->getValue())); 827 for (const char *P = Name.getKeyData(), 828 *E = Name.getKeyData()+Name.getKeyLength(); P != E; ++P) 829 NameVals.push_back((unsigned char)*P); 830 831 // Emit the finished record. 832 Stream.EmitRecord(Code, NameVals, AbbrevToUse); 833 NameVals.clear(); 834 } 835 Stream.ExitBlock(); 836 } 837 838 /// WriteFunction - Emit a function body to the module stream. 839 static void WriteFunction(const Function &F, ValueEnumerator &VE, 840 BitstreamWriter &Stream) { 841 Stream.EnterSubblock(bitc::FUNCTION_BLOCK_ID, 3); 842 VE.incorporateFunction(F); 843 844 SmallVector<unsigned, 64> Vals; 845 846 // Emit the number of basic blocks, so the reader can create them ahead of 847 // time. 848 Vals.push_back(VE.getBasicBlocks().size()); 849 Stream.EmitRecord(bitc::FUNC_CODE_DECLAREBLOCKS, Vals); 850 Vals.clear(); 851 852 // FIXME: Function attributes? 853 854 // If there are function-local constants, emit them now. 855 unsigned CstStart, CstEnd; 856 VE.getFunctionConstantRange(CstStart, CstEnd); 857 WriteConstants(CstStart, CstEnd, VE, Stream, false); 858 859 // Keep a running idea of what the instruction ID is. 860 unsigned InstID = CstEnd; 861 862 // Finally, emit all the instructions, in order. 863 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) 864 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); 865 I != E; ++I) { 866 WriteInstruction(*I, InstID, VE, Stream, Vals); 867 if (I->getType() != Type::VoidTy) 868 ++InstID; 869 } 870 871 // Emit names for all the instructions etc. 872 WriteValueSymbolTable(F.getValueSymbolTable(), VE, Stream); 873 874 VE.purgeFunction(); 875 Stream.ExitBlock(); 876 } 877 878 /// WriteTypeSymbolTable - Emit a block for the specified type symtab. 879 static void WriteTypeSymbolTable(const TypeSymbolTable &TST, 880 const ValueEnumerator &VE, 881 BitstreamWriter &Stream) { 882 if (TST.empty()) return; 883 884 Stream.EnterSubblock(bitc::TYPE_SYMTAB_BLOCK_ID, 3); 885 886 // 7-bit fixed width VST_CODE_ENTRY strings. 887 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 888 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY)); 889 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 890 Log2_32_Ceil(VE.getTypes().size()+1))); 891 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 892 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); 893 unsigned V7Abbrev = Stream.EmitAbbrev(Abbv); 894 895 SmallVector<unsigned, 64> NameVals; 896 897 for (TypeSymbolTable::const_iterator TI = TST.begin(), TE = TST.end(); 898 TI != TE; ++TI) { 899 // TST_ENTRY: [typeid, namechar x N] 900 NameVals.push_back(VE.getTypeID(TI->second)); 901 902 const std::string &Str = TI->first; 903 bool is7Bit = true; 904 for (unsigned i = 0, e = Str.size(); i != e; ++i) { 905 NameVals.push_back((unsigned char)Str[i]); 906 if (Str[i] & 128) 907 is7Bit = false; 908 } 909 910 // Emit the finished record. 911 Stream.EmitRecord(bitc::VST_CODE_ENTRY, NameVals, is7Bit ? V7Abbrev : 0); 912 NameVals.clear(); 913 } 914 915 Stream.ExitBlock(); 916 } 917 918 // Emit blockinfo, which defines the standard abbreviations etc. 919 static void WriteBlockInfo(const ValueEnumerator &VE, BitstreamWriter &Stream) { 920 // We only want to emit block info records for blocks that have multiple 921 // instances: CONSTANTS_BLOCK, FUNCTION_BLOCK and VALUE_SYMTAB_BLOCK. Other 922 // blocks can defined their abbrevs inline. 923 Stream.EnterBlockInfoBlock(2); 924 925 { // 8-bit fixed-width VST_ENTRY/VST_BBENTRY strings. 926 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 927 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); 928 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 929 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 930 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); 931 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, 932 Abbv) != VST_ENTRY_8_ABBREV) 933 assert(0 && "Unexpected abbrev ordering!"); 934 } 935 936 { // 7-bit fixed width VST_ENTRY strings. 937 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 938 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY)); 939 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 940 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 941 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); 942 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, 943 Abbv) != VST_ENTRY_7_ABBREV) 944 assert(0 && "Unexpected abbrev ordering!"); 945 } 946 { // 6-bit char6 VST_ENTRY strings. 947 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 948 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY)); 949 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 950 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 951 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6)); 952 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, 953 Abbv) != VST_ENTRY_6_ABBREV) 954 assert(0 && "Unexpected abbrev ordering!"); 955 } 956 { // 6-bit char6 VST_BBENTRY strings. 957 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 958 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_BBENTRY)); 959 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 960 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 961 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6)); 962 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, 963 Abbv) != VST_BBENTRY_6_ABBREV) 964 assert(0 && "Unexpected abbrev ordering!"); 965 } 966 967 968 969 { // SETTYPE abbrev for CONSTANTS_BLOCK. 970 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 971 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_SETTYPE)); 972 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 973 Log2_32_Ceil(VE.getTypes().size()+1))); 974 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, 975 Abbv) != CONSTANTS_SETTYPE_ABBREV) 976 assert(0 && "Unexpected abbrev ordering!"); 977 } 978 979 { // INTEGER abbrev for CONSTANTS_BLOCK. 980 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 981 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_INTEGER)); 982 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 983 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, 984 Abbv) != CONSTANTS_INTEGER_ABBREV) 985 assert(0 && "Unexpected abbrev ordering!"); 986 } 987 988 { // CE_CAST abbrev for CONSTANTS_BLOCK. 989 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 990 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CE_CAST)); 991 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // cast opc 992 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // typeid 993 Log2_32_Ceil(VE.getTypes().size()+1))); 994 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id 995 996 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, 997 Abbv) != CONSTANTS_CE_CAST_Abbrev) 998 assert(0 && "Unexpected abbrev ordering!"); 999 } 1000 { // NULL abbrev for CONSTANTS_BLOCK. 1001 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1002 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_NULL)); 1003 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, 1004 Abbv) != CONSTANTS_NULL_Abbrev) 1005 assert(0 && "Unexpected abbrev ordering!"); 1006 } 1007 1008 // FIXME: This should only use space for first class types! 1009 1010 { // INST_LOAD abbrev for FUNCTION_BLOCK. 1011 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1012 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_LOAD)); 1013 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Ptr 1014 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // Align 1015 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // volatile 1016 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, 1017 Abbv) != FUNCTION_INST_LOAD_ABBREV) 1018 assert(0 && "Unexpected abbrev ordering!"); 1019 } 1020 1021 Stream.ExitBlock(); 1022 } 1023 1024 1025 /// WriteModule - Emit the specified module to the bitstream. 1026 static void WriteModule(const Module *M, BitstreamWriter &Stream) { 1027 Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3); 1028 1029 // Emit the version number if it is non-zero. 1030 if (CurVersion) { 1031 SmallVector<unsigned, 1> Vals; 1032 Vals.push_back(CurVersion); 1033 Stream.EmitRecord(bitc::MODULE_CODE_VERSION, Vals); 1034 } 1035 1036 // Analyze the module, enumerating globals, functions, etc. 1037 ValueEnumerator VE(M); 1038 1039 // Emit blockinfo, which defines the standard abbreviations etc. 1040 WriteBlockInfo(VE, Stream); 1041 1042 // Emit information about parameter attributes. 1043 WriteParamAttrTable(VE, Stream); 1044 1045 // Emit information describing all of the types in the module. 1046 WriteTypeTable(VE, Stream); 1047 1048 // Emit top-level description of module, including target triple, inline asm, 1049 // descriptors for global variables, and function prototype info. 1050 WriteModuleInfo(M, VE, Stream); 1051 1052 // Emit constants. 1053 WriteModuleConstants(VE, Stream); 1054 1055 // If we have any aggregate values in the value table, purge them - these can 1056 // only be used to initialize global variables. Doing so makes the value 1057 // namespace smaller for code in functions. 1058 int NumNonAggregates = VE.PurgeAggregateValues(); 1059 if (NumNonAggregates != -1) { 1060 SmallVector<unsigned, 1> Vals; 1061 Vals.push_back(NumNonAggregates); 1062 Stream.EmitRecord(bitc::MODULE_CODE_PURGEVALS, Vals); 1063 } 1064 1065 // Emit function bodies. 1066 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) 1067 if (!I->isDeclaration()) 1068 WriteFunction(*I, VE, Stream); 1069 1070 // Emit the type symbol table information. 1071 WriteTypeSymbolTable(M->getTypeSymbolTable(), VE, Stream); 1072 1073 // Emit names for globals/functions etc. 1074 WriteValueSymbolTable(M->getValueSymbolTable(), VE, Stream); 1075 1076 Stream.ExitBlock(); 1077 } 1078 1079 1080 /// WriteBitcodeToFile - Write the specified module to the specified output 1081 /// stream. 1082 void llvm::WriteBitcodeToFile(const Module *M, std::ostream &Out) { 1083 std::vector<unsigned char> Buffer; 1084 BitstreamWriter Stream(Buffer); 1085 1086 Buffer.reserve(256*1024); 1087 1088 // Emit the file header. 1089 Stream.Emit((unsigned)'B', 8); 1090 Stream.Emit((unsigned)'C', 8); 1091 Stream.Emit(0x0, 4); 1092 Stream.Emit(0xC, 4); 1093 Stream.Emit(0xE, 4); 1094 Stream.Emit(0xD, 4); 1095 1096 // Emit the module. 1097 WriteModule(M, Stream); 1098 1099 // Write the generated bitstream to "Out". 1100 Out.write((char*)&Buffer.front(), Buffer.size()); 1101 } 1102