blob: 37e201f2807d644b3723b7c28aa8aae483d8938b [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) {
Chris Lattnere8e46052002-07-30 18:54:22 +0000189 // FIXME: Encoding of constant exprs could be much more compact!
190 unsigned Opcode;
191 std::vector<Constant*> ArgVec;
192 ArgVec.reserve(isExprNumArgs);
193 if (read_vbr(Buf, EndBuf, Opcode)) return failure(true);
194
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000195 // Read the slot number and types of each of the arguments
Chris Lattnere8e46052002-07-30 18:54:22 +0000196 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);
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000202
Chris Lattnere8e46052002-07-30 18:54:22 +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
Chris Lattnere8e46052002-07-30 18:54:22 +0000207 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
Chris Lattnere8e46052002-07-30 18:54:22 +0000213 C = fwdRefs.GetFwdRefToConstant(ArgTy, ArgValSlot);
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000214 }
Chris Lattnere8e46052002-07-30 18:54:22 +0000215 ArgVec.push_back(C);
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000216 }
217
218 // Construct a ConstantExpr of the appropriate kind
219 if (isExprNumArgs == 1) { // All one-operand expressions
Chris Lattnerad333482002-08-14 18:24:09 +0000220 assert(Opcode == Instruction::Cast);
221 V = ConstantExpr::getCast(ArgVec[0], Ty);
Chris Lattnere8e46052002-07-30 18:54:22 +0000222 } else if (Opcode == Instruction::GetElementPtr) { // GetElementPtr
223 std::vector<Constant*> IdxList(ArgVec.begin()+1, ArgVec.end());
224 V = ConstantExpr::getGetElementPtr(ArgVec[0], IdxList);
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000225 } else { // All other 2-operand expressions
Chris Lattnere8e46052002-07-30 18:54:22 +0000226 V = ConstantExpr::get(Opcode, ArgVec[0], ArgVec[1]);
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000227 }
228 return false;
229 }
230
231 // Ok, not an ConstantExpr. We now know how to read the given type...
Chris Lattner00950542001-06-06 20:29:01 +0000232 switch (Ty->getPrimitiveID()) {
233 case Type::BoolTyID: {
234 unsigned Val;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000235 if (read_vbr(Buf, EndBuf, Val)) return failure(true);
236 if (Val != 0 && Val != 1) return failure(true);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000237 V = ConstantBool::get(Val == 1);
Chris Lattner00950542001-06-06 20:29:01 +0000238 break;
239 }
240
241 case Type::UByteTyID: // Unsigned integer types...
242 case Type::UShortTyID:
243 case Type::UIntTyID: {
244 unsigned Val;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000245 if (read_vbr(Buf, EndBuf, Val)) return failure(true);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000246 if (!ConstantUInt::isValueValidForType(Ty, Val)) return failure(true);
247 V = ConstantUInt::get(Ty, Val);
Chris Lattner00950542001-06-06 20:29:01 +0000248 break;
249 }
250
251 case Type::ULongTyID: {
252 uint64_t Val;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000253 if (read_vbr(Buf, EndBuf, Val)) return failure(true);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000254 V = ConstantUInt::get(Ty, Val);
Chris Lattner00950542001-06-06 20:29:01 +0000255 break;
256 }
257
258 case Type::SByteTyID: // Unsigned integer types...
259 case Type::ShortTyID:
260 case Type::IntTyID: {
261 int Val;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000262 if (read_vbr(Buf, EndBuf, Val)) return failure(true);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000263 if (!ConstantSInt::isValueValidForType(Ty, Val)) return failure(true);
264 V = ConstantSInt::get(Ty, Val);
Chris Lattner00950542001-06-06 20:29:01 +0000265 break;
266 }
267
268 case Type::LongTyID: {
269 int64_t Val;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000270 if (read_vbr(Buf, EndBuf, Val)) return failure(true);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000271 V = ConstantSInt::get(Ty, Val);
Chris Lattner00950542001-06-06 20:29:01 +0000272 break;
273 }
274
Chris Lattnera1375302001-07-15 00:17:18 +0000275 case Type::FloatTyID: {
276 float F;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000277 if (input_data(Buf, EndBuf, &F, &F+1)) return failure(true);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000278 V = ConstantFP::get(Ty, F);
Chris Lattnera1375302001-07-15 00:17:18 +0000279 break;
280 }
281
282 case Type::DoubleTyID: {
283 double Val;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000284 if (input_data(Buf, EndBuf, &Val, &Val+1)) return failure(true);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000285 V = ConstantFP::get(Ty, Val);
Chris Lattnera1375302001-07-15 00:17:18 +0000286 break;
287 }
288
Chris Lattner00950542001-06-06 20:29:01 +0000289 case Type::TypeTyID:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000290 assert(0 && "Type constants should be handled seperately!!!");
291 abort();
Chris Lattner00950542001-06-06 20:29:01 +0000292
293 case Type::ArrayTyID: {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000294 const ArrayType *AT = cast<const ArrayType>(Ty);
Chris Lattner90865152001-12-14 16:30:51 +0000295 unsigned NumElements = AT->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +0000296
Chris Lattner697954c2002-01-20 22:54:45 +0000297 std::vector<Constant*> Elements;
Chris Lattner00950542001-06-06 20:29:01 +0000298 while (NumElements--) { // Read all of the elements of the constant.
299 unsigned Slot;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000300 if (read_vbr(Buf, EndBuf, Slot)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000301 Value *V = getValue(AT->getElementType(), Slot, false);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000302 if (!V || !isa<Constant>(V)) return failure(true);
303 Elements.push_back(cast<Constant>(V));
Chris Lattner00950542001-06-06 20:29:01 +0000304 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000305 V = ConstantArray::get(AT, Elements);
Chris Lattner00950542001-06-06 20:29:01 +0000306 break;
307 }
308
309 case Type::StructTyID: {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000310 const StructType *ST = cast<StructType>(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000311 const StructType::ElementTypes &ET = ST->getElementTypes();
312
Chris Lattner697954c2002-01-20 22:54:45 +0000313 std::vector<Constant *> Elements;
Chris Lattner00950542001-06-06 20:29:01 +0000314 for (unsigned i = 0; i < ET.size(); ++i) {
315 unsigned Slot;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000316 if (read_vbr(Buf, EndBuf, Slot)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000317 Value *V = getValue(ET[i], Slot, false);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000318 if (!V || !isa<Constant>(V))
Chris Lattner3d3f2892001-07-28 17:50:18 +0000319 return failure(true);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000320 Elements.push_back(cast<Constant>(V));
Chris Lattner00950542001-06-06 20:29:01 +0000321 }
322
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000323 V = ConstantStruct::get(ST, Elements);
Chris Lattner00950542001-06-06 20:29:01 +0000324 break;
325 }
326
Chris Lattner1a1cb112001-09-30 22:46:54 +0000327 case Type::PointerTyID: {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000328 const PointerType *PT = cast<const PointerType>(Ty);
Chris Lattner1a1cb112001-09-30 22:46:54 +0000329 unsigned SubClass;
330 if (read_vbr(Buf, EndBuf, SubClass)) return failure(true);
Chris Lattner05950c32001-10-13 06:47:01 +0000331 switch (SubClass) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000332 case 0: // ConstantPointerNull value...
333 V = ConstantPointerNull::get(PT);
Chris Lattner05950c32001-10-13 06:47:01 +0000334 break;
Chris Lattner1a1cb112001-09-30 22:46:54 +0000335
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000336 case 1: { // ConstantPointerRef value...
Chris Lattner05950c32001-10-13 06:47:01 +0000337 unsigned Slot;
338 if (read_vbr(Buf, EndBuf, Slot)) return failure(true);
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000339 BCR_TRACE(4, "CPR: Type: '" << Ty << "' slot: " << Slot << "\n");
Chris Lattner1a1cb112001-09-30 22:46:54 +0000340
Chris Lattner05950c32001-10-13 06:47:01 +0000341 // Check to see if we have already read this global variable yet...
342 Value *Val = getValue(PT, Slot, false);
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000343 GlobalValue* GV;
Chris Lattner05950c32001-10-13 06:47:01 +0000344 if (Val) {
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000345 if (!(GV = dyn_cast<GlobalValue>(Val))) return failure(true);
346 BCR_TRACE(5, "Value Found in ValueTable!\n");
347 } else { // Nope... find or create a forward ref. for it
348 GV = fwdRefs.GetFwdRefToGlobal(PT, Slot);
Chris Lattner05950c32001-10-13 06:47:01 +0000349 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000350 V = ConstantPointerRef::get(GV);
Chris Lattner05950c32001-10-13 06:47:01 +0000351 break;
352 }
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000353
Chris Lattner05950c32001-10-13 06:47:01 +0000354 default:
Chris Lattner90865152001-12-14 16:30:51 +0000355 BCR_TRACE(5, "UNKNOWN Pointer Constant Type!\n");
Chris Lattner05950c32001-10-13 06:47:01 +0000356 return failure(true);
357 }
Chris Lattner1a1cb112001-09-30 22:46:54 +0000358 break;
359 }
360
Chris Lattner00950542001-06-06 20:29:01 +0000361 default:
Anand Shuklaeea60fc2002-06-25 20:44:04 +0000362 std::cerr << __FILE__ << ":" << __LINE__
Chris Lattner194f0a52002-06-30 16:04:37 +0000363 << ": Don't know how to deserialize constant value of type '"
364 << Ty->getName() << "'\n";
Chris Lattner3d3f2892001-07-28 17:50:18 +0000365 return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000366 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000367
Chris Lattner00950542001-06-06 20:29:01 +0000368 return false;
369}
370
371bool BytecodeParser::ParseConstantPool(const uchar *&Buf, const uchar *EndBuf,
Chris Lattner1d670cc2001-09-07 16:37:43 +0000372 ValueTable &Tab,
373 TypeValuesListTy &TypeTab) {
Chris Lattner00950542001-06-06 20:29:01 +0000374 while (Buf < EndBuf) {
375 unsigned NumEntries, Typ;
376
377 if (read_vbr(Buf, EndBuf, NumEntries) ||
Chris Lattner3d3f2892001-07-28 17:50:18 +0000378 read_vbr(Buf, EndBuf, Typ)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000379 const Type *Ty = getType(Typ);
Chris Lattner3d3f2892001-07-28 17:50:18 +0000380 if (Ty == 0) return failure(true);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000381 BCR_TRACE(3, "Type: '" << Ty << "' NumEntries: " << NumEntries << "\n");
Chris Lattner00950542001-06-06 20:29:01 +0000382
Chris Lattner1d670cc2001-09-07 16:37:43 +0000383 if (Typ == Type::TypeTyID) {
384 if (parseTypeConstants(Buf, EndBuf, TypeTab, NumEntries)) return true;
385 } else {
Chris Lattner7eadfa12001-10-24 06:21:22 +0000386 for (unsigned i = 0; i < NumEntries; ++i) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000387 Constant *I;
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000388 int Slot;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000389 if (parseConstantValue(Buf, EndBuf, Ty, I)) return failure(true);
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000390 assert(I && "parseConstantValue returned `!failure' and NULL result");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000391 BCR_TRACE(4, "Read Constant: '" << I << "'\n");
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000392 if ((Slot = insertValue(I, Tab)) < 0) return failure(true);
393 resolveRefsToConstant(I, (unsigned) Slot);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000394 }
Chris Lattner00950542001-06-06 20:29:01 +0000395 }
396 }
397
Chris Lattner3d3f2892001-07-28 17:50:18 +0000398 if (Buf > EndBuf) return failure(true);
399 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000400}