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