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