2001-06-06 20:29:01 +00:00
|
|
|
//===- ReadConst.cpp - Code to constants and constant pools -----------------===
|
|
|
|
//
|
|
|
|
// This file implements functionality to deserialize constants and entire
|
|
|
|
// constant pools.
|
|
|
|
//
|
|
|
|
// Note that this library should be as fast as possible, reentrant, and
|
|
|
|
// threadsafe!!
|
|
|
|
//
|
|
|
|
//===------------------------------------------------------------------------===
|
|
|
|
|
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/BasicBlock.h"
|
|
|
|
#include "llvm/ConstPoolVals.h"
|
|
|
|
#include "llvm/DerivedTypes.h"
|
|
|
|
#include "ReaderInternals.h"
|
|
|
|
|
|
|
|
bool BytecodeParser::parseTypeConstant(const uchar *&Buf, const uchar *EndBuf,
|
|
|
|
ConstPoolVal *&V) {
|
|
|
|
const Type *Val = 0;
|
|
|
|
|
|
|
|
unsigned PrimType;
|
2001-07-28 17:50:18 +00:00
|
|
|
if (read_vbr(Buf, EndBuf, PrimType)) return failure(true);
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
if ((Val = Type::getPrimitiveType((Type::PrimitiveID)PrimType))) {
|
|
|
|
V = new ConstPoolType(Val); // It's just a primitive ID.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (PrimType) {
|
|
|
|
case Type::MethodTyID: {
|
|
|
|
unsigned Typ;
|
2001-07-28 17:50:18 +00:00
|
|
|
if (read_vbr(Buf, EndBuf, Typ)) return failure(true);
|
2001-06-06 20:29:01 +00:00
|
|
|
const Type *RetType = getType(Typ);
|
2001-07-28 17:50:18 +00:00
|
|
|
if (RetType == 0) return failure(true);
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2001-07-25 22:47:55 +00:00
|
|
|
unsigned NumParams;
|
2001-07-28 17:50:18 +00:00
|
|
|
if (read_vbr(Buf, EndBuf, NumParams)) return failure(true);
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2001-07-25 22:47:55 +00:00
|
|
|
MethodType::ParamTypes Params;
|
|
|
|
while (NumParams--) {
|
2001-07-28 17:50:18 +00:00
|
|
|
if (read_vbr(Buf, EndBuf, Typ)) return failure(true);
|
2001-06-06 20:29:01 +00:00
|
|
|
const Type *Ty = getType(Typ);
|
2001-07-28 17:50:18 +00:00
|
|
|
if (Ty == 0) return failure(true);
|
2001-06-06 20:29:01 +00:00
|
|
|
Params.push_back(Ty);
|
|
|
|
}
|
|
|
|
|
|
|
|
Val = MethodType::getMethodType(RetType, Params);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Type::ArrayTyID: {
|
|
|
|
unsigned ElTyp;
|
2001-07-28 17:50:18 +00:00
|
|
|
if (read_vbr(Buf, EndBuf, ElTyp)) return failure(true);
|
2001-06-06 20:29:01 +00:00
|
|
|
const Type *ElementType = getType(ElTyp);
|
2001-07-28 17:50:18 +00:00
|
|
|
if (ElementType == 0) return failure(true);
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
int NumElements;
|
2001-07-28 17:50:18 +00:00
|
|
|
if (read_vbr(Buf, EndBuf, NumElements)) return failure(true);
|
2001-06-06 20:29:01 +00:00
|
|
|
Val = ArrayType::getArrayType(ElementType, NumElements);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Type::StructTyID: {
|
|
|
|
unsigned Typ;
|
|
|
|
StructType::ElementTypes Elements;
|
|
|
|
|
2001-07-28 17:50:18 +00:00
|
|
|
if (read_vbr(Buf, EndBuf, Typ)) return failure(true);
|
2001-06-06 20:29:01 +00:00
|
|
|
while (Typ) { // List is terminated by void/0 typeid
|
|
|
|
const Type *Ty = getType(Typ);
|
2001-07-28 17:50:18 +00:00
|
|
|
if (Ty == 0) return failure(true);
|
2001-06-06 20:29:01 +00:00
|
|
|
Elements.push_back(Ty);
|
|
|
|
|
2001-07-28 17:50:18 +00:00
|
|
|
if (read_vbr(Buf, EndBuf, Typ)) return failure(true);
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Val = StructType::getStructType(Elements);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Type::PointerTyID: {
|
|
|
|
unsigned ElTyp;
|
2001-07-28 17:50:18 +00:00
|
|
|
if (read_vbr(Buf, EndBuf, ElTyp)) return failure(true);
|
2001-06-06 20:29:01 +00:00
|
|
|
const Type *ElementType = getType(ElTyp);
|
2001-07-28 17:50:18 +00:00
|
|
|
if (ElementType == 0) return failure(true);
|
2001-06-06 20:29:01 +00:00
|
|
|
Val = PointerType::getPointerType(ElementType);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to deserialize"
|
|
|
|
<< " primitive Type " << PrimType << "\n";
|
2001-07-28 17:50:18 +00:00
|
|
|
return failure(true);
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
V = new ConstPoolType(Val);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BytecodeParser::parseConstPoolValue(const uchar *&Buf,
|
|
|
|
const uchar *EndBuf,
|
|
|
|
const Type *Ty, ConstPoolVal *&V) {
|
|
|
|
switch (Ty->getPrimitiveID()) {
|
|
|
|
case Type::BoolTyID: {
|
|
|
|
unsigned Val;
|
2001-07-28 17:50:18 +00:00
|
|
|
if (read_vbr(Buf, EndBuf, Val)) return failure(true);
|
|
|
|
if (Val != 0 && Val != 1) return failure(true);
|
2001-06-06 20:29:01 +00:00
|
|
|
V = new ConstPoolBool(Val == 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Type::UByteTyID: // Unsigned integer types...
|
|
|
|
case Type::UShortTyID:
|
|
|
|
case Type::UIntTyID: {
|
|
|
|
unsigned Val;
|
2001-07-28 17:50:18 +00:00
|
|
|
if (read_vbr(Buf, EndBuf, Val)) return failure(true);
|
|
|
|
if (!ConstPoolUInt::isValueValidForType(Ty, Val)) return failure(true);
|
2001-06-06 20:29:01 +00:00
|
|
|
V = new ConstPoolUInt(Ty, Val);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Type::ULongTyID: {
|
|
|
|
uint64_t Val;
|
2001-07-28 17:50:18 +00:00
|
|
|
if (read_vbr(Buf, EndBuf, Val)) return failure(true);
|
2001-06-06 20:29:01 +00:00
|
|
|
V = new ConstPoolUInt(Ty, Val);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Type::SByteTyID: // Unsigned integer types...
|
|
|
|
case Type::ShortTyID:
|
|
|
|
case Type::IntTyID: {
|
|
|
|
int Val;
|
2001-07-28 17:50:18 +00:00
|
|
|
if (read_vbr(Buf, EndBuf, Val)) return failure(true);
|
|
|
|
if (!ConstPoolSInt::isValueValidForType(Ty, Val)) return failure(true);
|
2001-06-06 20:29:01 +00:00
|
|
|
V = new ConstPoolSInt(Ty, Val);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Type::LongTyID: {
|
|
|
|
int64_t Val;
|
2001-07-28 17:50:18 +00:00
|
|
|
if (read_vbr(Buf, EndBuf, Val)) return failure(true);
|
2001-06-06 20:29:01 +00:00
|
|
|
V = new ConstPoolSInt(Ty, Val);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2001-07-15 00:17:18 +00:00
|
|
|
case Type::FloatTyID: {
|
|
|
|
float F;
|
2001-07-28 17:50:18 +00:00
|
|
|
if (input_data(Buf, EndBuf, &F, &F+1)) return failure(true);
|
2001-07-15 00:17:18 +00:00
|
|
|
V = new ConstPoolFP(Ty, F);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Type::DoubleTyID: {
|
|
|
|
double Val;
|
2001-07-28 17:50:18 +00:00
|
|
|
if (input_data(Buf, EndBuf, &Val, &Val+1)) return failure(true);
|
2001-07-15 00:17:18 +00:00
|
|
|
V = new ConstPoolFP(Ty, Val);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2001-06-06 20:29:01 +00:00
|
|
|
case Type::TypeTyID:
|
2001-07-28 17:50:18 +00:00
|
|
|
if (parseTypeConstant(Buf, EndBuf, V)) return failure(true);
|
2001-06-06 20:29:01 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Type::ArrayTyID: {
|
|
|
|
const ArrayType *AT = (const ArrayType*)Ty;
|
|
|
|
unsigned NumElements;
|
|
|
|
if (AT->isSized()) // Sized array, # elements stored in type!
|
|
|
|
NumElements = (unsigned)AT->getNumElements();
|
|
|
|
else // Unsized array, # elements stored in stream!
|
2001-07-28 17:50:18 +00:00
|
|
|
if (read_vbr(Buf, EndBuf, NumElements)) return failure(true);
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
vector<ConstPoolVal *> Elements;
|
|
|
|
while (NumElements--) { // Read all of the elements of the constant.
|
|
|
|
unsigned Slot;
|
2001-07-28 17:50:18 +00:00
|
|
|
if (read_vbr(Buf, EndBuf, Slot)) return failure(true);
|
2001-06-06 20:29:01 +00:00
|
|
|
Value *V = getValue(AT->getElementType(), Slot, false);
|
2001-07-28 17:50:18 +00:00
|
|
|
if (!V || !V->isConstant()) return failure(true);
|
2001-06-06 20:29:01 +00:00
|
|
|
Elements.push_back((ConstPoolVal*)V);
|
|
|
|
}
|
|
|
|
V = new ConstPoolArray(AT, Elements);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Type::StructTyID: {
|
|
|
|
const StructType *ST = (const StructType*)Ty;
|
|
|
|
const StructType::ElementTypes &ET = ST->getElementTypes();
|
|
|
|
|
|
|
|
vector<ConstPoolVal *> Elements;
|
|
|
|
for (unsigned i = 0; i < ET.size(); ++i) {
|
|
|
|
unsigned Slot;
|
2001-07-28 17:50:18 +00:00
|
|
|
if (read_vbr(Buf, EndBuf, Slot)) return failure(true);
|
2001-06-06 20:29:01 +00:00
|
|
|
Value *V = getValue(ET[i], Slot, false);
|
2001-06-27 23:41:11 +00:00
|
|
|
if (!V || !V->isConstant())
|
2001-07-28 17:50:18 +00:00
|
|
|
return failure(true);
|
2001-06-06 20:29:01 +00:00
|
|
|
Elements.push_back((ConstPoolVal*)V);
|
|
|
|
}
|
|
|
|
|
|
|
|
V = new ConstPoolStruct(ST, Elements);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
cerr << __FILE__ << ":" << __LINE__
|
|
|
|
<< ": Don't know how to deserialize constant value of type '"
|
|
|
|
<< Ty->getName() << "'\n";
|
2001-07-28 17:50:18 +00:00
|
|
|
return failure(true);
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BytecodeParser::ParseConstantPool(const uchar *&Buf, const uchar *EndBuf,
|
|
|
|
SymTabValue::ConstantPoolType &CP,
|
|
|
|
ValueTable &Tab) {
|
|
|
|
while (Buf < EndBuf) {
|
|
|
|
unsigned NumEntries, Typ;
|
|
|
|
|
|
|
|
if (read_vbr(Buf, EndBuf, NumEntries) ||
|
2001-07-28 17:50:18 +00:00
|
|
|
read_vbr(Buf, EndBuf, Typ)) return failure(true);
|
2001-06-06 20:29:01 +00:00
|
|
|
const Type *Ty = getType(Typ);
|
2001-07-28 17:50:18 +00:00
|
|
|
if (Ty == 0) return failure(true);
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
for (unsigned i = 0; i < NumEntries; i++) {
|
|
|
|
ConstPoolVal *I;
|
2001-07-28 17:50:18 +00:00
|
|
|
if (parseConstPoolValue(Buf, EndBuf, Ty, I)) return failure(true);
|
2001-06-06 20:29:01 +00:00
|
|
|
#if 0
|
|
|
|
cerr << " Read const value: <" << I->getType()->getName()
|
|
|
|
<< ">: " << I->getStrValue() << endl;
|
|
|
|
#endif
|
|
|
|
insertValue(I, Tab);
|
|
|
|
CP.insert(I);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-07-28 17:50:18 +00:00
|
|
|
if (Buf > EndBuf) return failure(true);
|
|
|
|
return false;
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|