blob: 2684eb6e3ce12a9eb87c9734bc03d5f281d42107 [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 Dunbaraf2f62c2008-08-13 00:59:25 +000017#include "CGObjCRuntime.h"
Daniel Dunbarde7fb842008-08-11 05:00:27 +000018#include "clang/AST/ASTContext.h"
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000019#include "clang/AST/DeclObjC.h"
Mike Stump15037ca2009-12-15 00:35:12 +000020#include "llvm/Intrinsics.h"
Mike Stump41513442009-12-15 00:59:40 +000021#include "clang/CodeGen/CodeGenOptions.h"
Eli Friedman316bb1b2008-05-17 20:03:47 +000022#include "llvm/Target/TargetData.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000023using namespace clang;
24using namespace CodeGen;
25
26//===--------------------------------------------------------------------===//
27// Miscellaneous Helper Methods
28//===--------------------------------------------------------------------===//
29
30/// CreateTempAlloca - This creates a alloca and inserts it into the entry
31/// block.
32llvm::AllocaInst *CodeGenFunction::CreateTempAlloca(const llvm::Type *Ty,
Daniel Dunbar259e9cc2009-10-19 01:21:05 +000033 const llvm::Twine &Name) {
Chris Lattnerf1466842009-03-22 00:24:14 +000034 if (!Builder.isNamePreserving())
Daniel Dunbar259e9cc2009-10-19 01:21:05 +000035 return new llvm::AllocaInst(Ty, 0, "", AllocaInsertPt);
Devang Pateld35e2e02009-10-12 22:29:02 +000036 return new llvm::AllocaInst(Ty, 0, Name, AllocaInsertPt);
Reid Spencer5f016e22007-07-11 17:01:13 +000037}
38
39/// EvaluateExprAsBool - Perform the usual unary conversions on the specified
40/// expression and compare the result against zero, returning an Int1Ty value.
41llvm::Value *CodeGenFunction::EvaluateExprAsBool(const Expr *E) {
Chris Lattner9069fa22007-08-26 16:46:58 +000042 QualType BoolTy = getContext().BoolTy;
Eli Friedman3a173702009-12-11 09:26:29 +000043 if (E->getType()->isMemberFunctionPointerType()) {
Daniel Dunbar18aba0d2010-02-05 19:38:31 +000044 LValue LV = EmitAggExprToLValue(E);
Eli Friedman3a173702009-12-11 09:26:29 +000045
46 // Get the pointer.
Daniel Dunbar18aba0d2010-02-05 19:38:31 +000047 llvm::Value *FuncPtr = Builder.CreateStructGEP(LV.getAddress(), 0,
48 "src.ptr");
Eli Friedman3a173702009-12-11 09:26:29 +000049 FuncPtr = Builder.CreateLoad(FuncPtr);
50
51 llvm::Value *IsNotNull =
52 Builder.CreateICmpNE(FuncPtr,
53 llvm::Constant::getNullValue(FuncPtr->getType()),
54 "tobool");
55
56 return IsNotNull;
57 }
Chris Lattner9b2dc282008-04-04 16:54:41 +000058 if (!E->getType()->isAnyComplexType())
Chris Lattner9069fa22007-08-26 16:46:58 +000059 return EmitScalarConversion(EmitScalarExpr(E), E->getType(), BoolTy);
Reid Spencer5f016e22007-07-11 17:01:13 +000060
Chris Lattner9069fa22007-08-26 16:46:58 +000061 return EmitComplexToScalarConversion(EmitComplexExpr(E), E->getType(),BoolTy);
Reid Spencer5f016e22007-07-11 17:01:13 +000062}
63
Chris Lattner9b655512007-08-31 22:49:20 +000064/// EmitAnyExpr - Emit code to compute the specified expression which can have
65/// any type. The result is returned as an RValue struct. If this is an
Mike Stumpdb52dcd2009-09-09 13:00:44 +000066/// aggregate expression, the aggloc/agglocvolatile arguments indicate where the
67/// result should be returned.
68RValue CodeGenFunction::EmitAnyExpr(const Expr *E, llvm::Value *AggLoc,
Anders Carlsson14c5cbf2009-08-16 07:36:22 +000069 bool IsAggLocVolatile, bool IgnoreResult,
70 bool IsInitializer) {
Chris Lattner9b655512007-08-31 22:49:20 +000071 if (!hasAggregateLLVMType(E->getType()))
Mike Stump7f79f9b2009-05-29 15:46:01 +000072 return RValue::get(EmitScalarExpr(E, IgnoreResult));
Chris Lattner9b2dc282008-04-04 16:54:41 +000073 else if (E->getType()->isAnyComplexType())
Mike Stump7f79f9b2009-05-29 15:46:01 +000074 return RValue::getComplex(EmitComplexExpr(E, false, false,
75 IgnoreResult, IgnoreResult));
Mike Stumpdb52dcd2009-09-09 13:00:44 +000076
Anders Carlsson14c5cbf2009-08-16 07:36:22 +000077 EmitAggExpr(E, AggLoc, IsAggLocVolatile, IgnoreResult, IsInitializer);
78 return RValue::getAggregate(AggLoc, IsAggLocVolatile);
Chris Lattner9b655512007-08-31 22:49:20 +000079}
80
Mike Stumpdb52dcd2009-09-09 13:00:44 +000081/// EmitAnyExprToTemp - Similary to EmitAnyExpr(), however, the result will
82/// always be accessible even if no aggregate location is provided.
83RValue CodeGenFunction::EmitAnyExprToTemp(const Expr *E,
Anders Carlsson14c5cbf2009-08-16 07:36:22 +000084 bool IsAggLocVolatile,
85 bool IsInitializer) {
86 llvm::Value *AggLoc = 0;
Mike Stumpdb52dcd2009-09-09 13:00:44 +000087
88 if (hasAggregateLLVMType(E->getType()) &&
Daniel Dunbar46f45b92008-09-09 01:06:48 +000089 !E->getType()->isAnyComplexType())
Daniel Dunbarb2cd7772010-02-05 20:02:42 +000090 AggLoc = CreateTempAlloca(ConvertTypeForMem(E->getType()), "agg.tmp");
Mike Stumpdb52dcd2009-09-09 13:00:44 +000091 return EmitAnyExpr(E, AggLoc, IsAggLocVolatile, /*IgnoreResult=*/false,
Anders Carlsson14c5cbf2009-08-16 07:36:22 +000092 IsInitializer);
Daniel Dunbar46f45b92008-09-09 01:06:48 +000093}
94
Anders Carlsson4029ca72009-05-20 00:24:07 +000095RValue CodeGenFunction::EmitReferenceBindingToExpr(const Expr* E,
Anders Carlsson14c5cbf2009-08-16 07:36:22 +000096 bool IsInitializer) {
Anders Carlssone1b7ea12009-10-18 23:09:21 +000097 bool ShouldDestroyTemporaries = false;
98 unsigned OldNumLiveTemporaries = 0;
Eli Friedman27a9b722009-12-19 00:20:10 +000099
100 if (const CXXDefaultArgExpr *DAE = dyn_cast<CXXDefaultArgExpr>(E))
101 E = DAE->getExpr();
102
Anders Carlssonb3f74422009-10-15 00:51:46 +0000103 if (const CXXExprWithTemporaries *TE = dyn_cast<CXXExprWithTemporaries>(E)) {
Anders Carlsson0ece4912009-12-15 20:51:39 +0000104 ShouldDestroyTemporaries = true;
105
Chris Lattnereb99b012009-10-28 17:39:19 +0000106 // Keep track of the current cleanup stack depth.
Anders Carlsson0ece4912009-12-15 20:51:39 +0000107 OldNumLiveTemporaries = LiveTemporaries.size();
Anders Carlssonb3f74422009-10-15 00:51:46 +0000108
Anders Carlssone1b7ea12009-10-18 23:09:21 +0000109 E = TE->getSubExpr();
Anders Carlssonb3f74422009-10-15 00:51:46 +0000110 }
111
Eli Friedman5df0d422009-05-20 02:31:19 +0000112 RValue Val;
113 if (E->isLvalue(getContext()) == Expr::LV_Valid) {
Anders Carlsson4bbab922009-05-20 00:36:58 +0000114 // Emit the expr as an lvalue.
115 LValue LV = EmitLValue(E);
Anders Carlsson0dc73662010-02-04 17:32:58 +0000116 if (LV.isSimple()) {
117 if (ShouldDestroyTemporaries) {
118 // Pop temporaries.
119 while (LiveTemporaries.size() > OldNumLiveTemporaries)
120 PopCXXTemporary();
121 }
122
Eli Friedman5df0d422009-05-20 02:31:19 +0000123 return RValue::get(LV.getAddress());
Anders Carlsson0dc73662010-02-04 17:32:58 +0000124 }
125
Eli Friedman5df0d422009-05-20 02:31:19 +0000126 Val = EmitLoadOfLValue(LV, E->getType());
Anders Carlssone1b7ea12009-10-18 23:09:21 +0000127
128 if (ShouldDestroyTemporaries) {
129 // Pop temporaries.
130 while (LiveTemporaries.size() > OldNumLiveTemporaries)
131 PopCXXTemporary();
132 }
Eli Friedman5df0d422009-05-20 02:31:19 +0000133 } else {
Anders Carlssonb3f74422009-10-15 00:51:46 +0000134 const CXXRecordDecl *BaseClassDecl = 0;
135 const CXXRecordDecl *DerivedClassDecl = 0;
136
137 if (const CastExpr *CE =
138 dyn_cast<CastExpr>(E->IgnoreParenNoopCasts(getContext()))) {
139 if (CE->getCastKind() == CastExpr::CK_DerivedToBase) {
140 E = CE->getSubExpr();
141
142 BaseClassDecl =
143 cast<CXXRecordDecl>(CE->getType()->getAs<RecordType>()->getDecl());
144 DerivedClassDecl =
145 cast<CXXRecordDecl>(E->getType()->getAs<RecordType>()->getDecl());
146 }
147 }
148
Anders Carlsson14c5cbf2009-08-16 07:36:22 +0000149 Val = EmitAnyExprToTemp(E, /*IsAggLocVolatile=*/false,
150 IsInitializer);
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000151
Anders Carlssone1b7ea12009-10-18 23:09:21 +0000152 if (ShouldDestroyTemporaries) {
153 // Pop temporaries.
154 while (LiveTemporaries.size() > OldNumLiveTemporaries)
155 PopCXXTemporary();
156 }
157
Anders Carlsson2da76932009-08-16 17:54:29 +0000158 if (IsInitializer) {
159 // We might have to destroy the temporary variable.
160 if (const RecordType *RT = E->getType()->getAs<RecordType>()) {
161 if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
162 if (!ClassDecl->hasTrivialDestructor()) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000163 const CXXDestructorDecl *Dtor =
Anders Carlsson2da76932009-08-16 17:54:29 +0000164 ClassDecl->getDestructor(getContext());
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000165
Mike Stumpd88ea562009-12-09 03:35:49 +0000166 {
Anders Carlsson6ec687d2009-12-11 01:00:09 +0000167 DelayedCleanupBlock Scope(*this);
Mike Stumpd88ea562009-12-09 03:35:49 +0000168 EmitCXXDestructorCall(Dtor, Dtor_Complete,
169 Val.getAggregateAddr());
Anders Carlsson6ec687d2009-12-11 01:00:09 +0000170
171 // Make sure to jump to the exit block.
172 EmitBranch(Scope.getCleanupExitBlock());
Mike Stumpd88ea562009-12-09 03:35:49 +0000173 }
174 if (Exceptions) {
175 EHCleanupBlock Cleanup(*this);
176 EmitCXXDestructorCall(Dtor, Dtor_Complete,
177 Val.getAggregateAddr());
178 }
Anders Carlsson2da76932009-08-16 17:54:29 +0000179 }
Anders Carlsson8478ce62009-08-16 17:50:25 +0000180 }
181 }
182 }
Anders Carlssonb3f74422009-10-15 00:51:46 +0000183
184 // Check if need to perform the derived-to-base cast.
185 if (BaseClassDecl) {
186 llvm::Value *Derived = Val.getAggregateAddr();
Anders Carlssonb3f74422009-10-15 00:51:46 +0000187 llvm::Value *Base =
Anders Carlssona3697c92009-11-23 17:57:54 +0000188 GetAddressOfBaseClass(Derived, DerivedClassDecl, BaseClassDecl,
189 /*NullCheckValue=*/false);
Anders Carlssonb3f74422009-10-15 00:51:46 +0000190 return RValue::get(Base);
191 }
Anders Carlsson4bbab922009-05-20 00:36:58 +0000192 }
Eli Friedman5df0d422009-05-20 02:31:19 +0000193
194 if (Val.isAggregate()) {
195 Val = RValue::get(Val.getAggregateAddr());
196 } else {
Anders Carlsson7cd3a642009-05-20 01:35:03 +0000197 // Create a temporary variable that we can bind the reference to.
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000198 llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(E->getType()),
Anders Carlssone04d1c72009-05-20 01:03:17 +0000199 "reftmp");
Eli Friedman5df0d422009-05-20 02:31:19 +0000200 if (Val.isScalar())
201 EmitStoreOfScalar(Val.getScalarVal(), Temp, false, E->getType());
202 else
203 StoreComplexToAddr(Val.getComplexVal(), Temp, false);
204 Val = RValue::get(Temp);
Anders Carlssone04d1c72009-05-20 01:03:17 +0000205 }
Eli Friedman5df0d422009-05-20 02:31:19 +0000206
207 return Val;
Anders Carlsson4029ca72009-05-20 00:24:07 +0000208}
209
210
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000211/// getAccessedFieldNo - Given an encoded value and a result number, return the
212/// input field number being accessed.
213unsigned CodeGenFunction::getAccessedFieldNo(unsigned Idx,
Dan Gohman4f8d1232008-05-22 00:50:06 +0000214 const llvm::Constant *Elts) {
215 if (isa<llvm::ConstantAggregateZero>(Elts))
216 return 0;
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000217
Dan Gohman4f8d1232008-05-22 00:50:06 +0000218 return cast<llvm::ConstantInt>(Elts->getOperand(Idx))->getZExtValue();
219}
220
Mike Stumpb14e62d2009-12-16 02:57:00 +0000221void CodeGenFunction::EmitCheck(llvm::Value *Address, unsigned Size) {
222 if (!CatchUndefined)
223 return;
224
225 const llvm::IntegerType *Size_tTy
226 = llvm::IntegerType::get(VMContext, LLVMPointerWidth);
227 Address = Builder.CreateBitCast(Address, PtrToInt8Ty);
228
229 const llvm::Type *ResType[] = {
230 Size_tTy
231 };
232 llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::objectsize, ResType, 1);
233 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
234 CGM.getTypes().ConvertType(CGM.getContext().IntTy));
235 // In time, people may want to control this and use a 1 here.
236 llvm::Value *Arg = llvm::ConstantInt::get(IntTy, 0);
237 llvm::Value *C = Builder.CreateCall2(F, Address, Arg);
238 llvm::BasicBlock *Cont = createBasicBlock();
239 llvm::BasicBlock *Check = createBasicBlock();
240 llvm::Value *NegativeOne = llvm::ConstantInt::get(Size_tTy, -1ULL);
241 Builder.CreateCondBr(Builder.CreateICmpEQ(C, NegativeOne), Cont, Check);
242
243 EmitBlock(Check);
244 Builder.CreateCondBr(Builder.CreateICmpUGE(C,
245 llvm::ConstantInt::get(Size_tTy, Size)),
246 Cont, getTrapBB());
247 EmitBlock(Cont);
248}
Chris Lattner9b655512007-08-31 22:49:20 +0000249
Chris Lattnerdd36d322010-01-09 21:40:03 +0000250
251llvm::Value *CodeGenFunction::
252EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV,
253 bool isInc, bool isPre) {
254 QualType ValTy = E->getSubExpr()->getType();
255 llvm::Value *InVal = EmitLoadOfLValue(LV, ValTy).getScalarVal();
256
257 int AmountVal = isInc ? 1 : -1;
258
259 if (ValTy->isPointerType() &&
260 ValTy->getAs<PointerType>()->isVariableArrayType()) {
261 // The amount of the addition/subtraction needs to account for the VLA size
262 ErrorUnsupported(E, "VLA pointer inc/dec");
263 }
264
265 llvm::Value *NextVal;
266 if (const llvm::PointerType *PT =
267 dyn_cast<llvm::PointerType>(InVal->getType())) {
268 llvm::Constant *Inc =
269 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), AmountVal);
270 if (!isa<llvm::FunctionType>(PT->getElementType())) {
271 QualType PTEE = ValTy->getPointeeType();
272 if (const ObjCInterfaceType *OIT =
273 dyn_cast<ObjCInterfaceType>(PTEE)) {
274 // Handle interface types, which are not represented with a concrete
275 // type.
276 int size = getContext().getTypeSize(OIT) / 8;
277 if (!isInc)
278 size = -size;
279 Inc = llvm::ConstantInt::get(Inc->getType(), size);
280 const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(VMContext);
281 InVal = Builder.CreateBitCast(InVal, i8Ty);
282 NextVal = Builder.CreateGEP(InVal, Inc, "add.ptr");
283 llvm::Value *lhs = LV.getAddress();
284 lhs = Builder.CreateBitCast(lhs, llvm::PointerType::getUnqual(i8Ty));
285 LV = LValue::MakeAddr(lhs, MakeQualifiers(ValTy));
286 } else
287 NextVal = Builder.CreateInBoundsGEP(InVal, Inc, "ptrincdec");
288 } else {
289 const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(VMContext);
290 NextVal = Builder.CreateBitCast(InVal, i8Ty, "tmp");
291 NextVal = Builder.CreateGEP(NextVal, Inc, "ptrincdec");
292 NextVal = Builder.CreateBitCast(NextVal, InVal->getType());
293 }
294 } else if (InVal->getType() == llvm::Type::getInt1Ty(VMContext) && isInc) {
295 // Bool++ is an interesting case, due to promotion rules, we get:
296 // Bool++ -> Bool = Bool+1 -> Bool = (int)Bool+1 ->
297 // Bool = ((int)Bool+1) != 0
298 // An interesting aspect of this is that increment is always true.
299 // Decrement does not have this property.
300 NextVal = llvm::ConstantInt::getTrue(VMContext);
301 } else if (isa<llvm::IntegerType>(InVal->getType())) {
302 NextVal = llvm::ConstantInt::get(InVal->getType(), AmountVal);
303
304 // Signed integer overflow is undefined behavior.
305 if (ValTy->isSignedIntegerType())
306 NextVal = Builder.CreateNSWAdd(InVal, NextVal, isInc ? "inc" : "dec");
307 else
308 NextVal = Builder.CreateAdd(InVal, NextVal, isInc ? "inc" : "dec");
309 } else {
310 // Add the inc/dec to the real part.
311 if (InVal->getType()->isFloatTy())
312 NextVal =
313 llvm::ConstantFP::get(VMContext,
314 llvm::APFloat(static_cast<float>(AmountVal)));
315 else if (InVal->getType()->isDoubleTy())
316 NextVal =
317 llvm::ConstantFP::get(VMContext,
318 llvm::APFloat(static_cast<double>(AmountVal)));
319 else {
320 llvm::APFloat F(static_cast<float>(AmountVal));
321 bool ignored;
322 F.convert(Target.getLongDoubleFormat(), llvm::APFloat::rmTowardZero,
323 &ignored);
324 NextVal = llvm::ConstantFP::get(VMContext, F);
325 }
326 NextVal = Builder.CreateFAdd(InVal, NextVal, isInc ? "inc" : "dec");
327 }
328
329 // Store the updated result through the lvalue.
330 if (LV.isBitfield())
331 EmitStoreThroughBitfieldLValue(RValue::get(NextVal), LV, ValTy, &NextVal);
332 else
333 EmitStoreThroughLValue(RValue::get(NextVal), LV, ValTy);
334
335 // If this is a postinc, return the value read from memory, otherwise use the
336 // updated value.
337 return isPre ? NextVal : InVal;
338}
339
340
341CodeGenFunction::ComplexPairTy CodeGenFunction::
342EmitComplexPrePostIncDec(const UnaryOperator *E, LValue LV,
343 bool isInc, bool isPre) {
344 ComplexPairTy InVal = LoadComplexFromAddr(LV.getAddress(),
345 LV.isVolatileQualified());
346
347 llvm::Value *NextVal;
348 if (isa<llvm::IntegerType>(InVal.first->getType())) {
349 uint64_t AmountVal = isInc ? 1 : -1;
350 NextVal = llvm::ConstantInt::get(InVal.first->getType(), AmountVal, true);
351
352 // Add the inc/dec to the real part.
353 NextVal = Builder.CreateAdd(InVal.first, NextVal, isInc ? "inc" : "dec");
354 } else {
355 QualType ElemTy = E->getType()->getAs<ComplexType>()->getElementType();
356 llvm::APFloat FVal(getContext().getFloatTypeSemantics(ElemTy), 1);
357 if (!isInc)
358 FVal.changeSign();
359 NextVal = llvm::ConstantFP::get(getLLVMContext(), FVal);
360
361 // Add the inc/dec to the real part.
362 NextVal = Builder.CreateFAdd(InVal.first, NextVal, isInc ? "inc" : "dec");
363 }
364
365 ComplexPairTy IncVal(NextVal, InVal.second);
366
367 // Store the updated result through the lvalue.
368 StoreComplexToAddr(IncVal, LV.getAddress(), LV.isVolatileQualified());
369
370 // If this is a postinc, return the value read from memory, otherwise use the
371 // updated value.
372 return isPre ? IncVal : InVal;
373}
374
375
Reid Spencer5f016e22007-07-11 17:01:13 +0000376//===----------------------------------------------------------------------===//
377// LValue Expression Emission
378//===----------------------------------------------------------------------===//
379
Daniel Dunbar13e81732009-02-05 07:09:07 +0000380RValue CodeGenFunction::GetUndefRValue(QualType Ty) {
Chris Lattnereb99b012009-10-28 17:39:19 +0000381 if (Ty->isVoidType())
Daniel Dunbar13e81732009-02-05 07:09:07 +0000382 return RValue::get(0);
Chris Lattnereb99b012009-10-28 17:39:19 +0000383
384 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
Daniel Dunbar8fa73ed2009-01-09 20:09:28 +0000385 const llvm::Type *EltTy = ConvertType(CTy->getElementType());
Owen Anderson03e20502009-07-30 23:11:26 +0000386 llvm::Value *U = llvm::UndefValue::get(EltTy);
Daniel Dunbar8fa73ed2009-01-09 20:09:28 +0000387 return RValue::getComplex(std::make_pair(U, U));
Chris Lattnereb99b012009-10-28 17:39:19 +0000388 }
389
390 if (hasAggregateLLVMType(Ty)) {
Owen Anderson96e0fc72009-07-29 22:16:19 +0000391 const llvm::Type *LTy = llvm::PointerType::getUnqual(ConvertType(Ty));
Owen Anderson03e20502009-07-30 23:11:26 +0000392 return RValue::getAggregate(llvm::UndefValue::get(LTy));
Daniel Dunbar8fa73ed2009-01-09 20:09:28 +0000393 }
Chris Lattnereb99b012009-10-28 17:39:19 +0000394
395 return RValue::get(llvm::UndefValue::get(ConvertType(Ty)));
Daniel Dunbarce1d38b2009-01-09 16:50:52 +0000396}
397
Daniel Dunbar13e81732009-02-05 07:09:07 +0000398RValue CodeGenFunction::EmitUnsupportedRValue(const Expr *E,
399 const char *Name) {
400 ErrorUnsupported(E, Name);
401 return GetUndefRValue(E->getType());
402}
403
Daniel Dunbar6ba82a42008-08-25 20:45:57 +0000404LValue CodeGenFunction::EmitUnsupportedLValue(const Expr *E,
405 const char *Name) {
406 ErrorUnsupported(E, Name);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000407 llvm::Type *Ty = llvm::PointerType::getUnqual(ConvertType(E->getType()));
Owen Anderson03e20502009-07-30 23:11:26 +0000408 return LValue::MakeAddr(llvm::UndefValue::get(Ty),
John McCall0953e762009-09-24 19:53:00 +0000409 MakeQualifiers(E->getType()));
Daniel Dunbar6ba82a42008-08-25 20:45:57 +0000410}
411
Mike Stumpb14e62d2009-12-16 02:57:00 +0000412LValue CodeGenFunction::EmitCheckedLValue(const Expr *E) {
413 LValue LV = EmitLValue(E);
414 if (!isa<DeclRefExpr>(E) && !LV.isBitfield() && LV.isSimple())
415 EmitCheck(LV.getAddress(), getContext().getTypeSize(E->getType()) / 8);
416 return LV;
417}
418
Reid Spencer5f016e22007-07-11 17:01:13 +0000419/// EmitLValue - Emit code to compute a designator that specifies the location
420/// of the expression.
421///
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000422/// This can return one of two things: a simple address or a bitfield reference.
423/// In either case, the LLVM Value* in the LValue structure is guaranteed to be
424/// an LLVM pointer type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000425///
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000426/// If this returns a bitfield reference, nothing about the pointee type of the
427/// LLVM value is known: For example, it may not be a pointer to an integer.
Reid Spencer5f016e22007-07-11 17:01:13 +0000428///
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000429/// If this returns a normal address, and if the lvalue's C type is fixed size,
430/// this method guarantees that the returned pointer type will point to an LLVM
431/// type of the same size of the lvalue's type. If the lvalue has a variable
432/// length type, this is not possible.
Reid Spencer5f016e22007-07-11 17:01:13 +0000433///
434LValue CodeGenFunction::EmitLValue(const Expr *E) {
435 switch (E->getStmtClass()) {
Daniel Dunbar6ba82a42008-08-25 20:45:57 +0000436 default: return EmitUnsupportedLValue(E, "l-value expression");
Reid Spencer5f016e22007-07-11 17:01:13 +0000437
Fariborz Jahanian820bca42009-12-09 23:35:29 +0000438 case Expr::ObjCIsaExprClass:
439 return EmitObjCIsaExpr(cast<ObjCIsaExpr>(E));
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000440 case Expr::BinaryOperatorClass:
Daniel Dunbar80e62c22008-09-04 03:20:13 +0000441 return EmitBinaryOperatorLValue(cast<BinaryOperator>(E));
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000442 case Expr::CallExprClass:
Anders Carlssonfaf86642009-09-01 21:18:52 +0000443 case Expr::CXXMemberCallExprClass:
Douglas Gregorb4609802008-11-14 16:09:21 +0000444 case Expr::CXXOperatorCallExprClass:
445 return EmitCallExprLValue(cast<CallExpr>(E));
Daniel Dunbar5b5c9ef2009-02-11 20:59:32 +0000446 case Expr::VAArgExprClass:
447 return EmitVAArgExprLValue(cast<VAArgExpr>(E));
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000448 case Expr::DeclRefExprClass:
Douglas Gregor1a49af92009-01-06 05:10:23 +0000449 return EmitDeclRefLValue(cast<DeclRefExpr>(E));
Reid Spencer5f016e22007-07-11 17:01:13 +0000450 case Expr::ParenExprClass:return EmitLValue(cast<ParenExpr>(E)->getSubExpr());
Chris Lattnerd9f69102008-08-10 01:53:14 +0000451 case Expr::PredefinedExprClass:
452 return EmitPredefinedLValue(cast<PredefinedExpr>(E));
Reid Spencer5f016e22007-07-11 17:01:13 +0000453 case Expr::StringLiteralClass:
454 return EmitStringLiteralLValue(cast<StringLiteral>(E));
Chris Lattnereaf2bb82009-02-24 22:18:39 +0000455 case Expr::ObjCEncodeExprClass:
456 return EmitObjCEncodeExprLValue(cast<ObjCEncodeExpr>(E));
Chris Lattner391d77a2008-03-30 23:03:07 +0000457
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000458 case Expr::BlockDeclRefExprClass:
Mike Stumpa99038c2009-02-28 09:07:16 +0000459 return EmitBlockDeclRefLValue(cast<BlockDeclRefExpr>(E));
460
Anders Carlssonb58d0172009-05-30 23:23:33 +0000461 case Expr::CXXTemporaryObjectExprClass:
462 case Expr::CXXConstructExprClass:
Anders Carlssone61c9e82009-05-30 23:30:54 +0000463 return EmitCXXConstructLValue(cast<CXXConstructExpr>(E));
464 case Expr::CXXBindTemporaryExprClass:
465 return EmitCXXBindTemporaryLValue(cast<CXXBindTemporaryExpr>(E));
Anders Carlssonb9ea0b52009-09-14 01:10:45 +0000466 case Expr::CXXExprWithTemporariesClass:
467 return EmitCXXExprWithTemporariesLValue(cast<CXXExprWithTemporaries>(E));
Anders Carlsson370e5382009-11-14 01:51:50 +0000468 case Expr::CXXZeroInitValueExprClass:
469 return EmitNullInitializationLValue(cast<CXXZeroInitValueExpr>(E));
470 case Expr::CXXDefaultArgExprClass:
471 return EmitLValue(cast<CXXDefaultArgExpr>(E)->getExpr());
Mike Stumpc2e84ae2009-11-15 08:09:41 +0000472 case Expr::CXXTypeidExprClass:
473 return EmitCXXTypeidLValue(cast<CXXTypeidExpr>(E));
Anders Carlssone61c9e82009-05-30 23:30:54 +0000474
Daniel Dunbar0a04d772008-08-23 10:51:21 +0000475 case Expr::ObjCMessageExprClass:
476 return EmitObjCMessageExprLValue(cast<ObjCMessageExpr>(E));
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000477 case Expr::ObjCIvarRefExprClass:
Chris Lattner391d77a2008-03-30 23:03:07 +0000478 return EmitObjCIvarRefLValue(cast<ObjCIvarRefExpr>(E));
Daniel Dunbar6ba82a42008-08-25 20:45:57 +0000479 case Expr::ObjCPropertyRefExprClass:
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000480 return EmitObjCPropertyRefLValue(cast<ObjCPropertyRefExpr>(E));
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000481 case Expr::ObjCImplicitSetterGetterRefExprClass:
482 return EmitObjCKVCRefLValue(cast<ObjCImplicitSetterGetterRefExpr>(E));
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000483 case Expr::ObjCSuperExprClass:
Chris Lattner65459942009-04-25 19:35:26 +0000484 return EmitObjCSuperExprLValue(cast<ObjCSuperExpr>(E));
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000485
Chris Lattner65459942009-04-25 19:35:26 +0000486 case Expr::StmtExprClass:
487 return EmitStmtExprLValue(cast<StmtExpr>(E));
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000488 case Expr::UnaryOperatorClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000489 return EmitUnaryOpLValue(cast<UnaryOperator>(E));
490 case Expr::ArraySubscriptExprClass:
491 return EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E));
Nate Begeman213541a2008-04-18 23:10:10 +0000492 case Expr::ExtVectorElementExprClass:
493 return EmitExtVectorElementExpr(cast<ExtVectorElementExpr>(E));
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000494 case Expr::MemberExprClass:
Douglas Gregorbd4c4ae2009-08-26 22:36:53 +0000495 return EmitMemberExpr(cast<MemberExpr>(E));
Eli Friedman06e863f2008-05-13 23:18:27 +0000496 case Expr::CompoundLiteralExprClass:
497 return EmitCompoundLiteralLValue(cast<CompoundLiteralExpr>(E));
Daniel Dunbar90345582009-03-24 02:38:23 +0000498 case Expr::ConditionalOperatorClass:
Anders Carlsson6fcec8b2009-09-15 16:35:24 +0000499 return EmitConditionalOperatorLValue(cast<ConditionalOperator>(E));
Chris Lattner670a62c2008-12-12 05:35:08 +0000500 case Expr::ChooseExprClass:
Eli Friedman79769322009-03-04 05:52:32 +0000501 return EmitLValue(cast<ChooseExpr>(E)->getChosenSubExpr(getContext()));
Chris Lattnerc3953a62009-03-18 04:02:57 +0000502 case Expr::ImplicitCastExprClass:
503 case Expr::CStyleCastExprClass:
504 case Expr::CXXFunctionalCastExprClass:
505 case Expr::CXXStaticCastExprClass:
506 case Expr::CXXDynamicCastExprClass:
507 case Expr::CXXReinterpretCastExprClass:
508 case Expr::CXXConstCastExprClass:
Chris Lattner75dfeda2009-03-18 18:28:57 +0000509 return EmitCastLValue(cast<CastExpr>(E));
Reid Spencer5f016e22007-07-11 17:01:13 +0000510 }
511}
512
Daniel Dunbar9d9cc872009-02-10 00:57:50 +0000513llvm::Value *CodeGenFunction::EmitLoadOfScalar(llvm::Value *Addr, bool Volatile,
514 QualType Ty) {
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000515 llvm::LoadInst *Load = Builder.CreateLoad(Addr, "tmp");
516 if (Volatile)
517 Load->setVolatile(true);
Daniel Dunbar9d9cc872009-02-10 00:57:50 +0000518
Anders Carlsson3bb423b2009-05-19 19:36:19 +0000519 // Bool can have different representation in memory than in registers.
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000520 llvm::Value *V = Load;
Daniel Dunbar9d9cc872009-02-10 00:57:50 +0000521 if (Ty->isBooleanType())
Owen Anderson0032b272009-08-13 21:57:51 +0000522 if (V->getType() != llvm::Type::getInt1Ty(VMContext))
523 V = Builder.CreateTrunc(V, llvm::Type::getInt1Ty(VMContext), "tobool");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000524
Daniel Dunbar9d9cc872009-02-10 00:57:50 +0000525 return V;
526}
527
528void CodeGenFunction::EmitStoreOfScalar(llvm::Value *Value, llvm::Value *Addr,
Anders Carlssonb4aa4662009-05-19 18:50:41 +0000529 bool Volatile, QualType Ty) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000530
Anders Carlsson3bb423b2009-05-19 19:36:19 +0000531 if (Ty->isBooleanType()) {
532 // Bool can have different representation in memory than in registers.
Anders Carlsson3bb423b2009-05-19 19:36:19 +0000533 const llvm::PointerType *DstPtr = cast<llvm::PointerType>(Addr->getType());
Eli Friedman2b06d342009-12-01 22:31:51 +0000534 Value = Builder.CreateIntCast(Value, DstPtr->getElementType(), false);
Daniel Dunbar9d9cc872009-02-10 00:57:50 +0000535 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000536 Builder.CreateStore(Value, Addr, Volatile);
Daniel Dunbar9d9cc872009-02-10 00:57:50 +0000537}
538
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000539/// EmitLoadOfLValue - Given an expression that represents a value lvalue, this
540/// method emits the address of the lvalue, then loads the result as an rvalue,
541/// returning the rvalue.
Reid Spencer5f016e22007-07-11 17:01:13 +0000542RValue CodeGenFunction::EmitLoadOfLValue(LValue LV, QualType ExprType) {
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +0000543 if (LV.isObjCWeak()) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000544 // load of a __weak object.
Fariborz Jahanian6dc23172008-11-18 21:45:40 +0000545 llvm::Value *AddrWeakObj = LV.getAddress();
Chris Lattnereb99b012009-10-28 17:39:19 +0000546 return RValue::get(CGM.getObjCRuntime().EmitObjCWeakRead(*this,
547 AddrWeakObj));
Fariborz Jahanian6dc23172008-11-18 21:45:40 +0000548 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000549
Reid Spencer5f016e22007-07-11 17:01:13 +0000550 if (LV.isSimple()) {
551 llvm::Value *Ptr = LV.getAddress();
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000552
Reid Spencer5f016e22007-07-11 17:01:13 +0000553 // Simple scalar l-value.
Daniel Dunbar02762712010-02-05 17:51:33 +0000554 if (!CodeGenFunction::hasAggregateLLVMType(ExprType))
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000555 return RValue::get(EmitLoadOfScalar(Ptr, LV.isVolatileQualified(),
Daniel Dunbar9d9cc872009-02-10 00:57:50 +0000556 ExprType));
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000557
Daniel Dunbar02762712010-02-05 17:51:33 +0000558 // FIXME: This case shouldn't be necessary?
Chris Lattner883f6a72007-08-11 00:04:45 +0000559 assert(ExprType->isFunctionType() && "Unknown scalar value");
560 return RValue::get(Ptr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000561 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000562
Reid Spencer5f016e22007-07-11 17:01:13 +0000563 if (LV.isVectorElt()) {
Eli Friedman1e692ac2008-06-13 23:01:12 +0000564 llvm::Value *Vec = Builder.CreateLoad(LV.getVectorAddr(),
565 LV.isVolatileQualified(), "tmp");
Reid Spencer5f016e22007-07-11 17:01:13 +0000566 return RValue::get(Builder.CreateExtractElement(Vec, LV.getVectorIdx(),
567 "vecext"));
568 }
Chris Lattner46ea8eb2007-08-03 00:16:29 +0000569
570 // If this is a reference to a subset of the elements of a vector, either
571 // shuffle the input or extract/insert them as appropriate.
Nate Begeman213541a2008-04-18 23:10:10 +0000572 if (LV.isExtVectorElt())
573 return EmitLoadOfExtVectorElementLValue(LV, ExprType);
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000574
575 if (LV.isBitfield())
576 return EmitLoadOfBitfieldLValue(LV, ExprType);
577
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000578 if (LV.isPropertyRef())
579 return EmitLoadOfPropertyRefLValue(LV, ExprType);
580
Chris Lattner73525de2009-02-16 21:11:58 +0000581 assert(LV.isKVCRef() && "Unknown LValue type!");
582 return EmitLoadOfKVCRefLValue(LV, ExprType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000583}
584
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000585RValue CodeGenFunction::EmitLoadOfBitfieldLValue(LValue LV,
586 QualType ExprType) {
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000587 unsigned StartBit = LV.getBitfieldStartBit();
588 unsigned BitfieldSize = LV.getBitfieldSize();
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000589 llvm::Value *Ptr = LV.getBitfieldAddr();
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000590
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000591 const llvm::Type *EltTy =
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000592 cast<llvm::PointerType>(Ptr->getType())->getElementType();
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000593 unsigned EltTySize = CGM.getTargetData().getTypeSizeInBits(EltTy);
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000594
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000595 // In some cases the bitfield may straddle two memory locations. Currently we
596 // load the entire bitfield, then do the magic to sign-extend it if
597 // necessary. This results in somewhat more code than necessary for the common
598 // case (one load), since two shifts accomplish both the masking and sign
599 // extension.
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000600 unsigned LowBits = std::min(BitfieldSize, EltTySize - StartBit);
601 llvm::Value *Val = Builder.CreateLoad(Ptr, LV.isVolatileQualified(), "tmp");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000602
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000603 // Shift to proper location.
Daniel Dunbarf3edc2f2008-11-13 02:20:34 +0000604 if (StartBit)
Chris Lattner86b85b22009-12-06 17:22:42 +0000605 Val = Builder.CreateLShr(Val, StartBit, "bf.lo");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000606
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000607 // Mask off unused bits.
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000608 llvm::Constant *LowMask = llvm::ConstantInt::get(VMContext,
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000609 llvm::APInt::getLowBitsSet(EltTySize, LowBits));
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000610 Val = Builder.CreateAnd(Val, LowMask, "bf.lo.cleared");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000611
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000612 // Fetch the high bits if necessary.
613 if (LowBits < BitfieldSize) {
614 unsigned HighBits = BitfieldSize - LowBits;
Owen Anderson0032b272009-08-13 21:57:51 +0000615 llvm::Value *HighPtr = Builder.CreateGEP(Ptr, llvm::ConstantInt::get(
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000616 llvm::Type::getInt32Ty(VMContext), 1), "bf.ptr.hi");
617 llvm::Value *HighVal = Builder.CreateLoad(HighPtr,
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000618 LV.isVolatileQualified(),
619 "tmp");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000620
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000621 // Mask off unused bits.
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000622 llvm::Constant *HighMask = llvm::ConstantInt::get(VMContext,
623 llvm::APInt::getLowBitsSet(EltTySize, HighBits));
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000624 HighVal = Builder.CreateAnd(HighVal, HighMask, "bf.lo.cleared");
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000625
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000626 // Shift to proper location and or in to bitfield value.
Chris Lattner86b85b22009-12-06 17:22:42 +0000627 HighVal = Builder.CreateShl(HighVal, LowBits);
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000628 Val = Builder.CreateOr(Val, HighVal, "bf.val");
629 }
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000630
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000631 // Sign extend if necessary.
632 if (LV.isBitfieldSigned()) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000633 llvm::Value *ExtraBits = llvm::ConstantInt::get(EltTy,
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000634 EltTySize - BitfieldSize);
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000635 Val = Builder.CreateAShr(Builder.CreateShl(Val, ExtraBits),
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000636 ExtraBits, "bf.val.sext");
637 }
Eli Friedman316bb1b2008-05-17 20:03:47 +0000638
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000639 // The bitfield type and the normal type differ when the storage sizes differ
640 // (currently just _Bool).
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000641 Val = Builder.CreateIntCast(Val, ConvertType(ExprType), false, "tmp");
Eli Friedman316bb1b2008-05-17 20:03:47 +0000642
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000643 return RValue::get(Val);
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000644}
645
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000646RValue CodeGenFunction::EmitLoadOfPropertyRefLValue(LValue LV,
647 QualType ExprType) {
648 return EmitObjCPropertyGet(LV.getPropertyRefExpr());
649}
650
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000651RValue CodeGenFunction::EmitLoadOfKVCRefLValue(LValue LV,
652 QualType ExprType) {
653 return EmitObjCPropertyGet(LV.getKVCRefExpr());
654}
655
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000656// If this is a reference to a subset of the elements of a vector, create an
657// appropriate shufflevector.
Nate Begeman213541a2008-04-18 23:10:10 +0000658RValue CodeGenFunction::EmitLoadOfExtVectorElementLValue(LValue LV,
659 QualType ExprType) {
Eli Friedman1e692ac2008-06-13 23:01:12 +0000660 llvm::Value *Vec = Builder.CreateLoad(LV.getExtVectorAddr(),
661 LV.isVolatileQualified(), "tmp");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000662
Nate Begeman8a997642008-05-09 06:41:27 +0000663 const llvm::Constant *Elts = LV.getExtVectorElts();
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000664
665 // If the result of the expression is a non-vector type, we must be extracting
666 // a single element. Just codegen as an extractelement.
John McCall183700f2009-09-21 23:43:11 +0000667 const VectorType *ExprVT = ExprType->getAs<VectorType>();
Chris Lattnercf60cd22007-08-10 17:10:08 +0000668 if (!ExprVT) {
Dan Gohman4f8d1232008-05-22 00:50:06 +0000669 unsigned InIdx = getAccessedFieldNo(0, Elts);
Owen Anderson0032b272009-08-13 21:57:51 +0000670 llvm::Value *Elt = llvm::ConstantInt::get(
671 llvm::Type::getInt32Ty(VMContext), InIdx);
Chris Lattner34cdc862007-08-03 16:18:34 +0000672 return RValue::get(Builder.CreateExtractElement(Vec, Elt, "tmp"));
673 }
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000674
675 // Always use shuffle vector to try to retain the original program structure
Chris Lattnercf60cd22007-08-10 17:10:08 +0000676 unsigned NumResultElts = ExprVT->getNumElements();
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000677
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000678 llvm::SmallVector<llvm::Constant*, 4> Mask;
Chris Lattner34cdc862007-08-03 16:18:34 +0000679 for (unsigned i = 0; i != NumResultElts; ++i) {
Dan Gohman4f8d1232008-05-22 00:50:06 +0000680 unsigned InIdx = getAccessedFieldNo(i, Elts);
Owen Anderson0032b272009-08-13 21:57:51 +0000681 Mask.push_back(llvm::ConstantInt::get(
682 llvm::Type::getInt32Ty(VMContext), InIdx));
Chris Lattner34cdc862007-08-03 16:18:34 +0000683 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000684
Owen Anderson4a289322009-07-28 21:22:35 +0000685 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000686 Vec = Builder.CreateShuffleVector(Vec,
Owen Anderson03e20502009-07-30 23:11:26 +0000687 llvm::UndefValue::get(Vec->getType()),
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000688 MaskV, "tmp");
689 return RValue::get(Vec);
Chris Lattner34cdc862007-08-03 16:18:34 +0000690}
691
692
Reid Spencer5f016e22007-07-11 17:01:13 +0000693
694/// EmitStoreThroughLValue - Store the specified rvalue into the specified
695/// lvalue, where both are guaranteed to the have the same type, and that type
696/// is 'Ty'.
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000697void CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst,
Reid Spencer5f016e22007-07-11 17:01:13 +0000698 QualType Ty) {
Chris Lattner017d6aa2007-08-03 16:28:33 +0000699 if (!Dst.isSimple()) {
700 if (Dst.isVectorElt()) {
701 // Read/modify/write the vector, inserting the new element.
Eli Friedman1e692ac2008-06-13 23:01:12 +0000702 llvm::Value *Vec = Builder.CreateLoad(Dst.getVectorAddr(),
703 Dst.isVolatileQualified(), "tmp");
Chris Lattner9b655512007-08-31 22:49:20 +0000704 Vec = Builder.CreateInsertElement(Vec, Src.getScalarVal(),
Chris Lattner017d6aa2007-08-03 16:28:33 +0000705 Dst.getVectorIdx(), "vecins");
Eli Friedman1e692ac2008-06-13 23:01:12 +0000706 Builder.CreateStore(Vec, Dst.getVectorAddr(),Dst.isVolatileQualified());
Chris Lattner017d6aa2007-08-03 16:28:33 +0000707 return;
708 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000709
Nate Begeman213541a2008-04-18 23:10:10 +0000710 // If this is an update of extended vector elements, insert them as
711 // appropriate.
712 if (Dst.isExtVectorElt())
713 return EmitStoreThroughExtVectorComponentLValue(Src, Dst, Ty);
Lauro Ramos Venancioa0c5d0e2008-01-22 22:36:45 +0000714
715 if (Dst.isBitfield())
716 return EmitStoreThroughBitfieldLValue(Src, Dst, Ty);
717
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000718 if (Dst.isPropertyRef())
719 return EmitStoreThroughPropertyRefLValue(Src, Dst, Ty);
720
Chris Lattnereb99b012009-10-28 17:39:19 +0000721 assert(Dst.isKVCRef() && "Unknown LValue type");
722 return EmitStoreThroughKVCRefLValue(Src, Dst, Ty);
Chris Lattner017d6aa2007-08-03 16:28:33 +0000723 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000724
Fariborz Jahanian4f676ed2009-02-21 00:30:43 +0000725 if (Dst.isObjCWeak() && !Dst.isNonGC()) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000726 // load of a __weak object.
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +0000727 llvm::Value *LvalueDst = Dst.getAddress();
728 llvm::Value *src = Src.getScalarVal();
Mike Stumpf33651c2009-04-14 00:57:29 +0000729 CGM.getObjCRuntime().EmitObjCWeakAssign(*this, src, LvalueDst);
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +0000730 return;
731 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000732
Fariborz Jahanian4f676ed2009-02-21 00:30:43 +0000733 if (Dst.isObjCStrong() && !Dst.isNonGC()) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000734 // load of a __strong object.
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +0000735 llvm::Value *LvalueDst = Dst.getAddress();
736 llvm::Value *src = Src.getScalarVal();
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000737 if (Dst.isObjCIvar()) {
738 assert(Dst.getBaseIvarExp() && "BaseIvarExp is NULL");
739 const llvm::Type *ResultType = ConvertType(getContext().LongTy);
740 llvm::Value *RHS = EmitScalarExpr(Dst.getBaseIvarExp());
Fariborz Jahanian76368e82009-09-25 00:00:20 +0000741 llvm::Value *dst = RHS;
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000742 RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast");
743 llvm::Value *LHS =
744 Builder.CreatePtrToInt(LvalueDst, ResultType, "sub.ptr.lhs.cast");
745 llvm::Value *BytesBetween = Builder.CreateSub(LHS, RHS, "ivar.offset");
Fariborz Jahanian76368e82009-09-25 00:00:20 +0000746 CGM.getObjCRuntime().EmitObjCIvarAssign(*this, src, dst,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000747 BytesBetween);
Chris Lattnereb99b012009-10-28 17:39:19 +0000748 } else if (Dst.isGlobalObjCRef())
Fariborz Jahanianbf63b872009-05-04 23:27:20 +0000749 CGM.getObjCRuntime().EmitObjCGlobalAssign(*this, src, LvalueDst);
750 else
751 CGM.getObjCRuntime().EmitObjCStrongCastAssign(*this, src, LvalueDst);
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +0000752 return;
753 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000754
Chris Lattner883f6a72007-08-11 00:04:45 +0000755 assert(Src.isScalar() && "Can't emit an agg store with this method");
Anders Carlssonb4aa4662009-05-19 18:50:41 +0000756 EmitStoreOfScalar(Src.getScalarVal(), Dst.getAddress(),
757 Dst.isVolatileQualified(), Ty);
Reid Spencer5f016e22007-07-11 17:01:13 +0000758}
759
Lauro Ramos Venancioa0c5d0e2008-01-22 22:36:45 +0000760void CodeGenFunction::EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst,
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000761 QualType Ty,
Daniel Dunbared3849b2008-11-19 09:36:46 +0000762 llvm::Value **Result) {
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000763 unsigned StartBit = Dst.getBitfieldStartBit();
764 unsigned BitfieldSize = Dst.getBitfieldSize();
Lauro Ramos Venancioa0c5d0e2008-01-22 22:36:45 +0000765 llvm::Value *Ptr = Dst.getBitfieldAddr();
Lauro Ramos Venancioa0c5d0e2008-01-22 22:36:45 +0000766
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000767 const llvm::Type *EltTy =
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000768 cast<llvm::PointerType>(Ptr->getType())->getElementType();
769 unsigned EltTySize = CGM.getTargetData().getTypeSizeInBits(EltTy);
770
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000771 // Get the new value, cast to the appropriate type and masked to exactly the
772 // size of the bit-field.
Daniel Dunbared3849b2008-11-19 09:36:46 +0000773 llvm::Value *SrcVal = Src.getScalarVal();
774 llvm::Value *NewVal = Builder.CreateIntCast(SrcVal, EltTy, false, "tmp");
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000775 llvm::Constant *Mask = llvm::ConstantInt::get(VMContext,
776 llvm::APInt::getLowBitsSet(EltTySize, BitfieldSize));
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000777 NewVal = Builder.CreateAnd(NewVal, Mask, "bf.value");
Lauro Ramos Venancioa0c5d0e2008-01-22 22:36:45 +0000778
Daniel Dunbared3849b2008-11-19 09:36:46 +0000779 // Return the new value of the bit-field, if requested.
780 if (Result) {
781 // Cast back to the proper type for result.
782 const llvm::Type *SrcTy = SrcVal->getType();
783 llvm::Value *SrcTrunc = Builder.CreateIntCast(NewVal, SrcTy, false,
784 "bf.reload.val");
785
786 // Sign extend if necessary.
787 if (Dst.isBitfieldSigned()) {
788 unsigned SrcTySize = CGM.getTargetData().getTypeSizeInBits(SrcTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000789 llvm::Value *ExtraBits = llvm::ConstantInt::get(SrcTy,
Daniel Dunbared3849b2008-11-19 09:36:46 +0000790 SrcTySize - BitfieldSize);
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000791 SrcTrunc = Builder.CreateAShr(Builder.CreateShl(SrcTrunc, ExtraBits),
Daniel Dunbared3849b2008-11-19 09:36:46 +0000792 ExtraBits, "bf.reload.sext");
793 }
794
795 *Result = SrcTrunc;
796 }
797
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000798 // In some cases the bitfield may straddle two memory locations. Emit the low
799 // part first and check to see if the high needs to be done.
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000800 unsigned LowBits = std::min(BitfieldSize, EltTySize - StartBit);
801 llvm::Value *LowVal = Builder.CreateLoad(Ptr, Dst.isVolatileQualified(),
802 "bf.prev.low");
Eli Friedman316bb1b2008-05-17 20:03:47 +0000803
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000804 // Compute the mask for zero-ing the low part of this bitfield.
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000805 llvm::Constant *InvMask =
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000806 llvm::ConstantInt::get(VMContext,
807 ~llvm::APInt::getBitsSet(EltTySize, StartBit, StartBit + LowBits));
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000808
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000809 // Compute the new low part as
810 // LowVal = (LowVal & InvMask) | (NewVal << StartBit),
811 // with the shift of NewVal implicitly stripping the high bits.
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000812 llvm::Value *NewLowVal =
Chris Lattner86b85b22009-12-06 17:22:42 +0000813 Builder.CreateShl(NewVal, StartBit, "bf.value.lo");
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000814 LowVal = Builder.CreateAnd(LowVal, InvMask, "bf.prev.lo.cleared");
815 LowVal = Builder.CreateOr(LowVal, NewLowVal, "bf.new.lo");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000816
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000817 // Write back.
818 Builder.CreateStore(LowVal, Ptr, Dst.isVolatileQualified());
Eli Friedman316bb1b2008-05-17 20:03:47 +0000819
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000820 // If the low part doesn't cover the bitfield emit a high part.
821 if (LowBits < BitfieldSize) {
822 unsigned HighBits = BitfieldSize - LowBits;
Owen Anderson0032b272009-08-13 21:57:51 +0000823 llvm::Value *HighPtr = Builder.CreateGEP(Ptr, llvm::ConstantInt::get(
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000824 llvm::Type::getInt32Ty(VMContext), 1), "bf.ptr.hi");
825 llvm::Value *HighVal = Builder.CreateLoad(HighPtr,
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000826 Dst.isVolatileQualified(),
827 "bf.prev.hi");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000828
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000829 // Compute the mask for zero-ing the high part of this bitfield.
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000830 llvm::Constant *InvMask =
831 llvm::ConstantInt::get(VMContext, ~llvm::APInt::getLowBitsSet(EltTySize,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000832 HighBits));
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000833
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000834 // Compute the new high part as
835 // HighVal = (HighVal & InvMask) | (NewVal lshr LowBits),
836 // where the high bits of NewVal have already been cleared and the
837 // shift stripping the low bits.
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000838 llvm::Value *NewHighVal =
Chris Lattner86b85b22009-12-06 17:22:42 +0000839 Builder.CreateLShr(NewVal, LowBits, "bf.value.high");
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000840 HighVal = Builder.CreateAnd(HighVal, InvMask, "bf.prev.hi.cleared");
841 HighVal = Builder.CreateOr(HighVal, NewHighVal, "bf.new.hi");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000842
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000843 // Write back.
844 Builder.CreateStore(HighVal, HighPtr, Dst.isVolatileQualified());
845 }
Lauro Ramos Venancioa0c5d0e2008-01-22 22:36:45 +0000846}
847
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000848void CodeGenFunction::EmitStoreThroughPropertyRefLValue(RValue Src,
849 LValue Dst,
850 QualType Ty) {
851 EmitObjCPropertySet(Dst.getPropertyRefExpr(), Src);
852}
853
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000854void CodeGenFunction::EmitStoreThroughKVCRefLValue(RValue Src,
855 LValue Dst,
856 QualType Ty) {
857 EmitObjCPropertySet(Dst.getKVCRefExpr(), Src);
858}
859
Nate Begeman213541a2008-04-18 23:10:10 +0000860void CodeGenFunction::EmitStoreThroughExtVectorComponentLValue(RValue Src,
861 LValue Dst,
862 QualType Ty) {
Chris Lattner017d6aa2007-08-03 16:28:33 +0000863 // This access turns into a read/modify/write of the vector. Load the input
864 // value now.
Eli Friedman1e692ac2008-06-13 23:01:12 +0000865 llvm::Value *Vec = Builder.CreateLoad(Dst.getExtVectorAddr(),
866 Dst.isVolatileQualified(), "tmp");
Nate Begeman8a997642008-05-09 06:41:27 +0000867 const llvm::Constant *Elts = Dst.getExtVectorElts();
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000868
Chris Lattner9b655512007-08-31 22:49:20 +0000869 llvm::Value *SrcVal = Src.getScalarVal();
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000870
John McCall183700f2009-09-21 23:43:11 +0000871 if (const VectorType *VTy = Ty->getAs<VectorType>()) {
Chris Lattner7e6b51b2007-08-03 16:37:04 +0000872 unsigned NumSrcElts = VTy->getNumElements();
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000873 unsigned NumDstElts =
874 cast<llvm::VectorType>(Vec->getType())->getNumElements();
875 if (NumDstElts == NumSrcElts) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000876 // Use shuffle vector is the src and destination are the same number of
877 // elements and restore the vector mask since it is on the side it will be
878 // stored.
Nate Begeman6e5dd862009-06-26 21:12:50 +0000879 llvm::SmallVector<llvm::Constant*, 4> Mask(NumDstElts);
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000880 for (unsigned i = 0; i != NumSrcElts; ++i) {
881 unsigned InIdx = getAccessedFieldNo(i, Elts);
Owen Anderson0032b272009-08-13 21:57:51 +0000882 Mask[InIdx] = llvm::ConstantInt::get(
883 llvm::Type::getInt32Ty(VMContext), i);
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000884 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000885
Owen Anderson4a289322009-07-28 21:22:35 +0000886 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000887 Vec = Builder.CreateShuffleVector(SrcVal,
Owen Anderson03e20502009-07-30 23:11:26 +0000888 llvm::UndefValue::get(Vec->getType()),
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000889 MaskV, "tmp");
Mike Stumpb3589f42009-07-30 22:28:39 +0000890 } else if (NumDstElts > NumSrcElts) {
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000891 // Extended the source vector to the same length and then shuffle it
892 // into the destination.
893 // FIXME: since we're shuffling with undef, can we just use the indices
894 // into that? This could be simpler.
895 llvm::SmallVector<llvm::Constant*, 4> ExtMask;
Chris Lattnereb99b012009-10-28 17:39:19 +0000896 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000897 unsigned i;
898 for (i = 0; i != NumSrcElts; ++i)
Chris Lattnereb99b012009-10-28 17:39:19 +0000899 ExtMask.push_back(llvm::ConstantInt::get(Int32Ty, i));
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000900 for (; i != NumDstElts; ++i)
Chris Lattnereb99b012009-10-28 17:39:19 +0000901 ExtMask.push_back(llvm::UndefValue::get(Int32Ty));
Owen Anderson4a289322009-07-28 21:22:35 +0000902 llvm::Value *ExtMaskV = llvm::ConstantVector::get(&ExtMask[0],
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000903 ExtMask.size());
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000904 llvm::Value *ExtSrcVal =
Daniel Dunbarbb767732009-02-17 18:31:04 +0000905 Builder.CreateShuffleVector(SrcVal,
Owen Anderson03e20502009-07-30 23:11:26 +0000906 llvm::UndefValue::get(SrcVal->getType()),
Daniel Dunbarbb767732009-02-17 18:31:04 +0000907 ExtMaskV, "tmp");
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000908 // build identity
909 llvm::SmallVector<llvm::Constant*, 4> Mask;
Chris Lattnereb99b012009-10-28 17:39:19 +0000910 for (unsigned i = 0; i != NumDstElts; ++i)
911 Mask.push_back(llvm::ConstantInt::get(Int32Ty, i));
912
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000913 // modify when what gets shuffled in
914 for (unsigned i = 0; i != NumSrcElts; ++i) {
915 unsigned Idx = getAccessedFieldNo(i, Elts);
Chris Lattnereb99b012009-10-28 17:39:19 +0000916 Mask[Idx] = llvm::ConstantInt::get(Int32Ty, i+NumDstElts);
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000917 }
Owen Anderson4a289322009-07-28 21:22:35 +0000918 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000919 Vec = Builder.CreateShuffleVector(Vec, ExtSrcVal, MaskV, "tmp");
Mike Stumpb3589f42009-07-30 22:28:39 +0000920 } else {
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000921 // We should never shorten the vector
922 assert(0 && "unexpected shorten vector length");
Chris Lattner7e6b51b2007-08-03 16:37:04 +0000923 }
924 } else {
925 // If the Src is a scalar (not a vector) it must be updating one element.
Dan Gohman4f8d1232008-05-22 00:50:06 +0000926 unsigned InIdx = getAccessedFieldNo(0, Elts);
Chris Lattnereb99b012009-10-28 17:39:19 +0000927 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
928 llvm::Value *Elt = llvm::ConstantInt::get(Int32Ty, InIdx);
Chris Lattner017d6aa2007-08-03 16:28:33 +0000929 Vec = Builder.CreateInsertElement(Vec, SrcVal, Elt, "tmp");
Chris Lattner017d6aa2007-08-03 16:28:33 +0000930 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000931
Eli Friedman1e692ac2008-06-13 23:01:12 +0000932 Builder.CreateStore(Vec, Dst.getExtVectorAddr(), Dst.isVolatileQualified());
Chris Lattner017d6aa2007-08-03 16:28:33 +0000933}
934
Fariborz Jahanianb123ea32009-09-16 21:37:16 +0000935// setObjCGCLValueClass - sets class of he lvalue for the purpose of
936// generating write-barries API. It is currently a global, ivar,
937// or neither.
Chris Lattnereb99b012009-10-28 17:39:19 +0000938static void setObjCGCLValueClass(const ASTContext &Ctx, const Expr *E,
939 LValue &LV) {
Fariborz Jahanianb9242592009-09-21 23:03:37 +0000940 if (Ctx.getLangOptions().getGCMode() == LangOptions::NonGC)
Fariborz Jahanianb123ea32009-09-16 21:37:16 +0000941 return;
942
Fariborz Jahaniandbf3cfd2009-09-16 23:11:23 +0000943 if (isa<ObjCIvarRefExpr>(E)) {
944 LV.SetObjCIvar(LV, true);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000945 ObjCIvarRefExpr *Exp = cast<ObjCIvarRefExpr>(const_cast<Expr*>(E));
946 LV.setBaseIvarExp(Exp->getBase());
Fariborz Jahanianfd02ed72009-09-21 18:54:29 +0000947 LV.SetObjCArray(LV, E->getType()->isArrayType());
Fariborz Jahaniandbf3cfd2009-09-16 23:11:23 +0000948 return;
949 }
Chris Lattnereb99b012009-10-28 17:39:19 +0000950
Fariborz Jahanianb123ea32009-09-16 21:37:16 +0000951 if (const DeclRefExpr *Exp = dyn_cast<DeclRefExpr>(E)) {
952 if (const VarDecl *VD = dyn_cast<VarDecl>(Exp->getDecl())) {
953 if ((VD->isBlockVarDecl() && !VD->hasLocalStorage()) ||
954 VD->isFileVarDecl())
955 LV.SetGlobalObjCRef(LV, true);
956 }
Fariborz Jahanianfd02ed72009-09-21 18:54:29 +0000957 LV.SetObjCArray(LV, E->getType()->isArrayType());
Chris Lattnereb99b012009-10-28 17:39:19 +0000958 return;
Fariborz Jahanianb123ea32009-09-16 21:37:16 +0000959 }
Chris Lattnereb99b012009-10-28 17:39:19 +0000960
961 if (const UnaryOperator *Exp = dyn_cast<UnaryOperator>(E)) {
Fariborz Jahanianb123ea32009-09-16 21:37:16 +0000962 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
Chris Lattnereb99b012009-10-28 17:39:19 +0000963 return;
964 }
965
966 if (const ParenExpr *Exp = dyn_cast<ParenExpr>(E)) {
Fariborz Jahanianb123ea32009-09-16 21:37:16 +0000967 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
Fariborz Jahanian75b08f12009-09-30 17:10:29 +0000968 if (LV.isObjCIvar()) {
969 // If cast is to a structure pointer, follow gcc's behavior and make it
970 // a non-ivar write-barrier.
971 QualType ExpTy = E->getType();
972 if (ExpTy->isPointerType())
973 ExpTy = ExpTy->getAs<PointerType>()->getPointeeType();
974 if (ExpTy->isRecordType())
975 LV.SetObjCIvar(LV, false);
Chris Lattnereb99b012009-10-28 17:39:19 +0000976 }
977 return;
Fariborz Jahanian75b08f12009-09-30 17:10:29 +0000978 }
Chris Lattnereb99b012009-10-28 17:39:19 +0000979 if (const ImplicitCastExpr *Exp = dyn_cast<ImplicitCastExpr>(E)) {
Fariborz Jahanianb123ea32009-09-16 21:37:16 +0000980 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
Chris Lattnereb99b012009-10-28 17:39:19 +0000981 return;
982 }
983
984 if (const CStyleCastExpr *Exp = dyn_cast<CStyleCastExpr>(E)) {
Fariborz Jahanianb123ea32009-09-16 21:37:16 +0000985 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
Chris Lattnereb99b012009-10-28 17:39:19 +0000986 return;
987 }
988
989 if (const ArraySubscriptExpr *Exp = dyn_cast<ArraySubscriptExpr>(E)) {
Fariborz Jahanianb123ea32009-09-16 21:37:16 +0000990 setObjCGCLValueClass(Ctx, Exp->getBase(), LV);
Fariborz Jahanianfd02ed72009-09-21 18:54:29 +0000991 if (LV.isObjCIvar() && !LV.isObjCArray())
Fariborz Jahanian1c1afc42009-09-18 00:04:00 +0000992 // Using array syntax to assigning to what an ivar points to is not
993 // same as assigning to the ivar itself. {id *Names;} Names[i] = 0;
994 LV.SetObjCIvar(LV, false);
Fariborz Jahanianfd02ed72009-09-21 18:54:29 +0000995 else if (LV.isGlobalObjCRef() && !LV.isObjCArray())
996 // Using array syntax to assigning to what global points to is not
997 // same as assigning to the global itself. {id *G;} G[i] = 0;
998 LV.SetGlobalObjCRef(LV, false);
Chris Lattnereb99b012009-10-28 17:39:19 +0000999 return;
Fariborz Jahanian1c1afc42009-09-18 00:04:00 +00001000 }
Chris Lattnereb99b012009-10-28 17:39:19 +00001001
1002 if (const MemberExpr *Exp = dyn_cast<MemberExpr>(E)) {
Fariborz Jahanianb123ea32009-09-16 21:37:16 +00001003 setObjCGCLValueClass(Ctx, Exp->getBase(), LV);
Fariborz Jahanian1c1afc42009-09-18 00:04:00 +00001004 // We don't know if member is an 'ivar', but this flag is looked at
1005 // only in the context of LV.isObjCIvar().
Fariborz Jahanianfd02ed72009-09-21 18:54:29 +00001006 LV.SetObjCArray(LV, E->getType()->isArrayType());
Chris Lattnereb99b012009-10-28 17:39:19 +00001007 return;
Fariborz Jahanianb123ea32009-09-16 21:37:16 +00001008 }
1009}
1010
Anders Carlssonce53f7d2009-11-07 23:06:58 +00001011static LValue EmitGlobalVarDeclLValue(CodeGenFunction &CGF,
1012 const Expr *E, const VarDecl *VD) {
Daniel Dunbard2113f22009-11-08 09:46:46 +00001013 assert((VD->hasExternalStorage() || VD->isFileVarDecl()) &&
Anders Carlssonce53f7d2009-11-07 23:06:58 +00001014 "Var decl must have external storage or be a file var decl!");
1015
1016 llvm::Value *V = CGF.CGM.GetAddrOfGlobalVar(VD);
1017 if (VD->getType()->isReferenceType())
1018 V = CGF.Builder.CreateLoad(V, "tmp");
1019 LValue LV = LValue::MakeAddr(V, CGF.MakeQualifiers(E->getType()));
1020 setObjCGCLValueClass(CGF.getContext(), E, LV);
1021 return LV;
1022}
1023
Eli Friedman9a146302009-11-26 06:08:14 +00001024static LValue EmitFunctionDeclLValue(CodeGenFunction &CGF,
1025 const Expr *E, const FunctionDecl *FD) {
1026 llvm::Value* V = CGF.CGM.GetAddrOfFunction(FD);
1027 if (!FD->hasPrototype()) {
1028 if (const FunctionProtoType *Proto =
1029 FD->getType()->getAs<FunctionProtoType>()) {
1030 // Ugly case: for a K&R-style definition, the type of the definition
1031 // isn't the same as the type of a use. Correct for this with a
1032 // bitcast.
1033 QualType NoProtoType =
1034 CGF.getContext().getFunctionNoProtoType(Proto->getResultType());
1035 NoProtoType = CGF.getContext().getPointerType(NoProtoType);
1036 V = CGF.Builder.CreateBitCast(V, CGF.ConvertType(NoProtoType), "tmp");
1037 }
1038 }
1039 return LValue::MakeAddr(V, CGF.MakeQualifiers(E->getType()));
1040}
1041
Reid Spencer5f016e22007-07-11 17:01:13 +00001042LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) {
Anders Carlsson1e74c4f2009-11-07 22:53:10 +00001043 const NamedDecl *ND = E->getDecl();
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001044
Anders Carlsson1e74c4f2009-11-07 22:53:10 +00001045 if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
Anders Carlsson1e74c4f2009-11-07 22:53:10 +00001046
1047 // Check if this is a global variable.
Anders Carlssonce53f7d2009-11-07 23:06:58 +00001048 if (VD->hasExternalStorage() || VD->isFileVarDecl())
1049 return EmitGlobalVarDeclLValue(*this, E, VD);
Anders Carlsson0bc70492009-11-07 22:46:42 +00001050
1051 bool NonGCable = VD->hasLocalStorage() && !VD->hasAttr<BlocksAttr>();
1052
1053 llvm::Value *V = LocalDeclMap[VD];
1054 assert(V && "DeclRefExpr not entered in LocalDeclMap?");
1055
1056 Qualifiers Quals = MakeQualifiers(E->getType());
1057 // local variables do not get their gc attribute set.
1058 // local static?
1059 if (NonGCable) Quals.removeObjCGCAttr();
1060
1061 if (VD->hasAttr<BlocksAttr>()) {
1062 V = Builder.CreateStructGEP(V, 1, "forwarding");
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00001063 V = Builder.CreateLoad(V);
Anders Carlsson0bc70492009-11-07 22:46:42 +00001064 V = Builder.CreateStructGEP(V, getByRefValueLLVMField(VD),
1065 VD->getNameAsString());
1066 }
1067 if (VD->getType()->isReferenceType())
1068 V = Builder.CreateLoad(V, "tmp");
Anders Carlssonce53f7d2009-11-07 23:06:58 +00001069 LValue LV = LValue::MakeAddr(V, Quals);
Anders Carlsson0bc70492009-11-07 22:46:42 +00001070 LValue::SetObjCNonGC(LV, NonGCable);
Fariborz Jahanianb123ea32009-09-16 21:37:16 +00001071 setObjCGCLValueClass(getContext(), E, LV);
Fariborz Jahanian2682d8b2008-11-20 00:15:42 +00001072 return LV;
Chris Lattnereb99b012009-10-28 17:39:19 +00001073 }
1074
Eli Friedman9a146302009-11-26 06:08:14 +00001075 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
1076 return EmitFunctionDeclLValue(*this, E, FD);
Chris Lattnereb99b012009-10-28 17:39:19 +00001077
Anders Carlsson45147d02010-02-02 03:37:46 +00001078 // FIXME: the qualifier check does not seem sufficient here
Chris Lattnereb99b012009-10-28 17:39:19 +00001079 if (E->getQualifier()) {
Anders Carlsson45147d02010-02-02 03:37:46 +00001080 const FieldDecl *FD = cast<FieldDecl>(ND);
1081 llvm::Value *V = CGM.EmitPointerToDataMember(FD);
1082
1083 return LValue::MakeAddr(V, MakeQualifiers(FD->getType()));
Chris Lattner41110242008-06-17 18:05:57 +00001084 }
Chris Lattnereb99b012009-10-28 17:39:19 +00001085
Anders Carlsson1e74c4f2009-11-07 22:53:10 +00001086 assert(false && "Unhandled DeclRefExpr");
1087
1088 // an invalid LValue, but the assert will
1089 // ensure that this point is never reached.
Chris Lattnerb1776cb2007-09-16 19:23:47 +00001090 return LValue();
Reid Spencer5f016e22007-07-11 17:01:13 +00001091}
1092
Mike Stumpa99038c2009-02-28 09:07:16 +00001093LValue CodeGenFunction::EmitBlockDeclRefLValue(const BlockDeclRefExpr *E) {
John McCall0953e762009-09-24 19:53:00 +00001094 return LValue::MakeAddr(GetAddrOfBlockDecl(E), MakeQualifiers(E->getType()));
Mike Stumpa99038c2009-02-28 09:07:16 +00001095}
1096
Reid Spencer5f016e22007-07-11 17:01:13 +00001097LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) {
1098 // __extension__ doesn't affect lvalue-ness.
1099 if (E->getOpcode() == UnaryOperator::Extension)
1100 return EmitLValue(E->getSubExpr());
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001101
Chris Lattner96196622008-07-26 22:37:01 +00001102 QualType ExprTy = getContext().getCanonicalType(E->getSubExpr()->getType());
Chris Lattner7da36f62007-10-30 22:53:42 +00001103 switch (E->getOpcode()) {
1104 default: assert(0 && "Unknown unary operator lvalue!");
Chris Lattnereb99b012009-10-28 17:39:19 +00001105 case UnaryOperator::Deref: {
1106 QualType T = E->getSubExpr()->getType()->getPointeeType();
1107 assert(!T.isNull() && "CodeGenFunction::EmitUnaryOpLValue: Illegal type");
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001108
Chris Lattnereb99b012009-10-28 17:39:19 +00001109 Qualifiers Quals = MakeQualifiers(T);
1110 Quals.setAddressSpace(ExprTy.getAddressSpace());
John McCall0953e762009-09-24 19:53:00 +00001111
Chris Lattnereb99b012009-10-28 17:39:19 +00001112 LValue LV = LValue::MakeAddr(EmitScalarExpr(E->getSubExpr()), Quals);
1113 // We should not generate __weak write barrier on indirect reference
1114 // of a pointer to object; as in void foo (__weak id *param); *param = 0;
1115 // But, we continue to generate __strong write barrier on indirect write
1116 // into a pointer to object.
1117 if (getContext().getLangOptions().ObjC1 &&
1118 getContext().getLangOptions().getGCMode() != LangOptions::NonGC &&
1119 LV.isObjCWeak())
1120 LValue::SetObjCNonGC(LV, !E->isOBJCGCCandidate(getContext()));
1121 return LV;
1122 }
Chris Lattner7da36f62007-10-30 22:53:42 +00001123 case UnaryOperator::Real:
Eli Friedmane401cd52009-11-09 04:20:47 +00001124 case UnaryOperator::Imag: {
Chris Lattner7da36f62007-10-30 22:53:42 +00001125 LValue LV = EmitLValue(E->getSubExpr());
Chris Lattner36b6a0a2008-03-19 05:19:41 +00001126 unsigned Idx = E->getOpcode() == UnaryOperator::Imag;
1127 return LValue::MakeAddr(Builder.CreateStructGEP(LV.getAddress(),
Chris Lattnerb77792e2008-07-26 22:17:49 +00001128 Idx, "idx"),
John McCall0953e762009-09-24 19:53:00 +00001129 MakeQualifiers(ExprTy));
Chris Lattner7da36f62007-10-30 22:53:42 +00001130 }
Eli Friedmane401cd52009-11-09 04:20:47 +00001131 case UnaryOperator::PreInc:
Chris Lattner197a3382010-01-09 21:44:40 +00001132 case UnaryOperator::PreDec: {
1133 LValue LV = EmitLValue(E->getSubExpr());
1134 bool isInc = E->getOpcode() == UnaryOperator::PreInc;
1135
1136 if (E->getType()->isAnyComplexType())
1137 EmitComplexPrePostIncDec(E, LV, isInc, true/*isPre*/);
1138 else
1139 EmitScalarPrePostIncDec(E, LV, isInc, true/*isPre*/);
1140 return LV;
1141 }
Eli Friedmane401cd52009-11-09 04:20:47 +00001142 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001143}
1144
1145LValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) {
John McCall0953e762009-09-24 19:53:00 +00001146 return LValue::MakeAddr(CGM.GetAddrOfConstantStringFromLiteral(E),
1147 Qualifiers());
Reid Spencer5f016e22007-07-11 17:01:13 +00001148}
1149
Chris Lattnereaf2bb82009-02-24 22:18:39 +00001150LValue CodeGenFunction::EmitObjCEncodeExprLValue(const ObjCEncodeExpr *E) {
John McCall0953e762009-09-24 19:53:00 +00001151 return LValue::MakeAddr(CGM.GetAddrOfConstantStringFromObjCEncode(E),
1152 Qualifiers());
Chris Lattnereaf2bb82009-02-24 22:18:39 +00001153}
1154
1155
Daniel Dunbar662b71e2008-10-17 21:58:32 +00001156LValue CodeGenFunction::EmitPredefinedFunctionName(unsigned Type) {
Anders Carlsson22742662007-07-21 05:21:51 +00001157 std::string GlobalVarName;
Daniel Dunbar662b71e2008-10-17 21:58:32 +00001158
1159 switch (Type) {
Chris Lattnereb99b012009-10-28 17:39:19 +00001160 default: assert(0 && "Invalid type");
Chris Lattnereaf2bb82009-02-24 22:18:39 +00001161 case PredefinedExpr::Func:
1162 GlobalVarName = "__func__.";
1163 break;
1164 case PredefinedExpr::Function:
1165 GlobalVarName = "__FUNCTION__.";
1166 break;
1167 case PredefinedExpr::PrettyFunction:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00001168 GlobalVarName = "__PRETTY_FUNCTION__.";
1169 break;
Anders Carlsson22742662007-07-21 05:21:51 +00001170 }
Daniel Dunbar662b71e2008-10-17 21:58:32 +00001171
Daniel Dunbar0a23d762009-09-12 23:06:21 +00001172 llvm::StringRef FnName = CurFn->getName();
1173 if (FnName.startswith("\01"))
1174 FnName = FnName.substr(1);
1175 GlobalVarName += FnName;
1176
Anders Carlsson3a082d82009-09-08 18:24:21 +00001177 std::string FunctionName =
Mike Stump1eb44332009-09-09 15:08:12 +00001178 PredefinedExpr::ComputeName(getContext(), (PredefinedExpr::IdentType)Type,
Anders Carlsson3a082d82009-09-08 18:24:21 +00001179 CurCodeDecl);
Daniel Dunbar662b71e2008-10-17 21:58:32 +00001180
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001181 llvm::Constant *C =
Daniel Dunbar662b71e2008-10-17 21:58:32 +00001182 CGM.GetAddrOfConstantCString(FunctionName, GlobalVarName.c_str());
John McCall0953e762009-09-24 19:53:00 +00001183 return LValue::MakeAddr(C, Qualifiers());
Daniel Dunbar662b71e2008-10-17 21:58:32 +00001184}
1185
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001186LValue CodeGenFunction::EmitPredefinedLValue(const PredefinedExpr *E) {
Daniel Dunbar662b71e2008-10-17 21:58:32 +00001187 switch (E->getIdentType()) {
1188 default:
1189 return EmitUnsupportedLValue(E, "predefined expression");
1190 case PredefinedExpr::Func:
1191 case PredefinedExpr::Function:
1192 case PredefinedExpr::PrettyFunction:
1193 return EmitPredefinedFunctionName(E->getIdentType());
1194 }
Anders Carlsson22742662007-07-21 05:21:51 +00001195}
1196
Mike Stumpd8af3602009-12-15 01:22:35 +00001197llvm::BasicBlock *CodeGenFunction::getTrapBB() {
Mike Stump41513442009-12-15 00:59:40 +00001198 const CodeGenOptions &GCO = CGM.getCodeGenOpts();
1199
1200 // If we are not optimzing, don't collapse all calls to trap in the function
1201 // to the same call, that way, in the debugger they can see which operation
1202 // did in fact fail. If we are optimizing, we collpase all call to trap down
1203 // to just one per function to save on codesize.
1204 if (GCO.OptimizationLevel
1205 && TrapBB)
Mike Stump15037ca2009-12-15 00:35:12 +00001206 return TrapBB;
Mike Stump9c276ae2009-12-12 01:27:46 +00001207
1208 llvm::BasicBlock *Cont = 0;
1209 if (HaveInsertPoint()) {
1210 Cont = createBasicBlock("cont");
1211 EmitBranch(Cont);
1212 }
Mike Stump15037ca2009-12-15 00:35:12 +00001213 TrapBB = createBasicBlock("trap");
1214 EmitBlock(TrapBB);
1215
1216 llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::trap, 0, 0);
1217 llvm::CallInst *TrapCall = Builder.CreateCall(F);
1218 TrapCall->setDoesNotReturn();
1219 TrapCall->setDoesNotThrow();
Mike Stump9c276ae2009-12-12 01:27:46 +00001220 Builder.CreateUnreachable();
1221
1222 if (Cont)
1223 EmitBlock(Cont);
Mike Stump15037ca2009-12-15 00:35:12 +00001224 return TrapBB;
Mike Stump9c276ae2009-12-12 01:27:46 +00001225}
1226
Reid Spencer5f016e22007-07-11 17:01:13 +00001227LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) {
Ted Kremenek23245122007-08-20 16:18:38 +00001228 // The index must always be an integer, which is not an aggregate. Emit it.
Chris Lattner7f02f722007-08-24 05:35:26 +00001229 llvm::Value *Idx = EmitScalarExpr(E->getIdx());
Eli Friedman61d004a2009-06-06 19:09:26 +00001230 QualType IdxTy = E->getIdx()->getType();
1231 bool IdxSigned = IdxTy->isSignedIntegerType();
1232
Reid Spencer5f016e22007-07-11 17:01:13 +00001233 // If the base is a vector type, then we are forming a vector element lvalue
1234 // with this subscript.
Eli Friedman1e692ac2008-06-13 23:01:12 +00001235 if (E->getBase()->getType()->isVectorType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001236 // Emit the vector as an lvalue to get its address.
Eli Friedman1e692ac2008-06-13 23:01:12 +00001237 LValue LHS = EmitLValue(E->getBase());
Ted Kremenek23245122007-08-20 16:18:38 +00001238 assert(LHS.isSimple() && "Can only subscript lvalue vectors here!");
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001239 Idx = Builder.CreateIntCast(Idx,
Owen Anderson0032b272009-08-13 21:57:51 +00001240 llvm::Type::getInt32Ty(VMContext), IdxSigned, "vidx");
Eli Friedman1e692ac2008-06-13 23:01:12 +00001241 return LValue::MakeVectorElt(LHS.getAddress(), Idx,
John McCall0953e762009-09-24 19:53:00 +00001242 E->getBase()->getType().getCVRQualifiers());
Reid Spencer5f016e22007-07-11 17:01:13 +00001243 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001244
Ted Kremenek23245122007-08-20 16:18:38 +00001245 // The base must be a pointer, which is not an aggregate. Emit it.
Chris Lattner7f02f722007-08-24 05:35:26 +00001246 llvm::Value *Base = EmitScalarExpr(E->getBase());
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001247
Ted Kremenek23245122007-08-20 16:18:38 +00001248 // Extend or truncate the index type to 32 or 64-bits.
Reid Spencer5f016e22007-07-11 17:01:13 +00001249 unsigned IdxBitwidth = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
Sanjiv Gupta7cabee52009-04-24 02:40:57 +00001250 if (IdxBitwidth != LLVMPointerWidth)
Owen Anderson0032b272009-08-13 21:57:51 +00001251 Idx = Builder.CreateIntCast(Idx,
1252 llvm::IntegerType::get(VMContext, LLVMPointerWidth),
Reid Spencer5f016e22007-07-11 17:01:13 +00001253 IdxSigned, "idxprom");
1254
Mike Stumpb14e62d2009-12-16 02:57:00 +00001255 // FIXME: As llvm implements the object size checking, this can come out.
Mike Stump9c276ae2009-12-12 01:27:46 +00001256 if (CatchUndefined) {
Mike Stumpb14e62d2009-12-16 02:57:00 +00001257 if (const ImplicitCastExpr *ICE=dyn_cast<ImplicitCastExpr>(E->getBase())) {
Mike Stump9c276ae2009-12-12 01:27:46 +00001258 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr())) {
1259 if (ICE->getCastKind() == CastExpr::CK_ArrayToPointerDecay) {
1260 if (const ConstantArrayType *CAT
1261 = getContext().getAsConstantArrayType(DRE->getType())) {
1262 llvm::APInt Size = CAT->getSize();
1263 llvm::BasicBlock *Cont = createBasicBlock("cont");
Mike Stump750c85e2009-12-14 22:14:31 +00001264 Builder.CreateCondBr(Builder.CreateICmpULE(Idx,
Mike Stump9c276ae2009-12-12 01:27:46 +00001265 llvm::ConstantInt::get(Idx->getType(), Size)),
Mike Stump15037ca2009-12-15 00:35:12 +00001266 Cont, getTrapBB());
Mike Stump96a063a2009-12-14 20:52:00 +00001267 EmitBlock(Cont);
Mike Stump9c276ae2009-12-12 01:27:46 +00001268 }
1269 }
1270 }
1271 }
1272 }
1273
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001274 // We know that the pointer points to a type of the correct size, unless the
1275 // size is a VLA or Objective-C interface.
Daniel Dunbar2a866252009-04-25 05:08:32 +00001276 llvm::Value *Address = 0;
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001277 if (const VariableArrayType *VAT =
Anders Carlsson8b33c082008-12-21 00:11:23 +00001278 getContext().getAsVariableArrayType(E->getType())) {
Chris Lattner881eb9c2009-08-14 23:43:22 +00001279 llvm::Value *VLASize = GetVLASize(VAT);
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001280
Anders Carlsson8b33c082008-12-21 00:11:23 +00001281 Idx = Builder.CreateMul(Idx, VLASize);
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001282
Anders Carlsson6183a992008-12-21 03:44:36 +00001283 QualType BaseType = getContext().getBaseElementType(VAT);
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001284
Ken Dyck199c3d62010-01-11 17:06:35 +00001285 CharUnits BaseTypeSize = getContext().getTypeSizeInChars(BaseType);
Anders Carlsson8b33c082008-12-21 00:11:23 +00001286 Idx = Builder.CreateUDiv(Idx,
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001287 llvm::ConstantInt::get(Idx->getType(),
Ken Dyck199c3d62010-01-11 17:06:35 +00001288 BaseTypeSize.getQuantity()));
Dan Gohman664f8932009-08-12 00:33:55 +00001289 Address = Builder.CreateInBoundsGEP(Base, Idx, "arrayidx");
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001290 } else if (const ObjCInterfaceType *OIT =
Daniel Dunbar2a866252009-04-25 05:08:32 +00001291 dyn_cast<ObjCInterfaceType>(E->getType())) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001292 llvm::Value *InterfaceSize =
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001293 llvm::ConstantInt::get(Idx->getType(),
Ken Dyck199c3d62010-01-11 17:06:35 +00001294 getContext().getTypeSizeInChars(OIT).getQuantity());
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001295
Daniel Dunbar2a866252009-04-25 05:08:32 +00001296 Idx = Builder.CreateMul(Idx, InterfaceSize);
1297
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00001298 const llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(VMContext);
Dan Gohman664f8932009-08-12 00:33:55 +00001299 Address = Builder.CreateGEP(Builder.CreateBitCast(Base, i8PTy),
Daniel Dunbar2a866252009-04-25 05:08:32 +00001300 Idx, "arrayidx");
1301 Address = Builder.CreateBitCast(Address, Base->getType());
1302 } else {
Dan Gohman664f8932009-08-12 00:33:55 +00001303 Address = Builder.CreateInBoundsGEP(Base, Idx, "arrayidx");
Anders Carlsson8b33c082008-12-21 00:11:23 +00001304 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001305
Steve Naroff14108da2009-07-10 23:34:53 +00001306 QualType T = E->getBase()->getType()->getPointeeType();
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001307 assert(!T.isNull() &&
Steve Naroff14108da2009-07-10 23:34:53 +00001308 "CodeGenFunction::EmitArraySubscriptExpr(): Illegal base type");
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001309
John McCall0953e762009-09-24 19:53:00 +00001310 Qualifiers Quals = MakeQualifiers(T);
1311 Quals.setAddressSpace(E->getBase()->getType().getAddressSpace());
1312
1313 LValue LV = LValue::MakeAddr(Address, Quals);
Fariborz Jahanian643887a2009-02-21 23:37:19 +00001314 if (getContext().getLangOptions().ObjC1 &&
Fariborz Jahanianb123ea32009-09-16 21:37:16 +00001315 getContext().getLangOptions().getGCMode() != LangOptions::NonGC) {
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001316 LValue::SetObjCNonGC(LV, !E->isOBJCGCCandidate(getContext()));
Fariborz Jahanianb123ea32009-09-16 21:37:16 +00001317 setObjCGCLValueClass(getContext(), E, LV);
1318 }
Fariborz Jahanian643887a2009-02-21 23:37:19 +00001319 return LV;
Reid Spencer5f016e22007-07-11 17:01:13 +00001320}
1321
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001322static
Owen Andersona1cf15f2009-07-14 23:10:40 +00001323llvm::Constant *GenerateConstantVector(llvm::LLVMContext &VMContext,
1324 llvm::SmallVector<unsigned, 4> &Elts) {
Chris Lattner998eab12009-12-23 21:31:11 +00001325 llvm::SmallVector<llvm::Constant*, 4> CElts;
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001326
Nate Begeman3b8d1162008-05-13 21:03:02 +00001327 for (unsigned i = 0, e = Elts.size(); i != e; ++i)
Owen Anderson0032b272009-08-13 21:57:51 +00001328 CElts.push_back(llvm::ConstantInt::get(
1329 llvm::Type::getInt32Ty(VMContext), Elts[i]));
Nate Begeman3b8d1162008-05-13 21:03:02 +00001330
Owen Anderson4a289322009-07-28 21:22:35 +00001331 return llvm::ConstantVector::get(&CElts[0], CElts.size());
Nate Begeman3b8d1162008-05-13 21:03:02 +00001332}
1333
Chris Lattner349aaec2007-08-02 23:37:31 +00001334LValue CodeGenFunction::
Nate Begeman213541a2008-04-18 23:10:10 +00001335EmitExtVectorElementExpr(const ExtVectorElementExpr *E) {
Chris Lattner998eab12009-12-23 21:31:11 +00001336 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
1337
Chris Lattner349aaec2007-08-02 23:37:31 +00001338 // Emit the base vector as an l-value.
Chris Lattner73525de2009-02-16 21:11:58 +00001339 LValue Base;
1340
1341 // ExtVectorElementExpr's base can either be a vector or pointer to vector.
Chris Lattner998eab12009-12-23 21:31:11 +00001342 if (E->isArrow()) {
1343 // If it is a pointer to a vector, emit the address and form an lvalue with
1344 // it.
Chris Lattner2140e902009-02-16 22:14:05 +00001345 llvm::Value *Ptr = EmitScalarExpr(E->getBase());
Chris Lattner998eab12009-12-23 21:31:11 +00001346 const PointerType *PT = E->getBase()->getType()->getAs<PointerType>();
John McCall0953e762009-09-24 19:53:00 +00001347 Qualifiers Quals = MakeQualifiers(PT->getPointeeType());
1348 Quals.removeObjCGCAttr();
1349 Base = LValue::MakeAddr(Ptr, Quals);
Chris Lattner998eab12009-12-23 21:31:11 +00001350 } else if (E->getBase()->isLvalue(getContext()) == Expr::LV_Valid) {
1351 // Otherwise, if the base is an lvalue ( as in the case of foo.x.x),
1352 // emit the base as an lvalue.
1353 assert(E->getBase()->getType()->isVectorType());
1354 Base = EmitLValue(E->getBase());
1355 } else {
1356 // Otherwise, the base is a normal rvalue (as in (V+V).x), emit it as such.
Daniel Dunbar302c3c22010-01-04 18:02:28 +00001357 assert(E->getBase()->getType()->getAs<VectorType>() &&
1358 "Result must be a vector");
Chris Lattner998eab12009-12-23 21:31:11 +00001359 llvm::Value *Vec = EmitScalarExpr(E->getBase());
1360
Chris Lattner0ad57fb2009-12-23 21:33:41 +00001361 // Store the vector to memory (because LValue wants an address).
Daniel Dunbarea485222010-02-05 18:56:49 +00001362 llvm::Value *VecMem = CreateTempAlloca(ConvertTypeForMem(
1363 E->getBase()->getType()));
Chris Lattner998eab12009-12-23 21:31:11 +00001364 Builder.CreateStore(Vec, VecMem);
Chris Lattner0ad57fb2009-12-23 21:33:41 +00001365 Base = LValue::MakeAddr(VecMem, Qualifiers());
Chris Lattner998eab12009-12-23 21:31:11 +00001366 }
1367
Nate Begeman3b8d1162008-05-13 21:03:02 +00001368 // Encode the element access list into a vector of unsigned indices.
1369 llvm::SmallVector<unsigned, 4> Indices;
1370 E->getEncodedElementAccess(Indices);
1371
1372 if (Base.isSimple()) {
Owen Andersona1cf15f2009-07-14 23:10:40 +00001373 llvm::Constant *CV = GenerateConstantVector(VMContext, Indices);
Eli Friedman1e692ac2008-06-13 23:01:12 +00001374 return LValue::MakeExtVectorElt(Base.getAddress(), CV,
John McCall0953e762009-09-24 19:53:00 +00001375 Base.getVRQualifiers());
Nate Begeman3b8d1162008-05-13 21:03:02 +00001376 }
1377 assert(Base.isExtVectorElt() && "Can only subscript lvalue vec elts here!");
1378
1379 llvm::Constant *BaseElts = Base.getExtVectorElts();
1380 llvm::SmallVector<llvm::Constant *, 4> CElts;
1381
1382 for (unsigned i = 0, e = Indices.size(); i != e; ++i) {
1383 if (isa<llvm::ConstantAggregateZero>(BaseElts))
Chris Lattner67665862009-10-28 05:12:07 +00001384 CElts.push_back(llvm::ConstantInt::get(Int32Ty, 0));
Nate Begeman3b8d1162008-05-13 21:03:02 +00001385 else
Chris Lattner67665862009-10-28 05:12:07 +00001386 CElts.push_back(cast<llvm::Constant>(BaseElts->getOperand(Indices[i])));
Nate Begeman3b8d1162008-05-13 21:03:02 +00001387 }
Owen Anderson4a289322009-07-28 21:22:35 +00001388 llvm::Constant *CV = llvm::ConstantVector::get(&CElts[0], CElts.size());
Eli Friedman1e692ac2008-06-13 23:01:12 +00001389 return LValue::MakeExtVectorElt(Base.getExtVectorAddr(), CV,
John McCall0953e762009-09-24 19:53:00 +00001390 Base.getVRQualifiers());
Chris Lattner349aaec2007-08-02 23:37:31 +00001391}
1392
Devang Patelb9b00ad2007-10-23 20:28:39 +00001393LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) {
Fariborz Jahanian4f676ed2009-02-21 00:30:43 +00001394 bool isNonGC = false;
Devang Patel126a8562007-10-24 22:26:28 +00001395 Expr *BaseExpr = E->getBase();
Devang Patel126a8562007-10-24 22:26:28 +00001396 llvm::Value *BaseValue = NULL;
John McCall0953e762009-09-24 19:53:00 +00001397 Qualifiers BaseQuals;
Eli Friedman1e692ac2008-06-13 23:01:12 +00001398
Chris Lattner12f65f62007-12-02 18:52:07 +00001399 // 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 +00001400 if (E->isArrow()) {
Devang Patel0a961182007-10-26 18:15:21 +00001401 BaseValue = EmitScalarExpr(BaseExpr);
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001402 const PointerType *PTy =
Ted Kremenek6217b802009-07-29 21:53:49 +00001403 BaseExpr->getType()->getAs<PointerType>();
John McCall0953e762009-09-24 19:53:00 +00001404 BaseQuals = PTy->getPointeeType().getQualifiers();
Fariborz Jahaniand2e1eb02009-09-01 17:02:21 +00001405 } else if (isa<ObjCPropertyRefExpr>(BaseExpr->IgnoreParens()) ||
1406 isa<ObjCImplicitSetterGetterRefExpr>(
1407 BaseExpr->IgnoreParens())) {
Fariborz Jahanian35c33292009-01-12 23:27:26 +00001408 RValue RV = EmitObjCPropertyGet(BaseExpr);
1409 BaseValue = RV.getAggregateAddr();
John McCall0953e762009-09-24 19:53:00 +00001410 BaseQuals = BaseExpr->getType().getQualifiers();
Chris Lattner1bd885e2009-02-16 22:25:49 +00001411 } else {
Chris Lattner12f65f62007-12-02 18:52:07 +00001412 LValue BaseLV = EmitLValue(BaseExpr);
Fariborz Jahanian4f676ed2009-02-21 00:30:43 +00001413 if (BaseLV.isNonGC())
1414 isNonGC = true;
Chris Lattner12f65f62007-12-02 18:52:07 +00001415 // FIXME: this isn't right for bitfields.
1416 BaseValue = BaseLV.getAddress();
Fariborz Jahaniana91d6a62009-07-29 00:44:13 +00001417 QualType BaseTy = BaseExpr->getType();
John McCall0953e762009-09-24 19:53:00 +00001418 BaseQuals = BaseTy.getQualifiers();
Chris Lattner12f65f62007-12-02 18:52:07 +00001419 }
Devang Patelb9b00ad2007-10-23 20:28:39 +00001420
Anders Carlssonce53f7d2009-11-07 23:06:58 +00001421 NamedDecl *ND = E->getMemberDecl();
1422 if (FieldDecl *Field = dyn_cast<FieldDecl>(ND)) {
Anders Carlssone6d2a532010-01-29 05:05:36 +00001423 LValue LV = EmitLValueForField(BaseValue, Field,
Anders Carlssonce53f7d2009-11-07 23:06:58 +00001424 BaseQuals.getCVRQualifiers());
1425 LValue::SetObjCNonGC(LV, isNonGC);
1426 setObjCGCLValueClass(getContext(), E, LV);
1427 return LV;
1428 }
1429
Anders Carlsson589f9e32009-11-07 23:16:50 +00001430 if (VarDecl *VD = dyn_cast<VarDecl>(ND))
1431 return EmitGlobalVarDeclLValue(*this, E, VD);
Eli Friedman9a146302009-11-26 06:08:14 +00001432
1433 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
1434 return EmitFunctionDeclLValue(*this, E, FD);
1435
Anders Carlssonce53f7d2009-11-07 23:06:58 +00001436 assert(false && "Unhandled member declaration!");
1437 return LValue();
Eli Friedman472778e2008-02-09 08:50:58 +00001438}
Devang Patelb9b00ad2007-10-23 20:28:39 +00001439
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +00001440LValue CodeGenFunction::EmitLValueForBitfield(llvm::Value* BaseValue,
Anders Carlsson0ed303c2009-11-17 03:57:07 +00001441 const FieldDecl* Field,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001442 unsigned CVRQualifiers) {
Anders Carlsson8330cee2009-07-23 17:01:21 +00001443 CodeGenTypes::BitFieldInfo Info = CGM.getTypes().getBitFieldInfo(Field);
1444
Mike Stumpf5408fe2009-05-16 07:57:57 +00001445 // FIXME: CodeGenTypes should expose a method to get the appropriate type for
1446 // FieldTy (the appropriate type is ABI-dependent).
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001447 const llvm::Type *FieldTy =
Daniel Dunbarbb767732009-02-17 18:31:04 +00001448 CGM.getTypes().ConvertTypeForMem(Field->getType());
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +00001449 const llvm::PointerType *BaseTy =
1450 cast<llvm::PointerType>(BaseValue->getType());
1451 unsigned AS = BaseTy->getAddressSpace();
1452 BaseValue = Builder.CreateBitCast(BaseValue,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001453 llvm::PointerType::get(FieldTy, AS),
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +00001454 "tmp");
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001455
1456 llvm::Value *Idx =
Owen Anderson0032b272009-08-13 21:57:51 +00001457 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), Info.FieldNo);
Anders Carlsson8330cee2009-07-23 17:01:21 +00001458 llvm::Value *V = Builder.CreateGEP(BaseValue, Idx, "tmp");
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001459
Anders Carlsson8330cee2009-07-23 17:01:21 +00001460 return LValue::MakeBitfield(V, Info.Start, Info.Size,
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +00001461 Field->getType()->isSignedIntegerType(),
1462 Field->getType().getCVRQualifiers()|CVRQualifiers);
1463}
1464
Eli Friedman472778e2008-02-09 08:50:58 +00001465LValue CodeGenFunction::EmitLValueForField(llvm::Value* BaseValue,
Anders Carlsson0ed303c2009-11-17 03:57:07 +00001466 const FieldDecl* Field,
Mike Stump1eb44332009-09-09 15:08:12 +00001467 unsigned CVRQualifiers) {
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +00001468 if (Field->isBitField())
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001469 return EmitLValueForBitfield(BaseValue, Field, CVRQualifiers);
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001470
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001471 unsigned idx = CGM.getTypes().getLLVMFieldNo(Field);
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +00001472 llvm::Value *V = Builder.CreateStructGEP(BaseValue, idx, "tmp");
Eli Friedman1e86b342008-05-29 11:33:25 +00001473
Devang Patelabad06c2007-10-26 19:42:18 +00001474 // Match union field type.
Anders Carlssone6d2a532010-01-29 05:05:36 +00001475 if (Field->getParent()->isUnion()) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001476 const llvm::Type *FieldTy =
Eli Friedman1e692ac2008-06-13 23:01:12 +00001477 CGM.getTypes().ConvertTypeForMem(Field->getType());
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001478 const llvm::PointerType * BaseTy =
Devang Patele9b8c0a2007-10-30 20:59:40 +00001479 cast<llvm::PointerType>(BaseValue->getType());
Eli Friedman788d5712008-05-21 13:24:44 +00001480 unsigned AS = BaseTy->getAddressSpace();
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001481 V = Builder.CreateBitCast(V,
1482 llvm::PointerType::get(FieldTy, AS),
Eli Friedman788d5712008-05-21 13:24:44 +00001483 "tmp");
Devang Patelabad06c2007-10-26 19:42:18 +00001484 }
Eli Friedman2be58612009-05-30 21:09:44 +00001485 if (Field->getType()->isReferenceType())
1486 V = Builder.CreateLoad(V, "tmp");
John McCall0953e762009-09-24 19:53:00 +00001487
1488 Qualifiers Quals = MakeQualifiers(Field->getType());
1489 Quals.addCVRQualifiers(CVRQualifiers);
Fariborz Jahanianfd02ed72009-09-21 18:54:29 +00001490 // __weak attribute on a field is ignored.
John McCall0953e762009-09-24 19:53:00 +00001491 if (Quals.getObjCGCAttr() == Qualifiers::Weak)
1492 Quals.removeObjCGCAttr();
Fariborz Jahanianfd02ed72009-09-21 18:54:29 +00001493
John McCall0953e762009-09-24 19:53:00 +00001494 return LValue::MakeAddr(V, Quals);
Devang Patelb9b00ad2007-10-23 20:28:39 +00001495}
1496
Anders Carlsson06a29702010-01-29 05:24:29 +00001497LValue
1498CodeGenFunction::EmitLValueForFieldInitialization(llvm::Value* BaseValue,
1499 const FieldDecl* Field,
1500 unsigned CVRQualifiers) {
1501 QualType FieldType = Field->getType();
1502
1503 if (!FieldType->isReferenceType())
1504 return EmitLValueForField(BaseValue, Field, CVRQualifiers);
1505
1506 unsigned idx = CGM.getTypes().getLLVMFieldNo(Field);
1507 llvm::Value *V = Builder.CreateStructGEP(BaseValue, idx, "tmp");
1508
1509 assert(!FieldType.getObjCGCAttr() && "fields cannot have GC attrs");
1510
1511 return LValue::MakeAddr(V, MakeQualifiers(FieldType));
1512}
1513
Chris Lattner75dfeda2009-03-18 18:28:57 +00001514LValue CodeGenFunction::EmitCompoundLiteralLValue(const CompoundLiteralExpr* E){
Daniel Dunbarb2cd7772010-02-05 20:02:42 +00001515 llvm::Value *DeclPtr = CreateTempAlloca(ConvertTypeForMem(E->getType()),
1516 ".compoundliteral");
Eli Friedman06e863f2008-05-13 23:18:27 +00001517
1518 const Expr* InitExpr = E->getInitializer();
John McCall0953e762009-09-24 19:53:00 +00001519 LValue Result = LValue::MakeAddr(DeclPtr, MakeQualifiers(E->getType()));
Eli Friedman06e863f2008-05-13 23:18:27 +00001520
Chris Lattnereb99b012009-10-28 17:39:19 +00001521 if (E->getType()->isComplexType())
Eli Friedman06e863f2008-05-13 23:18:27 +00001522 EmitComplexExprIntoAddr(InitExpr, DeclPtr, false);
Chris Lattnereb99b012009-10-28 17:39:19 +00001523 else if (hasAggregateLLVMType(E->getType()))
Eli Friedman06e863f2008-05-13 23:18:27 +00001524 EmitAnyExpr(InitExpr, DeclPtr, false);
Chris Lattnereb99b012009-10-28 17:39:19 +00001525 else
Eli Friedman06e863f2008-05-13 23:18:27 +00001526 EmitStoreThroughLValue(EmitAnyExpr(InitExpr), Result, E->getType());
Eli Friedman06e863f2008-05-13 23:18:27 +00001527
1528 return Result;
1529}
1530
Anders Carlsson6fcec8b2009-09-15 16:35:24 +00001531LValue
1532CodeGenFunction::EmitConditionalOperatorLValue(const ConditionalOperator* E) {
1533 if (E->isLvalue(getContext()) == Expr::LV_Valid) {
Eli Friedmanab189952009-12-25 05:29:40 +00001534 if (int Cond = ConstantFoldsToSimpleInteger(E->getCond())) {
1535 Expr *Live = Cond == 1 ? E->getLHS() : E->getRHS();
1536 if (Live)
1537 return EmitLValue(Live);
1538 }
1539
1540 if (!E->getLHS())
1541 return EmitUnsupportedLValue(E, "conditional operator with missing LHS");
1542
Anders Carlsson6fcec8b2009-09-15 16:35:24 +00001543 llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true");
1544 llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false");
1545 llvm::BasicBlock *ContBlock = createBasicBlock("cond.end");
1546
Eli Friedman8e274bd2009-12-25 06:17:05 +00001547 EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock);
Anders Carlsson6fcec8b2009-09-15 16:35:24 +00001548
Anders Carlssonc1b32f62010-02-04 17:26:01 +00001549 // Any temporaries created here are conditional.
1550 BeginConditionalBranch();
Anders Carlsson6fcec8b2009-09-15 16:35:24 +00001551 EmitBlock(LHSBlock);
Anders Carlsson6fcec8b2009-09-15 16:35:24 +00001552 LValue LHS = EmitLValue(E->getLHS());
Anders Carlssonc1b32f62010-02-04 17:26:01 +00001553 EndConditionalBranch();
1554
Anders Carlsson6fcec8b2009-09-15 16:35:24 +00001555 if (!LHS.isSimple())
1556 return EmitUnsupportedLValue(E, "conditional operator");
1557
Chris Lattnereb99b012009-10-28 17:39:19 +00001558 llvm::Value *Temp = CreateTempAlloca(LHS.getAddress()->getType(),"condtmp");
Anders Carlsson6fcec8b2009-09-15 16:35:24 +00001559 Builder.CreateStore(LHS.getAddress(), Temp);
1560 EmitBranch(ContBlock);
1561
Anders Carlssonc1b32f62010-02-04 17:26:01 +00001562 // Any temporaries created here are conditional.
1563 BeginConditionalBranch();
Anders Carlsson6fcec8b2009-09-15 16:35:24 +00001564 EmitBlock(RHSBlock);
1565 LValue RHS = EmitLValue(E->getRHS());
Anders Carlssonc1b32f62010-02-04 17:26:01 +00001566 EndConditionalBranch();
Anders Carlsson6fcec8b2009-09-15 16:35:24 +00001567 if (!RHS.isSimple())
1568 return EmitUnsupportedLValue(E, "conditional operator");
1569
1570 Builder.CreateStore(RHS.getAddress(), Temp);
1571 EmitBranch(ContBlock);
1572
1573 EmitBlock(ContBlock);
1574
1575 Temp = Builder.CreateLoad(Temp, "lv");
John McCall0953e762009-09-24 19:53:00 +00001576 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
Anders Carlsson6fcec8b2009-09-15 16:35:24 +00001577 }
1578
Daniel Dunbar90345582009-03-24 02:38:23 +00001579 // ?: here should be an aggregate.
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001580 assert((hasAggregateLLVMType(E->getType()) &&
Daniel Dunbar90345582009-03-24 02:38:23 +00001581 !E->getType()->isAnyComplexType()) &&
1582 "Unexpected conditional operator!");
1583
Daniel Dunbar18aba0d2010-02-05 19:38:31 +00001584 return EmitAggExprToLValue(E);
Daniel Dunbar90345582009-03-24 02:38:23 +00001585}
1586
Mike Stumpc849c052009-11-16 06:50:58 +00001587/// EmitCastLValue - Casts are never lvalues unless that cast is a dynamic_cast.
1588/// If the cast is a dynamic_cast, we can have the usual lvalue result,
1589/// otherwise if a cast is needed by the code generator in an lvalue context,
1590/// then it must mean that we need the address of an aggregate in order to
1591/// access one of its fields. This can happen for all the reasons that casts
1592/// are permitted with aggregate result, including noop aggregate casts, and
1593/// cast from scalar to union.
Chris Lattner75dfeda2009-03-18 18:28:57 +00001594LValue CodeGenFunction::EmitCastLValue(const CastExpr *E) {
Anders Carlsson0ee33cf2009-09-12 16:16:49 +00001595 switch (E->getCastKind()) {
1596 default:
Eli Friedmaneaae78a2009-11-16 05:48:01 +00001597 return EmitUnsupportedLValue(E, "unexpected cast lvalue");
1598
Mike Stumpc849c052009-11-16 06:50:58 +00001599 case CastExpr::CK_Dynamic: {
1600 LValue LV = EmitLValue(E->getSubExpr());
1601 llvm::Value *V = LV.getAddress();
1602 const CXXDynamicCastExpr *DCE = cast<CXXDynamicCastExpr>(E);
1603 return LValue::MakeAddr(EmitDynamicCast(V, DCE),
1604 MakeQualifiers(E->getType()));
1605 }
1606
Anders Carlsson0ee33cf2009-09-12 16:16:49 +00001607 case CastExpr::CK_NoOp:
1608 case CastExpr::CK_ConstructorConversion:
1609 case CastExpr::CK_UserDefinedConversion:
Fariborz Jahaniana7fa7cd2009-12-15 21:34:52 +00001610 case CastExpr::CK_AnyPointerToObjCPointerCast:
Chris Lattner75dfeda2009-03-18 18:28:57 +00001611 return EmitLValue(E->getSubExpr());
Anders Carlsson0ee33cf2009-09-12 16:16:49 +00001612
1613 case CastExpr::CK_DerivedToBase: {
1614 const RecordType *DerivedClassTy =
1615 E->getSubExpr()->getType()->getAs<RecordType>();
1616 CXXRecordDecl *DerivedClassDecl =
1617 cast<CXXRecordDecl>(DerivedClassTy->getDecl());
Chris Lattner75dfeda2009-03-18 18:28:57 +00001618
Anders Carlsson0ee33cf2009-09-12 16:16:49 +00001619 const RecordType *BaseClassTy = E->getType()->getAs<RecordType>();
1620 CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseClassTy->getDecl());
1621
1622 LValue LV = EmitLValue(E->getSubExpr());
1623
1624 // Perform the derived-to-base conversion
1625 llvm::Value *Base =
Anders Carlssona3697c92009-11-23 17:57:54 +00001626 GetAddressOfBaseClass(LV.getAddress(), DerivedClassDecl,
1627 BaseClassDecl, /*NullCheckValue=*/false);
Anders Carlsson0ee33cf2009-09-12 16:16:49 +00001628
John McCall0953e762009-09-24 19:53:00 +00001629 return LValue::MakeAddr(Base, MakeQualifiers(E->getType()));
Anders Carlsson0ee33cf2009-09-12 16:16:49 +00001630 }
Daniel Dunbarb2cd7772010-02-05 20:02:42 +00001631 case CastExpr::CK_ToUnion:
1632 return EmitAggExprToLValue(E);
Eli Friedmaneaae78a2009-11-16 05:48:01 +00001633 case CastExpr::CK_BaseToDerived: {
Anders Carlssona3697c92009-11-23 17:57:54 +00001634 const RecordType *BaseClassTy =
1635 E->getSubExpr()->getType()->getAs<RecordType>();
1636 CXXRecordDecl *BaseClassDecl =
1637 cast<CXXRecordDecl>(BaseClassTy->getDecl());
1638
1639 const RecordType *DerivedClassTy = E->getType()->getAs<RecordType>();
1640 CXXRecordDecl *DerivedClassDecl =
1641 cast<CXXRecordDecl>(DerivedClassTy->getDecl());
1642
1643 LValue LV = EmitLValue(E->getSubExpr());
1644
1645 // Perform the base-to-derived conversion
1646 llvm::Value *Derived =
1647 GetAddressOfDerivedClass(LV.getAddress(), BaseClassDecl,
1648 DerivedClassDecl, /*NullCheckValue=*/false);
1649
1650 return LValue::MakeAddr(Derived, MakeQualifiers(E->getType()));
Eli Friedmaneaae78a2009-11-16 05:48:01 +00001651 }
Anders Carlsson658e8122009-11-14 21:21:42 +00001652 case CastExpr::CK_BitCast: {
Eli Friedmaneaae78a2009-11-16 05:48:01 +00001653 // This must be a reinterpret_cast (or c-style equivalent).
1654 const ExplicitCastExpr *CE = cast<ExplicitCastExpr>(E);
Anders Carlsson658e8122009-11-14 21:21:42 +00001655
1656 LValue LV = EmitLValue(E->getSubExpr());
1657 llvm::Value *V = Builder.CreateBitCast(LV.getAddress(),
1658 ConvertType(CE->getTypeAsWritten()));
1659 return LValue::MakeAddr(V, MakeQualifiers(E->getType()));
1660 }
Anders Carlsson0ee33cf2009-09-12 16:16:49 +00001661 }
Chris Lattner75dfeda2009-03-18 18:28:57 +00001662}
1663
Fariborz Jahanian48620ba2009-10-20 23:29:04 +00001664LValue CodeGenFunction::EmitNullInitializationLValue(
1665 const CXXZeroInitValueExpr *E) {
1666 QualType Ty = E->getType();
Daniel Dunbarb2cd7772010-02-05 20:02:42 +00001667 llvm::AllocaInst *Alloc = CreateTempAlloca(ConvertTypeForMem(Ty));
Ken Dyck3228f422010-01-26 18:35:11 +00001668 CharUnits Align = getContext().getTypeAlignInChars(Ty);
1669 Alloc->setAlignment(Align.getQuantity());
Fariborz Jahanian48620ba2009-10-20 23:29:04 +00001670 LValue lvalue = LValue::MakeAddr(Alloc, Qualifiers());
1671 EmitMemSetToZero(lvalue.getAddress(), Ty);
1672 return lvalue;
1673}
1674
Reid Spencer5f016e22007-07-11 17:01:13 +00001675//===--------------------------------------------------------------------===//
1676// Expression Emission
1677//===--------------------------------------------------------------------===//
1678
Chris Lattner7016a702007-08-20 22:37:10 +00001679
Anders Carlssond2490a92009-12-24 20:40:36 +00001680RValue CodeGenFunction::EmitCallExpr(const CallExpr *E,
1681 ReturnValueSlot ReturnValue) {
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +00001682 // Builtins never have block type.
Daniel Dunbarce1d38b2009-01-09 16:50:52 +00001683 if (E->getCallee()->getType()->isBlockPointerType())
Anders Carlssona1736c02009-12-24 21:13:40 +00001684 return EmitBlockCallExpr(E, ReturnValue);
Daniel Dunbarce1d38b2009-01-09 16:50:52 +00001685
Anders Carlsson774e7c62009-04-03 22:50:24 +00001686 if (const CXXMemberCallExpr *CE = dyn_cast<CXXMemberCallExpr>(E))
Anders Carlssona1736c02009-12-24 21:13:40 +00001687 return EmitCXXMemberCallExpr(CE, ReturnValue);
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001688
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +00001689 const Decl *TargetDecl = 0;
Daniel Dunbardd7b8972009-02-20 19:34:33 +00001690 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E->getCallee())) {
1691 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CE->getSubExpr())) {
1692 TargetDecl = DRE->getDecl();
1693 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(TargetDecl))
Douglas Gregor7814e6d2009-09-12 00:22:50 +00001694 if (unsigned builtinID = FD->getBuiltinID())
Daniel Dunbardd7b8972009-02-20 19:34:33 +00001695 return EmitBuiltinExpr(FD, builtinID, E);
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +00001696 }
1697 }
1698
Chris Lattner5db7ae52009-06-13 00:26:38 +00001699 if (const CXXOperatorCallExpr *CE = dyn_cast<CXXOperatorCallExpr>(E))
Anders Carlsson0f294632009-05-27 04:18:27 +00001700 if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(TargetDecl))
Anders Carlssona1736c02009-12-24 21:13:40 +00001701 return EmitCXXOperatorMemberCallExpr(CE, MD, ReturnValue);
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001702
Eli Friedmanc4451db2009-12-08 02:09:46 +00001703 if (isa<CXXPseudoDestructorExpr>(E->getCallee()->IgnoreParens())) {
Douglas Gregora71d8192009-09-04 17:36:40 +00001704 // C++ [expr.pseudo]p1:
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001705 // The result shall only be used as the operand for the function call
Douglas Gregora71d8192009-09-04 17:36:40 +00001706 // operator (), and the result of such a call has type void. The only
1707 // effect is the evaluation of the postfix-expression before the dot or
1708 // arrow.
1709 EmitScalarExpr(E->getCallee());
1710 return RValue::get(0);
1711 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001712
Chris Lattner7f02f722007-08-24 05:35:26 +00001713 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
Anders Carlssond2490a92009-12-24 20:40:36 +00001714 return EmitCall(E->getCallee()->getType(), Callee, ReturnValue,
Anders Carlsson98647712009-05-27 01:22:39 +00001715 E->arg_begin(), E->arg_end(), TargetDecl);
Chris Lattnerc5e940f2007-08-31 04:44:06 +00001716}
1717
Daniel Dunbar80e62c22008-09-04 03:20:13 +00001718LValue CodeGenFunction::EmitBinaryOperatorLValue(const BinaryOperator *E) {
Chris Lattner7a574cc2009-05-12 21:28:12 +00001719 // Comma expressions just emit their LHS then their RHS as an l-value.
1720 if (E->getOpcode() == BinaryOperator::Comma) {
1721 EmitAnyExpr(E->getLHS());
Eli Friedman130c69e2009-12-07 20:18:11 +00001722 EnsureInsertPoint();
Chris Lattner7a574cc2009-05-12 21:28:12 +00001723 return EmitLValue(E->getRHS());
1724 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001725
Fariborz Jahanian52f08bc2009-10-26 21:58:25 +00001726 if (E->getOpcode() == BinaryOperator::PtrMemD ||
1727 E->getOpcode() == BinaryOperator::PtrMemI)
Fariborz Jahanian8bfd31f2009-10-22 22:57:31 +00001728 return EmitPointerToDataMemberBinaryExpr(E);
1729
Daniel Dunbar80e62c22008-09-04 03:20:13 +00001730 // Can only get l-value for binary operator expressions which are a
1731 // simple assignment of aggregate type.
1732 if (E->getOpcode() != BinaryOperator::Assign)
1733 return EmitUnsupportedLValue(E, "binary l-value expression");
1734
Anders Carlsson86aa0cd2009-10-19 18:28:22 +00001735 if (!hasAggregateLLVMType(E->getType())) {
1736 // Emit the LHS as an l-value.
1737 LValue LV = EmitLValue(E->getLHS());
1738
1739 llvm::Value *RHS = EmitScalarExpr(E->getRHS());
1740 EmitStoreOfScalar(RHS, LV.getAddress(), LV.isVolatileQualified(),
1741 E->getType());
1742 return LV;
1743 }
1744
Daniel Dunbar18aba0d2010-02-05 19:38:31 +00001745 return EmitAggExprToLValue(E);
Daniel Dunbar80e62c22008-09-04 03:20:13 +00001746}
1747
Christopher Lamb22c940e2007-12-29 05:02:41 +00001748LValue CodeGenFunction::EmitCallExprLValue(const CallExpr *E) {
Christopher Lamb22c940e2007-12-29 05:02:41 +00001749 RValue RV = EmitCallExpr(E);
Anders Carlsson48265682009-05-27 01:45:47 +00001750
Chris Lattnereb99b012009-10-28 17:39:19 +00001751 if (!RV.isScalar())
1752 return LValue::MakeAddr(RV.getAggregateAddr(),MakeQualifiers(E->getType()));
1753
1754 assert(E->getCallReturnType()->isReferenceType() &&
1755 "Can't have a scalar return unless the return type is a "
1756 "reference type!");
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001757
Chris Lattnereb99b012009-10-28 17:39:19 +00001758 return LValue::MakeAddr(RV.getScalarVal(), MakeQualifiers(E->getType()));
Christopher Lamb22c940e2007-12-29 05:02:41 +00001759}
1760
Daniel Dunbar5b5c9ef2009-02-11 20:59:32 +00001761LValue CodeGenFunction::EmitVAArgExprLValue(const VAArgExpr *E) {
1762 // FIXME: This shouldn't require another copy.
Daniel Dunbar18aba0d2010-02-05 19:38:31 +00001763 return EmitAggExprToLValue(E);
Daniel Dunbar5b5c9ef2009-02-11 20:59:32 +00001764}
1765
Anders Carlssonb58d0172009-05-30 23:23:33 +00001766LValue CodeGenFunction::EmitCXXConstructLValue(const CXXConstructExpr *E) {
1767 llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(E->getType()), "tmp");
1768 EmitCXXConstructExpr(Temp, E);
John McCall0953e762009-09-24 19:53:00 +00001769 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
Anders Carlssonb58d0172009-05-30 23:23:33 +00001770}
1771
Anders Carlssone61c9e82009-05-30 23:30:54 +00001772LValue
Mike Stumpc2e84ae2009-11-15 08:09:41 +00001773CodeGenFunction::EmitCXXTypeidLValue(const CXXTypeidExpr *E) {
1774 llvm::Value *Temp = EmitCXXTypeidExpr(E);
1775 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
1776}
1777
1778LValue
Anders Carlssone61c9e82009-05-30 23:30:54 +00001779CodeGenFunction::EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr *E) {
1780 LValue LV = EmitLValue(E->getSubExpr());
Anders Carlsson543ac0c2009-05-31 00:34:10 +00001781 PushCXXTemporary(E->getTemporary(), LV.getAddress());
Anders Carlssone61c9e82009-05-30 23:30:54 +00001782 return LV;
1783}
1784
Daniel Dunbar0a04d772008-08-23 10:51:21 +00001785LValue CodeGenFunction::EmitObjCMessageExprLValue(const ObjCMessageExpr *E) {
1786 // Can only get l-value for message expression returning aggregate type
1787 RValue RV = EmitObjCMessageExpr(E);
1788 // FIXME: can this be volatile?
John McCall0953e762009-09-24 19:53:00 +00001789 return LValue::MakeAddr(RV.getAggregateAddr(), MakeQualifiers(E->getType()));
Daniel Dunbar0a04d772008-08-23 10:51:21 +00001790}
1791
Daniel Dunbar2a031922009-04-22 05:08:15 +00001792llvm::Value *CodeGenFunction::EmitIvarOffset(const ObjCInterfaceDecl *Interface,
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +00001793 const ObjCIvarDecl *Ivar) {
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001794 return CGM.getObjCRuntime().EmitIvarOffset(*this, Interface, Ivar);
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +00001795}
1796
Fariborz Jahanian45012a72009-02-03 00:09:52 +00001797LValue CodeGenFunction::EmitLValueForIvar(QualType ObjectTy,
1798 llvm::Value *BaseValue,
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +00001799 const ObjCIvarDecl *Ivar,
1800 unsigned CVRQualifiers) {
Chris Lattner26f074b2009-04-17 17:44:48 +00001801 return CGM.getObjCRuntime().EmitObjCValueForIvar(*this, ObjectTy, BaseValue,
Daniel Dunbar525c9b72009-04-21 01:19:28 +00001802 Ivar, CVRQualifiers);
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +00001803}
1804
1805LValue CodeGenFunction::EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E) {
Anders Carlsson29b7e502008-08-25 01:53:23 +00001806 // FIXME: A lot of the code below could be shared with EmitMemberExpr.
1807 llvm::Value *BaseValue = 0;
1808 const Expr *BaseExpr = E->getBase();
John McCall0953e762009-09-24 19:53:00 +00001809 Qualifiers BaseQuals;
Fariborz Jahanian45012a72009-02-03 00:09:52 +00001810 QualType ObjectTy;
Anders Carlsson29b7e502008-08-25 01:53:23 +00001811 if (E->isArrow()) {
1812 BaseValue = EmitScalarExpr(BaseExpr);
Steve Naroff14108da2009-07-10 23:34:53 +00001813 ObjectTy = BaseExpr->getType()->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00001814 BaseQuals = ObjectTy.getQualifiers();
Anders Carlsson29b7e502008-08-25 01:53:23 +00001815 } else {
1816 LValue BaseLV = EmitLValue(BaseExpr);
1817 // FIXME: this isn't right for bitfields.
1818 BaseValue = BaseLV.getAddress();
Fariborz Jahanian45012a72009-02-03 00:09:52 +00001819 ObjectTy = BaseExpr->getType();
John McCall0953e762009-09-24 19:53:00 +00001820 BaseQuals = ObjectTy.getQualifiers();
Anders Carlsson29b7e502008-08-25 01:53:23 +00001821 }
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +00001822
Fariborz Jahaniandbf3cfd2009-09-16 23:11:23 +00001823 LValue LV =
John McCall0953e762009-09-24 19:53:00 +00001824 EmitLValueForIvar(ObjectTy, BaseValue, E->getDecl(),
1825 BaseQuals.getCVRQualifiers());
Fariborz Jahaniandbf3cfd2009-09-16 23:11:23 +00001826 setObjCGCLValueClass(getContext(), E, LV);
1827 return LV;
Chris Lattner391d77a2008-03-30 23:03:07 +00001828}
1829
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001830LValue
Daniel Dunbar85c59ed2008-08-29 08:11:39 +00001831CodeGenFunction::EmitObjCPropertyRefLValue(const ObjCPropertyRefExpr *E) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001832 // This is a special l-value that just issues sends when we load or store
1833 // through it.
Daniel Dunbar85c59ed2008-08-29 08:11:39 +00001834 return LValue::MakePropertyRef(E, E->getType().getCVRQualifiers());
1835}
1836
Chris Lattnereb99b012009-10-28 17:39:19 +00001837LValue CodeGenFunction::EmitObjCKVCRefLValue(
Fariborz Jahanian09105f52009-08-20 17:02:02 +00001838 const ObjCImplicitSetterGetterRefExpr *E) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001839 // This is a special l-value that just issues sends when we load or store
1840 // through it.
Fariborz Jahanian43f44702008-11-22 22:30:21 +00001841 return LValue::MakeKVCRef(E, E->getType().getCVRQualifiers());
1842}
1843
Chris Lattnereb99b012009-10-28 17:39:19 +00001844LValue CodeGenFunction::EmitObjCSuperExprLValue(const ObjCSuperExpr *E) {
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00001845 return EmitUnsupportedLValue(E, "use of super");
1846}
1847
Chris Lattner65459942009-04-25 19:35:26 +00001848LValue CodeGenFunction::EmitStmtExprLValue(const StmtExpr *E) {
Chris Lattner65459942009-04-25 19:35:26 +00001849 // Can only get l-value for message expression returning aggregate type
1850 RValue RV = EmitAnyExprToTemp(E);
1851 // FIXME: can this be volatile?
John McCall0953e762009-09-24 19:53:00 +00001852 return LValue::MakeAddr(RV.getAggregateAddr(), MakeQualifiers(E->getType()));
Chris Lattner65459942009-04-25 19:35:26 +00001853}
1854
Charles Davis16c4f3c2010-02-05 18:13:10 +00001855static unsigned ClangCallConvToLLVMCallConv(CallingConv CC) {
1856 switch (CC) {
1857 default: return llvm::CallingConv::C;
1858 case CC_X86StdCall: return llvm::CallingConv::X86_StdCall;
1859 case CC_X86FastCall: return llvm::CallingConv::X86_FastCall;
1860 }
1861}
1862
Anders Carlsson31777a22009-12-24 19:08:58 +00001863RValue CodeGenFunction::EmitCall(QualType CalleeType, llvm::Value *Callee,
Anders Carlssond2490a92009-12-24 20:40:36 +00001864 ReturnValueSlot ReturnValue,
Anders Carlsson98647712009-05-27 01:22:39 +00001865 CallExpr::const_arg_iterator ArgBeg,
1866 CallExpr::const_arg_iterator ArgEnd,
1867 const Decl *TargetDecl) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001868 // Get the actual function type. The callee type will always be a pointer to
1869 // function type or a block pointer type.
1870 assert(CalleeType->isFunctionPointerType() &&
Anders Carlsson8ac67a72009-04-07 18:53:02 +00001871 "Call must have function pointer type!");
1872
John McCall00a1ad92009-10-23 08:22:42 +00001873 CalleeType = getContext().getCanonicalType(CalleeType);
1874
1875 QualType FnType = cast<PointerType>(CalleeType)->getPointeeType();
1876 QualType ResultType = cast<FunctionType>(FnType)->getResultType();
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001877
1878 CallArgList Args;
John McCall00a1ad92009-10-23 08:22:42 +00001879 EmitCallArgs(Args, dyn_cast<FunctionProtoType>(FnType), ArgBeg, ArgEnd);
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001880
Charles Davis16c4f3c2010-02-05 18:13:10 +00001881 unsigned CallingConvention =
1882 ClangCallConvToLLVMCallConv(FnType->getAs<FunctionType>()->getCallConv());
Daniel Dunbar8a9f3fd2009-09-11 22:25:00 +00001883 return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args,
1884 CallingConvention),
Anders Carlssond2490a92009-12-24 20:40:36 +00001885 Callee, ReturnValue, Args, TargetDecl);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001886}
Fariborz Jahanian8bfd31f2009-10-22 22:57:31 +00001887
Chris Lattnereb99b012009-10-28 17:39:19 +00001888LValue CodeGenFunction::
1889EmitPointerToDataMemberBinaryExpr(const BinaryOperator *E) {
Eli Friedman1c5c1a02009-11-18 05:01:17 +00001890 llvm::Value *BaseV;
Fariborz Jahanian52f08bc2009-10-26 21:58:25 +00001891 if (E->getOpcode() == BinaryOperator::PtrMemI)
Eli Friedman1c5c1a02009-11-18 05:01:17 +00001892 BaseV = EmitScalarExpr(E->getLHS());
1893 else
1894 BaseV = EmitLValue(E->getLHS()).getAddress();
Fariborz Jahanian8bfd31f2009-10-22 22:57:31 +00001895 const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(getLLVMContext());
1896 BaseV = Builder.CreateBitCast(BaseV, i8Ty);
Eli Friedman1c5c1a02009-11-18 05:01:17 +00001897 llvm::Value *OffsetV = EmitScalarExpr(E->getRHS());
Fariborz Jahanian8bfd31f2009-10-22 22:57:31 +00001898 llvm::Value *AddV = Builder.CreateInBoundsGEP(BaseV, OffsetV, "add.ptr");
Chris Lattnereb99b012009-10-28 17:39:19 +00001899
Fariborz Jahanian8bfd31f2009-10-22 22:57:31 +00001900 QualType Ty = E->getRHS()->getType();
Chris Lattnereb99b012009-10-28 17:39:19 +00001901 Ty = Ty->getAs<MemberPointerType>()->getPointeeType();
1902
1903 const llvm::Type *PType = ConvertType(getContext().getPointerType(Ty));
Fariborz Jahanian8bfd31f2009-10-22 22:57:31 +00001904 AddV = Builder.CreateBitCast(AddV, PType);
Chris Lattnereb99b012009-10-28 17:39:19 +00001905 return LValue::MakeAddr(AddV, MakeQualifiers(Ty));
Fariborz Jahanian8bfd31f2009-10-22 22:57:31 +00001906}
1907