blob: 0886ad040a4b87c330b7559ed5b6cfabbb16c0d9 [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 Lattner7fbb70d2009-03-22 00:24:14 +000032 if (!Builder.isNamePreserving())
33 Name = "";
Chris Lattner4b009652007-07-25 00:24:17 +000034 return new llvm::AllocaInst(Ty, 0, Name, AllocaInsertPt);
35}
36
37/// EvaluateExprAsBool - Perform the usual unary conversions on the specified
38/// expression and compare the result against zero, returning an Int1Ty value.
39llvm::Value *CodeGenFunction::EvaluateExprAsBool(const Expr *E) {
Chris Lattnercc50a512007-08-26 16:46:58 +000040 QualType BoolTy = getContext().BoolTy;
Chris Lattnerde0908b2008-04-04 16:54:41 +000041 if (!E->getType()->isAnyComplexType())
Chris Lattnercc50a512007-08-26 16:46:58 +000042 return EmitScalarConversion(EmitScalarExpr(E), E->getType(), BoolTy);
Chris Lattner4b009652007-07-25 00:24:17 +000043
Chris Lattnercc50a512007-08-26 16:46:58 +000044 return EmitComplexToScalarConversion(EmitComplexExpr(E), E->getType(),BoolTy);
Chris Lattner4b009652007-07-25 00:24:17 +000045}
46
Chris Lattnere24c4cf2007-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
49/// aggregate expression, the aggloc/agglocvolatile arguments indicate where
50/// the result should be returned.
51RValue CodeGenFunction::EmitAnyExpr(const Expr *E, llvm::Value *AggLoc,
52 bool isAggLocVolatile) {
53 if (!hasAggregateLLVMType(E->getType()))
54 return RValue::get(EmitScalarExpr(E));
Chris Lattnerde0908b2008-04-04 16:54:41 +000055 else if (E->getType()->isAnyComplexType())
Chris Lattnere24c4cf2007-08-31 22:49:20 +000056 return RValue::getComplex(EmitComplexExpr(E));
57
58 EmitAggExpr(E, AggLoc, isAggLocVolatile);
59 return RValue::getAggregate(AggLoc);
60}
61
Daniel Dunbar0a2da0f2008-09-09 01:06:48 +000062/// EmitAnyExprToTemp - Similary to EmitAnyExpr(), however, the result
63/// will always be accessible even if no aggregate location is
64/// provided.
65RValue CodeGenFunction::EmitAnyExprToTemp(const Expr *E, llvm::Value *AggLoc,
66 bool isAggLocVolatile) {
67 if (!AggLoc && hasAggregateLLVMType(E->getType()) &&
68 !E->getType()->isAnyComplexType())
69 AggLoc = CreateTempAlloca(ConvertType(E->getType()), "agg.tmp");
70 return EmitAnyExpr(E, AggLoc, isAggLocVolatile);
71}
72
Anders Carlssonde55abc2009-05-20 00:24:07 +000073RValue CodeGenFunction::EmitReferenceBindingToExpr(const Expr* E,
74 QualType DestType) {
Anders Carlsson110536d2009-05-20 01:24:22 +000075 if (E->isLvalue(getContext()) == Expr::LV_Valid && !E->getBitField()) {
Anders Carlsson23ea02a2009-05-20 00:36:58 +000076 // Emit the expr as an lvalue.
77 LValue LV = EmitLValue(E);
78 return RValue::get(LV.getAddress());
79 }
80
Anders Carlsson0c0f7442009-05-20 01:03:17 +000081 if (!hasAggregateLLVMType(E->getType())) {
Anders Carlssonf4f07372009-05-20 01:35:03 +000082 // Create a temporary variable that we can bind the reference to.
Anders Carlsson0c0f7442009-05-20 01:03:17 +000083 llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(E->getType()),
84 "reftmp");
85 EmitStoreOfScalar(EmitScalarExpr(E), Temp, false, E->getType());
86 return RValue::get(Temp);
Anders Carlssonf4f07372009-05-20 01:35:03 +000087 } else if (E->getType()->isAnyComplexType()) {
88 // Create a temporary variable that we can bind the reference to.
89 llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(E->getType()),
90 "reftmp");
91 EmitComplexExprIntoAddr(E, Temp, false);
92 return RValue::get(Temp);
Anders Carlsson0c0f7442009-05-20 01:03:17 +000093 }
94
Anders Carlssonde55abc2009-05-20 00:24:07 +000095 CGM.ErrorUnsupported(E, "reference binding");
96 return GetUndefRValue(DestType);
97}
98
99
Dan Gohman4751a3a2008-05-22 00:50:06 +0000100/// getAccessedFieldNo - Given an encoded value and a result number, return
101/// the input field number being accessed.
102unsigned CodeGenFunction::getAccessedFieldNo(unsigned Idx,
103 const llvm::Constant *Elts) {
104 if (isa<llvm::ConstantAggregateZero>(Elts))
105 return 0;
106
107 return cast<llvm::ConstantInt>(Elts->getOperand(Idx))->getZExtValue();
108}
109
Chris Lattnere24c4cf2007-08-31 22:49:20 +0000110
Chris Lattner4b009652007-07-25 00:24:17 +0000111//===----------------------------------------------------------------------===//
112// LValue Expression Emission
113//===----------------------------------------------------------------------===//
114
Daniel Dunbar900c85a2009-02-05 07:09:07 +0000115RValue CodeGenFunction::GetUndefRValue(QualType Ty) {
116 if (Ty->isVoidType()) {
117 return RValue::get(0);
118 } else if (const ComplexType *CTy = Ty->getAsComplexType()) {
Daniel Dunbar8cb73402009-01-09 20:09:28 +0000119 const llvm::Type *EltTy = ConvertType(CTy->getElementType());
120 llvm::Value *U = llvm::UndefValue::get(EltTy);
121 return RValue::getComplex(std::make_pair(U, U));
Daniel Dunbar900c85a2009-02-05 07:09:07 +0000122 } else if (hasAggregateLLVMType(Ty)) {
123 const llvm::Type *LTy = llvm::PointerType::getUnqual(ConvertType(Ty));
124 return RValue::getAggregate(llvm::UndefValue::get(LTy));
Daniel Dunbar8cb73402009-01-09 20:09:28 +0000125 } else {
Daniel Dunbar900c85a2009-02-05 07:09:07 +0000126 return RValue::get(llvm::UndefValue::get(ConvertType(Ty)));
Daniel Dunbar8cb73402009-01-09 20:09:28 +0000127 }
Daniel Dunbare3a6a682009-01-09 16:50:52 +0000128}
129
Daniel Dunbar900c85a2009-02-05 07:09:07 +0000130RValue CodeGenFunction::EmitUnsupportedRValue(const Expr *E,
131 const char *Name) {
132 ErrorUnsupported(E, Name);
133 return GetUndefRValue(E->getType());
134}
135
Daniel Dunbarde1bd942008-08-25 20:45:57 +0000136LValue CodeGenFunction::EmitUnsupportedLValue(const Expr *E,
137 const char *Name) {
138 ErrorUnsupported(E, Name);
139 llvm::Type *Ty = llvm::PointerType::getUnqual(ConvertType(E->getType()));
140 return LValue::MakeAddr(llvm::UndefValue::get(Ty),
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +0000141 E->getType().getCVRQualifiers(),
142 getContext().getObjCGCAttrKind(E->getType()));
Daniel Dunbarde1bd942008-08-25 20:45:57 +0000143}
144
Chris Lattner4b009652007-07-25 00:24:17 +0000145/// EmitLValue - Emit code to compute a designator that specifies the location
146/// of the expression.
147///
148/// This can return one of two things: a simple address or a bitfield
149/// reference. In either case, the LLVM Value* in the LValue structure is
150/// guaranteed to be an LLVM pointer type.
151///
152/// If this returns a bitfield reference, nothing about the pointee type of
153/// the LLVM value is known: For example, it may not be a pointer to an
154/// integer.
155///
156/// If this returns a normal address, and if the lvalue's C type is fixed
157/// size, this method guarantees that the returned pointer type will point to
158/// an LLVM type of the same size of the lvalue's type. If the lvalue has a
159/// variable length type, this is not possible.
160///
161LValue CodeGenFunction::EmitLValue(const Expr *E) {
162 switch (E->getStmtClass()) {
Daniel Dunbarde1bd942008-08-25 20:45:57 +0000163 default: return EmitUnsupportedLValue(E, "l-value expression");
Chris Lattner4b009652007-07-25 00:24:17 +0000164
Daniel Dunbaref0d4c72008-09-04 03:20:13 +0000165 case Expr::BinaryOperatorClass:
166 return EmitBinaryOperatorLValue(cast<BinaryOperator>(E));
Douglas Gregor65fedaf2008-11-14 16:09:21 +0000167 case Expr::CallExprClass:
168 case Expr::CXXOperatorCallExprClass:
169 return EmitCallExprLValue(cast<CallExpr>(E));
Daniel Dunbar95d08f22009-02-11 20:59:32 +0000170 case Expr::VAArgExprClass:
171 return EmitVAArgExprLValue(cast<VAArgExpr>(E));
Douglas Gregor566782a2009-01-06 05:10:23 +0000172 case Expr::DeclRefExprClass:
173 case Expr::QualifiedDeclRefExprClass:
174 return EmitDeclRefLValue(cast<DeclRefExpr>(E));
Chris Lattner4b009652007-07-25 00:24:17 +0000175 case Expr::ParenExprClass:return EmitLValue(cast<ParenExpr>(E)->getSubExpr());
Chris Lattner69909292008-08-10 01:53:14 +0000176 case Expr::PredefinedExprClass:
177 return EmitPredefinedLValue(cast<PredefinedExpr>(E));
Chris Lattner4b009652007-07-25 00:24:17 +0000178 case Expr::StringLiteralClass:
179 return EmitStringLiteralLValue(cast<StringLiteral>(E));
Chris Lattnerc5d32632009-02-24 22:18:39 +0000180 case Expr::ObjCEncodeExprClass:
181 return EmitObjCEncodeExprLValue(cast<ObjCEncodeExpr>(E));
Chris Lattnerb326b172008-03-30 23:03:07 +0000182
Mike Stump2b6933f2009-02-28 09:07:16 +0000183 case Expr::BlockDeclRefExprClass:
184 return EmitBlockDeclRefLValue(cast<BlockDeclRefExpr>(E));
185
Argiris Kirtzidisbf615b02008-09-10 02:36:38 +0000186 case Expr::CXXConditionDeclExprClass:
187 return EmitCXXConditionDeclLValue(cast<CXXConditionDeclExpr>(E));
188
Daniel Dunbar5e105892008-08-23 10:51:21 +0000189 case Expr::ObjCMessageExprClass:
190 return EmitObjCMessageExprLValue(cast<ObjCMessageExpr>(E));
Chris Lattnerb326b172008-03-30 23:03:07 +0000191 case Expr::ObjCIvarRefExprClass:
192 return EmitObjCIvarRefLValue(cast<ObjCIvarRefExpr>(E));
Daniel Dunbarde1bd942008-08-25 20:45:57 +0000193 case Expr::ObjCPropertyRefExprClass:
Daniel Dunbare6c31752008-08-29 08:11:39 +0000194 return EmitObjCPropertyRefLValue(cast<ObjCPropertyRefExpr>(E));
Fariborz Jahanianb0973da2008-11-22 22:30:21 +0000195 case Expr::ObjCKVCRefExprClass:
196 return EmitObjCKVCRefLValue(cast<ObjCKVCRefExpr>(E));
Douglas Gregord8606632008-11-04 14:56:14 +0000197 case Expr::ObjCSuperExprClass:
Chris Lattnereec3a592009-04-25 19:35:26 +0000198 return EmitObjCSuperExprLValue(cast<ObjCSuperExpr>(E));
Douglas Gregord8606632008-11-04 14:56:14 +0000199
Chris Lattnereec3a592009-04-25 19:35:26 +0000200 case Expr::StmtExprClass:
201 return EmitStmtExprLValue(cast<StmtExpr>(E));
Chris Lattner4b009652007-07-25 00:24:17 +0000202 case Expr::UnaryOperatorClass:
203 return EmitUnaryOpLValue(cast<UnaryOperator>(E));
204 case Expr::ArraySubscriptExprClass:
205 return EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E));
Nate Begemanaf6ed502008-04-18 23:10:10 +0000206 case Expr::ExtVectorElementExprClass:
207 return EmitExtVectorElementExpr(cast<ExtVectorElementExpr>(E));
Devang Patel41b66252007-10-23 20:28:39 +0000208 case Expr::MemberExprClass: return EmitMemberExpr(cast<MemberExpr>(E));
Eli Friedmanf3c2cb42008-05-13 23:18:27 +0000209 case Expr::CompoundLiteralExprClass:
210 return EmitCompoundLiteralLValue(cast<CompoundLiteralExpr>(E));
Daniel Dunbaraecb4932009-03-24 02:38:23 +0000211 case Expr::ConditionalOperatorClass:
212 return EmitConditionalOperator(cast<ConditionalOperator>(E));
Chris Lattnera5f779dc2008-12-12 05:35:08 +0000213 case Expr::ChooseExprClass:
Eli Friedmand540c112009-03-04 05:52:32 +0000214 return EmitLValue(cast<ChooseExpr>(E)->getChosenSubExpr(getContext()));
Chris Lattner504239f2009-03-18 04:02:57 +0000215 case Expr::ImplicitCastExprClass:
216 case Expr::CStyleCastExprClass:
217 case Expr::CXXFunctionalCastExprClass:
218 case Expr::CXXStaticCastExprClass:
219 case Expr::CXXDynamicCastExprClass:
220 case Expr::CXXReinterpretCastExprClass:
221 case Expr::CXXConstCastExprClass:
Chris Lattner22523ba2009-03-18 18:28:57 +0000222 return EmitCastLValue(cast<CastExpr>(E));
Chris Lattner4b009652007-07-25 00:24:17 +0000223 }
224}
225
Daniel Dunbarf1c5d842009-02-10 00:57:50 +0000226llvm::Value *CodeGenFunction::EmitLoadOfScalar(llvm::Value *Addr, bool Volatile,
227 QualType Ty) {
228 llvm::Value *V = Builder.CreateLoad(Addr, Volatile, "tmp");
229
Anders Carlsson20786092009-05-19 19:36:19 +0000230 // Bool can have different representation in memory than in registers.
Daniel Dunbarf1c5d842009-02-10 00:57:50 +0000231 if (Ty->isBooleanType())
232 if (V->getType() != llvm::Type::Int1Ty)
233 V = Builder.CreateTrunc(V, llvm::Type::Int1Ty, "tobool");
234
235 return V;
236}
237
238void CodeGenFunction::EmitStoreOfScalar(llvm::Value *Value, llvm::Value *Addr,
Anders Carlsson05dfa992009-05-19 18:50:41 +0000239 bool Volatile, QualType Ty) {
Anders Carlsson20786092009-05-19 19:36:19 +0000240
241 if (Ty->isBooleanType()) {
242 // Bool can have different representation in memory than in registers.
243 const llvm::Type *SrcTy = Value->getType();
244 const llvm::PointerType *DstPtr = cast<llvm::PointerType>(Addr->getType());
245 if (DstPtr->getElementType() != SrcTy) {
246 const llvm::Type *MemTy =
247 llvm::PointerType::get(SrcTy, DstPtr->getAddressSpace());
248 Addr = Builder.CreateBitCast(Addr, MemTy, "storetmp");
249 }
Daniel Dunbarf1c5d842009-02-10 00:57:50 +0000250 }
Anders Carlsson20786092009-05-19 19:36:19 +0000251
Daniel Dunbarf1c5d842009-02-10 00:57:50 +0000252 Builder.CreateStore(Value, Addr, Volatile);
253}
254
Chris Lattner4b009652007-07-25 00:24:17 +0000255/// EmitLoadOfLValue - Given an expression that represents a value lvalue,
256/// this method emits the address of the lvalue, then loads the result as an
257/// rvalue, returning the rvalue.
258RValue CodeGenFunction::EmitLoadOfLValue(LValue LV, QualType ExprType) {
Fariborz Jahaniand2f661a2008-11-19 17:34:06 +0000259 if (LV.isObjCWeak()) {
Fariborz Jahanian3305ad32008-11-18 21:45:40 +0000260 // load of a __weak object.
261 llvm::Value *AddrWeakObj = LV.getAddress();
Fariborz Jahanian252d87f2008-11-18 22:37:34 +0000262 llvm::Value *read_weak = CGM.getObjCRuntime().EmitObjCWeakRead(*this,
Fariborz Jahanian3305ad32008-11-18 21:45:40 +0000263 AddrWeakObj);
264 return RValue::get(read_weak);
265 }
266
Chris Lattner4b009652007-07-25 00:24:17 +0000267 if (LV.isSimple()) {
268 llvm::Value *Ptr = LV.getAddress();
269 const llvm::Type *EltTy =
270 cast<llvm::PointerType>(Ptr->getType())->getElementType();
271
272 // Simple scalar l-value.
Daniel Dunbarf1c5d842009-02-10 00:57:50 +0000273 if (EltTy->isSingleValueType())
274 return RValue::get(EmitLoadOfScalar(Ptr, LV.isVolatileQualified(),
275 ExprType));
Chris Lattner4b009652007-07-25 00:24:17 +0000276
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000277 assert(ExprType->isFunctionType() && "Unknown scalar value");
278 return RValue::get(Ptr);
Chris Lattner4b009652007-07-25 00:24:17 +0000279 }
280
281 if (LV.isVectorElt()) {
Eli Friedman2e630542008-06-13 23:01:12 +0000282 llvm::Value *Vec = Builder.CreateLoad(LV.getVectorAddr(),
283 LV.isVolatileQualified(), "tmp");
Chris Lattner4b009652007-07-25 00:24:17 +0000284 return RValue::get(Builder.CreateExtractElement(Vec, LV.getVectorIdx(),
285 "vecext"));
286 }
Chris Lattnera735fac2007-08-03 00:16:29 +0000287
288 // If this is a reference to a subset of the elements of a vector, either
289 // shuffle the input or extract/insert them as appropriate.
Nate Begemanaf6ed502008-04-18 23:10:10 +0000290 if (LV.isExtVectorElt())
291 return EmitLoadOfExtVectorElementLValue(LV, ExprType);
Lauro Ramos Venanciob40307c2008-01-22 20:17:04 +0000292
293 if (LV.isBitfield())
294 return EmitLoadOfBitfieldLValue(LV, ExprType);
295
Daniel Dunbare6c31752008-08-29 08:11:39 +0000296 if (LV.isPropertyRef())
297 return EmitLoadOfPropertyRefLValue(LV, ExprType);
298
Chris Lattner09020ee2009-02-16 21:11:58 +0000299 assert(LV.isKVCRef() && "Unknown LValue type!");
300 return EmitLoadOfKVCRefLValue(LV, ExprType);
Chris Lattner4b009652007-07-25 00:24:17 +0000301}
302
Lauro Ramos Venanciob40307c2008-01-22 20:17:04 +0000303RValue CodeGenFunction::EmitLoadOfBitfieldLValue(LValue LV,
304 QualType ExprType) {
Daniel Dunbar833b03b2008-08-06 05:08:45 +0000305 unsigned StartBit = LV.getBitfieldStartBit();
306 unsigned BitfieldSize = LV.getBitfieldSize();
Lauro Ramos Venanciob40307c2008-01-22 20:17:04 +0000307 llvm::Value *Ptr = LV.getBitfieldAddr();
Daniel Dunbar833b03b2008-08-06 05:08:45 +0000308
309 const llvm::Type *EltTy =
Lauro Ramos Venanciob40307c2008-01-22 20:17:04 +0000310 cast<llvm::PointerType>(Ptr->getType())->getElementType();
Daniel Dunbar833b03b2008-08-06 05:08:45 +0000311 unsigned EltTySize = CGM.getTargetData().getTypeSizeInBits(EltTy);
Lauro Ramos Venanciob40307c2008-01-22 20:17:04 +0000312
Daniel Dunbar833b03b2008-08-06 05:08:45 +0000313 // In some cases the bitfield may straddle two memory locations.
314 // Currently we load the entire bitfield, then do the magic to
315 // sign-extend it if necessary. This results in somewhat more code
316 // than necessary for the common case (one load), since two shifts
317 // accomplish both the masking and sign extension.
318 unsigned LowBits = std::min(BitfieldSize, EltTySize - StartBit);
319 llvm::Value *Val = Builder.CreateLoad(Ptr, LV.isVolatileQualified(), "tmp");
320
321 // Shift to proper location.
Daniel Dunbar198edd52008-11-13 02:20:34 +0000322 if (StartBit)
323 Val = Builder.CreateLShr(Val, llvm::ConstantInt::get(EltTy, StartBit),
324 "bf.lo");
Daniel Dunbar833b03b2008-08-06 05:08:45 +0000325
326 // Mask off unused bits.
327 llvm::Constant *LowMask =
328 llvm::ConstantInt::get(llvm::APInt::getLowBitsSet(EltTySize, LowBits));
329 Val = Builder.CreateAnd(Val, LowMask, "bf.lo.cleared");
330
331 // Fetch the high bits if necessary.
332 if (LowBits < BitfieldSize) {
333 unsigned HighBits = BitfieldSize - LowBits;
334 llvm::Value *HighPtr =
335 Builder.CreateGEP(Ptr, llvm::ConstantInt::get(llvm::Type::Int32Ty, 1),
336 "bf.ptr.hi");
337 llvm::Value *HighVal = Builder.CreateLoad(HighPtr,
338 LV.isVolatileQualified(),
339 "tmp");
340
341 // Mask off unused bits.
342 llvm::Constant *HighMask =
343 llvm::ConstantInt::get(llvm::APInt::getLowBitsSet(EltTySize, HighBits));
344 HighVal = Builder.CreateAnd(HighVal, HighMask, "bf.lo.cleared");
Lauro Ramos Venanciob40307c2008-01-22 20:17:04 +0000345
Daniel Dunbar833b03b2008-08-06 05:08:45 +0000346 // Shift to proper location and or in to bitfield value.
347 HighVal = Builder.CreateShl(HighVal,
348 llvm::ConstantInt::get(EltTy, LowBits));
349 Val = Builder.CreateOr(Val, HighVal, "bf.val");
350 }
Lauro Ramos Venanciob40307c2008-01-22 20:17:04 +0000351
Daniel Dunbar833b03b2008-08-06 05:08:45 +0000352 // Sign extend if necessary.
353 if (LV.isBitfieldSigned()) {
354 llvm::Value *ExtraBits = llvm::ConstantInt::get(EltTy,
355 EltTySize - BitfieldSize);
356 Val = Builder.CreateAShr(Builder.CreateShl(Val, ExtraBits),
357 ExtraBits, "bf.val.sext");
358 }
Eli Friedmana04e70d2008-05-17 20:03:47 +0000359
360 // The bitfield type and the normal type differ when the storage sizes
361 // differ (currently just _Bool).
Daniel Dunbar833b03b2008-08-06 05:08:45 +0000362 Val = Builder.CreateIntCast(Val, ConvertType(ExprType), false, "tmp");
Eli Friedmana04e70d2008-05-17 20:03:47 +0000363
Daniel Dunbar833b03b2008-08-06 05:08:45 +0000364 return RValue::get(Val);
Lauro Ramos Venanciob40307c2008-01-22 20:17:04 +0000365}
366
Daniel Dunbare6c31752008-08-29 08:11:39 +0000367RValue CodeGenFunction::EmitLoadOfPropertyRefLValue(LValue LV,
368 QualType ExprType) {
369 return EmitObjCPropertyGet(LV.getPropertyRefExpr());
370}
371
Fariborz Jahanianb0973da2008-11-22 22:30:21 +0000372RValue CodeGenFunction::EmitLoadOfKVCRefLValue(LValue LV,
373 QualType ExprType) {
374 return EmitObjCPropertyGet(LV.getKVCRefExpr());
375}
376
Nate Begeman7903d052009-01-18 06:42:49 +0000377// If this is a reference to a subset of the elements of a vector, create an
378// appropriate shufflevector.
Nate Begemanaf6ed502008-04-18 23:10:10 +0000379RValue CodeGenFunction::EmitLoadOfExtVectorElementLValue(LValue LV,
380 QualType ExprType) {
Eli Friedman2e630542008-06-13 23:01:12 +0000381 llvm::Value *Vec = Builder.CreateLoad(LV.getExtVectorAddr(),
382 LV.isVolatileQualified(), "tmp");
Chris Lattner944f7962007-08-03 16:18:34 +0000383
Nate Begemanc8e51f82008-05-09 06:41:27 +0000384 const llvm::Constant *Elts = LV.getExtVectorElts();
Chris Lattner944f7962007-08-03 16:18:34 +0000385
386 // If the result of the expression is a non-vector type, we must be
387 // extracting a single element. Just codegen as an extractelement.
Chris Lattner4b492962007-08-10 17:10:08 +0000388 const VectorType *ExprVT = ExprType->getAsVectorType();
389 if (!ExprVT) {
Dan Gohman4751a3a2008-05-22 00:50:06 +0000390 unsigned InIdx = getAccessedFieldNo(0, Elts);
Chris Lattner944f7962007-08-03 16:18:34 +0000391 llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx);
392 return RValue::get(Builder.CreateExtractElement(Vec, Elt, "tmp"));
393 }
Nate Begeman7903d052009-01-18 06:42:49 +0000394
395 // Always use shuffle vector to try to retain the original program structure
Chris Lattner4b492962007-08-10 17:10:08 +0000396 unsigned NumResultElts = ExprVT->getNumElements();
Chris Lattner944f7962007-08-03 16:18:34 +0000397
Nate Begeman7903d052009-01-18 06:42:49 +0000398 llvm::SmallVector<llvm::Constant*, 4> Mask;
Chris Lattner944f7962007-08-03 16:18:34 +0000399 for (unsigned i = 0; i != NumResultElts; ++i) {
Dan Gohman4751a3a2008-05-22 00:50:06 +0000400 unsigned InIdx = getAccessedFieldNo(i, Elts);
Nate Begeman7903d052009-01-18 06:42:49 +0000401 Mask.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx));
Chris Lattner944f7962007-08-03 16:18:34 +0000402 }
403
Nate Begeman7903d052009-01-18 06:42:49 +0000404 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
405 Vec = Builder.CreateShuffleVector(Vec,
406 llvm::UndefValue::get(Vec->getType()),
407 MaskV, "tmp");
408 return RValue::get(Vec);
Chris Lattner944f7962007-08-03 16:18:34 +0000409}
410
411
Chris Lattner4b009652007-07-25 00:24:17 +0000412
413/// EmitStoreThroughLValue - Store the specified rvalue into the specified
414/// lvalue, where both are guaranteed to the have the same type, and that type
415/// is 'Ty'.
416void CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst,
417 QualType Ty) {
Chris Lattner5bfdd232007-08-03 16:28:33 +0000418 if (!Dst.isSimple()) {
419 if (Dst.isVectorElt()) {
420 // Read/modify/write the vector, inserting the new element.
Eli Friedman2e630542008-06-13 23:01:12 +0000421 llvm::Value *Vec = Builder.CreateLoad(Dst.getVectorAddr(),
422 Dst.isVolatileQualified(), "tmp");
Chris Lattnere24c4cf2007-08-31 22:49:20 +0000423 Vec = Builder.CreateInsertElement(Vec, Src.getScalarVal(),
Chris Lattner5bfdd232007-08-03 16:28:33 +0000424 Dst.getVectorIdx(), "vecins");
Eli Friedman2e630542008-06-13 23:01:12 +0000425 Builder.CreateStore(Vec, Dst.getVectorAddr(),Dst.isVolatileQualified());
Chris Lattner5bfdd232007-08-03 16:28:33 +0000426 return;
427 }
Chris Lattner4b009652007-07-25 00:24:17 +0000428
Nate Begemanaf6ed502008-04-18 23:10:10 +0000429 // If this is an update of extended vector elements, insert them as
430 // appropriate.
431 if (Dst.isExtVectorElt())
432 return EmitStoreThroughExtVectorComponentLValue(Src, Dst, Ty);
Lauro Ramos Venancio2d7a34c2008-01-22 22:36:45 +0000433
434 if (Dst.isBitfield())
435 return EmitStoreThroughBitfieldLValue(Src, Dst, Ty);
436
Daniel Dunbare6c31752008-08-29 08:11:39 +0000437 if (Dst.isPropertyRef())
438 return EmitStoreThroughPropertyRefLValue(Src, Dst, Ty);
439
Fariborz Jahanianb0973da2008-11-22 22:30:21 +0000440 if (Dst.isKVCRef())
441 return EmitStoreThroughKVCRefLValue(Src, Dst, Ty);
442
Lauro Ramos Venancio14d39842008-01-22 22:38:35 +0000443 assert(0 && "Unknown LValue type");
Chris Lattner5bfdd232007-08-03 16:28:33 +0000444 }
Chris Lattner4b009652007-07-25 00:24:17 +0000445
Fariborz Jahaniana4c010e2009-02-21 00:30:43 +0000446 if (Dst.isObjCWeak() && !Dst.isNonGC()) {
Fariborz Jahaniand2f661a2008-11-19 17:34:06 +0000447 // load of a __weak object.
448 llvm::Value *LvalueDst = Dst.getAddress();
449 llvm::Value *src = Src.getScalarVal();
Mike Stumpf41021d2009-04-14 00:57:29 +0000450 CGM.getObjCRuntime().EmitObjCWeakAssign(*this, src, LvalueDst);
Fariborz Jahaniand2f661a2008-11-19 17:34:06 +0000451 return;
452 }
453
Fariborz Jahaniana4c010e2009-02-21 00:30:43 +0000454 if (Dst.isObjCStrong() && !Dst.isNonGC()) {
Fariborz Jahaniand2f661a2008-11-19 17:34:06 +0000455 // load of a __strong object.
456 llvm::Value *LvalueDst = Dst.getAddress();
457 llvm::Value *src = Src.getScalarVal();
Fariborz Jahanian5a0c3412009-02-19 18:29:24 +0000458#if 0
Mike Stumpba2cb0e2009-05-16 07:57:57 +0000459 // FIXME. We cannot positively determine if we have an 'ivar' assignment,
460 // object assignment or an unknown assignment. For now, generate call to
461 // objc_assign_strongCast assignment which is a safe, but consevative
462 // assumption.
Fariborz Jahanian70522662008-11-20 20:53:20 +0000463 if (Dst.isObjCIvar())
464 CGM.getObjCRuntime().EmitObjCIvarAssign(*this, src, LvalueDst);
465 else
466 CGM.getObjCRuntime().EmitObjCGlobalAssign(*this, src, LvalueDst);
Fariborz Jahanian5a0c3412009-02-19 18:29:24 +0000467#endif
Fariborz Jahanian955b39c2009-05-04 23:27:20 +0000468 if (Dst.isGlobalObjCRef())
469 CGM.getObjCRuntime().EmitObjCGlobalAssign(*this, src, LvalueDst);
470 else
471 CGM.getObjCRuntime().EmitObjCStrongCastAssign(*this, src, LvalueDst);
Fariborz Jahaniand2f661a2008-11-19 17:34:06 +0000472 return;
473 }
474
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000475 assert(Src.isScalar() && "Can't emit an agg store with this method");
Anders Carlsson05dfa992009-05-19 18:50:41 +0000476 EmitStoreOfScalar(Src.getScalarVal(), Dst.getAddress(),
477 Dst.isVolatileQualified(), Ty);
Chris Lattner4b009652007-07-25 00:24:17 +0000478}
479
Lauro Ramos Venancio2d7a34c2008-01-22 22:36:45 +0000480void CodeGenFunction::EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst,
Daniel Dunbar2668dd12008-11-19 09:36:46 +0000481 QualType Ty,
482 llvm::Value **Result) {
Daniel Dunbar833b03b2008-08-06 05:08:45 +0000483 unsigned StartBit = Dst.getBitfieldStartBit();
484 unsigned BitfieldSize = Dst.getBitfieldSize();
Lauro Ramos Venancio2d7a34c2008-01-22 22:36:45 +0000485 llvm::Value *Ptr = Dst.getBitfieldAddr();
Lauro Ramos Venancio2d7a34c2008-01-22 22:36:45 +0000486
Daniel Dunbar833b03b2008-08-06 05:08:45 +0000487 const llvm::Type *EltTy =
488 cast<llvm::PointerType>(Ptr->getType())->getElementType();
489 unsigned EltTySize = CGM.getTargetData().getTypeSizeInBits(EltTy);
490
491 // Get the new value, cast to the appropriate type and masked to
492 // exactly the size of the bit-field.
Daniel Dunbar2668dd12008-11-19 09:36:46 +0000493 llvm::Value *SrcVal = Src.getScalarVal();
494 llvm::Value *NewVal = Builder.CreateIntCast(SrcVal, EltTy, false, "tmp");
Daniel Dunbar833b03b2008-08-06 05:08:45 +0000495 llvm::Constant *Mask =
496 llvm::ConstantInt::get(llvm::APInt::getLowBitsSet(EltTySize, BitfieldSize));
497 NewVal = Builder.CreateAnd(NewVal, Mask, "bf.value");
Lauro Ramos Venancio2d7a34c2008-01-22 22:36:45 +0000498
Daniel Dunbar2668dd12008-11-19 09:36:46 +0000499 // Return the new value of the bit-field, if requested.
500 if (Result) {
501 // Cast back to the proper type for result.
502 const llvm::Type *SrcTy = SrcVal->getType();
503 llvm::Value *SrcTrunc = Builder.CreateIntCast(NewVal, SrcTy, false,
504 "bf.reload.val");
505
506 // Sign extend if necessary.
507 if (Dst.isBitfieldSigned()) {
508 unsigned SrcTySize = CGM.getTargetData().getTypeSizeInBits(SrcTy);
509 llvm::Value *ExtraBits = llvm::ConstantInt::get(SrcTy,
510 SrcTySize - BitfieldSize);
511 SrcTrunc = Builder.CreateAShr(Builder.CreateShl(SrcTrunc, ExtraBits),
512 ExtraBits, "bf.reload.sext");
513 }
514
515 *Result = SrcTrunc;
516 }
517
Daniel Dunbar833b03b2008-08-06 05:08:45 +0000518 // In some cases the bitfield may straddle two memory locations.
519 // Emit the low part first and check to see if the high needs to be
520 // done.
521 unsigned LowBits = std::min(BitfieldSize, EltTySize - StartBit);
522 llvm::Value *LowVal = Builder.CreateLoad(Ptr, Dst.isVolatileQualified(),
523 "bf.prev.low");
Eli Friedmana04e70d2008-05-17 20:03:47 +0000524
Daniel Dunbar833b03b2008-08-06 05:08:45 +0000525 // Compute the mask for zero-ing the low part of this bitfield.
526 llvm::Constant *InvMask =
527 llvm::ConstantInt::get(~llvm::APInt::getBitsSet(EltTySize, StartBit,
528 StartBit + LowBits));
529
530 // Compute the new low part as
531 // LowVal = (LowVal & InvMask) | (NewVal << StartBit),
532 // with the shift of NewVal implicitly stripping the high bits.
533 llvm::Value *NewLowVal =
534 Builder.CreateShl(NewVal, llvm::ConstantInt::get(EltTy, StartBit),
535 "bf.value.lo");
536 LowVal = Builder.CreateAnd(LowVal, InvMask, "bf.prev.lo.cleared");
537 LowVal = Builder.CreateOr(LowVal, NewLowVal, "bf.new.lo");
538
539 // Write back.
540 Builder.CreateStore(LowVal, Ptr, Dst.isVolatileQualified());
Eli Friedmana04e70d2008-05-17 20:03:47 +0000541
Daniel Dunbar833b03b2008-08-06 05:08:45 +0000542 // If the low part doesn't cover the bitfield emit a high part.
543 if (LowBits < BitfieldSize) {
544 unsigned HighBits = BitfieldSize - LowBits;
545 llvm::Value *HighPtr =
546 Builder.CreateGEP(Ptr, llvm::ConstantInt::get(llvm::Type::Int32Ty, 1),
547 "bf.ptr.hi");
548 llvm::Value *HighVal = Builder.CreateLoad(HighPtr,
549 Dst.isVolatileQualified(),
550 "bf.prev.hi");
551
552 // Compute the mask for zero-ing the high part of this bitfield.
553 llvm::Constant *InvMask =
554 llvm::ConstantInt::get(~llvm::APInt::getLowBitsSet(EltTySize, HighBits));
555
556 // Compute the new high part as
557 // HighVal = (HighVal & InvMask) | (NewVal lshr LowBits),
558 // where the high bits of NewVal have already been cleared and the
559 // shift stripping the low bits.
560 llvm::Value *NewHighVal =
561 Builder.CreateLShr(NewVal, llvm::ConstantInt::get(EltTy, LowBits),
562 "bf.value.high");
563 HighVal = Builder.CreateAnd(HighVal, InvMask, "bf.prev.hi.cleared");
564 HighVal = Builder.CreateOr(HighVal, NewHighVal, "bf.new.hi");
565
566 // Write back.
567 Builder.CreateStore(HighVal, HighPtr, Dst.isVolatileQualified());
568 }
Lauro Ramos Venancio2d7a34c2008-01-22 22:36:45 +0000569}
570
Daniel Dunbare6c31752008-08-29 08:11:39 +0000571void CodeGenFunction::EmitStoreThroughPropertyRefLValue(RValue Src,
572 LValue Dst,
573 QualType Ty) {
574 EmitObjCPropertySet(Dst.getPropertyRefExpr(), Src);
575}
576
Fariborz Jahanianb0973da2008-11-22 22:30:21 +0000577void CodeGenFunction::EmitStoreThroughKVCRefLValue(RValue Src,
578 LValue Dst,
579 QualType Ty) {
580 EmitObjCPropertySet(Dst.getKVCRefExpr(), Src);
581}
582
Nate Begemanaf6ed502008-04-18 23:10:10 +0000583void CodeGenFunction::EmitStoreThroughExtVectorComponentLValue(RValue Src,
584 LValue Dst,
585 QualType Ty) {
Chris Lattner5bfdd232007-08-03 16:28:33 +0000586 // This access turns into a read/modify/write of the vector. Load the input
587 // value now.
Eli Friedman2e630542008-06-13 23:01:12 +0000588 llvm::Value *Vec = Builder.CreateLoad(Dst.getExtVectorAddr(),
589 Dst.isVolatileQualified(), "tmp");
Nate Begemanc8e51f82008-05-09 06:41:27 +0000590 const llvm::Constant *Elts = Dst.getExtVectorElts();
Chris Lattner5bfdd232007-08-03 16:28:33 +0000591
Chris Lattnere24c4cf2007-08-31 22:49:20 +0000592 llvm::Value *SrcVal = Src.getScalarVal();
Chris Lattner5bfdd232007-08-03 16:28:33 +0000593
Chris Lattner940966d2007-08-03 16:37:04 +0000594 if (const VectorType *VTy = Ty->getAsVectorType()) {
595 unsigned NumSrcElts = VTy->getNumElements();
Nate Begeman7903d052009-01-18 06:42:49 +0000596 unsigned NumDstElts =
597 cast<llvm::VectorType>(Vec->getType())->getNumElements();
598 if (NumDstElts == NumSrcElts) {
599 // Use shuffle vector is the src and destination are the same number
600 // of elements
601 llvm::SmallVector<llvm::Constant*, 4> Mask;
602 for (unsigned i = 0; i != NumSrcElts; ++i) {
603 unsigned InIdx = getAccessedFieldNo(i, Elts);
604 Mask.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx));
605 }
606
607 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
608 Vec = Builder.CreateShuffleVector(SrcVal,
609 llvm::UndefValue::get(Vec->getType()),
610 MaskV, "tmp");
611 }
612 else if (NumDstElts > NumSrcElts) {
613 // Extended the source vector to the same length and then shuffle it
614 // into the destination.
615 // FIXME: since we're shuffling with undef, can we just use the indices
616 // into that? This could be simpler.
617 llvm::SmallVector<llvm::Constant*, 4> ExtMask;
618 unsigned i;
619 for (i = 0; i != NumSrcElts; ++i)
620 ExtMask.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, i));
621 for (; i != NumDstElts; ++i)
622 ExtMask.push_back(llvm::UndefValue::get(llvm::Type::Int32Ty));
623 llvm::Value *ExtMaskV = llvm::ConstantVector::get(&ExtMask[0],
624 ExtMask.size());
Daniel Dunbar6a3b16e2009-02-17 18:31:04 +0000625 llvm::Value *ExtSrcVal =
626 Builder.CreateShuffleVector(SrcVal,
627 llvm::UndefValue::get(SrcVal->getType()),
628 ExtMaskV, "tmp");
Nate Begeman7903d052009-01-18 06:42:49 +0000629 // build identity
630 llvm::SmallVector<llvm::Constant*, 4> Mask;
631 for (unsigned i = 0; i != NumDstElts; ++i) {
632 Mask.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, i));
633 }
634 // modify when what gets shuffled in
635 for (unsigned i = 0; i != NumSrcElts; ++i) {
636 unsigned Idx = getAccessedFieldNo(i, Elts);
637 Mask[Idx] =llvm::ConstantInt::get(llvm::Type::Int32Ty, i+NumDstElts);
638 }
639 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
640 Vec = Builder.CreateShuffleVector(Vec, ExtSrcVal, MaskV, "tmp");
641 }
642 else {
643 // We should never shorten the vector
644 assert(0 && "unexpected shorten vector length");
Chris Lattner940966d2007-08-03 16:37:04 +0000645 }
646 } else {
647 // If the Src is a scalar (not a vector) it must be updating one element.
Dan Gohman4751a3a2008-05-22 00:50:06 +0000648 unsigned InIdx = getAccessedFieldNo(0, Elts);
Chris Lattner5bfdd232007-08-03 16:28:33 +0000649 llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx);
650 Vec = Builder.CreateInsertElement(Vec, SrcVal, Elt, "tmp");
Chris Lattner5bfdd232007-08-03 16:28:33 +0000651 }
652
Eli Friedman2e630542008-06-13 23:01:12 +0000653 Builder.CreateStore(Vec, Dst.getExtVectorAddr(), Dst.isVolatileQualified());
Chris Lattner5bfdd232007-08-03 16:28:33 +0000654}
655
Chris Lattner4b009652007-07-25 00:24:17 +0000656LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) {
Steve Naroff72a6ebc2008-04-15 22:42:06 +0000657 const VarDecl *VD = dyn_cast<VarDecl>(E->getDecl());
658
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000659 if (VD && (VD->isBlockVarDecl() || isa<ParmVarDecl>(VD) ||
660 isa<ImplicitParamDecl>(VD))) {
Fariborz Jahanianf0ca65f2008-11-20 00:15:42 +0000661 LValue LV;
Mike Stump1214b582009-04-14 18:24:37 +0000662 bool GCable = VD->hasLocalStorage() && !VD->hasAttr<BlocksAttr>();
Daniel Dunbar644c15e2009-04-14 02:25:56 +0000663 if (VD->hasExternalStorage()) {
Anders Carlsson474a2a92009-05-19 20:40:02 +0000664 llvm::Value *V = CGM.GetAddrOfGlobalVar(VD);
665 if (VD->getType()->isReferenceType())
666 V = Builder.CreateLoad(V, "tmp");
667 LV = LValue::MakeAddr(V, E->getType().getCVRQualifiers(),
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +0000668 getContext().getObjCGCAttrKind(E->getType()));
Fariborz Jahanianf0ca65f2008-11-20 00:15:42 +0000669 }
Lauro Ramos Venancio2348d972008-02-16 22:30:38 +0000670 else {
Steve Naroff72a6ebc2008-04-15 22:42:06 +0000671 llvm::Value *V = LocalDeclMap[VD];
Mike Stump2b6933f2009-02-28 09:07:16 +0000672 assert(V && "DeclRefExpr not entered in LocalDeclMap?");
Fariborz Jahanian4d41b952009-02-20 01:14:43 +0000673 // local variables do not get their gc attribute set.
674 QualType::GCAttrTypes attr = QualType::GCNone;
675 // local static?
Mike Stumpf41021d2009-04-14 00:57:29 +0000676 if (!GCable)
Fariborz Jahanian4d41b952009-02-20 01:14:43 +0000677 attr = getContext().getObjCGCAttrKind(E->getType());
Daniel Dunbar78582862009-04-13 21:08:27 +0000678 if (VD->hasAttr<BlocksAttr>()) {
Mike Stumpad9605d2009-03-04 03:23:46 +0000679 bool needsCopyDispose = BlockRequiresCopying(VD->getType());
680 const llvm::Type *PtrStructTy = V->getType();
681 const llvm::Type *Ty = PtrStructTy;
682 Ty = llvm::PointerType::get(Ty, 0);
683 V = Builder.CreateStructGEP(V, 1, "forwarding");
684 V = Builder.CreateBitCast(V, Ty);
685 V = Builder.CreateLoad(V, false);
686 V = Builder.CreateBitCast(V, PtrStructTy);
687 V = Builder.CreateStructGEP(V, needsCopyDispose*2 + 4, "x");
688 }
Anders Carlsson474a2a92009-05-19 20:40:02 +0000689 if (VD->getType()->isReferenceType())
690 V = Builder.CreateLoad(V, "tmp");
Fariborz Jahanian4d41b952009-02-20 01:14:43 +0000691 LV = LValue::MakeAddr(V, E->getType().getCVRQualifiers(), attr);
Lauro Ramos Venancio2348d972008-02-16 22:30:38 +0000692 }
Mike Stumpf41021d2009-04-14 00:57:29 +0000693 LValue::SetObjCNonGC(LV, GCable);
Fariborz Jahanianf0ca65f2008-11-20 00:15:42 +0000694 return LV;
Steve Naroff72a6ebc2008-04-15 22:42:06 +0000695 } else if (VD && VD->isFileVarDecl()) {
Anders Carlsson474a2a92009-05-19 20:40:02 +0000696 llvm::Value *V = CGM.GetAddrOfGlobalVar(VD);
697 if (VD->getType()->isReferenceType())
698 V = Builder.CreateLoad(V, "tmp");
699 LValue LV = LValue::MakeAddr(V, E->getType().getCVRQualifiers(),
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +0000700 getContext().getObjCGCAttrKind(E->getType()));
Fariborz Jahanian955b39c2009-05-04 23:27:20 +0000701 if (LV.isObjCStrong())
702 LV.SetGlobalObjCRef(LV, true);
Fariborz Jahanianc192d4d2008-11-18 20:18:11 +0000703 return LV;
Steve Naroff72a6ebc2008-04-15 22:42:06 +0000704 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(E->getDecl())) {
Chris Lattner80f39cc2009-05-12 21:21:08 +0000705 return LValue::MakeAddr(CGM.GetAddrOfFunction(GlobalDecl(FD)),
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +0000706 E->getType().getCVRQualifiers(),
707 getContext().getObjCGCAttrKind(E->getType()));
Chris Lattner4b009652007-07-25 00:24:17 +0000708 }
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000709 else if (const ImplicitParamDecl *IPD =
710 dyn_cast<ImplicitParamDecl>(E->getDecl())) {
711 llvm::Value *V = LocalDeclMap[IPD];
712 assert(V && "BlockVarDecl not entered in LocalDeclMap?");
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +0000713 return LValue::MakeAddr(V, E->getType().getCVRQualifiers(),
714 getContext().getObjCGCAttrKind(E->getType()));
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000715 }
Chris Lattner4b009652007-07-25 00:24:17 +0000716 assert(0 && "Unimp declref");
Chris Lattner1d2b4612007-09-16 19:23:47 +0000717 //an invalid LValue, but the assert will
718 //ensure that this point is never reached.
719 return LValue();
Chris Lattner4b009652007-07-25 00:24:17 +0000720}
721
Mike Stump2b6933f2009-02-28 09:07:16 +0000722LValue CodeGenFunction::EmitBlockDeclRefLValue(const BlockDeclRefExpr *E) {
723 return LValue::MakeAddr(GetAddrOfBlockDecl(E), 0);
724}
725
Chris Lattner4b009652007-07-25 00:24:17 +0000726LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) {
727 // __extension__ doesn't affect lvalue-ness.
728 if (E->getOpcode() == UnaryOperator::Extension)
729 return EmitLValue(E->getSubExpr());
730
Chris Lattnerc154ac12008-07-26 22:37:01 +0000731 QualType ExprTy = getContext().getCanonicalType(E->getSubExpr()->getType());
Chris Lattner5bf72022007-10-30 22:53:42 +0000732 switch (E->getOpcode()) {
733 default: assert(0 && "Unknown unary operator lvalue!");
734 case UnaryOperator::Deref:
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +0000735 {
Chris Lattnerc5d32632009-02-24 22:18:39 +0000736 QualType T =
737 E->getSubExpr()->getType()->getAsPointerType()->getPointeeType();
Fariborz Jahanian2224fb22009-02-23 18:59:50 +0000738 LValue LV = LValue::MakeAddr(EmitScalarExpr(E->getSubExpr()),
739 ExprTy->getAsPointerType()->getPointeeType()
Fariborz Jahanian4d41b952009-02-20 01:14:43 +0000740 .getCVRQualifiers(),
Fariborz Jahanian2224fb22009-02-23 18:59:50 +0000741 getContext().getObjCGCAttrKind(T));
742 // We should not generate __weak write barrier on indirect reference
743 // of a pointer to object; as in void foo (__weak id *param); *param = 0;
744 // But, we continue to generate __strong write barrier on indirect write
745 // into a pointer to object.
746 if (getContext().getLangOptions().ObjC1 &&
747 getContext().getLangOptions().getGCMode() != LangOptions::NonGC &&
748 LV.isObjCWeak())
749 LValue::SetObjCNonGC(LV, !E->isOBJCGCCandidate());
750 return LV;
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +0000751 }
Chris Lattner5bf72022007-10-30 22:53:42 +0000752 case UnaryOperator::Real:
753 case UnaryOperator::Imag:
754 LValue LV = EmitLValue(E->getSubExpr());
Chris Lattner07307562008-03-19 05:19:41 +0000755 unsigned Idx = E->getOpcode() == UnaryOperator::Imag;
756 return LValue::MakeAddr(Builder.CreateStructGEP(LV.getAddress(),
Chris Lattnerd5a56aa2008-07-26 22:17:49 +0000757 Idx, "idx"),
758 ExprTy.getCVRQualifiers());
Chris Lattner5bf72022007-10-30 22:53:42 +0000759 }
Chris Lattner4b009652007-07-25 00:24:17 +0000760}
761
762LValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) {
Daniel Dunbar31fe9c32008-08-13 23:20:05 +0000763 return LValue::MakeAddr(CGM.GetAddrOfConstantStringFromLiteral(E), 0);
Chris Lattner4b009652007-07-25 00:24:17 +0000764}
765
Chris Lattnerc5d32632009-02-24 22:18:39 +0000766LValue CodeGenFunction::EmitObjCEncodeExprLValue(const ObjCEncodeExpr *E) {
767 return LValue::MakeAddr(CGM.GetAddrOfConstantStringFromObjCEncode(E), 0);
768}
769
770
Daniel Dunbara9f0be22008-10-17 21:58:32 +0000771LValue CodeGenFunction::EmitPredefinedFunctionName(unsigned Type) {
Chris Lattner4b009652007-07-25 00:24:17 +0000772 std::string GlobalVarName;
Daniel Dunbara9f0be22008-10-17 21:58:32 +0000773
774 switch (Type) {
Chris Lattnerc5d32632009-02-24 22:18:39 +0000775 default:
776 assert(0 && "Invalid type");
777 case PredefinedExpr::Func:
778 GlobalVarName = "__func__.";
779 break;
780 case PredefinedExpr::Function:
781 GlobalVarName = "__FUNCTION__.";
782 break;
783 case PredefinedExpr::PrettyFunction:
784 // FIXME:: Demangle C++ method names
785 GlobalVarName = "__PRETTY_FUNCTION__.";
786 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000787 }
Daniel Dunbara9f0be22008-10-17 21:58:32 +0000788
Chris Lattnerf6279ae2009-04-23 05:30:27 +0000789 // FIXME: This isn't right at all. The logic for computing this should go
790 // into a method on PredefinedExpr. This would allow sema and codegen to be
791 // consistent for things like sizeof(__func__) etc.
Daniel Dunbara9f0be22008-10-17 21:58:32 +0000792 std::string FunctionName;
Chris Lattnerf6279ae2009-04-23 05:30:27 +0000793 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CurCodeDecl)) {
Douglas Gregor3c3c4542009-02-18 23:53:56 +0000794 FunctionName = CGM.getMangledName(FD);
Daniel Dunbara9f0be22008-10-17 21:58:32 +0000795 } else {
Daniel Dunbara2d275d2009-04-07 05:48:37 +0000796 // Just get the mangled name; skipping the asm prefix if it
797 // exists.
Daniel Dunbara9f0be22008-10-17 21:58:32 +0000798 FunctionName = CurFn->getName();
Daniel Dunbara2d275d2009-04-07 05:48:37 +0000799 if (FunctionName[0] == '\01')
800 FunctionName = FunctionName.substr(1, std::string::npos);
Daniel Dunbara9f0be22008-10-17 21:58:32 +0000801 }
802
Chris Lattner6e6a5972008-04-04 04:07:35 +0000803 GlobalVarName += FunctionName;
Daniel Dunbara9f0be22008-10-17 21:58:32 +0000804 llvm::Constant *C =
805 CGM.GetAddrOfConstantCString(FunctionName, GlobalVarName.c_str());
806 return LValue::MakeAddr(C, 0);
807}
808
809LValue CodeGenFunction::EmitPredefinedLValue(const PredefinedExpr *E) {
810 switch (E->getIdentType()) {
811 default:
812 return EmitUnsupportedLValue(E, "predefined expression");
813 case PredefinedExpr::Func:
814 case PredefinedExpr::Function:
815 case PredefinedExpr::PrettyFunction:
816 return EmitPredefinedFunctionName(E->getIdentType());
817 }
Chris Lattner4b009652007-07-25 00:24:17 +0000818}
819
820LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) {
Ted Kremenek1c1700f2007-08-20 16:18:38 +0000821 // The index must always be an integer, which is not an aggregate. Emit it.
Chris Lattner9fba49a2007-08-24 05:35:26 +0000822 llvm::Value *Idx = EmitScalarExpr(E->getIdx());
Chris Lattner4b009652007-07-25 00:24:17 +0000823
824 // If the base is a vector type, then we are forming a vector element lvalue
825 // with this subscript.
Eli Friedman2e630542008-06-13 23:01:12 +0000826 if (E->getBase()->getType()->isVectorType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000827 // Emit the vector as an lvalue to get its address.
Eli Friedman2e630542008-06-13 23:01:12 +0000828 LValue LHS = EmitLValue(E->getBase());
Ted Kremenek1c1700f2007-08-20 16:18:38 +0000829 assert(LHS.isSimple() && "Can only subscript lvalue vectors here!");
Chris Lattner4b009652007-07-25 00:24:17 +0000830 // FIXME: This should properly sign/zero/extend or truncate Idx to i32.
Eli Friedman2e630542008-06-13 23:01:12 +0000831 return LValue::MakeVectorElt(LHS.getAddress(), Idx,
832 E->getBase()->getType().getCVRQualifiers());
Chris Lattner4b009652007-07-25 00:24:17 +0000833 }
834
Ted Kremenek1c1700f2007-08-20 16:18:38 +0000835 // The base must be a pointer, which is not an aggregate. Emit it.
Chris Lattner9fba49a2007-08-24 05:35:26 +0000836 llvm::Value *Base = EmitScalarExpr(E->getBase());
Chris Lattner4b009652007-07-25 00:24:17 +0000837
Ted Kremenek1c1700f2007-08-20 16:18:38 +0000838 // Extend or truncate the index type to 32 or 64-bits.
Chris Lattner2af72ac2007-08-08 17:43:05 +0000839 QualType IdxTy = E->getIdx()->getType();
Chris Lattner4b009652007-07-25 00:24:17 +0000840 bool IdxSigned = IdxTy->isSignedIntegerType();
841 unsigned IdxBitwidth = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
Sanjiv Guptacee8fea2009-04-24 02:40:57 +0000842 if (IdxBitwidth != LLVMPointerWidth)
843 Idx = Builder.CreateIntCast(Idx, llvm::IntegerType::get(LLVMPointerWidth),
Chris Lattner4b009652007-07-25 00:24:17 +0000844 IdxSigned, "idxprom");
845
Daniel Dunbar6864c0d2009-04-25 05:08:32 +0000846 // We know that the pointer points to a type of the correct size,
847 // unless the size is a VLA or Objective-C interface.
848 llvm::Value *Address = 0;
Anders Carlsson3bb57e82008-12-21 00:11:23 +0000849 if (const VariableArrayType *VAT =
850 getContext().getAsVariableArrayType(E->getType())) {
851 llvm::Value *VLASize = VLASizeMap[VAT];
852
853 Idx = Builder.CreateMul(Idx, VLASize);
854
Anders Carlsson76d19c82008-12-21 03:44:36 +0000855 QualType BaseType = getContext().getBaseElementType(VAT);
Anders Carlsson3bb57e82008-12-21 00:11:23 +0000856
857 uint64_t BaseTypeSize = getContext().getTypeSize(BaseType) / 8;
858 Idx = Builder.CreateUDiv(Idx,
859 llvm::ConstantInt::get(Idx->getType(),
860 BaseTypeSize));
Daniel Dunbar6864c0d2009-04-25 05:08:32 +0000861 Address = Builder.CreateGEP(Base, Idx, "arrayidx");
862 } else if (const ObjCInterfaceType *OIT =
863 dyn_cast<ObjCInterfaceType>(E->getType())) {
864 llvm::Value *InterfaceSize =
865 llvm::ConstantInt::get(Idx->getType(),
866 getContext().getTypeSize(OIT) / 8);
867
868 Idx = Builder.CreateMul(Idx, InterfaceSize);
869
870 llvm::Type *i8PTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
871 Address = Builder.CreateGEP(Builder.CreateBitCast(Base, i8PTy),
872 Idx, "arrayidx");
873 Address = Builder.CreateBitCast(Address, Base->getType());
874 } else {
875 Address = Builder.CreateGEP(Base, Idx, "arrayidx");
Anders Carlsson3bb57e82008-12-21 00:11:23 +0000876 }
877
Daniel Dunbard4271f62009-04-18 08:54:40 +0000878 QualType T = E->getBase()->getType()->getAsPointerType()->getPointeeType();
Daniel Dunbar6864c0d2009-04-25 05:08:32 +0000879 LValue LV = LValue::MakeAddr(Address,
Daniel Dunbard4271f62009-04-18 08:54:40 +0000880 T.getCVRQualifiers(),
881 getContext().getObjCGCAttrKind(T));
Fariborz Jahanian1ff3c9d2009-02-21 23:37:19 +0000882 if (getContext().getLangOptions().ObjC1 &&
883 getContext().getLangOptions().getGCMode() != LangOptions::NonGC)
Fariborz Jahanian0c195b92009-02-22 18:40:18 +0000884 LValue::SetObjCNonGC(LV, !E->isOBJCGCCandidate());
Fariborz Jahanian1ff3c9d2009-02-21 23:37:19 +0000885 return LV;
Chris Lattner4b009652007-07-25 00:24:17 +0000886}
887
Nate Begemana1ae7442008-05-13 21:03:02 +0000888static
889llvm::Constant *GenerateConstantVector(llvm::SmallVector<unsigned, 4> &Elts) {
890 llvm::SmallVector<llvm::Constant *, 4> CElts;
891
892 for (unsigned i = 0, e = Elts.size(); i != e; ++i)
893 CElts.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, Elts[i]));
894
895 return llvm::ConstantVector::get(&CElts[0], CElts.size());
896}
897
Chris Lattner65520192007-08-02 23:37:31 +0000898LValue CodeGenFunction::
Nate Begemanaf6ed502008-04-18 23:10:10 +0000899EmitExtVectorElementExpr(const ExtVectorElementExpr *E) {
Chris Lattner65520192007-08-02 23:37:31 +0000900 // Emit the base vector as an l-value.
Chris Lattner09020ee2009-02-16 21:11:58 +0000901 LValue Base;
902
903 // ExtVectorElementExpr's base can either be a vector or pointer to vector.
Chris Lattner98e7fcc2009-02-16 22:14:05 +0000904 if (!E->isArrow()) {
Chris Lattner09020ee2009-02-16 21:11:58 +0000905 assert(E->getBase()->getType()->isVectorType());
906 Base = EmitLValue(E->getBase());
Chris Lattner98e7fcc2009-02-16 22:14:05 +0000907 } else {
908 const PointerType *PT = E->getBase()->getType()->getAsPointerType();
909 llvm::Value *Ptr = EmitScalarExpr(E->getBase());
910 Base = LValue::MakeAddr(Ptr, PT->getPointeeType().getCVRQualifiers());
Chris Lattner09020ee2009-02-16 21:11:58 +0000911 }
Chris Lattner65520192007-08-02 23:37:31 +0000912
Nate Begemana1ae7442008-05-13 21:03:02 +0000913 // Encode the element access list into a vector of unsigned indices.
914 llvm::SmallVector<unsigned, 4> Indices;
915 E->getEncodedElementAccess(Indices);
916
917 if (Base.isSimple()) {
918 llvm::Constant *CV = GenerateConstantVector(Indices);
Eli Friedman2e630542008-06-13 23:01:12 +0000919 return LValue::MakeExtVectorElt(Base.getAddress(), CV,
Chris Lattner9df79c32009-02-16 22:25:49 +0000920 Base.getQualifiers());
Nate Begemana1ae7442008-05-13 21:03:02 +0000921 }
922 assert(Base.isExtVectorElt() && "Can only subscript lvalue vec elts here!");
923
924 llvm::Constant *BaseElts = Base.getExtVectorElts();
925 llvm::SmallVector<llvm::Constant *, 4> CElts;
926
927 for (unsigned i = 0, e = Indices.size(); i != e; ++i) {
928 if (isa<llvm::ConstantAggregateZero>(BaseElts))
929 CElts.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, 0));
930 else
931 CElts.push_back(BaseElts->getOperand(Indices[i]));
932 }
933 llvm::Constant *CV = llvm::ConstantVector::get(&CElts[0], CElts.size());
Eli Friedman2e630542008-06-13 23:01:12 +0000934 return LValue::MakeExtVectorElt(Base.getExtVectorAddr(), CV,
Chris Lattner9df79c32009-02-16 22:25:49 +0000935 Base.getQualifiers());
Chris Lattner65520192007-08-02 23:37:31 +0000936}
937
Devang Patel41b66252007-10-23 20:28:39 +0000938LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) {
Devang Patele1f79db2007-12-11 21:33:16 +0000939 bool isUnion = false;
Fariborz Jahanian08c06f42008-11-21 18:14:01 +0000940 bool isIvar = false;
Fariborz Jahaniana4c010e2009-02-21 00:30:43 +0000941 bool isNonGC = false;
Devang Patel9dd3e2b2007-10-24 22:26:28 +0000942 Expr *BaseExpr = E->getBase();
Devang Patel9dd3e2b2007-10-24 22:26:28 +0000943 llvm::Value *BaseValue = NULL;
Eli Friedman2e630542008-06-13 23:01:12 +0000944 unsigned CVRQualifiers=0;
945
Chris Lattner659079e2007-12-02 18:52:07 +0000946 // 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 +0000947 if (E->isArrow()) {
Devang Patel2b24fd92007-10-26 18:15:21 +0000948 BaseValue = EmitScalarExpr(BaseExpr);
Devang Patele1f79db2007-12-11 21:33:16 +0000949 const PointerType *PTy =
Daniel Dunbard4271f62009-04-18 08:54:40 +0000950 BaseExpr->getType()->getAsPointerType();
Devang Patele1f79db2007-12-11 21:33:16 +0000951 if (PTy->getPointeeType()->isUnionType())
952 isUnion = true;
Eli Friedman2e630542008-06-13 23:01:12 +0000953 CVRQualifiers = PTy->getPointeeType().getCVRQualifiers();
Chris Lattner9df79c32009-02-16 22:25:49 +0000954 } else if (isa<ObjCPropertyRefExpr>(BaseExpr) ||
955 isa<ObjCKVCRefExpr>(BaseExpr)) {
Fariborz Jahanian4e881652009-01-12 23:27:26 +0000956 RValue RV = EmitObjCPropertyGet(BaseExpr);
957 BaseValue = RV.getAggregateAddr();
958 if (BaseExpr->getType()->isUnionType())
959 isUnion = true;
960 CVRQualifiers = BaseExpr->getType().getCVRQualifiers();
Chris Lattner9df79c32009-02-16 22:25:49 +0000961 } else {
Chris Lattner659079e2007-12-02 18:52:07 +0000962 LValue BaseLV = EmitLValue(BaseExpr);
Fariborz Jahanian08c06f42008-11-21 18:14:01 +0000963 if (BaseLV.isObjCIvar())
964 isIvar = true;
Fariborz Jahaniana4c010e2009-02-21 00:30:43 +0000965 if (BaseLV.isNonGC())
966 isNonGC = true;
Chris Lattner659079e2007-12-02 18:52:07 +0000967 // FIXME: this isn't right for bitfields.
968 BaseValue = BaseLV.getAddress();
Devang Patele1f79db2007-12-11 21:33:16 +0000969 if (BaseExpr->getType()->isUnionType())
970 isUnion = true;
Eli Friedman2e630542008-06-13 23:01:12 +0000971 CVRQualifiers = BaseExpr->getType().getCVRQualifiers();
Chris Lattner659079e2007-12-02 18:52:07 +0000972 }
Devang Patel41b66252007-10-23 20:28:39 +0000973
Douglas Gregor82d44772008-12-20 23:49:58 +0000974 FieldDecl *Field = dyn_cast<FieldDecl>(E->getMemberDecl());
975 // FIXME: Handle non-field member expressions
976 assert(Field && "No code generation for non-field member references");
Chris Lattner9df79c32009-02-16 22:25:49 +0000977 LValue MemExpLV = EmitLValueForField(BaseValue, Field, isUnion,
978 CVRQualifiers);
Fariborz Jahanian08c06f42008-11-21 18:14:01 +0000979 LValue::SetObjCIvar(MemExpLV, isIvar);
Fariborz Jahaniana4c010e2009-02-21 00:30:43 +0000980 LValue::SetObjCNonGC(MemExpLV, isNonGC);
Fariborz Jahanian08c06f42008-11-21 18:14:01 +0000981 return MemExpLV;
Eli Friedmand3550112008-02-09 08:50:58 +0000982}
Devang Patel41b66252007-10-23 20:28:39 +0000983
Fariborz Jahanian86008c02008-12-15 20:35:07 +0000984LValue CodeGenFunction::EmitLValueForBitfield(llvm::Value* BaseValue,
985 FieldDecl* Field,
Fariborz Jahanianc912eb72009-02-03 19:03:09 +0000986 unsigned CVRQualifiers) {
Daniel Dunbar6a3b16e2009-02-17 18:31:04 +0000987 unsigned idx = CGM.getTypes().getLLVMFieldNo(Field);
Mike Stumpba2cb0e2009-05-16 07:57:57 +0000988 // FIXME: CodeGenTypes should expose a method to get the appropriate type for
989 // FieldTy (the appropriate type is ABI-dependent).
Daniel Dunbar6a3b16e2009-02-17 18:31:04 +0000990 const llvm::Type *FieldTy =
991 CGM.getTypes().ConvertTypeForMem(Field->getType());
Fariborz Jahanian86008c02008-12-15 20:35:07 +0000992 const llvm::PointerType *BaseTy =
993 cast<llvm::PointerType>(BaseValue->getType());
994 unsigned AS = BaseTy->getAddressSpace();
995 BaseValue = Builder.CreateBitCast(BaseValue,
996 llvm::PointerType::get(FieldTy, AS),
997 "tmp");
998 llvm::Value *V = Builder.CreateGEP(BaseValue,
999 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx),
1000 "tmp");
1001
1002 CodeGenTypes::BitFieldInfo bitFieldInfo =
1003 CGM.getTypes().getBitFieldInfo(Field);
1004 return LValue::MakeBitfield(V, bitFieldInfo.Begin, bitFieldInfo.Size,
1005 Field->getType()->isSignedIntegerType(),
1006 Field->getType().getCVRQualifiers()|CVRQualifiers);
1007}
1008
Eli Friedmand3550112008-02-09 08:50:58 +00001009LValue CodeGenFunction::EmitLValueForField(llvm::Value* BaseValue,
1010 FieldDecl* Field,
Eli Friedman2e630542008-06-13 23:01:12 +00001011 bool isUnion,
1012 unsigned CVRQualifiers)
Eli Friedmand3550112008-02-09 08:50:58 +00001013{
Fariborz Jahanian86008c02008-12-15 20:35:07 +00001014 if (Field->isBitField())
Fariborz Jahanianc912eb72009-02-03 19:03:09 +00001015 return EmitLValueForBitfield(BaseValue, Field, CVRQualifiers);
Fariborz Jahanianf0ca65f2008-11-20 00:15:42 +00001016
Fariborz Jahanianc912eb72009-02-03 19:03:09 +00001017 unsigned idx = CGM.getTypes().getLLVMFieldNo(Field);
Fariborz Jahanian86008c02008-12-15 20:35:07 +00001018 llvm::Value *V = Builder.CreateStructGEP(BaseValue, idx, "tmp");
Eli Friedman66813742008-05-29 11:33:25 +00001019
Devang Patel9b1ca9e2007-10-26 19:42:18 +00001020 // Match union field type.
Lauro Ramos Venancio63fc38f2008-02-07 19:29:53 +00001021 if (isUnion) {
Eli Friedman2e630542008-06-13 23:01:12 +00001022 const llvm::Type *FieldTy =
1023 CGM.getTypes().ConvertTypeForMem(Field->getType());
Devang Patel0f2a8fb2007-10-30 20:59:40 +00001024 const llvm::PointerType * BaseTy =
1025 cast<llvm::PointerType>(BaseValue->getType());
Eli Friedmancecdc6b2008-05-21 13:24:44 +00001026 unsigned AS = BaseTy->getAddressSpace();
1027 V = Builder.CreateBitCast(V,
1028 llvm::PointerType::get(FieldTy, AS),
1029 "tmp");
Devang Patel9b1ca9e2007-10-26 19:42:18 +00001030 }
Lauro Ramos Venanciob40307c2008-01-22 20:17:04 +00001031
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +00001032 QualType::GCAttrTypes attr = QualType::GCNone;
Fariborz Jahanian80ff83c2009-02-18 17:52:36 +00001033 if (CGM.getLangOptions().ObjC1 &&
Fariborz Jahanian31804e12009-02-18 18:52:41 +00001034 CGM.getLangOptions().getGCMode() != LangOptions::NonGC) {
1035 QualType Ty = Field->getType();
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +00001036 attr = Ty.getObjCGCAttr();
Fariborz Jahaniancc59d472009-02-19 00:48:05 +00001037 if (attr != QualType::GCNone) {
Fariborz Jahanian31804e12009-02-18 18:52:41 +00001038 // __weak attribute on a field is ignored.
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +00001039 if (attr == QualType::Weak)
1040 attr = QualType::GCNone;
Fariborz Jahaniancc59d472009-02-19 00:48:05 +00001041 }
Fariborz Jahanian31804e12009-02-18 18:52:41 +00001042 else if (getContext().isObjCObjectPointerType(Ty))
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +00001043 attr = QualType::Strong;
Fariborz Jahanian31804e12009-02-18 18:52:41 +00001044 }
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +00001045 LValue LV =
1046 LValue::MakeAddr(V,
1047 Field->getType().getCVRQualifiers()|CVRQualifiers,
1048 attr);
Fariborz Jahanianf0ca65f2008-11-20 00:15:42 +00001049 return LV;
Devang Patel41b66252007-10-23 20:28:39 +00001050}
1051
Chris Lattner22523ba2009-03-18 18:28:57 +00001052LValue CodeGenFunction::EmitCompoundLiteralLValue(const CompoundLiteralExpr* E){
Eli Friedmanf3c2cb42008-05-13 23:18:27 +00001053 const llvm::Type *LTy = ConvertType(E->getType());
1054 llvm::Value *DeclPtr = CreateTempAlloca(LTy, ".compoundliteral");
1055
1056 const Expr* InitExpr = E->getInitializer();
Eli Friedman2e630542008-06-13 23:01:12 +00001057 LValue Result = LValue::MakeAddr(DeclPtr, E->getType().getCVRQualifiers());
Eli Friedmanf3c2cb42008-05-13 23:18:27 +00001058
1059 if (E->getType()->isComplexType()) {
1060 EmitComplexExprIntoAddr(InitExpr, DeclPtr, false);
1061 } else if (hasAggregateLLVMType(E->getType())) {
1062 EmitAnyExpr(InitExpr, DeclPtr, false);
1063 } else {
1064 EmitStoreThroughLValue(EmitAnyExpr(InitExpr), Result, E->getType());
1065 }
1066
1067 return Result;
1068}
1069
Daniel Dunbaraecb4932009-03-24 02:38:23 +00001070LValue CodeGenFunction::EmitConditionalOperator(const ConditionalOperator* E) {
1071 // We don't handle vectors yet.
1072 if (E->getType()->isVectorType())
1073 return EmitUnsupportedLValue(E, "conditional operator");
1074
1075 // ?: here should be an aggregate.
1076 assert((hasAggregateLLVMType(E->getType()) &&
1077 !E->getType()->isAnyComplexType()) &&
1078 "Unexpected conditional operator!");
1079
1080 llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType()));
1081 EmitAggExpr(E, Temp, false);
1082
1083 return LValue::MakeAddr(Temp, E->getType().getCVRQualifiers(),
1084 getContext().getObjCGCAttrKind(E->getType()));
1085
1086}
1087
Chris Lattner22523ba2009-03-18 18:28:57 +00001088/// EmitCastLValue - Casts are never lvalues. If a cast is needed by the code
1089/// generator in an lvalue context, then it must mean that we need the address
1090/// of an aggregate in order to access one of its fields. This can happen for
1091/// all the reasons that casts are permitted with aggregate result, including
1092/// noop aggregate casts, and cast from scalar to union.
1093LValue CodeGenFunction::EmitCastLValue(const CastExpr *E) {
1094 // If this is an aggregate-to-aggregate cast, just use the input's address as
1095 // the lvalue.
1096 if (getContext().hasSameUnqualifiedType(E->getType(),
1097 E->getSubExpr()->getType()))
1098 return EmitLValue(E->getSubExpr());
1099
1100 // Otherwise, we must have a cast from scalar to union.
1101 assert(E->getType()->isUnionType() && "Expected scalar-to-union cast");
1102
1103 // Casts are only lvalues when the source and destination types are the same.
1104 llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType()));
Chris Lattner96a3a2d2009-03-18 18:30:44 +00001105 EmitAnyExpr(E->getSubExpr(), Temp, false);
Chris Lattner22523ba2009-03-18 18:28:57 +00001106
1107 return LValue::MakeAddr(Temp, E->getType().getCVRQualifiers(),
1108 getContext().getObjCGCAttrKind(E->getType()));
1109}
1110
Chris Lattner4b009652007-07-25 00:24:17 +00001111//===--------------------------------------------------------------------===//
1112// Expression Emission
1113//===--------------------------------------------------------------------===//
1114
Chris Lattnerb2cb9cb2007-08-20 22:37:10 +00001115
Chris Lattner4b009652007-07-25 00:24:17 +00001116RValue CodeGenFunction::EmitCallExpr(const CallExpr *E) {
Daniel Dunbar191eb9e2009-02-20 18:06:48 +00001117 // Builtins never have block type.
Daniel Dunbare3a6a682009-01-09 16:50:52 +00001118 if (E->getCallee()->getType()->isBlockPointerType())
Anders Carlssond2a889b2009-02-12 00:39:25 +00001119 return EmitBlockCallExpr(E);
Daniel Dunbare3a6a682009-01-09 16:50:52 +00001120
Anders Carlsson7a9b2982009-04-03 22:50:24 +00001121 if (const CXXMemberCallExpr *CE = dyn_cast<CXXMemberCallExpr>(E))
1122 return EmitCXXMemberCallExpr(CE);
1123
Daniel Dunbar191eb9e2009-02-20 18:06:48 +00001124 const Decl *TargetDecl = 0;
Daniel Dunbar337f60a2009-02-20 19:34:33 +00001125 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E->getCallee())) {
1126 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CE->getSubExpr())) {
1127 TargetDecl = DRE->getDecl();
1128 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(TargetDecl))
1129 if (unsigned builtinID = FD->getBuiltinID(getContext()))
1130 return EmitBuiltinExpr(FD, builtinID, E);
Daniel Dunbar191eb9e2009-02-20 18:06:48 +00001131 }
1132 }
1133
Chris Lattner9fba49a2007-08-24 05:35:26 +00001134 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
Eli Friedman261f4ad2008-01-30 01:32:06 +00001135 return EmitCallExpr(Callee, E->getCallee()->getType(),
Daniel Dunbar191eb9e2009-02-20 18:06:48 +00001136 E->arg_begin(), E->arg_end(), TargetDecl);
Chris Lattner02c60f52007-08-31 04:44:06 +00001137}
1138
Daniel Dunbaref0d4c72008-09-04 03:20:13 +00001139LValue CodeGenFunction::EmitBinaryOperatorLValue(const BinaryOperator *E) {
Chris Lattnerb7062332009-05-12 21:28:12 +00001140 // Comma expressions just emit their LHS then their RHS as an l-value.
1141 if (E->getOpcode() == BinaryOperator::Comma) {
1142 EmitAnyExpr(E->getLHS());
1143 return EmitLValue(E->getRHS());
1144 }
1145
Daniel Dunbaref0d4c72008-09-04 03:20:13 +00001146 // Can only get l-value for binary operator expressions which are a
1147 // simple assignment of aggregate type.
1148 if (E->getOpcode() != BinaryOperator::Assign)
1149 return EmitUnsupportedLValue(E, "binary l-value expression");
1150
1151 llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType()));
1152 EmitAggExpr(E, Temp, false);
1153 // FIXME: Are these qualifiers correct?
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +00001154 return LValue::MakeAddr(Temp, E->getType().getCVRQualifiers(),
1155 getContext().getObjCGCAttrKind(E->getType()));
Daniel Dunbaref0d4c72008-09-04 03:20:13 +00001156}
1157
Christopher Lambad327ba2007-12-29 05:02:41 +00001158LValue CodeGenFunction::EmitCallExprLValue(const CallExpr *E) {
1159 // Can only get l-value for call expression returning aggregate type
1160 RValue RV = EmitCallExpr(E);
Eli Friedman2e630542008-06-13 23:01:12 +00001161 return LValue::MakeAddr(RV.getAggregateAddr(),
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +00001162 E->getType().getCVRQualifiers(),
1163 getContext().getObjCGCAttrKind(E->getType()));
Christopher Lambad327ba2007-12-29 05:02:41 +00001164}
1165
Daniel Dunbar95d08f22009-02-11 20:59:32 +00001166LValue CodeGenFunction::EmitVAArgExprLValue(const VAArgExpr *E) {
1167 // FIXME: This shouldn't require another copy.
1168 llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType()));
1169 EmitAggExpr(E, Temp, false);
1170 return LValue::MakeAddr(Temp, E->getType().getCVRQualifiers());
1171}
1172
Argiris Kirtzidisbf615b02008-09-10 02:36:38 +00001173LValue
1174CodeGenFunction::EmitCXXConditionDeclLValue(const CXXConditionDeclExpr *E) {
1175 EmitLocalBlockVarDecl(*E->getVarDecl());
1176 return EmitDeclRefLValue(E);
1177}
1178
Daniel Dunbar5e105892008-08-23 10:51:21 +00001179LValue CodeGenFunction::EmitObjCMessageExprLValue(const ObjCMessageExpr *E) {
1180 // Can only get l-value for message expression returning aggregate type
1181 RValue RV = EmitObjCMessageExpr(E);
1182 // FIXME: can this be volatile?
1183 return LValue::MakeAddr(RV.getAggregateAddr(),
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +00001184 E->getType().getCVRQualifiers(),
1185 getContext().getObjCGCAttrKind(E->getType()));
Daniel Dunbar5e105892008-08-23 10:51:21 +00001186}
1187
Daniel Dunbar61e14a62009-04-22 05:08:15 +00001188llvm::Value *CodeGenFunction::EmitIvarOffset(const ObjCInterfaceDecl *Interface,
Daniel Dunbare856ac22008-09-24 04:00:38 +00001189 const ObjCIvarDecl *Ivar) {
Fariborz Jahanian27cc6662009-02-10 19:02:04 +00001190 return CGM.getObjCRuntime().EmitIvarOffset(*this, Interface, Ivar);
Daniel Dunbare856ac22008-09-24 04:00:38 +00001191}
1192
Fariborz Jahanian55343922009-02-03 00:09:52 +00001193LValue CodeGenFunction::EmitLValueForIvar(QualType ObjectTy,
1194 llvm::Value *BaseValue,
Daniel Dunbare856ac22008-09-24 04:00:38 +00001195 const ObjCIvarDecl *Ivar,
1196 unsigned CVRQualifiers) {
Chris Lattner552914b2009-04-17 17:44:48 +00001197 return CGM.getObjCRuntime().EmitObjCValueForIvar(*this, ObjectTy, BaseValue,
Daniel Dunbarf5254bd2009-04-21 01:19:28 +00001198 Ivar, CVRQualifiers);
Daniel Dunbare856ac22008-09-24 04:00:38 +00001199}
1200
1201LValue CodeGenFunction::EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E) {
Anders Carlssonae61b002008-08-25 01:53:23 +00001202 // FIXME: A lot of the code below could be shared with EmitMemberExpr.
1203 llvm::Value *BaseValue = 0;
1204 const Expr *BaseExpr = E->getBase();
1205 unsigned CVRQualifiers = 0;
Fariborz Jahanian55343922009-02-03 00:09:52 +00001206 QualType ObjectTy;
Anders Carlssonae61b002008-08-25 01:53:23 +00001207 if (E->isArrow()) {
1208 BaseValue = EmitScalarExpr(BaseExpr);
Daniel Dunbard4271f62009-04-18 08:54:40 +00001209 const PointerType *PTy = BaseExpr->getType()->getAsPointerType();
Fariborz Jahanian55343922009-02-03 00:09:52 +00001210 ObjectTy = PTy->getPointeeType();
1211 CVRQualifiers = ObjectTy.getCVRQualifiers();
Anders Carlssonae61b002008-08-25 01:53:23 +00001212 } else {
1213 LValue BaseLV = EmitLValue(BaseExpr);
1214 // FIXME: this isn't right for bitfields.
1215 BaseValue = BaseLV.getAddress();
Fariborz Jahanian55343922009-02-03 00:09:52 +00001216 ObjectTy = BaseExpr->getType();
1217 CVRQualifiers = ObjectTy.getCVRQualifiers();
Anders Carlssonae61b002008-08-25 01:53:23 +00001218 }
Daniel Dunbare856ac22008-09-24 04:00:38 +00001219
Daniel Dunbarf5254bd2009-04-21 01:19:28 +00001220 return EmitLValueForIvar(ObjectTy, BaseValue, E->getDecl(), CVRQualifiers);
Chris Lattnerb326b172008-03-30 23:03:07 +00001221}
1222
Daniel Dunbare6c31752008-08-29 08:11:39 +00001223LValue
1224CodeGenFunction::EmitObjCPropertyRefLValue(const ObjCPropertyRefExpr *E) {
1225 // This is a special l-value that just issues sends when we load or
1226 // store through it.
1227 return LValue::MakePropertyRef(E, E->getType().getCVRQualifiers());
1228}
1229
Fariborz Jahanianb0973da2008-11-22 22:30:21 +00001230LValue
1231CodeGenFunction::EmitObjCKVCRefLValue(const ObjCKVCRefExpr *E) {
1232 // This is a special l-value that just issues sends when we load or
1233 // store through it.
1234 return LValue::MakeKVCRef(E, E->getType().getCVRQualifiers());
1235}
1236
Douglas Gregord8606632008-11-04 14:56:14 +00001237LValue
Chris Lattnereec3a592009-04-25 19:35:26 +00001238CodeGenFunction::EmitObjCSuperExprLValue(const ObjCSuperExpr *E) {
Douglas Gregord8606632008-11-04 14:56:14 +00001239 return EmitUnsupportedLValue(E, "use of super");
1240}
1241
Chris Lattnereec3a592009-04-25 19:35:26 +00001242LValue CodeGenFunction::EmitStmtExprLValue(const StmtExpr *E) {
1243
1244 // Can only get l-value for message expression returning aggregate type
1245 RValue RV = EmitAnyExprToTemp(E);
1246 // FIXME: can this be volatile?
1247 return LValue::MakeAddr(RV.getAggregateAddr(),
1248 E->getType().getCVRQualifiers(),
1249 getContext().getObjCGCAttrKind(E->getType()));
1250}
1251
1252
Daniel Dunbare3a6a682009-01-09 16:50:52 +00001253RValue CodeGenFunction::EmitCallExpr(llvm::Value *Callee, QualType CalleeType,
Ted Kremenek2719e982008-06-17 02:43:46 +00001254 CallExpr::const_arg_iterator ArgBeg,
Daniel Dunbar191eb9e2009-02-20 18:06:48 +00001255 CallExpr::const_arg_iterator ArgEnd,
1256 const Decl *TargetDecl) {
Daniel Dunbare3a6a682009-01-09 16:50:52 +00001257 // Get the actual function type. The callee type will always be a
1258 // pointer to function type or a block pointer type.
Anders Carlsson7acb3a42009-04-07 18:53:02 +00001259 assert(CalleeType->isFunctionPointerType() &&
1260 "Call must have function pointer type!");
1261
1262 QualType FnType = CalleeType->getAsPointerType()->getPointeeType();
1263 QualType ResultType = FnType->getAsFunctionType()->getResultType();
Daniel Dunbar0ed60b02008-08-30 03:02:31 +00001264
1265 CallArgList Args;
Anders Carlsson6759c4d2009-04-08 23:13:16 +00001266 EmitCallArgs(Args, FnType->getAsFunctionProtoType(), ArgBeg, ArgEnd);
Daniel Dunbar0ed60b02008-08-30 03:02:31 +00001267
Daniel Dunbar34bda882009-02-02 23:23:47 +00001268 return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args),
Daniel Dunbar191eb9e2009-02-20 18:06:48 +00001269 Callee, Args, TargetDecl);
Daniel Dunbara04840b2008-08-23 03:46:30 +00001270}