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; |
| 27 | |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 28 | namespace CodeGen { |
| 29 | |
| 30 | /// RValue - This trivial value class is used to represent the result of an |
| 31 | /// expression that is evaluated. It can be one of three things: either a |
| 32 | /// simple LLVM SSA value, a pair of SSA values for complex numbers, or the |
| 33 | /// address of an aggregate value in memory. |
| 34 | class RValue { |
| 35 | llvm::Value *V1, *V2; |
| 36 | // TODO: Encode this into the low bit of pointer for more efficient |
| 37 | // return-by-value. |
| 38 | enum { Scalar, Complex, Aggregate } Flavor; |
| 39 | |
| 40 | // FIXME: Aggregate rvalues need to retain information about whether they are |
| 41 | // volatile or not. |
| 42 | public: |
| 43 | |
| 44 | bool isScalar() const { return Flavor == Scalar; } |
| 45 | bool isComplex() const { return Flavor == Complex; } |
| 46 | bool isAggregate() const { return Flavor == Aggregate; } |
| 47 | |
| 48 | /// getScalar() - Return the Value* of this scalar value. |
| 49 | llvm::Value *getScalarVal() const { |
| 50 | assert(isScalar() && "Not a scalar!"); |
| 51 | return V1; |
| 52 | } |
| 53 | |
| 54 | /// getComplexVal - Return the real/imag components of this complex value. |
| 55 | /// |
| 56 | std::pair<llvm::Value *, llvm::Value *> getComplexVal() const { |
| 57 | return std::pair<llvm::Value *, llvm::Value *>(V1, V2); |
| 58 | } |
| 59 | |
| 60 | /// getAggregateAddr() - Return the Value* of the address of the aggregate. |
| 61 | llvm::Value *getAggregateAddr() const { |
| 62 | assert(isAggregate() && "Not an aggregate!"); |
| 63 | return V1; |
| 64 | } |
| 65 | |
| 66 | static RValue get(llvm::Value *V) { |
| 67 | RValue ER; |
| 68 | ER.V1 = V; |
| 69 | ER.Flavor = Scalar; |
| 70 | return ER; |
| 71 | } |
| 72 | static RValue getComplex(llvm::Value *V1, llvm::Value *V2) { |
| 73 | RValue ER; |
| 74 | ER.V1 = V1; |
| 75 | ER.V2 = V2; |
| 76 | ER.Flavor = Complex; |
| 77 | return ER; |
| 78 | } |
| 79 | static RValue getComplex(const std::pair<llvm::Value *, llvm::Value *> &C) { |
| 80 | RValue ER; |
| 81 | ER.V1 = C.first; |
| 82 | ER.V2 = C.second; |
| 83 | ER.Flavor = Complex; |
| 84 | return ER; |
| 85 | } |
| 86 | static RValue getAggregate(llvm::Value *V) { |
| 87 | RValue ER; |
| 88 | ER.V1 = V; |
| 89 | ER.Flavor = Aggregate; |
| 90 | return ER; |
| 91 | } |
| 92 | }; |
| 93 | |
| 94 | |
| 95 | /// LValue - This represents an lvalue references. Because C/C++ allow |
| 96 | /// bitfields, this is not a simple LLVM pointer, it may be a pointer plus a |
| 97 | /// bitrange. |
| 98 | class LValue { |
| 99 | // FIXME: alignment? |
| 100 | |
| 101 | enum { |
| 102 | Simple, // This is a normal l-value, use getAddress(). |
| 103 | VectorElt, // This is a vector element l-value (V[i]), use getVector* |
| 104 | BitField, // This is a bitfield l-value, use getBitfield*. |
Daniel Dunbar | 85c59ed | 2008-08-29 08:11:39 +0000 | [diff] [blame] | 105 | ExtVectorElt, // This is an extended vector subset, use getExtVectorComp |
| 106 | PropertyRef // This is an Objective-C property reference, use |
| 107 | // getPropertyRefExpr |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 108 | } LVType; |
| 109 | |
| 110 | llvm::Value *V; |
| 111 | |
| 112 | union { |
| 113 | // Index into a vector subscript: V[i] |
| 114 | llvm::Value *VectorIdx; |
| 115 | |
| 116 | // ExtVector element subset: V.xyx |
| 117 | llvm::Constant *VectorElts; |
| 118 | |
| 119 | // BitField start bit and size |
| 120 | struct { |
| 121 | unsigned short StartBit; |
| 122 | unsigned short Size; |
| 123 | bool IsSigned; |
| 124 | } BitfieldData; |
Daniel Dunbar | 85c59ed | 2008-08-29 08:11:39 +0000 | [diff] [blame] | 125 | |
| 126 | // Obj-C property reference expression |
| 127 | const ObjCPropertyRefExpr *PropertyRefExpr; |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 128 | }; |
| 129 | |
| 130 | bool Volatile:1; |
| 131 | // FIXME: set but never used, what effect should it have? |
| 132 | bool Restrict:1; |
Fariborz Jahanian | 6d657c4 | 2008-11-18 20:18:11 +0000 | [diff] [blame^] | 133 | |
| 134 | bool ObjcWeak:1; |
| 135 | bool ObjcStrong:1; |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 136 | |
| 137 | private: |
| 138 | static void SetQualifiers(unsigned Qualifiers, LValue& R) { |
| 139 | R.Volatile = (Qualifiers&QualType::Volatile)!=0; |
| 140 | R.Restrict = (Qualifiers&QualType::Restrict)!=0; |
| 141 | } |
Fariborz Jahanian | 6d657c4 | 2008-11-18 20:18:11 +0000 | [diff] [blame^] | 142 | |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 143 | public: |
| 144 | bool isSimple() const { return LVType == Simple; } |
| 145 | bool isVectorElt() const { return LVType == VectorElt; } |
| 146 | bool isBitfield() const { return LVType == BitField; } |
| 147 | bool isExtVectorElt() const { return LVType == ExtVectorElt; } |
Daniel Dunbar | 85c59ed | 2008-08-29 08:11:39 +0000 | [diff] [blame] | 148 | bool isPropertyRef() const { return LVType == PropertyRef; } |
| 149 | |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 150 | bool isVolatileQualified() const { return Volatile; } |
| 151 | bool isRestrictQualified() const { return Restrict; } |
Fariborz Jahanian | 6d657c4 | 2008-11-18 20:18:11 +0000 | [diff] [blame^] | 152 | |
| 153 | bool isObjcWeak() const { return ObjcWeak; } |
| 154 | bool isObjcStrong() const { return ObjcStrong; } |
| 155 | |
| 156 | static void SetObjCGCAttrs(unsigned Weak, unsigned Strong, LValue& R) { |
| 157 | R.ObjcWeak = Weak; |
| 158 | R.ObjcStrong = Strong; |
| 159 | } |
| 160 | |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 161 | // simple lvalue |
| 162 | llvm::Value *getAddress() const { assert(isSimple()); return V; } |
| 163 | // vector elt lvalue |
| 164 | llvm::Value *getVectorAddr() const { assert(isVectorElt()); return V; } |
| 165 | llvm::Value *getVectorIdx() const { assert(isVectorElt()); return VectorIdx; } |
| 166 | // extended vector elements. |
| 167 | llvm::Value *getExtVectorAddr() const { assert(isExtVectorElt()); return V; } |
| 168 | llvm::Constant *getExtVectorElts() const { |
| 169 | assert(isExtVectorElt()); |
| 170 | return VectorElts; |
| 171 | } |
| 172 | // bitfield lvalue |
| 173 | llvm::Value *getBitfieldAddr() const { assert(isBitfield()); return V; } |
| 174 | unsigned short getBitfieldStartBit() const { |
| 175 | assert(isBitfield()); |
| 176 | return BitfieldData.StartBit; |
| 177 | } |
| 178 | unsigned short getBitfieldSize() const { |
| 179 | assert(isBitfield()); |
| 180 | return BitfieldData.Size; |
| 181 | } |
| 182 | bool isBitfieldSigned() const { |
| 183 | assert(isBitfield()); |
| 184 | return BitfieldData.IsSigned; |
| 185 | } |
Daniel Dunbar | 85c59ed | 2008-08-29 08:11:39 +0000 | [diff] [blame] | 186 | // property ref lvalue |
| 187 | const ObjCPropertyRefExpr *getPropertyRefExpr() const { |
| 188 | assert(isPropertyRef()); |
| 189 | return PropertyRefExpr; |
| 190 | } |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 191 | |
| 192 | static LValue MakeAddr(llvm::Value *V, unsigned Qualifiers) { |
| 193 | LValue R; |
| 194 | R.LVType = Simple; |
| 195 | R.V = V; |
| 196 | SetQualifiers(Qualifiers,R); |
| 197 | return R; |
| 198 | } |
| 199 | |
| 200 | static LValue MakeVectorElt(llvm::Value *Vec, llvm::Value *Idx, |
| 201 | unsigned Qualifiers) { |
| 202 | LValue R; |
| 203 | R.LVType = VectorElt; |
| 204 | R.V = Vec; |
| 205 | R.VectorIdx = Idx; |
| 206 | SetQualifiers(Qualifiers,R); |
| 207 | return R; |
| 208 | } |
| 209 | |
| 210 | static LValue MakeExtVectorElt(llvm::Value *Vec, llvm::Constant *Elts, |
| 211 | unsigned Qualifiers) { |
| 212 | LValue R; |
| 213 | R.LVType = ExtVectorElt; |
| 214 | R.V = Vec; |
| 215 | R.VectorElts = Elts; |
| 216 | SetQualifiers(Qualifiers,R); |
| 217 | return R; |
| 218 | } |
| 219 | |
| 220 | static LValue MakeBitfield(llvm::Value *V, unsigned short StartBit, |
| 221 | unsigned short Size, bool IsSigned, |
| 222 | unsigned Qualifiers) { |
| 223 | LValue R; |
| 224 | R.LVType = BitField; |
| 225 | R.V = V; |
| 226 | R.BitfieldData.StartBit = StartBit; |
| 227 | R.BitfieldData.Size = Size; |
| 228 | R.BitfieldData.IsSigned = IsSigned; |
| 229 | SetQualifiers(Qualifiers,R); |
| 230 | return R; |
| 231 | } |
Daniel Dunbar | 85c59ed | 2008-08-29 08:11:39 +0000 | [diff] [blame] | 232 | |
Daniel Dunbar | f951719 | 2008-10-17 23:02:24 +0000 | [diff] [blame] | 233 | // FIXME: It is probably bad that we aren't emitting the target when |
| 234 | // we build the lvalue. However, this complicates the code a bit, |
| 235 | // 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] | 236 | static LValue MakePropertyRef(const ObjCPropertyRefExpr *E, |
| 237 | unsigned Qualifiers) { |
| 238 | LValue R; |
| 239 | R.LVType = PropertyRef; |
| 240 | R.PropertyRefExpr = E; |
| 241 | SetQualifiers(Qualifiers,R); |
| 242 | return R; |
| 243 | } |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 244 | }; |
| 245 | |
| 246 | } // end namespace CodeGen |
| 247 | } // end namespace clang |
| 248 | |
| 249 | #endif |