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 | |
| 14 | #include "BitcodeReader.h" |
| 15 | #include "llvm/Bitcode/BitstreamReader.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" |
| 18 | #include "llvm/Module.h" |
Chris Lattner | 0b2482a | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallString.h" |
Chris Lattner | 0eef080 | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 20 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 21 | using namespace llvm; |
| 22 | |
| 23 | /// ConvertToString - Convert a string from a record into an std::string, return |
| 24 | /// true on failure. |
Chris Lattner | 0b2482a | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 25 | template<typename StrTy> |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 26 | static bool ConvertToString(SmallVector<uint64_t, 64> &Record, unsigned Idx, |
Chris Lattner | 0b2482a | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 27 | StrTy &Result) { |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 28 | if (Record.size() < Idx+1 || Record.size() < Record[Idx]+Idx+1) |
| 29 | return true; |
| 30 | |
| 31 | for (unsigned i = 0, e = Record[Idx]; i != e; ++i) |
| 32 | Result += (char)Record[Idx+i+1]; |
| 33 | return false; |
| 34 | } |
| 35 | |
| 36 | static GlobalValue::LinkageTypes GetDecodedLinkage(unsigned Val) { |
| 37 | switch (Val) { |
| 38 | default: // Map unknown/new linkages to external |
| 39 | case 0: return GlobalValue::ExternalLinkage; |
| 40 | case 1: return GlobalValue::WeakLinkage; |
| 41 | case 2: return GlobalValue::AppendingLinkage; |
| 42 | case 3: return GlobalValue::InternalLinkage; |
| 43 | case 4: return GlobalValue::LinkOnceLinkage; |
| 44 | case 5: return GlobalValue::DLLImportLinkage; |
| 45 | case 6: return GlobalValue::DLLExportLinkage; |
| 46 | case 7: return GlobalValue::ExternalWeakLinkage; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | static GlobalValue::VisibilityTypes GetDecodedVisibility(unsigned Val) { |
| 51 | switch (Val) { |
| 52 | default: // Map unknown visibilities to default. |
| 53 | case 0: return GlobalValue::DefaultVisibility; |
| 54 | case 1: return GlobalValue::HiddenVisibility; |
| 55 | } |
| 56 | } |
| 57 | |
Chris Lattner | f581c3b | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 58 | static int GetDecodedCastOpcode(unsigned Val) { |
| 59 | switch (Val) { |
| 60 | default: return -1; |
| 61 | case bitc::CAST_TRUNC : return Instruction::Trunc; |
| 62 | case bitc::CAST_ZEXT : return Instruction::ZExt; |
| 63 | case bitc::CAST_SEXT : return Instruction::SExt; |
| 64 | case bitc::CAST_FPTOUI : return Instruction::FPToUI; |
| 65 | case bitc::CAST_FPTOSI : return Instruction::FPToSI; |
| 66 | case bitc::CAST_UITOFP : return Instruction::UIToFP; |
| 67 | case bitc::CAST_SITOFP : return Instruction::SIToFP; |
| 68 | case bitc::CAST_FPTRUNC : return Instruction::FPTrunc; |
| 69 | case bitc::CAST_FPEXT : return Instruction::FPExt; |
| 70 | case bitc::CAST_PTRTOINT: return Instruction::PtrToInt; |
| 71 | case bitc::CAST_INTTOPTR: return Instruction::IntToPtr; |
| 72 | case bitc::CAST_BITCAST : return Instruction::BitCast; |
| 73 | } |
| 74 | } |
| 75 | static int GetDecodedBinaryOpcode(unsigned Val, const Type *Ty) { |
| 76 | switch (Val) { |
| 77 | default: return -1; |
| 78 | case bitc::BINOP_ADD: return Instruction::Add; |
| 79 | case bitc::BINOP_SUB: return Instruction::Sub; |
| 80 | case bitc::BINOP_MUL: return Instruction::Mul; |
| 81 | case bitc::BINOP_UDIV: return Instruction::UDiv; |
| 82 | case bitc::BINOP_SDIV: |
| 83 | return Ty->isFPOrFPVector() ? Instruction::FDiv : Instruction::SDiv; |
| 84 | case bitc::BINOP_UREM: return Instruction::URem; |
| 85 | case bitc::BINOP_SREM: |
| 86 | return Ty->isFPOrFPVector() ? Instruction::FRem : Instruction::SRem; |
| 87 | case bitc::BINOP_SHL: return Instruction::Shl; |
| 88 | case bitc::BINOP_LSHR: return Instruction::LShr; |
| 89 | case bitc::BINOP_ASHR: return Instruction::AShr; |
| 90 | case bitc::BINOP_AND: return Instruction::And; |
| 91 | case bitc::BINOP_OR: return Instruction::Or; |
| 92 | case bitc::BINOP_XOR: return Instruction::Xor; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | |
Chris Lattner | 522b7b1 | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 97 | namespace { |
| 98 | /// @brief A class for maintaining the slot number definition |
| 99 | /// as a placeholder for the actual definition for forward constants defs. |
| 100 | class ConstantPlaceHolder : public ConstantExpr { |
| 101 | ConstantPlaceHolder(); // DO NOT IMPLEMENT |
| 102 | void operator=(const ConstantPlaceHolder &); // DO NOT IMPLEMENT |
Chris Lattner | f581c3b | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 103 | public: |
| 104 | Use Op; |
| 105 | ConstantPlaceHolder(const Type *Ty) |
| 106 | : ConstantExpr(Ty, Instruction::UserOp1, &Op, 1), |
| 107 | Op(UndefValue::get(Type::Int32Ty), this) { |
Chris Lattner | 522b7b1 | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 108 | } |
| 109 | }; |
| 110 | } |
| 111 | |
| 112 | Constant *BitcodeReaderValueList::getConstantFwdRef(unsigned Idx, |
| 113 | const Type *Ty) { |
| 114 | if (Idx >= size()) { |
| 115 | // Insert a bunch of null values. |
| 116 | Uses.resize(Idx+1); |
| 117 | OperandList = &Uses[0]; |
| 118 | NumOperands = Idx+1; |
| 119 | } |
| 120 | |
Chris Lattner | f581c3b | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 121 | if (Uses[Idx]) { |
| 122 | assert(Ty == getOperand(Idx)->getType() && |
| 123 | "Type mismatch in constant table!"); |
Chris Lattner | 522b7b1 | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 124 | return cast<Constant>(getOperand(Idx)); |
Chris Lattner | f581c3b | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 125 | } |
Chris Lattner | 522b7b1 | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 126 | |
| 127 | // Create and return a placeholder, which will later be RAUW'd. |
| 128 | Constant *C = new ConstantPlaceHolder(Ty); |
| 129 | Uses[Idx].init(C, this); |
| 130 | return C; |
| 131 | } |
| 132 | |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 133 | |
| 134 | const Type *BitcodeReader::getTypeByID(unsigned ID, bool isTypeTable) { |
| 135 | // If the TypeID is in range, return it. |
| 136 | if (ID < TypeList.size()) |
| 137 | return TypeList[ID].get(); |
| 138 | if (!isTypeTable) return 0; |
| 139 | |
| 140 | // The type table allows forward references. Push as many Opaque types as |
| 141 | // needed to get up to ID. |
| 142 | while (TypeList.size() <= ID) |
| 143 | TypeList.push_back(OpaqueType::get()); |
| 144 | return TypeList.back().get(); |
| 145 | } |
| 146 | |
| 147 | |
| 148 | bool BitcodeReader::ParseTypeTable(BitstreamReader &Stream) { |
| 149 | if (Stream.EnterSubBlock()) |
| 150 | return Error("Malformed block record"); |
| 151 | |
| 152 | if (!TypeList.empty()) |
| 153 | return Error("Multiple TYPE_BLOCKs found!"); |
| 154 | |
| 155 | SmallVector<uint64_t, 64> Record; |
| 156 | unsigned NumRecords = 0; |
| 157 | |
| 158 | // Read all the records for this type table. |
| 159 | while (1) { |
| 160 | unsigned Code = Stream.ReadCode(); |
| 161 | if (Code == bitc::END_BLOCK) { |
| 162 | if (NumRecords != TypeList.size()) |
| 163 | return Error("Invalid type forward reference in TYPE_BLOCK"); |
Chris Lattner | f66d20d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 164 | if (Stream.ReadBlockEnd()) |
| 165 | return Error("Error at end of type table block"); |
| 166 | return false; |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | if (Code == bitc::ENTER_SUBBLOCK) { |
| 170 | // No known subblocks, always skip them. |
| 171 | Stream.ReadSubBlockID(); |
| 172 | if (Stream.SkipBlock()) |
| 173 | return Error("Malformed block record"); |
| 174 | continue; |
| 175 | } |
| 176 | |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 177 | if (Code == bitc::DEFINE_ABBREV) { |
Chris Lattner | d127c1b | 2007-04-23 18:58:34 +0000 | [diff] [blame] | 178 | Stream.ReadAbbrevRecord(); |
| 179 | continue; |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | // Read a record. |
| 183 | Record.clear(); |
| 184 | const Type *ResultTy = 0; |
| 185 | switch (Stream.ReadRecord(Code, Record)) { |
| 186 | default: // Default behavior: unknown type. |
| 187 | ResultTy = 0; |
| 188 | break; |
| 189 | case bitc::TYPE_CODE_NUMENTRY: // TYPE_CODE_NUMENTRY: [numentries] |
| 190 | // TYPE_CODE_NUMENTRY contains a count of the number of types in the |
| 191 | // type list. This allows us to reserve space. |
| 192 | if (Record.size() < 1) |
| 193 | return Error("Invalid TYPE_CODE_NUMENTRY record"); |
| 194 | TypeList.reserve(Record[0]); |
| 195 | continue; |
| 196 | case bitc::TYPE_CODE_META: // TYPE_CODE_META: [metacode]... |
| 197 | // No metadata supported yet. |
| 198 | if (Record.size() < 1) |
| 199 | return Error("Invalid TYPE_CODE_META record"); |
| 200 | continue; |
| 201 | |
| 202 | case bitc::TYPE_CODE_VOID: // VOID |
| 203 | ResultTy = Type::VoidTy; |
| 204 | break; |
| 205 | case bitc::TYPE_CODE_FLOAT: // FLOAT |
| 206 | ResultTy = Type::FloatTy; |
| 207 | break; |
| 208 | case bitc::TYPE_CODE_DOUBLE: // DOUBLE |
| 209 | ResultTy = Type::DoubleTy; |
| 210 | break; |
| 211 | case bitc::TYPE_CODE_LABEL: // LABEL |
| 212 | ResultTy = Type::LabelTy; |
| 213 | break; |
| 214 | case bitc::TYPE_CODE_OPAQUE: // OPAQUE |
| 215 | ResultTy = 0; |
| 216 | break; |
| 217 | case bitc::TYPE_CODE_INTEGER: // INTEGER: [width] |
| 218 | if (Record.size() < 1) |
| 219 | return Error("Invalid Integer type record"); |
| 220 | |
| 221 | ResultTy = IntegerType::get(Record[0]); |
| 222 | break; |
| 223 | case bitc::TYPE_CODE_POINTER: // POINTER: [pointee type] |
| 224 | if (Record.size() < 1) |
| 225 | return Error("Invalid POINTER type record"); |
| 226 | ResultTy = PointerType::get(getTypeByID(Record[0], true)); |
| 227 | break; |
| 228 | case bitc::TYPE_CODE_FUNCTION: { |
| 229 | // FUNCTION: [vararg, retty, #pararms, paramty N] |
| 230 | if (Record.size() < 3 || Record.size() < Record[2]+3) |
| 231 | return Error("Invalid FUNCTION type record"); |
| 232 | std::vector<const Type*> ArgTys; |
| 233 | for (unsigned i = 0, e = Record[2]; i != e; ++i) |
| 234 | ArgTys.push_back(getTypeByID(Record[3+i], true)); |
| 235 | |
| 236 | // FIXME: PARAM TYS. |
| 237 | ResultTy = FunctionType::get(getTypeByID(Record[1], true), ArgTys, |
| 238 | Record[0]); |
| 239 | break; |
| 240 | } |
| 241 | case bitc::TYPE_CODE_STRUCT: { // STRUCT: [ispacked, #elts, eltty x N] |
| 242 | if (Record.size() < 2 || Record.size() < Record[1]+2) |
| 243 | return Error("Invalid STRUCT type record"); |
| 244 | std::vector<const Type*> EltTys; |
| 245 | for (unsigned i = 0, e = Record[1]; i != e; ++i) |
| 246 | EltTys.push_back(getTypeByID(Record[2+i], true)); |
| 247 | ResultTy = StructType::get(EltTys, Record[0]); |
| 248 | break; |
| 249 | } |
| 250 | case bitc::TYPE_CODE_ARRAY: // ARRAY: [numelts, eltty] |
| 251 | if (Record.size() < 2) |
| 252 | return Error("Invalid ARRAY type record"); |
| 253 | ResultTy = ArrayType::get(getTypeByID(Record[1], true), Record[0]); |
| 254 | break; |
| 255 | case bitc::TYPE_CODE_VECTOR: // VECTOR: [numelts, eltty] |
| 256 | if (Record.size() < 2) |
| 257 | return Error("Invalid VECTOR type record"); |
| 258 | ResultTy = VectorType::get(getTypeByID(Record[1], true), Record[0]); |
| 259 | break; |
| 260 | } |
| 261 | |
| 262 | if (NumRecords == TypeList.size()) { |
| 263 | // If this is a new type slot, just append it. |
| 264 | TypeList.push_back(ResultTy ? ResultTy : OpaqueType::get()); |
| 265 | ++NumRecords; |
| 266 | } else if (ResultTy == 0) { |
| 267 | // Otherwise, this was forward referenced, so an opaque type was created, |
| 268 | // but the result type is actually just an opaque. Leave the one we |
| 269 | // created previously. |
| 270 | ++NumRecords; |
| 271 | } else { |
| 272 | // Otherwise, this was forward referenced, so an opaque type was created. |
| 273 | // Resolve the opaque type to the real type now. |
| 274 | assert(NumRecords < TypeList.size() && "Typelist imbalance"); |
| 275 | const OpaqueType *OldTy = cast<OpaqueType>(TypeList[NumRecords++].get()); |
| 276 | |
| 277 | // Don't directly push the new type on the Tab. Instead we want to replace |
| 278 | // the opaque type we previously inserted with the new concrete value. The |
| 279 | // refinement from the abstract (opaque) type to the new type causes all |
| 280 | // uses of the abstract type to use the concrete type (NewTy). This will |
| 281 | // also cause the opaque type to be deleted. |
| 282 | const_cast<OpaqueType*>(OldTy)->refineAbstractTypeTo(ResultTy); |
| 283 | |
| 284 | // 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] | 285 | // value table... or with a preexisting type that was already in the |
| 286 | // system. Let's just make sure it did. |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 287 | assert(TypeList[NumRecords-1].get() != OldTy && |
| 288 | "refineAbstractType didn't work!"); |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | |
| 294 | bool BitcodeReader::ParseTypeSymbolTable(BitstreamReader &Stream) { |
| 295 | if (Stream.EnterSubBlock()) |
| 296 | return Error("Malformed block record"); |
| 297 | |
| 298 | SmallVector<uint64_t, 64> Record; |
| 299 | |
| 300 | // Read all the records for this type table. |
| 301 | std::string TypeName; |
| 302 | while (1) { |
| 303 | unsigned Code = Stream.ReadCode(); |
Chris Lattner | f66d20d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 304 | if (Code == bitc::END_BLOCK) { |
| 305 | if (Stream.ReadBlockEnd()) |
| 306 | return Error("Error at end of type symbol table block"); |
| 307 | return false; |
| 308 | } |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 309 | |
| 310 | if (Code == bitc::ENTER_SUBBLOCK) { |
| 311 | // No known subblocks, always skip them. |
| 312 | Stream.ReadSubBlockID(); |
| 313 | if (Stream.SkipBlock()) |
| 314 | return Error("Malformed block record"); |
| 315 | continue; |
| 316 | } |
| 317 | |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 318 | if (Code == bitc::DEFINE_ABBREV) { |
Chris Lattner | d127c1b | 2007-04-23 18:58:34 +0000 | [diff] [blame] | 319 | Stream.ReadAbbrevRecord(); |
| 320 | continue; |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | // Read a record. |
| 324 | Record.clear(); |
| 325 | switch (Stream.ReadRecord(Code, Record)) { |
| 326 | default: // Default behavior: unknown type. |
| 327 | break; |
Chris Lattner | 6dbfd7b | 2007-04-24 00:18:21 +0000 | [diff] [blame] | 328 | case bitc::TST_CODE_ENTRY: // TST_ENTRY: [typeid, namelen, namechar x N] |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 329 | if (ConvertToString(Record, 1, TypeName)) |
| 330 | return Error("Invalid TST_ENTRY record"); |
| 331 | unsigned TypeID = Record[0]; |
| 332 | if (TypeID >= TypeList.size()) |
| 333 | return Error("Invalid Type ID in TST_ENTRY record"); |
| 334 | |
| 335 | TheModule->addTypeName(TypeName, TypeList[TypeID].get()); |
| 336 | TypeName.clear(); |
| 337 | break; |
| 338 | } |
| 339 | } |
| 340 | } |
| 341 | |
Chris Lattner | 0b2482a | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 342 | bool BitcodeReader::ParseValueSymbolTable(BitstreamReader &Stream) { |
| 343 | if (Stream.EnterSubBlock()) |
| 344 | return Error("Malformed block record"); |
| 345 | |
| 346 | SmallVector<uint64_t, 64> Record; |
| 347 | |
| 348 | // Read all the records for this value table. |
| 349 | SmallString<128> ValueName; |
| 350 | while (1) { |
| 351 | unsigned Code = Stream.ReadCode(); |
Chris Lattner | f66d20d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 352 | if (Code == bitc::END_BLOCK) { |
| 353 | if (Stream.ReadBlockEnd()) |
| 354 | return Error("Error at end of value symbol table block"); |
| 355 | return false; |
| 356 | } |
Chris Lattner | 0b2482a | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 357 | if (Code == bitc::ENTER_SUBBLOCK) { |
| 358 | // No known subblocks, always skip them. |
| 359 | Stream.ReadSubBlockID(); |
| 360 | if (Stream.SkipBlock()) |
| 361 | return Error("Malformed block record"); |
| 362 | continue; |
| 363 | } |
| 364 | |
| 365 | if (Code == bitc::DEFINE_ABBREV) { |
| 366 | Stream.ReadAbbrevRecord(); |
| 367 | continue; |
| 368 | } |
| 369 | |
| 370 | // Read a record. |
| 371 | Record.clear(); |
| 372 | switch (Stream.ReadRecord(Code, Record)) { |
| 373 | default: // Default behavior: unknown type. |
| 374 | break; |
Chris Lattner | 6dbfd7b | 2007-04-24 00:18:21 +0000 | [diff] [blame] | 375 | case bitc::TST_CODE_ENTRY: // VST_ENTRY: [valueid, namelen, namechar x N] |
Chris Lattner | 0b2482a | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 376 | if (ConvertToString(Record, 1, ValueName)) |
| 377 | return Error("Invalid TST_ENTRY record"); |
| 378 | unsigned ValueID = Record[0]; |
| 379 | if (ValueID >= ValueList.size()) |
| 380 | return Error("Invalid Value ID in VST_ENTRY record"); |
| 381 | Value *V = ValueList[ValueID]; |
| 382 | |
| 383 | V->setName(&ValueName[0], ValueName.size()); |
| 384 | ValueName.clear(); |
| 385 | break; |
| 386 | } |
| 387 | } |
| 388 | } |
| 389 | |
Chris Lattner | 0eef080 | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 390 | /// DecodeSignRotatedValue - Decode a signed value stored with the sign bit in |
| 391 | /// the LSB for dense VBR encoding. |
| 392 | static uint64_t DecodeSignRotatedValue(uint64_t V) { |
| 393 | if ((V & 1) == 0) |
| 394 | return V >> 1; |
| 395 | if (V != 1) |
| 396 | return -(V >> 1); |
| 397 | // There is no such thing as -0 with integers. "-0" really means MININT. |
| 398 | return 1ULL << 63; |
| 399 | } |
| 400 | |
Chris Lattner | 07d98b4 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 401 | /// ResolveGlobalAndAliasInits - Resolve all of the initializers for global |
| 402 | /// values and aliases that we can. |
| 403 | bool BitcodeReader::ResolveGlobalAndAliasInits() { |
| 404 | std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInitWorklist; |
| 405 | std::vector<std::pair<GlobalAlias*, unsigned> > AliasInitWorklist; |
| 406 | |
| 407 | GlobalInitWorklist.swap(GlobalInits); |
| 408 | AliasInitWorklist.swap(AliasInits); |
| 409 | |
| 410 | while (!GlobalInitWorklist.empty()) { |
Chris Lattner | 198f34a | 2007-04-26 03:27:58 +0000 | [diff] [blame^] | 411 | unsigned ValID = GlobalInitWorklist.back().second; |
Chris Lattner | 07d98b4 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 412 | if (ValID >= ValueList.size()) { |
| 413 | // 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^] | 414 | GlobalInits.push_back(GlobalInitWorklist.back()); |
Chris Lattner | 07d98b4 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 415 | } else { |
| 416 | if (Constant *C = dyn_cast<Constant>(ValueList[ValID])) |
| 417 | GlobalInitWorklist.back().first->setInitializer(C); |
| 418 | else |
| 419 | return Error("Global variable initializer is not a constant!"); |
| 420 | } |
| 421 | GlobalInitWorklist.pop_back(); |
| 422 | } |
| 423 | |
| 424 | while (!AliasInitWorklist.empty()) { |
| 425 | unsigned ValID = AliasInitWorklist.back().second; |
| 426 | if (ValID >= ValueList.size()) { |
| 427 | AliasInits.push_back(AliasInitWorklist.back()); |
| 428 | } else { |
| 429 | if (Constant *C = dyn_cast<Constant>(ValueList[ValID])) |
| 430 | AliasInitWorklist.back().first->setAliasee( |
| 431 | // FIXME: |
| 432 | cast<GlobalValue>(C)); |
| 433 | else |
| 434 | return Error("Alias initializer is not a constant!"); |
| 435 | } |
| 436 | AliasInitWorklist.pop_back(); |
| 437 | } |
| 438 | return false; |
| 439 | } |
| 440 | |
| 441 | |
Chris Lattner | e16504e | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 442 | bool BitcodeReader::ParseConstants(BitstreamReader &Stream) { |
| 443 | if (Stream.EnterSubBlock()) |
| 444 | return Error("Malformed block record"); |
| 445 | |
| 446 | SmallVector<uint64_t, 64> Record; |
| 447 | |
| 448 | // Read all the records for this value table. |
| 449 | const Type *CurTy = Type::Int32Ty; |
Chris Lattner | 522b7b1 | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 450 | unsigned NextCstNo = ValueList.size(); |
Chris Lattner | e16504e | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 451 | while (1) { |
| 452 | unsigned Code = Stream.ReadCode(); |
| 453 | if (Code == bitc::END_BLOCK) { |
Chris Lattner | 522b7b1 | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 454 | if (NextCstNo != ValueList.size()) |
| 455 | return Error("Invalid constant reference!"); |
| 456 | |
Chris Lattner | f66d20d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 457 | if (Stream.ReadBlockEnd()) |
| 458 | return Error("Error at end of constants block"); |
| 459 | return false; |
Chris Lattner | e16504e | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | if (Code == bitc::ENTER_SUBBLOCK) { |
| 463 | // No known subblocks, always skip them. |
| 464 | Stream.ReadSubBlockID(); |
| 465 | if (Stream.SkipBlock()) |
| 466 | return Error("Malformed block record"); |
| 467 | continue; |
| 468 | } |
| 469 | |
| 470 | if (Code == bitc::DEFINE_ABBREV) { |
| 471 | Stream.ReadAbbrevRecord(); |
| 472 | continue; |
| 473 | } |
| 474 | |
| 475 | // Read a record. |
| 476 | Record.clear(); |
| 477 | Value *V = 0; |
| 478 | switch (Stream.ReadRecord(Code, Record)) { |
| 479 | default: // Default behavior: unknown constant |
| 480 | case bitc::CST_CODE_UNDEF: // UNDEF |
| 481 | V = UndefValue::get(CurTy); |
| 482 | break; |
| 483 | case bitc::CST_CODE_SETTYPE: // SETTYPE: [typeid] |
| 484 | if (Record.empty()) |
| 485 | return Error("Malformed CST_SETTYPE record"); |
| 486 | if (Record[0] >= TypeList.size()) |
| 487 | return Error("Invalid Type ID in CST_SETTYPE record"); |
| 488 | CurTy = TypeList[Record[0]]; |
Chris Lattner | 0eef080 | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 489 | continue; // Skip the ValueList manipulation. |
Chris Lattner | e16504e | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 490 | case bitc::CST_CODE_NULL: // NULL |
| 491 | V = Constant::getNullValue(CurTy); |
| 492 | break; |
| 493 | case bitc::CST_CODE_INTEGER: // INTEGER: [intval] |
Chris Lattner | 0eef080 | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 494 | if (!isa<IntegerType>(CurTy) || Record.empty()) |
| 495 | return Error("Invalid CST_INTEGER record"); |
| 496 | V = ConstantInt::get(CurTy, DecodeSignRotatedValue(Record[0])); |
| 497 | break; |
| 498 | case bitc::CST_CODE_WIDE_INTEGER: {// WIDE_INTEGER: [n, n x intval] |
| 499 | if (!isa<IntegerType>(CurTy) || Record.empty() || |
| 500 | Record.size() < Record[0]+1) |
| 501 | return Error("Invalid WIDE_INTEGER record"); |
| 502 | |
| 503 | unsigned NumWords = Record[0]; |
Chris Lattner | 084a844 | 2007-04-24 17:22:05 +0000 | [diff] [blame] | 504 | SmallVector<uint64_t, 8> Words; |
| 505 | Words.resize(NumWords); |
Chris Lattner | 0eef080 | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 506 | for (unsigned i = 0; i != NumWords; ++i) |
Chris Lattner | 084a844 | 2007-04-24 17:22:05 +0000 | [diff] [blame] | 507 | Words[i] = DecodeSignRotatedValue(Record[i+1]); |
Chris Lattner | 0eef080 | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 508 | V = ConstantInt::get(APInt(cast<IntegerType>(CurTy)->getBitWidth(), |
Chris Lattner | 084a844 | 2007-04-24 17:22:05 +0000 | [diff] [blame] | 509 | NumWords, &Words[0])); |
Chris Lattner | 0eef080 | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 510 | break; |
| 511 | } |
| 512 | case bitc::CST_CODE_FLOAT: // FLOAT: [fpval] |
| 513 | if (Record.empty()) |
| 514 | return Error("Invalid FLOAT record"); |
| 515 | if (CurTy == Type::FloatTy) |
| 516 | V = ConstantFP::get(CurTy, BitsToFloat(Record[0])); |
| 517 | else if (CurTy == Type::DoubleTy) |
| 518 | V = ConstantFP::get(CurTy, BitsToDouble(Record[0])); |
Chris Lattner | e16504e | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 519 | else |
Chris Lattner | 0eef080 | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 520 | V = UndefValue::get(CurTy); |
Chris Lattner | e16504e | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 521 | break; |
Chris Lattner | 522b7b1 | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 522 | |
| 523 | case bitc::CST_CODE_AGGREGATE: {// AGGREGATE: [n, n x value number] |
| 524 | if (Record.empty() || Record.size() < Record[0]+1) |
| 525 | return Error("Invalid CST_AGGREGATE record"); |
| 526 | |
| 527 | unsigned Size = Record[0]; |
| 528 | std::vector<Constant*> Elts; |
| 529 | |
| 530 | if (const StructType *STy = dyn_cast<StructType>(CurTy)) { |
| 531 | for (unsigned i = 0; i != Size; ++i) |
| 532 | Elts.push_back(ValueList.getConstantFwdRef(Record[i+1], |
| 533 | STy->getElementType(i))); |
| 534 | V = ConstantStruct::get(STy, Elts); |
| 535 | } else if (const ArrayType *ATy = dyn_cast<ArrayType>(CurTy)) { |
| 536 | const Type *EltTy = ATy->getElementType(); |
| 537 | for (unsigned i = 0; i != Size; ++i) |
| 538 | Elts.push_back(ValueList.getConstantFwdRef(Record[i+1], EltTy)); |
| 539 | V = ConstantArray::get(ATy, Elts); |
| 540 | } else if (const VectorType *VTy = dyn_cast<VectorType>(CurTy)) { |
| 541 | const Type *EltTy = VTy->getElementType(); |
| 542 | for (unsigned i = 0; i != Size; ++i) |
| 543 | Elts.push_back(ValueList.getConstantFwdRef(Record[i+1], EltTy)); |
| 544 | V = ConstantVector::get(Elts); |
| 545 | } else { |
| 546 | V = UndefValue::get(CurTy); |
| 547 | } |
Chris Lattner | f581c3b | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 548 | break; |
| 549 | } |
| 550 | |
| 551 | case bitc::CST_CODE_CE_BINOP: { // CE_BINOP: [opcode, opval, opval] |
| 552 | if (Record.size() < 3) return Error("Invalid CE_BINOP record"); |
| 553 | int Opc = GetDecodedBinaryOpcode(Record[0], CurTy); |
Chris Lattner | f66d20d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 554 | if (Opc < 0) { |
| 555 | V = UndefValue::get(CurTy); // Unknown binop. |
| 556 | } else { |
| 557 | Constant *LHS = ValueList.getConstantFwdRef(Record[1], CurTy); |
| 558 | Constant *RHS = ValueList.getConstantFwdRef(Record[2], CurTy); |
| 559 | V = ConstantExpr::get(Opc, LHS, RHS); |
| 560 | } |
Chris Lattner | f581c3b | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 561 | break; |
| 562 | } |
| 563 | case bitc::CST_CODE_CE_CAST: { // CE_CAST: [opcode, opty, opval] |
| 564 | if (Record.size() < 3) return Error("Invalid CE_CAST record"); |
| 565 | int Opc = GetDecodedCastOpcode(Record[0]); |
Chris Lattner | f66d20d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 566 | if (Opc < 0) { |
| 567 | V = UndefValue::get(CurTy); // Unknown cast. |
| 568 | } else { |
| 569 | const Type *OpTy = getTypeByID(Record[1]); |
| 570 | Constant *Op = ValueList.getConstantFwdRef(Record[2], OpTy); |
| 571 | V = ConstantExpr::getCast(Opc, Op, CurTy); |
| 572 | } |
Chris Lattner | f581c3b | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 573 | break; |
| 574 | } |
| 575 | case bitc::CST_CODE_CE_GEP: { // CE_GEP: [n x operands] |
| 576 | if ((Record.size() & 1) == 0) return Error("Invalid CE_GEP record"); |
| 577 | SmallVector<Constant*, 16> Elts; |
| 578 | for (unsigned i = 1, e = Record.size(); i != e; i += 2) { |
| 579 | const Type *ElTy = getTypeByID(Record[i]); |
| 580 | if (!ElTy) return Error("Invalid CE_GEP record"); |
| 581 | Elts.push_back(ValueList.getConstantFwdRef(Record[i+1], ElTy)); |
| 582 | } |
Chris Lattner | f66d20d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 583 | V = ConstantExpr::getGetElementPtr(Elts[0], &Elts[1], Elts.size()-1); |
| 584 | break; |
Chris Lattner | f581c3b | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 585 | } |
| 586 | case bitc::CST_CODE_CE_SELECT: // CE_SELECT: [opval#, opval#, opval#] |
| 587 | if (Record.size() < 3) return Error("Invalid CE_SELECT record"); |
| 588 | V = ConstantExpr::getSelect(ValueList.getConstantFwdRef(Record[0], |
| 589 | Type::Int1Ty), |
| 590 | ValueList.getConstantFwdRef(Record[1],CurTy), |
| 591 | ValueList.getConstantFwdRef(Record[2],CurTy)); |
| 592 | break; |
| 593 | case bitc::CST_CODE_CE_EXTRACTELT: { // CE_EXTRACTELT: [opty, opval, opval] |
| 594 | if (Record.size() < 3) return Error("Invalid CE_EXTRACTELT record"); |
| 595 | const VectorType *OpTy = |
| 596 | dyn_cast_or_null<VectorType>(getTypeByID(Record[0])); |
| 597 | if (OpTy == 0) return Error("Invalid CE_EXTRACTELT record"); |
| 598 | Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy); |
| 599 | Constant *Op1 = ValueList.getConstantFwdRef(Record[2], |
| 600 | OpTy->getElementType()); |
| 601 | V = ConstantExpr::getExtractElement(Op0, Op1); |
| 602 | break; |
| 603 | } |
| 604 | case bitc::CST_CODE_CE_INSERTELT: { // CE_INSERTELT: [opval, opval, opval] |
| 605 | const VectorType *OpTy = dyn_cast<VectorType>(CurTy); |
| 606 | if (Record.size() < 3 || OpTy == 0) |
| 607 | return Error("Invalid CE_INSERTELT record"); |
| 608 | Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy); |
| 609 | Constant *Op1 = ValueList.getConstantFwdRef(Record[1], |
| 610 | OpTy->getElementType()); |
| 611 | Constant *Op2 = ValueList.getConstantFwdRef(Record[2], Type::Int32Ty); |
| 612 | V = ConstantExpr::getInsertElement(Op0, Op1, Op2); |
| 613 | break; |
| 614 | } |
| 615 | case bitc::CST_CODE_CE_SHUFFLEVEC: { // CE_SHUFFLEVEC: [opval, opval, opval] |
| 616 | const VectorType *OpTy = dyn_cast<VectorType>(CurTy); |
| 617 | if (Record.size() < 3 || OpTy == 0) |
| 618 | return Error("Invalid CE_INSERTELT record"); |
| 619 | Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy); |
| 620 | Constant *Op1 = ValueList.getConstantFwdRef(Record[1], OpTy); |
| 621 | const Type *ShufTy=VectorType::get(Type::Int32Ty, OpTy->getNumElements()); |
| 622 | Constant *Op2 = ValueList.getConstantFwdRef(Record[2], ShufTy); |
| 623 | V = ConstantExpr::getShuffleVector(Op0, Op1, Op2); |
| 624 | break; |
| 625 | } |
| 626 | case bitc::CST_CODE_CE_CMP: { // CE_CMP: [opty, opval, opval, pred] |
| 627 | if (Record.size() < 4) return Error("Invalid CE_CMP record"); |
| 628 | const Type *OpTy = getTypeByID(Record[0]); |
| 629 | if (OpTy == 0) return Error("Invalid CE_CMP record"); |
| 630 | Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy); |
| 631 | Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy); |
| 632 | |
| 633 | if (OpTy->isFloatingPoint()) |
| 634 | V = ConstantExpr::getFCmp(Record[3], Op0, Op1); |
| 635 | else |
| 636 | V = ConstantExpr::getICmp(Record[3], Op0, Op1); |
| 637 | break; |
Chris Lattner | 522b7b1 | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 638 | } |
Chris Lattner | e16504e | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 639 | } |
| 640 | |
Chris Lattner | 522b7b1 | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 641 | if (NextCstNo == ValueList.size()) |
| 642 | ValueList.push_back(V); |
| 643 | else if (ValueList[NextCstNo] == 0) |
| 644 | ValueList.initVal(NextCstNo, V); |
| 645 | else { |
| 646 | // If there was a forward reference to this constant, |
| 647 | Value *OldV = ValueList[NextCstNo]; |
| 648 | ValueList.setOperand(NextCstNo, V); |
| 649 | OldV->replaceAllUsesWith(V); |
| 650 | delete OldV; |
| 651 | } |
| 652 | |
| 653 | ++NextCstNo; |
Chris Lattner | e16504e | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 654 | } |
| 655 | } |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 656 | |
| 657 | bool BitcodeReader::ParseModule(BitstreamReader &Stream, |
| 658 | const std::string &ModuleID) { |
| 659 | // Reject multiple MODULE_BLOCK's in a single bitstream. |
| 660 | if (TheModule) |
| 661 | return Error("Multiple MODULE_BLOCKs in same stream"); |
| 662 | |
| 663 | if (Stream.EnterSubBlock()) |
| 664 | return Error("Malformed block record"); |
| 665 | |
| 666 | // Otherwise, create the module. |
| 667 | TheModule = new Module(ModuleID); |
| 668 | |
| 669 | SmallVector<uint64_t, 64> Record; |
| 670 | std::vector<std::string> SectionTable; |
| 671 | |
| 672 | // Read all the records for this module. |
| 673 | while (!Stream.AtEndOfStream()) { |
| 674 | unsigned Code = Stream.ReadCode(); |
Chris Lattner | e84bcb9 | 2007-04-24 00:21:45 +0000 | [diff] [blame] | 675 | if (Code == bitc::END_BLOCK) { |
Chris Lattner | 07d98b4 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 676 | ResolveGlobalAndAliasInits(); |
| 677 | if (!GlobalInits.empty() || !AliasInits.empty()) |
Chris Lattner | e84bcb9 | 2007-04-24 00:21:45 +0000 | [diff] [blame] | 678 | return Error("Malformed global initializer set"); |
Chris Lattner | f66d20d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 679 | if (Stream.ReadBlockEnd()) |
| 680 | return Error("Error at end of module block"); |
| 681 | return false; |
Chris Lattner | e84bcb9 | 2007-04-24 00:21:45 +0000 | [diff] [blame] | 682 | } |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 683 | |
| 684 | if (Code == bitc::ENTER_SUBBLOCK) { |
| 685 | switch (Stream.ReadSubBlockID()) { |
| 686 | default: // Skip unknown content. |
| 687 | if (Stream.SkipBlock()) |
| 688 | return Error("Malformed block record"); |
| 689 | break; |
| 690 | case bitc::TYPE_BLOCK_ID: |
| 691 | if (ParseTypeTable(Stream)) |
| 692 | return true; |
| 693 | break; |
| 694 | case bitc::TYPE_SYMTAB_BLOCK_ID: |
| 695 | if (ParseTypeSymbolTable(Stream)) |
| 696 | return true; |
| 697 | break; |
Chris Lattner | 0b2482a | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 698 | case bitc::VALUE_SYMTAB_BLOCK_ID: |
| 699 | if (ParseValueSymbolTable(Stream)) |
| 700 | return true; |
| 701 | break; |
Chris Lattner | e16504e | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 702 | case bitc::CONSTANTS_BLOCK_ID: |
Chris Lattner | 07d98b4 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 703 | if (ParseConstants(Stream) || ResolveGlobalAndAliasInits()) |
Chris Lattner | e16504e | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 704 | return true; |
| 705 | break; |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 706 | } |
| 707 | continue; |
| 708 | } |
| 709 | |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 710 | if (Code == bitc::DEFINE_ABBREV) { |
Chris Lattner | d127c1b | 2007-04-23 18:58:34 +0000 | [diff] [blame] | 711 | Stream.ReadAbbrevRecord(); |
| 712 | continue; |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | // Read a record. |
| 716 | switch (Stream.ReadRecord(Code, Record)) { |
| 717 | default: break; // Default behavior, ignore unknown content. |
| 718 | case bitc::MODULE_CODE_VERSION: // VERSION: [version#] |
| 719 | if (Record.size() < 1) |
| 720 | return Error("Malformed MODULE_CODE_VERSION"); |
| 721 | // Only version #0 is supported so far. |
| 722 | if (Record[0] != 0) |
| 723 | return Error("Unknown bitstream version!"); |
| 724 | break; |
| 725 | case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strlen, strchr x N] |
| 726 | std::string S; |
| 727 | if (ConvertToString(Record, 0, S)) |
| 728 | return Error("Invalid MODULE_CODE_TRIPLE record"); |
| 729 | TheModule->setTargetTriple(S); |
| 730 | break; |
| 731 | } |
| 732 | case bitc::MODULE_CODE_DATALAYOUT: { // DATALAYOUT: [strlen, strchr x N] |
| 733 | std::string S; |
| 734 | if (ConvertToString(Record, 0, S)) |
| 735 | return Error("Invalid MODULE_CODE_DATALAYOUT record"); |
| 736 | TheModule->setDataLayout(S); |
| 737 | break; |
| 738 | } |
| 739 | case bitc::MODULE_CODE_ASM: { // ASM: [strlen, strchr x N] |
| 740 | std::string S; |
| 741 | if (ConvertToString(Record, 0, S)) |
| 742 | return Error("Invalid MODULE_CODE_ASM record"); |
| 743 | TheModule->setModuleInlineAsm(S); |
| 744 | break; |
| 745 | } |
| 746 | case bitc::MODULE_CODE_DEPLIB: { // DEPLIB: [strlen, strchr x N] |
| 747 | std::string S; |
| 748 | if (ConvertToString(Record, 0, S)) |
| 749 | return Error("Invalid MODULE_CODE_DEPLIB record"); |
| 750 | TheModule->addLibrary(S); |
| 751 | break; |
| 752 | } |
| 753 | case bitc::MODULE_CODE_SECTIONNAME: { // SECTIONNAME: [strlen, strchr x N] |
| 754 | std::string S; |
| 755 | if (ConvertToString(Record, 0, S)) |
| 756 | return Error("Invalid MODULE_CODE_SECTIONNAME record"); |
| 757 | SectionTable.push_back(S); |
| 758 | break; |
| 759 | } |
| 760 | // GLOBALVAR: [type, isconst, initid, |
| 761 | // linkage, alignment, section, visibility, threadlocal] |
| 762 | case bitc::MODULE_CODE_GLOBALVAR: { |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 763 | if (Record.size() < 6) |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 764 | return Error("Invalid MODULE_CODE_GLOBALVAR record"); |
| 765 | const Type *Ty = getTypeByID(Record[0]); |
| 766 | if (!isa<PointerType>(Ty)) |
| 767 | return Error("Global not a pointer type!"); |
| 768 | Ty = cast<PointerType>(Ty)->getElementType(); |
| 769 | |
| 770 | bool isConstant = Record[1]; |
| 771 | GlobalValue::LinkageTypes Linkage = GetDecodedLinkage(Record[3]); |
| 772 | unsigned Alignment = (1 << Record[4]) >> 1; |
| 773 | std::string Section; |
| 774 | if (Record[5]) { |
| 775 | if (Record[5]-1 >= SectionTable.size()) |
| 776 | return Error("Invalid section ID"); |
| 777 | Section = SectionTable[Record[5]-1]; |
| 778 | } |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 779 | GlobalValue::VisibilityTypes Visibility = GlobalValue::DefaultVisibility; |
| 780 | if (Record.size() >= 6) Visibility = GetDecodedVisibility(Record[6]); |
| 781 | bool isThreadLocal = false; |
| 782 | if (Record.size() >= 7) isThreadLocal = Record[7]; |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 783 | |
| 784 | GlobalVariable *NewGV = |
| 785 | new GlobalVariable(Ty, isConstant, Linkage, 0, "", TheModule); |
| 786 | NewGV->setAlignment(Alignment); |
| 787 | if (!Section.empty()) |
| 788 | NewGV->setSection(Section); |
| 789 | NewGV->setVisibility(Visibility); |
| 790 | NewGV->setThreadLocal(isThreadLocal); |
| 791 | |
Chris Lattner | 0b2482a | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 792 | ValueList.push_back(NewGV); |
| 793 | |
Chris Lattner | 6dbfd7b | 2007-04-24 00:18:21 +0000 | [diff] [blame] | 794 | // Remember which value to use for the global initializer. |
| 795 | if (unsigned InitID = Record[2]) |
| 796 | GlobalInits.push_back(std::make_pair(NewGV, InitID-1)); |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 797 | break; |
| 798 | } |
| 799 | // FUNCTION: [type, callingconv, isproto, linkage, alignment, section, |
| 800 | // visibility] |
| 801 | case bitc::MODULE_CODE_FUNCTION: { |
| 802 | if (Record.size() < 7) |
| 803 | return Error("Invalid MODULE_CODE_FUNCTION record"); |
| 804 | const Type *Ty = getTypeByID(Record[0]); |
| 805 | if (!isa<PointerType>(Ty)) |
| 806 | return Error("Function not a pointer type!"); |
| 807 | const FunctionType *FTy = |
| 808 | dyn_cast<FunctionType>(cast<PointerType>(Ty)->getElementType()); |
| 809 | if (!FTy) |
| 810 | return Error("Function not a pointer to function type!"); |
| 811 | |
| 812 | Function *Func = new Function(FTy, GlobalValue::ExternalLinkage, |
| 813 | "", TheModule); |
| 814 | |
| 815 | Func->setCallingConv(Record[1]); |
| 816 | Func->setLinkage(GetDecodedLinkage(Record[3])); |
| 817 | Func->setAlignment((1 << Record[4]) >> 1); |
| 818 | if (Record[5]) { |
| 819 | if (Record[5]-1 >= SectionTable.size()) |
| 820 | return Error("Invalid section ID"); |
| 821 | Func->setSection(SectionTable[Record[5]-1]); |
| 822 | } |
| 823 | Func->setVisibility(GetDecodedVisibility(Record[6])); |
| 824 | |
Chris Lattner | 0b2482a | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 825 | ValueList.push_back(Func); |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 826 | break; |
| 827 | } |
Chris Lattner | 07d98b4 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 828 | // ALIAS: [alias type, aliasee val#, linkage] |
Chris Lattner | 198f34a | 2007-04-26 03:27:58 +0000 | [diff] [blame^] | 829 | case bitc::MODULE_CODE_ALIAS: { |
Chris Lattner | 07d98b4 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 830 | if (Record.size() < 3) |
| 831 | return Error("Invalid MODULE_ALIAS record"); |
| 832 | const Type *Ty = getTypeByID(Record[0]); |
| 833 | if (!isa<PointerType>(Ty)) |
| 834 | return Error("Function not a pointer type!"); |
| 835 | |
| 836 | GlobalAlias *NewGA = new GlobalAlias(Ty, GetDecodedLinkage(Record[2]), |
| 837 | "", 0, TheModule); |
| 838 | ValueList.push_back(NewGA); |
| 839 | AliasInits.push_back(std::make_pair(NewGA, Record[1])); |
| 840 | break; |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 841 | } |
Chris Lattner | 198f34a | 2007-04-26 03:27:58 +0000 | [diff] [blame^] | 842 | /// MODULE_CODE_PURGEVALS: [numvals] |
| 843 | case bitc::MODULE_CODE_PURGEVALS: |
| 844 | // Trim down the value list to the specified size. |
| 845 | if (Record.size() < 1 || Record[0] > ValueList.size()) |
| 846 | return Error("Invalid MODULE_PURGEVALS record"); |
| 847 | ValueList.shrinkTo(Record[0]); |
| 848 | break; |
| 849 | } |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 850 | Record.clear(); |
| 851 | } |
| 852 | |
| 853 | return Error("Premature end of bitstream"); |
| 854 | } |
| 855 | |
| 856 | |
| 857 | bool BitcodeReader::ParseBitcode(unsigned char *Buf, unsigned Length, |
| 858 | const std::string &ModuleID) { |
| 859 | TheModule = 0; |
| 860 | |
| 861 | if (Length & 3) |
| 862 | return Error("Bitcode stream should be a multiple of 4 bytes in length"); |
| 863 | |
| 864 | BitstreamReader Stream(Buf, Buf+Length); |
| 865 | |
| 866 | // Sniff for the signature. |
| 867 | if (Stream.Read(8) != 'B' || |
| 868 | Stream.Read(8) != 'C' || |
| 869 | Stream.Read(4) != 0x0 || |
| 870 | Stream.Read(4) != 0xC || |
| 871 | Stream.Read(4) != 0xE || |
| 872 | Stream.Read(4) != 0xD) |
| 873 | return Error("Invalid bitcode signature"); |
| 874 | |
| 875 | // We expect a number of well-defined blocks, though we don't necessarily |
| 876 | // need to understand them all. |
| 877 | while (!Stream.AtEndOfStream()) { |
| 878 | unsigned Code = Stream.ReadCode(); |
| 879 | |
| 880 | if (Code != bitc::ENTER_SUBBLOCK) |
| 881 | return Error("Invalid record at top-level"); |
| 882 | |
| 883 | unsigned BlockID = Stream.ReadSubBlockID(); |
| 884 | |
| 885 | // We only know the MODULE subblock ID. |
| 886 | if (BlockID == bitc::MODULE_BLOCK_ID) { |
| 887 | if (ParseModule(Stream, ModuleID)) |
| 888 | return true; |
| 889 | } else if (Stream.SkipBlock()) { |
| 890 | return Error("Malformed block record"); |
| 891 | } |
| 892 | } |
| 893 | |
| 894 | return false; |
| 895 | } |