Chris Lattner | 1a9df8e | 2007-04-29 05:31:57 +0000 | [diff] [blame] | 1 | //===--- Bitcode/Writer/BitcodeWriter.cpp - Bitcode Writer ----------------===// |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 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" |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 20 | #include "llvm/Instructions.h" |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 21 | #include "llvm/Module.h" |
Chris Lattner | f0a6531 | 2007-05-04 02:59:04 +0000 | [diff] [blame] | 22 | #include "llvm/ParameterAttributes.h" |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 23 | #include "llvm/TypeSymbolTable.h" |
Chris Lattner | b992be1 | 2007-04-23 20:35:01 +0000 | [diff] [blame] | 24 | #include "llvm/ValueSymbolTable.h" |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 25 | #include "llvm/Support/MathExtras.h" |
| 26 | using namespace llvm; |
| 27 | |
Chris Lattner | 5969830 | 2007-05-04 20:52:02 +0000 | [diff] [blame] | 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, |
Chris Lattner | 2453f71 | 2007-05-04 20:58:35 +0000 | [diff] [blame] | 35 | VST_ENTRY_7_ABBREV, |
Chris Lattner | ff294a4 | 2007-05-05 01:26:50 +0000 | [diff] [blame] | 36 | VST_ENTRY_6_ABBREV, |
Chris Lattner | a0f1ecc | 2007-05-05 07:36:14 +0000 | [diff] [blame] | 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, |
Chris Lattner | 440168b | 2007-05-05 07:44:49 +0000 | [diff] [blame] | 43 | CONSTANTS_NULL_Abbrev, |
| 44 | |
| 45 | // FUNCTION_BLOCK abbrev id's. |
| 46 | FUNCTION_INST_LOAD_ABBREV = bitc::FIRST_APPLICATION_ABBREV |
Chris Lattner | 5969830 | 2007-05-04 20:52:02 +0000 | [diff] [blame] | 47 | }; |
| 48 | |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 49 | |
Chris Lattner | f581c3b | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 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 | |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 91 | static void WriteStringRecord(unsigned Code, const std::string &Str, |
| 92 | unsigned AbbrevToUse, BitstreamWriter &Stream) { |
| 93 | SmallVector<unsigned, 64> Vals; |
| 94 | |
Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 95 | // Code: [strchar x N] |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 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 | |
Chris Lattner | 62bbeea | 2007-05-04 00:44:52 +0000 | [diff] [blame] | 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 | |
Chris Lattner | f0a6531 | 2007-05-04 02:59:04 +0000 | [diff] [blame] | 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)); |
Chris Lattner | f6f9cd1 | 2007-05-04 03:14:09 +0000 | [diff] [blame] | 116 | Record.push_back(A->getParamAttrsAtIndex(op)); |
Chris Lattner | f0a6531 | 2007-05-04 02:59:04 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | Stream.EmitRecord(bitc::PARAMATTR_CODE_ENTRY, Record); |
| 120 | Record.clear(); |
| 121 | } |
Chris Lattner | 62bbeea | 2007-05-04 00:44:52 +0000 | [diff] [blame] | 122 | |
Chris Lattner | f0a6531 | 2007-05-04 02:59:04 +0000 | [diff] [blame] | 123 | Stream.ExitBlock(); |
Chris Lattner | 62bbeea | 2007-05-04 00:44:52 +0000 | [diff] [blame] | 124 | } |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 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 | |
Chris Lattner | d092e0e | 2007-05-05 06:30:12 +0000 | [diff] [blame] | 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); |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 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())); |
Chris Lattner | d092e0e | 2007-05-05 06:30:12 +0000 | [diff] [blame] | 196 | AbbrevToUse = PtrAbbrev; |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 197 | break; |
| 198 | |
| 199 | case Type::FunctionTyID: { |
| 200 | const FunctionType *FT = cast<FunctionType>(T); |
Chris Lattner | d092e0e | 2007-05-05 06:30:12 +0000 | [diff] [blame] | 201 | // FUNCTION: [isvararg, attrid, retty, paramty x N] |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 202 | Code = bitc::TYPE_CODE_FUNCTION; |
| 203 | TypeVals.push_back(FT->isVarArg()); |
Chris Lattner | 9113e73 | 2007-05-04 03:41:34 +0000 | [diff] [blame] | 204 | TypeVals.push_back(VE.getParamAttrID(FT->getParamAttrs())); |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 205 | TypeVals.push_back(VE.getTypeID(FT->getReturnType())); |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 206 | for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) |
| 207 | TypeVals.push_back(VE.getTypeID(FT->getParamType(i))); |
Chris Lattner | d092e0e | 2007-05-05 06:30:12 +0000 | [diff] [blame] | 208 | AbbrevToUse = FunctionAbbrev; |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 209 | break; |
| 210 | } |
| 211 | case Type::StructTyID: { |
| 212 | const StructType *ST = cast<StructType>(T); |
Chris Lattner | d092e0e | 2007-05-05 06:30:12 +0000 | [diff] [blame] | 213 | // STRUCT: [ispacked, eltty x N] |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 214 | Code = bitc::TYPE_CODE_STRUCT; |
| 215 | TypeVals.push_back(ST->isPacked()); |
Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 216 | // Output all of the element types. |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 217 | for (StructType::element_iterator I = ST->element_begin(), |
| 218 | E = ST->element_end(); I != E; ++I) |
| 219 | TypeVals.push_back(VE.getTypeID(*I)); |
Chris Lattner | d092e0e | 2007-05-05 06:30:12 +0000 | [diff] [blame] | 220 | AbbrevToUse = StructAbbrev; |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 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())); |
Chris Lattner | d092e0e | 2007-05-05 06:30:12 +0000 | [diff] [blame] | 229 | AbbrevToUse = ArrayAbbrev; |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 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 | |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 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!"); |
Anton Korobeynikov | 9cd3ccf | 2007-04-29 20:56:48 +0000 | [diff] [blame] | 267 | case GlobalValue::DefaultVisibility: return 0; |
| 268 | case GlobalValue::HiddenVisibility: return 1; |
| 269 | case GlobalValue::ProtectedVisibility: return 2; |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 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 | |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 292 | // Emit information about sections, computing how many there are. Also |
| 293 | // compute the maximum alignment value. |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 294 | std::map<std::string, unsigned> SectionMap; |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 295 | unsigned MaxAlignment = 0; |
Chris Lattner | d127c1b | 2007-04-23 18:58:34 +0000 | [diff] [blame] | 296 | unsigned MaxGlobalType = 0; |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 297 | for (Module::const_global_iterator GV = M->global_begin(),E = M->global_end(); |
| 298 | GV != E; ++GV) { |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 299 | MaxAlignment = std::max(MaxAlignment, GV->getAlignment()); |
Chris Lattner | d127c1b | 2007-04-23 18:58:34 +0000 | [diff] [blame] | 300 | MaxGlobalType = std::max(MaxGlobalType, VE.getTypeID(GV->getType())); |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 301 | |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 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) { |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 311 | MaxAlignment = std::max(MaxAlignment, F->getAlignment()); |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 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 | |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 321 | // Emit abbrev for globals, now that we know # sections and max alignment. |
| 322 | unsigned SimpleGVarAbbrev = 0; |
Chris Lattner | d127c1b | 2007-04-23 18:58:34 +0000 | [diff] [blame] | 323 | if (!M->global_empty()) { |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 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)); |
Chris Lattner | 2e7899d | 2007-05-04 20:34:50 +0000 | [diff] [blame] | 327 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, |
Chris Lattner | d127c1b | 2007-04-23 18:58:34 +0000 | [diff] [blame] | 328 | Log2_32_Ceil(MaxGlobalType+1))); |
Chris Lattner | 2e7899d | 2007-05-04 20:34:50 +0000 | [diff] [blame] | 329 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Constant. |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 330 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Initializer. |
Chris Lattner | 2e7899d | 2007-05-04 20:34:50 +0000 | [diff] [blame] | 331 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Linkage. |
| 332 | if (MaxAlignment == 0) // Alignment. |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 333 | Abbv->Add(BitCodeAbbrevOp(0)); |
| 334 | else { |
| 335 | unsigned MaxEncAlignment = Log2_32(MaxAlignment)+1; |
Chris Lattner | 2e7899d | 2007-05-04 20:34:50 +0000 | [diff] [blame] | 336 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, |
Chris Lattner | d127c1b | 2007-04-23 18:58:34 +0000 | [diff] [blame] | 337 | Log2_32_Ceil(MaxEncAlignment+1))); |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 338 | } |
| 339 | if (SectionMap.empty()) // Section. |
| 340 | Abbv->Add(BitCodeAbbrevOp(0)); |
| 341 | else |
Chris Lattner | 2e7899d | 2007-05-04 20:34:50 +0000 | [diff] [blame] | 342 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, |
Chris Lattner | 631a8ed | 2007-04-24 03:29:47 +0000 | [diff] [blame] | 343 | Log2_32_Ceil(SectionMap.size()+1))); |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 344 | // Don't bother emitting vis + thread local. |
| 345 | SimpleGVarAbbrev = Stream.EmitAbbrev(Abbv); |
| 346 | } |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 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) { |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 352 | unsigned AbbrevToUse = 0; |
| 353 | |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 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); |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 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 | } |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 370 | |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 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 | } |
Chris Lattner | 07d98b4 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 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 | } |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | |
Chris Lattner | 2edd22b | 2007-04-24 00:16:04 +0000 | [diff] [blame] | 406 | static void WriteConstants(unsigned FirstVal, unsigned LastVal, |
| 407 | const ValueEnumerator &VE, |
Chris Lattner | a0f1ecc | 2007-05-05 07:36:14 +0000 | [diff] [blame] | 408 | BitstreamWriter &Stream, bool isGlobal) { |
Chris Lattner | 2edd22b | 2007-04-24 00:16:04 +0000 | [diff] [blame] | 409 | if (FirstVal == LastVal) return; |
| 410 | |
Chris Lattner | a0f1ecc | 2007-05-05 07:36:14 +0000 | [diff] [blame] | 411 | Stream.EnterSubblock(bitc::CONSTANTS_BLOCK_ID, 4); |
Chris Lattner | b992be1 | 2007-04-23 20:35:01 +0000 | [diff] [blame] | 412 | |
Chris Lattner | a0f1ecc | 2007-05-05 07:36:14 +0000 | [diff] [blame] | 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 | |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 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. |
Chris Lattner | 2edd22b | 2007-04-24 00:16:04 +0000 | [diff] [blame] | 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)); |
Chris Lattner | a0f1ecc | 2007-05-05 07:36:14 +0000 | [diff] [blame] | 438 | Stream.EmitRecord(bitc::CST_CODE_SETTYPE, Record, |
| 439 | CONSTANTS_SETTYPE_ABBREV); |
Chris Lattner | 2edd22b | 2007-04-24 00:16:04 +0000 | [diff] [blame] | 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; |
Chris Lattner | a0f1ecc | 2007-05-05 07:36:14 +0000 | [diff] [blame] | 462 | AbbrevToUse = CONSTANTS_INTEGER_ABBREV; |
Chris Lattner | 2edd22b | 2007-04-24 00:16:04 +0000 | [diff] [blame] | 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(); |
Chris Lattner | 2edd22b | 2007-04-24 00:16:04 +0000 | [diff] [blame] | 470 | for (unsigned i = 0; i != NWords; ++i) { |
| 471 | int64_t V = RawWords[i]; |
| 472 | if (V >= 0) |
| 473 | Record.push_back(V << 1); |
| 474 | else |
| 475 | Record.push_back((-V << 1) | 1); |
| 476 | } |
| 477 | Code = bitc::CST_CODE_WIDE_INTEGER; |
| 478 | } |
| 479 | } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) { |
| 480 | Code = bitc::CST_CODE_FLOAT; |
| 481 | if (CFP->getType() == Type::FloatTy) { |
| 482 | Record.push_back(FloatToBits((float)CFP->getValue())); |
| 483 | } else { |
| 484 | assert (CFP->getType() == Type::DoubleTy && "Unknown FP type!"); |
| 485 | Record.push_back(DoubleToBits((double)CFP->getValue())); |
| 486 | } |
| 487 | } else if (isa<ConstantArray>(C) || isa<ConstantStruct>(V) || |
| 488 | isa<ConstantVector>(V)) { |
| 489 | Code = bitc::CST_CODE_AGGREGATE; |
Chris Lattner | 2edd22b | 2007-04-24 00:16:04 +0000 | [diff] [blame] | 490 | for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) |
| 491 | Record.push_back(VE.getValueID(C->getOperand(i))); |
Chris Lattner | a0f1ecc | 2007-05-05 07:36:14 +0000 | [diff] [blame] | 492 | AbbrevToUse = AggregateAbbrev; |
Chris Lattner | 2edd22b | 2007-04-24 00:16:04 +0000 | [diff] [blame] | 493 | } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { |
Chris Lattner | f581c3b | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 494 | switch (CE->getOpcode()) { |
| 495 | default: |
| 496 | if (Instruction::isCast(CE->getOpcode())) { |
| 497 | Code = bitc::CST_CODE_CE_CAST; |
| 498 | Record.push_back(GetEncodedCastOpcode(CE->getOpcode())); |
| 499 | Record.push_back(VE.getTypeID(C->getOperand(0)->getType())); |
| 500 | Record.push_back(VE.getValueID(C->getOperand(0))); |
Chris Lattner | a0f1ecc | 2007-05-05 07:36:14 +0000 | [diff] [blame] | 501 | AbbrevToUse = CONSTANTS_CE_CAST_Abbrev; |
Chris Lattner | f581c3b | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 502 | } else { |
| 503 | assert(CE->getNumOperands() == 2 && "Unknown constant expr!"); |
| 504 | Code = bitc::CST_CODE_CE_BINOP; |
| 505 | Record.push_back(GetEncodedBinaryOpcode(CE->getOpcode())); |
| 506 | Record.push_back(VE.getValueID(C->getOperand(0))); |
| 507 | Record.push_back(VE.getValueID(C->getOperand(1))); |
| 508 | } |
| 509 | break; |
| 510 | case Instruction::GetElementPtr: |
| 511 | Code = bitc::CST_CODE_CE_GEP; |
Chris Lattner | f581c3b | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 512 | for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) { |
| 513 | Record.push_back(VE.getTypeID(C->getOperand(i)->getType())); |
| 514 | Record.push_back(VE.getValueID(C->getOperand(i))); |
| 515 | } |
Chris Lattner | a0f1ecc | 2007-05-05 07:36:14 +0000 | [diff] [blame] | 516 | AbbrevToUse = GEPAbbrev; |
Chris Lattner | f581c3b | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 517 | break; |
| 518 | case Instruction::Select: |
| 519 | Code = bitc::CST_CODE_CE_SELECT; |
| 520 | Record.push_back(VE.getValueID(C->getOperand(0))); |
| 521 | Record.push_back(VE.getValueID(C->getOperand(1))); |
| 522 | Record.push_back(VE.getValueID(C->getOperand(2))); |
| 523 | break; |
| 524 | case Instruction::ExtractElement: |
| 525 | Code = bitc::CST_CODE_CE_EXTRACTELT; |
| 526 | Record.push_back(VE.getTypeID(C->getOperand(0)->getType())); |
| 527 | Record.push_back(VE.getValueID(C->getOperand(0))); |
| 528 | Record.push_back(VE.getValueID(C->getOperand(1))); |
| 529 | break; |
| 530 | case Instruction::InsertElement: |
| 531 | Code = bitc::CST_CODE_CE_INSERTELT; |
| 532 | Record.push_back(VE.getValueID(C->getOperand(0))); |
| 533 | Record.push_back(VE.getValueID(C->getOperand(1))); |
| 534 | Record.push_back(VE.getValueID(C->getOperand(2))); |
| 535 | break; |
| 536 | case Instruction::ShuffleVector: |
| 537 | Code = bitc::CST_CODE_CE_SHUFFLEVEC; |
| 538 | Record.push_back(VE.getValueID(C->getOperand(0))); |
| 539 | Record.push_back(VE.getValueID(C->getOperand(1))); |
| 540 | Record.push_back(VE.getValueID(C->getOperand(2))); |
| 541 | break; |
| 542 | case Instruction::ICmp: |
| 543 | case Instruction::FCmp: |
| 544 | Code = bitc::CST_CODE_CE_CMP; |
| 545 | Record.push_back(VE.getTypeID(C->getOperand(0)->getType())); |
| 546 | Record.push_back(VE.getValueID(C->getOperand(0))); |
| 547 | Record.push_back(VE.getValueID(C->getOperand(1))); |
| 548 | Record.push_back(CE->getPredicate()); |
| 549 | break; |
| 550 | } |
Chris Lattner | 2edd22b | 2007-04-24 00:16:04 +0000 | [diff] [blame] | 551 | } else { |
| 552 | assert(0 && "Unknown constant!"); |
| 553 | } |
| 554 | Stream.EmitRecord(Code, Record, AbbrevToUse); |
| 555 | Record.clear(); |
| 556 | } |
| 557 | |
| 558 | Stream.ExitBlock(); |
| 559 | } |
| 560 | |
| 561 | static void WriteModuleConstants(const ValueEnumerator &VE, |
| 562 | BitstreamWriter &Stream) { |
| 563 | const ValueEnumerator::ValueList &Vals = VE.getValues(); |
| 564 | |
| 565 | // Find the first constant to emit, which is the first non-globalvalue value. |
| 566 | // We know globalvalues have been emitted by WriteModuleInfo. |
| 567 | for (unsigned i = 0, e = Vals.size(); i != e; ++i) { |
| 568 | if (!isa<GlobalValue>(Vals[i].first)) { |
Chris Lattner | a0f1ecc | 2007-05-05 07:36:14 +0000 | [diff] [blame] | 569 | WriteConstants(i, Vals.size(), VE, Stream, true); |
Chris Lattner | 2edd22b | 2007-04-24 00:16:04 +0000 | [diff] [blame] | 570 | return; |
| 571 | } |
| 572 | } |
| 573 | } |
Chris Lattner | b992be1 | 2007-04-23 20:35:01 +0000 | [diff] [blame] | 574 | |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 575 | /// WriteInstruction - Emit an instruction to the specified stream. |
| 576 | static void WriteInstruction(const Instruction &I, ValueEnumerator &VE, |
| 577 | BitstreamWriter &Stream, |
| 578 | SmallVector<unsigned, 64> &Vals) { |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 579 | unsigned Code = 0; |
| 580 | unsigned AbbrevToUse = 0; |
| 581 | switch (I.getOpcode()) { |
| 582 | default: |
| 583 | if (Instruction::isCast(I.getOpcode())) { |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 584 | Code = bitc::FUNC_CODE_INST_CAST; |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 585 | Vals.push_back(GetEncodedCastOpcode(I.getOpcode())); |
| 586 | Vals.push_back(VE.getTypeID(I.getType())); |
| 587 | Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); |
| 588 | Vals.push_back(VE.getValueID(I.getOperand(0))); |
| 589 | } else { |
| 590 | assert(isa<BinaryOperator>(I) && "Unknown instruction!"); |
Chris Lattner | f639875 | 2007-05-02 04:26:36 +0000 | [diff] [blame] | 591 | Code = bitc::FUNC_CODE_INST_BINOP; |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 592 | Vals.push_back(GetEncodedBinaryOpcode(I.getOpcode())); |
| 593 | Vals.push_back(VE.getTypeID(I.getType())); |
| 594 | Vals.push_back(VE.getValueID(I.getOperand(0))); |
| 595 | Vals.push_back(VE.getValueID(I.getOperand(1))); |
| 596 | } |
| 597 | break; |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 598 | |
| 599 | case Instruction::GetElementPtr: |
| 600 | Code = bitc::FUNC_CODE_INST_GEP; |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 601 | for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) { |
| 602 | Vals.push_back(VE.getTypeID(I.getOperand(i)->getType())); |
| 603 | Vals.push_back(VE.getValueID(I.getOperand(i))); |
| 604 | } |
| 605 | break; |
| 606 | case Instruction::Select: |
| 607 | Code = bitc::FUNC_CODE_INST_SELECT; |
| 608 | Vals.push_back(VE.getTypeID(I.getType())); |
| 609 | Vals.push_back(VE.getValueID(I.getOperand(0))); |
| 610 | Vals.push_back(VE.getValueID(I.getOperand(1))); |
| 611 | Vals.push_back(VE.getValueID(I.getOperand(2))); |
| 612 | break; |
| 613 | case Instruction::ExtractElement: |
| 614 | Code = bitc::FUNC_CODE_INST_EXTRACTELT; |
| 615 | Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); |
| 616 | Vals.push_back(VE.getValueID(I.getOperand(0))); |
| 617 | Vals.push_back(VE.getValueID(I.getOperand(1))); |
| 618 | break; |
| 619 | case Instruction::InsertElement: |
| 620 | Code = bitc::FUNC_CODE_INST_INSERTELT; |
| 621 | Vals.push_back(VE.getTypeID(I.getType())); |
| 622 | Vals.push_back(VE.getValueID(I.getOperand(0))); |
| 623 | Vals.push_back(VE.getValueID(I.getOperand(1))); |
| 624 | Vals.push_back(VE.getValueID(I.getOperand(2))); |
| 625 | break; |
| 626 | case Instruction::ShuffleVector: |
| 627 | Code = bitc::FUNC_CODE_INST_SHUFFLEVEC; |
| 628 | Vals.push_back(VE.getTypeID(I.getType())); |
| 629 | Vals.push_back(VE.getValueID(I.getOperand(0))); |
| 630 | Vals.push_back(VE.getValueID(I.getOperand(1))); |
| 631 | Vals.push_back(VE.getValueID(I.getOperand(2))); |
| 632 | break; |
| 633 | case Instruction::ICmp: |
| 634 | case Instruction::FCmp: |
| 635 | Code = bitc::FUNC_CODE_INST_CMP; |
| 636 | Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); |
| 637 | Vals.push_back(VE.getValueID(I.getOperand(0))); |
| 638 | Vals.push_back(VE.getValueID(I.getOperand(1))); |
| 639 | Vals.push_back(cast<CmpInst>(I).getPredicate()); |
| 640 | break; |
| 641 | |
| 642 | case Instruction::Ret: |
| 643 | Code = bitc::FUNC_CODE_INST_RET; |
| 644 | if (I.getNumOperands()) { |
| 645 | Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); |
| 646 | Vals.push_back(VE.getValueID(I.getOperand(0))); |
| 647 | } |
| 648 | break; |
| 649 | case Instruction::Br: |
| 650 | Code = bitc::FUNC_CODE_INST_BR; |
| 651 | Vals.push_back(VE.getValueID(I.getOperand(0))); |
| 652 | if (cast<BranchInst>(I).isConditional()) { |
| 653 | Vals.push_back(VE.getValueID(I.getOperand(1))); |
| 654 | Vals.push_back(VE.getValueID(I.getOperand(2))); |
| 655 | } |
| 656 | break; |
| 657 | case Instruction::Switch: |
| 658 | Code = bitc::FUNC_CODE_INST_SWITCH; |
| 659 | Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 660 | for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) |
| 661 | Vals.push_back(VE.getValueID(I.getOperand(i))); |
| 662 | break; |
Chris Lattner | 60ce9b5 | 2007-05-01 07:03:37 +0000 | [diff] [blame] | 663 | case Instruction::Invoke: { |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 664 | Code = bitc::FUNC_CODE_INST_INVOKE; |
Chris Lattner | 7652019 | 2007-05-03 22:34:03 +0000 | [diff] [blame] | 665 | Vals.push_back(cast<InvokeInst>(I).getCallingConv()); |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 666 | Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); |
| 667 | Vals.push_back(VE.getValueID(I.getOperand(0))); // callee |
| 668 | Vals.push_back(VE.getValueID(I.getOperand(1))); // normal |
| 669 | Vals.push_back(VE.getValueID(I.getOperand(2))); // unwind |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 670 | |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 671 | // Emit value #'s for the fixed parameters. |
| 672 | const PointerType *PTy = cast<PointerType>(I.getOperand(0)->getType()); |
| 673 | const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); |
| 674 | for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) |
| 675 | Vals.push_back(VE.getValueID(I.getOperand(i+3))); // fixed param. |
| 676 | |
| 677 | // Emit type/value pairs for varargs params. |
| 678 | if (FTy->isVarArg()) { |
| 679 | unsigned NumVarargs = I.getNumOperands()-3-FTy->getNumParams(); |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 680 | for (unsigned i = I.getNumOperands()-NumVarargs, e = I.getNumOperands(); |
| 681 | i != e; ++i) { |
| 682 | Vals.push_back(VE.getTypeID(I.getOperand(i)->getType())); |
| 683 | Vals.push_back(VE.getValueID(I.getOperand(i))); |
| 684 | } |
| 685 | } |
| 686 | break; |
Chris Lattner | 60ce9b5 | 2007-05-01 07:03:37 +0000 | [diff] [blame] | 687 | } |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 688 | case Instruction::Unwind: |
| 689 | Code = bitc::FUNC_CODE_INST_UNWIND; |
| 690 | break; |
| 691 | case Instruction::Unreachable: |
| 692 | Code = bitc::FUNC_CODE_INST_UNREACHABLE; |
| 693 | break; |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 694 | |
| 695 | case Instruction::PHI: |
| 696 | Code = bitc::FUNC_CODE_INST_PHI; |
| 697 | Vals.push_back(VE.getTypeID(I.getType())); |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 698 | for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) |
| 699 | Vals.push_back(VE.getValueID(I.getOperand(i))); |
| 700 | break; |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 701 | |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 702 | case Instruction::Malloc: |
| 703 | Code = bitc::FUNC_CODE_INST_MALLOC; |
| 704 | Vals.push_back(VE.getTypeID(I.getType())); |
| 705 | Vals.push_back(VE.getValueID(I.getOperand(0))); // size. |
| 706 | Vals.push_back(Log2_32(cast<MallocInst>(I).getAlignment())+1); |
| 707 | break; |
| 708 | |
| 709 | case Instruction::Free: |
| 710 | Code = bitc::FUNC_CODE_INST_FREE; |
| 711 | Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); |
| 712 | Vals.push_back(VE.getValueID(I.getOperand(0))); |
| 713 | break; |
| 714 | |
| 715 | case Instruction::Alloca: |
| 716 | Code = bitc::FUNC_CODE_INST_ALLOCA; |
| 717 | Vals.push_back(VE.getTypeID(I.getType())); |
| 718 | Vals.push_back(VE.getValueID(I.getOperand(0))); // size. |
| 719 | Vals.push_back(Log2_32(cast<AllocaInst>(I).getAlignment())+1); |
| 720 | break; |
| 721 | |
| 722 | case Instruction::Load: |
| 723 | Code = bitc::FUNC_CODE_INST_LOAD; |
| 724 | Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); |
| 725 | Vals.push_back(VE.getValueID(I.getOperand(0))); // ptr. |
| 726 | Vals.push_back(Log2_32(cast<LoadInst>(I).getAlignment())+1); |
| 727 | Vals.push_back(cast<LoadInst>(I).isVolatile()); |
Chris Lattner | 440168b | 2007-05-05 07:44:49 +0000 | [diff] [blame] | 728 | AbbrevToUse = FUNCTION_INST_LOAD_ABBREV; |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 729 | break; |
| 730 | case Instruction::Store: |
| 731 | Code = bitc::FUNC_CODE_INST_STORE; |
| 732 | Vals.push_back(VE.getTypeID(I.getOperand(1)->getType())); // Pointer |
| 733 | Vals.push_back(VE.getValueID(I.getOperand(0))); // val. |
| 734 | Vals.push_back(VE.getValueID(I.getOperand(1))); // ptr. |
| 735 | Vals.push_back(Log2_32(cast<StoreInst>(I).getAlignment())+1); |
| 736 | Vals.push_back(cast<StoreInst>(I).isVolatile()); |
| 737 | break; |
| 738 | case Instruction::Call: { |
| 739 | Code = bitc::FUNC_CODE_INST_CALL; |
Chris Lattner | 7652019 | 2007-05-03 22:34:03 +0000 | [diff] [blame] | 740 | Vals.push_back((cast<CallInst>(I).getCallingConv() << 1) | |
| 741 | cast<CallInst>(I).isTailCall()); |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 742 | Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); |
| 743 | Vals.push_back(VE.getValueID(I.getOperand(0))); // callee |
| 744 | |
| 745 | // Emit value #'s for the fixed parameters. |
| 746 | const PointerType *PTy = cast<PointerType>(I.getOperand(0)->getType()); |
| 747 | const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); |
| 748 | for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) |
| 749 | Vals.push_back(VE.getValueID(I.getOperand(i+1))); // fixed param. |
| 750 | |
Chris Lattner | 60ce9b5 | 2007-05-01 07:03:37 +0000 | [diff] [blame] | 751 | // Emit type/value pairs for varargs params. |
| 752 | if (FTy->isVarArg()) { |
| 753 | unsigned NumVarargs = I.getNumOperands()-1-FTy->getNumParams(); |
Chris Lattner | 60ce9b5 | 2007-05-01 07:03:37 +0000 | [diff] [blame] | 754 | for (unsigned i = I.getNumOperands()-NumVarargs, e = I.getNumOperands(); |
| 755 | i != e; ++i) { |
| 756 | Vals.push_back(VE.getTypeID(I.getOperand(i)->getType())); |
| 757 | Vals.push_back(VE.getValueID(I.getOperand(i))); |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 758 | } |
| 759 | } |
| 760 | break; |
Chris Lattner | 60ce9b5 | 2007-05-01 07:03:37 +0000 | [diff] [blame] | 761 | } |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 762 | case Instruction::VAArg: |
| 763 | Code = bitc::FUNC_CODE_INST_VAARG; |
| 764 | Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); // valistty |
| 765 | Vals.push_back(VE.getValueID(I.getOperand(0))); // valist. |
| 766 | Vals.push_back(VE.getTypeID(I.getType())); // restype. |
| 767 | break; |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | Stream.EmitRecord(Code, Vals, AbbrevToUse); |
| 771 | Vals.clear(); |
| 772 | } |
| 773 | |
Chris Lattner | be1f993 | 2007-05-01 02:14:57 +0000 | [diff] [blame] | 774 | // Emit names for globals/functions etc. |
| 775 | static void WriteValueSymbolTable(const ValueSymbolTable &VST, |
| 776 | const ValueEnumerator &VE, |
| 777 | BitstreamWriter &Stream) { |
| 778 | if (VST.empty()) return; |
Chris Lattner | ff294a4 | 2007-05-05 01:26:50 +0000 | [diff] [blame] | 779 | Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4); |
Chris Lattner | 2e7899d | 2007-05-04 20:34:50 +0000 | [diff] [blame] | 780 | |
Chris Lattner | be1f993 | 2007-05-01 02:14:57 +0000 | [diff] [blame] | 781 | // FIXME: Set up the abbrev, we know how many values there are! |
| 782 | // FIXME: We know if the type names can use 7-bit ascii. |
| 783 | SmallVector<unsigned, 64> NameVals; |
| 784 | |
| 785 | for (ValueSymbolTable::const_iterator SI = VST.begin(), SE = VST.end(); |
| 786 | SI != SE; ++SI) { |
Chris Lattner | 5969830 | 2007-05-04 20:52:02 +0000 | [diff] [blame] | 787 | |
| 788 | const ValueName &Name = *SI; |
| 789 | |
| 790 | // Figure out the encoding to use for the name. |
| 791 | bool is7Bit = true; |
Chris Lattner | ff294a4 | 2007-05-05 01:26:50 +0000 | [diff] [blame] | 792 | bool isChar6 = true; |
| 793 | for (const char *C = Name.getKeyData(), *E = C+Name.getKeyLength(); |
| 794 | C != E; ++C) { |
| 795 | if (isChar6) |
| 796 | isChar6 = BitCodeAbbrevOp::isChar6(*C); |
| 797 | if ((unsigned char)*C & 128) { |
Chris Lattner | 5969830 | 2007-05-04 20:52:02 +0000 | [diff] [blame] | 798 | is7Bit = false; |
Chris Lattner | ff294a4 | 2007-05-05 01:26:50 +0000 | [diff] [blame] | 799 | break; // don't bother scanning the rest. |
Chris Lattner | 5969830 | 2007-05-04 20:52:02 +0000 | [diff] [blame] | 800 | } |
Chris Lattner | ff294a4 | 2007-05-05 01:26:50 +0000 | [diff] [blame] | 801 | } |
Chris Lattner | 5969830 | 2007-05-04 20:52:02 +0000 | [diff] [blame] | 802 | |
Chris Lattner | fd1ae95 | 2007-05-04 21:31:13 +0000 | [diff] [blame] | 803 | unsigned AbbrevToUse = VST_ENTRY_8_ABBREV; |
Chris Lattner | be1f993 | 2007-05-01 02:14:57 +0000 | [diff] [blame] | 804 | |
Chris Lattner | fd1ae95 | 2007-05-04 21:31:13 +0000 | [diff] [blame] | 805 | // VST_ENTRY: [valueid, namechar x N] |
| 806 | // VST_BBENTRY: [bbid, namechar x N] |
Chris Lattner | e825ed5 | 2007-05-03 22:18:21 +0000 | [diff] [blame] | 807 | unsigned Code; |
| 808 | if (isa<BasicBlock>(SI->getValue())) { |
| 809 | Code = bitc::VST_CODE_BBENTRY; |
Chris Lattner | ff294a4 | 2007-05-05 01:26:50 +0000 | [diff] [blame] | 810 | if (isChar6) |
| 811 | AbbrevToUse = VST_BBENTRY_6_ABBREV; |
Chris Lattner | e825ed5 | 2007-05-03 22:18:21 +0000 | [diff] [blame] | 812 | } else { |
| 813 | Code = bitc::VST_CODE_ENTRY; |
Chris Lattner | ff294a4 | 2007-05-05 01:26:50 +0000 | [diff] [blame] | 814 | if (isChar6) |
| 815 | AbbrevToUse = VST_ENTRY_6_ABBREV; |
| 816 | else if (is7Bit) |
| 817 | AbbrevToUse = VST_ENTRY_7_ABBREV; |
Chris Lattner | e825ed5 | 2007-05-03 22:18:21 +0000 | [diff] [blame] | 818 | } |
Chris Lattner | be1f993 | 2007-05-01 02:14:57 +0000 | [diff] [blame] | 819 | |
Chris Lattner | e825ed5 | 2007-05-03 22:18:21 +0000 | [diff] [blame] | 820 | NameVals.push_back(VE.getValueID(SI->getValue())); |
Chris Lattner | 5969830 | 2007-05-04 20:52:02 +0000 | [diff] [blame] | 821 | for (const char *P = Name.getKeyData(), |
| 822 | *E = Name.getKeyData()+Name.getKeyLength(); P != E; ++P) |
Chris Lattner | be1f993 | 2007-05-01 02:14:57 +0000 | [diff] [blame] | 823 | NameVals.push_back((unsigned char)*P); |
| 824 | |
| 825 | // Emit the finished record. |
Chris Lattner | e825ed5 | 2007-05-03 22:18:21 +0000 | [diff] [blame] | 826 | Stream.EmitRecord(Code, NameVals, AbbrevToUse); |
Chris Lattner | be1f993 | 2007-05-01 02:14:57 +0000 | [diff] [blame] | 827 | NameVals.clear(); |
| 828 | } |
| 829 | Stream.ExitBlock(); |
| 830 | } |
| 831 | |
Chris Lattner | 8d35c79 | 2007-04-26 03:50:57 +0000 | [diff] [blame] | 832 | /// WriteFunction - Emit a function body to the module stream. |
Chris Lattner | 198f34a | 2007-04-26 03:27:58 +0000 | [diff] [blame] | 833 | static void WriteFunction(const Function &F, ValueEnumerator &VE, |
| 834 | BitstreamWriter &Stream) { |
Chris Lattner | 01b2745 | 2007-04-29 05:49:09 +0000 | [diff] [blame] | 835 | Stream.EnterSubblock(bitc::FUNCTION_BLOCK_ID, 3); |
Chris Lattner | 8d35c79 | 2007-04-26 03:50:57 +0000 | [diff] [blame] | 836 | VE.incorporateFunction(F); |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 837 | |
| 838 | SmallVector<unsigned, 64> Vals; |
| 839 | |
| 840 | // Emit the number of basic blocks, so the reader can create them ahead of |
| 841 | // time. |
| 842 | Vals.push_back(VE.getBasicBlocks().size()); |
| 843 | Stream.EmitRecord(bitc::FUNC_CODE_DECLAREBLOCKS, Vals); |
| 844 | Vals.clear(); |
| 845 | |
| 846 | // FIXME: Function attributes? |
| 847 | |
| 848 | // If there are function-local constants, emit them now. |
| 849 | unsigned CstStart, CstEnd; |
| 850 | VE.getFunctionConstantRange(CstStart, CstEnd); |
Chris Lattner | a0f1ecc | 2007-05-05 07:36:14 +0000 | [diff] [blame] | 851 | WriteConstants(CstStart, CstEnd, VE, Stream, false); |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 852 | |
| 853 | // Finally, emit all the instructions, in order. |
| 854 | for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) |
| 855 | for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) |
| 856 | WriteInstruction(*I, VE, Stream, Vals); |
Chris Lattner | 198f34a | 2007-04-26 03:27:58 +0000 | [diff] [blame] | 857 | |
Chris Lattner | be1f993 | 2007-05-01 02:14:57 +0000 | [diff] [blame] | 858 | // Emit names for all the instructions etc. |
| 859 | WriteValueSymbolTable(F.getValueSymbolTable(), VE, Stream); |
| 860 | |
Chris Lattner | 8d35c79 | 2007-04-26 03:50:57 +0000 | [diff] [blame] | 861 | VE.purgeFunction(); |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 862 | Stream.ExitBlock(); |
Chris Lattner | 198f34a | 2007-04-26 03:27:58 +0000 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | /// WriteTypeSymbolTable - Emit a block for the specified type symtab. |
| 866 | static void WriteTypeSymbolTable(const TypeSymbolTable &TST, |
| 867 | const ValueEnumerator &VE, |
| 868 | BitstreamWriter &Stream) { |
| 869 | if (TST.empty()) return; |
| 870 | |
| 871 | Stream.EnterSubblock(bitc::TYPE_SYMTAB_BLOCK_ID, 3); |
| 872 | |
Chris Lattner | 7a263ea | 2007-05-05 00:47:19 +0000 | [diff] [blame] | 873 | // 7-bit fixed width VST_CODE_ENTRY strings. |
| 874 | BitCodeAbbrev *Abbv = new BitCodeAbbrev(); |
| 875 | Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY)); |
| 876 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, |
| 877 | Log2_32_Ceil(VE.getTypes().size()+1))); |
| 878 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); |
| 879 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); |
| 880 | unsigned V7Abbrev = Stream.EmitAbbrev(Abbv); |
Chris Lattner | 198f34a | 2007-04-26 03:27:58 +0000 | [diff] [blame] | 881 | |
| 882 | SmallVector<unsigned, 64> NameVals; |
| 883 | |
| 884 | for (TypeSymbolTable::const_iterator TI = TST.begin(), TE = TST.end(); |
| 885 | TI != TE; ++TI) { |
Chris Lattner | 7a263ea | 2007-05-05 00:47:19 +0000 | [diff] [blame] | 886 | // TST_ENTRY: [typeid, namechar x N] |
Chris Lattner | 198f34a | 2007-04-26 03:27:58 +0000 | [diff] [blame] | 887 | NameVals.push_back(VE.getTypeID(TI->second)); |
| 888 | |
| 889 | const std::string &Str = TI->first; |
Chris Lattner | 7a263ea | 2007-05-05 00:47:19 +0000 | [diff] [blame] | 890 | bool is7Bit = true; |
| 891 | for (unsigned i = 0, e = Str.size(); i != e; ++i) { |
| 892 | NameVals.push_back((unsigned char)Str[i]); |
| 893 | if (Str[i] & 128) |
| 894 | is7Bit = false; |
| 895 | } |
Chris Lattner | 198f34a | 2007-04-26 03:27:58 +0000 | [diff] [blame] | 896 | |
| 897 | // Emit the finished record. |
Chris Lattner | 7a263ea | 2007-05-05 00:47:19 +0000 | [diff] [blame] | 898 | Stream.EmitRecord(bitc::VST_CODE_ENTRY, NameVals, is7Bit ? V7Abbrev : 0); |
Chris Lattner | 198f34a | 2007-04-26 03:27:58 +0000 | [diff] [blame] | 899 | NameVals.clear(); |
| 900 | } |
| 901 | |
| 902 | Stream.ExitBlock(); |
| 903 | } |
| 904 | |
Chris Lattner | 07faafc | 2007-05-04 18:26:27 +0000 | [diff] [blame] | 905 | // Emit blockinfo, which defines the standard abbreviations etc. |
Chris Lattner | a0f1ecc | 2007-05-05 07:36:14 +0000 | [diff] [blame] | 906 | static void WriteBlockInfo(const ValueEnumerator &VE, BitstreamWriter &Stream) { |
Chris Lattner | 07faafc | 2007-05-04 18:26:27 +0000 | [diff] [blame] | 907 | // We only want to emit block info records for blocks that have multiple |
| 908 | // instances: CONSTANTS_BLOCK, FUNCTION_BLOCK and VALUE_SYMTAB_BLOCK. Other |
| 909 | // blocks can defined their abbrevs inline. |
Chris Lattner | e17b658 | 2007-05-05 00:17:00 +0000 | [diff] [blame] | 910 | Stream.EnterBlockInfoBlock(2); |
Chris Lattner | 07faafc | 2007-05-04 18:26:27 +0000 | [diff] [blame] | 911 | |
Chris Lattner | e17b658 | 2007-05-05 00:17:00 +0000 | [diff] [blame] | 912 | { // 8-bit fixed-width VST_ENTRY/VST_BBENTRY strings. |
| 913 | BitCodeAbbrev *Abbv = new BitCodeAbbrev(); |
| 914 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); |
| 915 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); |
| 916 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); |
| 917 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); |
| 918 | if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, |
| 919 | Abbv) != VST_ENTRY_8_ABBREV) |
| 920 | assert(0 && "Unexpected abbrev ordering!"); |
| 921 | } |
| 922 | |
| 923 | { // 7-bit fixed width VST_ENTRY strings. |
| 924 | BitCodeAbbrev *Abbv = new BitCodeAbbrev(); |
| 925 | Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY)); |
| 926 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); |
| 927 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); |
| 928 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); |
| 929 | if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, |
| 930 | Abbv) != VST_ENTRY_7_ABBREV) |
| 931 | assert(0 && "Unexpected abbrev ordering!"); |
| 932 | } |
Chris Lattner | ff294a4 | 2007-05-05 01:26:50 +0000 | [diff] [blame] | 933 | { // 6-bit char6 VST_ENTRY strings. |
| 934 | BitCodeAbbrev *Abbv = new BitCodeAbbrev(); |
| 935 | Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY)); |
| 936 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); |
| 937 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); |
| 938 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6)); |
| 939 | if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, |
| 940 | Abbv) != VST_ENTRY_6_ABBREV) |
| 941 | assert(0 && "Unexpected abbrev ordering!"); |
| 942 | } |
| 943 | { // 6-bit char6 VST_BBENTRY strings. |
Chris Lattner | e17b658 | 2007-05-05 00:17:00 +0000 | [diff] [blame] | 944 | BitCodeAbbrev *Abbv = new BitCodeAbbrev(); |
| 945 | Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_BBENTRY)); |
| 946 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); |
| 947 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); |
Chris Lattner | ff294a4 | 2007-05-05 01:26:50 +0000 | [diff] [blame] | 948 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6)); |
Chris Lattner | e17b658 | 2007-05-05 00:17:00 +0000 | [diff] [blame] | 949 | if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, |
Chris Lattner | ff294a4 | 2007-05-05 01:26:50 +0000 | [diff] [blame] | 950 | Abbv) != VST_BBENTRY_6_ABBREV) |
Chris Lattner | e17b658 | 2007-05-05 00:17:00 +0000 | [diff] [blame] | 951 | assert(0 && "Unexpected abbrev ordering!"); |
| 952 | } |
| 953 | |
Chris Lattner | 440168b | 2007-05-05 07:44:49 +0000 | [diff] [blame] | 954 | |
| 955 | |
Chris Lattner | a0f1ecc | 2007-05-05 07:36:14 +0000 | [diff] [blame] | 956 | { // SETTYPE abbrev for CONSTANTS_BLOCK. |
| 957 | BitCodeAbbrev *Abbv = new BitCodeAbbrev(); |
| 958 | Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_SETTYPE)); |
| 959 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, |
| 960 | Log2_32_Ceil(VE.getTypes().size()+1))); |
| 961 | if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, |
| 962 | Abbv) != CONSTANTS_SETTYPE_ABBREV) |
| 963 | assert(0 && "Unexpected abbrev ordering!"); |
| 964 | } |
| 965 | |
| 966 | { // INTEGER abbrev for CONSTANTS_BLOCK. |
| 967 | BitCodeAbbrev *Abbv = new BitCodeAbbrev(); |
| 968 | Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_INTEGER)); |
| 969 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); |
| 970 | if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, |
| 971 | Abbv) != CONSTANTS_INTEGER_ABBREV) |
| 972 | assert(0 && "Unexpected abbrev ordering!"); |
| 973 | } |
| 974 | |
| 975 | { // CE_CAST abbrev for CONSTANTS_BLOCK. |
| 976 | BitCodeAbbrev *Abbv = new BitCodeAbbrev(); |
| 977 | Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CE_CAST)); |
| 978 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // cast opc |
| 979 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // typeid |
| 980 | Log2_32_Ceil(VE.getTypes().size()+1))); |
| 981 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id |
| 982 | |
| 983 | if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, |
| 984 | Abbv) != CONSTANTS_CE_CAST_Abbrev) |
| 985 | assert(0 && "Unexpected abbrev ordering!"); |
| 986 | } |
| 987 | { // NULL abbrev for CONSTANTS_BLOCK. |
| 988 | BitCodeAbbrev *Abbv = new BitCodeAbbrev(); |
| 989 | Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_NULL)); |
| 990 | if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, |
| 991 | Abbv) != CONSTANTS_NULL_Abbrev) |
| 992 | assert(0 && "Unexpected abbrev ordering!"); |
| 993 | } |
| 994 | |
Chris Lattner | 440168b | 2007-05-05 07:44:49 +0000 | [diff] [blame] | 995 | // FIXME: This should only use space for first class types! |
| 996 | |
| 997 | { // INST_LOAD abbrev for FUNCTION_BLOCK. |
| 998 | BitCodeAbbrev *Abbv = new BitCodeAbbrev(); |
| 999 | Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_LOAD)); |
| 1000 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // typeid |
| 1001 | Log2_32_Ceil(VE.getTypes().size()+1))); |
| 1002 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Ptr |
| 1003 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // Align |
| 1004 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // volatile |
| 1005 | if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, |
| 1006 | Abbv) != FUNCTION_INST_LOAD_ABBREV) |
| 1007 | assert(0 && "Unexpected abbrev ordering!"); |
| 1008 | } |
| 1009 | |
Chris Lattner | a0f1ecc | 2007-05-05 07:36:14 +0000 | [diff] [blame] | 1010 | Stream.ExitBlock(); |
| 1011 | } |
| 1012 | |
| 1013 | |
| 1014 | /// WriteModule - Emit the specified module to the bitstream. |
| 1015 | static void WriteModule(const Module *M, BitstreamWriter &Stream) { |
| 1016 | Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3); |
| 1017 | |
| 1018 | // Emit the version number if it is non-zero. |
| 1019 | if (CurVersion) { |
| 1020 | SmallVector<unsigned, 1> Vals; |
| 1021 | Vals.push_back(CurVersion); |
| 1022 | Stream.EmitRecord(bitc::MODULE_CODE_VERSION, Vals); |
| 1023 | } |
| 1024 | |
| 1025 | // Analyze the module, enumerating globals, functions, etc. |
| 1026 | ValueEnumerator VE(M); |
| 1027 | |
| 1028 | // Emit blockinfo, which defines the standard abbreviations etc. |
| 1029 | WriteBlockInfo(VE, Stream); |
| 1030 | |
| 1031 | // Emit information about parameter attributes. |
| 1032 | WriteParamAttrTable(VE, Stream); |
| 1033 | |
| 1034 | // Emit information describing all of the types in the module. |
| 1035 | WriteTypeTable(VE, Stream); |
| 1036 | |
| 1037 | // Emit top-level description of module, including target triple, inline asm, |
| 1038 | // descriptors for global variables, and function prototype info. |
| 1039 | WriteModuleInfo(M, VE, Stream); |
| 1040 | |
| 1041 | // Emit constants. |
| 1042 | WriteModuleConstants(VE, Stream); |
| 1043 | |
| 1044 | // If we have any aggregate values in the value table, purge them - these can |
| 1045 | // only be used to initialize global variables. Doing so makes the value |
| 1046 | // namespace smaller for code in functions. |
| 1047 | int NumNonAggregates = VE.PurgeAggregateValues(); |
| 1048 | if (NumNonAggregates != -1) { |
| 1049 | SmallVector<unsigned, 1> Vals; |
| 1050 | Vals.push_back(NumNonAggregates); |
| 1051 | Stream.EmitRecord(bitc::MODULE_CODE_PURGEVALS, Vals); |
| 1052 | } |
| 1053 | |
| 1054 | // Emit function bodies. |
| 1055 | for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 1056 | if (!I->isDeclaration()) |
| 1057 | WriteFunction(*I, VE, Stream); |
| 1058 | |
| 1059 | // Emit the type symbol table information. |
| 1060 | WriteTypeSymbolTable(M->getTypeSymbolTable(), VE, Stream); |
| 1061 | |
| 1062 | // Emit names for globals/functions etc. |
| 1063 | WriteValueSymbolTable(M->getValueSymbolTable(), VE, Stream); |
| 1064 | |
Chris Lattner | 07faafc | 2007-05-04 18:26:27 +0000 | [diff] [blame] | 1065 | Stream.ExitBlock(); |
| 1066 | } |
| 1067 | |
| 1068 | |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 1069 | /// WriteBitcodeToFile - Write the specified module to the specified output |
| 1070 | /// stream. |
| 1071 | void llvm::WriteBitcodeToFile(const Module *M, std::ostream &Out) { |
| 1072 | std::vector<unsigned char> Buffer; |
| 1073 | BitstreamWriter Stream(Buffer); |
| 1074 | |
| 1075 | Buffer.reserve(256*1024); |
| 1076 | |
| 1077 | // Emit the file header. |
| 1078 | Stream.Emit((unsigned)'B', 8); |
| 1079 | Stream.Emit((unsigned)'C', 8); |
| 1080 | Stream.Emit(0x0, 4); |
| 1081 | Stream.Emit(0xC, 4); |
| 1082 | Stream.Emit(0xE, 4); |
| 1083 | Stream.Emit(0xD, 4); |
| 1084 | |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 1085 | // Emit the module. |
| 1086 | WriteModule(M, Stream); |
| 1087 | |
| 1088 | // Write the generated bitstream to "Out". |
| 1089 | Out.write((char*)&Buffer.front(), Buffer.size()); |
| 1090 | } |