blob: 13f8747cb7dd484a9e75da2620f9fec88ed9cdc9 [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"
Chris Lattner05950c32001-10-13 06:47:01 +000015#include "llvm/GlobalVariable.h"
Chris Lattner00950542001-06-06 20:29:01 +000016#include "ReaderInternals.h"
Chris Lattner1d670cc2001-09-07 16:37:43 +000017#include <algorithm>
Chris Lattner00950542001-06-06 20:29:01 +000018
Chris Lattner00950542001-06-06 20:29:01 +000019
Chris Lattner1d670cc2001-09-07 16:37:43 +000020
21const Type *BytecodeParser::parseTypeConstant(const uchar *&Buf,
22 const uchar *EndBuf) {
Chris Lattner00950542001-06-06 20:29:01 +000023 unsigned PrimType;
Chris Lattner1d670cc2001-09-07 16:37:43 +000024 if (read_vbr(Buf, EndBuf, PrimType)) return failure<const Type*>(0);
Chris Lattner00950542001-06-06 20:29:01 +000025
Chris Lattner1d670cc2001-09-07 16:37:43 +000026 const Type *Val = 0;
27 if ((Val = Type::getPrimitiveType((Type::PrimitiveID)PrimType)))
28 return Val;
Chris Lattner00950542001-06-06 20:29:01 +000029
30 switch (PrimType) {
31 case Type::MethodTyID: {
32 unsigned Typ;
Chris Lattner1d670cc2001-09-07 16:37:43 +000033 if (read_vbr(Buf, EndBuf, Typ)) return failure(Val);
Chris Lattner00950542001-06-06 20:29:01 +000034 const Type *RetType = getType(Typ);
Chris Lattner1d670cc2001-09-07 16:37:43 +000035 if (RetType == 0) return failure(Val);
Chris Lattner00950542001-06-06 20:29:01 +000036
Chris Lattnere5a57ee2001-07-25 22:47:55 +000037 unsigned NumParams;
Chris Lattner1d670cc2001-09-07 16:37:43 +000038 if (read_vbr(Buf, EndBuf, NumParams)) return failure(Val);
Chris Lattner00950542001-06-06 20:29:01 +000039
Chris Lattner1d670cc2001-09-07 16:37:43 +000040 vector<const Type*> Params;
Chris Lattnere5a57ee2001-07-25 22:47:55 +000041 while (NumParams--) {
Chris Lattner1d670cc2001-09-07 16:37:43 +000042 if (read_vbr(Buf, EndBuf, Typ)) return failure(Val);
Chris Lattner00950542001-06-06 20:29:01 +000043 const Type *Ty = getType(Typ);
Chris Lattner1d670cc2001-09-07 16:37:43 +000044 if (Ty == 0) return failure(Val);
Chris Lattner00950542001-06-06 20:29:01 +000045 Params.push_back(Ty);
Chris Lattner00950542001-06-06 20:29:01 +000046 }
47
Chris Lattner05950c32001-10-13 06:47:01 +000048 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
49 if (isVarArg) Params.pop_back();
50
51 Val = MethodType::get(RetType, Params, isVarArg);
Chris Lattner00950542001-06-06 20:29:01 +000052 break;
53 }
54 case Type::ArrayTyID: {
55 unsigned ElTyp;
Chris Lattner1d670cc2001-09-07 16:37:43 +000056 if (read_vbr(Buf, EndBuf, ElTyp)) return failure(Val);
Chris Lattner00950542001-06-06 20:29:01 +000057 const Type *ElementType = getType(ElTyp);
Chris Lattner1d670cc2001-09-07 16:37:43 +000058 if (ElementType == 0) return failure(Val);
Chris Lattner00950542001-06-06 20:29:01 +000059
60 int NumElements;
Chris Lattner1d670cc2001-09-07 16:37:43 +000061 if (read_vbr(Buf, EndBuf, NumElements)) return failure(Val);
62 Val = ArrayType::get(ElementType, NumElements);
Chris Lattner00950542001-06-06 20:29:01 +000063 break;
64 }
65 case Type::StructTyID: {
66 unsigned Typ;
Chris Lattner1d670cc2001-09-07 16:37:43 +000067 vector<const Type*> Elements;
Chris Lattner00950542001-06-06 20:29:01 +000068
Chris Lattner1d670cc2001-09-07 16:37:43 +000069 if (read_vbr(Buf, EndBuf, Typ)) return failure(Val);
Chris Lattner00950542001-06-06 20:29:01 +000070 while (Typ) { // List is terminated by void/0 typeid
71 const Type *Ty = getType(Typ);
Chris Lattner1d670cc2001-09-07 16:37:43 +000072 if (Ty == 0) return failure(Val);
Chris Lattner00950542001-06-06 20:29:01 +000073 Elements.push_back(Ty);
74
Chris Lattner1d670cc2001-09-07 16:37:43 +000075 if (read_vbr(Buf, EndBuf, Typ)) return failure(Val);
Chris Lattner00950542001-06-06 20:29:01 +000076 }
77
Chris Lattner1d670cc2001-09-07 16:37:43 +000078 Val = StructType::get(Elements);
Chris Lattner00950542001-06-06 20:29:01 +000079 break;
80 }
81 case Type::PointerTyID: {
82 unsigned ElTyp;
Chris Lattner1d670cc2001-09-07 16:37:43 +000083 if (read_vbr(Buf, EndBuf, ElTyp)) return failure(Val);
Chris Lattner00950542001-06-06 20:29:01 +000084 const Type *ElementType = getType(ElTyp);
Chris Lattner1d670cc2001-09-07 16:37:43 +000085 if (ElementType == 0) return failure(Val);
86 Val = PointerType::get(ElementType);
Chris Lattner00950542001-06-06 20:29:01 +000087 break;
88 }
89
Chris Lattnerd48d6c72001-10-23 01:53:01 +000090 case Type::OpaqueTyID: {
91 Val = OpaqueType::get();
92 break;
93 }
94
Chris Lattner00950542001-06-06 20:29:01 +000095 default:
96 cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to deserialize"
97 << " primitive Type " << PrimType << "\n";
Chris Lattner1d670cc2001-09-07 16:37:43 +000098 return failure(Val);
Chris Lattner00950542001-06-06 20:29:01 +000099 }
100
Chris Lattner1d670cc2001-09-07 16:37:43 +0000101 return Val;
102}
103
104// refineAbstractType - The callback method is invoked when one of the
105// elements of TypeValues becomes more concrete...
106//
107void BytecodeParser::refineAbstractType(const DerivedType *OldType,
108 const Type *NewType) {
Chris Lattner05950c32001-10-13 06:47:01 +0000109 if (OldType == NewType) return; // Type is modified, but same
110
Chris Lattner1d670cc2001-09-07 16:37:43 +0000111 TypeValuesListTy::iterator I = find(MethodTypeValues.begin(),
112 MethodTypeValues.end(), OldType);
113 if (I == MethodTypeValues.end()) {
114 I = find(ModuleTypeValues.begin(), ModuleTypeValues.end(), OldType);
115 assert(I != ModuleTypeValues.end() &&
116 "Can't refine a type I don't know about!");
117 }
118
119 *I = NewType; // Update to point to new, more refined type.
120}
121
122
123
124// parseTypeConstants - We have to use this wierd code to handle recursive
125// types. We know that recursive types will only reference the current slab of
126// values in the type plane, but they can forward reference types before they
127// have been read. For example, Type #0 might be '{ Ty#1 }' and Type #1 might
128// be 'Ty#0*'. When reading Type #0, type number one doesn't exist. To fix
129// this ugly problem, we pesimistically insert an opaque type for each type we
130// are about to read. This means that forward references will resolve to
131// something and when we reread the type later, we can replace the opaque type
132// with a new resolved concrete type.
133//
134bool BytecodeParser::parseTypeConstants(const uchar *&Buf, const uchar *EndBuf,
135 TypeValuesListTy &Tab,
136 unsigned NumEntries) {
Chris Lattner05950c32001-10-13 06:47:01 +0000137 assert(Tab.size() == 0 && "should not have read type constants in before!");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000138
139 // Insert a bunch of opaque types to be resolved later...
140 for (unsigned i = 0; i < NumEntries; i++)
141 Tab.push_back(PATypeHandle<Type>(OpaqueType::get(), this));
142
143 // Loop through reading all of the types. Forward types will make use of the
144 // opaque types just inserted.
145 //
146 for (unsigned i = 0; i < NumEntries; i++) {
Chris Lattner05950c32001-10-13 06:47:01 +0000147 const Type *NewTy = parseTypeConstant(Buf, EndBuf), *OldTy = Tab[i].get();
Chris Lattner1d670cc2001-09-07 16:37:43 +0000148 if (NewTy == 0) return failure(true);
149 BCR_TRACE(4, "Read Type Constant: '" << NewTy << "'\n");
150
151 // Don't insertValue the new type... instead we want to replace the opaque
152 // type with the new concrete value...
153 //
154
155 // Refine the abstract type to the new type. This causes all uses of the
156 // abstract type to use the newty. This also will cause the opaque type
157 // to be deleted...
158 //
Chris Lattner05950c32001-10-13 06:47:01 +0000159 cast<DerivedType>(Tab[i].get())->refineAbstractTypeTo(NewTy);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000160
161 // This should have replace the old opaque type with the new type in the
Chris Lattner05950c32001-10-13 06:47:01 +0000162 // value table... or with a preexisting type that was already in the system
163 assert(Tab[i] != OldTy && "refineAbstractType didn't work!");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000164 }
165
166 BCR_TRACE(5, "Resulting types:\n");
167 for (unsigned i = 0; i < NumEntries; i++) {
Chris Lattner05950c32001-10-13 06:47:01 +0000168 BCR_TRACE(5, cast<const Type>(Tab[i]) << "\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000169 }
Chris Lattner00950542001-06-06 20:29:01 +0000170 return false;
171}
172
Chris Lattner1d670cc2001-09-07 16:37:43 +0000173
Chris Lattner00950542001-06-06 20:29:01 +0000174bool BytecodeParser::parseConstPoolValue(const uchar *&Buf,
175 const uchar *EndBuf,
176 const Type *Ty, ConstPoolVal *&V) {
177 switch (Ty->getPrimitiveID()) {
178 case Type::BoolTyID: {
179 unsigned Val;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000180 if (read_vbr(Buf, EndBuf, Val)) return failure(true);
181 if (Val != 0 && Val != 1) return failure(true);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000182 V = ConstPoolBool::get(Val == 1);
Chris Lattner00950542001-06-06 20:29:01 +0000183 break;
184 }
185
186 case Type::UByteTyID: // Unsigned integer types...
187 case Type::UShortTyID:
188 case Type::UIntTyID: {
189 unsigned Val;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000190 if (read_vbr(Buf, EndBuf, Val)) return failure(true);
191 if (!ConstPoolUInt::isValueValidForType(Ty, 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::ULongTyID: {
197 uint64_t Val;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000198 if (read_vbr(Buf, EndBuf, Val)) return failure(true);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000199 V = ConstPoolUInt::get(Ty, Val);
Chris Lattner00950542001-06-06 20:29:01 +0000200 break;
201 }
202
203 case Type::SByteTyID: // Unsigned integer types...
204 case Type::ShortTyID:
205 case Type::IntTyID: {
206 int Val;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000207 if (read_vbr(Buf, EndBuf, Val)) return failure(true);
208 if (!ConstPoolSInt::isValueValidForType(Ty, 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
213 case Type::LongTyID: {
214 int64_t Val;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000215 if (read_vbr(Buf, EndBuf, Val)) return failure(true);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000216 V = ConstPoolSInt::get(Ty, Val);
Chris Lattner00950542001-06-06 20:29:01 +0000217 break;
218 }
219
Chris Lattnera1375302001-07-15 00:17:18 +0000220 case Type::FloatTyID: {
221 float F;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000222 if (input_data(Buf, EndBuf, &F, &F+1)) return failure(true);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000223 V = ConstPoolFP::get(Ty, F);
Chris Lattnera1375302001-07-15 00:17:18 +0000224 break;
225 }
226
227 case Type::DoubleTyID: {
228 double Val;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000229 if (input_data(Buf, EndBuf, &Val, &Val+1)) return failure(true);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000230 V = ConstPoolFP::get(Ty, Val);
Chris Lattnera1375302001-07-15 00:17:18 +0000231 break;
232 }
233
Chris Lattner00950542001-06-06 20:29:01 +0000234 case Type::TypeTyID:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000235 assert(0 && "Type constants should be handled seperately!!!");
236 abort();
Chris Lattner00950542001-06-06 20:29:01 +0000237
238 case Type::ArrayTyID: {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000239 const ArrayType *AT = cast<const ArrayType>(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000240 unsigned NumElements;
241 if (AT->isSized()) // Sized array, # elements stored in type!
242 NumElements = (unsigned)AT->getNumElements();
243 else // Unsized array, # elements stored in stream!
Chris Lattner3d3f2892001-07-28 17:50:18 +0000244 if (read_vbr(Buf, EndBuf, NumElements)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000245
246 vector<ConstPoolVal *> Elements;
247 while (NumElements--) { // Read all of the elements of the constant.
248 unsigned Slot;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000249 if (read_vbr(Buf, EndBuf, Slot)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000250 Value *V = getValue(AT->getElementType(), Slot, false);
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000251 if (!V || !isa<ConstPoolVal>(V)) return failure(true);
252 Elements.push_back(cast<ConstPoolVal>(V));
Chris Lattner00950542001-06-06 20:29:01 +0000253 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000254 V = ConstPoolArray::get(AT, Elements);
Chris Lattner00950542001-06-06 20:29:01 +0000255 break;
256 }
257
258 case Type::StructTyID: {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000259 const StructType *ST = cast<StructType>(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000260 const StructType::ElementTypes &ET = ST->getElementTypes();
261
262 vector<ConstPoolVal *> Elements;
263 for (unsigned i = 0; i < ET.size(); ++i) {
264 unsigned Slot;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000265 if (read_vbr(Buf, EndBuf, Slot)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000266 Value *V = getValue(ET[i], Slot, false);
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000267 if (!V || !isa<ConstPoolVal>(V))
Chris Lattner3d3f2892001-07-28 17:50:18 +0000268 return failure(true);
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000269 Elements.push_back(cast<ConstPoolVal>(V));
Chris Lattner00950542001-06-06 20:29:01 +0000270 }
271
Chris Lattner1d670cc2001-09-07 16:37:43 +0000272 V = ConstPoolStruct::get(ST, Elements);
Chris Lattner00950542001-06-06 20:29:01 +0000273 break;
274 }
275
Chris Lattner1a1cb112001-09-30 22:46:54 +0000276 case Type::PointerTyID: {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000277 const PointerType *PT = cast<const PointerType>(Ty);
Chris Lattner1a1cb112001-09-30 22:46:54 +0000278 unsigned SubClass;
279 if (read_vbr(Buf, EndBuf, SubClass)) return failure(true);
Chris Lattner05950c32001-10-13 06:47:01 +0000280 switch (SubClass) {
281 case 0: // ConstPoolPointerNull value...
282 V = ConstPoolPointerNull::get(PT);
283 break;
Chris Lattner1a1cb112001-09-30 22:46:54 +0000284
Chris Lattnerc18545d2001-10-15 13:21:42 +0000285 case 1: { // ConstPoolPointerRef value...
Chris Lattner05950c32001-10-13 06:47:01 +0000286 unsigned Slot;
287 if (read_vbr(Buf, EndBuf, Slot)) return failure(true);
288 BCR_TRACE(4, "CPPR: Type: '" << Ty << "' slot: " << Slot << "\n");
Chris Lattner1a1cb112001-09-30 22:46:54 +0000289
Chris Lattner05950c32001-10-13 06:47:01 +0000290 // Check to see if we have already read this global variable yet...
291 Value *Val = getValue(PT, Slot, false);
292 GlobalValue *GV;
293 if (Val) {
294 if (!(GV = dyn_cast<GlobalValue>(Val))) return failure(true);
295 BCR_TRACE(5, "Value Found in ValueTable!\n");
296 } else { // Nope... see if we have previously forward ref'd it
297 GlobalRefsType::iterator I = GlobalRefs.find(make_pair(PT, Slot));
298 if (I != GlobalRefs.end()) {
299 BCR_TRACE(5, "Previous forward ref found!\n");
300 GV = I->second;
301 } else {
302 BCR_TRACE(5, "Creating new forward ref variable!\n");
303
304 // Create a placeholder for the global variable reference...
305 GlobalVariable *GVar = new GlobalVariable(PT->getValueType(), false);
306
307 // Keep track of the fact that we have a forward ref to recycle it
308 GlobalRefs.insert(make_pair(make_pair(PT, Slot), GVar));
309
310 // Must temporarily push this value into the module table...
311 TheModule->getGlobalList().push_back(GVar);
312 GV = GVar;
313 }
314 }
315
Chris Lattnerc18545d2001-10-15 13:21:42 +0000316 V = ConstPoolPointerRef::get(GV);
Chris Lattner05950c32001-10-13 06:47:01 +0000317 break;
318 }
319 default:
320 return failure(true);
321 }
Chris Lattner1a1cb112001-09-30 22:46:54 +0000322 break;
323 }
324
Chris Lattner00950542001-06-06 20:29:01 +0000325 default:
326 cerr << __FILE__ << ":" << __LINE__
327 << ": Don't know how to deserialize constant value of type '"
328 << Ty->getName() << "'\n";
Chris Lattner3d3f2892001-07-28 17:50:18 +0000329 return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000330 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000331
Chris Lattner00950542001-06-06 20:29:01 +0000332 return false;
333}
334
335bool BytecodeParser::ParseConstantPool(const uchar *&Buf, const uchar *EndBuf,
Chris Lattner1d670cc2001-09-07 16:37:43 +0000336 ValueTable &Tab,
337 TypeValuesListTy &TypeTab) {
Chris Lattner00950542001-06-06 20:29:01 +0000338 while (Buf < EndBuf) {
339 unsigned NumEntries, Typ;
340
341 if (read_vbr(Buf, EndBuf, NumEntries) ||
Chris Lattner3d3f2892001-07-28 17:50:18 +0000342 read_vbr(Buf, EndBuf, Typ)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000343 const Type *Ty = getType(Typ);
Chris Lattner3d3f2892001-07-28 17:50:18 +0000344 if (Ty == 0) return failure(true);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000345 BCR_TRACE(3, "Type: '" << Ty << "' NumEntries: " << NumEntries << "\n");
Chris Lattner00950542001-06-06 20:29:01 +0000346
Chris Lattner1d670cc2001-09-07 16:37:43 +0000347 if (Typ == Type::TypeTyID) {
348 if (parseTypeConstants(Buf, EndBuf, TypeTab, NumEntries)) return true;
349 } else {
350 for (unsigned i = 0; i < NumEntries; i++) {
351 ConstPoolVal *I;
352 if (parseConstPoolValue(Buf, EndBuf, Ty, I)) return failure(true);
353 BCR_TRACE(4, "Read Constant: '" << I << "'\n");
Chris Lattner05950c32001-10-13 06:47:01 +0000354 if (insertValue(I, Tab) == -1) return failure(true);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000355 }
Chris Lattner00950542001-06-06 20:29:01 +0000356 }
357 }
358
Chris Lattner3d3f2892001-07-28 17:50:18 +0000359 if (Buf > EndBuf) return failure(true);
360 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000361}