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"
16 const Type *BytecodeParser::parseTypeConstant(const unsigned char *&Buf,
17 const unsigned char *EndBuf) {
19 if (read_vbr(Buf, EndBuf, PrimType)) return 0;
22 if ((Val = Type::getPrimitiveType((Type::PrimitiveID)PrimType)))
26 case Type::FunctionTyID: {
28 if (read_vbr(Buf, EndBuf, Typ)) return Val;
29 const Type *RetType = getType(Typ);
30 if (RetType == 0) return Val;
33 if (read_vbr(Buf, EndBuf, NumParams)) return Val;
35 std::vector<const Type*> Params;
37 if (read_vbr(Buf, EndBuf, Typ)) return Val;
38 const Type *Ty = getType(Typ);
39 if (Ty == 0) return Val;
43 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
44 if (isVarArg) Params.pop_back();
46 return FunctionType::get(RetType, Params, isVarArg);
48 case Type::ArrayTyID: {
50 if (read_vbr(Buf, EndBuf, ElTyp)) return Val;
51 const Type *ElementType = getType(ElTyp);
52 if (ElementType == 0) return Val;
55 if (read_vbr(Buf, EndBuf, NumElements)) return Val;
57 BCR_TRACE(5, "Array Type Constant #" << ElTyp << " size="
58 << NumElements << "\n");
59 return ArrayType::get(ElementType, NumElements);
61 case Type::StructTyID: {
63 std::vector<const Type*> Elements;
65 if (read_vbr(Buf, EndBuf, Typ)) return Val;
66 while (Typ) { // List is terminated by void/0 typeid
67 const Type *Ty = getType(Typ);
68 if (Ty == 0) return Val;
69 Elements.push_back(Ty);
71 if (read_vbr(Buf, EndBuf, Typ)) return Val;
74 return StructType::get(Elements);
76 case Type::PointerTyID: {
78 if (read_vbr(Buf, EndBuf, ElTyp)) return Val;
79 BCR_TRACE(5, "Pointer Type Constant #" << (ElTyp-14) << "\n");
80 const Type *ElementType = getType(ElTyp);
81 if (ElementType == 0) return Val;
82 return PointerType::get(ElementType);
85 case Type::OpaqueTyID: {
86 return OpaqueType::get();
90 std::cerr << __FILE__ << ":" << __LINE__
91 << ": Don't know how to deserialize"
92 << " primitive Type " << PrimType << "\n";
97 // refineAbstractType - The callback method is invoked when one of the
98 // elements of TypeValues becomes more concrete...
100 void BytecodeParser::refineAbstractType(const DerivedType *OldType,
101 const Type *NewType) {
102 if (OldType == NewType &&
103 OldType->isAbstract()) return; // Type is modified, but same
105 TypeValuesListTy::iterator I = find(FunctionTypeValues.begin(),
106 FunctionTypeValues.end(), OldType);
107 if (I == FunctionTypeValues.end()) {
108 I = find(ModuleTypeValues.begin(), ModuleTypeValues.end(), OldType);
109 assert(I != ModuleTypeValues.end() &&
110 "Can't refine a type I don't know about!");
113 I->removeUserFromConcrete();
114 *I = NewType; // Update to point to new, more refined type.
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.
129 void debug_type_tables();
130 bool BytecodeParser::parseTypeConstants(const unsigned char *&Buf,
131 const unsigned char *EndBuf,
132 TypeValuesListTy &Tab,
133 unsigned NumEntries) {
134 assert(Tab.size() == 0 && "should not have read type constants in before!");
136 // Insert a bunch of opaque types to be resolved later...
137 for (unsigned i = 0; i < NumEntries; ++i)
138 Tab.push_back(PATypeHandle(OpaqueType::get(), this));
140 // Loop through reading all of the types. Forward types will make use of the
141 // opaque types just inserted.
143 for (unsigned i = 0; i < NumEntries; ++i) {
144 const Type *NewTy = parseTypeConstant(Buf, EndBuf), *OldTy = Tab[i].get();
145 if (NewTy == 0) return true;
146 BCR_TRACE(4, "#" << i << ": Read Type Constant: '" << NewTy <<
147 "' Replacing: " << OldTy << "\n");
149 // Don't insertValue the new type... instead we want to replace the opaque
150 // type with the new concrete value...
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
157 ((DerivedType*)Tab[i].get())->refineAbstractTypeTo(NewTy);
159 // This should have replace the old opaque type with the new type in the
160 // value table... or with a preexisting type that was already in the system
161 assert(Tab[i] != OldTy && "refineAbstractType didn't work!");
164 BCR_TRACE(5, "Resulting types:\n");
165 for (unsigned i = 0; i < NumEntries; ++i) {
166 BCR_TRACE(5, (void*)Tab[i].get() << " - " << Tab[i].get() << "\n");
173 bool BytecodeParser::parseConstantValue(const unsigned char *&Buf,
174 const unsigned char *EndBuf,
175 const Type *Ty, Constant *&V) {
177 // We must check for a ConstantExpr before switching by type because
178 // a ConstantExpr can be of any type, and has no explicit value.
180 unsigned isExprNumArgs; // 0 if not expr; numArgs if is expr
181 if (read_vbr(Buf, EndBuf, isExprNumArgs)) return true;
183 // FIXME: Encoding of constant exprs could be much more compact!
185 std::vector<Constant*> ArgVec;
186 ArgVec.reserve(isExprNumArgs);
187 if (read_vbr(Buf, EndBuf, Opcode)) return true;
189 // Read the slot number and types of each of the arguments
190 for (unsigned i = 0; i != isExprNumArgs; ++i) {
191 unsigned ArgValSlot, ArgTypeSlot;
192 if (read_vbr(Buf, EndBuf, ArgValSlot)) return true;
193 if (read_vbr(Buf, EndBuf, ArgTypeSlot)) return true;
194 const Type *ArgTy = getType(ArgTypeSlot);
195 if (ArgTy == 0) return true;
197 BCR_TRACE(4, "CE Arg " << i << ": Type: '" << ArgTy << "' slot: "
198 << ArgValSlot << "\n");
200 // Get the arg value from its slot if it exists, otherwise a placeholder
201 Constant *C = getConstantValue(ArgTy, ArgValSlot);
202 if (C == 0) return true;
206 // Construct a ConstantExpr of the appropriate kind
207 if (isExprNumArgs == 1) { // All one-operand expressions
208 assert(Opcode == Instruction::Cast);
209 V = ConstantExpr::getCast(ArgVec[0], Ty);
210 } else if (Opcode == Instruction::GetElementPtr) { // GetElementPtr
211 std::vector<Constant*> IdxList(ArgVec.begin()+1, ArgVec.end());
212 V = ConstantExpr::getGetElementPtr(ArgVec[0], IdxList);
213 } else { // All other 2-operand expressions
214 V = ConstantExpr::get(Opcode, ArgVec[0], ArgVec[1]);
219 // Ok, not an ConstantExpr. We now know how to read the given type...
220 switch (Ty->getPrimitiveID()) {
221 case Type::BoolTyID: {
223 if (read_vbr(Buf, EndBuf, Val)) return true;
224 if (Val != 0 && Val != 1) return true;
225 V = ConstantBool::get(Val == 1);
229 case Type::UByteTyID: // Unsigned integer types...
230 case Type::UShortTyID:
231 case Type::UIntTyID: {
233 if (read_vbr(Buf, EndBuf, Val)) return true;
234 if (!ConstantUInt::isValueValidForType(Ty, Val)) return true;
235 V = ConstantUInt::get(Ty, Val);
239 case Type::ULongTyID: {
241 if (read_vbr(Buf, EndBuf, Val)) return true;
242 V = ConstantUInt::get(Ty, Val);
246 case Type::SByteTyID: // Signed integer types...
247 case Type::ShortTyID:
248 case Type::IntTyID: {
251 if (read_vbr(Buf, EndBuf, Val)) return true;
252 if (!ConstantSInt::isValueValidForType(Ty, Val)) return true;
253 V = ConstantSInt::get(Ty, Val);
257 case Type::FloatTyID: {
259 if (input_data(Buf, EndBuf, &F, &F+1)) return true;
260 V = ConstantFP::get(Ty, F);
264 case Type::DoubleTyID: {
266 if (input_data(Buf, EndBuf, &Val, &Val+1)) return true;
267 V = ConstantFP::get(Ty, Val);
272 assert(0 && "Type constants should be handled separately!!!");
275 case Type::ArrayTyID: {
276 const ArrayType *AT = cast<ArrayType>(Ty);
277 unsigned NumElements = AT->getNumElements();
279 std::vector<Constant*> Elements;
280 while (NumElements--) { // Read all of the elements of the constant.
282 if (read_vbr(Buf, EndBuf, Slot)) return true;
283 Constant *C = getConstantValue(AT->getElementType(), Slot);
285 Elements.push_back(C);
287 V = ConstantArray::get(AT, Elements);
291 case Type::StructTyID: {
292 const StructType *ST = cast<StructType>(Ty);
293 const StructType::ElementTypes &ET = ST->getElementTypes();
295 std::vector<Constant *> Elements;
296 for (unsigned i = 0; i < ET.size(); ++i) {
298 if (read_vbr(Buf, EndBuf, Slot)) return true;
299 Constant *C = getConstantValue(ET[i], Slot);
301 Elements.push_back(C);
304 V = ConstantStruct::get(ST, Elements);
308 case Type::PointerTyID: {
309 const PointerType *PT = cast<PointerType>(Ty);
311 if (HasImplicitZeroInitializer)
314 if (read_vbr(Buf, EndBuf, SubClass)) return true;
317 case 0: // ConstantPointerNull value...
318 V = ConstantPointerNull::get(PT);
321 case 1: { // ConstantPointerRef value...
323 if (read_vbr(Buf, EndBuf, Slot)) return true;
324 BCR_TRACE(4, "CPR: Type: '" << Ty << "' slot: " << Slot << "\n");
326 // Check to see if we have already read this global variable...
327 Value *Val = getValue(PT, Slot, false);
330 if (!(GV = dyn_cast<GlobalValue>(Val))) return true;
331 BCR_TRACE(5, "Value Found in ValueTable!\n");
332 } else if (RevisionNum > 0) {
333 // Revision #0 could have forward references to globals that were wierd.
334 // We got rid of this in subsequent revs.
336 } else { // Nope... find or create a forward ref. for it
337 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(PT, Slot));
339 if (I != GlobalRefs.end()) {
340 BCR_TRACE(5, "Previous forward ref found!\n");
341 GV = cast<GlobalValue>(I->second);
343 BCR_TRACE(5, "Creating new forward ref to a global variable!\n");
345 // Create a placeholder for the global variable reference...
346 GlobalVariable *GVar =
347 new GlobalVariable(PT->getElementType(), false,
348 GlobalValue::InternalLinkage);
350 // Keep track of the fact that we have a forward ref to recycle it
351 GlobalRefs.insert(std::make_pair(std::make_pair(PT, Slot), GVar));
353 // Must temporarily push this value into the module table...
354 TheModule->getGlobalList().push_back(GVar);
359 V = ConstantPointerRef::get(GV);
364 BCR_TRACE(5, "UNKNOWN Pointer Constant Type!\n");
371 std::cerr << __FILE__ << ":" << __LINE__
372 << ": Don't know how to deserialize constant value of type '"
373 << Ty->getName() << "'\n";
380 bool BytecodeParser::ParseGlobalTypes(const unsigned char *&Buf,
381 const unsigned char *EndBuf) {
383 return ParseConstantPool(Buf, EndBuf, T, ModuleTypeValues);
386 bool BytecodeParser::ParseConstantPool(const unsigned char *&Buf,
387 const unsigned char *EndBuf,
389 TypeValuesListTy &TypeTab) {
390 while (Buf < EndBuf) {
391 unsigned NumEntries, Typ;
393 if (read_vbr(Buf, EndBuf, NumEntries) ||
394 read_vbr(Buf, EndBuf, Typ)) return true;
395 const Type *Ty = getType(Typ);
396 if (Ty == 0) return true;
397 BCR_TRACE(3, "Type: '" << Ty << "' NumEntries: " << NumEntries << "\n");
399 if (Typ == Type::TypeTyID) {
400 if (parseTypeConstants(Buf, EndBuf, TypeTab, NumEntries)) return true;
402 for (unsigned i = 0; i < NumEntries; ++i) {
405 if (parseConstantValue(Buf, EndBuf, Ty, C)) return true;
406 assert(C && "parseConstantValue returned NULL!");
407 BCR_TRACE(4, "Read Constant: '" << *C << "'\n");
408 if ((Slot = insertValue(C, Tab)) == -1) return true;
410 // If we are reading a function constant table, make sure that we adjust
411 // the slot number to be the real global constant number.
413 if (&Tab != &ModuleValues && Typ < ModuleValues.size())
414 Slot += ModuleValues[Typ]->size();
415 ResolveReferencesToValue(C, (unsigned)Slot);
420 if (Buf > EndBuf) return true;