Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 1 | //===--- Bitcode/Writer/Writer.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" |
Chris Lattner | 47f96bf | 2007-04-23 01:01:37 +0000 | [diff] [blame] | 16 | #include "llvm/Bitcode/LLVMBitCodes.h" |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 17 | #include "ValueEnumerator.h" |
Chris Lattner | 2edd22b | 2007-04-24 00:16:04 +0000 | [diff] [blame] | 18 | #include "llvm/Constants.h" |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 19 | #include "llvm/DerivedTypes.h" |
| 20 | #include "llvm/Module.h" |
| 21 | #include "llvm/TypeSymbolTable.h" |
Chris Lattner | b992be1 | 2007-04-23 20:35:01 +0000 | [diff] [blame] | 22 | #include "llvm/ValueSymbolTable.h" |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 23 | #include "llvm/Support/MathExtras.h" |
| 24 | using namespace llvm; |
| 25 | |
| 26 | static const unsigned CurVersion = 0; |
| 27 | |
Chris Lattner | f581c3b | 2007-04-24 07:07:11 +0000 | [diff] [blame^] | 28 | static unsigned GetEncodedCastOpcode(unsigned Opcode) { |
| 29 | switch (Opcode) { |
| 30 | default: assert(0 && "Unknown cast instruction!"); |
| 31 | case Instruction::Trunc : return bitc::CAST_TRUNC; |
| 32 | case Instruction::ZExt : return bitc::CAST_ZEXT; |
| 33 | case Instruction::SExt : return bitc::CAST_SEXT; |
| 34 | case Instruction::FPToUI : return bitc::CAST_FPTOUI; |
| 35 | case Instruction::FPToSI : return bitc::CAST_FPTOSI; |
| 36 | case Instruction::UIToFP : return bitc::CAST_UITOFP; |
| 37 | case Instruction::SIToFP : return bitc::CAST_SITOFP; |
| 38 | case Instruction::FPTrunc : return bitc::CAST_FPTRUNC; |
| 39 | case Instruction::FPExt : return bitc::CAST_FPEXT; |
| 40 | case Instruction::PtrToInt: return bitc::CAST_PTRTOINT; |
| 41 | case Instruction::IntToPtr: return bitc::CAST_INTTOPTR; |
| 42 | case Instruction::BitCast : return bitc::CAST_BITCAST; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | static unsigned GetEncodedBinaryOpcode(unsigned Opcode) { |
| 47 | switch (Opcode) { |
| 48 | default: assert(0 && "Unknown binary instruction!"); |
| 49 | case Instruction::Add: return bitc::BINOP_ADD; |
| 50 | case Instruction::Sub: return bitc::BINOP_SUB; |
| 51 | case Instruction::Mul: return bitc::BINOP_MUL; |
| 52 | case Instruction::UDiv: return bitc::BINOP_UDIV; |
| 53 | case Instruction::FDiv: |
| 54 | case Instruction::SDiv: return bitc::BINOP_SDIV; |
| 55 | case Instruction::URem: return bitc::BINOP_UREM; |
| 56 | case Instruction::FRem: |
| 57 | case Instruction::SRem: return bitc::BINOP_SREM; |
| 58 | case Instruction::Shl: return bitc::BINOP_SHL; |
| 59 | case Instruction::LShr: return bitc::BINOP_LSHR; |
| 60 | case Instruction::AShr: return bitc::BINOP_ASHR; |
| 61 | case Instruction::And: return bitc::BINOP_AND; |
| 62 | case Instruction::Or: return bitc::BINOP_OR; |
| 63 | case Instruction::Xor: return bitc::BINOP_XOR; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | |
| 68 | |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 69 | static void WriteStringRecord(unsigned Code, const std::string &Str, |
| 70 | unsigned AbbrevToUse, BitstreamWriter &Stream) { |
| 71 | SmallVector<unsigned, 64> Vals; |
| 72 | |
| 73 | // Code: [strlen, strchar x N] |
| 74 | Vals.push_back(Str.size()); |
| 75 | for (unsigned i = 0, e = Str.size(); i != e; ++i) |
| 76 | Vals.push_back(Str[i]); |
| 77 | |
| 78 | // Emit the finished record. |
| 79 | Stream.EmitRecord(Code, Vals, AbbrevToUse); |
| 80 | } |
| 81 | |
| 82 | |
| 83 | /// WriteTypeTable - Write out the type table for a module. |
| 84 | static void WriteTypeTable(const ValueEnumerator &VE, BitstreamWriter &Stream) { |
| 85 | const ValueEnumerator::TypeList &TypeList = VE.getTypes(); |
| 86 | |
| 87 | Stream.EnterSubblock(bitc::TYPE_BLOCK_ID, 4 /*count from # abbrevs */); |
| 88 | SmallVector<uint64_t, 64> TypeVals; |
| 89 | |
| 90 | // FIXME: Set up abbrevs now that we know the width of the type fields, etc. |
| 91 | |
| 92 | // Emit an entry count so the reader can reserve space. |
| 93 | TypeVals.push_back(TypeList.size()); |
| 94 | Stream.EmitRecord(bitc::TYPE_CODE_NUMENTRY, TypeVals); |
| 95 | TypeVals.clear(); |
| 96 | |
| 97 | // Loop over all of the types, emitting each in turn. |
| 98 | for (unsigned i = 0, e = TypeList.size(); i != e; ++i) { |
| 99 | const Type *T = TypeList[i].first; |
| 100 | int AbbrevToUse = 0; |
| 101 | unsigned Code = 0; |
| 102 | |
| 103 | switch (T->getTypeID()) { |
| 104 | case Type::PackedStructTyID: // FIXME: Delete Type::PackedStructTyID. |
| 105 | default: assert(0 && "Unknown type!"); |
| 106 | case Type::VoidTyID: Code = bitc::TYPE_CODE_VOID; break; |
| 107 | case Type::FloatTyID: Code = bitc::TYPE_CODE_FLOAT; break; |
| 108 | case Type::DoubleTyID: Code = bitc::TYPE_CODE_DOUBLE; break; |
| 109 | case Type::LabelTyID: Code = bitc::TYPE_CODE_LABEL; break; |
| 110 | case Type::OpaqueTyID: Code = bitc::TYPE_CODE_OPAQUE; break; |
| 111 | case Type::IntegerTyID: |
| 112 | // INTEGER: [width] |
| 113 | Code = bitc::TYPE_CODE_INTEGER; |
| 114 | TypeVals.push_back(cast<IntegerType>(T)->getBitWidth()); |
| 115 | break; |
| 116 | case Type::PointerTyID: |
| 117 | // POINTER: [pointee type] |
| 118 | Code = bitc::TYPE_CODE_POINTER; |
| 119 | TypeVals.push_back(VE.getTypeID(cast<PointerType>(T)->getElementType())); |
| 120 | break; |
| 121 | |
| 122 | case Type::FunctionTyID: { |
| 123 | const FunctionType *FT = cast<FunctionType>(T); |
| 124 | // FUNCTION: [isvararg, #pararms, paramty x N] |
| 125 | Code = bitc::TYPE_CODE_FUNCTION; |
| 126 | TypeVals.push_back(FT->isVarArg()); |
| 127 | TypeVals.push_back(VE.getTypeID(FT->getReturnType())); |
| 128 | // FIXME: PARAM ATTR ID! |
| 129 | TypeVals.push_back(FT->getNumParams()); |
| 130 | for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) |
| 131 | TypeVals.push_back(VE.getTypeID(FT->getParamType(i))); |
| 132 | break; |
| 133 | } |
| 134 | case Type::StructTyID: { |
| 135 | const StructType *ST = cast<StructType>(T); |
| 136 | // STRUCT: [ispacked, #elts, eltty x N] |
| 137 | Code = bitc::TYPE_CODE_STRUCT; |
| 138 | TypeVals.push_back(ST->isPacked()); |
| 139 | TypeVals.push_back(ST->getNumElements()); |
| 140 | // Output all of the element types... |
| 141 | for (StructType::element_iterator I = ST->element_begin(), |
| 142 | E = ST->element_end(); I != E; ++I) |
| 143 | TypeVals.push_back(VE.getTypeID(*I)); |
| 144 | break; |
| 145 | } |
| 146 | case Type::ArrayTyID: { |
| 147 | const ArrayType *AT = cast<ArrayType>(T); |
| 148 | // ARRAY: [numelts, eltty] |
| 149 | Code = bitc::TYPE_CODE_ARRAY; |
| 150 | TypeVals.push_back(AT->getNumElements()); |
| 151 | TypeVals.push_back(VE.getTypeID(AT->getElementType())); |
| 152 | break; |
| 153 | } |
| 154 | case Type::VectorTyID: { |
| 155 | const VectorType *VT = cast<VectorType>(T); |
| 156 | // VECTOR [numelts, eltty] |
| 157 | Code = bitc::TYPE_CODE_VECTOR; |
| 158 | TypeVals.push_back(VT->getNumElements()); |
| 159 | TypeVals.push_back(VE.getTypeID(VT->getElementType())); |
| 160 | break; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | // Emit the finished record. |
| 165 | Stream.EmitRecord(Code, TypeVals, AbbrevToUse); |
| 166 | TypeVals.clear(); |
| 167 | } |
| 168 | |
| 169 | Stream.ExitBlock(); |
| 170 | } |
| 171 | |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 172 | static unsigned getEncodedLinkage(const GlobalValue *GV) { |
| 173 | switch (GV->getLinkage()) { |
| 174 | default: assert(0 && "Invalid linkage!"); |
| 175 | case GlobalValue::ExternalLinkage: return 0; |
| 176 | case GlobalValue::WeakLinkage: return 1; |
| 177 | case GlobalValue::AppendingLinkage: return 2; |
| 178 | case GlobalValue::InternalLinkage: return 3; |
| 179 | case GlobalValue::LinkOnceLinkage: return 4; |
| 180 | case GlobalValue::DLLImportLinkage: return 5; |
| 181 | case GlobalValue::DLLExportLinkage: return 6; |
| 182 | case GlobalValue::ExternalWeakLinkage: return 7; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | static unsigned getEncodedVisibility(const GlobalValue *GV) { |
| 187 | switch (GV->getVisibility()) { |
| 188 | default: assert(0 && "Invalid visibility!"); |
| 189 | case GlobalValue::DefaultVisibility: return 0; |
| 190 | case GlobalValue::HiddenVisibility: return 1; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | // Emit top-level description of module, including target triple, inline asm, |
| 195 | // descriptors for global variables, and function prototype info. |
| 196 | static void WriteModuleInfo(const Module *M, const ValueEnumerator &VE, |
| 197 | BitstreamWriter &Stream) { |
| 198 | // Emit the list of dependent libraries for the Module. |
| 199 | for (Module::lib_iterator I = M->lib_begin(), E = M->lib_end(); I != E; ++I) |
| 200 | WriteStringRecord(bitc::MODULE_CODE_DEPLIB, *I, 0/*TODO*/, Stream); |
| 201 | |
| 202 | // Emit various pieces of data attached to a module. |
| 203 | if (!M->getTargetTriple().empty()) |
| 204 | WriteStringRecord(bitc::MODULE_CODE_TRIPLE, M->getTargetTriple(), |
| 205 | 0/*TODO*/, Stream); |
| 206 | if (!M->getDataLayout().empty()) |
| 207 | WriteStringRecord(bitc::MODULE_CODE_DATALAYOUT, M->getDataLayout(), |
| 208 | 0/*TODO*/, Stream); |
| 209 | if (!M->getModuleInlineAsm().empty()) |
| 210 | WriteStringRecord(bitc::MODULE_CODE_ASM, M->getModuleInlineAsm(), |
| 211 | 0/*TODO*/, Stream); |
| 212 | |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 213 | // Emit information about sections, computing how many there are. Also |
| 214 | // compute the maximum alignment value. |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 215 | std::map<std::string, unsigned> SectionMap; |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 216 | unsigned MaxAlignment = 0; |
Chris Lattner | d127c1b | 2007-04-23 18:58:34 +0000 | [diff] [blame] | 217 | unsigned MaxGlobalType = 0; |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 218 | for (Module::const_global_iterator GV = M->global_begin(),E = M->global_end(); |
| 219 | GV != E; ++GV) { |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 220 | MaxAlignment = std::max(MaxAlignment, GV->getAlignment()); |
Chris Lattner | d127c1b | 2007-04-23 18:58:34 +0000 | [diff] [blame] | 221 | MaxGlobalType = std::max(MaxGlobalType, VE.getTypeID(GV->getType())); |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 222 | |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 223 | if (!GV->hasSection()) continue; |
| 224 | // Give section names unique ID's. |
| 225 | unsigned &Entry = SectionMap[GV->getSection()]; |
| 226 | if (Entry != 0) continue; |
| 227 | WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, GV->getSection(), |
| 228 | 0/*TODO*/, Stream); |
| 229 | Entry = SectionMap.size(); |
| 230 | } |
| 231 | for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) { |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 232 | MaxAlignment = std::max(MaxAlignment, F->getAlignment()); |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 233 | if (!F->hasSection()) continue; |
| 234 | // Give section names unique ID's. |
| 235 | unsigned &Entry = SectionMap[F->getSection()]; |
| 236 | if (Entry != 0) continue; |
| 237 | WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, F->getSection(), |
| 238 | 0/*TODO*/, Stream); |
| 239 | Entry = SectionMap.size(); |
| 240 | } |
| 241 | |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 242 | // Emit abbrev for globals, now that we know # sections and max alignment. |
| 243 | unsigned SimpleGVarAbbrev = 0; |
Chris Lattner | d127c1b | 2007-04-23 18:58:34 +0000 | [diff] [blame] | 244 | if (!M->global_empty()) { |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 245 | // Add an abbrev for common globals with no visibility or thread localness. |
| 246 | BitCodeAbbrev *Abbv = new BitCodeAbbrev(); |
| 247 | Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_GLOBALVAR)); |
Chris Lattner | d127c1b | 2007-04-23 18:58:34 +0000 | [diff] [blame] | 248 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::FixedWidth, |
| 249 | Log2_32_Ceil(MaxGlobalType+1))); |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 250 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::FixedWidth, 1)); // Constant. |
| 251 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Initializer. |
| 252 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::FixedWidth, 3)); // Linkage. |
| 253 | if (MaxAlignment == 0) // Alignment. |
| 254 | Abbv->Add(BitCodeAbbrevOp(0)); |
| 255 | else { |
| 256 | unsigned MaxEncAlignment = Log2_32(MaxAlignment)+1; |
| 257 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::FixedWidth, |
Chris Lattner | d127c1b | 2007-04-23 18:58:34 +0000 | [diff] [blame] | 258 | Log2_32_Ceil(MaxEncAlignment+1))); |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 259 | } |
| 260 | if (SectionMap.empty()) // Section. |
| 261 | Abbv->Add(BitCodeAbbrevOp(0)); |
| 262 | else |
| 263 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::FixedWidth, |
Chris Lattner | 631a8ed | 2007-04-24 03:29:47 +0000 | [diff] [blame] | 264 | Log2_32_Ceil(SectionMap.size()+1))); |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 265 | // Don't bother emitting vis + thread local. |
| 266 | SimpleGVarAbbrev = Stream.EmitAbbrev(Abbv); |
| 267 | } |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 268 | |
| 269 | // Emit the global variable information. |
| 270 | SmallVector<unsigned, 64> Vals; |
| 271 | for (Module::const_global_iterator GV = M->global_begin(),E = M->global_end(); |
| 272 | GV != E; ++GV) { |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 273 | unsigned AbbrevToUse = 0; |
| 274 | |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 275 | // GLOBALVAR: [type, isconst, initid, |
| 276 | // linkage, alignment, section, visibility, threadlocal] |
| 277 | Vals.push_back(VE.getTypeID(GV->getType())); |
| 278 | Vals.push_back(GV->isConstant()); |
| 279 | Vals.push_back(GV->isDeclaration() ? 0 : |
| 280 | (VE.getValueID(GV->getInitializer()) + 1)); |
| 281 | Vals.push_back(getEncodedLinkage(GV)); |
| 282 | Vals.push_back(Log2_32(GV->getAlignment())+1); |
| 283 | Vals.push_back(GV->hasSection() ? SectionMap[GV->getSection()] : 0); |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 284 | if (GV->isThreadLocal() || |
| 285 | GV->getVisibility() != GlobalValue::DefaultVisibility) { |
| 286 | Vals.push_back(getEncodedVisibility(GV)); |
| 287 | Vals.push_back(GV->isThreadLocal()); |
| 288 | } else { |
| 289 | AbbrevToUse = SimpleGVarAbbrev; |
| 290 | } |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 291 | |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 292 | Stream.EmitRecord(bitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse); |
| 293 | Vals.clear(); |
| 294 | } |
| 295 | |
| 296 | // Emit the function proto information. |
| 297 | for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) { |
| 298 | // FUNCTION: [type, callingconv, isproto, linkage, alignment, section, |
| 299 | // visibility] |
| 300 | Vals.push_back(VE.getTypeID(F->getType())); |
| 301 | Vals.push_back(F->getCallingConv()); |
| 302 | Vals.push_back(F->isDeclaration()); |
| 303 | Vals.push_back(getEncodedLinkage(F)); |
| 304 | Vals.push_back(Log2_32(F->getAlignment())+1); |
| 305 | Vals.push_back(F->hasSection() ? SectionMap[F->getSection()] : 0); |
| 306 | Vals.push_back(getEncodedVisibility(F)); |
| 307 | |
| 308 | unsigned AbbrevToUse = 0; |
| 309 | Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse); |
| 310 | Vals.clear(); |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | |
Chris Lattner | b992be1 | 2007-04-23 20:35:01 +0000 | [diff] [blame] | 315 | /// WriteTypeSymbolTable - Emit a block for the specified type symtab. |
| 316 | static void WriteTypeSymbolTable(const TypeSymbolTable &TST, |
| 317 | const ValueEnumerator &VE, |
| 318 | BitstreamWriter &Stream) { |
| 319 | if (TST.empty()) return; |
| 320 | |
| 321 | Stream.EnterSubblock(bitc::TYPE_SYMTAB_BLOCK_ID, 3); |
| 322 | |
| 323 | // FIXME: Set up the abbrev, we know how many types there are! |
| 324 | // FIXME: We know if the type names can use 7-bit ascii. |
| 325 | |
| 326 | SmallVector<unsigned, 64> NameVals; |
| 327 | |
| 328 | for (TypeSymbolTable::const_iterator TI = TST.begin(), TE = TST.end(); |
| 329 | TI != TE; ++TI) { |
| 330 | unsigned AbbrevToUse = 0; |
| 331 | |
| 332 | // TST_ENTRY: [typeid, namelen, namechar x N] |
| 333 | NameVals.push_back(VE.getTypeID(TI->second)); |
| 334 | |
| 335 | const std::string &Str = TI->first; |
| 336 | NameVals.push_back(Str.size()); |
| 337 | for (unsigned i = 0, e = Str.size(); i != e; ++i) |
| 338 | NameVals.push_back(Str[i]); |
| 339 | |
| 340 | // Emit the finished record. |
Chris Lattner | 2edd22b | 2007-04-24 00:16:04 +0000 | [diff] [blame] | 341 | Stream.EmitRecord(bitc::VST_CODE_ENTRY, NameVals, AbbrevToUse); |
Chris Lattner | b992be1 | 2007-04-23 20:35:01 +0000 | [diff] [blame] | 342 | NameVals.clear(); |
| 343 | } |
| 344 | |
| 345 | Stream.ExitBlock(); |
| 346 | } |
| 347 | |
| 348 | // Emit names for globals/functions etc. |
| 349 | static void WriteValueSymbolTable(const ValueSymbolTable &VST, |
| 350 | const ValueEnumerator &VE, |
| 351 | BitstreamWriter &Stream) { |
| 352 | if (VST.empty()) return; |
| 353 | Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 3); |
| 354 | |
| 355 | // FIXME: Set up the abbrev, we know how many values there are! |
| 356 | // FIXME: We know if the type names can use 7-bit ascii. |
| 357 | SmallVector<unsigned, 64> NameVals; |
| 358 | |
| 359 | for (ValueSymbolTable::const_iterator SI = VST.begin(), SE = VST.end(); |
| 360 | SI != SE; ++SI) { |
| 361 | unsigned AbbrevToUse = 0; |
| 362 | |
| 363 | // VST_ENTRY: [valueid, namelen, namechar x N] |
| 364 | NameVals.push_back(VE.getValueID(SI->getValue())); |
| 365 | |
| 366 | NameVals.push_back(SI->getKeyLength()); |
| 367 | for (const char *P = SI->getKeyData(), |
| 368 | *E = SI->getKeyData()+SI->getKeyLength(); P != E; ++P) |
| 369 | NameVals.push_back((unsigned char)*P); |
| 370 | |
| 371 | // Emit the finished record. |
Chris Lattner | 2edd22b | 2007-04-24 00:16:04 +0000 | [diff] [blame] | 372 | Stream.EmitRecord(bitc::VST_CODE_ENTRY, NameVals, AbbrevToUse); |
Chris Lattner | b992be1 | 2007-04-23 20:35:01 +0000 | [diff] [blame] | 373 | NameVals.clear(); |
| 374 | } |
| 375 | Stream.ExitBlock(); |
| 376 | } |
| 377 | |
Chris Lattner | 2edd22b | 2007-04-24 00:16:04 +0000 | [diff] [blame] | 378 | static void WriteConstants(unsigned FirstVal, unsigned LastVal, |
| 379 | const ValueEnumerator &VE, |
| 380 | BitstreamWriter &Stream) { |
| 381 | if (FirstVal == LastVal) return; |
| 382 | |
| 383 | Stream.EnterSubblock(bitc::CONSTANTS_BLOCK_ID, 2); |
Chris Lattner | b992be1 | 2007-04-23 20:35:01 +0000 | [diff] [blame] | 384 | |
Chris Lattner | 2edd22b | 2007-04-24 00:16:04 +0000 | [diff] [blame] | 385 | // FIXME: Install and use abbrevs to reduce size. |
| 386 | |
| 387 | SmallVector<uint64_t, 64> Record; |
| 388 | |
| 389 | const ValueEnumerator::ValueList &Vals = VE.getValues(); |
| 390 | const Type *LastTy = 0; |
| 391 | for (unsigned i = FirstVal; i != LastVal; ++i) { |
| 392 | const Value *V = Vals[i].first; |
| 393 | // If we need to switch types, do so now. |
| 394 | if (V->getType() != LastTy) { |
| 395 | LastTy = V->getType(); |
| 396 | Record.push_back(VE.getTypeID(LastTy)); |
| 397 | Stream.EmitRecord(bitc::CST_CODE_SETTYPE, Record); |
| 398 | Record.clear(); |
| 399 | } |
| 400 | |
| 401 | if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) { |
| 402 | assert(0 && IA && "FIXME: Inline asm writing unimp!"); |
| 403 | continue; |
| 404 | } |
| 405 | const Constant *C = cast<Constant>(V); |
| 406 | unsigned Code = -1U; |
| 407 | unsigned AbbrevToUse = 0; |
| 408 | if (C->isNullValue()) { |
| 409 | Code = bitc::CST_CODE_NULL; |
| 410 | } else if (isa<UndefValue>(C)) { |
| 411 | Code = bitc::CST_CODE_UNDEF; |
| 412 | } else if (const ConstantInt *IV = dyn_cast<ConstantInt>(C)) { |
| 413 | if (IV->getBitWidth() <= 64) { |
| 414 | int64_t V = IV->getSExtValue(); |
| 415 | if (V >= 0) |
| 416 | Record.push_back(V << 1); |
| 417 | else |
| 418 | Record.push_back((-V << 1) | 1); |
| 419 | Code = bitc::CST_CODE_INTEGER; |
| 420 | } else { // Wide integers, > 64 bits in size. |
| 421 | // We have an arbitrary precision integer value to write whose |
| 422 | // bit width is > 64. However, in canonical unsigned integer |
| 423 | // format it is likely that the high bits are going to be zero. |
| 424 | // So, we only write the number of active words. |
| 425 | unsigned NWords = IV->getValue().getActiveWords(); |
| 426 | const uint64_t *RawWords = IV->getValue().getRawData(); |
| 427 | Record.push_back(NWords); |
| 428 | for (unsigned i = 0; i != NWords; ++i) { |
| 429 | int64_t V = RawWords[i]; |
| 430 | if (V >= 0) |
| 431 | Record.push_back(V << 1); |
| 432 | else |
| 433 | Record.push_back((-V << 1) | 1); |
| 434 | } |
| 435 | Code = bitc::CST_CODE_WIDE_INTEGER; |
| 436 | } |
| 437 | } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) { |
| 438 | Code = bitc::CST_CODE_FLOAT; |
| 439 | if (CFP->getType() == Type::FloatTy) { |
| 440 | Record.push_back(FloatToBits((float)CFP->getValue())); |
| 441 | } else { |
| 442 | assert (CFP->getType() == Type::DoubleTy && "Unknown FP type!"); |
| 443 | Record.push_back(DoubleToBits((double)CFP->getValue())); |
| 444 | } |
| 445 | } else if (isa<ConstantArray>(C) || isa<ConstantStruct>(V) || |
| 446 | isa<ConstantVector>(V)) { |
| 447 | Code = bitc::CST_CODE_AGGREGATE; |
| 448 | Record.push_back(C->getNumOperands()); |
| 449 | for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) |
| 450 | Record.push_back(VE.getValueID(C->getOperand(i))); |
| 451 | } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { |
Chris Lattner | f581c3b | 2007-04-24 07:07:11 +0000 | [diff] [blame^] | 452 | switch (CE->getOpcode()) { |
| 453 | default: |
| 454 | if (Instruction::isCast(CE->getOpcode())) { |
| 455 | Code = bitc::CST_CODE_CE_CAST; |
| 456 | Record.push_back(GetEncodedCastOpcode(CE->getOpcode())); |
| 457 | Record.push_back(VE.getTypeID(C->getOperand(0)->getType())); |
| 458 | Record.push_back(VE.getValueID(C->getOperand(0))); |
| 459 | } else { |
| 460 | assert(CE->getNumOperands() == 2 && "Unknown constant expr!"); |
| 461 | Code = bitc::CST_CODE_CE_BINOP; |
| 462 | Record.push_back(GetEncodedBinaryOpcode(CE->getOpcode())); |
| 463 | Record.push_back(VE.getValueID(C->getOperand(0))); |
| 464 | Record.push_back(VE.getValueID(C->getOperand(1))); |
| 465 | } |
| 466 | break; |
| 467 | case Instruction::GetElementPtr: |
| 468 | Code = bitc::CST_CODE_CE_GEP; |
| 469 | Record.push_back(CE->getNumOperands()); |
| 470 | for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) { |
| 471 | Record.push_back(VE.getTypeID(C->getOperand(i)->getType())); |
| 472 | Record.push_back(VE.getValueID(C->getOperand(i))); |
| 473 | } |
| 474 | break; |
| 475 | case Instruction::Select: |
| 476 | Code = bitc::CST_CODE_CE_SELECT; |
| 477 | Record.push_back(VE.getValueID(C->getOperand(0))); |
| 478 | Record.push_back(VE.getValueID(C->getOperand(1))); |
| 479 | Record.push_back(VE.getValueID(C->getOperand(2))); |
| 480 | break; |
| 481 | case Instruction::ExtractElement: |
| 482 | Code = bitc::CST_CODE_CE_EXTRACTELT; |
| 483 | Record.push_back(VE.getTypeID(C->getOperand(0)->getType())); |
| 484 | Record.push_back(VE.getValueID(C->getOperand(0))); |
| 485 | Record.push_back(VE.getValueID(C->getOperand(1))); |
| 486 | break; |
| 487 | case Instruction::InsertElement: |
| 488 | Code = bitc::CST_CODE_CE_INSERTELT; |
| 489 | Record.push_back(VE.getValueID(C->getOperand(0))); |
| 490 | Record.push_back(VE.getValueID(C->getOperand(1))); |
| 491 | Record.push_back(VE.getValueID(C->getOperand(2))); |
| 492 | break; |
| 493 | case Instruction::ShuffleVector: |
| 494 | Code = bitc::CST_CODE_CE_SHUFFLEVEC; |
| 495 | Record.push_back(VE.getValueID(C->getOperand(0))); |
| 496 | Record.push_back(VE.getValueID(C->getOperand(1))); |
| 497 | Record.push_back(VE.getValueID(C->getOperand(2))); |
| 498 | break; |
| 499 | case Instruction::ICmp: |
| 500 | case Instruction::FCmp: |
| 501 | Code = bitc::CST_CODE_CE_CMP; |
| 502 | Record.push_back(VE.getTypeID(C->getOperand(0)->getType())); |
| 503 | Record.push_back(VE.getValueID(C->getOperand(0))); |
| 504 | Record.push_back(VE.getValueID(C->getOperand(1))); |
| 505 | Record.push_back(CE->getPredicate()); |
| 506 | break; |
| 507 | } |
Chris Lattner | 2edd22b | 2007-04-24 00:16:04 +0000 | [diff] [blame] | 508 | } else { |
| 509 | assert(0 && "Unknown constant!"); |
| 510 | } |
| 511 | Stream.EmitRecord(Code, Record, AbbrevToUse); |
| 512 | Record.clear(); |
| 513 | } |
| 514 | |
| 515 | Stream.ExitBlock(); |
| 516 | } |
| 517 | |
| 518 | static void WriteModuleConstants(const ValueEnumerator &VE, |
| 519 | BitstreamWriter &Stream) { |
| 520 | const ValueEnumerator::ValueList &Vals = VE.getValues(); |
| 521 | |
| 522 | // Find the first constant to emit, which is the first non-globalvalue value. |
| 523 | // We know globalvalues have been emitted by WriteModuleInfo. |
| 524 | for (unsigned i = 0, e = Vals.size(); i != e; ++i) { |
| 525 | if (!isa<GlobalValue>(Vals[i].first)) { |
| 526 | WriteConstants(i, Vals.size(), VE, Stream); |
| 527 | return; |
| 528 | } |
| 529 | } |
| 530 | } |
Chris Lattner | b992be1 | 2007-04-23 20:35:01 +0000 | [diff] [blame] | 531 | |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 532 | /// WriteModule - Emit the specified module to the bitstream. |
| 533 | static void WriteModule(const Module *M, BitstreamWriter &Stream) { |
Chris Lattner | d127c1b | 2007-04-23 18:58:34 +0000 | [diff] [blame] | 534 | Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3); |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 535 | |
| 536 | // Emit the version number if it is non-zero. |
| 537 | if (CurVersion) { |
| 538 | SmallVector<unsigned, 1> VersionVals; |
| 539 | VersionVals.push_back(CurVersion); |
| 540 | Stream.EmitRecord(bitc::MODULE_CODE_VERSION, VersionVals); |
| 541 | } |
| 542 | |
| 543 | // Analyze the module, enumerating globals, functions, etc. |
| 544 | ValueEnumerator VE(M); |
| 545 | |
| 546 | // Emit information describing all of the types in the module. |
| 547 | WriteTypeTable(VE, Stream); |
| 548 | |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 549 | // Emit top-level description of module, including target triple, inline asm, |
| 550 | // descriptors for global variables, and function prototype info. |
| 551 | WriteModuleInfo(M, VE, Stream); |
| 552 | |
Chris Lattner | 2edd22b | 2007-04-24 00:16:04 +0000 | [diff] [blame] | 553 | // Emit constants. |
| 554 | WriteModuleConstants(VE, Stream); |
| 555 | |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 556 | // Emit the type symbol table information. |
| 557 | WriteTypeSymbolTable(M->getTypeSymbolTable(), VE, Stream); |
Chris Lattner | b992be1 | 2007-04-23 20:35:01 +0000 | [diff] [blame] | 558 | |
| 559 | // Emit names for globals/functions etc. |
| 560 | WriteValueSymbolTable(M->getValueSymbolTable(), VE, Stream); |
| 561 | |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 562 | Stream.ExitBlock(); |
| 563 | } |
| 564 | |
| 565 | /// WriteBitcodeToFile - Write the specified module to the specified output |
| 566 | /// stream. |
| 567 | void llvm::WriteBitcodeToFile(const Module *M, std::ostream &Out) { |
| 568 | std::vector<unsigned char> Buffer; |
| 569 | BitstreamWriter Stream(Buffer); |
| 570 | |
| 571 | Buffer.reserve(256*1024); |
| 572 | |
| 573 | // Emit the file header. |
| 574 | Stream.Emit((unsigned)'B', 8); |
| 575 | Stream.Emit((unsigned)'C', 8); |
| 576 | Stream.Emit(0x0, 4); |
| 577 | Stream.Emit(0xC, 4); |
| 578 | Stream.Emit(0xE, 4); |
| 579 | Stream.Emit(0xD, 4); |
| 580 | |
| 581 | // Emit the module. |
| 582 | WriteModule(M, Stream); |
| 583 | |
| 584 | // Write the generated bitstream to "Out". |
| 585 | Out.write((char*)&Buffer.front(), Buffer.size()); |
| 586 | } |