1 //===-- iConstPool.cpp - Implement ConstPool instructions --------*- C++ -*--=//
3 // This file implements the ConstPool* classes...
5 //===----------------------------------------------------------------------===//
7 #define __STDC_LIMIT_MACROS // Get defs for INT64_MAX and friends...
8 #include "llvm/ConstPoolVals.h"
9 #include "llvm/Support/StringExtras.h" // itostr
10 #include "llvm/DerivedTypes.h"
11 #include "llvm/SymbolTable.h"
12 #include "llvm/GlobalVariable.h" // TODO make this GlobalValue.h
16 ConstPoolBool *ConstPoolBool::True = new ConstPoolBool(true);
17 ConstPoolBool *ConstPoolBool::False = new ConstPoolBool(false);
20 //===----------------------------------------------------------------------===//
22 //===----------------------------------------------------------------------===//
24 // Specialize setName to take care of symbol table majik
25 void ConstPoolVal::setName(const string &Name, SymbolTable *ST) {
26 assert(ST && "Type::setName - Must provide symbol table argument!");
28 if (Name.size()) ST->insert(Name, this);
31 // Static constructor to create a '0' constant of arbitrary type...
32 ConstPoolVal *ConstPoolVal::getNullConstant(const Type *Ty) {
33 switch (Ty->getPrimitiveID()) {
34 case Type::BoolTyID: return ConstPoolBool::get(false);
38 case Type::LongTyID: return ConstPoolSInt::get(Ty, 0);
41 case Type::UShortTyID:
43 case Type::ULongTyID: return ConstPoolUInt::get(Ty, 0);
46 case Type::DoubleTyID: return ConstPoolFP::get(Ty, 0);
48 case Type::PointerTyID:
49 return ConstPoolPointer::getNull(cast<PointerType>(Ty));
55 bool ConstPoolInt::classof(const ConstPoolVal *CPV) {
56 return CPV->getType()->isIntegral();
59 //===----------------------------------------------------------------------===//
60 // ConstPoolXXX Classes
61 //===----------------------------------------------------------------------===//
63 //===----------------------------------------------------------------------===//
64 // Normal Constructors
66 ConstPoolBool::ConstPoolBool(bool V) : ConstPoolVal(Type::BoolTy) {
70 ConstPoolInt::ConstPoolInt(const Type *Ty, uint64_t V) : ConstPoolVal(Ty) {
74 ConstPoolSInt::ConstPoolSInt(const Type *Ty, int64_t V) : ConstPoolInt(Ty, V) {
75 assert(isValueValidForType(Ty, V) && "Value too large for type!");
78 ConstPoolUInt::ConstPoolUInt(const Type *Ty, uint64_t V) : ConstPoolInt(Ty, V) {
79 assert(isValueValidForType(Ty, V) && "Value too large for type!");
82 ConstPoolFP::ConstPoolFP(const Type *Ty, double V) : ConstPoolVal(Ty) {
83 assert(isValueValidForType(Ty, V) && "Value too large for type!");
87 ConstPoolArray::ConstPoolArray(const ArrayType *T,
88 const vector<ConstPoolVal*> &V)
90 for (unsigned i = 0; i < V.size(); i++) {
91 assert(V[i]->getType() == T->getElementType());
92 Operands.push_back(Use(V[i], this));
96 ConstPoolStruct::ConstPoolStruct(const StructType *T,
97 const vector<ConstPoolVal*> &V)
99 const StructType::ElementTypes &ETypes = T->getElementTypes();
101 for (unsigned i = 0; i < V.size(); i++) {
102 assert(V[i]->getType() == ETypes[i]);
103 Operands.push_back(Use(V[i], this));
107 ConstPoolPointer::ConstPoolPointer(const PointerType *T) : ConstPoolVal(T) {}
109 ConstPoolPointerReference::ConstPoolPointerReference(GlobalValue *GV)
110 : ConstPoolPointer(GV->getType()) {
111 Operands.push_back(Use(GV, this));
116 //===----------------------------------------------------------------------===//
117 // getStrValue implementations
119 string ConstPoolBool::getStrValue() const {
120 return Val ? "true" : "false";
123 string ConstPoolSInt::getStrValue() const {
124 return itostr(Val.Signed);
127 string ConstPoolUInt::getStrValue() const {
128 return utostr(Val.Unsigned);
131 string ConstPoolFP::getStrValue() const {
135 string ConstPoolArray::getStrValue() const {
137 if (Operands.size()) {
138 Result += " " + Operands[0]->getType()->getDescription() +
139 " " + cast<ConstPoolVal>(Operands[0])->getStrValue();
140 for (unsigned i = 1; i < Operands.size(); i++)
141 Result += ", " + Operands[i]->getType()->getDescription() +
142 " " + cast<ConstPoolVal>(Operands[i])->getStrValue();
145 return Result + " ]";
148 string ConstPoolStruct::getStrValue() const {
150 if (Operands.size()) {
151 Result += " " + Operands[0]->getType()->getDescription() +
152 " " + cast<ConstPoolVal>(Operands[0])->getStrValue();
153 for (unsigned i = 1; i < Operands.size(); i++)
154 Result += ", " + Operands[i]->getType()->getDescription() +
155 " " + cast<ConstPoolVal>(Operands[i])->getStrValue();
158 return Result + " }";
161 string ConstPoolPointer::getStrValue() const {
165 string ConstPoolPointerReference::getStrValue() const {
166 return "<pointer reference>";
169 //===----------------------------------------------------------------------===//
170 // isValueValidForType implementations
172 bool ConstPoolSInt::isValueValidForType(const Type *Ty, int64_t Val) {
173 switch (Ty->getPrimitiveID()) {
175 return false; // These can't be represented as integers!!!
178 case Type::SByteTyID:
179 return (Val <= INT8_MAX && Val >= INT8_MIN);
180 case Type::ShortTyID:
181 return (Val <= INT16_MAX && Val >= INT16_MIN);
183 return (Val <= INT32_MAX && Val >= INT32_MIN);
185 return true; // This is the largest type...
191 bool ConstPoolUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
192 switch (Ty->getPrimitiveID()) {
194 return false; // These can't be represented as integers!!!
197 case Type::UByteTyID:
198 return (Val <= UINT8_MAX);
199 case Type::UShortTyID:
200 return (Val <= UINT16_MAX);
202 return (Val <= UINT32_MAX);
203 case Type::ULongTyID:
204 return true; // This is the largest type...
210 bool ConstPoolFP::isValueValidForType(const Type *Ty, double Val) {
211 switch (Ty->getPrimitiveID()) {
213 return false; // These can't be represented as floating point!
215 // TODO: Figure out how to test if a double can be cast to a float!
216 case Type::FloatTyID:
218 return (Val <= UINT8_MAX);
220 case Type::DoubleTyID:
221 return true; // This is the largest type...
225 //===----------------------------------------------------------------------===//
226 // Hash Function Implementations
228 unsigned ConstPoolSInt::hash(const Type *Ty, int64_t V) {
229 return unsigned(Ty->getPrimitiveID() ^ V);
232 unsigned ConstPoolUInt::hash(const Type *Ty, uint64_t V) {
233 return unsigned(Ty->getPrimitiveID() ^ V);
236 unsigned ConstPoolFP::hash(const Type *Ty, double V) {
237 return Ty->getPrimitiveID() ^ unsigned(V);
240 unsigned ConstPoolArray::hash(const ArrayType *Ty,
241 const vector<ConstPoolVal*> &V) {
242 unsigned Result = (Ty->getUniqueID() << 5) ^ (Ty->getUniqueID() * 7);
243 for (unsigned i = 0; i < V.size(); ++i)
244 Result ^= V[i]->getHash() << (i & 7);
248 unsigned ConstPoolStruct::hash(const StructType *Ty,
249 const vector<ConstPoolVal*> &V) {
250 unsigned Result = (Ty->getUniqueID() << 5) ^ (Ty->getUniqueID() * 7);
251 for (unsigned i = 0; i < V.size(); ++i)
252 Result ^= V[i]->getHash() << (i & 7);
257 //===----------------------------------------------------------------------===//
258 // Factory Function Implementation
260 template<class ValType, class ConstPoolClass>
262 typedef pair<const Type*, ValType> ConstHashKey;
263 map<ConstHashKey, ConstPoolClass *> Map;
265 inline ConstPoolClass *get(const Type *Ty, ValType V) {
266 map<ConstHashKey,ConstPoolClass *>::iterator I =
267 Map.find(ConstHashKey(Ty, V));
268 return (I != Map.end()) ? I->second : 0;
271 inline void add(const Type *Ty, ValType V, ConstPoolClass *CP) {
272 Map.insert(make_pair(ConstHashKey(Ty, V), CP));
276 //---- ConstPoolUInt::get() and ConstPoolSInt::get() implementations...
278 static ValueMap<uint64_t, ConstPoolInt> IntConstants;
280 ConstPoolSInt *ConstPoolSInt::get(const Type *Ty, int64_t V) {
281 ConstPoolSInt *Result = (ConstPoolSInt*)IntConstants.get(Ty, (uint64_t)V);
282 if (!Result) // If no preexisting value, create one now...
283 IntConstants.add(Ty, V, Result = new ConstPoolSInt(Ty, V));
287 ConstPoolUInt *ConstPoolUInt::get(const Type *Ty, uint64_t V) {
288 ConstPoolUInt *Result = (ConstPoolUInt*)IntConstants.get(Ty, V);
289 if (!Result) // If no preexisting value, create one now...
290 IntConstants.add(Ty, V, Result = new ConstPoolUInt(Ty, V));
294 ConstPoolInt *ConstPoolInt::get(const Type *Ty, unsigned char V) {
295 assert(V <= 127 && "Can only be used with very small positive constants!");
296 if (Ty->isSigned()) return ConstPoolSInt::get(Ty, V);
297 return ConstPoolUInt::get(Ty, V);
300 //---- ConstPoolFP::get() implementation...
302 static ValueMap<double, ConstPoolFP> FPConstants;
304 ConstPoolFP *ConstPoolFP::get(const Type *Ty, double V) {
305 ConstPoolFP *Result = FPConstants.get(Ty, V);
306 if (!Result) // If no preexisting value, create one now...
307 FPConstants.add(Ty, V, Result = new ConstPoolFP(Ty, V));
311 //---- ConstPoolArray::get() implementation...
313 static ValueMap<vector<ConstPoolVal*>, ConstPoolArray> ArrayConstants;
315 ConstPoolArray *ConstPoolArray::get(const ArrayType *Ty,
316 const vector<ConstPoolVal*> &V) {
317 ConstPoolArray *Result = ArrayConstants.get(Ty, V);
318 if (!Result) // If no preexisting value, create one now...
319 ArrayConstants.add(Ty, V, Result = new ConstPoolArray(Ty, V));
323 //---- ConstPoolStruct::get() implementation...
325 static ValueMap<vector<ConstPoolVal*>, ConstPoolStruct> StructConstants;
327 ConstPoolStruct *ConstPoolStruct::get(const StructType *Ty,
328 const vector<ConstPoolVal*> &V) {
329 ConstPoolStruct *Result = StructConstants.get(Ty, V);
330 if (!Result) // If no preexisting value, create one now...
331 StructConstants.add(Ty, V, Result = new ConstPoolStruct(Ty, V));
335 //---- ConstPoolPointer::get() implementation...
337 static ValueMap<char, ConstPoolPointer> NullPtrConstants;
339 ConstPoolPointer *ConstPoolPointer::getNull(const PointerType *Ty) {
340 ConstPoolPointer *Result = NullPtrConstants.get(Ty, 0);
341 if (!Result) // If no preexisting value, create one now...
342 NullPtrConstants.add(Ty, 0, Result = new ConstPoolPointer(Ty));
346 //---- ConstPoolPointerReference::get() implementation...
348 ConstPoolPointerReference *ConstPoolPointerReference::get(GlobalValue *GV) {
349 assert(GV->getParent());
350 // FIXME: These should all be shared!
351 return new ConstPoolPointerReference(GV);