| Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1 | //===- BitcodeReader.cpp - Internal BitcodeReader implementation ----------===// | 
 | 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 | // This header defines the BitcodeReader class. | 
 | 11 | // | 
 | 12 | //===----------------------------------------------------------------------===// | 
 | 13 |  | 
| Chris Lattner | c453f76 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 14 | #include "llvm/Bitcode/ReaderWriter.h" | 
| Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 15 | #include "BitcodeReader.h" | 
| Chris Lattner | e16504e | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 16 | #include "llvm/Constants.h" | 
| Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 17 | #include "llvm/DerivedTypes.h" | 
| Chris Lattner | a7c49aa | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 18 | #include "llvm/Instructions.h" | 
| Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 19 | #include "llvm/Module.h" | 
| Chris Lattner | 48c85b8 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 20 | #include "llvm/ParameterAttributes.h" | 
| Chris Lattner | 0b2482a | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallString.h" | 
| Chris Lattner | 0eef080 | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 22 | #include "llvm/Support/MathExtras.h" | 
| Chris Lattner | c453f76 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 23 | #include "llvm/Support/MemoryBuffer.h" | 
| Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 24 | using namespace llvm; | 
 | 25 |  | 
| Chris Lattner | c453f76 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 26 | BitcodeReader::~BitcodeReader() { | 
 | 27 |   delete Buffer; | 
 | 28 | } | 
 | 29 |  | 
| Chris Lattner | 48c85b8 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 30 | //===----------------------------------------------------------------------===// | 
 | 31 | //  Helper functions to implement forward reference resolution, etc. | 
 | 32 | //===----------------------------------------------------------------------===// | 
| Chris Lattner | c453f76 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 33 |  | 
| Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 34 | /// ConvertToString - Convert a string from a record into an std::string, return | 
 | 35 | /// true on failure. | 
| Chris Lattner | 0b2482a | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 36 | template<typename StrTy> | 
| Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 37 | static bool ConvertToString(SmallVector<uint64_t, 64> &Record, unsigned Idx, | 
| Chris Lattner | 0b2482a | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 38 |                             StrTy &Result) { | 
| Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 39 |   if (Idx > Record.size()) | 
| Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 40 |     return true; | 
 | 41 |    | 
| Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 42 |   for (unsigned i = Idx, e = Record.size(); i != e; ++i) | 
 | 43 |     Result += (char)Record[i]; | 
| Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 44 |   return false; | 
 | 45 | } | 
 | 46 |  | 
 | 47 | static GlobalValue::LinkageTypes GetDecodedLinkage(unsigned Val) { | 
 | 48 |   switch (Val) { | 
 | 49 |   default: // Map unknown/new linkages to external | 
 | 50 |   case 0: return GlobalValue::ExternalLinkage; | 
 | 51 |   case 1: return GlobalValue::WeakLinkage; | 
 | 52 |   case 2: return GlobalValue::AppendingLinkage; | 
 | 53 |   case 3: return GlobalValue::InternalLinkage; | 
 | 54 |   case 4: return GlobalValue::LinkOnceLinkage; | 
 | 55 |   case 5: return GlobalValue::DLLImportLinkage; | 
 | 56 |   case 6: return GlobalValue::DLLExportLinkage; | 
 | 57 |   case 7: return GlobalValue::ExternalWeakLinkage; | 
 | 58 |   } | 
 | 59 | } | 
 | 60 |  | 
 | 61 | static GlobalValue::VisibilityTypes GetDecodedVisibility(unsigned Val) { | 
 | 62 |   switch (Val) { | 
 | 63 |   default: // Map unknown visibilities to default. | 
 | 64 |   case 0: return GlobalValue::DefaultVisibility; | 
 | 65 |   case 1: return GlobalValue::HiddenVisibility; | 
| Anton Korobeynikov | 9cd3ccf | 2007-04-29 20:56:48 +0000 | [diff] [blame] | 66 |   case 2: return GlobalValue::ProtectedVisibility; | 
| Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 67 |   } | 
 | 68 | } | 
 | 69 |  | 
| Chris Lattner | f581c3b | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 70 | static int GetDecodedCastOpcode(unsigned Val) { | 
 | 71 |   switch (Val) { | 
 | 72 |   default: return -1; | 
 | 73 |   case bitc::CAST_TRUNC   : return Instruction::Trunc; | 
 | 74 |   case bitc::CAST_ZEXT    : return Instruction::ZExt; | 
 | 75 |   case bitc::CAST_SEXT    : return Instruction::SExt; | 
 | 76 |   case bitc::CAST_FPTOUI  : return Instruction::FPToUI; | 
 | 77 |   case bitc::CAST_FPTOSI  : return Instruction::FPToSI; | 
 | 78 |   case bitc::CAST_UITOFP  : return Instruction::UIToFP; | 
 | 79 |   case bitc::CAST_SITOFP  : return Instruction::SIToFP; | 
 | 80 |   case bitc::CAST_FPTRUNC : return Instruction::FPTrunc; | 
 | 81 |   case bitc::CAST_FPEXT   : return Instruction::FPExt; | 
 | 82 |   case bitc::CAST_PTRTOINT: return Instruction::PtrToInt; | 
 | 83 |   case bitc::CAST_INTTOPTR: return Instruction::IntToPtr; | 
 | 84 |   case bitc::CAST_BITCAST : return Instruction::BitCast; | 
 | 85 |   } | 
 | 86 | } | 
 | 87 | static int GetDecodedBinaryOpcode(unsigned Val, const Type *Ty) { | 
 | 88 |   switch (Val) { | 
 | 89 |   default: return -1; | 
 | 90 |   case bitc::BINOP_ADD:  return Instruction::Add; | 
 | 91 |   case bitc::BINOP_SUB:  return Instruction::Sub; | 
 | 92 |   case bitc::BINOP_MUL:  return Instruction::Mul; | 
 | 93 |   case bitc::BINOP_UDIV: return Instruction::UDiv; | 
 | 94 |   case bitc::BINOP_SDIV: | 
 | 95 |     return Ty->isFPOrFPVector() ? Instruction::FDiv : Instruction::SDiv; | 
 | 96 |   case bitc::BINOP_UREM: return Instruction::URem; | 
 | 97 |   case bitc::BINOP_SREM: | 
 | 98 |     return Ty->isFPOrFPVector() ? Instruction::FRem : Instruction::SRem; | 
 | 99 |   case bitc::BINOP_SHL:  return Instruction::Shl; | 
 | 100 |   case bitc::BINOP_LSHR: return Instruction::LShr; | 
 | 101 |   case bitc::BINOP_ASHR: return Instruction::AShr; | 
 | 102 |   case bitc::BINOP_AND:  return Instruction::And; | 
 | 103 |   case bitc::BINOP_OR:   return Instruction::Or; | 
 | 104 |   case bitc::BINOP_XOR:  return Instruction::Xor; | 
 | 105 |   } | 
 | 106 | } | 
 | 107 |  | 
 | 108 |  | 
| Chris Lattner | 522b7b1 | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 109 | namespace { | 
 | 110 |   /// @brief A class for maintaining the slot number definition | 
 | 111 |   /// as a placeholder for the actual definition for forward constants defs. | 
 | 112 |   class ConstantPlaceHolder : public ConstantExpr { | 
 | 113 |     ConstantPlaceHolder();                       // DO NOT IMPLEMENT | 
 | 114 |     void operator=(const ConstantPlaceHolder &); // DO NOT IMPLEMENT | 
| Chris Lattner | f581c3b | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 115 |   public: | 
 | 116 |     Use Op; | 
 | 117 |     ConstantPlaceHolder(const Type *Ty) | 
 | 118 |       : ConstantExpr(Ty, Instruction::UserOp1, &Op, 1), | 
 | 119 |         Op(UndefValue::get(Type::Int32Ty), this) { | 
| Chris Lattner | 522b7b1 | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 120 |     } | 
 | 121 |   }; | 
 | 122 | } | 
 | 123 |  | 
 | 124 | Constant *BitcodeReaderValueList::getConstantFwdRef(unsigned Idx, | 
 | 125 |                                                     const Type *Ty) { | 
 | 126 |   if (Idx >= size()) { | 
 | 127 |     // Insert a bunch of null values. | 
 | 128 |     Uses.resize(Idx+1); | 
 | 129 |     OperandList = &Uses[0]; | 
 | 130 |     NumOperands = Idx+1; | 
 | 131 |   } | 
 | 132 |  | 
| Chris Lattner | a7c49aa | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 133 |   if (Value *V = Uses[Idx]) { | 
 | 134 |     assert(Ty == V->getType() && "Type mismatch in constant table!"); | 
 | 135 |     return cast<Constant>(V); | 
| Chris Lattner | f581c3b | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 136 |   } | 
| Chris Lattner | 522b7b1 | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 137 |  | 
 | 138 |   // Create and return a placeholder, which will later be RAUW'd. | 
 | 139 |   Constant *C = new ConstantPlaceHolder(Ty); | 
 | 140 |   Uses[Idx].init(C, this); | 
 | 141 |   return C; | 
 | 142 | } | 
 | 143 |  | 
| Chris Lattner | a7c49aa | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 144 | Value *BitcodeReaderValueList::getValueFwdRef(unsigned Idx, const Type *Ty) { | 
 | 145 |   if (Idx >= size()) { | 
 | 146 |     // Insert a bunch of null values. | 
 | 147 |     Uses.resize(Idx+1); | 
 | 148 |     OperandList = &Uses[0]; | 
 | 149 |     NumOperands = Idx+1; | 
 | 150 |   } | 
 | 151 |    | 
 | 152 |   if (Value *V = Uses[Idx]) { | 
 | 153 |     assert((Ty == 0 || Ty == V->getType()) && "Type mismatch in value table!"); | 
 | 154 |     return V; | 
 | 155 |   } | 
 | 156 |    | 
| Chris Lattner | 01ff65f | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 157 |   // No type specified, must be invalid reference. | 
 | 158 |   if (Ty == 0) return 0; | 
 | 159 |    | 
| Chris Lattner | a7c49aa | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 160 |   // Create and return a placeholder, which will later be RAUW'd. | 
 | 161 |   Value *V = new Argument(Ty); | 
 | 162 |   Uses[Idx].init(V, this); | 
 | 163 |   return V; | 
 | 164 | } | 
 | 165 |  | 
| Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 166 |  | 
 | 167 | const Type *BitcodeReader::getTypeByID(unsigned ID, bool isTypeTable) { | 
 | 168 |   // If the TypeID is in range, return it. | 
 | 169 |   if (ID < TypeList.size()) | 
 | 170 |     return TypeList[ID].get(); | 
 | 171 |   if (!isTypeTable) return 0; | 
 | 172 |    | 
 | 173 |   // The type table allows forward references.  Push as many Opaque types as | 
 | 174 |   // needed to get up to ID. | 
 | 175 |   while (TypeList.size() <= ID) | 
 | 176 |     TypeList.push_back(OpaqueType::get()); | 
 | 177 |   return TypeList.back().get(); | 
 | 178 | } | 
 | 179 |  | 
| Chris Lattner | 48c85b8 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 180 | //===----------------------------------------------------------------------===// | 
 | 181 | //  Functions for parsing blocks from the bitcode file | 
 | 182 | //===----------------------------------------------------------------------===// | 
 | 183 |  | 
 | 184 | bool BitcodeReader::ParseParamAttrBlock() { | 
| Chris Lattner | e17b658 | 2007-05-05 00:17:00 +0000 | [diff] [blame] | 185 |   if (Stream.EnterSubBlock(bitc::PARAMATTR_BLOCK_ID)) | 
| Chris Lattner | 48c85b8 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 186 |     return Error("Malformed block record"); | 
 | 187 |    | 
 | 188 |   if (!ParamAttrs.empty()) | 
 | 189 |     return Error("Multiple PARAMATTR blocks found!"); | 
 | 190 |    | 
 | 191 |   SmallVector<uint64_t, 64> Record; | 
 | 192 |    | 
 | 193 |   ParamAttrsVector Attrs; | 
 | 194 |    | 
 | 195 |   // Read all the records. | 
 | 196 |   while (1) { | 
 | 197 |     unsigned Code = Stream.ReadCode(); | 
 | 198 |     if (Code == bitc::END_BLOCK) { | 
 | 199 |       if (Stream.ReadBlockEnd()) | 
 | 200 |         return Error("Error at end of PARAMATTR block"); | 
 | 201 |       return false; | 
 | 202 |     } | 
 | 203 |      | 
 | 204 |     if (Code == bitc::ENTER_SUBBLOCK) { | 
 | 205 |       // No known subblocks, always skip them. | 
 | 206 |       Stream.ReadSubBlockID(); | 
 | 207 |       if (Stream.SkipBlock()) | 
 | 208 |         return Error("Malformed block record"); | 
 | 209 |       continue; | 
 | 210 |     } | 
 | 211 |      | 
 | 212 |     if (Code == bitc::DEFINE_ABBREV) { | 
 | 213 |       Stream.ReadAbbrevRecord(); | 
 | 214 |       continue; | 
 | 215 |     } | 
 | 216 |      | 
 | 217 |     // Read a record. | 
 | 218 |     Record.clear(); | 
 | 219 |     switch (Stream.ReadRecord(Code, Record)) { | 
 | 220 |     default:  // Default behavior: ignore. | 
 | 221 |       break; | 
 | 222 |     case bitc::PARAMATTR_CODE_ENTRY: { // ENTRY: [paramidx0, attr0, ...] | 
 | 223 |       if (Record.size() & 1) | 
 | 224 |         return Error("Invalid ENTRY record"); | 
 | 225 |  | 
 | 226 |       ParamAttrsWithIndex PAWI; | 
 | 227 |       for (unsigned i = 0, e = Record.size(); i != e; i += 2) { | 
 | 228 |         PAWI.index = Record[i]; | 
 | 229 |         PAWI.attrs = Record[i+1]; | 
 | 230 |         Attrs.push_back(PAWI); | 
 | 231 |       } | 
 | 232 |       ParamAttrs.push_back(ParamAttrsList::get(Attrs)); | 
 | 233 |       Attrs.clear(); | 
 | 234 |       break; | 
 | 235 |     } | 
 | 236 |     }     | 
 | 237 |   } | 
 | 238 | } | 
 | 239 |  | 
 | 240 |  | 
| Chris Lattner | 8669714 | 2007-05-01 05:01:34 +0000 | [diff] [blame] | 241 | bool BitcodeReader::ParseTypeTable() { | 
| Chris Lattner | e17b658 | 2007-05-05 00:17:00 +0000 | [diff] [blame] | 242 |   if (Stream.EnterSubBlock(bitc::TYPE_BLOCK_ID)) | 
| Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 243 |     return Error("Malformed block record"); | 
 | 244 |    | 
 | 245 |   if (!TypeList.empty()) | 
 | 246 |     return Error("Multiple TYPE_BLOCKs found!"); | 
 | 247 |  | 
 | 248 |   SmallVector<uint64_t, 64> Record; | 
 | 249 |   unsigned NumRecords = 0; | 
 | 250 |  | 
 | 251 |   // Read all the records for this type table. | 
 | 252 |   while (1) { | 
 | 253 |     unsigned Code = Stream.ReadCode(); | 
 | 254 |     if (Code == bitc::END_BLOCK) { | 
 | 255 |       if (NumRecords != TypeList.size()) | 
 | 256 |         return Error("Invalid type forward reference in TYPE_BLOCK"); | 
| Chris Lattner | f66d20d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 257 |       if (Stream.ReadBlockEnd()) | 
 | 258 |         return Error("Error at end of type table block"); | 
 | 259 |       return false; | 
| Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 260 |     } | 
 | 261 |      | 
 | 262 |     if (Code == bitc::ENTER_SUBBLOCK) { | 
 | 263 |       // No known subblocks, always skip them. | 
 | 264 |       Stream.ReadSubBlockID(); | 
 | 265 |       if (Stream.SkipBlock()) | 
 | 266 |         return Error("Malformed block record"); | 
 | 267 |       continue; | 
 | 268 |     } | 
 | 269 |      | 
| Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 270 |     if (Code == bitc::DEFINE_ABBREV) { | 
| Chris Lattner | d127c1b | 2007-04-23 18:58:34 +0000 | [diff] [blame] | 271 |       Stream.ReadAbbrevRecord(); | 
 | 272 |       continue; | 
| Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 273 |     } | 
 | 274 |      | 
 | 275 |     // Read a record. | 
 | 276 |     Record.clear(); | 
 | 277 |     const Type *ResultTy = 0; | 
 | 278 |     switch (Stream.ReadRecord(Code, Record)) { | 
 | 279 |     default:  // Default behavior: unknown type. | 
 | 280 |       ResultTy = 0; | 
 | 281 |       break; | 
 | 282 |     case bitc::TYPE_CODE_NUMENTRY: // TYPE_CODE_NUMENTRY: [numentries] | 
 | 283 |       // TYPE_CODE_NUMENTRY contains a count of the number of types in the | 
 | 284 |       // type list.  This allows us to reserve space. | 
 | 285 |       if (Record.size() < 1) | 
 | 286 |         return Error("Invalid TYPE_CODE_NUMENTRY record"); | 
 | 287 |       TypeList.reserve(Record[0]); | 
 | 288 |       continue; | 
| Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 289 |     case bitc::TYPE_CODE_VOID:      // VOID | 
 | 290 |       ResultTy = Type::VoidTy; | 
 | 291 |       break; | 
 | 292 |     case bitc::TYPE_CODE_FLOAT:     // FLOAT | 
 | 293 |       ResultTy = Type::FloatTy; | 
 | 294 |       break; | 
 | 295 |     case bitc::TYPE_CODE_DOUBLE:    // DOUBLE | 
 | 296 |       ResultTy = Type::DoubleTy; | 
 | 297 |       break; | 
 | 298 |     case bitc::TYPE_CODE_LABEL:     // LABEL | 
 | 299 |       ResultTy = Type::LabelTy; | 
 | 300 |       break; | 
 | 301 |     case bitc::TYPE_CODE_OPAQUE:    // OPAQUE | 
 | 302 |       ResultTy = 0; | 
 | 303 |       break; | 
 | 304 |     case bitc::TYPE_CODE_INTEGER:   // INTEGER: [width] | 
 | 305 |       if (Record.size() < 1) | 
 | 306 |         return Error("Invalid Integer type record"); | 
 | 307 |        | 
 | 308 |       ResultTy = IntegerType::get(Record[0]); | 
 | 309 |       break; | 
 | 310 |     case bitc::TYPE_CODE_POINTER:   // POINTER: [pointee type] | 
 | 311 |       if (Record.size() < 1) | 
 | 312 |         return Error("Invalid POINTER type record"); | 
 | 313 |       ResultTy = PointerType::get(getTypeByID(Record[0], true)); | 
 | 314 |       break; | 
 | 315 |     case bitc::TYPE_CODE_FUNCTION: { | 
| Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 316 |       // FUNCTION: [vararg, attrid, retty, paramty x N] | 
 | 317 |       if (Record.size() < 3) | 
| Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 318 |         return Error("Invalid FUNCTION type record"); | 
 | 319 |       std::vector<const Type*> ArgTys; | 
| Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 320 |       for (unsigned i = 3, e = Record.size(); i != e; ++i) | 
 | 321 |         ArgTys.push_back(getTypeByID(Record[i], true)); | 
| Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 322 |        | 
| Chris Lattner | 9113e73 | 2007-05-04 03:41:34 +0000 | [diff] [blame] | 323 |       ResultTy = FunctionType::get(getTypeByID(Record[2], true), ArgTys, | 
 | 324 |                                    Record[0], getParamAttrs(Record[1])); | 
| Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 325 |       break; | 
 | 326 |     } | 
| Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 327 |     case bitc::TYPE_CODE_STRUCT: {  // STRUCT: [ispacked, eltty x N] | 
 | 328 |       if (Record.size() < 2) | 
| Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 329 |         return Error("Invalid STRUCT type record"); | 
 | 330 |       std::vector<const Type*> EltTys; | 
| Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 331 |       for (unsigned i = 1, e = Record.size(); i != e; ++i) | 
 | 332 |         EltTys.push_back(getTypeByID(Record[i], true)); | 
| Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 333 |       ResultTy = StructType::get(EltTys, Record[0]); | 
 | 334 |       break; | 
 | 335 |     } | 
 | 336 |     case bitc::TYPE_CODE_ARRAY:     // ARRAY: [numelts, eltty] | 
 | 337 |       if (Record.size() < 2) | 
 | 338 |         return Error("Invalid ARRAY type record"); | 
 | 339 |       ResultTy = ArrayType::get(getTypeByID(Record[1], true), Record[0]); | 
 | 340 |       break; | 
 | 341 |     case bitc::TYPE_CODE_VECTOR:    // VECTOR: [numelts, eltty] | 
 | 342 |       if (Record.size() < 2) | 
 | 343 |         return Error("Invalid VECTOR type record"); | 
 | 344 |       ResultTy = VectorType::get(getTypeByID(Record[1], true), Record[0]); | 
 | 345 |       break; | 
 | 346 |     } | 
 | 347 |      | 
 | 348 |     if (NumRecords == TypeList.size()) { | 
 | 349 |       // If this is a new type slot, just append it. | 
 | 350 |       TypeList.push_back(ResultTy ? ResultTy : OpaqueType::get()); | 
 | 351 |       ++NumRecords; | 
 | 352 |     } else if (ResultTy == 0) { | 
 | 353 |       // Otherwise, this was forward referenced, so an opaque type was created, | 
 | 354 |       // but the result type is actually just an opaque.  Leave the one we | 
 | 355 |       // created previously. | 
 | 356 |       ++NumRecords; | 
 | 357 |     } else { | 
 | 358 |       // Otherwise, this was forward referenced, so an opaque type was created. | 
 | 359 |       // Resolve the opaque type to the real type now. | 
 | 360 |       assert(NumRecords < TypeList.size() && "Typelist imbalance"); | 
 | 361 |       const OpaqueType *OldTy = cast<OpaqueType>(TypeList[NumRecords++].get()); | 
 | 362 |       | 
 | 363 |       // Don't directly push the new type on the Tab. Instead we want to replace | 
 | 364 |       // the opaque type we previously inserted with the new concrete value. The | 
 | 365 |       // refinement from the abstract (opaque) type to the new type causes all | 
 | 366 |       // uses of the abstract type to use the concrete type (NewTy). This will | 
 | 367 |       // also cause the opaque type to be deleted. | 
 | 368 |       const_cast<OpaqueType*>(OldTy)->refineAbstractTypeTo(ResultTy); | 
 | 369 |        | 
 | 370 |       // This should have replaced the old opaque type with the new type in the | 
| Chris Lattner | 0eef080 | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 371 |       // value table... or with a preexisting type that was already in the | 
 | 372 |       // system.  Let's just make sure it did. | 
| Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 373 |       assert(TypeList[NumRecords-1].get() != OldTy && | 
 | 374 |              "refineAbstractType didn't work!"); | 
 | 375 |     } | 
 | 376 |   } | 
 | 377 | } | 
 | 378 |  | 
 | 379 |  | 
| Chris Lattner | 8669714 | 2007-05-01 05:01:34 +0000 | [diff] [blame] | 380 | bool BitcodeReader::ParseTypeSymbolTable() { | 
| Chris Lattner | e17b658 | 2007-05-05 00:17:00 +0000 | [diff] [blame] | 381 |   if (Stream.EnterSubBlock(bitc::TYPE_SYMTAB_BLOCK_ID)) | 
| Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 382 |     return Error("Malformed block record"); | 
 | 383 |    | 
 | 384 |   SmallVector<uint64_t, 64> Record; | 
 | 385 |    | 
 | 386 |   // Read all the records for this type table. | 
 | 387 |   std::string TypeName; | 
 | 388 |   while (1) { | 
 | 389 |     unsigned Code = Stream.ReadCode(); | 
| Chris Lattner | f66d20d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 390 |     if (Code == bitc::END_BLOCK) { | 
 | 391 |       if (Stream.ReadBlockEnd()) | 
 | 392 |         return Error("Error at end of type symbol table block"); | 
 | 393 |       return false; | 
 | 394 |     } | 
| Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 395 |      | 
 | 396 |     if (Code == bitc::ENTER_SUBBLOCK) { | 
 | 397 |       // No known subblocks, always skip them. | 
 | 398 |       Stream.ReadSubBlockID(); | 
 | 399 |       if (Stream.SkipBlock()) | 
 | 400 |         return Error("Malformed block record"); | 
 | 401 |       continue; | 
 | 402 |     } | 
 | 403 |      | 
| Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 404 |     if (Code == bitc::DEFINE_ABBREV) { | 
| Chris Lattner | d127c1b | 2007-04-23 18:58:34 +0000 | [diff] [blame] | 405 |       Stream.ReadAbbrevRecord(); | 
 | 406 |       continue; | 
| Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 407 |     } | 
 | 408 |      | 
 | 409 |     // Read a record. | 
 | 410 |     Record.clear(); | 
 | 411 |     switch (Stream.ReadRecord(Code, Record)) { | 
 | 412 |     default:  // Default behavior: unknown type. | 
 | 413 |       break; | 
| Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 414 |     case bitc::TST_CODE_ENTRY:    // TST_ENTRY: [typeid, namechar x N] | 
| Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 415 |       if (ConvertToString(Record, 1, TypeName)) | 
 | 416 |         return Error("Invalid TST_ENTRY record"); | 
 | 417 |       unsigned TypeID = Record[0]; | 
 | 418 |       if (TypeID >= TypeList.size()) | 
 | 419 |         return Error("Invalid Type ID in TST_ENTRY record"); | 
 | 420 |  | 
 | 421 |       TheModule->addTypeName(TypeName, TypeList[TypeID].get()); | 
 | 422 |       TypeName.clear(); | 
 | 423 |       break; | 
 | 424 |     } | 
 | 425 |   } | 
 | 426 | } | 
 | 427 |  | 
| Chris Lattner | 8669714 | 2007-05-01 05:01:34 +0000 | [diff] [blame] | 428 | bool BitcodeReader::ParseValueSymbolTable() { | 
| Chris Lattner | e17b658 | 2007-05-05 00:17:00 +0000 | [diff] [blame] | 429 |   if (Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID)) | 
| Chris Lattner | 0b2482a | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 430 |     return Error("Malformed block record"); | 
 | 431 |  | 
 | 432 |   SmallVector<uint64_t, 64> Record; | 
 | 433 |    | 
 | 434 |   // Read all the records for this value table. | 
 | 435 |   SmallString<128> ValueName; | 
 | 436 |   while (1) { | 
 | 437 |     unsigned Code = Stream.ReadCode(); | 
| Chris Lattner | f66d20d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 438 |     if (Code == bitc::END_BLOCK) { | 
 | 439 |       if (Stream.ReadBlockEnd()) | 
 | 440 |         return Error("Error at end of value symbol table block"); | 
 | 441 |       return false; | 
 | 442 |     }     | 
| Chris Lattner | 0b2482a | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 443 |     if (Code == bitc::ENTER_SUBBLOCK) { | 
 | 444 |       // No known subblocks, always skip them. | 
 | 445 |       Stream.ReadSubBlockID(); | 
 | 446 |       if (Stream.SkipBlock()) | 
 | 447 |         return Error("Malformed block record"); | 
 | 448 |       continue; | 
 | 449 |     } | 
 | 450 |      | 
 | 451 |     if (Code == bitc::DEFINE_ABBREV) { | 
 | 452 |       Stream.ReadAbbrevRecord(); | 
 | 453 |       continue; | 
 | 454 |     } | 
 | 455 |      | 
 | 456 |     // Read a record. | 
 | 457 |     Record.clear(); | 
 | 458 |     switch (Stream.ReadRecord(Code, Record)) { | 
 | 459 |     default:  // Default behavior: unknown type. | 
 | 460 |       break; | 
| Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 461 |     case bitc::VST_CODE_ENTRY: {  // VST_ENTRY: [valueid, namechar x N] | 
| Chris Lattner | 0b2482a | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 462 |       if (ConvertToString(Record, 1, ValueName)) | 
 | 463 |         return Error("Invalid TST_ENTRY record"); | 
 | 464 |       unsigned ValueID = Record[0]; | 
 | 465 |       if (ValueID >= ValueList.size()) | 
 | 466 |         return Error("Invalid Value ID in VST_ENTRY record"); | 
 | 467 |       Value *V = ValueList[ValueID]; | 
 | 468 |        | 
 | 469 |       V->setName(&ValueName[0], ValueName.size()); | 
 | 470 |       ValueName.clear(); | 
 | 471 |       break; | 
| Reid Spencer | c8f8a24 | 2007-05-04 01:43:33 +0000 | [diff] [blame] | 472 |     } | 
 | 473 |     case bitc::VST_CODE_BBENTRY: { | 
| Chris Lattner | e825ed5 | 2007-05-03 22:18:21 +0000 | [diff] [blame] | 474 |       if (ConvertToString(Record, 1, ValueName)) | 
 | 475 |         return Error("Invalid VST_BBENTRY record"); | 
 | 476 |       BasicBlock *BB = getBasicBlock(Record[0]); | 
 | 477 |       if (BB == 0) | 
 | 478 |         return Error("Invalid BB ID in VST_BBENTRY record"); | 
 | 479 |        | 
 | 480 |       BB->setName(&ValueName[0], ValueName.size()); | 
 | 481 |       ValueName.clear(); | 
 | 482 |       break; | 
| Chris Lattner | 0b2482a | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 483 |     } | 
| Reid Spencer | c8f8a24 | 2007-05-04 01:43:33 +0000 | [diff] [blame] | 484 |     } | 
| Chris Lattner | 0b2482a | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 485 |   } | 
 | 486 | } | 
 | 487 |  | 
| Chris Lattner | 0eef080 | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 488 | /// DecodeSignRotatedValue - Decode a signed value stored with the sign bit in | 
 | 489 | /// the LSB for dense VBR encoding. | 
 | 490 | static uint64_t DecodeSignRotatedValue(uint64_t V) { | 
 | 491 |   if ((V & 1) == 0) | 
 | 492 |     return V >> 1; | 
 | 493 |   if (V != 1)  | 
 | 494 |     return -(V >> 1); | 
 | 495 |   // There is no such thing as -0 with integers.  "-0" really means MININT. | 
 | 496 |   return 1ULL << 63; | 
 | 497 | } | 
 | 498 |  | 
| Chris Lattner | 07d98b4 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 499 | /// ResolveGlobalAndAliasInits - Resolve all of the initializers for global | 
 | 500 | /// values and aliases that we can. | 
 | 501 | bool BitcodeReader::ResolveGlobalAndAliasInits() { | 
 | 502 |   std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInitWorklist; | 
 | 503 |   std::vector<std::pair<GlobalAlias*, unsigned> > AliasInitWorklist; | 
 | 504 |    | 
 | 505 |   GlobalInitWorklist.swap(GlobalInits); | 
 | 506 |   AliasInitWorklist.swap(AliasInits); | 
 | 507 |  | 
 | 508 |   while (!GlobalInitWorklist.empty()) { | 
| Chris Lattner | 198f34a | 2007-04-26 03:27:58 +0000 | [diff] [blame] | 509 |     unsigned ValID = GlobalInitWorklist.back().second; | 
| Chris Lattner | 07d98b4 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 510 |     if (ValID >= ValueList.size()) { | 
 | 511 |       // Not ready to resolve this yet, it requires something later in the file. | 
| Chris Lattner | 198f34a | 2007-04-26 03:27:58 +0000 | [diff] [blame] | 512 |       GlobalInits.push_back(GlobalInitWorklist.back()); | 
| Chris Lattner | 07d98b4 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 513 |     } else { | 
 | 514 |       if (Constant *C = dyn_cast<Constant>(ValueList[ValID])) | 
 | 515 |         GlobalInitWorklist.back().first->setInitializer(C); | 
 | 516 |       else | 
 | 517 |         return Error("Global variable initializer is not a constant!"); | 
 | 518 |     } | 
 | 519 |     GlobalInitWorklist.pop_back();  | 
 | 520 |   } | 
 | 521 |  | 
 | 522 |   while (!AliasInitWorklist.empty()) { | 
 | 523 |     unsigned ValID = AliasInitWorklist.back().second; | 
 | 524 |     if (ValID >= ValueList.size()) { | 
 | 525 |       AliasInits.push_back(AliasInitWorklist.back()); | 
 | 526 |     } else { | 
 | 527 |       if (Constant *C = dyn_cast<Constant>(ValueList[ValID])) | 
| Anton Korobeynikov | 7dde0ff | 2007-04-28 14:57:59 +0000 | [diff] [blame] | 528 |         AliasInitWorklist.back().first->setAliasee(C); | 
| Chris Lattner | 07d98b4 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 529 |       else | 
 | 530 |         return Error("Alias initializer is not a constant!"); | 
 | 531 |     } | 
 | 532 |     AliasInitWorklist.pop_back();  | 
 | 533 |   } | 
 | 534 |   return false; | 
 | 535 | } | 
 | 536 |  | 
 | 537 |  | 
| Chris Lattner | 8669714 | 2007-05-01 05:01:34 +0000 | [diff] [blame] | 538 | bool BitcodeReader::ParseConstants() { | 
| Chris Lattner | e17b658 | 2007-05-05 00:17:00 +0000 | [diff] [blame] | 539 |   if (Stream.EnterSubBlock(bitc::CONSTANTS_BLOCK_ID)) | 
| Chris Lattner | e16504e | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 540 |     return Error("Malformed block record"); | 
 | 541 |  | 
 | 542 |   SmallVector<uint64_t, 64> Record; | 
 | 543 |    | 
 | 544 |   // Read all the records for this value table. | 
 | 545 |   const Type *CurTy = Type::Int32Ty; | 
| Chris Lattner | 522b7b1 | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 546 |   unsigned NextCstNo = ValueList.size(); | 
| Chris Lattner | e16504e | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 547 |   while (1) { | 
 | 548 |     unsigned Code = Stream.ReadCode(); | 
 | 549 |     if (Code == bitc::END_BLOCK) { | 
| Chris Lattner | 522b7b1 | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 550 |       if (NextCstNo != ValueList.size()) | 
 | 551 |         return Error("Invalid constant reference!"); | 
 | 552 |        | 
| Chris Lattner | f66d20d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 553 |       if (Stream.ReadBlockEnd()) | 
 | 554 |         return Error("Error at end of constants block"); | 
 | 555 |       return false; | 
| Chris Lattner | e16504e | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 556 |     } | 
 | 557 |      | 
 | 558 |     if (Code == bitc::ENTER_SUBBLOCK) { | 
 | 559 |       // No known subblocks, always skip them. | 
 | 560 |       Stream.ReadSubBlockID(); | 
 | 561 |       if (Stream.SkipBlock()) | 
 | 562 |         return Error("Malformed block record"); | 
 | 563 |       continue; | 
 | 564 |     } | 
 | 565 |      | 
 | 566 |     if (Code == bitc::DEFINE_ABBREV) { | 
 | 567 |       Stream.ReadAbbrevRecord(); | 
 | 568 |       continue; | 
 | 569 |     } | 
 | 570 |      | 
 | 571 |     // Read a record. | 
 | 572 |     Record.clear(); | 
 | 573 |     Value *V = 0; | 
 | 574 |     switch (Stream.ReadRecord(Code, Record)) { | 
 | 575 |     default:  // Default behavior: unknown constant | 
 | 576 |     case bitc::CST_CODE_UNDEF:     // UNDEF | 
 | 577 |       V = UndefValue::get(CurTy); | 
 | 578 |       break; | 
 | 579 |     case bitc::CST_CODE_SETTYPE:   // SETTYPE: [typeid] | 
 | 580 |       if (Record.empty()) | 
 | 581 |         return Error("Malformed CST_SETTYPE record"); | 
 | 582 |       if (Record[0] >= TypeList.size()) | 
 | 583 |         return Error("Invalid Type ID in CST_SETTYPE record"); | 
 | 584 |       CurTy = TypeList[Record[0]]; | 
| Chris Lattner | 0eef080 | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 585 |       continue;  // Skip the ValueList manipulation. | 
| Chris Lattner | e16504e | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 586 |     case bitc::CST_CODE_NULL:      // NULL | 
 | 587 |       V = Constant::getNullValue(CurTy); | 
 | 588 |       break; | 
 | 589 |     case bitc::CST_CODE_INTEGER:   // INTEGER: [intval] | 
| Chris Lattner | 0eef080 | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 590 |       if (!isa<IntegerType>(CurTy) || Record.empty()) | 
 | 591 |         return Error("Invalid CST_INTEGER record"); | 
 | 592 |       V = ConstantInt::get(CurTy, DecodeSignRotatedValue(Record[0])); | 
 | 593 |       break; | 
| Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 594 |     case bitc::CST_CODE_WIDE_INTEGER: {// WIDE_INTEGER: [n x intval] | 
 | 595 |       if (!isa<IntegerType>(CurTy) || Record.empty()) | 
| Chris Lattner | 0eef080 | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 596 |         return Error("Invalid WIDE_INTEGER record"); | 
 | 597 |        | 
| Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 598 |       unsigned NumWords = Record.size(); | 
| Chris Lattner | 084a844 | 2007-04-24 17:22:05 +0000 | [diff] [blame] | 599 |       SmallVector<uint64_t, 8> Words; | 
 | 600 |       Words.resize(NumWords); | 
| Chris Lattner | 0eef080 | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 601 |       for (unsigned i = 0; i != NumWords; ++i) | 
| Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 602 |         Words[i] = DecodeSignRotatedValue(Record[i]); | 
| Chris Lattner | 0eef080 | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 603 |       V = ConstantInt::get(APInt(cast<IntegerType>(CurTy)->getBitWidth(), | 
| Chris Lattner | 084a844 | 2007-04-24 17:22:05 +0000 | [diff] [blame] | 604 |                                  NumWords, &Words[0])); | 
| Chris Lattner | 0eef080 | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 605 |       break; | 
 | 606 |     } | 
 | 607 |     case bitc::CST_CODE_FLOAT:     // FLOAT: [fpval] | 
 | 608 |       if (Record.empty()) | 
 | 609 |         return Error("Invalid FLOAT record"); | 
 | 610 |       if (CurTy == Type::FloatTy) | 
 | 611 |         V = ConstantFP::get(CurTy, BitsToFloat(Record[0])); | 
 | 612 |       else if (CurTy == Type::DoubleTy) | 
 | 613 |         V = ConstantFP::get(CurTy, BitsToDouble(Record[0])); | 
| Chris Lattner | e16504e | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 614 |       else | 
| Chris Lattner | 0eef080 | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 615 |         V = UndefValue::get(CurTy); | 
| Chris Lattner | e16504e | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 616 |       break; | 
| Chris Lattner | 522b7b1 | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 617 |        | 
| Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 618 |     case bitc::CST_CODE_AGGREGATE: {// AGGREGATE: [n x value number] | 
 | 619 |       if (Record.empty()) | 
| Chris Lattner | 522b7b1 | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 620 |         return Error("Invalid CST_AGGREGATE record"); | 
 | 621 |        | 
| Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 622 |       unsigned Size = Record.size(); | 
| Chris Lattner | 522b7b1 | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 623 |       std::vector<Constant*> Elts; | 
 | 624 |        | 
 | 625 |       if (const StructType *STy = dyn_cast<StructType>(CurTy)) { | 
 | 626 |         for (unsigned i = 0; i != Size; ++i) | 
| Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 627 |           Elts.push_back(ValueList.getConstantFwdRef(Record[i], | 
| Chris Lattner | 522b7b1 | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 628 |                                                      STy->getElementType(i))); | 
 | 629 |         V = ConstantStruct::get(STy, Elts); | 
 | 630 |       } else if (const ArrayType *ATy = dyn_cast<ArrayType>(CurTy)) { | 
 | 631 |         const Type *EltTy = ATy->getElementType(); | 
 | 632 |         for (unsigned i = 0; i != Size; ++i) | 
| Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 633 |           Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy)); | 
| Chris Lattner | 522b7b1 | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 634 |         V = ConstantArray::get(ATy, Elts); | 
 | 635 |       } else if (const VectorType *VTy = dyn_cast<VectorType>(CurTy)) { | 
 | 636 |         const Type *EltTy = VTy->getElementType(); | 
 | 637 |         for (unsigned i = 0; i != Size; ++i) | 
| Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 638 |           Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy)); | 
| Chris Lattner | 522b7b1 | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 639 |         V = ConstantVector::get(Elts); | 
 | 640 |       } else { | 
 | 641 |         V = UndefValue::get(CurTy); | 
 | 642 |       } | 
| Chris Lattner | f581c3b | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 643 |       break; | 
 | 644 |     } | 
| Chris Lattner | ff7fc5d | 2007-05-06 00:35:24 +0000 | [diff] [blame] | 645 |     case bitc::CST_CODE_STRING: { // STRING: [values] | 
 | 646 |       if (Record.empty()) | 
 | 647 |         return Error("Invalid CST_AGGREGATE record"); | 
| Chris Lattner | f581c3b | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 648 |  | 
| Chris Lattner | ff7fc5d | 2007-05-06 00:35:24 +0000 | [diff] [blame] | 649 |       const ArrayType *ATy = cast<ArrayType>(CurTy); | 
 | 650 |       const Type *EltTy = ATy->getElementType(); | 
 | 651 |        | 
 | 652 |       unsigned Size = Record.size(); | 
 | 653 |       std::vector<Constant*> Elts; | 
 | 654 |        | 
 | 655 |       for (unsigned i = 0; i != Size; ++i) | 
 | 656 |         Elts.push_back(ConstantInt::get(EltTy, Record[i])); | 
 | 657 |       V = ConstantArray::get(ATy, Elts); | 
 | 658 |       break; | 
 | 659 |     } | 
| Chris Lattner | f581c3b | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 660 |     case bitc::CST_CODE_CE_BINOP: {  // CE_BINOP: [opcode, opval, opval] | 
 | 661 |       if (Record.size() < 3) return Error("Invalid CE_BINOP record"); | 
 | 662 |       int Opc = GetDecodedBinaryOpcode(Record[0], CurTy); | 
| Chris Lattner | f66d20d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 663 |       if (Opc < 0) { | 
 | 664 |         V = UndefValue::get(CurTy);  // Unknown binop. | 
 | 665 |       } else { | 
 | 666 |         Constant *LHS = ValueList.getConstantFwdRef(Record[1], CurTy); | 
 | 667 |         Constant *RHS = ValueList.getConstantFwdRef(Record[2], CurTy); | 
 | 668 |         V = ConstantExpr::get(Opc, LHS, RHS); | 
 | 669 |       } | 
| Chris Lattner | f581c3b | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 670 |       break; | 
 | 671 |     }   | 
 | 672 |     case bitc::CST_CODE_CE_CAST: {  // CE_CAST: [opcode, opty, opval] | 
 | 673 |       if (Record.size() < 3) return Error("Invalid CE_CAST record"); | 
 | 674 |       int Opc = GetDecodedCastOpcode(Record[0]); | 
| Chris Lattner | f66d20d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 675 |       if (Opc < 0) { | 
 | 676 |         V = UndefValue::get(CurTy);  // Unknown cast. | 
 | 677 |       } else { | 
 | 678 |         const Type *OpTy = getTypeByID(Record[1]); | 
 | 679 |         Constant *Op = ValueList.getConstantFwdRef(Record[2], OpTy); | 
 | 680 |         V = ConstantExpr::getCast(Opc, Op, CurTy); | 
 | 681 |       } | 
| Chris Lattner | f581c3b | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 682 |       break; | 
 | 683 |     }   | 
 | 684 |     case bitc::CST_CODE_CE_GEP: {  // CE_GEP:        [n x operands] | 
| Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 685 |       if (Record.size() & 1) return Error("Invalid CE_GEP record"); | 
| Chris Lattner | f581c3b | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 686 |       SmallVector<Constant*, 16> Elts; | 
| Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 687 |       for (unsigned i = 0, e = Record.size(); i != e; i += 2) { | 
| Chris Lattner | f581c3b | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 688 |         const Type *ElTy = getTypeByID(Record[i]); | 
 | 689 |         if (!ElTy) return Error("Invalid CE_GEP record"); | 
 | 690 |         Elts.push_back(ValueList.getConstantFwdRef(Record[i+1], ElTy)); | 
 | 691 |       } | 
| Chris Lattner | f66d20d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 692 |       V = ConstantExpr::getGetElementPtr(Elts[0], &Elts[1], Elts.size()-1); | 
 | 693 |       break; | 
| Chris Lattner | f581c3b | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 694 |     } | 
 | 695 |     case bitc::CST_CODE_CE_SELECT:  // CE_SELECT: [opval#, opval#, opval#] | 
 | 696 |       if (Record.size() < 3) return Error("Invalid CE_SELECT record"); | 
 | 697 |       V = ConstantExpr::getSelect(ValueList.getConstantFwdRef(Record[0], | 
 | 698 |                                                               Type::Int1Ty), | 
 | 699 |                                   ValueList.getConstantFwdRef(Record[1],CurTy), | 
 | 700 |                                   ValueList.getConstantFwdRef(Record[2],CurTy)); | 
 | 701 |       break; | 
 | 702 |     case bitc::CST_CODE_CE_EXTRACTELT: { // CE_EXTRACTELT: [opty, opval, opval] | 
 | 703 |       if (Record.size() < 3) return Error("Invalid CE_EXTRACTELT record"); | 
 | 704 |       const VectorType *OpTy =  | 
 | 705 |         dyn_cast_or_null<VectorType>(getTypeByID(Record[0])); | 
 | 706 |       if (OpTy == 0) return Error("Invalid CE_EXTRACTELT record"); | 
 | 707 |       Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy); | 
 | 708 |       Constant *Op1 = ValueList.getConstantFwdRef(Record[2], | 
 | 709 |                                                   OpTy->getElementType()); | 
 | 710 |       V = ConstantExpr::getExtractElement(Op0, Op1); | 
 | 711 |       break; | 
 | 712 |     } | 
 | 713 |     case bitc::CST_CODE_CE_INSERTELT: { // CE_INSERTELT: [opval, opval, opval] | 
 | 714 |       const VectorType *OpTy = dyn_cast<VectorType>(CurTy); | 
 | 715 |       if (Record.size() < 3 || OpTy == 0) | 
 | 716 |         return Error("Invalid CE_INSERTELT record"); | 
 | 717 |       Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy); | 
 | 718 |       Constant *Op1 = ValueList.getConstantFwdRef(Record[1], | 
 | 719 |                                                   OpTy->getElementType()); | 
 | 720 |       Constant *Op2 = ValueList.getConstantFwdRef(Record[2], Type::Int32Ty); | 
 | 721 |       V = ConstantExpr::getInsertElement(Op0, Op1, Op2); | 
 | 722 |       break; | 
 | 723 |     } | 
 | 724 |     case bitc::CST_CODE_CE_SHUFFLEVEC: { // CE_SHUFFLEVEC: [opval, opval, opval] | 
 | 725 |       const VectorType *OpTy = dyn_cast<VectorType>(CurTy); | 
 | 726 |       if (Record.size() < 3 || OpTy == 0) | 
 | 727 |         return Error("Invalid CE_INSERTELT record"); | 
 | 728 |       Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy); | 
 | 729 |       Constant *Op1 = ValueList.getConstantFwdRef(Record[1], OpTy); | 
 | 730 |       const Type *ShufTy=VectorType::get(Type::Int32Ty, OpTy->getNumElements()); | 
 | 731 |       Constant *Op2 = ValueList.getConstantFwdRef(Record[2], ShufTy); | 
 | 732 |       V = ConstantExpr::getShuffleVector(Op0, Op1, Op2); | 
 | 733 |       break; | 
 | 734 |     } | 
 | 735 |     case bitc::CST_CODE_CE_CMP: {     // CE_CMP: [opty, opval, opval, pred] | 
 | 736 |       if (Record.size() < 4) return Error("Invalid CE_CMP record"); | 
 | 737 |       const Type *OpTy = getTypeByID(Record[0]); | 
 | 738 |       if (OpTy == 0) return Error("Invalid CE_CMP record"); | 
 | 739 |       Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy); | 
 | 740 |       Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy); | 
 | 741 |  | 
 | 742 |       if (OpTy->isFloatingPoint()) | 
 | 743 |         V = ConstantExpr::getFCmp(Record[3], Op0, Op1); | 
 | 744 |       else | 
 | 745 |         V = ConstantExpr::getICmp(Record[3], Op0, Op1); | 
 | 746 |       break; | 
| Chris Lattner | 522b7b1 | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 747 |     } | 
| Chris Lattner | e16504e | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 748 |     } | 
 | 749 |      | 
| Chris Lattner | a7c49aa | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 750 |     ValueList.AssignValue(V, NextCstNo); | 
| Chris Lattner | 522b7b1 | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 751 |     ++NextCstNo; | 
| Chris Lattner | e16504e | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 752 |   } | 
 | 753 | } | 
| Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 754 |  | 
| Chris Lattner | 980e5aa | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 755 | /// RememberAndSkipFunctionBody - When we see the block for a function body, | 
 | 756 | /// remember where it is and then skip it.  This lets us lazily deserialize the | 
 | 757 | /// functions. | 
 | 758 | bool BitcodeReader::RememberAndSkipFunctionBody() { | 
| Chris Lattner | 48f8487 | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 759 |   // Get the function we are talking about. | 
 | 760 |   if (FunctionsWithBodies.empty()) | 
 | 761 |     return Error("Insufficient function protos"); | 
 | 762 |    | 
 | 763 |   Function *Fn = FunctionsWithBodies.back(); | 
 | 764 |   FunctionsWithBodies.pop_back(); | 
 | 765 |    | 
 | 766 |   // Save the current stream state. | 
 | 767 |   uint64_t CurBit = Stream.GetCurrentBitNo(); | 
 | 768 |   DeferredFunctionInfo[Fn] = std::make_pair(CurBit, Fn->getLinkage()); | 
 | 769 |    | 
 | 770 |   // Set the functions linkage to GhostLinkage so we know it is lazily | 
 | 771 |   // deserialized. | 
 | 772 |   Fn->setLinkage(GlobalValue::GhostLinkage); | 
 | 773 |    | 
 | 774 |   // Skip over the function block for now. | 
 | 775 |   if (Stream.SkipBlock()) | 
 | 776 |     return Error("Malformed block record"); | 
 | 777 |   return false; | 
 | 778 | } | 
 | 779 |  | 
| Chris Lattner | 8669714 | 2007-05-01 05:01:34 +0000 | [diff] [blame] | 780 | bool BitcodeReader::ParseModule(const std::string &ModuleID) { | 
| Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 781 |   // Reject multiple MODULE_BLOCK's in a single bitstream. | 
 | 782 |   if (TheModule) | 
 | 783 |     return Error("Multiple MODULE_BLOCKs in same stream"); | 
 | 784 |    | 
| Chris Lattner | e17b658 | 2007-05-05 00:17:00 +0000 | [diff] [blame] | 785 |   if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) | 
| Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 786 |     return Error("Malformed block record"); | 
 | 787 |  | 
 | 788 |   // Otherwise, create the module. | 
 | 789 |   TheModule = new Module(ModuleID); | 
 | 790 |    | 
 | 791 |   SmallVector<uint64_t, 64> Record; | 
 | 792 |   std::vector<std::string> SectionTable; | 
 | 793 |  | 
 | 794 |   // Read all the records for this module. | 
 | 795 |   while (!Stream.AtEndOfStream()) { | 
 | 796 |     unsigned Code = Stream.ReadCode(); | 
| Chris Lattner | e84bcb9 | 2007-04-24 00:21:45 +0000 | [diff] [blame] | 797 |     if (Code == bitc::END_BLOCK) { | 
| Chris Lattner | 980e5aa | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 798 |       if (Stream.ReadBlockEnd()) | 
 | 799 |         return Error("Error at end of module block"); | 
 | 800 |  | 
 | 801 |       // Patch the initializers for globals and aliases up. | 
| Chris Lattner | 07d98b4 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 802 |       ResolveGlobalAndAliasInits(); | 
 | 803 |       if (!GlobalInits.empty() || !AliasInits.empty()) | 
| Chris Lattner | e84bcb9 | 2007-04-24 00:21:45 +0000 | [diff] [blame] | 804 |         return Error("Malformed global initializer set"); | 
| Chris Lattner | 48f8487 | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 805 |       if (!FunctionsWithBodies.empty()) | 
 | 806 |         return Error("Too few function bodies found"); | 
| Chris Lattner | 980e5aa | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 807 |  | 
 | 808 |       // Force deallocation of memory for these vectors to favor the client that | 
 | 809 |       // want lazy deserialization. | 
 | 810 |       std::vector<std::pair<GlobalVariable*, unsigned> >().swap(GlobalInits); | 
 | 811 |       std::vector<std::pair<GlobalAlias*, unsigned> >().swap(AliasInits); | 
 | 812 |       std::vector<Function*>().swap(FunctionsWithBodies); | 
| Chris Lattner | f66d20d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 813 |       return false; | 
| Chris Lattner | e84bcb9 | 2007-04-24 00:21:45 +0000 | [diff] [blame] | 814 |     } | 
| Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 815 |      | 
 | 816 |     if (Code == bitc::ENTER_SUBBLOCK) { | 
 | 817 |       switch (Stream.ReadSubBlockID()) { | 
 | 818 |       default:  // Skip unknown content. | 
 | 819 |         if (Stream.SkipBlock()) | 
 | 820 |           return Error("Malformed block record"); | 
 | 821 |         break; | 
| Chris Lattner | 3f79980 | 2007-05-05 18:57:30 +0000 | [diff] [blame] | 822 |       case bitc::BLOCKINFO_BLOCK_ID: | 
 | 823 |         if (Stream.ReadBlockInfoBlock()) | 
 | 824 |           return Error("Malformed BlockInfoBlock"); | 
 | 825 |         break; | 
| Chris Lattner | 48c85b8 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 826 |       case bitc::PARAMATTR_BLOCK_ID: | 
 | 827 |         if (ParseParamAttrBlock()) | 
 | 828 |           return true; | 
 | 829 |         break; | 
| Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 830 |       case bitc::TYPE_BLOCK_ID: | 
| Chris Lattner | 8669714 | 2007-05-01 05:01:34 +0000 | [diff] [blame] | 831 |         if (ParseTypeTable()) | 
| Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 832 |           return true; | 
 | 833 |         break; | 
 | 834 |       case bitc::TYPE_SYMTAB_BLOCK_ID: | 
| Chris Lattner | 8669714 | 2007-05-01 05:01:34 +0000 | [diff] [blame] | 835 |         if (ParseTypeSymbolTable()) | 
| Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 836 |           return true; | 
 | 837 |         break; | 
| Chris Lattner | 0b2482a | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 838 |       case bitc::VALUE_SYMTAB_BLOCK_ID: | 
| Chris Lattner | 8669714 | 2007-05-01 05:01:34 +0000 | [diff] [blame] | 839 |         if (ParseValueSymbolTable()) | 
| Chris Lattner | 0b2482a | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 840 |           return true; | 
 | 841 |         break; | 
| Chris Lattner | e16504e | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 842 |       case bitc::CONSTANTS_BLOCK_ID: | 
| Chris Lattner | 8669714 | 2007-05-01 05:01:34 +0000 | [diff] [blame] | 843 |         if (ParseConstants() || ResolveGlobalAndAliasInits()) | 
| Chris Lattner | e16504e | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 844 |           return true; | 
 | 845 |         break; | 
| Chris Lattner | 48f8487 | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 846 |       case bitc::FUNCTION_BLOCK_ID: | 
 | 847 |         // If this is the first function body we've seen, reverse the | 
 | 848 |         // FunctionsWithBodies list. | 
 | 849 |         if (!HasReversedFunctionsWithBodies) { | 
 | 850 |           std::reverse(FunctionsWithBodies.begin(), FunctionsWithBodies.end()); | 
 | 851 |           HasReversedFunctionsWithBodies = true; | 
 | 852 |         } | 
 | 853 |          | 
| Chris Lattner | 980e5aa | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 854 |         if (RememberAndSkipFunctionBody()) | 
| Chris Lattner | 48f8487 | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 855 |           return true; | 
 | 856 |         break; | 
| Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 857 |       } | 
 | 858 |       continue; | 
 | 859 |     } | 
 | 860 |      | 
| Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 861 |     if (Code == bitc::DEFINE_ABBREV) { | 
| Chris Lattner | d127c1b | 2007-04-23 18:58:34 +0000 | [diff] [blame] | 862 |       Stream.ReadAbbrevRecord(); | 
 | 863 |       continue; | 
| Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 864 |     } | 
 | 865 |      | 
 | 866 |     // Read a record. | 
 | 867 |     switch (Stream.ReadRecord(Code, Record)) { | 
 | 868 |     default: break;  // Default behavior, ignore unknown content. | 
 | 869 |     case bitc::MODULE_CODE_VERSION:  // VERSION: [version#] | 
 | 870 |       if (Record.size() < 1) | 
 | 871 |         return Error("Malformed MODULE_CODE_VERSION"); | 
 | 872 |       // Only version #0 is supported so far. | 
 | 873 |       if (Record[0] != 0) | 
 | 874 |         return Error("Unknown bitstream version!"); | 
 | 875 |       break; | 
| Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 876 |     case bitc::MODULE_CODE_TRIPLE: {  // TRIPLE: [strchr x N] | 
| Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 877 |       std::string S; | 
 | 878 |       if (ConvertToString(Record, 0, S)) | 
 | 879 |         return Error("Invalid MODULE_CODE_TRIPLE record"); | 
 | 880 |       TheModule->setTargetTriple(S); | 
 | 881 |       break; | 
 | 882 |     } | 
| Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 883 |     case bitc::MODULE_CODE_DATALAYOUT: {  // DATALAYOUT: [strchr x N] | 
| Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 884 |       std::string S; | 
 | 885 |       if (ConvertToString(Record, 0, S)) | 
 | 886 |         return Error("Invalid MODULE_CODE_DATALAYOUT record"); | 
 | 887 |       TheModule->setDataLayout(S); | 
 | 888 |       break; | 
 | 889 |     } | 
| Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 890 |     case bitc::MODULE_CODE_ASM: {  // ASM: [strchr x N] | 
| Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 891 |       std::string S; | 
 | 892 |       if (ConvertToString(Record, 0, S)) | 
 | 893 |         return Error("Invalid MODULE_CODE_ASM record"); | 
 | 894 |       TheModule->setModuleInlineAsm(S); | 
 | 895 |       break; | 
 | 896 |     } | 
| Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 897 |     case bitc::MODULE_CODE_DEPLIB: {  // DEPLIB: [strchr x N] | 
| Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 898 |       std::string S; | 
 | 899 |       if (ConvertToString(Record, 0, S)) | 
 | 900 |         return Error("Invalid MODULE_CODE_DEPLIB record"); | 
 | 901 |       TheModule->addLibrary(S); | 
 | 902 |       break; | 
 | 903 |     } | 
| Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 904 |     case bitc::MODULE_CODE_SECTIONNAME: {  // SECTIONNAME: [strchr x N] | 
| Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 905 |       std::string S; | 
 | 906 |       if (ConvertToString(Record, 0, S)) | 
 | 907 |         return Error("Invalid MODULE_CODE_SECTIONNAME record"); | 
 | 908 |       SectionTable.push_back(S); | 
 | 909 |       break; | 
 | 910 |     } | 
 | 911 |     // GLOBALVAR: [type, isconst, initid,  | 
 | 912 |     //             linkage, alignment, section, visibility, threadlocal] | 
 | 913 |     case bitc::MODULE_CODE_GLOBALVAR: { | 
| Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 914 |       if (Record.size() < 6) | 
| Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 915 |         return Error("Invalid MODULE_CODE_GLOBALVAR record"); | 
 | 916 |       const Type *Ty = getTypeByID(Record[0]); | 
 | 917 |       if (!isa<PointerType>(Ty)) | 
 | 918 |         return Error("Global not a pointer type!"); | 
 | 919 |       Ty = cast<PointerType>(Ty)->getElementType(); | 
 | 920 |        | 
 | 921 |       bool isConstant = Record[1]; | 
 | 922 |       GlobalValue::LinkageTypes Linkage = GetDecodedLinkage(Record[3]); | 
 | 923 |       unsigned Alignment = (1 << Record[4]) >> 1; | 
 | 924 |       std::string Section; | 
 | 925 |       if (Record[5]) { | 
 | 926 |         if (Record[5]-1 >= SectionTable.size()) | 
 | 927 |           return Error("Invalid section ID"); | 
 | 928 |         Section = SectionTable[Record[5]-1]; | 
 | 929 |       } | 
| Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 930 |       GlobalValue::VisibilityTypes Visibility = GlobalValue::DefaultVisibility; | 
 | 931 |       if (Record.size() >= 6) Visibility = GetDecodedVisibility(Record[6]); | 
 | 932 |       bool isThreadLocal = false; | 
 | 933 |       if (Record.size() >= 7) isThreadLocal = Record[7]; | 
| Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 934 |  | 
 | 935 |       GlobalVariable *NewGV = | 
 | 936 |         new GlobalVariable(Ty, isConstant, Linkage, 0, "", TheModule); | 
 | 937 |       NewGV->setAlignment(Alignment); | 
 | 938 |       if (!Section.empty()) | 
 | 939 |         NewGV->setSection(Section); | 
 | 940 |       NewGV->setVisibility(Visibility); | 
 | 941 |       NewGV->setThreadLocal(isThreadLocal); | 
 | 942 |        | 
| Chris Lattner | 0b2482a | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 943 |       ValueList.push_back(NewGV); | 
 | 944 |        | 
| Chris Lattner | 6dbfd7b | 2007-04-24 00:18:21 +0000 | [diff] [blame] | 945 |       // Remember which value to use for the global initializer. | 
 | 946 |       if (unsigned InitID = Record[2]) | 
 | 947 |         GlobalInits.push_back(std::make_pair(NewGV, InitID-1)); | 
| Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 948 |       break; | 
 | 949 |     } | 
 | 950 |     // FUNCTION:  [type, callingconv, isproto, linkage, alignment, section, | 
 | 951 |     //             visibility] | 
 | 952 |     case bitc::MODULE_CODE_FUNCTION: { | 
 | 953 |       if (Record.size() < 7) | 
 | 954 |         return Error("Invalid MODULE_CODE_FUNCTION record"); | 
 | 955 |       const Type *Ty = getTypeByID(Record[0]); | 
 | 956 |       if (!isa<PointerType>(Ty)) | 
 | 957 |         return Error("Function not a pointer type!"); | 
 | 958 |       const FunctionType *FTy = | 
 | 959 |         dyn_cast<FunctionType>(cast<PointerType>(Ty)->getElementType()); | 
 | 960 |       if (!FTy) | 
 | 961 |         return Error("Function not a pointer to function type!"); | 
 | 962 |  | 
 | 963 |       Function *Func = new Function(FTy, GlobalValue::ExternalLinkage, | 
 | 964 |                                     "", TheModule); | 
 | 965 |  | 
 | 966 |       Func->setCallingConv(Record[1]); | 
| Chris Lattner | 48f8487 | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 967 |       bool isProto = Record[2]; | 
| Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 968 |       Func->setLinkage(GetDecodedLinkage(Record[3])); | 
 | 969 |       Func->setAlignment((1 << Record[4]) >> 1); | 
 | 970 |       if (Record[5]) { | 
 | 971 |         if (Record[5]-1 >= SectionTable.size()) | 
 | 972 |           return Error("Invalid section ID"); | 
 | 973 |         Func->setSection(SectionTable[Record[5]-1]); | 
 | 974 |       } | 
 | 975 |       Func->setVisibility(GetDecodedVisibility(Record[6])); | 
 | 976 |        | 
| Chris Lattner | 0b2482a | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 977 |       ValueList.push_back(Func); | 
| Chris Lattner | 48f8487 | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 978 |        | 
 | 979 |       // If this is a function with a body, remember the prototype we are | 
 | 980 |       // creating now, so that we can match up the body with them later. | 
 | 981 |       if (!isProto) | 
 | 982 |         FunctionsWithBodies.push_back(Func); | 
| Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 983 |       break; | 
 | 984 |     } | 
| Chris Lattner | 07d98b4 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 985 |     // ALIAS: [alias type, aliasee val#, linkage] | 
| Chris Lattner | 198f34a | 2007-04-26 03:27:58 +0000 | [diff] [blame] | 986 |     case bitc::MODULE_CODE_ALIAS: { | 
| Chris Lattner | 07d98b4 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 987 |       if (Record.size() < 3) | 
 | 988 |         return Error("Invalid MODULE_ALIAS record"); | 
 | 989 |       const Type *Ty = getTypeByID(Record[0]); | 
 | 990 |       if (!isa<PointerType>(Ty)) | 
 | 991 |         return Error("Function not a pointer type!"); | 
 | 992 |        | 
 | 993 |       GlobalAlias *NewGA = new GlobalAlias(Ty, GetDecodedLinkage(Record[2]), | 
 | 994 |                                            "", 0, TheModule); | 
 | 995 |       ValueList.push_back(NewGA); | 
 | 996 |       AliasInits.push_back(std::make_pair(NewGA, Record[1])); | 
 | 997 |       break; | 
| Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 998 |     } | 
| Chris Lattner | 198f34a | 2007-04-26 03:27:58 +0000 | [diff] [blame] | 999 |     /// MODULE_CODE_PURGEVALS: [numvals] | 
 | 1000 |     case bitc::MODULE_CODE_PURGEVALS: | 
 | 1001 |       // Trim down the value list to the specified size. | 
 | 1002 |       if (Record.size() < 1 || Record[0] > ValueList.size()) | 
 | 1003 |         return Error("Invalid MODULE_PURGEVALS record"); | 
 | 1004 |       ValueList.shrinkTo(Record[0]); | 
 | 1005 |       break; | 
 | 1006 |     } | 
| Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1007 |     Record.clear(); | 
 | 1008 |   } | 
 | 1009 |    | 
 | 1010 |   return Error("Premature end of bitstream"); | 
 | 1011 | } | 
 | 1012 |  | 
 | 1013 |  | 
| Chris Lattner | c453f76 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 1014 | bool BitcodeReader::ParseBitcode() { | 
| Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1015 |   TheModule = 0; | 
 | 1016 |    | 
| Chris Lattner | c453f76 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 1017 |   if (Buffer->getBufferSize() & 3) | 
| Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1018 |     return Error("Bitcode stream should be a multiple of 4 bytes in length"); | 
 | 1019 |    | 
| Chris Lattner | c453f76 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 1020 |   unsigned char *BufPtr = (unsigned char *)Buffer->getBufferStart(); | 
| Chris Lattner | 48f8487 | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 1021 |   Stream.init(BufPtr, BufPtr+Buffer->getBufferSize()); | 
| Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1022 |    | 
 | 1023 |   // Sniff for the signature. | 
 | 1024 |   if (Stream.Read(8) != 'B' || | 
 | 1025 |       Stream.Read(8) != 'C' || | 
 | 1026 |       Stream.Read(4) != 0x0 || | 
 | 1027 |       Stream.Read(4) != 0xC || | 
 | 1028 |       Stream.Read(4) != 0xE || | 
 | 1029 |       Stream.Read(4) != 0xD) | 
 | 1030 |     return Error("Invalid bitcode signature"); | 
 | 1031 |    | 
 | 1032 |   // We expect a number of well-defined blocks, though we don't necessarily | 
 | 1033 |   // need to understand them all. | 
 | 1034 |   while (!Stream.AtEndOfStream()) { | 
 | 1035 |     unsigned Code = Stream.ReadCode(); | 
 | 1036 |      | 
 | 1037 |     if (Code != bitc::ENTER_SUBBLOCK) | 
 | 1038 |       return Error("Invalid record at top-level"); | 
 | 1039 |      | 
 | 1040 |     unsigned BlockID = Stream.ReadSubBlockID(); | 
 | 1041 |      | 
 | 1042 |     // We only know the MODULE subblock ID. | 
| Chris Lattner | e17b658 | 2007-05-05 00:17:00 +0000 | [diff] [blame] | 1043 |     switch (BlockID) { | 
 | 1044 |     case bitc::BLOCKINFO_BLOCK_ID: | 
 | 1045 |       if (Stream.ReadBlockInfoBlock()) | 
 | 1046 |         return Error("Malformed BlockInfoBlock"); | 
 | 1047 |       break; | 
 | 1048 |     case bitc::MODULE_BLOCK_ID: | 
| Chris Lattner | 8669714 | 2007-05-01 05:01:34 +0000 | [diff] [blame] | 1049 |       if (ParseModule(Buffer->getBufferIdentifier())) | 
| Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1050 |         return true; | 
| Chris Lattner | e17b658 | 2007-05-05 00:17:00 +0000 | [diff] [blame] | 1051 |       break; | 
 | 1052 |     default: | 
 | 1053 |       if (Stream.SkipBlock()) | 
 | 1054 |         return Error("Malformed block record"); | 
 | 1055 |       break; | 
| Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1056 |     } | 
 | 1057 |   } | 
 | 1058 |    | 
 | 1059 |   return false; | 
 | 1060 | } | 
| Chris Lattner | c453f76 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 1061 |  | 
| Chris Lattner | 48f8487 | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 1062 |  | 
 | 1063 | bool BitcodeReader::materializeFunction(Function *F, std::string *ErrInfo) { | 
 | 1064 |   // If it already is material, ignore the request. | 
 | 1065 |   if (!F->hasNotBeenReadFromBytecode()) return false; | 
 | 1066 |  | 
 | 1067 |   DenseMap<Function*, std::pair<uint64_t, unsigned> >::iterator DFII =  | 
 | 1068 |     DeferredFunctionInfo.find(F); | 
 | 1069 |   assert(DFII != DeferredFunctionInfo.end() && "Deferred function not found!"); | 
 | 1070 |    | 
 | 1071 |   // Move the bit stream to the saved position of the deferred function body and | 
 | 1072 |   // restore the real linkage type for the function. | 
 | 1073 |   Stream.JumpToBit(DFII->second.first); | 
 | 1074 |   F->setLinkage((GlobalValue::LinkageTypes)DFII->second.second); | 
 | 1075 |   DeferredFunctionInfo.erase(DFII); | 
 | 1076 |    | 
| Chris Lattner | 980e5aa | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 1077 |   if (ParseFunctionBody(F)) { | 
 | 1078 |     if (ErrInfo) *ErrInfo = ErrorString; | 
 | 1079 |     return true; | 
 | 1080 |   } | 
 | 1081 |    | 
 | 1082 |   return false; | 
 | 1083 | } | 
 | 1084 |  | 
 | 1085 | Module *BitcodeReader::materializeModule(std::string *ErrInfo) { | 
 | 1086 |   DenseMap<Function*, std::pair<uint64_t, unsigned> >::iterator I =  | 
 | 1087 |     DeferredFunctionInfo.begin(); | 
 | 1088 |   while (!DeferredFunctionInfo.empty()) { | 
 | 1089 |     Function *F = (*I++).first; | 
 | 1090 |     assert(F->hasNotBeenReadFromBytecode() && | 
 | 1091 |            "Deserialized function found in map!"); | 
 | 1092 |     if (materializeFunction(F, ErrInfo)) | 
 | 1093 |       return 0; | 
 | 1094 |   } | 
 | 1095 |   return TheModule; | 
 | 1096 | } | 
 | 1097 |  | 
 | 1098 |  | 
 | 1099 | /// ParseFunctionBody - Lazily parse the specified function body block. | 
 | 1100 | bool BitcodeReader::ParseFunctionBody(Function *F) { | 
| Chris Lattner | e17b658 | 2007-05-05 00:17:00 +0000 | [diff] [blame] | 1101 |   if (Stream.EnterSubBlock(bitc::FUNCTION_BLOCK_ID)) | 
| Chris Lattner | 980e5aa | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 1102 |     return Error("Malformed block record"); | 
 | 1103 |    | 
 | 1104 |   unsigned ModuleValueListSize = ValueList.size(); | 
 | 1105 |    | 
 | 1106 |   // Add all the function arguments to the value table. | 
 | 1107 |   for(Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I) | 
 | 1108 |     ValueList.push_back(I); | 
 | 1109 |    | 
| Chris Lattner | a7c49aa | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 1110 |   unsigned NextValueNo = ValueList.size(); | 
| Chris Lattner | 231cbcb | 2007-05-02 04:27:25 +0000 | [diff] [blame] | 1111 |   BasicBlock *CurBB = 0; | 
 | 1112 |   unsigned CurBBNo = 0; | 
 | 1113 |  | 
| Chris Lattner | 980e5aa | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 1114 |   // Read all the records. | 
 | 1115 |   SmallVector<uint64_t, 64> Record; | 
 | 1116 |   while (1) { | 
 | 1117 |     unsigned Code = Stream.ReadCode(); | 
 | 1118 |     if (Code == bitc::END_BLOCK) { | 
 | 1119 |       if (Stream.ReadBlockEnd()) | 
 | 1120 |         return Error("Error at end of function block"); | 
 | 1121 |       break; | 
 | 1122 |     } | 
 | 1123 |      | 
 | 1124 |     if (Code == bitc::ENTER_SUBBLOCK) { | 
 | 1125 |       switch (Stream.ReadSubBlockID()) { | 
 | 1126 |       default:  // Skip unknown content. | 
 | 1127 |         if (Stream.SkipBlock()) | 
 | 1128 |           return Error("Malformed block record"); | 
 | 1129 |         break; | 
 | 1130 |       case bitc::CONSTANTS_BLOCK_ID: | 
 | 1131 |         if (ParseConstants()) return true; | 
| Chris Lattner | a7c49aa | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 1132 |         NextValueNo = ValueList.size(); | 
| Chris Lattner | 980e5aa | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 1133 |         break; | 
 | 1134 |       case bitc::VALUE_SYMTAB_BLOCK_ID: | 
 | 1135 |         if (ParseValueSymbolTable()) return true; | 
 | 1136 |         break; | 
 | 1137 |       } | 
 | 1138 |       continue; | 
 | 1139 |     } | 
 | 1140 |      | 
 | 1141 |     if (Code == bitc::DEFINE_ABBREV) { | 
 | 1142 |       Stream.ReadAbbrevRecord(); | 
 | 1143 |       continue; | 
 | 1144 |     } | 
 | 1145 |      | 
 | 1146 |     // Read a record. | 
 | 1147 |     Record.clear(); | 
| Chris Lattner | a7c49aa | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 1148 |     Instruction *I = 0; | 
| Chris Lattner | 980e5aa | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 1149 |     switch (Stream.ReadRecord(Code, Record)) { | 
| Chris Lattner | a7c49aa | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 1150 |     default: // Default behavior: reject | 
 | 1151 |       return Error("Unknown instruction"); | 
| Chris Lattner | 980e5aa | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 1152 |     case bitc::FUNC_CODE_DECLAREBLOCKS:     // DECLAREBLOCKS: [nblocks] | 
| Chris Lattner | a7c49aa | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 1153 |       if (Record.size() < 1 || Record[0] == 0) | 
 | 1154 |         return Error("Invalid DECLAREBLOCKS record"); | 
| Chris Lattner | 980e5aa | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 1155 |       // Create all the basic blocks for the function. | 
| Chris Lattner | f61e645 | 2007-05-03 22:09:51 +0000 | [diff] [blame] | 1156 |       FunctionBBs.resize(Record[0]); | 
| Chris Lattner | 980e5aa | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 1157 |       for (unsigned i = 0, e = FunctionBBs.size(); i != e; ++i) | 
 | 1158 |         FunctionBBs[i] = new BasicBlock("", F); | 
| Chris Lattner | a7c49aa | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 1159 |       CurBB = FunctionBBs[0]; | 
 | 1160 |       continue; | 
 | 1161 |        | 
| Chris Lattner | abfbf85 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 1162 |     case bitc::FUNC_CODE_INST_BINOP: {    // BINOP: [opval, ty, opval, opcode] | 
 | 1163 |       unsigned OpNum = 0; | 
 | 1164 |       Value *LHS, *RHS; | 
 | 1165 |       if (getValueTypePair(Record, OpNum, NextValueNo, LHS) || | 
 | 1166 |           getValue(Record, OpNum, LHS->getType(), RHS) || | 
 | 1167 |           OpNum+1 != Record.size()) | 
 | 1168 |         return Error("Invalid BINOP record"); | 
 | 1169 |        | 
 | 1170 |       int Opc = GetDecodedBinaryOpcode(Record[OpNum], LHS->getType()); | 
 | 1171 |       if (Opc == -1) return Error("Invalid BINOP record"); | 
| Chris Lattner | a7c49aa | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 1172 |       I = BinaryOperator::create((Instruction::BinaryOps)Opc, LHS, RHS); | 
| Chris Lattner | 980e5aa | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 1173 |       break; | 
 | 1174 |     } | 
| Chris Lattner | abfbf85 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 1175 |     case bitc::FUNC_CODE_INST_CAST: {    // CAST: [opval, opty, destty, castopc] | 
 | 1176 |       unsigned OpNum = 0; | 
 | 1177 |       Value *Op; | 
 | 1178 |       if (getValueTypePair(Record, OpNum, NextValueNo, Op) || | 
 | 1179 |           OpNum+2 != Record.size()) | 
 | 1180 |         return Error("Invalid CAST record"); | 
 | 1181 |        | 
 | 1182 |       const Type *ResTy = getTypeByID(Record[OpNum]); | 
 | 1183 |       int Opc = GetDecodedCastOpcode(Record[OpNum+1]); | 
 | 1184 |       if (Opc == -1 || ResTy == 0) | 
| Chris Lattner | 231cbcb | 2007-05-02 04:27:25 +0000 | [diff] [blame] | 1185 |         return Error("Invalid CAST record"); | 
 | 1186 |       I = CastInst::create((Instruction::CastOps)Opc, Op, ResTy); | 
 | 1187 |       break; | 
 | 1188 |     } | 
| Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 1189 |     case bitc::FUNC_CODE_INST_GEP: { // GEP: [n x operands] | 
| Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1190 |       unsigned OpNum = 0; | 
 | 1191 |       Value *BasePtr; | 
 | 1192 |       if (getValueTypePair(Record, OpNum, NextValueNo, BasePtr)) | 
| Chris Lattner | 01ff65f | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 1193 |         return Error("Invalid GEP record"); | 
 | 1194 |  | 
| Chris Lattner | f4c8e52 | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 1195 |       SmallVector<Value*, 16> GEPIdx; | 
| Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1196 |       while (OpNum != Record.size()) { | 
 | 1197 |         Value *Op; | 
 | 1198 |         if (getValueTypePair(Record, OpNum, NextValueNo, Op)) | 
| Chris Lattner | 01ff65f | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 1199 |           return Error("Invalid GEP record"); | 
| Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1200 |         GEPIdx.push_back(Op); | 
| Chris Lattner | 01ff65f | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 1201 |       } | 
 | 1202 |  | 
| Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1203 |       I = new GetElementPtrInst(BasePtr, &GEPIdx[0], GEPIdx.size()); | 
| Chris Lattner | 01ff65f | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 1204 |       break; | 
 | 1205 |     } | 
| Chris Lattner | 231cbcb | 2007-05-02 04:27:25 +0000 | [diff] [blame] | 1206 |        | 
| Chris Lattner | abfbf85 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 1207 |     case bitc::FUNC_CODE_INST_SELECT: { // SELECT: [opval, ty, opval, opval] | 
 | 1208 |       unsigned OpNum = 0; | 
 | 1209 |       Value *TrueVal, *FalseVal, *Cond; | 
 | 1210 |       if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal) || | 
 | 1211 |           getValue(Record, OpNum, TrueVal->getType(), FalseVal) || | 
 | 1212 |           getValue(Record, OpNum, Type::Int1Ty, Cond)) | 
| Chris Lattner | 01ff65f | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 1213 |         return Error("Invalid SELECT record"); | 
| Chris Lattner | abfbf85 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 1214 |        | 
 | 1215 |       I = new SelectInst(Cond, TrueVal, FalseVal); | 
| Chris Lattner | 01ff65f | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 1216 |       break; | 
 | 1217 |     } | 
 | 1218 |        | 
 | 1219 |     case bitc::FUNC_CODE_INST_EXTRACTELT: { // EXTRACTELT: [opty, opval, opval] | 
| Chris Lattner | abfbf85 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 1220 |       unsigned OpNum = 0; | 
 | 1221 |       Value *Vec, *Idx; | 
 | 1222 |       if (getValueTypePair(Record, OpNum, NextValueNo, Vec) || | 
 | 1223 |           getValue(Record, OpNum, Type::Int32Ty, Idx)) | 
| Chris Lattner | 01ff65f | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 1224 |         return Error("Invalid EXTRACTELT record"); | 
 | 1225 |       I = new ExtractElementInst(Vec, Idx); | 
 | 1226 |       break; | 
 | 1227 |     } | 
 | 1228 |        | 
 | 1229 |     case bitc::FUNC_CODE_INST_INSERTELT: { // INSERTELT: [ty, opval,opval,opval] | 
| Chris Lattner | abfbf85 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 1230 |       unsigned OpNum = 0; | 
 | 1231 |       Value *Vec, *Elt, *Idx; | 
 | 1232 |       if (getValueTypePair(Record, OpNum, NextValueNo, Vec) || | 
 | 1233 |           getValue(Record, OpNum,  | 
 | 1234 |                    cast<VectorType>(Vec->getType())->getElementType(), Elt) || | 
 | 1235 |           getValue(Record, OpNum, Type::Int32Ty, Idx)) | 
| Chris Lattner | 01ff65f | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 1236 |         return Error("Invalid INSERTELT record"); | 
 | 1237 |       I = new InsertElementInst(Vec, Elt, Idx); | 
 | 1238 |       break; | 
 | 1239 |     } | 
 | 1240 |        | 
| Chris Lattner | abfbf85 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 1241 |     case bitc::FUNC_CODE_INST_SHUFFLEVEC: {// SHUFFLEVEC: [opval,ty,opval,opval] | 
 | 1242 |       unsigned OpNum = 0; | 
 | 1243 |       Value *Vec1, *Vec2, *Mask; | 
 | 1244 |       if (getValueTypePair(Record, OpNum, NextValueNo, Vec1) || | 
 | 1245 |           getValue(Record, OpNum, Vec1->getType(), Vec2)) | 
 | 1246 |         return Error("Invalid SHUFFLEVEC record"); | 
 | 1247 |  | 
 | 1248 |       const Type *MaskTy = | 
 | 1249 |         VectorType::get(Type::Int32Ty,  | 
 | 1250 |                         cast<VectorType>(Vec1->getType())->getNumElements()); | 
 | 1251 |  | 
 | 1252 |       if (getValue(Record, OpNum, MaskTy, Mask)) | 
| Chris Lattner | 01ff65f | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 1253 |         return Error("Invalid SHUFFLEVEC record"); | 
 | 1254 |       I = new ShuffleVectorInst(Vec1, Vec2, Mask); | 
 | 1255 |       break; | 
 | 1256 |     } | 
 | 1257 |        | 
 | 1258 |     case bitc::FUNC_CODE_INST_CMP: { // CMP: [opty, opval, opval, pred] | 
| Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1259 |       unsigned OpNum = 0; | 
 | 1260 |       Value *LHS, *RHS; | 
 | 1261 |       if (getValueTypePair(Record, OpNum, NextValueNo, LHS) || | 
 | 1262 |           getValue(Record, OpNum, LHS->getType(), RHS) || | 
 | 1263 |           OpNum+1 != Record.size()) | 
| Chris Lattner | 01ff65f | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 1264 |         return Error("Invalid CMP record"); | 
| Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1265 |        | 
 | 1266 |       if (LHS->getType()->isFPOrFPVector()) | 
 | 1267 |         I = new FCmpInst((FCmpInst::Predicate)Record[OpNum], LHS, RHS); | 
| Chris Lattner | 01ff65f | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 1268 |       else | 
| Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1269 |         I = new ICmpInst((ICmpInst::Predicate)Record[OpNum], LHS, RHS); | 
| Chris Lattner | 01ff65f | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 1270 |       break; | 
 | 1271 |     } | 
 | 1272 |      | 
| Chris Lattner | 231cbcb | 2007-05-02 04:27:25 +0000 | [diff] [blame] | 1273 |     case bitc::FUNC_CODE_INST_RET: // RET: [opty,opval<optional>] | 
 | 1274 |       if (Record.size() == 0) { | 
 | 1275 |         I = new ReturnInst(); | 
 | 1276 |         break; | 
| Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1277 |       } else { | 
 | 1278 |         unsigned OpNum = 0; | 
 | 1279 |         Value *Op; | 
 | 1280 |         if (getValueTypePair(Record, OpNum, NextValueNo, Op) || | 
 | 1281 |             OpNum != Record.size()) | 
| Chris Lattner | 2a98cca | 2007-05-03 18:58:09 +0000 | [diff] [blame] | 1282 |           return Error("Invalid RET record"); | 
| Chris Lattner | 231cbcb | 2007-05-02 04:27:25 +0000 | [diff] [blame] | 1283 |         I = new ReturnInst(Op); | 
 | 1284 |         break; | 
 | 1285 |       } | 
| Chris Lattner | f4c8e52 | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 1286 |     case bitc::FUNC_CODE_INST_BR: { // BR: [bb#, bb#, opval] or [bb#] | 
| Chris Lattner | f61e645 | 2007-05-03 22:09:51 +0000 | [diff] [blame] | 1287 |       if (Record.size() != 1 && Record.size() != 3) | 
| Chris Lattner | f4c8e52 | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 1288 |         return Error("Invalid BR record"); | 
 | 1289 |       BasicBlock *TrueDest = getBasicBlock(Record[0]); | 
 | 1290 |       if (TrueDest == 0) | 
 | 1291 |         return Error("Invalid BR record"); | 
 | 1292 |  | 
 | 1293 |       if (Record.size() == 1) | 
 | 1294 |         I = new BranchInst(TrueDest); | 
 | 1295 |       else { | 
 | 1296 |         BasicBlock *FalseDest = getBasicBlock(Record[1]); | 
 | 1297 |         Value *Cond = getFnValueByID(Record[2], Type::Int1Ty); | 
 | 1298 |         if (FalseDest == 0 || Cond == 0) | 
 | 1299 |           return Error("Invalid BR record"); | 
 | 1300 |         I = new BranchInst(TrueDest, FalseDest, Cond); | 
 | 1301 |       } | 
 | 1302 |       break; | 
 | 1303 |     } | 
 | 1304 |     case bitc::FUNC_CODE_INST_SWITCH: { // SWITCH: [opty, opval, n, n x ops] | 
 | 1305 |       if (Record.size() < 3 || (Record.size() & 1) == 0) | 
 | 1306 |         return Error("Invalid SWITCH record"); | 
 | 1307 |       const Type *OpTy = getTypeByID(Record[0]); | 
 | 1308 |       Value *Cond = getFnValueByID(Record[1], OpTy); | 
 | 1309 |       BasicBlock *Default = getBasicBlock(Record[2]); | 
 | 1310 |       if (OpTy == 0 || Cond == 0 || Default == 0) | 
 | 1311 |         return Error("Invalid SWITCH record"); | 
 | 1312 |       unsigned NumCases = (Record.size()-3)/2; | 
 | 1313 |       SwitchInst *SI = new SwitchInst(Cond, Default, NumCases); | 
 | 1314 |       for (unsigned i = 0, e = NumCases; i != e; ++i) { | 
 | 1315 |         ConstantInt *CaseVal =  | 
 | 1316 |           dyn_cast_or_null<ConstantInt>(getFnValueByID(Record[3+i*2], OpTy)); | 
 | 1317 |         BasicBlock *DestBB = getBasicBlock(Record[1+3+i*2]); | 
 | 1318 |         if (CaseVal == 0 || DestBB == 0) { | 
 | 1319 |           delete SI; | 
 | 1320 |           return Error("Invalid SWITCH record!"); | 
 | 1321 |         } | 
 | 1322 |         SI->addCase(CaseVal, DestBB); | 
 | 1323 |       } | 
 | 1324 |       I = SI; | 
 | 1325 |       break; | 
 | 1326 |     } | 
 | 1327 |        | 
| Chris Lattner | 7652019 | 2007-05-03 22:34:03 +0000 | [diff] [blame] | 1328 |     case bitc::FUNC_CODE_INST_INVOKE: { // INVOKE: [cc,fnty, op0,op1,op2, ...] | 
| Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1329 |       if (Record.size() < 3) return Error("Invalid INVOKE record"); | 
| Chris Lattner | 7652019 | 2007-05-03 22:34:03 +0000 | [diff] [blame] | 1330 |       unsigned CCInfo = Record[0]; | 
| Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1331 |       BasicBlock *NormalBB = getBasicBlock(Record[1]); | 
 | 1332 |       BasicBlock *UnwindBB = getBasicBlock(Record[2]); | 
 | 1333 |        | 
 | 1334 |       unsigned OpNum = 3; | 
 | 1335 |       Value *Callee; | 
 | 1336 |       if (getValueTypePair(Record, OpNum, NextValueNo, Callee)) | 
| Chris Lattner | f4c8e52 | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 1337 |         return Error("Invalid INVOKE record"); | 
 | 1338 |        | 
| Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1339 |       const PointerType *CalleeTy = dyn_cast<PointerType>(Callee->getType()); | 
 | 1340 |       const FunctionType *FTy = !CalleeTy ? 0 : | 
| Chris Lattner | f4c8e52 | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 1341 |         dyn_cast<FunctionType>(CalleeTy->getElementType()); | 
 | 1342 |  | 
 | 1343 |       // Check that the right number of fixed parameters are here. | 
| Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1344 |       if (FTy == 0 || NormalBB == 0 || UnwindBB == 0 || | 
 | 1345 |           Record.size() < OpNum+FTy->getNumParams()) | 
| Chris Lattner | f4c8e52 | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 1346 |         return Error("Invalid INVOKE record"); | 
| Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1347 |        | 
| Chris Lattner | f4c8e52 | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 1348 |       SmallVector<Value*, 16> Ops; | 
| Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1349 |       for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) { | 
 | 1350 |         Ops.push_back(getFnValueByID(Record[OpNum], FTy->getParamType(i))); | 
 | 1351 |         if (Ops.back() == 0) return Error("Invalid INVOKE record"); | 
| Chris Lattner | f4c8e52 | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 1352 |       } | 
 | 1353 |        | 
| Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1354 |       if (!FTy->isVarArg()) { | 
 | 1355 |         if (Record.size() != OpNum) | 
| Chris Lattner | f4c8e52 | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 1356 |           return Error("Invalid INVOKE record"); | 
| Chris Lattner | f4c8e52 | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 1357 |       } else { | 
| Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1358 |         // Read type/value pairs for varargs params. | 
 | 1359 |         while (OpNum != Record.size()) { | 
 | 1360 |           Value *Op; | 
 | 1361 |           if (getValueTypePair(Record, OpNum, NextValueNo, Op)) | 
 | 1362 |             return Error("Invalid INVOKE record"); | 
 | 1363 |           Ops.push_back(Op); | 
 | 1364 |         } | 
| Chris Lattner | f4c8e52 | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 1365 |       } | 
 | 1366 |        | 
 | 1367 |       I = new InvokeInst(Callee, NormalBB, UnwindBB, &Ops[0], Ops.size()); | 
| Chris Lattner | 7652019 | 2007-05-03 22:34:03 +0000 | [diff] [blame] | 1368 |       cast<InvokeInst>(I)->setCallingConv(CCInfo); | 
| Chris Lattner | f4c8e52 | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 1369 |       break; | 
 | 1370 |     } | 
| Chris Lattner | 231cbcb | 2007-05-02 04:27:25 +0000 | [diff] [blame] | 1371 |     case bitc::FUNC_CODE_INST_UNWIND: // UNWIND | 
 | 1372 |       I = new UnwindInst(); | 
 | 1373 |       break; | 
 | 1374 |     case bitc::FUNC_CODE_INST_UNREACHABLE: // UNREACHABLE | 
 | 1375 |       I = new UnreachableInst(); | 
 | 1376 |       break; | 
| Chris Lattner | abfbf85 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 1377 |     case bitc::FUNC_CODE_INST_PHI: { // PHI: [ty, val0,bb0, ...] | 
| Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 1378 |       if (Record.size() < 1 || ((Record.size()-1)&1)) | 
| Chris Lattner | 2a98cca | 2007-05-03 18:58:09 +0000 | [diff] [blame] | 1379 |         return Error("Invalid PHI record"); | 
 | 1380 |       const Type *Ty = getTypeByID(Record[0]); | 
 | 1381 |       if (!Ty) return Error("Invalid PHI record"); | 
 | 1382 |        | 
 | 1383 |       PHINode *PN = new PHINode(Ty); | 
| Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 1384 |       PN->reserveOperandSpace(Record.size()-1); | 
| Chris Lattner | 2a98cca | 2007-05-03 18:58:09 +0000 | [diff] [blame] | 1385 |        | 
| Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 1386 |       for (unsigned i = 0, e = Record.size()-1; i != e; i += 2) { | 
 | 1387 |         Value *V = getFnValueByID(Record[1+i], Ty); | 
 | 1388 |         BasicBlock *BB = getBasicBlock(Record[2+i]); | 
| Chris Lattner | 2a98cca | 2007-05-03 18:58:09 +0000 | [diff] [blame] | 1389 |         if (!V || !BB) return Error("Invalid PHI record"); | 
 | 1390 |         PN->addIncoming(V, BB); | 
 | 1391 |       } | 
 | 1392 |       I = PN; | 
 | 1393 |       break; | 
 | 1394 |     } | 
 | 1395 |        | 
 | 1396 |     case bitc::FUNC_CODE_INST_MALLOC: { // MALLOC: [instty, op, align] | 
 | 1397 |       if (Record.size() < 3) | 
 | 1398 |         return Error("Invalid MALLOC record"); | 
 | 1399 |       const PointerType *Ty = | 
 | 1400 |         dyn_cast_or_null<PointerType>(getTypeByID(Record[0])); | 
 | 1401 |       Value *Size = getFnValueByID(Record[1], Type::Int32Ty); | 
 | 1402 |       unsigned Align = Record[2]; | 
 | 1403 |       if (!Ty || !Size) return Error("Invalid MALLOC record"); | 
 | 1404 |       I = new MallocInst(Ty->getElementType(), Size, (1 << Align) >> 1); | 
 | 1405 |       break; | 
 | 1406 |     } | 
| Chris Lattner | abfbf85 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 1407 |     case bitc::FUNC_CODE_INST_FREE: { // FREE: [op, opty] | 
 | 1408 |       unsigned OpNum = 0; | 
 | 1409 |       Value *Op; | 
 | 1410 |       if (getValueTypePair(Record, OpNum, NextValueNo, Op) || | 
 | 1411 |           OpNum != Record.size()) | 
| Chris Lattner | 2a98cca | 2007-05-03 18:58:09 +0000 | [diff] [blame] | 1412 |         return Error("Invalid FREE record"); | 
 | 1413 |       I = new FreeInst(Op); | 
 | 1414 |       break; | 
 | 1415 |     } | 
 | 1416 |     case bitc::FUNC_CODE_INST_ALLOCA: { // ALLOCA: [instty, op, align] | 
 | 1417 |       if (Record.size() < 3) | 
 | 1418 |         return Error("Invalid ALLOCA record"); | 
 | 1419 |       const PointerType *Ty = | 
 | 1420 |         dyn_cast_or_null<PointerType>(getTypeByID(Record[0])); | 
 | 1421 |       Value *Size = getFnValueByID(Record[1], Type::Int32Ty); | 
 | 1422 |       unsigned Align = Record[2]; | 
 | 1423 |       if (!Ty || !Size) return Error("Invalid ALLOCA record"); | 
 | 1424 |       I = new AllocaInst(Ty->getElementType(), Size, (1 << Align) >> 1); | 
 | 1425 |       break; | 
 | 1426 |     } | 
| Chris Lattner | 0579f7f | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 1427 |     case bitc::FUNC_CODE_INST_LOAD: { // LOAD: [opty, op, align, vol] | 
| Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1428 |       unsigned OpNum = 0; | 
 | 1429 |       Value *Op; | 
 | 1430 |       if (getValueTypePair(Record, OpNum, NextValueNo, Op) || | 
 | 1431 |           OpNum+2 != Record.size()) | 
| Chris Lattner | abfbf85 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 1432 |         return Error("Invalid LOAD record"); | 
| Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1433 |        | 
 | 1434 |       I = new LoadInst(Op, "", Record[OpNum+1], (1 << Record[OpNum]) >> 1); | 
| Chris Lattner | a7c49aa | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 1435 |       break; | 
| Chris Lattner | 0579f7f | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 1436 |     } | 
| Chris Lattner | abfbf85 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 1437 |     case bitc::FUNC_CODE_INST_STORE: { // STORE:[val, valty, ptr, align, vol] | 
 | 1438 |       unsigned OpNum = 0; | 
 | 1439 |       Value *Val, *Ptr; | 
 | 1440 |       if (getValueTypePair(Record, OpNum, NextValueNo, Val) || | 
 | 1441 |           getValue(Record, OpNum, PointerType::get(Val->getType()), Ptr) || | 
 | 1442 |           OpNum+2 != Record.size()) | 
| Chris Lattner | 0579f7f | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 1443 |         return Error("Invalid STORE record"); | 
| Chris Lattner | abfbf85 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 1444 |        | 
 | 1445 |       I = new StoreInst(Val, Ptr, Record[OpNum+1], (1 << Record[OpNum]) >> 1); | 
| Chris Lattner | 0579f7f | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 1446 |       break; | 
 | 1447 |     } | 
| Chris Lattner | 7652019 | 2007-05-03 22:34:03 +0000 | [diff] [blame] | 1448 |     case bitc::FUNC_CODE_INST_CALL: { // CALL: [cc, fnty, fnid, arg0, arg1...] | 
| Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1449 |       if (Record.size() < 1) | 
| Chris Lattner | 0579f7f | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 1450 |         return Error("Invalid CALL record"); | 
| Chris Lattner | 7652019 | 2007-05-03 22:34:03 +0000 | [diff] [blame] | 1451 |       unsigned CCInfo = Record[0]; | 
| Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1452 |        | 
 | 1453 |       unsigned OpNum = 1; | 
 | 1454 |       Value *Callee; | 
 | 1455 |       if (getValueTypePair(Record, OpNum, NextValueNo, Callee)) | 
 | 1456 |         return Error("Invalid CALL record"); | 
 | 1457 |        | 
 | 1458 |       const PointerType *OpTy = dyn_cast<PointerType>(Callee->getType()); | 
| Chris Lattner | 0579f7f | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 1459 |       const FunctionType *FTy = 0; | 
 | 1460 |       if (OpTy) FTy = dyn_cast<FunctionType>(OpTy->getElementType()); | 
| Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1461 |       if (!FTy || Record.size() < FTy->getNumParams()+OpNum) | 
| Chris Lattner | 0579f7f | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 1462 |         return Error("Invalid CALL record"); | 
 | 1463 |        | 
 | 1464 |       SmallVector<Value*, 16> Args; | 
 | 1465 |       // Read the fixed params. | 
| Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1466 |       for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) { | 
 | 1467 |         Args.push_back(getFnValueByID(Record[OpNum], FTy->getParamType(i))); | 
| Chris Lattner | 0579f7f | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 1468 |         if (Args.back() == 0) return Error("Invalid CALL record"); | 
 | 1469 |       } | 
 | 1470 |        | 
| Chris Lattner | 0579f7f | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 1471 |       // Read type/value pairs for varargs params. | 
| Chris Lattner | 0579f7f | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 1472 |       if (!FTy->isVarArg()) { | 
| Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1473 |         if (OpNum != Record.size()) | 
| Chris Lattner | 0579f7f | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 1474 |           return Error("Invalid CALL record"); | 
 | 1475 |       } else { | 
| Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1476 |         while (OpNum != Record.size()) { | 
 | 1477 |           Value *Op; | 
 | 1478 |           if (getValueTypePair(Record, OpNum, NextValueNo, Op)) | 
 | 1479 |             return Error("Invalid CALL record"); | 
 | 1480 |           Args.push_back(Op); | 
| Chris Lattner | 0579f7f | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 1481 |         } | 
 | 1482 |       } | 
 | 1483 |        | 
 | 1484 |       I = new CallInst(Callee, &Args[0], Args.size()); | 
| Chris Lattner | 7652019 | 2007-05-03 22:34:03 +0000 | [diff] [blame] | 1485 |       cast<CallInst>(I)->setCallingConv(CCInfo>>1); | 
 | 1486 |       cast<CallInst>(I)->setTailCall(CCInfo & 1); | 
| Chris Lattner | 0579f7f | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 1487 |       break; | 
 | 1488 |     } | 
 | 1489 |     case bitc::FUNC_CODE_INST_VAARG: { // VAARG: [valistty, valist, instty] | 
 | 1490 |       if (Record.size() < 3) | 
 | 1491 |         return Error("Invalid VAARG record"); | 
 | 1492 |       const Type *OpTy = getTypeByID(Record[0]); | 
 | 1493 |       Value *Op = getFnValueByID(Record[1], OpTy); | 
 | 1494 |       const Type *ResTy = getTypeByID(Record[2]); | 
 | 1495 |       if (!OpTy || !Op || !ResTy) | 
 | 1496 |         return Error("Invalid VAARG record"); | 
 | 1497 |       I = new VAArgInst(Op, ResTy); | 
 | 1498 |       break; | 
 | 1499 |     } | 
| Chris Lattner | a7c49aa | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 1500 |     } | 
 | 1501 |  | 
 | 1502 |     // Add instruction to end of current BB.  If there is no current BB, reject | 
 | 1503 |     // this file. | 
 | 1504 |     if (CurBB == 0) { | 
 | 1505 |       delete I; | 
 | 1506 |       return Error("Invalid instruction with no BB"); | 
 | 1507 |     } | 
 | 1508 |     CurBB->getInstList().push_back(I); | 
 | 1509 |      | 
 | 1510 |     // If this was a terminator instruction, move to the next block. | 
 | 1511 |     if (isa<TerminatorInst>(I)) { | 
 | 1512 |       ++CurBBNo; | 
 | 1513 |       CurBB = CurBBNo < FunctionBBs.size() ? FunctionBBs[CurBBNo] : 0; | 
 | 1514 |     } | 
 | 1515 |      | 
 | 1516 |     // Non-void values get registered in the value table for future use. | 
 | 1517 |     if (I && I->getType() != Type::VoidTy) | 
 | 1518 |       ValueList.AssignValue(I, NextValueNo++); | 
| Chris Lattner | 980e5aa | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 1519 |   } | 
 | 1520 |    | 
| Chris Lattner | a7c49aa | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 1521 |   // Check the function list for unresolved values. | 
 | 1522 |   if (Argument *A = dyn_cast<Argument>(ValueList.back())) { | 
 | 1523 |     if (A->getParent() == 0) { | 
 | 1524 |       // We found at least one unresolved value.  Nuke them all to avoid leaks. | 
 | 1525 |       for (unsigned i = ModuleValueListSize, e = ValueList.size(); i != e; ++i){ | 
 | 1526 |         if ((A = dyn_cast<Argument>(ValueList.back())) && A->getParent() == 0) { | 
 | 1527 |           A->replaceAllUsesWith(UndefValue::get(A->getType())); | 
 | 1528 |           delete A; | 
 | 1529 |         } | 
 | 1530 |       } | 
| Chris Lattner | 35a0470 | 2007-05-04 03:50:29 +0000 | [diff] [blame] | 1531 |       return Error("Never resolved value found in function!"); | 
| Chris Lattner | a7c49aa | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 1532 |     } | 
| Chris Lattner | a7c49aa | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 1533 |   } | 
| Chris Lattner | 980e5aa | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 1534 |    | 
 | 1535 |   // Trim the value list down to the size it was before we parsed this function. | 
 | 1536 |   ValueList.shrinkTo(ModuleValueListSize); | 
 | 1537 |   std::vector<BasicBlock*>().swap(FunctionBBs); | 
 | 1538 |    | 
| Chris Lattner | 48f8487 | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 1539 |   return false; | 
 | 1540 | } | 
 | 1541 |  | 
 | 1542 |  | 
| Chris Lattner | c453f76 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 1543 | //===----------------------------------------------------------------------===// | 
 | 1544 | // External interface | 
 | 1545 | //===----------------------------------------------------------------------===// | 
 | 1546 |  | 
 | 1547 | /// getBitcodeModuleProvider - lazy function-at-a-time loading from a file. | 
 | 1548 | /// | 
 | 1549 | ModuleProvider *llvm::getBitcodeModuleProvider(MemoryBuffer *Buffer, | 
 | 1550 |                                                std::string *ErrMsg) { | 
 | 1551 |   BitcodeReader *R = new BitcodeReader(Buffer); | 
 | 1552 |   if (R->ParseBitcode()) { | 
 | 1553 |     if (ErrMsg) | 
 | 1554 |       *ErrMsg = R->getErrorString(); | 
 | 1555 |      | 
 | 1556 |     // Don't let the BitcodeReader dtor delete 'Buffer'. | 
 | 1557 |     R->releaseMemoryBuffer(); | 
 | 1558 |     delete R; | 
 | 1559 |     return 0; | 
 | 1560 |   } | 
 | 1561 |   return R; | 
 | 1562 | } | 
 | 1563 |  | 
 | 1564 | /// ParseBitcodeFile - Read the specified bitcode file, returning the module. | 
 | 1565 | /// If an error occurs, return null and fill in *ErrMsg if non-null. | 
 | 1566 | Module *llvm::ParseBitcodeFile(MemoryBuffer *Buffer, std::string *ErrMsg){ | 
 | 1567 |   BitcodeReader *R; | 
 | 1568 |   R = static_cast<BitcodeReader*>(getBitcodeModuleProvider(Buffer, ErrMsg)); | 
 | 1569 |   if (!R) return 0; | 
 | 1570 |    | 
 | 1571 |   // Read the whole module, get a pointer to it, tell ModuleProvider not to | 
 | 1572 |   // delete it when its dtor is run. | 
 | 1573 |   Module *M = R->releaseModule(ErrMsg); | 
 | 1574 |    | 
 | 1575 |   // Don't let the BitcodeReader dtor delete 'Buffer'. | 
 | 1576 |   R->releaseMemoryBuffer(); | 
 | 1577 |   delete R; | 
 | 1578 |   return M; | 
 | 1579 | } |