blob: 72d8caed67302d329c529c7a7f4c047381e97e5d [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 Lattner5fa428f2004-04-05 01:27:26 +000018#include "llvm/Support/GetElementPtrTypeIterator.h"
Chris Lattner1d670cc2001-09-07 16:37:43 +000019#include <algorithm>
Chris Lattnerebfb9f42003-11-19 17:17:36 +000020using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000021
Chris Lattner12e64652003-05-22 18:08:30 +000022const Type *BytecodeParser::parseTypeConstant(const unsigned char *&Buf,
23 const unsigned char *EndBuf) {
Chris Lattner7969dc22004-01-15 06:13:09 +000024 unsigned PrimType = read_vbr_uint(Buf, EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +000025
Chris Lattner1d670cc2001-09-07 16:37:43 +000026 const Type *Val = 0;
Chris Lattnerf70c22b2004-06-17 18:19:28 +000027 if ((Val = Type::getPrimitiveType((Type::TypeID)PrimType)))
Chris Lattner1d670cc2001-09-07 16:37:43 +000028 return Val;
Chris Lattner00950542001-06-06 20:29:01 +000029
30 switch (PrimType) {
Chris Lattnerc9aa7df2002-03-29 03:51:11 +000031 case Type::FunctionTyID: {
Chris Lattner7969dc22004-01-15 06:13:09 +000032 const Type *RetType = getType(read_vbr_uint(Buf, EndBuf));
Chris Lattner00950542001-06-06 20:29:01 +000033
Chris Lattner7969dc22004-01-15 06:13:09 +000034 unsigned NumParams = read_vbr_uint(Buf, EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +000035
Chris Lattner697954c2002-01-20 22:54:45 +000036 std::vector<const Type*> Params;
Chris Lattner7969dc22004-01-15 06:13:09 +000037 while (NumParams--)
38 Params.push_back(getType(read_vbr_uint(Buf, EndBuf)));
Chris Lattner00950542001-06-06 20:29:01 +000039
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: {
Chris Lattner7969dc22004-01-15 06:13:09 +000046 unsigned ElTyp = read_vbr_uint(Buf, EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +000047 const Type *ElementType = getType(ElTyp);
Chris Lattner00950542001-06-06 20:29:01 +000048
Chris Lattner7969dc22004-01-15 06:13:09 +000049 unsigned NumElements = read_vbr_uint(Buf, EndBuf);
Chris Lattner7eadfa12001-10-24 06:21:22 +000050
51 BCR_TRACE(5, "Array Type Constant #" << ElTyp << " size="
Chris Lattner697954c2002-01-20 22:54:45 +000052 << NumElements << "\n");
Chris Lattner7eadfa12001-10-24 06:21:22 +000053 return ArrayType::get(ElementType, NumElements);
Chris Lattner00950542001-06-06 20:29:01 +000054 }
55 case Type::StructTyID: {
Chris Lattner697954c2002-01-20 22:54:45 +000056 std::vector<const Type*> Elements;
Chris Lattner7969dc22004-01-15 06:13:09 +000057 unsigned Typ = read_vbr_uint(Buf, EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +000058 while (Typ) { // List is terminated by void/0 typeid
Chris Lattner927b1852003-10-09 20:22:47 +000059 Elements.push_back(getType(Typ));
Chris Lattner7969dc22004-01-15 06:13:09 +000060 Typ = read_vbr_uint(Buf, EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +000061 }
62
Chris Lattner7eadfa12001-10-24 06:21:22 +000063 return StructType::get(Elements);
Chris Lattner00950542001-06-06 20:29:01 +000064 }
65 case Type::PointerTyID: {
Chris Lattner7969dc22004-01-15 06:13:09 +000066 unsigned ElTyp = read_vbr_uint(Buf, EndBuf);
Chris Lattner6c51a362003-09-03 20:25:27 +000067 BCR_TRACE(5, "Pointer Type Constant #" << ElTyp << "\n");
Chris Lattner927b1852003-10-09 20:22:47 +000068 return PointerType::get(getType(ElTyp));
Chris Lattner00950542001-06-06 20:29:01 +000069 }
70
Chris Lattnerd48d6c72001-10-23 01:53:01 +000071 case Type::OpaqueTyID: {
Chris Lattner7eadfa12001-10-24 06:21:22 +000072 return OpaqueType::get();
Chris Lattnerd48d6c72001-10-23 01:53:01 +000073 }
74
Chris Lattner00950542001-06-06 20:29:01 +000075 default:
Anand Shuklaeea60fc2002-06-25 20:44:04 +000076 std::cerr << __FILE__ << ":" << __LINE__
Chris Lattner194f0a52002-06-30 16:04:37 +000077 << ": Don't know how to deserialize"
78 << " primitive Type " << PrimType << "\n";
Chris Lattner74734132002-08-17 22:01:27 +000079 return Val;
Chris Lattner00950542001-06-06 20:29:01 +000080 }
Chris Lattner1d670cc2001-09-07 16:37:43 +000081}
82
Misha Brukman37f92e22003-09-11 22:34:13 +000083// parseTypeConstants - We have to use this weird code to handle recursive
Chris Lattner1d670cc2001-09-07 16:37:43 +000084// types. We know that recursive types will only reference the current slab of
85// values in the type plane, but they can forward reference types before they
86// have been read. For example, Type #0 might be '{ Ty#1 }' and Type #1 might
87// be 'Ty#0*'. When reading Type #0, type number one doesn't exist. To fix
Misha Brukman37f92e22003-09-11 22:34:13 +000088// this ugly problem, we pessimistically insert an opaque type for each type we
Chris Lattner1d670cc2001-09-07 16:37:43 +000089// are about to read. This means that forward references will resolve to
90// something and when we reread the type later, we can replace the opaque type
91// with a new resolved concrete type.
92//
Misha Brukman12c29d12003-09-22 23:38:23 +000093void BytecodeParser::parseTypeConstants(const unsigned char *&Buf,
Chris Lattner12e64652003-05-22 18:08:30 +000094 const unsigned char *EndBuf,
Chris Lattner1d670cc2001-09-07 16:37:43 +000095 TypeValuesListTy &Tab,
96 unsigned NumEntries) {
Chris Lattner05950c32001-10-13 06:47:01 +000097 assert(Tab.size() == 0 && "should not have read type constants in before!");
Chris Lattner1d670cc2001-09-07 16:37:43 +000098
99 // Insert a bunch of opaque types to be resolved later...
Chris Lattnerebfb9f42003-11-19 17:17:36 +0000100 Tab.reserve(NumEntries);
Chris Lattnerddceeb72003-12-26 06:16:00 +0000101 for (unsigned i = 0; i != NumEntries; ++i)
Chris Lattnerc7b6f032003-10-02 20:26:18 +0000102 Tab.push_back(OpaqueType::get());
Chris Lattner1d670cc2001-09-07 16:37:43 +0000103
104 // Loop through reading all of the types. Forward types will make use of the
105 // opaque types just inserted.
106 //
Chris Lattnerddceeb72003-12-26 06:16:00 +0000107 for (unsigned i = 0; i != NumEntries; ++i) {
Chris Lattner05950c32001-10-13 06:47:01 +0000108 const Type *NewTy = parseTypeConstant(Buf, EndBuf), *OldTy = Tab[i].get();
Chris Lattnerddceeb72003-12-26 06:16:00 +0000109 if (NewTy == 0) throw std::string("Couldn't parse type!");
Chris Lattner7eadfa12001-10-24 06:21:22 +0000110 BCR_TRACE(4, "#" << i << ": Read Type Constant: '" << NewTy <<
111 "' Replacing: " << OldTy << "\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000112
113 // Don't insertValue the new type... instead we want to replace the opaque
114 // type with the new concrete value...
115 //
116
117 // Refine the abstract type to the new type. This causes all uses of the
Chris Lattnerddceeb72003-12-26 06:16:00 +0000118 // abstract type to use NewTy. This also will cause the opaque type to be
119 // deleted...
Chris Lattner1d670cc2001-09-07 16:37:43 +0000120 //
Chris Lattnerddceeb72003-12-26 06:16:00 +0000121 cast<DerivedType>(const_cast<Type*>(OldTy))->refineAbstractTypeTo(NewTy);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000122
123 // This should have replace the old opaque type with the new type in the
Chris Lattner05950c32001-10-13 06:47:01 +0000124 // value table... or with a preexisting type that was already in the system
125 assert(Tab[i] != OldTy && "refineAbstractType didn't work!");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000126 }
127
128 BCR_TRACE(5, "Resulting types:\n");
Chris Lattner7eadfa12001-10-24 06:21:22 +0000129 for (unsigned i = 0; i < NumEntries; ++i) {
Chris Lattner92940ac2002-04-07 06:11:22 +0000130 BCR_TRACE(5, (void*)Tab[i].get() << " - " << Tab[i].get() << "\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000131 }
Chris Lattner00950542001-06-06 20:29:01 +0000132}
133
Chris Lattner1d670cc2001-09-07 16:37:43 +0000134
Chris Lattner9e460f22003-10-04 20:00:03 +0000135Constant *BytecodeParser::parseConstantValue(const unsigned char *&Buf,
136 const unsigned char *EndBuf,
Chris Lattnerebfb9f42003-11-19 17:17:36 +0000137 unsigned TypeID) {
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000138
139 // We must check for a ConstantExpr before switching by type because
140 // a ConstantExpr can be of any type, and has no explicit value.
141 //
Chris Lattner7969dc22004-01-15 06:13:09 +0000142 // 0 if not expr; numArgs if is expr
143 unsigned isExprNumArgs = read_vbr_uint(Buf, EndBuf);
144
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000145 if (isExprNumArgs) {
Chris Lattnere8e46052002-07-30 18:54:22 +0000146 // FIXME: Encoding of constant exprs could be much more compact!
Chris Lattnere8e46052002-07-30 18:54:22 +0000147 std::vector<Constant*> ArgVec;
148 ArgVec.reserve(isExprNumArgs);
Chris Lattner7969dc22004-01-15 06:13:09 +0000149 unsigned Opcode = read_vbr_uint(Buf, EndBuf);
150
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000151 // Read the slot number and types of each of the arguments
Chris Lattnere8e46052002-07-30 18:54:22 +0000152 for (unsigned i = 0; i != isExprNumArgs; ++i) {
Chris Lattner7969dc22004-01-15 06:13:09 +0000153 unsigned ArgValSlot = read_vbr_uint(Buf, EndBuf);
154 unsigned ArgTypeSlot = read_vbr_uint(Buf, EndBuf);
Chris Lattner1c3673b2003-11-19 06:01:12 +0000155 BCR_TRACE(4, "CE Arg " << i << ": Type: '" << *getType(ArgTypeSlot)
156 << "' slot: " << ArgValSlot << "\n");
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000157
158 // Get the arg value from its slot if it exists, otherwise a placeholder
Chris Lattner1c3673b2003-11-19 06:01:12 +0000159 ArgVec.push_back(getConstantValue(ArgTypeSlot, ArgValSlot));
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000160 }
161
162 // Construct a ConstantExpr of the appropriate kind
163 if (isExprNumArgs == 1) { // All one-operand expressions
Chris Lattnerad333482002-08-14 18:24:09 +0000164 assert(Opcode == Instruction::Cast);
Chris Lattnerebfb9f42003-11-19 17:17:36 +0000165 return ConstantExpr::getCast(ArgVec[0], getType(TypeID));
Chris Lattnere8e46052002-07-30 18:54:22 +0000166 } else if (Opcode == Instruction::GetElementPtr) { // GetElementPtr
167 std::vector<Constant*> IdxList(ArgVec.begin()+1, ArgVec.end());
Chris Lattner5fa428f2004-04-05 01:27:26 +0000168
169 if (hasRestrictedGEPTypes) {
170 const Type *BaseTy = ArgVec[0]->getType();
171 generic_gep_type_iterator<std::vector<Constant*>::iterator>
172 GTI = gep_type_begin(BaseTy, IdxList.begin(), IdxList.end()),
173 E = gep_type_end(BaseTy, IdxList.begin(), IdxList.end());
174 for (unsigned i = 0; GTI != E; ++GTI, ++i)
175 if (isa<StructType>(*GTI)) {
176 if (IdxList[i]->getType() != Type::UByteTy)
177 throw std::string("Invalid index for getelementptr!");
178 IdxList[i] = ConstantExpr::getCast(IdxList[i], Type::UIntTy);
179 }
180 }
181
Chris Lattner9e460f22003-10-04 20:00:03 +0000182 return ConstantExpr::getGetElementPtr(ArgVec[0], IdxList);
Chris Lattner1e490ba2004-03-31 02:53:59 +0000183 } else if (Opcode == Instruction::Select) {
184 assert(ArgVec.size() == 3);
185 return ConstantExpr::getSelect(ArgVec[0], ArgVec[1], ArgVec[2]);
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000186 } else { // All other 2-operand expressions
Chris Lattner9e460f22003-10-04 20:00:03 +0000187 return ConstantExpr::get(Opcode, ArgVec[0], ArgVec[1]);
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000188 }
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000189 }
190
191 // Ok, not an ConstantExpr. We now know how to read the given type...
Chris Lattnerebfb9f42003-11-19 17:17:36 +0000192 const Type *Ty = getType(TypeID);
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000193 switch (Ty->getTypeID()) {
Chris Lattner00950542001-06-06 20:29:01 +0000194 case Type::BoolTyID: {
Chris Lattner7969dc22004-01-15 06:13:09 +0000195 unsigned Val = read_vbr_uint(Buf, EndBuf);
Misha Brukman12c29d12003-09-22 23:38:23 +0000196 if (Val != 0 && Val != 1) throw std::string("Invalid boolean value read.");
Chris Lattner9e460f22003-10-04 20:00:03 +0000197 return ConstantBool::get(Val == 1);
Chris Lattner00950542001-06-06 20:29:01 +0000198 }
199
200 case Type::UByteTyID: // Unsigned integer types...
201 case Type::UShortTyID:
202 case Type::UIntTyID: {
Chris Lattner7969dc22004-01-15 06:13:09 +0000203 unsigned Val = read_vbr_uint(Buf, EndBuf);
Misha Brukman12c29d12003-09-22 23:38:23 +0000204 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: {
Chris Lattner7969dc22004-01-15 06:13:09 +0000210 return ConstantUInt::get(Ty, read_vbr_uint64(Buf, EndBuf));
Chris Lattner00950542001-06-06 20:29:01 +0000211 }
212
Chris Lattner8017a752003-05-12 15:13:52 +0000213 case Type::SByteTyID: // Signed integer types...
Chris Lattner00950542001-06-06 20:29:01 +0000214 case Type::ShortTyID:
215 case Type::IntTyID: {
Chris Lattner8017a752003-05-12 15:13:52 +0000216 case Type::LongTyID:
Chris Lattner7969dc22004-01-15 06:13:09 +0000217 int64_t Val = read_vbr_int64(Buf, EndBuf);
Misha Brukman12c29d12003-09-22 23:38:23 +0000218 if (!ConstantSInt::isValueValidForType(Ty, Val))
219 throw std::string("Invalid signed byte/short/int/long read.");
Chris Lattner9e460f22003-10-04 20:00:03 +0000220 return ConstantSInt::get(Ty, Val);
Chris Lattner00950542001-06-06 20:29:01 +0000221 }
222
Chris Lattnera1375302001-07-15 00:17:18 +0000223 case Type::FloatTyID: {
224 float F;
Chris Lattner7969dc22004-01-15 06:13:09 +0000225 input_data(Buf, EndBuf, &F, &F+1);
Chris Lattner9e460f22003-10-04 20:00:03 +0000226 return ConstantFP::get(Ty, F);
Chris Lattnera1375302001-07-15 00:17:18 +0000227 }
228
229 case Type::DoubleTyID: {
230 double Val;
Chris Lattner7969dc22004-01-15 06:13:09 +0000231 input_data(Buf, EndBuf, &Val, &Val+1);
Chris Lattner9e460f22003-10-04 20:00:03 +0000232 return ConstantFP::get(Ty, Val);
Chris Lattnera1375302001-07-15 00:17:18 +0000233 }
234
Chris Lattner00950542001-06-06 20:29:01 +0000235 case Type::TypeTyID:
Chris Lattner9e460f22003-10-04 20:00:03 +0000236 throw std::string("Type constants shouldn't live in constant table!");
Chris Lattner00950542001-06-06 20:29:01 +0000237
238 case Type::ArrayTyID: {
Chris Lattner949a3622003-07-23 15:30:06 +0000239 const ArrayType *AT = cast<ArrayType>(Ty);
Chris Lattner90865152001-12-14 16:30:51 +0000240 unsigned NumElements = AT->getNumElements();
Chris Lattner1c3673b2003-11-19 06:01:12 +0000241 unsigned TypeSlot = getTypeSlot(AT->getElementType());
Chris Lattner697954c2002-01-20 22:54:45 +0000242 std::vector<Constant*> Elements;
Chris Lattner7969dc22004-01-15 06:13:09 +0000243 Elements.reserve(NumElements);
244 while (NumElements--) // Read all of the elements of the constant.
245 Elements.push_back(getConstantValue(TypeSlot,
246 read_vbr_uint(Buf, EndBuf)));
Chris Lattner9e460f22003-10-04 20:00:03 +0000247 return ConstantArray::get(AT, Elements);
Chris Lattner00950542001-06-06 20:29:01 +0000248 }
249
250 case Type::StructTyID: {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000251 const StructType *ST = cast<StructType>(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000252
Chris Lattner697954c2002-01-20 22:54:45 +0000253 std::vector<Constant *> Elements;
Chris Lattnerd21cd802004-02-09 04:37:31 +0000254 Elements.reserve(ST->getNumElements());
255 for (unsigned i = 0; i != ST->getNumElements(); ++i)
256 Elements.push_back(getConstantValue(ST->getElementType(i),
257 read_vbr_uint(Buf, EndBuf)));
Chris Lattner00950542001-06-06 20:29:01 +0000258
Chris Lattner9e460f22003-10-04 20:00:03 +0000259 return ConstantStruct::get(ST, Elements);
Chris Lattner00950542001-06-06 20:29:01 +0000260 }
261
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000262 case Type::PointerTyID: { // ConstantPointerRef value...
Chris Lattner949a3622003-07-23 15:30:06 +0000263 const PointerType *PT = cast<PointerType>(Ty);
Chris Lattner7969dc22004-01-15 06:13:09 +0000264 unsigned Slot = read_vbr_uint(Buf, EndBuf);
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000265 BCR_TRACE(4, "CPR: Type: '" << Ty << "' slot: " << Slot << "\n");
266
267 // Check to see if we have already read this global variable...
Chris Lattnerebfb9f42003-11-19 17:17:36 +0000268 Value *Val = getValue(TypeID, Slot, false);
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000269 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");
Chris Lattnerebfb9f42003-11-19 17:17:36 +0000274 } else {
275 throw std::string("Forward references are not allowed here.");
Chris Lattner05950c32001-10-13 06:47:01 +0000276 }
Vikram S. Advec668b7c2002-07-14 23:05:09 +0000277
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000278 return ConstantPointerRef::get(GV);
Chris Lattner1a1cb112001-09-30 22:46:54 +0000279 }
280
Chris Lattner00950542001-06-06 20:29:01 +0000281 default:
Misha Brukman12c29d12003-09-22 23:38:23 +0000282 throw std::string("Don't know how to deserialize constant value of type '"+
Chris Lattner9e460f22003-10-04 20:00:03 +0000283 Ty->getDescription());
Chris Lattner00950542001-06-06 20:29:01 +0000284 }
Chris Lattner00950542001-06-06 20:29:01 +0000285}
286
Misha Brukman12c29d12003-09-22 23:38:23 +0000287void BytecodeParser::ParseGlobalTypes(const unsigned char *&Buf,
Chris Lattner12e64652003-05-22 18:08:30 +0000288 const unsigned char *EndBuf) {
Chris Lattner52e20b02003-03-19 20:54:26 +0000289 ValueTable T;
Misha Brukman12c29d12003-09-22 23:38:23 +0000290 ParseConstantPool(Buf, EndBuf, T, ModuleTypeValues);
Chris Lattner52e20b02003-03-19 20:54:26 +0000291}
292
Chris Lattner9e893e82004-01-14 23:35:21 +0000293void BytecodeParser::parseStringConstants(const unsigned char *&Buf,
294 const unsigned char *EndBuf,
295 unsigned NumEntries, ValueTable &Tab){
Chris Lattner9e893e82004-01-14 23:35:21 +0000296 for (; NumEntries; --NumEntries) {
Chris Lattner7969dc22004-01-15 06:13:09 +0000297 unsigned Typ = read_vbr_uint(Buf, EndBuf);
Chris Lattner9e893e82004-01-14 23:35:21 +0000298 const Type *Ty = getType(Typ);
299 if (!isa<ArrayType>(Ty))
300 throw std::string("String constant data invalid!");
301
302 const ArrayType *ATy = cast<ArrayType>(Ty);
303 if (ATy->getElementType() != Type::SByteTy &&
304 ATy->getElementType() != Type::UByteTy)
305 throw std::string("String constant data invalid!");
306
307 // Read character data. The type tells us how long the string is.
308 char Data[ATy->getNumElements()];
Chris Lattner7969dc22004-01-15 06:13:09 +0000309 input_data(Buf, EndBuf, Data, Data+ATy->getNumElements());
Chris Lattner9e893e82004-01-14 23:35:21 +0000310
311 std::vector<Constant*> Elements(ATy->getNumElements());
312 if (ATy->getElementType() == Type::SByteTy)
313 for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i)
Chris Lattnerff47e7d2004-01-15 18:39:06 +0000314 Elements[i] = ConstantSInt::get(Type::SByteTy, (signed char)Data[i]);
Chris Lattner9e893e82004-01-14 23:35:21 +0000315 else
316 for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i)
Chris Lattnerff47e7d2004-01-15 18:39:06 +0000317 Elements[i] = ConstantUInt::get(Type::UByteTy, (unsigned char)Data[i]);
Chris Lattner9e893e82004-01-14 23:35:21 +0000318
319 // Create the constant, inserting it as needed.
320 Constant *C = ConstantArray::get(ATy, Elements);
321 unsigned Slot = insertValue(C, Typ, Tab);
322 ResolveReferencesToConstant(C, Slot);
323 }
324}
325
326
Misha Brukman12c29d12003-09-22 23:38:23 +0000327void BytecodeParser::ParseConstantPool(const unsigned char *&Buf,
Chris Lattner12e64652003-05-22 18:08:30 +0000328 const unsigned char *EndBuf,
Misha Brukman12c29d12003-09-22 23:38:23 +0000329 ValueTable &Tab,
330 TypeValuesListTy &TypeTab) {
Chris Lattner00950542001-06-06 20:29:01 +0000331 while (Buf < EndBuf) {
Chris Lattner7969dc22004-01-15 06:13:09 +0000332 unsigned NumEntries = read_vbr_uint(Buf, EndBuf);
333 unsigned Typ = read_vbr_uint(Buf, EndBuf);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000334 if (Typ == Type::TypeTyID) {
Chris Lattner927b1852003-10-09 20:22:47 +0000335 BCR_TRACE(3, "Type: 'type' NumEntries: " << NumEntries << "\n");
Misha Brukman12c29d12003-09-22 23:38:23 +0000336 parseTypeConstants(Buf, EndBuf, TypeTab, NumEntries);
Chris Lattner9e893e82004-01-14 23:35:21 +0000337 } else if (Typ == Type::VoidTyID) {
338 assert(&Tab == &ModuleValues && "Cannot read strings in functions!");
339 parseStringConstants(Buf, EndBuf, NumEntries, Tab);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000340 } else {
Chris Lattnerebfb9f42003-11-19 17:17:36 +0000341 BCR_TRACE(3, "Type: '" << *getType(Typ) << "' NumEntries: "
342 << NumEntries << "\n");
Chris Lattner927b1852003-10-09 20:22:47 +0000343
Chris Lattner7eadfa12001-10-24 06:21:22 +0000344 for (unsigned i = 0; i < NumEntries; ++i) {
Chris Lattnerebfb9f42003-11-19 17:17:36 +0000345 Constant *C = parseConstantValue(Buf, EndBuf, Typ);
Chris Lattner52e20b02003-03-19 20:54:26 +0000346 assert(C && "parseConstantValue returned NULL!");
Misha Brukman12c29d12003-09-22 23:38:23 +0000347 BCR_TRACE(4, "Read Constant: '" << *C << "'\n");
Chris Lattnerf0d92732003-10-13 14:34:59 +0000348 unsigned Slot = insertValue(C, Typ, Tab);
Chris Lattner74734132002-08-17 22:01:27 +0000349
350 // If we are reading a function constant table, make sure that we adjust
351 // the slot number to be the real global constant number.
352 //
Chris Lattnerc2f4b112003-11-19 17:20:42 +0000353 if (&Tab != &ModuleValues && Typ < ModuleValues.size() &&
354 ModuleValues[Typ])
Chris Lattner52e20b02003-03-19 20:54:26 +0000355 Slot += ModuleValues[Typ]->size();
Chris Lattnerebfb9f42003-11-19 17:17:36 +0000356 ResolveReferencesToConstant(C, Slot);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000357 }
Chris Lattner00950542001-06-06 20:29:01 +0000358 }
359 }
360
Misha Brukman12c29d12003-09-22 23:38:23 +0000361 if (Buf > EndBuf) throw std::string("Read past end of buffer.");
Chris Lattner00950542001-06-06 20:29:01 +0000362}