//
//===----------------------------------------------------------------------===//
-#define __STDC_LIMIT_MACROS // Get defs for INT64_MAX and friends...
#include "llvm/Constants.h"
#include "llvm/DerivedTypes.h"
#include "llvm/iMemory.h"
case Type::UIntTyID:
case Type::ULongTyID: return getAllOnesValue(Ty);
- default:
- assert(0 && "Non-integral type specified!");
- return 0;
+ default: return 0;
}
}
case Type::UIntTyID:
case Type::ULongTyID: return ConstantUInt::get(Ty, 0);
- default:
- assert(0 && "Non-integral type specified!");
- return 0;
+ default: return 0;
}
}
Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
return ConstantUInt::get(Ty, Val);
}
- default:
- assert(0 && "Non-integral type specified!");
- return 0;
+ default: return 0;
}
}
}
ConstantSInt::ConstantSInt(const Type *Ty, int64_t V) : ConstantInt(Ty, V) {
+ assert(Ty->isInteger() && Ty->isSigned() &&
+ "Illegal type for unsigned integer constant!");
assert(isValueValidForType(Ty, V) && "Value too large for type!");
}
ConstantUInt::ConstantUInt(const Type *Ty, uint64_t V) : ConstantInt(Ty, V) {
+ assert(Ty->isInteger() && Ty->isUnsigned() &&
+ "Illegal type for unsigned integer constant!");
assert(isValueValidForType(Ty, V) && "Value too large for type!");
}
// classof implementations
bool ConstantIntegral::classof(const Constant *CPV) {
- return (CPV->getType()->isIntegral() || CPV->getType() == Type::BoolTy) &&
- !isa<ConstantExpr>(CPV);
+ return CPV->getType()->isIntegral() && !isa<ConstantExpr>(CPV);
}
bool ConstantInt::classof(const Constant *CPV) {
- return CPV->getType()->isIntegral() && !isa<ConstantExpr>(CPV);
+ return CPV->getType()->isInteger() && !isa<ConstantExpr>(CPV);
}
bool ConstantSInt::classof(const Constant *CPV) {
return CPV->getType()->isSigned() && !isa<ConstantExpr>(CPV);
destroyConstantImpl();
}
+// getAsString - If the sub-element type of this array is either sbyte or ubyte,
+// then this method converts the array to an std::string and returns it.
+// Otherwise, it asserts out.
+//
+std::string ConstantArray::getAsString() const {
+ std::string Result;
+ if (getType()->getElementType() == Type::SByteTy)
+ for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
+ Result += (char)cast<ConstantSInt>(getOperand(i))->getValue();
+ else {
+ assert(getType()->getElementType() == Type::UByteTy && "Not a string!");
+ for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
+ Result += (char)cast<ConstantUInt>(getOperand(i))->getValue();
+ }
+ return Result;
+}
+
+
//---- ConstantStruct::get() implementation...
//
static ValueMap<std::vector<Constant*>, ConstantStruct> StructConstants;
destroyConstantImpl();
}
+
//---- ConstantPointerNull::get() implementation...
//
static ValueMap<char, ConstantPointerNull> NullPtrConstants;
return Result;
}
+// destroyConstant - Remove the constant from the constant table...
+//
+void ConstantPointerNull::destroyConstant() {
+ NullPtrConstants.remove(this);
+ destroyConstantImpl();
+}
+
+
//---- ConstantPointerRef::get() implementation...
//
ConstantPointerRef *ConstantPointerRef::get(GlobalValue *GV) {
return GV->getParent()->getConstantPointerRef(GV);
}
+// destroyConstant - Remove the constant from the constant table...
+//
+void ConstantPointerRef::destroyConstant() {
+ getValue()->getParent()->destroyConstantPointerRef(this);
+ destroyConstantImpl();
+}
+
+
//---- ConstantExpr::get() implementations...
//
typedef pair<unsigned, vector<Constant*> > ExprMapKeyType;
static ValueMap<const ExprMapKeyType, ConstantExpr> ExprConstants;
-ConstantExpr *ConstantExpr::get(unsigned Opcode, Constant *C, const Type *Ty) {
+ConstantExpr *ConstantExpr::getCast(Constant *C, const Type *Ty) {
// Look up the constant in the table first to ensure uniqueness
vector<Constant*> argVec(1, C);
- const ExprMapKeyType &Key = make_pair(Opcode, argVec);
+ const ExprMapKeyType &Key = make_pair(Instruction::Cast, argVec);
ConstantExpr *Result = ExprConstants.get(Ty, Key);
if (Result) return Result;
// Its not in the table so create a new one and put it in the table.
- // Check the operands for consistency first
- assert(Opcode == Instruction::Cast ||
- (Opcode >= Instruction::FirstUnaryOp &&
- Opcode < Instruction::NumUnaryOps) &&
- "Invalid opcode in unary ConstantExpr!");
-
- // type of operand will not match result for Cast operation
- assert((Opcode == Instruction::Cast || Ty == C->getType()) &&
- "Type of operand in unary constant expression should match result");
-
- Result = new ConstantExpr(Opcode, C, Ty);
+ Result = new ConstantExpr(Instruction::Cast, C, Ty);
ExprConstants.add(Ty, Key, Result);
return Result;
}