blob: 0746b3b58ac955e4444285f66d8e1e7fc40514ba [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 Lattner00950542001-06-06 20:29:01 +000030
Chris Lattnere5a57ee2001-07-25 22:47:55 +000031 unsigned NumParams;
Chris Lattner74734132002-08-17 22:01:27 +000032 if (read_vbr(Buf, EndBuf, NumParams)) return Val;
Chris Lattner00950542001-06-06 20:29:01 +000033
Chris Lattner697954c2002-01-20 22:54:45 +000034 std::vector<const Type*> Params;
Chris Lattnere5a57ee2001-07-25 22:47:55 +000035 while (NumParams--) {
Chris Lattner74734132002-08-17 22:01:27 +000036 if (read_vbr(Buf, EndBuf, Typ)) return Val;
Chris Lattner927b1852003-10-09 20:22:47 +000037 Params.push_back(getType(Typ));
Chris Lattner00950542001-06-06 20:29:01 +000038 }
39
Chris Lattner05950c32001-10-13 06:47:01 +000040 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
41 if (isVarArg) Params.pop_back();
42
Chris Lattnerc9aa7df2002-03-29 03:51:11 +000043 return FunctionType::get(RetType, Params, isVarArg);
Chris Lattner00950542001-06-06 20:29:01 +000044 }
45 case Type::ArrayTyID: {
46 unsigned ElTyp;
Chris Lattner74734132002-08-17 22:01:27 +000047 if (read_vbr(Buf, EndBuf, ElTyp)) return Val;
Chris Lattner00950542001-06-06 20:29:01 +000048 const Type *ElementType = getType(ElTyp);
Chris Lattner00950542001-06-06 20:29:01 +000049
Chris Lattner90865152001-12-14 16:30:51 +000050 unsigned NumElements;
Chris Lattner74734132002-08-17 22:01:27 +000051 if (read_vbr(Buf, EndBuf, NumElements)) return Val;
Chris Lattner7eadfa12001-10-24 06:21:22 +000052
53 BCR_TRACE(5, "Array Type Constant #" << ElTyp << " size="
Chris Lattner697954c2002-01-20 22:54:45 +000054 << NumElements << "\n");
Chris Lattner7eadfa12001-10-24 06:21:22 +000055 return ArrayType::get(ElementType, NumElements);
Chris Lattner00950542001-06-06 20:29:01 +000056 }
57 case Type::StructTyID: {
58 unsigned Typ;
Chris Lattner697954c2002-01-20 22:54:45 +000059 std::vector<const Type*> Elements;
Chris Lattner00950542001-06-06 20:29:01 +000060
Chris Lattner74734132002-08-17 22:01:27 +000061 if (read_vbr(Buf, EndBuf, Typ)) return Val;
Chris Lattner00950542001-06-06 20:29:01 +000062 while (Typ) { // List is terminated by void/0 typeid
Chris Lattner927b1852003-10-09 20:22:47 +000063 Elements.push_back(getType(Typ));
Chris Lattner74734132002-08-17 22:01:27 +000064 if (read_vbr(Buf, EndBuf, Typ)) return Val;
Chris Lattner00950542001-06-06 20:29:01 +000065 }
66
Chris Lattner7eadfa12001-10-24 06:21:22 +000067 return StructType::get(Elements);
Chris Lattner00950542001-06-06 20:29:01 +000068 }
69 case Type::PointerTyID: {
70 unsigned ElTyp;
Chris Lattner74734132002-08-17 22:01:27 +000071 if (read_vbr(Buf, EndBuf, ElTyp)) return Val;
Chris Lattner6c51a362003-09-03 20:25:27 +000072 BCR_TRACE(5, "Pointer Type Constant #" << ElTyp << "\n");
Chris Lattner927b1852003-10-09 20:22:47 +000073 return PointerType::get(getType(ElTyp));
Chris Lattner00950542001-06-06 20:29:01 +000074 }
75
Chris Lattnerd48d6c72001-10-23 01:53:01 +000076 case Type::OpaqueTyID: {
Chris Lattner7eadfa12001-10-24 06:21:22 +000077 return OpaqueType::get();
Chris Lattnerd48d6c72001-10-23 01:53:01 +000078 }
79
Chris Lattner00950542001-06-06 20:29:01 +000080 default:
Anand Shuklaeea60fc2002-06-25 20:44:04 +000081 std::cerr << __FILE__ << ":" << __LINE__
Chris Lattner194f0a52002-06-30 16:04:37 +000082 << ": Don't know how to deserialize"
83 << " primitive Type " << PrimType << "\n";
Chris Lattner74734132002-08-17 22:01:27 +000084 return Val;
Chris Lattner00950542001-06-06 20:29:01 +000085 }
Chris Lattner1d670cc2001-09-07 16:37:43 +000086}
87
Misha Brukman37f92e22003-09-11 22:34:13 +000088// parseTypeConstants - We have to use this weird code to handle recursive
Chris Lattner1d670cc2001-09-07 16:37:43 +000089// types. We know that recursive types will only reference the current slab of
90// values in the type plane, but they can forward reference types before they
91// have been read. For example, Type #0 might be '{ Ty#1 }' and Type #1 might
92// be 'Ty#0*'. When reading Type #0, type number one doesn't exist. To fix
Misha Brukman37f92e22003-09-11 22:34:13 +000093// this ugly problem, we pessimistically insert an opaque type for each type we
Chris Lattner1d670cc2001-09-07 16:37:43 +000094// are about to read. This means that forward references will resolve to
95// something and when we reread the type later, we can replace the opaque type
96// with a new resolved concrete type.
97//
Chris Lattner92940ac2002-04-07 06:11:22 +000098void debug_type_tables();
Misha Brukman12c29d12003-09-22 23:38:23 +000099void BytecodeParser::parseTypeConstants(const unsigned char *&Buf,
Chris Lattner12e64652003-05-22 18:08:30 +0000100 const unsigned char *EndBuf,
Chris Lattner1d670cc2001-09-07 16:37:43 +0000101 TypeValuesListTy &Tab,
102 unsigned NumEntries) {
Chris Lattner05950c32001-10-13 06:47:01 +0000103 assert(Tab.size() == 0 && "should not have read type constants in before!");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000104
105 // Insert a bunch of opaque types to be resolved later...
Chris Lattner7eadfa12001-10-24 06:21:22 +0000106 for (unsigned i = 0; i < NumEntries; ++i)
Chris Lattnerc7b6f032003-10-02 20:26:18 +0000107 Tab.push_back(OpaqueType::get());
Chris Lattner1d670cc2001-09-07 16:37:43 +0000108
109 // Loop through reading all of the types. Forward types will make use of the
110 // opaque types just inserted.
111 //
Chris Lattner7eadfa12001-10-24 06:21:22 +0000112 for (unsigned i = 0; i < NumEntries; ++i) {
Chris Lattner05950c32001-10-13 06:47:01 +0000113 const Type *NewTy = parseTypeConstant(Buf, EndBuf), *OldTy = Tab[i].get();
Misha Brukman12c29d12003-09-22 23:38:23 +0000114 if (NewTy == 0) throw std::string("Parsed invalid type.");
Chris Lattner7eadfa12001-10-24 06:21:22 +0000115 BCR_TRACE(4, "#" << i << ": Read Type Constant: '" << NewTy <<
116 "' Replacing: " << OldTy << "\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000117
118 // Don't insertValue the new type... instead we want to replace the opaque
119 // type with the new concrete value...
120 //
121
122 // Refine the abstract type to the new type. This causes all uses of the
123 // abstract type to use the newty. This also will cause the opaque type
124 // to be deleted...
125 //
Chris Lattnera48836b2002-06-05 17:38:28 +0000126 ((DerivedType*)Tab[i].get())->refineAbstractTypeTo(NewTy);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000127
128 // This should have replace the old opaque type with the new type in the
Chris Lattner05950c32001-10-13 06:47:01 +0000129 // value table... or with a preexisting type that was already in the system
130 assert(Tab[i] != OldTy && "refineAbstractType didn't work!");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000131 }
132
133 BCR_TRACE(5, "Resulting types:\n");
Chris Lattner7eadfa12001-10-24 06:21:22 +0000134 for (unsigned i = 0; i < NumEntries; ++i) {
Chris Lattner92940ac2002-04-07 06:11:22 +0000135 BCR_TRACE(5, (void*)Tab[i].get() << " - " << Tab[i].get() << "\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000136 }
Chris Lattner92940ac2002-04-07 06:11:22 +0000137 debug_type_tables();
Chris Lattner00950542001-06-06 20:29:01 +0000138}
139
Chris Lattner1d670cc2001-09-07 16:37:43 +0000140
Chris Lattner9e460f22003-10-04 20:00:03 +0000141Constant *BytecodeParser::parseConstantValue(const unsigned char *&Buf,
142 const unsigned char *EndBuf,
143 const Type *Ty) {
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000144
145 // We must check for a ConstantExpr before switching by type because
146 // a ConstantExpr can be of any type, and has no explicit value.
147 //
148 unsigned isExprNumArgs; // 0 if not expr; numArgs if is expr
Misha Brukman12c29d12003-09-22 23:38:23 +0000149 if (read_vbr(Buf, EndBuf, isExprNumArgs)) throw Error_readvbr;
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000150 if (isExprNumArgs) {
Chris Lattnere8e46052002-07-30 18:54:22 +0000151 // FIXME: Encoding of constant exprs could be much more compact!
152 unsigned Opcode;
153 std::vector<Constant*> ArgVec;
154 ArgVec.reserve(isExprNumArgs);
Misha Brukman12c29d12003-09-22 23:38:23 +0000155 if (read_vbr(Buf, EndBuf, Opcode)) throw Error_readvbr;
Chris Lattnere8e46052002-07-30 18:54:22 +0000156
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000157 // Read the slot number and types of each of the arguments
Chris Lattnere8e46052002-07-30 18:54:22 +0000158 for (unsigned i = 0; i != isExprNumArgs; ++i) {
159 unsigned ArgValSlot, ArgTypeSlot;
Misha Brukman12c29d12003-09-22 23:38:23 +0000160 if (read_vbr(Buf, EndBuf, ArgValSlot)) throw Error_readvbr;
161 if (read_vbr(Buf, EndBuf, ArgTypeSlot)) throw Error_readvbr;
Chris Lattnere8e46052002-07-30 18:54:22 +0000162 const Type *ArgTy = getType(ArgTypeSlot);
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000163
Chris Lattner927b1852003-10-09 20:22:47 +0000164 BCR_TRACE(4, "CE Arg " << i << ": Type: '" << *ArgTy << "' slot: "
Chris Lattnere8e46052002-07-30 18:54:22 +0000165 << ArgValSlot << "\n");
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000166
167 // Get the arg value from its slot if it exists, otherwise a placeholder
Chris Lattnerc9456ca2003-10-09 20:41:16 +0000168 ArgVec.push_back(getConstantValue(ArgTy, ArgValSlot));
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000169 }
170
171 // Construct a ConstantExpr of the appropriate kind
172 if (isExprNumArgs == 1) { // All one-operand expressions
Chris Lattnerad333482002-08-14 18:24:09 +0000173 assert(Opcode == Instruction::Cast);
Chris Lattner9e460f22003-10-04 20:00:03 +0000174 return ConstantExpr::getCast(ArgVec[0], Ty);
Chris Lattnere8e46052002-07-30 18:54:22 +0000175 } else if (Opcode == Instruction::GetElementPtr) { // GetElementPtr
176 std::vector<Constant*> IdxList(ArgVec.begin()+1, ArgVec.end());
Chris Lattner9e460f22003-10-04 20:00:03 +0000177 return ConstantExpr::getGetElementPtr(ArgVec[0], IdxList);
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000178 } else { // All other 2-operand expressions
Chris Lattner9e460f22003-10-04 20:00:03 +0000179 return ConstantExpr::get(Opcode, ArgVec[0], ArgVec[1]);
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000180 }
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000181 }
182
183 // Ok, not an ConstantExpr. We now know how to read the given type...
Chris Lattner00950542001-06-06 20:29:01 +0000184 switch (Ty->getPrimitiveID()) {
185 case Type::BoolTyID: {
186 unsigned Val;
Misha Brukman12c29d12003-09-22 23:38:23 +0000187 if (read_vbr(Buf, EndBuf, Val)) throw Error_readvbr;
188 if (Val != 0 && Val != 1) throw std::string("Invalid boolean value read.");
Chris Lattner9e460f22003-10-04 20:00:03 +0000189 return ConstantBool::get(Val == 1);
Chris Lattner00950542001-06-06 20:29:01 +0000190 }
191
192 case Type::UByteTyID: // Unsigned integer types...
193 case Type::UShortTyID:
194 case Type::UIntTyID: {
195 unsigned Val;
Misha Brukman12c29d12003-09-22 23:38:23 +0000196 if (read_vbr(Buf, EndBuf, Val)) throw Error_readvbr;
197 if (!ConstantUInt::isValueValidForType(Ty, Val))
198 throw std::string("Invalid unsigned byte/short/int read.");
Chris Lattner9e460f22003-10-04 20:00:03 +0000199 return ConstantUInt::get(Ty, Val);
Chris Lattner00950542001-06-06 20:29:01 +0000200 }
201
202 case Type::ULongTyID: {
203 uint64_t Val;
Misha Brukman12c29d12003-09-22 23:38:23 +0000204 if (read_vbr(Buf, EndBuf, Val)) throw Error_readvbr;
Chris Lattner9e460f22003-10-04 20:00:03 +0000205 return ConstantUInt::get(Ty, Val);
Chris Lattner00950542001-06-06 20:29:01 +0000206 }
207
Chris Lattner8017a752003-05-12 15:13:52 +0000208 case Type::SByteTyID: // Signed integer types...
Chris Lattner00950542001-06-06 20:29:01 +0000209 case Type::ShortTyID:
210 case Type::IntTyID: {
Chris Lattner8017a752003-05-12 15:13:52 +0000211 case Type::LongTyID:
Chris Lattner00950542001-06-06 20:29:01 +0000212 int64_t Val;
Misha Brukman12c29d12003-09-22 23:38:23 +0000213 if (read_vbr(Buf, EndBuf, Val)) throw Error_readvbr;
214 if (!ConstantSInt::isValueValidForType(Ty, Val))
215 throw std::string("Invalid signed byte/short/int/long read.");
Chris Lattner9e460f22003-10-04 20:00:03 +0000216 return ConstantSInt::get(Ty, Val);
Chris Lattner00950542001-06-06 20:29:01 +0000217 }
218
Chris Lattnera1375302001-07-15 00:17:18 +0000219 case Type::FloatTyID: {
220 float F;
Misha Brukman12c29d12003-09-22 23:38:23 +0000221 if (input_data(Buf, EndBuf, &F, &F+1)) throw Error_inputdata;
Chris Lattner9e460f22003-10-04 20:00:03 +0000222 return ConstantFP::get(Ty, F);
Chris Lattnera1375302001-07-15 00:17:18 +0000223 }
224
225 case Type::DoubleTyID: {
226 double Val;
Misha Brukman12c29d12003-09-22 23:38:23 +0000227 if (input_data(Buf, EndBuf, &Val, &Val+1)) throw Error_inputdata;
Chris Lattner9e460f22003-10-04 20:00:03 +0000228 return ConstantFP::get(Ty, Val);
Chris Lattnera1375302001-07-15 00:17:18 +0000229 }
230
Chris Lattner00950542001-06-06 20:29:01 +0000231 case Type::TypeTyID:
Chris Lattner9e460f22003-10-04 20:00:03 +0000232 throw std::string("Type constants shouldn't live in constant table!");
Chris Lattner00950542001-06-06 20:29:01 +0000233
234 case Type::ArrayTyID: {
Chris Lattner949a3622003-07-23 15:30:06 +0000235 const ArrayType *AT = cast<ArrayType>(Ty);
Chris Lattner90865152001-12-14 16:30:51 +0000236 unsigned NumElements = AT->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +0000237
Chris Lattner697954c2002-01-20 22:54:45 +0000238 std::vector<Constant*> Elements;
Chris Lattner00950542001-06-06 20:29:01 +0000239 while (NumElements--) { // Read all of the elements of the constant.
240 unsigned Slot;
Misha Brukman12c29d12003-09-22 23:38:23 +0000241 if (read_vbr(Buf, EndBuf, Slot)) throw Error_readvbr;
Chris Lattnerc9456ca2003-10-09 20:41:16 +0000242 Elements.push_back(getConstantValue(AT->getElementType(), Slot));
Chris Lattner00950542001-06-06 20:29:01 +0000243 }
Chris Lattner9e460f22003-10-04 20:00:03 +0000244 return ConstantArray::get(AT, Elements);
Chris Lattner00950542001-06-06 20:29:01 +0000245 }
246
247 case Type::StructTyID: {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000248 const StructType *ST = cast<StructType>(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000249 const StructType::ElementTypes &ET = ST->getElementTypes();
250
Chris Lattner697954c2002-01-20 22:54:45 +0000251 std::vector<Constant *> Elements;
Chris Lattner00950542001-06-06 20:29:01 +0000252 for (unsigned i = 0; i < ET.size(); ++i) {
253 unsigned Slot;
Misha Brukman12c29d12003-09-22 23:38:23 +0000254 if (read_vbr(Buf, EndBuf, Slot)) throw Error_readvbr;
Chris Lattnerc9456ca2003-10-09 20:41:16 +0000255 Elements.push_back(getConstantValue(ET[i], Slot));
Chris Lattner00950542001-06-06 20:29:01 +0000256 }
257
Chris Lattner9e460f22003-10-04 20:00:03 +0000258 return ConstantStruct::get(ST, Elements);
Chris Lattner00950542001-06-06 20:29:01 +0000259 }
260
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000261 case Type::PointerTyID: { // ConstantPointerRef value...
Chris Lattner949a3622003-07-23 15:30:06 +0000262 const PointerType *PT = cast<PointerType>(Ty);
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000263 unsigned Slot;
264 if (read_vbr(Buf, EndBuf, Slot)) throw Error_readvbr;
265 BCR_TRACE(4, "CPR: Type: '" << Ty << "' slot: " << Slot << "\n");
266
267 // Check to see if we have already read this global variable...
268 Value *Val = getValue(PT, Slot, false);
269 GlobalValue *GV;
270 if (Val) {
271 if (!(GV = dyn_cast<GlobalValue>(Val)))
272 throw std::string("Value of ConstantPointerRef not in ValueTable!");
273 BCR_TRACE(5, "Value Found in ValueTable!\n");
274 } else if (RevisionNum > 0) {
275 // Revision #0 could have forward references to globals that were weird.
276 // We got rid of this in subsequent revs.
277 throw std::string("Forward references to globals not allowed.");
278 } else { // Nope... find or create a forward ref. for it
279 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(PT, Slot));
280
281 if (I != GlobalRefs.end()) {
282 BCR_TRACE(5, "Previous forward ref found!\n");
283 GV = cast<GlobalValue>(I->second);
284 } else {
285 BCR_TRACE(5, "Creating new forward ref to a global variable!\n");
286
287 // Create a placeholder for the global variable reference...
288 GlobalVariable *GVar =
289 new GlobalVariable(PT->getElementType(), false,
290 GlobalValue::InternalLinkage);
291
292 // Keep track of the fact that we have a forward ref to recycle it
293 GlobalRefs.insert(std::make_pair(std::make_pair(PT, Slot), GVar));
294
295 // Must temporarily push this value into the module table...
296 TheModule->getGlobalList().push_back(GVar);
297 GV = GVar;
Chris Lattner05950c32001-10-13 06:47:01 +0000298 }
Chris Lattner05950c32001-10-13 06:47:01 +0000299 }
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000300
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000301 return ConstantPointerRef::get(GV);
Chris Lattner1a1cb112001-09-30 22:46:54 +0000302 }
303
Chris Lattner00950542001-06-06 20:29:01 +0000304 default:
Misha Brukman12c29d12003-09-22 23:38:23 +0000305 throw std::string("Don't know how to deserialize constant value of type '"+
Chris Lattner9e460f22003-10-04 20:00:03 +0000306 Ty->getDescription());
Chris Lattner00950542001-06-06 20:29:01 +0000307 }
Chris Lattner00950542001-06-06 20:29:01 +0000308}
309
Misha Brukman12c29d12003-09-22 23:38:23 +0000310void BytecodeParser::ParseGlobalTypes(const unsigned char *&Buf,
Chris Lattner12e64652003-05-22 18:08:30 +0000311 const unsigned char *EndBuf) {
Chris Lattner52e20b02003-03-19 20:54:26 +0000312 ValueTable T;
Misha Brukman12c29d12003-09-22 23:38:23 +0000313 ParseConstantPool(Buf, EndBuf, T, ModuleTypeValues);
Chris Lattner52e20b02003-03-19 20:54:26 +0000314}
315
Misha Brukman12c29d12003-09-22 23:38:23 +0000316void BytecodeParser::ParseConstantPool(const unsigned char *&Buf,
Chris Lattner12e64652003-05-22 18:08:30 +0000317 const unsigned char *EndBuf,
Misha Brukman12c29d12003-09-22 23:38:23 +0000318 ValueTable &Tab,
319 TypeValuesListTy &TypeTab) {
Chris Lattner00950542001-06-06 20:29:01 +0000320 while (Buf < EndBuf) {
321 unsigned NumEntries, Typ;
322
323 if (read_vbr(Buf, EndBuf, NumEntries) ||
Misha Brukman12c29d12003-09-22 23:38:23 +0000324 read_vbr(Buf, EndBuf, Typ)) throw Error_readvbr;
Chris Lattner1d670cc2001-09-07 16:37:43 +0000325 if (Typ == Type::TypeTyID) {
Chris Lattner927b1852003-10-09 20:22:47 +0000326 BCR_TRACE(3, "Type: 'type' NumEntries: " << NumEntries << "\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000327 parseTypeConstants(Buf, EndBuf, TypeTab, NumEntries);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000328 } else {
Chris Lattner927b1852003-10-09 20:22:47 +0000329 const Type *Ty = getType(Typ);
330 BCR_TRACE(3, "Type: '" << *Ty << "' NumEntries: " << NumEntries << "\n");
331
Chris Lattner7eadfa12001-10-24 06:21:22 +0000332 for (unsigned i = 0; i < NumEntries; ++i) {
Chris Lattner9e460f22003-10-04 20:00:03 +0000333 Constant *C = parseConstantValue(Buf, EndBuf, Ty);
Chris Lattner52e20b02003-03-19 20:54:26 +0000334 assert(C && "parseConstantValue returned NULL!");
Misha Brukman12c29d12003-09-22 23:38:23 +0000335 BCR_TRACE(4, "Read Constant: '" << *C << "'\n");
Chris Lattnerf0d92732003-10-13 14:34:59 +0000336 unsigned Slot = insertValue(C, Typ, Tab);
Chris Lattner74734132002-08-17 22:01:27 +0000337
338 // If we are reading a function constant table, make sure that we adjust
339 // the slot number to be the real global constant number.
340 //
Chris Lattner52e20b02003-03-19 20:54:26 +0000341 if (&Tab != &ModuleValues && Typ < ModuleValues.size())
342 Slot += ModuleValues[Typ]->size();
Chris Lattner927b1852003-10-09 20:22:47 +0000343 ResolveReferencesToValue(C, Slot);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000344 }
Chris Lattner00950542001-06-06 20:29:01 +0000345 }
346 }
347
Misha Brukman12c29d12003-09-22 23:38:23 +0000348 if (Buf > EndBuf) throw std::string("Read past end of buffer.");
Chris Lattner00950542001-06-06 20:29:01 +0000349}