blob: 027264f25bbfa37babbdb9bd34e277e4d0d361e1 [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
Daniel Dunbard0049182010-02-16 19:44:13 +000039llvm::Value *CodeGenFunction::CreateIRTemp(QualType Ty,
40 const llvm::Twine &Name) {
41 llvm::AllocaInst *Alloc = CreateTempAlloca(ConvertType(Ty), Name);
42 // FIXME: Should we prefer the preferred type alignment here?
43 CharUnits Align = getContext().getTypeAlignInChars(Ty);
44 Alloc->setAlignment(Align.getQuantity());
45 return Alloc;
46}
47
48llvm::Value *CodeGenFunction::CreateMemTemp(QualType Ty,
49 const llvm::Twine &Name) {
Daniel Dunbara7566f12010-02-09 02:48:28 +000050 llvm::AllocaInst *Alloc = CreateTempAlloca(ConvertTypeForMem(Ty), Name);
51 // FIXME: Should we prefer the preferred type alignment here?
52 CharUnits Align = getContext().getTypeAlignInChars(Ty);
53 Alloc->setAlignment(Align.getQuantity());
54 return Alloc;
55}
56
Chris Lattner8394d792007-06-05 20:53:16 +000057/// EvaluateExprAsBool - Perform the usual unary conversions on the specified
58/// expression and compare the result against zero, returning an Int1Ty value.
Chris Lattner23b7eb62007-06-15 23:05:46 +000059llvm::Value *CodeGenFunction::EvaluateExprAsBool(const Expr *E) {
Chris Lattner268fcce2007-08-26 16:46:58 +000060 QualType BoolTy = getContext().BoolTy;
Eli Friedman68396b12009-12-11 09:26:29 +000061 if (E->getType()->isMemberFunctionPointerType()) {
Daniel Dunbard0bc7b92010-02-05 19:38:31 +000062 LValue LV = EmitAggExprToLValue(E);
Eli Friedman68396b12009-12-11 09:26:29 +000063
64 // Get the pointer.
Daniel Dunbard0bc7b92010-02-05 19:38:31 +000065 llvm::Value *FuncPtr = Builder.CreateStructGEP(LV.getAddress(), 0,
66 "src.ptr");
Eli Friedman68396b12009-12-11 09:26:29 +000067 FuncPtr = Builder.CreateLoad(FuncPtr);
68
69 llvm::Value *IsNotNull =
70 Builder.CreateICmpNE(FuncPtr,
71 llvm::Constant::getNullValue(FuncPtr->getType()),
72 "tobool");
73
74 return IsNotNull;
75 }
Chris Lattnerf3bc75a2008-04-04 16:54:41 +000076 if (!E->getType()->isAnyComplexType())
Chris Lattner268fcce2007-08-26 16:46:58 +000077 return EmitScalarConversion(EmitScalarExpr(E), E->getType(), BoolTy);
Chris Lattner8394d792007-06-05 20:53:16 +000078
Chris Lattner268fcce2007-08-26 16:46:58 +000079 return EmitComplexToScalarConversion(EmitComplexExpr(E), E->getType(),BoolTy);
Chris Lattnerf0106d22007-06-02 19:33:17 +000080}
81
Chris Lattner4647a212007-08-31 22:49:20 +000082/// EmitAnyExpr - Emit code to compute the specified expression which can have
83/// any type. The result is returned as an RValue struct. If this is an
Mike Stump4a3999f2009-09-09 13:00:44 +000084/// aggregate expression, the aggloc/agglocvolatile arguments indicate where the
85/// result should be returned.
86RValue CodeGenFunction::EmitAnyExpr(const Expr *E, llvm::Value *AggLoc,
Anders Carlsson5b106a72009-08-16 07:36:22 +000087 bool IsAggLocVolatile, bool IgnoreResult,
88 bool IsInitializer) {
Chris Lattner4647a212007-08-31 22:49:20 +000089 if (!hasAggregateLLVMType(E->getType()))
Mike Stumpdf0fe272009-05-29 15:46:01 +000090 return RValue::get(EmitScalarExpr(E, IgnoreResult));
Chris Lattnerf3bc75a2008-04-04 16:54:41 +000091 else if (E->getType()->isAnyComplexType())
Mike Stumpdf0fe272009-05-29 15:46:01 +000092 return RValue::getComplex(EmitComplexExpr(E, false, false,
93 IgnoreResult, IgnoreResult));
Mike Stump4a3999f2009-09-09 13:00:44 +000094
Anders Carlsson5b106a72009-08-16 07:36:22 +000095 EmitAggExpr(E, AggLoc, IsAggLocVolatile, IgnoreResult, IsInitializer);
96 return RValue::getAggregate(AggLoc, IsAggLocVolatile);
Chris Lattner4647a212007-08-31 22:49:20 +000097}
98
Mike Stump4a3999f2009-09-09 13:00:44 +000099/// EmitAnyExprToTemp - Similary to EmitAnyExpr(), however, the result will
100/// always be accessible even if no aggregate location is provided.
101RValue CodeGenFunction::EmitAnyExprToTemp(const Expr *E,
Anders Carlsson5b106a72009-08-16 07:36:22 +0000102 bool IsAggLocVolatile,
103 bool IsInitializer) {
104 llvm::Value *AggLoc = 0;
Mike Stump4a3999f2009-09-09 13:00:44 +0000105
106 if (hasAggregateLLVMType(E->getType()) &&
Daniel Dunbar41cf9de2008-09-09 01:06:48 +0000107 !E->getType()->isAnyComplexType())
John McCall7538eec2010-02-15 01:23:36 +0000108 AggLoc = CreateMemTemp(E->getType(), "agg.tmp");
Mike Stump4a3999f2009-09-09 13:00:44 +0000109 return EmitAnyExpr(E, AggLoc, IsAggLocVolatile, /*IgnoreResult=*/false,
Anders Carlsson5b106a72009-08-16 07:36:22 +0000110 IsInitializer);
Daniel Dunbar41cf9de2008-09-09 01:06:48 +0000111}
112
Anders Carlsson6f5a0152009-05-20 00:24:07 +0000113RValue CodeGenFunction::EmitReferenceBindingToExpr(const Expr* E,
Anders Carlsson5b106a72009-08-16 07:36:22 +0000114 bool IsInitializer) {
Anders Carlsson69c2c4b2009-10-18 23:09:21 +0000115 bool ShouldDestroyTemporaries = false;
116 unsigned OldNumLiveTemporaries = 0;
Eli Friedman357e8c92009-12-19 00:20:10 +0000117
118 if (const CXXDefaultArgExpr *DAE = dyn_cast<CXXDefaultArgExpr>(E))
119 E = DAE->getExpr();
120
Anders Carlsson66413c22009-10-15 00:51:46 +0000121 if (const CXXExprWithTemporaries *TE = dyn_cast<CXXExprWithTemporaries>(E)) {
Anders Carlsson6e997b22009-12-15 20:51:39 +0000122 ShouldDestroyTemporaries = true;
123
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000124 // Keep track of the current cleanup stack depth.
Anders Carlsson6e997b22009-12-15 20:51:39 +0000125 OldNumLiveTemporaries = LiveTemporaries.size();
Anders Carlsson66413c22009-10-15 00:51:46 +0000126
Anders Carlsson69c2c4b2009-10-18 23:09:21 +0000127 E = TE->getSubExpr();
Anders Carlsson66413c22009-10-15 00:51:46 +0000128 }
129
Eli Friedmanc21cb442009-05-20 02:31:19 +0000130 RValue Val;
131 if (E->isLvalue(getContext()) == Expr::LV_Valid) {
Anders Carlsson7d4c0832009-05-20 00:36:58 +0000132 // Emit the expr as an lvalue.
133 LValue LV = EmitLValue(E);
Anders Carlsson824e0612010-02-04 17:32:58 +0000134 if (LV.isSimple()) {
135 if (ShouldDestroyTemporaries) {
136 // Pop temporaries.
137 while (LiveTemporaries.size() > OldNumLiveTemporaries)
138 PopCXXTemporary();
139 }
140
Eli Friedmanc21cb442009-05-20 02:31:19 +0000141 return RValue::get(LV.getAddress());
Anders Carlsson824e0612010-02-04 17:32:58 +0000142 }
143
Eli Friedmanc21cb442009-05-20 02:31:19 +0000144 Val = EmitLoadOfLValue(LV, E->getType());
Anders Carlsson69c2c4b2009-10-18 23:09:21 +0000145
146 if (ShouldDestroyTemporaries) {
147 // Pop temporaries.
148 while (LiveTemporaries.size() > OldNumLiveTemporaries)
149 PopCXXTemporary();
150 }
Eli Friedmanc21cb442009-05-20 02:31:19 +0000151 } else {
Anders Carlsson66413c22009-10-15 00:51:46 +0000152 const CXXRecordDecl *BaseClassDecl = 0;
153 const CXXRecordDecl *DerivedClassDecl = 0;
154
155 if (const CastExpr *CE =
156 dyn_cast<CastExpr>(E->IgnoreParenNoopCasts(getContext()))) {
157 if (CE->getCastKind() == CastExpr::CK_DerivedToBase) {
158 E = CE->getSubExpr();
159
160 BaseClassDecl =
161 cast<CXXRecordDecl>(CE->getType()->getAs<RecordType>()->getDecl());
162 DerivedClassDecl =
163 cast<CXXRecordDecl>(E->getType()->getAs<RecordType>()->getDecl());
164 }
165 }
166
Anders Carlsson5b106a72009-08-16 07:36:22 +0000167 Val = EmitAnyExprToTemp(E, /*IsAggLocVolatile=*/false,
168 IsInitializer);
Mike Stump4a3999f2009-09-09 13:00:44 +0000169
Anders Carlsson69c2c4b2009-10-18 23:09:21 +0000170 if (ShouldDestroyTemporaries) {
171 // Pop temporaries.
172 while (LiveTemporaries.size() > OldNumLiveTemporaries)
173 PopCXXTemporary();
174 }
175
Anders Carlsson3b848942009-08-16 17:54:29 +0000176 if (IsInitializer) {
177 // We might have to destroy the temporary variable.
178 if (const RecordType *RT = E->getType()->getAs<RecordType>()) {
179 if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
180 if (!ClassDecl->hasTrivialDestructor()) {
Mike Stump4a3999f2009-09-09 13:00:44 +0000181 const CXXDestructorDecl *Dtor =
Anders Carlsson3b848942009-08-16 17:54:29 +0000182 ClassDecl->getDestructor(getContext());
Mike Stump4a3999f2009-09-09 13:00:44 +0000183
Mike Stumpaff69af2009-12-09 03:35:49 +0000184 {
Anders Carlsson0c6a7d82009-12-11 01:00:09 +0000185 DelayedCleanupBlock Scope(*this);
Mike Stumpaff69af2009-12-09 03:35:49 +0000186 EmitCXXDestructorCall(Dtor, Dtor_Complete,
187 Val.getAggregateAddr());
Anders Carlsson0c6a7d82009-12-11 01:00:09 +0000188
189 // Make sure to jump to the exit block.
190 EmitBranch(Scope.getCleanupExitBlock());
Mike Stumpaff69af2009-12-09 03:35:49 +0000191 }
192 if (Exceptions) {
193 EHCleanupBlock Cleanup(*this);
194 EmitCXXDestructorCall(Dtor, Dtor_Complete,
195 Val.getAggregateAddr());
196 }
Anders Carlsson3b848942009-08-16 17:54:29 +0000197 }
Anders Carlssonb80760b2009-08-16 17:50:25 +0000198 }
199 }
200 }
Anders Carlsson66413c22009-10-15 00:51:46 +0000201
202 // Check if need to perform the derived-to-base cast.
203 if (BaseClassDecl) {
204 llvm::Value *Derived = Val.getAggregateAddr();
Anders Carlsson66413c22009-10-15 00:51:46 +0000205 llvm::Value *Base =
Anders Carlsson8c793172009-11-23 17:57:54 +0000206 GetAddressOfBaseClass(Derived, DerivedClassDecl, BaseClassDecl,
207 /*NullCheckValue=*/false);
Anders Carlsson66413c22009-10-15 00:51:46 +0000208 return RValue::get(Base);
209 }
Anders Carlsson7d4c0832009-05-20 00:36:58 +0000210 }
Eli Friedmanc21cb442009-05-20 02:31:19 +0000211
212 if (Val.isAggregate()) {
213 Val = RValue::get(Val.getAggregateAddr());
214 } else {
Anders Carlsson02bb7f02009-05-20 01:35:03 +0000215 // Create a temporary variable that we can bind the reference to.
Daniel Dunbara7566f12010-02-09 02:48:28 +0000216 llvm::Value *Temp = CreateMemTemp(E->getType(), "reftmp");
Eli Friedmanc21cb442009-05-20 02:31:19 +0000217 if (Val.isScalar())
218 EmitStoreOfScalar(Val.getScalarVal(), Temp, false, E->getType());
219 else
220 StoreComplexToAddr(Val.getComplexVal(), Temp, false);
221 Val = RValue::get(Temp);
Anders Carlsson145eae52009-05-20 01:03:17 +0000222 }
Eli Friedmanc21cb442009-05-20 02:31:19 +0000223
224 return Val;
Anders Carlsson6f5a0152009-05-20 00:24:07 +0000225}
226
227
Mike Stump4a3999f2009-09-09 13:00:44 +0000228/// getAccessedFieldNo - Given an encoded value and a result number, return the
229/// input field number being accessed.
230unsigned CodeGenFunction::getAccessedFieldNo(unsigned Idx,
Dan Gohman75d69da2008-05-22 00:50:06 +0000231 const llvm::Constant *Elts) {
232 if (isa<llvm::ConstantAggregateZero>(Elts))
233 return 0;
Mike Stump4a3999f2009-09-09 13:00:44 +0000234
Dan Gohman75d69da2008-05-22 00:50:06 +0000235 return cast<llvm::ConstantInt>(Elts->getOperand(Idx))->getZExtValue();
236}
237
Mike Stump3f6f9fe2009-12-16 02:57:00 +0000238void CodeGenFunction::EmitCheck(llvm::Value *Address, unsigned Size) {
239 if (!CatchUndefined)
240 return;
241
242 const llvm::IntegerType *Size_tTy
243 = llvm::IntegerType::get(VMContext, LLVMPointerWidth);
244 Address = Builder.CreateBitCast(Address, PtrToInt8Ty);
245
246 const llvm::Type *ResType[] = {
247 Size_tTy
248 };
249 llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::objectsize, ResType, 1);
250 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
251 CGM.getTypes().ConvertType(CGM.getContext().IntTy));
252 // In time, people may want to control this and use a 1 here.
253 llvm::Value *Arg = llvm::ConstantInt::get(IntTy, 0);
254 llvm::Value *C = Builder.CreateCall2(F, Address, Arg);
255 llvm::BasicBlock *Cont = createBasicBlock();
256 llvm::BasicBlock *Check = createBasicBlock();
257 llvm::Value *NegativeOne = llvm::ConstantInt::get(Size_tTy, -1ULL);
258 Builder.CreateCondBr(Builder.CreateICmpEQ(C, NegativeOne), Cont, Check);
259
260 EmitBlock(Check);
261 Builder.CreateCondBr(Builder.CreateICmpUGE(C,
262 llvm::ConstantInt::get(Size_tTy, Size)),
263 Cont, getTrapBB());
264 EmitBlock(Cont);
265}
Chris Lattner4647a212007-08-31 22:49:20 +0000266
Chris Lattner116ce8f2010-01-09 21:40:03 +0000267
268llvm::Value *CodeGenFunction::
269EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV,
270 bool isInc, bool isPre) {
271 QualType ValTy = E->getSubExpr()->getType();
272 llvm::Value *InVal = EmitLoadOfLValue(LV, ValTy).getScalarVal();
273
274 int AmountVal = isInc ? 1 : -1;
275
276 if (ValTy->isPointerType() &&
277 ValTy->getAs<PointerType>()->isVariableArrayType()) {
278 // The amount of the addition/subtraction needs to account for the VLA size
279 ErrorUnsupported(E, "VLA pointer inc/dec");
280 }
281
282 llvm::Value *NextVal;
283 if (const llvm::PointerType *PT =
284 dyn_cast<llvm::PointerType>(InVal->getType())) {
285 llvm::Constant *Inc =
286 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), AmountVal);
287 if (!isa<llvm::FunctionType>(PT->getElementType())) {
288 QualType PTEE = ValTy->getPointeeType();
289 if (const ObjCInterfaceType *OIT =
290 dyn_cast<ObjCInterfaceType>(PTEE)) {
291 // Handle interface types, which are not represented with a concrete
292 // type.
293 int size = getContext().getTypeSize(OIT) / 8;
294 if (!isInc)
295 size = -size;
296 Inc = llvm::ConstantInt::get(Inc->getType(), size);
297 const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(VMContext);
298 InVal = Builder.CreateBitCast(InVal, i8Ty);
299 NextVal = Builder.CreateGEP(InVal, Inc, "add.ptr");
300 llvm::Value *lhs = LV.getAddress();
301 lhs = Builder.CreateBitCast(lhs, llvm::PointerType::getUnqual(i8Ty));
302 LV = LValue::MakeAddr(lhs, MakeQualifiers(ValTy));
303 } else
304 NextVal = Builder.CreateInBoundsGEP(InVal, Inc, "ptrincdec");
305 } else {
306 const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(VMContext);
307 NextVal = Builder.CreateBitCast(InVal, i8Ty, "tmp");
308 NextVal = Builder.CreateGEP(NextVal, Inc, "ptrincdec");
309 NextVal = Builder.CreateBitCast(NextVal, InVal->getType());
310 }
311 } else if (InVal->getType() == llvm::Type::getInt1Ty(VMContext) && isInc) {
312 // Bool++ is an interesting case, due to promotion rules, we get:
313 // Bool++ -> Bool = Bool+1 -> Bool = (int)Bool+1 ->
314 // Bool = ((int)Bool+1) != 0
315 // An interesting aspect of this is that increment is always true.
316 // Decrement does not have this property.
317 NextVal = llvm::ConstantInt::getTrue(VMContext);
318 } else if (isa<llvm::IntegerType>(InVal->getType())) {
319 NextVal = llvm::ConstantInt::get(InVal->getType(), AmountVal);
320
321 // Signed integer overflow is undefined behavior.
322 if (ValTy->isSignedIntegerType())
323 NextVal = Builder.CreateNSWAdd(InVal, NextVal, isInc ? "inc" : "dec");
324 else
325 NextVal = Builder.CreateAdd(InVal, NextVal, isInc ? "inc" : "dec");
326 } else {
327 // Add the inc/dec to the real part.
328 if (InVal->getType()->isFloatTy())
329 NextVal =
330 llvm::ConstantFP::get(VMContext,
331 llvm::APFloat(static_cast<float>(AmountVal)));
332 else if (InVal->getType()->isDoubleTy())
333 NextVal =
334 llvm::ConstantFP::get(VMContext,
335 llvm::APFloat(static_cast<double>(AmountVal)));
336 else {
337 llvm::APFloat F(static_cast<float>(AmountVal));
338 bool ignored;
339 F.convert(Target.getLongDoubleFormat(), llvm::APFloat::rmTowardZero,
340 &ignored);
341 NextVal = llvm::ConstantFP::get(VMContext, F);
342 }
343 NextVal = Builder.CreateFAdd(InVal, NextVal, isInc ? "inc" : "dec");
344 }
345
346 // Store the updated result through the lvalue.
347 if (LV.isBitfield())
348 EmitStoreThroughBitfieldLValue(RValue::get(NextVal), LV, ValTy, &NextVal);
349 else
350 EmitStoreThroughLValue(RValue::get(NextVal), LV, ValTy);
351
352 // If this is a postinc, return the value read from memory, otherwise use the
353 // updated value.
354 return isPre ? NextVal : InVal;
355}
356
357
358CodeGenFunction::ComplexPairTy CodeGenFunction::
359EmitComplexPrePostIncDec(const UnaryOperator *E, LValue LV,
360 bool isInc, bool isPre) {
361 ComplexPairTy InVal = LoadComplexFromAddr(LV.getAddress(),
362 LV.isVolatileQualified());
363
364 llvm::Value *NextVal;
365 if (isa<llvm::IntegerType>(InVal.first->getType())) {
366 uint64_t AmountVal = isInc ? 1 : -1;
367 NextVal = llvm::ConstantInt::get(InVal.first->getType(), AmountVal, true);
368
369 // Add the inc/dec to the real part.
370 NextVal = Builder.CreateAdd(InVal.first, NextVal, isInc ? "inc" : "dec");
371 } else {
372 QualType ElemTy = E->getType()->getAs<ComplexType>()->getElementType();
373 llvm::APFloat FVal(getContext().getFloatTypeSemantics(ElemTy), 1);
374 if (!isInc)
375 FVal.changeSign();
376 NextVal = llvm::ConstantFP::get(getLLVMContext(), FVal);
377
378 // Add the inc/dec to the real part.
379 NextVal = Builder.CreateFAdd(InVal.first, NextVal, isInc ? "inc" : "dec");
380 }
381
382 ComplexPairTy IncVal(NextVal, InVal.second);
383
384 // Store the updated result through the lvalue.
385 StoreComplexToAddr(IncVal, LV.getAddress(), LV.isVolatileQualified());
386
387 // If this is a postinc, return the value read from memory, otherwise use the
388 // updated value.
389 return isPre ? IncVal : InVal;
390}
391
392
Chris Lattnera45c5af2007-06-02 19:47:04 +0000393//===----------------------------------------------------------------------===//
Chris Lattnerd7f58862007-06-02 05:24:33 +0000394// LValue Expression Emission
Chris Lattnera45c5af2007-06-02 19:47:04 +0000395//===----------------------------------------------------------------------===//
Chris Lattnerd7f58862007-06-02 05:24:33 +0000396
Daniel Dunbarc79407f2009-02-05 07:09:07 +0000397RValue CodeGenFunction::GetUndefRValue(QualType Ty) {
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000398 if (Ty->isVoidType())
Daniel Dunbarc79407f2009-02-05 07:09:07 +0000399 return RValue::get(0);
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000400
401 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
Daniel Dunbar8429dbc2009-01-09 20:09:28 +0000402 const llvm::Type *EltTy = ConvertType(CTy->getElementType());
Owen Anderson7ec07a52009-07-30 23:11:26 +0000403 llvm::Value *U = llvm::UndefValue::get(EltTy);
Daniel Dunbar8429dbc2009-01-09 20:09:28 +0000404 return RValue::getComplex(std::make_pair(U, U));
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000405 }
406
407 if (hasAggregateLLVMType(Ty)) {
Owen Anderson9793f0e2009-07-29 22:16:19 +0000408 const llvm::Type *LTy = llvm::PointerType::getUnqual(ConvertType(Ty));
Owen Anderson7ec07a52009-07-30 23:11:26 +0000409 return RValue::getAggregate(llvm::UndefValue::get(LTy));
Daniel Dunbar8429dbc2009-01-09 20:09:28 +0000410 }
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000411
412 return RValue::get(llvm::UndefValue::get(ConvertType(Ty)));
Daniel Dunbarbb197e42009-01-09 16:50:52 +0000413}
414
Daniel Dunbarc79407f2009-02-05 07:09:07 +0000415RValue CodeGenFunction::EmitUnsupportedRValue(const Expr *E,
416 const char *Name) {
417 ErrorUnsupported(E, Name);
418 return GetUndefRValue(E->getType());
419}
420
Daniel Dunbarf2e69882008-08-25 20:45:57 +0000421LValue CodeGenFunction::EmitUnsupportedLValue(const Expr *E,
422 const char *Name) {
423 ErrorUnsupported(E, Name);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000424 llvm::Type *Ty = llvm::PointerType::getUnqual(ConvertType(E->getType()));
Owen Anderson7ec07a52009-07-30 23:11:26 +0000425 return LValue::MakeAddr(llvm::UndefValue::get(Ty),
John McCall8ccfcb52009-09-24 19:53:00 +0000426 MakeQualifiers(E->getType()));
Daniel Dunbarf2e69882008-08-25 20:45:57 +0000427}
428
Mike Stump3f6f9fe2009-12-16 02:57:00 +0000429LValue CodeGenFunction::EmitCheckedLValue(const Expr *E) {
430 LValue LV = EmitLValue(E);
431 if (!isa<DeclRefExpr>(E) && !LV.isBitfield() && LV.isSimple())
432 EmitCheck(LV.getAddress(), getContext().getTypeSize(E->getType()) / 8);
433 return LV;
434}
435
Chris Lattner8394d792007-06-05 20:53:16 +0000436/// EmitLValue - Emit code to compute a designator that specifies the location
437/// of the expression.
438///
Mike Stump4a3999f2009-09-09 13:00:44 +0000439/// This can return one of two things: a simple address or a bitfield reference.
440/// In either case, the LLVM Value* in the LValue structure is guaranteed to be
441/// an LLVM pointer type.
Chris Lattner8394d792007-06-05 20:53:16 +0000442///
Mike Stump4a3999f2009-09-09 13:00:44 +0000443/// If this returns a bitfield reference, nothing about the pointee type of the
444/// LLVM value is known: For example, it may not be a pointer to an integer.
Chris Lattner8394d792007-06-05 20:53:16 +0000445///
Mike Stump4a3999f2009-09-09 13:00:44 +0000446/// If this returns a normal address, and if the lvalue's C type is fixed size,
447/// this method guarantees that the returned pointer type will point to an LLVM
448/// type of the same size of the lvalue's type. If the lvalue has a variable
449/// length type, this is not possible.
Chris Lattner8394d792007-06-05 20:53:16 +0000450///
Chris Lattnerd7f58862007-06-02 05:24:33 +0000451LValue CodeGenFunction::EmitLValue(const Expr *E) {
452 switch (E->getStmtClass()) {
Daniel Dunbarf2e69882008-08-25 20:45:57 +0000453 default: return EmitUnsupportedLValue(E, "l-value expression");
Chris Lattnerd7f58862007-06-02 05:24:33 +0000454
Fariborz Jahanian531c16f2009-12-09 23:35:29 +0000455 case Expr::ObjCIsaExprClass:
456 return EmitObjCIsaExpr(cast<ObjCIsaExpr>(E));
Mike Stump4a3999f2009-09-09 13:00:44 +0000457 case Expr::BinaryOperatorClass:
Daniel Dunbar8cde00a2008-09-04 03:20:13 +0000458 return EmitBinaryOperatorLValue(cast<BinaryOperator>(E));
Mike Stump4a3999f2009-09-09 13:00:44 +0000459 case Expr::CallExprClass:
Anders Carlssonc82555f2009-09-01 21:18:52 +0000460 case Expr::CXXMemberCallExprClass:
Douglas Gregor993603d2008-11-14 16:09:21 +0000461 case Expr::CXXOperatorCallExprClass:
462 return EmitCallExprLValue(cast<CallExpr>(E));
Daniel Dunbar8d9dc4a2009-02-11 20:59:32 +0000463 case Expr::VAArgExprClass:
464 return EmitVAArgExprLValue(cast<VAArgExpr>(E));
Mike Stump4a3999f2009-09-09 13:00:44 +0000465 case Expr::DeclRefExprClass:
Douglas Gregorc7acfdf2009-01-06 05:10:23 +0000466 return EmitDeclRefLValue(cast<DeclRefExpr>(E));
Chris Lattner946aa312007-06-05 03:59:43 +0000467 case Expr::ParenExprClass:return EmitLValue(cast<ParenExpr>(E)->getSubExpr());
Chris Lattner6307f192008-08-10 01:53:14 +0000468 case Expr::PredefinedExprClass:
469 return EmitPredefinedLValue(cast<PredefinedExpr>(E));
Chris Lattner4347e3692007-06-06 04:54:52 +0000470 case Expr::StringLiteralClass:
471 return EmitStringLiteralLValue(cast<StringLiteral>(E));
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +0000472 case Expr::ObjCEncodeExprClass:
473 return EmitObjCEncodeExprLValue(cast<ObjCEncodeExpr>(E));
Chris Lattner4bd55962008-03-30 23:03:07 +0000474
Mike Stump4a3999f2009-09-09 13:00:44 +0000475 case Expr::BlockDeclRefExprClass:
Mike Stump1db7d042009-02-28 09:07:16 +0000476 return EmitBlockDeclRefLValue(cast<BlockDeclRefExpr>(E));
477
Anders Carlsson3be22e22009-05-30 23:23:33 +0000478 case Expr::CXXTemporaryObjectExprClass:
479 case Expr::CXXConstructExprClass:
Anders Carlssonfd2af0c2009-05-30 23:30:54 +0000480 return EmitCXXConstructLValue(cast<CXXConstructExpr>(E));
481 case Expr::CXXBindTemporaryExprClass:
482 return EmitCXXBindTemporaryLValue(cast<CXXBindTemporaryExpr>(E));
Anders Carlsson96bad9a2009-09-14 01:10:45 +0000483 case Expr::CXXExprWithTemporariesClass:
484 return EmitCXXExprWithTemporariesLValue(cast<CXXExprWithTemporaries>(E));
Anders Carlsson52ce3bb2009-11-14 01:51:50 +0000485 case Expr::CXXZeroInitValueExprClass:
486 return EmitNullInitializationLValue(cast<CXXZeroInitValueExpr>(E));
487 case Expr::CXXDefaultArgExprClass:
488 return EmitLValue(cast<CXXDefaultArgExpr>(E)->getExpr());
Mike Stumpc9b231c2009-11-15 08:09:41 +0000489 case Expr::CXXTypeidExprClass:
490 return EmitCXXTypeidLValue(cast<CXXTypeidExpr>(E));
Anders Carlssonfd2af0c2009-05-30 23:30:54 +0000491
Daniel Dunbarc8317a42008-08-23 10:51:21 +0000492 case Expr::ObjCMessageExprClass:
493 return EmitObjCMessageExprLValue(cast<ObjCMessageExpr>(E));
Mike Stump4a3999f2009-09-09 13:00:44 +0000494 case Expr::ObjCIvarRefExprClass:
Chris Lattner4bd55962008-03-30 23:03:07 +0000495 return EmitObjCIvarRefLValue(cast<ObjCIvarRefExpr>(E));
Daniel Dunbarf2e69882008-08-25 20:45:57 +0000496 case Expr::ObjCPropertyRefExprClass:
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +0000497 return EmitObjCPropertyRefLValue(cast<ObjCPropertyRefExpr>(E));
Fariborz Jahanian9a846652009-08-20 17:02:02 +0000498 case Expr::ObjCImplicitSetterGetterRefExprClass:
499 return EmitObjCKVCRefLValue(cast<ObjCImplicitSetterGetterRefExpr>(E));
Douglas Gregor8ea1f532008-11-04 14:56:14 +0000500 case Expr::ObjCSuperExprClass:
Chris Lattnera4185c52009-04-25 19:35:26 +0000501 return EmitObjCSuperExprLValue(cast<ObjCSuperExpr>(E));
Douglas Gregor8ea1f532008-11-04 14:56:14 +0000502
Chris Lattnera4185c52009-04-25 19:35:26 +0000503 case Expr::StmtExprClass:
504 return EmitStmtExprLValue(cast<StmtExpr>(E));
Mike Stump4a3999f2009-09-09 13:00:44 +0000505 case Expr::UnaryOperatorClass:
Chris Lattner8394d792007-06-05 20:53:16 +0000506 return EmitUnaryOpLValue(cast<UnaryOperator>(E));
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000507 case Expr::ArraySubscriptExprClass:
508 return EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E));
Nate Begemance4d7fc2008-04-18 23:10:10 +0000509 case Expr::ExtVectorElementExprClass:
510 return EmitExtVectorElementExpr(cast<ExtVectorElementExpr>(E));
Mike Stump4a3999f2009-09-09 13:00:44 +0000511 case Expr::MemberExprClass:
Douglas Gregorc1905232009-08-26 22:36:53 +0000512 return EmitMemberExpr(cast<MemberExpr>(E));
Eli Friedman9fd8b682008-05-13 23:18:27 +0000513 case Expr::CompoundLiteralExprClass:
514 return EmitCompoundLiteralLValue(cast<CompoundLiteralExpr>(E));
Daniel Dunbarbf1fe8c2009-03-24 02:38:23 +0000515 case Expr::ConditionalOperatorClass:
Anders Carlsson1450adb2009-09-15 16:35:24 +0000516 return EmitConditionalOperatorLValue(cast<ConditionalOperator>(E));
Chris Lattner053441f2008-12-12 05:35:08 +0000517 case Expr::ChooseExprClass:
Eli Friedmane0a5b8b2009-03-04 05:52:32 +0000518 return EmitLValue(cast<ChooseExpr>(E)->getChosenSubExpr(getContext()));
Chris Lattner63d06ab2009-03-18 04:02:57 +0000519 case Expr::ImplicitCastExprClass:
520 case Expr::CStyleCastExprClass:
521 case Expr::CXXFunctionalCastExprClass:
522 case Expr::CXXStaticCastExprClass:
523 case Expr::CXXDynamicCastExprClass:
524 case Expr::CXXReinterpretCastExprClass:
525 case Expr::CXXConstCastExprClass:
Chris Lattner28bcf1a2009-03-18 18:28:57 +0000526 return EmitCastLValue(cast<CastExpr>(E));
Chris Lattnerd7f58862007-06-02 05:24:33 +0000527 }
528}
529
Daniel Dunbar1d425462009-02-10 00:57:50 +0000530llvm::Value *CodeGenFunction::EmitLoadOfScalar(llvm::Value *Addr, bool Volatile,
531 QualType Ty) {
Daniel Dunbarc76493a2009-11-29 21:23:36 +0000532 llvm::LoadInst *Load = Builder.CreateLoad(Addr, "tmp");
533 if (Volatile)
534 Load->setVolatile(true);
Daniel Dunbar1d425462009-02-10 00:57:50 +0000535
Anders Carlsson29a1be32009-05-19 19:36:19 +0000536 // Bool can have different representation in memory than in registers.
Daniel Dunbarc76493a2009-11-29 21:23:36 +0000537 llvm::Value *V = Load;
Daniel Dunbar1d425462009-02-10 00:57:50 +0000538 if (Ty->isBooleanType())
Owen Anderson41a75022009-08-13 21:57:51 +0000539 if (V->getType() != llvm::Type::getInt1Ty(VMContext))
540 V = Builder.CreateTrunc(V, llvm::Type::getInt1Ty(VMContext), "tobool");
Mike Stump4a3999f2009-09-09 13:00:44 +0000541
Daniel Dunbar1d425462009-02-10 00:57:50 +0000542 return V;
543}
544
545void CodeGenFunction::EmitStoreOfScalar(llvm::Value *Value, llvm::Value *Addr,
Anders Carlsson83709642009-05-19 18:50:41 +0000546 bool Volatile, QualType Ty) {
Mike Stump4a3999f2009-09-09 13:00:44 +0000547
Anders Carlsson29a1be32009-05-19 19:36:19 +0000548 if (Ty->isBooleanType()) {
549 // Bool can have different representation in memory than in registers.
Anders Carlsson29a1be32009-05-19 19:36:19 +0000550 const llvm::PointerType *DstPtr = cast<llvm::PointerType>(Addr->getType());
Eli Friedmanb2b120f2009-12-01 22:31:51 +0000551 Value = Builder.CreateIntCast(Value, DstPtr->getElementType(), false);
Daniel Dunbar1d425462009-02-10 00:57:50 +0000552 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000553 Builder.CreateStore(Value, Addr, Volatile);
Daniel Dunbar1d425462009-02-10 00:57:50 +0000554}
555
Mike Stump4a3999f2009-09-09 13:00:44 +0000556/// EmitLoadOfLValue - Given an expression that represents a value lvalue, this
557/// method emits the address of the lvalue, then loads the result as an rvalue,
558/// returning the rvalue.
Chris Lattner9369a562007-06-29 16:31:29 +0000559RValue CodeGenFunction::EmitLoadOfLValue(LValue LV, QualType ExprType) {
Fariborz Jahanian50a12702008-11-19 17:34:06 +0000560 if (LV.isObjCWeak()) {
Mike Stump4a3999f2009-09-09 13:00:44 +0000561 // load of a __weak object.
Fariborz Jahanianf5125d12008-11-18 21:45:40 +0000562 llvm::Value *AddrWeakObj = LV.getAddress();
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000563 return RValue::get(CGM.getObjCRuntime().EmitObjCWeakRead(*this,
564 AddrWeakObj));
Fariborz Jahanianf5125d12008-11-18 21:45:40 +0000565 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000566
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000567 if (LV.isSimple()) {
568 llvm::Value *Ptr = LV.getAddress();
Douglas Gregora6437802010-02-05 21:10:36 +0000569 const llvm::Type *EltTy =
570 cast<llvm::PointerType>(Ptr->getType())->getElementType();
Mike Stump4a3999f2009-09-09 13:00:44 +0000571
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000572 // Simple scalar l-value.
Daniel Dunbar3d33fab2010-02-08 22:53:07 +0000573 //
574 // FIXME: We shouldn't have to use isSingleValueType here.
Douglas Gregora6437802010-02-05 21:10:36 +0000575 if (EltTy->isSingleValueType())
Mike Stump4a3999f2009-09-09 13:00:44 +0000576 return RValue::get(EmitLoadOfScalar(Ptr, LV.isVolatileQualified(),
Daniel Dunbar1d425462009-02-10 00:57:50 +0000577 ExprType));
Mike Stump4a3999f2009-09-09 13:00:44 +0000578
Chris Lattner6278e6a2007-08-11 00:04:45 +0000579 assert(ExprType->isFunctionType() && "Unknown scalar value");
580 return RValue::get(Ptr);
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000581 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000582
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000583 if (LV.isVectorElt()) {
Eli Friedman327944b2008-06-13 23:01:12 +0000584 llvm::Value *Vec = Builder.CreateLoad(LV.getVectorAddr(),
585 LV.isVolatileQualified(), "tmp");
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000586 return RValue::get(Builder.CreateExtractElement(Vec, LV.getVectorIdx(),
587 "vecext"));
588 }
Chris Lattner73ab9b32007-08-03 00:16:29 +0000589
590 // If this is a reference to a subset of the elements of a vector, either
591 // shuffle the input or extract/insert them as appropriate.
Nate Begemance4d7fc2008-04-18 23:10:10 +0000592 if (LV.isExtVectorElt())
593 return EmitLoadOfExtVectorElementLValue(LV, ExprType);
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000594
595 if (LV.isBitfield())
596 return EmitLoadOfBitfieldLValue(LV, ExprType);
597
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +0000598 if (LV.isPropertyRef())
599 return EmitLoadOfPropertyRefLValue(LV, ExprType);
600
Chris Lattner6c7ce102009-02-16 21:11:58 +0000601 assert(LV.isKVCRef() && "Unknown LValue type!");
602 return EmitLoadOfKVCRefLValue(LV, ExprType);
Chris Lattner8394d792007-06-05 20:53:16 +0000603}
604
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000605RValue CodeGenFunction::EmitLoadOfBitfieldLValue(LValue LV,
606 QualType ExprType) {
Daniel Dunbaread7c912008-08-06 05:08:45 +0000607 unsigned StartBit = LV.getBitfieldStartBit();
608 unsigned BitfieldSize = LV.getBitfieldSize();
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000609 llvm::Value *Ptr = LV.getBitfieldAddr();
Daniel Dunbaread7c912008-08-06 05:08:45 +0000610
Mike Stump4a3999f2009-09-09 13:00:44 +0000611 const llvm::Type *EltTy =
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000612 cast<llvm::PointerType>(Ptr->getType())->getElementType();
Daniel Dunbaread7c912008-08-06 05:08:45 +0000613 unsigned EltTySize = CGM.getTargetData().getTypeSizeInBits(EltTy);
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000614
Mike Stump4a3999f2009-09-09 13:00:44 +0000615 // In some cases the bitfield may straddle two memory locations. Currently we
616 // load the entire bitfield, then do the magic to sign-extend it if
617 // necessary. This results in somewhat more code than necessary for the common
618 // case (one load), since two shifts accomplish both the masking and sign
619 // extension.
Daniel Dunbaread7c912008-08-06 05:08:45 +0000620 unsigned LowBits = std::min(BitfieldSize, EltTySize - StartBit);
621 llvm::Value *Val = Builder.CreateLoad(Ptr, LV.isVolatileQualified(), "tmp");
Mike Stump4a3999f2009-09-09 13:00:44 +0000622
Daniel Dunbaread7c912008-08-06 05:08:45 +0000623 // Shift to proper location.
Daniel Dunbarf7fb7502008-11-13 02:20:34 +0000624 if (StartBit)
Chris Lattner72ecc682009-12-06 17:22:42 +0000625 Val = Builder.CreateLShr(Val, StartBit, "bf.lo");
Mike Stump4a3999f2009-09-09 13:00:44 +0000626
Daniel Dunbaread7c912008-08-06 05:08:45 +0000627 // Mask off unused bits.
Mike Stump4a3999f2009-09-09 13:00:44 +0000628 llvm::Constant *LowMask = llvm::ConstantInt::get(VMContext,
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000629 llvm::APInt::getLowBitsSet(EltTySize, LowBits));
Daniel Dunbaread7c912008-08-06 05:08:45 +0000630 Val = Builder.CreateAnd(Val, LowMask, "bf.lo.cleared");
Mike Stump4a3999f2009-09-09 13:00:44 +0000631
Daniel Dunbaread7c912008-08-06 05:08:45 +0000632 // Fetch the high bits if necessary.
633 if (LowBits < BitfieldSize) {
634 unsigned HighBits = BitfieldSize - LowBits;
Owen Anderson41a75022009-08-13 21:57:51 +0000635 llvm::Value *HighPtr = Builder.CreateGEP(Ptr, llvm::ConstantInt::get(
Mike Stump4a3999f2009-09-09 13:00:44 +0000636 llvm::Type::getInt32Ty(VMContext), 1), "bf.ptr.hi");
637 llvm::Value *HighVal = Builder.CreateLoad(HighPtr,
Daniel Dunbaread7c912008-08-06 05:08:45 +0000638 LV.isVolatileQualified(),
639 "tmp");
Mike Stump4a3999f2009-09-09 13:00:44 +0000640
Daniel Dunbaread7c912008-08-06 05:08:45 +0000641 // Mask off unused bits.
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000642 llvm::Constant *HighMask = llvm::ConstantInt::get(VMContext,
643 llvm::APInt::getLowBitsSet(EltTySize, HighBits));
Daniel Dunbaread7c912008-08-06 05:08:45 +0000644 HighVal = Builder.CreateAnd(HighVal, HighMask, "bf.lo.cleared");
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000645
Daniel Dunbaread7c912008-08-06 05:08:45 +0000646 // Shift to proper location and or in to bitfield value.
Chris Lattner72ecc682009-12-06 17:22:42 +0000647 HighVal = Builder.CreateShl(HighVal, LowBits);
Daniel Dunbaread7c912008-08-06 05:08:45 +0000648 Val = Builder.CreateOr(Val, HighVal, "bf.val");
649 }
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000650
Daniel Dunbaread7c912008-08-06 05:08:45 +0000651 // Sign extend if necessary.
652 if (LV.isBitfieldSigned()) {
Mike Stump4a3999f2009-09-09 13:00:44 +0000653 llvm::Value *ExtraBits = llvm::ConstantInt::get(EltTy,
Daniel Dunbaread7c912008-08-06 05:08:45 +0000654 EltTySize - BitfieldSize);
Mike Stump4a3999f2009-09-09 13:00:44 +0000655 Val = Builder.CreateAShr(Builder.CreateShl(Val, ExtraBits),
Daniel Dunbaread7c912008-08-06 05:08:45 +0000656 ExtraBits, "bf.val.sext");
657 }
Eli Friedmanf2442dc2008-05-17 20:03:47 +0000658
Mike Stump4a3999f2009-09-09 13:00:44 +0000659 // The bitfield type and the normal type differ when the storage sizes differ
660 // (currently just _Bool).
Daniel Dunbaread7c912008-08-06 05:08:45 +0000661 Val = Builder.CreateIntCast(Val, ConvertType(ExprType), false, "tmp");
Eli Friedmanf2442dc2008-05-17 20:03:47 +0000662
Daniel Dunbaread7c912008-08-06 05:08:45 +0000663 return RValue::get(Val);
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000664}
665
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +0000666RValue CodeGenFunction::EmitLoadOfPropertyRefLValue(LValue LV,
667 QualType ExprType) {
668 return EmitObjCPropertyGet(LV.getPropertyRefExpr());
669}
670
Fariborz Jahanian9ac53512008-11-22 22:30:21 +0000671RValue CodeGenFunction::EmitLoadOfKVCRefLValue(LValue LV,
672 QualType ExprType) {
673 return EmitObjCPropertyGet(LV.getKVCRefExpr());
674}
675
Nate Begemanb699c9b2009-01-18 06:42:49 +0000676// If this is a reference to a subset of the elements of a vector, create an
677// appropriate shufflevector.
Nate Begemance4d7fc2008-04-18 23:10:10 +0000678RValue CodeGenFunction::EmitLoadOfExtVectorElementLValue(LValue LV,
679 QualType ExprType) {
Eli Friedman327944b2008-06-13 23:01:12 +0000680 llvm::Value *Vec = Builder.CreateLoad(LV.getExtVectorAddr(),
681 LV.isVolatileQualified(), "tmp");
Mike Stump4a3999f2009-09-09 13:00:44 +0000682
Nate Begemanf322eab2008-05-09 06:41:27 +0000683 const llvm::Constant *Elts = LV.getExtVectorElts();
Mike Stump4a3999f2009-09-09 13:00:44 +0000684
685 // If the result of the expression is a non-vector type, we must be extracting
686 // a single element. Just codegen as an extractelement.
John McCall9dd450b2009-09-21 23:43:11 +0000687 const VectorType *ExprVT = ExprType->getAs<VectorType>();
Chris Lattner8eab8ff2007-08-10 17:10:08 +0000688 if (!ExprVT) {
Dan Gohman75d69da2008-05-22 00:50:06 +0000689 unsigned InIdx = getAccessedFieldNo(0, Elts);
Owen Anderson41a75022009-08-13 21:57:51 +0000690 llvm::Value *Elt = llvm::ConstantInt::get(
691 llvm::Type::getInt32Ty(VMContext), InIdx);
Chris Lattner40ff7012007-08-03 16:18:34 +0000692 return RValue::get(Builder.CreateExtractElement(Vec, Elt, "tmp"));
693 }
Nate Begemanb699c9b2009-01-18 06:42:49 +0000694
695 // Always use shuffle vector to try to retain the original program structure
Chris Lattner8eab8ff2007-08-10 17:10:08 +0000696 unsigned NumResultElts = ExprVT->getNumElements();
Mike Stump4a3999f2009-09-09 13:00:44 +0000697
Nate Begemanb699c9b2009-01-18 06:42:49 +0000698 llvm::SmallVector<llvm::Constant*, 4> Mask;
Chris Lattner40ff7012007-08-03 16:18:34 +0000699 for (unsigned i = 0; i != NumResultElts; ++i) {
Dan Gohman75d69da2008-05-22 00:50:06 +0000700 unsigned InIdx = getAccessedFieldNo(i, Elts);
Owen Anderson41a75022009-08-13 21:57:51 +0000701 Mask.push_back(llvm::ConstantInt::get(
702 llvm::Type::getInt32Ty(VMContext), InIdx));
Chris Lattner40ff7012007-08-03 16:18:34 +0000703 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000704
Owen Anderson3cc120a2009-07-28 21:22:35 +0000705 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
Nate Begemanb699c9b2009-01-18 06:42:49 +0000706 Vec = Builder.CreateShuffleVector(Vec,
Owen Anderson7ec07a52009-07-30 23:11:26 +0000707 llvm::UndefValue::get(Vec->getType()),
Nate Begemanb699c9b2009-01-18 06:42:49 +0000708 MaskV, "tmp");
709 return RValue::get(Vec);
Chris Lattner40ff7012007-08-03 16:18:34 +0000710}
711
712
Chris Lattner9369a562007-06-29 16:31:29 +0000713
Chris Lattner8394d792007-06-05 20:53:16 +0000714/// EmitStoreThroughLValue - Store the specified rvalue into the specified
715/// lvalue, where both are guaranteed to the have the same type, and that type
716/// is 'Ty'.
Mike Stump4a3999f2009-09-09 13:00:44 +0000717void CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst,
Chris Lattner8394d792007-06-05 20:53:16 +0000718 QualType Ty) {
Chris Lattner41d480e2007-08-03 16:28:33 +0000719 if (!Dst.isSimple()) {
720 if (Dst.isVectorElt()) {
721 // Read/modify/write the vector, inserting the new element.
Eli Friedman327944b2008-06-13 23:01:12 +0000722 llvm::Value *Vec = Builder.CreateLoad(Dst.getVectorAddr(),
723 Dst.isVolatileQualified(), "tmp");
Chris Lattner4647a212007-08-31 22:49:20 +0000724 Vec = Builder.CreateInsertElement(Vec, Src.getScalarVal(),
Chris Lattner41d480e2007-08-03 16:28:33 +0000725 Dst.getVectorIdx(), "vecins");
Eli Friedman327944b2008-06-13 23:01:12 +0000726 Builder.CreateStore(Vec, Dst.getVectorAddr(),Dst.isVolatileQualified());
Chris Lattner41d480e2007-08-03 16:28:33 +0000727 return;
728 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000729
Nate Begemance4d7fc2008-04-18 23:10:10 +0000730 // If this is an update of extended vector elements, insert them as
731 // appropriate.
732 if (Dst.isExtVectorElt())
733 return EmitStoreThroughExtVectorComponentLValue(Src, Dst, Ty);
Lauro Ramos Venancio09af71c2008-01-22 22:36:45 +0000734
735 if (Dst.isBitfield())
736 return EmitStoreThroughBitfieldLValue(Src, Dst, Ty);
737
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +0000738 if (Dst.isPropertyRef())
739 return EmitStoreThroughPropertyRefLValue(Src, Dst, Ty);
740
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000741 assert(Dst.isKVCRef() && "Unknown LValue type");
742 return EmitStoreThroughKVCRefLValue(Src, Dst, Ty);
Chris Lattner41d480e2007-08-03 16:28:33 +0000743 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000744
Fariborz Jahanian10bec102009-02-21 00:30:43 +0000745 if (Dst.isObjCWeak() && !Dst.isNonGC()) {
Mike Stump4a3999f2009-09-09 13:00:44 +0000746 // load of a __weak object.
Fariborz Jahanian50a12702008-11-19 17:34:06 +0000747 llvm::Value *LvalueDst = Dst.getAddress();
748 llvm::Value *src = Src.getScalarVal();
Mike Stumpca5ae662009-04-14 00:57:29 +0000749 CGM.getObjCRuntime().EmitObjCWeakAssign(*this, src, LvalueDst);
Fariborz Jahanian50a12702008-11-19 17:34:06 +0000750 return;
751 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000752
Fariborz Jahanian10bec102009-02-21 00:30:43 +0000753 if (Dst.isObjCStrong() && !Dst.isNonGC()) {
Mike Stump4a3999f2009-09-09 13:00:44 +0000754 // load of a __strong object.
Fariborz Jahanian50a12702008-11-19 17:34:06 +0000755 llvm::Value *LvalueDst = Dst.getAddress();
756 llvm::Value *src = Src.getScalarVal();
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000757 if (Dst.isObjCIvar()) {
758 assert(Dst.getBaseIvarExp() && "BaseIvarExp is NULL");
759 const llvm::Type *ResultType = ConvertType(getContext().LongTy);
760 llvm::Value *RHS = EmitScalarExpr(Dst.getBaseIvarExp());
Fariborz Jahanian1f9ed582009-09-25 00:00:20 +0000761 llvm::Value *dst = RHS;
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000762 RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast");
763 llvm::Value *LHS =
764 Builder.CreatePtrToInt(LvalueDst, ResultType, "sub.ptr.lhs.cast");
765 llvm::Value *BytesBetween = Builder.CreateSub(LHS, RHS, "ivar.offset");
Fariborz Jahanian1f9ed582009-09-25 00:00:20 +0000766 CGM.getObjCRuntime().EmitObjCIvarAssign(*this, src, dst,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000767 BytesBetween);
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000768 } else if (Dst.isGlobalObjCRef())
Fariborz Jahanian32ff7ae2009-05-04 23:27:20 +0000769 CGM.getObjCRuntime().EmitObjCGlobalAssign(*this, src, LvalueDst);
770 else
771 CGM.getObjCRuntime().EmitObjCStrongCastAssign(*this, src, LvalueDst);
Fariborz Jahanian50a12702008-11-19 17:34:06 +0000772 return;
773 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000774
Chris Lattner6278e6a2007-08-11 00:04:45 +0000775 assert(Src.isScalar() && "Can't emit an agg store with this method");
Anders Carlsson83709642009-05-19 18:50:41 +0000776 EmitStoreOfScalar(Src.getScalarVal(), Dst.getAddress(),
777 Dst.isVolatileQualified(), Ty);
Chris Lattner8394d792007-06-05 20:53:16 +0000778}
779
Lauro Ramos Venancio09af71c2008-01-22 22:36:45 +0000780void CodeGenFunction::EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst,
Mike Stump4a3999f2009-09-09 13:00:44 +0000781 QualType Ty,
Daniel Dunbar9b1335e2008-11-19 09:36:46 +0000782 llvm::Value **Result) {
Daniel Dunbaread7c912008-08-06 05:08:45 +0000783 unsigned StartBit = Dst.getBitfieldStartBit();
784 unsigned BitfieldSize = Dst.getBitfieldSize();
Lauro Ramos Venancio09af71c2008-01-22 22:36:45 +0000785 llvm::Value *Ptr = Dst.getBitfieldAddr();
Lauro Ramos Venancio09af71c2008-01-22 22:36:45 +0000786
Mike Stump4a3999f2009-09-09 13:00:44 +0000787 const llvm::Type *EltTy =
Daniel Dunbaread7c912008-08-06 05:08:45 +0000788 cast<llvm::PointerType>(Ptr->getType())->getElementType();
789 unsigned EltTySize = CGM.getTargetData().getTypeSizeInBits(EltTy);
790
Mike Stump4a3999f2009-09-09 13:00:44 +0000791 // Get the new value, cast to the appropriate type and masked to exactly the
792 // size of the bit-field.
Daniel Dunbar9b1335e2008-11-19 09:36:46 +0000793 llvm::Value *SrcVal = Src.getScalarVal();
794 llvm::Value *NewVal = Builder.CreateIntCast(SrcVal, EltTy, false, "tmp");
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000795 llvm::Constant *Mask = llvm::ConstantInt::get(VMContext,
796 llvm::APInt::getLowBitsSet(EltTySize, BitfieldSize));
Daniel Dunbaread7c912008-08-06 05:08:45 +0000797 NewVal = Builder.CreateAnd(NewVal, Mask, "bf.value");
Lauro Ramos Venancio09af71c2008-01-22 22:36:45 +0000798
Daniel Dunbar9b1335e2008-11-19 09:36:46 +0000799 // Return the new value of the bit-field, if requested.
800 if (Result) {
801 // Cast back to the proper type for result.
802 const llvm::Type *SrcTy = SrcVal->getType();
803 llvm::Value *SrcTrunc = Builder.CreateIntCast(NewVal, SrcTy, false,
804 "bf.reload.val");
805
806 // Sign extend if necessary.
807 if (Dst.isBitfieldSigned()) {
808 unsigned SrcTySize = CGM.getTargetData().getTypeSizeInBits(SrcTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000809 llvm::Value *ExtraBits = llvm::ConstantInt::get(SrcTy,
Daniel Dunbar9b1335e2008-11-19 09:36:46 +0000810 SrcTySize - BitfieldSize);
Mike Stump4a3999f2009-09-09 13:00:44 +0000811 SrcTrunc = Builder.CreateAShr(Builder.CreateShl(SrcTrunc, ExtraBits),
Daniel Dunbar9b1335e2008-11-19 09:36:46 +0000812 ExtraBits, "bf.reload.sext");
813 }
814
815 *Result = SrcTrunc;
816 }
817
Mike Stump4a3999f2009-09-09 13:00:44 +0000818 // In some cases the bitfield may straddle two memory locations. Emit the low
819 // part first and check to see if the high needs to be done.
Daniel Dunbaread7c912008-08-06 05:08:45 +0000820 unsigned LowBits = std::min(BitfieldSize, EltTySize - StartBit);
821 llvm::Value *LowVal = Builder.CreateLoad(Ptr, Dst.isVolatileQualified(),
822 "bf.prev.low");
Eli Friedmanf2442dc2008-05-17 20:03:47 +0000823
Daniel Dunbaread7c912008-08-06 05:08:45 +0000824 // Compute the mask for zero-ing the low part of this bitfield.
Mike Stump4a3999f2009-09-09 13:00:44 +0000825 llvm::Constant *InvMask =
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000826 llvm::ConstantInt::get(VMContext,
827 ~llvm::APInt::getBitsSet(EltTySize, StartBit, StartBit + LowBits));
Mike Stump4a3999f2009-09-09 13:00:44 +0000828
Daniel Dunbaread7c912008-08-06 05:08:45 +0000829 // Compute the new low part as
830 // LowVal = (LowVal & InvMask) | (NewVal << StartBit),
831 // with the shift of NewVal implicitly stripping the high bits.
Mike Stump4a3999f2009-09-09 13:00:44 +0000832 llvm::Value *NewLowVal =
Chris Lattner72ecc682009-12-06 17:22:42 +0000833 Builder.CreateShl(NewVal, StartBit, "bf.value.lo");
Daniel Dunbaread7c912008-08-06 05:08:45 +0000834 LowVal = Builder.CreateAnd(LowVal, InvMask, "bf.prev.lo.cleared");
835 LowVal = Builder.CreateOr(LowVal, NewLowVal, "bf.new.lo");
Mike Stump4a3999f2009-09-09 13:00:44 +0000836
Daniel Dunbaread7c912008-08-06 05:08:45 +0000837 // Write back.
838 Builder.CreateStore(LowVal, Ptr, Dst.isVolatileQualified());
Eli Friedmanf2442dc2008-05-17 20:03:47 +0000839
Daniel Dunbaread7c912008-08-06 05:08:45 +0000840 // If the low part doesn't cover the bitfield emit a high part.
841 if (LowBits < BitfieldSize) {
842 unsigned HighBits = BitfieldSize - LowBits;
Owen Anderson41a75022009-08-13 21:57:51 +0000843 llvm::Value *HighPtr = Builder.CreateGEP(Ptr, llvm::ConstantInt::get(
Mike Stump4a3999f2009-09-09 13:00:44 +0000844 llvm::Type::getInt32Ty(VMContext), 1), "bf.ptr.hi");
845 llvm::Value *HighVal = Builder.CreateLoad(HighPtr,
Daniel Dunbaread7c912008-08-06 05:08:45 +0000846 Dst.isVolatileQualified(),
847 "bf.prev.hi");
Mike Stump4a3999f2009-09-09 13:00:44 +0000848
Daniel Dunbaread7c912008-08-06 05:08:45 +0000849 // Compute the mask for zero-ing the high part of this bitfield.
Mike Stump4a3999f2009-09-09 13:00:44 +0000850 llvm::Constant *InvMask =
851 llvm::ConstantInt::get(VMContext, ~llvm::APInt::getLowBitsSet(EltTySize,
Owen Anderson170229f2009-07-14 23:10:40 +0000852 HighBits));
Mike Stump4a3999f2009-09-09 13:00:44 +0000853
Daniel Dunbaread7c912008-08-06 05:08:45 +0000854 // Compute the new high part as
855 // HighVal = (HighVal & InvMask) | (NewVal lshr LowBits),
856 // where the high bits of NewVal have already been cleared and the
857 // shift stripping the low bits.
Mike Stump4a3999f2009-09-09 13:00:44 +0000858 llvm::Value *NewHighVal =
Chris Lattner72ecc682009-12-06 17:22:42 +0000859 Builder.CreateLShr(NewVal, LowBits, "bf.value.high");
Daniel Dunbaread7c912008-08-06 05:08:45 +0000860 HighVal = Builder.CreateAnd(HighVal, InvMask, "bf.prev.hi.cleared");
861 HighVal = Builder.CreateOr(HighVal, NewHighVal, "bf.new.hi");
Mike Stump4a3999f2009-09-09 13:00:44 +0000862
Daniel Dunbaread7c912008-08-06 05:08:45 +0000863 // Write back.
864 Builder.CreateStore(HighVal, HighPtr, Dst.isVolatileQualified());
865 }
Lauro Ramos Venancio09af71c2008-01-22 22:36:45 +0000866}
867
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +0000868void CodeGenFunction::EmitStoreThroughPropertyRefLValue(RValue Src,
869 LValue Dst,
870 QualType Ty) {
871 EmitObjCPropertySet(Dst.getPropertyRefExpr(), Src);
872}
873
Fariborz Jahanian9ac53512008-11-22 22:30:21 +0000874void CodeGenFunction::EmitStoreThroughKVCRefLValue(RValue Src,
875 LValue Dst,
876 QualType Ty) {
877 EmitObjCPropertySet(Dst.getKVCRefExpr(), Src);
878}
879
Nate Begemance4d7fc2008-04-18 23:10:10 +0000880void CodeGenFunction::EmitStoreThroughExtVectorComponentLValue(RValue Src,
881 LValue Dst,
882 QualType Ty) {
Chris Lattner41d480e2007-08-03 16:28:33 +0000883 // This access turns into a read/modify/write of the vector. Load the input
884 // value now.
Eli Friedman327944b2008-06-13 23:01:12 +0000885 llvm::Value *Vec = Builder.CreateLoad(Dst.getExtVectorAddr(),
886 Dst.isVolatileQualified(), "tmp");
Nate Begemanf322eab2008-05-09 06:41:27 +0000887 const llvm::Constant *Elts = Dst.getExtVectorElts();
Mike Stump4a3999f2009-09-09 13:00:44 +0000888
Chris Lattner4647a212007-08-31 22:49:20 +0000889 llvm::Value *SrcVal = Src.getScalarVal();
Mike Stump4a3999f2009-09-09 13:00:44 +0000890
John McCall9dd450b2009-09-21 23:43:11 +0000891 if (const VectorType *VTy = Ty->getAs<VectorType>()) {
Chris Lattner3a44aa72007-08-03 16:37:04 +0000892 unsigned NumSrcElts = VTy->getNumElements();
Nate Begemanb699c9b2009-01-18 06:42:49 +0000893 unsigned NumDstElts =
894 cast<llvm::VectorType>(Vec->getType())->getNumElements();
895 if (NumDstElts == NumSrcElts) {
Mike Stump4a3999f2009-09-09 13:00:44 +0000896 // Use shuffle vector is the src and destination are the same number of
897 // elements and restore the vector mask since it is on the side it will be
898 // stored.
Nate Begemanea12f6e2009-06-26 21:12:50 +0000899 llvm::SmallVector<llvm::Constant*, 4> Mask(NumDstElts);
Nate Begemanb699c9b2009-01-18 06:42:49 +0000900 for (unsigned i = 0; i != NumSrcElts; ++i) {
901 unsigned InIdx = getAccessedFieldNo(i, Elts);
Owen Anderson41a75022009-08-13 21:57:51 +0000902 Mask[InIdx] = llvm::ConstantInt::get(
903 llvm::Type::getInt32Ty(VMContext), i);
Nate Begemanb699c9b2009-01-18 06:42:49 +0000904 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000905
Owen Anderson3cc120a2009-07-28 21:22:35 +0000906 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
Nate Begemanb699c9b2009-01-18 06:42:49 +0000907 Vec = Builder.CreateShuffleVector(SrcVal,
Owen Anderson7ec07a52009-07-30 23:11:26 +0000908 llvm::UndefValue::get(Vec->getType()),
Nate Begemanb699c9b2009-01-18 06:42:49 +0000909 MaskV, "tmp");
Mike Stump658fe022009-07-30 22:28:39 +0000910 } else if (NumDstElts > NumSrcElts) {
Nate Begemanb699c9b2009-01-18 06:42:49 +0000911 // Extended the source vector to the same length and then shuffle it
912 // into the destination.
913 // FIXME: since we're shuffling with undef, can we just use the indices
914 // into that? This could be simpler.
915 llvm::SmallVector<llvm::Constant*, 4> ExtMask;
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000916 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
Nate Begemanb699c9b2009-01-18 06:42:49 +0000917 unsigned i;
918 for (i = 0; i != NumSrcElts; ++i)
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000919 ExtMask.push_back(llvm::ConstantInt::get(Int32Ty, i));
Nate Begemanb699c9b2009-01-18 06:42:49 +0000920 for (; i != NumDstElts; ++i)
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000921 ExtMask.push_back(llvm::UndefValue::get(Int32Ty));
Owen Anderson3cc120a2009-07-28 21:22:35 +0000922 llvm::Value *ExtMaskV = llvm::ConstantVector::get(&ExtMask[0],
Nate Begemanb699c9b2009-01-18 06:42:49 +0000923 ExtMask.size());
Mike Stump4a3999f2009-09-09 13:00:44 +0000924 llvm::Value *ExtSrcVal =
Daniel Dunbar3d926cb2009-02-17 18:31:04 +0000925 Builder.CreateShuffleVector(SrcVal,
Owen Anderson7ec07a52009-07-30 23:11:26 +0000926 llvm::UndefValue::get(SrcVal->getType()),
Daniel Dunbar3d926cb2009-02-17 18:31:04 +0000927 ExtMaskV, "tmp");
Nate Begemanb699c9b2009-01-18 06:42:49 +0000928 // build identity
929 llvm::SmallVector<llvm::Constant*, 4> Mask;
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000930 for (unsigned i = 0; i != NumDstElts; ++i)
931 Mask.push_back(llvm::ConstantInt::get(Int32Ty, i));
932
Nate Begemanb699c9b2009-01-18 06:42:49 +0000933 // modify when what gets shuffled in
934 for (unsigned i = 0; i != NumSrcElts; ++i) {
935 unsigned Idx = getAccessedFieldNo(i, Elts);
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000936 Mask[Idx] = llvm::ConstantInt::get(Int32Ty, i+NumDstElts);
Nate Begemanb699c9b2009-01-18 06:42:49 +0000937 }
Owen Anderson3cc120a2009-07-28 21:22:35 +0000938 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
Nate Begemanb699c9b2009-01-18 06:42:49 +0000939 Vec = Builder.CreateShuffleVector(Vec, ExtSrcVal, MaskV, "tmp");
Mike Stump658fe022009-07-30 22:28:39 +0000940 } else {
Nate Begemanb699c9b2009-01-18 06:42:49 +0000941 // We should never shorten the vector
942 assert(0 && "unexpected shorten vector length");
Chris Lattner3a44aa72007-08-03 16:37:04 +0000943 }
944 } else {
945 // If the Src is a scalar (not a vector) it must be updating one element.
Dan Gohman75d69da2008-05-22 00:50:06 +0000946 unsigned InIdx = getAccessedFieldNo(0, Elts);
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000947 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
948 llvm::Value *Elt = llvm::ConstantInt::get(Int32Ty, InIdx);
Chris Lattner41d480e2007-08-03 16:28:33 +0000949 Vec = Builder.CreateInsertElement(Vec, SrcVal, Elt, "tmp");
Chris Lattner41d480e2007-08-03 16:28:33 +0000950 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000951
Eli Friedman327944b2008-06-13 23:01:12 +0000952 Builder.CreateStore(Vec, Dst.getExtVectorAddr(), Dst.isVolatileQualified());
Chris Lattner41d480e2007-08-03 16:28:33 +0000953}
954
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +0000955// setObjCGCLValueClass - sets class of he lvalue for the purpose of
956// generating write-barries API. It is currently a global, ivar,
957// or neither.
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000958static void setObjCGCLValueClass(const ASTContext &Ctx, const Expr *E,
959 LValue &LV) {
Fariborz Jahanian71848a32009-09-21 23:03:37 +0000960 if (Ctx.getLangOptions().getGCMode() == LangOptions::NonGC)
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +0000961 return;
962
Fariborz Jahaniande1d3242009-09-16 23:11:23 +0000963 if (isa<ObjCIvarRefExpr>(E)) {
964 LV.SetObjCIvar(LV, true);
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000965 ObjCIvarRefExpr *Exp = cast<ObjCIvarRefExpr>(const_cast<Expr*>(E));
966 LV.setBaseIvarExp(Exp->getBase());
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +0000967 LV.SetObjCArray(LV, E->getType()->isArrayType());
Fariborz Jahaniande1d3242009-09-16 23:11:23 +0000968 return;
969 }
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000970
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +0000971 if (const DeclRefExpr *Exp = dyn_cast<DeclRefExpr>(E)) {
972 if (const VarDecl *VD = dyn_cast<VarDecl>(Exp->getDecl())) {
973 if ((VD->isBlockVarDecl() && !VD->hasLocalStorage()) ||
974 VD->isFileVarDecl())
975 LV.SetGlobalObjCRef(LV, true);
976 }
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +0000977 LV.SetObjCArray(LV, E->getType()->isArrayType());
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000978 return;
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +0000979 }
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000980
981 if (const UnaryOperator *Exp = dyn_cast<UnaryOperator>(E)) {
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +0000982 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000983 return;
984 }
985
986 if (const ParenExpr *Exp = dyn_cast<ParenExpr>(E)) {
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +0000987 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
Fariborz Jahaniane01e4342009-09-30 17:10:29 +0000988 if (LV.isObjCIvar()) {
989 // If cast is to a structure pointer, follow gcc's behavior and make it
990 // a non-ivar write-barrier.
991 QualType ExpTy = E->getType();
992 if (ExpTy->isPointerType())
993 ExpTy = ExpTy->getAs<PointerType>()->getPointeeType();
994 if (ExpTy->isRecordType())
995 LV.SetObjCIvar(LV, false);
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000996 }
997 return;
Fariborz Jahaniane01e4342009-09-30 17:10:29 +0000998 }
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000999 if (const ImplicitCastExpr *Exp = dyn_cast<ImplicitCastExpr>(E)) {
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +00001000 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001001 return;
1002 }
1003
1004 if (const CStyleCastExpr *Exp = dyn_cast<CStyleCastExpr>(E)) {
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +00001005 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001006 return;
1007 }
1008
1009 if (const ArraySubscriptExpr *Exp = dyn_cast<ArraySubscriptExpr>(E)) {
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +00001010 setObjCGCLValueClass(Ctx, Exp->getBase(), LV);
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +00001011 if (LV.isObjCIvar() && !LV.isObjCArray())
Fariborz Jahanian2e32ddc2009-09-18 00:04:00 +00001012 // Using array syntax to assigning to what an ivar points to is not
1013 // same as assigning to the ivar itself. {id *Names;} Names[i] = 0;
1014 LV.SetObjCIvar(LV, false);
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +00001015 else if (LV.isGlobalObjCRef() && !LV.isObjCArray())
1016 // Using array syntax to assigning to what global points to is not
1017 // same as assigning to the global itself. {id *G;} G[i] = 0;
1018 LV.SetGlobalObjCRef(LV, false);
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001019 return;
Fariborz Jahanian2e32ddc2009-09-18 00:04:00 +00001020 }
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001021
1022 if (const MemberExpr *Exp = dyn_cast<MemberExpr>(E)) {
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +00001023 setObjCGCLValueClass(Ctx, Exp->getBase(), LV);
Fariborz Jahanian2e32ddc2009-09-18 00:04:00 +00001024 // We don't know if member is an 'ivar', but this flag is looked at
1025 // only in the context of LV.isObjCIvar().
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +00001026 LV.SetObjCArray(LV, E->getType()->isArrayType());
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001027 return;
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +00001028 }
1029}
1030
Anders Carlssonea4c30b2009-11-07 23:06:58 +00001031static LValue EmitGlobalVarDeclLValue(CodeGenFunction &CGF,
1032 const Expr *E, const VarDecl *VD) {
Daniel Dunbar7e215ea2009-11-08 09:46:46 +00001033 assert((VD->hasExternalStorage() || VD->isFileVarDecl()) &&
Anders Carlssonea4c30b2009-11-07 23:06:58 +00001034 "Var decl must have external storage or be a file var decl!");
1035
1036 llvm::Value *V = CGF.CGM.GetAddrOfGlobalVar(VD);
1037 if (VD->getType()->isReferenceType())
1038 V = CGF.Builder.CreateLoad(V, "tmp");
1039 LValue LV = LValue::MakeAddr(V, CGF.MakeQualifiers(E->getType()));
1040 setObjCGCLValueClass(CGF.getContext(), E, LV);
1041 return LV;
1042}
1043
Eli Friedmand15eb34d2009-11-26 06:08:14 +00001044static LValue EmitFunctionDeclLValue(CodeGenFunction &CGF,
1045 const Expr *E, const FunctionDecl *FD) {
1046 llvm::Value* V = CGF.CGM.GetAddrOfFunction(FD);
1047 if (!FD->hasPrototype()) {
1048 if (const FunctionProtoType *Proto =
1049 FD->getType()->getAs<FunctionProtoType>()) {
1050 // Ugly case: for a K&R-style definition, the type of the definition
1051 // isn't the same as the type of a use. Correct for this with a
1052 // bitcast.
1053 QualType NoProtoType =
1054 CGF.getContext().getFunctionNoProtoType(Proto->getResultType());
1055 NoProtoType = CGF.getContext().getPointerType(NoProtoType);
1056 V = CGF.Builder.CreateBitCast(V, CGF.ConvertType(NoProtoType), "tmp");
1057 }
1058 }
1059 return LValue::MakeAddr(V, CGF.MakeQualifiers(E->getType()));
1060}
1061
Chris Lattnerd7f58862007-06-02 05:24:33 +00001062LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) {
Anders Carlsson2ff6395d2009-11-07 22:53:10 +00001063 const NamedDecl *ND = E->getDecl();
Mike Stump4a3999f2009-09-09 13:00:44 +00001064
Rafael Espindola2e42fec2010-03-04 18:17:24 +00001065 if (ND->hasAttr<WeakRefAttr>()) {
1066 const ValueDecl* VD = cast<ValueDecl>(ND);
1067 llvm::Constant *Aliasee = CGM.GetWeakRefReference(VD);
1068
1069 Qualifiers Quals = MakeQualifiers(E->getType());
1070 LValue LV = LValue::MakeAddr(Aliasee, Quals);
1071
1072 return LV;
1073 }
1074
Anders Carlsson2ff6395d2009-11-07 22:53:10 +00001075 if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
Anders Carlsson2ff6395d2009-11-07 22:53:10 +00001076
1077 // Check if this is a global variable.
Anders Carlssonea4c30b2009-11-07 23:06:58 +00001078 if (VD->hasExternalStorage() || VD->isFileVarDecl())
1079 return EmitGlobalVarDeclLValue(*this, E, VD);
Anders Carlsson6eee9722009-11-07 22:46:42 +00001080
1081 bool NonGCable = VD->hasLocalStorage() && !VD->hasAttr<BlocksAttr>();
1082
1083 llvm::Value *V = LocalDeclMap[VD];
1084 assert(V && "DeclRefExpr not entered in LocalDeclMap?");
1085
1086 Qualifiers Quals = MakeQualifiers(E->getType());
1087 // local variables do not get their gc attribute set.
1088 // local static?
1089 if (NonGCable) Quals.removeObjCGCAttr();
1090
1091 if (VD->hasAttr<BlocksAttr>()) {
1092 V = Builder.CreateStructGEP(V, 1, "forwarding");
Daniel Dunbarc76493a2009-11-29 21:23:36 +00001093 V = Builder.CreateLoad(V);
Anders Carlsson6eee9722009-11-07 22:46:42 +00001094 V = Builder.CreateStructGEP(V, getByRefValueLLVMField(VD),
1095 VD->getNameAsString());
1096 }
1097 if (VD->getType()->isReferenceType())
1098 V = Builder.CreateLoad(V, "tmp");
Anders Carlssonea4c30b2009-11-07 23:06:58 +00001099 LValue LV = LValue::MakeAddr(V, Quals);
Anders Carlsson6eee9722009-11-07 22:46:42 +00001100 LValue::SetObjCNonGC(LV, NonGCable);
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +00001101 setObjCGCLValueClass(getContext(), E, LV);
Fariborz Jahanian003e8302008-11-20 00:15:42 +00001102 return LV;
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001103 }
1104
Eli Friedmand15eb34d2009-11-26 06:08:14 +00001105 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
1106 return EmitFunctionDeclLValue(*this, E, FD);
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001107
Anders Carlsson259688c2010-02-02 03:37:46 +00001108 // FIXME: the qualifier check does not seem sufficient here
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001109 if (E->getQualifier()) {
Anders Carlsson259688c2010-02-02 03:37:46 +00001110 const FieldDecl *FD = cast<FieldDecl>(ND);
1111 llvm::Value *V = CGM.EmitPointerToDataMember(FD);
1112
1113 return LValue::MakeAddr(V, MakeQualifiers(FD->getType()));
Chris Lattner5696e7b2008-06-17 18:05:57 +00001114 }
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001115
Anders Carlsson2ff6395d2009-11-07 22:53:10 +00001116 assert(false && "Unhandled DeclRefExpr");
1117
1118 // an invalid LValue, but the assert will
1119 // ensure that this point is never reached.
Chris Lattner793d10c2007-09-16 19:23:47 +00001120 return LValue();
Chris Lattnerd7f58862007-06-02 05:24:33 +00001121}
Chris Lattnere47e4402007-06-01 18:02:12 +00001122
Mike Stump1db7d042009-02-28 09:07:16 +00001123LValue CodeGenFunction::EmitBlockDeclRefLValue(const BlockDeclRefExpr *E) {
John McCall8ccfcb52009-09-24 19:53:00 +00001124 return LValue::MakeAddr(GetAddrOfBlockDecl(E), MakeQualifiers(E->getType()));
Mike Stump1db7d042009-02-28 09:07:16 +00001125}
1126
Chris Lattner8394d792007-06-05 20:53:16 +00001127LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) {
1128 // __extension__ doesn't affect lvalue-ness.
1129 if (E->getOpcode() == UnaryOperator::Extension)
1130 return EmitLValue(E->getSubExpr());
Mike Stump4a3999f2009-09-09 13:00:44 +00001131
Chris Lattner0f398c42008-07-26 22:37:01 +00001132 QualType ExprTy = getContext().getCanonicalType(E->getSubExpr()->getType());
Chris Lattner595db862007-10-30 22:53:42 +00001133 switch (E->getOpcode()) {
1134 default: assert(0 && "Unknown unary operator lvalue!");
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001135 case UnaryOperator::Deref: {
1136 QualType T = E->getSubExpr()->getType()->getPointeeType();
1137 assert(!T.isNull() && "CodeGenFunction::EmitUnaryOpLValue: Illegal type");
Mike Stump4a3999f2009-09-09 13:00:44 +00001138
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001139 Qualifiers Quals = MakeQualifiers(T);
1140 Quals.setAddressSpace(ExprTy.getAddressSpace());
John McCall8ccfcb52009-09-24 19:53:00 +00001141
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001142 LValue LV = LValue::MakeAddr(EmitScalarExpr(E->getSubExpr()), Quals);
1143 // We should not generate __weak write barrier on indirect reference
1144 // of a pointer to object; as in void foo (__weak id *param); *param = 0;
1145 // But, we continue to generate __strong write barrier on indirect write
1146 // into a pointer to object.
1147 if (getContext().getLangOptions().ObjC1 &&
1148 getContext().getLangOptions().getGCMode() != LangOptions::NonGC &&
1149 LV.isObjCWeak())
1150 LValue::SetObjCNonGC(LV, !E->isOBJCGCCandidate(getContext()));
1151 return LV;
1152 }
Chris Lattner595db862007-10-30 22:53:42 +00001153 case UnaryOperator::Real:
Eli Friedmana72bf0f2009-11-09 04:20:47 +00001154 case UnaryOperator::Imag: {
Chris Lattner595db862007-10-30 22:53:42 +00001155 LValue LV = EmitLValue(E->getSubExpr());
Chris Lattner3e593cd2008-03-19 05:19:41 +00001156 unsigned Idx = E->getOpcode() == UnaryOperator::Imag;
1157 return LValue::MakeAddr(Builder.CreateStructGEP(LV.getAddress(),
Chris Lattner574dee62008-07-26 22:17:49 +00001158 Idx, "idx"),
John McCall8ccfcb52009-09-24 19:53:00 +00001159 MakeQualifiers(ExprTy));
Chris Lattner595db862007-10-30 22:53:42 +00001160 }
Eli Friedmana72bf0f2009-11-09 04:20:47 +00001161 case UnaryOperator::PreInc:
Chris Lattnerbb8976e2010-01-09 21:44:40 +00001162 case UnaryOperator::PreDec: {
1163 LValue LV = EmitLValue(E->getSubExpr());
1164 bool isInc = E->getOpcode() == UnaryOperator::PreInc;
1165
1166 if (E->getType()->isAnyComplexType())
1167 EmitComplexPrePostIncDec(E, LV, isInc, true/*isPre*/);
1168 else
1169 EmitScalarPrePostIncDec(E, LV, isInc, true/*isPre*/);
1170 return LV;
1171 }
Eli Friedmana72bf0f2009-11-09 04:20:47 +00001172 }
Chris Lattner8394d792007-06-05 20:53:16 +00001173}
1174
Chris Lattner4347e3692007-06-06 04:54:52 +00001175LValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) {
John McCall8ccfcb52009-09-24 19:53:00 +00001176 return LValue::MakeAddr(CGM.GetAddrOfConstantStringFromLiteral(E),
1177 Qualifiers());
Chris Lattner4347e3692007-06-06 04:54:52 +00001178}
1179
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00001180LValue CodeGenFunction::EmitObjCEncodeExprLValue(const ObjCEncodeExpr *E) {
John McCall8ccfcb52009-09-24 19:53:00 +00001181 return LValue::MakeAddr(CGM.GetAddrOfConstantStringFromObjCEncode(E),
1182 Qualifiers());
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00001183}
1184
1185
Daniel Dunbarb3517472008-10-17 21:58:32 +00001186LValue CodeGenFunction::EmitPredefinedFunctionName(unsigned Type) {
Anders Carlsson625bfc82007-07-21 05:21:51 +00001187 std::string GlobalVarName;
Daniel Dunbarb3517472008-10-17 21:58:32 +00001188
1189 switch (Type) {
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001190 default: assert(0 && "Invalid type");
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00001191 case PredefinedExpr::Func:
1192 GlobalVarName = "__func__.";
1193 break;
1194 case PredefinedExpr::Function:
1195 GlobalVarName = "__FUNCTION__.";
1196 break;
1197 case PredefinedExpr::PrettyFunction:
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00001198 GlobalVarName = "__PRETTY_FUNCTION__.";
1199 break;
Anders Carlsson625bfc82007-07-21 05:21:51 +00001200 }
Daniel Dunbarb3517472008-10-17 21:58:32 +00001201
Daniel Dunbar0482cfd2009-09-12 23:06:21 +00001202 llvm::StringRef FnName = CurFn->getName();
1203 if (FnName.startswith("\01"))
1204 FnName = FnName.substr(1);
1205 GlobalVarName += FnName;
1206
Anders Carlsson2fb08242009-09-08 18:24:21 +00001207 std::string FunctionName =
Anders Carlsson5bd8d192010-02-11 18:20:28 +00001208 PredefinedExpr::ComputeName((PredefinedExpr::IdentType)Type, CurCodeDecl);
Daniel Dunbarb3517472008-10-17 21:58:32 +00001209
Mike Stump4a3999f2009-09-09 13:00:44 +00001210 llvm::Constant *C =
Daniel Dunbarb3517472008-10-17 21:58:32 +00001211 CGM.GetAddrOfConstantCString(FunctionName, GlobalVarName.c_str());
John McCall8ccfcb52009-09-24 19:53:00 +00001212 return LValue::MakeAddr(C, Qualifiers());
Daniel Dunbarb3517472008-10-17 21:58:32 +00001213}
1214
Mike Stump4a3999f2009-09-09 13:00:44 +00001215LValue CodeGenFunction::EmitPredefinedLValue(const PredefinedExpr *E) {
Daniel Dunbarb3517472008-10-17 21:58:32 +00001216 switch (E->getIdentType()) {
1217 default:
1218 return EmitUnsupportedLValue(E, "predefined expression");
1219 case PredefinedExpr::Func:
1220 case PredefinedExpr::Function:
1221 case PredefinedExpr::PrettyFunction:
1222 return EmitPredefinedFunctionName(E->getIdentType());
1223 }
Anders Carlsson625bfc82007-07-21 05:21:51 +00001224}
1225
Mike Stumpcf16d2c2009-12-15 01:22:35 +00001226llvm::BasicBlock *CodeGenFunction::getTrapBB() {
Mike Stump9a4e0122009-12-15 00:59:40 +00001227 const CodeGenOptions &GCO = CGM.getCodeGenOpts();
1228
1229 // If we are not optimzing, don't collapse all calls to trap in the function
1230 // to the same call, that way, in the debugger they can see which operation
1231 // did in fact fail. If we are optimizing, we collpase all call to trap down
1232 // to just one per function to save on codesize.
1233 if (GCO.OptimizationLevel
1234 && TrapBB)
Mike Stumpe8c3b3e2009-12-15 00:35:12 +00001235 return TrapBB;
Mike Stumpd9546382009-12-12 01:27:46 +00001236
1237 llvm::BasicBlock *Cont = 0;
1238 if (HaveInsertPoint()) {
1239 Cont = createBasicBlock("cont");
1240 EmitBranch(Cont);
1241 }
Mike Stumpe8c3b3e2009-12-15 00:35:12 +00001242 TrapBB = createBasicBlock("trap");
1243 EmitBlock(TrapBB);
1244
1245 llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::trap, 0, 0);
1246 llvm::CallInst *TrapCall = Builder.CreateCall(F);
1247 TrapCall->setDoesNotReturn();
1248 TrapCall->setDoesNotThrow();
Mike Stumpd9546382009-12-12 01:27:46 +00001249 Builder.CreateUnreachable();
1250
1251 if (Cont)
1252 EmitBlock(Cont);
Mike Stumpe8c3b3e2009-12-15 00:35:12 +00001253 return TrapBB;
Mike Stumpd9546382009-12-12 01:27:46 +00001254}
1255
Chris Lattnerd9d2fb12007-06-08 23:31:14 +00001256LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) {
Ted Kremenekc81614d2007-08-20 16:18:38 +00001257 // The index must always be an integer, which is not an aggregate. Emit it.
Chris Lattner2da04b32007-08-24 05:35:26 +00001258 llvm::Value *Idx = EmitScalarExpr(E->getIdx());
Eli Friedman07bbeca2009-06-06 19:09:26 +00001259 QualType IdxTy = E->getIdx()->getType();
1260 bool IdxSigned = IdxTy->isSignedIntegerType();
1261
Chris Lattner08c4b9f2007-07-10 21:17:59 +00001262 // If the base is a vector type, then we are forming a vector element lvalue
1263 // with this subscript.
Eli Friedman327944b2008-06-13 23:01:12 +00001264 if (E->getBase()->getType()->isVectorType()) {
Chris Lattner08c4b9f2007-07-10 21:17:59 +00001265 // Emit the vector as an lvalue to get its address.
Eli Friedman327944b2008-06-13 23:01:12 +00001266 LValue LHS = EmitLValue(E->getBase());
Ted Kremenekc81614d2007-08-20 16:18:38 +00001267 assert(LHS.isSimple() && "Can only subscript lvalue vectors here!");
Mike Stump4a3999f2009-09-09 13:00:44 +00001268 Idx = Builder.CreateIntCast(Idx,
Owen Anderson41a75022009-08-13 21:57:51 +00001269 llvm::Type::getInt32Ty(VMContext), IdxSigned, "vidx");
Eli Friedman327944b2008-06-13 23:01:12 +00001270 return LValue::MakeVectorElt(LHS.getAddress(), Idx,
John McCall8ccfcb52009-09-24 19:53:00 +00001271 E->getBase()->getType().getCVRQualifiers());
Chris Lattner08c4b9f2007-07-10 21:17:59 +00001272 }
Mike Stump4a3999f2009-09-09 13:00:44 +00001273
Ted Kremenekc81614d2007-08-20 16:18:38 +00001274 // The base must be a pointer, which is not an aggregate. Emit it.
Chris Lattner2da04b32007-08-24 05:35:26 +00001275 llvm::Value *Base = EmitScalarExpr(E->getBase());
Mike Stump4a3999f2009-09-09 13:00:44 +00001276
Ted Kremenekc81614d2007-08-20 16:18:38 +00001277 // Extend or truncate the index type to 32 or 64-bits.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001278 unsigned IdxBitwidth = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
Sanjiv Gupta47425152009-04-24 02:40:57 +00001279 if (IdxBitwidth != LLVMPointerWidth)
Owen Anderson41a75022009-08-13 21:57:51 +00001280 Idx = Builder.CreateIntCast(Idx,
1281 llvm::IntegerType::get(VMContext, LLVMPointerWidth),
Chris Lattnerd9d2fb12007-06-08 23:31:14 +00001282 IdxSigned, "idxprom");
1283
Mike Stump3f6f9fe2009-12-16 02:57:00 +00001284 // FIXME: As llvm implements the object size checking, this can come out.
Mike Stumpd9546382009-12-12 01:27:46 +00001285 if (CatchUndefined) {
Mike Stump3f6f9fe2009-12-16 02:57:00 +00001286 if (const ImplicitCastExpr *ICE=dyn_cast<ImplicitCastExpr>(E->getBase())) {
Mike Stumpd9546382009-12-12 01:27:46 +00001287 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr())) {
1288 if (ICE->getCastKind() == CastExpr::CK_ArrayToPointerDecay) {
1289 if (const ConstantArrayType *CAT
1290 = getContext().getAsConstantArrayType(DRE->getType())) {
1291 llvm::APInt Size = CAT->getSize();
1292 llvm::BasicBlock *Cont = createBasicBlock("cont");
Mike Stump590d18f2009-12-14 22:14:31 +00001293 Builder.CreateCondBr(Builder.CreateICmpULE(Idx,
Mike Stumpd9546382009-12-12 01:27:46 +00001294 llvm::ConstantInt::get(Idx->getType(), Size)),
Mike Stumpe8c3b3e2009-12-15 00:35:12 +00001295 Cont, getTrapBB());
Mike Stumpf8858af2009-12-14 20:52:00 +00001296 EmitBlock(Cont);
Mike Stumpd9546382009-12-12 01:27:46 +00001297 }
1298 }
1299 }
1300 }
1301 }
1302
Mike Stump4a3999f2009-09-09 13:00:44 +00001303 // We know that the pointer points to a type of the correct size, unless the
1304 // size is a VLA or Objective-C interface.
Daniel Dunbaref2ffbc2009-04-25 05:08:32 +00001305 llvm::Value *Address = 0;
Mike Stump4a3999f2009-09-09 13:00:44 +00001306 if (const VariableArrayType *VAT =
Anders Carlsson3d312f82008-12-21 00:11:23 +00001307 getContext().getAsVariableArrayType(E->getType())) {
Chris Lattner19efdd62009-08-14 23:43:22 +00001308 llvm::Value *VLASize = GetVLASize(VAT);
Mike Stump4a3999f2009-09-09 13:00:44 +00001309
Anders Carlsson3d312f82008-12-21 00:11:23 +00001310 Idx = Builder.CreateMul(Idx, VLASize);
Mike Stump4a3999f2009-09-09 13:00:44 +00001311
Anders Carlssone0808df2008-12-21 03:44:36 +00001312 QualType BaseType = getContext().getBaseElementType(VAT);
Mike Stump4a3999f2009-09-09 13:00:44 +00001313
Ken Dyck40775002010-01-11 17:06:35 +00001314 CharUnits BaseTypeSize = getContext().getTypeSizeInChars(BaseType);
Anders Carlsson3d312f82008-12-21 00:11:23 +00001315 Idx = Builder.CreateUDiv(Idx,
Mike Stump4a3999f2009-09-09 13:00:44 +00001316 llvm::ConstantInt::get(Idx->getType(),
Ken Dyck40775002010-01-11 17:06:35 +00001317 BaseTypeSize.getQuantity()));
Dan Gohman43b44842009-08-12 00:33:55 +00001318 Address = Builder.CreateInBoundsGEP(Base, Idx, "arrayidx");
Mike Stump4a3999f2009-09-09 13:00:44 +00001319 } else if (const ObjCInterfaceType *OIT =
Daniel Dunbaref2ffbc2009-04-25 05:08:32 +00001320 dyn_cast<ObjCInterfaceType>(E->getType())) {
Mike Stump4a3999f2009-09-09 13:00:44 +00001321 llvm::Value *InterfaceSize =
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001322 llvm::ConstantInt::get(Idx->getType(),
Ken Dyck40775002010-01-11 17:06:35 +00001323 getContext().getTypeSizeInChars(OIT).getQuantity());
Mike Stump4a3999f2009-09-09 13:00:44 +00001324
Daniel Dunbaref2ffbc2009-04-25 05:08:32 +00001325 Idx = Builder.CreateMul(Idx, InterfaceSize);
1326
Benjamin Kramerabd5b902009-10-13 10:07:13 +00001327 const llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(VMContext);
Dan Gohman43b44842009-08-12 00:33:55 +00001328 Address = Builder.CreateGEP(Builder.CreateBitCast(Base, i8PTy),
Daniel Dunbaref2ffbc2009-04-25 05:08:32 +00001329 Idx, "arrayidx");
1330 Address = Builder.CreateBitCast(Address, Base->getType());
1331 } else {
Dan Gohman43b44842009-08-12 00:33:55 +00001332 Address = Builder.CreateInBoundsGEP(Base, Idx, "arrayidx");
Anders Carlsson3d312f82008-12-21 00:11:23 +00001333 }
Mike Stump4a3999f2009-09-09 13:00:44 +00001334
Steve Naroff7cae42b2009-07-10 23:34:53 +00001335 QualType T = E->getBase()->getType()->getPointeeType();
Mike Stump4a3999f2009-09-09 13:00:44 +00001336 assert(!T.isNull() &&
Steve Naroff7cae42b2009-07-10 23:34:53 +00001337 "CodeGenFunction::EmitArraySubscriptExpr(): Illegal base type");
Mike Stump4a3999f2009-09-09 13:00:44 +00001338
John McCall8ccfcb52009-09-24 19:53:00 +00001339 Qualifiers Quals = MakeQualifiers(T);
1340 Quals.setAddressSpace(E->getBase()->getType().getAddressSpace());
1341
1342 LValue LV = LValue::MakeAddr(Address, Quals);
Fariborz Jahaniana9fecf32009-02-21 23:37:19 +00001343 if (getContext().getLangOptions().ObjC1 &&
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +00001344 getContext().getLangOptions().getGCMode() != LangOptions::NonGC) {
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001345 LValue::SetObjCNonGC(LV, !E->isOBJCGCCandidate(getContext()));
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +00001346 setObjCGCLValueClass(getContext(), E, LV);
1347 }
Fariborz Jahaniana9fecf32009-02-21 23:37:19 +00001348 return LV;
Chris Lattnerd9d2fb12007-06-08 23:31:14 +00001349}
1350
Mike Stump4a3999f2009-09-09 13:00:44 +00001351static
Owen Anderson170229f2009-07-14 23:10:40 +00001352llvm::Constant *GenerateConstantVector(llvm::LLVMContext &VMContext,
1353 llvm::SmallVector<unsigned, 4> &Elts) {
Chris Lattner4e1a3232009-12-23 21:31:11 +00001354 llvm::SmallVector<llvm::Constant*, 4> CElts;
Mike Stump4a3999f2009-09-09 13:00:44 +00001355
Nate Begemand3862152008-05-13 21:03:02 +00001356 for (unsigned i = 0, e = Elts.size(); i != e; ++i)
Owen Anderson41a75022009-08-13 21:57:51 +00001357 CElts.push_back(llvm::ConstantInt::get(
1358 llvm::Type::getInt32Ty(VMContext), Elts[i]));
Nate Begemand3862152008-05-13 21:03:02 +00001359
Owen Anderson3cc120a2009-07-28 21:22:35 +00001360 return llvm::ConstantVector::get(&CElts[0], CElts.size());
Nate Begemand3862152008-05-13 21:03:02 +00001361}
1362
Chris Lattner9e751ca2007-08-02 23:37:31 +00001363LValue CodeGenFunction::
Nate Begemance4d7fc2008-04-18 23:10:10 +00001364EmitExtVectorElementExpr(const ExtVectorElementExpr *E) {
Chris Lattner4e1a3232009-12-23 21:31:11 +00001365 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
1366
Chris Lattner9e751ca2007-08-02 23:37:31 +00001367 // Emit the base vector as an l-value.
Chris Lattner6c7ce102009-02-16 21:11:58 +00001368 LValue Base;
1369
1370 // ExtVectorElementExpr's base can either be a vector or pointer to vector.
Chris Lattner4e1a3232009-12-23 21:31:11 +00001371 if (E->isArrow()) {
1372 // If it is a pointer to a vector, emit the address and form an lvalue with
1373 // it.
Chris Lattnerb8211f62009-02-16 22:14:05 +00001374 llvm::Value *Ptr = EmitScalarExpr(E->getBase());
Chris Lattner4e1a3232009-12-23 21:31:11 +00001375 const PointerType *PT = E->getBase()->getType()->getAs<PointerType>();
John McCall8ccfcb52009-09-24 19:53:00 +00001376 Qualifiers Quals = MakeQualifiers(PT->getPointeeType());
1377 Quals.removeObjCGCAttr();
1378 Base = LValue::MakeAddr(Ptr, Quals);
Chris Lattner4e1a3232009-12-23 21:31:11 +00001379 } else if (E->getBase()->isLvalue(getContext()) == Expr::LV_Valid) {
1380 // Otherwise, if the base is an lvalue ( as in the case of foo.x.x),
1381 // emit the base as an lvalue.
1382 assert(E->getBase()->getType()->isVectorType());
1383 Base = EmitLValue(E->getBase());
1384 } else {
1385 // Otherwise, the base is a normal rvalue (as in (V+V).x), emit it as such.
Daniel Dunbar5b901952010-01-04 18:02:28 +00001386 assert(E->getBase()->getType()->getAs<VectorType>() &&
1387 "Result must be a vector");
Chris Lattner4e1a3232009-12-23 21:31:11 +00001388 llvm::Value *Vec = EmitScalarExpr(E->getBase());
1389
Chris Lattnerf0a9ba32009-12-23 21:33:41 +00001390 // Store the vector to memory (because LValue wants an address).
Daniel Dunbara7566f12010-02-09 02:48:28 +00001391 llvm::Value *VecMem = CreateMemTemp(E->getBase()->getType());
Chris Lattner4e1a3232009-12-23 21:31:11 +00001392 Builder.CreateStore(Vec, VecMem);
Chris Lattnerf0a9ba32009-12-23 21:33:41 +00001393 Base = LValue::MakeAddr(VecMem, Qualifiers());
Chris Lattner4e1a3232009-12-23 21:31:11 +00001394 }
1395
Nate Begemand3862152008-05-13 21:03:02 +00001396 // Encode the element access list into a vector of unsigned indices.
1397 llvm::SmallVector<unsigned, 4> Indices;
1398 E->getEncodedElementAccess(Indices);
1399
1400 if (Base.isSimple()) {
Owen Anderson170229f2009-07-14 23:10:40 +00001401 llvm::Constant *CV = GenerateConstantVector(VMContext, Indices);
Eli Friedman327944b2008-06-13 23:01:12 +00001402 return LValue::MakeExtVectorElt(Base.getAddress(), CV,
John McCall8ccfcb52009-09-24 19:53:00 +00001403 Base.getVRQualifiers());
Nate Begemand3862152008-05-13 21:03:02 +00001404 }
1405 assert(Base.isExtVectorElt() && "Can only subscript lvalue vec elts here!");
1406
1407 llvm::Constant *BaseElts = Base.getExtVectorElts();
1408 llvm::SmallVector<llvm::Constant *, 4> CElts;
1409
1410 for (unsigned i = 0, e = Indices.size(); i != e; ++i) {
1411 if (isa<llvm::ConstantAggregateZero>(BaseElts))
Chris Lattner5e71d432009-10-28 05:12:07 +00001412 CElts.push_back(llvm::ConstantInt::get(Int32Ty, 0));
Nate Begemand3862152008-05-13 21:03:02 +00001413 else
Chris Lattner5e71d432009-10-28 05:12:07 +00001414 CElts.push_back(cast<llvm::Constant>(BaseElts->getOperand(Indices[i])));
Nate Begemand3862152008-05-13 21:03:02 +00001415 }
Owen Anderson3cc120a2009-07-28 21:22:35 +00001416 llvm::Constant *CV = llvm::ConstantVector::get(&CElts[0], CElts.size());
Eli Friedman327944b2008-06-13 23:01:12 +00001417 return LValue::MakeExtVectorElt(Base.getExtVectorAddr(), CV,
John McCall8ccfcb52009-09-24 19:53:00 +00001418 Base.getVRQualifiers());
Chris Lattner9e751ca2007-08-02 23:37:31 +00001419}
1420
Devang Patel30efa2e2007-10-23 20:28:39 +00001421LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) {
Fariborz Jahanian10bec102009-02-21 00:30:43 +00001422 bool isNonGC = false;
Devang Pateld68df202007-10-24 22:26:28 +00001423 Expr *BaseExpr = E->getBase();
Devang Pateld68df202007-10-24 22:26:28 +00001424 llvm::Value *BaseValue = NULL;
John McCall8ccfcb52009-09-24 19:53:00 +00001425 Qualifiers BaseQuals;
Eli Friedman327944b2008-06-13 23:01:12 +00001426
Chris Lattner4e4186b2007-12-02 18:52:07 +00001427 // 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 +00001428 if (E->isArrow()) {
Devang Patel7718d7a2007-10-26 18:15:21 +00001429 BaseValue = EmitScalarExpr(BaseExpr);
Mike Stump4a3999f2009-09-09 13:00:44 +00001430 const PointerType *PTy =
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001431 BaseExpr->getType()->getAs<PointerType>();
John McCall8ccfcb52009-09-24 19:53:00 +00001432 BaseQuals = PTy->getPointeeType().getQualifiers();
Fariborz Jahanian1a504772009-09-01 17:02:21 +00001433 } else if (isa<ObjCPropertyRefExpr>(BaseExpr->IgnoreParens()) ||
1434 isa<ObjCImplicitSetterGetterRefExpr>(
1435 BaseExpr->IgnoreParens())) {
Fariborz Jahanian30e78642009-01-12 23:27:26 +00001436 RValue RV = EmitObjCPropertyGet(BaseExpr);
1437 BaseValue = RV.getAggregateAddr();
John McCall8ccfcb52009-09-24 19:53:00 +00001438 BaseQuals = BaseExpr->getType().getQualifiers();
Chris Lattnere084c012009-02-16 22:25:49 +00001439 } else {
Chris Lattner4e4186b2007-12-02 18:52:07 +00001440 LValue BaseLV = EmitLValue(BaseExpr);
Fariborz Jahanian10bec102009-02-21 00:30:43 +00001441 if (BaseLV.isNonGC())
1442 isNonGC = true;
Chris Lattner4e4186b2007-12-02 18:52:07 +00001443 // FIXME: this isn't right for bitfields.
1444 BaseValue = BaseLV.getAddress();
Fariborz Jahanian82e28742009-07-29 00:44:13 +00001445 QualType BaseTy = BaseExpr->getType();
John McCall8ccfcb52009-09-24 19:53:00 +00001446 BaseQuals = BaseTy.getQualifiers();
Chris Lattner4e4186b2007-12-02 18:52:07 +00001447 }
Devang Patel30efa2e2007-10-23 20:28:39 +00001448
Anders Carlssonea4c30b2009-11-07 23:06:58 +00001449 NamedDecl *ND = E->getMemberDecl();
1450 if (FieldDecl *Field = dyn_cast<FieldDecl>(ND)) {
Anders Carlsson5d8645b2010-01-29 05:05:36 +00001451 LValue LV = EmitLValueForField(BaseValue, Field,
Anders Carlssonea4c30b2009-11-07 23:06:58 +00001452 BaseQuals.getCVRQualifiers());
1453 LValue::SetObjCNonGC(LV, isNonGC);
1454 setObjCGCLValueClass(getContext(), E, LV);
1455 return LV;
1456 }
1457
Anders Carlsson5bbdc9f2009-11-07 23:16:50 +00001458 if (VarDecl *VD = dyn_cast<VarDecl>(ND))
1459 return EmitGlobalVarDeclLValue(*this, E, VD);
Eli Friedmand15eb34d2009-11-26 06:08:14 +00001460
1461 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
1462 return EmitFunctionDeclLValue(*this, E, FD);
1463
Anders Carlssonea4c30b2009-11-07 23:06:58 +00001464 assert(false && "Unhandled member declaration!");
1465 return LValue();
Eli Friedmana62f3e12008-02-09 08:50:58 +00001466}
Devang Patel30efa2e2007-10-23 20:28:39 +00001467
Fariborz Jahanianb517e902008-12-15 20:35:07 +00001468LValue CodeGenFunction::EmitLValueForBitfield(llvm::Value* BaseValue,
Anders Carlssoncfd30122009-11-17 03:57:07 +00001469 const FieldDecl* Field,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001470 unsigned CVRQualifiers) {
Anders Carlsson8af896c2009-07-23 17:01:21 +00001471 CodeGenTypes::BitFieldInfo Info = CGM.getTypes().getBitFieldInfo(Field);
1472
Mike Stump18bb9282009-05-16 07:57:57 +00001473 // FIXME: CodeGenTypes should expose a method to get the appropriate type for
1474 // FieldTy (the appropriate type is ABI-dependent).
Mike Stump4a3999f2009-09-09 13:00:44 +00001475 const llvm::Type *FieldTy =
Daniel Dunbar3d926cb2009-02-17 18:31:04 +00001476 CGM.getTypes().ConvertTypeForMem(Field->getType());
Fariborz Jahanianb517e902008-12-15 20:35:07 +00001477 const llvm::PointerType *BaseTy =
1478 cast<llvm::PointerType>(BaseValue->getType());
1479 unsigned AS = BaseTy->getAddressSpace();
1480 BaseValue = Builder.CreateBitCast(BaseValue,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001481 llvm::PointerType::get(FieldTy, AS),
Fariborz Jahanianb517e902008-12-15 20:35:07 +00001482 "tmp");
Mike Stump4a3999f2009-09-09 13:00:44 +00001483
1484 llvm::Value *Idx =
Owen Anderson41a75022009-08-13 21:57:51 +00001485 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), Info.FieldNo);
Anders Carlsson8af896c2009-07-23 17:01:21 +00001486 llvm::Value *V = Builder.CreateGEP(BaseValue, Idx, "tmp");
Mike Stump4a3999f2009-09-09 13:00:44 +00001487
Anders Carlsson8af896c2009-07-23 17:01:21 +00001488 return LValue::MakeBitfield(V, Info.Start, Info.Size,
Fariborz Jahanianb517e902008-12-15 20:35:07 +00001489 Field->getType()->isSignedIntegerType(),
1490 Field->getType().getCVRQualifiers()|CVRQualifiers);
1491}
1492
Eli Friedmana62f3e12008-02-09 08:50:58 +00001493LValue CodeGenFunction::EmitLValueForField(llvm::Value* BaseValue,
Anders Carlssoncfd30122009-11-17 03:57:07 +00001494 const FieldDecl* Field,
Mike Stump11289f42009-09-09 15:08:12 +00001495 unsigned CVRQualifiers) {
Fariborz Jahanianb517e902008-12-15 20:35:07 +00001496 if (Field->isBitField())
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001497 return EmitLValueForBitfield(BaseValue, Field, CVRQualifiers);
Mike Stump4a3999f2009-09-09 13:00:44 +00001498
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001499 unsigned idx = CGM.getTypes().getLLVMFieldNo(Field);
Fariborz Jahanianb517e902008-12-15 20:35:07 +00001500 llvm::Value *V = Builder.CreateStructGEP(BaseValue, idx, "tmp");
Eli Friedman133e8042008-05-29 11:33:25 +00001501
Devang Pateled93c3c2007-10-26 19:42:18 +00001502 // Match union field type.
Anders Carlsson5d8645b2010-01-29 05:05:36 +00001503 if (Field->getParent()->isUnion()) {
Mike Stump4a3999f2009-09-09 13:00:44 +00001504 const llvm::Type *FieldTy =
Eli Friedman327944b2008-06-13 23:01:12 +00001505 CGM.getTypes().ConvertTypeForMem(Field->getType());
Mike Stump4a3999f2009-09-09 13:00:44 +00001506 const llvm::PointerType * BaseTy =
Devang Patelffe1e212007-10-30 20:59:40 +00001507 cast<llvm::PointerType>(BaseValue->getType());
Eli Friedman9a5ffcb2008-05-21 13:24:44 +00001508 unsigned AS = BaseTy->getAddressSpace();
Mike Stump4a3999f2009-09-09 13:00:44 +00001509 V = Builder.CreateBitCast(V,
1510 llvm::PointerType::get(FieldTy, AS),
Eli Friedman9a5ffcb2008-05-21 13:24:44 +00001511 "tmp");
Devang Pateled93c3c2007-10-26 19:42:18 +00001512 }
Eli Friedmanf7f9f682009-05-30 21:09:44 +00001513 if (Field->getType()->isReferenceType())
1514 V = Builder.CreateLoad(V, "tmp");
John McCall8ccfcb52009-09-24 19:53:00 +00001515
1516 Qualifiers Quals = MakeQualifiers(Field->getType());
1517 Quals.addCVRQualifiers(CVRQualifiers);
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +00001518 // __weak attribute on a field is ignored.
John McCall8ccfcb52009-09-24 19:53:00 +00001519 if (Quals.getObjCGCAttr() == Qualifiers::Weak)
1520 Quals.removeObjCGCAttr();
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +00001521
John McCall8ccfcb52009-09-24 19:53:00 +00001522 return LValue::MakeAddr(V, Quals);
Devang Patel30efa2e2007-10-23 20:28:39 +00001523}
1524
Anders Carlssondb78f0a2010-01-29 05:24:29 +00001525LValue
1526CodeGenFunction::EmitLValueForFieldInitialization(llvm::Value* BaseValue,
1527 const FieldDecl* Field,
1528 unsigned CVRQualifiers) {
1529 QualType FieldType = Field->getType();
1530
1531 if (!FieldType->isReferenceType())
1532 return EmitLValueForField(BaseValue, Field, CVRQualifiers);
1533
1534 unsigned idx = CGM.getTypes().getLLVMFieldNo(Field);
1535 llvm::Value *V = Builder.CreateStructGEP(BaseValue, idx, "tmp");
1536
1537 assert(!FieldType.getObjCGCAttr() && "fields cannot have GC attrs");
1538
1539 return LValue::MakeAddr(V, MakeQualifiers(FieldType));
1540}
1541
Chris Lattner28bcf1a2009-03-18 18:28:57 +00001542LValue CodeGenFunction::EmitCompoundLiteralLValue(const CompoundLiteralExpr* E){
Daniel Dunbar27bacaf2010-02-16 19:43:39 +00001543 llvm::Value *DeclPtr = CreateMemTemp(E->getType(), ".compoundliteral");
Eli Friedman9fd8b682008-05-13 23:18:27 +00001544 const Expr* InitExpr = E->getInitializer();
John McCall8ccfcb52009-09-24 19:53:00 +00001545 LValue Result = LValue::MakeAddr(DeclPtr, MakeQualifiers(E->getType()));
Eli Friedman9fd8b682008-05-13 23:18:27 +00001546
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001547 if (E->getType()->isComplexType())
Eli Friedman9fd8b682008-05-13 23:18:27 +00001548 EmitComplexExprIntoAddr(InitExpr, DeclPtr, false);
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001549 else if (hasAggregateLLVMType(E->getType()))
Eli Friedman9fd8b682008-05-13 23:18:27 +00001550 EmitAnyExpr(InitExpr, DeclPtr, false);
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001551 else
Eli Friedman9fd8b682008-05-13 23:18:27 +00001552 EmitStoreThroughLValue(EmitAnyExpr(InitExpr), Result, E->getType());
Eli Friedman9fd8b682008-05-13 23:18:27 +00001553
1554 return Result;
1555}
1556
Anders Carlsson1450adb2009-09-15 16:35:24 +00001557LValue
1558CodeGenFunction::EmitConditionalOperatorLValue(const ConditionalOperator* E) {
1559 if (E->isLvalue(getContext()) == Expr::LV_Valid) {
Eli Friedman2e06e8b2009-12-25 05:29:40 +00001560 if (int Cond = ConstantFoldsToSimpleInteger(E->getCond())) {
1561 Expr *Live = Cond == 1 ? E->getLHS() : E->getRHS();
1562 if (Live)
1563 return EmitLValue(Live);
1564 }
1565
1566 if (!E->getLHS())
1567 return EmitUnsupportedLValue(E, "conditional operator with missing LHS");
1568
Anders Carlsson1450adb2009-09-15 16:35:24 +00001569 llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true");
1570 llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false");
1571 llvm::BasicBlock *ContBlock = createBasicBlock("cond.end");
1572
Eli Friedmanb8841af2009-12-25 06:17:05 +00001573 EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock);
Anders Carlsson1450adb2009-09-15 16:35:24 +00001574
Anders Carlsson9b942c62010-02-04 17:26:01 +00001575 // Any temporaries created here are conditional.
1576 BeginConditionalBranch();
Anders Carlsson1450adb2009-09-15 16:35:24 +00001577 EmitBlock(LHSBlock);
Anders Carlsson1450adb2009-09-15 16:35:24 +00001578 LValue LHS = EmitLValue(E->getLHS());
Anders Carlsson9b942c62010-02-04 17:26:01 +00001579 EndConditionalBranch();
1580
Anders Carlsson1450adb2009-09-15 16:35:24 +00001581 if (!LHS.isSimple())
1582 return EmitUnsupportedLValue(E, "conditional operator");
1583
Daniel Dunbara7566f12010-02-09 02:48:28 +00001584 // FIXME: We shouldn't need an alloca for this.
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001585 llvm::Value *Temp = CreateTempAlloca(LHS.getAddress()->getType(),"condtmp");
Anders Carlsson1450adb2009-09-15 16:35:24 +00001586 Builder.CreateStore(LHS.getAddress(), Temp);
1587 EmitBranch(ContBlock);
1588
Anders Carlsson9b942c62010-02-04 17:26:01 +00001589 // Any temporaries created here are conditional.
1590 BeginConditionalBranch();
Anders Carlsson1450adb2009-09-15 16:35:24 +00001591 EmitBlock(RHSBlock);
1592 LValue RHS = EmitLValue(E->getRHS());
Anders Carlsson9b942c62010-02-04 17:26:01 +00001593 EndConditionalBranch();
Anders Carlsson1450adb2009-09-15 16:35:24 +00001594 if (!RHS.isSimple())
1595 return EmitUnsupportedLValue(E, "conditional operator");
1596
1597 Builder.CreateStore(RHS.getAddress(), Temp);
1598 EmitBranch(ContBlock);
1599
1600 EmitBlock(ContBlock);
1601
1602 Temp = Builder.CreateLoad(Temp, "lv");
John McCall8ccfcb52009-09-24 19:53:00 +00001603 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
Anders Carlsson1450adb2009-09-15 16:35:24 +00001604 }
1605
Daniel Dunbarbf1fe8c2009-03-24 02:38:23 +00001606 // ?: here should be an aggregate.
Mike Stump4a3999f2009-09-09 13:00:44 +00001607 assert((hasAggregateLLVMType(E->getType()) &&
Daniel Dunbarbf1fe8c2009-03-24 02:38:23 +00001608 !E->getType()->isAnyComplexType()) &&
1609 "Unexpected conditional operator!");
1610
Daniel Dunbard0bc7b92010-02-05 19:38:31 +00001611 return EmitAggExprToLValue(E);
Daniel Dunbarbf1fe8c2009-03-24 02:38:23 +00001612}
1613
Mike Stump65511702009-11-16 06:50:58 +00001614/// EmitCastLValue - Casts are never lvalues unless that cast is a dynamic_cast.
1615/// If the cast is a dynamic_cast, we can have the usual lvalue result,
1616/// otherwise if a cast is needed by the code generator in an lvalue context,
1617/// then it must mean that we need the address of an aggregate in order to
1618/// access one of its fields. This can happen for all the reasons that casts
1619/// are permitted with aggregate result, including noop aggregate casts, and
1620/// cast from scalar to union.
Chris Lattner28bcf1a2009-03-18 18:28:57 +00001621LValue CodeGenFunction::EmitCastLValue(const CastExpr *E) {
Anders Carlssond95f9602009-09-12 16:16:49 +00001622 switch (E->getCastKind()) {
1623 default:
Eli Friedman8c98dff2009-11-16 05:48:01 +00001624 return EmitUnsupportedLValue(E, "unexpected cast lvalue");
1625
Mike Stump65511702009-11-16 06:50:58 +00001626 case CastExpr::CK_Dynamic: {
1627 LValue LV = EmitLValue(E->getSubExpr());
1628 llvm::Value *V = LV.getAddress();
1629 const CXXDynamicCastExpr *DCE = cast<CXXDynamicCastExpr>(E);
1630 return LValue::MakeAddr(EmitDynamicCast(V, DCE),
1631 MakeQualifiers(E->getType()));
1632 }
1633
Anders Carlssond95f9602009-09-12 16:16:49 +00001634 case CastExpr::CK_NoOp:
1635 case CastExpr::CK_ConstructorConversion:
1636 case CastExpr::CK_UserDefinedConversion:
Fariborz Jahanian2b9fc832009-12-15 21:34:52 +00001637 case CastExpr::CK_AnyPointerToObjCPointerCast:
Chris Lattner28bcf1a2009-03-18 18:28:57 +00001638 return EmitLValue(E->getSubExpr());
Anders Carlssond95f9602009-09-12 16:16:49 +00001639
John McCalld9c7c6562010-03-30 23:58:03 +00001640 case CastExpr::CK_UncheckedDerivedToBase:
Anders Carlssond95f9602009-09-12 16:16:49 +00001641 case CastExpr::CK_DerivedToBase: {
1642 const RecordType *DerivedClassTy =
1643 E->getSubExpr()->getType()->getAs<RecordType>();
1644 CXXRecordDecl *DerivedClassDecl =
1645 cast<CXXRecordDecl>(DerivedClassTy->getDecl());
Chris Lattner28bcf1a2009-03-18 18:28:57 +00001646
Anders Carlssond95f9602009-09-12 16:16:49 +00001647 const RecordType *BaseClassTy = E->getType()->getAs<RecordType>();
1648 CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseClassTy->getDecl());
1649
1650 LValue LV = EmitLValue(E->getSubExpr());
1651
1652 // Perform the derived-to-base conversion
1653 llvm::Value *Base =
Anders Carlsson8c793172009-11-23 17:57:54 +00001654 GetAddressOfBaseClass(LV.getAddress(), DerivedClassDecl,
1655 BaseClassDecl, /*NullCheckValue=*/false);
Anders Carlssond95f9602009-09-12 16:16:49 +00001656
John McCall8ccfcb52009-09-24 19:53:00 +00001657 return LValue::MakeAddr(Base, MakeQualifiers(E->getType()));
Anders Carlssond95f9602009-09-12 16:16:49 +00001658 }
Daniel Dunbar9c4e4652010-02-05 20:02:42 +00001659 case CastExpr::CK_ToUnion:
1660 return EmitAggExprToLValue(E);
Eli Friedman8c98dff2009-11-16 05:48:01 +00001661 case CastExpr::CK_BaseToDerived: {
Anders Carlsson8c793172009-11-23 17:57:54 +00001662 const RecordType *BaseClassTy =
1663 E->getSubExpr()->getType()->getAs<RecordType>();
1664 CXXRecordDecl *BaseClassDecl =
1665 cast<CXXRecordDecl>(BaseClassTy->getDecl());
1666
1667 const RecordType *DerivedClassTy = E->getType()->getAs<RecordType>();
1668 CXXRecordDecl *DerivedClassDecl =
1669 cast<CXXRecordDecl>(DerivedClassTy->getDecl());
1670
1671 LValue LV = EmitLValue(E->getSubExpr());
1672
1673 // Perform the base-to-derived conversion
1674 llvm::Value *Derived =
1675 GetAddressOfDerivedClass(LV.getAddress(), BaseClassDecl,
1676 DerivedClassDecl, /*NullCheckValue=*/false);
1677
1678 return LValue::MakeAddr(Derived, MakeQualifiers(E->getType()));
Eli Friedman8c98dff2009-11-16 05:48:01 +00001679 }
Anders Carlsson50cb3212009-11-14 21:21:42 +00001680 case CastExpr::CK_BitCast: {
Eli Friedman8c98dff2009-11-16 05:48:01 +00001681 // This must be a reinterpret_cast (or c-style equivalent).
1682 const ExplicitCastExpr *CE = cast<ExplicitCastExpr>(E);
Anders Carlsson50cb3212009-11-14 21:21:42 +00001683
1684 LValue LV = EmitLValue(E->getSubExpr());
1685 llvm::Value *V = Builder.CreateBitCast(LV.getAddress(),
1686 ConvertType(CE->getTypeAsWritten()));
1687 return LValue::MakeAddr(V, MakeQualifiers(E->getType()));
1688 }
Anders Carlssond95f9602009-09-12 16:16:49 +00001689 }
Chris Lattner28bcf1a2009-03-18 18:28:57 +00001690}
1691
Fariborz Jahaniane4d94ce2009-10-20 23:29:04 +00001692LValue CodeGenFunction::EmitNullInitializationLValue(
1693 const CXXZeroInitValueExpr *E) {
1694 QualType Ty = E->getType();
Daniel Dunbara7566f12010-02-09 02:48:28 +00001695 LValue LV = LValue::MakeAddr(CreateMemTemp(Ty), MakeQualifiers(Ty));
1696 EmitMemSetToZero(LV.getAddress(), Ty);
1697 return LV;
Fariborz Jahaniane4d94ce2009-10-20 23:29:04 +00001698}
1699
Chris Lattnere47e4402007-06-01 18:02:12 +00001700//===--------------------------------------------------------------------===//
1701// Expression Emission
1702//===--------------------------------------------------------------------===//
1703
Chris Lattner76ba8492007-08-20 22:37:10 +00001704
Anders Carlsson17490832009-12-24 20:40:36 +00001705RValue CodeGenFunction::EmitCallExpr(const CallExpr *E,
1706 ReturnValueSlot ReturnValue) {
Daniel Dunbarcdbb5e32009-02-20 18:06:48 +00001707 // Builtins never have block type.
Daniel Dunbarbb197e42009-01-09 16:50:52 +00001708 if (E->getCallee()->getType()->isBlockPointerType())
Anders Carlssonbfb36712009-12-24 21:13:40 +00001709 return EmitBlockCallExpr(E, ReturnValue);
Daniel Dunbarbb197e42009-01-09 16:50:52 +00001710
Anders Carlssone5fd6f22009-04-03 22:50:24 +00001711 if (const CXXMemberCallExpr *CE = dyn_cast<CXXMemberCallExpr>(E))
Anders Carlssonbfb36712009-12-24 21:13:40 +00001712 return EmitCXXMemberCallExpr(CE, ReturnValue);
Mike Stump4a3999f2009-09-09 13:00:44 +00001713
Daniel Dunbarcdbb5e32009-02-20 18:06:48 +00001714 const Decl *TargetDecl = 0;
Daniel Dunbar27032de2009-02-20 19:34:33 +00001715 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E->getCallee())) {
1716 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CE->getSubExpr())) {
1717 TargetDecl = DRE->getDecl();
1718 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(TargetDecl))
Douglas Gregor15fc9562009-09-12 00:22:50 +00001719 if (unsigned builtinID = FD->getBuiltinID())
Daniel Dunbar27032de2009-02-20 19:34:33 +00001720 return EmitBuiltinExpr(FD, builtinID, E);
Daniel Dunbarcdbb5e32009-02-20 18:06:48 +00001721 }
1722 }
1723
Chris Lattner4ca97c32009-06-13 00:26:38 +00001724 if (const CXXOperatorCallExpr *CE = dyn_cast<CXXOperatorCallExpr>(E))
Anders Carlsson4034a952009-05-27 04:18:27 +00001725 if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(TargetDecl))
Anders Carlssonbfb36712009-12-24 21:13:40 +00001726 return EmitCXXOperatorMemberCallExpr(CE, MD, ReturnValue);
Mike Stump4a3999f2009-09-09 13:00:44 +00001727
Eli Friedman8aaff692009-12-08 02:09:46 +00001728 if (isa<CXXPseudoDestructorExpr>(E->getCallee()->IgnoreParens())) {
Douglas Gregorad8a3362009-09-04 17:36:40 +00001729 // C++ [expr.pseudo]p1:
Mike Stump4a3999f2009-09-09 13:00:44 +00001730 // The result shall only be used as the operand for the function call
Douglas Gregorad8a3362009-09-04 17:36:40 +00001731 // operator (), and the result of such a call has type void. The only
1732 // effect is the evaluation of the postfix-expression before the dot or
1733 // arrow.
1734 EmitScalarExpr(E->getCallee());
1735 return RValue::get(0);
1736 }
Mike Stump4a3999f2009-09-09 13:00:44 +00001737
Chris Lattner2da04b32007-08-24 05:35:26 +00001738 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
Anders Carlsson17490832009-12-24 20:40:36 +00001739 return EmitCall(E->getCallee()->getType(), Callee, ReturnValue,
Anders Carlsson3a9463b2009-05-27 01:22:39 +00001740 E->arg_begin(), E->arg_end(), TargetDecl);
Chris Lattner9e47ead2007-08-31 04:44:06 +00001741}
1742
Daniel Dunbar8cde00a2008-09-04 03:20:13 +00001743LValue CodeGenFunction::EmitBinaryOperatorLValue(const BinaryOperator *E) {
Chris Lattnere541ea32009-05-12 21:28:12 +00001744 // Comma expressions just emit their LHS then their RHS as an l-value.
1745 if (E->getOpcode() == BinaryOperator::Comma) {
1746 EmitAnyExpr(E->getLHS());
Eli Friedman5445f6e2009-12-07 20:18:11 +00001747 EnsureInsertPoint();
Chris Lattnere541ea32009-05-12 21:28:12 +00001748 return EmitLValue(E->getRHS());
1749 }
Mike Stump4a3999f2009-09-09 13:00:44 +00001750
Fariborz Jahanian038374f2009-10-26 21:58:25 +00001751 if (E->getOpcode() == BinaryOperator::PtrMemD ||
1752 E->getOpcode() == BinaryOperator::PtrMemI)
Fariborz Jahanianffba6622009-10-22 22:57:31 +00001753 return EmitPointerToDataMemberBinaryExpr(E);
1754
Daniel Dunbar8cde00a2008-09-04 03:20:13 +00001755 // Can only get l-value for binary operator expressions which are a
1756 // simple assignment of aggregate type.
1757 if (E->getOpcode() != BinaryOperator::Assign)
1758 return EmitUnsupportedLValue(E, "binary l-value expression");
1759
Anders Carlsson0999aaf2009-10-19 18:28:22 +00001760 if (!hasAggregateLLVMType(E->getType())) {
1761 // Emit the LHS as an l-value.
1762 LValue LV = EmitLValue(E->getLHS());
1763
1764 llvm::Value *RHS = EmitScalarExpr(E->getRHS());
1765 EmitStoreOfScalar(RHS, LV.getAddress(), LV.isVolatileQualified(),
1766 E->getType());
1767 return LV;
1768 }
1769
Daniel Dunbard0bc7b92010-02-05 19:38:31 +00001770 return EmitAggExprToLValue(E);
Daniel Dunbar8cde00a2008-09-04 03:20:13 +00001771}
1772
Christopher Lambd91c3d42007-12-29 05:02:41 +00001773LValue CodeGenFunction::EmitCallExprLValue(const CallExpr *E) {
Christopher Lambd91c3d42007-12-29 05:02:41 +00001774 RValue RV = EmitCallExpr(E);
Anders Carlsson4ae70ff2009-05-27 01:45:47 +00001775
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001776 if (!RV.isScalar())
1777 return LValue::MakeAddr(RV.getAggregateAddr(),MakeQualifiers(E->getType()));
1778
1779 assert(E->getCallReturnType()->isReferenceType() &&
1780 "Can't have a scalar return unless the return type is a "
1781 "reference type!");
Mike Stump4a3999f2009-09-09 13:00:44 +00001782
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001783 return LValue::MakeAddr(RV.getScalarVal(), MakeQualifiers(E->getType()));
Christopher Lambd91c3d42007-12-29 05:02:41 +00001784}
1785
Daniel Dunbar8d9dc4a2009-02-11 20:59:32 +00001786LValue CodeGenFunction::EmitVAArgExprLValue(const VAArgExpr *E) {
1787 // FIXME: This shouldn't require another copy.
Daniel Dunbard0bc7b92010-02-05 19:38:31 +00001788 return EmitAggExprToLValue(E);
Daniel Dunbar8d9dc4a2009-02-11 20:59:32 +00001789}
1790
Anders Carlsson3be22e22009-05-30 23:23:33 +00001791LValue CodeGenFunction::EmitCXXConstructLValue(const CXXConstructExpr *E) {
Daniel Dunbara7566f12010-02-09 02:48:28 +00001792 llvm::Value *Temp = CreateMemTemp(E->getType(), "tmp");
Anders Carlsson3be22e22009-05-30 23:23:33 +00001793 EmitCXXConstructExpr(Temp, E);
John McCall8ccfcb52009-09-24 19:53:00 +00001794 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
Anders Carlsson3be22e22009-05-30 23:23:33 +00001795}
1796
Anders Carlssonfd2af0c2009-05-30 23:30:54 +00001797LValue
Mike Stumpc9b231c2009-11-15 08:09:41 +00001798CodeGenFunction::EmitCXXTypeidLValue(const CXXTypeidExpr *E) {
1799 llvm::Value *Temp = EmitCXXTypeidExpr(E);
1800 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
1801}
1802
1803LValue
Anders Carlssonfd2af0c2009-05-30 23:30:54 +00001804CodeGenFunction::EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr *E) {
1805 LValue LV = EmitLValue(E->getSubExpr());
Anders Carlsson8eb93e72009-05-31 00:34:10 +00001806 PushCXXTemporary(E->getTemporary(), LV.getAddress());
Anders Carlssonfd2af0c2009-05-30 23:30:54 +00001807 return LV;
1808}
1809
Daniel Dunbarc8317a42008-08-23 10:51:21 +00001810LValue CodeGenFunction::EmitObjCMessageExprLValue(const ObjCMessageExpr *E) {
1811 // Can only get l-value for message expression returning aggregate type
1812 RValue RV = EmitObjCMessageExpr(E);
1813 // FIXME: can this be volatile?
John McCall8ccfcb52009-09-24 19:53:00 +00001814 return LValue::MakeAddr(RV.getAggregateAddr(), MakeQualifiers(E->getType()));
Daniel Dunbarc8317a42008-08-23 10:51:21 +00001815}
1816
Daniel Dunbar722f4242009-04-22 05:08:15 +00001817llvm::Value *CodeGenFunction::EmitIvarOffset(const ObjCInterfaceDecl *Interface,
Daniel Dunbar1c64e5d2008-09-24 04:00:38 +00001818 const ObjCIvarDecl *Ivar) {
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001819 return CGM.getObjCRuntime().EmitIvarOffset(*this, Interface, Ivar);
Daniel Dunbar1c64e5d2008-09-24 04:00:38 +00001820}
1821
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00001822LValue CodeGenFunction::EmitLValueForIvar(QualType ObjectTy,
1823 llvm::Value *BaseValue,
Daniel Dunbar1c64e5d2008-09-24 04:00:38 +00001824 const ObjCIvarDecl *Ivar,
1825 unsigned CVRQualifiers) {
Chris Lattnerc4688d22009-04-17 17:44:48 +00001826 return CGM.getObjCRuntime().EmitObjCValueForIvar(*this, ObjectTy, BaseValue,
Daniel Dunbar9ebf9512009-04-21 01:19:28 +00001827 Ivar, CVRQualifiers);
Daniel Dunbar1c64e5d2008-09-24 04:00:38 +00001828}
1829
1830LValue CodeGenFunction::EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E) {
Anders Carlssonc13b85a2008-08-25 01:53:23 +00001831 // FIXME: A lot of the code below could be shared with EmitMemberExpr.
1832 llvm::Value *BaseValue = 0;
1833 const Expr *BaseExpr = E->getBase();
John McCall8ccfcb52009-09-24 19:53:00 +00001834 Qualifiers BaseQuals;
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00001835 QualType ObjectTy;
Anders Carlssonc13b85a2008-08-25 01:53:23 +00001836 if (E->isArrow()) {
1837 BaseValue = EmitScalarExpr(BaseExpr);
Steve Naroff7cae42b2009-07-10 23:34:53 +00001838 ObjectTy = BaseExpr->getType()->getPointeeType();
John McCall8ccfcb52009-09-24 19:53:00 +00001839 BaseQuals = ObjectTy.getQualifiers();
Anders Carlssonc13b85a2008-08-25 01:53:23 +00001840 } else {
1841 LValue BaseLV = EmitLValue(BaseExpr);
1842 // FIXME: this isn't right for bitfields.
1843 BaseValue = BaseLV.getAddress();
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00001844 ObjectTy = BaseExpr->getType();
John McCall8ccfcb52009-09-24 19:53:00 +00001845 BaseQuals = ObjectTy.getQualifiers();
Anders Carlssonc13b85a2008-08-25 01:53:23 +00001846 }
Daniel Dunbar1c64e5d2008-09-24 04:00:38 +00001847
Fariborz Jahaniande1d3242009-09-16 23:11:23 +00001848 LValue LV =
John McCall8ccfcb52009-09-24 19:53:00 +00001849 EmitLValueForIvar(ObjectTy, BaseValue, E->getDecl(),
1850 BaseQuals.getCVRQualifiers());
Fariborz Jahaniande1d3242009-09-16 23:11:23 +00001851 setObjCGCLValueClass(getContext(), E, LV);
1852 return LV;
Chris Lattner4bd55962008-03-30 23:03:07 +00001853}
1854
Mike Stump4a3999f2009-09-09 13:00:44 +00001855LValue
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +00001856CodeGenFunction::EmitObjCPropertyRefLValue(const ObjCPropertyRefExpr *E) {
Mike Stump4a3999f2009-09-09 13:00:44 +00001857 // This is a special l-value that just issues sends when we load or store
1858 // through it.
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +00001859 return LValue::MakePropertyRef(E, E->getType().getCVRQualifiers());
1860}
1861
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001862LValue CodeGenFunction::EmitObjCKVCRefLValue(
Fariborz Jahanian9a846652009-08-20 17:02:02 +00001863 const ObjCImplicitSetterGetterRefExpr *E) {
Mike Stump4a3999f2009-09-09 13:00:44 +00001864 // This is a special l-value that just issues sends when we load or store
1865 // through it.
Fariborz Jahanian9ac53512008-11-22 22:30:21 +00001866 return LValue::MakeKVCRef(E, E->getType().getCVRQualifiers());
1867}
1868
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001869LValue CodeGenFunction::EmitObjCSuperExprLValue(const ObjCSuperExpr *E) {
Douglas Gregor8ea1f532008-11-04 14:56:14 +00001870 return EmitUnsupportedLValue(E, "use of super");
1871}
1872
Chris Lattnera4185c52009-04-25 19:35:26 +00001873LValue CodeGenFunction::EmitStmtExprLValue(const StmtExpr *E) {
Chris Lattnera4185c52009-04-25 19:35:26 +00001874 // Can only get l-value for message expression returning aggregate type
1875 RValue RV = EmitAnyExprToTemp(E);
John McCall8ccfcb52009-09-24 19:53:00 +00001876 return LValue::MakeAddr(RV.getAggregateAddr(), MakeQualifiers(E->getType()));
Chris Lattnera4185c52009-04-25 19:35:26 +00001877}
1878
Anders Carlsson0435ed52009-12-24 19:08:58 +00001879RValue CodeGenFunction::EmitCall(QualType CalleeType, llvm::Value *Callee,
Anders Carlsson17490832009-12-24 20:40:36 +00001880 ReturnValueSlot ReturnValue,
Anders Carlsson3a9463b2009-05-27 01:22:39 +00001881 CallExpr::const_arg_iterator ArgBeg,
1882 CallExpr::const_arg_iterator ArgEnd,
1883 const Decl *TargetDecl) {
Mike Stump4a3999f2009-09-09 13:00:44 +00001884 // Get the actual function type. The callee type will always be a pointer to
1885 // function type or a block pointer type.
1886 assert(CalleeType->isFunctionPointerType() &&
Anders Carlssond8db8532009-04-07 18:53:02 +00001887 "Call must have function pointer type!");
1888
John McCall6fd4c232009-10-23 08:22:42 +00001889 CalleeType = getContext().getCanonicalType(CalleeType);
1890
John McCallab26cfa2010-02-05 21:31:56 +00001891 const FunctionType *FnType
1892 = cast<FunctionType>(cast<PointerType>(CalleeType)->getPointeeType());
1893 QualType ResultType = FnType->getResultType();
Daniel Dunbarc722b852008-08-30 03:02:31 +00001894
1895 CallArgList Args;
John McCall6fd4c232009-10-23 08:22:42 +00001896 EmitCallArgs(Args, dyn_cast<FunctionProtoType>(FnType), ArgBeg, ArgEnd);
Daniel Dunbarc722b852008-08-30 03:02:31 +00001897
John McCallab26cfa2010-02-05 21:31:56 +00001898 return EmitCall(CGM.getTypes().getFunctionInfo(Args, FnType),
Anders Carlsson17490832009-12-24 20:40:36 +00001899 Callee, ReturnValue, Args, TargetDecl);
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001900}
Fariborz Jahanianffba6622009-10-22 22:57:31 +00001901
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001902LValue CodeGenFunction::
1903EmitPointerToDataMemberBinaryExpr(const BinaryOperator *E) {
Eli Friedman928a5672009-11-18 05:01:17 +00001904 llvm::Value *BaseV;
Fariborz Jahanian038374f2009-10-26 21:58:25 +00001905 if (E->getOpcode() == BinaryOperator::PtrMemI)
Eli Friedman928a5672009-11-18 05:01:17 +00001906 BaseV = EmitScalarExpr(E->getLHS());
1907 else
1908 BaseV = EmitLValue(E->getLHS()).getAddress();
Fariborz Jahanianffba6622009-10-22 22:57:31 +00001909 const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(getLLVMContext());
1910 BaseV = Builder.CreateBitCast(BaseV, i8Ty);
Eli Friedman928a5672009-11-18 05:01:17 +00001911 llvm::Value *OffsetV = EmitScalarExpr(E->getRHS());
Fariborz Jahanianffba6622009-10-22 22:57:31 +00001912 llvm::Value *AddV = Builder.CreateInBoundsGEP(BaseV, OffsetV, "add.ptr");
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001913
Fariborz Jahanianffba6622009-10-22 22:57:31 +00001914 QualType Ty = E->getRHS()->getType();
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001915 Ty = Ty->getAs<MemberPointerType>()->getPointeeType();
1916
1917 const llvm::Type *PType = ConvertType(getContext().getPointerType(Ty));
Fariborz Jahanianffba6622009-10-22 22:57:31 +00001918 AddV = Builder.CreateBitCast(AddV, PType);
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001919 return LValue::MakeAddr(AddV, MakeQualifiers(Ty));
Fariborz Jahanianffba6622009-10-22 22:57:31 +00001920}
1921