blob: aed9fc156def7763b9c388f5f140cc9ff7f07b60 [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
41 // FIXME: Aggregate rvalues need to retain information about whether they are
42 // volatile or not.
43public:
44
45 bool isScalar() const { return Flavor == Scalar; }
46 bool isComplex() const { return Flavor == Complex; }
47 bool isAggregate() const { return Flavor == Aggregate; }
48
49 /// getScalar() - Return the Value* of this scalar value.
50 llvm::Value *getScalarVal() const {
51 assert(isScalar() && "Not a scalar!");
52 return V1;
53 }
54
55 /// getComplexVal - Return the real/imag components of this complex value.
56 ///
57 std::pair<llvm::Value *, llvm::Value *> getComplexVal() const {
58 return std::pair<llvm::Value *, llvm::Value *>(V1, V2);
59 }
60
61 /// getAggregateAddr() - Return the Value* of the address of the aggregate.
62 llvm::Value *getAggregateAddr() const {
63 assert(isAggregate() && "Not an aggregate!");
64 return V1;
65 }
66
67 static RValue get(llvm::Value *V) {
68 RValue ER;
69 ER.V1 = V;
70 ER.Flavor = Scalar;
71 return ER;
72 }
73 static RValue getComplex(llvm::Value *V1, llvm::Value *V2) {
74 RValue ER;
75 ER.V1 = V1;
76 ER.V2 = V2;
77 ER.Flavor = Complex;
78 return ER;
79 }
80 static RValue getComplex(const std::pair<llvm::Value *, llvm::Value *> &C) {
81 RValue ER;
82 ER.V1 = C.first;
83 ER.V2 = C.second;
84 ER.Flavor = Complex;
85 return ER;
86 }
87 static RValue getAggregate(llvm::Value *V) {
88 RValue ER;
89 ER.V1 = V;
90 ER.Flavor = Aggregate;
91 return ER;
92 }
93};
94
95
96/// LValue - This represents an lvalue references. Because C/C++ allow
97/// bitfields, this is not a simple LLVM pointer, it may be a pointer plus a
98/// bitrange.
99class LValue {
100 // FIXME: alignment?
101
102 enum {
103 Simple, // This is a normal l-value, use getAddress().
104 VectorElt, // This is a vector element l-value (V[i]), use getVector*
105 BitField, // This is a bitfield l-value, use getBitfield*.
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000106 ExtVectorElt, // This is an extended vector subset, use getExtVectorComp
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000107 PropertyRef, // This is an Objective-C property reference, use
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000108 // getPropertyRefExpr
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000109 KVCRef // This is an objective-c 'implicit' property ref,
110 // use getKVCRefExpr
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000111 } LVType;
Fariborz Jahanian58626502008-11-19 00:59:10 +0000112
113 enum ObjCType {
Nate Begemanfea86852008-12-16 19:57:09 +0000114 None = 0, // object with no gc attribute.
115 Weak, // __weak object expression
116 Strong // __strong object expression
Fariborz Jahanian58626502008-11-19 00:59:10 +0000117 };
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000118
119 llvm::Value *V;
120
121 union {
122 // Index into a vector subscript: V[i]
123 llvm::Value *VectorIdx;
124
125 // ExtVector element subset: V.xyx
126 llvm::Constant *VectorElts;
127
128 // BitField start bit and size
129 struct {
130 unsigned short StartBit;
131 unsigned short Size;
132 bool IsSigned;
133 } BitfieldData;
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000134
135 // Obj-C property reference expression
136 const ObjCPropertyRefExpr *PropertyRefExpr;
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000137 // ObjC 'implicit' property reference expression
138 const ObjCKVCRefExpr *KVCRefExpr;
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000139 };
140
141 bool Volatile:1;
142 // FIXME: set but never used, what effect should it have?
143 bool Restrict:1;
Fariborz Jahanian58626502008-11-19 00:59:10 +0000144
Fariborz Jahaniand1cc8042008-11-20 20:53:20 +0000145 // objective-c's ivar
146 bool Ivar:1;
Fariborz Jahanian4f676ed2009-02-21 00:30:43 +0000147
148 // LValue is non-gc'able for any reason, including being a parameter or local
149 // variable.
150 bool NonGC: 1;
Fariborz Jahaniand1cc8042008-11-20 20:53:20 +0000151
Fariborz Jahanianbf63b872009-05-04 23:27:20 +0000152 // Lvalue is a global reference of an objective-c object
153 bool GlobalObjCRef : 1;
154
Fariborz Jahanian58626502008-11-19 00:59:10 +0000155 // objective-c's gc attributes
156 unsigned ObjCType : 2;
Fariborz Jahanianbf63b872009-05-04 23:27:20 +0000157
Fariborz Jahaniand1cc8042008-11-20 20:53:20 +0000158
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000159
160private:
161 static void SetQualifiers(unsigned Qualifiers, LValue& R) {
162 R.Volatile = (Qualifiers&QualType::Volatile)!=0;
163 R.Restrict = (Qualifiers&QualType::Restrict)!=0;
Mike Stumpf5408fe2009-05-16 07:57:57 +0000164 // FIXME: Convenient place to set objc flags to 0. This should really be
165 // done in a user-defined constructor instead.
Fariborz Jahanian58626502008-11-19 00:59:10 +0000166 R.ObjCType = None;
Fariborz Jahanianbf63b872009-05-04 23:27:20 +0000167 R.Ivar = R.NonGC = R.GlobalObjCRef = false;
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000168 }
Fariborz Jahanian6d657c42008-11-18 20:18:11 +0000169
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000170public:
171 bool isSimple() const { return LVType == Simple; }
172 bool isVectorElt() const { return LVType == VectorElt; }
173 bool isBitfield() const { return LVType == BitField; }
174 bool isExtVectorElt() const { return LVType == ExtVectorElt; }
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000175 bool isPropertyRef() const { return LVType == PropertyRef; }
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000176 bool isKVCRef() const { return LVType == KVCRef; }
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000177
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000178 bool isVolatileQualified() const { return Volatile; }
179 bool isRestrictQualified() const { return Restrict; }
Chris Lattner1bd885e2009-02-16 22:25:49 +0000180 unsigned getQualifiers() const {
181 return (Volatile ? QualType::Volatile : 0) |
182 (Restrict ? QualType::Restrict : 0);
183 }
Fariborz Jahanian6d657c42008-11-18 20:18:11 +0000184
Fariborz Jahaniand1cc8042008-11-20 20:53:20 +0000185 bool isObjCIvar() const { return Ivar; }
Fariborz Jahanian4f676ed2009-02-21 00:30:43 +0000186 bool isNonGC () const { return NonGC; }
Fariborz Jahanianbf63b872009-05-04 23:27:20 +0000187 bool isGlobalObjCRef() const { return GlobalObjCRef; }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +0000188 bool isObjCWeak() const { return ObjCType == Weak; }
189 bool isObjCStrong() const { return ObjCType == Strong; }
Fariborz Jahanian6d657c42008-11-18 20:18:11 +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 Jahanianbf63b872009-05-04 23:27:20 +0000194
195 static void SetGlobalObjCRef(LValue& R, bool iValue) {
196 R.GlobalObjCRef = iValue;
197 }
Fariborz Jahanian4f676ed2009-02-21 00:30:43 +0000198
199 static void SetObjCNonGC(LValue& R, bool iValue) {
200 R.NonGC = iValue;
201 }
Fariborz Jahanianc1debf32009-02-19 00:48:05 +0000202 static void SetObjCType(QualType::GCAttrTypes GCAttrs, LValue& R) {
203 if (GCAttrs == QualType::Weak)
Fariborz Jahanian58626502008-11-19 00:59:10 +0000204 R.ObjCType = Weak;
Fariborz Jahanianc1debf32009-02-19 00:48:05 +0000205 else if (GCAttrs == QualType::Strong)
Fariborz Jahanian58626502008-11-19 00:59:10 +0000206 R.ObjCType = Strong;
Fariborz Jahanian4f676ed2009-02-21 00:30:43 +0000207 else
208 R.ObjCType = None;
Fariborz Jahanian6d657c42008-11-18 20:18:11 +0000209 }
210
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000211 // simple lvalue
212 llvm::Value *getAddress() const { assert(isSimple()); return V; }
213 // vector elt lvalue
214 llvm::Value *getVectorAddr() const { assert(isVectorElt()); return V; }
215 llvm::Value *getVectorIdx() const { assert(isVectorElt()); return VectorIdx; }
216 // extended vector elements.
217 llvm::Value *getExtVectorAddr() const { assert(isExtVectorElt()); return V; }
218 llvm::Constant *getExtVectorElts() const {
219 assert(isExtVectorElt());
220 return VectorElts;
221 }
222 // bitfield lvalue
223 llvm::Value *getBitfieldAddr() const { assert(isBitfield()); return V; }
224 unsigned short getBitfieldStartBit() const {
225 assert(isBitfield());
226 return BitfieldData.StartBit;
227 }
228 unsigned short getBitfieldSize() const {
229 assert(isBitfield());
230 return BitfieldData.Size;
231 }
232 bool isBitfieldSigned() const {
233 assert(isBitfield());
234 return BitfieldData.IsSigned;
235 }
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000236 // property ref lvalue
237 const ObjCPropertyRefExpr *getPropertyRefExpr() const {
238 assert(isPropertyRef());
239 return PropertyRefExpr;
240 }
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000241
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000242 // 'implicit' property ref lvalue
243 const ObjCKVCRefExpr *getKVCRefExpr() const {
244 assert(isKVCRef());
245 return KVCRefExpr;
246 }
247
Fariborz Jahanianc1debf32009-02-19 00:48:05 +0000248 static LValue MakeAddr(llvm::Value *V, unsigned Qualifiers,
249 QualType::GCAttrTypes GCAttrs = QualType::GCNone) {
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000250 LValue R;
251 R.LVType = Simple;
252 R.V = V;
253 SetQualifiers(Qualifiers,R);
Fariborz Jahanianc1debf32009-02-19 00:48:05 +0000254 SetObjCType(GCAttrs, R);
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000255 return R;
256 }
257
258 static LValue MakeVectorElt(llvm::Value *Vec, llvm::Value *Idx,
259 unsigned Qualifiers) {
260 LValue R;
261 R.LVType = VectorElt;
262 R.V = Vec;
263 R.VectorIdx = Idx;
264 SetQualifiers(Qualifiers,R);
265 return R;
266 }
267
268 static LValue MakeExtVectorElt(llvm::Value *Vec, llvm::Constant *Elts,
269 unsigned Qualifiers) {
270 LValue R;
271 R.LVType = ExtVectorElt;
272 R.V = Vec;
273 R.VectorElts = Elts;
274 SetQualifiers(Qualifiers,R);
275 return R;
276 }
277
278 static LValue MakeBitfield(llvm::Value *V, unsigned short StartBit,
279 unsigned short Size, bool IsSigned,
280 unsigned Qualifiers) {
281 LValue R;
282 R.LVType = BitField;
283 R.V = V;
284 R.BitfieldData.StartBit = StartBit;
285 R.BitfieldData.Size = Size;
286 R.BitfieldData.IsSigned = IsSigned;
287 SetQualifiers(Qualifiers,R);
288 return R;
289 }
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000290
Mike Stumpf5408fe2009-05-16 07:57:57 +0000291 // FIXME: It is probably bad that we aren't emitting the target when we build
292 // the lvalue. However, this complicates the code a bit, and I haven't figured
293 // out how to make it go wrong yet.
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000294 static LValue MakePropertyRef(const ObjCPropertyRefExpr *E,
295 unsigned Qualifiers) {
296 LValue R;
297 R.LVType = PropertyRef;
298 R.PropertyRefExpr = E;
299 SetQualifiers(Qualifiers,R);
300 return R;
301 }
Chris Lattner1bd885e2009-02-16 22:25:49 +0000302
303 static LValue MakeKVCRef(const ObjCKVCRefExpr *E, unsigned Qualifiers) {
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000304 LValue R;
305 R.LVType = KVCRef;
306 R.KVCRefExpr = E;
307 SetQualifiers(Qualifiers,R);
308 return R;
309 }
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000310};
311
312} // end namespace CodeGen
313} // end namespace clang
314
315#endif