blob: 3865a5e9825e4c9d7ce4ee9ed2175d21ac36a1c4 [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
90 default:
91 cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to deserialize"
92 << " primitive Type " << PrimType << "\n";
Chris Lattner1d670cc2001-09-07 16:37:43 +000093 return failure(Val);
Chris Lattner00950542001-06-06 20:29:01 +000094 }
95
Chris Lattner1d670cc2001-09-07 16:37:43 +000096 return Val;
97}
98
99// refineAbstractType - The callback method is invoked when one of the
100// elements of TypeValues becomes more concrete...
101//
102void BytecodeParser::refineAbstractType(const DerivedType *OldType,
103 const Type *NewType) {
Chris Lattner05950c32001-10-13 06:47:01 +0000104 if (OldType == NewType) return; // Type is modified, but same
105
Chris Lattner1d670cc2001-09-07 16:37:43 +0000106 TypeValuesListTy::iterator I = find(MethodTypeValues.begin(),
107 MethodTypeValues.end(), OldType);
108 if (I == MethodTypeValues.end()) {
109 I = find(ModuleTypeValues.begin(), ModuleTypeValues.end(), OldType);
110 assert(I != ModuleTypeValues.end() &&
111 "Can't refine a type I don't know about!");
112 }
113
114 *I = NewType; // Update to point to new, more refined type.
115}
116
117
118
119// parseTypeConstants - We have to use this wierd code to handle recursive
120// types. We know that recursive types will only reference the current slab of
121// values in the type plane, but they can forward reference types before they
122// have been read. For example, Type #0 might be '{ Ty#1 }' and Type #1 might
123// be 'Ty#0*'. When reading Type #0, type number one doesn't exist. To fix
124// this ugly problem, we pesimistically insert an opaque type for each type we
125// are about to read. This means that forward references will resolve to
126// something and when we reread the type later, we can replace the opaque type
127// with a new resolved concrete type.
128//
129bool BytecodeParser::parseTypeConstants(const uchar *&Buf, const uchar *EndBuf,
130 TypeValuesListTy &Tab,
131 unsigned NumEntries) {
Chris Lattner05950c32001-10-13 06:47:01 +0000132 assert(Tab.size() == 0 && "should not have read type constants in before!");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000133
134 // Insert a bunch of opaque types to be resolved later...
135 for (unsigned i = 0; i < NumEntries; i++)
136 Tab.push_back(PATypeHandle<Type>(OpaqueType::get(), this));
137
138 // Loop through reading all of the types. Forward types will make use of the
139 // opaque types just inserted.
140 //
141 for (unsigned i = 0; i < NumEntries; i++) {
Chris Lattner05950c32001-10-13 06:47:01 +0000142 const Type *NewTy = parseTypeConstant(Buf, EndBuf), *OldTy = Tab[i].get();
Chris Lattner1d670cc2001-09-07 16:37:43 +0000143 if (NewTy == 0) return failure(true);
144 BCR_TRACE(4, "Read Type Constant: '" << NewTy << "'\n");
145
146 // Don't insertValue the new type... instead we want to replace the opaque
147 // type with the new concrete value...
148 //
149
150 // Refine the abstract type to the new type. This causes all uses of the
151 // abstract type to use the newty. This also will cause the opaque type
152 // to be deleted...
153 //
Chris Lattner05950c32001-10-13 06:47:01 +0000154 cast<DerivedType>(Tab[i].get())->refineAbstractTypeTo(NewTy);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000155
156 // This should have replace the old opaque type with the new type in the
Chris Lattner05950c32001-10-13 06:47:01 +0000157 // value table... or with a preexisting type that was already in the system
158 assert(Tab[i] != OldTy && "refineAbstractType didn't work!");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000159 }
160
161 BCR_TRACE(5, "Resulting types:\n");
162 for (unsigned i = 0; i < NumEntries; i++) {
Chris Lattner05950c32001-10-13 06:47:01 +0000163 BCR_TRACE(5, cast<const Type>(Tab[i]) << "\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000164 }
Chris Lattner00950542001-06-06 20:29:01 +0000165 return false;
166}
167
Chris Lattner1d670cc2001-09-07 16:37:43 +0000168
Chris Lattner00950542001-06-06 20:29:01 +0000169bool BytecodeParser::parseConstPoolValue(const uchar *&Buf,
170 const uchar *EndBuf,
171 const Type *Ty, ConstPoolVal *&V) {
172 switch (Ty->getPrimitiveID()) {
173 case Type::BoolTyID: {
174 unsigned Val;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000175 if (read_vbr(Buf, EndBuf, Val)) return failure(true);
176 if (Val != 0 && Val != 1) return failure(true);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000177 V = ConstPoolBool::get(Val == 1);
Chris Lattner00950542001-06-06 20:29:01 +0000178 break;
179 }
180
181 case Type::UByteTyID: // Unsigned integer types...
182 case Type::UShortTyID:
183 case Type::UIntTyID: {
184 unsigned Val;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000185 if (read_vbr(Buf, EndBuf, Val)) return failure(true);
186 if (!ConstPoolUInt::isValueValidForType(Ty, Val)) return failure(true);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000187 V = ConstPoolUInt::get(Ty, Val);
Chris Lattner00950542001-06-06 20:29:01 +0000188 break;
189 }
190
191 case Type::ULongTyID: {
192 uint64_t Val;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000193 if (read_vbr(Buf, EndBuf, Val)) return failure(true);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000194 V = ConstPoolUInt::get(Ty, Val);
Chris Lattner00950542001-06-06 20:29:01 +0000195 break;
196 }
197
198 case Type::SByteTyID: // Unsigned integer types...
199 case Type::ShortTyID:
200 case Type::IntTyID: {
201 int Val;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000202 if (read_vbr(Buf, EndBuf, Val)) return failure(true);
203 if (!ConstPoolSInt::isValueValidForType(Ty, Val)) return failure(true);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000204 V = ConstPoolSInt::get(Ty, Val);
Chris Lattner00950542001-06-06 20:29:01 +0000205 break;
206 }
207
208 case Type::LongTyID: {
209 int64_t Val;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000210 if (read_vbr(Buf, EndBuf, Val)) return failure(true);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000211 V = ConstPoolSInt::get(Ty, Val);
Chris Lattner00950542001-06-06 20:29:01 +0000212 break;
213 }
214
Chris Lattnera1375302001-07-15 00:17:18 +0000215 case Type::FloatTyID: {
216 float F;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000217 if (input_data(Buf, EndBuf, &F, &F+1)) return failure(true);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000218 V = ConstPoolFP::get(Ty, F);
Chris Lattnera1375302001-07-15 00:17:18 +0000219 break;
220 }
221
222 case Type::DoubleTyID: {
223 double Val;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000224 if (input_data(Buf, EndBuf, &Val, &Val+1)) return failure(true);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000225 V = ConstPoolFP::get(Ty, Val);
Chris Lattnera1375302001-07-15 00:17:18 +0000226 break;
227 }
228
Chris Lattner00950542001-06-06 20:29:01 +0000229 case Type::TypeTyID:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000230 assert(0 && "Type constants should be handled seperately!!!");
231 abort();
Chris Lattner00950542001-06-06 20:29:01 +0000232
233 case Type::ArrayTyID: {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000234 const ArrayType *AT = cast<const ArrayType>(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000235 unsigned NumElements;
236 if (AT->isSized()) // Sized array, # elements stored in type!
237 NumElements = (unsigned)AT->getNumElements();
238 else // Unsized array, # elements stored in stream!
Chris Lattner3d3f2892001-07-28 17:50:18 +0000239 if (read_vbr(Buf, EndBuf, NumElements)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000240
241 vector<ConstPoolVal *> Elements;
242 while (NumElements--) { // Read all of the elements of the constant.
243 unsigned Slot;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000244 if (read_vbr(Buf, EndBuf, Slot)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000245 Value *V = getValue(AT->getElementType(), Slot, false);
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000246 if (!V || !isa<ConstPoolVal>(V)) return failure(true);
247 Elements.push_back(cast<ConstPoolVal>(V));
Chris Lattner00950542001-06-06 20:29:01 +0000248 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000249 V = ConstPoolArray::get(AT, Elements);
Chris Lattner00950542001-06-06 20:29:01 +0000250 break;
251 }
252
253 case Type::StructTyID: {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000254 const StructType *ST = cast<StructType>(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000255 const StructType::ElementTypes &ET = ST->getElementTypes();
256
257 vector<ConstPoolVal *> Elements;
258 for (unsigned i = 0; i < ET.size(); ++i) {
259 unsigned Slot;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000260 if (read_vbr(Buf, EndBuf, Slot)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000261 Value *V = getValue(ET[i], Slot, false);
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000262 if (!V || !isa<ConstPoolVal>(V))
Chris Lattner3d3f2892001-07-28 17:50:18 +0000263 return failure(true);
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000264 Elements.push_back(cast<ConstPoolVal>(V));
Chris Lattner00950542001-06-06 20:29:01 +0000265 }
266
Chris Lattner1d670cc2001-09-07 16:37:43 +0000267 V = ConstPoolStruct::get(ST, Elements);
Chris Lattner00950542001-06-06 20:29:01 +0000268 break;
269 }
270
Chris Lattner1a1cb112001-09-30 22:46:54 +0000271 case Type::PointerTyID: {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000272 const PointerType *PT = cast<const PointerType>(Ty);
Chris Lattner1a1cb112001-09-30 22:46:54 +0000273 unsigned SubClass;
274 if (read_vbr(Buf, EndBuf, SubClass)) return failure(true);
Chris Lattner05950c32001-10-13 06:47:01 +0000275 switch (SubClass) {
276 case 0: // ConstPoolPointerNull value...
277 V = ConstPoolPointerNull::get(PT);
278 break;
Chris Lattner1a1cb112001-09-30 22:46:54 +0000279
Chris Lattner05950c32001-10-13 06:47:01 +0000280 case 1: { // ConstPoolPointerReference value...
281 unsigned Slot;
282 if (read_vbr(Buf, EndBuf, Slot)) return failure(true);
283 BCR_TRACE(4, "CPPR: Type: '" << Ty << "' slot: " << Slot << "\n");
Chris Lattner1a1cb112001-09-30 22:46:54 +0000284
Chris Lattner05950c32001-10-13 06:47:01 +0000285 // Check to see if we have already read this global variable yet...
286 Value *Val = getValue(PT, Slot, false);
287 GlobalValue *GV;
288 if (Val) {
289 if (!(GV = dyn_cast<GlobalValue>(Val))) return failure(true);
290 BCR_TRACE(5, "Value Found in ValueTable!\n");
291 } else { // Nope... see if we have previously forward ref'd it
292 GlobalRefsType::iterator I = GlobalRefs.find(make_pair(PT, Slot));
293 if (I != GlobalRefs.end()) {
294 BCR_TRACE(5, "Previous forward ref found!\n");
295 GV = I->second;
296 } else {
297 BCR_TRACE(5, "Creating new forward ref variable!\n");
298
299 // Create a placeholder for the global variable reference...
300 GlobalVariable *GVar = new GlobalVariable(PT->getValueType(), false);
301
302 // Keep track of the fact that we have a forward ref to recycle it
303 GlobalRefs.insert(make_pair(make_pair(PT, Slot), GVar));
304
305 // Must temporarily push this value into the module table...
306 TheModule->getGlobalList().push_back(GVar);
307 GV = GVar;
308 }
309 }
310
311 V = ConstPoolPointerReference::get(GV);
312 break;
313 }
314 default:
315 return failure(true);
316 }
Chris Lattner1a1cb112001-09-30 22:46:54 +0000317 break;
318 }
319
Chris Lattner00950542001-06-06 20:29:01 +0000320 default:
321 cerr << __FILE__ << ":" << __LINE__
322 << ": Don't know how to deserialize constant value of type '"
323 << Ty->getName() << "'\n";
Chris Lattner3d3f2892001-07-28 17:50:18 +0000324 return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000325 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000326
Chris Lattner00950542001-06-06 20:29:01 +0000327 return false;
328}
329
330bool BytecodeParser::ParseConstantPool(const uchar *&Buf, const uchar *EndBuf,
Chris Lattner1d670cc2001-09-07 16:37:43 +0000331 ValueTable &Tab,
332 TypeValuesListTy &TypeTab) {
Chris Lattner00950542001-06-06 20:29:01 +0000333 while (Buf < EndBuf) {
334 unsigned NumEntries, Typ;
335
336 if (read_vbr(Buf, EndBuf, NumEntries) ||
Chris Lattner3d3f2892001-07-28 17:50:18 +0000337 read_vbr(Buf, EndBuf, Typ)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000338 const Type *Ty = getType(Typ);
Chris Lattner3d3f2892001-07-28 17:50:18 +0000339 if (Ty == 0) return failure(true);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000340 BCR_TRACE(3, "Type: '" << Ty << "' NumEntries: " << NumEntries << "\n");
Chris Lattner00950542001-06-06 20:29:01 +0000341
Chris Lattner1d670cc2001-09-07 16:37:43 +0000342 if (Typ == Type::TypeTyID) {
343 if (parseTypeConstants(Buf, EndBuf, TypeTab, NumEntries)) return true;
344 } else {
345 for (unsigned i = 0; i < NumEntries; i++) {
346 ConstPoolVal *I;
347 if (parseConstPoolValue(Buf, EndBuf, Ty, I)) return failure(true);
348 BCR_TRACE(4, "Read Constant: '" << I << "'\n");
Chris Lattner05950c32001-10-13 06:47:01 +0000349 if (insertValue(I, Tab) == -1) return failure(true);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000350 }
Chris Lattner00950542001-06-06 20:29:01 +0000351 }
352 }
353
Chris Lattner3d3f2892001-07-28 17:50:18 +0000354 if (Buf > EndBuf) return failure(true);
355 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000356}