blob: 097bdbe1266fa6ae31698ca7ced5a9faafddf439 [file] [log] [blame]
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001//===- ReadConst.cpp - Code to constants and constant pools ---------------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
10// This file implements functionality to deserialize constants and entire
11// constant pools.
12//
13// Note that this library should be as fast as possible, reentrant, and
Misha Brukman37f92e22003-09-11 22:34:13 +000014// thread-safe!!
Chris Lattner00950542001-06-06 20:29:01 +000015//
Chris Lattnere9bb2df2001-12-03 22:26:30 +000016//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +000017
Chris Lattner7061dc52001-12-03 18:02:31 +000018#include "ReaderInternals.h"
Chris Lattner00950542001-06-06 20:29:01 +000019#include "llvm/Module.h"
Chris Lattnere4619182002-08-17 22:02:41 +000020#include "llvm/Constants.h"
Chris Lattner1d670cc2001-09-07 16:37:43 +000021#include <algorithm>
Anand Shuklaeea60fc2002-06-25 20:44:04 +000022
Chris Lattner12e64652003-05-22 18:08:30 +000023const Type *BytecodeParser::parseTypeConstant(const unsigned char *&Buf,
24 const unsigned char *EndBuf) {
Chris Lattner00950542001-06-06 20:29:01 +000025 unsigned PrimType;
Misha Brukman12c29d12003-09-22 23:38:23 +000026 if (read_vbr(Buf, EndBuf, PrimType)) throw Error_readvbr;
Chris Lattner00950542001-06-06 20:29:01 +000027
Chris Lattner1d670cc2001-09-07 16:37:43 +000028 const Type *Val = 0;
29 if ((Val = Type::getPrimitiveType((Type::PrimitiveID)PrimType)))
30 return Val;
Chris Lattner00950542001-06-06 20:29:01 +000031
32 switch (PrimType) {
Chris Lattnerc9aa7df2002-03-29 03:51:11 +000033 case Type::FunctionTyID: {
Chris Lattner00950542001-06-06 20:29:01 +000034 unsigned Typ;
Chris Lattner74734132002-08-17 22:01:27 +000035 if (read_vbr(Buf, EndBuf, Typ)) return Val;
Chris Lattner00950542001-06-06 20:29:01 +000036 const Type *RetType = getType(Typ);
Chris Lattner00950542001-06-06 20:29:01 +000037
Chris Lattnere5a57ee2001-07-25 22:47:55 +000038 unsigned NumParams;
Chris Lattner74734132002-08-17 22:01:27 +000039 if (read_vbr(Buf, EndBuf, NumParams)) return Val;
Chris Lattner00950542001-06-06 20:29:01 +000040
Chris Lattner697954c2002-01-20 22:54:45 +000041 std::vector<const Type*> Params;
Chris Lattnere5a57ee2001-07-25 22:47:55 +000042 while (NumParams--) {
Chris Lattner74734132002-08-17 22:01:27 +000043 if (read_vbr(Buf, EndBuf, Typ)) return Val;
Chris Lattner927b1852003-10-09 20:22:47 +000044 Params.push_back(getType(Typ));
Chris Lattner00950542001-06-06 20:29:01 +000045 }
46
Chris Lattner05950c32001-10-13 06:47:01 +000047 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
48 if (isVarArg) Params.pop_back();
49
Chris Lattnerc9aa7df2002-03-29 03:51:11 +000050 return FunctionType::get(RetType, Params, isVarArg);
Chris Lattner00950542001-06-06 20:29:01 +000051 }
52 case Type::ArrayTyID: {
53 unsigned ElTyp;
Chris Lattner74734132002-08-17 22:01:27 +000054 if (read_vbr(Buf, EndBuf, ElTyp)) return Val;
Chris Lattner00950542001-06-06 20:29:01 +000055 const Type *ElementType = getType(ElTyp);
Chris Lattner00950542001-06-06 20:29:01 +000056
Chris Lattner90865152001-12-14 16:30:51 +000057 unsigned NumElements;
Chris Lattner74734132002-08-17 22:01:27 +000058 if (read_vbr(Buf, EndBuf, NumElements)) return Val;
Chris Lattner7eadfa12001-10-24 06:21:22 +000059
60 BCR_TRACE(5, "Array Type Constant #" << ElTyp << " size="
Chris Lattner697954c2002-01-20 22:54:45 +000061 << NumElements << "\n");
Chris Lattner7eadfa12001-10-24 06:21:22 +000062 return ArrayType::get(ElementType, NumElements);
Chris Lattner00950542001-06-06 20:29:01 +000063 }
64 case Type::StructTyID: {
65 unsigned Typ;
Chris Lattner697954c2002-01-20 22:54:45 +000066 std::vector<const Type*> Elements;
Chris Lattner00950542001-06-06 20:29:01 +000067
Chris Lattner74734132002-08-17 22:01:27 +000068 if (read_vbr(Buf, EndBuf, Typ)) return Val;
Chris Lattner00950542001-06-06 20:29:01 +000069 while (Typ) { // List is terminated by void/0 typeid
Chris Lattner927b1852003-10-09 20:22:47 +000070 Elements.push_back(getType(Typ));
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 Lattner927b1852003-10-09 20:22:47 +000080 return PointerType::get(getType(ElTyp));
Chris Lattner00950542001-06-06 20:29:01 +000081 }
82
Chris Lattnerd48d6c72001-10-23 01:53:01 +000083 case Type::OpaqueTyID: {
Chris Lattner7eadfa12001-10-24 06:21:22 +000084 return OpaqueType::get();
Chris Lattnerd48d6c72001-10-23 01:53:01 +000085 }
86
Chris Lattner00950542001-06-06 20:29:01 +000087 default:
Anand Shuklaeea60fc2002-06-25 20:44:04 +000088 std::cerr << __FILE__ << ":" << __LINE__
Chris Lattner194f0a52002-06-30 16:04:37 +000089 << ": Don't know how to deserialize"
90 << " primitive Type " << PrimType << "\n";
Chris Lattner74734132002-08-17 22:01:27 +000091 return Val;
Chris Lattner00950542001-06-06 20:29:01 +000092 }
Chris Lattner1d670cc2001-09-07 16:37:43 +000093}
94
Misha Brukman37f92e22003-09-11 22:34:13 +000095// parseTypeConstants - We have to use this weird code to handle recursive
Chris Lattner1d670cc2001-09-07 16:37:43 +000096// types. We know that recursive types will only reference the current slab of
97// values in the type plane, but they can forward reference types before they
98// have been read. For example, Type #0 might be '{ Ty#1 }' and Type #1 might
99// be 'Ty#0*'. When reading Type #0, type number one doesn't exist. To fix
Misha Brukman37f92e22003-09-11 22:34:13 +0000100// this ugly problem, we pessimistically insert an opaque type for each type we
Chris Lattner1d670cc2001-09-07 16:37:43 +0000101// are about to read. This means that forward references will resolve to
102// something and when we reread the type later, we can replace the opaque type
103// with a new resolved concrete type.
104//
Chris Lattner92940ac2002-04-07 06:11:22 +0000105void debug_type_tables();
Misha Brukman12c29d12003-09-22 23:38:23 +0000106void BytecodeParser::parseTypeConstants(const unsigned char *&Buf,
Chris Lattner12e64652003-05-22 18:08:30 +0000107 const unsigned char *EndBuf,
Chris Lattner1d670cc2001-09-07 16:37:43 +0000108 TypeValuesListTy &Tab,
109 unsigned NumEntries) {
Chris Lattner05950c32001-10-13 06:47:01 +0000110 assert(Tab.size() == 0 && "should not have read type constants in before!");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000111
112 // Insert a bunch of opaque types to be resolved later...
Chris Lattner7eadfa12001-10-24 06:21:22 +0000113 for (unsigned i = 0; i < NumEntries; ++i)
Chris Lattnerc7b6f032003-10-02 20:26:18 +0000114 Tab.push_back(OpaqueType::get());
Chris Lattner1d670cc2001-09-07 16:37:43 +0000115
116 // Loop through reading all of the types. Forward types will make use of the
117 // opaque types just inserted.
118 //
Chris Lattner7eadfa12001-10-24 06:21:22 +0000119 for (unsigned i = 0; i < NumEntries; ++i) {
Chris Lattner05950c32001-10-13 06:47:01 +0000120 const Type *NewTy = parseTypeConstant(Buf, EndBuf), *OldTy = Tab[i].get();
Misha Brukman12c29d12003-09-22 23:38:23 +0000121 if (NewTy == 0) throw std::string("Parsed invalid type.");
Chris Lattner7eadfa12001-10-24 06:21:22 +0000122 BCR_TRACE(4, "#" << i << ": Read Type Constant: '" << NewTy <<
123 "' Replacing: " << OldTy << "\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000124
125 // Don't insertValue the new type... instead we want to replace the opaque
126 // type with the new concrete value...
127 //
128
129 // Refine the abstract type to the new type. This causes all uses of the
130 // abstract type to use the newty. This also will cause the opaque type
131 // to be deleted...
132 //
Chris Lattnera48836b2002-06-05 17:38:28 +0000133 ((DerivedType*)Tab[i].get())->refineAbstractTypeTo(NewTy);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000134
135 // This should have replace the old opaque type with the new type in the
Chris Lattner05950c32001-10-13 06:47:01 +0000136 // value table... or with a preexisting type that was already in the system
137 assert(Tab[i] != OldTy && "refineAbstractType didn't work!");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000138 }
139
140 BCR_TRACE(5, "Resulting types:\n");
Chris Lattner7eadfa12001-10-24 06:21:22 +0000141 for (unsigned i = 0; i < NumEntries; ++i) {
Chris Lattner92940ac2002-04-07 06:11:22 +0000142 BCR_TRACE(5, (void*)Tab[i].get() << " - " << Tab[i].get() << "\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000143 }
Chris Lattner92940ac2002-04-07 06:11:22 +0000144 debug_type_tables();
Chris Lattner00950542001-06-06 20:29:01 +0000145}
146
Chris Lattner1d670cc2001-09-07 16:37:43 +0000147
Chris Lattner9e460f22003-10-04 20:00:03 +0000148Constant *BytecodeParser::parseConstantValue(const unsigned char *&Buf,
149 const unsigned char *EndBuf,
150 const Type *Ty) {
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000151
152 // We must check for a ConstantExpr before switching by type because
153 // a ConstantExpr can be of any type, and has no explicit value.
154 //
155 unsigned isExprNumArgs; // 0 if not expr; numArgs if is expr
Misha Brukman12c29d12003-09-22 23:38:23 +0000156 if (read_vbr(Buf, EndBuf, isExprNumArgs)) throw Error_readvbr;
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000157 if (isExprNumArgs) {
Chris Lattnere8e46052002-07-30 18:54:22 +0000158 // FIXME: Encoding of constant exprs could be much more compact!
159 unsigned Opcode;
160 std::vector<Constant*> ArgVec;
161 ArgVec.reserve(isExprNumArgs);
Misha Brukman12c29d12003-09-22 23:38:23 +0000162 if (read_vbr(Buf, EndBuf, Opcode)) throw Error_readvbr;
Chris Lattnere8e46052002-07-30 18:54:22 +0000163
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000164 // Read the slot number and types of each of the arguments
Chris Lattnere8e46052002-07-30 18:54:22 +0000165 for (unsigned i = 0; i != isExprNumArgs; ++i) {
166 unsigned ArgValSlot, ArgTypeSlot;
Misha Brukman12c29d12003-09-22 23:38:23 +0000167 if (read_vbr(Buf, EndBuf, ArgValSlot)) throw Error_readvbr;
168 if (read_vbr(Buf, EndBuf, ArgTypeSlot)) throw Error_readvbr;
Chris Lattnere8e46052002-07-30 18:54:22 +0000169 const Type *ArgTy = getType(ArgTypeSlot);
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000170
Chris Lattner927b1852003-10-09 20:22:47 +0000171 BCR_TRACE(4, "CE Arg " << i << ": Type: '" << *ArgTy << "' slot: "
Chris Lattnere8e46052002-07-30 18:54:22 +0000172 << ArgValSlot << "\n");
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000173
174 // Get the arg value from its slot if it exists, otherwise a placeholder
Chris Lattnerc9456ca2003-10-09 20:41:16 +0000175 ArgVec.push_back(getConstantValue(ArgTy, ArgValSlot));
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000176 }
177
178 // Construct a ConstantExpr of the appropriate kind
179 if (isExprNumArgs == 1) { // All one-operand expressions
Chris Lattnerad333482002-08-14 18:24:09 +0000180 assert(Opcode == Instruction::Cast);
Chris Lattner9e460f22003-10-04 20:00:03 +0000181 return ConstantExpr::getCast(ArgVec[0], Ty);
Chris Lattnere8e46052002-07-30 18:54:22 +0000182 } else if (Opcode == Instruction::GetElementPtr) { // GetElementPtr
183 std::vector<Constant*> IdxList(ArgVec.begin()+1, ArgVec.end());
Chris Lattner9e460f22003-10-04 20:00:03 +0000184 return ConstantExpr::getGetElementPtr(ArgVec[0], IdxList);
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000185 } else { // All other 2-operand expressions
Chris Lattner9e460f22003-10-04 20:00:03 +0000186 return ConstantExpr::get(Opcode, ArgVec[0], ArgVec[1]);
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000187 }
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000188 }
189
190 // Ok, not an ConstantExpr. We now know how to read the given type...
Chris Lattner00950542001-06-06 20:29:01 +0000191 switch (Ty->getPrimitiveID()) {
192 case Type::BoolTyID: {
193 unsigned Val;
Misha Brukman12c29d12003-09-22 23:38:23 +0000194 if (read_vbr(Buf, EndBuf, Val)) throw Error_readvbr;
195 if (Val != 0 && Val != 1) throw std::string("Invalid boolean value read.");
Chris Lattner9e460f22003-10-04 20:00:03 +0000196 return ConstantBool::get(Val == 1);
Chris Lattner00950542001-06-06 20:29:01 +0000197 }
198
199 case Type::UByteTyID: // Unsigned integer types...
200 case Type::UShortTyID:
201 case Type::UIntTyID: {
202 unsigned Val;
Misha Brukman12c29d12003-09-22 23:38:23 +0000203 if (read_vbr(Buf, EndBuf, Val)) throw Error_readvbr;
204 if (!ConstantUInt::isValueValidForType(Ty, Val))
205 throw std::string("Invalid unsigned byte/short/int read.");
Chris Lattner9e460f22003-10-04 20:00:03 +0000206 return ConstantUInt::get(Ty, Val);
Chris Lattner00950542001-06-06 20:29:01 +0000207 }
208
209 case Type::ULongTyID: {
210 uint64_t Val;
Misha Brukman12c29d12003-09-22 23:38:23 +0000211 if (read_vbr(Buf, EndBuf, Val)) throw Error_readvbr;
Chris Lattner9e460f22003-10-04 20:00:03 +0000212 return ConstantUInt::get(Ty, Val);
Chris Lattner00950542001-06-06 20:29:01 +0000213 }
214
Chris Lattner8017a752003-05-12 15:13:52 +0000215 case Type::SByteTyID: // Signed integer types...
Chris Lattner00950542001-06-06 20:29:01 +0000216 case Type::ShortTyID:
217 case Type::IntTyID: {
Chris Lattner8017a752003-05-12 15:13:52 +0000218 case Type::LongTyID:
Chris Lattner00950542001-06-06 20:29:01 +0000219 int64_t Val;
Misha Brukman12c29d12003-09-22 23:38:23 +0000220 if (read_vbr(Buf, EndBuf, Val)) throw Error_readvbr;
221 if (!ConstantSInt::isValueValidForType(Ty, Val))
222 throw std::string("Invalid signed byte/short/int/long read.");
Chris Lattner9e460f22003-10-04 20:00:03 +0000223 return ConstantSInt::get(Ty, Val);
Chris Lattner00950542001-06-06 20:29:01 +0000224 }
225
Chris Lattnera1375302001-07-15 00:17:18 +0000226 case Type::FloatTyID: {
227 float F;
Misha Brukman12c29d12003-09-22 23:38:23 +0000228 if (input_data(Buf, EndBuf, &F, &F+1)) throw Error_inputdata;
Chris Lattner9e460f22003-10-04 20:00:03 +0000229 return ConstantFP::get(Ty, F);
Chris Lattnera1375302001-07-15 00:17:18 +0000230 }
231
232 case Type::DoubleTyID: {
233 double Val;
Misha Brukman12c29d12003-09-22 23:38:23 +0000234 if (input_data(Buf, EndBuf, &Val, &Val+1)) throw Error_inputdata;
Chris Lattner9e460f22003-10-04 20:00:03 +0000235 return ConstantFP::get(Ty, Val);
Chris Lattnera1375302001-07-15 00:17:18 +0000236 }
237
Chris Lattner00950542001-06-06 20:29:01 +0000238 case Type::TypeTyID:
Chris Lattner9e460f22003-10-04 20:00:03 +0000239 throw std::string("Type constants shouldn't live in constant table!");
Chris Lattner00950542001-06-06 20:29:01 +0000240
241 case Type::ArrayTyID: {
Chris Lattner949a3622003-07-23 15:30:06 +0000242 const ArrayType *AT = cast<ArrayType>(Ty);
Chris Lattner90865152001-12-14 16:30:51 +0000243 unsigned NumElements = AT->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +0000244
Chris Lattner697954c2002-01-20 22:54:45 +0000245 std::vector<Constant*> Elements;
Chris Lattner00950542001-06-06 20:29:01 +0000246 while (NumElements--) { // Read all of the elements of the constant.
247 unsigned Slot;
Misha Brukman12c29d12003-09-22 23:38:23 +0000248 if (read_vbr(Buf, EndBuf, Slot)) throw Error_readvbr;
Chris Lattnerc9456ca2003-10-09 20:41:16 +0000249 Elements.push_back(getConstantValue(AT->getElementType(), Slot));
Chris Lattner00950542001-06-06 20:29:01 +0000250 }
Chris Lattner9e460f22003-10-04 20:00:03 +0000251 return ConstantArray::get(AT, Elements);
Chris Lattner00950542001-06-06 20:29:01 +0000252 }
253
254 case Type::StructTyID: {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000255 const StructType *ST = cast<StructType>(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000256 const StructType::ElementTypes &ET = ST->getElementTypes();
257
Chris Lattner697954c2002-01-20 22:54:45 +0000258 std::vector<Constant *> Elements;
Chris Lattner00950542001-06-06 20:29:01 +0000259 for (unsigned i = 0; i < ET.size(); ++i) {
260 unsigned Slot;
Misha Brukman12c29d12003-09-22 23:38:23 +0000261 if (read_vbr(Buf, EndBuf, Slot)) throw Error_readvbr;
Chris Lattnerc9456ca2003-10-09 20:41:16 +0000262 Elements.push_back(getConstantValue(ET[i], Slot));
Chris Lattner00950542001-06-06 20:29:01 +0000263 }
264
Chris Lattner9e460f22003-10-04 20:00:03 +0000265 return ConstantStruct::get(ST, Elements);
Chris Lattner00950542001-06-06 20:29:01 +0000266 }
267
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000268 case Type::PointerTyID: { // ConstantPointerRef value...
Chris Lattner949a3622003-07-23 15:30:06 +0000269 const PointerType *PT = cast<PointerType>(Ty);
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000270 unsigned Slot;
271 if (read_vbr(Buf, EndBuf, Slot)) throw Error_readvbr;
272 BCR_TRACE(4, "CPR: Type: '" << Ty << "' slot: " << Slot << "\n");
273
274 // Check to see if we have already read this global variable...
275 Value *Val = getValue(PT, Slot, false);
276 GlobalValue *GV;
277 if (Val) {
278 if (!(GV = dyn_cast<GlobalValue>(Val)))
279 throw std::string("Value of ConstantPointerRef not in ValueTable!");
280 BCR_TRACE(5, "Value Found in ValueTable!\n");
281 } else if (RevisionNum > 0) {
282 // Revision #0 could have forward references to globals that were weird.
283 // We got rid of this in subsequent revs.
284 throw std::string("Forward references to globals not allowed.");
285 } else { // Nope... find or create a forward ref. for it
286 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(PT, Slot));
287
288 if (I != GlobalRefs.end()) {
289 BCR_TRACE(5, "Previous forward ref found!\n");
290 GV = cast<GlobalValue>(I->second);
291 } else {
292 BCR_TRACE(5, "Creating new forward ref to a global variable!\n");
293
294 // Create a placeholder for the global variable reference...
295 GlobalVariable *GVar =
296 new GlobalVariable(PT->getElementType(), false,
297 GlobalValue::InternalLinkage);
298
299 // Keep track of the fact that we have a forward ref to recycle it
300 GlobalRefs.insert(std::make_pair(std::make_pair(PT, Slot), GVar));
301
302 // Must temporarily push this value into the module table...
303 TheModule->getGlobalList().push_back(GVar);
304 GV = GVar;
Chris Lattner05950c32001-10-13 06:47:01 +0000305 }
Chris Lattner05950c32001-10-13 06:47:01 +0000306 }
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000307
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000308 return ConstantPointerRef::get(GV);
Chris Lattner1a1cb112001-09-30 22:46:54 +0000309 }
310
Chris Lattner00950542001-06-06 20:29:01 +0000311 default:
Misha Brukman12c29d12003-09-22 23:38:23 +0000312 throw std::string("Don't know how to deserialize constant value of type '"+
Chris Lattner9e460f22003-10-04 20:00:03 +0000313 Ty->getDescription());
Chris Lattner00950542001-06-06 20:29:01 +0000314 }
Chris Lattner00950542001-06-06 20:29:01 +0000315}
316
Misha Brukman12c29d12003-09-22 23:38:23 +0000317void BytecodeParser::ParseGlobalTypes(const unsigned char *&Buf,
Chris Lattner12e64652003-05-22 18:08:30 +0000318 const unsigned char *EndBuf) {
Chris Lattner52e20b02003-03-19 20:54:26 +0000319 ValueTable T;
Misha Brukman12c29d12003-09-22 23:38:23 +0000320 ParseConstantPool(Buf, EndBuf, T, ModuleTypeValues);
Chris Lattner52e20b02003-03-19 20:54:26 +0000321}
322
Misha Brukman12c29d12003-09-22 23:38:23 +0000323void BytecodeParser::ParseConstantPool(const unsigned char *&Buf,
Chris Lattner12e64652003-05-22 18:08:30 +0000324 const unsigned char *EndBuf,
Misha Brukman12c29d12003-09-22 23:38:23 +0000325 ValueTable &Tab,
326 TypeValuesListTy &TypeTab) {
Chris Lattner00950542001-06-06 20:29:01 +0000327 while (Buf < EndBuf) {
328 unsigned NumEntries, Typ;
329
330 if (read_vbr(Buf, EndBuf, NumEntries) ||
Misha Brukman12c29d12003-09-22 23:38:23 +0000331 read_vbr(Buf, EndBuf, Typ)) throw Error_readvbr;
Chris Lattner1d670cc2001-09-07 16:37:43 +0000332 if (Typ == Type::TypeTyID) {
Chris Lattner927b1852003-10-09 20:22:47 +0000333 BCR_TRACE(3, "Type: 'type' NumEntries: " << NumEntries << "\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000334 parseTypeConstants(Buf, EndBuf, TypeTab, NumEntries);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000335 } else {
Chris Lattner927b1852003-10-09 20:22:47 +0000336 const Type *Ty = getType(Typ);
337 BCR_TRACE(3, "Type: '" << *Ty << "' NumEntries: " << NumEntries << "\n");
338
Chris Lattner7eadfa12001-10-24 06:21:22 +0000339 for (unsigned i = 0; i < NumEntries; ++i) {
Chris Lattner9e460f22003-10-04 20:00:03 +0000340 Constant *C = parseConstantValue(Buf, EndBuf, Ty);
Chris Lattner52e20b02003-03-19 20:54:26 +0000341 assert(C && "parseConstantValue returned NULL!");
Misha Brukman12c29d12003-09-22 23:38:23 +0000342 BCR_TRACE(4, "Read Constant: '" << *C << "'\n");
Chris Lattnerf0d92732003-10-13 14:34:59 +0000343 unsigned Slot = insertValue(C, Typ, Tab);
Chris Lattner74734132002-08-17 22:01:27 +0000344
345 // If we are reading a function constant table, make sure that we adjust
346 // the slot number to be the real global constant number.
347 //
Chris Lattner52e20b02003-03-19 20:54:26 +0000348 if (&Tab != &ModuleValues && Typ < ModuleValues.size())
349 Slot += ModuleValues[Typ]->size();
Chris Lattner927b1852003-10-09 20:22:47 +0000350 ResolveReferencesToValue(C, Slot);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000351 }
Chris Lattner00950542001-06-06 20:29:01 +0000352 }
353 }
354
Misha Brukman12c29d12003-09-22 23:38:23 +0000355 if (Buf > EndBuf) throw std::string("Read past end of buffer.");
Chris Lattner00950542001-06-06 20:29:01 +0000356}