blob: 465ea769137cbe9aac025f5a6eab06df844675e1 [file] [log] [blame]
Daniel Dunbar2eecaab2008-08-23 03:10:25 +00001//===-- 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 Dunbar46f45b92008-09-09 01:06:48 +000020namespace llvm {
21 class Constant;
22 class Value;
23}
24
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000025namespace clang {
Daniel Dunbar85c59ed2008-08-29 08:11:39 +000026 class ObjCPropertyRefExpr;
Fariborz Jahanian09105f52009-08-20 17:02:02 +000027 class ObjCImplicitSetterGetterRefExpr;
Daniel Dunbar85c59ed2008-08-29 08:11:39 +000028
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000029namespace CodeGen {
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +000030 class CGBitFieldInfo;
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000031
32/// RValue - This trivial value class is used to represent the result of an
33/// expression that is evaluated. It can be one of three things: either a
34/// simple LLVM SSA value, a pair of SSA values for complex numbers, or the
35/// address of an aggregate value in memory.
36class RValue {
37 llvm::Value *V1, *V2;
38 // TODO: Encode this into the low bit of pointer for more efficient
39 // return-by-value.
40 enum { Scalar, Complex, Aggregate } Flavor;
Mike Stump1eb44332009-09-09 15:08:12 +000041
Mike Stump8b3d93a2009-05-23 20:21:36 +000042 bool Volatile:1;
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000043public:
Mike Stump1eb44332009-09-09 15:08:12 +000044
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000045 bool isScalar() const { return Flavor == Scalar; }
46 bool isComplex() const { return Flavor == Complex; }
47 bool isAggregate() const { return Flavor == Aggregate; }
Mike Stump1eb44332009-09-09 15:08:12 +000048
Mike Stump8b3d93a2009-05-23 20:21:36 +000049 bool isVolatileQualified() const { return Volatile; }
50
Mike Stump519202d2009-11-03 16:11:57 +000051 /// getScalarVal() - Return the Value* of this scalar value.
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000052 llvm::Value *getScalarVal() const {
53 assert(isScalar() && "Not a scalar!");
54 return V1;
55 }
56
57 /// getComplexVal - Return the real/imag components of this complex value.
58 ///
59 std::pair<llvm::Value *, llvm::Value *> getComplexVal() const {
60 return std::pair<llvm::Value *, llvm::Value *>(V1, V2);
61 }
Mike Stump1eb44332009-09-09 15:08:12 +000062
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000063 /// getAggregateAddr() - Return the Value* of the address of the aggregate.
64 llvm::Value *getAggregateAddr() const {
65 assert(isAggregate() && "Not an aggregate!");
66 return V1;
67 }
Mike Stump1eb44332009-09-09 15:08:12 +000068
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000069 static RValue get(llvm::Value *V) {
70 RValue ER;
71 ER.V1 = V;
72 ER.Flavor = Scalar;
Mike Stump6b735682009-05-28 00:16:27 +000073 ER.Volatile = false;
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000074 return ER;
75 }
76 static RValue getComplex(llvm::Value *V1, llvm::Value *V2) {
77 RValue ER;
78 ER.V1 = V1;
79 ER.V2 = V2;
80 ER.Flavor = Complex;
Mike Stump6b735682009-05-28 00:16:27 +000081 ER.Volatile = false;
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000082 return ER;
83 }
84 static RValue getComplex(const std::pair<llvm::Value *, llvm::Value *> &C) {
85 RValue ER;
86 ER.V1 = C.first;
87 ER.V2 = C.second;
88 ER.Flavor = Complex;
Mike Stump6b735682009-05-28 00:16:27 +000089 ER.Volatile = false;
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000090 return ER;
91 }
Mike Stump8b3d93a2009-05-23 20:21:36 +000092 // FIXME: Aggregate rvalues need to retain information about whether they are
93 // volatile or not. Remove default to find all places that probably get this
94 // wrong.
95 static RValue getAggregate(llvm::Value *V, bool Vol = false) {
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000096 RValue ER;
97 ER.V1 = V;
98 ER.Flavor = Aggregate;
Mike Stump8b3d93a2009-05-23 20:21:36 +000099 ER.Volatile = Vol;
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000100 return ER;
101 }
102};
103
104
105/// LValue - This represents an lvalue references. Because C/C++ allow
106/// bitfields, this is not a simple LLVM pointer, it may be a pointer plus a
107/// bitrange.
108class LValue {
109 // FIXME: alignment?
Mike Stump1eb44332009-09-09 15:08:12 +0000110
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000111 enum {
112 Simple, // This is a normal l-value, use getAddress().
113 VectorElt, // This is a vector element l-value (V[i]), use getVector*
114 BitField, // This is a bitfield l-value, use getBitfield*.
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000115 ExtVectorElt, // This is an extended vector subset, use getExtVectorComp
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000116 PropertyRef, // This is an Objective-C property reference, use
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000117 // getPropertyRefExpr
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000118 KVCRef // This is an objective-c 'implicit' property ref,
119 // use getKVCRefExpr
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000120 } LVType;
Fariborz Jahanian58626502008-11-19 00:59:10 +0000121
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000122 llvm::Value *V;
Mike Stump1eb44332009-09-09 15:08:12 +0000123
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000124 union {
125 // Index into a vector subscript: V[i]
126 llvm::Value *VectorIdx;
127
128 // ExtVector element subset: V.xyx
129 llvm::Constant *VectorElts;
Mike Stump1eb44332009-09-09 15:08:12 +0000130
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000131 // BitField start bit and size
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000132 const CGBitFieldInfo *BitFieldInfo;
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000133
134 // Obj-C property reference expression
135 const ObjCPropertyRefExpr *PropertyRefExpr;
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000136
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000137 // ObjC 'implicit' property reference expression
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000138 const ObjCImplicitSetterGetterRefExpr *KVCRefExpr;
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000139 };
140
John McCall0953e762009-09-24 19:53:00 +0000141 // 'const' is unused here
142 Qualifiers Quals;
Fariborz Jahanian58626502008-11-19 00:59:10 +0000143
Fariborz Jahaniand1cc8042008-11-20 20:53:20 +0000144 // objective-c's ivar
145 bool Ivar:1;
Fariborz Jahanian1c1afc42009-09-18 00:04:00 +0000146
147 // objective-c's ivar is an array
Fariborz Jahanianfd02ed72009-09-21 18:54:29 +0000148 bool ObjIsArray:1;
Mike Stump1eb44332009-09-09 15:08:12 +0000149
Fariborz Jahanian4f676ed2009-02-21 00:30:43 +0000150 // LValue is non-gc'able for any reason, including being a parameter or local
151 // variable.
152 bool NonGC: 1;
Fariborz Jahaniand1cc8042008-11-20 20:53:20 +0000153
Fariborz Jahanianbf63b872009-05-04 23:27:20 +0000154 // Lvalue is a global reference of an objective-c object
155 bool GlobalObjCRef : 1;
156
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000157 /// Is the bit-field value signed.
158 bool BitFieldIsSigned : 1;
159
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000160 Expr *BaseIvarExp;
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000161private:
John McCall0953e762009-09-24 19:53:00 +0000162 void SetQualifiers(Qualifiers Quals) {
163 this->Quals = Quals;
164
Mike Stumpf5408fe2009-05-16 07:57:57 +0000165 // FIXME: Convenient place to set objc flags to 0. This should really be
166 // done in a user-defined constructor instead.
John McCall0953e762009-09-24 19:53:00 +0000167 this->Ivar = this->ObjIsArray = this->NonGC = this->GlobalObjCRef = false;
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000168 this->BaseIvarExp = 0;
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000169 }
Mike Stump1eb44332009-09-09 15:08:12 +0000170
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000171public:
172 bool isSimple() const { return LVType == Simple; }
173 bool isVectorElt() const { return LVType == VectorElt; }
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000174 bool isBitField() const { return LVType == BitField; }
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000175 bool isExtVectorElt() const { return LVType == ExtVectorElt; }
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000176 bool isPropertyRef() const { return LVType == PropertyRef; }
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000177 bool isKVCRef() const { return LVType == KVCRef; }
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000178
John McCall0953e762009-09-24 19:53:00 +0000179 bool isVolatileQualified() const { return Quals.hasVolatile(); }
180 bool isRestrictQualified() const { return Quals.hasRestrict(); }
181 unsigned getVRQualifiers() const {
182 return Quals.getCVRQualifiers() & ~Qualifiers::Const;
Chris Lattner1bd885e2009-02-16 22:25:49 +0000183 }
Mike Stump1eb44332009-09-09 15:08:12 +0000184
Fariborz Jahaniand1cc8042008-11-20 20:53:20 +0000185 bool isObjCIvar() const { return Ivar; }
Fariborz Jahanianfd02ed72009-09-21 18:54:29 +0000186 bool isObjCArray() const { return ObjIsArray; }
Fariborz Jahanian4f676ed2009-02-21 00:30:43 +0000187 bool isNonGC () const { return NonGC; }
Fariborz Jahanianbf63b872009-05-04 23:27:20 +0000188 bool isGlobalObjCRef() const { return GlobalObjCRef; }
John McCall0953e762009-09-24 19:53:00 +0000189 bool isObjCWeak() const { return Quals.getObjCGCAttr() == Qualifiers::Weak; }
190 bool isObjCStrong() const { return Quals.getObjCGCAttr() == Qualifiers::Strong; }
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000191
192 Expr *getBaseIvarExp() const { return BaseIvarExp; }
193 void setBaseIvarExp(Expr *V) { BaseIvarExp = V; }
Mon P Wangc6a38a42009-07-22 03:08:17 +0000194
John McCall0953e762009-09-24 19:53:00 +0000195 unsigned getAddressSpace() const { return Quals.getAddressSpace(); }
Mon P Wangc6a38a42009-07-22 03:08:17 +0000196
Fariborz Jahanian2ab19682008-11-21 18:14:01 +0000197 static void SetObjCIvar(LValue& R, bool iValue) {
198 R.Ivar = iValue;
Fariborz Jahaniand1cc8042008-11-20 20:53:20 +0000199 }
Fariborz Jahanianfd02ed72009-09-21 18:54:29 +0000200 static void SetObjCArray(LValue& R, bool iValue) {
201 R.ObjIsArray = iValue;
Fariborz Jahanian1c1afc42009-09-18 00:04:00 +0000202 }
Fariborz Jahanianbf63b872009-05-04 23:27:20 +0000203 static void SetGlobalObjCRef(LValue& R, bool iValue) {
204 R.GlobalObjCRef = iValue;
205 }
Mike Stump1eb44332009-09-09 15:08:12 +0000206
Fariborz Jahanian4f676ed2009-02-21 00:30:43 +0000207 static void SetObjCNonGC(LValue& R, bool iValue) {
208 R.NonGC = iValue;
209 }
Mike Stump1eb44332009-09-09 15:08:12 +0000210
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000211 // simple lvalue
212 llvm::Value *getAddress() const { assert(isSimple()); return V; }
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000213
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000214 // vector elt lvalue
215 llvm::Value *getVectorAddr() const { assert(isVectorElt()); return V; }
216 llvm::Value *getVectorIdx() const { assert(isVectorElt()); return VectorIdx; }
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000217
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000218 // extended vector elements.
219 llvm::Value *getExtVectorAddr() const { assert(isExtVectorElt()); return V; }
220 llvm::Constant *getExtVectorElts() const {
221 assert(isExtVectorElt());
222 return VectorElts;
223 }
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000224
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000225 // bitfield lvalue
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000226 llvm::Value *getBitFieldAddr() const {
227 assert(isBitField());
228 return V;
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000229 }
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000230 const CGBitFieldInfo &getBitFieldInfo() const {
231 assert(isBitField());
232 return *BitFieldInfo;
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000233 }
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000234 bool isBitFieldSigned() const {
235 assert(isBitField());
236 return BitFieldIsSigned;
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000237 }
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000238
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000239 // property ref lvalue
240 const ObjCPropertyRefExpr *getPropertyRefExpr() const {
241 assert(isPropertyRef());
242 return PropertyRefExpr;
243 }
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000244
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000245 // 'implicit' property ref lvalue
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000246 const ObjCImplicitSetterGetterRefExpr *getKVCRefExpr() const {
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000247 assert(isKVCRef());
248 return KVCRefExpr;
249 }
250
John McCall0953e762009-09-24 19:53:00 +0000251 static LValue MakeAddr(llvm::Value *V, Qualifiers Quals) {
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000252 LValue R;
253 R.LVType = Simple;
254 R.V = V;
John McCall0953e762009-09-24 19:53:00 +0000255 R.SetQualifiers(Quals);
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000256 return R;
257 }
Mike Stump1eb44332009-09-09 15:08:12 +0000258
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000259 static LValue MakeVectorElt(llvm::Value *Vec, llvm::Value *Idx,
John McCall0953e762009-09-24 19:53:00 +0000260 unsigned CVR) {
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000261 LValue R;
262 R.LVType = VectorElt;
263 R.V = Vec;
264 R.VectorIdx = Idx;
John McCall0953e762009-09-24 19:53:00 +0000265 R.SetQualifiers(Qualifiers::fromCVRMask(CVR));
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000266 return R;
267 }
Mike Stump1eb44332009-09-09 15:08:12 +0000268
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000269 static LValue MakeExtVectorElt(llvm::Value *Vec, llvm::Constant *Elts,
John McCall0953e762009-09-24 19:53:00 +0000270 unsigned CVR) {
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000271 LValue R;
272 R.LVType = ExtVectorElt;
273 R.V = Vec;
274 R.VectorElts = Elts;
John McCall0953e762009-09-24 19:53:00 +0000275 R.SetQualifiers(Qualifiers::fromCVRMask(CVR));
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000276 return R;
277 }
278
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000279 static LValue MakeBitfield(llvm::Value *V, const CGBitFieldInfo &Info,
280 bool IsSigned, unsigned CVR) {
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000281 LValue R;
282 R.LVType = BitField;
283 R.V = V;
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000284 R.BitFieldInfo = &Info;
285 R.BitFieldIsSigned = IsSigned;
John McCall0953e762009-09-24 19:53:00 +0000286 R.SetQualifiers(Qualifiers::fromCVRMask(CVR));
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000287 return R;
288 }
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000289
Mike Stumpf5408fe2009-05-16 07:57:57 +0000290 // FIXME: It is probably bad that we aren't emitting the target when we build
291 // the lvalue. However, this complicates the code a bit, and I haven't figured
292 // out how to make it go wrong yet.
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000293 static LValue MakePropertyRef(const ObjCPropertyRefExpr *E,
John McCall0953e762009-09-24 19:53:00 +0000294 unsigned CVR) {
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000295 LValue R;
296 R.LVType = PropertyRef;
297 R.PropertyRefExpr = E;
John McCall0953e762009-09-24 19:53:00 +0000298 R.SetQualifiers(Qualifiers::fromCVRMask(CVR));
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000299 return R;
300 }
Mike Stump1eb44332009-09-09 15:08:12 +0000301
302 static LValue MakeKVCRef(const ObjCImplicitSetterGetterRefExpr *E,
John McCall0953e762009-09-24 19:53:00 +0000303 unsigned CVR) {
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000304 LValue R;
305 R.LVType = KVCRef;
306 R.KVCRefExpr = E;
John McCall0953e762009-09-24 19:53:00 +0000307 R.SetQualifiers(Qualifiers::fromCVRMask(CVR));
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000308 return R;
309 }
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000310};
311
312} // end namespace CodeGen
313} // end namespace clang
314
315#endif