blob: fcf92fa5366827f5379691474ced21b9f3405496 [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
Chris Lattner7eadfa12001-10-24 06:21:22 +000051 return MethodType::get(RetType, Params, isVarArg);
Chris Lattner00950542001-06-06 20:29:01 +000052 }
53 case Type::ArrayTyID: {
54 unsigned ElTyp;
Chris Lattner1d670cc2001-09-07 16:37:43 +000055 if (read_vbr(Buf, EndBuf, ElTyp)) return failure(Val);
Chris Lattner00950542001-06-06 20:29:01 +000056 const Type *ElementType = getType(ElTyp);
Chris Lattner1d670cc2001-09-07 16:37:43 +000057 if (ElementType == 0) return failure(Val);
Chris Lattner00950542001-06-06 20:29:01 +000058
59 int NumElements;
Chris Lattner1d670cc2001-09-07 16:37:43 +000060 if (read_vbr(Buf, EndBuf, NumElements)) return failure(Val);
Chris Lattner7eadfa12001-10-24 06:21:22 +000061
62 BCR_TRACE(5, "Array Type Constant #" << ElTyp << " size="
63 << NumElements << endl);
64 return ArrayType::get(ElementType, NumElements);
Chris Lattner00950542001-06-06 20:29:01 +000065 }
66 case Type::StructTyID: {
67 unsigned Typ;
Chris Lattner1d670cc2001-09-07 16:37:43 +000068 vector<const Type*> Elements;
Chris Lattner00950542001-06-06 20:29:01 +000069
Chris Lattner1d670cc2001-09-07 16:37:43 +000070 if (read_vbr(Buf, EndBuf, Typ)) return failure(Val);
Chris Lattner00950542001-06-06 20:29:01 +000071 while (Typ) { // List is terminated by void/0 typeid
72 const Type *Ty = getType(Typ);
Chris Lattner1d670cc2001-09-07 16:37:43 +000073 if (Ty == 0) return failure(Val);
Chris Lattner00950542001-06-06 20:29:01 +000074 Elements.push_back(Ty);
75
Chris Lattner1d670cc2001-09-07 16:37:43 +000076 if (read_vbr(Buf, EndBuf, Typ)) return failure(Val);
Chris Lattner00950542001-06-06 20:29:01 +000077 }
78
Chris Lattner7eadfa12001-10-24 06:21:22 +000079 return StructType::get(Elements);
Chris Lattner00950542001-06-06 20:29:01 +000080 }
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 Lattner7eadfa12001-10-24 06:21:22 +000084 BCR_TRACE(5, "Pointer Type Constant #" << (ElTyp-14) << endl);
Chris Lattner00950542001-06-06 20:29:01 +000085 const Type *ElementType = getType(ElTyp);
Chris Lattner1d670cc2001-09-07 16:37:43 +000086 if (ElementType == 0) return failure(Val);
Chris Lattner7eadfa12001-10-24 06:21:22 +000087 return PointerType::get(ElementType);
Chris Lattner00950542001-06-06 20:29:01 +000088 }
89
Chris Lattnerd48d6c72001-10-23 01:53:01 +000090 case Type::OpaqueTyID: {
Chris Lattner7eadfa12001-10-24 06:21:22 +000091 return OpaqueType::get();
Chris Lattnerd48d6c72001-10-23 01:53:01 +000092 }
93
Chris Lattner00950542001-06-06 20:29:01 +000094 default:
95 cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to deserialize"
Chris Lattner7eadfa12001-10-24 06:21:22 +000096 << " primitive Type " << PrimType << "\n";
Chris Lattner1d670cc2001-09-07 16:37:43 +000097 return failure(Val);
Chris Lattner00950542001-06-06 20:29:01 +000098 }
Chris Lattner1d670cc2001-09-07 16:37:43 +000099}
100
101// refineAbstractType - The callback method is invoked when one of the
102// elements of TypeValues becomes more concrete...
103//
104void BytecodeParser::refineAbstractType(const DerivedType *OldType,
105 const Type *NewType) {
Chris Lattner05950c32001-10-13 06:47:01 +0000106 if (OldType == NewType) return; // Type is modified, but same
107
Chris Lattner1d670cc2001-09-07 16:37:43 +0000108 TypeValuesListTy::iterator I = find(MethodTypeValues.begin(),
109 MethodTypeValues.end(), OldType);
110 if (I == MethodTypeValues.end()) {
111 I = find(ModuleTypeValues.begin(), ModuleTypeValues.end(), OldType);
112 assert(I != ModuleTypeValues.end() &&
113 "Can't refine a type I don't know about!");
114 }
115
116 *I = NewType; // Update to point to new, more refined type.
117}
118
119
120
121// parseTypeConstants - We have to use this wierd code to handle recursive
122// types. We know that recursive types will only reference the current slab of
123// values in the type plane, but they can forward reference types before they
124// have been read. For example, Type #0 might be '{ Ty#1 }' and Type #1 might
125// be 'Ty#0*'. When reading Type #0, type number one doesn't exist. To fix
126// this ugly problem, we pesimistically insert an opaque type for each type we
127// are about to read. This means that forward references will resolve to
128// something and when we reread the type later, we can replace the opaque type
129// with a new resolved concrete type.
130//
131bool BytecodeParser::parseTypeConstants(const uchar *&Buf, const uchar *EndBuf,
132 TypeValuesListTy &Tab,
133 unsigned NumEntries) {
Chris Lattner05950c32001-10-13 06:47:01 +0000134 assert(Tab.size() == 0 && "should not have read type constants in before!");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000135
136 // Insert a bunch of opaque types to be resolved later...
Chris Lattner7eadfa12001-10-24 06:21:22 +0000137 for (unsigned i = 0; i < NumEntries; ++i)
Chris Lattner1d670cc2001-09-07 16:37:43 +0000138 Tab.push_back(PATypeHandle<Type>(OpaqueType::get(), this));
139
140 // Loop through reading all of the types. Forward types will make use of the
141 // opaque types just inserted.
142 //
Chris Lattner7eadfa12001-10-24 06:21:22 +0000143 for (unsigned i = 0; i < NumEntries; ++i) {
Chris Lattner05950c32001-10-13 06:47:01 +0000144 const Type *NewTy = parseTypeConstant(Buf, EndBuf), *OldTy = Tab[i].get();
Chris Lattner1d670cc2001-09-07 16:37:43 +0000145 if (NewTy == 0) return failure(true);
Chris Lattner7eadfa12001-10-24 06:21:22 +0000146 BCR_TRACE(4, "#" << i << ": Read Type Constant: '" << NewTy <<
147 "' Replacing: " << OldTy << "\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000148
149 // Don't insertValue the new type... instead we want to replace the opaque
150 // type with the new concrete value...
151 //
152
153 // Refine the abstract type to the new type. This causes all uses of the
154 // abstract type to use the newty. This also will cause the opaque type
155 // to be deleted...
156 //
Chris Lattner05950c32001-10-13 06:47:01 +0000157 cast<DerivedType>(Tab[i].get())->refineAbstractTypeTo(NewTy);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000158
159 // This should have replace the old opaque type with the new type in the
Chris Lattner05950c32001-10-13 06:47:01 +0000160 // value table... or with a preexisting type that was already in the system
161 assert(Tab[i] != OldTy && "refineAbstractType didn't work!");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000162 }
163
164 BCR_TRACE(5, "Resulting types:\n");
Chris Lattner7eadfa12001-10-24 06:21:22 +0000165 for (unsigned i = 0; i < NumEntries; ++i) {
Chris Lattner05950c32001-10-13 06:47:01 +0000166 BCR_TRACE(5, cast<const Type>(Tab[i]) << "\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000167 }
Chris Lattner00950542001-06-06 20:29:01 +0000168 return false;
169}
170
Chris Lattner1d670cc2001-09-07 16:37:43 +0000171
Chris Lattner00950542001-06-06 20:29:01 +0000172bool BytecodeParser::parseConstPoolValue(const uchar *&Buf,
173 const uchar *EndBuf,
174 const Type *Ty, ConstPoolVal *&V) {
175 switch (Ty->getPrimitiveID()) {
176 case Type::BoolTyID: {
177 unsigned Val;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000178 if (read_vbr(Buf, EndBuf, Val)) return failure(true);
179 if (Val != 0 && Val != 1) return failure(true);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000180 V = ConstPoolBool::get(Val == 1);
Chris Lattner00950542001-06-06 20:29:01 +0000181 break;
182 }
183
184 case Type::UByteTyID: // Unsigned integer types...
185 case Type::UShortTyID:
186 case Type::UIntTyID: {
187 unsigned Val;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000188 if (read_vbr(Buf, EndBuf, Val)) return failure(true);
189 if (!ConstPoolUInt::isValueValidForType(Ty, Val)) return failure(true);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000190 V = ConstPoolUInt::get(Ty, Val);
Chris Lattner00950542001-06-06 20:29:01 +0000191 break;
192 }
193
194 case Type::ULongTyID: {
195 uint64_t Val;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000196 if (read_vbr(Buf, EndBuf, Val)) return failure(true);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000197 V = ConstPoolUInt::get(Ty, Val);
Chris Lattner00950542001-06-06 20:29:01 +0000198 break;
199 }
200
201 case Type::SByteTyID: // Unsigned integer types...
202 case Type::ShortTyID:
203 case Type::IntTyID: {
204 int Val;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000205 if (read_vbr(Buf, EndBuf, Val)) return failure(true);
206 if (!ConstPoolSInt::isValueValidForType(Ty, Val)) return failure(true);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000207 V = ConstPoolSInt::get(Ty, Val);
Chris Lattner00950542001-06-06 20:29:01 +0000208 break;
209 }
210
211 case Type::LongTyID: {
212 int64_t Val;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000213 if (read_vbr(Buf, EndBuf, Val)) return failure(true);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000214 V = ConstPoolSInt::get(Ty, Val);
Chris Lattner00950542001-06-06 20:29:01 +0000215 break;
216 }
217
Chris Lattnera1375302001-07-15 00:17:18 +0000218 case Type::FloatTyID: {
219 float F;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000220 if (input_data(Buf, EndBuf, &F, &F+1)) return failure(true);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000221 V = ConstPoolFP::get(Ty, F);
Chris Lattnera1375302001-07-15 00:17:18 +0000222 break;
223 }
224
225 case Type::DoubleTyID: {
226 double Val;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000227 if (input_data(Buf, EndBuf, &Val, &Val+1)) return failure(true);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000228 V = ConstPoolFP::get(Ty, Val);
Chris Lattnera1375302001-07-15 00:17:18 +0000229 break;
230 }
231
Chris Lattner00950542001-06-06 20:29:01 +0000232 case Type::TypeTyID:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000233 assert(0 && "Type constants should be handled seperately!!!");
234 abort();
Chris Lattner00950542001-06-06 20:29:01 +0000235
236 case Type::ArrayTyID: {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000237 const ArrayType *AT = cast<const ArrayType>(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000238 unsigned NumElements;
239 if (AT->isSized()) // Sized array, # elements stored in type!
240 NumElements = (unsigned)AT->getNumElements();
241 else // Unsized array, # elements stored in stream!
Chris Lattner3d3f2892001-07-28 17:50:18 +0000242 if (read_vbr(Buf, EndBuf, NumElements)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000243
244 vector<ConstPoolVal *> Elements;
245 while (NumElements--) { // Read all of the elements of the constant.
246 unsigned Slot;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000247 if (read_vbr(Buf, EndBuf, Slot)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000248 Value *V = getValue(AT->getElementType(), Slot, false);
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000249 if (!V || !isa<ConstPoolVal>(V)) return failure(true);
250 Elements.push_back(cast<ConstPoolVal>(V));
Chris Lattner00950542001-06-06 20:29:01 +0000251 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000252 V = ConstPoolArray::get(AT, Elements);
Chris Lattner00950542001-06-06 20:29:01 +0000253 break;
254 }
255
256 case Type::StructTyID: {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000257 const StructType *ST = cast<StructType>(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000258 const StructType::ElementTypes &ET = ST->getElementTypes();
259
260 vector<ConstPoolVal *> Elements;
261 for (unsigned i = 0; i < ET.size(); ++i) {
262 unsigned Slot;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000263 if (read_vbr(Buf, EndBuf, Slot)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000264 Value *V = getValue(ET[i], Slot, false);
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000265 if (!V || !isa<ConstPoolVal>(V))
Chris Lattner3d3f2892001-07-28 17:50:18 +0000266 return failure(true);
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000267 Elements.push_back(cast<ConstPoolVal>(V));
Chris Lattner00950542001-06-06 20:29:01 +0000268 }
269
Chris Lattner1d670cc2001-09-07 16:37:43 +0000270 V = ConstPoolStruct::get(ST, Elements);
Chris Lattner00950542001-06-06 20:29:01 +0000271 break;
272 }
273
Chris Lattner1a1cb112001-09-30 22:46:54 +0000274 case Type::PointerTyID: {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000275 const PointerType *PT = cast<const PointerType>(Ty);
Chris Lattner1a1cb112001-09-30 22:46:54 +0000276 unsigned SubClass;
277 if (read_vbr(Buf, EndBuf, SubClass)) return failure(true);
Chris Lattner05950c32001-10-13 06:47:01 +0000278 switch (SubClass) {
279 case 0: // ConstPoolPointerNull value...
280 V = ConstPoolPointerNull::get(PT);
281 break;
Chris Lattner1a1cb112001-09-30 22:46:54 +0000282
Chris Lattnerc18545d2001-10-15 13:21:42 +0000283 case 1: { // ConstPoolPointerRef value...
Chris Lattner05950c32001-10-13 06:47:01 +0000284 unsigned Slot;
285 if (read_vbr(Buf, EndBuf, Slot)) return failure(true);
286 BCR_TRACE(4, "CPPR: Type: '" << Ty << "' slot: " << Slot << "\n");
Chris Lattner1a1cb112001-09-30 22:46:54 +0000287
Chris Lattner05950c32001-10-13 06:47:01 +0000288 // Check to see if we have already read this global variable yet...
289 Value *Val = getValue(PT, Slot, false);
290 GlobalValue *GV;
291 if (Val) {
292 if (!(GV = dyn_cast<GlobalValue>(Val))) return failure(true);
293 BCR_TRACE(5, "Value Found in ValueTable!\n");
294 } else { // Nope... see if we have previously forward ref'd it
295 GlobalRefsType::iterator I = GlobalRefs.find(make_pair(PT, Slot));
296 if (I != GlobalRefs.end()) {
297 BCR_TRACE(5, "Previous forward ref found!\n");
298 GV = I->second;
299 } else {
300 BCR_TRACE(5, "Creating new forward ref variable!\n");
301
302 // Create a placeholder for the global variable reference...
303 GlobalVariable *GVar = new GlobalVariable(PT->getValueType(), false);
304
305 // Keep track of the fact that we have a forward ref to recycle it
306 GlobalRefs.insert(make_pair(make_pair(PT, Slot), GVar));
307
308 // Must temporarily push this value into the module table...
309 TheModule->getGlobalList().push_back(GVar);
310 GV = GVar;
311 }
312 }
313
Chris Lattnerc18545d2001-10-15 13:21:42 +0000314 V = ConstPoolPointerRef::get(GV);
Chris Lattner05950c32001-10-13 06:47:01 +0000315 break;
316 }
317 default:
318 return failure(true);
319 }
Chris Lattner1a1cb112001-09-30 22:46:54 +0000320 break;
321 }
322
Chris Lattner00950542001-06-06 20:29:01 +0000323 default:
324 cerr << __FILE__ << ":" << __LINE__
325 << ": Don't know how to deserialize constant value of type '"
326 << Ty->getName() << "'\n";
Chris Lattner3d3f2892001-07-28 17:50:18 +0000327 return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000328 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000329
Chris Lattner00950542001-06-06 20:29:01 +0000330 return false;
331}
332
333bool BytecodeParser::ParseConstantPool(const uchar *&Buf, const uchar *EndBuf,
Chris Lattner1d670cc2001-09-07 16:37:43 +0000334 ValueTable &Tab,
335 TypeValuesListTy &TypeTab) {
Chris Lattner00950542001-06-06 20:29:01 +0000336 while (Buf < EndBuf) {
337 unsigned NumEntries, Typ;
338
339 if (read_vbr(Buf, EndBuf, NumEntries) ||
Chris Lattner3d3f2892001-07-28 17:50:18 +0000340 read_vbr(Buf, EndBuf, Typ)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000341 const Type *Ty = getType(Typ);
Chris Lattner3d3f2892001-07-28 17:50:18 +0000342 if (Ty == 0) return failure(true);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000343 BCR_TRACE(3, "Type: '" << Ty << "' NumEntries: " << NumEntries << "\n");
Chris Lattner00950542001-06-06 20:29:01 +0000344
Chris Lattner1d670cc2001-09-07 16:37:43 +0000345 if (Typ == Type::TypeTyID) {
346 if (parseTypeConstants(Buf, EndBuf, TypeTab, NumEntries)) return true;
347 } else {
Chris Lattner7eadfa12001-10-24 06:21:22 +0000348 for (unsigned i = 0; i < NumEntries; ++i) {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000349 ConstPoolVal *I;
350 if (parseConstPoolValue(Buf, EndBuf, Ty, I)) return failure(true);
351 BCR_TRACE(4, "Read Constant: '" << I << "'\n");
Chris Lattner05950c32001-10-13 06:47:01 +0000352 if (insertValue(I, Tab) == -1) return failure(true);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000353 }
Chris Lattner00950542001-06-06 20:29:01 +0000354 }
355 }
356
Chris Lattner3d3f2892001-07-28 17:50:18 +0000357 if (Buf > EndBuf) return failure(true);
358 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000359}