a2a5d8a7ff1adbac1f6b8ad4e0faa310f8a8ecf9
[oota-llvm.git] / include / llvm / ConstPoolVals.h
1 //===-- llvm/ConstPoolVals.h - Constant Value nodes --------------*- C++ -*--=//
2 //
3 // This file contains the declarations for the ConstPoolVal class and all of
4 // its subclasses, which represent the different type of constant pool values
5 //
6 //===----------------------------------------------------------------------===//
7
8 #ifndef LLVM_CONSTPOOLVALS_H
9 #define LLVM_CONSTPOOLVALS_H
10
11 #include "llvm/User.h"
12 #include "llvm/SymTabValue.h"
13 #include "llvm/Support/DataTypes.h"
14 #include <vector>
15
16 class ArrayType;
17 class StructType;
18
19 //===----------------------------------------------------------------------===//
20 //                            ConstPoolVal Class
21 //===----------------------------------------------------------------------===//
22
23 class ConstPoolVal : public User {
24   SymTabValue *Parent;
25
26   friend class ValueHolder<ConstPoolVal, SymTabValue, SymTabValue>;
27   inline void setParent(SymTabValue *parent) { 
28     Parent = parent;
29   }
30
31 protected:
32   inline ConstPoolVal(const Type *Ty, const string &Name = "") 
33     : User(Ty, Value::ConstantVal, Name) { Parent = 0; }
34
35 public:
36   // Specialize setName to handle symbol table majik...
37   virtual void setName(const string &name);
38
39   // Static constructor to create a '0' constant of arbitrary type...
40   static ConstPoolVal *getNullConstant(const Type *Ty);
41
42   // clone() - Create a copy of 'this' value that is identical in all ways
43   // except the following:
44   //   * The value has no parent
45   //   * The value has no name
46   //
47   virtual ConstPoolVal *clone() const = 0;
48
49   virtual string getStrValue() const = 0;
50   virtual bool equals(const ConstPoolVal *V) const = 0;
51
52   inline const SymTabValue *getParent() const { return Parent; }
53   inline       SymTabValue *getParent()       { return Parent; }
54   inline const     Value *getParentV() const { return Parent->getSTVParent(); }
55   inline           Value *getParentV()       { return Parent->getSTVParent(); }
56 };
57
58
59
60 //===----------------------------------------------------------------------===//
61 //              Classes to represent constant pool variable defs
62 //===----------------------------------------------------------------------===//
63
64 //===---------------------------------------------------------------------------
65 // ConstPoolBool - Boolean Values
66 //
67 class ConstPoolBool : public ConstPoolVal {
68   bool Val;
69   ConstPoolBool(const ConstPoolBool &CP);
70 public:
71   ConstPoolBool(const Type *Ty, bool V, const string &Name = "");
72   ConstPoolBool(bool V, const string &Name = "");
73
74   virtual string getStrValue() const;
75   virtual bool equals(const ConstPoolVal *V) const;
76
77   virtual ConstPoolVal *clone() const { return new ConstPoolBool(*this); }
78
79   inline bool getValue() const { return Val; }
80
81   // setValue - Be careful... if there is more than one 'use' of this node, then
82   // they will ALL see the value that you set...
83   //
84   inline void setValue(bool v) { Val = v; } 
85 };
86
87
88 //===---------------------------------------------------------------------------
89 // ConstPoolInt - Superclass of ConstPoolSInt & ConstPoolUInt, to make dealing
90 // with integral constants easier.
91 //
92 class ConstPoolInt : public ConstPoolVal {
93 protected:
94   union {
95     int64_t  Signed;
96     uint64_t Unsigned;
97   } Val;
98   ConstPoolInt(const ConstPoolInt &CP);
99 public:
100   ConstPoolInt(const Type *Ty,  int64_t V, const string &Name = "");
101   ConstPoolInt(const Type *Ty, uint64_t V, const string &Name = "");
102
103   virtual bool equals(const ConstPoolVal *V) const;
104
105   // equalsInt - Provide a helper method that can be used to determine if the 
106   // constant contained within is equal to a constant.  This only works for very
107   // small values, because this is all that can be represented with all types.
108   //
109   bool equalsInt(unsigned char V) const {
110     assert(V <= 127 && "equals: Can only be used with very small constants!");
111     return Val.Unsigned == V;
112   }
113
114   // ConstPoolInt::get static method: return a constant pool int with the
115   // specified value.  as above, we work only with very small values here.
116   //
117   static ConstPoolInt *get(const Type *Ty, unsigned char V);
118 };
119
120
121 //===---------------------------------------------------------------------------
122 // ConstPoolSInt - Signed Integer Values [sbyte, short, int, long]
123 //
124 class ConstPoolSInt : public ConstPoolInt {
125   ConstPoolSInt(const ConstPoolSInt &CP) : ConstPoolInt(CP) {}
126 public:
127   ConstPoolSInt(const Type *Ty, int64_t V, const string &Name = "");
128
129   virtual ConstPoolVal *clone() const { return new ConstPoolSInt(*this); }
130
131   virtual string getStrValue() const;
132
133   static bool isValueValidForType(const Type *Ty, int64_t V);
134   inline int64_t getValue() const { return Val.Signed; }
135 };
136
137
138 //===---------------------------------------------------------------------------
139 // ConstPoolUInt - Unsigned Integer Values [ubyte, ushort, uint, ulong]
140 //
141 class ConstPoolUInt : public ConstPoolInt {
142   ConstPoolUInt(const ConstPoolUInt &CP) : ConstPoolInt(CP) {}
143 public:
144   ConstPoolUInt(const Type *Ty, uint64_t V, const string &Name = "");
145
146   virtual ConstPoolVal *clone() const { return new ConstPoolUInt(*this); }
147
148   virtual string getStrValue() const;
149
150   static bool isValueValidForType(const Type *Ty, uint64_t V);
151   inline uint64_t getValue() const { return Val.Unsigned; }
152 };
153
154
155 //===---------------------------------------------------------------------------
156 // ConstPoolFP - Floating Point Values [float, double]
157 //
158 class ConstPoolFP : public ConstPoolVal {
159   double Val;
160   ConstPoolFP(const ConstPoolFP &CP);
161 public:
162   ConstPoolFP(const Type *Ty, double V, const string &Name = "");
163
164   virtual ConstPoolVal *clone() const { return new ConstPoolFP(*this); }
165   virtual string getStrValue() const;
166   virtual bool equals(const ConstPoolVal *V) const;
167
168   static bool isValueValidForType(const Type *Ty, double V);
169   inline double getValue() const { return Val; }
170 };
171
172
173 //===---------------------------------------------------------------------------
174 // ConstPoolType - Type Declarations
175 //
176 class ConstPoolType : public ConstPoolVal {
177   const Type *Val;
178   ConstPoolType(const ConstPoolType &CPT);
179 public:
180   ConstPoolType(const Type *V, const string &Name = "");
181
182   virtual ConstPoolVal *clone() const { return new ConstPoolType(*this); }
183   virtual string getStrValue() const;
184   virtual bool equals(const ConstPoolVal *V) const;
185
186   inline const Type *getValue() const { return Val; }
187 };
188
189
190 //===---------------------------------------------------------------------------
191 // ConstPoolArray - Constant Array Declarations
192 //
193 class ConstPoolArray : public ConstPoolVal {
194   ConstPoolArray(const ConstPoolArray &CPT);
195 public:
196   ConstPoolArray(const ArrayType *T, vector<ConstPoolVal*> &V, 
197                  const string &Name = "");
198
199   virtual ConstPoolVal *clone() const { return new ConstPoolArray(*this); }
200   virtual string getStrValue() const;
201   virtual bool equals(const ConstPoolVal *V) const;
202
203   inline const vector<Use> &getValues() const { return Operands; }
204 };
205
206
207 //===---------------------------------------------------------------------------
208 // ConstPoolStruct - Constant Struct Declarations
209 //
210 class ConstPoolStruct : public ConstPoolVal {
211   ConstPoolStruct(const ConstPoolStruct &CPT);
212 public:
213   ConstPoolStruct(const StructType *T, vector<ConstPoolVal*> &V, 
214                   const string &Name = "");
215
216   virtual ConstPoolVal *clone() const { return new ConstPoolStruct(*this); }
217   virtual string getStrValue() const;
218   virtual bool equals(const ConstPoolVal *V) const;
219
220   inline const vector<Use> &getValues() const { return Operands; }
221 };
222
223 //===---------------------------------------------------------------------------
224 // External functions
225 //
226
227 // Convenience functions to get the value of an integer constant, for an
228 // appropriate integer or non-integer type that can be held in an integer.
229 // The type of the argument must be the following:
230 //   GetSignedIntConstantValue:   signed integer or bool
231 //   GetUnsignedIntConstantValue: unsigned integer, bool, or pointer
232 //   GetConstantValueAsSignedInt: any of the above, but the value
233 //                                must fit into a int64_t.
234 // 
235 // isValidConstant is set to true if a valid constant was found.
236 // 
237 int64_t  GetSignedIntConstantValue      (const Value* val, bool& isValidConst);
238 uint64_t GetUnsignedIntConstantValue    (const Value* val, bool& isValidConst);
239 int64_t  GetConstantValueAsSignedInt    (const Value* val, bool& isValidConst);
240
241 //===---------------------------------------------------------------------------
242
243 #endif