blob: 92ef9dc4a11f53330c585573dfc6a1206c15600c [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 {
Benjamin Kramer81bf3b32010-05-02 14:59:10 +000037 enum Flavor { Scalar, Complex, Aggregate };
Mike Stump1eb44332009-09-09 15:08:12 +000038
Benjamin Kramer81bf3b32010-05-02 14:59:10 +000039 // Stores first value and flavor.
40 llvm::PointerIntPair<llvm::Value *, 2, Flavor> V1;
41 // Stores second value and volatility.
42 llvm::PointerIntPair<llvm::Value *, 1, bool> V2;
43
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000044public:
Benjamin Kramer81bf3b32010-05-02 14:59:10 +000045 bool isScalar() const { return V1.getInt() == Scalar; }
46 bool isComplex() const { return V1.getInt() == Complex; }
47 bool isAggregate() const { return V1.getInt() == Aggregate; }
Mike Stump1eb44332009-09-09 15:08:12 +000048
Benjamin Kramer81bf3b32010-05-02 14:59:10 +000049 bool isVolatileQualified() const { return V2.getInt(); }
Mike Stump8b3d93a2009-05-23 20:21:36 +000050
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!");
Benjamin Kramer81bf3b32010-05-02 14:59:10 +000054 return V1.getPointer();
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000055 }
56
57 /// getComplexVal - Return the real/imag components of this complex value.
58 ///
59 std::pair<llvm::Value *, llvm::Value *> getComplexVal() const {
Benjamin Kramer81bf3b32010-05-02 14:59:10 +000060 return std::make_pair(V1.getPointer(), V2.getPointer());
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000061 }
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!");
Benjamin Kramer81bf3b32010-05-02 14:59:10 +000066 return V1.getPointer();
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000067 }
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;
Benjamin Kramer81bf3b32010-05-02 14:59:10 +000071 ER.V1.setPointer(V);
72 ER.V1.setInt(Scalar);
73 ER.V2.setInt(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;
Benjamin Kramer81bf3b32010-05-02 14:59:10 +000078 ER.V1.setPointer(V1);
79 ER.V2.setPointer(V2);
80 ER.V1.setInt(Complex);
81 ER.V2.setInt(false);
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000082 return ER;
83 }
84 static RValue getComplex(const std::pair<llvm::Value *, llvm::Value *> &C) {
Benjamin Kramer81bf3b32010-05-02 14:59:10 +000085 return getComplex(C.first, C.second);
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000086 }
Mike Stump8b3d93a2009-05-23 20:21:36 +000087 // FIXME: Aggregate rvalues need to retain information about whether they are
88 // volatile or not. Remove default to find all places that probably get this
89 // wrong.
Benjamin Kramer81bf3b32010-05-02 14:59:10 +000090 static RValue getAggregate(llvm::Value *V, bool Volatile = false) {
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000091 RValue ER;
Benjamin Kramer81bf3b32010-05-02 14:59:10 +000092 ER.V1.setPointer(V);
93 ER.V1.setInt(Aggregate);
94 ER.V2.setInt(Volatile);
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000095 return ER;
96 }
97};
98
99
100/// LValue - This represents an lvalue references. Because C/C++ allow
101/// bitfields, this is not a simple LLVM pointer, it may be a pointer plus a
102/// bitrange.
103class LValue {
104 // FIXME: alignment?
Mike Stump1eb44332009-09-09 15:08:12 +0000105
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000106 enum {
107 Simple, // This is a normal l-value, use getAddress().
108 VectorElt, // This is a vector element l-value (V[i]), use getVector*
109 BitField, // This is a bitfield l-value, use getBitfield*.
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000110 ExtVectorElt, // This is an extended vector subset, use getExtVectorComp
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000111 PropertyRef, // This is an Objective-C property reference, use
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000112 // getPropertyRefExpr
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000113 KVCRef // This is an objective-c 'implicit' property ref,
114 // use getKVCRefExpr
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000115 } LVType;
Fariborz Jahanian58626502008-11-19 00:59:10 +0000116
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000117 llvm::Value *V;
Mike Stump1eb44332009-09-09 15:08:12 +0000118
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000119 union {
120 // Index into a vector subscript: V[i]
121 llvm::Value *VectorIdx;
122
123 // ExtVector element subset: V.xyx
124 llvm::Constant *VectorElts;
Mike Stump1eb44332009-09-09 15:08:12 +0000125
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000126 // BitField start bit and size
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000127 const CGBitFieldInfo *BitFieldInfo;
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000128
129 // Obj-C property reference expression
130 const ObjCPropertyRefExpr *PropertyRefExpr;
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000131
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000132 // ObjC 'implicit' property reference expression
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000133 const ObjCImplicitSetterGetterRefExpr *KVCRefExpr;
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000134 };
135
John McCall0953e762009-09-24 19:53:00 +0000136 // 'const' is unused here
137 Qualifiers Quals;
Fariborz Jahanian58626502008-11-19 00:59:10 +0000138
Fariborz Jahaniand1cc8042008-11-20 20:53:20 +0000139 // objective-c's ivar
140 bool Ivar:1;
Fariborz Jahanian1c1afc42009-09-18 00:04:00 +0000141
142 // objective-c's ivar is an array
Fariborz Jahanianfd02ed72009-09-21 18:54:29 +0000143 bool ObjIsArray:1;
Mike Stump1eb44332009-09-09 15:08:12 +0000144
Fariborz Jahanian4f676ed2009-02-21 00:30:43 +0000145 // LValue is non-gc'able for any reason, including being a parameter or local
146 // variable.
147 bool NonGC: 1;
Fariborz Jahaniand1cc8042008-11-20 20:53:20 +0000148
Fariborz Jahanianbf63b872009-05-04 23:27:20 +0000149 // Lvalue is a global reference of an objective-c object
150 bool GlobalObjCRef : 1;
151
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000152 Expr *BaseIvarExp;
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000153private:
John McCall0953e762009-09-24 19:53:00 +0000154 void SetQualifiers(Qualifiers Quals) {
155 this->Quals = Quals;
156
Mike Stumpf5408fe2009-05-16 07:57:57 +0000157 // FIXME: Convenient place to set objc flags to 0. This should really be
158 // done in a user-defined constructor instead.
John McCall0953e762009-09-24 19:53:00 +0000159 this->Ivar = this->ObjIsArray = this->NonGC = this->GlobalObjCRef = false;
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000160 this->BaseIvarExp = 0;
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000161 }
Mike Stump1eb44332009-09-09 15:08:12 +0000162
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000163public:
164 bool isSimple() const { return LVType == Simple; }
165 bool isVectorElt() const { return LVType == VectorElt; }
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000166 bool isBitField() const { return LVType == BitField; }
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000167 bool isExtVectorElt() const { return LVType == ExtVectorElt; }
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000168 bool isPropertyRef() const { return LVType == PropertyRef; }
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000169 bool isKVCRef() const { return LVType == KVCRef; }
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000170
John McCall0953e762009-09-24 19:53:00 +0000171 bool isVolatileQualified() const { return Quals.hasVolatile(); }
172 bool isRestrictQualified() const { return Quals.hasRestrict(); }
173 unsigned getVRQualifiers() const {
174 return Quals.getCVRQualifiers() & ~Qualifiers::Const;
Chris Lattner1bd885e2009-02-16 22:25:49 +0000175 }
Mike Stump1eb44332009-09-09 15:08:12 +0000176
Fariborz Jahaniand1cc8042008-11-20 20:53:20 +0000177 bool isObjCIvar() const { return Ivar; }
Fariborz Jahanianfd02ed72009-09-21 18:54:29 +0000178 bool isObjCArray() const { return ObjIsArray; }
Fariborz Jahanian4f676ed2009-02-21 00:30:43 +0000179 bool isNonGC () const { return NonGC; }
Fariborz Jahanianbf63b872009-05-04 23:27:20 +0000180 bool isGlobalObjCRef() const { return GlobalObjCRef; }
John McCall0953e762009-09-24 19:53:00 +0000181 bool isObjCWeak() const { return Quals.getObjCGCAttr() == Qualifiers::Weak; }
182 bool isObjCStrong() const { return Quals.getObjCGCAttr() == Qualifiers::Strong; }
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000183
184 Expr *getBaseIvarExp() const { return BaseIvarExp; }
185 void setBaseIvarExp(Expr *V) { BaseIvarExp = V; }
Mon P Wangc6a38a42009-07-22 03:08:17 +0000186
John McCall0953e762009-09-24 19:53:00 +0000187 unsigned getAddressSpace() const { return Quals.getAddressSpace(); }
Mon P Wangc6a38a42009-07-22 03:08:17 +0000188
Fariborz Jahanian2ab19682008-11-21 18:14:01 +0000189 static void SetObjCIvar(LValue& R, bool iValue) {
190 R.Ivar = iValue;
Fariborz Jahaniand1cc8042008-11-20 20:53:20 +0000191 }
Fariborz Jahanianfd02ed72009-09-21 18:54:29 +0000192 static void SetObjCArray(LValue& R, bool iValue) {
193 R.ObjIsArray = iValue;
Fariborz Jahanian1c1afc42009-09-18 00:04:00 +0000194 }
Fariborz Jahanianbf63b872009-05-04 23:27:20 +0000195 static void SetGlobalObjCRef(LValue& R, bool iValue) {
196 R.GlobalObjCRef = iValue;
197 }
Mike Stump1eb44332009-09-09 15:08:12 +0000198
Fariborz Jahanian4f676ed2009-02-21 00:30:43 +0000199 static void SetObjCNonGC(LValue& R, bool iValue) {
200 R.NonGC = iValue;
201 }
Mike Stump1eb44332009-09-09 15:08:12 +0000202
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000203 // simple lvalue
204 llvm::Value *getAddress() const { assert(isSimple()); return V; }
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000205
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000206 // vector elt lvalue
207 llvm::Value *getVectorAddr() const { assert(isVectorElt()); return V; }
208 llvm::Value *getVectorIdx() const { assert(isVectorElt()); return VectorIdx; }
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000209
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000210 // extended vector elements.
211 llvm::Value *getExtVectorAddr() const { assert(isExtVectorElt()); return V; }
212 llvm::Constant *getExtVectorElts() const {
213 assert(isExtVectorElt());
214 return VectorElts;
215 }
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000216
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000217 // bitfield lvalue
Daniel Dunbar7f289642010-04-08 02:59:45 +0000218 llvm::Value *getBitFieldBaseAddr() const {
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000219 assert(isBitField());
220 return V;
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000221 }
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000222 const CGBitFieldInfo &getBitFieldInfo() const {
223 assert(isBitField());
224 return *BitFieldInfo;
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000225 }
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000226
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000227 // property ref lvalue
228 const ObjCPropertyRefExpr *getPropertyRefExpr() const {
229 assert(isPropertyRef());
230 return PropertyRefExpr;
231 }
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000232
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000233 // 'implicit' property ref lvalue
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000234 const ObjCImplicitSetterGetterRefExpr *getKVCRefExpr() const {
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000235 assert(isKVCRef());
236 return KVCRefExpr;
237 }
238
John McCall0953e762009-09-24 19:53:00 +0000239 static LValue MakeAddr(llvm::Value *V, Qualifiers Quals) {
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000240 LValue R;
241 R.LVType = Simple;
242 R.V = V;
John McCall0953e762009-09-24 19:53:00 +0000243 R.SetQualifiers(Quals);
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000244 return R;
245 }
Mike Stump1eb44332009-09-09 15:08:12 +0000246
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000247 static LValue MakeVectorElt(llvm::Value *Vec, llvm::Value *Idx,
John McCall0953e762009-09-24 19:53:00 +0000248 unsigned CVR) {
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000249 LValue R;
250 R.LVType = VectorElt;
251 R.V = Vec;
252 R.VectorIdx = Idx;
John McCall0953e762009-09-24 19:53:00 +0000253 R.SetQualifiers(Qualifiers::fromCVRMask(CVR));
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000254 return R;
255 }
Mike Stump1eb44332009-09-09 15:08:12 +0000256
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000257 static LValue MakeExtVectorElt(llvm::Value *Vec, llvm::Constant *Elts,
John McCall0953e762009-09-24 19:53:00 +0000258 unsigned CVR) {
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000259 LValue R;
260 R.LVType = ExtVectorElt;
261 R.V = Vec;
262 R.VectorElts = Elts;
John McCall0953e762009-09-24 19:53:00 +0000263 R.SetQualifiers(Qualifiers::fromCVRMask(CVR));
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000264 return R;
265 }
266
Daniel Dunbar7f289642010-04-08 02:59:45 +0000267 /// \brief Create a new object to represent a bit-field access.
268 ///
269 /// \param BaseValue - The base address of the structure containing the
270 /// bit-field.
271 /// \param Info - The information describing how to perform the bit-field
272 /// access.
273 static LValue MakeBitfield(llvm::Value *BaseValue, const CGBitFieldInfo &Info,
Daniel Dunbarefbf4872010-04-06 01:07:44 +0000274 unsigned CVR) {
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000275 LValue R;
276 R.LVType = BitField;
Daniel Dunbar7f289642010-04-08 02:59:45 +0000277 R.V = BaseValue;
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000278 R.BitFieldInfo = &Info;
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