Factor parentness out of Module & GlobalVariable into GlobalValue
[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 "llvm/GlobalVariable.h"   // TODO make this GlobalValue.h
13 #include <algorithm>
14 #include <assert.h>
15
16 ConstPoolBool *ConstPoolBool::True  = new ConstPoolBool(true);
17 ConstPoolBool *ConstPoolBool::False = new ConstPoolBool(false);
18
19
20 //===----------------------------------------------------------------------===//
21 //                              ConstPoolVal Class
22 //===----------------------------------------------------------------------===//
23
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!");
27
28   if (Name.size()) ST->insert(Name, this);
29 }
30
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);
35   case Type::SByteTyID:
36   case Type::ShortTyID:
37   case Type::IntTyID:
38   case Type::LongTyID:   return ConstPoolSInt::get(Ty, 0);
39
40   case Type::UByteTyID:
41   case Type::UShortTyID:
42   case Type::UIntTyID:
43   case Type::ULongTyID:  return ConstPoolUInt::get(Ty, 0);
44
45   case Type::FloatTyID:
46   case Type::DoubleTyID: return ConstPoolFP::get(Ty, 0);
47
48   case Type::PointerTyID: 
49     return ConstPoolPointer::getNull(cast<PointerType>(Ty));
50   default:
51     return 0;
52   }
53 }
54
55 bool ConstPoolInt::classof(const ConstPoolVal *CPV) {
56   return CPV->getType()->isIntegral();
57 }
58
59 //===----------------------------------------------------------------------===//
60 //                            ConstPoolXXX Classes
61 //===----------------------------------------------------------------------===//
62
63 //===----------------------------------------------------------------------===//
64 //                             Normal Constructors
65
66 ConstPoolBool::ConstPoolBool(bool V) : ConstPoolVal(Type::BoolTy) {
67   Val = V;
68 }
69
70 ConstPoolInt::ConstPoolInt(const Type *Ty, uint64_t V) : ConstPoolVal(Ty) {
71   Val.Unsigned = V;
72 }
73
74 ConstPoolSInt::ConstPoolSInt(const Type *Ty, int64_t V) : ConstPoolInt(Ty, V) {
75   assert(isValueValidForType(Ty, V) && "Value too large for type!");
76 }
77
78 ConstPoolUInt::ConstPoolUInt(const Type *Ty, uint64_t V) : ConstPoolInt(Ty, V) {
79   assert(isValueValidForType(Ty, V) && "Value too large for type!");
80 }
81
82 ConstPoolFP::ConstPoolFP(const Type *Ty, double V) : ConstPoolVal(Ty) {
83   assert(isValueValidForType(Ty, V) && "Value too large for type!");
84   Val = V;
85 }
86
87 ConstPoolArray::ConstPoolArray(const ArrayType *T,
88                                const vector<ConstPoolVal*> &V)
89   : ConstPoolVal(T) {
90   for (unsigned i = 0; i < V.size(); i++) {
91     assert(V[i]->getType() == T->getElementType());
92     Operands.push_back(Use(V[i], this));
93   }
94 }
95
96 ConstPoolStruct::ConstPoolStruct(const StructType *T,
97                                  const vector<ConstPoolVal*> &V)
98   : ConstPoolVal(T) {
99   const StructType::ElementTypes &ETypes = T->getElementTypes();
100   
101   for (unsigned i = 0; i < V.size(); i++) {
102     assert(V[i]->getType() == ETypes[i]);
103     Operands.push_back(Use(V[i], this));
104   }
105 }
106
107 ConstPoolPointer::ConstPoolPointer(const PointerType *T) : ConstPoolVal(T) {}
108
109 ConstPoolPointerReference::ConstPoolPointerReference(GlobalValue *GV)
110   : ConstPoolPointer(GV->getType()) {
111   Operands.push_back(Use(GV, this));
112 }
113
114
115
116 //===----------------------------------------------------------------------===//
117 //                          getStrValue implementations
118
119 string ConstPoolBool::getStrValue() const {
120   return Val ? "true" : "false";
121 }
122
123 string ConstPoolSInt::getStrValue() const {
124   return itostr(Val.Signed);
125 }
126
127 string ConstPoolUInt::getStrValue() const {
128   return utostr(Val.Unsigned);
129 }
130
131 string ConstPoolFP::getStrValue() const {
132   return ftostr(Val);
133 }
134
135 string ConstPoolArray::getStrValue() const {
136   string Result = "[";
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();
143   }
144
145   return Result + " ]";
146 }
147
148 string ConstPoolStruct::getStrValue() const {
149   string Result = "{";
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();
156   }
157
158   return Result + " }";
159 }
160
161 string ConstPoolPointer::getStrValue() const {
162   return "null";
163 }
164
165 string ConstPoolPointerReference::getStrValue() const {
166   return "<pointer reference>";
167 }
168
169 //===----------------------------------------------------------------------===//
170 //                      isValueValidForType implementations
171
172 bool ConstPoolSInt::isValueValidForType(const Type *Ty, int64_t Val) {
173   switch (Ty->getPrimitiveID()) {
174   default:
175     return false;         // These can't be represented as integers!!!
176
177     // Signed types...
178   case Type::SByteTyID:
179     return (Val <= INT8_MAX && Val >= INT8_MIN);
180   case Type::ShortTyID:
181     return (Val <= INT16_MAX && Val >= INT16_MIN);
182   case Type::IntTyID:
183     return (Val <= INT32_MAX && Val >= INT32_MIN);
184   case Type::LongTyID:
185     return true;          // This is the largest type...
186   }
187   assert(0 && "WTF?");
188   return false;
189 }
190
191 bool ConstPoolUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
192   switch (Ty->getPrimitiveID()) {
193   default:
194     return false;         // These can't be represented as integers!!!
195
196     // Unsigned types...
197   case Type::UByteTyID:
198     return (Val <= UINT8_MAX);
199   case Type::UShortTyID:
200     return (Val <= UINT16_MAX);
201   case Type::UIntTyID:
202     return (Val <= UINT32_MAX);
203   case Type::ULongTyID:
204     return true;          // This is the largest type...
205   }
206   assert(0 && "WTF?");
207   return false;
208 }
209
210 bool ConstPoolFP::isValueValidForType(const Type *Ty, double Val) {
211   switch (Ty->getPrimitiveID()) {
212   default:
213     return false;         // These can't be represented as floating point!
214
215     // TODO: Figure out how to test if a double can be cast to a float!
216   case Type::FloatTyID:
217     /*
218     return (Val <= UINT8_MAX);
219     */
220   case Type::DoubleTyID:
221     return true;          // This is the largest type...
222   }
223 };
224
225 //===----------------------------------------------------------------------===//
226 //                      Hash Function Implementations
227 #if 0
228 unsigned ConstPoolSInt::hash(const Type *Ty, int64_t V) {
229   return unsigned(Ty->getPrimitiveID() ^ V);
230 }
231
232 unsigned ConstPoolUInt::hash(const Type *Ty, uint64_t V) {
233   return unsigned(Ty->getPrimitiveID() ^ V);
234 }
235
236 unsigned ConstPoolFP::hash(const Type *Ty, double V) {
237   return Ty->getPrimitiveID() ^ unsigned(V);
238 }
239
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);
245   return Result;
246 }
247
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);
253   return Result;
254 }
255 #endif
256
257 //===----------------------------------------------------------------------===//
258 //                      Factory Function Implementation
259
260 template<class ValType, class ConstPoolClass>
261 struct ValueMap {
262   typedef pair<const Type*, ValType> ConstHashKey;
263   map<ConstHashKey, ConstPoolClass *> Map;
264
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;
269   }
270
271   inline void add(const Type *Ty, ValType V, ConstPoolClass *CP) {
272     Map.insert(make_pair(ConstHashKey(Ty, V), CP));
273   }
274 };
275
276 //---- ConstPoolUInt::get() and ConstPoolSInt::get() implementations...
277 //
278 static ValueMap<uint64_t, ConstPoolInt> IntConstants;
279
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));
284   return Result;
285 }
286
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));
291   return Result;
292 }
293
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);
298 }
299
300 //---- ConstPoolFP::get() implementation...
301 //
302 static ValueMap<double, ConstPoolFP> FPConstants;
303
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));
308   return Result;
309 }
310
311 //---- ConstPoolArray::get() implementation...
312 //
313 static ValueMap<vector<ConstPoolVal*>, ConstPoolArray> ArrayConstants;
314
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));
320   return Result;
321 }
322
323 //---- ConstPoolStruct::get() implementation...
324 //
325 static ValueMap<vector<ConstPoolVal*>, ConstPoolStruct> StructConstants;
326
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));
332   return Result;
333 }
334
335 //---- ConstPoolPointer::get() implementation...
336 //
337 static ValueMap<char, ConstPoolPointer> NullPtrConstants;
338
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));
343   return Result;
344 }
345
346 //---- ConstPoolPointerReference::get() implementation...
347 //
348 ConstPoolPointerReference *ConstPoolPointerReference::get(GlobalValue *GV) {
349   assert(GV->getParent());
350   // FIXME: These should all be shared!
351   return new ConstPoolPointerReference(GV);
352 }