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. |
Chris Lattner | 94687ac | 2007-05-06 01:28:01 +0000 | [diff] [blame^] | 46 | FUNCTION_INST_LOAD_ABBREV = bitc::FIRST_APPLICATION_ABBREV, |
| 47 | FUNCTION_INST_RET_VOID_ABBREV, |
| 48 | FUNCTION_INST_RET_VAL_ABBREV, |
| 49 | FUNCTION_INST_UNREACHABLE_ABBREV |
Chris Lattner | 5969830 | 2007-05-04 20:52:02 +0000 | [diff] [blame] | 50 | }; |
| 51 | |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 52 | |
Chris Lattner | f581c3b | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 53 | static unsigned GetEncodedCastOpcode(unsigned Opcode) { |
| 54 | switch (Opcode) { |
| 55 | default: assert(0 && "Unknown cast instruction!"); |
| 56 | case Instruction::Trunc : return bitc::CAST_TRUNC; |
| 57 | case Instruction::ZExt : return bitc::CAST_ZEXT; |
| 58 | case Instruction::SExt : return bitc::CAST_SEXT; |
| 59 | case Instruction::FPToUI : return bitc::CAST_FPTOUI; |
| 60 | case Instruction::FPToSI : return bitc::CAST_FPTOSI; |
| 61 | case Instruction::UIToFP : return bitc::CAST_UITOFP; |
| 62 | case Instruction::SIToFP : return bitc::CAST_SITOFP; |
| 63 | case Instruction::FPTrunc : return bitc::CAST_FPTRUNC; |
| 64 | case Instruction::FPExt : return bitc::CAST_FPEXT; |
| 65 | case Instruction::PtrToInt: return bitc::CAST_PTRTOINT; |
| 66 | case Instruction::IntToPtr: return bitc::CAST_INTTOPTR; |
| 67 | case Instruction::BitCast : return bitc::CAST_BITCAST; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | static unsigned GetEncodedBinaryOpcode(unsigned Opcode) { |
| 72 | switch (Opcode) { |
| 73 | default: assert(0 && "Unknown binary instruction!"); |
| 74 | case Instruction::Add: return bitc::BINOP_ADD; |
| 75 | case Instruction::Sub: return bitc::BINOP_SUB; |
| 76 | case Instruction::Mul: return bitc::BINOP_MUL; |
| 77 | case Instruction::UDiv: return bitc::BINOP_UDIV; |
| 78 | case Instruction::FDiv: |
| 79 | case Instruction::SDiv: return bitc::BINOP_SDIV; |
| 80 | case Instruction::URem: return bitc::BINOP_UREM; |
| 81 | case Instruction::FRem: |
| 82 | case Instruction::SRem: return bitc::BINOP_SREM; |
| 83 | case Instruction::Shl: return bitc::BINOP_SHL; |
| 84 | case Instruction::LShr: return bitc::BINOP_LSHR; |
| 85 | case Instruction::AShr: return bitc::BINOP_ASHR; |
| 86 | case Instruction::And: return bitc::BINOP_AND; |
| 87 | case Instruction::Or: return bitc::BINOP_OR; |
| 88 | case Instruction::Xor: return bitc::BINOP_XOR; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | |
| 93 | |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 94 | static void WriteStringRecord(unsigned Code, const std::string &Str, |
| 95 | unsigned AbbrevToUse, BitstreamWriter &Stream) { |
| 96 | SmallVector<unsigned, 64> Vals; |
| 97 | |
Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 98 | // Code: [strchar x N] |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 99 | for (unsigned i = 0, e = Str.size(); i != e; ++i) |
| 100 | Vals.push_back(Str[i]); |
| 101 | |
| 102 | // Emit the finished record. |
| 103 | Stream.EmitRecord(Code, Vals, AbbrevToUse); |
| 104 | } |
| 105 | |
Chris Lattner | 62bbeea | 2007-05-04 00:44:52 +0000 | [diff] [blame] | 106 | // Emit information about parameter attributes. |
| 107 | static void WriteParamAttrTable(const ValueEnumerator &VE, |
| 108 | BitstreamWriter &Stream) { |
| 109 | const std::vector<const ParamAttrsList*> &Attrs = VE.getParamAttrs(); |
| 110 | if (Attrs.empty()) return; |
| 111 | |
Chris Lattner | f0a6531 | 2007-05-04 02:59:04 +0000 | [diff] [blame] | 112 | Stream.EnterSubblock(bitc::PARAMATTR_BLOCK_ID, 3); |
| 113 | |
| 114 | SmallVector<uint64_t, 64> Record; |
| 115 | for (unsigned i = 0, e = Attrs.size(); i != e; ++i) { |
| 116 | const ParamAttrsList *A = Attrs[i]; |
| 117 | for (unsigned op = 0, e = A->size(); op != e; ++op) { |
| 118 | Record.push_back(A->getParamIndex(op)); |
Chris Lattner | f6f9cd1 | 2007-05-04 03:14:09 +0000 | [diff] [blame] | 119 | Record.push_back(A->getParamAttrsAtIndex(op)); |
Chris Lattner | f0a6531 | 2007-05-04 02:59:04 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | Stream.EmitRecord(bitc::PARAMATTR_CODE_ENTRY, Record); |
| 123 | Record.clear(); |
| 124 | } |
Chris Lattner | 62bbeea | 2007-05-04 00:44:52 +0000 | [diff] [blame] | 125 | |
Chris Lattner | f0a6531 | 2007-05-04 02:59:04 +0000 | [diff] [blame] | 126 | Stream.ExitBlock(); |
Chris Lattner | 62bbeea | 2007-05-04 00:44:52 +0000 | [diff] [blame] | 127 | } |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 128 | |
| 129 | /// WriteTypeTable - Write out the type table for a module. |
| 130 | static void WriteTypeTable(const ValueEnumerator &VE, BitstreamWriter &Stream) { |
| 131 | const ValueEnumerator::TypeList &TypeList = VE.getTypes(); |
| 132 | |
| 133 | Stream.EnterSubblock(bitc::TYPE_BLOCK_ID, 4 /*count from # abbrevs */); |
| 134 | SmallVector<uint64_t, 64> TypeVals; |
| 135 | |
Chris Lattner | d092e0e | 2007-05-05 06:30:12 +0000 | [diff] [blame] | 136 | // Abbrev for TYPE_CODE_POINTER. |
| 137 | BitCodeAbbrev *Abbv = new BitCodeAbbrev(); |
| 138 | Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_POINTER)); |
| 139 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, |
| 140 | Log2_32_Ceil(VE.getTypes().size()+1))); |
| 141 | unsigned PtrAbbrev = Stream.EmitAbbrev(Abbv); |
| 142 | |
| 143 | // Abbrev for TYPE_CODE_FUNCTION. |
| 144 | Abbv = new BitCodeAbbrev(); |
| 145 | Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_FUNCTION)); |
| 146 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isvararg |
| 147 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, |
| 148 | Log2_32_Ceil(VE.getParamAttrs().size()+1))); |
| 149 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); |
| 150 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, |
| 151 | Log2_32_Ceil(VE.getTypes().size()+1))); |
| 152 | unsigned FunctionAbbrev = Stream.EmitAbbrev(Abbv); |
| 153 | |
| 154 | // Abbrev for TYPE_CODE_STRUCT. |
| 155 | Abbv = new BitCodeAbbrev(); |
| 156 | Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT)); |
| 157 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked |
| 158 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); |
| 159 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, |
| 160 | Log2_32_Ceil(VE.getTypes().size()+1))); |
| 161 | unsigned StructAbbrev = Stream.EmitAbbrev(Abbv); |
| 162 | |
| 163 | // Abbrev for TYPE_CODE_ARRAY. |
| 164 | Abbv = new BitCodeAbbrev(); |
| 165 | Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_ARRAY)); |
| 166 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // size |
| 167 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, |
| 168 | Log2_32_Ceil(VE.getTypes().size()+1))); |
| 169 | unsigned ArrayAbbrev = Stream.EmitAbbrev(Abbv); |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 170 | |
| 171 | // Emit an entry count so the reader can reserve space. |
| 172 | TypeVals.push_back(TypeList.size()); |
| 173 | Stream.EmitRecord(bitc::TYPE_CODE_NUMENTRY, TypeVals); |
| 174 | TypeVals.clear(); |
| 175 | |
| 176 | // Loop over all of the types, emitting each in turn. |
| 177 | for (unsigned i = 0, e = TypeList.size(); i != e; ++i) { |
| 178 | const Type *T = TypeList[i].first; |
| 179 | int AbbrevToUse = 0; |
| 180 | unsigned Code = 0; |
| 181 | |
| 182 | switch (T->getTypeID()) { |
| 183 | case Type::PackedStructTyID: // FIXME: Delete Type::PackedStructTyID. |
| 184 | default: assert(0 && "Unknown type!"); |
| 185 | case Type::VoidTyID: Code = bitc::TYPE_CODE_VOID; break; |
| 186 | case Type::FloatTyID: Code = bitc::TYPE_CODE_FLOAT; break; |
| 187 | case Type::DoubleTyID: Code = bitc::TYPE_CODE_DOUBLE; break; |
| 188 | case Type::LabelTyID: Code = bitc::TYPE_CODE_LABEL; break; |
| 189 | case Type::OpaqueTyID: Code = bitc::TYPE_CODE_OPAQUE; break; |
| 190 | case Type::IntegerTyID: |
| 191 | // INTEGER: [width] |
| 192 | Code = bitc::TYPE_CODE_INTEGER; |
| 193 | TypeVals.push_back(cast<IntegerType>(T)->getBitWidth()); |
| 194 | break; |
| 195 | case Type::PointerTyID: |
| 196 | // POINTER: [pointee type] |
| 197 | Code = bitc::TYPE_CODE_POINTER; |
| 198 | TypeVals.push_back(VE.getTypeID(cast<PointerType>(T)->getElementType())); |
Chris Lattner | d092e0e | 2007-05-05 06:30:12 +0000 | [diff] [blame] | 199 | AbbrevToUse = PtrAbbrev; |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 200 | break; |
| 201 | |
| 202 | case Type::FunctionTyID: { |
| 203 | const FunctionType *FT = cast<FunctionType>(T); |
Chris Lattner | d092e0e | 2007-05-05 06:30:12 +0000 | [diff] [blame] | 204 | // FUNCTION: [isvararg, attrid, retty, paramty x N] |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 205 | Code = bitc::TYPE_CODE_FUNCTION; |
| 206 | TypeVals.push_back(FT->isVarArg()); |
Chris Lattner | 9113e73 | 2007-05-04 03:41:34 +0000 | [diff] [blame] | 207 | TypeVals.push_back(VE.getParamAttrID(FT->getParamAttrs())); |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 208 | TypeVals.push_back(VE.getTypeID(FT->getReturnType())); |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 209 | for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) |
| 210 | TypeVals.push_back(VE.getTypeID(FT->getParamType(i))); |
Chris Lattner | d092e0e | 2007-05-05 06:30:12 +0000 | [diff] [blame] | 211 | AbbrevToUse = FunctionAbbrev; |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 212 | break; |
| 213 | } |
| 214 | case Type::StructTyID: { |
| 215 | const StructType *ST = cast<StructType>(T); |
Chris Lattner | d092e0e | 2007-05-05 06:30:12 +0000 | [diff] [blame] | 216 | // STRUCT: [ispacked, eltty x N] |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 217 | Code = bitc::TYPE_CODE_STRUCT; |
| 218 | TypeVals.push_back(ST->isPacked()); |
Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 219 | // Output all of the element types. |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 220 | for (StructType::element_iterator I = ST->element_begin(), |
| 221 | E = ST->element_end(); I != E; ++I) |
| 222 | TypeVals.push_back(VE.getTypeID(*I)); |
Chris Lattner | d092e0e | 2007-05-05 06:30:12 +0000 | [diff] [blame] | 223 | AbbrevToUse = StructAbbrev; |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 224 | break; |
| 225 | } |
| 226 | case Type::ArrayTyID: { |
| 227 | const ArrayType *AT = cast<ArrayType>(T); |
| 228 | // ARRAY: [numelts, eltty] |
| 229 | Code = bitc::TYPE_CODE_ARRAY; |
| 230 | TypeVals.push_back(AT->getNumElements()); |
| 231 | TypeVals.push_back(VE.getTypeID(AT->getElementType())); |
Chris Lattner | d092e0e | 2007-05-05 06:30:12 +0000 | [diff] [blame] | 232 | AbbrevToUse = ArrayAbbrev; |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 233 | break; |
| 234 | } |
| 235 | case Type::VectorTyID: { |
| 236 | const VectorType *VT = cast<VectorType>(T); |
| 237 | // VECTOR [numelts, eltty] |
| 238 | Code = bitc::TYPE_CODE_VECTOR; |
| 239 | TypeVals.push_back(VT->getNumElements()); |
| 240 | TypeVals.push_back(VE.getTypeID(VT->getElementType())); |
| 241 | break; |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | // Emit the finished record. |
| 246 | Stream.EmitRecord(Code, TypeVals, AbbrevToUse); |
| 247 | TypeVals.clear(); |
| 248 | } |
| 249 | |
| 250 | Stream.ExitBlock(); |
| 251 | } |
| 252 | |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 253 | static unsigned getEncodedLinkage(const GlobalValue *GV) { |
| 254 | switch (GV->getLinkage()) { |
| 255 | default: assert(0 && "Invalid linkage!"); |
| 256 | case GlobalValue::ExternalLinkage: return 0; |
| 257 | case GlobalValue::WeakLinkage: return 1; |
| 258 | case GlobalValue::AppendingLinkage: return 2; |
| 259 | case GlobalValue::InternalLinkage: return 3; |
| 260 | case GlobalValue::LinkOnceLinkage: return 4; |
| 261 | case GlobalValue::DLLImportLinkage: return 5; |
| 262 | case GlobalValue::DLLExportLinkage: return 6; |
| 263 | case GlobalValue::ExternalWeakLinkage: return 7; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | static unsigned getEncodedVisibility(const GlobalValue *GV) { |
| 268 | switch (GV->getVisibility()) { |
| 269 | default: assert(0 && "Invalid visibility!"); |
Anton Korobeynikov | 9cd3ccf | 2007-04-29 20:56:48 +0000 | [diff] [blame] | 270 | case GlobalValue::DefaultVisibility: return 0; |
| 271 | case GlobalValue::HiddenVisibility: return 1; |
| 272 | case GlobalValue::ProtectedVisibility: return 2; |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | |
| 276 | // Emit top-level description of module, including target triple, inline asm, |
| 277 | // descriptors for global variables, and function prototype info. |
| 278 | static void WriteModuleInfo(const Module *M, const ValueEnumerator &VE, |
| 279 | BitstreamWriter &Stream) { |
| 280 | // Emit the list of dependent libraries for the Module. |
| 281 | for (Module::lib_iterator I = M->lib_begin(), E = M->lib_end(); I != E; ++I) |
| 282 | WriteStringRecord(bitc::MODULE_CODE_DEPLIB, *I, 0/*TODO*/, Stream); |
| 283 | |
| 284 | // Emit various pieces of data attached to a module. |
| 285 | if (!M->getTargetTriple().empty()) |
| 286 | WriteStringRecord(bitc::MODULE_CODE_TRIPLE, M->getTargetTriple(), |
| 287 | 0/*TODO*/, Stream); |
| 288 | if (!M->getDataLayout().empty()) |
| 289 | WriteStringRecord(bitc::MODULE_CODE_DATALAYOUT, M->getDataLayout(), |
| 290 | 0/*TODO*/, Stream); |
| 291 | if (!M->getModuleInlineAsm().empty()) |
| 292 | WriteStringRecord(bitc::MODULE_CODE_ASM, M->getModuleInlineAsm(), |
| 293 | 0/*TODO*/, Stream); |
| 294 | |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 295 | // Emit information about sections, computing how many there are. Also |
| 296 | // compute the maximum alignment value. |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 297 | std::map<std::string, unsigned> SectionMap; |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 298 | unsigned MaxAlignment = 0; |
Chris Lattner | d127c1b | 2007-04-23 18:58:34 +0000 | [diff] [blame] | 299 | unsigned MaxGlobalType = 0; |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 300 | for (Module::const_global_iterator GV = M->global_begin(),E = M->global_end(); |
| 301 | GV != E; ++GV) { |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 302 | MaxAlignment = std::max(MaxAlignment, GV->getAlignment()); |
Chris Lattner | d127c1b | 2007-04-23 18:58:34 +0000 | [diff] [blame] | 303 | MaxGlobalType = std::max(MaxGlobalType, VE.getTypeID(GV->getType())); |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 304 | |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 305 | if (!GV->hasSection()) continue; |
| 306 | // Give section names unique ID's. |
| 307 | unsigned &Entry = SectionMap[GV->getSection()]; |
| 308 | if (Entry != 0) continue; |
| 309 | WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, GV->getSection(), |
| 310 | 0/*TODO*/, Stream); |
| 311 | Entry = SectionMap.size(); |
| 312 | } |
| 313 | 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] | 314 | MaxAlignment = std::max(MaxAlignment, F->getAlignment()); |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 315 | if (!F->hasSection()) continue; |
| 316 | // Give section names unique ID's. |
| 317 | unsigned &Entry = SectionMap[F->getSection()]; |
| 318 | if (Entry != 0) continue; |
| 319 | WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, F->getSection(), |
| 320 | 0/*TODO*/, Stream); |
| 321 | Entry = SectionMap.size(); |
| 322 | } |
| 323 | |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 324 | // Emit abbrev for globals, now that we know # sections and max alignment. |
| 325 | unsigned SimpleGVarAbbrev = 0; |
Chris Lattner | d127c1b | 2007-04-23 18:58:34 +0000 | [diff] [blame] | 326 | if (!M->global_empty()) { |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 327 | // Add an abbrev for common globals with no visibility or thread localness. |
| 328 | BitCodeAbbrev *Abbv = new BitCodeAbbrev(); |
| 329 | Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_GLOBALVAR)); |
Chris Lattner | 2e7899d | 2007-05-04 20:34:50 +0000 | [diff] [blame] | 330 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, |
Chris Lattner | d127c1b | 2007-04-23 18:58:34 +0000 | [diff] [blame] | 331 | Log2_32_Ceil(MaxGlobalType+1))); |
Chris Lattner | 2e7899d | 2007-05-04 20:34:50 +0000 | [diff] [blame] | 332 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Constant. |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 333 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Initializer. |
Chris Lattner | 2e7899d | 2007-05-04 20:34:50 +0000 | [diff] [blame] | 334 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Linkage. |
| 335 | if (MaxAlignment == 0) // Alignment. |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 336 | Abbv->Add(BitCodeAbbrevOp(0)); |
| 337 | else { |
| 338 | unsigned MaxEncAlignment = Log2_32(MaxAlignment)+1; |
Chris Lattner | 2e7899d | 2007-05-04 20:34:50 +0000 | [diff] [blame] | 339 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, |
Chris Lattner | d127c1b | 2007-04-23 18:58:34 +0000 | [diff] [blame] | 340 | Log2_32_Ceil(MaxEncAlignment+1))); |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 341 | } |
| 342 | if (SectionMap.empty()) // Section. |
| 343 | Abbv->Add(BitCodeAbbrevOp(0)); |
| 344 | else |
Chris Lattner | 2e7899d | 2007-05-04 20:34:50 +0000 | [diff] [blame] | 345 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, |
Chris Lattner | 631a8ed | 2007-04-24 03:29:47 +0000 | [diff] [blame] | 346 | Log2_32_Ceil(SectionMap.size()+1))); |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 347 | // Don't bother emitting vis + thread local. |
| 348 | SimpleGVarAbbrev = Stream.EmitAbbrev(Abbv); |
| 349 | } |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 350 | |
| 351 | // Emit the global variable information. |
| 352 | SmallVector<unsigned, 64> Vals; |
| 353 | for (Module::const_global_iterator GV = M->global_begin(),E = M->global_end(); |
| 354 | GV != E; ++GV) { |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 355 | unsigned AbbrevToUse = 0; |
| 356 | |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 357 | // GLOBALVAR: [type, isconst, initid, |
| 358 | // linkage, alignment, section, visibility, threadlocal] |
| 359 | Vals.push_back(VE.getTypeID(GV->getType())); |
| 360 | Vals.push_back(GV->isConstant()); |
| 361 | Vals.push_back(GV->isDeclaration() ? 0 : |
| 362 | (VE.getValueID(GV->getInitializer()) + 1)); |
| 363 | Vals.push_back(getEncodedLinkage(GV)); |
| 364 | Vals.push_back(Log2_32(GV->getAlignment())+1); |
| 365 | Vals.push_back(GV->hasSection() ? SectionMap[GV->getSection()] : 0); |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 366 | if (GV->isThreadLocal() || |
| 367 | GV->getVisibility() != GlobalValue::DefaultVisibility) { |
| 368 | Vals.push_back(getEncodedVisibility(GV)); |
| 369 | Vals.push_back(GV->isThreadLocal()); |
| 370 | } else { |
| 371 | AbbrevToUse = SimpleGVarAbbrev; |
| 372 | } |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 373 | |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 374 | Stream.EmitRecord(bitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse); |
| 375 | Vals.clear(); |
| 376 | } |
| 377 | |
| 378 | // Emit the function proto information. |
| 379 | for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) { |
| 380 | // FUNCTION: [type, callingconv, isproto, linkage, alignment, section, |
| 381 | // visibility] |
| 382 | Vals.push_back(VE.getTypeID(F->getType())); |
| 383 | Vals.push_back(F->getCallingConv()); |
| 384 | Vals.push_back(F->isDeclaration()); |
| 385 | Vals.push_back(getEncodedLinkage(F)); |
| 386 | Vals.push_back(Log2_32(F->getAlignment())+1); |
| 387 | Vals.push_back(F->hasSection() ? SectionMap[F->getSection()] : 0); |
| 388 | Vals.push_back(getEncodedVisibility(F)); |
| 389 | |
| 390 | unsigned AbbrevToUse = 0; |
| 391 | Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse); |
| 392 | Vals.clear(); |
| 393 | } |
Chris Lattner | 07d98b4 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 394 | |
| 395 | |
| 396 | // Emit the alias information. |
| 397 | for (Module::const_alias_iterator AI = M->alias_begin(), E = M->alias_end(); |
| 398 | AI != E; ++AI) { |
| 399 | Vals.push_back(VE.getTypeID(AI->getType())); |
| 400 | Vals.push_back(VE.getValueID(AI->getAliasee())); |
| 401 | Vals.push_back(getEncodedLinkage(AI)); |
| 402 | unsigned AbbrevToUse = 0; |
| 403 | Stream.EmitRecord(bitc::MODULE_CODE_ALIAS, Vals, AbbrevToUse); |
| 404 | Vals.clear(); |
| 405 | } |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | |
Chris Lattner | 2edd22b | 2007-04-24 00:16:04 +0000 | [diff] [blame] | 409 | static void WriteConstants(unsigned FirstVal, unsigned LastVal, |
| 410 | const ValueEnumerator &VE, |
Chris Lattner | a0f1ecc | 2007-05-05 07:36:14 +0000 | [diff] [blame] | 411 | BitstreamWriter &Stream, bool isGlobal) { |
Chris Lattner | 2edd22b | 2007-04-24 00:16:04 +0000 | [diff] [blame] | 412 | if (FirstVal == LastVal) return; |
| 413 | |
Chris Lattner | a0f1ecc | 2007-05-05 07:36:14 +0000 | [diff] [blame] | 414 | Stream.EnterSubblock(bitc::CONSTANTS_BLOCK_ID, 4); |
Chris Lattner | b992be1 | 2007-04-23 20:35:01 +0000 | [diff] [blame] | 415 | |
Chris Lattner | a0f1ecc | 2007-05-05 07:36:14 +0000 | [diff] [blame] | 416 | unsigned AggregateAbbrev = 0; |
Chris Lattner | cb3d91b | 2007-05-06 00:53:07 +0000 | [diff] [blame] | 417 | unsigned String8Abbrev = 0; |
| 418 | unsigned CString7Abbrev = 0; |
| 419 | unsigned CString6Abbrev = 0; |
Chris Lattner | a0f1ecc | 2007-05-05 07:36:14 +0000 | [diff] [blame] | 420 | // If this is a constant pool for the module, emit module-specific abbrevs. |
| 421 | if (isGlobal) { |
| 422 | // Abbrev for CST_CODE_AGGREGATE. |
| 423 | BitCodeAbbrev *Abbv = new BitCodeAbbrev(); |
| 424 | Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_AGGREGATE)); |
| 425 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); |
| 426 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, Log2_32_Ceil(LastVal+1))); |
| 427 | AggregateAbbrev = Stream.EmitAbbrev(Abbv); |
Chris Lattner | 817f08a | 2007-05-06 00:42:18 +0000 | [diff] [blame] | 428 | |
| 429 | // Abbrev for CST_CODE_STRING. |
| 430 | Abbv = new BitCodeAbbrev(); |
| 431 | Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_STRING)); |
| 432 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); |
Chris Lattner | cb3d91b | 2007-05-06 00:53:07 +0000 | [diff] [blame] | 433 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); |
| 434 | String8Abbrev = Stream.EmitAbbrev(Abbv); |
| 435 | // Abbrev for CST_CODE_CSTRING. |
| 436 | Abbv = new BitCodeAbbrev(); |
| 437 | Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING)); |
| 438 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); |
Chris Lattner | 817f08a | 2007-05-06 00:42:18 +0000 | [diff] [blame] | 439 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); |
Chris Lattner | cb3d91b | 2007-05-06 00:53:07 +0000 | [diff] [blame] | 440 | CString7Abbrev = Stream.EmitAbbrev(Abbv); |
| 441 | // Abbrev for CST_CODE_CSTRING. |
| 442 | Abbv = new BitCodeAbbrev(); |
| 443 | Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING)); |
| 444 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); |
| 445 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6)); |
| 446 | CString6Abbrev = Stream.EmitAbbrev(Abbv); |
Chris Lattner | a0f1ecc | 2007-05-05 07:36:14 +0000 | [diff] [blame] | 447 | } |
| 448 | |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 449 | // FIXME: Install and use abbrevs to reduce size. Install them globally so |
| 450 | // they don't need to be reemitted for each function body. |
Chris Lattner | 2edd22b | 2007-04-24 00:16:04 +0000 | [diff] [blame] | 451 | |
| 452 | SmallVector<uint64_t, 64> Record; |
| 453 | |
| 454 | const ValueEnumerator::ValueList &Vals = VE.getValues(); |
| 455 | const Type *LastTy = 0; |
| 456 | for (unsigned i = FirstVal; i != LastVal; ++i) { |
| 457 | const Value *V = Vals[i].first; |
| 458 | // If we need to switch types, do so now. |
| 459 | if (V->getType() != LastTy) { |
| 460 | LastTy = V->getType(); |
| 461 | Record.push_back(VE.getTypeID(LastTy)); |
Chris Lattner | a0f1ecc | 2007-05-05 07:36:14 +0000 | [diff] [blame] | 462 | Stream.EmitRecord(bitc::CST_CODE_SETTYPE, Record, |
| 463 | CONSTANTS_SETTYPE_ABBREV); |
Chris Lattner | 2edd22b | 2007-04-24 00:16:04 +0000 | [diff] [blame] | 464 | Record.clear(); |
| 465 | } |
| 466 | |
| 467 | if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) { |
| 468 | assert(0 && IA && "FIXME: Inline asm writing unimp!"); |
| 469 | continue; |
| 470 | } |
| 471 | const Constant *C = cast<Constant>(V); |
| 472 | unsigned Code = -1U; |
| 473 | unsigned AbbrevToUse = 0; |
| 474 | if (C->isNullValue()) { |
| 475 | Code = bitc::CST_CODE_NULL; |
| 476 | } else if (isa<UndefValue>(C)) { |
| 477 | Code = bitc::CST_CODE_UNDEF; |
| 478 | } else if (const ConstantInt *IV = dyn_cast<ConstantInt>(C)) { |
| 479 | if (IV->getBitWidth() <= 64) { |
| 480 | int64_t V = IV->getSExtValue(); |
| 481 | if (V >= 0) |
| 482 | Record.push_back(V << 1); |
| 483 | else |
| 484 | Record.push_back((-V << 1) | 1); |
| 485 | Code = bitc::CST_CODE_INTEGER; |
Chris Lattner | a0f1ecc | 2007-05-05 07:36:14 +0000 | [diff] [blame] | 486 | AbbrevToUse = CONSTANTS_INTEGER_ABBREV; |
Chris Lattner | 2edd22b | 2007-04-24 00:16:04 +0000 | [diff] [blame] | 487 | } else { // Wide integers, > 64 bits in size. |
| 488 | // We have an arbitrary precision integer value to write whose |
| 489 | // bit width is > 64. However, in canonical unsigned integer |
| 490 | // format it is likely that the high bits are going to be zero. |
| 491 | // So, we only write the number of active words. |
| 492 | unsigned NWords = IV->getValue().getActiveWords(); |
| 493 | const uint64_t *RawWords = IV->getValue().getRawData(); |
Chris Lattner | 2edd22b | 2007-04-24 00:16:04 +0000 | [diff] [blame] | 494 | for (unsigned i = 0; i != NWords; ++i) { |
| 495 | int64_t V = RawWords[i]; |
| 496 | if (V >= 0) |
| 497 | Record.push_back(V << 1); |
| 498 | else |
| 499 | Record.push_back((-V << 1) | 1); |
| 500 | } |
| 501 | Code = bitc::CST_CODE_WIDE_INTEGER; |
| 502 | } |
| 503 | } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) { |
| 504 | Code = bitc::CST_CODE_FLOAT; |
| 505 | if (CFP->getType() == Type::FloatTy) { |
| 506 | Record.push_back(FloatToBits((float)CFP->getValue())); |
| 507 | } else { |
| 508 | assert (CFP->getType() == Type::DoubleTy && "Unknown FP type!"); |
| 509 | Record.push_back(DoubleToBits((double)CFP->getValue())); |
| 510 | } |
Chris Lattner | ff7fc5d | 2007-05-06 00:35:24 +0000 | [diff] [blame] | 511 | } else if (isa<ConstantArray>(C) && cast<ConstantArray>(C)->isString()) { |
| 512 | // Emit constant strings specially. |
Chris Lattner | cb3d91b | 2007-05-06 00:53:07 +0000 | [diff] [blame] | 513 | unsigned NumOps = C->getNumOperands(); |
| 514 | // If this is a null-terminated string, use the denser CSTRING encoding. |
| 515 | if (C->getOperand(NumOps-1)->isNullValue()) { |
| 516 | Code = bitc::CST_CODE_CSTRING; |
| 517 | --NumOps; // Don't encode the null, which isn't allowed by char6. |
| 518 | } else { |
| 519 | Code = bitc::CST_CODE_STRING; |
| 520 | AbbrevToUse = String8Abbrev; |
| 521 | } |
| 522 | bool isCStr7 = Code == bitc::CST_CODE_CSTRING; |
| 523 | bool isCStrChar6 = Code == bitc::CST_CODE_CSTRING; |
| 524 | for (unsigned i = 0; i != NumOps; ++i) { |
Chris Lattner | 817f08a | 2007-05-06 00:42:18 +0000 | [diff] [blame] | 525 | unsigned char V = cast<ConstantInt>(C->getOperand(i))->getZExtValue(); |
| 526 | Record.push_back(V); |
Chris Lattner | cb3d91b | 2007-05-06 00:53:07 +0000 | [diff] [blame] | 527 | isCStr7 &= (V & 128) == 0; |
| 528 | if (isCStrChar6) |
| 529 | isCStrChar6 = BitCodeAbbrevOp::isChar6(V); |
Chris Lattner | 817f08a | 2007-05-06 00:42:18 +0000 | [diff] [blame] | 530 | } |
Chris Lattner | cb3d91b | 2007-05-06 00:53:07 +0000 | [diff] [blame] | 531 | |
| 532 | if (isCStrChar6) |
| 533 | AbbrevToUse = CString6Abbrev; |
| 534 | else if (isCStr7) |
| 535 | AbbrevToUse = CString7Abbrev; |
Chris Lattner | 2edd22b | 2007-04-24 00:16:04 +0000 | [diff] [blame] | 536 | } else if (isa<ConstantArray>(C) || isa<ConstantStruct>(V) || |
| 537 | isa<ConstantVector>(V)) { |
| 538 | Code = bitc::CST_CODE_AGGREGATE; |
Chris Lattner | 2edd22b | 2007-04-24 00:16:04 +0000 | [diff] [blame] | 539 | for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) |
| 540 | Record.push_back(VE.getValueID(C->getOperand(i))); |
Chris Lattner | a0f1ecc | 2007-05-05 07:36:14 +0000 | [diff] [blame] | 541 | AbbrevToUse = AggregateAbbrev; |
Chris Lattner | 2edd22b | 2007-04-24 00:16:04 +0000 | [diff] [blame] | 542 | } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { |
Chris Lattner | f581c3b | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 543 | switch (CE->getOpcode()) { |
| 544 | default: |
| 545 | if (Instruction::isCast(CE->getOpcode())) { |
| 546 | Code = bitc::CST_CODE_CE_CAST; |
| 547 | Record.push_back(GetEncodedCastOpcode(CE->getOpcode())); |
| 548 | Record.push_back(VE.getTypeID(C->getOperand(0)->getType())); |
| 549 | Record.push_back(VE.getValueID(C->getOperand(0))); |
Chris Lattner | a0f1ecc | 2007-05-05 07:36:14 +0000 | [diff] [blame] | 550 | AbbrevToUse = CONSTANTS_CE_CAST_Abbrev; |
Chris Lattner | f581c3b | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 551 | } else { |
| 552 | assert(CE->getNumOperands() == 2 && "Unknown constant expr!"); |
| 553 | Code = bitc::CST_CODE_CE_BINOP; |
| 554 | Record.push_back(GetEncodedBinaryOpcode(CE->getOpcode())); |
| 555 | Record.push_back(VE.getValueID(C->getOperand(0))); |
| 556 | Record.push_back(VE.getValueID(C->getOperand(1))); |
| 557 | } |
| 558 | break; |
| 559 | case Instruction::GetElementPtr: |
| 560 | Code = bitc::CST_CODE_CE_GEP; |
Chris Lattner | f581c3b | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 561 | for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) { |
| 562 | Record.push_back(VE.getTypeID(C->getOperand(i)->getType())); |
| 563 | Record.push_back(VE.getValueID(C->getOperand(i))); |
| 564 | } |
| 565 | break; |
| 566 | case Instruction::Select: |
| 567 | Code = bitc::CST_CODE_CE_SELECT; |
| 568 | Record.push_back(VE.getValueID(C->getOperand(0))); |
| 569 | Record.push_back(VE.getValueID(C->getOperand(1))); |
| 570 | Record.push_back(VE.getValueID(C->getOperand(2))); |
| 571 | break; |
| 572 | case Instruction::ExtractElement: |
| 573 | Code = bitc::CST_CODE_CE_EXTRACTELT; |
| 574 | Record.push_back(VE.getTypeID(C->getOperand(0)->getType())); |
| 575 | Record.push_back(VE.getValueID(C->getOperand(0))); |
| 576 | Record.push_back(VE.getValueID(C->getOperand(1))); |
| 577 | break; |
| 578 | case Instruction::InsertElement: |
| 579 | Code = bitc::CST_CODE_CE_INSERTELT; |
| 580 | Record.push_back(VE.getValueID(C->getOperand(0))); |
| 581 | Record.push_back(VE.getValueID(C->getOperand(1))); |
| 582 | Record.push_back(VE.getValueID(C->getOperand(2))); |
| 583 | break; |
| 584 | case Instruction::ShuffleVector: |
| 585 | Code = bitc::CST_CODE_CE_SHUFFLEVEC; |
| 586 | Record.push_back(VE.getValueID(C->getOperand(0))); |
| 587 | Record.push_back(VE.getValueID(C->getOperand(1))); |
| 588 | Record.push_back(VE.getValueID(C->getOperand(2))); |
| 589 | break; |
| 590 | case Instruction::ICmp: |
| 591 | case Instruction::FCmp: |
| 592 | Code = bitc::CST_CODE_CE_CMP; |
| 593 | Record.push_back(VE.getTypeID(C->getOperand(0)->getType())); |
| 594 | Record.push_back(VE.getValueID(C->getOperand(0))); |
| 595 | Record.push_back(VE.getValueID(C->getOperand(1))); |
| 596 | Record.push_back(CE->getPredicate()); |
| 597 | break; |
| 598 | } |
Chris Lattner | 2edd22b | 2007-04-24 00:16:04 +0000 | [diff] [blame] | 599 | } else { |
| 600 | assert(0 && "Unknown constant!"); |
| 601 | } |
| 602 | Stream.EmitRecord(Code, Record, AbbrevToUse); |
| 603 | Record.clear(); |
| 604 | } |
| 605 | |
| 606 | Stream.ExitBlock(); |
| 607 | } |
| 608 | |
| 609 | static void WriteModuleConstants(const ValueEnumerator &VE, |
| 610 | BitstreamWriter &Stream) { |
| 611 | const ValueEnumerator::ValueList &Vals = VE.getValues(); |
| 612 | |
| 613 | // Find the first constant to emit, which is the first non-globalvalue value. |
| 614 | // We know globalvalues have been emitted by WriteModuleInfo. |
| 615 | for (unsigned i = 0, e = Vals.size(); i != e; ++i) { |
| 616 | if (!isa<GlobalValue>(Vals[i].first)) { |
Chris Lattner | a0f1ecc | 2007-05-05 07:36:14 +0000 | [diff] [blame] | 617 | WriteConstants(i, Vals.size(), VE, Stream, true); |
Chris Lattner | 2edd22b | 2007-04-24 00:16:04 +0000 | [diff] [blame] | 618 | return; |
| 619 | } |
| 620 | } |
| 621 | } |
Chris Lattner | b992be1 | 2007-04-23 20:35:01 +0000 | [diff] [blame] | 622 | |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 623 | /// PushValueAndType - The file has to encode both the value and type id for |
| 624 | /// many values, because we need to know what type to create for forward |
| 625 | /// references. However, most operands are not forward references, so this type |
| 626 | /// field is not needed. |
| 627 | /// |
| 628 | /// This function adds V's value ID to Vals. If the value ID is higher than the |
| 629 | /// instruction ID, then it is a forward reference, and it also includes the |
| 630 | /// type ID. |
| 631 | static bool PushValueAndType(Value *V, unsigned InstID, |
| 632 | SmallVector<unsigned, 64> &Vals, |
| 633 | ValueEnumerator &VE) { |
| 634 | unsigned ValID = VE.getValueID(V); |
| 635 | Vals.push_back(ValID); |
| 636 | if (ValID >= InstID) { |
| 637 | Vals.push_back(VE.getTypeID(V->getType())); |
| 638 | return true; |
| 639 | } |
| 640 | return false; |
| 641 | } |
| 642 | |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 643 | /// WriteInstruction - Emit an instruction to the specified stream. |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 644 | static void WriteInstruction(const Instruction &I, unsigned InstID, |
| 645 | ValueEnumerator &VE, BitstreamWriter &Stream, |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 646 | SmallVector<unsigned, 64> &Vals) { |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 647 | unsigned Code = 0; |
| 648 | unsigned AbbrevToUse = 0; |
| 649 | switch (I.getOpcode()) { |
| 650 | default: |
| 651 | if (Instruction::isCast(I.getOpcode())) { |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 652 | Code = bitc::FUNC_CODE_INST_CAST; |
Chris Lattner | abfbf85 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 653 | PushValueAndType(I.getOperand(0), InstID, Vals, VE); |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 654 | Vals.push_back(VE.getTypeID(I.getType())); |
Chris Lattner | abfbf85 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 655 | Vals.push_back(GetEncodedCastOpcode(I.getOpcode())); |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 656 | } else { |
| 657 | assert(isa<BinaryOperator>(I) && "Unknown instruction!"); |
Chris Lattner | f639875 | 2007-05-02 04:26:36 +0000 | [diff] [blame] | 658 | Code = bitc::FUNC_CODE_INST_BINOP; |
Chris Lattner | abfbf85 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 659 | PushValueAndType(I.getOperand(0), InstID, Vals, VE); |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 660 | Vals.push_back(VE.getValueID(I.getOperand(1))); |
Chris Lattner | abfbf85 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 661 | Vals.push_back(GetEncodedBinaryOpcode(I.getOpcode())); |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 662 | } |
| 663 | break; |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 664 | |
| 665 | case Instruction::GetElementPtr: |
| 666 | Code = bitc::FUNC_CODE_INST_GEP; |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 667 | for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) |
| 668 | PushValueAndType(I.getOperand(i), InstID, Vals, VE); |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 669 | break; |
| 670 | case Instruction::Select: |
| 671 | Code = bitc::FUNC_CODE_INST_SELECT; |
Chris Lattner | abfbf85 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 672 | PushValueAndType(I.getOperand(1), InstID, Vals, VE); |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 673 | Vals.push_back(VE.getValueID(I.getOperand(2))); |
Chris Lattner | abfbf85 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 674 | Vals.push_back(VE.getValueID(I.getOperand(0))); |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 675 | break; |
| 676 | case Instruction::ExtractElement: |
| 677 | Code = bitc::FUNC_CODE_INST_EXTRACTELT; |
Chris Lattner | abfbf85 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 678 | PushValueAndType(I.getOperand(0), InstID, Vals, VE); |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 679 | Vals.push_back(VE.getValueID(I.getOperand(1))); |
| 680 | break; |
| 681 | case Instruction::InsertElement: |
| 682 | Code = bitc::FUNC_CODE_INST_INSERTELT; |
Chris Lattner | abfbf85 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 683 | PushValueAndType(I.getOperand(0), InstID, Vals, VE); |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 684 | Vals.push_back(VE.getValueID(I.getOperand(1))); |
| 685 | Vals.push_back(VE.getValueID(I.getOperand(2))); |
| 686 | break; |
| 687 | case Instruction::ShuffleVector: |
| 688 | Code = bitc::FUNC_CODE_INST_SHUFFLEVEC; |
Chris Lattner | abfbf85 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 689 | PushValueAndType(I.getOperand(0), InstID, Vals, VE); |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 690 | Vals.push_back(VE.getValueID(I.getOperand(1))); |
| 691 | Vals.push_back(VE.getValueID(I.getOperand(2))); |
| 692 | break; |
| 693 | case Instruction::ICmp: |
| 694 | case Instruction::FCmp: |
| 695 | Code = bitc::FUNC_CODE_INST_CMP; |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 696 | PushValueAndType(I.getOperand(0), InstID, Vals, VE); |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 697 | Vals.push_back(VE.getValueID(I.getOperand(1))); |
| 698 | Vals.push_back(cast<CmpInst>(I).getPredicate()); |
| 699 | break; |
| 700 | |
| 701 | case Instruction::Ret: |
| 702 | Code = bitc::FUNC_CODE_INST_RET; |
Chris Lattner | 94687ac | 2007-05-06 01:28:01 +0000 | [diff] [blame^] | 703 | if (!I.getNumOperands()) |
| 704 | AbbrevToUse = FUNCTION_INST_RET_VOID_ABBREV; |
| 705 | else if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE)) |
| 706 | AbbrevToUse = FUNCTION_INST_RET_VAL_ABBREV; |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 707 | break; |
| 708 | case Instruction::Br: |
| 709 | Code = bitc::FUNC_CODE_INST_BR; |
| 710 | Vals.push_back(VE.getValueID(I.getOperand(0))); |
| 711 | if (cast<BranchInst>(I).isConditional()) { |
| 712 | Vals.push_back(VE.getValueID(I.getOperand(1))); |
| 713 | Vals.push_back(VE.getValueID(I.getOperand(2))); |
| 714 | } |
| 715 | break; |
| 716 | case Instruction::Switch: |
| 717 | Code = bitc::FUNC_CODE_INST_SWITCH; |
| 718 | Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 719 | for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) |
| 720 | Vals.push_back(VE.getValueID(I.getOperand(i))); |
| 721 | break; |
Chris Lattner | 60ce9b5 | 2007-05-01 07:03:37 +0000 | [diff] [blame] | 722 | case Instruction::Invoke: { |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 723 | Code = bitc::FUNC_CODE_INST_INVOKE; |
Chris Lattner | 7652019 | 2007-05-03 22:34:03 +0000 | [diff] [blame] | 724 | Vals.push_back(cast<InvokeInst>(I).getCallingConv()); |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 725 | Vals.push_back(VE.getValueID(I.getOperand(1))); // normal dest |
| 726 | Vals.push_back(VE.getValueID(I.getOperand(2))); // unwind dest |
| 727 | PushValueAndType(I.getOperand(0), InstID, Vals, VE); // callee |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 728 | |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 729 | // Emit value #'s for the fixed parameters. |
| 730 | const PointerType *PTy = cast<PointerType>(I.getOperand(0)->getType()); |
| 731 | const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); |
| 732 | for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) |
| 733 | Vals.push_back(VE.getValueID(I.getOperand(i+3))); // fixed param. |
| 734 | |
| 735 | // Emit type/value pairs for varargs params. |
| 736 | if (FTy->isVarArg()) { |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 737 | for (unsigned i = 3+FTy->getNumParams(), e = I.getNumOperands(); |
| 738 | i != e; ++i) |
| 739 | PushValueAndType(I.getOperand(i), InstID, Vals, VE); // vararg |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 740 | } |
| 741 | break; |
Chris Lattner | 60ce9b5 | 2007-05-01 07:03:37 +0000 | [diff] [blame] | 742 | } |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 743 | case Instruction::Unwind: |
| 744 | Code = bitc::FUNC_CODE_INST_UNWIND; |
| 745 | break; |
| 746 | case Instruction::Unreachable: |
| 747 | Code = bitc::FUNC_CODE_INST_UNREACHABLE; |
Chris Lattner | 94687ac | 2007-05-06 01:28:01 +0000 | [diff] [blame^] | 748 | AbbrevToUse = FUNCTION_INST_UNREACHABLE_ABBREV; |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 749 | break; |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 750 | |
| 751 | case Instruction::PHI: |
| 752 | Code = bitc::FUNC_CODE_INST_PHI; |
| 753 | Vals.push_back(VE.getTypeID(I.getType())); |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 754 | for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) |
| 755 | Vals.push_back(VE.getValueID(I.getOperand(i))); |
| 756 | break; |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 757 | |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 758 | case Instruction::Malloc: |
| 759 | Code = bitc::FUNC_CODE_INST_MALLOC; |
| 760 | Vals.push_back(VE.getTypeID(I.getType())); |
| 761 | Vals.push_back(VE.getValueID(I.getOperand(0))); // size. |
| 762 | Vals.push_back(Log2_32(cast<MallocInst>(I).getAlignment())+1); |
| 763 | break; |
| 764 | |
| 765 | case Instruction::Free: |
| 766 | Code = bitc::FUNC_CODE_INST_FREE; |
Chris Lattner | abfbf85 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 767 | PushValueAndType(I.getOperand(0), InstID, Vals, VE); |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 768 | break; |
| 769 | |
| 770 | case Instruction::Alloca: |
| 771 | Code = bitc::FUNC_CODE_INST_ALLOCA; |
| 772 | Vals.push_back(VE.getTypeID(I.getType())); |
| 773 | Vals.push_back(VE.getValueID(I.getOperand(0))); // size. |
| 774 | Vals.push_back(Log2_32(cast<AllocaInst>(I).getAlignment())+1); |
| 775 | break; |
| 776 | |
| 777 | case Instruction::Load: |
| 778 | Code = bitc::FUNC_CODE_INST_LOAD; |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 779 | if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE)) // ptr |
| 780 | AbbrevToUse = FUNCTION_INST_LOAD_ABBREV; |
| 781 | |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 782 | Vals.push_back(Log2_32(cast<LoadInst>(I).getAlignment())+1); |
| 783 | Vals.push_back(cast<LoadInst>(I).isVolatile()); |
| 784 | break; |
| 785 | case Instruction::Store: |
| 786 | Code = bitc::FUNC_CODE_INST_STORE; |
Chris Lattner | abfbf85 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 787 | PushValueAndType(I.getOperand(0), InstID, Vals, VE); // val. |
| 788 | Vals.push_back(VE.getValueID(I.getOperand(1))); // ptr. |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 789 | Vals.push_back(Log2_32(cast<StoreInst>(I).getAlignment())+1); |
| 790 | Vals.push_back(cast<StoreInst>(I).isVolatile()); |
| 791 | break; |
| 792 | case Instruction::Call: { |
| 793 | Code = bitc::FUNC_CODE_INST_CALL; |
Chris Lattner | 7652019 | 2007-05-03 22:34:03 +0000 | [diff] [blame] | 794 | Vals.push_back((cast<CallInst>(I).getCallingConv() << 1) | |
| 795 | cast<CallInst>(I).isTailCall()); |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 796 | PushValueAndType(I.getOperand(0), InstID, Vals, VE); // Callee |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 797 | |
| 798 | // Emit value #'s for the fixed parameters. |
| 799 | const PointerType *PTy = cast<PointerType>(I.getOperand(0)->getType()); |
| 800 | const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); |
| 801 | for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) |
| 802 | Vals.push_back(VE.getValueID(I.getOperand(i+1))); // fixed param. |
| 803 | |
Chris Lattner | 60ce9b5 | 2007-05-01 07:03:37 +0000 | [diff] [blame] | 804 | // Emit type/value pairs for varargs params. |
| 805 | if (FTy->isVarArg()) { |
| 806 | unsigned NumVarargs = I.getNumOperands()-1-FTy->getNumParams(); |
Chris Lattner | 60ce9b5 | 2007-05-01 07:03:37 +0000 | [diff] [blame] | 807 | for (unsigned i = I.getNumOperands()-NumVarargs, e = I.getNumOperands(); |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 808 | i != e; ++i) |
| 809 | PushValueAndType(I.getOperand(i), InstID, Vals, VE); // varargs |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 810 | } |
| 811 | break; |
Chris Lattner | 60ce9b5 | 2007-05-01 07:03:37 +0000 | [diff] [blame] | 812 | } |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 813 | case Instruction::VAArg: |
| 814 | Code = bitc::FUNC_CODE_INST_VAARG; |
| 815 | Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); // valistty |
| 816 | Vals.push_back(VE.getValueID(I.getOperand(0))); // valist. |
| 817 | Vals.push_back(VE.getTypeID(I.getType())); // restype. |
| 818 | break; |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 819 | } |
| 820 | |
| 821 | Stream.EmitRecord(Code, Vals, AbbrevToUse); |
| 822 | Vals.clear(); |
| 823 | } |
| 824 | |
Chris Lattner | be1f993 | 2007-05-01 02:14:57 +0000 | [diff] [blame] | 825 | // Emit names for globals/functions etc. |
| 826 | static void WriteValueSymbolTable(const ValueSymbolTable &VST, |
| 827 | const ValueEnumerator &VE, |
| 828 | BitstreamWriter &Stream) { |
| 829 | if (VST.empty()) return; |
Chris Lattner | ff294a4 | 2007-05-05 01:26:50 +0000 | [diff] [blame] | 830 | Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4); |
Chris Lattner | 2e7899d | 2007-05-04 20:34:50 +0000 | [diff] [blame] | 831 | |
Chris Lattner | be1f993 | 2007-05-01 02:14:57 +0000 | [diff] [blame] | 832 | // FIXME: Set up the abbrev, we know how many values there are! |
| 833 | // FIXME: We know if the type names can use 7-bit ascii. |
| 834 | SmallVector<unsigned, 64> NameVals; |
| 835 | |
| 836 | for (ValueSymbolTable::const_iterator SI = VST.begin(), SE = VST.end(); |
| 837 | SI != SE; ++SI) { |
Chris Lattner | 5969830 | 2007-05-04 20:52:02 +0000 | [diff] [blame] | 838 | |
| 839 | const ValueName &Name = *SI; |
| 840 | |
| 841 | // Figure out the encoding to use for the name. |
| 842 | bool is7Bit = true; |
Chris Lattner | ff294a4 | 2007-05-05 01:26:50 +0000 | [diff] [blame] | 843 | bool isChar6 = true; |
| 844 | for (const char *C = Name.getKeyData(), *E = C+Name.getKeyLength(); |
| 845 | C != E; ++C) { |
| 846 | if (isChar6) |
| 847 | isChar6 = BitCodeAbbrevOp::isChar6(*C); |
| 848 | if ((unsigned char)*C & 128) { |
Chris Lattner | 5969830 | 2007-05-04 20:52:02 +0000 | [diff] [blame] | 849 | is7Bit = false; |
Chris Lattner | ff294a4 | 2007-05-05 01:26:50 +0000 | [diff] [blame] | 850 | break; // don't bother scanning the rest. |
Chris Lattner | 5969830 | 2007-05-04 20:52:02 +0000 | [diff] [blame] | 851 | } |
Chris Lattner | ff294a4 | 2007-05-05 01:26:50 +0000 | [diff] [blame] | 852 | } |
Chris Lattner | 5969830 | 2007-05-04 20:52:02 +0000 | [diff] [blame] | 853 | |
Chris Lattner | fd1ae95 | 2007-05-04 21:31:13 +0000 | [diff] [blame] | 854 | unsigned AbbrevToUse = VST_ENTRY_8_ABBREV; |
Chris Lattner | be1f993 | 2007-05-01 02:14:57 +0000 | [diff] [blame] | 855 | |
Chris Lattner | fd1ae95 | 2007-05-04 21:31:13 +0000 | [diff] [blame] | 856 | // VST_ENTRY: [valueid, namechar x N] |
| 857 | // VST_BBENTRY: [bbid, namechar x N] |
Chris Lattner | e825ed5 | 2007-05-03 22:18:21 +0000 | [diff] [blame] | 858 | unsigned Code; |
| 859 | if (isa<BasicBlock>(SI->getValue())) { |
| 860 | Code = bitc::VST_CODE_BBENTRY; |
Chris Lattner | ff294a4 | 2007-05-05 01:26:50 +0000 | [diff] [blame] | 861 | if (isChar6) |
| 862 | AbbrevToUse = VST_BBENTRY_6_ABBREV; |
Chris Lattner | e825ed5 | 2007-05-03 22:18:21 +0000 | [diff] [blame] | 863 | } else { |
| 864 | Code = bitc::VST_CODE_ENTRY; |
Chris Lattner | ff294a4 | 2007-05-05 01:26:50 +0000 | [diff] [blame] | 865 | if (isChar6) |
| 866 | AbbrevToUse = VST_ENTRY_6_ABBREV; |
| 867 | else if (is7Bit) |
| 868 | AbbrevToUse = VST_ENTRY_7_ABBREV; |
Chris Lattner | e825ed5 | 2007-05-03 22:18:21 +0000 | [diff] [blame] | 869 | } |
Chris Lattner | be1f993 | 2007-05-01 02:14:57 +0000 | [diff] [blame] | 870 | |
Chris Lattner | e825ed5 | 2007-05-03 22:18:21 +0000 | [diff] [blame] | 871 | NameVals.push_back(VE.getValueID(SI->getValue())); |
Chris Lattner | 5969830 | 2007-05-04 20:52:02 +0000 | [diff] [blame] | 872 | for (const char *P = Name.getKeyData(), |
| 873 | *E = Name.getKeyData()+Name.getKeyLength(); P != E; ++P) |
Chris Lattner | be1f993 | 2007-05-01 02:14:57 +0000 | [diff] [blame] | 874 | NameVals.push_back((unsigned char)*P); |
| 875 | |
| 876 | // Emit the finished record. |
Chris Lattner | e825ed5 | 2007-05-03 22:18:21 +0000 | [diff] [blame] | 877 | Stream.EmitRecord(Code, NameVals, AbbrevToUse); |
Chris Lattner | be1f993 | 2007-05-01 02:14:57 +0000 | [diff] [blame] | 878 | NameVals.clear(); |
| 879 | } |
| 880 | Stream.ExitBlock(); |
| 881 | } |
| 882 | |
Chris Lattner | 8d35c79 | 2007-04-26 03:50:57 +0000 | [diff] [blame] | 883 | /// WriteFunction - Emit a function body to the module stream. |
Chris Lattner | 198f34a | 2007-04-26 03:27:58 +0000 | [diff] [blame] | 884 | static void WriteFunction(const Function &F, ValueEnumerator &VE, |
| 885 | BitstreamWriter &Stream) { |
Chris Lattner | 01b2745 | 2007-04-29 05:49:09 +0000 | [diff] [blame] | 886 | Stream.EnterSubblock(bitc::FUNCTION_BLOCK_ID, 3); |
Chris Lattner | 8d35c79 | 2007-04-26 03:50:57 +0000 | [diff] [blame] | 887 | VE.incorporateFunction(F); |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 888 | |
| 889 | SmallVector<unsigned, 64> Vals; |
| 890 | |
| 891 | // Emit the number of basic blocks, so the reader can create them ahead of |
| 892 | // time. |
| 893 | Vals.push_back(VE.getBasicBlocks().size()); |
| 894 | Stream.EmitRecord(bitc::FUNC_CODE_DECLAREBLOCKS, Vals); |
| 895 | Vals.clear(); |
| 896 | |
| 897 | // FIXME: Function attributes? |
| 898 | |
| 899 | // If there are function-local constants, emit them now. |
| 900 | unsigned CstStart, CstEnd; |
| 901 | VE.getFunctionConstantRange(CstStart, CstEnd); |
Chris Lattner | a0f1ecc | 2007-05-05 07:36:14 +0000 | [diff] [blame] | 902 | WriteConstants(CstStart, CstEnd, VE, Stream, false); |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 903 | |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 904 | // Keep a running idea of what the instruction ID is. |
| 905 | unsigned InstID = CstEnd; |
| 906 | |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 907 | // Finally, emit all the instructions, in order. |
| 908 | for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 909 | for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); |
| 910 | I != E; ++I) { |
| 911 | WriteInstruction(*I, InstID, VE, Stream, Vals); |
| 912 | if (I->getType() != Type::VoidTy) |
| 913 | ++InstID; |
| 914 | } |
Chris Lattner | 198f34a | 2007-04-26 03:27:58 +0000 | [diff] [blame] | 915 | |
Chris Lattner | be1f993 | 2007-05-01 02:14:57 +0000 | [diff] [blame] | 916 | // Emit names for all the instructions etc. |
| 917 | WriteValueSymbolTable(F.getValueSymbolTable(), VE, Stream); |
| 918 | |
Chris Lattner | 8d35c79 | 2007-04-26 03:50:57 +0000 | [diff] [blame] | 919 | VE.purgeFunction(); |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 920 | Stream.ExitBlock(); |
Chris Lattner | 198f34a | 2007-04-26 03:27:58 +0000 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | /// WriteTypeSymbolTable - Emit a block for the specified type symtab. |
| 924 | static void WriteTypeSymbolTable(const TypeSymbolTable &TST, |
| 925 | const ValueEnumerator &VE, |
| 926 | BitstreamWriter &Stream) { |
| 927 | if (TST.empty()) return; |
| 928 | |
| 929 | Stream.EnterSubblock(bitc::TYPE_SYMTAB_BLOCK_ID, 3); |
| 930 | |
Chris Lattner | 7a263ea | 2007-05-05 00:47:19 +0000 | [diff] [blame] | 931 | // 7-bit fixed width VST_CODE_ENTRY strings. |
| 932 | BitCodeAbbrev *Abbv = new BitCodeAbbrev(); |
| 933 | Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY)); |
| 934 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, |
| 935 | Log2_32_Ceil(VE.getTypes().size()+1))); |
| 936 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); |
| 937 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); |
| 938 | unsigned V7Abbrev = Stream.EmitAbbrev(Abbv); |
Chris Lattner | 198f34a | 2007-04-26 03:27:58 +0000 | [diff] [blame] | 939 | |
| 940 | SmallVector<unsigned, 64> NameVals; |
| 941 | |
| 942 | for (TypeSymbolTable::const_iterator TI = TST.begin(), TE = TST.end(); |
| 943 | TI != TE; ++TI) { |
Chris Lattner | 7a263ea | 2007-05-05 00:47:19 +0000 | [diff] [blame] | 944 | // TST_ENTRY: [typeid, namechar x N] |
Chris Lattner | 198f34a | 2007-04-26 03:27:58 +0000 | [diff] [blame] | 945 | NameVals.push_back(VE.getTypeID(TI->second)); |
| 946 | |
| 947 | const std::string &Str = TI->first; |
Chris Lattner | 7a263ea | 2007-05-05 00:47:19 +0000 | [diff] [blame] | 948 | bool is7Bit = true; |
| 949 | for (unsigned i = 0, e = Str.size(); i != e; ++i) { |
| 950 | NameVals.push_back((unsigned char)Str[i]); |
| 951 | if (Str[i] & 128) |
| 952 | is7Bit = false; |
| 953 | } |
Chris Lattner | 198f34a | 2007-04-26 03:27:58 +0000 | [diff] [blame] | 954 | |
| 955 | // Emit the finished record. |
Chris Lattner | 7a263ea | 2007-05-05 00:47:19 +0000 | [diff] [blame] | 956 | Stream.EmitRecord(bitc::VST_CODE_ENTRY, NameVals, is7Bit ? V7Abbrev : 0); |
Chris Lattner | 198f34a | 2007-04-26 03:27:58 +0000 | [diff] [blame] | 957 | NameVals.clear(); |
| 958 | } |
| 959 | |
| 960 | Stream.ExitBlock(); |
| 961 | } |
| 962 | |
Chris Lattner | 07faafc | 2007-05-04 18:26:27 +0000 | [diff] [blame] | 963 | // Emit blockinfo, which defines the standard abbreviations etc. |
Chris Lattner | a0f1ecc | 2007-05-05 07:36:14 +0000 | [diff] [blame] | 964 | static void WriteBlockInfo(const ValueEnumerator &VE, BitstreamWriter &Stream) { |
Chris Lattner | 07faafc | 2007-05-04 18:26:27 +0000 | [diff] [blame] | 965 | // We only want to emit block info records for blocks that have multiple |
| 966 | // instances: CONSTANTS_BLOCK, FUNCTION_BLOCK and VALUE_SYMTAB_BLOCK. Other |
| 967 | // blocks can defined their abbrevs inline. |
Chris Lattner | e17b658 | 2007-05-05 00:17:00 +0000 | [diff] [blame] | 968 | Stream.EnterBlockInfoBlock(2); |
Chris Lattner | 07faafc | 2007-05-04 18:26:27 +0000 | [diff] [blame] | 969 | |
Chris Lattner | e17b658 | 2007-05-05 00:17:00 +0000 | [diff] [blame] | 970 | { // 8-bit fixed-width VST_ENTRY/VST_BBENTRY strings. |
| 971 | BitCodeAbbrev *Abbv = new BitCodeAbbrev(); |
| 972 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); |
| 973 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); |
| 974 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); |
| 975 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); |
| 976 | if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, |
| 977 | Abbv) != VST_ENTRY_8_ABBREV) |
| 978 | assert(0 && "Unexpected abbrev ordering!"); |
| 979 | } |
| 980 | |
| 981 | { // 7-bit fixed width VST_ENTRY strings. |
| 982 | BitCodeAbbrev *Abbv = new BitCodeAbbrev(); |
| 983 | Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY)); |
| 984 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); |
| 985 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); |
| 986 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); |
| 987 | if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, |
| 988 | Abbv) != VST_ENTRY_7_ABBREV) |
| 989 | assert(0 && "Unexpected abbrev ordering!"); |
| 990 | } |
Chris Lattner | ff294a4 | 2007-05-05 01:26:50 +0000 | [diff] [blame] | 991 | { // 6-bit char6 VST_ENTRY strings. |
| 992 | BitCodeAbbrev *Abbv = new BitCodeAbbrev(); |
| 993 | Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY)); |
| 994 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); |
| 995 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); |
| 996 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6)); |
| 997 | if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, |
| 998 | Abbv) != VST_ENTRY_6_ABBREV) |
| 999 | assert(0 && "Unexpected abbrev ordering!"); |
| 1000 | } |
| 1001 | { // 6-bit char6 VST_BBENTRY strings. |
Chris Lattner | e17b658 | 2007-05-05 00:17:00 +0000 | [diff] [blame] | 1002 | BitCodeAbbrev *Abbv = new BitCodeAbbrev(); |
| 1003 | Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_BBENTRY)); |
| 1004 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); |
| 1005 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); |
Chris Lattner | ff294a4 | 2007-05-05 01:26:50 +0000 | [diff] [blame] | 1006 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6)); |
Chris Lattner | e17b658 | 2007-05-05 00:17:00 +0000 | [diff] [blame] | 1007 | if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, |
Chris Lattner | ff294a4 | 2007-05-05 01:26:50 +0000 | [diff] [blame] | 1008 | Abbv) != VST_BBENTRY_6_ABBREV) |
Chris Lattner | e17b658 | 2007-05-05 00:17:00 +0000 | [diff] [blame] | 1009 | assert(0 && "Unexpected abbrev ordering!"); |
| 1010 | } |
| 1011 | |
Chris Lattner | 440168b | 2007-05-05 07:44:49 +0000 | [diff] [blame] | 1012 | |
| 1013 | |
Chris Lattner | a0f1ecc | 2007-05-05 07:36:14 +0000 | [diff] [blame] | 1014 | { // SETTYPE abbrev for CONSTANTS_BLOCK. |
| 1015 | BitCodeAbbrev *Abbv = new BitCodeAbbrev(); |
| 1016 | Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_SETTYPE)); |
| 1017 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, |
| 1018 | Log2_32_Ceil(VE.getTypes().size()+1))); |
| 1019 | if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, |
| 1020 | Abbv) != CONSTANTS_SETTYPE_ABBREV) |
| 1021 | assert(0 && "Unexpected abbrev ordering!"); |
| 1022 | } |
| 1023 | |
| 1024 | { // INTEGER abbrev for CONSTANTS_BLOCK. |
| 1025 | BitCodeAbbrev *Abbv = new BitCodeAbbrev(); |
| 1026 | Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_INTEGER)); |
| 1027 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); |
| 1028 | if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, |
| 1029 | Abbv) != CONSTANTS_INTEGER_ABBREV) |
| 1030 | assert(0 && "Unexpected abbrev ordering!"); |
| 1031 | } |
| 1032 | |
| 1033 | { // CE_CAST abbrev for CONSTANTS_BLOCK. |
| 1034 | BitCodeAbbrev *Abbv = new BitCodeAbbrev(); |
| 1035 | Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CE_CAST)); |
| 1036 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // cast opc |
| 1037 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // typeid |
| 1038 | Log2_32_Ceil(VE.getTypes().size()+1))); |
| 1039 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id |
| 1040 | |
| 1041 | if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, |
| 1042 | Abbv) != CONSTANTS_CE_CAST_Abbrev) |
| 1043 | assert(0 && "Unexpected abbrev ordering!"); |
| 1044 | } |
| 1045 | { // NULL abbrev for CONSTANTS_BLOCK. |
| 1046 | BitCodeAbbrev *Abbv = new BitCodeAbbrev(); |
| 1047 | Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_NULL)); |
| 1048 | if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, |
| 1049 | Abbv) != CONSTANTS_NULL_Abbrev) |
| 1050 | assert(0 && "Unexpected abbrev ordering!"); |
| 1051 | } |
| 1052 | |
Chris Lattner | 440168b | 2007-05-05 07:44:49 +0000 | [diff] [blame] | 1053 | // FIXME: This should only use space for first class types! |
| 1054 | |
| 1055 | { // INST_LOAD abbrev for FUNCTION_BLOCK. |
| 1056 | BitCodeAbbrev *Abbv = new BitCodeAbbrev(); |
| 1057 | Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_LOAD)); |
Chris Lattner | 440168b | 2007-05-05 07:44:49 +0000 | [diff] [blame] | 1058 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Ptr |
| 1059 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // Align |
| 1060 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // volatile |
| 1061 | if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, |
| 1062 | Abbv) != FUNCTION_INST_LOAD_ABBREV) |
| 1063 | assert(0 && "Unexpected abbrev ordering!"); |
| 1064 | } |
Chris Lattner | 94687ac | 2007-05-06 01:28:01 +0000 | [diff] [blame^] | 1065 | { // INST_RET abbrev for FUNCTION_BLOCK. |
| 1066 | BitCodeAbbrev *Abbv = new BitCodeAbbrev(); |
| 1067 | Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET)); |
| 1068 | if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, |
| 1069 | Abbv) != FUNCTION_INST_RET_VOID_ABBREV) |
| 1070 | assert(0 && "Unexpected abbrev ordering!"); |
| 1071 | } |
| 1072 | { // INST_RET abbrev for FUNCTION_BLOCK. |
| 1073 | BitCodeAbbrev *Abbv = new BitCodeAbbrev(); |
| 1074 | Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET)); |
| 1075 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ValID |
| 1076 | if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, |
| 1077 | Abbv) != FUNCTION_INST_RET_VAL_ABBREV) |
| 1078 | assert(0 && "Unexpected abbrev ordering!"); |
| 1079 | } |
| 1080 | { // INST_UNREACHABLE abbrev for FUNCTION_BLOCK. |
| 1081 | BitCodeAbbrev *Abbv = new BitCodeAbbrev(); |
| 1082 | Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_UNREACHABLE)); |
| 1083 | if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, |
| 1084 | Abbv) != FUNCTION_INST_UNREACHABLE_ABBREV) |
| 1085 | assert(0 && "Unexpected abbrev ordering!"); |
| 1086 | } |
Chris Lattner | 440168b | 2007-05-05 07:44:49 +0000 | [diff] [blame] | 1087 | |
Chris Lattner | a0f1ecc | 2007-05-05 07:36:14 +0000 | [diff] [blame] | 1088 | Stream.ExitBlock(); |
| 1089 | } |
| 1090 | |
| 1091 | |
| 1092 | /// WriteModule - Emit the specified module to the bitstream. |
| 1093 | static void WriteModule(const Module *M, BitstreamWriter &Stream) { |
| 1094 | Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3); |
| 1095 | |
| 1096 | // Emit the version number if it is non-zero. |
| 1097 | if (CurVersion) { |
| 1098 | SmallVector<unsigned, 1> Vals; |
| 1099 | Vals.push_back(CurVersion); |
| 1100 | Stream.EmitRecord(bitc::MODULE_CODE_VERSION, Vals); |
| 1101 | } |
| 1102 | |
| 1103 | // Analyze the module, enumerating globals, functions, etc. |
| 1104 | ValueEnumerator VE(M); |
| 1105 | |
| 1106 | // Emit blockinfo, which defines the standard abbreviations etc. |
| 1107 | WriteBlockInfo(VE, Stream); |
| 1108 | |
| 1109 | // Emit information about parameter attributes. |
| 1110 | WriteParamAttrTable(VE, Stream); |
| 1111 | |
| 1112 | // Emit information describing all of the types in the module. |
| 1113 | WriteTypeTable(VE, Stream); |
| 1114 | |
| 1115 | // Emit top-level description of module, including target triple, inline asm, |
| 1116 | // descriptors for global variables, and function prototype info. |
| 1117 | WriteModuleInfo(M, VE, Stream); |
| 1118 | |
| 1119 | // Emit constants. |
| 1120 | WriteModuleConstants(VE, Stream); |
| 1121 | |
| 1122 | // If we have any aggregate values in the value table, purge them - these can |
| 1123 | // only be used to initialize global variables. Doing so makes the value |
| 1124 | // namespace smaller for code in functions. |
| 1125 | int NumNonAggregates = VE.PurgeAggregateValues(); |
| 1126 | if (NumNonAggregates != -1) { |
| 1127 | SmallVector<unsigned, 1> Vals; |
| 1128 | Vals.push_back(NumNonAggregates); |
| 1129 | Stream.EmitRecord(bitc::MODULE_CODE_PURGEVALS, Vals); |
| 1130 | } |
| 1131 | |
| 1132 | // Emit function bodies. |
| 1133 | for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 1134 | if (!I->isDeclaration()) |
| 1135 | WriteFunction(*I, VE, Stream); |
| 1136 | |
| 1137 | // Emit the type symbol table information. |
| 1138 | WriteTypeSymbolTable(M->getTypeSymbolTable(), VE, Stream); |
| 1139 | |
| 1140 | // Emit names for globals/functions etc. |
| 1141 | WriteValueSymbolTable(M->getValueSymbolTable(), VE, Stream); |
| 1142 | |
Chris Lattner | 07faafc | 2007-05-04 18:26:27 +0000 | [diff] [blame] | 1143 | Stream.ExitBlock(); |
| 1144 | } |
| 1145 | |
| 1146 | |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 1147 | /// WriteBitcodeToFile - Write the specified module to the specified output |
| 1148 | /// stream. |
| 1149 | void llvm::WriteBitcodeToFile(const Module *M, std::ostream &Out) { |
| 1150 | std::vector<unsigned char> Buffer; |
| 1151 | BitstreamWriter Stream(Buffer); |
| 1152 | |
| 1153 | Buffer.reserve(256*1024); |
| 1154 | |
| 1155 | // Emit the file header. |
| 1156 | Stream.Emit((unsigned)'B', 8); |
| 1157 | Stream.Emit((unsigned)'C', 8); |
| 1158 | Stream.Emit(0x0, 4); |
| 1159 | Stream.Emit(0xC, 4); |
| 1160 | Stream.Emit(0xE, 4); |
| 1161 | Stream.Emit(0xD, 4); |
| 1162 | |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 1163 | // Emit the module. |
| 1164 | WriteModule(M, Stream); |
| 1165 | |
| 1166 | // Write the generated bitstream to "Out". |
| 1167 | Out.write((char*)&Buffer.front(), Buffer.size()); |
| 1168 | } |