blob: 68d6a62249d2150e0dbcc158c36f01ce2b3d7646 [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
203 BCR_TRACE(4, "CE Arg " << i << ": Type: '" << argTy << "' slot: " << argValSlot << "\n");
204
205 // Get the arg value from its slot if it exists, otherwise a placeholder
206 Value *Val = getValue(argTy, argValSlot, false);
207 Constant* C;
208 if (Val) {
209 if (!(C = dyn_cast<Constant>(Val))) return failure(true);
210 BCR_TRACE(5, "Constant Found in ValueTable!\n");
211 } else { // Nope... find or create a forward ref. for it
212 C = fwdRefs.GetFwdRefToConstant(argTy, argValSlot);
213 }
214 argVec.push_back(C);
215 }
216
217 // Construct a ConstantExpr of the appropriate kind
218 if (isExprNumArgs == 1) { // All one-operand expressions
219 V = ConstantExpr::get(opCode, argVec[0], Ty);
220 } else if (opCode == Instruction::GetElementPtr) { // GetElementPtr
Anand Shukla18be6fb2002-07-16 00:04:15 +0000221 std::vector<Value*> IdxList(argVec.begin()+1, argVec.end());
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000222 V = ConstantExpr::get(opCode, argVec[0], IdxList, Ty);
223 } else { // All other 2-operand expressions
224 V = ConstantExpr::get(opCode, argVec[0], argVec[1], Ty);
225 }
226 return false;
227 }
228
229 // Ok, not an ConstantExpr. We now know how to read the given type...
Chris Lattner00950542001-06-06 20:29:01 +0000230 switch (Ty->getPrimitiveID()) {
231 case Type::BoolTyID: {
232 unsigned Val;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000233 if (read_vbr(Buf, EndBuf, Val)) return failure(true);
234 if (Val != 0 && Val != 1) return failure(true);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000235 V = ConstantBool::get(Val == 1);
Chris Lattner00950542001-06-06 20:29:01 +0000236 break;
237 }
238
239 case Type::UByteTyID: // Unsigned integer types...
240 case Type::UShortTyID:
241 case Type::UIntTyID: {
242 unsigned Val;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000243 if (read_vbr(Buf, EndBuf, Val)) return failure(true);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000244 if (!ConstantUInt::isValueValidForType(Ty, Val)) return failure(true);
245 V = ConstantUInt::get(Ty, Val);
Chris Lattner00950542001-06-06 20:29:01 +0000246 break;
247 }
248
249 case Type::ULongTyID: {
250 uint64_t Val;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000251 if (read_vbr(Buf, EndBuf, Val)) return failure(true);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000252 V = ConstantUInt::get(Ty, Val);
Chris Lattner00950542001-06-06 20:29:01 +0000253 break;
254 }
255
256 case Type::SByteTyID: // Unsigned integer types...
257 case Type::ShortTyID:
258 case Type::IntTyID: {
259 int Val;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000260 if (read_vbr(Buf, EndBuf, Val)) return failure(true);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000261 if (!ConstantSInt::isValueValidForType(Ty, Val)) return failure(true);
262 V = ConstantSInt::get(Ty, Val);
Chris Lattner00950542001-06-06 20:29:01 +0000263 break;
264 }
265
266 case Type::LongTyID: {
267 int64_t Val;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000268 if (read_vbr(Buf, EndBuf, Val)) return failure(true);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000269 V = ConstantSInt::get(Ty, Val);
Chris Lattner00950542001-06-06 20:29:01 +0000270 break;
271 }
272
Chris Lattnera1375302001-07-15 00:17:18 +0000273 case Type::FloatTyID: {
274 float F;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000275 if (input_data(Buf, EndBuf, &F, &F+1)) return failure(true);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000276 V = ConstantFP::get(Ty, F);
Chris Lattnera1375302001-07-15 00:17:18 +0000277 break;
278 }
279
280 case Type::DoubleTyID: {
281 double Val;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000282 if (input_data(Buf, EndBuf, &Val, &Val+1)) return failure(true);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000283 V = ConstantFP::get(Ty, Val);
Chris Lattnera1375302001-07-15 00:17:18 +0000284 break;
285 }
286
Chris Lattner00950542001-06-06 20:29:01 +0000287 case Type::TypeTyID:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000288 assert(0 && "Type constants should be handled seperately!!!");
289 abort();
Chris Lattner00950542001-06-06 20:29:01 +0000290
291 case Type::ArrayTyID: {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000292 const ArrayType *AT = cast<const ArrayType>(Ty);
Chris Lattner90865152001-12-14 16:30:51 +0000293 unsigned NumElements = AT->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +0000294
Chris Lattner697954c2002-01-20 22:54:45 +0000295 std::vector<Constant*> Elements;
Chris Lattner00950542001-06-06 20:29:01 +0000296 while (NumElements--) { // Read all of the elements of the constant.
297 unsigned Slot;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000298 if (read_vbr(Buf, EndBuf, Slot)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000299 Value *V = getValue(AT->getElementType(), Slot, false);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000300 if (!V || !isa<Constant>(V)) return failure(true);
301 Elements.push_back(cast<Constant>(V));
Chris Lattner00950542001-06-06 20:29:01 +0000302 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000303 V = ConstantArray::get(AT, Elements);
Chris Lattner00950542001-06-06 20:29:01 +0000304 break;
305 }
306
307 case Type::StructTyID: {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000308 const StructType *ST = cast<StructType>(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000309 const StructType::ElementTypes &ET = ST->getElementTypes();
310
Chris Lattner697954c2002-01-20 22:54:45 +0000311 std::vector<Constant *> Elements;
Chris Lattner00950542001-06-06 20:29:01 +0000312 for (unsigned i = 0; i < ET.size(); ++i) {
313 unsigned Slot;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000314 if (read_vbr(Buf, EndBuf, Slot)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000315 Value *V = getValue(ET[i], Slot, false);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000316 if (!V || !isa<Constant>(V))
Chris Lattner3d3f2892001-07-28 17:50:18 +0000317 return failure(true);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000318 Elements.push_back(cast<Constant>(V));
Chris Lattner00950542001-06-06 20:29:01 +0000319 }
320
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000321 V = ConstantStruct::get(ST, Elements);
Chris Lattner00950542001-06-06 20:29:01 +0000322 break;
323 }
324
Chris Lattner1a1cb112001-09-30 22:46:54 +0000325 case Type::PointerTyID: {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000326 const PointerType *PT = cast<const PointerType>(Ty);
Chris Lattner1a1cb112001-09-30 22:46:54 +0000327 unsigned SubClass;
328 if (read_vbr(Buf, EndBuf, SubClass)) return failure(true);
Chris Lattner05950c32001-10-13 06:47:01 +0000329 switch (SubClass) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000330 case 0: // ConstantPointerNull value...
331 V = ConstantPointerNull::get(PT);
Chris Lattner05950c32001-10-13 06:47:01 +0000332 break;
Chris Lattner1a1cb112001-09-30 22:46:54 +0000333
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000334 case 1: { // ConstantPointerRef value...
Chris Lattner05950c32001-10-13 06:47:01 +0000335 unsigned Slot;
336 if (read_vbr(Buf, EndBuf, Slot)) return failure(true);
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000337 BCR_TRACE(4, "CPR: Type: '" << Ty << "' slot: " << Slot << "\n");
Chris Lattner1a1cb112001-09-30 22:46:54 +0000338
Chris Lattner05950c32001-10-13 06:47:01 +0000339 // Check to see if we have already read this global variable yet...
340 Value *Val = getValue(PT, Slot, false);
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000341 GlobalValue* GV;
Chris Lattner05950c32001-10-13 06:47:01 +0000342 if (Val) {
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000343 if (!(GV = dyn_cast<GlobalValue>(Val))) return failure(true);
344 BCR_TRACE(5, "Value Found in ValueTable!\n");
345 } else { // Nope... find or create a forward ref. for it
346 GV = fwdRefs.GetFwdRefToGlobal(PT, Slot);
Chris Lattner05950c32001-10-13 06:47:01 +0000347 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000348 V = ConstantPointerRef::get(GV);
Chris Lattner05950c32001-10-13 06:47:01 +0000349 break;
350 }
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000351
Chris Lattner05950c32001-10-13 06:47:01 +0000352 default:
Chris Lattner90865152001-12-14 16:30:51 +0000353 BCR_TRACE(5, "UNKNOWN Pointer Constant Type!\n");
Chris Lattner05950c32001-10-13 06:47:01 +0000354 return failure(true);
355 }
Chris Lattner1a1cb112001-09-30 22:46:54 +0000356 break;
357 }
358
Chris Lattner00950542001-06-06 20:29:01 +0000359 default:
Anand Shuklaeea60fc2002-06-25 20:44:04 +0000360 std::cerr << __FILE__ << ":" << __LINE__
Chris Lattner194f0a52002-06-30 16:04:37 +0000361 << ": Don't know how to deserialize constant value of type '"
362 << Ty->getName() << "'\n";
Chris Lattner3d3f2892001-07-28 17:50:18 +0000363 return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000364 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000365
Chris Lattner00950542001-06-06 20:29:01 +0000366 return false;
367}
368
369bool BytecodeParser::ParseConstantPool(const uchar *&Buf, const uchar *EndBuf,
Chris Lattner1d670cc2001-09-07 16:37:43 +0000370 ValueTable &Tab,
371 TypeValuesListTy &TypeTab) {
Chris Lattner00950542001-06-06 20:29:01 +0000372 while (Buf < EndBuf) {
373 unsigned NumEntries, Typ;
374
375 if (read_vbr(Buf, EndBuf, NumEntries) ||
Chris Lattner3d3f2892001-07-28 17:50:18 +0000376 read_vbr(Buf, EndBuf, Typ)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000377 const Type *Ty = getType(Typ);
Chris Lattner3d3f2892001-07-28 17:50:18 +0000378 if (Ty == 0) return failure(true);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000379 BCR_TRACE(3, "Type: '" << Ty << "' NumEntries: " << NumEntries << "\n");
Chris Lattner00950542001-06-06 20:29:01 +0000380
Chris Lattner1d670cc2001-09-07 16:37:43 +0000381 if (Typ == Type::TypeTyID) {
382 if (parseTypeConstants(Buf, EndBuf, TypeTab, NumEntries)) return true;
383 } else {
Chris Lattner7eadfa12001-10-24 06:21:22 +0000384 for (unsigned i = 0; i < NumEntries; ++i) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000385 Constant *I;
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000386 int Slot;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000387 if (parseConstantValue(Buf, EndBuf, Ty, I)) return failure(true);
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000388 assert(I && "parseConstantValue returned `!failure' and NULL result");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000389 BCR_TRACE(4, "Read Constant: '" << I << "'\n");
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000390 if ((Slot = insertValue(I, Tab)) < 0) return failure(true);
391 resolveRefsToConstant(I, (unsigned) Slot);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000392 }
Chris Lattner00950542001-06-06 20:29:01 +0000393 }
394 }
395
Chris Lattner3d3f2892001-07-28 17:50:18 +0000396 if (Buf > EndBuf) return failure(true);
397 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000398}