blob: 7809972630be814356b230534f621b9db8622229 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- CGExpr.cpp - Emit LLVM Code from Expressions ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit Expr nodes as LLVM code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenFunction.h"
15#include "CodeGenModule.h"
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000016#include "CGCall.h"
Daniel Dunbar198bcb42010-03-31 01:09:11 +000017#include "CGRecordLayout.h"
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +000018#include "CGObjCRuntime.h"
Daniel Dunbarde7fb842008-08-11 05:00:27 +000019#include "clang/AST/ASTContext.h"
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000020#include "clang/AST/DeclObjC.h"
Mike Stump15037ca2009-12-15 00:35:12 +000021#include "llvm/Intrinsics.h"
Mike Stump41513442009-12-15 00:59:40 +000022#include "clang/CodeGen/CodeGenOptions.h"
Eli Friedman316bb1b2008-05-17 20:03:47 +000023#include "llvm/Target/TargetData.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000024using namespace clang;
25using namespace CodeGen;
26
27//===--------------------------------------------------------------------===//
28// Miscellaneous Helper Methods
29//===--------------------------------------------------------------------===//
30
31/// CreateTempAlloca - This creates a alloca and inserts it into the entry
32/// block.
33llvm::AllocaInst *CodeGenFunction::CreateTempAlloca(const llvm::Type *Ty,
Daniel Dunbar259e9cc2009-10-19 01:21:05 +000034 const llvm::Twine &Name) {
Chris Lattnerf1466842009-03-22 00:24:14 +000035 if (!Builder.isNamePreserving())
Daniel Dunbar259e9cc2009-10-19 01:21:05 +000036 return new llvm::AllocaInst(Ty, 0, "", AllocaInsertPt);
Devang Pateld35e2e02009-10-12 22:29:02 +000037 return new llvm::AllocaInst(Ty, 0, Name, AllocaInsertPt);
Reid Spencer5f016e22007-07-11 17:01:13 +000038}
39
Daniel Dunbar9bd4da22010-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 Dunbar195337d2010-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
Reid Spencer5f016e22007-07-11 17:01:13 +000058/// EvaluateExprAsBool - Perform the usual unary conversions on the specified
59/// expression and compare the result against zero, returning an Int1Ty value.
60llvm::Value *CodeGenFunction::EvaluateExprAsBool(const Expr *E) {
Chris Lattner9069fa22007-08-26 16:46:58 +000061 QualType BoolTy = getContext().BoolTy;
Eli Friedman3a173702009-12-11 09:26:29 +000062 if (E->getType()->isMemberFunctionPointerType()) {
Daniel Dunbar18aba0d2010-02-05 19:38:31 +000063 LValue LV = EmitAggExprToLValue(E);
Eli Friedman3a173702009-12-11 09:26:29 +000064
65 // Get the pointer.
Daniel Dunbar18aba0d2010-02-05 19:38:31 +000066 llvm::Value *FuncPtr = Builder.CreateStructGEP(LV.getAddress(), 0,
67 "src.ptr");
Eli Friedman3a173702009-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 Lattner9b2dc282008-04-04 16:54:41 +000077 if (!E->getType()->isAnyComplexType())
Chris Lattner9069fa22007-08-26 16:46:58 +000078 return EmitScalarConversion(EmitScalarExpr(E), E->getType(), BoolTy);
Reid Spencer5f016e22007-07-11 17:01:13 +000079
Chris Lattner9069fa22007-08-26 16:46:58 +000080 return EmitComplexToScalarConversion(EmitComplexExpr(E), E->getType(),BoolTy);
Reid Spencer5f016e22007-07-11 17:01:13 +000081}
82
Chris Lattner9b655512007-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 Stumpdb52dcd2009-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 Carlsson14c5cbf2009-08-16 07:36:22 +000088 bool IsAggLocVolatile, bool IgnoreResult,
89 bool IsInitializer) {
Chris Lattner9b655512007-08-31 22:49:20 +000090 if (!hasAggregateLLVMType(E->getType()))
Mike Stump7f79f9b2009-05-29 15:46:01 +000091 return RValue::get(EmitScalarExpr(E, IgnoreResult));
Chris Lattner9b2dc282008-04-04 16:54:41 +000092 else if (E->getType()->isAnyComplexType())
Mike Stump7f79f9b2009-05-29 15:46:01 +000093 return RValue::getComplex(EmitComplexExpr(E, false, false,
94 IgnoreResult, IgnoreResult));
Mike Stumpdb52dcd2009-09-09 13:00:44 +000095
Anders Carlsson14c5cbf2009-08-16 07:36:22 +000096 EmitAggExpr(E, AggLoc, IsAggLocVolatile, IgnoreResult, IsInitializer);
97 return RValue::getAggregate(AggLoc, IsAggLocVolatile);
Chris Lattner9b655512007-08-31 22:49:20 +000098}
99
Mike Stumpdb52dcd2009-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 Carlsson14c5cbf2009-08-16 07:36:22 +0000103 bool IsAggLocVolatile,
104 bool IsInitializer) {
105 llvm::Value *AggLoc = 0;
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000106
107 if (hasAggregateLLVMType(E->getType()) &&
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000108 !E->getType()->isAnyComplexType())
John McCall63efd332010-02-15 01:23:36 +0000109 AggLoc = CreateMemTemp(E->getType(), "agg.tmp");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000110 return EmitAnyExpr(E, AggLoc, IsAggLocVolatile, /*IgnoreResult=*/false,
Anders Carlsson14c5cbf2009-08-16 07:36:22 +0000111 IsInitializer);
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000112}
113
Anders Carlsson4029ca72009-05-20 00:24:07 +0000114RValue CodeGenFunction::EmitReferenceBindingToExpr(const Expr* E,
Anders Carlsson14c5cbf2009-08-16 07:36:22 +0000115 bool IsInitializer) {
Anders Carlssone1b7ea12009-10-18 23:09:21 +0000116 bool ShouldDestroyTemporaries = false;
117 unsigned OldNumLiveTemporaries = 0;
Eli Friedman27a9b722009-12-19 00:20:10 +0000118
119 if (const CXXDefaultArgExpr *DAE = dyn_cast<CXXDefaultArgExpr>(E))
120 E = DAE->getExpr();
121
Anders Carlssonb3f74422009-10-15 00:51:46 +0000122 if (const CXXExprWithTemporaries *TE = dyn_cast<CXXExprWithTemporaries>(E)) {
Anders Carlsson0ece4912009-12-15 20:51:39 +0000123 ShouldDestroyTemporaries = true;
124
Chris Lattnereb99b012009-10-28 17:39:19 +0000125 // Keep track of the current cleanup stack depth.
Anders Carlsson0ece4912009-12-15 20:51:39 +0000126 OldNumLiveTemporaries = LiveTemporaries.size();
Anders Carlssonb3f74422009-10-15 00:51:46 +0000127
Anders Carlssone1b7ea12009-10-18 23:09:21 +0000128 E = TE->getSubExpr();
Anders Carlssonb3f74422009-10-15 00:51:46 +0000129 }
130
Eli Friedman5df0d422009-05-20 02:31:19 +0000131 RValue Val;
132 if (E->isLvalue(getContext()) == Expr::LV_Valid) {
Anders Carlsson4bbab922009-05-20 00:36:58 +0000133 // Emit the expr as an lvalue.
134 LValue LV = EmitLValue(E);
Anders Carlsson0dc73662010-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 Friedman5df0d422009-05-20 02:31:19 +0000142 return RValue::get(LV.getAddress());
Anders Carlsson0dc73662010-02-04 17:32:58 +0000143 }
144
Eli Friedman5df0d422009-05-20 02:31:19 +0000145 Val = EmitLoadOfLValue(LV, E->getType());
Anders Carlssone1b7ea12009-10-18 23:09:21 +0000146
147 if (ShouldDestroyTemporaries) {
148 // Pop temporaries.
149 while (LiveTemporaries.size() > OldNumLiveTemporaries)
150 PopCXXTemporary();
151 }
Eli Friedman5df0d422009-05-20 02:31:19 +0000152 } else {
Anders Carlssonb3f74422009-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 Carlsson14c5cbf2009-08-16 07:36:22 +0000168 Val = EmitAnyExprToTemp(E, /*IsAggLocVolatile=*/false,
169 IsInitializer);
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000170
Anders Carlssone1b7ea12009-10-18 23:09:21 +0000171 if (ShouldDestroyTemporaries) {
172 // Pop temporaries.
173 while (LiveTemporaries.size() > OldNumLiveTemporaries)
174 PopCXXTemporary();
175 }
176
Anders Carlsson2da76932009-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 Stumpdb52dcd2009-09-09 13:00:44 +0000182 const CXXDestructorDecl *Dtor =
Anders Carlsson2da76932009-08-16 17:54:29 +0000183 ClassDecl->getDestructor(getContext());
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000184
Mike Stumpd88ea562009-12-09 03:35:49 +0000185 {
Anders Carlsson6ec687d2009-12-11 01:00:09 +0000186 DelayedCleanupBlock Scope(*this);
Mike Stumpd88ea562009-12-09 03:35:49 +0000187 EmitCXXDestructorCall(Dtor, Dtor_Complete,
188 Val.getAggregateAddr());
Anders Carlsson6ec687d2009-12-11 01:00:09 +0000189
190 // Make sure to jump to the exit block.
191 EmitBranch(Scope.getCleanupExitBlock());
Mike Stumpd88ea562009-12-09 03:35:49 +0000192 }
193 if (Exceptions) {
194 EHCleanupBlock Cleanup(*this);
195 EmitCXXDestructorCall(Dtor, Dtor_Complete,
196 Val.getAggregateAddr());
197 }
Anders Carlsson2da76932009-08-16 17:54:29 +0000198 }
Anders Carlsson8478ce62009-08-16 17:50:25 +0000199 }
200 }
201 }
Anders Carlssonb3f74422009-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 Carlssonb3f74422009-10-15 00:51:46 +0000206 llvm::Value *Base =
Anders Carlssona3697c92009-11-23 17:57:54 +0000207 GetAddressOfBaseClass(Derived, DerivedClassDecl, BaseClassDecl,
208 /*NullCheckValue=*/false);
Anders Carlssonb3f74422009-10-15 00:51:46 +0000209 return RValue::get(Base);
210 }
Anders Carlsson4bbab922009-05-20 00:36:58 +0000211 }
Eli Friedman5df0d422009-05-20 02:31:19 +0000212
213 if (Val.isAggregate()) {
214 Val = RValue::get(Val.getAggregateAddr());
215 } else {
Anders Carlsson7cd3a642009-05-20 01:35:03 +0000216 // Create a temporary variable that we can bind the reference to.
Daniel Dunbar195337d2010-02-09 02:48:28 +0000217 llvm::Value *Temp = CreateMemTemp(E->getType(), "reftmp");
Eli Friedman5df0d422009-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 Carlssone04d1c72009-05-20 01:03:17 +0000223 }
Eli Friedman5df0d422009-05-20 02:31:19 +0000224
225 return Val;
Anders Carlsson4029ca72009-05-20 00:24:07 +0000226}
227
228
Mike Stumpdb52dcd2009-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 Gohman4f8d1232008-05-22 00:50:06 +0000232 const llvm::Constant *Elts) {
233 if (isa<llvm::ConstantAggregateZero>(Elts))
234 return 0;
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000235
Dan Gohman4f8d1232008-05-22 00:50:06 +0000236 return cast<llvm::ConstantInt>(Elts->getOperand(Idx))->getZExtValue();
237}
238
Mike Stumpb14e62d2009-12-16 02:57:00 +0000239void CodeGenFunction::EmitCheck(llvm::Value *Address, unsigned Size) {
240 if (!CatchUndefined)
241 return;
242
Chris Lattnerc24b9c42010-04-10 18:34:14 +0000243 const llvm::Type *Size_tTy
Mike Stumpb14e62d2009-12-16 02:57:00 +0000244 = llvm::IntegerType::get(VMContext, LLVMPointerWidth);
245 Address = Builder.CreateBitCast(Address, PtrToInt8Ty);
246
Chris Lattnerc24b9c42010-04-10 18:34:14 +0000247 llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::objectsize, &Size_tTy, 1);
248 const llvm::IntegerType *Int1Ty = llvm::IntegerType::get(VMContext, 1);
249
Mike Stumpb14e62d2009-12-16 02:57:00 +0000250 // In time, people may want to control this and use a 1 here.
Chris Lattnerc24b9c42010-04-10 18:34:14 +0000251 llvm::Value *Arg = llvm::ConstantInt::get(Int1Ty, 0);
Mike Stumpb14e62d2009-12-16 02:57:00 +0000252 llvm::Value *C = Builder.CreateCall2(F, Address, Arg);
253 llvm::BasicBlock *Cont = createBasicBlock();
254 llvm::BasicBlock *Check = createBasicBlock();
255 llvm::Value *NegativeOne = llvm::ConstantInt::get(Size_tTy, -1ULL);
256 Builder.CreateCondBr(Builder.CreateICmpEQ(C, NegativeOne), Cont, Check);
257
258 EmitBlock(Check);
259 Builder.CreateCondBr(Builder.CreateICmpUGE(C,
260 llvm::ConstantInt::get(Size_tTy, Size)),
261 Cont, getTrapBB());
262 EmitBlock(Cont);
263}
Chris Lattner9b655512007-08-31 22:49:20 +0000264
Chris Lattnerdd36d322010-01-09 21:40:03 +0000265
266llvm::Value *CodeGenFunction::
267EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV,
268 bool isInc, bool isPre) {
269 QualType ValTy = E->getSubExpr()->getType();
270 llvm::Value *InVal = EmitLoadOfLValue(LV, ValTy).getScalarVal();
271
272 int AmountVal = isInc ? 1 : -1;
273
274 if (ValTy->isPointerType() &&
275 ValTy->getAs<PointerType>()->isVariableArrayType()) {
276 // The amount of the addition/subtraction needs to account for the VLA size
277 ErrorUnsupported(E, "VLA pointer inc/dec");
278 }
279
280 llvm::Value *NextVal;
281 if (const llvm::PointerType *PT =
282 dyn_cast<llvm::PointerType>(InVal->getType())) {
283 llvm::Constant *Inc =
284 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), AmountVal);
285 if (!isa<llvm::FunctionType>(PT->getElementType())) {
286 QualType PTEE = ValTy->getPointeeType();
287 if (const ObjCInterfaceType *OIT =
288 dyn_cast<ObjCInterfaceType>(PTEE)) {
289 // Handle interface types, which are not represented with a concrete
290 // type.
291 int size = getContext().getTypeSize(OIT) / 8;
292 if (!isInc)
293 size = -size;
294 Inc = llvm::ConstantInt::get(Inc->getType(), size);
295 const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(VMContext);
296 InVal = Builder.CreateBitCast(InVal, i8Ty);
297 NextVal = Builder.CreateGEP(InVal, Inc, "add.ptr");
298 llvm::Value *lhs = LV.getAddress();
299 lhs = Builder.CreateBitCast(lhs, llvm::PointerType::getUnqual(i8Ty));
300 LV = LValue::MakeAddr(lhs, MakeQualifiers(ValTy));
301 } else
302 NextVal = Builder.CreateInBoundsGEP(InVal, Inc, "ptrincdec");
303 } else {
304 const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(VMContext);
305 NextVal = Builder.CreateBitCast(InVal, i8Ty, "tmp");
306 NextVal = Builder.CreateGEP(NextVal, Inc, "ptrincdec");
307 NextVal = Builder.CreateBitCast(NextVal, InVal->getType());
308 }
309 } else if (InVal->getType() == llvm::Type::getInt1Ty(VMContext) && isInc) {
310 // Bool++ is an interesting case, due to promotion rules, we get:
311 // Bool++ -> Bool = Bool+1 -> Bool = (int)Bool+1 ->
312 // Bool = ((int)Bool+1) != 0
313 // An interesting aspect of this is that increment is always true.
314 // Decrement does not have this property.
315 NextVal = llvm::ConstantInt::getTrue(VMContext);
316 } else if (isa<llvm::IntegerType>(InVal->getType())) {
317 NextVal = llvm::ConstantInt::get(InVal->getType(), AmountVal);
318
319 // Signed integer overflow is undefined behavior.
320 if (ValTy->isSignedIntegerType())
321 NextVal = Builder.CreateNSWAdd(InVal, NextVal, isInc ? "inc" : "dec");
322 else
323 NextVal = Builder.CreateAdd(InVal, NextVal, isInc ? "inc" : "dec");
324 } else {
325 // Add the inc/dec to the real part.
326 if (InVal->getType()->isFloatTy())
327 NextVal =
328 llvm::ConstantFP::get(VMContext,
329 llvm::APFloat(static_cast<float>(AmountVal)));
330 else if (InVal->getType()->isDoubleTy())
331 NextVal =
332 llvm::ConstantFP::get(VMContext,
333 llvm::APFloat(static_cast<double>(AmountVal)));
334 else {
335 llvm::APFloat F(static_cast<float>(AmountVal));
336 bool ignored;
337 F.convert(Target.getLongDoubleFormat(), llvm::APFloat::rmTowardZero,
338 &ignored);
339 NextVal = llvm::ConstantFP::get(VMContext, F);
340 }
341 NextVal = Builder.CreateFAdd(InVal, NextVal, isInc ? "inc" : "dec");
342 }
343
344 // Store the updated result through the lvalue.
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000345 if (LV.isBitField())
Chris Lattnerdd36d322010-01-09 21:40:03 +0000346 EmitStoreThroughBitfieldLValue(RValue::get(NextVal), LV, ValTy, &NextVal);
347 else
348 EmitStoreThroughLValue(RValue::get(NextVal), LV, ValTy);
349
350 // If this is a postinc, return the value read from memory, otherwise use the
351 // updated value.
352 return isPre ? NextVal : InVal;
353}
354
355
356CodeGenFunction::ComplexPairTy CodeGenFunction::
357EmitComplexPrePostIncDec(const UnaryOperator *E, LValue LV,
358 bool isInc, bool isPre) {
359 ComplexPairTy InVal = LoadComplexFromAddr(LV.getAddress(),
360 LV.isVolatileQualified());
361
362 llvm::Value *NextVal;
363 if (isa<llvm::IntegerType>(InVal.first->getType())) {
364 uint64_t AmountVal = isInc ? 1 : -1;
365 NextVal = llvm::ConstantInt::get(InVal.first->getType(), AmountVal, true);
366
367 // Add the inc/dec to the real part.
368 NextVal = Builder.CreateAdd(InVal.first, NextVal, isInc ? "inc" : "dec");
369 } else {
370 QualType ElemTy = E->getType()->getAs<ComplexType>()->getElementType();
371 llvm::APFloat FVal(getContext().getFloatTypeSemantics(ElemTy), 1);
372 if (!isInc)
373 FVal.changeSign();
374 NextVal = llvm::ConstantFP::get(getLLVMContext(), FVal);
375
376 // Add the inc/dec to the real part.
377 NextVal = Builder.CreateFAdd(InVal.first, NextVal, isInc ? "inc" : "dec");
378 }
379
380 ComplexPairTy IncVal(NextVal, InVal.second);
381
382 // Store the updated result through the lvalue.
383 StoreComplexToAddr(IncVal, LV.getAddress(), LV.isVolatileQualified());
384
385 // If this is a postinc, return the value read from memory, otherwise use the
386 // updated value.
387 return isPre ? IncVal : InVal;
388}
389
390
Reid Spencer5f016e22007-07-11 17:01:13 +0000391//===----------------------------------------------------------------------===//
392// LValue Expression Emission
393//===----------------------------------------------------------------------===//
394
Daniel Dunbar13e81732009-02-05 07:09:07 +0000395RValue CodeGenFunction::GetUndefRValue(QualType Ty) {
Chris Lattnereb99b012009-10-28 17:39:19 +0000396 if (Ty->isVoidType())
Daniel Dunbar13e81732009-02-05 07:09:07 +0000397 return RValue::get(0);
Chris Lattnereb99b012009-10-28 17:39:19 +0000398
399 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
Daniel Dunbar8fa73ed2009-01-09 20:09:28 +0000400 const llvm::Type *EltTy = ConvertType(CTy->getElementType());
Owen Anderson03e20502009-07-30 23:11:26 +0000401 llvm::Value *U = llvm::UndefValue::get(EltTy);
Daniel Dunbar8fa73ed2009-01-09 20:09:28 +0000402 return RValue::getComplex(std::make_pair(U, U));
Chris Lattnereb99b012009-10-28 17:39:19 +0000403 }
404
405 if (hasAggregateLLVMType(Ty)) {
Owen Anderson96e0fc72009-07-29 22:16:19 +0000406 const llvm::Type *LTy = llvm::PointerType::getUnqual(ConvertType(Ty));
Owen Anderson03e20502009-07-30 23:11:26 +0000407 return RValue::getAggregate(llvm::UndefValue::get(LTy));
Daniel Dunbar8fa73ed2009-01-09 20:09:28 +0000408 }
Chris Lattnereb99b012009-10-28 17:39:19 +0000409
410 return RValue::get(llvm::UndefValue::get(ConvertType(Ty)));
Daniel Dunbarce1d38b2009-01-09 16:50:52 +0000411}
412
Daniel Dunbar13e81732009-02-05 07:09:07 +0000413RValue CodeGenFunction::EmitUnsupportedRValue(const Expr *E,
414 const char *Name) {
415 ErrorUnsupported(E, Name);
416 return GetUndefRValue(E->getType());
417}
418
Daniel Dunbar6ba82a42008-08-25 20:45:57 +0000419LValue CodeGenFunction::EmitUnsupportedLValue(const Expr *E,
420 const char *Name) {
421 ErrorUnsupported(E, Name);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000422 llvm::Type *Ty = llvm::PointerType::getUnqual(ConvertType(E->getType()));
Owen Anderson03e20502009-07-30 23:11:26 +0000423 return LValue::MakeAddr(llvm::UndefValue::get(Ty),
John McCall0953e762009-09-24 19:53:00 +0000424 MakeQualifiers(E->getType()));
Daniel Dunbar6ba82a42008-08-25 20:45:57 +0000425}
426
Mike Stumpb14e62d2009-12-16 02:57:00 +0000427LValue CodeGenFunction::EmitCheckedLValue(const Expr *E) {
428 LValue LV = EmitLValue(E);
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000429 if (!isa<DeclRefExpr>(E) && !LV.isBitField() && LV.isSimple())
Mike Stumpb14e62d2009-12-16 02:57:00 +0000430 EmitCheck(LV.getAddress(), getContext().getTypeSize(E->getType()) / 8);
431 return LV;
432}
433
Reid Spencer5f016e22007-07-11 17:01:13 +0000434/// EmitLValue - Emit code to compute a designator that specifies the location
435/// of the expression.
436///
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000437/// This can return one of two things: a simple address or a bitfield reference.
438/// In either case, the LLVM Value* in the LValue structure is guaranteed to be
439/// an LLVM pointer type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000440///
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000441/// If this returns a bitfield reference, nothing about the pointee type of the
442/// LLVM value is known: For example, it may not be a pointer to an integer.
Reid Spencer5f016e22007-07-11 17:01:13 +0000443///
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000444/// If this returns a normal address, and if the lvalue's C type is fixed size,
445/// this method guarantees that the returned pointer type will point to an LLVM
446/// type of the same size of the lvalue's type. If the lvalue has a variable
447/// length type, this is not possible.
Reid Spencer5f016e22007-07-11 17:01:13 +0000448///
449LValue CodeGenFunction::EmitLValue(const Expr *E) {
450 switch (E->getStmtClass()) {
Daniel Dunbar6ba82a42008-08-25 20:45:57 +0000451 default: return EmitUnsupportedLValue(E, "l-value expression");
Reid Spencer5f016e22007-07-11 17:01:13 +0000452
Fariborz Jahanian820bca42009-12-09 23:35:29 +0000453 case Expr::ObjCIsaExprClass:
454 return EmitObjCIsaExpr(cast<ObjCIsaExpr>(E));
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000455 case Expr::BinaryOperatorClass:
Daniel Dunbar80e62c22008-09-04 03:20:13 +0000456 return EmitBinaryOperatorLValue(cast<BinaryOperator>(E));
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000457 case Expr::CallExprClass:
Anders Carlssonfaf86642009-09-01 21:18:52 +0000458 case Expr::CXXMemberCallExprClass:
Douglas Gregorb4609802008-11-14 16:09:21 +0000459 case Expr::CXXOperatorCallExprClass:
460 return EmitCallExprLValue(cast<CallExpr>(E));
Daniel Dunbar5b5c9ef2009-02-11 20:59:32 +0000461 case Expr::VAArgExprClass:
462 return EmitVAArgExprLValue(cast<VAArgExpr>(E));
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000463 case Expr::DeclRefExprClass:
Douglas Gregor1a49af92009-01-06 05:10:23 +0000464 return EmitDeclRefLValue(cast<DeclRefExpr>(E));
Reid Spencer5f016e22007-07-11 17:01:13 +0000465 case Expr::ParenExprClass:return EmitLValue(cast<ParenExpr>(E)->getSubExpr());
Chris Lattnerd9f69102008-08-10 01:53:14 +0000466 case Expr::PredefinedExprClass:
467 return EmitPredefinedLValue(cast<PredefinedExpr>(E));
Reid Spencer5f016e22007-07-11 17:01:13 +0000468 case Expr::StringLiteralClass:
469 return EmitStringLiteralLValue(cast<StringLiteral>(E));
Chris Lattnereaf2bb82009-02-24 22:18:39 +0000470 case Expr::ObjCEncodeExprClass:
471 return EmitObjCEncodeExprLValue(cast<ObjCEncodeExpr>(E));
Chris Lattner391d77a2008-03-30 23:03:07 +0000472
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000473 case Expr::BlockDeclRefExprClass:
Mike Stumpa99038c2009-02-28 09:07:16 +0000474 return EmitBlockDeclRefLValue(cast<BlockDeclRefExpr>(E));
475
Anders Carlssonb58d0172009-05-30 23:23:33 +0000476 case Expr::CXXTemporaryObjectExprClass:
477 case Expr::CXXConstructExprClass:
Anders Carlssone61c9e82009-05-30 23:30:54 +0000478 return EmitCXXConstructLValue(cast<CXXConstructExpr>(E));
479 case Expr::CXXBindTemporaryExprClass:
480 return EmitCXXBindTemporaryLValue(cast<CXXBindTemporaryExpr>(E));
Anders Carlssonb9ea0b52009-09-14 01:10:45 +0000481 case Expr::CXXExprWithTemporariesClass:
482 return EmitCXXExprWithTemporariesLValue(cast<CXXExprWithTemporaries>(E));
Anders Carlsson370e5382009-11-14 01:51:50 +0000483 case Expr::CXXZeroInitValueExprClass:
484 return EmitNullInitializationLValue(cast<CXXZeroInitValueExpr>(E));
485 case Expr::CXXDefaultArgExprClass:
486 return EmitLValue(cast<CXXDefaultArgExpr>(E)->getExpr());
Mike Stumpc2e84ae2009-11-15 08:09:41 +0000487 case Expr::CXXTypeidExprClass:
488 return EmitCXXTypeidLValue(cast<CXXTypeidExpr>(E));
Anders Carlssone61c9e82009-05-30 23:30:54 +0000489
Daniel Dunbar0a04d772008-08-23 10:51:21 +0000490 case Expr::ObjCMessageExprClass:
491 return EmitObjCMessageExprLValue(cast<ObjCMessageExpr>(E));
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000492 case Expr::ObjCIvarRefExprClass:
Chris Lattner391d77a2008-03-30 23:03:07 +0000493 return EmitObjCIvarRefLValue(cast<ObjCIvarRefExpr>(E));
Daniel Dunbar6ba82a42008-08-25 20:45:57 +0000494 case Expr::ObjCPropertyRefExprClass:
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000495 return EmitObjCPropertyRefLValue(cast<ObjCPropertyRefExpr>(E));
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000496 case Expr::ObjCImplicitSetterGetterRefExprClass:
497 return EmitObjCKVCRefLValue(cast<ObjCImplicitSetterGetterRefExpr>(E));
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000498 case Expr::ObjCSuperExprClass:
Chris Lattner65459942009-04-25 19:35:26 +0000499 return EmitObjCSuperExprLValue(cast<ObjCSuperExpr>(E));
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000500
Chris Lattner65459942009-04-25 19:35:26 +0000501 case Expr::StmtExprClass:
502 return EmitStmtExprLValue(cast<StmtExpr>(E));
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000503 case Expr::UnaryOperatorClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000504 return EmitUnaryOpLValue(cast<UnaryOperator>(E));
505 case Expr::ArraySubscriptExprClass:
506 return EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E));
Nate Begeman213541a2008-04-18 23:10:10 +0000507 case Expr::ExtVectorElementExprClass:
508 return EmitExtVectorElementExpr(cast<ExtVectorElementExpr>(E));
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000509 case Expr::MemberExprClass:
Douglas Gregorbd4c4ae2009-08-26 22:36:53 +0000510 return EmitMemberExpr(cast<MemberExpr>(E));
Eli Friedman06e863f2008-05-13 23:18:27 +0000511 case Expr::CompoundLiteralExprClass:
512 return EmitCompoundLiteralLValue(cast<CompoundLiteralExpr>(E));
Daniel Dunbar90345582009-03-24 02:38:23 +0000513 case Expr::ConditionalOperatorClass:
Anders Carlsson6fcec8b2009-09-15 16:35:24 +0000514 return EmitConditionalOperatorLValue(cast<ConditionalOperator>(E));
Chris Lattner670a62c2008-12-12 05:35:08 +0000515 case Expr::ChooseExprClass:
Eli Friedman79769322009-03-04 05:52:32 +0000516 return EmitLValue(cast<ChooseExpr>(E)->getChosenSubExpr(getContext()));
Chris Lattnerc3953a62009-03-18 04:02:57 +0000517 case Expr::ImplicitCastExprClass:
518 case Expr::CStyleCastExprClass:
519 case Expr::CXXFunctionalCastExprClass:
520 case Expr::CXXStaticCastExprClass:
521 case Expr::CXXDynamicCastExprClass:
522 case Expr::CXXReinterpretCastExprClass:
523 case Expr::CXXConstCastExprClass:
Chris Lattner75dfeda2009-03-18 18:28:57 +0000524 return EmitCastLValue(cast<CastExpr>(E));
Reid Spencer5f016e22007-07-11 17:01:13 +0000525 }
526}
527
Daniel Dunbar9d9cc872009-02-10 00:57:50 +0000528llvm::Value *CodeGenFunction::EmitLoadOfScalar(llvm::Value *Addr, bool Volatile,
529 QualType Ty) {
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000530 llvm::LoadInst *Load = Builder.CreateLoad(Addr, "tmp");
531 if (Volatile)
532 Load->setVolatile(true);
Daniel Dunbar9d9cc872009-02-10 00:57:50 +0000533
Anders Carlsson3bb423b2009-05-19 19:36:19 +0000534 // Bool can have different representation in memory than in registers.
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000535 llvm::Value *V = Load;
Daniel Dunbar9d9cc872009-02-10 00:57:50 +0000536 if (Ty->isBooleanType())
Owen Anderson0032b272009-08-13 21:57:51 +0000537 if (V->getType() != llvm::Type::getInt1Ty(VMContext))
538 V = Builder.CreateTrunc(V, llvm::Type::getInt1Ty(VMContext), "tobool");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000539
Daniel Dunbar9d9cc872009-02-10 00:57:50 +0000540 return V;
541}
542
543void CodeGenFunction::EmitStoreOfScalar(llvm::Value *Value, llvm::Value *Addr,
Anders Carlssonb4aa4662009-05-19 18:50:41 +0000544 bool Volatile, QualType Ty) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000545
Anders Carlsson3bb423b2009-05-19 19:36:19 +0000546 if (Ty->isBooleanType()) {
547 // Bool can have different representation in memory than in registers.
Anders Carlsson3bb423b2009-05-19 19:36:19 +0000548 const llvm::PointerType *DstPtr = cast<llvm::PointerType>(Addr->getType());
Eli Friedman2b06d342009-12-01 22:31:51 +0000549 Value = Builder.CreateIntCast(Value, DstPtr->getElementType(), false);
Daniel Dunbar9d9cc872009-02-10 00:57:50 +0000550 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000551 Builder.CreateStore(Value, Addr, Volatile);
Daniel Dunbar9d9cc872009-02-10 00:57:50 +0000552}
553
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000554/// EmitLoadOfLValue - Given an expression that represents a value lvalue, this
555/// method emits the address of the lvalue, then loads the result as an rvalue,
556/// returning the rvalue.
Reid Spencer5f016e22007-07-11 17:01:13 +0000557RValue CodeGenFunction::EmitLoadOfLValue(LValue LV, QualType ExprType) {
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +0000558 if (LV.isObjCWeak()) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000559 // load of a __weak object.
Fariborz Jahanian6dc23172008-11-18 21:45:40 +0000560 llvm::Value *AddrWeakObj = LV.getAddress();
Chris Lattnereb99b012009-10-28 17:39:19 +0000561 return RValue::get(CGM.getObjCRuntime().EmitObjCWeakRead(*this,
562 AddrWeakObj));
Fariborz Jahanian6dc23172008-11-18 21:45:40 +0000563 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000564
Reid Spencer5f016e22007-07-11 17:01:13 +0000565 if (LV.isSimple()) {
566 llvm::Value *Ptr = LV.getAddress();
Douglas Gregora5002102010-02-05 21:10:36 +0000567 const llvm::Type *EltTy =
568 cast<llvm::PointerType>(Ptr->getType())->getElementType();
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000569
Reid Spencer5f016e22007-07-11 17:01:13 +0000570 // Simple scalar l-value.
Daniel Dunbare86bcf02010-02-08 22:53:07 +0000571 //
572 // FIXME: We shouldn't have to use isSingleValueType here.
Douglas Gregora5002102010-02-05 21:10:36 +0000573 if (EltTy->isSingleValueType())
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000574 return RValue::get(EmitLoadOfScalar(Ptr, LV.isVolatileQualified(),
Daniel Dunbar9d9cc872009-02-10 00:57:50 +0000575 ExprType));
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000576
Chris Lattner883f6a72007-08-11 00:04:45 +0000577 assert(ExprType->isFunctionType() && "Unknown scalar value");
578 return RValue::get(Ptr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000579 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000580
Reid Spencer5f016e22007-07-11 17:01:13 +0000581 if (LV.isVectorElt()) {
Eli Friedman1e692ac2008-06-13 23:01:12 +0000582 llvm::Value *Vec = Builder.CreateLoad(LV.getVectorAddr(),
583 LV.isVolatileQualified(), "tmp");
Reid Spencer5f016e22007-07-11 17:01:13 +0000584 return RValue::get(Builder.CreateExtractElement(Vec, LV.getVectorIdx(),
585 "vecext"));
586 }
Chris Lattner46ea8eb2007-08-03 00:16:29 +0000587
588 // If this is a reference to a subset of the elements of a vector, either
589 // shuffle the input or extract/insert them as appropriate.
Nate Begeman213541a2008-04-18 23:10:10 +0000590 if (LV.isExtVectorElt())
591 return EmitLoadOfExtVectorElementLValue(LV, ExprType);
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000592
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000593 if (LV.isBitField())
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000594 return EmitLoadOfBitfieldLValue(LV, ExprType);
595
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000596 if (LV.isPropertyRef())
597 return EmitLoadOfPropertyRefLValue(LV, ExprType);
598
Chris Lattner73525de2009-02-16 21:11:58 +0000599 assert(LV.isKVCRef() && "Unknown LValue type!");
600 return EmitLoadOfKVCRefLValue(LV, ExprType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000601}
602
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000603RValue CodeGenFunction::EmitLoadOfBitfieldLValue(LValue LV,
604 QualType ExprType) {
Daniel Dunbarefbf4872010-04-06 01:07:44 +0000605 const CGBitFieldInfo &Info = LV.getBitFieldInfo();
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000606
Daniel Dunbarecdb41e2010-04-13 23:34:15 +0000607 // Get the output type.
608 const llvm::Type *ResLTy = ConvertType(ExprType);
609 unsigned ResSizeInBits = CGM.getTargetData().getTypeSizeInBits(ResLTy);
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000610
Daniel Dunbarecdb41e2010-04-13 23:34:15 +0000611 // Compute the result as an OR of all of the individual component accesses.
612 llvm::Value *Res = 0;
613 for (unsigned i = 0, e = Info.getNumComponents(); i != e; ++i) {
614 const CGBitFieldInfo::AccessInfo &AI = Info.getComponent(i);
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000615
Daniel Dunbarecdb41e2010-04-13 23:34:15 +0000616 // Get the field pointer.
617 llvm::Value *Ptr = LV.getBitFieldBaseAddr();
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000618
Daniel Dunbarecdb41e2010-04-13 23:34:15 +0000619 // Only offset by the field index if used, so that incoming values are not
620 // required to be structures.
621 if (AI.FieldIndex)
622 Ptr = Builder.CreateStructGEP(Ptr, AI.FieldIndex, "bf.field");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000623
Daniel Dunbarecdb41e2010-04-13 23:34:15 +0000624 // Offset by the byte offset, if used.
625 if (AI.FieldByteOffset) {
626 const llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(VMContext);
627 Ptr = Builder.CreateBitCast(Ptr, i8PTy);
628 Ptr = Builder.CreateConstGEP1_32(Ptr, AI.FieldByteOffset,"bf.field.offs");
629 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000630
Daniel Dunbarecdb41e2010-04-13 23:34:15 +0000631 // Cast to the access type.
632 const llvm::Type *PTy = llvm::Type::getIntNPtrTy(VMContext, AI.AccessWidth,
633 ExprType.getAddressSpace());
634 Ptr = Builder.CreateBitCast(Ptr, PTy);
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000635
Daniel Dunbarecdb41e2010-04-13 23:34:15 +0000636 // Perform the load.
637 llvm::LoadInst *Load = Builder.CreateLoad(Ptr, LV.isVolatileQualified());
638 if (AI.AccessAlignment)
639 Load->setAlignment(AI.AccessAlignment);
640
641 // Shift out unused low bits and mask out unused high bits.
642 llvm::Value *Val = Load;
643 if (AI.FieldBitStart)
Daniel Dunbar26772612010-04-15 03:47:33 +0000644 Val = Builder.CreateLShr(Load, AI.FieldBitStart);
Daniel Dunbarecdb41e2010-04-13 23:34:15 +0000645 Val = Builder.CreateAnd(Val, llvm::APInt::getLowBitsSet(AI.AccessWidth,
646 AI.TargetBitWidth),
647 "bf.clear");
648
649 // Extend or truncate to the target size.
650 if (AI.AccessWidth < ResSizeInBits)
651 Val = Builder.CreateZExt(Val, ResLTy);
652 else if (AI.AccessWidth > ResSizeInBits)
653 Val = Builder.CreateTrunc(Val, ResLTy);
654
655 // Shift into place, and OR into the result.
656 if (AI.TargetBitOffset)
657 Val = Builder.CreateShl(Val, AI.TargetBitOffset);
658 Res = Res ? Builder.CreateOr(Res, Val) : Val;
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000659 }
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000660
Daniel Dunbarecdb41e2010-04-13 23:34:15 +0000661 // If the bit-field is signed, perform the sign-extension.
662 //
663 // FIXME: This can easily be folded into the load of the high bits, which
664 // could also eliminate the mask of high bits in some situations.
665 if (Info.isSigned()) {
Daniel Dunbar26772612010-04-15 03:47:33 +0000666 unsigned ExtraBits = ResSizeInBits - Info.getSize();
Daniel Dunbarecdb41e2010-04-13 23:34:15 +0000667 if (ExtraBits)
668 Res = Builder.CreateAShr(Builder.CreateShl(Res, ExtraBits),
669 ExtraBits, "bf.val.sext");
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000670 }
Eli Friedman316bb1b2008-05-17 20:03:47 +0000671
Daniel Dunbarecdb41e2010-04-13 23:34:15 +0000672 return RValue::get(Res);
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000673}
674
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000675RValue CodeGenFunction::EmitLoadOfPropertyRefLValue(LValue LV,
676 QualType ExprType) {
677 return EmitObjCPropertyGet(LV.getPropertyRefExpr());
678}
679
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000680RValue CodeGenFunction::EmitLoadOfKVCRefLValue(LValue LV,
681 QualType ExprType) {
682 return EmitObjCPropertyGet(LV.getKVCRefExpr());
683}
684
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000685// If this is a reference to a subset of the elements of a vector, create an
686// appropriate shufflevector.
Nate Begeman213541a2008-04-18 23:10:10 +0000687RValue CodeGenFunction::EmitLoadOfExtVectorElementLValue(LValue LV,
688 QualType ExprType) {
Eli Friedman1e692ac2008-06-13 23:01:12 +0000689 llvm::Value *Vec = Builder.CreateLoad(LV.getExtVectorAddr(),
690 LV.isVolatileQualified(), "tmp");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000691
Nate Begeman8a997642008-05-09 06:41:27 +0000692 const llvm::Constant *Elts = LV.getExtVectorElts();
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000693
694 // If the result of the expression is a non-vector type, we must be extracting
695 // a single element. Just codegen as an extractelement.
John McCall183700f2009-09-21 23:43:11 +0000696 const VectorType *ExprVT = ExprType->getAs<VectorType>();
Chris Lattnercf60cd22007-08-10 17:10:08 +0000697 if (!ExprVT) {
Dan Gohman4f8d1232008-05-22 00:50:06 +0000698 unsigned InIdx = getAccessedFieldNo(0, Elts);
Owen Anderson0032b272009-08-13 21:57:51 +0000699 llvm::Value *Elt = llvm::ConstantInt::get(
700 llvm::Type::getInt32Ty(VMContext), InIdx);
Chris Lattner34cdc862007-08-03 16:18:34 +0000701 return RValue::get(Builder.CreateExtractElement(Vec, Elt, "tmp"));
702 }
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000703
704 // Always use shuffle vector to try to retain the original program structure
Chris Lattnercf60cd22007-08-10 17:10:08 +0000705 unsigned NumResultElts = ExprVT->getNumElements();
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000706
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000707 llvm::SmallVector<llvm::Constant*, 4> Mask;
Chris Lattner34cdc862007-08-03 16:18:34 +0000708 for (unsigned i = 0; i != NumResultElts; ++i) {
Dan Gohman4f8d1232008-05-22 00:50:06 +0000709 unsigned InIdx = getAccessedFieldNo(i, Elts);
Owen Anderson0032b272009-08-13 21:57:51 +0000710 Mask.push_back(llvm::ConstantInt::get(
711 llvm::Type::getInt32Ty(VMContext), InIdx));
Chris Lattner34cdc862007-08-03 16:18:34 +0000712 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000713
Owen Anderson4a289322009-07-28 21:22:35 +0000714 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000715 Vec = Builder.CreateShuffleVector(Vec,
Owen Anderson03e20502009-07-30 23:11:26 +0000716 llvm::UndefValue::get(Vec->getType()),
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000717 MaskV, "tmp");
718 return RValue::get(Vec);
Chris Lattner34cdc862007-08-03 16:18:34 +0000719}
720
721
Reid Spencer5f016e22007-07-11 17:01:13 +0000722
723/// EmitStoreThroughLValue - Store the specified rvalue into the specified
724/// lvalue, where both are guaranteed to the have the same type, and that type
725/// is 'Ty'.
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000726void CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst,
Reid Spencer5f016e22007-07-11 17:01:13 +0000727 QualType Ty) {
Chris Lattner017d6aa2007-08-03 16:28:33 +0000728 if (!Dst.isSimple()) {
729 if (Dst.isVectorElt()) {
730 // Read/modify/write the vector, inserting the new element.
Eli Friedman1e692ac2008-06-13 23:01:12 +0000731 llvm::Value *Vec = Builder.CreateLoad(Dst.getVectorAddr(),
732 Dst.isVolatileQualified(), "tmp");
Chris Lattner9b655512007-08-31 22:49:20 +0000733 Vec = Builder.CreateInsertElement(Vec, Src.getScalarVal(),
Chris Lattner017d6aa2007-08-03 16:28:33 +0000734 Dst.getVectorIdx(), "vecins");
Eli Friedman1e692ac2008-06-13 23:01:12 +0000735 Builder.CreateStore(Vec, Dst.getVectorAddr(),Dst.isVolatileQualified());
Chris Lattner017d6aa2007-08-03 16:28:33 +0000736 return;
737 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000738
Nate Begeman213541a2008-04-18 23:10:10 +0000739 // If this is an update of extended vector elements, insert them as
740 // appropriate.
741 if (Dst.isExtVectorElt())
742 return EmitStoreThroughExtVectorComponentLValue(Src, Dst, Ty);
Lauro Ramos Venancioa0c5d0e2008-01-22 22:36:45 +0000743
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000744 if (Dst.isBitField())
Lauro Ramos Venancioa0c5d0e2008-01-22 22:36:45 +0000745 return EmitStoreThroughBitfieldLValue(Src, Dst, Ty);
746
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000747 if (Dst.isPropertyRef())
748 return EmitStoreThroughPropertyRefLValue(Src, Dst, Ty);
749
Chris Lattnereb99b012009-10-28 17:39:19 +0000750 assert(Dst.isKVCRef() && "Unknown LValue type");
751 return EmitStoreThroughKVCRefLValue(Src, Dst, Ty);
Chris Lattner017d6aa2007-08-03 16:28:33 +0000752 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000753
Fariborz Jahanian4f676ed2009-02-21 00:30:43 +0000754 if (Dst.isObjCWeak() && !Dst.isNonGC()) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000755 // load of a __weak object.
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +0000756 llvm::Value *LvalueDst = Dst.getAddress();
757 llvm::Value *src = Src.getScalarVal();
Mike Stumpf33651c2009-04-14 00:57:29 +0000758 CGM.getObjCRuntime().EmitObjCWeakAssign(*this, src, LvalueDst);
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +0000759 return;
760 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000761
Fariborz Jahanian4f676ed2009-02-21 00:30:43 +0000762 if (Dst.isObjCStrong() && !Dst.isNonGC()) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000763 // load of a __strong object.
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +0000764 llvm::Value *LvalueDst = Dst.getAddress();
765 llvm::Value *src = Src.getScalarVal();
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000766 if (Dst.isObjCIvar()) {
767 assert(Dst.getBaseIvarExp() && "BaseIvarExp is NULL");
768 const llvm::Type *ResultType = ConvertType(getContext().LongTy);
769 llvm::Value *RHS = EmitScalarExpr(Dst.getBaseIvarExp());
Fariborz Jahanian76368e82009-09-25 00:00:20 +0000770 llvm::Value *dst = RHS;
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000771 RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast");
772 llvm::Value *LHS =
773 Builder.CreatePtrToInt(LvalueDst, ResultType, "sub.ptr.lhs.cast");
774 llvm::Value *BytesBetween = Builder.CreateSub(LHS, RHS, "ivar.offset");
Fariborz Jahanian76368e82009-09-25 00:00:20 +0000775 CGM.getObjCRuntime().EmitObjCIvarAssign(*this, src, dst,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000776 BytesBetween);
Chris Lattnereb99b012009-10-28 17:39:19 +0000777 } else if (Dst.isGlobalObjCRef())
Fariborz Jahanianbf63b872009-05-04 23:27:20 +0000778 CGM.getObjCRuntime().EmitObjCGlobalAssign(*this, src, LvalueDst);
779 else
780 CGM.getObjCRuntime().EmitObjCStrongCastAssign(*this, src, LvalueDst);
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +0000781 return;
782 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000783
Chris Lattner883f6a72007-08-11 00:04:45 +0000784 assert(Src.isScalar() && "Can't emit an agg store with this method");
Anders Carlssonb4aa4662009-05-19 18:50:41 +0000785 EmitStoreOfScalar(Src.getScalarVal(), Dst.getAddress(),
786 Dst.isVolatileQualified(), Ty);
Reid Spencer5f016e22007-07-11 17:01:13 +0000787}
788
Lauro Ramos Venancioa0c5d0e2008-01-22 22:36:45 +0000789void CodeGenFunction::EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst,
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000790 QualType Ty,
Daniel Dunbared3849b2008-11-19 09:36:46 +0000791 llvm::Value **Result) {
Daniel Dunbarefbf4872010-04-06 01:07:44 +0000792 const CGBitFieldInfo &Info = Dst.getBitFieldInfo();
Lauro Ramos Venancioa0c5d0e2008-01-22 22:36:45 +0000793
Daniel Dunbar26772612010-04-15 03:47:33 +0000794 // Get the output type.
Anders Carlsson48035352010-04-17 21:52:22 +0000795 const llvm::Type *ResLTy = ConvertTypeForMem(Ty);
Daniel Dunbar26772612010-04-15 03:47:33 +0000796 unsigned ResSizeInBits = CGM.getTargetData().getTypeSizeInBits(ResLTy);
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000797
Daniel Dunbar26772612010-04-15 03:47:33 +0000798 // Get the source value, truncated to the width of the bit-field.
Daniel Dunbared3849b2008-11-19 09:36:46 +0000799 llvm::Value *SrcVal = Src.getScalarVal();
Anders Carlsson48035352010-04-17 21:52:22 +0000800
801 if (Ty->isBooleanType())
802 SrcVal = Builder.CreateIntCast(SrcVal, ResLTy, /*IsSigned=*/false);
803
Daniel Dunbar26772612010-04-15 03:47:33 +0000804 SrcVal = Builder.CreateAnd(SrcVal, llvm::APInt::getLowBitsSet(ResSizeInBits,
805 Info.getSize()),
806 "bf.value");
Lauro Ramos Venancioa0c5d0e2008-01-22 22:36:45 +0000807
Daniel Dunbared3849b2008-11-19 09:36:46 +0000808 // Return the new value of the bit-field, if requested.
809 if (Result) {
810 // Cast back to the proper type for result.
Daniel Dunbar26772612010-04-15 03:47:33 +0000811 const llvm::Type *SrcTy = Src.getScalarVal()->getType();
812 llvm::Value *ReloadVal = Builder.CreateIntCast(SrcVal, SrcTy, false,
813 "bf.reload.val");
Daniel Dunbared3849b2008-11-19 09:36:46 +0000814
815 // Sign extend if necessary.
Daniel Dunbar26772612010-04-15 03:47:33 +0000816 if (Info.isSigned()) {
817 unsigned ExtraBits = ResSizeInBits - Info.getSize();
818 if (ExtraBits)
819 ReloadVal = Builder.CreateAShr(Builder.CreateShl(ReloadVal, ExtraBits),
820 ExtraBits, "bf.reload.sext");
Daniel Dunbared3849b2008-11-19 09:36:46 +0000821 }
822
Daniel Dunbar26772612010-04-15 03:47:33 +0000823 *Result = ReloadVal;
Daniel Dunbared3849b2008-11-19 09:36:46 +0000824 }
825
Daniel Dunbar26772612010-04-15 03:47:33 +0000826 // Iterate over the components, writing each piece to memory.
827 for (unsigned i = 0, e = Info.getNumComponents(); i != e; ++i) {
828 const CGBitFieldInfo::AccessInfo &AI = Info.getComponent(i);
Eli Friedman316bb1b2008-05-17 20:03:47 +0000829
Daniel Dunbar26772612010-04-15 03:47:33 +0000830 // Get the field pointer.
831 llvm::Value *Ptr = Dst.getBitFieldBaseAddr();
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000832
Daniel Dunbar26772612010-04-15 03:47:33 +0000833 // Only offset by the field index if used, so that incoming values are not
834 // required to be structures.
835 if (AI.FieldIndex)
836 Ptr = Builder.CreateStructGEP(Ptr, AI.FieldIndex, "bf.field");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000837
Daniel Dunbar26772612010-04-15 03:47:33 +0000838 // Offset by the byte offset, if used.
839 if (AI.FieldByteOffset) {
840 const llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(VMContext);
841 Ptr = Builder.CreateBitCast(Ptr, i8PTy);
842 Ptr = Builder.CreateConstGEP1_32(Ptr, AI.FieldByteOffset,"bf.field.offs");
843 }
Eli Friedman316bb1b2008-05-17 20:03:47 +0000844
Daniel Dunbar26772612010-04-15 03:47:33 +0000845 // Cast to the access type.
846 const llvm::Type *PTy = llvm::Type::getIntNPtrTy(VMContext, AI.AccessWidth,
847 Ty.getAddressSpace());
848 Ptr = Builder.CreateBitCast(Ptr, PTy);
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000849
Daniel Dunbar26772612010-04-15 03:47:33 +0000850 // Extract the piece of the bit-field value to write in this access, limited
851 // to the values that are part of this access.
852 llvm::Value *Val = SrcVal;
853 if (AI.TargetBitOffset)
854 Val = Builder.CreateLShr(Val, AI.TargetBitOffset);
855 Val = Builder.CreateAnd(Val, llvm::APInt::getLowBitsSet(ResSizeInBits,
856 AI.TargetBitWidth));
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000857
Daniel Dunbar26772612010-04-15 03:47:33 +0000858 // Extend or truncate to the access size.
859 const llvm::Type *AccessLTy =
860 llvm::Type::getIntNTy(VMContext, AI.AccessWidth);
861 if (ResSizeInBits < AI.AccessWidth)
862 Val = Builder.CreateZExt(Val, AccessLTy);
863 else if (ResSizeInBits > AI.AccessWidth)
864 Val = Builder.CreateTrunc(Val, AccessLTy);
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000865
Daniel Dunbar26772612010-04-15 03:47:33 +0000866 // Shift into the position in memory.
867 if (AI.FieldBitStart)
868 Val = Builder.CreateShl(Val, AI.FieldBitStart);
869
870 // If necessary, load and OR in bits that are outside of the bit-field.
871 if (AI.TargetBitWidth != AI.AccessWidth) {
872 llvm::LoadInst *Load = Builder.CreateLoad(Ptr, Dst.isVolatileQualified());
873 if (AI.AccessAlignment)
874 Load->setAlignment(AI.AccessAlignment);
875
876 // Compute the mask for zeroing the bits that are part of the bit-field.
877 llvm::APInt InvMask =
878 ~llvm::APInt::getBitsSet(AI.AccessWidth, AI.FieldBitStart,
879 AI.FieldBitStart + AI.TargetBitWidth);
880
881 // Apply the mask and OR in to the value to write.
882 Val = Builder.CreateOr(Builder.CreateAnd(Load, InvMask), Val);
883 }
884
885 // Write the value.
886 llvm::StoreInst *Store = Builder.CreateStore(Val, Ptr,
887 Dst.isVolatileQualified());
888 if (AI.AccessAlignment)
889 Store->setAlignment(AI.AccessAlignment);
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000890 }
Lauro Ramos Venancioa0c5d0e2008-01-22 22:36:45 +0000891}
892
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000893void CodeGenFunction::EmitStoreThroughPropertyRefLValue(RValue Src,
894 LValue Dst,
895 QualType Ty) {
896 EmitObjCPropertySet(Dst.getPropertyRefExpr(), Src);
897}
898
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000899void CodeGenFunction::EmitStoreThroughKVCRefLValue(RValue Src,
900 LValue Dst,
901 QualType Ty) {
902 EmitObjCPropertySet(Dst.getKVCRefExpr(), Src);
903}
904
Nate Begeman213541a2008-04-18 23:10:10 +0000905void CodeGenFunction::EmitStoreThroughExtVectorComponentLValue(RValue Src,
906 LValue Dst,
907 QualType Ty) {
Chris Lattner017d6aa2007-08-03 16:28:33 +0000908 // This access turns into a read/modify/write of the vector. Load the input
909 // value now.
Eli Friedman1e692ac2008-06-13 23:01:12 +0000910 llvm::Value *Vec = Builder.CreateLoad(Dst.getExtVectorAddr(),
911 Dst.isVolatileQualified(), "tmp");
Nate Begeman8a997642008-05-09 06:41:27 +0000912 const llvm::Constant *Elts = Dst.getExtVectorElts();
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000913
Chris Lattner9b655512007-08-31 22:49:20 +0000914 llvm::Value *SrcVal = Src.getScalarVal();
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000915
John McCall183700f2009-09-21 23:43:11 +0000916 if (const VectorType *VTy = Ty->getAs<VectorType>()) {
Chris Lattner7e6b51b2007-08-03 16:37:04 +0000917 unsigned NumSrcElts = VTy->getNumElements();
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000918 unsigned NumDstElts =
919 cast<llvm::VectorType>(Vec->getType())->getNumElements();
920 if (NumDstElts == NumSrcElts) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000921 // Use shuffle vector is the src and destination are the same number of
922 // elements and restore the vector mask since it is on the side it will be
923 // stored.
Nate Begeman6e5dd862009-06-26 21:12:50 +0000924 llvm::SmallVector<llvm::Constant*, 4> Mask(NumDstElts);
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000925 for (unsigned i = 0; i != NumSrcElts; ++i) {
926 unsigned InIdx = getAccessedFieldNo(i, Elts);
Owen Anderson0032b272009-08-13 21:57:51 +0000927 Mask[InIdx] = llvm::ConstantInt::get(
928 llvm::Type::getInt32Ty(VMContext), i);
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000929 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000930
Owen Anderson4a289322009-07-28 21:22:35 +0000931 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000932 Vec = Builder.CreateShuffleVector(SrcVal,
Owen Anderson03e20502009-07-30 23:11:26 +0000933 llvm::UndefValue::get(Vec->getType()),
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000934 MaskV, "tmp");
Mike Stumpb3589f42009-07-30 22:28:39 +0000935 } else if (NumDstElts > NumSrcElts) {
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000936 // Extended the source vector to the same length and then shuffle it
937 // into the destination.
938 // FIXME: since we're shuffling with undef, can we just use the indices
939 // into that? This could be simpler.
940 llvm::SmallVector<llvm::Constant*, 4> ExtMask;
Chris Lattnereb99b012009-10-28 17:39:19 +0000941 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000942 unsigned i;
943 for (i = 0; i != NumSrcElts; ++i)
Chris Lattnereb99b012009-10-28 17:39:19 +0000944 ExtMask.push_back(llvm::ConstantInt::get(Int32Ty, i));
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000945 for (; i != NumDstElts; ++i)
Chris Lattnereb99b012009-10-28 17:39:19 +0000946 ExtMask.push_back(llvm::UndefValue::get(Int32Ty));
Owen Anderson4a289322009-07-28 21:22:35 +0000947 llvm::Value *ExtMaskV = llvm::ConstantVector::get(&ExtMask[0],
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000948 ExtMask.size());
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000949 llvm::Value *ExtSrcVal =
Daniel Dunbarbb767732009-02-17 18:31:04 +0000950 Builder.CreateShuffleVector(SrcVal,
Owen Anderson03e20502009-07-30 23:11:26 +0000951 llvm::UndefValue::get(SrcVal->getType()),
Daniel Dunbarbb767732009-02-17 18:31:04 +0000952 ExtMaskV, "tmp");
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000953 // build identity
954 llvm::SmallVector<llvm::Constant*, 4> Mask;
Chris Lattnereb99b012009-10-28 17:39:19 +0000955 for (unsigned i = 0; i != NumDstElts; ++i)
956 Mask.push_back(llvm::ConstantInt::get(Int32Ty, i));
957
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000958 // modify when what gets shuffled in
959 for (unsigned i = 0; i != NumSrcElts; ++i) {
960 unsigned Idx = getAccessedFieldNo(i, Elts);
Chris Lattnereb99b012009-10-28 17:39:19 +0000961 Mask[Idx] = llvm::ConstantInt::get(Int32Ty, i+NumDstElts);
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000962 }
Owen Anderson4a289322009-07-28 21:22:35 +0000963 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000964 Vec = Builder.CreateShuffleVector(Vec, ExtSrcVal, MaskV, "tmp");
Mike Stumpb3589f42009-07-30 22:28:39 +0000965 } else {
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000966 // We should never shorten the vector
967 assert(0 && "unexpected shorten vector length");
Chris Lattner7e6b51b2007-08-03 16:37:04 +0000968 }
969 } else {
970 // If the Src is a scalar (not a vector) it must be updating one element.
Dan Gohman4f8d1232008-05-22 00:50:06 +0000971 unsigned InIdx = getAccessedFieldNo(0, Elts);
Chris Lattnereb99b012009-10-28 17:39:19 +0000972 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
973 llvm::Value *Elt = llvm::ConstantInt::get(Int32Ty, InIdx);
Chris Lattner017d6aa2007-08-03 16:28:33 +0000974 Vec = Builder.CreateInsertElement(Vec, SrcVal, Elt, "tmp");
Chris Lattner017d6aa2007-08-03 16:28:33 +0000975 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000976
Eli Friedman1e692ac2008-06-13 23:01:12 +0000977 Builder.CreateStore(Vec, Dst.getExtVectorAddr(), Dst.isVolatileQualified());
Chris Lattner017d6aa2007-08-03 16:28:33 +0000978}
979
Fariborz Jahanianb123ea32009-09-16 21:37:16 +0000980// setObjCGCLValueClass - sets class of he lvalue for the purpose of
981// generating write-barries API. It is currently a global, ivar,
982// or neither.
Chris Lattnereb99b012009-10-28 17:39:19 +0000983static void setObjCGCLValueClass(const ASTContext &Ctx, const Expr *E,
984 LValue &LV) {
Fariborz Jahanianb9242592009-09-21 23:03:37 +0000985 if (Ctx.getLangOptions().getGCMode() == LangOptions::NonGC)
Fariborz Jahanianb123ea32009-09-16 21:37:16 +0000986 return;
987
Fariborz Jahaniandbf3cfd2009-09-16 23:11:23 +0000988 if (isa<ObjCIvarRefExpr>(E)) {
989 LV.SetObjCIvar(LV, true);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000990 ObjCIvarRefExpr *Exp = cast<ObjCIvarRefExpr>(const_cast<Expr*>(E));
991 LV.setBaseIvarExp(Exp->getBase());
Fariborz Jahanianfd02ed72009-09-21 18:54:29 +0000992 LV.SetObjCArray(LV, E->getType()->isArrayType());
Fariborz Jahaniandbf3cfd2009-09-16 23:11:23 +0000993 return;
994 }
Chris Lattnereb99b012009-10-28 17:39:19 +0000995
Fariborz Jahanianb123ea32009-09-16 21:37:16 +0000996 if (const DeclRefExpr *Exp = dyn_cast<DeclRefExpr>(E)) {
997 if (const VarDecl *VD = dyn_cast<VarDecl>(Exp->getDecl())) {
998 if ((VD->isBlockVarDecl() && !VD->hasLocalStorage()) ||
999 VD->isFileVarDecl())
1000 LV.SetGlobalObjCRef(LV, true);
1001 }
Fariborz Jahanianfd02ed72009-09-21 18:54:29 +00001002 LV.SetObjCArray(LV, E->getType()->isArrayType());
Chris Lattnereb99b012009-10-28 17:39:19 +00001003 return;
Fariborz Jahanianb123ea32009-09-16 21:37:16 +00001004 }
Chris Lattnereb99b012009-10-28 17:39:19 +00001005
1006 if (const UnaryOperator *Exp = dyn_cast<UnaryOperator>(E)) {
Fariborz Jahanianb123ea32009-09-16 21:37:16 +00001007 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
Chris Lattnereb99b012009-10-28 17:39:19 +00001008 return;
1009 }
1010
1011 if (const ParenExpr *Exp = dyn_cast<ParenExpr>(E)) {
Fariborz Jahanianb123ea32009-09-16 21:37:16 +00001012 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
Fariborz Jahanian75b08f12009-09-30 17:10:29 +00001013 if (LV.isObjCIvar()) {
1014 // If cast is to a structure pointer, follow gcc's behavior and make it
1015 // a non-ivar write-barrier.
1016 QualType ExpTy = E->getType();
1017 if (ExpTy->isPointerType())
1018 ExpTy = ExpTy->getAs<PointerType>()->getPointeeType();
1019 if (ExpTy->isRecordType())
1020 LV.SetObjCIvar(LV, false);
Chris Lattnereb99b012009-10-28 17:39:19 +00001021 }
1022 return;
Fariborz Jahanian75b08f12009-09-30 17:10:29 +00001023 }
Chris Lattnereb99b012009-10-28 17:39:19 +00001024 if (const ImplicitCastExpr *Exp = dyn_cast<ImplicitCastExpr>(E)) {
Fariborz Jahanianb123ea32009-09-16 21:37:16 +00001025 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
Chris Lattnereb99b012009-10-28 17:39:19 +00001026 return;
1027 }
1028
1029 if (const CStyleCastExpr *Exp = dyn_cast<CStyleCastExpr>(E)) {
Fariborz Jahanianb123ea32009-09-16 21:37:16 +00001030 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
Chris Lattnereb99b012009-10-28 17:39:19 +00001031 return;
1032 }
1033
1034 if (const ArraySubscriptExpr *Exp = dyn_cast<ArraySubscriptExpr>(E)) {
Fariborz Jahanianb123ea32009-09-16 21:37:16 +00001035 setObjCGCLValueClass(Ctx, Exp->getBase(), LV);
Fariborz Jahanianfd02ed72009-09-21 18:54:29 +00001036 if (LV.isObjCIvar() && !LV.isObjCArray())
Fariborz Jahanian1c1afc42009-09-18 00:04:00 +00001037 // Using array syntax to assigning to what an ivar points to is not
1038 // same as assigning to the ivar itself. {id *Names;} Names[i] = 0;
1039 LV.SetObjCIvar(LV, false);
Fariborz Jahanianfd02ed72009-09-21 18:54:29 +00001040 else if (LV.isGlobalObjCRef() && !LV.isObjCArray())
1041 // Using array syntax to assigning to what global points to is not
1042 // same as assigning to the global itself. {id *G;} G[i] = 0;
1043 LV.SetGlobalObjCRef(LV, false);
Chris Lattnereb99b012009-10-28 17:39:19 +00001044 return;
Fariborz Jahanian1c1afc42009-09-18 00:04:00 +00001045 }
Chris Lattnereb99b012009-10-28 17:39:19 +00001046
1047 if (const MemberExpr *Exp = dyn_cast<MemberExpr>(E)) {
Fariborz Jahanianb123ea32009-09-16 21:37:16 +00001048 setObjCGCLValueClass(Ctx, Exp->getBase(), LV);
Fariborz Jahanian1c1afc42009-09-18 00:04:00 +00001049 // We don't know if member is an 'ivar', but this flag is looked at
1050 // only in the context of LV.isObjCIvar().
Fariborz Jahanianfd02ed72009-09-21 18:54:29 +00001051 LV.SetObjCArray(LV, E->getType()->isArrayType());
Chris Lattnereb99b012009-10-28 17:39:19 +00001052 return;
Fariborz Jahanianb123ea32009-09-16 21:37:16 +00001053 }
1054}
1055
Anders Carlssonce53f7d2009-11-07 23:06:58 +00001056static LValue EmitGlobalVarDeclLValue(CodeGenFunction &CGF,
1057 const Expr *E, const VarDecl *VD) {
Daniel Dunbard2113f22009-11-08 09:46:46 +00001058 assert((VD->hasExternalStorage() || VD->isFileVarDecl()) &&
Anders Carlssonce53f7d2009-11-07 23:06:58 +00001059 "Var decl must have external storage or be a file var decl!");
1060
1061 llvm::Value *V = CGF.CGM.GetAddrOfGlobalVar(VD);
1062 if (VD->getType()->isReferenceType())
1063 V = CGF.Builder.CreateLoad(V, "tmp");
1064 LValue LV = LValue::MakeAddr(V, CGF.MakeQualifiers(E->getType()));
1065 setObjCGCLValueClass(CGF.getContext(), E, LV);
1066 return LV;
1067}
1068
Eli Friedman9a146302009-11-26 06:08:14 +00001069static LValue EmitFunctionDeclLValue(CodeGenFunction &CGF,
1070 const Expr *E, const FunctionDecl *FD) {
1071 llvm::Value* V = CGF.CGM.GetAddrOfFunction(FD);
1072 if (!FD->hasPrototype()) {
1073 if (const FunctionProtoType *Proto =
1074 FD->getType()->getAs<FunctionProtoType>()) {
1075 // Ugly case: for a K&R-style definition, the type of the definition
1076 // isn't the same as the type of a use. Correct for this with a
1077 // bitcast.
1078 QualType NoProtoType =
1079 CGF.getContext().getFunctionNoProtoType(Proto->getResultType());
1080 NoProtoType = CGF.getContext().getPointerType(NoProtoType);
1081 V = CGF.Builder.CreateBitCast(V, CGF.ConvertType(NoProtoType), "tmp");
1082 }
1083 }
1084 return LValue::MakeAddr(V, CGF.MakeQualifiers(E->getType()));
1085}
1086
Reid Spencer5f016e22007-07-11 17:01:13 +00001087LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) {
Anders Carlsson1e74c4f2009-11-07 22:53:10 +00001088 const NamedDecl *ND = E->getDecl();
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001089
Rafael Espindola6a836702010-03-04 18:17:24 +00001090 if (ND->hasAttr<WeakRefAttr>()) {
1091 const ValueDecl* VD = cast<ValueDecl>(ND);
1092 llvm::Constant *Aliasee = CGM.GetWeakRefReference(VD);
1093
1094 Qualifiers Quals = MakeQualifiers(E->getType());
1095 LValue LV = LValue::MakeAddr(Aliasee, Quals);
1096
1097 return LV;
1098 }
1099
Anders Carlsson1e74c4f2009-11-07 22:53:10 +00001100 if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
Anders Carlsson1e74c4f2009-11-07 22:53:10 +00001101
1102 // Check if this is a global variable.
Anders Carlssonce53f7d2009-11-07 23:06:58 +00001103 if (VD->hasExternalStorage() || VD->isFileVarDecl())
1104 return EmitGlobalVarDeclLValue(*this, E, VD);
Anders Carlsson0bc70492009-11-07 22:46:42 +00001105
1106 bool NonGCable = VD->hasLocalStorage() && !VD->hasAttr<BlocksAttr>();
1107
1108 llvm::Value *V = LocalDeclMap[VD];
Fariborz Jahanian63326a52010-04-19 18:15:02 +00001109 if (!V && getContext().getLangOptions().CPlusPlus &&
1110 VD->isStaticLocal())
1111 V = CGM.getStaticLocalDeclAddress(VD);
Anders Carlsson0bc70492009-11-07 22:46:42 +00001112 assert(V && "DeclRefExpr not entered in LocalDeclMap?");
1113
1114 Qualifiers Quals = MakeQualifiers(E->getType());
1115 // local variables do not get their gc attribute set.
1116 // local static?
1117 if (NonGCable) Quals.removeObjCGCAttr();
1118
1119 if (VD->hasAttr<BlocksAttr>()) {
1120 V = Builder.CreateStructGEP(V, 1, "forwarding");
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00001121 V = Builder.CreateLoad(V);
Anders Carlsson0bc70492009-11-07 22:46:42 +00001122 V = Builder.CreateStructGEP(V, getByRefValueLLVMField(VD),
1123 VD->getNameAsString());
1124 }
1125 if (VD->getType()->isReferenceType())
1126 V = Builder.CreateLoad(V, "tmp");
Anders Carlssonce53f7d2009-11-07 23:06:58 +00001127 LValue LV = LValue::MakeAddr(V, Quals);
Anders Carlsson0bc70492009-11-07 22:46:42 +00001128 LValue::SetObjCNonGC(LV, NonGCable);
Fariborz Jahanianb123ea32009-09-16 21:37:16 +00001129 setObjCGCLValueClass(getContext(), E, LV);
Fariborz Jahanian2682d8b2008-11-20 00:15:42 +00001130 return LV;
Chris Lattnereb99b012009-10-28 17:39:19 +00001131 }
1132
Eli Friedman9a146302009-11-26 06:08:14 +00001133 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
1134 return EmitFunctionDeclLValue(*this, E, FD);
Chris Lattnereb99b012009-10-28 17:39:19 +00001135
Anders Carlsson45147d02010-02-02 03:37:46 +00001136 // FIXME: the qualifier check does not seem sufficient here
Chris Lattnereb99b012009-10-28 17:39:19 +00001137 if (E->getQualifier()) {
Anders Carlsson45147d02010-02-02 03:37:46 +00001138 const FieldDecl *FD = cast<FieldDecl>(ND);
1139 llvm::Value *V = CGM.EmitPointerToDataMember(FD);
1140
1141 return LValue::MakeAddr(V, MakeQualifiers(FD->getType()));
Chris Lattner41110242008-06-17 18:05:57 +00001142 }
Chris Lattnereb99b012009-10-28 17:39:19 +00001143
Anders Carlsson1e74c4f2009-11-07 22:53:10 +00001144 assert(false && "Unhandled DeclRefExpr");
1145
1146 // an invalid LValue, but the assert will
1147 // ensure that this point is never reached.
Chris Lattnerb1776cb2007-09-16 19:23:47 +00001148 return LValue();
Reid Spencer5f016e22007-07-11 17:01:13 +00001149}
1150
Mike Stumpa99038c2009-02-28 09:07:16 +00001151LValue CodeGenFunction::EmitBlockDeclRefLValue(const BlockDeclRefExpr *E) {
John McCall0953e762009-09-24 19:53:00 +00001152 return LValue::MakeAddr(GetAddrOfBlockDecl(E), MakeQualifiers(E->getType()));
Mike Stumpa99038c2009-02-28 09:07:16 +00001153}
1154
Reid Spencer5f016e22007-07-11 17:01:13 +00001155LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) {
1156 // __extension__ doesn't affect lvalue-ness.
1157 if (E->getOpcode() == UnaryOperator::Extension)
1158 return EmitLValue(E->getSubExpr());
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001159
Chris Lattner96196622008-07-26 22:37:01 +00001160 QualType ExprTy = getContext().getCanonicalType(E->getSubExpr()->getType());
Chris Lattner7da36f62007-10-30 22:53:42 +00001161 switch (E->getOpcode()) {
1162 default: assert(0 && "Unknown unary operator lvalue!");
Chris Lattnereb99b012009-10-28 17:39:19 +00001163 case UnaryOperator::Deref: {
1164 QualType T = E->getSubExpr()->getType()->getPointeeType();
1165 assert(!T.isNull() && "CodeGenFunction::EmitUnaryOpLValue: Illegal type");
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001166
Chris Lattnereb99b012009-10-28 17:39:19 +00001167 Qualifiers Quals = MakeQualifiers(T);
1168 Quals.setAddressSpace(ExprTy.getAddressSpace());
John McCall0953e762009-09-24 19:53:00 +00001169
Chris Lattnereb99b012009-10-28 17:39:19 +00001170 LValue LV = LValue::MakeAddr(EmitScalarExpr(E->getSubExpr()), Quals);
1171 // We should not generate __weak write barrier on indirect reference
1172 // of a pointer to object; as in void foo (__weak id *param); *param = 0;
1173 // But, we continue to generate __strong write barrier on indirect write
1174 // into a pointer to object.
1175 if (getContext().getLangOptions().ObjC1 &&
1176 getContext().getLangOptions().getGCMode() != LangOptions::NonGC &&
1177 LV.isObjCWeak())
1178 LValue::SetObjCNonGC(LV, !E->isOBJCGCCandidate(getContext()));
1179 return LV;
1180 }
Chris Lattner7da36f62007-10-30 22:53:42 +00001181 case UnaryOperator::Real:
Eli Friedmane401cd52009-11-09 04:20:47 +00001182 case UnaryOperator::Imag: {
Chris Lattner7da36f62007-10-30 22:53:42 +00001183 LValue LV = EmitLValue(E->getSubExpr());
Chris Lattner36b6a0a2008-03-19 05:19:41 +00001184 unsigned Idx = E->getOpcode() == UnaryOperator::Imag;
1185 return LValue::MakeAddr(Builder.CreateStructGEP(LV.getAddress(),
Chris Lattnerb77792e2008-07-26 22:17:49 +00001186 Idx, "idx"),
John McCall0953e762009-09-24 19:53:00 +00001187 MakeQualifiers(ExprTy));
Chris Lattner7da36f62007-10-30 22:53:42 +00001188 }
Eli Friedmane401cd52009-11-09 04:20:47 +00001189 case UnaryOperator::PreInc:
Chris Lattner197a3382010-01-09 21:44:40 +00001190 case UnaryOperator::PreDec: {
1191 LValue LV = EmitLValue(E->getSubExpr());
1192 bool isInc = E->getOpcode() == UnaryOperator::PreInc;
1193
1194 if (E->getType()->isAnyComplexType())
1195 EmitComplexPrePostIncDec(E, LV, isInc, true/*isPre*/);
1196 else
1197 EmitScalarPrePostIncDec(E, LV, isInc, true/*isPre*/);
1198 return LV;
1199 }
Eli Friedmane401cd52009-11-09 04:20:47 +00001200 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001201}
1202
1203LValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) {
John McCall0953e762009-09-24 19:53:00 +00001204 return LValue::MakeAddr(CGM.GetAddrOfConstantStringFromLiteral(E),
1205 Qualifiers());
Reid Spencer5f016e22007-07-11 17:01:13 +00001206}
1207
Chris Lattnereaf2bb82009-02-24 22:18:39 +00001208LValue CodeGenFunction::EmitObjCEncodeExprLValue(const ObjCEncodeExpr *E) {
John McCall0953e762009-09-24 19:53:00 +00001209 return LValue::MakeAddr(CGM.GetAddrOfConstantStringFromObjCEncode(E),
1210 Qualifiers());
Chris Lattnereaf2bb82009-02-24 22:18:39 +00001211}
1212
1213
Daniel Dunbar662b71e2008-10-17 21:58:32 +00001214LValue CodeGenFunction::EmitPredefinedFunctionName(unsigned Type) {
Anders Carlsson22742662007-07-21 05:21:51 +00001215 std::string GlobalVarName;
Daniel Dunbar662b71e2008-10-17 21:58:32 +00001216
1217 switch (Type) {
Chris Lattnereb99b012009-10-28 17:39:19 +00001218 default: assert(0 && "Invalid type");
Chris Lattnereaf2bb82009-02-24 22:18:39 +00001219 case PredefinedExpr::Func:
1220 GlobalVarName = "__func__.";
1221 break;
1222 case PredefinedExpr::Function:
1223 GlobalVarName = "__FUNCTION__.";
1224 break;
1225 case PredefinedExpr::PrettyFunction:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00001226 GlobalVarName = "__PRETTY_FUNCTION__.";
1227 break;
Anders Carlsson22742662007-07-21 05:21:51 +00001228 }
Daniel Dunbar662b71e2008-10-17 21:58:32 +00001229
Daniel Dunbar0a23d762009-09-12 23:06:21 +00001230 llvm::StringRef FnName = CurFn->getName();
1231 if (FnName.startswith("\01"))
1232 FnName = FnName.substr(1);
1233 GlobalVarName += FnName;
1234
Anders Carlsson3a082d82009-09-08 18:24:21 +00001235 std::string FunctionName =
Anders Carlsson848fa642010-02-11 18:20:28 +00001236 PredefinedExpr::ComputeName((PredefinedExpr::IdentType)Type, CurCodeDecl);
Daniel Dunbar662b71e2008-10-17 21:58:32 +00001237
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001238 llvm::Constant *C =
Daniel Dunbar662b71e2008-10-17 21:58:32 +00001239 CGM.GetAddrOfConstantCString(FunctionName, GlobalVarName.c_str());
John McCall0953e762009-09-24 19:53:00 +00001240 return LValue::MakeAddr(C, Qualifiers());
Daniel Dunbar662b71e2008-10-17 21:58:32 +00001241}
1242
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001243LValue CodeGenFunction::EmitPredefinedLValue(const PredefinedExpr *E) {
Daniel Dunbar662b71e2008-10-17 21:58:32 +00001244 switch (E->getIdentType()) {
1245 default:
1246 return EmitUnsupportedLValue(E, "predefined expression");
1247 case PredefinedExpr::Func:
1248 case PredefinedExpr::Function:
1249 case PredefinedExpr::PrettyFunction:
1250 return EmitPredefinedFunctionName(E->getIdentType());
1251 }
Anders Carlsson22742662007-07-21 05:21:51 +00001252}
1253
Mike Stumpd8af3602009-12-15 01:22:35 +00001254llvm::BasicBlock *CodeGenFunction::getTrapBB() {
Mike Stump41513442009-12-15 00:59:40 +00001255 const CodeGenOptions &GCO = CGM.getCodeGenOpts();
1256
1257 // If we are not optimzing, don't collapse all calls to trap in the function
1258 // to the same call, that way, in the debugger they can see which operation
1259 // did in fact fail. If we are optimizing, we collpase all call to trap down
1260 // to just one per function to save on codesize.
1261 if (GCO.OptimizationLevel
1262 && TrapBB)
Mike Stump15037ca2009-12-15 00:35:12 +00001263 return TrapBB;
Mike Stump9c276ae2009-12-12 01:27:46 +00001264
1265 llvm::BasicBlock *Cont = 0;
1266 if (HaveInsertPoint()) {
1267 Cont = createBasicBlock("cont");
1268 EmitBranch(Cont);
1269 }
Mike Stump15037ca2009-12-15 00:35:12 +00001270 TrapBB = createBasicBlock("trap");
1271 EmitBlock(TrapBB);
1272
1273 llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::trap, 0, 0);
1274 llvm::CallInst *TrapCall = Builder.CreateCall(F);
1275 TrapCall->setDoesNotReturn();
1276 TrapCall->setDoesNotThrow();
Mike Stump9c276ae2009-12-12 01:27:46 +00001277 Builder.CreateUnreachable();
1278
1279 if (Cont)
1280 EmitBlock(Cont);
Mike Stump15037ca2009-12-15 00:35:12 +00001281 return TrapBB;
Mike Stump9c276ae2009-12-12 01:27:46 +00001282}
1283
Reid Spencer5f016e22007-07-11 17:01:13 +00001284LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) {
Ted Kremenek23245122007-08-20 16:18:38 +00001285 // The index must always be an integer, which is not an aggregate. Emit it.
Chris Lattner7f02f722007-08-24 05:35:26 +00001286 llvm::Value *Idx = EmitScalarExpr(E->getIdx());
Eli Friedman61d004a2009-06-06 19:09:26 +00001287 QualType IdxTy = E->getIdx()->getType();
1288 bool IdxSigned = IdxTy->isSignedIntegerType();
1289
Reid Spencer5f016e22007-07-11 17:01:13 +00001290 // If the base is a vector type, then we are forming a vector element lvalue
1291 // with this subscript.
Eli Friedman1e692ac2008-06-13 23:01:12 +00001292 if (E->getBase()->getType()->isVectorType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001293 // Emit the vector as an lvalue to get its address.
Eli Friedman1e692ac2008-06-13 23:01:12 +00001294 LValue LHS = EmitLValue(E->getBase());
Ted Kremenek23245122007-08-20 16:18:38 +00001295 assert(LHS.isSimple() && "Can only subscript lvalue vectors here!");
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001296 Idx = Builder.CreateIntCast(Idx,
Owen Anderson0032b272009-08-13 21:57:51 +00001297 llvm::Type::getInt32Ty(VMContext), IdxSigned, "vidx");
Eli Friedman1e692ac2008-06-13 23:01:12 +00001298 return LValue::MakeVectorElt(LHS.getAddress(), Idx,
John McCall0953e762009-09-24 19:53:00 +00001299 E->getBase()->getType().getCVRQualifiers());
Reid Spencer5f016e22007-07-11 17:01:13 +00001300 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001301
Ted Kremenek23245122007-08-20 16:18:38 +00001302 // The base must be a pointer, which is not an aggregate. Emit it.
Chris Lattner7f02f722007-08-24 05:35:26 +00001303 llvm::Value *Base = EmitScalarExpr(E->getBase());
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001304
Ted Kremenek23245122007-08-20 16:18:38 +00001305 // Extend or truncate the index type to 32 or 64-bits.
Reid Spencer5f016e22007-07-11 17:01:13 +00001306 unsigned IdxBitwidth = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
Sanjiv Gupta7cabee52009-04-24 02:40:57 +00001307 if (IdxBitwidth != LLVMPointerWidth)
Owen Anderson0032b272009-08-13 21:57:51 +00001308 Idx = Builder.CreateIntCast(Idx,
1309 llvm::IntegerType::get(VMContext, LLVMPointerWidth),
Reid Spencer5f016e22007-07-11 17:01:13 +00001310 IdxSigned, "idxprom");
1311
Mike Stumpb14e62d2009-12-16 02:57:00 +00001312 // FIXME: As llvm implements the object size checking, this can come out.
Mike Stump9c276ae2009-12-12 01:27:46 +00001313 if (CatchUndefined) {
Mike Stumpb14e62d2009-12-16 02:57:00 +00001314 if (const ImplicitCastExpr *ICE=dyn_cast<ImplicitCastExpr>(E->getBase())) {
Mike Stump9c276ae2009-12-12 01:27:46 +00001315 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr())) {
1316 if (ICE->getCastKind() == CastExpr::CK_ArrayToPointerDecay) {
1317 if (const ConstantArrayType *CAT
1318 = getContext().getAsConstantArrayType(DRE->getType())) {
1319 llvm::APInt Size = CAT->getSize();
1320 llvm::BasicBlock *Cont = createBasicBlock("cont");
Mike Stump750c85e2009-12-14 22:14:31 +00001321 Builder.CreateCondBr(Builder.CreateICmpULE(Idx,
Mike Stump9c276ae2009-12-12 01:27:46 +00001322 llvm::ConstantInt::get(Idx->getType(), Size)),
Mike Stump15037ca2009-12-15 00:35:12 +00001323 Cont, getTrapBB());
Mike Stump96a063a2009-12-14 20:52:00 +00001324 EmitBlock(Cont);
Mike Stump9c276ae2009-12-12 01:27:46 +00001325 }
1326 }
1327 }
1328 }
1329 }
1330
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001331 // We know that the pointer points to a type of the correct size, unless the
1332 // size is a VLA or Objective-C interface.
Daniel Dunbar2a866252009-04-25 05:08:32 +00001333 llvm::Value *Address = 0;
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001334 if (const VariableArrayType *VAT =
Anders Carlsson8b33c082008-12-21 00:11:23 +00001335 getContext().getAsVariableArrayType(E->getType())) {
Chris Lattner881eb9c2009-08-14 23:43:22 +00001336 llvm::Value *VLASize = GetVLASize(VAT);
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001337
Anders Carlsson8b33c082008-12-21 00:11:23 +00001338 Idx = Builder.CreateMul(Idx, VLASize);
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001339
Anders Carlsson6183a992008-12-21 03:44:36 +00001340 QualType BaseType = getContext().getBaseElementType(VAT);
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001341
Ken Dyck199c3d62010-01-11 17:06:35 +00001342 CharUnits BaseTypeSize = getContext().getTypeSizeInChars(BaseType);
Anders Carlsson8b33c082008-12-21 00:11:23 +00001343 Idx = Builder.CreateUDiv(Idx,
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001344 llvm::ConstantInt::get(Idx->getType(),
Ken Dyck199c3d62010-01-11 17:06:35 +00001345 BaseTypeSize.getQuantity()));
Dan Gohman664f8932009-08-12 00:33:55 +00001346 Address = Builder.CreateInBoundsGEP(Base, Idx, "arrayidx");
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001347 } else if (const ObjCInterfaceType *OIT =
Daniel Dunbar2a866252009-04-25 05:08:32 +00001348 dyn_cast<ObjCInterfaceType>(E->getType())) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001349 llvm::Value *InterfaceSize =
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001350 llvm::ConstantInt::get(Idx->getType(),
Ken Dyck199c3d62010-01-11 17:06:35 +00001351 getContext().getTypeSizeInChars(OIT).getQuantity());
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001352
Daniel Dunbar2a866252009-04-25 05:08:32 +00001353 Idx = Builder.CreateMul(Idx, InterfaceSize);
1354
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00001355 const llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(VMContext);
Dan Gohman664f8932009-08-12 00:33:55 +00001356 Address = Builder.CreateGEP(Builder.CreateBitCast(Base, i8PTy),
Daniel Dunbar2a866252009-04-25 05:08:32 +00001357 Idx, "arrayidx");
1358 Address = Builder.CreateBitCast(Address, Base->getType());
1359 } else {
Dan Gohman664f8932009-08-12 00:33:55 +00001360 Address = Builder.CreateInBoundsGEP(Base, Idx, "arrayidx");
Anders Carlsson8b33c082008-12-21 00:11:23 +00001361 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001362
Steve Naroff14108da2009-07-10 23:34:53 +00001363 QualType T = E->getBase()->getType()->getPointeeType();
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001364 assert(!T.isNull() &&
Steve Naroff14108da2009-07-10 23:34:53 +00001365 "CodeGenFunction::EmitArraySubscriptExpr(): Illegal base type");
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001366
John McCall0953e762009-09-24 19:53:00 +00001367 Qualifiers Quals = MakeQualifiers(T);
1368 Quals.setAddressSpace(E->getBase()->getType().getAddressSpace());
1369
1370 LValue LV = LValue::MakeAddr(Address, Quals);
Fariborz Jahanian643887a2009-02-21 23:37:19 +00001371 if (getContext().getLangOptions().ObjC1 &&
Fariborz Jahanianb123ea32009-09-16 21:37:16 +00001372 getContext().getLangOptions().getGCMode() != LangOptions::NonGC) {
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001373 LValue::SetObjCNonGC(LV, !E->isOBJCGCCandidate(getContext()));
Fariborz Jahanianb123ea32009-09-16 21:37:16 +00001374 setObjCGCLValueClass(getContext(), E, LV);
1375 }
Fariborz Jahanian643887a2009-02-21 23:37:19 +00001376 return LV;
Reid Spencer5f016e22007-07-11 17:01:13 +00001377}
1378
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001379static
Owen Andersona1cf15f2009-07-14 23:10:40 +00001380llvm::Constant *GenerateConstantVector(llvm::LLVMContext &VMContext,
1381 llvm::SmallVector<unsigned, 4> &Elts) {
Chris Lattner998eab12009-12-23 21:31:11 +00001382 llvm::SmallVector<llvm::Constant*, 4> CElts;
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001383
Nate Begeman3b8d1162008-05-13 21:03:02 +00001384 for (unsigned i = 0, e = Elts.size(); i != e; ++i)
Owen Anderson0032b272009-08-13 21:57:51 +00001385 CElts.push_back(llvm::ConstantInt::get(
1386 llvm::Type::getInt32Ty(VMContext), Elts[i]));
Nate Begeman3b8d1162008-05-13 21:03:02 +00001387
Owen Anderson4a289322009-07-28 21:22:35 +00001388 return llvm::ConstantVector::get(&CElts[0], CElts.size());
Nate Begeman3b8d1162008-05-13 21:03:02 +00001389}
1390
Chris Lattner349aaec2007-08-02 23:37:31 +00001391LValue CodeGenFunction::
Nate Begeman213541a2008-04-18 23:10:10 +00001392EmitExtVectorElementExpr(const ExtVectorElementExpr *E) {
Chris Lattner998eab12009-12-23 21:31:11 +00001393 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
1394
Chris Lattner349aaec2007-08-02 23:37:31 +00001395 // Emit the base vector as an l-value.
Chris Lattner73525de2009-02-16 21:11:58 +00001396 LValue Base;
1397
1398 // ExtVectorElementExpr's base can either be a vector or pointer to vector.
Chris Lattner998eab12009-12-23 21:31:11 +00001399 if (E->isArrow()) {
1400 // If it is a pointer to a vector, emit the address and form an lvalue with
1401 // it.
Chris Lattner2140e902009-02-16 22:14:05 +00001402 llvm::Value *Ptr = EmitScalarExpr(E->getBase());
Chris Lattner998eab12009-12-23 21:31:11 +00001403 const PointerType *PT = E->getBase()->getType()->getAs<PointerType>();
John McCall0953e762009-09-24 19:53:00 +00001404 Qualifiers Quals = MakeQualifiers(PT->getPointeeType());
1405 Quals.removeObjCGCAttr();
1406 Base = LValue::MakeAddr(Ptr, Quals);
Chris Lattner998eab12009-12-23 21:31:11 +00001407 } else if (E->getBase()->isLvalue(getContext()) == Expr::LV_Valid) {
1408 // Otherwise, if the base is an lvalue ( as in the case of foo.x.x),
1409 // emit the base as an lvalue.
1410 assert(E->getBase()->getType()->isVectorType());
1411 Base = EmitLValue(E->getBase());
1412 } else {
1413 // Otherwise, the base is a normal rvalue (as in (V+V).x), emit it as such.
Daniel Dunbar302c3c22010-01-04 18:02:28 +00001414 assert(E->getBase()->getType()->getAs<VectorType>() &&
1415 "Result must be a vector");
Chris Lattner998eab12009-12-23 21:31:11 +00001416 llvm::Value *Vec = EmitScalarExpr(E->getBase());
1417
Chris Lattner0ad57fb2009-12-23 21:33:41 +00001418 // Store the vector to memory (because LValue wants an address).
Daniel Dunbar195337d2010-02-09 02:48:28 +00001419 llvm::Value *VecMem = CreateMemTemp(E->getBase()->getType());
Chris Lattner998eab12009-12-23 21:31:11 +00001420 Builder.CreateStore(Vec, VecMem);
Chris Lattner0ad57fb2009-12-23 21:33:41 +00001421 Base = LValue::MakeAddr(VecMem, Qualifiers());
Chris Lattner998eab12009-12-23 21:31:11 +00001422 }
1423
Nate Begeman3b8d1162008-05-13 21:03:02 +00001424 // Encode the element access list into a vector of unsigned indices.
1425 llvm::SmallVector<unsigned, 4> Indices;
1426 E->getEncodedElementAccess(Indices);
1427
1428 if (Base.isSimple()) {
Owen Andersona1cf15f2009-07-14 23:10:40 +00001429 llvm::Constant *CV = GenerateConstantVector(VMContext, Indices);
Eli Friedman1e692ac2008-06-13 23:01:12 +00001430 return LValue::MakeExtVectorElt(Base.getAddress(), CV,
John McCall0953e762009-09-24 19:53:00 +00001431 Base.getVRQualifiers());
Nate Begeman3b8d1162008-05-13 21:03:02 +00001432 }
1433 assert(Base.isExtVectorElt() && "Can only subscript lvalue vec elts here!");
1434
1435 llvm::Constant *BaseElts = Base.getExtVectorElts();
1436 llvm::SmallVector<llvm::Constant *, 4> CElts;
1437
1438 for (unsigned i = 0, e = Indices.size(); i != e; ++i) {
1439 if (isa<llvm::ConstantAggregateZero>(BaseElts))
Chris Lattner67665862009-10-28 05:12:07 +00001440 CElts.push_back(llvm::ConstantInt::get(Int32Ty, 0));
Nate Begeman3b8d1162008-05-13 21:03:02 +00001441 else
Chris Lattner67665862009-10-28 05:12:07 +00001442 CElts.push_back(cast<llvm::Constant>(BaseElts->getOperand(Indices[i])));
Nate Begeman3b8d1162008-05-13 21:03:02 +00001443 }
Owen Anderson4a289322009-07-28 21:22:35 +00001444 llvm::Constant *CV = llvm::ConstantVector::get(&CElts[0], CElts.size());
Eli Friedman1e692ac2008-06-13 23:01:12 +00001445 return LValue::MakeExtVectorElt(Base.getExtVectorAddr(), CV,
John McCall0953e762009-09-24 19:53:00 +00001446 Base.getVRQualifiers());
Chris Lattner349aaec2007-08-02 23:37:31 +00001447}
1448
Devang Patelb9b00ad2007-10-23 20:28:39 +00001449LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) {
Fariborz Jahanian4f676ed2009-02-21 00:30:43 +00001450 bool isNonGC = false;
Devang Patel126a8562007-10-24 22:26:28 +00001451 Expr *BaseExpr = E->getBase();
Devang Patel126a8562007-10-24 22:26:28 +00001452 llvm::Value *BaseValue = NULL;
John McCall0953e762009-09-24 19:53:00 +00001453 Qualifiers BaseQuals;
Eli Friedman1e692ac2008-06-13 23:01:12 +00001454
Chris Lattner12f65f62007-12-02 18:52:07 +00001455 // If this is s.x, emit s as an lvalue. If it is s->x, emit s as a scalar.
Devang Patelfe2419a2007-12-11 21:33:16 +00001456 if (E->isArrow()) {
Devang Patel0a961182007-10-26 18:15:21 +00001457 BaseValue = EmitScalarExpr(BaseExpr);
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001458 const PointerType *PTy =
Ted Kremenek6217b802009-07-29 21:53:49 +00001459 BaseExpr->getType()->getAs<PointerType>();
John McCall0953e762009-09-24 19:53:00 +00001460 BaseQuals = PTy->getPointeeType().getQualifiers();
Fariborz Jahaniand2e1eb02009-09-01 17:02:21 +00001461 } else if (isa<ObjCPropertyRefExpr>(BaseExpr->IgnoreParens()) ||
1462 isa<ObjCImplicitSetterGetterRefExpr>(
1463 BaseExpr->IgnoreParens())) {
Fariborz Jahanian35c33292009-01-12 23:27:26 +00001464 RValue RV = EmitObjCPropertyGet(BaseExpr);
1465 BaseValue = RV.getAggregateAddr();
John McCall0953e762009-09-24 19:53:00 +00001466 BaseQuals = BaseExpr->getType().getQualifiers();
Chris Lattner1bd885e2009-02-16 22:25:49 +00001467 } else {
Chris Lattner12f65f62007-12-02 18:52:07 +00001468 LValue BaseLV = EmitLValue(BaseExpr);
Fariborz Jahanian4f676ed2009-02-21 00:30:43 +00001469 if (BaseLV.isNonGC())
1470 isNonGC = true;
Chris Lattner12f65f62007-12-02 18:52:07 +00001471 // FIXME: this isn't right for bitfields.
1472 BaseValue = BaseLV.getAddress();
Fariborz Jahaniana91d6a62009-07-29 00:44:13 +00001473 QualType BaseTy = BaseExpr->getType();
John McCall0953e762009-09-24 19:53:00 +00001474 BaseQuals = BaseTy.getQualifiers();
Chris Lattner12f65f62007-12-02 18:52:07 +00001475 }
Devang Patelb9b00ad2007-10-23 20:28:39 +00001476
Anders Carlssonce53f7d2009-11-07 23:06:58 +00001477 NamedDecl *ND = E->getMemberDecl();
1478 if (FieldDecl *Field = dyn_cast<FieldDecl>(ND)) {
Anders Carlssone6d2a532010-01-29 05:05:36 +00001479 LValue LV = EmitLValueForField(BaseValue, Field,
Anders Carlssonce53f7d2009-11-07 23:06:58 +00001480 BaseQuals.getCVRQualifiers());
1481 LValue::SetObjCNonGC(LV, isNonGC);
1482 setObjCGCLValueClass(getContext(), E, LV);
1483 return LV;
1484 }
1485
Anders Carlsson589f9e32009-11-07 23:16:50 +00001486 if (VarDecl *VD = dyn_cast<VarDecl>(ND))
1487 return EmitGlobalVarDeclLValue(*this, E, VD);
Eli Friedman9a146302009-11-26 06:08:14 +00001488
1489 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
1490 return EmitFunctionDeclLValue(*this, E, FD);
1491
Anders Carlssonce53f7d2009-11-07 23:06:58 +00001492 assert(false && "Unhandled member declaration!");
1493 return LValue();
Eli Friedman472778e2008-02-09 08:50:58 +00001494}
Devang Patelb9b00ad2007-10-23 20:28:39 +00001495
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +00001496LValue CodeGenFunction::EmitLValueForBitfield(llvm::Value* BaseValue,
Anders Carlsson0ed303c2009-11-17 03:57:07 +00001497 const FieldDecl* Field,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001498 unsigned CVRQualifiers) {
Daniel Dunbar198bcb42010-03-31 01:09:11 +00001499 const CGRecordLayout &RL =
1500 CGM.getTypes().getCGRecordLayout(Field->getParent());
Daniel Dunbar2eec0b22010-04-05 16:20:44 +00001501 const CGBitFieldInfo &Info = RL.getBitFieldInfo(Field);
Daniel Dunbar7f289642010-04-08 02:59:45 +00001502 return LValue::MakeBitfield(BaseValue, Info,
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +00001503 Field->getType().getCVRQualifiers()|CVRQualifiers);
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +00001504}
1505
Eli Friedman472778e2008-02-09 08:50:58 +00001506LValue CodeGenFunction::EmitLValueForField(llvm::Value* BaseValue,
Anders Carlsson0ed303c2009-11-17 03:57:07 +00001507 const FieldDecl* Field,
Mike Stump1eb44332009-09-09 15:08:12 +00001508 unsigned CVRQualifiers) {
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +00001509 if (Field->isBitField())
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001510 return EmitLValueForBitfield(BaseValue, Field, CVRQualifiers);
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001511
Daniel Dunbar198bcb42010-03-31 01:09:11 +00001512 const CGRecordLayout &RL =
1513 CGM.getTypes().getCGRecordLayout(Field->getParent());
1514 unsigned idx = RL.getLLVMFieldNo(Field);
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +00001515 llvm::Value *V = Builder.CreateStructGEP(BaseValue, idx, "tmp");
Eli Friedman1e86b342008-05-29 11:33:25 +00001516
Devang Patelabad06c2007-10-26 19:42:18 +00001517 // Match union field type.
Anders Carlssone6d2a532010-01-29 05:05:36 +00001518 if (Field->getParent()->isUnion()) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001519 const llvm::Type *FieldTy =
Eli Friedman1e692ac2008-06-13 23:01:12 +00001520 CGM.getTypes().ConvertTypeForMem(Field->getType());
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001521 const llvm::PointerType * BaseTy =
Devang Patele9b8c0a2007-10-30 20:59:40 +00001522 cast<llvm::PointerType>(BaseValue->getType());
Eli Friedman788d5712008-05-21 13:24:44 +00001523 unsigned AS = BaseTy->getAddressSpace();
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001524 V = Builder.CreateBitCast(V,
1525 llvm::PointerType::get(FieldTy, AS),
Eli Friedman788d5712008-05-21 13:24:44 +00001526 "tmp");
Devang Patelabad06c2007-10-26 19:42:18 +00001527 }
Eli Friedman2be58612009-05-30 21:09:44 +00001528 if (Field->getType()->isReferenceType())
1529 V = Builder.CreateLoad(V, "tmp");
John McCall0953e762009-09-24 19:53:00 +00001530
1531 Qualifiers Quals = MakeQualifiers(Field->getType());
1532 Quals.addCVRQualifiers(CVRQualifiers);
Fariborz Jahanianfd02ed72009-09-21 18:54:29 +00001533 // __weak attribute on a field is ignored.
John McCall0953e762009-09-24 19:53:00 +00001534 if (Quals.getObjCGCAttr() == Qualifiers::Weak)
1535 Quals.removeObjCGCAttr();
Fariborz Jahanianfd02ed72009-09-21 18:54:29 +00001536
John McCall0953e762009-09-24 19:53:00 +00001537 return LValue::MakeAddr(V, Quals);
Devang Patelb9b00ad2007-10-23 20:28:39 +00001538}
1539
Anders Carlsson06a29702010-01-29 05:24:29 +00001540LValue
1541CodeGenFunction::EmitLValueForFieldInitialization(llvm::Value* BaseValue,
1542 const FieldDecl* Field,
1543 unsigned CVRQualifiers) {
1544 QualType FieldType = Field->getType();
1545
1546 if (!FieldType->isReferenceType())
1547 return EmitLValueForField(BaseValue, Field, CVRQualifiers);
1548
Daniel Dunbar198bcb42010-03-31 01:09:11 +00001549 const CGRecordLayout &RL =
1550 CGM.getTypes().getCGRecordLayout(Field->getParent());
1551 unsigned idx = RL.getLLVMFieldNo(Field);
Anders Carlsson06a29702010-01-29 05:24:29 +00001552 llvm::Value *V = Builder.CreateStructGEP(BaseValue, idx, "tmp");
1553
1554 assert(!FieldType.getObjCGCAttr() && "fields cannot have GC attrs");
1555
1556 return LValue::MakeAddr(V, MakeQualifiers(FieldType));
1557}
1558
Chris Lattner75dfeda2009-03-18 18:28:57 +00001559LValue CodeGenFunction::EmitCompoundLiteralLValue(const CompoundLiteralExpr* E){
Daniel Dunbar15006572010-02-16 19:43:39 +00001560 llvm::Value *DeclPtr = CreateMemTemp(E->getType(), ".compoundliteral");
Eli Friedman06e863f2008-05-13 23:18:27 +00001561 const Expr* InitExpr = E->getInitializer();
John McCall0953e762009-09-24 19:53:00 +00001562 LValue Result = LValue::MakeAddr(DeclPtr, MakeQualifiers(E->getType()));
Eli Friedman06e863f2008-05-13 23:18:27 +00001563
Chris Lattnereb99b012009-10-28 17:39:19 +00001564 if (E->getType()->isComplexType())
Eli Friedman06e863f2008-05-13 23:18:27 +00001565 EmitComplexExprIntoAddr(InitExpr, DeclPtr, false);
Chris Lattnereb99b012009-10-28 17:39:19 +00001566 else if (hasAggregateLLVMType(E->getType()))
Eli Friedman06e863f2008-05-13 23:18:27 +00001567 EmitAnyExpr(InitExpr, DeclPtr, false);
Chris Lattnereb99b012009-10-28 17:39:19 +00001568 else
Eli Friedman06e863f2008-05-13 23:18:27 +00001569 EmitStoreThroughLValue(EmitAnyExpr(InitExpr), Result, E->getType());
Eli Friedman06e863f2008-05-13 23:18:27 +00001570
1571 return Result;
1572}
1573
Anders Carlsson6fcec8b2009-09-15 16:35:24 +00001574LValue
1575CodeGenFunction::EmitConditionalOperatorLValue(const ConditionalOperator* E) {
1576 if (E->isLvalue(getContext()) == Expr::LV_Valid) {
Eli Friedmanab189952009-12-25 05:29:40 +00001577 if (int Cond = ConstantFoldsToSimpleInteger(E->getCond())) {
1578 Expr *Live = Cond == 1 ? E->getLHS() : E->getRHS();
1579 if (Live)
1580 return EmitLValue(Live);
1581 }
1582
1583 if (!E->getLHS())
1584 return EmitUnsupportedLValue(E, "conditional operator with missing LHS");
1585
Anders Carlsson6fcec8b2009-09-15 16:35:24 +00001586 llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true");
1587 llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false");
1588 llvm::BasicBlock *ContBlock = createBasicBlock("cond.end");
1589
Eli Friedman8e274bd2009-12-25 06:17:05 +00001590 EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock);
Anders Carlsson6fcec8b2009-09-15 16:35:24 +00001591
Anders Carlssonc1b32f62010-02-04 17:26:01 +00001592 // Any temporaries created here are conditional.
1593 BeginConditionalBranch();
Anders Carlsson6fcec8b2009-09-15 16:35:24 +00001594 EmitBlock(LHSBlock);
Anders Carlsson6fcec8b2009-09-15 16:35:24 +00001595 LValue LHS = EmitLValue(E->getLHS());
Anders Carlssonc1b32f62010-02-04 17:26:01 +00001596 EndConditionalBranch();
1597
Anders Carlsson6fcec8b2009-09-15 16:35:24 +00001598 if (!LHS.isSimple())
1599 return EmitUnsupportedLValue(E, "conditional operator");
1600
Daniel Dunbar195337d2010-02-09 02:48:28 +00001601 // FIXME: We shouldn't need an alloca for this.
Chris Lattnereb99b012009-10-28 17:39:19 +00001602 llvm::Value *Temp = CreateTempAlloca(LHS.getAddress()->getType(),"condtmp");
Anders Carlsson6fcec8b2009-09-15 16:35:24 +00001603 Builder.CreateStore(LHS.getAddress(), Temp);
1604 EmitBranch(ContBlock);
1605
Anders Carlssonc1b32f62010-02-04 17:26:01 +00001606 // Any temporaries created here are conditional.
1607 BeginConditionalBranch();
Anders Carlsson6fcec8b2009-09-15 16:35:24 +00001608 EmitBlock(RHSBlock);
1609 LValue RHS = EmitLValue(E->getRHS());
Anders Carlssonc1b32f62010-02-04 17:26:01 +00001610 EndConditionalBranch();
Anders Carlsson6fcec8b2009-09-15 16:35:24 +00001611 if (!RHS.isSimple())
1612 return EmitUnsupportedLValue(E, "conditional operator");
1613
1614 Builder.CreateStore(RHS.getAddress(), Temp);
1615 EmitBranch(ContBlock);
1616
1617 EmitBlock(ContBlock);
1618
1619 Temp = Builder.CreateLoad(Temp, "lv");
John McCall0953e762009-09-24 19:53:00 +00001620 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
Anders Carlsson6fcec8b2009-09-15 16:35:24 +00001621 }
1622
Daniel Dunbar90345582009-03-24 02:38:23 +00001623 // ?: here should be an aggregate.
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001624 assert((hasAggregateLLVMType(E->getType()) &&
Daniel Dunbar90345582009-03-24 02:38:23 +00001625 !E->getType()->isAnyComplexType()) &&
1626 "Unexpected conditional operator!");
1627
Daniel Dunbar18aba0d2010-02-05 19:38:31 +00001628 return EmitAggExprToLValue(E);
Daniel Dunbar90345582009-03-24 02:38:23 +00001629}
1630
Mike Stumpc849c052009-11-16 06:50:58 +00001631/// EmitCastLValue - Casts are never lvalues unless that cast is a dynamic_cast.
1632/// If the cast is a dynamic_cast, we can have the usual lvalue result,
1633/// otherwise if a cast is needed by the code generator in an lvalue context,
1634/// then it must mean that we need the address of an aggregate in order to
1635/// access one of its fields. This can happen for all the reasons that casts
1636/// are permitted with aggregate result, including noop aggregate casts, and
1637/// cast from scalar to union.
Chris Lattner75dfeda2009-03-18 18:28:57 +00001638LValue CodeGenFunction::EmitCastLValue(const CastExpr *E) {
Anders Carlsson0ee33cf2009-09-12 16:16:49 +00001639 switch (E->getCastKind()) {
1640 default:
Eli Friedmaneaae78a2009-11-16 05:48:01 +00001641 return EmitUnsupportedLValue(E, "unexpected cast lvalue");
1642
Mike Stumpc849c052009-11-16 06:50:58 +00001643 case CastExpr::CK_Dynamic: {
1644 LValue LV = EmitLValue(E->getSubExpr());
1645 llvm::Value *V = LV.getAddress();
1646 const CXXDynamicCastExpr *DCE = cast<CXXDynamicCastExpr>(E);
1647 return LValue::MakeAddr(EmitDynamicCast(V, DCE),
1648 MakeQualifiers(E->getType()));
1649 }
1650
Anders Carlsson0ee33cf2009-09-12 16:16:49 +00001651 case CastExpr::CK_NoOp:
1652 case CastExpr::CK_ConstructorConversion:
1653 case CastExpr::CK_UserDefinedConversion:
Fariborz Jahaniana7fa7cd2009-12-15 21:34:52 +00001654 case CastExpr::CK_AnyPointerToObjCPointerCast:
Chris Lattner75dfeda2009-03-18 18:28:57 +00001655 return EmitLValue(E->getSubExpr());
Anders Carlsson0ee33cf2009-09-12 16:16:49 +00001656
John McCall23cba802010-03-30 23:58:03 +00001657 case CastExpr::CK_UncheckedDerivedToBase:
Anders Carlsson0ee33cf2009-09-12 16:16:49 +00001658 case CastExpr::CK_DerivedToBase: {
1659 const RecordType *DerivedClassTy =
1660 E->getSubExpr()->getType()->getAs<RecordType>();
1661 CXXRecordDecl *DerivedClassDecl =
1662 cast<CXXRecordDecl>(DerivedClassTy->getDecl());
Chris Lattner75dfeda2009-03-18 18:28:57 +00001663
Anders Carlsson0ee33cf2009-09-12 16:16:49 +00001664 const RecordType *BaseClassTy = E->getType()->getAs<RecordType>();
1665 CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseClassTy->getDecl());
1666
1667 LValue LV = EmitLValue(E->getSubExpr());
1668
1669 // Perform the derived-to-base conversion
1670 llvm::Value *Base =
Anders Carlssona3697c92009-11-23 17:57:54 +00001671 GetAddressOfBaseClass(LV.getAddress(), DerivedClassDecl,
1672 BaseClassDecl, /*NullCheckValue=*/false);
Anders Carlsson0ee33cf2009-09-12 16:16:49 +00001673
John McCall0953e762009-09-24 19:53:00 +00001674 return LValue::MakeAddr(Base, MakeQualifiers(E->getType()));
Anders Carlsson0ee33cf2009-09-12 16:16:49 +00001675 }
Daniel Dunbarb2cd7772010-02-05 20:02:42 +00001676 case CastExpr::CK_ToUnion:
1677 return EmitAggExprToLValue(E);
Eli Friedmaneaae78a2009-11-16 05:48:01 +00001678 case CastExpr::CK_BaseToDerived: {
Anders Carlssona3697c92009-11-23 17:57:54 +00001679 const RecordType *BaseClassTy =
1680 E->getSubExpr()->getType()->getAs<RecordType>();
1681 CXXRecordDecl *BaseClassDecl =
1682 cast<CXXRecordDecl>(BaseClassTy->getDecl());
1683
1684 const RecordType *DerivedClassTy = E->getType()->getAs<RecordType>();
1685 CXXRecordDecl *DerivedClassDecl =
1686 cast<CXXRecordDecl>(DerivedClassTy->getDecl());
1687
1688 LValue LV = EmitLValue(E->getSubExpr());
1689
1690 // Perform the base-to-derived conversion
1691 llvm::Value *Derived =
1692 GetAddressOfDerivedClass(LV.getAddress(), BaseClassDecl,
1693 DerivedClassDecl, /*NullCheckValue=*/false);
1694
1695 return LValue::MakeAddr(Derived, MakeQualifiers(E->getType()));
Eli Friedmaneaae78a2009-11-16 05:48:01 +00001696 }
Anders Carlsson658e8122009-11-14 21:21:42 +00001697 case CastExpr::CK_BitCast: {
Eli Friedmaneaae78a2009-11-16 05:48:01 +00001698 // This must be a reinterpret_cast (or c-style equivalent).
1699 const ExplicitCastExpr *CE = cast<ExplicitCastExpr>(E);
Anders Carlsson658e8122009-11-14 21:21:42 +00001700
1701 LValue LV = EmitLValue(E->getSubExpr());
1702 llvm::Value *V = Builder.CreateBitCast(LV.getAddress(),
1703 ConvertType(CE->getTypeAsWritten()));
1704 return LValue::MakeAddr(V, MakeQualifiers(E->getType()));
1705 }
Anders Carlsson0ee33cf2009-09-12 16:16:49 +00001706 }
Chris Lattner75dfeda2009-03-18 18:28:57 +00001707}
1708
Fariborz Jahanian48620ba2009-10-20 23:29:04 +00001709LValue CodeGenFunction::EmitNullInitializationLValue(
1710 const CXXZeroInitValueExpr *E) {
1711 QualType Ty = E->getType();
Daniel Dunbar195337d2010-02-09 02:48:28 +00001712 LValue LV = LValue::MakeAddr(CreateMemTemp(Ty), MakeQualifiers(Ty));
1713 EmitMemSetToZero(LV.getAddress(), Ty);
1714 return LV;
Fariborz Jahanian48620ba2009-10-20 23:29:04 +00001715}
1716
Reid Spencer5f016e22007-07-11 17:01:13 +00001717//===--------------------------------------------------------------------===//
1718// Expression Emission
1719//===--------------------------------------------------------------------===//
1720
Chris Lattner7016a702007-08-20 22:37:10 +00001721
Anders Carlssond2490a92009-12-24 20:40:36 +00001722RValue CodeGenFunction::EmitCallExpr(const CallExpr *E,
1723 ReturnValueSlot ReturnValue) {
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +00001724 // Builtins never have block type.
Daniel Dunbarce1d38b2009-01-09 16:50:52 +00001725 if (E->getCallee()->getType()->isBlockPointerType())
Anders Carlssona1736c02009-12-24 21:13:40 +00001726 return EmitBlockCallExpr(E, ReturnValue);
Daniel Dunbarce1d38b2009-01-09 16:50:52 +00001727
Anders Carlsson774e7c62009-04-03 22:50:24 +00001728 if (const CXXMemberCallExpr *CE = dyn_cast<CXXMemberCallExpr>(E))
Anders Carlssona1736c02009-12-24 21:13:40 +00001729 return EmitCXXMemberCallExpr(CE, ReturnValue);
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001730
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +00001731 const Decl *TargetDecl = 0;
Daniel Dunbardd7b8972009-02-20 19:34:33 +00001732 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E->getCallee())) {
1733 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CE->getSubExpr())) {
1734 TargetDecl = DRE->getDecl();
1735 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(TargetDecl))
Douglas Gregor7814e6d2009-09-12 00:22:50 +00001736 if (unsigned builtinID = FD->getBuiltinID())
Daniel Dunbardd7b8972009-02-20 19:34:33 +00001737 return EmitBuiltinExpr(FD, builtinID, E);
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +00001738 }
1739 }
1740
Chris Lattner5db7ae52009-06-13 00:26:38 +00001741 if (const CXXOperatorCallExpr *CE = dyn_cast<CXXOperatorCallExpr>(E))
Anders Carlsson0f294632009-05-27 04:18:27 +00001742 if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(TargetDecl))
Anders Carlssona1736c02009-12-24 21:13:40 +00001743 return EmitCXXOperatorMemberCallExpr(CE, MD, ReturnValue);
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001744
Eli Friedmanc4451db2009-12-08 02:09:46 +00001745 if (isa<CXXPseudoDestructorExpr>(E->getCallee()->IgnoreParens())) {
Douglas Gregora71d8192009-09-04 17:36:40 +00001746 // C++ [expr.pseudo]p1:
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001747 // The result shall only be used as the operand for the function call
Douglas Gregora71d8192009-09-04 17:36:40 +00001748 // operator (), and the result of such a call has type void. The only
1749 // effect is the evaluation of the postfix-expression before the dot or
1750 // arrow.
1751 EmitScalarExpr(E->getCallee());
1752 return RValue::get(0);
1753 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001754
Chris Lattner7f02f722007-08-24 05:35:26 +00001755 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
Anders Carlssond2490a92009-12-24 20:40:36 +00001756 return EmitCall(E->getCallee()->getType(), Callee, ReturnValue,
Anders Carlsson98647712009-05-27 01:22:39 +00001757 E->arg_begin(), E->arg_end(), TargetDecl);
Chris Lattnerc5e940f2007-08-31 04:44:06 +00001758}
1759
Daniel Dunbar80e62c22008-09-04 03:20:13 +00001760LValue CodeGenFunction::EmitBinaryOperatorLValue(const BinaryOperator *E) {
Chris Lattner7a574cc2009-05-12 21:28:12 +00001761 // Comma expressions just emit their LHS then their RHS as an l-value.
1762 if (E->getOpcode() == BinaryOperator::Comma) {
1763 EmitAnyExpr(E->getLHS());
Eli Friedman130c69e2009-12-07 20:18:11 +00001764 EnsureInsertPoint();
Chris Lattner7a574cc2009-05-12 21:28:12 +00001765 return EmitLValue(E->getRHS());
1766 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001767
Fariborz Jahanian52f08bc2009-10-26 21:58:25 +00001768 if (E->getOpcode() == BinaryOperator::PtrMemD ||
1769 E->getOpcode() == BinaryOperator::PtrMemI)
Fariborz Jahanian8bfd31f2009-10-22 22:57:31 +00001770 return EmitPointerToDataMemberBinaryExpr(E);
1771
Daniel Dunbar80e62c22008-09-04 03:20:13 +00001772 // Can only get l-value for binary operator expressions which are a
1773 // simple assignment of aggregate type.
1774 if (E->getOpcode() != BinaryOperator::Assign)
1775 return EmitUnsupportedLValue(E, "binary l-value expression");
1776
Anders Carlsson86aa0cd2009-10-19 18:28:22 +00001777 if (!hasAggregateLLVMType(E->getType())) {
1778 // Emit the LHS as an l-value.
1779 LValue LV = EmitLValue(E->getLHS());
1780
1781 llvm::Value *RHS = EmitScalarExpr(E->getRHS());
1782 EmitStoreOfScalar(RHS, LV.getAddress(), LV.isVolatileQualified(),
1783 E->getType());
1784 return LV;
1785 }
1786
Daniel Dunbar18aba0d2010-02-05 19:38:31 +00001787 return EmitAggExprToLValue(E);
Daniel Dunbar80e62c22008-09-04 03:20:13 +00001788}
1789
Christopher Lamb22c940e2007-12-29 05:02:41 +00001790LValue CodeGenFunction::EmitCallExprLValue(const CallExpr *E) {
Christopher Lamb22c940e2007-12-29 05:02:41 +00001791 RValue RV = EmitCallExpr(E);
Anders Carlsson48265682009-05-27 01:45:47 +00001792
Chris Lattnereb99b012009-10-28 17:39:19 +00001793 if (!RV.isScalar())
1794 return LValue::MakeAddr(RV.getAggregateAddr(),MakeQualifiers(E->getType()));
1795
1796 assert(E->getCallReturnType()->isReferenceType() &&
1797 "Can't have a scalar return unless the return type is a "
1798 "reference type!");
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001799
Chris Lattnereb99b012009-10-28 17:39:19 +00001800 return LValue::MakeAddr(RV.getScalarVal(), MakeQualifiers(E->getType()));
Christopher Lamb22c940e2007-12-29 05:02:41 +00001801}
1802
Daniel Dunbar5b5c9ef2009-02-11 20:59:32 +00001803LValue CodeGenFunction::EmitVAArgExprLValue(const VAArgExpr *E) {
1804 // FIXME: This shouldn't require another copy.
Daniel Dunbar18aba0d2010-02-05 19:38:31 +00001805 return EmitAggExprToLValue(E);
Daniel Dunbar5b5c9ef2009-02-11 20:59:32 +00001806}
1807
Anders Carlssonb58d0172009-05-30 23:23:33 +00001808LValue CodeGenFunction::EmitCXXConstructLValue(const CXXConstructExpr *E) {
Daniel Dunbar195337d2010-02-09 02:48:28 +00001809 llvm::Value *Temp = CreateMemTemp(E->getType(), "tmp");
Anders Carlssonb58d0172009-05-30 23:23:33 +00001810 EmitCXXConstructExpr(Temp, E);
John McCall0953e762009-09-24 19:53:00 +00001811 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
Anders Carlssonb58d0172009-05-30 23:23:33 +00001812}
1813
Anders Carlssone61c9e82009-05-30 23:30:54 +00001814LValue
Mike Stumpc2e84ae2009-11-15 08:09:41 +00001815CodeGenFunction::EmitCXXTypeidLValue(const CXXTypeidExpr *E) {
1816 llvm::Value *Temp = EmitCXXTypeidExpr(E);
1817 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
1818}
1819
1820LValue
Anders Carlssone61c9e82009-05-30 23:30:54 +00001821CodeGenFunction::EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr *E) {
1822 LValue LV = EmitLValue(E->getSubExpr());
Anders Carlsson543ac0c2009-05-31 00:34:10 +00001823 PushCXXTemporary(E->getTemporary(), LV.getAddress());
Anders Carlssone61c9e82009-05-30 23:30:54 +00001824 return LV;
1825}
1826
Daniel Dunbar0a04d772008-08-23 10:51:21 +00001827LValue CodeGenFunction::EmitObjCMessageExprLValue(const ObjCMessageExpr *E) {
1828 // Can only get l-value for message expression returning aggregate type
1829 RValue RV = EmitObjCMessageExpr(E);
1830 // FIXME: can this be volatile?
John McCall0953e762009-09-24 19:53:00 +00001831 return LValue::MakeAddr(RV.getAggregateAddr(), MakeQualifiers(E->getType()));
Daniel Dunbar0a04d772008-08-23 10:51:21 +00001832}
1833
Daniel Dunbar2a031922009-04-22 05:08:15 +00001834llvm::Value *CodeGenFunction::EmitIvarOffset(const ObjCInterfaceDecl *Interface,
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +00001835 const ObjCIvarDecl *Ivar) {
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001836 return CGM.getObjCRuntime().EmitIvarOffset(*this, Interface, Ivar);
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +00001837}
1838
Fariborz Jahanian45012a72009-02-03 00:09:52 +00001839LValue CodeGenFunction::EmitLValueForIvar(QualType ObjectTy,
1840 llvm::Value *BaseValue,
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +00001841 const ObjCIvarDecl *Ivar,
1842 unsigned CVRQualifiers) {
Chris Lattner26f074b2009-04-17 17:44:48 +00001843 return CGM.getObjCRuntime().EmitObjCValueForIvar(*this, ObjectTy, BaseValue,
Daniel Dunbar525c9b72009-04-21 01:19:28 +00001844 Ivar, CVRQualifiers);
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +00001845}
1846
1847LValue CodeGenFunction::EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E) {
Anders Carlsson29b7e502008-08-25 01:53:23 +00001848 // FIXME: A lot of the code below could be shared with EmitMemberExpr.
1849 llvm::Value *BaseValue = 0;
1850 const Expr *BaseExpr = E->getBase();
John McCall0953e762009-09-24 19:53:00 +00001851 Qualifiers BaseQuals;
Fariborz Jahanian45012a72009-02-03 00:09:52 +00001852 QualType ObjectTy;
Anders Carlsson29b7e502008-08-25 01:53:23 +00001853 if (E->isArrow()) {
1854 BaseValue = EmitScalarExpr(BaseExpr);
Steve Naroff14108da2009-07-10 23:34:53 +00001855 ObjectTy = BaseExpr->getType()->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00001856 BaseQuals = ObjectTy.getQualifiers();
Anders Carlsson29b7e502008-08-25 01:53:23 +00001857 } else {
1858 LValue BaseLV = EmitLValue(BaseExpr);
1859 // FIXME: this isn't right for bitfields.
1860 BaseValue = BaseLV.getAddress();
Fariborz Jahanian45012a72009-02-03 00:09:52 +00001861 ObjectTy = BaseExpr->getType();
John McCall0953e762009-09-24 19:53:00 +00001862 BaseQuals = ObjectTy.getQualifiers();
Anders Carlsson29b7e502008-08-25 01:53:23 +00001863 }
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +00001864
Fariborz Jahaniandbf3cfd2009-09-16 23:11:23 +00001865 LValue LV =
John McCall0953e762009-09-24 19:53:00 +00001866 EmitLValueForIvar(ObjectTy, BaseValue, E->getDecl(),
1867 BaseQuals.getCVRQualifiers());
Fariborz Jahaniandbf3cfd2009-09-16 23:11:23 +00001868 setObjCGCLValueClass(getContext(), E, LV);
1869 return LV;
Chris Lattner391d77a2008-03-30 23:03:07 +00001870}
1871
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001872LValue
Daniel Dunbar85c59ed2008-08-29 08:11:39 +00001873CodeGenFunction::EmitObjCPropertyRefLValue(const ObjCPropertyRefExpr *E) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001874 // This is a special l-value that just issues sends when we load or store
1875 // through it.
Daniel Dunbar85c59ed2008-08-29 08:11:39 +00001876 return LValue::MakePropertyRef(E, E->getType().getCVRQualifiers());
1877}
1878
Chris Lattnereb99b012009-10-28 17:39:19 +00001879LValue CodeGenFunction::EmitObjCKVCRefLValue(
Fariborz Jahanian09105f52009-08-20 17:02:02 +00001880 const ObjCImplicitSetterGetterRefExpr *E) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001881 // This is a special l-value that just issues sends when we load or store
1882 // through it.
Fariborz Jahanian43f44702008-11-22 22:30:21 +00001883 return LValue::MakeKVCRef(E, E->getType().getCVRQualifiers());
1884}
1885
Chris Lattnereb99b012009-10-28 17:39:19 +00001886LValue CodeGenFunction::EmitObjCSuperExprLValue(const ObjCSuperExpr *E) {
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00001887 return EmitUnsupportedLValue(E, "use of super");
1888}
1889
Chris Lattner65459942009-04-25 19:35:26 +00001890LValue CodeGenFunction::EmitStmtExprLValue(const StmtExpr *E) {
Chris Lattner65459942009-04-25 19:35:26 +00001891 // Can only get l-value for message expression returning aggregate type
1892 RValue RV = EmitAnyExprToTemp(E);
John McCall0953e762009-09-24 19:53:00 +00001893 return LValue::MakeAddr(RV.getAggregateAddr(), MakeQualifiers(E->getType()));
Chris Lattner65459942009-04-25 19:35:26 +00001894}
1895
Anders Carlsson31777a22009-12-24 19:08:58 +00001896RValue CodeGenFunction::EmitCall(QualType CalleeType, llvm::Value *Callee,
Anders Carlssond2490a92009-12-24 20:40:36 +00001897 ReturnValueSlot ReturnValue,
Anders Carlsson98647712009-05-27 01:22:39 +00001898 CallExpr::const_arg_iterator ArgBeg,
1899 CallExpr::const_arg_iterator ArgEnd,
1900 const Decl *TargetDecl) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001901 // Get the actual function type. The callee type will always be a pointer to
1902 // function type or a block pointer type.
1903 assert(CalleeType->isFunctionPointerType() &&
Anders Carlsson8ac67a72009-04-07 18:53:02 +00001904 "Call must have function pointer type!");
1905
John McCall00a1ad92009-10-23 08:22:42 +00001906 CalleeType = getContext().getCanonicalType(CalleeType);
1907
John McCall04a67a62010-02-05 21:31:56 +00001908 const FunctionType *FnType
1909 = cast<FunctionType>(cast<PointerType>(CalleeType)->getPointeeType());
1910 QualType ResultType = FnType->getResultType();
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001911
1912 CallArgList Args;
John McCall00a1ad92009-10-23 08:22:42 +00001913 EmitCallArgs(Args, dyn_cast<FunctionProtoType>(FnType), ArgBeg, ArgEnd);
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001914
John McCall04a67a62010-02-05 21:31:56 +00001915 return EmitCall(CGM.getTypes().getFunctionInfo(Args, FnType),
Anders Carlssond2490a92009-12-24 20:40:36 +00001916 Callee, ReturnValue, Args, TargetDecl);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001917}
Fariborz Jahanian8bfd31f2009-10-22 22:57:31 +00001918
Chris Lattnereb99b012009-10-28 17:39:19 +00001919LValue CodeGenFunction::
1920EmitPointerToDataMemberBinaryExpr(const BinaryOperator *E) {
Eli Friedman1c5c1a02009-11-18 05:01:17 +00001921 llvm::Value *BaseV;
Fariborz Jahanian52f08bc2009-10-26 21:58:25 +00001922 if (E->getOpcode() == BinaryOperator::PtrMemI)
Eli Friedman1c5c1a02009-11-18 05:01:17 +00001923 BaseV = EmitScalarExpr(E->getLHS());
1924 else
1925 BaseV = EmitLValue(E->getLHS()).getAddress();
Fariborz Jahanian8bfd31f2009-10-22 22:57:31 +00001926 const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(getLLVMContext());
1927 BaseV = Builder.CreateBitCast(BaseV, i8Ty);
Eli Friedman1c5c1a02009-11-18 05:01:17 +00001928 llvm::Value *OffsetV = EmitScalarExpr(E->getRHS());
Fariborz Jahanian8bfd31f2009-10-22 22:57:31 +00001929 llvm::Value *AddV = Builder.CreateInBoundsGEP(BaseV, OffsetV, "add.ptr");
Chris Lattnereb99b012009-10-28 17:39:19 +00001930
Fariborz Jahanian8bfd31f2009-10-22 22:57:31 +00001931 QualType Ty = E->getRHS()->getType();
Chris Lattnereb99b012009-10-28 17:39:19 +00001932 Ty = Ty->getAs<MemberPointerType>()->getPointeeType();
1933
1934 const llvm::Type *PType = ConvertType(getContext().getPointerType(Ty));
Fariborz Jahanian8bfd31f2009-10-22 22:57:31 +00001935 AddV = Builder.CreateBitCast(AddV, PType);
Chris Lattnereb99b012009-10-28 17:39:19 +00001936 return LValue::MakeAddr(AddV, MakeQualifiers(Ty));
Fariborz Jahanian8bfd31f2009-10-22 22:57:31 +00001937}
1938