blob: 6e86997f224471fa216a769f56c60636cb226988 [file] [log] [blame]
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001//===- ReadConst.cpp - Code to constants and constant pools ---------------===//
Chris Lattner00950542001-06-06 20:29:01 +00002//
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//
Chris Lattnere9bb2df2001-12-03 22:26:30 +00009//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +000010
Chris Lattner7061dc52001-12-03 18:02:31 +000011#include "ReaderInternals.h"
Chris Lattner00950542001-06-06 20:29:01 +000012#include "llvm/Module.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000013#include "llvm/Constants.h"
Chris Lattner05950c32001-10-13 06:47:01 +000014#include "llvm/GlobalVariable.h"
Chris Lattner1d670cc2001-09-07 16:37:43 +000015#include <algorithm>
Chris Lattner697954c2002-01-20 22:54:45 +000016#include <iostream>
Anand Shuklaeea60fc2002-06-25 20:44:04 +000017
Chris Lattner697954c2002-01-20 22:54:45 +000018using std::make_pair;
Chris Lattner1d670cc2001-09-07 16:37:43 +000019
20const Type *BytecodeParser::parseTypeConstant(const uchar *&Buf,
21 const uchar *EndBuf) {
Chris Lattner00950542001-06-06 20:29:01 +000022 unsigned PrimType;
Chris Lattner1d670cc2001-09-07 16:37:43 +000023 if (read_vbr(Buf, EndBuf, PrimType)) return failure<const Type*>(0);
Chris Lattner00950542001-06-06 20:29:01 +000024
Chris Lattner1d670cc2001-09-07 16:37:43 +000025 const Type *Val = 0;
26 if ((Val = Type::getPrimitiveType((Type::PrimitiveID)PrimType)))
27 return Val;
Chris Lattner00950542001-06-06 20:29:01 +000028
29 switch (PrimType) {
Chris Lattnerc9aa7df2002-03-29 03:51:11 +000030 case Type::FunctionTyID: {
Chris Lattner00950542001-06-06 20:29:01 +000031 unsigned Typ;
Chris Lattner1d670cc2001-09-07 16:37:43 +000032 if (read_vbr(Buf, EndBuf, Typ)) return failure(Val);
Chris Lattner00950542001-06-06 20:29:01 +000033 const Type *RetType = getType(Typ);
Chris Lattner1d670cc2001-09-07 16:37:43 +000034 if (RetType == 0) return failure(Val);
Chris Lattner00950542001-06-06 20:29:01 +000035
Chris Lattnere5a57ee2001-07-25 22:47:55 +000036 unsigned NumParams;
Chris Lattner1d670cc2001-09-07 16:37:43 +000037 if (read_vbr(Buf, EndBuf, NumParams)) return failure(Val);
Chris Lattner00950542001-06-06 20:29:01 +000038
Chris Lattner697954c2002-01-20 22:54:45 +000039 std::vector<const Type*> Params;
Chris Lattnere5a57ee2001-07-25 22:47:55 +000040 while (NumParams--) {
Chris Lattner1d670cc2001-09-07 16:37:43 +000041 if (read_vbr(Buf, EndBuf, Typ)) return failure(Val);
Chris Lattner00950542001-06-06 20:29:01 +000042 const Type *Ty = getType(Typ);
Chris Lattner1d670cc2001-09-07 16:37:43 +000043 if (Ty == 0) return failure(Val);
Chris Lattner00950542001-06-06 20:29:01 +000044 Params.push_back(Ty);
Chris Lattner00950542001-06-06 20:29:01 +000045 }
46
Chris Lattner05950c32001-10-13 06:47:01 +000047 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
48 if (isVarArg) Params.pop_back();
49
Chris Lattnerc9aa7df2002-03-29 03:51:11 +000050 return FunctionType::get(RetType, Params, isVarArg);
Chris Lattner00950542001-06-06 20:29:01 +000051 }
52 case Type::ArrayTyID: {
53 unsigned ElTyp;
Chris Lattner1d670cc2001-09-07 16:37:43 +000054 if (read_vbr(Buf, EndBuf, ElTyp)) return failure(Val);
Chris Lattner00950542001-06-06 20:29:01 +000055 const Type *ElementType = getType(ElTyp);
Chris Lattner1d670cc2001-09-07 16:37:43 +000056 if (ElementType == 0) return failure(Val);
Chris Lattner00950542001-06-06 20:29:01 +000057
Chris Lattner90865152001-12-14 16:30:51 +000058 unsigned NumElements;
Chris Lattner1d670cc2001-09-07 16:37:43 +000059 if (read_vbr(Buf, EndBuf, NumElements)) return failure(Val);
Chris Lattner7eadfa12001-10-24 06:21:22 +000060
61 BCR_TRACE(5, "Array Type Constant #" << ElTyp << " size="
Chris Lattner697954c2002-01-20 22:54:45 +000062 << NumElements << "\n");
Chris Lattner7eadfa12001-10-24 06:21:22 +000063 return ArrayType::get(ElementType, NumElements);
Chris Lattner00950542001-06-06 20:29:01 +000064 }
65 case Type::StructTyID: {
66 unsigned Typ;
Chris Lattner697954c2002-01-20 22:54:45 +000067 std::vector<const Type*> Elements;
Chris Lattner00950542001-06-06 20:29:01 +000068
Chris Lattner1d670cc2001-09-07 16:37:43 +000069 if (read_vbr(Buf, EndBuf, Typ)) return failure(Val);
Chris Lattner00950542001-06-06 20:29:01 +000070 while (Typ) { // List is terminated by void/0 typeid
71 const Type *Ty = getType(Typ);
Chris Lattner1d670cc2001-09-07 16:37:43 +000072 if (Ty == 0) return failure(Val);
Chris Lattner00950542001-06-06 20:29:01 +000073 Elements.push_back(Ty);
74
Chris Lattner1d670cc2001-09-07 16:37:43 +000075 if (read_vbr(Buf, EndBuf, Typ)) return failure(Val);
Chris Lattner00950542001-06-06 20:29:01 +000076 }
77
Chris Lattner7eadfa12001-10-24 06:21:22 +000078 return StructType::get(Elements);
Chris Lattner00950542001-06-06 20:29:01 +000079 }
80 case Type::PointerTyID: {
81 unsigned ElTyp;
Chris Lattner1d670cc2001-09-07 16:37:43 +000082 if (read_vbr(Buf, EndBuf, ElTyp)) return failure(Val);
Chris Lattner697954c2002-01-20 22:54:45 +000083 BCR_TRACE(5, "Pointer Type Constant #" << (ElTyp-14) << "\n");
Chris Lattner00950542001-06-06 20:29:01 +000084 const Type *ElementType = getType(ElTyp);
Chris Lattner1d670cc2001-09-07 16:37:43 +000085 if (ElementType == 0) return failure(Val);
Chris Lattner7eadfa12001-10-24 06:21:22 +000086 return PointerType::get(ElementType);
Chris Lattner00950542001-06-06 20:29:01 +000087 }
88
Chris Lattnerd48d6c72001-10-23 01:53:01 +000089 case Type::OpaqueTyID: {
Chris Lattner7eadfa12001-10-24 06:21:22 +000090 return OpaqueType::get();
Chris Lattnerd48d6c72001-10-23 01:53:01 +000091 }
92
Chris Lattner00950542001-06-06 20:29:01 +000093 default:
Anand Shuklaeea60fc2002-06-25 20:44:04 +000094 std::cerr << __FILE__ << ":" << __LINE__
Chris Lattner194f0a52002-06-30 16:04:37 +000095 << ": Don't know how to deserialize"
96 << " primitive Type " << PrimType << "\n";
Chris Lattner1d670cc2001-09-07 16:37:43 +000097 return failure(Val);
Chris Lattner00950542001-06-06 20:29:01 +000098 }
Chris Lattner1d670cc2001-09-07 16:37:43 +000099}
100
101// refineAbstractType - The callback method is invoked when one of the
102// elements of TypeValues becomes more concrete...
103//
104void BytecodeParser::refineAbstractType(const DerivedType *OldType,
105 const Type *NewType) {
Chris Lattnere244a252001-11-03 03:27:53 +0000106 if (OldType == NewType &&
107 OldType->isAbstract()) return; // Type is modified, but same
Chris Lattner05950c32001-10-13 06:47:01 +0000108
Chris Lattner1d670cc2001-09-07 16:37:43 +0000109 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 Lattnere244a252001-11-03 03:27:53 +0000117 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 Lattner1d670cc2001-09-07 16:37:43 +0000123}
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//
Chris Lattner92940ac2002-04-07 06:11:22 +0000137void debug_type_tables();
Chris Lattner1d670cc2001-09-07 16:37:43 +0000138bool BytecodeParser::parseTypeConstants(const uchar *&Buf, const uchar *EndBuf,
139 TypeValuesListTy &Tab,
140 unsigned NumEntries) {
Chris Lattner05950c32001-10-13 06:47:01 +0000141 assert(Tab.size() == 0 && "should not have read type constants in before!");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000142
143 // Insert a bunch of opaque types to be resolved later...
Chris Lattner7eadfa12001-10-24 06:21:22 +0000144 for (unsigned i = 0; i < NumEntries; ++i)
Chris Lattner1d670cc2001-09-07 16:37:43 +0000145 Tab.push_back(PATypeHandle<Type>(OpaqueType::get(), this));
146
147 // Loop through reading all of the types. Forward types will make use of the
148 // opaque types just inserted.
149 //
Chris Lattner7eadfa12001-10-24 06:21:22 +0000150 for (unsigned i = 0; i < NumEntries; ++i) {
Chris Lattner05950c32001-10-13 06:47:01 +0000151 const Type *NewTy = parseTypeConstant(Buf, EndBuf), *OldTy = Tab[i].get();
Chris Lattner1d670cc2001-09-07 16:37:43 +0000152 if (NewTy == 0) return failure(true);
Chris Lattner7eadfa12001-10-24 06:21:22 +0000153 BCR_TRACE(4, "#" << i << ": Read Type Constant: '" << NewTy <<
154 "' Replacing: " << OldTy << "\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000155
156 // Don't insertValue the new type... instead we want to replace the opaque
157 // type with the new concrete value...
158 //
159
160 // Refine the abstract type to the new type. This causes all uses of the
161 // abstract type to use the newty. This also will cause the opaque type
162 // to be deleted...
163 //
Chris Lattnera48836b2002-06-05 17:38:28 +0000164 ((DerivedType*)Tab[i].get())->refineAbstractTypeTo(NewTy);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000165
166 // This should have replace the old opaque type with the new type in the
Chris Lattner05950c32001-10-13 06:47:01 +0000167 // value table... or with a preexisting type that was already in the system
168 assert(Tab[i] != OldTy && "refineAbstractType didn't work!");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000169 }
170
171 BCR_TRACE(5, "Resulting types:\n");
Chris Lattner7eadfa12001-10-24 06:21:22 +0000172 for (unsigned i = 0; i < NumEntries; ++i) {
Chris Lattner92940ac2002-04-07 06:11:22 +0000173 BCR_TRACE(5, (void*)Tab[i].get() << " - " << Tab[i].get() << "\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000174 }
Chris Lattner92940ac2002-04-07 06:11:22 +0000175 debug_type_tables();
Chris Lattner00950542001-06-06 20:29:01 +0000176 return false;
177}
178
Chris Lattner1d670cc2001-09-07 16:37:43 +0000179
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000180bool BytecodeParser::parseConstantValue(const uchar *&Buf, const uchar *EndBuf,
181 const Type *Ty, Constant *&V) {
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000182
183 // We must check for a ConstantExpr before switching by type because
184 // a ConstantExpr can be of any type, and has no explicit value.
185 //
186 unsigned isExprNumArgs; // 0 if not expr; numArgs if is expr
187 if (read_vbr(Buf, EndBuf, isExprNumArgs)) return failure(true);
188 if (isExprNumArgs) {
189 unsigned opCode;
Anand Shukla18be6fb2002-07-16 00:04:15 +0000190 std::vector<Constant*> argVec;
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000191 argVec.reserve(isExprNumArgs);
192
193 if (read_vbr(Buf, EndBuf, opCode)) return failure(true);
194
195 // Read the slot number and types of each of the arguments
196 for (unsigned i=0; i < isExprNumArgs; ++i) {
197 unsigned argValSlot, argTypeSlot;
198 if (read_vbr(Buf, EndBuf, argValSlot)) return failure(true);
199 if (read_vbr(Buf, EndBuf, argTypeSlot)) return failure(true);
200 const Type *argTy = getType(argTypeSlot);
201 if (argTy == 0) return failure(true);
202
Chris Lattner9a9433b2002-07-25 15:40:04 +0000203 BCR_TRACE(4, "CE Arg " << i << ": Type: '" << argTy << "' slot: "
204 << argValSlot << "\n");
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000205
206 // Get the arg value from its slot if it exists, otherwise a placeholder
207 Value *Val = getValue(argTy, argValSlot, false);
Chris Lattnercc4b6ec2002-07-18 00:14:27 +0000208 Constant *C;
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000209 if (Val) {
210 if (!(C = dyn_cast<Constant>(Val))) return failure(true);
211 BCR_TRACE(5, "Constant Found in ValueTable!\n");
212 } else { // Nope... find or create a forward ref. for it
213 C = fwdRefs.GetFwdRefToConstant(argTy, argValSlot);
214 }
215 argVec.push_back(C);
216 }
217
218 // Construct a ConstantExpr of the appropriate kind
219 if (isExprNumArgs == 1) { // All one-operand expressions
220 V = ConstantExpr::get(opCode, argVec[0], Ty);
221 } else if (opCode == Instruction::GetElementPtr) { // GetElementPtr
Chris Lattnercc4b6ec2002-07-18 00:14:27 +0000222 std::vector<Constant*> IdxList(argVec.begin()+1, argVec.end());
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000223 V = ConstantExpr::get(opCode, argVec[0], IdxList, Ty);
224 } else { // All other 2-operand expressions
225 V = ConstantExpr::get(opCode, argVec[0], argVec[1], Ty);
226 }
227 return false;
228 }
229
230 // Ok, not an ConstantExpr. We now know how to read the given type...
Chris Lattner00950542001-06-06 20:29:01 +0000231 switch (Ty->getPrimitiveID()) {
232 case Type::BoolTyID: {
233 unsigned Val;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000234 if (read_vbr(Buf, EndBuf, Val)) return failure(true);
235 if (Val != 0 && Val != 1) return failure(true);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000236 V = ConstantBool::get(Val == 1);
Chris Lattner00950542001-06-06 20:29:01 +0000237 break;
238 }
239
240 case Type::UByteTyID: // Unsigned integer types...
241 case Type::UShortTyID:
242 case Type::UIntTyID: {
243 unsigned Val;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000244 if (read_vbr(Buf, EndBuf, Val)) return failure(true);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000245 if (!ConstantUInt::isValueValidForType(Ty, Val)) return failure(true);
246 V = ConstantUInt::get(Ty, Val);
Chris Lattner00950542001-06-06 20:29:01 +0000247 break;
248 }
249
250 case Type::ULongTyID: {
251 uint64_t Val;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000252 if (read_vbr(Buf, EndBuf, Val)) return failure(true);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000253 V = ConstantUInt::get(Ty, Val);
Chris Lattner00950542001-06-06 20:29:01 +0000254 break;
255 }
256
257 case Type::SByteTyID: // Unsigned integer types...
258 case Type::ShortTyID:
259 case Type::IntTyID: {
260 int Val;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000261 if (read_vbr(Buf, EndBuf, Val)) return failure(true);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000262 if (!ConstantSInt::isValueValidForType(Ty, Val)) return failure(true);
263 V = ConstantSInt::get(Ty, Val);
Chris Lattner00950542001-06-06 20:29:01 +0000264 break;
265 }
266
267 case Type::LongTyID: {
268 int64_t Val;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000269 if (read_vbr(Buf, EndBuf, Val)) return failure(true);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000270 V = ConstantSInt::get(Ty, Val);
Chris Lattner00950542001-06-06 20:29:01 +0000271 break;
272 }
273
Chris Lattnera1375302001-07-15 00:17:18 +0000274 case Type::FloatTyID: {
275 float F;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000276 if (input_data(Buf, EndBuf, &F, &F+1)) return failure(true);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000277 V = ConstantFP::get(Ty, F);
Chris Lattnera1375302001-07-15 00:17:18 +0000278 break;
279 }
280
281 case Type::DoubleTyID: {
282 double Val;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000283 if (input_data(Buf, EndBuf, &Val, &Val+1)) return failure(true);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000284 V = ConstantFP::get(Ty, Val);
Chris Lattnera1375302001-07-15 00:17:18 +0000285 break;
286 }
287
Chris Lattner00950542001-06-06 20:29:01 +0000288 case Type::TypeTyID:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000289 assert(0 && "Type constants should be handled seperately!!!");
290 abort();
Chris Lattner00950542001-06-06 20:29:01 +0000291
292 case Type::ArrayTyID: {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000293 const ArrayType *AT = cast<const ArrayType>(Ty);
Chris Lattner90865152001-12-14 16:30:51 +0000294 unsigned NumElements = AT->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +0000295
Chris Lattner697954c2002-01-20 22:54:45 +0000296 std::vector<Constant*> Elements;
Chris Lattner00950542001-06-06 20:29:01 +0000297 while (NumElements--) { // Read all of the elements of the constant.
298 unsigned Slot;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000299 if (read_vbr(Buf, EndBuf, Slot)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000300 Value *V = getValue(AT->getElementType(), Slot, false);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000301 if (!V || !isa<Constant>(V)) return failure(true);
302 Elements.push_back(cast<Constant>(V));
Chris Lattner00950542001-06-06 20:29:01 +0000303 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000304 V = ConstantArray::get(AT, Elements);
Chris Lattner00950542001-06-06 20:29:01 +0000305 break;
306 }
307
308 case Type::StructTyID: {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000309 const StructType *ST = cast<StructType>(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000310 const StructType::ElementTypes &ET = ST->getElementTypes();
311
Chris Lattner697954c2002-01-20 22:54:45 +0000312 std::vector<Constant *> Elements;
Chris Lattner00950542001-06-06 20:29:01 +0000313 for (unsigned i = 0; i < ET.size(); ++i) {
314 unsigned Slot;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000315 if (read_vbr(Buf, EndBuf, Slot)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000316 Value *V = getValue(ET[i], Slot, false);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000317 if (!V || !isa<Constant>(V))
Chris Lattner3d3f2892001-07-28 17:50:18 +0000318 return failure(true);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000319 Elements.push_back(cast<Constant>(V));
Chris Lattner00950542001-06-06 20:29:01 +0000320 }
321
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000322 V = ConstantStruct::get(ST, Elements);
Chris Lattner00950542001-06-06 20:29:01 +0000323 break;
324 }
325
Chris Lattner1a1cb112001-09-30 22:46:54 +0000326 case Type::PointerTyID: {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000327 const PointerType *PT = cast<const PointerType>(Ty);
Chris Lattner1a1cb112001-09-30 22:46:54 +0000328 unsigned SubClass;
329 if (read_vbr(Buf, EndBuf, SubClass)) return failure(true);
Chris Lattner05950c32001-10-13 06:47:01 +0000330 switch (SubClass) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000331 case 0: // ConstantPointerNull value...
332 V = ConstantPointerNull::get(PT);
Chris Lattner05950c32001-10-13 06:47:01 +0000333 break;
Chris Lattner1a1cb112001-09-30 22:46:54 +0000334
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000335 case 1: { // ConstantPointerRef value...
Chris Lattner05950c32001-10-13 06:47:01 +0000336 unsigned Slot;
337 if (read_vbr(Buf, EndBuf, Slot)) return failure(true);
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000338 BCR_TRACE(4, "CPR: Type: '" << Ty << "' slot: " << Slot << "\n");
Chris Lattner1a1cb112001-09-30 22:46:54 +0000339
Chris Lattner05950c32001-10-13 06:47:01 +0000340 // Check to see if we have already read this global variable yet...
341 Value *Val = getValue(PT, Slot, false);
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000342 GlobalValue* GV;
Chris Lattner05950c32001-10-13 06:47:01 +0000343 if (Val) {
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000344 if (!(GV = dyn_cast<GlobalValue>(Val))) return failure(true);
345 BCR_TRACE(5, "Value Found in ValueTable!\n");
346 } else { // Nope... find or create a forward ref. for it
347 GV = fwdRefs.GetFwdRefToGlobal(PT, Slot);
Chris Lattner05950c32001-10-13 06:47:01 +0000348 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000349 V = ConstantPointerRef::get(GV);
Chris Lattner05950c32001-10-13 06:47:01 +0000350 break;
351 }
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000352
Chris Lattner05950c32001-10-13 06:47:01 +0000353 default:
Chris Lattner90865152001-12-14 16:30:51 +0000354 BCR_TRACE(5, "UNKNOWN Pointer Constant Type!\n");
Chris Lattner05950c32001-10-13 06:47:01 +0000355 return failure(true);
356 }
Chris Lattner1a1cb112001-09-30 22:46:54 +0000357 break;
358 }
359
Chris Lattner00950542001-06-06 20:29:01 +0000360 default:
Anand Shuklaeea60fc2002-06-25 20:44:04 +0000361 std::cerr << __FILE__ << ":" << __LINE__
Chris Lattner194f0a52002-06-30 16:04:37 +0000362 << ": Don't know how to deserialize constant value of type '"
363 << Ty->getName() << "'\n";
Chris Lattner3d3f2892001-07-28 17:50:18 +0000364 return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000365 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000366
Chris Lattner00950542001-06-06 20:29:01 +0000367 return false;
368}
369
370bool BytecodeParser::ParseConstantPool(const uchar *&Buf, const uchar *EndBuf,
Chris Lattner1d670cc2001-09-07 16:37:43 +0000371 ValueTable &Tab,
372 TypeValuesListTy &TypeTab) {
Chris Lattner00950542001-06-06 20:29:01 +0000373 while (Buf < EndBuf) {
374 unsigned NumEntries, Typ;
375
376 if (read_vbr(Buf, EndBuf, NumEntries) ||
Chris Lattner3d3f2892001-07-28 17:50:18 +0000377 read_vbr(Buf, EndBuf, Typ)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000378 const Type *Ty = getType(Typ);
Chris Lattner3d3f2892001-07-28 17:50:18 +0000379 if (Ty == 0) return failure(true);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000380 BCR_TRACE(3, "Type: '" << Ty << "' NumEntries: " << NumEntries << "\n");
Chris Lattner00950542001-06-06 20:29:01 +0000381
Chris Lattner1d670cc2001-09-07 16:37:43 +0000382 if (Typ == Type::TypeTyID) {
383 if (parseTypeConstants(Buf, EndBuf, TypeTab, NumEntries)) return true;
384 } else {
Chris Lattner7eadfa12001-10-24 06:21:22 +0000385 for (unsigned i = 0; i < NumEntries; ++i) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000386 Constant *I;
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000387 int Slot;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000388 if (parseConstantValue(Buf, EndBuf, Ty, I)) return failure(true);
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000389 assert(I && "parseConstantValue returned `!failure' and NULL result");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000390 BCR_TRACE(4, "Read Constant: '" << I << "'\n");
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000391 if ((Slot = insertValue(I, Tab)) < 0) return failure(true);
392 resolveRefsToConstant(I, (unsigned) Slot);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000393 }
Chris Lattner00950542001-06-06 20:29:01 +0000394 }
395 }
396
Chris Lattner3d3f2892001-07-28 17:50:18 +0000397 if (Buf > EndBuf) return failure(true);
398 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000399}