blob: b4a219df6b2005ee148413c88198a3f4bb79d140 [file] [log] [blame]
Chris Lattner89e02532004-01-18 21:08:15 +00001//===- ConstantReader.cpp - Code to constants and types ====---------------===//
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//
Chris Lattner89e02532004-01-18 21:08:15 +000010// This file implements functionality to deserialize constants and types from
11// bytecode files.
Chris Lattner00950542001-06-06 20:29:01 +000012//
Chris Lattnere9bb2df2001-12-03 22:26:30 +000013//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +000014
Chris Lattner7061dc52001-12-03 18:02:31 +000015#include "ReaderInternals.h"
Chris Lattner00950542001-06-06 20:29:01 +000016#include "llvm/Module.h"
Chris Lattnere4619182002-08-17 22:02:41 +000017#include "llvm/Constants.h"
Chris Lattner1d670cc2001-09-07 16:37:43 +000018#include <algorithm>
Chris Lattnerebfb9f42003-11-19 17:17:36 +000019using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000020
Chris Lattner12e64652003-05-22 18:08:30 +000021const Type *BytecodeParser::parseTypeConstant(const unsigned char *&Buf,
22 const unsigned char *EndBuf) {
Chris Lattner7969dc22004-01-15 06:13:09 +000023 unsigned PrimType = read_vbr_uint(Buf, EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +000024
Chris Lattner1d670cc2001-09-07 16:37:43 +000025 const Type *Val = 0;
26 if ((Val = Type::getPrimitiveType((Type::PrimitiveID)PrimType)))
27 return Val;
Chris Lattner00950542001-06-06 20:29:01 +000028
29 switch (PrimType) {
Chris Lattnerc9aa7df2002-03-29 03:51:11 +000030 case Type::FunctionTyID: {
Chris Lattner7969dc22004-01-15 06:13:09 +000031 const Type *RetType = getType(read_vbr_uint(Buf, EndBuf));
Chris Lattner00950542001-06-06 20:29:01 +000032
Chris Lattner7969dc22004-01-15 06:13:09 +000033 unsigned NumParams = read_vbr_uint(Buf, EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +000034
Chris Lattner697954c2002-01-20 22:54:45 +000035 std::vector<const Type*> Params;
Chris Lattner7969dc22004-01-15 06:13:09 +000036 while (NumParams--)
37 Params.push_back(getType(read_vbr_uint(Buf, EndBuf)));
Chris Lattner00950542001-06-06 20:29:01 +000038
Chris Lattner05950c32001-10-13 06:47:01 +000039 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
40 if (isVarArg) Params.pop_back();
41
Chris Lattnerc9aa7df2002-03-29 03:51:11 +000042 return FunctionType::get(RetType, Params, isVarArg);
Chris Lattner00950542001-06-06 20:29:01 +000043 }
44 case Type::ArrayTyID: {
Chris Lattner7969dc22004-01-15 06:13:09 +000045 unsigned ElTyp = read_vbr_uint(Buf, EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +000046 const Type *ElementType = getType(ElTyp);
Chris Lattner00950542001-06-06 20:29:01 +000047
Chris Lattner7969dc22004-01-15 06:13:09 +000048 unsigned NumElements = read_vbr_uint(Buf, EndBuf);
Chris Lattner7eadfa12001-10-24 06:21:22 +000049
50 BCR_TRACE(5, "Array Type Constant #" << ElTyp << " size="
Chris Lattner697954c2002-01-20 22:54:45 +000051 << NumElements << "\n");
Chris Lattner7eadfa12001-10-24 06:21:22 +000052 return ArrayType::get(ElementType, NumElements);
Chris Lattner00950542001-06-06 20:29:01 +000053 }
54 case Type::StructTyID: {
Chris Lattner697954c2002-01-20 22:54:45 +000055 std::vector<const Type*> Elements;
Chris Lattner7969dc22004-01-15 06:13:09 +000056 unsigned Typ = read_vbr_uint(Buf, EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +000057 while (Typ) { // List is terminated by void/0 typeid
Chris Lattner927b1852003-10-09 20:22:47 +000058 Elements.push_back(getType(Typ));
Chris Lattner7969dc22004-01-15 06:13:09 +000059 Typ = read_vbr_uint(Buf, EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +000060 }
61
Chris Lattner7eadfa12001-10-24 06:21:22 +000062 return StructType::get(Elements);
Chris Lattner00950542001-06-06 20:29:01 +000063 }
64 case Type::PointerTyID: {
Chris Lattner7969dc22004-01-15 06:13:09 +000065 unsigned ElTyp = read_vbr_uint(Buf, EndBuf);
Chris Lattner6c51a362003-09-03 20:25:27 +000066 BCR_TRACE(5, "Pointer Type Constant #" << ElTyp << "\n");
Chris Lattner927b1852003-10-09 20:22:47 +000067 return PointerType::get(getType(ElTyp));
Chris Lattner00950542001-06-06 20:29:01 +000068 }
69
Chris Lattnerd48d6c72001-10-23 01:53:01 +000070 case Type::OpaqueTyID: {
Chris Lattner7eadfa12001-10-24 06:21:22 +000071 return OpaqueType::get();
Chris Lattnerd48d6c72001-10-23 01:53:01 +000072 }
73
Chris Lattner00950542001-06-06 20:29:01 +000074 default:
Anand Shuklaeea60fc2002-06-25 20:44:04 +000075 std::cerr << __FILE__ << ":" << __LINE__
Chris Lattner194f0a52002-06-30 16:04:37 +000076 << ": Don't know how to deserialize"
77 << " primitive Type " << PrimType << "\n";
Chris Lattner74734132002-08-17 22:01:27 +000078 return Val;
Chris Lattner00950542001-06-06 20:29:01 +000079 }
Chris Lattner1d670cc2001-09-07 16:37:43 +000080}
81
Misha Brukman37f92e22003-09-11 22:34:13 +000082// parseTypeConstants - We have to use this weird code to handle recursive
Chris Lattner1d670cc2001-09-07 16:37:43 +000083// types. We know that recursive types will only reference the current slab of
84// values in the type plane, but they can forward reference types before they
85// have been read. For example, Type #0 might be '{ Ty#1 }' and Type #1 might
86// be 'Ty#0*'. When reading Type #0, type number one doesn't exist. To fix
Misha Brukman37f92e22003-09-11 22:34:13 +000087// this ugly problem, we pessimistically insert an opaque type for each type we
Chris Lattner1d670cc2001-09-07 16:37:43 +000088// are about to read. This means that forward references will resolve to
89// something and when we reread the type later, we can replace the opaque type
90// with a new resolved concrete type.
91//
Misha Brukman12c29d12003-09-22 23:38:23 +000092void BytecodeParser::parseTypeConstants(const unsigned char *&Buf,
Chris Lattner12e64652003-05-22 18:08:30 +000093 const unsigned char *EndBuf,
Chris Lattner1d670cc2001-09-07 16:37:43 +000094 TypeValuesListTy &Tab,
95 unsigned NumEntries) {
Chris Lattner05950c32001-10-13 06:47:01 +000096 assert(Tab.size() == 0 && "should not have read type constants in before!");
Chris Lattner1d670cc2001-09-07 16:37:43 +000097
98 // Insert a bunch of opaque types to be resolved later...
Chris Lattnerebfb9f42003-11-19 17:17:36 +000099 Tab.reserve(NumEntries);
Chris Lattnerddceeb72003-12-26 06:16:00 +0000100 for (unsigned i = 0; i != NumEntries; ++i)
Chris Lattnerc7b6f032003-10-02 20:26:18 +0000101 Tab.push_back(OpaqueType::get());
Chris Lattner1d670cc2001-09-07 16:37:43 +0000102
103 // Loop through reading all of the types. Forward types will make use of the
104 // opaque types just inserted.
105 //
Chris Lattnerddceeb72003-12-26 06:16:00 +0000106 for (unsigned i = 0; i != NumEntries; ++i) {
Chris Lattner05950c32001-10-13 06:47:01 +0000107 const Type *NewTy = parseTypeConstant(Buf, EndBuf), *OldTy = Tab[i].get();
Chris Lattnerddceeb72003-12-26 06:16:00 +0000108 if (NewTy == 0) throw std::string("Couldn't parse type!");
Chris Lattner7eadfa12001-10-24 06:21:22 +0000109 BCR_TRACE(4, "#" << i << ": Read Type Constant: '" << NewTy <<
110 "' Replacing: " << OldTy << "\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000111
112 // Don't insertValue the new type... instead we want to replace the opaque
113 // type with the new concrete value...
114 //
115
116 // Refine the abstract type to the new type. This causes all uses of the
Chris Lattnerddceeb72003-12-26 06:16:00 +0000117 // abstract type to use NewTy. This also will cause the opaque type to be
118 // deleted...
Chris Lattner1d670cc2001-09-07 16:37:43 +0000119 //
Chris Lattnerddceeb72003-12-26 06:16:00 +0000120 cast<DerivedType>(const_cast<Type*>(OldTy))->refineAbstractTypeTo(NewTy);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000121
122 // This should have replace the old opaque type with the new type in the
Chris Lattner05950c32001-10-13 06:47:01 +0000123 // value table... or with a preexisting type that was already in the system
124 assert(Tab[i] != OldTy && "refineAbstractType didn't work!");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000125 }
126
127 BCR_TRACE(5, "Resulting types:\n");
Chris Lattner7eadfa12001-10-24 06:21:22 +0000128 for (unsigned i = 0; i < NumEntries; ++i) {
Chris Lattner92940ac2002-04-07 06:11:22 +0000129 BCR_TRACE(5, (void*)Tab[i].get() << " - " << Tab[i].get() << "\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000130 }
Chris Lattner00950542001-06-06 20:29:01 +0000131}
132
Chris Lattner1d670cc2001-09-07 16:37:43 +0000133
Chris Lattner9e460f22003-10-04 20:00:03 +0000134Constant *BytecodeParser::parseConstantValue(const unsigned char *&Buf,
135 const unsigned char *EndBuf,
Chris Lattnerebfb9f42003-11-19 17:17:36 +0000136 unsigned TypeID) {
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000137
138 // We must check for a ConstantExpr before switching by type because
139 // a ConstantExpr can be of any type, and has no explicit value.
140 //
Chris Lattner7969dc22004-01-15 06:13:09 +0000141 // 0 if not expr; numArgs if is expr
142 unsigned isExprNumArgs = read_vbr_uint(Buf, EndBuf);
143
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000144 if (isExprNumArgs) {
Chris Lattnere8e46052002-07-30 18:54:22 +0000145 // FIXME: Encoding of constant exprs could be much more compact!
Chris Lattnere8e46052002-07-30 18:54:22 +0000146 std::vector<Constant*> ArgVec;
147 ArgVec.reserve(isExprNumArgs);
Chris Lattner7969dc22004-01-15 06:13:09 +0000148 unsigned Opcode = read_vbr_uint(Buf, EndBuf);
149
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000150 // Read the slot number and types of each of the arguments
Chris Lattnere8e46052002-07-30 18:54:22 +0000151 for (unsigned i = 0; i != isExprNumArgs; ++i) {
Chris Lattner7969dc22004-01-15 06:13:09 +0000152 unsigned ArgValSlot = read_vbr_uint(Buf, EndBuf);
153 unsigned ArgTypeSlot = read_vbr_uint(Buf, EndBuf);
Chris Lattner1c3673b2003-11-19 06:01:12 +0000154 BCR_TRACE(4, "CE Arg " << i << ": Type: '" << *getType(ArgTypeSlot)
155 << "' slot: " << ArgValSlot << "\n");
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000156
157 // Get the arg value from its slot if it exists, otherwise a placeholder
Chris Lattner1c3673b2003-11-19 06:01:12 +0000158 ArgVec.push_back(getConstantValue(ArgTypeSlot, ArgValSlot));
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000159 }
160
161 // Construct a ConstantExpr of the appropriate kind
162 if (isExprNumArgs == 1) { // All one-operand expressions
Chris Lattnerad333482002-08-14 18:24:09 +0000163 assert(Opcode == Instruction::Cast);
Chris Lattnerebfb9f42003-11-19 17:17:36 +0000164 return ConstantExpr::getCast(ArgVec[0], getType(TypeID));
Chris Lattnere8e46052002-07-30 18:54:22 +0000165 } else if (Opcode == Instruction::GetElementPtr) { // GetElementPtr
166 std::vector<Constant*> IdxList(ArgVec.begin()+1, ArgVec.end());
Chris Lattner9e460f22003-10-04 20:00:03 +0000167 return ConstantExpr::getGetElementPtr(ArgVec[0], IdxList);
Chris Lattner1e490ba2004-03-31 02:53:59 +0000168 } else if (Opcode == Instruction::Select) {
169 assert(ArgVec.size() == 3);
170 return ConstantExpr::getSelect(ArgVec[0], ArgVec[1], ArgVec[2]);
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000171 } else { // All other 2-operand expressions
Chris Lattner9e460f22003-10-04 20:00:03 +0000172 return ConstantExpr::get(Opcode, ArgVec[0], ArgVec[1]);
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000173 }
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000174 }
175
176 // Ok, not an ConstantExpr. We now know how to read the given type...
Chris Lattnerebfb9f42003-11-19 17:17:36 +0000177 const Type *Ty = getType(TypeID);
Chris Lattner00950542001-06-06 20:29:01 +0000178 switch (Ty->getPrimitiveID()) {
179 case Type::BoolTyID: {
Chris Lattner7969dc22004-01-15 06:13:09 +0000180 unsigned Val = read_vbr_uint(Buf, EndBuf);
Misha Brukman12c29d12003-09-22 23:38:23 +0000181 if (Val != 0 && Val != 1) throw std::string("Invalid boolean value read.");
Chris Lattner9e460f22003-10-04 20:00:03 +0000182 return ConstantBool::get(Val == 1);
Chris Lattner00950542001-06-06 20:29:01 +0000183 }
184
185 case Type::UByteTyID: // Unsigned integer types...
186 case Type::UShortTyID:
187 case Type::UIntTyID: {
Chris Lattner7969dc22004-01-15 06:13:09 +0000188 unsigned Val = read_vbr_uint(Buf, EndBuf);
Misha Brukman12c29d12003-09-22 23:38:23 +0000189 if (!ConstantUInt::isValueValidForType(Ty, Val))
190 throw std::string("Invalid unsigned byte/short/int read.");
Chris Lattner9e460f22003-10-04 20:00:03 +0000191 return ConstantUInt::get(Ty, Val);
Chris Lattner00950542001-06-06 20:29:01 +0000192 }
193
194 case Type::ULongTyID: {
Chris Lattner7969dc22004-01-15 06:13:09 +0000195 return ConstantUInt::get(Ty, read_vbr_uint64(Buf, EndBuf));
Chris Lattner00950542001-06-06 20:29:01 +0000196 }
197
Chris Lattner8017a752003-05-12 15:13:52 +0000198 case Type::SByteTyID: // Signed integer types...
Chris Lattner00950542001-06-06 20:29:01 +0000199 case Type::ShortTyID:
200 case Type::IntTyID: {
Chris Lattner8017a752003-05-12 15:13:52 +0000201 case Type::LongTyID:
Chris Lattner7969dc22004-01-15 06:13:09 +0000202 int64_t Val = read_vbr_int64(Buf, EndBuf);
Misha Brukman12c29d12003-09-22 23:38:23 +0000203 if (!ConstantSInt::isValueValidForType(Ty, Val))
204 throw std::string("Invalid signed byte/short/int/long read.");
Chris Lattner9e460f22003-10-04 20:00:03 +0000205 return ConstantSInt::get(Ty, Val);
Chris Lattner00950542001-06-06 20:29:01 +0000206 }
207
Chris Lattnera1375302001-07-15 00:17:18 +0000208 case Type::FloatTyID: {
209 float F;
Chris Lattner7969dc22004-01-15 06:13:09 +0000210 input_data(Buf, EndBuf, &F, &F+1);
Chris Lattner9e460f22003-10-04 20:00:03 +0000211 return ConstantFP::get(Ty, F);
Chris Lattnera1375302001-07-15 00:17:18 +0000212 }
213
214 case Type::DoubleTyID: {
215 double Val;
Chris Lattner7969dc22004-01-15 06:13:09 +0000216 input_data(Buf, EndBuf, &Val, &Val+1);
Chris Lattner9e460f22003-10-04 20:00:03 +0000217 return ConstantFP::get(Ty, Val);
Chris Lattnera1375302001-07-15 00:17:18 +0000218 }
219
Chris Lattner00950542001-06-06 20:29:01 +0000220 case Type::TypeTyID:
Chris Lattner9e460f22003-10-04 20:00:03 +0000221 throw std::string("Type constants shouldn't live in constant table!");
Chris Lattner00950542001-06-06 20:29:01 +0000222
223 case Type::ArrayTyID: {
Chris Lattner949a3622003-07-23 15:30:06 +0000224 const ArrayType *AT = cast<ArrayType>(Ty);
Chris Lattner90865152001-12-14 16:30:51 +0000225 unsigned NumElements = AT->getNumElements();
Chris Lattner1c3673b2003-11-19 06:01:12 +0000226 unsigned TypeSlot = getTypeSlot(AT->getElementType());
Chris Lattner697954c2002-01-20 22:54:45 +0000227 std::vector<Constant*> Elements;
Chris Lattner7969dc22004-01-15 06:13:09 +0000228 Elements.reserve(NumElements);
229 while (NumElements--) // Read all of the elements of the constant.
230 Elements.push_back(getConstantValue(TypeSlot,
231 read_vbr_uint(Buf, EndBuf)));
Chris Lattner9e460f22003-10-04 20:00:03 +0000232 return ConstantArray::get(AT, Elements);
Chris Lattner00950542001-06-06 20:29:01 +0000233 }
234
235 case Type::StructTyID: {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000236 const StructType *ST = cast<StructType>(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000237
Chris Lattner697954c2002-01-20 22:54:45 +0000238 std::vector<Constant *> Elements;
Chris Lattnerd21cd802004-02-09 04:37:31 +0000239 Elements.reserve(ST->getNumElements());
240 for (unsigned i = 0; i != ST->getNumElements(); ++i)
241 Elements.push_back(getConstantValue(ST->getElementType(i),
242 read_vbr_uint(Buf, EndBuf)));
Chris Lattner00950542001-06-06 20:29:01 +0000243
Chris Lattner9e460f22003-10-04 20:00:03 +0000244 return ConstantStruct::get(ST, Elements);
Chris Lattner00950542001-06-06 20:29:01 +0000245 }
246
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000247 case Type::PointerTyID: { // ConstantPointerRef value...
Chris Lattner949a3622003-07-23 15:30:06 +0000248 const PointerType *PT = cast<PointerType>(Ty);
Chris Lattner7969dc22004-01-15 06:13:09 +0000249 unsigned Slot = read_vbr_uint(Buf, EndBuf);
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000250 BCR_TRACE(4, "CPR: Type: '" << Ty << "' slot: " << Slot << "\n");
251
252 // Check to see if we have already read this global variable...
Chris Lattnerebfb9f42003-11-19 17:17:36 +0000253 Value *Val = getValue(TypeID, Slot, false);
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000254 GlobalValue *GV;
255 if (Val) {
256 if (!(GV = dyn_cast<GlobalValue>(Val)))
257 throw std::string("Value of ConstantPointerRef not in ValueTable!");
258 BCR_TRACE(5, "Value Found in ValueTable!\n");
Chris Lattnerebfb9f42003-11-19 17:17:36 +0000259 } else {
260 throw std::string("Forward references are not allowed here.");
Chris Lattner05950c32001-10-13 06:47:01 +0000261 }
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000262
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000263 return ConstantPointerRef::get(GV);
Chris Lattner1a1cb112001-09-30 22:46:54 +0000264 }
265
Chris Lattner00950542001-06-06 20:29:01 +0000266 default:
Misha Brukman12c29d12003-09-22 23:38:23 +0000267 throw std::string("Don't know how to deserialize constant value of type '"+
Chris Lattner9e460f22003-10-04 20:00:03 +0000268 Ty->getDescription());
Chris Lattner00950542001-06-06 20:29:01 +0000269 }
Chris Lattner00950542001-06-06 20:29:01 +0000270}
271
Misha Brukman12c29d12003-09-22 23:38:23 +0000272void BytecodeParser::ParseGlobalTypes(const unsigned char *&Buf,
Chris Lattner12e64652003-05-22 18:08:30 +0000273 const unsigned char *EndBuf) {
Chris Lattner52e20b02003-03-19 20:54:26 +0000274 ValueTable T;
Misha Brukman12c29d12003-09-22 23:38:23 +0000275 ParseConstantPool(Buf, EndBuf, T, ModuleTypeValues);
Chris Lattner52e20b02003-03-19 20:54:26 +0000276}
277
Chris Lattner9e893e82004-01-14 23:35:21 +0000278void BytecodeParser::parseStringConstants(const unsigned char *&Buf,
279 const unsigned char *EndBuf,
280 unsigned NumEntries, ValueTable &Tab){
Chris Lattner9e893e82004-01-14 23:35:21 +0000281 for (; NumEntries; --NumEntries) {
Chris Lattner7969dc22004-01-15 06:13:09 +0000282 unsigned Typ = read_vbr_uint(Buf, EndBuf);
Chris Lattner9e893e82004-01-14 23:35:21 +0000283 const Type *Ty = getType(Typ);
284 if (!isa<ArrayType>(Ty))
285 throw std::string("String constant data invalid!");
286
287 const ArrayType *ATy = cast<ArrayType>(Ty);
288 if (ATy->getElementType() != Type::SByteTy &&
289 ATy->getElementType() != Type::UByteTy)
290 throw std::string("String constant data invalid!");
291
292 // Read character data. The type tells us how long the string is.
293 char Data[ATy->getNumElements()];
Chris Lattner7969dc22004-01-15 06:13:09 +0000294 input_data(Buf, EndBuf, Data, Data+ATy->getNumElements());
Chris Lattner9e893e82004-01-14 23:35:21 +0000295
296 std::vector<Constant*> Elements(ATy->getNumElements());
297 if (ATy->getElementType() == Type::SByteTy)
298 for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i)
Chris Lattnerff47e7d2004-01-15 18:39:06 +0000299 Elements[i] = ConstantSInt::get(Type::SByteTy, (signed char)Data[i]);
Chris Lattner9e893e82004-01-14 23:35:21 +0000300 else
301 for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i)
Chris Lattnerff47e7d2004-01-15 18:39:06 +0000302 Elements[i] = ConstantUInt::get(Type::UByteTy, (unsigned char)Data[i]);
Chris Lattner9e893e82004-01-14 23:35:21 +0000303
304 // Create the constant, inserting it as needed.
305 Constant *C = ConstantArray::get(ATy, Elements);
306 unsigned Slot = insertValue(C, Typ, Tab);
307 ResolveReferencesToConstant(C, Slot);
308 }
309}
310
311
Misha Brukman12c29d12003-09-22 23:38:23 +0000312void BytecodeParser::ParseConstantPool(const unsigned char *&Buf,
Chris Lattner12e64652003-05-22 18:08:30 +0000313 const unsigned char *EndBuf,
Misha Brukman12c29d12003-09-22 23:38:23 +0000314 ValueTable &Tab,
315 TypeValuesListTy &TypeTab) {
Chris Lattner00950542001-06-06 20:29:01 +0000316 while (Buf < EndBuf) {
Chris Lattner7969dc22004-01-15 06:13:09 +0000317 unsigned NumEntries = read_vbr_uint(Buf, EndBuf);
318 unsigned Typ = read_vbr_uint(Buf, EndBuf);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000319 if (Typ == Type::TypeTyID) {
Chris Lattner927b1852003-10-09 20:22:47 +0000320 BCR_TRACE(3, "Type: 'type' NumEntries: " << NumEntries << "\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000321 parseTypeConstants(Buf, EndBuf, TypeTab, NumEntries);
Chris Lattner9e893e82004-01-14 23:35:21 +0000322 } else if (Typ == Type::VoidTyID) {
323 assert(&Tab == &ModuleValues && "Cannot read strings in functions!");
324 parseStringConstants(Buf, EndBuf, NumEntries, Tab);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000325 } else {
Chris Lattnerebfb9f42003-11-19 17:17:36 +0000326 BCR_TRACE(3, "Type: '" << *getType(Typ) << "' NumEntries: "
327 << NumEntries << "\n");
Chris Lattner927b1852003-10-09 20:22:47 +0000328
Chris Lattner7eadfa12001-10-24 06:21:22 +0000329 for (unsigned i = 0; i < NumEntries; ++i) {
Chris Lattnerebfb9f42003-11-19 17:17:36 +0000330 Constant *C = parseConstantValue(Buf, EndBuf, Typ);
Chris Lattner52e20b02003-03-19 20:54:26 +0000331 assert(C && "parseConstantValue returned NULL!");
Misha Brukman12c29d12003-09-22 23:38:23 +0000332 BCR_TRACE(4, "Read Constant: '" << *C << "'\n");
Chris Lattnerf0d92732003-10-13 14:34:59 +0000333 unsigned Slot = insertValue(C, Typ, Tab);
Chris Lattner74734132002-08-17 22:01:27 +0000334
335 // If we are reading a function constant table, make sure that we adjust
336 // the slot number to be the real global constant number.
337 //
Chris Lattnerc2f4b112003-11-19 17:20:42 +0000338 if (&Tab != &ModuleValues && Typ < ModuleValues.size() &&
339 ModuleValues[Typ])
Chris Lattner52e20b02003-03-19 20:54:26 +0000340 Slot += ModuleValues[Typ]->size();
Chris Lattnerebfb9f42003-11-19 17:17:36 +0000341 ResolveReferencesToConstant(C, Slot);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000342 }
Chris Lattner00950542001-06-06 20:29:01 +0000343 }
344 }
345
Misha Brukman12c29d12003-09-22 23:38:23 +0000346 if (Buf > EndBuf) throw std::string("Read past end of buffer.");
Chris Lattner00950542001-06-06 20:29:01 +0000347}