Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1 | //===- ReadConst.cpp - Code to constants and constant pools -----------------=== |
| 2 | // |
| 3 | // This file implements functionality to deserialize constants and entire |
| 4 | // constant pools. |
| 5 | // |
| 6 | // Note that this library should be as fast as possible, reentrant, and |
| 7 | // threadsafe!! |
| 8 | // |
| 9 | //===------------------------------------------------------------------------=== |
| 10 | |
| 11 | #include "llvm/Module.h" |
| 12 | #include "llvm/BasicBlock.h" |
| 13 | #include "llvm/ConstPoolVals.h" |
| 14 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 05950c3 | 2001-10-13 06:47:01 +0000 | [diff] [blame] | 15 | #include "llvm/GlobalVariable.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 16 | #include "ReaderInternals.h" |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 17 | #include <algorithm> |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 18 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 19 | |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 20 | |
| 21 | const Type *BytecodeParser::parseTypeConstant(const uchar *&Buf, |
| 22 | const uchar *EndBuf) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 23 | unsigned PrimType; |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 24 | if (read_vbr(Buf, EndBuf, PrimType)) return failure<const Type*>(0); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 25 | |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 26 | const Type *Val = 0; |
| 27 | if ((Val = Type::getPrimitiveType((Type::PrimitiveID)PrimType))) |
| 28 | return Val; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 29 | |
| 30 | switch (PrimType) { |
| 31 | case Type::MethodTyID: { |
| 32 | unsigned Typ; |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 33 | if (read_vbr(Buf, EndBuf, Typ)) return failure(Val); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 34 | const Type *RetType = getType(Typ); |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 35 | if (RetType == 0) return failure(Val); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 36 | |
Chris Lattner | e5a57ee | 2001-07-25 22:47:55 +0000 | [diff] [blame] | 37 | unsigned NumParams; |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 38 | if (read_vbr(Buf, EndBuf, NumParams)) return failure(Val); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 39 | |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 40 | vector<const Type*> Params; |
Chris Lattner | e5a57ee | 2001-07-25 22:47:55 +0000 | [diff] [blame] | 41 | while (NumParams--) { |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 42 | if (read_vbr(Buf, EndBuf, Typ)) return failure(Val); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 43 | const Type *Ty = getType(Typ); |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 44 | if (Ty == 0) return failure(Val); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 45 | Params.push_back(Ty); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Chris Lattner | 05950c3 | 2001-10-13 06:47:01 +0000 | [diff] [blame] | 48 | bool isVarArg = Params.size() && Params.back() == Type::VoidTy; |
| 49 | if (isVarArg) Params.pop_back(); |
| 50 | |
Chris Lattner | 7eadfa1 | 2001-10-24 06:21:22 +0000 | [diff] [blame] | 51 | return MethodType::get(RetType, Params, isVarArg); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 52 | } |
| 53 | case Type::ArrayTyID: { |
| 54 | unsigned ElTyp; |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 55 | if (read_vbr(Buf, EndBuf, ElTyp)) return failure(Val); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 56 | const Type *ElementType = getType(ElTyp); |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 57 | if (ElementType == 0) return failure(Val); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 58 | |
| 59 | int NumElements; |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 60 | if (read_vbr(Buf, EndBuf, NumElements)) return failure(Val); |
Chris Lattner | 7eadfa1 | 2001-10-24 06:21:22 +0000 | [diff] [blame] | 61 | |
| 62 | BCR_TRACE(5, "Array Type Constant #" << ElTyp << " size=" |
| 63 | << NumElements << endl); |
| 64 | return ArrayType::get(ElementType, NumElements); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 65 | } |
| 66 | case Type::StructTyID: { |
| 67 | unsigned Typ; |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 68 | vector<const Type*> Elements; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 69 | |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 70 | if (read_vbr(Buf, EndBuf, Typ)) return failure(Val); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 71 | while (Typ) { // List is terminated by void/0 typeid |
| 72 | const Type *Ty = getType(Typ); |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 73 | if (Ty == 0) return failure(Val); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 74 | Elements.push_back(Ty); |
| 75 | |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 76 | if (read_vbr(Buf, EndBuf, Typ)) return failure(Val); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Chris Lattner | 7eadfa1 | 2001-10-24 06:21:22 +0000 | [diff] [blame] | 79 | return StructType::get(Elements); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 80 | } |
| 81 | case Type::PointerTyID: { |
| 82 | unsigned ElTyp; |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 83 | if (read_vbr(Buf, EndBuf, ElTyp)) return failure(Val); |
Chris Lattner | 7eadfa1 | 2001-10-24 06:21:22 +0000 | [diff] [blame] | 84 | BCR_TRACE(5, "Pointer Type Constant #" << (ElTyp-14) << endl); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 85 | const Type *ElementType = getType(ElTyp); |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 86 | if (ElementType == 0) return failure(Val); |
Chris Lattner | 7eadfa1 | 2001-10-24 06:21:22 +0000 | [diff] [blame] | 87 | return PointerType::get(ElementType); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Chris Lattner | d48d6c7 | 2001-10-23 01:53:01 +0000 | [diff] [blame] | 90 | case Type::OpaqueTyID: { |
Chris Lattner | 7eadfa1 | 2001-10-24 06:21:22 +0000 | [diff] [blame] | 91 | return OpaqueType::get(); |
Chris Lattner | d48d6c7 | 2001-10-23 01:53:01 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 94 | default: |
| 95 | cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to deserialize" |
Chris Lattner | 7eadfa1 | 2001-10-24 06:21:22 +0000 | [diff] [blame] | 96 | << " primitive Type " << PrimType << "\n"; |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 97 | return failure(Val); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 98 | } |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | // refineAbstractType - The callback method is invoked when one of the |
| 102 | // elements of TypeValues becomes more concrete... |
| 103 | // |
| 104 | void BytecodeParser::refineAbstractType(const DerivedType *OldType, |
| 105 | const Type *NewType) { |
Chris Lattner | e244a25 | 2001-11-03 03:27:53 +0000 | [diff] [blame] | 106 | if (OldType == NewType && |
| 107 | OldType->isAbstract()) return; // Type is modified, but same |
Chris Lattner | 05950c3 | 2001-10-13 06:47:01 +0000 | [diff] [blame] | 108 | |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 109 | TypeValuesListTy::iterator I = find(MethodTypeValues.begin(), |
| 110 | MethodTypeValues.end(), OldType); |
| 111 | if (I == MethodTypeValues.end()) { |
| 112 | I = find(ModuleTypeValues.begin(), ModuleTypeValues.end(), OldType); |
| 113 | assert(I != ModuleTypeValues.end() && |
| 114 | "Can't refine a type I don't know about!"); |
| 115 | } |
| 116 | |
Chris Lattner | e244a25 | 2001-11-03 03:27:53 +0000 | [diff] [blame] | 117 | if (OldType == NewType) { |
| 118 | assert(!OldType->isAbstract()); |
| 119 | I->removeUserFromConcrete(); |
| 120 | } else { |
| 121 | *I = NewType; // Update to point to new, more refined type. |
| 122 | } |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | |
| 126 | |
| 127 | // parseTypeConstants - We have to use this wierd code to handle recursive |
| 128 | // types. We know that recursive types will only reference the current slab of |
| 129 | // values in the type plane, but they can forward reference types before they |
| 130 | // have been read. For example, Type #0 might be '{ Ty#1 }' and Type #1 might |
| 131 | // be 'Ty#0*'. When reading Type #0, type number one doesn't exist. To fix |
| 132 | // this ugly problem, we pesimistically insert an opaque type for each type we |
| 133 | // are about to read. This means that forward references will resolve to |
| 134 | // something and when we reread the type later, we can replace the opaque type |
| 135 | // with a new resolved concrete type. |
| 136 | // |
| 137 | bool BytecodeParser::parseTypeConstants(const uchar *&Buf, const uchar *EndBuf, |
| 138 | TypeValuesListTy &Tab, |
| 139 | unsigned NumEntries) { |
Chris Lattner | 05950c3 | 2001-10-13 06:47:01 +0000 | [diff] [blame] | 140 | assert(Tab.size() == 0 && "should not have read type constants in before!"); |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 141 | |
| 142 | // Insert a bunch of opaque types to be resolved later... |
Chris Lattner | 7eadfa1 | 2001-10-24 06:21:22 +0000 | [diff] [blame] | 143 | for (unsigned i = 0; i < NumEntries; ++i) |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 144 | Tab.push_back(PATypeHandle<Type>(OpaqueType::get(), this)); |
| 145 | |
| 146 | // Loop through reading all of the types. Forward types will make use of the |
| 147 | // opaque types just inserted. |
| 148 | // |
Chris Lattner | 7eadfa1 | 2001-10-24 06:21:22 +0000 | [diff] [blame] | 149 | for (unsigned i = 0; i < NumEntries; ++i) { |
Chris Lattner | 05950c3 | 2001-10-13 06:47:01 +0000 | [diff] [blame] | 150 | const Type *NewTy = parseTypeConstant(Buf, EndBuf), *OldTy = Tab[i].get(); |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 151 | if (NewTy == 0) return failure(true); |
Chris Lattner | 7eadfa1 | 2001-10-24 06:21:22 +0000 | [diff] [blame] | 152 | BCR_TRACE(4, "#" << i << ": Read Type Constant: '" << NewTy << |
| 153 | "' Replacing: " << OldTy << "\n"); |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 154 | |
| 155 | // Don't insertValue the new type... instead we want to replace the opaque |
| 156 | // type with the new concrete value... |
| 157 | // |
| 158 | |
| 159 | // Refine the abstract type to the new type. This causes all uses of the |
| 160 | // abstract type to use the newty. This also will cause the opaque type |
| 161 | // to be deleted... |
| 162 | // |
Chris Lattner | 05950c3 | 2001-10-13 06:47:01 +0000 | [diff] [blame] | 163 | cast<DerivedType>(Tab[i].get())->refineAbstractTypeTo(NewTy); |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 164 | |
| 165 | // This should have replace the old opaque type with the new type in the |
Chris Lattner | 05950c3 | 2001-10-13 06:47:01 +0000 | [diff] [blame] | 166 | // value table... or with a preexisting type that was already in the system |
| 167 | assert(Tab[i] != OldTy && "refineAbstractType didn't work!"); |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | BCR_TRACE(5, "Resulting types:\n"); |
Chris Lattner | 7eadfa1 | 2001-10-24 06:21:22 +0000 | [diff] [blame] | 171 | for (unsigned i = 0; i < NumEntries; ++i) { |
Chris Lattner | 05950c3 | 2001-10-13 06:47:01 +0000 | [diff] [blame] | 172 | BCR_TRACE(5, cast<const Type>(Tab[i]) << "\n"); |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 173 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 174 | return false; |
| 175 | } |
| 176 | |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 177 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 178 | bool BytecodeParser::parseConstPoolValue(const uchar *&Buf, |
| 179 | const uchar *EndBuf, |
| 180 | const Type *Ty, ConstPoolVal *&V) { |
| 181 | switch (Ty->getPrimitiveID()) { |
| 182 | case Type::BoolTyID: { |
| 183 | unsigned Val; |
Chris Lattner | 3d3f289 | 2001-07-28 17:50:18 +0000 | [diff] [blame] | 184 | if (read_vbr(Buf, EndBuf, Val)) return failure(true); |
| 185 | if (Val != 0 && Val != 1) return failure(true); |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 186 | V = ConstPoolBool::get(Val == 1); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 187 | break; |
| 188 | } |
| 189 | |
| 190 | case Type::UByteTyID: // Unsigned integer types... |
| 191 | case Type::UShortTyID: |
| 192 | case Type::UIntTyID: { |
| 193 | unsigned Val; |
Chris Lattner | 3d3f289 | 2001-07-28 17:50:18 +0000 | [diff] [blame] | 194 | if (read_vbr(Buf, EndBuf, Val)) return failure(true); |
| 195 | if (!ConstPoolUInt::isValueValidForType(Ty, Val)) return failure(true); |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 196 | V = ConstPoolUInt::get(Ty, Val); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 197 | break; |
| 198 | } |
| 199 | |
| 200 | case Type::ULongTyID: { |
| 201 | uint64_t Val; |
Chris Lattner | 3d3f289 | 2001-07-28 17:50:18 +0000 | [diff] [blame] | 202 | if (read_vbr(Buf, EndBuf, Val)) return failure(true); |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 203 | V = ConstPoolUInt::get(Ty, Val); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 204 | break; |
| 205 | } |
| 206 | |
| 207 | case Type::SByteTyID: // Unsigned integer types... |
| 208 | case Type::ShortTyID: |
| 209 | case Type::IntTyID: { |
| 210 | int Val; |
Chris Lattner | 3d3f289 | 2001-07-28 17:50:18 +0000 | [diff] [blame] | 211 | if (read_vbr(Buf, EndBuf, Val)) return failure(true); |
| 212 | if (!ConstPoolSInt::isValueValidForType(Ty, Val)) return failure(true); |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 213 | V = ConstPoolSInt::get(Ty, Val); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 214 | break; |
| 215 | } |
| 216 | |
| 217 | case Type::LongTyID: { |
| 218 | int64_t Val; |
Chris Lattner | 3d3f289 | 2001-07-28 17:50:18 +0000 | [diff] [blame] | 219 | if (read_vbr(Buf, EndBuf, Val)) return failure(true); |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 220 | V = ConstPoolSInt::get(Ty, Val); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 221 | break; |
| 222 | } |
| 223 | |
Chris Lattner | a137530 | 2001-07-15 00:17:18 +0000 | [diff] [blame] | 224 | case Type::FloatTyID: { |
| 225 | float F; |
Chris Lattner | 3d3f289 | 2001-07-28 17:50:18 +0000 | [diff] [blame] | 226 | if (input_data(Buf, EndBuf, &F, &F+1)) return failure(true); |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 227 | V = ConstPoolFP::get(Ty, F); |
Chris Lattner | a137530 | 2001-07-15 00:17:18 +0000 | [diff] [blame] | 228 | break; |
| 229 | } |
| 230 | |
| 231 | case Type::DoubleTyID: { |
| 232 | double Val; |
Chris Lattner | 3d3f289 | 2001-07-28 17:50:18 +0000 | [diff] [blame] | 233 | if (input_data(Buf, EndBuf, &Val, &Val+1)) return failure(true); |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 234 | V = ConstPoolFP::get(Ty, Val); |
Chris Lattner | a137530 | 2001-07-15 00:17:18 +0000 | [diff] [blame] | 235 | break; |
| 236 | } |
| 237 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 238 | case Type::TypeTyID: |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 239 | assert(0 && "Type constants should be handled seperately!!!"); |
| 240 | abort(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 241 | |
| 242 | case Type::ArrayTyID: { |
Chris Lattner | b00c582 | 2001-10-02 03:41:24 +0000 | [diff] [blame] | 243 | const ArrayType *AT = cast<const ArrayType>(Ty); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 244 | unsigned NumElements; |
| 245 | if (AT->isSized()) // Sized array, # elements stored in type! |
| 246 | NumElements = (unsigned)AT->getNumElements(); |
| 247 | else // Unsized array, # elements stored in stream! |
Chris Lattner | 3d3f289 | 2001-07-28 17:50:18 +0000 | [diff] [blame] | 248 | if (read_vbr(Buf, EndBuf, NumElements)) return failure(true); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 249 | |
| 250 | vector<ConstPoolVal *> Elements; |
| 251 | while (NumElements--) { // Read all of the elements of the constant. |
| 252 | unsigned Slot; |
Chris Lattner | 3d3f289 | 2001-07-28 17:50:18 +0000 | [diff] [blame] | 253 | if (read_vbr(Buf, EndBuf, Slot)) return failure(true); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 254 | Value *V = getValue(AT->getElementType(), Slot, false); |
Chris Lattner | 1d87bcf | 2001-10-01 20:11:19 +0000 | [diff] [blame] | 255 | if (!V || !isa<ConstPoolVal>(V)) return failure(true); |
| 256 | Elements.push_back(cast<ConstPoolVal>(V)); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 257 | } |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 258 | V = ConstPoolArray::get(AT, Elements); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 259 | break; |
| 260 | } |
| 261 | |
| 262 | case Type::StructTyID: { |
Chris Lattner | b00c582 | 2001-10-02 03:41:24 +0000 | [diff] [blame] | 263 | const StructType *ST = cast<StructType>(Ty); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 264 | const StructType::ElementTypes &ET = ST->getElementTypes(); |
| 265 | |
| 266 | vector<ConstPoolVal *> Elements; |
| 267 | for (unsigned i = 0; i < ET.size(); ++i) { |
| 268 | unsigned Slot; |
Chris Lattner | 3d3f289 | 2001-07-28 17:50:18 +0000 | [diff] [blame] | 269 | if (read_vbr(Buf, EndBuf, Slot)) return failure(true); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 270 | Value *V = getValue(ET[i], Slot, false); |
Chris Lattner | 1d87bcf | 2001-10-01 20:11:19 +0000 | [diff] [blame] | 271 | if (!V || !isa<ConstPoolVal>(V)) |
Chris Lattner | 3d3f289 | 2001-07-28 17:50:18 +0000 | [diff] [blame] | 272 | return failure(true); |
Chris Lattner | 1d87bcf | 2001-10-01 20:11:19 +0000 | [diff] [blame] | 273 | Elements.push_back(cast<ConstPoolVal>(V)); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 274 | } |
| 275 | |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 276 | V = ConstPoolStruct::get(ST, Elements); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 277 | break; |
| 278 | } |
| 279 | |
Chris Lattner | 1a1cb11 | 2001-09-30 22:46:54 +0000 | [diff] [blame] | 280 | case Type::PointerTyID: { |
Chris Lattner | b00c582 | 2001-10-02 03:41:24 +0000 | [diff] [blame] | 281 | const PointerType *PT = cast<const PointerType>(Ty); |
Chris Lattner | 1a1cb11 | 2001-09-30 22:46:54 +0000 | [diff] [blame] | 282 | unsigned SubClass; |
| 283 | if (read_vbr(Buf, EndBuf, SubClass)) return failure(true); |
Chris Lattner | 05950c3 | 2001-10-13 06:47:01 +0000 | [diff] [blame] | 284 | switch (SubClass) { |
| 285 | case 0: // ConstPoolPointerNull value... |
| 286 | V = ConstPoolPointerNull::get(PT); |
| 287 | break; |
Chris Lattner | 1a1cb11 | 2001-09-30 22:46:54 +0000 | [diff] [blame] | 288 | |
Chris Lattner | c18545d | 2001-10-15 13:21:42 +0000 | [diff] [blame] | 289 | case 1: { // ConstPoolPointerRef value... |
Chris Lattner | 05950c3 | 2001-10-13 06:47:01 +0000 | [diff] [blame] | 290 | unsigned Slot; |
| 291 | if (read_vbr(Buf, EndBuf, Slot)) return failure(true); |
| 292 | BCR_TRACE(4, "CPPR: Type: '" << Ty << "' slot: " << Slot << "\n"); |
Chris Lattner | 1a1cb11 | 2001-09-30 22:46:54 +0000 | [diff] [blame] | 293 | |
Chris Lattner | 05950c3 | 2001-10-13 06:47:01 +0000 | [diff] [blame] | 294 | // Check to see if we have already read this global variable yet... |
| 295 | Value *Val = getValue(PT, Slot, false); |
| 296 | GlobalValue *GV; |
| 297 | if (Val) { |
| 298 | if (!(GV = dyn_cast<GlobalValue>(Val))) return failure(true); |
| 299 | BCR_TRACE(5, "Value Found in ValueTable!\n"); |
| 300 | } else { // Nope... see if we have previously forward ref'd it |
| 301 | GlobalRefsType::iterator I = GlobalRefs.find(make_pair(PT, Slot)); |
| 302 | if (I != GlobalRefs.end()) { |
| 303 | BCR_TRACE(5, "Previous forward ref found!\n"); |
| 304 | GV = I->second; |
| 305 | } else { |
| 306 | BCR_TRACE(5, "Creating new forward ref variable!\n"); |
| 307 | |
| 308 | // Create a placeholder for the global variable reference... |
Chris Lattner | d23b1d3 | 2001-11-26 18:56:10 +0000 | [diff] [blame] | 309 | GlobalVariable *GVar = |
| 310 | new GlobalVariable(PT->getValueType(), false, true); |
Chris Lattner | 05950c3 | 2001-10-13 06:47:01 +0000 | [diff] [blame] | 311 | |
| 312 | // Keep track of the fact that we have a forward ref to recycle it |
| 313 | GlobalRefs.insert(make_pair(make_pair(PT, Slot), GVar)); |
| 314 | |
| 315 | // Must temporarily push this value into the module table... |
| 316 | TheModule->getGlobalList().push_back(GVar); |
| 317 | GV = GVar; |
| 318 | } |
| 319 | } |
| 320 | |
Chris Lattner | c18545d | 2001-10-15 13:21:42 +0000 | [diff] [blame] | 321 | V = ConstPoolPointerRef::get(GV); |
Chris Lattner | 05950c3 | 2001-10-13 06:47:01 +0000 | [diff] [blame] | 322 | break; |
| 323 | } |
| 324 | default: |
| 325 | return failure(true); |
| 326 | } |
Chris Lattner | 1a1cb11 | 2001-09-30 22:46:54 +0000 | [diff] [blame] | 327 | break; |
| 328 | } |
| 329 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 330 | default: |
| 331 | cerr << __FILE__ << ":" << __LINE__ |
| 332 | << ": Don't know how to deserialize constant value of type '" |
| 333 | << Ty->getName() << "'\n"; |
Chris Lattner | 3d3f289 | 2001-07-28 17:50:18 +0000 | [diff] [blame] | 334 | return failure(true); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 335 | } |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 336 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 337 | return false; |
| 338 | } |
| 339 | |
| 340 | bool BytecodeParser::ParseConstantPool(const uchar *&Buf, const uchar *EndBuf, |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 341 | ValueTable &Tab, |
| 342 | TypeValuesListTy &TypeTab) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 343 | while (Buf < EndBuf) { |
| 344 | unsigned NumEntries, Typ; |
| 345 | |
| 346 | if (read_vbr(Buf, EndBuf, NumEntries) || |
Chris Lattner | 3d3f289 | 2001-07-28 17:50:18 +0000 | [diff] [blame] | 347 | read_vbr(Buf, EndBuf, Typ)) return failure(true); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 348 | const Type *Ty = getType(Typ); |
Chris Lattner | 3d3f289 | 2001-07-28 17:50:18 +0000 | [diff] [blame] | 349 | if (Ty == 0) return failure(true); |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 350 | BCR_TRACE(3, "Type: '" << Ty << "' NumEntries: " << NumEntries << "\n"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 351 | |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 352 | if (Typ == Type::TypeTyID) { |
| 353 | if (parseTypeConstants(Buf, EndBuf, TypeTab, NumEntries)) return true; |
| 354 | } else { |
Chris Lattner | 7eadfa1 | 2001-10-24 06:21:22 +0000 | [diff] [blame] | 355 | for (unsigned i = 0; i < NumEntries; ++i) { |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 356 | ConstPoolVal *I; |
| 357 | if (parseConstPoolValue(Buf, EndBuf, Ty, I)) return failure(true); |
| 358 | BCR_TRACE(4, "Read Constant: '" << I << "'\n"); |
Chris Lattner | 05950c3 | 2001-10-13 06:47:01 +0000 | [diff] [blame] | 359 | if (insertValue(I, Tab) == -1) return failure(true); |
Chris Lattner | 1d670cc | 2001-09-07 16:37:43 +0000 | [diff] [blame] | 360 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | |
Chris Lattner | 3d3f289 | 2001-07-28 17:50:18 +0000 | [diff] [blame] | 364 | if (Buf > EndBuf) return failure(true); |
| 365 | return false; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 366 | } |