1 //===-- ConstantVals.cpp - Implement Constant nodes --------------*- C++ -*--=//
3 // This file implements the Constant* classes...
5 //===----------------------------------------------------------------------===//
7 #define __STDC_LIMIT_MACROS // Get defs for INT64_MAX and friends...
8 #include "llvm/ConstantVals.h"
9 #include "llvm/DerivedTypes.h"
10 #include "llvm/SymbolTable.h"
11 #include "llvm/GlobalValue.h"
12 #include "llvm/Module.h"
13 #include "llvm/Analysis/SlotCalculator.h"
14 #include "Support/StringExtras.h"
22 ConstantBool *ConstantBool::True = new ConstantBool(true);
23 ConstantBool *ConstantBool::False = new ConstantBool(false);
26 //===----------------------------------------------------------------------===//
28 //===----------------------------------------------------------------------===//
30 // Specialize setName to take care of symbol table majik
31 void Constant::setName(const std::string &Name, SymbolTable *ST) {
32 assert(ST && "Type::setName - Must provide symbol table argument!");
34 if (Name.size()) ST->insert(Name, this);
37 // Static constructor to create a '0' constant of arbitrary type...
38 Constant *Constant::getNullConstant(const Type *Ty) {
39 switch (Ty->getPrimitiveID()) {
40 case Type::BoolTyID: return ConstantBool::get(false);
44 case Type::LongTyID: return ConstantSInt::get(Ty, 0);
47 case Type::UShortTyID:
49 case Type::ULongTyID: return ConstantUInt::get(Ty, 0);
52 case Type::DoubleTyID: return ConstantFP::get(Ty, 0);
54 case Type::PointerTyID:
55 return ConstantPointerNull::get(cast<PointerType>(Ty));
62 #include "llvm/Assembly/Writer.h"
66 void Constant::destroyConstantImpl() {
67 // When a Constant is destroyed, there may be lingering
68 // references to the constant by other constants in the constant pool. These
69 // constants are implicitly dependant on the module that is being deleted,
70 // but they don't know that. Because we only find out when the CPV is
71 // deleted, we must now notify all of our users (that should only be
72 // Constants) that they are, in fact, invalid now and should be deleted.
74 while (!use_empty()) {
75 Value *V = use_back();
76 #ifndef NDEBUG // Only in -g mode...
77 if (!isa<Constant>(V)) {
78 cerr << "While deleting: " << this << "\n";
79 cerr << "Use still stuck around after Def is destroyed: " << V << "\n";
82 assert(isa<Constant>(V) && "References remain to ConstantPointerRef!");
83 Constant *CPV = cast<Constant>(V);
84 CPV->destroyConstant();
86 // The constant should remove itself from our use list...
87 assert((use_empty() || use_back() == V) && "Constant not removed!");
90 // Value has no outstanding references it is safe to delete it now...
94 //===----------------------------------------------------------------------===//
95 // ConstantXXX Classes
96 //===----------------------------------------------------------------------===//
98 //===----------------------------------------------------------------------===//
99 // Normal Constructors
101 ConstantBool::ConstantBool(bool V) : Constant(Type::BoolTy) {
105 ConstantInt::ConstantInt(const Type *Ty, uint64_t V) : Constant(Ty) {
109 ConstantSInt::ConstantSInt(const Type *Ty, int64_t V) : ConstantInt(Ty, V) {
110 assert(isValueValidForType(Ty, V) && "Value too large for type!");
113 ConstantUInt::ConstantUInt(const Type *Ty, uint64_t V) : ConstantInt(Ty, V) {
114 assert(isValueValidForType(Ty, V) && "Value too large for type!");
117 ConstantFP::ConstantFP(const Type *Ty, double V) : Constant(Ty) {
118 assert(isValueValidForType(Ty, V) && "Value too large for type!");
122 ConstantArray::ConstantArray(const ArrayType *T,
123 const std::vector<Constant*> &V) : Constant(T) {
124 for (unsigned i = 0; i < V.size(); i++) {
125 assert(V[i]->getType() == T->getElementType());
126 Operands.push_back(Use(V[i], this));
130 ConstantStruct::ConstantStruct(const StructType *T,
131 const std::vector<Constant*> &V) : Constant(T) {
132 const StructType::ElementTypes &ETypes = T->getElementTypes();
134 for (unsigned i = 0; i < V.size(); i++) {
135 assert(V[i]->getType() == ETypes[i]);
136 Operands.push_back(Use(V[i], this));
140 ConstantPointerRef::ConstantPointerRef(GlobalValue *GV)
141 : ConstantPointer(GV->getType()) {
142 Operands.push_back(Use(GV, this));
147 //===----------------------------------------------------------------------===//
148 // getStrValue implementations
150 std::string ConstantBool::getStrValue() const {
151 return Val ? "true" : "false";
154 std::string ConstantSInt::getStrValue() const {
155 return itostr(Val.Signed);
158 std::string ConstantUInt::getStrValue() const {
159 return utostr(Val.Unsigned);
162 std::string ConstantFP::getStrValue() const {
166 std::string ConstantArray::getStrValue() const {
169 // As a special case, print the array as a string if it is an array of
170 // ubytes or an array of sbytes with positive values.
172 const Type *ETy = cast<ArrayType>(getType())->getElementType();
173 bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
175 if (ETy == Type::SByteTy) {
176 for (unsigned i = 0; i < Operands.size(); ++i)
177 if (ETy == Type::SByteTy &&
178 cast<ConstantSInt>(Operands[i])->getValue() < 0) {
186 for (unsigned i = 0; i < Operands.size(); ++i) {
187 unsigned char C = (ETy == Type::SByteTy) ?
188 (unsigned char)cast<ConstantSInt>(Operands[i])->getValue() :
189 (unsigned char)cast<ConstantUInt>(Operands[i])->getValue();
195 Result += ( C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A');
196 Result += ((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A');
203 if (Operands.size()) {
204 Result += " " + Operands[0]->getType()->getDescription() +
205 " " + cast<Constant>(Operands[0])->getStrValue();
206 for (unsigned i = 1; i < Operands.size(); i++)
207 Result += ", " + Operands[i]->getType()->getDescription() +
208 " " + cast<Constant>(Operands[i])->getStrValue();
216 std::string ConstantStruct::getStrValue() const {
217 std::string Result = "{";
218 if (Operands.size()) {
219 Result += " " + Operands[0]->getType()->getDescription() +
220 " " + cast<Constant>(Operands[0])->getStrValue();
221 for (unsigned i = 1; i < Operands.size(); i++)
222 Result += ", " + Operands[i]->getType()->getDescription() +
223 " " + cast<Constant>(Operands[i])->getStrValue();
226 return Result + " }";
229 std::string ConstantPointerNull::getStrValue() const {
233 std::string ConstantPointerRef::getStrValue() const {
234 const GlobalValue *V = getValue();
235 if (V->hasName()) return "%" + V->getName();
237 SlotCalculator *Table = new SlotCalculator(V->getParent(), true);
238 int Slot = Table->getValSlot(V);
241 if (Slot >= 0) return std::string(" %") + itostr(Slot);
242 else return "<pointer reference badref>";
246 //===----------------------------------------------------------------------===//
247 // classof implementations
249 bool ConstantInt::classof(const Constant *CPV) {
250 return CPV->getType()->isIntegral();
252 bool ConstantSInt::classof(const Constant *CPV) {
253 return CPV->getType()->isSigned();
255 bool ConstantUInt::classof(const Constant *CPV) {
256 return CPV->getType()->isUnsigned();
258 bool ConstantFP::classof(const Constant *CPV) {
259 const Type *Ty = CPV->getType();
260 return Ty == Type::FloatTy || Ty == Type::DoubleTy;
262 bool ConstantArray::classof(const Constant *CPV) {
263 return isa<ArrayType>(CPV->getType());
265 bool ConstantStruct::classof(const Constant *CPV) {
266 return isa<StructType>(CPV->getType());
268 bool ConstantPointer::classof(const Constant *CPV) {
269 return isa<PointerType>(CPV->getType());
273 //===----------------------------------------------------------------------===//
274 // isValueValidForType implementations
276 bool ConstantSInt::isValueValidForType(const Type *Ty, int64_t Val) {
277 switch (Ty->getPrimitiveID()) {
279 return false; // These can't be represented as integers!!!
282 case Type::SByteTyID:
283 return (Val <= INT8_MAX && Val >= INT8_MIN);
284 case Type::ShortTyID:
285 return (Val <= INT16_MAX && Val >= INT16_MIN);
287 return (Val <= INT32_MAX && Val >= INT32_MIN);
289 return true; // This is the largest type...
295 bool ConstantUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
296 switch (Ty->getPrimitiveID()) {
298 return false; // These can't be represented as integers!!!
301 case Type::UByteTyID:
302 return (Val <= UINT8_MAX);
303 case Type::UShortTyID:
304 return (Val <= UINT16_MAX);
306 return (Val <= UINT32_MAX);
307 case Type::ULongTyID:
308 return true; // This is the largest type...
314 bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
315 switch (Ty->getPrimitiveID()) {
317 return false; // These can't be represented as floating point!
319 // TODO: Figure out how to test if a double can be cast to a float!
320 case Type::FloatTyID:
322 return (Val <= UINT8_MAX);
324 case Type::DoubleTyID:
325 return true; // This is the largest type...
329 //===----------------------------------------------------------------------===//
330 // Hash Function Implementations
332 unsigned ConstantSInt::hash(const Type *Ty, int64_t V) {
333 return unsigned(Ty->getPrimitiveID() ^ V);
336 unsigned ConstantUInt::hash(const Type *Ty, uint64_t V) {
337 return unsigned(Ty->getPrimitiveID() ^ V);
340 unsigned ConstantFP::hash(const Type *Ty, double V) {
341 return Ty->getPrimitiveID() ^ unsigned(V);
344 unsigned ConstantArray::hash(const ArrayType *Ty,
345 const std::vector<Constant*> &V) {
346 unsigned Result = (Ty->getUniqueID() << 5) ^ (Ty->getUniqueID() * 7);
347 for (unsigned i = 0; i < V.size(); ++i)
348 Result ^= V[i]->getHash() << (i & 7);
352 unsigned ConstantStruct::hash(const StructType *Ty,
353 const std::vector<Constant*> &V) {
354 unsigned Result = (Ty->getUniqueID() << 5) ^ (Ty->getUniqueID() * 7);
355 for (unsigned i = 0; i < V.size(); ++i)
356 Result ^= V[i]->getHash() << (i & 7);
361 //===----------------------------------------------------------------------===//
362 // Factory Function Implementation
364 template<class ValType, class ConstantClass>
366 typedef pair<const Type*, ValType> ConstHashKey;
367 map<ConstHashKey, ConstantClass *> Map;
369 inline ConstantClass *get(const Type *Ty, ValType V) {
370 map<ConstHashKey,ConstantClass *>::iterator I =
371 Map.find(ConstHashKey(Ty, V));
372 return (I != Map.end()) ? I->second : 0;
375 inline void add(const Type *Ty, ValType V, ConstantClass *CP) {
376 Map.insert(make_pair(ConstHashKey(Ty, V), CP));
379 inline void remove(ConstantClass *CP) {
380 for (map<ConstHashKey,ConstantClass *>::iterator I = Map.begin(),
381 E = Map.end(); I != E;++I)
382 if (I->second == CP) {
389 //---- ConstantUInt::get() and ConstantSInt::get() implementations...
391 static ValueMap<uint64_t, ConstantInt> IntConstants;
393 ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) {
394 ConstantSInt *Result = (ConstantSInt*)IntConstants.get(Ty, (uint64_t)V);
395 if (!Result) // If no preexisting value, create one now...
396 IntConstants.add(Ty, V, Result = new ConstantSInt(Ty, V));
400 ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) {
401 ConstantUInt *Result = (ConstantUInt*)IntConstants.get(Ty, V);
402 if (!Result) // If no preexisting value, create one now...
403 IntConstants.add(Ty, V, Result = new ConstantUInt(Ty, V));
407 ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) {
408 assert(V <= 127 && "Can only be used with very small positive constants!");
409 if (Ty->isSigned()) return ConstantSInt::get(Ty, V);
410 return ConstantUInt::get(Ty, V);
413 //---- ConstantFP::get() implementation...
415 static ValueMap<double, ConstantFP> FPConstants;
417 ConstantFP *ConstantFP::get(const Type *Ty, double V) {
418 ConstantFP *Result = FPConstants.get(Ty, V);
419 if (!Result) // If no preexisting value, create one now...
420 FPConstants.add(Ty, V, Result = new ConstantFP(Ty, V));
424 //---- ConstantArray::get() implementation...
426 static ValueMap<std::vector<Constant*>, ConstantArray> ArrayConstants;
428 ConstantArray *ConstantArray::get(const ArrayType *Ty,
429 const std::vector<Constant*> &V) {
430 ConstantArray *Result = ArrayConstants.get(Ty, V);
431 if (!Result) // If no preexisting value, create one now...
432 ArrayConstants.add(Ty, V, Result = new ConstantArray(Ty, V));
436 // ConstantArray::get(const string&) - Return an array that is initialized to
437 // contain the specified string. A null terminator is added to the specified
438 // string so that it may be used in a natural way...
440 ConstantArray *ConstantArray::get(const std::string &Str) {
441 std::vector<Constant*> ElementVals;
443 for (unsigned i = 0; i < Str.length(); ++i)
444 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i]));
446 // Add a null terminator to the string...
447 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
449 ArrayType *ATy = ArrayType::get(Type::SByteTy, Str.length()+1);
450 return ConstantArray::get(ATy, ElementVals);
454 // destroyConstant - Remove the constant from the constant table...
456 void ConstantArray::destroyConstant() {
457 ArrayConstants.remove(this);
458 destroyConstantImpl();
461 //---- ConstantStruct::get() implementation...
463 static ValueMap<std::vector<Constant*>, ConstantStruct> StructConstants;
465 ConstantStruct *ConstantStruct::get(const StructType *Ty,
466 const std::vector<Constant*> &V) {
467 ConstantStruct *Result = StructConstants.get(Ty, V);
468 if (!Result) // If no preexisting value, create one now...
469 StructConstants.add(Ty, V, Result = new ConstantStruct(Ty, V));
473 // destroyConstant - Remove the constant from the constant table...
475 void ConstantStruct::destroyConstant() {
476 StructConstants.remove(this);
477 destroyConstantImpl();
480 //---- ConstantPointerNull::get() implementation...
482 static ValueMap<char, ConstantPointerNull> NullPtrConstants;
484 ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
485 ConstantPointerNull *Result = NullPtrConstants.get(Ty, 0);
486 if (!Result) // If no preexisting value, create one now...
487 NullPtrConstants.add(Ty, 0, Result = new ConstantPointerNull(Ty));
491 //---- ConstantPointerRef::get() implementation...
493 ConstantPointerRef *ConstantPointerRef::get(GlobalValue *GV) {
494 assert(GV->getParent() && "Global Value must be attached to a module!");
496 // The Module handles the pointer reference sharing...
497 return GV->getParent()->getConstantPointerRef(GV);
501 void ConstantPointerRef::mutateReference(GlobalValue *NewGV) {
502 getValue()->getParent()->mutateConstantPointerRef(getValue(), NewGV);