blob: e4175bb62d8218562ea874ca15ecfd2dac8161e8 [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 Jahanian43f44702008-11-22 22:30:21 +000027 class ObjCKVCRefExpr;
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;
40
Mike Stump8b3d93a2009-05-23 20:21:36 +000041 bool Volatile:1;
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000042public:
43
44 bool isScalar() const { return Flavor == Scalar; }
45 bool isComplex() const { return Flavor == Complex; }
46 bool isAggregate() const { return Flavor == Aggregate; }
47
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 }
61
62 /// 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 }
67
68 static RValue get(llvm::Value *V) {
69 RValue ER;
70 ER.V1 = V;
71 ER.Flavor = Scalar;
72 return ER;
73 }
74 static RValue getComplex(llvm::Value *V1, llvm::Value *V2) {
75 RValue ER;
76 ER.V1 = V1;
77 ER.V2 = V2;
78 ER.Flavor = Complex;
79 return ER;
80 }
81 static RValue getComplex(const std::pair<llvm::Value *, llvm::Value *> &C) {
82 RValue ER;
83 ER.V1 = C.first;
84 ER.V2 = C.second;
85 ER.Flavor = Complex;
86 return ER;
87 }
Mike Stump8b3d93a2009-05-23 20:21:36 +000088 // FIXME: Aggregate rvalues need to retain information about whether they are
89 // volatile or not. Remove default to find all places that probably get this
90 // wrong.
91 static RValue getAggregate(llvm::Value *V, bool Vol = false) {
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000092 RValue ER;
93 ER.V1 = V;
94 ER.Flavor = Aggregate;
Mike Stump8b3d93a2009-05-23 20:21:36 +000095 ER.Volatile = Vol;
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000096 return ER;
97 }
98};
99
100
101/// LValue - This represents an lvalue references. Because C/C++ allow
102/// bitfields, this is not a simple LLVM pointer, it may be a pointer plus a
103/// bitrange.
104class LValue {
105 // FIXME: alignment?
106
107 enum {
108 Simple, // This is a normal l-value, use getAddress().
109 VectorElt, // This is a vector element l-value (V[i]), use getVector*
110 BitField, // This is a bitfield l-value, use getBitfield*.
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000111 ExtVectorElt, // This is an extended vector subset, use getExtVectorComp
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000112 PropertyRef, // This is an Objective-C property reference, use
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000113 // getPropertyRefExpr
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000114 KVCRef // This is an objective-c 'implicit' property ref,
115 // use getKVCRefExpr
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000116 } LVType;
Fariborz Jahanian58626502008-11-19 00:59:10 +0000117
118 enum ObjCType {
Nate Begemanfea86852008-12-16 19:57:09 +0000119 None = 0, // object with no gc attribute.
120 Weak, // __weak object expression
121 Strong // __strong object expression
Fariborz Jahanian58626502008-11-19 00:59:10 +0000122 };
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000123
124 llvm::Value *V;
125
126 union {
127 // Index into a vector subscript: V[i]
128 llvm::Value *VectorIdx;
129
130 // ExtVector element subset: V.xyx
131 llvm::Constant *VectorElts;
132
133 // BitField start bit and size
134 struct {
135 unsigned short StartBit;
136 unsigned short Size;
137 bool IsSigned;
138 } BitfieldData;
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000139
140 // Obj-C property reference expression
141 const ObjCPropertyRefExpr *PropertyRefExpr;
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000142 // ObjC 'implicit' property reference expression
143 const ObjCKVCRefExpr *KVCRefExpr;
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000144 };
145
146 bool Volatile:1;
147 // FIXME: set but never used, what effect should it have?
148 bool Restrict:1;
Fariborz Jahanian58626502008-11-19 00:59:10 +0000149
Fariborz Jahaniand1cc8042008-11-20 20:53:20 +0000150 // objective-c's ivar
151 bool Ivar:1;
Fariborz Jahanian4f676ed2009-02-21 00:30:43 +0000152
153 // LValue is non-gc'able for any reason, including being a parameter or local
154 // variable.
155 bool NonGC: 1;
Fariborz Jahaniand1cc8042008-11-20 20:53:20 +0000156
Fariborz Jahanianbf63b872009-05-04 23:27:20 +0000157 // Lvalue is a global reference of an objective-c object
158 bool GlobalObjCRef : 1;
159
Fariborz Jahanian58626502008-11-19 00:59:10 +0000160 // objective-c's gc attributes
161 unsigned ObjCType : 2;
Fariborz Jahanianbf63b872009-05-04 23:27:20 +0000162
Fariborz Jahaniand1cc8042008-11-20 20:53:20 +0000163
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000164
165private:
166 static void SetQualifiers(unsigned Qualifiers, LValue& R) {
167 R.Volatile = (Qualifiers&QualType::Volatile)!=0;
168 R.Restrict = (Qualifiers&QualType::Restrict)!=0;
Mike Stumpf5408fe2009-05-16 07:57:57 +0000169 // FIXME: Convenient place to set objc flags to 0. This should really be
170 // done in a user-defined constructor instead.
Fariborz Jahanian58626502008-11-19 00:59:10 +0000171 R.ObjCType = None;
Fariborz Jahanianbf63b872009-05-04 23:27:20 +0000172 R.Ivar = R.NonGC = R.GlobalObjCRef = false;
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000173 }
Fariborz Jahanian6d657c42008-11-18 20:18:11 +0000174
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000175public:
176 bool isSimple() const { return LVType == Simple; }
177 bool isVectorElt() const { return LVType == VectorElt; }
178 bool isBitfield() const { return LVType == BitField; }
179 bool isExtVectorElt() const { return LVType == ExtVectorElt; }
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000180 bool isPropertyRef() const { return LVType == PropertyRef; }
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000181 bool isKVCRef() const { return LVType == KVCRef; }
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000182
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000183 bool isVolatileQualified() const { return Volatile; }
184 bool isRestrictQualified() const { return Restrict; }
Chris Lattner1bd885e2009-02-16 22:25:49 +0000185 unsigned getQualifiers() const {
186 return (Volatile ? QualType::Volatile : 0) |
187 (Restrict ? QualType::Restrict : 0);
188 }
Fariborz Jahanian6d657c42008-11-18 20:18:11 +0000189
Fariborz Jahaniand1cc8042008-11-20 20:53:20 +0000190 bool isObjCIvar() const { return Ivar; }
Fariborz Jahanian4f676ed2009-02-21 00:30:43 +0000191 bool isNonGC () const { return NonGC; }
Fariborz Jahanianbf63b872009-05-04 23:27:20 +0000192 bool isGlobalObjCRef() const { return GlobalObjCRef; }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +0000193 bool isObjCWeak() const { return ObjCType == Weak; }
194 bool isObjCStrong() const { return ObjCType == Strong; }
Fariborz Jahanian6d657c42008-11-18 20:18:11 +0000195
Fariborz Jahanian2ab19682008-11-21 18:14:01 +0000196 static void SetObjCIvar(LValue& R, bool iValue) {
197 R.Ivar = iValue;
Fariborz Jahaniand1cc8042008-11-20 20:53:20 +0000198 }
Fariborz Jahanianbf63b872009-05-04 23:27:20 +0000199
200 static void SetGlobalObjCRef(LValue& R, bool iValue) {
201 R.GlobalObjCRef = iValue;
202 }
Fariborz Jahanian4f676ed2009-02-21 00:30:43 +0000203
204 static void SetObjCNonGC(LValue& R, bool iValue) {
205 R.NonGC = iValue;
206 }
Fariborz Jahanianc1debf32009-02-19 00:48:05 +0000207 static void SetObjCType(QualType::GCAttrTypes GCAttrs, LValue& R) {
208 if (GCAttrs == QualType::Weak)
Fariborz Jahanian58626502008-11-19 00:59:10 +0000209 R.ObjCType = Weak;
Fariborz Jahanianc1debf32009-02-19 00:48:05 +0000210 else if (GCAttrs == QualType::Strong)
Fariborz Jahanian58626502008-11-19 00:59:10 +0000211 R.ObjCType = Strong;
Fariborz Jahanian4f676ed2009-02-21 00:30:43 +0000212 else
213 R.ObjCType = None;
Fariborz Jahanian6d657c42008-11-18 20:18:11 +0000214 }
215
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000216 // simple lvalue
217 llvm::Value *getAddress() const { assert(isSimple()); return V; }
218 // vector elt lvalue
219 llvm::Value *getVectorAddr() const { assert(isVectorElt()); return V; }
220 llvm::Value *getVectorIdx() const { assert(isVectorElt()); return VectorIdx; }
221 // extended vector elements.
222 llvm::Value *getExtVectorAddr() const { assert(isExtVectorElt()); return V; }
223 llvm::Constant *getExtVectorElts() const {
224 assert(isExtVectorElt());
225 return VectorElts;
226 }
227 // bitfield lvalue
228 llvm::Value *getBitfieldAddr() const { assert(isBitfield()); return V; }
229 unsigned short getBitfieldStartBit() const {
230 assert(isBitfield());
231 return BitfieldData.StartBit;
232 }
233 unsigned short getBitfieldSize() const {
234 assert(isBitfield());
235 return BitfieldData.Size;
236 }
237 bool isBitfieldSigned() const {
238 assert(isBitfield());
239 return BitfieldData.IsSigned;
240 }
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000241 // property ref lvalue
242 const ObjCPropertyRefExpr *getPropertyRefExpr() const {
243 assert(isPropertyRef());
244 return PropertyRefExpr;
245 }
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000246
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000247 // 'implicit' property ref lvalue
248 const ObjCKVCRefExpr *getKVCRefExpr() const {
249 assert(isKVCRef());
250 return KVCRefExpr;
251 }
252
Fariborz Jahanianc1debf32009-02-19 00:48:05 +0000253 static LValue MakeAddr(llvm::Value *V, unsigned Qualifiers,
254 QualType::GCAttrTypes GCAttrs = QualType::GCNone) {
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000255 LValue R;
256 R.LVType = Simple;
257 R.V = V;
258 SetQualifiers(Qualifiers,R);
Fariborz Jahanianc1debf32009-02-19 00:48:05 +0000259 SetObjCType(GCAttrs, R);
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000260 return R;
261 }
262
263 static LValue MakeVectorElt(llvm::Value *Vec, llvm::Value *Idx,
264 unsigned Qualifiers) {
265 LValue R;
266 R.LVType = VectorElt;
267 R.V = Vec;
268 R.VectorIdx = Idx;
269 SetQualifiers(Qualifiers,R);
270 return R;
271 }
272
273 static LValue MakeExtVectorElt(llvm::Value *Vec, llvm::Constant *Elts,
274 unsigned Qualifiers) {
275 LValue R;
276 R.LVType = ExtVectorElt;
277 R.V = Vec;
278 R.VectorElts = Elts;
279 SetQualifiers(Qualifiers,R);
280 return R;
281 }
282
283 static LValue MakeBitfield(llvm::Value *V, unsigned short StartBit,
284 unsigned short Size, bool IsSigned,
285 unsigned Qualifiers) {
286 LValue R;
287 R.LVType = BitField;
288 R.V = V;
289 R.BitfieldData.StartBit = StartBit;
290 R.BitfieldData.Size = Size;
291 R.BitfieldData.IsSigned = IsSigned;
292 SetQualifiers(Qualifiers,R);
293 return R;
294 }
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000295
Mike Stumpf5408fe2009-05-16 07:57:57 +0000296 // FIXME: It is probably bad that we aren't emitting the target when we build
297 // the lvalue. However, this complicates the code a bit, and I haven't figured
298 // out how to make it go wrong yet.
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000299 static LValue MakePropertyRef(const ObjCPropertyRefExpr *E,
300 unsigned Qualifiers) {
301 LValue R;
302 R.LVType = PropertyRef;
303 R.PropertyRefExpr = E;
304 SetQualifiers(Qualifiers,R);
305 return R;
306 }
Chris Lattner1bd885e2009-02-16 22:25:49 +0000307
308 static LValue MakeKVCRef(const ObjCKVCRefExpr *E, unsigned Qualifiers) {
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000309 LValue R;
310 R.LVType = KVCRef;
311 R.KVCRefExpr = E;
312 SetQualifiers(Qualifiers,R);
313 return R;
314 }
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000315};
316
317} // end namespace CodeGen
318} // end namespace clang
319
320#endif