blob: d34b0f5ea38bf114a4cdd9c43c60f3997b7485a3 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- CGExpr.cpp - Emit LLVM Code from Expressions ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-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 Lattner4b009652007-07-25 00:24:17 +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 Dunbara8f02052008-09-08 21:33:45 +000016#include "CGCall.h"
Daniel Dunbar84bb85f2008-08-13 00:59:25 +000017#include "CGObjCRuntime.h"
Daniel Dunbareee5cd12008-08-11 05:00:27 +000018#include "clang/AST/ASTContext.h"
Daniel Dunbar64789f82008-08-11 05:35:13 +000019#include "clang/AST/DeclObjC.h"
Eli Friedmana04e70d2008-05-17 20:03:47 +000020#include "llvm/Target/TargetData.h"
Chris Lattner4b009652007-07-25 00:24:17 +000021using namespace clang;
22using namespace CodeGen;
23
24//===--------------------------------------------------------------------===//
25// Miscellaneous Helper Methods
26//===--------------------------------------------------------------------===//
27
28/// 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 Lattner535998b2009-03-21 07:48:31 +000032 // FIXME: Should not pass name if names are disabled in IRBuilder.
Chris Lattner4b009652007-07-25 00:24:17 +000033 return new llvm::AllocaInst(Ty, 0, Name, AllocaInsertPt);
34}
35
36/// EvaluateExprAsBool - Perform the usual unary conversions on the specified
37/// expression and compare the result against zero, returning an Int1Ty value.
38llvm::Value *CodeGenFunction::EvaluateExprAsBool(const Expr *E) {
Chris Lattnercc50a512007-08-26 16:46:58 +000039 QualType BoolTy = getContext().BoolTy;
Chris Lattnerde0908b2008-04-04 16:54:41 +000040 if (!E->getType()->isAnyComplexType())
Chris Lattnercc50a512007-08-26 16:46:58 +000041 return EmitScalarConversion(EmitScalarExpr(E), E->getType(), BoolTy);
Chris Lattner4b009652007-07-25 00:24:17 +000042
Chris Lattnercc50a512007-08-26 16:46:58 +000043 return EmitComplexToScalarConversion(EmitComplexExpr(E), E->getType(),BoolTy);
Chris Lattner4b009652007-07-25 00:24:17 +000044}
45
Chris Lattnere24c4cf2007-08-31 22:49:20 +000046/// EmitAnyExpr - Emit code to compute the specified expression which can have
47/// any type. The result is returned as an RValue struct. If this is an
48/// aggregate expression, the aggloc/agglocvolatile arguments indicate where
49/// the result should be returned.
50RValue CodeGenFunction::EmitAnyExpr(const Expr *E, llvm::Value *AggLoc,
51 bool isAggLocVolatile) {
52 if (!hasAggregateLLVMType(E->getType()))
53 return RValue::get(EmitScalarExpr(E));
Chris Lattnerde0908b2008-04-04 16:54:41 +000054 else if (E->getType()->isAnyComplexType())
Chris Lattnere24c4cf2007-08-31 22:49:20 +000055 return RValue::getComplex(EmitComplexExpr(E));
56
57 EmitAggExpr(E, AggLoc, isAggLocVolatile);
58 return RValue::getAggregate(AggLoc);
59}
60
Daniel Dunbar0a2da0f2008-09-09 01:06:48 +000061/// EmitAnyExprToTemp - Similary to EmitAnyExpr(), however, the result
62/// will always be accessible even if no aggregate location is
63/// provided.
64RValue CodeGenFunction::EmitAnyExprToTemp(const Expr *E, llvm::Value *AggLoc,
65 bool isAggLocVolatile) {
66 if (!AggLoc && hasAggregateLLVMType(E->getType()) &&
67 !E->getType()->isAnyComplexType())
68 AggLoc = CreateTempAlloca(ConvertType(E->getType()), "agg.tmp");
69 return EmitAnyExpr(E, AggLoc, isAggLocVolatile);
70}
71
Dan Gohman4751a3a2008-05-22 00:50:06 +000072/// getAccessedFieldNo - Given an encoded value and a result number, return
73/// the input field number being accessed.
74unsigned CodeGenFunction::getAccessedFieldNo(unsigned Idx,
75 const llvm::Constant *Elts) {
76 if (isa<llvm::ConstantAggregateZero>(Elts))
77 return 0;
78
79 return cast<llvm::ConstantInt>(Elts->getOperand(Idx))->getZExtValue();
80}
81
Chris Lattnere24c4cf2007-08-31 22:49:20 +000082
Chris Lattner4b009652007-07-25 00:24:17 +000083//===----------------------------------------------------------------------===//
84// LValue Expression Emission
85//===----------------------------------------------------------------------===//
86
Daniel Dunbar900c85a2009-02-05 07:09:07 +000087RValue CodeGenFunction::GetUndefRValue(QualType Ty) {
88 if (Ty->isVoidType()) {
89 return RValue::get(0);
90 } else if (const ComplexType *CTy = Ty->getAsComplexType()) {
Daniel Dunbar8cb73402009-01-09 20:09:28 +000091 const llvm::Type *EltTy = ConvertType(CTy->getElementType());
92 llvm::Value *U = llvm::UndefValue::get(EltTy);
93 return RValue::getComplex(std::make_pair(U, U));
Daniel Dunbar900c85a2009-02-05 07:09:07 +000094 } else if (hasAggregateLLVMType(Ty)) {
95 const llvm::Type *LTy = llvm::PointerType::getUnqual(ConvertType(Ty));
96 return RValue::getAggregate(llvm::UndefValue::get(LTy));
Daniel Dunbar8cb73402009-01-09 20:09:28 +000097 } else {
Daniel Dunbar900c85a2009-02-05 07:09:07 +000098 return RValue::get(llvm::UndefValue::get(ConvertType(Ty)));
Daniel Dunbar8cb73402009-01-09 20:09:28 +000099 }
Daniel Dunbare3a6a682009-01-09 16:50:52 +0000100}
101
Daniel Dunbar900c85a2009-02-05 07:09:07 +0000102RValue CodeGenFunction::EmitUnsupportedRValue(const Expr *E,
103 const char *Name) {
104 ErrorUnsupported(E, Name);
105 return GetUndefRValue(E->getType());
106}
107
Daniel Dunbarde1bd942008-08-25 20:45:57 +0000108LValue CodeGenFunction::EmitUnsupportedLValue(const Expr *E,
109 const char *Name) {
110 ErrorUnsupported(E, Name);
111 llvm::Type *Ty = llvm::PointerType::getUnqual(ConvertType(E->getType()));
112 return LValue::MakeAddr(llvm::UndefValue::get(Ty),
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +0000113 E->getType().getCVRQualifiers(),
114 getContext().getObjCGCAttrKind(E->getType()));
Daniel Dunbarde1bd942008-08-25 20:45:57 +0000115}
116
Chris Lattner4b009652007-07-25 00:24:17 +0000117/// EmitLValue - Emit code to compute a designator that specifies the location
118/// of the expression.
119///
120/// This can return one of two things: a simple address or a bitfield
121/// reference. In either case, the LLVM Value* in the LValue structure is
122/// guaranteed to be an LLVM pointer type.
123///
124/// If this returns a bitfield reference, nothing about the pointee type of
125/// the LLVM value is known: For example, it may not be a pointer to an
126/// integer.
127///
128/// If this returns a normal address, and if the lvalue's C type is fixed
129/// size, this method guarantees that the returned pointer type will point to
130/// an LLVM type of the same size of the lvalue's type. If the lvalue has a
131/// variable length type, this is not possible.
132///
133LValue CodeGenFunction::EmitLValue(const Expr *E) {
134 switch (E->getStmtClass()) {
Daniel Dunbarde1bd942008-08-25 20:45:57 +0000135 default: return EmitUnsupportedLValue(E, "l-value expression");
Chris Lattner4b009652007-07-25 00:24:17 +0000136
Daniel Dunbaref0d4c72008-09-04 03:20:13 +0000137 case Expr::BinaryOperatorClass:
138 return EmitBinaryOperatorLValue(cast<BinaryOperator>(E));
Douglas Gregor65fedaf2008-11-14 16:09:21 +0000139 case Expr::CallExprClass:
140 case Expr::CXXOperatorCallExprClass:
141 return EmitCallExprLValue(cast<CallExpr>(E));
Daniel Dunbar95d08f22009-02-11 20:59:32 +0000142 case Expr::VAArgExprClass:
143 return EmitVAArgExprLValue(cast<VAArgExpr>(E));
Douglas Gregor566782a2009-01-06 05:10:23 +0000144 case Expr::DeclRefExprClass:
145 case Expr::QualifiedDeclRefExprClass:
146 return EmitDeclRefLValue(cast<DeclRefExpr>(E));
Chris Lattner4b009652007-07-25 00:24:17 +0000147 case Expr::ParenExprClass:return EmitLValue(cast<ParenExpr>(E)->getSubExpr());
Chris Lattner69909292008-08-10 01:53:14 +0000148 case Expr::PredefinedExprClass:
149 return EmitPredefinedLValue(cast<PredefinedExpr>(E));
Chris Lattner4b009652007-07-25 00:24:17 +0000150 case Expr::StringLiteralClass:
151 return EmitStringLiteralLValue(cast<StringLiteral>(E));
Chris Lattnerc5d32632009-02-24 22:18:39 +0000152 case Expr::ObjCEncodeExprClass:
153 return EmitObjCEncodeExprLValue(cast<ObjCEncodeExpr>(E));
Chris Lattnerb326b172008-03-30 23:03:07 +0000154
Mike Stump2b6933f2009-02-28 09:07:16 +0000155 case Expr::BlockDeclRefExprClass:
156 return EmitBlockDeclRefLValue(cast<BlockDeclRefExpr>(E));
157
Argiris Kirtzidisbf615b02008-09-10 02:36:38 +0000158 case Expr::CXXConditionDeclExprClass:
159 return EmitCXXConditionDeclLValue(cast<CXXConditionDeclExpr>(E));
160
Daniel Dunbar5e105892008-08-23 10:51:21 +0000161 case Expr::ObjCMessageExprClass:
162 return EmitObjCMessageExprLValue(cast<ObjCMessageExpr>(E));
Chris Lattnerb326b172008-03-30 23:03:07 +0000163 case Expr::ObjCIvarRefExprClass:
164 return EmitObjCIvarRefLValue(cast<ObjCIvarRefExpr>(E));
Daniel Dunbarde1bd942008-08-25 20:45:57 +0000165 case Expr::ObjCPropertyRefExprClass:
Daniel Dunbare6c31752008-08-29 08:11:39 +0000166 return EmitObjCPropertyRefLValue(cast<ObjCPropertyRefExpr>(E));
Fariborz Jahanianb0973da2008-11-22 22:30:21 +0000167 case Expr::ObjCKVCRefExprClass:
168 return EmitObjCKVCRefLValue(cast<ObjCKVCRefExpr>(E));
Douglas Gregord8606632008-11-04 14:56:14 +0000169 case Expr::ObjCSuperExprClass:
170 return EmitObjCSuperExpr(cast<ObjCSuperExpr>(E));
171
Chris Lattner4b009652007-07-25 00:24:17 +0000172 case Expr::UnaryOperatorClass:
173 return EmitUnaryOpLValue(cast<UnaryOperator>(E));
174 case Expr::ArraySubscriptExprClass:
175 return EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E));
Nate Begemanaf6ed502008-04-18 23:10:10 +0000176 case Expr::ExtVectorElementExprClass:
177 return EmitExtVectorElementExpr(cast<ExtVectorElementExpr>(E));
Devang Patel41b66252007-10-23 20:28:39 +0000178 case Expr::MemberExprClass: return EmitMemberExpr(cast<MemberExpr>(E));
Eli Friedmanf3c2cb42008-05-13 23:18:27 +0000179 case Expr::CompoundLiteralExprClass:
180 return EmitCompoundLiteralLValue(cast<CompoundLiteralExpr>(E));
Chris Lattnera5f779dc2008-12-12 05:35:08 +0000181 case Expr::ChooseExprClass:
Eli Friedmand540c112009-03-04 05:52:32 +0000182 return EmitLValue(cast<ChooseExpr>(E)->getChosenSubExpr(getContext()));
Chris Lattner504239f2009-03-18 04:02:57 +0000183 case Expr::ImplicitCastExprClass:
184 case Expr::CStyleCastExprClass:
185 case Expr::CXXFunctionalCastExprClass:
186 case Expr::CXXStaticCastExprClass:
187 case Expr::CXXDynamicCastExprClass:
188 case Expr::CXXReinterpretCastExprClass:
189 case Expr::CXXConstCastExprClass:
Chris Lattner22523ba2009-03-18 18:28:57 +0000190 return EmitCastLValue(cast<CastExpr>(E));
Chris Lattner4b009652007-07-25 00:24:17 +0000191 }
192}
193
Daniel Dunbarf1c5d842009-02-10 00:57:50 +0000194llvm::Value *CodeGenFunction::EmitLoadOfScalar(llvm::Value *Addr, bool Volatile,
195 QualType Ty) {
196 llvm::Value *V = Builder.CreateLoad(Addr, Volatile, "tmp");
197
198 // Bool can have different representation in memory than in
199 // registers.
200 if (Ty->isBooleanType())
201 if (V->getType() != llvm::Type::Int1Ty)
202 V = Builder.CreateTrunc(V, llvm::Type::Int1Ty, "tobool");
203
204 return V;
205}
206
207void CodeGenFunction::EmitStoreOfScalar(llvm::Value *Value, llvm::Value *Addr,
208 bool Volatile) {
209 // Handle stores of types which have different representations in
210 // memory and as LLVM values.
211
212 // FIXME: We shouldn't be this loose, we should only do this
213 // conversion when we have a type we know has a different memory
214 // representation (e.g., bool).
215
216 const llvm::Type *SrcTy = Value->getType();
217 const llvm::PointerType *DstPtr = cast<llvm::PointerType>(Addr->getType());
218 if (DstPtr->getElementType() != SrcTy) {
219 const llvm::Type *MemTy =
220 llvm::PointerType::get(SrcTy, DstPtr->getAddressSpace());
221 Addr = Builder.CreateBitCast(Addr, MemTy, "storetmp");
222 }
223
224 Builder.CreateStore(Value, Addr, Volatile);
225}
226
Chris Lattner4b009652007-07-25 00:24:17 +0000227/// EmitLoadOfLValue - Given an expression that represents a value lvalue,
228/// this method emits the address of the lvalue, then loads the result as an
229/// rvalue, returning the rvalue.
230RValue CodeGenFunction::EmitLoadOfLValue(LValue LV, QualType ExprType) {
Fariborz Jahaniand2f661a2008-11-19 17:34:06 +0000231 if (LV.isObjCWeak()) {
Fariborz Jahanian3305ad32008-11-18 21:45:40 +0000232 // load of a __weak object.
233 llvm::Value *AddrWeakObj = LV.getAddress();
Fariborz Jahanian252d87f2008-11-18 22:37:34 +0000234 llvm::Value *read_weak = CGM.getObjCRuntime().EmitObjCWeakRead(*this,
Fariborz Jahanian3305ad32008-11-18 21:45:40 +0000235 AddrWeakObj);
236 return RValue::get(read_weak);
237 }
238
Chris Lattner4b009652007-07-25 00:24:17 +0000239 if (LV.isSimple()) {
240 llvm::Value *Ptr = LV.getAddress();
241 const llvm::Type *EltTy =
242 cast<llvm::PointerType>(Ptr->getType())->getElementType();
243
244 // Simple scalar l-value.
Daniel Dunbarf1c5d842009-02-10 00:57:50 +0000245 if (EltTy->isSingleValueType())
246 return RValue::get(EmitLoadOfScalar(Ptr, LV.isVolatileQualified(),
247 ExprType));
Chris Lattner4b009652007-07-25 00:24:17 +0000248
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000249 assert(ExprType->isFunctionType() && "Unknown scalar value");
250 return RValue::get(Ptr);
Chris Lattner4b009652007-07-25 00:24:17 +0000251 }
252
253 if (LV.isVectorElt()) {
Eli Friedman2e630542008-06-13 23:01:12 +0000254 llvm::Value *Vec = Builder.CreateLoad(LV.getVectorAddr(),
255 LV.isVolatileQualified(), "tmp");
Chris Lattner4b009652007-07-25 00:24:17 +0000256 return RValue::get(Builder.CreateExtractElement(Vec, LV.getVectorIdx(),
257 "vecext"));
258 }
Chris Lattnera735fac2007-08-03 00:16:29 +0000259
260 // If this is a reference to a subset of the elements of a vector, either
261 // shuffle the input or extract/insert them as appropriate.
Nate Begemanaf6ed502008-04-18 23:10:10 +0000262 if (LV.isExtVectorElt())
263 return EmitLoadOfExtVectorElementLValue(LV, ExprType);
Lauro Ramos Venanciob40307c2008-01-22 20:17:04 +0000264
265 if (LV.isBitfield())
266 return EmitLoadOfBitfieldLValue(LV, ExprType);
267
Daniel Dunbare6c31752008-08-29 08:11:39 +0000268 if (LV.isPropertyRef())
269 return EmitLoadOfPropertyRefLValue(LV, ExprType);
270
Chris Lattner09020ee2009-02-16 21:11:58 +0000271 assert(LV.isKVCRef() && "Unknown LValue type!");
272 return EmitLoadOfKVCRefLValue(LV, ExprType);
Chris Lattner4b009652007-07-25 00:24:17 +0000273}
274
Lauro Ramos Venanciob40307c2008-01-22 20:17:04 +0000275RValue CodeGenFunction::EmitLoadOfBitfieldLValue(LValue LV,
276 QualType ExprType) {
Daniel Dunbar833b03b2008-08-06 05:08:45 +0000277 unsigned StartBit = LV.getBitfieldStartBit();
278 unsigned BitfieldSize = LV.getBitfieldSize();
Lauro Ramos Venanciob40307c2008-01-22 20:17:04 +0000279 llvm::Value *Ptr = LV.getBitfieldAddr();
Daniel Dunbar833b03b2008-08-06 05:08:45 +0000280
281 const llvm::Type *EltTy =
Lauro Ramos Venanciob40307c2008-01-22 20:17:04 +0000282 cast<llvm::PointerType>(Ptr->getType())->getElementType();
Daniel Dunbar833b03b2008-08-06 05:08:45 +0000283 unsigned EltTySize = CGM.getTargetData().getTypeSizeInBits(EltTy);
Lauro Ramos Venanciob40307c2008-01-22 20:17:04 +0000284
Daniel Dunbar833b03b2008-08-06 05:08:45 +0000285 // In some cases the bitfield may straddle two memory locations.
286 // Currently we load the entire bitfield, then do the magic to
287 // sign-extend it if necessary. This results in somewhat more code
288 // than necessary for the common case (one load), since two shifts
289 // accomplish both the masking and sign extension.
290 unsigned LowBits = std::min(BitfieldSize, EltTySize - StartBit);
291 llvm::Value *Val = Builder.CreateLoad(Ptr, LV.isVolatileQualified(), "tmp");
292
293 // Shift to proper location.
Daniel Dunbar198edd52008-11-13 02:20:34 +0000294 if (StartBit)
295 Val = Builder.CreateLShr(Val, llvm::ConstantInt::get(EltTy, StartBit),
296 "bf.lo");
Daniel Dunbar833b03b2008-08-06 05:08:45 +0000297
298 // Mask off unused bits.
299 llvm::Constant *LowMask =
300 llvm::ConstantInt::get(llvm::APInt::getLowBitsSet(EltTySize, LowBits));
301 Val = Builder.CreateAnd(Val, LowMask, "bf.lo.cleared");
302
303 // Fetch the high bits if necessary.
304 if (LowBits < BitfieldSize) {
305 unsigned HighBits = BitfieldSize - LowBits;
306 llvm::Value *HighPtr =
307 Builder.CreateGEP(Ptr, llvm::ConstantInt::get(llvm::Type::Int32Ty, 1),
308 "bf.ptr.hi");
309 llvm::Value *HighVal = Builder.CreateLoad(HighPtr,
310 LV.isVolatileQualified(),
311 "tmp");
312
313 // Mask off unused bits.
314 llvm::Constant *HighMask =
315 llvm::ConstantInt::get(llvm::APInt::getLowBitsSet(EltTySize, HighBits));
316 HighVal = Builder.CreateAnd(HighVal, HighMask, "bf.lo.cleared");
Lauro Ramos Venanciob40307c2008-01-22 20:17:04 +0000317
Daniel Dunbar833b03b2008-08-06 05:08:45 +0000318 // Shift to proper location and or in to bitfield value.
319 HighVal = Builder.CreateShl(HighVal,
320 llvm::ConstantInt::get(EltTy, LowBits));
321 Val = Builder.CreateOr(Val, HighVal, "bf.val");
322 }
Lauro Ramos Venanciob40307c2008-01-22 20:17:04 +0000323
Daniel Dunbar833b03b2008-08-06 05:08:45 +0000324 // Sign extend if necessary.
325 if (LV.isBitfieldSigned()) {
326 llvm::Value *ExtraBits = llvm::ConstantInt::get(EltTy,
327 EltTySize - BitfieldSize);
328 Val = Builder.CreateAShr(Builder.CreateShl(Val, ExtraBits),
329 ExtraBits, "bf.val.sext");
330 }
Eli Friedmana04e70d2008-05-17 20:03:47 +0000331
332 // The bitfield type and the normal type differ when the storage sizes
333 // differ (currently just _Bool).
Daniel Dunbar833b03b2008-08-06 05:08:45 +0000334 Val = Builder.CreateIntCast(Val, ConvertType(ExprType), false, "tmp");
Eli Friedmana04e70d2008-05-17 20:03:47 +0000335
Daniel Dunbar833b03b2008-08-06 05:08:45 +0000336 return RValue::get(Val);
Lauro Ramos Venanciob40307c2008-01-22 20:17:04 +0000337}
338
Daniel Dunbare6c31752008-08-29 08:11:39 +0000339RValue CodeGenFunction::EmitLoadOfPropertyRefLValue(LValue LV,
340 QualType ExprType) {
341 return EmitObjCPropertyGet(LV.getPropertyRefExpr());
342}
343
Fariborz Jahanianb0973da2008-11-22 22:30:21 +0000344RValue CodeGenFunction::EmitLoadOfKVCRefLValue(LValue LV,
345 QualType ExprType) {
346 return EmitObjCPropertyGet(LV.getKVCRefExpr());
347}
348
Nate Begeman7903d052009-01-18 06:42:49 +0000349// If this is a reference to a subset of the elements of a vector, create an
350// appropriate shufflevector.
Nate Begemanaf6ed502008-04-18 23:10:10 +0000351RValue CodeGenFunction::EmitLoadOfExtVectorElementLValue(LValue LV,
352 QualType ExprType) {
Eli Friedman2e630542008-06-13 23:01:12 +0000353 llvm::Value *Vec = Builder.CreateLoad(LV.getExtVectorAddr(),
354 LV.isVolatileQualified(), "tmp");
Chris Lattner944f7962007-08-03 16:18:34 +0000355
Nate Begemanc8e51f82008-05-09 06:41:27 +0000356 const llvm::Constant *Elts = LV.getExtVectorElts();
Chris Lattner944f7962007-08-03 16:18:34 +0000357
358 // If the result of the expression is a non-vector type, we must be
359 // extracting a single element. Just codegen as an extractelement.
Chris Lattner4b492962007-08-10 17:10:08 +0000360 const VectorType *ExprVT = ExprType->getAsVectorType();
361 if (!ExprVT) {
Dan Gohman4751a3a2008-05-22 00:50:06 +0000362 unsigned InIdx = getAccessedFieldNo(0, Elts);
Chris Lattner944f7962007-08-03 16:18:34 +0000363 llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx);
364 return RValue::get(Builder.CreateExtractElement(Vec, Elt, "tmp"));
365 }
Nate Begeman7903d052009-01-18 06:42:49 +0000366
367 // Always use shuffle vector to try to retain the original program structure
Chris Lattner4b492962007-08-10 17:10:08 +0000368 unsigned NumResultElts = ExprVT->getNumElements();
Chris Lattner944f7962007-08-03 16:18:34 +0000369
Nate Begeman7903d052009-01-18 06:42:49 +0000370 llvm::SmallVector<llvm::Constant*, 4> Mask;
Chris Lattner944f7962007-08-03 16:18:34 +0000371 for (unsigned i = 0; i != NumResultElts; ++i) {
Dan Gohman4751a3a2008-05-22 00:50:06 +0000372 unsigned InIdx = getAccessedFieldNo(i, Elts);
Nate Begeman7903d052009-01-18 06:42:49 +0000373 Mask.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx));
Chris Lattner944f7962007-08-03 16:18:34 +0000374 }
375
Nate Begeman7903d052009-01-18 06:42:49 +0000376 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
377 Vec = Builder.CreateShuffleVector(Vec,
378 llvm::UndefValue::get(Vec->getType()),
379 MaskV, "tmp");
380 return RValue::get(Vec);
Chris Lattner944f7962007-08-03 16:18:34 +0000381}
382
383
Chris Lattner4b009652007-07-25 00:24:17 +0000384
385/// EmitStoreThroughLValue - Store the specified rvalue into the specified
386/// lvalue, where both are guaranteed to the have the same type, and that type
387/// is 'Ty'.
388void CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst,
389 QualType Ty) {
Chris Lattner5bfdd232007-08-03 16:28:33 +0000390 if (!Dst.isSimple()) {
391 if (Dst.isVectorElt()) {
392 // Read/modify/write the vector, inserting the new element.
Eli Friedman2e630542008-06-13 23:01:12 +0000393 llvm::Value *Vec = Builder.CreateLoad(Dst.getVectorAddr(),
394 Dst.isVolatileQualified(), "tmp");
Chris Lattnere24c4cf2007-08-31 22:49:20 +0000395 Vec = Builder.CreateInsertElement(Vec, Src.getScalarVal(),
Chris Lattner5bfdd232007-08-03 16:28:33 +0000396 Dst.getVectorIdx(), "vecins");
Eli Friedman2e630542008-06-13 23:01:12 +0000397 Builder.CreateStore(Vec, Dst.getVectorAddr(),Dst.isVolatileQualified());
Chris Lattner5bfdd232007-08-03 16:28:33 +0000398 return;
399 }
Chris Lattner4b009652007-07-25 00:24:17 +0000400
Nate Begemanaf6ed502008-04-18 23:10:10 +0000401 // If this is an update of extended vector elements, insert them as
402 // appropriate.
403 if (Dst.isExtVectorElt())
404 return EmitStoreThroughExtVectorComponentLValue(Src, Dst, Ty);
Lauro Ramos Venancio2d7a34c2008-01-22 22:36:45 +0000405
406 if (Dst.isBitfield())
407 return EmitStoreThroughBitfieldLValue(Src, Dst, Ty);
408
Daniel Dunbare6c31752008-08-29 08:11:39 +0000409 if (Dst.isPropertyRef())
410 return EmitStoreThroughPropertyRefLValue(Src, Dst, Ty);
411
Fariborz Jahanianb0973da2008-11-22 22:30:21 +0000412 if (Dst.isKVCRef())
413 return EmitStoreThroughKVCRefLValue(Src, Dst, Ty);
414
Lauro Ramos Venancio14d39842008-01-22 22:38:35 +0000415 assert(0 && "Unknown LValue type");
Chris Lattner5bfdd232007-08-03 16:28:33 +0000416 }
Chris Lattner4b009652007-07-25 00:24:17 +0000417
Fariborz Jahaniana4c010e2009-02-21 00:30:43 +0000418 if (Dst.isObjCWeak() && !Dst.isNonGC()) {
Fariborz Jahaniand2f661a2008-11-19 17:34:06 +0000419 // load of a __weak object.
420 llvm::Value *LvalueDst = Dst.getAddress();
421 llvm::Value *src = Src.getScalarVal();
422 CGM.getObjCRuntime().EmitObjCWeakAssign(*this, src, LvalueDst);
423 return;
424 }
425
Fariborz Jahaniana4c010e2009-02-21 00:30:43 +0000426 if (Dst.isObjCStrong() && !Dst.isNonGC()) {
Fariborz Jahaniand2f661a2008-11-19 17:34:06 +0000427 // load of a __strong object.
428 llvm::Value *LvalueDst = Dst.getAddress();
429 llvm::Value *src = Src.getScalarVal();
Fariborz Jahanian5a0c3412009-02-19 18:29:24 +0000430#if 0
431 // FIXME. We cannot positively determine if we have an
432 // 'ivar' assignment, object assignment or an unknown
433 // assignment. For now, generate call to objc_assign_strongCast
434 // assignment which is a safe, but consevative assumption.
Fariborz Jahanian70522662008-11-20 20:53:20 +0000435 if (Dst.isObjCIvar())
436 CGM.getObjCRuntime().EmitObjCIvarAssign(*this, src, LvalueDst);
437 else
438 CGM.getObjCRuntime().EmitObjCGlobalAssign(*this, src, LvalueDst);
Fariborz Jahanian5a0c3412009-02-19 18:29:24 +0000439#endif
440 CGM.getObjCRuntime().EmitObjCStrongCastAssign(*this, src, LvalueDst);
Fariborz Jahaniand2f661a2008-11-19 17:34:06 +0000441 return;
442 }
443
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000444 assert(Src.isScalar() && "Can't emit an agg store with this method");
Daniel Dunbarf1c5d842009-02-10 00:57:50 +0000445 EmitStoreOfScalar(Src.getScalarVal(), Dst.getAddress(),
446 Dst.isVolatileQualified());
Chris Lattner4b009652007-07-25 00:24:17 +0000447}
448
Lauro Ramos Venancio2d7a34c2008-01-22 22:36:45 +0000449void CodeGenFunction::EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst,
Daniel Dunbar2668dd12008-11-19 09:36:46 +0000450 QualType Ty,
451 llvm::Value **Result) {
Daniel Dunbar833b03b2008-08-06 05:08:45 +0000452 unsigned StartBit = Dst.getBitfieldStartBit();
453 unsigned BitfieldSize = Dst.getBitfieldSize();
Lauro Ramos Venancio2d7a34c2008-01-22 22:36:45 +0000454 llvm::Value *Ptr = Dst.getBitfieldAddr();
Lauro Ramos Venancio2d7a34c2008-01-22 22:36:45 +0000455
Daniel Dunbar833b03b2008-08-06 05:08:45 +0000456 const llvm::Type *EltTy =
457 cast<llvm::PointerType>(Ptr->getType())->getElementType();
458 unsigned EltTySize = CGM.getTargetData().getTypeSizeInBits(EltTy);
459
460 // Get the new value, cast to the appropriate type and masked to
461 // exactly the size of the bit-field.
Daniel Dunbar2668dd12008-11-19 09:36:46 +0000462 llvm::Value *SrcVal = Src.getScalarVal();
463 llvm::Value *NewVal = Builder.CreateIntCast(SrcVal, EltTy, false, "tmp");
Daniel Dunbar833b03b2008-08-06 05:08:45 +0000464 llvm::Constant *Mask =
465 llvm::ConstantInt::get(llvm::APInt::getLowBitsSet(EltTySize, BitfieldSize));
466 NewVal = Builder.CreateAnd(NewVal, Mask, "bf.value");
Lauro Ramos Venancio2d7a34c2008-01-22 22:36:45 +0000467
Daniel Dunbar2668dd12008-11-19 09:36:46 +0000468 // Return the new value of the bit-field, if requested.
469 if (Result) {
470 // Cast back to the proper type for result.
471 const llvm::Type *SrcTy = SrcVal->getType();
472 llvm::Value *SrcTrunc = Builder.CreateIntCast(NewVal, SrcTy, false,
473 "bf.reload.val");
474
475 // Sign extend if necessary.
476 if (Dst.isBitfieldSigned()) {
477 unsigned SrcTySize = CGM.getTargetData().getTypeSizeInBits(SrcTy);
478 llvm::Value *ExtraBits = llvm::ConstantInt::get(SrcTy,
479 SrcTySize - BitfieldSize);
480 SrcTrunc = Builder.CreateAShr(Builder.CreateShl(SrcTrunc, ExtraBits),
481 ExtraBits, "bf.reload.sext");
482 }
483
484 *Result = SrcTrunc;
485 }
486
Daniel Dunbar833b03b2008-08-06 05:08:45 +0000487 // In some cases the bitfield may straddle two memory locations.
488 // Emit the low part first and check to see if the high needs to be
489 // done.
490 unsigned LowBits = std::min(BitfieldSize, EltTySize - StartBit);
491 llvm::Value *LowVal = Builder.CreateLoad(Ptr, Dst.isVolatileQualified(),
492 "bf.prev.low");
Eli Friedmana04e70d2008-05-17 20:03:47 +0000493
Daniel Dunbar833b03b2008-08-06 05:08:45 +0000494 // Compute the mask for zero-ing the low part of this bitfield.
495 llvm::Constant *InvMask =
496 llvm::ConstantInt::get(~llvm::APInt::getBitsSet(EltTySize, StartBit,
497 StartBit + LowBits));
498
499 // Compute the new low part as
500 // LowVal = (LowVal & InvMask) | (NewVal << StartBit),
501 // with the shift of NewVal implicitly stripping the high bits.
502 llvm::Value *NewLowVal =
503 Builder.CreateShl(NewVal, llvm::ConstantInt::get(EltTy, StartBit),
504 "bf.value.lo");
505 LowVal = Builder.CreateAnd(LowVal, InvMask, "bf.prev.lo.cleared");
506 LowVal = Builder.CreateOr(LowVal, NewLowVal, "bf.new.lo");
507
508 // Write back.
509 Builder.CreateStore(LowVal, Ptr, Dst.isVolatileQualified());
Eli Friedmana04e70d2008-05-17 20:03:47 +0000510
Daniel Dunbar833b03b2008-08-06 05:08:45 +0000511 // If the low part doesn't cover the bitfield emit a high part.
512 if (LowBits < BitfieldSize) {
513 unsigned HighBits = BitfieldSize - LowBits;
514 llvm::Value *HighPtr =
515 Builder.CreateGEP(Ptr, llvm::ConstantInt::get(llvm::Type::Int32Ty, 1),
516 "bf.ptr.hi");
517 llvm::Value *HighVal = Builder.CreateLoad(HighPtr,
518 Dst.isVolatileQualified(),
519 "bf.prev.hi");
520
521 // Compute the mask for zero-ing the high part of this bitfield.
522 llvm::Constant *InvMask =
523 llvm::ConstantInt::get(~llvm::APInt::getLowBitsSet(EltTySize, HighBits));
524
525 // Compute the new high part as
526 // HighVal = (HighVal & InvMask) | (NewVal lshr LowBits),
527 // where the high bits of NewVal have already been cleared and the
528 // shift stripping the low bits.
529 llvm::Value *NewHighVal =
530 Builder.CreateLShr(NewVal, llvm::ConstantInt::get(EltTy, LowBits),
531 "bf.value.high");
532 HighVal = Builder.CreateAnd(HighVal, InvMask, "bf.prev.hi.cleared");
533 HighVal = Builder.CreateOr(HighVal, NewHighVal, "bf.new.hi");
534
535 // Write back.
536 Builder.CreateStore(HighVal, HighPtr, Dst.isVolatileQualified());
537 }
Lauro Ramos Venancio2d7a34c2008-01-22 22:36:45 +0000538}
539
Daniel Dunbare6c31752008-08-29 08:11:39 +0000540void CodeGenFunction::EmitStoreThroughPropertyRefLValue(RValue Src,
541 LValue Dst,
542 QualType Ty) {
543 EmitObjCPropertySet(Dst.getPropertyRefExpr(), Src);
544}
545
Fariborz Jahanianb0973da2008-11-22 22:30:21 +0000546void CodeGenFunction::EmitStoreThroughKVCRefLValue(RValue Src,
547 LValue Dst,
548 QualType Ty) {
549 EmitObjCPropertySet(Dst.getKVCRefExpr(), Src);
550}
551
Nate Begemanaf6ed502008-04-18 23:10:10 +0000552void CodeGenFunction::EmitStoreThroughExtVectorComponentLValue(RValue Src,
553 LValue Dst,
554 QualType Ty) {
Chris Lattner5bfdd232007-08-03 16:28:33 +0000555 // This access turns into a read/modify/write of the vector. Load the input
556 // value now.
Eli Friedman2e630542008-06-13 23:01:12 +0000557 llvm::Value *Vec = Builder.CreateLoad(Dst.getExtVectorAddr(),
558 Dst.isVolatileQualified(), "tmp");
Nate Begemanc8e51f82008-05-09 06:41:27 +0000559 const llvm::Constant *Elts = Dst.getExtVectorElts();
Chris Lattner5bfdd232007-08-03 16:28:33 +0000560
Chris Lattnere24c4cf2007-08-31 22:49:20 +0000561 llvm::Value *SrcVal = Src.getScalarVal();
Chris Lattner5bfdd232007-08-03 16:28:33 +0000562
Chris Lattner940966d2007-08-03 16:37:04 +0000563 if (const VectorType *VTy = Ty->getAsVectorType()) {
564 unsigned NumSrcElts = VTy->getNumElements();
Nate Begeman7903d052009-01-18 06:42:49 +0000565 unsigned NumDstElts =
566 cast<llvm::VectorType>(Vec->getType())->getNumElements();
567 if (NumDstElts == NumSrcElts) {
568 // Use shuffle vector is the src and destination are the same number
569 // of elements
570 llvm::SmallVector<llvm::Constant*, 4> Mask;
571 for (unsigned i = 0; i != NumSrcElts; ++i) {
572 unsigned InIdx = getAccessedFieldNo(i, Elts);
573 Mask.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx));
574 }
575
576 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
577 Vec = Builder.CreateShuffleVector(SrcVal,
578 llvm::UndefValue::get(Vec->getType()),
579 MaskV, "tmp");
580 }
581 else if (NumDstElts > NumSrcElts) {
582 // Extended the source vector to the same length and then shuffle it
583 // into the destination.
584 // FIXME: since we're shuffling with undef, can we just use the indices
585 // into that? This could be simpler.
586 llvm::SmallVector<llvm::Constant*, 4> ExtMask;
587 unsigned i;
588 for (i = 0; i != NumSrcElts; ++i)
589 ExtMask.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, i));
590 for (; i != NumDstElts; ++i)
591 ExtMask.push_back(llvm::UndefValue::get(llvm::Type::Int32Ty));
592 llvm::Value *ExtMaskV = llvm::ConstantVector::get(&ExtMask[0],
593 ExtMask.size());
Daniel Dunbar6a3b16e2009-02-17 18:31:04 +0000594 llvm::Value *ExtSrcVal =
595 Builder.CreateShuffleVector(SrcVal,
596 llvm::UndefValue::get(SrcVal->getType()),
597 ExtMaskV, "tmp");
Nate Begeman7903d052009-01-18 06:42:49 +0000598 // build identity
599 llvm::SmallVector<llvm::Constant*, 4> Mask;
600 for (unsigned i = 0; i != NumDstElts; ++i) {
601 Mask.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, i));
602 }
603 // modify when what gets shuffled in
604 for (unsigned i = 0; i != NumSrcElts; ++i) {
605 unsigned Idx = getAccessedFieldNo(i, Elts);
606 Mask[Idx] =llvm::ConstantInt::get(llvm::Type::Int32Ty, i+NumDstElts);
607 }
608 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
609 Vec = Builder.CreateShuffleVector(Vec, ExtSrcVal, MaskV, "tmp");
610 }
611 else {
612 // We should never shorten the vector
613 assert(0 && "unexpected shorten vector length");
Chris Lattner940966d2007-08-03 16:37:04 +0000614 }
615 } else {
616 // If the Src is a scalar (not a vector) it must be updating one element.
Dan Gohman4751a3a2008-05-22 00:50:06 +0000617 unsigned InIdx = getAccessedFieldNo(0, Elts);
Chris Lattner5bfdd232007-08-03 16:28:33 +0000618 llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx);
619 Vec = Builder.CreateInsertElement(Vec, SrcVal, Elt, "tmp");
Chris Lattner5bfdd232007-08-03 16:28:33 +0000620 }
621
Eli Friedman2e630542008-06-13 23:01:12 +0000622 Builder.CreateStore(Vec, Dst.getExtVectorAddr(), Dst.isVolatileQualified());
Chris Lattner5bfdd232007-08-03 16:28:33 +0000623}
624
Chris Lattner4b009652007-07-25 00:24:17 +0000625LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) {
Steve Naroff72a6ebc2008-04-15 22:42:06 +0000626 const VarDecl *VD = dyn_cast<VarDecl>(E->getDecl());
627
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000628 if (VD && (VD->isBlockVarDecl() || isa<ParmVarDecl>(VD) ||
629 isa<ImplicitParamDecl>(VD))) {
Fariborz Jahanianf0ca65f2008-11-20 00:15:42 +0000630 LValue LV;
631 if (VD->getStorageClass() == VarDecl::Extern) {
632 LV = LValue::MakeAddr(CGM.GetAddrOfGlobalVar(VD),
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +0000633 E->getType().getCVRQualifiers(),
634 getContext().getObjCGCAttrKind(E->getType()));
Fariborz Jahanianf0ca65f2008-11-20 00:15:42 +0000635 }
Lauro Ramos Venancio2348d972008-02-16 22:30:38 +0000636 else {
Steve Naroff72a6ebc2008-04-15 22:42:06 +0000637 llvm::Value *V = LocalDeclMap[VD];
Mike Stump2b6933f2009-02-28 09:07:16 +0000638 assert(V && "DeclRefExpr not entered in LocalDeclMap?");
Fariborz Jahanian4d41b952009-02-20 01:14:43 +0000639 // local variables do not get their gc attribute set.
640 QualType::GCAttrTypes attr = QualType::GCNone;
641 // local static?
Fariborz Jahaniana4c010e2009-02-21 00:30:43 +0000642 if (!VD->hasLocalStorage())
Fariborz Jahanian4d41b952009-02-20 01:14:43 +0000643 attr = getContext().getObjCGCAttrKind(E->getType());
Mike Stumpad9605d2009-03-04 03:23:46 +0000644 if (VD->getAttr<BlocksAttr>()) {
645 bool needsCopyDispose = BlockRequiresCopying(VD->getType());
646 const llvm::Type *PtrStructTy = V->getType();
647 const llvm::Type *Ty = PtrStructTy;
648 Ty = llvm::PointerType::get(Ty, 0);
649 V = Builder.CreateStructGEP(V, 1, "forwarding");
650 V = Builder.CreateBitCast(V, Ty);
651 V = Builder.CreateLoad(V, false);
652 V = Builder.CreateBitCast(V, PtrStructTy);
653 V = Builder.CreateStructGEP(V, needsCopyDispose*2 + 4, "x");
654 }
Fariborz Jahanian4d41b952009-02-20 01:14:43 +0000655 LV = LValue::MakeAddr(V, E->getType().getCVRQualifiers(), attr);
Lauro Ramos Venancio2348d972008-02-16 22:30:38 +0000656 }
Fariborz Jahaniana4c010e2009-02-21 00:30:43 +0000657 LValue::SetObjCNonGC(LV, VD->hasLocalStorage());
Fariborz Jahanianf0ca65f2008-11-20 00:15:42 +0000658 return LV;
Steve Naroff72a6ebc2008-04-15 22:42:06 +0000659 } else if (VD && VD->isFileVarDecl()) {
Fariborz Jahanianc192d4d2008-11-18 20:18:11 +0000660 LValue LV = LValue::MakeAddr(CGM.GetAddrOfGlobalVar(VD),
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +0000661 E->getType().getCVRQualifiers(),
662 getContext().getObjCGCAttrKind(E->getType()));
Fariborz Jahanianc192d4d2008-11-18 20:18:11 +0000663 return LV;
Steve Naroff72a6ebc2008-04-15 22:42:06 +0000664 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(E->getDecl())) {
Daniel Dunbar7bf5b3d2008-07-29 23:18:29 +0000665 return LValue::MakeAddr(CGM.GetAddrOfFunction(FD),
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +0000666 E->getType().getCVRQualifiers(),
667 getContext().getObjCGCAttrKind(E->getType()));
Chris Lattner4b009652007-07-25 00:24:17 +0000668 }
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000669 else if (const ImplicitParamDecl *IPD =
670 dyn_cast<ImplicitParamDecl>(E->getDecl())) {
671 llvm::Value *V = LocalDeclMap[IPD];
672 assert(V && "BlockVarDecl not entered in LocalDeclMap?");
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +0000673 return LValue::MakeAddr(V, E->getType().getCVRQualifiers(),
674 getContext().getObjCGCAttrKind(E->getType()));
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000675 }
Chris Lattner4b009652007-07-25 00:24:17 +0000676 assert(0 && "Unimp declref");
Chris Lattner1d2b4612007-09-16 19:23:47 +0000677 //an invalid LValue, but the assert will
678 //ensure that this point is never reached.
679 return LValue();
Chris Lattner4b009652007-07-25 00:24:17 +0000680}
681
Mike Stump2b6933f2009-02-28 09:07:16 +0000682LValue CodeGenFunction::EmitBlockDeclRefLValue(const BlockDeclRefExpr *E) {
683 return LValue::MakeAddr(GetAddrOfBlockDecl(E), 0);
684}
685
Chris Lattner4b009652007-07-25 00:24:17 +0000686LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) {
687 // __extension__ doesn't affect lvalue-ness.
688 if (E->getOpcode() == UnaryOperator::Extension)
689 return EmitLValue(E->getSubExpr());
690
Chris Lattnerc154ac12008-07-26 22:37:01 +0000691 QualType ExprTy = getContext().getCanonicalType(E->getSubExpr()->getType());
Chris Lattner5bf72022007-10-30 22:53:42 +0000692 switch (E->getOpcode()) {
693 default: assert(0 && "Unknown unary operator lvalue!");
694 case UnaryOperator::Deref:
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +0000695 {
Chris Lattnerc5d32632009-02-24 22:18:39 +0000696 QualType T =
697 E->getSubExpr()->getType()->getAsPointerType()->getPointeeType();
Fariborz Jahanian2224fb22009-02-23 18:59:50 +0000698 LValue LV = LValue::MakeAddr(EmitScalarExpr(E->getSubExpr()),
699 ExprTy->getAsPointerType()->getPointeeType()
Fariborz Jahanian4d41b952009-02-20 01:14:43 +0000700 .getCVRQualifiers(),
Fariborz Jahanian2224fb22009-02-23 18:59:50 +0000701 getContext().getObjCGCAttrKind(T));
702 // We should not generate __weak write barrier on indirect reference
703 // of a pointer to object; as in void foo (__weak id *param); *param = 0;
704 // But, we continue to generate __strong write barrier on indirect write
705 // into a pointer to object.
706 if (getContext().getLangOptions().ObjC1 &&
707 getContext().getLangOptions().getGCMode() != LangOptions::NonGC &&
708 LV.isObjCWeak())
709 LValue::SetObjCNonGC(LV, !E->isOBJCGCCandidate());
710 return LV;
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +0000711 }
Chris Lattner5bf72022007-10-30 22:53:42 +0000712 case UnaryOperator::Real:
713 case UnaryOperator::Imag:
714 LValue LV = EmitLValue(E->getSubExpr());
Chris Lattner07307562008-03-19 05:19:41 +0000715 unsigned Idx = E->getOpcode() == UnaryOperator::Imag;
716 return LValue::MakeAddr(Builder.CreateStructGEP(LV.getAddress(),
Chris Lattnerd5a56aa2008-07-26 22:17:49 +0000717 Idx, "idx"),
718 ExprTy.getCVRQualifiers());
Chris Lattner5bf72022007-10-30 22:53:42 +0000719 }
Chris Lattner4b009652007-07-25 00:24:17 +0000720}
721
722LValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) {
Daniel Dunbar31fe9c32008-08-13 23:20:05 +0000723 return LValue::MakeAddr(CGM.GetAddrOfConstantStringFromLiteral(E), 0);
Chris Lattner4b009652007-07-25 00:24:17 +0000724}
725
Chris Lattnerc5d32632009-02-24 22:18:39 +0000726LValue CodeGenFunction::EmitObjCEncodeExprLValue(const ObjCEncodeExpr *E) {
727 return LValue::MakeAddr(CGM.GetAddrOfConstantStringFromObjCEncode(E), 0);
728}
729
730
Daniel Dunbara9f0be22008-10-17 21:58:32 +0000731LValue CodeGenFunction::EmitPredefinedFunctionName(unsigned Type) {
Chris Lattner4b009652007-07-25 00:24:17 +0000732 std::string GlobalVarName;
Daniel Dunbara9f0be22008-10-17 21:58:32 +0000733
734 switch (Type) {
Chris Lattnerc5d32632009-02-24 22:18:39 +0000735 default:
736 assert(0 && "Invalid type");
737 case PredefinedExpr::Func:
738 GlobalVarName = "__func__.";
739 break;
740 case PredefinedExpr::Function:
741 GlobalVarName = "__FUNCTION__.";
742 break;
743 case PredefinedExpr::PrettyFunction:
744 // FIXME:: Demangle C++ method names
745 GlobalVarName = "__PRETTY_FUNCTION__.";
746 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000747 }
Daniel Dunbara9f0be22008-10-17 21:58:32 +0000748
749 std::string FunctionName;
750 if(const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurFuncDecl)) {
Douglas Gregor3c3c4542009-02-18 23:53:56 +0000751 FunctionName = CGM.getMangledName(FD);
Daniel Dunbara9f0be22008-10-17 21:58:32 +0000752 } else {
753 // Just get the mangled name.
754 FunctionName = CurFn->getName();
755 }
756
Chris Lattner6e6a5972008-04-04 04:07:35 +0000757 GlobalVarName += FunctionName;
Daniel Dunbara9f0be22008-10-17 21:58:32 +0000758 llvm::Constant *C =
759 CGM.GetAddrOfConstantCString(FunctionName, GlobalVarName.c_str());
760 return LValue::MakeAddr(C, 0);
761}
762
763LValue CodeGenFunction::EmitPredefinedLValue(const PredefinedExpr *E) {
764 switch (E->getIdentType()) {
765 default:
766 return EmitUnsupportedLValue(E, "predefined expression");
767 case PredefinedExpr::Func:
768 case PredefinedExpr::Function:
769 case PredefinedExpr::PrettyFunction:
770 return EmitPredefinedFunctionName(E->getIdentType());
771 }
Chris Lattner4b009652007-07-25 00:24:17 +0000772}
773
774LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) {
Ted Kremenek1c1700f2007-08-20 16:18:38 +0000775 // The index must always be an integer, which is not an aggregate. Emit it.
Chris Lattner9fba49a2007-08-24 05:35:26 +0000776 llvm::Value *Idx = EmitScalarExpr(E->getIdx());
Chris Lattner4b009652007-07-25 00:24:17 +0000777
778 // If the base is a vector type, then we are forming a vector element lvalue
779 // with this subscript.
Eli Friedman2e630542008-06-13 23:01:12 +0000780 if (E->getBase()->getType()->isVectorType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000781 // Emit the vector as an lvalue to get its address.
Eli Friedman2e630542008-06-13 23:01:12 +0000782 LValue LHS = EmitLValue(E->getBase());
Ted Kremenek1c1700f2007-08-20 16:18:38 +0000783 assert(LHS.isSimple() && "Can only subscript lvalue vectors here!");
Chris Lattner4b009652007-07-25 00:24:17 +0000784 // FIXME: This should properly sign/zero/extend or truncate Idx to i32.
Eli Friedman2e630542008-06-13 23:01:12 +0000785 return LValue::MakeVectorElt(LHS.getAddress(), Idx,
786 E->getBase()->getType().getCVRQualifiers());
Chris Lattner4b009652007-07-25 00:24:17 +0000787 }
788
Ted Kremenek1c1700f2007-08-20 16:18:38 +0000789 // The base must be a pointer, which is not an aggregate. Emit it.
Chris Lattner9fba49a2007-08-24 05:35:26 +0000790 llvm::Value *Base = EmitScalarExpr(E->getBase());
Chris Lattner4b009652007-07-25 00:24:17 +0000791
Ted Kremenek1c1700f2007-08-20 16:18:38 +0000792 // Extend or truncate the index type to 32 or 64-bits.
Chris Lattner2af72ac2007-08-08 17:43:05 +0000793 QualType IdxTy = E->getIdx()->getType();
Chris Lattner4b009652007-07-25 00:24:17 +0000794 bool IdxSigned = IdxTy->isSignedIntegerType();
795 unsigned IdxBitwidth = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
796 if (IdxBitwidth != LLVMPointerWidth)
797 Idx = Builder.CreateIntCast(Idx, llvm::IntegerType::get(LLVMPointerWidth),
798 IdxSigned, "idxprom");
799
800 // We know that the pointer points to a type of the correct size, unless the
801 // size is a VLA.
Anders Carlsson3bb57e82008-12-21 00:11:23 +0000802 if (const VariableArrayType *VAT =
803 getContext().getAsVariableArrayType(E->getType())) {
804 llvm::Value *VLASize = VLASizeMap[VAT];
805
806 Idx = Builder.CreateMul(Idx, VLASize);
807
Anders Carlsson76d19c82008-12-21 03:44:36 +0000808 QualType BaseType = getContext().getBaseElementType(VAT);
Anders Carlsson3bb57e82008-12-21 00:11:23 +0000809
810 uint64_t BaseTypeSize = getContext().getTypeSize(BaseType) / 8;
811 Idx = Builder.CreateUDiv(Idx,
812 llvm::ConstantInt::get(Idx->getType(),
813 BaseTypeSize));
814 }
815
Fariborz Jahaniancc59d472009-02-19 00:48:05 +0000816 QualType T = E->getBase()->getType();
817 QualType ExprTy = getContext().getCanonicalType(T);
818 T = T->getAsPointerType()->getPointeeType();
Fariborz Jahanian1ff3c9d2009-02-21 23:37:19 +0000819 LValue LV =
820 LValue::MakeAddr(Builder.CreateGEP(Base, Idx, "arrayidx"),
Fariborz Jahaniancc59d472009-02-19 00:48:05 +0000821 ExprTy->getAsPointerType()->getPointeeType().getCVRQualifiers(),
822 getContext().getObjCGCAttrKind(T));
Fariborz Jahanian1ff3c9d2009-02-21 23:37:19 +0000823 if (getContext().getLangOptions().ObjC1 &&
824 getContext().getLangOptions().getGCMode() != LangOptions::NonGC)
Fariborz Jahanian0c195b92009-02-22 18:40:18 +0000825 LValue::SetObjCNonGC(LV, !E->isOBJCGCCandidate());
Fariborz Jahanian1ff3c9d2009-02-21 23:37:19 +0000826 return LV;
Chris Lattner4b009652007-07-25 00:24:17 +0000827}
828
Nate Begemana1ae7442008-05-13 21:03:02 +0000829static
830llvm::Constant *GenerateConstantVector(llvm::SmallVector<unsigned, 4> &Elts) {
831 llvm::SmallVector<llvm::Constant *, 4> CElts;
832
833 for (unsigned i = 0, e = Elts.size(); i != e; ++i)
834 CElts.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, Elts[i]));
835
836 return llvm::ConstantVector::get(&CElts[0], CElts.size());
837}
838
Chris Lattner65520192007-08-02 23:37:31 +0000839LValue CodeGenFunction::
Nate Begemanaf6ed502008-04-18 23:10:10 +0000840EmitExtVectorElementExpr(const ExtVectorElementExpr *E) {
Chris Lattner65520192007-08-02 23:37:31 +0000841 // Emit the base vector as an l-value.
Chris Lattner09020ee2009-02-16 21:11:58 +0000842 LValue Base;
843
844 // ExtVectorElementExpr's base can either be a vector or pointer to vector.
Chris Lattner98e7fcc2009-02-16 22:14:05 +0000845 if (!E->isArrow()) {
Chris Lattner09020ee2009-02-16 21:11:58 +0000846 assert(E->getBase()->getType()->isVectorType());
847 Base = EmitLValue(E->getBase());
Chris Lattner98e7fcc2009-02-16 22:14:05 +0000848 } else {
849 const PointerType *PT = E->getBase()->getType()->getAsPointerType();
850 llvm::Value *Ptr = EmitScalarExpr(E->getBase());
851 Base = LValue::MakeAddr(Ptr, PT->getPointeeType().getCVRQualifiers());
Chris Lattner09020ee2009-02-16 21:11:58 +0000852 }
Chris Lattner65520192007-08-02 23:37:31 +0000853
Nate Begemana1ae7442008-05-13 21:03:02 +0000854 // Encode the element access list into a vector of unsigned indices.
855 llvm::SmallVector<unsigned, 4> Indices;
856 E->getEncodedElementAccess(Indices);
857
858 if (Base.isSimple()) {
859 llvm::Constant *CV = GenerateConstantVector(Indices);
Eli Friedman2e630542008-06-13 23:01:12 +0000860 return LValue::MakeExtVectorElt(Base.getAddress(), CV,
Chris Lattner9df79c32009-02-16 22:25:49 +0000861 Base.getQualifiers());
Nate Begemana1ae7442008-05-13 21:03:02 +0000862 }
863 assert(Base.isExtVectorElt() && "Can only subscript lvalue vec elts here!");
864
865 llvm::Constant *BaseElts = Base.getExtVectorElts();
866 llvm::SmallVector<llvm::Constant *, 4> CElts;
867
868 for (unsigned i = 0, e = Indices.size(); i != e; ++i) {
869 if (isa<llvm::ConstantAggregateZero>(BaseElts))
870 CElts.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, 0));
871 else
872 CElts.push_back(BaseElts->getOperand(Indices[i]));
873 }
874 llvm::Constant *CV = llvm::ConstantVector::get(&CElts[0], CElts.size());
Eli Friedman2e630542008-06-13 23:01:12 +0000875 return LValue::MakeExtVectorElt(Base.getExtVectorAddr(), CV,
Chris Lattner9df79c32009-02-16 22:25:49 +0000876 Base.getQualifiers());
Chris Lattner65520192007-08-02 23:37:31 +0000877}
878
Devang Patel41b66252007-10-23 20:28:39 +0000879LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) {
Devang Patele1f79db2007-12-11 21:33:16 +0000880 bool isUnion = false;
Fariborz Jahanian08c06f42008-11-21 18:14:01 +0000881 bool isIvar = false;
Fariborz Jahaniana4c010e2009-02-21 00:30:43 +0000882 bool isNonGC = false;
Devang Patel9dd3e2b2007-10-24 22:26:28 +0000883 Expr *BaseExpr = E->getBase();
Devang Patel9dd3e2b2007-10-24 22:26:28 +0000884 llvm::Value *BaseValue = NULL;
Eli Friedman2e630542008-06-13 23:01:12 +0000885 unsigned CVRQualifiers=0;
886
Chris Lattner659079e2007-12-02 18:52:07 +0000887 // If this is s.x, emit s as an lvalue. If it is s->x, emit s as a scalar.
Devang Patele1f79db2007-12-11 21:33:16 +0000888 if (E->isArrow()) {
Devang Patel2b24fd92007-10-26 18:15:21 +0000889 BaseValue = EmitScalarExpr(BaseExpr);
Devang Patele1f79db2007-12-11 21:33:16 +0000890 const PointerType *PTy =
Chris Lattnerc154ac12008-07-26 22:37:01 +0000891 cast<PointerType>(getContext().getCanonicalType(BaseExpr->getType()));
Devang Patele1f79db2007-12-11 21:33:16 +0000892 if (PTy->getPointeeType()->isUnionType())
893 isUnion = true;
Eli Friedman2e630542008-06-13 23:01:12 +0000894 CVRQualifiers = PTy->getPointeeType().getCVRQualifiers();
Chris Lattner9df79c32009-02-16 22:25:49 +0000895 } else if (isa<ObjCPropertyRefExpr>(BaseExpr) ||
896 isa<ObjCKVCRefExpr>(BaseExpr)) {
Fariborz Jahanian4e881652009-01-12 23:27:26 +0000897 RValue RV = EmitObjCPropertyGet(BaseExpr);
898 BaseValue = RV.getAggregateAddr();
899 if (BaseExpr->getType()->isUnionType())
900 isUnion = true;
901 CVRQualifiers = BaseExpr->getType().getCVRQualifiers();
Chris Lattner9df79c32009-02-16 22:25:49 +0000902 } else {
Chris Lattner659079e2007-12-02 18:52:07 +0000903 LValue BaseLV = EmitLValue(BaseExpr);
Fariborz Jahanian08c06f42008-11-21 18:14:01 +0000904 if (BaseLV.isObjCIvar())
905 isIvar = true;
Fariborz Jahaniana4c010e2009-02-21 00:30:43 +0000906 if (BaseLV.isNonGC())
907 isNonGC = true;
Chris Lattner659079e2007-12-02 18:52:07 +0000908 // FIXME: this isn't right for bitfields.
909 BaseValue = BaseLV.getAddress();
Devang Patele1f79db2007-12-11 21:33:16 +0000910 if (BaseExpr->getType()->isUnionType())
911 isUnion = true;
Eli Friedman2e630542008-06-13 23:01:12 +0000912 CVRQualifiers = BaseExpr->getType().getCVRQualifiers();
Chris Lattner659079e2007-12-02 18:52:07 +0000913 }
Devang Patel41b66252007-10-23 20:28:39 +0000914
Douglas Gregor82d44772008-12-20 23:49:58 +0000915 FieldDecl *Field = dyn_cast<FieldDecl>(E->getMemberDecl());
916 // FIXME: Handle non-field member expressions
917 assert(Field && "No code generation for non-field member references");
Chris Lattner9df79c32009-02-16 22:25:49 +0000918 LValue MemExpLV = EmitLValueForField(BaseValue, Field, isUnion,
919 CVRQualifiers);
Fariborz Jahanian08c06f42008-11-21 18:14:01 +0000920 LValue::SetObjCIvar(MemExpLV, isIvar);
Fariborz Jahaniana4c010e2009-02-21 00:30:43 +0000921 LValue::SetObjCNonGC(MemExpLV, isNonGC);
Fariborz Jahanian08c06f42008-11-21 18:14:01 +0000922 return MemExpLV;
Eli Friedmand3550112008-02-09 08:50:58 +0000923}
Devang Patel41b66252007-10-23 20:28:39 +0000924
Fariborz Jahanian86008c02008-12-15 20:35:07 +0000925LValue CodeGenFunction::EmitLValueForBitfield(llvm::Value* BaseValue,
926 FieldDecl* Field,
Fariborz Jahanianc912eb72009-02-03 19:03:09 +0000927 unsigned CVRQualifiers) {
Daniel Dunbar6a3b16e2009-02-17 18:31:04 +0000928 unsigned idx = CGM.getTypes().getLLVMFieldNo(Field);
Fariborz Jahanian86008c02008-12-15 20:35:07 +0000929 // FIXME: CodeGenTypes should expose a method to get the appropriate
930 // type for FieldTy (the appropriate type is ABI-dependent).
Daniel Dunbar6a3b16e2009-02-17 18:31:04 +0000931 const llvm::Type *FieldTy =
932 CGM.getTypes().ConvertTypeForMem(Field->getType());
Fariborz Jahanian86008c02008-12-15 20:35:07 +0000933 const llvm::PointerType *BaseTy =
934 cast<llvm::PointerType>(BaseValue->getType());
935 unsigned AS = BaseTy->getAddressSpace();
936 BaseValue = Builder.CreateBitCast(BaseValue,
937 llvm::PointerType::get(FieldTy, AS),
938 "tmp");
939 llvm::Value *V = Builder.CreateGEP(BaseValue,
940 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx),
941 "tmp");
942
943 CodeGenTypes::BitFieldInfo bitFieldInfo =
944 CGM.getTypes().getBitFieldInfo(Field);
945 return LValue::MakeBitfield(V, bitFieldInfo.Begin, bitFieldInfo.Size,
946 Field->getType()->isSignedIntegerType(),
947 Field->getType().getCVRQualifiers()|CVRQualifiers);
948}
949
Eli Friedmand3550112008-02-09 08:50:58 +0000950LValue CodeGenFunction::EmitLValueForField(llvm::Value* BaseValue,
951 FieldDecl* Field,
Eli Friedman2e630542008-06-13 23:01:12 +0000952 bool isUnion,
953 unsigned CVRQualifiers)
Eli Friedmand3550112008-02-09 08:50:58 +0000954{
Fariborz Jahanian86008c02008-12-15 20:35:07 +0000955 if (Field->isBitField())
Fariborz Jahanianc912eb72009-02-03 19:03:09 +0000956 return EmitLValueForBitfield(BaseValue, Field, CVRQualifiers);
Fariborz Jahanianf0ca65f2008-11-20 00:15:42 +0000957
Fariborz Jahanianc912eb72009-02-03 19:03:09 +0000958 unsigned idx = CGM.getTypes().getLLVMFieldNo(Field);
Fariborz Jahanian86008c02008-12-15 20:35:07 +0000959 llvm::Value *V = Builder.CreateStructGEP(BaseValue, idx, "tmp");
Eli Friedman66813742008-05-29 11:33:25 +0000960
Devang Patel9b1ca9e2007-10-26 19:42:18 +0000961 // Match union field type.
Lauro Ramos Venancio63fc38f2008-02-07 19:29:53 +0000962 if (isUnion) {
Eli Friedman2e630542008-06-13 23:01:12 +0000963 const llvm::Type *FieldTy =
964 CGM.getTypes().ConvertTypeForMem(Field->getType());
Devang Patel0f2a8fb2007-10-30 20:59:40 +0000965 const llvm::PointerType * BaseTy =
966 cast<llvm::PointerType>(BaseValue->getType());
Eli Friedmancecdc6b2008-05-21 13:24:44 +0000967 unsigned AS = BaseTy->getAddressSpace();
968 V = Builder.CreateBitCast(V,
969 llvm::PointerType::get(FieldTy, AS),
970 "tmp");
Devang Patel9b1ca9e2007-10-26 19:42:18 +0000971 }
Lauro Ramos Venanciob40307c2008-01-22 20:17:04 +0000972
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +0000973 QualType::GCAttrTypes attr = QualType::GCNone;
Fariborz Jahanian80ff83c2009-02-18 17:52:36 +0000974 if (CGM.getLangOptions().ObjC1 &&
Fariborz Jahanian31804e12009-02-18 18:52:41 +0000975 CGM.getLangOptions().getGCMode() != LangOptions::NonGC) {
976 QualType Ty = Field->getType();
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +0000977 attr = Ty.getObjCGCAttr();
Fariborz Jahaniancc59d472009-02-19 00:48:05 +0000978 if (attr != QualType::GCNone) {
Fariborz Jahanian31804e12009-02-18 18:52:41 +0000979 // __weak attribute on a field is ignored.
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +0000980 if (attr == QualType::Weak)
981 attr = QualType::GCNone;
Fariborz Jahaniancc59d472009-02-19 00:48:05 +0000982 }
Fariborz Jahanian31804e12009-02-18 18:52:41 +0000983 else if (getContext().isObjCObjectPointerType(Ty))
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +0000984 attr = QualType::Strong;
Fariborz Jahanian31804e12009-02-18 18:52:41 +0000985 }
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +0000986 LValue LV =
987 LValue::MakeAddr(V,
988 Field->getType().getCVRQualifiers()|CVRQualifiers,
989 attr);
Fariborz Jahanianf0ca65f2008-11-20 00:15:42 +0000990 return LV;
Devang Patel41b66252007-10-23 20:28:39 +0000991}
992
Chris Lattner22523ba2009-03-18 18:28:57 +0000993LValue CodeGenFunction::EmitCompoundLiteralLValue(const CompoundLiteralExpr* E){
Eli Friedmanf3c2cb42008-05-13 23:18:27 +0000994 const llvm::Type *LTy = ConvertType(E->getType());
995 llvm::Value *DeclPtr = CreateTempAlloca(LTy, ".compoundliteral");
996
997 const Expr* InitExpr = E->getInitializer();
Eli Friedman2e630542008-06-13 23:01:12 +0000998 LValue Result = LValue::MakeAddr(DeclPtr, E->getType().getCVRQualifiers());
Eli Friedmanf3c2cb42008-05-13 23:18:27 +0000999
1000 if (E->getType()->isComplexType()) {
1001 EmitComplexExprIntoAddr(InitExpr, DeclPtr, false);
1002 } else if (hasAggregateLLVMType(E->getType())) {
1003 EmitAnyExpr(InitExpr, DeclPtr, false);
1004 } else {
1005 EmitStoreThroughLValue(EmitAnyExpr(InitExpr), Result, E->getType());
1006 }
1007
1008 return Result;
1009}
1010
Chris Lattner22523ba2009-03-18 18:28:57 +00001011/// EmitCastLValue - Casts are never lvalues. If a cast is needed by the code
1012/// generator in an lvalue context, then it must mean that we need the address
1013/// of an aggregate in order to access one of its fields. This can happen for
1014/// all the reasons that casts are permitted with aggregate result, including
1015/// noop aggregate casts, and cast from scalar to union.
1016LValue CodeGenFunction::EmitCastLValue(const CastExpr *E) {
1017 // If this is an aggregate-to-aggregate cast, just use the input's address as
1018 // the lvalue.
1019 if (getContext().hasSameUnqualifiedType(E->getType(),
1020 E->getSubExpr()->getType()))
1021 return EmitLValue(E->getSubExpr());
1022
1023 // Otherwise, we must have a cast from scalar to union.
1024 assert(E->getType()->isUnionType() && "Expected scalar-to-union cast");
1025
1026 // Casts are only lvalues when the source and destination types are the same.
1027 llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType()));
Chris Lattner96a3a2d2009-03-18 18:30:44 +00001028 EmitAnyExpr(E->getSubExpr(), Temp, false);
Chris Lattner22523ba2009-03-18 18:28:57 +00001029
1030 return LValue::MakeAddr(Temp, E->getType().getCVRQualifiers(),
1031 getContext().getObjCGCAttrKind(E->getType()));
1032}
1033
Chris Lattner4b009652007-07-25 00:24:17 +00001034//===--------------------------------------------------------------------===//
1035// Expression Emission
1036//===--------------------------------------------------------------------===//
1037
Chris Lattnerb2cb9cb2007-08-20 22:37:10 +00001038
Chris Lattner4b009652007-07-25 00:24:17 +00001039RValue CodeGenFunction::EmitCallExpr(const CallExpr *E) {
Daniel Dunbar191eb9e2009-02-20 18:06:48 +00001040 // Builtins never have block type.
Daniel Dunbare3a6a682009-01-09 16:50:52 +00001041 if (E->getCallee()->getType()->isBlockPointerType())
Anders Carlssond2a889b2009-02-12 00:39:25 +00001042 return EmitBlockCallExpr(E);
Daniel Dunbare3a6a682009-01-09 16:50:52 +00001043
Daniel Dunbar191eb9e2009-02-20 18:06:48 +00001044 const Decl *TargetDecl = 0;
Daniel Dunbar337f60a2009-02-20 19:34:33 +00001045 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E->getCallee())) {
1046 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CE->getSubExpr())) {
1047 TargetDecl = DRE->getDecl();
1048 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(TargetDecl))
1049 if (unsigned builtinID = FD->getBuiltinID(getContext()))
1050 return EmitBuiltinExpr(FD, builtinID, E);
Daniel Dunbar191eb9e2009-02-20 18:06:48 +00001051 }
1052 }
1053
Chris Lattner9fba49a2007-08-24 05:35:26 +00001054 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
Eli Friedman261f4ad2008-01-30 01:32:06 +00001055 return EmitCallExpr(Callee, E->getCallee()->getType(),
Daniel Dunbar191eb9e2009-02-20 18:06:48 +00001056 E->arg_begin(), E->arg_end(), TargetDecl);
Chris Lattner02c60f52007-08-31 04:44:06 +00001057}
1058
Daniel Dunbaref0d4c72008-09-04 03:20:13 +00001059LValue CodeGenFunction::EmitBinaryOperatorLValue(const BinaryOperator *E) {
1060 // Can only get l-value for binary operator expressions which are a
1061 // simple assignment of aggregate type.
1062 if (E->getOpcode() != BinaryOperator::Assign)
1063 return EmitUnsupportedLValue(E, "binary l-value expression");
1064
1065 llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType()));
1066 EmitAggExpr(E, Temp, false);
1067 // FIXME: Are these qualifiers correct?
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +00001068 return LValue::MakeAddr(Temp, E->getType().getCVRQualifiers(),
1069 getContext().getObjCGCAttrKind(E->getType()));
Daniel Dunbaref0d4c72008-09-04 03:20:13 +00001070}
1071
Christopher Lambad327ba2007-12-29 05:02:41 +00001072LValue CodeGenFunction::EmitCallExprLValue(const CallExpr *E) {
1073 // Can only get l-value for call expression returning aggregate type
1074 RValue RV = EmitCallExpr(E);
Eli Friedman2e630542008-06-13 23:01:12 +00001075 return LValue::MakeAddr(RV.getAggregateAddr(),
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +00001076 E->getType().getCVRQualifiers(),
1077 getContext().getObjCGCAttrKind(E->getType()));
Christopher Lambad327ba2007-12-29 05:02:41 +00001078}
1079
Daniel Dunbar95d08f22009-02-11 20:59:32 +00001080LValue CodeGenFunction::EmitVAArgExprLValue(const VAArgExpr *E) {
1081 // FIXME: This shouldn't require another copy.
1082 llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType()));
1083 EmitAggExpr(E, Temp, false);
1084 return LValue::MakeAddr(Temp, E->getType().getCVRQualifiers());
1085}
1086
Argiris Kirtzidisbf615b02008-09-10 02:36:38 +00001087LValue
1088CodeGenFunction::EmitCXXConditionDeclLValue(const CXXConditionDeclExpr *E) {
1089 EmitLocalBlockVarDecl(*E->getVarDecl());
1090 return EmitDeclRefLValue(E);
1091}
1092
Daniel Dunbar5e105892008-08-23 10:51:21 +00001093LValue CodeGenFunction::EmitObjCMessageExprLValue(const ObjCMessageExpr *E) {
1094 // Can only get l-value for message expression returning aggregate type
1095 RValue RV = EmitObjCMessageExpr(E);
1096 // FIXME: can this be volatile?
1097 return LValue::MakeAddr(RV.getAggregateAddr(),
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +00001098 E->getType().getCVRQualifiers(),
1099 getContext().getObjCGCAttrKind(E->getType()));
Daniel Dunbar5e105892008-08-23 10:51:21 +00001100}
1101
Daniel Dunbare856ac22008-09-24 04:00:38 +00001102llvm::Value *CodeGenFunction::EmitIvarOffset(ObjCInterfaceDecl *Interface,
1103 const ObjCIvarDecl *Ivar) {
Chris Lattnerb326b172008-03-30 23:03:07 +00001104 // Objective-C objects are traditionally C structures with their layout
1105 // defined at compile-time. In some implementations, their layout is not
1106 // defined until run time in order to allow instance variables to be added to
1107 // a class without recompiling all of the subclasses. If this is the case
1108 // then the CGObjCRuntime subclass must return true to LateBoundIvars and
1109 // implement the lookup itself.
Daniel Dunbare856ac22008-09-24 04:00:38 +00001110 if (CGM.getObjCRuntime().LateBoundIVars())
1111 assert(0 && "late-bound ivars are unsupported");
Fariborz Jahanian27cc6662009-02-10 19:02:04 +00001112 return CGM.getObjCRuntime().EmitIvarOffset(*this, Interface, Ivar);
Daniel Dunbare856ac22008-09-24 04:00:38 +00001113}
1114
Fariborz Jahanian55343922009-02-03 00:09:52 +00001115LValue CodeGenFunction::EmitLValueForIvar(QualType ObjectTy,
1116 llvm::Value *BaseValue,
Daniel Dunbare856ac22008-09-24 04:00:38 +00001117 const ObjCIvarDecl *Ivar,
Fariborz Jahanian86008c02008-12-15 20:35:07 +00001118 const FieldDecl *Field,
Daniel Dunbare856ac22008-09-24 04:00:38 +00001119 unsigned CVRQualifiers) {
1120 // See comment in EmitIvarOffset.
1121 if (CGM.getObjCRuntime().LateBoundIVars())
1122 assert(0 && "late-bound ivars are unsupported");
Daniel Dunbare856ac22008-09-24 04:00:38 +00001123
Daniel Dunbar6a3b16e2009-02-17 18:31:04 +00001124 LValue LV = CGM.getObjCRuntime().EmitObjCValueForIvar(*this,
1125 ObjectTy,
1126 BaseValue, Ivar, Field,
1127 CVRQualifiers);
Fariborz Jahanian70522662008-11-20 20:53:20 +00001128 return LV;
Daniel Dunbare856ac22008-09-24 04:00:38 +00001129}
1130
1131LValue CodeGenFunction::EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E) {
Anders Carlssonae61b002008-08-25 01:53:23 +00001132 // FIXME: A lot of the code below could be shared with EmitMemberExpr.
1133 llvm::Value *BaseValue = 0;
1134 const Expr *BaseExpr = E->getBase();
1135 unsigned CVRQualifiers = 0;
Fariborz Jahanian55343922009-02-03 00:09:52 +00001136 QualType ObjectTy;
Anders Carlssonae61b002008-08-25 01:53:23 +00001137 if (E->isArrow()) {
1138 BaseValue = EmitScalarExpr(BaseExpr);
1139 const PointerType *PTy =
1140 cast<PointerType>(getContext().getCanonicalType(BaseExpr->getType()));
Fariborz Jahanian55343922009-02-03 00:09:52 +00001141 ObjectTy = PTy->getPointeeType();
1142 CVRQualifiers = ObjectTy.getCVRQualifiers();
Anders Carlssonae61b002008-08-25 01:53:23 +00001143 } else {
1144 LValue BaseLV = EmitLValue(BaseExpr);
1145 // FIXME: this isn't right for bitfields.
1146 BaseValue = BaseLV.getAddress();
Fariborz Jahanian55343922009-02-03 00:09:52 +00001147 ObjectTy = BaseExpr->getType();
1148 CVRQualifiers = ObjectTy.getCVRQualifiers();
Anders Carlssonae61b002008-08-25 01:53:23 +00001149 }
Daniel Dunbare856ac22008-09-24 04:00:38 +00001150
Fariborz Jahanian55343922009-02-03 00:09:52 +00001151 return EmitLValueForIvar(ObjectTy, BaseValue, E->getDecl(),
Fariborz Jahanianea944842008-12-18 17:29:46 +00001152 getContext().getFieldDecl(E), CVRQualifiers);
Chris Lattnerb326b172008-03-30 23:03:07 +00001153}
1154
Daniel Dunbare6c31752008-08-29 08:11:39 +00001155LValue
1156CodeGenFunction::EmitObjCPropertyRefLValue(const ObjCPropertyRefExpr *E) {
1157 // This is a special l-value that just issues sends when we load or
1158 // store through it.
1159 return LValue::MakePropertyRef(E, E->getType().getCVRQualifiers());
1160}
1161
Fariborz Jahanianb0973da2008-11-22 22:30:21 +00001162LValue
1163CodeGenFunction::EmitObjCKVCRefLValue(const ObjCKVCRefExpr *E) {
1164 // This is a special l-value that just issues sends when we load or
1165 // store through it.
1166 return LValue::MakeKVCRef(E, E->getType().getCVRQualifiers());
1167}
1168
Douglas Gregord8606632008-11-04 14:56:14 +00001169LValue
1170CodeGenFunction::EmitObjCSuperExpr(const ObjCSuperExpr *E) {
1171 return EmitUnsupportedLValue(E, "use of super");
1172}
1173
Daniel Dunbare3a6a682009-01-09 16:50:52 +00001174RValue CodeGenFunction::EmitCallExpr(llvm::Value *Callee, QualType CalleeType,
Ted Kremenek2719e982008-06-17 02:43:46 +00001175 CallExpr::const_arg_iterator ArgBeg,
Daniel Dunbar191eb9e2009-02-20 18:06:48 +00001176 CallExpr::const_arg_iterator ArgEnd,
1177 const Decl *TargetDecl) {
Daniel Dunbare3a6a682009-01-09 16:50:52 +00001178 // Get the actual function type. The callee type will always be a
1179 // pointer to function type or a block pointer type.
1180 QualType ResultType;
1181 if (const BlockPointerType *BPT = dyn_cast<BlockPointerType>(CalleeType)) {
1182 ResultType = BPT->getPointeeType()->getAsFunctionType()->getResultType();
1183 } else {
1184 assert(CalleeType->isFunctionPointerType() &&
1185 "Call must have function pointer type!");
1186 QualType FnType = CalleeType->getAsPointerType()->getPointeeType();
1187 ResultType = FnType->getAsFunctionType()->getResultType();
1188 }
Daniel Dunbar0ed60b02008-08-30 03:02:31 +00001189
1190 CallArgList Args;
1191 for (CallExpr::const_arg_iterator I = ArgBeg; I != ArgEnd; ++I)
Daniel Dunbar0a2da0f2008-09-09 01:06:48 +00001192 Args.push_back(std::make_pair(EmitAnyExprToTemp(*I),
1193 I->getType()));
Daniel Dunbar0ed60b02008-08-30 03:02:31 +00001194
Daniel Dunbar34bda882009-02-02 23:23:47 +00001195 return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args),
Daniel Dunbar191eb9e2009-02-20 18:06:48 +00001196 Callee, Args, TargetDecl);
Daniel Dunbara04840b2008-08-23 03:46:30 +00001197}