Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 1 | //===-- CGValue.h - LLVM CodeGen wrappers for llvm::Value* ------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // These classes implement wrappers around llvm::Value in order to |
| 11 | // fully represent the range of values for C L- and R- values. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #ifndef CLANG_CODEGEN_CGVALUE_H |
| 16 | #define CLANG_CODEGEN_CGVALUE_H |
| 17 | |
| 18 | #include "clang/AST/Type.h" |
| 19 | |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 20 | namespace llvm { |
| 21 | class Constant; |
| 22 | class Value; |
| 23 | } |
| 24 | |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 25 | namespace clang { |
Daniel Dunbar | 85c59ed | 2008-08-29 08:11:39 +0000 | [diff] [blame] | 26 | class ObjCPropertyRefExpr; |
Fariborz Jahanian | 43f4470 | 2008-11-22 22:30:21 +0000 | [diff] [blame] | 27 | class ObjCKVCRefExpr; |
Daniel Dunbar | 85c59ed | 2008-08-29 08:11:39 +0000 | [diff] [blame] | 28 | |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 29 | namespace CodeGen { |
| 30 | |
| 31 | /// RValue - This trivial value class is used to represent the result of an |
| 32 | /// expression that is evaluated. It can be one of three things: either a |
| 33 | /// simple LLVM SSA value, a pair of SSA values for complex numbers, or the |
| 34 | /// address of an aggregate value in memory. |
| 35 | class RValue { |
| 36 | llvm::Value *V1, *V2; |
| 37 | // TODO: Encode this into the low bit of pointer for more efficient |
| 38 | // return-by-value. |
| 39 | enum { Scalar, Complex, Aggregate } Flavor; |
| 40 | |
| 41 | // FIXME: Aggregate rvalues need to retain information about whether they are |
| 42 | // volatile or not. |
| 43 | public: |
| 44 | |
| 45 | bool isScalar() const { return Flavor == Scalar; } |
| 46 | bool isComplex() const { return Flavor == Complex; } |
| 47 | bool isAggregate() const { return Flavor == Aggregate; } |
| 48 | |
| 49 | /// getScalar() - Return the Value* of this scalar value. |
| 50 | llvm::Value *getScalarVal() const { |
| 51 | assert(isScalar() && "Not a scalar!"); |
| 52 | return V1; |
| 53 | } |
| 54 | |
| 55 | /// getComplexVal - Return the real/imag components of this complex value. |
| 56 | /// |
| 57 | std::pair<llvm::Value *, llvm::Value *> getComplexVal() const { |
| 58 | return std::pair<llvm::Value *, llvm::Value *>(V1, V2); |
| 59 | } |
| 60 | |
| 61 | /// getAggregateAddr() - Return the Value* of the address of the aggregate. |
| 62 | llvm::Value *getAggregateAddr() const { |
| 63 | assert(isAggregate() && "Not an aggregate!"); |
| 64 | return V1; |
| 65 | } |
| 66 | |
| 67 | static RValue get(llvm::Value *V) { |
| 68 | RValue ER; |
| 69 | ER.V1 = V; |
| 70 | ER.Flavor = Scalar; |
| 71 | return ER; |
| 72 | } |
| 73 | static RValue getComplex(llvm::Value *V1, llvm::Value *V2) { |
| 74 | RValue ER; |
| 75 | ER.V1 = V1; |
| 76 | ER.V2 = V2; |
| 77 | ER.Flavor = Complex; |
| 78 | return ER; |
| 79 | } |
| 80 | static RValue getComplex(const std::pair<llvm::Value *, llvm::Value *> &C) { |
| 81 | RValue ER; |
| 82 | ER.V1 = C.first; |
| 83 | ER.V2 = C.second; |
| 84 | ER.Flavor = Complex; |
| 85 | return ER; |
| 86 | } |
| 87 | static RValue getAggregate(llvm::Value *V) { |
| 88 | RValue ER; |
| 89 | ER.V1 = V; |
| 90 | ER.Flavor = Aggregate; |
| 91 | return ER; |
| 92 | } |
| 93 | }; |
| 94 | |
| 95 | |
| 96 | /// LValue - This represents an lvalue references. Because C/C++ allow |
| 97 | /// bitfields, this is not a simple LLVM pointer, it may be a pointer plus a |
| 98 | /// bitrange. |
| 99 | class LValue { |
| 100 | // FIXME: alignment? |
| 101 | |
| 102 | enum { |
| 103 | Simple, // This is a normal l-value, use getAddress(). |
| 104 | VectorElt, // This is a vector element l-value (V[i]), use getVector* |
| 105 | BitField, // This is a bitfield l-value, use getBitfield*. |
Daniel Dunbar | 85c59ed | 2008-08-29 08:11:39 +0000 | [diff] [blame] | 106 | ExtVectorElt, // This is an extended vector subset, use getExtVectorComp |
Fariborz Jahanian | 43f4470 | 2008-11-22 22:30:21 +0000 | [diff] [blame] | 107 | PropertyRef, // This is an Objective-C property reference, use |
Daniel Dunbar | 85c59ed | 2008-08-29 08:11:39 +0000 | [diff] [blame] | 108 | // getPropertyRefExpr |
Fariborz Jahanian | 43f4470 | 2008-11-22 22:30:21 +0000 | [diff] [blame] | 109 | KVCRef // This is an objective-c 'implicit' property ref, |
| 110 | // use getKVCRefExpr |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 111 | } LVType; |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 112 | |
| 113 | enum ObjCType { |
Nate Begeman | fea8685 | 2008-12-16 19:57:09 +0000 | [diff] [blame] | 114 | None = 0, // object with no gc attribute. |
| 115 | Weak, // __weak object expression |
| 116 | Strong // __strong object expression |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 117 | }; |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 118 | |
| 119 | llvm::Value *V; |
| 120 | |
| 121 | union { |
| 122 | // Index into a vector subscript: V[i] |
| 123 | llvm::Value *VectorIdx; |
| 124 | |
| 125 | // ExtVector element subset: V.xyx |
| 126 | llvm::Constant *VectorElts; |
| 127 | |
| 128 | // BitField start bit and size |
| 129 | struct { |
| 130 | unsigned short StartBit; |
| 131 | unsigned short Size; |
| 132 | bool IsSigned; |
| 133 | } BitfieldData; |
Daniel Dunbar | 85c59ed | 2008-08-29 08:11:39 +0000 | [diff] [blame] | 134 | |
| 135 | // Obj-C property reference expression |
| 136 | const ObjCPropertyRefExpr *PropertyRefExpr; |
Fariborz Jahanian | 43f4470 | 2008-11-22 22:30:21 +0000 | [diff] [blame] | 137 | // ObjC 'implicit' property reference expression |
| 138 | const ObjCKVCRefExpr *KVCRefExpr; |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 139 | }; |
| 140 | |
| 141 | bool Volatile:1; |
| 142 | // FIXME: set but never used, what effect should it have? |
| 143 | bool Restrict:1; |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 144 | |
Fariborz Jahanian | d1cc804 | 2008-11-20 20:53:20 +0000 | [diff] [blame] | 145 | // objective-c's ivar |
| 146 | bool Ivar:1; |
Fariborz Jahanian | 4f676ed | 2009-02-21 00:30:43 +0000 | [diff] [blame^] | 147 | |
| 148 | // LValue is non-gc'able for any reason, including being a parameter or local |
| 149 | // variable. |
| 150 | bool NonGC: 1; |
Fariborz Jahanian | d1cc804 | 2008-11-20 20:53:20 +0000 | [diff] [blame] | 151 | |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 152 | // objective-c's gc attributes |
| 153 | unsigned ObjCType : 2; |
Fariborz Jahanian | d1cc804 | 2008-11-20 20:53:20 +0000 | [diff] [blame] | 154 | |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 155 | |
| 156 | private: |
| 157 | static void SetQualifiers(unsigned Qualifiers, LValue& R) { |
| 158 | R.Volatile = (Qualifiers&QualType::Volatile)!=0; |
| 159 | R.Restrict = (Qualifiers&QualType::Restrict)!=0; |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 160 | // FIXME: Convenient place to set objc flags to 0. This |
| 161 | // should really be done in a user-defined constructor instead. |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 162 | R.ObjCType = None; |
Fariborz Jahanian | 4f676ed | 2009-02-21 00:30:43 +0000 | [diff] [blame^] | 163 | R.Ivar = R.NonGC = false; |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 164 | } |
Fariborz Jahanian | 6d657c4 | 2008-11-18 20:18:11 +0000 | [diff] [blame] | 165 | |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 166 | public: |
| 167 | bool isSimple() const { return LVType == Simple; } |
| 168 | bool isVectorElt() const { return LVType == VectorElt; } |
| 169 | bool isBitfield() const { return LVType == BitField; } |
| 170 | bool isExtVectorElt() const { return LVType == ExtVectorElt; } |
Daniel Dunbar | 85c59ed | 2008-08-29 08:11:39 +0000 | [diff] [blame] | 171 | bool isPropertyRef() const { return LVType == PropertyRef; } |
Fariborz Jahanian | 43f4470 | 2008-11-22 22:30:21 +0000 | [diff] [blame] | 172 | bool isKVCRef() const { return LVType == KVCRef; } |
Daniel Dunbar | 85c59ed | 2008-08-29 08:11:39 +0000 | [diff] [blame] | 173 | |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 174 | bool isVolatileQualified() const { return Volatile; } |
| 175 | bool isRestrictQualified() const { return Restrict; } |
Chris Lattner | 1bd885e | 2009-02-16 22:25:49 +0000 | [diff] [blame] | 176 | unsigned getQualifiers() const { |
| 177 | return (Volatile ? QualType::Volatile : 0) | |
| 178 | (Restrict ? QualType::Restrict : 0); |
| 179 | } |
Fariborz Jahanian | 6d657c4 | 2008-11-18 20:18:11 +0000 | [diff] [blame] | 180 | |
Fariborz Jahanian | d1cc804 | 2008-11-20 20:53:20 +0000 | [diff] [blame] | 181 | bool isObjCIvar() const { return Ivar; } |
Fariborz Jahanian | 4f676ed | 2009-02-21 00:30:43 +0000 | [diff] [blame^] | 182 | bool isNonGC () const { return NonGC; } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 183 | bool isObjCWeak() const { return ObjCType == Weak; } |
| 184 | bool isObjCStrong() const { return ObjCType == Strong; } |
Fariborz Jahanian | 6d657c4 | 2008-11-18 20:18:11 +0000 | [diff] [blame] | 185 | |
Fariborz Jahanian | 2ab1968 | 2008-11-21 18:14:01 +0000 | [diff] [blame] | 186 | static void SetObjCIvar(LValue& R, bool iValue) { |
| 187 | R.Ivar = iValue; |
Fariborz Jahanian | d1cc804 | 2008-11-20 20:53:20 +0000 | [diff] [blame] | 188 | } |
Fariborz Jahanian | 4f676ed | 2009-02-21 00:30:43 +0000 | [diff] [blame^] | 189 | |
| 190 | static void SetObjCNonGC(LValue& R, bool iValue) { |
| 191 | R.NonGC = iValue; |
| 192 | } |
Fariborz Jahanian | c1debf3 | 2009-02-19 00:48:05 +0000 | [diff] [blame] | 193 | static void SetObjCType(QualType::GCAttrTypes GCAttrs, LValue& R) { |
| 194 | if (GCAttrs == QualType::Weak) |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 195 | R.ObjCType = Weak; |
Fariborz Jahanian | c1debf3 | 2009-02-19 00:48:05 +0000 | [diff] [blame] | 196 | else if (GCAttrs == QualType::Strong) |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 197 | R.ObjCType = Strong; |
Fariborz Jahanian | 4f676ed | 2009-02-21 00:30:43 +0000 | [diff] [blame^] | 198 | else |
| 199 | R.ObjCType = None; |
Fariborz Jahanian | 6d657c4 | 2008-11-18 20:18:11 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 202 | // simple lvalue |
| 203 | llvm::Value *getAddress() const { assert(isSimple()); return V; } |
| 204 | // vector elt lvalue |
| 205 | llvm::Value *getVectorAddr() const { assert(isVectorElt()); return V; } |
| 206 | llvm::Value *getVectorIdx() const { assert(isVectorElt()); return VectorIdx; } |
| 207 | // extended vector elements. |
| 208 | llvm::Value *getExtVectorAddr() const { assert(isExtVectorElt()); return V; } |
| 209 | llvm::Constant *getExtVectorElts() const { |
| 210 | assert(isExtVectorElt()); |
| 211 | return VectorElts; |
| 212 | } |
| 213 | // bitfield lvalue |
| 214 | llvm::Value *getBitfieldAddr() const { assert(isBitfield()); return V; } |
| 215 | unsigned short getBitfieldStartBit() const { |
| 216 | assert(isBitfield()); |
| 217 | return BitfieldData.StartBit; |
| 218 | } |
| 219 | unsigned short getBitfieldSize() const { |
| 220 | assert(isBitfield()); |
| 221 | return BitfieldData.Size; |
| 222 | } |
| 223 | bool isBitfieldSigned() const { |
| 224 | assert(isBitfield()); |
| 225 | return BitfieldData.IsSigned; |
| 226 | } |
Daniel Dunbar | 85c59ed | 2008-08-29 08:11:39 +0000 | [diff] [blame] | 227 | // property ref lvalue |
| 228 | const ObjCPropertyRefExpr *getPropertyRefExpr() const { |
| 229 | assert(isPropertyRef()); |
| 230 | return PropertyRefExpr; |
| 231 | } |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 232 | |
Fariborz Jahanian | 43f4470 | 2008-11-22 22:30:21 +0000 | [diff] [blame] | 233 | // 'implicit' property ref lvalue |
| 234 | const ObjCKVCRefExpr *getKVCRefExpr() const { |
| 235 | assert(isKVCRef()); |
| 236 | return KVCRefExpr; |
| 237 | } |
| 238 | |
Fariborz Jahanian | c1debf3 | 2009-02-19 00:48:05 +0000 | [diff] [blame] | 239 | static LValue MakeAddr(llvm::Value *V, unsigned Qualifiers, |
| 240 | QualType::GCAttrTypes GCAttrs = QualType::GCNone) { |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 241 | LValue R; |
| 242 | R.LVType = Simple; |
| 243 | R.V = V; |
| 244 | SetQualifiers(Qualifiers,R); |
Fariborz Jahanian | c1debf3 | 2009-02-19 00:48:05 +0000 | [diff] [blame] | 245 | SetObjCType(GCAttrs, R); |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 246 | return R; |
| 247 | } |
| 248 | |
| 249 | static LValue MakeVectorElt(llvm::Value *Vec, llvm::Value *Idx, |
| 250 | unsigned Qualifiers) { |
| 251 | LValue R; |
| 252 | R.LVType = VectorElt; |
| 253 | R.V = Vec; |
| 254 | R.VectorIdx = Idx; |
| 255 | SetQualifiers(Qualifiers,R); |
| 256 | return R; |
| 257 | } |
| 258 | |
| 259 | static LValue MakeExtVectorElt(llvm::Value *Vec, llvm::Constant *Elts, |
| 260 | unsigned Qualifiers) { |
| 261 | LValue R; |
| 262 | R.LVType = ExtVectorElt; |
| 263 | R.V = Vec; |
| 264 | R.VectorElts = Elts; |
| 265 | SetQualifiers(Qualifiers,R); |
| 266 | return R; |
| 267 | } |
| 268 | |
| 269 | static LValue MakeBitfield(llvm::Value *V, unsigned short StartBit, |
| 270 | unsigned short Size, bool IsSigned, |
| 271 | unsigned Qualifiers) { |
| 272 | LValue R; |
| 273 | R.LVType = BitField; |
| 274 | R.V = V; |
| 275 | R.BitfieldData.StartBit = StartBit; |
| 276 | R.BitfieldData.Size = Size; |
| 277 | R.BitfieldData.IsSigned = IsSigned; |
| 278 | SetQualifiers(Qualifiers,R); |
| 279 | return R; |
| 280 | } |
Daniel Dunbar | 85c59ed | 2008-08-29 08:11:39 +0000 | [diff] [blame] | 281 | |
Daniel Dunbar | f951719 | 2008-10-17 23:02:24 +0000 | [diff] [blame] | 282 | // FIXME: It is probably bad that we aren't emitting the target when |
| 283 | // we build the lvalue. However, this complicates the code a bit, |
| 284 | // and I haven't figured out how to make it go wrong yet. |
Daniel Dunbar | 85c59ed | 2008-08-29 08:11:39 +0000 | [diff] [blame] | 285 | static LValue MakePropertyRef(const ObjCPropertyRefExpr *E, |
| 286 | unsigned Qualifiers) { |
| 287 | LValue R; |
| 288 | R.LVType = PropertyRef; |
| 289 | R.PropertyRefExpr = E; |
| 290 | SetQualifiers(Qualifiers,R); |
| 291 | return R; |
| 292 | } |
Chris Lattner | 1bd885e | 2009-02-16 22:25:49 +0000 | [diff] [blame] | 293 | |
| 294 | static LValue MakeKVCRef(const ObjCKVCRefExpr *E, unsigned Qualifiers) { |
Fariborz Jahanian | 43f4470 | 2008-11-22 22:30:21 +0000 | [diff] [blame] | 295 | LValue R; |
| 296 | R.LVType = KVCRef; |
| 297 | R.KVCRefExpr = E; |
| 298 | SetQualifiers(Qualifiers,R); |
| 299 | return R; |
| 300 | } |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 301 | }; |
| 302 | |
| 303 | } // end namespace CodeGen |
| 304 | } // end namespace clang |
| 305 | |
| 306 | #endif |