1 //===- ReadConst.cpp - Code to constants and constant pools -----------------===
3 // This file implements functionality to deserialize constants and entire
6 // Note that this library should be as fast as possible, reentrant, and
9 //===------------------------------------------------------------------------===
11 #include "llvm/Module.h"
12 #include "llvm/BasicBlock.h"
13 #include "llvm/ConstPoolVals.h"
14 #include "llvm/DerivedTypes.h"
15 #include "ReaderInternals.h"
20 const Type *BytecodeParser::parseTypeConstant(const uchar *&Buf,
21 const uchar *EndBuf) {
23 if (read_vbr(Buf, EndBuf, PrimType)) return failure<const Type*>(0);
26 if ((Val = Type::getPrimitiveType((Type::PrimitiveID)PrimType)))
30 case Type::MethodTyID: {
32 if (read_vbr(Buf, EndBuf, Typ)) return failure(Val);
33 const Type *RetType = getType(Typ);
34 if (RetType == 0) return failure(Val);
37 if (read_vbr(Buf, EndBuf, NumParams)) return failure(Val);
39 vector<const Type*> Params;
41 if (read_vbr(Buf, EndBuf, Typ)) return failure(Val);
42 const Type *Ty = getType(Typ);
43 if (Ty == 0) return failure(Val);
47 Val = MethodType::get(RetType, Params);
50 case Type::ArrayTyID: {
52 if (read_vbr(Buf, EndBuf, ElTyp)) return failure(Val);
53 const Type *ElementType = getType(ElTyp);
54 if (ElementType == 0) return failure(Val);
57 if (read_vbr(Buf, EndBuf, NumElements)) return failure(Val);
58 Val = ArrayType::get(ElementType, NumElements);
61 case Type::StructTyID: {
63 vector<const Type*> Elements;
65 if (read_vbr(Buf, EndBuf, Typ)) return failure(Val);
66 while (Typ) { // List is terminated by void/0 typeid
67 const Type *Ty = getType(Typ);
68 if (Ty == 0) return failure(Val);
69 Elements.push_back(Ty);
71 if (read_vbr(Buf, EndBuf, Typ)) return failure(Val);
74 Val = StructType::get(Elements);
77 case Type::PointerTyID: {
79 if (read_vbr(Buf, EndBuf, ElTyp)) return failure(Val);
80 const Type *ElementType = getType(ElTyp);
81 if (ElementType == 0) return failure(Val);
82 Val = PointerType::get(ElementType);
87 cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to deserialize"
88 << " primitive Type " << PrimType << "\n";
95 // refineAbstractType - The callback method is invoked when one of the
96 // elements of TypeValues becomes more concrete...
98 void BytecodeParser::refineAbstractType(const DerivedType *OldType,
99 const Type *NewType) {
100 TypeValuesListTy::iterator I = find(MethodTypeValues.begin(),
101 MethodTypeValues.end(), OldType);
102 if (I == MethodTypeValues.end()) {
103 I = find(ModuleTypeValues.begin(), ModuleTypeValues.end(), OldType);
104 assert(I != ModuleTypeValues.end() &&
105 "Can't refine a type I don't know about!");
108 *I = NewType; // Update to point to new, more refined type.
113 // parseTypeConstants - We have to use this wierd code to handle recursive
114 // types. We know that recursive types will only reference the current slab of
115 // values in the type plane, but they can forward reference types before they
116 // have been read. For example, Type #0 might be '{ Ty#1 }' and Type #1 might
117 // be 'Ty#0*'. When reading Type #0, type number one doesn't exist. To fix
118 // this ugly problem, we pesimistically insert an opaque type for each type we
119 // are about to read. This means that forward references will resolve to
120 // something and when we reread the type later, we can replace the opaque type
121 // with a new resolved concrete type.
123 bool BytecodeParser::parseTypeConstants(const uchar *&Buf, const uchar *EndBuf,
124 TypeValuesListTy &Tab,
125 unsigned NumEntries) {
126 assert(Tab.size() == 0 && "I think table should always be empty here!"
127 "This should simplify later code");
129 // Record the base, starting level that we will begin with.
130 unsigned BaseLevel = Tab.size();
132 // Insert a bunch of opaque types to be resolved later...
133 for (unsigned i = 0; i < NumEntries; i++)
134 Tab.push_back(PATypeHandle<Type>(OpaqueType::get(), this));
136 // Loop through reading all of the types. Forward types will make use of the
137 // opaque types just inserted.
139 for (unsigned i = 0; i < NumEntries; i++) {
140 const Type *NewTy = parseTypeConstant(Buf, EndBuf);
141 if (NewTy == 0) return failure(true);
142 BCR_TRACE(4, "Read Type Constant: '" << NewTy << "'\n");
144 // Don't insertValue the new type... instead we want to replace the opaque
145 // type with the new concrete value...
148 // Refine the abstract type to the new type. This causes all uses of the
149 // abstract type to use the newty. This also will cause the opaque type
152 // FIXME when types are not const
153 const_cast<DerivedType*>(Tab[i+BaseLevel]->castDerivedTypeAsserting())->refineAbstractTypeTo(NewTy);
155 // This should have replace the old opaque type with the new type in the
157 assert(Tab[i+BaseLevel] == NewTy && "refineAbstractType didn't work!");
160 BCR_TRACE(5, "Resulting types:\n");
161 for (unsigned i = 0; i < NumEntries; i++) {
162 BCR_TRACE(5, Tab[i+BaseLevel]->castTypeAsserting() << "\n");
168 bool BytecodeParser::parseConstPoolValue(const uchar *&Buf,
170 const Type *Ty, ConstPoolVal *&V) {
171 switch (Ty->getPrimitiveID()) {
172 case Type::BoolTyID: {
174 if (read_vbr(Buf, EndBuf, Val)) return failure(true);
175 if (Val != 0 && Val != 1) return failure(true);
176 V = ConstPoolBool::get(Val == 1);
180 case Type::UByteTyID: // Unsigned integer types...
181 case Type::UShortTyID:
182 case Type::UIntTyID: {
184 if (read_vbr(Buf, EndBuf, Val)) return failure(true);
185 if (!ConstPoolUInt::isValueValidForType(Ty, Val)) return failure(true);
186 V = ConstPoolUInt::get(Ty, Val);
190 case Type::ULongTyID: {
192 if (read_vbr(Buf, EndBuf, Val)) return failure(true);
193 V = ConstPoolUInt::get(Ty, Val);
197 case Type::SByteTyID: // Unsigned integer types...
198 case Type::ShortTyID:
199 case Type::IntTyID: {
201 if (read_vbr(Buf, EndBuf, Val)) return failure(true);
202 if (!ConstPoolSInt::isValueValidForType(Ty, Val)) return failure(true);
203 V = ConstPoolSInt::get(Ty, Val);
207 case Type::LongTyID: {
209 if (read_vbr(Buf, EndBuf, Val)) return failure(true);
210 V = ConstPoolSInt::get(Ty, Val);
214 case Type::FloatTyID: {
216 if (input_data(Buf, EndBuf, &F, &F+1)) return failure(true);
217 V = ConstPoolFP::get(Ty, F);
221 case Type::DoubleTyID: {
223 if (input_data(Buf, EndBuf, &Val, &Val+1)) return failure(true);
224 V = ConstPoolFP::get(Ty, Val);
229 assert(0 && "Type constants should be handled seperately!!!");
232 case Type::ArrayTyID: {
233 const ArrayType *AT = (const ArrayType*)Ty;
234 unsigned NumElements;
235 if (AT->isSized()) // Sized array, # elements stored in type!
236 NumElements = (unsigned)AT->getNumElements();
237 else // Unsized array, # elements stored in stream!
238 if (read_vbr(Buf, EndBuf, NumElements)) return failure(true);
240 vector<ConstPoolVal *> Elements;
241 while (NumElements--) { // Read all of the elements of the constant.
243 if (read_vbr(Buf, EndBuf, Slot)) return failure(true);
244 Value *V = getValue(AT->getElementType(), Slot, false);
245 if (!V || !V->isConstant()) return failure(true);
246 Elements.push_back((ConstPoolVal*)V);
248 V = ConstPoolArray::get(AT, Elements);
252 case Type::StructTyID: {
253 const StructType *ST = Ty->castStructType();
254 const StructType::ElementTypes &ET = ST->getElementTypes();
256 vector<ConstPoolVal *> Elements;
257 for (unsigned i = 0; i < ET.size(); ++i) {
259 if (read_vbr(Buf, EndBuf, Slot)) return failure(true);
260 Value *V = getValue(ET[i], Slot, false);
261 if (!V || !V->isConstant())
262 return failure(true);
263 Elements.push_back((ConstPoolVal*)V);
266 V = ConstPoolStruct::get(ST, Elements);
270 case Type::PointerTyID: {
271 const PointerType *PT = Ty->castPointerType();
273 if (read_vbr(Buf, EndBuf, SubClass)) return failure(true);
274 if (SubClass != 0) return failure(true);
277 V = ConstPoolPointer::getNullPointer(PT);
282 cerr << __FILE__ << ":" << __LINE__
283 << ": Don't know how to deserialize constant value of type '"
284 << Ty->getName() << "'\n";
285 return failure(true);
291 bool BytecodeParser::ParseConstantPool(const uchar *&Buf, const uchar *EndBuf,
293 TypeValuesListTy &TypeTab) {
294 while (Buf < EndBuf) {
295 unsigned NumEntries, Typ;
297 if (read_vbr(Buf, EndBuf, NumEntries) ||
298 read_vbr(Buf, EndBuf, Typ)) return failure(true);
299 const Type *Ty = getType(Typ);
300 if (Ty == 0) return failure(true);
301 BCR_TRACE(3, "Type: '" << Ty << "' NumEntries: " << NumEntries << "\n");
303 if (Typ == Type::TypeTyID) {
304 if (parseTypeConstants(Buf, EndBuf, TypeTab, NumEntries)) return true;
306 for (unsigned i = 0; i < NumEntries; i++) {
308 if (parseConstPoolValue(Buf, EndBuf, Ty, I)) return failure(true);
309 BCR_TRACE(4, "Read Constant: '" << I << "'\n");
315 if (Buf > EndBuf) return failure(true);