blob: 91baa566d7f1ac9742a0377b43d48962791d9c08 [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
Misha Brukman37f92e22003-09-11 22:34:13 +00007// thread-safe!!
Chris Lattner00950542001-06-06 20:29:01 +00008//
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 Lattner12e64652003-05-22 18:08:30 +000016const Type *BytecodeParser::parseTypeConstant(const unsigned char *&Buf,
17 const unsigned char *EndBuf) {
Chris Lattner00950542001-06-06 20:29:01 +000018 unsigned PrimType;
Misha Brukman12c29d12003-09-22 23:38:23 +000019 if (read_vbr(Buf, EndBuf, PrimType)) throw Error_readvbr;
Chris Lattner00950542001-06-06 20:29:01 +000020
Chris Lattner1d670cc2001-09-07 16:37:43 +000021 const Type *Val = 0;
22 if ((Val = Type::getPrimitiveType((Type::PrimitiveID)PrimType)))
23 return Val;
Chris Lattner00950542001-06-06 20:29:01 +000024
25 switch (PrimType) {
Chris Lattnerc9aa7df2002-03-29 03:51:11 +000026 case Type::FunctionTyID: {
Chris Lattner00950542001-06-06 20:29:01 +000027 unsigned Typ;
Chris Lattner74734132002-08-17 22:01:27 +000028 if (read_vbr(Buf, EndBuf, Typ)) return Val;
Chris Lattner00950542001-06-06 20:29:01 +000029 const Type *RetType = getType(Typ);
Chris Lattner74734132002-08-17 22:01:27 +000030 if (RetType == 0) return Val;
Chris Lattner00950542001-06-06 20:29:01 +000031
Chris Lattnere5a57ee2001-07-25 22:47:55 +000032 unsigned NumParams;
Chris Lattner74734132002-08-17 22:01:27 +000033 if (read_vbr(Buf, EndBuf, NumParams)) return Val;
Chris Lattner00950542001-06-06 20:29:01 +000034
Chris Lattner697954c2002-01-20 22:54:45 +000035 std::vector<const Type*> Params;
Chris Lattnere5a57ee2001-07-25 22:47:55 +000036 while (NumParams--) {
Chris Lattner74734132002-08-17 22:01:27 +000037 if (read_vbr(Buf, EndBuf, Typ)) return Val;
Chris Lattner00950542001-06-06 20:29:01 +000038 const Type *Ty = getType(Typ);
Chris Lattner74734132002-08-17 22:01:27 +000039 if (Ty == 0) return Val;
Chris Lattner00950542001-06-06 20:29:01 +000040 Params.push_back(Ty);
Chris Lattner00950542001-06-06 20:29:01 +000041 }
42
Chris Lattner05950c32001-10-13 06:47:01 +000043 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
44 if (isVarArg) Params.pop_back();
45
Chris Lattnerc9aa7df2002-03-29 03:51:11 +000046 return FunctionType::get(RetType, Params, isVarArg);
Chris Lattner00950542001-06-06 20:29:01 +000047 }
48 case Type::ArrayTyID: {
49 unsigned ElTyp;
Chris Lattner74734132002-08-17 22:01:27 +000050 if (read_vbr(Buf, EndBuf, ElTyp)) return Val;
Chris Lattner00950542001-06-06 20:29:01 +000051 const Type *ElementType = getType(ElTyp);
Chris Lattner74734132002-08-17 22:01:27 +000052 if (ElementType == 0) return Val;
Chris Lattner00950542001-06-06 20:29:01 +000053
Chris Lattner90865152001-12-14 16:30:51 +000054 unsigned NumElements;
Chris Lattner74734132002-08-17 22:01:27 +000055 if (read_vbr(Buf, EndBuf, NumElements)) return Val;
Chris Lattner7eadfa12001-10-24 06:21:22 +000056
57 BCR_TRACE(5, "Array Type Constant #" << ElTyp << " size="
Chris Lattner697954c2002-01-20 22:54:45 +000058 << NumElements << "\n");
Chris Lattner7eadfa12001-10-24 06:21:22 +000059 return ArrayType::get(ElementType, NumElements);
Chris Lattner00950542001-06-06 20:29:01 +000060 }
61 case Type::StructTyID: {
62 unsigned Typ;
Chris Lattner697954c2002-01-20 22:54:45 +000063 std::vector<const Type*> Elements;
Chris Lattner00950542001-06-06 20:29:01 +000064
Chris Lattner74734132002-08-17 22:01:27 +000065 if (read_vbr(Buf, EndBuf, Typ)) return Val;
Chris Lattner00950542001-06-06 20:29:01 +000066 while (Typ) { // List is terminated by void/0 typeid
67 const Type *Ty = getType(Typ);
Chris Lattner74734132002-08-17 22:01:27 +000068 if (Ty == 0) return Val;
Chris Lattner00950542001-06-06 20:29:01 +000069 Elements.push_back(Ty);
70
Chris Lattner74734132002-08-17 22:01:27 +000071 if (read_vbr(Buf, EndBuf, Typ)) return Val;
Chris Lattner00950542001-06-06 20:29:01 +000072 }
73
Chris Lattner7eadfa12001-10-24 06:21:22 +000074 return StructType::get(Elements);
Chris Lattner00950542001-06-06 20:29:01 +000075 }
76 case Type::PointerTyID: {
77 unsigned ElTyp;
Chris Lattner74734132002-08-17 22:01:27 +000078 if (read_vbr(Buf, EndBuf, ElTyp)) return Val;
Chris Lattner6c51a362003-09-03 20:25:27 +000079 BCR_TRACE(5, "Pointer Type Constant #" << ElTyp << "\n");
Chris Lattner00950542001-06-06 20:29:01 +000080 const Type *ElementType = getType(ElTyp);
Chris Lattner74734132002-08-17 22:01:27 +000081 if (ElementType == 0) return Val;
Chris Lattner7eadfa12001-10-24 06:21:22 +000082 return PointerType::get(ElementType);
Chris Lattner00950542001-06-06 20:29:01 +000083 }
84
Chris Lattnerd48d6c72001-10-23 01:53:01 +000085 case Type::OpaqueTyID: {
Chris Lattner7eadfa12001-10-24 06:21:22 +000086 return OpaqueType::get();
Chris Lattnerd48d6c72001-10-23 01:53:01 +000087 }
88
Chris Lattner00950542001-06-06 20:29:01 +000089 default:
Anand Shuklaeea60fc2002-06-25 20:44:04 +000090 std::cerr << __FILE__ << ":" << __LINE__
Chris Lattner194f0a52002-06-30 16:04:37 +000091 << ": Don't know how to deserialize"
92 << " primitive Type " << PrimType << "\n";
Chris Lattner74734132002-08-17 22:01:27 +000093 return Val;
Chris Lattner00950542001-06-06 20:29:01 +000094 }
Chris Lattner1d670cc2001-09-07 16:37:43 +000095}
96
97// refineAbstractType - The callback method is invoked when one of the
98// elements of TypeValues becomes more concrete...
99//
100void BytecodeParser::refineAbstractType(const DerivedType *OldType,
101 const Type *NewType) {
Chris Lattner6e5a0e42003-03-06 17:18:14 +0000102 TypeValuesListTy::iterator I = find(FunctionTypeValues.begin(),
103 FunctionTypeValues.end(), OldType);
104 if (I == FunctionTypeValues.end()) {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000105 I = find(ModuleTypeValues.begin(), ModuleTypeValues.end(), OldType);
106 assert(I != ModuleTypeValues.end() &&
107 "Can't refine a type I don't know about!");
108 }
109
Chris Lattner3e80abe2003-09-03 16:01:54 +0000110 I->removeUserFromConcrete();
111 *I = NewType; // Update to point to new, more refined type.
Chris Lattner1d670cc2001-09-07 16:37:43 +0000112}
113
114
115
Misha Brukman37f92e22003-09-11 22:34:13 +0000116// parseTypeConstants - We have to use this weird code to handle recursive
Chris Lattner1d670cc2001-09-07 16:37:43 +0000117// types. We know that recursive types will only reference the current slab of
118// values in the type plane, but they can forward reference types before they
119// have been read. For example, Type #0 might be '{ Ty#1 }' and Type #1 might
120// be 'Ty#0*'. When reading Type #0, type number one doesn't exist. To fix
Misha Brukman37f92e22003-09-11 22:34:13 +0000121// this ugly problem, we pessimistically insert an opaque type for each type we
Chris Lattner1d670cc2001-09-07 16:37:43 +0000122// are about to read. This means that forward references will resolve to
123// something and when we reread the type later, we can replace the opaque type
124// with a new resolved concrete type.
125//
Chris Lattner92940ac2002-04-07 06:11:22 +0000126void debug_type_tables();
Misha Brukman12c29d12003-09-22 23:38:23 +0000127void BytecodeParser::parseTypeConstants(const unsigned char *&Buf,
Chris Lattner12e64652003-05-22 18:08:30 +0000128 const unsigned char *EndBuf,
Chris Lattner1d670cc2001-09-07 16:37:43 +0000129 TypeValuesListTy &Tab,
130 unsigned NumEntries) {
Chris Lattner05950c32001-10-13 06:47:01 +0000131 assert(Tab.size() == 0 && "should not have read type constants in before!");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000132
133 // Insert a bunch of opaque types to be resolved later...
Chris Lattner7eadfa12001-10-24 06:21:22 +0000134 for (unsigned i = 0; i < NumEntries; ++i)
Chris Lattner893f0252003-06-18 19:22:36 +0000135 Tab.push_back(PATypeHandle(OpaqueType::get(), this));
Chris Lattner1d670cc2001-09-07 16:37:43 +0000136
137 // Loop through reading all of the types. Forward types will make use of the
138 // opaque types just inserted.
139 //
Chris Lattner7eadfa12001-10-24 06:21:22 +0000140 for (unsigned i = 0; i < NumEntries; ++i) {
Chris Lattner05950c32001-10-13 06:47:01 +0000141 const Type *NewTy = parseTypeConstant(Buf, EndBuf), *OldTy = Tab[i].get();
Misha Brukman12c29d12003-09-22 23:38:23 +0000142 if (NewTy == 0) throw std::string("Parsed invalid type.");
Chris Lattner7eadfa12001-10-24 06:21:22 +0000143 BCR_TRACE(4, "#" << i << ": Read Type Constant: '" << NewTy <<
144 "' Replacing: " << OldTy << "\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000145
146 // Don't insertValue the new type... instead we want to replace the opaque
147 // type with the new concrete value...
148 //
149
150 // Refine the abstract type to the new type. This causes all uses of the
151 // abstract type to use the newty. This also will cause the opaque type
152 // to be deleted...
153 //
Chris Lattnera48836b2002-06-05 17:38:28 +0000154 ((DerivedType*)Tab[i].get())->refineAbstractTypeTo(NewTy);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000155
156 // This should have replace the old opaque type with the new type in the
Chris Lattner05950c32001-10-13 06:47:01 +0000157 // value table... or with a preexisting type that was already in the system
158 assert(Tab[i] != OldTy && "refineAbstractType didn't work!");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000159 }
160
161 BCR_TRACE(5, "Resulting types:\n");
Chris Lattner7eadfa12001-10-24 06:21:22 +0000162 for (unsigned i = 0; i < NumEntries; ++i) {
Chris Lattner92940ac2002-04-07 06:11:22 +0000163 BCR_TRACE(5, (void*)Tab[i].get() << " - " << Tab[i].get() << "\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000164 }
Chris Lattner92940ac2002-04-07 06:11:22 +0000165 debug_type_tables();
Chris Lattner00950542001-06-06 20:29:01 +0000166}
167
Chris Lattner1d670cc2001-09-07 16:37:43 +0000168
Misha Brukman12c29d12003-09-22 23:38:23 +0000169void BytecodeParser::parseConstantValue(const unsigned char *&Buf,
Chris Lattner12e64652003-05-22 18:08:30 +0000170 const unsigned char *EndBuf,
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000171 const Type *Ty, Constant *&V) {
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000172
173 // We must check for a ConstantExpr before switching by type because
174 // a ConstantExpr can be of any type, and has no explicit value.
175 //
176 unsigned isExprNumArgs; // 0 if not expr; numArgs if is expr
Misha Brukman12c29d12003-09-22 23:38:23 +0000177 if (read_vbr(Buf, EndBuf, isExprNumArgs)) throw Error_readvbr;
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000178 if (isExprNumArgs) {
Chris Lattnere8e46052002-07-30 18:54:22 +0000179 // FIXME: Encoding of constant exprs could be much more compact!
180 unsigned Opcode;
181 std::vector<Constant*> ArgVec;
182 ArgVec.reserve(isExprNumArgs);
Misha Brukman12c29d12003-09-22 23:38:23 +0000183 if (read_vbr(Buf, EndBuf, Opcode)) throw Error_readvbr;
Chris Lattnere8e46052002-07-30 18:54:22 +0000184
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000185 // Read the slot number and types of each of the arguments
Chris Lattnere8e46052002-07-30 18:54:22 +0000186 for (unsigned i = 0; i != isExprNumArgs; ++i) {
187 unsigned ArgValSlot, ArgTypeSlot;
Misha Brukman12c29d12003-09-22 23:38:23 +0000188 if (read_vbr(Buf, EndBuf, ArgValSlot)) throw Error_readvbr;
189 if (read_vbr(Buf, EndBuf, ArgTypeSlot)) throw Error_readvbr;
Chris Lattnere8e46052002-07-30 18:54:22 +0000190 const Type *ArgTy = getType(ArgTypeSlot);
Misha Brukman12c29d12003-09-22 23:38:23 +0000191 if (ArgTy == 0) throw std::string("Argument type slot not found.");
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000192
Chris Lattnere8e46052002-07-30 18:54:22 +0000193 BCR_TRACE(4, "CE Arg " << i << ": Type: '" << ArgTy << "' slot: "
194 << ArgValSlot << "\n");
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000195
196 // Get the arg value from its slot if it exists, otherwise a placeholder
Chris Lattnerbbd4b302002-10-14 03:33:02 +0000197 Constant *C = getConstantValue(ArgTy, ArgValSlot);
Misha Brukman12c29d12003-09-22 23:38:23 +0000198 if (C == 0) throw std::string("No arg value or placeholder found.");
Chris Lattnere8e46052002-07-30 18:54:22 +0000199 ArgVec.push_back(C);
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000200 }
201
202 // Construct a ConstantExpr of the appropriate kind
203 if (isExprNumArgs == 1) { // All one-operand expressions
Chris Lattnerad333482002-08-14 18:24:09 +0000204 assert(Opcode == Instruction::Cast);
205 V = ConstantExpr::getCast(ArgVec[0], Ty);
Chris Lattnere8e46052002-07-30 18:54:22 +0000206 } else if (Opcode == Instruction::GetElementPtr) { // GetElementPtr
207 std::vector<Constant*> IdxList(ArgVec.begin()+1, ArgVec.end());
208 V = ConstantExpr::getGetElementPtr(ArgVec[0], IdxList);
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000209 } else { // All other 2-operand expressions
Chris Lattnere8e46052002-07-30 18:54:22 +0000210 V = ConstantExpr::get(Opcode, ArgVec[0], ArgVec[1]);
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000211 }
Misha Brukman12c29d12003-09-22 23:38:23 +0000212 return;
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000213 }
214
215 // Ok, not an ConstantExpr. We now know how to read the given type...
Chris Lattner00950542001-06-06 20:29:01 +0000216 switch (Ty->getPrimitiveID()) {
217 case Type::BoolTyID: {
218 unsigned Val;
Misha Brukman12c29d12003-09-22 23:38:23 +0000219 if (read_vbr(Buf, EndBuf, Val)) throw Error_readvbr;
220 if (Val != 0 && Val != 1) throw std::string("Invalid boolean value read.");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000221 V = ConstantBool::get(Val == 1);
Chris Lattner00950542001-06-06 20:29:01 +0000222 break;
223 }
224
225 case Type::UByteTyID: // Unsigned integer types...
226 case Type::UShortTyID:
227 case Type::UIntTyID: {
228 unsigned Val;
Misha Brukman12c29d12003-09-22 23:38:23 +0000229 if (read_vbr(Buf, EndBuf, Val)) throw Error_readvbr;
230 if (!ConstantUInt::isValueValidForType(Ty, Val))
231 throw std::string("Invalid unsigned byte/short/int read.");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000232 V = ConstantUInt::get(Ty, Val);
Chris Lattner00950542001-06-06 20:29:01 +0000233 break;
234 }
235
236 case Type::ULongTyID: {
237 uint64_t Val;
Misha Brukman12c29d12003-09-22 23:38:23 +0000238 if (read_vbr(Buf, EndBuf, Val)) throw Error_readvbr;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000239 V = ConstantUInt::get(Ty, Val);
Chris Lattner00950542001-06-06 20:29:01 +0000240 break;
241 }
242
Chris Lattner8017a752003-05-12 15:13:52 +0000243 case Type::SByteTyID: // Signed integer types...
Chris Lattner00950542001-06-06 20:29:01 +0000244 case Type::ShortTyID:
245 case Type::IntTyID: {
Chris Lattner8017a752003-05-12 15:13:52 +0000246 case Type::LongTyID:
Chris Lattner00950542001-06-06 20:29:01 +0000247 int64_t Val;
Misha Brukman12c29d12003-09-22 23:38:23 +0000248 if (read_vbr(Buf, EndBuf, Val)) throw Error_readvbr;
249 if (!ConstantSInt::isValueValidForType(Ty, Val))
250 throw std::string("Invalid signed byte/short/int/long read.");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000251 V = ConstantSInt::get(Ty, Val);
Chris Lattner00950542001-06-06 20:29:01 +0000252 break;
253 }
254
Chris Lattnera1375302001-07-15 00:17:18 +0000255 case Type::FloatTyID: {
256 float F;
Misha Brukman12c29d12003-09-22 23:38:23 +0000257 if (input_data(Buf, EndBuf, &F, &F+1)) throw Error_inputdata;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000258 V = ConstantFP::get(Ty, F);
Chris Lattnera1375302001-07-15 00:17:18 +0000259 break;
260 }
261
262 case Type::DoubleTyID: {
263 double Val;
Misha Brukman12c29d12003-09-22 23:38:23 +0000264 if (input_data(Buf, EndBuf, &Val, &Val+1)) throw Error_inputdata;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000265 V = ConstantFP::get(Ty, Val);
Chris Lattnera1375302001-07-15 00:17:18 +0000266 break;
267 }
268
Chris Lattner00950542001-06-06 20:29:01 +0000269 case Type::TypeTyID:
Misha Brukmanbc0e9982003-07-14 17:20:40 +0000270 assert(0 && "Type constants should be handled separately!!!");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000271 abort();
Chris Lattner00950542001-06-06 20:29:01 +0000272
273 case Type::ArrayTyID: {
Chris Lattner949a3622003-07-23 15:30:06 +0000274 const ArrayType *AT = cast<ArrayType>(Ty);
Chris Lattner90865152001-12-14 16:30:51 +0000275 unsigned NumElements = AT->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +0000276
Chris Lattner697954c2002-01-20 22:54:45 +0000277 std::vector<Constant*> Elements;
Chris Lattner00950542001-06-06 20:29:01 +0000278 while (NumElements--) { // Read all of the elements of the constant.
279 unsigned Slot;
Misha Brukman12c29d12003-09-22 23:38:23 +0000280 if (read_vbr(Buf, EndBuf, Slot)) throw Error_readvbr;
Chris Lattnerbbd4b302002-10-14 03:33:02 +0000281 Constant *C = getConstantValue(AT->getElementType(), Slot);
Misha Brukman12c29d12003-09-22 23:38:23 +0000282 if (!C) throw std::string("Unable to get const value of array slot.");
Chris Lattnerbbd4b302002-10-14 03:33:02 +0000283 Elements.push_back(C);
Chris Lattner00950542001-06-06 20:29:01 +0000284 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000285 V = ConstantArray::get(AT, Elements);
Chris Lattner00950542001-06-06 20:29:01 +0000286 break;
287 }
288
289 case Type::StructTyID: {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000290 const StructType *ST = cast<StructType>(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000291 const StructType::ElementTypes &ET = ST->getElementTypes();
292
Chris Lattner697954c2002-01-20 22:54:45 +0000293 std::vector<Constant *> Elements;
Chris Lattner00950542001-06-06 20:29:01 +0000294 for (unsigned i = 0; i < ET.size(); ++i) {
295 unsigned Slot;
Misha Brukman12c29d12003-09-22 23:38:23 +0000296 if (read_vbr(Buf, EndBuf, Slot)) throw Error_readvbr;
Chris Lattnerbbd4b302002-10-14 03:33:02 +0000297 Constant *C = getConstantValue(ET[i], Slot);
Misha Brukman12c29d12003-09-22 23:38:23 +0000298 if (!C) throw std::string("Could not read const value in struct slot.");
Chris Lattnerbbd4b302002-10-14 03:33:02 +0000299 Elements.push_back(C);
Chris Lattner00950542001-06-06 20:29:01 +0000300 }
301
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000302 V = ConstantStruct::get(ST, Elements);
Chris Lattner00950542001-06-06 20:29:01 +0000303 break;
304 }
305
Chris Lattner1a1cb112001-09-30 22:46:54 +0000306 case Type::PointerTyID: {
Chris Lattner949a3622003-07-23 15:30:06 +0000307 const PointerType *PT = cast<PointerType>(Ty);
Chris Lattner1a1cb112001-09-30 22:46:54 +0000308 unsigned SubClass;
Chris Lattner52e20b02003-03-19 20:54:26 +0000309 if (HasImplicitZeroInitializer)
310 SubClass = 1;
311 else
Misha Brukman12c29d12003-09-22 23:38:23 +0000312 if (read_vbr(Buf, EndBuf, SubClass)) throw Error_readvbr;
Chris Lattner52e20b02003-03-19 20:54:26 +0000313
Chris Lattner05950c32001-10-13 06:47:01 +0000314 switch (SubClass) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000315 case 0: // ConstantPointerNull value...
316 V = ConstantPointerNull::get(PT);
Chris Lattner05950c32001-10-13 06:47:01 +0000317 break;
Chris Lattner1a1cb112001-09-30 22:46:54 +0000318
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000319 case 1: { // ConstantPointerRef value...
Chris Lattner05950c32001-10-13 06:47:01 +0000320 unsigned Slot;
Misha Brukman12c29d12003-09-22 23:38:23 +0000321 if (read_vbr(Buf, EndBuf, Slot)) throw Error_readvbr;
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000322 BCR_TRACE(4, "CPR: Type: '" << Ty << "' slot: " << Slot << "\n");
Chris Lattner1a1cb112001-09-30 22:46:54 +0000323
Chris Lattner74734132002-08-17 22:01:27 +0000324 // Check to see if we have already read this global variable...
Chris Lattner05950c32001-10-13 06:47:01 +0000325 Value *Val = getValue(PT, Slot, false);
Chris Lattner74734132002-08-17 22:01:27 +0000326 GlobalValue *GV;
Chris Lattner05950c32001-10-13 06:47:01 +0000327 if (Val) {
Misha Brukman12c29d12003-09-22 23:38:23 +0000328 if (!(GV = dyn_cast<GlobalValue>(Val)))
329 throw std::string("Value of ConstantPointerRef not in ValueTable!");
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000330 BCR_TRACE(5, "Value Found in ValueTable!\n");
Chris Lattner52e20b02003-03-19 20:54:26 +0000331 } else if (RevisionNum > 0) {
Misha Brukman37f92e22003-09-11 22:34:13 +0000332 // Revision #0 could have forward references to globals that were weird.
Chris Lattner52e20b02003-03-19 20:54:26 +0000333 // We got rid of this in subsequent revs.
Misha Brukman12c29d12003-09-22 23:38:23 +0000334 throw std::string("Forward references to globals not allowed.");
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000335 } else { // Nope... find or create a forward ref. for it
Chris Lattner0d75d8d72003-03-06 16:32:25 +0000336 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(PT, Slot));
Chris Lattner74734132002-08-17 22:01:27 +0000337
338 if (I != GlobalRefs.end()) {
339 BCR_TRACE(5, "Previous forward ref found!\n");
340 GV = cast<GlobalValue>(I->second);
341 } else {
342 BCR_TRACE(5, "Creating new forward ref to a global variable!\n");
Chris Lattner52e20b02003-03-19 20:54:26 +0000343
344 // Create a placeholder for the global variable reference...
Chris Lattner74734132002-08-17 22:01:27 +0000345 GlobalVariable *GVar =
Chris Lattner4ad02e72003-04-16 20:28:45 +0000346 new GlobalVariable(PT->getElementType(), false,
347 GlobalValue::InternalLinkage);
Chris Lattner74734132002-08-17 22:01:27 +0000348
Chris Lattner52e20b02003-03-19 20:54:26 +0000349 // Keep track of the fact that we have a forward ref to recycle it
Chris Lattner0d75d8d72003-03-06 16:32:25 +0000350 GlobalRefs.insert(std::make_pair(std::make_pair(PT, Slot), GVar));
Chris Lattner74734132002-08-17 22:01:27 +0000351
352 // Must temporarily push this value into the module table...
353 TheModule->getGlobalList().push_back(GVar);
354 GV = GVar;
355 }
Chris Lattner05950c32001-10-13 06:47:01 +0000356 }
Chris Lattner52e20b02003-03-19 20:54:26 +0000357
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000358 V = ConstantPointerRef::get(GV);
Chris Lattner05950c32001-10-13 06:47:01 +0000359 break;
360 }
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000361
Chris Lattner05950c32001-10-13 06:47:01 +0000362 default:
Chris Lattner90865152001-12-14 16:30:51 +0000363 BCR_TRACE(5, "UNKNOWN Pointer Constant Type!\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000364 throw std::string("Unknown pointer constant type.");
Chris Lattner05950c32001-10-13 06:47:01 +0000365 }
Chris Lattner1a1cb112001-09-30 22:46:54 +0000366 break;
367 }
368
Chris Lattner00950542001-06-06 20:29:01 +0000369 default:
Anand Shuklaeea60fc2002-06-25 20:44:04 +0000370 std::cerr << __FILE__ << ":" << __LINE__
Chris Lattner194f0a52002-06-30 16:04:37 +0000371 << ": Don't know how to deserialize constant value of type '"
372 << Ty->getName() << "'\n";
Misha Brukman12c29d12003-09-22 23:38:23 +0000373 throw std::string("Don't know how to deserialize constant value of type '"+
374 Ty->getName());
Chris Lattner00950542001-06-06 20:29:01 +0000375 }
Chris Lattner00950542001-06-06 20:29:01 +0000376}
377
Misha Brukman12c29d12003-09-22 23:38:23 +0000378void BytecodeParser::ParseGlobalTypes(const unsigned char *&Buf,
Chris Lattner12e64652003-05-22 18:08:30 +0000379 const unsigned char *EndBuf) {
Chris Lattner52e20b02003-03-19 20:54:26 +0000380 ValueTable T;
Misha Brukman12c29d12003-09-22 23:38:23 +0000381 ParseConstantPool(Buf, EndBuf, T, ModuleTypeValues);
Chris Lattner52e20b02003-03-19 20:54:26 +0000382}
383
Misha Brukman12c29d12003-09-22 23:38:23 +0000384void BytecodeParser::ParseConstantPool(const unsigned char *&Buf,
Chris Lattner12e64652003-05-22 18:08:30 +0000385 const unsigned char *EndBuf,
Misha Brukman12c29d12003-09-22 23:38:23 +0000386 ValueTable &Tab,
387 TypeValuesListTy &TypeTab) {
Chris Lattner00950542001-06-06 20:29:01 +0000388 while (Buf < EndBuf) {
389 unsigned NumEntries, Typ;
390
391 if (read_vbr(Buf, EndBuf, NumEntries) ||
Misha Brukman12c29d12003-09-22 23:38:23 +0000392 read_vbr(Buf, EndBuf, Typ)) throw Error_readvbr;
Chris Lattner00950542001-06-06 20:29:01 +0000393 const Type *Ty = getType(Typ);
Misha Brukman12c29d12003-09-22 23:38:23 +0000394 if (Ty == 0) throw std::string("Invalid type read.");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000395 BCR_TRACE(3, "Type: '" << Ty << "' NumEntries: " << NumEntries << "\n");
Chris Lattner00950542001-06-06 20:29:01 +0000396
Chris Lattner1d670cc2001-09-07 16:37:43 +0000397 if (Typ == Type::TypeTyID) {
Misha Brukman12c29d12003-09-22 23:38:23 +0000398 parseTypeConstants(Buf, EndBuf, TypeTab, NumEntries);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000399 } else {
Chris Lattner7eadfa12001-10-24 06:21:22 +0000400 for (unsigned i = 0; i < NumEntries; ++i) {
Misha Brukman12c29d12003-09-22 23:38:23 +0000401 Constant *C;
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000402 int Slot;
Misha Brukman12c29d12003-09-22 23:38:23 +0000403 parseConstantValue(Buf, EndBuf, Ty, C);
Chris Lattner52e20b02003-03-19 20:54:26 +0000404 assert(C && "parseConstantValue returned NULL!");
Misha Brukman12c29d12003-09-22 23:38:23 +0000405 BCR_TRACE(4, "Read Constant: '" << *C << "'\n");
406 if ((Slot = insertValue(C, Tab)) == -1)
407 throw std::string("Could not insert value into ValueTable.");
Chris Lattner74734132002-08-17 22:01:27 +0000408
409 // If we are reading a function constant table, make sure that we adjust
410 // the slot number to be the real global constant number.
411 //
Chris Lattner52e20b02003-03-19 20:54:26 +0000412 if (&Tab != &ModuleValues && Typ < ModuleValues.size())
413 Slot += ModuleValues[Typ]->size();
414 ResolveReferencesToValue(C, (unsigned)Slot);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000415 }
Chris Lattner00950542001-06-06 20:29:01 +0000416 }
417 }
418
Misha Brukman12c29d12003-09-22 23:38:23 +0000419 if (Buf > EndBuf) throw std::string("Read past end of buffer.");
Chris Lattner00950542001-06-06 20:29:01 +0000420}