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"
15 ConstPoolBool *ConstPoolBool::True = new ConstPoolBool(true);
16 ConstPoolBool *ConstPoolBool::False = new ConstPoolBool(false);
19 //===----------------------------------------------------------------------===//
21 //===----------------------------------------------------------------------===//
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!");
27 if (Name.size()) ST->insert(Name, this);
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);
37 case Type::LongTyID: return ConstPoolSInt::get(Ty, 0);
40 case Type::UShortTyID:
42 case Type::ULongTyID: return ConstPoolUInt::get(Ty, 0);
45 case Type::DoubleTyID: return ConstPoolFP::get(Ty, 0);
47 case Type::PointerTyID:
48 return ConstPoolPointer::getNullPointer(Ty->castPointerType());
56 //===----------------------------------------------------------------------===//
57 // ConstPoolXXX Classes
58 //===----------------------------------------------------------------------===//
60 //===----------------------------------------------------------------------===//
61 // Normal Constructors
63 ConstPoolBool::ConstPoolBool(bool V) : ConstPoolVal(Type::BoolTy) {
67 ConstPoolInt::ConstPoolInt(const Type *Ty, uint64_t V) : ConstPoolVal(Ty) {
71 ConstPoolSInt::ConstPoolSInt(const Type *Ty, int64_t V) : ConstPoolInt(Ty, V) {
72 assert(isValueValidForType(Ty, V) && "Value too large for type!");
75 ConstPoolUInt::ConstPoolUInt(const Type *Ty, uint64_t V) : ConstPoolInt(Ty, V) {
76 assert(isValueValidForType(Ty, V) && "Value too large for type!");
79 ConstPoolFP::ConstPoolFP(const Type *Ty, double V) : ConstPoolVal(Ty) {
80 assert(isValueValidForType(Ty, V) && "Value too large for type!");
84 ConstPoolArray::ConstPoolArray(const ArrayType *T,
85 const vector<ConstPoolVal*> &V)
87 for (unsigned i = 0; i < V.size(); i++) {
88 assert(V[i]->getType() == T->getElementType());
89 Operands.push_back(Use(V[i], this));
93 ConstPoolStruct::ConstPoolStruct(const StructType *T,
94 const vector<ConstPoolVal*> &V)
96 const StructType::ElementTypes &ETypes = T->getElementTypes();
98 for (unsigned i = 0; i < V.size(); i++) {
99 assert(V[i]->getType() == ETypes[i]);
100 Operands.push_back(Use(V[i], this));
104 ConstPoolPointer::ConstPoolPointer(const PointerType *T) : ConstPoolVal(T) {}
107 //===----------------------------------------------------------------------===//
108 // getStrValue implementations
110 string ConstPoolBool::getStrValue() const {
111 return Val ? "true" : "false";
114 string ConstPoolSInt::getStrValue() const {
115 return itostr(Val.Signed);
118 string ConstPoolUInt::getStrValue() const {
119 return utostr(Val.Unsigned);
122 string ConstPoolFP::getStrValue() const {
126 string ConstPoolArray::getStrValue() const {
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();
136 return Result + " ]";
139 string ConstPoolStruct::getStrValue() const {
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();
149 return Result + " }";
152 string ConstPoolPointer::getStrValue() const {
156 //===----------------------------------------------------------------------===//
157 // isValueValidForType implementations
159 bool ConstPoolSInt::isValueValidForType(const Type *Ty, int64_t Val) {
160 switch (Ty->getPrimitiveID()) {
162 return false; // These can't be represented as integers!!!
165 case Type::SByteTyID:
166 return (Val <= INT8_MAX && Val >= INT8_MIN);
167 case Type::ShortTyID:
168 return (Val <= INT16_MAX && Val >= INT16_MIN);
170 return (Val <= INT32_MAX && Val >= INT32_MIN);
172 return true; // This is the largest type...
178 bool ConstPoolUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
179 switch (Ty->getPrimitiveID()) {
181 return false; // These can't be represented as integers!!!
184 case Type::UByteTyID:
185 return (Val <= UINT8_MAX);
186 case Type::UShortTyID:
187 return (Val <= UINT16_MAX);
189 return (Val <= UINT32_MAX);
190 case Type::ULongTyID:
191 return true; // This is the largest type...
197 bool ConstPoolFP::isValueValidForType(const Type *Ty, double Val) {
198 switch (Ty->getPrimitiveID()) {
200 return false; // These can't be represented as floating point!
202 // TODO: Figure out how to test if a double can be cast to a float!
203 case Type::FloatTyID:
205 return (Val <= UINT8_MAX);
207 case Type::DoubleTyID:
208 return true; // This is the largest type...
212 //===----------------------------------------------------------------------===//
213 // Hash Function Implementations
215 unsigned ConstPoolSInt::hash(const Type *Ty, int64_t V) {
216 return unsigned(Ty->getPrimitiveID() ^ V);
219 unsigned ConstPoolUInt::hash(const Type *Ty, uint64_t V) {
220 return unsigned(Ty->getPrimitiveID() ^ V);
223 unsigned ConstPoolFP::hash(const Type *Ty, double V) {
224 return Ty->getPrimitiveID() ^ unsigned(V);
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);
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);
244 //===----------------------------------------------------------------------===//
245 // Factory Function Implementation
247 template<class ValType, class ConstPoolClass>
249 typedef pair<const Type*, ValType> ConstHashKey;
250 map<ConstHashKey, ConstPoolClass *> Map;
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;
258 inline void add(const Type *Ty, ValType V, ConstPoolClass *CP) {
259 Map.insert(make_pair(ConstHashKey(Ty, V), CP));
263 //---- ConstPoolUInt::get() and ConstPoolSInt::get() implementations...
265 static ValueMap<uint64_t, ConstPoolInt> IntConstants;
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));
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));
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);
287 //---- ConstPoolFP::get() implementation...
289 static ValueMap<double, ConstPoolFP> FPConstants;
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));
298 //---- ConstPoolArray::get() implementation...
300 static ValueMap<vector<ConstPoolVal*>, ConstPoolArray> ArrayConstants;
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));
310 //---- ConstPoolStruct::get() implementation...
312 static ValueMap<vector<ConstPoolVal*>, ConstPoolStruct> StructConstants;
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));