blob: e71441edd8aeb83f23d5f777c05fca3d069e3f49 [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 Lattnerbbd4b302002-10-14 03:33:02 +0000205 Constant *C = getConstantValue(ArgTy, ArgValSlot);
206 if (C == 0) return true;
Chris Lattnere8e46052002-07-30 18:54:22 +0000207 ArgVec.push_back(C);
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000208 }
209
210 // Construct a ConstantExpr of the appropriate kind
211 if (isExprNumArgs == 1) { // All one-operand expressions
Chris Lattnerad333482002-08-14 18:24:09 +0000212 assert(Opcode == Instruction::Cast);
213 V = ConstantExpr::getCast(ArgVec[0], Ty);
Chris Lattnere8e46052002-07-30 18:54:22 +0000214 } else if (Opcode == Instruction::GetElementPtr) { // GetElementPtr
215 std::vector<Constant*> IdxList(ArgVec.begin()+1, ArgVec.end());
216 V = ConstantExpr::getGetElementPtr(ArgVec[0], IdxList);
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000217 } else { // All other 2-operand expressions
Chris Lattnere8e46052002-07-30 18:54:22 +0000218 V = ConstantExpr::get(Opcode, ArgVec[0], ArgVec[1]);
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000219 }
220 return false;
221 }
222
223 // Ok, not an ConstantExpr. We now know how to read the given type...
Chris Lattner00950542001-06-06 20:29:01 +0000224 switch (Ty->getPrimitiveID()) {
225 case Type::BoolTyID: {
226 unsigned Val;
Chris Lattner74734132002-08-17 22:01:27 +0000227 if (read_vbr(Buf, EndBuf, Val)) return true;
228 if (Val != 0 && Val != 1) return true;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000229 V = ConstantBool::get(Val == 1);
Chris Lattner00950542001-06-06 20:29:01 +0000230 break;
231 }
232
233 case Type::UByteTyID: // Unsigned integer types...
234 case Type::UShortTyID:
235 case Type::UIntTyID: {
236 unsigned Val;
Chris Lattner74734132002-08-17 22:01:27 +0000237 if (read_vbr(Buf, EndBuf, Val)) return true;
238 if (!ConstantUInt::isValueValidForType(Ty, Val)) return true;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000239 V = ConstantUInt::get(Ty, Val);
Chris Lattner00950542001-06-06 20:29:01 +0000240 break;
241 }
242
243 case Type::ULongTyID: {
244 uint64_t Val;
Chris Lattner74734132002-08-17 22:01:27 +0000245 if (read_vbr(Buf, EndBuf, Val)) return true;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000246 V = ConstantUInt::get(Ty, Val);
Chris Lattner00950542001-06-06 20:29:01 +0000247 break;
248 }
249
250 case Type::SByteTyID: // Unsigned integer types...
251 case Type::ShortTyID:
252 case Type::IntTyID: {
253 int Val;
Chris Lattner74734132002-08-17 22:01:27 +0000254 if (read_vbr(Buf, EndBuf, Val)) return true;
255 if (!ConstantSInt::isValueValidForType(Ty, Val)) return true;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000256 V = ConstantSInt::get(Ty, Val);
Chris Lattner00950542001-06-06 20:29:01 +0000257 break;
258 }
259
260 case Type::LongTyID: {
261 int64_t Val;
Chris Lattner74734132002-08-17 22:01:27 +0000262 if (read_vbr(Buf, EndBuf, Val)) return true;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000263 V = ConstantSInt::get(Ty, Val);
Chris Lattner00950542001-06-06 20:29:01 +0000264 break;
265 }
266
Chris Lattnera1375302001-07-15 00:17:18 +0000267 case Type::FloatTyID: {
268 float F;
Chris Lattner74734132002-08-17 22:01:27 +0000269 if (input_data(Buf, EndBuf, &F, &F+1)) return true;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000270 V = ConstantFP::get(Ty, F);
Chris Lattnera1375302001-07-15 00:17:18 +0000271 break;
272 }
273
274 case Type::DoubleTyID: {
275 double Val;
Chris Lattner74734132002-08-17 22:01:27 +0000276 if (input_data(Buf, EndBuf, &Val, &Val+1)) return true;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000277 V = ConstantFP::get(Ty, Val);
Chris Lattnera1375302001-07-15 00:17:18 +0000278 break;
279 }
280
Chris Lattner00950542001-06-06 20:29:01 +0000281 case Type::TypeTyID:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000282 assert(0 && "Type constants should be handled seperately!!!");
283 abort();
Chris Lattner00950542001-06-06 20:29:01 +0000284
285 case Type::ArrayTyID: {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000286 const ArrayType *AT = cast<const ArrayType>(Ty);
Chris Lattner90865152001-12-14 16:30:51 +0000287 unsigned NumElements = AT->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +0000288
Chris Lattner697954c2002-01-20 22:54:45 +0000289 std::vector<Constant*> Elements;
Chris Lattner00950542001-06-06 20:29:01 +0000290 while (NumElements--) { // Read all of the elements of the constant.
291 unsigned Slot;
Chris Lattner74734132002-08-17 22:01:27 +0000292 if (read_vbr(Buf, EndBuf, Slot)) return true;
Chris Lattnerbbd4b302002-10-14 03:33:02 +0000293 Constant *C = getConstantValue(AT->getElementType(), Slot);
294 if (!C) return true;
295 Elements.push_back(C);
Chris Lattner00950542001-06-06 20:29:01 +0000296 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000297 V = ConstantArray::get(AT, Elements);
Chris Lattner00950542001-06-06 20:29:01 +0000298 break;
299 }
300
301 case Type::StructTyID: {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000302 const StructType *ST = cast<StructType>(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000303 const StructType::ElementTypes &ET = ST->getElementTypes();
304
Chris Lattner697954c2002-01-20 22:54:45 +0000305 std::vector<Constant *> Elements;
Chris Lattner00950542001-06-06 20:29:01 +0000306 for (unsigned i = 0; i < ET.size(); ++i) {
307 unsigned Slot;
Chris Lattner74734132002-08-17 22:01:27 +0000308 if (read_vbr(Buf, EndBuf, Slot)) return true;
Chris Lattnerbbd4b302002-10-14 03:33:02 +0000309 Constant *C = getConstantValue(ET[i], Slot);
310 if (!C) return true;
311 Elements.push_back(C);
Chris Lattner00950542001-06-06 20:29:01 +0000312 }
313
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000314 V = ConstantStruct::get(ST, Elements);
Chris Lattner00950542001-06-06 20:29:01 +0000315 break;
316 }
317
Chris Lattner1a1cb112001-09-30 22:46:54 +0000318 case Type::PointerTyID: {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000319 const PointerType *PT = cast<const PointerType>(Ty);
Chris Lattner1a1cb112001-09-30 22:46:54 +0000320 unsigned SubClass;
Chris Lattner74734132002-08-17 22:01:27 +0000321 if (read_vbr(Buf, EndBuf, SubClass)) return true;
Chris Lattner05950c32001-10-13 06:47:01 +0000322 switch (SubClass) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000323 case 0: // ConstantPointerNull value...
324 V = ConstantPointerNull::get(PT);
Chris Lattner05950c32001-10-13 06:47:01 +0000325 break;
Chris Lattner1a1cb112001-09-30 22:46:54 +0000326
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000327 case 1: { // ConstantPointerRef value...
Chris Lattner05950c32001-10-13 06:47:01 +0000328 unsigned Slot;
Chris Lattner74734132002-08-17 22:01:27 +0000329 if (read_vbr(Buf, EndBuf, Slot)) return true;
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000330 BCR_TRACE(4, "CPR: Type: '" << Ty << "' slot: " << Slot << "\n");
Chris Lattner1a1cb112001-09-30 22:46:54 +0000331
Chris Lattner74734132002-08-17 22:01:27 +0000332 // Check to see if we have already read this global variable...
Chris Lattner05950c32001-10-13 06:47:01 +0000333 Value *Val = getValue(PT, Slot, false);
Chris Lattner74734132002-08-17 22:01:27 +0000334 GlobalValue *GV;
Chris Lattner05950c32001-10-13 06:47:01 +0000335 if (Val) {
Chris Lattner74734132002-08-17 22:01:27 +0000336 if (!(GV = dyn_cast<GlobalValue>(Val))) return true;
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000337 BCR_TRACE(5, "Value Found in ValueTable!\n");
338 } else { // Nope... find or create a forward ref. for it
Chris Lattner74734132002-08-17 22:01:27 +0000339 GlobalRefsType::iterator I = GlobalRefs.find(make_pair(PT, Slot));
340
341 if (I != GlobalRefs.end()) {
342 BCR_TRACE(5, "Previous forward ref found!\n");
343 GV = cast<GlobalValue>(I->second);
344 } else {
345 BCR_TRACE(5, "Creating new forward ref to a global variable!\n");
346
347 // Create a placeholder for the global variable reference...
348 GlobalVariable *GVar =
349 new GlobalVariable(PT->getElementType(), false, true);
350
351 // Keep track of the fact that we have a forward ref to recycle it
352 GlobalRefs.insert(make_pair(make_pair(PT, Slot), GVar));
353
354 // Must temporarily push this value into the module table...
355 TheModule->getGlobalList().push_back(GVar);
356 GV = GVar;
357 }
Chris Lattner05950c32001-10-13 06:47:01 +0000358 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000359 V = ConstantPointerRef::get(GV);
Chris Lattner05950c32001-10-13 06:47:01 +0000360 break;
361 }
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000362
Chris Lattner05950c32001-10-13 06:47:01 +0000363 default:
Chris Lattner90865152001-12-14 16:30:51 +0000364 BCR_TRACE(5, "UNKNOWN Pointer Constant Type!\n");
Chris Lattner74734132002-08-17 22:01:27 +0000365 return true;
Chris Lattner05950c32001-10-13 06:47:01 +0000366 }
Chris Lattner1a1cb112001-09-30 22:46:54 +0000367 break;
368 }
369
Chris Lattner00950542001-06-06 20:29:01 +0000370 default:
Anand Shuklaeea60fc2002-06-25 20:44:04 +0000371 std::cerr << __FILE__ << ":" << __LINE__
Chris Lattner194f0a52002-06-30 16:04:37 +0000372 << ": Don't know how to deserialize constant value of type '"
373 << Ty->getName() << "'\n";
Chris Lattner74734132002-08-17 22:01:27 +0000374 return true;
Chris Lattner00950542001-06-06 20:29:01 +0000375 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000376
Chris Lattner00950542001-06-06 20:29:01 +0000377 return false;
378}
379
380bool BytecodeParser::ParseConstantPool(const uchar *&Buf, const uchar *EndBuf,
Chris Lattner1d670cc2001-09-07 16:37:43 +0000381 ValueTable &Tab,
382 TypeValuesListTy &TypeTab) {
Chris Lattner00950542001-06-06 20:29:01 +0000383 while (Buf < EndBuf) {
384 unsigned NumEntries, Typ;
385
386 if (read_vbr(Buf, EndBuf, NumEntries) ||
Chris Lattner74734132002-08-17 22:01:27 +0000387 read_vbr(Buf, EndBuf, Typ)) return true;
Chris Lattner00950542001-06-06 20:29:01 +0000388 const Type *Ty = getType(Typ);
Chris Lattner74734132002-08-17 22:01:27 +0000389 if (Ty == 0) return true;
Chris Lattner1d670cc2001-09-07 16:37:43 +0000390 BCR_TRACE(3, "Type: '" << Ty << "' NumEntries: " << NumEntries << "\n");
Chris Lattner00950542001-06-06 20:29:01 +0000391
Chris Lattner1d670cc2001-09-07 16:37:43 +0000392 if (Typ == Type::TypeTyID) {
393 if (parseTypeConstants(Buf, EndBuf, TypeTab, NumEntries)) return true;
394 } else {
Chris Lattner7eadfa12001-10-24 06:21:22 +0000395 for (unsigned i = 0; i < NumEntries; ++i) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000396 Constant *I;
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000397 int Slot;
Chris Lattner74734132002-08-17 22:01:27 +0000398 if (parseConstantValue(Buf, EndBuf, Ty, I)) return true;
399 assert(I && "parseConstantValue returned NULL!");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000400 BCR_TRACE(4, "Read Constant: '" << I << "'\n");
Chris Lattner74734132002-08-17 22:01:27 +0000401 if ((Slot = insertValue(I, Tab)) < 0) return true;
402
403 // If we are reading a function constant table, make sure that we adjust
404 // the slot number to be the real global constant number.
405 //
406 if (&Tab != &ModuleValues)
407 Slot += ModuleValues[Typ].size();
408
409 ResolveReferencesToValue(I, (unsigned)Slot);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000410 }
Chris Lattner00950542001-06-06 20:29:01 +0000411 }
412 }
413
Chris Lattner74734132002-08-17 22:01:27 +0000414 if (Buf > EndBuf) return true;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000415 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000416}