blob: c5a89e032b29466e7fdf14eaecf02d583c253899 [file] [log] [blame]
Chris Lattnere47e4402007-06-01 18:02:12 +00001//===--- CGExpr.cpp - Emit LLVM Code from Expressions ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnere47e4402007-06-01 18:02:12 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit Expr nodes as LLVM code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenFunction.h"
Chris Lattnerb6984c42007-06-20 04:44:43 +000015#include "CodeGenModule.h"
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000016#include "CGCall.h"
Daniel Dunbar89da6ad2008-08-13 00:59:25 +000017#include "CGObjCRuntime.h"
Daniel Dunbarad319a72008-08-11 05:00:27 +000018#include "clang/AST/ASTContext.h"
Daniel Dunbar6e8aa532008-08-11 05:35:13 +000019#include "clang/AST/DeclObjC.h"
Mike Stumpe8c3b3e2009-12-15 00:35:12 +000020#include "llvm/Intrinsics.h"
Mike Stump9a4e0122009-12-15 00:59:40 +000021#include "clang/CodeGen/CodeGenOptions.h"
Eli Friedmanf2442dc2008-05-17 20:03:47 +000022#include "llvm/Target/TargetData.h"
Chris Lattnere47e4402007-06-01 18:02:12 +000023using namespace clang;
24using namespace CodeGen;
25
Chris Lattnerd7f58862007-06-02 05:24:33 +000026//===--------------------------------------------------------------------===//
Chris Lattnerf0106d22007-06-02 19:33:17 +000027// Miscellaneous Helper Methods
28//===--------------------------------------------------------------------===//
29
Chris Lattnere9a64532007-06-22 21:44:33 +000030/// CreateTempAlloca - This creates a alloca and inserts it into the entry
31/// block.
32llvm::AllocaInst *CodeGenFunction::CreateTempAlloca(const llvm::Type *Ty,
Daniel Dunbarb5aacc22009-10-19 01:21:05 +000033 const llvm::Twine &Name) {
Chris Lattner47640222009-03-22 00:24:14 +000034 if (!Builder.isNamePreserving())
Daniel Dunbarb5aacc22009-10-19 01:21:05 +000035 return new llvm::AllocaInst(Ty, 0, "", AllocaInsertPt);
Devang Pateldac79de2009-10-12 22:29:02 +000036 return new llvm::AllocaInst(Ty, 0, Name, AllocaInsertPt);
Chris Lattnere9a64532007-06-22 21:44:33 +000037}
Chris Lattner8394d792007-06-05 20:53:16 +000038
39/// EvaluateExprAsBool - Perform the usual unary conversions on the specified
40/// expression and compare the result against zero, returning an Int1Ty value.
Chris Lattner23b7eb62007-06-15 23:05:46 +000041llvm::Value *CodeGenFunction::EvaluateExprAsBool(const Expr *E) {
Chris Lattner268fcce2007-08-26 16:46:58 +000042 QualType BoolTy = getContext().BoolTy;
Eli Friedman68396b12009-12-11 09:26:29 +000043 if (E->getType()->isMemberFunctionPointerType()) {
44 llvm::Value *Ptr = CreateTempAlloca(ConvertType(E->getType()));
45 EmitAggExpr(E, Ptr, /*VolatileDest=*/false);
46
47 // Get the pointer.
48 llvm::Value *FuncPtr = Builder.CreateStructGEP(Ptr, 0, "src.ptr");
49 FuncPtr = Builder.CreateLoad(FuncPtr);
50
51 llvm::Value *IsNotNull =
52 Builder.CreateICmpNE(FuncPtr,
53 llvm::Constant::getNullValue(FuncPtr->getType()),
54 "tobool");
55
56 return IsNotNull;
57 }
Chris Lattnerf3bc75a2008-04-04 16:54:41 +000058 if (!E->getType()->isAnyComplexType())
Chris Lattner268fcce2007-08-26 16:46:58 +000059 return EmitScalarConversion(EmitScalarExpr(E), E->getType(), BoolTy);
Chris Lattner8394d792007-06-05 20:53:16 +000060
Chris Lattner268fcce2007-08-26 16:46:58 +000061 return EmitComplexToScalarConversion(EmitComplexExpr(E), E->getType(),BoolTy);
Chris Lattnerf0106d22007-06-02 19:33:17 +000062}
63
Chris Lattner4647a212007-08-31 22:49:20 +000064/// EmitAnyExpr - Emit code to compute the specified expression which can have
65/// any type. The result is returned as an RValue struct. If this is an
Mike Stump4a3999f2009-09-09 13:00:44 +000066/// aggregate expression, the aggloc/agglocvolatile arguments indicate where the
67/// result should be returned.
68RValue CodeGenFunction::EmitAnyExpr(const Expr *E, llvm::Value *AggLoc,
Anders Carlsson5b106a72009-08-16 07:36:22 +000069 bool IsAggLocVolatile, bool IgnoreResult,
70 bool IsInitializer) {
Chris Lattner4647a212007-08-31 22:49:20 +000071 if (!hasAggregateLLVMType(E->getType()))
Mike Stumpdf0fe272009-05-29 15:46:01 +000072 return RValue::get(EmitScalarExpr(E, IgnoreResult));
Chris Lattnerf3bc75a2008-04-04 16:54:41 +000073 else if (E->getType()->isAnyComplexType())
Mike Stumpdf0fe272009-05-29 15:46:01 +000074 return RValue::getComplex(EmitComplexExpr(E, false, false,
75 IgnoreResult, IgnoreResult));
Mike Stump4a3999f2009-09-09 13:00:44 +000076
Anders Carlsson5b106a72009-08-16 07:36:22 +000077 EmitAggExpr(E, AggLoc, IsAggLocVolatile, IgnoreResult, IsInitializer);
78 return RValue::getAggregate(AggLoc, IsAggLocVolatile);
Chris Lattner4647a212007-08-31 22:49:20 +000079}
80
Mike Stump4a3999f2009-09-09 13:00:44 +000081/// EmitAnyExprToTemp - Similary to EmitAnyExpr(), however, the result will
82/// always be accessible even if no aggregate location is provided.
83RValue CodeGenFunction::EmitAnyExprToTemp(const Expr *E,
Anders Carlsson5b106a72009-08-16 07:36:22 +000084 bool IsAggLocVolatile,
85 bool IsInitializer) {
86 llvm::Value *AggLoc = 0;
Mike Stump4a3999f2009-09-09 13:00:44 +000087
88 if (hasAggregateLLVMType(E->getType()) &&
Daniel Dunbar41cf9de2008-09-09 01:06:48 +000089 !E->getType()->isAnyComplexType())
90 AggLoc = CreateTempAlloca(ConvertType(E->getType()), "agg.tmp");
Mike Stump4a3999f2009-09-09 13:00:44 +000091 return EmitAnyExpr(E, AggLoc, IsAggLocVolatile, /*IgnoreResult=*/false,
Anders Carlsson5b106a72009-08-16 07:36:22 +000092 IsInitializer);
Daniel Dunbar41cf9de2008-09-09 01:06:48 +000093}
94
Anders Carlsson6f5a0152009-05-20 00:24:07 +000095RValue CodeGenFunction::EmitReferenceBindingToExpr(const Expr* E,
Anders Carlsson5b106a72009-08-16 07:36:22 +000096 QualType DestType,
97 bool IsInitializer) {
Anders Carlsson69c2c4b2009-10-18 23:09:21 +000098 bool ShouldDestroyTemporaries = false;
99 unsigned OldNumLiveTemporaries = 0;
100
Anders Carlsson66413c22009-10-15 00:51:46 +0000101 if (const CXXExprWithTemporaries *TE = dyn_cast<CXXExprWithTemporaries>(E)) {
Anders Carlsson6e997b22009-12-15 20:51:39 +0000102 ShouldDestroyTemporaries = true;
103
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000104 // Keep track of the current cleanup stack depth.
Anders Carlsson6e997b22009-12-15 20:51:39 +0000105 OldNumLiveTemporaries = LiveTemporaries.size();
Anders Carlsson66413c22009-10-15 00:51:46 +0000106
Anders Carlsson69c2c4b2009-10-18 23:09:21 +0000107 E = TE->getSubExpr();
Anders Carlsson66413c22009-10-15 00:51:46 +0000108 }
109
Eli Friedmanc21cb442009-05-20 02:31:19 +0000110 RValue Val;
111 if (E->isLvalue(getContext()) == Expr::LV_Valid) {
Anders Carlsson7d4c0832009-05-20 00:36:58 +0000112 // Emit the expr as an lvalue.
113 LValue LV = EmitLValue(E);
Eli Friedmanc21cb442009-05-20 02:31:19 +0000114 if (LV.isSimple())
115 return RValue::get(LV.getAddress());
116 Val = EmitLoadOfLValue(LV, E->getType());
Anders Carlsson69c2c4b2009-10-18 23:09:21 +0000117
118 if (ShouldDestroyTemporaries) {
119 // Pop temporaries.
120 while (LiveTemporaries.size() > OldNumLiveTemporaries)
121 PopCXXTemporary();
122 }
Eli Friedmanc21cb442009-05-20 02:31:19 +0000123 } else {
Anders Carlsson66413c22009-10-15 00:51:46 +0000124 const CXXRecordDecl *BaseClassDecl = 0;
125 const CXXRecordDecl *DerivedClassDecl = 0;
126
127 if (const CastExpr *CE =
128 dyn_cast<CastExpr>(E->IgnoreParenNoopCasts(getContext()))) {
129 if (CE->getCastKind() == CastExpr::CK_DerivedToBase) {
130 E = CE->getSubExpr();
131
132 BaseClassDecl =
133 cast<CXXRecordDecl>(CE->getType()->getAs<RecordType>()->getDecl());
134 DerivedClassDecl =
135 cast<CXXRecordDecl>(E->getType()->getAs<RecordType>()->getDecl());
136 }
137 }
138
Anders Carlsson5b106a72009-08-16 07:36:22 +0000139 Val = EmitAnyExprToTemp(E, /*IsAggLocVolatile=*/false,
140 IsInitializer);
Mike Stump4a3999f2009-09-09 13:00:44 +0000141
Anders Carlsson69c2c4b2009-10-18 23:09:21 +0000142 if (ShouldDestroyTemporaries) {
143 // Pop temporaries.
144 while (LiveTemporaries.size() > OldNumLiveTemporaries)
145 PopCXXTemporary();
146 }
147
Anders Carlsson3b848942009-08-16 17:54:29 +0000148 if (IsInitializer) {
149 // We might have to destroy the temporary variable.
150 if (const RecordType *RT = E->getType()->getAs<RecordType>()) {
151 if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
152 if (!ClassDecl->hasTrivialDestructor()) {
Mike Stump4a3999f2009-09-09 13:00:44 +0000153 const CXXDestructorDecl *Dtor =
Anders Carlsson3b848942009-08-16 17:54:29 +0000154 ClassDecl->getDestructor(getContext());
Mike Stump4a3999f2009-09-09 13:00:44 +0000155
Mike Stumpaff69af2009-12-09 03:35:49 +0000156 {
Anders Carlsson0c6a7d82009-12-11 01:00:09 +0000157 DelayedCleanupBlock Scope(*this);
Mike Stumpaff69af2009-12-09 03:35:49 +0000158 EmitCXXDestructorCall(Dtor, Dtor_Complete,
159 Val.getAggregateAddr());
Anders Carlsson0c6a7d82009-12-11 01:00:09 +0000160
161 // Make sure to jump to the exit block.
162 EmitBranch(Scope.getCleanupExitBlock());
Mike Stumpaff69af2009-12-09 03:35:49 +0000163 }
164 if (Exceptions) {
165 EHCleanupBlock Cleanup(*this);
166 EmitCXXDestructorCall(Dtor, Dtor_Complete,
167 Val.getAggregateAddr());
168 }
Anders Carlsson3b848942009-08-16 17:54:29 +0000169 }
Anders Carlssonb80760b2009-08-16 17:50:25 +0000170 }
171 }
172 }
Anders Carlsson66413c22009-10-15 00:51:46 +0000173
174 // Check if need to perform the derived-to-base cast.
175 if (BaseClassDecl) {
176 llvm::Value *Derived = Val.getAggregateAddr();
Anders Carlsson66413c22009-10-15 00:51:46 +0000177 llvm::Value *Base =
Anders Carlsson8c793172009-11-23 17:57:54 +0000178 GetAddressOfBaseClass(Derived, DerivedClassDecl, BaseClassDecl,
179 /*NullCheckValue=*/false);
Anders Carlsson66413c22009-10-15 00:51:46 +0000180 return RValue::get(Base);
181 }
Anders Carlsson7d4c0832009-05-20 00:36:58 +0000182 }
Eli Friedmanc21cb442009-05-20 02:31:19 +0000183
184 if (Val.isAggregate()) {
185 Val = RValue::get(Val.getAggregateAddr());
186 } else {
Anders Carlsson02bb7f02009-05-20 01:35:03 +0000187 // Create a temporary variable that we can bind the reference to.
Mike Stump4a3999f2009-09-09 13:00:44 +0000188 llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(E->getType()),
Anders Carlsson145eae52009-05-20 01:03:17 +0000189 "reftmp");
Eli Friedmanc21cb442009-05-20 02:31:19 +0000190 if (Val.isScalar())
191 EmitStoreOfScalar(Val.getScalarVal(), Temp, false, E->getType());
192 else
193 StoreComplexToAddr(Val.getComplexVal(), Temp, false);
194 Val = RValue::get(Temp);
Anders Carlsson145eae52009-05-20 01:03:17 +0000195 }
Eli Friedmanc21cb442009-05-20 02:31:19 +0000196
197 return Val;
Anders Carlsson6f5a0152009-05-20 00:24:07 +0000198}
199
200
Mike Stump4a3999f2009-09-09 13:00:44 +0000201/// getAccessedFieldNo - Given an encoded value and a result number, return the
202/// input field number being accessed.
203unsigned CodeGenFunction::getAccessedFieldNo(unsigned Idx,
Dan Gohman75d69da2008-05-22 00:50:06 +0000204 const llvm::Constant *Elts) {
205 if (isa<llvm::ConstantAggregateZero>(Elts))
206 return 0;
Mike Stump4a3999f2009-09-09 13:00:44 +0000207
Dan Gohman75d69da2008-05-22 00:50:06 +0000208 return cast<llvm::ConstantInt>(Elts->getOperand(Idx))->getZExtValue();
209}
210
Chris Lattner4647a212007-08-31 22:49:20 +0000211
Chris Lattnera45c5af2007-06-02 19:47:04 +0000212//===----------------------------------------------------------------------===//
Chris Lattnerd7f58862007-06-02 05:24:33 +0000213// LValue Expression Emission
Chris Lattnera45c5af2007-06-02 19:47:04 +0000214//===----------------------------------------------------------------------===//
Chris Lattnerd7f58862007-06-02 05:24:33 +0000215
Daniel Dunbarc79407f2009-02-05 07:09:07 +0000216RValue CodeGenFunction::GetUndefRValue(QualType Ty) {
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000217 if (Ty->isVoidType())
Daniel Dunbarc79407f2009-02-05 07:09:07 +0000218 return RValue::get(0);
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000219
220 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
Daniel Dunbar8429dbc2009-01-09 20:09:28 +0000221 const llvm::Type *EltTy = ConvertType(CTy->getElementType());
Owen Anderson7ec07a52009-07-30 23:11:26 +0000222 llvm::Value *U = llvm::UndefValue::get(EltTy);
Daniel Dunbar8429dbc2009-01-09 20:09:28 +0000223 return RValue::getComplex(std::make_pair(U, U));
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000224 }
225
226 if (hasAggregateLLVMType(Ty)) {
Owen Anderson9793f0e2009-07-29 22:16:19 +0000227 const llvm::Type *LTy = llvm::PointerType::getUnqual(ConvertType(Ty));
Owen Anderson7ec07a52009-07-30 23:11:26 +0000228 return RValue::getAggregate(llvm::UndefValue::get(LTy));
Daniel Dunbar8429dbc2009-01-09 20:09:28 +0000229 }
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000230
231 return RValue::get(llvm::UndefValue::get(ConvertType(Ty)));
Daniel Dunbarbb197e42009-01-09 16:50:52 +0000232}
233
Daniel Dunbarc79407f2009-02-05 07:09:07 +0000234RValue CodeGenFunction::EmitUnsupportedRValue(const Expr *E,
235 const char *Name) {
236 ErrorUnsupported(E, Name);
237 return GetUndefRValue(E->getType());
238}
239
Daniel Dunbarf2e69882008-08-25 20:45:57 +0000240LValue CodeGenFunction::EmitUnsupportedLValue(const Expr *E,
241 const char *Name) {
242 ErrorUnsupported(E, Name);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000243 llvm::Type *Ty = llvm::PointerType::getUnqual(ConvertType(E->getType()));
Owen Anderson7ec07a52009-07-30 23:11:26 +0000244 return LValue::MakeAddr(llvm::UndefValue::get(Ty),
John McCall8ccfcb52009-09-24 19:53:00 +0000245 MakeQualifiers(E->getType()));
Daniel Dunbarf2e69882008-08-25 20:45:57 +0000246}
247
Chris Lattner8394d792007-06-05 20:53:16 +0000248/// EmitLValue - Emit code to compute a designator that specifies the location
249/// of the expression.
250///
Mike Stump4a3999f2009-09-09 13:00:44 +0000251/// This can return one of two things: a simple address or a bitfield reference.
252/// In either case, the LLVM Value* in the LValue structure is guaranteed to be
253/// an LLVM pointer type.
Chris Lattner8394d792007-06-05 20:53:16 +0000254///
Mike Stump4a3999f2009-09-09 13:00:44 +0000255/// If this returns a bitfield reference, nothing about the pointee type of the
256/// LLVM value is known: For example, it may not be a pointer to an integer.
Chris Lattner8394d792007-06-05 20:53:16 +0000257///
Mike Stump4a3999f2009-09-09 13:00:44 +0000258/// If this returns a normal address, and if the lvalue's C type is fixed size,
259/// this method guarantees that the returned pointer type will point to an LLVM
260/// type of the same size of the lvalue's type. If the lvalue has a variable
261/// length type, this is not possible.
Chris Lattner8394d792007-06-05 20:53:16 +0000262///
Chris Lattnerd7f58862007-06-02 05:24:33 +0000263LValue CodeGenFunction::EmitLValue(const Expr *E) {
264 switch (E->getStmtClass()) {
Daniel Dunbarf2e69882008-08-25 20:45:57 +0000265 default: return EmitUnsupportedLValue(E, "l-value expression");
Chris Lattnerd7f58862007-06-02 05:24:33 +0000266
Fariborz Jahanian531c16f2009-12-09 23:35:29 +0000267 case Expr::ObjCIsaExprClass:
268 return EmitObjCIsaExpr(cast<ObjCIsaExpr>(E));
Mike Stump4a3999f2009-09-09 13:00:44 +0000269 case Expr::BinaryOperatorClass:
Daniel Dunbar8cde00a2008-09-04 03:20:13 +0000270 return EmitBinaryOperatorLValue(cast<BinaryOperator>(E));
Mike Stump4a3999f2009-09-09 13:00:44 +0000271 case Expr::CallExprClass:
Anders Carlssonc82555f2009-09-01 21:18:52 +0000272 case Expr::CXXMemberCallExprClass:
Douglas Gregor993603d2008-11-14 16:09:21 +0000273 case Expr::CXXOperatorCallExprClass:
274 return EmitCallExprLValue(cast<CallExpr>(E));
Daniel Dunbar8d9dc4a2009-02-11 20:59:32 +0000275 case Expr::VAArgExprClass:
276 return EmitVAArgExprLValue(cast<VAArgExpr>(E));
Mike Stump4a3999f2009-09-09 13:00:44 +0000277 case Expr::DeclRefExprClass:
Douglas Gregorc7acfdf2009-01-06 05:10:23 +0000278 return EmitDeclRefLValue(cast<DeclRefExpr>(E));
Chris Lattner946aa312007-06-05 03:59:43 +0000279 case Expr::ParenExprClass:return EmitLValue(cast<ParenExpr>(E)->getSubExpr());
Chris Lattner6307f192008-08-10 01:53:14 +0000280 case Expr::PredefinedExprClass:
281 return EmitPredefinedLValue(cast<PredefinedExpr>(E));
Chris Lattner4347e3692007-06-06 04:54:52 +0000282 case Expr::StringLiteralClass:
283 return EmitStringLiteralLValue(cast<StringLiteral>(E));
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +0000284 case Expr::ObjCEncodeExprClass:
285 return EmitObjCEncodeExprLValue(cast<ObjCEncodeExpr>(E));
Chris Lattner4bd55962008-03-30 23:03:07 +0000286
Mike Stump4a3999f2009-09-09 13:00:44 +0000287 case Expr::BlockDeclRefExprClass:
Mike Stump1db7d042009-02-28 09:07:16 +0000288 return EmitBlockDeclRefLValue(cast<BlockDeclRefExpr>(E));
289
Anders Carlsson3be22e22009-05-30 23:23:33 +0000290 case Expr::CXXTemporaryObjectExprClass:
291 case Expr::CXXConstructExprClass:
Anders Carlssonfd2af0c2009-05-30 23:30:54 +0000292 return EmitCXXConstructLValue(cast<CXXConstructExpr>(E));
293 case Expr::CXXBindTemporaryExprClass:
294 return EmitCXXBindTemporaryLValue(cast<CXXBindTemporaryExpr>(E));
Anders Carlsson96bad9a2009-09-14 01:10:45 +0000295 case Expr::CXXExprWithTemporariesClass:
296 return EmitCXXExprWithTemporariesLValue(cast<CXXExprWithTemporaries>(E));
Anders Carlsson52ce3bb2009-11-14 01:51:50 +0000297 case Expr::CXXZeroInitValueExprClass:
298 return EmitNullInitializationLValue(cast<CXXZeroInitValueExpr>(E));
299 case Expr::CXXDefaultArgExprClass:
300 return EmitLValue(cast<CXXDefaultArgExpr>(E)->getExpr());
Mike Stumpc9b231c2009-11-15 08:09:41 +0000301 case Expr::CXXTypeidExprClass:
302 return EmitCXXTypeidLValue(cast<CXXTypeidExpr>(E));
Anders Carlssonfd2af0c2009-05-30 23:30:54 +0000303
Daniel Dunbarc8317a42008-08-23 10:51:21 +0000304 case Expr::ObjCMessageExprClass:
305 return EmitObjCMessageExprLValue(cast<ObjCMessageExpr>(E));
Mike Stump4a3999f2009-09-09 13:00:44 +0000306 case Expr::ObjCIvarRefExprClass:
Chris Lattner4bd55962008-03-30 23:03:07 +0000307 return EmitObjCIvarRefLValue(cast<ObjCIvarRefExpr>(E));
Daniel Dunbarf2e69882008-08-25 20:45:57 +0000308 case Expr::ObjCPropertyRefExprClass:
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +0000309 return EmitObjCPropertyRefLValue(cast<ObjCPropertyRefExpr>(E));
Fariborz Jahanian9a846652009-08-20 17:02:02 +0000310 case Expr::ObjCImplicitSetterGetterRefExprClass:
311 return EmitObjCKVCRefLValue(cast<ObjCImplicitSetterGetterRefExpr>(E));
Douglas Gregor8ea1f532008-11-04 14:56:14 +0000312 case Expr::ObjCSuperExprClass:
Chris Lattnera4185c52009-04-25 19:35:26 +0000313 return EmitObjCSuperExprLValue(cast<ObjCSuperExpr>(E));
Douglas Gregor8ea1f532008-11-04 14:56:14 +0000314
Chris Lattnera4185c52009-04-25 19:35:26 +0000315 case Expr::StmtExprClass:
316 return EmitStmtExprLValue(cast<StmtExpr>(E));
Mike Stump4a3999f2009-09-09 13:00:44 +0000317 case Expr::UnaryOperatorClass:
Chris Lattner8394d792007-06-05 20:53:16 +0000318 return EmitUnaryOpLValue(cast<UnaryOperator>(E));
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000319 case Expr::ArraySubscriptExprClass:
320 return EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E));
Nate Begemance4d7fc2008-04-18 23:10:10 +0000321 case Expr::ExtVectorElementExprClass:
322 return EmitExtVectorElementExpr(cast<ExtVectorElementExpr>(E));
Mike Stump4a3999f2009-09-09 13:00:44 +0000323 case Expr::MemberExprClass:
Douglas Gregorc1905232009-08-26 22:36:53 +0000324 return EmitMemberExpr(cast<MemberExpr>(E));
Eli Friedman9fd8b682008-05-13 23:18:27 +0000325 case Expr::CompoundLiteralExprClass:
326 return EmitCompoundLiteralLValue(cast<CompoundLiteralExpr>(E));
Daniel Dunbarbf1fe8c2009-03-24 02:38:23 +0000327 case Expr::ConditionalOperatorClass:
Anders Carlsson1450adb2009-09-15 16:35:24 +0000328 return EmitConditionalOperatorLValue(cast<ConditionalOperator>(E));
Chris Lattner053441f2008-12-12 05:35:08 +0000329 case Expr::ChooseExprClass:
Eli Friedmane0a5b8b2009-03-04 05:52:32 +0000330 return EmitLValue(cast<ChooseExpr>(E)->getChosenSubExpr(getContext()));
Chris Lattner63d06ab2009-03-18 04:02:57 +0000331 case Expr::ImplicitCastExprClass:
332 case Expr::CStyleCastExprClass:
333 case Expr::CXXFunctionalCastExprClass:
334 case Expr::CXXStaticCastExprClass:
335 case Expr::CXXDynamicCastExprClass:
336 case Expr::CXXReinterpretCastExprClass:
337 case Expr::CXXConstCastExprClass:
Chris Lattner28bcf1a2009-03-18 18:28:57 +0000338 return EmitCastLValue(cast<CastExpr>(E));
Chris Lattnerd7f58862007-06-02 05:24:33 +0000339 }
340}
341
Daniel Dunbar1d425462009-02-10 00:57:50 +0000342llvm::Value *CodeGenFunction::EmitLoadOfScalar(llvm::Value *Addr, bool Volatile,
343 QualType Ty) {
Daniel Dunbarc76493a2009-11-29 21:23:36 +0000344 llvm::LoadInst *Load = Builder.CreateLoad(Addr, "tmp");
345 if (Volatile)
346 Load->setVolatile(true);
Daniel Dunbar1d425462009-02-10 00:57:50 +0000347
Anders Carlsson29a1be32009-05-19 19:36:19 +0000348 // Bool can have different representation in memory than in registers.
Daniel Dunbarc76493a2009-11-29 21:23:36 +0000349 llvm::Value *V = Load;
Daniel Dunbar1d425462009-02-10 00:57:50 +0000350 if (Ty->isBooleanType())
Owen Anderson41a75022009-08-13 21:57:51 +0000351 if (V->getType() != llvm::Type::getInt1Ty(VMContext))
352 V = Builder.CreateTrunc(V, llvm::Type::getInt1Ty(VMContext), "tobool");
Mike Stump4a3999f2009-09-09 13:00:44 +0000353
Daniel Dunbar1d425462009-02-10 00:57:50 +0000354 return V;
355}
356
357void CodeGenFunction::EmitStoreOfScalar(llvm::Value *Value, llvm::Value *Addr,
Anders Carlsson83709642009-05-19 18:50:41 +0000358 bool Volatile, QualType Ty) {
Mike Stump4a3999f2009-09-09 13:00:44 +0000359
Anders Carlsson29a1be32009-05-19 19:36:19 +0000360 if (Ty->isBooleanType()) {
361 // Bool can have different representation in memory than in registers.
Anders Carlsson29a1be32009-05-19 19:36:19 +0000362 const llvm::PointerType *DstPtr = cast<llvm::PointerType>(Addr->getType());
Eli Friedmanb2b120f2009-12-01 22:31:51 +0000363 Value = Builder.CreateIntCast(Value, DstPtr->getElementType(), false);
Daniel Dunbar1d425462009-02-10 00:57:50 +0000364 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000365 Builder.CreateStore(Value, Addr, Volatile);
Daniel Dunbar1d425462009-02-10 00:57:50 +0000366}
367
Mike Stump4a3999f2009-09-09 13:00:44 +0000368/// EmitLoadOfLValue - Given an expression that represents a value lvalue, this
369/// method emits the address of the lvalue, then loads the result as an rvalue,
370/// returning the rvalue.
Chris Lattner9369a562007-06-29 16:31:29 +0000371RValue CodeGenFunction::EmitLoadOfLValue(LValue LV, QualType ExprType) {
Fariborz Jahanian50a12702008-11-19 17:34:06 +0000372 if (LV.isObjCWeak()) {
Mike Stump4a3999f2009-09-09 13:00:44 +0000373 // load of a __weak object.
Fariborz Jahanianf5125d12008-11-18 21:45:40 +0000374 llvm::Value *AddrWeakObj = LV.getAddress();
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000375 return RValue::get(CGM.getObjCRuntime().EmitObjCWeakRead(*this,
376 AddrWeakObj));
Fariborz Jahanianf5125d12008-11-18 21:45:40 +0000377 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000378
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000379 if (LV.isSimple()) {
380 llvm::Value *Ptr = LV.getAddress();
381 const llvm::Type *EltTy =
382 cast<llvm::PointerType>(Ptr->getType())->getElementType();
Mike Stump4a3999f2009-09-09 13:00:44 +0000383
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000384 // Simple scalar l-value.
Daniel Dunbar1d425462009-02-10 00:57:50 +0000385 if (EltTy->isSingleValueType())
Mike Stump4a3999f2009-09-09 13:00:44 +0000386 return RValue::get(EmitLoadOfScalar(Ptr, LV.isVolatileQualified(),
Daniel Dunbar1d425462009-02-10 00:57:50 +0000387 ExprType));
Mike Stump4a3999f2009-09-09 13:00:44 +0000388
Chris Lattner6278e6a2007-08-11 00:04:45 +0000389 assert(ExprType->isFunctionType() && "Unknown scalar value");
390 return RValue::get(Ptr);
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000391 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000392
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000393 if (LV.isVectorElt()) {
Eli Friedman327944b2008-06-13 23:01:12 +0000394 llvm::Value *Vec = Builder.CreateLoad(LV.getVectorAddr(),
395 LV.isVolatileQualified(), "tmp");
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000396 return RValue::get(Builder.CreateExtractElement(Vec, LV.getVectorIdx(),
397 "vecext"));
398 }
Chris Lattner73ab9b32007-08-03 00:16:29 +0000399
400 // If this is a reference to a subset of the elements of a vector, either
401 // shuffle the input or extract/insert them as appropriate.
Nate Begemance4d7fc2008-04-18 23:10:10 +0000402 if (LV.isExtVectorElt())
403 return EmitLoadOfExtVectorElementLValue(LV, ExprType);
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000404
405 if (LV.isBitfield())
406 return EmitLoadOfBitfieldLValue(LV, ExprType);
407
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +0000408 if (LV.isPropertyRef())
409 return EmitLoadOfPropertyRefLValue(LV, ExprType);
410
Chris Lattner6c7ce102009-02-16 21:11:58 +0000411 assert(LV.isKVCRef() && "Unknown LValue type!");
412 return EmitLoadOfKVCRefLValue(LV, ExprType);
Chris Lattner8394d792007-06-05 20:53:16 +0000413}
414
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000415RValue CodeGenFunction::EmitLoadOfBitfieldLValue(LValue LV,
416 QualType ExprType) {
Daniel Dunbaread7c912008-08-06 05:08:45 +0000417 unsigned StartBit = LV.getBitfieldStartBit();
418 unsigned BitfieldSize = LV.getBitfieldSize();
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000419 llvm::Value *Ptr = LV.getBitfieldAddr();
Daniel Dunbaread7c912008-08-06 05:08:45 +0000420
Mike Stump4a3999f2009-09-09 13:00:44 +0000421 const llvm::Type *EltTy =
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000422 cast<llvm::PointerType>(Ptr->getType())->getElementType();
Daniel Dunbaread7c912008-08-06 05:08:45 +0000423 unsigned EltTySize = CGM.getTargetData().getTypeSizeInBits(EltTy);
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000424
Mike Stump4a3999f2009-09-09 13:00:44 +0000425 // In some cases the bitfield may straddle two memory locations. Currently we
426 // load the entire bitfield, then do the magic to sign-extend it if
427 // necessary. This results in somewhat more code than necessary for the common
428 // case (one load), since two shifts accomplish both the masking and sign
429 // extension.
Daniel Dunbaread7c912008-08-06 05:08:45 +0000430 unsigned LowBits = std::min(BitfieldSize, EltTySize - StartBit);
431 llvm::Value *Val = Builder.CreateLoad(Ptr, LV.isVolatileQualified(), "tmp");
Mike Stump4a3999f2009-09-09 13:00:44 +0000432
Daniel Dunbaread7c912008-08-06 05:08:45 +0000433 // Shift to proper location.
Daniel Dunbarf7fb7502008-11-13 02:20:34 +0000434 if (StartBit)
Chris Lattner72ecc682009-12-06 17:22:42 +0000435 Val = Builder.CreateLShr(Val, StartBit, "bf.lo");
Mike Stump4a3999f2009-09-09 13:00:44 +0000436
Daniel Dunbaread7c912008-08-06 05:08:45 +0000437 // Mask off unused bits.
Mike Stump4a3999f2009-09-09 13:00:44 +0000438 llvm::Constant *LowMask = llvm::ConstantInt::get(VMContext,
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000439 llvm::APInt::getLowBitsSet(EltTySize, LowBits));
Daniel Dunbaread7c912008-08-06 05:08:45 +0000440 Val = Builder.CreateAnd(Val, LowMask, "bf.lo.cleared");
Mike Stump4a3999f2009-09-09 13:00:44 +0000441
Daniel Dunbaread7c912008-08-06 05:08:45 +0000442 // Fetch the high bits if necessary.
443 if (LowBits < BitfieldSize) {
444 unsigned HighBits = BitfieldSize - LowBits;
Owen Anderson41a75022009-08-13 21:57:51 +0000445 llvm::Value *HighPtr = Builder.CreateGEP(Ptr, llvm::ConstantInt::get(
Mike Stump4a3999f2009-09-09 13:00:44 +0000446 llvm::Type::getInt32Ty(VMContext), 1), "bf.ptr.hi");
447 llvm::Value *HighVal = Builder.CreateLoad(HighPtr,
Daniel Dunbaread7c912008-08-06 05:08:45 +0000448 LV.isVolatileQualified(),
449 "tmp");
Mike Stump4a3999f2009-09-09 13:00:44 +0000450
Daniel Dunbaread7c912008-08-06 05:08:45 +0000451 // Mask off unused bits.
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000452 llvm::Constant *HighMask = llvm::ConstantInt::get(VMContext,
453 llvm::APInt::getLowBitsSet(EltTySize, HighBits));
Daniel Dunbaread7c912008-08-06 05:08:45 +0000454 HighVal = Builder.CreateAnd(HighVal, HighMask, "bf.lo.cleared");
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000455
Daniel Dunbaread7c912008-08-06 05:08:45 +0000456 // Shift to proper location and or in to bitfield value.
Chris Lattner72ecc682009-12-06 17:22:42 +0000457 HighVal = Builder.CreateShl(HighVal, LowBits);
Daniel Dunbaread7c912008-08-06 05:08:45 +0000458 Val = Builder.CreateOr(Val, HighVal, "bf.val");
459 }
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000460
Daniel Dunbaread7c912008-08-06 05:08:45 +0000461 // Sign extend if necessary.
462 if (LV.isBitfieldSigned()) {
Mike Stump4a3999f2009-09-09 13:00:44 +0000463 llvm::Value *ExtraBits = llvm::ConstantInt::get(EltTy,
Daniel Dunbaread7c912008-08-06 05:08:45 +0000464 EltTySize - BitfieldSize);
Mike Stump4a3999f2009-09-09 13:00:44 +0000465 Val = Builder.CreateAShr(Builder.CreateShl(Val, ExtraBits),
Daniel Dunbaread7c912008-08-06 05:08:45 +0000466 ExtraBits, "bf.val.sext");
467 }
Eli Friedmanf2442dc2008-05-17 20:03:47 +0000468
Mike Stump4a3999f2009-09-09 13:00:44 +0000469 // The bitfield type and the normal type differ when the storage sizes differ
470 // (currently just _Bool).
Daniel Dunbaread7c912008-08-06 05:08:45 +0000471 Val = Builder.CreateIntCast(Val, ConvertType(ExprType), false, "tmp");
Eli Friedmanf2442dc2008-05-17 20:03:47 +0000472
Daniel Dunbaread7c912008-08-06 05:08:45 +0000473 return RValue::get(Val);
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000474}
475
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +0000476RValue CodeGenFunction::EmitLoadOfPropertyRefLValue(LValue LV,
477 QualType ExprType) {
478 return EmitObjCPropertyGet(LV.getPropertyRefExpr());
479}
480
Fariborz Jahanian9ac53512008-11-22 22:30:21 +0000481RValue CodeGenFunction::EmitLoadOfKVCRefLValue(LValue LV,
482 QualType ExprType) {
483 return EmitObjCPropertyGet(LV.getKVCRefExpr());
484}
485
Nate Begemanb699c9b2009-01-18 06:42:49 +0000486// If this is a reference to a subset of the elements of a vector, create an
487// appropriate shufflevector.
Nate Begemance4d7fc2008-04-18 23:10:10 +0000488RValue CodeGenFunction::EmitLoadOfExtVectorElementLValue(LValue LV,
489 QualType ExprType) {
Eli Friedman327944b2008-06-13 23:01:12 +0000490 llvm::Value *Vec = Builder.CreateLoad(LV.getExtVectorAddr(),
491 LV.isVolatileQualified(), "tmp");
Mike Stump4a3999f2009-09-09 13:00:44 +0000492
Nate Begemanf322eab2008-05-09 06:41:27 +0000493 const llvm::Constant *Elts = LV.getExtVectorElts();
Mike Stump4a3999f2009-09-09 13:00:44 +0000494
495 // If the result of the expression is a non-vector type, we must be extracting
496 // a single element. Just codegen as an extractelement.
John McCall9dd450b2009-09-21 23:43:11 +0000497 const VectorType *ExprVT = ExprType->getAs<VectorType>();
Chris Lattner8eab8ff2007-08-10 17:10:08 +0000498 if (!ExprVT) {
Dan Gohman75d69da2008-05-22 00:50:06 +0000499 unsigned InIdx = getAccessedFieldNo(0, Elts);
Owen Anderson41a75022009-08-13 21:57:51 +0000500 llvm::Value *Elt = llvm::ConstantInt::get(
501 llvm::Type::getInt32Ty(VMContext), InIdx);
Chris Lattner40ff7012007-08-03 16:18:34 +0000502 return RValue::get(Builder.CreateExtractElement(Vec, Elt, "tmp"));
503 }
Nate Begemanb699c9b2009-01-18 06:42:49 +0000504
505 // Always use shuffle vector to try to retain the original program structure
Chris Lattner8eab8ff2007-08-10 17:10:08 +0000506 unsigned NumResultElts = ExprVT->getNumElements();
Mike Stump4a3999f2009-09-09 13:00:44 +0000507
Nate Begemanb699c9b2009-01-18 06:42:49 +0000508 llvm::SmallVector<llvm::Constant*, 4> Mask;
Chris Lattner40ff7012007-08-03 16:18:34 +0000509 for (unsigned i = 0; i != NumResultElts; ++i) {
Dan Gohman75d69da2008-05-22 00:50:06 +0000510 unsigned InIdx = getAccessedFieldNo(i, Elts);
Owen Anderson41a75022009-08-13 21:57:51 +0000511 Mask.push_back(llvm::ConstantInt::get(
512 llvm::Type::getInt32Ty(VMContext), InIdx));
Chris Lattner40ff7012007-08-03 16:18:34 +0000513 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000514
Owen Anderson3cc120a2009-07-28 21:22:35 +0000515 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
Nate Begemanb699c9b2009-01-18 06:42:49 +0000516 Vec = Builder.CreateShuffleVector(Vec,
Owen Anderson7ec07a52009-07-30 23:11:26 +0000517 llvm::UndefValue::get(Vec->getType()),
Nate Begemanb699c9b2009-01-18 06:42:49 +0000518 MaskV, "tmp");
519 return RValue::get(Vec);
Chris Lattner40ff7012007-08-03 16:18:34 +0000520}
521
522
Chris Lattner9369a562007-06-29 16:31:29 +0000523
Chris Lattner8394d792007-06-05 20:53:16 +0000524/// EmitStoreThroughLValue - Store the specified rvalue into the specified
525/// lvalue, where both are guaranteed to the have the same type, and that type
526/// is 'Ty'.
Mike Stump4a3999f2009-09-09 13:00:44 +0000527void CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst,
Chris Lattner8394d792007-06-05 20:53:16 +0000528 QualType Ty) {
Chris Lattner41d480e2007-08-03 16:28:33 +0000529 if (!Dst.isSimple()) {
530 if (Dst.isVectorElt()) {
531 // Read/modify/write the vector, inserting the new element.
Eli Friedman327944b2008-06-13 23:01:12 +0000532 llvm::Value *Vec = Builder.CreateLoad(Dst.getVectorAddr(),
533 Dst.isVolatileQualified(), "tmp");
Chris Lattner4647a212007-08-31 22:49:20 +0000534 Vec = Builder.CreateInsertElement(Vec, Src.getScalarVal(),
Chris Lattner41d480e2007-08-03 16:28:33 +0000535 Dst.getVectorIdx(), "vecins");
Eli Friedman327944b2008-06-13 23:01:12 +0000536 Builder.CreateStore(Vec, Dst.getVectorAddr(),Dst.isVolatileQualified());
Chris Lattner41d480e2007-08-03 16:28:33 +0000537 return;
538 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000539
Nate Begemance4d7fc2008-04-18 23:10:10 +0000540 // If this is an update of extended vector elements, insert them as
541 // appropriate.
542 if (Dst.isExtVectorElt())
543 return EmitStoreThroughExtVectorComponentLValue(Src, Dst, Ty);
Lauro Ramos Venancio09af71c2008-01-22 22:36:45 +0000544
545 if (Dst.isBitfield())
546 return EmitStoreThroughBitfieldLValue(Src, Dst, Ty);
547
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +0000548 if (Dst.isPropertyRef())
549 return EmitStoreThroughPropertyRefLValue(Src, Dst, Ty);
550
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000551 assert(Dst.isKVCRef() && "Unknown LValue type");
552 return EmitStoreThroughKVCRefLValue(Src, Dst, Ty);
Chris Lattner41d480e2007-08-03 16:28:33 +0000553 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000554
Fariborz Jahanian10bec102009-02-21 00:30:43 +0000555 if (Dst.isObjCWeak() && !Dst.isNonGC()) {
Mike Stump4a3999f2009-09-09 13:00:44 +0000556 // load of a __weak object.
Fariborz Jahanian50a12702008-11-19 17:34:06 +0000557 llvm::Value *LvalueDst = Dst.getAddress();
558 llvm::Value *src = Src.getScalarVal();
Mike Stumpca5ae662009-04-14 00:57:29 +0000559 CGM.getObjCRuntime().EmitObjCWeakAssign(*this, src, LvalueDst);
Fariborz Jahanian50a12702008-11-19 17:34:06 +0000560 return;
561 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000562
Fariborz Jahanian10bec102009-02-21 00:30:43 +0000563 if (Dst.isObjCStrong() && !Dst.isNonGC()) {
Mike Stump4a3999f2009-09-09 13:00:44 +0000564 // load of a __strong object.
Fariborz Jahanian50a12702008-11-19 17:34:06 +0000565 llvm::Value *LvalueDst = Dst.getAddress();
566 llvm::Value *src = Src.getScalarVal();
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000567 if (Dst.isObjCIvar()) {
568 assert(Dst.getBaseIvarExp() && "BaseIvarExp is NULL");
569 const llvm::Type *ResultType = ConvertType(getContext().LongTy);
570 llvm::Value *RHS = EmitScalarExpr(Dst.getBaseIvarExp());
Fariborz Jahanian1f9ed582009-09-25 00:00:20 +0000571 llvm::Value *dst = RHS;
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000572 RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast");
573 llvm::Value *LHS =
574 Builder.CreatePtrToInt(LvalueDst, ResultType, "sub.ptr.lhs.cast");
575 llvm::Value *BytesBetween = Builder.CreateSub(LHS, RHS, "ivar.offset");
Fariborz Jahanian1f9ed582009-09-25 00:00:20 +0000576 CGM.getObjCRuntime().EmitObjCIvarAssign(*this, src, dst,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000577 BytesBetween);
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000578 } else if (Dst.isGlobalObjCRef())
Fariborz Jahanian32ff7ae2009-05-04 23:27:20 +0000579 CGM.getObjCRuntime().EmitObjCGlobalAssign(*this, src, LvalueDst);
580 else
581 CGM.getObjCRuntime().EmitObjCStrongCastAssign(*this, src, LvalueDst);
Fariborz Jahanian50a12702008-11-19 17:34:06 +0000582 return;
583 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000584
Chris Lattner6278e6a2007-08-11 00:04:45 +0000585 assert(Src.isScalar() && "Can't emit an agg store with this method");
Anders Carlsson83709642009-05-19 18:50:41 +0000586 EmitStoreOfScalar(Src.getScalarVal(), Dst.getAddress(),
587 Dst.isVolatileQualified(), Ty);
Chris Lattner8394d792007-06-05 20:53:16 +0000588}
589
Lauro Ramos Venancio09af71c2008-01-22 22:36:45 +0000590void CodeGenFunction::EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst,
Mike Stump4a3999f2009-09-09 13:00:44 +0000591 QualType Ty,
Daniel Dunbar9b1335e2008-11-19 09:36:46 +0000592 llvm::Value **Result) {
Daniel Dunbaread7c912008-08-06 05:08:45 +0000593 unsigned StartBit = Dst.getBitfieldStartBit();
594 unsigned BitfieldSize = Dst.getBitfieldSize();
Lauro Ramos Venancio09af71c2008-01-22 22:36:45 +0000595 llvm::Value *Ptr = Dst.getBitfieldAddr();
Lauro Ramos Venancio09af71c2008-01-22 22:36:45 +0000596
Mike Stump4a3999f2009-09-09 13:00:44 +0000597 const llvm::Type *EltTy =
Daniel Dunbaread7c912008-08-06 05:08:45 +0000598 cast<llvm::PointerType>(Ptr->getType())->getElementType();
599 unsigned EltTySize = CGM.getTargetData().getTypeSizeInBits(EltTy);
600
Mike Stump4a3999f2009-09-09 13:00:44 +0000601 // Get the new value, cast to the appropriate type and masked to exactly the
602 // size of the bit-field.
Daniel Dunbar9b1335e2008-11-19 09:36:46 +0000603 llvm::Value *SrcVal = Src.getScalarVal();
604 llvm::Value *NewVal = Builder.CreateIntCast(SrcVal, EltTy, false, "tmp");
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000605 llvm::Constant *Mask = llvm::ConstantInt::get(VMContext,
606 llvm::APInt::getLowBitsSet(EltTySize, BitfieldSize));
Daniel Dunbaread7c912008-08-06 05:08:45 +0000607 NewVal = Builder.CreateAnd(NewVal, Mask, "bf.value");
Lauro Ramos Venancio09af71c2008-01-22 22:36:45 +0000608
Daniel Dunbar9b1335e2008-11-19 09:36:46 +0000609 // Return the new value of the bit-field, if requested.
610 if (Result) {
611 // Cast back to the proper type for result.
612 const llvm::Type *SrcTy = SrcVal->getType();
613 llvm::Value *SrcTrunc = Builder.CreateIntCast(NewVal, SrcTy, false,
614 "bf.reload.val");
615
616 // Sign extend if necessary.
617 if (Dst.isBitfieldSigned()) {
618 unsigned SrcTySize = CGM.getTargetData().getTypeSizeInBits(SrcTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000619 llvm::Value *ExtraBits = llvm::ConstantInt::get(SrcTy,
Daniel Dunbar9b1335e2008-11-19 09:36:46 +0000620 SrcTySize - BitfieldSize);
Mike Stump4a3999f2009-09-09 13:00:44 +0000621 SrcTrunc = Builder.CreateAShr(Builder.CreateShl(SrcTrunc, ExtraBits),
Daniel Dunbar9b1335e2008-11-19 09:36:46 +0000622 ExtraBits, "bf.reload.sext");
623 }
624
625 *Result = SrcTrunc;
626 }
627
Mike Stump4a3999f2009-09-09 13:00:44 +0000628 // In some cases the bitfield may straddle two memory locations. Emit the low
629 // part first and check to see if the high needs to be done.
Daniel Dunbaread7c912008-08-06 05:08:45 +0000630 unsigned LowBits = std::min(BitfieldSize, EltTySize - StartBit);
631 llvm::Value *LowVal = Builder.CreateLoad(Ptr, Dst.isVolatileQualified(),
632 "bf.prev.low");
Eli Friedmanf2442dc2008-05-17 20:03:47 +0000633
Daniel Dunbaread7c912008-08-06 05:08:45 +0000634 // Compute the mask for zero-ing the low part of this bitfield.
Mike Stump4a3999f2009-09-09 13:00:44 +0000635 llvm::Constant *InvMask =
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000636 llvm::ConstantInt::get(VMContext,
637 ~llvm::APInt::getBitsSet(EltTySize, StartBit, StartBit + LowBits));
Mike Stump4a3999f2009-09-09 13:00:44 +0000638
Daniel Dunbaread7c912008-08-06 05:08:45 +0000639 // Compute the new low part as
640 // LowVal = (LowVal & InvMask) | (NewVal << StartBit),
641 // with the shift of NewVal implicitly stripping the high bits.
Mike Stump4a3999f2009-09-09 13:00:44 +0000642 llvm::Value *NewLowVal =
Chris Lattner72ecc682009-12-06 17:22:42 +0000643 Builder.CreateShl(NewVal, StartBit, "bf.value.lo");
Daniel Dunbaread7c912008-08-06 05:08:45 +0000644 LowVal = Builder.CreateAnd(LowVal, InvMask, "bf.prev.lo.cleared");
645 LowVal = Builder.CreateOr(LowVal, NewLowVal, "bf.new.lo");
Mike Stump4a3999f2009-09-09 13:00:44 +0000646
Daniel Dunbaread7c912008-08-06 05:08:45 +0000647 // Write back.
648 Builder.CreateStore(LowVal, Ptr, Dst.isVolatileQualified());
Eli Friedmanf2442dc2008-05-17 20:03:47 +0000649
Daniel Dunbaread7c912008-08-06 05:08:45 +0000650 // If the low part doesn't cover the bitfield emit a high part.
651 if (LowBits < BitfieldSize) {
652 unsigned HighBits = BitfieldSize - LowBits;
Owen Anderson41a75022009-08-13 21:57:51 +0000653 llvm::Value *HighPtr = Builder.CreateGEP(Ptr, llvm::ConstantInt::get(
Mike Stump4a3999f2009-09-09 13:00:44 +0000654 llvm::Type::getInt32Ty(VMContext), 1), "bf.ptr.hi");
655 llvm::Value *HighVal = Builder.CreateLoad(HighPtr,
Daniel Dunbaread7c912008-08-06 05:08:45 +0000656 Dst.isVolatileQualified(),
657 "bf.prev.hi");
Mike Stump4a3999f2009-09-09 13:00:44 +0000658
Daniel Dunbaread7c912008-08-06 05:08:45 +0000659 // Compute the mask for zero-ing the high part of this bitfield.
Mike Stump4a3999f2009-09-09 13:00:44 +0000660 llvm::Constant *InvMask =
661 llvm::ConstantInt::get(VMContext, ~llvm::APInt::getLowBitsSet(EltTySize,
Owen Anderson170229f2009-07-14 23:10:40 +0000662 HighBits));
Mike Stump4a3999f2009-09-09 13:00:44 +0000663
Daniel Dunbaread7c912008-08-06 05:08:45 +0000664 // Compute the new high part as
665 // HighVal = (HighVal & InvMask) | (NewVal lshr LowBits),
666 // where the high bits of NewVal have already been cleared and the
667 // shift stripping the low bits.
Mike Stump4a3999f2009-09-09 13:00:44 +0000668 llvm::Value *NewHighVal =
Chris Lattner72ecc682009-12-06 17:22:42 +0000669 Builder.CreateLShr(NewVal, LowBits, "bf.value.high");
Daniel Dunbaread7c912008-08-06 05:08:45 +0000670 HighVal = Builder.CreateAnd(HighVal, InvMask, "bf.prev.hi.cleared");
671 HighVal = Builder.CreateOr(HighVal, NewHighVal, "bf.new.hi");
Mike Stump4a3999f2009-09-09 13:00:44 +0000672
Daniel Dunbaread7c912008-08-06 05:08:45 +0000673 // Write back.
674 Builder.CreateStore(HighVal, HighPtr, Dst.isVolatileQualified());
675 }
Lauro Ramos Venancio09af71c2008-01-22 22:36:45 +0000676}
677
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +0000678void CodeGenFunction::EmitStoreThroughPropertyRefLValue(RValue Src,
679 LValue Dst,
680 QualType Ty) {
681 EmitObjCPropertySet(Dst.getPropertyRefExpr(), Src);
682}
683
Fariborz Jahanian9ac53512008-11-22 22:30:21 +0000684void CodeGenFunction::EmitStoreThroughKVCRefLValue(RValue Src,
685 LValue Dst,
686 QualType Ty) {
687 EmitObjCPropertySet(Dst.getKVCRefExpr(), Src);
688}
689
Nate Begemance4d7fc2008-04-18 23:10:10 +0000690void CodeGenFunction::EmitStoreThroughExtVectorComponentLValue(RValue Src,
691 LValue Dst,
692 QualType Ty) {
Chris Lattner41d480e2007-08-03 16:28:33 +0000693 // This access turns into a read/modify/write of the vector. Load the input
694 // value now.
Eli Friedman327944b2008-06-13 23:01:12 +0000695 llvm::Value *Vec = Builder.CreateLoad(Dst.getExtVectorAddr(),
696 Dst.isVolatileQualified(), "tmp");
Nate Begemanf322eab2008-05-09 06:41:27 +0000697 const llvm::Constant *Elts = Dst.getExtVectorElts();
Mike Stump4a3999f2009-09-09 13:00:44 +0000698
Chris Lattner4647a212007-08-31 22:49:20 +0000699 llvm::Value *SrcVal = Src.getScalarVal();
Mike Stump4a3999f2009-09-09 13:00:44 +0000700
John McCall9dd450b2009-09-21 23:43:11 +0000701 if (const VectorType *VTy = Ty->getAs<VectorType>()) {
Chris Lattner3a44aa72007-08-03 16:37:04 +0000702 unsigned NumSrcElts = VTy->getNumElements();
Nate Begemanb699c9b2009-01-18 06:42:49 +0000703 unsigned NumDstElts =
704 cast<llvm::VectorType>(Vec->getType())->getNumElements();
705 if (NumDstElts == NumSrcElts) {
Mike Stump4a3999f2009-09-09 13:00:44 +0000706 // Use shuffle vector is the src and destination are the same number of
707 // elements and restore the vector mask since it is on the side it will be
708 // stored.
Nate Begemanea12f6e2009-06-26 21:12:50 +0000709 llvm::SmallVector<llvm::Constant*, 4> Mask(NumDstElts);
Nate Begemanb699c9b2009-01-18 06:42:49 +0000710 for (unsigned i = 0; i != NumSrcElts; ++i) {
711 unsigned InIdx = getAccessedFieldNo(i, Elts);
Owen Anderson41a75022009-08-13 21:57:51 +0000712 Mask[InIdx] = llvm::ConstantInt::get(
713 llvm::Type::getInt32Ty(VMContext), i);
Nate Begemanb699c9b2009-01-18 06:42:49 +0000714 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000715
Owen Anderson3cc120a2009-07-28 21:22:35 +0000716 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
Nate Begemanb699c9b2009-01-18 06:42:49 +0000717 Vec = Builder.CreateShuffleVector(SrcVal,
Owen Anderson7ec07a52009-07-30 23:11:26 +0000718 llvm::UndefValue::get(Vec->getType()),
Nate Begemanb699c9b2009-01-18 06:42:49 +0000719 MaskV, "tmp");
Mike Stump658fe022009-07-30 22:28:39 +0000720 } else if (NumDstElts > NumSrcElts) {
Nate Begemanb699c9b2009-01-18 06:42:49 +0000721 // Extended the source vector to the same length and then shuffle it
722 // into the destination.
723 // FIXME: since we're shuffling with undef, can we just use the indices
724 // into that? This could be simpler.
725 llvm::SmallVector<llvm::Constant*, 4> ExtMask;
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000726 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
Nate Begemanb699c9b2009-01-18 06:42:49 +0000727 unsigned i;
728 for (i = 0; i != NumSrcElts; ++i)
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000729 ExtMask.push_back(llvm::ConstantInt::get(Int32Ty, i));
Nate Begemanb699c9b2009-01-18 06:42:49 +0000730 for (; i != NumDstElts; ++i)
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000731 ExtMask.push_back(llvm::UndefValue::get(Int32Ty));
Owen Anderson3cc120a2009-07-28 21:22:35 +0000732 llvm::Value *ExtMaskV = llvm::ConstantVector::get(&ExtMask[0],
Nate Begemanb699c9b2009-01-18 06:42:49 +0000733 ExtMask.size());
Mike Stump4a3999f2009-09-09 13:00:44 +0000734 llvm::Value *ExtSrcVal =
Daniel Dunbar3d926cb2009-02-17 18:31:04 +0000735 Builder.CreateShuffleVector(SrcVal,
Owen Anderson7ec07a52009-07-30 23:11:26 +0000736 llvm::UndefValue::get(SrcVal->getType()),
Daniel Dunbar3d926cb2009-02-17 18:31:04 +0000737 ExtMaskV, "tmp");
Nate Begemanb699c9b2009-01-18 06:42:49 +0000738 // build identity
739 llvm::SmallVector<llvm::Constant*, 4> Mask;
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000740 for (unsigned i = 0; i != NumDstElts; ++i)
741 Mask.push_back(llvm::ConstantInt::get(Int32Ty, i));
742
Nate Begemanb699c9b2009-01-18 06:42:49 +0000743 // modify when what gets shuffled in
744 for (unsigned i = 0; i != NumSrcElts; ++i) {
745 unsigned Idx = getAccessedFieldNo(i, Elts);
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000746 Mask[Idx] = llvm::ConstantInt::get(Int32Ty, i+NumDstElts);
Nate Begemanb699c9b2009-01-18 06:42:49 +0000747 }
Owen Anderson3cc120a2009-07-28 21:22:35 +0000748 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
Nate Begemanb699c9b2009-01-18 06:42:49 +0000749 Vec = Builder.CreateShuffleVector(Vec, ExtSrcVal, MaskV, "tmp");
Mike Stump658fe022009-07-30 22:28:39 +0000750 } else {
Nate Begemanb699c9b2009-01-18 06:42:49 +0000751 // We should never shorten the vector
752 assert(0 && "unexpected shorten vector length");
Chris Lattner3a44aa72007-08-03 16:37:04 +0000753 }
754 } else {
755 // If the Src is a scalar (not a vector) it must be updating one element.
Dan Gohman75d69da2008-05-22 00:50:06 +0000756 unsigned InIdx = getAccessedFieldNo(0, Elts);
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000757 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
758 llvm::Value *Elt = llvm::ConstantInt::get(Int32Ty, InIdx);
Chris Lattner41d480e2007-08-03 16:28:33 +0000759 Vec = Builder.CreateInsertElement(Vec, SrcVal, Elt, "tmp");
Chris Lattner41d480e2007-08-03 16:28:33 +0000760 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000761
Eli Friedman327944b2008-06-13 23:01:12 +0000762 Builder.CreateStore(Vec, Dst.getExtVectorAddr(), Dst.isVolatileQualified());
Chris Lattner41d480e2007-08-03 16:28:33 +0000763}
764
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +0000765// setObjCGCLValueClass - sets class of he lvalue for the purpose of
766// generating write-barries API. It is currently a global, ivar,
767// or neither.
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000768static void setObjCGCLValueClass(const ASTContext &Ctx, const Expr *E,
769 LValue &LV) {
Fariborz Jahanian71848a32009-09-21 23:03:37 +0000770 if (Ctx.getLangOptions().getGCMode() == LangOptions::NonGC)
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +0000771 return;
772
Fariborz Jahaniande1d3242009-09-16 23:11:23 +0000773 if (isa<ObjCIvarRefExpr>(E)) {
774 LV.SetObjCIvar(LV, true);
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000775 ObjCIvarRefExpr *Exp = cast<ObjCIvarRefExpr>(const_cast<Expr*>(E));
776 LV.setBaseIvarExp(Exp->getBase());
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +0000777 LV.SetObjCArray(LV, E->getType()->isArrayType());
Fariborz Jahaniande1d3242009-09-16 23:11:23 +0000778 return;
779 }
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000780
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +0000781 if (const DeclRefExpr *Exp = dyn_cast<DeclRefExpr>(E)) {
782 if (const VarDecl *VD = dyn_cast<VarDecl>(Exp->getDecl())) {
783 if ((VD->isBlockVarDecl() && !VD->hasLocalStorage()) ||
784 VD->isFileVarDecl())
785 LV.SetGlobalObjCRef(LV, true);
786 }
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +0000787 LV.SetObjCArray(LV, E->getType()->isArrayType());
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000788 return;
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +0000789 }
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000790
791 if (const UnaryOperator *Exp = dyn_cast<UnaryOperator>(E)) {
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +0000792 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000793 return;
794 }
795
796 if (const ParenExpr *Exp = dyn_cast<ParenExpr>(E)) {
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +0000797 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
Fariborz Jahaniane01e4342009-09-30 17:10:29 +0000798 if (LV.isObjCIvar()) {
799 // If cast is to a structure pointer, follow gcc's behavior and make it
800 // a non-ivar write-barrier.
801 QualType ExpTy = E->getType();
802 if (ExpTy->isPointerType())
803 ExpTy = ExpTy->getAs<PointerType>()->getPointeeType();
804 if (ExpTy->isRecordType())
805 LV.SetObjCIvar(LV, false);
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000806 }
807 return;
Fariborz Jahaniane01e4342009-09-30 17:10:29 +0000808 }
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000809 if (const ImplicitCastExpr *Exp = dyn_cast<ImplicitCastExpr>(E)) {
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +0000810 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000811 return;
812 }
813
814 if (const CStyleCastExpr *Exp = dyn_cast<CStyleCastExpr>(E)) {
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +0000815 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000816 return;
817 }
818
819 if (const ArraySubscriptExpr *Exp = dyn_cast<ArraySubscriptExpr>(E)) {
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +0000820 setObjCGCLValueClass(Ctx, Exp->getBase(), LV);
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +0000821 if (LV.isObjCIvar() && !LV.isObjCArray())
Fariborz Jahanian2e32ddc2009-09-18 00:04:00 +0000822 // Using array syntax to assigning to what an ivar points to is not
823 // same as assigning to the ivar itself. {id *Names;} Names[i] = 0;
824 LV.SetObjCIvar(LV, false);
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +0000825 else if (LV.isGlobalObjCRef() && !LV.isObjCArray())
826 // Using array syntax to assigning to what global points to is not
827 // same as assigning to the global itself. {id *G;} G[i] = 0;
828 LV.SetGlobalObjCRef(LV, false);
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000829 return;
Fariborz Jahanian2e32ddc2009-09-18 00:04:00 +0000830 }
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000831
832 if (const MemberExpr *Exp = dyn_cast<MemberExpr>(E)) {
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +0000833 setObjCGCLValueClass(Ctx, Exp->getBase(), LV);
Fariborz Jahanian2e32ddc2009-09-18 00:04:00 +0000834 // We don't know if member is an 'ivar', but this flag is looked at
835 // only in the context of LV.isObjCIvar().
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +0000836 LV.SetObjCArray(LV, E->getType()->isArrayType());
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000837 return;
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +0000838 }
839}
840
Anders Carlssonea4c30b2009-11-07 23:06:58 +0000841static LValue EmitGlobalVarDeclLValue(CodeGenFunction &CGF,
842 const Expr *E, const VarDecl *VD) {
Daniel Dunbar7e215ea2009-11-08 09:46:46 +0000843 assert((VD->hasExternalStorage() || VD->isFileVarDecl()) &&
Anders Carlssonea4c30b2009-11-07 23:06:58 +0000844 "Var decl must have external storage or be a file var decl!");
845
846 llvm::Value *V = CGF.CGM.GetAddrOfGlobalVar(VD);
847 if (VD->getType()->isReferenceType())
848 V = CGF.Builder.CreateLoad(V, "tmp");
849 LValue LV = LValue::MakeAddr(V, CGF.MakeQualifiers(E->getType()));
850 setObjCGCLValueClass(CGF.getContext(), E, LV);
851 return LV;
852}
853
Eli Friedmand15eb34d2009-11-26 06:08:14 +0000854static LValue EmitFunctionDeclLValue(CodeGenFunction &CGF,
855 const Expr *E, const FunctionDecl *FD) {
856 llvm::Value* V = CGF.CGM.GetAddrOfFunction(FD);
857 if (!FD->hasPrototype()) {
858 if (const FunctionProtoType *Proto =
859 FD->getType()->getAs<FunctionProtoType>()) {
860 // Ugly case: for a K&R-style definition, the type of the definition
861 // isn't the same as the type of a use. Correct for this with a
862 // bitcast.
863 QualType NoProtoType =
864 CGF.getContext().getFunctionNoProtoType(Proto->getResultType());
865 NoProtoType = CGF.getContext().getPointerType(NoProtoType);
866 V = CGF.Builder.CreateBitCast(V, CGF.ConvertType(NoProtoType), "tmp");
867 }
868 }
869 return LValue::MakeAddr(V, CGF.MakeQualifiers(E->getType()));
870}
871
Chris Lattnerd7f58862007-06-02 05:24:33 +0000872LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) {
Anders Carlsson2ff6395d2009-11-07 22:53:10 +0000873 const NamedDecl *ND = E->getDecl();
Mike Stump4a3999f2009-09-09 13:00:44 +0000874
Anders Carlsson2ff6395d2009-11-07 22:53:10 +0000875 if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
Anders Carlsson2ff6395d2009-11-07 22:53:10 +0000876
877 // Check if this is a global variable.
Anders Carlssonea4c30b2009-11-07 23:06:58 +0000878 if (VD->hasExternalStorage() || VD->isFileVarDecl())
879 return EmitGlobalVarDeclLValue(*this, E, VD);
Anders Carlsson6eee9722009-11-07 22:46:42 +0000880
881 bool NonGCable = VD->hasLocalStorage() && !VD->hasAttr<BlocksAttr>();
882
883 llvm::Value *V = LocalDeclMap[VD];
884 assert(V && "DeclRefExpr not entered in LocalDeclMap?");
885
886 Qualifiers Quals = MakeQualifiers(E->getType());
887 // local variables do not get their gc attribute set.
888 // local static?
889 if (NonGCable) Quals.removeObjCGCAttr();
890
891 if (VD->hasAttr<BlocksAttr>()) {
892 V = Builder.CreateStructGEP(V, 1, "forwarding");
Daniel Dunbarc76493a2009-11-29 21:23:36 +0000893 V = Builder.CreateLoad(V);
Anders Carlsson6eee9722009-11-07 22:46:42 +0000894 V = Builder.CreateStructGEP(V, getByRefValueLLVMField(VD),
895 VD->getNameAsString());
896 }
897 if (VD->getType()->isReferenceType())
898 V = Builder.CreateLoad(V, "tmp");
Anders Carlssonea4c30b2009-11-07 23:06:58 +0000899 LValue LV = LValue::MakeAddr(V, Quals);
Anders Carlsson6eee9722009-11-07 22:46:42 +0000900 LValue::SetObjCNonGC(LV, NonGCable);
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +0000901 setObjCGCLValueClass(getContext(), E, LV);
Fariborz Jahanian003e8302008-11-20 00:15:42 +0000902 return LV;
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000903 }
904
Eli Friedmand15eb34d2009-11-26 06:08:14 +0000905 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
906 return EmitFunctionDeclLValue(*this, E, FD);
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000907
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000908 if (E->getQualifier()) {
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000909 // FIXME: the qualifier check does not seem sufficient here
Anders Carlsson2ff6395d2009-11-07 22:53:10 +0000910 return EmitPointerToDataMemberLValue(cast<FieldDecl>(ND));
Chris Lattner5696e7b2008-06-17 18:05:57 +0000911 }
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000912
Anders Carlsson2ff6395d2009-11-07 22:53:10 +0000913 assert(false && "Unhandled DeclRefExpr");
914
915 // an invalid LValue, but the assert will
916 // ensure that this point is never reached.
Chris Lattner793d10c2007-09-16 19:23:47 +0000917 return LValue();
Chris Lattnerd7f58862007-06-02 05:24:33 +0000918}
Chris Lattnere47e4402007-06-01 18:02:12 +0000919
Mike Stump1db7d042009-02-28 09:07:16 +0000920LValue CodeGenFunction::EmitBlockDeclRefLValue(const BlockDeclRefExpr *E) {
John McCall8ccfcb52009-09-24 19:53:00 +0000921 return LValue::MakeAddr(GetAddrOfBlockDecl(E), MakeQualifiers(E->getType()));
Mike Stump1db7d042009-02-28 09:07:16 +0000922}
923
Chris Lattner8394d792007-06-05 20:53:16 +0000924LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) {
925 // __extension__ doesn't affect lvalue-ness.
926 if (E->getOpcode() == UnaryOperator::Extension)
927 return EmitLValue(E->getSubExpr());
Mike Stump4a3999f2009-09-09 13:00:44 +0000928
Chris Lattner0f398c42008-07-26 22:37:01 +0000929 QualType ExprTy = getContext().getCanonicalType(E->getSubExpr()->getType());
Chris Lattner595db862007-10-30 22:53:42 +0000930 switch (E->getOpcode()) {
931 default: assert(0 && "Unknown unary operator lvalue!");
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000932 case UnaryOperator::Deref: {
933 QualType T = E->getSubExpr()->getType()->getPointeeType();
934 assert(!T.isNull() && "CodeGenFunction::EmitUnaryOpLValue: Illegal type");
Mike Stump4a3999f2009-09-09 13:00:44 +0000935
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000936 Qualifiers Quals = MakeQualifiers(T);
937 Quals.setAddressSpace(ExprTy.getAddressSpace());
John McCall8ccfcb52009-09-24 19:53:00 +0000938
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000939 LValue LV = LValue::MakeAddr(EmitScalarExpr(E->getSubExpr()), Quals);
940 // We should not generate __weak write barrier on indirect reference
941 // of a pointer to object; as in void foo (__weak id *param); *param = 0;
942 // But, we continue to generate __strong write barrier on indirect write
943 // into a pointer to object.
944 if (getContext().getLangOptions().ObjC1 &&
945 getContext().getLangOptions().getGCMode() != LangOptions::NonGC &&
946 LV.isObjCWeak())
947 LValue::SetObjCNonGC(LV, !E->isOBJCGCCandidate(getContext()));
948 return LV;
949 }
Chris Lattner595db862007-10-30 22:53:42 +0000950 case UnaryOperator::Real:
Eli Friedmana72bf0f2009-11-09 04:20:47 +0000951 case UnaryOperator::Imag: {
Chris Lattner595db862007-10-30 22:53:42 +0000952 LValue LV = EmitLValue(E->getSubExpr());
Chris Lattner3e593cd2008-03-19 05:19:41 +0000953 unsigned Idx = E->getOpcode() == UnaryOperator::Imag;
954 return LValue::MakeAddr(Builder.CreateStructGEP(LV.getAddress(),
Chris Lattner574dee62008-07-26 22:17:49 +0000955 Idx, "idx"),
John McCall8ccfcb52009-09-24 19:53:00 +0000956 MakeQualifiers(ExprTy));
Chris Lattner595db862007-10-30 22:53:42 +0000957 }
Eli Friedmana72bf0f2009-11-09 04:20:47 +0000958 case UnaryOperator::PreInc:
959 case UnaryOperator::PreDec:
960 return EmitUnsupportedLValue(E, "pre-inc/dec expression");
961 }
Chris Lattner8394d792007-06-05 20:53:16 +0000962}
963
Chris Lattner4347e3692007-06-06 04:54:52 +0000964LValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) {
John McCall8ccfcb52009-09-24 19:53:00 +0000965 return LValue::MakeAddr(CGM.GetAddrOfConstantStringFromLiteral(E),
966 Qualifiers());
Chris Lattner4347e3692007-06-06 04:54:52 +0000967}
968
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +0000969LValue CodeGenFunction::EmitObjCEncodeExprLValue(const ObjCEncodeExpr *E) {
John McCall8ccfcb52009-09-24 19:53:00 +0000970 return LValue::MakeAddr(CGM.GetAddrOfConstantStringFromObjCEncode(E),
971 Qualifiers());
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +0000972}
973
974
Daniel Dunbarb3517472008-10-17 21:58:32 +0000975LValue CodeGenFunction::EmitPredefinedFunctionName(unsigned Type) {
Anders Carlsson625bfc82007-07-21 05:21:51 +0000976 std::string GlobalVarName;
Daniel Dunbarb3517472008-10-17 21:58:32 +0000977
978 switch (Type) {
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000979 default: assert(0 && "Invalid type");
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +0000980 case PredefinedExpr::Func:
981 GlobalVarName = "__func__.";
982 break;
983 case PredefinedExpr::Function:
984 GlobalVarName = "__FUNCTION__.";
985 break;
986 case PredefinedExpr::PrettyFunction:
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +0000987 GlobalVarName = "__PRETTY_FUNCTION__.";
988 break;
Anders Carlsson625bfc82007-07-21 05:21:51 +0000989 }
Daniel Dunbarb3517472008-10-17 21:58:32 +0000990
Daniel Dunbar0482cfd2009-09-12 23:06:21 +0000991 llvm::StringRef FnName = CurFn->getName();
992 if (FnName.startswith("\01"))
993 FnName = FnName.substr(1);
994 GlobalVarName += FnName;
995
Anders Carlsson2fb08242009-09-08 18:24:21 +0000996 std::string FunctionName =
Mike Stump11289f42009-09-09 15:08:12 +0000997 PredefinedExpr::ComputeName(getContext(), (PredefinedExpr::IdentType)Type,
Anders Carlsson2fb08242009-09-08 18:24:21 +0000998 CurCodeDecl);
Daniel Dunbarb3517472008-10-17 21:58:32 +0000999
Mike Stump4a3999f2009-09-09 13:00:44 +00001000 llvm::Constant *C =
Daniel Dunbarb3517472008-10-17 21:58:32 +00001001 CGM.GetAddrOfConstantCString(FunctionName, GlobalVarName.c_str());
John McCall8ccfcb52009-09-24 19:53:00 +00001002 return LValue::MakeAddr(C, Qualifiers());
Daniel Dunbarb3517472008-10-17 21:58:32 +00001003}
1004
Mike Stump4a3999f2009-09-09 13:00:44 +00001005LValue CodeGenFunction::EmitPredefinedLValue(const PredefinedExpr *E) {
Daniel Dunbarb3517472008-10-17 21:58:32 +00001006 switch (E->getIdentType()) {
1007 default:
1008 return EmitUnsupportedLValue(E, "predefined expression");
1009 case PredefinedExpr::Func:
1010 case PredefinedExpr::Function:
1011 case PredefinedExpr::PrettyFunction:
1012 return EmitPredefinedFunctionName(E->getIdentType());
1013 }
Anders Carlsson625bfc82007-07-21 05:21:51 +00001014}
1015
Mike Stumpcf16d2c2009-12-15 01:22:35 +00001016llvm::BasicBlock *CodeGenFunction::getTrapBB() {
Mike Stump9a4e0122009-12-15 00:59:40 +00001017 const CodeGenOptions &GCO = CGM.getCodeGenOpts();
1018
1019 // If we are not optimzing, don't collapse all calls to trap in the function
1020 // to the same call, that way, in the debugger they can see which operation
1021 // did in fact fail. If we are optimizing, we collpase all call to trap down
1022 // to just one per function to save on codesize.
1023 if (GCO.OptimizationLevel
1024 && TrapBB)
Mike Stumpe8c3b3e2009-12-15 00:35:12 +00001025 return TrapBB;
Mike Stumpd9546382009-12-12 01:27:46 +00001026
1027 llvm::BasicBlock *Cont = 0;
1028 if (HaveInsertPoint()) {
1029 Cont = createBasicBlock("cont");
1030 EmitBranch(Cont);
1031 }
Mike Stumpe8c3b3e2009-12-15 00:35:12 +00001032 TrapBB = createBasicBlock("trap");
1033 EmitBlock(TrapBB);
1034
1035 llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::trap, 0, 0);
1036 llvm::CallInst *TrapCall = Builder.CreateCall(F);
1037 TrapCall->setDoesNotReturn();
1038 TrapCall->setDoesNotThrow();
Mike Stumpd9546382009-12-12 01:27:46 +00001039 Builder.CreateUnreachable();
1040
1041 if (Cont)
1042 EmitBlock(Cont);
Mike Stumpe8c3b3e2009-12-15 00:35:12 +00001043 return TrapBB;
Mike Stumpd9546382009-12-12 01:27:46 +00001044}
1045
Chris Lattnerd9d2fb12007-06-08 23:31:14 +00001046LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) {
Ted Kremenekc81614d2007-08-20 16:18:38 +00001047 // The index must always be an integer, which is not an aggregate. Emit it.
Chris Lattner2da04b32007-08-24 05:35:26 +00001048 llvm::Value *Idx = EmitScalarExpr(E->getIdx());
Eli Friedman07bbeca2009-06-06 19:09:26 +00001049 QualType IdxTy = E->getIdx()->getType();
1050 bool IdxSigned = IdxTy->isSignedIntegerType();
1051
Chris Lattner08c4b9f2007-07-10 21:17:59 +00001052 // If the base is a vector type, then we are forming a vector element lvalue
1053 // with this subscript.
Eli Friedman327944b2008-06-13 23:01:12 +00001054 if (E->getBase()->getType()->isVectorType()) {
Chris Lattner08c4b9f2007-07-10 21:17:59 +00001055 // Emit the vector as an lvalue to get its address.
Eli Friedman327944b2008-06-13 23:01:12 +00001056 LValue LHS = EmitLValue(E->getBase());
Ted Kremenekc81614d2007-08-20 16:18:38 +00001057 assert(LHS.isSimple() && "Can only subscript lvalue vectors here!");
Mike Stump4a3999f2009-09-09 13:00:44 +00001058 Idx = Builder.CreateIntCast(Idx,
Owen Anderson41a75022009-08-13 21:57:51 +00001059 llvm::Type::getInt32Ty(VMContext), IdxSigned, "vidx");
Eli Friedman327944b2008-06-13 23:01:12 +00001060 return LValue::MakeVectorElt(LHS.getAddress(), Idx,
John McCall8ccfcb52009-09-24 19:53:00 +00001061 E->getBase()->getType().getCVRQualifiers());
Chris Lattner08c4b9f2007-07-10 21:17:59 +00001062 }
Mike Stump4a3999f2009-09-09 13:00:44 +00001063
Ted Kremenekc81614d2007-08-20 16:18:38 +00001064 // The base must be a pointer, which is not an aggregate. Emit it.
Chris Lattner2da04b32007-08-24 05:35:26 +00001065 llvm::Value *Base = EmitScalarExpr(E->getBase());
Mike Stump4a3999f2009-09-09 13:00:44 +00001066
Ted Kremenekc81614d2007-08-20 16:18:38 +00001067 // Extend or truncate the index type to 32 or 64-bits.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001068 unsigned IdxBitwidth = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
Sanjiv Gupta47425152009-04-24 02:40:57 +00001069 if (IdxBitwidth != LLVMPointerWidth)
Owen Anderson41a75022009-08-13 21:57:51 +00001070 Idx = Builder.CreateIntCast(Idx,
1071 llvm::IntegerType::get(VMContext, LLVMPointerWidth),
Chris Lattnerd9d2fb12007-06-08 23:31:14 +00001072 IdxSigned, "idxprom");
1073
Mike Stumpd9546382009-12-12 01:27:46 +00001074 if (CatchUndefined) {
1075 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E->getBase())) {
1076 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr())) {
1077 if (ICE->getCastKind() == CastExpr::CK_ArrayToPointerDecay) {
1078 if (const ConstantArrayType *CAT
1079 = getContext().getAsConstantArrayType(DRE->getType())) {
1080 llvm::APInt Size = CAT->getSize();
1081 llvm::BasicBlock *Cont = createBasicBlock("cont");
Mike Stump590d18f2009-12-14 22:14:31 +00001082 Builder.CreateCondBr(Builder.CreateICmpULE(Idx,
Mike Stumpd9546382009-12-12 01:27:46 +00001083 llvm::ConstantInt::get(Idx->getType(), Size)),
Mike Stumpe8c3b3e2009-12-15 00:35:12 +00001084 Cont, getTrapBB());
Mike Stumpf8858af2009-12-14 20:52:00 +00001085 EmitBlock(Cont);
Mike Stumpd9546382009-12-12 01:27:46 +00001086 }
1087 }
1088 }
1089 }
1090 }
1091
Mike Stump4a3999f2009-09-09 13:00:44 +00001092 // We know that the pointer points to a type of the correct size, unless the
1093 // size is a VLA or Objective-C interface.
Daniel Dunbaref2ffbc2009-04-25 05:08:32 +00001094 llvm::Value *Address = 0;
Mike Stump4a3999f2009-09-09 13:00:44 +00001095 if (const VariableArrayType *VAT =
Anders Carlsson3d312f82008-12-21 00:11:23 +00001096 getContext().getAsVariableArrayType(E->getType())) {
Chris Lattner19efdd62009-08-14 23:43:22 +00001097 llvm::Value *VLASize = GetVLASize(VAT);
Mike Stump4a3999f2009-09-09 13:00:44 +00001098
Anders Carlsson3d312f82008-12-21 00:11:23 +00001099 Idx = Builder.CreateMul(Idx, VLASize);
Mike Stump4a3999f2009-09-09 13:00:44 +00001100
Anders Carlssone0808df2008-12-21 03:44:36 +00001101 QualType BaseType = getContext().getBaseElementType(VAT);
Mike Stump4a3999f2009-09-09 13:00:44 +00001102
Anders Carlsson3d312f82008-12-21 00:11:23 +00001103 uint64_t BaseTypeSize = getContext().getTypeSize(BaseType) / 8;
1104 Idx = Builder.CreateUDiv(Idx,
Mike Stump4a3999f2009-09-09 13:00:44 +00001105 llvm::ConstantInt::get(Idx->getType(),
Anders Carlsson3d312f82008-12-21 00:11:23 +00001106 BaseTypeSize));
Dan Gohman43b44842009-08-12 00:33:55 +00001107 Address = Builder.CreateInBoundsGEP(Base, Idx, "arrayidx");
Mike Stump4a3999f2009-09-09 13:00:44 +00001108 } else if (const ObjCInterfaceType *OIT =
Daniel Dunbaref2ffbc2009-04-25 05:08:32 +00001109 dyn_cast<ObjCInterfaceType>(E->getType())) {
Mike Stump4a3999f2009-09-09 13:00:44 +00001110 llvm::Value *InterfaceSize =
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001111 llvm::ConstantInt::get(Idx->getType(),
Daniel Dunbaref2ffbc2009-04-25 05:08:32 +00001112 getContext().getTypeSize(OIT) / 8);
Mike Stump4a3999f2009-09-09 13:00:44 +00001113
Daniel Dunbaref2ffbc2009-04-25 05:08:32 +00001114 Idx = Builder.CreateMul(Idx, InterfaceSize);
1115
Benjamin Kramerabd5b902009-10-13 10:07:13 +00001116 const llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(VMContext);
Dan Gohman43b44842009-08-12 00:33:55 +00001117 Address = Builder.CreateGEP(Builder.CreateBitCast(Base, i8PTy),
Daniel Dunbaref2ffbc2009-04-25 05:08:32 +00001118 Idx, "arrayidx");
1119 Address = Builder.CreateBitCast(Address, Base->getType());
1120 } else {
Dan Gohman43b44842009-08-12 00:33:55 +00001121 Address = Builder.CreateInBoundsGEP(Base, Idx, "arrayidx");
Anders Carlsson3d312f82008-12-21 00:11:23 +00001122 }
Mike Stump4a3999f2009-09-09 13:00:44 +00001123
Steve Naroff7cae42b2009-07-10 23:34:53 +00001124 QualType T = E->getBase()->getType()->getPointeeType();
Mike Stump4a3999f2009-09-09 13:00:44 +00001125 assert(!T.isNull() &&
Steve Naroff7cae42b2009-07-10 23:34:53 +00001126 "CodeGenFunction::EmitArraySubscriptExpr(): Illegal base type");
Mike Stump4a3999f2009-09-09 13:00:44 +00001127
John McCall8ccfcb52009-09-24 19:53:00 +00001128 Qualifiers Quals = MakeQualifiers(T);
1129 Quals.setAddressSpace(E->getBase()->getType().getAddressSpace());
1130
1131 LValue LV = LValue::MakeAddr(Address, Quals);
Fariborz Jahaniana9fecf32009-02-21 23:37:19 +00001132 if (getContext().getLangOptions().ObjC1 &&
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +00001133 getContext().getLangOptions().getGCMode() != LangOptions::NonGC) {
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001134 LValue::SetObjCNonGC(LV, !E->isOBJCGCCandidate(getContext()));
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +00001135 setObjCGCLValueClass(getContext(), E, LV);
1136 }
Fariborz Jahaniana9fecf32009-02-21 23:37:19 +00001137 return LV;
Chris Lattnerd9d2fb12007-06-08 23:31:14 +00001138}
1139
Mike Stump4a3999f2009-09-09 13:00:44 +00001140static
Owen Anderson170229f2009-07-14 23:10:40 +00001141llvm::Constant *GenerateConstantVector(llvm::LLVMContext &VMContext,
1142 llvm::SmallVector<unsigned, 4> &Elts) {
Nate Begemand3862152008-05-13 21:03:02 +00001143 llvm::SmallVector<llvm::Constant *, 4> CElts;
Mike Stump4a3999f2009-09-09 13:00:44 +00001144
Nate Begemand3862152008-05-13 21:03:02 +00001145 for (unsigned i = 0, e = Elts.size(); i != e; ++i)
Owen Anderson41a75022009-08-13 21:57:51 +00001146 CElts.push_back(llvm::ConstantInt::get(
1147 llvm::Type::getInt32Ty(VMContext), Elts[i]));
Nate Begemand3862152008-05-13 21:03:02 +00001148
Owen Anderson3cc120a2009-07-28 21:22:35 +00001149 return llvm::ConstantVector::get(&CElts[0], CElts.size());
Nate Begemand3862152008-05-13 21:03:02 +00001150}
1151
Chris Lattner9e751ca2007-08-02 23:37:31 +00001152LValue CodeGenFunction::
Nate Begemance4d7fc2008-04-18 23:10:10 +00001153EmitExtVectorElementExpr(const ExtVectorElementExpr *E) {
Chris Lattner9e751ca2007-08-02 23:37:31 +00001154 // Emit the base vector as an l-value.
Chris Lattner6c7ce102009-02-16 21:11:58 +00001155 LValue Base;
1156
1157 // ExtVectorElementExpr's base can either be a vector or pointer to vector.
Chris Lattnerb8211f62009-02-16 22:14:05 +00001158 if (!E->isArrow()) {
Chris Lattner6c7ce102009-02-16 21:11:58 +00001159 assert(E->getBase()->getType()->isVectorType());
1160 Base = EmitLValue(E->getBase());
Chris Lattnerb8211f62009-02-16 22:14:05 +00001161 } else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001162 const PointerType *PT = E->getBase()->getType()->getAs<PointerType>();
Chris Lattnerb8211f62009-02-16 22:14:05 +00001163 llvm::Value *Ptr = EmitScalarExpr(E->getBase());
John McCall8ccfcb52009-09-24 19:53:00 +00001164 Qualifiers Quals = MakeQualifiers(PT->getPointeeType());
1165 Quals.removeObjCGCAttr();
1166 Base = LValue::MakeAddr(Ptr, Quals);
Chris Lattner6c7ce102009-02-16 21:11:58 +00001167 }
Chris Lattner9e751ca2007-08-02 23:37:31 +00001168
Nate Begemand3862152008-05-13 21:03:02 +00001169 // Encode the element access list into a vector of unsigned indices.
1170 llvm::SmallVector<unsigned, 4> Indices;
1171 E->getEncodedElementAccess(Indices);
1172
1173 if (Base.isSimple()) {
Owen Anderson170229f2009-07-14 23:10:40 +00001174 llvm::Constant *CV = GenerateConstantVector(VMContext, Indices);
Eli Friedman327944b2008-06-13 23:01:12 +00001175 return LValue::MakeExtVectorElt(Base.getAddress(), CV,
John McCall8ccfcb52009-09-24 19:53:00 +00001176 Base.getVRQualifiers());
Nate Begemand3862152008-05-13 21:03:02 +00001177 }
1178 assert(Base.isExtVectorElt() && "Can only subscript lvalue vec elts here!");
1179
1180 llvm::Constant *BaseElts = Base.getExtVectorElts();
1181 llvm::SmallVector<llvm::Constant *, 4> CElts;
1182
Chris Lattner5e71d432009-10-28 05:12:07 +00001183 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
Nate Begemand3862152008-05-13 21:03:02 +00001184 for (unsigned i = 0, e = Indices.size(); i != e; ++i) {
1185 if (isa<llvm::ConstantAggregateZero>(BaseElts))
Chris Lattner5e71d432009-10-28 05:12:07 +00001186 CElts.push_back(llvm::ConstantInt::get(Int32Ty, 0));
Nate Begemand3862152008-05-13 21:03:02 +00001187 else
Chris Lattner5e71d432009-10-28 05:12:07 +00001188 CElts.push_back(cast<llvm::Constant>(BaseElts->getOperand(Indices[i])));
Nate Begemand3862152008-05-13 21:03:02 +00001189 }
Owen Anderson3cc120a2009-07-28 21:22:35 +00001190 llvm::Constant *CV = llvm::ConstantVector::get(&CElts[0], CElts.size());
Eli Friedman327944b2008-06-13 23:01:12 +00001191 return LValue::MakeExtVectorElt(Base.getExtVectorAddr(), CV,
John McCall8ccfcb52009-09-24 19:53:00 +00001192 Base.getVRQualifiers());
Chris Lattner9e751ca2007-08-02 23:37:31 +00001193}
1194
Devang Patel30efa2e2007-10-23 20:28:39 +00001195LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) {
Devang Patelb37b12d2007-12-11 21:33:16 +00001196 bool isUnion = false;
Fariborz Jahanian10bec102009-02-21 00:30:43 +00001197 bool isNonGC = false;
Devang Pateld68df202007-10-24 22:26:28 +00001198 Expr *BaseExpr = E->getBase();
Devang Pateld68df202007-10-24 22:26:28 +00001199 llvm::Value *BaseValue = NULL;
John McCall8ccfcb52009-09-24 19:53:00 +00001200 Qualifiers BaseQuals;
Eli Friedman327944b2008-06-13 23:01:12 +00001201
Chris Lattner4e4186b2007-12-02 18:52:07 +00001202 // If this is s.x, emit s as an lvalue. If it is s->x, emit s as a scalar.
Devang Patelb37b12d2007-12-11 21:33:16 +00001203 if (E->isArrow()) {
Devang Patel7718d7a2007-10-26 18:15:21 +00001204 BaseValue = EmitScalarExpr(BaseExpr);
Mike Stump4a3999f2009-09-09 13:00:44 +00001205 const PointerType *PTy =
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001206 BaseExpr->getType()->getAs<PointerType>();
Devang Patelb37b12d2007-12-11 21:33:16 +00001207 if (PTy->getPointeeType()->isUnionType())
1208 isUnion = true;
John McCall8ccfcb52009-09-24 19:53:00 +00001209 BaseQuals = PTy->getPointeeType().getQualifiers();
Fariborz Jahanian1a504772009-09-01 17:02:21 +00001210 } else if (isa<ObjCPropertyRefExpr>(BaseExpr->IgnoreParens()) ||
1211 isa<ObjCImplicitSetterGetterRefExpr>(
1212 BaseExpr->IgnoreParens())) {
Fariborz Jahanian30e78642009-01-12 23:27:26 +00001213 RValue RV = EmitObjCPropertyGet(BaseExpr);
1214 BaseValue = RV.getAggregateAddr();
1215 if (BaseExpr->getType()->isUnionType())
1216 isUnion = true;
John McCall8ccfcb52009-09-24 19:53:00 +00001217 BaseQuals = BaseExpr->getType().getQualifiers();
Chris Lattnere084c012009-02-16 22:25:49 +00001218 } else {
Chris Lattner4e4186b2007-12-02 18:52:07 +00001219 LValue BaseLV = EmitLValue(BaseExpr);
Fariborz Jahanian10bec102009-02-21 00:30:43 +00001220 if (BaseLV.isNonGC())
1221 isNonGC = true;
Chris Lattner4e4186b2007-12-02 18:52:07 +00001222 // FIXME: this isn't right for bitfields.
1223 BaseValue = BaseLV.getAddress();
Fariborz Jahanian82e28742009-07-29 00:44:13 +00001224 QualType BaseTy = BaseExpr->getType();
1225 if (BaseTy->isUnionType())
Devang Patelb37b12d2007-12-11 21:33:16 +00001226 isUnion = true;
John McCall8ccfcb52009-09-24 19:53:00 +00001227 BaseQuals = BaseTy.getQualifiers();
Chris Lattner4e4186b2007-12-02 18:52:07 +00001228 }
Devang Patel30efa2e2007-10-23 20:28:39 +00001229
Anders Carlssonea4c30b2009-11-07 23:06:58 +00001230 NamedDecl *ND = E->getMemberDecl();
1231 if (FieldDecl *Field = dyn_cast<FieldDecl>(ND)) {
1232 LValue LV = EmitLValueForField(BaseValue, Field, isUnion,
1233 BaseQuals.getCVRQualifiers());
1234 LValue::SetObjCNonGC(LV, isNonGC);
1235 setObjCGCLValueClass(getContext(), E, LV);
1236 return LV;
1237 }
1238
Anders Carlsson5bbdc9f2009-11-07 23:16:50 +00001239 if (VarDecl *VD = dyn_cast<VarDecl>(ND))
1240 return EmitGlobalVarDeclLValue(*this, E, VD);
Eli Friedmand15eb34d2009-11-26 06:08:14 +00001241
1242 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
1243 return EmitFunctionDeclLValue(*this, E, FD);
1244
Anders Carlssonea4c30b2009-11-07 23:06:58 +00001245 assert(false && "Unhandled member declaration!");
1246 return LValue();
Eli Friedmana62f3e12008-02-09 08:50:58 +00001247}
Devang Patel30efa2e2007-10-23 20:28:39 +00001248
Fariborz Jahanianb517e902008-12-15 20:35:07 +00001249LValue CodeGenFunction::EmitLValueForBitfield(llvm::Value* BaseValue,
Anders Carlssoncfd30122009-11-17 03:57:07 +00001250 const FieldDecl* Field,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001251 unsigned CVRQualifiers) {
Anders Carlsson8af896c2009-07-23 17:01:21 +00001252 CodeGenTypes::BitFieldInfo Info = CGM.getTypes().getBitFieldInfo(Field);
1253
Mike Stump18bb9282009-05-16 07:57:57 +00001254 // FIXME: CodeGenTypes should expose a method to get the appropriate type for
1255 // FieldTy (the appropriate type is ABI-dependent).
Mike Stump4a3999f2009-09-09 13:00:44 +00001256 const llvm::Type *FieldTy =
Daniel Dunbar3d926cb2009-02-17 18:31:04 +00001257 CGM.getTypes().ConvertTypeForMem(Field->getType());
Fariborz Jahanianb517e902008-12-15 20:35:07 +00001258 const llvm::PointerType *BaseTy =
1259 cast<llvm::PointerType>(BaseValue->getType());
1260 unsigned AS = BaseTy->getAddressSpace();
1261 BaseValue = Builder.CreateBitCast(BaseValue,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001262 llvm::PointerType::get(FieldTy, AS),
Fariborz Jahanianb517e902008-12-15 20:35:07 +00001263 "tmp");
Mike Stump4a3999f2009-09-09 13:00:44 +00001264
1265 llvm::Value *Idx =
Owen Anderson41a75022009-08-13 21:57:51 +00001266 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), Info.FieldNo);
Anders Carlsson8af896c2009-07-23 17:01:21 +00001267 llvm::Value *V = Builder.CreateGEP(BaseValue, Idx, "tmp");
Mike Stump4a3999f2009-09-09 13:00:44 +00001268
Anders Carlsson8af896c2009-07-23 17:01:21 +00001269 return LValue::MakeBitfield(V, Info.Start, Info.Size,
Fariborz Jahanianb517e902008-12-15 20:35:07 +00001270 Field->getType()->isSignedIntegerType(),
1271 Field->getType().getCVRQualifiers()|CVRQualifiers);
1272}
1273
Eli Friedmana62f3e12008-02-09 08:50:58 +00001274LValue CodeGenFunction::EmitLValueForField(llvm::Value* BaseValue,
Anders Carlssoncfd30122009-11-17 03:57:07 +00001275 const FieldDecl* Field,
Eli Friedman327944b2008-06-13 23:01:12 +00001276 bool isUnion,
Mike Stump11289f42009-09-09 15:08:12 +00001277 unsigned CVRQualifiers) {
Fariborz Jahanianb517e902008-12-15 20:35:07 +00001278 if (Field->isBitField())
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001279 return EmitLValueForBitfield(BaseValue, Field, CVRQualifiers);
Mike Stump4a3999f2009-09-09 13:00:44 +00001280
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001281 unsigned idx = CGM.getTypes().getLLVMFieldNo(Field);
Fariborz Jahanianb517e902008-12-15 20:35:07 +00001282 llvm::Value *V = Builder.CreateStructGEP(BaseValue, idx, "tmp");
Eli Friedman133e8042008-05-29 11:33:25 +00001283
Devang Pateled93c3c2007-10-26 19:42:18 +00001284 // Match union field type.
Lauro Ramos Venancio9eff02d2008-02-07 19:29:53 +00001285 if (isUnion) {
Mike Stump4a3999f2009-09-09 13:00:44 +00001286 const llvm::Type *FieldTy =
Eli Friedman327944b2008-06-13 23:01:12 +00001287 CGM.getTypes().ConvertTypeForMem(Field->getType());
Mike Stump4a3999f2009-09-09 13:00:44 +00001288 const llvm::PointerType * BaseTy =
Devang Patelffe1e212007-10-30 20:59:40 +00001289 cast<llvm::PointerType>(BaseValue->getType());
Eli Friedman9a5ffcb2008-05-21 13:24:44 +00001290 unsigned AS = BaseTy->getAddressSpace();
Mike Stump4a3999f2009-09-09 13:00:44 +00001291 V = Builder.CreateBitCast(V,
1292 llvm::PointerType::get(FieldTy, AS),
Eli Friedman9a5ffcb2008-05-21 13:24:44 +00001293 "tmp");
Devang Pateled93c3c2007-10-26 19:42:18 +00001294 }
Eli Friedmanf7f9f682009-05-30 21:09:44 +00001295 if (Field->getType()->isReferenceType())
1296 V = Builder.CreateLoad(V, "tmp");
John McCall8ccfcb52009-09-24 19:53:00 +00001297
1298 Qualifiers Quals = MakeQualifiers(Field->getType());
1299 Quals.addCVRQualifiers(CVRQualifiers);
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +00001300 // __weak attribute on a field is ignored.
John McCall8ccfcb52009-09-24 19:53:00 +00001301 if (Quals.getObjCGCAttr() == Qualifiers::Weak)
1302 Quals.removeObjCGCAttr();
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +00001303
John McCall8ccfcb52009-09-24 19:53:00 +00001304 return LValue::MakeAddr(V, Quals);
Devang Patel30efa2e2007-10-23 20:28:39 +00001305}
1306
Chris Lattner28bcf1a2009-03-18 18:28:57 +00001307LValue CodeGenFunction::EmitCompoundLiteralLValue(const CompoundLiteralExpr* E){
Eli Friedman9fd8b682008-05-13 23:18:27 +00001308 const llvm::Type *LTy = ConvertType(E->getType());
1309 llvm::Value *DeclPtr = CreateTempAlloca(LTy, ".compoundliteral");
1310
1311 const Expr* InitExpr = E->getInitializer();
John McCall8ccfcb52009-09-24 19:53:00 +00001312 LValue Result = LValue::MakeAddr(DeclPtr, MakeQualifiers(E->getType()));
Eli Friedman9fd8b682008-05-13 23:18:27 +00001313
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001314 if (E->getType()->isComplexType())
Eli Friedman9fd8b682008-05-13 23:18:27 +00001315 EmitComplexExprIntoAddr(InitExpr, DeclPtr, false);
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001316 else if (hasAggregateLLVMType(E->getType()))
Eli Friedman9fd8b682008-05-13 23:18:27 +00001317 EmitAnyExpr(InitExpr, DeclPtr, false);
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001318 else
Eli Friedman9fd8b682008-05-13 23:18:27 +00001319 EmitStoreThroughLValue(EmitAnyExpr(InitExpr), Result, E->getType());
Eli Friedman9fd8b682008-05-13 23:18:27 +00001320
1321 return Result;
1322}
1323
Anders Carlsson1450adb2009-09-15 16:35:24 +00001324LValue
1325CodeGenFunction::EmitConditionalOperatorLValue(const ConditionalOperator* E) {
1326 if (E->isLvalue(getContext()) == Expr::LV_Valid) {
1327 llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true");
1328 llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false");
1329 llvm::BasicBlock *ContBlock = createBasicBlock("cond.end");
1330
1331 llvm::Value *Cond = EvaluateExprAsBool(E->getCond());
1332 Builder.CreateCondBr(Cond, LHSBlock, RHSBlock);
1333
1334 EmitBlock(LHSBlock);
Daniel Dunbarbf1fe8c2009-03-24 02:38:23 +00001335
Anders Carlsson1450adb2009-09-15 16:35:24 +00001336 LValue LHS = EmitLValue(E->getLHS());
1337 if (!LHS.isSimple())
1338 return EmitUnsupportedLValue(E, "conditional operator");
1339
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001340 llvm::Value *Temp = CreateTempAlloca(LHS.getAddress()->getType(),"condtmp");
Anders Carlsson1450adb2009-09-15 16:35:24 +00001341 Builder.CreateStore(LHS.getAddress(), Temp);
1342 EmitBranch(ContBlock);
1343
1344 EmitBlock(RHSBlock);
1345 LValue RHS = EmitLValue(E->getRHS());
1346 if (!RHS.isSimple())
1347 return EmitUnsupportedLValue(E, "conditional operator");
1348
1349 Builder.CreateStore(RHS.getAddress(), Temp);
1350 EmitBranch(ContBlock);
1351
1352 EmitBlock(ContBlock);
1353
1354 Temp = Builder.CreateLoad(Temp, "lv");
John McCall8ccfcb52009-09-24 19:53:00 +00001355 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
Anders Carlsson1450adb2009-09-15 16:35:24 +00001356 }
1357
Daniel Dunbarbf1fe8c2009-03-24 02:38:23 +00001358 // ?: here should be an aggregate.
Mike Stump4a3999f2009-09-09 13:00:44 +00001359 assert((hasAggregateLLVMType(E->getType()) &&
Daniel Dunbarbf1fe8c2009-03-24 02:38:23 +00001360 !E->getType()->isAnyComplexType()) &&
1361 "Unexpected conditional operator!");
1362
1363 llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType()));
1364 EmitAggExpr(E, Temp, false);
1365
John McCall8ccfcb52009-09-24 19:53:00 +00001366 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
Daniel Dunbarbf1fe8c2009-03-24 02:38:23 +00001367}
1368
Mike Stump65511702009-11-16 06:50:58 +00001369/// EmitCastLValue - Casts are never lvalues unless that cast is a dynamic_cast.
1370/// If the cast is a dynamic_cast, we can have the usual lvalue result,
1371/// otherwise if a cast is needed by the code generator in an lvalue context,
1372/// then it must mean that we need the address of an aggregate in order to
1373/// access one of its fields. This can happen for all the reasons that casts
1374/// are permitted with aggregate result, including noop aggregate casts, and
1375/// cast from scalar to union.
Chris Lattner28bcf1a2009-03-18 18:28:57 +00001376LValue CodeGenFunction::EmitCastLValue(const CastExpr *E) {
Anders Carlssond95f9602009-09-12 16:16:49 +00001377 switch (E->getCastKind()) {
1378 default:
Eli Friedman8c98dff2009-11-16 05:48:01 +00001379 return EmitUnsupportedLValue(E, "unexpected cast lvalue");
1380
Mike Stump65511702009-11-16 06:50:58 +00001381 case CastExpr::CK_Dynamic: {
1382 LValue LV = EmitLValue(E->getSubExpr());
1383 llvm::Value *V = LV.getAddress();
1384 const CXXDynamicCastExpr *DCE = cast<CXXDynamicCastExpr>(E);
1385 return LValue::MakeAddr(EmitDynamicCast(V, DCE),
1386 MakeQualifiers(E->getType()));
1387 }
1388
Anders Carlssond95f9602009-09-12 16:16:49 +00001389 case CastExpr::CK_NoOp:
1390 case CastExpr::CK_ConstructorConversion:
1391 case CastExpr::CK_UserDefinedConversion:
Fariborz Jahanian2b9fc832009-12-15 21:34:52 +00001392 case CastExpr::CK_AnyPointerToObjCPointerCast:
Chris Lattner28bcf1a2009-03-18 18:28:57 +00001393 return EmitLValue(E->getSubExpr());
Anders Carlssond95f9602009-09-12 16:16:49 +00001394
1395 case CastExpr::CK_DerivedToBase: {
1396 const RecordType *DerivedClassTy =
1397 E->getSubExpr()->getType()->getAs<RecordType>();
1398 CXXRecordDecl *DerivedClassDecl =
1399 cast<CXXRecordDecl>(DerivedClassTy->getDecl());
Chris Lattner28bcf1a2009-03-18 18:28:57 +00001400
Anders Carlssond95f9602009-09-12 16:16:49 +00001401 const RecordType *BaseClassTy = E->getType()->getAs<RecordType>();
1402 CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseClassTy->getDecl());
1403
1404 LValue LV = EmitLValue(E->getSubExpr());
1405
1406 // Perform the derived-to-base conversion
1407 llvm::Value *Base =
Anders Carlsson8c793172009-11-23 17:57:54 +00001408 GetAddressOfBaseClass(LV.getAddress(), DerivedClassDecl,
1409 BaseClassDecl, /*NullCheckValue=*/false);
Anders Carlssond95f9602009-09-12 16:16:49 +00001410
John McCall8ccfcb52009-09-24 19:53:00 +00001411 return LValue::MakeAddr(Base, MakeQualifiers(E->getType()));
Anders Carlssond95f9602009-09-12 16:16:49 +00001412 }
Anders Carlssond95f9602009-09-12 16:16:49 +00001413 case CastExpr::CK_ToUnion: {
1414 llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType()));
1415 EmitAnyExpr(E->getSubExpr(), Temp, false);
Mike Stump4a3999f2009-09-09 13:00:44 +00001416
John McCall8ccfcb52009-09-24 19:53:00 +00001417 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
Anders Carlsson50cb3212009-11-14 21:21:42 +00001418 }
Eli Friedman8c98dff2009-11-16 05:48:01 +00001419 case CastExpr::CK_BaseToDerived: {
Anders Carlsson8c793172009-11-23 17:57:54 +00001420 const RecordType *BaseClassTy =
1421 E->getSubExpr()->getType()->getAs<RecordType>();
1422 CXXRecordDecl *BaseClassDecl =
1423 cast<CXXRecordDecl>(BaseClassTy->getDecl());
1424
1425 const RecordType *DerivedClassTy = E->getType()->getAs<RecordType>();
1426 CXXRecordDecl *DerivedClassDecl =
1427 cast<CXXRecordDecl>(DerivedClassTy->getDecl());
1428
1429 LValue LV = EmitLValue(E->getSubExpr());
1430
1431 // Perform the base-to-derived conversion
1432 llvm::Value *Derived =
1433 GetAddressOfDerivedClass(LV.getAddress(), BaseClassDecl,
1434 DerivedClassDecl, /*NullCheckValue=*/false);
1435
1436 return LValue::MakeAddr(Derived, MakeQualifiers(E->getType()));
Eli Friedman8c98dff2009-11-16 05:48:01 +00001437 }
Anders Carlsson50cb3212009-11-14 21:21:42 +00001438 case CastExpr::CK_BitCast: {
Eli Friedman8c98dff2009-11-16 05:48:01 +00001439 // This must be a reinterpret_cast (or c-style equivalent).
1440 const ExplicitCastExpr *CE = cast<ExplicitCastExpr>(E);
Anders Carlsson50cb3212009-11-14 21:21:42 +00001441
1442 LValue LV = EmitLValue(E->getSubExpr());
1443 llvm::Value *V = Builder.CreateBitCast(LV.getAddress(),
1444 ConvertType(CE->getTypeAsWritten()));
1445 return LValue::MakeAddr(V, MakeQualifiers(E->getType()));
1446 }
Anders Carlssond95f9602009-09-12 16:16:49 +00001447 }
Chris Lattner28bcf1a2009-03-18 18:28:57 +00001448}
1449
Fariborz Jahaniane4d94ce2009-10-20 23:29:04 +00001450LValue CodeGenFunction::EmitNullInitializationLValue(
1451 const CXXZeroInitValueExpr *E) {
1452 QualType Ty = E->getType();
1453 const llvm::Type *LTy = ConvertTypeForMem(Ty);
1454 llvm::AllocaInst *Alloc = CreateTempAlloca(LTy);
1455 unsigned Align = getContext().getTypeAlign(Ty)/8;
1456 Alloc->setAlignment(Align);
1457 LValue lvalue = LValue::MakeAddr(Alloc, Qualifiers());
1458 EmitMemSetToZero(lvalue.getAddress(), Ty);
1459 return lvalue;
1460}
1461
Chris Lattnere47e4402007-06-01 18:02:12 +00001462//===--------------------------------------------------------------------===//
1463// Expression Emission
1464//===--------------------------------------------------------------------===//
1465
Chris Lattner76ba8492007-08-20 22:37:10 +00001466
Chris Lattner2b228c92007-06-15 21:34:29 +00001467RValue CodeGenFunction::EmitCallExpr(const CallExpr *E) {
Daniel Dunbarcdbb5e32009-02-20 18:06:48 +00001468 // Builtins never have block type.
Daniel Dunbarbb197e42009-01-09 16:50:52 +00001469 if (E->getCallee()->getType()->isBlockPointerType())
Anders Carlsson2437cbf2009-02-12 00:39:25 +00001470 return EmitBlockCallExpr(E);
Daniel Dunbarbb197e42009-01-09 16:50:52 +00001471
Anders Carlssone5fd6f22009-04-03 22:50:24 +00001472 if (const CXXMemberCallExpr *CE = dyn_cast<CXXMemberCallExpr>(E))
1473 return EmitCXXMemberCallExpr(CE);
Mike Stump4a3999f2009-09-09 13:00:44 +00001474
Daniel Dunbarcdbb5e32009-02-20 18:06:48 +00001475 const Decl *TargetDecl = 0;
Daniel Dunbar27032de2009-02-20 19:34:33 +00001476 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E->getCallee())) {
1477 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CE->getSubExpr())) {
1478 TargetDecl = DRE->getDecl();
1479 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(TargetDecl))
Douglas Gregor15fc9562009-09-12 00:22:50 +00001480 if (unsigned builtinID = FD->getBuiltinID())
Daniel Dunbar27032de2009-02-20 19:34:33 +00001481 return EmitBuiltinExpr(FD, builtinID, E);
Daniel Dunbarcdbb5e32009-02-20 18:06:48 +00001482 }
1483 }
1484
Chris Lattner4ca97c32009-06-13 00:26:38 +00001485 if (const CXXOperatorCallExpr *CE = dyn_cast<CXXOperatorCallExpr>(E))
Anders Carlsson4034a952009-05-27 04:18:27 +00001486 if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(TargetDecl))
1487 return EmitCXXOperatorMemberCallExpr(CE, MD);
Mike Stump4a3999f2009-09-09 13:00:44 +00001488
Eli Friedman8aaff692009-12-08 02:09:46 +00001489 if (isa<CXXPseudoDestructorExpr>(E->getCallee()->IgnoreParens())) {
Douglas Gregorad8a3362009-09-04 17:36:40 +00001490 // C++ [expr.pseudo]p1:
Mike Stump4a3999f2009-09-09 13:00:44 +00001491 // The result shall only be used as the operand for the function call
Douglas Gregorad8a3362009-09-04 17:36:40 +00001492 // operator (), and the result of such a call has type void. The only
1493 // effect is the evaluation of the postfix-expression before the dot or
1494 // arrow.
1495 EmitScalarExpr(E->getCallee());
1496 return RValue::get(0);
1497 }
Mike Stump4a3999f2009-09-09 13:00:44 +00001498
Chris Lattner2da04b32007-08-24 05:35:26 +00001499 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
Anders Carlsson3a9463b2009-05-27 01:22:39 +00001500 return EmitCall(Callee, E->getCallee()->getType(),
1501 E->arg_begin(), E->arg_end(), TargetDecl);
Chris Lattner9e47ead2007-08-31 04:44:06 +00001502}
1503
Daniel Dunbar8cde00a2008-09-04 03:20:13 +00001504LValue CodeGenFunction::EmitBinaryOperatorLValue(const BinaryOperator *E) {
Chris Lattnere541ea32009-05-12 21:28:12 +00001505 // Comma expressions just emit their LHS then their RHS as an l-value.
1506 if (E->getOpcode() == BinaryOperator::Comma) {
1507 EmitAnyExpr(E->getLHS());
Eli Friedman5445f6e2009-12-07 20:18:11 +00001508 EnsureInsertPoint();
Chris Lattnere541ea32009-05-12 21:28:12 +00001509 return EmitLValue(E->getRHS());
1510 }
Mike Stump4a3999f2009-09-09 13:00:44 +00001511
Fariborz Jahanian038374f2009-10-26 21:58:25 +00001512 if (E->getOpcode() == BinaryOperator::PtrMemD ||
1513 E->getOpcode() == BinaryOperator::PtrMemI)
Fariborz Jahanianffba6622009-10-22 22:57:31 +00001514 return EmitPointerToDataMemberBinaryExpr(E);
1515
Daniel Dunbar8cde00a2008-09-04 03:20:13 +00001516 // Can only get l-value for binary operator expressions which are a
1517 // simple assignment of aggregate type.
1518 if (E->getOpcode() != BinaryOperator::Assign)
1519 return EmitUnsupportedLValue(E, "binary l-value expression");
1520
Anders Carlsson0999aaf2009-10-19 18:28:22 +00001521 if (!hasAggregateLLVMType(E->getType())) {
1522 // Emit the LHS as an l-value.
1523 LValue LV = EmitLValue(E->getLHS());
1524
1525 llvm::Value *RHS = EmitScalarExpr(E->getRHS());
1526 EmitStoreOfScalar(RHS, LV.getAddress(), LV.isVolatileQualified(),
1527 E->getType());
1528 return LV;
1529 }
1530
Daniel Dunbar8cde00a2008-09-04 03:20:13 +00001531 llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType()));
1532 EmitAggExpr(E, Temp, false);
1533 // FIXME: Are these qualifiers correct?
John McCall8ccfcb52009-09-24 19:53:00 +00001534 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
Daniel Dunbar8cde00a2008-09-04 03:20:13 +00001535}
1536
Christopher Lambd91c3d42007-12-29 05:02:41 +00001537LValue CodeGenFunction::EmitCallExprLValue(const CallExpr *E) {
Christopher Lambd91c3d42007-12-29 05:02:41 +00001538 RValue RV = EmitCallExpr(E);
Anders Carlsson4ae70ff2009-05-27 01:45:47 +00001539
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001540 if (!RV.isScalar())
1541 return LValue::MakeAddr(RV.getAggregateAddr(),MakeQualifiers(E->getType()));
1542
1543 assert(E->getCallReturnType()->isReferenceType() &&
1544 "Can't have a scalar return unless the return type is a "
1545 "reference type!");
Mike Stump4a3999f2009-09-09 13:00:44 +00001546
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001547 return LValue::MakeAddr(RV.getScalarVal(), MakeQualifiers(E->getType()));
Christopher Lambd91c3d42007-12-29 05:02:41 +00001548}
1549
Daniel Dunbar8d9dc4a2009-02-11 20:59:32 +00001550LValue CodeGenFunction::EmitVAArgExprLValue(const VAArgExpr *E) {
1551 // FIXME: This shouldn't require another copy.
1552 llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType()));
1553 EmitAggExpr(E, Temp, false);
John McCall8ccfcb52009-09-24 19:53:00 +00001554 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
Daniel Dunbar8d9dc4a2009-02-11 20:59:32 +00001555}
1556
Anders Carlsson3be22e22009-05-30 23:23:33 +00001557LValue CodeGenFunction::EmitCXXConstructLValue(const CXXConstructExpr *E) {
1558 llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(E->getType()), "tmp");
1559 EmitCXXConstructExpr(Temp, E);
John McCall8ccfcb52009-09-24 19:53:00 +00001560 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
Anders Carlsson3be22e22009-05-30 23:23:33 +00001561}
1562
Anders Carlssonfd2af0c2009-05-30 23:30:54 +00001563LValue
Mike Stumpc9b231c2009-11-15 08:09:41 +00001564CodeGenFunction::EmitCXXTypeidLValue(const CXXTypeidExpr *E) {
1565 llvm::Value *Temp = EmitCXXTypeidExpr(E);
1566 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
1567}
1568
1569LValue
Anders Carlssonfd2af0c2009-05-30 23:30:54 +00001570CodeGenFunction::EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr *E) {
1571 LValue LV = EmitLValue(E->getSubExpr());
Anders Carlsson8eb93e72009-05-31 00:34:10 +00001572 PushCXXTemporary(E->getTemporary(), LV.getAddress());
Anders Carlssonfd2af0c2009-05-30 23:30:54 +00001573 return LV;
1574}
1575
Daniel Dunbarc8317a42008-08-23 10:51:21 +00001576LValue CodeGenFunction::EmitObjCMessageExprLValue(const ObjCMessageExpr *E) {
1577 // Can only get l-value for message expression returning aggregate type
1578 RValue RV = EmitObjCMessageExpr(E);
1579 // FIXME: can this be volatile?
John McCall8ccfcb52009-09-24 19:53:00 +00001580 return LValue::MakeAddr(RV.getAggregateAddr(), MakeQualifiers(E->getType()));
Daniel Dunbarc8317a42008-08-23 10:51:21 +00001581}
1582
Daniel Dunbar722f4242009-04-22 05:08:15 +00001583llvm::Value *CodeGenFunction::EmitIvarOffset(const ObjCInterfaceDecl *Interface,
Daniel Dunbar1c64e5d2008-09-24 04:00:38 +00001584 const ObjCIvarDecl *Ivar) {
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001585 return CGM.getObjCRuntime().EmitIvarOffset(*this, Interface, Ivar);
Daniel Dunbar1c64e5d2008-09-24 04:00:38 +00001586}
1587
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00001588LValue CodeGenFunction::EmitLValueForIvar(QualType ObjectTy,
1589 llvm::Value *BaseValue,
Daniel Dunbar1c64e5d2008-09-24 04:00:38 +00001590 const ObjCIvarDecl *Ivar,
1591 unsigned CVRQualifiers) {
Chris Lattnerc4688d22009-04-17 17:44:48 +00001592 return CGM.getObjCRuntime().EmitObjCValueForIvar(*this, ObjectTy, BaseValue,
Daniel Dunbar9ebf9512009-04-21 01:19:28 +00001593 Ivar, CVRQualifiers);
Daniel Dunbar1c64e5d2008-09-24 04:00:38 +00001594}
1595
1596LValue CodeGenFunction::EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E) {
Anders Carlssonc13b85a2008-08-25 01:53:23 +00001597 // FIXME: A lot of the code below could be shared with EmitMemberExpr.
1598 llvm::Value *BaseValue = 0;
1599 const Expr *BaseExpr = E->getBase();
John McCall8ccfcb52009-09-24 19:53:00 +00001600 Qualifiers BaseQuals;
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00001601 QualType ObjectTy;
Anders Carlssonc13b85a2008-08-25 01:53:23 +00001602 if (E->isArrow()) {
1603 BaseValue = EmitScalarExpr(BaseExpr);
Steve Naroff7cae42b2009-07-10 23:34:53 +00001604 ObjectTy = BaseExpr->getType()->getPointeeType();
John McCall8ccfcb52009-09-24 19:53:00 +00001605 BaseQuals = ObjectTy.getQualifiers();
Anders Carlssonc13b85a2008-08-25 01:53:23 +00001606 } else {
1607 LValue BaseLV = EmitLValue(BaseExpr);
1608 // FIXME: this isn't right for bitfields.
1609 BaseValue = BaseLV.getAddress();
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00001610 ObjectTy = BaseExpr->getType();
John McCall8ccfcb52009-09-24 19:53:00 +00001611 BaseQuals = ObjectTy.getQualifiers();
Anders Carlssonc13b85a2008-08-25 01:53:23 +00001612 }
Daniel Dunbar1c64e5d2008-09-24 04:00:38 +00001613
Fariborz Jahaniande1d3242009-09-16 23:11:23 +00001614 LValue LV =
John McCall8ccfcb52009-09-24 19:53:00 +00001615 EmitLValueForIvar(ObjectTy, BaseValue, E->getDecl(),
1616 BaseQuals.getCVRQualifiers());
Fariborz Jahaniande1d3242009-09-16 23:11:23 +00001617 setObjCGCLValueClass(getContext(), E, LV);
1618 return LV;
Chris Lattner4bd55962008-03-30 23:03:07 +00001619}
1620
Mike Stump4a3999f2009-09-09 13:00:44 +00001621LValue
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +00001622CodeGenFunction::EmitObjCPropertyRefLValue(const ObjCPropertyRefExpr *E) {
Mike Stump4a3999f2009-09-09 13:00:44 +00001623 // This is a special l-value that just issues sends when we load or store
1624 // through it.
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +00001625 return LValue::MakePropertyRef(E, E->getType().getCVRQualifiers());
1626}
1627
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001628LValue CodeGenFunction::EmitObjCKVCRefLValue(
Fariborz Jahanian9a846652009-08-20 17:02:02 +00001629 const ObjCImplicitSetterGetterRefExpr *E) {
Mike Stump4a3999f2009-09-09 13:00:44 +00001630 // This is a special l-value that just issues sends when we load or store
1631 // through it.
Fariborz Jahanian9ac53512008-11-22 22:30:21 +00001632 return LValue::MakeKVCRef(E, E->getType().getCVRQualifiers());
1633}
1634
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001635LValue CodeGenFunction::EmitObjCSuperExprLValue(const ObjCSuperExpr *E) {
Douglas Gregor8ea1f532008-11-04 14:56:14 +00001636 return EmitUnsupportedLValue(E, "use of super");
1637}
1638
Chris Lattnera4185c52009-04-25 19:35:26 +00001639LValue CodeGenFunction::EmitStmtExprLValue(const StmtExpr *E) {
Chris Lattnera4185c52009-04-25 19:35:26 +00001640 // Can only get l-value for message expression returning aggregate type
1641 RValue RV = EmitAnyExprToTemp(E);
1642 // FIXME: can this be volatile?
John McCall8ccfcb52009-09-24 19:53:00 +00001643 return LValue::MakeAddr(RV.getAggregateAddr(), MakeQualifiers(E->getType()));
Chris Lattnera4185c52009-04-25 19:35:26 +00001644}
1645
1646
Anders Carlsson509850e2009-11-07 22:00:15 +00001647LValue CodeGenFunction::EmitPointerToDataMemberLValue(const FieldDecl *Field) {
Fariborz Jahanianb25817a2009-10-21 21:01:47 +00001648 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(Field->getDeclContext());
1649 QualType NNSpecTy =
1650 getContext().getCanonicalType(
1651 getContext().getTypeDeclType(const_cast<CXXRecordDecl*>(ClassDecl)));
Fariborz Jahanian4ebdff52009-10-21 18:38:00 +00001652 NNSpecTy = getContext().getPointerType(NNSpecTy);
1653 llvm::Value *V = llvm::Constant::getNullValue(ConvertType(NNSpecTy));
Anders Carlssoncfd30122009-11-17 03:57:07 +00001654 LValue MemExpLV = EmitLValueForField(V, Field, /*isUnion=*/false,
1655 /*Qualifiers=*/0);
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001656 const llvm::Type *ResultType = ConvertType(getContext().getPointerDiffType());
1657 V = Builder.CreatePtrToInt(MemExpLV.getAddress(), ResultType, "datamember");
Anders Carlsson509850e2009-11-07 22:00:15 +00001658 return LValue::MakeAddr(V, MakeQualifiers(Field->getType()));
Fariborz Jahanian4ebdff52009-10-21 18:38:00 +00001659}
1660
Mike Stump4a3999f2009-09-09 13:00:44 +00001661RValue CodeGenFunction::EmitCall(llvm::Value *Callee, QualType CalleeType,
Anders Carlsson3a9463b2009-05-27 01:22:39 +00001662 CallExpr::const_arg_iterator ArgBeg,
1663 CallExpr::const_arg_iterator ArgEnd,
1664 const Decl *TargetDecl) {
Mike Stump4a3999f2009-09-09 13:00:44 +00001665 // Get the actual function type. The callee type will always be a pointer to
1666 // function type or a block pointer type.
1667 assert(CalleeType->isFunctionPointerType() &&
Anders Carlssond8db8532009-04-07 18:53:02 +00001668 "Call must have function pointer type!");
1669
John McCall6fd4c232009-10-23 08:22:42 +00001670 CalleeType = getContext().getCanonicalType(CalleeType);
1671
1672 QualType FnType = cast<PointerType>(CalleeType)->getPointeeType();
1673 QualType ResultType = cast<FunctionType>(FnType)->getResultType();
Daniel Dunbarc722b852008-08-30 03:02:31 +00001674
1675 CallArgList Args;
John McCall6fd4c232009-10-23 08:22:42 +00001676 EmitCallArgs(Args, dyn_cast<FunctionProtoType>(FnType), ArgBeg, ArgEnd);
Daniel Dunbarc722b852008-08-30 03:02:31 +00001677
Daniel Dunbarbbaeca42009-09-11 22:25:00 +00001678 // FIXME: We should not need to do this, it should be part of the function
1679 // type.
1680 unsigned CallingConvention = 0;
1681 if (const llvm::Function *F =
1682 dyn_cast<llvm::Function>(Callee->stripPointerCasts()))
1683 CallingConvention = F->getCallingConv();
1684 return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args,
1685 CallingConvention),
Daniel Dunbarcdbb5e32009-02-20 18:06:48 +00001686 Callee, Args, TargetDecl);
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001687}
Fariborz Jahanianffba6622009-10-22 22:57:31 +00001688
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001689LValue CodeGenFunction::
1690EmitPointerToDataMemberBinaryExpr(const BinaryOperator *E) {
Eli Friedman928a5672009-11-18 05:01:17 +00001691 llvm::Value *BaseV;
Fariborz Jahanian038374f2009-10-26 21:58:25 +00001692 if (E->getOpcode() == BinaryOperator::PtrMemI)
Eli Friedman928a5672009-11-18 05:01:17 +00001693 BaseV = EmitScalarExpr(E->getLHS());
1694 else
1695 BaseV = EmitLValue(E->getLHS()).getAddress();
Fariborz Jahanianffba6622009-10-22 22:57:31 +00001696 const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(getLLVMContext());
1697 BaseV = Builder.CreateBitCast(BaseV, i8Ty);
Eli Friedman928a5672009-11-18 05:01:17 +00001698 llvm::Value *OffsetV = EmitScalarExpr(E->getRHS());
Fariborz Jahanianffba6622009-10-22 22:57:31 +00001699 llvm::Value *AddV = Builder.CreateInBoundsGEP(BaseV, OffsetV, "add.ptr");
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001700
Fariborz Jahanianffba6622009-10-22 22:57:31 +00001701 QualType Ty = E->getRHS()->getType();
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001702 Ty = Ty->getAs<MemberPointerType>()->getPointeeType();
1703
1704 const llvm::Type *PType = ConvertType(getContext().getPointerType(Ty));
Fariborz Jahanianffba6622009-10-22 22:57:31 +00001705 AddV = Builder.CreateBitCast(AddV, PType);
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001706 return LValue::MakeAddr(AddV, MakeQualifiers(Ty));
Fariborz Jahanianffba6622009-10-22 22:57:31 +00001707}
1708