blob: c8e9feb0bef1dbf7314338d7dc400d727d52085a [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>
Chris Lattnerebfb9f42003-11-19 17:17:36 +000022using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000023
Chris Lattner12e64652003-05-22 18:08:30 +000024const Type *BytecodeParser::parseTypeConstant(const unsigned char *&Buf,
25 const unsigned char *EndBuf) {
Chris Lattner7969dc22004-01-15 06:13:09 +000026 unsigned PrimType = read_vbr_uint(Buf, EndBuf);
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 Lattner7969dc22004-01-15 06:13:09 +000034 const Type *RetType = getType(read_vbr_uint(Buf, EndBuf));
Chris Lattner00950542001-06-06 20:29:01 +000035
Chris Lattner7969dc22004-01-15 06:13:09 +000036 unsigned NumParams = read_vbr_uint(Buf, EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +000037
Chris Lattner697954c2002-01-20 22:54:45 +000038 std::vector<const Type*> Params;
Chris Lattner7969dc22004-01-15 06:13:09 +000039 while (NumParams--)
40 Params.push_back(getType(read_vbr_uint(Buf, EndBuf)));
Chris Lattner00950542001-06-06 20:29:01 +000041
Chris Lattner05950c32001-10-13 06:47:01 +000042 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
43 if (isVarArg) Params.pop_back();
44
Chris Lattnerc9aa7df2002-03-29 03:51:11 +000045 return FunctionType::get(RetType, Params, isVarArg);
Chris Lattner00950542001-06-06 20:29:01 +000046 }
47 case Type::ArrayTyID: {
Chris Lattner7969dc22004-01-15 06:13:09 +000048 unsigned ElTyp = read_vbr_uint(Buf, EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +000049 const Type *ElementType = getType(ElTyp);
Chris Lattner00950542001-06-06 20:29:01 +000050
Chris Lattner7969dc22004-01-15 06:13:09 +000051 unsigned NumElements = read_vbr_uint(Buf, EndBuf);
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: {
Chris Lattner697954c2002-01-20 22:54:45 +000058 std::vector<const Type*> Elements;
Chris Lattner7969dc22004-01-15 06:13:09 +000059 unsigned Typ = read_vbr_uint(Buf, EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +000060 while (Typ) { // List is terminated by void/0 typeid
Chris Lattner927b1852003-10-09 20:22:47 +000061 Elements.push_back(getType(Typ));
Chris Lattner7969dc22004-01-15 06:13:09 +000062 Typ = read_vbr_uint(Buf, EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +000063 }
64
Chris Lattner7eadfa12001-10-24 06:21:22 +000065 return StructType::get(Elements);
Chris Lattner00950542001-06-06 20:29:01 +000066 }
67 case Type::PointerTyID: {
Chris Lattner7969dc22004-01-15 06:13:09 +000068 unsigned ElTyp = read_vbr_uint(Buf, EndBuf);
Chris Lattner6c51a362003-09-03 20:25:27 +000069 BCR_TRACE(5, "Pointer Type Constant #" << ElTyp << "\n");
Chris Lattner927b1852003-10-09 20:22:47 +000070 return PointerType::get(getType(ElTyp));
Chris Lattner00950542001-06-06 20:29:01 +000071 }
72
Chris Lattnerd48d6c72001-10-23 01:53:01 +000073 case Type::OpaqueTyID: {
Chris Lattner7eadfa12001-10-24 06:21:22 +000074 return OpaqueType::get();
Chris Lattnerd48d6c72001-10-23 01:53:01 +000075 }
76
Chris Lattner00950542001-06-06 20:29:01 +000077 default:
Anand Shuklaeea60fc2002-06-25 20:44:04 +000078 std::cerr << __FILE__ << ":" << __LINE__
Chris Lattner194f0a52002-06-30 16:04:37 +000079 << ": Don't know how to deserialize"
80 << " primitive Type " << PrimType << "\n";
Chris Lattner74734132002-08-17 22:01:27 +000081 return Val;
Chris Lattner00950542001-06-06 20:29:01 +000082 }
Chris Lattner1d670cc2001-09-07 16:37:43 +000083}
84
Misha Brukman37f92e22003-09-11 22:34:13 +000085// parseTypeConstants - We have to use this weird code to handle recursive
Chris Lattner1d670cc2001-09-07 16:37:43 +000086// types. We know that recursive types will only reference the current slab of
87// values in the type plane, but they can forward reference types before they
88// have been read. For example, Type #0 might be '{ Ty#1 }' and Type #1 might
89// be 'Ty#0*'. When reading Type #0, type number one doesn't exist. To fix
Misha Brukman37f92e22003-09-11 22:34:13 +000090// this ugly problem, we pessimistically insert an opaque type for each type we
Chris Lattner1d670cc2001-09-07 16:37:43 +000091// are about to read. This means that forward references will resolve to
92// something and when we reread the type later, we can replace the opaque type
93// with a new resolved concrete type.
94//
Chris Lattnerebfb9f42003-11-19 17:17:36 +000095namespace llvm { void debug_type_tables(); }
Misha Brukman12c29d12003-09-22 23:38:23 +000096void BytecodeParser::parseTypeConstants(const unsigned char *&Buf,
Chris Lattner12e64652003-05-22 18:08:30 +000097 const unsigned char *EndBuf,
Chris Lattner1d670cc2001-09-07 16:37:43 +000098 TypeValuesListTy &Tab,
99 unsigned NumEntries) {
Chris Lattner05950c32001-10-13 06:47:01 +0000100 assert(Tab.size() == 0 && "should not have read type constants in before!");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000101
102 // Insert a bunch of opaque types to be resolved later...
Chris Lattnerebfb9f42003-11-19 17:17:36 +0000103 Tab.reserve(NumEntries);
Chris Lattnerddceeb72003-12-26 06:16:00 +0000104 for (unsigned i = 0; i != NumEntries; ++i)
Chris Lattnerc7b6f032003-10-02 20:26:18 +0000105 Tab.push_back(OpaqueType::get());
Chris Lattner1d670cc2001-09-07 16:37:43 +0000106
107 // Loop through reading all of the types. Forward types will make use of the
108 // opaque types just inserted.
109 //
Chris Lattnerddceeb72003-12-26 06:16:00 +0000110 for (unsigned i = 0; i != NumEntries; ++i) {
Chris Lattner05950c32001-10-13 06:47:01 +0000111 const Type *NewTy = parseTypeConstant(Buf, EndBuf), *OldTy = Tab[i].get();
Chris Lattnerddceeb72003-12-26 06:16:00 +0000112 if (NewTy == 0) throw std::string("Couldn't parse type!");
Chris Lattner7eadfa12001-10-24 06:21:22 +0000113 BCR_TRACE(4, "#" << i << ": Read Type Constant: '" << NewTy <<
114 "' Replacing: " << OldTy << "\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000115
116 // Don't insertValue the new type... instead we want to replace the opaque
117 // type with the new concrete value...
118 //
119
120 // Refine the abstract type to the new type. This causes all uses of the
Chris Lattnerddceeb72003-12-26 06:16:00 +0000121 // abstract type to use NewTy. This also will cause the opaque type to be
122 // deleted...
Chris Lattner1d670cc2001-09-07 16:37:43 +0000123 //
Chris Lattnerddceeb72003-12-26 06:16:00 +0000124 cast<DerivedType>(const_cast<Type*>(OldTy))->refineAbstractTypeTo(NewTy);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000125
126 // This should have replace the old opaque type with the new type in the
Chris Lattner05950c32001-10-13 06:47:01 +0000127 // value table... or with a preexisting type that was already in the system
128 assert(Tab[i] != OldTy && "refineAbstractType didn't work!");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000129 }
130
131 BCR_TRACE(5, "Resulting types:\n");
Chris Lattner7eadfa12001-10-24 06:21:22 +0000132 for (unsigned i = 0; i < NumEntries; ++i) {
Chris Lattner92940ac2002-04-07 06:11:22 +0000133 BCR_TRACE(5, (void*)Tab[i].get() << " - " << Tab[i].get() << "\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000134 }
Chris Lattner92940ac2002-04-07 06:11:22 +0000135 debug_type_tables();
Chris Lattner00950542001-06-06 20:29:01 +0000136}
137
Chris Lattner1d670cc2001-09-07 16:37:43 +0000138
Chris Lattner9e460f22003-10-04 20:00:03 +0000139Constant *BytecodeParser::parseConstantValue(const unsigned char *&Buf,
140 const unsigned char *EndBuf,
Chris Lattnerebfb9f42003-11-19 17:17:36 +0000141 unsigned TypeID) {
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000142
143 // We must check for a ConstantExpr before switching by type because
144 // a ConstantExpr can be of any type, and has no explicit value.
145 //
Chris Lattner7969dc22004-01-15 06:13:09 +0000146 // 0 if not expr; numArgs if is expr
147 unsigned isExprNumArgs = read_vbr_uint(Buf, EndBuf);
148
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000149 if (isExprNumArgs) {
Chris Lattnere8e46052002-07-30 18:54:22 +0000150 // FIXME: Encoding of constant exprs could be much more compact!
Chris Lattnere8e46052002-07-30 18:54:22 +0000151 std::vector<Constant*> ArgVec;
152 ArgVec.reserve(isExprNumArgs);
Chris Lattner7969dc22004-01-15 06:13:09 +0000153 unsigned Opcode = read_vbr_uint(Buf, EndBuf);
154
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000155 // Read the slot number and types of each of the arguments
Chris Lattnere8e46052002-07-30 18:54:22 +0000156 for (unsigned i = 0; i != isExprNumArgs; ++i) {
Chris Lattner7969dc22004-01-15 06:13:09 +0000157 unsigned ArgValSlot = read_vbr_uint(Buf, EndBuf);
158 unsigned ArgTypeSlot = read_vbr_uint(Buf, EndBuf);
Chris Lattner1c3673b2003-11-19 06:01:12 +0000159 BCR_TRACE(4, "CE Arg " << i << ": Type: '" << *getType(ArgTypeSlot)
160 << "' slot: " << ArgValSlot << "\n");
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000161
162 // Get the arg value from its slot if it exists, otherwise a placeholder
Chris Lattner1c3673b2003-11-19 06:01:12 +0000163 ArgVec.push_back(getConstantValue(ArgTypeSlot, ArgValSlot));
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000164 }
165
166 // Construct a ConstantExpr of the appropriate kind
167 if (isExprNumArgs == 1) { // All one-operand expressions
Chris Lattnerad333482002-08-14 18:24:09 +0000168 assert(Opcode == Instruction::Cast);
Chris Lattnerebfb9f42003-11-19 17:17:36 +0000169 return ConstantExpr::getCast(ArgVec[0], getType(TypeID));
Chris Lattnere8e46052002-07-30 18:54:22 +0000170 } else if (Opcode == Instruction::GetElementPtr) { // GetElementPtr
171 std::vector<Constant*> IdxList(ArgVec.begin()+1, ArgVec.end());
Chris Lattner9e460f22003-10-04 20:00:03 +0000172 return ConstantExpr::getGetElementPtr(ArgVec[0], IdxList);
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000173 } else { // All other 2-operand expressions
Chris Lattner9e460f22003-10-04 20:00:03 +0000174 return ConstantExpr::get(Opcode, ArgVec[0], ArgVec[1]);
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000175 }
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000176 }
177
178 // Ok, not an ConstantExpr. We now know how to read the given type...
Chris Lattnerebfb9f42003-11-19 17:17:36 +0000179 const Type *Ty = getType(TypeID);
Chris Lattner00950542001-06-06 20:29:01 +0000180 switch (Ty->getPrimitiveID()) {
181 case Type::BoolTyID: {
Chris Lattner7969dc22004-01-15 06:13:09 +0000182 unsigned Val = read_vbr_uint(Buf, EndBuf);
Misha Brukman12c29d12003-09-22 23:38:23 +0000183 if (Val != 0 && Val != 1) throw std::string("Invalid boolean value read.");
Chris Lattner9e460f22003-10-04 20:00:03 +0000184 return ConstantBool::get(Val == 1);
Chris Lattner00950542001-06-06 20:29:01 +0000185 }
186
187 case Type::UByteTyID: // Unsigned integer types...
188 case Type::UShortTyID:
189 case Type::UIntTyID: {
Chris Lattner7969dc22004-01-15 06:13:09 +0000190 unsigned Val = read_vbr_uint(Buf, EndBuf);
Misha Brukman12c29d12003-09-22 23:38:23 +0000191 if (!ConstantUInt::isValueValidForType(Ty, Val))
192 throw std::string("Invalid unsigned byte/short/int read.");
Chris Lattner9e460f22003-10-04 20:00:03 +0000193 return ConstantUInt::get(Ty, Val);
Chris Lattner00950542001-06-06 20:29:01 +0000194 }
195
196 case Type::ULongTyID: {
Chris Lattner7969dc22004-01-15 06:13:09 +0000197 return ConstantUInt::get(Ty, read_vbr_uint64(Buf, EndBuf));
Chris Lattner00950542001-06-06 20:29:01 +0000198 }
199
Chris Lattner8017a752003-05-12 15:13:52 +0000200 case Type::SByteTyID: // Signed integer types...
Chris Lattner00950542001-06-06 20:29:01 +0000201 case Type::ShortTyID:
202 case Type::IntTyID: {
Chris Lattner8017a752003-05-12 15:13:52 +0000203 case Type::LongTyID:
Chris Lattner7969dc22004-01-15 06:13:09 +0000204 int64_t Val = read_vbr_int64(Buf, EndBuf);
Misha Brukman12c29d12003-09-22 23:38:23 +0000205 if (!ConstantSInt::isValueValidForType(Ty, Val))
206 throw std::string("Invalid signed byte/short/int/long read.");
Chris Lattner9e460f22003-10-04 20:00:03 +0000207 return ConstantSInt::get(Ty, Val);
Chris Lattner00950542001-06-06 20:29:01 +0000208 }
209
Chris Lattnera1375302001-07-15 00:17:18 +0000210 case Type::FloatTyID: {
211 float F;
Chris Lattner7969dc22004-01-15 06:13:09 +0000212 input_data(Buf, EndBuf, &F, &F+1);
Chris Lattner9e460f22003-10-04 20:00:03 +0000213 return ConstantFP::get(Ty, F);
Chris Lattnera1375302001-07-15 00:17:18 +0000214 }
215
216 case Type::DoubleTyID: {
217 double Val;
Chris Lattner7969dc22004-01-15 06:13:09 +0000218 input_data(Buf, EndBuf, &Val, &Val+1);
Chris Lattner9e460f22003-10-04 20:00:03 +0000219 return ConstantFP::get(Ty, Val);
Chris Lattnera1375302001-07-15 00:17:18 +0000220 }
221
Chris Lattner00950542001-06-06 20:29:01 +0000222 case Type::TypeTyID:
Chris Lattner9e460f22003-10-04 20:00:03 +0000223 throw std::string("Type constants shouldn't live in constant table!");
Chris Lattner00950542001-06-06 20:29:01 +0000224
225 case Type::ArrayTyID: {
Chris Lattner949a3622003-07-23 15:30:06 +0000226 const ArrayType *AT = cast<ArrayType>(Ty);
Chris Lattner90865152001-12-14 16:30:51 +0000227 unsigned NumElements = AT->getNumElements();
Chris Lattner1c3673b2003-11-19 06:01:12 +0000228 unsigned TypeSlot = getTypeSlot(AT->getElementType());
Chris Lattner697954c2002-01-20 22:54:45 +0000229 std::vector<Constant*> Elements;
Chris Lattner7969dc22004-01-15 06:13:09 +0000230 Elements.reserve(NumElements);
231 while (NumElements--) // Read all of the elements of the constant.
232 Elements.push_back(getConstantValue(TypeSlot,
233 read_vbr_uint(Buf, EndBuf)));
Chris Lattner9e460f22003-10-04 20:00:03 +0000234 return ConstantArray::get(AT, Elements);
Chris Lattner00950542001-06-06 20:29:01 +0000235 }
236
237 case Type::StructTyID: {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000238 const StructType *ST = cast<StructType>(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000239 const StructType::ElementTypes &ET = ST->getElementTypes();
240
Chris Lattner697954c2002-01-20 22:54:45 +0000241 std::vector<Constant *> Elements;
Chris Lattner7969dc22004-01-15 06:13:09 +0000242 Elements.reserve(ET.size());
243 for (unsigned i = 0; i != ET.size(); ++i)
244 Elements.push_back(getConstantValue(ET[i], read_vbr_uint(Buf, EndBuf)));
Chris Lattner00950542001-06-06 20:29:01 +0000245
Chris Lattner9e460f22003-10-04 20:00:03 +0000246 return ConstantStruct::get(ST, Elements);
Chris Lattner00950542001-06-06 20:29:01 +0000247 }
248
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000249 case Type::PointerTyID: { // ConstantPointerRef value...
Chris Lattner949a3622003-07-23 15:30:06 +0000250 const PointerType *PT = cast<PointerType>(Ty);
Chris Lattner7969dc22004-01-15 06:13:09 +0000251 unsigned Slot = read_vbr_uint(Buf, EndBuf);
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000252 BCR_TRACE(4, "CPR: Type: '" << Ty << "' slot: " << Slot << "\n");
253
254 // Check to see if we have already read this global variable...
Chris Lattnerebfb9f42003-11-19 17:17:36 +0000255 Value *Val = getValue(TypeID, Slot, false);
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000256 GlobalValue *GV;
257 if (Val) {
258 if (!(GV = dyn_cast<GlobalValue>(Val)))
259 throw std::string("Value of ConstantPointerRef not in ValueTable!");
260 BCR_TRACE(5, "Value Found in ValueTable!\n");
Chris Lattnerebfb9f42003-11-19 17:17:36 +0000261 } else {
262 throw std::string("Forward references are not allowed here.");
Chris Lattner05950c32001-10-13 06:47:01 +0000263 }
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000264
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000265 return ConstantPointerRef::get(GV);
Chris Lattner1a1cb112001-09-30 22:46:54 +0000266 }
267
Chris Lattner00950542001-06-06 20:29:01 +0000268 default:
Misha Brukman12c29d12003-09-22 23:38:23 +0000269 throw std::string("Don't know how to deserialize constant value of type '"+
Chris Lattner9e460f22003-10-04 20:00:03 +0000270 Ty->getDescription());
Chris Lattner00950542001-06-06 20:29:01 +0000271 }
Chris Lattner00950542001-06-06 20:29:01 +0000272}
273
Misha Brukman12c29d12003-09-22 23:38:23 +0000274void BytecodeParser::ParseGlobalTypes(const unsigned char *&Buf,
Chris Lattner12e64652003-05-22 18:08:30 +0000275 const unsigned char *EndBuf) {
Chris Lattner52e20b02003-03-19 20:54:26 +0000276 ValueTable T;
Misha Brukman12c29d12003-09-22 23:38:23 +0000277 ParseConstantPool(Buf, EndBuf, T, ModuleTypeValues);
Chris Lattner52e20b02003-03-19 20:54:26 +0000278}
279
Chris Lattner9e893e82004-01-14 23:35:21 +0000280void BytecodeParser::parseStringConstants(const unsigned char *&Buf,
281 const unsigned char *EndBuf,
282 unsigned NumEntries, ValueTable &Tab){
Chris Lattner9e893e82004-01-14 23:35:21 +0000283 for (; NumEntries; --NumEntries) {
Chris Lattner7969dc22004-01-15 06:13:09 +0000284 unsigned Typ = read_vbr_uint(Buf, EndBuf);
Chris Lattner9e893e82004-01-14 23:35:21 +0000285 const Type *Ty = getType(Typ);
286 if (!isa<ArrayType>(Ty))
287 throw std::string("String constant data invalid!");
288
289 const ArrayType *ATy = cast<ArrayType>(Ty);
290 if (ATy->getElementType() != Type::SByteTy &&
291 ATy->getElementType() != Type::UByteTy)
292 throw std::string("String constant data invalid!");
293
294 // Read character data. The type tells us how long the string is.
295 char Data[ATy->getNumElements()];
Chris Lattner7969dc22004-01-15 06:13:09 +0000296 input_data(Buf, EndBuf, Data, Data+ATy->getNumElements());
Chris Lattner9e893e82004-01-14 23:35:21 +0000297
298 std::vector<Constant*> Elements(ATy->getNumElements());
299 if (ATy->getElementType() == Type::SByteTy)
300 for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i)
Chris Lattnerff47e7d2004-01-15 18:39:06 +0000301 Elements[i] = ConstantSInt::get(Type::SByteTy, (signed char)Data[i]);
Chris Lattner9e893e82004-01-14 23:35:21 +0000302 else
303 for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i)
Chris Lattnerff47e7d2004-01-15 18:39:06 +0000304 Elements[i] = ConstantUInt::get(Type::UByteTy, (unsigned char)Data[i]);
Chris Lattner9e893e82004-01-14 23:35:21 +0000305
306 // Create the constant, inserting it as needed.
307 Constant *C = ConstantArray::get(ATy, Elements);
308 unsigned Slot = insertValue(C, Typ, Tab);
309 ResolveReferencesToConstant(C, Slot);
310 }
311}
312
313
Misha Brukman12c29d12003-09-22 23:38:23 +0000314void BytecodeParser::ParseConstantPool(const unsigned char *&Buf,
Chris Lattner12e64652003-05-22 18:08:30 +0000315 const unsigned char *EndBuf,
Misha Brukman12c29d12003-09-22 23:38:23 +0000316 ValueTable &Tab,
317 TypeValuesListTy &TypeTab) {
Chris Lattner00950542001-06-06 20:29:01 +0000318 while (Buf < EndBuf) {
Chris Lattner7969dc22004-01-15 06:13:09 +0000319 unsigned NumEntries = read_vbr_uint(Buf, EndBuf);
320 unsigned Typ = read_vbr_uint(Buf, EndBuf);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000321 if (Typ == Type::TypeTyID) {
Chris Lattner927b1852003-10-09 20:22:47 +0000322 BCR_TRACE(3, "Type: 'type' NumEntries: " << NumEntries << "\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000323 parseTypeConstants(Buf, EndBuf, TypeTab, NumEntries);
Chris Lattner9e893e82004-01-14 23:35:21 +0000324 } else if (Typ == Type::VoidTyID) {
325 assert(&Tab == &ModuleValues && "Cannot read strings in functions!");
326 parseStringConstants(Buf, EndBuf, NumEntries, Tab);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000327 } else {
Chris Lattnerebfb9f42003-11-19 17:17:36 +0000328 BCR_TRACE(3, "Type: '" << *getType(Typ) << "' NumEntries: "
329 << NumEntries << "\n");
Chris Lattner927b1852003-10-09 20:22:47 +0000330
Chris Lattner7eadfa12001-10-24 06:21:22 +0000331 for (unsigned i = 0; i < NumEntries; ++i) {
Chris Lattnerebfb9f42003-11-19 17:17:36 +0000332 Constant *C = parseConstantValue(Buf, EndBuf, Typ);
Chris Lattner52e20b02003-03-19 20:54:26 +0000333 assert(C && "parseConstantValue returned NULL!");
Misha Brukman12c29d12003-09-22 23:38:23 +0000334 BCR_TRACE(4, "Read Constant: '" << *C << "'\n");
Chris Lattnerf0d92732003-10-13 14:34:59 +0000335 unsigned Slot = insertValue(C, Typ, Tab);
Chris Lattner74734132002-08-17 22:01:27 +0000336
337 // If we are reading a function constant table, make sure that we adjust
338 // the slot number to be the real global constant number.
339 //
Chris Lattnerc2f4b112003-11-19 17:20:42 +0000340 if (&Tab != &ModuleValues && Typ < ModuleValues.size() &&
341 ModuleValues[Typ])
Chris Lattner52e20b02003-03-19 20:54:26 +0000342 Slot += ModuleValues[Typ]->size();
Chris Lattnerebfb9f42003-11-19 17:17:36 +0000343 ResolveReferencesToConstant(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}