2001-12-03 23:26:30 +01:00
|
|
|
//===-- ConstantVals.cpp - Implement Constant nodes --------------*- C++ -*--=//
|
2001-06-06 22:29:01 +02:00
|
|
|
//
|
2001-12-03 23:26:30 +01:00
|
|
|
// This file implements the Constant* classes...
|
2001-06-06 22:29:01 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define __STDC_LIMIT_MACROS // Get defs for INT64_MAX and friends...
|
2001-12-03 23:26:30 +01:00
|
|
|
#include "llvm/ConstantVals.h"
|
2001-06-06 22:29:01 +02:00
|
|
|
#include "llvm/DerivedTypes.h"
|
|
|
|
#include "llvm/SymbolTable.h"
|
2001-10-13 08:57:33 +02:00
|
|
|
#include "llvm/GlobalValue.h"
|
|
|
|
#include "llvm/Module.h"
|
2002-04-08 00:49:37 +02:00
|
|
|
#include "llvm/SlotCalculator.h"
|
2001-11-27 01:03:19 +01:00
|
|
|
#include "Support/StringExtras.h"
|
2001-06-06 22:29:01 +02:00
|
|
|
#include <algorithm>
|
|
|
|
|
2002-01-20 23:54:45 +01:00
|
|
|
using std::map;
|
|
|
|
using std::pair;
|
|
|
|
using std::make_pair;
|
|
|
|
|
2001-12-03 23:26:30 +01:00
|
|
|
ConstantBool *ConstantBool::True = new ConstantBool(true);
|
|
|
|
ConstantBool *ConstantBool::False = new ConstantBool(false);
|
2001-06-06 22:29:01 +02:00
|
|
|
|
2001-07-20 21:16:02 +02:00
|
|
|
|
2001-06-06 22:29:01 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-12-03 23:26:30 +01:00
|
|
|
// Constant Class
|
2001-06-06 22:29:01 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
// Specialize setName to take care of symbol table majik
|
2002-01-20 23:54:45 +01:00
|
|
|
void Constant::setName(const std::string &Name, SymbolTable *ST) {
|
2001-09-07 18:46:31 +02:00
|
|
|
assert(ST && "Type::setName - Must provide symbol table argument!");
|
|
|
|
|
|
|
|
if (Name.size()) ST->insert(Name, this);
|
2001-06-06 22:29:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Static constructor to create a '0' constant of arbitrary type...
|
2001-12-03 23:26:30 +01:00
|
|
|
Constant *Constant::getNullConstant(const Type *Ty) {
|
2001-06-06 22:29:01 +02:00
|
|
|
switch (Ty->getPrimitiveID()) {
|
2001-12-03 23:26:30 +01:00
|
|
|
case Type::BoolTyID: return ConstantBool::get(false);
|
2001-06-06 22:29:01 +02:00
|
|
|
case Type::SByteTyID:
|
|
|
|
case Type::ShortTyID:
|
|
|
|
case Type::IntTyID:
|
2001-12-03 23:26:30 +01:00
|
|
|
case Type::LongTyID: return ConstantSInt::get(Ty, 0);
|
2001-06-06 22:29:01 +02:00
|
|
|
|
|
|
|
case Type::UByteTyID:
|
|
|
|
case Type::UShortTyID:
|
|
|
|
case Type::UIntTyID:
|
2001-12-03 23:26:30 +01:00
|
|
|
case Type::ULongTyID: return ConstantUInt::get(Ty, 0);
|
2001-06-06 22:29:01 +02:00
|
|
|
|
|
|
|
case Type::FloatTyID:
|
2001-12-03 23:26:30 +01:00
|
|
|
case Type::DoubleTyID: return ConstantFP::get(Ty, 0);
|
2001-09-30 22:14:07 +02:00
|
|
|
|
|
|
|
case Type::PointerTyID:
|
2001-12-03 23:26:30 +01:00
|
|
|
return ConstantPointerNull::get(cast<PointerType>(Ty));
|
2001-06-06 22:29:01 +02:00
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-12-03 23:26:30 +01:00
|
|
|
void Constant::destroyConstantImpl() {
|
|
|
|
// When a Constant is destroyed, there may be lingering
|
2001-10-13 08:57:33 +02:00
|
|
|
// references to the constant by other constants in the constant pool. These
|
|
|
|
// constants are implicitly dependant on the module that is being deleted,
|
|
|
|
// but they don't know that. Because we only find out when the CPV is
|
|
|
|
// deleted, we must now notify all of our users (that should only be
|
2001-12-03 23:26:30 +01:00
|
|
|
// Constants) that they are, in fact, invalid now and should be deleted.
|
2001-10-13 08:57:33 +02:00
|
|
|
//
|
|
|
|
while (!use_empty()) {
|
|
|
|
Value *V = use_back();
|
|
|
|
#ifndef NDEBUG // Only in -g mode...
|
2001-12-03 23:26:30 +01:00
|
|
|
if (!isa<Constant>(V)) {
|
2002-04-08 00:32:25 +02:00
|
|
|
std::cerr << "While deleting: ";
|
|
|
|
dump();
|
|
|
|
std::cerr << "\nUse still stuck around after Def is destroyed: ";
|
|
|
|
V->dump();
|
|
|
|
std::cerr << "\n";
|
2001-10-13 08:57:33 +02:00
|
|
|
}
|
|
|
|
#endif
|
2001-12-03 23:26:30 +01:00
|
|
|
assert(isa<Constant>(V) && "References remain to ConstantPointerRef!");
|
|
|
|
Constant *CPV = cast<Constant>(V);
|
2001-10-13 08:57:33 +02:00
|
|
|
CPV->destroyConstant();
|
|
|
|
|
|
|
|
// The constant should remove itself from our use list...
|
|
|
|
assert((use_empty() || use_back() == V) && "Constant not removed!");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value has no outstanding references it is safe to delete it now...
|
|
|
|
delete this;
|
2001-10-01 22:11:19 +02:00
|
|
|
}
|
2001-06-06 22:29:01 +02:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2001-12-03 23:26:30 +01:00
|
|
|
// ConstantXXX Classes
|
2001-06-06 22:29:01 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Normal Constructors
|
|
|
|
|
2001-12-03 23:26:30 +01:00
|
|
|
ConstantBool::ConstantBool(bool V) : Constant(Type::BoolTy) {
|
2001-06-06 22:29:01 +02:00
|
|
|
Val = V;
|
|
|
|
}
|
|
|
|
|
2001-12-03 23:26:30 +01:00
|
|
|
ConstantInt::ConstantInt(const Type *Ty, uint64_t V) : Constant(Ty) {
|
2001-09-07 18:46:31 +02:00
|
|
|
Val.Unsigned = V;
|
|
|
|
}
|
2001-07-20 21:16:02 +02:00
|
|
|
|
2001-12-03 23:26:30 +01:00
|
|
|
ConstantSInt::ConstantSInt(const Type *Ty, int64_t V) : ConstantInt(Ty, V) {
|
2001-07-20 21:16:02 +02:00
|
|
|
assert(isValueValidForType(Ty, V) && "Value too large for type!");
|
2001-06-06 22:29:01 +02:00
|
|
|
}
|
|
|
|
|
2001-12-03 23:26:30 +01:00
|
|
|
ConstantUInt::ConstantUInt(const Type *Ty, uint64_t V) : ConstantInt(Ty, V) {
|
2001-07-20 21:16:02 +02:00
|
|
|
assert(isValueValidForType(Ty, V) && "Value too large for type!");
|
2001-06-06 22:29:01 +02:00
|
|
|
}
|
|
|
|
|
2001-12-03 23:26:30 +01:00
|
|
|
ConstantFP::ConstantFP(const Type *Ty, double V) : Constant(Ty) {
|
2001-07-20 21:16:02 +02:00
|
|
|
assert(isValueValidForType(Ty, V) && "Value too large for type!");
|
2001-06-06 22:29:01 +02:00
|
|
|
Val = V;
|
|
|
|
}
|
|
|
|
|
2001-12-03 23:26:30 +01:00
|
|
|
ConstantArray::ConstantArray(const ArrayType *T,
|
2002-01-20 23:54:45 +01:00
|
|
|
const std::vector<Constant*> &V) : Constant(T) {
|
2001-06-06 22:29:01 +02:00
|
|
|
for (unsigned i = 0; i < V.size(); i++) {
|
|
|
|
assert(V[i]->getType() == T->getElementType());
|
2001-07-07 10:36:50 +02:00
|
|
|
Operands.push_back(Use(V[i], this));
|
2001-06-06 22:29:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-12-03 23:26:30 +01:00
|
|
|
ConstantStruct::ConstantStruct(const StructType *T,
|
2002-01-20 23:54:45 +01:00
|
|
|
const std::vector<Constant*> &V) : Constant(T) {
|
2001-06-06 22:29:01 +02:00
|
|
|
const StructType::ElementTypes &ETypes = T->getElementTypes();
|
2001-09-07 18:46:31 +02:00
|
|
|
|
2001-06-06 22:29:01 +02:00
|
|
|
for (unsigned i = 0; i < V.size(); i++) {
|
|
|
|
assert(V[i]->getType() == ETypes[i]);
|
2001-07-07 10:36:50 +02:00
|
|
|
Operands.push_back(Use(V[i], this));
|
2001-06-06 22:29:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-12-03 23:26:30 +01:00
|
|
|
ConstantPointerRef::ConstantPointerRef(GlobalValue *GV)
|
|
|
|
: ConstantPointer(GV->getType()) {
|
2001-10-03 08:12:09 +02:00
|
|
|
Operands.push_back(Use(GV, this));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-06 22:29:01 +02:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// getStrValue implementations
|
|
|
|
|
2002-01-20 23:54:45 +01:00
|
|
|
std::string ConstantBool::getStrValue() const {
|
2001-06-28 01:41:11 +02:00
|
|
|
return Val ? "true" : "false";
|
2001-06-06 22:29:01 +02:00
|
|
|
}
|
|
|
|
|
2002-01-20 23:54:45 +01:00
|
|
|
std::string ConstantSInt::getStrValue() const {
|
2001-07-20 21:16:02 +02:00
|
|
|
return itostr(Val.Signed);
|
2001-06-06 22:29:01 +02:00
|
|
|
}
|
|
|
|
|
2002-01-20 23:54:45 +01:00
|
|
|
std::string ConstantUInt::getStrValue() const {
|
2001-07-20 21:16:02 +02:00
|
|
|
return utostr(Val.Unsigned);
|
2001-06-06 22:29:01 +02:00
|
|
|
}
|
|
|
|
|
2002-04-07 10:37:11 +02:00
|
|
|
// ConstantFP::getStrValue - We would like to output the FP constant value in
|
|
|
|
// exponential notation, but we cannot do this if doing so will lose precision.
|
|
|
|
// Check here to make sure that we only output it in exponential format if we
|
|
|
|
// can parse the value back and get the same value.
|
|
|
|
//
|
2002-01-20 23:54:45 +01:00
|
|
|
std::string ConstantFP::getStrValue() const {
|
2002-04-07 10:37:11 +02:00
|
|
|
std::string StrVal = ftostr(Val);
|
2002-04-07 10:42:53 +02:00
|
|
|
|
|
|
|
// Check to make sure that the stringized number is not some string like "Inf"
|
|
|
|
// or NaN, that atof will accept, but the lexer will not. Check that the
|
|
|
|
// string matches the "[-+]?[0-9]" regex.
|
|
|
|
//
|
|
|
|
if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
|
|
|
|
((StrVal[0] == '-' || StrVal[0] == '+') &&
|
|
|
|
(StrVal[0] >= '0' && StrVal[0] <= '9'))) {
|
|
|
|
double TestVal = atof(StrVal.c_str()); // Reparse stringized version!
|
|
|
|
if (TestVal == Val)
|
|
|
|
return StrVal;
|
|
|
|
}
|
2002-04-07 10:37:11 +02:00
|
|
|
|
|
|
|
// Otherwise we could not reparse it to exactly the same value, so we must
|
|
|
|
// output the string in hexadecimal format!
|
|
|
|
//
|
|
|
|
// Behave nicely in the face of C TBAA rules... see:
|
|
|
|
// http://www.nullstone.com/htmls/category/aliastyp.htm
|
|
|
|
//
|
|
|
|
char *Ptr = (char*)&Val;
|
|
|
|
assert(sizeof(double) == sizeof(uint64_t) && sizeof(double) == 8 &&
|
|
|
|
"assuming that double is 64 bits!");
|
|
|
|
return "0x"+utohexstr(*(uint64_t*)Ptr);
|
2001-06-06 22:29:01 +02:00
|
|
|
}
|
|
|
|
|
2002-01-20 23:54:45 +01:00
|
|
|
std::string ConstantArray::getStrValue() const {
|
|
|
|
std::string Result;
|
2001-10-29 14:27:09 +01:00
|
|
|
|
|
|
|
// As a special case, print the array as a string if it is an array of
|
|
|
|
// ubytes or an array of sbytes with positive values.
|
|
|
|
//
|
2001-10-15 01:54:12 +02:00
|
|
|
const Type *ETy = cast<ArrayType>(getType())->getElementType();
|
|
|
|
bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
|
2001-10-28 22:48:05 +01:00
|
|
|
|
2001-10-29 14:27:09 +01:00
|
|
|
if (ETy == Type::SByteTy) {
|
|
|
|
for (unsigned i = 0; i < Operands.size(); ++i)
|
|
|
|
if (ETy == Type::SByteTy &&
|
2001-12-03 23:26:30 +01:00
|
|
|
cast<ConstantSInt>(Operands[i])->getValue() < 0) {
|
2001-10-29 14:27:09 +01:00
|
|
|
isString = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2001-10-15 01:54:12 +02:00
|
|
|
|
2001-10-29 14:27:09 +01:00
|
|
|
if (isString) {
|
|
|
|
Result = "c\"";
|
2001-10-15 02:05:03 +02:00
|
|
|
for (unsigned i = 0; i < Operands.size(); ++i) {
|
2001-10-15 01:54:12 +02:00
|
|
|
unsigned char C = (ETy == Type::SByteTy) ?
|
2001-12-03 23:26:30 +01:00
|
|
|
(unsigned char)cast<ConstantSInt>(Operands[i])->getValue() :
|
|
|
|
(unsigned char)cast<ConstantUInt>(Operands[i])->getValue();
|
2001-10-15 01:54:12 +02:00
|
|
|
|
|
|
|
if (isprint(C)) {
|
|
|
|
Result += C;
|
2001-10-29 14:27:09 +01:00
|
|
|
} else {
|
|
|
|
Result += '\\';
|
|
|
|
Result += ( C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A');
|
|
|
|
Result += ((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A');
|
2001-10-15 01:54:12 +02:00
|
|
|
}
|
2001-10-15 01:17:20 +02:00
|
|
|
}
|
|
|
|
Result += "\"";
|
2001-10-15 01:54:12 +02:00
|
|
|
|
2001-10-15 01:17:20 +02:00
|
|
|
} else {
|
|
|
|
Result = "[";
|
|
|
|
if (Operands.size()) {
|
|
|
|
Result += " " + Operands[0]->getType()->getDescription() +
|
2001-12-03 23:26:30 +01:00
|
|
|
" " + cast<Constant>(Operands[0])->getStrValue();
|
2001-10-15 01:17:20 +02:00
|
|
|
for (unsigned i = 1; i < Operands.size(); i++)
|
|
|
|
Result += ", " + Operands[i]->getType()->getDescription() +
|
2001-12-03 23:26:30 +01:00
|
|
|
" " + cast<Constant>(Operands[i])->getStrValue();
|
2001-10-15 01:17:20 +02:00
|
|
|
}
|
|
|
|
Result += " ]";
|
|
|
|
}
|
|
|
|
|
|
|
|
return Result;
|
2001-06-06 22:29:01 +02:00
|
|
|
}
|
|
|
|
|
2002-01-20 23:54:45 +01:00
|
|
|
std::string ConstantStruct::getStrValue() const {
|
|
|
|
std::string Result = "{";
|
2001-07-07 10:36:50 +02:00
|
|
|
if (Operands.size()) {
|
2001-09-07 18:46:31 +02:00
|
|
|
Result += " " + Operands[0]->getType()->getDescription() +
|
2001-12-03 23:26:30 +01:00
|
|
|
" " + cast<Constant>(Operands[0])->getStrValue();
|
2001-07-07 10:36:50 +02:00
|
|
|
for (unsigned i = 1; i < Operands.size(); i++)
|
2001-09-07 18:46:31 +02:00
|
|
|
Result += ", " + Operands[i]->getType()->getDescription() +
|
2001-12-03 23:26:30 +01:00
|
|
|
" " + cast<Constant>(Operands[i])->getStrValue();
|
2001-06-06 22:29:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return Result + " }";
|
|
|
|
}
|
|
|
|
|
2002-01-20 23:54:45 +01:00
|
|
|
std::string ConstantPointerNull::getStrValue() const {
|
2001-09-30 22:14:07 +02:00
|
|
|
return "null";
|
|
|
|
}
|
|
|
|
|
2002-01-20 23:54:45 +01:00
|
|
|
std::string ConstantPointerRef::getStrValue() const {
|
2001-10-13 08:57:33 +02:00
|
|
|
const GlobalValue *V = getValue();
|
|
|
|
if (V->hasName()) return "%" + V->getName();
|
|
|
|
|
2002-04-08 00:49:37 +02:00
|
|
|
// FIXME: This is a gross hack.
|
2001-10-13 08:57:33 +02:00
|
|
|
SlotCalculator *Table = new SlotCalculator(V->getParent(), true);
|
|
|
|
int Slot = Table->getValSlot(V);
|
|
|
|
delete Table;
|
|
|
|
|
2002-01-20 23:54:45 +01:00
|
|
|
if (Slot >= 0) return std::string(" %") + itostr(Slot);
|
2001-10-13 08:57:33 +02:00
|
|
|
else return "<pointer reference badref>";
|
2001-10-03 08:12:09 +02:00
|
|
|
}
|
|
|
|
|
2001-10-13 08:57:33 +02:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// classof implementations
|
|
|
|
|
2001-12-03 23:26:30 +01:00
|
|
|
bool ConstantInt::classof(const Constant *CPV) {
|
2001-10-13 08:57:33 +02:00
|
|
|
return CPV->getType()->isIntegral();
|
|
|
|
}
|
2001-12-03 23:26:30 +01:00
|
|
|
bool ConstantSInt::classof(const Constant *CPV) {
|
2001-10-13 08:57:33 +02:00
|
|
|
return CPV->getType()->isSigned();
|
|
|
|
}
|
2001-12-03 23:26:30 +01:00
|
|
|
bool ConstantUInt::classof(const Constant *CPV) {
|
2001-10-13 08:57:33 +02:00
|
|
|
return CPV->getType()->isUnsigned();
|
|
|
|
}
|
2001-12-03 23:26:30 +01:00
|
|
|
bool ConstantFP::classof(const Constant *CPV) {
|
2001-10-13 08:57:33 +02:00
|
|
|
const Type *Ty = CPV->getType();
|
|
|
|
return Ty == Type::FloatTy || Ty == Type::DoubleTy;
|
|
|
|
}
|
2001-12-03 23:26:30 +01:00
|
|
|
bool ConstantArray::classof(const Constant *CPV) {
|
2001-10-13 08:57:33 +02:00
|
|
|
return isa<ArrayType>(CPV->getType());
|
|
|
|
}
|
2001-12-03 23:26:30 +01:00
|
|
|
bool ConstantStruct::classof(const Constant *CPV) {
|
2001-10-13 08:57:33 +02:00
|
|
|
return isa<StructType>(CPV->getType());
|
|
|
|
}
|
2001-12-03 23:26:30 +01:00
|
|
|
bool ConstantPointer::classof(const Constant *CPV) {
|
2001-10-13 08:57:33 +02:00
|
|
|
return isa<PointerType>(CPV->getType());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-06 22:29:01 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// isValueValidForType implementations
|
|
|
|
|
2001-12-03 23:26:30 +01:00
|
|
|
bool ConstantSInt::isValueValidForType(const Type *Ty, int64_t Val) {
|
2001-06-06 22:29:01 +02:00
|
|
|
switch (Ty->getPrimitiveID()) {
|
|
|
|
default:
|
|
|
|
return false; // These can't be represented as integers!!!
|
|
|
|
|
|
|
|
// Signed types...
|
|
|
|
case Type::SByteTyID:
|
|
|
|
return (Val <= INT8_MAX && Val >= INT8_MIN);
|
|
|
|
case Type::ShortTyID:
|
|
|
|
return (Val <= INT16_MAX && Val >= INT16_MIN);
|
|
|
|
case Type::IntTyID:
|
|
|
|
return (Val <= INT32_MAX && Val >= INT32_MIN);
|
|
|
|
case Type::LongTyID:
|
|
|
|
return true; // This is the largest type...
|
|
|
|
}
|
|
|
|
assert(0 && "WTF?");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2001-12-03 23:26:30 +01:00
|
|
|
bool ConstantUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
|
2001-06-06 22:29:01 +02:00
|
|
|
switch (Ty->getPrimitiveID()) {
|
|
|
|
default:
|
|
|
|
return false; // These can't be represented as integers!!!
|
|
|
|
|
|
|
|
// Unsigned types...
|
|
|
|
case Type::UByteTyID:
|
|
|
|
return (Val <= UINT8_MAX);
|
|
|
|
case Type::UShortTyID:
|
|
|
|
return (Val <= UINT16_MAX);
|
|
|
|
case Type::UIntTyID:
|
|
|
|
return (Val <= UINT32_MAX);
|
|
|
|
case Type::ULongTyID:
|
|
|
|
return true; // This is the largest type...
|
|
|
|
}
|
|
|
|
assert(0 && "WTF?");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2001-12-03 23:26:30 +01:00
|
|
|
bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
|
2001-06-06 22:29:01 +02:00
|
|
|
switch (Ty->getPrimitiveID()) {
|
|
|
|
default:
|
|
|
|
return false; // These can't be represented as floating point!
|
|
|
|
|
|
|
|
// TODO: Figure out how to test if a double can be cast to a float!
|
|
|
|
case Type::FloatTyID:
|
2001-07-15 02:18:39 +02:00
|
|
|
/*
|
2001-06-06 22:29:01 +02:00
|
|
|
return (Val <= UINT8_MAX);
|
|
|
|
*/
|
|
|
|
case Type::DoubleTyID:
|
|
|
|
return true; // This is the largest type...
|
|
|
|
}
|
|
|
|
};
|
2001-07-20 21:16:02 +02:00
|
|
|
|
2001-09-07 18:46:31 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Hash Function Implementations
|
|
|
|
#if 0
|
2001-12-03 23:26:30 +01:00
|
|
|
unsigned ConstantSInt::hash(const Type *Ty, int64_t V) {
|
2001-09-07 18:46:31 +02:00
|
|
|
return unsigned(Ty->getPrimitiveID() ^ V);
|
|
|
|
}
|
|
|
|
|
2001-12-03 23:26:30 +01:00
|
|
|
unsigned ConstantUInt::hash(const Type *Ty, uint64_t V) {
|
2001-09-07 18:46:31 +02:00
|
|
|
return unsigned(Ty->getPrimitiveID() ^ V);
|
|
|
|
}
|
|
|
|
|
2001-12-03 23:26:30 +01:00
|
|
|
unsigned ConstantFP::hash(const Type *Ty, double V) {
|
2001-09-07 18:46:31 +02:00
|
|
|
return Ty->getPrimitiveID() ^ unsigned(V);
|
|
|
|
}
|
|
|
|
|
2001-12-03 23:26:30 +01:00
|
|
|
unsigned ConstantArray::hash(const ArrayType *Ty,
|
2002-01-20 23:54:45 +01:00
|
|
|
const std::vector<Constant*> &V) {
|
2001-09-07 18:46:31 +02:00
|
|
|
unsigned Result = (Ty->getUniqueID() << 5) ^ (Ty->getUniqueID() * 7);
|
|
|
|
for (unsigned i = 0; i < V.size(); ++i)
|
|
|
|
Result ^= V[i]->getHash() << (i & 7);
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2001-12-03 23:26:30 +01:00
|
|
|
unsigned ConstantStruct::hash(const StructType *Ty,
|
2002-01-20 23:54:45 +01:00
|
|
|
const std::vector<Constant*> &V) {
|
2001-09-07 18:46:31 +02:00
|
|
|
unsigned Result = (Ty->getUniqueID() << 5) ^ (Ty->getUniqueID() * 7);
|
|
|
|
for (unsigned i = 0; i < V.size(); ++i)
|
|
|
|
Result ^= V[i]->getHash() << (i & 7);
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
#endif
|
2001-07-20 21:16:02 +02:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2001-09-07 18:46:31 +02:00
|
|
|
// Factory Function Implementation
|
|
|
|
|
2001-12-03 23:26:30 +01:00
|
|
|
template<class ValType, class ConstantClass>
|
2001-09-07 18:46:31 +02:00
|
|
|
struct ValueMap {
|
|
|
|
typedef pair<const Type*, ValType> ConstHashKey;
|
2001-12-03 23:26:30 +01:00
|
|
|
map<ConstHashKey, ConstantClass *> Map;
|
2001-09-07 18:46:31 +02:00
|
|
|
|
2001-12-03 23:26:30 +01:00
|
|
|
inline ConstantClass *get(const Type *Ty, ValType V) {
|
|
|
|
map<ConstHashKey,ConstantClass *>::iterator I =
|
2001-09-07 18:46:31 +02:00
|
|
|
Map.find(ConstHashKey(Ty, V));
|
|
|
|
return (I != Map.end()) ? I->second : 0;
|
|
|
|
}
|
|
|
|
|
2001-12-03 23:26:30 +01:00
|
|
|
inline void add(const Type *Ty, ValType V, ConstantClass *CP) {
|
2001-09-07 18:46:31 +02:00
|
|
|
Map.insert(make_pair(ConstHashKey(Ty, V), CP));
|
|
|
|
}
|
2001-10-13 08:57:33 +02:00
|
|
|
|
2001-12-03 23:26:30 +01:00
|
|
|
inline void remove(ConstantClass *CP) {
|
|
|
|
for (map<ConstHashKey,ConstantClass *>::iterator I = Map.begin(),
|
2001-10-13 08:57:33 +02:00
|
|
|
E = Map.end(); I != E;++I)
|
|
|
|
if (I->second == CP) {
|
|
|
|
Map.erase(I);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2001-09-07 18:46:31 +02:00
|
|
|
};
|
|
|
|
|
2001-12-03 23:26:30 +01:00
|
|
|
//---- ConstantUInt::get() and ConstantSInt::get() implementations...
|
2001-09-07 18:46:31 +02:00
|
|
|
//
|
2001-12-03 23:26:30 +01:00
|
|
|
static ValueMap<uint64_t, ConstantInt> IntConstants;
|
2001-09-07 18:46:31 +02:00
|
|
|
|
2001-12-03 23:26:30 +01:00
|
|
|
ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) {
|
|
|
|
ConstantSInt *Result = (ConstantSInt*)IntConstants.get(Ty, (uint64_t)V);
|
2001-09-07 18:46:31 +02:00
|
|
|
if (!Result) // If no preexisting value, create one now...
|
2001-12-03 23:26:30 +01:00
|
|
|
IntConstants.add(Ty, V, Result = new ConstantSInt(Ty, V));
|
2001-09-07 18:46:31 +02:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2001-12-03 23:26:30 +01:00
|
|
|
ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) {
|
|
|
|
ConstantUInt *Result = (ConstantUInt*)IntConstants.get(Ty, V);
|
2001-09-07 18:46:31 +02:00
|
|
|
if (!Result) // If no preexisting value, create one now...
|
2001-12-03 23:26:30 +01:00
|
|
|
IntConstants.add(Ty, V, Result = new ConstantUInt(Ty, V));
|
2001-09-07 18:46:31 +02:00
|
|
|
return Result;
|
|
|
|
}
|
2001-07-20 21:16:02 +02:00
|
|
|
|
2001-12-03 23:26:30 +01:00
|
|
|
ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) {
|
2001-09-07 18:46:31 +02:00
|
|
|
assert(V <= 127 && "Can only be used with very small positive constants!");
|
2001-12-03 23:26:30 +01:00
|
|
|
if (Ty->isSigned()) return ConstantSInt::get(Ty, V);
|
|
|
|
return ConstantUInt::get(Ty, V);
|
2001-09-07 18:46:31 +02:00
|
|
|
}
|
|
|
|
|
2001-12-03 23:26:30 +01:00
|
|
|
//---- ConstantFP::get() implementation...
|
2001-09-07 18:46:31 +02:00
|
|
|
//
|
2001-12-03 23:26:30 +01:00
|
|
|
static ValueMap<double, ConstantFP> FPConstants;
|
2001-09-07 18:46:31 +02:00
|
|
|
|
2001-12-03 23:26:30 +01:00
|
|
|
ConstantFP *ConstantFP::get(const Type *Ty, double V) {
|
|
|
|
ConstantFP *Result = FPConstants.get(Ty, V);
|
2001-09-07 18:46:31 +02:00
|
|
|
if (!Result) // If no preexisting value, create one now...
|
2001-12-03 23:26:30 +01:00
|
|
|
FPConstants.add(Ty, V, Result = new ConstantFP(Ty, V));
|
2001-09-07 18:46:31 +02:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2001-12-03 23:26:30 +01:00
|
|
|
//---- ConstantArray::get() implementation...
|
2001-09-07 18:46:31 +02:00
|
|
|
//
|
2002-01-20 23:54:45 +01:00
|
|
|
static ValueMap<std::vector<Constant*>, ConstantArray> ArrayConstants;
|
2001-09-07 18:46:31 +02:00
|
|
|
|
2001-12-03 23:26:30 +01:00
|
|
|
ConstantArray *ConstantArray::get(const ArrayType *Ty,
|
2002-01-20 23:54:45 +01:00
|
|
|
const std::vector<Constant*> &V) {
|
2001-12-03 23:26:30 +01:00
|
|
|
ConstantArray *Result = ArrayConstants.get(Ty, V);
|
2001-09-07 18:46:31 +02:00
|
|
|
if (!Result) // If no preexisting value, create one now...
|
2001-12-03 23:26:30 +01:00
|
|
|
ArrayConstants.add(Ty, V, Result = new ConstantArray(Ty, V));
|
2001-09-07 18:46:31 +02:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2001-12-03 23:26:30 +01:00
|
|
|
// ConstantArray::get(const string&) - Return an array that is initialized to
|
2001-10-15 01:54:12 +02:00
|
|
|
// contain the specified string. A null terminator is added to the specified
|
|
|
|
// string so that it may be used in a natural way...
|
|
|
|
//
|
2002-01-20 23:54:45 +01:00
|
|
|
ConstantArray *ConstantArray::get(const std::string &Str) {
|
|
|
|
std::vector<Constant*> ElementVals;
|
2001-10-15 01:54:12 +02:00
|
|
|
|
|
|
|
for (unsigned i = 0; i < Str.length(); ++i)
|
2001-12-14 17:41:18 +01:00
|
|
|
ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i]));
|
2001-10-15 01:54:12 +02:00
|
|
|
|
|
|
|
// Add a null terminator to the string...
|
2001-12-14 17:41:18 +01:00
|
|
|
ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
|
2001-10-15 01:54:12 +02:00
|
|
|
|
2001-12-14 17:41:18 +01:00
|
|
|
ArrayType *ATy = ArrayType::get(Type::SByteTy, Str.length()+1);
|
2001-12-03 23:26:30 +01:00
|
|
|
return ConstantArray::get(ATy, ElementVals);
|
2001-10-15 01:17:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-10-13 08:57:33 +02:00
|
|
|
// destroyConstant - Remove the constant from the constant table...
|
|
|
|
//
|
2001-12-03 23:26:30 +01:00
|
|
|
void ConstantArray::destroyConstant() {
|
2001-10-13 08:57:33 +02:00
|
|
|
ArrayConstants.remove(this);
|
|
|
|
destroyConstantImpl();
|
|
|
|
}
|
|
|
|
|
2001-12-03 23:26:30 +01:00
|
|
|
//---- ConstantStruct::get() implementation...
|
2001-09-07 18:46:31 +02:00
|
|
|
//
|
2002-01-20 23:54:45 +01:00
|
|
|
static ValueMap<std::vector<Constant*>, ConstantStruct> StructConstants;
|
2001-09-07 18:46:31 +02:00
|
|
|
|
2001-12-03 23:26:30 +01:00
|
|
|
ConstantStruct *ConstantStruct::get(const StructType *Ty,
|
2002-01-20 23:54:45 +01:00
|
|
|
const std::vector<Constant*> &V) {
|
2001-12-03 23:26:30 +01:00
|
|
|
ConstantStruct *Result = StructConstants.get(Ty, V);
|
2001-09-07 18:46:31 +02:00
|
|
|
if (!Result) // If no preexisting value, create one now...
|
2001-12-03 23:26:30 +01:00
|
|
|
StructConstants.add(Ty, V, Result = new ConstantStruct(Ty, V));
|
2001-09-07 18:46:31 +02:00
|
|
|
return Result;
|
|
|
|
}
|
2001-10-03 17:39:36 +02:00
|
|
|
|
2001-10-13 08:57:33 +02:00
|
|
|
// destroyConstant - Remove the constant from the constant table...
|
|
|
|
//
|
2001-12-03 23:26:30 +01:00
|
|
|
void ConstantStruct::destroyConstant() {
|
2001-10-13 08:57:33 +02:00
|
|
|
StructConstants.remove(this);
|
|
|
|
destroyConstantImpl();
|
|
|
|
}
|
|
|
|
|
2001-12-03 23:26:30 +01:00
|
|
|
//---- ConstantPointerNull::get() implementation...
|
2001-10-03 17:39:36 +02:00
|
|
|
//
|
2001-12-03 23:26:30 +01:00
|
|
|
static ValueMap<char, ConstantPointerNull> NullPtrConstants;
|
2001-10-03 17:39:36 +02:00
|
|
|
|
2001-12-03 23:26:30 +01:00
|
|
|
ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
|
|
|
|
ConstantPointerNull *Result = NullPtrConstants.get(Ty, 0);
|
2001-10-03 17:39:36 +02:00
|
|
|
if (!Result) // If no preexisting value, create one now...
|
2001-12-03 23:26:30 +01:00
|
|
|
NullPtrConstants.add(Ty, 0, Result = new ConstantPointerNull(Ty));
|
2001-10-03 17:39:36 +02:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2001-12-03 23:26:30 +01:00
|
|
|
//---- ConstantPointerRef::get() implementation...
|
2001-10-03 21:28:15 +02:00
|
|
|
//
|
2001-12-03 23:26:30 +01:00
|
|
|
ConstantPointerRef *ConstantPointerRef::get(GlobalValue *GV) {
|
2001-10-13 08:57:33 +02:00
|
|
|
assert(GV->getParent() && "Global Value must be attached to a module!");
|
|
|
|
|
|
|
|
// The Module handles the pointer reference sharing...
|
2001-12-03 23:26:30 +01:00
|
|
|
return GV->getParent()->getConstantPointerRef(GV);
|
2001-10-13 08:57:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-12-03 23:26:30 +01:00
|
|
|
void ConstantPointerRef::mutateReference(GlobalValue *NewGV) {
|
|
|
|
getValue()->getParent()->mutateConstantPointerRef(getValue(), NewGV);
|
2001-10-13 08:57:33 +02:00
|
|
|
Operands[0] = NewGV;
|
2001-10-03 21:28:15 +02:00
|
|
|
}
|