-//===-- ConstantVals.cpp - Implement Constant nodes --------------*- C++ -*--=//
+//===-- Constants.cpp - Implement Constant nodes -----------------*- C++ -*--=//
//
// This file implements the Constant* classes...
//
//===----------------------------------------------------------------------===//
#define __STDC_LIMIT_MACROS // Get defs for INT64_MAX and friends...
-#include "llvm/ConstantVals.h"
+#include "llvm/Constants.h"
#include "llvm/DerivedTypes.h"
#include "llvm/SymbolTable.h"
#include "llvm/GlobalValue.h"
#include "llvm/Module.h"
-#include "llvm/Analysis/SlotCalculator.h"
+#include "llvm/SlotCalculator.h"
#include "Support/StringExtras.h"
#include <algorithm>
}
// Static constructor to create a '0' constant of arbitrary type...
-Constant *Constant::getNullConstant(const Type *Ty) {
+Constant *Constant::getNullValue(const Type *Ty) {
switch (Ty->getPrimitiveID()) {
case Type::BoolTyID: return ConstantBool::get(false);
case Type::SByteTyID:
}
}
-#ifndef NDEBUG
-#include "llvm/Assembly/Writer.h"
-using std::cerr;
-#endif
-
void Constant::destroyConstantImpl() {
// When a Constant is destroyed, there may be lingering
// references to the constant by other constants in the constant pool. These
Value *V = use_back();
#ifndef NDEBUG // Only in -g mode...
if (!isa<Constant>(V)) {
- cerr << "While deleting: " << this << "\n";
- cerr << "Use still stuck around after Def is destroyed: " << V << "\n";
+ std::cerr << "While deleting: ";
+ dump();
+ std::cerr << "\nUse still stuck around after Def is destroyed: ";
+ V->dump();
+ std::cerr << "\n";
}
#endif
assert(isa<Constant>(V) && "References remain to ConstantPointerRef!");
-//===----------------------------------------------------------------------===//
-// getStrValue implementations
-
-std::string ConstantBool::getStrValue() const {
- return Val ? "true" : "false";
-}
-
-std::string ConstantSInt::getStrValue() const {
- return itostr(Val.Signed);
-}
-
-std::string ConstantUInt::getStrValue() const {
- return utostr(Val.Unsigned);
-}
-
-// 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.
-//
-std::string ConstantFP::getStrValue() const {
- std::string StrVal = ftostr(Val);
- double TestVal = atof(StrVal.c_str()); // Reparse stringized version!
- if (TestVal == Val)
- return StrVal;
-
- // 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);
-}
-
-std::string ConstantArray::getStrValue() const {
- std::string Result;
-
- // 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.
- //
- const Type *ETy = cast<ArrayType>(getType())->getElementType();
- bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
-
- if (ETy == Type::SByteTy) {
- for (unsigned i = 0; i < Operands.size(); ++i)
- if (ETy == Type::SByteTy &&
- cast<ConstantSInt>(Operands[i])->getValue() < 0) {
- isString = false;
- break;
- }
- }
-
- if (isString) {
- Result = "c\"";
- for (unsigned i = 0; i < Operands.size(); ++i) {
- unsigned char C = (ETy == Type::SByteTy) ?
- (unsigned char)cast<ConstantSInt>(Operands[i])->getValue() :
- (unsigned char)cast<ConstantUInt>(Operands[i])->getValue();
-
- if (isprint(C)) {
- Result += C;
- } else {
- Result += '\\';
- Result += ( C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A');
- Result += ((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A');
- }
- }
- Result += "\"";
-
- } else {
- Result = "[";
- if (Operands.size()) {
- Result += " " + Operands[0]->getType()->getDescription() +
- " " + cast<Constant>(Operands[0])->getStrValue();
- for (unsigned i = 1; i < Operands.size(); i++)
- Result += ", " + Operands[i]->getType()->getDescription() +
- " " + cast<Constant>(Operands[i])->getStrValue();
- }
- Result += " ]";
- }
-
- return Result;
-}
-
-std::string ConstantStruct::getStrValue() const {
- std::string Result = "{";
- if (Operands.size()) {
- Result += " " + Operands[0]->getType()->getDescription() +
- " " + cast<Constant>(Operands[0])->getStrValue();
- for (unsigned i = 1; i < Operands.size(); i++)
- Result += ", " + Operands[i]->getType()->getDescription() +
- " " + cast<Constant>(Operands[i])->getStrValue();
- }
-
- return Result + " }";
-}
-
-std::string ConstantPointerNull::getStrValue() const {
- return "null";
-}
-
-std::string ConstantPointerRef::getStrValue() const {
- const GlobalValue *V = getValue();
- if (V->hasName()) return "%" + V->getName();
-
- SlotCalculator *Table = new SlotCalculator(V->getParent(), true);
- int Slot = Table->getValSlot(V);
- delete Table;
-
- if (Slot >= 0) return std::string(" %") + itostr(Slot);
- else return "<pointer reference badref>";
-}
-
-
//===----------------------------------------------------------------------===//
// classof implementations
}
};
-//===----------------------------------------------------------------------===//
-// Hash Function Implementations
-#if 0
-unsigned ConstantSInt::hash(const Type *Ty, int64_t V) {
- return unsigned(Ty->getPrimitiveID() ^ V);
-}
-
-unsigned ConstantUInt::hash(const Type *Ty, uint64_t V) {
- return unsigned(Ty->getPrimitiveID() ^ V);
-}
-
-unsigned ConstantFP::hash(const Type *Ty, double V) {
- return Ty->getPrimitiveID() ^ unsigned(V);
-}
-
-unsigned ConstantArray::hash(const ArrayType *Ty,
- const std::vector<Constant*> &V) {
- unsigned Result = (Ty->getUniqueID() << 5) ^ (Ty->getUniqueID() * 7);
- for (unsigned i = 0; i < V.size(); ++i)
- Result ^= V[i]->getHash() << (i & 7);
- return Result;
-}
-
-unsigned ConstantStruct::hash(const StructType *Ty,
- const std::vector<Constant*> &V) {
- 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
-
//===----------------------------------------------------------------------===//
// Factory Function Implementation