blob: 88429a3c8629dc7a63a4c2e23706934f23951aad [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- CGExpr.cpp - Emit LLVM Code from Expressions ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit Expr nodes as LLVM code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenFunction.h"
15#include "CodeGenModule.h"
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000016#include "CGCall.h"
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +000017#include "CGObjCRuntime.h"
Daniel Dunbarde7fb842008-08-11 05:00:27 +000018#include "clang/AST/ASTContext.h"
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000019#include "clang/AST/DeclObjC.h"
Eli Friedman316bb1b2008-05-17 20:03:47 +000020#include "llvm/Target/TargetData.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021using namespace clang;
22using namespace CodeGen;
23
24//===--------------------------------------------------------------------===//
25// Miscellaneous Helper Methods
26//===--------------------------------------------------------------------===//
27
28/// CreateTempAlloca - This creates a alloca and inserts it into the entry
29/// block.
30llvm::AllocaInst *CodeGenFunction::CreateTempAlloca(const llvm::Type *Ty,
Daniel Dunbar259e9cc2009-10-19 01:21:05 +000031 const llvm::Twine &Name) {
Chris Lattnerf1466842009-03-22 00:24:14 +000032 if (!Builder.isNamePreserving())
Daniel Dunbar259e9cc2009-10-19 01:21:05 +000033 return new llvm::AllocaInst(Ty, 0, "", AllocaInsertPt);
Devang Pateld35e2e02009-10-12 22:29:02 +000034 return new llvm::AllocaInst(Ty, 0, Name, AllocaInsertPt);
Reid Spencer5f016e22007-07-11 17:01:13 +000035}
36
37/// EvaluateExprAsBool - Perform the usual unary conversions on the specified
38/// expression and compare the result against zero, returning an Int1Ty value.
39llvm::Value *CodeGenFunction::EvaluateExprAsBool(const Expr *E) {
Chris Lattner9069fa22007-08-26 16:46:58 +000040 QualType BoolTy = getContext().BoolTy;
Chris Lattner9b2dc282008-04-04 16:54:41 +000041 if (!E->getType()->isAnyComplexType())
Chris Lattner9069fa22007-08-26 16:46:58 +000042 return EmitScalarConversion(EmitScalarExpr(E), E->getType(), BoolTy);
Reid Spencer5f016e22007-07-11 17:01:13 +000043
Chris Lattner9069fa22007-08-26 16:46:58 +000044 return EmitComplexToScalarConversion(EmitComplexExpr(E), E->getType(),BoolTy);
Reid Spencer5f016e22007-07-11 17:01:13 +000045}
46
Chris Lattner9b655512007-08-31 22:49:20 +000047/// EmitAnyExpr - Emit code to compute the specified expression which can have
48/// any type. The result is returned as an RValue struct. If this is an
Mike Stumpdb52dcd2009-09-09 13:00:44 +000049/// aggregate expression, the aggloc/agglocvolatile arguments indicate where the
50/// result should be returned.
51RValue CodeGenFunction::EmitAnyExpr(const Expr *E, llvm::Value *AggLoc,
Anders Carlsson14c5cbf2009-08-16 07:36:22 +000052 bool IsAggLocVolatile, bool IgnoreResult,
53 bool IsInitializer) {
Chris Lattner9b655512007-08-31 22:49:20 +000054 if (!hasAggregateLLVMType(E->getType()))
Mike Stump7f79f9b2009-05-29 15:46:01 +000055 return RValue::get(EmitScalarExpr(E, IgnoreResult));
Chris Lattner9b2dc282008-04-04 16:54:41 +000056 else if (E->getType()->isAnyComplexType())
Mike Stump7f79f9b2009-05-29 15:46:01 +000057 return RValue::getComplex(EmitComplexExpr(E, false, false,
58 IgnoreResult, IgnoreResult));
Mike Stumpdb52dcd2009-09-09 13:00:44 +000059
Anders Carlsson14c5cbf2009-08-16 07:36:22 +000060 EmitAggExpr(E, AggLoc, IsAggLocVolatile, IgnoreResult, IsInitializer);
61 return RValue::getAggregate(AggLoc, IsAggLocVolatile);
Chris Lattner9b655512007-08-31 22:49:20 +000062}
63
Mike Stumpdb52dcd2009-09-09 13:00:44 +000064/// EmitAnyExprToTemp - Similary to EmitAnyExpr(), however, the result will
65/// always be accessible even if no aggregate location is provided.
66RValue CodeGenFunction::EmitAnyExprToTemp(const Expr *E,
Anders Carlsson14c5cbf2009-08-16 07:36:22 +000067 bool IsAggLocVolatile,
68 bool IsInitializer) {
69 llvm::Value *AggLoc = 0;
Mike Stumpdb52dcd2009-09-09 13:00:44 +000070
71 if (hasAggregateLLVMType(E->getType()) &&
Daniel Dunbar46f45b92008-09-09 01:06:48 +000072 !E->getType()->isAnyComplexType())
73 AggLoc = CreateTempAlloca(ConvertType(E->getType()), "agg.tmp");
Mike Stumpdb52dcd2009-09-09 13:00:44 +000074 return EmitAnyExpr(E, AggLoc, IsAggLocVolatile, /*IgnoreResult=*/false,
Anders Carlsson14c5cbf2009-08-16 07:36:22 +000075 IsInitializer);
Daniel Dunbar46f45b92008-09-09 01:06:48 +000076}
77
Anders Carlsson4029ca72009-05-20 00:24:07 +000078RValue CodeGenFunction::EmitReferenceBindingToExpr(const Expr* E,
Anders Carlsson14c5cbf2009-08-16 07:36:22 +000079 QualType DestType,
80 bool IsInitializer) {
Anders Carlssone1b7ea12009-10-18 23:09:21 +000081 bool ShouldDestroyTemporaries = false;
82 unsigned OldNumLiveTemporaries = 0;
83
Anders Carlssonb3f74422009-10-15 00:51:46 +000084 if (const CXXExprWithTemporaries *TE = dyn_cast<CXXExprWithTemporaries>(E)) {
Anders Carlssone1b7ea12009-10-18 23:09:21 +000085 ShouldDestroyTemporaries = TE->shouldDestroyTemporaries();
86
Chris Lattnereb99b012009-10-28 17:39:19 +000087 // Keep track of the current cleanup stack depth.
88 if (ShouldDestroyTemporaries)
Anders Carlssone1b7ea12009-10-18 23:09:21 +000089 OldNumLiveTemporaries = LiveTemporaries.size();
Anders Carlssonb3f74422009-10-15 00:51:46 +000090
Anders Carlssone1b7ea12009-10-18 23:09:21 +000091 E = TE->getSubExpr();
Anders Carlssonb3f74422009-10-15 00:51:46 +000092 }
93
Eli Friedman5df0d422009-05-20 02:31:19 +000094 RValue Val;
95 if (E->isLvalue(getContext()) == Expr::LV_Valid) {
Anders Carlsson4bbab922009-05-20 00:36:58 +000096 // Emit the expr as an lvalue.
97 LValue LV = EmitLValue(E);
Eli Friedman5df0d422009-05-20 02:31:19 +000098 if (LV.isSimple())
99 return RValue::get(LV.getAddress());
100 Val = EmitLoadOfLValue(LV, E->getType());
Anders Carlssone1b7ea12009-10-18 23:09:21 +0000101
102 if (ShouldDestroyTemporaries) {
103 // Pop temporaries.
104 while (LiveTemporaries.size() > OldNumLiveTemporaries)
105 PopCXXTemporary();
106 }
Eli Friedman5df0d422009-05-20 02:31:19 +0000107 } else {
Anders Carlssonb3f74422009-10-15 00:51:46 +0000108 const CXXRecordDecl *BaseClassDecl = 0;
109 const CXXRecordDecl *DerivedClassDecl = 0;
110
111 if (const CastExpr *CE =
112 dyn_cast<CastExpr>(E->IgnoreParenNoopCasts(getContext()))) {
113 if (CE->getCastKind() == CastExpr::CK_DerivedToBase) {
114 E = CE->getSubExpr();
115
116 BaseClassDecl =
117 cast<CXXRecordDecl>(CE->getType()->getAs<RecordType>()->getDecl());
118 DerivedClassDecl =
119 cast<CXXRecordDecl>(E->getType()->getAs<RecordType>()->getDecl());
120 }
121 }
122
Anders Carlsson14c5cbf2009-08-16 07:36:22 +0000123 Val = EmitAnyExprToTemp(E, /*IsAggLocVolatile=*/false,
124 IsInitializer);
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000125
Anders Carlssone1b7ea12009-10-18 23:09:21 +0000126 if (ShouldDestroyTemporaries) {
127 // Pop temporaries.
128 while (LiveTemporaries.size() > OldNumLiveTemporaries)
129 PopCXXTemporary();
130 }
131
Anders Carlsson2da76932009-08-16 17:54:29 +0000132 if (IsInitializer) {
133 // We might have to destroy the temporary variable.
134 if (const RecordType *RT = E->getType()->getAs<RecordType>()) {
135 if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
136 if (!ClassDecl->hasTrivialDestructor()) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000137 const CXXDestructorDecl *Dtor =
Anders Carlsson2da76932009-08-16 17:54:29 +0000138 ClassDecl->getDestructor(getContext());
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000139
Douglas Gregord5782d52009-11-24 16:21:10 +0000140 DelayedCleanupBlock scope(*this);
Anders Carlsson2da76932009-08-16 17:54:29 +0000141 EmitCXXDestructorCall(Dtor, Dtor_Complete, Val.getAggregateAddr());
142 }
Anders Carlsson8478ce62009-08-16 17:50:25 +0000143 }
144 }
145 }
Anders Carlssonb3f74422009-10-15 00:51:46 +0000146
147 // Check if need to perform the derived-to-base cast.
148 if (BaseClassDecl) {
149 llvm::Value *Derived = Val.getAggregateAddr();
Anders Carlssonb3f74422009-10-15 00:51:46 +0000150 llvm::Value *Base =
Anders Carlssona3697c92009-11-23 17:57:54 +0000151 GetAddressOfBaseClass(Derived, DerivedClassDecl, BaseClassDecl,
152 /*NullCheckValue=*/false);
Anders Carlssonb3f74422009-10-15 00:51:46 +0000153 return RValue::get(Base);
154 }
Anders Carlsson4bbab922009-05-20 00:36:58 +0000155 }
Eli Friedman5df0d422009-05-20 02:31:19 +0000156
157 if (Val.isAggregate()) {
158 Val = RValue::get(Val.getAggregateAddr());
159 } else {
Anders Carlsson7cd3a642009-05-20 01:35:03 +0000160 // Create a temporary variable that we can bind the reference to.
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000161 llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(E->getType()),
Anders Carlssone04d1c72009-05-20 01:03:17 +0000162 "reftmp");
Eli Friedman5df0d422009-05-20 02:31:19 +0000163 if (Val.isScalar())
164 EmitStoreOfScalar(Val.getScalarVal(), Temp, false, E->getType());
165 else
166 StoreComplexToAddr(Val.getComplexVal(), Temp, false);
167 Val = RValue::get(Temp);
Anders Carlssone04d1c72009-05-20 01:03:17 +0000168 }
Eli Friedman5df0d422009-05-20 02:31:19 +0000169
170 return Val;
Anders Carlsson4029ca72009-05-20 00:24:07 +0000171}
172
173
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000174/// getAccessedFieldNo - Given an encoded value and a result number, return the
175/// input field number being accessed.
176unsigned CodeGenFunction::getAccessedFieldNo(unsigned Idx,
Dan Gohman4f8d1232008-05-22 00:50:06 +0000177 const llvm::Constant *Elts) {
178 if (isa<llvm::ConstantAggregateZero>(Elts))
179 return 0;
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000180
Dan Gohman4f8d1232008-05-22 00:50:06 +0000181 return cast<llvm::ConstantInt>(Elts->getOperand(Idx))->getZExtValue();
182}
183
Chris Lattner9b655512007-08-31 22:49:20 +0000184
Reid Spencer5f016e22007-07-11 17:01:13 +0000185//===----------------------------------------------------------------------===//
186// LValue Expression Emission
187//===----------------------------------------------------------------------===//
188
Daniel Dunbar13e81732009-02-05 07:09:07 +0000189RValue CodeGenFunction::GetUndefRValue(QualType Ty) {
Chris Lattnereb99b012009-10-28 17:39:19 +0000190 if (Ty->isVoidType())
Daniel Dunbar13e81732009-02-05 07:09:07 +0000191 return RValue::get(0);
Chris Lattnereb99b012009-10-28 17:39:19 +0000192
193 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
Daniel Dunbar8fa73ed2009-01-09 20:09:28 +0000194 const llvm::Type *EltTy = ConvertType(CTy->getElementType());
Owen Anderson03e20502009-07-30 23:11:26 +0000195 llvm::Value *U = llvm::UndefValue::get(EltTy);
Daniel Dunbar8fa73ed2009-01-09 20:09:28 +0000196 return RValue::getComplex(std::make_pair(U, U));
Chris Lattnereb99b012009-10-28 17:39:19 +0000197 }
198
199 if (hasAggregateLLVMType(Ty)) {
Owen Anderson96e0fc72009-07-29 22:16:19 +0000200 const llvm::Type *LTy = llvm::PointerType::getUnqual(ConvertType(Ty));
Owen Anderson03e20502009-07-30 23:11:26 +0000201 return RValue::getAggregate(llvm::UndefValue::get(LTy));
Daniel Dunbar8fa73ed2009-01-09 20:09:28 +0000202 }
Chris Lattnereb99b012009-10-28 17:39:19 +0000203
204 return RValue::get(llvm::UndefValue::get(ConvertType(Ty)));
Daniel Dunbarce1d38b2009-01-09 16:50:52 +0000205}
206
Daniel Dunbar13e81732009-02-05 07:09:07 +0000207RValue CodeGenFunction::EmitUnsupportedRValue(const Expr *E,
208 const char *Name) {
209 ErrorUnsupported(E, Name);
210 return GetUndefRValue(E->getType());
211}
212
Daniel Dunbar6ba82a42008-08-25 20:45:57 +0000213LValue CodeGenFunction::EmitUnsupportedLValue(const Expr *E,
214 const char *Name) {
215 ErrorUnsupported(E, Name);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000216 llvm::Type *Ty = llvm::PointerType::getUnqual(ConvertType(E->getType()));
Owen Anderson03e20502009-07-30 23:11:26 +0000217 return LValue::MakeAddr(llvm::UndefValue::get(Ty),
John McCall0953e762009-09-24 19:53:00 +0000218 MakeQualifiers(E->getType()));
Daniel Dunbar6ba82a42008-08-25 20:45:57 +0000219}
220
Reid Spencer5f016e22007-07-11 17:01:13 +0000221/// EmitLValue - Emit code to compute a designator that specifies the location
222/// of the expression.
223///
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000224/// This can return one of two things: a simple address or a bitfield reference.
225/// In either case, the LLVM Value* in the LValue structure is guaranteed to be
226/// an LLVM pointer type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000227///
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000228/// If this returns a bitfield reference, nothing about the pointee type of the
229/// LLVM value is known: For example, it may not be a pointer to an integer.
Reid Spencer5f016e22007-07-11 17:01:13 +0000230///
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000231/// If this returns a normal address, and if the lvalue's C type is fixed size,
232/// this method guarantees that the returned pointer type will point to an LLVM
233/// type of the same size of the lvalue's type. If the lvalue has a variable
234/// length type, this is not possible.
Reid Spencer5f016e22007-07-11 17:01:13 +0000235///
236LValue CodeGenFunction::EmitLValue(const Expr *E) {
237 switch (E->getStmtClass()) {
Daniel Dunbar6ba82a42008-08-25 20:45:57 +0000238 default: return EmitUnsupportedLValue(E, "l-value expression");
Reid Spencer5f016e22007-07-11 17:01:13 +0000239
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000240 case Expr::BinaryOperatorClass:
Daniel Dunbar80e62c22008-09-04 03:20:13 +0000241 return EmitBinaryOperatorLValue(cast<BinaryOperator>(E));
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000242 case Expr::CallExprClass:
Anders Carlssonfaf86642009-09-01 21:18:52 +0000243 case Expr::CXXMemberCallExprClass:
Douglas Gregorb4609802008-11-14 16:09:21 +0000244 case Expr::CXXOperatorCallExprClass:
245 return EmitCallExprLValue(cast<CallExpr>(E));
Daniel Dunbar5b5c9ef2009-02-11 20:59:32 +0000246 case Expr::VAArgExprClass:
247 return EmitVAArgExprLValue(cast<VAArgExpr>(E));
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000248 case Expr::DeclRefExprClass:
Douglas Gregor1a49af92009-01-06 05:10:23 +0000249 return EmitDeclRefLValue(cast<DeclRefExpr>(E));
Reid Spencer5f016e22007-07-11 17:01:13 +0000250 case Expr::ParenExprClass:return EmitLValue(cast<ParenExpr>(E)->getSubExpr());
Chris Lattnerd9f69102008-08-10 01:53:14 +0000251 case Expr::PredefinedExprClass:
252 return EmitPredefinedLValue(cast<PredefinedExpr>(E));
Reid Spencer5f016e22007-07-11 17:01:13 +0000253 case Expr::StringLiteralClass:
254 return EmitStringLiteralLValue(cast<StringLiteral>(E));
Chris Lattnereaf2bb82009-02-24 22:18:39 +0000255 case Expr::ObjCEncodeExprClass:
256 return EmitObjCEncodeExprLValue(cast<ObjCEncodeExpr>(E));
Chris Lattner391d77a2008-03-30 23:03:07 +0000257
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000258 case Expr::BlockDeclRefExprClass:
Mike Stumpa99038c2009-02-28 09:07:16 +0000259 return EmitBlockDeclRefLValue(cast<BlockDeclRefExpr>(E));
260
Anders Carlssonb58d0172009-05-30 23:23:33 +0000261 case Expr::CXXTemporaryObjectExprClass:
262 case Expr::CXXConstructExprClass:
Anders Carlssone61c9e82009-05-30 23:30:54 +0000263 return EmitCXXConstructLValue(cast<CXXConstructExpr>(E));
264 case Expr::CXXBindTemporaryExprClass:
265 return EmitCXXBindTemporaryLValue(cast<CXXBindTemporaryExpr>(E));
Anders Carlssonb9ea0b52009-09-14 01:10:45 +0000266 case Expr::CXXExprWithTemporariesClass:
267 return EmitCXXExprWithTemporariesLValue(cast<CXXExprWithTemporaries>(E));
Anders Carlsson370e5382009-11-14 01:51:50 +0000268 case Expr::CXXZeroInitValueExprClass:
269 return EmitNullInitializationLValue(cast<CXXZeroInitValueExpr>(E));
270 case Expr::CXXDefaultArgExprClass:
271 return EmitLValue(cast<CXXDefaultArgExpr>(E)->getExpr());
Mike Stumpc2e84ae2009-11-15 08:09:41 +0000272 case Expr::CXXTypeidExprClass:
273 return EmitCXXTypeidLValue(cast<CXXTypeidExpr>(E));
Anders Carlssone61c9e82009-05-30 23:30:54 +0000274
Daniel Dunbar0a04d772008-08-23 10:51:21 +0000275 case Expr::ObjCMessageExprClass:
276 return EmitObjCMessageExprLValue(cast<ObjCMessageExpr>(E));
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000277 case Expr::ObjCIvarRefExprClass:
Chris Lattner391d77a2008-03-30 23:03:07 +0000278 return EmitObjCIvarRefLValue(cast<ObjCIvarRefExpr>(E));
Daniel Dunbar6ba82a42008-08-25 20:45:57 +0000279 case Expr::ObjCPropertyRefExprClass:
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000280 return EmitObjCPropertyRefLValue(cast<ObjCPropertyRefExpr>(E));
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000281 case Expr::ObjCImplicitSetterGetterRefExprClass:
282 return EmitObjCKVCRefLValue(cast<ObjCImplicitSetterGetterRefExpr>(E));
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000283 case Expr::ObjCSuperExprClass:
Chris Lattner65459942009-04-25 19:35:26 +0000284 return EmitObjCSuperExprLValue(cast<ObjCSuperExpr>(E));
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000285
Chris Lattner65459942009-04-25 19:35:26 +0000286 case Expr::StmtExprClass:
287 return EmitStmtExprLValue(cast<StmtExpr>(E));
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000288 case Expr::UnaryOperatorClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000289 return EmitUnaryOpLValue(cast<UnaryOperator>(E));
290 case Expr::ArraySubscriptExprClass:
291 return EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E));
Nate Begeman213541a2008-04-18 23:10:10 +0000292 case Expr::ExtVectorElementExprClass:
293 return EmitExtVectorElementExpr(cast<ExtVectorElementExpr>(E));
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000294 case Expr::MemberExprClass:
Douglas Gregorbd4c4ae2009-08-26 22:36:53 +0000295 return EmitMemberExpr(cast<MemberExpr>(E));
Eli Friedman06e863f2008-05-13 23:18:27 +0000296 case Expr::CompoundLiteralExprClass:
297 return EmitCompoundLiteralLValue(cast<CompoundLiteralExpr>(E));
Daniel Dunbar90345582009-03-24 02:38:23 +0000298 case Expr::ConditionalOperatorClass:
Anders Carlsson6fcec8b2009-09-15 16:35:24 +0000299 return EmitConditionalOperatorLValue(cast<ConditionalOperator>(E));
Chris Lattner670a62c2008-12-12 05:35:08 +0000300 case Expr::ChooseExprClass:
Eli Friedman79769322009-03-04 05:52:32 +0000301 return EmitLValue(cast<ChooseExpr>(E)->getChosenSubExpr(getContext()));
Chris Lattnerc3953a62009-03-18 04:02:57 +0000302 case Expr::ImplicitCastExprClass:
303 case Expr::CStyleCastExprClass:
304 case Expr::CXXFunctionalCastExprClass:
305 case Expr::CXXStaticCastExprClass:
306 case Expr::CXXDynamicCastExprClass:
307 case Expr::CXXReinterpretCastExprClass:
308 case Expr::CXXConstCastExprClass:
Chris Lattner75dfeda2009-03-18 18:28:57 +0000309 return EmitCastLValue(cast<CastExpr>(E));
Reid Spencer5f016e22007-07-11 17:01:13 +0000310 }
311}
312
Daniel Dunbar9d9cc872009-02-10 00:57:50 +0000313llvm::Value *CodeGenFunction::EmitLoadOfScalar(llvm::Value *Addr, bool Volatile,
314 QualType Ty) {
315 llvm::Value *V = Builder.CreateLoad(Addr, Volatile, "tmp");
316
Anders Carlsson3bb423b2009-05-19 19:36:19 +0000317 // Bool can have different representation in memory than in registers.
Daniel Dunbar9d9cc872009-02-10 00:57:50 +0000318 if (Ty->isBooleanType())
Owen Anderson0032b272009-08-13 21:57:51 +0000319 if (V->getType() != llvm::Type::getInt1Ty(VMContext))
320 V = Builder.CreateTrunc(V, llvm::Type::getInt1Ty(VMContext), "tobool");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000321
Daniel Dunbar9d9cc872009-02-10 00:57:50 +0000322 return V;
323}
324
325void CodeGenFunction::EmitStoreOfScalar(llvm::Value *Value, llvm::Value *Addr,
Anders Carlssonb4aa4662009-05-19 18:50:41 +0000326 bool Volatile, QualType Ty) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000327
Anders Carlsson3bb423b2009-05-19 19:36:19 +0000328 if (Ty->isBooleanType()) {
329 // Bool can have different representation in memory than in registers.
330 const llvm::Type *SrcTy = Value->getType();
331 const llvm::PointerType *DstPtr = cast<llvm::PointerType>(Addr->getType());
332 if (DstPtr->getElementType() != SrcTy) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000333 const llvm::Type *MemTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000334 llvm::PointerType::get(SrcTy, DstPtr->getAddressSpace());
Anders Carlsson3bb423b2009-05-19 19:36:19 +0000335 Addr = Builder.CreateBitCast(Addr, MemTy, "storetmp");
336 }
Daniel Dunbar9d9cc872009-02-10 00:57:50 +0000337 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000338 Builder.CreateStore(Value, Addr, Volatile);
Daniel Dunbar9d9cc872009-02-10 00:57:50 +0000339}
340
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000341/// EmitLoadOfLValue - Given an expression that represents a value lvalue, this
342/// method emits the address of the lvalue, then loads the result as an rvalue,
343/// returning the rvalue.
Reid Spencer5f016e22007-07-11 17:01:13 +0000344RValue CodeGenFunction::EmitLoadOfLValue(LValue LV, QualType ExprType) {
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +0000345 if (LV.isObjCWeak()) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000346 // load of a __weak object.
Fariborz Jahanian6dc23172008-11-18 21:45:40 +0000347 llvm::Value *AddrWeakObj = LV.getAddress();
Chris Lattnereb99b012009-10-28 17:39:19 +0000348 return RValue::get(CGM.getObjCRuntime().EmitObjCWeakRead(*this,
349 AddrWeakObj));
Fariborz Jahanian6dc23172008-11-18 21:45:40 +0000350 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000351
Reid Spencer5f016e22007-07-11 17:01:13 +0000352 if (LV.isSimple()) {
353 llvm::Value *Ptr = LV.getAddress();
354 const llvm::Type *EltTy =
355 cast<llvm::PointerType>(Ptr->getType())->getElementType();
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000356
Reid Spencer5f016e22007-07-11 17:01:13 +0000357 // Simple scalar l-value.
Daniel Dunbar9d9cc872009-02-10 00:57:50 +0000358 if (EltTy->isSingleValueType())
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000359 return RValue::get(EmitLoadOfScalar(Ptr, LV.isVolatileQualified(),
Daniel Dunbar9d9cc872009-02-10 00:57:50 +0000360 ExprType));
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000361
Chris Lattner883f6a72007-08-11 00:04:45 +0000362 assert(ExprType->isFunctionType() && "Unknown scalar value");
363 return RValue::get(Ptr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000364 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000365
Reid Spencer5f016e22007-07-11 17:01:13 +0000366 if (LV.isVectorElt()) {
Eli Friedman1e692ac2008-06-13 23:01:12 +0000367 llvm::Value *Vec = Builder.CreateLoad(LV.getVectorAddr(),
368 LV.isVolatileQualified(), "tmp");
Reid Spencer5f016e22007-07-11 17:01:13 +0000369 return RValue::get(Builder.CreateExtractElement(Vec, LV.getVectorIdx(),
370 "vecext"));
371 }
Chris Lattner46ea8eb2007-08-03 00:16:29 +0000372
373 // If this is a reference to a subset of the elements of a vector, either
374 // shuffle the input or extract/insert them as appropriate.
Nate Begeman213541a2008-04-18 23:10:10 +0000375 if (LV.isExtVectorElt())
376 return EmitLoadOfExtVectorElementLValue(LV, ExprType);
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000377
378 if (LV.isBitfield())
379 return EmitLoadOfBitfieldLValue(LV, ExprType);
380
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000381 if (LV.isPropertyRef())
382 return EmitLoadOfPropertyRefLValue(LV, ExprType);
383
Chris Lattner73525de2009-02-16 21:11:58 +0000384 assert(LV.isKVCRef() && "Unknown LValue type!");
385 return EmitLoadOfKVCRefLValue(LV, ExprType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000386}
387
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000388RValue CodeGenFunction::EmitLoadOfBitfieldLValue(LValue LV,
389 QualType ExprType) {
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000390 unsigned StartBit = LV.getBitfieldStartBit();
391 unsigned BitfieldSize = LV.getBitfieldSize();
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000392 llvm::Value *Ptr = LV.getBitfieldAddr();
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000393
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000394 const llvm::Type *EltTy =
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000395 cast<llvm::PointerType>(Ptr->getType())->getElementType();
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000396 unsigned EltTySize = CGM.getTargetData().getTypeSizeInBits(EltTy);
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000397
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000398 // In some cases the bitfield may straddle two memory locations. Currently we
399 // load the entire bitfield, then do the magic to sign-extend it if
400 // necessary. This results in somewhat more code than necessary for the common
401 // case (one load), since two shifts accomplish both the masking and sign
402 // extension.
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000403 unsigned LowBits = std::min(BitfieldSize, EltTySize - StartBit);
404 llvm::Value *Val = Builder.CreateLoad(Ptr, LV.isVolatileQualified(), "tmp");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000405
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000406 // Shift to proper location.
Daniel Dunbarf3edc2f2008-11-13 02:20:34 +0000407 if (StartBit)
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000408 Val = Builder.CreateLShr(Val, llvm::ConstantInt::get(EltTy, StartBit),
Daniel Dunbarf3edc2f2008-11-13 02:20:34 +0000409 "bf.lo");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000410
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000411 // Mask off unused bits.
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000412 llvm::Constant *LowMask = llvm::ConstantInt::get(VMContext,
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000413 llvm::APInt::getLowBitsSet(EltTySize, LowBits));
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000414 Val = Builder.CreateAnd(Val, LowMask, "bf.lo.cleared");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000415
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000416 // Fetch the high bits if necessary.
417 if (LowBits < BitfieldSize) {
418 unsigned HighBits = BitfieldSize - LowBits;
Owen Anderson0032b272009-08-13 21:57:51 +0000419 llvm::Value *HighPtr = Builder.CreateGEP(Ptr, llvm::ConstantInt::get(
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000420 llvm::Type::getInt32Ty(VMContext), 1), "bf.ptr.hi");
421 llvm::Value *HighVal = Builder.CreateLoad(HighPtr,
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000422 LV.isVolatileQualified(),
423 "tmp");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000424
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000425 // Mask off unused bits.
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000426 llvm::Constant *HighMask = llvm::ConstantInt::get(VMContext,
427 llvm::APInt::getLowBitsSet(EltTySize, HighBits));
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000428 HighVal = Builder.CreateAnd(HighVal, HighMask, "bf.lo.cleared");
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000429
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000430 // Shift to proper location and or in to bitfield value.
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000431 HighVal = Builder.CreateShl(HighVal,
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000432 llvm::ConstantInt::get(EltTy, LowBits));
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000433 Val = Builder.CreateOr(Val, HighVal, "bf.val");
434 }
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000435
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000436 // Sign extend if necessary.
437 if (LV.isBitfieldSigned()) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000438 llvm::Value *ExtraBits = llvm::ConstantInt::get(EltTy,
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000439 EltTySize - BitfieldSize);
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000440 Val = Builder.CreateAShr(Builder.CreateShl(Val, ExtraBits),
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000441 ExtraBits, "bf.val.sext");
442 }
Eli Friedman316bb1b2008-05-17 20:03:47 +0000443
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000444 // The bitfield type and the normal type differ when the storage sizes differ
445 // (currently just _Bool).
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000446 Val = Builder.CreateIntCast(Val, ConvertType(ExprType), false, "tmp");
Eli Friedman316bb1b2008-05-17 20:03:47 +0000447
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000448 return RValue::get(Val);
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000449}
450
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000451RValue CodeGenFunction::EmitLoadOfPropertyRefLValue(LValue LV,
452 QualType ExprType) {
453 return EmitObjCPropertyGet(LV.getPropertyRefExpr());
454}
455
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000456RValue CodeGenFunction::EmitLoadOfKVCRefLValue(LValue LV,
457 QualType ExprType) {
458 return EmitObjCPropertyGet(LV.getKVCRefExpr());
459}
460
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000461// If this is a reference to a subset of the elements of a vector, create an
462// appropriate shufflevector.
Nate Begeman213541a2008-04-18 23:10:10 +0000463RValue CodeGenFunction::EmitLoadOfExtVectorElementLValue(LValue LV,
464 QualType ExprType) {
Eli Friedman1e692ac2008-06-13 23:01:12 +0000465 llvm::Value *Vec = Builder.CreateLoad(LV.getExtVectorAddr(),
466 LV.isVolatileQualified(), "tmp");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000467
Nate Begeman8a997642008-05-09 06:41:27 +0000468 const llvm::Constant *Elts = LV.getExtVectorElts();
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000469
470 // If the result of the expression is a non-vector type, we must be extracting
471 // a single element. Just codegen as an extractelement.
John McCall183700f2009-09-21 23:43:11 +0000472 const VectorType *ExprVT = ExprType->getAs<VectorType>();
Chris Lattnercf60cd22007-08-10 17:10:08 +0000473 if (!ExprVT) {
Dan Gohman4f8d1232008-05-22 00:50:06 +0000474 unsigned InIdx = getAccessedFieldNo(0, Elts);
Owen Anderson0032b272009-08-13 21:57:51 +0000475 llvm::Value *Elt = llvm::ConstantInt::get(
476 llvm::Type::getInt32Ty(VMContext), InIdx);
Chris Lattner34cdc862007-08-03 16:18:34 +0000477 return RValue::get(Builder.CreateExtractElement(Vec, Elt, "tmp"));
478 }
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000479
480 // Always use shuffle vector to try to retain the original program structure
Chris Lattnercf60cd22007-08-10 17:10:08 +0000481 unsigned NumResultElts = ExprVT->getNumElements();
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000482
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000483 llvm::SmallVector<llvm::Constant*, 4> Mask;
Chris Lattner34cdc862007-08-03 16:18:34 +0000484 for (unsigned i = 0; i != NumResultElts; ++i) {
Dan Gohman4f8d1232008-05-22 00:50:06 +0000485 unsigned InIdx = getAccessedFieldNo(i, Elts);
Owen Anderson0032b272009-08-13 21:57:51 +0000486 Mask.push_back(llvm::ConstantInt::get(
487 llvm::Type::getInt32Ty(VMContext), InIdx));
Chris Lattner34cdc862007-08-03 16:18:34 +0000488 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000489
Owen Anderson4a289322009-07-28 21:22:35 +0000490 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000491 Vec = Builder.CreateShuffleVector(Vec,
Owen Anderson03e20502009-07-30 23:11:26 +0000492 llvm::UndefValue::get(Vec->getType()),
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000493 MaskV, "tmp");
494 return RValue::get(Vec);
Chris Lattner34cdc862007-08-03 16:18:34 +0000495}
496
497
Reid Spencer5f016e22007-07-11 17:01:13 +0000498
499/// EmitStoreThroughLValue - Store the specified rvalue into the specified
500/// lvalue, where both are guaranteed to the have the same type, and that type
501/// is 'Ty'.
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000502void CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst,
Reid Spencer5f016e22007-07-11 17:01:13 +0000503 QualType Ty) {
Chris Lattner017d6aa2007-08-03 16:28:33 +0000504 if (!Dst.isSimple()) {
505 if (Dst.isVectorElt()) {
506 // Read/modify/write the vector, inserting the new element.
Eli Friedman1e692ac2008-06-13 23:01:12 +0000507 llvm::Value *Vec = Builder.CreateLoad(Dst.getVectorAddr(),
508 Dst.isVolatileQualified(), "tmp");
Chris Lattner9b655512007-08-31 22:49:20 +0000509 Vec = Builder.CreateInsertElement(Vec, Src.getScalarVal(),
Chris Lattner017d6aa2007-08-03 16:28:33 +0000510 Dst.getVectorIdx(), "vecins");
Eli Friedman1e692ac2008-06-13 23:01:12 +0000511 Builder.CreateStore(Vec, Dst.getVectorAddr(),Dst.isVolatileQualified());
Chris Lattner017d6aa2007-08-03 16:28:33 +0000512 return;
513 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000514
Nate Begeman213541a2008-04-18 23:10:10 +0000515 // If this is an update of extended vector elements, insert them as
516 // appropriate.
517 if (Dst.isExtVectorElt())
518 return EmitStoreThroughExtVectorComponentLValue(Src, Dst, Ty);
Lauro Ramos Venancioa0c5d0e2008-01-22 22:36:45 +0000519
520 if (Dst.isBitfield())
521 return EmitStoreThroughBitfieldLValue(Src, Dst, Ty);
522
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000523 if (Dst.isPropertyRef())
524 return EmitStoreThroughPropertyRefLValue(Src, Dst, Ty);
525
Chris Lattnereb99b012009-10-28 17:39:19 +0000526 assert(Dst.isKVCRef() && "Unknown LValue type");
527 return EmitStoreThroughKVCRefLValue(Src, Dst, Ty);
Chris Lattner017d6aa2007-08-03 16:28:33 +0000528 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000529
Fariborz Jahanian4f676ed2009-02-21 00:30:43 +0000530 if (Dst.isObjCWeak() && !Dst.isNonGC()) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000531 // load of a __weak object.
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +0000532 llvm::Value *LvalueDst = Dst.getAddress();
533 llvm::Value *src = Src.getScalarVal();
Mike Stumpf33651c2009-04-14 00:57:29 +0000534 CGM.getObjCRuntime().EmitObjCWeakAssign(*this, src, LvalueDst);
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +0000535 return;
536 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000537
Fariborz Jahanian4f676ed2009-02-21 00:30:43 +0000538 if (Dst.isObjCStrong() && !Dst.isNonGC()) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000539 // load of a __strong object.
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +0000540 llvm::Value *LvalueDst = Dst.getAddress();
541 llvm::Value *src = Src.getScalarVal();
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000542 if (Dst.isObjCIvar()) {
543 assert(Dst.getBaseIvarExp() && "BaseIvarExp is NULL");
544 const llvm::Type *ResultType = ConvertType(getContext().LongTy);
545 llvm::Value *RHS = EmitScalarExpr(Dst.getBaseIvarExp());
Fariborz Jahanian76368e82009-09-25 00:00:20 +0000546 llvm::Value *dst = RHS;
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000547 RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast");
548 llvm::Value *LHS =
549 Builder.CreatePtrToInt(LvalueDst, ResultType, "sub.ptr.lhs.cast");
550 llvm::Value *BytesBetween = Builder.CreateSub(LHS, RHS, "ivar.offset");
Fariborz Jahanian76368e82009-09-25 00:00:20 +0000551 CGM.getObjCRuntime().EmitObjCIvarAssign(*this, src, dst,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000552 BytesBetween);
Chris Lattnereb99b012009-10-28 17:39:19 +0000553 } else if (Dst.isGlobalObjCRef())
Fariborz Jahanianbf63b872009-05-04 23:27:20 +0000554 CGM.getObjCRuntime().EmitObjCGlobalAssign(*this, src, LvalueDst);
555 else
556 CGM.getObjCRuntime().EmitObjCStrongCastAssign(*this, src, LvalueDst);
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +0000557 return;
558 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000559
Chris Lattner883f6a72007-08-11 00:04:45 +0000560 assert(Src.isScalar() && "Can't emit an agg store with this method");
Anders Carlssonb4aa4662009-05-19 18:50:41 +0000561 EmitStoreOfScalar(Src.getScalarVal(), Dst.getAddress(),
562 Dst.isVolatileQualified(), Ty);
Reid Spencer5f016e22007-07-11 17:01:13 +0000563}
564
Lauro Ramos Venancioa0c5d0e2008-01-22 22:36:45 +0000565void CodeGenFunction::EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst,
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000566 QualType Ty,
Daniel Dunbared3849b2008-11-19 09:36:46 +0000567 llvm::Value **Result) {
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000568 unsigned StartBit = Dst.getBitfieldStartBit();
569 unsigned BitfieldSize = Dst.getBitfieldSize();
Lauro Ramos Venancioa0c5d0e2008-01-22 22:36:45 +0000570 llvm::Value *Ptr = Dst.getBitfieldAddr();
Lauro Ramos Venancioa0c5d0e2008-01-22 22:36:45 +0000571
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000572 const llvm::Type *EltTy =
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000573 cast<llvm::PointerType>(Ptr->getType())->getElementType();
574 unsigned EltTySize = CGM.getTargetData().getTypeSizeInBits(EltTy);
575
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000576 // Get the new value, cast to the appropriate type and masked to exactly the
577 // size of the bit-field.
Daniel Dunbared3849b2008-11-19 09:36:46 +0000578 llvm::Value *SrcVal = Src.getScalarVal();
579 llvm::Value *NewVal = Builder.CreateIntCast(SrcVal, EltTy, false, "tmp");
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000580 llvm::Constant *Mask = llvm::ConstantInt::get(VMContext,
581 llvm::APInt::getLowBitsSet(EltTySize, BitfieldSize));
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000582 NewVal = Builder.CreateAnd(NewVal, Mask, "bf.value");
Lauro Ramos Venancioa0c5d0e2008-01-22 22:36:45 +0000583
Daniel Dunbared3849b2008-11-19 09:36:46 +0000584 // Return the new value of the bit-field, if requested.
585 if (Result) {
586 // Cast back to the proper type for result.
587 const llvm::Type *SrcTy = SrcVal->getType();
588 llvm::Value *SrcTrunc = Builder.CreateIntCast(NewVal, SrcTy, false,
589 "bf.reload.val");
590
591 // Sign extend if necessary.
592 if (Dst.isBitfieldSigned()) {
593 unsigned SrcTySize = CGM.getTargetData().getTypeSizeInBits(SrcTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000594 llvm::Value *ExtraBits = llvm::ConstantInt::get(SrcTy,
Daniel Dunbared3849b2008-11-19 09:36:46 +0000595 SrcTySize - BitfieldSize);
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000596 SrcTrunc = Builder.CreateAShr(Builder.CreateShl(SrcTrunc, ExtraBits),
Daniel Dunbared3849b2008-11-19 09:36:46 +0000597 ExtraBits, "bf.reload.sext");
598 }
599
600 *Result = SrcTrunc;
601 }
602
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000603 // In some cases the bitfield may straddle two memory locations. Emit the low
604 // part first and check to see if the high needs to be done.
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000605 unsigned LowBits = std::min(BitfieldSize, EltTySize - StartBit);
606 llvm::Value *LowVal = Builder.CreateLoad(Ptr, Dst.isVolatileQualified(),
607 "bf.prev.low");
Eli Friedman316bb1b2008-05-17 20:03:47 +0000608
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000609 // Compute the mask for zero-ing the low part of this bitfield.
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000610 llvm::Constant *InvMask =
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000611 llvm::ConstantInt::get(VMContext,
612 ~llvm::APInt::getBitsSet(EltTySize, StartBit, StartBit + LowBits));
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000613
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000614 // Compute the new low part as
615 // LowVal = (LowVal & InvMask) | (NewVal << StartBit),
616 // with the shift of NewVal implicitly stripping the high bits.
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000617 llvm::Value *NewLowVal =
618 Builder.CreateShl(NewVal, llvm::ConstantInt::get(EltTy, StartBit),
619 "bf.value.lo");
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000620 LowVal = Builder.CreateAnd(LowVal, InvMask, "bf.prev.lo.cleared");
621 LowVal = Builder.CreateOr(LowVal, NewLowVal, "bf.new.lo");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000622
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000623 // Write back.
624 Builder.CreateStore(LowVal, Ptr, Dst.isVolatileQualified());
Eli Friedman316bb1b2008-05-17 20:03:47 +0000625
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000626 // If the low part doesn't cover the bitfield emit a high part.
627 if (LowBits < BitfieldSize) {
628 unsigned HighBits = BitfieldSize - LowBits;
Owen Anderson0032b272009-08-13 21:57:51 +0000629 llvm::Value *HighPtr = Builder.CreateGEP(Ptr, llvm::ConstantInt::get(
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000630 llvm::Type::getInt32Ty(VMContext), 1), "bf.ptr.hi");
631 llvm::Value *HighVal = Builder.CreateLoad(HighPtr,
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000632 Dst.isVolatileQualified(),
633 "bf.prev.hi");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000634
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000635 // Compute the mask for zero-ing the high part of this bitfield.
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000636 llvm::Constant *InvMask =
637 llvm::ConstantInt::get(VMContext, ~llvm::APInt::getLowBitsSet(EltTySize,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000638 HighBits));
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000639
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000640 // Compute the new high part as
641 // HighVal = (HighVal & InvMask) | (NewVal lshr LowBits),
642 // where the high bits of NewVal have already been cleared and the
643 // shift stripping the low bits.
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000644 llvm::Value *NewHighVal =
645 Builder.CreateLShr(NewVal, llvm::ConstantInt::get(EltTy, LowBits),
646 "bf.value.high");
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000647 HighVal = Builder.CreateAnd(HighVal, InvMask, "bf.prev.hi.cleared");
648 HighVal = Builder.CreateOr(HighVal, NewHighVal, "bf.new.hi");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000649
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000650 // Write back.
651 Builder.CreateStore(HighVal, HighPtr, Dst.isVolatileQualified());
652 }
Lauro Ramos Venancioa0c5d0e2008-01-22 22:36:45 +0000653}
654
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000655void CodeGenFunction::EmitStoreThroughPropertyRefLValue(RValue Src,
656 LValue Dst,
657 QualType Ty) {
658 EmitObjCPropertySet(Dst.getPropertyRefExpr(), Src);
659}
660
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000661void CodeGenFunction::EmitStoreThroughKVCRefLValue(RValue Src,
662 LValue Dst,
663 QualType Ty) {
664 EmitObjCPropertySet(Dst.getKVCRefExpr(), Src);
665}
666
Nate Begeman213541a2008-04-18 23:10:10 +0000667void CodeGenFunction::EmitStoreThroughExtVectorComponentLValue(RValue Src,
668 LValue Dst,
669 QualType Ty) {
Chris Lattner017d6aa2007-08-03 16:28:33 +0000670 // This access turns into a read/modify/write of the vector. Load the input
671 // value now.
Eli Friedman1e692ac2008-06-13 23:01:12 +0000672 llvm::Value *Vec = Builder.CreateLoad(Dst.getExtVectorAddr(),
673 Dst.isVolatileQualified(), "tmp");
Nate Begeman8a997642008-05-09 06:41:27 +0000674 const llvm::Constant *Elts = Dst.getExtVectorElts();
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000675
Chris Lattner9b655512007-08-31 22:49:20 +0000676 llvm::Value *SrcVal = Src.getScalarVal();
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000677
John McCall183700f2009-09-21 23:43:11 +0000678 if (const VectorType *VTy = Ty->getAs<VectorType>()) {
Chris Lattner7e6b51b2007-08-03 16:37:04 +0000679 unsigned NumSrcElts = VTy->getNumElements();
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000680 unsigned NumDstElts =
681 cast<llvm::VectorType>(Vec->getType())->getNumElements();
682 if (NumDstElts == NumSrcElts) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000683 // Use shuffle vector is the src and destination are the same number of
684 // elements and restore the vector mask since it is on the side it will be
685 // stored.
Nate Begeman6e5dd862009-06-26 21:12:50 +0000686 llvm::SmallVector<llvm::Constant*, 4> Mask(NumDstElts);
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000687 for (unsigned i = 0; i != NumSrcElts; ++i) {
688 unsigned InIdx = getAccessedFieldNo(i, Elts);
Owen Anderson0032b272009-08-13 21:57:51 +0000689 Mask[InIdx] = llvm::ConstantInt::get(
690 llvm::Type::getInt32Ty(VMContext), i);
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000691 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000692
Owen Anderson4a289322009-07-28 21:22:35 +0000693 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000694 Vec = Builder.CreateShuffleVector(SrcVal,
Owen Anderson03e20502009-07-30 23:11:26 +0000695 llvm::UndefValue::get(Vec->getType()),
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000696 MaskV, "tmp");
Mike Stumpb3589f42009-07-30 22:28:39 +0000697 } else if (NumDstElts > NumSrcElts) {
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000698 // Extended the source vector to the same length and then shuffle it
699 // into the destination.
700 // FIXME: since we're shuffling with undef, can we just use the indices
701 // into that? This could be simpler.
702 llvm::SmallVector<llvm::Constant*, 4> ExtMask;
Chris Lattnereb99b012009-10-28 17:39:19 +0000703 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000704 unsigned i;
705 for (i = 0; i != NumSrcElts; ++i)
Chris Lattnereb99b012009-10-28 17:39:19 +0000706 ExtMask.push_back(llvm::ConstantInt::get(Int32Ty, i));
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000707 for (; i != NumDstElts; ++i)
Chris Lattnereb99b012009-10-28 17:39:19 +0000708 ExtMask.push_back(llvm::UndefValue::get(Int32Ty));
Owen Anderson4a289322009-07-28 21:22:35 +0000709 llvm::Value *ExtMaskV = llvm::ConstantVector::get(&ExtMask[0],
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000710 ExtMask.size());
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000711 llvm::Value *ExtSrcVal =
Daniel Dunbarbb767732009-02-17 18:31:04 +0000712 Builder.CreateShuffleVector(SrcVal,
Owen Anderson03e20502009-07-30 23:11:26 +0000713 llvm::UndefValue::get(SrcVal->getType()),
Daniel Dunbarbb767732009-02-17 18:31:04 +0000714 ExtMaskV, "tmp");
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000715 // build identity
716 llvm::SmallVector<llvm::Constant*, 4> Mask;
Chris Lattnereb99b012009-10-28 17:39:19 +0000717 for (unsigned i = 0; i != NumDstElts; ++i)
718 Mask.push_back(llvm::ConstantInt::get(Int32Ty, i));
719
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000720 // modify when what gets shuffled in
721 for (unsigned i = 0; i != NumSrcElts; ++i) {
722 unsigned Idx = getAccessedFieldNo(i, Elts);
Chris Lattnereb99b012009-10-28 17:39:19 +0000723 Mask[Idx] = llvm::ConstantInt::get(Int32Ty, i+NumDstElts);
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000724 }
Owen Anderson4a289322009-07-28 21:22:35 +0000725 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000726 Vec = Builder.CreateShuffleVector(Vec, ExtSrcVal, MaskV, "tmp");
Mike Stumpb3589f42009-07-30 22:28:39 +0000727 } else {
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000728 // We should never shorten the vector
729 assert(0 && "unexpected shorten vector length");
Chris Lattner7e6b51b2007-08-03 16:37:04 +0000730 }
731 } else {
732 // If the Src is a scalar (not a vector) it must be updating one element.
Dan Gohman4f8d1232008-05-22 00:50:06 +0000733 unsigned InIdx = getAccessedFieldNo(0, Elts);
Chris Lattnereb99b012009-10-28 17:39:19 +0000734 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
735 llvm::Value *Elt = llvm::ConstantInt::get(Int32Ty, InIdx);
Chris Lattner017d6aa2007-08-03 16:28:33 +0000736 Vec = Builder.CreateInsertElement(Vec, SrcVal, Elt, "tmp");
Chris Lattner017d6aa2007-08-03 16:28:33 +0000737 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000738
Eli Friedman1e692ac2008-06-13 23:01:12 +0000739 Builder.CreateStore(Vec, Dst.getExtVectorAddr(), Dst.isVolatileQualified());
Chris Lattner017d6aa2007-08-03 16:28:33 +0000740}
741
Fariborz Jahanianb123ea32009-09-16 21:37:16 +0000742// setObjCGCLValueClass - sets class of he lvalue for the purpose of
743// generating write-barries API. It is currently a global, ivar,
744// or neither.
Chris Lattnereb99b012009-10-28 17:39:19 +0000745static void setObjCGCLValueClass(const ASTContext &Ctx, const Expr *E,
746 LValue &LV) {
Fariborz Jahanianb9242592009-09-21 23:03:37 +0000747 if (Ctx.getLangOptions().getGCMode() == LangOptions::NonGC)
Fariborz Jahanianb123ea32009-09-16 21:37:16 +0000748 return;
749
Fariborz Jahaniandbf3cfd2009-09-16 23:11:23 +0000750 if (isa<ObjCIvarRefExpr>(E)) {
751 LV.SetObjCIvar(LV, true);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000752 ObjCIvarRefExpr *Exp = cast<ObjCIvarRefExpr>(const_cast<Expr*>(E));
753 LV.setBaseIvarExp(Exp->getBase());
Fariborz Jahanianfd02ed72009-09-21 18:54:29 +0000754 LV.SetObjCArray(LV, E->getType()->isArrayType());
Fariborz Jahaniandbf3cfd2009-09-16 23:11:23 +0000755 return;
756 }
Chris Lattnereb99b012009-10-28 17:39:19 +0000757
Fariborz Jahanianb123ea32009-09-16 21:37:16 +0000758 if (const DeclRefExpr *Exp = dyn_cast<DeclRefExpr>(E)) {
759 if (const VarDecl *VD = dyn_cast<VarDecl>(Exp->getDecl())) {
760 if ((VD->isBlockVarDecl() && !VD->hasLocalStorage()) ||
761 VD->isFileVarDecl())
762 LV.SetGlobalObjCRef(LV, true);
763 }
Fariborz Jahanianfd02ed72009-09-21 18:54:29 +0000764 LV.SetObjCArray(LV, E->getType()->isArrayType());
Chris Lattnereb99b012009-10-28 17:39:19 +0000765 return;
Fariborz Jahanianb123ea32009-09-16 21:37:16 +0000766 }
Chris Lattnereb99b012009-10-28 17:39:19 +0000767
768 if (const UnaryOperator *Exp = dyn_cast<UnaryOperator>(E)) {
Fariborz Jahanianb123ea32009-09-16 21:37:16 +0000769 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
Chris Lattnereb99b012009-10-28 17:39:19 +0000770 return;
771 }
772
773 if (const ParenExpr *Exp = dyn_cast<ParenExpr>(E)) {
Fariborz Jahanianb123ea32009-09-16 21:37:16 +0000774 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
Fariborz Jahanian75b08f12009-09-30 17:10:29 +0000775 if (LV.isObjCIvar()) {
776 // If cast is to a structure pointer, follow gcc's behavior and make it
777 // a non-ivar write-barrier.
778 QualType ExpTy = E->getType();
779 if (ExpTy->isPointerType())
780 ExpTy = ExpTy->getAs<PointerType>()->getPointeeType();
781 if (ExpTy->isRecordType())
782 LV.SetObjCIvar(LV, false);
Chris Lattnereb99b012009-10-28 17:39:19 +0000783 }
784 return;
Fariborz Jahanian75b08f12009-09-30 17:10:29 +0000785 }
Chris Lattnereb99b012009-10-28 17:39:19 +0000786 if (const ImplicitCastExpr *Exp = dyn_cast<ImplicitCastExpr>(E)) {
Fariborz Jahanianb123ea32009-09-16 21:37:16 +0000787 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
Chris Lattnereb99b012009-10-28 17:39:19 +0000788 return;
789 }
790
791 if (const CStyleCastExpr *Exp = dyn_cast<CStyleCastExpr>(E)) {
Fariborz Jahanianb123ea32009-09-16 21:37:16 +0000792 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
Chris Lattnereb99b012009-10-28 17:39:19 +0000793 return;
794 }
795
796 if (const ArraySubscriptExpr *Exp = dyn_cast<ArraySubscriptExpr>(E)) {
Fariborz Jahanianb123ea32009-09-16 21:37:16 +0000797 setObjCGCLValueClass(Ctx, Exp->getBase(), LV);
Fariborz Jahanianfd02ed72009-09-21 18:54:29 +0000798 if (LV.isObjCIvar() && !LV.isObjCArray())
Fariborz Jahanian1c1afc42009-09-18 00:04:00 +0000799 // Using array syntax to assigning to what an ivar points to is not
800 // same as assigning to the ivar itself. {id *Names;} Names[i] = 0;
801 LV.SetObjCIvar(LV, false);
Fariborz Jahanianfd02ed72009-09-21 18:54:29 +0000802 else if (LV.isGlobalObjCRef() && !LV.isObjCArray())
803 // Using array syntax to assigning to what global points to is not
804 // same as assigning to the global itself. {id *G;} G[i] = 0;
805 LV.SetGlobalObjCRef(LV, false);
Chris Lattnereb99b012009-10-28 17:39:19 +0000806 return;
Fariborz Jahanian1c1afc42009-09-18 00:04:00 +0000807 }
Chris Lattnereb99b012009-10-28 17:39:19 +0000808
809 if (const MemberExpr *Exp = dyn_cast<MemberExpr>(E)) {
Fariborz Jahanianb123ea32009-09-16 21:37:16 +0000810 setObjCGCLValueClass(Ctx, Exp->getBase(), LV);
Fariborz Jahanian1c1afc42009-09-18 00:04:00 +0000811 // We don't know if member is an 'ivar', but this flag is looked at
812 // only in the context of LV.isObjCIvar().
Fariborz Jahanianfd02ed72009-09-21 18:54:29 +0000813 LV.SetObjCArray(LV, E->getType()->isArrayType());
Chris Lattnereb99b012009-10-28 17:39:19 +0000814 return;
Fariborz Jahanianb123ea32009-09-16 21:37:16 +0000815 }
816}
817
Anders Carlssonce53f7d2009-11-07 23:06:58 +0000818static LValue EmitGlobalVarDeclLValue(CodeGenFunction &CGF,
819 const Expr *E, const VarDecl *VD) {
Daniel Dunbard2113f22009-11-08 09:46:46 +0000820 assert((VD->hasExternalStorage() || VD->isFileVarDecl()) &&
Anders Carlssonce53f7d2009-11-07 23:06:58 +0000821 "Var decl must have external storage or be a file var decl!");
822
823 llvm::Value *V = CGF.CGM.GetAddrOfGlobalVar(VD);
824 if (VD->getType()->isReferenceType())
825 V = CGF.Builder.CreateLoad(V, "tmp");
826 LValue LV = LValue::MakeAddr(V, CGF.MakeQualifiers(E->getType()));
827 setObjCGCLValueClass(CGF.getContext(), E, LV);
828 return LV;
829}
830
Eli Friedman9a146302009-11-26 06:08:14 +0000831static LValue EmitFunctionDeclLValue(CodeGenFunction &CGF,
832 const Expr *E, const FunctionDecl *FD) {
833 llvm::Value* V = CGF.CGM.GetAddrOfFunction(FD);
834 if (!FD->hasPrototype()) {
835 if (const FunctionProtoType *Proto =
836 FD->getType()->getAs<FunctionProtoType>()) {
837 // Ugly case: for a K&R-style definition, the type of the definition
838 // isn't the same as the type of a use. Correct for this with a
839 // bitcast.
840 QualType NoProtoType =
841 CGF.getContext().getFunctionNoProtoType(Proto->getResultType());
842 NoProtoType = CGF.getContext().getPointerType(NoProtoType);
843 V = CGF.Builder.CreateBitCast(V, CGF.ConvertType(NoProtoType), "tmp");
844 }
845 }
846 return LValue::MakeAddr(V, CGF.MakeQualifiers(E->getType()));
847}
848
Reid Spencer5f016e22007-07-11 17:01:13 +0000849LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) {
Anders Carlsson1e74c4f2009-11-07 22:53:10 +0000850 const NamedDecl *ND = E->getDecl();
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000851
Anders Carlsson1e74c4f2009-11-07 22:53:10 +0000852 if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
Anders Carlsson1e74c4f2009-11-07 22:53:10 +0000853
854 // Check if this is a global variable.
Anders Carlssonce53f7d2009-11-07 23:06:58 +0000855 if (VD->hasExternalStorage() || VD->isFileVarDecl())
856 return EmitGlobalVarDeclLValue(*this, E, VD);
Anders Carlsson0bc70492009-11-07 22:46:42 +0000857
858 bool NonGCable = VD->hasLocalStorage() && !VD->hasAttr<BlocksAttr>();
859
860 llvm::Value *V = LocalDeclMap[VD];
861 assert(V && "DeclRefExpr not entered in LocalDeclMap?");
862
863 Qualifiers Quals = MakeQualifiers(E->getType());
864 // local variables do not get their gc attribute set.
865 // local static?
866 if (NonGCable) Quals.removeObjCGCAttr();
867
868 if (VD->hasAttr<BlocksAttr>()) {
869 V = Builder.CreateStructGEP(V, 1, "forwarding");
870 V = Builder.CreateLoad(V, false);
871 V = Builder.CreateStructGEP(V, getByRefValueLLVMField(VD),
872 VD->getNameAsString());
873 }
874 if (VD->getType()->isReferenceType())
875 V = Builder.CreateLoad(V, "tmp");
Anders Carlssonce53f7d2009-11-07 23:06:58 +0000876 LValue LV = LValue::MakeAddr(V, Quals);
Anders Carlsson0bc70492009-11-07 22:46:42 +0000877 LValue::SetObjCNonGC(LV, NonGCable);
Fariborz Jahanianb123ea32009-09-16 21:37:16 +0000878 setObjCGCLValueClass(getContext(), E, LV);
Fariborz Jahanian2682d8b2008-11-20 00:15:42 +0000879 return LV;
Chris Lattnereb99b012009-10-28 17:39:19 +0000880 }
881
Eli Friedman9a146302009-11-26 06:08:14 +0000882 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
883 return EmitFunctionDeclLValue(*this, E, FD);
Chris Lattnereb99b012009-10-28 17:39:19 +0000884
Chris Lattnereb99b012009-10-28 17:39:19 +0000885 if (E->getQualifier()) {
Douglas Gregora2813ce2009-10-23 18:54:35 +0000886 // FIXME: the qualifier check does not seem sufficient here
Anders Carlsson1e74c4f2009-11-07 22:53:10 +0000887 return EmitPointerToDataMemberLValue(cast<FieldDecl>(ND));
Chris Lattner41110242008-06-17 18:05:57 +0000888 }
Chris Lattnereb99b012009-10-28 17:39:19 +0000889
Anders Carlsson1e74c4f2009-11-07 22:53:10 +0000890 assert(false && "Unhandled DeclRefExpr");
891
892 // an invalid LValue, but the assert will
893 // ensure that this point is never reached.
Chris Lattnerb1776cb2007-09-16 19:23:47 +0000894 return LValue();
Reid Spencer5f016e22007-07-11 17:01:13 +0000895}
896
Mike Stumpa99038c2009-02-28 09:07:16 +0000897LValue CodeGenFunction::EmitBlockDeclRefLValue(const BlockDeclRefExpr *E) {
John McCall0953e762009-09-24 19:53:00 +0000898 return LValue::MakeAddr(GetAddrOfBlockDecl(E), MakeQualifiers(E->getType()));
Mike Stumpa99038c2009-02-28 09:07:16 +0000899}
900
Reid Spencer5f016e22007-07-11 17:01:13 +0000901LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) {
902 // __extension__ doesn't affect lvalue-ness.
903 if (E->getOpcode() == UnaryOperator::Extension)
904 return EmitLValue(E->getSubExpr());
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000905
Chris Lattner96196622008-07-26 22:37:01 +0000906 QualType ExprTy = getContext().getCanonicalType(E->getSubExpr()->getType());
Chris Lattner7da36f62007-10-30 22:53:42 +0000907 switch (E->getOpcode()) {
908 default: assert(0 && "Unknown unary operator lvalue!");
Chris Lattnereb99b012009-10-28 17:39:19 +0000909 case UnaryOperator::Deref: {
910 QualType T = E->getSubExpr()->getType()->getPointeeType();
911 assert(!T.isNull() && "CodeGenFunction::EmitUnaryOpLValue: Illegal type");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000912
Chris Lattnereb99b012009-10-28 17:39:19 +0000913 Qualifiers Quals = MakeQualifiers(T);
914 Quals.setAddressSpace(ExprTy.getAddressSpace());
John McCall0953e762009-09-24 19:53:00 +0000915
Chris Lattnereb99b012009-10-28 17:39:19 +0000916 LValue LV = LValue::MakeAddr(EmitScalarExpr(E->getSubExpr()), Quals);
917 // We should not generate __weak write barrier on indirect reference
918 // of a pointer to object; as in void foo (__weak id *param); *param = 0;
919 // But, we continue to generate __strong write barrier on indirect write
920 // into a pointer to object.
921 if (getContext().getLangOptions().ObjC1 &&
922 getContext().getLangOptions().getGCMode() != LangOptions::NonGC &&
923 LV.isObjCWeak())
924 LValue::SetObjCNonGC(LV, !E->isOBJCGCCandidate(getContext()));
925 return LV;
926 }
Chris Lattner7da36f62007-10-30 22:53:42 +0000927 case UnaryOperator::Real:
Eli Friedmane401cd52009-11-09 04:20:47 +0000928 case UnaryOperator::Imag: {
Chris Lattner7da36f62007-10-30 22:53:42 +0000929 LValue LV = EmitLValue(E->getSubExpr());
Chris Lattner36b6a0a2008-03-19 05:19:41 +0000930 unsigned Idx = E->getOpcode() == UnaryOperator::Imag;
931 return LValue::MakeAddr(Builder.CreateStructGEP(LV.getAddress(),
Chris Lattnerb77792e2008-07-26 22:17:49 +0000932 Idx, "idx"),
John McCall0953e762009-09-24 19:53:00 +0000933 MakeQualifiers(ExprTy));
Chris Lattner7da36f62007-10-30 22:53:42 +0000934 }
Eli Friedmane401cd52009-11-09 04:20:47 +0000935 case UnaryOperator::PreInc:
936 case UnaryOperator::PreDec:
937 return EmitUnsupportedLValue(E, "pre-inc/dec expression");
938 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000939}
940
941LValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) {
John McCall0953e762009-09-24 19:53:00 +0000942 return LValue::MakeAddr(CGM.GetAddrOfConstantStringFromLiteral(E),
943 Qualifiers());
Reid Spencer5f016e22007-07-11 17:01:13 +0000944}
945
Chris Lattnereaf2bb82009-02-24 22:18:39 +0000946LValue CodeGenFunction::EmitObjCEncodeExprLValue(const ObjCEncodeExpr *E) {
John McCall0953e762009-09-24 19:53:00 +0000947 return LValue::MakeAddr(CGM.GetAddrOfConstantStringFromObjCEncode(E),
948 Qualifiers());
Chris Lattnereaf2bb82009-02-24 22:18:39 +0000949}
950
951
Daniel Dunbar662b71e2008-10-17 21:58:32 +0000952LValue CodeGenFunction::EmitPredefinedFunctionName(unsigned Type) {
Anders Carlsson22742662007-07-21 05:21:51 +0000953 std::string GlobalVarName;
Daniel Dunbar662b71e2008-10-17 21:58:32 +0000954
955 switch (Type) {
Chris Lattnereb99b012009-10-28 17:39:19 +0000956 default: assert(0 && "Invalid type");
Chris Lattnereaf2bb82009-02-24 22:18:39 +0000957 case PredefinedExpr::Func:
958 GlobalVarName = "__func__.";
959 break;
960 case PredefinedExpr::Function:
961 GlobalVarName = "__FUNCTION__.";
962 break;
963 case PredefinedExpr::PrettyFunction:
Chris Lattnereaf2bb82009-02-24 22:18:39 +0000964 GlobalVarName = "__PRETTY_FUNCTION__.";
965 break;
Anders Carlsson22742662007-07-21 05:21:51 +0000966 }
Daniel Dunbar662b71e2008-10-17 21:58:32 +0000967
Daniel Dunbar0a23d762009-09-12 23:06:21 +0000968 llvm::StringRef FnName = CurFn->getName();
969 if (FnName.startswith("\01"))
970 FnName = FnName.substr(1);
971 GlobalVarName += FnName;
972
Anders Carlsson3a082d82009-09-08 18:24:21 +0000973 std::string FunctionName =
Mike Stump1eb44332009-09-09 15:08:12 +0000974 PredefinedExpr::ComputeName(getContext(), (PredefinedExpr::IdentType)Type,
Anders Carlsson3a082d82009-09-08 18:24:21 +0000975 CurCodeDecl);
Daniel Dunbar662b71e2008-10-17 21:58:32 +0000976
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000977 llvm::Constant *C =
Daniel Dunbar662b71e2008-10-17 21:58:32 +0000978 CGM.GetAddrOfConstantCString(FunctionName, GlobalVarName.c_str());
John McCall0953e762009-09-24 19:53:00 +0000979 return LValue::MakeAddr(C, Qualifiers());
Daniel Dunbar662b71e2008-10-17 21:58:32 +0000980}
981
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000982LValue CodeGenFunction::EmitPredefinedLValue(const PredefinedExpr *E) {
Daniel Dunbar662b71e2008-10-17 21:58:32 +0000983 switch (E->getIdentType()) {
984 default:
985 return EmitUnsupportedLValue(E, "predefined expression");
986 case PredefinedExpr::Func:
987 case PredefinedExpr::Function:
988 case PredefinedExpr::PrettyFunction:
989 return EmitPredefinedFunctionName(E->getIdentType());
990 }
Anders Carlsson22742662007-07-21 05:21:51 +0000991}
992
Reid Spencer5f016e22007-07-11 17:01:13 +0000993LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) {
Ted Kremenek23245122007-08-20 16:18:38 +0000994 // The index must always be an integer, which is not an aggregate. Emit it.
Chris Lattner7f02f722007-08-24 05:35:26 +0000995 llvm::Value *Idx = EmitScalarExpr(E->getIdx());
Eli Friedman61d004a2009-06-06 19:09:26 +0000996 QualType IdxTy = E->getIdx()->getType();
997 bool IdxSigned = IdxTy->isSignedIntegerType();
998
Reid Spencer5f016e22007-07-11 17:01:13 +0000999 // If the base is a vector type, then we are forming a vector element lvalue
1000 // with this subscript.
Eli Friedman1e692ac2008-06-13 23:01:12 +00001001 if (E->getBase()->getType()->isVectorType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001002 // Emit the vector as an lvalue to get its address.
Eli Friedman1e692ac2008-06-13 23:01:12 +00001003 LValue LHS = EmitLValue(E->getBase());
Ted Kremenek23245122007-08-20 16:18:38 +00001004 assert(LHS.isSimple() && "Can only subscript lvalue vectors here!");
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001005 Idx = Builder.CreateIntCast(Idx,
Owen Anderson0032b272009-08-13 21:57:51 +00001006 llvm::Type::getInt32Ty(VMContext), IdxSigned, "vidx");
Eli Friedman1e692ac2008-06-13 23:01:12 +00001007 return LValue::MakeVectorElt(LHS.getAddress(), Idx,
John McCall0953e762009-09-24 19:53:00 +00001008 E->getBase()->getType().getCVRQualifiers());
Reid Spencer5f016e22007-07-11 17:01:13 +00001009 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001010
Ted Kremenek23245122007-08-20 16:18:38 +00001011 // The base must be a pointer, which is not an aggregate. Emit it.
Chris Lattner7f02f722007-08-24 05:35:26 +00001012 llvm::Value *Base = EmitScalarExpr(E->getBase());
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001013
Ted Kremenek23245122007-08-20 16:18:38 +00001014 // Extend or truncate the index type to 32 or 64-bits.
Reid Spencer5f016e22007-07-11 17:01:13 +00001015 unsigned IdxBitwidth = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
Sanjiv Gupta7cabee52009-04-24 02:40:57 +00001016 if (IdxBitwidth != LLVMPointerWidth)
Owen Anderson0032b272009-08-13 21:57:51 +00001017 Idx = Builder.CreateIntCast(Idx,
1018 llvm::IntegerType::get(VMContext, LLVMPointerWidth),
Reid Spencer5f016e22007-07-11 17:01:13 +00001019 IdxSigned, "idxprom");
1020
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001021 // We know that the pointer points to a type of the correct size, unless the
1022 // size is a VLA or Objective-C interface.
Daniel Dunbar2a866252009-04-25 05:08:32 +00001023 llvm::Value *Address = 0;
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001024 if (const VariableArrayType *VAT =
Anders Carlsson8b33c082008-12-21 00:11:23 +00001025 getContext().getAsVariableArrayType(E->getType())) {
Chris Lattner881eb9c2009-08-14 23:43:22 +00001026 llvm::Value *VLASize = GetVLASize(VAT);
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001027
Anders Carlsson8b33c082008-12-21 00:11:23 +00001028 Idx = Builder.CreateMul(Idx, VLASize);
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001029
Anders Carlsson6183a992008-12-21 03:44:36 +00001030 QualType BaseType = getContext().getBaseElementType(VAT);
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001031
Anders Carlsson8b33c082008-12-21 00:11:23 +00001032 uint64_t BaseTypeSize = getContext().getTypeSize(BaseType) / 8;
1033 Idx = Builder.CreateUDiv(Idx,
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001034 llvm::ConstantInt::get(Idx->getType(),
Anders Carlsson8b33c082008-12-21 00:11:23 +00001035 BaseTypeSize));
Dan Gohman664f8932009-08-12 00:33:55 +00001036 Address = Builder.CreateInBoundsGEP(Base, Idx, "arrayidx");
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001037 } else if (const ObjCInterfaceType *OIT =
Daniel Dunbar2a866252009-04-25 05:08:32 +00001038 dyn_cast<ObjCInterfaceType>(E->getType())) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001039 llvm::Value *InterfaceSize =
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001040 llvm::ConstantInt::get(Idx->getType(),
Daniel Dunbar2a866252009-04-25 05:08:32 +00001041 getContext().getTypeSize(OIT) / 8);
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001042
Daniel Dunbar2a866252009-04-25 05:08:32 +00001043 Idx = Builder.CreateMul(Idx, InterfaceSize);
1044
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00001045 const llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(VMContext);
Dan Gohman664f8932009-08-12 00:33:55 +00001046 Address = Builder.CreateGEP(Builder.CreateBitCast(Base, i8PTy),
Daniel Dunbar2a866252009-04-25 05:08:32 +00001047 Idx, "arrayidx");
1048 Address = Builder.CreateBitCast(Address, Base->getType());
1049 } else {
Dan Gohman664f8932009-08-12 00:33:55 +00001050 Address = Builder.CreateInBoundsGEP(Base, Idx, "arrayidx");
Anders Carlsson8b33c082008-12-21 00:11:23 +00001051 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001052
Steve Naroff14108da2009-07-10 23:34:53 +00001053 QualType T = E->getBase()->getType()->getPointeeType();
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001054 assert(!T.isNull() &&
Steve Naroff14108da2009-07-10 23:34:53 +00001055 "CodeGenFunction::EmitArraySubscriptExpr(): Illegal base type");
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001056
John McCall0953e762009-09-24 19:53:00 +00001057 Qualifiers Quals = MakeQualifiers(T);
1058 Quals.setAddressSpace(E->getBase()->getType().getAddressSpace());
1059
1060 LValue LV = LValue::MakeAddr(Address, Quals);
Fariborz Jahanian643887a2009-02-21 23:37:19 +00001061 if (getContext().getLangOptions().ObjC1 &&
Fariborz Jahanianb123ea32009-09-16 21:37:16 +00001062 getContext().getLangOptions().getGCMode() != LangOptions::NonGC) {
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001063 LValue::SetObjCNonGC(LV, !E->isOBJCGCCandidate(getContext()));
Fariborz Jahanianb123ea32009-09-16 21:37:16 +00001064 setObjCGCLValueClass(getContext(), E, LV);
1065 }
Fariborz Jahanian643887a2009-02-21 23:37:19 +00001066 return LV;
Reid Spencer5f016e22007-07-11 17:01:13 +00001067}
1068
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001069static
Owen Andersona1cf15f2009-07-14 23:10:40 +00001070llvm::Constant *GenerateConstantVector(llvm::LLVMContext &VMContext,
1071 llvm::SmallVector<unsigned, 4> &Elts) {
Nate Begeman3b8d1162008-05-13 21:03:02 +00001072 llvm::SmallVector<llvm::Constant *, 4> CElts;
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001073
Nate Begeman3b8d1162008-05-13 21:03:02 +00001074 for (unsigned i = 0, e = Elts.size(); i != e; ++i)
Owen Anderson0032b272009-08-13 21:57:51 +00001075 CElts.push_back(llvm::ConstantInt::get(
1076 llvm::Type::getInt32Ty(VMContext), Elts[i]));
Nate Begeman3b8d1162008-05-13 21:03:02 +00001077
Owen Anderson4a289322009-07-28 21:22:35 +00001078 return llvm::ConstantVector::get(&CElts[0], CElts.size());
Nate Begeman3b8d1162008-05-13 21:03:02 +00001079}
1080
Chris Lattner349aaec2007-08-02 23:37:31 +00001081LValue CodeGenFunction::
Nate Begeman213541a2008-04-18 23:10:10 +00001082EmitExtVectorElementExpr(const ExtVectorElementExpr *E) {
Chris Lattner349aaec2007-08-02 23:37:31 +00001083 // Emit the base vector as an l-value.
Chris Lattner73525de2009-02-16 21:11:58 +00001084 LValue Base;
1085
1086 // ExtVectorElementExpr's base can either be a vector or pointer to vector.
Chris Lattner2140e902009-02-16 22:14:05 +00001087 if (!E->isArrow()) {
Chris Lattner73525de2009-02-16 21:11:58 +00001088 assert(E->getBase()->getType()->isVectorType());
1089 Base = EmitLValue(E->getBase());
Chris Lattner2140e902009-02-16 22:14:05 +00001090 } else {
Ted Kremenek6217b802009-07-29 21:53:49 +00001091 const PointerType *PT = E->getBase()->getType()->getAs<PointerType>();
Chris Lattner2140e902009-02-16 22:14:05 +00001092 llvm::Value *Ptr = EmitScalarExpr(E->getBase());
John McCall0953e762009-09-24 19:53:00 +00001093 Qualifiers Quals = MakeQualifiers(PT->getPointeeType());
1094 Quals.removeObjCGCAttr();
1095 Base = LValue::MakeAddr(Ptr, Quals);
Chris Lattner73525de2009-02-16 21:11:58 +00001096 }
Chris Lattner349aaec2007-08-02 23:37:31 +00001097
Nate Begeman3b8d1162008-05-13 21:03:02 +00001098 // Encode the element access list into a vector of unsigned indices.
1099 llvm::SmallVector<unsigned, 4> Indices;
1100 E->getEncodedElementAccess(Indices);
1101
1102 if (Base.isSimple()) {
Owen Andersona1cf15f2009-07-14 23:10:40 +00001103 llvm::Constant *CV = GenerateConstantVector(VMContext, Indices);
Eli Friedman1e692ac2008-06-13 23:01:12 +00001104 return LValue::MakeExtVectorElt(Base.getAddress(), CV,
John McCall0953e762009-09-24 19:53:00 +00001105 Base.getVRQualifiers());
Nate Begeman3b8d1162008-05-13 21:03:02 +00001106 }
1107 assert(Base.isExtVectorElt() && "Can only subscript lvalue vec elts here!");
1108
1109 llvm::Constant *BaseElts = Base.getExtVectorElts();
1110 llvm::SmallVector<llvm::Constant *, 4> CElts;
1111
Chris Lattner67665862009-10-28 05:12:07 +00001112 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
Nate Begeman3b8d1162008-05-13 21:03:02 +00001113 for (unsigned i = 0, e = Indices.size(); i != e; ++i) {
1114 if (isa<llvm::ConstantAggregateZero>(BaseElts))
Chris Lattner67665862009-10-28 05:12:07 +00001115 CElts.push_back(llvm::ConstantInt::get(Int32Ty, 0));
Nate Begeman3b8d1162008-05-13 21:03:02 +00001116 else
Chris Lattner67665862009-10-28 05:12:07 +00001117 CElts.push_back(cast<llvm::Constant>(BaseElts->getOperand(Indices[i])));
Nate Begeman3b8d1162008-05-13 21:03:02 +00001118 }
Owen Anderson4a289322009-07-28 21:22:35 +00001119 llvm::Constant *CV = llvm::ConstantVector::get(&CElts[0], CElts.size());
Eli Friedman1e692ac2008-06-13 23:01:12 +00001120 return LValue::MakeExtVectorElt(Base.getExtVectorAddr(), CV,
John McCall0953e762009-09-24 19:53:00 +00001121 Base.getVRQualifiers());
Chris Lattner349aaec2007-08-02 23:37:31 +00001122}
1123
Devang Patelb9b00ad2007-10-23 20:28:39 +00001124LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) {
Devang Patelfe2419a2007-12-11 21:33:16 +00001125 bool isUnion = false;
Fariborz Jahanian4f676ed2009-02-21 00:30:43 +00001126 bool isNonGC = false;
Devang Patel126a8562007-10-24 22:26:28 +00001127 Expr *BaseExpr = E->getBase();
Devang Patel126a8562007-10-24 22:26:28 +00001128 llvm::Value *BaseValue = NULL;
John McCall0953e762009-09-24 19:53:00 +00001129 Qualifiers BaseQuals;
Eli Friedman1e692ac2008-06-13 23:01:12 +00001130
Chris Lattner12f65f62007-12-02 18:52:07 +00001131 // If this is s.x, emit s as an lvalue. If it is s->x, emit s as a scalar.
Devang Patelfe2419a2007-12-11 21:33:16 +00001132 if (E->isArrow()) {
Devang Patel0a961182007-10-26 18:15:21 +00001133 BaseValue = EmitScalarExpr(BaseExpr);
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001134 const PointerType *PTy =
Ted Kremenek6217b802009-07-29 21:53:49 +00001135 BaseExpr->getType()->getAs<PointerType>();
Devang Patelfe2419a2007-12-11 21:33:16 +00001136 if (PTy->getPointeeType()->isUnionType())
1137 isUnion = true;
John McCall0953e762009-09-24 19:53:00 +00001138 BaseQuals = PTy->getPointeeType().getQualifiers();
Fariborz Jahaniand2e1eb02009-09-01 17:02:21 +00001139 } else if (isa<ObjCPropertyRefExpr>(BaseExpr->IgnoreParens()) ||
1140 isa<ObjCImplicitSetterGetterRefExpr>(
1141 BaseExpr->IgnoreParens())) {
Fariborz Jahanian35c33292009-01-12 23:27:26 +00001142 RValue RV = EmitObjCPropertyGet(BaseExpr);
1143 BaseValue = RV.getAggregateAddr();
1144 if (BaseExpr->getType()->isUnionType())
1145 isUnion = true;
John McCall0953e762009-09-24 19:53:00 +00001146 BaseQuals = BaseExpr->getType().getQualifiers();
Chris Lattner1bd885e2009-02-16 22:25:49 +00001147 } else {
Chris Lattner12f65f62007-12-02 18:52:07 +00001148 LValue BaseLV = EmitLValue(BaseExpr);
Fariborz Jahanian4f676ed2009-02-21 00:30:43 +00001149 if (BaseLV.isNonGC())
1150 isNonGC = true;
Chris Lattner12f65f62007-12-02 18:52:07 +00001151 // FIXME: this isn't right for bitfields.
1152 BaseValue = BaseLV.getAddress();
Fariborz Jahaniana91d6a62009-07-29 00:44:13 +00001153 QualType BaseTy = BaseExpr->getType();
1154 if (BaseTy->isUnionType())
Devang Patelfe2419a2007-12-11 21:33:16 +00001155 isUnion = true;
John McCall0953e762009-09-24 19:53:00 +00001156 BaseQuals = BaseTy.getQualifiers();
Chris Lattner12f65f62007-12-02 18:52:07 +00001157 }
Devang Patelb9b00ad2007-10-23 20:28:39 +00001158
Anders Carlssonce53f7d2009-11-07 23:06:58 +00001159 NamedDecl *ND = E->getMemberDecl();
1160 if (FieldDecl *Field = dyn_cast<FieldDecl>(ND)) {
1161 LValue LV = EmitLValueForField(BaseValue, Field, isUnion,
1162 BaseQuals.getCVRQualifiers());
1163 LValue::SetObjCNonGC(LV, isNonGC);
1164 setObjCGCLValueClass(getContext(), E, LV);
1165 return LV;
1166 }
1167
Anders Carlsson589f9e32009-11-07 23:16:50 +00001168 if (VarDecl *VD = dyn_cast<VarDecl>(ND))
1169 return EmitGlobalVarDeclLValue(*this, E, VD);
Eli Friedman9a146302009-11-26 06:08:14 +00001170
1171 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
1172 return EmitFunctionDeclLValue(*this, E, FD);
1173
Anders Carlssonce53f7d2009-11-07 23:06:58 +00001174 assert(false && "Unhandled member declaration!");
1175 return LValue();
Eli Friedman472778e2008-02-09 08:50:58 +00001176}
Devang Patelb9b00ad2007-10-23 20:28:39 +00001177
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +00001178LValue CodeGenFunction::EmitLValueForBitfield(llvm::Value* BaseValue,
Anders Carlsson0ed303c2009-11-17 03:57:07 +00001179 const FieldDecl* Field,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001180 unsigned CVRQualifiers) {
Anders Carlsson8330cee2009-07-23 17:01:21 +00001181 CodeGenTypes::BitFieldInfo Info = CGM.getTypes().getBitFieldInfo(Field);
1182
Mike Stumpf5408fe2009-05-16 07:57:57 +00001183 // FIXME: CodeGenTypes should expose a method to get the appropriate type for
1184 // FieldTy (the appropriate type is ABI-dependent).
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001185 const llvm::Type *FieldTy =
Daniel Dunbarbb767732009-02-17 18:31:04 +00001186 CGM.getTypes().ConvertTypeForMem(Field->getType());
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +00001187 const llvm::PointerType *BaseTy =
1188 cast<llvm::PointerType>(BaseValue->getType());
1189 unsigned AS = BaseTy->getAddressSpace();
1190 BaseValue = Builder.CreateBitCast(BaseValue,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001191 llvm::PointerType::get(FieldTy, AS),
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +00001192 "tmp");
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001193
1194 llvm::Value *Idx =
Owen Anderson0032b272009-08-13 21:57:51 +00001195 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), Info.FieldNo);
Anders Carlsson8330cee2009-07-23 17:01:21 +00001196 llvm::Value *V = Builder.CreateGEP(BaseValue, Idx, "tmp");
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001197
Anders Carlsson8330cee2009-07-23 17:01:21 +00001198 return LValue::MakeBitfield(V, Info.Start, Info.Size,
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +00001199 Field->getType()->isSignedIntegerType(),
1200 Field->getType().getCVRQualifiers()|CVRQualifiers);
1201}
1202
Eli Friedman472778e2008-02-09 08:50:58 +00001203LValue CodeGenFunction::EmitLValueForField(llvm::Value* BaseValue,
Anders Carlsson0ed303c2009-11-17 03:57:07 +00001204 const FieldDecl* Field,
Eli Friedman1e692ac2008-06-13 23:01:12 +00001205 bool isUnion,
Mike Stump1eb44332009-09-09 15:08:12 +00001206 unsigned CVRQualifiers) {
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +00001207 if (Field->isBitField())
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001208 return EmitLValueForBitfield(BaseValue, Field, CVRQualifiers);
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001209
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001210 unsigned idx = CGM.getTypes().getLLVMFieldNo(Field);
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +00001211 llvm::Value *V = Builder.CreateStructGEP(BaseValue, idx, "tmp");
Eli Friedman1e86b342008-05-29 11:33:25 +00001212
Devang Patelabad06c2007-10-26 19:42:18 +00001213 // Match union field type.
Lauro Ramos Venanciod957aa02008-02-07 19:29:53 +00001214 if (isUnion) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001215 const llvm::Type *FieldTy =
Eli Friedman1e692ac2008-06-13 23:01:12 +00001216 CGM.getTypes().ConvertTypeForMem(Field->getType());
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001217 const llvm::PointerType * BaseTy =
Devang Patele9b8c0a2007-10-30 20:59:40 +00001218 cast<llvm::PointerType>(BaseValue->getType());
Eli Friedman788d5712008-05-21 13:24:44 +00001219 unsigned AS = BaseTy->getAddressSpace();
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001220 V = Builder.CreateBitCast(V,
1221 llvm::PointerType::get(FieldTy, AS),
Eli Friedman788d5712008-05-21 13:24:44 +00001222 "tmp");
Devang Patelabad06c2007-10-26 19:42:18 +00001223 }
Eli Friedman2be58612009-05-30 21:09:44 +00001224 if (Field->getType()->isReferenceType())
1225 V = Builder.CreateLoad(V, "tmp");
John McCall0953e762009-09-24 19:53:00 +00001226
1227 Qualifiers Quals = MakeQualifiers(Field->getType());
1228 Quals.addCVRQualifiers(CVRQualifiers);
Fariborz Jahanianfd02ed72009-09-21 18:54:29 +00001229 // __weak attribute on a field is ignored.
John McCall0953e762009-09-24 19:53:00 +00001230 if (Quals.getObjCGCAttr() == Qualifiers::Weak)
1231 Quals.removeObjCGCAttr();
Fariborz Jahanianfd02ed72009-09-21 18:54:29 +00001232
John McCall0953e762009-09-24 19:53:00 +00001233 return LValue::MakeAddr(V, Quals);
Devang Patelb9b00ad2007-10-23 20:28:39 +00001234}
1235
Chris Lattner75dfeda2009-03-18 18:28:57 +00001236LValue CodeGenFunction::EmitCompoundLiteralLValue(const CompoundLiteralExpr* E){
Eli Friedman06e863f2008-05-13 23:18:27 +00001237 const llvm::Type *LTy = ConvertType(E->getType());
1238 llvm::Value *DeclPtr = CreateTempAlloca(LTy, ".compoundliteral");
1239
1240 const Expr* InitExpr = E->getInitializer();
John McCall0953e762009-09-24 19:53:00 +00001241 LValue Result = LValue::MakeAddr(DeclPtr, MakeQualifiers(E->getType()));
Eli Friedman06e863f2008-05-13 23:18:27 +00001242
Chris Lattnereb99b012009-10-28 17:39:19 +00001243 if (E->getType()->isComplexType())
Eli Friedman06e863f2008-05-13 23:18:27 +00001244 EmitComplexExprIntoAddr(InitExpr, DeclPtr, false);
Chris Lattnereb99b012009-10-28 17:39:19 +00001245 else if (hasAggregateLLVMType(E->getType()))
Eli Friedman06e863f2008-05-13 23:18:27 +00001246 EmitAnyExpr(InitExpr, DeclPtr, false);
Chris Lattnereb99b012009-10-28 17:39:19 +00001247 else
Eli Friedman06e863f2008-05-13 23:18:27 +00001248 EmitStoreThroughLValue(EmitAnyExpr(InitExpr), Result, E->getType());
Eli Friedman06e863f2008-05-13 23:18:27 +00001249
1250 return Result;
1251}
1252
Anders Carlsson6fcec8b2009-09-15 16:35:24 +00001253LValue
1254CodeGenFunction::EmitConditionalOperatorLValue(const ConditionalOperator* E) {
1255 if (E->isLvalue(getContext()) == Expr::LV_Valid) {
1256 llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true");
1257 llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false");
1258 llvm::BasicBlock *ContBlock = createBasicBlock("cond.end");
1259
1260 llvm::Value *Cond = EvaluateExprAsBool(E->getCond());
1261 Builder.CreateCondBr(Cond, LHSBlock, RHSBlock);
1262
1263 EmitBlock(LHSBlock);
Daniel Dunbar90345582009-03-24 02:38:23 +00001264
Anders Carlsson6fcec8b2009-09-15 16:35:24 +00001265 LValue LHS = EmitLValue(E->getLHS());
1266 if (!LHS.isSimple())
1267 return EmitUnsupportedLValue(E, "conditional operator");
1268
Chris Lattnereb99b012009-10-28 17:39:19 +00001269 llvm::Value *Temp = CreateTempAlloca(LHS.getAddress()->getType(),"condtmp");
Anders Carlsson6fcec8b2009-09-15 16:35:24 +00001270 Builder.CreateStore(LHS.getAddress(), Temp);
1271 EmitBranch(ContBlock);
1272
1273 EmitBlock(RHSBlock);
1274 LValue RHS = EmitLValue(E->getRHS());
1275 if (!RHS.isSimple())
1276 return EmitUnsupportedLValue(E, "conditional operator");
1277
1278 Builder.CreateStore(RHS.getAddress(), Temp);
1279 EmitBranch(ContBlock);
1280
1281 EmitBlock(ContBlock);
1282
1283 Temp = Builder.CreateLoad(Temp, "lv");
John McCall0953e762009-09-24 19:53:00 +00001284 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
Anders Carlsson6fcec8b2009-09-15 16:35:24 +00001285 }
1286
Daniel Dunbar90345582009-03-24 02:38:23 +00001287 // ?: here should be an aggregate.
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001288 assert((hasAggregateLLVMType(E->getType()) &&
Daniel Dunbar90345582009-03-24 02:38:23 +00001289 !E->getType()->isAnyComplexType()) &&
1290 "Unexpected conditional operator!");
1291
1292 llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType()));
1293 EmitAggExpr(E, Temp, false);
1294
John McCall0953e762009-09-24 19:53:00 +00001295 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
Daniel Dunbar90345582009-03-24 02:38:23 +00001296}
1297
Mike Stumpc849c052009-11-16 06:50:58 +00001298/// EmitCastLValue - Casts are never lvalues unless that cast is a dynamic_cast.
1299/// If the cast is a dynamic_cast, we can have the usual lvalue result,
1300/// otherwise if a cast is needed by the code generator in an lvalue context,
1301/// then it must mean that we need the address of an aggregate in order to
1302/// access one of its fields. This can happen for all the reasons that casts
1303/// are permitted with aggregate result, including noop aggregate casts, and
1304/// cast from scalar to union.
Chris Lattner75dfeda2009-03-18 18:28:57 +00001305LValue CodeGenFunction::EmitCastLValue(const CastExpr *E) {
Anders Carlsson0ee33cf2009-09-12 16:16:49 +00001306 switch (E->getCastKind()) {
1307 default:
Eli Friedmaneaae78a2009-11-16 05:48:01 +00001308 return EmitUnsupportedLValue(E, "unexpected cast lvalue");
1309
Mike Stumpc849c052009-11-16 06:50:58 +00001310 case CastExpr::CK_Dynamic: {
1311 LValue LV = EmitLValue(E->getSubExpr());
1312 llvm::Value *V = LV.getAddress();
1313 const CXXDynamicCastExpr *DCE = cast<CXXDynamicCastExpr>(E);
1314 return LValue::MakeAddr(EmitDynamicCast(V, DCE),
1315 MakeQualifiers(E->getType()));
1316 }
1317
Anders Carlsson0ee33cf2009-09-12 16:16:49 +00001318 case CastExpr::CK_NoOp:
1319 case CastExpr::CK_ConstructorConversion:
1320 case CastExpr::CK_UserDefinedConversion:
Chris Lattner75dfeda2009-03-18 18:28:57 +00001321 return EmitLValue(E->getSubExpr());
Anders Carlsson0ee33cf2009-09-12 16:16:49 +00001322
1323 case CastExpr::CK_DerivedToBase: {
1324 const RecordType *DerivedClassTy =
1325 E->getSubExpr()->getType()->getAs<RecordType>();
1326 CXXRecordDecl *DerivedClassDecl =
1327 cast<CXXRecordDecl>(DerivedClassTy->getDecl());
Chris Lattner75dfeda2009-03-18 18:28:57 +00001328
Anders Carlsson0ee33cf2009-09-12 16:16:49 +00001329 const RecordType *BaseClassTy = E->getType()->getAs<RecordType>();
1330 CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseClassTy->getDecl());
1331
1332 LValue LV = EmitLValue(E->getSubExpr());
1333
1334 // Perform the derived-to-base conversion
1335 llvm::Value *Base =
Anders Carlssona3697c92009-11-23 17:57:54 +00001336 GetAddressOfBaseClass(LV.getAddress(), DerivedClassDecl,
1337 BaseClassDecl, /*NullCheckValue=*/false);
Anders Carlsson0ee33cf2009-09-12 16:16:49 +00001338
John McCall0953e762009-09-24 19:53:00 +00001339 return LValue::MakeAddr(Base, MakeQualifiers(E->getType()));
Anders Carlsson0ee33cf2009-09-12 16:16:49 +00001340 }
Anders Carlsson0ee33cf2009-09-12 16:16:49 +00001341 case CastExpr::CK_ToUnion: {
1342 llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType()));
1343 EmitAnyExpr(E->getSubExpr(), Temp, false);
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001344
John McCall0953e762009-09-24 19:53:00 +00001345 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
Anders Carlsson658e8122009-11-14 21:21:42 +00001346 }
Eli Friedmaneaae78a2009-11-16 05:48:01 +00001347 case CastExpr::CK_BaseToDerived: {
Anders Carlssona3697c92009-11-23 17:57:54 +00001348 const RecordType *BaseClassTy =
1349 E->getSubExpr()->getType()->getAs<RecordType>();
1350 CXXRecordDecl *BaseClassDecl =
1351 cast<CXXRecordDecl>(BaseClassTy->getDecl());
1352
1353 const RecordType *DerivedClassTy = E->getType()->getAs<RecordType>();
1354 CXXRecordDecl *DerivedClassDecl =
1355 cast<CXXRecordDecl>(DerivedClassTy->getDecl());
1356
1357 LValue LV = EmitLValue(E->getSubExpr());
1358
1359 // Perform the base-to-derived conversion
1360 llvm::Value *Derived =
1361 GetAddressOfDerivedClass(LV.getAddress(), BaseClassDecl,
1362 DerivedClassDecl, /*NullCheckValue=*/false);
1363
1364 return LValue::MakeAddr(Derived, MakeQualifiers(E->getType()));
Eli Friedmaneaae78a2009-11-16 05:48:01 +00001365 }
Anders Carlsson658e8122009-11-14 21:21:42 +00001366 case CastExpr::CK_BitCast: {
Eli Friedmaneaae78a2009-11-16 05:48:01 +00001367 // This must be a reinterpret_cast (or c-style equivalent).
1368 const ExplicitCastExpr *CE = cast<ExplicitCastExpr>(E);
Anders Carlsson658e8122009-11-14 21:21:42 +00001369
1370 LValue LV = EmitLValue(E->getSubExpr());
1371 llvm::Value *V = Builder.CreateBitCast(LV.getAddress(),
1372 ConvertType(CE->getTypeAsWritten()));
1373 return LValue::MakeAddr(V, MakeQualifiers(E->getType()));
1374 }
Anders Carlsson0ee33cf2009-09-12 16:16:49 +00001375 }
Chris Lattner75dfeda2009-03-18 18:28:57 +00001376}
1377
Fariborz Jahanian48620ba2009-10-20 23:29:04 +00001378LValue CodeGenFunction::EmitNullInitializationLValue(
1379 const CXXZeroInitValueExpr *E) {
1380 QualType Ty = E->getType();
1381 const llvm::Type *LTy = ConvertTypeForMem(Ty);
1382 llvm::AllocaInst *Alloc = CreateTempAlloca(LTy);
1383 unsigned Align = getContext().getTypeAlign(Ty)/8;
1384 Alloc->setAlignment(Align);
1385 LValue lvalue = LValue::MakeAddr(Alloc, Qualifiers());
1386 EmitMemSetToZero(lvalue.getAddress(), Ty);
1387 return lvalue;
1388}
1389
Reid Spencer5f016e22007-07-11 17:01:13 +00001390//===--------------------------------------------------------------------===//
1391// Expression Emission
1392//===--------------------------------------------------------------------===//
1393
Chris Lattner7016a702007-08-20 22:37:10 +00001394
Reid Spencer5f016e22007-07-11 17:01:13 +00001395RValue CodeGenFunction::EmitCallExpr(const CallExpr *E) {
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +00001396 // Builtins never have block type.
Daniel Dunbarce1d38b2009-01-09 16:50:52 +00001397 if (E->getCallee()->getType()->isBlockPointerType())
Anders Carlssonacfde802009-02-12 00:39:25 +00001398 return EmitBlockCallExpr(E);
Daniel Dunbarce1d38b2009-01-09 16:50:52 +00001399
Anders Carlsson774e7c62009-04-03 22:50:24 +00001400 if (const CXXMemberCallExpr *CE = dyn_cast<CXXMemberCallExpr>(E))
1401 return EmitCXXMemberCallExpr(CE);
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001402
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +00001403 const Decl *TargetDecl = 0;
Daniel Dunbardd7b8972009-02-20 19:34:33 +00001404 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E->getCallee())) {
1405 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CE->getSubExpr())) {
1406 TargetDecl = DRE->getDecl();
1407 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(TargetDecl))
Douglas Gregor7814e6d2009-09-12 00:22:50 +00001408 if (unsigned builtinID = FD->getBuiltinID())
Daniel Dunbardd7b8972009-02-20 19:34:33 +00001409 return EmitBuiltinExpr(FD, builtinID, E);
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +00001410 }
1411 }
1412
Chris Lattner5db7ae52009-06-13 00:26:38 +00001413 if (const CXXOperatorCallExpr *CE = dyn_cast<CXXOperatorCallExpr>(E))
Anders Carlsson0f294632009-05-27 04:18:27 +00001414 if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(TargetDecl))
1415 return EmitCXXOperatorMemberCallExpr(CE, MD);
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001416
Douglas Gregora71d8192009-09-04 17:36:40 +00001417 if (isa<CXXPseudoDestructorExpr>(E->getCallee())) {
1418 // C++ [expr.pseudo]p1:
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001419 // The result shall only be used as the operand for the function call
Douglas Gregora71d8192009-09-04 17:36:40 +00001420 // operator (), and the result of such a call has type void. The only
1421 // effect is the evaluation of the postfix-expression before the dot or
1422 // arrow.
1423 EmitScalarExpr(E->getCallee());
1424 return RValue::get(0);
1425 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001426
Chris Lattner7f02f722007-08-24 05:35:26 +00001427 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
Anders Carlsson98647712009-05-27 01:22:39 +00001428 return EmitCall(Callee, E->getCallee()->getType(),
1429 E->arg_begin(), E->arg_end(), TargetDecl);
Chris Lattnerc5e940f2007-08-31 04:44:06 +00001430}
1431
Daniel Dunbar80e62c22008-09-04 03:20:13 +00001432LValue CodeGenFunction::EmitBinaryOperatorLValue(const BinaryOperator *E) {
Chris Lattner7a574cc2009-05-12 21:28:12 +00001433 // Comma expressions just emit their LHS then their RHS as an l-value.
1434 if (E->getOpcode() == BinaryOperator::Comma) {
1435 EmitAnyExpr(E->getLHS());
1436 return EmitLValue(E->getRHS());
1437 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001438
Fariborz Jahanian52f08bc2009-10-26 21:58:25 +00001439 if (E->getOpcode() == BinaryOperator::PtrMemD ||
1440 E->getOpcode() == BinaryOperator::PtrMemI)
Fariborz Jahanian8bfd31f2009-10-22 22:57:31 +00001441 return EmitPointerToDataMemberBinaryExpr(E);
1442
Daniel Dunbar80e62c22008-09-04 03:20:13 +00001443 // Can only get l-value for binary operator expressions which are a
1444 // simple assignment of aggregate type.
1445 if (E->getOpcode() != BinaryOperator::Assign)
1446 return EmitUnsupportedLValue(E, "binary l-value expression");
1447
Anders Carlsson86aa0cd2009-10-19 18:28:22 +00001448 if (!hasAggregateLLVMType(E->getType())) {
1449 // Emit the LHS as an l-value.
1450 LValue LV = EmitLValue(E->getLHS());
1451
1452 llvm::Value *RHS = EmitScalarExpr(E->getRHS());
1453 EmitStoreOfScalar(RHS, LV.getAddress(), LV.isVolatileQualified(),
1454 E->getType());
1455 return LV;
1456 }
1457
Daniel Dunbar80e62c22008-09-04 03:20:13 +00001458 llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType()));
1459 EmitAggExpr(E, Temp, false);
1460 // FIXME: Are these qualifiers correct?
John McCall0953e762009-09-24 19:53:00 +00001461 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
Daniel Dunbar80e62c22008-09-04 03:20:13 +00001462}
1463
Christopher Lamb22c940e2007-12-29 05:02:41 +00001464LValue CodeGenFunction::EmitCallExprLValue(const CallExpr *E) {
Christopher Lamb22c940e2007-12-29 05:02:41 +00001465 RValue RV = EmitCallExpr(E);
Anders Carlsson48265682009-05-27 01:45:47 +00001466
Chris Lattnereb99b012009-10-28 17:39:19 +00001467 if (!RV.isScalar())
1468 return LValue::MakeAddr(RV.getAggregateAddr(),MakeQualifiers(E->getType()));
1469
1470 assert(E->getCallReturnType()->isReferenceType() &&
1471 "Can't have a scalar return unless the return type is a "
1472 "reference type!");
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001473
Chris Lattnereb99b012009-10-28 17:39:19 +00001474 return LValue::MakeAddr(RV.getScalarVal(), MakeQualifiers(E->getType()));
Christopher Lamb22c940e2007-12-29 05:02:41 +00001475}
1476
Daniel Dunbar5b5c9ef2009-02-11 20:59:32 +00001477LValue CodeGenFunction::EmitVAArgExprLValue(const VAArgExpr *E) {
1478 // FIXME: This shouldn't require another copy.
1479 llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType()));
1480 EmitAggExpr(E, Temp, false);
John McCall0953e762009-09-24 19:53:00 +00001481 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
Daniel Dunbar5b5c9ef2009-02-11 20:59:32 +00001482}
1483
Anders Carlssonb58d0172009-05-30 23:23:33 +00001484LValue CodeGenFunction::EmitCXXConstructLValue(const CXXConstructExpr *E) {
1485 llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(E->getType()), "tmp");
1486 EmitCXXConstructExpr(Temp, E);
John McCall0953e762009-09-24 19:53:00 +00001487 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
Anders Carlssonb58d0172009-05-30 23:23:33 +00001488}
1489
Anders Carlssone61c9e82009-05-30 23:30:54 +00001490LValue
Mike Stumpc2e84ae2009-11-15 08:09:41 +00001491CodeGenFunction::EmitCXXTypeidLValue(const CXXTypeidExpr *E) {
1492 llvm::Value *Temp = EmitCXXTypeidExpr(E);
1493 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
1494}
1495
1496LValue
Anders Carlssone61c9e82009-05-30 23:30:54 +00001497CodeGenFunction::EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr *E) {
1498 LValue LV = EmitLValue(E->getSubExpr());
Anders Carlsson543ac0c2009-05-31 00:34:10 +00001499 PushCXXTemporary(E->getTemporary(), LV.getAddress());
Anders Carlssone61c9e82009-05-30 23:30:54 +00001500 return LV;
1501}
1502
Daniel Dunbar0a04d772008-08-23 10:51:21 +00001503LValue CodeGenFunction::EmitObjCMessageExprLValue(const ObjCMessageExpr *E) {
1504 // Can only get l-value for message expression returning aggregate type
1505 RValue RV = EmitObjCMessageExpr(E);
1506 // FIXME: can this be volatile?
John McCall0953e762009-09-24 19:53:00 +00001507 return LValue::MakeAddr(RV.getAggregateAddr(), MakeQualifiers(E->getType()));
Daniel Dunbar0a04d772008-08-23 10:51:21 +00001508}
1509
Daniel Dunbar2a031922009-04-22 05:08:15 +00001510llvm::Value *CodeGenFunction::EmitIvarOffset(const ObjCInterfaceDecl *Interface,
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +00001511 const ObjCIvarDecl *Ivar) {
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001512 return CGM.getObjCRuntime().EmitIvarOffset(*this, Interface, Ivar);
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +00001513}
1514
Fariborz Jahanian45012a72009-02-03 00:09:52 +00001515LValue CodeGenFunction::EmitLValueForIvar(QualType ObjectTy,
1516 llvm::Value *BaseValue,
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +00001517 const ObjCIvarDecl *Ivar,
1518 unsigned CVRQualifiers) {
Chris Lattner26f074b2009-04-17 17:44:48 +00001519 return CGM.getObjCRuntime().EmitObjCValueForIvar(*this, ObjectTy, BaseValue,
Daniel Dunbar525c9b72009-04-21 01:19:28 +00001520 Ivar, CVRQualifiers);
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +00001521}
1522
1523LValue CodeGenFunction::EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E) {
Anders Carlsson29b7e502008-08-25 01:53:23 +00001524 // FIXME: A lot of the code below could be shared with EmitMemberExpr.
1525 llvm::Value *BaseValue = 0;
1526 const Expr *BaseExpr = E->getBase();
John McCall0953e762009-09-24 19:53:00 +00001527 Qualifiers BaseQuals;
Fariborz Jahanian45012a72009-02-03 00:09:52 +00001528 QualType ObjectTy;
Anders Carlsson29b7e502008-08-25 01:53:23 +00001529 if (E->isArrow()) {
1530 BaseValue = EmitScalarExpr(BaseExpr);
Steve Naroff14108da2009-07-10 23:34:53 +00001531 ObjectTy = BaseExpr->getType()->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00001532 BaseQuals = ObjectTy.getQualifiers();
Anders Carlsson29b7e502008-08-25 01:53:23 +00001533 } else {
1534 LValue BaseLV = EmitLValue(BaseExpr);
1535 // FIXME: this isn't right for bitfields.
1536 BaseValue = BaseLV.getAddress();
Fariborz Jahanian45012a72009-02-03 00:09:52 +00001537 ObjectTy = BaseExpr->getType();
John McCall0953e762009-09-24 19:53:00 +00001538 BaseQuals = ObjectTy.getQualifiers();
Anders Carlsson29b7e502008-08-25 01:53:23 +00001539 }
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +00001540
Fariborz Jahaniandbf3cfd2009-09-16 23:11:23 +00001541 LValue LV =
John McCall0953e762009-09-24 19:53:00 +00001542 EmitLValueForIvar(ObjectTy, BaseValue, E->getDecl(),
1543 BaseQuals.getCVRQualifiers());
Fariborz Jahaniandbf3cfd2009-09-16 23:11:23 +00001544 setObjCGCLValueClass(getContext(), E, LV);
1545 return LV;
Chris Lattner391d77a2008-03-30 23:03:07 +00001546}
1547
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001548LValue
Daniel Dunbar85c59ed2008-08-29 08:11:39 +00001549CodeGenFunction::EmitObjCPropertyRefLValue(const ObjCPropertyRefExpr *E) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001550 // This is a special l-value that just issues sends when we load or store
1551 // through it.
Daniel Dunbar85c59ed2008-08-29 08:11:39 +00001552 return LValue::MakePropertyRef(E, E->getType().getCVRQualifiers());
1553}
1554
Chris Lattnereb99b012009-10-28 17:39:19 +00001555LValue CodeGenFunction::EmitObjCKVCRefLValue(
Fariborz Jahanian09105f52009-08-20 17:02:02 +00001556 const ObjCImplicitSetterGetterRefExpr *E) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001557 // This is a special l-value that just issues sends when we load or store
1558 // through it.
Fariborz Jahanian43f44702008-11-22 22:30:21 +00001559 return LValue::MakeKVCRef(E, E->getType().getCVRQualifiers());
1560}
1561
Chris Lattnereb99b012009-10-28 17:39:19 +00001562LValue CodeGenFunction::EmitObjCSuperExprLValue(const ObjCSuperExpr *E) {
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00001563 return EmitUnsupportedLValue(E, "use of super");
1564}
1565
Chris Lattner65459942009-04-25 19:35:26 +00001566LValue CodeGenFunction::EmitStmtExprLValue(const StmtExpr *E) {
Chris Lattner65459942009-04-25 19:35:26 +00001567 // Can only get l-value for message expression returning aggregate type
1568 RValue RV = EmitAnyExprToTemp(E);
1569 // FIXME: can this be volatile?
John McCall0953e762009-09-24 19:53:00 +00001570 return LValue::MakeAddr(RV.getAggregateAddr(), MakeQualifiers(E->getType()));
Chris Lattner65459942009-04-25 19:35:26 +00001571}
1572
1573
Anders Carlsson909fbf72009-11-07 22:00:15 +00001574LValue CodeGenFunction::EmitPointerToDataMemberLValue(const FieldDecl *Field) {
Fariborz Jahanian39762952009-10-21 21:01:47 +00001575 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(Field->getDeclContext());
1576 QualType NNSpecTy =
1577 getContext().getCanonicalType(
1578 getContext().getTypeDeclType(const_cast<CXXRecordDecl*>(ClassDecl)));
Fariborz Jahaniana6362992009-10-21 18:38:00 +00001579 NNSpecTy = getContext().getPointerType(NNSpecTy);
1580 llvm::Value *V = llvm::Constant::getNullValue(ConvertType(NNSpecTy));
Anders Carlsson0ed303c2009-11-17 03:57:07 +00001581 LValue MemExpLV = EmitLValueForField(V, Field, /*isUnion=*/false,
1582 /*Qualifiers=*/0);
Chris Lattnereb99b012009-10-28 17:39:19 +00001583 const llvm::Type *ResultType = ConvertType(getContext().getPointerDiffType());
1584 V = Builder.CreatePtrToInt(MemExpLV.getAddress(), ResultType, "datamember");
Anders Carlsson909fbf72009-11-07 22:00:15 +00001585 return LValue::MakeAddr(V, MakeQualifiers(Field->getType()));
Fariborz Jahaniana6362992009-10-21 18:38:00 +00001586}
1587
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001588RValue CodeGenFunction::EmitCall(llvm::Value *Callee, QualType CalleeType,
Anders Carlsson98647712009-05-27 01:22:39 +00001589 CallExpr::const_arg_iterator ArgBeg,
1590 CallExpr::const_arg_iterator ArgEnd,
1591 const Decl *TargetDecl) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001592 // Get the actual function type. The callee type will always be a pointer to
1593 // function type or a block pointer type.
1594 assert(CalleeType->isFunctionPointerType() &&
Anders Carlsson8ac67a72009-04-07 18:53:02 +00001595 "Call must have function pointer type!");
1596
John McCall00a1ad92009-10-23 08:22:42 +00001597 CalleeType = getContext().getCanonicalType(CalleeType);
1598
1599 QualType FnType = cast<PointerType>(CalleeType)->getPointeeType();
1600 QualType ResultType = cast<FunctionType>(FnType)->getResultType();
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001601
1602 CallArgList Args;
John McCall00a1ad92009-10-23 08:22:42 +00001603 EmitCallArgs(Args, dyn_cast<FunctionProtoType>(FnType), ArgBeg, ArgEnd);
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001604
Daniel Dunbar8a9f3fd2009-09-11 22:25:00 +00001605 // FIXME: We should not need to do this, it should be part of the function
1606 // type.
1607 unsigned CallingConvention = 0;
1608 if (const llvm::Function *F =
1609 dyn_cast<llvm::Function>(Callee->stripPointerCasts()))
1610 CallingConvention = F->getCallingConv();
1611 return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args,
1612 CallingConvention),
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +00001613 Callee, Args, TargetDecl);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001614}
Fariborz Jahanian8bfd31f2009-10-22 22:57:31 +00001615
Chris Lattnereb99b012009-10-28 17:39:19 +00001616LValue CodeGenFunction::
1617EmitPointerToDataMemberBinaryExpr(const BinaryOperator *E) {
Eli Friedman1c5c1a02009-11-18 05:01:17 +00001618 llvm::Value *BaseV;
Fariborz Jahanian52f08bc2009-10-26 21:58:25 +00001619 if (E->getOpcode() == BinaryOperator::PtrMemI)
Eli Friedman1c5c1a02009-11-18 05:01:17 +00001620 BaseV = EmitScalarExpr(E->getLHS());
1621 else
1622 BaseV = EmitLValue(E->getLHS()).getAddress();
Fariborz Jahanian8bfd31f2009-10-22 22:57:31 +00001623 const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(getLLVMContext());
1624 BaseV = Builder.CreateBitCast(BaseV, i8Ty);
Eli Friedman1c5c1a02009-11-18 05:01:17 +00001625 llvm::Value *OffsetV = EmitScalarExpr(E->getRHS());
Fariborz Jahanian8bfd31f2009-10-22 22:57:31 +00001626 llvm::Value *AddV = Builder.CreateInBoundsGEP(BaseV, OffsetV, "add.ptr");
Chris Lattnereb99b012009-10-28 17:39:19 +00001627
Fariborz Jahanian8bfd31f2009-10-22 22:57:31 +00001628 QualType Ty = E->getRHS()->getType();
Chris Lattnereb99b012009-10-28 17:39:19 +00001629 Ty = Ty->getAs<MemberPointerType>()->getPointeeType();
1630
1631 const llvm::Type *PType = ConvertType(getContext().getPointerType(Ty));
Fariborz Jahanian8bfd31f2009-10-22 22:57:31 +00001632 AddV = Builder.CreateBitCast(AddV, PType);
Chris Lattnereb99b012009-10-28 17:39:19 +00001633 return LValue::MakeAddr(AddV, MakeQualifiers(Ty));
Fariborz Jahanian8bfd31f2009-10-22 22:57:31 +00001634}
1635