2002-04-29 03:55:58 +08:00
|
|
|
//===-- Constants.cpp - Implement Constant nodes -----------------*- C++ -*--=//
|
2001-06-07 04:29:01 +08:00
|
|
|
//
|
2001-12-04 06:26:30 +08:00
|
|
|
// This file implements the Constant* classes...
|
2001-06-07 04:29:01 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define __STDC_LIMIT_MACROS // Get defs for INT64_MAX and friends...
|
2002-04-29 03:55:58 +08:00
|
|
|
#include "llvm/Constants.h"
|
2001-06-07 04:29:01 +08:00
|
|
|
#include "llvm/DerivedTypes.h"
|
2002-07-15 07:13:17 +08:00
|
|
|
#include "llvm/iMemory.h"
|
2001-06-07 04:29:01 +08:00
|
|
|
#include "llvm/SymbolTable.h"
|
2001-10-13 14:57:33 +08:00
|
|
|
#include "llvm/Module.h"
|
2002-04-08 06:49:37 +08:00
|
|
|
#include "llvm/SlotCalculator.h"
|
2001-11-27 08:03:19 +08:00
|
|
|
#include "Support/StringExtras.h"
|
2001-06-07 04:29:01 +08:00
|
|
|
#include <algorithm>
|
|
|
|
|
2002-01-21 06:54:45 +08:00
|
|
|
using std::map;
|
|
|
|
using std::pair;
|
|
|
|
using std::make_pair;
|
|
|
|
|
2001-12-04 06:26:30 +08:00
|
|
|
ConstantBool *ConstantBool::True = new ConstantBool(true);
|
|
|
|
ConstantBool *ConstantBool::False = new ConstantBool(false);
|
2001-06-07 04:29:01 +08:00
|
|
|
|
2001-07-21 03:16:02 +08:00
|
|
|
|
2001-06-07 04:29:01 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-12-04 06:26:30 +08:00
|
|
|
// Constant Class
|
2001-06-07 04:29:01 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
// Specialize setName to take care of symbol table majik
|
2002-01-21 06:54:45 +08:00
|
|
|
void Constant::setName(const std::string &Name, SymbolTable *ST) {
|
2001-09-08 00:46:31 +08:00
|
|
|
assert(ST && "Type::setName - Must provide symbol table argument!");
|
|
|
|
|
|
|
|
if (Name.size()) ST->insert(Name, this);
|
2001-06-07 04:29:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Static constructor to create a '0' constant of arbitrary type...
|
2002-04-27 10:25:43 +08:00
|
|
|
Constant *Constant::getNullValue(const Type *Ty) {
|
2001-06-07 04:29:01 +08:00
|
|
|
switch (Ty->getPrimitiveID()) {
|
2001-12-04 06:26:30 +08:00
|
|
|
case Type::BoolTyID: return ConstantBool::get(false);
|
2001-06-07 04:29:01 +08:00
|
|
|
case Type::SByteTyID:
|
|
|
|
case Type::ShortTyID:
|
|
|
|
case Type::IntTyID:
|
2001-12-04 06:26:30 +08:00
|
|
|
case Type::LongTyID: return ConstantSInt::get(Ty, 0);
|
2001-06-07 04:29:01 +08:00
|
|
|
|
|
|
|
case Type::UByteTyID:
|
|
|
|
case Type::UShortTyID:
|
|
|
|
case Type::UIntTyID:
|
2001-12-04 06:26:30 +08:00
|
|
|
case Type::ULongTyID: return ConstantUInt::get(Ty, 0);
|
2001-06-07 04:29:01 +08:00
|
|
|
|
|
|
|
case Type::FloatTyID:
|
2001-12-04 06:26:30 +08:00
|
|
|
case Type::DoubleTyID: return ConstantFP::get(Ty, 0);
|
2001-10-01 04:14:07 +08:00
|
|
|
|
|
|
|
case Type::PointerTyID:
|
2001-12-04 06:26:30 +08:00
|
|
|
return ConstantPointerNull::get(cast<PointerType>(Ty));
|
2001-06-07 04:29:01 +08:00
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-12-04 06:26:30 +08:00
|
|
|
void Constant::destroyConstantImpl() {
|
|
|
|
// When a Constant is destroyed, there may be lingering
|
2001-10-13 14:57:33 +08: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-04 06:26:30 +08:00
|
|
|
// Constants) that they are, in fact, invalid now and should be deleted.
|
2001-10-13 14:57:33 +08:00
|
|
|
//
|
|
|
|
while (!use_empty()) {
|
|
|
|
Value *V = use_back();
|
|
|
|
#ifndef NDEBUG // Only in -g mode...
|
2001-12-04 06:26:30 +08:00
|
|
|
if (!isa<Constant>(V)) {
|
2002-04-08 06:32:25 +08:00
|
|
|
std::cerr << "While deleting: ";
|
|
|
|
dump();
|
|
|
|
std::cerr << "\nUse still stuck around after Def is destroyed: ";
|
|
|
|
V->dump();
|
|
|
|
std::cerr << "\n";
|
2001-10-13 14:57:33 +08:00
|
|
|
}
|
|
|
|
#endif
|
2002-07-15 07:13:17 +08:00
|
|
|
assert(isa<Constant>(V) && "References remain to Constant being destroyed");
|
2001-12-04 06:26:30 +08:00
|
|
|
Constant *CPV = cast<Constant>(V);
|
2001-10-13 14:57:33 +08:00
|
|
|
CPV->destroyConstant();
|
|
|
|
|
|
|
|
// The constant should remove itself from our use list...
|
2002-07-15 07:13:17 +08:00
|
|
|
assert((use_empty() || use_back() != V) && "Constant not removed!");
|
2001-10-13 14:57:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Value has no outstanding references it is safe to delete it now...
|
|
|
|
delete this;
|
2001-10-02 04:11:19 +08:00
|
|
|
}
|
2001-06-07 04:29:01 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2001-12-04 06:26:30 +08:00
|
|
|
// ConstantXXX Classes
|
2001-06-07 04:29:01 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Normal Constructors
|
|
|
|
|
2001-12-04 06:26:30 +08:00
|
|
|
ConstantBool::ConstantBool(bool V) : Constant(Type::BoolTy) {
|
2001-06-07 04:29:01 +08:00
|
|
|
Val = V;
|
|
|
|
}
|
|
|
|
|
2001-12-04 06:26:30 +08:00
|
|
|
ConstantInt::ConstantInt(const Type *Ty, uint64_t V) : Constant(Ty) {
|
2001-09-08 00:46:31 +08:00
|
|
|
Val.Unsigned = V;
|
|
|
|
}
|
2001-07-21 03:16:02 +08:00
|
|
|
|
2001-12-04 06:26:30 +08:00
|
|
|
ConstantSInt::ConstantSInt(const Type *Ty, int64_t V) : ConstantInt(Ty, V) {
|
2001-07-21 03:16:02 +08:00
|
|
|
assert(isValueValidForType(Ty, V) && "Value too large for type!");
|
2001-06-07 04:29:01 +08:00
|
|
|
}
|
|
|
|
|
2001-12-04 06:26:30 +08:00
|
|
|
ConstantUInt::ConstantUInt(const Type *Ty, uint64_t V) : ConstantInt(Ty, V) {
|
2001-07-21 03:16:02 +08:00
|
|
|
assert(isValueValidForType(Ty, V) && "Value too large for type!");
|
2001-06-07 04:29:01 +08:00
|
|
|
}
|
|
|
|
|
2001-12-04 06:26:30 +08:00
|
|
|
ConstantFP::ConstantFP(const Type *Ty, double V) : Constant(Ty) {
|
2001-07-21 03:16:02 +08:00
|
|
|
assert(isValueValidForType(Ty, V) && "Value too large for type!");
|
2001-06-07 04:29:01 +08:00
|
|
|
Val = V;
|
|
|
|
}
|
|
|
|
|
2001-12-04 06:26:30 +08:00
|
|
|
ConstantArray::ConstantArray(const ArrayType *T,
|
2002-01-21 06:54:45 +08:00
|
|
|
const std::vector<Constant*> &V) : Constant(T) {
|
2001-06-07 04:29:01 +08:00
|
|
|
for (unsigned i = 0; i < V.size(); i++) {
|
|
|
|
assert(V[i]->getType() == T->getElementType());
|
2001-07-07 16:36:50 +08:00
|
|
|
Operands.push_back(Use(V[i], this));
|
2001-06-07 04:29:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-12-04 06:26:30 +08:00
|
|
|
ConstantStruct::ConstantStruct(const StructType *T,
|
2002-01-21 06:54:45 +08:00
|
|
|
const std::vector<Constant*> &V) : Constant(T) {
|
2001-06-07 04:29:01 +08:00
|
|
|
const StructType::ElementTypes &ETypes = T->getElementTypes();
|
2002-07-15 07:13:17 +08:00
|
|
|
assert(V.size() == ETypes.size() &&
|
|
|
|
"Invalid initializer vector for constant structure");
|
2001-06-07 04:29:01 +08:00
|
|
|
for (unsigned i = 0; i < V.size(); i++) {
|
|
|
|
assert(V[i]->getType() == ETypes[i]);
|
2001-07-07 16:36:50 +08:00
|
|
|
Operands.push_back(Use(V[i], this));
|
2001-06-07 04:29:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-12-04 06:26:30 +08:00
|
|
|
ConstantPointerRef::ConstantPointerRef(GlobalValue *GV)
|
|
|
|
: ConstantPointer(GV->getType()) {
|
2001-10-03 14:12:09 +08:00
|
|
|
Operands.push_back(Use(GV, this));
|
|
|
|
}
|
|
|
|
|
2002-07-15 07:13:17 +08:00
|
|
|
ConstantExpr::ConstantExpr(unsigned opCode, Constant *C, const Type *Ty)
|
|
|
|
: Constant(Ty), iType(opCode) {
|
|
|
|
Operands.push_back(Use(C, this));
|
|
|
|
}
|
|
|
|
|
|
|
|
ConstantExpr::ConstantExpr(unsigned opCode, Constant* C1,
|
|
|
|
Constant* C2, const Type *Ty)
|
|
|
|
: Constant(Ty), iType(opCode) {
|
|
|
|
Operands.push_back(Use(C1, this));
|
|
|
|
Operands.push_back(Use(C2, this));
|
|
|
|
}
|
|
|
|
|
|
|
|
ConstantExpr::ConstantExpr(unsigned opCode, Constant* C,
|
|
|
|
const std::vector<Value*>& IdxList, const Type *Ty)
|
|
|
|
: Constant(Ty), iType(opCode) {
|
|
|
|
Operands.reserve(1+IdxList.size());
|
|
|
|
Operands.push_back(Use(C, this));
|
|
|
|
for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
|
|
|
|
Operands.push_back(Use(IdxList[i], this));
|
|
|
|
}
|
|
|
|
|
2001-10-03 14:12:09 +08:00
|
|
|
|
2001-06-07 04:29:01 +08:00
|
|
|
|
2001-10-13 14:57:33 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// classof implementations
|
|
|
|
|
2001-12-04 06:26:30 +08:00
|
|
|
bool ConstantInt::classof(const Constant *CPV) {
|
2002-07-15 07:13:17 +08:00
|
|
|
return CPV->getType()->isIntegral() && ! isa<ConstantExpr>(CPV);
|
2001-10-13 14:57:33 +08:00
|
|
|
}
|
2001-12-04 06:26:30 +08:00
|
|
|
bool ConstantSInt::classof(const Constant *CPV) {
|
2002-07-15 07:13:17 +08:00
|
|
|
return CPV->getType()->isSigned() && ! isa<ConstantExpr>(CPV);
|
2001-10-13 14:57:33 +08:00
|
|
|
}
|
2001-12-04 06:26:30 +08:00
|
|
|
bool ConstantUInt::classof(const Constant *CPV) {
|
2002-07-15 07:13:17 +08:00
|
|
|
return CPV->getType()->isUnsigned() && ! isa<ConstantExpr>(CPV);
|
2001-10-13 14:57:33 +08:00
|
|
|
}
|
2001-12-04 06:26:30 +08:00
|
|
|
bool ConstantFP::classof(const Constant *CPV) {
|
2001-10-13 14:57:33 +08:00
|
|
|
const Type *Ty = CPV->getType();
|
2002-07-15 07:13:17 +08:00
|
|
|
return ((Ty == Type::FloatTy || Ty == Type::DoubleTy) &&
|
|
|
|
! isa<ConstantExpr>(CPV));
|
2001-10-13 14:57:33 +08:00
|
|
|
}
|
2001-12-04 06:26:30 +08:00
|
|
|
bool ConstantArray::classof(const Constant *CPV) {
|
2002-07-15 07:13:17 +08:00
|
|
|
return isa<ArrayType>(CPV->getType()) && ! isa<ConstantExpr>(CPV);
|
2001-10-13 14:57:33 +08:00
|
|
|
}
|
2001-12-04 06:26:30 +08:00
|
|
|
bool ConstantStruct::classof(const Constant *CPV) {
|
2002-07-15 07:13:17 +08:00
|
|
|
return isa<StructType>(CPV->getType()) && ! isa<ConstantExpr>(CPV);
|
2001-10-13 14:57:33 +08:00
|
|
|
}
|
2001-12-04 06:26:30 +08:00
|
|
|
bool ConstantPointer::classof(const Constant *CPV) {
|
2002-07-15 07:13:17 +08:00
|
|
|
return (isa<PointerType>(CPV->getType()) && ! isa<ConstantExpr>(CPV));
|
2001-10-13 14:57:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-15 07:13:17 +08:00
|
|
|
|
2001-06-07 04:29:01 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// isValueValidForType implementations
|
|
|
|
|
2001-12-04 06:26:30 +08:00
|
|
|
bool ConstantSInt::isValueValidForType(const Type *Ty, int64_t Val) {
|
2001-06-07 04:29:01 +08: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-04 06:26:30 +08:00
|
|
|
bool ConstantUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
|
2001-06-07 04:29:01 +08: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-04 06:26:30 +08:00
|
|
|
bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
|
2001-06-07 04:29:01 +08: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 08:18:39 +08:00
|
|
|
/*
|
2001-06-07 04:29:01 +08:00
|
|
|
return (Val <= UINT8_MAX);
|
|
|
|
*/
|
|
|
|
case Type::DoubleTyID:
|
|
|
|
return true; // This is the largest type...
|
|
|
|
}
|
|
|
|
};
|
2001-07-21 03:16:02 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2001-09-08 00:46:31 +08:00
|
|
|
// Factory Function Implementation
|
|
|
|
|
2001-12-04 06:26:30 +08:00
|
|
|
template<class ValType, class ConstantClass>
|
2001-09-08 00:46:31 +08:00
|
|
|
struct ValueMap {
|
|
|
|
typedef pair<const Type*, ValType> ConstHashKey;
|
2001-12-04 06:26:30 +08:00
|
|
|
map<ConstHashKey, ConstantClass *> Map;
|
2001-09-08 00:46:31 +08:00
|
|
|
|
2001-12-04 06:26:30 +08:00
|
|
|
inline ConstantClass *get(const Type *Ty, ValType V) {
|
|
|
|
map<ConstHashKey,ConstantClass *>::iterator I =
|
2001-09-08 00:46:31 +08:00
|
|
|
Map.find(ConstHashKey(Ty, V));
|
|
|
|
return (I != Map.end()) ? I->second : 0;
|
|
|
|
}
|
|
|
|
|
2001-12-04 06:26:30 +08:00
|
|
|
inline void add(const Type *Ty, ValType V, ConstantClass *CP) {
|
2001-09-08 00:46:31 +08:00
|
|
|
Map.insert(make_pair(ConstHashKey(Ty, V), CP));
|
|
|
|
}
|
2001-10-13 14:57:33 +08:00
|
|
|
|
2001-12-04 06:26:30 +08:00
|
|
|
inline void remove(ConstantClass *CP) {
|
|
|
|
for (map<ConstHashKey,ConstantClass *>::iterator I = Map.begin(),
|
2001-10-13 14:57:33 +08:00
|
|
|
E = Map.end(); I != E;++I)
|
|
|
|
if (I->second == CP) {
|
|
|
|
Map.erase(I);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2001-09-08 00:46:31 +08:00
|
|
|
};
|
|
|
|
|
2001-12-04 06:26:30 +08:00
|
|
|
//---- ConstantUInt::get() and ConstantSInt::get() implementations...
|
2001-09-08 00:46:31 +08:00
|
|
|
//
|
2001-12-04 06:26:30 +08:00
|
|
|
static ValueMap<uint64_t, ConstantInt> IntConstants;
|
2001-09-08 00:46:31 +08:00
|
|
|
|
2001-12-04 06:26:30 +08:00
|
|
|
ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) {
|
|
|
|
ConstantSInt *Result = (ConstantSInt*)IntConstants.get(Ty, (uint64_t)V);
|
2001-09-08 00:46:31 +08:00
|
|
|
if (!Result) // If no preexisting value, create one now...
|
2001-12-04 06:26:30 +08:00
|
|
|
IntConstants.add(Ty, V, Result = new ConstantSInt(Ty, V));
|
2001-09-08 00:46:31 +08:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2001-12-04 06:26:30 +08:00
|
|
|
ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) {
|
|
|
|
ConstantUInt *Result = (ConstantUInt*)IntConstants.get(Ty, V);
|
2001-09-08 00:46:31 +08:00
|
|
|
if (!Result) // If no preexisting value, create one now...
|
2001-12-04 06:26:30 +08:00
|
|
|
IntConstants.add(Ty, V, Result = new ConstantUInt(Ty, V));
|
2001-09-08 00:46:31 +08:00
|
|
|
return Result;
|
|
|
|
}
|
2001-07-21 03:16:02 +08:00
|
|
|
|
2001-12-04 06:26:30 +08:00
|
|
|
ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) {
|
2001-09-08 00:46:31 +08:00
|
|
|
assert(V <= 127 && "Can only be used with very small positive constants!");
|
2001-12-04 06:26:30 +08:00
|
|
|
if (Ty->isSigned()) return ConstantSInt::get(Ty, V);
|
|
|
|
return ConstantUInt::get(Ty, V);
|
2001-09-08 00:46:31 +08:00
|
|
|
}
|
|
|
|
|
2001-12-04 06:26:30 +08:00
|
|
|
//---- ConstantFP::get() implementation...
|
2001-09-08 00:46:31 +08:00
|
|
|
//
|
2001-12-04 06:26:30 +08:00
|
|
|
static ValueMap<double, ConstantFP> FPConstants;
|
2001-09-08 00:46:31 +08:00
|
|
|
|
2001-12-04 06:26:30 +08:00
|
|
|
ConstantFP *ConstantFP::get(const Type *Ty, double V) {
|
|
|
|
ConstantFP *Result = FPConstants.get(Ty, V);
|
2001-09-08 00:46:31 +08:00
|
|
|
if (!Result) // If no preexisting value, create one now...
|
2001-12-04 06:26:30 +08:00
|
|
|
FPConstants.add(Ty, V, Result = new ConstantFP(Ty, V));
|
2001-09-08 00:46:31 +08:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2001-12-04 06:26:30 +08:00
|
|
|
//---- ConstantArray::get() implementation...
|
2001-09-08 00:46:31 +08:00
|
|
|
//
|
2002-01-21 06:54:45 +08:00
|
|
|
static ValueMap<std::vector<Constant*>, ConstantArray> ArrayConstants;
|
2001-09-08 00:46:31 +08:00
|
|
|
|
2001-12-04 06:26:30 +08:00
|
|
|
ConstantArray *ConstantArray::get(const ArrayType *Ty,
|
2002-01-21 06:54:45 +08:00
|
|
|
const std::vector<Constant*> &V) {
|
2001-12-04 06:26:30 +08:00
|
|
|
ConstantArray *Result = ArrayConstants.get(Ty, V);
|
2001-09-08 00:46:31 +08:00
|
|
|
if (!Result) // If no preexisting value, create one now...
|
2001-12-04 06:26:30 +08:00
|
|
|
ArrayConstants.add(Ty, V, Result = new ConstantArray(Ty, V));
|
2001-09-08 00:46:31 +08:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2001-12-04 06:26:30 +08:00
|
|
|
// ConstantArray::get(const string&) - Return an array that is initialized to
|
2001-10-15 07:54:12 +08: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-21 06:54:45 +08:00
|
|
|
ConstantArray *ConstantArray::get(const std::string &Str) {
|
|
|
|
std::vector<Constant*> ElementVals;
|
2001-10-15 07:54:12 +08:00
|
|
|
|
|
|
|
for (unsigned i = 0; i < Str.length(); ++i)
|
2001-12-15 00:41:18 +08:00
|
|
|
ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i]));
|
2001-10-15 07:54:12 +08:00
|
|
|
|
|
|
|
// Add a null terminator to the string...
|
2001-12-15 00:41:18 +08:00
|
|
|
ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
|
2001-10-15 07:54:12 +08:00
|
|
|
|
2001-12-15 00:41:18 +08:00
|
|
|
ArrayType *ATy = ArrayType::get(Type::SByteTy, Str.length()+1);
|
2001-12-04 06:26:30 +08:00
|
|
|
return ConstantArray::get(ATy, ElementVals);
|
2001-10-15 07:17:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-10-13 14:57:33 +08:00
|
|
|
// destroyConstant - Remove the constant from the constant table...
|
|
|
|
//
|
2001-12-04 06:26:30 +08:00
|
|
|
void ConstantArray::destroyConstant() {
|
2001-10-13 14:57:33 +08:00
|
|
|
ArrayConstants.remove(this);
|
|
|
|
destroyConstantImpl();
|
|
|
|
}
|
|
|
|
|
2001-12-04 06:26:30 +08:00
|
|
|
//---- ConstantStruct::get() implementation...
|
2001-09-08 00:46:31 +08:00
|
|
|
//
|
2002-01-21 06:54:45 +08:00
|
|
|
static ValueMap<std::vector<Constant*>, ConstantStruct> StructConstants;
|
2001-09-08 00:46:31 +08:00
|
|
|
|
2001-12-04 06:26:30 +08:00
|
|
|
ConstantStruct *ConstantStruct::get(const StructType *Ty,
|
2002-01-21 06:54:45 +08:00
|
|
|
const std::vector<Constant*> &V) {
|
2001-12-04 06:26:30 +08:00
|
|
|
ConstantStruct *Result = StructConstants.get(Ty, V);
|
2001-09-08 00:46:31 +08:00
|
|
|
if (!Result) // If no preexisting value, create one now...
|
2001-12-04 06:26:30 +08:00
|
|
|
StructConstants.add(Ty, V, Result = new ConstantStruct(Ty, V));
|
2001-09-08 00:46:31 +08:00
|
|
|
return Result;
|
|
|
|
}
|
2001-10-03 23:39:36 +08:00
|
|
|
|
2001-10-13 14:57:33 +08:00
|
|
|
// destroyConstant - Remove the constant from the constant table...
|
|
|
|
//
|
2001-12-04 06:26:30 +08:00
|
|
|
void ConstantStruct::destroyConstant() {
|
2001-10-13 14:57:33 +08:00
|
|
|
StructConstants.remove(this);
|
|
|
|
destroyConstantImpl();
|
|
|
|
}
|
|
|
|
|
2001-12-04 06:26:30 +08:00
|
|
|
//---- ConstantPointerNull::get() implementation...
|
2001-10-03 23:39:36 +08:00
|
|
|
//
|
2001-12-04 06:26:30 +08:00
|
|
|
static ValueMap<char, ConstantPointerNull> NullPtrConstants;
|
2001-10-03 23:39:36 +08:00
|
|
|
|
2001-12-04 06:26:30 +08:00
|
|
|
ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
|
|
|
|
ConstantPointerNull *Result = NullPtrConstants.get(Ty, 0);
|
2001-10-03 23:39:36 +08:00
|
|
|
if (!Result) // If no preexisting value, create one now...
|
2001-12-04 06:26:30 +08:00
|
|
|
NullPtrConstants.add(Ty, 0, Result = new ConstantPointerNull(Ty));
|
2001-10-03 23:39:36 +08:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2001-12-04 06:26:30 +08:00
|
|
|
//---- ConstantPointerRef::get() implementation...
|
2001-10-04 03:28:15 +08:00
|
|
|
//
|
2001-12-04 06:26:30 +08:00
|
|
|
ConstantPointerRef *ConstantPointerRef::get(GlobalValue *GV) {
|
2001-10-13 14:57:33 +08:00
|
|
|
assert(GV->getParent() && "Global Value must be attached to a module!");
|
2002-07-15 07:13:17 +08:00
|
|
|
|
2001-10-13 14:57:33 +08:00
|
|
|
// The Module handles the pointer reference sharing...
|
2001-12-04 06:26:30 +08:00
|
|
|
return GV->getParent()->getConstantPointerRef(GV);
|
2001-10-13 14:57:33 +08:00
|
|
|
}
|
|
|
|
|
2002-07-15 07:13:17 +08:00
|
|
|
//---- ConstantExpr::get() implementations...
|
|
|
|
// Return NULL on invalid expressions.
|
|
|
|
//
|
|
|
|
ConstantExpr*
|
|
|
|
ConstantExpr::get(unsigned opCode, Constant *C, const Type *Ty) {
|
|
|
|
if (opCode != Instruction::Cast &&
|
|
|
|
(opCode < Instruction::FirstUnaryOp ||
|
|
|
|
opCode >= Instruction::NumUnaryOps)) {
|
|
|
|
cerr << "Invalid opcode " << ConstantExpr::getOpcodeName(opCode)
|
|
|
|
<< " in unary constant expression" << endl;
|
|
|
|
return NULL; // Not Cast or other unary opcode
|
|
|
|
}
|
|
|
|
// type of operand will not match result for Cast operation
|
|
|
|
if (opCode != Instruction::Cast && Ty != C->getType()) {
|
|
|
|
cerr << "Type of operand in unary constant expression should match result" << endl;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return new ConstantExpr(opCode, C, Ty);
|
|
|
|
}
|
|
|
|
|
|
|
|
ConstantExpr*
|
|
|
|
ConstantExpr::get(unsigned opCode, Constant *C1, Constant *C2,const Type *Ty) {
|
|
|
|
if (opCode < Instruction::FirstBinaryOp ||
|
|
|
|
opCode >= Instruction::NumBinaryOps) {
|
|
|
|
cerr << "Invalid opcode " << ConstantExpr::getOpcodeName(opCode)
|
|
|
|
<< " in binary constant expression" << endl;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (Ty != C1->getType() || Ty != C2->getType()) {
|
|
|
|
cerr << "Types of both operands in binary constant expression should match result" << endl;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return new ConstantExpr(opCode, C1, C2, Ty);
|
|
|
|
}
|
2001-10-13 14:57:33 +08:00
|
|
|
|
2002-07-15 07:13:17 +08:00
|
|
|
ConstantExpr*
|
|
|
|
ConstantExpr::get(unsigned opCode, Constant*C,
|
|
|
|
const std::vector<Value*>& idxList, const Type *Ty) {
|
|
|
|
// Must be a getElementPtr. Check for valid getElementPtr expression.
|
|
|
|
//
|
|
|
|
if (opCode != Instruction::GetElementPtr) {
|
|
|
|
cerr << "operator other than GetElementPtr used with an index list" << endl;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (!isa<ConstantPointer>(C)) {
|
|
|
|
cerr << "Constant GelElementPtr expression using something other than a constant pointer" << endl;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (!isa<PointerType>(Ty)) {
|
|
|
|
cerr << "Non-pointer type for constant GelElementPtr expression" << endl;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
const Type* fldType = GetElementPtrInst::getIndexedType(C->getType(),
|
|
|
|
idxList, true);
|
|
|
|
if (!fldType) {
|
|
|
|
cerr << "Invalid index list for constant GelElementPtr expression" << endl;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (cast<PointerType>(Ty)->getElementType() != fldType) {
|
|
|
|
cerr << "Type for constant GelElementPtr expression does not match field type" << endl;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new ConstantExpr(opCode, C, idxList, Ty);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char*
|
|
|
|
ConstantExpr::getOpcodeName(unsigned opCode) {
|
|
|
|
return Instruction::getOpcodeName(opCode);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//---- ConstantPointerRef::mutateReferences() implementation...
|
|
|
|
//
|
|
|
|
unsigned
|
|
|
|
ConstantPointerRef::mutateReferences(Value* OldV, Value *NewV) {
|
|
|
|
assert(getValue() == OldV && "Cannot mutate old value if I'm not using it!");
|
|
|
|
GlobalValue* NewGV = cast<GlobalValue>(NewV);
|
2001-12-04 06:26:30 +08:00
|
|
|
getValue()->getParent()->mutateConstantPointerRef(getValue(), NewGV);
|
2001-10-13 14:57:33 +08:00
|
|
|
Operands[0] = NewGV;
|
2002-07-15 07:13:17 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//---- ConstantPointerExpr::mutateReferences() implementation...
|
|
|
|
//
|
|
|
|
unsigned
|
|
|
|
ConstantExpr::mutateReferences(Value* OldV, Value *NewV) {
|
|
|
|
unsigned numReplaced = 0;
|
|
|
|
Constant* NewC = cast<Constant>(NewV);
|
|
|
|
for (unsigned i=0, N = getNumOperands(); i < N; ++i)
|
|
|
|
if (Operands[i] == OldV) {
|
|
|
|
++numReplaced;
|
|
|
|
Operands[i] = NewC;
|
|
|
|
}
|
|
|
|
return numReplaced;
|
2001-10-04 03:28:15 +08:00
|
|
|
}
|