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