Add more support for new style casts
[oota-llvm.git] / lib / VMCore / ConstPoolVals.cpp
1 //===-- iConstPool.cpp - Implement ConstPool instructions --------*- C++ -*--=//
2 //
3 // This file implements the ConstPool* classes...
4 //
5 //===----------------------------------------------------------------------===//
6
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 <algorithm>
13 #include <assert.h>
14
15 ConstPoolBool *ConstPoolBool::True  = new ConstPoolBool(true);
16 ConstPoolBool *ConstPoolBool::False = new ConstPoolBool(false);
17
18
19 //===----------------------------------------------------------------------===//
20 //                              ConstPoolVal Class
21 //===----------------------------------------------------------------------===//
22
23 // Specialize setName to take care of symbol table majik
24 void ConstPoolVal::setName(const string &Name, SymbolTable *ST) {
25   assert(ST && "Type::setName - Must provide symbol table argument!");
26
27   if (Name.size()) ST->insert(Name, this);
28 }
29
30 // Static constructor to create a '0' constant of arbitrary type...
31 ConstPoolVal *ConstPoolVal::getNullConstant(const Type *Ty) {
32   switch (Ty->getPrimitiveID()) {
33   case Type::BoolTyID:   return ConstPoolBool::get(false);
34   case Type::SByteTyID:
35   case Type::ShortTyID:
36   case Type::IntTyID:
37   case Type::LongTyID:   return ConstPoolSInt::get(Ty, 0);
38
39   case Type::UByteTyID:
40   case Type::UShortTyID:
41   case Type::UIntTyID:
42   case Type::ULongTyID:  return ConstPoolUInt::get(Ty, 0);
43
44   case Type::FloatTyID:
45   case Type::DoubleTyID: return ConstPoolFP::get(Ty, 0);
46
47   case Type::PointerTyID: 
48     return ConstPoolPointer::getNullPointer(Ty->castPointerType());
49   default:
50     return 0;
51   }
52 }
53
54
55
56 //===----------------------------------------------------------------------===//
57 //                            ConstPoolXXX Classes
58 //===----------------------------------------------------------------------===//
59
60 //===----------------------------------------------------------------------===//
61 //                             Normal Constructors
62
63 ConstPoolBool::ConstPoolBool(bool V) : ConstPoolVal(Type::BoolTy) {
64   Val = V;
65 }
66
67 ConstPoolInt::ConstPoolInt(const Type *Ty, uint64_t V) : ConstPoolVal(Ty) {
68   Val.Unsigned = V;
69 }
70
71 ConstPoolSInt::ConstPoolSInt(const Type *Ty, int64_t V) : ConstPoolInt(Ty, V) {
72   assert(isValueValidForType(Ty, V) && "Value too large for type!");
73 }
74
75 ConstPoolUInt::ConstPoolUInt(const Type *Ty, uint64_t V) : ConstPoolInt(Ty, V) {
76   assert(isValueValidForType(Ty, V) && "Value too large for type!");
77 }
78
79 ConstPoolFP::ConstPoolFP(const Type *Ty, double V) : ConstPoolVal(Ty) {
80   assert(isValueValidForType(Ty, V) && "Value too large for type!");
81   Val = V;
82 }
83
84 ConstPoolArray::ConstPoolArray(const ArrayType *T,
85                                const vector<ConstPoolVal*> &V)
86   : ConstPoolVal(T) {
87   for (unsigned i = 0; i < V.size(); i++) {
88     assert(V[i]->getType() == T->getElementType());
89     Operands.push_back(Use(V[i], this));
90   }
91 }
92
93 ConstPoolStruct::ConstPoolStruct(const StructType *T,
94                                  const vector<ConstPoolVal*> &V)
95   : ConstPoolVal(T) {
96   const StructType::ElementTypes &ETypes = T->getElementTypes();
97   
98   for (unsigned i = 0; i < V.size(); i++) {
99     assert(V[i]->getType() == ETypes[i]);
100     Operands.push_back(Use(V[i], this));
101   }
102 }
103
104 ConstPoolPointer::ConstPoolPointer(const PointerType *T) : ConstPoolVal(T) {}
105
106
107 //===----------------------------------------------------------------------===//
108 //                          getStrValue implementations
109
110 string ConstPoolBool::getStrValue() const {
111   return Val ? "true" : "false";
112 }
113
114 string ConstPoolSInt::getStrValue() const {
115   return itostr(Val.Signed);
116 }
117
118 string ConstPoolUInt::getStrValue() const {
119   return utostr(Val.Unsigned);
120 }
121
122 string ConstPoolFP::getStrValue() const {
123   return ftostr(Val);
124 }
125
126 string ConstPoolArray::getStrValue() const {
127   string Result = "[";
128   if (Operands.size()) {
129     Result += " " + Operands[0]->getType()->getDescription() + 
130               " " + cast<ConstPoolVal>(Operands[0])->getStrValue();
131     for (unsigned i = 1; i < Operands.size(); i++)
132       Result += ", " + Operands[i]->getType()->getDescription() + 
133                  " " + cast<ConstPoolVal>(Operands[i])->getStrValue();
134   }
135
136   return Result + " ]";
137 }
138
139 string ConstPoolStruct::getStrValue() const {
140   string Result = "{";
141   if (Operands.size()) {
142     Result += " " + Operands[0]->getType()->getDescription() + 
143               " " + cast<ConstPoolVal>(Operands[0])->getStrValue();
144     for (unsigned i = 1; i < Operands.size(); i++)
145       Result += ", " + Operands[i]->getType()->getDescription() + 
146                  " " + cast<ConstPoolVal>(Operands[i])->getStrValue();
147   }
148
149   return Result + " }";
150 }
151
152 string ConstPoolPointer::getStrValue() const {
153   return "null";
154 }
155
156 //===----------------------------------------------------------------------===//
157 //                      isValueValidForType implementations
158
159 bool ConstPoolSInt::isValueValidForType(const Type *Ty, int64_t Val) {
160   switch (Ty->getPrimitiveID()) {
161   default:
162     return false;         // These can't be represented as integers!!!
163
164     // Signed types...
165   case Type::SByteTyID:
166     return (Val <= INT8_MAX && Val >= INT8_MIN);
167   case Type::ShortTyID:
168     return (Val <= INT16_MAX && Val >= INT16_MIN);
169   case Type::IntTyID:
170     return (Val <= INT32_MAX && Val >= INT32_MIN);
171   case Type::LongTyID:
172     return true;          // This is the largest type...
173   }
174   assert(0 && "WTF?");
175   return false;
176 }
177
178 bool ConstPoolUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
179   switch (Ty->getPrimitiveID()) {
180   default:
181     return false;         // These can't be represented as integers!!!
182
183     // Unsigned types...
184   case Type::UByteTyID:
185     return (Val <= UINT8_MAX);
186   case Type::UShortTyID:
187     return (Val <= UINT16_MAX);
188   case Type::UIntTyID:
189     return (Val <= UINT32_MAX);
190   case Type::ULongTyID:
191     return true;          // This is the largest type...
192   }
193   assert(0 && "WTF?");
194   return false;
195 }
196
197 bool ConstPoolFP::isValueValidForType(const Type *Ty, double Val) {
198   switch (Ty->getPrimitiveID()) {
199   default:
200     return false;         // These can't be represented as floating point!
201
202     // TODO: Figure out how to test if a double can be cast to a float!
203   case Type::FloatTyID:
204     /*
205     return (Val <= UINT8_MAX);
206     */
207   case Type::DoubleTyID:
208     return true;          // This is the largest type...
209   }
210 };
211
212 //===----------------------------------------------------------------------===//
213 //                      Hash Function Implementations
214 #if 0
215 unsigned ConstPoolSInt::hash(const Type *Ty, int64_t V) {
216   return unsigned(Ty->getPrimitiveID() ^ V);
217 }
218
219 unsigned ConstPoolUInt::hash(const Type *Ty, uint64_t V) {
220   return unsigned(Ty->getPrimitiveID() ^ V);
221 }
222
223 unsigned ConstPoolFP::hash(const Type *Ty, double V) {
224   return Ty->getPrimitiveID() ^ unsigned(V);
225 }
226
227 unsigned ConstPoolArray::hash(const ArrayType *Ty,
228                               const vector<ConstPoolVal*> &V) {
229   unsigned Result = (Ty->getUniqueID() << 5) ^ (Ty->getUniqueID() * 7);
230   for (unsigned i = 0; i < V.size(); ++i)
231     Result ^= V[i]->getHash() << (i & 7);
232   return Result;
233 }
234
235 unsigned ConstPoolStruct::hash(const StructType *Ty,
236                                const vector<ConstPoolVal*> &V) {
237   unsigned Result = (Ty->getUniqueID() << 5) ^ (Ty->getUniqueID() * 7);
238   for (unsigned i = 0; i < V.size(); ++i)
239     Result ^= V[i]->getHash() << (i & 7);
240   return Result;
241 }
242 #endif
243
244 //===----------------------------------------------------------------------===//
245 //                      Factory Function Implementation
246
247 template<class ValType, class ConstPoolClass>
248 struct ValueMap {
249   typedef pair<const Type*, ValType> ConstHashKey;
250   map<ConstHashKey, ConstPoolClass *> Map;
251
252   inline ConstPoolClass *get(const Type *Ty, ValType V) {
253     map<ConstHashKey,ConstPoolClass *>::iterator I =
254       Map.find(ConstHashKey(Ty, V));
255     return (I != Map.end()) ? I->second : 0;
256   }
257
258   inline void add(const Type *Ty, ValType V, ConstPoolClass *CP) {
259     Map.insert(make_pair(ConstHashKey(Ty, V), CP));
260   }
261 };
262
263 //---- ConstPoolUInt::get() and ConstPoolSInt::get() implementations...
264 //
265 static ValueMap<uint64_t, ConstPoolInt> IntConstants;
266
267 ConstPoolSInt *ConstPoolSInt::get(const Type *Ty, int64_t V) {
268   ConstPoolSInt *Result = (ConstPoolSInt*)IntConstants.get(Ty, (uint64_t)V);
269   if (!Result)   // If no preexisting value, create one now...
270     IntConstants.add(Ty, V, Result = new ConstPoolSInt(Ty, V));
271   return Result;
272 }
273
274 ConstPoolUInt *ConstPoolUInt::get(const Type *Ty, uint64_t V) {
275   ConstPoolUInt *Result = (ConstPoolUInt*)IntConstants.get(Ty, V);
276   if (!Result)   // If no preexisting value, create one now...
277     IntConstants.add(Ty, V, Result = new ConstPoolUInt(Ty, V));
278   return Result;
279 }
280
281 ConstPoolInt *ConstPoolInt::get(const Type *Ty, unsigned char V) {
282   assert(V <= 127 && "Can only be used with very small positive constants!");
283   if (Ty->isSigned()) return ConstPoolSInt::get(Ty, V);
284   return ConstPoolUInt::get(Ty, V);
285 }
286
287 //---- ConstPoolFP::get() implementation...
288 //
289 static ValueMap<double, ConstPoolFP> FPConstants;
290
291 ConstPoolFP *ConstPoolFP::get(const Type *Ty, double V) {
292   ConstPoolFP *Result = FPConstants.get(Ty, V);
293   if (!Result)   // If no preexisting value, create one now...
294     FPConstants.add(Ty, V, Result = new ConstPoolFP(Ty, V));
295   return Result;
296 }
297
298 //---- ConstPoolArray::get() implementation...
299 //
300 static ValueMap<vector<ConstPoolVal*>, ConstPoolArray> ArrayConstants;
301
302 ConstPoolArray *ConstPoolArray::get(const ArrayType *Ty,
303                                     const vector<ConstPoolVal*> &V) {
304   ConstPoolArray *Result = ArrayConstants.get(Ty, V);
305   if (!Result)   // If no preexisting value, create one now...
306     ArrayConstants.add(Ty, V, Result = new ConstPoolArray(Ty, V));
307   return Result;
308 }
309
310 //---- ConstPoolStruct::get() implementation...
311 //
312 static ValueMap<vector<ConstPoolVal*>, ConstPoolStruct> StructConstants;
313
314 ConstPoolStruct *ConstPoolStruct::get(const StructType *Ty,
315                                       const vector<ConstPoolVal*> &V) {
316   ConstPoolStruct *Result = StructConstants.get(Ty, V);
317   if (!Result)   // If no preexisting value, create one now...
318     StructConstants.add(Ty, V, Result = new ConstPoolStruct(Ty, V));
319   return Result;
320 }