1 //===- ConstantReader.cpp - Code to constants and types ====---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements functionality to deserialize constants and types from
13 //===----------------------------------------------------------------------===//
15 #include "ReaderInternals.h"
16 #include "llvm/Module.h"
17 #include "llvm/Constants.h"
21 const Type *BytecodeParser::parseTypeConstant(const unsigned char *&Buf,
22 const unsigned char *EndBuf) {
23 unsigned PrimType = read_vbr_uint(Buf, EndBuf);
26 if ((Val = Type::getPrimitiveType((Type::PrimitiveID)PrimType)))
30 case Type::FunctionTyID: {
31 const Type *RetType = getType(read_vbr_uint(Buf, EndBuf));
33 unsigned NumParams = read_vbr_uint(Buf, EndBuf);
35 std::vector<const Type*> Params;
37 Params.push_back(getType(read_vbr_uint(Buf, EndBuf)));
39 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
40 if (isVarArg) Params.pop_back();
42 return FunctionType::get(RetType, Params, isVarArg);
44 case Type::ArrayTyID: {
45 unsigned ElTyp = read_vbr_uint(Buf, EndBuf);
46 const Type *ElementType = getType(ElTyp);
48 unsigned NumElements = read_vbr_uint(Buf, EndBuf);
50 BCR_TRACE(5, "Array Type Constant #" << ElTyp << " size="
51 << NumElements << "\n");
52 return ArrayType::get(ElementType, NumElements);
54 case Type::StructTyID: {
55 std::vector<const Type*> Elements;
56 unsigned Typ = read_vbr_uint(Buf, EndBuf);
57 while (Typ) { // List is terminated by void/0 typeid
58 Elements.push_back(getType(Typ));
59 Typ = read_vbr_uint(Buf, EndBuf);
62 return StructType::get(Elements);
64 case Type::PointerTyID: {
65 unsigned ElTyp = read_vbr_uint(Buf, EndBuf);
66 BCR_TRACE(5, "Pointer Type Constant #" << ElTyp << "\n");
67 return PointerType::get(getType(ElTyp));
70 case Type::OpaqueTyID: {
71 return OpaqueType::get();
75 std::cerr << __FILE__ << ":" << __LINE__
76 << ": Don't know how to deserialize"
77 << " primitive Type " << PrimType << "\n";
82 // parseTypeConstants - We have to use this weird code to handle recursive
83 // types. We know that recursive types will only reference the current slab of
84 // values in the type plane, but they can forward reference types before they
85 // have been read. For example, Type #0 might be '{ Ty#1 }' and Type #1 might
86 // be 'Ty#0*'. When reading Type #0, type number one doesn't exist. To fix
87 // this ugly problem, we pessimistically insert an opaque type for each type we
88 // are about to read. This means that forward references will resolve to
89 // something and when we reread the type later, we can replace the opaque type
90 // with a new resolved concrete type.
92 namespace llvm { void debug_type_tables(); }
93 void BytecodeParser::parseTypeConstants(const unsigned char *&Buf,
94 const unsigned char *EndBuf,
95 TypeValuesListTy &Tab,
96 unsigned NumEntries) {
97 assert(Tab.size() == 0 && "should not have read type constants in before!");
99 // Insert a bunch of opaque types to be resolved later...
100 Tab.reserve(NumEntries);
101 for (unsigned i = 0; i != NumEntries; ++i)
102 Tab.push_back(OpaqueType::get());
104 // Loop through reading all of the types. Forward types will make use of the
105 // opaque types just inserted.
107 for (unsigned i = 0; i != NumEntries; ++i) {
108 const Type *NewTy = parseTypeConstant(Buf, EndBuf), *OldTy = Tab[i].get();
109 if (NewTy == 0) throw std::string("Couldn't parse type!");
110 BCR_TRACE(4, "#" << i << ": Read Type Constant: '" << NewTy <<
111 "' Replacing: " << OldTy << "\n");
113 // Don't insertValue the new type... instead we want to replace the opaque
114 // type with the new concrete value...
117 // Refine the abstract type to the new type. This causes all uses of the
118 // abstract type to use NewTy. This also will cause the opaque type to be
121 cast<DerivedType>(const_cast<Type*>(OldTy))->refineAbstractTypeTo(NewTy);
123 // This should have replace the old opaque type with the new type in the
124 // value table... or with a preexisting type that was already in the system
125 assert(Tab[i] != OldTy && "refineAbstractType didn't work!");
128 BCR_TRACE(5, "Resulting types:\n");
129 for (unsigned i = 0; i < NumEntries; ++i) {
130 BCR_TRACE(5, (void*)Tab[i].get() << " - " << Tab[i].get() << "\n");
136 Constant *BytecodeParser::parseConstantValue(const unsigned char *&Buf,
137 const unsigned char *EndBuf,
140 // We must check for a ConstantExpr before switching by type because
141 // a ConstantExpr can be of any type, and has no explicit value.
143 // 0 if not expr; numArgs if is expr
144 unsigned isExprNumArgs = read_vbr_uint(Buf, EndBuf);
147 // FIXME: Encoding of constant exprs could be much more compact!
148 std::vector<Constant*> ArgVec;
149 ArgVec.reserve(isExprNumArgs);
150 unsigned Opcode = read_vbr_uint(Buf, EndBuf);
152 // Read the slot number and types of each of the arguments
153 for (unsigned i = 0; i != isExprNumArgs; ++i) {
154 unsigned ArgValSlot = read_vbr_uint(Buf, EndBuf);
155 unsigned ArgTypeSlot = read_vbr_uint(Buf, EndBuf);
156 BCR_TRACE(4, "CE Arg " << i << ": Type: '" << *getType(ArgTypeSlot)
157 << "' slot: " << ArgValSlot << "\n");
159 // Get the arg value from its slot if it exists, otherwise a placeholder
160 ArgVec.push_back(getConstantValue(ArgTypeSlot, ArgValSlot));
163 // Construct a ConstantExpr of the appropriate kind
164 if (isExprNumArgs == 1) { // All one-operand expressions
165 assert(Opcode == Instruction::Cast);
166 return ConstantExpr::getCast(ArgVec[0], getType(TypeID));
167 } else if (Opcode == Instruction::GetElementPtr) { // GetElementPtr
168 std::vector<Constant*> IdxList(ArgVec.begin()+1, ArgVec.end());
169 return ConstantExpr::getGetElementPtr(ArgVec[0], IdxList);
170 } else { // All other 2-operand expressions
171 return ConstantExpr::get(Opcode, ArgVec[0], ArgVec[1]);
175 // Ok, not an ConstantExpr. We now know how to read the given type...
176 const Type *Ty = getType(TypeID);
177 switch (Ty->getPrimitiveID()) {
178 case Type::BoolTyID: {
179 unsigned Val = read_vbr_uint(Buf, EndBuf);
180 if (Val != 0 && Val != 1) throw std::string("Invalid boolean value read.");
181 return ConstantBool::get(Val == 1);
184 case Type::UByteTyID: // Unsigned integer types...
185 case Type::UShortTyID:
186 case Type::UIntTyID: {
187 unsigned Val = read_vbr_uint(Buf, EndBuf);
188 if (!ConstantUInt::isValueValidForType(Ty, Val))
189 throw std::string("Invalid unsigned byte/short/int read.");
190 return ConstantUInt::get(Ty, Val);
193 case Type::ULongTyID: {
194 return ConstantUInt::get(Ty, read_vbr_uint64(Buf, EndBuf));
197 case Type::SByteTyID: // Signed integer types...
198 case Type::ShortTyID:
199 case Type::IntTyID: {
201 int64_t Val = read_vbr_int64(Buf, EndBuf);
202 if (!ConstantSInt::isValueValidForType(Ty, Val))
203 throw std::string("Invalid signed byte/short/int/long read.");
204 return ConstantSInt::get(Ty, Val);
207 case Type::FloatTyID: {
209 input_data(Buf, EndBuf, &F, &F+1);
210 return ConstantFP::get(Ty, F);
213 case Type::DoubleTyID: {
215 input_data(Buf, EndBuf, &Val, &Val+1);
216 return ConstantFP::get(Ty, Val);
220 throw std::string("Type constants shouldn't live in constant table!");
222 case Type::ArrayTyID: {
223 const ArrayType *AT = cast<ArrayType>(Ty);
224 unsigned NumElements = AT->getNumElements();
225 unsigned TypeSlot = getTypeSlot(AT->getElementType());
226 std::vector<Constant*> Elements;
227 Elements.reserve(NumElements);
228 while (NumElements--) // Read all of the elements of the constant.
229 Elements.push_back(getConstantValue(TypeSlot,
230 read_vbr_uint(Buf, EndBuf)));
231 return ConstantArray::get(AT, Elements);
234 case Type::StructTyID: {
235 const StructType *ST = cast<StructType>(Ty);
237 std::vector<Constant *> Elements;
238 Elements.reserve(ST->getNumElements());
239 for (unsigned i = 0; i != ST->getNumElements(); ++i)
240 Elements.push_back(getConstantValue(ST->getElementType(i),
241 read_vbr_uint(Buf, EndBuf)));
243 return ConstantStruct::get(ST, Elements);
246 case Type::PointerTyID: { // ConstantPointerRef value...
247 const PointerType *PT = cast<PointerType>(Ty);
248 unsigned Slot = read_vbr_uint(Buf, EndBuf);
249 BCR_TRACE(4, "CPR: Type: '" << Ty << "' slot: " << Slot << "\n");
251 // Check to see if we have already read this global variable...
252 Value *Val = getValue(TypeID, Slot, false);
255 if (!(GV = dyn_cast<GlobalValue>(Val)))
256 throw std::string("Value of ConstantPointerRef not in ValueTable!");
257 BCR_TRACE(5, "Value Found in ValueTable!\n");
259 throw std::string("Forward references are not allowed here.");
262 return ConstantPointerRef::get(GV);
266 throw std::string("Don't know how to deserialize constant value of type '"+
267 Ty->getDescription());
271 void BytecodeParser::ParseGlobalTypes(const unsigned char *&Buf,
272 const unsigned char *EndBuf) {
274 ParseConstantPool(Buf, EndBuf, T, ModuleTypeValues);
277 void BytecodeParser::parseStringConstants(const unsigned char *&Buf,
278 const unsigned char *EndBuf,
279 unsigned NumEntries, ValueTable &Tab){
280 for (; NumEntries; --NumEntries) {
281 unsigned Typ = read_vbr_uint(Buf, EndBuf);
282 const Type *Ty = getType(Typ);
283 if (!isa<ArrayType>(Ty))
284 throw std::string("String constant data invalid!");
286 const ArrayType *ATy = cast<ArrayType>(Ty);
287 if (ATy->getElementType() != Type::SByteTy &&
288 ATy->getElementType() != Type::UByteTy)
289 throw std::string("String constant data invalid!");
291 // Read character data. The type tells us how long the string is.
292 char Data[ATy->getNumElements()];
293 input_data(Buf, EndBuf, Data, Data+ATy->getNumElements());
295 std::vector<Constant*> Elements(ATy->getNumElements());
296 if (ATy->getElementType() == Type::SByteTy)
297 for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i)
298 Elements[i] = ConstantSInt::get(Type::SByteTy, (signed char)Data[i]);
300 for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i)
301 Elements[i] = ConstantUInt::get(Type::UByteTy, (unsigned char)Data[i]);
303 // Create the constant, inserting it as needed.
304 Constant *C = ConstantArray::get(ATy, Elements);
305 unsigned Slot = insertValue(C, Typ, Tab);
306 ResolveReferencesToConstant(C, Slot);
311 void BytecodeParser::ParseConstantPool(const unsigned char *&Buf,
312 const unsigned char *EndBuf,
314 TypeValuesListTy &TypeTab) {
315 while (Buf < EndBuf) {
316 unsigned NumEntries = read_vbr_uint(Buf, EndBuf);
317 unsigned Typ = read_vbr_uint(Buf, EndBuf);
318 if (Typ == Type::TypeTyID) {
319 BCR_TRACE(3, "Type: 'type' NumEntries: " << NumEntries << "\n");
320 parseTypeConstants(Buf, EndBuf, TypeTab, NumEntries);
321 } else if (Typ == Type::VoidTyID) {
322 assert(&Tab == &ModuleValues && "Cannot read strings in functions!");
323 parseStringConstants(Buf, EndBuf, NumEntries, Tab);
325 BCR_TRACE(3, "Type: '" << *getType(Typ) << "' NumEntries: "
326 << NumEntries << "\n");
328 for (unsigned i = 0; i < NumEntries; ++i) {
329 Constant *C = parseConstantValue(Buf, EndBuf, Typ);
330 assert(C && "parseConstantValue returned NULL!");
331 BCR_TRACE(4, "Read Constant: '" << *C << "'\n");
332 unsigned Slot = insertValue(C, Typ, Tab);
334 // If we are reading a function constant table, make sure that we adjust
335 // the slot number to be the real global constant number.
337 if (&Tab != &ModuleValues && Typ < ModuleValues.size() &&
339 Slot += ModuleValues[Typ]->size();
340 ResolveReferencesToConstant(C, Slot);
345 if (Buf > EndBuf) throw std::string("Read past end of buffer.");