blob: 085113edf57b340bcd7552445b45f5f0c8b9f75b [file] [log] [blame]
Chris Lattnere47e4402007-06-01 18:02:12 +00001//===--- CGExpr.cpp - Emit LLVM Code from Expressions ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnere47e4402007-06-01 18:02:12 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit Expr nodes as LLVM code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenFunction.h"
Chris Lattnerb6984c42007-06-20 04:44:43 +000015#include "CodeGenModule.h"
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000016#include "CGCall.h"
Daniel Dunbar034299e2010-03-31 01:09:11 +000017#include "CGRecordLayout.h"
Daniel Dunbar89da6ad2008-08-13 00:59:25 +000018#include "CGObjCRuntime.h"
Daniel Dunbarad319a72008-08-11 05:00:27 +000019#include "clang/AST/ASTContext.h"
Daniel Dunbar6e8aa532008-08-11 05:35:13 +000020#include "clang/AST/DeclObjC.h"
Mike Stumpe8c3b3e2009-12-15 00:35:12 +000021#include "llvm/Intrinsics.h"
Mike Stump9a4e0122009-12-15 00:59:40 +000022#include "clang/CodeGen/CodeGenOptions.h"
Eli Friedmanf2442dc2008-05-17 20:03:47 +000023#include "llvm/Target/TargetData.h"
Chris Lattnere47e4402007-06-01 18:02:12 +000024using namespace clang;
25using namespace CodeGen;
26
Chris Lattnerd7f58862007-06-02 05:24:33 +000027//===--------------------------------------------------------------------===//
Chris Lattnerf0106d22007-06-02 19:33:17 +000028// Miscellaneous Helper Methods
29//===--------------------------------------------------------------------===//
30
Chris Lattnere9a64532007-06-22 21:44:33 +000031/// CreateTempAlloca - This creates a alloca and inserts it into the entry
32/// block.
33llvm::AllocaInst *CodeGenFunction::CreateTempAlloca(const llvm::Type *Ty,
Daniel Dunbarb5aacc22009-10-19 01:21:05 +000034 const llvm::Twine &Name) {
Chris Lattner47640222009-03-22 00:24:14 +000035 if (!Builder.isNamePreserving())
Daniel Dunbarb5aacc22009-10-19 01:21:05 +000036 return new llvm::AllocaInst(Ty, 0, "", AllocaInsertPt);
Devang Pateldac79de2009-10-12 22:29:02 +000037 return new llvm::AllocaInst(Ty, 0, Name, AllocaInsertPt);
Chris Lattnere9a64532007-06-22 21:44:33 +000038}
Chris Lattner8394d792007-06-05 20:53:16 +000039
John McCall2e6567a2010-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 Dunbard0049182010-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 Dunbara7566f12010-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
Chris Lattner8394d792007-06-05 20:53:16 +000065/// EvaluateExprAsBool - Perform the usual unary conversions on the specified
66/// expression and compare the result against zero, returning an Int1Ty value.
Chris Lattner23b7eb62007-06-15 23:05:46 +000067llvm::Value *CodeGenFunction::EvaluateExprAsBool(const Expr *E) {
Chris Lattner268fcce2007-08-26 16:46:58 +000068 QualType BoolTy = getContext().BoolTy;
Eli Friedman68396b12009-12-11 09:26:29 +000069 if (E->getType()->isMemberFunctionPointerType()) {
Daniel Dunbard0bc7b92010-02-05 19:38:31 +000070 LValue LV = EmitAggExprToLValue(E);
Eli Friedman68396b12009-12-11 09:26:29 +000071
72 // Get the pointer.
Daniel Dunbard0bc7b92010-02-05 19:38:31 +000073 llvm::Value *FuncPtr = Builder.CreateStructGEP(LV.getAddress(), 0,
74 "src.ptr");
Eli Friedman68396b12009-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 Lattnerf3bc75a2008-04-04 16:54:41 +000084 if (!E->getType()->isAnyComplexType())
Chris Lattner268fcce2007-08-26 16:46:58 +000085 return EmitScalarConversion(EmitScalarExpr(E), E->getType(), BoolTy);
Chris Lattner8394d792007-06-05 20:53:16 +000086
Chris Lattner268fcce2007-08-26 16:46:58 +000087 return EmitComplexToScalarConversion(EmitComplexExpr(E), E->getType(),BoolTy);
Chris Lattnerf0106d22007-06-02 19:33:17 +000088}
89
Chris Lattner4647a212007-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 Stump4a3999f2009-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 Carlsson5b106a72009-08-16 07:36:22 +000095 bool IsAggLocVolatile, bool IgnoreResult,
96 bool IsInitializer) {
Chris Lattner4647a212007-08-31 22:49:20 +000097 if (!hasAggregateLLVMType(E->getType()))
Mike Stumpdf0fe272009-05-29 15:46:01 +000098 return RValue::get(EmitScalarExpr(E, IgnoreResult));
Chris Lattnerf3bc75a2008-04-04 16:54:41 +000099 else if (E->getType()->isAnyComplexType())
Mike Stumpdf0fe272009-05-29 15:46:01 +0000100 return RValue::getComplex(EmitComplexExpr(E, false, false,
101 IgnoreResult, IgnoreResult));
Mike Stump4a3999f2009-09-09 13:00:44 +0000102
Anders Carlsson5b106a72009-08-16 07:36:22 +0000103 EmitAggExpr(E, AggLoc, IsAggLocVolatile, IgnoreResult, IsInitializer);
104 return RValue::getAggregate(AggLoc, IsAggLocVolatile);
Chris Lattner4647a212007-08-31 22:49:20 +0000105}
106
Mike Stump4a3999f2009-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 Carlsson5b106a72009-08-16 07:36:22 +0000110 bool IsAggLocVolatile,
111 bool IsInitializer) {
112 llvm::Value *AggLoc = 0;
Mike Stump4a3999f2009-09-09 13:00:44 +0000113
114 if (hasAggregateLLVMType(E->getType()) &&
Daniel Dunbar41cf9de2008-09-09 01:06:48 +0000115 !E->getType()->isAnyComplexType())
John McCall7538eec2010-02-15 01:23:36 +0000116 AggLoc = CreateMemTemp(E->getType(), "agg.tmp");
Mike Stump4a3999f2009-09-09 13:00:44 +0000117 return EmitAnyExpr(E, AggLoc, IsAggLocVolatile, /*IgnoreResult=*/false,
Anders Carlsson5b106a72009-08-16 07:36:22 +0000118 IsInitializer);
Daniel Dunbar41cf9de2008-09-09 01:06:48 +0000119}
120
John McCall21886962010-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 Carlsson6f5a0152009-05-20 00:24:07 +0000138RValue CodeGenFunction::EmitReferenceBindingToExpr(const Expr* E,
Anders Carlsson5b106a72009-08-16 07:36:22 +0000139 bool IsInitializer) {
Anders Carlsson69c2c4b2009-10-18 23:09:21 +0000140 bool ShouldDestroyTemporaries = false;
141 unsigned OldNumLiveTemporaries = 0;
Eli Friedman357e8c92009-12-19 00:20:10 +0000142
143 if (const CXXDefaultArgExpr *DAE = dyn_cast<CXXDefaultArgExpr>(E))
144 E = DAE->getExpr();
145
Anders Carlsson66413c22009-10-15 00:51:46 +0000146 if (const CXXExprWithTemporaries *TE = dyn_cast<CXXExprWithTemporaries>(E)) {
Anders Carlsson6e997b22009-12-15 20:51:39 +0000147 ShouldDestroyTemporaries = true;
148
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000149 // Keep track of the current cleanup stack depth.
Anders Carlsson6e997b22009-12-15 20:51:39 +0000150 OldNumLiveTemporaries = LiveTemporaries.size();
Anders Carlsson66413c22009-10-15 00:51:46 +0000151
Anders Carlsson69c2c4b2009-10-18 23:09:21 +0000152 E = TE->getSubExpr();
Anders Carlsson66413c22009-10-15 00:51:46 +0000153 }
154
Eli Friedmanc21cb442009-05-20 02:31:19 +0000155 RValue Val;
156 if (E->isLvalue(getContext()) == Expr::LV_Valid) {
Anders Carlsson7d4c0832009-05-20 00:36:58 +0000157 // Emit the expr as an lvalue.
158 LValue LV = EmitLValue(E);
Anders Carlsson824e0612010-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 Friedmanc21cb442009-05-20 02:31:19 +0000166 return RValue::get(LV.getAddress());
Anders Carlsson824e0612010-02-04 17:32:58 +0000167 }
168
Eli Friedmanc21cb442009-05-20 02:31:19 +0000169 Val = EmitLoadOfLValue(LV, E->getType());
Anders Carlsson69c2c4b2009-10-18 23:09:21 +0000170
171 if (ShouldDestroyTemporaries) {
172 // Pop temporaries.
173 while (LiveTemporaries.size() > OldNumLiveTemporaries)
174 PopCXXTemporary();
175 }
Eli Friedmanc21cb442009-05-20 02:31:19 +0000176 } else {
Anders Carlsson66413c22009-10-15 00:51:46 +0000177 const CXXRecordDecl *BaseClassDecl = 0;
178 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();
184
185 BaseClassDecl =
186 cast<CXXRecordDecl>(CE->getType()->getAs<RecordType>()->getDecl());
187 DerivedClassDecl =
188 cast<CXXRecordDecl>(E->getType()->getAs<RecordType>()->getDecl());
189 }
190 }
191
Anders Carlsson5b106a72009-08-16 07:36:22 +0000192 Val = EmitAnyExprToTemp(E, /*IsAggLocVolatile=*/false,
193 IsInitializer);
Mike Stump4a3999f2009-09-09 13:00:44 +0000194
Anders Carlsson69c2c4b2009-10-18 23:09:21 +0000195 if (ShouldDestroyTemporaries) {
196 // Pop temporaries.
197 while (LiveTemporaries.size() > OldNumLiveTemporaries)
198 PopCXXTemporary();
199 }
200
Anders Carlsson3b848942009-08-16 17:54:29 +0000201 if (IsInitializer) {
202 // We might have to destroy the temporary variable.
203 if (const RecordType *RT = E->getType()->getAs<RecordType>()) {
204 if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
205 if (!ClassDecl->hasTrivialDestructor()) {
Mike Stump4a3999f2009-09-09 13:00:44 +0000206 const CXXDestructorDecl *Dtor =
Anders Carlsson3b848942009-08-16 17:54:29 +0000207 ClassDecl->getDestructor(getContext());
Mike Stump4a3999f2009-09-09 13:00:44 +0000208
Mike Stumpaff69af2009-12-09 03:35:49 +0000209 {
Anders Carlsson0c6a7d82009-12-11 01:00:09 +0000210 DelayedCleanupBlock Scope(*this);
Mike Stumpaff69af2009-12-09 03:35:49 +0000211 EmitCXXDestructorCall(Dtor, Dtor_Complete,
212 Val.getAggregateAddr());
Anders Carlsson0c6a7d82009-12-11 01:00:09 +0000213
214 // Make sure to jump to the exit block.
215 EmitBranch(Scope.getCleanupExitBlock());
Mike Stumpaff69af2009-12-09 03:35:49 +0000216 }
217 if (Exceptions) {
218 EHCleanupBlock Cleanup(*this);
219 EmitCXXDestructorCall(Dtor, Dtor_Complete,
220 Val.getAggregateAddr());
221 }
Anders Carlsson3b848942009-08-16 17:54:29 +0000222 }
Anders Carlssonb80760b2009-08-16 17:50:25 +0000223 }
224 }
225 }
Anders Carlsson66413c22009-10-15 00:51:46 +0000226
227 // Check if need to perform the derived-to-base cast.
228 if (BaseClassDecl) {
229 llvm::Value *Derived = Val.getAggregateAddr();
Anders Carlsson66413c22009-10-15 00:51:46 +0000230 llvm::Value *Base =
Anders Carlsson8c793172009-11-23 17:57:54 +0000231 GetAddressOfBaseClass(Derived, DerivedClassDecl, BaseClassDecl,
232 /*NullCheckValue=*/false);
Anders Carlsson66413c22009-10-15 00:51:46 +0000233 return RValue::get(Base);
234 }
Anders Carlsson7d4c0832009-05-20 00:36:58 +0000235 }
Eli Friedmanc21cb442009-05-20 02:31:19 +0000236
237 if (Val.isAggregate()) {
238 Val = RValue::get(Val.getAggregateAddr());
239 } else {
Anders Carlsson02bb7f02009-05-20 01:35:03 +0000240 // Create a temporary variable that we can bind the reference to.
Daniel Dunbara7566f12010-02-09 02:48:28 +0000241 llvm::Value *Temp = CreateMemTemp(E->getType(), "reftmp");
Eli Friedmanc21cb442009-05-20 02:31:19 +0000242 if (Val.isScalar())
243 EmitStoreOfScalar(Val.getScalarVal(), Temp, false, E->getType());
244 else
245 StoreComplexToAddr(Val.getComplexVal(), Temp, false);
246 Val = RValue::get(Temp);
Anders Carlsson145eae52009-05-20 01:03:17 +0000247 }
Eli Friedmanc21cb442009-05-20 02:31:19 +0000248
249 return Val;
Anders Carlsson6f5a0152009-05-20 00:24:07 +0000250}
251
252
Mike Stump4a3999f2009-09-09 13:00:44 +0000253/// getAccessedFieldNo - Given an encoded value and a result number, return the
254/// input field number being accessed.
255unsigned CodeGenFunction::getAccessedFieldNo(unsigned Idx,
Dan Gohman75d69da2008-05-22 00:50:06 +0000256 const llvm::Constant *Elts) {
257 if (isa<llvm::ConstantAggregateZero>(Elts))
258 return 0;
Mike Stump4a3999f2009-09-09 13:00:44 +0000259
Dan Gohman75d69da2008-05-22 00:50:06 +0000260 return cast<llvm::ConstantInt>(Elts->getOperand(Idx))->getZExtValue();
261}
262
Mike Stump3f6f9fe2009-12-16 02:57:00 +0000263void CodeGenFunction::EmitCheck(llvm::Value *Address, unsigned Size) {
264 if (!CatchUndefined)
265 return;
266
Chris Lattnerbc3be652010-04-10 18:34:14 +0000267 const llvm::Type *Size_tTy
Mike Stump3f6f9fe2009-12-16 02:57:00 +0000268 = llvm::IntegerType::get(VMContext, LLVMPointerWidth);
269 Address = Builder.CreateBitCast(Address, PtrToInt8Ty);
270
Chris Lattnerbc3be652010-04-10 18:34:14 +0000271 llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::objectsize, &Size_tTy, 1);
272 const llvm::IntegerType *Int1Ty = llvm::IntegerType::get(VMContext, 1);
273
Mike Stump3f6f9fe2009-12-16 02:57:00 +0000274 // In time, people may want to control this and use a 1 here.
Chris Lattnerbc3be652010-04-10 18:34:14 +0000275 llvm::Value *Arg = llvm::ConstantInt::get(Int1Ty, 0);
Mike Stump3f6f9fe2009-12-16 02:57:00 +0000276 llvm::Value *C = Builder.CreateCall2(F, Address, Arg);
277 llvm::BasicBlock *Cont = createBasicBlock();
278 llvm::BasicBlock *Check = createBasicBlock();
279 llvm::Value *NegativeOne = llvm::ConstantInt::get(Size_tTy, -1ULL);
280 Builder.CreateCondBr(Builder.CreateICmpEQ(C, NegativeOne), Cont, Check);
281
282 EmitBlock(Check);
283 Builder.CreateCondBr(Builder.CreateICmpUGE(C,
284 llvm::ConstantInt::get(Size_tTy, Size)),
285 Cont, getTrapBB());
286 EmitBlock(Cont);
287}
Chris Lattner4647a212007-08-31 22:49:20 +0000288
Chris Lattner116ce8f2010-01-09 21:40:03 +0000289
290llvm::Value *CodeGenFunction::
291EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV,
292 bool isInc, bool isPre) {
293 QualType ValTy = E->getSubExpr()->getType();
294 llvm::Value *InVal = EmitLoadOfLValue(LV, ValTy).getScalarVal();
295
296 int AmountVal = isInc ? 1 : -1;
297
298 if (ValTy->isPointerType() &&
299 ValTy->getAs<PointerType>()->isVariableArrayType()) {
300 // The amount of the addition/subtraction needs to account for the VLA size
301 ErrorUnsupported(E, "VLA pointer inc/dec");
302 }
303
304 llvm::Value *NextVal;
305 if (const llvm::PointerType *PT =
306 dyn_cast<llvm::PointerType>(InVal->getType())) {
307 llvm::Constant *Inc =
308 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), AmountVal);
309 if (!isa<llvm::FunctionType>(PT->getElementType())) {
310 QualType PTEE = ValTy->getPointeeType();
311 if (const ObjCInterfaceType *OIT =
312 dyn_cast<ObjCInterfaceType>(PTEE)) {
313 // Handle interface types, which are not represented with a concrete
314 // type.
315 int size = getContext().getTypeSize(OIT) / 8;
316 if (!isInc)
317 size = -size;
318 Inc = llvm::ConstantInt::get(Inc->getType(), size);
319 const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(VMContext);
320 InVal = Builder.CreateBitCast(InVal, i8Ty);
321 NextVal = Builder.CreateGEP(InVal, Inc, "add.ptr");
322 llvm::Value *lhs = LV.getAddress();
323 lhs = Builder.CreateBitCast(lhs, llvm::PointerType::getUnqual(i8Ty));
324 LV = LValue::MakeAddr(lhs, MakeQualifiers(ValTy));
325 } else
326 NextVal = Builder.CreateInBoundsGEP(InVal, Inc, "ptrincdec");
327 } else {
328 const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(VMContext);
329 NextVal = Builder.CreateBitCast(InVal, i8Ty, "tmp");
330 NextVal = Builder.CreateGEP(NextVal, Inc, "ptrincdec");
331 NextVal = Builder.CreateBitCast(NextVal, InVal->getType());
332 }
333 } else if (InVal->getType() == llvm::Type::getInt1Ty(VMContext) && isInc) {
334 // Bool++ is an interesting case, due to promotion rules, we get:
335 // Bool++ -> Bool = Bool+1 -> Bool = (int)Bool+1 ->
336 // Bool = ((int)Bool+1) != 0
337 // An interesting aspect of this is that increment is always true.
338 // Decrement does not have this property.
339 NextVal = llvm::ConstantInt::getTrue(VMContext);
340 } else if (isa<llvm::IntegerType>(InVal->getType())) {
341 NextVal = llvm::ConstantInt::get(InVal->getType(), AmountVal);
342
343 // Signed integer overflow is undefined behavior.
344 if (ValTy->isSignedIntegerType())
345 NextVal = Builder.CreateNSWAdd(InVal, NextVal, isInc ? "inc" : "dec");
346 else
347 NextVal = Builder.CreateAdd(InVal, NextVal, isInc ? "inc" : "dec");
348 } else {
349 // Add the inc/dec to the real part.
350 if (InVal->getType()->isFloatTy())
351 NextVal =
352 llvm::ConstantFP::get(VMContext,
353 llvm::APFloat(static_cast<float>(AmountVal)));
354 else if (InVal->getType()->isDoubleTy())
355 NextVal =
356 llvm::ConstantFP::get(VMContext,
357 llvm::APFloat(static_cast<double>(AmountVal)));
358 else {
359 llvm::APFloat F(static_cast<float>(AmountVal));
360 bool ignored;
361 F.convert(Target.getLongDoubleFormat(), llvm::APFloat::rmTowardZero,
362 &ignored);
363 NextVal = llvm::ConstantFP::get(VMContext, F);
364 }
365 NextVal = Builder.CreateFAdd(InVal, NextVal, isInc ? "inc" : "dec");
366 }
367
368 // Store the updated result through the lvalue.
Daniel Dunbardc406b82010-04-05 21:36:35 +0000369 if (LV.isBitField())
Chris Lattner116ce8f2010-01-09 21:40:03 +0000370 EmitStoreThroughBitfieldLValue(RValue::get(NextVal), LV, ValTy, &NextVal);
371 else
372 EmitStoreThroughLValue(RValue::get(NextVal), LV, ValTy);
373
374 // If this is a postinc, return the value read from memory, otherwise use the
375 // updated value.
376 return isPre ? NextVal : InVal;
377}
378
379
380CodeGenFunction::ComplexPairTy CodeGenFunction::
381EmitComplexPrePostIncDec(const UnaryOperator *E, LValue LV,
382 bool isInc, bool isPre) {
383 ComplexPairTy InVal = LoadComplexFromAddr(LV.getAddress(),
384 LV.isVolatileQualified());
385
386 llvm::Value *NextVal;
387 if (isa<llvm::IntegerType>(InVal.first->getType())) {
388 uint64_t AmountVal = isInc ? 1 : -1;
389 NextVal = llvm::ConstantInt::get(InVal.first->getType(), AmountVal, true);
390
391 // Add the inc/dec to the real part.
392 NextVal = Builder.CreateAdd(InVal.first, NextVal, isInc ? "inc" : "dec");
393 } else {
394 QualType ElemTy = E->getType()->getAs<ComplexType>()->getElementType();
395 llvm::APFloat FVal(getContext().getFloatTypeSemantics(ElemTy), 1);
396 if (!isInc)
397 FVal.changeSign();
398 NextVal = llvm::ConstantFP::get(getLLVMContext(), FVal);
399
400 // Add the inc/dec to the real part.
401 NextVal = Builder.CreateFAdd(InVal.first, NextVal, isInc ? "inc" : "dec");
402 }
403
404 ComplexPairTy IncVal(NextVal, InVal.second);
405
406 // Store the updated result through the lvalue.
407 StoreComplexToAddr(IncVal, LV.getAddress(), LV.isVolatileQualified());
408
409 // If this is a postinc, return the value read from memory, otherwise use the
410 // updated value.
411 return isPre ? IncVal : InVal;
412}
413
414
Chris Lattnera45c5af2007-06-02 19:47:04 +0000415//===----------------------------------------------------------------------===//
Chris Lattnerd7f58862007-06-02 05:24:33 +0000416// LValue Expression Emission
Chris Lattnera45c5af2007-06-02 19:47:04 +0000417//===----------------------------------------------------------------------===//
Chris Lattnerd7f58862007-06-02 05:24:33 +0000418
Daniel Dunbarc79407f2009-02-05 07:09:07 +0000419RValue CodeGenFunction::GetUndefRValue(QualType Ty) {
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000420 if (Ty->isVoidType())
Daniel Dunbarc79407f2009-02-05 07:09:07 +0000421 return RValue::get(0);
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000422
423 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
Daniel Dunbar8429dbc2009-01-09 20:09:28 +0000424 const llvm::Type *EltTy = ConvertType(CTy->getElementType());
Owen Anderson7ec07a52009-07-30 23:11:26 +0000425 llvm::Value *U = llvm::UndefValue::get(EltTy);
Daniel Dunbar8429dbc2009-01-09 20:09:28 +0000426 return RValue::getComplex(std::make_pair(U, U));
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000427 }
428
429 if (hasAggregateLLVMType(Ty)) {
Owen Anderson9793f0e2009-07-29 22:16:19 +0000430 const llvm::Type *LTy = llvm::PointerType::getUnqual(ConvertType(Ty));
Owen Anderson7ec07a52009-07-30 23:11:26 +0000431 return RValue::getAggregate(llvm::UndefValue::get(LTy));
Daniel Dunbar8429dbc2009-01-09 20:09:28 +0000432 }
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000433
434 return RValue::get(llvm::UndefValue::get(ConvertType(Ty)));
Daniel Dunbarbb197e42009-01-09 16:50:52 +0000435}
436
Daniel Dunbarc79407f2009-02-05 07:09:07 +0000437RValue CodeGenFunction::EmitUnsupportedRValue(const Expr *E,
438 const char *Name) {
439 ErrorUnsupported(E, Name);
440 return GetUndefRValue(E->getType());
441}
442
Daniel Dunbarf2e69882008-08-25 20:45:57 +0000443LValue CodeGenFunction::EmitUnsupportedLValue(const Expr *E,
444 const char *Name) {
445 ErrorUnsupported(E, Name);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000446 llvm::Type *Ty = llvm::PointerType::getUnqual(ConvertType(E->getType()));
Owen Anderson7ec07a52009-07-30 23:11:26 +0000447 return LValue::MakeAddr(llvm::UndefValue::get(Ty),
John McCall8ccfcb52009-09-24 19:53:00 +0000448 MakeQualifiers(E->getType()));
Daniel Dunbarf2e69882008-08-25 20:45:57 +0000449}
450
Mike Stump3f6f9fe2009-12-16 02:57:00 +0000451LValue CodeGenFunction::EmitCheckedLValue(const Expr *E) {
452 LValue LV = EmitLValue(E);
Daniel Dunbardc406b82010-04-05 21:36:35 +0000453 if (!isa<DeclRefExpr>(E) && !LV.isBitField() && LV.isSimple())
Mike Stump3f6f9fe2009-12-16 02:57:00 +0000454 EmitCheck(LV.getAddress(), getContext().getTypeSize(E->getType()) / 8);
455 return LV;
456}
457
Chris Lattner8394d792007-06-05 20:53:16 +0000458/// EmitLValue - Emit code to compute a designator that specifies the location
459/// of the expression.
460///
Mike Stump4a3999f2009-09-09 13:00:44 +0000461/// This can return one of two things: a simple address or a bitfield reference.
462/// In either case, the LLVM Value* in the LValue structure is guaranteed to be
463/// an LLVM pointer type.
Chris Lattner8394d792007-06-05 20:53:16 +0000464///
Mike Stump4a3999f2009-09-09 13:00:44 +0000465/// If this returns a bitfield reference, nothing about the pointee type of the
466/// LLVM value is known: For example, it may not be a pointer to an integer.
Chris Lattner8394d792007-06-05 20:53:16 +0000467///
Mike Stump4a3999f2009-09-09 13:00:44 +0000468/// If this returns a normal address, and if the lvalue's C type is fixed size,
469/// this method guarantees that the returned pointer type will point to an LLVM
470/// type of the same size of the lvalue's type. If the lvalue has a variable
471/// length type, this is not possible.
Chris Lattner8394d792007-06-05 20:53:16 +0000472///
Chris Lattnerd7f58862007-06-02 05:24:33 +0000473LValue CodeGenFunction::EmitLValue(const Expr *E) {
474 switch (E->getStmtClass()) {
Daniel Dunbarf2e69882008-08-25 20:45:57 +0000475 default: return EmitUnsupportedLValue(E, "l-value expression");
Chris Lattnerd7f58862007-06-02 05:24:33 +0000476
Fariborz Jahanian531c16f2009-12-09 23:35:29 +0000477 case Expr::ObjCIsaExprClass:
478 return EmitObjCIsaExpr(cast<ObjCIsaExpr>(E));
Mike Stump4a3999f2009-09-09 13:00:44 +0000479 case Expr::BinaryOperatorClass:
Daniel Dunbar8cde00a2008-09-04 03:20:13 +0000480 return EmitBinaryOperatorLValue(cast<BinaryOperator>(E));
Mike Stump4a3999f2009-09-09 13:00:44 +0000481 case Expr::CallExprClass:
Anders Carlssonc82555f2009-09-01 21:18:52 +0000482 case Expr::CXXMemberCallExprClass:
Douglas Gregor993603d2008-11-14 16:09:21 +0000483 case Expr::CXXOperatorCallExprClass:
484 return EmitCallExprLValue(cast<CallExpr>(E));
Daniel Dunbar8d9dc4a2009-02-11 20:59:32 +0000485 case Expr::VAArgExprClass:
486 return EmitVAArgExprLValue(cast<VAArgExpr>(E));
Mike Stump4a3999f2009-09-09 13:00:44 +0000487 case Expr::DeclRefExprClass:
Douglas Gregorc7acfdf2009-01-06 05:10:23 +0000488 return EmitDeclRefLValue(cast<DeclRefExpr>(E));
Chris Lattner946aa312007-06-05 03:59:43 +0000489 case Expr::ParenExprClass:return EmitLValue(cast<ParenExpr>(E)->getSubExpr());
Chris Lattner6307f192008-08-10 01:53:14 +0000490 case Expr::PredefinedExprClass:
491 return EmitPredefinedLValue(cast<PredefinedExpr>(E));
Chris Lattner4347e3692007-06-06 04:54:52 +0000492 case Expr::StringLiteralClass:
493 return EmitStringLiteralLValue(cast<StringLiteral>(E));
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +0000494 case Expr::ObjCEncodeExprClass:
495 return EmitObjCEncodeExprLValue(cast<ObjCEncodeExpr>(E));
Chris Lattner4bd55962008-03-30 23:03:07 +0000496
Mike Stump4a3999f2009-09-09 13:00:44 +0000497 case Expr::BlockDeclRefExprClass:
Mike Stump1db7d042009-02-28 09:07:16 +0000498 return EmitBlockDeclRefLValue(cast<BlockDeclRefExpr>(E));
499
Anders Carlsson3be22e22009-05-30 23:23:33 +0000500 case Expr::CXXTemporaryObjectExprClass:
501 case Expr::CXXConstructExprClass:
Anders Carlssonfd2af0c2009-05-30 23:30:54 +0000502 return EmitCXXConstructLValue(cast<CXXConstructExpr>(E));
503 case Expr::CXXBindTemporaryExprClass:
504 return EmitCXXBindTemporaryLValue(cast<CXXBindTemporaryExpr>(E));
Anders Carlsson96bad9a2009-09-14 01:10:45 +0000505 case Expr::CXXExprWithTemporariesClass:
506 return EmitCXXExprWithTemporariesLValue(cast<CXXExprWithTemporaries>(E));
Anders Carlsson52ce3bb2009-11-14 01:51:50 +0000507 case Expr::CXXZeroInitValueExprClass:
508 return EmitNullInitializationLValue(cast<CXXZeroInitValueExpr>(E));
509 case Expr::CXXDefaultArgExprClass:
510 return EmitLValue(cast<CXXDefaultArgExpr>(E)->getExpr());
Mike Stumpc9b231c2009-11-15 08:09:41 +0000511 case Expr::CXXTypeidExprClass:
512 return EmitCXXTypeidLValue(cast<CXXTypeidExpr>(E));
Anders Carlssonfd2af0c2009-05-30 23:30:54 +0000513
Daniel Dunbarc8317a42008-08-23 10:51:21 +0000514 case Expr::ObjCMessageExprClass:
515 return EmitObjCMessageExprLValue(cast<ObjCMessageExpr>(E));
Mike Stump4a3999f2009-09-09 13:00:44 +0000516 case Expr::ObjCIvarRefExprClass:
Chris Lattner4bd55962008-03-30 23:03:07 +0000517 return EmitObjCIvarRefLValue(cast<ObjCIvarRefExpr>(E));
Daniel Dunbarf2e69882008-08-25 20:45:57 +0000518 case Expr::ObjCPropertyRefExprClass:
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +0000519 return EmitObjCPropertyRefLValue(cast<ObjCPropertyRefExpr>(E));
Fariborz Jahanian9a846652009-08-20 17:02:02 +0000520 case Expr::ObjCImplicitSetterGetterRefExprClass:
521 return EmitObjCKVCRefLValue(cast<ObjCImplicitSetterGetterRefExpr>(E));
Douglas Gregor8ea1f532008-11-04 14:56:14 +0000522 case Expr::ObjCSuperExprClass:
Chris Lattnera4185c52009-04-25 19:35:26 +0000523 return EmitObjCSuperExprLValue(cast<ObjCSuperExpr>(E));
Douglas Gregor8ea1f532008-11-04 14:56:14 +0000524
Chris Lattnera4185c52009-04-25 19:35:26 +0000525 case Expr::StmtExprClass:
526 return EmitStmtExprLValue(cast<StmtExpr>(E));
Mike Stump4a3999f2009-09-09 13:00:44 +0000527 case Expr::UnaryOperatorClass:
Chris Lattner8394d792007-06-05 20:53:16 +0000528 return EmitUnaryOpLValue(cast<UnaryOperator>(E));
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000529 case Expr::ArraySubscriptExprClass:
530 return EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E));
Nate Begemance4d7fc2008-04-18 23:10:10 +0000531 case Expr::ExtVectorElementExprClass:
532 return EmitExtVectorElementExpr(cast<ExtVectorElementExpr>(E));
Mike Stump4a3999f2009-09-09 13:00:44 +0000533 case Expr::MemberExprClass:
Douglas Gregorc1905232009-08-26 22:36:53 +0000534 return EmitMemberExpr(cast<MemberExpr>(E));
Eli Friedman9fd8b682008-05-13 23:18:27 +0000535 case Expr::CompoundLiteralExprClass:
536 return EmitCompoundLiteralLValue(cast<CompoundLiteralExpr>(E));
Daniel Dunbarbf1fe8c2009-03-24 02:38:23 +0000537 case Expr::ConditionalOperatorClass:
Anders Carlsson1450adb2009-09-15 16:35:24 +0000538 return EmitConditionalOperatorLValue(cast<ConditionalOperator>(E));
Chris Lattner053441f2008-12-12 05:35:08 +0000539 case Expr::ChooseExprClass:
Eli Friedmane0a5b8b2009-03-04 05:52:32 +0000540 return EmitLValue(cast<ChooseExpr>(E)->getChosenSubExpr(getContext()));
Chris Lattner63d06ab2009-03-18 04:02:57 +0000541 case Expr::ImplicitCastExprClass:
542 case Expr::CStyleCastExprClass:
543 case Expr::CXXFunctionalCastExprClass:
544 case Expr::CXXStaticCastExprClass:
545 case Expr::CXXDynamicCastExprClass:
546 case Expr::CXXReinterpretCastExprClass:
547 case Expr::CXXConstCastExprClass:
Chris Lattner28bcf1a2009-03-18 18:28:57 +0000548 return EmitCastLValue(cast<CastExpr>(E));
Chris Lattnerd7f58862007-06-02 05:24:33 +0000549 }
550}
551
Daniel Dunbar1d425462009-02-10 00:57:50 +0000552llvm::Value *CodeGenFunction::EmitLoadOfScalar(llvm::Value *Addr, bool Volatile,
553 QualType Ty) {
Daniel Dunbarc76493a2009-11-29 21:23:36 +0000554 llvm::LoadInst *Load = Builder.CreateLoad(Addr, "tmp");
555 if (Volatile)
556 Load->setVolatile(true);
Daniel Dunbar1d425462009-02-10 00:57:50 +0000557
Anders Carlsson29a1be32009-05-19 19:36:19 +0000558 // Bool can have different representation in memory than in registers.
Daniel Dunbarc76493a2009-11-29 21:23:36 +0000559 llvm::Value *V = Load;
Daniel Dunbar1d425462009-02-10 00:57:50 +0000560 if (Ty->isBooleanType())
Owen Anderson41a75022009-08-13 21:57:51 +0000561 if (V->getType() != llvm::Type::getInt1Ty(VMContext))
562 V = Builder.CreateTrunc(V, llvm::Type::getInt1Ty(VMContext), "tobool");
Mike Stump4a3999f2009-09-09 13:00:44 +0000563
Daniel Dunbar1d425462009-02-10 00:57:50 +0000564 return V;
565}
566
567void CodeGenFunction::EmitStoreOfScalar(llvm::Value *Value, llvm::Value *Addr,
Anders Carlsson83709642009-05-19 18:50:41 +0000568 bool Volatile, QualType Ty) {
Mike Stump4a3999f2009-09-09 13:00:44 +0000569
Anders Carlsson29a1be32009-05-19 19:36:19 +0000570 if (Ty->isBooleanType()) {
571 // Bool can have different representation in memory than in registers.
Anders Carlsson29a1be32009-05-19 19:36:19 +0000572 const llvm::PointerType *DstPtr = cast<llvm::PointerType>(Addr->getType());
Eli Friedmanb2b120f2009-12-01 22:31:51 +0000573 Value = Builder.CreateIntCast(Value, DstPtr->getElementType(), false);
Daniel Dunbar1d425462009-02-10 00:57:50 +0000574 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000575 Builder.CreateStore(Value, Addr, Volatile);
Daniel Dunbar1d425462009-02-10 00:57:50 +0000576}
577
Mike Stump4a3999f2009-09-09 13:00:44 +0000578/// EmitLoadOfLValue - Given an expression that represents a value lvalue, this
579/// method emits the address of the lvalue, then loads the result as an rvalue,
580/// returning the rvalue.
Chris Lattner9369a562007-06-29 16:31:29 +0000581RValue CodeGenFunction::EmitLoadOfLValue(LValue LV, QualType ExprType) {
Fariborz Jahanian50a12702008-11-19 17:34:06 +0000582 if (LV.isObjCWeak()) {
Mike Stump4a3999f2009-09-09 13:00:44 +0000583 // load of a __weak object.
Fariborz Jahanianf5125d12008-11-18 21:45:40 +0000584 llvm::Value *AddrWeakObj = LV.getAddress();
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000585 return RValue::get(CGM.getObjCRuntime().EmitObjCWeakRead(*this,
586 AddrWeakObj));
Fariborz Jahanianf5125d12008-11-18 21:45:40 +0000587 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000588
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000589 if (LV.isSimple()) {
590 llvm::Value *Ptr = LV.getAddress();
Douglas Gregora6437802010-02-05 21:10:36 +0000591 const llvm::Type *EltTy =
592 cast<llvm::PointerType>(Ptr->getType())->getElementType();
Mike Stump4a3999f2009-09-09 13:00:44 +0000593
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000594 // Simple scalar l-value.
Daniel Dunbar3d33fab2010-02-08 22:53:07 +0000595 //
596 // FIXME: We shouldn't have to use isSingleValueType here.
Douglas Gregora6437802010-02-05 21:10:36 +0000597 if (EltTy->isSingleValueType())
Mike Stump4a3999f2009-09-09 13:00:44 +0000598 return RValue::get(EmitLoadOfScalar(Ptr, LV.isVolatileQualified(),
Daniel Dunbar1d425462009-02-10 00:57:50 +0000599 ExprType));
Mike Stump4a3999f2009-09-09 13:00:44 +0000600
Chris Lattner6278e6a2007-08-11 00:04:45 +0000601 assert(ExprType->isFunctionType() && "Unknown scalar value");
602 return RValue::get(Ptr);
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000603 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000604
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000605 if (LV.isVectorElt()) {
Eli Friedman327944b2008-06-13 23:01:12 +0000606 llvm::Value *Vec = Builder.CreateLoad(LV.getVectorAddr(),
607 LV.isVolatileQualified(), "tmp");
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000608 return RValue::get(Builder.CreateExtractElement(Vec, LV.getVectorIdx(),
609 "vecext"));
610 }
Chris Lattner73ab9b32007-08-03 00:16:29 +0000611
612 // If this is a reference to a subset of the elements of a vector, either
613 // shuffle the input or extract/insert them as appropriate.
Nate Begemance4d7fc2008-04-18 23:10:10 +0000614 if (LV.isExtVectorElt())
615 return EmitLoadOfExtVectorElementLValue(LV, ExprType);
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000616
Daniel Dunbardc406b82010-04-05 21:36:35 +0000617 if (LV.isBitField())
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000618 return EmitLoadOfBitfieldLValue(LV, ExprType);
619
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +0000620 if (LV.isPropertyRef())
621 return EmitLoadOfPropertyRefLValue(LV, ExprType);
622
Chris Lattner6c7ce102009-02-16 21:11:58 +0000623 assert(LV.isKVCRef() && "Unknown LValue type!");
624 return EmitLoadOfKVCRefLValue(LV, ExprType);
Chris Lattner8394d792007-06-05 20:53:16 +0000625}
626
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000627RValue CodeGenFunction::EmitLoadOfBitfieldLValue(LValue LV,
628 QualType ExprType) {
Daniel Dunbar196ea442010-04-06 01:07:44 +0000629 const CGBitFieldInfo &Info = LV.getBitFieldInfo();
Daniel Dunbaread7c912008-08-06 05:08:45 +0000630
Daniel Dunbar3447a022010-04-13 23:34:15 +0000631 // Get the output type.
632 const llvm::Type *ResLTy = ConvertType(ExprType);
633 unsigned ResSizeInBits = CGM.getTargetData().getTypeSizeInBits(ResLTy);
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000634
Daniel Dunbar3447a022010-04-13 23:34:15 +0000635 // Compute the result as an OR of all of the individual component accesses.
636 llvm::Value *Res = 0;
637 for (unsigned i = 0, e = Info.getNumComponents(); i != e; ++i) {
638 const CGBitFieldInfo::AccessInfo &AI = Info.getComponent(i);
Mike Stump4a3999f2009-09-09 13:00:44 +0000639
Daniel Dunbar3447a022010-04-13 23:34:15 +0000640 // Get the field pointer.
641 llvm::Value *Ptr = LV.getBitFieldBaseAddr();
Mike Stump4a3999f2009-09-09 13:00:44 +0000642
Daniel Dunbar3447a022010-04-13 23:34:15 +0000643 // Only offset by the field index if used, so that incoming values are not
644 // required to be structures.
645 if (AI.FieldIndex)
646 Ptr = Builder.CreateStructGEP(Ptr, AI.FieldIndex, "bf.field");
Mike Stump4a3999f2009-09-09 13:00:44 +0000647
Daniel Dunbar3447a022010-04-13 23:34:15 +0000648 // Offset by the byte offset, if used.
649 if (AI.FieldByteOffset) {
650 const llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(VMContext);
651 Ptr = Builder.CreateBitCast(Ptr, i8PTy);
652 Ptr = Builder.CreateConstGEP1_32(Ptr, AI.FieldByteOffset,"bf.field.offs");
653 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000654
Daniel Dunbar3447a022010-04-13 23:34:15 +0000655 // Cast to the access type.
656 const llvm::Type *PTy = llvm::Type::getIntNPtrTy(VMContext, AI.AccessWidth,
657 ExprType.getAddressSpace());
658 Ptr = Builder.CreateBitCast(Ptr, PTy);
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000659
Daniel Dunbar3447a022010-04-13 23:34:15 +0000660 // Perform the load.
661 llvm::LoadInst *Load = Builder.CreateLoad(Ptr, LV.isVolatileQualified());
662 if (AI.AccessAlignment)
663 Load->setAlignment(AI.AccessAlignment);
664
665 // Shift out unused low bits and mask out unused high bits.
666 llvm::Value *Val = Load;
667 if (AI.FieldBitStart)
Daniel Dunbar67aba792010-04-15 03:47:33 +0000668 Val = Builder.CreateLShr(Load, AI.FieldBitStart);
Daniel Dunbar3447a022010-04-13 23:34:15 +0000669 Val = Builder.CreateAnd(Val, llvm::APInt::getLowBitsSet(AI.AccessWidth,
670 AI.TargetBitWidth),
671 "bf.clear");
672
673 // Extend or truncate to the target size.
674 if (AI.AccessWidth < ResSizeInBits)
675 Val = Builder.CreateZExt(Val, ResLTy);
676 else if (AI.AccessWidth > ResSizeInBits)
677 Val = Builder.CreateTrunc(Val, ResLTy);
678
679 // Shift into place, and OR into the result.
680 if (AI.TargetBitOffset)
681 Val = Builder.CreateShl(Val, AI.TargetBitOffset);
682 Res = Res ? Builder.CreateOr(Res, Val) : Val;
Daniel Dunbaread7c912008-08-06 05:08:45 +0000683 }
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000684
Daniel Dunbar3447a022010-04-13 23:34:15 +0000685 // If the bit-field is signed, perform the sign-extension.
686 //
687 // FIXME: This can easily be folded into the load of the high bits, which
688 // could also eliminate the mask of high bits in some situations.
689 if (Info.isSigned()) {
Daniel Dunbar67aba792010-04-15 03:47:33 +0000690 unsigned ExtraBits = ResSizeInBits - Info.getSize();
Daniel Dunbar3447a022010-04-13 23:34:15 +0000691 if (ExtraBits)
692 Res = Builder.CreateAShr(Builder.CreateShl(Res, ExtraBits),
693 ExtraBits, "bf.val.sext");
Daniel Dunbaread7c912008-08-06 05:08:45 +0000694 }
Eli Friedmanf2442dc2008-05-17 20:03:47 +0000695
Daniel Dunbar3447a022010-04-13 23:34:15 +0000696 return RValue::get(Res);
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000697}
698
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +0000699RValue CodeGenFunction::EmitLoadOfPropertyRefLValue(LValue LV,
700 QualType ExprType) {
701 return EmitObjCPropertyGet(LV.getPropertyRefExpr());
702}
703
Fariborz Jahanian9ac53512008-11-22 22:30:21 +0000704RValue CodeGenFunction::EmitLoadOfKVCRefLValue(LValue LV,
705 QualType ExprType) {
706 return EmitObjCPropertyGet(LV.getKVCRefExpr());
707}
708
Nate Begemanb699c9b2009-01-18 06:42:49 +0000709// If this is a reference to a subset of the elements of a vector, create an
710// appropriate shufflevector.
Nate Begemance4d7fc2008-04-18 23:10:10 +0000711RValue CodeGenFunction::EmitLoadOfExtVectorElementLValue(LValue LV,
712 QualType ExprType) {
Eli Friedman327944b2008-06-13 23:01:12 +0000713 llvm::Value *Vec = Builder.CreateLoad(LV.getExtVectorAddr(),
714 LV.isVolatileQualified(), "tmp");
Mike Stump4a3999f2009-09-09 13:00:44 +0000715
Nate Begemanf322eab2008-05-09 06:41:27 +0000716 const llvm::Constant *Elts = LV.getExtVectorElts();
Mike Stump4a3999f2009-09-09 13:00:44 +0000717
718 // If the result of the expression is a non-vector type, we must be extracting
719 // a single element. Just codegen as an extractelement.
John McCall9dd450b2009-09-21 23:43:11 +0000720 const VectorType *ExprVT = ExprType->getAs<VectorType>();
Chris Lattner8eab8ff2007-08-10 17:10:08 +0000721 if (!ExprVT) {
Dan Gohman75d69da2008-05-22 00:50:06 +0000722 unsigned InIdx = getAccessedFieldNo(0, Elts);
Owen Anderson41a75022009-08-13 21:57:51 +0000723 llvm::Value *Elt = llvm::ConstantInt::get(
724 llvm::Type::getInt32Ty(VMContext), InIdx);
Chris Lattner40ff7012007-08-03 16:18:34 +0000725 return RValue::get(Builder.CreateExtractElement(Vec, Elt, "tmp"));
726 }
Nate Begemanb699c9b2009-01-18 06:42:49 +0000727
728 // Always use shuffle vector to try to retain the original program structure
Chris Lattner8eab8ff2007-08-10 17:10:08 +0000729 unsigned NumResultElts = ExprVT->getNumElements();
Mike Stump4a3999f2009-09-09 13:00:44 +0000730
Nate Begemanb699c9b2009-01-18 06:42:49 +0000731 llvm::SmallVector<llvm::Constant*, 4> Mask;
Chris Lattner40ff7012007-08-03 16:18:34 +0000732 for (unsigned i = 0; i != NumResultElts; ++i) {
Dan Gohman75d69da2008-05-22 00:50:06 +0000733 unsigned InIdx = getAccessedFieldNo(i, Elts);
Owen Anderson41a75022009-08-13 21:57:51 +0000734 Mask.push_back(llvm::ConstantInt::get(
735 llvm::Type::getInt32Ty(VMContext), InIdx));
Chris Lattner40ff7012007-08-03 16:18:34 +0000736 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000737
Owen Anderson3cc120a2009-07-28 21:22:35 +0000738 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
Nate Begemanb699c9b2009-01-18 06:42:49 +0000739 Vec = Builder.CreateShuffleVector(Vec,
Owen Anderson7ec07a52009-07-30 23:11:26 +0000740 llvm::UndefValue::get(Vec->getType()),
Nate Begemanb699c9b2009-01-18 06:42:49 +0000741 MaskV, "tmp");
742 return RValue::get(Vec);
Chris Lattner40ff7012007-08-03 16:18:34 +0000743}
744
745
Chris Lattner9369a562007-06-29 16:31:29 +0000746
Chris Lattner8394d792007-06-05 20:53:16 +0000747/// EmitStoreThroughLValue - Store the specified rvalue into the specified
748/// lvalue, where both are guaranteed to the have the same type, and that type
749/// is 'Ty'.
Mike Stump4a3999f2009-09-09 13:00:44 +0000750void CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst,
Chris Lattner8394d792007-06-05 20:53:16 +0000751 QualType Ty) {
Chris Lattner41d480e2007-08-03 16:28:33 +0000752 if (!Dst.isSimple()) {
753 if (Dst.isVectorElt()) {
754 // Read/modify/write the vector, inserting the new element.
Eli Friedman327944b2008-06-13 23:01:12 +0000755 llvm::Value *Vec = Builder.CreateLoad(Dst.getVectorAddr(),
756 Dst.isVolatileQualified(), "tmp");
Chris Lattner4647a212007-08-31 22:49:20 +0000757 Vec = Builder.CreateInsertElement(Vec, Src.getScalarVal(),
Chris Lattner41d480e2007-08-03 16:28:33 +0000758 Dst.getVectorIdx(), "vecins");
Eli Friedman327944b2008-06-13 23:01:12 +0000759 Builder.CreateStore(Vec, Dst.getVectorAddr(),Dst.isVolatileQualified());
Chris Lattner41d480e2007-08-03 16:28:33 +0000760 return;
761 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000762
Nate Begemance4d7fc2008-04-18 23:10:10 +0000763 // If this is an update of extended vector elements, insert them as
764 // appropriate.
765 if (Dst.isExtVectorElt())
766 return EmitStoreThroughExtVectorComponentLValue(Src, Dst, Ty);
Lauro Ramos Venancio09af71c2008-01-22 22:36:45 +0000767
Daniel Dunbardc406b82010-04-05 21:36:35 +0000768 if (Dst.isBitField())
Lauro Ramos Venancio09af71c2008-01-22 22:36:45 +0000769 return EmitStoreThroughBitfieldLValue(Src, Dst, Ty);
770
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +0000771 if (Dst.isPropertyRef())
772 return EmitStoreThroughPropertyRefLValue(Src, Dst, Ty);
773
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000774 assert(Dst.isKVCRef() && "Unknown LValue type");
775 return EmitStoreThroughKVCRefLValue(Src, Dst, Ty);
Chris Lattner41d480e2007-08-03 16:28:33 +0000776 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000777
Fariborz Jahanian10bec102009-02-21 00:30:43 +0000778 if (Dst.isObjCWeak() && !Dst.isNonGC()) {
Mike Stump4a3999f2009-09-09 13:00:44 +0000779 // load of a __weak object.
Fariborz Jahanian50a12702008-11-19 17:34:06 +0000780 llvm::Value *LvalueDst = Dst.getAddress();
781 llvm::Value *src = Src.getScalarVal();
Mike Stumpca5ae662009-04-14 00:57:29 +0000782 CGM.getObjCRuntime().EmitObjCWeakAssign(*this, src, LvalueDst);
Fariborz Jahanian50a12702008-11-19 17:34:06 +0000783 return;
784 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000785
Fariborz Jahanian10bec102009-02-21 00:30:43 +0000786 if (Dst.isObjCStrong() && !Dst.isNonGC()) {
Mike Stump4a3999f2009-09-09 13:00:44 +0000787 // load of a __strong object.
Fariborz Jahanian50a12702008-11-19 17:34:06 +0000788 llvm::Value *LvalueDst = Dst.getAddress();
789 llvm::Value *src = Src.getScalarVal();
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000790 if (Dst.isObjCIvar()) {
791 assert(Dst.getBaseIvarExp() && "BaseIvarExp is NULL");
792 const llvm::Type *ResultType = ConvertType(getContext().LongTy);
793 llvm::Value *RHS = EmitScalarExpr(Dst.getBaseIvarExp());
Fariborz Jahanian1f9ed582009-09-25 00:00:20 +0000794 llvm::Value *dst = RHS;
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000795 RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast");
796 llvm::Value *LHS =
797 Builder.CreatePtrToInt(LvalueDst, ResultType, "sub.ptr.lhs.cast");
798 llvm::Value *BytesBetween = Builder.CreateSub(LHS, RHS, "ivar.offset");
Fariborz Jahanian1f9ed582009-09-25 00:00:20 +0000799 CGM.getObjCRuntime().EmitObjCIvarAssign(*this, src, dst,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000800 BytesBetween);
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000801 } else if (Dst.isGlobalObjCRef())
Fariborz Jahanian32ff7ae2009-05-04 23:27:20 +0000802 CGM.getObjCRuntime().EmitObjCGlobalAssign(*this, src, LvalueDst);
803 else
804 CGM.getObjCRuntime().EmitObjCStrongCastAssign(*this, src, LvalueDst);
Fariborz Jahanian50a12702008-11-19 17:34:06 +0000805 return;
806 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000807
Chris Lattner6278e6a2007-08-11 00:04:45 +0000808 assert(Src.isScalar() && "Can't emit an agg store with this method");
Anders Carlsson83709642009-05-19 18:50:41 +0000809 EmitStoreOfScalar(Src.getScalarVal(), Dst.getAddress(),
810 Dst.isVolatileQualified(), Ty);
Chris Lattner8394d792007-06-05 20:53:16 +0000811}
812
Lauro Ramos Venancio09af71c2008-01-22 22:36:45 +0000813void CodeGenFunction::EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst,
Mike Stump4a3999f2009-09-09 13:00:44 +0000814 QualType Ty,
Daniel Dunbar9b1335e2008-11-19 09:36:46 +0000815 llvm::Value **Result) {
Daniel Dunbar196ea442010-04-06 01:07:44 +0000816 const CGBitFieldInfo &Info = Dst.getBitFieldInfo();
Lauro Ramos Venancio09af71c2008-01-22 22:36:45 +0000817
Daniel Dunbar67aba792010-04-15 03:47:33 +0000818 // Get the output type.
Anders Carlsson8345a702010-04-17 21:52:22 +0000819 const llvm::Type *ResLTy = ConvertTypeForMem(Ty);
Daniel Dunbar67aba792010-04-15 03:47:33 +0000820 unsigned ResSizeInBits = CGM.getTargetData().getTypeSizeInBits(ResLTy);
Daniel Dunbaread7c912008-08-06 05:08:45 +0000821
Daniel Dunbar67aba792010-04-15 03:47:33 +0000822 // Get the source value, truncated to the width of the bit-field.
Daniel Dunbar9b1335e2008-11-19 09:36:46 +0000823 llvm::Value *SrcVal = Src.getScalarVal();
Anders Carlsson8345a702010-04-17 21:52:22 +0000824
825 if (Ty->isBooleanType())
826 SrcVal = Builder.CreateIntCast(SrcVal, ResLTy, /*IsSigned=*/false);
827
Daniel Dunbar67aba792010-04-15 03:47:33 +0000828 SrcVal = Builder.CreateAnd(SrcVal, llvm::APInt::getLowBitsSet(ResSizeInBits,
829 Info.getSize()),
830 "bf.value");
Lauro Ramos Venancio09af71c2008-01-22 22:36:45 +0000831
Daniel Dunbar9b1335e2008-11-19 09:36:46 +0000832 // Return the new value of the bit-field, if requested.
833 if (Result) {
834 // Cast back to the proper type for result.
Daniel Dunbar67aba792010-04-15 03:47:33 +0000835 const llvm::Type *SrcTy = Src.getScalarVal()->getType();
836 llvm::Value *ReloadVal = Builder.CreateIntCast(SrcVal, SrcTy, false,
837 "bf.reload.val");
Daniel Dunbar9b1335e2008-11-19 09:36:46 +0000838
839 // Sign extend if necessary.
Daniel Dunbar67aba792010-04-15 03:47:33 +0000840 if (Info.isSigned()) {
841 unsigned ExtraBits = ResSizeInBits - Info.getSize();
842 if (ExtraBits)
843 ReloadVal = Builder.CreateAShr(Builder.CreateShl(ReloadVal, ExtraBits),
844 ExtraBits, "bf.reload.sext");
Daniel Dunbar9b1335e2008-11-19 09:36:46 +0000845 }
846
Daniel Dunbar67aba792010-04-15 03:47:33 +0000847 *Result = ReloadVal;
Daniel Dunbar9b1335e2008-11-19 09:36:46 +0000848 }
849
Daniel Dunbar67aba792010-04-15 03:47:33 +0000850 // Iterate over the components, writing each piece to memory.
851 for (unsigned i = 0, e = Info.getNumComponents(); i != e; ++i) {
852 const CGBitFieldInfo::AccessInfo &AI = Info.getComponent(i);
Eli Friedmanf2442dc2008-05-17 20:03:47 +0000853
Daniel Dunbar67aba792010-04-15 03:47:33 +0000854 // Get the field pointer.
855 llvm::Value *Ptr = Dst.getBitFieldBaseAddr();
Mike Stump4a3999f2009-09-09 13:00:44 +0000856
Daniel Dunbar67aba792010-04-15 03:47:33 +0000857 // Only offset by the field index if used, so that incoming values are not
858 // required to be structures.
859 if (AI.FieldIndex)
860 Ptr = Builder.CreateStructGEP(Ptr, AI.FieldIndex, "bf.field");
Mike Stump4a3999f2009-09-09 13:00:44 +0000861
Daniel Dunbar67aba792010-04-15 03:47:33 +0000862 // Offset by the byte offset, if used.
863 if (AI.FieldByteOffset) {
864 const llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(VMContext);
865 Ptr = Builder.CreateBitCast(Ptr, i8PTy);
866 Ptr = Builder.CreateConstGEP1_32(Ptr, AI.FieldByteOffset,"bf.field.offs");
867 }
Eli Friedmanf2442dc2008-05-17 20:03:47 +0000868
Daniel Dunbar67aba792010-04-15 03:47:33 +0000869 // Cast to the access type.
870 const llvm::Type *PTy = llvm::Type::getIntNPtrTy(VMContext, AI.AccessWidth,
871 Ty.getAddressSpace());
872 Ptr = Builder.CreateBitCast(Ptr, PTy);
Mike Stump4a3999f2009-09-09 13:00:44 +0000873
Daniel Dunbar67aba792010-04-15 03:47:33 +0000874 // Extract the piece of the bit-field value to write in this access, limited
875 // to the values that are part of this access.
876 llvm::Value *Val = SrcVal;
877 if (AI.TargetBitOffset)
878 Val = Builder.CreateLShr(Val, AI.TargetBitOffset);
879 Val = Builder.CreateAnd(Val, llvm::APInt::getLowBitsSet(ResSizeInBits,
880 AI.TargetBitWidth));
Mike Stump4a3999f2009-09-09 13:00:44 +0000881
Daniel Dunbar67aba792010-04-15 03:47:33 +0000882 // Extend or truncate to the access size.
883 const llvm::Type *AccessLTy =
884 llvm::Type::getIntNTy(VMContext, AI.AccessWidth);
885 if (ResSizeInBits < AI.AccessWidth)
886 Val = Builder.CreateZExt(Val, AccessLTy);
887 else if (ResSizeInBits > AI.AccessWidth)
888 Val = Builder.CreateTrunc(Val, AccessLTy);
Mike Stump4a3999f2009-09-09 13:00:44 +0000889
Daniel Dunbar67aba792010-04-15 03:47:33 +0000890 // Shift into the position in memory.
891 if (AI.FieldBitStart)
892 Val = Builder.CreateShl(Val, AI.FieldBitStart);
893
894 // If necessary, load and OR in bits that are outside of the bit-field.
895 if (AI.TargetBitWidth != AI.AccessWidth) {
896 llvm::LoadInst *Load = Builder.CreateLoad(Ptr, Dst.isVolatileQualified());
897 if (AI.AccessAlignment)
898 Load->setAlignment(AI.AccessAlignment);
899
900 // Compute the mask for zeroing the bits that are part of the bit-field.
901 llvm::APInt InvMask =
902 ~llvm::APInt::getBitsSet(AI.AccessWidth, AI.FieldBitStart,
903 AI.FieldBitStart + AI.TargetBitWidth);
904
905 // Apply the mask and OR in to the value to write.
906 Val = Builder.CreateOr(Builder.CreateAnd(Load, InvMask), Val);
907 }
908
909 // Write the value.
910 llvm::StoreInst *Store = Builder.CreateStore(Val, Ptr,
911 Dst.isVolatileQualified());
912 if (AI.AccessAlignment)
913 Store->setAlignment(AI.AccessAlignment);
Daniel Dunbaread7c912008-08-06 05:08:45 +0000914 }
Lauro Ramos Venancio09af71c2008-01-22 22:36:45 +0000915}
916
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +0000917void CodeGenFunction::EmitStoreThroughPropertyRefLValue(RValue Src,
918 LValue Dst,
919 QualType Ty) {
920 EmitObjCPropertySet(Dst.getPropertyRefExpr(), Src);
921}
922
Fariborz Jahanian9ac53512008-11-22 22:30:21 +0000923void CodeGenFunction::EmitStoreThroughKVCRefLValue(RValue Src,
924 LValue Dst,
925 QualType Ty) {
926 EmitObjCPropertySet(Dst.getKVCRefExpr(), Src);
927}
928
Nate Begemance4d7fc2008-04-18 23:10:10 +0000929void CodeGenFunction::EmitStoreThroughExtVectorComponentLValue(RValue Src,
930 LValue Dst,
931 QualType Ty) {
Chris Lattner41d480e2007-08-03 16:28:33 +0000932 // This access turns into a read/modify/write of the vector. Load the input
933 // value now.
Eli Friedman327944b2008-06-13 23:01:12 +0000934 llvm::Value *Vec = Builder.CreateLoad(Dst.getExtVectorAddr(),
935 Dst.isVolatileQualified(), "tmp");
Nate Begemanf322eab2008-05-09 06:41:27 +0000936 const llvm::Constant *Elts = Dst.getExtVectorElts();
Mike Stump4a3999f2009-09-09 13:00:44 +0000937
Chris Lattner4647a212007-08-31 22:49:20 +0000938 llvm::Value *SrcVal = Src.getScalarVal();
Mike Stump4a3999f2009-09-09 13:00:44 +0000939
John McCall9dd450b2009-09-21 23:43:11 +0000940 if (const VectorType *VTy = Ty->getAs<VectorType>()) {
Chris Lattner3a44aa72007-08-03 16:37:04 +0000941 unsigned NumSrcElts = VTy->getNumElements();
Nate Begemanb699c9b2009-01-18 06:42:49 +0000942 unsigned NumDstElts =
943 cast<llvm::VectorType>(Vec->getType())->getNumElements();
944 if (NumDstElts == NumSrcElts) {
Mike Stump4a3999f2009-09-09 13:00:44 +0000945 // Use shuffle vector is the src and destination are the same number of
946 // elements and restore the vector mask since it is on the side it will be
947 // stored.
Nate Begemanea12f6e2009-06-26 21:12:50 +0000948 llvm::SmallVector<llvm::Constant*, 4> Mask(NumDstElts);
Nate Begemanb699c9b2009-01-18 06:42:49 +0000949 for (unsigned i = 0; i != NumSrcElts; ++i) {
950 unsigned InIdx = getAccessedFieldNo(i, Elts);
Owen Anderson41a75022009-08-13 21:57:51 +0000951 Mask[InIdx] = llvm::ConstantInt::get(
952 llvm::Type::getInt32Ty(VMContext), i);
Nate Begemanb699c9b2009-01-18 06:42:49 +0000953 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000954
Owen Anderson3cc120a2009-07-28 21:22:35 +0000955 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
Nate Begemanb699c9b2009-01-18 06:42:49 +0000956 Vec = Builder.CreateShuffleVector(SrcVal,
Owen Anderson7ec07a52009-07-30 23:11:26 +0000957 llvm::UndefValue::get(Vec->getType()),
Nate Begemanb699c9b2009-01-18 06:42:49 +0000958 MaskV, "tmp");
Mike Stump658fe022009-07-30 22:28:39 +0000959 } else if (NumDstElts > NumSrcElts) {
Nate Begemanb699c9b2009-01-18 06:42:49 +0000960 // Extended the source vector to the same length and then shuffle it
961 // into the destination.
962 // FIXME: since we're shuffling with undef, can we just use the indices
963 // into that? This could be simpler.
964 llvm::SmallVector<llvm::Constant*, 4> ExtMask;
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000965 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
Nate Begemanb699c9b2009-01-18 06:42:49 +0000966 unsigned i;
967 for (i = 0; i != NumSrcElts; ++i)
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000968 ExtMask.push_back(llvm::ConstantInt::get(Int32Ty, i));
Nate Begemanb699c9b2009-01-18 06:42:49 +0000969 for (; i != NumDstElts; ++i)
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000970 ExtMask.push_back(llvm::UndefValue::get(Int32Ty));
Owen Anderson3cc120a2009-07-28 21:22:35 +0000971 llvm::Value *ExtMaskV = llvm::ConstantVector::get(&ExtMask[0],
Nate Begemanb699c9b2009-01-18 06:42:49 +0000972 ExtMask.size());
Mike Stump4a3999f2009-09-09 13:00:44 +0000973 llvm::Value *ExtSrcVal =
Daniel Dunbar3d926cb2009-02-17 18:31:04 +0000974 Builder.CreateShuffleVector(SrcVal,
Owen Anderson7ec07a52009-07-30 23:11:26 +0000975 llvm::UndefValue::get(SrcVal->getType()),
Daniel Dunbar3d926cb2009-02-17 18:31:04 +0000976 ExtMaskV, "tmp");
Nate Begemanb699c9b2009-01-18 06:42:49 +0000977 // build identity
978 llvm::SmallVector<llvm::Constant*, 4> Mask;
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000979 for (unsigned i = 0; i != NumDstElts; ++i)
980 Mask.push_back(llvm::ConstantInt::get(Int32Ty, i));
981
Nate Begemanb699c9b2009-01-18 06:42:49 +0000982 // modify when what gets shuffled in
983 for (unsigned i = 0; i != NumSrcElts; ++i) {
984 unsigned Idx = getAccessedFieldNo(i, Elts);
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000985 Mask[Idx] = llvm::ConstantInt::get(Int32Ty, i+NumDstElts);
Nate Begemanb699c9b2009-01-18 06:42:49 +0000986 }
Owen Anderson3cc120a2009-07-28 21:22:35 +0000987 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
Nate Begemanb699c9b2009-01-18 06:42:49 +0000988 Vec = Builder.CreateShuffleVector(Vec, ExtSrcVal, MaskV, "tmp");
Mike Stump658fe022009-07-30 22:28:39 +0000989 } else {
Nate Begemanb699c9b2009-01-18 06:42:49 +0000990 // We should never shorten the vector
991 assert(0 && "unexpected shorten vector length");
Chris Lattner3a44aa72007-08-03 16:37:04 +0000992 }
993 } else {
994 // If the Src is a scalar (not a vector) it must be updating one element.
Dan Gohman75d69da2008-05-22 00:50:06 +0000995 unsigned InIdx = getAccessedFieldNo(0, Elts);
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000996 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
997 llvm::Value *Elt = llvm::ConstantInt::get(Int32Ty, InIdx);
Chris Lattner41d480e2007-08-03 16:28:33 +0000998 Vec = Builder.CreateInsertElement(Vec, SrcVal, Elt, "tmp");
Chris Lattner41d480e2007-08-03 16:28:33 +0000999 }
Mike Stump4a3999f2009-09-09 13:00:44 +00001000
Eli Friedman327944b2008-06-13 23:01:12 +00001001 Builder.CreateStore(Vec, Dst.getExtVectorAddr(), Dst.isVolatileQualified());
Chris Lattner41d480e2007-08-03 16:28:33 +00001002}
1003
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +00001004// setObjCGCLValueClass - sets class of he lvalue for the purpose of
1005// generating write-barries API. It is currently a global, ivar,
1006// or neither.
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001007static void setObjCGCLValueClass(const ASTContext &Ctx, const Expr *E,
1008 LValue &LV) {
Fariborz Jahanian71848a32009-09-21 23:03:37 +00001009 if (Ctx.getLangOptions().getGCMode() == LangOptions::NonGC)
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +00001010 return;
1011
Fariborz Jahaniande1d3242009-09-16 23:11:23 +00001012 if (isa<ObjCIvarRefExpr>(E)) {
1013 LV.SetObjCIvar(LV, true);
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00001014 ObjCIvarRefExpr *Exp = cast<ObjCIvarRefExpr>(const_cast<Expr*>(E));
1015 LV.setBaseIvarExp(Exp->getBase());
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +00001016 LV.SetObjCArray(LV, E->getType()->isArrayType());
Fariborz Jahaniande1d3242009-09-16 23:11:23 +00001017 return;
1018 }
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001019
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +00001020 if (const DeclRefExpr *Exp = dyn_cast<DeclRefExpr>(E)) {
1021 if (const VarDecl *VD = dyn_cast<VarDecl>(Exp->getDecl())) {
1022 if ((VD->isBlockVarDecl() && !VD->hasLocalStorage()) ||
1023 VD->isFileVarDecl())
1024 LV.SetGlobalObjCRef(LV, true);
1025 }
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +00001026 LV.SetObjCArray(LV, E->getType()->isArrayType());
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001027 return;
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +00001028 }
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001029
1030 if (const UnaryOperator *Exp = dyn_cast<UnaryOperator>(E)) {
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +00001031 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001032 return;
1033 }
1034
1035 if (const ParenExpr *Exp = dyn_cast<ParenExpr>(E)) {
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +00001036 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
Fariborz Jahaniane01e4342009-09-30 17:10:29 +00001037 if (LV.isObjCIvar()) {
1038 // If cast is to a structure pointer, follow gcc's behavior and make it
1039 // a non-ivar write-barrier.
1040 QualType ExpTy = E->getType();
1041 if (ExpTy->isPointerType())
1042 ExpTy = ExpTy->getAs<PointerType>()->getPointeeType();
1043 if (ExpTy->isRecordType())
1044 LV.SetObjCIvar(LV, false);
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001045 }
1046 return;
Fariborz Jahaniane01e4342009-09-30 17:10:29 +00001047 }
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001048 if (const ImplicitCastExpr *Exp = dyn_cast<ImplicitCastExpr>(E)) {
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +00001049 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001050 return;
1051 }
1052
1053 if (const CStyleCastExpr *Exp = dyn_cast<CStyleCastExpr>(E)) {
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +00001054 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001055 return;
1056 }
1057
1058 if (const ArraySubscriptExpr *Exp = dyn_cast<ArraySubscriptExpr>(E)) {
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +00001059 setObjCGCLValueClass(Ctx, Exp->getBase(), LV);
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +00001060 if (LV.isObjCIvar() && !LV.isObjCArray())
Fariborz Jahanian2e32ddc2009-09-18 00:04:00 +00001061 // Using array syntax to assigning to what an ivar points to is not
1062 // same as assigning to the ivar itself. {id *Names;} Names[i] = 0;
1063 LV.SetObjCIvar(LV, false);
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +00001064 else if (LV.isGlobalObjCRef() && !LV.isObjCArray())
1065 // Using array syntax to assigning to what global points to is not
1066 // same as assigning to the global itself. {id *G;} G[i] = 0;
1067 LV.SetGlobalObjCRef(LV, false);
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001068 return;
Fariborz Jahanian2e32ddc2009-09-18 00:04:00 +00001069 }
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001070
1071 if (const MemberExpr *Exp = dyn_cast<MemberExpr>(E)) {
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +00001072 setObjCGCLValueClass(Ctx, Exp->getBase(), LV);
Fariborz Jahanian2e32ddc2009-09-18 00:04:00 +00001073 // We don't know if member is an 'ivar', but this flag is looked at
1074 // only in the context of LV.isObjCIvar().
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +00001075 LV.SetObjCArray(LV, E->getType()->isArrayType());
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001076 return;
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +00001077 }
1078}
1079
Anders Carlssonea4c30b2009-11-07 23:06:58 +00001080static LValue EmitGlobalVarDeclLValue(CodeGenFunction &CGF,
1081 const Expr *E, const VarDecl *VD) {
Daniel Dunbar7e215ea2009-11-08 09:46:46 +00001082 assert((VD->hasExternalStorage() || VD->isFileVarDecl()) &&
Anders Carlssonea4c30b2009-11-07 23:06:58 +00001083 "Var decl must have external storage or be a file var decl!");
1084
1085 llvm::Value *V = CGF.CGM.GetAddrOfGlobalVar(VD);
1086 if (VD->getType()->isReferenceType())
1087 V = CGF.Builder.CreateLoad(V, "tmp");
1088 LValue LV = LValue::MakeAddr(V, CGF.MakeQualifiers(E->getType()));
1089 setObjCGCLValueClass(CGF.getContext(), E, LV);
1090 return LV;
1091}
1092
Eli Friedmand15eb34d2009-11-26 06:08:14 +00001093static LValue EmitFunctionDeclLValue(CodeGenFunction &CGF,
1094 const Expr *E, const FunctionDecl *FD) {
1095 llvm::Value* V = CGF.CGM.GetAddrOfFunction(FD);
1096 if (!FD->hasPrototype()) {
1097 if (const FunctionProtoType *Proto =
1098 FD->getType()->getAs<FunctionProtoType>()) {
1099 // Ugly case: for a K&R-style definition, the type of the definition
1100 // isn't the same as the type of a use. Correct for this with a
1101 // bitcast.
1102 QualType NoProtoType =
1103 CGF.getContext().getFunctionNoProtoType(Proto->getResultType());
1104 NoProtoType = CGF.getContext().getPointerType(NoProtoType);
1105 V = CGF.Builder.CreateBitCast(V, CGF.ConvertType(NoProtoType), "tmp");
1106 }
1107 }
1108 return LValue::MakeAddr(V, CGF.MakeQualifiers(E->getType()));
1109}
1110
Chris Lattnerd7f58862007-06-02 05:24:33 +00001111LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) {
Anders Carlsson2ff6395d2009-11-07 22:53:10 +00001112 const NamedDecl *ND = E->getDecl();
Mike Stump4a3999f2009-09-09 13:00:44 +00001113
Rafael Espindola2e42fec2010-03-04 18:17:24 +00001114 if (ND->hasAttr<WeakRefAttr>()) {
1115 const ValueDecl* VD = cast<ValueDecl>(ND);
1116 llvm::Constant *Aliasee = CGM.GetWeakRefReference(VD);
1117
1118 Qualifiers Quals = MakeQualifiers(E->getType());
1119 LValue LV = LValue::MakeAddr(Aliasee, Quals);
1120
1121 return LV;
1122 }
1123
Anders Carlsson2ff6395d2009-11-07 22:53:10 +00001124 if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
Anders Carlsson2ff6395d2009-11-07 22:53:10 +00001125
1126 // Check if this is a global variable.
Anders Carlssonea4c30b2009-11-07 23:06:58 +00001127 if (VD->hasExternalStorage() || VD->isFileVarDecl())
1128 return EmitGlobalVarDeclLValue(*this, E, VD);
Anders Carlsson6eee9722009-11-07 22:46:42 +00001129
1130 bool NonGCable = VD->hasLocalStorage() && !VD->hasAttr<BlocksAttr>();
1131
1132 llvm::Value *V = LocalDeclMap[VD];
Fariborz Jahanian4d55b2d2010-04-19 18:15:02 +00001133 if (!V && getContext().getLangOptions().CPlusPlus &&
1134 VD->isStaticLocal())
1135 V = CGM.getStaticLocalDeclAddress(VD);
Anders Carlsson6eee9722009-11-07 22:46:42 +00001136 assert(V && "DeclRefExpr not entered in LocalDeclMap?");
1137
1138 Qualifiers Quals = MakeQualifiers(E->getType());
1139 // local variables do not get their gc attribute set.
1140 // local static?
1141 if (NonGCable) Quals.removeObjCGCAttr();
1142
1143 if (VD->hasAttr<BlocksAttr>()) {
1144 V = Builder.CreateStructGEP(V, 1, "forwarding");
Daniel Dunbarc76493a2009-11-29 21:23:36 +00001145 V = Builder.CreateLoad(V);
Anders Carlsson6eee9722009-11-07 22:46:42 +00001146 V = Builder.CreateStructGEP(V, getByRefValueLLVMField(VD),
1147 VD->getNameAsString());
1148 }
1149 if (VD->getType()->isReferenceType())
1150 V = Builder.CreateLoad(V, "tmp");
Anders Carlssonea4c30b2009-11-07 23:06:58 +00001151 LValue LV = LValue::MakeAddr(V, Quals);
Anders Carlsson6eee9722009-11-07 22:46:42 +00001152 LValue::SetObjCNonGC(LV, NonGCable);
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +00001153 setObjCGCLValueClass(getContext(), E, LV);
Fariborz Jahanian003e8302008-11-20 00:15:42 +00001154 return LV;
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001155 }
1156
Eli Friedmand15eb34d2009-11-26 06:08:14 +00001157 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
1158 return EmitFunctionDeclLValue(*this, E, FD);
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001159
Anders Carlsson259688c2010-02-02 03:37:46 +00001160 // FIXME: the qualifier check does not seem sufficient here
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001161 if (E->getQualifier()) {
Anders Carlsson259688c2010-02-02 03:37:46 +00001162 const FieldDecl *FD = cast<FieldDecl>(ND);
1163 llvm::Value *V = CGM.EmitPointerToDataMember(FD);
1164
1165 return LValue::MakeAddr(V, MakeQualifiers(FD->getType()));
Chris Lattner5696e7b2008-06-17 18:05:57 +00001166 }
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001167
Anders Carlsson2ff6395d2009-11-07 22:53:10 +00001168 assert(false && "Unhandled DeclRefExpr");
1169
1170 // an invalid LValue, but the assert will
1171 // ensure that this point is never reached.
Chris Lattner793d10c2007-09-16 19:23:47 +00001172 return LValue();
Chris Lattnerd7f58862007-06-02 05:24:33 +00001173}
Chris Lattnere47e4402007-06-01 18:02:12 +00001174
Mike Stump1db7d042009-02-28 09:07:16 +00001175LValue CodeGenFunction::EmitBlockDeclRefLValue(const BlockDeclRefExpr *E) {
John McCall8ccfcb52009-09-24 19:53:00 +00001176 return LValue::MakeAddr(GetAddrOfBlockDecl(E), MakeQualifiers(E->getType()));
Mike Stump1db7d042009-02-28 09:07:16 +00001177}
1178
Chris Lattner8394d792007-06-05 20:53:16 +00001179LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) {
1180 // __extension__ doesn't affect lvalue-ness.
1181 if (E->getOpcode() == UnaryOperator::Extension)
1182 return EmitLValue(E->getSubExpr());
Mike Stump4a3999f2009-09-09 13:00:44 +00001183
Chris Lattner0f398c42008-07-26 22:37:01 +00001184 QualType ExprTy = getContext().getCanonicalType(E->getSubExpr()->getType());
Chris Lattner595db862007-10-30 22:53:42 +00001185 switch (E->getOpcode()) {
1186 default: assert(0 && "Unknown unary operator lvalue!");
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001187 case UnaryOperator::Deref: {
1188 QualType T = E->getSubExpr()->getType()->getPointeeType();
1189 assert(!T.isNull() && "CodeGenFunction::EmitUnaryOpLValue: Illegal type");
Mike Stump4a3999f2009-09-09 13:00:44 +00001190
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001191 Qualifiers Quals = MakeQualifiers(T);
1192 Quals.setAddressSpace(ExprTy.getAddressSpace());
John McCall8ccfcb52009-09-24 19:53:00 +00001193
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001194 LValue LV = LValue::MakeAddr(EmitScalarExpr(E->getSubExpr()), Quals);
1195 // We should not generate __weak write barrier on indirect reference
1196 // of a pointer to object; as in void foo (__weak id *param); *param = 0;
1197 // But, we continue to generate __strong write barrier on indirect write
1198 // into a pointer to object.
1199 if (getContext().getLangOptions().ObjC1 &&
1200 getContext().getLangOptions().getGCMode() != LangOptions::NonGC &&
1201 LV.isObjCWeak())
1202 LValue::SetObjCNonGC(LV, !E->isOBJCGCCandidate(getContext()));
1203 return LV;
1204 }
Chris Lattner595db862007-10-30 22:53:42 +00001205 case UnaryOperator::Real:
Eli Friedmana72bf0f2009-11-09 04:20:47 +00001206 case UnaryOperator::Imag: {
Chris Lattner595db862007-10-30 22:53:42 +00001207 LValue LV = EmitLValue(E->getSubExpr());
Chris Lattner3e593cd2008-03-19 05:19:41 +00001208 unsigned Idx = E->getOpcode() == UnaryOperator::Imag;
1209 return LValue::MakeAddr(Builder.CreateStructGEP(LV.getAddress(),
Chris Lattner574dee62008-07-26 22:17:49 +00001210 Idx, "idx"),
John McCall8ccfcb52009-09-24 19:53:00 +00001211 MakeQualifiers(ExprTy));
Chris Lattner595db862007-10-30 22:53:42 +00001212 }
Eli Friedmana72bf0f2009-11-09 04:20:47 +00001213 case UnaryOperator::PreInc:
Chris Lattnerbb8976e2010-01-09 21:44:40 +00001214 case UnaryOperator::PreDec: {
1215 LValue LV = EmitLValue(E->getSubExpr());
1216 bool isInc = E->getOpcode() == UnaryOperator::PreInc;
1217
1218 if (E->getType()->isAnyComplexType())
1219 EmitComplexPrePostIncDec(E, LV, isInc, true/*isPre*/);
1220 else
1221 EmitScalarPrePostIncDec(E, LV, isInc, true/*isPre*/);
1222 return LV;
1223 }
Eli Friedmana72bf0f2009-11-09 04:20:47 +00001224 }
Chris Lattner8394d792007-06-05 20:53:16 +00001225}
1226
Chris Lattner4347e3692007-06-06 04:54:52 +00001227LValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) {
John McCall8ccfcb52009-09-24 19:53:00 +00001228 return LValue::MakeAddr(CGM.GetAddrOfConstantStringFromLiteral(E),
1229 Qualifiers());
Chris Lattner4347e3692007-06-06 04:54:52 +00001230}
1231
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00001232LValue CodeGenFunction::EmitObjCEncodeExprLValue(const ObjCEncodeExpr *E) {
John McCall8ccfcb52009-09-24 19:53:00 +00001233 return LValue::MakeAddr(CGM.GetAddrOfConstantStringFromObjCEncode(E),
1234 Qualifiers());
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00001235}
1236
1237
Daniel Dunbarb3517472008-10-17 21:58:32 +00001238LValue CodeGenFunction::EmitPredefinedFunctionName(unsigned Type) {
Anders Carlsson625bfc82007-07-21 05:21:51 +00001239 std::string GlobalVarName;
Daniel Dunbarb3517472008-10-17 21:58:32 +00001240
1241 switch (Type) {
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001242 default: assert(0 && "Invalid type");
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00001243 case PredefinedExpr::Func:
1244 GlobalVarName = "__func__.";
1245 break;
1246 case PredefinedExpr::Function:
1247 GlobalVarName = "__FUNCTION__.";
1248 break;
1249 case PredefinedExpr::PrettyFunction:
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00001250 GlobalVarName = "__PRETTY_FUNCTION__.";
1251 break;
Anders Carlsson625bfc82007-07-21 05:21:51 +00001252 }
Daniel Dunbarb3517472008-10-17 21:58:32 +00001253
Daniel Dunbar0482cfd2009-09-12 23:06:21 +00001254 llvm::StringRef FnName = CurFn->getName();
1255 if (FnName.startswith("\01"))
1256 FnName = FnName.substr(1);
1257 GlobalVarName += FnName;
1258
Anders Carlsson2fb08242009-09-08 18:24:21 +00001259 std::string FunctionName =
Anders Carlsson5bd8d192010-02-11 18:20:28 +00001260 PredefinedExpr::ComputeName((PredefinedExpr::IdentType)Type, CurCodeDecl);
Daniel Dunbarb3517472008-10-17 21:58:32 +00001261
Mike Stump4a3999f2009-09-09 13:00:44 +00001262 llvm::Constant *C =
Daniel Dunbarb3517472008-10-17 21:58:32 +00001263 CGM.GetAddrOfConstantCString(FunctionName, GlobalVarName.c_str());
John McCall8ccfcb52009-09-24 19:53:00 +00001264 return LValue::MakeAddr(C, Qualifiers());
Daniel Dunbarb3517472008-10-17 21:58:32 +00001265}
1266
Mike Stump4a3999f2009-09-09 13:00:44 +00001267LValue CodeGenFunction::EmitPredefinedLValue(const PredefinedExpr *E) {
Daniel Dunbarb3517472008-10-17 21:58:32 +00001268 switch (E->getIdentType()) {
1269 default:
1270 return EmitUnsupportedLValue(E, "predefined expression");
1271 case PredefinedExpr::Func:
1272 case PredefinedExpr::Function:
1273 case PredefinedExpr::PrettyFunction:
1274 return EmitPredefinedFunctionName(E->getIdentType());
1275 }
Anders Carlsson625bfc82007-07-21 05:21:51 +00001276}
1277
Mike Stumpcf16d2c2009-12-15 01:22:35 +00001278llvm::BasicBlock *CodeGenFunction::getTrapBB() {
Mike Stump9a4e0122009-12-15 00:59:40 +00001279 const CodeGenOptions &GCO = CGM.getCodeGenOpts();
1280
1281 // If we are not optimzing, don't collapse all calls to trap in the function
1282 // to the same call, that way, in the debugger they can see which operation
1283 // did in fact fail. If we are optimizing, we collpase all call to trap down
1284 // to just one per function to save on codesize.
1285 if (GCO.OptimizationLevel
1286 && TrapBB)
Mike Stumpe8c3b3e2009-12-15 00:35:12 +00001287 return TrapBB;
Mike Stumpd9546382009-12-12 01:27:46 +00001288
1289 llvm::BasicBlock *Cont = 0;
1290 if (HaveInsertPoint()) {
1291 Cont = createBasicBlock("cont");
1292 EmitBranch(Cont);
1293 }
Mike Stumpe8c3b3e2009-12-15 00:35:12 +00001294 TrapBB = createBasicBlock("trap");
1295 EmitBlock(TrapBB);
1296
1297 llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::trap, 0, 0);
1298 llvm::CallInst *TrapCall = Builder.CreateCall(F);
1299 TrapCall->setDoesNotReturn();
1300 TrapCall->setDoesNotThrow();
Mike Stumpd9546382009-12-12 01:27:46 +00001301 Builder.CreateUnreachable();
1302
1303 if (Cont)
1304 EmitBlock(Cont);
Mike Stumpe8c3b3e2009-12-15 00:35:12 +00001305 return TrapBB;
Mike Stumpd9546382009-12-12 01:27:46 +00001306}
1307
Chris Lattnerd9d2fb12007-06-08 23:31:14 +00001308LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) {
Ted Kremenekc81614d2007-08-20 16:18:38 +00001309 // The index must always be an integer, which is not an aggregate. Emit it.
Chris Lattner2da04b32007-08-24 05:35:26 +00001310 llvm::Value *Idx = EmitScalarExpr(E->getIdx());
Eli Friedman07bbeca2009-06-06 19:09:26 +00001311 QualType IdxTy = E->getIdx()->getType();
1312 bool IdxSigned = IdxTy->isSignedIntegerType();
1313
Chris Lattner08c4b9f2007-07-10 21:17:59 +00001314 // If the base is a vector type, then we are forming a vector element lvalue
1315 // with this subscript.
Eli Friedman327944b2008-06-13 23:01:12 +00001316 if (E->getBase()->getType()->isVectorType()) {
Chris Lattner08c4b9f2007-07-10 21:17:59 +00001317 // Emit the vector as an lvalue to get its address.
Eli Friedman327944b2008-06-13 23:01:12 +00001318 LValue LHS = EmitLValue(E->getBase());
Ted Kremenekc81614d2007-08-20 16:18:38 +00001319 assert(LHS.isSimple() && "Can only subscript lvalue vectors here!");
Mike Stump4a3999f2009-09-09 13:00:44 +00001320 Idx = Builder.CreateIntCast(Idx,
Owen Anderson41a75022009-08-13 21:57:51 +00001321 llvm::Type::getInt32Ty(VMContext), IdxSigned, "vidx");
Eli Friedman327944b2008-06-13 23:01:12 +00001322 return LValue::MakeVectorElt(LHS.getAddress(), Idx,
John McCall8ccfcb52009-09-24 19:53:00 +00001323 E->getBase()->getType().getCVRQualifiers());
Chris Lattner08c4b9f2007-07-10 21:17:59 +00001324 }
Mike Stump4a3999f2009-09-09 13:00:44 +00001325
Ted Kremenekc81614d2007-08-20 16:18:38 +00001326 // The base must be a pointer, which is not an aggregate. Emit it.
Chris Lattner2da04b32007-08-24 05:35:26 +00001327 llvm::Value *Base = EmitScalarExpr(E->getBase());
Mike Stump4a3999f2009-09-09 13:00:44 +00001328
Ted Kremenekc81614d2007-08-20 16:18:38 +00001329 // Extend or truncate the index type to 32 or 64-bits.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001330 unsigned IdxBitwidth = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
Sanjiv Gupta47425152009-04-24 02:40:57 +00001331 if (IdxBitwidth != LLVMPointerWidth)
Owen Anderson41a75022009-08-13 21:57:51 +00001332 Idx = Builder.CreateIntCast(Idx,
1333 llvm::IntegerType::get(VMContext, LLVMPointerWidth),
Chris Lattnerd9d2fb12007-06-08 23:31:14 +00001334 IdxSigned, "idxprom");
1335
Mike Stump3f6f9fe2009-12-16 02:57:00 +00001336 // FIXME: As llvm implements the object size checking, this can come out.
Mike Stumpd9546382009-12-12 01:27:46 +00001337 if (CatchUndefined) {
Mike Stump3f6f9fe2009-12-16 02:57:00 +00001338 if (const ImplicitCastExpr *ICE=dyn_cast<ImplicitCastExpr>(E->getBase())) {
Mike Stumpd9546382009-12-12 01:27:46 +00001339 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr())) {
1340 if (ICE->getCastKind() == CastExpr::CK_ArrayToPointerDecay) {
1341 if (const ConstantArrayType *CAT
1342 = getContext().getAsConstantArrayType(DRE->getType())) {
1343 llvm::APInt Size = CAT->getSize();
1344 llvm::BasicBlock *Cont = createBasicBlock("cont");
Mike Stump590d18f2009-12-14 22:14:31 +00001345 Builder.CreateCondBr(Builder.CreateICmpULE(Idx,
Mike Stumpd9546382009-12-12 01:27:46 +00001346 llvm::ConstantInt::get(Idx->getType(), Size)),
Mike Stumpe8c3b3e2009-12-15 00:35:12 +00001347 Cont, getTrapBB());
Mike Stumpf8858af2009-12-14 20:52:00 +00001348 EmitBlock(Cont);
Mike Stumpd9546382009-12-12 01:27:46 +00001349 }
1350 }
1351 }
1352 }
1353 }
1354
Mike Stump4a3999f2009-09-09 13:00:44 +00001355 // We know that the pointer points to a type of the correct size, unless the
1356 // size is a VLA or Objective-C interface.
Daniel Dunbaref2ffbc2009-04-25 05:08:32 +00001357 llvm::Value *Address = 0;
Mike Stump4a3999f2009-09-09 13:00:44 +00001358 if (const VariableArrayType *VAT =
Anders Carlsson3d312f82008-12-21 00:11:23 +00001359 getContext().getAsVariableArrayType(E->getType())) {
Chris Lattner19efdd62009-08-14 23:43:22 +00001360 llvm::Value *VLASize = GetVLASize(VAT);
Mike Stump4a3999f2009-09-09 13:00:44 +00001361
Anders Carlsson3d312f82008-12-21 00:11:23 +00001362 Idx = Builder.CreateMul(Idx, VLASize);
Mike Stump4a3999f2009-09-09 13:00:44 +00001363
Anders Carlssone0808df2008-12-21 03:44:36 +00001364 QualType BaseType = getContext().getBaseElementType(VAT);
Mike Stump4a3999f2009-09-09 13:00:44 +00001365
Ken Dyck40775002010-01-11 17:06:35 +00001366 CharUnits BaseTypeSize = getContext().getTypeSizeInChars(BaseType);
Anders Carlsson3d312f82008-12-21 00:11:23 +00001367 Idx = Builder.CreateUDiv(Idx,
Mike Stump4a3999f2009-09-09 13:00:44 +00001368 llvm::ConstantInt::get(Idx->getType(),
Ken Dyck40775002010-01-11 17:06:35 +00001369 BaseTypeSize.getQuantity()));
Dan Gohman43b44842009-08-12 00:33:55 +00001370 Address = Builder.CreateInBoundsGEP(Base, Idx, "arrayidx");
Mike Stump4a3999f2009-09-09 13:00:44 +00001371 } else if (const ObjCInterfaceType *OIT =
Daniel Dunbaref2ffbc2009-04-25 05:08:32 +00001372 dyn_cast<ObjCInterfaceType>(E->getType())) {
Mike Stump4a3999f2009-09-09 13:00:44 +00001373 llvm::Value *InterfaceSize =
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001374 llvm::ConstantInt::get(Idx->getType(),
Ken Dyck40775002010-01-11 17:06:35 +00001375 getContext().getTypeSizeInChars(OIT).getQuantity());
Mike Stump4a3999f2009-09-09 13:00:44 +00001376
Daniel Dunbaref2ffbc2009-04-25 05:08:32 +00001377 Idx = Builder.CreateMul(Idx, InterfaceSize);
1378
Benjamin Kramerabd5b902009-10-13 10:07:13 +00001379 const llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(VMContext);
Dan Gohman43b44842009-08-12 00:33:55 +00001380 Address = Builder.CreateGEP(Builder.CreateBitCast(Base, i8PTy),
Daniel Dunbaref2ffbc2009-04-25 05:08:32 +00001381 Idx, "arrayidx");
1382 Address = Builder.CreateBitCast(Address, Base->getType());
1383 } else {
Dan Gohman43b44842009-08-12 00:33:55 +00001384 Address = Builder.CreateInBoundsGEP(Base, Idx, "arrayidx");
Anders Carlsson3d312f82008-12-21 00:11:23 +00001385 }
Mike Stump4a3999f2009-09-09 13:00:44 +00001386
Steve Naroff7cae42b2009-07-10 23:34:53 +00001387 QualType T = E->getBase()->getType()->getPointeeType();
Mike Stump4a3999f2009-09-09 13:00:44 +00001388 assert(!T.isNull() &&
Steve Naroff7cae42b2009-07-10 23:34:53 +00001389 "CodeGenFunction::EmitArraySubscriptExpr(): Illegal base type");
Mike Stump4a3999f2009-09-09 13:00:44 +00001390
John McCall8ccfcb52009-09-24 19:53:00 +00001391 Qualifiers Quals = MakeQualifiers(T);
1392 Quals.setAddressSpace(E->getBase()->getType().getAddressSpace());
1393
1394 LValue LV = LValue::MakeAddr(Address, Quals);
Fariborz Jahaniana9fecf32009-02-21 23:37:19 +00001395 if (getContext().getLangOptions().ObjC1 &&
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +00001396 getContext().getLangOptions().getGCMode() != LangOptions::NonGC) {
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001397 LValue::SetObjCNonGC(LV, !E->isOBJCGCCandidate(getContext()));
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +00001398 setObjCGCLValueClass(getContext(), E, LV);
1399 }
Fariborz Jahaniana9fecf32009-02-21 23:37:19 +00001400 return LV;
Chris Lattnerd9d2fb12007-06-08 23:31:14 +00001401}
1402
Mike Stump4a3999f2009-09-09 13:00:44 +00001403static
Owen Anderson170229f2009-07-14 23:10:40 +00001404llvm::Constant *GenerateConstantVector(llvm::LLVMContext &VMContext,
1405 llvm::SmallVector<unsigned, 4> &Elts) {
Chris Lattner4e1a3232009-12-23 21:31:11 +00001406 llvm::SmallVector<llvm::Constant*, 4> CElts;
Mike Stump4a3999f2009-09-09 13:00:44 +00001407
Nate Begemand3862152008-05-13 21:03:02 +00001408 for (unsigned i = 0, e = Elts.size(); i != e; ++i)
Owen Anderson41a75022009-08-13 21:57:51 +00001409 CElts.push_back(llvm::ConstantInt::get(
1410 llvm::Type::getInt32Ty(VMContext), Elts[i]));
Nate Begemand3862152008-05-13 21:03:02 +00001411
Owen Anderson3cc120a2009-07-28 21:22:35 +00001412 return llvm::ConstantVector::get(&CElts[0], CElts.size());
Nate Begemand3862152008-05-13 21:03:02 +00001413}
1414
Chris Lattner9e751ca2007-08-02 23:37:31 +00001415LValue CodeGenFunction::
Nate Begemance4d7fc2008-04-18 23:10:10 +00001416EmitExtVectorElementExpr(const ExtVectorElementExpr *E) {
Chris Lattner4e1a3232009-12-23 21:31:11 +00001417 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
1418
Chris Lattner9e751ca2007-08-02 23:37:31 +00001419 // Emit the base vector as an l-value.
Chris Lattner6c7ce102009-02-16 21:11:58 +00001420 LValue Base;
1421
1422 // ExtVectorElementExpr's base can either be a vector or pointer to vector.
Chris Lattner4e1a3232009-12-23 21:31:11 +00001423 if (E->isArrow()) {
1424 // If it is a pointer to a vector, emit the address and form an lvalue with
1425 // it.
Chris Lattnerb8211f62009-02-16 22:14:05 +00001426 llvm::Value *Ptr = EmitScalarExpr(E->getBase());
Chris Lattner4e1a3232009-12-23 21:31:11 +00001427 const PointerType *PT = E->getBase()->getType()->getAs<PointerType>();
John McCall8ccfcb52009-09-24 19:53:00 +00001428 Qualifiers Quals = MakeQualifiers(PT->getPointeeType());
1429 Quals.removeObjCGCAttr();
1430 Base = LValue::MakeAddr(Ptr, Quals);
Chris Lattner4e1a3232009-12-23 21:31:11 +00001431 } else if (E->getBase()->isLvalue(getContext()) == Expr::LV_Valid) {
1432 // Otherwise, if the base is an lvalue ( as in the case of foo.x.x),
1433 // emit the base as an lvalue.
1434 assert(E->getBase()->getType()->isVectorType());
1435 Base = EmitLValue(E->getBase());
1436 } else {
1437 // Otherwise, the base is a normal rvalue (as in (V+V).x), emit it as such.
Daniel Dunbar5b901952010-01-04 18:02:28 +00001438 assert(E->getBase()->getType()->getAs<VectorType>() &&
1439 "Result must be a vector");
Chris Lattner4e1a3232009-12-23 21:31:11 +00001440 llvm::Value *Vec = EmitScalarExpr(E->getBase());
1441
Chris Lattnerf0a9ba32009-12-23 21:33:41 +00001442 // Store the vector to memory (because LValue wants an address).
Daniel Dunbara7566f12010-02-09 02:48:28 +00001443 llvm::Value *VecMem = CreateMemTemp(E->getBase()->getType());
Chris Lattner4e1a3232009-12-23 21:31:11 +00001444 Builder.CreateStore(Vec, VecMem);
Chris Lattnerf0a9ba32009-12-23 21:33:41 +00001445 Base = LValue::MakeAddr(VecMem, Qualifiers());
Chris Lattner4e1a3232009-12-23 21:31:11 +00001446 }
1447
Nate Begemand3862152008-05-13 21:03:02 +00001448 // Encode the element access list into a vector of unsigned indices.
1449 llvm::SmallVector<unsigned, 4> Indices;
1450 E->getEncodedElementAccess(Indices);
1451
1452 if (Base.isSimple()) {
Owen Anderson170229f2009-07-14 23:10:40 +00001453 llvm::Constant *CV = GenerateConstantVector(VMContext, Indices);
Eli Friedman327944b2008-06-13 23:01:12 +00001454 return LValue::MakeExtVectorElt(Base.getAddress(), CV,
John McCall8ccfcb52009-09-24 19:53:00 +00001455 Base.getVRQualifiers());
Nate Begemand3862152008-05-13 21:03:02 +00001456 }
1457 assert(Base.isExtVectorElt() && "Can only subscript lvalue vec elts here!");
1458
1459 llvm::Constant *BaseElts = Base.getExtVectorElts();
1460 llvm::SmallVector<llvm::Constant *, 4> CElts;
1461
1462 for (unsigned i = 0, e = Indices.size(); i != e; ++i) {
1463 if (isa<llvm::ConstantAggregateZero>(BaseElts))
Chris Lattner5e71d432009-10-28 05:12:07 +00001464 CElts.push_back(llvm::ConstantInt::get(Int32Ty, 0));
Nate Begemand3862152008-05-13 21:03:02 +00001465 else
Chris Lattner5e71d432009-10-28 05:12:07 +00001466 CElts.push_back(cast<llvm::Constant>(BaseElts->getOperand(Indices[i])));
Nate Begemand3862152008-05-13 21:03:02 +00001467 }
Owen Anderson3cc120a2009-07-28 21:22:35 +00001468 llvm::Constant *CV = llvm::ConstantVector::get(&CElts[0], CElts.size());
Eli Friedman327944b2008-06-13 23:01:12 +00001469 return LValue::MakeExtVectorElt(Base.getExtVectorAddr(), CV,
John McCall8ccfcb52009-09-24 19:53:00 +00001470 Base.getVRQualifiers());
Chris Lattner9e751ca2007-08-02 23:37:31 +00001471}
1472
Devang Patel30efa2e2007-10-23 20:28:39 +00001473LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) {
Fariborz Jahanian10bec102009-02-21 00:30:43 +00001474 bool isNonGC = false;
Devang Pateld68df202007-10-24 22:26:28 +00001475 Expr *BaseExpr = E->getBase();
Devang Pateld68df202007-10-24 22:26:28 +00001476 llvm::Value *BaseValue = NULL;
John McCall8ccfcb52009-09-24 19:53:00 +00001477 Qualifiers BaseQuals;
Eli Friedman327944b2008-06-13 23:01:12 +00001478
Chris Lattner4e4186b2007-12-02 18:52:07 +00001479 // If this is s.x, emit s as an lvalue. If it is s->x, emit s as a scalar.
Devang Patelb37b12d2007-12-11 21:33:16 +00001480 if (E->isArrow()) {
Devang Patel7718d7a2007-10-26 18:15:21 +00001481 BaseValue = EmitScalarExpr(BaseExpr);
Mike Stump4a3999f2009-09-09 13:00:44 +00001482 const PointerType *PTy =
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001483 BaseExpr->getType()->getAs<PointerType>();
John McCall8ccfcb52009-09-24 19:53:00 +00001484 BaseQuals = PTy->getPointeeType().getQualifiers();
Fariborz Jahanian1a504772009-09-01 17:02:21 +00001485 } else if (isa<ObjCPropertyRefExpr>(BaseExpr->IgnoreParens()) ||
1486 isa<ObjCImplicitSetterGetterRefExpr>(
1487 BaseExpr->IgnoreParens())) {
Fariborz Jahanian30e78642009-01-12 23:27:26 +00001488 RValue RV = EmitObjCPropertyGet(BaseExpr);
1489 BaseValue = RV.getAggregateAddr();
John McCall8ccfcb52009-09-24 19:53:00 +00001490 BaseQuals = BaseExpr->getType().getQualifiers();
Chris Lattnere084c012009-02-16 22:25:49 +00001491 } else {
Chris Lattner4e4186b2007-12-02 18:52:07 +00001492 LValue BaseLV = EmitLValue(BaseExpr);
Fariborz Jahanian10bec102009-02-21 00:30:43 +00001493 if (BaseLV.isNonGC())
1494 isNonGC = true;
Chris Lattner4e4186b2007-12-02 18:52:07 +00001495 // FIXME: this isn't right for bitfields.
1496 BaseValue = BaseLV.getAddress();
Fariborz Jahanian82e28742009-07-29 00:44:13 +00001497 QualType BaseTy = BaseExpr->getType();
John McCall8ccfcb52009-09-24 19:53:00 +00001498 BaseQuals = BaseTy.getQualifiers();
Chris Lattner4e4186b2007-12-02 18:52:07 +00001499 }
Devang Patel30efa2e2007-10-23 20:28:39 +00001500
Anders Carlssonea4c30b2009-11-07 23:06:58 +00001501 NamedDecl *ND = E->getMemberDecl();
1502 if (FieldDecl *Field = dyn_cast<FieldDecl>(ND)) {
Anders Carlsson5d8645b2010-01-29 05:05:36 +00001503 LValue LV = EmitLValueForField(BaseValue, Field,
Anders Carlssonea4c30b2009-11-07 23:06:58 +00001504 BaseQuals.getCVRQualifiers());
1505 LValue::SetObjCNonGC(LV, isNonGC);
1506 setObjCGCLValueClass(getContext(), E, LV);
1507 return LV;
1508 }
1509
Anders Carlsson5bbdc9f2009-11-07 23:16:50 +00001510 if (VarDecl *VD = dyn_cast<VarDecl>(ND))
1511 return EmitGlobalVarDeclLValue(*this, E, VD);
Eli Friedmand15eb34d2009-11-26 06:08:14 +00001512
1513 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
1514 return EmitFunctionDeclLValue(*this, E, FD);
1515
Anders Carlssonea4c30b2009-11-07 23:06:58 +00001516 assert(false && "Unhandled member declaration!");
1517 return LValue();
Eli Friedmana62f3e12008-02-09 08:50:58 +00001518}
Devang Patel30efa2e2007-10-23 20:28:39 +00001519
Fariborz Jahanianb517e902008-12-15 20:35:07 +00001520LValue CodeGenFunction::EmitLValueForBitfield(llvm::Value* BaseValue,
Anders Carlssoncfd30122009-11-17 03:57:07 +00001521 const FieldDecl* Field,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001522 unsigned CVRQualifiers) {
Daniel Dunbar034299e2010-03-31 01:09:11 +00001523 const CGRecordLayout &RL =
1524 CGM.getTypes().getCGRecordLayout(Field->getParent());
Daniel Dunbarcd3d5e72010-04-05 16:20:44 +00001525 const CGBitFieldInfo &Info = RL.getBitFieldInfo(Field);
Daniel Dunbarc75c8bd2010-04-08 02:59:45 +00001526 return LValue::MakeBitfield(BaseValue, Info,
Daniel Dunbardc406b82010-04-05 21:36:35 +00001527 Field->getType().getCVRQualifiers()|CVRQualifiers);
Fariborz Jahanianb517e902008-12-15 20:35:07 +00001528}
1529
Eli Friedmana62f3e12008-02-09 08:50:58 +00001530LValue CodeGenFunction::EmitLValueForField(llvm::Value* BaseValue,
Anders Carlssoncfd30122009-11-17 03:57:07 +00001531 const FieldDecl* Field,
Mike Stump11289f42009-09-09 15:08:12 +00001532 unsigned CVRQualifiers) {
Fariborz Jahanianb517e902008-12-15 20:35:07 +00001533 if (Field->isBitField())
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001534 return EmitLValueForBitfield(BaseValue, Field, CVRQualifiers);
Mike Stump4a3999f2009-09-09 13:00:44 +00001535
Daniel Dunbar034299e2010-03-31 01:09:11 +00001536 const CGRecordLayout &RL =
1537 CGM.getTypes().getCGRecordLayout(Field->getParent());
1538 unsigned idx = RL.getLLVMFieldNo(Field);
Fariborz Jahanianb517e902008-12-15 20:35:07 +00001539 llvm::Value *V = Builder.CreateStructGEP(BaseValue, idx, "tmp");
Eli Friedman133e8042008-05-29 11:33:25 +00001540
Devang Pateled93c3c2007-10-26 19:42:18 +00001541 // Match union field type.
Anders Carlsson5d8645b2010-01-29 05:05:36 +00001542 if (Field->getParent()->isUnion()) {
Mike Stump4a3999f2009-09-09 13:00:44 +00001543 const llvm::Type *FieldTy =
Eli Friedman327944b2008-06-13 23:01:12 +00001544 CGM.getTypes().ConvertTypeForMem(Field->getType());
Mike Stump4a3999f2009-09-09 13:00:44 +00001545 const llvm::PointerType * BaseTy =
Devang Patelffe1e212007-10-30 20:59:40 +00001546 cast<llvm::PointerType>(BaseValue->getType());
Eli Friedman9a5ffcb2008-05-21 13:24:44 +00001547 unsigned AS = BaseTy->getAddressSpace();
Mike Stump4a3999f2009-09-09 13:00:44 +00001548 V = Builder.CreateBitCast(V,
1549 llvm::PointerType::get(FieldTy, AS),
Eli Friedman9a5ffcb2008-05-21 13:24:44 +00001550 "tmp");
Devang Pateled93c3c2007-10-26 19:42:18 +00001551 }
Eli Friedmanf7f9f682009-05-30 21:09:44 +00001552 if (Field->getType()->isReferenceType())
1553 V = Builder.CreateLoad(V, "tmp");
John McCall8ccfcb52009-09-24 19:53:00 +00001554
1555 Qualifiers Quals = MakeQualifiers(Field->getType());
1556 Quals.addCVRQualifiers(CVRQualifiers);
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +00001557 // __weak attribute on a field is ignored.
John McCall8ccfcb52009-09-24 19:53:00 +00001558 if (Quals.getObjCGCAttr() == Qualifiers::Weak)
1559 Quals.removeObjCGCAttr();
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +00001560
John McCall8ccfcb52009-09-24 19:53:00 +00001561 return LValue::MakeAddr(V, Quals);
Devang Patel30efa2e2007-10-23 20:28:39 +00001562}
1563
Anders Carlssondb78f0a2010-01-29 05:24:29 +00001564LValue
1565CodeGenFunction::EmitLValueForFieldInitialization(llvm::Value* BaseValue,
1566 const FieldDecl* Field,
1567 unsigned CVRQualifiers) {
1568 QualType FieldType = Field->getType();
1569
1570 if (!FieldType->isReferenceType())
1571 return EmitLValueForField(BaseValue, Field, CVRQualifiers);
1572
Daniel Dunbar034299e2010-03-31 01:09:11 +00001573 const CGRecordLayout &RL =
1574 CGM.getTypes().getCGRecordLayout(Field->getParent());
1575 unsigned idx = RL.getLLVMFieldNo(Field);
Anders Carlssondb78f0a2010-01-29 05:24:29 +00001576 llvm::Value *V = Builder.CreateStructGEP(BaseValue, idx, "tmp");
1577
1578 assert(!FieldType.getObjCGCAttr() && "fields cannot have GC attrs");
1579
1580 return LValue::MakeAddr(V, MakeQualifiers(FieldType));
1581}
1582
Chris Lattner28bcf1a2009-03-18 18:28:57 +00001583LValue CodeGenFunction::EmitCompoundLiteralLValue(const CompoundLiteralExpr* E){
Daniel Dunbar27bacaf2010-02-16 19:43:39 +00001584 llvm::Value *DeclPtr = CreateMemTemp(E->getType(), ".compoundliteral");
Eli Friedman9fd8b682008-05-13 23:18:27 +00001585 const Expr* InitExpr = E->getInitializer();
John McCall8ccfcb52009-09-24 19:53:00 +00001586 LValue Result = LValue::MakeAddr(DeclPtr, MakeQualifiers(E->getType()));
Eli Friedman9fd8b682008-05-13 23:18:27 +00001587
John McCall21886962010-04-21 10:05:39 +00001588 EmitAnyExprToMem(InitExpr, DeclPtr, /*Volatile*/ false);
Eli Friedman9fd8b682008-05-13 23:18:27 +00001589
1590 return Result;
1591}
1592
Anders Carlsson1450adb2009-09-15 16:35:24 +00001593LValue
1594CodeGenFunction::EmitConditionalOperatorLValue(const ConditionalOperator* E) {
1595 if (E->isLvalue(getContext()) == Expr::LV_Valid) {
Eli Friedman2e06e8b2009-12-25 05:29:40 +00001596 if (int Cond = ConstantFoldsToSimpleInteger(E->getCond())) {
1597 Expr *Live = Cond == 1 ? E->getLHS() : E->getRHS();
1598 if (Live)
1599 return EmitLValue(Live);
1600 }
1601
1602 if (!E->getLHS())
1603 return EmitUnsupportedLValue(E, "conditional operator with missing LHS");
1604
Anders Carlsson1450adb2009-09-15 16:35:24 +00001605 llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true");
1606 llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false");
1607 llvm::BasicBlock *ContBlock = createBasicBlock("cond.end");
1608
Eli Friedmanb8841af2009-12-25 06:17:05 +00001609 EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock);
Anders Carlsson1450adb2009-09-15 16:35:24 +00001610
Anders Carlsson9b942c62010-02-04 17:26:01 +00001611 // Any temporaries created here are conditional.
1612 BeginConditionalBranch();
Anders Carlsson1450adb2009-09-15 16:35:24 +00001613 EmitBlock(LHSBlock);
Anders Carlsson1450adb2009-09-15 16:35:24 +00001614 LValue LHS = EmitLValue(E->getLHS());
Anders Carlsson9b942c62010-02-04 17:26:01 +00001615 EndConditionalBranch();
1616
Anders Carlsson1450adb2009-09-15 16:35:24 +00001617 if (!LHS.isSimple())
1618 return EmitUnsupportedLValue(E, "conditional operator");
1619
Daniel Dunbara7566f12010-02-09 02:48:28 +00001620 // FIXME: We shouldn't need an alloca for this.
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001621 llvm::Value *Temp = CreateTempAlloca(LHS.getAddress()->getType(),"condtmp");
Anders Carlsson1450adb2009-09-15 16:35:24 +00001622 Builder.CreateStore(LHS.getAddress(), Temp);
1623 EmitBranch(ContBlock);
1624
Anders Carlsson9b942c62010-02-04 17:26:01 +00001625 // Any temporaries created here are conditional.
1626 BeginConditionalBranch();
Anders Carlsson1450adb2009-09-15 16:35:24 +00001627 EmitBlock(RHSBlock);
1628 LValue RHS = EmitLValue(E->getRHS());
Anders Carlsson9b942c62010-02-04 17:26:01 +00001629 EndConditionalBranch();
Anders Carlsson1450adb2009-09-15 16:35:24 +00001630 if (!RHS.isSimple())
1631 return EmitUnsupportedLValue(E, "conditional operator");
1632
1633 Builder.CreateStore(RHS.getAddress(), Temp);
1634 EmitBranch(ContBlock);
1635
1636 EmitBlock(ContBlock);
1637
1638 Temp = Builder.CreateLoad(Temp, "lv");
John McCall8ccfcb52009-09-24 19:53:00 +00001639 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
Anders Carlsson1450adb2009-09-15 16:35:24 +00001640 }
1641
Daniel Dunbarbf1fe8c2009-03-24 02:38:23 +00001642 // ?: here should be an aggregate.
Mike Stump4a3999f2009-09-09 13:00:44 +00001643 assert((hasAggregateLLVMType(E->getType()) &&
Daniel Dunbarbf1fe8c2009-03-24 02:38:23 +00001644 !E->getType()->isAnyComplexType()) &&
1645 "Unexpected conditional operator!");
1646
Daniel Dunbard0bc7b92010-02-05 19:38:31 +00001647 return EmitAggExprToLValue(E);
Daniel Dunbarbf1fe8c2009-03-24 02:38:23 +00001648}
1649
Mike Stump65511702009-11-16 06:50:58 +00001650/// EmitCastLValue - Casts are never lvalues unless that cast is a dynamic_cast.
1651/// If the cast is a dynamic_cast, we can have the usual lvalue result,
1652/// otherwise if a cast is needed by the code generator in an lvalue context,
1653/// then it must mean that we need the address of an aggregate in order to
1654/// access one of its fields. This can happen for all the reasons that casts
1655/// are permitted with aggregate result, including noop aggregate casts, and
1656/// cast from scalar to union.
Chris Lattner28bcf1a2009-03-18 18:28:57 +00001657LValue CodeGenFunction::EmitCastLValue(const CastExpr *E) {
Anders Carlssond95f9602009-09-12 16:16:49 +00001658 switch (E->getCastKind()) {
1659 default:
Eli Friedman8c98dff2009-11-16 05:48:01 +00001660 return EmitUnsupportedLValue(E, "unexpected cast lvalue");
1661
Mike Stump65511702009-11-16 06:50:58 +00001662 case CastExpr::CK_Dynamic: {
1663 LValue LV = EmitLValue(E->getSubExpr());
1664 llvm::Value *V = LV.getAddress();
1665 const CXXDynamicCastExpr *DCE = cast<CXXDynamicCastExpr>(E);
1666 return LValue::MakeAddr(EmitDynamicCast(V, DCE),
1667 MakeQualifiers(E->getType()));
1668 }
1669
Anders Carlssond95f9602009-09-12 16:16:49 +00001670 case CastExpr::CK_NoOp:
1671 case CastExpr::CK_ConstructorConversion:
1672 case CastExpr::CK_UserDefinedConversion:
Fariborz Jahanian2b9fc832009-12-15 21:34:52 +00001673 case CastExpr::CK_AnyPointerToObjCPointerCast:
Chris Lattner28bcf1a2009-03-18 18:28:57 +00001674 return EmitLValue(E->getSubExpr());
Anders Carlssond95f9602009-09-12 16:16:49 +00001675
John McCalld9c7c6562010-03-30 23:58:03 +00001676 case CastExpr::CK_UncheckedDerivedToBase:
Anders Carlssond95f9602009-09-12 16:16:49 +00001677 case CastExpr::CK_DerivedToBase: {
1678 const RecordType *DerivedClassTy =
1679 E->getSubExpr()->getType()->getAs<RecordType>();
1680 CXXRecordDecl *DerivedClassDecl =
1681 cast<CXXRecordDecl>(DerivedClassTy->getDecl());
Chris Lattner28bcf1a2009-03-18 18:28:57 +00001682
Anders Carlssond95f9602009-09-12 16:16:49 +00001683 const RecordType *BaseClassTy = E->getType()->getAs<RecordType>();
1684 CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseClassTy->getDecl());
1685
1686 LValue LV = EmitLValue(E->getSubExpr());
1687
1688 // Perform the derived-to-base conversion
1689 llvm::Value *Base =
Anders Carlsson8c793172009-11-23 17:57:54 +00001690 GetAddressOfBaseClass(LV.getAddress(), DerivedClassDecl,
1691 BaseClassDecl, /*NullCheckValue=*/false);
Anders Carlssond95f9602009-09-12 16:16:49 +00001692
John McCall8ccfcb52009-09-24 19:53:00 +00001693 return LValue::MakeAddr(Base, MakeQualifiers(E->getType()));
Anders Carlssond95f9602009-09-12 16:16:49 +00001694 }
Daniel Dunbar9c4e4652010-02-05 20:02:42 +00001695 case CastExpr::CK_ToUnion:
1696 return EmitAggExprToLValue(E);
Eli Friedman8c98dff2009-11-16 05:48:01 +00001697 case CastExpr::CK_BaseToDerived: {
Anders Carlsson8c793172009-11-23 17:57:54 +00001698 const RecordType *BaseClassTy =
1699 E->getSubExpr()->getType()->getAs<RecordType>();
1700 CXXRecordDecl *BaseClassDecl =
1701 cast<CXXRecordDecl>(BaseClassTy->getDecl());
1702
1703 const RecordType *DerivedClassTy = E->getType()->getAs<RecordType>();
1704 CXXRecordDecl *DerivedClassDecl =
1705 cast<CXXRecordDecl>(DerivedClassTy->getDecl());
1706
1707 LValue LV = EmitLValue(E->getSubExpr());
1708
1709 // Perform the base-to-derived conversion
1710 llvm::Value *Derived =
1711 GetAddressOfDerivedClass(LV.getAddress(), BaseClassDecl,
1712 DerivedClassDecl, /*NullCheckValue=*/false);
1713
1714 return LValue::MakeAddr(Derived, MakeQualifiers(E->getType()));
Eli Friedman8c98dff2009-11-16 05:48:01 +00001715 }
Anders Carlsson50cb3212009-11-14 21:21:42 +00001716 case CastExpr::CK_BitCast: {
Eli Friedman8c98dff2009-11-16 05:48:01 +00001717 // This must be a reinterpret_cast (or c-style equivalent).
1718 const ExplicitCastExpr *CE = cast<ExplicitCastExpr>(E);
Anders Carlsson50cb3212009-11-14 21:21:42 +00001719
1720 LValue LV = EmitLValue(E->getSubExpr());
1721 llvm::Value *V = Builder.CreateBitCast(LV.getAddress(),
1722 ConvertType(CE->getTypeAsWritten()));
1723 return LValue::MakeAddr(V, MakeQualifiers(E->getType()));
1724 }
Anders Carlssond95f9602009-09-12 16:16:49 +00001725 }
Chris Lattner28bcf1a2009-03-18 18:28:57 +00001726}
1727
Fariborz Jahaniane4d94ce2009-10-20 23:29:04 +00001728LValue CodeGenFunction::EmitNullInitializationLValue(
1729 const CXXZeroInitValueExpr *E) {
1730 QualType Ty = E->getType();
Daniel Dunbara7566f12010-02-09 02:48:28 +00001731 LValue LV = LValue::MakeAddr(CreateMemTemp(Ty), MakeQualifiers(Ty));
1732 EmitMemSetToZero(LV.getAddress(), Ty);
1733 return LV;
Fariborz Jahaniane4d94ce2009-10-20 23:29:04 +00001734}
1735
Chris Lattnere47e4402007-06-01 18:02:12 +00001736//===--------------------------------------------------------------------===//
1737// Expression Emission
1738//===--------------------------------------------------------------------===//
1739
Chris Lattner76ba8492007-08-20 22:37:10 +00001740
Anders Carlsson17490832009-12-24 20:40:36 +00001741RValue CodeGenFunction::EmitCallExpr(const CallExpr *E,
1742 ReturnValueSlot ReturnValue) {
Daniel Dunbarcdbb5e32009-02-20 18:06:48 +00001743 // Builtins never have block type.
Daniel Dunbarbb197e42009-01-09 16:50:52 +00001744 if (E->getCallee()->getType()->isBlockPointerType())
Anders Carlssonbfb36712009-12-24 21:13:40 +00001745 return EmitBlockCallExpr(E, ReturnValue);
Daniel Dunbarbb197e42009-01-09 16:50:52 +00001746
Anders Carlssone5fd6f22009-04-03 22:50:24 +00001747 if (const CXXMemberCallExpr *CE = dyn_cast<CXXMemberCallExpr>(E))
Anders Carlssonbfb36712009-12-24 21:13:40 +00001748 return EmitCXXMemberCallExpr(CE, ReturnValue);
Mike Stump4a3999f2009-09-09 13:00:44 +00001749
Daniel Dunbarcdbb5e32009-02-20 18:06:48 +00001750 const Decl *TargetDecl = 0;
Daniel Dunbar27032de2009-02-20 19:34:33 +00001751 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E->getCallee())) {
1752 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CE->getSubExpr())) {
1753 TargetDecl = DRE->getDecl();
1754 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(TargetDecl))
Douglas Gregor15fc9562009-09-12 00:22:50 +00001755 if (unsigned builtinID = FD->getBuiltinID())
Daniel Dunbar27032de2009-02-20 19:34:33 +00001756 return EmitBuiltinExpr(FD, builtinID, E);
Daniel Dunbarcdbb5e32009-02-20 18:06:48 +00001757 }
1758 }
1759
Chris Lattner4ca97c32009-06-13 00:26:38 +00001760 if (const CXXOperatorCallExpr *CE = dyn_cast<CXXOperatorCallExpr>(E))
Anders Carlsson4034a952009-05-27 04:18:27 +00001761 if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(TargetDecl))
Anders Carlssonbfb36712009-12-24 21:13:40 +00001762 return EmitCXXOperatorMemberCallExpr(CE, MD, ReturnValue);
Mike Stump4a3999f2009-09-09 13:00:44 +00001763
Eli Friedman8aaff692009-12-08 02:09:46 +00001764 if (isa<CXXPseudoDestructorExpr>(E->getCallee()->IgnoreParens())) {
Douglas Gregorad8a3362009-09-04 17:36:40 +00001765 // C++ [expr.pseudo]p1:
Mike Stump4a3999f2009-09-09 13:00:44 +00001766 // The result shall only be used as the operand for the function call
Douglas Gregorad8a3362009-09-04 17:36:40 +00001767 // operator (), and the result of such a call has type void. The only
1768 // effect is the evaluation of the postfix-expression before the dot or
1769 // arrow.
1770 EmitScalarExpr(E->getCallee());
1771 return RValue::get(0);
1772 }
Mike Stump4a3999f2009-09-09 13:00:44 +00001773
Chris Lattner2da04b32007-08-24 05:35:26 +00001774 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
Anders Carlsson17490832009-12-24 20:40:36 +00001775 return EmitCall(E->getCallee()->getType(), Callee, ReturnValue,
Anders Carlsson3a9463b2009-05-27 01:22:39 +00001776 E->arg_begin(), E->arg_end(), TargetDecl);
Chris Lattner9e47ead2007-08-31 04:44:06 +00001777}
1778
Daniel Dunbar8cde00a2008-09-04 03:20:13 +00001779LValue CodeGenFunction::EmitBinaryOperatorLValue(const BinaryOperator *E) {
Chris Lattnere541ea32009-05-12 21:28:12 +00001780 // Comma expressions just emit their LHS then their RHS as an l-value.
1781 if (E->getOpcode() == BinaryOperator::Comma) {
1782 EmitAnyExpr(E->getLHS());
Eli Friedman5445f6e2009-12-07 20:18:11 +00001783 EnsureInsertPoint();
Chris Lattnere541ea32009-05-12 21:28:12 +00001784 return EmitLValue(E->getRHS());
1785 }
Mike Stump4a3999f2009-09-09 13:00:44 +00001786
Fariborz Jahanian038374f2009-10-26 21:58:25 +00001787 if (E->getOpcode() == BinaryOperator::PtrMemD ||
1788 E->getOpcode() == BinaryOperator::PtrMemI)
Fariborz Jahanianffba6622009-10-22 22:57:31 +00001789 return EmitPointerToDataMemberBinaryExpr(E);
1790
Daniel Dunbar8cde00a2008-09-04 03:20:13 +00001791 // Can only get l-value for binary operator expressions which are a
1792 // simple assignment of aggregate type.
1793 if (E->getOpcode() != BinaryOperator::Assign)
1794 return EmitUnsupportedLValue(E, "binary l-value expression");
1795
Anders Carlsson0999aaf2009-10-19 18:28:22 +00001796 if (!hasAggregateLLVMType(E->getType())) {
1797 // Emit the LHS as an l-value.
1798 LValue LV = EmitLValue(E->getLHS());
1799
1800 llvm::Value *RHS = EmitScalarExpr(E->getRHS());
1801 EmitStoreOfScalar(RHS, LV.getAddress(), LV.isVolatileQualified(),
1802 E->getType());
1803 return LV;
1804 }
1805
Daniel Dunbard0bc7b92010-02-05 19:38:31 +00001806 return EmitAggExprToLValue(E);
Daniel Dunbar8cde00a2008-09-04 03:20:13 +00001807}
1808
Christopher Lambd91c3d42007-12-29 05:02:41 +00001809LValue CodeGenFunction::EmitCallExprLValue(const CallExpr *E) {
Christopher Lambd91c3d42007-12-29 05:02:41 +00001810 RValue RV = EmitCallExpr(E);
Anders Carlsson4ae70ff2009-05-27 01:45:47 +00001811
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001812 if (!RV.isScalar())
1813 return LValue::MakeAddr(RV.getAggregateAddr(),MakeQualifiers(E->getType()));
1814
1815 assert(E->getCallReturnType()->isReferenceType() &&
1816 "Can't have a scalar return unless the return type is a "
1817 "reference type!");
Mike Stump4a3999f2009-09-09 13:00:44 +00001818
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001819 return LValue::MakeAddr(RV.getScalarVal(), MakeQualifiers(E->getType()));
Christopher Lambd91c3d42007-12-29 05:02:41 +00001820}
1821
Daniel Dunbar8d9dc4a2009-02-11 20:59:32 +00001822LValue CodeGenFunction::EmitVAArgExprLValue(const VAArgExpr *E) {
1823 // FIXME: This shouldn't require another copy.
Daniel Dunbard0bc7b92010-02-05 19:38:31 +00001824 return EmitAggExprToLValue(E);
Daniel Dunbar8d9dc4a2009-02-11 20:59:32 +00001825}
1826
Anders Carlsson3be22e22009-05-30 23:23:33 +00001827LValue CodeGenFunction::EmitCXXConstructLValue(const CXXConstructExpr *E) {
Daniel Dunbara7566f12010-02-09 02:48:28 +00001828 llvm::Value *Temp = CreateMemTemp(E->getType(), "tmp");
Anders Carlsson3be22e22009-05-30 23:23:33 +00001829 EmitCXXConstructExpr(Temp, E);
John McCall8ccfcb52009-09-24 19:53:00 +00001830 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
Anders Carlsson3be22e22009-05-30 23:23:33 +00001831}
1832
Anders Carlssonfd2af0c2009-05-30 23:30:54 +00001833LValue
Mike Stumpc9b231c2009-11-15 08:09:41 +00001834CodeGenFunction::EmitCXXTypeidLValue(const CXXTypeidExpr *E) {
1835 llvm::Value *Temp = EmitCXXTypeidExpr(E);
1836 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
1837}
1838
1839LValue
Anders Carlssonfd2af0c2009-05-30 23:30:54 +00001840CodeGenFunction::EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr *E) {
1841 LValue LV = EmitLValue(E->getSubExpr());
Anders Carlsson8eb93e72009-05-31 00:34:10 +00001842 PushCXXTemporary(E->getTemporary(), LV.getAddress());
Anders Carlssonfd2af0c2009-05-30 23:30:54 +00001843 return LV;
1844}
1845
Daniel Dunbarc8317a42008-08-23 10:51:21 +00001846LValue CodeGenFunction::EmitObjCMessageExprLValue(const ObjCMessageExpr *E) {
1847 // Can only get l-value for message expression returning aggregate type
1848 RValue RV = EmitObjCMessageExpr(E);
1849 // FIXME: can this be volatile?
John McCall8ccfcb52009-09-24 19:53:00 +00001850 return LValue::MakeAddr(RV.getAggregateAddr(), MakeQualifiers(E->getType()));
Daniel Dunbarc8317a42008-08-23 10:51:21 +00001851}
1852
Daniel Dunbar722f4242009-04-22 05:08:15 +00001853llvm::Value *CodeGenFunction::EmitIvarOffset(const ObjCInterfaceDecl *Interface,
Daniel Dunbar1c64e5d2008-09-24 04:00:38 +00001854 const ObjCIvarDecl *Ivar) {
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001855 return CGM.getObjCRuntime().EmitIvarOffset(*this, Interface, Ivar);
Daniel Dunbar1c64e5d2008-09-24 04:00:38 +00001856}
1857
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00001858LValue CodeGenFunction::EmitLValueForIvar(QualType ObjectTy,
1859 llvm::Value *BaseValue,
Daniel Dunbar1c64e5d2008-09-24 04:00:38 +00001860 const ObjCIvarDecl *Ivar,
1861 unsigned CVRQualifiers) {
Chris Lattnerc4688d22009-04-17 17:44:48 +00001862 return CGM.getObjCRuntime().EmitObjCValueForIvar(*this, ObjectTy, BaseValue,
Daniel Dunbar9ebf9512009-04-21 01:19:28 +00001863 Ivar, CVRQualifiers);
Daniel Dunbar1c64e5d2008-09-24 04:00:38 +00001864}
1865
1866LValue CodeGenFunction::EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E) {
Anders Carlssonc13b85a2008-08-25 01:53:23 +00001867 // FIXME: A lot of the code below could be shared with EmitMemberExpr.
1868 llvm::Value *BaseValue = 0;
1869 const Expr *BaseExpr = E->getBase();
John McCall8ccfcb52009-09-24 19:53:00 +00001870 Qualifiers BaseQuals;
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00001871 QualType ObjectTy;
Anders Carlssonc13b85a2008-08-25 01:53:23 +00001872 if (E->isArrow()) {
1873 BaseValue = EmitScalarExpr(BaseExpr);
Steve Naroff7cae42b2009-07-10 23:34:53 +00001874 ObjectTy = BaseExpr->getType()->getPointeeType();
John McCall8ccfcb52009-09-24 19:53:00 +00001875 BaseQuals = ObjectTy.getQualifiers();
Anders Carlssonc13b85a2008-08-25 01:53:23 +00001876 } else {
1877 LValue BaseLV = EmitLValue(BaseExpr);
1878 // FIXME: this isn't right for bitfields.
1879 BaseValue = BaseLV.getAddress();
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00001880 ObjectTy = BaseExpr->getType();
John McCall8ccfcb52009-09-24 19:53:00 +00001881 BaseQuals = ObjectTy.getQualifiers();
Anders Carlssonc13b85a2008-08-25 01:53:23 +00001882 }
Daniel Dunbar1c64e5d2008-09-24 04:00:38 +00001883
Fariborz Jahaniande1d3242009-09-16 23:11:23 +00001884 LValue LV =
John McCall8ccfcb52009-09-24 19:53:00 +00001885 EmitLValueForIvar(ObjectTy, BaseValue, E->getDecl(),
1886 BaseQuals.getCVRQualifiers());
Fariborz Jahaniande1d3242009-09-16 23:11:23 +00001887 setObjCGCLValueClass(getContext(), E, LV);
1888 return LV;
Chris Lattner4bd55962008-03-30 23:03:07 +00001889}
1890
Mike Stump4a3999f2009-09-09 13:00:44 +00001891LValue
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +00001892CodeGenFunction::EmitObjCPropertyRefLValue(const ObjCPropertyRefExpr *E) {
Mike Stump4a3999f2009-09-09 13:00:44 +00001893 // This is a special l-value that just issues sends when we load or store
1894 // through it.
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +00001895 return LValue::MakePropertyRef(E, E->getType().getCVRQualifiers());
1896}
1897
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001898LValue CodeGenFunction::EmitObjCKVCRefLValue(
Fariborz Jahanian9a846652009-08-20 17:02:02 +00001899 const ObjCImplicitSetterGetterRefExpr *E) {
Mike Stump4a3999f2009-09-09 13:00:44 +00001900 // This is a special l-value that just issues sends when we load or store
1901 // through it.
Fariborz Jahanian9ac53512008-11-22 22:30:21 +00001902 return LValue::MakeKVCRef(E, E->getType().getCVRQualifiers());
1903}
1904
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001905LValue CodeGenFunction::EmitObjCSuperExprLValue(const ObjCSuperExpr *E) {
Douglas Gregor8ea1f532008-11-04 14:56:14 +00001906 return EmitUnsupportedLValue(E, "use of super");
1907}
1908
Chris Lattnera4185c52009-04-25 19:35:26 +00001909LValue CodeGenFunction::EmitStmtExprLValue(const StmtExpr *E) {
Chris Lattnera4185c52009-04-25 19:35:26 +00001910 // Can only get l-value for message expression returning aggregate type
1911 RValue RV = EmitAnyExprToTemp(E);
John McCall8ccfcb52009-09-24 19:53:00 +00001912 return LValue::MakeAddr(RV.getAggregateAddr(), MakeQualifiers(E->getType()));
Chris Lattnera4185c52009-04-25 19:35:26 +00001913}
1914
Anders Carlsson0435ed52009-12-24 19:08:58 +00001915RValue CodeGenFunction::EmitCall(QualType CalleeType, llvm::Value *Callee,
Anders Carlsson17490832009-12-24 20:40:36 +00001916 ReturnValueSlot ReturnValue,
Anders Carlsson3a9463b2009-05-27 01:22:39 +00001917 CallExpr::const_arg_iterator ArgBeg,
1918 CallExpr::const_arg_iterator ArgEnd,
1919 const Decl *TargetDecl) {
Mike Stump4a3999f2009-09-09 13:00:44 +00001920 // Get the actual function type. The callee type will always be a pointer to
1921 // function type or a block pointer type.
1922 assert(CalleeType->isFunctionPointerType() &&
Anders Carlssond8db8532009-04-07 18:53:02 +00001923 "Call must have function pointer type!");
1924
John McCall6fd4c232009-10-23 08:22:42 +00001925 CalleeType = getContext().getCanonicalType(CalleeType);
1926
John McCallab26cfa2010-02-05 21:31:56 +00001927 const FunctionType *FnType
1928 = cast<FunctionType>(cast<PointerType>(CalleeType)->getPointeeType());
1929 QualType ResultType = FnType->getResultType();
Daniel Dunbarc722b852008-08-30 03:02:31 +00001930
1931 CallArgList Args;
John McCall6fd4c232009-10-23 08:22:42 +00001932 EmitCallArgs(Args, dyn_cast<FunctionProtoType>(FnType), ArgBeg, ArgEnd);
Daniel Dunbarc722b852008-08-30 03:02:31 +00001933
John McCallab26cfa2010-02-05 21:31:56 +00001934 return EmitCall(CGM.getTypes().getFunctionInfo(Args, FnType),
Anders Carlsson17490832009-12-24 20:40:36 +00001935 Callee, ReturnValue, Args, TargetDecl);
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001936}
Fariborz Jahanianffba6622009-10-22 22:57:31 +00001937
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001938LValue CodeGenFunction::
1939EmitPointerToDataMemberBinaryExpr(const BinaryOperator *E) {
Eli Friedman928a5672009-11-18 05:01:17 +00001940 llvm::Value *BaseV;
Fariborz Jahanian038374f2009-10-26 21:58:25 +00001941 if (E->getOpcode() == BinaryOperator::PtrMemI)
Eli Friedman928a5672009-11-18 05:01:17 +00001942 BaseV = EmitScalarExpr(E->getLHS());
1943 else
1944 BaseV = EmitLValue(E->getLHS()).getAddress();
Fariborz Jahanianffba6622009-10-22 22:57:31 +00001945 const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(getLLVMContext());
1946 BaseV = Builder.CreateBitCast(BaseV, i8Ty);
Eli Friedman928a5672009-11-18 05:01:17 +00001947 llvm::Value *OffsetV = EmitScalarExpr(E->getRHS());
Fariborz Jahanianffba6622009-10-22 22:57:31 +00001948 llvm::Value *AddV = Builder.CreateInBoundsGEP(BaseV, OffsetV, "add.ptr");
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001949
Fariborz Jahanianffba6622009-10-22 22:57:31 +00001950 QualType Ty = E->getRHS()->getType();
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001951 Ty = Ty->getAs<MemberPointerType>()->getPointeeType();
1952
1953 const llvm::Type *PType = ConvertType(getContext().getPointerType(Ty));
Fariborz Jahanianffba6622009-10-22 22:57:31 +00001954 AddV = Builder.CreateBitCast(AddV, PType);
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001955 return LValue::MakeAddr(AddV, MakeQualifiers(Ty));
Fariborz Jahanianffba6622009-10-22 22:57:31 +00001956}
1957