blob: 0aa4438f4f47d35cae29dc0ec351547fcf8868c3 [file] [log] [blame]
Chris Lattnere47e4402007-06-01 18:02:12 +00001//===--- CGExpr.cpp - Emit LLVM Code from Expressions ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnere47e4402007-06-01 18:02:12 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit Expr nodes as LLVM code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenFunction.h"
Chris Lattnerb6984c42007-06-20 04:44:43 +000015#include "CodeGenModule.h"
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000016#include "CGCall.h"
Daniel Dunbar034299e2010-03-31 01:09:11 +000017#include "CGRecordLayout.h"
Daniel Dunbar89da6ad2008-08-13 00:59:25 +000018#include "CGObjCRuntime.h"
Daniel Dunbarad319a72008-08-11 05:00:27 +000019#include "clang/AST/ASTContext.h"
Daniel Dunbar6e8aa532008-08-11 05:35:13 +000020#include "clang/AST/DeclObjC.h"
Mike Stumpe8c3b3e2009-12-15 00:35:12 +000021#include "llvm/Intrinsics.h"
Mike Stump9a4e0122009-12-15 00:59:40 +000022#include "clang/CodeGen/CodeGenOptions.h"
Eli Friedmanf2442dc2008-05-17 20:03:47 +000023#include "llvm/Target/TargetData.h"
Chris Lattnere47e4402007-06-01 18:02:12 +000024using namespace clang;
25using namespace CodeGen;
26
Chris Lattnerd7f58862007-06-02 05:24:33 +000027//===--------------------------------------------------------------------===//
Chris Lattnerf0106d22007-06-02 19:33:17 +000028// Miscellaneous Helper Methods
29//===--------------------------------------------------------------------===//
30
Chris Lattnere9a64532007-06-22 21:44:33 +000031/// CreateTempAlloca - This creates a alloca and inserts it into the entry
32/// block.
33llvm::AllocaInst *CodeGenFunction::CreateTempAlloca(const llvm::Type *Ty,
Daniel Dunbarb5aacc22009-10-19 01:21:05 +000034 const llvm::Twine &Name) {
Chris Lattner47640222009-03-22 00:24:14 +000035 if (!Builder.isNamePreserving())
Daniel Dunbarb5aacc22009-10-19 01:21:05 +000036 return new llvm::AllocaInst(Ty, 0, "", AllocaInsertPt);
Devang Pateldac79de2009-10-12 22:29:02 +000037 return new llvm::AllocaInst(Ty, 0, Name, AllocaInsertPt);
Chris Lattnere9a64532007-06-22 21:44:33 +000038}
Chris Lattner8394d792007-06-05 20:53:16 +000039
Daniel Dunbard0049182010-02-16 19:44:13 +000040llvm::Value *CodeGenFunction::CreateIRTemp(QualType Ty,
41 const llvm::Twine &Name) {
42 llvm::AllocaInst *Alloc = CreateTempAlloca(ConvertType(Ty), Name);
43 // FIXME: Should we prefer the preferred type alignment here?
44 CharUnits Align = getContext().getTypeAlignInChars(Ty);
45 Alloc->setAlignment(Align.getQuantity());
46 return Alloc;
47}
48
49llvm::Value *CodeGenFunction::CreateMemTemp(QualType Ty,
50 const llvm::Twine &Name) {
Daniel Dunbara7566f12010-02-09 02:48:28 +000051 llvm::AllocaInst *Alloc = CreateTempAlloca(ConvertTypeForMem(Ty), Name);
52 // FIXME: Should we prefer the preferred type alignment here?
53 CharUnits Align = getContext().getTypeAlignInChars(Ty);
54 Alloc->setAlignment(Align.getQuantity());
55 return Alloc;
56}
57
Chris Lattner8394d792007-06-05 20:53:16 +000058/// EvaluateExprAsBool - Perform the usual unary conversions on the specified
59/// expression and compare the result against zero, returning an Int1Ty value.
Chris Lattner23b7eb62007-06-15 23:05:46 +000060llvm::Value *CodeGenFunction::EvaluateExprAsBool(const Expr *E) {
Chris Lattner268fcce2007-08-26 16:46:58 +000061 QualType BoolTy = getContext().BoolTy;
Eli Friedman68396b12009-12-11 09:26:29 +000062 if (E->getType()->isMemberFunctionPointerType()) {
Daniel Dunbard0bc7b92010-02-05 19:38:31 +000063 LValue LV = EmitAggExprToLValue(E);
Eli Friedman68396b12009-12-11 09:26:29 +000064
65 // Get the pointer.
Daniel Dunbard0bc7b92010-02-05 19:38:31 +000066 llvm::Value *FuncPtr = Builder.CreateStructGEP(LV.getAddress(), 0,
67 "src.ptr");
Eli Friedman68396b12009-12-11 09:26:29 +000068 FuncPtr = Builder.CreateLoad(FuncPtr);
69
70 llvm::Value *IsNotNull =
71 Builder.CreateICmpNE(FuncPtr,
72 llvm::Constant::getNullValue(FuncPtr->getType()),
73 "tobool");
74
75 return IsNotNull;
76 }
Chris Lattnerf3bc75a2008-04-04 16:54:41 +000077 if (!E->getType()->isAnyComplexType())
Chris Lattner268fcce2007-08-26 16:46:58 +000078 return EmitScalarConversion(EmitScalarExpr(E), E->getType(), BoolTy);
Chris Lattner8394d792007-06-05 20:53:16 +000079
Chris Lattner268fcce2007-08-26 16:46:58 +000080 return EmitComplexToScalarConversion(EmitComplexExpr(E), E->getType(),BoolTy);
Chris Lattnerf0106d22007-06-02 19:33:17 +000081}
82
Chris Lattner4647a212007-08-31 22:49:20 +000083/// EmitAnyExpr - Emit code to compute the specified expression which can have
84/// any type. The result is returned as an RValue struct. If this is an
Mike Stump4a3999f2009-09-09 13:00:44 +000085/// aggregate expression, the aggloc/agglocvolatile arguments indicate where the
86/// result should be returned.
87RValue CodeGenFunction::EmitAnyExpr(const Expr *E, llvm::Value *AggLoc,
Anders Carlsson5b106a72009-08-16 07:36:22 +000088 bool IsAggLocVolatile, bool IgnoreResult,
89 bool IsInitializer) {
Chris Lattner4647a212007-08-31 22:49:20 +000090 if (!hasAggregateLLVMType(E->getType()))
Mike Stumpdf0fe272009-05-29 15:46:01 +000091 return RValue::get(EmitScalarExpr(E, IgnoreResult));
Chris Lattnerf3bc75a2008-04-04 16:54:41 +000092 else if (E->getType()->isAnyComplexType())
Mike Stumpdf0fe272009-05-29 15:46:01 +000093 return RValue::getComplex(EmitComplexExpr(E, false, false,
94 IgnoreResult, IgnoreResult));
Mike Stump4a3999f2009-09-09 13:00:44 +000095
Anders Carlsson5b106a72009-08-16 07:36:22 +000096 EmitAggExpr(E, AggLoc, IsAggLocVolatile, IgnoreResult, IsInitializer);
97 return RValue::getAggregate(AggLoc, IsAggLocVolatile);
Chris Lattner4647a212007-08-31 22:49:20 +000098}
99
Mike Stump4a3999f2009-09-09 13:00:44 +0000100/// EmitAnyExprToTemp - Similary to EmitAnyExpr(), however, the result will
101/// always be accessible even if no aggregate location is provided.
102RValue CodeGenFunction::EmitAnyExprToTemp(const Expr *E,
Anders Carlsson5b106a72009-08-16 07:36:22 +0000103 bool IsAggLocVolatile,
104 bool IsInitializer) {
105 llvm::Value *AggLoc = 0;
Mike Stump4a3999f2009-09-09 13:00:44 +0000106
107 if (hasAggregateLLVMType(E->getType()) &&
Daniel Dunbar41cf9de2008-09-09 01:06:48 +0000108 !E->getType()->isAnyComplexType())
John McCall7538eec2010-02-15 01:23:36 +0000109 AggLoc = CreateMemTemp(E->getType(), "agg.tmp");
Mike Stump4a3999f2009-09-09 13:00:44 +0000110 return EmitAnyExpr(E, AggLoc, IsAggLocVolatile, /*IgnoreResult=*/false,
Anders Carlsson5b106a72009-08-16 07:36:22 +0000111 IsInitializer);
Daniel Dunbar41cf9de2008-09-09 01:06:48 +0000112}
113
Anders Carlsson6f5a0152009-05-20 00:24:07 +0000114RValue CodeGenFunction::EmitReferenceBindingToExpr(const Expr* E,
Anders Carlsson5b106a72009-08-16 07:36:22 +0000115 bool IsInitializer) {
Anders Carlsson69c2c4b2009-10-18 23:09:21 +0000116 bool ShouldDestroyTemporaries = false;
117 unsigned OldNumLiveTemporaries = 0;
Eli Friedman357e8c92009-12-19 00:20:10 +0000118
119 if (const CXXDefaultArgExpr *DAE = dyn_cast<CXXDefaultArgExpr>(E))
120 E = DAE->getExpr();
121
Anders Carlsson66413c22009-10-15 00:51:46 +0000122 if (const CXXExprWithTemporaries *TE = dyn_cast<CXXExprWithTemporaries>(E)) {
Anders Carlsson6e997b22009-12-15 20:51:39 +0000123 ShouldDestroyTemporaries = true;
124
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000125 // Keep track of the current cleanup stack depth.
Anders Carlsson6e997b22009-12-15 20:51:39 +0000126 OldNumLiveTemporaries = LiveTemporaries.size();
Anders Carlsson66413c22009-10-15 00:51:46 +0000127
Anders Carlsson69c2c4b2009-10-18 23:09:21 +0000128 E = TE->getSubExpr();
Anders Carlsson66413c22009-10-15 00:51:46 +0000129 }
130
Eli Friedmanc21cb442009-05-20 02:31:19 +0000131 RValue Val;
132 if (E->isLvalue(getContext()) == Expr::LV_Valid) {
Anders Carlsson7d4c0832009-05-20 00:36:58 +0000133 // Emit the expr as an lvalue.
134 LValue LV = EmitLValue(E);
Anders Carlsson824e0612010-02-04 17:32:58 +0000135 if (LV.isSimple()) {
136 if (ShouldDestroyTemporaries) {
137 // Pop temporaries.
138 while (LiveTemporaries.size() > OldNumLiveTemporaries)
139 PopCXXTemporary();
140 }
141
Eli Friedmanc21cb442009-05-20 02:31:19 +0000142 return RValue::get(LV.getAddress());
Anders Carlsson824e0612010-02-04 17:32:58 +0000143 }
144
Eli Friedmanc21cb442009-05-20 02:31:19 +0000145 Val = EmitLoadOfLValue(LV, E->getType());
Anders Carlsson69c2c4b2009-10-18 23:09:21 +0000146
147 if (ShouldDestroyTemporaries) {
148 // Pop temporaries.
149 while (LiveTemporaries.size() > OldNumLiveTemporaries)
150 PopCXXTemporary();
151 }
Eli Friedmanc21cb442009-05-20 02:31:19 +0000152 } else {
Anders Carlsson66413c22009-10-15 00:51:46 +0000153 const CXXRecordDecl *BaseClassDecl = 0;
154 const CXXRecordDecl *DerivedClassDecl = 0;
155
156 if (const CastExpr *CE =
157 dyn_cast<CastExpr>(E->IgnoreParenNoopCasts(getContext()))) {
158 if (CE->getCastKind() == CastExpr::CK_DerivedToBase) {
159 E = CE->getSubExpr();
160
161 BaseClassDecl =
162 cast<CXXRecordDecl>(CE->getType()->getAs<RecordType>()->getDecl());
163 DerivedClassDecl =
164 cast<CXXRecordDecl>(E->getType()->getAs<RecordType>()->getDecl());
165 }
166 }
167
Anders Carlsson5b106a72009-08-16 07:36:22 +0000168 Val = EmitAnyExprToTemp(E, /*IsAggLocVolatile=*/false,
169 IsInitializer);
Mike Stump4a3999f2009-09-09 13:00:44 +0000170
Anders Carlsson69c2c4b2009-10-18 23:09:21 +0000171 if (ShouldDestroyTemporaries) {
172 // Pop temporaries.
173 while (LiveTemporaries.size() > OldNumLiveTemporaries)
174 PopCXXTemporary();
175 }
176
Anders Carlsson3b848942009-08-16 17:54:29 +0000177 if (IsInitializer) {
178 // We might have to destroy the temporary variable.
179 if (const RecordType *RT = E->getType()->getAs<RecordType>()) {
180 if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
181 if (!ClassDecl->hasTrivialDestructor()) {
Mike Stump4a3999f2009-09-09 13:00:44 +0000182 const CXXDestructorDecl *Dtor =
Anders Carlsson3b848942009-08-16 17:54:29 +0000183 ClassDecl->getDestructor(getContext());
Mike Stump4a3999f2009-09-09 13:00:44 +0000184
Mike Stumpaff69af2009-12-09 03:35:49 +0000185 {
Anders Carlsson0c6a7d82009-12-11 01:00:09 +0000186 DelayedCleanupBlock Scope(*this);
Mike Stumpaff69af2009-12-09 03:35:49 +0000187 EmitCXXDestructorCall(Dtor, Dtor_Complete,
188 Val.getAggregateAddr());
Anders Carlsson0c6a7d82009-12-11 01:00:09 +0000189
190 // Make sure to jump to the exit block.
191 EmitBranch(Scope.getCleanupExitBlock());
Mike Stumpaff69af2009-12-09 03:35:49 +0000192 }
193 if (Exceptions) {
194 EHCleanupBlock Cleanup(*this);
195 EmitCXXDestructorCall(Dtor, Dtor_Complete,
196 Val.getAggregateAddr());
197 }
Anders Carlsson3b848942009-08-16 17:54:29 +0000198 }
Anders Carlssonb80760b2009-08-16 17:50:25 +0000199 }
200 }
201 }
Anders Carlsson66413c22009-10-15 00:51:46 +0000202
203 // Check if need to perform the derived-to-base cast.
204 if (BaseClassDecl) {
205 llvm::Value *Derived = Val.getAggregateAddr();
Anders Carlsson66413c22009-10-15 00:51:46 +0000206 llvm::Value *Base =
Anders Carlsson8c793172009-11-23 17:57:54 +0000207 GetAddressOfBaseClass(Derived, DerivedClassDecl, BaseClassDecl,
208 /*NullCheckValue=*/false);
Anders Carlsson66413c22009-10-15 00:51:46 +0000209 return RValue::get(Base);
210 }
Anders Carlsson7d4c0832009-05-20 00:36:58 +0000211 }
Eli Friedmanc21cb442009-05-20 02:31:19 +0000212
213 if (Val.isAggregate()) {
214 Val = RValue::get(Val.getAggregateAddr());
215 } else {
Anders Carlsson02bb7f02009-05-20 01:35:03 +0000216 // Create a temporary variable that we can bind the reference to.
Daniel Dunbara7566f12010-02-09 02:48:28 +0000217 llvm::Value *Temp = CreateMemTemp(E->getType(), "reftmp");
Eli Friedmanc21cb442009-05-20 02:31:19 +0000218 if (Val.isScalar())
219 EmitStoreOfScalar(Val.getScalarVal(), Temp, false, E->getType());
220 else
221 StoreComplexToAddr(Val.getComplexVal(), Temp, false);
222 Val = RValue::get(Temp);
Anders Carlsson145eae52009-05-20 01:03:17 +0000223 }
Eli Friedmanc21cb442009-05-20 02:31:19 +0000224
225 return Val;
Anders Carlsson6f5a0152009-05-20 00:24:07 +0000226}
227
228
Mike Stump4a3999f2009-09-09 13:00:44 +0000229/// getAccessedFieldNo - Given an encoded value and a result number, return the
230/// input field number being accessed.
231unsigned CodeGenFunction::getAccessedFieldNo(unsigned Idx,
Dan Gohman75d69da2008-05-22 00:50:06 +0000232 const llvm::Constant *Elts) {
233 if (isa<llvm::ConstantAggregateZero>(Elts))
234 return 0;
Mike Stump4a3999f2009-09-09 13:00:44 +0000235
Dan Gohman75d69da2008-05-22 00:50:06 +0000236 return cast<llvm::ConstantInt>(Elts->getOperand(Idx))->getZExtValue();
237}
238
Mike Stump3f6f9fe2009-12-16 02:57:00 +0000239void CodeGenFunction::EmitCheck(llvm::Value *Address, unsigned Size) {
240 if (!CatchUndefined)
241 return;
242
243 const llvm::IntegerType *Size_tTy
244 = llvm::IntegerType::get(VMContext, LLVMPointerWidth);
245 Address = Builder.CreateBitCast(Address, PtrToInt8Ty);
246
247 const llvm::Type *ResType[] = {
248 Size_tTy
249 };
250 llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::objectsize, ResType, 1);
251 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
252 CGM.getTypes().ConvertType(CGM.getContext().IntTy));
253 // In time, people may want to control this and use a 1 here.
254 llvm::Value *Arg = llvm::ConstantInt::get(IntTy, 0);
255 llvm::Value *C = Builder.CreateCall2(F, Address, Arg);
256 llvm::BasicBlock *Cont = createBasicBlock();
257 llvm::BasicBlock *Check = createBasicBlock();
258 llvm::Value *NegativeOne = llvm::ConstantInt::get(Size_tTy, -1ULL);
259 Builder.CreateCondBr(Builder.CreateICmpEQ(C, NegativeOne), Cont, Check);
260
261 EmitBlock(Check);
262 Builder.CreateCondBr(Builder.CreateICmpUGE(C,
263 llvm::ConstantInt::get(Size_tTy, Size)),
264 Cont, getTrapBB());
265 EmitBlock(Cont);
266}
Chris Lattner4647a212007-08-31 22:49:20 +0000267
Chris Lattner116ce8f2010-01-09 21:40:03 +0000268
269llvm::Value *CodeGenFunction::
270EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV,
271 bool isInc, bool isPre) {
272 QualType ValTy = E->getSubExpr()->getType();
273 llvm::Value *InVal = EmitLoadOfLValue(LV, ValTy).getScalarVal();
274
275 int AmountVal = isInc ? 1 : -1;
276
277 if (ValTy->isPointerType() &&
278 ValTy->getAs<PointerType>()->isVariableArrayType()) {
279 // The amount of the addition/subtraction needs to account for the VLA size
280 ErrorUnsupported(E, "VLA pointer inc/dec");
281 }
282
283 llvm::Value *NextVal;
284 if (const llvm::PointerType *PT =
285 dyn_cast<llvm::PointerType>(InVal->getType())) {
286 llvm::Constant *Inc =
287 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), AmountVal);
288 if (!isa<llvm::FunctionType>(PT->getElementType())) {
289 QualType PTEE = ValTy->getPointeeType();
290 if (const ObjCInterfaceType *OIT =
291 dyn_cast<ObjCInterfaceType>(PTEE)) {
292 // Handle interface types, which are not represented with a concrete
293 // type.
294 int size = getContext().getTypeSize(OIT) / 8;
295 if (!isInc)
296 size = -size;
297 Inc = llvm::ConstantInt::get(Inc->getType(), size);
298 const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(VMContext);
299 InVal = Builder.CreateBitCast(InVal, i8Ty);
300 NextVal = Builder.CreateGEP(InVal, Inc, "add.ptr");
301 llvm::Value *lhs = LV.getAddress();
302 lhs = Builder.CreateBitCast(lhs, llvm::PointerType::getUnqual(i8Ty));
303 LV = LValue::MakeAddr(lhs, MakeQualifiers(ValTy));
304 } else
305 NextVal = Builder.CreateInBoundsGEP(InVal, Inc, "ptrincdec");
306 } else {
307 const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(VMContext);
308 NextVal = Builder.CreateBitCast(InVal, i8Ty, "tmp");
309 NextVal = Builder.CreateGEP(NextVal, Inc, "ptrincdec");
310 NextVal = Builder.CreateBitCast(NextVal, InVal->getType());
311 }
312 } else if (InVal->getType() == llvm::Type::getInt1Ty(VMContext) && isInc) {
313 // Bool++ is an interesting case, due to promotion rules, we get:
314 // Bool++ -> Bool = Bool+1 -> Bool = (int)Bool+1 ->
315 // Bool = ((int)Bool+1) != 0
316 // An interesting aspect of this is that increment is always true.
317 // Decrement does not have this property.
318 NextVal = llvm::ConstantInt::getTrue(VMContext);
319 } else if (isa<llvm::IntegerType>(InVal->getType())) {
320 NextVal = llvm::ConstantInt::get(InVal->getType(), AmountVal);
321
322 // Signed integer overflow is undefined behavior.
323 if (ValTy->isSignedIntegerType())
324 NextVal = Builder.CreateNSWAdd(InVal, NextVal, isInc ? "inc" : "dec");
325 else
326 NextVal = Builder.CreateAdd(InVal, NextVal, isInc ? "inc" : "dec");
327 } else {
328 // Add the inc/dec to the real part.
329 if (InVal->getType()->isFloatTy())
330 NextVal =
331 llvm::ConstantFP::get(VMContext,
332 llvm::APFloat(static_cast<float>(AmountVal)));
333 else if (InVal->getType()->isDoubleTy())
334 NextVal =
335 llvm::ConstantFP::get(VMContext,
336 llvm::APFloat(static_cast<double>(AmountVal)));
337 else {
338 llvm::APFloat F(static_cast<float>(AmountVal));
339 bool ignored;
340 F.convert(Target.getLongDoubleFormat(), llvm::APFloat::rmTowardZero,
341 &ignored);
342 NextVal = llvm::ConstantFP::get(VMContext, F);
343 }
344 NextVal = Builder.CreateFAdd(InVal, NextVal, isInc ? "inc" : "dec");
345 }
346
347 // Store the updated result through the lvalue.
Daniel Dunbardc406b82010-04-05 21:36:35 +0000348 if (LV.isBitField())
Chris Lattner116ce8f2010-01-09 21:40:03 +0000349 EmitStoreThroughBitfieldLValue(RValue::get(NextVal), LV, ValTy, &NextVal);
350 else
351 EmitStoreThroughLValue(RValue::get(NextVal), LV, ValTy);
352
353 // If this is a postinc, return the value read from memory, otherwise use the
354 // updated value.
355 return isPre ? NextVal : InVal;
356}
357
358
359CodeGenFunction::ComplexPairTy CodeGenFunction::
360EmitComplexPrePostIncDec(const UnaryOperator *E, LValue LV,
361 bool isInc, bool isPre) {
362 ComplexPairTy InVal = LoadComplexFromAddr(LV.getAddress(),
363 LV.isVolatileQualified());
364
365 llvm::Value *NextVal;
366 if (isa<llvm::IntegerType>(InVal.first->getType())) {
367 uint64_t AmountVal = isInc ? 1 : -1;
368 NextVal = llvm::ConstantInt::get(InVal.first->getType(), AmountVal, true);
369
370 // Add the inc/dec to the real part.
371 NextVal = Builder.CreateAdd(InVal.first, NextVal, isInc ? "inc" : "dec");
372 } else {
373 QualType ElemTy = E->getType()->getAs<ComplexType>()->getElementType();
374 llvm::APFloat FVal(getContext().getFloatTypeSemantics(ElemTy), 1);
375 if (!isInc)
376 FVal.changeSign();
377 NextVal = llvm::ConstantFP::get(getLLVMContext(), FVal);
378
379 // Add the inc/dec to the real part.
380 NextVal = Builder.CreateFAdd(InVal.first, NextVal, isInc ? "inc" : "dec");
381 }
382
383 ComplexPairTy IncVal(NextVal, InVal.second);
384
385 // Store the updated result through the lvalue.
386 StoreComplexToAddr(IncVal, LV.getAddress(), LV.isVolatileQualified());
387
388 // If this is a postinc, return the value read from memory, otherwise use the
389 // updated value.
390 return isPre ? IncVal : InVal;
391}
392
393
Chris Lattnera45c5af2007-06-02 19:47:04 +0000394//===----------------------------------------------------------------------===//
Chris Lattnerd7f58862007-06-02 05:24:33 +0000395// LValue Expression Emission
Chris Lattnera45c5af2007-06-02 19:47:04 +0000396//===----------------------------------------------------------------------===//
Chris Lattnerd7f58862007-06-02 05:24:33 +0000397
Daniel Dunbarc79407f2009-02-05 07:09:07 +0000398RValue CodeGenFunction::GetUndefRValue(QualType Ty) {
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000399 if (Ty->isVoidType())
Daniel Dunbarc79407f2009-02-05 07:09:07 +0000400 return RValue::get(0);
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000401
402 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
Daniel Dunbar8429dbc2009-01-09 20:09:28 +0000403 const llvm::Type *EltTy = ConvertType(CTy->getElementType());
Owen Anderson7ec07a52009-07-30 23:11:26 +0000404 llvm::Value *U = llvm::UndefValue::get(EltTy);
Daniel Dunbar8429dbc2009-01-09 20:09:28 +0000405 return RValue::getComplex(std::make_pair(U, U));
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000406 }
407
408 if (hasAggregateLLVMType(Ty)) {
Owen Anderson9793f0e2009-07-29 22:16:19 +0000409 const llvm::Type *LTy = llvm::PointerType::getUnqual(ConvertType(Ty));
Owen Anderson7ec07a52009-07-30 23:11:26 +0000410 return RValue::getAggregate(llvm::UndefValue::get(LTy));
Daniel Dunbar8429dbc2009-01-09 20:09:28 +0000411 }
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000412
413 return RValue::get(llvm::UndefValue::get(ConvertType(Ty)));
Daniel Dunbarbb197e42009-01-09 16:50:52 +0000414}
415
Daniel Dunbarc79407f2009-02-05 07:09:07 +0000416RValue CodeGenFunction::EmitUnsupportedRValue(const Expr *E,
417 const char *Name) {
418 ErrorUnsupported(E, Name);
419 return GetUndefRValue(E->getType());
420}
421
Daniel Dunbarf2e69882008-08-25 20:45:57 +0000422LValue CodeGenFunction::EmitUnsupportedLValue(const Expr *E,
423 const char *Name) {
424 ErrorUnsupported(E, Name);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000425 llvm::Type *Ty = llvm::PointerType::getUnqual(ConvertType(E->getType()));
Owen Anderson7ec07a52009-07-30 23:11:26 +0000426 return LValue::MakeAddr(llvm::UndefValue::get(Ty),
John McCall8ccfcb52009-09-24 19:53:00 +0000427 MakeQualifiers(E->getType()));
Daniel Dunbarf2e69882008-08-25 20:45:57 +0000428}
429
Mike Stump3f6f9fe2009-12-16 02:57:00 +0000430LValue CodeGenFunction::EmitCheckedLValue(const Expr *E) {
431 LValue LV = EmitLValue(E);
Daniel Dunbardc406b82010-04-05 21:36:35 +0000432 if (!isa<DeclRefExpr>(E) && !LV.isBitField() && LV.isSimple())
Mike Stump3f6f9fe2009-12-16 02:57:00 +0000433 EmitCheck(LV.getAddress(), getContext().getTypeSize(E->getType()) / 8);
434 return LV;
435}
436
Chris Lattner8394d792007-06-05 20:53:16 +0000437/// EmitLValue - Emit code to compute a designator that specifies the location
438/// of the expression.
439///
Mike Stump4a3999f2009-09-09 13:00:44 +0000440/// This can return one of two things: a simple address or a bitfield reference.
441/// In either case, the LLVM Value* in the LValue structure is guaranteed to be
442/// an LLVM pointer type.
Chris Lattner8394d792007-06-05 20:53:16 +0000443///
Mike Stump4a3999f2009-09-09 13:00:44 +0000444/// If this returns a bitfield reference, nothing about the pointee type of the
445/// LLVM value is known: For example, it may not be a pointer to an integer.
Chris Lattner8394d792007-06-05 20:53:16 +0000446///
Mike Stump4a3999f2009-09-09 13:00:44 +0000447/// If this returns a normal address, and if the lvalue's C type is fixed size,
448/// this method guarantees that the returned pointer type will point to an LLVM
449/// type of the same size of the lvalue's type. If the lvalue has a variable
450/// length type, this is not possible.
Chris Lattner8394d792007-06-05 20:53:16 +0000451///
Chris Lattnerd7f58862007-06-02 05:24:33 +0000452LValue CodeGenFunction::EmitLValue(const Expr *E) {
453 switch (E->getStmtClass()) {
Daniel Dunbarf2e69882008-08-25 20:45:57 +0000454 default: return EmitUnsupportedLValue(E, "l-value expression");
Chris Lattnerd7f58862007-06-02 05:24:33 +0000455
Fariborz Jahanian531c16f2009-12-09 23:35:29 +0000456 case Expr::ObjCIsaExprClass:
457 return EmitObjCIsaExpr(cast<ObjCIsaExpr>(E));
Mike Stump4a3999f2009-09-09 13:00:44 +0000458 case Expr::BinaryOperatorClass:
Daniel Dunbar8cde00a2008-09-04 03:20:13 +0000459 return EmitBinaryOperatorLValue(cast<BinaryOperator>(E));
Mike Stump4a3999f2009-09-09 13:00:44 +0000460 case Expr::CallExprClass:
Anders Carlssonc82555f2009-09-01 21:18:52 +0000461 case Expr::CXXMemberCallExprClass:
Douglas Gregor993603d2008-11-14 16:09:21 +0000462 case Expr::CXXOperatorCallExprClass:
463 return EmitCallExprLValue(cast<CallExpr>(E));
Daniel Dunbar8d9dc4a2009-02-11 20:59:32 +0000464 case Expr::VAArgExprClass:
465 return EmitVAArgExprLValue(cast<VAArgExpr>(E));
Mike Stump4a3999f2009-09-09 13:00:44 +0000466 case Expr::DeclRefExprClass:
Douglas Gregorc7acfdf2009-01-06 05:10:23 +0000467 return EmitDeclRefLValue(cast<DeclRefExpr>(E));
Chris Lattner946aa312007-06-05 03:59:43 +0000468 case Expr::ParenExprClass:return EmitLValue(cast<ParenExpr>(E)->getSubExpr());
Chris Lattner6307f192008-08-10 01:53:14 +0000469 case Expr::PredefinedExprClass:
470 return EmitPredefinedLValue(cast<PredefinedExpr>(E));
Chris Lattner4347e3692007-06-06 04:54:52 +0000471 case Expr::StringLiteralClass:
472 return EmitStringLiteralLValue(cast<StringLiteral>(E));
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +0000473 case Expr::ObjCEncodeExprClass:
474 return EmitObjCEncodeExprLValue(cast<ObjCEncodeExpr>(E));
Chris Lattner4bd55962008-03-30 23:03:07 +0000475
Mike Stump4a3999f2009-09-09 13:00:44 +0000476 case Expr::BlockDeclRefExprClass:
Mike Stump1db7d042009-02-28 09:07:16 +0000477 return EmitBlockDeclRefLValue(cast<BlockDeclRefExpr>(E));
478
Anders Carlsson3be22e22009-05-30 23:23:33 +0000479 case Expr::CXXTemporaryObjectExprClass:
480 case Expr::CXXConstructExprClass:
Anders Carlssonfd2af0c2009-05-30 23:30:54 +0000481 return EmitCXXConstructLValue(cast<CXXConstructExpr>(E));
482 case Expr::CXXBindTemporaryExprClass:
483 return EmitCXXBindTemporaryLValue(cast<CXXBindTemporaryExpr>(E));
Anders Carlsson96bad9a2009-09-14 01:10:45 +0000484 case Expr::CXXExprWithTemporariesClass:
485 return EmitCXXExprWithTemporariesLValue(cast<CXXExprWithTemporaries>(E));
Anders Carlsson52ce3bb2009-11-14 01:51:50 +0000486 case Expr::CXXZeroInitValueExprClass:
487 return EmitNullInitializationLValue(cast<CXXZeroInitValueExpr>(E));
488 case Expr::CXXDefaultArgExprClass:
489 return EmitLValue(cast<CXXDefaultArgExpr>(E)->getExpr());
Mike Stumpc9b231c2009-11-15 08:09:41 +0000490 case Expr::CXXTypeidExprClass:
491 return EmitCXXTypeidLValue(cast<CXXTypeidExpr>(E));
Anders Carlssonfd2af0c2009-05-30 23:30:54 +0000492
Daniel Dunbarc8317a42008-08-23 10:51:21 +0000493 case Expr::ObjCMessageExprClass:
494 return EmitObjCMessageExprLValue(cast<ObjCMessageExpr>(E));
Mike Stump4a3999f2009-09-09 13:00:44 +0000495 case Expr::ObjCIvarRefExprClass:
Chris Lattner4bd55962008-03-30 23:03:07 +0000496 return EmitObjCIvarRefLValue(cast<ObjCIvarRefExpr>(E));
Daniel Dunbarf2e69882008-08-25 20:45:57 +0000497 case Expr::ObjCPropertyRefExprClass:
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +0000498 return EmitObjCPropertyRefLValue(cast<ObjCPropertyRefExpr>(E));
Fariborz Jahanian9a846652009-08-20 17:02:02 +0000499 case Expr::ObjCImplicitSetterGetterRefExprClass:
500 return EmitObjCKVCRefLValue(cast<ObjCImplicitSetterGetterRefExpr>(E));
Douglas Gregor8ea1f532008-11-04 14:56:14 +0000501 case Expr::ObjCSuperExprClass:
Chris Lattnera4185c52009-04-25 19:35:26 +0000502 return EmitObjCSuperExprLValue(cast<ObjCSuperExpr>(E));
Douglas Gregor8ea1f532008-11-04 14:56:14 +0000503
Chris Lattnera4185c52009-04-25 19:35:26 +0000504 case Expr::StmtExprClass:
505 return EmitStmtExprLValue(cast<StmtExpr>(E));
Mike Stump4a3999f2009-09-09 13:00:44 +0000506 case Expr::UnaryOperatorClass:
Chris Lattner8394d792007-06-05 20:53:16 +0000507 return EmitUnaryOpLValue(cast<UnaryOperator>(E));
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000508 case Expr::ArraySubscriptExprClass:
509 return EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E));
Nate Begemance4d7fc2008-04-18 23:10:10 +0000510 case Expr::ExtVectorElementExprClass:
511 return EmitExtVectorElementExpr(cast<ExtVectorElementExpr>(E));
Mike Stump4a3999f2009-09-09 13:00:44 +0000512 case Expr::MemberExprClass:
Douglas Gregorc1905232009-08-26 22:36:53 +0000513 return EmitMemberExpr(cast<MemberExpr>(E));
Eli Friedman9fd8b682008-05-13 23:18:27 +0000514 case Expr::CompoundLiteralExprClass:
515 return EmitCompoundLiteralLValue(cast<CompoundLiteralExpr>(E));
Daniel Dunbarbf1fe8c2009-03-24 02:38:23 +0000516 case Expr::ConditionalOperatorClass:
Anders Carlsson1450adb2009-09-15 16:35:24 +0000517 return EmitConditionalOperatorLValue(cast<ConditionalOperator>(E));
Chris Lattner053441f2008-12-12 05:35:08 +0000518 case Expr::ChooseExprClass:
Eli Friedmane0a5b8b2009-03-04 05:52:32 +0000519 return EmitLValue(cast<ChooseExpr>(E)->getChosenSubExpr(getContext()));
Chris Lattner63d06ab2009-03-18 04:02:57 +0000520 case Expr::ImplicitCastExprClass:
521 case Expr::CStyleCastExprClass:
522 case Expr::CXXFunctionalCastExprClass:
523 case Expr::CXXStaticCastExprClass:
524 case Expr::CXXDynamicCastExprClass:
525 case Expr::CXXReinterpretCastExprClass:
526 case Expr::CXXConstCastExprClass:
Chris Lattner28bcf1a2009-03-18 18:28:57 +0000527 return EmitCastLValue(cast<CastExpr>(E));
Chris Lattnerd7f58862007-06-02 05:24:33 +0000528 }
529}
530
Daniel Dunbar1d425462009-02-10 00:57:50 +0000531llvm::Value *CodeGenFunction::EmitLoadOfScalar(llvm::Value *Addr, bool Volatile,
532 QualType Ty) {
Daniel Dunbarc76493a2009-11-29 21:23:36 +0000533 llvm::LoadInst *Load = Builder.CreateLoad(Addr, "tmp");
534 if (Volatile)
535 Load->setVolatile(true);
Daniel Dunbar1d425462009-02-10 00:57:50 +0000536
Anders Carlsson29a1be32009-05-19 19:36:19 +0000537 // Bool can have different representation in memory than in registers.
Daniel Dunbarc76493a2009-11-29 21:23:36 +0000538 llvm::Value *V = Load;
Daniel Dunbar1d425462009-02-10 00:57:50 +0000539 if (Ty->isBooleanType())
Owen Anderson41a75022009-08-13 21:57:51 +0000540 if (V->getType() != llvm::Type::getInt1Ty(VMContext))
541 V = Builder.CreateTrunc(V, llvm::Type::getInt1Ty(VMContext), "tobool");
Mike Stump4a3999f2009-09-09 13:00:44 +0000542
Daniel Dunbar1d425462009-02-10 00:57:50 +0000543 return V;
544}
545
546void CodeGenFunction::EmitStoreOfScalar(llvm::Value *Value, llvm::Value *Addr,
Anders Carlsson83709642009-05-19 18:50:41 +0000547 bool Volatile, QualType Ty) {
Mike Stump4a3999f2009-09-09 13:00:44 +0000548
Anders Carlsson29a1be32009-05-19 19:36:19 +0000549 if (Ty->isBooleanType()) {
550 // Bool can have different representation in memory than in registers.
Anders Carlsson29a1be32009-05-19 19:36:19 +0000551 const llvm::PointerType *DstPtr = cast<llvm::PointerType>(Addr->getType());
Eli Friedmanb2b120f2009-12-01 22:31:51 +0000552 Value = Builder.CreateIntCast(Value, DstPtr->getElementType(), false);
Daniel Dunbar1d425462009-02-10 00:57:50 +0000553 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000554 Builder.CreateStore(Value, Addr, Volatile);
Daniel Dunbar1d425462009-02-10 00:57:50 +0000555}
556
Mike Stump4a3999f2009-09-09 13:00:44 +0000557/// EmitLoadOfLValue - Given an expression that represents a value lvalue, this
558/// method emits the address of the lvalue, then loads the result as an rvalue,
559/// returning the rvalue.
Chris Lattner9369a562007-06-29 16:31:29 +0000560RValue CodeGenFunction::EmitLoadOfLValue(LValue LV, QualType ExprType) {
Fariborz Jahanian50a12702008-11-19 17:34:06 +0000561 if (LV.isObjCWeak()) {
Mike Stump4a3999f2009-09-09 13:00:44 +0000562 // load of a __weak object.
Fariborz Jahanianf5125d12008-11-18 21:45:40 +0000563 llvm::Value *AddrWeakObj = LV.getAddress();
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000564 return RValue::get(CGM.getObjCRuntime().EmitObjCWeakRead(*this,
565 AddrWeakObj));
Fariborz Jahanianf5125d12008-11-18 21:45:40 +0000566 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000567
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000568 if (LV.isSimple()) {
569 llvm::Value *Ptr = LV.getAddress();
Douglas Gregora6437802010-02-05 21:10:36 +0000570 const llvm::Type *EltTy =
571 cast<llvm::PointerType>(Ptr->getType())->getElementType();
Mike Stump4a3999f2009-09-09 13:00:44 +0000572
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000573 // Simple scalar l-value.
Daniel Dunbar3d33fab2010-02-08 22:53:07 +0000574 //
575 // FIXME: We shouldn't have to use isSingleValueType here.
Douglas Gregora6437802010-02-05 21:10:36 +0000576 if (EltTy->isSingleValueType())
Mike Stump4a3999f2009-09-09 13:00:44 +0000577 return RValue::get(EmitLoadOfScalar(Ptr, LV.isVolatileQualified(),
Daniel Dunbar1d425462009-02-10 00:57:50 +0000578 ExprType));
Mike Stump4a3999f2009-09-09 13:00:44 +0000579
Chris Lattner6278e6a2007-08-11 00:04:45 +0000580 assert(ExprType->isFunctionType() && "Unknown scalar value");
581 return RValue::get(Ptr);
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000582 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000583
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000584 if (LV.isVectorElt()) {
Eli Friedman327944b2008-06-13 23:01:12 +0000585 llvm::Value *Vec = Builder.CreateLoad(LV.getVectorAddr(),
586 LV.isVolatileQualified(), "tmp");
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000587 return RValue::get(Builder.CreateExtractElement(Vec, LV.getVectorIdx(),
588 "vecext"));
589 }
Chris Lattner73ab9b32007-08-03 00:16:29 +0000590
591 // If this is a reference to a subset of the elements of a vector, either
592 // shuffle the input or extract/insert them as appropriate.
Nate Begemance4d7fc2008-04-18 23:10:10 +0000593 if (LV.isExtVectorElt())
594 return EmitLoadOfExtVectorElementLValue(LV, ExprType);
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000595
Daniel Dunbardc406b82010-04-05 21:36:35 +0000596 if (LV.isBitField())
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000597 return EmitLoadOfBitfieldLValue(LV, ExprType);
598
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +0000599 if (LV.isPropertyRef())
600 return EmitLoadOfPropertyRefLValue(LV, ExprType);
601
Chris Lattner6c7ce102009-02-16 21:11:58 +0000602 assert(LV.isKVCRef() && "Unknown LValue type!");
603 return EmitLoadOfKVCRefLValue(LV, ExprType);
Chris Lattner8394d792007-06-05 20:53:16 +0000604}
605
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000606RValue CodeGenFunction::EmitLoadOfBitfieldLValue(LValue LV,
607 QualType ExprType) {
Daniel Dunbar196ea442010-04-06 01:07:44 +0000608 const CGBitFieldInfo &Info = LV.getBitFieldInfo();
609 unsigned StartBit = Info.Start;
610 unsigned BitfieldSize = Info.Size;
Daniel Dunbardc406b82010-04-05 21:36:35 +0000611 llvm::Value *Ptr = LV.getBitFieldAddr();
Daniel Dunbaread7c912008-08-06 05:08:45 +0000612
Mike Stump4a3999f2009-09-09 13:00:44 +0000613 const llvm::Type *EltTy =
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000614 cast<llvm::PointerType>(Ptr->getType())->getElementType();
Daniel Dunbaread7c912008-08-06 05:08:45 +0000615 unsigned EltTySize = CGM.getTargetData().getTypeSizeInBits(EltTy);
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000616
Mike Stump4a3999f2009-09-09 13:00:44 +0000617 // In some cases the bitfield may straddle two memory locations. Currently we
618 // load the entire bitfield, then do the magic to sign-extend it if
619 // necessary. This results in somewhat more code than necessary for the common
620 // case (one load), since two shifts accomplish both the masking and sign
621 // extension.
Daniel Dunbaread7c912008-08-06 05:08:45 +0000622 unsigned LowBits = std::min(BitfieldSize, EltTySize - StartBit);
623 llvm::Value *Val = Builder.CreateLoad(Ptr, LV.isVolatileQualified(), "tmp");
Mike Stump4a3999f2009-09-09 13:00:44 +0000624
Daniel Dunbaread7c912008-08-06 05:08:45 +0000625 // Shift to proper location.
Daniel Dunbarf7fb7502008-11-13 02:20:34 +0000626 if (StartBit)
Chris Lattner72ecc682009-12-06 17:22:42 +0000627 Val = Builder.CreateLShr(Val, StartBit, "bf.lo");
Mike Stump4a3999f2009-09-09 13:00:44 +0000628
Daniel Dunbaread7c912008-08-06 05:08:45 +0000629 // Mask off unused bits.
Mike Stump4a3999f2009-09-09 13:00:44 +0000630 llvm::Constant *LowMask = llvm::ConstantInt::get(VMContext,
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000631 llvm::APInt::getLowBitsSet(EltTySize, LowBits));
Daniel Dunbaread7c912008-08-06 05:08:45 +0000632 Val = Builder.CreateAnd(Val, LowMask, "bf.lo.cleared");
Mike Stump4a3999f2009-09-09 13:00:44 +0000633
Daniel Dunbaread7c912008-08-06 05:08:45 +0000634 // Fetch the high bits if necessary.
635 if (LowBits < BitfieldSize) {
636 unsigned HighBits = BitfieldSize - LowBits;
Owen Anderson41a75022009-08-13 21:57:51 +0000637 llvm::Value *HighPtr = Builder.CreateGEP(Ptr, llvm::ConstantInt::get(
Mike Stump4a3999f2009-09-09 13:00:44 +0000638 llvm::Type::getInt32Ty(VMContext), 1), "bf.ptr.hi");
639 llvm::Value *HighVal = Builder.CreateLoad(HighPtr,
Daniel Dunbaread7c912008-08-06 05:08:45 +0000640 LV.isVolatileQualified(),
641 "tmp");
Mike Stump4a3999f2009-09-09 13:00:44 +0000642
Daniel Dunbaread7c912008-08-06 05:08:45 +0000643 // Mask off unused bits.
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000644 llvm::Constant *HighMask = llvm::ConstantInt::get(VMContext,
645 llvm::APInt::getLowBitsSet(EltTySize, HighBits));
Daniel Dunbaread7c912008-08-06 05:08:45 +0000646 HighVal = Builder.CreateAnd(HighVal, HighMask, "bf.lo.cleared");
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000647
Daniel Dunbaread7c912008-08-06 05:08:45 +0000648 // Shift to proper location and or in to bitfield value.
Chris Lattner72ecc682009-12-06 17:22:42 +0000649 HighVal = Builder.CreateShl(HighVal, LowBits);
Daniel Dunbaread7c912008-08-06 05:08:45 +0000650 Val = Builder.CreateOr(Val, HighVal, "bf.val");
651 }
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000652
Daniel Dunbaread7c912008-08-06 05:08:45 +0000653 // Sign extend if necessary.
Daniel Dunbar196ea442010-04-06 01:07:44 +0000654 if (Info.IsSigned) {
Mike Stump4a3999f2009-09-09 13:00:44 +0000655 llvm::Value *ExtraBits = llvm::ConstantInt::get(EltTy,
Daniel Dunbaread7c912008-08-06 05:08:45 +0000656 EltTySize - BitfieldSize);
Mike Stump4a3999f2009-09-09 13:00:44 +0000657 Val = Builder.CreateAShr(Builder.CreateShl(Val, ExtraBits),
Daniel Dunbaread7c912008-08-06 05:08:45 +0000658 ExtraBits, "bf.val.sext");
659 }
Eli Friedmanf2442dc2008-05-17 20:03:47 +0000660
Mike Stump4a3999f2009-09-09 13:00:44 +0000661 // The bitfield type and the normal type differ when the storage sizes differ
662 // (currently just _Bool).
Daniel Dunbaread7c912008-08-06 05:08:45 +0000663 Val = Builder.CreateIntCast(Val, ConvertType(ExprType), false, "tmp");
Eli Friedmanf2442dc2008-05-17 20:03:47 +0000664
Daniel Dunbaread7c912008-08-06 05:08:45 +0000665 return RValue::get(Val);
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000666}
667
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +0000668RValue CodeGenFunction::EmitLoadOfPropertyRefLValue(LValue LV,
669 QualType ExprType) {
670 return EmitObjCPropertyGet(LV.getPropertyRefExpr());
671}
672
Fariborz Jahanian9ac53512008-11-22 22:30:21 +0000673RValue CodeGenFunction::EmitLoadOfKVCRefLValue(LValue LV,
674 QualType ExprType) {
675 return EmitObjCPropertyGet(LV.getKVCRefExpr());
676}
677
Nate Begemanb699c9b2009-01-18 06:42:49 +0000678// If this is a reference to a subset of the elements of a vector, create an
679// appropriate shufflevector.
Nate Begemance4d7fc2008-04-18 23:10:10 +0000680RValue CodeGenFunction::EmitLoadOfExtVectorElementLValue(LValue LV,
681 QualType ExprType) {
Eli Friedman327944b2008-06-13 23:01:12 +0000682 llvm::Value *Vec = Builder.CreateLoad(LV.getExtVectorAddr(),
683 LV.isVolatileQualified(), "tmp");
Mike Stump4a3999f2009-09-09 13:00:44 +0000684
Nate Begemanf322eab2008-05-09 06:41:27 +0000685 const llvm::Constant *Elts = LV.getExtVectorElts();
Mike Stump4a3999f2009-09-09 13:00:44 +0000686
687 // If the result of the expression is a non-vector type, we must be extracting
688 // a single element. Just codegen as an extractelement.
John McCall9dd450b2009-09-21 23:43:11 +0000689 const VectorType *ExprVT = ExprType->getAs<VectorType>();
Chris Lattner8eab8ff2007-08-10 17:10:08 +0000690 if (!ExprVT) {
Dan Gohman75d69da2008-05-22 00:50:06 +0000691 unsigned InIdx = getAccessedFieldNo(0, Elts);
Owen Anderson41a75022009-08-13 21:57:51 +0000692 llvm::Value *Elt = llvm::ConstantInt::get(
693 llvm::Type::getInt32Ty(VMContext), InIdx);
Chris Lattner40ff7012007-08-03 16:18:34 +0000694 return RValue::get(Builder.CreateExtractElement(Vec, Elt, "tmp"));
695 }
Nate Begemanb699c9b2009-01-18 06:42:49 +0000696
697 // Always use shuffle vector to try to retain the original program structure
Chris Lattner8eab8ff2007-08-10 17:10:08 +0000698 unsigned NumResultElts = ExprVT->getNumElements();
Mike Stump4a3999f2009-09-09 13:00:44 +0000699
Nate Begemanb699c9b2009-01-18 06:42:49 +0000700 llvm::SmallVector<llvm::Constant*, 4> Mask;
Chris Lattner40ff7012007-08-03 16:18:34 +0000701 for (unsigned i = 0; i != NumResultElts; ++i) {
Dan Gohman75d69da2008-05-22 00:50:06 +0000702 unsigned InIdx = getAccessedFieldNo(i, Elts);
Owen Anderson41a75022009-08-13 21:57:51 +0000703 Mask.push_back(llvm::ConstantInt::get(
704 llvm::Type::getInt32Ty(VMContext), InIdx));
Chris Lattner40ff7012007-08-03 16:18:34 +0000705 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000706
Owen Anderson3cc120a2009-07-28 21:22:35 +0000707 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
Nate Begemanb699c9b2009-01-18 06:42:49 +0000708 Vec = Builder.CreateShuffleVector(Vec,
Owen Anderson7ec07a52009-07-30 23:11:26 +0000709 llvm::UndefValue::get(Vec->getType()),
Nate Begemanb699c9b2009-01-18 06:42:49 +0000710 MaskV, "tmp");
711 return RValue::get(Vec);
Chris Lattner40ff7012007-08-03 16:18:34 +0000712}
713
714
Chris Lattner9369a562007-06-29 16:31:29 +0000715
Chris Lattner8394d792007-06-05 20:53:16 +0000716/// EmitStoreThroughLValue - Store the specified rvalue into the specified
717/// lvalue, where both are guaranteed to the have the same type, and that type
718/// is 'Ty'.
Mike Stump4a3999f2009-09-09 13:00:44 +0000719void CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst,
Chris Lattner8394d792007-06-05 20:53:16 +0000720 QualType Ty) {
Chris Lattner41d480e2007-08-03 16:28:33 +0000721 if (!Dst.isSimple()) {
722 if (Dst.isVectorElt()) {
723 // Read/modify/write the vector, inserting the new element.
Eli Friedman327944b2008-06-13 23:01:12 +0000724 llvm::Value *Vec = Builder.CreateLoad(Dst.getVectorAddr(),
725 Dst.isVolatileQualified(), "tmp");
Chris Lattner4647a212007-08-31 22:49:20 +0000726 Vec = Builder.CreateInsertElement(Vec, Src.getScalarVal(),
Chris Lattner41d480e2007-08-03 16:28:33 +0000727 Dst.getVectorIdx(), "vecins");
Eli Friedman327944b2008-06-13 23:01:12 +0000728 Builder.CreateStore(Vec, Dst.getVectorAddr(),Dst.isVolatileQualified());
Chris Lattner41d480e2007-08-03 16:28:33 +0000729 return;
730 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000731
Nate Begemance4d7fc2008-04-18 23:10:10 +0000732 // If this is an update of extended vector elements, insert them as
733 // appropriate.
734 if (Dst.isExtVectorElt())
735 return EmitStoreThroughExtVectorComponentLValue(Src, Dst, Ty);
Lauro Ramos Venancio09af71c2008-01-22 22:36:45 +0000736
Daniel Dunbardc406b82010-04-05 21:36:35 +0000737 if (Dst.isBitField())
Lauro Ramos Venancio09af71c2008-01-22 22:36:45 +0000738 return EmitStoreThroughBitfieldLValue(Src, Dst, Ty);
739
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +0000740 if (Dst.isPropertyRef())
741 return EmitStoreThroughPropertyRefLValue(Src, Dst, Ty);
742
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000743 assert(Dst.isKVCRef() && "Unknown LValue type");
744 return EmitStoreThroughKVCRefLValue(Src, Dst, Ty);
Chris Lattner41d480e2007-08-03 16:28:33 +0000745 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000746
Fariborz Jahanian10bec102009-02-21 00:30:43 +0000747 if (Dst.isObjCWeak() && !Dst.isNonGC()) {
Mike Stump4a3999f2009-09-09 13:00:44 +0000748 // load of a __weak object.
Fariborz Jahanian50a12702008-11-19 17:34:06 +0000749 llvm::Value *LvalueDst = Dst.getAddress();
750 llvm::Value *src = Src.getScalarVal();
Mike Stumpca5ae662009-04-14 00:57:29 +0000751 CGM.getObjCRuntime().EmitObjCWeakAssign(*this, src, LvalueDst);
Fariborz Jahanian50a12702008-11-19 17:34:06 +0000752 return;
753 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000754
Fariborz Jahanian10bec102009-02-21 00:30:43 +0000755 if (Dst.isObjCStrong() && !Dst.isNonGC()) {
Mike Stump4a3999f2009-09-09 13:00:44 +0000756 // load of a __strong object.
Fariborz Jahanian50a12702008-11-19 17:34:06 +0000757 llvm::Value *LvalueDst = Dst.getAddress();
758 llvm::Value *src = Src.getScalarVal();
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000759 if (Dst.isObjCIvar()) {
760 assert(Dst.getBaseIvarExp() && "BaseIvarExp is NULL");
761 const llvm::Type *ResultType = ConvertType(getContext().LongTy);
762 llvm::Value *RHS = EmitScalarExpr(Dst.getBaseIvarExp());
Fariborz Jahanian1f9ed582009-09-25 00:00:20 +0000763 llvm::Value *dst = RHS;
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000764 RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast");
765 llvm::Value *LHS =
766 Builder.CreatePtrToInt(LvalueDst, ResultType, "sub.ptr.lhs.cast");
767 llvm::Value *BytesBetween = Builder.CreateSub(LHS, RHS, "ivar.offset");
Fariborz Jahanian1f9ed582009-09-25 00:00:20 +0000768 CGM.getObjCRuntime().EmitObjCIvarAssign(*this, src, dst,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000769 BytesBetween);
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000770 } else if (Dst.isGlobalObjCRef())
Fariborz Jahanian32ff7ae2009-05-04 23:27:20 +0000771 CGM.getObjCRuntime().EmitObjCGlobalAssign(*this, src, LvalueDst);
772 else
773 CGM.getObjCRuntime().EmitObjCStrongCastAssign(*this, src, LvalueDst);
Fariborz Jahanian50a12702008-11-19 17:34:06 +0000774 return;
775 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000776
Chris Lattner6278e6a2007-08-11 00:04:45 +0000777 assert(Src.isScalar() && "Can't emit an agg store with this method");
Anders Carlsson83709642009-05-19 18:50:41 +0000778 EmitStoreOfScalar(Src.getScalarVal(), Dst.getAddress(),
779 Dst.isVolatileQualified(), Ty);
Chris Lattner8394d792007-06-05 20:53:16 +0000780}
781
Lauro Ramos Venancio09af71c2008-01-22 22:36:45 +0000782void CodeGenFunction::EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst,
Mike Stump4a3999f2009-09-09 13:00:44 +0000783 QualType Ty,
Daniel Dunbar9b1335e2008-11-19 09:36:46 +0000784 llvm::Value **Result) {
Daniel Dunbar196ea442010-04-06 01:07:44 +0000785 const CGBitFieldInfo &Info = Dst.getBitFieldInfo();
786 unsigned StartBit = Info.Start;
787 unsigned BitfieldSize = Info.Size;
Daniel Dunbardc406b82010-04-05 21:36:35 +0000788 llvm::Value *Ptr = Dst.getBitFieldAddr();
Lauro Ramos Venancio09af71c2008-01-22 22:36:45 +0000789
Mike Stump4a3999f2009-09-09 13:00:44 +0000790 const llvm::Type *EltTy =
Daniel Dunbaread7c912008-08-06 05:08:45 +0000791 cast<llvm::PointerType>(Ptr->getType())->getElementType();
792 unsigned EltTySize = CGM.getTargetData().getTypeSizeInBits(EltTy);
793
Mike Stump4a3999f2009-09-09 13:00:44 +0000794 // Get the new value, cast to the appropriate type and masked to exactly the
795 // size of the bit-field.
Daniel Dunbar9b1335e2008-11-19 09:36:46 +0000796 llvm::Value *SrcVal = Src.getScalarVal();
797 llvm::Value *NewVal = Builder.CreateIntCast(SrcVal, EltTy, false, "tmp");
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000798 llvm::Constant *Mask = llvm::ConstantInt::get(VMContext,
799 llvm::APInt::getLowBitsSet(EltTySize, BitfieldSize));
Daniel Dunbaread7c912008-08-06 05:08:45 +0000800 NewVal = Builder.CreateAnd(NewVal, Mask, "bf.value");
Lauro Ramos Venancio09af71c2008-01-22 22:36:45 +0000801
Daniel Dunbar9b1335e2008-11-19 09:36:46 +0000802 // Return the new value of the bit-field, if requested.
803 if (Result) {
804 // Cast back to the proper type for result.
805 const llvm::Type *SrcTy = SrcVal->getType();
806 llvm::Value *SrcTrunc = Builder.CreateIntCast(NewVal, SrcTy, false,
807 "bf.reload.val");
808
809 // Sign extend if necessary.
Daniel Dunbar196ea442010-04-06 01:07:44 +0000810 if (Info.IsSigned) {
Daniel Dunbar9b1335e2008-11-19 09:36:46 +0000811 unsigned SrcTySize = CGM.getTargetData().getTypeSizeInBits(SrcTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000812 llvm::Value *ExtraBits = llvm::ConstantInt::get(SrcTy,
Daniel Dunbar9b1335e2008-11-19 09:36:46 +0000813 SrcTySize - BitfieldSize);
Mike Stump4a3999f2009-09-09 13:00:44 +0000814 SrcTrunc = Builder.CreateAShr(Builder.CreateShl(SrcTrunc, ExtraBits),
Daniel Dunbar9b1335e2008-11-19 09:36:46 +0000815 ExtraBits, "bf.reload.sext");
816 }
817
818 *Result = SrcTrunc;
819 }
820
Mike Stump4a3999f2009-09-09 13:00:44 +0000821 // In some cases the bitfield may straddle two memory locations. Emit the low
822 // part first and check to see if the high needs to be done.
Daniel Dunbaread7c912008-08-06 05:08:45 +0000823 unsigned LowBits = std::min(BitfieldSize, EltTySize - StartBit);
824 llvm::Value *LowVal = Builder.CreateLoad(Ptr, Dst.isVolatileQualified(),
825 "bf.prev.low");
Eli Friedmanf2442dc2008-05-17 20:03:47 +0000826
Daniel Dunbaread7c912008-08-06 05:08:45 +0000827 // Compute the mask for zero-ing the low part of this bitfield.
Mike Stump4a3999f2009-09-09 13:00:44 +0000828 llvm::Constant *InvMask =
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000829 llvm::ConstantInt::get(VMContext,
830 ~llvm::APInt::getBitsSet(EltTySize, StartBit, StartBit + LowBits));
Mike Stump4a3999f2009-09-09 13:00:44 +0000831
Daniel Dunbaread7c912008-08-06 05:08:45 +0000832 // Compute the new low part as
833 // LowVal = (LowVal & InvMask) | (NewVal << StartBit),
834 // with the shift of NewVal implicitly stripping the high bits.
Mike Stump4a3999f2009-09-09 13:00:44 +0000835 llvm::Value *NewLowVal =
Chris Lattner72ecc682009-12-06 17:22:42 +0000836 Builder.CreateShl(NewVal, StartBit, "bf.value.lo");
Daniel Dunbaread7c912008-08-06 05:08:45 +0000837 LowVal = Builder.CreateAnd(LowVal, InvMask, "bf.prev.lo.cleared");
838 LowVal = Builder.CreateOr(LowVal, NewLowVal, "bf.new.lo");
Mike Stump4a3999f2009-09-09 13:00:44 +0000839
Daniel Dunbaread7c912008-08-06 05:08:45 +0000840 // Write back.
841 Builder.CreateStore(LowVal, Ptr, Dst.isVolatileQualified());
Eli Friedmanf2442dc2008-05-17 20:03:47 +0000842
Daniel Dunbaread7c912008-08-06 05:08:45 +0000843 // If the low part doesn't cover the bitfield emit a high part.
844 if (LowBits < BitfieldSize) {
845 unsigned HighBits = BitfieldSize - LowBits;
Owen Anderson41a75022009-08-13 21:57:51 +0000846 llvm::Value *HighPtr = Builder.CreateGEP(Ptr, llvm::ConstantInt::get(
Mike Stump4a3999f2009-09-09 13:00:44 +0000847 llvm::Type::getInt32Ty(VMContext), 1), "bf.ptr.hi");
848 llvm::Value *HighVal = Builder.CreateLoad(HighPtr,
Daniel Dunbaread7c912008-08-06 05:08:45 +0000849 Dst.isVolatileQualified(),
850 "bf.prev.hi");
Mike Stump4a3999f2009-09-09 13:00:44 +0000851
Daniel Dunbaread7c912008-08-06 05:08:45 +0000852 // Compute the mask for zero-ing the high part of this bitfield.
Mike Stump4a3999f2009-09-09 13:00:44 +0000853 llvm::Constant *InvMask =
854 llvm::ConstantInt::get(VMContext, ~llvm::APInt::getLowBitsSet(EltTySize,
Owen Anderson170229f2009-07-14 23:10:40 +0000855 HighBits));
Mike Stump4a3999f2009-09-09 13:00:44 +0000856
Daniel Dunbaread7c912008-08-06 05:08:45 +0000857 // Compute the new high part as
858 // HighVal = (HighVal & InvMask) | (NewVal lshr LowBits),
859 // where the high bits of NewVal have already been cleared and the
860 // shift stripping the low bits.
Mike Stump4a3999f2009-09-09 13:00:44 +0000861 llvm::Value *NewHighVal =
Chris Lattner72ecc682009-12-06 17:22:42 +0000862 Builder.CreateLShr(NewVal, LowBits, "bf.value.high");
Daniel Dunbaread7c912008-08-06 05:08:45 +0000863 HighVal = Builder.CreateAnd(HighVal, InvMask, "bf.prev.hi.cleared");
864 HighVal = Builder.CreateOr(HighVal, NewHighVal, "bf.new.hi");
Mike Stump4a3999f2009-09-09 13:00:44 +0000865
Daniel Dunbaread7c912008-08-06 05:08:45 +0000866 // Write back.
867 Builder.CreateStore(HighVal, HighPtr, Dst.isVolatileQualified());
868 }
Lauro Ramos Venancio09af71c2008-01-22 22:36:45 +0000869}
870
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +0000871void CodeGenFunction::EmitStoreThroughPropertyRefLValue(RValue Src,
872 LValue Dst,
873 QualType Ty) {
874 EmitObjCPropertySet(Dst.getPropertyRefExpr(), Src);
875}
876
Fariborz Jahanian9ac53512008-11-22 22:30:21 +0000877void CodeGenFunction::EmitStoreThroughKVCRefLValue(RValue Src,
878 LValue Dst,
879 QualType Ty) {
880 EmitObjCPropertySet(Dst.getKVCRefExpr(), Src);
881}
882
Nate Begemance4d7fc2008-04-18 23:10:10 +0000883void CodeGenFunction::EmitStoreThroughExtVectorComponentLValue(RValue Src,
884 LValue Dst,
885 QualType Ty) {
Chris Lattner41d480e2007-08-03 16:28:33 +0000886 // This access turns into a read/modify/write of the vector. Load the input
887 // value now.
Eli Friedman327944b2008-06-13 23:01:12 +0000888 llvm::Value *Vec = Builder.CreateLoad(Dst.getExtVectorAddr(),
889 Dst.isVolatileQualified(), "tmp");
Nate Begemanf322eab2008-05-09 06:41:27 +0000890 const llvm::Constant *Elts = Dst.getExtVectorElts();
Mike Stump4a3999f2009-09-09 13:00:44 +0000891
Chris Lattner4647a212007-08-31 22:49:20 +0000892 llvm::Value *SrcVal = Src.getScalarVal();
Mike Stump4a3999f2009-09-09 13:00:44 +0000893
John McCall9dd450b2009-09-21 23:43:11 +0000894 if (const VectorType *VTy = Ty->getAs<VectorType>()) {
Chris Lattner3a44aa72007-08-03 16:37:04 +0000895 unsigned NumSrcElts = VTy->getNumElements();
Nate Begemanb699c9b2009-01-18 06:42:49 +0000896 unsigned NumDstElts =
897 cast<llvm::VectorType>(Vec->getType())->getNumElements();
898 if (NumDstElts == NumSrcElts) {
Mike Stump4a3999f2009-09-09 13:00:44 +0000899 // Use shuffle vector is the src and destination are the same number of
900 // elements and restore the vector mask since it is on the side it will be
901 // stored.
Nate Begemanea12f6e2009-06-26 21:12:50 +0000902 llvm::SmallVector<llvm::Constant*, 4> Mask(NumDstElts);
Nate Begemanb699c9b2009-01-18 06:42:49 +0000903 for (unsigned i = 0; i != NumSrcElts; ++i) {
904 unsigned InIdx = getAccessedFieldNo(i, Elts);
Owen Anderson41a75022009-08-13 21:57:51 +0000905 Mask[InIdx] = llvm::ConstantInt::get(
906 llvm::Type::getInt32Ty(VMContext), i);
Nate Begemanb699c9b2009-01-18 06:42:49 +0000907 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000908
Owen Anderson3cc120a2009-07-28 21:22:35 +0000909 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
Nate Begemanb699c9b2009-01-18 06:42:49 +0000910 Vec = Builder.CreateShuffleVector(SrcVal,
Owen Anderson7ec07a52009-07-30 23:11:26 +0000911 llvm::UndefValue::get(Vec->getType()),
Nate Begemanb699c9b2009-01-18 06:42:49 +0000912 MaskV, "tmp");
Mike Stump658fe022009-07-30 22:28:39 +0000913 } else if (NumDstElts > NumSrcElts) {
Nate Begemanb699c9b2009-01-18 06:42:49 +0000914 // Extended the source vector to the same length and then shuffle it
915 // into the destination.
916 // FIXME: since we're shuffling with undef, can we just use the indices
917 // into that? This could be simpler.
918 llvm::SmallVector<llvm::Constant*, 4> ExtMask;
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000919 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
Nate Begemanb699c9b2009-01-18 06:42:49 +0000920 unsigned i;
921 for (i = 0; i != NumSrcElts; ++i)
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000922 ExtMask.push_back(llvm::ConstantInt::get(Int32Ty, i));
Nate Begemanb699c9b2009-01-18 06:42:49 +0000923 for (; i != NumDstElts; ++i)
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000924 ExtMask.push_back(llvm::UndefValue::get(Int32Ty));
Owen Anderson3cc120a2009-07-28 21:22:35 +0000925 llvm::Value *ExtMaskV = llvm::ConstantVector::get(&ExtMask[0],
Nate Begemanb699c9b2009-01-18 06:42:49 +0000926 ExtMask.size());
Mike Stump4a3999f2009-09-09 13:00:44 +0000927 llvm::Value *ExtSrcVal =
Daniel Dunbar3d926cb2009-02-17 18:31:04 +0000928 Builder.CreateShuffleVector(SrcVal,
Owen Anderson7ec07a52009-07-30 23:11:26 +0000929 llvm::UndefValue::get(SrcVal->getType()),
Daniel Dunbar3d926cb2009-02-17 18:31:04 +0000930 ExtMaskV, "tmp");
Nate Begemanb699c9b2009-01-18 06:42:49 +0000931 // build identity
932 llvm::SmallVector<llvm::Constant*, 4> Mask;
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000933 for (unsigned i = 0; i != NumDstElts; ++i)
934 Mask.push_back(llvm::ConstantInt::get(Int32Ty, i));
935
Nate Begemanb699c9b2009-01-18 06:42:49 +0000936 // modify when what gets shuffled in
937 for (unsigned i = 0; i != NumSrcElts; ++i) {
938 unsigned Idx = getAccessedFieldNo(i, Elts);
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000939 Mask[Idx] = llvm::ConstantInt::get(Int32Ty, i+NumDstElts);
Nate Begemanb699c9b2009-01-18 06:42:49 +0000940 }
Owen Anderson3cc120a2009-07-28 21:22:35 +0000941 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
Nate Begemanb699c9b2009-01-18 06:42:49 +0000942 Vec = Builder.CreateShuffleVector(Vec, ExtSrcVal, MaskV, "tmp");
Mike Stump658fe022009-07-30 22:28:39 +0000943 } else {
Nate Begemanb699c9b2009-01-18 06:42:49 +0000944 // We should never shorten the vector
945 assert(0 && "unexpected shorten vector length");
Chris Lattner3a44aa72007-08-03 16:37:04 +0000946 }
947 } else {
948 // If the Src is a scalar (not a vector) it must be updating one element.
Dan Gohman75d69da2008-05-22 00:50:06 +0000949 unsigned InIdx = getAccessedFieldNo(0, Elts);
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000950 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
951 llvm::Value *Elt = llvm::ConstantInt::get(Int32Ty, InIdx);
Chris Lattner41d480e2007-08-03 16:28:33 +0000952 Vec = Builder.CreateInsertElement(Vec, SrcVal, Elt, "tmp");
Chris Lattner41d480e2007-08-03 16:28:33 +0000953 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000954
Eli Friedman327944b2008-06-13 23:01:12 +0000955 Builder.CreateStore(Vec, Dst.getExtVectorAddr(), Dst.isVolatileQualified());
Chris Lattner41d480e2007-08-03 16:28:33 +0000956}
957
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +0000958// setObjCGCLValueClass - sets class of he lvalue for the purpose of
959// generating write-barries API. It is currently a global, ivar,
960// or neither.
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000961static void setObjCGCLValueClass(const ASTContext &Ctx, const Expr *E,
962 LValue &LV) {
Fariborz Jahanian71848a32009-09-21 23:03:37 +0000963 if (Ctx.getLangOptions().getGCMode() == LangOptions::NonGC)
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +0000964 return;
965
Fariborz Jahaniande1d3242009-09-16 23:11:23 +0000966 if (isa<ObjCIvarRefExpr>(E)) {
967 LV.SetObjCIvar(LV, true);
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000968 ObjCIvarRefExpr *Exp = cast<ObjCIvarRefExpr>(const_cast<Expr*>(E));
969 LV.setBaseIvarExp(Exp->getBase());
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +0000970 LV.SetObjCArray(LV, E->getType()->isArrayType());
Fariborz Jahaniande1d3242009-09-16 23:11:23 +0000971 return;
972 }
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000973
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +0000974 if (const DeclRefExpr *Exp = dyn_cast<DeclRefExpr>(E)) {
975 if (const VarDecl *VD = dyn_cast<VarDecl>(Exp->getDecl())) {
976 if ((VD->isBlockVarDecl() && !VD->hasLocalStorage()) ||
977 VD->isFileVarDecl())
978 LV.SetGlobalObjCRef(LV, true);
979 }
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +0000980 LV.SetObjCArray(LV, E->getType()->isArrayType());
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000981 return;
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +0000982 }
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000983
984 if (const UnaryOperator *Exp = dyn_cast<UnaryOperator>(E)) {
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +0000985 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000986 return;
987 }
988
989 if (const ParenExpr *Exp = dyn_cast<ParenExpr>(E)) {
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +0000990 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
Fariborz Jahaniane01e4342009-09-30 17:10:29 +0000991 if (LV.isObjCIvar()) {
992 // If cast is to a structure pointer, follow gcc's behavior and make it
993 // a non-ivar write-barrier.
994 QualType ExpTy = E->getType();
995 if (ExpTy->isPointerType())
996 ExpTy = ExpTy->getAs<PointerType>()->getPointeeType();
997 if (ExpTy->isRecordType())
998 LV.SetObjCIvar(LV, false);
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000999 }
1000 return;
Fariborz Jahaniane01e4342009-09-30 17:10:29 +00001001 }
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001002 if (const ImplicitCastExpr *Exp = dyn_cast<ImplicitCastExpr>(E)) {
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +00001003 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001004 return;
1005 }
1006
1007 if (const CStyleCastExpr *Exp = dyn_cast<CStyleCastExpr>(E)) {
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +00001008 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001009 return;
1010 }
1011
1012 if (const ArraySubscriptExpr *Exp = dyn_cast<ArraySubscriptExpr>(E)) {
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +00001013 setObjCGCLValueClass(Ctx, Exp->getBase(), LV);
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +00001014 if (LV.isObjCIvar() && !LV.isObjCArray())
Fariborz Jahanian2e32ddc2009-09-18 00:04:00 +00001015 // Using array syntax to assigning to what an ivar points to is not
1016 // same as assigning to the ivar itself. {id *Names;} Names[i] = 0;
1017 LV.SetObjCIvar(LV, false);
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +00001018 else if (LV.isGlobalObjCRef() && !LV.isObjCArray())
1019 // Using array syntax to assigning to what global points to is not
1020 // same as assigning to the global itself. {id *G;} G[i] = 0;
1021 LV.SetGlobalObjCRef(LV, false);
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001022 return;
Fariborz Jahanian2e32ddc2009-09-18 00:04:00 +00001023 }
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001024
1025 if (const MemberExpr *Exp = dyn_cast<MemberExpr>(E)) {
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +00001026 setObjCGCLValueClass(Ctx, Exp->getBase(), LV);
Fariborz Jahanian2e32ddc2009-09-18 00:04:00 +00001027 // We don't know if member is an 'ivar', but this flag is looked at
1028 // only in the context of LV.isObjCIvar().
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +00001029 LV.SetObjCArray(LV, E->getType()->isArrayType());
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001030 return;
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +00001031 }
1032}
1033
Anders Carlssonea4c30b2009-11-07 23:06:58 +00001034static LValue EmitGlobalVarDeclLValue(CodeGenFunction &CGF,
1035 const Expr *E, const VarDecl *VD) {
Daniel Dunbar7e215ea2009-11-08 09:46:46 +00001036 assert((VD->hasExternalStorage() || VD->isFileVarDecl()) &&
Anders Carlssonea4c30b2009-11-07 23:06:58 +00001037 "Var decl must have external storage or be a file var decl!");
1038
1039 llvm::Value *V = CGF.CGM.GetAddrOfGlobalVar(VD);
1040 if (VD->getType()->isReferenceType())
1041 V = CGF.Builder.CreateLoad(V, "tmp");
1042 LValue LV = LValue::MakeAddr(V, CGF.MakeQualifiers(E->getType()));
1043 setObjCGCLValueClass(CGF.getContext(), E, LV);
1044 return LV;
1045}
1046
Eli Friedmand15eb34d2009-11-26 06:08:14 +00001047static LValue EmitFunctionDeclLValue(CodeGenFunction &CGF,
1048 const Expr *E, const FunctionDecl *FD) {
1049 llvm::Value* V = CGF.CGM.GetAddrOfFunction(FD);
1050 if (!FD->hasPrototype()) {
1051 if (const FunctionProtoType *Proto =
1052 FD->getType()->getAs<FunctionProtoType>()) {
1053 // Ugly case: for a K&R-style definition, the type of the definition
1054 // isn't the same as the type of a use. Correct for this with a
1055 // bitcast.
1056 QualType NoProtoType =
1057 CGF.getContext().getFunctionNoProtoType(Proto->getResultType());
1058 NoProtoType = CGF.getContext().getPointerType(NoProtoType);
1059 V = CGF.Builder.CreateBitCast(V, CGF.ConvertType(NoProtoType), "tmp");
1060 }
1061 }
1062 return LValue::MakeAddr(V, CGF.MakeQualifiers(E->getType()));
1063}
1064
Chris Lattnerd7f58862007-06-02 05:24:33 +00001065LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) {
Anders Carlsson2ff6395d2009-11-07 22:53:10 +00001066 const NamedDecl *ND = E->getDecl();
Mike Stump4a3999f2009-09-09 13:00:44 +00001067
Rafael Espindola2e42fec2010-03-04 18:17:24 +00001068 if (ND->hasAttr<WeakRefAttr>()) {
1069 const ValueDecl* VD = cast<ValueDecl>(ND);
1070 llvm::Constant *Aliasee = CGM.GetWeakRefReference(VD);
1071
1072 Qualifiers Quals = MakeQualifiers(E->getType());
1073 LValue LV = LValue::MakeAddr(Aliasee, Quals);
1074
1075 return LV;
1076 }
1077
Anders Carlsson2ff6395d2009-11-07 22:53:10 +00001078 if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
Anders Carlsson2ff6395d2009-11-07 22:53:10 +00001079
1080 // Check if this is a global variable.
Anders Carlssonea4c30b2009-11-07 23:06:58 +00001081 if (VD->hasExternalStorage() || VD->isFileVarDecl())
1082 return EmitGlobalVarDeclLValue(*this, E, VD);
Anders Carlsson6eee9722009-11-07 22:46:42 +00001083
1084 bool NonGCable = VD->hasLocalStorage() && !VD->hasAttr<BlocksAttr>();
1085
1086 llvm::Value *V = LocalDeclMap[VD];
1087 assert(V && "DeclRefExpr not entered in LocalDeclMap?");
1088
1089 Qualifiers Quals = MakeQualifiers(E->getType());
1090 // local variables do not get their gc attribute set.
1091 // local static?
1092 if (NonGCable) Quals.removeObjCGCAttr();
1093
1094 if (VD->hasAttr<BlocksAttr>()) {
1095 V = Builder.CreateStructGEP(V, 1, "forwarding");
Daniel Dunbarc76493a2009-11-29 21:23:36 +00001096 V = Builder.CreateLoad(V);
Anders Carlsson6eee9722009-11-07 22:46:42 +00001097 V = Builder.CreateStructGEP(V, getByRefValueLLVMField(VD),
1098 VD->getNameAsString());
1099 }
1100 if (VD->getType()->isReferenceType())
1101 V = Builder.CreateLoad(V, "tmp");
Anders Carlssonea4c30b2009-11-07 23:06:58 +00001102 LValue LV = LValue::MakeAddr(V, Quals);
Anders Carlsson6eee9722009-11-07 22:46:42 +00001103 LValue::SetObjCNonGC(LV, NonGCable);
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +00001104 setObjCGCLValueClass(getContext(), E, LV);
Fariborz Jahanian003e8302008-11-20 00:15:42 +00001105 return LV;
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001106 }
1107
Eli Friedmand15eb34d2009-11-26 06:08:14 +00001108 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
1109 return EmitFunctionDeclLValue(*this, E, FD);
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001110
Anders Carlsson259688c2010-02-02 03:37:46 +00001111 // FIXME: the qualifier check does not seem sufficient here
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001112 if (E->getQualifier()) {
Anders Carlsson259688c2010-02-02 03:37:46 +00001113 const FieldDecl *FD = cast<FieldDecl>(ND);
1114 llvm::Value *V = CGM.EmitPointerToDataMember(FD);
1115
1116 return LValue::MakeAddr(V, MakeQualifiers(FD->getType()));
Chris Lattner5696e7b2008-06-17 18:05:57 +00001117 }
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001118
Anders Carlsson2ff6395d2009-11-07 22:53:10 +00001119 assert(false && "Unhandled DeclRefExpr");
1120
1121 // an invalid LValue, but the assert will
1122 // ensure that this point is never reached.
Chris Lattner793d10c2007-09-16 19:23:47 +00001123 return LValue();
Chris Lattnerd7f58862007-06-02 05:24:33 +00001124}
Chris Lattnere47e4402007-06-01 18:02:12 +00001125
Mike Stump1db7d042009-02-28 09:07:16 +00001126LValue CodeGenFunction::EmitBlockDeclRefLValue(const BlockDeclRefExpr *E) {
John McCall8ccfcb52009-09-24 19:53:00 +00001127 return LValue::MakeAddr(GetAddrOfBlockDecl(E), MakeQualifiers(E->getType()));
Mike Stump1db7d042009-02-28 09:07:16 +00001128}
1129
Chris Lattner8394d792007-06-05 20:53:16 +00001130LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) {
1131 // __extension__ doesn't affect lvalue-ness.
1132 if (E->getOpcode() == UnaryOperator::Extension)
1133 return EmitLValue(E->getSubExpr());
Mike Stump4a3999f2009-09-09 13:00:44 +00001134
Chris Lattner0f398c42008-07-26 22:37:01 +00001135 QualType ExprTy = getContext().getCanonicalType(E->getSubExpr()->getType());
Chris Lattner595db862007-10-30 22:53:42 +00001136 switch (E->getOpcode()) {
1137 default: assert(0 && "Unknown unary operator lvalue!");
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001138 case UnaryOperator::Deref: {
1139 QualType T = E->getSubExpr()->getType()->getPointeeType();
1140 assert(!T.isNull() && "CodeGenFunction::EmitUnaryOpLValue: Illegal type");
Mike Stump4a3999f2009-09-09 13:00:44 +00001141
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001142 Qualifiers Quals = MakeQualifiers(T);
1143 Quals.setAddressSpace(ExprTy.getAddressSpace());
John McCall8ccfcb52009-09-24 19:53:00 +00001144
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001145 LValue LV = LValue::MakeAddr(EmitScalarExpr(E->getSubExpr()), Quals);
1146 // We should not generate __weak write barrier on indirect reference
1147 // of a pointer to object; as in void foo (__weak id *param); *param = 0;
1148 // But, we continue to generate __strong write barrier on indirect write
1149 // into a pointer to object.
1150 if (getContext().getLangOptions().ObjC1 &&
1151 getContext().getLangOptions().getGCMode() != LangOptions::NonGC &&
1152 LV.isObjCWeak())
1153 LValue::SetObjCNonGC(LV, !E->isOBJCGCCandidate(getContext()));
1154 return LV;
1155 }
Chris Lattner595db862007-10-30 22:53:42 +00001156 case UnaryOperator::Real:
Eli Friedmana72bf0f2009-11-09 04:20:47 +00001157 case UnaryOperator::Imag: {
Chris Lattner595db862007-10-30 22:53:42 +00001158 LValue LV = EmitLValue(E->getSubExpr());
Chris Lattner3e593cd2008-03-19 05:19:41 +00001159 unsigned Idx = E->getOpcode() == UnaryOperator::Imag;
1160 return LValue::MakeAddr(Builder.CreateStructGEP(LV.getAddress(),
Chris Lattner574dee62008-07-26 22:17:49 +00001161 Idx, "idx"),
John McCall8ccfcb52009-09-24 19:53:00 +00001162 MakeQualifiers(ExprTy));
Chris Lattner595db862007-10-30 22:53:42 +00001163 }
Eli Friedmana72bf0f2009-11-09 04:20:47 +00001164 case UnaryOperator::PreInc:
Chris Lattnerbb8976e2010-01-09 21:44:40 +00001165 case UnaryOperator::PreDec: {
1166 LValue LV = EmitLValue(E->getSubExpr());
1167 bool isInc = E->getOpcode() == UnaryOperator::PreInc;
1168
1169 if (E->getType()->isAnyComplexType())
1170 EmitComplexPrePostIncDec(E, LV, isInc, true/*isPre*/);
1171 else
1172 EmitScalarPrePostIncDec(E, LV, isInc, true/*isPre*/);
1173 return LV;
1174 }
Eli Friedmana72bf0f2009-11-09 04:20:47 +00001175 }
Chris Lattner8394d792007-06-05 20:53:16 +00001176}
1177
Chris Lattner4347e3692007-06-06 04:54:52 +00001178LValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) {
John McCall8ccfcb52009-09-24 19:53:00 +00001179 return LValue::MakeAddr(CGM.GetAddrOfConstantStringFromLiteral(E),
1180 Qualifiers());
Chris Lattner4347e3692007-06-06 04:54:52 +00001181}
1182
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00001183LValue CodeGenFunction::EmitObjCEncodeExprLValue(const ObjCEncodeExpr *E) {
John McCall8ccfcb52009-09-24 19:53:00 +00001184 return LValue::MakeAddr(CGM.GetAddrOfConstantStringFromObjCEncode(E),
1185 Qualifiers());
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00001186}
1187
1188
Daniel Dunbarb3517472008-10-17 21:58:32 +00001189LValue CodeGenFunction::EmitPredefinedFunctionName(unsigned Type) {
Anders Carlsson625bfc82007-07-21 05:21:51 +00001190 std::string GlobalVarName;
Daniel Dunbarb3517472008-10-17 21:58:32 +00001191
1192 switch (Type) {
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001193 default: assert(0 && "Invalid type");
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00001194 case PredefinedExpr::Func:
1195 GlobalVarName = "__func__.";
1196 break;
1197 case PredefinedExpr::Function:
1198 GlobalVarName = "__FUNCTION__.";
1199 break;
1200 case PredefinedExpr::PrettyFunction:
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00001201 GlobalVarName = "__PRETTY_FUNCTION__.";
1202 break;
Anders Carlsson625bfc82007-07-21 05:21:51 +00001203 }
Daniel Dunbarb3517472008-10-17 21:58:32 +00001204
Daniel Dunbar0482cfd2009-09-12 23:06:21 +00001205 llvm::StringRef FnName = CurFn->getName();
1206 if (FnName.startswith("\01"))
1207 FnName = FnName.substr(1);
1208 GlobalVarName += FnName;
1209
Anders Carlsson2fb08242009-09-08 18:24:21 +00001210 std::string FunctionName =
Anders Carlsson5bd8d192010-02-11 18:20:28 +00001211 PredefinedExpr::ComputeName((PredefinedExpr::IdentType)Type, CurCodeDecl);
Daniel Dunbarb3517472008-10-17 21:58:32 +00001212
Mike Stump4a3999f2009-09-09 13:00:44 +00001213 llvm::Constant *C =
Daniel Dunbarb3517472008-10-17 21:58:32 +00001214 CGM.GetAddrOfConstantCString(FunctionName, GlobalVarName.c_str());
John McCall8ccfcb52009-09-24 19:53:00 +00001215 return LValue::MakeAddr(C, Qualifiers());
Daniel Dunbarb3517472008-10-17 21:58:32 +00001216}
1217
Mike Stump4a3999f2009-09-09 13:00:44 +00001218LValue CodeGenFunction::EmitPredefinedLValue(const PredefinedExpr *E) {
Daniel Dunbarb3517472008-10-17 21:58:32 +00001219 switch (E->getIdentType()) {
1220 default:
1221 return EmitUnsupportedLValue(E, "predefined expression");
1222 case PredefinedExpr::Func:
1223 case PredefinedExpr::Function:
1224 case PredefinedExpr::PrettyFunction:
1225 return EmitPredefinedFunctionName(E->getIdentType());
1226 }
Anders Carlsson625bfc82007-07-21 05:21:51 +00001227}
1228
Mike Stumpcf16d2c2009-12-15 01:22:35 +00001229llvm::BasicBlock *CodeGenFunction::getTrapBB() {
Mike Stump9a4e0122009-12-15 00:59:40 +00001230 const CodeGenOptions &GCO = CGM.getCodeGenOpts();
1231
1232 // If we are not optimzing, don't collapse all calls to trap in the function
1233 // to the same call, that way, in the debugger they can see which operation
1234 // did in fact fail. If we are optimizing, we collpase all call to trap down
1235 // to just one per function to save on codesize.
1236 if (GCO.OptimizationLevel
1237 && TrapBB)
Mike Stumpe8c3b3e2009-12-15 00:35:12 +00001238 return TrapBB;
Mike Stumpd9546382009-12-12 01:27:46 +00001239
1240 llvm::BasicBlock *Cont = 0;
1241 if (HaveInsertPoint()) {
1242 Cont = createBasicBlock("cont");
1243 EmitBranch(Cont);
1244 }
Mike Stumpe8c3b3e2009-12-15 00:35:12 +00001245 TrapBB = createBasicBlock("trap");
1246 EmitBlock(TrapBB);
1247
1248 llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::trap, 0, 0);
1249 llvm::CallInst *TrapCall = Builder.CreateCall(F);
1250 TrapCall->setDoesNotReturn();
1251 TrapCall->setDoesNotThrow();
Mike Stumpd9546382009-12-12 01:27:46 +00001252 Builder.CreateUnreachable();
1253
1254 if (Cont)
1255 EmitBlock(Cont);
Mike Stumpe8c3b3e2009-12-15 00:35:12 +00001256 return TrapBB;
Mike Stumpd9546382009-12-12 01:27:46 +00001257}
1258
Chris Lattnerd9d2fb12007-06-08 23:31:14 +00001259LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) {
Ted Kremenekc81614d2007-08-20 16:18:38 +00001260 // The index must always be an integer, which is not an aggregate. Emit it.
Chris Lattner2da04b32007-08-24 05:35:26 +00001261 llvm::Value *Idx = EmitScalarExpr(E->getIdx());
Eli Friedman07bbeca2009-06-06 19:09:26 +00001262 QualType IdxTy = E->getIdx()->getType();
1263 bool IdxSigned = IdxTy->isSignedIntegerType();
1264
Chris Lattner08c4b9f2007-07-10 21:17:59 +00001265 // If the base is a vector type, then we are forming a vector element lvalue
1266 // with this subscript.
Eli Friedman327944b2008-06-13 23:01:12 +00001267 if (E->getBase()->getType()->isVectorType()) {
Chris Lattner08c4b9f2007-07-10 21:17:59 +00001268 // Emit the vector as an lvalue to get its address.
Eli Friedman327944b2008-06-13 23:01:12 +00001269 LValue LHS = EmitLValue(E->getBase());
Ted Kremenekc81614d2007-08-20 16:18:38 +00001270 assert(LHS.isSimple() && "Can only subscript lvalue vectors here!");
Mike Stump4a3999f2009-09-09 13:00:44 +00001271 Idx = Builder.CreateIntCast(Idx,
Owen Anderson41a75022009-08-13 21:57:51 +00001272 llvm::Type::getInt32Ty(VMContext), IdxSigned, "vidx");
Eli Friedman327944b2008-06-13 23:01:12 +00001273 return LValue::MakeVectorElt(LHS.getAddress(), Idx,
John McCall8ccfcb52009-09-24 19:53:00 +00001274 E->getBase()->getType().getCVRQualifiers());
Chris Lattner08c4b9f2007-07-10 21:17:59 +00001275 }
Mike Stump4a3999f2009-09-09 13:00:44 +00001276
Ted Kremenekc81614d2007-08-20 16:18:38 +00001277 // The base must be a pointer, which is not an aggregate. Emit it.
Chris Lattner2da04b32007-08-24 05:35:26 +00001278 llvm::Value *Base = EmitScalarExpr(E->getBase());
Mike Stump4a3999f2009-09-09 13:00:44 +00001279
Ted Kremenekc81614d2007-08-20 16:18:38 +00001280 // Extend or truncate the index type to 32 or 64-bits.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001281 unsigned IdxBitwidth = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
Sanjiv Gupta47425152009-04-24 02:40:57 +00001282 if (IdxBitwidth != LLVMPointerWidth)
Owen Anderson41a75022009-08-13 21:57:51 +00001283 Idx = Builder.CreateIntCast(Idx,
1284 llvm::IntegerType::get(VMContext, LLVMPointerWidth),
Chris Lattnerd9d2fb12007-06-08 23:31:14 +00001285 IdxSigned, "idxprom");
1286
Mike Stump3f6f9fe2009-12-16 02:57:00 +00001287 // FIXME: As llvm implements the object size checking, this can come out.
Mike Stumpd9546382009-12-12 01:27:46 +00001288 if (CatchUndefined) {
Mike Stump3f6f9fe2009-12-16 02:57:00 +00001289 if (const ImplicitCastExpr *ICE=dyn_cast<ImplicitCastExpr>(E->getBase())) {
Mike Stumpd9546382009-12-12 01:27:46 +00001290 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr())) {
1291 if (ICE->getCastKind() == CastExpr::CK_ArrayToPointerDecay) {
1292 if (const ConstantArrayType *CAT
1293 = getContext().getAsConstantArrayType(DRE->getType())) {
1294 llvm::APInt Size = CAT->getSize();
1295 llvm::BasicBlock *Cont = createBasicBlock("cont");
Mike Stump590d18f2009-12-14 22:14:31 +00001296 Builder.CreateCondBr(Builder.CreateICmpULE(Idx,
Mike Stumpd9546382009-12-12 01:27:46 +00001297 llvm::ConstantInt::get(Idx->getType(), Size)),
Mike Stumpe8c3b3e2009-12-15 00:35:12 +00001298 Cont, getTrapBB());
Mike Stumpf8858af2009-12-14 20:52:00 +00001299 EmitBlock(Cont);
Mike Stumpd9546382009-12-12 01:27:46 +00001300 }
1301 }
1302 }
1303 }
1304 }
1305
Mike Stump4a3999f2009-09-09 13:00:44 +00001306 // We know that the pointer points to a type of the correct size, unless the
1307 // size is a VLA or Objective-C interface.
Daniel Dunbaref2ffbc2009-04-25 05:08:32 +00001308 llvm::Value *Address = 0;
Mike Stump4a3999f2009-09-09 13:00:44 +00001309 if (const VariableArrayType *VAT =
Anders Carlsson3d312f82008-12-21 00:11:23 +00001310 getContext().getAsVariableArrayType(E->getType())) {
Chris Lattner19efdd62009-08-14 23:43:22 +00001311 llvm::Value *VLASize = GetVLASize(VAT);
Mike Stump4a3999f2009-09-09 13:00:44 +00001312
Anders Carlsson3d312f82008-12-21 00:11:23 +00001313 Idx = Builder.CreateMul(Idx, VLASize);
Mike Stump4a3999f2009-09-09 13:00:44 +00001314
Anders Carlssone0808df2008-12-21 03:44:36 +00001315 QualType BaseType = getContext().getBaseElementType(VAT);
Mike Stump4a3999f2009-09-09 13:00:44 +00001316
Ken Dyck40775002010-01-11 17:06:35 +00001317 CharUnits BaseTypeSize = getContext().getTypeSizeInChars(BaseType);
Anders Carlsson3d312f82008-12-21 00:11:23 +00001318 Idx = Builder.CreateUDiv(Idx,
Mike Stump4a3999f2009-09-09 13:00:44 +00001319 llvm::ConstantInt::get(Idx->getType(),
Ken Dyck40775002010-01-11 17:06:35 +00001320 BaseTypeSize.getQuantity()));
Dan Gohman43b44842009-08-12 00:33:55 +00001321 Address = Builder.CreateInBoundsGEP(Base, Idx, "arrayidx");
Mike Stump4a3999f2009-09-09 13:00:44 +00001322 } else if (const ObjCInterfaceType *OIT =
Daniel Dunbaref2ffbc2009-04-25 05:08:32 +00001323 dyn_cast<ObjCInterfaceType>(E->getType())) {
Mike Stump4a3999f2009-09-09 13:00:44 +00001324 llvm::Value *InterfaceSize =
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001325 llvm::ConstantInt::get(Idx->getType(),
Ken Dyck40775002010-01-11 17:06:35 +00001326 getContext().getTypeSizeInChars(OIT).getQuantity());
Mike Stump4a3999f2009-09-09 13:00:44 +00001327
Daniel Dunbaref2ffbc2009-04-25 05:08:32 +00001328 Idx = Builder.CreateMul(Idx, InterfaceSize);
1329
Benjamin Kramerabd5b902009-10-13 10:07:13 +00001330 const llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(VMContext);
Dan Gohman43b44842009-08-12 00:33:55 +00001331 Address = Builder.CreateGEP(Builder.CreateBitCast(Base, i8PTy),
Daniel Dunbaref2ffbc2009-04-25 05:08:32 +00001332 Idx, "arrayidx");
1333 Address = Builder.CreateBitCast(Address, Base->getType());
1334 } else {
Dan Gohman43b44842009-08-12 00:33:55 +00001335 Address = Builder.CreateInBoundsGEP(Base, Idx, "arrayidx");
Anders Carlsson3d312f82008-12-21 00:11:23 +00001336 }
Mike Stump4a3999f2009-09-09 13:00:44 +00001337
Steve Naroff7cae42b2009-07-10 23:34:53 +00001338 QualType T = E->getBase()->getType()->getPointeeType();
Mike Stump4a3999f2009-09-09 13:00:44 +00001339 assert(!T.isNull() &&
Steve Naroff7cae42b2009-07-10 23:34:53 +00001340 "CodeGenFunction::EmitArraySubscriptExpr(): Illegal base type");
Mike Stump4a3999f2009-09-09 13:00:44 +00001341
John McCall8ccfcb52009-09-24 19:53:00 +00001342 Qualifiers Quals = MakeQualifiers(T);
1343 Quals.setAddressSpace(E->getBase()->getType().getAddressSpace());
1344
1345 LValue LV = LValue::MakeAddr(Address, Quals);
Fariborz Jahaniana9fecf32009-02-21 23:37:19 +00001346 if (getContext().getLangOptions().ObjC1 &&
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +00001347 getContext().getLangOptions().getGCMode() != LangOptions::NonGC) {
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001348 LValue::SetObjCNonGC(LV, !E->isOBJCGCCandidate(getContext()));
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +00001349 setObjCGCLValueClass(getContext(), E, LV);
1350 }
Fariborz Jahaniana9fecf32009-02-21 23:37:19 +00001351 return LV;
Chris Lattnerd9d2fb12007-06-08 23:31:14 +00001352}
1353
Mike Stump4a3999f2009-09-09 13:00:44 +00001354static
Owen Anderson170229f2009-07-14 23:10:40 +00001355llvm::Constant *GenerateConstantVector(llvm::LLVMContext &VMContext,
1356 llvm::SmallVector<unsigned, 4> &Elts) {
Chris Lattner4e1a3232009-12-23 21:31:11 +00001357 llvm::SmallVector<llvm::Constant*, 4> CElts;
Mike Stump4a3999f2009-09-09 13:00:44 +00001358
Nate Begemand3862152008-05-13 21:03:02 +00001359 for (unsigned i = 0, e = Elts.size(); i != e; ++i)
Owen Anderson41a75022009-08-13 21:57:51 +00001360 CElts.push_back(llvm::ConstantInt::get(
1361 llvm::Type::getInt32Ty(VMContext), Elts[i]));
Nate Begemand3862152008-05-13 21:03:02 +00001362
Owen Anderson3cc120a2009-07-28 21:22:35 +00001363 return llvm::ConstantVector::get(&CElts[0], CElts.size());
Nate Begemand3862152008-05-13 21:03:02 +00001364}
1365
Chris Lattner9e751ca2007-08-02 23:37:31 +00001366LValue CodeGenFunction::
Nate Begemance4d7fc2008-04-18 23:10:10 +00001367EmitExtVectorElementExpr(const ExtVectorElementExpr *E) {
Chris Lattner4e1a3232009-12-23 21:31:11 +00001368 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
1369
Chris Lattner9e751ca2007-08-02 23:37:31 +00001370 // Emit the base vector as an l-value.
Chris Lattner6c7ce102009-02-16 21:11:58 +00001371 LValue Base;
1372
1373 // ExtVectorElementExpr's base can either be a vector or pointer to vector.
Chris Lattner4e1a3232009-12-23 21:31:11 +00001374 if (E->isArrow()) {
1375 // If it is a pointer to a vector, emit the address and form an lvalue with
1376 // it.
Chris Lattnerb8211f62009-02-16 22:14:05 +00001377 llvm::Value *Ptr = EmitScalarExpr(E->getBase());
Chris Lattner4e1a3232009-12-23 21:31:11 +00001378 const PointerType *PT = E->getBase()->getType()->getAs<PointerType>();
John McCall8ccfcb52009-09-24 19:53:00 +00001379 Qualifiers Quals = MakeQualifiers(PT->getPointeeType());
1380 Quals.removeObjCGCAttr();
1381 Base = LValue::MakeAddr(Ptr, Quals);
Chris Lattner4e1a3232009-12-23 21:31:11 +00001382 } else if (E->getBase()->isLvalue(getContext()) == Expr::LV_Valid) {
1383 // Otherwise, if the base is an lvalue ( as in the case of foo.x.x),
1384 // emit the base as an lvalue.
1385 assert(E->getBase()->getType()->isVectorType());
1386 Base = EmitLValue(E->getBase());
1387 } else {
1388 // Otherwise, the base is a normal rvalue (as in (V+V).x), emit it as such.
Daniel Dunbar5b901952010-01-04 18:02:28 +00001389 assert(E->getBase()->getType()->getAs<VectorType>() &&
1390 "Result must be a vector");
Chris Lattner4e1a3232009-12-23 21:31:11 +00001391 llvm::Value *Vec = EmitScalarExpr(E->getBase());
1392
Chris Lattnerf0a9ba32009-12-23 21:33:41 +00001393 // Store the vector to memory (because LValue wants an address).
Daniel Dunbara7566f12010-02-09 02:48:28 +00001394 llvm::Value *VecMem = CreateMemTemp(E->getBase()->getType());
Chris Lattner4e1a3232009-12-23 21:31:11 +00001395 Builder.CreateStore(Vec, VecMem);
Chris Lattnerf0a9ba32009-12-23 21:33:41 +00001396 Base = LValue::MakeAddr(VecMem, Qualifiers());
Chris Lattner4e1a3232009-12-23 21:31:11 +00001397 }
1398
Nate Begemand3862152008-05-13 21:03:02 +00001399 // Encode the element access list into a vector of unsigned indices.
1400 llvm::SmallVector<unsigned, 4> Indices;
1401 E->getEncodedElementAccess(Indices);
1402
1403 if (Base.isSimple()) {
Owen Anderson170229f2009-07-14 23:10:40 +00001404 llvm::Constant *CV = GenerateConstantVector(VMContext, Indices);
Eli Friedman327944b2008-06-13 23:01:12 +00001405 return LValue::MakeExtVectorElt(Base.getAddress(), CV,
John McCall8ccfcb52009-09-24 19:53:00 +00001406 Base.getVRQualifiers());
Nate Begemand3862152008-05-13 21:03:02 +00001407 }
1408 assert(Base.isExtVectorElt() && "Can only subscript lvalue vec elts here!");
1409
1410 llvm::Constant *BaseElts = Base.getExtVectorElts();
1411 llvm::SmallVector<llvm::Constant *, 4> CElts;
1412
1413 for (unsigned i = 0, e = Indices.size(); i != e; ++i) {
1414 if (isa<llvm::ConstantAggregateZero>(BaseElts))
Chris Lattner5e71d432009-10-28 05:12:07 +00001415 CElts.push_back(llvm::ConstantInt::get(Int32Ty, 0));
Nate Begemand3862152008-05-13 21:03:02 +00001416 else
Chris Lattner5e71d432009-10-28 05:12:07 +00001417 CElts.push_back(cast<llvm::Constant>(BaseElts->getOperand(Indices[i])));
Nate Begemand3862152008-05-13 21:03:02 +00001418 }
Owen Anderson3cc120a2009-07-28 21:22:35 +00001419 llvm::Constant *CV = llvm::ConstantVector::get(&CElts[0], CElts.size());
Eli Friedman327944b2008-06-13 23:01:12 +00001420 return LValue::MakeExtVectorElt(Base.getExtVectorAddr(), CV,
John McCall8ccfcb52009-09-24 19:53:00 +00001421 Base.getVRQualifiers());
Chris Lattner9e751ca2007-08-02 23:37:31 +00001422}
1423
Devang Patel30efa2e2007-10-23 20:28:39 +00001424LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) {
Fariborz Jahanian10bec102009-02-21 00:30:43 +00001425 bool isNonGC = false;
Devang Pateld68df202007-10-24 22:26:28 +00001426 Expr *BaseExpr = E->getBase();
Devang Pateld68df202007-10-24 22:26:28 +00001427 llvm::Value *BaseValue = NULL;
John McCall8ccfcb52009-09-24 19:53:00 +00001428 Qualifiers BaseQuals;
Eli Friedman327944b2008-06-13 23:01:12 +00001429
Chris Lattner4e4186b2007-12-02 18:52:07 +00001430 // 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 +00001431 if (E->isArrow()) {
Devang Patel7718d7a2007-10-26 18:15:21 +00001432 BaseValue = EmitScalarExpr(BaseExpr);
Mike Stump4a3999f2009-09-09 13:00:44 +00001433 const PointerType *PTy =
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001434 BaseExpr->getType()->getAs<PointerType>();
John McCall8ccfcb52009-09-24 19:53:00 +00001435 BaseQuals = PTy->getPointeeType().getQualifiers();
Fariborz Jahanian1a504772009-09-01 17:02:21 +00001436 } else if (isa<ObjCPropertyRefExpr>(BaseExpr->IgnoreParens()) ||
1437 isa<ObjCImplicitSetterGetterRefExpr>(
1438 BaseExpr->IgnoreParens())) {
Fariborz Jahanian30e78642009-01-12 23:27:26 +00001439 RValue RV = EmitObjCPropertyGet(BaseExpr);
1440 BaseValue = RV.getAggregateAddr();
John McCall8ccfcb52009-09-24 19:53:00 +00001441 BaseQuals = BaseExpr->getType().getQualifiers();
Chris Lattnere084c012009-02-16 22:25:49 +00001442 } else {
Chris Lattner4e4186b2007-12-02 18:52:07 +00001443 LValue BaseLV = EmitLValue(BaseExpr);
Fariborz Jahanian10bec102009-02-21 00:30:43 +00001444 if (BaseLV.isNonGC())
1445 isNonGC = true;
Chris Lattner4e4186b2007-12-02 18:52:07 +00001446 // FIXME: this isn't right for bitfields.
1447 BaseValue = BaseLV.getAddress();
Fariborz Jahanian82e28742009-07-29 00:44:13 +00001448 QualType BaseTy = BaseExpr->getType();
John McCall8ccfcb52009-09-24 19:53:00 +00001449 BaseQuals = BaseTy.getQualifiers();
Chris Lattner4e4186b2007-12-02 18:52:07 +00001450 }
Devang Patel30efa2e2007-10-23 20:28:39 +00001451
Anders Carlssonea4c30b2009-11-07 23:06:58 +00001452 NamedDecl *ND = E->getMemberDecl();
1453 if (FieldDecl *Field = dyn_cast<FieldDecl>(ND)) {
Anders Carlsson5d8645b2010-01-29 05:05:36 +00001454 LValue LV = EmitLValueForField(BaseValue, Field,
Anders Carlssonea4c30b2009-11-07 23:06:58 +00001455 BaseQuals.getCVRQualifiers());
1456 LValue::SetObjCNonGC(LV, isNonGC);
1457 setObjCGCLValueClass(getContext(), E, LV);
1458 return LV;
1459 }
1460
Anders Carlsson5bbdc9f2009-11-07 23:16:50 +00001461 if (VarDecl *VD = dyn_cast<VarDecl>(ND))
1462 return EmitGlobalVarDeclLValue(*this, E, VD);
Eli Friedmand15eb34d2009-11-26 06:08:14 +00001463
1464 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
1465 return EmitFunctionDeclLValue(*this, E, FD);
1466
Anders Carlssonea4c30b2009-11-07 23:06:58 +00001467 assert(false && "Unhandled member declaration!");
1468 return LValue();
Eli Friedmana62f3e12008-02-09 08:50:58 +00001469}
Devang Patel30efa2e2007-10-23 20:28:39 +00001470
Fariborz Jahanianb517e902008-12-15 20:35:07 +00001471LValue CodeGenFunction::EmitLValueForBitfield(llvm::Value* BaseValue,
Anders Carlssoncfd30122009-11-17 03:57:07 +00001472 const FieldDecl* Field,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001473 unsigned CVRQualifiers) {
Daniel Dunbar034299e2010-03-31 01:09:11 +00001474 const CGRecordLayout &RL =
1475 CGM.getTypes().getCGRecordLayout(Field->getParent());
Daniel Dunbarcd3d5e72010-04-05 16:20:44 +00001476 const CGBitFieldInfo &Info = RL.getBitFieldInfo(Field);
Anders Carlsson8af896c2009-07-23 17:01:21 +00001477
Mike Stump18bb9282009-05-16 07:57:57 +00001478 // FIXME: CodeGenTypes should expose a method to get the appropriate type for
1479 // FieldTy (the appropriate type is ABI-dependent).
Mike Stump4a3999f2009-09-09 13:00:44 +00001480 const llvm::Type *FieldTy =
Daniel Dunbar3d926cb2009-02-17 18:31:04 +00001481 CGM.getTypes().ConvertTypeForMem(Field->getType());
Fariborz Jahanianb517e902008-12-15 20:35:07 +00001482 const llvm::PointerType *BaseTy =
1483 cast<llvm::PointerType>(BaseValue->getType());
1484 unsigned AS = BaseTy->getAddressSpace();
1485 BaseValue = Builder.CreateBitCast(BaseValue,
Daniel Dunbar60d81e82010-04-06 01:07:39 +00001486 llvm::PointerType::get(FieldTy, AS));
1487 llvm::Value *V = Builder.CreateConstGEP1_32(BaseValue, Info.FieldNo);
Mike Stump4a3999f2009-09-09 13:00:44 +00001488
Daniel Dunbar196ea442010-04-06 01:07:44 +00001489 return LValue::MakeBitfield(V, Info,
Daniel Dunbardc406b82010-04-05 21:36:35 +00001490 Field->getType().getCVRQualifiers()|CVRQualifiers);
Fariborz Jahanianb517e902008-12-15 20:35:07 +00001491}
1492
Eli Friedmana62f3e12008-02-09 08:50:58 +00001493LValue CodeGenFunction::EmitLValueForField(llvm::Value* BaseValue,
Anders Carlssoncfd30122009-11-17 03:57:07 +00001494 const FieldDecl* Field,
Mike Stump11289f42009-09-09 15:08:12 +00001495 unsigned CVRQualifiers) {
Fariborz Jahanianb517e902008-12-15 20:35:07 +00001496 if (Field->isBitField())
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001497 return EmitLValueForBitfield(BaseValue, Field, CVRQualifiers);
Mike Stump4a3999f2009-09-09 13:00:44 +00001498
Daniel Dunbar034299e2010-03-31 01:09:11 +00001499 const CGRecordLayout &RL =
1500 CGM.getTypes().getCGRecordLayout(Field->getParent());
1501 unsigned idx = RL.getLLVMFieldNo(Field);
Fariborz Jahanianb517e902008-12-15 20:35:07 +00001502 llvm::Value *V = Builder.CreateStructGEP(BaseValue, idx, "tmp");
Eli Friedman133e8042008-05-29 11:33:25 +00001503
Devang Pateled93c3c2007-10-26 19:42:18 +00001504 // Match union field type.
Anders Carlsson5d8645b2010-01-29 05:05:36 +00001505 if (Field->getParent()->isUnion()) {
Mike Stump4a3999f2009-09-09 13:00:44 +00001506 const llvm::Type *FieldTy =
Eli Friedman327944b2008-06-13 23:01:12 +00001507 CGM.getTypes().ConvertTypeForMem(Field->getType());
Mike Stump4a3999f2009-09-09 13:00:44 +00001508 const llvm::PointerType * BaseTy =
Devang Patelffe1e212007-10-30 20:59:40 +00001509 cast<llvm::PointerType>(BaseValue->getType());
Eli Friedman9a5ffcb2008-05-21 13:24:44 +00001510 unsigned AS = BaseTy->getAddressSpace();
Mike Stump4a3999f2009-09-09 13:00:44 +00001511 V = Builder.CreateBitCast(V,
1512 llvm::PointerType::get(FieldTy, AS),
Eli Friedman9a5ffcb2008-05-21 13:24:44 +00001513 "tmp");
Devang Pateled93c3c2007-10-26 19:42:18 +00001514 }
Eli Friedmanf7f9f682009-05-30 21:09:44 +00001515 if (Field->getType()->isReferenceType())
1516 V = Builder.CreateLoad(V, "tmp");
John McCall8ccfcb52009-09-24 19:53:00 +00001517
1518 Qualifiers Quals = MakeQualifiers(Field->getType());
1519 Quals.addCVRQualifiers(CVRQualifiers);
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +00001520 // __weak attribute on a field is ignored.
John McCall8ccfcb52009-09-24 19:53:00 +00001521 if (Quals.getObjCGCAttr() == Qualifiers::Weak)
1522 Quals.removeObjCGCAttr();
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +00001523
John McCall8ccfcb52009-09-24 19:53:00 +00001524 return LValue::MakeAddr(V, Quals);
Devang Patel30efa2e2007-10-23 20:28:39 +00001525}
1526
Anders Carlssondb78f0a2010-01-29 05:24:29 +00001527LValue
1528CodeGenFunction::EmitLValueForFieldInitialization(llvm::Value* BaseValue,
1529 const FieldDecl* Field,
1530 unsigned CVRQualifiers) {
1531 QualType FieldType = Field->getType();
1532
1533 if (!FieldType->isReferenceType())
1534 return EmitLValueForField(BaseValue, Field, CVRQualifiers);
1535
Daniel Dunbar034299e2010-03-31 01:09:11 +00001536 const CGRecordLayout &RL =
1537 CGM.getTypes().getCGRecordLayout(Field->getParent());
1538 unsigned idx = RL.getLLVMFieldNo(Field);
Anders Carlssondb78f0a2010-01-29 05:24:29 +00001539 llvm::Value *V = Builder.CreateStructGEP(BaseValue, idx, "tmp");
1540
1541 assert(!FieldType.getObjCGCAttr() && "fields cannot have GC attrs");
1542
1543 return LValue::MakeAddr(V, MakeQualifiers(FieldType));
1544}
1545
Chris Lattner28bcf1a2009-03-18 18:28:57 +00001546LValue CodeGenFunction::EmitCompoundLiteralLValue(const CompoundLiteralExpr* E){
Daniel Dunbar27bacaf2010-02-16 19:43:39 +00001547 llvm::Value *DeclPtr = CreateMemTemp(E->getType(), ".compoundliteral");
Eli Friedman9fd8b682008-05-13 23:18:27 +00001548 const Expr* InitExpr = E->getInitializer();
John McCall8ccfcb52009-09-24 19:53:00 +00001549 LValue Result = LValue::MakeAddr(DeclPtr, MakeQualifiers(E->getType()));
Eli Friedman9fd8b682008-05-13 23:18:27 +00001550
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001551 if (E->getType()->isComplexType())
Eli Friedman9fd8b682008-05-13 23:18:27 +00001552 EmitComplexExprIntoAddr(InitExpr, DeclPtr, false);
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001553 else if (hasAggregateLLVMType(E->getType()))
Eli Friedman9fd8b682008-05-13 23:18:27 +00001554 EmitAnyExpr(InitExpr, DeclPtr, false);
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001555 else
Eli Friedman9fd8b682008-05-13 23:18:27 +00001556 EmitStoreThroughLValue(EmitAnyExpr(InitExpr), Result, E->getType());
Eli Friedman9fd8b682008-05-13 23:18:27 +00001557
1558 return Result;
1559}
1560
Anders Carlsson1450adb2009-09-15 16:35:24 +00001561LValue
1562CodeGenFunction::EmitConditionalOperatorLValue(const ConditionalOperator* E) {
1563 if (E->isLvalue(getContext()) == Expr::LV_Valid) {
Eli Friedman2e06e8b2009-12-25 05:29:40 +00001564 if (int Cond = ConstantFoldsToSimpleInteger(E->getCond())) {
1565 Expr *Live = Cond == 1 ? E->getLHS() : E->getRHS();
1566 if (Live)
1567 return EmitLValue(Live);
1568 }
1569
1570 if (!E->getLHS())
1571 return EmitUnsupportedLValue(E, "conditional operator with missing LHS");
1572
Anders Carlsson1450adb2009-09-15 16:35:24 +00001573 llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true");
1574 llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false");
1575 llvm::BasicBlock *ContBlock = createBasicBlock("cond.end");
1576
Eli Friedmanb8841af2009-12-25 06:17:05 +00001577 EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock);
Anders Carlsson1450adb2009-09-15 16:35:24 +00001578
Anders Carlsson9b942c62010-02-04 17:26:01 +00001579 // Any temporaries created here are conditional.
1580 BeginConditionalBranch();
Anders Carlsson1450adb2009-09-15 16:35:24 +00001581 EmitBlock(LHSBlock);
Anders Carlsson1450adb2009-09-15 16:35:24 +00001582 LValue LHS = EmitLValue(E->getLHS());
Anders Carlsson9b942c62010-02-04 17:26:01 +00001583 EndConditionalBranch();
1584
Anders Carlsson1450adb2009-09-15 16:35:24 +00001585 if (!LHS.isSimple())
1586 return EmitUnsupportedLValue(E, "conditional operator");
1587
Daniel Dunbara7566f12010-02-09 02:48:28 +00001588 // FIXME: We shouldn't need an alloca for this.
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001589 llvm::Value *Temp = CreateTempAlloca(LHS.getAddress()->getType(),"condtmp");
Anders Carlsson1450adb2009-09-15 16:35:24 +00001590 Builder.CreateStore(LHS.getAddress(), Temp);
1591 EmitBranch(ContBlock);
1592
Anders Carlsson9b942c62010-02-04 17:26:01 +00001593 // Any temporaries created here are conditional.
1594 BeginConditionalBranch();
Anders Carlsson1450adb2009-09-15 16:35:24 +00001595 EmitBlock(RHSBlock);
1596 LValue RHS = EmitLValue(E->getRHS());
Anders Carlsson9b942c62010-02-04 17:26:01 +00001597 EndConditionalBranch();
Anders Carlsson1450adb2009-09-15 16:35:24 +00001598 if (!RHS.isSimple())
1599 return EmitUnsupportedLValue(E, "conditional operator");
1600
1601 Builder.CreateStore(RHS.getAddress(), Temp);
1602 EmitBranch(ContBlock);
1603
1604 EmitBlock(ContBlock);
1605
1606 Temp = Builder.CreateLoad(Temp, "lv");
John McCall8ccfcb52009-09-24 19:53:00 +00001607 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
Anders Carlsson1450adb2009-09-15 16:35:24 +00001608 }
1609
Daniel Dunbarbf1fe8c2009-03-24 02:38:23 +00001610 // ?: here should be an aggregate.
Mike Stump4a3999f2009-09-09 13:00:44 +00001611 assert((hasAggregateLLVMType(E->getType()) &&
Daniel Dunbarbf1fe8c2009-03-24 02:38:23 +00001612 !E->getType()->isAnyComplexType()) &&
1613 "Unexpected conditional operator!");
1614
Daniel Dunbard0bc7b92010-02-05 19:38:31 +00001615 return EmitAggExprToLValue(E);
Daniel Dunbarbf1fe8c2009-03-24 02:38:23 +00001616}
1617
Mike Stump65511702009-11-16 06:50:58 +00001618/// EmitCastLValue - Casts are never lvalues unless that cast is a dynamic_cast.
1619/// If the cast is a dynamic_cast, we can have the usual lvalue result,
1620/// otherwise if a cast is needed by the code generator in an lvalue context,
1621/// then it must mean that we need the address of an aggregate in order to
1622/// access one of its fields. This can happen for all the reasons that casts
1623/// are permitted with aggregate result, including noop aggregate casts, and
1624/// cast from scalar to union.
Chris Lattner28bcf1a2009-03-18 18:28:57 +00001625LValue CodeGenFunction::EmitCastLValue(const CastExpr *E) {
Anders Carlssond95f9602009-09-12 16:16:49 +00001626 switch (E->getCastKind()) {
1627 default:
Eli Friedman8c98dff2009-11-16 05:48:01 +00001628 return EmitUnsupportedLValue(E, "unexpected cast lvalue");
1629
Mike Stump65511702009-11-16 06:50:58 +00001630 case CastExpr::CK_Dynamic: {
1631 LValue LV = EmitLValue(E->getSubExpr());
1632 llvm::Value *V = LV.getAddress();
1633 const CXXDynamicCastExpr *DCE = cast<CXXDynamicCastExpr>(E);
1634 return LValue::MakeAddr(EmitDynamicCast(V, DCE),
1635 MakeQualifiers(E->getType()));
1636 }
1637
Anders Carlssond95f9602009-09-12 16:16:49 +00001638 case CastExpr::CK_NoOp:
1639 case CastExpr::CK_ConstructorConversion:
1640 case CastExpr::CK_UserDefinedConversion:
Fariborz Jahanian2b9fc832009-12-15 21:34:52 +00001641 case CastExpr::CK_AnyPointerToObjCPointerCast:
Chris Lattner28bcf1a2009-03-18 18:28:57 +00001642 return EmitLValue(E->getSubExpr());
Anders Carlssond95f9602009-09-12 16:16:49 +00001643
John McCalld9c7c6562010-03-30 23:58:03 +00001644 case CastExpr::CK_UncheckedDerivedToBase:
Anders Carlssond95f9602009-09-12 16:16:49 +00001645 case CastExpr::CK_DerivedToBase: {
1646 const RecordType *DerivedClassTy =
1647 E->getSubExpr()->getType()->getAs<RecordType>();
1648 CXXRecordDecl *DerivedClassDecl =
1649 cast<CXXRecordDecl>(DerivedClassTy->getDecl());
Chris Lattner28bcf1a2009-03-18 18:28:57 +00001650
Anders Carlssond95f9602009-09-12 16:16:49 +00001651 const RecordType *BaseClassTy = E->getType()->getAs<RecordType>();
1652 CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseClassTy->getDecl());
1653
1654 LValue LV = EmitLValue(E->getSubExpr());
1655
1656 // Perform the derived-to-base conversion
1657 llvm::Value *Base =
Anders Carlsson8c793172009-11-23 17:57:54 +00001658 GetAddressOfBaseClass(LV.getAddress(), DerivedClassDecl,
1659 BaseClassDecl, /*NullCheckValue=*/false);
Anders Carlssond95f9602009-09-12 16:16:49 +00001660
John McCall8ccfcb52009-09-24 19:53:00 +00001661 return LValue::MakeAddr(Base, MakeQualifiers(E->getType()));
Anders Carlssond95f9602009-09-12 16:16:49 +00001662 }
Daniel Dunbar9c4e4652010-02-05 20:02:42 +00001663 case CastExpr::CK_ToUnion:
1664 return EmitAggExprToLValue(E);
Eli Friedman8c98dff2009-11-16 05:48:01 +00001665 case CastExpr::CK_BaseToDerived: {
Anders Carlsson8c793172009-11-23 17:57:54 +00001666 const RecordType *BaseClassTy =
1667 E->getSubExpr()->getType()->getAs<RecordType>();
1668 CXXRecordDecl *BaseClassDecl =
1669 cast<CXXRecordDecl>(BaseClassTy->getDecl());
1670
1671 const RecordType *DerivedClassTy = E->getType()->getAs<RecordType>();
1672 CXXRecordDecl *DerivedClassDecl =
1673 cast<CXXRecordDecl>(DerivedClassTy->getDecl());
1674
1675 LValue LV = EmitLValue(E->getSubExpr());
1676
1677 // Perform the base-to-derived conversion
1678 llvm::Value *Derived =
1679 GetAddressOfDerivedClass(LV.getAddress(), BaseClassDecl,
1680 DerivedClassDecl, /*NullCheckValue=*/false);
1681
1682 return LValue::MakeAddr(Derived, MakeQualifiers(E->getType()));
Eli Friedman8c98dff2009-11-16 05:48:01 +00001683 }
Anders Carlsson50cb3212009-11-14 21:21:42 +00001684 case CastExpr::CK_BitCast: {
Eli Friedman8c98dff2009-11-16 05:48:01 +00001685 // This must be a reinterpret_cast (or c-style equivalent).
1686 const ExplicitCastExpr *CE = cast<ExplicitCastExpr>(E);
Anders Carlsson50cb3212009-11-14 21:21:42 +00001687
1688 LValue LV = EmitLValue(E->getSubExpr());
1689 llvm::Value *V = Builder.CreateBitCast(LV.getAddress(),
1690 ConvertType(CE->getTypeAsWritten()));
1691 return LValue::MakeAddr(V, MakeQualifiers(E->getType()));
1692 }
Anders Carlssond95f9602009-09-12 16:16:49 +00001693 }
Chris Lattner28bcf1a2009-03-18 18:28:57 +00001694}
1695
Fariborz Jahaniane4d94ce2009-10-20 23:29:04 +00001696LValue CodeGenFunction::EmitNullInitializationLValue(
1697 const CXXZeroInitValueExpr *E) {
1698 QualType Ty = E->getType();
Daniel Dunbara7566f12010-02-09 02:48:28 +00001699 LValue LV = LValue::MakeAddr(CreateMemTemp(Ty), MakeQualifiers(Ty));
1700 EmitMemSetToZero(LV.getAddress(), Ty);
1701 return LV;
Fariborz Jahaniane4d94ce2009-10-20 23:29:04 +00001702}
1703
Chris Lattnere47e4402007-06-01 18:02:12 +00001704//===--------------------------------------------------------------------===//
1705// Expression Emission
1706//===--------------------------------------------------------------------===//
1707
Chris Lattner76ba8492007-08-20 22:37:10 +00001708
Anders Carlsson17490832009-12-24 20:40:36 +00001709RValue CodeGenFunction::EmitCallExpr(const CallExpr *E,
1710 ReturnValueSlot ReturnValue) {
Daniel Dunbarcdbb5e32009-02-20 18:06:48 +00001711 // Builtins never have block type.
Daniel Dunbarbb197e42009-01-09 16:50:52 +00001712 if (E->getCallee()->getType()->isBlockPointerType())
Anders Carlssonbfb36712009-12-24 21:13:40 +00001713 return EmitBlockCallExpr(E, ReturnValue);
Daniel Dunbarbb197e42009-01-09 16:50:52 +00001714
Anders Carlssone5fd6f22009-04-03 22:50:24 +00001715 if (const CXXMemberCallExpr *CE = dyn_cast<CXXMemberCallExpr>(E))
Anders Carlssonbfb36712009-12-24 21:13:40 +00001716 return EmitCXXMemberCallExpr(CE, ReturnValue);
Mike Stump4a3999f2009-09-09 13:00:44 +00001717
Daniel Dunbarcdbb5e32009-02-20 18:06:48 +00001718 const Decl *TargetDecl = 0;
Daniel Dunbar27032de2009-02-20 19:34:33 +00001719 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E->getCallee())) {
1720 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CE->getSubExpr())) {
1721 TargetDecl = DRE->getDecl();
1722 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(TargetDecl))
Douglas Gregor15fc9562009-09-12 00:22:50 +00001723 if (unsigned builtinID = FD->getBuiltinID())
Daniel Dunbar27032de2009-02-20 19:34:33 +00001724 return EmitBuiltinExpr(FD, builtinID, E);
Daniel Dunbarcdbb5e32009-02-20 18:06:48 +00001725 }
1726 }
1727
Chris Lattner4ca97c32009-06-13 00:26:38 +00001728 if (const CXXOperatorCallExpr *CE = dyn_cast<CXXOperatorCallExpr>(E))
Anders Carlsson4034a952009-05-27 04:18:27 +00001729 if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(TargetDecl))
Anders Carlssonbfb36712009-12-24 21:13:40 +00001730 return EmitCXXOperatorMemberCallExpr(CE, MD, ReturnValue);
Mike Stump4a3999f2009-09-09 13:00:44 +00001731
Eli Friedman8aaff692009-12-08 02:09:46 +00001732 if (isa<CXXPseudoDestructorExpr>(E->getCallee()->IgnoreParens())) {
Douglas Gregorad8a3362009-09-04 17:36:40 +00001733 // C++ [expr.pseudo]p1:
Mike Stump4a3999f2009-09-09 13:00:44 +00001734 // The result shall only be used as the operand for the function call
Douglas Gregorad8a3362009-09-04 17:36:40 +00001735 // operator (), and the result of such a call has type void. The only
1736 // effect is the evaluation of the postfix-expression before the dot or
1737 // arrow.
1738 EmitScalarExpr(E->getCallee());
1739 return RValue::get(0);
1740 }
Mike Stump4a3999f2009-09-09 13:00:44 +00001741
Chris Lattner2da04b32007-08-24 05:35:26 +00001742 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
Anders Carlsson17490832009-12-24 20:40:36 +00001743 return EmitCall(E->getCallee()->getType(), Callee, ReturnValue,
Anders Carlsson3a9463b2009-05-27 01:22:39 +00001744 E->arg_begin(), E->arg_end(), TargetDecl);
Chris Lattner9e47ead2007-08-31 04:44:06 +00001745}
1746
Daniel Dunbar8cde00a2008-09-04 03:20:13 +00001747LValue CodeGenFunction::EmitBinaryOperatorLValue(const BinaryOperator *E) {
Chris Lattnere541ea32009-05-12 21:28:12 +00001748 // Comma expressions just emit their LHS then their RHS as an l-value.
1749 if (E->getOpcode() == BinaryOperator::Comma) {
1750 EmitAnyExpr(E->getLHS());
Eli Friedman5445f6e2009-12-07 20:18:11 +00001751 EnsureInsertPoint();
Chris Lattnere541ea32009-05-12 21:28:12 +00001752 return EmitLValue(E->getRHS());
1753 }
Mike Stump4a3999f2009-09-09 13:00:44 +00001754
Fariborz Jahanian038374f2009-10-26 21:58:25 +00001755 if (E->getOpcode() == BinaryOperator::PtrMemD ||
1756 E->getOpcode() == BinaryOperator::PtrMemI)
Fariborz Jahanianffba6622009-10-22 22:57:31 +00001757 return EmitPointerToDataMemberBinaryExpr(E);
1758
Daniel Dunbar8cde00a2008-09-04 03:20:13 +00001759 // Can only get l-value for binary operator expressions which are a
1760 // simple assignment of aggregate type.
1761 if (E->getOpcode() != BinaryOperator::Assign)
1762 return EmitUnsupportedLValue(E, "binary l-value expression");
1763
Anders Carlsson0999aaf2009-10-19 18:28:22 +00001764 if (!hasAggregateLLVMType(E->getType())) {
1765 // Emit the LHS as an l-value.
1766 LValue LV = EmitLValue(E->getLHS());
1767
1768 llvm::Value *RHS = EmitScalarExpr(E->getRHS());
1769 EmitStoreOfScalar(RHS, LV.getAddress(), LV.isVolatileQualified(),
1770 E->getType());
1771 return LV;
1772 }
1773
Daniel Dunbard0bc7b92010-02-05 19:38:31 +00001774 return EmitAggExprToLValue(E);
Daniel Dunbar8cde00a2008-09-04 03:20:13 +00001775}
1776
Christopher Lambd91c3d42007-12-29 05:02:41 +00001777LValue CodeGenFunction::EmitCallExprLValue(const CallExpr *E) {
Christopher Lambd91c3d42007-12-29 05:02:41 +00001778 RValue RV = EmitCallExpr(E);
Anders Carlsson4ae70ff2009-05-27 01:45:47 +00001779
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001780 if (!RV.isScalar())
1781 return LValue::MakeAddr(RV.getAggregateAddr(),MakeQualifiers(E->getType()));
1782
1783 assert(E->getCallReturnType()->isReferenceType() &&
1784 "Can't have a scalar return unless the return type is a "
1785 "reference type!");
Mike Stump4a3999f2009-09-09 13:00:44 +00001786
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001787 return LValue::MakeAddr(RV.getScalarVal(), MakeQualifiers(E->getType()));
Christopher Lambd91c3d42007-12-29 05:02:41 +00001788}
1789
Daniel Dunbar8d9dc4a2009-02-11 20:59:32 +00001790LValue CodeGenFunction::EmitVAArgExprLValue(const VAArgExpr *E) {
1791 // FIXME: This shouldn't require another copy.
Daniel Dunbard0bc7b92010-02-05 19:38:31 +00001792 return EmitAggExprToLValue(E);
Daniel Dunbar8d9dc4a2009-02-11 20:59:32 +00001793}
1794
Anders Carlsson3be22e22009-05-30 23:23:33 +00001795LValue CodeGenFunction::EmitCXXConstructLValue(const CXXConstructExpr *E) {
Daniel Dunbara7566f12010-02-09 02:48:28 +00001796 llvm::Value *Temp = CreateMemTemp(E->getType(), "tmp");
Anders Carlsson3be22e22009-05-30 23:23:33 +00001797 EmitCXXConstructExpr(Temp, E);
John McCall8ccfcb52009-09-24 19:53:00 +00001798 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
Anders Carlsson3be22e22009-05-30 23:23:33 +00001799}
1800
Anders Carlssonfd2af0c2009-05-30 23:30:54 +00001801LValue
Mike Stumpc9b231c2009-11-15 08:09:41 +00001802CodeGenFunction::EmitCXXTypeidLValue(const CXXTypeidExpr *E) {
1803 llvm::Value *Temp = EmitCXXTypeidExpr(E);
1804 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
1805}
1806
1807LValue
Anders Carlssonfd2af0c2009-05-30 23:30:54 +00001808CodeGenFunction::EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr *E) {
1809 LValue LV = EmitLValue(E->getSubExpr());
Anders Carlsson8eb93e72009-05-31 00:34:10 +00001810 PushCXXTemporary(E->getTemporary(), LV.getAddress());
Anders Carlssonfd2af0c2009-05-30 23:30:54 +00001811 return LV;
1812}
1813
Daniel Dunbarc8317a42008-08-23 10:51:21 +00001814LValue CodeGenFunction::EmitObjCMessageExprLValue(const ObjCMessageExpr *E) {
1815 // Can only get l-value for message expression returning aggregate type
1816 RValue RV = EmitObjCMessageExpr(E);
1817 // FIXME: can this be volatile?
John McCall8ccfcb52009-09-24 19:53:00 +00001818 return LValue::MakeAddr(RV.getAggregateAddr(), MakeQualifiers(E->getType()));
Daniel Dunbarc8317a42008-08-23 10:51:21 +00001819}
1820
Daniel Dunbar722f4242009-04-22 05:08:15 +00001821llvm::Value *CodeGenFunction::EmitIvarOffset(const ObjCInterfaceDecl *Interface,
Daniel Dunbar1c64e5d2008-09-24 04:00:38 +00001822 const ObjCIvarDecl *Ivar) {
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001823 return CGM.getObjCRuntime().EmitIvarOffset(*this, Interface, Ivar);
Daniel Dunbar1c64e5d2008-09-24 04:00:38 +00001824}
1825
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00001826LValue CodeGenFunction::EmitLValueForIvar(QualType ObjectTy,
1827 llvm::Value *BaseValue,
Daniel Dunbar1c64e5d2008-09-24 04:00:38 +00001828 const ObjCIvarDecl *Ivar,
1829 unsigned CVRQualifiers) {
Chris Lattnerc4688d22009-04-17 17:44:48 +00001830 return CGM.getObjCRuntime().EmitObjCValueForIvar(*this, ObjectTy, BaseValue,
Daniel Dunbar9ebf9512009-04-21 01:19:28 +00001831 Ivar, CVRQualifiers);
Daniel Dunbar1c64e5d2008-09-24 04:00:38 +00001832}
1833
1834LValue CodeGenFunction::EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E) {
Anders Carlssonc13b85a2008-08-25 01:53:23 +00001835 // FIXME: A lot of the code below could be shared with EmitMemberExpr.
1836 llvm::Value *BaseValue = 0;
1837 const Expr *BaseExpr = E->getBase();
John McCall8ccfcb52009-09-24 19:53:00 +00001838 Qualifiers BaseQuals;
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00001839 QualType ObjectTy;
Anders Carlssonc13b85a2008-08-25 01:53:23 +00001840 if (E->isArrow()) {
1841 BaseValue = EmitScalarExpr(BaseExpr);
Steve Naroff7cae42b2009-07-10 23:34:53 +00001842 ObjectTy = BaseExpr->getType()->getPointeeType();
John McCall8ccfcb52009-09-24 19:53:00 +00001843 BaseQuals = ObjectTy.getQualifiers();
Anders Carlssonc13b85a2008-08-25 01:53:23 +00001844 } else {
1845 LValue BaseLV = EmitLValue(BaseExpr);
1846 // FIXME: this isn't right for bitfields.
1847 BaseValue = BaseLV.getAddress();
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00001848 ObjectTy = BaseExpr->getType();
John McCall8ccfcb52009-09-24 19:53:00 +00001849 BaseQuals = ObjectTy.getQualifiers();
Anders Carlssonc13b85a2008-08-25 01:53:23 +00001850 }
Daniel Dunbar1c64e5d2008-09-24 04:00:38 +00001851
Fariborz Jahaniande1d3242009-09-16 23:11:23 +00001852 LValue LV =
John McCall8ccfcb52009-09-24 19:53:00 +00001853 EmitLValueForIvar(ObjectTy, BaseValue, E->getDecl(),
1854 BaseQuals.getCVRQualifiers());
Fariborz Jahaniande1d3242009-09-16 23:11:23 +00001855 setObjCGCLValueClass(getContext(), E, LV);
1856 return LV;
Chris Lattner4bd55962008-03-30 23:03:07 +00001857}
1858
Mike Stump4a3999f2009-09-09 13:00:44 +00001859LValue
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +00001860CodeGenFunction::EmitObjCPropertyRefLValue(const ObjCPropertyRefExpr *E) {
Mike Stump4a3999f2009-09-09 13:00:44 +00001861 // This is a special l-value that just issues sends when we load or store
1862 // through it.
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +00001863 return LValue::MakePropertyRef(E, E->getType().getCVRQualifiers());
1864}
1865
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001866LValue CodeGenFunction::EmitObjCKVCRefLValue(
Fariborz Jahanian9a846652009-08-20 17:02:02 +00001867 const ObjCImplicitSetterGetterRefExpr *E) {
Mike Stump4a3999f2009-09-09 13:00:44 +00001868 // This is a special l-value that just issues sends when we load or store
1869 // through it.
Fariborz Jahanian9ac53512008-11-22 22:30:21 +00001870 return LValue::MakeKVCRef(E, E->getType().getCVRQualifiers());
1871}
1872
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001873LValue CodeGenFunction::EmitObjCSuperExprLValue(const ObjCSuperExpr *E) {
Douglas Gregor8ea1f532008-11-04 14:56:14 +00001874 return EmitUnsupportedLValue(E, "use of super");
1875}
1876
Chris Lattnera4185c52009-04-25 19:35:26 +00001877LValue CodeGenFunction::EmitStmtExprLValue(const StmtExpr *E) {
Chris Lattnera4185c52009-04-25 19:35:26 +00001878 // Can only get l-value for message expression returning aggregate type
1879 RValue RV = EmitAnyExprToTemp(E);
John McCall8ccfcb52009-09-24 19:53:00 +00001880 return LValue::MakeAddr(RV.getAggregateAddr(), MakeQualifiers(E->getType()));
Chris Lattnera4185c52009-04-25 19:35:26 +00001881}
1882
Anders Carlsson0435ed52009-12-24 19:08:58 +00001883RValue CodeGenFunction::EmitCall(QualType CalleeType, llvm::Value *Callee,
Anders Carlsson17490832009-12-24 20:40:36 +00001884 ReturnValueSlot ReturnValue,
Anders Carlsson3a9463b2009-05-27 01:22:39 +00001885 CallExpr::const_arg_iterator ArgBeg,
1886 CallExpr::const_arg_iterator ArgEnd,
1887 const Decl *TargetDecl) {
Mike Stump4a3999f2009-09-09 13:00:44 +00001888 // Get the actual function type. The callee type will always be a pointer to
1889 // function type or a block pointer type.
1890 assert(CalleeType->isFunctionPointerType() &&
Anders Carlssond8db8532009-04-07 18:53:02 +00001891 "Call must have function pointer type!");
1892
John McCall6fd4c232009-10-23 08:22:42 +00001893 CalleeType = getContext().getCanonicalType(CalleeType);
1894
John McCallab26cfa2010-02-05 21:31:56 +00001895 const FunctionType *FnType
1896 = cast<FunctionType>(cast<PointerType>(CalleeType)->getPointeeType());
1897 QualType ResultType = FnType->getResultType();
Daniel Dunbarc722b852008-08-30 03:02:31 +00001898
1899 CallArgList Args;
John McCall6fd4c232009-10-23 08:22:42 +00001900 EmitCallArgs(Args, dyn_cast<FunctionProtoType>(FnType), ArgBeg, ArgEnd);
Daniel Dunbarc722b852008-08-30 03:02:31 +00001901
John McCallab26cfa2010-02-05 21:31:56 +00001902 return EmitCall(CGM.getTypes().getFunctionInfo(Args, FnType),
Anders Carlsson17490832009-12-24 20:40:36 +00001903 Callee, ReturnValue, Args, TargetDecl);
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001904}
Fariborz Jahanianffba6622009-10-22 22:57:31 +00001905
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001906LValue CodeGenFunction::
1907EmitPointerToDataMemberBinaryExpr(const BinaryOperator *E) {
Eli Friedman928a5672009-11-18 05:01:17 +00001908 llvm::Value *BaseV;
Fariborz Jahanian038374f2009-10-26 21:58:25 +00001909 if (E->getOpcode() == BinaryOperator::PtrMemI)
Eli Friedman928a5672009-11-18 05:01:17 +00001910 BaseV = EmitScalarExpr(E->getLHS());
1911 else
1912 BaseV = EmitLValue(E->getLHS()).getAddress();
Fariborz Jahanianffba6622009-10-22 22:57:31 +00001913 const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(getLLVMContext());
1914 BaseV = Builder.CreateBitCast(BaseV, i8Ty);
Eli Friedman928a5672009-11-18 05:01:17 +00001915 llvm::Value *OffsetV = EmitScalarExpr(E->getRHS());
Fariborz Jahanianffba6622009-10-22 22:57:31 +00001916 llvm::Value *AddV = Builder.CreateInBoundsGEP(BaseV, OffsetV, "add.ptr");
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001917
Fariborz Jahanianffba6622009-10-22 22:57:31 +00001918 QualType Ty = E->getRHS()->getType();
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001919 Ty = Ty->getAs<MemberPointerType>()->getPointeeType();
1920
1921 const llvm::Type *PType = ConvertType(getContext().getPointerType(Ty));
Fariborz Jahanianffba6622009-10-22 22:57:31 +00001922 AddV = Builder.CreateBitCast(AddV, PType);
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001923 return LValue::MakeAddr(AddV, MakeQualifiers(Ty));
Fariborz Jahanianffba6622009-10-22 22:57:31 +00001924}
1925