blob: 15cb72e9fb88900c57b308dc387e7c5d8ecee75f [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
Anders Carlsson2da76932009-08-16 17:54:29 +0000140 CleanupScope scope(*this);
141 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 =
151 GetAddressCXXOfBaseClass(Derived, DerivedClassDecl, BaseClassDecl,
152 /*NullCheckValue=*/false);
153 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
Argyrios Kyrtzidise3a09e62008-09-10 02:36:38 +0000261 case Expr::CXXConditionDeclExprClass:
262 return EmitCXXConditionDeclLValue(cast<CXXConditionDeclExpr>(E));
Anders Carlssonb58d0172009-05-30 23:23:33 +0000263 case Expr::CXXTemporaryObjectExprClass:
264 case Expr::CXXConstructExprClass:
Anders Carlssone61c9e82009-05-30 23:30:54 +0000265 return EmitCXXConstructLValue(cast<CXXConstructExpr>(E));
266 case Expr::CXXBindTemporaryExprClass:
267 return EmitCXXBindTemporaryLValue(cast<CXXBindTemporaryExpr>(E));
Anders Carlssonb9ea0b52009-09-14 01:10:45 +0000268 case Expr::CXXExprWithTemporariesClass:
269 return EmitCXXExprWithTemporariesLValue(cast<CXXExprWithTemporaries>(E));
Anders Carlssone61c9e82009-05-30 23:30:54 +0000270
Daniel Dunbar0a04d772008-08-23 10:51:21 +0000271 case Expr::ObjCMessageExprClass:
272 return EmitObjCMessageExprLValue(cast<ObjCMessageExpr>(E));
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000273 case Expr::ObjCIvarRefExprClass:
Chris Lattner391d77a2008-03-30 23:03:07 +0000274 return EmitObjCIvarRefLValue(cast<ObjCIvarRefExpr>(E));
Daniel Dunbar6ba82a42008-08-25 20:45:57 +0000275 case Expr::ObjCPropertyRefExprClass:
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000276 return EmitObjCPropertyRefLValue(cast<ObjCPropertyRefExpr>(E));
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000277 case Expr::ObjCImplicitSetterGetterRefExprClass:
278 return EmitObjCKVCRefLValue(cast<ObjCImplicitSetterGetterRefExpr>(E));
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000279 case Expr::ObjCSuperExprClass:
Chris Lattner65459942009-04-25 19:35:26 +0000280 return EmitObjCSuperExprLValue(cast<ObjCSuperExpr>(E));
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000281
Chris Lattner65459942009-04-25 19:35:26 +0000282 case Expr::StmtExprClass:
283 return EmitStmtExprLValue(cast<StmtExpr>(E));
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000284 case Expr::UnaryOperatorClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000285 return EmitUnaryOpLValue(cast<UnaryOperator>(E));
286 case Expr::ArraySubscriptExprClass:
287 return EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E));
Nate Begeman213541a2008-04-18 23:10:10 +0000288 case Expr::ExtVectorElementExprClass:
289 return EmitExtVectorElementExpr(cast<ExtVectorElementExpr>(E));
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000290 case Expr::MemberExprClass:
Douglas Gregorbd4c4ae2009-08-26 22:36:53 +0000291 return EmitMemberExpr(cast<MemberExpr>(E));
Eli Friedman06e863f2008-05-13 23:18:27 +0000292 case Expr::CompoundLiteralExprClass:
293 return EmitCompoundLiteralLValue(cast<CompoundLiteralExpr>(E));
Daniel Dunbar90345582009-03-24 02:38:23 +0000294 case Expr::ConditionalOperatorClass:
Anders Carlsson6fcec8b2009-09-15 16:35:24 +0000295 return EmitConditionalOperatorLValue(cast<ConditionalOperator>(E));
Chris Lattner670a62c2008-12-12 05:35:08 +0000296 case Expr::ChooseExprClass:
Eli Friedman79769322009-03-04 05:52:32 +0000297 return EmitLValue(cast<ChooseExpr>(E)->getChosenSubExpr(getContext()));
Chris Lattnerc3953a62009-03-18 04:02:57 +0000298 case Expr::ImplicitCastExprClass:
299 case Expr::CStyleCastExprClass:
300 case Expr::CXXFunctionalCastExprClass:
301 case Expr::CXXStaticCastExprClass:
302 case Expr::CXXDynamicCastExprClass:
303 case Expr::CXXReinterpretCastExprClass:
304 case Expr::CXXConstCastExprClass:
Chris Lattner75dfeda2009-03-18 18:28:57 +0000305 return EmitCastLValue(cast<CastExpr>(E));
Fariborz Jahanian48620ba2009-10-20 23:29:04 +0000306 case Expr::CXXZeroInitValueExprClass:
307 return EmitNullInitializationLValue(cast<CXXZeroInitValueExpr>(E));
Reid Spencer5f016e22007-07-11 17:01:13 +0000308 }
309}
310
Daniel Dunbar9d9cc872009-02-10 00:57:50 +0000311llvm::Value *CodeGenFunction::EmitLoadOfScalar(llvm::Value *Addr, bool Volatile,
312 QualType Ty) {
313 llvm::Value *V = Builder.CreateLoad(Addr, Volatile, "tmp");
314
Anders Carlsson3bb423b2009-05-19 19:36:19 +0000315 // Bool can have different representation in memory than in registers.
Daniel Dunbar9d9cc872009-02-10 00:57:50 +0000316 if (Ty->isBooleanType())
Owen Anderson0032b272009-08-13 21:57:51 +0000317 if (V->getType() != llvm::Type::getInt1Ty(VMContext))
318 V = Builder.CreateTrunc(V, llvm::Type::getInt1Ty(VMContext), "tobool");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000319
Daniel Dunbar9d9cc872009-02-10 00:57:50 +0000320 return V;
321}
322
323void CodeGenFunction::EmitStoreOfScalar(llvm::Value *Value, llvm::Value *Addr,
Anders Carlssonb4aa4662009-05-19 18:50:41 +0000324 bool Volatile, QualType Ty) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000325
Anders Carlsson3bb423b2009-05-19 19:36:19 +0000326 if (Ty->isBooleanType()) {
327 // Bool can have different representation in memory than in registers.
328 const llvm::Type *SrcTy = Value->getType();
329 const llvm::PointerType *DstPtr = cast<llvm::PointerType>(Addr->getType());
330 if (DstPtr->getElementType() != SrcTy) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000331 const llvm::Type *MemTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000332 llvm::PointerType::get(SrcTy, DstPtr->getAddressSpace());
Anders Carlsson3bb423b2009-05-19 19:36:19 +0000333 Addr = Builder.CreateBitCast(Addr, MemTy, "storetmp");
334 }
Daniel Dunbar9d9cc872009-02-10 00:57:50 +0000335 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000336 Builder.CreateStore(Value, Addr, Volatile);
Daniel Dunbar9d9cc872009-02-10 00:57:50 +0000337}
338
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000339/// EmitLoadOfLValue - Given an expression that represents a value lvalue, this
340/// method emits the address of the lvalue, then loads the result as an rvalue,
341/// returning the rvalue.
Reid Spencer5f016e22007-07-11 17:01:13 +0000342RValue CodeGenFunction::EmitLoadOfLValue(LValue LV, QualType ExprType) {
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +0000343 if (LV.isObjCWeak()) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000344 // load of a __weak object.
Fariborz Jahanian6dc23172008-11-18 21:45:40 +0000345 llvm::Value *AddrWeakObj = LV.getAddress();
Chris Lattnereb99b012009-10-28 17:39:19 +0000346 return RValue::get(CGM.getObjCRuntime().EmitObjCWeakRead(*this,
347 AddrWeakObj));
Fariborz Jahanian6dc23172008-11-18 21:45:40 +0000348 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000349
Reid Spencer5f016e22007-07-11 17:01:13 +0000350 if (LV.isSimple()) {
351 llvm::Value *Ptr = LV.getAddress();
352 const llvm::Type *EltTy =
353 cast<llvm::PointerType>(Ptr->getType())->getElementType();
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000354
Reid Spencer5f016e22007-07-11 17:01:13 +0000355 // Simple scalar l-value.
Daniel Dunbar9d9cc872009-02-10 00:57:50 +0000356 if (EltTy->isSingleValueType())
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000357 return RValue::get(EmitLoadOfScalar(Ptr, LV.isVolatileQualified(),
Daniel Dunbar9d9cc872009-02-10 00:57:50 +0000358 ExprType));
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000359
Chris Lattner883f6a72007-08-11 00:04:45 +0000360 assert(ExprType->isFunctionType() && "Unknown scalar value");
361 return RValue::get(Ptr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000362 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000363
Reid Spencer5f016e22007-07-11 17:01:13 +0000364 if (LV.isVectorElt()) {
Eli Friedman1e692ac2008-06-13 23:01:12 +0000365 llvm::Value *Vec = Builder.CreateLoad(LV.getVectorAddr(),
366 LV.isVolatileQualified(), "tmp");
Reid Spencer5f016e22007-07-11 17:01:13 +0000367 return RValue::get(Builder.CreateExtractElement(Vec, LV.getVectorIdx(),
368 "vecext"));
369 }
Chris Lattner46ea8eb2007-08-03 00:16:29 +0000370
371 // If this is a reference to a subset of the elements of a vector, either
372 // shuffle the input or extract/insert them as appropriate.
Nate Begeman213541a2008-04-18 23:10:10 +0000373 if (LV.isExtVectorElt())
374 return EmitLoadOfExtVectorElementLValue(LV, ExprType);
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000375
376 if (LV.isBitfield())
377 return EmitLoadOfBitfieldLValue(LV, ExprType);
378
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000379 if (LV.isPropertyRef())
380 return EmitLoadOfPropertyRefLValue(LV, ExprType);
381
Chris Lattner73525de2009-02-16 21:11:58 +0000382 assert(LV.isKVCRef() && "Unknown LValue type!");
383 return EmitLoadOfKVCRefLValue(LV, ExprType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000384}
385
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000386RValue CodeGenFunction::EmitLoadOfBitfieldLValue(LValue LV,
387 QualType ExprType) {
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000388 unsigned StartBit = LV.getBitfieldStartBit();
389 unsigned BitfieldSize = LV.getBitfieldSize();
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000390 llvm::Value *Ptr = LV.getBitfieldAddr();
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000391
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000392 const llvm::Type *EltTy =
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000393 cast<llvm::PointerType>(Ptr->getType())->getElementType();
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000394 unsigned EltTySize = CGM.getTargetData().getTypeSizeInBits(EltTy);
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000395
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000396 // In some cases the bitfield may straddle two memory locations. Currently we
397 // load the entire bitfield, then do the magic to sign-extend it if
398 // necessary. This results in somewhat more code than necessary for the common
399 // case (one load), since two shifts accomplish both the masking and sign
400 // extension.
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000401 unsigned LowBits = std::min(BitfieldSize, EltTySize - StartBit);
402 llvm::Value *Val = Builder.CreateLoad(Ptr, LV.isVolatileQualified(), "tmp");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000403
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000404 // Shift to proper location.
Daniel Dunbarf3edc2f2008-11-13 02:20:34 +0000405 if (StartBit)
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000406 Val = Builder.CreateLShr(Val, llvm::ConstantInt::get(EltTy, StartBit),
Daniel Dunbarf3edc2f2008-11-13 02:20:34 +0000407 "bf.lo");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000408
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000409 // Mask off unused bits.
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000410 llvm::Constant *LowMask = llvm::ConstantInt::get(VMContext,
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000411 llvm::APInt::getLowBitsSet(EltTySize, LowBits));
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000412 Val = Builder.CreateAnd(Val, LowMask, "bf.lo.cleared");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000413
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000414 // Fetch the high bits if necessary.
415 if (LowBits < BitfieldSize) {
416 unsigned HighBits = BitfieldSize - LowBits;
Owen Anderson0032b272009-08-13 21:57:51 +0000417 llvm::Value *HighPtr = Builder.CreateGEP(Ptr, llvm::ConstantInt::get(
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000418 llvm::Type::getInt32Ty(VMContext), 1), "bf.ptr.hi");
419 llvm::Value *HighVal = Builder.CreateLoad(HighPtr,
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000420 LV.isVolatileQualified(),
421 "tmp");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000422
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000423 // Mask off unused bits.
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000424 llvm::Constant *HighMask = llvm::ConstantInt::get(VMContext,
425 llvm::APInt::getLowBitsSet(EltTySize, HighBits));
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000426 HighVal = Builder.CreateAnd(HighVal, HighMask, "bf.lo.cleared");
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000427
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000428 // Shift to proper location and or in to bitfield value.
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000429 HighVal = Builder.CreateShl(HighVal,
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000430 llvm::ConstantInt::get(EltTy, LowBits));
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000431 Val = Builder.CreateOr(Val, HighVal, "bf.val");
432 }
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000433
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000434 // Sign extend if necessary.
435 if (LV.isBitfieldSigned()) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000436 llvm::Value *ExtraBits = llvm::ConstantInt::get(EltTy,
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000437 EltTySize - BitfieldSize);
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000438 Val = Builder.CreateAShr(Builder.CreateShl(Val, ExtraBits),
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000439 ExtraBits, "bf.val.sext");
440 }
Eli Friedman316bb1b2008-05-17 20:03:47 +0000441
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000442 // The bitfield type and the normal type differ when the storage sizes differ
443 // (currently just _Bool).
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000444 Val = Builder.CreateIntCast(Val, ConvertType(ExprType), false, "tmp");
Eli Friedman316bb1b2008-05-17 20:03:47 +0000445
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000446 return RValue::get(Val);
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000447}
448
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000449RValue CodeGenFunction::EmitLoadOfPropertyRefLValue(LValue LV,
450 QualType ExprType) {
451 return EmitObjCPropertyGet(LV.getPropertyRefExpr());
452}
453
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000454RValue CodeGenFunction::EmitLoadOfKVCRefLValue(LValue LV,
455 QualType ExprType) {
456 return EmitObjCPropertyGet(LV.getKVCRefExpr());
457}
458
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000459// If this is a reference to a subset of the elements of a vector, create an
460// appropriate shufflevector.
Nate Begeman213541a2008-04-18 23:10:10 +0000461RValue CodeGenFunction::EmitLoadOfExtVectorElementLValue(LValue LV,
462 QualType ExprType) {
Eli Friedman1e692ac2008-06-13 23:01:12 +0000463 llvm::Value *Vec = Builder.CreateLoad(LV.getExtVectorAddr(),
464 LV.isVolatileQualified(), "tmp");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000465
Nate Begeman8a997642008-05-09 06:41:27 +0000466 const llvm::Constant *Elts = LV.getExtVectorElts();
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000467
468 // If the result of the expression is a non-vector type, we must be extracting
469 // a single element. Just codegen as an extractelement.
John McCall183700f2009-09-21 23:43:11 +0000470 const VectorType *ExprVT = ExprType->getAs<VectorType>();
Chris Lattnercf60cd22007-08-10 17:10:08 +0000471 if (!ExprVT) {
Dan Gohman4f8d1232008-05-22 00:50:06 +0000472 unsigned InIdx = getAccessedFieldNo(0, Elts);
Owen Anderson0032b272009-08-13 21:57:51 +0000473 llvm::Value *Elt = llvm::ConstantInt::get(
474 llvm::Type::getInt32Ty(VMContext), InIdx);
Chris Lattner34cdc862007-08-03 16:18:34 +0000475 return RValue::get(Builder.CreateExtractElement(Vec, Elt, "tmp"));
476 }
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000477
478 // Always use shuffle vector to try to retain the original program structure
Chris Lattnercf60cd22007-08-10 17:10:08 +0000479 unsigned NumResultElts = ExprVT->getNumElements();
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000480
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000481 llvm::SmallVector<llvm::Constant*, 4> Mask;
Chris Lattner34cdc862007-08-03 16:18:34 +0000482 for (unsigned i = 0; i != NumResultElts; ++i) {
Dan Gohman4f8d1232008-05-22 00:50:06 +0000483 unsigned InIdx = getAccessedFieldNo(i, Elts);
Owen Anderson0032b272009-08-13 21:57:51 +0000484 Mask.push_back(llvm::ConstantInt::get(
485 llvm::Type::getInt32Ty(VMContext), InIdx));
Chris Lattner34cdc862007-08-03 16:18:34 +0000486 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000487
Owen Anderson4a289322009-07-28 21:22:35 +0000488 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000489 Vec = Builder.CreateShuffleVector(Vec,
Owen Anderson03e20502009-07-30 23:11:26 +0000490 llvm::UndefValue::get(Vec->getType()),
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000491 MaskV, "tmp");
492 return RValue::get(Vec);
Chris Lattner34cdc862007-08-03 16:18:34 +0000493}
494
495
Reid Spencer5f016e22007-07-11 17:01:13 +0000496
497/// EmitStoreThroughLValue - Store the specified rvalue into the specified
498/// lvalue, where both are guaranteed to the have the same type, and that type
499/// is 'Ty'.
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000500void CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst,
Reid Spencer5f016e22007-07-11 17:01:13 +0000501 QualType Ty) {
Chris Lattner017d6aa2007-08-03 16:28:33 +0000502 if (!Dst.isSimple()) {
503 if (Dst.isVectorElt()) {
504 // Read/modify/write the vector, inserting the new element.
Eli Friedman1e692ac2008-06-13 23:01:12 +0000505 llvm::Value *Vec = Builder.CreateLoad(Dst.getVectorAddr(),
506 Dst.isVolatileQualified(), "tmp");
Chris Lattner9b655512007-08-31 22:49:20 +0000507 Vec = Builder.CreateInsertElement(Vec, Src.getScalarVal(),
Chris Lattner017d6aa2007-08-03 16:28:33 +0000508 Dst.getVectorIdx(), "vecins");
Eli Friedman1e692ac2008-06-13 23:01:12 +0000509 Builder.CreateStore(Vec, Dst.getVectorAddr(),Dst.isVolatileQualified());
Chris Lattner017d6aa2007-08-03 16:28:33 +0000510 return;
511 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000512
Nate Begeman213541a2008-04-18 23:10:10 +0000513 // If this is an update of extended vector elements, insert them as
514 // appropriate.
515 if (Dst.isExtVectorElt())
516 return EmitStoreThroughExtVectorComponentLValue(Src, Dst, Ty);
Lauro Ramos Venancioa0c5d0e2008-01-22 22:36:45 +0000517
518 if (Dst.isBitfield())
519 return EmitStoreThroughBitfieldLValue(Src, Dst, Ty);
520
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000521 if (Dst.isPropertyRef())
522 return EmitStoreThroughPropertyRefLValue(Src, Dst, Ty);
523
Chris Lattnereb99b012009-10-28 17:39:19 +0000524 assert(Dst.isKVCRef() && "Unknown LValue type");
525 return EmitStoreThroughKVCRefLValue(Src, Dst, Ty);
Chris Lattner017d6aa2007-08-03 16:28:33 +0000526 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000527
Fariborz Jahanian4f676ed2009-02-21 00:30:43 +0000528 if (Dst.isObjCWeak() && !Dst.isNonGC()) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000529 // load of a __weak object.
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +0000530 llvm::Value *LvalueDst = Dst.getAddress();
531 llvm::Value *src = Src.getScalarVal();
Mike Stumpf33651c2009-04-14 00:57:29 +0000532 CGM.getObjCRuntime().EmitObjCWeakAssign(*this, src, LvalueDst);
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +0000533 return;
534 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000535
Fariborz Jahanian4f676ed2009-02-21 00:30:43 +0000536 if (Dst.isObjCStrong() && !Dst.isNonGC()) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000537 // load of a __strong object.
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +0000538 llvm::Value *LvalueDst = Dst.getAddress();
539 llvm::Value *src = Src.getScalarVal();
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000540 if (Dst.isObjCIvar()) {
541 assert(Dst.getBaseIvarExp() && "BaseIvarExp is NULL");
542 const llvm::Type *ResultType = ConvertType(getContext().LongTy);
543 llvm::Value *RHS = EmitScalarExpr(Dst.getBaseIvarExp());
Fariborz Jahanian76368e82009-09-25 00:00:20 +0000544 llvm::Value *dst = RHS;
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000545 RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast");
546 llvm::Value *LHS =
547 Builder.CreatePtrToInt(LvalueDst, ResultType, "sub.ptr.lhs.cast");
548 llvm::Value *BytesBetween = Builder.CreateSub(LHS, RHS, "ivar.offset");
Fariborz Jahanian76368e82009-09-25 00:00:20 +0000549 CGM.getObjCRuntime().EmitObjCIvarAssign(*this, src, dst,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000550 BytesBetween);
Chris Lattnereb99b012009-10-28 17:39:19 +0000551 } else if (Dst.isGlobalObjCRef())
Fariborz Jahanianbf63b872009-05-04 23:27:20 +0000552 CGM.getObjCRuntime().EmitObjCGlobalAssign(*this, src, LvalueDst);
553 else
554 CGM.getObjCRuntime().EmitObjCStrongCastAssign(*this, src, LvalueDst);
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +0000555 return;
556 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000557
Chris Lattner883f6a72007-08-11 00:04:45 +0000558 assert(Src.isScalar() && "Can't emit an agg store with this method");
Anders Carlssonb4aa4662009-05-19 18:50:41 +0000559 EmitStoreOfScalar(Src.getScalarVal(), Dst.getAddress(),
560 Dst.isVolatileQualified(), Ty);
Reid Spencer5f016e22007-07-11 17:01:13 +0000561}
562
Lauro Ramos Venancioa0c5d0e2008-01-22 22:36:45 +0000563void CodeGenFunction::EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst,
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000564 QualType Ty,
Daniel Dunbared3849b2008-11-19 09:36:46 +0000565 llvm::Value **Result) {
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000566 unsigned StartBit = Dst.getBitfieldStartBit();
567 unsigned BitfieldSize = Dst.getBitfieldSize();
Lauro Ramos Venancioa0c5d0e2008-01-22 22:36:45 +0000568 llvm::Value *Ptr = Dst.getBitfieldAddr();
Lauro Ramos Venancioa0c5d0e2008-01-22 22:36:45 +0000569
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000570 const llvm::Type *EltTy =
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000571 cast<llvm::PointerType>(Ptr->getType())->getElementType();
572 unsigned EltTySize = CGM.getTargetData().getTypeSizeInBits(EltTy);
573
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000574 // Get the new value, cast to the appropriate type and masked to exactly the
575 // size of the bit-field.
Daniel Dunbared3849b2008-11-19 09:36:46 +0000576 llvm::Value *SrcVal = Src.getScalarVal();
577 llvm::Value *NewVal = Builder.CreateIntCast(SrcVal, EltTy, false, "tmp");
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000578 llvm::Constant *Mask = llvm::ConstantInt::get(VMContext,
579 llvm::APInt::getLowBitsSet(EltTySize, BitfieldSize));
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000580 NewVal = Builder.CreateAnd(NewVal, Mask, "bf.value");
Lauro Ramos Venancioa0c5d0e2008-01-22 22:36:45 +0000581
Daniel Dunbared3849b2008-11-19 09:36:46 +0000582 // Return the new value of the bit-field, if requested.
583 if (Result) {
584 // Cast back to the proper type for result.
585 const llvm::Type *SrcTy = SrcVal->getType();
586 llvm::Value *SrcTrunc = Builder.CreateIntCast(NewVal, SrcTy, false,
587 "bf.reload.val");
588
589 // Sign extend if necessary.
590 if (Dst.isBitfieldSigned()) {
591 unsigned SrcTySize = CGM.getTargetData().getTypeSizeInBits(SrcTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000592 llvm::Value *ExtraBits = llvm::ConstantInt::get(SrcTy,
Daniel Dunbared3849b2008-11-19 09:36:46 +0000593 SrcTySize - BitfieldSize);
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000594 SrcTrunc = Builder.CreateAShr(Builder.CreateShl(SrcTrunc, ExtraBits),
Daniel Dunbared3849b2008-11-19 09:36:46 +0000595 ExtraBits, "bf.reload.sext");
596 }
597
598 *Result = SrcTrunc;
599 }
600
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000601 // In some cases the bitfield may straddle two memory locations. Emit the low
602 // part first and check to see if the high needs to be done.
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000603 unsigned LowBits = std::min(BitfieldSize, EltTySize - StartBit);
604 llvm::Value *LowVal = Builder.CreateLoad(Ptr, Dst.isVolatileQualified(),
605 "bf.prev.low");
Eli Friedman316bb1b2008-05-17 20:03:47 +0000606
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000607 // Compute the mask for zero-ing the low part of this bitfield.
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000608 llvm::Constant *InvMask =
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000609 llvm::ConstantInt::get(VMContext,
610 ~llvm::APInt::getBitsSet(EltTySize, StartBit, StartBit + LowBits));
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000611
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000612 // Compute the new low part as
613 // LowVal = (LowVal & InvMask) | (NewVal << StartBit),
614 // with the shift of NewVal implicitly stripping the high bits.
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000615 llvm::Value *NewLowVal =
616 Builder.CreateShl(NewVal, llvm::ConstantInt::get(EltTy, StartBit),
617 "bf.value.lo");
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000618 LowVal = Builder.CreateAnd(LowVal, InvMask, "bf.prev.lo.cleared");
619 LowVal = Builder.CreateOr(LowVal, NewLowVal, "bf.new.lo");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000620
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000621 // Write back.
622 Builder.CreateStore(LowVal, Ptr, Dst.isVolatileQualified());
Eli Friedman316bb1b2008-05-17 20:03:47 +0000623
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000624 // If the low part doesn't cover the bitfield emit a high part.
625 if (LowBits < BitfieldSize) {
626 unsigned HighBits = BitfieldSize - LowBits;
Owen Anderson0032b272009-08-13 21:57:51 +0000627 llvm::Value *HighPtr = Builder.CreateGEP(Ptr, llvm::ConstantInt::get(
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000628 llvm::Type::getInt32Ty(VMContext), 1), "bf.ptr.hi");
629 llvm::Value *HighVal = Builder.CreateLoad(HighPtr,
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000630 Dst.isVolatileQualified(),
631 "bf.prev.hi");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000632
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000633 // Compute the mask for zero-ing the high part of this bitfield.
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000634 llvm::Constant *InvMask =
635 llvm::ConstantInt::get(VMContext, ~llvm::APInt::getLowBitsSet(EltTySize,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000636 HighBits));
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000637
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000638 // Compute the new high part as
639 // HighVal = (HighVal & InvMask) | (NewVal lshr LowBits),
640 // where the high bits of NewVal have already been cleared and the
641 // shift stripping the low bits.
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000642 llvm::Value *NewHighVal =
643 Builder.CreateLShr(NewVal, llvm::ConstantInt::get(EltTy, LowBits),
644 "bf.value.high");
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000645 HighVal = Builder.CreateAnd(HighVal, InvMask, "bf.prev.hi.cleared");
646 HighVal = Builder.CreateOr(HighVal, NewHighVal, "bf.new.hi");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000647
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000648 // Write back.
649 Builder.CreateStore(HighVal, HighPtr, Dst.isVolatileQualified());
650 }
Lauro Ramos Venancioa0c5d0e2008-01-22 22:36:45 +0000651}
652
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000653void CodeGenFunction::EmitStoreThroughPropertyRefLValue(RValue Src,
654 LValue Dst,
655 QualType Ty) {
656 EmitObjCPropertySet(Dst.getPropertyRefExpr(), Src);
657}
658
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000659void CodeGenFunction::EmitStoreThroughKVCRefLValue(RValue Src,
660 LValue Dst,
661 QualType Ty) {
662 EmitObjCPropertySet(Dst.getKVCRefExpr(), Src);
663}
664
Nate Begeman213541a2008-04-18 23:10:10 +0000665void CodeGenFunction::EmitStoreThroughExtVectorComponentLValue(RValue Src,
666 LValue Dst,
667 QualType Ty) {
Chris Lattner017d6aa2007-08-03 16:28:33 +0000668 // This access turns into a read/modify/write of the vector. Load the input
669 // value now.
Eli Friedman1e692ac2008-06-13 23:01:12 +0000670 llvm::Value *Vec = Builder.CreateLoad(Dst.getExtVectorAddr(),
671 Dst.isVolatileQualified(), "tmp");
Nate Begeman8a997642008-05-09 06:41:27 +0000672 const llvm::Constant *Elts = Dst.getExtVectorElts();
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000673
Chris Lattner9b655512007-08-31 22:49:20 +0000674 llvm::Value *SrcVal = Src.getScalarVal();
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000675
John McCall183700f2009-09-21 23:43:11 +0000676 if (const VectorType *VTy = Ty->getAs<VectorType>()) {
Chris Lattner7e6b51b2007-08-03 16:37:04 +0000677 unsigned NumSrcElts = VTy->getNumElements();
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000678 unsigned NumDstElts =
679 cast<llvm::VectorType>(Vec->getType())->getNumElements();
680 if (NumDstElts == NumSrcElts) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000681 // Use shuffle vector is the src and destination are the same number of
682 // elements and restore the vector mask since it is on the side it will be
683 // stored.
Nate Begeman6e5dd862009-06-26 21:12:50 +0000684 llvm::SmallVector<llvm::Constant*, 4> Mask(NumDstElts);
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000685 for (unsigned i = 0; i != NumSrcElts; ++i) {
686 unsigned InIdx = getAccessedFieldNo(i, Elts);
Owen Anderson0032b272009-08-13 21:57:51 +0000687 Mask[InIdx] = llvm::ConstantInt::get(
688 llvm::Type::getInt32Ty(VMContext), i);
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000689 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000690
Owen Anderson4a289322009-07-28 21:22:35 +0000691 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000692 Vec = Builder.CreateShuffleVector(SrcVal,
Owen Anderson03e20502009-07-30 23:11:26 +0000693 llvm::UndefValue::get(Vec->getType()),
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000694 MaskV, "tmp");
Mike Stumpb3589f42009-07-30 22:28:39 +0000695 } else if (NumDstElts > NumSrcElts) {
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000696 // Extended the source vector to the same length and then shuffle it
697 // into the destination.
698 // FIXME: since we're shuffling with undef, can we just use the indices
699 // into that? This could be simpler.
700 llvm::SmallVector<llvm::Constant*, 4> ExtMask;
Chris Lattnereb99b012009-10-28 17:39:19 +0000701 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000702 unsigned i;
703 for (i = 0; i != NumSrcElts; ++i)
Chris Lattnereb99b012009-10-28 17:39:19 +0000704 ExtMask.push_back(llvm::ConstantInt::get(Int32Ty, i));
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000705 for (; i != NumDstElts; ++i)
Chris Lattnereb99b012009-10-28 17:39:19 +0000706 ExtMask.push_back(llvm::UndefValue::get(Int32Ty));
Owen Anderson4a289322009-07-28 21:22:35 +0000707 llvm::Value *ExtMaskV = llvm::ConstantVector::get(&ExtMask[0],
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000708 ExtMask.size());
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000709 llvm::Value *ExtSrcVal =
Daniel Dunbarbb767732009-02-17 18:31:04 +0000710 Builder.CreateShuffleVector(SrcVal,
Owen Anderson03e20502009-07-30 23:11:26 +0000711 llvm::UndefValue::get(SrcVal->getType()),
Daniel Dunbarbb767732009-02-17 18:31:04 +0000712 ExtMaskV, "tmp");
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000713 // build identity
714 llvm::SmallVector<llvm::Constant*, 4> Mask;
Chris Lattnereb99b012009-10-28 17:39:19 +0000715 for (unsigned i = 0; i != NumDstElts; ++i)
716 Mask.push_back(llvm::ConstantInt::get(Int32Ty, i));
717
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000718 // modify when what gets shuffled in
719 for (unsigned i = 0; i != NumSrcElts; ++i) {
720 unsigned Idx = getAccessedFieldNo(i, Elts);
Chris Lattnereb99b012009-10-28 17:39:19 +0000721 Mask[Idx] = llvm::ConstantInt::get(Int32Ty, i+NumDstElts);
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000722 }
Owen Anderson4a289322009-07-28 21:22:35 +0000723 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000724 Vec = Builder.CreateShuffleVector(Vec, ExtSrcVal, MaskV, "tmp");
Mike Stumpb3589f42009-07-30 22:28:39 +0000725 } else {
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000726 // We should never shorten the vector
727 assert(0 && "unexpected shorten vector length");
Chris Lattner7e6b51b2007-08-03 16:37:04 +0000728 }
729 } else {
730 // If the Src is a scalar (not a vector) it must be updating one element.
Dan Gohman4f8d1232008-05-22 00:50:06 +0000731 unsigned InIdx = getAccessedFieldNo(0, Elts);
Chris Lattnereb99b012009-10-28 17:39:19 +0000732 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
733 llvm::Value *Elt = llvm::ConstantInt::get(Int32Ty, InIdx);
Chris Lattner017d6aa2007-08-03 16:28:33 +0000734 Vec = Builder.CreateInsertElement(Vec, SrcVal, Elt, "tmp");
Chris Lattner017d6aa2007-08-03 16:28:33 +0000735 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000736
Eli Friedman1e692ac2008-06-13 23:01:12 +0000737 Builder.CreateStore(Vec, Dst.getExtVectorAddr(), Dst.isVolatileQualified());
Chris Lattner017d6aa2007-08-03 16:28:33 +0000738}
739
Fariborz Jahanianb123ea32009-09-16 21:37:16 +0000740// setObjCGCLValueClass - sets class of he lvalue for the purpose of
741// generating write-barries API. It is currently a global, ivar,
742// or neither.
Chris Lattnereb99b012009-10-28 17:39:19 +0000743static void setObjCGCLValueClass(const ASTContext &Ctx, const Expr *E,
744 LValue &LV) {
Fariborz Jahanianb9242592009-09-21 23:03:37 +0000745 if (Ctx.getLangOptions().getGCMode() == LangOptions::NonGC)
Fariborz Jahanianb123ea32009-09-16 21:37:16 +0000746 return;
747
Fariborz Jahaniandbf3cfd2009-09-16 23:11:23 +0000748 if (isa<ObjCIvarRefExpr>(E)) {
749 LV.SetObjCIvar(LV, true);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000750 ObjCIvarRefExpr *Exp = cast<ObjCIvarRefExpr>(const_cast<Expr*>(E));
751 LV.setBaseIvarExp(Exp->getBase());
Fariborz Jahanianfd02ed72009-09-21 18:54:29 +0000752 LV.SetObjCArray(LV, E->getType()->isArrayType());
Fariborz Jahaniandbf3cfd2009-09-16 23:11:23 +0000753 return;
754 }
Chris Lattnereb99b012009-10-28 17:39:19 +0000755
Fariborz Jahanianb123ea32009-09-16 21:37:16 +0000756 if (const DeclRefExpr *Exp = dyn_cast<DeclRefExpr>(E)) {
757 if (const VarDecl *VD = dyn_cast<VarDecl>(Exp->getDecl())) {
758 if ((VD->isBlockVarDecl() && !VD->hasLocalStorage()) ||
759 VD->isFileVarDecl())
760 LV.SetGlobalObjCRef(LV, true);
761 }
Fariborz Jahanianfd02ed72009-09-21 18:54:29 +0000762 LV.SetObjCArray(LV, E->getType()->isArrayType());
Chris Lattnereb99b012009-10-28 17:39:19 +0000763 return;
Fariborz Jahanianb123ea32009-09-16 21:37:16 +0000764 }
Chris Lattnereb99b012009-10-28 17:39:19 +0000765
766 if (const UnaryOperator *Exp = dyn_cast<UnaryOperator>(E)) {
Fariborz Jahanianb123ea32009-09-16 21:37:16 +0000767 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
Chris Lattnereb99b012009-10-28 17:39:19 +0000768 return;
769 }
770
771 if (const ParenExpr *Exp = dyn_cast<ParenExpr>(E)) {
Fariborz Jahanianb123ea32009-09-16 21:37:16 +0000772 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
Fariborz Jahanian75b08f12009-09-30 17:10:29 +0000773 if (LV.isObjCIvar()) {
774 // If cast is to a structure pointer, follow gcc's behavior and make it
775 // a non-ivar write-barrier.
776 QualType ExpTy = E->getType();
777 if (ExpTy->isPointerType())
778 ExpTy = ExpTy->getAs<PointerType>()->getPointeeType();
779 if (ExpTy->isRecordType())
780 LV.SetObjCIvar(LV, false);
Chris Lattnereb99b012009-10-28 17:39:19 +0000781 }
782 return;
Fariborz Jahanian75b08f12009-09-30 17:10:29 +0000783 }
Chris Lattnereb99b012009-10-28 17:39:19 +0000784 if (const ImplicitCastExpr *Exp = dyn_cast<ImplicitCastExpr>(E)) {
Fariborz Jahanianb123ea32009-09-16 21:37:16 +0000785 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
Chris Lattnereb99b012009-10-28 17:39:19 +0000786 return;
787 }
788
789 if (const CStyleCastExpr *Exp = dyn_cast<CStyleCastExpr>(E)) {
Fariborz Jahanianb123ea32009-09-16 21:37:16 +0000790 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
Chris Lattnereb99b012009-10-28 17:39:19 +0000791 return;
792 }
793
794 if (const ArraySubscriptExpr *Exp = dyn_cast<ArraySubscriptExpr>(E)) {
Fariborz Jahanianb123ea32009-09-16 21:37:16 +0000795 setObjCGCLValueClass(Ctx, Exp->getBase(), LV);
Fariborz Jahanianfd02ed72009-09-21 18:54:29 +0000796 if (LV.isObjCIvar() && !LV.isObjCArray())
Fariborz Jahanian1c1afc42009-09-18 00:04:00 +0000797 // Using array syntax to assigning to what an ivar points to is not
798 // same as assigning to the ivar itself. {id *Names;} Names[i] = 0;
799 LV.SetObjCIvar(LV, false);
Fariborz Jahanianfd02ed72009-09-21 18:54:29 +0000800 else if (LV.isGlobalObjCRef() && !LV.isObjCArray())
801 // Using array syntax to assigning to what global points to is not
802 // same as assigning to the global itself. {id *G;} G[i] = 0;
803 LV.SetGlobalObjCRef(LV, false);
Chris Lattnereb99b012009-10-28 17:39:19 +0000804 return;
Fariborz Jahanian1c1afc42009-09-18 00:04:00 +0000805 }
Chris Lattnereb99b012009-10-28 17:39:19 +0000806
807 if (const MemberExpr *Exp = dyn_cast<MemberExpr>(E)) {
Fariborz Jahanianb123ea32009-09-16 21:37:16 +0000808 setObjCGCLValueClass(Ctx, Exp->getBase(), LV);
Fariborz Jahanian1c1afc42009-09-18 00:04:00 +0000809 // We don't know if member is an 'ivar', but this flag is looked at
810 // only in the context of LV.isObjCIvar().
Fariborz Jahanianfd02ed72009-09-21 18:54:29 +0000811 LV.SetObjCArray(LV, E->getType()->isArrayType());
Chris Lattnereb99b012009-10-28 17:39:19 +0000812 return;
Fariborz Jahanianb123ea32009-09-16 21:37:16 +0000813 }
814}
815
Anders Carlssonce53f7d2009-11-07 23:06:58 +0000816static LValue EmitGlobalVarDeclLValue(CodeGenFunction &CGF,
817 const Expr *E, const VarDecl *VD) {
Daniel Dunbard2113f22009-11-08 09:46:46 +0000818 assert((VD->hasExternalStorage() || VD->isFileVarDecl()) &&
Anders Carlssonce53f7d2009-11-07 23:06:58 +0000819 "Var decl must have external storage or be a file var decl!");
820
821 llvm::Value *V = CGF.CGM.GetAddrOfGlobalVar(VD);
822 if (VD->getType()->isReferenceType())
823 V = CGF.Builder.CreateLoad(V, "tmp");
824 LValue LV = LValue::MakeAddr(V, CGF.MakeQualifiers(E->getType()));
825 setObjCGCLValueClass(CGF.getContext(), E, LV);
826 return LV;
827}
828
Reid Spencer5f016e22007-07-11 17:01:13 +0000829LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) {
Anders Carlsson1e74c4f2009-11-07 22:53:10 +0000830 const NamedDecl *ND = E->getDecl();
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000831
Anders Carlsson1e74c4f2009-11-07 22:53:10 +0000832 if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
Anders Carlsson1e74c4f2009-11-07 22:53:10 +0000833
834 // Check if this is a global variable.
Anders Carlssonce53f7d2009-11-07 23:06:58 +0000835 if (VD->hasExternalStorage() || VD->isFileVarDecl())
836 return EmitGlobalVarDeclLValue(*this, E, VD);
Anders Carlsson0bc70492009-11-07 22:46:42 +0000837
838 bool NonGCable = VD->hasLocalStorage() && !VD->hasAttr<BlocksAttr>();
839
840 llvm::Value *V = LocalDeclMap[VD];
841 assert(V && "DeclRefExpr not entered in LocalDeclMap?");
842
843 Qualifiers Quals = MakeQualifiers(E->getType());
844 // local variables do not get their gc attribute set.
845 // local static?
846 if (NonGCable) Quals.removeObjCGCAttr();
847
848 if (VD->hasAttr<BlocksAttr>()) {
849 V = Builder.CreateStructGEP(V, 1, "forwarding");
850 V = Builder.CreateLoad(V, false);
851 V = Builder.CreateStructGEP(V, getByRefValueLLVMField(VD),
852 VD->getNameAsString());
853 }
854 if (VD->getType()->isReferenceType())
855 V = Builder.CreateLoad(V, "tmp");
Anders Carlssonce53f7d2009-11-07 23:06:58 +0000856 LValue LV = LValue::MakeAddr(V, Quals);
Anders Carlsson0bc70492009-11-07 22:46:42 +0000857 LValue::SetObjCNonGC(LV, NonGCable);
Fariborz Jahanianb123ea32009-09-16 21:37:16 +0000858 setObjCGCLValueClass(getContext(), E, LV);
Fariborz Jahanian2682d8b2008-11-20 00:15:42 +0000859 return LV;
Chris Lattnereb99b012009-10-28 17:39:19 +0000860 }
861
Anders Carlsson1e74c4f2009-11-07 22:53:10 +0000862 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Anders Carlsson555b4bb2009-09-10 23:43:36 +0000863 llvm::Value* V = CGM.GetAddrOfFunction(FD);
Eli Friedman3fbc4732009-06-01 10:04:20 +0000864 if (!FD->hasPrototype()) {
865 if (const FunctionProtoType *Proto =
John McCall183700f2009-09-21 23:43:11 +0000866 FD->getType()->getAs<FunctionProtoType>()) {
Eli Friedman3fbc4732009-06-01 10:04:20 +0000867 // Ugly case: for a K&R-style definition, the type of the definition
868 // isn't the same as the type of a use. Correct for this with a
869 // bitcast.
870 QualType NoProtoType =
871 getContext().getFunctionNoProtoType(Proto->getResultType());
872 NoProtoType = getContext().getPointerType(NoProtoType);
873 V = Builder.CreateBitCast(V, ConvertType(NoProtoType), "tmp");
874 }
875 }
John McCall0953e762009-09-24 19:53:00 +0000876 return LValue::MakeAddr(V, MakeQualifiers(E->getType()));
Chris Lattnereb99b012009-10-28 17:39:19 +0000877 }
878
Chris Lattnereb99b012009-10-28 17:39:19 +0000879 if (E->getQualifier()) {
Douglas Gregora2813ce2009-10-23 18:54:35 +0000880 // FIXME: the qualifier check does not seem sufficient here
Anders Carlsson1e74c4f2009-11-07 22:53:10 +0000881 return EmitPointerToDataMemberLValue(cast<FieldDecl>(ND));
Chris Lattner41110242008-06-17 18:05:57 +0000882 }
Chris Lattnereb99b012009-10-28 17:39:19 +0000883
Anders Carlsson1e74c4f2009-11-07 22:53:10 +0000884 assert(false && "Unhandled DeclRefExpr");
885
886 // an invalid LValue, but the assert will
887 // ensure that this point is never reached.
Chris Lattnerb1776cb2007-09-16 19:23:47 +0000888 return LValue();
Reid Spencer5f016e22007-07-11 17:01:13 +0000889}
890
Mike Stumpa99038c2009-02-28 09:07:16 +0000891LValue CodeGenFunction::EmitBlockDeclRefLValue(const BlockDeclRefExpr *E) {
John McCall0953e762009-09-24 19:53:00 +0000892 return LValue::MakeAddr(GetAddrOfBlockDecl(E), MakeQualifiers(E->getType()));
Mike Stumpa99038c2009-02-28 09:07:16 +0000893}
894
Reid Spencer5f016e22007-07-11 17:01:13 +0000895LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) {
896 // __extension__ doesn't affect lvalue-ness.
897 if (E->getOpcode() == UnaryOperator::Extension)
898 return EmitLValue(E->getSubExpr());
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000899
Chris Lattner96196622008-07-26 22:37:01 +0000900 QualType ExprTy = getContext().getCanonicalType(E->getSubExpr()->getType());
Chris Lattner7da36f62007-10-30 22:53:42 +0000901 switch (E->getOpcode()) {
902 default: assert(0 && "Unknown unary operator lvalue!");
Chris Lattnereb99b012009-10-28 17:39:19 +0000903 case UnaryOperator::Deref: {
904 QualType T = E->getSubExpr()->getType()->getPointeeType();
905 assert(!T.isNull() && "CodeGenFunction::EmitUnaryOpLValue: Illegal type");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000906
Chris Lattnereb99b012009-10-28 17:39:19 +0000907 Qualifiers Quals = MakeQualifiers(T);
908 Quals.setAddressSpace(ExprTy.getAddressSpace());
John McCall0953e762009-09-24 19:53:00 +0000909
Chris Lattnereb99b012009-10-28 17:39:19 +0000910 LValue LV = LValue::MakeAddr(EmitScalarExpr(E->getSubExpr()), Quals);
911 // We should not generate __weak write barrier on indirect reference
912 // of a pointer to object; as in void foo (__weak id *param); *param = 0;
913 // But, we continue to generate __strong write barrier on indirect write
914 // into a pointer to object.
915 if (getContext().getLangOptions().ObjC1 &&
916 getContext().getLangOptions().getGCMode() != LangOptions::NonGC &&
917 LV.isObjCWeak())
918 LValue::SetObjCNonGC(LV, !E->isOBJCGCCandidate(getContext()));
919 return LV;
920 }
Chris Lattner7da36f62007-10-30 22:53:42 +0000921 case UnaryOperator::Real:
922 case UnaryOperator::Imag:
923 LValue LV = EmitLValue(E->getSubExpr());
Chris Lattner36b6a0a2008-03-19 05:19:41 +0000924 unsigned Idx = E->getOpcode() == UnaryOperator::Imag;
925 return LValue::MakeAddr(Builder.CreateStructGEP(LV.getAddress(),
Chris Lattnerb77792e2008-07-26 22:17:49 +0000926 Idx, "idx"),
John McCall0953e762009-09-24 19:53:00 +0000927 MakeQualifiers(ExprTy));
Chris Lattner7da36f62007-10-30 22:53:42 +0000928 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000929}
930
931LValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) {
John McCall0953e762009-09-24 19:53:00 +0000932 return LValue::MakeAddr(CGM.GetAddrOfConstantStringFromLiteral(E),
933 Qualifiers());
Reid Spencer5f016e22007-07-11 17:01:13 +0000934}
935
Chris Lattnereaf2bb82009-02-24 22:18:39 +0000936LValue CodeGenFunction::EmitObjCEncodeExprLValue(const ObjCEncodeExpr *E) {
John McCall0953e762009-09-24 19:53:00 +0000937 return LValue::MakeAddr(CGM.GetAddrOfConstantStringFromObjCEncode(E),
938 Qualifiers());
Chris Lattnereaf2bb82009-02-24 22:18:39 +0000939}
940
941
Daniel Dunbar662b71e2008-10-17 21:58:32 +0000942LValue CodeGenFunction::EmitPredefinedFunctionName(unsigned Type) {
Anders Carlsson22742662007-07-21 05:21:51 +0000943 std::string GlobalVarName;
Daniel Dunbar662b71e2008-10-17 21:58:32 +0000944
945 switch (Type) {
Chris Lattnereb99b012009-10-28 17:39:19 +0000946 default: assert(0 && "Invalid type");
Chris Lattnereaf2bb82009-02-24 22:18:39 +0000947 case PredefinedExpr::Func:
948 GlobalVarName = "__func__.";
949 break;
950 case PredefinedExpr::Function:
951 GlobalVarName = "__FUNCTION__.";
952 break;
953 case PredefinedExpr::PrettyFunction:
Chris Lattnereaf2bb82009-02-24 22:18:39 +0000954 GlobalVarName = "__PRETTY_FUNCTION__.";
955 break;
Anders Carlsson22742662007-07-21 05:21:51 +0000956 }
Daniel Dunbar662b71e2008-10-17 21:58:32 +0000957
Daniel Dunbar0a23d762009-09-12 23:06:21 +0000958 llvm::StringRef FnName = CurFn->getName();
959 if (FnName.startswith("\01"))
960 FnName = FnName.substr(1);
961 GlobalVarName += FnName;
962
Anders Carlsson3a082d82009-09-08 18:24:21 +0000963 std::string FunctionName =
Mike Stump1eb44332009-09-09 15:08:12 +0000964 PredefinedExpr::ComputeName(getContext(), (PredefinedExpr::IdentType)Type,
Anders Carlsson3a082d82009-09-08 18:24:21 +0000965 CurCodeDecl);
Daniel Dunbar662b71e2008-10-17 21:58:32 +0000966
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000967 llvm::Constant *C =
Daniel Dunbar662b71e2008-10-17 21:58:32 +0000968 CGM.GetAddrOfConstantCString(FunctionName, GlobalVarName.c_str());
John McCall0953e762009-09-24 19:53:00 +0000969 return LValue::MakeAddr(C, Qualifiers());
Daniel Dunbar662b71e2008-10-17 21:58:32 +0000970}
971
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000972LValue CodeGenFunction::EmitPredefinedLValue(const PredefinedExpr *E) {
Daniel Dunbar662b71e2008-10-17 21:58:32 +0000973 switch (E->getIdentType()) {
974 default:
975 return EmitUnsupportedLValue(E, "predefined expression");
976 case PredefinedExpr::Func:
977 case PredefinedExpr::Function:
978 case PredefinedExpr::PrettyFunction:
979 return EmitPredefinedFunctionName(E->getIdentType());
980 }
Anders Carlsson22742662007-07-21 05:21:51 +0000981}
982
Reid Spencer5f016e22007-07-11 17:01:13 +0000983LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) {
Ted Kremenek23245122007-08-20 16:18:38 +0000984 // The index must always be an integer, which is not an aggregate. Emit it.
Chris Lattner7f02f722007-08-24 05:35:26 +0000985 llvm::Value *Idx = EmitScalarExpr(E->getIdx());
Eli Friedman61d004a2009-06-06 19:09:26 +0000986 QualType IdxTy = E->getIdx()->getType();
987 bool IdxSigned = IdxTy->isSignedIntegerType();
988
Reid Spencer5f016e22007-07-11 17:01:13 +0000989 // If the base is a vector type, then we are forming a vector element lvalue
990 // with this subscript.
Eli Friedman1e692ac2008-06-13 23:01:12 +0000991 if (E->getBase()->getType()->isVectorType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000992 // Emit the vector as an lvalue to get its address.
Eli Friedman1e692ac2008-06-13 23:01:12 +0000993 LValue LHS = EmitLValue(E->getBase());
Ted Kremenek23245122007-08-20 16:18:38 +0000994 assert(LHS.isSimple() && "Can only subscript lvalue vectors here!");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000995 Idx = Builder.CreateIntCast(Idx,
Owen Anderson0032b272009-08-13 21:57:51 +0000996 llvm::Type::getInt32Ty(VMContext), IdxSigned, "vidx");
Eli Friedman1e692ac2008-06-13 23:01:12 +0000997 return LValue::MakeVectorElt(LHS.getAddress(), Idx,
John McCall0953e762009-09-24 19:53:00 +0000998 E->getBase()->getType().getCVRQualifiers());
Reid Spencer5f016e22007-07-11 17:01:13 +0000999 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001000
Ted Kremenek23245122007-08-20 16:18:38 +00001001 // The base must be a pointer, which is not an aggregate. Emit it.
Chris Lattner7f02f722007-08-24 05:35:26 +00001002 llvm::Value *Base = EmitScalarExpr(E->getBase());
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001003
Ted Kremenek23245122007-08-20 16:18:38 +00001004 // Extend or truncate the index type to 32 or 64-bits.
Reid Spencer5f016e22007-07-11 17:01:13 +00001005 unsigned IdxBitwidth = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
Sanjiv Gupta7cabee52009-04-24 02:40:57 +00001006 if (IdxBitwidth != LLVMPointerWidth)
Owen Anderson0032b272009-08-13 21:57:51 +00001007 Idx = Builder.CreateIntCast(Idx,
1008 llvm::IntegerType::get(VMContext, LLVMPointerWidth),
Reid Spencer5f016e22007-07-11 17:01:13 +00001009 IdxSigned, "idxprom");
1010
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001011 // We know that the pointer points to a type of the correct size, unless the
1012 // size is a VLA or Objective-C interface.
Daniel Dunbar2a866252009-04-25 05:08:32 +00001013 llvm::Value *Address = 0;
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001014 if (const VariableArrayType *VAT =
Anders Carlsson8b33c082008-12-21 00:11:23 +00001015 getContext().getAsVariableArrayType(E->getType())) {
Chris Lattner881eb9c2009-08-14 23:43:22 +00001016 llvm::Value *VLASize = GetVLASize(VAT);
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001017
Anders Carlsson8b33c082008-12-21 00:11:23 +00001018 Idx = Builder.CreateMul(Idx, VLASize);
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001019
Anders Carlsson6183a992008-12-21 03:44:36 +00001020 QualType BaseType = getContext().getBaseElementType(VAT);
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001021
Anders Carlsson8b33c082008-12-21 00:11:23 +00001022 uint64_t BaseTypeSize = getContext().getTypeSize(BaseType) / 8;
1023 Idx = Builder.CreateUDiv(Idx,
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001024 llvm::ConstantInt::get(Idx->getType(),
Anders Carlsson8b33c082008-12-21 00:11:23 +00001025 BaseTypeSize));
Dan Gohman664f8932009-08-12 00:33:55 +00001026 Address = Builder.CreateInBoundsGEP(Base, Idx, "arrayidx");
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001027 } else if (const ObjCInterfaceType *OIT =
Daniel Dunbar2a866252009-04-25 05:08:32 +00001028 dyn_cast<ObjCInterfaceType>(E->getType())) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001029 llvm::Value *InterfaceSize =
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001030 llvm::ConstantInt::get(Idx->getType(),
Daniel Dunbar2a866252009-04-25 05:08:32 +00001031 getContext().getTypeSize(OIT) / 8);
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001032
Daniel Dunbar2a866252009-04-25 05:08:32 +00001033 Idx = Builder.CreateMul(Idx, InterfaceSize);
1034
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00001035 const llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(VMContext);
Dan Gohman664f8932009-08-12 00:33:55 +00001036 Address = Builder.CreateGEP(Builder.CreateBitCast(Base, i8PTy),
Daniel Dunbar2a866252009-04-25 05:08:32 +00001037 Idx, "arrayidx");
1038 Address = Builder.CreateBitCast(Address, Base->getType());
1039 } else {
Dan Gohman664f8932009-08-12 00:33:55 +00001040 Address = Builder.CreateInBoundsGEP(Base, Idx, "arrayidx");
Anders Carlsson8b33c082008-12-21 00:11:23 +00001041 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001042
Steve Naroff14108da2009-07-10 23:34:53 +00001043 QualType T = E->getBase()->getType()->getPointeeType();
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001044 assert(!T.isNull() &&
Steve Naroff14108da2009-07-10 23:34:53 +00001045 "CodeGenFunction::EmitArraySubscriptExpr(): Illegal base type");
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001046
John McCall0953e762009-09-24 19:53:00 +00001047 Qualifiers Quals = MakeQualifiers(T);
1048 Quals.setAddressSpace(E->getBase()->getType().getAddressSpace());
1049
1050 LValue LV = LValue::MakeAddr(Address, Quals);
Fariborz Jahanian643887a2009-02-21 23:37:19 +00001051 if (getContext().getLangOptions().ObjC1 &&
Fariborz Jahanianb123ea32009-09-16 21:37:16 +00001052 getContext().getLangOptions().getGCMode() != LangOptions::NonGC) {
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001053 LValue::SetObjCNonGC(LV, !E->isOBJCGCCandidate(getContext()));
Fariborz Jahanianb123ea32009-09-16 21:37:16 +00001054 setObjCGCLValueClass(getContext(), E, LV);
1055 }
Fariborz Jahanian643887a2009-02-21 23:37:19 +00001056 return LV;
Reid Spencer5f016e22007-07-11 17:01:13 +00001057}
1058
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001059static
Owen Andersona1cf15f2009-07-14 23:10:40 +00001060llvm::Constant *GenerateConstantVector(llvm::LLVMContext &VMContext,
1061 llvm::SmallVector<unsigned, 4> &Elts) {
Nate Begeman3b8d1162008-05-13 21:03:02 +00001062 llvm::SmallVector<llvm::Constant *, 4> CElts;
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001063
Nate Begeman3b8d1162008-05-13 21:03:02 +00001064 for (unsigned i = 0, e = Elts.size(); i != e; ++i)
Owen Anderson0032b272009-08-13 21:57:51 +00001065 CElts.push_back(llvm::ConstantInt::get(
1066 llvm::Type::getInt32Ty(VMContext), Elts[i]));
Nate Begeman3b8d1162008-05-13 21:03:02 +00001067
Owen Anderson4a289322009-07-28 21:22:35 +00001068 return llvm::ConstantVector::get(&CElts[0], CElts.size());
Nate Begeman3b8d1162008-05-13 21:03:02 +00001069}
1070
Chris Lattner349aaec2007-08-02 23:37:31 +00001071LValue CodeGenFunction::
Nate Begeman213541a2008-04-18 23:10:10 +00001072EmitExtVectorElementExpr(const ExtVectorElementExpr *E) {
Chris Lattner349aaec2007-08-02 23:37:31 +00001073 // Emit the base vector as an l-value.
Chris Lattner73525de2009-02-16 21:11:58 +00001074 LValue Base;
1075
1076 // ExtVectorElementExpr's base can either be a vector or pointer to vector.
Chris Lattner2140e902009-02-16 22:14:05 +00001077 if (!E->isArrow()) {
Chris Lattner73525de2009-02-16 21:11:58 +00001078 assert(E->getBase()->getType()->isVectorType());
1079 Base = EmitLValue(E->getBase());
Chris Lattner2140e902009-02-16 22:14:05 +00001080 } else {
Ted Kremenek6217b802009-07-29 21:53:49 +00001081 const PointerType *PT = E->getBase()->getType()->getAs<PointerType>();
Chris Lattner2140e902009-02-16 22:14:05 +00001082 llvm::Value *Ptr = EmitScalarExpr(E->getBase());
John McCall0953e762009-09-24 19:53:00 +00001083 Qualifiers Quals = MakeQualifiers(PT->getPointeeType());
1084 Quals.removeObjCGCAttr();
1085 Base = LValue::MakeAddr(Ptr, Quals);
Chris Lattner73525de2009-02-16 21:11:58 +00001086 }
Chris Lattner349aaec2007-08-02 23:37:31 +00001087
Nate Begeman3b8d1162008-05-13 21:03:02 +00001088 // Encode the element access list into a vector of unsigned indices.
1089 llvm::SmallVector<unsigned, 4> Indices;
1090 E->getEncodedElementAccess(Indices);
1091
1092 if (Base.isSimple()) {
Owen Andersona1cf15f2009-07-14 23:10:40 +00001093 llvm::Constant *CV = GenerateConstantVector(VMContext, Indices);
Eli Friedman1e692ac2008-06-13 23:01:12 +00001094 return LValue::MakeExtVectorElt(Base.getAddress(), CV,
John McCall0953e762009-09-24 19:53:00 +00001095 Base.getVRQualifiers());
Nate Begeman3b8d1162008-05-13 21:03:02 +00001096 }
1097 assert(Base.isExtVectorElt() && "Can only subscript lvalue vec elts here!");
1098
1099 llvm::Constant *BaseElts = Base.getExtVectorElts();
1100 llvm::SmallVector<llvm::Constant *, 4> CElts;
1101
Chris Lattner67665862009-10-28 05:12:07 +00001102 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
Nate Begeman3b8d1162008-05-13 21:03:02 +00001103 for (unsigned i = 0, e = Indices.size(); i != e; ++i) {
1104 if (isa<llvm::ConstantAggregateZero>(BaseElts))
Chris Lattner67665862009-10-28 05:12:07 +00001105 CElts.push_back(llvm::ConstantInt::get(Int32Ty, 0));
Nate Begeman3b8d1162008-05-13 21:03:02 +00001106 else
Chris Lattner67665862009-10-28 05:12:07 +00001107 CElts.push_back(cast<llvm::Constant>(BaseElts->getOperand(Indices[i])));
Nate Begeman3b8d1162008-05-13 21:03:02 +00001108 }
Owen Anderson4a289322009-07-28 21:22:35 +00001109 llvm::Constant *CV = llvm::ConstantVector::get(&CElts[0], CElts.size());
Eli Friedman1e692ac2008-06-13 23:01:12 +00001110 return LValue::MakeExtVectorElt(Base.getExtVectorAddr(), CV,
John McCall0953e762009-09-24 19:53:00 +00001111 Base.getVRQualifiers());
Chris Lattner349aaec2007-08-02 23:37:31 +00001112}
1113
Devang Patelb9b00ad2007-10-23 20:28:39 +00001114LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) {
Devang Patelfe2419a2007-12-11 21:33:16 +00001115 bool isUnion = false;
Fariborz Jahanian4f676ed2009-02-21 00:30:43 +00001116 bool isNonGC = false;
Devang Patel126a8562007-10-24 22:26:28 +00001117 Expr *BaseExpr = E->getBase();
Devang Patel126a8562007-10-24 22:26:28 +00001118 llvm::Value *BaseValue = NULL;
John McCall0953e762009-09-24 19:53:00 +00001119 Qualifiers BaseQuals;
Eli Friedman1e692ac2008-06-13 23:01:12 +00001120
Chris Lattner12f65f62007-12-02 18:52:07 +00001121 // 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 +00001122 if (E->isArrow()) {
Devang Patel0a961182007-10-26 18:15:21 +00001123 BaseValue = EmitScalarExpr(BaseExpr);
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001124 const PointerType *PTy =
Ted Kremenek6217b802009-07-29 21:53:49 +00001125 BaseExpr->getType()->getAs<PointerType>();
Devang Patelfe2419a2007-12-11 21:33:16 +00001126 if (PTy->getPointeeType()->isUnionType())
1127 isUnion = true;
John McCall0953e762009-09-24 19:53:00 +00001128 BaseQuals = PTy->getPointeeType().getQualifiers();
Fariborz Jahaniand2e1eb02009-09-01 17:02:21 +00001129 } else if (isa<ObjCPropertyRefExpr>(BaseExpr->IgnoreParens()) ||
1130 isa<ObjCImplicitSetterGetterRefExpr>(
1131 BaseExpr->IgnoreParens())) {
Fariborz Jahanian35c33292009-01-12 23:27:26 +00001132 RValue RV = EmitObjCPropertyGet(BaseExpr);
1133 BaseValue = RV.getAggregateAddr();
1134 if (BaseExpr->getType()->isUnionType())
1135 isUnion = true;
John McCall0953e762009-09-24 19:53:00 +00001136 BaseQuals = BaseExpr->getType().getQualifiers();
Chris Lattner1bd885e2009-02-16 22:25:49 +00001137 } else {
Chris Lattner12f65f62007-12-02 18:52:07 +00001138 LValue BaseLV = EmitLValue(BaseExpr);
Fariborz Jahanian4f676ed2009-02-21 00:30:43 +00001139 if (BaseLV.isNonGC())
1140 isNonGC = true;
Chris Lattner12f65f62007-12-02 18:52:07 +00001141 // FIXME: this isn't right for bitfields.
1142 BaseValue = BaseLV.getAddress();
Fariborz Jahaniana91d6a62009-07-29 00:44:13 +00001143 QualType BaseTy = BaseExpr->getType();
1144 if (BaseTy->isUnionType())
Devang Patelfe2419a2007-12-11 21:33:16 +00001145 isUnion = true;
John McCall0953e762009-09-24 19:53:00 +00001146 BaseQuals = BaseTy.getQualifiers();
Chris Lattner12f65f62007-12-02 18:52:07 +00001147 }
Devang Patelb9b00ad2007-10-23 20:28:39 +00001148
Anders Carlssonce53f7d2009-11-07 23:06:58 +00001149 NamedDecl *ND = E->getMemberDecl();
1150 if (FieldDecl *Field = dyn_cast<FieldDecl>(ND)) {
1151 LValue LV = EmitLValueForField(BaseValue, Field, isUnion,
1152 BaseQuals.getCVRQualifiers());
1153 LValue::SetObjCNonGC(LV, isNonGC);
1154 setObjCGCLValueClass(getContext(), E, LV);
1155 return LV;
1156 }
1157
Anders Carlsson589f9e32009-11-07 23:16:50 +00001158 if (VarDecl *VD = dyn_cast<VarDecl>(ND))
1159 return EmitGlobalVarDeclLValue(*this, E, VD);
1160
Anders Carlssonce53f7d2009-11-07 23:06:58 +00001161 assert(false && "Unhandled member declaration!");
1162 return LValue();
Eli Friedman472778e2008-02-09 08:50:58 +00001163}
Devang Patelb9b00ad2007-10-23 20:28:39 +00001164
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +00001165LValue CodeGenFunction::EmitLValueForBitfield(llvm::Value* BaseValue,
1166 FieldDecl* Field,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001167 unsigned CVRQualifiers) {
Anders Carlsson8330cee2009-07-23 17:01:21 +00001168 CodeGenTypes::BitFieldInfo Info = CGM.getTypes().getBitFieldInfo(Field);
1169
Mike Stumpf5408fe2009-05-16 07:57:57 +00001170 // FIXME: CodeGenTypes should expose a method to get the appropriate type for
1171 // FieldTy (the appropriate type is ABI-dependent).
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001172 const llvm::Type *FieldTy =
Daniel Dunbarbb767732009-02-17 18:31:04 +00001173 CGM.getTypes().ConvertTypeForMem(Field->getType());
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +00001174 const llvm::PointerType *BaseTy =
1175 cast<llvm::PointerType>(BaseValue->getType());
1176 unsigned AS = BaseTy->getAddressSpace();
1177 BaseValue = Builder.CreateBitCast(BaseValue,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001178 llvm::PointerType::get(FieldTy, AS),
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +00001179 "tmp");
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001180
1181 llvm::Value *Idx =
Owen Anderson0032b272009-08-13 21:57:51 +00001182 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), Info.FieldNo);
Anders Carlsson8330cee2009-07-23 17:01:21 +00001183 llvm::Value *V = Builder.CreateGEP(BaseValue, Idx, "tmp");
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001184
Anders Carlsson8330cee2009-07-23 17:01:21 +00001185 return LValue::MakeBitfield(V, Info.Start, Info.Size,
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +00001186 Field->getType()->isSignedIntegerType(),
1187 Field->getType().getCVRQualifiers()|CVRQualifiers);
1188}
1189
Eli Friedman472778e2008-02-09 08:50:58 +00001190LValue CodeGenFunction::EmitLValueForField(llvm::Value* BaseValue,
1191 FieldDecl* Field,
Eli Friedman1e692ac2008-06-13 23:01:12 +00001192 bool isUnion,
Mike Stump1eb44332009-09-09 15:08:12 +00001193 unsigned CVRQualifiers) {
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +00001194 if (Field->isBitField())
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001195 return EmitLValueForBitfield(BaseValue, Field, CVRQualifiers);
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001196
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001197 unsigned idx = CGM.getTypes().getLLVMFieldNo(Field);
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +00001198 llvm::Value *V = Builder.CreateStructGEP(BaseValue, idx, "tmp");
Eli Friedman1e86b342008-05-29 11:33:25 +00001199
Devang Patelabad06c2007-10-26 19:42:18 +00001200 // Match union field type.
Lauro Ramos Venanciod957aa02008-02-07 19:29:53 +00001201 if (isUnion) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001202 const llvm::Type *FieldTy =
Eli Friedman1e692ac2008-06-13 23:01:12 +00001203 CGM.getTypes().ConvertTypeForMem(Field->getType());
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001204 const llvm::PointerType * BaseTy =
Devang Patele9b8c0a2007-10-30 20:59:40 +00001205 cast<llvm::PointerType>(BaseValue->getType());
Eli Friedman788d5712008-05-21 13:24:44 +00001206 unsigned AS = BaseTy->getAddressSpace();
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001207 V = Builder.CreateBitCast(V,
1208 llvm::PointerType::get(FieldTy, AS),
Eli Friedman788d5712008-05-21 13:24:44 +00001209 "tmp");
Devang Patelabad06c2007-10-26 19:42:18 +00001210 }
Eli Friedman2be58612009-05-30 21:09:44 +00001211 if (Field->getType()->isReferenceType())
1212 V = Builder.CreateLoad(V, "tmp");
John McCall0953e762009-09-24 19:53:00 +00001213
1214 Qualifiers Quals = MakeQualifiers(Field->getType());
1215 Quals.addCVRQualifiers(CVRQualifiers);
Fariborz Jahanianfd02ed72009-09-21 18:54:29 +00001216 // __weak attribute on a field is ignored.
John McCall0953e762009-09-24 19:53:00 +00001217 if (Quals.getObjCGCAttr() == Qualifiers::Weak)
1218 Quals.removeObjCGCAttr();
Fariborz Jahanianfd02ed72009-09-21 18:54:29 +00001219
John McCall0953e762009-09-24 19:53:00 +00001220 return LValue::MakeAddr(V, Quals);
Devang Patelb9b00ad2007-10-23 20:28:39 +00001221}
1222
Chris Lattner75dfeda2009-03-18 18:28:57 +00001223LValue CodeGenFunction::EmitCompoundLiteralLValue(const CompoundLiteralExpr* E){
Eli Friedman06e863f2008-05-13 23:18:27 +00001224 const llvm::Type *LTy = ConvertType(E->getType());
1225 llvm::Value *DeclPtr = CreateTempAlloca(LTy, ".compoundliteral");
1226
1227 const Expr* InitExpr = E->getInitializer();
John McCall0953e762009-09-24 19:53:00 +00001228 LValue Result = LValue::MakeAddr(DeclPtr, MakeQualifiers(E->getType()));
Eli Friedman06e863f2008-05-13 23:18:27 +00001229
Chris Lattnereb99b012009-10-28 17:39:19 +00001230 if (E->getType()->isComplexType())
Eli Friedman06e863f2008-05-13 23:18:27 +00001231 EmitComplexExprIntoAddr(InitExpr, DeclPtr, false);
Chris Lattnereb99b012009-10-28 17:39:19 +00001232 else if (hasAggregateLLVMType(E->getType()))
Eli Friedman06e863f2008-05-13 23:18:27 +00001233 EmitAnyExpr(InitExpr, DeclPtr, false);
Chris Lattnereb99b012009-10-28 17:39:19 +00001234 else
Eli Friedman06e863f2008-05-13 23:18:27 +00001235 EmitStoreThroughLValue(EmitAnyExpr(InitExpr), Result, E->getType());
Eli Friedman06e863f2008-05-13 23:18:27 +00001236
1237 return Result;
1238}
1239
Anders Carlsson6fcec8b2009-09-15 16:35:24 +00001240LValue
1241CodeGenFunction::EmitConditionalOperatorLValue(const ConditionalOperator* E) {
1242 if (E->isLvalue(getContext()) == Expr::LV_Valid) {
1243 llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true");
1244 llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false");
1245 llvm::BasicBlock *ContBlock = createBasicBlock("cond.end");
1246
1247 llvm::Value *Cond = EvaluateExprAsBool(E->getCond());
1248 Builder.CreateCondBr(Cond, LHSBlock, RHSBlock);
1249
1250 EmitBlock(LHSBlock);
Daniel Dunbar90345582009-03-24 02:38:23 +00001251
Anders Carlsson6fcec8b2009-09-15 16:35:24 +00001252 LValue LHS = EmitLValue(E->getLHS());
1253 if (!LHS.isSimple())
1254 return EmitUnsupportedLValue(E, "conditional operator");
1255
Chris Lattnereb99b012009-10-28 17:39:19 +00001256 llvm::Value *Temp = CreateTempAlloca(LHS.getAddress()->getType(),"condtmp");
Anders Carlsson6fcec8b2009-09-15 16:35:24 +00001257 Builder.CreateStore(LHS.getAddress(), Temp);
1258 EmitBranch(ContBlock);
1259
1260 EmitBlock(RHSBlock);
1261 LValue RHS = EmitLValue(E->getRHS());
1262 if (!RHS.isSimple())
1263 return EmitUnsupportedLValue(E, "conditional operator");
1264
1265 Builder.CreateStore(RHS.getAddress(), Temp);
1266 EmitBranch(ContBlock);
1267
1268 EmitBlock(ContBlock);
1269
1270 Temp = Builder.CreateLoad(Temp, "lv");
John McCall0953e762009-09-24 19:53:00 +00001271 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
Anders Carlsson6fcec8b2009-09-15 16:35:24 +00001272 }
1273
Daniel Dunbar90345582009-03-24 02:38:23 +00001274 // ?: here should be an aggregate.
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001275 assert((hasAggregateLLVMType(E->getType()) &&
Daniel Dunbar90345582009-03-24 02:38:23 +00001276 !E->getType()->isAnyComplexType()) &&
1277 "Unexpected conditional operator!");
1278
1279 llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType()));
1280 EmitAggExpr(E, Temp, false);
1281
John McCall0953e762009-09-24 19:53:00 +00001282 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
Daniel Dunbar90345582009-03-24 02:38:23 +00001283}
1284
Chris Lattner75dfeda2009-03-18 18:28:57 +00001285/// EmitCastLValue - Casts are never lvalues. If a cast is needed by the code
1286/// generator in an lvalue context, then it must mean that we need the address
1287/// of an aggregate in order to access one of its fields. This can happen for
1288/// all the reasons that casts are permitted with aggregate result, including
1289/// noop aggregate casts, and cast from scalar to union.
1290LValue CodeGenFunction::EmitCastLValue(const CastExpr *E) {
Anders Carlsson0ee33cf2009-09-12 16:16:49 +00001291 switch (E->getCastKind()) {
1292 default:
1293 // If this is an lvalue cast, treat it as a no-op.
1294 // FIXME: We shouldn't need to check for this explicitly!
1295 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
1296 if (ICE->isLvalueCast())
1297 return EmitLValue(E->getSubExpr());
1298
1299 assert(0 && "Unhandled cast!");
1300
1301 case CastExpr::CK_NoOp:
1302 case CastExpr::CK_ConstructorConversion:
1303 case CastExpr::CK_UserDefinedConversion:
Chris Lattner75dfeda2009-03-18 18:28:57 +00001304 return EmitLValue(E->getSubExpr());
Anders Carlsson0ee33cf2009-09-12 16:16:49 +00001305
1306 case CastExpr::CK_DerivedToBase: {
1307 const RecordType *DerivedClassTy =
1308 E->getSubExpr()->getType()->getAs<RecordType>();
1309 CXXRecordDecl *DerivedClassDecl =
1310 cast<CXXRecordDecl>(DerivedClassTy->getDecl());
Chris Lattner75dfeda2009-03-18 18:28:57 +00001311
Anders Carlsson0ee33cf2009-09-12 16:16:49 +00001312 const RecordType *BaseClassTy = E->getType()->getAs<RecordType>();
1313 CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseClassTy->getDecl());
1314
1315 LValue LV = EmitLValue(E->getSubExpr());
1316
1317 // Perform the derived-to-base conversion
1318 llvm::Value *Base =
1319 GetAddressCXXOfBaseClass(LV.getAddress(), DerivedClassDecl,
1320 BaseClassDecl, /*NullCheckValue=*/false);
1321
John McCall0953e762009-09-24 19:53:00 +00001322 return LValue::MakeAddr(Base, MakeQualifiers(E->getType()));
Anders Carlsson0ee33cf2009-09-12 16:16:49 +00001323 }
Eli Friedmana77a07e2009-08-27 21:19:33 +00001324
Anders Carlsson0ee33cf2009-09-12 16:16:49 +00001325 case CastExpr::CK_ToUnion: {
1326 llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType()));
1327 EmitAnyExpr(E->getSubExpr(), Temp, false);
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001328
John McCall0953e762009-09-24 19:53:00 +00001329 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
Anders Carlsson0ee33cf2009-09-12 16:16:49 +00001330 }
1331 }
Chris Lattner75dfeda2009-03-18 18:28:57 +00001332}
1333
Fariborz Jahanian48620ba2009-10-20 23:29:04 +00001334LValue CodeGenFunction::EmitNullInitializationLValue(
1335 const CXXZeroInitValueExpr *E) {
1336 QualType Ty = E->getType();
1337 const llvm::Type *LTy = ConvertTypeForMem(Ty);
1338 llvm::AllocaInst *Alloc = CreateTempAlloca(LTy);
1339 unsigned Align = getContext().getTypeAlign(Ty)/8;
1340 Alloc->setAlignment(Align);
1341 LValue lvalue = LValue::MakeAddr(Alloc, Qualifiers());
1342 EmitMemSetToZero(lvalue.getAddress(), Ty);
1343 return lvalue;
1344}
1345
Reid Spencer5f016e22007-07-11 17:01:13 +00001346//===--------------------------------------------------------------------===//
1347// Expression Emission
1348//===--------------------------------------------------------------------===//
1349
Chris Lattner7016a702007-08-20 22:37:10 +00001350
Reid Spencer5f016e22007-07-11 17:01:13 +00001351RValue CodeGenFunction::EmitCallExpr(const CallExpr *E) {
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +00001352 // Builtins never have block type.
Daniel Dunbarce1d38b2009-01-09 16:50:52 +00001353 if (E->getCallee()->getType()->isBlockPointerType())
Anders Carlssonacfde802009-02-12 00:39:25 +00001354 return EmitBlockCallExpr(E);
Daniel Dunbarce1d38b2009-01-09 16:50:52 +00001355
Anders Carlsson774e7c62009-04-03 22:50:24 +00001356 if (const CXXMemberCallExpr *CE = dyn_cast<CXXMemberCallExpr>(E))
1357 return EmitCXXMemberCallExpr(CE);
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001358
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +00001359 const Decl *TargetDecl = 0;
Daniel Dunbardd7b8972009-02-20 19:34:33 +00001360 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E->getCallee())) {
1361 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CE->getSubExpr())) {
1362 TargetDecl = DRE->getDecl();
1363 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(TargetDecl))
Douglas Gregor7814e6d2009-09-12 00:22:50 +00001364 if (unsigned builtinID = FD->getBuiltinID())
Daniel Dunbardd7b8972009-02-20 19:34:33 +00001365 return EmitBuiltinExpr(FD, builtinID, E);
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +00001366 }
1367 }
1368
Chris Lattner5db7ae52009-06-13 00:26:38 +00001369 if (const CXXOperatorCallExpr *CE = dyn_cast<CXXOperatorCallExpr>(E))
Anders Carlsson0f294632009-05-27 04:18:27 +00001370 if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(TargetDecl))
1371 return EmitCXXOperatorMemberCallExpr(CE, MD);
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001372
Douglas Gregora71d8192009-09-04 17:36:40 +00001373 if (isa<CXXPseudoDestructorExpr>(E->getCallee())) {
1374 // C++ [expr.pseudo]p1:
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001375 // The result shall only be used as the operand for the function call
Douglas Gregora71d8192009-09-04 17:36:40 +00001376 // operator (), and the result of such a call has type void. The only
1377 // effect is the evaluation of the postfix-expression before the dot or
1378 // arrow.
1379 EmitScalarExpr(E->getCallee());
1380 return RValue::get(0);
1381 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001382
Chris Lattner7f02f722007-08-24 05:35:26 +00001383 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
Anders Carlsson98647712009-05-27 01:22:39 +00001384 return EmitCall(Callee, E->getCallee()->getType(),
1385 E->arg_begin(), E->arg_end(), TargetDecl);
Chris Lattnerc5e940f2007-08-31 04:44:06 +00001386}
1387
Daniel Dunbar80e62c22008-09-04 03:20:13 +00001388LValue CodeGenFunction::EmitBinaryOperatorLValue(const BinaryOperator *E) {
Chris Lattner7a574cc2009-05-12 21:28:12 +00001389 // Comma expressions just emit their LHS then their RHS as an l-value.
1390 if (E->getOpcode() == BinaryOperator::Comma) {
1391 EmitAnyExpr(E->getLHS());
1392 return EmitLValue(E->getRHS());
1393 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001394
Fariborz Jahanian52f08bc2009-10-26 21:58:25 +00001395 if (E->getOpcode() == BinaryOperator::PtrMemD ||
1396 E->getOpcode() == BinaryOperator::PtrMemI)
Fariborz Jahanian8bfd31f2009-10-22 22:57:31 +00001397 return EmitPointerToDataMemberBinaryExpr(E);
1398
Daniel Dunbar80e62c22008-09-04 03:20:13 +00001399 // Can only get l-value for binary operator expressions which are a
1400 // simple assignment of aggregate type.
1401 if (E->getOpcode() != BinaryOperator::Assign)
1402 return EmitUnsupportedLValue(E, "binary l-value expression");
1403
Anders Carlsson86aa0cd2009-10-19 18:28:22 +00001404 if (!hasAggregateLLVMType(E->getType())) {
1405 // Emit the LHS as an l-value.
1406 LValue LV = EmitLValue(E->getLHS());
1407
1408 llvm::Value *RHS = EmitScalarExpr(E->getRHS());
1409 EmitStoreOfScalar(RHS, LV.getAddress(), LV.isVolatileQualified(),
1410 E->getType());
1411 return LV;
1412 }
1413
Daniel Dunbar80e62c22008-09-04 03:20:13 +00001414 llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType()));
1415 EmitAggExpr(E, Temp, false);
1416 // FIXME: Are these qualifiers correct?
John McCall0953e762009-09-24 19:53:00 +00001417 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
Daniel Dunbar80e62c22008-09-04 03:20:13 +00001418}
1419
Christopher Lamb22c940e2007-12-29 05:02:41 +00001420LValue CodeGenFunction::EmitCallExprLValue(const CallExpr *E) {
Christopher Lamb22c940e2007-12-29 05:02:41 +00001421 RValue RV = EmitCallExpr(E);
Anders Carlsson48265682009-05-27 01:45:47 +00001422
Chris Lattnereb99b012009-10-28 17:39:19 +00001423 if (!RV.isScalar())
1424 return LValue::MakeAddr(RV.getAggregateAddr(),MakeQualifiers(E->getType()));
1425
1426 assert(E->getCallReturnType()->isReferenceType() &&
1427 "Can't have a scalar return unless the return type is a "
1428 "reference type!");
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001429
Chris Lattnereb99b012009-10-28 17:39:19 +00001430 return LValue::MakeAddr(RV.getScalarVal(), MakeQualifiers(E->getType()));
Christopher Lamb22c940e2007-12-29 05:02:41 +00001431}
1432
Daniel Dunbar5b5c9ef2009-02-11 20:59:32 +00001433LValue CodeGenFunction::EmitVAArgExprLValue(const VAArgExpr *E) {
1434 // FIXME: This shouldn't require another copy.
1435 llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType()));
1436 EmitAggExpr(E, Temp, false);
John McCall0953e762009-09-24 19:53:00 +00001437 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
Daniel Dunbar5b5c9ef2009-02-11 20:59:32 +00001438}
1439
Argyrios Kyrtzidise3a09e62008-09-10 02:36:38 +00001440LValue
1441CodeGenFunction::EmitCXXConditionDeclLValue(const CXXConditionDeclExpr *E) {
1442 EmitLocalBlockVarDecl(*E->getVarDecl());
1443 return EmitDeclRefLValue(E);
1444}
1445
Anders Carlssonb58d0172009-05-30 23:23:33 +00001446LValue CodeGenFunction::EmitCXXConstructLValue(const CXXConstructExpr *E) {
1447 llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(E->getType()), "tmp");
1448 EmitCXXConstructExpr(Temp, E);
John McCall0953e762009-09-24 19:53:00 +00001449 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
Anders Carlssonb58d0172009-05-30 23:23:33 +00001450}
1451
Anders Carlssone61c9e82009-05-30 23:30:54 +00001452LValue
1453CodeGenFunction::EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr *E) {
1454 LValue LV = EmitLValue(E->getSubExpr());
Anders Carlsson543ac0c2009-05-31 00:34:10 +00001455 PushCXXTemporary(E->getTemporary(), LV.getAddress());
Anders Carlssone61c9e82009-05-30 23:30:54 +00001456 return LV;
1457}
1458
Daniel Dunbar0a04d772008-08-23 10:51:21 +00001459LValue CodeGenFunction::EmitObjCMessageExprLValue(const ObjCMessageExpr *E) {
1460 // Can only get l-value for message expression returning aggregate type
1461 RValue RV = EmitObjCMessageExpr(E);
1462 // FIXME: can this be volatile?
John McCall0953e762009-09-24 19:53:00 +00001463 return LValue::MakeAddr(RV.getAggregateAddr(), MakeQualifiers(E->getType()));
Daniel Dunbar0a04d772008-08-23 10:51:21 +00001464}
1465
Daniel Dunbar2a031922009-04-22 05:08:15 +00001466llvm::Value *CodeGenFunction::EmitIvarOffset(const ObjCInterfaceDecl *Interface,
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +00001467 const ObjCIvarDecl *Ivar) {
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001468 return CGM.getObjCRuntime().EmitIvarOffset(*this, Interface, Ivar);
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +00001469}
1470
Fariborz Jahanian45012a72009-02-03 00:09:52 +00001471LValue CodeGenFunction::EmitLValueForIvar(QualType ObjectTy,
1472 llvm::Value *BaseValue,
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +00001473 const ObjCIvarDecl *Ivar,
1474 unsigned CVRQualifiers) {
Chris Lattner26f074b2009-04-17 17:44:48 +00001475 return CGM.getObjCRuntime().EmitObjCValueForIvar(*this, ObjectTy, BaseValue,
Daniel Dunbar525c9b72009-04-21 01:19:28 +00001476 Ivar, CVRQualifiers);
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +00001477}
1478
1479LValue CodeGenFunction::EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E) {
Anders Carlsson29b7e502008-08-25 01:53:23 +00001480 // FIXME: A lot of the code below could be shared with EmitMemberExpr.
1481 llvm::Value *BaseValue = 0;
1482 const Expr *BaseExpr = E->getBase();
John McCall0953e762009-09-24 19:53:00 +00001483 Qualifiers BaseQuals;
Fariborz Jahanian45012a72009-02-03 00:09:52 +00001484 QualType ObjectTy;
Anders Carlsson29b7e502008-08-25 01:53:23 +00001485 if (E->isArrow()) {
1486 BaseValue = EmitScalarExpr(BaseExpr);
Steve Naroff14108da2009-07-10 23:34:53 +00001487 ObjectTy = BaseExpr->getType()->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00001488 BaseQuals = ObjectTy.getQualifiers();
Anders Carlsson29b7e502008-08-25 01:53:23 +00001489 } else {
1490 LValue BaseLV = EmitLValue(BaseExpr);
1491 // FIXME: this isn't right for bitfields.
1492 BaseValue = BaseLV.getAddress();
Fariborz Jahanian45012a72009-02-03 00:09:52 +00001493 ObjectTy = BaseExpr->getType();
John McCall0953e762009-09-24 19:53:00 +00001494 BaseQuals = ObjectTy.getQualifiers();
Anders Carlsson29b7e502008-08-25 01:53:23 +00001495 }
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +00001496
Fariborz Jahaniandbf3cfd2009-09-16 23:11:23 +00001497 LValue LV =
John McCall0953e762009-09-24 19:53:00 +00001498 EmitLValueForIvar(ObjectTy, BaseValue, E->getDecl(),
1499 BaseQuals.getCVRQualifiers());
Fariborz Jahaniandbf3cfd2009-09-16 23:11:23 +00001500 setObjCGCLValueClass(getContext(), E, LV);
1501 return LV;
Chris Lattner391d77a2008-03-30 23:03:07 +00001502}
1503
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001504LValue
Daniel Dunbar85c59ed2008-08-29 08:11:39 +00001505CodeGenFunction::EmitObjCPropertyRefLValue(const ObjCPropertyRefExpr *E) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001506 // This is a special l-value that just issues sends when we load or store
1507 // through it.
Daniel Dunbar85c59ed2008-08-29 08:11:39 +00001508 return LValue::MakePropertyRef(E, E->getType().getCVRQualifiers());
1509}
1510
Chris Lattnereb99b012009-10-28 17:39:19 +00001511LValue CodeGenFunction::EmitObjCKVCRefLValue(
Fariborz Jahanian09105f52009-08-20 17:02:02 +00001512 const ObjCImplicitSetterGetterRefExpr *E) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001513 // This is a special l-value that just issues sends when we load or store
1514 // through it.
Fariborz Jahanian43f44702008-11-22 22:30:21 +00001515 return LValue::MakeKVCRef(E, E->getType().getCVRQualifiers());
1516}
1517
Chris Lattnereb99b012009-10-28 17:39:19 +00001518LValue CodeGenFunction::EmitObjCSuperExprLValue(const ObjCSuperExpr *E) {
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00001519 return EmitUnsupportedLValue(E, "use of super");
1520}
1521
Chris Lattner65459942009-04-25 19:35:26 +00001522LValue CodeGenFunction::EmitStmtExprLValue(const StmtExpr *E) {
Chris Lattner65459942009-04-25 19:35:26 +00001523 // Can only get l-value for message expression returning aggregate type
1524 RValue RV = EmitAnyExprToTemp(E);
1525 // FIXME: can this be volatile?
John McCall0953e762009-09-24 19:53:00 +00001526 return LValue::MakeAddr(RV.getAggregateAddr(), MakeQualifiers(E->getType()));
Chris Lattner65459942009-04-25 19:35:26 +00001527}
1528
1529
Anders Carlsson909fbf72009-11-07 22:00:15 +00001530LValue CodeGenFunction::EmitPointerToDataMemberLValue(const FieldDecl *Field) {
Fariborz Jahanian39762952009-10-21 21:01:47 +00001531 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(Field->getDeclContext());
1532 QualType NNSpecTy =
1533 getContext().getCanonicalType(
1534 getContext().getTypeDeclType(const_cast<CXXRecordDecl*>(ClassDecl)));
Fariborz Jahaniana6362992009-10-21 18:38:00 +00001535 NNSpecTy = getContext().getPointerType(NNSpecTy);
1536 llvm::Value *V = llvm::Constant::getNullValue(ConvertType(NNSpecTy));
1537 LValue MemExpLV = EmitLValueForField(V, const_cast<FieldDecl*>(Field),
1538 /*isUnion*/false, /*Qualifiers*/0);
Chris Lattnereb99b012009-10-28 17:39:19 +00001539 const llvm::Type *ResultType = ConvertType(getContext().getPointerDiffType());
1540 V = Builder.CreatePtrToInt(MemExpLV.getAddress(), ResultType, "datamember");
Anders Carlsson909fbf72009-11-07 22:00:15 +00001541 return LValue::MakeAddr(V, MakeQualifiers(Field->getType()));
Fariborz Jahaniana6362992009-10-21 18:38:00 +00001542}
1543
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001544RValue CodeGenFunction::EmitCall(llvm::Value *Callee, QualType CalleeType,
Anders Carlsson98647712009-05-27 01:22:39 +00001545 CallExpr::const_arg_iterator ArgBeg,
1546 CallExpr::const_arg_iterator ArgEnd,
1547 const Decl *TargetDecl) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001548 // Get the actual function type. The callee type will always be a pointer to
1549 // function type or a block pointer type.
1550 assert(CalleeType->isFunctionPointerType() &&
Anders Carlsson8ac67a72009-04-07 18:53:02 +00001551 "Call must have function pointer type!");
1552
John McCall00a1ad92009-10-23 08:22:42 +00001553 CalleeType = getContext().getCanonicalType(CalleeType);
1554
1555 QualType FnType = cast<PointerType>(CalleeType)->getPointeeType();
1556 QualType ResultType = cast<FunctionType>(FnType)->getResultType();
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001557
1558 CallArgList Args;
John McCall00a1ad92009-10-23 08:22:42 +00001559 EmitCallArgs(Args, dyn_cast<FunctionProtoType>(FnType), ArgBeg, ArgEnd);
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001560
Daniel Dunbar8a9f3fd2009-09-11 22:25:00 +00001561 // FIXME: We should not need to do this, it should be part of the function
1562 // type.
1563 unsigned CallingConvention = 0;
1564 if (const llvm::Function *F =
1565 dyn_cast<llvm::Function>(Callee->stripPointerCasts()))
1566 CallingConvention = F->getCallingConv();
1567 return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args,
1568 CallingConvention),
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +00001569 Callee, Args, TargetDecl);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001570}
Fariborz Jahanian8bfd31f2009-10-22 22:57:31 +00001571
Chris Lattnereb99b012009-10-28 17:39:19 +00001572LValue CodeGenFunction::
1573EmitPointerToDataMemberBinaryExpr(const BinaryOperator *E) {
Fariborz Jahanian8bfd31f2009-10-22 22:57:31 +00001574 llvm::Value *BaseV = EmitLValue(E->getLHS()).getAddress();
Fariborz Jahanian52f08bc2009-10-26 21:58:25 +00001575 if (E->getOpcode() == BinaryOperator::PtrMemI)
1576 BaseV = Builder.CreateLoad(BaseV, "indir.ptr");
Fariborz Jahanian8bfd31f2009-10-22 22:57:31 +00001577 const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(getLLVMContext());
1578 BaseV = Builder.CreateBitCast(BaseV, i8Ty);
1579 LValue RHSLV = EmitLValue(E->getRHS());
1580 llvm::Value *OffsetV =
1581 EmitLoadOfLValue(RHSLV, E->getRHS()->getType()).getScalarVal();
1582 const llvm::Type* ResultType = ConvertType(getContext().getPointerDiffType());
1583 OffsetV = Builder.CreateBitCast(OffsetV, ResultType);
1584 llvm::Value *AddV = Builder.CreateInBoundsGEP(BaseV, OffsetV, "add.ptr");
Chris Lattnereb99b012009-10-28 17:39:19 +00001585
Fariborz Jahanian8bfd31f2009-10-22 22:57:31 +00001586 QualType Ty = E->getRHS()->getType();
Chris Lattnereb99b012009-10-28 17:39:19 +00001587 Ty = Ty->getAs<MemberPointerType>()->getPointeeType();
1588
1589 const llvm::Type *PType = ConvertType(getContext().getPointerType(Ty));
Fariborz Jahanian8bfd31f2009-10-22 22:57:31 +00001590 AddV = Builder.CreateBitCast(AddV, PType);
Chris Lattnereb99b012009-10-28 17:39:19 +00001591 return LValue::MakeAddr(AddV, MakeQualifiers(Ty));
Fariborz Jahanian8bfd31f2009-10-22 22:57:31 +00001592}
1593