blob: cfb5af724bf12b4be9ac796d1ad0bbe59ce15990 [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"
Chandler Carruth85098242010-06-15 23:19:56 +000022#include "clang/Frontend/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
Douglas Gregor7c38f152010-05-20 08:36:28 +0000138/// \brief An adjustment to be made to the temporary created when emitting a
139/// reference binding, which accesses a particular subobject of that temporary.
140struct SubobjectAdjustment {
141 enum { DerivedToBaseAdjustment, FieldAdjustment } Kind;
142
143 union {
144 struct {
145 const CXXBaseSpecifierArray *BasePath;
146 const CXXRecordDecl *DerivedClass;
147 } DerivedToBase;
148
149 struct {
150 FieldDecl *Field;
151 unsigned CVRQualifiers;
152 } Field;
153 };
154
155 SubobjectAdjustment(const CXXBaseSpecifierArray *BasePath,
156 const CXXRecordDecl *DerivedClass)
157 : Kind(DerivedToBaseAdjustment)
158 {
159 DerivedToBase.BasePath = BasePath;
160 DerivedToBase.DerivedClass = DerivedClass;
161 }
162
163 SubobjectAdjustment(FieldDecl *Field, unsigned CVRQualifiers)
164 : Kind(FieldAdjustment)
165 {
166 this->Field.Field = Field;
167 this->Field.CVRQualifiers = CVRQualifiers;
168 }
169};
170
Anders Carlsson6f5a0152009-05-20 00:24:07 +0000171RValue CodeGenFunction::EmitReferenceBindingToExpr(const Expr* E,
Anders Carlsson5b106a72009-08-16 07:36:22 +0000172 bool IsInitializer) {
Anders Carlsson69c2c4b2009-10-18 23:09:21 +0000173 bool ShouldDestroyTemporaries = false;
174 unsigned OldNumLiveTemporaries = 0;
Eli Friedman357e8c92009-12-19 00:20:10 +0000175
176 if (const CXXDefaultArgExpr *DAE = dyn_cast<CXXDefaultArgExpr>(E))
177 E = DAE->getExpr();
178
Anders Carlsson66413c22009-10-15 00:51:46 +0000179 if (const CXXExprWithTemporaries *TE = dyn_cast<CXXExprWithTemporaries>(E)) {
Anders Carlsson6e997b22009-12-15 20:51:39 +0000180 ShouldDestroyTemporaries = true;
181
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000182 // Keep track of the current cleanup stack depth.
Anders Carlsson6e997b22009-12-15 20:51:39 +0000183 OldNumLiveTemporaries = LiveTemporaries.size();
Anders Carlsson66413c22009-10-15 00:51:46 +0000184
Anders Carlsson69c2c4b2009-10-18 23:09:21 +0000185 E = TE->getSubExpr();
Anders Carlsson66413c22009-10-15 00:51:46 +0000186 }
187
Eli Friedmanc21cb442009-05-20 02:31:19 +0000188 RValue Val;
189 if (E->isLvalue(getContext()) == Expr::LV_Valid) {
Anders Carlsson7d4c0832009-05-20 00:36:58 +0000190 // Emit the expr as an lvalue.
191 LValue LV = EmitLValue(E);
Anders Carlsson824e0612010-02-04 17:32:58 +0000192 if (LV.isSimple()) {
193 if (ShouldDestroyTemporaries) {
194 // Pop temporaries.
195 while (LiveTemporaries.size() > OldNumLiveTemporaries)
196 PopCXXTemporary();
197 }
198
Eli Friedmanc21cb442009-05-20 02:31:19 +0000199 return RValue::get(LV.getAddress());
Anders Carlsson824e0612010-02-04 17:32:58 +0000200 }
201
Eli Friedmanc21cb442009-05-20 02:31:19 +0000202 Val = EmitLoadOfLValue(LV, E->getType());
Anders Carlsson69c2c4b2009-10-18 23:09:21 +0000203
204 if (ShouldDestroyTemporaries) {
205 // Pop temporaries.
206 while (LiveTemporaries.size() > OldNumLiveTemporaries)
207 PopCXXTemporary();
208 }
Eli Friedmanc21cb442009-05-20 02:31:19 +0000209 } else {
Douglas Gregor7c38f152010-05-20 08:36:28 +0000210 QualType ResultTy = E->getType();
Anders Carlsson66413c22009-10-15 00:51:46 +0000211
Douglas Gregor7c38f152010-05-20 08:36:28 +0000212 llvm::SmallVector<SubobjectAdjustment, 2> Adjustments;
213 do {
Douglas Gregoraae38d62010-05-22 05:17:18 +0000214 if (const ParenExpr *PE = dyn_cast<ParenExpr>(E)) {
215 E = PE->getSubExpr();
216 continue;
217 }
218
219 if (const CastExpr *CE = dyn_cast<CastExpr>(E)) {
220 if ((CE->getCastKind() == CastExpr::CK_DerivedToBase ||
221 CE->getCastKind() == CastExpr::CK_UncheckedDerivedToBase) &&
222 E->getType()->isRecordType()) {
Douglas Gregor7c38f152010-05-20 08:36:28 +0000223 E = CE->getSubExpr();
224 CXXRecordDecl *Derived
225 = cast<CXXRecordDecl>(E->getType()->getAs<RecordType>()->getDecl());
226 Adjustments.push_back(SubobjectAdjustment(&CE->getBasePath(),
227 Derived));
228 continue;
229 }
Douglas Gregoraae38d62010-05-22 05:17:18 +0000230
231 if (CE->getCastKind() == CastExpr::CK_NoOp) {
232 E = CE->getSubExpr();
233 continue;
234 }
235 } else if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
Douglas Gregor7c38f152010-05-20 08:36:28 +0000236 if (ME->getBase()->isLvalue(getContext()) != Expr::LV_Valid &&
237 ME->getBase()->getType()->isRecordType()) {
238 if (FieldDecl *Field = dyn_cast<FieldDecl>(ME->getMemberDecl())) {
239 E = ME->getBase();
240 Adjustments.push_back(SubobjectAdjustment(Field,
241 E->getType().getCVRQualifiers()));
242 continue;
243 }
244 }
Anders Carlsson66413c22009-10-15 00:51:46 +0000245 }
Douglas Gregoraae38d62010-05-22 05:17:18 +0000246
247 // Nothing changed.
248 break;
249 } while (true);
Anders Carlsson66413c22009-10-15 00:51:46 +0000250
Anders Carlsson5b106a72009-08-16 07:36:22 +0000251 Val = EmitAnyExprToTemp(E, /*IsAggLocVolatile=*/false,
252 IsInitializer);
Mike Stump4a3999f2009-09-09 13:00:44 +0000253
Anders Carlsson69c2c4b2009-10-18 23:09:21 +0000254 if (ShouldDestroyTemporaries) {
255 // Pop temporaries.
256 while (LiveTemporaries.size() > OldNumLiveTemporaries)
257 PopCXXTemporary();
258 }
259
Anders Carlsson3b848942009-08-16 17:54:29 +0000260 if (IsInitializer) {
261 // We might have to destroy the temporary variable.
262 if (const RecordType *RT = E->getType()->getAs<RecordType>()) {
263 if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
264 if (!ClassDecl->hasTrivialDestructor()) {
Mike Stump4a3999f2009-09-09 13:00:44 +0000265 const CXXDestructorDecl *Dtor =
Anders Carlsson3b848942009-08-16 17:54:29 +0000266 ClassDecl->getDestructor(getContext());
Mike Stump4a3999f2009-09-09 13:00:44 +0000267
Mike Stumpaff69af2009-12-09 03:35:49 +0000268 {
Anders Carlsson0c6a7d82009-12-11 01:00:09 +0000269 DelayedCleanupBlock Scope(*this);
Mike Stumpaff69af2009-12-09 03:35:49 +0000270 EmitCXXDestructorCall(Dtor, Dtor_Complete,
Anders Carlssonf8a71f02010-05-02 23:29:11 +0000271 /*ForVirtualBase=*/false,
Mike Stumpaff69af2009-12-09 03:35:49 +0000272 Val.getAggregateAddr());
Anders Carlsson0c6a7d82009-12-11 01:00:09 +0000273
274 // Make sure to jump to the exit block.
275 EmitBranch(Scope.getCleanupExitBlock());
Mike Stumpaff69af2009-12-09 03:35:49 +0000276 }
277 if (Exceptions) {
278 EHCleanupBlock Cleanup(*this);
279 EmitCXXDestructorCall(Dtor, Dtor_Complete,
Anders Carlssonf8a71f02010-05-02 23:29:11 +0000280 /*ForVirtualBase=*/false,
Mike Stumpaff69af2009-12-09 03:35:49 +0000281 Val.getAggregateAddr());
282 }
Anders Carlsson3b848942009-08-16 17:54:29 +0000283 }
Anders Carlssonb80760b2009-08-16 17:50:25 +0000284 }
285 }
286 }
Anders Carlsson66413c22009-10-15 00:51:46 +0000287
Douglas Gregor7c38f152010-05-20 08:36:28 +0000288 // Check if need to perform derived-to-base casts and/or field accesses, to
289 // get from the temporary object we created (and, potentially, for which we
290 // extended the lifetime) to the subobject we're binding the reference to.
291 if (!Adjustments.empty()) {
292 llvm::Value *Object = Val.getAggregateAddr();
293 for (unsigned I = Adjustments.size(); I != 0; --I) {
294 SubobjectAdjustment &Adjustment = Adjustments[I-1];
295 switch (Adjustment.Kind) {
296 case SubobjectAdjustment::DerivedToBaseAdjustment:
297 Object = GetAddressOfBaseClass(Object,
298 Adjustment.DerivedToBase.DerivedClass,
299 *Adjustment.DerivedToBase.BasePath,
300 /*NullCheckValue=*/false);
301 break;
302
303 case SubobjectAdjustment::FieldAdjustment: {
304 unsigned CVR = Adjustment.Field.CVRQualifiers;
305 LValue LV = EmitLValueForField(Object, Adjustment.Field.Field, CVR);
306 if (LV.isSimple()) {
307 Object = LV.getAddress();
308 break;
309 }
310
311 // For non-simple lvalues, we actually have to create a copy of
312 // the object we're binding to.
313 QualType T = Adjustment.Field.Field->getType().getNonReferenceType()
314 .getUnqualifiedType();
315 Object = CreateTempAlloca(ConvertType(T), "lv");
316 EmitStoreThroughLValue(EmitLoadOfLValue(LV, T),
317 LValue::MakeAddr(Object,
318 Qualifiers::fromCVRMask(CVR)),
319 T);
320 break;
321 }
322 }
323 }
324
325 const llvm::Type *ResultPtrTy
326 = llvm::PointerType::get(ConvertType(ResultTy), 0);
327 Object = Builder.CreateBitCast(Object, ResultPtrTy, "temp");
328 return RValue::get(Object);
Anders Carlsson66413c22009-10-15 00:51:46 +0000329 }
Anders Carlsson7d4c0832009-05-20 00:36:58 +0000330 }
Eli Friedmanc21cb442009-05-20 02:31:19 +0000331
332 if (Val.isAggregate()) {
333 Val = RValue::get(Val.getAggregateAddr());
334 } else {
Anders Carlsson02bb7f02009-05-20 01:35:03 +0000335 // Create a temporary variable that we can bind the reference to.
Daniel Dunbara7566f12010-02-09 02:48:28 +0000336 llvm::Value *Temp = CreateMemTemp(E->getType(), "reftmp");
Eli Friedmanc21cb442009-05-20 02:31:19 +0000337 if (Val.isScalar())
338 EmitStoreOfScalar(Val.getScalarVal(), Temp, false, E->getType());
339 else
340 StoreComplexToAddr(Val.getComplexVal(), Temp, false);
341 Val = RValue::get(Temp);
Anders Carlsson145eae52009-05-20 01:03:17 +0000342 }
Eli Friedmanc21cb442009-05-20 02:31:19 +0000343
344 return Val;
Anders Carlsson6f5a0152009-05-20 00:24:07 +0000345}
346
347
Mike Stump4a3999f2009-09-09 13:00:44 +0000348/// getAccessedFieldNo - Given an encoded value and a result number, return the
349/// input field number being accessed.
350unsigned CodeGenFunction::getAccessedFieldNo(unsigned Idx,
Dan Gohman75d69da2008-05-22 00:50:06 +0000351 const llvm::Constant *Elts) {
352 if (isa<llvm::ConstantAggregateZero>(Elts))
353 return 0;
Mike Stump4a3999f2009-09-09 13:00:44 +0000354
Dan Gohman75d69da2008-05-22 00:50:06 +0000355 return cast<llvm::ConstantInt>(Elts->getOperand(Idx))->getZExtValue();
356}
357
Mike Stump3f6f9fe2009-12-16 02:57:00 +0000358void CodeGenFunction::EmitCheck(llvm::Value *Address, unsigned Size) {
359 if (!CatchUndefined)
360 return;
361
Chris Lattnerbc3be652010-04-10 18:34:14 +0000362 const llvm::Type *Size_tTy
Mike Stump3f6f9fe2009-12-16 02:57:00 +0000363 = llvm::IntegerType::get(VMContext, LLVMPointerWidth);
364 Address = Builder.CreateBitCast(Address, PtrToInt8Ty);
365
Chris Lattnerbc3be652010-04-10 18:34:14 +0000366 llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::objectsize, &Size_tTy, 1);
367 const llvm::IntegerType *Int1Ty = llvm::IntegerType::get(VMContext, 1);
368
Mike Stump3f6f9fe2009-12-16 02:57:00 +0000369 // In time, people may want to control this and use a 1 here.
Chris Lattnerbc3be652010-04-10 18:34:14 +0000370 llvm::Value *Arg = llvm::ConstantInt::get(Int1Ty, 0);
Mike Stump3f6f9fe2009-12-16 02:57:00 +0000371 llvm::Value *C = Builder.CreateCall2(F, Address, Arg);
372 llvm::BasicBlock *Cont = createBasicBlock();
373 llvm::BasicBlock *Check = createBasicBlock();
374 llvm::Value *NegativeOne = llvm::ConstantInt::get(Size_tTy, -1ULL);
375 Builder.CreateCondBr(Builder.CreateICmpEQ(C, NegativeOne), Cont, Check);
376
377 EmitBlock(Check);
378 Builder.CreateCondBr(Builder.CreateICmpUGE(C,
379 llvm::ConstantInt::get(Size_tTy, Size)),
380 Cont, getTrapBB());
381 EmitBlock(Cont);
382}
Chris Lattner4647a212007-08-31 22:49:20 +0000383
Chris Lattner116ce8f2010-01-09 21:40:03 +0000384
385llvm::Value *CodeGenFunction::
386EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV,
387 bool isInc, bool isPre) {
388 QualType ValTy = E->getSubExpr()->getType();
389 llvm::Value *InVal = EmitLoadOfLValue(LV, ValTy).getScalarVal();
390
391 int AmountVal = isInc ? 1 : -1;
392
393 if (ValTy->isPointerType() &&
394 ValTy->getAs<PointerType>()->isVariableArrayType()) {
395 // The amount of the addition/subtraction needs to account for the VLA size
396 ErrorUnsupported(E, "VLA pointer inc/dec");
397 }
398
399 llvm::Value *NextVal;
400 if (const llvm::PointerType *PT =
401 dyn_cast<llvm::PointerType>(InVal->getType())) {
402 llvm::Constant *Inc =
403 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), AmountVal);
404 if (!isa<llvm::FunctionType>(PT->getElementType())) {
405 QualType PTEE = ValTy->getPointeeType();
John McCall8b07ec22010-05-15 11:32:37 +0000406 if (const ObjCObjectType *OIT = PTEE->getAs<ObjCObjectType>()) {
Chris Lattner116ce8f2010-01-09 21:40:03 +0000407 // Handle interface types, which are not represented with a concrete
408 // type.
409 int size = getContext().getTypeSize(OIT) / 8;
410 if (!isInc)
411 size = -size;
412 Inc = llvm::ConstantInt::get(Inc->getType(), size);
413 const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(VMContext);
414 InVal = Builder.CreateBitCast(InVal, i8Ty);
415 NextVal = Builder.CreateGEP(InVal, Inc, "add.ptr");
416 llvm::Value *lhs = LV.getAddress();
417 lhs = Builder.CreateBitCast(lhs, llvm::PointerType::getUnqual(i8Ty));
418 LV = LValue::MakeAddr(lhs, MakeQualifiers(ValTy));
419 } else
420 NextVal = Builder.CreateInBoundsGEP(InVal, Inc, "ptrincdec");
421 } else {
422 const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(VMContext);
423 NextVal = Builder.CreateBitCast(InVal, i8Ty, "tmp");
424 NextVal = Builder.CreateGEP(NextVal, Inc, "ptrincdec");
425 NextVal = Builder.CreateBitCast(NextVal, InVal->getType());
426 }
427 } else if (InVal->getType() == llvm::Type::getInt1Ty(VMContext) && isInc) {
428 // Bool++ is an interesting case, due to promotion rules, we get:
429 // Bool++ -> Bool = Bool+1 -> Bool = (int)Bool+1 ->
430 // Bool = ((int)Bool+1) != 0
431 // An interesting aspect of this is that increment is always true.
432 // Decrement does not have this property.
433 NextVal = llvm::ConstantInt::getTrue(VMContext);
434 } else if (isa<llvm::IntegerType>(InVal->getType())) {
435 NextVal = llvm::ConstantInt::get(InVal->getType(), AmountVal);
436
437 // Signed integer overflow is undefined behavior.
438 if (ValTy->isSignedIntegerType())
439 NextVal = Builder.CreateNSWAdd(InVal, NextVal, isInc ? "inc" : "dec");
440 else
441 NextVal = Builder.CreateAdd(InVal, NextVal, isInc ? "inc" : "dec");
442 } else {
443 // Add the inc/dec to the real part.
444 if (InVal->getType()->isFloatTy())
445 NextVal =
446 llvm::ConstantFP::get(VMContext,
447 llvm::APFloat(static_cast<float>(AmountVal)));
448 else if (InVal->getType()->isDoubleTy())
449 NextVal =
450 llvm::ConstantFP::get(VMContext,
451 llvm::APFloat(static_cast<double>(AmountVal)));
452 else {
453 llvm::APFloat F(static_cast<float>(AmountVal));
454 bool ignored;
455 F.convert(Target.getLongDoubleFormat(), llvm::APFloat::rmTowardZero,
456 &ignored);
457 NextVal = llvm::ConstantFP::get(VMContext, F);
458 }
459 NextVal = Builder.CreateFAdd(InVal, NextVal, isInc ? "inc" : "dec");
460 }
461
462 // Store the updated result through the lvalue.
Daniel Dunbardc406b82010-04-05 21:36:35 +0000463 if (LV.isBitField())
Chris Lattner116ce8f2010-01-09 21:40:03 +0000464 EmitStoreThroughBitfieldLValue(RValue::get(NextVal), LV, ValTy, &NextVal);
465 else
466 EmitStoreThroughLValue(RValue::get(NextVal), LV, ValTy);
467
468 // If this is a postinc, return the value read from memory, otherwise use the
469 // updated value.
470 return isPre ? NextVal : InVal;
471}
472
473
474CodeGenFunction::ComplexPairTy CodeGenFunction::
475EmitComplexPrePostIncDec(const UnaryOperator *E, LValue LV,
476 bool isInc, bool isPre) {
477 ComplexPairTy InVal = LoadComplexFromAddr(LV.getAddress(),
478 LV.isVolatileQualified());
479
480 llvm::Value *NextVal;
481 if (isa<llvm::IntegerType>(InVal.first->getType())) {
482 uint64_t AmountVal = isInc ? 1 : -1;
483 NextVal = llvm::ConstantInt::get(InVal.first->getType(), AmountVal, true);
484
485 // Add the inc/dec to the real part.
486 NextVal = Builder.CreateAdd(InVal.first, NextVal, isInc ? "inc" : "dec");
487 } else {
488 QualType ElemTy = E->getType()->getAs<ComplexType>()->getElementType();
489 llvm::APFloat FVal(getContext().getFloatTypeSemantics(ElemTy), 1);
490 if (!isInc)
491 FVal.changeSign();
492 NextVal = llvm::ConstantFP::get(getLLVMContext(), FVal);
493
494 // Add the inc/dec to the real part.
495 NextVal = Builder.CreateFAdd(InVal.first, NextVal, isInc ? "inc" : "dec");
496 }
497
498 ComplexPairTy IncVal(NextVal, InVal.second);
499
500 // Store the updated result through the lvalue.
501 StoreComplexToAddr(IncVal, LV.getAddress(), LV.isVolatileQualified());
502
503 // If this is a postinc, return the value read from memory, otherwise use the
504 // updated value.
505 return isPre ? IncVal : InVal;
506}
507
508
Chris Lattnera45c5af2007-06-02 19:47:04 +0000509//===----------------------------------------------------------------------===//
Chris Lattnerd7f58862007-06-02 05:24:33 +0000510// LValue Expression Emission
Chris Lattnera45c5af2007-06-02 19:47:04 +0000511//===----------------------------------------------------------------------===//
Chris Lattnerd7f58862007-06-02 05:24:33 +0000512
Daniel Dunbarc79407f2009-02-05 07:09:07 +0000513RValue CodeGenFunction::GetUndefRValue(QualType Ty) {
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000514 if (Ty->isVoidType())
Daniel Dunbarc79407f2009-02-05 07:09:07 +0000515 return RValue::get(0);
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000516
517 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
Daniel Dunbar8429dbc2009-01-09 20:09:28 +0000518 const llvm::Type *EltTy = ConvertType(CTy->getElementType());
Owen Anderson7ec07a52009-07-30 23:11:26 +0000519 llvm::Value *U = llvm::UndefValue::get(EltTy);
Daniel Dunbar8429dbc2009-01-09 20:09:28 +0000520 return RValue::getComplex(std::make_pair(U, U));
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000521 }
522
523 if (hasAggregateLLVMType(Ty)) {
Owen Anderson9793f0e2009-07-29 22:16:19 +0000524 const llvm::Type *LTy = llvm::PointerType::getUnqual(ConvertType(Ty));
Owen Anderson7ec07a52009-07-30 23:11:26 +0000525 return RValue::getAggregate(llvm::UndefValue::get(LTy));
Daniel Dunbar8429dbc2009-01-09 20:09:28 +0000526 }
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000527
528 return RValue::get(llvm::UndefValue::get(ConvertType(Ty)));
Daniel Dunbarbb197e42009-01-09 16:50:52 +0000529}
530
Daniel Dunbarc79407f2009-02-05 07:09:07 +0000531RValue CodeGenFunction::EmitUnsupportedRValue(const Expr *E,
532 const char *Name) {
533 ErrorUnsupported(E, Name);
534 return GetUndefRValue(E->getType());
535}
536
Daniel Dunbarf2e69882008-08-25 20:45:57 +0000537LValue CodeGenFunction::EmitUnsupportedLValue(const Expr *E,
538 const char *Name) {
539 ErrorUnsupported(E, Name);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000540 llvm::Type *Ty = llvm::PointerType::getUnqual(ConvertType(E->getType()));
Owen Anderson7ec07a52009-07-30 23:11:26 +0000541 return LValue::MakeAddr(llvm::UndefValue::get(Ty),
John McCall8ccfcb52009-09-24 19:53:00 +0000542 MakeQualifiers(E->getType()));
Daniel Dunbarf2e69882008-08-25 20:45:57 +0000543}
544
Mike Stump3f6f9fe2009-12-16 02:57:00 +0000545LValue CodeGenFunction::EmitCheckedLValue(const Expr *E) {
546 LValue LV = EmitLValue(E);
Daniel Dunbardc406b82010-04-05 21:36:35 +0000547 if (!isa<DeclRefExpr>(E) && !LV.isBitField() && LV.isSimple())
Mike Stump3f6f9fe2009-12-16 02:57:00 +0000548 EmitCheck(LV.getAddress(), getContext().getTypeSize(E->getType()) / 8);
549 return LV;
550}
551
Chris Lattner8394d792007-06-05 20:53:16 +0000552/// EmitLValue - Emit code to compute a designator that specifies the location
553/// of the expression.
554///
Mike Stump4a3999f2009-09-09 13:00:44 +0000555/// This can return one of two things: a simple address or a bitfield reference.
556/// In either case, the LLVM Value* in the LValue structure is guaranteed to be
557/// an LLVM pointer type.
Chris Lattner8394d792007-06-05 20:53:16 +0000558///
Mike Stump4a3999f2009-09-09 13:00:44 +0000559/// If this returns a bitfield reference, nothing about the pointee type of the
560/// LLVM value is known: For example, it may not be a pointer to an integer.
Chris Lattner8394d792007-06-05 20:53:16 +0000561///
Mike Stump4a3999f2009-09-09 13:00:44 +0000562/// If this returns a normal address, and if the lvalue's C type is fixed size,
563/// this method guarantees that the returned pointer type will point to an LLVM
564/// type of the same size of the lvalue's type. If the lvalue has a variable
565/// length type, this is not possible.
Chris Lattner8394d792007-06-05 20:53:16 +0000566///
Chris Lattnerd7f58862007-06-02 05:24:33 +0000567LValue CodeGenFunction::EmitLValue(const Expr *E) {
568 switch (E->getStmtClass()) {
Daniel Dunbarf2e69882008-08-25 20:45:57 +0000569 default: return EmitUnsupportedLValue(E, "l-value expression");
Chris Lattnerd7f58862007-06-02 05:24:33 +0000570
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +0000571 case Expr::ObjCSelectorExprClass:
572 return EmitObjCSelectorLValue(cast<ObjCSelectorExpr>(E));
Fariborz Jahanian531c16f2009-12-09 23:35:29 +0000573 case Expr::ObjCIsaExprClass:
574 return EmitObjCIsaExpr(cast<ObjCIsaExpr>(E));
Mike Stump4a3999f2009-09-09 13:00:44 +0000575 case Expr::BinaryOperatorClass:
Daniel Dunbar8cde00a2008-09-04 03:20:13 +0000576 return EmitBinaryOperatorLValue(cast<BinaryOperator>(E));
Douglas Gregor914af212010-04-23 04:16:32 +0000577 case Expr::CompoundAssignOperatorClass:
578 return EmitCompoundAssignOperatorLValue(cast<CompoundAssignOperator>(E));
Mike Stump4a3999f2009-09-09 13:00:44 +0000579 case Expr::CallExprClass:
Anders Carlssonc82555f2009-09-01 21:18:52 +0000580 case Expr::CXXMemberCallExprClass:
Douglas Gregor993603d2008-11-14 16:09:21 +0000581 case Expr::CXXOperatorCallExprClass:
582 return EmitCallExprLValue(cast<CallExpr>(E));
Daniel Dunbar8d9dc4a2009-02-11 20:59:32 +0000583 case Expr::VAArgExprClass:
584 return EmitVAArgExprLValue(cast<VAArgExpr>(E));
Mike Stump4a3999f2009-09-09 13:00:44 +0000585 case Expr::DeclRefExprClass:
Douglas Gregorc7acfdf2009-01-06 05:10:23 +0000586 return EmitDeclRefLValue(cast<DeclRefExpr>(E));
Chris Lattner946aa312007-06-05 03:59:43 +0000587 case Expr::ParenExprClass:return EmitLValue(cast<ParenExpr>(E)->getSubExpr());
Chris Lattner6307f192008-08-10 01:53:14 +0000588 case Expr::PredefinedExprClass:
589 return EmitPredefinedLValue(cast<PredefinedExpr>(E));
Chris Lattner4347e3692007-06-06 04:54:52 +0000590 case Expr::StringLiteralClass:
591 return EmitStringLiteralLValue(cast<StringLiteral>(E));
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +0000592 case Expr::ObjCEncodeExprClass:
593 return EmitObjCEncodeExprLValue(cast<ObjCEncodeExpr>(E));
Chris Lattner4bd55962008-03-30 23:03:07 +0000594
Mike Stump4a3999f2009-09-09 13:00:44 +0000595 case Expr::BlockDeclRefExprClass:
Mike Stump1db7d042009-02-28 09:07:16 +0000596 return EmitBlockDeclRefLValue(cast<BlockDeclRefExpr>(E));
597
Anders Carlsson3be22e22009-05-30 23:23:33 +0000598 case Expr::CXXTemporaryObjectExprClass:
599 case Expr::CXXConstructExprClass:
Anders Carlssonfd2af0c2009-05-30 23:30:54 +0000600 return EmitCXXConstructLValue(cast<CXXConstructExpr>(E));
601 case Expr::CXXBindTemporaryExprClass:
602 return EmitCXXBindTemporaryLValue(cast<CXXBindTemporaryExpr>(E));
Anders Carlsson96bad9a2009-09-14 01:10:45 +0000603 case Expr::CXXExprWithTemporariesClass:
604 return EmitCXXExprWithTemporariesLValue(cast<CXXExprWithTemporaries>(E));
Anders Carlsson52ce3bb2009-11-14 01:51:50 +0000605 case Expr::CXXZeroInitValueExprClass:
606 return EmitNullInitializationLValue(cast<CXXZeroInitValueExpr>(E));
607 case Expr::CXXDefaultArgExprClass:
608 return EmitLValue(cast<CXXDefaultArgExpr>(E)->getExpr());
Mike Stumpc9b231c2009-11-15 08:09:41 +0000609 case Expr::CXXTypeidExprClass:
610 return EmitCXXTypeidLValue(cast<CXXTypeidExpr>(E));
Anders Carlssonfd2af0c2009-05-30 23:30:54 +0000611
Daniel Dunbarc8317a42008-08-23 10:51:21 +0000612 case Expr::ObjCMessageExprClass:
613 return EmitObjCMessageExprLValue(cast<ObjCMessageExpr>(E));
Mike Stump4a3999f2009-09-09 13:00:44 +0000614 case Expr::ObjCIvarRefExprClass:
Chris Lattner4bd55962008-03-30 23:03:07 +0000615 return EmitObjCIvarRefLValue(cast<ObjCIvarRefExpr>(E));
Daniel Dunbarf2e69882008-08-25 20:45:57 +0000616 case Expr::ObjCPropertyRefExprClass:
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +0000617 return EmitObjCPropertyRefLValue(cast<ObjCPropertyRefExpr>(E));
Fariborz Jahanian9a846652009-08-20 17:02:02 +0000618 case Expr::ObjCImplicitSetterGetterRefExprClass:
619 return EmitObjCKVCRefLValue(cast<ObjCImplicitSetterGetterRefExpr>(E));
Douglas Gregor8ea1f532008-11-04 14:56:14 +0000620 case Expr::ObjCSuperExprClass:
Chris Lattnera4185c52009-04-25 19:35:26 +0000621 return EmitObjCSuperExprLValue(cast<ObjCSuperExpr>(E));
Douglas Gregor8ea1f532008-11-04 14:56:14 +0000622
Chris Lattnera4185c52009-04-25 19:35:26 +0000623 case Expr::StmtExprClass:
624 return EmitStmtExprLValue(cast<StmtExpr>(E));
Mike Stump4a3999f2009-09-09 13:00:44 +0000625 case Expr::UnaryOperatorClass:
Chris Lattner8394d792007-06-05 20:53:16 +0000626 return EmitUnaryOpLValue(cast<UnaryOperator>(E));
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000627 case Expr::ArraySubscriptExprClass:
628 return EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E));
Nate Begemance4d7fc2008-04-18 23:10:10 +0000629 case Expr::ExtVectorElementExprClass:
630 return EmitExtVectorElementExpr(cast<ExtVectorElementExpr>(E));
Mike Stump4a3999f2009-09-09 13:00:44 +0000631 case Expr::MemberExprClass:
Douglas Gregorc1905232009-08-26 22:36:53 +0000632 return EmitMemberExpr(cast<MemberExpr>(E));
Eli Friedman9fd8b682008-05-13 23:18:27 +0000633 case Expr::CompoundLiteralExprClass:
634 return EmitCompoundLiteralLValue(cast<CompoundLiteralExpr>(E));
Daniel Dunbarbf1fe8c2009-03-24 02:38:23 +0000635 case Expr::ConditionalOperatorClass:
Anders Carlsson1450adb2009-09-15 16:35:24 +0000636 return EmitConditionalOperatorLValue(cast<ConditionalOperator>(E));
Chris Lattner053441f2008-12-12 05:35:08 +0000637 case Expr::ChooseExprClass:
Eli Friedmane0a5b8b2009-03-04 05:52:32 +0000638 return EmitLValue(cast<ChooseExpr>(E)->getChosenSubExpr(getContext()));
Chris Lattner63d06ab2009-03-18 04:02:57 +0000639 case Expr::ImplicitCastExprClass:
640 case Expr::CStyleCastExprClass:
641 case Expr::CXXFunctionalCastExprClass:
642 case Expr::CXXStaticCastExprClass:
643 case Expr::CXXDynamicCastExprClass:
644 case Expr::CXXReinterpretCastExprClass:
645 case Expr::CXXConstCastExprClass:
Chris Lattner28bcf1a2009-03-18 18:28:57 +0000646 return EmitCastLValue(cast<CastExpr>(E));
Chris Lattnerd7f58862007-06-02 05:24:33 +0000647 }
648}
649
Daniel Dunbar1d425462009-02-10 00:57:50 +0000650llvm::Value *CodeGenFunction::EmitLoadOfScalar(llvm::Value *Addr, bool Volatile,
651 QualType Ty) {
Daniel Dunbarc76493a2009-11-29 21:23:36 +0000652 llvm::LoadInst *Load = Builder.CreateLoad(Addr, "tmp");
653 if (Volatile)
654 Load->setVolatile(true);
Daniel Dunbar1d425462009-02-10 00:57:50 +0000655
Anders Carlsson29a1be32009-05-19 19:36:19 +0000656 // Bool can have different representation in memory than in registers.
Daniel Dunbarc76493a2009-11-29 21:23:36 +0000657 llvm::Value *V = Load;
Daniel Dunbar1d425462009-02-10 00:57:50 +0000658 if (Ty->isBooleanType())
Owen Anderson41a75022009-08-13 21:57:51 +0000659 if (V->getType() != llvm::Type::getInt1Ty(VMContext))
660 V = Builder.CreateTrunc(V, llvm::Type::getInt1Ty(VMContext), "tobool");
Mike Stump4a3999f2009-09-09 13:00:44 +0000661
Daniel Dunbar1d425462009-02-10 00:57:50 +0000662 return V;
663}
664
665void CodeGenFunction::EmitStoreOfScalar(llvm::Value *Value, llvm::Value *Addr,
Anders Carlsson83709642009-05-19 18:50:41 +0000666 bool Volatile, QualType Ty) {
Mike Stump4a3999f2009-09-09 13:00:44 +0000667
Anders Carlsson29a1be32009-05-19 19:36:19 +0000668 if (Ty->isBooleanType()) {
669 // Bool can have different representation in memory than in registers.
Anders Carlsson29a1be32009-05-19 19:36:19 +0000670 const llvm::PointerType *DstPtr = cast<llvm::PointerType>(Addr->getType());
Eli Friedmanb2b120f2009-12-01 22:31:51 +0000671 Value = Builder.CreateIntCast(Value, DstPtr->getElementType(), false);
Daniel Dunbar1d425462009-02-10 00:57:50 +0000672 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000673 Builder.CreateStore(Value, Addr, Volatile);
Daniel Dunbar1d425462009-02-10 00:57:50 +0000674}
675
Mike Stump4a3999f2009-09-09 13:00:44 +0000676/// EmitLoadOfLValue - Given an expression that represents a value lvalue, this
677/// method emits the address of the lvalue, then loads the result as an rvalue,
678/// returning the rvalue.
Chris Lattner9369a562007-06-29 16:31:29 +0000679RValue CodeGenFunction::EmitLoadOfLValue(LValue LV, QualType ExprType) {
Fariborz Jahanian50a12702008-11-19 17:34:06 +0000680 if (LV.isObjCWeak()) {
Mike Stump4a3999f2009-09-09 13:00:44 +0000681 // load of a __weak object.
Fariborz Jahanianf5125d12008-11-18 21:45:40 +0000682 llvm::Value *AddrWeakObj = LV.getAddress();
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000683 return RValue::get(CGM.getObjCRuntime().EmitObjCWeakRead(*this,
684 AddrWeakObj));
Fariborz Jahanianf5125d12008-11-18 21:45:40 +0000685 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000686
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000687 if (LV.isSimple()) {
688 llvm::Value *Ptr = LV.getAddress();
Douglas Gregora6437802010-02-05 21:10:36 +0000689 const llvm::Type *EltTy =
690 cast<llvm::PointerType>(Ptr->getType())->getElementType();
Mike Stump4a3999f2009-09-09 13:00:44 +0000691
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000692 // Simple scalar l-value.
Daniel Dunbar3d33fab2010-02-08 22:53:07 +0000693 //
694 // FIXME: We shouldn't have to use isSingleValueType here.
Douglas Gregora6437802010-02-05 21:10:36 +0000695 if (EltTy->isSingleValueType())
Mike Stump4a3999f2009-09-09 13:00:44 +0000696 return RValue::get(EmitLoadOfScalar(Ptr, LV.isVolatileQualified(),
Daniel Dunbar1d425462009-02-10 00:57:50 +0000697 ExprType));
Mike Stump4a3999f2009-09-09 13:00:44 +0000698
Chris Lattner6278e6a2007-08-11 00:04:45 +0000699 assert(ExprType->isFunctionType() && "Unknown scalar value");
700 return RValue::get(Ptr);
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000701 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000702
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000703 if (LV.isVectorElt()) {
Eli Friedman327944b2008-06-13 23:01:12 +0000704 llvm::Value *Vec = Builder.CreateLoad(LV.getVectorAddr(),
705 LV.isVolatileQualified(), "tmp");
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000706 return RValue::get(Builder.CreateExtractElement(Vec, LV.getVectorIdx(),
707 "vecext"));
708 }
Chris Lattner73ab9b32007-08-03 00:16:29 +0000709
710 // If this is a reference to a subset of the elements of a vector, either
711 // shuffle the input or extract/insert them as appropriate.
Nate Begemance4d7fc2008-04-18 23:10:10 +0000712 if (LV.isExtVectorElt())
713 return EmitLoadOfExtVectorElementLValue(LV, ExprType);
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000714
Daniel Dunbardc406b82010-04-05 21:36:35 +0000715 if (LV.isBitField())
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000716 return EmitLoadOfBitfieldLValue(LV, ExprType);
717
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +0000718 if (LV.isPropertyRef())
719 return EmitLoadOfPropertyRefLValue(LV, ExprType);
720
Chris Lattner6c7ce102009-02-16 21:11:58 +0000721 assert(LV.isKVCRef() && "Unknown LValue type!");
722 return EmitLoadOfKVCRefLValue(LV, ExprType);
Chris Lattner8394d792007-06-05 20:53:16 +0000723}
724
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000725RValue CodeGenFunction::EmitLoadOfBitfieldLValue(LValue LV,
726 QualType ExprType) {
Daniel Dunbar196ea442010-04-06 01:07:44 +0000727 const CGBitFieldInfo &Info = LV.getBitFieldInfo();
Daniel Dunbaread7c912008-08-06 05:08:45 +0000728
Daniel Dunbar3447a022010-04-13 23:34:15 +0000729 // Get the output type.
730 const llvm::Type *ResLTy = ConvertType(ExprType);
731 unsigned ResSizeInBits = CGM.getTargetData().getTypeSizeInBits(ResLTy);
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000732
Daniel Dunbar3447a022010-04-13 23:34:15 +0000733 // Compute the result as an OR of all of the individual component accesses.
734 llvm::Value *Res = 0;
735 for (unsigned i = 0, e = Info.getNumComponents(); i != e; ++i) {
736 const CGBitFieldInfo::AccessInfo &AI = Info.getComponent(i);
Mike Stump4a3999f2009-09-09 13:00:44 +0000737
Daniel Dunbar3447a022010-04-13 23:34:15 +0000738 // Get the field pointer.
739 llvm::Value *Ptr = LV.getBitFieldBaseAddr();
Mike Stump4a3999f2009-09-09 13:00:44 +0000740
Daniel Dunbar3447a022010-04-13 23:34:15 +0000741 // Only offset by the field index if used, so that incoming values are not
742 // required to be structures.
743 if (AI.FieldIndex)
744 Ptr = Builder.CreateStructGEP(Ptr, AI.FieldIndex, "bf.field");
Mike Stump4a3999f2009-09-09 13:00:44 +0000745
Daniel Dunbar3447a022010-04-13 23:34:15 +0000746 // Offset by the byte offset, if used.
747 if (AI.FieldByteOffset) {
748 const llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(VMContext);
749 Ptr = Builder.CreateBitCast(Ptr, i8PTy);
750 Ptr = Builder.CreateConstGEP1_32(Ptr, AI.FieldByteOffset,"bf.field.offs");
751 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000752
Daniel Dunbar3447a022010-04-13 23:34:15 +0000753 // Cast to the access type.
754 const llvm::Type *PTy = llvm::Type::getIntNPtrTy(VMContext, AI.AccessWidth,
755 ExprType.getAddressSpace());
756 Ptr = Builder.CreateBitCast(Ptr, PTy);
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000757
Daniel Dunbar3447a022010-04-13 23:34:15 +0000758 // Perform the load.
759 llvm::LoadInst *Load = Builder.CreateLoad(Ptr, LV.isVolatileQualified());
760 if (AI.AccessAlignment)
761 Load->setAlignment(AI.AccessAlignment);
762
763 // Shift out unused low bits and mask out unused high bits.
764 llvm::Value *Val = Load;
765 if (AI.FieldBitStart)
Daniel Dunbar67aba792010-04-15 03:47:33 +0000766 Val = Builder.CreateLShr(Load, AI.FieldBitStart);
Daniel Dunbar3447a022010-04-13 23:34:15 +0000767 Val = Builder.CreateAnd(Val, llvm::APInt::getLowBitsSet(AI.AccessWidth,
768 AI.TargetBitWidth),
769 "bf.clear");
770
771 // Extend or truncate to the target size.
772 if (AI.AccessWidth < ResSizeInBits)
773 Val = Builder.CreateZExt(Val, ResLTy);
774 else if (AI.AccessWidth > ResSizeInBits)
775 Val = Builder.CreateTrunc(Val, ResLTy);
776
777 // Shift into place, and OR into the result.
778 if (AI.TargetBitOffset)
779 Val = Builder.CreateShl(Val, AI.TargetBitOffset);
780 Res = Res ? Builder.CreateOr(Res, Val) : Val;
Daniel Dunbaread7c912008-08-06 05:08:45 +0000781 }
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000782
Daniel Dunbar3447a022010-04-13 23:34:15 +0000783 // If the bit-field is signed, perform the sign-extension.
784 //
785 // FIXME: This can easily be folded into the load of the high bits, which
786 // could also eliminate the mask of high bits in some situations.
787 if (Info.isSigned()) {
Daniel Dunbar67aba792010-04-15 03:47:33 +0000788 unsigned ExtraBits = ResSizeInBits - Info.getSize();
Daniel Dunbar3447a022010-04-13 23:34:15 +0000789 if (ExtraBits)
790 Res = Builder.CreateAShr(Builder.CreateShl(Res, ExtraBits),
791 ExtraBits, "bf.val.sext");
Daniel Dunbaread7c912008-08-06 05:08:45 +0000792 }
Eli Friedmanf2442dc2008-05-17 20:03:47 +0000793
Daniel Dunbar3447a022010-04-13 23:34:15 +0000794 return RValue::get(Res);
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000795}
796
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +0000797RValue CodeGenFunction::EmitLoadOfPropertyRefLValue(LValue LV,
798 QualType ExprType) {
799 return EmitObjCPropertyGet(LV.getPropertyRefExpr());
800}
801
Fariborz Jahanian9ac53512008-11-22 22:30:21 +0000802RValue CodeGenFunction::EmitLoadOfKVCRefLValue(LValue LV,
803 QualType ExprType) {
804 return EmitObjCPropertyGet(LV.getKVCRefExpr());
805}
806
Nate Begemanb699c9b2009-01-18 06:42:49 +0000807// If this is a reference to a subset of the elements of a vector, create an
808// appropriate shufflevector.
Nate Begemance4d7fc2008-04-18 23:10:10 +0000809RValue CodeGenFunction::EmitLoadOfExtVectorElementLValue(LValue LV,
810 QualType ExprType) {
Eli Friedman327944b2008-06-13 23:01:12 +0000811 llvm::Value *Vec = Builder.CreateLoad(LV.getExtVectorAddr(),
812 LV.isVolatileQualified(), "tmp");
Mike Stump4a3999f2009-09-09 13:00:44 +0000813
Nate Begemanf322eab2008-05-09 06:41:27 +0000814 const llvm::Constant *Elts = LV.getExtVectorElts();
Mike Stump4a3999f2009-09-09 13:00:44 +0000815
816 // If the result of the expression is a non-vector type, we must be extracting
817 // a single element. Just codegen as an extractelement.
John McCall9dd450b2009-09-21 23:43:11 +0000818 const VectorType *ExprVT = ExprType->getAs<VectorType>();
Chris Lattner8eab8ff2007-08-10 17:10:08 +0000819 if (!ExprVT) {
Dan Gohman75d69da2008-05-22 00:50:06 +0000820 unsigned InIdx = getAccessedFieldNo(0, Elts);
Owen Anderson41a75022009-08-13 21:57:51 +0000821 llvm::Value *Elt = llvm::ConstantInt::get(
822 llvm::Type::getInt32Ty(VMContext), InIdx);
Chris Lattner40ff7012007-08-03 16:18:34 +0000823 return RValue::get(Builder.CreateExtractElement(Vec, Elt, "tmp"));
824 }
Nate Begemanb699c9b2009-01-18 06:42:49 +0000825
826 // Always use shuffle vector to try to retain the original program structure
Chris Lattner8eab8ff2007-08-10 17:10:08 +0000827 unsigned NumResultElts = ExprVT->getNumElements();
Mike Stump4a3999f2009-09-09 13:00:44 +0000828
Nate Begemanb699c9b2009-01-18 06:42:49 +0000829 llvm::SmallVector<llvm::Constant*, 4> Mask;
Chris Lattner40ff7012007-08-03 16:18:34 +0000830 for (unsigned i = 0; i != NumResultElts; ++i) {
Dan Gohman75d69da2008-05-22 00:50:06 +0000831 unsigned InIdx = getAccessedFieldNo(i, Elts);
Owen Anderson41a75022009-08-13 21:57:51 +0000832 Mask.push_back(llvm::ConstantInt::get(
833 llvm::Type::getInt32Ty(VMContext), InIdx));
Chris Lattner40ff7012007-08-03 16:18:34 +0000834 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000835
Owen Anderson3cc120a2009-07-28 21:22:35 +0000836 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
Nate Begemanb699c9b2009-01-18 06:42:49 +0000837 Vec = Builder.CreateShuffleVector(Vec,
Owen Anderson7ec07a52009-07-30 23:11:26 +0000838 llvm::UndefValue::get(Vec->getType()),
Nate Begemanb699c9b2009-01-18 06:42:49 +0000839 MaskV, "tmp");
840 return RValue::get(Vec);
Chris Lattner40ff7012007-08-03 16:18:34 +0000841}
842
843
Chris Lattner9369a562007-06-29 16:31:29 +0000844
Chris Lattner8394d792007-06-05 20:53:16 +0000845/// EmitStoreThroughLValue - Store the specified rvalue into the specified
846/// lvalue, where both are guaranteed to the have the same type, and that type
847/// is 'Ty'.
Mike Stump4a3999f2009-09-09 13:00:44 +0000848void CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst,
Chris Lattner8394d792007-06-05 20:53:16 +0000849 QualType Ty) {
Chris Lattner41d480e2007-08-03 16:28:33 +0000850 if (!Dst.isSimple()) {
851 if (Dst.isVectorElt()) {
852 // Read/modify/write the vector, inserting the new element.
Eli Friedman327944b2008-06-13 23:01:12 +0000853 llvm::Value *Vec = Builder.CreateLoad(Dst.getVectorAddr(),
854 Dst.isVolatileQualified(), "tmp");
Chris Lattner4647a212007-08-31 22:49:20 +0000855 Vec = Builder.CreateInsertElement(Vec, Src.getScalarVal(),
Chris Lattner41d480e2007-08-03 16:28:33 +0000856 Dst.getVectorIdx(), "vecins");
Eli Friedman327944b2008-06-13 23:01:12 +0000857 Builder.CreateStore(Vec, Dst.getVectorAddr(),Dst.isVolatileQualified());
Chris Lattner41d480e2007-08-03 16:28:33 +0000858 return;
859 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000860
Nate Begemance4d7fc2008-04-18 23:10:10 +0000861 // If this is an update of extended vector elements, insert them as
862 // appropriate.
863 if (Dst.isExtVectorElt())
864 return EmitStoreThroughExtVectorComponentLValue(Src, Dst, Ty);
Lauro Ramos Venancio09af71c2008-01-22 22:36:45 +0000865
Daniel Dunbardc406b82010-04-05 21:36:35 +0000866 if (Dst.isBitField())
Lauro Ramos Venancio09af71c2008-01-22 22:36:45 +0000867 return EmitStoreThroughBitfieldLValue(Src, Dst, Ty);
868
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +0000869 if (Dst.isPropertyRef())
870 return EmitStoreThroughPropertyRefLValue(Src, Dst, Ty);
871
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000872 assert(Dst.isKVCRef() && "Unknown LValue type");
873 return EmitStoreThroughKVCRefLValue(Src, Dst, Ty);
Chris Lattner41d480e2007-08-03 16:28:33 +0000874 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000875
Fariborz Jahanian10bec102009-02-21 00:30:43 +0000876 if (Dst.isObjCWeak() && !Dst.isNonGC()) {
Mike Stump4a3999f2009-09-09 13:00:44 +0000877 // load of a __weak object.
Fariborz Jahanian50a12702008-11-19 17:34:06 +0000878 llvm::Value *LvalueDst = Dst.getAddress();
879 llvm::Value *src = Src.getScalarVal();
Mike Stumpca5ae662009-04-14 00:57:29 +0000880 CGM.getObjCRuntime().EmitObjCWeakAssign(*this, src, LvalueDst);
Fariborz Jahanian50a12702008-11-19 17:34:06 +0000881 return;
882 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000883
Fariborz Jahanian10bec102009-02-21 00:30:43 +0000884 if (Dst.isObjCStrong() && !Dst.isNonGC()) {
Mike Stump4a3999f2009-09-09 13:00:44 +0000885 // load of a __strong object.
Fariborz Jahanian50a12702008-11-19 17:34:06 +0000886 llvm::Value *LvalueDst = Dst.getAddress();
887 llvm::Value *src = Src.getScalarVal();
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000888 if (Dst.isObjCIvar()) {
889 assert(Dst.getBaseIvarExp() && "BaseIvarExp is NULL");
890 const llvm::Type *ResultType = ConvertType(getContext().LongTy);
891 llvm::Value *RHS = EmitScalarExpr(Dst.getBaseIvarExp());
Fariborz Jahanian1f9ed582009-09-25 00:00:20 +0000892 llvm::Value *dst = RHS;
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000893 RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast");
894 llvm::Value *LHS =
895 Builder.CreatePtrToInt(LvalueDst, ResultType, "sub.ptr.lhs.cast");
896 llvm::Value *BytesBetween = Builder.CreateSub(LHS, RHS, "ivar.offset");
Fariborz Jahanian1f9ed582009-09-25 00:00:20 +0000897 CGM.getObjCRuntime().EmitObjCIvarAssign(*this, src, dst,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000898 BytesBetween);
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000899 } else if (Dst.isGlobalObjCRef())
Fariborz Jahanian32ff7ae2009-05-04 23:27:20 +0000900 CGM.getObjCRuntime().EmitObjCGlobalAssign(*this, src, LvalueDst);
901 else
902 CGM.getObjCRuntime().EmitObjCStrongCastAssign(*this, src, LvalueDst);
Fariborz Jahanian50a12702008-11-19 17:34:06 +0000903 return;
904 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000905
Chris Lattner6278e6a2007-08-11 00:04:45 +0000906 assert(Src.isScalar() && "Can't emit an agg store with this method");
Anders Carlsson83709642009-05-19 18:50:41 +0000907 EmitStoreOfScalar(Src.getScalarVal(), Dst.getAddress(),
908 Dst.isVolatileQualified(), Ty);
Chris Lattner8394d792007-06-05 20:53:16 +0000909}
910
Lauro Ramos Venancio09af71c2008-01-22 22:36:45 +0000911void CodeGenFunction::EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst,
Mike Stump4a3999f2009-09-09 13:00:44 +0000912 QualType Ty,
Daniel Dunbar9b1335e2008-11-19 09:36:46 +0000913 llvm::Value **Result) {
Daniel Dunbar196ea442010-04-06 01:07:44 +0000914 const CGBitFieldInfo &Info = Dst.getBitFieldInfo();
Lauro Ramos Venancio09af71c2008-01-22 22:36:45 +0000915
Daniel Dunbar67aba792010-04-15 03:47:33 +0000916 // Get the output type.
Anders Carlsson8345a702010-04-17 21:52:22 +0000917 const llvm::Type *ResLTy = ConvertTypeForMem(Ty);
Daniel Dunbar67aba792010-04-15 03:47:33 +0000918 unsigned ResSizeInBits = CGM.getTargetData().getTypeSizeInBits(ResLTy);
Daniel Dunbaread7c912008-08-06 05:08:45 +0000919
Daniel Dunbar67aba792010-04-15 03:47:33 +0000920 // Get the source value, truncated to the width of the bit-field.
Daniel Dunbar9b1335e2008-11-19 09:36:46 +0000921 llvm::Value *SrcVal = Src.getScalarVal();
Anders Carlsson8345a702010-04-17 21:52:22 +0000922
923 if (Ty->isBooleanType())
924 SrcVal = Builder.CreateIntCast(SrcVal, ResLTy, /*IsSigned=*/false);
925
Daniel Dunbar67aba792010-04-15 03:47:33 +0000926 SrcVal = Builder.CreateAnd(SrcVal, llvm::APInt::getLowBitsSet(ResSizeInBits,
927 Info.getSize()),
928 "bf.value");
Lauro Ramos Venancio09af71c2008-01-22 22:36:45 +0000929
Daniel Dunbar9b1335e2008-11-19 09:36:46 +0000930 // Return the new value of the bit-field, if requested.
931 if (Result) {
932 // Cast back to the proper type for result.
Daniel Dunbar67aba792010-04-15 03:47:33 +0000933 const llvm::Type *SrcTy = Src.getScalarVal()->getType();
934 llvm::Value *ReloadVal = Builder.CreateIntCast(SrcVal, SrcTy, false,
935 "bf.reload.val");
Daniel Dunbar9b1335e2008-11-19 09:36:46 +0000936
937 // Sign extend if necessary.
Daniel Dunbar67aba792010-04-15 03:47:33 +0000938 if (Info.isSigned()) {
939 unsigned ExtraBits = ResSizeInBits - Info.getSize();
940 if (ExtraBits)
941 ReloadVal = Builder.CreateAShr(Builder.CreateShl(ReloadVal, ExtraBits),
942 ExtraBits, "bf.reload.sext");
Daniel Dunbar9b1335e2008-11-19 09:36:46 +0000943 }
944
Daniel Dunbar67aba792010-04-15 03:47:33 +0000945 *Result = ReloadVal;
Daniel Dunbar9b1335e2008-11-19 09:36:46 +0000946 }
947
Daniel Dunbar67aba792010-04-15 03:47:33 +0000948 // Iterate over the components, writing each piece to memory.
949 for (unsigned i = 0, e = Info.getNumComponents(); i != e; ++i) {
950 const CGBitFieldInfo::AccessInfo &AI = Info.getComponent(i);
Eli Friedmanf2442dc2008-05-17 20:03:47 +0000951
Daniel Dunbar67aba792010-04-15 03:47:33 +0000952 // Get the field pointer.
953 llvm::Value *Ptr = Dst.getBitFieldBaseAddr();
Mike Stump4a3999f2009-09-09 13:00:44 +0000954
Daniel Dunbar67aba792010-04-15 03:47:33 +0000955 // Only offset by the field index if used, so that incoming values are not
956 // required to be structures.
957 if (AI.FieldIndex)
958 Ptr = Builder.CreateStructGEP(Ptr, AI.FieldIndex, "bf.field");
Mike Stump4a3999f2009-09-09 13:00:44 +0000959
Daniel Dunbar67aba792010-04-15 03:47:33 +0000960 // Offset by the byte offset, if used.
961 if (AI.FieldByteOffset) {
962 const llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(VMContext);
963 Ptr = Builder.CreateBitCast(Ptr, i8PTy);
964 Ptr = Builder.CreateConstGEP1_32(Ptr, AI.FieldByteOffset,"bf.field.offs");
965 }
Eli Friedmanf2442dc2008-05-17 20:03:47 +0000966
Daniel Dunbar67aba792010-04-15 03:47:33 +0000967 // Cast to the access type.
968 const llvm::Type *PTy = llvm::Type::getIntNPtrTy(VMContext, AI.AccessWidth,
969 Ty.getAddressSpace());
970 Ptr = Builder.CreateBitCast(Ptr, PTy);
Mike Stump4a3999f2009-09-09 13:00:44 +0000971
Daniel Dunbar67aba792010-04-15 03:47:33 +0000972 // Extract the piece of the bit-field value to write in this access, limited
973 // to the values that are part of this access.
974 llvm::Value *Val = SrcVal;
975 if (AI.TargetBitOffset)
976 Val = Builder.CreateLShr(Val, AI.TargetBitOffset);
977 Val = Builder.CreateAnd(Val, llvm::APInt::getLowBitsSet(ResSizeInBits,
978 AI.TargetBitWidth));
Mike Stump4a3999f2009-09-09 13:00:44 +0000979
Daniel Dunbar67aba792010-04-15 03:47:33 +0000980 // Extend or truncate to the access size.
981 const llvm::Type *AccessLTy =
982 llvm::Type::getIntNTy(VMContext, AI.AccessWidth);
983 if (ResSizeInBits < AI.AccessWidth)
984 Val = Builder.CreateZExt(Val, AccessLTy);
985 else if (ResSizeInBits > AI.AccessWidth)
986 Val = Builder.CreateTrunc(Val, AccessLTy);
Mike Stump4a3999f2009-09-09 13:00:44 +0000987
Daniel Dunbar67aba792010-04-15 03:47:33 +0000988 // Shift into the position in memory.
989 if (AI.FieldBitStart)
990 Val = Builder.CreateShl(Val, AI.FieldBitStart);
991
992 // If necessary, load and OR in bits that are outside of the bit-field.
993 if (AI.TargetBitWidth != AI.AccessWidth) {
994 llvm::LoadInst *Load = Builder.CreateLoad(Ptr, Dst.isVolatileQualified());
995 if (AI.AccessAlignment)
996 Load->setAlignment(AI.AccessAlignment);
997
998 // Compute the mask for zeroing the bits that are part of the bit-field.
999 llvm::APInt InvMask =
1000 ~llvm::APInt::getBitsSet(AI.AccessWidth, AI.FieldBitStart,
1001 AI.FieldBitStart + AI.TargetBitWidth);
1002
1003 // Apply the mask and OR in to the value to write.
1004 Val = Builder.CreateOr(Builder.CreateAnd(Load, InvMask), Val);
1005 }
1006
1007 // Write the value.
1008 llvm::StoreInst *Store = Builder.CreateStore(Val, Ptr,
1009 Dst.isVolatileQualified());
1010 if (AI.AccessAlignment)
1011 Store->setAlignment(AI.AccessAlignment);
Daniel Dunbaread7c912008-08-06 05:08:45 +00001012 }
Lauro Ramos Venancio09af71c2008-01-22 22:36:45 +00001013}
1014
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +00001015void CodeGenFunction::EmitStoreThroughPropertyRefLValue(RValue Src,
1016 LValue Dst,
1017 QualType Ty) {
1018 EmitObjCPropertySet(Dst.getPropertyRefExpr(), Src);
1019}
1020
Fariborz Jahanian9ac53512008-11-22 22:30:21 +00001021void CodeGenFunction::EmitStoreThroughKVCRefLValue(RValue Src,
1022 LValue Dst,
1023 QualType Ty) {
1024 EmitObjCPropertySet(Dst.getKVCRefExpr(), Src);
1025}
1026
Nate Begemance4d7fc2008-04-18 23:10:10 +00001027void CodeGenFunction::EmitStoreThroughExtVectorComponentLValue(RValue Src,
1028 LValue Dst,
1029 QualType Ty) {
Chris Lattner41d480e2007-08-03 16:28:33 +00001030 // This access turns into a read/modify/write of the vector. Load the input
1031 // value now.
Eli Friedman327944b2008-06-13 23:01:12 +00001032 llvm::Value *Vec = Builder.CreateLoad(Dst.getExtVectorAddr(),
1033 Dst.isVolatileQualified(), "tmp");
Nate Begemanf322eab2008-05-09 06:41:27 +00001034 const llvm::Constant *Elts = Dst.getExtVectorElts();
Mike Stump4a3999f2009-09-09 13:00:44 +00001035
Chris Lattner4647a212007-08-31 22:49:20 +00001036 llvm::Value *SrcVal = Src.getScalarVal();
Mike Stump4a3999f2009-09-09 13:00:44 +00001037
John McCall9dd450b2009-09-21 23:43:11 +00001038 if (const VectorType *VTy = Ty->getAs<VectorType>()) {
Chris Lattner3a44aa72007-08-03 16:37:04 +00001039 unsigned NumSrcElts = VTy->getNumElements();
Nate Begemanb699c9b2009-01-18 06:42:49 +00001040 unsigned NumDstElts =
1041 cast<llvm::VectorType>(Vec->getType())->getNumElements();
1042 if (NumDstElts == NumSrcElts) {
Mike Stump4a3999f2009-09-09 13:00:44 +00001043 // Use shuffle vector is the src and destination are the same number of
1044 // elements and restore the vector mask since it is on the side it will be
1045 // stored.
Nate Begemanea12f6e2009-06-26 21:12:50 +00001046 llvm::SmallVector<llvm::Constant*, 4> Mask(NumDstElts);
Nate Begemanb699c9b2009-01-18 06:42:49 +00001047 for (unsigned i = 0; i != NumSrcElts; ++i) {
1048 unsigned InIdx = getAccessedFieldNo(i, Elts);
Owen Anderson41a75022009-08-13 21:57:51 +00001049 Mask[InIdx] = llvm::ConstantInt::get(
1050 llvm::Type::getInt32Ty(VMContext), i);
Nate Begemanb699c9b2009-01-18 06:42:49 +00001051 }
Mike Stump4a3999f2009-09-09 13:00:44 +00001052
Owen Anderson3cc120a2009-07-28 21:22:35 +00001053 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
Nate Begemanb699c9b2009-01-18 06:42:49 +00001054 Vec = Builder.CreateShuffleVector(SrcVal,
Owen Anderson7ec07a52009-07-30 23:11:26 +00001055 llvm::UndefValue::get(Vec->getType()),
Nate Begemanb699c9b2009-01-18 06:42:49 +00001056 MaskV, "tmp");
Mike Stump658fe022009-07-30 22:28:39 +00001057 } else if (NumDstElts > NumSrcElts) {
Nate Begemanb699c9b2009-01-18 06:42:49 +00001058 // Extended the source vector to the same length and then shuffle it
1059 // into the destination.
1060 // FIXME: since we're shuffling with undef, can we just use the indices
1061 // into that? This could be simpler.
1062 llvm::SmallVector<llvm::Constant*, 4> ExtMask;
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001063 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
Nate Begemanb699c9b2009-01-18 06:42:49 +00001064 unsigned i;
1065 for (i = 0; i != NumSrcElts; ++i)
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001066 ExtMask.push_back(llvm::ConstantInt::get(Int32Ty, i));
Nate Begemanb699c9b2009-01-18 06:42:49 +00001067 for (; i != NumDstElts; ++i)
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001068 ExtMask.push_back(llvm::UndefValue::get(Int32Ty));
Owen Anderson3cc120a2009-07-28 21:22:35 +00001069 llvm::Value *ExtMaskV = llvm::ConstantVector::get(&ExtMask[0],
Nate Begemanb699c9b2009-01-18 06:42:49 +00001070 ExtMask.size());
Mike Stump4a3999f2009-09-09 13:00:44 +00001071 llvm::Value *ExtSrcVal =
Daniel Dunbar3d926cb2009-02-17 18:31:04 +00001072 Builder.CreateShuffleVector(SrcVal,
Owen Anderson7ec07a52009-07-30 23:11:26 +00001073 llvm::UndefValue::get(SrcVal->getType()),
Daniel Dunbar3d926cb2009-02-17 18:31:04 +00001074 ExtMaskV, "tmp");
Nate Begemanb699c9b2009-01-18 06:42:49 +00001075 // build identity
1076 llvm::SmallVector<llvm::Constant*, 4> Mask;
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001077 for (unsigned i = 0; i != NumDstElts; ++i)
1078 Mask.push_back(llvm::ConstantInt::get(Int32Ty, i));
1079
Nate Begemanb699c9b2009-01-18 06:42:49 +00001080 // modify when what gets shuffled in
1081 for (unsigned i = 0; i != NumSrcElts; ++i) {
1082 unsigned Idx = getAccessedFieldNo(i, Elts);
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001083 Mask[Idx] = llvm::ConstantInt::get(Int32Ty, i+NumDstElts);
Nate Begemanb699c9b2009-01-18 06:42:49 +00001084 }
Owen Anderson3cc120a2009-07-28 21:22:35 +00001085 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
Nate Begemanb699c9b2009-01-18 06:42:49 +00001086 Vec = Builder.CreateShuffleVector(Vec, ExtSrcVal, MaskV, "tmp");
Mike Stump658fe022009-07-30 22:28:39 +00001087 } else {
Nate Begemanb699c9b2009-01-18 06:42:49 +00001088 // We should never shorten the vector
1089 assert(0 && "unexpected shorten vector length");
Chris Lattner3a44aa72007-08-03 16:37:04 +00001090 }
1091 } else {
1092 // If the Src is a scalar (not a vector) it must be updating one element.
Dan Gohman75d69da2008-05-22 00:50:06 +00001093 unsigned InIdx = getAccessedFieldNo(0, Elts);
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001094 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
1095 llvm::Value *Elt = llvm::ConstantInt::get(Int32Ty, InIdx);
Chris Lattner41d480e2007-08-03 16:28:33 +00001096 Vec = Builder.CreateInsertElement(Vec, SrcVal, Elt, "tmp");
Chris Lattner41d480e2007-08-03 16:28:33 +00001097 }
Mike Stump4a3999f2009-09-09 13:00:44 +00001098
Eli Friedman327944b2008-06-13 23:01:12 +00001099 Builder.CreateStore(Vec, Dst.getExtVectorAddr(), Dst.isVolatileQualified());
Chris Lattner41d480e2007-08-03 16:28:33 +00001100}
1101
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +00001102// setObjCGCLValueClass - sets class of he lvalue for the purpose of
1103// generating write-barries API. It is currently a global, ivar,
1104// or neither.
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001105static void setObjCGCLValueClass(const ASTContext &Ctx, const Expr *E,
1106 LValue &LV) {
Fariborz Jahanian71848a32009-09-21 23:03:37 +00001107 if (Ctx.getLangOptions().getGCMode() == LangOptions::NonGC)
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +00001108 return;
1109
Fariborz Jahaniande1d3242009-09-16 23:11:23 +00001110 if (isa<ObjCIvarRefExpr>(E)) {
1111 LV.SetObjCIvar(LV, true);
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00001112 ObjCIvarRefExpr *Exp = cast<ObjCIvarRefExpr>(const_cast<Expr*>(E));
1113 LV.setBaseIvarExp(Exp->getBase());
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +00001114 LV.SetObjCArray(LV, E->getType()->isArrayType());
Fariborz Jahaniande1d3242009-09-16 23:11:23 +00001115 return;
1116 }
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001117
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +00001118 if (const DeclRefExpr *Exp = dyn_cast<DeclRefExpr>(E)) {
1119 if (const VarDecl *VD = dyn_cast<VarDecl>(Exp->getDecl())) {
1120 if ((VD->isBlockVarDecl() && !VD->hasLocalStorage()) ||
1121 VD->isFileVarDecl())
1122 LV.SetGlobalObjCRef(LV, true);
1123 }
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +00001124 LV.SetObjCArray(LV, E->getType()->isArrayType());
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001125 return;
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +00001126 }
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001127
1128 if (const UnaryOperator *Exp = dyn_cast<UnaryOperator>(E)) {
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +00001129 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001130 return;
1131 }
1132
1133 if (const ParenExpr *Exp = dyn_cast<ParenExpr>(E)) {
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +00001134 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
Fariborz Jahaniane01e4342009-09-30 17:10:29 +00001135 if (LV.isObjCIvar()) {
1136 // If cast is to a structure pointer, follow gcc's behavior and make it
1137 // a non-ivar write-barrier.
1138 QualType ExpTy = E->getType();
1139 if (ExpTy->isPointerType())
1140 ExpTy = ExpTy->getAs<PointerType>()->getPointeeType();
1141 if (ExpTy->isRecordType())
1142 LV.SetObjCIvar(LV, false);
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001143 }
1144 return;
Fariborz Jahaniane01e4342009-09-30 17:10:29 +00001145 }
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001146 if (const ImplicitCastExpr *Exp = dyn_cast<ImplicitCastExpr>(E)) {
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +00001147 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001148 return;
1149 }
1150
1151 if (const CStyleCastExpr *Exp = dyn_cast<CStyleCastExpr>(E)) {
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +00001152 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001153 return;
1154 }
1155
1156 if (const ArraySubscriptExpr *Exp = dyn_cast<ArraySubscriptExpr>(E)) {
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +00001157 setObjCGCLValueClass(Ctx, Exp->getBase(), LV);
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +00001158 if (LV.isObjCIvar() && !LV.isObjCArray())
Fariborz Jahanian2e32ddc2009-09-18 00:04:00 +00001159 // Using array syntax to assigning to what an ivar points to is not
1160 // same as assigning to the ivar itself. {id *Names;} Names[i] = 0;
1161 LV.SetObjCIvar(LV, false);
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +00001162 else if (LV.isGlobalObjCRef() && !LV.isObjCArray())
1163 // Using array syntax to assigning to what global points to is not
1164 // same as assigning to the global itself. {id *G;} G[i] = 0;
1165 LV.SetGlobalObjCRef(LV, false);
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001166 return;
Fariborz Jahanian2e32ddc2009-09-18 00:04:00 +00001167 }
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001168
1169 if (const MemberExpr *Exp = dyn_cast<MemberExpr>(E)) {
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +00001170 setObjCGCLValueClass(Ctx, Exp->getBase(), LV);
Fariborz Jahanian2e32ddc2009-09-18 00:04:00 +00001171 // We don't know if member is an 'ivar', but this flag is looked at
1172 // only in the context of LV.isObjCIvar().
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +00001173 LV.SetObjCArray(LV, E->getType()->isArrayType());
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001174 return;
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +00001175 }
1176}
1177
Anders Carlssonea4c30b2009-11-07 23:06:58 +00001178static LValue EmitGlobalVarDeclLValue(CodeGenFunction &CGF,
1179 const Expr *E, const VarDecl *VD) {
Daniel Dunbar7e215ea2009-11-08 09:46:46 +00001180 assert((VD->hasExternalStorage() || VD->isFileVarDecl()) &&
Anders Carlssonea4c30b2009-11-07 23:06:58 +00001181 "Var decl must have external storage or be a file var decl!");
1182
1183 llvm::Value *V = CGF.CGM.GetAddrOfGlobalVar(VD);
1184 if (VD->getType()->isReferenceType())
1185 V = CGF.Builder.CreateLoad(V, "tmp");
1186 LValue LV = LValue::MakeAddr(V, CGF.MakeQualifiers(E->getType()));
1187 setObjCGCLValueClass(CGF.getContext(), E, LV);
1188 return LV;
1189}
1190
Eli Friedmand15eb34d2009-11-26 06:08:14 +00001191static LValue EmitFunctionDeclLValue(CodeGenFunction &CGF,
1192 const Expr *E, const FunctionDecl *FD) {
1193 llvm::Value* V = CGF.CGM.GetAddrOfFunction(FD);
1194 if (!FD->hasPrototype()) {
1195 if (const FunctionProtoType *Proto =
1196 FD->getType()->getAs<FunctionProtoType>()) {
1197 // Ugly case: for a K&R-style definition, the type of the definition
1198 // isn't the same as the type of a use. Correct for this with a
1199 // bitcast.
1200 QualType NoProtoType =
1201 CGF.getContext().getFunctionNoProtoType(Proto->getResultType());
1202 NoProtoType = CGF.getContext().getPointerType(NoProtoType);
1203 V = CGF.Builder.CreateBitCast(V, CGF.ConvertType(NoProtoType), "tmp");
1204 }
1205 }
1206 return LValue::MakeAddr(V, CGF.MakeQualifiers(E->getType()));
1207}
1208
Chris Lattnerd7f58862007-06-02 05:24:33 +00001209LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) {
Anders Carlsson2ff6395d2009-11-07 22:53:10 +00001210 const NamedDecl *ND = E->getDecl();
Mike Stump4a3999f2009-09-09 13:00:44 +00001211
Rafael Espindola2e42fec2010-03-04 18:17:24 +00001212 if (ND->hasAttr<WeakRefAttr>()) {
1213 const ValueDecl* VD = cast<ValueDecl>(ND);
1214 llvm::Constant *Aliasee = CGM.GetWeakRefReference(VD);
1215
1216 Qualifiers Quals = MakeQualifiers(E->getType());
1217 LValue LV = LValue::MakeAddr(Aliasee, Quals);
1218
1219 return LV;
1220 }
1221
Anders Carlsson2ff6395d2009-11-07 22:53:10 +00001222 if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
Anders Carlsson2ff6395d2009-11-07 22:53:10 +00001223
1224 // Check if this is a global variable.
Anders Carlssonea4c30b2009-11-07 23:06:58 +00001225 if (VD->hasExternalStorage() || VD->isFileVarDecl())
1226 return EmitGlobalVarDeclLValue(*this, E, VD);
Anders Carlsson6eee9722009-11-07 22:46:42 +00001227
1228 bool NonGCable = VD->hasLocalStorage() && !VD->hasAttr<BlocksAttr>();
1229
1230 llvm::Value *V = LocalDeclMap[VD];
Fariborz Jahanian4d55b2d2010-04-19 18:15:02 +00001231 if (!V && getContext().getLangOptions().CPlusPlus &&
1232 VD->isStaticLocal())
1233 V = CGM.getStaticLocalDeclAddress(VD);
Anders Carlsson6eee9722009-11-07 22:46:42 +00001234 assert(V && "DeclRefExpr not entered in LocalDeclMap?");
1235
1236 Qualifiers Quals = MakeQualifiers(E->getType());
1237 // local variables do not get their gc attribute set.
1238 // local static?
1239 if (NonGCable) Quals.removeObjCGCAttr();
1240
1241 if (VD->hasAttr<BlocksAttr>()) {
1242 V = Builder.CreateStructGEP(V, 1, "forwarding");
Daniel Dunbarc76493a2009-11-29 21:23:36 +00001243 V = Builder.CreateLoad(V);
Anders Carlsson6eee9722009-11-07 22:46:42 +00001244 V = Builder.CreateStructGEP(V, getByRefValueLLVMField(VD),
1245 VD->getNameAsString());
1246 }
1247 if (VD->getType()->isReferenceType())
1248 V = Builder.CreateLoad(V, "tmp");
Anders Carlssonea4c30b2009-11-07 23:06:58 +00001249 LValue LV = LValue::MakeAddr(V, Quals);
Anders Carlsson6eee9722009-11-07 22:46:42 +00001250 LValue::SetObjCNonGC(LV, NonGCable);
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +00001251 setObjCGCLValueClass(getContext(), E, LV);
Fariborz Jahanian003e8302008-11-20 00:15:42 +00001252 return LV;
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001253 }
1254
Eli Friedmand15eb34d2009-11-26 06:08:14 +00001255 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
1256 return EmitFunctionDeclLValue(*this, E, FD);
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001257
Anders Carlsson259688c2010-02-02 03:37:46 +00001258 // FIXME: the qualifier check does not seem sufficient here
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001259 if (E->getQualifier()) {
Anders Carlsson259688c2010-02-02 03:37:46 +00001260 const FieldDecl *FD = cast<FieldDecl>(ND);
1261 llvm::Value *V = CGM.EmitPointerToDataMember(FD);
1262
1263 return LValue::MakeAddr(V, MakeQualifiers(FD->getType()));
Chris Lattner5696e7b2008-06-17 18:05:57 +00001264 }
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001265
Anders Carlsson2ff6395d2009-11-07 22:53:10 +00001266 assert(false && "Unhandled DeclRefExpr");
1267
1268 // an invalid LValue, but the assert will
1269 // ensure that this point is never reached.
Chris Lattner793d10c2007-09-16 19:23:47 +00001270 return LValue();
Chris Lattnerd7f58862007-06-02 05:24:33 +00001271}
Chris Lattnere47e4402007-06-01 18:02:12 +00001272
Mike Stump1db7d042009-02-28 09:07:16 +00001273LValue CodeGenFunction::EmitBlockDeclRefLValue(const BlockDeclRefExpr *E) {
John McCall8ccfcb52009-09-24 19:53:00 +00001274 return LValue::MakeAddr(GetAddrOfBlockDecl(E), MakeQualifiers(E->getType()));
Mike Stump1db7d042009-02-28 09:07:16 +00001275}
1276
Chris Lattner8394d792007-06-05 20:53:16 +00001277LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) {
1278 // __extension__ doesn't affect lvalue-ness.
1279 if (E->getOpcode() == UnaryOperator::Extension)
1280 return EmitLValue(E->getSubExpr());
Mike Stump4a3999f2009-09-09 13:00:44 +00001281
Chris Lattner0f398c42008-07-26 22:37:01 +00001282 QualType ExprTy = getContext().getCanonicalType(E->getSubExpr()->getType());
Chris Lattner595db862007-10-30 22:53:42 +00001283 switch (E->getOpcode()) {
1284 default: assert(0 && "Unknown unary operator lvalue!");
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001285 case UnaryOperator::Deref: {
1286 QualType T = E->getSubExpr()->getType()->getPointeeType();
1287 assert(!T.isNull() && "CodeGenFunction::EmitUnaryOpLValue: Illegal type");
Mike Stump4a3999f2009-09-09 13:00:44 +00001288
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001289 Qualifiers Quals = MakeQualifiers(T);
1290 Quals.setAddressSpace(ExprTy.getAddressSpace());
John McCall8ccfcb52009-09-24 19:53:00 +00001291
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001292 LValue LV = LValue::MakeAddr(EmitScalarExpr(E->getSubExpr()), Quals);
1293 // We should not generate __weak write barrier on indirect reference
1294 // of a pointer to object; as in void foo (__weak id *param); *param = 0;
1295 // But, we continue to generate __strong write barrier on indirect write
1296 // into a pointer to object.
1297 if (getContext().getLangOptions().ObjC1 &&
1298 getContext().getLangOptions().getGCMode() != LangOptions::NonGC &&
1299 LV.isObjCWeak())
1300 LValue::SetObjCNonGC(LV, !E->isOBJCGCCandidate(getContext()));
1301 return LV;
1302 }
Chris Lattner595db862007-10-30 22:53:42 +00001303 case UnaryOperator::Real:
Eli Friedmana72bf0f2009-11-09 04:20:47 +00001304 case UnaryOperator::Imag: {
Chris Lattner595db862007-10-30 22:53:42 +00001305 LValue LV = EmitLValue(E->getSubExpr());
Chris Lattner3e593cd2008-03-19 05:19:41 +00001306 unsigned Idx = E->getOpcode() == UnaryOperator::Imag;
1307 return LValue::MakeAddr(Builder.CreateStructGEP(LV.getAddress(),
Chris Lattner574dee62008-07-26 22:17:49 +00001308 Idx, "idx"),
John McCall8ccfcb52009-09-24 19:53:00 +00001309 MakeQualifiers(ExprTy));
Chris Lattner595db862007-10-30 22:53:42 +00001310 }
Eli Friedmana72bf0f2009-11-09 04:20:47 +00001311 case UnaryOperator::PreInc:
Chris Lattnerbb8976e2010-01-09 21:44:40 +00001312 case UnaryOperator::PreDec: {
1313 LValue LV = EmitLValue(E->getSubExpr());
1314 bool isInc = E->getOpcode() == UnaryOperator::PreInc;
1315
1316 if (E->getType()->isAnyComplexType())
1317 EmitComplexPrePostIncDec(E, LV, isInc, true/*isPre*/);
1318 else
1319 EmitScalarPrePostIncDec(E, LV, isInc, true/*isPre*/);
1320 return LV;
1321 }
Eli Friedmana72bf0f2009-11-09 04:20:47 +00001322 }
Chris Lattner8394d792007-06-05 20:53:16 +00001323}
1324
Chris Lattner4347e3692007-06-06 04:54:52 +00001325LValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) {
John McCall8ccfcb52009-09-24 19:53:00 +00001326 return LValue::MakeAddr(CGM.GetAddrOfConstantStringFromLiteral(E),
1327 Qualifiers());
Chris Lattner4347e3692007-06-06 04:54:52 +00001328}
1329
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00001330LValue CodeGenFunction::EmitObjCEncodeExprLValue(const ObjCEncodeExpr *E) {
John McCall8ccfcb52009-09-24 19:53:00 +00001331 return LValue::MakeAddr(CGM.GetAddrOfConstantStringFromObjCEncode(E),
1332 Qualifiers());
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00001333}
1334
1335
Daniel Dunbarb3517472008-10-17 21:58:32 +00001336LValue CodeGenFunction::EmitPredefinedFunctionName(unsigned Type) {
Anders Carlsson625bfc82007-07-21 05:21:51 +00001337 std::string GlobalVarName;
Daniel Dunbarb3517472008-10-17 21:58:32 +00001338
1339 switch (Type) {
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001340 default: assert(0 && "Invalid type");
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00001341 case PredefinedExpr::Func:
1342 GlobalVarName = "__func__.";
1343 break;
1344 case PredefinedExpr::Function:
1345 GlobalVarName = "__FUNCTION__.";
1346 break;
1347 case PredefinedExpr::PrettyFunction:
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00001348 GlobalVarName = "__PRETTY_FUNCTION__.";
1349 break;
Anders Carlsson625bfc82007-07-21 05:21:51 +00001350 }
Daniel Dunbarb3517472008-10-17 21:58:32 +00001351
Daniel Dunbar0482cfd2009-09-12 23:06:21 +00001352 llvm::StringRef FnName = CurFn->getName();
1353 if (FnName.startswith("\01"))
1354 FnName = FnName.substr(1);
1355 GlobalVarName += FnName;
1356
Anders Carlsson2fb08242009-09-08 18:24:21 +00001357 std::string FunctionName =
Anders Carlsson5bd8d192010-02-11 18:20:28 +00001358 PredefinedExpr::ComputeName((PredefinedExpr::IdentType)Type, CurCodeDecl);
Daniel Dunbarb3517472008-10-17 21:58:32 +00001359
Mike Stump4a3999f2009-09-09 13:00:44 +00001360 llvm::Constant *C =
Daniel Dunbarb3517472008-10-17 21:58:32 +00001361 CGM.GetAddrOfConstantCString(FunctionName, GlobalVarName.c_str());
John McCall8ccfcb52009-09-24 19:53:00 +00001362 return LValue::MakeAddr(C, Qualifiers());
Daniel Dunbarb3517472008-10-17 21:58:32 +00001363}
1364
Mike Stump4a3999f2009-09-09 13:00:44 +00001365LValue CodeGenFunction::EmitPredefinedLValue(const PredefinedExpr *E) {
Daniel Dunbarb3517472008-10-17 21:58:32 +00001366 switch (E->getIdentType()) {
1367 default:
1368 return EmitUnsupportedLValue(E, "predefined expression");
1369 case PredefinedExpr::Func:
1370 case PredefinedExpr::Function:
1371 case PredefinedExpr::PrettyFunction:
1372 return EmitPredefinedFunctionName(E->getIdentType());
1373 }
Anders Carlsson625bfc82007-07-21 05:21:51 +00001374}
1375
Mike Stumpcf16d2c2009-12-15 01:22:35 +00001376llvm::BasicBlock *CodeGenFunction::getTrapBB() {
Mike Stump9a4e0122009-12-15 00:59:40 +00001377 const CodeGenOptions &GCO = CGM.getCodeGenOpts();
1378
1379 // If we are not optimzing, don't collapse all calls to trap in the function
1380 // to the same call, that way, in the debugger they can see which operation
1381 // did in fact fail. If we are optimizing, we collpase all call to trap down
1382 // to just one per function to save on codesize.
1383 if (GCO.OptimizationLevel
1384 && TrapBB)
Mike Stumpe8c3b3e2009-12-15 00:35:12 +00001385 return TrapBB;
Mike Stumpd9546382009-12-12 01:27:46 +00001386
1387 llvm::BasicBlock *Cont = 0;
1388 if (HaveInsertPoint()) {
1389 Cont = createBasicBlock("cont");
1390 EmitBranch(Cont);
1391 }
Mike Stumpe8c3b3e2009-12-15 00:35:12 +00001392 TrapBB = createBasicBlock("trap");
1393 EmitBlock(TrapBB);
1394
1395 llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::trap, 0, 0);
1396 llvm::CallInst *TrapCall = Builder.CreateCall(F);
1397 TrapCall->setDoesNotReturn();
1398 TrapCall->setDoesNotThrow();
Mike Stumpd9546382009-12-12 01:27:46 +00001399 Builder.CreateUnreachable();
1400
1401 if (Cont)
1402 EmitBlock(Cont);
Mike Stumpe8c3b3e2009-12-15 00:35:12 +00001403 return TrapBB;
Mike Stumpd9546382009-12-12 01:27:46 +00001404}
1405
Chris Lattnerd9d2fb12007-06-08 23:31:14 +00001406LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) {
Ted Kremenekc81614d2007-08-20 16:18:38 +00001407 // The index must always be an integer, which is not an aggregate. Emit it.
Chris Lattner2da04b32007-08-24 05:35:26 +00001408 llvm::Value *Idx = EmitScalarExpr(E->getIdx());
Eli Friedman07bbeca2009-06-06 19:09:26 +00001409 QualType IdxTy = E->getIdx()->getType();
1410 bool IdxSigned = IdxTy->isSignedIntegerType();
1411
Chris Lattner08c4b9f2007-07-10 21:17:59 +00001412 // If the base is a vector type, then we are forming a vector element lvalue
1413 // with this subscript.
Eli Friedman327944b2008-06-13 23:01:12 +00001414 if (E->getBase()->getType()->isVectorType()) {
Chris Lattner08c4b9f2007-07-10 21:17:59 +00001415 // Emit the vector as an lvalue to get its address.
Eli Friedman327944b2008-06-13 23:01:12 +00001416 LValue LHS = EmitLValue(E->getBase());
Ted Kremenekc81614d2007-08-20 16:18:38 +00001417 assert(LHS.isSimple() && "Can only subscript lvalue vectors here!");
Mike Stump4a3999f2009-09-09 13:00:44 +00001418 Idx = Builder.CreateIntCast(Idx,
Owen Anderson41a75022009-08-13 21:57:51 +00001419 llvm::Type::getInt32Ty(VMContext), IdxSigned, "vidx");
Eli Friedman327944b2008-06-13 23:01:12 +00001420 return LValue::MakeVectorElt(LHS.getAddress(), Idx,
John McCall8ccfcb52009-09-24 19:53:00 +00001421 E->getBase()->getType().getCVRQualifiers());
Chris Lattner08c4b9f2007-07-10 21:17:59 +00001422 }
Mike Stump4a3999f2009-09-09 13:00:44 +00001423
Ted Kremenekc81614d2007-08-20 16:18:38 +00001424 // The base must be a pointer, which is not an aggregate. Emit it.
Chris Lattner2da04b32007-08-24 05:35:26 +00001425 llvm::Value *Base = EmitScalarExpr(E->getBase());
Mike Stump4a3999f2009-09-09 13:00:44 +00001426
Ted Kremenekc81614d2007-08-20 16:18:38 +00001427 // Extend or truncate the index type to 32 or 64-bits.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001428 unsigned IdxBitwidth = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
Sanjiv Gupta47425152009-04-24 02:40:57 +00001429 if (IdxBitwidth != LLVMPointerWidth)
Owen Anderson41a75022009-08-13 21:57:51 +00001430 Idx = Builder.CreateIntCast(Idx,
1431 llvm::IntegerType::get(VMContext, LLVMPointerWidth),
Chris Lattnerd9d2fb12007-06-08 23:31:14 +00001432 IdxSigned, "idxprom");
1433
Mike Stump3f6f9fe2009-12-16 02:57:00 +00001434 // FIXME: As llvm implements the object size checking, this can come out.
Mike Stumpd9546382009-12-12 01:27:46 +00001435 if (CatchUndefined) {
Mike Stump3f6f9fe2009-12-16 02:57:00 +00001436 if (const ImplicitCastExpr *ICE=dyn_cast<ImplicitCastExpr>(E->getBase())) {
Mike Stumpd9546382009-12-12 01:27:46 +00001437 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr())) {
1438 if (ICE->getCastKind() == CastExpr::CK_ArrayToPointerDecay) {
1439 if (const ConstantArrayType *CAT
1440 = getContext().getAsConstantArrayType(DRE->getType())) {
1441 llvm::APInt Size = CAT->getSize();
1442 llvm::BasicBlock *Cont = createBasicBlock("cont");
Mike Stump590d18f2009-12-14 22:14:31 +00001443 Builder.CreateCondBr(Builder.CreateICmpULE(Idx,
Mike Stumpd9546382009-12-12 01:27:46 +00001444 llvm::ConstantInt::get(Idx->getType(), Size)),
Mike Stumpe8c3b3e2009-12-15 00:35:12 +00001445 Cont, getTrapBB());
Mike Stumpf8858af2009-12-14 20:52:00 +00001446 EmitBlock(Cont);
Mike Stumpd9546382009-12-12 01:27:46 +00001447 }
1448 }
1449 }
1450 }
1451 }
1452
Mike Stump4a3999f2009-09-09 13:00:44 +00001453 // We know that the pointer points to a type of the correct size, unless the
1454 // size is a VLA or Objective-C interface.
Daniel Dunbaref2ffbc2009-04-25 05:08:32 +00001455 llvm::Value *Address = 0;
Mike Stump4a3999f2009-09-09 13:00:44 +00001456 if (const VariableArrayType *VAT =
Anders Carlsson3d312f82008-12-21 00:11:23 +00001457 getContext().getAsVariableArrayType(E->getType())) {
Chris Lattner19efdd62009-08-14 23:43:22 +00001458 llvm::Value *VLASize = GetVLASize(VAT);
Mike Stump4a3999f2009-09-09 13:00:44 +00001459
Anders Carlsson3d312f82008-12-21 00:11:23 +00001460 Idx = Builder.CreateMul(Idx, VLASize);
Mike Stump4a3999f2009-09-09 13:00:44 +00001461
Anders Carlssone0808df2008-12-21 03:44:36 +00001462 QualType BaseType = getContext().getBaseElementType(VAT);
Mike Stump4a3999f2009-09-09 13:00:44 +00001463
Ken Dyck40775002010-01-11 17:06:35 +00001464 CharUnits BaseTypeSize = getContext().getTypeSizeInChars(BaseType);
Anders Carlsson3d312f82008-12-21 00:11:23 +00001465 Idx = Builder.CreateUDiv(Idx,
Mike Stump4a3999f2009-09-09 13:00:44 +00001466 llvm::ConstantInt::get(Idx->getType(),
Ken Dyck40775002010-01-11 17:06:35 +00001467 BaseTypeSize.getQuantity()));
Dan Gohman43b44842009-08-12 00:33:55 +00001468 Address = Builder.CreateInBoundsGEP(Base, Idx, "arrayidx");
John McCall8b07ec22010-05-15 11:32:37 +00001469 } else if (const ObjCObjectType *OIT =
1470 E->getType()->getAs<ObjCObjectType>()) {
Mike Stump4a3999f2009-09-09 13:00:44 +00001471 llvm::Value *InterfaceSize =
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001472 llvm::ConstantInt::get(Idx->getType(),
Ken Dyck40775002010-01-11 17:06:35 +00001473 getContext().getTypeSizeInChars(OIT).getQuantity());
Mike Stump4a3999f2009-09-09 13:00:44 +00001474
Daniel Dunbaref2ffbc2009-04-25 05:08:32 +00001475 Idx = Builder.CreateMul(Idx, InterfaceSize);
1476
Benjamin Kramerabd5b902009-10-13 10:07:13 +00001477 const llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(VMContext);
Dan Gohman43b44842009-08-12 00:33:55 +00001478 Address = Builder.CreateGEP(Builder.CreateBitCast(Base, i8PTy),
Daniel Dunbaref2ffbc2009-04-25 05:08:32 +00001479 Idx, "arrayidx");
1480 Address = Builder.CreateBitCast(Address, Base->getType());
1481 } else {
Dan Gohman43b44842009-08-12 00:33:55 +00001482 Address = Builder.CreateInBoundsGEP(Base, Idx, "arrayidx");
Anders Carlsson3d312f82008-12-21 00:11:23 +00001483 }
Mike Stump4a3999f2009-09-09 13:00:44 +00001484
Steve Naroff7cae42b2009-07-10 23:34:53 +00001485 QualType T = E->getBase()->getType()->getPointeeType();
Mike Stump4a3999f2009-09-09 13:00:44 +00001486 assert(!T.isNull() &&
Steve Naroff7cae42b2009-07-10 23:34:53 +00001487 "CodeGenFunction::EmitArraySubscriptExpr(): Illegal base type");
Mike Stump4a3999f2009-09-09 13:00:44 +00001488
John McCall8ccfcb52009-09-24 19:53:00 +00001489 Qualifiers Quals = MakeQualifiers(T);
1490 Quals.setAddressSpace(E->getBase()->getType().getAddressSpace());
1491
1492 LValue LV = LValue::MakeAddr(Address, Quals);
Fariborz Jahaniana9fecf32009-02-21 23:37:19 +00001493 if (getContext().getLangOptions().ObjC1 &&
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +00001494 getContext().getLangOptions().getGCMode() != LangOptions::NonGC) {
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001495 LValue::SetObjCNonGC(LV, !E->isOBJCGCCandidate(getContext()));
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +00001496 setObjCGCLValueClass(getContext(), E, LV);
1497 }
Fariborz Jahaniana9fecf32009-02-21 23:37:19 +00001498 return LV;
Chris Lattnerd9d2fb12007-06-08 23:31:14 +00001499}
1500
Mike Stump4a3999f2009-09-09 13:00:44 +00001501static
Owen Anderson170229f2009-07-14 23:10:40 +00001502llvm::Constant *GenerateConstantVector(llvm::LLVMContext &VMContext,
1503 llvm::SmallVector<unsigned, 4> &Elts) {
Chris Lattner4e1a3232009-12-23 21:31:11 +00001504 llvm::SmallVector<llvm::Constant*, 4> CElts;
Mike Stump4a3999f2009-09-09 13:00:44 +00001505
Nate Begemand3862152008-05-13 21:03:02 +00001506 for (unsigned i = 0, e = Elts.size(); i != e; ++i)
Owen Anderson41a75022009-08-13 21:57:51 +00001507 CElts.push_back(llvm::ConstantInt::get(
1508 llvm::Type::getInt32Ty(VMContext), Elts[i]));
Nate Begemand3862152008-05-13 21:03:02 +00001509
Owen Anderson3cc120a2009-07-28 21:22:35 +00001510 return llvm::ConstantVector::get(&CElts[0], CElts.size());
Nate Begemand3862152008-05-13 21:03:02 +00001511}
1512
Chris Lattner9e751ca2007-08-02 23:37:31 +00001513LValue CodeGenFunction::
Nate Begemance4d7fc2008-04-18 23:10:10 +00001514EmitExtVectorElementExpr(const ExtVectorElementExpr *E) {
Chris Lattner4e1a3232009-12-23 21:31:11 +00001515 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
1516
Chris Lattner9e751ca2007-08-02 23:37:31 +00001517 // Emit the base vector as an l-value.
Chris Lattner6c7ce102009-02-16 21:11:58 +00001518 LValue Base;
1519
1520 // ExtVectorElementExpr's base can either be a vector or pointer to vector.
Chris Lattner4e1a3232009-12-23 21:31:11 +00001521 if (E->isArrow()) {
1522 // If it is a pointer to a vector, emit the address and form an lvalue with
1523 // it.
Chris Lattnerb8211f62009-02-16 22:14:05 +00001524 llvm::Value *Ptr = EmitScalarExpr(E->getBase());
Chris Lattner4e1a3232009-12-23 21:31:11 +00001525 const PointerType *PT = E->getBase()->getType()->getAs<PointerType>();
John McCall8ccfcb52009-09-24 19:53:00 +00001526 Qualifiers Quals = MakeQualifiers(PT->getPointeeType());
1527 Quals.removeObjCGCAttr();
1528 Base = LValue::MakeAddr(Ptr, Quals);
Chris Lattner4e1a3232009-12-23 21:31:11 +00001529 } else if (E->getBase()->isLvalue(getContext()) == Expr::LV_Valid) {
1530 // Otherwise, if the base is an lvalue ( as in the case of foo.x.x),
1531 // emit the base as an lvalue.
1532 assert(E->getBase()->getType()->isVectorType());
1533 Base = EmitLValue(E->getBase());
1534 } else {
1535 // Otherwise, the base is a normal rvalue (as in (V+V).x), emit it as such.
Daniel Dunbar5b901952010-01-04 18:02:28 +00001536 assert(E->getBase()->getType()->getAs<VectorType>() &&
1537 "Result must be a vector");
Chris Lattner4e1a3232009-12-23 21:31:11 +00001538 llvm::Value *Vec = EmitScalarExpr(E->getBase());
1539
Chris Lattnerf0a9ba32009-12-23 21:33:41 +00001540 // Store the vector to memory (because LValue wants an address).
Daniel Dunbara7566f12010-02-09 02:48:28 +00001541 llvm::Value *VecMem = CreateMemTemp(E->getBase()->getType());
Chris Lattner4e1a3232009-12-23 21:31:11 +00001542 Builder.CreateStore(Vec, VecMem);
Chris Lattnerf0a9ba32009-12-23 21:33:41 +00001543 Base = LValue::MakeAddr(VecMem, Qualifiers());
Chris Lattner4e1a3232009-12-23 21:31:11 +00001544 }
1545
Nate Begemand3862152008-05-13 21:03:02 +00001546 // Encode the element access list into a vector of unsigned indices.
1547 llvm::SmallVector<unsigned, 4> Indices;
1548 E->getEncodedElementAccess(Indices);
1549
1550 if (Base.isSimple()) {
Owen Anderson170229f2009-07-14 23:10:40 +00001551 llvm::Constant *CV = GenerateConstantVector(VMContext, Indices);
Eli Friedman327944b2008-06-13 23:01:12 +00001552 return LValue::MakeExtVectorElt(Base.getAddress(), CV,
John McCall8ccfcb52009-09-24 19:53:00 +00001553 Base.getVRQualifiers());
Nate Begemand3862152008-05-13 21:03:02 +00001554 }
1555 assert(Base.isExtVectorElt() && "Can only subscript lvalue vec elts here!");
1556
1557 llvm::Constant *BaseElts = Base.getExtVectorElts();
1558 llvm::SmallVector<llvm::Constant *, 4> CElts;
1559
1560 for (unsigned i = 0, e = Indices.size(); i != e; ++i) {
1561 if (isa<llvm::ConstantAggregateZero>(BaseElts))
Chris Lattner5e71d432009-10-28 05:12:07 +00001562 CElts.push_back(llvm::ConstantInt::get(Int32Ty, 0));
Nate Begemand3862152008-05-13 21:03:02 +00001563 else
Chris Lattner5e71d432009-10-28 05:12:07 +00001564 CElts.push_back(cast<llvm::Constant>(BaseElts->getOperand(Indices[i])));
Nate Begemand3862152008-05-13 21:03:02 +00001565 }
Owen Anderson3cc120a2009-07-28 21:22:35 +00001566 llvm::Constant *CV = llvm::ConstantVector::get(&CElts[0], CElts.size());
Eli Friedman327944b2008-06-13 23:01:12 +00001567 return LValue::MakeExtVectorElt(Base.getExtVectorAddr(), CV,
John McCall8ccfcb52009-09-24 19:53:00 +00001568 Base.getVRQualifiers());
Chris Lattner9e751ca2007-08-02 23:37:31 +00001569}
1570
Devang Patel30efa2e2007-10-23 20:28:39 +00001571LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) {
Fariborz Jahanian10bec102009-02-21 00:30:43 +00001572 bool isNonGC = false;
Devang Pateld68df202007-10-24 22:26:28 +00001573 Expr *BaseExpr = E->getBase();
Devang Pateld68df202007-10-24 22:26:28 +00001574 llvm::Value *BaseValue = NULL;
John McCall8ccfcb52009-09-24 19:53:00 +00001575 Qualifiers BaseQuals;
Eli Friedman327944b2008-06-13 23:01:12 +00001576
Chris Lattner4e4186b2007-12-02 18:52:07 +00001577 // 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 +00001578 if (E->isArrow()) {
Devang Patel7718d7a2007-10-26 18:15:21 +00001579 BaseValue = EmitScalarExpr(BaseExpr);
Mike Stump4a3999f2009-09-09 13:00:44 +00001580 const PointerType *PTy =
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001581 BaseExpr->getType()->getAs<PointerType>();
John McCall8ccfcb52009-09-24 19:53:00 +00001582 BaseQuals = PTy->getPointeeType().getQualifiers();
Fariborz Jahanian1a504772009-09-01 17:02:21 +00001583 } else if (isa<ObjCPropertyRefExpr>(BaseExpr->IgnoreParens()) ||
1584 isa<ObjCImplicitSetterGetterRefExpr>(
1585 BaseExpr->IgnoreParens())) {
Fariborz Jahanian30e78642009-01-12 23:27:26 +00001586 RValue RV = EmitObjCPropertyGet(BaseExpr);
1587 BaseValue = RV.getAggregateAddr();
John McCall8ccfcb52009-09-24 19:53:00 +00001588 BaseQuals = BaseExpr->getType().getQualifiers();
Chris Lattnere084c012009-02-16 22:25:49 +00001589 } else {
Chris Lattner4e4186b2007-12-02 18:52:07 +00001590 LValue BaseLV = EmitLValue(BaseExpr);
Fariborz Jahanian10bec102009-02-21 00:30:43 +00001591 if (BaseLV.isNonGC())
1592 isNonGC = true;
Chris Lattner4e4186b2007-12-02 18:52:07 +00001593 // FIXME: this isn't right for bitfields.
1594 BaseValue = BaseLV.getAddress();
Fariborz Jahanian82e28742009-07-29 00:44:13 +00001595 QualType BaseTy = BaseExpr->getType();
John McCall8ccfcb52009-09-24 19:53:00 +00001596 BaseQuals = BaseTy.getQualifiers();
Chris Lattner4e4186b2007-12-02 18:52:07 +00001597 }
Devang Patel30efa2e2007-10-23 20:28:39 +00001598
Anders Carlssonea4c30b2009-11-07 23:06:58 +00001599 NamedDecl *ND = E->getMemberDecl();
1600 if (FieldDecl *Field = dyn_cast<FieldDecl>(ND)) {
Anders Carlsson5d8645b2010-01-29 05:05:36 +00001601 LValue LV = EmitLValueForField(BaseValue, Field,
Anders Carlssonea4c30b2009-11-07 23:06:58 +00001602 BaseQuals.getCVRQualifiers());
1603 LValue::SetObjCNonGC(LV, isNonGC);
1604 setObjCGCLValueClass(getContext(), E, LV);
1605 return LV;
1606 }
1607
Anders Carlsson5bbdc9f2009-11-07 23:16:50 +00001608 if (VarDecl *VD = dyn_cast<VarDecl>(ND))
1609 return EmitGlobalVarDeclLValue(*this, E, VD);
Eli Friedmand15eb34d2009-11-26 06:08:14 +00001610
1611 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
1612 return EmitFunctionDeclLValue(*this, E, FD);
1613
Anders Carlssonea4c30b2009-11-07 23:06:58 +00001614 assert(false && "Unhandled member declaration!");
1615 return LValue();
Eli Friedmana62f3e12008-02-09 08:50:58 +00001616}
Devang Patel30efa2e2007-10-23 20:28:39 +00001617
Fariborz Jahanianb517e902008-12-15 20:35:07 +00001618LValue CodeGenFunction::EmitLValueForBitfield(llvm::Value* BaseValue,
Anders Carlssoncfd30122009-11-17 03:57:07 +00001619 const FieldDecl* Field,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001620 unsigned CVRQualifiers) {
Daniel Dunbar034299e2010-03-31 01:09:11 +00001621 const CGRecordLayout &RL =
1622 CGM.getTypes().getCGRecordLayout(Field->getParent());
Daniel Dunbarcd3d5e72010-04-05 16:20:44 +00001623 const CGBitFieldInfo &Info = RL.getBitFieldInfo(Field);
Daniel Dunbarc75c8bd2010-04-08 02:59:45 +00001624 return LValue::MakeBitfield(BaseValue, Info,
Daniel Dunbardc406b82010-04-05 21:36:35 +00001625 Field->getType().getCVRQualifiers()|CVRQualifiers);
Fariborz Jahanianb517e902008-12-15 20:35:07 +00001626}
1627
John McCallc4094932010-05-21 01:18:57 +00001628/// EmitLValueForAnonRecordField - Given that the field is a member of
1629/// an anonymous struct or union buried inside a record, and given
1630/// that the base value is a pointer to the enclosing record, derive
1631/// an lvalue for the ultimate field.
1632LValue CodeGenFunction::EmitLValueForAnonRecordField(llvm::Value *BaseValue,
1633 const FieldDecl *Field,
1634 unsigned CVRQualifiers) {
1635 llvm::SmallVector<const FieldDecl *, 8> Path;
1636 Path.push_back(Field);
1637
1638 while (Field->getParent()->isAnonymousStructOrUnion()) {
1639 const ValueDecl *VD = Field->getParent()->getAnonymousStructOrUnionObject();
1640 if (!isa<FieldDecl>(VD)) break;
1641 Field = cast<FieldDecl>(VD);
1642 Path.push_back(Field);
1643 }
1644
1645 llvm::SmallVectorImpl<const FieldDecl*>::reverse_iterator
1646 I = Path.rbegin(), E = Path.rend();
1647 while (true) {
1648 LValue LV = EmitLValueForField(BaseValue, *I, CVRQualifiers);
1649 if (++I == E) return LV;
1650
1651 assert(LV.isSimple());
1652 BaseValue = LV.getAddress();
1653 CVRQualifiers |= LV.getVRQualifiers();
1654 }
1655}
1656
Eli Friedmana62f3e12008-02-09 08:50:58 +00001657LValue CodeGenFunction::EmitLValueForField(llvm::Value* BaseValue,
Anders Carlssoncfd30122009-11-17 03:57:07 +00001658 const FieldDecl* Field,
Mike Stump11289f42009-09-09 15:08:12 +00001659 unsigned CVRQualifiers) {
Fariborz Jahanianb517e902008-12-15 20:35:07 +00001660 if (Field->isBitField())
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001661 return EmitLValueForBitfield(BaseValue, Field, CVRQualifiers);
Mike Stump4a3999f2009-09-09 13:00:44 +00001662
Daniel Dunbar034299e2010-03-31 01:09:11 +00001663 const CGRecordLayout &RL =
1664 CGM.getTypes().getCGRecordLayout(Field->getParent());
1665 unsigned idx = RL.getLLVMFieldNo(Field);
Fariborz Jahanianb517e902008-12-15 20:35:07 +00001666 llvm::Value *V = Builder.CreateStructGEP(BaseValue, idx, "tmp");
Eli Friedman133e8042008-05-29 11:33:25 +00001667
Devang Pateled93c3c2007-10-26 19:42:18 +00001668 // Match union field type.
Anders Carlsson5d8645b2010-01-29 05:05:36 +00001669 if (Field->getParent()->isUnion()) {
Mike Stump4a3999f2009-09-09 13:00:44 +00001670 const llvm::Type *FieldTy =
Eli Friedman327944b2008-06-13 23:01:12 +00001671 CGM.getTypes().ConvertTypeForMem(Field->getType());
Mike Stump4a3999f2009-09-09 13:00:44 +00001672 const llvm::PointerType * BaseTy =
Devang Patelffe1e212007-10-30 20:59:40 +00001673 cast<llvm::PointerType>(BaseValue->getType());
Eli Friedman9a5ffcb2008-05-21 13:24:44 +00001674 unsigned AS = BaseTy->getAddressSpace();
Mike Stump4a3999f2009-09-09 13:00:44 +00001675 V = Builder.CreateBitCast(V,
1676 llvm::PointerType::get(FieldTy, AS),
Eli Friedman9a5ffcb2008-05-21 13:24:44 +00001677 "tmp");
Devang Pateled93c3c2007-10-26 19:42:18 +00001678 }
Eli Friedmanf7f9f682009-05-30 21:09:44 +00001679 if (Field->getType()->isReferenceType())
1680 V = Builder.CreateLoad(V, "tmp");
John McCall8ccfcb52009-09-24 19:53:00 +00001681
1682 Qualifiers Quals = MakeQualifiers(Field->getType());
1683 Quals.addCVRQualifiers(CVRQualifiers);
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +00001684 // __weak attribute on a field is ignored.
John McCall8ccfcb52009-09-24 19:53:00 +00001685 if (Quals.getObjCGCAttr() == Qualifiers::Weak)
1686 Quals.removeObjCGCAttr();
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +00001687
John McCall8ccfcb52009-09-24 19:53:00 +00001688 return LValue::MakeAddr(V, Quals);
Devang Patel30efa2e2007-10-23 20:28:39 +00001689}
1690
Anders Carlssondb78f0a2010-01-29 05:24:29 +00001691LValue
1692CodeGenFunction::EmitLValueForFieldInitialization(llvm::Value* BaseValue,
1693 const FieldDecl* Field,
1694 unsigned CVRQualifiers) {
1695 QualType FieldType = Field->getType();
1696
1697 if (!FieldType->isReferenceType())
1698 return EmitLValueForField(BaseValue, Field, CVRQualifiers);
1699
Daniel Dunbar034299e2010-03-31 01:09:11 +00001700 const CGRecordLayout &RL =
1701 CGM.getTypes().getCGRecordLayout(Field->getParent());
1702 unsigned idx = RL.getLLVMFieldNo(Field);
Anders Carlssondb78f0a2010-01-29 05:24:29 +00001703 llvm::Value *V = Builder.CreateStructGEP(BaseValue, idx, "tmp");
1704
1705 assert(!FieldType.getObjCGCAttr() && "fields cannot have GC attrs");
1706
1707 return LValue::MakeAddr(V, MakeQualifiers(FieldType));
1708}
1709
Chris Lattner28bcf1a2009-03-18 18:28:57 +00001710LValue CodeGenFunction::EmitCompoundLiteralLValue(const CompoundLiteralExpr* E){
Daniel Dunbar27bacaf2010-02-16 19:43:39 +00001711 llvm::Value *DeclPtr = CreateMemTemp(E->getType(), ".compoundliteral");
Eli Friedman9fd8b682008-05-13 23:18:27 +00001712 const Expr* InitExpr = E->getInitializer();
John McCall8ccfcb52009-09-24 19:53:00 +00001713 LValue Result = LValue::MakeAddr(DeclPtr, MakeQualifiers(E->getType()));
Eli Friedman9fd8b682008-05-13 23:18:27 +00001714
John McCall21886962010-04-21 10:05:39 +00001715 EmitAnyExprToMem(InitExpr, DeclPtr, /*Volatile*/ false);
Eli Friedman9fd8b682008-05-13 23:18:27 +00001716
1717 return Result;
1718}
1719
Anders Carlsson1450adb2009-09-15 16:35:24 +00001720LValue
1721CodeGenFunction::EmitConditionalOperatorLValue(const ConditionalOperator* E) {
1722 if (E->isLvalue(getContext()) == Expr::LV_Valid) {
Eli Friedman2e06e8b2009-12-25 05:29:40 +00001723 if (int Cond = ConstantFoldsToSimpleInteger(E->getCond())) {
1724 Expr *Live = Cond == 1 ? E->getLHS() : E->getRHS();
1725 if (Live)
1726 return EmitLValue(Live);
1727 }
1728
1729 if (!E->getLHS())
1730 return EmitUnsupportedLValue(E, "conditional operator with missing LHS");
1731
Anders Carlsson1450adb2009-09-15 16:35:24 +00001732 llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true");
1733 llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false");
1734 llvm::BasicBlock *ContBlock = createBasicBlock("cond.end");
1735
Eli Friedmanb8841af2009-12-25 06:17:05 +00001736 EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock);
Anders Carlsson1450adb2009-09-15 16:35:24 +00001737
Anders Carlsson9b942c62010-02-04 17:26:01 +00001738 // Any temporaries created here are conditional.
1739 BeginConditionalBranch();
Anders Carlsson1450adb2009-09-15 16:35:24 +00001740 EmitBlock(LHSBlock);
Anders Carlsson1450adb2009-09-15 16:35:24 +00001741 LValue LHS = EmitLValue(E->getLHS());
Anders Carlsson9b942c62010-02-04 17:26:01 +00001742 EndConditionalBranch();
1743
Anders Carlsson1450adb2009-09-15 16:35:24 +00001744 if (!LHS.isSimple())
1745 return EmitUnsupportedLValue(E, "conditional operator");
1746
Daniel Dunbara7566f12010-02-09 02:48:28 +00001747 // FIXME: We shouldn't need an alloca for this.
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001748 llvm::Value *Temp = CreateTempAlloca(LHS.getAddress()->getType(),"condtmp");
Anders Carlsson1450adb2009-09-15 16:35:24 +00001749 Builder.CreateStore(LHS.getAddress(), Temp);
1750 EmitBranch(ContBlock);
1751
Anders Carlsson9b942c62010-02-04 17:26:01 +00001752 // Any temporaries created here are conditional.
1753 BeginConditionalBranch();
Anders Carlsson1450adb2009-09-15 16:35:24 +00001754 EmitBlock(RHSBlock);
1755 LValue RHS = EmitLValue(E->getRHS());
Anders Carlsson9b942c62010-02-04 17:26:01 +00001756 EndConditionalBranch();
Anders Carlsson1450adb2009-09-15 16:35:24 +00001757 if (!RHS.isSimple())
1758 return EmitUnsupportedLValue(E, "conditional operator");
1759
1760 Builder.CreateStore(RHS.getAddress(), Temp);
1761 EmitBranch(ContBlock);
1762
1763 EmitBlock(ContBlock);
1764
1765 Temp = Builder.CreateLoad(Temp, "lv");
John McCall8ccfcb52009-09-24 19:53:00 +00001766 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
Anders Carlsson1450adb2009-09-15 16:35:24 +00001767 }
1768
Daniel Dunbarbf1fe8c2009-03-24 02:38:23 +00001769 // ?: here should be an aggregate.
Mike Stump4a3999f2009-09-09 13:00:44 +00001770 assert((hasAggregateLLVMType(E->getType()) &&
Daniel Dunbarbf1fe8c2009-03-24 02:38:23 +00001771 !E->getType()->isAnyComplexType()) &&
1772 "Unexpected conditional operator!");
1773
Daniel Dunbard0bc7b92010-02-05 19:38:31 +00001774 return EmitAggExprToLValue(E);
Daniel Dunbarbf1fe8c2009-03-24 02:38:23 +00001775}
1776
Mike Stump65511702009-11-16 06:50:58 +00001777/// EmitCastLValue - Casts are never lvalues unless that cast is a dynamic_cast.
1778/// If the cast is a dynamic_cast, we can have the usual lvalue result,
1779/// otherwise if a cast is needed by the code generator in an lvalue context,
1780/// then it must mean that we need the address of an aggregate in order to
1781/// access one of its fields. This can happen for all the reasons that casts
1782/// are permitted with aggregate result, including noop aggregate casts, and
1783/// cast from scalar to union.
Chris Lattner28bcf1a2009-03-18 18:28:57 +00001784LValue CodeGenFunction::EmitCastLValue(const CastExpr *E) {
Anders Carlssond95f9602009-09-12 16:16:49 +00001785 switch (E->getCastKind()) {
1786 default:
Eli Friedman8c98dff2009-11-16 05:48:01 +00001787 return EmitUnsupportedLValue(E, "unexpected cast lvalue");
1788
Mike Stump65511702009-11-16 06:50:58 +00001789 case CastExpr::CK_Dynamic: {
1790 LValue LV = EmitLValue(E->getSubExpr());
1791 llvm::Value *V = LV.getAddress();
1792 const CXXDynamicCastExpr *DCE = cast<CXXDynamicCastExpr>(E);
1793 return LValue::MakeAddr(EmitDynamicCast(V, DCE),
1794 MakeQualifiers(E->getType()));
1795 }
1796
Fariborz Jahanian43a40f92010-05-10 22:57:35 +00001797 case CastExpr::CK_NoOp: {
1798 LValue LV = EmitLValue(E->getSubExpr());
Fariborz Jahanian43a40f92010-05-10 22:57:35 +00001799 if (LV.isPropertyRef()) {
Fariborz Jahanian2d2623c2010-05-11 16:31:10 +00001800 QualType QT = E->getSubExpr()->getType();
1801 RValue RV = EmitLoadOfPropertyRefLValue(LV, QT);
1802 assert(!RV.isScalar() && "EmitCastLValue - scalar cast of property ref");
1803 llvm::Value *V = RV.getAggregateAddr();
1804 return LValue::MakeAddr(V, MakeQualifiers(QT));
Fariborz Jahanian43a40f92010-05-10 22:57:35 +00001805 }
1806 return LV;
1807 }
Anders Carlssond95f9602009-09-12 16:16:49 +00001808 case CastExpr::CK_ConstructorConversion:
1809 case CastExpr::CK_UserDefinedConversion:
Fariborz Jahanian2b9fc832009-12-15 21:34:52 +00001810 case CastExpr::CK_AnyPointerToObjCPointerCast:
Chris Lattner28bcf1a2009-03-18 18:28:57 +00001811 return EmitLValue(E->getSubExpr());
Anders Carlssond95f9602009-09-12 16:16:49 +00001812
John McCalld9c7c6562010-03-30 23:58:03 +00001813 case CastExpr::CK_UncheckedDerivedToBase:
Anders Carlssond95f9602009-09-12 16:16:49 +00001814 case CastExpr::CK_DerivedToBase: {
1815 const RecordType *DerivedClassTy =
1816 E->getSubExpr()->getType()->getAs<RecordType>();
1817 CXXRecordDecl *DerivedClassDecl =
1818 cast<CXXRecordDecl>(DerivedClassTy->getDecl());
Anders Carlssond95f9602009-09-12 16:16:49 +00001819
1820 LValue LV = EmitLValue(E->getSubExpr());
Fariborz Jahanian64cda8b2010-06-17 23:00:29 +00001821 llvm::Value *This;
1822 if (LV.isPropertyRef()) {
1823 RValue RV = EmitLoadOfPropertyRefLValue(LV, E->getSubExpr()->getType());
1824 assert (!RV.isScalar() && "EmitCastLValue");
1825 This = RV.getAggregateAddr();
1826 }
1827 else
1828 This = LV.getAddress();
Anders Carlssond95f9602009-09-12 16:16:49 +00001829
1830 // Perform the derived-to-base conversion
1831 llvm::Value *Base =
Fariborz Jahanian64cda8b2010-06-17 23:00:29 +00001832 GetAddressOfBaseClass(This, DerivedClassDecl,
Anders Carlssonc6eaea72010-04-24 21:12:55 +00001833 E->getBasePath(), /*NullCheckValue=*/false);
Anders Carlssond95f9602009-09-12 16:16:49 +00001834
John McCall8ccfcb52009-09-24 19:53:00 +00001835 return LValue::MakeAddr(Base, MakeQualifiers(E->getType()));
Anders Carlssond95f9602009-09-12 16:16:49 +00001836 }
Daniel Dunbar9c4e4652010-02-05 20:02:42 +00001837 case CastExpr::CK_ToUnion:
1838 return EmitAggExprToLValue(E);
Eli Friedman8c98dff2009-11-16 05:48:01 +00001839 case CastExpr::CK_BaseToDerived: {
Anders Carlsson8c793172009-11-23 17:57:54 +00001840 const RecordType *DerivedClassTy = E->getType()->getAs<RecordType>();
1841 CXXRecordDecl *DerivedClassDecl =
1842 cast<CXXRecordDecl>(DerivedClassTy->getDecl());
1843
1844 LValue LV = EmitLValue(E->getSubExpr());
1845
1846 // Perform the base-to-derived conversion
1847 llvm::Value *Derived =
Anders Carlsson8a64c1c2010-04-24 21:23:59 +00001848 GetAddressOfDerivedClass(LV.getAddress(), DerivedClassDecl,
1849 E->getBasePath(),/*NullCheckValue=*/false);
Anders Carlsson8c793172009-11-23 17:57:54 +00001850
1851 return LValue::MakeAddr(Derived, MakeQualifiers(E->getType()));
Eli Friedman8c98dff2009-11-16 05:48:01 +00001852 }
Anders Carlsson50cb3212009-11-14 21:21:42 +00001853 case CastExpr::CK_BitCast: {
Eli Friedman8c98dff2009-11-16 05:48:01 +00001854 // This must be a reinterpret_cast (or c-style equivalent).
1855 const ExplicitCastExpr *CE = cast<ExplicitCastExpr>(E);
Anders Carlsson50cb3212009-11-14 21:21:42 +00001856
1857 LValue LV = EmitLValue(E->getSubExpr());
1858 llvm::Value *V = Builder.CreateBitCast(LV.getAddress(),
1859 ConvertType(CE->getTypeAsWritten()));
1860 return LValue::MakeAddr(V, MakeQualifiers(E->getType()));
1861 }
Anders Carlssond95f9602009-09-12 16:16:49 +00001862 }
Chris Lattner28bcf1a2009-03-18 18:28:57 +00001863}
1864
Fariborz Jahaniane4d94ce2009-10-20 23:29:04 +00001865LValue CodeGenFunction::EmitNullInitializationLValue(
1866 const CXXZeroInitValueExpr *E) {
1867 QualType Ty = E->getType();
Daniel Dunbara7566f12010-02-09 02:48:28 +00001868 LValue LV = LValue::MakeAddr(CreateMemTemp(Ty), MakeQualifiers(Ty));
Anders Carlssonc0964b62010-05-22 17:35:42 +00001869 EmitNullInitialization(LV.getAddress(), Ty);
Daniel Dunbara7566f12010-02-09 02:48:28 +00001870 return LV;
Fariborz Jahaniane4d94ce2009-10-20 23:29:04 +00001871}
1872
Chris Lattnere47e4402007-06-01 18:02:12 +00001873//===--------------------------------------------------------------------===//
1874// Expression Emission
1875//===--------------------------------------------------------------------===//
1876
Chris Lattner76ba8492007-08-20 22:37:10 +00001877
Anders Carlsson17490832009-12-24 20:40:36 +00001878RValue CodeGenFunction::EmitCallExpr(const CallExpr *E,
1879 ReturnValueSlot ReturnValue) {
Daniel Dunbarcdbb5e32009-02-20 18:06:48 +00001880 // Builtins never have block type.
Daniel Dunbarbb197e42009-01-09 16:50:52 +00001881 if (E->getCallee()->getType()->isBlockPointerType())
Anders Carlssonbfb36712009-12-24 21:13:40 +00001882 return EmitBlockCallExpr(E, ReturnValue);
Daniel Dunbarbb197e42009-01-09 16:50:52 +00001883
Anders Carlssone5fd6f22009-04-03 22:50:24 +00001884 if (const CXXMemberCallExpr *CE = dyn_cast<CXXMemberCallExpr>(E))
Anders Carlssonbfb36712009-12-24 21:13:40 +00001885 return EmitCXXMemberCallExpr(CE, ReturnValue);
Mike Stump4a3999f2009-09-09 13:00:44 +00001886
Daniel Dunbarcdbb5e32009-02-20 18:06:48 +00001887 const Decl *TargetDecl = 0;
Daniel Dunbar27032de2009-02-20 19:34:33 +00001888 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E->getCallee())) {
1889 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CE->getSubExpr())) {
1890 TargetDecl = DRE->getDecl();
1891 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(TargetDecl))
Douglas Gregor15fc9562009-09-12 00:22:50 +00001892 if (unsigned builtinID = FD->getBuiltinID())
Daniel Dunbar27032de2009-02-20 19:34:33 +00001893 return EmitBuiltinExpr(FD, builtinID, E);
Daniel Dunbarcdbb5e32009-02-20 18:06:48 +00001894 }
1895 }
1896
Chris Lattner4ca97c32009-06-13 00:26:38 +00001897 if (const CXXOperatorCallExpr *CE = dyn_cast<CXXOperatorCallExpr>(E))
Anders Carlsson4034a952009-05-27 04:18:27 +00001898 if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(TargetDecl))
Anders Carlssonbfb36712009-12-24 21:13:40 +00001899 return EmitCXXOperatorMemberCallExpr(CE, MD, ReturnValue);
Mike Stump4a3999f2009-09-09 13:00:44 +00001900
Eli Friedman8aaff692009-12-08 02:09:46 +00001901 if (isa<CXXPseudoDestructorExpr>(E->getCallee()->IgnoreParens())) {
Douglas Gregorad8a3362009-09-04 17:36:40 +00001902 // C++ [expr.pseudo]p1:
Mike Stump4a3999f2009-09-09 13:00:44 +00001903 // The result shall only be used as the operand for the function call
Douglas Gregorad8a3362009-09-04 17:36:40 +00001904 // operator (), and the result of such a call has type void. The only
1905 // effect is the evaluation of the postfix-expression before the dot or
1906 // arrow.
1907 EmitScalarExpr(E->getCallee());
1908 return RValue::get(0);
1909 }
Mike Stump4a3999f2009-09-09 13:00:44 +00001910
Chris Lattner2da04b32007-08-24 05:35:26 +00001911 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
Anders Carlsson17490832009-12-24 20:40:36 +00001912 return EmitCall(E->getCallee()->getType(), Callee, ReturnValue,
Anders Carlsson3a9463b2009-05-27 01:22:39 +00001913 E->arg_begin(), E->arg_end(), TargetDecl);
Chris Lattner9e47ead2007-08-31 04:44:06 +00001914}
1915
Daniel Dunbar8cde00a2008-09-04 03:20:13 +00001916LValue CodeGenFunction::EmitBinaryOperatorLValue(const BinaryOperator *E) {
Chris Lattnere541ea32009-05-12 21:28:12 +00001917 // Comma expressions just emit their LHS then their RHS as an l-value.
1918 if (E->getOpcode() == BinaryOperator::Comma) {
1919 EmitAnyExpr(E->getLHS());
Eli Friedman5445f6e2009-12-07 20:18:11 +00001920 EnsureInsertPoint();
Chris Lattnere541ea32009-05-12 21:28:12 +00001921 return EmitLValue(E->getRHS());
1922 }
Mike Stump4a3999f2009-09-09 13:00:44 +00001923
Fariborz Jahanian038374f2009-10-26 21:58:25 +00001924 if (E->getOpcode() == BinaryOperator::PtrMemD ||
1925 E->getOpcode() == BinaryOperator::PtrMemI)
Fariborz Jahanianffba6622009-10-22 22:57:31 +00001926 return EmitPointerToDataMemberBinaryExpr(E);
1927
Daniel Dunbar8cde00a2008-09-04 03:20:13 +00001928 // Can only get l-value for binary operator expressions which are a
1929 // simple assignment of aggregate type.
1930 if (E->getOpcode() != BinaryOperator::Assign)
1931 return EmitUnsupportedLValue(E, "binary l-value expression");
1932
Anders Carlsson0999aaf2009-10-19 18:28:22 +00001933 if (!hasAggregateLLVMType(E->getType())) {
1934 // Emit the LHS as an l-value.
1935 LValue LV = EmitLValue(E->getLHS());
1936
1937 llvm::Value *RHS = EmitScalarExpr(E->getRHS());
1938 EmitStoreOfScalar(RHS, LV.getAddress(), LV.isVolatileQualified(),
1939 E->getType());
1940 return LV;
1941 }
1942
Daniel Dunbard0bc7b92010-02-05 19:38:31 +00001943 return EmitAggExprToLValue(E);
Daniel Dunbar8cde00a2008-09-04 03:20:13 +00001944}
1945
Christopher Lambd91c3d42007-12-29 05:02:41 +00001946LValue CodeGenFunction::EmitCallExprLValue(const CallExpr *E) {
Christopher Lambd91c3d42007-12-29 05:02:41 +00001947 RValue RV = EmitCallExpr(E);
Anders Carlsson4ae70ff2009-05-27 01:45:47 +00001948
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001949 if (!RV.isScalar())
1950 return LValue::MakeAddr(RV.getAggregateAddr(),MakeQualifiers(E->getType()));
1951
1952 assert(E->getCallReturnType()->isReferenceType() &&
1953 "Can't have a scalar return unless the return type is a "
1954 "reference type!");
Mike Stump4a3999f2009-09-09 13:00:44 +00001955
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001956 return LValue::MakeAddr(RV.getScalarVal(), MakeQualifiers(E->getType()));
Christopher Lambd91c3d42007-12-29 05:02:41 +00001957}
1958
Daniel Dunbar8d9dc4a2009-02-11 20:59:32 +00001959LValue CodeGenFunction::EmitVAArgExprLValue(const VAArgExpr *E) {
1960 // FIXME: This shouldn't require another copy.
Daniel Dunbard0bc7b92010-02-05 19:38:31 +00001961 return EmitAggExprToLValue(E);
Daniel Dunbar8d9dc4a2009-02-11 20:59:32 +00001962}
1963
Anders Carlsson3be22e22009-05-30 23:23:33 +00001964LValue CodeGenFunction::EmitCXXConstructLValue(const CXXConstructExpr *E) {
Daniel Dunbara7566f12010-02-09 02:48:28 +00001965 llvm::Value *Temp = CreateMemTemp(E->getType(), "tmp");
Anders Carlsson3be22e22009-05-30 23:23:33 +00001966 EmitCXXConstructExpr(Temp, E);
John McCall8ccfcb52009-09-24 19:53:00 +00001967 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
Anders Carlsson3be22e22009-05-30 23:23:33 +00001968}
1969
Anders Carlssonfd2af0c2009-05-30 23:30:54 +00001970LValue
Mike Stumpc9b231c2009-11-15 08:09:41 +00001971CodeGenFunction::EmitCXXTypeidLValue(const CXXTypeidExpr *E) {
1972 llvm::Value *Temp = EmitCXXTypeidExpr(E);
1973 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
1974}
1975
1976LValue
Anders Carlssonfd2af0c2009-05-30 23:30:54 +00001977CodeGenFunction::EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr *E) {
1978 LValue LV = EmitLValue(E->getSubExpr());
Anders Carlsson8eb93e72009-05-31 00:34:10 +00001979 PushCXXTemporary(E->getTemporary(), LV.getAddress());
Anders Carlssonfd2af0c2009-05-30 23:30:54 +00001980 return LV;
1981}
1982
Daniel Dunbarc8317a42008-08-23 10:51:21 +00001983LValue CodeGenFunction::EmitObjCMessageExprLValue(const ObjCMessageExpr *E) {
Daniel Dunbarc8317a42008-08-23 10:51:21 +00001984 RValue RV = EmitObjCMessageExpr(E);
Anders Carlsson280e61f12010-06-21 20:59:55 +00001985
1986 if (!RV.isScalar())
1987 return LValue::MakeAddr(RV.getAggregateAddr(),
1988 MakeQualifiers(E->getType()));
1989
1990 assert(E->getMethodDecl()->getResultType()->isReferenceType() &&
1991 "Can't have a scalar return unless the return type is a "
1992 "reference type!");
1993
1994 return LValue::MakeAddr(RV.getScalarVal(), MakeQualifiers(E->getType()));
Daniel Dunbarc8317a42008-08-23 10:51:21 +00001995}
1996
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001997LValue CodeGenFunction::EmitObjCSelectorLValue(const ObjCSelectorExpr *E) {
1998 llvm::Value *V =
1999 CGM.getObjCRuntime().GetSelector(Builder, E->getSelector(), true);
2000 return LValue::MakeAddr(V, MakeQualifiers(E->getType()));
2001}
2002
Daniel Dunbar722f4242009-04-22 05:08:15 +00002003llvm::Value *CodeGenFunction::EmitIvarOffset(const ObjCInterfaceDecl *Interface,
Daniel Dunbar1c64e5d2008-09-24 04:00:38 +00002004 const ObjCIvarDecl *Ivar) {
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00002005 return CGM.getObjCRuntime().EmitIvarOffset(*this, Interface, Ivar);
Daniel Dunbar1c64e5d2008-09-24 04:00:38 +00002006}
2007
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00002008LValue CodeGenFunction::EmitLValueForIvar(QualType ObjectTy,
2009 llvm::Value *BaseValue,
Daniel Dunbar1c64e5d2008-09-24 04:00:38 +00002010 const ObjCIvarDecl *Ivar,
2011 unsigned CVRQualifiers) {
Chris Lattnerc4688d22009-04-17 17:44:48 +00002012 return CGM.getObjCRuntime().EmitObjCValueForIvar(*this, ObjectTy, BaseValue,
Daniel Dunbar9ebf9512009-04-21 01:19:28 +00002013 Ivar, CVRQualifiers);
Daniel Dunbar1c64e5d2008-09-24 04:00:38 +00002014}
2015
2016LValue CodeGenFunction::EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E) {
Anders Carlssonc13b85a2008-08-25 01:53:23 +00002017 // FIXME: A lot of the code below could be shared with EmitMemberExpr.
2018 llvm::Value *BaseValue = 0;
2019 const Expr *BaseExpr = E->getBase();
John McCall8ccfcb52009-09-24 19:53:00 +00002020 Qualifiers BaseQuals;
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00002021 QualType ObjectTy;
Anders Carlssonc13b85a2008-08-25 01:53:23 +00002022 if (E->isArrow()) {
2023 BaseValue = EmitScalarExpr(BaseExpr);
Steve Naroff7cae42b2009-07-10 23:34:53 +00002024 ObjectTy = BaseExpr->getType()->getPointeeType();
John McCall8ccfcb52009-09-24 19:53:00 +00002025 BaseQuals = ObjectTy.getQualifiers();
Anders Carlssonc13b85a2008-08-25 01:53:23 +00002026 } else {
2027 LValue BaseLV = EmitLValue(BaseExpr);
2028 // FIXME: this isn't right for bitfields.
2029 BaseValue = BaseLV.getAddress();
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00002030 ObjectTy = BaseExpr->getType();
John McCall8ccfcb52009-09-24 19:53:00 +00002031 BaseQuals = ObjectTy.getQualifiers();
Anders Carlssonc13b85a2008-08-25 01:53:23 +00002032 }
Daniel Dunbar1c64e5d2008-09-24 04:00:38 +00002033
Fariborz Jahaniande1d3242009-09-16 23:11:23 +00002034 LValue LV =
John McCall8ccfcb52009-09-24 19:53:00 +00002035 EmitLValueForIvar(ObjectTy, BaseValue, E->getDecl(),
2036 BaseQuals.getCVRQualifiers());
Fariborz Jahaniande1d3242009-09-16 23:11:23 +00002037 setObjCGCLValueClass(getContext(), E, LV);
2038 return LV;
Chris Lattner4bd55962008-03-30 23:03:07 +00002039}
2040
Mike Stump4a3999f2009-09-09 13:00:44 +00002041LValue
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +00002042CodeGenFunction::EmitObjCPropertyRefLValue(const ObjCPropertyRefExpr *E) {
Mike Stump4a3999f2009-09-09 13:00:44 +00002043 // This is a special l-value that just issues sends when we load or store
2044 // through it.
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +00002045 return LValue::MakePropertyRef(E, E->getType().getCVRQualifiers());
2046}
2047
Chris Lattnerab5e0af2009-10-28 17:39:19 +00002048LValue CodeGenFunction::EmitObjCKVCRefLValue(
Fariborz Jahanian9a846652009-08-20 17:02:02 +00002049 const ObjCImplicitSetterGetterRefExpr *E) {
Mike Stump4a3999f2009-09-09 13:00:44 +00002050 // This is a special l-value that just issues sends when we load or store
2051 // through it.
Fariborz Jahanian9ac53512008-11-22 22:30:21 +00002052 return LValue::MakeKVCRef(E, E->getType().getCVRQualifiers());
2053}
2054
Chris Lattnerab5e0af2009-10-28 17:39:19 +00002055LValue CodeGenFunction::EmitObjCSuperExprLValue(const ObjCSuperExpr *E) {
Douglas Gregor8ea1f532008-11-04 14:56:14 +00002056 return EmitUnsupportedLValue(E, "use of super");
2057}
2058
Chris Lattnera4185c52009-04-25 19:35:26 +00002059LValue CodeGenFunction::EmitStmtExprLValue(const StmtExpr *E) {
Chris Lattnera4185c52009-04-25 19:35:26 +00002060 // Can only get l-value for message expression returning aggregate type
2061 RValue RV = EmitAnyExprToTemp(E);
John McCall8ccfcb52009-09-24 19:53:00 +00002062 return LValue::MakeAddr(RV.getAggregateAddr(), MakeQualifiers(E->getType()));
Chris Lattnera4185c52009-04-25 19:35:26 +00002063}
2064
Anders Carlsson0435ed52009-12-24 19:08:58 +00002065RValue CodeGenFunction::EmitCall(QualType CalleeType, llvm::Value *Callee,
Anders Carlsson17490832009-12-24 20:40:36 +00002066 ReturnValueSlot ReturnValue,
Anders Carlsson3a9463b2009-05-27 01:22:39 +00002067 CallExpr::const_arg_iterator ArgBeg,
2068 CallExpr::const_arg_iterator ArgEnd,
2069 const Decl *TargetDecl) {
Mike Stump4a3999f2009-09-09 13:00:44 +00002070 // Get the actual function type. The callee type will always be a pointer to
2071 // function type or a block pointer type.
2072 assert(CalleeType->isFunctionPointerType() &&
Anders Carlssond8db8532009-04-07 18:53:02 +00002073 "Call must have function pointer type!");
2074
John McCall6fd4c232009-10-23 08:22:42 +00002075 CalleeType = getContext().getCanonicalType(CalleeType);
2076
John McCallab26cfa2010-02-05 21:31:56 +00002077 const FunctionType *FnType
2078 = cast<FunctionType>(cast<PointerType>(CalleeType)->getPointeeType());
2079 QualType ResultType = FnType->getResultType();
Daniel Dunbarc722b852008-08-30 03:02:31 +00002080
2081 CallArgList Args;
John McCall6fd4c232009-10-23 08:22:42 +00002082 EmitCallArgs(Args, dyn_cast<FunctionProtoType>(FnType), ArgBeg, ArgEnd);
Daniel Dunbarc722b852008-08-30 03:02:31 +00002083
John McCallab26cfa2010-02-05 21:31:56 +00002084 return EmitCall(CGM.getTypes().getFunctionInfo(Args, FnType),
Anders Carlsson17490832009-12-24 20:40:36 +00002085 Callee, ReturnValue, Args, TargetDecl);
Daniel Dunbar97db84c2008-08-23 03:46:30 +00002086}
Fariborz Jahanianffba6622009-10-22 22:57:31 +00002087
Chris Lattnerab5e0af2009-10-28 17:39:19 +00002088LValue CodeGenFunction::
2089EmitPointerToDataMemberBinaryExpr(const BinaryOperator *E) {
Eli Friedman928a5672009-11-18 05:01:17 +00002090 llvm::Value *BaseV;
Fariborz Jahanian038374f2009-10-26 21:58:25 +00002091 if (E->getOpcode() == BinaryOperator::PtrMemI)
Eli Friedman928a5672009-11-18 05:01:17 +00002092 BaseV = EmitScalarExpr(E->getLHS());
2093 else
2094 BaseV = EmitLValue(E->getLHS()).getAddress();
Fariborz Jahanianffba6622009-10-22 22:57:31 +00002095 const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(getLLVMContext());
2096 BaseV = Builder.CreateBitCast(BaseV, i8Ty);
Eli Friedman928a5672009-11-18 05:01:17 +00002097 llvm::Value *OffsetV = EmitScalarExpr(E->getRHS());
Fariborz Jahanianffba6622009-10-22 22:57:31 +00002098 llvm::Value *AddV = Builder.CreateInBoundsGEP(BaseV, OffsetV, "add.ptr");
Chris Lattnerab5e0af2009-10-28 17:39:19 +00002099
Fariborz Jahanianffba6622009-10-22 22:57:31 +00002100 QualType Ty = E->getRHS()->getType();
Chris Lattnerab5e0af2009-10-28 17:39:19 +00002101 Ty = Ty->getAs<MemberPointerType>()->getPointeeType();
2102
2103 const llvm::Type *PType = ConvertType(getContext().getPointerType(Ty));
Fariborz Jahanianffba6622009-10-22 22:57:31 +00002104 AddV = Builder.CreateBitCast(AddV, PType);
Chris Lattnerab5e0af2009-10-28 17:39:19 +00002105 return LValue::MakeAddr(AddV, MakeQualifiers(Ty));
Fariborz Jahanianffba6622009-10-22 22:57:31 +00002106}
2107