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