Transform two methods to return pointers directly instead of returning them

as 'by reference' arguments.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@8849 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner 2003-10-04 20:00:03 +00:00
parent 51ca860bda
commit 9e460f23bb
3 changed files with 42 additions and 66 deletions

View File

@ -147,9 +147,9 @@ void BytecodeParser::parseTypeConstants(const unsigned char *&Buf,
} }
void BytecodeParser::parseConstantValue(const unsigned char *&Buf, Constant *BytecodeParser::parseConstantValue(const unsigned char *&Buf,
const unsigned char *EndBuf, const unsigned char *EndBuf,
const Type *Ty, Constant *&V) { const Type *Ty) {
// We must check for a ConstantExpr before switching by type because // We must check for a ConstantExpr before switching by type because
// a ConstantExpr can be of any type, and has no explicit value. // a ConstantExpr can be of any type, and has no explicit value.
@ -183,14 +183,13 @@ void BytecodeParser::parseConstantValue(const unsigned char *&Buf,
// Construct a ConstantExpr of the appropriate kind // Construct a ConstantExpr of the appropriate kind
if (isExprNumArgs == 1) { // All one-operand expressions if (isExprNumArgs == 1) { // All one-operand expressions
assert(Opcode == Instruction::Cast); assert(Opcode == Instruction::Cast);
V = ConstantExpr::getCast(ArgVec[0], Ty); return ConstantExpr::getCast(ArgVec[0], Ty);
} else if (Opcode == Instruction::GetElementPtr) { // GetElementPtr } else if (Opcode == Instruction::GetElementPtr) { // GetElementPtr
std::vector<Constant*> IdxList(ArgVec.begin()+1, ArgVec.end()); std::vector<Constant*> IdxList(ArgVec.begin()+1, ArgVec.end());
V = ConstantExpr::getGetElementPtr(ArgVec[0], IdxList); return ConstantExpr::getGetElementPtr(ArgVec[0], IdxList);
} else { // All other 2-operand expressions } else { // All other 2-operand expressions
V = ConstantExpr::get(Opcode, ArgVec[0], ArgVec[1]); return ConstantExpr::get(Opcode, ArgVec[0], ArgVec[1]);
} }
return;
} }
// Ok, not an ConstantExpr. We now know how to read the given type... // Ok, not an ConstantExpr. We now know how to read the given type...
@ -199,8 +198,7 @@ void BytecodeParser::parseConstantValue(const unsigned char *&Buf,
unsigned Val; unsigned Val;
if (read_vbr(Buf, EndBuf, Val)) throw Error_readvbr; if (read_vbr(Buf, EndBuf, Val)) throw Error_readvbr;
if (Val != 0 && Val != 1) throw std::string("Invalid boolean value read."); if (Val != 0 && Val != 1) throw std::string("Invalid boolean value read.");
V = ConstantBool::get(Val == 1); return ConstantBool::get(Val == 1);
break;
} }
case Type::UByteTyID: // Unsigned integer types... case Type::UByteTyID: // Unsigned integer types...
@ -210,15 +208,13 @@ void BytecodeParser::parseConstantValue(const unsigned char *&Buf,
if (read_vbr(Buf, EndBuf, Val)) throw Error_readvbr; if (read_vbr(Buf, EndBuf, Val)) throw Error_readvbr;
if (!ConstantUInt::isValueValidForType(Ty, Val)) if (!ConstantUInt::isValueValidForType(Ty, Val))
throw std::string("Invalid unsigned byte/short/int read."); throw std::string("Invalid unsigned byte/short/int read.");
V = ConstantUInt::get(Ty, Val); return ConstantUInt::get(Ty, Val);
break;
} }
case Type::ULongTyID: { case Type::ULongTyID: {
uint64_t Val; uint64_t Val;
if (read_vbr(Buf, EndBuf, Val)) throw Error_readvbr; if (read_vbr(Buf, EndBuf, Val)) throw Error_readvbr;
V = ConstantUInt::get(Ty, Val); return ConstantUInt::get(Ty, Val);
break;
} }
case Type::SByteTyID: // Signed integer types... case Type::SByteTyID: // Signed integer types...
@ -229,27 +225,23 @@ void BytecodeParser::parseConstantValue(const unsigned char *&Buf,
if (read_vbr(Buf, EndBuf, Val)) throw Error_readvbr; if (read_vbr(Buf, EndBuf, Val)) throw Error_readvbr;
if (!ConstantSInt::isValueValidForType(Ty, Val)) if (!ConstantSInt::isValueValidForType(Ty, Val))
throw std::string("Invalid signed byte/short/int/long read."); throw std::string("Invalid signed byte/short/int/long read.");
V = ConstantSInt::get(Ty, Val); return ConstantSInt::get(Ty, Val);
break;
} }
case Type::FloatTyID: { case Type::FloatTyID: {
float F; float F;
if (input_data(Buf, EndBuf, &F, &F+1)) throw Error_inputdata; if (input_data(Buf, EndBuf, &F, &F+1)) throw Error_inputdata;
V = ConstantFP::get(Ty, F); return ConstantFP::get(Ty, F);
break;
} }
case Type::DoubleTyID: { case Type::DoubleTyID: {
double Val; double Val;
if (input_data(Buf, EndBuf, &Val, &Val+1)) throw Error_inputdata; if (input_data(Buf, EndBuf, &Val, &Val+1)) throw Error_inputdata;
V = ConstantFP::get(Ty, Val); return ConstantFP::get(Ty, Val);
break;
} }
case Type::TypeTyID: case Type::TypeTyID:
assert(0 && "Type constants should be handled separately!!!"); throw std::string("Type constants shouldn't live in constant table!");
abort();
case Type::ArrayTyID: { case Type::ArrayTyID: {
const ArrayType *AT = cast<ArrayType>(Ty); const ArrayType *AT = cast<ArrayType>(Ty);
@ -263,8 +255,7 @@ void BytecodeParser::parseConstantValue(const unsigned char *&Buf,
if (!C) throw std::string("Unable to get const value of array slot."); if (!C) throw std::string("Unable to get const value of array slot.");
Elements.push_back(C); Elements.push_back(C);
} }
V = ConstantArray::get(AT, Elements); return ConstantArray::get(AT, Elements);
break;
} }
case Type::StructTyID: { case Type::StructTyID: {
@ -280,8 +271,7 @@ void BytecodeParser::parseConstantValue(const unsigned char *&Buf,
Elements.push_back(C); Elements.push_back(C);
} }
V = ConstantStruct::get(ST, Elements); return ConstantStruct::get(ST, Elements);
break;
} }
case Type::PointerTyID: { case Type::PointerTyID: {
@ -294,8 +284,7 @@ void BytecodeParser::parseConstantValue(const unsigned char *&Buf,
switch (SubClass) { switch (SubClass) {
case 0: // ConstantPointerNull value... case 0: // ConstantPointerNull value...
V = ConstantPointerNull::get(PT); return ConstantPointerNull::get(PT);
break;
case 1: { // ConstantPointerRef value... case 1: { // ConstantPointerRef value...
unsigned Slot; unsigned Slot;
@ -336,23 +325,18 @@ void BytecodeParser::parseConstantValue(const unsigned char *&Buf,
} }
} }
V = ConstantPointerRef::get(GV); return ConstantPointerRef::get(GV);
break;
} }
default: default:
BCR_TRACE(5, "UNKNOWN Pointer Constant Type!\n"); BCR_TRACE(5, "UNKNOWN Pointer Constant Type!\n");
throw std::string("Unknown pointer constant type."); throw std::string("Unknown pointer constant type.");
} }
break;
} }
default: default:
std::cerr << __FILE__ << ":" << __LINE__
<< ": Don't know how to deserialize constant value of type '"
<< Ty->getName() << "'\n";
throw std::string("Don't know how to deserialize constant value of type '"+ throw std::string("Don't know how to deserialize constant value of type '"+
Ty->getName()); Ty->getDescription());
} }
} }
@ -379,11 +363,10 @@ void BytecodeParser::ParseConstantPool(const unsigned char *&Buf,
parseTypeConstants(Buf, EndBuf, TypeTab, NumEntries); parseTypeConstants(Buf, EndBuf, TypeTab, NumEntries);
} else { } else {
for (unsigned i = 0; i < NumEntries; ++i) { for (unsigned i = 0; i < NumEntries; ++i) {
Constant *C; Constant *C = parseConstantValue(Buf, EndBuf, Ty);
int Slot;
parseConstantValue(Buf, EndBuf, Ty, C);
assert(C && "parseConstantValue returned NULL!"); assert(C && "parseConstantValue returned NULL!");
BCR_TRACE(4, "Read Constant: '" << *C << "'\n"); BCR_TRACE(4, "Read Constant: '" << *C << "'\n");
int Slot;
if ((Slot = insertValue(C, Tab)) == -1) if ((Slot = insertValue(C, Tab)) == -1)
throw std::string("Could not insert value into ValueTable."); throw std::string("Could not insert value into ValueTable.");

View File

@ -31,25 +31,21 @@ static inline void ALIGN32(const unsigned char *&begin,
throw std::string("Alignment error in buffer: read past end of block."); throw std::string("Alignment error in buffer: read past end of block.");
} }
void unsigned BytecodeParser::getTypeSlot(const Type *Ty) {
BytecodeParser::getTypeSlot(const Type *Ty, unsigned &Slot) { if (Ty->isPrimitiveType())
if (Ty->isPrimitiveType()) { return Ty->getPrimitiveID();
Slot = Ty->getPrimitiveID();
} else { // Check the function level types first...
// Check the function level types first... TypeValuesListTy::iterator I = find(FunctionTypeValues.begin(),
TypeValuesListTy::iterator I = find(FunctionTypeValues.begin(), FunctionTypeValues.end(), Ty);
FunctionTypeValues.end(), Ty); if (I != FunctionTypeValues.end())
if (I != FunctionTypeValues.end()) { return FirstDerivedTyID + ModuleTypeValues.size() +
Slot = FirstDerivedTyID + ModuleTypeValues.size() + (&*I - &FunctionTypeValues[0]);
(&*I - &FunctionTypeValues[0]);
} else { I = find(ModuleTypeValues.begin(), ModuleTypeValues.end(), Ty);
I = find(ModuleTypeValues.begin(), ModuleTypeValues.end(), Ty); if (I == ModuleTypeValues.end())
if (I == ModuleTypeValues.end()) throw std::string("Didn't find type in ModuleTypeValues.");
throw std::string("Didn't find type in ModuleTypeValues."); return FirstDerivedTyID + (&*I - &ModuleTypeValues[0]);
Slot = FirstDerivedTyID + (&*I - &ModuleTypeValues[0]);
}
}
//cerr << "getTypeSlot '" << Ty->getName() << "' = " << Slot << "\n";
} }
const Type *BytecodeParser::getType(unsigned ID) { const Type *BytecodeParser::getType(unsigned ID) {
@ -68,8 +64,7 @@ int BytecodeParser::insertValue(Value *Val, ValueTable &ValueTab) {
Val->getType()->isPrimitiveType() || Val->getType()->isPrimitiveType() ||
!cast<Constant>(Val)->isNullValue()) && !cast<Constant>(Val)->isNullValue()) &&
"Cannot read null values from bytecode!"); "Cannot read null values from bytecode!");
unsigned type; unsigned type = getTypeSlot(Val->getType());
getTypeSlot(Val->getType(), type);
assert(type != Type::TypeTyID && "Types should never be insertValue'd!"); assert(type != Type::TypeTyID && "Types should never be insertValue'd!");
if (ValueTab.size() <= type) { if (ValueTab.size() <= type) {
@ -93,19 +88,16 @@ int BytecodeParser::insertValue(Value *Val, ValueTable &ValueTab) {
void BytecodeParser::setValueTo(ValueTable &ValueTab, unsigned Slot, void BytecodeParser::setValueTo(ValueTable &ValueTab, unsigned Slot,
Value *Val) { Value *Val) {
assert(&ValueTab == &ModuleValues && "Can only setValueTo on Module values!"); assert(&ValueTab == &ModuleValues && "Can only setValueTo on Module values!");
unsigned type;
getTypeSlot(Val->getType(), type);
assert((!HasImplicitZeroInitializer || Slot != 0) && assert((!HasImplicitZeroInitializer || Slot != 0) &&
"Cannot change zero init"); "Cannot change zero init");
unsigned type = getTypeSlot(Val->getType());
assert(type < ValueTab.size() && Slot <= ValueTab[type]->size()); assert(type < ValueTab.size() && Slot <= ValueTab[type]->size());
ValueTab[type]->setOperand(Slot-HasImplicitZeroInitializer, Val); ValueTab[type]->setOperand(Slot-HasImplicitZeroInitializer, Val);
} }
Value *BytecodeParser::getValue(const Type *Ty, unsigned oNum, bool Create) { Value *BytecodeParser::getValue(const Type *Ty, unsigned oNum, bool Create) {
unsigned Num = oNum; unsigned Num = oNum;
unsigned type; // The type plane it lives in... unsigned type = getTypeSlot(Ty); // The type plane it lives in...
getTypeSlot(Ty, type);
if (type == Type::TypeTyID) { // The 'type' plane has implicit values if (type == Type::TypeTyID) { // The 'type' plane has implicit values
assert(Create == false); assert(Create == false);

View File

@ -169,8 +169,9 @@ private:
void ParseConstantPool(const unsigned char *&Buf, const unsigned char *EndBuf, void ParseConstantPool(const unsigned char *&Buf, const unsigned char *EndBuf,
ValueTable &Tab, TypeValuesListTy &TypeTab); ValueTable &Tab, TypeValuesListTy &TypeTab);
void parseConstantValue(const unsigned char *&Buf, const unsigned char *End, Constant *parseConstantValue(const unsigned char *&Buf,
const Type *Ty, Constant *&V); const unsigned char *End,
const Type *Ty);
void parseTypeConstants(const unsigned char *&Buf, void parseTypeConstants(const unsigned char *&Buf,
const unsigned char *EndBuf, const unsigned char *EndBuf,
TypeValuesListTy &Tab, unsigned NumEntries); TypeValuesListTy &Tab, unsigned NumEntries);
@ -185,7 +186,7 @@ private:
void setValueTo(ValueTable &D, unsigned Slot, Value *V); void setValueTo(ValueTable &D, unsigned Slot, Value *V);
void postResolveValues(ValueTable &ValTab); void postResolveValues(ValueTable &ValTab);
void getTypeSlot(const Type *Ty, unsigned &Slot); unsigned getTypeSlot(const Type *Ty);
// resolve all references to the placeholder (if any) for the given value // resolve all references to the placeholder (if any) for the given value
void ResolveReferencesToValue(Value *Val, unsigned Slot); void ResolveReferencesToValue(Value *Val, unsigned Slot);