blob: 67cfff7b973fb2ecea2bc6b5f15f4057ae439515 [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 //
Chris Lattnercfe26c92001-10-01 18:26:53 +0000152 cast<DerivedType>(Tab[i+BaseLevel].get())->refineAbstractTypeTo(NewTy);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000153
154 // This should have replace the old opaque type with the new type in the
155 // value table...
156 assert(Tab[i+BaseLevel] == NewTy && "refineAbstractType didn't work!");
157 }
158
159 BCR_TRACE(5, "Resulting types:\n");
160 for (unsigned i = 0; i < NumEntries; i++) {
Chris Lattnercfe26c92001-10-01 18:26:53 +0000161 BCR_TRACE(5, cast<Type>(Tab[i+BaseLevel]) << "\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000162 }
Chris Lattner00950542001-06-06 20:29:01 +0000163 return false;
164}
165
Chris Lattner1d670cc2001-09-07 16:37:43 +0000166
Chris Lattner00950542001-06-06 20:29:01 +0000167bool BytecodeParser::parseConstPoolValue(const uchar *&Buf,
168 const uchar *EndBuf,
169 const Type *Ty, ConstPoolVal *&V) {
170 switch (Ty->getPrimitiveID()) {
171 case Type::BoolTyID: {
172 unsigned Val;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000173 if (read_vbr(Buf, EndBuf, Val)) return failure(true);
174 if (Val != 0 && Val != 1) return failure(true);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000175 V = ConstPoolBool::get(Val == 1);
Chris Lattner00950542001-06-06 20:29:01 +0000176 break;
177 }
178
179 case Type::UByteTyID: // Unsigned integer types...
180 case Type::UShortTyID:
181 case Type::UIntTyID: {
182 unsigned Val;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000183 if (read_vbr(Buf, EndBuf, Val)) return failure(true);
184 if (!ConstPoolUInt::isValueValidForType(Ty, Val)) return failure(true);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000185 V = ConstPoolUInt::get(Ty, Val);
Chris Lattner00950542001-06-06 20:29:01 +0000186 break;
187 }
188
189 case Type::ULongTyID: {
190 uint64_t Val;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000191 if (read_vbr(Buf, EndBuf, Val)) return failure(true);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000192 V = ConstPoolUInt::get(Ty, Val);
Chris Lattner00950542001-06-06 20:29:01 +0000193 break;
194 }
195
196 case Type::SByteTyID: // Unsigned integer types...
197 case Type::ShortTyID:
198 case Type::IntTyID: {
199 int Val;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000200 if (read_vbr(Buf, EndBuf, Val)) return failure(true);
201 if (!ConstPoolSInt::isValueValidForType(Ty, Val)) return failure(true);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000202 V = ConstPoolSInt::get(Ty, Val);
Chris Lattner00950542001-06-06 20:29:01 +0000203 break;
204 }
205
206 case Type::LongTyID: {
207 int64_t Val;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000208 if (read_vbr(Buf, EndBuf, Val)) return failure(true);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000209 V = ConstPoolSInt::get(Ty, Val);
Chris Lattner00950542001-06-06 20:29:01 +0000210 break;
211 }
212
Chris Lattnera1375302001-07-15 00:17:18 +0000213 case Type::FloatTyID: {
214 float F;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000215 if (input_data(Buf, EndBuf, &F, &F+1)) return failure(true);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000216 V = ConstPoolFP::get(Ty, F);
Chris Lattnera1375302001-07-15 00:17:18 +0000217 break;
218 }
219
220 case Type::DoubleTyID: {
221 double Val;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000222 if (input_data(Buf, EndBuf, &Val, &Val+1)) return failure(true);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000223 V = ConstPoolFP::get(Ty, Val);
Chris Lattnera1375302001-07-15 00:17:18 +0000224 break;
225 }
226
Chris Lattner00950542001-06-06 20:29:01 +0000227 case Type::TypeTyID:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000228 assert(0 && "Type constants should be handled seperately!!!");
229 abort();
Chris Lattner00950542001-06-06 20:29:01 +0000230
231 case Type::ArrayTyID: {
232 const ArrayType *AT = (const ArrayType*)Ty;
233 unsigned NumElements;
234 if (AT->isSized()) // Sized array, # elements stored in type!
235 NumElements = (unsigned)AT->getNumElements();
236 else // Unsized array, # elements stored in stream!
Chris Lattner3d3f2892001-07-28 17:50:18 +0000237 if (read_vbr(Buf, EndBuf, NumElements)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000238
239 vector<ConstPoolVal *> Elements;
240 while (NumElements--) { // Read all of the elements of the constant.
241 unsigned Slot;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000242 if (read_vbr(Buf, EndBuf, Slot)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000243 Value *V = getValue(AT->getElementType(), Slot, false);
Chris Lattner3d3f2892001-07-28 17:50:18 +0000244 if (!V || !V->isConstant()) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000245 Elements.push_back((ConstPoolVal*)V);
246 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000247 V = ConstPoolArray::get(AT, Elements);
Chris Lattner00950542001-06-06 20:29:01 +0000248 break;
249 }
250
251 case Type::StructTyID: {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000252 const StructType *ST = Ty->castStructType();
Chris Lattner00950542001-06-06 20:29:01 +0000253 const StructType::ElementTypes &ET = ST->getElementTypes();
254
255 vector<ConstPoolVal *> Elements;
256 for (unsigned i = 0; i < ET.size(); ++i) {
257 unsigned Slot;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000258 if (read_vbr(Buf, EndBuf, Slot)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000259 Value *V = getValue(ET[i], Slot, false);
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000260 if (!V || !V->isConstant())
Chris Lattner3d3f2892001-07-28 17:50:18 +0000261 return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000262 Elements.push_back((ConstPoolVal*)V);
263 }
264
Chris Lattner1d670cc2001-09-07 16:37:43 +0000265 V = ConstPoolStruct::get(ST, Elements);
Chris Lattner00950542001-06-06 20:29:01 +0000266 break;
267 }
268
Chris Lattner1a1cb112001-09-30 22:46:54 +0000269 case Type::PointerTyID: {
270 const PointerType *PT = Ty->castPointerType();
271 unsigned SubClass;
272 if (read_vbr(Buf, EndBuf, SubClass)) return failure(true);
273 if (SubClass != 0) return failure(true);
274
275
276 V = ConstPoolPointer::getNullPointer(PT);
277 break;
278 }
279
Chris Lattner00950542001-06-06 20:29:01 +0000280 default:
281 cerr << __FILE__ << ":" << __LINE__
282 << ": Don't know how to deserialize constant value of type '"
283 << Ty->getName() << "'\n";
Chris Lattner3d3f2892001-07-28 17:50:18 +0000284 return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000285 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000286
Chris Lattner00950542001-06-06 20:29:01 +0000287 return false;
288}
289
290bool BytecodeParser::ParseConstantPool(const uchar *&Buf, const uchar *EndBuf,
Chris Lattner1d670cc2001-09-07 16:37:43 +0000291 ValueTable &Tab,
292 TypeValuesListTy &TypeTab) {
Chris Lattner00950542001-06-06 20:29:01 +0000293 while (Buf < EndBuf) {
294 unsigned NumEntries, Typ;
295
296 if (read_vbr(Buf, EndBuf, NumEntries) ||
Chris Lattner3d3f2892001-07-28 17:50:18 +0000297 read_vbr(Buf, EndBuf, Typ)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000298 const Type *Ty = getType(Typ);
Chris Lattner3d3f2892001-07-28 17:50:18 +0000299 if (Ty == 0) return failure(true);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000300 BCR_TRACE(3, "Type: '" << Ty << "' NumEntries: " << NumEntries << "\n");
Chris Lattner00950542001-06-06 20:29:01 +0000301
Chris Lattner1d670cc2001-09-07 16:37:43 +0000302 if (Typ == Type::TypeTyID) {
303 if (parseTypeConstants(Buf, EndBuf, TypeTab, NumEntries)) return true;
304 } else {
305 for (unsigned i = 0; i < NumEntries; i++) {
306 ConstPoolVal *I;
307 if (parseConstPoolValue(Buf, EndBuf, Ty, I)) return failure(true);
308 BCR_TRACE(4, "Read Constant: '" << I << "'\n");
309 insertValue(I, Tab);
310 }
Chris Lattner00950542001-06-06 20:29:01 +0000311 }
312 }
313
Chris Lattner3d3f2892001-07-28 17:50:18 +0000314 if (Buf > EndBuf) return failure(true);
315 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000316}