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