blob: bb9f20003ac18b55109090eea0ec27f6d3446905 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- CGExpr.cpp - Emit LLVM Code from Expressions ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit Expr nodes as LLVM code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenFunction.h"
15#include "CodeGenModule.h"
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000016#include "CGCall.h"
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +000017#include "CGObjCRuntime.h"
Daniel Dunbarde7fb842008-08-11 05:00:27 +000018#include "clang/AST/ASTContext.h"
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000019#include "clang/AST/DeclObjC.h"
Eli Friedman316bb1b2008-05-17 20:03:47 +000020#include "llvm/Target/TargetData.h"
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattnerf1466842009-03-22 00:24:14 +000032 if (!Builder.isNamePreserving())
33 Name = "";
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner9069fa22007-08-26 16:46:58 +000040 QualType BoolTy = getContext().BoolTy;
Chris Lattner9b2dc282008-04-04 16:54:41 +000041 if (!E->getType()->isAnyComplexType())
Chris Lattner9069fa22007-08-26 16:46:58 +000042 return EmitScalarConversion(EmitScalarExpr(E), E->getType(), BoolTy);
Reid Spencer5f016e22007-07-11 17:01:13 +000043
Chris Lattner9069fa22007-08-26 16:46:58 +000044 return EmitComplexToScalarConversion(EmitComplexExpr(E), E->getType(),BoolTy);
Reid Spencer5f016e22007-07-11 17:01:13 +000045}
46
Chris Lattner9b655512007-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 Lattner9b2dc282008-04-04 16:54:41 +000055 else if (E->getType()->isAnyComplexType())
Chris Lattner9b655512007-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 Dunbar46f45b92008-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 Carlsson4029ca72009-05-20 00:24:07 +000073RValue CodeGenFunction::EmitReferenceBindingToExpr(const Expr* E,
74 QualType DestType) {
Anders Carlsson4bbab922009-05-20 00:36:58 +000075 if (E->isLvalue(getContext()) == Expr::LV_Valid) {
76 // Emit the expr as an lvalue.
77 LValue LV = EmitLValue(E);
78 return RValue::get(LV.getAddress());
79 }
80
Anders Carlsson4029ca72009-05-20 00:24:07 +000081 CGM.ErrorUnsupported(E, "reference binding");
82 return GetUndefRValue(DestType);
83}
84
85
Dan Gohman4f8d1232008-05-22 00:50:06 +000086/// getAccessedFieldNo - Given an encoded value and a result number, return
87/// the input field number being accessed.
88unsigned CodeGenFunction::getAccessedFieldNo(unsigned Idx,
89 const llvm::Constant *Elts) {
90 if (isa<llvm::ConstantAggregateZero>(Elts))
91 return 0;
92
93 return cast<llvm::ConstantInt>(Elts->getOperand(Idx))->getZExtValue();
94}
95
Chris Lattner9b655512007-08-31 22:49:20 +000096
Reid Spencer5f016e22007-07-11 17:01:13 +000097//===----------------------------------------------------------------------===//
98// LValue Expression Emission
99//===----------------------------------------------------------------------===//
100
Daniel Dunbar13e81732009-02-05 07:09:07 +0000101RValue CodeGenFunction::GetUndefRValue(QualType Ty) {
102 if (Ty->isVoidType()) {
103 return RValue::get(0);
104 } else if (const ComplexType *CTy = Ty->getAsComplexType()) {
Daniel Dunbar8fa73ed2009-01-09 20:09:28 +0000105 const llvm::Type *EltTy = ConvertType(CTy->getElementType());
106 llvm::Value *U = llvm::UndefValue::get(EltTy);
107 return RValue::getComplex(std::make_pair(U, U));
Daniel Dunbar13e81732009-02-05 07:09:07 +0000108 } else if (hasAggregateLLVMType(Ty)) {
109 const llvm::Type *LTy = llvm::PointerType::getUnqual(ConvertType(Ty));
110 return RValue::getAggregate(llvm::UndefValue::get(LTy));
Daniel Dunbar8fa73ed2009-01-09 20:09:28 +0000111 } else {
Daniel Dunbar13e81732009-02-05 07:09:07 +0000112 return RValue::get(llvm::UndefValue::get(ConvertType(Ty)));
Daniel Dunbar8fa73ed2009-01-09 20:09:28 +0000113 }
Daniel Dunbarce1d38b2009-01-09 16:50:52 +0000114}
115
Daniel Dunbar13e81732009-02-05 07:09:07 +0000116RValue CodeGenFunction::EmitUnsupportedRValue(const Expr *E,
117 const char *Name) {
118 ErrorUnsupported(E, Name);
119 return GetUndefRValue(E->getType());
120}
121
Daniel Dunbar6ba82a42008-08-25 20:45:57 +0000122LValue CodeGenFunction::EmitUnsupportedLValue(const Expr *E,
123 const char *Name) {
124 ErrorUnsupported(E, Name);
125 llvm::Type *Ty = llvm::PointerType::getUnqual(ConvertType(E->getType()));
126 return LValue::MakeAddr(llvm::UndefValue::get(Ty),
Fariborz Jahaniana223cca2009-02-19 23:36:06 +0000127 E->getType().getCVRQualifiers(),
128 getContext().getObjCGCAttrKind(E->getType()));
Daniel Dunbar6ba82a42008-08-25 20:45:57 +0000129}
130
Reid Spencer5f016e22007-07-11 17:01:13 +0000131/// EmitLValue - Emit code to compute a designator that specifies the location
132/// of the expression.
133///
134/// This can return one of two things: a simple address or a bitfield
135/// reference. In either case, the LLVM Value* in the LValue structure is
136/// guaranteed to be an LLVM pointer type.
137///
138/// If this returns a bitfield reference, nothing about the pointee type of
139/// the LLVM value is known: For example, it may not be a pointer to an
140/// integer.
141///
142/// If this returns a normal address, and if the lvalue's C type is fixed
143/// size, this method guarantees that the returned pointer type will point to
144/// an LLVM type of the same size of the lvalue's type. If the lvalue has a
145/// variable length type, this is not possible.
146///
147LValue CodeGenFunction::EmitLValue(const Expr *E) {
148 switch (E->getStmtClass()) {
Daniel Dunbar6ba82a42008-08-25 20:45:57 +0000149 default: return EmitUnsupportedLValue(E, "l-value expression");
Reid Spencer5f016e22007-07-11 17:01:13 +0000150
Daniel Dunbar80e62c22008-09-04 03:20:13 +0000151 case Expr::BinaryOperatorClass:
152 return EmitBinaryOperatorLValue(cast<BinaryOperator>(E));
Douglas Gregorb4609802008-11-14 16:09:21 +0000153 case Expr::CallExprClass:
154 case Expr::CXXOperatorCallExprClass:
155 return EmitCallExprLValue(cast<CallExpr>(E));
Daniel Dunbar5b5c9ef2009-02-11 20:59:32 +0000156 case Expr::VAArgExprClass:
157 return EmitVAArgExprLValue(cast<VAArgExpr>(E));
Douglas Gregor1a49af92009-01-06 05:10:23 +0000158 case Expr::DeclRefExprClass:
159 case Expr::QualifiedDeclRefExprClass:
160 return EmitDeclRefLValue(cast<DeclRefExpr>(E));
Reid Spencer5f016e22007-07-11 17:01:13 +0000161 case Expr::ParenExprClass:return EmitLValue(cast<ParenExpr>(E)->getSubExpr());
Chris Lattnerd9f69102008-08-10 01:53:14 +0000162 case Expr::PredefinedExprClass:
163 return EmitPredefinedLValue(cast<PredefinedExpr>(E));
Reid Spencer5f016e22007-07-11 17:01:13 +0000164 case Expr::StringLiteralClass:
165 return EmitStringLiteralLValue(cast<StringLiteral>(E));
Chris Lattnereaf2bb82009-02-24 22:18:39 +0000166 case Expr::ObjCEncodeExprClass:
167 return EmitObjCEncodeExprLValue(cast<ObjCEncodeExpr>(E));
Chris Lattner391d77a2008-03-30 23:03:07 +0000168
Mike Stumpa99038c2009-02-28 09:07:16 +0000169 case Expr::BlockDeclRefExprClass:
170 return EmitBlockDeclRefLValue(cast<BlockDeclRefExpr>(E));
171
Argyrios Kyrtzidise3a09e62008-09-10 02:36:38 +0000172 case Expr::CXXConditionDeclExprClass:
173 return EmitCXXConditionDeclLValue(cast<CXXConditionDeclExpr>(E));
174
Daniel Dunbar0a04d772008-08-23 10:51:21 +0000175 case Expr::ObjCMessageExprClass:
176 return EmitObjCMessageExprLValue(cast<ObjCMessageExpr>(E));
Chris Lattner391d77a2008-03-30 23:03:07 +0000177 case Expr::ObjCIvarRefExprClass:
178 return EmitObjCIvarRefLValue(cast<ObjCIvarRefExpr>(E));
Daniel Dunbar6ba82a42008-08-25 20:45:57 +0000179 case Expr::ObjCPropertyRefExprClass:
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000180 return EmitObjCPropertyRefLValue(cast<ObjCPropertyRefExpr>(E));
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000181 case Expr::ObjCKVCRefExprClass:
182 return EmitObjCKVCRefLValue(cast<ObjCKVCRefExpr>(E));
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000183 case Expr::ObjCSuperExprClass:
Chris Lattner65459942009-04-25 19:35:26 +0000184 return EmitObjCSuperExprLValue(cast<ObjCSuperExpr>(E));
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000185
Chris Lattner65459942009-04-25 19:35:26 +0000186 case Expr::StmtExprClass:
187 return EmitStmtExprLValue(cast<StmtExpr>(E));
Reid Spencer5f016e22007-07-11 17:01:13 +0000188 case Expr::UnaryOperatorClass:
189 return EmitUnaryOpLValue(cast<UnaryOperator>(E));
190 case Expr::ArraySubscriptExprClass:
191 return EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E));
Nate Begeman213541a2008-04-18 23:10:10 +0000192 case Expr::ExtVectorElementExprClass:
193 return EmitExtVectorElementExpr(cast<ExtVectorElementExpr>(E));
Devang Patelb9b00ad2007-10-23 20:28:39 +0000194 case Expr::MemberExprClass: return EmitMemberExpr(cast<MemberExpr>(E));
Eli Friedman06e863f2008-05-13 23:18:27 +0000195 case Expr::CompoundLiteralExprClass:
196 return EmitCompoundLiteralLValue(cast<CompoundLiteralExpr>(E));
Daniel Dunbar90345582009-03-24 02:38:23 +0000197 case Expr::ConditionalOperatorClass:
198 return EmitConditionalOperator(cast<ConditionalOperator>(E));
Chris Lattner670a62c2008-12-12 05:35:08 +0000199 case Expr::ChooseExprClass:
Eli Friedman79769322009-03-04 05:52:32 +0000200 return EmitLValue(cast<ChooseExpr>(E)->getChosenSubExpr(getContext()));
Chris Lattnerc3953a62009-03-18 04:02:57 +0000201 case Expr::ImplicitCastExprClass:
202 case Expr::CStyleCastExprClass:
203 case Expr::CXXFunctionalCastExprClass:
204 case Expr::CXXStaticCastExprClass:
205 case Expr::CXXDynamicCastExprClass:
206 case Expr::CXXReinterpretCastExprClass:
207 case Expr::CXXConstCastExprClass:
Chris Lattner75dfeda2009-03-18 18:28:57 +0000208 return EmitCastLValue(cast<CastExpr>(E));
Reid Spencer5f016e22007-07-11 17:01:13 +0000209 }
210}
211
Daniel Dunbar9d9cc872009-02-10 00:57:50 +0000212llvm::Value *CodeGenFunction::EmitLoadOfScalar(llvm::Value *Addr, bool Volatile,
213 QualType Ty) {
214 llvm::Value *V = Builder.CreateLoad(Addr, Volatile, "tmp");
215
Anders Carlsson3bb423b2009-05-19 19:36:19 +0000216 // Bool can have different representation in memory than in registers.
Daniel Dunbar9d9cc872009-02-10 00:57:50 +0000217 if (Ty->isBooleanType())
218 if (V->getType() != llvm::Type::Int1Ty)
219 V = Builder.CreateTrunc(V, llvm::Type::Int1Ty, "tobool");
220
221 return V;
222}
223
224void CodeGenFunction::EmitStoreOfScalar(llvm::Value *Value, llvm::Value *Addr,
Anders Carlssonb4aa4662009-05-19 18:50:41 +0000225 bool Volatile, QualType Ty) {
Anders Carlsson3bb423b2009-05-19 19:36:19 +0000226
227 if (Ty->isBooleanType()) {
228 // Bool can have different representation in memory than in registers.
229 const llvm::Type *SrcTy = Value->getType();
230 const llvm::PointerType *DstPtr = cast<llvm::PointerType>(Addr->getType());
231 if (DstPtr->getElementType() != SrcTy) {
232 const llvm::Type *MemTy =
233 llvm::PointerType::get(SrcTy, DstPtr->getAddressSpace());
234 Addr = Builder.CreateBitCast(Addr, MemTy, "storetmp");
235 }
Daniel Dunbar9d9cc872009-02-10 00:57:50 +0000236 }
Anders Carlsson3bb423b2009-05-19 19:36:19 +0000237
Daniel Dunbar9d9cc872009-02-10 00:57:50 +0000238 Builder.CreateStore(Value, Addr, Volatile);
239}
240
Reid Spencer5f016e22007-07-11 17:01:13 +0000241/// EmitLoadOfLValue - Given an expression that represents a value lvalue,
242/// this method emits the address of the lvalue, then loads the result as an
243/// rvalue, returning the rvalue.
244RValue CodeGenFunction::EmitLoadOfLValue(LValue LV, QualType ExprType) {
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +0000245 if (LV.isObjCWeak()) {
Fariborz Jahanian6dc23172008-11-18 21:45:40 +0000246 // load of a __weak object.
247 llvm::Value *AddrWeakObj = LV.getAddress();
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000248 llvm::Value *read_weak = CGM.getObjCRuntime().EmitObjCWeakRead(*this,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +0000249 AddrWeakObj);
250 return RValue::get(read_weak);
251 }
252
Reid Spencer5f016e22007-07-11 17:01:13 +0000253 if (LV.isSimple()) {
254 llvm::Value *Ptr = LV.getAddress();
255 const llvm::Type *EltTy =
256 cast<llvm::PointerType>(Ptr->getType())->getElementType();
257
258 // Simple scalar l-value.
Daniel Dunbar9d9cc872009-02-10 00:57:50 +0000259 if (EltTy->isSingleValueType())
260 return RValue::get(EmitLoadOfScalar(Ptr, LV.isVolatileQualified(),
261 ExprType));
Reid Spencer5f016e22007-07-11 17:01:13 +0000262
Chris Lattner883f6a72007-08-11 00:04:45 +0000263 assert(ExprType->isFunctionType() && "Unknown scalar value");
264 return RValue::get(Ptr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000265 }
266
267 if (LV.isVectorElt()) {
Eli Friedman1e692ac2008-06-13 23:01:12 +0000268 llvm::Value *Vec = Builder.CreateLoad(LV.getVectorAddr(),
269 LV.isVolatileQualified(), "tmp");
Reid Spencer5f016e22007-07-11 17:01:13 +0000270 return RValue::get(Builder.CreateExtractElement(Vec, LV.getVectorIdx(),
271 "vecext"));
272 }
Chris Lattner46ea8eb2007-08-03 00:16:29 +0000273
274 // If this is a reference to a subset of the elements of a vector, either
275 // shuffle the input or extract/insert them as appropriate.
Nate Begeman213541a2008-04-18 23:10:10 +0000276 if (LV.isExtVectorElt())
277 return EmitLoadOfExtVectorElementLValue(LV, ExprType);
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000278
279 if (LV.isBitfield())
280 return EmitLoadOfBitfieldLValue(LV, ExprType);
281
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000282 if (LV.isPropertyRef())
283 return EmitLoadOfPropertyRefLValue(LV, ExprType);
284
Chris Lattner73525de2009-02-16 21:11:58 +0000285 assert(LV.isKVCRef() && "Unknown LValue type!");
286 return EmitLoadOfKVCRefLValue(LV, ExprType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000287}
288
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000289RValue CodeGenFunction::EmitLoadOfBitfieldLValue(LValue LV,
290 QualType ExprType) {
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000291 unsigned StartBit = LV.getBitfieldStartBit();
292 unsigned BitfieldSize = LV.getBitfieldSize();
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000293 llvm::Value *Ptr = LV.getBitfieldAddr();
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000294
295 const llvm::Type *EltTy =
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000296 cast<llvm::PointerType>(Ptr->getType())->getElementType();
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000297 unsigned EltTySize = CGM.getTargetData().getTypeSizeInBits(EltTy);
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000298
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000299 // In some cases the bitfield may straddle two memory locations.
300 // Currently we load the entire bitfield, then do the magic to
301 // sign-extend it if necessary. This results in somewhat more code
302 // than necessary for the common case (one load), since two shifts
303 // accomplish both the masking and sign extension.
304 unsigned LowBits = std::min(BitfieldSize, EltTySize - StartBit);
305 llvm::Value *Val = Builder.CreateLoad(Ptr, LV.isVolatileQualified(), "tmp");
306
307 // Shift to proper location.
Daniel Dunbarf3edc2f2008-11-13 02:20:34 +0000308 if (StartBit)
309 Val = Builder.CreateLShr(Val, llvm::ConstantInt::get(EltTy, StartBit),
310 "bf.lo");
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000311
312 // Mask off unused bits.
313 llvm::Constant *LowMask =
314 llvm::ConstantInt::get(llvm::APInt::getLowBitsSet(EltTySize, LowBits));
315 Val = Builder.CreateAnd(Val, LowMask, "bf.lo.cleared");
316
317 // Fetch the high bits if necessary.
318 if (LowBits < BitfieldSize) {
319 unsigned HighBits = BitfieldSize - LowBits;
320 llvm::Value *HighPtr =
321 Builder.CreateGEP(Ptr, llvm::ConstantInt::get(llvm::Type::Int32Ty, 1),
322 "bf.ptr.hi");
323 llvm::Value *HighVal = Builder.CreateLoad(HighPtr,
324 LV.isVolatileQualified(),
325 "tmp");
326
327 // Mask off unused bits.
328 llvm::Constant *HighMask =
329 llvm::ConstantInt::get(llvm::APInt::getLowBitsSet(EltTySize, HighBits));
330 HighVal = Builder.CreateAnd(HighVal, HighMask, "bf.lo.cleared");
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000331
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000332 // Shift to proper location and or in to bitfield value.
333 HighVal = Builder.CreateShl(HighVal,
334 llvm::ConstantInt::get(EltTy, LowBits));
335 Val = Builder.CreateOr(Val, HighVal, "bf.val");
336 }
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000337
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000338 // Sign extend if necessary.
339 if (LV.isBitfieldSigned()) {
340 llvm::Value *ExtraBits = llvm::ConstantInt::get(EltTy,
341 EltTySize - BitfieldSize);
342 Val = Builder.CreateAShr(Builder.CreateShl(Val, ExtraBits),
343 ExtraBits, "bf.val.sext");
344 }
Eli Friedman316bb1b2008-05-17 20:03:47 +0000345
346 // The bitfield type and the normal type differ when the storage sizes
347 // differ (currently just _Bool).
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000348 Val = Builder.CreateIntCast(Val, ConvertType(ExprType), false, "tmp");
Eli Friedman316bb1b2008-05-17 20:03:47 +0000349
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000350 return RValue::get(Val);
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000351}
352
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000353RValue CodeGenFunction::EmitLoadOfPropertyRefLValue(LValue LV,
354 QualType ExprType) {
355 return EmitObjCPropertyGet(LV.getPropertyRefExpr());
356}
357
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000358RValue CodeGenFunction::EmitLoadOfKVCRefLValue(LValue LV,
359 QualType ExprType) {
360 return EmitObjCPropertyGet(LV.getKVCRefExpr());
361}
362
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000363// If this is a reference to a subset of the elements of a vector, create an
364// appropriate shufflevector.
Nate Begeman213541a2008-04-18 23:10:10 +0000365RValue CodeGenFunction::EmitLoadOfExtVectorElementLValue(LValue LV,
366 QualType ExprType) {
Eli Friedman1e692ac2008-06-13 23:01:12 +0000367 llvm::Value *Vec = Builder.CreateLoad(LV.getExtVectorAddr(),
368 LV.isVolatileQualified(), "tmp");
Chris Lattner34cdc862007-08-03 16:18:34 +0000369
Nate Begeman8a997642008-05-09 06:41:27 +0000370 const llvm::Constant *Elts = LV.getExtVectorElts();
Chris Lattner34cdc862007-08-03 16:18:34 +0000371
372 // If the result of the expression is a non-vector type, we must be
373 // extracting a single element. Just codegen as an extractelement.
Chris Lattnercf60cd22007-08-10 17:10:08 +0000374 const VectorType *ExprVT = ExprType->getAsVectorType();
375 if (!ExprVT) {
Dan Gohman4f8d1232008-05-22 00:50:06 +0000376 unsigned InIdx = getAccessedFieldNo(0, Elts);
Chris Lattner34cdc862007-08-03 16:18:34 +0000377 llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx);
378 return RValue::get(Builder.CreateExtractElement(Vec, Elt, "tmp"));
379 }
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000380
381 // Always use shuffle vector to try to retain the original program structure
Chris Lattnercf60cd22007-08-10 17:10:08 +0000382 unsigned NumResultElts = ExprVT->getNumElements();
Chris Lattner34cdc862007-08-03 16:18:34 +0000383
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000384 llvm::SmallVector<llvm::Constant*, 4> Mask;
Chris Lattner34cdc862007-08-03 16:18:34 +0000385 for (unsigned i = 0; i != NumResultElts; ++i) {
Dan Gohman4f8d1232008-05-22 00:50:06 +0000386 unsigned InIdx = getAccessedFieldNo(i, Elts);
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000387 Mask.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx));
Chris Lattner34cdc862007-08-03 16:18:34 +0000388 }
389
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000390 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
391 Vec = Builder.CreateShuffleVector(Vec,
392 llvm::UndefValue::get(Vec->getType()),
393 MaskV, "tmp");
394 return RValue::get(Vec);
Chris Lattner34cdc862007-08-03 16:18:34 +0000395}
396
397
Reid Spencer5f016e22007-07-11 17:01:13 +0000398
399/// EmitStoreThroughLValue - Store the specified rvalue into the specified
400/// lvalue, where both are guaranteed to the have the same type, and that type
401/// is 'Ty'.
402void CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst,
403 QualType Ty) {
Chris Lattner017d6aa2007-08-03 16:28:33 +0000404 if (!Dst.isSimple()) {
405 if (Dst.isVectorElt()) {
406 // Read/modify/write the vector, inserting the new element.
Eli Friedman1e692ac2008-06-13 23:01:12 +0000407 llvm::Value *Vec = Builder.CreateLoad(Dst.getVectorAddr(),
408 Dst.isVolatileQualified(), "tmp");
Chris Lattner9b655512007-08-31 22:49:20 +0000409 Vec = Builder.CreateInsertElement(Vec, Src.getScalarVal(),
Chris Lattner017d6aa2007-08-03 16:28:33 +0000410 Dst.getVectorIdx(), "vecins");
Eli Friedman1e692ac2008-06-13 23:01:12 +0000411 Builder.CreateStore(Vec, Dst.getVectorAddr(),Dst.isVolatileQualified());
Chris Lattner017d6aa2007-08-03 16:28:33 +0000412 return;
413 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000414
Nate Begeman213541a2008-04-18 23:10:10 +0000415 // If this is an update of extended vector elements, insert them as
416 // appropriate.
417 if (Dst.isExtVectorElt())
418 return EmitStoreThroughExtVectorComponentLValue(Src, Dst, Ty);
Lauro Ramos Venancioa0c5d0e2008-01-22 22:36:45 +0000419
420 if (Dst.isBitfield())
421 return EmitStoreThroughBitfieldLValue(Src, Dst, Ty);
422
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000423 if (Dst.isPropertyRef())
424 return EmitStoreThroughPropertyRefLValue(Src, Dst, Ty);
425
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000426 if (Dst.isKVCRef())
427 return EmitStoreThroughKVCRefLValue(Src, Dst, Ty);
428
Lauro Ramos Venancio65539822008-01-22 22:38:35 +0000429 assert(0 && "Unknown LValue type");
Chris Lattner017d6aa2007-08-03 16:28:33 +0000430 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000431
Fariborz Jahanian4f676ed2009-02-21 00:30:43 +0000432 if (Dst.isObjCWeak() && !Dst.isNonGC()) {
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +0000433 // load of a __weak object.
434 llvm::Value *LvalueDst = Dst.getAddress();
435 llvm::Value *src = Src.getScalarVal();
Mike Stumpf33651c2009-04-14 00:57:29 +0000436 CGM.getObjCRuntime().EmitObjCWeakAssign(*this, src, LvalueDst);
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +0000437 return;
438 }
439
Fariborz Jahanian4f676ed2009-02-21 00:30:43 +0000440 if (Dst.isObjCStrong() && !Dst.isNonGC()) {
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +0000441 // load of a __strong object.
442 llvm::Value *LvalueDst = Dst.getAddress();
443 llvm::Value *src = Src.getScalarVal();
Fariborz Jahanian167fdc12009-02-19 18:29:24 +0000444#if 0
Mike Stumpf5408fe2009-05-16 07:57:57 +0000445 // FIXME. We cannot positively determine if we have an 'ivar' assignment,
446 // object assignment or an unknown assignment. For now, generate call to
447 // objc_assign_strongCast assignment which is a safe, but consevative
448 // assumption.
Fariborz Jahaniand1cc8042008-11-20 20:53:20 +0000449 if (Dst.isObjCIvar())
450 CGM.getObjCRuntime().EmitObjCIvarAssign(*this, src, LvalueDst);
451 else
452 CGM.getObjCRuntime().EmitObjCGlobalAssign(*this, src, LvalueDst);
Fariborz Jahanian167fdc12009-02-19 18:29:24 +0000453#endif
Fariborz Jahanianbf63b872009-05-04 23:27:20 +0000454 if (Dst.isGlobalObjCRef())
455 CGM.getObjCRuntime().EmitObjCGlobalAssign(*this, src, LvalueDst);
456 else
457 CGM.getObjCRuntime().EmitObjCStrongCastAssign(*this, src, LvalueDst);
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +0000458 return;
459 }
460
Chris Lattner883f6a72007-08-11 00:04:45 +0000461 assert(Src.isScalar() && "Can't emit an agg store with this method");
Anders Carlssonb4aa4662009-05-19 18:50:41 +0000462 EmitStoreOfScalar(Src.getScalarVal(), Dst.getAddress(),
463 Dst.isVolatileQualified(), Ty);
Reid Spencer5f016e22007-07-11 17:01:13 +0000464}
465
Lauro Ramos Venancioa0c5d0e2008-01-22 22:36:45 +0000466void CodeGenFunction::EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst,
Daniel Dunbared3849b2008-11-19 09:36:46 +0000467 QualType Ty,
468 llvm::Value **Result) {
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000469 unsigned StartBit = Dst.getBitfieldStartBit();
470 unsigned BitfieldSize = Dst.getBitfieldSize();
Lauro Ramos Venancioa0c5d0e2008-01-22 22:36:45 +0000471 llvm::Value *Ptr = Dst.getBitfieldAddr();
Lauro Ramos Venancioa0c5d0e2008-01-22 22:36:45 +0000472
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000473 const llvm::Type *EltTy =
474 cast<llvm::PointerType>(Ptr->getType())->getElementType();
475 unsigned EltTySize = CGM.getTargetData().getTypeSizeInBits(EltTy);
476
477 // Get the new value, cast to the appropriate type and masked to
478 // exactly the size of the bit-field.
Daniel Dunbared3849b2008-11-19 09:36:46 +0000479 llvm::Value *SrcVal = Src.getScalarVal();
480 llvm::Value *NewVal = Builder.CreateIntCast(SrcVal, EltTy, false, "tmp");
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000481 llvm::Constant *Mask =
482 llvm::ConstantInt::get(llvm::APInt::getLowBitsSet(EltTySize, BitfieldSize));
483 NewVal = Builder.CreateAnd(NewVal, Mask, "bf.value");
Lauro Ramos Venancioa0c5d0e2008-01-22 22:36:45 +0000484
Daniel Dunbared3849b2008-11-19 09:36:46 +0000485 // Return the new value of the bit-field, if requested.
486 if (Result) {
487 // Cast back to the proper type for result.
488 const llvm::Type *SrcTy = SrcVal->getType();
489 llvm::Value *SrcTrunc = Builder.CreateIntCast(NewVal, SrcTy, false,
490 "bf.reload.val");
491
492 // Sign extend if necessary.
493 if (Dst.isBitfieldSigned()) {
494 unsigned SrcTySize = CGM.getTargetData().getTypeSizeInBits(SrcTy);
495 llvm::Value *ExtraBits = llvm::ConstantInt::get(SrcTy,
496 SrcTySize - BitfieldSize);
497 SrcTrunc = Builder.CreateAShr(Builder.CreateShl(SrcTrunc, ExtraBits),
498 ExtraBits, "bf.reload.sext");
499 }
500
501 *Result = SrcTrunc;
502 }
503
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000504 // In some cases the bitfield may straddle two memory locations.
505 // Emit the low part first and check to see if the high needs to be
506 // done.
507 unsigned LowBits = std::min(BitfieldSize, EltTySize - StartBit);
508 llvm::Value *LowVal = Builder.CreateLoad(Ptr, Dst.isVolatileQualified(),
509 "bf.prev.low");
Eli Friedman316bb1b2008-05-17 20:03:47 +0000510
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000511 // Compute the mask for zero-ing the low part of this bitfield.
512 llvm::Constant *InvMask =
513 llvm::ConstantInt::get(~llvm::APInt::getBitsSet(EltTySize, StartBit,
514 StartBit + LowBits));
515
516 // Compute the new low part as
517 // LowVal = (LowVal & InvMask) | (NewVal << StartBit),
518 // with the shift of NewVal implicitly stripping the high bits.
519 llvm::Value *NewLowVal =
520 Builder.CreateShl(NewVal, llvm::ConstantInt::get(EltTy, StartBit),
521 "bf.value.lo");
522 LowVal = Builder.CreateAnd(LowVal, InvMask, "bf.prev.lo.cleared");
523 LowVal = Builder.CreateOr(LowVal, NewLowVal, "bf.new.lo");
524
525 // Write back.
526 Builder.CreateStore(LowVal, Ptr, Dst.isVolatileQualified());
Eli Friedman316bb1b2008-05-17 20:03:47 +0000527
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000528 // If the low part doesn't cover the bitfield emit a high part.
529 if (LowBits < BitfieldSize) {
530 unsigned HighBits = BitfieldSize - LowBits;
531 llvm::Value *HighPtr =
532 Builder.CreateGEP(Ptr, llvm::ConstantInt::get(llvm::Type::Int32Ty, 1),
533 "bf.ptr.hi");
534 llvm::Value *HighVal = Builder.CreateLoad(HighPtr,
535 Dst.isVolatileQualified(),
536 "bf.prev.hi");
537
538 // Compute the mask for zero-ing the high part of this bitfield.
539 llvm::Constant *InvMask =
540 llvm::ConstantInt::get(~llvm::APInt::getLowBitsSet(EltTySize, HighBits));
541
542 // Compute the new high part as
543 // HighVal = (HighVal & InvMask) | (NewVal lshr LowBits),
544 // where the high bits of NewVal have already been cleared and the
545 // shift stripping the low bits.
546 llvm::Value *NewHighVal =
547 Builder.CreateLShr(NewVal, llvm::ConstantInt::get(EltTy, LowBits),
548 "bf.value.high");
549 HighVal = Builder.CreateAnd(HighVal, InvMask, "bf.prev.hi.cleared");
550 HighVal = Builder.CreateOr(HighVal, NewHighVal, "bf.new.hi");
551
552 // Write back.
553 Builder.CreateStore(HighVal, HighPtr, Dst.isVolatileQualified());
554 }
Lauro Ramos Venancioa0c5d0e2008-01-22 22:36:45 +0000555}
556
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000557void CodeGenFunction::EmitStoreThroughPropertyRefLValue(RValue Src,
558 LValue Dst,
559 QualType Ty) {
560 EmitObjCPropertySet(Dst.getPropertyRefExpr(), Src);
561}
562
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000563void CodeGenFunction::EmitStoreThroughKVCRefLValue(RValue Src,
564 LValue Dst,
565 QualType Ty) {
566 EmitObjCPropertySet(Dst.getKVCRefExpr(), Src);
567}
568
Nate Begeman213541a2008-04-18 23:10:10 +0000569void CodeGenFunction::EmitStoreThroughExtVectorComponentLValue(RValue Src,
570 LValue Dst,
571 QualType Ty) {
Chris Lattner017d6aa2007-08-03 16:28:33 +0000572 // This access turns into a read/modify/write of the vector. Load the input
573 // value now.
Eli Friedman1e692ac2008-06-13 23:01:12 +0000574 llvm::Value *Vec = Builder.CreateLoad(Dst.getExtVectorAddr(),
575 Dst.isVolatileQualified(), "tmp");
Nate Begeman8a997642008-05-09 06:41:27 +0000576 const llvm::Constant *Elts = Dst.getExtVectorElts();
Chris Lattner017d6aa2007-08-03 16:28:33 +0000577
Chris Lattner9b655512007-08-31 22:49:20 +0000578 llvm::Value *SrcVal = Src.getScalarVal();
Chris Lattner017d6aa2007-08-03 16:28:33 +0000579
Chris Lattner7e6b51b2007-08-03 16:37:04 +0000580 if (const VectorType *VTy = Ty->getAsVectorType()) {
581 unsigned NumSrcElts = VTy->getNumElements();
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000582 unsigned NumDstElts =
583 cast<llvm::VectorType>(Vec->getType())->getNumElements();
584 if (NumDstElts == NumSrcElts) {
585 // Use shuffle vector is the src and destination are the same number
586 // of elements
587 llvm::SmallVector<llvm::Constant*, 4> Mask;
588 for (unsigned i = 0; i != NumSrcElts; ++i) {
589 unsigned InIdx = getAccessedFieldNo(i, Elts);
590 Mask.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx));
591 }
592
593 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
594 Vec = Builder.CreateShuffleVector(SrcVal,
595 llvm::UndefValue::get(Vec->getType()),
596 MaskV, "tmp");
597 }
598 else if (NumDstElts > NumSrcElts) {
599 // Extended the source vector to the same length and then shuffle it
600 // into the destination.
601 // FIXME: since we're shuffling with undef, can we just use the indices
602 // into that? This could be simpler.
603 llvm::SmallVector<llvm::Constant*, 4> ExtMask;
604 unsigned i;
605 for (i = 0; i != NumSrcElts; ++i)
606 ExtMask.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, i));
607 for (; i != NumDstElts; ++i)
608 ExtMask.push_back(llvm::UndefValue::get(llvm::Type::Int32Ty));
609 llvm::Value *ExtMaskV = llvm::ConstantVector::get(&ExtMask[0],
610 ExtMask.size());
Daniel Dunbarbb767732009-02-17 18:31:04 +0000611 llvm::Value *ExtSrcVal =
612 Builder.CreateShuffleVector(SrcVal,
613 llvm::UndefValue::get(SrcVal->getType()),
614 ExtMaskV, "tmp");
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000615 // build identity
616 llvm::SmallVector<llvm::Constant*, 4> Mask;
617 for (unsigned i = 0; i != NumDstElts; ++i) {
618 Mask.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, i));
619 }
620 // modify when what gets shuffled in
621 for (unsigned i = 0; i != NumSrcElts; ++i) {
622 unsigned Idx = getAccessedFieldNo(i, Elts);
623 Mask[Idx] =llvm::ConstantInt::get(llvm::Type::Int32Ty, i+NumDstElts);
624 }
625 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
626 Vec = Builder.CreateShuffleVector(Vec, ExtSrcVal, MaskV, "tmp");
627 }
628 else {
629 // We should never shorten the vector
630 assert(0 && "unexpected shorten vector length");
Chris Lattner7e6b51b2007-08-03 16:37:04 +0000631 }
632 } else {
633 // If the Src is a scalar (not a vector) it must be updating one element.
Dan Gohman4f8d1232008-05-22 00:50:06 +0000634 unsigned InIdx = getAccessedFieldNo(0, Elts);
Chris Lattner017d6aa2007-08-03 16:28:33 +0000635 llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx);
636 Vec = Builder.CreateInsertElement(Vec, SrcVal, Elt, "tmp");
Chris Lattner017d6aa2007-08-03 16:28:33 +0000637 }
638
Eli Friedman1e692ac2008-06-13 23:01:12 +0000639 Builder.CreateStore(Vec, Dst.getExtVectorAddr(), Dst.isVolatileQualified());
Chris Lattner017d6aa2007-08-03 16:28:33 +0000640}
641
Reid Spencer5f016e22007-07-11 17:01:13 +0000642LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) {
Steve Naroff248a7532008-04-15 22:42:06 +0000643 const VarDecl *VD = dyn_cast<VarDecl>(E->getDecl());
644
Chris Lattner41110242008-06-17 18:05:57 +0000645 if (VD && (VD->isBlockVarDecl() || isa<ParmVarDecl>(VD) ||
646 isa<ImplicitParamDecl>(VD))) {
Fariborz Jahanian2682d8b2008-11-20 00:15:42 +0000647 LValue LV;
Mike Stumpaa771a82009-04-14 18:24:37 +0000648 bool GCable = VD->hasLocalStorage() && !VD->hasAttr<BlocksAttr>();
Daniel Dunbar5466c7b2009-04-14 02:25:56 +0000649 if (VD->hasExternalStorage()) {
Anders Carlssonc8667a82009-05-19 20:40:02 +0000650 llvm::Value *V = CGM.GetAddrOfGlobalVar(VD);
651 if (VD->getType()->isReferenceType())
652 V = Builder.CreateLoad(V, "tmp");
653 LV = LValue::MakeAddr(V, E->getType().getCVRQualifiers(),
Fariborz Jahaniana223cca2009-02-19 23:36:06 +0000654 getContext().getObjCGCAttrKind(E->getType()));
Fariborz Jahanian2682d8b2008-11-20 00:15:42 +0000655 }
Lauro Ramos Venanciofea90b82008-02-16 22:30:38 +0000656 else {
Steve Naroff248a7532008-04-15 22:42:06 +0000657 llvm::Value *V = LocalDeclMap[VD];
Mike Stumpa99038c2009-02-28 09:07:16 +0000658 assert(V && "DeclRefExpr not entered in LocalDeclMap?");
Fariborz Jahanian4f545262009-02-20 01:14:43 +0000659 // local variables do not get their gc attribute set.
660 QualType::GCAttrTypes attr = QualType::GCNone;
661 // local static?
Mike Stumpf33651c2009-04-14 00:57:29 +0000662 if (!GCable)
Fariborz Jahanian4f545262009-02-20 01:14:43 +0000663 attr = getContext().getObjCGCAttrKind(E->getType());
Daniel Dunbarb11fa0d2009-04-13 21:08:27 +0000664 if (VD->hasAttr<BlocksAttr>()) {
Mike Stumpdab514f2009-03-04 03:23:46 +0000665 bool needsCopyDispose = BlockRequiresCopying(VD->getType());
666 const llvm::Type *PtrStructTy = V->getType();
667 const llvm::Type *Ty = PtrStructTy;
668 Ty = llvm::PointerType::get(Ty, 0);
669 V = Builder.CreateStructGEP(V, 1, "forwarding");
670 V = Builder.CreateBitCast(V, Ty);
671 V = Builder.CreateLoad(V, false);
672 V = Builder.CreateBitCast(V, PtrStructTy);
673 V = Builder.CreateStructGEP(V, needsCopyDispose*2 + 4, "x");
674 }
Anders Carlssonc8667a82009-05-19 20:40:02 +0000675 if (VD->getType()->isReferenceType())
676 V = Builder.CreateLoad(V, "tmp");
Fariborz Jahanian4f545262009-02-20 01:14:43 +0000677 LV = LValue::MakeAddr(V, E->getType().getCVRQualifiers(), attr);
Lauro Ramos Venanciofea90b82008-02-16 22:30:38 +0000678 }
Mike Stumpf33651c2009-04-14 00:57:29 +0000679 LValue::SetObjCNonGC(LV, GCable);
Fariborz Jahanian2682d8b2008-11-20 00:15:42 +0000680 return LV;
Steve Naroff248a7532008-04-15 22:42:06 +0000681 } else if (VD && VD->isFileVarDecl()) {
Anders Carlssonc8667a82009-05-19 20:40:02 +0000682 llvm::Value *V = CGM.GetAddrOfGlobalVar(VD);
683 if (VD->getType()->isReferenceType())
684 V = Builder.CreateLoad(V, "tmp");
685 LValue LV = LValue::MakeAddr(V, E->getType().getCVRQualifiers(),
Fariborz Jahaniana223cca2009-02-19 23:36:06 +0000686 getContext().getObjCGCAttrKind(E->getType()));
Fariborz Jahanianbf63b872009-05-04 23:27:20 +0000687 if (LV.isObjCStrong())
688 LV.SetGlobalObjCRef(LV, true);
Fariborz Jahanian6d657c42008-11-18 20:18:11 +0000689 return LV;
Steve Naroff248a7532008-04-15 22:42:06 +0000690 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(E->getDecl())) {
Chris Lattnerb4880ba2009-05-12 21:21:08 +0000691 return LValue::MakeAddr(CGM.GetAddrOfFunction(GlobalDecl(FD)),
Fariborz Jahaniana223cca2009-02-19 23:36:06 +0000692 E->getType().getCVRQualifiers(),
693 getContext().getObjCGCAttrKind(E->getType()));
Reid Spencer5f016e22007-07-11 17:01:13 +0000694 }
Chris Lattner41110242008-06-17 18:05:57 +0000695 else if (const ImplicitParamDecl *IPD =
696 dyn_cast<ImplicitParamDecl>(E->getDecl())) {
697 llvm::Value *V = LocalDeclMap[IPD];
698 assert(V && "BlockVarDecl not entered in LocalDeclMap?");
Fariborz Jahaniana223cca2009-02-19 23:36:06 +0000699 return LValue::MakeAddr(V, E->getType().getCVRQualifiers(),
700 getContext().getObjCGCAttrKind(E->getType()));
Chris Lattner41110242008-06-17 18:05:57 +0000701 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000702 assert(0 && "Unimp declref");
Chris Lattnerb1776cb2007-09-16 19:23:47 +0000703 //an invalid LValue, but the assert will
704 //ensure that this point is never reached.
705 return LValue();
Reid Spencer5f016e22007-07-11 17:01:13 +0000706}
707
Mike Stumpa99038c2009-02-28 09:07:16 +0000708LValue CodeGenFunction::EmitBlockDeclRefLValue(const BlockDeclRefExpr *E) {
709 return LValue::MakeAddr(GetAddrOfBlockDecl(E), 0);
710}
711
Reid Spencer5f016e22007-07-11 17:01:13 +0000712LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) {
713 // __extension__ doesn't affect lvalue-ness.
714 if (E->getOpcode() == UnaryOperator::Extension)
715 return EmitLValue(E->getSubExpr());
716
Chris Lattner96196622008-07-26 22:37:01 +0000717 QualType ExprTy = getContext().getCanonicalType(E->getSubExpr()->getType());
Chris Lattner7da36f62007-10-30 22:53:42 +0000718 switch (E->getOpcode()) {
719 default: assert(0 && "Unknown unary operator lvalue!");
720 case UnaryOperator::Deref:
Fariborz Jahaniana223cca2009-02-19 23:36:06 +0000721 {
Chris Lattnereaf2bb82009-02-24 22:18:39 +0000722 QualType T =
723 E->getSubExpr()->getType()->getAsPointerType()->getPointeeType();
Fariborz Jahanian207c5212009-02-23 18:59:50 +0000724 LValue LV = LValue::MakeAddr(EmitScalarExpr(E->getSubExpr()),
725 ExprTy->getAsPointerType()->getPointeeType()
Fariborz Jahanian4f545262009-02-20 01:14:43 +0000726 .getCVRQualifiers(),
Fariborz Jahanian207c5212009-02-23 18:59:50 +0000727 getContext().getObjCGCAttrKind(T));
728 // We should not generate __weak write barrier on indirect reference
729 // of a pointer to object; as in void foo (__weak id *param); *param = 0;
730 // But, we continue to generate __strong write barrier on indirect write
731 // into a pointer to object.
732 if (getContext().getLangOptions().ObjC1 &&
733 getContext().getLangOptions().getGCMode() != LangOptions::NonGC &&
734 LV.isObjCWeak())
735 LValue::SetObjCNonGC(LV, !E->isOBJCGCCandidate());
736 return LV;
Fariborz Jahaniana223cca2009-02-19 23:36:06 +0000737 }
Chris Lattner7da36f62007-10-30 22:53:42 +0000738 case UnaryOperator::Real:
739 case UnaryOperator::Imag:
740 LValue LV = EmitLValue(E->getSubExpr());
Chris Lattner36b6a0a2008-03-19 05:19:41 +0000741 unsigned Idx = E->getOpcode() == UnaryOperator::Imag;
742 return LValue::MakeAddr(Builder.CreateStructGEP(LV.getAddress(),
Chris Lattnerb77792e2008-07-26 22:17:49 +0000743 Idx, "idx"),
744 ExprTy.getCVRQualifiers());
Chris Lattner7da36f62007-10-30 22:53:42 +0000745 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000746}
747
748LValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) {
Daniel Dunbar61432932008-08-13 23:20:05 +0000749 return LValue::MakeAddr(CGM.GetAddrOfConstantStringFromLiteral(E), 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000750}
751
Chris Lattnereaf2bb82009-02-24 22:18:39 +0000752LValue CodeGenFunction::EmitObjCEncodeExprLValue(const ObjCEncodeExpr *E) {
753 return LValue::MakeAddr(CGM.GetAddrOfConstantStringFromObjCEncode(E), 0);
754}
755
756
Daniel Dunbar662b71e2008-10-17 21:58:32 +0000757LValue CodeGenFunction::EmitPredefinedFunctionName(unsigned Type) {
Anders Carlsson22742662007-07-21 05:21:51 +0000758 std::string GlobalVarName;
Daniel Dunbar662b71e2008-10-17 21:58:32 +0000759
760 switch (Type) {
Chris Lattnereaf2bb82009-02-24 22:18:39 +0000761 default:
762 assert(0 && "Invalid type");
763 case PredefinedExpr::Func:
764 GlobalVarName = "__func__.";
765 break;
766 case PredefinedExpr::Function:
767 GlobalVarName = "__FUNCTION__.";
768 break;
769 case PredefinedExpr::PrettyFunction:
770 // FIXME:: Demangle C++ method names
771 GlobalVarName = "__PRETTY_FUNCTION__.";
772 break;
Anders Carlsson22742662007-07-21 05:21:51 +0000773 }
Daniel Dunbar662b71e2008-10-17 21:58:32 +0000774
Chris Lattnerb5437d22009-04-23 05:30:27 +0000775 // FIXME: This isn't right at all. The logic for computing this should go
776 // into a method on PredefinedExpr. This would allow sema and codegen to be
777 // consistent for things like sizeof(__func__) etc.
Daniel Dunbar662b71e2008-10-17 21:58:32 +0000778 std::string FunctionName;
Chris Lattnerb5437d22009-04-23 05:30:27 +0000779 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CurCodeDecl)) {
Douglas Gregor6ec36682009-02-18 23:53:56 +0000780 FunctionName = CGM.getMangledName(FD);
Daniel Dunbar662b71e2008-10-17 21:58:32 +0000781 } else {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +0000782 // Just get the mangled name; skipping the asm prefix if it
783 // exists.
Daniel Dunbar662b71e2008-10-17 21:58:32 +0000784 FunctionName = CurFn->getName();
Daniel Dunbar6ab187a2009-04-07 05:48:37 +0000785 if (FunctionName[0] == '\01')
786 FunctionName = FunctionName.substr(1, std::string::npos);
Daniel Dunbar662b71e2008-10-17 21:58:32 +0000787 }
788
Chris Lattnerc8aa5f12008-04-04 04:07:35 +0000789 GlobalVarName += FunctionName;
Daniel Dunbar662b71e2008-10-17 21:58:32 +0000790 llvm::Constant *C =
791 CGM.GetAddrOfConstantCString(FunctionName, GlobalVarName.c_str());
792 return LValue::MakeAddr(C, 0);
793}
794
795LValue CodeGenFunction::EmitPredefinedLValue(const PredefinedExpr *E) {
796 switch (E->getIdentType()) {
797 default:
798 return EmitUnsupportedLValue(E, "predefined expression");
799 case PredefinedExpr::Func:
800 case PredefinedExpr::Function:
801 case PredefinedExpr::PrettyFunction:
802 return EmitPredefinedFunctionName(E->getIdentType());
803 }
Anders Carlsson22742662007-07-21 05:21:51 +0000804}
805
Reid Spencer5f016e22007-07-11 17:01:13 +0000806LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) {
Ted Kremenek23245122007-08-20 16:18:38 +0000807 // The index must always be an integer, which is not an aggregate. Emit it.
Chris Lattner7f02f722007-08-24 05:35:26 +0000808 llvm::Value *Idx = EmitScalarExpr(E->getIdx());
Reid Spencer5f016e22007-07-11 17:01:13 +0000809
810 // If the base is a vector type, then we are forming a vector element lvalue
811 // with this subscript.
Eli Friedman1e692ac2008-06-13 23:01:12 +0000812 if (E->getBase()->getType()->isVectorType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000813 // Emit the vector as an lvalue to get its address.
Eli Friedman1e692ac2008-06-13 23:01:12 +0000814 LValue LHS = EmitLValue(E->getBase());
Ted Kremenek23245122007-08-20 16:18:38 +0000815 assert(LHS.isSimple() && "Can only subscript lvalue vectors here!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000816 // FIXME: This should properly sign/zero/extend or truncate Idx to i32.
Eli Friedman1e692ac2008-06-13 23:01:12 +0000817 return LValue::MakeVectorElt(LHS.getAddress(), Idx,
818 E->getBase()->getType().getCVRQualifiers());
Reid Spencer5f016e22007-07-11 17:01:13 +0000819 }
820
Ted Kremenek23245122007-08-20 16:18:38 +0000821 // The base must be a pointer, which is not an aggregate. Emit it.
Chris Lattner7f02f722007-08-24 05:35:26 +0000822 llvm::Value *Base = EmitScalarExpr(E->getBase());
Reid Spencer5f016e22007-07-11 17:01:13 +0000823
Ted Kremenek23245122007-08-20 16:18:38 +0000824 // Extend or truncate the index type to 32 or 64-bits.
Chris Lattnerd4f08022007-08-08 17:43:05 +0000825 QualType IdxTy = E->getIdx()->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000826 bool IdxSigned = IdxTy->isSignedIntegerType();
827 unsigned IdxBitwidth = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
Sanjiv Gupta7cabee52009-04-24 02:40:57 +0000828 if (IdxBitwidth != LLVMPointerWidth)
829 Idx = Builder.CreateIntCast(Idx, llvm::IntegerType::get(LLVMPointerWidth),
Reid Spencer5f016e22007-07-11 17:01:13 +0000830 IdxSigned, "idxprom");
831
Daniel Dunbar2a866252009-04-25 05:08:32 +0000832 // We know that the pointer points to a type of the correct size,
833 // unless the size is a VLA or Objective-C interface.
834 llvm::Value *Address = 0;
Anders Carlsson8b33c082008-12-21 00:11:23 +0000835 if (const VariableArrayType *VAT =
836 getContext().getAsVariableArrayType(E->getType())) {
837 llvm::Value *VLASize = VLASizeMap[VAT];
838
839 Idx = Builder.CreateMul(Idx, VLASize);
840
Anders Carlsson6183a992008-12-21 03:44:36 +0000841 QualType BaseType = getContext().getBaseElementType(VAT);
Anders Carlsson8b33c082008-12-21 00:11:23 +0000842
843 uint64_t BaseTypeSize = getContext().getTypeSize(BaseType) / 8;
844 Idx = Builder.CreateUDiv(Idx,
845 llvm::ConstantInt::get(Idx->getType(),
846 BaseTypeSize));
Daniel Dunbar2a866252009-04-25 05:08:32 +0000847 Address = Builder.CreateGEP(Base, Idx, "arrayidx");
848 } else if (const ObjCInterfaceType *OIT =
849 dyn_cast<ObjCInterfaceType>(E->getType())) {
850 llvm::Value *InterfaceSize =
851 llvm::ConstantInt::get(Idx->getType(),
852 getContext().getTypeSize(OIT) / 8);
853
854 Idx = Builder.CreateMul(Idx, InterfaceSize);
855
856 llvm::Type *i8PTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
857 Address = Builder.CreateGEP(Builder.CreateBitCast(Base, i8PTy),
858 Idx, "arrayidx");
859 Address = Builder.CreateBitCast(Address, Base->getType());
860 } else {
861 Address = Builder.CreateGEP(Base, Idx, "arrayidx");
Anders Carlsson8b33c082008-12-21 00:11:23 +0000862 }
863
Daniel Dunbarf3ef07c2009-04-18 08:54:40 +0000864 QualType T = E->getBase()->getType()->getAsPointerType()->getPointeeType();
Daniel Dunbar2a866252009-04-25 05:08:32 +0000865 LValue LV = LValue::MakeAddr(Address,
Daniel Dunbarf3ef07c2009-04-18 08:54:40 +0000866 T.getCVRQualifiers(),
867 getContext().getObjCGCAttrKind(T));
Fariborz Jahanian643887a2009-02-21 23:37:19 +0000868 if (getContext().getLangOptions().ObjC1 &&
869 getContext().getLangOptions().getGCMode() != LangOptions::NonGC)
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +0000870 LValue::SetObjCNonGC(LV, !E->isOBJCGCCandidate());
Fariborz Jahanian643887a2009-02-21 23:37:19 +0000871 return LV;
Reid Spencer5f016e22007-07-11 17:01:13 +0000872}
873
Nate Begeman3b8d1162008-05-13 21:03:02 +0000874static
875llvm::Constant *GenerateConstantVector(llvm::SmallVector<unsigned, 4> &Elts) {
876 llvm::SmallVector<llvm::Constant *, 4> CElts;
877
878 for (unsigned i = 0, e = Elts.size(); i != e; ++i)
879 CElts.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, Elts[i]));
880
881 return llvm::ConstantVector::get(&CElts[0], CElts.size());
882}
883
Chris Lattner349aaec2007-08-02 23:37:31 +0000884LValue CodeGenFunction::
Nate Begeman213541a2008-04-18 23:10:10 +0000885EmitExtVectorElementExpr(const ExtVectorElementExpr *E) {
Chris Lattner349aaec2007-08-02 23:37:31 +0000886 // Emit the base vector as an l-value.
Chris Lattner73525de2009-02-16 21:11:58 +0000887 LValue Base;
888
889 // ExtVectorElementExpr's base can either be a vector or pointer to vector.
Chris Lattner2140e902009-02-16 22:14:05 +0000890 if (!E->isArrow()) {
Chris Lattner73525de2009-02-16 21:11:58 +0000891 assert(E->getBase()->getType()->isVectorType());
892 Base = EmitLValue(E->getBase());
Chris Lattner2140e902009-02-16 22:14:05 +0000893 } else {
894 const PointerType *PT = E->getBase()->getType()->getAsPointerType();
895 llvm::Value *Ptr = EmitScalarExpr(E->getBase());
896 Base = LValue::MakeAddr(Ptr, PT->getPointeeType().getCVRQualifiers());
Chris Lattner73525de2009-02-16 21:11:58 +0000897 }
Chris Lattner349aaec2007-08-02 23:37:31 +0000898
Nate Begeman3b8d1162008-05-13 21:03:02 +0000899 // Encode the element access list into a vector of unsigned indices.
900 llvm::SmallVector<unsigned, 4> Indices;
901 E->getEncodedElementAccess(Indices);
902
903 if (Base.isSimple()) {
904 llvm::Constant *CV = GenerateConstantVector(Indices);
Eli Friedman1e692ac2008-06-13 23:01:12 +0000905 return LValue::MakeExtVectorElt(Base.getAddress(), CV,
Chris Lattner1bd885e2009-02-16 22:25:49 +0000906 Base.getQualifiers());
Nate Begeman3b8d1162008-05-13 21:03:02 +0000907 }
908 assert(Base.isExtVectorElt() && "Can only subscript lvalue vec elts here!");
909
910 llvm::Constant *BaseElts = Base.getExtVectorElts();
911 llvm::SmallVector<llvm::Constant *, 4> CElts;
912
913 for (unsigned i = 0, e = Indices.size(); i != e; ++i) {
914 if (isa<llvm::ConstantAggregateZero>(BaseElts))
915 CElts.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, 0));
916 else
917 CElts.push_back(BaseElts->getOperand(Indices[i]));
918 }
919 llvm::Constant *CV = llvm::ConstantVector::get(&CElts[0], CElts.size());
Eli Friedman1e692ac2008-06-13 23:01:12 +0000920 return LValue::MakeExtVectorElt(Base.getExtVectorAddr(), CV,
Chris Lattner1bd885e2009-02-16 22:25:49 +0000921 Base.getQualifiers());
Chris Lattner349aaec2007-08-02 23:37:31 +0000922}
923
Devang Patelb9b00ad2007-10-23 20:28:39 +0000924LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) {
Devang Patelfe2419a2007-12-11 21:33:16 +0000925 bool isUnion = false;
Fariborz Jahanian2ab19682008-11-21 18:14:01 +0000926 bool isIvar = false;
Fariborz Jahanian4f676ed2009-02-21 00:30:43 +0000927 bool isNonGC = false;
Devang Patel126a8562007-10-24 22:26:28 +0000928 Expr *BaseExpr = E->getBase();
Devang Patel126a8562007-10-24 22:26:28 +0000929 llvm::Value *BaseValue = NULL;
Eli Friedman1e692ac2008-06-13 23:01:12 +0000930 unsigned CVRQualifiers=0;
931
Chris Lattner12f65f62007-12-02 18:52:07 +0000932 // If this is s.x, emit s as an lvalue. If it is s->x, emit s as a scalar.
Devang Patelfe2419a2007-12-11 21:33:16 +0000933 if (E->isArrow()) {
Devang Patel0a961182007-10-26 18:15:21 +0000934 BaseValue = EmitScalarExpr(BaseExpr);
Devang Patelfe2419a2007-12-11 21:33:16 +0000935 const PointerType *PTy =
Daniel Dunbarf3ef07c2009-04-18 08:54:40 +0000936 BaseExpr->getType()->getAsPointerType();
Devang Patelfe2419a2007-12-11 21:33:16 +0000937 if (PTy->getPointeeType()->isUnionType())
938 isUnion = true;
Eli Friedman1e692ac2008-06-13 23:01:12 +0000939 CVRQualifiers = PTy->getPointeeType().getCVRQualifiers();
Chris Lattner1bd885e2009-02-16 22:25:49 +0000940 } else if (isa<ObjCPropertyRefExpr>(BaseExpr) ||
941 isa<ObjCKVCRefExpr>(BaseExpr)) {
Fariborz Jahanian35c33292009-01-12 23:27:26 +0000942 RValue RV = EmitObjCPropertyGet(BaseExpr);
943 BaseValue = RV.getAggregateAddr();
944 if (BaseExpr->getType()->isUnionType())
945 isUnion = true;
946 CVRQualifiers = BaseExpr->getType().getCVRQualifiers();
Chris Lattner1bd885e2009-02-16 22:25:49 +0000947 } else {
Chris Lattner12f65f62007-12-02 18:52:07 +0000948 LValue BaseLV = EmitLValue(BaseExpr);
Fariborz Jahanian2ab19682008-11-21 18:14:01 +0000949 if (BaseLV.isObjCIvar())
950 isIvar = true;
Fariborz Jahanian4f676ed2009-02-21 00:30:43 +0000951 if (BaseLV.isNonGC())
952 isNonGC = true;
Chris Lattner12f65f62007-12-02 18:52:07 +0000953 // FIXME: this isn't right for bitfields.
954 BaseValue = BaseLV.getAddress();
Devang Patelfe2419a2007-12-11 21:33:16 +0000955 if (BaseExpr->getType()->isUnionType())
956 isUnion = true;
Eli Friedman1e692ac2008-06-13 23:01:12 +0000957 CVRQualifiers = BaseExpr->getType().getCVRQualifiers();
Chris Lattner12f65f62007-12-02 18:52:07 +0000958 }
Devang Patelb9b00ad2007-10-23 20:28:39 +0000959
Douglas Gregor86f19402008-12-20 23:49:58 +0000960 FieldDecl *Field = dyn_cast<FieldDecl>(E->getMemberDecl());
961 // FIXME: Handle non-field member expressions
962 assert(Field && "No code generation for non-field member references");
Chris Lattner1bd885e2009-02-16 22:25:49 +0000963 LValue MemExpLV = EmitLValueForField(BaseValue, Field, isUnion,
964 CVRQualifiers);
Fariborz Jahanian2ab19682008-11-21 18:14:01 +0000965 LValue::SetObjCIvar(MemExpLV, isIvar);
Fariborz Jahanian4f676ed2009-02-21 00:30:43 +0000966 LValue::SetObjCNonGC(MemExpLV, isNonGC);
Fariborz Jahanian2ab19682008-11-21 18:14:01 +0000967 return MemExpLV;
Eli Friedman472778e2008-02-09 08:50:58 +0000968}
Devang Patelb9b00ad2007-10-23 20:28:39 +0000969
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +0000970LValue CodeGenFunction::EmitLValueForBitfield(llvm::Value* BaseValue,
971 FieldDecl* Field,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +0000972 unsigned CVRQualifiers) {
Daniel Dunbarbb767732009-02-17 18:31:04 +0000973 unsigned idx = CGM.getTypes().getLLVMFieldNo(Field);
Mike Stumpf5408fe2009-05-16 07:57:57 +0000974 // FIXME: CodeGenTypes should expose a method to get the appropriate type for
975 // FieldTy (the appropriate type is ABI-dependent).
Daniel Dunbarbb767732009-02-17 18:31:04 +0000976 const llvm::Type *FieldTy =
977 CGM.getTypes().ConvertTypeForMem(Field->getType());
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +0000978 const llvm::PointerType *BaseTy =
979 cast<llvm::PointerType>(BaseValue->getType());
980 unsigned AS = BaseTy->getAddressSpace();
981 BaseValue = Builder.CreateBitCast(BaseValue,
982 llvm::PointerType::get(FieldTy, AS),
983 "tmp");
984 llvm::Value *V = Builder.CreateGEP(BaseValue,
985 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx),
986 "tmp");
987
988 CodeGenTypes::BitFieldInfo bitFieldInfo =
989 CGM.getTypes().getBitFieldInfo(Field);
990 return LValue::MakeBitfield(V, bitFieldInfo.Begin, bitFieldInfo.Size,
991 Field->getType()->isSignedIntegerType(),
992 Field->getType().getCVRQualifiers()|CVRQualifiers);
993}
994
Eli Friedman472778e2008-02-09 08:50:58 +0000995LValue CodeGenFunction::EmitLValueForField(llvm::Value* BaseValue,
996 FieldDecl* Field,
Eli Friedman1e692ac2008-06-13 23:01:12 +0000997 bool isUnion,
998 unsigned CVRQualifiers)
Eli Friedman472778e2008-02-09 08:50:58 +0000999{
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +00001000 if (Field->isBitField())
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001001 return EmitLValueForBitfield(BaseValue, Field, CVRQualifiers);
Fariborz Jahanian2682d8b2008-11-20 00:15:42 +00001002
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001003 unsigned idx = CGM.getTypes().getLLVMFieldNo(Field);
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +00001004 llvm::Value *V = Builder.CreateStructGEP(BaseValue, idx, "tmp");
Eli Friedman1e86b342008-05-29 11:33:25 +00001005
Devang Patelabad06c2007-10-26 19:42:18 +00001006 // Match union field type.
Lauro Ramos Venanciod957aa02008-02-07 19:29:53 +00001007 if (isUnion) {
Eli Friedman1e692ac2008-06-13 23:01:12 +00001008 const llvm::Type *FieldTy =
1009 CGM.getTypes().ConvertTypeForMem(Field->getType());
Devang Patele9b8c0a2007-10-30 20:59:40 +00001010 const llvm::PointerType * BaseTy =
1011 cast<llvm::PointerType>(BaseValue->getType());
Eli Friedman788d5712008-05-21 13:24:44 +00001012 unsigned AS = BaseTy->getAddressSpace();
1013 V = Builder.CreateBitCast(V,
1014 llvm::PointerType::get(FieldTy, AS),
1015 "tmp");
Devang Patelabad06c2007-10-26 19:42:18 +00001016 }
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +00001017
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00001018 QualType::GCAttrTypes attr = QualType::GCNone;
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001019 if (CGM.getLangOptions().ObjC1 &&
Fariborz Jahanian5934e752009-02-18 18:52:41 +00001020 CGM.getLangOptions().getGCMode() != LangOptions::NonGC) {
1021 QualType Ty = Field->getType();
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00001022 attr = Ty.getObjCGCAttr();
Fariborz Jahanianc1debf32009-02-19 00:48:05 +00001023 if (attr != QualType::GCNone) {
Fariborz Jahanian5934e752009-02-18 18:52:41 +00001024 // __weak attribute on a field is ignored.
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00001025 if (attr == QualType::Weak)
1026 attr = QualType::GCNone;
Fariborz Jahanianc1debf32009-02-19 00:48:05 +00001027 }
Fariborz Jahanian5934e752009-02-18 18:52:41 +00001028 else if (getContext().isObjCObjectPointerType(Ty))
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00001029 attr = QualType::Strong;
Fariborz Jahanian5934e752009-02-18 18:52:41 +00001030 }
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00001031 LValue LV =
1032 LValue::MakeAddr(V,
1033 Field->getType().getCVRQualifiers()|CVRQualifiers,
1034 attr);
Fariborz Jahanian2682d8b2008-11-20 00:15:42 +00001035 return LV;
Devang Patelb9b00ad2007-10-23 20:28:39 +00001036}
1037
Chris Lattner75dfeda2009-03-18 18:28:57 +00001038LValue CodeGenFunction::EmitCompoundLiteralLValue(const CompoundLiteralExpr* E){
Eli Friedman06e863f2008-05-13 23:18:27 +00001039 const llvm::Type *LTy = ConvertType(E->getType());
1040 llvm::Value *DeclPtr = CreateTempAlloca(LTy, ".compoundliteral");
1041
1042 const Expr* InitExpr = E->getInitializer();
Eli Friedman1e692ac2008-06-13 23:01:12 +00001043 LValue Result = LValue::MakeAddr(DeclPtr, E->getType().getCVRQualifiers());
Eli Friedman06e863f2008-05-13 23:18:27 +00001044
1045 if (E->getType()->isComplexType()) {
1046 EmitComplexExprIntoAddr(InitExpr, DeclPtr, false);
1047 } else if (hasAggregateLLVMType(E->getType())) {
1048 EmitAnyExpr(InitExpr, DeclPtr, false);
1049 } else {
1050 EmitStoreThroughLValue(EmitAnyExpr(InitExpr), Result, E->getType());
1051 }
1052
1053 return Result;
1054}
1055
Daniel Dunbar90345582009-03-24 02:38:23 +00001056LValue CodeGenFunction::EmitConditionalOperator(const ConditionalOperator* E) {
1057 // We don't handle vectors yet.
1058 if (E->getType()->isVectorType())
1059 return EmitUnsupportedLValue(E, "conditional operator");
1060
1061 // ?: here should be an aggregate.
1062 assert((hasAggregateLLVMType(E->getType()) &&
1063 !E->getType()->isAnyComplexType()) &&
1064 "Unexpected conditional operator!");
1065
1066 llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType()));
1067 EmitAggExpr(E, Temp, false);
1068
1069 return LValue::MakeAddr(Temp, E->getType().getCVRQualifiers(),
1070 getContext().getObjCGCAttrKind(E->getType()));
1071
1072}
1073
Chris Lattner75dfeda2009-03-18 18:28:57 +00001074/// EmitCastLValue - Casts are never lvalues. If a cast is needed by the code
1075/// generator in an lvalue context, then it must mean that we need the address
1076/// of an aggregate in order to access one of its fields. This can happen for
1077/// all the reasons that casts are permitted with aggregate result, including
1078/// noop aggregate casts, and cast from scalar to union.
1079LValue CodeGenFunction::EmitCastLValue(const CastExpr *E) {
1080 // If this is an aggregate-to-aggregate cast, just use the input's address as
1081 // the lvalue.
1082 if (getContext().hasSameUnqualifiedType(E->getType(),
1083 E->getSubExpr()->getType()))
1084 return EmitLValue(E->getSubExpr());
1085
1086 // Otherwise, we must have a cast from scalar to union.
1087 assert(E->getType()->isUnionType() && "Expected scalar-to-union cast");
1088
1089 // Casts are only lvalues when the source and destination types are the same.
1090 llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType()));
Chris Lattner40f92922009-03-18 18:30:44 +00001091 EmitAnyExpr(E->getSubExpr(), Temp, false);
Chris Lattner75dfeda2009-03-18 18:28:57 +00001092
1093 return LValue::MakeAddr(Temp, E->getType().getCVRQualifiers(),
1094 getContext().getObjCGCAttrKind(E->getType()));
1095}
1096
Reid Spencer5f016e22007-07-11 17:01:13 +00001097//===--------------------------------------------------------------------===//
1098// Expression Emission
1099//===--------------------------------------------------------------------===//
1100
Chris Lattner7016a702007-08-20 22:37:10 +00001101
Reid Spencer5f016e22007-07-11 17:01:13 +00001102RValue CodeGenFunction::EmitCallExpr(const CallExpr *E) {
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +00001103 // Builtins never have block type.
Daniel Dunbarce1d38b2009-01-09 16:50:52 +00001104 if (E->getCallee()->getType()->isBlockPointerType())
Anders Carlssonacfde802009-02-12 00:39:25 +00001105 return EmitBlockCallExpr(E);
Daniel Dunbarce1d38b2009-01-09 16:50:52 +00001106
Anders Carlsson774e7c62009-04-03 22:50:24 +00001107 if (const CXXMemberCallExpr *CE = dyn_cast<CXXMemberCallExpr>(E))
1108 return EmitCXXMemberCallExpr(CE);
1109
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +00001110 const Decl *TargetDecl = 0;
Daniel Dunbardd7b8972009-02-20 19:34:33 +00001111 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E->getCallee())) {
1112 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CE->getSubExpr())) {
1113 TargetDecl = DRE->getDecl();
1114 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(TargetDecl))
1115 if (unsigned builtinID = FD->getBuiltinID(getContext()))
1116 return EmitBuiltinExpr(FD, builtinID, E);
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +00001117 }
1118 }
1119
Chris Lattner7f02f722007-08-24 05:35:26 +00001120 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
Eli Friedman5193b8a2008-01-30 01:32:06 +00001121 return EmitCallExpr(Callee, E->getCallee()->getType(),
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +00001122 E->arg_begin(), E->arg_end(), TargetDecl);
Chris Lattnerc5e940f2007-08-31 04:44:06 +00001123}
1124
Daniel Dunbar80e62c22008-09-04 03:20:13 +00001125LValue CodeGenFunction::EmitBinaryOperatorLValue(const BinaryOperator *E) {
Chris Lattner7a574cc2009-05-12 21:28:12 +00001126 // Comma expressions just emit their LHS then their RHS as an l-value.
1127 if (E->getOpcode() == BinaryOperator::Comma) {
1128 EmitAnyExpr(E->getLHS());
1129 return EmitLValue(E->getRHS());
1130 }
1131
Daniel Dunbar80e62c22008-09-04 03:20:13 +00001132 // Can only get l-value for binary operator expressions which are a
1133 // simple assignment of aggregate type.
1134 if (E->getOpcode() != BinaryOperator::Assign)
1135 return EmitUnsupportedLValue(E, "binary l-value expression");
1136
1137 llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType()));
1138 EmitAggExpr(E, Temp, false);
1139 // FIXME: Are these qualifiers correct?
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00001140 return LValue::MakeAddr(Temp, E->getType().getCVRQualifiers(),
1141 getContext().getObjCGCAttrKind(E->getType()));
Daniel Dunbar80e62c22008-09-04 03:20:13 +00001142}
1143
Christopher Lamb22c940e2007-12-29 05:02:41 +00001144LValue CodeGenFunction::EmitCallExprLValue(const CallExpr *E) {
1145 // Can only get l-value for call expression returning aggregate type
1146 RValue RV = EmitCallExpr(E);
Eli Friedman1e692ac2008-06-13 23:01:12 +00001147 return LValue::MakeAddr(RV.getAggregateAddr(),
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00001148 E->getType().getCVRQualifiers(),
1149 getContext().getObjCGCAttrKind(E->getType()));
Christopher Lamb22c940e2007-12-29 05:02:41 +00001150}
1151
Daniel Dunbar5b5c9ef2009-02-11 20:59:32 +00001152LValue CodeGenFunction::EmitVAArgExprLValue(const VAArgExpr *E) {
1153 // FIXME: This shouldn't require another copy.
1154 llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType()));
1155 EmitAggExpr(E, Temp, false);
1156 return LValue::MakeAddr(Temp, E->getType().getCVRQualifiers());
1157}
1158
Argyrios Kyrtzidise3a09e62008-09-10 02:36:38 +00001159LValue
1160CodeGenFunction::EmitCXXConditionDeclLValue(const CXXConditionDeclExpr *E) {
1161 EmitLocalBlockVarDecl(*E->getVarDecl());
1162 return EmitDeclRefLValue(E);
1163}
1164
Daniel Dunbar0a04d772008-08-23 10:51:21 +00001165LValue CodeGenFunction::EmitObjCMessageExprLValue(const ObjCMessageExpr *E) {
1166 // Can only get l-value for message expression returning aggregate type
1167 RValue RV = EmitObjCMessageExpr(E);
1168 // FIXME: can this be volatile?
1169 return LValue::MakeAddr(RV.getAggregateAddr(),
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00001170 E->getType().getCVRQualifiers(),
1171 getContext().getObjCGCAttrKind(E->getType()));
Daniel Dunbar0a04d772008-08-23 10:51:21 +00001172}
1173
Daniel Dunbar2a031922009-04-22 05:08:15 +00001174llvm::Value *CodeGenFunction::EmitIvarOffset(const ObjCInterfaceDecl *Interface,
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +00001175 const ObjCIvarDecl *Ivar) {
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001176 return CGM.getObjCRuntime().EmitIvarOffset(*this, Interface, Ivar);
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +00001177}
1178
Fariborz Jahanian45012a72009-02-03 00:09:52 +00001179LValue CodeGenFunction::EmitLValueForIvar(QualType ObjectTy,
1180 llvm::Value *BaseValue,
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +00001181 const ObjCIvarDecl *Ivar,
1182 unsigned CVRQualifiers) {
Chris Lattner26f074b2009-04-17 17:44:48 +00001183 return CGM.getObjCRuntime().EmitObjCValueForIvar(*this, ObjectTy, BaseValue,
Daniel Dunbar525c9b72009-04-21 01:19:28 +00001184 Ivar, CVRQualifiers);
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +00001185}
1186
1187LValue CodeGenFunction::EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E) {
Anders Carlsson29b7e502008-08-25 01:53:23 +00001188 // FIXME: A lot of the code below could be shared with EmitMemberExpr.
1189 llvm::Value *BaseValue = 0;
1190 const Expr *BaseExpr = E->getBase();
1191 unsigned CVRQualifiers = 0;
Fariborz Jahanian45012a72009-02-03 00:09:52 +00001192 QualType ObjectTy;
Anders Carlsson29b7e502008-08-25 01:53:23 +00001193 if (E->isArrow()) {
1194 BaseValue = EmitScalarExpr(BaseExpr);
Daniel Dunbarf3ef07c2009-04-18 08:54:40 +00001195 const PointerType *PTy = BaseExpr->getType()->getAsPointerType();
Fariborz Jahanian45012a72009-02-03 00:09:52 +00001196 ObjectTy = PTy->getPointeeType();
1197 CVRQualifiers = ObjectTy.getCVRQualifiers();
Anders Carlsson29b7e502008-08-25 01:53:23 +00001198 } else {
1199 LValue BaseLV = EmitLValue(BaseExpr);
1200 // FIXME: this isn't right for bitfields.
1201 BaseValue = BaseLV.getAddress();
Fariborz Jahanian45012a72009-02-03 00:09:52 +00001202 ObjectTy = BaseExpr->getType();
1203 CVRQualifiers = ObjectTy.getCVRQualifiers();
Anders Carlsson29b7e502008-08-25 01:53:23 +00001204 }
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +00001205
Daniel Dunbar525c9b72009-04-21 01:19:28 +00001206 return EmitLValueForIvar(ObjectTy, BaseValue, E->getDecl(), CVRQualifiers);
Chris Lattner391d77a2008-03-30 23:03:07 +00001207}
1208
Daniel Dunbar85c59ed2008-08-29 08:11:39 +00001209LValue
1210CodeGenFunction::EmitObjCPropertyRefLValue(const ObjCPropertyRefExpr *E) {
1211 // This is a special l-value that just issues sends when we load or
1212 // store through it.
1213 return LValue::MakePropertyRef(E, E->getType().getCVRQualifiers());
1214}
1215
Fariborz Jahanian43f44702008-11-22 22:30:21 +00001216LValue
1217CodeGenFunction::EmitObjCKVCRefLValue(const ObjCKVCRefExpr *E) {
1218 // This is a special l-value that just issues sends when we load or
1219 // store through it.
1220 return LValue::MakeKVCRef(E, E->getType().getCVRQualifiers());
1221}
1222
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00001223LValue
Chris Lattner65459942009-04-25 19:35:26 +00001224CodeGenFunction::EmitObjCSuperExprLValue(const ObjCSuperExpr *E) {
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00001225 return EmitUnsupportedLValue(E, "use of super");
1226}
1227
Chris Lattner65459942009-04-25 19:35:26 +00001228LValue CodeGenFunction::EmitStmtExprLValue(const StmtExpr *E) {
1229
1230 // Can only get l-value for message expression returning aggregate type
1231 RValue RV = EmitAnyExprToTemp(E);
1232 // FIXME: can this be volatile?
1233 return LValue::MakeAddr(RV.getAggregateAddr(),
1234 E->getType().getCVRQualifiers(),
1235 getContext().getObjCGCAttrKind(E->getType()));
1236}
1237
1238
Daniel Dunbarce1d38b2009-01-09 16:50:52 +00001239RValue CodeGenFunction::EmitCallExpr(llvm::Value *Callee, QualType CalleeType,
Ted Kremenek55499762008-06-17 02:43:46 +00001240 CallExpr::const_arg_iterator ArgBeg,
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +00001241 CallExpr::const_arg_iterator ArgEnd,
1242 const Decl *TargetDecl) {
Daniel Dunbarce1d38b2009-01-09 16:50:52 +00001243 // Get the actual function type. The callee type will always be a
1244 // pointer to function type or a block pointer type.
Anders Carlsson8ac67a72009-04-07 18:53:02 +00001245 assert(CalleeType->isFunctionPointerType() &&
1246 "Call must have function pointer type!");
1247
1248 QualType FnType = CalleeType->getAsPointerType()->getPointeeType();
1249 QualType ResultType = FnType->getAsFunctionType()->getResultType();
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001250
1251 CallArgList Args;
Anders Carlsson782f3972009-04-08 23:13:16 +00001252 EmitCallArgs(Args, FnType->getAsFunctionProtoType(), ArgBeg, ArgEnd);
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001253
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001254 return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args),
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +00001255 Callee, Args, TargetDecl);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001256}