blob: df1336084e30f7609c706c7fe7b42a16cd8ba375 [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 Lattnere4619182002-08-17 22:02:41 +000013#include "llvm/Constants.h"
Chris Lattner1d670cc2001-09-07 16:37:43 +000014#include <algorithm>
Anand Shuklaeea60fc2002-06-25 20:44:04 +000015
Chris Lattner697954c2002-01-20 22:54:45 +000016using std::make_pair;
Chris Lattner1d670cc2001-09-07 16:37:43 +000017
18const Type *BytecodeParser::parseTypeConstant(const uchar *&Buf,
19 const uchar *EndBuf) {
Chris Lattner00950542001-06-06 20:29:01 +000020 unsigned PrimType;
Chris Lattner74734132002-08-17 22:01:27 +000021 if (read_vbr(Buf, EndBuf, PrimType)) return 0;
Chris Lattner00950542001-06-06 20:29:01 +000022
Chris Lattner1d670cc2001-09-07 16:37:43 +000023 const Type *Val = 0;
24 if ((Val = Type::getPrimitiveType((Type::PrimitiveID)PrimType)))
25 return Val;
Chris Lattner00950542001-06-06 20:29:01 +000026
27 switch (PrimType) {
Chris Lattnerc9aa7df2002-03-29 03:51:11 +000028 case Type::FunctionTyID: {
Chris Lattner00950542001-06-06 20:29:01 +000029 unsigned Typ;
Chris Lattner74734132002-08-17 22:01:27 +000030 if (read_vbr(Buf, EndBuf, Typ)) return Val;
Chris Lattner00950542001-06-06 20:29:01 +000031 const Type *RetType = getType(Typ);
Chris Lattner74734132002-08-17 22:01:27 +000032 if (RetType == 0) return Val;
Chris Lattner00950542001-06-06 20:29:01 +000033
Chris Lattnere5a57ee2001-07-25 22:47:55 +000034 unsigned NumParams;
Chris Lattner74734132002-08-17 22:01:27 +000035 if (read_vbr(Buf, EndBuf, NumParams)) return Val;
Chris Lattner00950542001-06-06 20:29:01 +000036
Chris Lattner697954c2002-01-20 22:54:45 +000037 std::vector<const Type*> Params;
Chris Lattnere5a57ee2001-07-25 22:47:55 +000038 while (NumParams--) {
Chris Lattner74734132002-08-17 22:01:27 +000039 if (read_vbr(Buf, EndBuf, Typ)) return Val;
Chris Lattner00950542001-06-06 20:29:01 +000040 const Type *Ty = getType(Typ);
Chris Lattner74734132002-08-17 22:01:27 +000041 if (Ty == 0) return Val;
Chris Lattner00950542001-06-06 20:29:01 +000042 Params.push_back(Ty);
Chris Lattner00950542001-06-06 20:29:01 +000043 }
44
Chris Lattner05950c32001-10-13 06:47:01 +000045 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
46 if (isVarArg) Params.pop_back();
47
Chris Lattnerc9aa7df2002-03-29 03:51:11 +000048 return FunctionType::get(RetType, Params, isVarArg);
Chris Lattner00950542001-06-06 20:29:01 +000049 }
50 case Type::ArrayTyID: {
51 unsigned ElTyp;
Chris Lattner74734132002-08-17 22:01:27 +000052 if (read_vbr(Buf, EndBuf, ElTyp)) return Val;
Chris Lattner00950542001-06-06 20:29:01 +000053 const Type *ElementType = getType(ElTyp);
Chris Lattner74734132002-08-17 22:01:27 +000054 if (ElementType == 0) return Val;
Chris Lattner00950542001-06-06 20:29:01 +000055
Chris Lattner90865152001-12-14 16:30:51 +000056 unsigned NumElements;
Chris Lattner74734132002-08-17 22:01:27 +000057 if (read_vbr(Buf, EndBuf, NumElements)) return Val;
Chris Lattner7eadfa12001-10-24 06:21:22 +000058
59 BCR_TRACE(5, "Array Type Constant #" << ElTyp << " size="
Chris Lattner697954c2002-01-20 22:54:45 +000060 << NumElements << "\n");
Chris Lattner7eadfa12001-10-24 06:21:22 +000061 return ArrayType::get(ElementType, NumElements);
Chris Lattner00950542001-06-06 20:29:01 +000062 }
63 case Type::StructTyID: {
64 unsigned Typ;
Chris Lattner697954c2002-01-20 22:54:45 +000065 std::vector<const Type*> Elements;
Chris Lattner00950542001-06-06 20:29:01 +000066
Chris Lattner74734132002-08-17 22:01:27 +000067 if (read_vbr(Buf, EndBuf, Typ)) return Val;
Chris Lattner00950542001-06-06 20:29:01 +000068 while (Typ) { // List is terminated by void/0 typeid
69 const Type *Ty = getType(Typ);
Chris Lattner74734132002-08-17 22:01:27 +000070 if (Ty == 0) return Val;
Chris Lattner00950542001-06-06 20:29:01 +000071 Elements.push_back(Ty);
72
Chris Lattner74734132002-08-17 22:01:27 +000073 if (read_vbr(Buf, EndBuf, Typ)) return Val;
Chris Lattner00950542001-06-06 20:29:01 +000074 }
75
Chris Lattner7eadfa12001-10-24 06:21:22 +000076 return StructType::get(Elements);
Chris Lattner00950542001-06-06 20:29:01 +000077 }
78 case Type::PointerTyID: {
79 unsigned ElTyp;
Chris Lattner74734132002-08-17 22:01:27 +000080 if (read_vbr(Buf, EndBuf, ElTyp)) return Val;
Chris Lattner697954c2002-01-20 22:54:45 +000081 BCR_TRACE(5, "Pointer Type Constant #" << (ElTyp-14) << "\n");
Chris Lattner00950542001-06-06 20:29:01 +000082 const Type *ElementType = getType(ElTyp);
Chris Lattner74734132002-08-17 22:01:27 +000083 if (ElementType == 0) return Val;
Chris Lattner7eadfa12001-10-24 06:21:22 +000084 return PointerType::get(ElementType);
Chris Lattner00950542001-06-06 20:29:01 +000085 }
86
Chris Lattnerd48d6c72001-10-23 01:53:01 +000087 case Type::OpaqueTyID: {
Chris Lattner7eadfa12001-10-24 06:21:22 +000088 return OpaqueType::get();
Chris Lattnerd48d6c72001-10-23 01:53:01 +000089 }
90
Chris Lattner00950542001-06-06 20:29:01 +000091 default:
Anand Shuklaeea60fc2002-06-25 20:44:04 +000092 std::cerr << __FILE__ << ":" << __LINE__
Chris Lattner194f0a52002-06-30 16:04:37 +000093 << ": Don't know how to deserialize"
94 << " primitive Type " << PrimType << "\n";
Chris Lattner74734132002-08-17 22:01:27 +000095 return Val;
Chris Lattner00950542001-06-06 20:29:01 +000096 }
Chris Lattner1d670cc2001-09-07 16:37:43 +000097}
98
99// refineAbstractType - The callback method is invoked when one of the
100// elements of TypeValues becomes more concrete...
101//
102void BytecodeParser::refineAbstractType(const DerivedType *OldType,
103 const Type *NewType) {
Chris Lattnere244a252001-11-03 03:27:53 +0000104 if (OldType == NewType &&
105 OldType->isAbstract()) return; // Type is modified, but same
Chris Lattner05950c32001-10-13 06:47:01 +0000106
Chris Lattner1d670cc2001-09-07 16:37:43 +0000107 TypeValuesListTy::iterator I = find(MethodTypeValues.begin(),
108 MethodTypeValues.end(), OldType);
109 if (I == MethodTypeValues.end()) {
110 I = find(ModuleTypeValues.begin(), ModuleTypeValues.end(), OldType);
111 assert(I != ModuleTypeValues.end() &&
112 "Can't refine a type I don't know about!");
113 }
114
Chris Lattnere244a252001-11-03 03:27:53 +0000115 if (OldType == NewType) {
116 assert(!OldType->isAbstract());
117 I->removeUserFromConcrete();
118 } else {
119 *I = NewType; // Update to point to new, more refined type.
120 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000121}
122
123
124
125// parseTypeConstants - We have to use this wierd code to handle recursive
126// types. We know that recursive types will only reference the current slab of
127// values in the type plane, but they can forward reference types before they
128// have been read. For example, Type #0 might be '{ Ty#1 }' and Type #1 might
129// be 'Ty#0*'. When reading Type #0, type number one doesn't exist. To fix
130// this ugly problem, we pesimistically insert an opaque type for each type we
131// are about to read. This means that forward references will resolve to
132// something and when we reread the type later, we can replace the opaque type
133// with a new resolved concrete type.
134//
Chris Lattner92940ac2002-04-07 06:11:22 +0000135void debug_type_tables();
Chris Lattner1d670cc2001-09-07 16:37:43 +0000136bool BytecodeParser::parseTypeConstants(const uchar *&Buf, const uchar *EndBuf,
137 TypeValuesListTy &Tab,
138 unsigned NumEntries) {
Chris Lattner05950c32001-10-13 06:47:01 +0000139 assert(Tab.size() == 0 && "should not have read type constants in before!");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000140
141 // Insert a bunch of opaque types to be resolved later...
Chris Lattner7eadfa12001-10-24 06:21:22 +0000142 for (unsigned i = 0; i < NumEntries; ++i)
Chris Lattner1d670cc2001-09-07 16:37:43 +0000143 Tab.push_back(PATypeHandle<Type>(OpaqueType::get(), this));
144
145 // Loop through reading all of the types. Forward types will make use of the
146 // opaque types just inserted.
147 //
Chris Lattner7eadfa12001-10-24 06:21:22 +0000148 for (unsigned i = 0; i < NumEntries; ++i) {
Chris Lattner05950c32001-10-13 06:47:01 +0000149 const Type *NewTy = parseTypeConstant(Buf, EndBuf), *OldTy = Tab[i].get();
Chris Lattner74734132002-08-17 22:01:27 +0000150 if (NewTy == 0) return true;
Chris Lattner7eadfa12001-10-24 06:21:22 +0000151 BCR_TRACE(4, "#" << i << ": Read Type Constant: '" << NewTy <<
152 "' Replacing: " << OldTy << "\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000153
154 // Don't insertValue the new type... instead we want to replace the opaque
155 // type with the new concrete value...
156 //
157
158 // Refine the abstract type to the new type. This causes all uses of the
159 // abstract type to use the newty. This also will cause the opaque type
160 // to be deleted...
161 //
Chris Lattnera48836b2002-06-05 17:38:28 +0000162 ((DerivedType*)Tab[i].get())->refineAbstractTypeTo(NewTy);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000163
164 // This should have replace the old opaque type with the new type in the
Chris Lattner05950c32001-10-13 06:47:01 +0000165 // value table... or with a preexisting type that was already in the system
166 assert(Tab[i] != OldTy && "refineAbstractType didn't work!");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000167 }
168
169 BCR_TRACE(5, "Resulting types:\n");
Chris Lattner7eadfa12001-10-24 06:21:22 +0000170 for (unsigned i = 0; i < NumEntries; ++i) {
Chris Lattner92940ac2002-04-07 06:11:22 +0000171 BCR_TRACE(5, (void*)Tab[i].get() << " - " << Tab[i].get() << "\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000172 }
Chris Lattner92940ac2002-04-07 06:11:22 +0000173 debug_type_tables();
Chris Lattner00950542001-06-06 20:29:01 +0000174 return false;
175}
176
Chris Lattner1d670cc2001-09-07 16:37:43 +0000177
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000178bool BytecodeParser::parseConstantValue(const uchar *&Buf, const uchar *EndBuf,
179 const Type *Ty, Constant *&V) {
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000180
181 // We must check for a ConstantExpr before switching by type because
182 // a ConstantExpr can be of any type, and has no explicit value.
183 //
184 unsigned isExprNumArgs; // 0 if not expr; numArgs if is expr
Chris Lattner74734132002-08-17 22:01:27 +0000185 if (read_vbr(Buf, EndBuf, isExprNumArgs)) return true;
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000186 if (isExprNumArgs) {
Chris Lattnere8e46052002-07-30 18:54:22 +0000187 // FIXME: Encoding of constant exprs could be much more compact!
188 unsigned Opcode;
189 std::vector<Constant*> ArgVec;
190 ArgVec.reserve(isExprNumArgs);
Chris Lattner74734132002-08-17 22:01:27 +0000191 if (read_vbr(Buf, EndBuf, Opcode)) return true;
Chris Lattnere8e46052002-07-30 18:54:22 +0000192
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000193 // Read the slot number and types of each of the arguments
Chris Lattnere8e46052002-07-30 18:54:22 +0000194 for (unsigned i = 0; i != isExprNumArgs; ++i) {
195 unsigned ArgValSlot, ArgTypeSlot;
Chris Lattner74734132002-08-17 22:01:27 +0000196 if (read_vbr(Buf, EndBuf, ArgValSlot)) return true;
197 if (read_vbr(Buf, EndBuf, ArgTypeSlot)) return true;
Chris Lattnere8e46052002-07-30 18:54:22 +0000198 const Type *ArgTy = getType(ArgTypeSlot);
Chris Lattner74734132002-08-17 22:01:27 +0000199 if (ArgTy == 0) return true;
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000200
Chris Lattnere8e46052002-07-30 18:54:22 +0000201 BCR_TRACE(4, "CE Arg " << i << ": Type: '" << ArgTy << "' slot: "
202 << ArgValSlot << "\n");
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000203
204 // Get the arg value from its slot if it exists, otherwise a placeholder
Chris Lattnere8e46052002-07-30 18:54:22 +0000205 Value *Val = getValue(ArgTy, ArgValSlot, false);
Chris Lattnercc4b6ec2002-07-18 00:14:27 +0000206 Constant *C;
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000207 if (Val) {
Chris Lattner74734132002-08-17 22:01:27 +0000208 if (!(C = dyn_cast<Constant>(Val))) return true;
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000209 BCR_TRACE(5, "Constant Found in ValueTable!\n");
210 } else { // Nope... find or create a forward ref. for it
Chris Lattner74734132002-08-17 22:01:27 +0000211 GlobalRefsType::iterator I = GlobalRefs.find(make_pair(Ty, ArgValSlot));
212
213 if (I != GlobalRefs.end()) {
214 BCR_TRACE(5, "Previous forward ref found!\n");
215 C = cast<Constant>(I->second);
216 } else {
217 // Create a placeholder for the constant reference and
218 // keep track of the fact that we have a forward ref to recycle it
219 BCR_TRACE(5, "Creating new forward ref to a constant!\n");
220 C = new ConstPHolder(ArgTy, ArgValSlot);
221
222 // Keep track of the fact that we have a forward ref to recycle it
223 GlobalRefs.insert(make_pair(make_pair(ArgTy, ArgValSlot), C));
224 }
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000225 }
Chris Lattnere8e46052002-07-30 18:54:22 +0000226 ArgVec.push_back(C);
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000227 }
228
229 // Construct a ConstantExpr of the appropriate kind
230 if (isExprNumArgs == 1) { // All one-operand expressions
Chris Lattnerad333482002-08-14 18:24:09 +0000231 assert(Opcode == Instruction::Cast);
232 V = ConstantExpr::getCast(ArgVec[0], Ty);
Chris Lattnere8e46052002-07-30 18:54:22 +0000233 } else if (Opcode == Instruction::GetElementPtr) { // GetElementPtr
234 std::vector<Constant*> IdxList(ArgVec.begin()+1, ArgVec.end());
235 V = ConstantExpr::getGetElementPtr(ArgVec[0], IdxList);
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000236 } else { // All other 2-operand expressions
Chris Lattnere8e46052002-07-30 18:54:22 +0000237 V = ConstantExpr::get(Opcode, ArgVec[0], ArgVec[1]);
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000238 }
239 return false;
240 }
241
242 // Ok, not an ConstantExpr. We now know how to read the given type...
Chris Lattner00950542001-06-06 20:29:01 +0000243 switch (Ty->getPrimitiveID()) {
244 case Type::BoolTyID: {
245 unsigned Val;
Chris Lattner74734132002-08-17 22:01:27 +0000246 if (read_vbr(Buf, EndBuf, Val)) return true;
247 if (Val != 0 && Val != 1) return true;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000248 V = ConstantBool::get(Val == 1);
Chris Lattner00950542001-06-06 20:29:01 +0000249 break;
250 }
251
252 case Type::UByteTyID: // Unsigned integer types...
253 case Type::UShortTyID:
254 case Type::UIntTyID: {
255 unsigned Val;
Chris Lattner74734132002-08-17 22:01:27 +0000256 if (read_vbr(Buf, EndBuf, Val)) return true;
257 if (!ConstantUInt::isValueValidForType(Ty, Val)) return true;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000258 V = ConstantUInt::get(Ty, Val);
Chris Lattner00950542001-06-06 20:29:01 +0000259 break;
260 }
261
262 case Type::ULongTyID: {
263 uint64_t Val;
Chris Lattner74734132002-08-17 22:01:27 +0000264 if (read_vbr(Buf, EndBuf, Val)) return true;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000265 V = ConstantUInt::get(Ty, Val);
Chris Lattner00950542001-06-06 20:29:01 +0000266 break;
267 }
268
269 case Type::SByteTyID: // Unsigned integer types...
270 case Type::ShortTyID:
271 case Type::IntTyID: {
272 int Val;
Chris Lattner74734132002-08-17 22:01:27 +0000273 if (read_vbr(Buf, EndBuf, Val)) return true;
274 if (!ConstantSInt::isValueValidForType(Ty, Val)) return true;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000275 V = ConstantSInt::get(Ty, Val);
Chris Lattner00950542001-06-06 20:29:01 +0000276 break;
277 }
278
279 case Type::LongTyID: {
280 int64_t Val;
Chris Lattner74734132002-08-17 22:01:27 +0000281 if (read_vbr(Buf, EndBuf, Val)) return true;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000282 V = ConstantSInt::get(Ty, Val);
Chris Lattner00950542001-06-06 20:29:01 +0000283 break;
284 }
285
Chris Lattnera1375302001-07-15 00:17:18 +0000286 case Type::FloatTyID: {
287 float F;
Chris Lattner74734132002-08-17 22:01:27 +0000288 if (input_data(Buf, EndBuf, &F, &F+1)) return true;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000289 V = ConstantFP::get(Ty, F);
Chris Lattnera1375302001-07-15 00:17:18 +0000290 break;
291 }
292
293 case Type::DoubleTyID: {
294 double Val;
Chris Lattner74734132002-08-17 22:01:27 +0000295 if (input_data(Buf, EndBuf, &Val, &Val+1)) return true;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000296 V = ConstantFP::get(Ty, Val);
Chris Lattnera1375302001-07-15 00:17:18 +0000297 break;
298 }
299
Chris Lattner00950542001-06-06 20:29:01 +0000300 case Type::TypeTyID:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000301 assert(0 && "Type constants should be handled seperately!!!");
302 abort();
Chris Lattner00950542001-06-06 20:29:01 +0000303
304 case Type::ArrayTyID: {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000305 const ArrayType *AT = cast<const ArrayType>(Ty);
Chris Lattner90865152001-12-14 16:30:51 +0000306 unsigned NumElements = AT->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +0000307
Chris Lattner697954c2002-01-20 22:54:45 +0000308 std::vector<Constant*> Elements;
Chris Lattner00950542001-06-06 20:29:01 +0000309 while (NumElements--) { // Read all of the elements of the constant.
310 unsigned Slot;
Chris Lattner74734132002-08-17 22:01:27 +0000311 if (read_vbr(Buf, EndBuf, Slot)) return true;
Chris Lattner00950542001-06-06 20:29:01 +0000312 Value *V = getValue(AT->getElementType(), Slot, false);
Chris Lattner74734132002-08-17 22:01:27 +0000313 if (!V || !isa<Constant>(V)) return true;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000314 Elements.push_back(cast<Constant>(V));
Chris Lattner00950542001-06-06 20:29:01 +0000315 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000316 V = ConstantArray::get(AT, Elements);
Chris Lattner00950542001-06-06 20:29:01 +0000317 break;
318 }
319
320 case Type::StructTyID: {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000321 const StructType *ST = cast<StructType>(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000322 const StructType::ElementTypes &ET = ST->getElementTypes();
323
Chris Lattner697954c2002-01-20 22:54:45 +0000324 std::vector<Constant *> Elements;
Chris Lattner00950542001-06-06 20:29:01 +0000325 for (unsigned i = 0; i < ET.size(); ++i) {
326 unsigned Slot;
Chris Lattner74734132002-08-17 22:01:27 +0000327 if (read_vbr(Buf, EndBuf, Slot)) return true;
Chris Lattner00950542001-06-06 20:29:01 +0000328 Value *V = getValue(ET[i], Slot, false);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000329 if (!V || !isa<Constant>(V))
Chris Lattner74734132002-08-17 22:01:27 +0000330 return true;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000331 Elements.push_back(cast<Constant>(V));
Chris Lattner00950542001-06-06 20:29:01 +0000332 }
333
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000334 V = ConstantStruct::get(ST, Elements);
Chris Lattner00950542001-06-06 20:29:01 +0000335 break;
336 }
337
Chris Lattner1a1cb112001-09-30 22:46:54 +0000338 case Type::PointerTyID: {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000339 const PointerType *PT = cast<const PointerType>(Ty);
Chris Lattner1a1cb112001-09-30 22:46:54 +0000340 unsigned SubClass;
Chris Lattner74734132002-08-17 22:01:27 +0000341 if (read_vbr(Buf, EndBuf, SubClass)) return true;
Chris Lattner05950c32001-10-13 06:47:01 +0000342 switch (SubClass) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000343 case 0: // ConstantPointerNull value...
344 V = ConstantPointerNull::get(PT);
Chris Lattner05950c32001-10-13 06:47:01 +0000345 break;
Chris Lattner1a1cb112001-09-30 22:46:54 +0000346
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000347 case 1: { // ConstantPointerRef value...
Chris Lattner05950c32001-10-13 06:47:01 +0000348 unsigned Slot;
Chris Lattner74734132002-08-17 22:01:27 +0000349 if (read_vbr(Buf, EndBuf, Slot)) return true;
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000350 BCR_TRACE(4, "CPR: Type: '" << Ty << "' slot: " << Slot << "\n");
Chris Lattner1a1cb112001-09-30 22:46:54 +0000351
Chris Lattner74734132002-08-17 22:01:27 +0000352 // Check to see if we have already read this global variable...
Chris Lattner05950c32001-10-13 06:47:01 +0000353 Value *Val = getValue(PT, Slot, false);
Chris Lattner74734132002-08-17 22:01:27 +0000354 GlobalValue *GV;
Chris Lattner05950c32001-10-13 06:47:01 +0000355 if (Val) {
Chris Lattner74734132002-08-17 22:01:27 +0000356 if (!(GV = dyn_cast<GlobalValue>(Val))) return true;
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000357 BCR_TRACE(5, "Value Found in ValueTable!\n");
358 } else { // Nope... find or create a forward ref. for it
Chris Lattner74734132002-08-17 22:01:27 +0000359 GlobalRefsType::iterator I = GlobalRefs.find(make_pair(PT, Slot));
360
361 if (I != GlobalRefs.end()) {
362 BCR_TRACE(5, "Previous forward ref found!\n");
363 GV = cast<GlobalValue>(I->second);
364 } else {
365 BCR_TRACE(5, "Creating new forward ref to a global variable!\n");
366
367 // Create a placeholder for the global variable reference...
368 GlobalVariable *GVar =
369 new GlobalVariable(PT->getElementType(), false, true);
370
371 // Keep track of the fact that we have a forward ref to recycle it
372 GlobalRefs.insert(make_pair(make_pair(PT, Slot), GVar));
373
374 // Must temporarily push this value into the module table...
375 TheModule->getGlobalList().push_back(GVar);
376 GV = GVar;
377 }
Chris Lattner05950c32001-10-13 06:47:01 +0000378 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000379 V = ConstantPointerRef::get(GV);
Chris Lattner05950c32001-10-13 06:47:01 +0000380 break;
381 }
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000382
Chris Lattner05950c32001-10-13 06:47:01 +0000383 default:
Chris Lattner90865152001-12-14 16:30:51 +0000384 BCR_TRACE(5, "UNKNOWN Pointer Constant Type!\n");
Chris Lattner74734132002-08-17 22:01:27 +0000385 return true;
Chris Lattner05950c32001-10-13 06:47:01 +0000386 }
Chris Lattner1a1cb112001-09-30 22:46:54 +0000387 break;
388 }
389
Chris Lattner00950542001-06-06 20:29:01 +0000390 default:
Anand Shuklaeea60fc2002-06-25 20:44:04 +0000391 std::cerr << __FILE__ << ":" << __LINE__
Chris Lattner194f0a52002-06-30 16:04:37 +0000392 << ": Don't know how to deserialize constant value of type '"
393 << Ty->getName() << "'\n";
Chris Lattner74734132002-08-17 22:01:27 +0000394 return true;
Chris Lattner00950542001-06-06 20:29:01 +0000395 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000396
Chris Lattner00950542001-06-06 20:29:01 +0000397 return false;
398}
399
400bool BytecodeParser::ParseConstantPool(const uchar *&Buf, const uchar *EndBuf,
Chris Lattner1d670cc2001-09-07 16:37:43 +0000401 ValueTable &Tab,
402 TypeValuesListTy &TypeTab) {
Chris Lattner00950542001-06-06 20:29:01 +0000403 while (Buf < EndBuf) {
404 unsigned NumEntries, Typ;
405
406 if (read_vbr(Buf, EndBuf, NumEntries) ||
Chris Lattner74734132002-08-17 22:01:27 +0000407 read_vbr(Buf, EndBuf, Typ)) return true;
Chris Lattner00950542001-06-06 20:29:01 +0000408 const Type *Ty = getType(Typ);
Chris Lattner74734132002-08-17 22:01:27 +0000409 if (Ty == 0) return true;
Chris Lattner1d670cc2001-09-07 16:37:43 +0000410 BCR_TRACE(3, "Type: '" << Ty << "' NumEntries: " << NumEntries << "\n");
Chris Lattner00950542001-06-06 20:29:01 +0000411
Chris Lattner1d670cc2001-09-07 16:37:43 +0000412 if (Typ == Type::TypeTyID) {
413 if (parseTypeConstants(Buf, EndBuf, TypeTab, NumEntries)) return true;
414 } else {
Chris Lattner7eadfa12001-10-24 06:21:22 +0000415 for (unsigned i = 0; i < NumEntries; ++i) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000416 Constant *I;
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000417 int Slot;
Chris Lattner74734132002-08-17 22:01:27 +0000418 if (parseConstantValue(Buf, EndBuf, Ty, I)) return true;
419 assert(I && "parseConstantValue returned NULL!");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000420 BCR_TRACE(4, "Read Constant: '" << I << "'\n");
Chris Lattner74734132002-08-17 22:01:27 +0000421 if ((Slot = insertValue(I, Tab)) < 0) return true;
422
423 // If we are reading a function constant table, make sure that we adjust
424 // the slot number to be the real global constant number.
425 //
426 if (&Tab != &ModuleValues)
427 Slot += ModuleValues[Typ].size();
428
429 ResolveReferencesToValue(I, (unsigned)Slot);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000430 }
Chris Lattner00950542001-06-06 20:29:01 +0000431 }
432 }
433
Chris Lattner74734132002-08-17 22:01:27 +0000434 if (Buf > EndBuf) return true;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000435 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000436}