blob: fbeb40cefc0fe40b4a0e0f38ccebd7f065863324 [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;
Eli Friedman357e8c92009-12-19 00:20:10 +0000100
101 if (const CXXDefaultArgExpr *DAE = dyn_cast<CXXDefaultArgExpr>(E))
102 E = DAE->getExpr();
103
Anders Carlsson66413c22009-10-15 00:51:46 +0000104 if (const CXXExprWithTemporaries *TE = dyn_cast<CXXExprWithTemporaries>(E)) {
Anders Carlsson6e997b22009-12-15 20:51:39 +0000105 ShouldDestroyTemporaries = true;
106
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000107 // Keep track of the current cleanup stack depth.
Anders Carlsson6e997b22009-12-15 20:51:39 +0000108 OldNumLiveTemporaries = LiveTemporaries.size();
Anders Carlsson66413c22009-10-15 00:51:46 +0000109
Anders Carlsson69c2c4b2009-10-18 23:09:21 +0000110 E = TE->getSubExpr();
Anders Carlsson66413c22009-10-15 00:51:46 +0000111 }
112
Eli Friedmanc21cb442009-05-20 02:31:19 +0000113 RValue Val;
114 if (E->isLvalue(getContext()) == Expr::LV_Valid) {
Anders Carlsson7d4c0832009-05-20 00:36:58 +0000115 // Emit the expr as an lvalue.
116 LValue LV = EmitLValue(E);
Eli Friedmanc21cb442009-05-20 02:31:19 +0000117 if (LV.isSimple())
118 return RValue::get(LV.getAddress());
119 Val = EmitLoadOfLValue(LV, E->getType());
Anders Carlsson69c2c4b2009-10-18 23:09:21 +0000120
121 if (ShouldDestroyTemporaries) {
122 // Pop temporaries.
123 while (LiveTemporaries.size() > OldNumLiveTemporaries)
124 PopCXXTemporary();
125 }
Eli Friedmanc21cb442009-05-20 02:31:19 +0000126 } else {
Anders Carlsson66413c22009-10-15 00:51:46 +0000127 const CXXRecordDecl *BaseClassDecl = 0;
128 const CXXRecordDecl *DerivedClassDecl = 0;
129
130 if (const CastExpr *CE =
131 dyn_cast<CastExpr>(E->IgnoreParenNoopCasts(getContext()))) {
132 if (CE->getCastKind() == CastExpr::CK_DerivedToBase) {
133 E = CE->getSubExpr();
134
135 BaseClassDecl =
136 cast<CXXRecordDecl>(CE->getType()->getAs<RecordType>()->getDecl());
137 DerivedClassDecl =
138 cast<CXXRecordDecl>(E->getType()->getAs<RecordType>()->getDecl());
139 }
140 }
141
Anders Carlsson5b106a72009-08-16 07:36:22 +0000142 Val = EmitAnyExprToTemp(E, /*IsAggLocVolatile=*/false,
143 IsInitializer);
Mike Stump4a3999f2009-09-09 13:00:44 +0000144
Anders Carlsson69c2c4b2009-10-18 23:09:21 +0000145 if (ShouldDestroyTemporaries) {
146 // Pop temporaries.
147 while (LiveTemporaries.size() > OldNumLiveTemporaries)
148 PopCXXTemporary();
149 }
150
Anders Carlsson3b848942009-08-16 17:54:29 +0000151 if (IsInitializer) {
152 // We might have to destroy the temporary variable.
153 if (const RecordType *RT = E->getType()->getAs<RecordType>()) {
154 if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
155 if (!ClassDecl->hasTrivialDestructor()) {
Mike Stump4a3999f2009-09-09 13:00:44 +0000156 const CXXDestructorDecl *Dtor =
Anders Carlsson3b848942009-08-16 17:54:29 +0000157 ClassDecl->getDestructor(getContext());
Mike Stump4a3999f2009-09-09 13:00:44 +0000158
Mike Stumpaff69af2009-12-09 03:35:49 +0000159 {
Anders Carlsson0c6a7d82009-12-11 01:00:09 +0000160 DelayedCleanupBlock Scope(*this);
Mike Stumpaff69af2009-12-09 03:35:49 +0000161 EmitCXXDestructorCall(Dtor, Dtor_Complete,
162 Val.getAggregateAddr());
Anders Carlsson0c6a7d82009-12-11 01:00:09 +0000163
164 // Make sure to jump to the exit block.
165 EmitBranch(Scope.getCleanupExitBlock());
Mike Stumpaff69af2009-12-09 03:35:49 +0000166 }
167 if (Exceptions) {
168 EHCleanupBlock Cleanup(*this);
169 EmitCXXDestructorCall(Dtor, Dtor_Complete,
170 Val.getAggregateAddr());
171 }
Anders Carlsson3b848942009-08-16 17:54:29 +0000172 }
Anders Carlssonb80760b2009-08-16 17:50:25 +0000173 }
174 }
175 }
Anders Carlsson66413c22009-10-15 00:51:46 +0000176
177 // Check if need to perform the derived-to-base cast.
178 if (BaseClassDecl) {
179 llvm::Value *Derived = Val.getAggregateAddr();
Anders Carlsson66413c22009-10-15 00:51:46 +0000180 llvm::Value *Base =
Anders Carlsson8c793172009-11-23 17:57:54 +0000181 GetAddressOfBaseClass(Derived, DerivedClassDecl, BaseClassDecl,
182 /*NullCheckValue=*/false);
Anders Carlsson66413c22009-10-15 00:51:46 +0000183 return RValue::get(Base);
184 }
Anders Carlsson7d4c0832009-05-20 00:36:58 +0000185 }
Eli Friedmanc21cb442009-05-20 02:31:19 +0000186
187 if (Val.isAggregate()) {
188 Val = RValue::get(Val.getAggregateAddr());
189 } else {
Anders Carlsson02bb7f02009-05-20 01:35:03 +0000190 // Create a temporary variable that we can bind the reference to.
Mike Stump4a3999f2009-09-09 13:00:44 +0000191 llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(E->getType()),
Anders Carlsson145eae52009-05-20 01:03:17 +0000192 "reftmp");
Eli Friedmanc21cb442009-05-20 02:31:19 +0000193 if (Val.isScalar())
194 EmitStoreOfScalar(Val.getScalarVal(), Temp, false, E->getType());
195 else
196 StoreComplexToAddr(Val.getComplexVal(), Temp, false);
197 Val = RValue::get(Temp);
Anders Carlsson145eae52009-05-20 01:03:17 +0000198 }
Eli Friedmanc21cb442009-05-20 02:31:19 +0000199
200 return Val;
Anders Carlsson6f5a0152009-05-20 00:24:07 +0000201}
202
203
Mike Stump4a3999f2009-09-09 13:00:44 +0000204/// getAccessedFieldNo - Given an encoded value and a result number, return the
205/// input field number being accessed.
206unsigned CodeGenFunction::getAccessedFieldNo(unsigned Idx,
Dan Gohman75d69da2008-05-22 00:50:06 +0000207 const llvm::Constant *Elts) {
208 if (isa<llvm::ConstantAggregateZero>(Elts))
209 return 0;
Mike Stump4a3999f2009-09-09 13:00:44 +0000210
Dan Gohman75d69da2008-05-22 00:50:06 +0000211 return cast<llvm::ConstantInt>(Elts->getOperand(Idx))->getZExtValue();
212}
213
Mike Stump3f6f9fe2009-12-16 02:57:00 +0000214void CodeGenFunction::EmitCheck(llvm::Value *Address, unsigned Size) {
215 if (!CatchUndefined)
216 return;
217
218 const llvm::IntegerType *Size_tTy
219 = llvm::IntegerType::get(VMContext, LLVMPointerWidth);
220 Address = Builder.CreateBitCast(Address, PtrToInt8Ty);
221
222 const llvm::Type *ResType[] = {
223 Size_tTy
224 };
225 llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::objectsize, ResType, 1);
226 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
227 CGM.getTypes().ConvertType(CGM.getContext().IntTy));
228 // In time, people may want to control this and use a 1 here.
229 llvm::Value *Arg = llvm::ConstantInt::get(IntTy, 0);
230 llvm::Value *C = Builder.CreateCall2(F, Address, Arg);
231 llvm::BasicBlock *Cont = createBasicBlock();
232 llvm::BasicBlock *Check = createBasicBlock();
233 llvm::Value *NegativeOne = llvm::ConstantInt::get(Size_tTy, -1ULL);
234 Builder.CreateCondBr(Builder.CreateICmpEQ(C, NegativeOne), Cont, Check);
235
236 EmitBlock(Check);
237 Builder.CreateCondBr(Builder.CreateICmpUGE(C,
238 llvm::ConstantInt::get(Size_tTy, Size)),
239 Cont, getTrapBB());
240 EmitBlock(Cont);
241}
Chris Lattner4647a212007-08-31 22:49:20 +0000242
Chris Lattnera45c5af2007-06-02 19:47:04 +0000243//===----------------------------------------------------------------------===//
Chris Lattnerd7f58862007-06-02 05:24:33 +0000244// LValue Expression Emission
Chris Lattnera45c5af2007-06-02 19:47:04 +0000245//===----------------------------------------------------------------------===//
Chris Lattnerd7f58862007-06-02 05:24:33 +0000246
Daniel Dunbarc79407f2009-02-05 07:09:07 +0000247RValue CodeGenFunction::GetUndefRValue(QualType Ty) {
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000248 if (Ty->isVoidType())
Daniel Dunbarc79407f2009-02-05 07:09:07 +0000249 return RValue::get(0);
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000250
251 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
Daniel Dunbar8429dbc2009-01-09 20:09:28 +0000252 const llvm::Type *EltTy = ConvertType(CTy->getElementType());
Owen Anderson7ec07a52009-07-30 23:11:26 +0000253 llvm::Value *U = llvm::UndefValue::get(EltTy);
Daniel Dunbar8429dbc2009-01-09 20:09:28 +0000254 return RValue::getComplex(std::make_pair(U, U));
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000255 }
256
257 if (hasAggregateLLVMType(Ty)) {
Owen Anderson9793f0e2009-07-29 22:16:19 +0000258 const llvm::Type *LTy = llvm::PointerType::getUnqual(ConvertType(Ty));
Owen Anderson7ec07a52009-07-30 23:11:26 +0000259 return RValue::getAggregate(llvm::UndefValue::get(LTy));
Daniel Dunbar8429dbc2009-01-09 20:09:28 +0000260 }
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000261
262 return RValue::get(llvm::UndefValue::get(ConvertType(Ty)));
Daniel Dunbarbb197e42009-01-09 16:50:52 +0000263}
264
Daniel Dunbarc79407f2009-02-05 07:09:07 +0000265RValue CodeGenFunction::EmitUnsupportedRValue(const Expr *E,
266 const char *Name) {
267 ErrorUnsupported(E, Name);
268 return GetUndefRValue(E->getType());
269}
270
Daniel Dunbarf2e69882008-08-25 20:45:57 +0000271LValue CodeGenFunction::EmitUnsupportedLValue(const Expr *E,
272 const char *Name) {
273 ErrorUnsupported(E, Name);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000274 llvm::Type *Ty = llvm::PointerType::getUnqual(ConvertType(E->getType()));
Owen Anderson7ec07a52009-07-30 23:11:26 +0000275 return LValue::MakeAddr(llvm::UndefValue::get(Ty),
John McCall8ccfcb52009-09-24 19:53:00 +0000276 MakeQualifiers(E->getType()));
Daniel Dunbarf2e69882008-08-25 20:45:57 +0000277}
278
Mike Stump3f6f9fe2009-12-16 02:57:00 +0000279LValue CodeGenFunction::EmitCheckedLValue(const Expr *E) {
280 LValue LV = EmitLValue(E);
281 if (!isa<DeclRefExpr>(E) && !LV.isBitfield() && LV.isSimple())
282 EmitCheck(LV.getAddress(), getContext().getTypeSize(E->getType()) / 8);
283 return LV;
284}
285
Chris Lattner8394d792007-06-05 20:53:16 +0000286/// EmitLValue - Emit code to compute a designator that specifies the location
287/// of the expression.
288///
Mike Stump4a3999f2009-09-09 13:00:44 +0000289/// This can return one of two things: a simple address or a bitfield reference.
290/// In either case, the LLVM Value* in the LValue structure is guaranteed to be
291/// an LLVM pointer type.
Chris Lattner8394d792007-06-05 20:53:16 +0000292///
Mike Stump4a3999f2009-09-09 13:00:44 +0000293/// If this returns a bitfield reference, nothing about the pointee type of the
294/// LLVM value is known: For example, it may not be a pointer to an integer.
Chris Lattner8394d792007-06-05 20:53:16 +0000295///
Mike Stump4a3999f2009-09-09 13:00:44 +0000296/// If this returns a normal address, and if the lvalue's C type is fixed size,
297/// this method guarantees that the returned pointer type will point to an LLVM
298/// type of the same size of the lvalue's type. If the lvalue has a variable
299/// length type, this is not possible.
Chris Lattner8394d792007-06-05 20:53:16 +0000300///
Chris Lattnerd7f58862007-06-02 05:24:33 +0000301LValue CodeGenFunction::EmitLValue(const Expr *E) {
302 switch (E->getStmtClass()) {
Daniel Dunbarf2e69882008-08-25 20:45:57 +0000303 default: return EmitUnsupportedLValue(E, "l-value expression");
Chris Lattnerd7f58862007-06-02 05:24:33 +0000304
Fariborz Jahanian531c16f2009-12-09 23:35:29 +0000305 case Expr::ObjCIsaExprClass:
306 return EmitObjCIsaExpr(cast<ObjCIsaExpr>(E));
Mike Stump4a3999f2009-09-09 13:00:44 +0000307 case Expr::BinaryOperatorClass:
Daniel Dunbar8cde00a2008-09-04 03:20:13 +0000308 return EmitBinaryOperatorLValue(cast<BinaryOperator>(E));
Mike Stump4a3999f2009-09-09 13:00:44 +0000309 case Expr::CallExprClass:
Anders Carlssonc82555f2009-09-01 21:18:52 +0000310 case Expr::CXXMemberCallExprClass:
Douglas Gregor993603d2008-11-14 16:09:21 +0000311 case Expr::CXXOperatorCallExprClass:
312 return EmitCallExprLValue(cast<CallExpr>(E));
Daniel Dunbar8d9dc4a2009-02-11 20:59:32 +0000313 case Expr::VAArgExprClass:
314 return EmitVAArgExprLValue(cast<VAArgExpr>(E));
Mike Stump4a3999f2009-09-09 13:00:44 +0000315 case Expr::DeclRefExprClass:
Douglas Gregorc7acfdf2009-01-06 05:10:23 +0000316 return EmitDeclRefLValue(cast<DeclRefExpr>(E));
Chris Lattner946aa312007-06-05 03:59:43 +0000317 case Expr::ParenExprClass:return EmitLValue(cast<ParenExpr>(E)->getSubExpr());
Chris Lattner6307f192008-08-10 01:53:14 +0000318 case Expr::PredefinedExprClass:
319 return EmitPredefinedLValue(cast<PredefinedExpr>(E));
Chris Lattner4347e3692007-06-06 04:54:52 +0000320 case Expr::StringLiteralClass:
321 return EmitStringLiteralLValue(cast<StringLiteral>(E));
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +0000322 case Expr::ObjCEncodeExprClass:
323 return EmitObjCEncodeExprLValue(cast<ObjCEncodeExpr>(E));
Chris Lattner4bd55962008-03-30 23:03:07 +0000324
Mike Stump4a3999f2009-09-09 13:00:44 +0000325 case Expr::BlockDeclRefExprClass:
Mike Stump1db7d042009-02-28 09:07:16 +0000326 return EmitBlockDeclRefLValue(cast<BlockDeclRefExpr>(E));
327
Anders Carlsson3be22e22009-05-30 23:23:33 +0000328 case Expr::CXXTemporaryObjectExprClass:
329 case Expr::CXXConstructExprClass:
Anders Carlssonfd2af0c2009-05-30 23:30:54 +0000330 return EmitCXXConstructLValue(cast<CXXConstructExpr>(E));
331 case Expr::CXXBindTemporaryExprClass:
332 return EmitCXXBindTemporaryLValue(cast<CXXBindTemporaryExpr>(E));
Anders Carlsson96bad9a2009-09-14 01:10:45 +0000333 case Expr::CXXExprWithTemporariesClass:
334 return EmitCXXExprWithTemporariesLValue(cast<CXXExprWithTemporaries>(E));
Anders Carlsson52ce3bb2009-11-14 01:51:50 +0000335 case Expr::CXXZeroInitValueExprClass:
336 return EmitNullInitializationLValue(cast<CXXZeroInitValueExpr>(E));
337 case Expr::CXXDefaultArgExprClass:
338 return EmitLValue(cast<CXXDefaultArgExpr>(E)->getExpr());
Mike Stumpc9b231c2009-11-15 08:09:41 +0000339 case Expr::CXXTypeidExprClass:
340 return EmitCXXTypeidLValue(cast<CXXTypeidExpr>(E));
Anders Carlssonfd2af0c2009-05-30 23:30:54 +0000341
Daniel Dunbarc8317a42008-08-23 10:51:21 +0000342 case Expr::ObjCMessageExprClass:
343 return EmitObjCMessageExprLValue(cast<ObjCMessageExpr>(E));
Mike Stump4a3999f2009-09-09 13:00:44 +0000344 case Expr::ObjCIvarRefExprClass:
Chris Lattner4bd55962008-03-30 23:03:07 +0000345 return EmitObjCIvarRefLValue(cast<ObjCIvarRefExpr>(E));
Daniel Dunbarf2e69882008-08-25 20:45:57 +0000346 case Expr::ObjCPropertyRefExprClass:
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +0000347 return EmitObjCPropertyRefLValue(cast<ObjCPropertyRefExpr>(E));
Fariborz Jahanian9a846652009-08-20 17:02:02 +0000348 case Expr::ObjCImplicitSetterGetterRefExprClass:
349 return EmitObjCKVCRefLValue(cast<ObjCImplicitSetterGetterRefExpr>(E));
Douglas Gregor8ea1f532008-11-04 14:56:14 +0000350 case Expr::ObjCSuperExprClass:
Chris Lattnera4185c52009-04-25 19:35:26 +0000351 return EmitObjCSuperExprLValue(cast<ObjCSuperExpr>(E));
Douglas Gregor8ea1f532008-11-04 14:56:14 +0000352
Chris Lattnera4185c52009-04-25 19:35:26 +0000353 case Expr::StmtExprClass:
354 return EmitStmtExprLValue(cast<StmtExpr>(E));
Mike Stump4a3999f2009-09-09 13:00:44 +0000355 case Expr::UnaryOperatorClass:
Chris Lattner8394d792007-06-05 20:53:16 +0000356 return EmitUnaryOpLValue(cast<UnaryOperator>(E));
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000357 case Expr::ArraySubscriptExprClass:
358 return EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E));
Nate Begemance4d7fc2008-04-18 23:10:10 +0000359 case Expr::ExtVectorElementExprClass:
360 return EmitExtVectorElementExpr(cast<ExtVectorElementExpr>(E));
Mike Stump4a3999f2009-09-09 13:00:44 +0000361 case Expr::MemberExprClass:
Douglas Gregorc1905232009-08-26 22:36:53 +0000362 return EmitMemberExpr(cast<MemberExpr>(E));
Eli Friedman9fd8b682008-05-13 23:18:27 +0000363 case Expr::CompoundLiteralExprClass:
364 return EmitCompoundLiteralLValue(cast<CompoundLiteralExpr>(E));
Daniel Dunbarbf1fe8c2009-03-24 02:38:23 +0000365 case Expr::ConditionalOperatorClass:
Anders Carlsson1450adb2009-09-15 16:35:24 +0000366 return EmitConditionalOperatorLValue(cast<ConditionalOperator>(E));
Chris Lattner053441f2008-12-12 05:35:08 +0000367 case Expr::ChooseExprClass:
Eli Friedmane0a5b8b2009-03-04 05:52:32 +0000368 return EmitLValue(cast<ChooseExpr>(E)->getChosenSubExpr(getContext()));
Chris Lattner63d06ab2009-03-18 04:02:57 +0000369 case Expr::ImplicitCastExprClass:
370 case Expr::CStyleCastExprClass:
371 case Expr::CXXFunctionalCastExprClass:
372 case Expr::CXXStaticCastExprClass:
373 case Expr::CXXDynamicCastExprClass:
374 case Expr::CXXReinterpretCastExprClass:
375 case Expr::CXXConstCastExprClass:
Chris Lattner28bcf1a2009-03-18 18:28:57 +0000376 return EmitCastLValue(cast<CastExpr>(E));
Chris Lattnerd7f58862007-06-02 05:24:33 +0000377 }
378}
379
Daniel Dunbar1d425462009-02-10 00:57:50 +0000380llvm::Value *CodeGenFunction::EmitLoadOfScalar(llvm::Value *Addr, bool Volatile,
381 QualType Ty) {
Daniel Dunbarc76493a2009-11-29 21:23:36 +0000382 llvm::LoadInst *Load = Builder.CreateLoad(Addr, "tmp");
383 if (Volatile)
384 Load->setVolatile(true);
Daniel Dunbar1d425462009-02-10 00:57:50 +0000385
Anders Carlsson29a1be32009-05-19 19:36:19 +0000386 // Bool can have different representation in memory than in registers.
Daniel Dunbarc76493a2009-11-29 21:23:36 +0000387 llvm::Value *V = Load;
Daniel Dunbar1d425462009-02-10 00:57:50 +0000388 if (Ty->isBooleanType())
Owen Anderson41a75022009-08-13 21:57:51 +0000389 if (V->getType() != llvm::Type::getInt1Ty(VMContext))
390 V = Builder.CreateTrunc(V, llvm::Type::getInt1Ty(VMContext), "tobool");
Mike Stump4a3999f2009-09-09 13:00:44 +0000391
Daniel Dunbar1d425462009-02-10 00:57:50 +0000392 return V;
393}
394
395void CodeGenFunction::EmitStoreOfScalar(llvm::Value *Value, llvm::Value *Addr,
Anders Carlsson83709642009-05-19 18:50:41 +0000396 bool Volatile, QualType Ty) {
Mike Stump4a3999f2009-09-09 13:00:44 +0000397
Anders Carlsson29a1be32009-05-19 19:36:19 +0000398 if (Ty->isBooleanType()) {
399 // Bool can have different representation in memory than in registers.
Anders Carlsson29a1be32009-05-19 19:36:19 +0000400 const llvm::PointerType *DstPtr = cast<llvm::PointerType>(Addr->getType());
Eli Friedmanb2b120f2009-12-01 22:31:51 +0000401 Value = Builder.CreateIntCast(Value, DstPtr->getElementType(), false);
Daniel Dunbar1d425462009-02-10 00:57:50 +0000402 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000403 Builder.CreateStore(Value, Addr, Volatile);
Daniel Dunbar1d425462009-02-10 00:57:50 +0000404}
405
Mike Stump4a3999f2009-09-09 13:00:44 +0000406/// EmitLoadOfLValue - Given an expression that represents a value lvalue, this
407/// method emits the address of the lvalue, then loads the result as an rvalue,
408/// returning the rvalue.
Chris Lattner9369a562007-06-29 16:31:29 +0000409RValue CodeGenFunction::EmitLoadOfLValue(LValue LV, QualType ExprType) {
Fariborz Jahanian50a12702008-11-19 17:34:06 +0000410 if (LV.isObjCWeak()) {
Mike Stump4a3999f2009-09-09 13:00:44 +0000411 // load of a __weak object.
Fariborz Jahanianf5125d12008-11-18 21:45:40 +0000412 llvm::Value *AddrWeakObj = LV.getAddress();
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000413 return RValue::get(CGM.getObjCRuntime().EmitObjCWeakRead(*this,
414 AddrWeakObj));
Fariborz Jahanianf5125d12008-11-18 21:45:40 +0000415 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000416
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000417 if (LV.isSimple()) {
418 llvm::Value *Ptr = LV.getAddress();
419 const llvm::Type *EltTy =
420 cast<llvm::PointerType>(Ptr->getType())->getElementType();
Mike Stump4a3999f2009-09-09 13:00:44 +0000421
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000422 // Simple scalar l-value.
Daniel Dunbar1d425462009-02-10 00:57:50 +0000423 if (EltTy->isSingleValueType())
Mike Stump4a3999f2009-09-09 13:00:44 +0000424 return RValue::get(EmitLoadOfScalar(Ptr, LV.isVolatileQualified(),
Daniel Dunbar1d425462009-02-10 00:57:50 +0000425 ExprType));
Mike Stump4a3999f2009-09-09 13:00:44 +0000426
Chris Lattner6278e6a2007-08-11 00:04:45 +0000427 assert(ExprType->isFunctionType() && "Unknown scalar value");
428 return RValue::get(Ptr);
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000429 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000430
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000431 if (LV.isVectorElt()) {
Eli Friedman327944b2008-06-13 23:01:12 +0000432 llvm::Value *Vec = Builder.CreateLoad(LV.getVectorAddr(),
433 LV.isVolatileQualified(), "tmp");
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000434 return RValue::get(Builder.CreateExtractElement(Vec, LV.getVectorIdx(),
435 "vecext"));
436 }
Chris Lattner73ab9b32007-08-03 00:16:29 +0000437
438 // If this is a reference to a subset of the elements of a vector, either
439 // shuffle the input or extract/insert them as appropriate.
Nate Begemance4d7fc2008-04-18 23:10:10 +0000440 if (LV.isExtVectorElt())
441 return EmitLoadOfExtVectorElementLValue(LV, ExprType);
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000442
443 if (LV.isBitfield())
444 return EmitLoadOfBitfieldLValue(LV, ExprType);
445
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +0000446 if (LV.isPropertyRef())
447 return EmitLoadOfPropertyRefLValue(LV, ExprType);
448
Chris Lattner6c7ce102009-02-16 21:11:58 +0000449 assert(LV.isKVCRef() && "Unknown LValue type!");
450 return EmitLoadOfKVCRefLValue(LV, ExprType);
Chris Lattner8394d792007-06-05 20:53:16 +0000451}
452
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000453RValue CodeGenFunction::EmitLoadOfBitfieldLValue(LValue LV,
454 QualType ExprType) {
Daniel Dunbaread7c912008-08-06 05:08:45 +0000455 unsigned StartBit = LV.getBitfieldStartBit();
456 unsigned BitfieldSize = LV.getBitfieldSize();
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000457 llvm::Value *Ptr = LV.getBitfieldAddr();
Daniel Dunbaread7c912008-08-06 05:08:45 +0000458
Mike Stump4a3999f2009-09-09 13:00:44 +0000459 const llvm::Type *EltTy =
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000460 cast<llvm::PointerType>(Ptr->getType())->getElementType();
Daniel Dunbaread7c912008-08-06 05:08:45 +0000461 unsigned EltTySize = CGM.getTargetData().getTypeSizeInBits(EltTy);
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000462
Mike Stump4a3999f2009-09-09 13:00:44 +0000463 // In some cases the bitfield may straddle two memory locations. Currently we
464 // load the entire bitfield, then do the magic to sign-extend it if
465 // necessary. This results in somewhat more code than necessary for the common
466 // case (one load), since two shifts accomplish both the masking and sign
467 // extension.
Daniel Dunbaread7c912008-08-06 05:08:45 +0000468 unsigned LowBits = std::min(BitfieldSize, EltTySize - StartBit);
469 llvm::Value *Val = Builder.CreateLoad(Ptr, LV.isVolatileQualified(), "tmp");
Mike Stump4a3999f2009-09-09 13:00:44 +0000470
Daniel Dunbaread7c912008-08-06 05:08:45 +0000471 // Shift to proper location.
Daniel Dunbarf7fb7502008-11-13 02:20:34 +0000472 if (StartBit)
Chris Lattner72ecc682009-12-06 17:22:42 +0000473 Val = Builder.CreateLShr(Val, StartBit, "bf.lo");
Mike Stump4a3999f2009-09-09 13:00:44 +0000474
Daniel Dunbaread7c912008-08-06 05:08:45 +0000475 // Mask off unused bits.
Mike Stump4a3999f2009-09-09 13:00:44 +0000476 llvm::Constant *LowMask = llvm::ConstantInt::get(VMContext,
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000477 llvm::APInt::getLowBitsSet(EltTySize, LowBits));
Daniel Dunbaread7c912008-08-06 05:08:45 +0000478 Val = Builder.CreateAnd(Val, LowMask, "bf.lo.cleared");
Mike Stump4a3999f2009-09-09 13:00:44 +0000479
Daniel Dunbaread7c912008-08-06 05:08:45 +0000480 // Fetch the high bits if necessary.
481 if (LowBits < BitfieldSize) {
482 unsigned HighBits = BitfieldSize - LowBits;
Owen Anderson41a75022009-08-13 21:57:51 +0000483 llvm::Value *HighPtr = Builder.CreateGEP(Ptr, llvm::ConstantInt::get(
Mike Stump4a3999f2009-09-09 13:00:44 +0000484 llvm::Type::getInt32Ty(VMContext), 1), "bf.ptr.hi");
485 llvm::Value *HighVal = Builder.CreateLoad(HighPtr,
Daniel Dunbaread7c912008-08-06 05:08:45 +0000486 LV.isVolatileQualified(),
487 "tmp");
Mike Stump4a3999f2009-09-09 13:00:44 +0000488
Daniel Dunbaread7c912008-08-06 05:08:45 +0000489 // Mask off unused bits.
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000490 llvm::Constant *HighMask = llvm::ConstantInt::get(VMContext,
491 llvm::APInt::getLowBitsSet(EltTySize, HighBits));
Daniel Dunbaread7c912008-08-06 05:08:45 +0000492 HighVal = Builder.CreateAnd(HighVal, HighMask, "bf.lo.cleared");
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000493
Daniel Dunbaread7c912008-08-06 05:08:45 +0000494 // Shift to proper location and or in to bitfield value.
Chris Lattner72ecc682009-12-06 17:22:42 +0000495 HighVal = Builder.CreateShl(HighVal, LowBits);
Daniel Dunbaread7c912008-08-06 05:08:45 +0000496 Val = Builder.CreateOr(Val, HighVal, "bf.val");
497 }
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000498
Daniel Dunbaread7c912008-08-06 05:08:45 +0000499 // Sign extend if necessary.
500 if (LV.isBitfieldSigned()) {
Mike Stump4a3999f2009-09-09 13:00:44 +0000501 llvm::Value *ExtraBits = llvm::ConstantInt::get(EltTy,
Daniel Dunbaread7c912008-08-06 05:08:45 +0000502 EltTySize - BitfieldSize);
Mike Stump4a3999f2009-09-09 13:00:44 +0000503 Val = Builder.CreateAShr(Builder.CreateShl(Val, ExtraBits),
Daniel Dunbaread7c912008-08-06 05:08:45 +0000504 ExtraBits, "bf.val.sext");
505 }
Eli Friedmanf2442dc2008-05-17 20:03:47 +0000506
Mike Stump4a3999f2009-09-09 13:00:44 +0000507 // The bitfield type and the normal type differ when the storage sizes differ
508 // (currently just _Bool).
Daniel Dunbaread7c912008-08-06 05:08:45 +0000509 Val = Builder.CreateIntCast(Val, ConvertType(ExprType), false, "tmp");
Eli Friedmanf2442dc2008-05-17 20:03:47 +0000510
Daniel Dunbaread7c912008-08-06 05:08:45 +0000511 return RValue::get(Val);
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000512}
513
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +0000514RValue CodeGenFunction::EmitLoadOfPropertyRefLValue(LValue LV,
515 QualType ExprType) {
516 return EmitObjCPropertyGet(LV.getPropertyRefExpr());
517}
518
Fariborz Jahanian9ac53512008-11-22 22:30:21 +0000519RValue CodeGenFunction::EmitLoadOfKVCRefLValue(LValue LV,
520 QualType ExprType) {
521 return EmitObjCPropertyGet(LV.getKVCRefExpr());
522}
523
Nate Begemanb699c9b2009-01-18 06:42:49 +0000524// If this is a reference to a subset of the elements of a vector, create an
525// appropriate shufflevector.
Nate Begemance4d7fc2008-04-18 23:10:10 +0000526RValue CodeGenFunction::EmitLoadOfExtVectorElementLValue(LValue LV,
527 QualType ExprType) {
Eli Friedman327944b2008-06-13 23:01:12 +0000528 llvm::Value *Vec = Builder.CreateLoad(LV.getExtVectorAddr(),
529 LV.isVolatileQualified(), "tmp");
Mike Stump4a3999f2009-09-09 13:00:44 +0000530
Nate Begemanf322eab2008-05-09 06:41:27 +0000531 const llvm::Constant *Elts = LV.getExtVectorElts();
Mike Stump4a3999f2009-09-09 13:00:44 +0000532
533 // If the result of the expression is a non-vector type, we must be extracting
534 // a single element. Just codegen as an extractelement.
John McCall9dd450b2009-09-21 23:43:11 +0000535 const VectorType *ExprVT = ExprType->getAs<VectorType>();
Chris Lattner8eab8ff2007-08-10 17:10:08 +0000536 if (!ExprVT) {
Dan Gohman75d69da2008-05-22 00:50:06 +0000537 unsigned InIdx = getAccessedFieldNo(0, Elts);
Owen Anderson41a75022009-08-13 21:57:51 +0000538 llvm::Value *Elt = llvm::ConstantInt::get(
539 llvm::Type::getInt32Ty(VMContext), InIdx);
Chris Lattner40ff7012007-08-03 16:18:34 +0000540 return RValue::get(Builder.CreateExtractElement(Vec, Elt, "tmp"));
541 }
Nate Begemanb699c9b2009-01-18 06:42:49 +0000542
543 // Always use shuffle vector to try to retain the original program structure
Chris Lattner8eab8ff2007-08-10 17:10:08 +0000544 unsigned NumResultElts = ExprVT->getNumElements();
Mike Stump4a3999f2009-09-09 13:00:44 +0000545
Nate Begemanb699c9b2009-01-18 06:42:49 +0000546 llvm::SmallVector<llvm::Constant*, 4> Mask;
Chris Lattner40ff7012007-08-03 16:18:34 +0000547 for (unsigned i = 0; i != NumResultElts; ++i) {
Dan Gohman75d69da2008-05-22 00:50:06 +0000548 unsigned InIdx = getAccessedFieldNo(i, Elts);
Owen Anderson41a75022009-08-13 21:57:51 +0000549 Mask.push_back(llvm::ConstantInt::get(
550 llvm::Type::getInt32Ty(VMContext), InIdx));
Chris Lattner40ff7012007-08-03 16:18:34 +0000551 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000552
Owen Anderson3cc120a2009-07-28 21:22:35 +0000553 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
Nate Begemanb699c9b2009-01-18 06:42:49 +0000554 Vec = Builder.CreateShuffleVector(Vec,
Owen Anderson7ec07a52009-07-30 23:11:26 +0000555 llvm::UndefValue::get(Vec->getType()),
Nate Begemanb699c9b2009-01-18 06:42:49 +0000556 MaskV, "tmp");
557 return RValue::get(Vec);
Chris Lattner40ff7012007-08-03 16:18:34 +0000558}
559
560
Chris Lattner9369a562007-06-29 16:31:29 +0000561
Chris Lattner8394d792007-06-05 20:53:16 +0000562/// EmitStoreThroughLValue - Store the specified rvalue into the specified
563/// lvalue, where both are guaranteed to the have the same type, and that type
564/// is 'Ty'.
Mike Stump4a3999f2009-09-09 13:00:44 +0000565void CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst,
Chris Lattner8394d792007-06-05 20:53:16 +0000566 QualType Ty) {
Chris Lattner41d480e2007-08-03 16:28:33 +0000567 if (!Dst.isSimple()) {
568 if (Dst.isVectorElt()) {
569 // Read/modify/write the vector, inserting the new element.
Eli Friedman327944b2008-06-13 23:01:12 +0000570 llvm::Value *Vec = Builder.CreateLoad(Dst.getVectorAddr(),
571 Dst.isVolatileQualified(), "tmp");
Chris Lattner4647a212007-08-31 22:49:20 +0000572 Vec = Builder.CreateInsertElement(Vec, Src.getScalarVal(),
Chris Lattner41d480e2007-08-03 16:28:33 +0000573 Dst.getVectorIdx(), "vecins");
Eli Friedman327944b2008-06-13 23:01:12 +0000574 Builder.CreateStore(Vec, Dst.getVectorAddr(),Dst.isVolatileQualified());
Chris Lattner41d480e2007-08-03 16:28:33 +0000575 return;
576 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000577
Nate Begemance4d7fc2008-04-18 23:10:10 +0000578 // If this is an update of extended vector elements, insert them as
579 // appropriate.
580 if (Dst.isExtVectorElt())
581 return EmitStoreThroughExtVectorComponentLValue(Src, Dst, Ty);
Lauro Ramos Venancio09af71c2008-01-22 22:36:45 +0000582
583 if (Dst.isBitfield())
584 return EmitStoreThroughBitfieldLValue(Src, Dst, Ty);
585
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +0000586 if (Dst.isPropertyRef())
587 return EmitStoreThroughPropertyRefLValue(Src, Dst, Ty);
588
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000589 assert(Dst.isKVCRef() && "Unknown LValue type");
590 return EmitStoreThroughKVCRefLValue(Src, Dst, Ty);
Chris Lattner41d480e2007-08-03 16:28:33 +0000591 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000592
Fariborz Jahanian10bec102009-02-21 00:30:43 +0000593 if (Dst.isObjCWeak() && !Dst.isNonGC()) {
Mike Stump4a3999f2009-09-09 13:00:44 +0000594 // load of a __weak object.
Fariborz Jahanian50a12702008-11-19 17:34:06 +0000595 llvm::Value *LvalueDst = Dst.getAddress();
596 llvm::Value *src = Src.getScalarVal();
Mike Stumpca5ae662009-04-14 00:57:29 +0000597 CGM.getObjCRuntime().EmitObjCWeakAssign(*this, src, LvalueDst);
Fariborz Jahanian50a12702008-11-19 17:34:06 +0000598 return;
599 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000600
Fariborz Jahanian10bec102009-02-21 00:30:43 +0000601 if (Dst.isObjCStrong() && !Dst.isNonGC()) {
Mike Stump4a3999f2009-09-09 13:00:44 +0000602 // load of a __strong object.
Fariborz Jahanian50a12702008-11-19 17:34:06 +0000603 llvm::Value *LvalueDst = Dst.getAddress();
604 llvm::Value *src = Src.getScalarVal();
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000605 if (Dst.isObjCIvar()) {
606 assert(Dst.getBaseIvarExp() && "BaseIvarExp is NULL");
607 const llvm::Type *ResultType = ConvertType(getContext().LongTy);
608 llvm::Value *RHS = EmitScalarExpr(Dst.getBaseIvarExp());
Fariborz Jahanian1f9ed582009-09-25 00:00:20 +0000609 llvm::Value *dst = RHS;
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000610 RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast");
611 llvm::Value *LHS =
612 Builder.CreatePtrToInt(LvalueDst, ResultType, "sub.ptr.lhs.cast");
613 llvm::Value *BytesBetween = Builder.CreateSub(LHS, RHS, "ivar.offset");
Fariborz Jahanian1f9ed582009-09-25 00:00:20 +0000614 CGM.getObjCRuntime().EmitObjCIvarAssign(*this, src, dst,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000615 BytesBetween);
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000616 } else if (Dst.isGlobalObjCRef())
Fariborz Jahanian32ff7ae2009-05-04 23:27:20 +0000617 CGM.getObjCRuntime().EmitObjCGlobalAssign(*this, src, LvalueDst);
618 else
619 CGM.getObjCRuntime().EmitObjCStrongCastAssign(*this, src, LvalueDst);
Fariborz Jahanian50a12702008-11-19 17:34:06 +0000620 return;
621 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000622
Chris Lattner6278e6a2007-08-11 00:04:45 +0000623 assert(Src.isScalar() && "Can't emit an agg store with this method");
Anders Carlsson83709642009-05-19 18:50:41 +0000624 EmitStoreOfScalar(Src.getScalarVal(), Dst.getAddress(),
625 Dst.isVolatileQualified(), Ty);
Chris Lattner8394d792007-06-05 20:53:16 +0000626}
627
Lauro Ramos Venancio09af71c2008-01-22 22:36:45 +0000628void CodeGenFunction::EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst,
Mike Stump4a3999f2009-09-09 13:00:44 +0000629 QualType Ty,
Daniel Dunbar9b1335e2008-11-19 09:36:46 +0000630 llvm::Value **Result) {
Daniel Dunbaread7c912008-08-06 05:08:45 +0000631 unsigned StartBit = Dst.getBitfieldStartBit();
632 unsigned BitfieldSize = Dst.getBitfieldSize();
Lauro Ramos Venancio09af71c2008-01-22 22:36:45 +0000633 llvm::Value *Ptr = Dst.getBitfieldAddr();
Lauro Ramos Venancio09af71c2008-01-22 22:36:45 +0000634
Mike Stump4a3999f2009-09-09 13:00:44 +0000635 const llvm::Type *EltTy =
Daniel Dunbaread7c912008-08-06 05:08:45 +0000636 cast<llvm::PointerType>(Ptr->getType())->getElementType();
637 unsigned EltTySize = CGM.getTargetData().getTypeSizeInBits(EltTy);
638
Mike Stump4a3999f2009-09-09 13:00:44 +0000639 // Get the new value, cast to the appropriate type and masked to exactly the
640 // size of the bit-field.
Daniel Dunbar9b1335e2008-11-19 09:36:46 +0000641 llvm::Value *SrcVal = Src.getScalarVal();
642 llvm::Value *NewVal = Builder.CreateIntCast(SrcVal, EltTy, false, "tmp");
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000643 llvm::Constant *Mask = llvm::ConstantInt::get(VMContext,
644 llvm::APInt::getLowBitsSet(EltTySize, BitfieldSize));
Daniel Dunbaread7c912008-08-06 05:08:45 +0000645 NewVal = Builder.CreateAnd(NewVal, Mask, "bf.value");
Lauro Ramos Venancio09af71c2008-01-22 22:36:45 +0000646
Daniel Dunbar9b1335e2008-11-19 09:36:46 +0000647 // Return the new value of the bit-field, if requested.
648 if (Result) {
649 // Cast back to the proper type for result.
650 const llvm::Type *SrcTy = SrcVal->getType();
651 llvm::Value *SrcTrunc = Builder.CreateIntCast(NewVal, SrcTy, false,
652 "bf.reload.val");
653
654 // Sign extend if necessary.
655 if (Dst.isBitfieldSigned()) {
656 unsigned SrcTySize = CGM.getTargetData().getTypeSizeInBits(SrcTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000657 llvm::Value *ExtraBits = llvm::ConstantInt::get(SrcTy,
Daniel Dunbar9b1335e2008-11-19 09:36:46 +0000658 SrcTySize - BitfieldSize);
Mike Stump4a3999f2009-09-09 13:00:44 +0000659 SrcTrunc = Builder.CreateAShr(Builder.CreateShl(SrcTrunc, ExtraBits),
Daniel Dunbar9b1335e2008-11-19 09:36:46 +0000660 ExtraBits, "bf.reload.sext");
661 }
662
663 *Result = SrcTrunc;
664 }
665
Mike Stump4a3999f2009-09-09 13:00:44 +0000666 // In some cases the bitfield may straddle two memory locations. Emit the low
667 // part first and check to see if the high needs to be done.
Daniel Dunbaread7c912008-08-06 05:08:45 +0000668 unsigned LowBits = std::min(BitfieldSize, EltTySize - StartBit);
669 llvm::Value *LowVal = Builder.CreateLoad(Ptr, Dst.isVolatileQualified(),
670 "bf.prev.low");
Eli Friedmanf2442dc2008-05-17 20:03:47 +0000671
Daniel Dunbaread7c912008-08-06 05:08:45 +0000672 // Compute the mask for zero-ing the low part of this bitfield.
Mike Stump4a3999f2009-09-09 13:00:44 +0000673 llvm::Constant *InvMask =
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000674 llvm::ConstantInt::get(VMContext,
675 ~llvm::APInt::getBitsSet(EltTySize, StartBit, StartBit + LowBits));
Mike Stump4a3999f2009-09-09 13:00:44 +0000676
Daniel Dunbaread7c912008-08-06 05:08:45 +0000677 // Compute the new low part as
678 // LowVal = (LowVal & InvMask) | (NewVal << StartBit),
679 // with the shift of NewVal implicitly stripping the high bits.
Mike Stump4a3999f2009-09-09 13:00:44 +0000680 llvm::Value *NewLowVal =
Chris Lattner72ecc682009-12-06 17:22:42 +0000681 Builder.CreateShl(NewVal, StartBit, "bf.value.lo");
Daniel Dunbaread7c912008-08-06 05:08:45 +0000682 LowVal = Builder.CreateAnd(LowVal, InvMask, "bf.prev.lo.cleared");
683 LowVal = Builder.CreateOr(LowVal, NewLowVal, "bf.new.lo");
Mike Stump4a3999f2009-09-09 13:00:44 +0000684
Daniel Dunbaread7c912008-08-06 05:08:45 +0000685 // Write back.
686 Builder.CreateStore(LowVal, Ptr, Dst.isVolatileQualified());
Eli Friedmanf2442dc2008-05-17 20:03:47 +0000687
Daniel Dunbaread7c912008-08-06 05:08:45 +0000688 // If the low part doesn't cover the bitfield emit a high part.
689 if (LowBits < BitfieldSize) {
690 unsigned HighBits = BitfieldSize - LowBits;
Owen Anderson41a75022009-08-13 21:57:51 +0000691 llvm::Value *HighPtr = Builder.CreateGEP(Ptr, llvm::ConstantInt::get(
Mike Stump4a3999f2009-09-09 13:00:44 +0000692 llvm::Type::getInt32Ty(VMContext), 1), "bf.ptr.hi");
693 llvm::Value *HighVal = Builder.CreateLoad(HighPtr,
Daniel Dunbaread7c912008-08-06 05:08:45 +0000694 Dst.isVolatileQualified(),
695 "bf.prev.hi");
Mike Stump4a3999f2009-09-09 13:00:44 +0000696
Daniel Dunbaread7c912008-08-06 05:08:45 +0000697 // Compute the mask for zero-ing the high part of this bitfield.
Mike Stump4a3999f2009-09-09 13:00:44 +0000698 llvm::Constant *InvMask =
699 llvm::ConstantInt::get(VMContext, ~llvm::APInt::getLowBitsSet(EltTySize,
Owen Anderson170229f2009-07-14 23:10:40 +0000700 HighBits));
Mike Stump4a3999f2009-09-09 13:00:44 +0000701
Daniel Dunbaread7c912008-08-06 05:08:45 +0000702 // Compute the new high part as
703 // HighVal = (HighVal & InvMask) | (NewVal lshr LowBits),
704 // where the high bits of NewVal have already been cleared and the
705 // shift stripping the low bits.
Mike Stump4a3999f2009-09-09 13:00:44 +0000706 llvm::Value *NewHighVal =
Chris Lattner72ecc682009-12-06 17:22:42 +0000707 Builder.CreateLShr(NewVal, LowBits, "bf.value.high");
Daniel Dunbaread7c912008-08-06 05:08:45 +0000708 HighVal = Builder.CreateAnd(HighVal, InvMask, "bf.prev.hi.cleared");
709 HighVal = Builder.CreateOr(HighVal, NewHighVal, "bf.new.hi");
Mike Stump4a3999f2009-09-09 13:00:44 +0000710
Daniel Dunbaread7c912008-08-06 05:08:45 +0000711 // Write back.
712 Builder.CreateStore(HighVal, HighPtr, Dst.isVolatileQualified());
713 }
Lauro Ramos Venancio09af71c2008-01-22 22:36:45 +0000714}
715
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +0000716void CodeGenFunction::EmitStoreThroughPropertyRefLValue(RValue Src,
717 LValue Dst,
718 QualType Ty) {
719 EmitObjCPropertySet(Dst.getPropertyRefExpr(), Src);
720}
721
Fariborz Jahanian9ac53512008-11-22 22:30:21 +0000722void CodeGenFunction::EmitStoreThroughKVCRefLValue(RValue Src,
723 LValue Dst,
724 QualType Ty) {
725 EmitObjCPropertySet(Dst.getKVCRefExpr(), Src);
726}
727
Nate Begemance4d7fc2008-04-18 23:10:10 +0000728void CodeGenFunction::EmitStoreThroughExtVectorComponentLValue(RValue Src,
729 LValue Dst,
730 QualType Ty) {
Chris Lattner41d480e2007-08-03 16:28:33 +0000731 // This access turns into a read/modify/write of the vector. Load the input
732 // value now.
Eli Friedman327944b2008-06-13 23:01:12 +0000733 llvm::Value *Vec = Builder.CreateLoad(Dst.getExtVectorAddr(),
734 Dst.isVolatileQualified(), "tmp");
Nate Begemanf322eab2008-05-09 06:41:27 +0000735 const llvm::Constant *Elts = Dst.getExtVectorElts();
Mike Stump4a3999f2009-09-09 13:00:44 +0000736
Chris Lattner4647a212007-08-31 22:49:20 +0000737 llvm::Value *SrcVal = Src.getScalarVal();
Mike Stump4a3999f2009-09-09 13:00:44 +0000738
John McCall9dd450b2009-09-21 23:43:11 +0000739 if (const VectorType *VTy = Ty->getAs<VectorType>()) {
Chris Lattner3a44aa72007-08-03 16:37:04 +0000740 unsigned NumSrcElts = VTy->getNumElements();
Nate Begemanb699c9b2009-01-18 06:42:49 +0000741 unsigned NumDstElts =
742 cast<llvm::VectorType>(Vec->getType())->getNumElements();
743 if (NumDstElts == NumSrcElts) {
Mike Stump4a3999f2009-09-09 13:00:44 +0000744 // Use shuffle vector is the src and destination are the same number of
745 // elements and restore the vector mask since it is on the side it will be
746 // stored.
Nate Begemanea12f6e2009-06-26 21:12:50 +0000747 llvm::SmallVector<llvm::Constant*, 4> Mask(NumDstElts);
Nate Begemanb699c9b2009-01-18 06:42:49 +0000748 for (unsigned i = 0; i != NumSrcElts; ++i) {
749 unsigned InIdx = getAccessedFieldNo(i, Elts);
Owen Anderson41a75022009-08-13 21:57:51 +0000750 Mask[InIdx] = llvm::ConstantInt::get(
751 llvm::Type::getInt32Ty(VMContext), i);
Nate Begemanb699c9b2009-01-18 06:42:49 +0000752 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000753
Owen Anderson3cc120a2009-07-28 21:22:35 +0000754 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
Nate Begemanb699c9b2009-01-18 06:42:49 +0000755 Vec = Builder.CreateShuffleVector(SrcVal,
Owen Anderson7ec07a52009-07-30 23:11:26 +0000756 llvm::UndefValue::get(Vec->getType()),
Nate Begemanb699c9b2009-01-18 06:42:49 +0000757 MaskV, "tmp");
Mike Stump658fe022009-07-30 22:28:39 +0000758 } else if (NumDstElts > NumSrcElts) {
Nate Begemanb699c9b2009-01-18 06:42:49 +0000759 // Extended the source vector to the same length and then shuffle it
760 // into the destination.
761 // FIXME: since we're shuffling with undef, can we just use the indices
762 // into that? This could be simpler.
763 llvm::SmallVector<llvm::Constant*, 4> ExtMask;
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000764 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
Nate Begemanb699c9b2009-01-18 06:42:49 +0000765 unsigned i;
766 for (i = 0; i != NumSrcElts; ++i)
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000767 ExtMask.push_back(llvm::ConstantInt::get(Int32Ty, i));
Nate Begemanb699c9b2009-01-18 06:42:49 +0000768 for (; i != NumDstElts; ++i)
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000769 ExtMask.push_back(llvm::UndefValue::get(Int32Ty));
Owen Anderson3cc120a2009-07-28 21:22:35 +0000770 llvm::Value *ExtMaskV = llvm::ConstantVector::get(&ExtMask[0],
Nate Begemanb699c9b2009-01-18 06:42:49 +0000771 ExtMask.size());
Mike Stump4a3999f2009-09-09 13:00:44 +0000772 llvm::Value *ExtSrcVal =
Daniel Dunbar3d926cb2009-02-17 18:31:04 +0000773 Builder.CreateShuffleVector(SrcVal,
Owen Anderson7ec07a52009-07-30 23:11:26 +0000774 llvm::UndefValue::get(SrcVal->getType()),
Daniel Dunbar3d926cb2009-02-17 18:31:04 +0000775 ExtMaskV, "tmp");
Nate Begemanb699c9b2009-01-18 06:42:49 +0000776 // build identity
777 llvm::SmallVector<llvm::Constant*, 4> Mask;
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000778 for (unsigned i = 0; i != NumDstElts; ++i)
779 Mask.push_back(llvm::ConstantInt::get(Int32Ty, i));
780
Nate Begemanb699c9b2009-01-18 06:42:49 +0000781 // modify when what gets shuffled in
782 for (unsigned i = 0; i != NumSrcElts; ++i) {
783 unsigned Idx = getAccessedFieldNo(i, Elts);
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000784 Mask[Idx] = llvm::ConstantInt::get(Int32Ty, i+NumDstElts);
Nate Begemanb699c9b2009-01-18 06:42:49 +0000785 }
Owen Anderson3cc120a2009-07-28 21:22:35 +0000786 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
Nate Begemanb699c9b2009-01-18 06:42:49 +0000787 Vec = Builder.CreateShuffleVector(Vec, ExtSrcVal, MaskV, "tmp");
Mike Stump658fe022009-07-30 22:28:39 +0000788 } else {
Nate Begemanb699c9b2009-01-18 06:42:49 +0000789 // We should never shorten the vector
790 assert(0 && "unexpected shorten vector length");
Chris Lattner3a44aa72007-08-03 16:37:04 +0000791 }
792 } else {
793 // If the Src is a scalar (not a vector) it must be updating one element.
Dan Gohman75d69da2008-05-22 00:50:06 +0000794 unsigned InIdx = getAccessedFieldNo(0, Elts);
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000795 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
796 llvm::Value *Elt = llvm::ConstantInt::get(Int32Ty, InIdx);
Chris Lattner41d480e2007-08-03 16:28:33 +0000797 Vec = Builder.CreateInsertElement(Vec, SrcVal, Elt, "tmp");
Chris Lattner41d480e2007-08-03 16:28:33 +0000798 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000799
Eli Friedman327944b2008-06-13 23:01:12 +0000800 Builder.CreateStore(Vec, Dst.getExtVectorAddr(), Dst.isVolatileQualified());
Chris Lattner41d480e2007-08-03 16:28:33 +0000801}
802
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +0000803// setObjCGCLValueClass - sets class of he lvalue for the purpose of
804// generating write-barries API. It is currently a global, ivar,
805// or neither.
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000806static void setObjCGCLValueClass(const ASTContext &Ctx, const Expr *E,
807 LValue &LV) {
Fariborz Jahanian71848a32009-09-21 23:03:37 +0000808 if (Ctx.getLangOptions().getGCMode() == LangOptions::NonGC)
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +0000809 return;
810
Fariborz Jahaniande1d3242009-09-16 23:11:23 +0000811 if (isa<ObjCIvarRefExpr>(E)) {
812 LV.SetObjCIvar(LV, true);
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000813 ObjCIvarRefExpr *Exp = cast<ObjCIvarRefExpr>(const_cast<Expr*>(E));
814 LV.setBaseIvarExp(Exp->getBase());
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +0000815 LV.SetObjCArray(LV, E->getType()->isArrayType());
Fariborz Jahaniande1d3242009-09-16 23:11:23 +0000816 return;
817 }
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000818
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +0000819 if (const DeclRefExpr *Exp = dyn_cast<DeclRefExpr>(E)) {
820 if (const VarDecl *VD = dyn_cast<VarDecl>(Exp->getDecl())) {
821 if ((VD->isBlockVarDecl() && !VD->hasLocalStorage()) ||
822 VD->isFileVarDecl())
823 LV.SetGlobalObjCRef(LV, true);
824 }
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +0000825 LV.SetObjCArray(LV, E->getType()->isArrayType());
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000826 return;
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +0000827 }
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000828
829 if (const UnaryOperator *Exp = dyn_cast<UnaryOperator>(E)) {
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +0000830 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000831 return;
832 }
833
834 if (const ParenExpr *Exp = dyn_cast<ParenExpr>(E)) {
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +0000835 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
Fariborz Jahaniane01e4342009-09-30 17:10:29 +0000836 if (LV.isObjCIvar()) {
837 // If cast is to a structure pointer, follow gcc's behavior and make it
838 // a non-ivar write-barrier.
839 QualType ExpTy = E->getType();
840 if (ExpTy->isPointerType())
841 ExpTy = ExpTy->getAs<PointerType>()->getPointeeType();
842 if (ExpTy->isRecordType())
843 LV.SetObjCIvar(LV, false);
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000844 }
845 return;
Fariborz Jahaniane01e4342009-09-30 17:10:29 +0000846 }
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000847 if (const ImplicitCastExpr *Exp = dyn_cast<ImplicitCastExpr>(E)) {
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +0000848 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000849 return;
850 }
851
852 if (const CStyleCastExpr *Exp = dyn_cast<CStyleCastExpr>(E)) {
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +0000853 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000854 return;
855 }
856
857 if (const ArraySubscriptExpr *Exp = dyn_cast<ArraySubscriptExpr>(E)) {
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +0000858 setObjCGCLValueClass(Ctx, Exp->getBase(), LV);
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +0000859 if (LV.isObjCIvar() && !LV.isObjCArray())
Fariborz Jahanian2e32ddc2009-09-18 00:04:00 +0000860 // Using array syntax to assigning to what an ivar points to is not
861 // same as assigning to the ivar itself. {id *Names;} Names[i] = 0;
862 LV.SetObjCIvar(LV, false);
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +0000863 else if (LV.isGlobalObjCRef() && !LV.isObjCArray())
864 // Using array syntax to assigning to what global points to is not
865 // same as assigning to the global itself. {id *G;} G[i] = 0;
866 LV.SetGlobalObjCRef(LV, false);
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000867 return;
Fariborz Jahanian2e32ddc2009-09-18 00:04:00 +0000868 }
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000869
870 if (const MemberExpr *Exp = dyn_cast<MemberExpr>(E)) {
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +0000871 setObjCGCLValueClass(Ctx, Exp->getBase(), LV);
Fariborz Jahanian2e32ddc2009-09-18 00:04:00 +0000872 // We don't know if member is an 'ivar', but this flag is looked at
873 // only in the context of LV.isObjCIvar().
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +0000874 LV.SetObjCArray(LV, E->getType()->isArrayType());
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000875 return;
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +0000876 }
877}
878
Anders Carlssonea4c30b2009-11-07 23:06:58 +0000879static LValue EmitGlobalVarDeclLValue(CodeGenFunction &CGF,
880 const Expr *E, const VarDecl *VD) {
Daniel Dunbar7e215ea2009-11-08 09:46:46 +0000881 assert((VD->hasExternalStorage() || VD->isFileVarDecl()) &&
Anders Carlssonea4c30b2009-11-07 23:06:58 +0000882 "Var decl must have external storage or be a file var decl!");
883
884 llvm::Value *V = CGF.CGM.GetAddrOfGlobalVar(VD);
885 if (VD->getType()->isReferenceType())
886 V = CGF.Builder.CreateLoad(V, "tmp");
887 LValue LV = LValue::MakeAddr(V, CGF.MakeQualifiers(E->getType()));
888 setObjCGCLValueClass(CGF.getContext(), E, LV);
889 return LV;
890}
891
Eli Friedmand15eb34d2009-11-26 06:08:14 +0000892static LValue EmitFunctionDeclLValue(CodeGenFunction &CGF,
893 const Expr *E, const FunctionDecl *FD) {
894 llvm::Value* V = CGF.CGM.GetAddrOfFunction(FD);
895 if (!FD->hasPrototype()) {
896 if (const FunctionProtoType *Proto =
897 FD->getType()->getAs<FunctionProtoType>()) {
898 // Ugly case: for a K&R-style definition, the type of the definition
899 // isn't the same as the type of a use. Correct for this with a
900 // bitcast.
901 QualType NoProtoType =
902 CGF.getContext().getFunctionNoProtoType(Proto->getResultType());
903 NoProtoType = CGF.getContext().getPointerType(NoProtoType);
904 V = CGF.Builder.CreateBitCast(V, CGF.ConvertType(NoProtoType), "tmp");
905 }
906 }
907 return LValue::MakeAddr(V, CGF.MakeQualifiers(E->getType()));
908}
909
Chris Lattnerd7f58862007-06-02 05:24:33 +0000910LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) {
Anders Carlsson2ff6395d2009-11-07 22:53:10 +0000911 const NamedDecl *ND = E->getDecl();
Mike Stump4a3999f2009-09-09 13:00:44 +0000912
Anders Carlsson2ff6395d2009-11-07 22:53:10 +0000913 if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
Anders Carlsson2ff6395d2009-11-07 22:53:10 +0000914
915 // Check if this is a global variable.
Anders Carlssonea4c30b2009-11-07 23:06:58 +0000916 if (VD->hasExternalStorage() || VD->isFileVarDecl())
917 return EmitGlobalVarDeclLValue(*this, E, VD);
Anders Carlsson6eee9722009-11-07 22:46:42 +0000918
919 bool NonGCable = VD->hasLocalStorage() && !VD->hasAttr<BlocksAttr>();
920
921 llvm::Value *V = LocalDeclMap[VD];
922 assert(V && "DeclRefExpr not entered in LocalDeclMap?");
923
924 Qualifiers Quals = MakeQualifiers(E->getType());
925 // local variables do not get their gc attribute set.
926 // local static?
927 if (NonGCable) Quals.removeObjCGCAttr();
928
929 if (VD->hasAttr<BlocksAttr>()) {
930 V = Builder.CreateStructGEP(V, 1, "forwarding");
Daniel Dunbarc76493a2009-11-29 21:23:36 +0000931 V = Builder.CreateLoad(V);
Anders Carlsson6eee9722009-11-07 22:46:42 +0000932 V = Builder.CreateStructGEP(V, getByRefValueLLVMField(VD),
933 VD->getNameAsString());
934 }
935 if (VD->getType()->isReferenceType())
936 V = Builder.CreateLoad(V, "tmp");
Anders Carlssonea4c30b2009-11-07 23:06:58 +0000937 LValue LV = LValue::MakeAddr(V, Quals);
Anders Carlsson6eee9722009-11-07 22:46:42 +0000938 LValue::SetObjCNonGC(LV, NonGCable);
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +0000939 setObjCGCLValueClass(getContext(), E, LV);
Fariborz Jahanian003e8302008-11-20 00:15:42 +0000940 return LV;
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000941 }
942
Eli Friedmand15eb34d2009-11-26 06:08:14 +0000943 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
944 return EmitFunctionDeclLValue(*this, E, FD);
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000945
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000946 if (E->getQualifier()) {
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000947 // FIXME: the qualifier check does not seem sufficient here
Anders Carlsson2ff6395d2009-11-07 22:53:10 +0000948 return EmitPointerToDataMemberLValue(cast<FieldDecl>(ND));
Chris Lattner5696e7b2008-06-17 18:05:57 +0000949 }
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000950
Anders Carlsson2ff6395d2009-11-07 22:53:10 +0000951 assert(false && "Unhandled DeclRefExpr");
952
953 // an invalid LValue, but the assert will
954 // ensure that this point is never reached.
Chris Lattner793d10c2007-09-16 19:23:47 +0000955 return LValue();
Chris Lattnerd7f58862007-06-02 05:24:33 +0000956}
Chris Lattnere47e4402007-06-01 18:02:12 +0000957
Mike Stump1db7d042009-02-28 09:07:16 +0000958LValue CodeGenFunction::EmitBlockDeclRefLValue(const BlockDeclRefExpr *E) {
John McCall8ccfcb52009-09-24 19:53:00 +0000959 return LValue::MakeAddr(GetAddrOfBlockDecl(E), MakeQualifiers(E->getType()));
Mike Stump1db7d042009-02-28 09:07:16 +0000960}
961
Chris Lattner8394d792007-06-05 20:53:16 +0000962LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) {
963 // __extension__ doesn't affect lvalue-ness.
964 if (E->getOpcode() == UnaryOperator::Extension)
965 return EmitLValue(E->getSubExpr());
Mike Stump4a3999f2009-09-09 13:00:44 +0000966
Chris Lattner0f398c42008-07-26 22:37:01 +0000967 QualType ExprTy = getContext().getCanonicalType(E->getSubExpr()->getType());
Chris Lattner595db862007-10-30 22:53:42 +0000968 switch (E->getOpcode()) {
969 default: assert(0 && "Unknown unary operator lvalue!");
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000970 case UnaryOperator::Deref: {
971 QualType T = E->getSubExpr()->getType()->getPointeeType();
972 assert(!T.isNull() && "CodeGenFunction::EmitUnaryOpLValue: Illegal type");
Mike Stump4a3999f2009-09-09 13:00:44 +0000973
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000974 Qualifiers Quals = MakeQualifiers(T);
975 Quals.setAddressSpace(ExprTy.getAddressSpace());
John McCall8ccfcb52009-09-24 19:53:00 +0000976
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000977 LValue LV = LValue::MakeAddr(EmitScalarExpr(E->getSubExpr()), Quals);
978 // We should not generate __weak write barrier on indirect reference
979 // of a pointer to object; as in void foo (__weak id *param); *param = 0;
980 // But, we continue to generate __strong write barrier on indirect write
981 // into a pointer to object.
982 if (getContext().getLangOptions().ObjC1 &&
983 getContext().getLangOptions().getGCMode() != LangOptions::NonGC &&
984 LV.isObjCWeak())
985 LValue::SetObjCNonGC(LV, !E->isOBJCGCCandidate(getContext()));
986 return LV;
987 }
Chris Lattner595db862007-10-30 22:53:42 +0000988 case UnaryOperator::Real:
Eli Friedmana72bf0f2009-11-09 04:20:47 +0000989 case UnaryOperator::Imag: {
Chris Lattner595db862007-10-30 22:53:42 +0000990 LValue LV = EmitLValue(E->getSubExpr());
Chris Lattner3e593cd2008-03-19 05:19:41 +0000991 unsigned Idx = E->getOpcode() == UnaryOperator::Imag;
992 return LValue::MakeAddr(Builder.CreateStructGEP(LV.getAddress(),
Chris Lattner574dee62008-07-26 22:17:49 +0000993 Idx, "idx"),
John McCall8ccfcb52009-09-24 19:53:00 +0000994 MakeQualifiers(ExprTy));
Chris Lattner595db862007-10-30 22:53:42 +0000995 }
Eli Friedmana72bf0f2009-11-09 04:20:47 +0000996 case UnaryOperator::PreInc:
997 case UnaryOperator::PreDec:
998 return EmitUnsupportedLValue(E, "pre-inc/dec expression");
999 }
Chris Lattner8394d792007-06-05 20:53:16 +00001000}
1001
Chris Lattner4347e3692007-06-06 04:54:52 +00001002LValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) {
John McCall8ccfcb52009-09-24 19:53:00 +00001003 return LValue::MakeAddr(CGM.GetAddrOfConstantStringFromLiteral(E),
1004 Qualifiers());
Chris Lattner4347e3692007-06-06 04:54:52 +00001005}
1006
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00001007LValue CodeGenFunction::EmitObjCEncodeExprLValue(const ObjCEncodeExpr *E) {
John McCall8ccfcb52009-09-24 19:53:00 +00001008 return LValue::MakeAddr(CGM.GetAddrOfConstantStringFromObjCEncode(E),
1009 Qualifiers());
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00001010}
1011
1012
Daniel Dunbarb3517472008-10-17 21:58:32 +00001013LValue CodeGenFunction::EmitPredefinedFunctionName(unsigned Type) {
Anders Carlsson625bfc82007-07-21 05:21:51 +00001014 std::string GlobalVarName;
Daniel Dunbarb3517472008-10-17 21:58:32 +00001015
1016 switch (Type) {
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001017 default: assert(0 && "Invalid type");
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00001018 case PredefinedExpr::Func:
1019 GlobalVarName = "__func__.";
1020 break;
1021 case PredefinedExpr::Function:
1022 GlobalVarName = "__FUNCTION__.";
1023 break;
1024 case PredefinedExpr::PrettyFunction:
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00001025 GlobalVarName = "__PRETTY_FUNCTION__.";
1026 break;
Anders Carlsson625bfc82007-07-21 05:21:51 +00001027 }
Daniel Dunbarb3517472008-10-17 21:58:32 +00001028
Daniel Dunbar0482cfd2009-09-12 23:06:21 +00001029 llvm::StringRef FnName = CurFn->getName();
1030 if (FnName.startswith("\01"))
1031 FnName = FnName.substr(1);
1032 GlobalVarName += FnName;
1033
Anders Carlsson2fb08242009-09-08 18:24:21 +00001034 std::string FunctionName =
Mike Stump11289f42009-09-09 15:08:12 +00001035 PredefinedExpr::ComputeName(getContext(), (PredefinedExpr::IdentType)Type,
Anders Carlsson2fb08242009-09-08 18:24:21 +00001036 CurCodeDecl);
Daniel Dunbarb3517472008-10-17 21:58:32 +00001037
Mike Stump4a3999f2009-09-09 13:00:44 +00001038 llvm::Constant *C =
Daniel Dunbarb3517472008-10-17 21:58:32 +00001039 CGM.GetAddrOfConstantCString(FunctionName, GlobalVarName.c_str());
John McCall8ccfcb52009-09-24 19:53:00 +00001040 return LValue::MakeAddr(C, Qualifiers());
Daniel Dunbarb3517472008-10-17 21:58:32 +00001041}
1042
Mike Stump4a3999f2009-09-09 13:00:44 +00001043LValue CodeGenFunction::EmitPredefinedLValue(const PredefinedExpr *E) {
Daniel Dunbarb3517472008-10-17 21:58:32 +00001044 switch (E->getIdentType()) {
1045 default:
1046 return EmitUnsupportedLValue(E, "predefined expression");
1047 case PredefinedExpr::Func:
1048 case PredefinedExpr::Function:
1049 case PredefinedExpr::PrettyFunction:
1050 return EmitPredefinedFunctionName(E->getIdentType());
1051 }
Anders Carlsson625bfc82007-07-21 05:21:51 +00001052}
1053
Mike Stumpcf16d2c2009-12-15 01:22:35 +00001054llvm::BasicBlock *CodeGenFunction::getTrapBB() {
Mike Stump9a4e0122009-12-15 00:59:40 +00001055 const CodeGenOptions &GCO = CGM.getCodeGenOpts();
1056
1057 // If we are not optimzing, don't collapse all calls to trap in the function
1058 // to the same call, that way, in the debugger they can see which operation
1059 // did in fact fail. If we are optimizing, we collpase all call to trap down
1060 // to just one per function to save on codesize.
1061 if (GCO.OptimizationLevel
1062 && TrapBB)
Mike Stumpe8c3b3e2009-12-15 00:35:12 +00001063 return TrapBB;
Mike Stumpd9546382009-12-12 01:27:46 +00001064
1065 llvm::BasicBlock *Cont = 0;
1066 if (HaveInsertPoint()) {
1067 Cont = createBasicBlock("cont");
1068 EmitBranch(Cont);
1069 }
Mike Stumpe8c3b3e2009-12-15 00:35:12 +00001070 TrapBB = createBasicBlock("trap");
1071 EmitBlock(TrapBB);
1072
1073 llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::trap, 0, 0);
1074 llvm::CallInst *TrapCall = Builder.CreateCall(F);
1075 TrapCall->setDoesNotReturn();
1076 TrapCall->setDoesNotThrow();
Mike Stumpd9546382009-12-12 01:27:46 +00001077 Builder.CreateUnreachable();
1078
1079 if (Cont)
1080 EmitBlock(Cont);
Mike Stumpe8c3b3e2009-12-15 00:35:12 +00001081 return TrapBB;
Mike Stumpd9546382009-12-12 01:27:46 +00001082}
1083
Chris Lattnerd9d2fb12007-06-08 23:31:14 +00001084LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) {
Ted Kremenekc81614d2007-08-20 16:18:38 +00001085 // The index must always be an integer, which is not an aggregate. Emit it.
Chris Lattner2da04b32007-08-24 05:35:26 +00001086 llvm::Value *Idx = EmitScalarExpr(E->getIdx());
Eli Friedman07bbeca2009-06-06 19:09:26 +00001087 QualType IdxTy = E->getIdx()->getType();
1088 bool IdxSigned = IdxTy->isSignedIntegerType();
1089
Chris Lattner08c4b9f2007-07-10 21:17:59 +00001090 // If the base is a vector type, then we are forming a vector element lvalue
1091 // with this subscript.
Eli Friedman327944b2008-06-13 23:01:12 +00001092 if (E->getBase()->getType()->isVectorType()) {
Chris Lattner08c4b9f2007-07-10 21:17:59 +00001093 // Emit the vector as an lvalue to get its address.
Eli Friedman327944b2008-06-13 23:01:12 +00001094 LValue LHS = EmitLValue(E->getBase());
Ted Kremenekc81614d2007-08-20 16:18:38 +00001095 assert(LHS.isSimple() && "Can only subscript lvalue vectors here!");
Mike Stump4a3999f2009-09-09 13:00:44 +00001096 Idx = Builder.CreateIntCast(Idx,
Owen Anderson41a75022009-08-13 21:57:51 +00001097 llvm::Type::getInt32Ty(VMContext), IdxSigned, "vidx");
Eli Friedman327944b2008-06-13 23:01:12 +00001098 return LValue::MakeVectorElt(LHS.getAddress(), Idx,
John McCall8ccfcb52009-09-24 19:53:00 +00001099 E->getBase()->getType().getCVRQualifiers());
Chris Lattner08c4b9f2007-07-10 21:17:59 +00001100 }
Mike Stump4a3999f2009-09-09 13:00:44 +00001101
Ted Kremenekc81614d2007-08-20 16:18:38 +00001102 // The base must be a pointer, which is not an aggregate. Emit it.
Chris Lattner2da04b32007-08-24 05:35:26 +00001103 llvm::Value *Base = EmitScalarExpr(E->getBase());
Mike Stump4a3999f2009-09-09 13:00:44 +00001104
Ted Kremenekc81614d2007-08-20 16:18:38 +00001105 // Extend or truncate the index type to 32 or 64-bits.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001106 unsigned IdxBitwidth = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
Sanjiv Gupta47425152009-04-24 02:40:57 +00001107 if (IdxBitwidth != LLVMPointerWidth)
Owen Anderson41a75022009-08-13 21:57:51 +00001108 Idx = Builder.CreateIntCast(Idx,
1109 llvm::IntegerType::get(VMContext, LLVMPointerWidth),
Chris Lattnerd9d2fb12007-06-08 23:31:14 +00001110 IdxSigned, "idxprom");
1111
Mike Stump3f6f9fe2009-12-16 02:57:00 +00001112 // FIXME: As llvm implements the object size checking, this can come out.
Mike Stumpd9546382009-12-12 01:27:46 +00001113 if (CatchUndefined) {
Mike Stump3f6f9fe2009-12-16 02:57:00 +00001114 if (const ImplicitCastExpr *ICE=dyn_cast<ImplicitCastExpr>(E->getBase())) {
Mike Stumpd9546382009-12-12 01:27:46 +00001115 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr())) {
1116 if (ICE->getCastKind() == CastExpr::CK_ArrayToPointerDecay) {
1117 if (const ConstantArrayType *CAT
1118 = getContext().getAsConstantArrayType(DRE->getType())) {
1119 llvm::APInt Size = CAT->getSize();
1120 llvm::BasicBlock *Cont = createBasicBlock("cont");
Mike Stump590d18f2009-12-14 22:14:31 +00001121 Builder.CreateCondBr(Builder.CreateICmpULE(Idx,
Mike Stumpd9546382009-12-12 01:27:46 +00001122 llvm::ConstantInt::get(Idx->getType(), Size)),
Mike Stumpe8c3b3e2009-12-15 00:35:12 +00001123 Cont, getTrapBB());
Mike Stumpf8858af2009-12-14 20:52:00 +00001124 EmitBlock(Cont);
Mike Stumpd9546382009-12-12 01:27:46 +00001125 }
1126 }
1127 }
1128 }
1129 }
1130
Mike Stump4a3999f2009-09-09 13:00:44 +00001131 // We know that the pointer points to a type of the correct size, unless the
1132 // size is a VLA or Objective-C interface.
Daniel Dunbaref2ffbc2009-04-25 05:08:32 +00001133 llvm::Value *Address = 0;
Mike Stump4a3999f2009-09-09 13:00:44 +00001134 if (const VariableArrayType *VAT =
Anders Carlsson3d312f82008-12-21 00:11:23 +00001135 getContext().getAsVariableArrayType(E->getType())) {
Chris Lattner19efdd62009-08-14 23:43:22 +00001136 llvm::Value *VLASize = GetVLASize(VAT);
Mike Stump4a3999f2009-09-09 13:00:44 +00001137
Anders Carlsson3d312f82008-12-21 00:11:23 +00001138 Idx = Builder.CreateMul(Idx, VLASize);
Mike Stump4a3999f2009-09-09 13:00:44 +00001139
Anders Carlssone0808df2008-12-21 03:44:36 +00001140 QualType BaseType = getContext().getBaseElementType(VAT);
Mike Stump4a3999f2009-09-09 13:00:44 +00001141
Anders Carlsson3d312f82008-12-21 00:11:23 +00001142 uint64_t BaseTypeSize = getContext().getTypeSize(BaseType) / 8;
1143 Idx = Builder.CreateUDiv(Idx,
Mike Stump4a3999f2009-09-09 13:00:44 +00001144 llvm::ConstantInt::get(Idx->getType(),
Anders Carlsson3d312f82008-12-21 00:11:23 +00001145 BaseTypeSize));
Dan Gohman43b44842009-08-12 00:33:55 +00001146 Address = Builder.CreateInBoundsGEP(Base, Idx, "arrayidx");
Mike Stump4a3999f2009-09-09 13:00:44 +00001147 } else if (const ObjCInterfaceType *OIT =
Daniel Dunbaref2ffbc2009-04-25 05:08:32 +00001148 dyn_cast<ObjCInterfaceType>(E->getType())) {
Mike Stump4a3999f2009-09-09 13:00:44 +00001149 llvm::Value *InterfaceSize =
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001150 llvm::ConstantInt::get(Idx->getType(),
Daniel Dunbaref2ffbc2009-04-25 05:08:32 +00001151 getContext().getTypeSize(OIT) / 8);
Mike Stump4a3999f2009-09-09 13:00:44 +00001152
Daniel Dunbaref2ffbc2009-04-25 05:08:32 +00001153 Idx = Builder.CreateMul(Idx, InterfaceSize);
1154
Benjamin Kramerabd5b902009-10-13 10:07:13 +00001155 const llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(VMContext);
Dan Gohman43b44842009-08-12 00:33:55 +00001156 Address = Builder.CreateGEP(Builder.CreateBitCast(Base, i8PTy),
Daniel Dunbaref2ffbc2009-04-25 05:08:32 +00001157 Idx, "arrayidx");
1158 Address = Builder.CreateBitCast(Address, Base->getType());
1159 } else {
Dan Gohman43b44842009-08-12 00:33:55 +00001160 Address = Builder.CreateInBoundsGEP(Base, Idx, "arrayidx");
Anders Carlsson3d312f82008-12-21 00:11:23 +00001161 }
Mike Stump4a3999f2009-09-09 13:00:44 +00001162
Steve Naroff7cae42b2009-07-10 23:34:53 +00001163 QualType T = E->getBase()->getType()->getPointeeType();
Mike Stump4a3999f2009-09-09 13:00:44 +00001164 assert(!T.isNull() &&
Steve Naroff7cae42b2009-07-10 23:34:53 +00001165 "CodeGenFunction::EmitArraySubscriptExpr(): Illegal base type");
Mike Stump4a3999f2009-09-09 13:00:44 +00001166
John McCall8ccfcb52009-09-24 19:53:00 +00001167 Qualifiers Quals = MakeQualifiers(T);
1168 Quals.setAddressSpace(E->getBase()->getType().getAddressSpace());
1169
1170 LValue LV = LValue::MakeAddr(Address, Quals);
Fariborz Jahaniana9fecf32009-02-21 23:37:19 +00001171 if (getContext().getLangOptions().ObjC1 &&
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +00001172 getContext().getLangOptions().getGCMode() != LangOptions::NonGC) {
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001173 LValue::SetObjCNonGC(LV, !E->isOBJCGCCandidate(getContext()));
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +00001174 setObjCGCLValueClass(getContext(), E, LV);
1175 }
Fariborz Jahaniana9fecf32009-02-21 23:37:19 +00001176 return LV;
Chris Lattnerd9d2fb12007-06-08 23:31:14 +00001177}
1178
Mike Stump4a3999f2009-09-09 13:00:44 +00001179static
Owen Anderson170229f2009-07-14 23:10:40 +00001180llvm::Constant *GenerateConstantVector(llvm::LLVMContext &VMContext,
1181 llvm::SmallVector<unsigned, 4> &Elts) {
Chris Lattner4e1a3232009-12-23 21:31:11 +00001182 llvm::SmallVector<llvm::Constant*, 4> CElts;
Mike Stump4a3999f2009-09-09 13:00:44 +00001183
Nate Begemand3862152008-05-13 21:03:02 +00001184 for (unsigned i = 0, e = Elts.size(); i != e; ++i)
Owen Anderson41a75022009-08-13 21:57:51 +00001185 CElts.push_back(llvm::ConstantInt::get(
1186 llvm::Type::getInt32Ty(VMContext), Elts[i]));
Nate Begemand3862152008-05-13 21:03:02 +00001187
Owen Anderson3cc120a2009-07-28 21:22:35 +00001188 return llvm::ConstantVector::get(&CElts[0], CElts.size());
Nate Begemand3862152008-05-13 21:03:02 +00001189}
1190
Chris Lattner9e751ca2007-08-02 23:37:31 +00001191LValue CodeGenFunction::
Nate Begemance4d7fc2008-04-18 23:10:10 +00001192EmitExtVectorElementExpr(const ExtVectorElementExpr *E) {
Chris Lattner4e1a3232009-12-23 21:31:11 +00001193 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
1194
Chris Lattner9e751ca2007-08-02 23:37:31 +00001195 // Emit the base vector as an l-value.
Chris Lattner6c7ce102009-02-16 21:11:58 +00001196 LValue Base;
1197
1198 // ExtVectorElementExpr's base can either be a vector or pointer to vector.
Chris Lattner4e1a3232009-12-23 21:31:11 +00001199 if (E->isArrow()) {
1200 // If it is a pointer to a vector, emit the address and form an lvalue with
1201 // it.
Chris Lattnerb8211f62009-02-16 22:14:05 +00001202 llvm::Value *Ptr = EmitScalarExpr(E->getBase());
Chris Lattner4e1a3232009-12-23 21:31:11 +00001203 const PointerType *PT = E->getBase()->getType()->getAs<PointerType>();
John McCall8ccfcb52009-09-24 19:53:00 +00001204 Qualifiers Quals = MakeQualifiers(PT->getPointeeType());
1205 Quals.removeObjCGCAttr();
1206 Base = LValue::MakeAddr(Ptr, Quals);
Chris Lattner4e1a3232009-12-23 21:31:11 +00001207 } else if (E->getBase()->isLvalue(getContext()) == Expr::LV_Valid) {
1208 // Otherwise, if the base is an lvalue ( as in the case of foo.x.x),
1209 // emit the base as an lvalue.
1210 assert(E->getBase()->getType()->isVectorType());
1211 Base = EmitLValue(E->getBase());
1212 } else {
1213 // Otherwise, the base is a normal rvalue (as in (V+V).x), emit it as such.
1214 const VectorType *VT = E->getBase()->getType()->getAs<VectorType>();
1215 assert(VT && "Result must be a vector");
1216 llvm::Value *Vec = EmitScalarExpr(E->getBase());
1217
Chris Lattnerf0a9ba32009-12-23 21:33:41 +00001218 // Store the vector to memory (because LValue wants an address).
Chris Lattner4e1a3232009-12-23 21:31:11 +00001219 llvm::Value *VecMem =CreateTempAlloca(ConvertType(E->getBase()->getType()));
1220 Builder.CreateStore(Vec, VecMem);
Chris Lattnerf0a9ba32009-12-23 21:33:41 +00001221 Base = LValue::MakeAddr(VecMem, Qualifiers());
Chris Lattner4e1a3232009-12-23 21:31:11 +00001222 }
1223
Nate Begemand3862152008-05-13 21:03:02 +00001224 // Encode the element access list into a vector of unsigned indices.
1225 llvm::SmallVector<unsigned, 4> Indices;
1226 E->getEncodedElementAccess(Indices);
1227
1228 if (Base.isSimple()) {
Owen Anderson170229f2009-07-14 23:10:40 +00001229 llvm::Constant *CV = GenerateConstantVector(VMContext, Indices);
Eli Friedman327944b2008-06-13 23:01:12 +00001230 return LValue::MakeExtVectorElt(Base.getAddress(), CV,
John McCall8ccfcb52009-09-24 19:53:00 +00001231 Base.getVRQualifiers());
Nate Begemand3862152008-05-13 21:03:02 +00001232 }
1233 assert(Base.isExtVectorElt() && "Can only subscript lvalue vec elts here!");
1234
1235 llvm::Constant *BaseElts = Base.getExtVectorElts();
1236 llvm::SmallVector<llvm::Constant *, 4> CElts;
1237
1238 for (unsigned i = 0, e = Indices.size(); i != e; ++i) {
1239 if (isa<llvm::ConstantAggregateZero>(BaseElts))
Chris Lattner5e71d432009-10-28 05:12:07 +00001240 CElts.push_back(llvm::ConstantInt::get(Int32Ty, 0));
Nate Begemand3862152008-05-13 21:03:02 +00001241 else
Chris Lattner5e71d432009-10-28 05:12:07 +00001242 CElts.push_back(cast<llvm::Constant>(BaseElts->getOperand(Indices[i])));
Nate Begemand3862152008-05-13 21:03:02 +00001243 }
Owen Anderson3cc120a2009-07-28 21:22:35 +00001244 llvm::Constant *CV = llvm::ConstantVector::get(&CElts[0], CElts.size());
Eli Friedman327944b2008-06-13 23:01:12 +00001245 return LValue::MakeExtVectorElt(Base.getExtVectorAddr(), CV,
John McCall8ccfcb52009-09-24 19:53:00 +00001246 Base.getVRQualifiers());
Chris Lattner9e751ca2007-08-02 23:37:31 +00001247}
1248
Devang Patel30efa2e2007-10-23 20:28:39 +00001249LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) {
Devang Patelb37b12d2007-12-11 21:33:16 +00001250 bool isUnion = false;
Fariborz Jahanian10bec102009-02-21 00:30:43 +00001251 bool isNonGC = false;
Devang Pateld68df202007-10-24 22:26:28 +00001252 Expr *BaseExpr = E->getBase();
Devang Pateld68df202007-10-24 22:26:28 +00001253 llvm::Value *BaseValue = NULL;
John McCall8ccfcb52009-09-24 19:53:00 +00001254 Qualifiers BaseQuals;
Eli Friedman327944b2008-06-13 23:01:12 +00001255
Chris Lattner4e4186b2007-12-02 18:52:07 +00001256 // 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 +00001257 if (E->isArrow()) {
Devang Patel7718d7a2007-10-26 18:15:21 +00001258 BaseValue = EmitScalarExpr(BaseExpr);
Mike Stump4a3999f2009-09-09 13:00:44 +00001259 const PointerType *PTy =
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001260 BaseExpr->getType()->getAs<PointerType>();
Devang Patelb37b12d2007-12-11 21:33:16 +00001261 if (PTy->getPointeeType()->isUnionType())
1262 isUnion = true;
John McCall8ccfcb52009-09-24 19:53:00 +00001263 BaseQuals = PTy->getPointeeType().getQualifiers();
Fariborz Jahanian1a504772009-09-01 17:02:21 +00001264 } else if (isa<ObjCPropertyRefExpr>(BaseExpr->IgnoreParens()) ||
1265 isa<ObjCImplicitSetterGetterRefExpr>(
1266 BaseExpr->IgnoreParens())) {
Fariborz Jahanian30e78642009-01-12 23:27:26 +00001267 RValue RV = EmitObjCPropertyGet(BaseExpr);
1268 BaseValue = RV.getAggregateAddr();
1269 if (BaseExpr->getType()->isUnionType())
1270 isUnion = true;
John McCall8ccfcb52009-09-24 19:53:00 +00001271 BaseQuals = BaseExpr->getType().getQualifiers();
Chris Lattnere084c012009-02-16 22:25:49 +00001272 } else {
Chris Lattner4e4186b2007-12-02 18:52:07 +00001273 LValue BaseLV = EmitLValue(BaseExpr);
Fariborz Jahanian10bec102009-02-21 00:30:43 +00001274 if (BaseLV.isNonGC())
1275 isNonGC = true;
Chris Lattner4e4186b2007-12-02 18:52:07 +00001276 // FIXME: this isn't right for bitfields.
1277 BaseValue = BaseLV.getAddress();
Fariborz Jahanian82e28742009-07-29 00:44:13 +00001278 QualType BaseTy = BaseExpr->getType();
1279 if (BaseTy->isUnionType())
Devang Patelb37b12d2007-12-11 21:33:16 +00001280 isUnion = true;
John McCall8ccfcb52009-09-24 19:53:00 +00001281 BaseQuals = BaseTy.getQualifiers();
Chris Lattner4e4186b2007-12-02 18:52:07 +00001282 }
Devang Patel30efa2e2007-10-23 20:28:39 +00001283
Anders Carlssonea4c30b2009-11-07 23:06:58 +00001284 NamedDecl *ND = E->getMemberDecl();
1285 if (FieldDecl *Field = dyn_cast<FieldDecl>(ND)) {
1286 LValue LV = EmitLValueForField(BaseValue, Field, isUnion,
1287 BaseQuals.getCVRQualifiers());
1288 LValue::SetObjCNonGC(LV, isNonGC);
1289 setObjCGCLValueClass(getContext(), E, LV);
1290 return LV;
1291 }
1292
Anders Carlsson5bbdc9f2009-11-07 23:16:50 +00001293 if (VarDecl *VD = dyn_cast<VarDecl>(ND))
1294 return EmitGlobalVarDeclLValue(*this, E, VD);
Eli Friedmand15eb34d2009-11-26 06:08:14 +00001295
1296 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
1297 return EmitFunctionDeclLValue(*this, E, FD);
1298
Anders Carlssonea4c30b2009-11-07 23:06:58 +00001299 assert(false && "Unhandled member declaration!");
1300 return LValue();
Eli Friedmana62f3e12008-02-09 08:50:58 +00001301}
Devang Patel30efa2e2007-10-23 20:28:39 +00001302
Fariborz Jahanianb517e902008-12-15 20:35:07 +00001303LValue CodeGenFunction::EmitLValueForBitfield(llvm::Value* BaseValue,
Anders Carlssoncfd30122009-11-17 03:57:07 +00001304 const FieldDecl* Field,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001305 unsigned CVRQualifiers) {
Anders Carlsson8af896c2009-07-23 17:01:21 +00001306 CodeGenTypes::BitFieldInfo Info = CGM.getTypes().getBitFieldInfo(Field);
1307
Mike Stump18bb9282009-05-16 07:57:57 +00001308 // FIXME: CodeGenTypes should expose a method to get the appropriate type for
1309 // FieldTy (the appropriate type is ABI-dependent).
Mike Stump4a3999f2009-09-09 13:00:44 +00001310 const llvm::Type *FieldTy =
Daniel Dunbar3d926cb2009-02-17 18:31:04 +00001311 CGM.getTypes().ConvertTypeForMem(Field->getType());
Fariborz Jahanianb517e902008-12-15 20:35:07 +00001312 const llvm::PointerType *BaseTy =
1313 cast<llvm::PointerType>(BaseValue->getType());
1314 unsigned AS = BaseTy->getAddressSpace();
1315 BaseValue = Builder.CreateBitCast(BaseValue,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001316 llvm::PointerType::get(FieldTy, AS),
Fariborz Jahanianb517e902008-12-15 20:35:07 +00001317 "tmp");
Mike Stump4a3999f2009-09-09 13:00:44 +00001318
1319 llvm::Value *Idx =
Owen Anderson41a75022009-08-13 21:57:51 +00001320 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), Info.FieldNo);
Anders Carlsson8af896c2009-07-23 17:01:21 +00001321 llvm::Value *V = Builder.CreateGEP(BaseValue, Idx, "tmp");
Mike Stump4a3999f2009-09-09 13:00:44 +00001322
Anders Carlsson8af896c2009-07-23 17:01:21 +00001323 return LValue::MakeBitfield(V, Info.Start, Info.Size,
Fariborz Jahanianb517e902008-12-15 20:35:07 +00001324 Field->getType()->isSignedIntegerType(),
1325 Field->getType().getCVRQualifiers()|CVRQualifiers);
1326}
1327
Eli Friedmana62f3e12008-02-09 08:50:58 +00001328LValue CodeGenFunction::EmitLValueForField(llvm::Value* BaseValue,
Anders Carlssoncfd30122009-11-17 03:57:07 +00001329 const FieldDecl* Field,
Eli Friedman327944b2008-06-13 23:01:12 +00001330 bool isUnion,
Mike Stump11289f42009-09-09 15:08:12 +00001331 unsigned CVRQualifiers) {
Fariborz Jahanianb517e902008-12-15 20:35:07 +00001332 if (Field->isBitField())
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001333 return EmitLValueForBitfield(BaseValue, Field, CVRQualifiers);
Mike Stump4a3999f2009-09-09 13:00:44 +00001334
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001335 unsigned idx = CGM.getTypes().getLLVMFieldNo(Field);
Fariborz Jahanianb517e902008-12-15 20:35:07 +00001336 llvm::Value *V = Builder.CreateStructGEP(BaseValue, idx, "tmp");
Eli Friedman133e8042008-05-29 11:33:25 +00001337
Devang Pateled93c3c2007-10-26 19:42:18 +00001338 // Match union field type.
Lauro Ramos Venancio9eff02d2008-02-07 19:29:53 +00001339 if (isUnion) {
Mike Stump4a3999f2009-09-09 13:00:44 +00001340 const llvm::Type *FieldTy =
Eli Friedman327944b2008-06-13 23:01:12 +00001341 CGM.getTypes().ConvertTypeForMem(Field->getType());
Mike Stump4a3999f2009-09-09 13:00:44 +00001342 const llvm::PointerType * BaseTy =
Devang Patelffe1e212007-10-30 20:59:40 +00001343 cast<llvm::PointerType>(BaseValue->getType());
Eli Friedman9a5ffcb2008-05-21 13:24:44 +00001344 unsigned AS = BaseTy->getAddressSpace();
Mike Stump4a3999f2009-09-09 13:00:44 +00001345 V = Builder.CreateBitCast(V,
1346 llvm::PointerType::get(FieldTy, AS),
Eli Friedman9a5ffcb2008-05-21 13:24:44 +00001347 "tmp");
Devang Pateled93c3c2007-10-26 19:42:18 +00001348 }
Eli Friedmanf7f9f682009-05-30 21:09:44 +00001349 if (Field->getType()->isReferenceType())
1350 V = Builder.CreateLoad(V, "tmp");
John McCall8ccfcb52009-09-24 19:53:00 +00001351
1352 Qualifiers Quals = MakeQualifiers(Field->getType());
1353 Quals.addCVRQualifiers(CVRQualifiers);
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +00001354 // __weak attribute on a field is ignored.
John McCall8ccfcb52009-09-24 19:53:00 +00001355 if (Quals.getObjCGCAttr() == Qualifiers::Weak)
1356 Quals.removeObjCGCAttr();
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +00001357
John McCall8ccfcb52009-09-24 19:53:00 +00001358 return LValue::MakeAddr(V, Quals);
Devang Patel30efa2e2007-10-23 20:28:39 +00001359}
1360
Chris Lattner28bcf1a2009-03-18 18:28:57 +00001361LValue CodeGenFunction::EmitCompoundLiteralLValue(const CompoundLiteralExpr* E){
Eli Friedman9fd8b682008-05-13 23:18:27 +00001362 const llvm::Type *LTy = ConvertType(E->getType());
1363 llvm::Value *DeclPtr = CreateTempAlloca(LTy, ".compoundliteral");
1364
1365 const Expr* InitExpr = E->getInitializer();
John McCall8ccfcb52009-09-24 19:53:00 +00001366 LValue Result = LValue::MakeAddr(DeclPtr, MakeQualifiers(E->getType()));
Eli Friedman9fd8b682008-05-13 23:18:27 +00001367
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001368 if (E->getType()->isComplexType())
Eli Friedman9fd8b682008-05-13 23:18:27 +00001369 EmitComplexExprIntoAddr(InitExpr, DeclPtr, false);
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001370 else if (hasAggregateLLVMType(E->getType()))
Eli Friedman9fd8b682008-05-13 23:18:27 +00001371 EmitAnyExpr(InitExpr, DeclPtr, false);
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001372 else
Eli Friedman9fd8b682008-05-13 23:18:27 +00001373 EmitStoreThroughLValue(EmitAnyExpr(InitExpr), Result, E->getType());
Eli Friedman9fd8b682008-05-13 23:18:27 +00001374
1375 return Result;
1376}
1377
Anders Carlsson1450adb2009-09-15 16:35:24 +00001378LValue
1379CodeGenFunction::EmitConditionalOperatorLValue(const ConditionalOperator* E) {
1380 if (E->isLvalue(getContext()) == Expr::LV_Valid) {
1381 llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true");
1382 llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false");
1383 llvm::BasicBlock *ContBlock = createBasicBlock("cond.end");
1384
1385 llvm::Value *Cond = EvaluateExprAsBool(E->getCond());
1386 Builder.CreateCondBr(Cond, LHSBlock, RHSBlock);
1387
1388 EmitBlock(LHSBlock);
Daniel Dunbarbf1fe8c2009-03-24 02:38:23 +00001389
Anders Carlsson1450adb2009-09-15 16:35:24 +00001390 LValue LHS = EmitLValue(E->getLHS());
1391 if (!LHS.isSimple())
1392 return EmitUnsupportedLValue(E, "conditional operator");
1393
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001394 llvm::Value *Temp = CreateTempAlloca(LHS.getAddress()->getType(),"condtmp");
Anders Carlsson1450adb2009-09-15 16:35:24 +00001395 Builder.CreateStore(LHS.getAddress(), Temp);
1396 EmitBranch(ContBlock);
1397
1398 EmitBlock(RHSBlock);
1399 LValue RHS = EmitLValue(E->getRHS());
1400 if (!RHS.isSimple())
1401 return EmitUnsupportedLValue(E, "conditional operator");
1402
1403 Builder.CreateStore(RHS.getAddress(), Temp);
1404 EmitBranch(ContBlock);
1405
1406 EmitBlock(ContBlock);
1407
1408 Temp = Builder.CreateLoad(Temp, "lv");
John McCall8ccfcb52009-09-24 19:53:00 +00001409 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
Anders Carlsson1450adb2009-09-15 16:35:24 +00001410 }
1411
Daniel Dunbarbf1fe8c2009-03-24 02:38:23 +00001412 // ?: here should be an aggregate.
Mike Stump4a3999f2009-09-09 13:00:44 +00001413 assert((hasAggregateLLVMType(E->getType()) &&
Daniel Dunbarbf1fe8c2009-03-24 02:38:23 +00001414 !E->getType()->isAnyComplexType()) &&
1415 "Unexpected conditional operator!");
1416
1417 llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType()));
1418 EmitAggExpr(E, Temp, false);
1419
John McCall8ccfcb52009-09-24 19:53:00 +00001420 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
Daniel Dunbarbf1fe8c2009-03-24 02:38:23 +00001421}
1422
Mike Stump65511702009-11-16 06:50:58 +00001423/// EmitCastLValue - Casts are never lvalues unless that cast is a dynamic_cast.
1424/// If the cast is a dynamic_cast, we can have the usual lvalue result,
1425/// otherwise if a cast is needed by the code generator in an lvalue context,
1426/// then it must mean that we need the address of an aggregate in order to
1427/// access one of its fields. This can happen for all the reasons that casts
1428/// are permitted with aggregate result, including noop aggregate casts, and
1429/// cast from scalar to union.
Chris Lattner28bcf1a2009-03-18 18:28:57 +00001430LValue CodeGenFunction::EmitCastLValue(const CastExpr *E) {
Anders Carlssond95f9602009-09-12 16:16:49 +00001431 switch (E->getCastKind()) {
1432 default:
Eli Friedman8c98dff2009-11-16 05:48:01 +00001433 return EmitUnsupportedLValue(E, "unexpected cast lvalue");
1434
Mike Stump65511702009-11-16 06:50:58 +00001435 case CastExpr::CK_Dynamic: {
1436 LValue LV = EmitLValue(E->getSubExpr());
1437 llvm::Value *V = LV.getAddress();
1438 const CXXDynamicCastExpr *DCE = cast<CXXDynamicCastExpr>(E);
1439 return LValue::MakeAddr(EmitDynamicCast(V, DCE),
1440 MakeQualifiers(E->getType()));
1441 }
1442
Anders Carlssond95f9602009-09-12 16:16:49 +00001443 case CastExpr::CK_NoOp:
1444 case CastExpr::CK_ConstructorConversion:
1445 case CastExpr::CK_UserDefinedConversion:
Fariborz Jahanian2b9fc832009-12-15 21:34:52 +00001446 case CastExpr::CK_AnyPointerToObjCPointerCast:
Chris Lattner28bcf1a2009-03-18 18:28:57 +00001447 return EmitLValue(E->getSubExpr());
Anders Carlssond95f9602009-09-12 16:16:49 +00001448
1449 case CastExpr::CK_DerivedToBase: {
1450 const RecordType *DerivedClassTy =
1451 E->getSubExpr()->getType()->getAs<RecordType>();
1452 CXXRecordDecl *DerivedClassDecl =
1453 cast<CXXRecordDecl>(DerivedClassTy->getDecl());
Chris Lattner28bcf1a2009-03-18 18:28:57 +00001454
Anders Carlssond95f9602009-09-12 16:16:49 +00001455 const RecordType *BaseClassTy = E->getType()->getAs<RecordType>();
1456 CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseClassTy->getDecl());
1457
1458 LValue LV = EmitLValue(E->getSubExpr());
1459
1460 // Perform the derived-to-base conversion
1461 llvm::Value *Base =
Anders Carlsson8c793172009-11-23 17:57:54 +00001462 GetAddressOfBaseClass(LV.getAddress(), DerivedClassDecl,
1463 BaseClassDecl, /*NullCheckValue=*/false);
Anders Carlssond95f9602009-09-12 16:16:49 +00001464
John McCall8ccfcb52009-09-24 19:53:00 +00001465 return LValue::MakeAddr(Base, MakeQualifiers(E->getType()));
Anders Carlssond95f9602009-09-12 16:16:49 +00001466 }
Anders Carlssond95f9602009-09-12 16:16:49 +00001467 case CastExpr::CK_ToUnion: {
1468 llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType()));
1469 EmitAnyExpr(E->getSubExpr(), Temp, false);
Mike Stump4a3999f2009-09-09 13:00:44 +00001470
John McCall8ccfcb52009-09-24 19:53:00 +00001471 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
Anders Carlsson50cb3212009-11-14 21:21:42 +00001472 }
Eli Friedman8c98dff2009-11-16 05:48:01 +00001473 case CastExpr::CK_BaseToDerived: {
Anders Carlsson8c793172009-11-23 17:57:54 +00001474 const RecordType *BaseClassTy =
1475 E->getSubExpr()->getType()->getAs<RecordType>();
1476 CXXRecordDecl *BaseClassDecl =
1477 cast<CXXRecordDecl>(BaseClassTy->getDecl());
1478
1479 const RecordType *DerivedClassTy = E->getType()->getAs<RecordType>();
1480 CXXRecordDecl *DerivedClassDecl =
1481 cast<CXXRecordDecl>(DerivedClassTy->getDecl());
1482
1483 LValue LV = EmitLValue(E->getSubExpr());
1484
1485 // Perform the base-to-derived conversion
1486 llvm::Value *Derived =
1487 GetAddressOfDerivedClass(LV.getAddress(), BaseClassDecl,
1488 DerivedClassDecl, /*NullCheckValue=*/false);
1489
1490 return LValue::MakeAddr(Derived, MakeQualifiers(E->getType()));
Eli Friedman8c98dff2009-11-16 05:48:01 +00001491 }
Anders Carlsson50cb3212009-11-14 21:21:42 +00001492 case CastExpr::CK_BitCast: {
Eli Friedman8c98dff2009-11-16 05:48:01 +00001493 // This must be a reinterpret_cast (or c-style equivalent).
1494 const ExplicitCastExpr *CE = cast<ExplicitCastExpr>(E);
Anders Carlsson50cb3212009-11-14 21:21:42 +00001495
1496 LValue LV = EmitLValue(E->getSubExpr());
1497 llvm::Value *V = Builder.CreateBitCast(LV.getAddress(),
1498 ConvertType(CE->getTypeAsWritten()));
1499 return LValue::MakeAddr(V, MakeQualifiers(E->getType()));
1500 }
Anders Carlssond95f9602009-09-12 16:16:49 +00001501 }
Chris Lattner28bcf1a2009-03-18 18:28:57 +00001502}
1503
Fariborz Jahaniane4d94ce2009-10-20 23:29:04 +00001504LValue CodeGenFunction::EmitNullInitializationLValue(
1505 const CXXZeroInitValueExpr *E) {
1506 QualType Ty = E->getType();
1507 const llvm::Type *LTy = ConvertTypeForMem(Ty);
1508 llvm::AllocaInst *Alloc = CreateTempAlloca(LTy);
1509 unsigned Align = getContext().getTypeAlign(Ty)/8;
1510 Alloc->setAlignment(Align);
1511 LValue lvalue = LValue::MakeAddr(Alloc, Qualifiers());
1512 EmitMemSetToZero(lvalue.getAddress(), Ty);
1513 return lvalue;
1514}
1515
Chris Lattnere47e4402007-06-01 18:02:12 +00001516//===--------------------------------------------------------------------===//
1517// Expression Emission
1518//===--------------------------------------------------------------------===//
1519
Chris Lattner76ba8492007-08-20 22:37:10 +00001520
Anders Carlsson17490832009-12-24 20:40:36 +00001521RValue CodeGenFunction::EmitCallExpr(const CallExpr *E,
1522 ReturnValueSlot ReturnValue) {
Daniel Dunbarcdbb5e32009-02-20 18:06:48 +00001523 // Builtins never have block type.
Daniel Dunbarbb197e42009-01-09 16:50:52 +00001524 if (E->getCallee()->getType()->isBlockPointerType())
Anders Carlssonbfb36712009-12-24 21:13:40 +00001525 return EmitBlockCallExpr(E, ReturnValue);
Daniel Dunbarbb197e42009-01-09 16:50:52 +00001526
Anders Carlssone5fd6f22009-04-03 22:50:24 +00001527 if (const CXXMemberCallExpr *CE = dyn_cast<CXXMemberCallExpr>(E))
Anders Carlssonbfb36712009-12-24 21:13:40 +00001528 return EmitCXXMemberCallExpr(CE, ReturnValue);
Mike Stump4a3999f2009-09-09 13:00:44 +00001529
Daniel Dunbarcdbb5e32009-02-20 18:06:48 +00001530 const Decl *TargetDecl = 0;
Daniel Dunbar27032de2009-02-20 19:34:33 +00001531 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E->getCallee())) {
1532 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CE->getSubExpr())) {
1533 TargetDecl = DRE->getDecl();
1534 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(TargetDecl))
Douglas Gregor15fc9562009-09-12 00:22:50 +00001535 if (unsigned builtinID = FD->getBuiltinID())
Daniel Dunbar27032de2009-02-20 19:34:33 +00001536 return EmitBuiltinExpr(FD, builtinID, E);
Daniel Dunbarcdbb5e32009-02-20 18:06:48 +00001537 }
1538 }
1539
Chris Lattner4ca97c32009-06-13 00:26:38 +00001540 if (const CXXOperatorCallExpr *CE = dyn_cast<CXXOperatorCallExpr>(E))
Anders Carlsson4034a952009-05-27 04:18:27 +00001541 if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(TargetDecl))
Anders Carlssonbfb36712009-12-24 21:13:40 +00001542 return EmitCXXOperatorMemberCallExpr(CE, MD, ReturnValue);
Mike Stump4a3999f2009-09-09 13:00:44 +00001543
Eli Friedman8aaff692009-12-08 02:09:46 +00001544 if (isa<CXXPseudoDestructorExpr>(E->getCallee()->IgnoreParens())) {
Douglas Gregorad8a3362009-09-04 17:36:40 +00001545 // C++ [expr.pseudo]p1:
Mike Stump4a3999f2009-09-09 13:00:44 +00001546 // The result shall only be used as the operand for the function call
Douglas Gregorad8a3362009-09-04 17:36:40 +00001547 // operator (), and the result of such a call has type void. The only
1548 // effect is the evaluation of the postfix-expression before the dot or
1549 // arrow.
1550 EmitScalarExpr(E->getCallee());
1551 return RValue::get(0);
1552 }
Mike Stump4a3999f2009-09-09 13:00:44 +00001553
Chris Lattner2da04b32007-08-24 05:35:26 +00001554 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
Anders Carlsson17490832009-12-24 20:40:36 +00001555 return EmitCall(E->getCallee()->getType(), Callee, ReturnValue,
Anders Carlsson3a9463b2009-05-27 01:22:39 +00001556 E->arg_begin(), E->arg_end(), TargetDecl);
Chris Lattner9e47ead2007-08-31 04:44:06 +00001557}
1558
Daniel Dunbar8cde00a2008-09-04 03:20:13 +00001559LValue CodeGenFunction::EmitBinaryOperatorLValue(const BinaryOperator *E) {
Chris Lattnere541ea32009-05-12 21:28:12 +00001560 // Comma expressions just emit their LHS then their RHS as an l-value.
1561 if (E->getOpcode() == BinaryOperator::Comma) {
1562 EmitAnyExpr(E->getLHS());
Eli Friedman5445f6e2009-12-07 20:18:11 +00001563 EnsureInsertPoint();
Chris Lattnere541ea32009-05-12 21:28:12 +00001564 return EmitLValue(E->getRHS());
1565 }
Mike Stump4a3999f2009-09-09 13:00:44 +00001566
Fariborz Jahanian038374f2009-10-26 21:58:25 +00001567 if (E->getOpcode() == BinaryOperator::PtrMemD ||
1568 E->getOpcode() == BinaryOperator::PtrMemI)
Fariborz Jahanianffba6622009-10-22 22:57:31 +00001569 return EmitPointerToDataMemberBinaryExpr(E);
1570
Daniel Dunbar8cde00a2008-09-04 03:20:13 +00001571 // Can only get l-value for binary operator expressions which are a
1572 // simple assignment of aggregate type.
1573 if (E->getOpcode() != BinaryOperator::Assign)
1574 return EmitUnsupportedLValue(E, "binary l-value expression");
1575
Anders Carlsson0999aaf2009-10-19 18:28:22 +00001576 if (!hasAggregateLLVMType(E->getType())) {
1577 // Emit the LHS as an l-value.
1578 LValue LV = EmitLValue(E->getLHS());
1579
1580 llvm::Value *RHS = EmitScalarExpr(E->getRHS());
1581 EmitStoreOfScalar(RHS, LV.getAddress(), LV.isVolatileQualified(),
1582 E->getType());
1583 return LV;
1584 }
1585
Daniel Dunbar8cde00a2008-09-04 03:20:13 +00001586 llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType()));
1587 EmitAggExpr(E, Temp, false);
1588 // FIXME: Are these qualifiers correct?
John McCall8ccfcb52009-09-24 19:53:00 +00001589 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
Daniel Dunbar8cde00a2008-09-04 03:20:13 +00001590}
1591
Christopher Lambd91c3d42007-12-29 05:02:41 +00001592LValue CodeGenFunction::EmitCallExprLValue(const CallExpr *E) {
Christopher Lambd91c3d42007-12-29 05:02:41 +00001593 RValue RV = EmitCallExpr(E);
Anders Carlsson4ae70ff2009-05-27 01:45:47 +00001594
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001595 if (!RV.isScalar())
1596 return LValue::MakeAddr(RV.getAggregateAddr(),MakeQualifiers(E->getType()));
1597
1598 assert(E->getCallReturnType()->isReferenceType() &&
1599 "Can't have a scalar return unless the return type is a "
1600 "reference type!");
Mike Stump4a3999f2009-09-09 13:00:44 +00001601
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001602 return LValue::MakeAddr(RV.getScalarVal(), MakeQualifiers(E->getType()));
Christopher Lambd91c3d42007-12-29 05:02:41 +00001603}
1604
Daniel Dunbar8d9dc4a2009-02-11 20:59:32 +00001605LValue CodeGenFunction::EmitVAArgExprLValue(const VAArgExpr *E) {
1606 // FIXME: This shouldn't require another copy.
1607 llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType()));
1608 EmitAggExpr(E, Temp, false);
John McCall8ccfcb52009-09-24 19:53:00 +00001609 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
Daniel Dunbar8d9dc4a2009-02-11 20:59:32 +00001610}
1611
Anders Carlsson3be22e22009-05-30 23:23:33 +00001612LValue CodeGenFunction::EmitCXXConstructLValue(const CXXConstructExpr *E) {
1613 llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(E->getType()), "tmp");
1614 EmitCXXConstructExpr(Temp, E);
John McCall8ccfcb52009-09-24 19:53:00 +00001615 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
Anders Carlsson3be22e22009-05-30 23:23:33 +00001616}
1617
Anders Carlssonfd2af0c2009-05-30 23:30:54 +00001618LValue
Mike Stumpc9b231c2009-11-15 08:09:41 +00001619CodeGenFunction::EmitCXXTypeidLValue(const CXXTypeidExpr *E) {
1620 llvm::Value *Temp = EmitCXXTypeidExpr(E);
1621 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
1622}
1623
1624LValue
Anders Carlssonfd2af0c2009-05-30 23:30:54 +00001625CodeGenFunction::EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr *E) {
1626 LValue LV = EmitLValue(E->getSubExpr());
Anders Carlsson8eb93e72009-05-31 00:34:10 +00001627 PushCXXTemporary(E->getTemporary(), LV.getAddress());
Anders Carlssonfd2af0c2009-05-30 23:30:54 +00001628 return LV;
1629}
1630
Daniel Dunbarc8317a42008-08-23 10:51:21 +00001631LValue CodeGenFunction::EmitObjCMessageExprLValue(const ObjCMessageExpr *E) {
1632 // Can only get l-value for message expression returning aggregate type
1633 RValue RV = EmitObjCMessageExpr(E);
1634 // FIXME: can this be volatile?
John McCall8ccfcb52009-09-24 19:53:00 +00001635 return LValue::MakeAddr(RV.getAggregateAddr(), MakeQualifiers(E->getType()));
Daniel Dunbarc8317a42008-08-23 10:51:21 +00001636}
1637
Daniel Dunbar722f4242009-04-22 05:08:15 +00001638llvm::Value *CodeGenFunction::EmitIvarOffset(const ObjCInterfaceDecl *Interface,
Daniel Dunbar1c64e5d2008-09-24 04:00:38 +00001639 const ObjCIvarDecl *Ivar) {
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001640 return CGM.getObjCRuntime().EmitIvarOffset(*this, Interface, Ivar);
Daniel Dunbar1c64e5d2008-09-24 04:00:38 +00001641}
1642
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00001643LValue CodeGenFunction::EmitLValueForIvar(QualType ObjectTy,
1644 llvm::Value *BaseValue,
Daniel Dunbar1c64e5d2008-09-24 04:00:38 +00001645 const ObjCIvarDecl *Ivar,
1646 unsigned CVRQualifiers) {
Chris Lattnerc4688d22009-04-17 17:44:48 +00001647 return CGM.getObjCRuntime().EmitObjCValueForIvar(*this, ObjectTy, BaseValue,
Daniel Dunbar9ebf9512009-04-21 01:19:28 +00001648 Ivar, CVRQualifiers);
Daniel Dunbar1c64e5d2008-09-24 04:00:38 +00001649}
1650
1651LValue CodeGenFunction::EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E) {
Anders Carlssonc13b85a2008-08-25 01:53:23 +00001652 // FIXME: A lot of the code below could be shared with EmitMemberExpr.
1653 llvm::Value *BaseValue = 0;
1654 const Expr *BaseExpr = E->getBase();
John McCall8ccfcb52009-09-24 19:53:00 +00001655 Qualifiers BaseQuals;
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00001656 QualType ObjectTy;
Anders Carlssonc13b85a2008-08-25 01:53:23 +00001657 if (E->isArrow()) {
1658 BaseValue = EmitScalarExpr(BaseExpr);
Steve Naroff7cae42b2009-07-10 23:34:53 +00001659 ObjectTy = BaseExpr->getType()->getPointeeType();
John McCall8ccfcb52009-09-24 19:53:00 +00001660 BaseQuals = ObjectTy.getQualifiers();
Anders Carlssonc13b85a2008-08-25 01:53:23 +00001661 } else {
1662 LValue BaseLV = EmitLValue(BaseExpr);
1663 // FIXME: this isn't right for bitfields.
1664 BaseValue = BaseLV.getAddress();
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00001665 ObjectTy = BaseExpr->getType();
John McCall8ccfcb52009-09-24 19:53:00 +00001666 BaseQuals = ObjectTy.getQualifiers();
Anders Carlssonc13b85a2008-08-25 01:53:23 +00001667 }
Daniel Dunbar1c64e5d2008-09-24 04:00:38 +00001668
Fariborz Jahaniande1d3242009-09-16 23:11:23 +00001669 LValue LV =
John McCall8ccfcb52009-09-24 19:53:00 +00001670 EmitLValueForIvar(ObjectTy, BaseValue, E->getDecl(),
1671 BaseQuals.getCVRQualifiers());
Fariborz Jahaniande1d3242009-09-16 23:11:23 +00001672 setObjCGCLValueClass(getContext(), E, LV);
1673 return LV;
Chris Lattner4bd55962008-03-30 23:03:07 +00001674}
1675
Mike Stump4a3999f2009-09-09 13:00:44 +00001676LValue
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +00001677CodeGenFunction::EmitObjCPropertyRefLValue(const ObjCPropertyRefExpr *E) {
Mike Stump4a3999f2009-09-09 13:00:44 +00001678 // This is a special l-value that just issues sends when we load or store
1679 // through it.
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +00001680 return LValue::MakePropertyRef(E, E->getType().getCVRQualifiers());
1681}
1682
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001683LValue CodeGenFunction::EmitObjCKVCRefLValue(
Fariborz Jahanian9a846652009-08-20 17:02:02 +00001684 const ObjCImplicitSetterGetterRefExpr *E) {
Mike Stump4a3999f2009-09-09 13:00:44 +00001685 // This is a special l-value that just issues sends when we load or store
1686 // through it.
Fariborz Jahanian9ac53512008-11-22 22:30:21 +00001687 return LValue::MakeKVCRef(E, E->getType().getCVRQualifiers());
1688}
1689
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001690LValue CodeGenFunction::EmitObjCSuperExprLValue(const ObjCSuperExpr *E) {
Douglas Gregor8ea1f532008-11-04 14:56:14 +00001691 return EmitUnsupportedLValue(E, "use of super");
1692}
1693
Chris Lattnera4185c52009-04-25 19:35:26 +00001694LValue CodeGenFunction::EmitStmtExprLValue(const StmtExpr *E) {
Chris Lattnera4185c52009-04-25 19:35:26 +00001695 // Can only get l-value for message expression returning aggregate type
1696 RValue RV = EmitAnyExprToTemp(E);
1697 // FIXME: can this be volatile?
John McCall8ccfcb52009-09-24 19:53:00 +00001698 return LValue::MakeAddr(RV.getAggregateAddr(), MakeQualifiers(E->getType()));
Chris Lattnera4185c52009-04-25 19:35:26 +00001699}
1700
1701
Anders Carlsson509850e2009-11-07 22:00:15 +00001702LValue CodeGenFunction::EmitPointerToDataMemberLValue(const FieldDecl *Field) {
Fariborz Jahanianb25817a2009-10-21 21:01:47 +00001703 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(Field->getDeclContext());
1704 QualType NNSpecTy =
1705 getContext().getCanonicalType(
1706 getContext().getTypeDeclType(const_cast<CXXRecordDecl*>(ClassDecl)));
Fariborz Jahanian4ebdff52009-10-21 18:38:00 +00001707 NNSpecTy = getContext().getPointerType(NNSpecTy);
1708 llvm::Value *V = llvm::Constant::getNullValue(ConvertType(NNSpecTy));
Anders Carlssoncfd30122009-11-17 03:57:07 +00001709 LValue MemExpLV = EmitLValueForField(V, Field, /*isUnion=*/false,
1710 /*Qualifiers=*/0);
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001711 const llvm::Type *ResultType = ConvertType(getContext().getPointerDiffType());
1712 V = Builder.CreatePtrToInt(MemExpLV.getAddress(), ResultType, "datamember");
Anders Carlsson509850e2009-11-07 22:00:15 +00001713 return LValue::MakeAddr(V, MakeQualifiers(Field->getType()));
Fariborz Jahanian4ebdff52009-10-21 18:38:00 +00001714}
1715
Anders Carlsson0435ed52009-12-24 19:08:58 +00001716RValue CodeGenFunction::EmitCall(QualType CalleeType, llvm::Value *Callee,
Anders Carlsson17490832009-12-24 20:40:36 +00001717 ReturnValueSlot ReturnValue,
Anders Carlsson3a9463b2009-05-27 01:22:39 +00001718 CallExpr::const_arg_iterator ArgBeg,
1719 CallExpr::const_arg_iterator ArgEnd,
1720 const Decl *TargetDecl) {
Mike Stump4a3999f2009-09-09 13:00:44 +00001721 // Get the actual function type. The callee type will always be a pointer to
1722 // function type or a block pointer type.
1723 assert(CalleeType->isFunctionPointerType() &&
Anders Carlssond8db8532009-04-07 18:53:02 +00001724 "Call must have function pointer type!");
1725
John McCall6fd4c232009-10-23 08:22:42 +00001726 CalleeType = getContext().getCanonicalType(CalleeType);
1727
1728 QualType FnType = cast<PointerType>(CalleeType)->getPointeeType();
1729 QualType ResultType = cast<FunctionType>(FnType)->getResultType();
Daniel Dunbarc722b852008-08-30 03:02:31 +00001730
1731 CallArgList Args;
John McCall6fd4c232009-10-23 08:22:42 +00001732 EmitCallArgs(Args, dyn_cast<FunctionProtoType>(FnType), ArgBeg, ArgEnd);
Daniel Dunbarc722b852008-08-30 03:02:31 +00001733
Daniel Dunbarbbaeca42009-09-11 22:25:00 +00001734 // FIXME: We should not need to do this, it should be part of the function
1735 // type.
1736 unsigned CallingConvention = 0;
1737 if (const llvm::Function *F =
1738 dyn_cast<llvm::Function>(Callee->stripPointerCasts()))
1739 CallingConvention = F->getCallingConv();
1740 return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args,
1741 CallingConvention),
Anders Carlsson17490832009-12-24 20:40:36 +00001742 Callee, ReturnValue, Args, TargetDecl);
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001743}
Fariborz Jahanianffba6622009-10-22 22:57:31 +00001744
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001745LValue CodeGenFunction::
1746EmitPointerToDataMemberBinaryExpr(const BinaryOperator *E) {
Eli Friedman928a5672009-11-18 05:01:17 +00001747 llvm::Value *BaseV;
Fariborz Jahanian038374f2009-10-26 21:58:25 +00001748 if (E->getOpcode() == BinaryOperator::PtrMemI)
Eli Friedman928a5672009-11-18 05:01:17 +00001749 BaseV = EmitScalarExpr(E->getLHS());
1750 else
1751 BaseV = EmitLValue(E->getLHS()).getAddress();
Fariborz Jahanianffba6622009-10-22 22:57:31 +00001752 const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(getLLVMContext());
1753 BaseV = Builder.CreateBitCast(BaseV, i8Ty);
Eli Friedman928a5672009-11-18 05:01:17 +00001754 llvm::Value *OffsetV = EmitScalarExpr(E->getRHS());
Fariborz Jahanianffba6622009-10-22 22:57:31 +00001755 llvm::Value *AddV = Builder.CreateInBoundsGEP(BaseV, OffsetV, "add.ptr");
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001756
Fariborz Jahanianffba6622009-10-22 22:57:31 +00001757 QualType Ty = E->getRHS()->getType();
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001758 Ty = Ty->getAs<MemberPointerType>()->getPointeeType();
1759
1760 const llvm::Type *PType = ConvertType(getContext().getPointerType(Ty));
Fariborz Jahanianffba6622009-10-22 22:57:31 +00001761 AddV = Builder.CreateBitCast(AddV, PType);
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001762 return LValue::MakeAddr(AddV, MakeQualifiers(Ty));
Fariborz Jahanianffba6622009-10-22 22:57:31 +00001763}
1764