blob: 88bfa3444a240596b9d1d3177ff1107555a1944a [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 Lattner1d670cc2001-09-07 16:37:43 +000013#include <algorithm>
Anand Shuklaeea60fc2002-06-25 20:44:04 +000014
Chris Lattner697954c2002-01-20 22:54:45 +000015using std::make_pair;
Chris Lattner1d670cc2001-09-07 16:37:43 +000016
17const Type *BytecodeParser::parseTypeConstant(const uchar *&Buf,
18 const uchar *EndBuf) {
Chris Lattner00950542001-06-06 20:29:01 +000019 unsigned PrimType;
Chris Lattner74734132002-08-17 22:01:27 +000020 if (read_vbr(Buf, EndBuf, PrimType)) return 0;
Chris Lattner00950542001-06-06 20:29:01 +000021
Chris Lattner1d670cc2001-09-07 16:37:43 +000022 const Type *Val = 0;
23 if ((Val = Type::getPrimitiveType((Type::PrimitiveID)PrimType)))
24 return Val;
Chris Lattner00950542001-06-06 20:29:01 +000025
26 switch (PrimType) {
Chris Lattnerc9aa7df2002-03-29 03:51:11 +000027 case Type::FunctionTyID: {
Chris Lattner00950542001-06-06 20:29:01 +000028 unsigned Typ;
Chris Lattner74734132002-08-17 22:01:27 +000029 if (read_vbr(Buf, EndBuf, Typ)) return Val;
Chris Lattner00950542001-06-06 20:29:01 +000030 const Type *RetType = getType(Typ);
Chris Lattner74734132002-08-17 22:01:27 +000031 if (RetType == 0) return Val;
Chris Lattner00950542001-06-06 20:29:01 +000032
Chris Lattnere5a57ee2001-07-25 22:47:55 +000033 unsigned NumParams;
Chris Lattner74734132002-08-17 22:01:27 +000034 if (read_vbr(Buf, EndBuf, NumParams)) return Val;
Chris Lattner00950542001-06-06 20:29:01 +000035
Chris Lattner697954c2002-01-20 22:54:45 +000036 std::vector<const Type*> Params;
Chris Lattnere5a57ee2001-07-25 22:47:55 +000037 while (NumParams--) {
Chris Lattner74734132002-08-17 22:01:27 +000038 if (read_vbr(Buf, EndBuf, Typ)) return Val;
Chris Lattner00950542001-06-06 20:29:01 +000039 const Type *Ty = getType(Typ);
Chris Lattner74734132002-08-17 22:01:27 +000040 if (Ty == 0) return Val;
Chris Lattner00950542001-06-06 20:29:01 +000041 Params.push_back(Ty);
Chris Lattner00950542001-06-06 20:29:01 +000042 }
43
Chris Lattner05950c32001-10-13 06:47:01 +000044 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
45 if (isVarArg) Params.pop_back();
46
Chris Lattnerc9aa7df2002-03-29 03:51:11 +000047 return FunctionType::get(RetType, Params, isVarArg);
Chris Lattner00950542001-06-06 20:29:01 +000048 }
49 case Type::ArrayTyID: {
50 unsigned ElTyp;
Chris Lattner74734132002-08-17 22:01:27 +000051 if (read_vbr(Buf, EndBuf, ElTyp)) return Val;
Chris Lattner00950542001-06-06 20:29:01 +000052 const Type *ElementType = getType(ElTyp);
Chris Lattner74734132002-08-17 22:01:27 +000053 if (ElementType == 0) return Val;
Chris Lattner00950542001-06-06 20:29:01 +000054
Chris Lattner90865152001-12-14 16:30:51 +000055 unsigned NumElements;
Chris Lattner74734132002-08-17 22:01:27 +000056 if (read_vbr(Buf, EndBuf, NumElements)) return Val;
Chris Lattner7eadfa12001-10-24 06:21:22 +000057
58 BCR_TRACE(5, "Array Type Constant #" << ElTyp << " size="
Chris Lattner697954c2002-01-20 22:54:45 +000059 << NumElements << "\n");
Chris Lattner7eadfa12001-10-24 06:21:22 +000060 return ArrayType::get(ElementType, NumElements);
Chris Lattner00950542001-06-06 20:29:01 +000061 }
62 case Type::StructTyID: {
63 unsigned Typ;
Chris Lattner697954c2002-01-20 22:54:45 +000064 std::vector<const Type*> Elements;
Chris Lattner00950542001-06-06 20:29:01 +000065
Chris Lattner74734132002-08-17 22:01:27 +000066 if (read_vbr(Buf, EndBuf, Typ)) return Val;
Chris Lattner00950542001-06-06 20:29:01 +000067 while (Typ) { // List is terminated by void/0 typeid
68 const Type *Ty = getType(Typ);
Chris Lattner74734132002-08-17 22:01:27 +000069 if (Ty == 0) return Val;
Chris Lattner00950542001-06-06 20:29:01 +000070 Elements.push_back(Ty);
71
Chris Lattner74734132002-08-17 22:01:27 +000072 if (read_vbr(Buf, EndBuf, Typ)) return Val;
Chris Lattner00950542001-06-06 20:29:01 +000073 }
74
Chris Lattner7eadfa12001-10-24 06:21:22 +000075 return StructType::get(Elements);
Chris Lattner00950542001-06-06 20:29:01 +000076 }
77 case Type::PointerTyID: {
78 unsigned ElTyp;
Chris Lattner74734132002-08-17 22:01:27 +000079 if (read_vbr(Buf, EndBuf, ElTyp)) return Val;
Chris Lattner697954c2002-01-20 22:54:45 +000080 BCR_TRACE(5, "Pointer Type Constant #" << (ElTyp-14) << "\n");
Chris Lattner00950542001-06-06 20:29:01 +000081 const Type *ElementType = getType(ElTyp);
Chris Lattner74734132002-08-17 22:01:27 +000082 if (ElementType == 0) return Val;
Chris Lattner7eadfa12001-10-24 06:21:22 +000083 return PointerType::get(ElementType);
Chris Lattner00950542001-06-06 20:29:01 +000084 }
85
Chris Lattnerd48d6c72001-10-23 01:53:01 +000086 case Type::OpaqueTyID: {
Chris Lattner7eadfa12001-10-24 06:21:22 +000087 return OpaqueType::get();
Chris Lattnerd48d6c72001-10-23 01:53:01 +000088 }
89
Chris Lattner00950542001-06-06 20:29:01 +000090 default:
Anand Shuklaeea60fc2002-06-25 20:44:04 +000091 std::cerr << __FILE__ << ":" << __LINE__
Chris Lattner194f0a52002-06-30 16:04:37 +000092 << ": Don't know how to deserialize"
93 << " primitive Type " << PrimType << "\n";
Chris Lattner74734132002-08-17 22:01:27 +000094 return Val;
Chris Lattner00950542001-06-06 20:29:01 +000095 }
Chris Lattner1d670cc2001-09-07 16:37:43 +000096}
97
98// refineAbstractType - The callback method is invoked when one of the
99// elements of TypeValues becomes more concrete...
100//
101void BytecodeParser::refineAbstractType(const DerivedType *OldType,
102 const Type *NewType) {
Chris Lattnere244a252001-11-03 03:27:53 +0000103 if (OldType == NewType &&
104 OldType->isAbstract()) return; // Type is modified, but same
Chris Lattner05950c32001-10-13 06:47:01 +0000105
Chris Lattner1d670cc2001-09-07 16:37:43 +0000106 TypeValuesListTy::iterator I = find(MethodTypeValues.begin(),
107 MethodTypeValues.end(), OldType);
108 if (I == MethodTypeValues.end()) {
109 I = find(ModuleTypeValues.begin(), ModuleTypeValues.end(), OldType);
110 assert(I != ModuleTypeValues.end() &&
111 "Can't refine a type I don't know about!");
112 }
113
Chris Lattnere244a252001-11-03 03:27:53 +0000114 if (OldType == NewType) {
115 assert(!OldType->isAbstract());
116 I->removeUserFromConcrete();
117 } else {
118 *I = NewType; // Update to point to new, more refined type.
119 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000120}
121
122
123
124// parseTypeConstants - We have to use this wierd code to handle recursive
125// types. We know that recursive types will only reference the current slab of
126// values in the type plane, but they can forward reference types before they
127// have been read. For example, Type #0 might be '{ Ty#1 }' and Type #1 might
128// be 'Ty#0*'. When reading Type #0, type number one doesn't exist. To fix
129// this ugly problem, we pesimistically insert an opaque type for each type we
130// are about to read. This means that forward references will resolve to
131// something and when we reread the type later, we can replace the opaque type
132// with a new resolved concrete type.
133//
Chris Lattner92940ac2002-04-07 06:11:22 +0000134void debug_type_tables();
Chris Lattner1d670cc2001-09-07 16:37:43 +0000135bool BytecodeParser::parseTypeConstants(const uchar *&Buf, const uchar *EndBuf,
136 TypeValuesListTy &Tab,
137 unsigned NumEntries) {
Chris Lattner05950c32001-10-13 06:47:01 +0000138 assert(Tab.size() == 0 && "should not have read type constants in before!");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000139
140 // Insert a bunch of opaque types to be resolved later...
Chris Lattner7eadfa12001-10-24 06:21:22 +0000141 for (unsigned i = 0; i < NumEntries; ++i)
Chris Lattner1d670cc2001-09-07 16:37:43 +0000142 Tab.push_back(PATypeHandle<Type>(OpaqueType::get(), this));
143
144 // Loop through reading all of the types. Forward types will make use of the
145 // opaque types just inserted.
146 //
Chris Lattner7eadfa12001-10-24 06:21:22 +0000147 for (unsigned i = 0; i < NumEntries; ++i) {
Chris Lattner05950c32001-10-13 06:47:01 +0000148 const Type *NewTy = parseTypeConstant(Buf, EndBuf), *OldTy = Tab[i].get();
Chris Lattner74734132002-08-17 22:01:27 +0000149 if (NewTy == 0) return true;
Chris Lattner7eadfa12001-10-24 06:21:22 +0000150 BCR_TRACE(4, "#" << i << ": Read Type Constant: '" << NewTy <<
151 "' Replacing: " << OldTy << "\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000152
153 // Don't insertValue the new type... instead we want to replace the opaque
154 // type with the new concrete value...
155 //
156
157 // Refine the abstract type to the new type. This causes all uses of the
158 // abstract type to use the newty. This also will cause the opaque type
159 // to be deleted...
160 //
Chris Lattnera48836b2002-06-05 17:38:28 +0000161 ((DerivedType*)Tab[i].get())->refineAbstractTypeTo(NewTy);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000162
163 // This should have replace the old opaque type with the new type in the
Chris Lattner05950c32001-10-13 06:47:01 +0000164 // value table... or with a preexisting type that was already in the system
165 assert(Tab[i] != OldTy && "refineAbstractType didn't work!");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000166 }
167
168 BCR_TRACE(5, "Resulting types:\n");
Chris Lattner7eadfa12001-10-24 06:21:22 +0000169 for (unsigned i = 0; i < NumEntries; ++i) {
Chris Lattner92940ac2002-04-07 06:11:22 +0000170 BCR_TRACE(5, (void*)Tab[i].get() << " - " << Tab[i].get() << "\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000171 }
Chris Lattner92940ac2002-04-07 06:11:22 +0000172 debug_type_tables();
Chris Lattner00950542001-06-06 20:29:01 +0000173 return false;
174}
175
Chris Lattner1d670cc2001-09-07 16:37:43 +0000176
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000177bool BytecodeParser::parseConstantValue(const uchar *&Buf, const uchar *EndBuf,
178 const Type *Ty, Constant *&V) {
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000179
180 // We must check for a ConstantExpr before switching by type because
181 // a ConstantExpr can be of any type, and has no explicit value.
182 //
183 unsigned isExprNumArgs; // 0 if not expr; numArgs if is expr
Chris Lattner74734132002-08-17 22:01:27 +0000184 if (read_vbr(Buf, EndBuf, isExprNumArgs)) return true;
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000185 if (isExprNumArgs) {
Chris Lattnere8e46052002-07-30 18:54:22 +0000186 // FIXME: Encoding of constant exprs could be much more compact!
187 unsigned Opcode;
188 std::vector<Constant*> ArgVec;
189 ArgVec.reserve(isExprNumArgs);
Chris Lattner74734132002-08-17 22:01:27 +0000190 if (read_vbr(Buf, EndBuf, Opcode)) return true;
Chris Lattnere8e46052002-07-30 18:54:22 +0000191
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000192 // Read the slot number and types of each of the arguments
Chris Lattnere8e46052002-07-30 18:54:22 +0000193 for (unsigned i = 0; i != isExprNumArgs; ++i) {
194 unsigned ArgValSlot, ArgTypeSlot;
Chris Lattner74734132002-08-17 22:01:27 +0000195 if (read_vbr(Buf, EndBuf, ArgValSlot)) return true;
196 if (read_vbr(Buf, EndBuf, ArgTypeSlot)) return true;
Chris Lattnere8e46052002-07-30 18:54:22 +0000197 const Type *ArgTy = getType(ArgTypeSlot);
Chris Lattner74734132002-08-17 22:01:27 +0000198 if (ArgTy == 0) return true;
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000199
Chris Lattnere8e46052002-07-30 18:54:22 +0000200 BCR_TRACE(4, "CE Arg " << i << ": Type: '" << ArgTy << "' slot: "
201 << ArgValSlot << "\n");
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000202
203 // Get the arg value from its slot if it exists, otherwise a placeholder
Chris Lattnere8e46052002-07-30 18:54:22 +0000204 Value *Val = getValue(ArgTy, ArgValSlot, false);
Chris Lattnercc4b6ec2002-07-18 00:14:27 +0000205 Constant *C;
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000206 if (Val) {
Chris Lattner74734132002-08-17 22:01:27 +0000207 if (!(C = dyn_cast<Constant>(Val))) return true;
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000208 BCR_TRACE(5, "Constant Found in ValueTable!\n");
209 } else { // Nope... find or create a forward ref. for it
Chris Lattner74734132002-08-17 22:01:27 +0000210 GlobalRefsType::iterator I = GlobalRefs.find(make_pair(Ty, ArgValSlot));
211
212 if (I != GlobalRefs.end()) {
213 BCR_TRACE(5, "Previous forward ref found!\n");
214 C = cast<Constant>(I->second);
215 } else {
216 // Create a placeholder for the constant reference and
217 // keep track of the fact that we have a forward ref to recycle it
218 BCR_TRACE(5, "Creating new forward ref to a constant!\n");
219 C = new ConstPHolder(ArgTy, ArgValSlot);
220
221 // Keep track of the fact that we have a forward ref to recycle it
222 GlobalRefs.insert(make_pair(make_pair(ArgTy, ArgValSlot), C));
223 }
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000224 }
Chris Lattnere8e46052002-07-30 18:54:22 +0000225 ArgVec.push_back(C);
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000226 }
227
228 // Construct a ConstantExpr of the appropriate kind
229 if (isExprNumArgs == 1) { // All one-operand expressions
Chris Lattnerad333482002-08-14 18:24:09 +0000230 assert(Opcode == Instruction::Cast);
231 V = ConstantExpr::getCast(ArgVec[0], Ty);
Chris Lattnere8e46052002-07-30 18:54:22 +0000232 } else if (Opcode == Instruction::GetElementPtr) { // GetElementPtr
233 std::vector<Constant*> IdxList(ArgVec.begin()+1, ArgVec.end());
234 V = ConstantExpr::getGetElementPtr(ArgVec[0], IdxList);
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000235 } else { // All other 2-operand expressions
Chris Lattnere8e46052002-07-30 18:54:22 +0000236 V = ConstantExpr::get(Opcode, ArgVec[0], ArgVec[1]);
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000237 }
238 return false;
239 }
240
241 // Ok, not an ConstantExpr. We now know how to read the given type...
Chris Lattner00950542001-06-06 20:29:01 +0000242 switch (Ty->getPrimitiveID()) {
243 case Type::BoolTyID: {
244 unsigned Val;
Chris Lattner74734132002-08-17 22:01:27 +0000245 if (read_vbr(Buf, EndBuf, Val)) return true;
246 if (Val != 0 && Val != 1) return true;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000247 V = ConstantBool::get(Val == 1);
Chris Lattner00950542001-06-06 20:29:01 +0000248 break;
249 }
250
251 case Type::UByteTyID: // Unsigned integer types...
252 case Type::UShortTyID:
253 case Type::UIntTyID: {
254 unsigned Val;
Chris Lattner74734132002-08-17 22:01:27 +0000255 if (read_vbr(Buf, EndBuf, Val)) return true;
256 if (!ConstantUInt::isValueValidForType(Ty, Val)) return true;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000257 V = ConstantUInt::get(Ty, Val);
Chris Lattner00950542001-06-06 20:29:01 +0000258 break;
259 }
260
261 case Type::ULongTyID: {
262 uint64_t Val;
Chris Lattner74734132002-08-17 22:01:27 +0000263 if (read_vbr(Buf, EndBuf, Val)) return true;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000264 V = ConstantUInt::get(Ty, Val);
Chris Lattner00950542001-06-06 20:29:01 +0000265 break;
266 }
267
268 case Type::SByteTyID: // Unsigned integer types...
269 case Type::ShortTyID:
270 case Type::IntTyID: {
271 int Val;
Chris Lattner74734132002-08-17 22:01:27 +0000272 if (read_vbr(Buf, EndBuf, Val)) return true;
273 if (!ConstantSInt::isValueValidForType(Ty, Val)) return true;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000274 V = ConstantSInt::get(Ty, Val);
Chris Lattner00950542001-06-06 20:29:01 +0000275 break;
276 }
277
278 case Type::LongTyID: {
279 int64_t Val;
Chris Lattner74734132002-08-17 22:01:27 +0000280 if (read_vbr(Buf, EndBuf, Val)) return true;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000281 V = ConstantSInt::get(Ty, Val);
Chris Lattner00950542001-06-06 20:29:01 +0000282 break;
283 }
284
Chris Lattnera1375302001-07-15 00:17:18 +0000285 case Type::FloatTyID: {
286 float F;
Chris Lattner74734132002-08-17 22:01:27 +0000287 if (input_data(Buf, EndBuf, &F, &F+1)) return true;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000288 V = ConstantFP::get(Ty, F);
Chris Lattnera1375302001-07-15 00:17:18 +0000289 break;
290 }
291
292 case Type::DoubleTyID: {
293 double Val;
Chris Lattner74734132002-08-17 22:01:27 +0000294 if (input_data(Buf, EndBuf, &Val, &Val+1)) return true;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000295 V = ConstantFP::get(Ty, Val);
Chris Lattnera1375302001-07-15 00:17:18 +0000296 break;
297 }
298
Chris Lattner00950542001-06-06 20:29:01 +0000299 case Type::TypeTyID:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000300 assert(0 && "Type constants should be handled seperately!!!");
301 abort();
Chris Lattner00950542001-06-06 20:29:01 +0000302
303 case Type::ArrayTyID: {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000304 const ArrayType *AT = cast<const ArrayType>(Ty);
Chris Lattner90865152001-12-14 16:30:51 +0000305 unsigned NumElements = AT->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +0000306
Chris Lattner697954c2002-01-20 22:54:45 +0000307 std::vector<Constant*> Elements;
Chris Lattner00950542001-06-06 20:29:01 +0000308 while (NumElements--) { // Read all of the elements of the constant.
309 unsigned Slot;
Chris Lattner74734132002-08-17 22:01:27 +0000310 if (read_vbr(Buf, EndBuf, Slot)) return true;
Chris Lattner00950542001-06-06 20:29:01 +0000311 Value *V = getValue(AT->getElementType(), Slot, false);
Chris Lattner74734132002-08-17 22:01:27 +0000312 if (!V || !isa<Constant>(V)) return true;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000313 Elements.push_back(cast<Constant>(V));
Chris Lattner00950542001-06-06 20:29:01 +0000314 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000315 V = ConstantArray::get(AT, Elements);
Chris Lattner00950542001-06-06 20:29:01 +0000316 break;
317 }
318
319 case Type::StructTyID: {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000320 const StructType *ST = cast<StructType>(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000321 const StructType::ElementTypes &ET = ST->getElementTypes();
322
Chris Lattner697954c2002-01-20 22:54:45 +0000323 std::vector<Constant *> Elements;
Chris Lattner00950542001-06-06 20:29:01 +0000324 for (unsigned i = 0; i < ET.size(); ++i) {
325 unsigned Slot;
Chris Lattner74734132002-08-17 22:01:27 +0000326 if (read_vbr(Buf, EndBuf, Slot)) return true;
Chris Lattner00950542001-06-06 20:29:01 +0000327 Value *V = getValue(ET[i], Slot, false);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000328 if (!V || !isa<Constant>(V))
Chris Lattner74734132002-08-17 22:01:27 +0000329 return true;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000330 Elements.push_back(cast<Constant>(V));
Chris Lattner00950542001-06-06 20:29:01 +0000331 }
332
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000333 V = ConstantStruct::get(ST, Elements);
Chris Lattner00950542001-06-06 20:29:01 +0000334 break;
335 }
336
Chris Lattner1a1cb112001-09-30 22:46:54 +0000337 case Type::PointerTyID: {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000338 const PointerType *PT = cast<const PointerType>(Ty);
Chris Lattner1a1cb112001-09-30 22:46:54 +0000339 unsigned SubClass;
Chris Lattner74734132002-08-17 22:01:27 +0000340 if (read_vbr(Buf, EndBuf, SubClass)) return true;
Chris Lattner05950c32001-10-13 06:47:01 +0000341 switch (SubClass) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000342 case 0: // ConstantPointerNull value...
343 V = ConstantPointerNull::get(PT);
Chris Lattner05950c32001-10-13 06:47:01 +0000344 break;
Chris Lattner1a1cb112001-09-30 22:46:54 +0000345
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000346 case 1: { // ConstantPointerRef value...
Chris Lattner05950c32001-10-13 06:47:01 +0000347 unsigned Slot;
Chris Lattner74734132002-08-17 22:01:27 +0000348 if (read_vbr(Buf, EndBuf, Slot)) return true;
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000349 BCR_TRACE(4, "CPR: Type: '" << Ty << "' slot: " << Slot << "\n");
Chris Lattner1a1cb112001-09-30 22:46:54 +0000350
Chris Lattner74734132002-08-17 22:01:27 +0000351 // Check to see if we have already read this global variable...
Chris Lattner05950c32001-10-13 06:47:01 +0000352 Value *Val = getValue(PT, Slot, false);
Chris Lattner74734132002-08-17 22:01:27 +0000353 GlobalValue *GV;
Chris Lattner05950c32001-10-13 06:47:01 +0000354 if (Val) {
Chris Lattner74734132002-08-17 22:01:27 +0000355 if (!(GV = dyn_cast<GlobalValue>(Val))) return true;
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000356 BCR_TRACE(5, "Value Found in ValueTable!\n");
357 } else { // Nope... find or create a forward ref. for it
Chris Lattner74734132002-08-17 22:01:27 +0000358 GlobalRefsType::iterator I = GlobalRefs.find(make_pair(PT, Slot));
359
360 if (I != GlobalRefs.end()) {
361 BCR_TRACE(5, "Previous forward ref found!\n");
362 GV = cast<GlobalValue>(I->second);
363 } else {
364 BCR_TRACE(5, "Creating new forward ref to a global variable!\n");
365
366 // Create a placeholder for the global variable reference...
367 GlobalVariable *GVar =
368 new GlobalVariable(PT->getElementType(), false, true);
369
370 // Keep track of the fact that we have a forward ref to recycle it
371 GlobalRefs.insert(make_pair(make_pair(PT, Slot), GVar));
372
373 // Must temporarily push this value into the module table...
374 TheModule->getGlobalList().push_back(GVar);
375 GV = GVar;
376 }
Chris Lattner05950c32001-10-13 06:47:01 +0000377 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000378 V = ConstantPointerRef::get(GV);
Chris Lattner05950c32001-10-13 06:47:01 +0000379 break;
380 }
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000381
Chris Lattner05950c32001-10-13 06:47:01 +0000382 default:
Chris Lattner90865152001-12-14 16:30:51 +0000383 BCR_TRACE(5, "UNKNOWN Pointer Constant Type!\n");
Chris Lattner74734132002-08-17 22:01:27 +0000384 return true;
Chris Lattner05950c32001-10-13 06:47:01 +0000385 }
Chris Lattner1a1cb112001-09-30 22:46:54 +0000386 break;
387 }
388
Chris Lattner00950542001-06-06 20:29:01 +0000389 default:
Anand Shuklaeea60fc2002-06-25 20:44:04 +0000390 std::cerr << __FILE__ << ":" << __LINE__
Chris Lattner194f0a52002-06-30 16:04:37 +0000391 << ": Don't know how to deserialize constant value of type '"
392 << Ty->getName() << "'\n";
Chris Lattner74734132002-08-17 22:01:27 +0000393 return true;
Chris Lattner00950542001-06-06 20:29:01 +0000394 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000395
Chris Lattner00950542001-06-06 20:29:01 +0000396 return false;
397}
398
399bool BytecodeParser::ParseConstantPool(const uchar *&Buf, const uchar *EndBuf,
Chris Lattner1d670cc2001-09-07 16:37:43 +0000400 ValueTable &Tab,
401 TypeValuesListTy &TypeTab) {
Chris Lattner00950542001-06-06 20:29:01 +0000402 while (Buf < EndBuf) {
403 unsigned NumEntries, Typ;
404
405 if (read_vbr(Buf, EndBuf, NumEntries) ||
Chris Lattner74734132002-08-17 22:01:27 +0000406 read_vbr(Buf, EndBuf, Typ)) return true;
Chris Lattner00950542001-06-06 20:29:01 +0000407 const Type *Ty = getType(Typ);
Chris Lattner74734132002-08-17 22:01:27 +0000408 if (Ty == 0) return true;
Chris Lattner1d670cc2001-09-07 16:37:43 +0000409 BCR_TRACE(3, "Type: '" << Ty << "' NumEntries: " << NumEntries << "\n");
Chris Lattner00950542001-06-06 20:29:01 +0000410
Chris Lattner1d670cc2001-09-07 16:37:43 +0000411 if (Typ == Type::TypeTyID) {
412 if (parseTypeConstants(Buf, EndBuf, TypeTab, NumEntries)) return true;
413 } else {
Chris Lattner7eadfa12001-10-24 06:21:22 +0000414 for (unsigned i = 0; i < NumEntries; ++i) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000415 Constant *I;
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000416 int Slot;
Chris Lattner74734132002-08-17 22:01:27 +0000417 if (parseConstantValue(Buf, EndBuf, Ty, I)) return true;
418 assert(I && "parseConstantValue returned NULL!");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000419 BCR_TRACE(4, "Read Constant: '" << I << "'\n");
Chris Lattner74734132002-08-17 22:01:27 +0000420 if ((Slot = insertValue(I, Tab)) < 0) return true;
421
422 // If we are reading a function constant table, make sure that we adjust
423 // the slot number to be the real global constant number.
424 //
425 if (&Tab != &ModuleValues)
426 Slot += ModuleValues[Typ].size();
427
428 ResolveReferencesToValue(I, (unsigned)Slot);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000429 }
Chris Lattner00950542001-06-06 20:29:01 +0000430 }
431 }
432
Chris Lattner74734132002-08-17 22:01:27 +0000433 if (Buf > EndBuf) return true;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000434 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000435}