blob: 53794152a854fa36f22de4e3877b70070deaa87a [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- CGExpr.cpp - Emit LLVM Code from Expressions ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit Expr nodes as LLVM code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenFunction.h"
15#include "CodeGenModule.h"
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000016#include "CGCall.h"
Daniel Dunbar198bcb42010-03-31 01:09:11 +000017#include "CGRecordLayout.h"
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +000018#include "CGObjCRuntime.h"
Daniel Dunbarde7fb842008-08-11 05:00:27 +000019#include "clang/AST/ASTContext.h"
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000020#include "clang/AST/DeclObjC.h"
Mike Stump15037ca2009-12-15 00:35:12 +000021#include "llvm/Intrinsics.h"
Mike Stump41513442009-12-15 00:59:40 +000022#include "clang/CodeGen/CodeGenOptions.h"
Eli Friedman316bb1b2008-05-17 20:03:47 +000023#include "llvm/Target/TargetData.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000024using namespace clang;
25using namespace CodeGen;
26
27//===--------------------------------------------------------------------===//
28// Miscellaneous Helper Methods
29//===--------------------------------------------------------------------===//
30
31/// CreateTempAlloca - This creates a alloca and inserts it into the entry
32/// block.
33llvm::AllocaInst *CodeGenFunction::CreateTempAlloca(const llvm::Type *Ty,
Daniel Dunbar259e9cc2009-10-19 01:21:05 +000034 const llvm::Twine &Name) {
Chris Lattnerf1466842009-03-22 00:24:14 +000035 if (!Builder.isNamePreserving())
Daniel Dunbar259e9cc2009-10-19 01:21:05 +000036 return new llvm::AllocaInst(Ty, 0, "", AllocaInsertPt);
Devang Pateld35e2e02009-10-12 22:29:02 +000037 return new llvm::AllocaInst(Ty, 0, Name, AllocaInsertPt);
Reid Spencer5f016e22007-07-11 17:01:13 +000038}
39
John McCallac418162010-04-22 01:10:34 +000040void CodeGenFunction::InitTempAlloca(llvm::AllocaInst *Var,
41 llvm::Value *Init) {
42 llvm::StoreInst *Store = new llvm::StoreInst(Init, Var);
43 llvm::BasicBlock *Block = AllocaInsertPt->getParent();
44 Block->getInstList().insertAfter(&*AllocaInsertPt, Store);
45}
46
Daniel Dunbar9bd4da22010-02-16 19:44:13 +000047llvm::Value *CodeGenFunction::CreateIRTemp(QualType Ty,
48 const llvm::Twine &Name) {
49 llvm::AllocaInst *Alloc = CreateTempAlloca(ConvertType(Ty), Name);
50 // FIXME: Should we prefer the preferred type alignment here?
51 CharUnits Align = getContext().getTypeAlignInChars(Ty);
52 Alloc->setAlignment(Align.getQuantity());
53 return Alloc;
54}
55
56llvm::Value *CodeGenFunction::CreateMemTemp(QualType Ty,
57 const llvm::Twine &Name) {
Daniel Dunbar195337d2010-02-09 02:48:28 +000058 llvm::AllocaInst *Alloc = CreateTempAlloca(ConvertTypeForMem(Ty), Name);
59 // FIXME: Should we prefer the preferred type alignment here?
60 CharUnits Align = getContext().getTypeAlignInChars(Ty);
61 Alloc->setAlignment(Align.getQuantity());
62 return Alloc;
63}
64
Reid Spencer5f016e22007-07-11 17:01:13 +000065/// EvaluateExprAsBool - Perform the usual unary conversions on the specified
66/// expression and compare the result against zero, returning an Int1Ty value.
67llvm::Value *CodeGenFunction::EvaluateExprAsBool(const Expr *E) {
Chris Lattner9069fa22007-08-26 16:46:58 +000068 QualType BoolTy = getContext().BoolTy;
Eli Friedman3a173702009-12-11 09:26:29 +000069 if (E->getType()->isMemberFunctionPointerType()) {
Daniel Dunbar18aba0d2010-02-05 19:38:31 +000070 LValue LV = EmitAggExprToLValue(E);
Eli Friedman3a173702009-12-11 09:26:29 +000071
72 // Get the pointer.
Daniel Dunbar18aba0d2010-02-05 19:38:31 +000073 llvm::Value *FuncPtr = Builder.CreateStructGEP(LV.getAddress(), 0,
74 "src.ptr");
Eli Friedman3a173702009-12-11 09:26:29 +000075 FuncPtr = Builder.CreateLoad(FuncPtr);
76
77 llvm::Value *IsNotNull =
78 Builder.CreateICmpNE(FuncPtr,
79 llvm::Constant::getNullValue(FuncPtr->getType()),
80 "tobool");
81
82 return IsNotNull;
83 }
Chris Lattner9b2dc282008-04-04 16:54:41 +000084 if (!E->getType()->isAnyComplexType())
Chris Lattner9069fa22007-08-26 16:46:58 +000085 return EmitScalarConversion(EmitScalarExpr(E), E->getType(), BoolTy);
Reid Spencer5f016e22007-07-11 17:01:13 +000086
Chris Lattner9069fa22007-08-26 16:46:58 +000087 return EmitComplexToScalarConversion(EmitComplexExpr(E), E->getType(),BoolTy);
Reid Spencer5f016e22007-07-11 17:01:13 +000088}
89
Chris Lattner9b655512007-08-31 22:49:20 +000090/// EmitAnyExpr - Emit code to compute the specified expression which can have
91/// any type. The result is returned as an RValue struct. If this is an
Mike Stumpdb52dcd2009-09-09 13:00:44 +000092/// aggregate expression, the aggloc/agglocvolatile arguments indicate where the
93/// result should be returned.
94RValue CodeGenFunction::EmitAnyExpr(const Expr *E, llvm::Value *AggLoc,
Anders Carlsson14c5cbf2009-08-16 07:36:22 +000095 bool IsAggLocVolatile, bool IgnoreResult,
96 bool IsInitializer) {
Chris Lattner9b655512007-08-31 22:49:20 +000097 if (!hasAggregateLLVMType(E->getType()))
Mike Stump7f79f9b2009-05-29 15:46:01 +000098 return RValue::get(EmitScalarExpr(E, IgnoreResult));
Chris Lattner9b2dc282008-04-04 16:54:41 +000099 else if (E->getType()->isAnyComplexType())
Mike Stump7f79f9b2009-05-29 15:46:01 +0000100 return RValue::getComplex(EmitComplexExpr(E, false, false,
101 IgnoreResult, IgnoreResult));
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000102
Anders Carlsson14c5cbf2009-08-16 07:36:22 +0000103 EmitAggExpr(E, AggLoc, IsAggLocVolatile, IgnoreResult, IsInitializer);
104 return RValue::getAggregate(AggLoc, IsAggLocVolatile);
Chris Lattner9b655512007-08-31 22:49:20 +0000105}
106
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000107/// EmitAnyExprToTemp - Similary to EmitAnyExpr(), however, the result will
108/// always be accessible even if no aggregate location is provided.
109RValue CodeGenFunction::EmitAnyExprToTemp(const Expr *E,
Anders Carlsson14c5cbf2009-08-16 07:36:22 +0000110 bool IsAggLocVolatile,
111 bool IsInitializer) {
112 llvm::Value *AggLoc = 0;
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000113
114 if (hasAggregateLLVMType(E->getType()) &&
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000115 !E->getType()->isAnyComplexType())
John McCall63efd332010-02-15 01:23:36 +0000116 AggLoc = CreateMemTemp(E->getType(), "agg.tmp");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000117 return EmitAnyExpr(E, AggLoc, IsAggLocVolatile, /*IgnoreResult=*/false,
Anders Carlsson14c5cbf2009-08-16 07:36:22 +0000118 IsInitializer);
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000119}
120
John McCall3d3ec1c2010-04-21 10:05:39 +0000121/// EmitAnyExprToMem - Evaluate an expression into a given memory
122/// location.
123void CodeGenFunction::EmitAnyExprToMem(const Expr *E,
124 llvm::Value *Location,
125 bool IsLocationVolatile,
126 bool IsInit) {
127 if (E->getType()->isComplexType())
128 EmitComplexExprIntoAddr(E, Location, IsLocationVolatile);
129 else if (hasAggregateLLVMType(E->getType()))
130 EmitAggExpr(E, Location, IsLocationVolatile, /*Ignore*/ false, IsInit);
131 else {
132 RValue RV = RValue::get(EmitScalarExpr(E, /*Ignore*/ false));
133 LValue LV = LValue::MakeAddr(Location, MakeQualifiers(E->getType()));
134 EmitStoreThroughLValue(RV, LV, E->getType());
135 }
136}
137
Anders Carlsson4029ca72009-05-20 00:24:07 +0000138RValue CodeGenFunction::EmitReferenceBindingToExpr(const Expr* E,
Anders Carlsson14c5cbf2009-08-16 07:36:22 +0000139 bool IsInitializer) {
Anders Carlssone1b7ea12009-10-18 23:09:21 +0000140 bool ShouldDestroyTemporaries = false;
141 unsigned OldNumLiveTemporaries = 0;
Eli Friedman27a9b722009-12-19 00:20:10 +0000142
143 if (const CXXDefaultArgExpr *DAE = dyn_cast<CXXDefaultArgExpr>(E))
144 E = DAE->getExpr();
145
Anders Carlssonb3f74422009-10-15 00:51:46 +0000146 if (const CXXExprWithTemporaries *TE = dyn_cast<CXXExprWithTemporaries>(E)) {
Anders Carlsson0ece4912009-12-15 20:51:39 +0000147 ShouldDestroyTemporaries = true;
148
Chris Lattnereb99b012009-10-28 17:39:19 +0000149 // Keep track of the current cleanup stack depth.
Anders Carlsson0ece4912009-12-15 20:51:39 +0000150 OldNumLiveTemporaries = LiveTemporaries.size();
Anders Carlssonb3f74422009-10-15 00:51:46 +0000151
Anders Carlssone1b7ea12009-10-18 23:09:21 +0000152 E = TE->getSubExpr();
Anders Carlssonb3f74422009-10-15 00:51:46 +0000153 }
154
Eli Friedman5df0d422009-05-20 02:31:19 +0000155 RValue Val;
156 if (E->isLvalue(getContext()) == Expr::LV_Valid) {
Anders Carlsson4bbab922009-05-20 00:36:58 +0000157 // Emit the expr as an lvalue.
158 LValue LV = EmitLValue(E);
Anders Carlsson0dc73662010-02-04 17:32:58 +0000159 if (LV.isSimple()) {
160 if (ShouldDestroyTemporaries) {
161 // Pop temporaries.
162 while (LiveTemporaries.size() > OldNumLiveTemporaries)
163 PopCXXTemporary();
164 }
165
Eli Friedman5df0d422009-05-20 02:31:19 +0000166 return RValue::get(LV.getAddress());
Anders Carlsson0dc73662010-02-04 17:32:58 +0000167 }
168
Eli Friedman5df0d422009-05-20 02:31:19 +0000169 Val = EmitLoadOfLValue(LV, E->getType());
Anders Carlssone1b7ea12009-10-18 23:09:21 +0000170
171 if (ShouldDestroyTemporaries) {
172 // Pop temporaries.
173 while (LiveTemporaries.size() > OldNumLiveTemporaries)
174 PopCXXTemporary();
175 }
Eli Friedman5df0d422009-05-20 02:31:19 +0000176 } else {
Anders Carlssonfc89c312010-04-24 21:12:55 +0000177 const CXXBaseSpecifierArray *BasePath = 0;
Anders Carlssonb3f74422009-10-15 00:51:46 +0000178 const CXXRecordDecl *DerivedClassDecl = 0;
179
180 if (const CastExpr *CE =
181 dyn_cast<CastExpr>(E->IgnoreParenNoopCasts(getContext()))) {
182 if (CE->getCastKind() == CastExpr::CK_DerivedToBase) {
183 E = CE->getSubExpr();
Anders Carlssonfc89c312010-04-24 21:12:55 +0000184
185 BasePath = &CE->getBasePath();
Anders Carlssonb3f74422009-10-15 00:51:46 +0000186 DerivedClassDecl =
187 cast<CXXRecordDecl>(E->getType()->getAs<RecordType>()->getDecl());
188 }
189 }
190
Anders Carlsson14c5cbf2009-08-16 07:36:22 +0000191 Val = EmitAnyExprToTemp(E, /*IsAggLocVolatile=*/false,
192 IsInitializer);
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000193
Anders Carlssone1b7ea12009-10-18 23:09:21 +0000194 if (ShouldDestroyTemporaries) {
195 // Pop temporaries.
196 while (LiveTemporaries.size() > OldNumLiveTemporaries)
197 PopCXXTemporary();
198 }
199
Anders Carlsson2da76932009-08-16 17:54:29 +0000200 if (IsInitializer) {
201 // We might have to destroy the temporary variable.
202 if (const RecordType *RT = E->getType()->getAs<RecordType>()) {
203 if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
204 if (!ClassDecl->hasTrivialDestructor()) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000205 const CXXDestructorDecl *Dtor =
Anders Carlsson2da76932009-08-16 17:54:29 +0000206 ClassDecl->getDestructor(getContext());
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000207
Mike Stumpd88ea562009-12-09 03:35:49 +0000208 {
Anders Carlsson6ec687d2009-12-11 01:00:09 +0000209 DelayedCleanupBlock Scope(*this);
Mike Stumpd88ea562009-12-09 03:35:49 +0000210 EmitCXXDestructorCall(Dtor, Dtor_Complete,
Anders Carlsson8e6404c2010-05-02 23:29:11 +0000211 /*ForVirtualBase=*/false,
Mike Stumpd88ea562009-12-09 03:35:49 +0000212 Val.getAggregateAddr());
Anders Carlsson6ec687d2009-12-11 01:00:09 +0000213
214 // Make sure to jump to the exit block.
215 EmitBranch(Scope.getCleanupExitBlock());
Mike Stumpd88ea562009-12-09 03:35:49 +0000216 }
217 if (Exceptions) {
218 EHCleanupBlock Cleanup(*this);
219 EmitCXXDestructorCall(Dtor, Dtor_Complete,
Anders Carlsson8e6404c2010-05-02 23:29:11 +0000220 /*ForVirtualBase=*/false,
Mike Stumpd88ea562009-12-09 03:35:49 +0000221 Val.getAggregateAddr());
222 }
Anders Carlsson2da76932009-08-16 17:54:29 +0000223 }
Anders Carlsson8478ce62009-08-16 17:50:25 +0000224 }
225 }
226 }
Anders Carlssonb3f74422009-10-15 00:51:46 +0000227
228 // Check if need to perform the derived-to-base cast.
Anders Carlssonfc89c312010-04-24 21:12:55 +0000229 if (BasePath) {
Anders Carlssonb3f74422009-10-15 00:51:46 +0000230 llvm::Value *Derived = Val.getAggregateAddr();
Anders Carlssonb3f74422009-10-15 00:51:46 +0000231 llvm::Value *Base =
Anders Carlssonfc89c312010-04-24 21:12:55 +0000232 GetAddressOfBaseClass(Derived, DerivedClassDecl, *BasePath,
Anders Carlssona3697c92009-11-23 17:57:54 +0000233 /*NullCheckValue=*/false);
Anders Carlssonb3f74422009-10-15 00:51:46 +0000234 return RValue::get(Base);
235 }
Anders Carlsson4bbab922009-05-20 00:36:58 +0000236 }
Eli Friedman5df0d422009-05-20 02:31:19 +0000237
238 if (Val.isAggregate()) {
239 Val = RValue::get(Val.getAggregateAddr());
240 } else {
Anders Carlsson7cd3a642009-05-20 01:35:03 +0000241 // Create a temporary variable that we can bind the reference to.
Daniel Dunbar195337d2010-02-09 02:48:28 +0000242 llvm::Value *Temp = CreateMemTemp(E->getType(), "reftmp");
Eli Friedman5df0d422009-05-20 02:31:19 +0000243 if (Val.isScalar())
244 EmitStoreOfScalar(Val.getScalarVal(), Temp, false, E->getType());
245 else
246 StoreComplexToAddr(Val.getComplexVal(), Temp, false);
247 Val = RValue::get(Temp);
Anders Carlssone04d1c72009-05-20 01:03:17 +0000248 }
Eli Friedman5df0d422009-05-20 02:31:19 +0000249
250 return Val;
Anders Carlsson4029ca72009-05-20 00:24:07 +0000251}
252
253
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000254/// getAccessedFieldNo - Given an encoded value and a result number, return the
255/// input field number being accessed.
256unsigned CodeGenFunction::getAccessedFieldNo(unsigned Idx,
Dan Gohman4f8d1232008-05-22 00:50:06 +0000257 const llvm::Constant *Elts) {
258 if (isa<llvm::ConstantAggregateZero>(Elts))
259 return 0;
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000260
Dan Gohman4f8d1232008-05-22 00:50:06 +0000261 return cast<llvm::ConstantInt>(Elts->getOperand(Idx))->getZExtValue();
262}
263
Mike Stumpb14e62d2009-12-16 02:57:00 +0000264void CodeGenFunction::EmitCheck(llvm::Value *Address, unsigned Size) {
265 if (!CatchUndefined)
266 return;
267
Chris Lattnerc24b9c42010-04-10 18:34:14 +0000268 const llvm::Type *Size_tTy
Mike Stumpb14e62d2009-12-16 02:57:00 +0000269 = llvm::IntegerType::get(VMContext, LLVMPointerWidth);
270 Address = Builder.CreateBitCast(Address, PtrToInt8Ty);
271
Chris Lattnerc24b9c42010-04-10 18:34:14 +0000272 llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::objectsize, &Size_tTy, 1);
273 const llvm::IntegerType *Int1Ty = llvm::IntegerType::get(VMContext, 1);
274
Mike Stumpb14e62d2009-12-16 02:57:00 +0000275 // In time, people may want to control this and use a 1 here.
Chris Lattnerc24b9c42010-04-10 18:34:14 +0000276 llvm::Value *Arg = llvm::ConstantInt::get(Int1Ty, 0);
Mike Stumpb14e62d2009-12-16 02:57:00 +0000277 llvm::Value *C = Builder.CreateCall2(F, Address, Arg);
278 llvm::BasicBlock *Cont = createBasicBlock();
279 llvm::BasicBlock *Check = createBasicBlock();
280 llvm::Value *NegativeOne = llvm::ConstantInt::get(Size_tTy, -1ULL);
281 Builder.CreateCondBr(Builder.CreateICmpEQ(C, NegativeOne), Cont, Check);
282
283 EmitBlock(Check);
284 Builder.CreateCondBr(Builder.CreateICmpUGE(C,
285 llvm::ConstantInt::get(Size_tTy, Size)),
286 Cont, getTrapBB());
287 EmitBlock(Cont);
288}
Chris Lattner9b655512007-08-31 22:49:20 +0000289
Chris Lattnerdd36d322010-01-09 21:40:03 +0000290
291llvm::Value *CodeGenFunction::
292EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV,
293 bool isInc, bool isPre) {
294 QualType ValTy = E->getSubExpr()->getType();
295 llvm::Value *InVal = EmitLoadOfLValue(LV, ValTy).getScalarVal();
296
297 int AmountVal = isInc ? 1 : -1;
298
299 if (ValTy->isPointerType() &&
300 ValTy->getAs<PointerType>()->isVariableArrayType()) {
301 // The amount of the addition/subtraction needs to account for the VLA size
302 ErrorUnsupported(E, "VLA pointer inc/dec");
303 }
304
305 llvm::Value *NextVal;
306 if (const llvm::PointerType *PT =
307 dyn_cast<llvm::PointerType>(InVal->getType())) {
308 llvm::Constant *Inc =
309 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), AmountVal);
310 if (!isa<llvm::FunctionType>(PT->getElementType())) {
311 QualType PTEE = ValTy->getPointeeType();
312 if (const ObjCInterfaceType *OIT =
313 dyn_cast<ObjCInterfaceType>(PTEE)) {
314 // Handle interface types, which are not represented with a concrete
315 // type.
316 int size = getContext().getTypeSize(OIT) / 8;
317 if (!isInc)
318 size = -size;
319 Inc = llvm::ConstantInt::get(Inc->getType(), size);
320 const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(VMContext);
321 InVal = Builder.CreateBitCast(InVal, i8Ty);
322 NextVal = Builder.CreateGEP(InVal, Inc, "add.ptr");
323 llvm::Value *lhs = LV.getAddress();
324 lhs = Builder.CreateBitCast(lhs, llvm::PointerType::getUnqual(i8Ty));
325 LV = LValue::MakeAddr(lhs, MakeQualifiers(ValTy));
326 } else
327 NextVal = Builder.CreateInBoundsGEP(InVal, Inc, "ptrincdec");
328 } else {
329 const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(VMContext);
330 NextVal = Builder.CreateBitCast(InVal, i8Ty, "tmp");
331 NextVal = Builder.CreateGEP(NextVal, Inc, "ptrincdec");
332 NextVal = Builder.CreateBitCast(NextVal, InVal->getType());
333 }
334 } else if (InVal->getType() == llvm::Type::getInt1Ty(VMContext) && isInc) {
335 // Bool++ is an interesting case, due to promotion rules, we get:
336 // Bool++ -> Bool = Bool+1 -> Bool = (int)Bool+1 ->
337 // Bool = ((int)Bool+1) != 0
338 // An interesting aspect of this is that increment is always true.
339 // Decrement does not have this property.
340 NextVal = llvm::ConstantInt::getTrue(VMContext);
341 } else if (isa<llvm::IntegerType>(InVal->getType())) {
342 NextVal = llvm::ConstantInt::get(InVal->getType(), AmountVal);
343
344 // Signed integer overflow is undefined behavior.
345 if (ValTy->isSignedIntegerType())
346 NextVal = Builder.CreateNSWAdd(InVal, NextVal, isInc ? "inc" : "dec");
347 else
348 NextVal = Builder.CreateAdd(InVal, NextVal, isInc ? "inc" : "dec");
349 } else {
350 // Add the inc/dec to the real part.
351 if (InVal->getType()->isFloatTy())
352 NextVal =
353 llvm::ConstantFP::get(VMContext,
354 llvm::APFloat(static_cast<float>(AmountVal)));
355 else if (InVal->getType()->isDoubleTy())
356 NextVal =
357 llvm::ConstantFP::get(VMContext,
358 llvm::APFloat(static_cast<double>(AmountVal)));
359 else {
360 llvm::APFloat F(static_cast<float>(AmountVal));
361 bool ignored;
362 F.convert(Target.getLongDoubleFormat(), llvm::APFloat::rmTowardZero,
363 &ignored);
364 NextVal = llvm::ConstantFP::get(VMContext, F);
365 }
366 NextVal = Builder.CreateFAdd(InVal, NextVal, isInc ? "inc" : "dec");
367 }
368
369 // Store the updated result through the lvalue.
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000370 if (LV.isBitField())
Chris Lattnerdd36d322010-01-09 21:40:03 +0000371 EmitStoreThroughBitfieldLValue(RValue::get(NextVal), LV, ValTy, &NextVal);
372 else
373 EmitStoreThroughLValue(RValue::get(NextVal), LV, ValTy);
374
375 // If this is a postinc, return the value read from memory, otherwise use the
376 // updated value.
377 return isPre ? NextVal : InVal;
378}
379
380
381CodeGenFunction::ComplexPairTy CodeGenFunction::
382EmitComplexPrePostIncDec(const UnaryOperator *E, LValue LV,
383 bool isInc, bool isPre) {
384 ComplexPairTy InVal = LoadComplexFromAddr(LV.getAddress(),
385 LV.isVolatileQualified());
386
387 llvm::Value *NextVal;
388 if (isa<llvm::IntegerType>(InVal.first->getType())) {
389 uint64_t AmountVal = isInc ? 1 : -1;
390 NextVal = llvm::ConstantInt::get(InVal.first->getType(), AmountVal, true);
391
392 // Add the inc/dec to the real part.
393 NextVal = Builder.CreateAdd(InVal.first, NextVal, isInc ? "inc" : "dec");
394 } else {
395 QualType ElemTy = E->getType()->getAs<ComplexType>()->getElementType();
396 llvm::APFloat FVal(getContext().getFloatTypeSemantics(ElemTy), 1);
397 if (!isInc)
398 FVal.changeSign();
399 NextVal = llvm::ConstantFP::get(getLLVMContext(), FVal);
400
401 // Add the inc/dec to the real part.
402 NextVal = Builder.CreateFAdd(InVal.first, NextVal, isInc ? "inc" : "dec");
403 }
404
405 ComplexPairTy IncVal(NextVal, InVal.second);
406
407 // Store the updated result through the lvalue.
408 StoreComplexToAddr(IncVal, LV.getAddress(), LV.isVolatileQualified());
409
410 // If this is a postinc, return the value read from memory, otherwise use the
411 // updated value.
412 return isPre ? IncVal : InVal;
413}
414
415
Reid Spencer5f016e22007-07-11 17:01:13 +0000416//===----------------------------------------------------------------------===//
417// LValue Expression Emission
418//===----------------------------------------------------------------------===//
419
Daniel Dunbar13e81732009-02-05 07:09:07 +0000420RValue CodeGenFunction::GetUndefRValue(QualType Ty) {
Chris Lattnereb99b012009-10-28 17:39:19 +0000421 if (Ty->isVoidType())
Daniel Dunbar13e81732009-02-05 07:09:07 +0000422 return RValue::get(0);
Chris Lattnereb99b012009-10-28 17:39:19 +0000423
424 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
Daniel Dunbar8fa73ed2009-01-09 20:09:28 +0000425 const llvm::Type *EltTy = ConvertType(CTy->getElementType());
Owen Anderson03e20502009-07-30 23:11:26 +0000426 llvm::Value *U = llvm::UndefValue::get(EltTy);
Daniel Dunbar8fa73ed2009-01-09 20:09:28 +0000427 return RValue::getComplex(std::make_pair(U, U));
Chris Lattnereb99b012009-10-28 17:39:19 +0000428 }
429
430 if (hasAggregateLLVMType(Ty)) {
Owen Anderson96e0fc72009-07-29 22:16:19 +0000431 const llvm::Type *LTy = llvm::PointerType::getUnqual(ConvertType(Ty));
Owen Anderson03e20502009-07-30 23:11:26 +0000432 return RValue::getAggregate(llvm::UndefValue::get(LTy));
Daniel Dunbar8fa73ed2009-01-09 20:09:28 +0000433 }
Chris Lattnereb99b012009-10-28 17:39:19 +0000434
435 return RValue::get(llvm::UndefValue::get(ConvertType(Ty)));
Daniel Dunbarce1d38b2009-01-09 16:50:52 +0000436}
437
Daniel Dunbar13e81732009-02-05 07:09:07 +0000438RValue CodeGenFunction::EmitUnsupportedRValue(const Expr *E,
439 const char *Name) {
440 ErrorUnsupported(E, Name);
441 return GetUndefRValue(E->getType());
442}
443
Daniel Dunbar6ba82a42008-08-25 20:45:57 +0000444LValue CodeGenFunction::EmitUnsupportedLValue(const Expr *E,
445 const char *Name) {
446 ErrorUnsupported(E, Name);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000447 llvm::Type *Ty = llvm::PointerType::getUnqual(ConvertType(E->getType()));
Owen Anderson03e20502009-07-30 23:11:26 +0000448 return LValue::MakeAddr(llvm::UndefValue::get(Ty),
John McCall0953e762009-09-24 19:53:00 +0000449 MakeQualifiers(E->getType()));
Daniel Dunbar6ba82a42008-08-25 20:45:57 +0000450}
451
Mike Stumpb14e62d2009-12-16 02:57:00 +0000452LValue CodeGenFunction::EmitCheckedLValue(const Expr *E) {
453 LValue LV = EmitLValue(E);
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000454 if (!isa<DeclRefExpr>(E) && !LV.isBitField() && LV.isSimple())
Mike Stumpb14e62d2009-12-16 02:57:00 +0000455 EmitCheck(LV.getAddress(), getContext().getTypeSize(E->getType()) / 8);
456 return LV;
457}
458
Reid Spencer5f016e22007-07-11 17:01:13 +0000459/// EmitLValue - Emit code to compute a designator that specifies the location
460/// of the expression.
461///
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000462/// This can return one of two things: a simple address or a bitfield reference.
463/// In either case, the LLVM Value* in the LValue structure is guaranteed to be
464/// an LLVM pointer type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000465///
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000466/// If this returns a bitfield reference, nothing about the pointee type of the
467/// LLVM value is known: For example, it may not be a pointer to an integer.
Reid Spencer5f016e22007-07-11 17:01:13 +0000468///
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000469/// If this returns a normal address, and if the lvalue's C type is fixed size,
470/// this method guarantees that the returned pointer type will point to an LLVM
471/// type of the same size of the lvalue's type. If the lvalue has a variable
472/// length type, this is not possible.
Reid Spencer5f016e22007-07-11 17:01:13 +0000473///
474LValue CodeGenFunction::EmitLValue(const Expr *E) {
475 switch (E->getStmtClass()) {
Daniel Dunbar6ba82a42008-08-25 20:45:57 +0000476 default: return EmitUnsupportedLValue(E, "l-value expression");
Reid Spencer5f016e22007-07-11 17:01:13 +0000477
Fariborz Jahanian820bca42009-12-09 23:35:29 +0000478 case Expr::ObjCIsaExprClass:
479 return EmitObjCIsaExpr(cast<ObjCIsaExpr>(E));
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000480 case Expr::BinaryOperatorClass:
Daniel Dunbar80e62c22008-09-04 03:20:13 +0000481 return EmitBinaryOperatorLValue(cast<BinaryOperator>(E));
Douglas Gregor6a03e342010-04-23 04:16:32 +0000482 case Expr::CompoundAssignOperatorClass:
483 return EmitCompoundAssignOperatorLValue(cast<CompoundAssignOperator>(E));
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000484 case Expr::CallExprClass:
Anders Carlssonfaf86642009-09-01 21:18:52 +0000485 case Expr::CXXMemberCallExprClass:
Douglas Gregorb4609802008-11-14 16:09:21 +0000486 case Expr::CXXOperatorCallExprClass:
487 return EmitCallExprLValue(cast<CallExpr>(E));
Daniel Dunbar5b5c9ef2009-02-11 20:59:32 +0000488 case Expr::VAArgExprClass:
489 return EmitVAArgExprLValue(cast<VAArgExpr>(E));
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000490 case Expr::DeclRefExprClass:
Douglas Gregor1a49af92009-01-06 05:10:23 +0000491 return EmitDeclRefLValue(cast<DeclRefExpr>(E));
Reid Spencer5f016e22007-07-11 17:01:13 +0000492 case Expr::ParenExprClass:return EmitLValue(cast<ParenExpr>(E)->getSubExpr());
Chris Lattnerd9f69102008-08-10 01:53:14 +0000493 case Expr::PredefinedExprClass:
494 return EmitPredefinedLValue(cast<PredefinedExpr>(E));
Reid Spencer5f016e22007-07-11 17:01:13 +0000495 case Expr::StringLiteralClass:
496 return EmitStringLiteralLValue(cast<StringLiteral>(E));
Chris Lattnereaf2bb82009-02-24 22:18:39 +0000497 case Expr::ObjCEncodeExprClass:
498 return EmitObjCEncodeExprLValue(cast<ObjCEncodeExpr>(E));
Chris Lattner391d77a2008-03-30 23:03:07 +0000499
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000500 case Expr::BlockDeclRefExprClass:
Mike Stumpa99038c2009-02-28 09:07:16 +0000501 return EmitBlockDeclRefLValue(cast<BlockDeclRefExpr>(E));
502
Anders Carlssonb58d0172009-05-30 23:23:33 +0000503 case Expr::CXXTemporaryObjectExprClass:
504 case Expr::CXXConstructExprClass:
Anders Carlssone61c9e82009-05-30 23:30:54 +0000505 return EmitCXXConstructLValue(cast<CXXConstructExpr>(E));
506 case Expr::CXXBindTemporaryExprClass:
507 return EmitCXXBindTemporaryLValue(cast<CXXBindTemporaryExpr>(E));
Anders Carlssonb9ea0b52009-09-14 01:10:45 +0000508 case Expr::CXXExprWithTemporariesClass:
509 return EmitCXXExprWithTemporariesLValue(cast<CXXExprWithTemporaries>(E));
Anders Carlsson370e5382009-11-14 01:51:50 +0000510 case Expr::CXXZeroInitValueExprClass:
511 return EmitNullInitializationLValue(cast<CXXZeroInitValueExpr>(E));
512 case Expr::CXXDefaultArgExprClass:
513 return EmitLValue(cast<CXXDefaultArgExpr>(E)->getExpr());
Mike Stumpc2e84ae2009-11-15 08:09:41 +0000514 case Expr::CXXTypeidExprClass:
515 return EmitCXXTypeidLValue(cast<CXXTypeidExpr>(E));
Anders Carlssone61c9e82009-05-30 23:30:54 +0000516
Daniel Dunbar0a04d772008-08-23 10:51:21 +0000517 case Expr::ObjCMessageExprClass:
518 return EmitObjCMessageExprLValue(cast<ObjCMessageExpr>(E));
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000519 case Expr::ObjCIvarRefExprClass:
Chris Lattner391d77a2008-03-30 23:03:07 +0000520 return EmitObjCIvarRefLValue(cast<ObjCIvarRefExpr>(E));
Daniel Dunbar6ba82a42008-08-25 20:45:57 +0000521 case Expr::ObjCPropertyRefExprClass:
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000522 return EmitObjCPropertyRefLValue(cast<ObjCPropertyRefExpr>(E));
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000523 case Expr::ObjCImplicitSetterGetterRefExprClass:
524 return EmitObjCKVCRefLValue(cast<ObjCImplicitSetterGetterRefExpr>(E));
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000525 case Expr::ObjCSuperExprClass:
Chris Lattner65459942009-04-25 19:35:26 +0000526 return EmitObjCSuperExprLValue(cast<ObjCSuperExpr>(E));
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000527
Chris Lattner65459942009-04-25 19:35:26 +0000528 case Expr::StmtExprClass:
529 return EmitStmtExprLValue(cast<StmtExpr>(E));
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000530 case Expr::UnaryOperatorClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000531 return EmitUnaryOpLValue(cast<UnaryOperator>(E));
532 case Expr::ArraySubscriptExprClass:
533 return EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E));
Nate Begeman213541a2008-04-18 23:10:10 +0000534 case Expr::ExtVectorElementExprClass:
535 return EmitExtVectorElementExpr(cast<ExtVectorElementExpr>(E));
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000536 case Expr::MemberExprClass:
Douglas Gregorbd4c4ae2009-08-26 22:36:53 +0000537 return EmitMemberExpr(cast<MemberExpr>(E));
Eli Friedman06e863f2008-05-13 23:18:27 +0000538 case Expr::CompoundLiteralExprClass:
539 return EmitCompoundLiteralLValue(cast<CompoundLiteralExpr>(E));
Daniel Dunbar90345582009-03-24 02:38:23 +0000540 case Expr::ConditionalOperatorClass:
Anders Carlsson6fcec8b2009-09-15 16:35:24 +0000541 return EmitConditionalOperatorLValue(cast<ConditionalOperator>(E));
Chris Lattner670a62c2008-12-12 05:35:08 +0000542 case Expr::ChooseExprClass:
Eli Friedman79769322009-03-04 05:52:32 +0000543 return EmitLValue(cast<ChooseExpr>(E)->getChosenSubExpr(getContext()));
Chris Lattnerc3953a62009-03-18 04:02:57 +0000544 case Expr::ImplicitCastExprClass:
545 case Expr::CStyleCastExprClass:
546 case Expr::CXXFunctionalCastExprClass:
547 case Expr::CXXStaticCastExprClass:
548 case Expr::CXXDynamicCastExprClass:
549 case Expr::CXXReinterpretCastExprClass:
550 case Expr::CXXConstCastExprClass:
Chris Lattner75dfeda2009-03-18 18:28:57 +0000551 return EmitCastLValue(cast<CastExpr>(E));
Reid Spencer5f016e22007-07-11 17:01:13 +0000552 }
553}
554
Daniel Dunbar9d9cc872009-02-10 00:57:50 +0000555llvm::Value *CodeGenFunction::EmitLoadOfScalar(llvm::Value *Addr, bool Volatile,
556 QualType Ty) {
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000557 llvm::LoadInst *Load = Builder.CreateLoad(Addr, "tmp");
558 if (Volatile)
559 Load->setVolatile(true);
Daniel Dunbar9d9cc872009-02-10 00:57:50 +0000560
Anders Carlsson3bb423b2009-05-19 19:36:19 +0000561 // Bool can have different representation in memory than in registers.
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000562 llvm::Value *V = Load;
Daniel Dunbar9d9cc872009-02-10 00:57:50 +0000563 if (Ty->isBooleanType())
Owen Anderson0032b272009-08-13 21:57:51 +0000564 if (V->getType() != llvm::Type::getInt1Ty(VMContext))
565 V = Builder.CreateTrunc(V, llvm::Type::getInt1Ty(VMContext), "tobool");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000566
Daniel Dunbar9d9cc872009-02-10 00:57:50 +0000567 return V;
568}
569
570void CodeGenFunction::EmitStoreOfScalar(llvm::Value *Value, llvm::Value *Addr,
Anders Carlssonb4aa4662009-05-19 18:50:41 +0000571 bool Volatile, QualType Ty) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000572
Anders Carlsson3bb423b2009-05-19 19:36:19 +0000573 if (Ty->isBooleanType()) {
574 // Bool can have different representation in memory than in registers.
Anders Carlsson3bb423b2009-05-19 19:36:19 +0000575 const llvm::PointerType *DstPtr = cast<llvm::PointerType>(Addr->getType());
Eli Friedman2b06d342009-12-01 22:31:51 +0000576 Value = Builder.CreateIntCast(Value, DstPtr->getElementType(), false);
Daniel Dunbar9d9cc872009-02-10 00:57:50 +0000577 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000578 Builder.CreateStore(Value, Addr, Volatile);
Daniel Dunbar9d9cc872009-02-10 00:57:50 +0000579}
580
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000581/// EmitLoadOfLValue - Given an expression that represents a value lvalue, this
582/// method emits the address of the lvalue, then loads the result as an rvalue,
583/// returning the rvalue.
Reid Spencer5f016e22007-07-11 17:01:13 +0000584RValue CodeGenFunction::EmitLoadOfLValue(LValue LV, QualType ExprType) {
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +0000585 if (LV.isObjCWeak()) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000586 // load of a __weak object.
Fariborz Jahanian6dc23172008-11-18 21:45:40 +0000587 llvm::Value *AddrWeakObj = LV.getAddress();
Chris Lattnereb99b012009-10-28 17:39:19 +0000588 return RValue::get(CGM.getObjCRuntime().EmitObjCWeakRead(*this,
589 AddrWeakObj));
Fariborz Jahanian6dc23172008-11-18 21:45:40 +0000590 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000591
Reid Spencer5f016e22007-07-11 17:01:13 +0000592 if (LV.isSimple()) {
593 llvm::Value *Ptr = LV.getAddress();
Douglas Gregora5002102010-02-05 21:10:36 +0000594 const llvm::Type *EltTy =
595 cast<llvm::PointerType>(Ptr->getType())->getElementType();
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000596
Reid Spencer5f016e22007-07-11 17:01:13 +0000597 // Simple scalar l-value.
Daniel Dunbare86bcf02010-02-08 22:53:07 +0000598 //
599 // FIXME: We shouldn't have to use isSingleValueType here.
Douglas Gregora5002102010-02-05 21:10:36 +0000600 if (EltTy->isSingleValueType())
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000601 return RValue::get(EmitLoadOfScalar(Ptr, LV.isVolatileQualified(),
Daniel Dunbar9d9cc872009-02-10 00:57:50 +0000602 ExprType));
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000603
Chris Lattner883f6a72007-08-11 00:04:45 +0000604 assert(ExprType->isFunctionType() && "Unknown scalar value");
605 return RValue::get(Ptr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000606 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000607
Reid Spencer5f016e22007-07-11 17:01:13 +0000608 if (LV.isVectorElt()) {
Eli Friedman1e692ac2008-06-13 23:01:12 +0000609 llvm::Value *Vec = Builder.CreateLoad(LV.getVectorAddr(),
610 LV.isVolatileQualified(), "tmp");
Reid Spencer5f016e22007-07-11 17:01:13 +0000611 return RValue::get(Builder.CreateExtractElement(Vec, LV.getVectorIdx(),
612 "vecext"));
613 }
Chris Lattner46ea8eb2007-08-03 00:16:29 +0000614
615 // If this is a reference to a subset of the elements of a vector, either
616 // shuffle the input or extract/insert them as appropriate.
Nate Begeman213541a2008-04-18 23:10:10 +0000617 if (LV.isExtVectorElt())
618 return EmitLoadOfExtVectorElementLValue(LV, ExprType);
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000619
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000620 if (LV.isBitField())
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000621 return EmitLoadOfBitfieldLValue(LV, ExprType);
622
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000623 if (LV.isPropertyRef())
624 return EmitLoadOfPropertyRefLValue(LV, ExprType);
625
Chris Lattner73525de2009-02-16 21:11:58 +0000626 assert(LV.isKVCRef() && "Unknown LValue type!");
627 return EmitLoadOfKVCRefLValue(LV, ExprType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000628}
629
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000630RValue CodeGenFunction::EmitLoadOfBitfieldLValue(LValue LV,
631 QualType ExprType) {
Daniel Dunbarefbf4872010-04-06 01:07:44 +0000632 const CGBitFieldInfo &Info = LV.getBitFieldInfo();
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000633
Daniel Dunbarecdb41e2010-04-13 23:34:15 +0000634 // Get the output type.
635 const llvm::Type *ResLTy = ConvertType(ExprType);
636 unsigned ResSizeInBits = CGM.getTargetData().getTypeSizeInBits(ResLTy);
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000637
Daniel Dunbarecdb41e2010-04-13 23:34:15 +0000638 // Compute the result as an OR of all of the individual component accesses.
639 llvm::Value *Res = 0;
640 for (unsigned i = 0, e = Info.getNumComponents(); i != e; ++i) {
641 const CGBitFieldInfo::AccessInfo &AI = Info.getComponent(i);
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000642
Daniel Dunbarecdb41e2010-04-13 23:34:15 +0000643 // Get the field pointer.
644 llvm::Value *Ptr = LV.getBitFieldBaseAddr();
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000645
Daniel Dunbarecdb41e2010-04-13 23:34:15 +0000646 // Only offset by the field index if used, so that incoming values are not
647 // required to be structures.
648 if (AI.FieldIndex)
649 Ptr = Builder.CreateStructGEP(Ptr, AI.FieldIndex, "bf.field");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000650
Daniel Dunbarecdb41e2010-04-13 23:34:15 +0000651 // Offset by the byte offset, if used.
652 if (AI.FieldByteOffset) {
653 const llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(VMContext);
654 Ptr = Builder.CreateBitCast(Ptr, i8PTy);
655 Ptr = Builder.CreateConstGEP1_32(Ptr, AI.FieldByteOffset,"bf.field.offs");
656 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000657
Daniel Dunbarecdb41e2010-04-13 23:34:15 +0000658 // Cast to the access type.
659 const llvm::Type *PTy = llvm::Type::getIntNPtrTy(VMContext, AI.AccessWidth,
660 ExprType.getAddressSpace());
661 Ptr = Builder.CreateBitCast(Ptr, PTy);
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000662
Daniel Dunbarecdb41e2010-04-13 23:34:15 +0000663 // Perform the load.
664 llvm::LoadInst *Load = Builder.CreateLoad(Ptr, LV.isVolatileQualified());
665 if (AI.AccessAlignment)
666 Load->setAlignment(AI.AccessAlignment);
667
668 // Shift out unused low bits and mask out unused high bits.
669 llvm::Value *Val = Load;
670 if (AI.FieldBitStart)
Daniel Dunbar26772612010-04-15 03:47:33 +0000671 Val = Builder.CreateLShr(Load, AI.FieldBitStart);
Daniel Dunbarecdb41e2010-04-13 23:34:15 +0000672 Val = Builder.CreateAnd(Val, llvm::APInt::getLowBitsSet(AI.AccessWidth,
673 AI.TargetBitWidth),
674 "bf.clear");
675
676 // Extend or truncate to the target size.
677 if (AI.AccessWidth < ResSizeInBits)
678 Val = Builder.CreateZExt(Val, ResLTy);
679 else if (AI.AccessWidth > ResSizeInBits)
680 Val = Builder.CreateTrunc(Val, ResLTy);
681
682 // Shift into place, and OR into the result.
683 if (AI.TargetBitOffset)
684 Val = Builder.CreateShl(Val, AI.TargetBitOffset);
685 Res = Res ? Builder.CreateOr(Res, Val) : Val;
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000686 }
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000687
Daniel Dunbarecdb41e2010-04-13 23:34:15 +0000688 // If the bit-field is signed, perform the sign-extension.
689 //
690 // FIXME: This can easily be folded into the load of the high bits, which
691 // could also eliminate the mask of high bits in some situations.
692 if (Info.isSigned()) {
Daniel Dunbar26772612010-04-15 03:47:33 +0000693 unsigned ExtraBits = ResSizeInBits - Info.getSize();
Daniel Dunbarecdb41e2010-04-13 23:34:15 +0000694 if (ExtraBits)
695 Res = Builder.CreateAShr(Builder.CreateShl(Res, ExtraBits),
696 ExtraBits, "bf.val.sext");
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000697 }
Eli Friedman316bb1b2008-05-17 20:03:47 +0000698
Daniel Dunbarecdb41e2010-04-13 23:34:15 +0000699 return RValue::get(Res);
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000700}
701
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000702RValue CodeGenFunction::EmitLoadOfPropertyRefLValue(LValue LV,
703 QualType ExprType) {
704 return EmitObjCPropertyGet(LV.getPropertyRefExpr());
705}
706
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000707RValue CodeGenFunction::EmitLoadOfKVCRefLValue(LValue LV,
708 QualType ExprType) {
709 return EmitObjCPropertyGet(LV.getKVCRefExpr());
710}
711
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000712// If this is a reference to a subset of the elements of a vector, create an
713// appropriate shufflevector.
Nate Begeman213541a2008-04-18 23:10:10 +0000714RValue CodeGenFunction::EmitLoadOfExtVectorElementLValue(LValue LV,
715 QualType ExprType) {
Eli Friedman1e692ac2008-06-13 23:01:12 +0000716 llvm::Value *Vec = Builder.CreateLoad(LV.getExtVectorAddr(),
717 LV.isVolatileQualified(), "tmp");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000718
Nate Begeman8a997642008-05-09 06:41:27 +0000719 const llvm::Constant *Elts = LV.getExtVectorElts();
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000720
721 // If the result of the expression is a non-vector type, we must be extracting
722 // a single element. Just codegen as an extractelement.
John McCall183700f2009-09-21 23:43:11 +0000723 const VectorType *ExprVT = ExprType->getAs<VectorType>();
Chris Lattnercf60cd22007-08-10 17:10:08 +0000724 if (!ExprVT) {
Dan Gohman4f8d1232008-05-22 00:50:06 +0000725 unsigned InIdx = getAccessedFieldNo(0, Elts);
Owen Anderson0032b272009-08-13 21:57:51 +0000726 llvm::Value *Elt = llvm::ConstantInt::get(
727 llvm::Type::getInt32Ty(VMContext), InIdx);
Chris Lattner34cdc862007-08-03 16:18:34 +0000728 return RValue::get(Builder.CreateExtractElement(Vec, Elt, "tmp"));
729 }
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000730
731 // Always use shuffle vector to try to retain the original program structure
Chris Lattnercf60cd22007-08-10 17:10:08 +0000732 unsigned NumResultElts = ExprVT->getNumElements();
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000733
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000734 llvm::SmallVector<llvm::Constant*, 4> Mask;
Chris Lattner34cdc862007-08-03 16:18:34 +0000735 for (unsigned i = 0; i != NumResultElts; ++i) {
Dan Gohman4f8d1232008-05-22 00:50:06 +0000736 unsigned InIdx = getAccessedFieldNo(i, Elts);
Owen Anderson0032b272009-08-13 21:57:51 +0000737 Mask.push_back(llvm::ConstantInt::get(
738 llvm::Type::getInt32Ty(VMContext), InIdx));
Chris Lattner34cdc862007-08-03 16:18:34 +0000739 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000740
Owen Anderson4a289322009-07-28 21:22:35 +0000741 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000742 Vec = Builder.CreateShuffleVector(Vec,
Owen Anderson03e20502009-07-30 23:11:26 +0000743 llvm::UndefValue::get(Vec->getType()),
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000744 MaskV, "tmp");
745 return RValue::get(Vec);
Chris Lattner34cdc862007-08-03 16:18:34 +0000746}
747
748
Reid Spencer5f016e22007-07-11 17:01:13 +0000749
750/// EmitStoreThroughLValue - Store the specified rvalue into the specified
751/// lvalue, where both are guaranteed to the have the same type, and that type
752/// is 'Ty'.
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000753void CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst,
Reid Spencer5f016e22007-07-11 17:01:13 +0000754 QualType Ty) {
Chris Lattner017d6aa2007-08-03 16:28:33 +0000755 if (!Dst.isSimple()) {
756 if (Dst.isVectorElt()) {
757 // Read/modify/write the vector, inserting the new element.
Eli Friedman1e692ac2008-06-13 23:01:12 +0000758 llvm::Value *Vec = Builder.CreateLoad(Dst.getVectorAddr(),
759 Dst.isVolatileQualified(), "tmp");
Chris Lattner9b655512007-08-31 22:49:20 +0000760 Vec = Builder.CreateInsertElement(Vec, Src.getScalarVal(),
Chris Lattner017d6aa2007-08-03 16:28:33 +0000761 Dst.getVectorIdx(), "vecins");
Eli Friedman1e692ac2008-06-13 23:01:12 +0000762 Builder.CreateStore(Vec, Dst.getVectorAddr(),Dst.isVolatileQualified());
Chris Lattner017d6aa2007-08-03 16:28:33 +0000763 return;
764 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000765
Nate Begeman213541a2008-04-18 23:10:10 +0000766 // If this is an update of extended vector elements, insert them as
767 // appropriate.
768 if (Dst.isExtVectorElt())
769 return EmitStoreThroughExtVectorComponentLValue(Src, Dst, Ty);
Lauro Ramos Venancioa0c5d0e2008-01-22 22:36:45 +0000770
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000771 if (Dst.isBitField())
Lauro Ramos Venancioa0c5d0e2008-01-22 22:36:45 +0000772 return EmitStoreThroughBitfieldLValue(Src, Dst, Ty);
773
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000774 if (Dst.isPropertyRef())
775 return EmitStoreThroughPropertyRefLValue(Src, Dst, Ty);
776
Chris Lattnereb99b012009-10-28 17:39:19 +0000777 assert(Dst.isKVCRef() && "Unknown LValue type");
778 return EmitStoreThroughKVCRefLValue(Src, Dst, Ty);
Chris Lattner017d6aa2007-08-03 16:28:33 +0000779 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000780
Fariborz Jahanian4f676ed2009-02-21 00:30:43 +0000781 if (Dst.isObjCWeak() && !Dst.isNonGC()) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000782 // load of a __weak object.
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +0000783 llvm::Value *LvalueDst = Dst.getAddress();
784 llvm::Value *src = Src.getScalarVal();
Mike Stumpf33651c2009-04-14 00:57:29 +0000785 CGM.getObjCRuntime().EmitObjCWeakAssign(*this, src, LvalueDst);
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +0000786 return;
787 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000788
Fariborz Jahanian4f676ed2009-02-21 00:30:43 +0000789 if (Dst.isObjCStrong() && !Dst.isNonGC()) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000790 // load of a __strong object.
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +0000791 llvm::Value *LvalueDst = Dst.getAddress();
792 llvm::Value *src = Src.getScalarVal();
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000793 if (Dst.isObjCIvar()) {
794 assert(Dst.getBaseIvarExp() && "BaseIvarExp is NULL");
795 const llvm::Type *ResultType = ConvertType(getContext().LongTy);
796 llvm::Value *RHS = EmitScalarExpr(Dst.getBaseIvarExp());
Fariborz Jahanian76368e82009-09-25 00:00:20 +0000797 llvm::Value *dst = RHS;
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000798 RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast");
799 llvm::Value *LHS =
800 Builder.CreatePtrToInt(LvalueDst, ResultType, "sub.ptr.lhs.cast");
801 llvm::Value *BytesBetween = Builder.CreateSub(LHS, RHS, "ivar.offset");
Fariborz Jahanian76368e82009-09-25 00:00:20 +0000802 CGM.getObjCRuntime().EmitObjCIvarAssign(*this, src, dst,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000803 BytesBetween);
Chris Lattnereb99b012009-10-28 17:39:19 +0000804 } else if (Dst.isGlobalObjCRef())
Fariborz Jahanianbf63b872009-05-04 23:27:20 +0000805 CGM.getObjCRuntime().EmitObjCGlobalAssign(*this, src, LvalueDst);
806 else
807 CGM.getObjCRuntime().EmitObjCStrongCastAssign(*this, src, LvalueDst);
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +0000808 return;
809 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000810
Chris Lattner883f6a72007-08-11 00:04:45 +0000811 assert(Src.isScalar() && "Can't emit an agg store with this method");
Anders Carlssonb4aa4662009-05-19 18:50:41 +0000812 EmitStoreOfScalar(Src.getScalarVal(), Dst.getAddress(),
813 Dst.isVolatileQualified(), Ty);
Reid Spencer5f016e22007-07-11 17:01:13 +0000814}
815
Lauro Ramos Venancioa0c5d0e2008-01-22 22:36:45 +0000816void CodeGenFunction::EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst,
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000817 QualType Ty,
Daniel Dunbared3849b2008-11-19 09:36:46 +0000818 llvm::Value **Result) {
Daniel Dunbarefbf4872010-04-06 01:07:44 +0000819 const CGBitFieldInfo &Info = Dst.getBitFieldInfo();
Lauro Ramos Venancioa0c5d0e2008-01-22 22:36:45 +0000820
Daniel Dunbar26772612010-04-15 03:47:33 +0000821 // Get the output type.
Anders Carlsson48035352010-04-17 21:52:22 +0000822 const llvm::Type *ResLTy = ConvertTypeForMem(Ty);
Daniel Dunbar26772612010-04-15 03:47:33 +0000823 unsigned ResSizeInBits = CGM.getTargetData().getTypeSizeInBits(ResLTy);
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000824
Daniel Dunbar26772612010-04-15 03:47:33 +0000825 // Get the source value, truncated to the width of the bit-field.
Daniel Dunbared3849b2008-11-19 09:36:46 +0000826 llvm::Value *SrcVal = Src.getScalarVal();
Anders Carlsson48035352010-04-17 21:52:22 +0000827
828 if (Ty->isBooleanType())
829 SrcVal = Builder.CreateIntCast(SrcVal, ResLTy, /*IsSigned=*/false);
830
Daniel Dunbar26772612010-04-15 03:47:33 +0000831 SrcVal = Builder.CreateAnd(SrcVal, llvm::APInt::getLowBitsSet(ResSizeInBits,
832 Info.getSize()),
833 "bf.value");
Lauro Ramos Venancioa0c5d0e2008-01-22 22:36:45 +0000834
Daniel Dunbared3849b2008-11-19 09:36:46 +0000835 // Return the new value of the bit-field, if requested.
836 if (Result) {
837 // Cast back to the proper type for result.
Daniel Dunbar26772612010-04-15 03:47:33 +0000838 const llvm::Type *SrcTy = Src.getScalarVal()->getType();
839 llvm::Value *ReloadVal = Builder.CreateIntCast(SrcVal, SrcTy, false,
840 "bf.reload.val");
Daniel Dunbared3849b2008-11-19 09:36:46 +0000841
842 // Sign extend if necessary.
Daniel Dunbar26772612010-04-15 03:47:33 +0000843 if (Info.isSigned()) {
844 unsigned ExtraBits = ResSizeInBits - Info.getSize();
845 if (ExtraBits)
846 ReloadVal = Builder.CreateAShr(Builder.CreateShl(ReloadVal, ExtraBits),
847 ExtraBits, "bf.reload.sext");
Daniel Dunbared3849b2008-11-19 09:36:46 +0000848 }
849
Daniel Dunbar26772612010-04-15 03:47:33 +0000850 *Result = ReloadVal;
Daniel Dunbared3849b2008-11-19 09:36:46 +0000851 }
852
Daniel Dunbar26772612010-04-15 03:47:33 +0000853 // Iterate over the components, writing each piece to memory.
854 for (unsigned i = 0, e = Info.getNumComponents(); i != e; ++i) {
855 const CGBitFieldInfo::AccessInfo &AI = Info.getComponent(i);
Eli Friedman316bb1b2008-05-17 20:03:47 +0000856
Daniel Dunbar26772612010-04-15 03:47:33 +0000857 // Get the field pointer.
858 llvm::Value *Ptr = Dst.getBitFieldBaseAddr();
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000859
Daniel Dunbar26772612010-04-15 03:47:33 +0000860 // Only offset by the field index if used, so that incoming values are not
861 // required to be structures.
862 if (AI.FieldIndex)
863 Ptr = Builder.CreateStructGEP(Ptr, AI.FieldIndex, "bf.field");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000864
Daniel Dunbar26772612010-04-15 03:47:33 +0000865 // Offset by the byte offset, if used.
866 if (AI.FieldByteOffset) {
867 const llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(VMContext);
868 Ptr = Builder.CreateBitCast(Ptr, i8PTy);
869 Ptr = Builder.CreateConstGEP1_32(Ptr, AI.FieldByteOffset,"bf.field.offs");
870 }
Eli Friedman316bb1b2008-05-17 20:03:47 +0000871
Daniel Dunbar26772612010-04-15 03:47:33 +0000872 // Cast to the access type.
873 const llvm::Type *PTy = llvm::Type::getIntNPtrTy(VMContext, AI.AccessWidth,
874 Ty.getAddressSpace());
875 Ptr = Builder.CreateBitCast(Ptr, PTy);
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000876
Daniel Dunbar26772612010-04-15 03:47:33 +0000877 // Extract the piece of the bit-field value to write in this access, limited
878 // to the values that are part of this access.
879 llvm::Value *Val = SrcVal;
880 if (AI.TargetBitOffset)
881 Val = Builder.CreateLShr(Val, AI.TargetBitOffset);
882 Val = Builder.CreateAnd(Val, llvm::APInt::getLowBitsSet(ResSizeInBits,
883 AI.TargetBitWidth));
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000884
Daniel Dunbar26772612010-04-15 03:47:33 +0000885 // Extend or truncate to the access size.
886 const llvm::Type *AccessLTy =
887 llvm::Type::getIntNTy(VMContext, AI.AccessWidth);
888 if (ResSizeInBits < AI.AccessWidth)
889 Val = Builder.CreateZExt(Val, AccessLTy);
890 else if (ResSizeInBits > AI.AccessWidth)
891 Val = Builder.CreateTrunc(Val, AccessLTy);
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000892
Daniel Dunbar26772612010-04-15 03:47:33 +0000893 // Shift into the position in memory.
894 if (AI.FieldBitStart)
895 Val = Builder.CreateShl(Val, AI.FieldBitStart);
896
897 // If necessary, load and OR in bits that are outside of the bit-field.
898 if (AI.TargetBitWidth != AI.AccessWidth) {
899 llvm::LoadInst *Load = Builder.CreateLoad(Ptr, Dst.isVolatileQualified());
900 if (AI.AccessAlignment)
901 Load->setAlignment(AI.AccessAlignment);
902
903 // Compute the mask for zeroing the bits that are part of the bit-field.
904 llvm::APInt InvMask =
905 ~llvm::APInt::getBitsSet(AI.AccessWidth, AI.FieldBitStart,
906 AI.FieldBitStart + AI.TargetBitWidth);
907
908 // Apply the mask and OR in to the value to write.
909 Val = Builder.CreateOr(Builder.CreateAnd(Load, InvMask), Val);
910 }
911
912 // Write the value.
913 llvm::StoreInst *Store = Builder.CreateStore(Val, Ptr,
914 Dst.isVolatileQualified());
915 if (AI.AccessAlignment)
916 Store->setAlignment(AI.AccessAlignment);
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000917 }
Lauro Ramos Venancioa0c5d0e2008-01-22 22:36:45 +0000918}
919
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000920void CodeGenFunction::EmitStoreThroughPropertyRefLValue(RValue Src,
921 LValue Dst,
922 QualType Ty) {
923 EmitObjCPropertySet(Dst.getPropertyRefExpr(), Src);
924}
925
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000926void CodeGenFunction::EmitStoreThroughKVCRefLValue(RValue Src,
927 LValue Dst,
928 QualType Ty) {
929 EmitObjCPropertySet(Dst.getKVCRefExpr(), Src);
930}
931
Nate Begeman213541a2008-04-18 23:10:10 +0000932void CodeGenFunction::EmitStoreThroughExtVectorComponentLValue(RValue Src,
933 LValue Dst,
934 QualType Ty) {
Chris Lattner017d6aa2007-08-03 16:28:33 +0000935 // This access turns into a read/modify/write of the vector. Load the input
936 // value now.
Eli Friedman1e692ac2008-06-13 23:01:12 +0000937 llvm::Value *Vec = Builder.CreateLoad(Dst.getExtVectorAddr(),
938 Dst.isVolatileQualified(), "tmp");
Nate Begeman8a997642008-05-09 06:41:27 +0000939 const llvm::Constant *Elts = Dst.getExtVectorElts();
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000940
Chris Lattner9b655512007-08-31 22:49:20 +0000941 llvm::Value *SrcVal = Src.getScalarVal();
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000942
John McCall183700f2009-09-21 23:43:11 +0000943 if (const VectorType *VTy = Ty->getAs<VectorType>()) {
Chris Lattner7e6b51b2007-08-03 16:37:04 +0000944 unsigned NumSrcElts = VTy->getNumElements();
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000945 unsigned NumDstElts =
946 cast<llvm::VectorType>(Vec->getType())->getNumElements();
947 if (NumDstElts == NumSrcElts) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000948 // Use shuffle vector is the src and destination are the same number of
949 // elements and restore the vector mask since it is on the side it will be
950 // stored.
Nate Begeman6e5dd862009-06-26 21:12:50 +0000951 llvm::SmallVector<llvm::Constant*, 4> Mask(NumDstElts);
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000952 for (unsigned i = 0; i != NumSrcElts; ++i) {
953 unsigned InIdx = getAccessedFieldNo(i, Elts);
Owen Anderson0032b272009-08-13 21:57:51 +0000954 Mask[InIdx] = llvm::ConstantInt::get(
955 llvm::Type::getInt32Ty(VMContext), i);
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000956 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000957
Owen Anderson4a289322009-07-28 21:22:35 +0000958 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000959 Vec = Builder.CreateShuffleVector(SrcVal,
Owen Anderson03e20502009-07-30 23:11:26 +0000960 llvm::UndefValue::get(Vec->getType()),
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000961 MaskV, "tmp");
Mike Stumpb3589f42009-07-30 22:28:39 +0000962 } else if (NumDstElts > NumSrcElts) {
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000963 // Extended the source vector to the same length and then shuffle it
964 // into the destination.
965 // FIXME: since we're shuffling with undef, can we just use the indices
966 // into that? This could be simpler.
967 llvm::SmallVector<llvm::Constant*, 4> ExtMask;
Chris Lattnereb99b012009-10-28 17:39:19 +0000968 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000969 unsigned i;
970 for (i = 0; i != NumSrcElts; ++i)
Chris Lattnereb99b012009-10-28 17:39:19 +0000971 ExtMask.push_back(llvm::ConstantInt::get(Int32Ty, i));
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000972 for (; i != NumDstElts; ++i)
Chris Lattnereb99b012009-10-28 17:39:19 +0000973 ExtMask.push_back(llvm::UndefValue::get(Int32Ty));
Owen Anderson4a289322009-07-28 21:22:35 +0000974 llvm::Value *ExtMaskV = llvm::ConstantVector::get(&ExtMask[0],
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000975 ExtMask.size());
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000976 llvm::Value *ExtSrcVal =
Daniel Dunbarbb767732009-02-17 18:31:04 +0000977 Builder.CreateShuffleVector(SrcVal,
Owen Anderson03e20502009-07-30 23:11:26 +0000978 llvm::UndefValue::get(SrcVal->getType()),
Daniel Dunbarbb767732009-02-17 18:31:04 +0000979 ExtMaskV, "tmp");
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000980 // build identity
981 llvm::SmallVector<llvm::Constant*, 4> Mask;
Chris Lattnereb99b012009-10-28 17:39:19 +0000982 for (unsigned i = 0; i != NumDstElts; ++i)
983 Mask.push_back(llvm::ConstantInt::get(Int32Ty, i));
984
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000985 // modify when what gets shuffled in
986 for (unsigned i = 0; i != NumSrcElts; ++i) {
987 unsigned Idx = getAccessedFieldNo(i, Elts);
Chris Lattnereb99b012009-10-28 17:39:19 +0000988 Mask[Idx] = llvm::ConstantInt::get(Int32Ty, i+NumDstElts);
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000989 }
Owen Anderson4a289322009-07-28 21:22:35 +0000990 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000991 Vec = Builder.CreateShuffleVector(Vec, ExtSrcVal, MaskV, "tmp");
Mike Stumpb3589f42009-07-30 22:28:39 +0000992 } else {
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000993 // We should never shorten the vector
994 assert(0 && "unexpected shorten vector length");
Chris Lattner7e6b51b2007-08-03 16:37:04 +0000995 }
996 } else {
997 // If the Src is a scalar (not a vector) it must be updating one element.
Dan Gohman4f8d1232008-05-22 00:50:06 +0000998 unsigned InIdx = getAccessedFieldNo(0, Elts);
Chris Lattnereb99b012009-10-28 17:39:19 +0000999 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
1000 llvm::Value *Elt = llvm::ConstantInt::get(Int32Ty, InIdx);
Chris Lattner017d6aa2007-08-03 16:28:33 +00001001 Vec = Builder.CreateInsertElement(Vec, SrcVal, Elt, "tmp");
Chris Lattner017d6aa2007-08-03 16:28:33 +00001002 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001003
Eli Friedman1e692ac2008-06-13 23:01:12 +00001004 Builder.CreateStore(Vec, Dst.getExtVectorAddr(), Dst.isVolatileQualified());
Chris Lattner017d6aa2007-08-03 16:28:33 +00001005}
1006
Fariborz Jahanianb123ea32009-09-16 21:37:16 +00001007// setObjCGCLValueClass - sets class of he lvalue for the purpose of
1008// generating write-barries API. It is currently a global, ivar,
1009// or neither.
Chris Lattnereb99b012009-10-28 17:39:19 +00001010static void setObjCGCLValueClass(const ASTContext &Ctx, const Expr *E,
1011 LValue &LV) {
Fariborz Jahanianb9242592009-09-21 23:03:37 +00001012 if (Ctx.getLangOptions().getGCMode() == LangOptions::NonGC)
Fariborz Jahanianb123ea32009-09-16 21:37:16 +00001013 return;
1014
Fariborz Jahaniandbf3cfd2009-09-16 23:11:23 +00001015 if (isa<ObjCIvarRefExpr>(E)) {
1016 LV.SetObjCIvar(LV, true);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00001017 ObjCIvarRefExpr *Exp = cast<ObjCIvarRefExpr>(const_cast<Expr*>(E));
1018 LV.setBaseIvarExp(Exp->getBase());
Fariborz Jahanianfd02ed72009-09-21 18:54:29 +00001019 LV.SetObjCArray(LV, E->getType()->isArrayType());
Fariborz Jahaniandbf3cfd2009-09-16 23:11:23 +00001020 return;
1021 }
Chris Lattnereb99b012009-10-28 17:39:19 +00001022
Fariborz Jahanianb123ea32009-09-16 21:37:16 +00001023 if (const DeclRefExpr *Exp = dyn_cast<DeclRefExpr>(E)) {
1024 if (const VarDecl *VD = dyn_cast<VarDecl>(Exp->getDecl())) {
1025 if ((VD->isBlockVarDecl() && !VD->hasLocalStorage()) ||
1026 VD->isFileVarDecl())
1027 LV.SetGlobalObjCRef(LV, true);
1028 }
Fariborz Jahanianfd02ed72009-09-21 18:54:29 +00001029 LV.SetObjCArray(LV, E->getType()->isArrayType());
Chris Lattnereb99b012009-10-28 17:39:19 +00001030 return;
Fariborz Jahanianb123ea32009-09-16 21:37:16 +00001031 }
Chris Lattnereb99b012009-10-28 17:39:19 +00001032
1033 if (const UnaryOperator *Exp = dyn_cast<UnaryOperator>(E)) {
Fariborz Jahanianb123ea32009-09-16 21:37:16 +00001034 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
Chris Lattnereb99b012009-10-28 17:39:19 +00001035 return;
1036 }
1037
1038 if (const ParenExpr *Exp = dyn_cast<ParenExpr>(E)) {
Fariborz Jahanianb123ea32009-09-16 21:37:16 +00001039 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
Fariborz Jahanian75b08f12009-09-30 17:10:29 +00001040 if (LV.isObjCIvar()) {
1041 // If cast is to a structure pointer, follow gcc's behavior and make it
1042 // a non-ivar write-barrier.
1043 QualType ExpTy = E->getType();
1044 if (ExpTy->isPointerType())
1045 ExpTy = ExpTy->getAs<PointerType>()->getPointeeType();
1046 if (ExpTy->isRecordType())
1047 LV.SetObjCIvar(LV, false);
Chris Lattnereb99b012009-10-28 17:39:19 +00001048 }
1049 return;
Fariborz Jahanian75b08f12009-09-30 17:10:29 +00001050 }
Chris Lattnereb99b012009-10-28 17:39:19 +00001051 if (const ImplicitCastExpr *Exp = dyn_cast<ImplicitCastExpr>(E)) {
Fariborz Jahanianb123ea32009-09-16 21:37:16 +00001052 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
Chris Lattnereb99b012009-10-28 17:39:19 +00001053 return;
1054 }
1055
1056 if (const CStyleCastExpr *Exp = dyn_cast<CStyleCastExpr>(E)) {
Fariborz Jahanianb123ea32009-09-16 21:37:16 +00001057 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
Chris Lattnereb99b012009-10-28 17:39:19 +00001058 return;
1059 }
1060
1061 if (const ArraySubscriptExpr *Exp = dyn_cast<ArraySubscriptExpr>(E)) {
Fariborz Jahanianb123ea32009-09-16 21:37:16 +00001062 setObjCGCLValueClass(Ctx, Exp->getBase(), LV);
Fariborz Jahanianfd02ed72009-09-21 18:54:29 +00001063 if (LV.isObjCIvar() && !LV.isObjCArray())
Fariborz Jahanian1c1afc42009-09-18 00:04:00 +00001064 // Using array syntax to assigning to what an ivar points to is not
1065 // same as assigning to the ivar itself. {id *Names;} Names[i] = 0;
1066 LV.SetObjCIvar(LV, false);
Fariborz Jahanianfd02ed72009-09-21 18:54:29 +00001067 else if (LV.isGlobalObjCRef() && !LV.isObjCArray())
1068 // Using array syntax to assigning to what global points to is not
1069 // same as assigning to the global itself. {id *G;} G[i] = 0;
1070 LV.SetGlobalObjCRef(LV, false);
Chris Lattnereb99b012009-10-28 17:39:19 +00001071 return;
Fariborz Jahanian1c1afc42009-09-18 00:04:00 +00001072 }
Chris Lattnereb99b012009-10-28 17:39:19 +00001073
1074 if (const MemberExpr *Exp = dyn_cast<MemberExpr>(E)) {
Fariborz Jahanianb123ea32009-09-16 21:37:16 +00001075 setObjCGCLValueClass(Ctx, Exp->getBase(), LV);
Fariborz Jahanian1c1afc42009-09-18 00:04:00 +00001076 // We don't know if member is an 'ivar', but this flag is looked at
1077 // only in the context of LV.isObjCIvar().
Fariborz Jahanianfd02ed72009-09-21 18:54:29 +00001078 LV.SetObjCArray(LV, E->getType()->isArrayType());
Chris Lattnereb99b012009-10-28 17:39:19 +00001079 return;
Fariborz Jahanianb123ea32009-09-16 21:37:16 +00001080 }
1081}
1082
Anders Carlssonce53f7d2009-11-07 23:06:58 +00001083static LValue EmitGlobalVarDeclLValue(CodeGenFunction &CGF,
1084 const Expr *E, const VarDecl *VD) {
Daniel Dunbard2113f22009-11-08 09:46:46 +00001085 assert((VD->hasExternalStorage() || VD->isFileVarDecl()) &&
Anders Carlssonce53f7d2009-11-07 23:06:58 +00001086 "Var decl must have external storage or be a file var decl!");
1087
1088 llvm::Value *V = CGF.CGM.GetAddrOfGlobalVar(VD);
1089 if (VD->getType()->isReferenceType())
1090 V = CGF.Builder.CreateLoad(V, "tmp");
1091 LValue LV = LValue::MakeAddr(V, CGF.MakeQualifiers(E->getType()));
1092 setObjCGCLValueClass(CGF.getContext(), E, LV);
1093 return LV;
1094}
1095
Eli Friedman9a146302009-11-26 06:08:14 +00001096static LValue EmitFunctionDeclLValue(CodeGenFunction &CGF,
1097 const Expr *E, const FunctionDecl *FD) {
1098 llvm::Value* V = CGF.CGM.GetAddrOfFunction(FD);
1099 if (!FD->hasPrototype()) {
1100 if (const FunctionProtoType *Proto =
1101 FD->getType()->getAs<FunctionProtoType>()) {
1102 // Ugly case: for a K&R-style definition, the type of the definition
1103 // isn't the same as the type of a use. Correct for this with a
1104 // bitcast.
1105 QualType NoProtoType =
1106 CGF.getContext().getFunctionNoProtoType(Proto->getResultType());
1107 NoProtoType = CGF.getContext().getPointerType(NoProtoType);
1108 V = CGF.Builder.CreateBitCast(V, CGF.ConvertType(NoProtoType), "tmp");
1109 }
1110 }
1111 return LValue::MakeAddr(V, CGF.MakeQualifiers(E->getType()));
1112}
1113
Reid Spencer5f016e22007-07-11 17:01:13 +00001114LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) {
Anders Carlsson1e74c4f2009-11-07 22:53:10 +00001115 const NamedDecl *ND = E->getDecl();
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001116
Rafael Espindola6a836702010-03-04 18:17:24 +00001117 if (ND->hasAttr<WeakRefAttr>()) {
1118 const ValueDecl* VD = cast<ValueDecl>(ND);
1119 llvm::Constant *Aliasee = CGM.GetWeakRefReference(VD);
1120
1121 Qualifiers Quals = MakeQualifiers(E->getType());
1122 LValue LV = LValue::MakeAddr(Aliasee, Quals);
1123
1124 return LV;
1125 }
1126
Anders Carlsson1e74c4f2009-11-07 22:53:10 +00001127 if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
Anders Carlsson1e74c4f2009-11-07 22:53:10 +00001128
1129 // Check if this is a global variable.
Anders Carlssonce53f7d2009-11-07 23:06:58 +00001130 if (VD->hasExternalStorage() || VD->isFileVarDecl())
1131 return EmitGlobalVarDeclLValue(*this, E, VD);
Anders Carlsson0bc70492009-11-07 22:46:42 +00001132
1133 bool NonGCable = VD->hasLocalStorage() && !VD->hasAttr<BlocksAttr>();
1134
1135 llvm::Value *V = LocalDeclMap[VD];
Fariborz Jahanian63326a52010-04-19 18:15:02 +00001136 if (!V && getContext().getLangOptions().CPlusPlus &&
1137 VD->isStaticLocal())
1138 V = CGM.getStaticLocalDeclAddress(VD);
Anders Carlsson0bc70492009-11-07 22:46:42 +00001139 assert(V && "DeclRefExpr not entered in LocalDeclMap?");
1140
1141 Qualifiers Quals = MakeQualifiers(E->getType());
1142 // local variables do not get their gc attribute set.
1143 // local static?
1144 if (NonGCable) Quals.removeObjCGCAttr();
1145
1146 if (VD->hasAttr<BlocksAttr>()) {
1147 V = Builder.CreateStructGEP(V, 1, "forwarding");
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00001148 V = Builder.CreateLoad(V);
Anders Carlsson0bc70492009-11-07 22:46:42 +00001149 V = Builder.CreateStructGEP(V, getByRefValueLLVMField(VD),
1150 VD->getNameAsString());
1151 }
1152 if (VD->getType()->isReferenceType())
1153 V = Builder.CreateLoad(V, "tmp");
Anders Carlssonce53f7d2009-11-07 23:06:58 +00001154 LValue LV = LValue::MakeAddr(V, Quals);
Anders Carlsson0bc70492009-11-07 22:46:42 +00001155 LValue::SetObjCNonGC(LV, NonGCable);
Fariborz Jahanianb123ea32009-09-16 21:37:16 +00001156 setObjCGCLValueClass(getContext(), E, LV);
Fariborz Jahanian2682d8b2008-11-20 00:15:42 +00001157 return LV;
Chris Lattnereb99b012009-10-28 17:39:19 +00001158 }
1159
Eli Friedman9a146302009-11-26 06:08:14 +00001160 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
1161 return EmitFunctionDeclLValue(*this, E, FD);
Chris Lattnereb99b012009-10-28 17:39:19 +00001162
Anders Carlsson45147d02010-02-02 03:37:46 +00001163 // FIXME: the qualifier check does not seem sufficient here
Chris Lattnereb99b012009-10-28 17:39:19 +00001164 if (E->getQualifier()) {
Anders Carlsson45147d02010-02-02 03:37:46 +00001165 const FieldDecl *FD = cast<FieldDecl>(ND);
1166 llvm::Value *V = CGM.EmitPointerToDataMember(FD);
1167
1168 return LValue::MakeAddr(V, MakeQualifiers(FD->getType()));
Chris Lattner41110242008-06-17 18:05:57 +00001169 }
Chris Lattnereb99b012009-10-28 17:39:19 +00001170
Anders Carlsson1e74c4f2009-11-07 22:53:10 +00001171 assert(false && "Unhandled DeclRefExpr");
1172
1173 // an invalid LValue, but the assert will
1174 // ensure that this point is never reached.
Chris Lattnerb1776cb2007-09-16 19:23:47 +00001175 return LValue();
Reid Spencer5f016e22007-07-11 17:01:13 +00001176}
1177
Mike Stumpa99038c2009-02-28 09:07:16 +00001178LValue CodeGenFunction::EmitBlockDeclRefLValue(const BlockDeclRefExpr *E) {
John McCall0953e762009-09-24 19:53:00 +00001179 return LValue::MakeAddr(GetAddrOfBlockDecl(E), MakeQualifiers(E->getType()));
Mike Stumpa99038c2009-02-28 09:07:16 +00001180}
1181
Reid Spencer5f016e22007-07-11 17:01:13 +00001182LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) {
1183 // __extension__ doesn't affect lvalue-ness.
1184 if (E->getOpcode() == UnaryOperator::Extension)
1185 return EmitLValue(E->getSubExpr());
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001186
Chris Lattner96196622008-07-26 22:37:01 +00001187 QualType ExprTy = getContext().getCanonicalType(E->getSubExpr()->getType());
Chris Lattner7da36f62007-10-30 22:53:42 +00001188 switch (E->getOpcode()) {
1189 default: assert(0 && "Unknown unary operator lvalue!");
Chris Lattnereb99b012009-10-28 17:39:19 +00001190 case UnaryOperator::Deref: {
1191 QualType T = E->getSubExpr()->getType()->getPointeeType();
1192 assert(!T.isNull() && "CodeGenFunction::EmitUnaryOpLValue: Illegal type");
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001193
Chris Lattnereb99b012009-10-28 17:39:19 +00001194 Qualifiers Quals = MakeQualifiers(T);
1195 Quals.setAddressSpace(ExprTy.getAddressSpace());
John McCall0953e762009-09-24 19:53:00 +00001196
Chris Lattnereb99b012009-10-28 17:39:19 +00001197 LValue LV = LValue::MakeAddr(EmitScalarExpr(E->getSubExpr()), Quals);
1198 // We should not generate __weak write barrier on indirect reference
1199 // of a pointer to object; as in void foo (__weak id *param); *param = 0;
1200 // But, we continue to generate __strong write barrier on indirect write
1201 // into a pointer to object.
1202 if (getContext().getLangOptions().ObjC1 &&
1203 getContext().getLangOptions().getGCMode() != LangOptions::NonGC &&
1204 LV.isObjCWeak())
1205 LValue::SetObjCNonGC(LV, !E->isOBJCGCCandidate(getContext()));
1206 return LV;
1207 }
Chris Lattner7da36f62007-10-30 22:53:42 +00001208 case UnaryOperator::Real:
Eli Friedmane401cd52009-11-09 04:20:47 +00001209 case UnaryOperator::Imag: {
Chris Lattner7da36f62007-10-30 22:53:42 +00001210 LValue LV = EmitLValue(E->getSubExpr());
Chris Lattner36b6a0a2008-03-19 05:19:41 +00001211 unsigned Idx = E->getOpcode() == UnaryOperator::Imag;
1212 return LValue::MakeAddr(Builder.CreateStructGEP(LV.getAddress(),
Chris Lattnerb77792e2008-07-26 22:17:49 +00001213 Idx, "idx"),
John McCall0953e762009-09-24 19:53:00 +00001214 MakeQualifiers(ExprTy));
Chris Lattner7da36f62007-10-30 22:53:42 +00001215 }
Eli Friedmane401cd52009-11-09 04:20:47 +00001216 case UnaryOperator::PreInc:
Chris Lattner197a3382010-01-09 21:44:40 +00001217 case UnaryOperator::PreDec: {
1218 LValue LV = EmitLValue(E->getSubExpr());
1219 bool isInc = E->getOpcode() == UnaryOperator::PreInc;
1220
1221 if (E->getType()->isAnyComplexType())
1222 EmitComplexPrePostIncDec(E, LV, isInc, true/*isPre*/);
1223 else
1224 EmitScalarPrePostIncDec(E, LV, isInc, true/*isPre*/);
1225 return LV;
1226 }
Eli Friedmane401cd52009-11-09 04:20:47 +00001227 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001228}
1229
1230LValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) {
John McCall0953e762009-09-24 19:53:00 +00001231 return LValue::MakeAddr(CGM.GetAddrOfConstantStringFromLiteral(E),
1232 Qualifiers());
Reid Spencer5f016e22007-07-11 17:01:13 +00001233}
1234
Chris Lattnereaf2bb82009-02-24 22:18:39 +00001235LValue CodeGenFunction::EmitObjCEncodeExprLValue(const ObjCEncodeExpr *E) {
John McCall0953e762009-09-24 19:53:00 +00001236 return LValue::MakeAddr(CGM.GetAddrOfConstantStringFromObjCEncode(E),
1237 Qualifiers());
Chris Lattnereaf2bb82009-02-24 22:18:39 +00001238}
1239
1240
Daniel Dunbar662b71e2008-10-17 21:58:32 +00001241LValue CodeGenFunction::EmitPredefinedFunctionName(unsigned Type) {
Anders Carlsson22742662007-07-21 05:21:51 +00001242 std::string GlobalVarName;
Daniel Dunbar662b71e2008-10-17 21:58:32 +00001243
1244 switch (Type) {
Chris Lattnereb99b012009-10-28 17:39:19 +00001245 default: assert(0 && "Invalid type");
Chris Lattnereaf2bb82009-02-24 22:18:39 +00001246 case PredefinedExpr::Func:
1247 GlobalVarName = "__func__.";
1248 break;
1249 case PredefinedExpr::Function:
1250 GlobalVarName = "__FUNCTION__.";
1251 break;
1252 case PredefinedExpr::PrettyFunction:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00001253 GlobalVarName = "__PRETTY_FUNCTION__.";
1254 break;
Anders Carlsson22742662007-07-21 05:21:51 +00001255 }
Daniel Dunbar662b71e2008-10-17 21:58:32 +00001256
Daniel Dunbar0a23d762009-09-12 23:06:21 +00001257 llvm::StringRef FnName = CurFn->getName();
1258 if (FnName.startswith("\01"))
1259 FnName = FnName.substr(1);
1260 GlobalVarName += FnName;
1261
Anders Carlsson3a082d82009-09-08 18:24:21 +00001262 std::string FunctionName =
Anders Carlsson848fa642010-02-11 18:20:28 +00001263 PredefinedExpr::ComputeName((PredefinedExpr::IdentType)Type, CurCodeDecl);
Daniel Dunbar662b71e2008-10-17 21:58:32 +00001264
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001265 llvm::Constant *C =
Daniel Dunbar662b71e2008-10-17 21:58:32 +00001266 CGM.GetAddrOfConstantCString(FunctionName, GlobalVarName.c_str());
John McCall0953e762009-09-24 19:53:00 +00001267 return LValue::MakeAddr(C, Qualifiers());
Daniel Dunbar662b71e2008-10-17 21:58:32 +00001268}
1269
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001270LValue CodeGenFunction::EmitPredefinedLValue(const PredefinedExpr *E) {
Daniel Dunbar662b71e2008-10-17 21:58:32 +00001271 switch (E->getIdentType()) {
1272 default:
1273 return EmitUnsupportedLValue(E, "predefined expression");
1274 case PredefinedExpr::Func:
1275 case PredefinedExpr::Function:
1276 case PredefinedExpr::PrettyFunction:
1277 return EmitPredefinedFunctionName(E->getIdentType());
1278 }
Anders Carlsson22742662007-07-21 05:21:51 +00001279}
1280
Mike Stumpd8af3602009-12-15 01:22:35 +00001281llvm::BasicBlock *CodeGenFunction::getTrapBB() {
Mike Stump41513442009-12-15 00:59:40 +00001282 const CodeGenOptions &GCO = CGM.getCodeGenOpts();
1283
1284 // If we are not optimzing, don't collapse all calls to trap in the function
1285 // to the same call, that way, in the debugger they can see which operation
1286 // did in fact fail. If we are optimizing, we collpase all call to trap down
1287 // to just one per function to save on codesize.
1288 if (GCO.OptimizationLevel
1289 && TrapBB)
Mike Stump15037ca2009-12-15 00:35:12 +00001290 return TrapBB;
Mike Stump9c276ae2009-12-12 01:27:46 +00001291
1292 llvm::BasicBlock *Cont = 0;
1293 if (HaveInsertPoint()) {
1294 Cont = createBasicBlock("cont");
1295 EmitBranch(Cont);
1296 }
Mike Stump15037ca2009-12-15 00:35:12 +00001297 TrapBB = createBasicBlock("trap");
1298 EmitBlock(TrapBB);
1299
1300 llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::trap, 0, 0);
1301 llvm::CallInst *TrapCall = Builder.CreateCall(F);
1302 TrapCall->setDoesNotReturn();
1303 TrapCall->setDoesNotThrow();
Mike Stump9c276ae2009-12-12 01:27:46 +00001304 Builder.CreateUnreachable();
1305
1306 if (Cont)
1307 EmitBlock(Cont);
Mike Stump15037ca2009-12-15 00:35:12 +00001308 return TrapBB;
Mike Stump9c276ae2009-12-12 01:27:46 +00001309}
1310
Reid Spencer5f016e22007-07-11 17:01:13 +00001311LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) {
Ted Kremenek23245122007-08-20 16:18:38 +00001312 // The index must always be an integer, which is not an aggregate. Emit it.
Chris Lattner7f02f722007-08-24 05:35:26 +00001313 llvm::Value *Idx = EmitScalarExpr(E->getIdx());
Eli Friedman61d004a2009-06-06 19:09:26 +00001314 QualType IdxTy = E->getIdx()->getType();
1315 bool IdxSigned = IdxTy->isSignedIntegerType();
1316
Reid Spencer5f016e22007-07-11 17:01:13 +00001317 // If the base is a vector type, then we are forming a vector element lvalue
1318 // with this subscript.
Eli Friedman1e692ac2008-06-13 23:01:12 +00001319 if (E->getBase()->getType()->isVectorType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001320 // Emit the vector as an lvalue to get its address.
Eli Friedman1e692ac2008-06-13 23:01:12 +00001321 LValue LHS = EmitLValue(E->getBase());
Ted Kremenek23245122007-08-20 16:18:38 +00001322 assert(LHS.isSimple() && "Can only subscript lvalue vectors here!");
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001323 Idx = Builder.CreateIntCast(Idx,
Owen Anderson0032b272009-08-13 21:57:51 +00001324 llvm::Type::getInt32Ty(VMContext), IdxSigned, "vidx");
Eli Friedman1e692ac2008-06-13 23:01:12 +00001325 return LValue::MakeVectorElt(LHS.getAddress(), Idx,
John McCall0953e762009-09-24 19:53:00 +00001326 E->getBase()->getType().getCVRQualifiers());
Reid Spencer5f016e22007-07-11 17:01:13 +00001327 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001328
Ted Kremenek23245122007-08-20 16:18:38 +00001329 // The base must be a pointer, which is not an aggregate. Emit it.
Chris Lattner7f02f722007-08-24 05:35:26 +00001330 llvm::Value *Base = EmitScalarExpr(E->getBase());
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001331
Ted Kremenek23245122007-08-20 16:18:38 +00001332 // Extend or truncate the index type to 32 or 64-bits.
Reid Spencer5f016e22007-07-11 17:01:13 +00001333 unsigned IdxBitwidth = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
Sanjiv Gupta7cabee52009-04-24 02:40:57 +00001334 if (IdxBitwidth != LLVMPointerWidth)
Owen Anderson0032b272009-08-13 21:57:51 +00001335 Idx = Builder.CreateIntCast(Idx,
1336 llvm::IntegerType::get(VMContext, LLVMPointerWidth),
Reid Spencer5f016e22007-07-11 17:01:13 +00001337 IdxSigned, "idxprom");
1338
Mike Stumpb14e62d2009-12-16 02:57:00 +00001339 // FIXME: As llvm implements the object size checking, this can come out.
Mike Stump9c276ae2009-12-12 01:27:46 +00001340 if (CatchUndefined) {
Mike Stumpb14e62d2009-12-16 02:57:00 +00001341 if (const ImplicitCastExpr *ICE=dyn_cast<ImplicitCastExpr>(E->getBase())) {
Mike Stump9c276ae2009-12-12 01:27:46 +00001342 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr())) {
1343 if (ICE->getCastKind() == CastExpr::CK_ArrayToPointerDecay) {
1344 if (const ConstantArrayType *CAT
1345 = getContext().getAsConstantArrayType(DRE->getType())) {
1346 llvm::APInt Size = CAT->getSize();
1347 llvm::BasicBlock *Cont = createBasicBlock("cont");
Mike Stump750c85e2009-12-14 22:14:31 +00001348 Builder.CreateCondBr(Builder.CreateICmpULE(Idx,
Mike Stump9c276ae2009-12-12 01:27:46 +00001349 llvm::ConstantInt::get(Idx->getType(), Size)),
Mike Stump15037ca2009-12-15 00:35:12 +00001350 Cont, getTrapBB());
Mike Stump96a063a2009-12-14 20:52:00 +00001351 EmitBlock(Cont);
Mike Stump9c276ae2009-12-12 01:27:46 +00001352 }
1353 }
1354 }
1355 }
1356 }
1357
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001358 // We know that the pointer points to a type of the correct size, unless the
1359 // size is a VLA or Objective-C interface.
Daniel Dunbar2a866252009-04-25 05:08:32 +00001360 llvm::Value *Address = 0;
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001361 if (const VariableArrayType *VAT =
Anders Carlsson8b33c082008-12-21 00:11:23 +00001362 getContext().getAsVariableArrayType(E->getType())) {
Chris Lattner881eb9c2009-08-14 23:43:22 +00001363 llvm::Value *VLASize = GetVLASize(VAT);
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001364
Anders Carlsson8b33c082008-12-21 00:11:23 +00001365 Idx = Builder.CreateMul(Idx, VLASize);
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001366
Anders Carlsson6183a992008-12-21 03:44:36 +00001367 QualType BaseType = getContext().getBaseElementType(VAT);
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001368
Ken Dyck199c3d62010-01-11 17:06:35 +00001369 CharUnits BaseTypeSize = getContext().getTypeSizeInChars(BaseType);
Anders Carlsson8b33c082008-12-21 00:11:23 +00001370 Idx = Builder.CreateUDiv(Idx,
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001371 llvm::ConstantInt::get(Idx->getType(),
Ken Dyck199c3d62010-01-11 17:06:35 +00001372 BaseTypeSize.getQuantity()));
Dan Gohman664f8932009-08-12 00:33:55 +00001373 Address = Builder.CreateInBoundsGEP(Base, Idx, "arrayidx");
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001374 } else if (const ObjCInterfaceType *OIT =
Daniel Dunbar2a866252009-04-25 05:08:32 +00001375 dyn_cast<ObjCInterfaceType>(E->getType())) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001376 llvm::Value *InterfaceSize =
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001377 llvm::ConstantInt::get(Idx->getType(),
Ken Dyck199c3d62010-01-11 17:06:35 +00001378 getContext().getTypeSizeInChars(OIT).getQuantity());
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001379
Daniel Dunbar2a866252009-04-25 05:08:32 +00001380 Idx = Builder.CreateMul(Idx, InterfaceSize);
1381
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00001382 const llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(VMContext);
Dan Gohman664f8932009-08-12 00:33:55 +00001383 Address = Builder.CreateGEP(Builder.CreateBitCast(Base, i8PTy),
Daniel Dunbar2a866252009-04-25 05:08:32 +00001384 Idx, "arrayidx");
1385 Address = Builder.CreateBitCast(Address, Base->getType());
1386 } else {
Dan Gohman664f8932009-08-12 00:33:55 +00001387 Address = Builder.CreateInBoundsGEP(Base, Idx, "arrayidx");
Anders Carlsson8b33c082008-12-21 00:11:23 +00001388 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001389
Steve Naroff14108da2009-07-10 23:34:53 +00001390 QualType T = E->getBase()->getType()->getPointeeType();
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001391 assert(!T.isNull() &&
Steve Naroff14108da2009-07-10 23:34:53 +00001392 "CodeGenFunction::EmitArraySubscriptExpr(): Illegal base type");
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001393
John McCall0953e762009-09-24 19:53:00 +00001394 Qualifiers Quals = MakeQualifiers(T);
1395 Quals.setAddressSpace(E->getBase()->getType().getAddressSpace());
1396
1397 LValue LV = LValue::MakeAddr(Address, Quals);
Fariborz Jahanian643887a2009-02-21 23:37:19 +00001398 if (getContext().getLangOptions().ObjC1 &&
Fariborz Jahanianb123ea32009-09-16 21:37:16 +00001399 getContext().getLangOptions().getGCMode() != LangOptions::NonGC) {
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001400 LValue::SetObjCNonGC(LV, !E->isOBJCGCCandidate(getContext()));
Fariborz Jahanianb123ea32009-09-16 21:37:16 +00001401 setObjCGCLValueClass(getContext(), E, LV);
1402 }
Fariborz Jahanian643887a2009-02-21 23:37:19 +00001403 return LV;
Reid Spencer5f016e22007-07-11 17:01:13 +00001404}
1405
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001406static
Owen Andersona1cf15f2009-07-14 23:10:40 +00001407llvm::Constant *GenerateConstantVector(llvm::LLVMContext &VMContext,
1408 llvm::SmallVector<unsigned, 4> &Elts) {
Chris Lattner998eab12009-12-23 21:31:11 +00001409 llvm::SmallVector<llvm::Constant*, 4> CElts;
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001410
Nate Begeman3b8d1162008-05-13 21:03:02 +00001411 for (unsigned i = 0, e = Elts.size(); i != e; ++i)
Owen Anderson0032b272009-08-13 21:57:51 +00001412 CElts.push_back(llvm::ConstantInt::get(
1413 llvm::Type::getInt32Ty(VMContext), Elts[i]));
Nate Begeman3b8d1162008-05-13 21:03:02 +00001414
Owen Anderson4a289322009-07-28 21:22:35 +00001415 return llvm::ConstantVector::get(&CElts[0], CElts.size());
Nate Begeman3b8d1162008-05-13 21:03:02 +00001416}
1417
Chris Lattner349aaec2007-08-02 23:37:31 +00001418LValue CodeGenFunction::
Nate Begeman213541a2008-04-18 23:10:10 +00001419EmitExtVectorElementExpr(const ExtVectorElementExpr *E) {
Chris Lattner998eab12009-12-23 21:31:11 +00001420 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
1421
Chris Lattner349aaec2007-08-02 23:37:31 +00001422 // Emit the base vector as an l-value.
Chris Lattner73525de2009-02-16 21:11:58 +00001423 LValue Base;
1424
1425 // ExtVectorElementExpr's base can either be a vector or pointer to vector.
Chris Lattner998eab12009-12-23 21:31:11 +00001426 if (E->isArrow()) {
1427 // If it is a pointer to a vector, emit the address and form an lvalue with
1428 // it.
Chris Lattner2140e902009-02-16 22:14:05 +00001429 llvm::Value *Ptr = EmitScalarExpr(E->getBase());
Chris Lattner998eab12009-12-23 21:31:11 +00001430 const PointerType *PT = E->getBase()->getType()->getAs<PointerType>();
John McCall0953e762009-09-24 19:53:00 +00001431 Qualifiers Quals = MakeQualifiers(PT->getPointeeType());
1432 Quals.removeObjCGCAttr();
1433 Base = LValue::MakeAddr(Ptr, Quals);
Chris Lattner998eab12009-12-23 21:31:11 +00001434 } else if (E->getBase()->isLvalue(getContext()) == Expr::LV_Valid) {
1435 // Otherwise, if the base is an lvalue ( as in the case of foo.x.x),
1436 // emit the base as an lvalue.
1437 assert(E->getBase()->getType()->isVectorType());
1438 Base = EmitLValue(E->getBase());
1439 } else {
1440 // Otherwise, the base is a normal rvalue (as in (V+V).x), emit it as such.
Daniel Dunbar302c3c22010-01-04 18:02:28 +00001441 assert(E->getBase()->getType()->getAs<VectorType>() &&
1442 "Result must be a vector");
Chris Lattner998eab12009-12-23 21:31:11 +00001443 llvm::Value *Vec = EmitScalarExpr(E->getBase());
1444
Chris Lattner0ad57fb2009-12-23 21:33:41 +00001445 // Store the vector to memory (because LValue wants an address).
Daniel Dunbar195337d2010-02-09 02:48:28 +00001446 llvm::Value *VecMem = CreateMemTemp(E->getBase()->getType());
Chris Lattner998eab12009-12-23 21:31:11 +00001447 Builder.CreateStore(Vec, VecMem);
Chris Lattner0ad57fb2009-12-23 21:33:41 +00001448 Base = LValue::MakeAddr(VecMem, Qualifiers());
Chris Lattner998eab12009-12-23 21:31:11 +00001449 }
1450
Nate Begeman3b8d1162008-05-13 21:03:02 +00001451 // Encode the element access list into a vector of unsigned indices.
1452 llvm::SmallVector<unsigned, 4> Indices;
1453 E->getEncodedElementAccess(Indices);
1454
1455 if (Base.isSimple()) {
Owen Andersona1cf15f2009-07-14 23:10:40 +00001456 llvm::Constant *CV = GenerateConstantVector(VMContext, Indices);
Eli Friedman1e692ac2008-06-13 23:01:12 +00001457 return LValue::MakeExtVectorElt(Base.getAddress(), CV,
John McCall0953e762009-09-24 19:53:00 +00001458 Base.getVRQualifiers());
Nate Begeman3b8d1162008-05-13 21:03:02 +00001459 }
1460 assert(Base.isExtVectorElt() && "Can only subscript lvalue vec elts here!");
1461
1462 llvm::Constant *BaseElts = Base.getExtVectorElts();
1463 llvm::SmallVector<llvm::Constant *, 4> CElts;
1464
1465 for (unsigned i = 0, e = Indices.size(); i != e; ++i) {
1466 if (isa<llvm::ConstantAggregateZero>(BaseElts))
Chris Lattner67665862009-10-28 05:12:07 +00001467 CElts.push_back(llvm::ConstantInt::get(Int32Ty, 0));
Nate Begeman3b8d1162008-05-13 21:03:02 +00001468 else
Chris Lattner67665862009-10-28 05:12:07 +00001469 CElts.push_back(cast<llvm::Constant>(BaseElts->getOperand(Indices[i])));
Nate Begeman3b8d1162008-05-13 21:03:02 +00001470 }
Owen Anderson4a289322009-07-28 21:22:35 +00001471 llvm::Constant *CV = llvm::ConstantVector::get(&CElts[0], CElts.size());
Eli Friedman1e692ac2008-06-13 23:01:12 +00001472 return LValue::MakeExtVectorElt(Base.getExtVectorAddr(), CV,
John McCall0953e762009-09-24 19:53:00 +00001473 Base.getVRQualifiers());
Chris Lattner349aaec2007-08-02 23:37:31 +00001474}
1475
Devang Patelb9b00ad2007-10-23 20:28:39 +00001476LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) {
Fariborz Jahanian4f676ed2009-02-21 00:30:43 +00001477 bool isNonGC = false;
Devang Patel126a8562007-10-24 22:26:28 +00001478 Expr *BaseExpr = E->getBase();
Devang Patel126a8562007-10-24 22:26:28 +00001479 llvm::Value *BaseValue = NULL;
John McCall0953e762009-09-24 19:53:00 +00001480 Qualifiers BaseQuals;
Eli Friedman1e692ac2008-06-13 23:01:12 +00001481
Chris Lattner12f65f62007-12-02 18:52:07 +00001482 // If this is s.x, emit s as an lvalue. If it is s->x, emit s as a scalar.
Devang Patelfe2419a2007-12-11 21:33:16 +00001483 if (E->isArrow()) {
Devang Patel0a961182007-10-26 18:15:21 +00001484 BaseValue = EmitScalarExpr(BaseExpr);
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001485 const PointerType *PTy =
Ted Kremenek6217b802009-07-29 21:53:49 +00001486 BaseExpr->getType()->getAs<PointerType>();
John McCall0953e762009-09-24 19:53:00 +00001487 BaseQuals = PTy->getPointeeType().getQualifiers();
Fariborz Jahaniand2e1eb02009-09-01 17:02:21 +00001488 } else if (isa<ObjCPropertyRefExpr>(BaseExpr->IgnoreParens()) ||
1489 isa<ObjCImplicitSetterGetterRefExpr>(
1490 BaseExpr->IgnoreParens())) {
Fariborz Jahanian35c33292009-01-12 23:27:26 +00001491 RValue RV = EmitObjCPropertyGet(BaseExpr);
1492 BaseValue = RV.getAggregateAddr();
John McCall0953e762009-09-24 19:53:00 +00001493 BaseQuals = BaseExpr->getType().getQualifiers();
Chris Lattner1bd885e2009-02-16 22:25:49 +00001494 } else {
Chris Lattner12f65f62007-12-02 18:52:07 +00001495 LValue BaseLV = EmitLValue(BaseExpr);
Fariborz Jahanian4f676ed2009-02-21 00:30:43 +00001496 if (BaseLV.isNonGC())
1497 isNonGC = true;
Chris Lattner12f65f62007-12-02 18:52:07 +00001498 // FIXME: this isn't right for bitfields.
1499 BaseValue = BaseLV.getAddress();
Fariborz Jahaniana91d6a62009-07-29 00:44:13 +00001500 QualType BaseTy = BaseExpr->getType();
John McCall0953e762009-09-24 19:53:00 +00001501 BaseQuals = BaseTy.getQualifiers();
Chris Lattner12f65f62007-12-02 18:52:07 +00001502 }
Devang Patelb9b00ad2007-10-23 20:28:39 +00001503
Anders Carlssonce53f7d2009-11-07 23:06:58 +00001504 NamedDecl *ND = E->getMemberDecl();
1505 if (FieldDecl *Field = dyn_cast<FieldDecl>(ND)) {
Anders Carlssone6d2a532010-01-29 05:05:36 +00001506 LValue LV = EmitLValueForField(BaseValue, Field,
Anders Carlssonce53f7d2009-11-07 23:06:58 +00001507 BaseQuals.getCVRQualifiers());
1508 LValue::SetObjCNonGC(LV, isNonGC);
1509 setObjCGCLValueClass(getContext(), E, LV);
1510 return LV;
1511 }
1512
Anders Carlsson589f9e32009-11-07 23:16:50 +00001513 if (VarDecl *VD = dyn_cast<VarDecl>(ND))
1514 return EmitGlobalVarDeclLValue(*this, E, VD);
Eli Friedman9a146302009-11-26 06:08:14 +00001515
1516 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
1517 return EmitFunctionDeclLValue(*this, E, FD);
1518
Anders Carlssonce53f7d2009-11-07 23:06:58 +00001519 assert(false && "Unhandled member declaration!");
1520 return LValue();
Eli Friedman472778e2008-02-09 08:50:58 +00001521}
Devang Patelb9b00ad2007-10-23 20:28:39 +00001522
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +00001523LValue CodeGenFunction::EmitLValueForBitfield(llvm::Value* BaseValue,
Anders Carlsson0ed303c2009-11-17 03:57:07 +00001524 const FieldDecl* Field,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001525 unsigned CVRQualifiers) {
Daniel Dunbar198bcb42010-03-31 01:09:11 +00001526 const CGRecordLayout &RL =
1527 CGM.getTypes().getCGRecordLayout(Field->getParent());
Daniel Dunbar2eec0b22010-04-05 16:20:44 +00001528 const CGBitFieldInfo &Info = RL.getBitFieldInfo(Field);
Daniel Dunbar7f289642010-04-08 02:59:45 +00001529 return LValue::MakeBitfield(BaseValue, Info,
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +00001530 Field->getType().getCVRQualifiers()|CVRQualifiers);
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +00001531}
1532
Eli Friedman472778e2008-02-09 08:50:58 +00001533LValue CodeGenFunction::EmitLValueForField(llvm::Value* BaseValue,
Anders Carlsson0ed303c2009-11-17 03:57:07 +00001534 const FieldDecl* Field,
Mike Stump1eb44332009-09-09 15:08:12 +00001535 unsigned CVRQualifiers) {
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +00001536 if (Field->isBitField())
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001537 return EmitLValueForBitfield(BaseValue, Field, CVRQualifiers);
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001538
Daniel Dunbar198bcb42010-03-31 01:09:11 +00001539 const CGRecordLayout &RL =
1540 CGM.getTypes().getCGRecordLayout(Field->getParent());
1541 unsigned idx = RL.getLLVMFieldNo(Field);
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +00001542 llvm::Value *V = Builder.CreateStructGEP(BaseValue, idx, "tmp");
Eli Friedman1e86b342008-05-29 11:33:25 +00001543
Devang Patelabad06c2007-10-26 19:42:18 +00001544 // Match union field type.
Anders Carlssone6d2a532010-01-29 05:05:36 +00001545 if (Field->getParent()->isUnion()) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001546 const llvm::Type *FieldTy =
Eli Friedman1e692ac2008-06-13 23:01:12 +00001547 CGM.getTypes().ConvertTypeForMem(Field->getType());
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001548 const llvm::PointerType * BaseTy =
Devang Patele9b8c0a2007-10-30 20:59:40 +00001549 cast<llvm::PointerType>(BaseValue->getType());
Eli Friedman788d5712008-05-21 13:24:44 +00001550 unsigned AS = BaseTy->getAddressSpace();
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001551 V = Builder.CreateBitCast(V,
1552 llvm::PointerType::get(FieldTy, AS),
Eli Friedman788d5712008-05-21 13:24:44 +00001553 "tmp");
Devang Patelabad06c2007-10-26 19:42:18 +00001554 }
Eli Friedman2be58612009-05-30 21:09:44 +00001555 if (Field->getType()->isReferenceType())
1556 V = Builder.CreateLoad(V, "tmp");
John McCall0953e762009-09-24 19:53:00 +00001557
1558 Qualifiers Quals = MakeQualifiers(Field->getType());
1559 Quals.addCVRQualifiers(CVRQualifiers);
Fariborz Jahanianfd02ed72009-09-21 18:54:29 +00001560 // __weak attribute on a field is ignored.
John McCall0953e762009-09-24 19:53:00 +00001561 if (Quals.getObjCGCAttr() == Qualifiers::Weak)
1562 Quals.removeObjCGCAttr();
Fariborz Jahanianfd02ed72009-09-21 18:54:29 +00001563
John McCall0953e762009-09-24 19:53:00 +00001564 return LValue::MakeAddr(V, Quals);
Devang Patelb9b00ad2007-10-23 20:28:39 +00001565}
1566
Anders Carlsson06a29702010-01-29 05:24:29 +00001567LValue
1568CodeGenFunction::EmitLValueForFieldInitialization(llvm::Value* BaseValue,
1569 const FieldDecl* Field,
1570 unsigned CVRQualifiers) {
1571 QualType FieldType = Field->getType();
1572
1573 if (!FieldType->isReferenceType())
1574 return EmitLValueForField(BaseValue, Field, CVRQualifiers);
1575
Daniel Dunbar198bcb42010-03-31 01:09:11 +00001576 const CGRecordLayout &RL =
1577 CGM.getTypes().getCGRecordLayout(Field->getParent());
1578 unsigned idx = RL.getLLVMFieldNo(Field);
Anders Carlsson06a29702010-01-29 05:24:29 +00001579 llvm::Value *V = Builder.CreateStructGEP(BaseValue, idx, "tmp");
1580
1581 assert(!FieldType.getObjCGCAttr() && "fields cannot have GC attrs");
1582
1583 return LValue::MakeAddr(V, MakeQualifiers(FieldType));
1584}
1585
Chris Lattner75dfeda2009-03-18 18:28:57 +00001586LValue CodeGenFunction::EmitCompoundLiteralLValue(const CompoundLiteralExpr* E){
Daniel Dunbar15006572010-02-16 19:43:39 +00001587 llvm::Value *DeclPtr = CreateMemTemp(E->getType(), ".compoundliteral");
Eli Friedman06e863f2008-05-13 23:18:27 +00001588 const Expr* InitExpr = E->getInitializer();
John McCall0953e762009-09-24 19:53:00 +00001589 LValue Result = LValue::MakeAddr(DeclPtr, MakeQualifiers(E->getType()));
Eli Friedman06e863f2008-05-13 23:18:27 +00001590
John McCall3d3ec1c2010-04-21 10:05:39 +00001591 EmitAnyExprToMem(InitExpr, DeclPtr, /*Volatile*/ false);
Eli Friedman06e863f2008-05-13 23:18:27 +00001592
1593 return Result;
1594}
1595
Anders Carlsson6fcec8b2009-09-15 16:35:24 +00001596LValue
1597CodeGenFunction::EmitConditionalOperatorLValue(const ConditionalOperator* E) {
1598 if (E->isLvalue(getContext()) == Expr::LV_Valid) {
Eli Friedmanab189952009-12-25 05:29:40 +00001599 if (int Cond = ConstantFoldsToSimpleInteger(E->getCond())) {
1600 Expr *Live = Cond == 1 ? E->getLHS() : E->getRHS();
1601 if (Live)
1602 return EmitLValue(Live);
1603 }
1604
1605 if (!E->getLHS())
1606 return EmitUnsupportedLValue(E, "conditional operator with missing LHS");
1607
Anders Carlsson6fcec8b2009-09-15 16:35:24 +00001608 llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true");
1609 llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false");
1610 llvm::BasicBlock *ContBlock = createBasicBlock("cond.end");
1611
Eli Friedman8e274bd2009-12-25 06:17:05 +00001612 EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock);
Anders Carlsson6fcec8b2009-09-15 16:35:24 +00001613
Anders Carlssonc1b32f62010-02-04 17:26:01 +00001614 // Any temporaries created here are conditional.
1615 BeginConditionalBranch();
Anders Carlsson6fcec8b2009-09-15 16:35:24 +00001616 EmitBlock(LHSBlock);
Anders Carlsson6fcec8b2009-09-15 16:35:24 +00001617 LValue LHS = EmitLValue(E->getLHS());
Anders Carlssonc1b32f62010-02-04 17:26:01 +00001618 EndConditionalBranch();
1619
Anders Carlsson6fcec8b2009-09-15 16:35:24 +00001620 if (!LHS.isSimple())
1621 return EmitUnsupportedLValue(E, "conditional operator");
1622
Daniel Dunbar195337d2010-02-09 02:48:28 +00001623 // FIXME: We shouldn't need an alloca for this.
Chris Lattnereb99b012009-10-28 17:39:19 +00001624 llvm::Value *Temp = CreateTempAlloca(LHS.getAddress()->getType(),"condtmp");
Anders Carlsson6fcec8b2009-09-15 16:35:24 +00001625 Builder.CreateStore(LHS.getAddress(), Temp);
1626 EmitBranch(ContBlock);
1627
Anders Carlssonc1b32f62010-02-04 17:26:01 +00001628 // Any temporaries created here are conditional.
1629 BeginConditionalBranch();
Anders Carlsson6fcec8b2009-09-15 16:35:24 +00001630 EmitBlock(RHSBlock);
1631 LValue RHS = EmitLValue(E->getRHS());
Anders Carlssonc1b32f62010-02-04 17:26:01 +00001632 EndConditionalBranch();
Anders Carlsson6fcec8b2009-09-15 16:35:24 +00001633 if (!RHS.isSimple())
1634 return EmitUnsupportedLValue(E, "conditional operator");
1635
1636 Builder.CreateStore(RHS.getAddress(), Temp);
1637 EmitBranch(ContBlock);
1638
1639 EmitBlock(ContBlock);
1640
1641 Temp = Builder.CreateLoad(Temp, "lv");
John McCall0953e762009-09-24 19:53:00 +00001642 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
Anders Carlsson6fcec8b2009-09-15 16:35:24 +00001643 }
1644
Daniel Dunbar90345582009-03-24 02:38:23 +00001645 // ?: here should be an aggregate.
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001646 assert((hasAggregateLLVMType(E->getType()) &&
Daniel Dunbar90345582009-03-24 02:38:23 +00001647 !E->getType()->isAnyComplexType()) &&
1648 "Unexpected conditional operator!");
1649
Daniel Dunbar18aba0d2010-02-05 19:38:31 +00001650 return EmitAggExprToLValue(E);
Daniel Dunbar90345582009-03-24 02:38:23 +00001651}
1652
Mike Stumpc849c052009-11-16 06:50:58 +00001653/// EmitCastLValue - Casts are never lvalues unless that cast is a dynamic_cast.
1654/// If the cast is a dynamic_cast, we can have the usual lvalue result,
1655/// otherwise if a cast is needed by the code generator in an lvalue context,
1656/// then it must mean that we need the address of an aggregate in order to
1657/// access one of its fields. This can happen for all the reasons that casts
1658/// are permitted with aggregate result, including noop aggregate casts, and
1659/// cast from scalar to union.
Chris Lattner75dfeda2009-03-18 18:28:57 +00001660LValue CodeGenFunction::EmitCastLValue(const CastExpr *E) {
Anders Carlsson0ee33cf2009-09-12 16:16:49 +00001661 switch (E->getCastKind()) {
1662 default:
Eli Friedmaneaae78a2009-11-16 05:48:01 +00001663 return EmitUnsupportedLValue(E, "unexpected cast lvalue");
1664
Mike Stumpc849c052009-11-16 06:50:58 +00001665 case CastExpr::CK_Dynamic: {
1666 LValue LV = EmitLValue(E->getSubExpr());
1667 llvm::Value *V = LV.getAddress();
1668 const CXXDynamicCastExpr *DCE = cast<CXXDynamicCastExpr>(E);
1669 return LValue::MakeAddr(EmitDynamicCast(V, DCE),
1670 MakeQualifiers(E->getType()));
1671 }
1672
Fariborz Jahanianb3ebe942010-05-10 22:57:35 +00001673 case CastExpr::CK_NoOp: {
1674 LValue LV = EmitLValue(E->getSubExpr());
Fariborz Jahanianb3ebe942010-05-10 22:57:35 +00001675 if (LV.isPropertyRef()) {
Fariborz Jahanian84c580f2010-05-11 16:31:10 +00001676 QualType QT = E->getSubExpr()->getType();
1677 RValue RV = EmitLoadOfPropertyRefLValue(LV, QT);
1678 assert(!RV.isScalar() && "EmitCastLValue - scalar cast of property ref");
1679 llvm::Value *V = RV.getAggregateAddr();
1680 return LValue::MakeAddr(V, MakeQualifiers(QT));
Fariborz Jahanianb3ebe942010-05-10 22:57:35 +00001681 }
1682 return LV;
1683 }
Anders Carlsson0ee33cf2009-09-12 16:16:49 +00001684 case CastExpr::CK_ConstructorConversion:
1685 case CastExpr::CK_UserDefinedConversion:
Fariborz Jahaniana7fa7cd2009-12-15 21:34:52 +00001686 case CastExpr::CK_AnyPointerToObjCPointerCast:
Chris Lattner75dfeda2009-03-18 18:28:57 +00001687 return EmitLValue(E->getSubExpr());
Anders Carlsson0ee33cf2009-09-12 16:16:49 +00001688
John McCall23cba802010-03-30 23:58:03 +00001689 case CastExpr::CK_UncheckedDerivedToBase:
Anders Carlsson0ee33cf2009-09-12 16:16:49 +00001690 case CastExpr::CK_DerivedToBase: {
1691 const RecordType *DerivedClassTy =
1692 E->getSubExpr()->getType()->getAs<RecordType>();
1693 CXXRecordDecl *DerivedClassDecl =
1694 cast<CXXRecordDecl>(DerivedClassTy->getDecl());
Anders Carlsson0ee33cf2009-09-12 16:16:49 +00001695
1696 LValue LV = EmitLValue(E->getSubExpr());
1697
1698 // Perform the derived-to-base conversion
1699 llvm::Value *Base =
Anders Carlssona3697c92009-11-23 17:57:54 +00001700 GetAddressOfBaseClass(LV.getAddress(), DerivedClassDecl,
Anders Carlssonfc89c312010-04-24 21:12:55 +00001701 E->getBasePath(), /*NullCheckValue=*/false);
Anders Carlsson0ee33cf2009-09-12 16:16:49 +00001702
John McCall0953e762009-09-24 19:53:00 +00001703 return LValue::MakeAddr(Base, MakeQualifiers(E->getType()));
Anders Carlsson0ee33cf2009-09-12 16:16:49 +00001704 }
Daniel Dunbarb2cd7772010-02-05 20:02:42 +00001705 case CastExpr::CK_ToUnion:
1706 return EmitAggExprToLValue(E);
Eli Friedmaneaae78a2009-11-16 05:48:01 +00001707 case CastExpr::CK_BaseToDerived: {
Anders Carlssona3697c92009-11-23 17:57:54 +00001708 const RecordType *DerivedClassTy = E->getType()->getAs<RecordType>();
1709 CXXRecordDecl *DerivedClassDecl =
1710 cast<CXXRecordDecl>(DerivedClassTy->getDecl());
1711
1712 LValue LV = EmitLValue(E->getSubExpr());
1713
1714 // Perform the base-to-derived conversion
1715 llvm::Value *Derived =
Anders Carlssona04efdf2010-04-24 21:23:59 +00001716 GetAddressOfDerivedClass(LV.getAddress(), DerivedClassDecl,
1717 E->getBasePath(),/*NullCheckValue=*/false);
Anders Carlssona3697c92009-11-23 17:57:54 +00001718
1719 return LValue::MakeAddr(Derived, MakeQualifiers(E->getType()));
Eli Friedmaneaae78a2009-11-16 05:48:01 +00001720 }
Anders Carlsson658e8122009-11-14 21:21:42 +00001721 case CastExpr::CK_BitCast: {
Eli Friedmaneaae78a2009-11-16 05:48:01 +00001722 // This must be a reinterpret_cast (or c-style equivalent).
1723 const ExplicitCastExpr *CE = cast<ExplicitCastExpr>(E);
Anders Carlsson658e8122009-11-14 21:21:42 +00001724
1725 LValue LV = EmitLValue(E->getSubExpr());
1726 llvm::Value *V = Builder.CreateBitCast(LV.getAddress(),
1727 ConvertType(CE->getTypeAsWritten()));
1728 return LValue::MakeAddr(V, MakeQualifiers(E->getType()));
1729 }
Anders Carlsson0ee33cf2009-09-12 16:16:49 +00001730 }
Chris Lattner75dfeda2009-03-18 18:28:57 +00001731}
1732
Fariborz Jahanian48620ba2009-10-20 23:29:04 +00001733LValue CodeGenFunction::EmitNullInitializationLValue(
1734 const CXXZeroInitValueExpr *E) {
1735 QualType Ty = E->getType();
Daniel Dunbar195337d2010-02-09 02:48:28 +00001736 LValue LV = LValue::MakeAddr(CreateMemTemp(Ty), MakeQualifiers(Ty));
1737 EmitMemSetToZero(LV.getAddress(), Ty);
1738 return LV;
Fariborz Jahanian48620ba2009-10-20 23:29:04 +00001739}
1740
Reid Spencer5f016e22007-07-11 17:01:13 +00001741//===--------------------------------------------------------------------===//
1742// Expression Emission
1743//===--------------------------------------------------------------------===//
1744
Chris Lattner7016a702007-08-20 22:37:10 +00001745
Anders Carlssond2490a92009-12-24 20:40:36 +00001746RValue CodeGenFunction::EmitCallExpr(const CallExpr *E,
1747 ReturnValueSlot ReturnValue) {
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +00001748 // Builtins never have block type.
Daniel Dunbarce1d38b2009-01-09 16:50:52 +00001749 if (E->getCallee()->getType()->isBlockPointerType())
Anders Carlssona1736c02009-12-24 21:13:40 +00001750 return EmitBlockCallExpr(E, ReturnValue);
Daniel Dunbarce1d38b2009-01-09 16:50:52 +00001751
Anders Carlsson774e7c62009-04-03 22:50:24 +00001752 if (const CXXMemberCallExpr *CE = dyn_cast<CXXMemberCallExpr>(E))
Anders Carlssona1736c02009-12-24 21:13:40 +00001753 return EmitCXXMemberCallExpr(CE, ReturnValue);
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001754
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +00001755 const Decl *TargetDecl = 0;
Daniel Dunbardd7b8972009-02-20 19:34:33 +00001756 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E->getCallee())) {
1757 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CE->getSubExpr())) {
1758 TargetDecl = DRE->getDecl();
1759 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(TargetDecl))
Douglas Gregor7814e6d2009-09-12 00:22:50 +00001760 if (unsigned builtinID = FD->getBuiltinID())
Daniel Dunbardd7b8972009-02-20 19:34:33 +00001761 return EmitBuiltinExpr(FD, builtinID, E);
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +00001762 }
1763 }
1764
Chris Lattner5db7ae52009-06-13 00:26:38 +00001765 if (const CXXOperatorCallExpr *CE = dyn_cast<CXXOperatorCallExpr>(E))
Anders Carlsson0f294632009-05-27 04:18:27 +00001766 if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(TargetDecl))
Anders Carlssona1736c02009-12-24 21:13:40 +00001767 return EmitCXXOperatorMemberCallExpr(CE, MD, ReturnValue);
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001768
Eli Friedmanc4451db2009-12-08 02:09:46 +00001769 if (isa<CXXPseudoDestructorExpr>(E->getCallee()->IgnoreParens())) {
Douglas Gregora71d8192009-09-04 17:36:40 +00001770 // C++ [expr.pseudo]p1:
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001771 // The result shall only be used as the operand for the function call
Douglas Gregora71d8192009-09-04 17:36:40 +00001772 // operator (), and the result of such a call has type void. The only
1773 // effect is the evaluation of the postfix-expression before the dot or
1774 // arrow.
1775 EmitScalarExpr(E->getCallee());
1776 return RValue::get(0);
1777 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001778
Chris Lattner7f02f722007-08-24 05:35:26 +00001779 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
Anders Carlssond2490a92009-12-24 20:40:36 +00001780 return EmitCall(E->getCallee()->getType(), Callee, ReturnValue,
Anders Carlsson98647712009-05-27 01:22:39 +00001781 E->arg_begin(), E->arg_end(), TargetDecl);
Chris Lattnerc5e940f2007-08-31 04:44:06 +00001782}
1783
Daniel Dunbar80e62c22008-09-04 03:20:13 +00001784LValue CodeGenFunction::EmitBinaryOperatorLValue(const BinaryOperator *E) {
Chris Lattner7a574cc2009-05-12 21:28:12 +00001785 // Comma expressions just emit their LHS then their RHS as an l-value.
1786 if (E->getOpcode() == BinaryOperator::Comma) {
1787 EmitAnyExpr(E->getLHS());
Eli Friedman130c69e2009-12-07 20:18:11 +00001788 EnsureInsertPoint();
Chris Lattner7a574cc2009-05-12 21:28:12 +00001789 return EmitLValue(E->getRHS());
1790 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001791
Fariborz Jahanian52f08bc2009-10-26 21:58:25 +00001792 if (E->getOpcode() == BinaryOperator::PtrMemD ||
1793 E->getOpcode() == BinaryOperator::PtrMemI)
Fariborz Jahanian8bfd31f2009-10-22 22:57:31 +00001794 return EmitPointerToDataMemberBinaryExpr(E);
1795
Daniel Dunbar80e62c22008-09-04 03:20:13 +00001796 // Can only get l-value for binary operator expressions which are a
1797 // simple assignment of aggregate type.
1798 if (E->getOpcode() != BinaryOperator::Assign)
1799 return EmitUnsupportedLValue(E, "binary l-value expression");
1800
Anders Carlsson86aa0cd2009-10-19 18:28:22 +00001801 if (!hasAggregateLLVMType(E->getType())) {
1802 // Emit the LHS as an l-value.
1803 LValue LV = EmitLValue(E->getLHS());
1804
1805 llvm::Value *RHS = EmitScalarExpr(E->getRHS());
1806 EmitStoreOfScalar(RHS, LV.getAddress(), LV.isVolatileQualified(),
1807 E->getType());
1808 return LV;
1809 }
1810
Daniel Dunbar18aba0d2010-02-05 19:38:31 +00001811 return EmitAggExprToLValue(E);
Daniel Dunbar80e62c22008-09-04 03:20:13 +00001812}
1813
Christopher Lamb22c940e2007-12-29 05:02:41 +00001814LValue CodeGenFunction::EmitCallExprLValue(const CallExpr *E) {
Christopher Lamb22c940e2007-12-29 05:02:41 +00001815 RValue RV = EmitCallExpr(E);
Anders Carlsson48265682009-05-27 01:45:47 +00001816
Chris Lattnereb99b012009-10-28 17:39:19 +00001817 if (!RV.isScalar())
1818 return LValue::MakeAddr(RV.getAggregateAddr(),MakeQualifiers(E->getType()));
1819
1820 assert(E->getCallReturnType()->isReferenceType() &&
1821 "Can't have a scalar return unless the return type is a "
1822 "reference type!");
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001823
Chris Lattnereb99b012009-10-28 17:39:19 +00001824 return LValue::MakeAddr(RV.getScalarVal(), MakeQualifiers(E->getType()));
Christopher Lamb22c940e2007-12-29 05:02:41 +00001825}
1826
Daniel Dunbar5b5c9ef2009-02-11 20:59:32 +00001827LValue CodeGenFunction::EmitVAArgExprLValue(const VAArgExpr *E) {
1828 // FIXME: This shouldn't require another copy.
Daniel Dunbar18aba0d2010-02-05 19:38:31 +00001829 return EmitAggExprToLValue(E);
Daniel Dunbar5b5c9ef2009-02-11 20:59:32 +00001830}
1831
Anders Carlssonb58d0172009-05-30 23:23:33 +00001832LValue CodeGenFunction::EmitCXXConstructLValue(const CXXConstructExpr *E) {
Daniel Dunbar195337d2010-02-09 02:48:28 +00001833 llvm::Value *Temp = CreateMemTemp(E->getType(), "tmp");
Anders Carlssonb58d0172009-05-30 23:23:33 +00001834 EmitCXXConstructExpr(Temp, E);
John McCall0953e762009-09-24 19:53:00 +00001835 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
Anders Carlssonb58d0172009-05-30 23:23:33 +00001836}
1837
Anders Carlssone61c9e82009-05-30 23:30:54 +00001838LValue
Mike Stumpc2e84ae2009-11-15 08:09:41 +00001839CodeGenFunction::EmitCXXTypeidLValue(const CXXTypeidExpr *E) {
1840 llvm::Value *Temp = EmitCXXTypeidExpr(E);
1841 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
1842}
1843
1844LValue
Anders Carlssone61c9e82009-05-30 23:30:54 +00001845CodeGenFunction::EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr *E) {
1846 LValue LV = EmitLValue(E->getSubExpr());
Anders Carlsson543ac0c2009-05-31 00:34:10 +00001847 PushCXXTemporary(E->getTemporary(), LV.getAddress());
Anders Carlssone61c9e82009-05-30 23:30:54 +00001848 return LV;
1849}
1850
Daniel Dunbar0a04d772008-08-23 10:51:21 +00001851LValue CodeGenFunction::EmitObjCMessageExprLValue(const ObjCMessageExpr *E) {
1852 // Can only get l-value for message expression returning aggregate type
1853 RValue RV = EmitObjCMessageExpr(E);
1854 // FIXME: can this be volatile?
John McCall0953e762009-09-24 19:53:00 +00001855 return LValue::MakeAddr(RV.getAggregateAddr(), MakeQualifiers(E->getType()));
Daniel Dunbar0a04d772008-08-23 10:51:21 +00001856}
1857
Daniel Dunbar2a031922009-04-22 05:08:15 +00001858llvm::Value *CodeGenFunction::EmitIvarOffset(const ObjCInterfaceDecl *Interface,
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +00001859 const ObjCIvarDecl *Ivar) {
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001860 return CGM.getObjCRuntime().EmitIvarOffset(*this, Interface, Ivar);
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +00001861}
1862
Fariborz Jahanian45012a72009-02-03 00:09:52 +00001863LValue CodeGenFunction::EmitLValueForIvar(QualType ObjectTy,
1864 llvm::Value *BaseValue,
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +00001865 const ObjCIvarDecl *Ivar,
1866 unsigned CVRQualifiers) {
Chris Lattner26f074b2009-04-17 17:44:48 +00001867 return CGM.getObjCRuntime().EmitObjCValueForIvar(*this, ObjectTy, BaseValue,
Daniel Dunbar525c9b72009-04-21 01:19:28 +00001868 Ivar, CVRQualifiers);
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +00001869}
1870
1871LValue CodeGenFunction::EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E) {
Anders Carlsson29b7e502008-08-25 01:53:23 +00001872 // FIXME: A lot of the code below could be shared with EmitMemberExpr.
1873 llvm::Value *BaseValue = 0;
1874 const Expr *BaseExpr = E->getBase();
John McCall0953e762009-09-24 19:53:00 +00001875 Qualifiers BaseQuals;
Fariborz Jahanian45012a72009-02-03 00:09:52 +00001876 QualType ObjectTy;
Anders Carlsson29b7e502008-08-25 01:53:23 +00001877 if (E->isArrow()) {
1878 BaseValue = EmitScalarExpr(BaseExpr);
Steve Naroff14108da2009-07-10 23:34:53 +00001879 ObjectTy = BaseExpr->getType()->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00001880 BaseQuals = ObjectTy.getQualifiers();
Anders Carlsson29b7e502008-08-25 01:53:23 +00001881 } else {
1882 LValue BaseLV = EmitLValue(BaseExpr);
1883 // FIXME: this isn't right for bitfields.
1884 BaseValue = BaseLV.getAddress();
Fariborz Jahanian45012a72009-02-03 00:09:52 +00001885 ObjectTy = BaseExpr->getType();
John McCall0953e762009-09-24 19:53:00 +00001886 BaseQuals = ObjectTy.getQualifiers();
Anders Carlsson29b7e502008-08-25 01:53:23 +00001887 }
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +00001888
Fariborz Jahaniandbf3cfd2009-09-16 23:11:23 +00001889 LValue LV =
John McCall0953e762009-09-24 19:53:00 +00001890 EmitLValueForIvar(ObjectTy, BaseValue, E->getDecl(),
1891 BaseQuals.getCVRQualifiers());
Fariborz Jahaniandbf3cfd2009-09-16 23:11:23 +00001892 setObjCGCLValueClass(getContext(), E, LV);
1893 return LV;
Chris Lattner391d77a2008-03-30 23:03:07 +00001894}
1895
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001896LValue
Daniel Dunbar85c59ed2008-08-29 08:11:39 +00001897CodeGenFunction::EmitObjCPropertyRefLValue(const ObjCPropertyRefExpr *E) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001898 // This is a special l-value that just issues sends when we load or store
1899 // through it.
Daniel Dunbar85c59ed2008-08-29 08:11:39 +00001900 return LValue::MakePropertyRef(E, E->getType().getCVRQualifiers());
1901}
1902
Chris Lattnereb99b012009-10-28 17:39:19 +00001903LValue CodeGenFunction::EmitObjCKVCRefLValue(
Fariborz Jahanian09105f52009-08-20 17:02:02 +00001904 const ObjCImplicitSetterGetterRefExpr *E) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001905 // This is a special l-value that just issues sends when we load or store
1906 // through it.
Fariborz Jahanian43f44702008-11-22 22:30:21 +00001907 return LValue::MakeKVCRef(E, E->getType().getCVRQualifiers());
1908}
1909
Chris Lattnereb99b012009-10-28 17:39:19 +00001910LValue CodeGenFunction::EmitObjCSuperExprLValue(const ObjCSuperExpr *E) {
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00001911 return EmitUnsupportedLValue(E, "use of super");
1912}
1913
Chris Lattner65459942009-04-25 19:35:26 +00001914LValue CodeGenFunction::EmitStmtExprLValue(const StmtExpr *E) {
Chris Lattner65459942009-04-25 19:35:26 +00001915 // Can only get l-value for message expression returning aggregate type
1916 RValue RV = EmitAnyExprToTemp(E);
John McCall0953e762009-09-24 19:53:00 +00001917 return LValue::MakeAddr(RV.getAggregateAddr(), MakeQualifiers(E->getType()));
Chris Lattner65459942009-04-25 19:35:26 +00001918}
1919
Anders Carlsson31777a22009-12-24 19:08:58 +00001920RValue CodeGenFunction::EmitCall(QualType CalleeType, llvm::Value *Callee,
Anders Carlssond2490a92009-12-24 20:40:36 +00001921 ReturnValueSlot ReturnValue,
Anders Carlsson98647712009-05-27 01:22:39 +00001922 CallExpr::const_arg_iterator ArgBeg,
1923 CallExpr::const_arg_iterator ArgEnd,
1924 const Decl *TargetDecl) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001925 // Get the actual function type. The callee type will always be a pointer to
1926 // function type or a block pointer type.
1927 assert(CalleeType->isFunctionPointerType() &&
Anders Carlsson8ac67a72009-04-07 18:53:02 +00001928 "Call must have function pointer type!");
1929
John McCall00a1ad92009-10-23 08:22:42 +00001930 CalleeType = getContext().getCanonicalType(CalleeType);
1931
John McCall04a67a62010-02-05 21:31:56 +00001932 const FunctionType *FnType
1933 = cast<FunctionType>(cast<PointerType>(CalleeType)->getPointeeType());
1934 QualType ResultType = FnType->getResultType();
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001935
1936 CallArgList Args;
John McCall00a1ad92009-10-23 08:22:42 +00001937 EmitCallArgs(Args, dyn_cast<FunctionProtoType>(FnType), ArgBeg, ArgEnd);
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001938
John McCall04a67a62010-02-05 21:31:56 +00001939 return EmitCall(CGM.getTypes().getFunctionInfo(Args, FnType),
Anders Carlssond2490a92009-12-24 20:40:36 +00001940 Callee, ReturnValue, Args, TargetDecl);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001941}
Fariborz Jahanian8bfd31f2009-10-22 22:57:31 +00001942
Chris Lattnereb99b012009-10-28 17:39:19 +00001943LValue CodeGenFunction::
1944EmitPointerToDataMemberBinaryExpr(const BinaryOperator *E) {
Eli Friedman1c5c1a02009-11-18 05:01:17 +00001945 llvm::Value *BaseV;
Fariborz Jahanian52f08bc2009-10-26 21:58:25 +00001946 if (E->getOpcode() == BinaryOperator::PtrMemI)
Eli Friedman1c5c1a02009-11-18 05:01:17 +00001947 BaseV = EmitScalarExpr(E->getLHS());
1948 else
1949 BaseV = EmitLValue(E->getLHS()).getAddress();
Fariborz Jahanian8bfd31f2009-10-22 22:57:31 +00001950 const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(getLLVMContext());
1951 BaseV = Builder.CreateBitCast(BaseV, i8Ty);
Eli Friedman1c5c1a02009-11-18 05:01:17 +00001952 llvm::Value *OffsetV = EmitScalarExpr(E->getRHS());
Fariborz Jahanian8bfd31f2009-10-22 22:57:31 +00001953 llvm::Value *AddV = Builder.CreateInBoundsGEP(BaseV, OffsetV, "add.ptr");
Chris Lattnereb99b012009-10-28 17:39:19 +00001954
Fariborz Jahanian8bfd31f2009-10-22 22:57:31 +00001955 QualType Ty = E->getRHS()->getType();
Chris Lattnereb99b012009-10-28 17:39:19 +00001956 Ty = Ty->getAs<MemberPointerType>()->getPointeeType();
1957
1958 const llvm::Type *PType = ConvertType(getContext().getPointerType(Ty));
Fariborz Jahanian8bfd31f2009-10-22 22:57:31 +00001959 AddV = Builder.CreateBitCast(AddV, PType);
Chris Lattnereb99b012009-10-28 17:39:19 +00001960 return LValue::MakeAddr(AddV, MakeQualifiers(Ty));
Fariborz Jahanian8bfd31f2009-10-22 22:57:31 +00001961}
1962