Add more support for new style casts
[oota-llvm.git] / lib / Bytecode / Reader / ConstantReader.cpp
1 //===- 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"
16 #include <algorithm>
17
18
19
20 const Type *BytecodeParser::parseTypeConstant(const uchar *&Buf,
21                                               const uchar *EndBuf) {
22   unsigned PrimType;
23   if (read_vbr(Buf, EndBuf, PrimType)) return failure<const Type*>(0);
24
25   const Type *Val = 0;
26   if ((Val = Type::getPrimitiveType((Type::PrimitiveID)PrimType)))
27     return Val;
28   
29   switch (PrimType) {
30   case Type::MethodTyID: {
31     unsigned Typ;
32     if (read_vbr(Buf, EndBuf, Typ)) return failure(Val);
33     const Type *RetType = getType(Typ);
34     if (RetType == 0) return failure(Val);
35
36     unsigned NumParams;
37     if (read_vbr(Buf, EndBuf, NumParams)) return failure(Val);
38
39     vector<const Type*> Params;
40     while (NumParams--) {
41       if (read_vbr(Buf, EndBuf, Typ)) return failure(Val);
42       const Type *Ty = getType(Typ);
43       if (Ty == 0) return failure(Val);
44       Params.push_back(Ty);
45     }
46
47     Val = MethodType::get(RetType, Params);
48     break;
49   }
50   case Type::ArrayTyID: {
51     unsigned ElTyp;
52     if (read_vbr(Buf, EndBuf, ElTyp)) return failure(Val);
53     const Type *ElementType = getType(ElTyp);
54     if (ElementType == 0) return failure(Val);
55
56     int NumElements;
57     if (read_vbr(Buf, EndBuf, NumElements)) return failure(Val);
58     Val = ArrayType::get(ElementType, NumElements);
59     break;
60   }
61   case Type::StructTyID: {
62     unsigned Typ;
63     vector<const Type*> Elements;
64
65     if (read_vbr(Buf, EndBuf, Typ)) return failure(Val);
66     while (Typ) {         // List is terminated by void/0 typeid
67       const Type *Ty = getType(Typ);
68       if (Ty == 0) return failure(Val);
69       Elements.push_back(Ty);
70       
71       if (read_vbr(Buf, EndBuf, Typ)) return failure(Val);
72     }
73
74     Val = StructType::get(Elements);
75     break;
76   }
77   case Type::PointerTyID: {
78     unsigned ElTyp;
79     if (read_vbr(Buf, EndBuf, ElTyp)) return failure(Val);
80     const Type *ElementType = getType(ElTyp);
81     if (ElementType == 0) return failure(Val);
82     Val = PointerType::get(ElementType);
83     break;
84   }
85
86   default:
87     cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to deserialize"
88          << " primitive Type " << PrimType << "\n";
89     return failure(Val);
90   }
91
92   return Val;
93 }
94
95 // refineAbstractType - The callback method is invoked when one of the
96 // elements of TypeValues becomes more concrete...
97 //
98 void 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 //
123 bool 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     cast<DerivedType>(Tab[i+BaseLevel].get())->refineAbstractTypeTo(NewTy);
153
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++) {
161     BCR_TRACE(5, cast<Type>(Tab[i+BaseLevel]) << "\n");
162   }
163   return false;
164 }
165
166
167 bool 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;
173     if (read_vbr(Buf, EndBuf, Val)) return failure(true);
174     if (Val != 0 && Val != 1) return failure(true);
175     V = ConstPoolBool::get(Val == 1);
176     break;
177   }
178
179   case Type::UByteTyID:   // Unsigned integer types...
180   case Type::UShortTyID:
181   case Type::UIntTyID: {
182     unsigned Val;
183     if (read_vbr(Buf, EndBuf, Val)) return failure(true);
184     if (!ConstPoolUInt::isValueValidForType(Ty, Val)) return failure(true);
185     V = ConstPoolUInt::get(Ty, Val);
186     break;
187   }
188
189   case Type::ULongTyID: {
190     uint64_t Val;
191     if (read_vbr(Buf, EndBuf, Val)) return failure(true);
192     V = ConstPoolUInt::get(Ty, Val);
193     break;
194   }
195
196   case Type::SByteTyID:   // Unsigned integer types...
197   case Type::ShortTyID:
198   case Type::IntTyID: {
199     int Val;
200     if (read_vbr(Buf, EndBuf, Val)) return failure(true);
201     if (!ConstPoolSInt::isValueValidForType(Ty, Val)) return failure(true);
202     V = ConstPoolSInt::get(Ty, Val);
203     break;
204   }
205
206   case Type::LongTyID: {
207     int64_t Val;
208     if (read_vbr(Buf, EndBuf, Val)) return failure(true);
209     V = ConstPoolSInt::get(Ty, Val);
210     break;
211   }
212
213   case Type::FloatTyID: {
214     float F;
215     if (input_data(Buf, EndBuf, &F, &F+1)) return failure(true);
216     V = ConstPoolFP::get(Ty, F);
217     break;
218   }
219
220   case Type::DoubleTyID: {
221     double Val;
222     if (input_data(Buf, EndBuf, &Val, &Val+1)) return failure(true);
223     V = ConstPoolFP::get(Ty, Val);
224     break;
225   }
226
227   case Type::TypeTyID:
228     assert(0 && "Type constants should be handled seperately!!!");
229     abort();
230
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!
237       if (read_vbr(Buf, EndBuf, NumElements)) return failure(true);
238
239     vector<ConstPoolVal *> Elements;
240     while (NumElements--) {   // Read all of the elements of the constant.
241       unsigned Slot;
242       if (read_vbr(Buf, EndBuf, Slot)) return failure(true);
243       Value *V = getValue(AT->getElementType(), Slot, false);
244       if (!V || !V->isConstant()) return failure(true);
245       Elements.push_back((ConstPoolVal*)V);
246     }
247     V = ConstPoolArray::get(AT, Elements);
248     break;
249   }
250
251   case Type::StructTyID: {
252     const StructType *ST = Ty->castStructType();
253     const StructType::ElementTypes &ET = ST->getElementTypes();
254
255     vector<ConstPoolVal *> Elements;
256     for (unsigned i = 0; i < ET.size(); ++i) {
257       unsigned Slot;
258       if (read_vbr(Buf, EndBuf, Slot)) return failure(true);
259       Value *V = getValue(ET[i], Slot, false);
260       if (!V || !V->isConstant())
261         return failure(true);
262       Elements.push_back((ConstPoolVal*)V);      
263     }
264
265     V = ConstPoolStruct::get(ST, Elements);
266     break;
267   }    
268
269   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
280   default:
281     cerr << __FILE__ << ":" << __LINE__ 
282          << ": Don't know how to deserialize constant value of type '"
283          << Ty->getName() << "'\n";
284     return failure(true);
285   }
286
287   return false;
288 }
289
290 bool BytecodeParser::ParseConstantPool(const uchar *&Buf, const uchar *EndBuf,
291                                        ValueTable &Tab, 
292                                        TypeValuesListTy &TypeTab) {
293   while (Buf < EndBuf) {
294     unsigned NumEntries, Typ;
295
296     if (read_vbr(Buf, EndBuf, NumEntries) ||
297         read_vbr(Buf, EndBuf, Typ)) return failure(true);
298     const Type *Ty = getType(Typ);
299     if (Ty == 0) return failure(true);
300     BCR_TRACE(3, "Type: '" << Ty << "'  NumEntries: " << NumEntries << "\n");
301
302     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       }
311     }
312   }
313   
314   if (Buf > EndBuf) return failure(true);
315   return false;
316 }