blob: fe43627006168568e2ba4b527ca79ea82ab4c81c [file] [log] [blame]
Chris Lattnere47e4402007-06-01 18:02:12 +00001//===--- CGExpr.cpp - Emit LLVM Code from Expressions ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnere47e4402007-06-01 18:02:12 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit Expr nodes as LLVM code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenFunction.h"
Chris Lattnerb6984c42007-06-20 04:44:43 +000015#include "CodeGenModule.h"
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000016#include "CGCall.h"
Daniel Dunbar89da6ad2008-08-13 00:59:25 +000017#include "CGObjCRuntime.h"
Daniel Dunbarad319a72008-08-11 05:00:27 +000018#include "clang/AST/ASTContext.h"
Daniel Dunbar6e8aa532008-08-11 05:35:13 +000019#include "clang/AST/DeclObjC.h"
Eli Friedmanf2442dc2008-05-17 20:03:47 +000020#include "llvm/Target/TargetData.h"
Chris Lattnere47e4402007-06-01 18:02:12 +000021using namespace clang;
22using namespace CodeGen;
23
Chris Lattnerd7f58862007-06-02 05:24:33 +000024//===--------------------------------------------------------------------===//
Chris Lattnerf0106d22007-06-02 19:33:17 +000025// Miscellaneous Helper Methods
26//===--------------------------------------------------------------------===//
27
Chris Lattnere9a64532007-06-22 21:44:33 +000028/// CreateTempAlloca - This creates a alloca and inserts it into the entry
29/// block.
30llvm::AllocaInst *CodeGenFunction::CreateTempAlloca(const llvm::Type *Ty,
31 const char *Name) {
Chris Lattner47640222009-03-22 00:24:14 +000032 if (!Builder.isNamePreserving())
33 Name = "";
Owen Anderson9f98d372009-07-16 00:14:12 +000034 return new llvm::AllocaInst(Ty, 0, Name, AllocaInsertPt);
Chris Lattnere9a64532007-06-22 21:44:33 +000035}
Chris Lattner8394d792007-06-05 20:53:16 +000036
37/// EvaluateExprAsBool - Perform the usual unary conversions on the specified
38/// expression and compare the result against zero, returning an Int1Ty value.
Chris Lattner23b7eb62007-06-15 23:05:46 +000039llvm::Value *CodeGenFunction::EvaluateExprAsBool(const Expr *E) {
Chris Lattner268fcce2007-08-26 16:46:58 +000040 QualType BoolTy = getContext().BoolTy;
Chris Lattnerf3bc75a2008-04-04 16:54:41 +000041 if (!E->getType()->isAnyComplexType())
Chris Lattner268fcce2007-08-26 16:46:58 +000042 return EmitScalarConversion(EmitScalarExpr(E), E->getType(), BoolTy);
Chris Lattner8394d792007-06-05 20:53:16 +000043
Chris Lattner268fcce2007-08-26 16:46:58 +000044 return EmitComplexToScalarConversion(EmitComplexExpr(E), E->getType(),BoolTy);
Chris Lattnerf0106d22007-06-02 19:33:17 +000045}
46
Chris Lattner4647a212007-08-31 22:49:20 +000047/// EmitAnyExpr - Emit code to compute the specified expression which can have
48/// any type. The result is returned as an RValue struct. If this is an
Mike Stump4a3999f2009-09-09 13:00:44 +000049/// aggregate expression, the aggloc/agglocvolatile arguments indicate where the
50/// result should be returned.
51RValue CodeGenFunction::EmitAnyExpr(const Expr *E, llvm::Value *AggLoc,
Anders Carlsson5b106a72009-08-16 07:36:22 +000052 bool IsAggLocVolatile, bool IgnoreResult,
53 bool IsInitializer) {
Chris Lattner4647a212007-08-31 22:49:20 +000054 if (!hasAggregateLLVMType(E->getType()))
Mike Stumpdf0fe272009-05-29 15:46:01 +000055 return RValue::get(EmitScalarExpr(E, IgnoreResult));
Chris Lattnerf3bc75a2008-04-04 16:54:41 +000056 else if (E->getType()->isAnyComplexType())
Mike Stumpdf0fe272009-05-29 15:46:01 +000057 return RValue::getComplex(EmitComplexExpr(E, false, false,
58 IgnoreResult, IgnoreResult));
Mike Stump4a3999f2009-09-09 13:00:44 +000059
Anders Carlsson5b106a72009-08-16 07:36:22 +000060 EmitAggExpr(E, AggLoc, IsAggLocVolatile, IgnoreResult, IsInitializer);
61 return RValue::getAggregate(AggLoc, IsAggLocVolatile);
Chris Lattner4647a212007-08-31 22:49:20 +000062}
63
Mike Stump4a3999f2009-09-09 13:00:44 +000064/// EmitAnyExprToTemp - Similary to EmitAnyExpr(), however, the result will
65/// always be accessible even if no aggregate location is provided.
66RValue CodeGenFunction::EmitAnyExprToTemp(const Expr *E,
Anders Carlsson5b106a72009-08-16 07:36:22 +000067 bool IsAggLocVolatile,
68 bool IsInitializer) {
69 llvm::Value *AggLoc = 0;
Mike Stump4a3999f2009-09-09 13:00:44 +000070
71 if (hasAggregateLLVMType(E->getType()) &&
Daniel Dunbar41cf9de2008-09-09 01:06:48 +000072 !E->getType()->isAnyComplexType())
73 AggLoc = CreateTempAlloca(ConvertType(E->getType()), "agg.tmp");
Mike Stump4a3999f2009-09-09 13:00:44 +000074 return EmitAnyExpr(E, AggLoc, IsAggLocVolatile, /*IgnoreResult=*/false,
Anders Carlsson5b106a72009-08-16 07:36:22 +000075 IsInitializer);
Daniel Dunbar41cf9de2008-09-09 01:06:48 +000076}
77
Anders Carlsson6f5a0152009-05-20 00:24:07 +000078RValue CodeGenFunction::EmitReferenceBindingToExpr(const Expr* E,
Anders Carlsson5b106a72009-08-16 07:36:22 +000079 QualType DestType,
80 bool IsInitializer) {
Eli Friedmanc21cb442009-05-20 02:31:19 +000081 RValue Val;
82 if (E->isLvalue(getContext()) == Expr::LV_Valid) {
Anders Carlsson7d4c0832009-05-20 00:36:58 +000083 // Emit the expr as an lvalue.
84 LValue LV = EmitLValue(E);
Eli Friedmanc21cb442009-05-20 02:31:19 +000085 if (LV.isSimple())
86 return RValue::get(LV.getAddress());
87 Val = EmitLoadOfLValue(LV, E->getType());
88 } else {
Anders Carlssonb80760b2009-08-16 17:50:25 +000089 // FIXME: Initializers don't work with casts yet. For example
90 // const A& a = B();
91 // if B inherits from A.
Anders Carlsson5b106a72009-08-16 07:36:22 +000092 Val = EmitAnyExprToTemp(E, /*IsAggLocVolatile=*/false,
93 IsInitializer);
Mike Stump4a3999f2009-09-09 13:00:44 +000094
Anders Carlsson3b848942009-08-16 17:54:29 +000095 if (IsInitializer) {
96 // We might have to destroy the temporary variable.
97 if (const RecordType *RT = E->getType()->getAs<RecordType>()) {
98 if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
99 if (!ClassDecl->hasTrivialDestructor()) {
Mike Stump4a3999f2009-09-09 13:00:44 +0000100 const CXXDestructorDecl *Dtor =
Anders Carlsson3b848942009-08-16 17:54:29 +0000101 ClassDecl->getDestructor(getContext());
Mike Stump4a3999f2009-09-09 13:00:44 +0000102
Anders Carlsson3b848942009-08-16 17:54:29 +0000103 CleanupScope scope(*this);
104 EmitCXXDestructorCall(Dtor, Dtor_Complete, Val.getAggregateAddr());
105 }
Anders Carlssonb80760b2009-08-16 17:50:25 +0000106 }
107 }
108 }
Anders Carlsson7d4c0832009-05-20 00:36:58 +0000109 }
Eli Friedmanc21cb442009-05-20 02:31:19 +0000110
111 if (Val.isAggregate()) {
112 Val = RValue::get(Val.getAggregateAddr());
113 } else {
Anders Carlsson02bb7f02009-05-20 01:35:03 +0000114 // Create a temporary variable that we can bind the reference to.
Mike Stump4a3999f2009-09-09 13:00:44 +0000115 llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(E->getType()),
Anders Carlsson145eae52009-05-20 01:03:17 +0000116 "reftmp");
Eli Friedmanc21cb442009-05-20 02:31:19 +0000117 if (Val.isScalar())
118 EmitStoreOfScalar(Val.getScalarVal(), Temp, false, E->getType());
119 else
120 StoreComplexToAddr(Val.getComplexVal(), Temp, false);
121 Val = RValue::get(Temp);
Anders Carlsson145eae52009-05-20 01:03:17 +0000122 }
Eli Friedmanc21cb442009-05-20 02:31:19 +0000123
124 return Val;
Anders Carlsson6f5a0152009-05-20 00:24:07 +0000125}
126
127
Mike Stump4a3999f2009-09-09 13:00:44 +0000128/// getAccessedFieldNo - Given an encoded value and a result number, return the
129/// input field number being accessed.
130unsigned CodeGenFunction::getAccessedFieldNo(unsigned Idx,
Dan Gohman75d69da2008-05-22 00:50:06 +0000131 const llvm::Constant *Elts) {
132 if (isa<llvm::ConstantAggregateZero>(Elts))
133 return 0;
Mike Stump4a3999f2009-09-09 13:00:44 +0000134
Dan Gohman75d69da2008-05-22 00:50:06 +0000135 return cast<llvm::ConstantInt>(Elts->getOperand(Idx))->getZExtValue();
136}
137
Chris Lattner4647a212007-08-31 22:49:20 +0000138
Chris Lattnera45c5af2007-06-02 19:47:04 +0000139//===----------------------------------------------------------------------===//
Chris Lattnerd7f58862007-06-02 05:24:33 +0000140// LValue Expression Emission
Chris Lattnera45c5af2007-06-02 19:47:04 +0000141//===----------------------------------------------------------------------===//
Chris Lattnerd7f58862007-06-02 05:24:33 +0000142
Daniel Dunbarc79407f2009-02-05 07:09:07 +0000143RValue CodeGenFunction::GetUndefRValue(QualType Ty) {
144 if (Ty->isVoidType()) {
145 return RValue::get(0);
146 } else if (const ComplexType *CTy = Ty->getAsComplexType()) {
Daniel Dunbar8429dbc2009-01-09 20:09:28 +0000147 const llvm::Type *EltTy = ConvertType(CTy->getElementType());
Owen Anderson7ec07a52009-07-30 23:11:26 +0000148 llvm::Value *U = llvm::UndefValue::get(EltTy);
Daniel Dunbar8429dbc2009-01-09 20:09:28 +0000149 return RValue::getComplex(std::make_pair(U, U));
Daniel Dunbarc79407f2009-02-05 07:09:07 +0000150 } else if (hasAggregateLLVMType(Ty)) {
Owen Anderson9793f0e2009-07-29 22:16:19 +0000151 const llvm::Type *LTy = llvm::PointerType::getUnqual(ConvertType(Ty));
Owen Anderson7ec07a52009-07-30 23:11:26 +0000152 return RValue::getAggregate(llvm::UndefValue::get(LTy));
Daniel Dunbar8429dbc2009-01-09 20:09:28 +0000153 } else {
Owen Anderson7ec07a52009-07-30 23:11:26 +0000154 return RValue::get(llvm::UndefValue::get(ConvertType(Ty)));
Daniel Dunbar8429dbc2009-01-09 20:09:28 +0000155 }
Daniel Dunbarbb197e42009-01-09 16:50:52 +0000156}
157
Daniel Dunbarc79407f2009-02-05 07:09:07 +0000158RValue CodeGenFunction::EmitUnsupportedRValue(const Expr *E,
159 const char *Name) {
160 ErrorUnsupported(E, Name);
161 return GetUndefRValue(E->getType());
162}
163
Daniel Dunbarf2e69882008-08-25 20:45:57 +0000164LValue CodeGenFunction::EmitUnsupportedLValue(const Expr *E,
165 const char *Name) {
166 ErrorUnsupported(E, Name);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000167 llvm::Type *Ty = llvm::PointerType::getUnqual(ConvertType(E->getType()));
Owen Anderson7ec07a52009-07-30 23:11:26 +0000168 return LValue::MakeAddr(llvm::UndefValue::get(Ty),
Fariborz Jahanian0f466c72009-02-19 23:36:06 +0000169 E->getType().getCVRQualifiers(),
Mon P Wangacedf772009-07-22 03:08:17 +0000170 getContext().getObjCGCAttrKind(E->getType()),
171 E->getType().getAddressSpace());
Daniel Dunbarf2e69882008-08-25 20:45:57 +0000172}
173
Chris Lattner8394d792007-06-05 20:53:16 +0000174/// EmitLValue - Emit code to compute a designator that specifies the location
175/// of the expression.
176///
Mike Stump4a3999f2009-09-09 13:00:44 +0000177/// This can return one of two things: a simple address or a bitfield reference.
178/// In either case, the LLVM Value* in the LValue structure is guaranteed to be
179/// an LLVM pointer type.
Chris Lattner8394d792007-06-05 20:53:16 +0000180///
Mike Stump4a3999f2009-09-09 13:00:44 +0000181/// If this returns a bitfield reference, nothing about the pointee type of the
182/// LLVM value is known: For example, it may not be a pointer to an integer.
Chris Lattner8394d792007-06-05 20:53:16 +0000183///
Mike Stump4a3999f2009-09-09 13:00:44 +0000184/// If this returns a normal address, and if the lvalue's C type is fixed size,
185/// this method guarantees that the returned pointer type will point to an LLVM
186/// type of the same size of the lvalue's type. If the lvalue has a variable
187/// length type, this is not possible.
Chris Lattner8394d792007-06-05 20:53:16 +0000188///
Chris Lattnerd7f58862007-06-02 05:24:33 +0000189LValue CodeGenFunction::EmitLValue(const Expr *E) {
190 switch (E->getStmtClass()) {
Daniel Dunbarf2e69882008-08-25 20:45:57 +0000191 default: return EmitUnsupportedLValue(E, "l-value expression");
Chris Lattnerd7f58862007-06-02 05:24:33 +0000192
Mike Stump4a3999f2009-09-09 13:00:44 +0000193 case Expr::BinaryOperatorClass:
Daniel Dunbar8cde00a2008-09-04 03:20:13 +0000194 return EmitBinaryOperatorLValue(cast<BinaryOperator>(E));
Mike Stump4a3999f2009-09-09 13:00:44 +0000195 case Expr::CallExprClass:
Anders Carlssonc82555f2009-09-01 21:18:52 +0000196 case Expr::CXXMemberCallExprClass:
Douglas Gregor993603d2008-11-14 16:09:21 +0000197 case Expr::CXXOperatorCallExprClass:
198 return EmitCallExprLValue(cast<CallExpr>(E));
Daniel Dunbar8d9dc4a2009-02-11 20:59:32 +0000199 case Expr::VAArgExprClass:
200 return EmitVAArgExprLValue(cast<VAArgExpr>(E));
Mike Stump4a3999f2009-09-09 13:00:44 +0000201 case Expr::DeclRefExprClass:
Douglas Gregorc7acfdf2009-01-06 05:10:23 +0000202 case Expr::QualifiedDeclRefExprClass:
203 return EmitDeclRefLValue(cast<DeclRefExpr>(E));
Chris Lattner946aa312007-06-05 03:59:43 +0000204 case Expr::ParenExprClass:return EmitLValue(cast<ParenExpr>(E)->getSubExpr());
Chris Lattner6307f192008-08-10 01:53:14 +0000205 case Expr::PredefinedExprClass:
206 return EmitPredefinedLValue(cast<PredefinedExpr>(E));
Chris Lattner4347e3692007-06-06 04:54:52 +0000207 case Expr::StringLiteralClass:
208 return EmitStringLiteralLValue(cast<StringLiteral>(E));
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +0000209 case Expr::ObjCEncodeExprClass:
210 return EmitObjCEncodeExprLValue(cast<ObjCEncodeExpr>(E));
Chris Lattner4bd55962008-03-30 23:03:07 +0000211
Mike Stump4a3999f2009-09-09 13:00:44 +0000212 case Expr::BlockDeclRefExprClass:
Mike Stump1db7d042009-02-28 09:07:16 +0000213 return EmitBlockDeclRefLValue(cast<BlockDeclRefExpr>(E));
214
Argyrios Kyrtzidis07052352008-09-10 02:36:38 +0000215 case Expr::CXXConditionDeclExprClass:
216 return EmitCXXConditionDeclLValue(cast<CXXConditionDeclExpr>(E));
Anders Carlsson3be22e22009-05-30 23:23:33 +0000217 case Expr::CXXTemporaryObjectExprClass:
218 case Expr::CXXConstructExprClass:
Anders Carlssonfd2af0c2009-05-30 23:30:54 +0000219 return EmitCXXConstructLValue(cast<CXXConstructExpr>(E));
220 case Expr::CXXBindTemporaryExprClass:
221 return EmitCXXBindTemporaryLValue(cast<CXXBindTemporaryExpr>(E));
222
Daniel Dunbarc8317a42008-08-23 10:51:21 +0000223 case Expr::ObjCMessageExprClass:
224 return EmitObjCMessageExprLValue(cast<ObjCMessageExpr>(E));
Mike Stump4a3999f2009-09-09 13:00:44 +0000225 case Expr::ObjCIvarRefExprClass:
Chris Lattner4bd55962008-03-30 23:03:07 +0000226 return EmitObjCIvarRefLValue(cast<ObjCIvarRefExpr>(E));
Daniel Dunbarf2e69882008-08-25 20:45:57 +0000227 case Expr::ObjCPropertyRefExprClass:
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +0000228 return EmitObjCPropertyRefLValue(cast<ObjCPropertyRefExpr>(E));
Fariborz Jahanian9a846652009-08-20 17:02:02 +0000229 case Expr::ObjCImplicitSetterGetterRefExprClass:
230 return EmitObjCKVCRefLValue(cast<ObjCImplicitSetterGetterRefExpr>(E));
Douglas Gregor8ea1f532008-11-04 14:56:14 +0000231 case Expr::ObjCSuperExprClass:
Chris Lattnera4185c52009-04-25 19:35:26 +0000232 return EmitObjCSuperExprLValue(cast<ObjCSuperExpr>(E));
Douglas Gregor8ea1f532008-11-04 14:56:14 +0000233
Chris Lattnera4185c52009-04-25 19:35:26 +0000234 case Expr::StmtExprClass:
235 return EmitStmtExprLValue(cast<StmtExpr>(E));
Mike Stump4a3999f2009-09-09 13:00:44 +0000236 case Expr::UnaryOperatorClass:
Chris Lattner8394d792007-06-05 20:53:16 +0000237 return EmitUnaryOpLValue(cast<UnaryOperator>(E));
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000238 case Expr::ArraySubscriptExprClass:
239 return EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E));
Nate Begemance4d7fc2008-04-18 23:10:10 +0000240 case Expr::ExtVectorElementExprClass:
241 return EmitExtVectorElementExpr(cast<ExtVectorElementExpr>(E));
Mike Stump4a3999f2009-09-09 13:00:44 +0000242 case Expr::MemberExprClass:
Douglas Gregorc1905232009-08-26 22:36:53 +0000243 return EmitMemberExpr(cast<MemberExpr>(E));
Eli Friedman9fd8b682008-05-13 23:18:27 +0000244 case Expr::CompoundLiteralExprClass:
245 return EmitCompoundLiteralLValue(cast<CompoundLiteralExpr>(E));
Daniel Dunbarbf1fe8c2009-03-24 02:38:23 +0000246 case Expr::ConditionalOperatorClass:
247 return EmitConditionalOperator(cast<ConditionalOperator>(E));
Chris Lattner053441f2008-12-12 05:35:08 +0000248 case Expr::ChooseExprClass:
Eli Friedmane0a5b8b2009-03-04 05:52:32 +0000249 return EmitLValue(cast<ChooseExpr>(E)->getChosenSubExpr(getContext()));
Chris Lattner63d06ab2009-03-18 04:02:57 +0000250 case Expr::ImplicitCastExprClass:
251 case Expr::CStyleCastExprClass:
252 case Expr::CXXFunctionalCastExprClass:
253 case Expr::CXXStaticCastExprClass:
254 case Expr::CXXDynamicCastExprClass:
255 case Expr::CXXReinterpretCastExprClass:
256 case Expr::CXXConstCastExprClass:
Chris Lattner28bcf1a2009-03-18 18:28:57 +0000257 return EmitCastLValue(cast<CastExpr>(E));
Chris Lattnerd7f58862007-06-02 05:24:33 +0000258 }
259}
260
Daniel Dunbar1d425462009-02-10 00:57:50 +0000261llvm::Value *CodeGenFunction::EmitLoadOfScalar(llvm::Value *Addr, bool Volatile,
262 QualType Ty) {
263 llvm::Value *V = Builder.CreateLoad(Addr, Volatile, "tmp");
264
Anders Carlsson29a1be32009-05-19 19:36:19 +0000265 // Bool can have different representation in memory than in registers.
Daniel Dunbar1d425462009-02-10 00:57:50 +0000266 if (Ty->isBooleanType())
Owen Anderson41a75022009-08-13 21:57:51 +0000267 if (V->getType() != llvm::Type::getInt1Ty(VMContext))
268 V = Builder.CreateTrunc(V, llvm::Type::getInt1Ty(VMContext), "tobool");
Mike Stump4a3999f2009-09-09 13:00:44 +0000269
Daniel Dunbar1d425462009-02-10 00:57:50 +0000270 return V;
271}
272
273void CodeGenFunction::EmitStoreOfScalar(llvm::Value *Value, llvm::Value *Addr,
Anders Carlsson83709642009-05-19 18:50:41 +0000274 bool Volatile, QualType Ty) {
Mike Stump4a3999f2009-09-09 13:00:44 +0000275
Anders Carlsson29a1be32009-05-19 19:36:19 +0000276 if (Ty->isBooleanType()) {
277 // Bool can have different representation in memory than in registers.
278 const llvm::Type *SrcTy = Value->getType();
279 const llvm::PointerType *DstPtr = cast<llvm::PointerType>(Addr->getType());
280 if (DstPtr->getElementType() != SrcTy) {
Mike Stump4a3999f2009-09-09 13:00:44 +0000281 const llvm::Type *MemTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000282 llvm::PointerType::get(SrcTy, DstPtr->getAddressSpace());
Anders Carlsson29a1be32009-05-19 19:36:19 +0000283 Addr = Builder.CreateBitCast(Addr, MemTy, "storetmp");
284 }
Daniel Dunbar1d425462009-02-10 00:57:50 +0000285 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000286 Builder.CreateStore(Value, Addr, Volatile);
Daniel Dunbar1d425462009-02-10 00:57:50 +0000287}
288
Mike Stump4a3999f2009-09-09 13:00:44 +0000289/// EmitLoadOfLValue - Given an expression that represents a value lvalue, this
290/// method emits the address of the lvalue, then loads the result as an rvalue,
291/// returning the rvalue.
Chris Lattner9369a562007-06-29 16:31:29 +0000292RValue CodeGenFunction::EmitLoadOfLValue(LValue LV, QualType ExprType) {
Fariborz Jahanian50a12702008-11-19 17:34:06 +0000293 if (LV.isObjCWeak()) {
Mike Stump4a3999f2009-09-09 13:00:44 +0000294 // load of a __weak object.
Fariborz Jahanianf5125d12008-11-18 21:45:40 +0000295 llvm::Value *AddrWeakObj = LV.getAddress();
Mike Stump4a3999f2009-09-09 13:00:44 +0000296 llvm::Value *read_weak = CGM.getObjCRuntime().EmitObjCWeakRead(*this,
Fariborz Jahanianf5125d12008-11-18 21:45:40 +0000297 AddrWeakObj);
298 return RValue::get(read_weak);
299 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000300
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000301 if (LV.isSimple()) {
302 llvm::Value *Ptr = LV.getAddress();
303 const llvm::Type *EltTy =
304 cast<llvm::PointerType>(Ptr->getType())->getElementType();
Mike Stump4a3999f2009-09-09 13:00:44 +0000305
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000306 // Simple scalar l-value.
Daniel Dunbar1d425462009-02-10 00:57:50 +0000307 if (EltTy->isSingleValueType())
Mike Stump4a3999f2009-09-09 13:00:44 +0000308 return RValue::get(EmitLoadOfScalar(Ptr, LV.isVolatileQualified(),
Daniel Dunbar1d425462009-02-10 00:57:50 +0000309 ExprType));
Mike Stump4a3999f2009-09-09 13:00:44 +0000310
Chris Lattner6278e6a2007-08-11 00:04:45 +0000311 assert(ExprType->isFunctionType() && "Unknown scalar value");
312 return RValue::get(Ptr);
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000313 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000314
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000315 if (LV.isVectorElt()) {
Eli Friedman327944b2008-06-13 23:01:12 +0000316 llvm::Value *Vec = Builder.CreateLoad(LV.getVectorAddr(),
317 LV.isVolatileQualified(), "tmp");
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000318 return RValue::get(Builder.CreateExtractElement(Vec, LV.getVectorIdx(),
319 "vecext"));
320 }
Chris Lattner73ab9b32007-08-03 00:16:29 +0000321
322 // If this is a reference to a subset of the elements of a vector, either
323 // shuffle the input or extract/insert them as appropriate.
Nate Begemance4d7fc2008-04-18 23:10:10 +0000324 if (LV.isExtVectorElt())
325 return EmitLoadOfExtVectorElementLValue(LV, ExprType);
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000326
327 if (LV.isBitfield())
328 return EmitLoadOfBitfieldLValue(LV, ExprType);
329
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +0000330 if (LV.isPropertyRef())
331 return EmitLoadOfPropertyRefLValue(LV, ExprType);
332
Chris Lattner6c7ce102009-02-16 21:11:58 +0000333 assert(LV.isKVCRef() && "Unknown LValue type!");
334 return EmitLoadOfKVCRefLValue(LV, ExprType);
Chris Lattner8394d792007-06-05 20:53:16 +0000335}
336
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000337RValue CodeGenFunction::EmitLoadOfBitfieldLValue(LValue LV,
338 QualType ExprType) {
Daniel Dunbaread7c912008-08-06 05:08:45 +0000339 unsigned StartBit = LV.getBitfieldStartBit();
340 unsigned BitfieldSize = LV.getBitfieldSize();
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000341 llvm::Value *Ptr = LV.getBitfieldAddr();
Daniel Dunbaread7c912008-08-06 05:08:45 +0000342
Mike Stump4a3999f2009-09-09 13:00:44 +0000343 const llvm::Type *EltTy =
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000344 cast<llvm::PointerType>(Ptr->getType())->getElementType();
Daniel Dunbaread7c912008-08-06 05:08:45 +0000345 unsigned EltTySize = CGM.getTargetData().getTypeSizeInBits(EltTy);
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000346
Mike Stump4a3999f2009-09-09 13:00:44 +0000347 // In some cases the bitfield may straddle two memory locations. Currently we
348 // load the entire bitfield, then do the magic to sign-extend it if
349 // necessary. This results in somewhat more code than necessary for the common
350 // case (one load), since two shifts accomplish both the masking and sign
351 // extension.
Daniel Dunbaread7c912008-08-06 05:08:45 +0000352 unsigned LowBits = std::min(BitfieldSize, EltTySize - StartBit);
353 llvm::Value *Val = Builder.CreateLoad(Ptr, LV.isVolatileQualified(), "tmp");
Mike Stump4a3999f2009-09-09 13:00:44 +0000354
Daniel Dunbaread7c912008-08-06 05:08:45 +0000355 // Shift to proper location.
Daniel Dunbarf7fb7502008-11-13 02:20:34 +0000356 if (StartBit)
Mike Stump4a3999f2009-09-09 13:00:44 +0000357 Val = Builder.CreateLShr(Val, llvm::ConstantInt::get(EltTy, StartBit),
Daniel Dunbarf7fb7502008-11-13 02:20:34 +0000358 "bf.lo");
Mike Stump4a3999f2009-09-09 13:00:44 +0000359
Daniel Dunbaread7c912008-08-06 05:08:45 +0000360 // Mask off unused bits.
Mike Stump4a3999f2009-09-09 13:00:44 +0000361 llvm::Constant *LowMask = llvm::ConstantInt::get(VMContext,
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000362 llvm::APInt::getLowBitsSet(EltTySize, LowBits));
Daniel Dunbaread7c912008-08-06 05:08:45 +0000363 Val = Builder.CreateAnd(Val, LowMask, "bf.lo.cleared");
Mike Stump4a3999f2009-09-09 13:00:44 +0000364
Daniel Dunbaread7c912008-08-06 05:08:45 +0000365 // Fetch the high bits if necessary.
366 if (LowBits < BitfieldSize) {
367 unsigned HighBits = BitfieldSize - LowBits;
Owen Anderson41a75022009-08-13 21:57:51 +0000368 llvm::Value *HighPtr = Builder.CreateGEP(Ptr, llvm::ConstantInt::get(
Mike Stump4a3999f2009-09-09 13:00:44 +0000369 llvm::Type::getInt32Ty(VMContext), 1), "bf.ptr.hi");
370 llvm::Value *HighVal = Builder.CreateLoad(HighPtr,
Daniel Dunbaread7c912008-08-06 05:08:45 +0000371 LV.isVolatileQualified(),
372 "tmp");
Mike Stump4a3999f2009-09-09 13:00:44 +0000373
Daniel Dunbaread7c912008-08-06 05:08:45 +0000374 // Mask off unused bits.
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000375 llvm::Constant *HighMask = llvm::ConstantInt::get(VMContext,
376 llvm::APInt::getLowBitsSet(EltTySize, HighBits));
Daniel Dunbaread7c912008-08-06 05:08:45 +0000377 HighVal = Builder.CreateAnd(HighVal, HighMask, "bf.lo.cleared");
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000378
Daniel Dunbaread7c912008-08-06 05:08:45 +0000379 // Shift to proper location and or in to bitfield value.
Mike Stump4a3999f2009-09-09 13:00:44 +0000380 HighVal = Builder.CreateShl(HighVal,
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000381 llvm::ConstantInt::get(EltTy, LowBits));
Daniel Dunbaread7c912008-08-06 05:08:45 +0000382 Val = Builder.CreateOr(Val, HighVal, "bf.val");
383 }
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000384
Daniel Dunbaread7c912008-08-06 05:08:45 +0000385 // Sign extend if necessary.
386 if (LV.isBitfieldSigned()) {
Mike Stump4a3999f2009-09-09 13:00:44 +0000387 llvm::Value *ExtraBits = llvm::ConstantInt::get(EltTy,
Daniel Dunbaread7c912008-08-06 05:08:45 +0000388 EltTySize - BitfieldSize);
Mike Stump4a3999f2009-09-09 13:00:44 +0000389 Val = Builder.CreateAShr(Builder.CreateShl(Val, ExtraBits),
Daniel Dunbaread7c912008-08-06 05:08:45 +0000390 ExtraBits, "bf.val.sext");
391 }
Eli Friedmanf2442dc2008-05-17 20:03:47 +0000392
Mike Stump4a3999f2009-09-09 13:00:44 +0000393 // The bitfield type and the normal type differ when the storage sizes differ
394 // (currently just _Bool).
Daniel Dunbaread7c912008-08-06 05:08:45 +0000395 Val = Builder.CreateIntCast(Val, ConvertType(ExprType), false, "tmp");
Eli Friedmanf2442dc2008-05-17 20:03:47 +0000396
Daniel Dunbaread7c912008-08-06 05:08:45 +0000397 return RValue::get(Val);
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000398}
399
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +0000400RValue CodeGenFunction::EmitLoadOfPropertyRefLValue(LValue LV,
401 QualType ExprType) {
402 return EmitObjCPropertyGet(LV.getPropertyRefExpr());
403}
404
Fariborz Jahanian9ac53512008-11-22 22:30:21 +0000405RValue CodeGenFunction::EmitLoadOfKVCRefLValue(LValue LV,
406 QualType ExprType) {
407 return EmitObjCPropertyGet(LV.getKVCRefExpr());
408}
409
Nate Begemanb699c9b2009-01-18 06:42:49 +0000410// If this is a reference to a subset of the elements of a vector, create an
411// appropriate shufflevector.
Nate Begemance4d7fc2008-04-18 23:10:10 +0000412RValue CodeGenFunction::EmitLoadOfExtVectorElementLValue(LValue LV,
413 QualType ExprType) {
Eli Friedman327944b2008-06-13 23:01:12 +0000414 llvm::Value *Vec = Builder.CreateLoad(LV.getExtVectorAddr(),
415 LV.isVolatileQualified(), "tmp");
Mike Stump4a3999f2009-09-09 13:00:44 +0000416
Nate Begemanf322eab2008-05-09 06:41:27 +0000417 const llvm::Constant *Elts = LV.getExtVectorElts();
Mike Stump4a3999f2009-09-09 13:00:44 +0000418
419 // If the result of the expression is a non-vector type, we must be extracting
420 // a single element. Just codegen as an extractelement.
Chris Lattner8eab8ff2007-08-10 17:10:08 +0000421 const VectorType *ExprVT = ExprType->getAsVectorType();
422 if (!ExprVT) {
Dan Gohman75d69da2008-05-22 00:50:06 +0000423 unsigned InIdx = getAccessedFieldNo(0, Elts);
Owen Anderson41a75022009-08-13 21:57:51 +0000424 llvm::Value *Elt = llvm::ConstantInt::get(
425 llvm::Type::getInt32Ty(VMContext), InIdx);
Chris Lattner40ff7012007-08-03 16:18:34 +0000426 return RValue::get(Builder.CreateExtractElement(Vec, Elt, "tmp"));
427 }
Nate Begemanb699c9b2009-01-18 06:42:49 +0000428
429 // Always use shuffle vector to try to retain the original program structure
Chris Lattner8eab8ff2007-08-10 17:10:08 +0000430 unsigned NumResultElts = ExprVT->getNumElements();
Mike Stump4a3999f2009-09-09 13:00:44 +0000431
Nate Begemanb699c9b2009-01-18 06:42:49 +0000432 llvm::SmallVector<llvm::Constant*, 4> Mask;
Chris Lattner40ff7012007-08-03 16:18:34 +0000433 for (unsigned i = 0; i != NumResultElts; ++i) {
Dan Gohman75d69da2008-05-22 00:50:06 +0000434 unsigned InIdx = getAccessedFieldNo(i, Elts);
Owen Anderson41a75022009-08-13 21:57:51 +0000435 Mask.push_back(llvm::ConstantInt::get(
436 llvm::Type::getInt32Ty(VMContext), InIdx));
Chris Lattner40ff7012007-08-03 16:18:34 +0000437 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000438
Owen Anderson3cc120a2009-07-28 21:22:35 +0000439 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
Nate Begemanb699c9b2009-01-18 06:42:49 +0000440 Vec = Builder.CreateShuffleVector(Vec,
Owen Anderson7ec07a52009-07-30 23:11:26 +0000441 llvm::UndefValue::get(Vec->getType()),
Nate Begemanb699c9b2009-01-18 06:42:49 +0000442 MaskV, "tmp");
443 return RValue::get(Vec);
Chris Lattner40ff7012007-08-03 16:18:34 +0000444}
445
446
Chris Lattner9369a562007-06-29 16:31:29 +0000447
Chris Lattner8394d792007-06-05 20:53:16 +0000448/// EmitStoreThroughLValue - Store the specified rvalue into the specified
449/// lvalue, where both are guaranteed to the have the same type, and that type
450/// is 'Ty'.
Mike Stump4a3999f2009-09-09 13:00:44 +0000451void CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst,
Chris Lattner8394d792007-06-05 20:53:16 +0000452 QualType Ty) {
Chris Lattner41d480e2007-08-03 16:28:33 +0000453 if (!Dst.isSimple()) {
454 if (Dst.isVectorElt()) {
455 // Read/modify/write the vector, inserting the new element.
Eli Friedman327944b2008-06-13 23:01:12 +0000456 llvm::Value *Vec = Builder.CreateLoad(Dst.getVectorAddr(),
457 Dst.isVolatileQualified(), "tmp");
Chris Lattner4647a212007-08-31 22:49:20 +0000458 Vec = Builder.CreateInsertElement(Vec, Src.getScalarVal(),
Chris Lattner41d480e2007-08-03 16:28:33 +0000459 Dst.getVectorIdx(), "vecins");
Eli Friedman327944b2008-06-13 23:01:12 +0000460 Builder.CreateStore(Vec, Dst.getVectorAddr(),Dst.isVolatileQualified());
Chris Lattner41d480e2007-08-03 16:28:33 +0000461 return;
462 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000463
Nate Begemance4d7fc2008-04-18 23:10:10 +0000464 // If this is an update of extended vector elements, insert them as
465 // appropriate.
466 if (Dst.isExtVectorElt())
467 return EmitStoreThroughExtVectorComponentLValue(Src, Dst, Ty);
Lauro Ramos Venancio09af71c2008-01-22 22:36:45 +0000468
469 if (Dst.isBitfield())
470 return EmitStoreThroughBitfieldLValue(Src, Dst, Ty);
471
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +0000472 if (Dst.isPropertyRef())
473 return EmitStoreThroughPropertyRefLValue(Src, Dst, Ty);
474
Fariborz Jahanian9ac53512008-11-22 22:30:21 +0000475 if (Dst.isKVCRef())
476 return EmitStoreThroughKVCRefLValue(Src, Dst, Ty);
477
Lauro Ramos Venanciodb449042008-01-22 22:38:35 +0000478 assert(0 && "Unknown LValue type");
Chris Lattner41d480e2007-08-03 16:28:33 +0000479 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000480
Fariborz Jahanian10bec102009-02-21 00:30:43 +0000481 if (Dst.isObjCWeak() && !Dst.isNonGC()) {
Mike Stump4a3999f2009-09-09 13:00:44 +0000482 // load of a __weak object.
Fariborz Jahanian50a12702008-11-19 17:34:06 +0000483 llvm::Value *LvalueDst = Dst.getAddress();
484 llvm::Value *src = Src.getScalarVal();
Mike Stumpca5ae662009-04-14 00:57:29 +0000485 CGM.getObjCRuntime().EmitObjCWeakAssign(*this, src, LvalueDst);
Fariborz Jahanian50a12702008-11-19 17:34:06 +0000486 return;
487 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000488
Fariborz Jahanian10bec102009-02-21 00:30:43 +0000489 if (Dst.isObjCStrong() && !Dst.isNonGC()) {
Mike Stump4a3999f2009-09-09 13:00:44 +0000490 // load of a __strong object.
Fariborz Jahanian50a12702008-11-19 17:34:06 +0000491 llvm::Value *LvalueDst = Dst.getAddress();
492 llvm::Value *src = Src.getScalarVal();
Fariborz Jahanian3114e942009-02-19 18:29:24 +0000493#if 0
Mike Stump4a3999f2009-09-09 13:00:44 +0000494 // FIXME: We cannot positively determine if we have an 'ivar' assignment,
Mike Stump18bb9282009-05-16 07:57:57 +0000495 // object assignment or an unknown assignment. For now, generate call to
496 // objc_assign_strongCast assignment which is a safe, but consevative
497 // assumption.
Fariborz Jahanian75686a52008-11-20 20:53:20 +0000498 if (Dst.isObjCIvar())
499 CGM.getObjCRuntime().EmitObjCIvarAssign(*this, src, LvalueDst);
500 else
501 CGM.getObjCRuntime().EmitObjCGlobalAssign(*this, src, LvalueDst);
Fariborz Jahanian3114e942009-02-19 18:29:24 +0000502#endif
Fariborz Jahanian32ff7ae2009-05-04 23:27:20 +0000503 if (Dst.isGlobalObjCRef())
504 CGM.getObjCRuntime().EmitObjCGlobalAssign(*this, src, LvalueDst);
505 else
506 CGM.getObjCRuntime().EmitObjCStrongCastAssign(*this, src, LvalueDst);
Fariborz Jahanian50a12702008-11-19 17:34:06 +0000507 return;
508 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000509
Chris Lattner6278e6a2007-08-11 00:04:45 +0000510 assert(Src.isScalar() && "Can't emit an agg store with this method");
Anders Carlsson83709642009-05-19 18:50:41 +0000511 EmitStoreOfScalar(Src.getScalarVal(), Dst.getAddress(),
512 Dst.isVolatileQualified(), Ty);
Chris Lattner8394d792007-06-05 20:53:16 +0000513}
514
Lauro Ramos Venancio09af71c2008-01-22 22:36:45 +0000515void CodeGenFunction::EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst,
Mike Stump4a3999f2009-09-09 13:00:44 +0000516 QualType Ty,
Daniel Dunbar9b1335e2008-11-19 09:36:46 +0000517 llvm::Value **Result) {
Daniel Dunbaread7c912008-08-06 05:08:45 +0000518 unsigned StartBit = Dst.getBitfieldStartBit();
519 unsigned BitfieldSize = Dst.getBitfieldSize();
Lauro Ramos Venancio09af71c2008-01-22 22:36:45 +0000520 llvm::Value *Ptr = Dst.getBitfieldAddr();
Lauro Ramos Venancio09af71c2008-01-22 22:36:45 +0000521
Mike Stump4a3999f2009-09-09 13:00:44 +0000522 const llvm::Type *EltTy =
Daniel Dunbaread7c912008-08-06 05:08:45 +0000523 cast<llvm::PointerType>(Ptr->getType())->getElementType();
524 unsigned EltTySize = CGM.getTargetData().getTypeSizeInBits(EltTy);
525
Mike Stump4a3999f2009-09-09 13:00:44 +0000526 // Get the new value, cast to the appropriate type and masked to exactly the
527 // size of the bit-field.
Daniel Dunbar9b1335e2008-11-19 09:36:46 +0000528 llvm::Value *SrcVal = Src.getScalarVal();
529 llvm::Value *NewVal = Builder.CreateIntCast(SrcVal, EltTy, false, "tmp");
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000530 llvm::Constant *Mask = llvm::ConstantInt::get(VMContext,
531 llvm::APInt::getLowBitsSet(EltTySize, BitfieldSize));
Daniel Dunbaread7c912008-08-06 05:08:45 +0000532 NewVal = Builder.CreateAnd(NewVal, Mask, "bf.value");
Lauro Ramos Venancio09af71c2008-01-22 22:36:45 +0000533
Daniel Dunbar9b1335e2008-11-19 09:36:46 +0000534 // Return the new value of the bit-field, if requested.
535 if (Result) {
536 // Cast back to the proper type for result.
537 const llvm::Type *SrcTy = SrcVal->getType();
538 llvm::Value *SrcTrunc = Builder.CreateIntCast(NewVal, SrcTy, false,
539 "bf.reload.val");
540
541 // Sign extend if necessary.
542 if (Dst.isBitfieldSigned()) {
543 unsigned SrcTySize = CGM.getTargetData().getTypeSizeInBits(SrcTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000544 llvm::Value *ExtraBits = llvm::ConstantInt::get(SrcTy,
Daniel Dunbar9b1335e2008-11-19 09:36:46 +0000545 SrcTySize - BitfieldSize);
Mike Stump4a3999f2009-09-09 13:00:44 +0000546 SrcTrunc = Builder.CreateAShr(Builder.CreateShl(SrcTrunc, ExtraBits),
Daniel Dunbar9b1335e2008-11-19 09:36:46 +0000547 ExtraBits, "bf.reload.sext");
548 }
549
550 *Result = SrcTrunc;
551 }
552
Mike Stump4a3999f2009-09-09 13:00:44 +0000553 // In some cases the bitfield may straddle two memory locations. Emit the low
554 // part first and check to see if the high needs to be done.
Daniel Dunbaread7c912008-08-06 05:08:45 +0000555 unsigned LowBits = std::min(BitfieldSize, EltTySize - StartBit);
556 llvm::Value *LowVal = Builder.CreateLoad(Ptr, Dst.isVolatileQualified(),
557 "bf.prev.low");
Eli Friedmanf2442dc2008-05-17 20:03:47 +0000558
Daniel Dunbaread7c912008-08-06 05:08:45 +0000559 // Compute the mask for zero-ing the low part of this bitfield.
Mike Stump4a3999f2009-09-09 13:00:44 +0000560 llvm::Constant *InvMask =
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000561 llvm::ConstantInt::get(VMContext,
562 ~llvm::APInt::getBitsSet(EltTySize, StartBit, StartBit + LowBits));
Mike Stump4a3999f2009-09-09 13:00:44 +0000563
Daniel Dunbaread7c912008-08-06 05:08:45 +0000564 // Compute the new low part as
565 // LowVal = (LowVal & InvMask) | (NewVal << StartBit),
566 // with the shift of NewVal implicitly stripping the high bits.
Mike Stump4a3999f2009-09-09 13:00:44 +0000567 llvm::Value *NewLowVal =
568 Builder.CreateShl(NewVal, llvm::ConstantInt::get(EltTy, StartBit),
569 "bf.value.lo");
Daniel Dunbaread7c912008-08-06 05:08:45 +0000570 LowVal = Builder.CreateAnd(LowVal, InvMask, "bf.prev.lo.cleared");
571 LowVal = Builder.CreateOr(LowVal, NewLowVal, "bf.new.lo");
Mike Stump4a3999f2009-09-09 13:00:44 +0000572
Daniel Dunbaread7c912008-08-06 05:08:45 +0000573 // Write back.
574 Builder.CreateStore(LowVal, Ptr, Dst.isVolatileQualified());
Eli Friedmanf2442dc2008-05-17 20:03:47 +0000575
Daniel Dunbaread7c912008-08-06 05:08:45 +0000576 // If the low part doesn't cover the bitfield emit a high part.
577 if (LowBits < BitfieldSize) {
578 unsigned HighBits = BitfieldSize - LowBits;
Owen Anderson41a75022009-08-13 21:57:51 +0000579 llvm::Value *HighPtr = Builder.CreateGEP(Ptr, llvm::ConstantInt::get(
Mike Stump4a3999f2009-09-09 13:00:44 +0000580 llvm::Type::getInt32Ty(VMContext), 1), "bf.ptr.hi");
581 llvm::Value *HighVal = Builder.CreateLoad(HighPtr,
Daniel Dunbaread7c912008-08-06 05:08:45 +0000582 Dst.isVolatileQualified(),
583 "bf.prev.hi");
Mike Stump4a3999f2009-09-09 13:00:44 +0000584
Daniel Dunbaread7c912008-08-06 05:08:45 +0000585 // Compute the mask for zero-ing the high part of this bitfield.
Mike Stump4a3999f2009-09-09 13:00:44 +0000586 llvm::Constant *InvMask =
587 llvm::ConstantInt::get(VMContext, ~llvm::APInt::getLowBitsSet(EltTySize,
Owen Anderson170229f2009-07-14 23:10:40 +0000588 HighBits));
Mike Stump4a3999f2009-09-09 13:00:44 +0000589
Daniel Dunbaread7c912008-08-06 05:08:45 +0000590 // Compute the new high part as
591 // HighVal = (HighVal & InvMask) | (NewVal lshr LowBits),
592 // where the high bits of NewVal have already been cleared and the
593 // shift stripping the low bits.
Mike Stump4a3999f2009-09-09 13:00:44 +0000594 llvm::Value *NewHighVal =
595 Builder.CreateLShr(NewVal, llvm::ConstantInt::get(EltTy, LowBits),
596 "bf.value.high");
Daniel Dunbaread7c912008-08-06 05:08:45 +0000597 HighVal = Builder.CreateAnd(HighVal, InvMask, "bf.prev.hi.cleared");
598 HighVal = Builder.CreateOr(HighVal, NewHighVal, "bf.new.hi");
Mike Stump4a3999f2009-09-09 13:00:44 +0000599
Daniel Dunbaread7c912008-08-06 05:08:45 +0000600 // Write back.
601 Builder.CreateStore(HighVal, HighPtr, Dst.isVolatileQualified());
602 }
Lauro Ramos Venancio09af71c2008-01-22 22:36:45 +0000603}
604
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +0000605void CodeGenFunction::EmitStoreThroughPropertyRefLValue(RValue Src,
606 LValue Dst,
607 QualType Ty) {
608 EmitObjCPropertySet(Dst.getPropertyRefExpr(), Src);
609}
610
Fariborz Jahanian9ac53512008-11-22 22:30:21 +0000611void CodeGenFunction::EmitStoreThroughKVCRefLValue(RValue Src,
612 LValue Dst,
613 QualType Ty) {
614 EmitObjCPropertySet(Dst.getKVCRefExpr(), Src);
615}
616
Nate Begemance4d7fc2008-04-18 23:10:10 +0000617void CodeGenFunction::EmitStoreThroughExtVectorComponentLValue(RValue Src,
618 LValue Dst,
619 QualType Ty) {
Chris Lattner41d480e2007-08-03 16:28:33 +0000620 // This access turns into a read/modify/write of the vector. Load the input
621 // value now.
Eli Friedman327944b2008-06-13 23:01:12 +0000622 llvm::Value *Vec = Builder.CreateLoad(Dst.getExtVectorAddr(),
623 Dst.isVolatileQualified(), "tmp");
Nate Begemanf322eab2008-05-09 06:41:27 +0000624 const llvm::Constant *Elts = Dst.getExtVectorElts();
Mike Stump4a3999f2009-09-09 13:00:44 +0000625
Chris Lattner4647a212007-08-31 22:49:20 +0000626 llvm::Value *SrcVal = Src.getScalarVal();
Mike Stump4a3999f2009-09-09 13:00:44 +0000627
Chris Lattner3a44aa72007-08-03 16:37:04 +0000628 if (const VectorType *VTy = Ty->getAsVectorType()) {
629 unsigned NumSrcElts = VTy->getNumElements();
Nate Begemanb699c9b2009-01-18 06:42:49 +0000630 unsigned NumDstElts =
631 cast<llvm::VectorType>(Vec->getType())->getNumElements();
632 if (NumDstElts == NumSrcElts) {
Mike Stump4a3999f2009-09-09 13:00:44 +0000633 // Use shuffle vector is the src and destination are the same number of
634 // elements and restore the vector mask since it is on the side it will be
635 // stored.
Nate Begemanea12f6e2009-06-26 21:12:50 +0000636 llvm::SmallVector<llvm::Constant*, 4> Mask(NumDstElts);
Nate Begemanb699c9b2009-01-18 06:42:49 +0000637 for (unsigned i = 0; i != NumSrcElts; ++i) {
638 unsigned InIdx = getAccessedFieldNo(i, Elts);
Owen Anderson41a75022009-08-13 21:57:51 +0000639 Mask[InIdx] = llvm::ConstantInt::get(
640 llvm::Type::getInt32Ty(VMContext), i);
Nate Begemanb699c9b2009-01-18 06:42:49 +0000641 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000642
Owen Anderson3cc120a2009-07-28 21:22:35 +0000643 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
Nate Begemanb699c9b2009-01-18 06:42:49 +0000644 Vec = Builder.CreateShuffleVector(SrcVal,
Owen Anderson7ec07a52009-07-30 23:11:26 +0000645 llvm::UndefValue::get(Vec->getType()),
Nate Begemanb699c9b2009-01-18 06:42:49 +0000646 MaskV, "tmp");
Mike Stump658fe022009-07-30 22:28:39 +0000647 } else if (NumDstElts > NumSrcElts) {
Nate Begemanb699c9b2009-01-18 06:42:49 +0000648 // Extended the source vector to the same length and then shuffle it
649 // into the destination.
650 // FIXME: since we're shuffling with undef, can we just use the indices
651 // into that? This could be simpler.
652 llvm::SmallVector<llvm::Constant*, 4> ExtMask;
653 unsigned i;
654 for (i = 0; i != NumSrcElts; ++i)
Owen Anderson41a75022009-08-13 21:57:51 +0000655 ExtMask.push_back(llvm::ConstantInt::get(
656 llvm::Type::getInt32Ty(VMContext), i));
Nate Begemanb699c9b2009-01-18 06:42:49 +0000657 for (; i != NumDstElts; ++i)
Owen Anderson41a75022009-08-13 21:57:51 +0000658 ExtMask.push_back(llvm::UndefValue::get(
659 llvm::Type::getInt32Ty(VMContext)));
Owen Anderson3cc120a2009-07-28 21:22:35 +0000660 llvm::Value *ExtMaskV = llvm::ConstantVector::get(&ExtMask[0],
Nate Begemanb699c9b2009-01-18 06:42:49 +0000661 ExtMask.size());
Mike Stump4a3999f2009-09-09 13:00:44 +0000662 llvm::Value *ExtSrcVal =
Daniel Dunbar3d926cb2009-02-17 18:31:04 +0000663 Builder.CreateShuffleVector(SrcVal,
Owen Anderson7ec07a52009-07-30 23:11:26 +0000664 llvm::UndefValue::get(SrcVal->getType()),
Daniel Dunbar3d926cb2009-02-17 18:31:04 +0000665 ExtMaskV, "tmp");
Nate Begemanb699c9b2009-01-18 06:42:49 +0000666 // build identity
667 llvm::SmallVector<llvm::Constant*, 4> Mask;
668 for (unsigned i = 0; i != NumDstElts; ++i) {
Owen Anderson41a75022009-08-13 21:57:51 +0000669 Mask.push_back(llvm::ConstantInt::get(
670 llvm::Type::getInt32Ty(VMContext), i));
Nate Begemanb699c9b2009-01-18 06:42:49 +0000671 }
672 // modify when what gets shuffled in
673 for (unsigned i = 0; i != NumSrcElts; ++i) {
674 unsigned Idx = getAccessedFieldNo(i, Elts);
Owen Anderson41a75022009-08-13 21:57:51 +0000675 Mask[Idx] = llvm::ConstantInt::get(
676 llvm::Type::getInt32Ty(VMContext), i+NumDstElts);
Nate Begemanb699c9b2009-01-18 06:42:49 +0000677 }
Owen Anderson3cc120a2009-07-28 21:22:35 +0000678 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
Nate Begemanb699c9b2009-01-18 06:42:49 +0000679 Vec = Builder.CreateShuffleVector(Vec, ExtSrcVal, MaskV, "tmp");
Mike Stump658fe022009-07-30 22:28:39 +0000680 } else {
Nate Begemanb699c9b2009-01-18 06:42:49 +0000681 // We should never shorten the vector
682 assert(0 && "unexpected shorten vector length");
Chris Lattner3a44aa72007-08-03 16:37:04 +0000683 }
684 } else {
685 // If the Src is a scalar (not a vector) it must be updating one element.
Dan Gohman75d69da2008-05-22 00:50:06 +0000686 unsigned InIdx = getAccessedFieldNo(0, Elts);
Owen Anderson41a75022009-08-13 21:57:51 +0000687 llvm::Value *Elt = llvm::ConstantInt::get(
688 llvm::Type::getInt32Ty(VMContext), InIdx);
Chris Lattner41d480e2007-08-03 16:28:33 +0000689 Vec = Builder.CreateInsertElement(Vec, SrcVal, Elt, "tmp");
Chris Lattner41d480e2007-08-03 16:28:33 +0000690 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000691
Eli Friedman327944b2008-06-13 23:01:12 +0000692 Builder.CreateStore(Vec, Dst.getExtVectorAddr(), Dst.isVolatileQualified());
Chris Lattner41d480e2007-08-03 16:28:33 +0000693}
694
Chris Lattnerd7f58862007-06-02 05:24:33 +0000695LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) {
Steve Naroff08899ff2008-04-15 22:42:06 +0000696 const VarDecl *VD = dyn_cast<VarDecl>(E->getDecl());
Mike Stump4a3999f2009-09-09 13:00:44 +0000697
Chris Lattner5696e7b2008-06-17 18:05:57 +0000698 if (VD && (VD->isBlockVarDecl() || isa<ParmVarDecl>(VD) ||
699 isa<ImplicitParamDecl>(VD))) {
Fariborz Jahanian003e8302008-11-20 00:15:42 +0000700 LValue LV;
Mike Stump4a3999f2009-09-09 13:00:44 +0000701 bool NonGCable = VD->hasLocalStorage() &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000702 !VD->hasAttr<BlocksAttr>();
Daniel Dunbar0ca16602009-04-14 02:25:56 +0000703 if (VD->hasExternalStorage()) {
Anders Carlsson2d228ce2009-05-19 20:40:02 +0000704 llvm::Value *V = CGM.GetAddrOfGlobalVar(VD);
705 if (VD->getType()->isReferenceType())
706 V = Builder.CreateLoad(V, "tmp");
707 LV = LValue::MakeAddr(V, E->getType().getCVRQualifiers(),
Mon P Wangacedf772009-07-22 03:08:17 +0000708 getContext().getObjCGCAttrKind(E->getType()),
709 E->getType().getAddressSpace());
Mike Stump658fe022009-07-30 22:28:39 +0000710 } else {
Steve Naroff08899ff2008-04-15 22:42:06 +0000711 llvm::Value *V = LocalDeclMap[VD];
Mike Stump1db7d042009-02-28 09:07:16 +0000712 assert(V && "DeclRefExpr not entered in LocalDeclMap?");
Fariborz Jahanianc86fb5e2009-02-20 01:14:43 +0000713 // local variables do not get their gc attribute set.
714 QualType::GCAttrTypes attr = QualType::GCNone;
715 // local static?
Fariborz Jahanian75512572009-05-27 19:48:48 +0000716 if (!NonGCable)
Fariborz Jahanianc86fb5e2009-02-20 01:14:43 +0000717 attr = getContext().getObjCGCAttrKind(E->getType());
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000718 if (VD->hasAttr<BlocksAttr>()) {
Mike Stump97d01d52009-03-04 03:23:46 +0000719 V = Builder.CreateStructGEP(V, 1, "forwarding");
Mike Stump97d01d52009-03-04 03:23:46 +0000720 V = Builder.CreateLoad(V, false);
Anders Carlssonf8e94f22009-09-12 02:44:18 +0000721 V = Builder.CreateStructGEP(V, getByRefValueLLVMField(VD),
722 VD->getNameAsString());
Mike Stump97d01d52009-03-04 03:23:46 +0000723 }
Anders Carlsson2d228ce2009-05-19 20:40:02 +0000724 if (VD->getType()->isReferenceType())
725 V = Builder.CreateLoad(V, "tmp");
Mon P Wangacedf772009-07-22 03:08:17 +0000726 LV = LValue::MakeAddr(V, E->getType().getCVRQualifiers(), attr,
727 E->getType().getAddressSpace());
Lauro Ramos Venanciobada8d42008-02-16 22:30:38 +0000728 }
Fariborz Jahanian75512572009-05-27 19:48:48 +0000729 LValue::SetObjCNonGC(LV, NonGCable);
Fariborz Jahanian003e8302008-11-20 00:15:42 +0000730 return LV;
Steve Naroff08899ff2008-04-15 22:42:06 +0000731 } else if (VD && VD->isFileVarDecl()) {
Anders Carlsson2d228ce2009-05-19 20:40:02 +0000732 llvm::Value *V = CGM.GetAddrOfGlobalVar(VD);
733 if (VD->getType()->isReferenceType())
734 V = Builder.CreateLoad(V, "tmp");
735 LValue LV = LValue::MakeAddr(V, E->getType().getCVRQualifiers(),
Mon P Wangacedf772009-07-22 03:08:17 +0000736 getContext().getObjCGCAttrKind(E->getType()),
737 E->getType().getAddressSpace());
Fariborz Jahanian32ff7ae2009-05-04 23:27:20 +0000738 if (LV.isObjCStrong())
739 LV.SetGlobalObjCRef(LV, true);
Fariborz Jahanian406b1172008-11-18 20:18:11 +0000740 return LV;
Steve Naroff08899ff2008-04-15 22:42:06 +0000741 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(E->getDecl())) {
Anders Carlssonecf9bf02009-09-10 23:43:36 +0000742 llvm::Value* V = CGM.GetAddrOfFunction(FD);
Eli Friedmane32c0212009-06-01 10:04:20 +0000743 if (!FD->hasPrototype()) {
744 if (const FunctionProtoType *Proto =
745 FD->getType()->getAsFunctionProtoType()) {
746 // Ugly case: for a K&R-style definition, the type of the definition
747 // isn't the same as the type of a use. Correct for this with a
748 // bitcast.
749 QualType NoProtoType =
750 getContext().getFunctionNoProtoType(Proto->getResultType());
751 NoProtoType = getContext().getPointerType(NoProtoType);
752 V = Builder.CreateBitCast(V, ConvertType(NoProtoType), "tmp");
753 }
754 }
755 return LValue::MakeAddr(V, E->getType().getCVRQualifiers(),
Mon P Wangacedf772009-07-22 03:08:17 +0000756 getContext().getObjCGCAttrKind(E->getType()),
757 E->getType().getAddressSpace());
Mike Stump658fe022009-07-30 22:28:39 +0000758 } else if (const ImplicitParamDecl *IPD =
Chris Lattner5696e7b2008-06-17 18:05:57 +0000759 dyn_cast<ImplicitParamDecl>(E->getDecl())) {
760 llvm::Value *V = LocalDeclMap[IPD];
761 assert(V && "BlockVarDecl not entered in LocalDeclMap?");
Fariborz Jahanian0f466c72009-02-19 23:36:06 +0000762 return LValue::MakeAddr(V, E->getType().getCVRQualifiers(),
Mon P Wangacedf772009-07-22 03:08:17 +0000763 getContext().getObjCGCAttrKind(E->getType()),
764 E->getType().getAddressSpace());
Chris Lattner5696e7b2008-06-17 18:05:57 +0000765 }
Chris Lattnerd7f58862007-06-02 05:24:33 +0000766 assert(0 && "Unimp declref");
Chris Lattner793d10c2007-09-16 19:23:47 +0000767 //an invalid LValue, but the assert will
768 //ensure that this point is never reached.
769 return LValue();
Chris Lattnerd7f58862007-06-02 05:24:33 +0000770}
Chris Lattnere47e4402007-06-01 18:02:12 +0000771
Mike Stump1db7d042009-02-28 09:07:16 +0000772LValue CodeGenFunction::EmitBlockDeclRefLValue(const BlockDeclRefExpr *E) {
Mike Stump4a3999f2009-09-09 13:00:44 +0000773 return LValue::MakeAddr(GetAddrOfBlockDecl(E),
Daniel Dunbar223db1c2009-05-23 02:49:02 +0000774 E->getType().getCVRQualifiers(),
Mon P Wangacedf772009-07-22 03:08:17 +0000775 getContext().getObjCGCAttrKind(E->getType()),
776 E->getType().getAddressSpace());
Mike Stump1db7d042009-02-28 09:07:16 +0000777}
778
Chris Lattner8394d792007-06-05 20:53:16 +0000779LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) {
780 // __extension__ doesn't affect lvalue-ness.
781 if (E->getOpcode() == UnaryOperator::Extension)
782 return EmitLValue(E->getSubExpr());
Mike Stump4a3999f2009-09-09 13:00:44 +0000783
Chris Lattner0f398c42008-07-26 22:37:01 +0000784 QualType ExprTy = getContext().getCanonicalType(E->getSubExpr()->getType());
Chris Lattner595db862007-10-30 22:53:42 +0000785 switch (E->getOpcode()) {
786 default: assert(0 && "Unknown unary operator lvalue!");
787 case UnaryOperator::Deref:
Fariborz Jahanian0f466c72009-02-19 23:36:06 +0000788 {
Steve Naroff7cae42b2009-07-10 23:34:53 +0000789 QualType T = E->getSubExpr()->getType()->getPointeeType();
790 assert(!T.isNull() && "CodeGenFunction::EmitUnaryOpLValue: Illegal type");
Mike Stump4a3999f2009-09-09 13:00:44 +0000791
Fariborz Jahanian392124c2009-02-23 18:59:50 +0000792 LValue LV = LValue::MakeAddr(EmitScalarExpr(E->getSubExpr()),
Mike Stump4a3999f2009-09-09 13:00:44 +0000793 T.getCVRQualifiers(),
Mon P Wangacedf772009-07-22 03:08:17 +0000794 getContext().getObjCGCAttrKind(T),
795 ExprTy.getAddressSpace());
Fariborz Jahanian392124c2009-02-23 18:59:50 +0000796 // We should not generate __weak write barrier on indirect reference
797 // of a pointer to object; as in void foo (__weak id *param); *param = 0;
798 // But, we continue to generate __strong write barrier on indirect write
799 // into a pointer to object.
800 if (getContext().getLangOptions().ObjC1 &&
801 getContext().getLangOptions().getGCMode() != LangOptions::NonGC &&
802 LV.isObjCWeak())
Fariborz Jahanianc6d98002009-06-01 21:29:32 +0000803 LValue::SetObjCNonGC(LV, !E->isOBJCGCCandidate(getContext()));
Fariborz Jahanian392124c2009-02-23 18:59:50 +0000804 return LV;
Fariborz Jahanian0f466c72009-02-19 23:36:06 +0000805 }
Chris Lattner595db862007-10-30 22:53:42 +0000806 case UnaryOperator::Real:
807 case UnaryOperator::Imag:
808 LValue LV = EmitLValue(E->getSubExpr());
Chris Lattner3e593cd2008-03-19 05:19:41 +0000809 unsigned Idx = E->getOpcode() == UnaryOperator::Imag;
810 return LValue::MakeAddr(Builder.CreateStructGEP(LV.getAddress(),
Chris Lattner574dee62008-07-26 22:17:49 +0000811 Idx, "idx"),
Mon P Wangacedf772009-07-22 03:08:17 +0000812 ExprTy.getCVRQualifiers(),
813 QualType::GCNone,
814 ExprTy.getAddressSpace());
Chris Lattner595db862007-10-30 22:53:42 +0000815 }
Chris Lattner8394d792007-06-05 20:53:16 +0000816}
817
Chris Lattner4347e3692007-06-06 04:54:52 +0000818LValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) {
Daniel Dunbarc4baa062008-08-13 23:20:05 +0000819 return LValue::MakeAddr(CGM.GetAddrOfConstantStringFromLiteral(E), 0);
Chris Lattner4347e3692007-06-06 04:54:52 +0000820}
821
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +0000822LValue CodeGenFunction::EmitObjCEncodeExprLValue(const ObjCEncodeExpr *E) {
823 return LValue::MakeAddr(CGM.GetAddrOfConstantStringFromObjCEncode(E), 0);
824}
825
826
Daniel Dunbarb3517472008-10-17 21:58:32 +0000827LValue CodeGenFunction::EmitPredefinedFunctionName(unsigned Type) {
Anders Carlsson625bfc82007-07-21 05:21:51 +0000828 std::string GlobalVarName;
Daniel Dunbarb3517472008-10-17 21:58:32 +0000829
830 switch (Type) {
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +0000831 default:
832 assert(0 && "Invalid type");
833 case PredefinedExpr::Func:
834 GlobalVarName = "__func__.";
835 break;
836 case PredefinedExpr::Function:
837 GlobalVarName = "__FUNCTION__.";
838 break;
839 case PredefinedExpr::PrettyFunction:
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +0000840 GlobalVarName = "__PRETTY_FUNCTION__.";
841 break;
Anders Carlsson625bfc82007-07-21 05:21:51 +0000842 }
Daniel Dunbarb3517472008-10-17 21:58:32 +0000843
Daniel Dunbar0482cfd2009-09-12 23:06:21 +0000844 llvm::StringRef FnName = CurFn->getName();
845 if (FnName.startswith("\01"))
846 FnName = FnName.substr(1);
847 GlobalVarName += FnName;
848
Anders Carlsson2fb08242009-09-08 18:24:21 +0000849 std::string FunctionName =
Mike Stump11289f42009-09-09 15:08:12 +0000850 PredefinedExpr::ComputeName(getContext(), (PredefinedExpr::IdentType)Type,
Anders Carlsson2fb08242009-09-08 18:24:21 +0000851 CurCodeDecl);
Daniel Dunbarb3517472008-10-17 21:58:32 +0000852
Mike Stump4a3999f2009-09-09 13:00:44 +0000853 llvm::Constant *C =
Daniel Dunbarb3517472008-10-17 21:58:32 +0000854 CGM.GetAddrOfConstantCString(FunctionName, GlobalVarName.c_str());
855 return LValue::MakeAddr(C, 0);
856}
857
Mike Stump4a3999f2009-09-09 13:00:44 +0000858LValue CodeGenFunction::EmitPredefinedLValue(const PredefinedExpr *E) {
Daniel Dunbarb3517472008-10-17 21:58:32 +0000859 switch (E->getIdentType()) {
860 default:
861 return EmitUnsupportedLValue(E, "predefined expression");
862 case PredefinedExpr::Func:
863 case PredefinedExpr::Function:
864 case PredefinedExpr::PrettyFunction:
865 return EmitPredefinedFunctionName(E->getIdentType());
866 }
Anders Carlsson625bfc82007-07-21 05:21:51 +0000867}
868
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000869LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) {
Ted Kremenekc81614d2007-08-20 16:18:38 +0000870 // The index must always be an integer, which is not an aggregate. Emit it.
Chris Lattner2da04b32007-08-24 05:35:26 +0000871 llvm::Value *Idx = EmitScalarExpr(E->getIdx());
Eli Friedman07bbeca2009-06-06 19:09:26 +0000872 QualType IdxTy = E->getIdx()->getType();
873 bool IdxSigned = IdxTy->isSignedIntegerType();
874
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000875 // If the base is a vector type, then we are forming a vector element lvalue
876 // with this subscript.
Eli Friedman327944b2008-06-13 23:01:12 +0000877 if (E->getBase()->getType()->isVectorType()) {
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000878 // Emit the vector as an lvalue to get its address.
Eli Friedman327944b2008-06-13 23:01:12 +0000879 LValue LHS = EmitLValue(E->getBase());
Ted Kremenekc81614d2007-08-20 16:18:38 +0000880 assert(LHS.isSimple() && "Can only subscript lvalue vectors here!");
Mike Stump4a3999f2009-09-09 13:00:44 +0000881 Idx = Builder.CreateIntCast(Idx,
Owen Anderson41a75022009-08-13 21:57:51 +0000882 llvm::Type::getInt32Ty(VMContext), IdxSigned, "vidx");
Eli Friedman327944b2008-06-13 23:01:12 +0000883 return LValue::MakeVectorElt(LHS.getAddress(), Idx,
884 E->getBase()->getType().getCVRQualifiers());
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000885 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000886
Ted Kremenekc81614d2007-08-20 16:18:38 +0000887 // The base must be a pointer, which is not an aggregate. Emit it.
Chris Lattner2da04b32007-08-24 05:35:26 +0000888 llvm::Value *Base = EmitScalarExpr(E->getBase());
Mike Stump4a3999f2009-09-09 13:00:44 +0000889
Ted Kremenekc81614d2007-08-20 16:18:38 +0000890 // Extend or truncate the index type to 32 or 64-bits.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000891 unsigned IdxBitwidth = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
Sanjiv Gupta47425152009-04-24 02:40:57 +0000892 if (IdxBitwidth != LLVMPointerWidth)
Owen Anderson41a75022009-08-13 21:57:51 +0000893 Idx = Builder.CreateIntCast(Idx,
894 llvm::IntegerType::get(VMContext, LLVMPointerWidth),
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000895 IdxSigned, "idxprom");
896
Mike Stump4a3999f2009-09-09 13:00:44 +0000897 // We know that the pointer points to a type of the correct size, unless the
898 // size is a VLA or Objective-C interface.
Daniel Dunbaref2ffbc2009-04-25 05:08:32 +0000899 llvm::Value *Address = 0;
Mike Stump4a3999f2009-09-09 13:00:44 +0000900 if (const VariableArrayType *VAT =
Anders Carlsson3d312f82008-12-21 00:11:23 +0000901 getContext().getAsVariableArrayType(E->getType())) {
Chris Lattner19efdd62009-08-14 23:43:22 +0000902 llvm::Value *VLASize = GetVLASize(VAT);
Mike Stump4a3999f2009-09-09 13:00:44 +0000903
Anders Carlsson3d312f82008-12-21 00:11:23 +0000904 Idx = Builder.CreateMul(Idx, VLASize);
Mike Stump4a3999f2009-09-09 13:00:44 +0000905
Anders Carlssone0808df2008-12-21 03:44:36 +0000906 QualType BaseType = getContext().getBaseElementType(VAT);
Mike Stump4a3999f2009-09-09 13:00:44 +0000907
Anders Carlsson3d312f82008-12-21 00:11:23 +0000908 uint64_t BaseTypeSize = getContext().getTypeSize(BaseType) / 8;
909 Idx = Builder.CreateUDiv(Idx,
Mike Stump4a3999f2009-09-09 13:00:44 +0000910 llvm::ConstantInt::get(Idx->getType(),
Anders Carlsson3d312f82008-12-21 00:11:23 +0000911 BaseTypeSize));
Dan Gohman43b44842009-08-12 00:33:55 +0000912 Address = Builder.CreateInBoundsGEP(Base, Idx, "arrayidx");
Mike Stump4a3999f2009-09-09 13:00:44 +0000913 } else if (const ObjCInterfaceType *OIT =
Daniel Dunbaref2ffbc2009-04-25 05:08:32 +0000914 dyn_cast<ObjCInterfaceType>(E->getType())) {
Mike Stump4a3999f2009-09-09 13:00:44 +0000915 llvm::Value *InterfaceSize =
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000916 llvm::ConstantInt::get(Idx->getType(),
Daniel Dunbaref2ffbc2009-04-25 05:08:32 +0000917 getContext().getTypeSize(OIT) / 8);
Mike Stump4a3999f2009-09-09 13:00:44 +0000918
Daniel Dunbaref2ffbc2009-04-25 05:08:32 +0000919 Idx = Builder.CreateMul(Idx, InterfaceSize);
920
Owen Anderson41a75022009-08-13 21:57:51 +0000921 llvm::Type *i8PTy =
922 llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext));
Dan Gohman43b44842009-08-12 00:33:55 +0000923 Address = Builder.CreateGEP(Builder.CreateBitCast(Base, i8PTy),
Daniel Dunbaref2ffbc2009-04-25 05:08:32 +0000924 Idx, "arrayidx");
925 Address = Builder.CreateBitCast(Address, Base->getType());
926 } else {
Dan Gohman43b44842009-08-12 00:33:55 +0000927 Address = Builder.CreateInBoundsGEP(Base, Idx, "arrayidx");
Anders Carlsson3d312f82008-12-21 00:11:23 +0000928 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000929
Steve Naroff7cae42b2009-07-10 23:34:53 +0000930 QualType T = E->getBase()->getType()->getPointeeType();
Mike Stump4a3999f2009-09-09 13:00:44 +0000931 assert(!T.isNull() &&
Steve Naroff7cae42b2009-07-10 23:34:53 +0000932 "CodeGenFunction::EmitArraySubscriptExpr(): Illegal base type");
Mike Stump4a3999f2009-09-09 13:00:44 +0000933
Daniel Dunbaref2ffbc2009-04-25 05:08:32 +0000934 LValue LV = LValue::MakeAddr(Address,
Daniel Dunbar41595d42009-04-18 08:54:40 +0000935 T.getCVRQualifiers(),
Mon P Wangacedf772009-07-22 03:08:17 +0000936 getContext().getObjCGCAttrKind(T),
937 E->getBase()->getType().getAddressSpace());
Fariborz Jahaniana9fecf32009-02-21 23:37:19 +0000938 if (getContext().getLangOptions().ObjC1 &&
939 getContext().getLangOptions().getGCMode() != LangOptions::NonGC)
Fariborz Jahanianc6d98002009-06-01 21:29:32 +0000940 LValue::SetObjCNonGC(LV, !E->isOBJCGCCandidate(getContext()));
Fariborz Jahaniana9fecf32009-02-21 23:37:19 +0000941 return LV;
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000942}
943
Mike Stump4a3999f2009-09-09 13:00:44 +0000944static
Owen Anderson170229f2009-07-14 23:10:40 +0000945llvm::Constant *GenerateConstantVector(llvm::LLVMContext &VMContext,
946 llvm::SmallVector<unsigned, 4> &Elts) {
Nate Begemand3862152008-05-13 21:03:02 +0000947 llvm::SmallVector<llvm::Constant *, 4> CElts;
Mike Stump4a3999f2009-09-09 13:00:44 +0000948
Nate Begemand3862152008-05-13 21:03:02 +0000949 for (unsigned i = 0, e = Elts.size(); i != e; ++i)
Owen Anderson41a75022009-08-13 21:57:51 +0000950 CElts.push_back(llvm::ConstantInt::get(
951 llvm::Type::getInt32Ty(VMContext), Elts[i]));
Nate Begemand3862152008-05-13 21:03:02 +0000952
Owen Anderson3cc120a2009-07-28 21:22:35 +0000953 return llvm::ConstantVector::get(&CElts[0], CElts.size());
Nate Begemand3862152008-05-13 21:03:02 +0000954}
955
Chris Lattner9e751ca2007-08-02 23:37:31 +0000956LValue CodeGenFunction::
Nate Begemance4d7fc2008-04-18 23:10:10 +0000957EmitExtVectorElementExpr(const ExtVectorElementExpr *E) {
Chris Lattner9e751ca2007-08-02 23:37:31 +0000958 // Emit the base vector as an l-value.
Chris Lattner6c7ce102009-02-16 21:11:58 +0000959 LValue Base;
960
961 // ExtVectorElementExpr's base can either be a vector or pointer to vector.
Chris Lattnerb8211f62009-02-16 22:14:05 +0000962 if (!E->isArrow()) {
Chris Lattner6c7ce102009-02-16 21:11:58 +0000963 assert(E->getBase()->getType()->isVectorType());
964 Base = EmitLValue(E->getBase());
Chris Lattnerb8211f62009-02-16 22:14:05 +0000965 } else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000966 const PointerType *PT = E->getBase()->getType()->getAs<PointerType>();
Chris Lattnerb8211f62009-02-16 22:14:05 +0000967 llvm::Value *Ptr = EmitScalarExpr(E->getBase());
Mon P Wangacedf772009-07-22 03:08:17 +0000968 Base = LValue::MakeAddr(Ptr, PT->getPointeeType().getCVRQualifiers(),
969 QualType::GCNone,
970 PT->getPointeeType().getAddressSpace());
Chris Lattner6c7ce102009-02-16 21:11:58 +0000971 }
Chris Lattner9e751ca2007-08-02 23:37:31 +0000972
Nate Begemand3862152008-05-13 21:03:02 +0000973 // Encode the element access list into a vector of unsigned indices.
974 llvm::SmallVector<unsigned, 4> Indices;
975 E->getEncodedElementAccess(Indices);
976
977 if (Base.isSimple()) {
Owen Anderson170229f2009-07-14 23:10:40 +0000978 llvm::Constant *CV = GenerateConstantVector(VMContext, Indices);
Eli Friedman327944b2008-06-13 23:01:12 +0000979 return LValue::MakeExtVectorElt(Base.getAddress(), CV,
Chris Lattnere084c012009-02-16 22:25:49 +0000980 Base.getQualifiers());
Nate Begemand3862152008-05-13 21:03:02 +0000981 }
982 assert(Base.isExtVectorElt() && "Can only subscript lvalue vec elts here!");
983
984 llvm::Constant *BaseElts = Base.getExtVectorElts();
985 llvm::SmallVector<llvm::Constant *, 4> CElts;
986
987 for (unsigned i = 0, e = Indices.size(); i != e; ++i) {
988 if (isa<llvm::ConstantAggregateZero>(BaseElts))
Owen Anderson41a75022009-08-13 21:57:51 +0000989 CElts.push_back(llvm::ConstantInt::get(
990 llvm::Type::getInt32Ty(VMContext), 0));
Nate Begemand3862152008-05-13 21:03:02 +0000991 else
992 CElts.push_back(BaseElts->getOperand(Indices[i]));
993 }
Owen Anderson3cc120a2009-07-28 21:22:35 +0000994 llvm::Constant *CV = llvm::ConstantVector::get(&CElts[0], CElts.size());
Eli Friedman327944b2008-06-13 23:01:12 +0000995 return LValue::MakeExtVectorElt(Base.getExtVectorAddr(), CV,
Chris Lattnere084c012009-02-16 22:25:49 +0000996 Base.getQualifiers());
Chris Lattner9e751ca2007-08-02 23:37:31 +0000997}
998
Devang Patel30efa2e2007-10-23 20:28:39 +0000999LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) {
Devang Patelb37b12d2007-12-11 21:33:16 +00001000 bool isUnion = false;
Fariborz Jahanian735a4152008-11-21 18:14:01 +00001001 bool isIvar = false;
Fariborz Jahanian10bec102009-02-21 00:30:43 +00001002 bool isNonGC = false;
Devang Pateld68df202007-10-24 22:26:28 +00001003 Expr *BaseExpr = E->getBase();
Devang Pateld68df202007-10-24 22:26:28 +00001004 llvm::Value *BaseValue = NULL;
Eli Friedman327944b2008-06-13 23:01:12 +00001005 unsigned CVRQualifiers=0;
1006
Chris Lattner4e4186b2007-12-02 18:52:07 +00001007 // If this is s.x, emit s as an lvalue. If it is s->x, emit s as a scalar.
Devang Patelb37b12d2007-12-11 21:33:16 +00001008 if (E->isArrow()) {
Devang Patel7718d7a2007-10-26 18:15:21 +00001009 BaseValue = EmitScalarExpr(BaseExpr);
Mike Stump4a3999f2009-09-09 13:00:44 +00001010 const PointerType *PTy =
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001011 BaseExpr->getType()->getAs<PointerType>();
Devang Patelb37b12d2007-12-11 21:33:16 +00001012 if (PTy->getPointeeType()->isUnionType())
1013 isUnion = true;
Eli Friedman327944b2008-06-13 23:01:12 +00001014 CVRQualifiers = PTy->getPointeeType().getCVRQualifiers();
Fariborz Jahanian1a504772009-09-01 17:02:21 +00001015 } else if (isa<ObjCPropertyRefExpr>(BaseExpr->IgnoreParens()) ||
1016 isa<ObjCImplicitSetterGetterRefExpr>(
1017 BaseExpr->IgnoreParens())) {
Fariborz Jahanian30e78642009-01-12 23:27:26 +00001018 RValue RV = EmitObjCPropertyGet(BaseExpr);
1019 BaseValue = RV.getAggregateAddr();
1020 if (BaseExpr->getType()->isUnionType())
1021 isUnion = true;
1022 CVRQualifiers = BaseExpr->getType().getCVRQualifiers();
Chris Lattnere084c012009-02-16 22:25:49 +00001023 } else {
Chris Lattner4e4186b2007-12-02 18:52:07 +00001024 LValue BaseLV = EmitLValue(BaseExpr);
Fariborz Jahanian735a4152008-11-21 18:14:01 +00001025 if (BaseLV.isObjCIvar())
1026 isIvar = true;
Fariborz Jahanian10bec102009-02-21 00:30:43 +00001027 if (BaseLV.isNonGC())
1028 isNonGC = true;
Chris Lattner4e4186b2007-12-02 18:52:07 +00001029 // FIXME: this isn't right for bitfields.
1030 BaseValue = BaseLV.getAddress();
Fariborz Jahanian82e28742009-07-29 00:44:13 +00001031 QualType BaseTy = BaseExpr->getType();
1032 if (BaseTy->isUnionType())
Devang Patelb37b12d2007-12-11 21:33:16 +00001033 isUnion = true;
Fariborz Jahanian82e28742009-07-29 00:44:13 +00001034 CVRQualifiers = BaseTy.getCVRQualifiers();
Chris Lattner4e4186b2007-12-02 18:52:07 +00001035 }
Devang Patel30efa2e2007-10-23 20:28:39 +00001036
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001037 FieldDecl *Field = dyn_cast<FieldDecl>(E->getMemberDecl());
1038 // FIXME: Handle non-field member expressions
1039 assert(Field && "No code generation for non-field member references");
Chris Lattnere084c012009-02-16 22:25:49 +00001040 LValue MemExpLV = EmitLValueForField(BaseValue, Field, isUnion,
1041 CVRQualifiers);
Fariborz Jahanian735a4152008-11-21 18:14:01 +00001042 LValue::SetObjCIvar(MemExpLV, isIvar);
Fariborz Jahanian10bec102009-02-21 00:30:43 +00001043 LValue::SetObjCNonGC(MemExpLV, isNonGC);
Fariborz Jahanian735a4152008-11-21 18:14:01 +00001044 return MemExpLV;
Eli Friedmana62f3e12008-02-09 08:50:58 +00001045}
Devang Patel30efa2e2007-10-23 20:28:39 +00001046
Fariborz Jahanianb517e902008-12-15 20:35:07 +00001047LValue CodeGenFunction::EmitLValueForBitfield(llvm::Value* BaseValue,
1048 FieldDecl* Field,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001049 unsigned CVRQualifiers) {
Anders Carlsson8af896c2009-07-23 17:01:21 +00001050 CodeGenTypes::BitFieldInfo Info = CGM.getTypes().getBitFieldInfo(Field);
1051
Mike Stump18bb9282009-05-16 07:57:57 +00001052 // FIXME: CodeGenTypes should expose a method to get the appropriate type for
1053 // FieldTy (the appropriate type is ABI-dependent).
Mike Stump4a3999f2009-09-09 13:00:44 +00001054 const llvm::Type *FieldTy =
Daniel Dunbar3d926cb2009-02-17 18:31:04 +00001055 CGM.getTypes().ConvertTypeForMem(Field->getType());
Fariborz Jahanianb517e902008-12-15 20:35:07 +00001056 const llvm::PointerType *BaseTy =
1057 cast<llvm::PointerType>(BaseValue->getType());
1058 unsigned AS = BaseTy->getAddressSpace();
1059 BaseValue = Builder.CreateBitCast(BaseValue,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001060 llvm::PointerType::get(FieldTy, AS),
Fariborz Jahanianb517e902008-12-15 20:35:07 +00001061 "tmp");
Mike Stump4a3999f2009-09-09 13:00:44 +00001062
1063 llvm::Value *Idx =
Owen Anderson41a75022009-08-13 21:57:51 +00001064 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), Info.FieldNo);
Anders Carlsson8af896c2009-07-23 17:01:21 +00001065 llvm::Value *V = Builder.CreateGEP(BaseValue, Idx, "tmp");
Mike Stump4a3999f2009-09-09 13:00:44 +00001066
Anders Carlsson8af896c2009-07-23 17:01:21 +00001067 return LValue::MakeBitfield(V, Info.Start, Info.Size,
Fariborz Jahanianb517e902008-12-15 20:35:07 +00001068 Field->getType()->isSignedIntegerType(),
1069 Field->getType().getCVRQualifiers()|CVRQualifiers);
1070}
1071
Eli Friedmana62f3e12008-02-09 08:50:58 +00001072LValue CodeGenFunction::EmitLValueForField(llvm::Value* BaseValue,
1073 FieldDecl* Field,
Eli Friedman327944b2008-06-13 23:01:12 +00001074 bool isUnion,
Mike Stump11289f42009-09-09 15:08:12 +00001075 unsigned CVRQualifiers) {
Fariborz Jahanianb517e902008-12-15 20:35:07 +00001076 if (Field->isBitField())
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001077 return EmitLValueForBitfield(BaseValue, Field, CVRQualifiers);
Mike Stump4a3999f2009-09-09 13:00:44 +00001078
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001079 unsigned idx = CGM.getTypes().getLLVMFieldNo(Field);
Fariborz Jahanianb517e902008-12-15 20:35:07 +00001080 llvm::Value *V = Builder.CreateStructGEP(BaseValue, idx, "tmp");
Eli Friedman133e8042008-05-29 11:33:25 +00001081
Devang Pateled93c3c2007-10-26 19:42:18 +00001082 // Match union field type.
Lauro Ramos Venancio9eff02d2008-02-07 19:29:53 +00001083 if (isUnion) {
Mike Stump4a3999f2009-09-09 13:00:44 +00001084 const llvm::Type *FieldTy =
Eli Friedman327944b2008-06-13 23:01:12 +00001085 CGM.getTypes().ConvertTypeForMem(Field->getType());
Mike Stump4a3999f2009-09-09 13:00:44 +00001086 const llvm::PointerType * BaseTy =
Devang Patelffe1e212007-10-30 20:59:40 +00001087 cast<llvm::PointerType>(BaseValue->getType());
Eli Friedman9a5ffcb2008-05-21 13:24:44 +00001088 unsigned AS = BaseTy->getAddressSpace();
Mike Stump4a3999f2009-09-09 13:00:44 +00001089 V = Builder.CreateBitCast(V,
1090 llvm::PointerType::get(FieldTy, AS),
Eli Friedman9a5ffcb2008-05-21 13:24:44 +00001091 "tmp");
Devang Pateled93c3c2007-10-26 19:42:18 +00001092 }
Eli Friedmanf7f9f682009-05-30 21:09:44 +00001093 if (Field->getType()->isReferenceType())
1094 V = Builder.CreateLoad(V, "tmp");
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +00001095
Fariborz Jahanian0f466c72009-02-19 23:36:06 +00001096 QualType::GCAttrTypes attr = QualType::GCNone;
Fariborz Jahanian257eac62009-02-18 17:52:36 +00001097 if (CGM.getLangOptions().ObjC1 &&
Fariborz Jahanian9959eee2009-02-18 18:52:41 +00001098 CGM.getLangOptions().getGCMode() != LangOptions::NonGC) {
1099 QualType Ty = Field->getType();
Fariborz Jahanian0f466c72009-02-19 23:36:06 +00001100 attr = Ty.getObjCGCAttr();
Fariborz Jahanian83e3eea2009-02-19 00:48:05 +00001101 if (attr != QualType::GCNone) {
Fariborz Jahanian9959eee2009-02-18 18:52:41 +00001102 // __weak attribute on a field is ignored.
Fariborz Jahanian0f466c72009-02-19 23:36:06 +00001103 if (attr == QualType::Weak)
1104 attr = QualType::GCNone;
Mike Stump658fe022009-07-30 22:28:39 +00001105 } else if (Ty->isObjCObjectPointerType())
Fariborz Jahanian0f466c72009-02-19 23:36:06 +00001106 attr = QualType::Strong;
Fariborz Jahanian9959eee2009-02-18 18:52:41 +00001107 }
Mike Stump4a3999f2009-09-09 13:00:44 +00001108 LValue LV =
1109 LValue::MakeAddr(V,
Fariborz Jahanian0f466c72009-02-19 23:36:06 +00001110 Field->getType().getCVRQualifiers()|CVRQualifiers,
Mon P Wangacedf772009-07-22 03:08:17 +00001111 attr,
1112 Field->getType().getAddressSpace());
Fariborz Jahanian003e8302008-11-20 00:15:42 +00001113 return LV;
Devang Patel30efa2e2007-10-23 20:28:39 +00001114}
1115
Chris Lattner28bcf1a2009-03-18 18:28:57 +00001116LValue CodeGenFunction::EmitCompoundLiteralLValue(const CompoundLiteralExpr* E){
Eli Friedman9fd8b682008-05-13 23:18:27 +00001117 const llvm::Type *LTy = ConvertType(E->getType());
1118 llvm::Value *DeclPtr = CreateTempAlloca(LTy, ".compoundliteral");
1119
1120 const Expr* InitExpr = E->getInitializer();
Mon P Wangacedf772009-07-22 03:08:17 +00001121 LValue Result = LValue::MakeAddr(DeclPtr, E->getType().getCVRQualifiers(),
1122 QualType::GCNone,
1123 E->getType().getAddressSpace());
Eli Friedman9fd8b682008-05-13 23:18:27 +00001124
1125 if (E->getType()->isComplexType()) {
1126 EmitComplexExprIntoAddr(InitExpr, DeclPtr, false);
1127 } else if (hasAggregateLLVMType(E->getType())) {
1128 EmitAnyExpr(InitExpr, DeclPtr, false);
1129 } else {
1130 EmitStoreThroughLValue(EmitAnyExpr(InitExpr), Result, E->getType());
1131 }
1132
1133 return Result;
1134}
1135
Daniel Dunbarbf1fe8c2009-03-24 02:38:23 +00001136LValue CodeGenFunction::EmitConditionalOperator(const ConditionalOperator* E) {
Eli Friedmane8caa2a2009-07-30 01:10:26 +00001137 if (E->isLvalue(getContext()) == Expr::LV_Valid)
Daniel Dunbarbf1fe8c2009-03-24 02:38:23 +00001138 return EmitUnsupportedLValue(E, "conditional operator");
1139
1140 // ?: here should be an aggregate.
Mike Stump4a3999f2009-09-09 13:00:44 +00001141 assert((hasAggregateLLVMType(E->getType()) &&
Daniel Dunbarbf1fe8c2009-03-24 02:38:23 +00001142 !E->getType()->isAnyComplexType()) &&
1143 "Unexpected conditional operator!");
1144
1145 llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType()));
1146 EmitAggExpr(E, Temp, false);
1147
1148 return LValue::MakeAddr(Temp, E->getType().getCVRQualifiers(),
Mon P Wangacedf772009-07-22 03:08:17 +00001149 getContext().getObjCGCAttrKind(E->getType()),
1150 E->getType().getAddressSpace());
Mike Stump4a3999f2009-09-09 13:00:44 +00001151
Daniel Dunbarbf1fe8c2009-03-24 02:38:23 +00001152}
1153
Chris Lattner28bcf1a2009-03-18 18:28:57 +00001154/// EmitCastLValue - Casts are never lvalues. If a cast is needed by the code
1155/// generator in an lvalue context, then it must mean that we need the address
1156/// of an aggregate in order to access one of its fields. This can happen for
1157/// all the reasons that casts are permitted with aggregate result, including
1158/// noop aggregate casts, and cast from scalar to union.
1159LValue CodeGenFunction::EmitCastLValue(const CastExpr *E) {
Anders Carlssond95f9602009-09-12 16:16:49 +00001160 switch (E->getCastKind()) {
1161 default:
1162 // If this is an lvalue cast, treat it as a no-op.
1163 // FIXME: We shouldn't need to check for this explicitly!
1164 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
1165 if (ICE->isLvalueCast())
1166 return EmitLValue(E->getSubExpr());
1167
1168 assert(0 && "Unhandled cast!");
1169
1170 case CastExpr::CK_NoOp:
1171 case CastExpr::CK_ConstructorConversion:
1172 case CastExpr::CK_UserDefinedConversion:
Chris Lattner28bcf1a2009-03-18 18:28:57 +00001173 return EmitLValue(E->getSubExpr());
Anders Carlssond95f9602009-09-12 16:16:49 +00001174
1175 case CastExpr::CK_DerivedToBase: {
1176 const RecordType *DerivedClassTy =
1177 E->getSubExpr()->getType()->getAs<RecordType>();
1178 CXXRecordDecl *DerivedClassDecl =
1179 cast<CXXRecordDecl>(DerivedClassTy->getDecl());
Chris Lattner28bcf1a2009-03-18 18:28:57 +00001180
Anders Carlssond95f9602009-09-12 16:16:49 +00001181 const RecordType *BaseClassTy = E->getType()->getAs<RecordType>();
1182 CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseClassTy->getDecl());
1183
1184 LValue LV = EmitLValue(E->getSubExpr());
1185
1186 // Perform the derived-to-base conversion
1187 llvm::Value *Base =
1188 GetAddressCXXOfBaseClass(LV.getAddress(), DerivedClassDecl,
1189 BaseClassDecl, /*NullCheckValue=*/false);
1190
1191 return LValue::MakeAddr(Base, E->getType().getCVRQualifiers(),
1192 getContext().getObjCGCAttrKind(E->getType()),
1193 E->getType().getAddressSpace());
1194 }
Eli Friedmanff083ef2009-08-27 21:19:33 +00001195
Anders Carlssond95f9602009-09-12 16:16:49 +00001196 case CastExpr::CK_ToUnion: {
1197 llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType()));
1198 EmitAnyExpr(E->getSubExpr(), Temp, false);
Mike Stump4a3999f2009-09-09 13:00:44 +00001199
Anders Carlssond95f9602009-09-12 16:16:49 +00001200 return LValue::MakeAddr(Temp, E->getType().getCVRQualifiers(),
1201 getContext().getObjCGCAttrKind(E->getType()),
1202 E->getType().getAddressSpace());
1203 }
1204 }
Chris Lattner28bcf1a2009-03-18 18:28:57 +00001205}
1206
Chris Lattnere47e4402007-06-01 18:02:12 +00001207//===--------------------------------------------------------------------===//
1208// Expression Emission
1209//===--------------------------------------------------------------------===//
1210
Chris Lattner76ba8492007-08-20 22:37:10 +00001211
Chris Lattner2b228c92007-06-15 21:34:29 +00001212RValue CodeGenFunction::EmitCallExpr(const CallExpr *E) {
Daniel Dunbarcdbb5e32009-02-20 18:06:48 +00001213 // Builtins never have block type.
Daniel Dunbarbb197e42009-01-09 16:50:52 +00001214 if (E->getCallee()->getType()->isBlockPointerType())
Anders Carlsson2437cbf2009-02-12 00:39:25 +00001215 return EmitBlockCallExpr(E);
Daniel Dunbarbb197e42009-01-09 16:50:52 +00001216
Anders Carlssone5fd6f22009-04-03 22:50:24 +00001217 if (const CXXMemberCallExpr *CE = dyn_cast<CXXMemberCallExpr>(E))
1218 return EmitCXXMemberCallExpr(CE);
Mike Stump4a3999f2009-09-09 13:00:44 +00001219
Daniel Dunbarcdbb5e32009-02-20 18:06:48 +00001220 const Decl *TargetDecl = 0;
Daniel Dunbar27032de2009-02-20 19:34:33 +00001221 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E->getCallee())) {
1222 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CE->getSubExpr())) {
1223 TargetDecl = DRE->getDecl();
1224 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(TargetDecl))
Douglas Gregor15fc9562009-09-12 00:22:50 +00001225 if (unsigned builtinID = FD->getBuiltinID())
Daniel Dunbar27032de2009-02-20 19:34:33 +00001226 return EmitBuiltinExpr(FD, builtinID, E);
Daniel Dunbarcdbb5e32009-02-20 18:06:48 +00001227 }
1228 }
1229
Chris Lattner4ca97c32009-06-13 00:26:38 +00001230 if (const CXXOperatorCallExpr *CE = dyn_cast<CXXOperatorCallExpr>(E))
Anders Carlsson4034a952009-05-27 04:18:27 +00001231 if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(TargetDecl))
1232 return EmitCXXOperatorMemberCallExpr(CE, MD);
Mike Stump4a3999f2009-09-09 13:00:44 +00001233
Douglas Gregorad8a3362009-09-04 17:36:40 +00001234 if (isa<CXXPseudoDestructorExpr>(E->getCallee())) {
1235 // C++ [expr.pseudo]p1:
Mike Stump4a3999f2009-09-09 13:00:44 +00001236 // The result shall only be used as the operand for the function call
Douglas Gregorad8a3362009-09-04 17:36:40 +00001237 // operator (), and the result of such a call has type void. The only
1238 // effect is the evaluation of the postfix-expression before the dot or
1239 // arrow.
1240 EmitScalarExpr(E->getCallee());
1241 return RValue::get(0);
1242 }
Mike Stump4a3999f2009-09-09 13:00:44 +00001243
Chris Lattner2da04b32007-08-24 05:35:26 +00001244 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
Anders Carlsson3a9463b2009-05-27 01:22:39 +00001245 return EmitCall(Callee, E->getCallee()->getType(),
1246 E->arg_begin(), E->arg_end(), TargetDecl);
Chris Lattner9e47ead2007-08-31 04:44:06 +00001247}
1248
Daniel Dunbar8cde00a2008-09-04 03:20:13 +00001249LValue CodeGenFunction::EmitBinaryOperatorLValue(const BinaryOperator *E) {
Chris Lattnere541ea32009-05-12 21:28:12 +00001250 // Comma expressions just emit their LHS then their RHS as an l-value.
1251 if (E->getOpcode() == BinaryOperator::Comma) {
1252 EmitAnyExpr(E->getLHS());
1253 return EmitLValue(E->getRHS());
1254 }
Mike Stump4a3999f2009-09-09 13:00:44 +00001255
Daniel Dunbar8cde00a2008-09-04 03:20:13 +00001256 // Can only get l-value for binary operator expressions which are a
1257 // simple assignment of aggregate type.
1258 if (E->getOpcode() != BinaryOperator::Assign)
1259 return EmitUnsupportedLValue(E, "binary l-value expression");
1260
1261 llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType()));
1262 EmitAggExpr(E, Temp, false);
1263 // FIXME: Are these qualifiers correct?
Fariborz Jahanian0f466c72009-02-19 23:36:06 +00001264 return LValue::MakeAddr(Temp, E->getType().getCVRQualifiers(),
Mon P Wangacedf772009-07-22 03:08:17 +00001265 getContext().getObjCGCAttrKind(E->getType()),
1266 E->getType().getAddressSpace());
Daniel Dunbar8cde00a2008-09-04 03:20:13 +00001267}
1268
Christopher Lambd91c3d42007-12-29 05:02:41 +00001269LValue CodeGenFunction::EmitCallExprLValue(const CallExpr *E) {
Christopher Lambd91c3d42007-12-29 05:02:41 +00001270 RValue RV = EmitCallExpr(E);
Anders Carlsson4ae70ff2009-05-27 01:45:47 +00001271
1272 if (RV.isScalar()) {
1273 assert(E->getCallReturnType()->isReferenceType() &&
1274 "Can't have a scalar return unless the return type is a "
1275 "reference type!");
Mike Stump4a3999f2009-09-09 13:00:44 +00001276
1277 return LValue::MakeAddr(RV.getScalarVal(), E->getType().getCVRQualifiers(),
Mon P Wangacedf772009-07-22 03:08:17 +00001278 getContext().getObjCGCAttrKind(E->getType()),
1279 E->getType().getAddressSpace());
Anders Carlsson4ae70ff2009-05-27 01:45:47 +00001280 }
Mike Stump4a3999f2009-09-09 13:00:44 +00001281
Eli Friedman327944b2008-06-13 23:01:12 +00001282 return LValue::MakeAddr(RV.getAggregateAddr(),
Fariborz Jahanian0f466c72009-02-19 23:36:06 +00001283 E->getType().getCVRQualifiers(),
Mon P Wangacedf772009-07-22 03:08:17 +00001284 getContext().getObjCGCAttrKind(E->getType()),
1285 E->getType().getAddressSpace());
Christopher Lambd91c3d42007-12-29 05:02:41 +00001286}
1287
Daniel Dunbar8d9dc4a2009-02-11 20:59:32 +00001288LValue CodeGenFunction::EmitVAArgExprLValue(const VAArgExpr *E) {
1289 // FIXME: This shouldn't require another copy.
1290 llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType()));
1291 EmitAggExpr(E, Temp, false);
Mon P Wangacedf772009-07-22 03:08:17 +00001292 return LValue::MakeAddr(Temp, E->getType().getCVRQualifiers(),
1293 QualType::GCNone, E->getType().getAddressSpace());
Daniel Dunbar8d9dc4a2009-02-11 20:59:32 +00001294}
1295
Argyrios Kyrtzidis07052352008-09-10 02:36:38 +00001296LValue
1297CodeGenFunction::EmitCXXConditionDeclLValue(const CXXConditionDeclExpr *E) {
1298 EmitLocalBlockVarDecl(*E->getVarDecl());
1299 return EmitDeclRefLValue(E);
1300}
1301
Anders Carlsson3be22e22009-05-30 23:23:33 +00001302LValue CodeGenFunction::EmitCXXConstructLValue(const CXXConstructExpr *E) {
1303 llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(E->getType()), "tmp");
1304 EmitCXXConstructExpr(Temp, E);
Mon P Wangacedf772009-07-22 03:08:17 +00001305 return LValue::MakeAddr(Temp, E->getType().getCVRQualifiers(),
1306 QualType::GCNone, E->getType().getAddressSpace());
Anders Carlsson3be22e22009-05-30 23:23:33 +00001307}
1308
Anders Carlssonfd2af0c2009-05-30 23:30:54 +00001309LValue
1310CodeGenFunction::EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr *E) {
1311 LValue LV = EmitLValue(E->getSubExpr());
Mike Stump4a3999f2009-09-09 13:00:44 +00001312
Anders Carlsson8eb93e72009-05-31 00:34:10 +00001313 PushCXXTemporary(E->getTemporary(), LV.getAddress());
Mike Stump4a3999f2009-09-09 13:00:44 +00001314
Anders Carlssonfd2af0c2009-05-30 23:30:54 +00001315 return LV;
1316}
1317
Daniel Dunbarc8317a42008-08-23 10:51:21 +00001318LValue CodeGenFunction::EmitObjCMessageExprLValue(const ObjCMessageExpr *E) {
1319 // Can only get l-value for message expression returning aggregate type
1320 RValue RV = EmitObjCMessageExpr(E);
1321 // FIXME: can this be volatile?
1322 return LValue::MakeAddr(RV.getAggregateAddr(),
Fariborz Jahanian0f466c72009-02-19 23:36:06 +00001323 E->getType().getCVRQualifiers(),
Mon P Wangacedf772009-07-22 03:08:17 +00001324 getContext().getObjCGCAttrKind(E->getType()),
1325 E->getType().getAddressSpace());
Daniel Dunbarc8317a42008-08-23 10:51:21 +00001326}
1327
Daniel Dunbar722f4242009-04-22 05:08:15 +00001328llvm::Value *CodeGenFunction::EmitIvarOffset(const ObjCInterfaceDecl *Interface,
Daniel Dunbar1c64e5d2008-09-24 04:00:38 +00001329 const ObjCIvarDecl *Ivar) {
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001330 return CGM.getObjCRuntime().EmitIvarOffset(*this, Interface, Ivar);
Daniel Dunbar1c64e5d2008-09-24 04:00:38 +00001331}
1332
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00001333LValue CodeGenFunction::EmitLValueForIvar(QualType ObjectTy,
1334 llvm::Value *BaseValue,
Daniel Dunbar1c64e5d2008-09-24 04:00:38 +00001335 const ObjCIvarDecl *Ivar,
1336 unsigned CVRQualifiers) {
Chris Lattnerc4688d22009-04-17 17:44:48 +00001337 return CGM.getObjCRuntime().EmitObjCValueForIvar(*this, ObjectTy, BaseValue,
Daniel Dunbar9ebf9512009-04-21 01:19:28 +00001338 Ivar, CVRQualifiers);
Daniel Dunbar1c64e5d2008-09-24 04:00:38 +00001339}
1340
1341LValue CodeGenFunction::EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E) {
Anders Carlssonc13b85a2008-08-25 01:53:23 +00001342 // FIXME: A lot of the code below could be shared with EmitMemberExpr.
1343 llvm::Value *BaseValue = 0;
1344 const Expr *BaseExpr = E->getBase();
1345 unsigned CVRQualifiers = 0;
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00001346 QualType ObjectTy;
Anders Carlssonc13b85a2008-08-25 01:53:23 +00001347 if (E->isArrow()) {
1348 BaseValue = EmitScalarExpr(BaseExpr);
Steve Naroff7cae42b2009-07-10 23:34:53 +00001349 ObjectTy = BaseExpr->getType()->getPointeeType();
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00001350 CVRQualifiers = ObjectTy.getCVRQualifiers();
Anders Carlssonc13b85a2008-08-25 01:53:23 +00001351 } else {
1352 LValue BaseLV = EmitLValue(BaseExpr);
1353 // FIXME: this isn't right for bitfields.
1354 BaseValue = BaseLV.getAddress();
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00001355 ObjectTy = BaseExpr->getType();
1356 CVRQualifiers = ObjectTy.getCVRQualifiers();
Anders Carlssonc13b85a2008-08-25 01:53:23 +00001357 }
Daniel Dunbar1c64e5d2008-09-24 04:00:38 +00001358
Daniel Dunbar9ebf9512009-04-21 01:19:28 +00001359 return EmitLValueForIvar(ObjectTy, BaseValue, E->getDecl(), CVRQualifiers);
Chris Lattner4bd55962008-03-30 23:03:07 +00001360}
1361
Mike Stump4a3999f2009-09-09 13:00:44 +00001362LValue
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +00001363CodeGenFunction::EmitObjCPropertyRefLValue(const ObjCPropertyRefExpr *E) {
Mike Stump4a3999f2009-09-09 13:00:44 +00001364 // This is a special l-value that just issues sends when we load or store
1365 // through it.
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +00001366 return LValue::MakePropertyRef(E, E->getType().getCVRQualifiers());
1367}
1368
Mike Stump4a3999f2009-09-09 13:00:44 +00001369LValue
Fariborz Jahanian88cc2342009-08-18 20:50:23 +00001370CodeGenFunction::EmitObjCKVCRefLValue(
Fariborz Jahanian9a846652009-08-20 17:02:02 +00001371 const ObjCImplicitSetterGetterRefExpr *E) {
Mike Stump4a3999f2009-09-09 13:00:44 +00001372 // This is a special l-value that just issues sends when we load or store
1373 // through it.
Fariborz Jahanian9ac53512008-11-22 22:30:21 +00001374 return LValue::MakeKVCRef(E, E->getType().getCVRQualifiers());
1375}
1376
Douglas Gregor8ea1f532008-11-04 14:56:14 +00001377LValue
Chris Lattnera4185c52009-04-25 19:35:26 +00001378CodeGenFunction::EmitObjCSuperExprLValue(const ObjCSuperExpr *E) {
Douglas Gregor8ea1f532008-11-04 14:56:14 +00001379 return EmitUnsupportedLValue(E, "use of super");
1380}
1381
Chris Lattnera4185c52009-04-25 19:35:26 +00001382LValue CodeGenFunction::EmitStmtExprLValue(const StmtExpr *E) {
Mike Stump4a3999f2009-09-09 13:00:44 +00001383
Chris Lattnera4185c52009-04-25 19:35:26 +00001384 // Can only get l-value for message expression returning aggregate type
1385 RValue RV = EmitAnyExprToTemp(E);
1386 // FIXME: can this be volatile?
1387 return LValue::MakeAddr(RV.getAggregateAddr(),
1388 E->getType().getCVRQualifiers(),
Mon P Wangacedf772009-07-22 03:08:17 +00001389 getContext().getObjCGCAttrKind(E->getType()),
1390 E->getType().getAddressSpace());
Chris Lattnera4185c52009-04-25 19:35:26 +00001391}
1392
1393
Mike Stump4a3999f2009-09-09 13:00:44 +00001394RValue CodeGenFunction::EmitCall(llvm::Value *Callee, QualType CalleeType,
Anders Carlsson3a9463b2009-05-27 01:22:39 +00001395 CallExpr::const_arg_iterator ArgBeg,
1396 CallExpr::const_arg_iterator ArgEnd,
1397 const Decl *TargetDecl) {
Mike Stump4a3999f2009-09-09 13:00:44 +00001398 // Get the actual function type. The callee type will always be a pointer to
1399 // function type or a block pointer type.
1400 assert(CalleeType->isFunctionPointerType() &&
Anders Carlssond8db8532009-04-07 18:53:02 +00001401 "Call must have function pointer type!");
1402
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001403 QualType FnType = CalleeType->getAs<PointerType>()->getPointeeType();
Anders Carlssond8db8532009-04-07 18:53:02 +00001404 QualType ResultType = FnType->getAsFunctionType()->getResultType();
Daniel Dunbarc722b852008-08-30 03:02:31 +00001405
1406 CallArgList Args;
Anders Carlsson479e6fc2009-04-08 23:13:16 +00001407 EmitCallArgs(Args, FnType->getAsFunctionProtoType(), ArgBeg, ArgEnd);
Daniel Dunbarc722b852008-08-30 03:02:31 +00001408
Daniel Dunbarbbaeca42009-09-11 22:25:00 +00001409 // FIXME: We should not need to do this, it should be part of the function
1410 // type.
1411 unsigned CallingConvention = 0;
1412 if (const llvm::Function *F =
1413 dyn_cast<llvm::Function>(Callee->stripPointerCasts()))
1414 CallingConvention = F->getCallingConv();
1415 return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args,
1416 CallingConvention),
Daniel Dunbarcdbb5e32009-02-20 18:06:48 +00001417 Callee, Args, TargetDecl);
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001418}