1 //===- ReadConst.cpp - Code to constants and constant pools ---------------===//
3 // This file implements functionality to deserialize constants and entire
6 // Note that this library should be as fast as possible, reentrant, and
9 //===----------------------------------------------------------------------===//
11 #include "ReaderInternals.h"
12 #include "llvm/Module.h"
13 #include "llvm/Constants.h"
14 #include "llvm/GlobalVariable.h"
20 const Type *BytecodeParser::parseTypeConstant(const uchar *&Buf,
21 const uchar *EndBuf) {
23 if (read_vbr(Buf, EndBuf, PrimType)) return failure<const Type*>(0);
26 if ((Val = Type::getPrimitiveType((Type::PrimitiveID)PrimType)))
30 case Type::FunctionTyID: {
32 if (read_vbr(Buf, EndBuf, Typ)) return failure(Val);
33 const Type *RetType = getType(Typ);
34 if (RetType == 0) return failure(Val);
37 if (read_vbr(Buf, EndBuf, NumParams)) return failure(Val);
39 std::vector<const Type*> Params;
41 if (read_vbr(Buf, EndBuf, Typ)) return failure(Val);
42 const Type *Ty = getType(Typ);
43 if (Ty == 0) return failure(Val);
47 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
48 if (isVarArg) Params.pop_back();
50 return FunctionType::get(RetType, Params, isVarArg);
52 case Type::ArrayTyID: {
54 if (read_vbr(Buf, EndBuf, ElTyp)) return failure(Val);
55 const Type *ElementType = getType(ElTyp);
56 if (ElementType == 0) return failure(Val);
59 if (read_vbr(Buf, EndBuf, NumElements)) return failure(Val);
61 BCR_TRACE(5, "Array Type Constant #" << ElTyp << " size="
62 << NumElements << "\n");
63 return ArrayType::get(ElementType, NumElements);
65 case Type::StructTyID: {
67 std::vector<const Type*> Elements;
69 if (read_vbr(Buf, EndBuf, Typ)) return failure(Val);
70 while (Typ) { // List is terminated by void/0 typeid
71 const Type *Ty = getType(Typ);
72 if (Ty == 0) return failure(Val);
73 Elements.push_back(Ty);
75 if (read_vbr(Buf, EndBuf, Typ)) return failure(Val);
78 return StructType::get(Elements);
80 case Type::PointerTyID: {
82 if (read_vbr(Buf, EndBuf, ElTyp)) return failure(Val);
83 BCR_TRACE(5, "Pointer Type Constant #" << (ElTyp-14) << "\n");
84 const Type *ElementType = getType(ElTyp);
85 if (ElementType == 0) return failure(Val);
86 return PointerType::get(ElementType);
89 case Type::OpaqueTyID: {
90 return OpaqueType::get();
94 std::cerr << __FILE__ << ":" << __LINE__
95 << ": Don't know how to deserialize"
96 << " primitive Type " << PrimType << "\n";
101 // refineAbstractType - The callback method is invoked when one of the
102 // elements of TypeValues becomes more concrete...
104 void BytecodeParser::refineAbstractType(const DerivedType *OldType,
105 const Type *NewType) {
106 if (OldType == NewType &&
107 OldType->isAbstract()) return; // Type is modified, but same
109 TypeValuesListTy::iterator I = find(MethodTypeValues.begin(),
110 MethodTypeValues.end(), OldType);
111 if (I == MethodTypeValues.end()) {
112 I = find(ModuleTypeValues.begin(), ModuleTypeValues.end(), OldType);
113 assert(I != ModuleTypeValues.end() &&
114 "Can't refine a type I don't know about!");
117 if (OldType == NewType) {
118 assert(!OldType->isAbstract());
119 I->removeUserFromConcrete();
121 *I = NewType; // Update to point to new, more refined type.
127 // parseTypeConstants - We have to use this wierd code to handle recursive
128 // types. We know that recursive types will only reference the current slab of
129 // values in the type plane, but they can forward reference types before they
130 // have been read. For example, Type #0 might be '{ Ty#1 }' and Type #1 might
131 // be 'Ty#0*'. When reading Type #0, type number one doesn't exist. To fix
132 // this ugly problem, we pesimistically insert an opaque type for each type we
133 // are about to read. This means that forward references will resolve to
134 // something and when we reread the type later, we can replace the opaque type
135 // with a new resolved concrete type.
137 void debug_type_tables();
138 bool BytecodeParser::parseTypeConstants(const uchar *&Buf, const uchar *EndBuf,
139 TypeValuesListTy &Tab,
140 unsigned NumEntries) {
141 assert(Tab.size() == 0 && "should not have read type constants in before!");
143 // Insert a bunch of opaque types to be resolved later...
144 for (unsigned i = 0; i < NumEntries; ++i)
145 Tab.push_back(PATypeHandle<Type>(OpaqueType::get(), this));
147 // Loop through reading all of the types. Forward types will make use of the
148 // opaque types just inserted.
150 for (unsigned i = 0; i < NumEntries; ++i) {
151 const Type *NewTy = parseTypeConstant(Buf, EndBuf), *OldTy = Tab[i].get();
152 if (NewTy == 0) return failure(true);
153 BCR_TRACE(4, "#" << i << ": Read Type Constant: '" << NewTy <<
154 "' Replacing: " << OldTy << "\n");
156 // Don't insertValue the new type... instead we want to replace the opaque
157 // type with the new concrete value...
160 // Refine the abstract type to the new type. This causes all uses of the
161 // abstract type to use the newty. This also will cause the opaque type
164 ((DerivedType*)Tab[i].get())->refineAbstractTypeTo(NewTy);
166 // This should have replace the old opaque type with the new type in the
167 // value table... or with a preexisting type that was already in the system
168 assert(Tab[i] != OldTy && "refineAbstractType didn't work!");
171 BCR_TRACE(5, "Resulting types:\n");
172 for (unsigned i = 0; i < NumEntries; ++i) {
173 BCR_TRACE(5, (void*)Tab[i].get() << " - " << Tab[i].get() << "\n");
180 bool BytecodeParser::parseConstantValue(const uchar *&Buf, const uchar *EndBuf,
181 const Type *Ty, Constant *&V) {
183 // We must check for a ConstantExpr before switching by type because
184 // a ConstantExpr can be of any type, and has no explicit value.
186 unsigned isExprNumArgs; // 0 if not expr; numArgs if is expr
187 if (read_vbr(Buf, EndBuf, isExprNumArgs)) return failure(true);
190 std::vector<Constant*> argVec;
191 argVec.reserve(isExprNumArgs);
193 if (read_vbr(Buf, EndBuf, opCode)) return failure(true);
195 // Read the slot number and types of each of the arguments
196 for (unsigned i=0; i < isExprNumArgs; ++i) {
197 unsigned argValSlot, argTypeSlot;
198 if (read_vbr(Buf, EndBuf, argValSlot)) return failure(true);
199 if (read_vbr(Buf, EndBuf, argTypeSlot)) return failure(true);
200 const Type *argTy = getType(argTypeSlot);
201 if (argTy == 0) return failure(true);
203 BCR_TRACE(4, "CE Arg " << i << ": Type: '" << argTy << "' slot: " << argValSlot << "\n");
205 // Get the arg value from its slot if it exists, otherwise a placeholder
206 Value *Val = getValue(argTy, argValSlot, false);
209 if (!(C = dyn_cast<Constant>(Val))) return failure(true);
210 BCR_TRACE(5, "Constant Found in ValueTable!\n");
211 } else { // Nope... find or create a forward ref. for it
212 C = fwdRefs.GetFwdRefToConstant(argTy, argValSlot);
217 // Construct a ConstantExpr of the appropriate kind
218 if (isExprNumArgs == 1) { // All one-operand expressions
219 V = ConstantExpr::get(opCode, argVec[0], Ty);
220 } else if (opCode == Instruction::GetElementPtr) { // GetElementPtr
221 std::vector<Value*> IdxList(argVec.begin()+1, argVec.end());
222 V = ConstantExpr::get(opCode, argVec[0], IdxList, Ty);
223 } else { // All other 2-operand expressions
224 V = ConstantExpr::get(opCode, argVec[0], argVec[1], Ty);
229 // Ok, not an ConstantExpr. We now know how to read the given type...
230 switch (Ty->getPrimitiveID()) {
231 case Type::BoolTyID: {
233 if (read_vbr(Buf, EndBuf, Val)) return failure(true);
234 if (Val != 0 && Val != 1) return failure(true);
235 V = ConstantBool::get(Val == 1);
239 case Type::UByteTyID: // Unsigned integer types...
240 case Type::UShortTyID:
241 case Type::UIntTyID: {
243 if (read_vbr(Buf, EndBuf, Val)) return failure(true);
244 if (!ConstantUInt::isValueValidForType(Ty, Val)) return failure(true);
245 V = ConstantUInt::get(Ty, Val);
249 case Type::ULongTyID: {
251 if (read_vbr(Buf, EndBuf, Val)) return failure(true);
252 V = ConstantUInt::get(Ty, Val);
256 case Type::SByteTyID: // Unsigned integer types...
257 case Type::ShortTyID:
258 case Type::IntTyID: {
260 if (read_vbr(Buf, EndBuf, Val)) return failure(true);
261 if (!ConstantSInt::isValueValidForType(Ty, Val)) return failure(true);
262 V = ConstantSInt::get(Ty, Val);
266 case Type::LongTyID: {
268 if (read_vbr(Buf, EndBuf, Val)) return failure(true);
269 V = ConstantSInt::get(Ty, Val);
273 case Type::FloatTyID: {
275 if (input_data(Buf, EndBuf, &F, &F+1)) return failure(true);
276 V = ConstantFP::get(Ty, F);
280 case Type::DoubleTyID: {
282 if (input_data(Buf, EndBuf, &Val, &Val+1)) return failure(true);
283 V = ConstantFP::get(Ty, Val);
288 assert(0 && "Type constants should be handled seperately!!!");
291 case Type::ArrayTyID: {
292 const ArrayType *AT = cast<const ArrayType>(Ty);
293 unsigned NumElements = AT->getNumElements();
295 std::vector<Constant*> Elements;
296 while (NumElements--) { // Read all of the elements of the constant.
298 if (read_vbr(Buf, EndBuf, Slot)) return failure(true);
299 Value *V = getValue(AT->getElementType(), Slot, false);
300 if (!V || !isa<Constant>(V)) return failure(true);
301 Elements.push_back(cast<Constant>(V));
303 V = ConstantArray::get(AT, Elements);
307 case Type::StructTyID: {
308 const StructType *ST = cast<StructType>(Ty);
309 const StructType::ElementTypes &ET = ST->getElementTypes();
311 std::vector<Constant *> Elements;
312 for (unsigned i = 0; i < ET.size(); ++i) {
314 if (read_vbr(Buf, EndBuf, Slot)) return failure(true);
315 Value *V = getValue(ET[i], Slot, false);
316 if (!V || !isa<Constant>(V))
317 return failure(true);
318 Elements.push_back(cast<Constant>(V));
321 V = ConstantStruct::get(ST, Elements);
325 case Type::PointerTyID: {
326 const PointerType *PT = cast<const PointerType>(Ty);
328 if (read_vbr(Buf, EndBuf, SubClass)) return failure(true);
330 case 0: // ConstantPointerNull value...
331 V = ConstantPointerNull::get(PT);
334 case 1: { // ConstantPointerRef value...
336 if (read_vbr(Buf, EndBuf, Slot)) return failure(true);
337 BCR_TRACE(4, "CPR: Type: '" << Ty << "' slot: " << Slot << "\n");
339 // Check to see if we have already read this global variable yet...
340 Value *Val = getValue(PT, Slot, false);
343 if (!(GV = dyn_cast<GlobalValue>(Val))) return failure(true);
344 BCR_TRACE(5, "Value Found in ValueTable!\n");
345 } else { // Nope... find or create a forward ref. for it
346 GV = fwdRefs.GetFwdRefToGlobal(PT, Slot);
348 V = ConstantPointerRef::get(GV);
353 BCR_TRACE(5, "UNKNOWN Pointer Constant Type!\n");
354 return failure(true);
360 std::cerr << __FILE__ << ":" << __LINE__
361 << ": Don't know how to deserialize constant value of type '"
362 << Ty->getName() << "'\n";
363 return failure(true);
369 bool BytecodeParser::ParseConstantPool(const uchar *&Buf, const uchar *EndBuf,
371 TypeValuesListTy &TypeTab) {
372 while (Buf < EndBuf) {
373 unsigned NumEntries, Typ;
375 if (read_vbr(Buf, EndBuf, NumEntries) ||
376 read_vbr(Buf, EndBuf, Typ)) return failure(true);
377 const Type *Ty = getType(Typ);
378 if (Ty == 0) return failure(true);
379 BCR_TRACE(3, "Type: '" << Ty << "' NumEntries: " << NumEntries << "\n");
381 if (Typ == Type::TypeTyID) {
382 if (parseTypeConstants(Buf, EndBuf, TypeTab, NumEntries)) return true;
384 for (unsigned i = 0; i < NumEntries; ++i) {
387 if (parseConstantValue(Buf, EndBuf, Ty, I)) return failure(true);
388 assert(I && "parseConstantValue returned `!failure' and NULL result");
389 BCR_TRACE(4, "Read Constant: '" << I << "'\n");
390 if ((Slot = insertValue(I, Tab)) < 0) return failure(true);
391 resolveRefsToConstant(I, (unsigned) Slot);
396 if (Buf > EndBuf) return failure(true);