blob: ae206daa3707428ab4a78efcb3f06f66292b6431 [file] [log] [blame]
Chris Lattner00950542001-06-06 20:29:01 +00001//===- ReadConst.cpp - Code to constants and constant pools -----------------===
2//
3// This file implements functionality to deserialize constants and entire
4// constant pools.
5//
6// Note that this library should be as fast as possible, reentrant, and
7// threadsafe!!
8//
9//===------------------------------------------------------------------------===
10
11#include "llvm/Module.h"
12#include "llvm/BasicBlock.h"
13#include "llvm/ConstPoolVals.h"
14#include "llvm/DerivedTypes.h"
15#include "ReaderInternals.h"
Chris Lattner1d670cc2001-09-07 16:37:43 +000016#include <algorithm>
Chris Lattner00950542001-06-06 20:29:01 +000017
Chris Lattner00950542001-06-06 20:29:01 +000018
Chris Lattner1d670cc2001-09-07 16:37:43 +000019
20const Type *BytecodeParser::parseTypeConstant(const uchar *&Buf,
21 const uchar *EndBuf) {
Chris Lattner00950542001-06-06 20:29:01 +000022 unsigned PrimType;
Chris Lattner1d670cc2001-09-07 16:37:43 +000023 if (read_vbr(Buf, EndBuf, PrimType)) return failure<const Type*>(0);
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) {
30 case Type::MethodTyID: {
31 unsigned Typ;
Chris Lattner1d670cc2001-09-07 16:37:43 +000032 if (read_vbr(Buf, EndBuf, Typ)) return failure(Val);
Chris Lattner00950542001-06-06 20:29:01 +000033 const Type *RetType = getType(Typ);
Chris Lattner1d670cc2001-09-07 16:37:43 +000034 if (RetType == 0) return failure(Val);
Chris Lattner00950542001-06-06 20:29:01 +000035
Chris Lattnere5a57ee2001-07-25 22:47:55 +000036 unsigned NumParams;
Chris Lattner1d670cc2001-09-07 16:37:43 +000037 if (read_vbr(Buf, EndBuf, NumParams)) return failure(Val);
Chris Lattner00950542001-06-06 20:29:01 +000038
Chris Lattner1d670cc2001-09-07 16:37:43 +000039 vector<const Type*> Params;
Chris Lattnere5a57ee2001-07-25 22:47:55 +000040 while (NumParams--) {
Chris Lattner1d670cc2001-09-07 16:37:43 +000041 if (read_vbr(Buf, EndBuf, Typ)) return failure(Val);
Chris Lattner00950542001-06-06 20:29:01 +000042 const Type *Ty = getType(Typ);
Chris Lattner1d670cc2001-09-07 16:37:43 +000043 if (Ty == 0) return failure(Val);
Chris Lattner00950542001-06-06 20:29:01 +000044 Params.push_back(Ty);
Chris Lattner00950542001-06-06 20:29:01 +000045 }
46
Chris Lattner1d670cc2001-09-07 16:37:43 +000047 Val = MethodType::get(RetType, Params);
Chris Lattner00950542001-06-06 20:29:01 +000048 break;
49 }
50 case Type::ArrayTyID: {
51 unsigned ElTyp;
Chris Lattner1d670cc2001-09-07 16:37:43 +000052 if (read_vbr(Buf, EndBuf, ElTyp)) return failure(Val);
Chris Lattner00950542001-06-06 20:29:01 +000053 const Type *ElementType = getType(ElTyp);
Chris Lattner1d670cc2001-09-07 16:37:43 +000054 if (ElementType == 0) return failure(Val);
Chris Lattner00950542001-06-06 20:29:01 +000055
56 int NumElements;
Chris Lattner1d670cc2001-09-07 16:37:43 +000057 if (read_vbr(Buf, EndBuf, NumElements)) return failure(Val);
58 Val = ArrayType::get(ElementType, NumElements);
Chris Lattner00950542001-06-06 20:29:01 +000059 break;
60 }
61 case Type::StructTyID: {
62 unsigned Typ;
Chris Lattner1d670cc2001-09-07 16:37:43 +000063 vector<const Type*> Elements;
Chris Lattner00950542001-06-06 20:29:01 +000064
Chris Lattner1d670cc2001-09-07 16:37:43 +000065 if (read_vbr(Buf, EndBuf, Typ)) return failure(Val);
Chris Lattner00950542001-06-06 20:29:01 +000066 while (Typ) { // List is terminated by void/0 typeid
67 const Type *Ty = getType(Typ);
Chris Lattner1d670cc2001-09-07 16:37:43 +000068 if (Ty == 0) return failure(Val);
Chris Lattner00950542001-06-06 20:29:01 +000069 Elements.push_back(Ty);
70
Chris Lattner1d670cc2001-09-07 16:37:43 +000071 if (read_vbr(Buf, EndBuf, Typ)) return failure(Val);
Chris Lattner00950542001-06-06 20:29:01 +000072 }
73
Chris Lattner1d670cc2001-09-07 16:37:43 +000074 Val = StructType::get(Elements);
Chris Lattner00950542001-06-06 20:29:01 +000075 break;
76 }
77 case Type::PointerTyID: {
78 unsigned ElTyp;
Chris Lattner1d670cc2001-09-07 16:37:43 +000079 if (read_vbr(Buf, EndBuf, ElTyp)) return failure(Val);
Chris Lattner00950542001-06-06 20:29:01 +000080 const Type *ElementType = getType(ElTyp);
Chris Lattner1d670cc2001-09-07 16:37:43 +000081 if (ElementType == 0) return failure(Val);
82 Val = PointerType::get(ElementType);
Chris Lattner00950542001-06-06 20:29:01 +000083 break;
84 }
85
86 default:
87 cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to deserialize"
88 << " primitive Type " << PrimType << "\n";
Chris Lattner1d670cc2001-09-07 16:37:43 +000089 return failure(Val);
Chris Lattner00950542001-06-06 20:29:01 +000090 }
91
Chris Lattner1d670cc2001-09-07 16:37:43 +000092 return Val;
93}
94
95// refineAbstractType - The callback method is invoked when one of the
96// elements of TypeValues becomes more concrete...
97//
98void 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!");
106 }
107
108 *I = NewType; // Update to point to new, more refined type.
109}
110
111
112
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.
122//
123bool 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");
128
129 // Record the base, starting level that we will begin with.
130 unsigned BaseLevel = Tab.size();
131
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));
135
136 // Loop through reading all of the types. Forward types will make use of the
137 // opaque types just inserted.
138 //
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");
143
144 // Don't insertValue the new type... instead we want to replace the opaque
145 // type with the new concrete value...
146 //
147
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
150 // to be deleted...
151 //
152 // FIXME when types are not const
153 const_cast<DerivedType*>(Tab[i+BaseLevel]->castDerivedTypeAsserting())->refineAbstractTypeTo(NewTy);
154
155 // This should have replace the old opaque type with the new type in the
156 // value table...
157 assert(Tab[i+BaseLevel] == NewTy && "refineAbstractType didn't work!");
158 }
159
160 BCR_TRACE(5, "Resulting types:\n");
161 for (unsigned i = 0; i < NumEntries; i++) {
162 BCR_TRACE(5, Tab[i+BaseLevel]->castTypeAsserting() << "\n");
163 }
Chris Lattner00950542001-06-06 20:29:01 +0000164 return false;
165}
166
Chris Lattner1d670cc2001-09-07 16:37:43 +0000167
Chris Lattner00950542001-06-06 20:29:01 +0000168bool BytecodeParser::parseConstPoolValue(const uchar *&Buf,
169 const uchar *EndBuf,
170 const Type *Ty, ConstPoolVal *&V) {
171 switch (Ty->getPrimitiveID()) {
172 case Type::BoolTyID: {
173 unsigned Val;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000174 if (read_vbr(Buf, EndBuf, Val)) return failure(true);
175 if (Val != 0 && Val != 1) return failure(true);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000176 V = ConstPoolBool::get(Val == 1);
Chris Lattner00950542001-06-06 20:29:01 +0000177 break;
178 }
179
180 case Type::UByteTyID: // Unsigned integer types...
181 case Type::UShortTyID:
182 case Type::UIntTyID: {
183 unsigned Val;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000184 if (read_vbr(Buf, EndBuf, Val)) return failure(true);
185 if (!ConstPoolUInt::isValueValidForType(Ty, Val)) return failure(true);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000186 V = ConstPoolUInt::get(Ty, Val);
Chris Lattner00950542001-06-06 20:29:01 +0000187 break;
188 }
189
190 case Type::ULongTyID: {
191 uint64_t Val;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000192 if (read_vbr(Buf, EndBuf, Val)) return failure(true);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000193 V = ConstPoolUInt::get(Ty, Val);
Chris Lattner00950542001-06-06 20:29:01 +0000194 break;
195 }
196
197 case Type::SByteTyID: // Unsigned integer types...
198 case Type::ShortTyID:
199 case Type::IntTyID: {
200 int Val;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000201 if (read_vbr(Buf, EndBuf, Val)) return failure(true);
202 if (!ConstPoolSInt::isValueValidForType(Ty, Val)) return failure(true);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000203 V = ConstPoolSInt::get(Ty, Val);
Chris Lattner00950542001-06-06 20:29:01 +0000204 break;
205 }
206
207 case Type::LongTyID: {
208 int64_t Val;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000209 if (read_vbr(Buf, EndBuf, Val)) return failure(true);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000210 V = ConstPoolSInt::get(Ty, Val);
Chris Lattner00950542001-06-06 20:29:01 +0000211 break;
212 }
213
Chris Lattnera1375302001-07-15 00:17:18 +0000214 case Type::FloatTyID: {
215 float F;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000216 if (input_data(Buf, EndBuf, &F, &F+1)) return failure(true);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000217 V = ConstPoolFP::get(Ty, F);
Chris Lattnera1375302001-07-15 00:17:18 +0000218 break;
219 }
220
221 case Type::DoubleTyID: {
222 double Val;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000223 if (input_data(Buf, EndBuf, &Val, &Val+1)) return failure(true);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000224 V = ConstPoolFP::get(Ty, Val);
Chris Lattnera1375302001-07-15 00:17:18 +0000225 break;
226 }
227
Chris Lattner00950542001-06-06 20:29:01 +0000228 case Type::TypeTyID:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000229 assert(0 && "Type constants should be handled seperately!!!");
230 abort();
Chris Lattner00950542001-06-06 20:29:01 +0000231
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!
Chris Lattner3d3f2892001-07-28 17:50:18 +0000238 if (read_vbr(Buf, EndBuf, NumElements)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000239
240 vector<ConstPoolVal *> Elements;
241 while (NumElements--) { // Read all of the elements of the constant.
242 unsigned Slot;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000243 if (read_vbr(Buf, EndBuf, Slot)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000244 Value *V = getValue(AT->getElementType(), Slot, false);
Chris Lattner3d3f2892001-07-28 17:50:18 +0000245 if (!V || !V->isConstant()) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000246 Elements.push_back((ConstPoolVal*)V);
247 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000248 V = ConstPoolArray::get(AT, Elements);
Chris Lattner00950542001-06-06 20:29:01 +0000249 break;
250 }
251
252 case Type::StructTyID: {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000253 const StructType *ST = Ty->castStructType();
Chris Lattner00950542001-06-06 20:29:01 +0000254 const StructType::ElementTypes &ET = ST->getElementTypes();
255
256 vector<ConstPoolVal *> Elements;
257 for (unsigned i = 0; i < ET.size(); ++i) {
258 unsigned Slot;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000259 if (read_vbr(Buf, EndBuf, Slot)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000260 Value *V = getValue(ET[i], Slot, false);
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000261 if (!V || !V->isConstant())
Chris Lattner3d3f2892001-07-28 17:50:18 +0000262 return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000263 Elements.push_back((ConstPoolVal*)V);
264 }
265
Chris Lattner1d670cc2001-09-07 16:37:43 +0000266 V = ConstPoolStruct::get(ST, Elements);
Chris Lattner00950542001-06-06 20:29:01 +0000267 break;
268 }
269
Chris Lattner1a1cb112001-09-30 22:46:54 +0000270 case Type::PointerTyID: {
271 const PointerType *PT = Ty->castPointerType();
272 unsigned SubClass;
273 if (read_vbr(Buf, EndBuf, SubClass)) return failure(true);
274 if (SubClass != 0) return failure(true);
275
276
277 V = ConstPoolPointer::getNullPointer(PT);
278 break;
279 }
280
Chris Lattner00950542001-06-06 20:29:01 +0000281 default:
282 cerr << __FILE__ << ":" << __LINE__
283 << ": Don't know how to deserialize constant value of type '"
284 << Ty->getName() << "'\n";
Chris Lattner3d3f2892001-07-28 17:50:18 +0000285 return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000286 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000287
Chris Lattner00950542001-06-06 20:29:01 +0000288 return false;
289}
290
291bool BytecodeParser::ParseConstantPool(const uchar *&Buf, const uchar *EndBuf,
Chris Lattner1d670cc2001-09-07 16:37:43 +0000292 ValueTable &Tab,
293 TypeValuesListTy &TypeTab) {
Chris Lattner00950542001-06-06 20:29:01 +0000294 while (Buf < EndBuf) {
295 unsigned NumEntries, Typ;
296
297 if (read_vbr(Buf, EndBuf, NumEntries) ||
Chris Lattner3d3f2892001-07-28 17:50:18 +0000298 read_vbr(Buf, EndBuf, Typ)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000299 const Type *Ty = getType(Typ);
Chris Lattner3d3f2892001-07-28 17:50:18 +0000300 if (Ty == 0) return failure(true);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000301 BCR_TRACE(3, "Type: '" << Ty << "' NumEntries: " << NumEntries << "\n");
Chris Lattner00950542001-06-06 20:29:01 +0000302
Chris Lattner1d670cc2001-09-07 16:37:43 +0000303 if (Typ == Type::TypeTyID) {
304 if (parseTypeConstants(Buf, EndBuf, TypeTab, NumEntries)) return true;
305 } else {
306 for (unsigned i = 0; i < NumEntries; i++) {
307 ConstPoolVal *I;
308 if (parseConstPoolValue(Buf, EndBuf, Ty, I)) return failure(true);
309 BCR_TRACE(4, "Read Constant: '" << I << "'\n");
310 insertValue(I, Tab);
311 }
Chris Lattner00950542001-06-06 20:29:01 +0000312 }
313 }
314
Chris Lattner3d3f2892001-07-28 17:50:18 +0000315 if (Buf > EndBuf) return failure(true);
316 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000317}