blob: ee9dc524c6ec8c3d814e7ec0a3b6e4d1a1e11266 [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 {
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.
35class 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;
Mike Stump1eb44332009-09-09 15:08:12 +000040
Mike Stump8b3d93a2009-05-23 20:21:36 +000041 bool Volatile:1;
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000042public:
Mike Stump1eb44332009-09-09 15:08:12 +000043
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000044 bool isScalar() const { return Flavor == Scalar; }
45 bool isComplex() const { return Flavor == Complex; }
46 bool isAggregate() const { return Flavor == Aggregate; }
Mike Stump1eb44332009-09-09 15:08:12 +000047
Mike Stump8b3d93a2009-05-23 20:21:36 +000048 bool isVolatileQualified() const { return Volatile; }
49
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000050 /// getScalar() - Return the Value* of this scalar value.
51 llvm::Value *getScalarVal() const {
52 assert(isScalar() && "Not a scalar!");
53 return V1;
54 }
55
56 /// getComplexVal - Return the real/imag components of this complex value.
57 ///
58 std::pair<llvm::Value *, llvm::Value *> getComplexVal() const {
59 return std::pair<llvm::Value *, llvm::Value *>(V1, V2);
60 }
Mike Stump1eb44332009-09-09 15:08:12 +000061
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000062 /// getAggregateAddr() - Return the Value* of the address of the aggregate.
63 llvm::Value *getAggregateAddr() const {
64 assert(isAggregate() && "Not an aggregate!");
65 return V1;
66 }
Mike Stump1eb44332009-09-09 15:08:12 +000067
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000068 static RValue get(llvm::Value *V) {
69 RValue ER;
70 ER.V1 = V;
71 ER.Flavor = Scalar;
Mike Stump6b735682009-05-28 00:16:27 +000072 ER.Volatile = false;
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000073 return ER;
74 }
75 static RValue getComplex(llvm::Value *V1, llvm::Value *V2) {
76 RValue ER;
77 ER.V1 = V1;
78 ER.V2 = V2;
79 ER.Flavor = Complex;
Mike Stump6b735682009-05-28 00:16:27 +000080 ER.Volatile = false;
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000081 return ER;
82 }
83 static RValue getComplex(const std::pair<llvm::Value *, llvm::Value *> &C) {
84 RValue ER;
85 ER.V1 = C.first;
86 ER.V2 = C.second;
87 ER.Flavor = Complex;
Mike Stump6b735682009-05-28 00:16:27 +000088 ER.Volatile = false;
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000089 return ER;
90 }
Mike Stump8b3d93a2009-05-23 20:21:36 +000091 // FIXME: Aggregate rvalues need to retain information about whether they are
92 // volatile or not. Remove default to find all places that probably get this
93 // wrong.
94 static RValue getAggregate(llvm::Value *V, bool Vol = false) {
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000095 RValue ER;
96 ER.V1 = V;
97 ER.Flavor = Aggregate;
Mike Stump8b3d93a2009-05-23 20:21:36 +000098 ER.Volatile = Vol;
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000099 return ER;
100 }
101};
102
103
104/// LValue - This represents an lvalue references. Because C/C++ allow
105/// bitfields, this is not a simple LLVM pointer, it may be a pointer plus a
106/// bitrange.
107class LValue {
108 // FIXME: alignment?
Mike Stump1eb44332009-09-09 15:08:12 +0000109
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000110 enum {
111 Simple, // This is a normal l-value, use getAddress().
112 VectorElt, // This is a vector element l-value (V[i]), use getVector*
113 BitField, // This is a bitfield l-value, use getBitfield*.
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000114 ExtVectorElt, // This is an extended vector subset, use getExtVectorComp
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000115 PropertyRef, // This is an Objective-C property reference, use
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000116 // getPropertyRefExpr
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000117 KVCRef // This is an objective-c 'implicit' property ref,
118 // use getKVCRefExpr
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000119 } LVType;
Fariborz Jahanian58626502008-11-19 00:59:10 +0000120
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000121 llvm::Value *V;
Mike Stump1eb44332009-09-09 15:08:12 +0000122
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000123 union {
124 // Index into a vector subscript: V[i]
125 llvm::Value *VectorIdx;
126
127 // ExtVector element subset: V.xyx
128 llvm::Constant *VectorElts;
Mike Stump1eb44332009-09-09 15:08:12 +0000129
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000130 // BitField start bit and size
131 struct {
132 unsigned short StartBit;
133 unsigned short Size;
134 bool IsSigned;
135 } BitfieldData;
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000136
137 // Obj-C property reference expression
138 const ObjCPropertyRefExpr *PropertyRefExpr;
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000139 // ObjC 'implicit' property reference expression
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000140 const ObjCImplicitSetterGetterRefExpr *KVCRefExpr;
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000141 };
142
John McCall0953e762009-09-24 19:53:00 +0000143 // 'const' is unused here
144 Qualifiers Quals;
Fariborz Jahanian58626502008-11-19 00:59:10 +0000145
Fariborz Jahaniand1cc8042008-11-20 20:53:20 +0000146 // objective-c's ivar
147 bool Ivar:1;
Fariborz Jahanian1c1afc42009-09-18 00:04:00 +0000148
149 // objective-c's ivar is an array
Fariborz Jahanianfd02ed72009-09-21 18:54:29 +0000150 bool ObjIsArray:1;
Mike Stump1eb44332009-09-09 15:08:12 +0000151
Fariborz Jahanian4f676ed2009-02-21 00:30:43 +0000152 // LValue is non-gc'able for any reason, including being a parameter or local
153 // variable.
154 bool NonGC: 1;
Fariborz Jahaniand1cc8042008-11-20 20:53:20 +0000155
Fariborz Jahanianbf63b872009-05-04 23:27:20 +0000156 // Lvalue is a global reference of an objective-c object
157 bool GlobalObjCRef : 1;
158
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000159private:
John McCall0953e762009-09-24 19:53:00 +0000160 void SetQualifiers(Qualifiers Quals) {
161 this->Quals = Quals;
162
Mike Stumpf5408fe2009-05-16 07:57:57 +0000163 // FIXME: Convenient place to set objc flags to 0. This should really be
164 // done in a user-defined constructor instead.
John McCall0953e762009-09-24 19:53:00 +0000165 this->Ivar = this->ObjIsArray = this->NonGC = this->GlobalObjCRef = false;
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000166 }
Mike Stump1eb44332009-09-09 15:08:12 +0000167
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000168public:
169 bool isSimple() const { return LVType == Simple; }
170 bool isVectorElt() const { return LVType == VectorElt; }
171 bool isBitfield() const { return LVType == BitField; }
172 bool isExtVectorElt() const { return LVType == ExtVectorElt; }
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000173 bool isPropertyRef() const { return LVType == PropertyRef; }
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000174 bool isKVCRef() const { return LVType == KVCRef; }
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000175
John McCall0953e762009-09-24 19:53:00 +0000176 bool isVolatileQualified() const { return Quals.hasVolatile(); }
177 bool isRestrictQualified() const { return Quals.hasRestrict(); }
178 unsigned getVRQualifiers() const {
179 return Quals.getCVRQualifiers() & ~Qualifiers::Const;
Chris Lattner1bd885e2009-02-16 22:25:49 +0000180 }
Mike Stump1eb44332009-09-09 15:08:12 +0000181
Fariborz Jahaniand1cc8042008-11-20 20:53:20 +0000182 bool isObjCIvar() const { return Ivar; }
Fariborz Jahanianfd02ed72009-09-21 18:54:29 +0000183 bool isObjCArray() const { return ObjIsArray; }
Fariborz Jahanian4f676ed2009-02-21 00:30:43 +0000184 bool isNonGC () const { return NonGC; }
Fariborz Jahanianbf63b872009-05-04 23:27:20 +0000185 bool isGlobalObjCRef() const { return GlobalObjCRef; }
John McCall0953e762009-09-24 19:53:00 +0000186 bool isObjCWeak() const { return Quals.getObjCGCAttr() == Qualifiers::Weak; }
187 bool isObjCStrong() const { return Quals.getObjCGCAttr() == Qualifiers::Strong; }
Mon P Wangc6a38a42009-07-22 03:08:17 +0000188
John McCall0953e762009-09-24 19:53:00 +0000189 unsigned getAddressSpace() const { return Quals.getAddressSpace(); }
Mon P Wangc6a38a42009-07-22 03:08:17 +0000190
Fariborz Jahanian2ab19682008-11-21 18:14:01 +0000191 static void SetObjCIvar(LValue& R, bool iValue) {
192 R.Ivar = iValue;
Fariborz Jahaniand1cc8042008-11-20 20:53:20 +0000193 }
Fariborz Jahanianfd02ed72009-09-21 18:54:29 +0000194 static void SetObjCArray(LValue& R, bool iValue) {
195 R.ObjIsArray = iValue;
Fariborz Jahanian1c1afc42009-09-18 00:04:00 +0000196 }
Fariborz Jahanianbf63b872009-05-04 23:27:20 +0000197 static void SetGlobalObjCRef(LValue& R, bool iValue) {
198 R.GlobalObjCRef = iValue;
199 }
Mike Stump1eb44332009-09-09 15:08:12 +0000200
Fariborz Jahanian4f676ed2009-02-21 00:30:43 +0000201 static void SetObjCNonGC(LValue& R, bool iValue) {
202 R.NonGC = iValue;
203 }
Mike Stump1eb44332009-09-09 15:08:12 +0000204
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000205 // simple lvalue
206 llvm::Value *getAddress() const { assert(isSimple()); return V; }
207 // vector elt lvalue
208 llvm::Value *getVectorAddr() const { assert(isVectorElt()); return V; }
209 llvm::Value *getVectorIdx() const { assert(isVectorElt()); return VectorIdx; }
210 // extended vector elements.
211 llvm::Value *getExtVectorAddr() const { assert(isExtVectorElt()); return V; }
212 llvm::Constant *getExtVectorElts() const {
213 assert(isExtVectorElt());
214 return VectorElts;
215 }
216 // bitfield lvalue
217 llvm::Value *getBitfieldAddr() const { assert(isBitfield()); return V; }
218 unsigned short getBitfieldStartBit() const {
219 assert(isBitfield());
220 return BitfieldData.StartBit;
221 }
222 unsigned short getBitfieldSize() const {
223 assert(isBitfield());
224 return BitfieldData.Size;
225 }
226 bool isBitfieldSigned() const {
227 assert(isBitfield());
228 return BitfieldData.IsSigned;
229 }
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000230 // property ref lvalue
231 const ObjCPropertyRefExpr *getPropertyRefExpr() const {
232 assert(isPropertyRef());
233 return PropertyRefExpr;
234 }
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000235
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000236 // 'implicit' property ref lvalue
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000237 const ObjCImplicitSetterGetterRefExpr *getKVCRefExpr() const {
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000238 assert(isKVCRef());
239 return KVCRefExpr;
240 }
241
John McCall0953e762009-09-24 19:53:00 +0000242 static LValue MakeAddr(llvm::Value *V, Qualifiers Quals) {
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000243 LValue R;
244 R.LVType = Simple;
245 R.V = V;
John McCall0953e762009-09-24 19:53:00 +0000246 R.SetQualifiers(Quals);
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000247 return R;
248 }
Mike Stump1eb44332009-09-09 15:08:12 +0000249
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000250 static LValue MakeVectorElt(llvm::Value *Vec, llvm::Value *Idx,
John McCall0953e762009-09-24 19:53:00 +0000251 unsigned CVR) {
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000252 LValue R;
253 R.LVType = VectorElt;
254 R.V = Vec;
255 R.VectorIdx = Idx;
John McCall0953e762009-09-24 19:53:00 +0000256 R.SetQualifiers(Qualifiers::fromCVRMask(CVR));
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000257 return R;
258 }
Mike Stump1eb44332009-09-09 15:08:12 +0000259
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000260 static LValue MakeExtVectorElt(llvm::Value *Vec, llvm::Constant *Elts,
John McCall0953e762009-09-24 19:53:00 +0000261 unsigned CVR) {
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000262 LValue R;
263 R.LVType = ExtVectorElt;
264 R.V = Vec;
265 R.VectorElts = Elts;
John McCall0953e762009-09-24 19:53:00 +0000266 R.SetQualifiers(Qualifiers::fromCVRMask(CVR));
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000267 return R;
268 }
269
270 static LValue MakeBitfield(llvm::Value *V, unsigned short StartBit,
271 unsigned short Size, bool IsSigned,
John McCall0953e762009-09-24 19:53:00 +0000272 unsigned CVR) {
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000273 LValue R;
274 R.LVType = BitField;
275 R.V = V;
276 R.BitfieldData.StartBit = StartBit;
277 R.BitfieldData.Size = Size;
278 R.BitfieldData.IsSigned = IsSigned;
John McCall0953e762009-09-24 19:53:00 +0000279 R.SetQualifiers(Qualifiers::fromCVRMask(CVR));
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000280 return R;
281 }
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000282
Mike Stumpf5408fe2009-05-16 07:57:57 +0000283 // FIXME: It is probably bad that we aren't emitting the target when we build
284 // the lvalue. However, this complicates the code a bit, and I haven't figured
285 // out how to make it go wrong yet.
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000286 static LValue MakePropertyRef(const ObjCPropertyRefExpr *E,
John McCall0953e762009-09-24 19:53:00 +0000287 unsigned CVR) {
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000288 LValue R;
289 R.LVType = PropertyRef;
290 R.PropertyRefExpr = E;
John McCall0953e762009-09-24 19:53:00 +0000291 R.SetQualifiers(Qualifiers::fromCVRMask(CVR));
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000292 return R;
293 }
Mike Stump1eb44332009-09-09 15:08:12 +0000294
295 static LValue MakeKVCRef(const ObjCImplicitSetterGetterRefExpr *E,
John McCall0953e762009-09-24 19:53:00 +0000296 unsigned CVR) {
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000297 LValue R;
298 R.LVType = KVCRef;
299 R.KVCRefExpr = E;
John McCall0953e762009-09-24 19:53:00 +0000300 R.SetQualifiers(Qualifiers::fromCVRMask(CVR));
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000301 return R;
302 }
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000303};
304
305} // end namespace CodeGen
306} // end namespace clang
307
308#endif