blob: 24d885cec54242f3a322e4968beb8d1aa150a360 [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
Dan Gohman4751a3a2008-05-22 00:50:06 +000073/// getAccessedFieldNo - Given an encoded value and a result number, return
74/// the input field number being accessed.
75unsigned CodeGenFunction::getAccessedFieldNo(unsigned Idx,
76 const llvm::Constant *Elts) {
77 if (isa<llvm::ConstantAggregateZero>(Elts))
78 return 0;
79
80 return cast<llvm::ConstantInt>(Elts->getOperand(Idx))->getZExtValue();
81}
82
Chris Lattnere24c4cf2007-08-31 22:49:20 +000083
Chris Lattner4b009652007-07-25 00:24:17 +000084//===----------------------------------------------------------------------===//
85// LValue Expression Emission
86//===----------------------------------------------------------------------===//
87
Daniel Dunbar900c85a2009-02-05 07:09:07 +000088RValue CodeGenFunction::GetUndefRValue(QualType Ty) {
89 if (Ty->isVoidType()) {
90 return RValue::get(0);
91 } else if (const ComplexType *CTy = Ty->getAsComplexType()) {
Daniel Dunbar8cb73402009-01-09 20:09:28 +000092 const llvm::Type *EltTy = ConvertType(CTy->getElementType());
93 llvm::Value *U = llvm::UndefValue::get(EltTy);
94 return RValue::getComplex(std::make_pair(U, U));
Daniel Dunbar900c85a2009-02-05 07:09:07 +000095 } else if (hasAggregateLLVMType(Ty)) {
96 const llvm::Type *LTy = llvm::PointerType::getUnqual(ConvertType(Ty));
97 return RValue::getAggregate(llvm::UndefValue::get(LTy));
Daniel Dunbar8cb73402009-01-09 20:09:28 +000098 } else {
Daniel Dunbar900c85a2009-02-05 07:09:07 +000099 return RValue::get(llvm::UndefValue::get(ConvertType(Ty)));
Daniel Dunbar8cb73402009-01-09 20:09:28 +0000100 }
Daniel Dunbare3a6a682009-01-09 16:50:52 +0000101}
102
Daniel Dunbar900c85a2009-02-05 07:09:07 +0000103RValue CodeGenFunction::EmitUnsupportedRValue(const Expr *E,
104 const char *Name) {
105 ErrorUnsupported(E, Name);
106 return GetUndefRValue(E->getType());
107}
108
Daniel Dunbarde1bd942008-08-25 20:45:57 +0000109LValue CodeGenFunction::EmitUnsupportedLValue(const Expr *E,
110 const char *Name) {
111 ErrorUnsupported(E, Name);
112 llvm::Type *Ty = llvm::PointerType::getUnqual(ConvertType(E->getType()));
113 return LValue::MakeAddr(llvm::UndefValue::get(Ty),
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +0000114 E->getType().getCVRQualifiers(),
115 getContext().getObjCGCAttrKind(E->getType()));
Daniel Dunbarde1bd942008-08-25 20:45:57 +0000116}
117
Chris Lattner4b009652007-07-25 00:24:17 +0000118/// EmitLValue - Emit code to compute a designator that specifies the location
119/// of the expression.
120///
121/// This can return one of two things: a simple address or a bitfield
122/// reference. In either case, the LLVM Value* in the LValue structure is
123/// guaranteed to be an LLVM pointer type.
124///
125/// If this returns a bitfield reference, nothing about the pointee type of
126/// the LLVM value is known: For example, it may not be a pointer to an
127/// integer.
128///
129/// If this returns a normal address, and if the lvalue's C type is fixed
130/// size, this method guarantees that the returned pointer type will point to
131/// an LLVM type of the same size of the lvalue's type. If the lvalue has a
132/// variable length type, this is not possible.
133///
134LValue CodeGenFunction::EmitLValue(const Expr *E) {
135 switch (E->getStmtClass()) {
Daniel Dunbarde1bd942008-08-25 20:45:57 +0000136 default: return EmitUnsupportedLValue(E, "l-value expression");
Chris Lattner4b009652007-07-25 00:24:17 +0000137
Daniel Dunbaref0d4c72008-09-04 03:20:13 +0000138 case Expr::BinaryOperatorClass:
139 return EmitBinaryOperatorLValue(cast<BinaryOperator>(E));
Douglas Gregor65fedaf2008-11-14 16:09:21 +0000140 case Expr::CallExprClass:
141 case Expr::CXXOperatorCallExprClass:
142 return EmitCallExprLValue(cast<CallExpr>(E));
Daniel Dunbar95d08f22009-02-11 20:59:32 +0000143 case Expr::VAArgExprClass:
144 return EmitVAArgExprLValue(cast<VAArgExpr>(E));
Douglas Gregor566782a2009-01-06 05:10:23 +0000145 case Expr::DeclRefExprClass:
146 case Expr::QualifiedDeclRefExprClass:
147 return EmitDeclRefLValue(cast<DeclRefExpr>(E));
Chris Lattner4b009652007-07-25 00:24:17 +0000148 case Expr::ParenExprClass:return EmitLValue(cast<ParenExpr>(E)->getSubExpr());
Chris Lattner69909292008-08-10 01:53:14 +0000149 case Expr::PredefinedExprClass:
150 return EmitPredefinedLValue(cast<PredefinedExpr>(E));
Chris Lattner4b009652007-07-25 00:24:17 +0000151 case Expr::StringLiteralClass:
152 return EmitStringLiteralLValue(cast<StringLiteral>(E));
Chris Lattnerc5d32632009-02-24 22:18:39 +0000153 case Expr::ObjCEncodeExprClass:
154 return EmitObjCEncodeExprLValue(cast<ObjCEncodeExpr>(E));
Chris Lattnerb326b172008-03-30 23:03:07 +0000155
Mike Stump2b6933f2009-02-28 09:07:16 +0000156 case Expr::BlockDeclRefExprClass:
157 return EmitBlockDeclRefLValue(cast<BlockDeclRefExpr>(E));
158
Argiris Kirtzidisbf615b02008-09-10 02:36:38 +0000159 case Expr::CXXConditionDeclExprClass:
160 return EmitCXXConditionDeclLValue(cast<CXXConditionDeclExpr>(E));
161
Daniel Dunbar5e105892008-08-23 10:51:21 +0000162 case Expr::ObjCMessageExprClass:
163 return EmitObjCMessageExprLValue(cast<ObjCMessageExpr>(E));
Chris Lattnerb326b172008-03-30 23:03:07 +0000164 case Expr::ObjCIvarRefExprClass:
165 return EmitObjCIvarRefLValue(cast<ObjCIvarRefExpr>(E));
Daniel Dunbarde1bd942008-08-25 20:45:57 +0000166 case Expr::ObjCPropertyRefExprClass:
Daniel Dunbare6c31752008-08-29 08:11:39 +0000167 return EmitObjCPropertyRefLValue(cast<ObjCPropertyRefExpr>(E));
Fariborz Jahanianb0973da2008-11-22 22:30:21 +0000168 case Expr::ObjCKVCRefExprClass:
169 return EmitObjCKVCRefLValue(cast<ObjCKVCRefExpr>(E));
Douglas Gregord8606632008-11-04 14:56:14 +0000170 case Expr::ObjCSuperExprClass:
Chris Lattnereec3a592009-04-25 19:35:26 +0000171 return EmitObjCSuperExprLValue(cast<ObjCSuperExpr>(E));
Douglas Gregord8606632008-11-04 14:56:14 +0000172
Chris Lattnereec3a592009-04-25 19:35:26 +0000173 case Expr::StmtExprClass:
174 return EmitStmtExprLValue(cast<StmtExpr>(E));
Chris Lattner4b009652007-07-25 00:24:17 +0000175 case Expr::UnaryOperatorClass:
176 return EmitUnaryOpLValue(cast<UnaryOperator>(E));
177 case Expr::ArraySubscriptExprClass:
178 return EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E));
Nate Begemanaf6ed502008-04-18 23:10:10 +0000179 case Expr::ExtVectorElementExprClass:
180 return EmitExtVectorElementExpr(cast<ExtVectorElementExpr>(E));
Devang Patel41b66252007-10-23 20:28:39 +0000181 case Expr::MemberExprClass: return EmitMemberExpr(cast<MemberExpr>(E));
Eli Friedmanf3c2cb42008-05-13 23:18:27 +0000182 case Expr::CompoundLiteralExprClass:
183 return EmitCompoundLiteralLValue(cast<CompoundLiteralExpr>(E));
Daniel Dunbaraecb4932009-03-24 02:38:23 +0000184 case Expr::ConditionalOperatorClass:
185 return EmitConditionalOperator(cast<ConditionalOperator>(E));
Chris Lattnera5f779dc2008-12-12 05:35:08 +0000186 case Expr::ChooseExprClass:
Eli Friedmand540c112009-03-04 05:52:32 +0000187 return EmitLValue(cast<ChooseExpr>(E)->getChosenSubExpr(getContext()));
Chris Lattner504239f2009-03-18 04:02:57 +0000188 case Expr::ImplicitCastExprClass:
189 case Expr::CStyleCastExprClass:
190 case Expr::CXXFunctionalCastExprClass:
191 case Expr::CXXStaticCastExprClass:
192 case Expr::CXXDynamicCastExprClass:
193 case Expr::CXXReinterpretCastExprClass:
194 case Expr::CXXConstCastExprClass:
Chris Lattner22523ba2009-03-18 18:28:57 +0000195 return EmitCastLValue(cast<CastExpr>(E));
Chris Lattner4b009652007-07-25 00:24:17 +0000196 }
197}
198
Daniel Dunbarf1c5d842009-02-10 00:57:50 +0000199llvm::Value *CodeGenFunction::EmitLoadOfScalar(llvm::Value *Addr, bool Volatile,
200 QualType Ty) {
201 llvm::Value *V = Builder.CreateLoad(Addr, Volatile, "tmp");
202
203 // Bool can have different representation in memory than in
204 // registers.
205 if (Ty->isBooleanType())
206 if (V->getType() != llvm::Type::Int1Ty)
207 V = Builder.CreateTrunc(V, llvm::Type::Int1Ty, "tobool");
208
209 return V;
210}
211
212void CodeGenFunction::EmitStoreOfScalar(llvm::Value *Value, llvm::Value *Addr,
Anders Carlsson05dfa992009-05-19 18:50:41 +0000213 bool Volatile, QualType Ty) {
Mike Stumpba2cb0e2009-05-16 07:57:57 +0000214 // Handle stores of types which have different representations in memory and
215 // as LLVM values.
Daniel Dunbarf1c5d842009-02-10 00:57:50 +0000216
Mike Stumpba2cb0e2009-05-16 07:57:57 +0000217 // FIXME: We shouldn't be this loose, we should only do this conversion when
218 // we have a type we know has a different memory representation (e.g., bool).
Daniel Dunbarf1c5d842009-02-10 00:57:50 +0000219
220 const llvm::Type *SrcTy = Value->getType();
221 const llvm::PointerType *DstPtr = cast<llvm::PointerType>(Addr->getType());
222 if (DstPtr->getElementType() != SrcTy) {
223 const llvm::Type *MemTy =
224 llvm::PointerType::get(SrcTy, DstPtr->getAddressSpace());
225 Addr = Builder.CreateBitCast(Addr, MemTy, "storetmp");
226 }
227
228 Builder.CreateStore(Value, Addr, Volatile);
229}
230
Chris Lattner4b009652007-07-25 00:24:17 +0000231/// EmitLoadOfLValue - Given an expression that represents a value lvalue,
232/// this method emits the address of the lvalue, then loads the result as an
233/// rvalue, returning the rvalue.
234RValue CodeGenFunction::EmitLoadOfLValue(LValue LV, QualType ExprType) {
Fariborz Jahaniand2f661a2008-11-19 17:34:06 +0000235 if (LV.isObjCWeak()) {
Fariborz Jahanian3305ad32008-11-18 21:45:40 +0000236 // load of a __weak object.
237 llvm::Value *AddrWeakObj = LV.getAddress();
Fariborz Jahanian252d87f2008-11-18 22:37:34 +0000238 llvm::Value *read_weak = CGM.getObjCRuntime().EmitObjCWeakRead(*this,
Fariborz Jahanian3305ad32008-11-18 21:45:40 +0000239 AddrWeakObj);
240 return RValue::get(read_weak);
241 }
242
Chris Lattner4b009652007-07-25 00:24:17 +0000243 if (LV.isSimple()) {
244 llvm::Value *Ptr = LV.getAddress();
245 const llvm::Type *EltTy =
246 cast<llvm::PointerType>(Ptr->getType())->getElementType();
247
248 // Simple scalar l-value.
Daniel Dunbarf1c5d842009-02-10 00:57:50 +0000249 if (EltTy->isSingleValueType())
250 return RValue::get(EmitLoadOfScalar(Ptr, LV.isVolatileQualified(),
251 ExprType));
Chris Lattner4b009652007-07-25 00:24:17 +0000252
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000253 assert(ExprType->isFunctionType() && "Unknown scalar value");
254 return RValue::get(Ptr);
Chris Lattner4b009652007-07-25 00:24:17 +0000255 }
256
257 if (LV.isVectorElt()) {
Eli Friedman2e630542008-06-13 23:01:12 +0000258 llvm::Value *Vec = Builder.CreateLoad(LV.getVectorAddr(),
259 LV.isVolatileQualified(), "tmp");
Chris Lattner4b009652007-07-25 00:24:17 +0000260 return RValue::get(Builder.CreateExtractElement(Vec, LV.getVectorIdx(),
261 "vecext"));
262 }
Chris Lattnera735fac2007-08-03 00:16:29 +0000263
264 // If this is a reference to a subset of the elements of a vector, either
265 // shuffle the input or extract/insert them as appropriate.
Nate Begemanaf6ed502008-04-18 23:10:10 +0000266 if (LV.isExtVectorElt())
267 return EmitLoadOfExtVectorElementLValue(LV, ExprType);
Lauro Ramos Venanciob40307c2008-01-22 20:17:04 +0000268
269 if (LV.isBitfield())
270 return EmitLoadOfBitfieldLValue(LV, ExprType);
271
Daniel Dunbare6c31752008-08-29 08:11:39 +0000272 if (LV.isPropertyRef())
273 return EmitLoadOfPropertyRefLValue(LV, ExprType);
274
Chris Lattner09020ee2009-02-16 21:11:58 +0000275 assert(LV.isKVCRef() && "Unknown LValue type!");
276 return EmitLoadOfKVCRefLValue(LV, ExprType);
Chris Lattner4b009652007-07-25 00:24:17 +0000277}
278
Lauro Ramos Venanciob40307c2008-01-22 20:17:04 +0000279RValue CodeGenFunction::EmitLoadOfBitfieldLValue(LValue LV,
280 QualType ExprType) {
Daniel Dunbar833b03b2008-08-06 05:08:45 +0000281 unsigned StartBit = LV.getBitfieldStartBit();
282 unsigned BitfieldSize = LV.getBitfieldSize();
Lauro Ramos Venanciob40307c2008-01-22 20:17:04 +0000283 llvm::Value *Ptr = LV.getBitfieldAddr();
Daniel Dunbar833b03b2008-08-06 05:08:45 +0000284
285 const llvm::Type *EltTy =
Lauro Ramos Venanciob40307c2008-01-22 20:17:04 +0000286 cast<llvm::PointerType>(Ptr->getType())->getElementType();
Daniel Dunbar833b03b2008-08-06 05:08:45 +0000287 unsigned EltTySize = CGM.getTargetData().getTypeSizeInBits(EltTy);
Lauro Ramos Venanciob40307c2008-01-22 20:17:04 +0000288
Daniel Dunbar833b03b2008-08-06 05:08:45 +0000289 // In some cases the bitfield may straddle two memory locations.
290 // Currently we load the entire bitfield, then do the magic to
291 // sign-extend it if necessary. This results in somewhat more code
292 // than necessary for the common case (one load), since two shifts
293 // accomplish both the masking and sign extension.
294 unsigned LowBits = std::min(BitfieldSize, EltTySize - StartBit);
295 llvm::Value *Val = Builder.CreateLoad(Ptr, LV.isVolatileQualified(), "tmp");
296
297 // Shift to proper location.
Daniel Dunbar198edd52008-11-13 02:20:34 +0000298 if (StartBit)
299 Val = Builder.CreateLShr(Val, llvm::ConstantInt::get(EltTy, StartBit),
300 "bf.lo");
Daniel Dunbar833b03b2008-08-06 05:08:45 +0000301
302 // Mask off unused bits.
303 llvm::Constant *LowMask =
304 llvm::ConstantInt::get(llvm::APInt::getLowBitsSet(EltTySize, LowBits));
305 Val = Builder.CreateAnd(Val, LowMask, "bf.lo.cleared");
306
307 // Fetch the high bits if necessary.
308 if (LowBits < BitfieldSize) {
309 unsigned HighBits = BitfieldSize - LowBits;
310 llvm::Value *HighPtr =
311 Builder.CreateGEP(Ptr, llvm::ConstantInt::get(llvm::Type::Int32Ty, 1),
312 "bf.ptr.hi");
313 llvm::Value *HighVal = Builder.CreateLoad(HighPtr,
314 LV.isVolatileQualified(),
315 "tmp");
316
317 // Mask off unused bits.
318 llvm::Constant *HighMask =
319 llvm::ConstantInt::get(llvm::APInt::getLowBitsSet(EltTySize, HighBits));
320 HighVal = Builder.CreateAnd(HighVal, HighMask, "bf.lo.cleared");
Lauro Ramos Venanciob40307c2008-01-22 20:17:04 +0000321
Daniel Dunbar833b03b2008-08-06 05:08:45 +0000322 // Shift to proper location and or in to bitfield value.
323 HighVal = Builder.CreateShl(HighVal,
324 llvm::ConstantInt::get(EltTy, LowBits));
325 Val = Builder.CreateOr(Val, HighVal, "bf.val");
326 }
Lauro Ramos Venanciob40307c2008-01-22 20:17:04 +0000327
Daniel Dunbar833b03b2008-08-06 05:08:45 +0000328 // Sign extend if necessary.
329 if (LV.isBitfieldSigned()) {
330 llvm::Value *ExtraBits = llvm::ConstantInt::get(EltTy,
331 EltTySize - BitfieldSize);
332 Val = Builder.CreateAShr(Builder.CreateShl(Val, ExtraBits),
333 ExtraBits, "bf.val.sext");
334 }
Eli Friedmana04e70d2008-05-17 20:03:47 +0000335
336 // The bitfield type and the normal type differ when the storage sizes
337 // differ (currently just _Bool).
Daniel Dunbar833b03b2008-08-06 05:08:45 +0000338 Val = Builder.CreateIntCast(Val, ConvertType(ExprType), false, "tmp");
Eli Friedmana04e70d2008-05-17 20:03:47 +0000339
Daniel Dunbar833b03b2008-08-06 05:08:45 +0000340 return RValue::get(Val);
Lauro Ramos Venanciob40307c2008-01-22 20:17:04 +0000341}
342
Daniel Dunbare6c31752008-08-29 08:11:39 +0000343RValue CodeGenFunction::EmitLoadOfPropertyRefLValue(LValue LV,
344 QualType ExprType) {
345 return EmitObjCPropertyGet(LV.getPropertyRefExpr());
346}
347
Fariborz Jahanianb0973da2008-11-22 22:30:21 +0000348RValue CodeGenFunction::EmitLoadOfKVCRefLValue(LValue LV,
349 QualType ExprType) {
350 return EmitObjCPropertyGet(LV.getKVCRefExpr());
351}
352
Nate Begeman7903d052009-01-18 06:42:49 +0000353// If this is a reference to a subset of the elements of a vector, create an
354// appropriate shufflevector.
Nate Begemanaf6ed502008-04-18 23:10:10 +0000355RValue CodeGenFunction::EmitLoadOfExtVectorElementLValue(LValue LV,
356 QualType ExprType) {
Eli Friedman2e630542008-06-13 23:01:12 +0000357 llvm::Value *Vec = Builder.CreateLoad(LV.getExtVectorAddr(),
358 LV.isVolatileQualified(), "tmp");
Chris Lattner944f7962007-08-03 16:18:34 +0000359
Nate Begemanc8e51f82008-05-09 06:41:27 +0000360 const llvm::Constant *Elts = LV.getExtVectorElts();
Chris Lattner944f7962007-08-03 16:18:34 +0000361
362 // If the result of the expression is a non-vector type, we must be
363 // extracting a single element. Just codegen as an extractelement.
Chris Lattner4b492962007-08-10 17:10:08 +0000364 const VectorType *ExprVT = ExprType->getAsVectorType();
365 if (!ExprVT) {
Dan Gohman4751a3a2008-05-22 00:50:06 +0000366 unsigned InIdx = getAccessedFieldNo(0, Elts);
Chris Lattner944f7962007-08-03 16:18:34 +0000367 llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx);
368 return RValue::get(Builder.CreateExtractElement(Vec, Elt, "tmp"));
369 }
Nate Begeman7903d052009-01-18 06:42:49 +0000370
371 // Always use shuffle vector to try to retain the original program structure
Chris Lattner4b492962007-08-10 17:10:08 +0000372 unsigned NumResultElts = ExprVT->getNumElements();
Chris Lattner944f7962007-08-03 16:18:34 +0000373
Nate Begeman7903d052009-01-18 06:42:49 +0000374 llvm::SmallVector<llvm::Constant*, 4> Mask;
Chris Lattner944f7962007-08-03 16:18:34 +0000375 for (unsigned i = 0; i != NumResultElts; ++i) {
Dan Gohman4751a3a2008-05-22 00:50:06 +0000376 unsigned InIdx = getAccessedFieldNo(i, Elts);
Nate Begeman7903d052009-01-18 06:42:49 +0000377 Mask.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx));
Chris Lattner944f7962007-08-03 16:18:34 +0000378 }
379
Nate Begeman7903d052009-01-18 06:42:49 +0000380 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
381 Vec = Builder.CreateShuffleVector(Vec,
382 llvm::UndefValue::get(Vec->getType()),
383 MaskV, "tmp");
384 return RValue::get(Vec);
Chris Lattner944f7962007-08-03 16:18:34 +0000385}
386
387
Chris Lattner4b009652007-07-25 00:24:17 +0000388
389/// EmitStoreThroughLValue - Store the specified rvalue into the specified
390/// lvalue, where both are guaranteed to the have the same type, and that type
391/// is 'Ty'.
392void CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst,
393 QualType Ty) {
Chris Lattner5bfdd232007-08-03 16:28:33 +0000394 if (!Dst.isSimple()) {
395 if (Dst.isVectorElt()) {
396 // Read/modify/write the vector, inserting the new element.
Eli Friedman2e630542008-06-13 23:01:12 +0000397 llvm::Value *Vec = Builder.CreateLoad(Dst.getVectorAddr(),
398 Dst.isVolatileQualified(), "tmp");
Chris Lattnere24c4cf2007-08-31 22:49:20 +0000399 Vec = Builder.CreateInsertElement(Vec, Src.getScalarVal(),
Chris Lattner5bfdd232007-08-03 16:28:33 +0000400 Dst.getVectorIdx(), "vecins");
Eli Friedman2e630542008-06-13 23:01:12 +0000401 Builder.CreateStore(Vec, Dst.getVectorAddr(),Dst.isVolatileQualified());
Chris Lattner5bfdd232007-08-03 16:28:33 +0000402 return;
403 }
Chris Lattner4b009652007-07-25 00:24:17 +0000404
Nate Begemanaf6ed502008-04-18 23:10:10 +0000405 // If this is an update of extended vector elements, insert them as
406 // appropriate.
407 if (Dst.isExtVectorElt())
408 return EmitStoreThroughExtVectorComponentLValue(Src, Dst, Ty);
Lauro Ramos Venancio2d7a34c2008-01-22 22:36:45 +0000409
410 if (Dst.isBitfield())
411 return EmitStoreThroughBitfieldLValue(Src, Dst, Ty);
412
Daniel Dunbare6c31752008-08-29 08:11:39 +0000413 if (Dst.isPropertyRef())
414 return EmitStoreThroughPropertyRefLValue(Src, Dst, Ty);
415
Fariborz Jahanianb0973da2008-11-22 22:30:21 +0000416 if (Dst.isKVCRef())
417 return EmitStoreThroughKVCRefLValue(Src, Dst, Ty);
418
Lauro Ramos Venancio14d39842008-01-22 22:38:35 +0000419 assert(0 && "Unknown LValue type");
Chris Lattner5bfdd232007-08-03 16:28:33 +0000420 }
Chris Lattner4b009652007-07-25 00:24:17 +0000421
Fariborz Jahaniana4c010e2009-02-21 00:30:43 +0000422 if (Dst.isObjCWeak() && !Dst.isNonGC()) {
Fariborz Jahaniand2f661a2008-11-19 17:34:06 +0000423 // load of a __weak object.
424 llvm::Value *LvalueDst = Dst.getAddress();
425 llvm::Value *src = Src.getScalarVal();
Mike Stumpf41021d2009-04-14 00:57:29 +0000426 CGM.getObjCRuntime().EmitObjCWeakAssign(*this, src, LvalueDst);
Fariborz Jahaniand2f661a2008-11-19 17:34:06 +0000427 return;
428 }
429
Fariborz Jahaniana4c010e2009-02-21 00:30:43 +0000430 if (Dst.isObjCStrong() && !Dst.isNonGC()) {
Fariborz Jahaniand2f661a2008-11-19 17:34:06 +0000431 // load of a __strong object.
432 llvm::Value *LvalueDst = Dst.getAddress();
433 llvm::Value *src = Src.getScalarVal();
Fariborz Jahanian5a0c3412009-02-19 18:29:24 +0000434#if 0
Mike Stumpba2cb0e2009-05-16 07:57:57 +0000435 // FIXME. We cannot positively determine if we have an 'ivar' assignment,
436 // object assignment or an unknown assignment. For now, generate call to
437 // objc_assign_strongCast assignment which is a safe, but consevative
438 // assumption.
Fariborz Jahanian70522662008-11-20 20:53:20 +0000439 if (Dst.isObjCIvar())
440 CGM.getObjCRuntime().EmitObjCIvarAssign(*this, src, LvalueDst);
441 else
442 CGM.getObjCRuntime().EmitObjCGlobalAssign(*this, src, LvalueDst);
Fariborz Jahanian5a0c3412009-02-19 18:29:24 +0000443#endif
Fariborz Jahanian955b39c2009-05-04 23:27:20 +0000444 if (Dst.isGlobalObjCRef())
445 CGM.getObjCRuntime().EmitObjCGlobalAssign(*this, src, LvalueDst);
446 else
447 CGM.getObjCRuntime().EmitObjCStrongCastAssign(*this, src, LvalueDst);
Fariborz Jahaniand2f661a2008-11-19 17:34:06 +0000448 return;
449 }
450
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000451 assert(Src.isScalar() && "Can't emit an agg store with this method");
Anders Carlsson05dfa992009-05-19 18:50:41 +0000452 EmitStoreOfScalar(Src.getScalarVal(), Dst.getAddress(),
453 Dst.isVolatileQualified(), Ty);
Chris Lattner4b009652007-07-25 00:24:17 +0000454}
455
Lauro Ramos Venancio2d7a34c2008-01-22 22:36:45 +0000456void CodeGenFunction::EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst,
Daniel Dunbar2668dd12008-11-19 09:36:46 +0000457 QualType Ty,
458 llvm::Value **Result) {
Daniel Dunbar833b03b2008-08-06 05:08:45 +0000459 unsigned StartBit = Dst.getBitfieldStartBit();
460 unsigned BitfieldSize = Dst.getBitfieldSize();
Lauro Ramos Venancio2d7a34c2008-01-22 22:36:45 +0000461 llvm::Value *Ptr = Dst.getBitfieldAddr();
Lauro Ramos Venancio2d7a34c2008-01-22 22:36:45 +0000462
Daniel Dunbar833b03b2008-08-06 05:08:45 +0000463 const llvm::Type *EltTy =
464 cast<llvm::PointerType>(Ptr->getType())->getElementType();
465 unsigned EltTySize = CGM.getTargetData().getTypeSizeInBits(EltTy);
466
467 // Get the new value, cast to the appropriate type and masked to
468 // exactly the size of the bit-field.
Daniel Dunbar2668dd12008-11-19 09:36:46 +0000469 llvm::Value *SrcVal = Src.getScalarVal();
470 llvm::Value *NewVal = Builder.CreateIntCast(SrcVal, EltTy, false, "tmp");
Daniel Dunbar833b03b2008-08-06 05:08:45 +0000471 llvm::Constant *Mask =
472 llvm::ConstantInt::get(llvm::APInt::getLowBitsSet(EltTySize, BitfieldSize));
473 NewVal = Builder.CreateAnd(NewVal, Mask, "bf.value");
Lauro Ramos Venancio2d7a34c2008-01-22 22:36:45 +0000474
Daniel Dunbar2668dd12008-11-19 09:36:46 +0000475 // Return the new value of the bit-field, if requested.
476 if (Result) {
477 // Cast back to the proper type for result.
478 const llvm::Type *SrcTy = SrcVal->getType();
479 llvm::Value *SrcTrunc = Builder.CreateIntCast(NewVal, SrcTy, false,
480 "bf.reload.val");
481
482 // Sign extend if necessary.
483 if (Dst.isBitfieldSigned()) {
484 unsigned SrcTySize = CGM.getTargetData().getTypeSizeInBits(SrcTy);
485 llvm::Value *ExtraBits = llvm::ConstantInt::get(SrcTy,
486 SrcTySize - BitfieldSize);
487 SrcTrunc = Builder.CreateAShr(Builder.CreateShl(SrcTrunc, ExtraBits),
488 ExtraBits, "bf.reload.sext");
489 }
490
491 *Result = SrcTrunc;
492 }
493
Daniel Dunbar833b03b2008-08-06 05:08:45 +0000494 // In some cases the bitfield may straddle two memory locations.
495 // Emit the low part first and check to see if the high needs to be
496 // done.
497 unsigned LowBits = std::min(BitfieldSize, EltTySize - StartBit);
498 llvm::Value *LowVal = Builder.CreateLoad(Ptr, Dst.isVolatileQualified(),
499 "bf.prev.low");
Eli Friedmana04e70d2008-05-17 20:03:47 +0000500
Daniel Dunbar833b03b2008-08-06 05:08:45 +0000501 // Compute the mask for zero-ing the low part of this bitfield.
502 llvm::Constant *InvMask =
503 llvm::ConstantInt::get(~llvm::APInt::getBitsSet(EltTySize, StartBit,
504 StartBit + LowBits));
505
506 // Compute the new low part as
507 // LowVal = (LowVal & InvMask) | (NewVal << StartBit),
508 // with the shift of NewVal implicitly stripping the high bits.
509 llvm::Value *NewLowVal =
510 Builder.CreateShl(NewVal, llvm::ConstantInt::get(EltTy, StartBit),
511 "bf.value.lo");
512 LowVal = Builder.CreateAnd(LowVal, InvMask, "bf.prev.lo.cleared");
513 LowVal = Builder.CreateOr(LowVal, NewLowVal, "bf.new.lo");
514
515 // Write back.
516 Builder.CreateStore(LowVal, Ptr, Dst.isVolatileQualified());
Eli Friedmana04e70d2008-05-17 20:03:47 +0000517
Daniel Dunbar833b03b2008-08-06 05:08:45 +0000518 // If the low part doesn't cover the bitfield emit a high part.
519 if (LowBits < BitfieldSize) {
520 unsigned HighBits = BitfieldSize - LowBits;
521 llvm::Value *HighPtr =
522 Builder.CreateGEP(Ptr, llvm::ConstantInt::get(llvm::Type::Int32Ty, 1),
523 "bf.ptr.hi");
524 llvm::Value *HighVal = Builder.CreateLoad(HighPtr,
525 Dst.isVolatileQualified(),
526 "bf.prev.hi");
527
528 // Compute the mask for zero-ing the high part of this bitfield.
529 llvm::Constant *InvMask =
530 llvm::ConstantInt::get(~llvm::APInt::getLowBitsSet(EltTySize, HighBits));
531
532 // Compute the new high part as
533 // HighVal = (HighVal & InvMask) | (NewVal lshr LowBits),
534 // where the high bits of NewVal have already been cleared and the
535 // shift stripping the low bits.
536 llvm::Value *NewHighVal =
537 Builder.CreateLShr(NewVal, llvm::ConstantInt::get(EltTy, LowBits),
538 "bf.value.high");
539 HighVal = Builder.CreateAnd(HighVal, InvMask, "bf.prev.hi.cleared");
540 HighVal = Builder.CreateOr(HighVal, NewHighVal, "bf.new.hi");
541
542 // Write back.
543 Builder.CreateStore(HighVal, HighPtr, Dst.isVolatileQualified());
544 }
Lauro Ramos Venancio2d7a34c2008-01-22 22:36:45 +0000545}
546
Daniel Dunbare6c31752008-08-29 08:11:39 +0000547void CodeGenFunction::EmitStoreThroughPropertyRefLValue(RValue Src,
548 LValue Dst,
549 QualType Ty) {
550 EmitObjCPropertySet(Dst.getPropertyRefExpr(), Src);
551}
552
Fariborz Jahanianb0973da2008-11-22 22:30:21 +0000553void CodeGenFunction::EmitStoreThroughKVCRefLValue(RValue Src,
554 LValue Dst,
555 QualType Ty) {
556 EmitObjCPropertySet(Dst.getKVCRefExpr(), Src);
557}
558
Nate Begemanaf6ed502008-04-18 23:10:10 +0000559void CodeGenFunction::EmitStoreThroughExtVectorComponentLValue(RValue Src,
560 LValue Dst,
561 QualType Ty) {
Chris Lattner5bfdd232007-08-03 16:28:33 +0000562 // This access turns into a read/modify/write of the vector. Load the input
563 // value now.
Eli Friedman2e630542008-06-13 23:01:12 +0000564 llvm::Value *Vec = Builder.CreateLoad(Dst.getExtVectorAddr(),
565 Dst.isVolatileQualified(), "tmp");
Nate Begemanc8e51f82008-05-09 06:41:27 +0000566 const llvm::Constant *Elts = Dst.getExtVectorElts();
Chris Lattner5bfdd232007-08-03 16:28:33 +0000567
Chris Lattnere24c4cf2007-08-31 22:49:20 +0000568 llvm::Value *SrcVal = Src.getScalarVal();
Chris Lattner5bfdd232007-08-03 16:28:33 +0000569
Chris Lattner940966d2007-08-03 16:37:04 +0000570 if (const VectorType *VTy = Ty->getAsVectorType()) {
571 unsigned NumSrcElts = VTy->getNumElements();
Nate Begeman7903d052009-01-18 06:42:49 +0000572 unsigned NumDstElts =
573 cast<llvm::VectorType>(Vec->getType())->getNumElements();
574 if (NumDstElts == NumSrcElts) {
575 // Use shuffle vector is the src and destination are the same number
576 // of elements
577 llvm::SmallVector<llvm::Constant*, 4> Mask;
578 for (unsigned i = 0; i != NumSrcElts; ++i) {
579 unsigned InIdx = getAccessedFieldNo(i, Elts);
580 Mask.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx));
581 }
582
583 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
584 Vec = Builder.CreateShuffleVector(SrcVal,
585 llvm::UndefValue::get(Vec->getType()),
586 MaskV, "tmp");
587 }
588 else if (NumDstElts > NumSrcElts) {
589 // Extended the source vector to the same length and then shuffle it
590 // into the destination.
591 // FIXME: since we're shuffling with undef, can we just use the indices
592 // into that? This could be simpler.
593 llvm::SmallVector<llvm::Constant*, 4> ExtMask;
594 unsigned i;
595 for (i = 0; i != NumSrcElts; ++i)
596 ExtMask.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, i));
597 for (; i != NumDstElts; ++i)
598 ExtMask.push_back(llvm::UndefValue::get(llvm::Type::Int32Ty));
599 llvm::Value *ExtMaskV = llvm::ConstantVector::get(&ExtMask[0],
600 ExtMask.size());
Daniel Dunbar6a3b16e2009-02-17 18:31:04 +0000601 llvm::Value *ExtSrcVal =
602 Builder.CreateShuffleVector(SrcVal,
603 llvm::UndefValue::get(SrcVal->getType()),
604 ExtMaskV, "tmp");
Nate Begeman7903d052009-01-18 06:42:49 +0000605 // build identity
606 llvm::SmallVector<llvm::Constant*, 4> Mask;
607 for (unsigned i = 0; i != NumDstElts; ++i) {
608 Mask.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, i));
609 }
610 // modify when what gets shuffled in
611 for (unsigned i = 0; i != NumSrcElts; ++i) {
612 unsigned Idx = getAccessedFieldNo(i, Elts);
613 Mask[Idx] =llvm::ConstantInt::get(llvm::Type::Int32Ty, i+NumDstElts);
614 }
615 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
616 Vec = Builder.CreateShuffleVector(Vec, ExtSrcVal, MaskV, "tmp");
617 }
618 else {
619 // We should never shorten the vector
620 assert(0 && "unexpected shorten vector length");
Chris Lattner940966d2007-08-03 16:37:04 +0000621 }
622 } else {
623 // If the Src is a scalar (not a vector) it must be updating one element.
Dan Gohman4751a3a2008-05-22 00:50:06 +0000624 unsigned InIdx = getAccessedFieldNo(0, Elts);
Chris Lattner5bfdd232007-08-03 16:28:33 +0000625 llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx);
626 Vec = Builder.CreateInsertElement(Vec, SrcVal, Elt, "tmp");
Chris Lattner5bfdd232007-08-03 16:28:33 +0000627 }
628
Eli Friedman2e630542008-06-13 23:01:12 +0000629 Builder.CreateStore(Vec, Dst.getExtVectorAddr(), Dst.isVolatileQualified());
Chris Lattner5bfdd232007-08-03 16:28:33 +0000630}
631
Chris Lattner4b009652007-07-25 00:24:17 +0000632LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) {
Steve Naroff72a6ebc2008-04-15 22:42:06 +0000633 const VarDecl *VD = dyn_cast<VarDecl>(E->getDecl());
634
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000635 if (VD && (VD->isBlockVarDecl() || isa<ParmVarDecl>(VD) ||
636 isa<ImplicitParamDecl>(VD))) {
Fariborz Jahanianf0ca65f2008-11-20 00:15:42 +0000637 LValue LV;
Mike Stump1214b582009-04-14 18:24:37 +0000638 bool GCable = VD->hasLocalStorage() && !VD->hasAttr<BlocksAttr>();
Daniel Dunbar644c15e2009-04-14 02:25:56 +0000639 if (VD->hasExternalStorage()) {
Fariborz Jahanianf0ca65f2008-11-20 00:15:42 +0000640 LV = LValue::MakeAddr(CGM.GetAddrOfGlobalVar(VD),
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +0000641 E->getType().getCVRQualifiers(),
642 getContext().getObjCGCAttrKind(E->getType()));
Fariborz Jahanianf0ca65f2008-11-20 00:15:42 +0000643 }
Lauro Ramos Venancio2348d972008-02-16 22:30:38 +0000644 else {
Steve Naroff72a6ebc2008-04-15 22:42:06 +0000645 llvm::Value *V = LocalDeclMap[VD];
Mike Stump2b6933f2009-02-28 09:07:16 +0000646 assert(V && "DeclRefExpr not entered in LocalDeclMap?");
Fariborz Jahanian4d41b952009-02-20 01:14:43 +0000647 // local variables do not get their gc attribute set.
648 QualType::GCAttrTypes attr = QualType::GCNone;
649 // local static?
Mike Stumpf41021d2009-04-14 00:57:29 +0000650 if (!GCable)
Fariborz Jahanian4d41b952009-02-20 01:14:43 +0000651 attr = getContext().getObjCGCAttrKind(E->getType());
Daniel Dunbar78582862009-04-13 21:08:27 +0000652 if (VD->hasAttr<BlocksAttr>()) {
Mike Stumpad9605d2009-03-04 03:23:46 +0000653 bool needsCopyDispose = BlockRequiresCopying(VD->getType());
654 const llvm::Type *PtrStructTy = V->getType();
655 const llvm::Type *Ty = PtrStructTy;
656 Ty = llvm::PointerType::get(Ty, 0);
657 V = Builder.CreateStructGEP(V, 1, "forwarding");
658 V = Builder.CreateBitCast(V, Ty);
659 V = Builder.CreateLoad(V, false);
660 V = Builder.CreateBitCast(V, PtrStructTy);
661 V = Builder.CreateStructGEP(V, needsCopyDispose*2 + 4, "x");
662 }
Fariborz Jahanian4d41b952009-02-20 01:14:43 +0000663 LV = LValue::MakeAddr(V, E->getType().getCVRQualifiers(), attr);
Lauro Ramos Venancio2348d972008-02-16 22:30:38 +0000664 }
Mike Stumpf41021d2009-04-14 00:57:29 +0000665 LValue::SetObjCNonGC(LV, GCable);
Fariborz Jahanianf0ca65f2008-11-20 00:15:42 +0000666 return LV;
Steve Naroff72a6ebc2008-04-15 22:42:06 +0000667 } else if (VD && VD->isFileVarDecl()) {
Fariborz Jahanianc192d4d2008-11-18 20:18:11 +0000668 LValue LV = LValue::MakeAddr(CGM.GetAddrOfGlobalVar(VD),
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +0000669 E->getType().getCVRQualifiers(),
670 getContext().getObjCGCAttrKind(E->getType()));
Fariborz Jahanian955b39c2009-05-04 23:27:20 +0000671 if (LV.isObjCStrong())
672 LV.SetGlobalObjCRef(LV, true);
Fariborz Jahanianc192d4d2008-11-18 20:18:11 +0000673 return LV;
Steve Naroff72a6ebc2008-04-15 22:42:06 +0000674 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(E->getDecl())) {
Chris Lattner80f39cc2009-05-12 21:21:08 +0000675 return LValue::MakeAddr(CGM.GetAddrOfFunction(GlobalDecl(FD)),
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +0000676 E->getType().getCVRQualifiers(),
677 getContext().getObjCGCAttrKind(E->getType()));
Chris Lattner4b009652007-07-25 00:24:17 +0000678 }
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000679 else if (const ImplicitParamDecl *IPD =
680 dyn_cast<ImplicitParamDecl>(E->getDecl())) {
681 llvm::Value *V = LocalDeclMap[IPD];
682 assert(V && "BlockVarDecl not entered in LocalDeclMap?");
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +0000683 return LValue::MakeAddr(V, E->getType().getCVRQualifiers(),
684 getContext().getObjCGCAttrKind(E->getType()));
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000685 }
Chris Lattner4b009652007-07-25 00:24:17 +0000686 assert(0 && "Unimp declref");
Chris Lattner1d2b4612007-09-16 19:23:47 +0000687 //an invalid LValue, but the assert will
688 //ensure that this point is never reached.
689 return LValue();
Chris Lattner4b009652007-07-25 00:24:17 +0000690}
691
Mike Stump2b6933f2009-02-28 09:07:16 +0000692LValue CodeGenFunction::EmitBlockDeclRefLValue(const BlockDeclRefExpr *E) {
693 return LValue::MakeAddr(GetAddrOfBlockDecl(E), 0);
694}
695
Chris Lattner4b009652007-07-25 00:24:17 +0000696LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) {
697 // __extension__ doesn't affect lvalue-ness.
698 if (E->getOpcode() == UnaryOperator::Extension)
699 return EmitLValue(E->getSubExpr());
700
Chris Lattnerc154ac12008-07-26 22:37:01 +0000701 QualType ExprTy = getContext().getCanonicalType(E->getSubExpr()->getType());
Chris Lattner5bf72022007-10-30 22:53:42 +0000702 switch (E->getOpcode()) {
703 default: assert(0 && "Unknown unary operator lvalue!");
704 case UnaryOperator::Deref:
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +0000705 {
Chris Lattnerc5d32632009-02-24 22:18:39 +0000706 QualType T =
707 E->getSubExpr()->getType()->getAsPointerType()->getPointeeType();
Fariborz Jahanian2224fb22009-02-23 18:59:50 +0000708 LValue LV = LValue::MakeAddr(EmitScalarExpr(E->getSubExpr()),
709 ExprTy->getAsPointerType()->getPointeeType()
Fariborz Jahanian4d41b952009-02-20 01:14:43 +0000710 .getCVRQualifiers(),
Fariborz Jahanian2224fb22009-02-23 18:59:50 +0000711 getContext().getObjCGCAttrKind(T));
712 // We should not generate __weak write barrier on indirect reference
713 // of a pointer to object; as in void foo (__weak id *param); *param = 0;
714 // But, we continue to generate __strong write barrier on indirect write
715 // into a pointer to object.
716 if (getContext().getLangOptions().ObjC1 &&
717 getContext().getLangOptions().getGCMode() != LangOptions::NonGC &&
718 LV.isObjCWeak())
719 LValue::SetObjCNonGC(LV, !E->isOBJCGCCandidate());
720 return LV;
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +0000721 }
Chris Lattner5bf72022007-10-30 22:53:42 +0000722 case UnaryOperator::Real:
723 case UnaryOperator::Imag:
724 LValue LV = EmitLValue(E->getSubExpr());
Chris Lattner07307562008-03-19 05:19:41 +0000725 unsigned Idx = E->getOpcode() == UnaryOperator::Imag;
726 return LValue::MakeAddr(Builder.CreateStructGEP(LV.getAddress(),
Chris Lattnerd5a56aa2008-07-26 22:17:49 +0000727 Idx, "idx"),
728 ExprTy.getCVRQualifiers());
Chris Lattner5bf72022007-10-30 22:53:42 +0000729 }
Chris Lattner4b009652007-07-25 00:24:17 +0000730}
731
732LValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) {
Daniel Dunbar31fe9c32008-08-13 23:20:05 +0000733 return LValue::MakeAddr(CGM.GetAddrOfConstantStringFromLiteral(E), 0);
Chris Lattner4b009652007-07-25 00:24:17 +0000734}
735
Chris Lattnerc5d32632009-02-24 22:18:39 +0000736LValue CodeGenFunction::EmitObjCEncodeExprLValue(const ObjCEncodeExpr *E) {
737 return LValue::MakeAddr(CGM.GetAddrOfConstantStringFromObjCEncode(E), 0);
738}
739
740
Daniel Dunbara9f0be22008-10-17 21:58:32 +0000741LValue CodeGenFunction::EmitPredefinedFunctionName(unsigned Type) {
Chris Lattner4b009652007-07-25 00:24:17 +0000742 std::string GlobalVarName;
Daniel Dunbara9f0be22008-10-17 21:58:32 +0000743
744 switch (Type) {
Chris Lattnerc5d32632009-02-24 22:18:39 +0000745 default:
746 assert(0 && "Invalid type");
747 case PredefinedExpr::Func:
748 GlobalVarName = "__func__.";
749 break;
750 case PredefinedExpr::Function:
751 GlobalVarName = "__FUNCTION__.";
752 break;
753 case PredefinedExpr::PrettyFunction:
754 // FIXME:: Demangle C++ method names
755 GlobalVarName = "__PRETTY_FUNCTION__.";
756 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000757 }
Daniel Dunbara9f0be22008-10-17 21:58:32 +0000758
Chris Lattnerf6279ae2009-04-23 05:30:27 +0000759 // FIXME: This isn't right at all. The logic for computing this should go
760 // into a method on PredefinedExpr. This would allow sema and codegen to be
761 // consistent for things like sizeof(__func__) etc.
Daniel Dunbara9f0be22008-10-17 21:58:32 +0000762 std::string FunctionName;
Chris Lattnerf6279ae2009-04-23 05:30:27 +0000763 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CurCodeDecl)) {
Douglas Gregor3c3c4542009-02-18 23:53:56 +0000764 FunctionName = CGM.getMangledName(FD);
Daniel Dunbara9f0be22008-10-17 21:58:32 +0000765 } else {
Daniel Dunbara2d275d2009-04-07 05:48:37 +0000766 // Just get the mangled name; skipping the asm prefix if it
767 // exists.
Daniel Dunbara9f0be22008-10-17 21:58:32 +0000768 FunctionName = CurFn->getName();
Daniel Dunbara2d275d2009-04-07 05:48:37 +0000769 if (FunctionName[0] == '\01')
770 FunctionName = FunctionName.substr(1, std::string::npos);
Daniel Dunbara9f0be22008-10-17 21:58:32 +0000771 }
772
Chris Lattner6e6a5972008-04-04 04:07:35 +0000773 GlobalVarName += FunctionName;
Daniel Dunbara9f0be22008-10-17 21:58:32 +0000774 llvm::Constant *C =
775 CGM.GetAddrOfConstantCString(FunctionName, GlobalVarName.c_str());
776 return LValue::MakeAddr(C, 0);
777}
778
779LValue CodeGenFunction::EmitPredefinedLValue(const PredefinedExpr *E) {
780 switch (E->getIdentType()) {
781 default:
782 return EmitUnsupportedLValue(E, "predefined expression");
783 case PredefinedExpr::Func:
784 case PredefinedExpr::Function:
785 case PredefinedExpr::PrettyFunction:
786 return EmitPredefinedFunctionName(E->getIdentType());
787 }
Chris Lattner4b009652007-07-25 00:24:17 +0000788}
789
790LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) {
Ted Kremenek1c1700f2007-08-20 16:18:38 +0000791 // The index must always be an integer, which is not an aggregate. Emit it.
Chris Lattner9fba49a2007-08-24 05:35:26 +0000792 llvm::Value *Idx = EmitScalarExpr(E->getIdx());
Chris Lattner4b009652007-07-25 00:24:17 +0000793
794 // If the base is a vector type, then we are forming a vector element lvalue
795 // with this subscript.
Eli Friedman2e630542008-06-13 23:01:12 +0000796 if (E->getBase()->getType()->isVectorType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000797 // Emit the vector as an lvalue to get its address.
Eli Friedman2e630542008-06-13 23:01:12 +0000798 LValue LHS = EmitLValue(E->getBase());
Ted Kremenek1c1700f2007-08-20 16:18:38 +0000799 assert(LHS.isSimple() && "Can only subscript lvalue vectors here!");
Chris Lattner4b009652007-07-25 00:24:17 +0000800 // FIXME: This should properly sign/zero/extend or truncate Idx to i32.
Eli Friedman2e630542008-06-13 23:01:12 +0000801 return LValue::MakeVectorElt(LHS.getAddress(), Idx,
802 E->getBase()->getType().getCVRQualifiers());
Chris Lattner4b009652007-07-25 00:24:17 +0000803 }
804
Ted Kremenek1c1700f2007-08-20 16:18:38 +0000805 // The base must be a pointer, which is not an aggregate. Emit it.
Chris Lattner9fba49a2007-08-24 05:35:26 +0000806 llvm::Value *Base = EmitScalarExpr(E->getBase());
Chris Lattner4b009652007-07-25 00:24:17 +0000807
Ted Kremenek1c1700f2007-08-20 16:18:38 +0000808 // Extend or truncate the index type to 32 or 64-bits.
Chris Lattner2af72ac2007-08-08 17:43:05 +0000809 QualType IdxTy = E->getIdx()->getType();
Chris Lattner4b009652007-07-25 00:24:17 +0000810 bool IdxSigned = IdxTy->isSignedIntegerType();
811 unsigned IdxBitwidth = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
Sanjiv Guptacee8fea2009-04-24 02:40:57 +0000812 if (IdxBitwidth != LLVMPointerWidth)
813 Idx = Builder.CreateIntCast(Idx, llvm::IntegerType::get(LLVMPointerWidth),
Chris Lattner4b009652007-07-25 00:24:17 +0000814 IdxSigned, "idxprom");
815
Daniel Dunbar6864c0d2009-04-25 05:08:32 +0000816 // We know that the pointer points to a type of the correct size,
817 // unless the size is a VLA or Objective-C interface.
818 llvm::Value *Address = 0;
Anders Carlsson3bb57e82008-12-21 00:11:23 +0000819 if (const VariableArrayType *VAT =
820 getContext().getAsVariableArrayType(E->getType())) {
821 llvm::Value *VLASize = VLASizeMap[VAT];
822
823 Idx = Builder.CreateMul(Idx, VLASize);
824
Anders Carlsson76d19c82008-12-21 03:44:36 +0000825 QualType BaseType = getContext().getBaseElementType(VAT);
Anders Carlsson3bb57e82008-12-21 00:11:23 +0000826
827 uint64_t BaseTypeSize = getContext().getTypeSize(BaseType) / 8;
828 Idx = Builder.CreateUDiv(Idx,
829 llvm::ConstantInt::get(Idx->getType(),
830 BaseTypeSize));
Daniel Dunbar6864c0d2009-04-25 05:08:32 +0000831 Address = Builder.CreateGEP(Base, Idx, "arrayidx");
832 } else if (const ObjCInterfaceType *OIT =
833 dyn_cast<ObjCInterfaceType>(E->getType())) {
834 llvm::Value *InterfaceSize =
835 llvm::ConstantInt::get(Idx->getType(),
836 getContext().getTypeSize(OIT) / 8);
837
838 Idx = Builder.CreateMul(Idx, InterfaceSize);
839
840 llvm::Type *i8PTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
841 Address = Builder.CreateGEP(Builder.CreateBitCast(Base, i8PTy),
842 Idx, "arrayidx");
843 Address = Builder.CreateBitCast(Address, Base->getType());
844 } else {
845 Address = Builder.CreateGEP(Base, Idx, "arrayidx");
Anders Carlsson3bb57e82008-12-21 00:11:23 +0000846 }
847
Daniel Dunbard4271f62009-04-18 08:54:40 +0000848 QualType T = E->getBase()->getType()->getAsPointerType()->getPointeeType();
Daniel Dunbar6864c0d2009-04-25 05:08:32 +0000849 LValue LV = LValue::MakeAddr(Address,
Daniel Dunbard4271f62009-04-18 08:54:40 +0000850 T.getCVRQualifiers(),
851 getContext().getObjCGCAttrKind(T));
Fariborz Jahanian1ff3c9d2009-02-21 23:37:19 +0000852 if (getContext().getLangOptions().ObjC1 &&
853 getContext().getLangOptions().getGCMode() != LangOptions::NonGC)
Fariborz Jahanian0c195b92009-02-22 18:40:18 +0000854 LValue::SetObjCNonGC(LV, !E->isOBJCGCCandidate());
Fariborz Jahanian1ff3c9d2009-02-21 23:37:19 +0000855 return LV;
Chris Lattner4b009652007-07-25 00:24:17 +0000856}
857
Nate Begemana1ae7442008-05-13 21:03:02 +0000858static
859llvm::Constant *GenerateConstantVector(llvm::SmallVector<unsigned, 4> &Elts) {
860 llvm::SmallVector<llvm::Constant *, 4> CElts;
861
862 for (unsigned i = 0, e = Elts.size(); i != e; ++i)
863 CElts.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, Elts[i]));
864
865 return llvm::ConstantVector::get(&CElts[0], CElts.size());
866}
867
Chris Lattner65520192007-08-02 23:37:31 +0000868LValue CodeGenFunction::
Nate Begemanaf6ed502008-04-18 23:10:10 +0000869EmitExtVectorElementExpr(const ExtVectorElementExpr *E) {
Chris Lattner65520192007-08-02 23:37:31 +0000870 // Emit the base vector as an l-value.
Chris Lattner09020ee2009-02-16 21:11:58 +0000871 LValue Base;
872
873 // ExtVectorElementExpr's base can either be a vector or pointer to vector.
Chris Lattner98e7fcc2009-02-16 22:14:05 +0000874 if (!E->isArrow()) {
Chris Lattner09020ee2009-02-16 21:11:58 +0000875 assert(E->getBase()->getType()->isVectorType());
876 Base = EmitLValue(E->getBase());
Chris Lattner98e7fcc2009-02-16 22:14:05 +0000877 } else {
878 const PointerType *PT = E->getBase()->getType()->getAsPointerType();
879 llvm::Value *Ptr = EmitScalarExpr(E->getBase());
880 Base = LValue::MakeAddr(Ptr, PT->getPointeeType().getCVRQualifiers());
Chris Lattner09020ee2009-02-16 21:11:58 +0000881 }
Chris Lattner65520192007-08-02 23:37:31 +0000882
Nate Begemana1ae7442008-05-13 21:03:02 +0000883 // Encode the element access list into a vector of unsigned indices.
884 llvm::SmallVector<unsigned, 4> Indices;
885 E->getEncodedElementAccess(Indices);
886
887 if (Base.isSimple()) {
888 llvm::Constant *CV = GenerateConstantVector(Indices);
Eli Friedman2e630542008-06-13 23:01:12 +0000889 return LValue::MakeExtVectorElt(Base.getAddress(), CV,
Chris Lattner9df79c32009-02-16 22:25:49 +0000890 Base.getQualifiers());
Nate Begemana1ae7442008-05-13 21:03:02 +0000891 }
892 assert(Base.isExtVectorElt() && "Can only subscript lvalue vec elts here!");
893
894 llvm::Constant *BaseElts = Base.getExtVectorElts();
895 llvm::SmallVector<llvm::Constant *, 4> CElts;
896
897 for (unsigned i = 0, e = Indices.size(); i != e; ++i) {
898 if (isa<llvm::ConstantAggregateZero>(BaseElts))
899 CElts.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, 0));
900 else
901 CElts.push_back(BaseElts->getOperand(Indices[i]));
902 }
903 llvm::Constant *CV = llvm::ConstantVector::get(&CElts[0], CElts.size());
Eli Friedman2e630542008-06-13 23:01:12 +0000904 return LValue::MakeExtVectorElt(Base.getExtVectorAddr(), CV,
Chris Lattner9df79c32009-02-16 22:25:49 +0000905 Base.getQualifiers());
Chris Lattner65520192007-08-02 23:37:31 +0000906}
907
Devang Patel41b66252007-10-23 20:28:39 +0000908LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) {
Devang Patele1f79db2007-12-11 21:33:16 +0000909 bool isUnion = false;
Fariborz Jahanian08c06f42008-11-21 18:14:01 +0000910 bool isIvar = false;
Fariborz Jahaniana4c010e2009-02-21 00:30:43 +0000911 bool isNonGC = false;
Devang Patel9dd3e2b2007-10-24 22:26:28 +0000912 Expr *BaseExpr = E->getBase();
Devang Patel9dd3e2b2007-10-24 22:26:28 +0000913 llvm::Value *BaseValue = NULL;
Eli Friedman2e630542008-06-13 23:01:12 +0000914 unsigned CVRQualifiers=0;
915
Chris Lattner659079e2007-12-02 18:52:07 +0000916 // 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 +0000917 if (E->isArrow()) {
Devang Patel2b24fd92007-10-26 18:15:21 +0000918 BaseValue = EmitScalarExpr(BaseExpr);
Devang Patele1f79db2007-12-11 21:33:16 +0000919 const PointerType *PTy =
Daniel Dunbard4271f62009-04-18 08:54:40 +0000920 BaseExpr->getType()->getAsPointerType();
Devang Patele1f79db2007-12-11 21:33:16 +0000921 if (PTy->getPointeeType()->isUnionType())
922 isUnion = true;
Eli Friedman2e630542008-06-13 23:01:12 +0000923 CVRQualifiers = PTy->getPointeeType().getCVRQualifiers();
Chris Lattner9df79c32009-02-16 22:25:49 +0000924 } else if (isa<ObjCPropertyRefExpr>(BaseExpr) ||
925 isa<ObjCKVCRefExpr>(BaseExpr)) {
Fariborz Jahanian4e881652009-01-12 23:27:26 +0000926 RValue RV = EmitObjCPropertyGet(BaseExpr);
927 BaseValue = RV.getAggregateAddr();
928 if (BaseExpr->getType()->isUnionType())
929 isUnion = true;
930 CVRQualifiers = BaseExpr->getType().getCVRQualifiers();
Chris Lattner9df79c32009-02-16 22:25:49 +0000931 } else {
Chris Lattner659079e2007-12-02 18:52:07 +0000932 LValue BaseLV = EmitLValue(BaseExpr);
Fariborz Jahanian08c06f42008-11-21 18:14:01 +0000933 if (BaseLV.isObjCIvar())
934 isIvar = true;
Fariborz Jahaniana4c010e2009-02-21 00:30:43 +0000935 if (BaseLV.isNonGC())
936 isNonGC = true;
Chris Lattner659079e2007-12-02 18:52:07 +0000937 // FIXME: this isn't right for bitfields.
938 BaseValue = BaseLV.getAddress();
Devang Patele1f79db2007-12-11 21:33:16 +0000939 if (BaseExpr->getType()->isUnionType())
940 isUnion = true;
Eli Friedman2e630542008-06-13 23:01:12 +0000941 CVRQualifiers = BaseExpr->getType().getCVRQualifiers();
Chris Lattner659079e2007-12-02 18:52:07 +0000942 }
Devang Patel41b66252007-10-23 20:28:39 +0000943
Douglas Gregor82d44772008-12-20 23:49:58 +0000944 FieldDecl *Field = dyn_cast<FieldDecl>(E->getMemberDecl());
945 // FIXME: Handle non-field member expressions
946 assert(Field && "No code generation for non-field member references");
Chris Lattner9df79c32009-02-16 22:25:49 +0000947 LValue MemExpLV = EmitLValueForField(BaseValue, Field, isUnion,
948 CVRQualifiers);
Fariborz Jahanian08c06f42008-11-21 18:14:01 +0000949 LValue::SetObjCIvar(MemExpLV, isIvar);
Fariborz Jahaniana4c010e2009-02-21 00:30:43 +0000950 LValue::SetObjCNonGC(MemExpLV, isNonGC);
Fariborz Jahanian08c06f42008-11-21 18:14:01 +0000951 return MemExpLV;
Eli Friedmand3550112008-02-09 08:50:58 +0000952}
Devang Patel41b66252007-10-23 20:28:39 +0000953
Fariborz Jahanian86008c02008-12-15 20:35:07 +0000954LValue CodeGenFunction::EmitLValueForBitfield(llvm::Value* BaseValue,
955 FieldDecl* Field,
Fariborz Jahanianc912eb72009-02-03 19:03:09 +0000956 unsigned CVRQualifiers) {
Daniel Dunbar6a3b16e2009-02-17 18:31:04 +0000957 unsigned idx = CGM.getTypes().getLLVMFieldNo(Field);
Mike Stumpba2cb0e2009-05-16 07:57:57 +0000958 // FIXME: CodeGenTypes should expose a method to get the appropriate type for
959 // FieldTy (the appropriate type is ABI-dependent).
Daniel Dunbar6a3b16e2009-02-17 18:31:04 +0000960 const llvm::Type *FieldTy =
961 CGM.getTypes().ConvertTypeForMem(Field->getType());
Fariborz Jahanian86008c02008-12-15 20:35:07 +0000962 const llvm::PointerType *BaseTy =
963 cast<llvm::PointerType>(BaseValue->getType());
964 unsigned AS = BaseTy->getAddressSpace();
965 BaseValue = Builder.CreateBitCast(BaseValue,
966 llvm::PointerType::get(FieldTy, AS),
967 "tmp");
968 llvm::Value *V = Builder.CreateGEP(BaseValue,
969 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx),
970 "tmp");
971
972 CodeGenTypes::BitFieldInfo bitFieldInfo =
973 CGM.getTypes().getBitFieldInfo(Field);
974 return LValue::MakeBitfield(V, bitFieldInfo.Begin, bitFieldInfo.Size,
975 Field->getType()->isSignedIntegerType(),
976 Field->getType().getCVRQualifiers()|CVRQualifiers);
977}
978
Eli Friedmand3550112008-02-09 08:50:58 +0000979LValue CodeGenFunction::EmitLValueForField(llvm::Value* BaseValue,
980 FieldDecl* Field,
Eli Friedman2e630542008-06-13 23:01:12 +0000981 bool isUnion,
982 unsigned CVRQualifiers)
Eli Friedmand3550112008-02-09 08:50:58 +0000983{
Fariborz Jahanian86008c02008-12-15 20:35:07 +0000984 if (Field->isBitField())
Fariborz Jahanianc912eb72009-02-03 19:03:09 +0000985 return EmitLValueForBitfield(BaseValue, Field, CVRQualifiers);
Fariborz Jahanianf0ca65f2008-11-20 00:15:42 +0000986
Fariborz Jahanianc912eb72009-02-03 19:03:09 +0000987 unsigned idx = CGM.getTypes().getLLVMFieldNo(Field);
Fariborz Jahanian86008c02008-12-15 20:35:07 +0000988 llvm::Value *V = Builder.CreateStructGEP(BaseValue, idx, "tmp");
Eli Friedman66813742008-05-29 11:33:25 +0000989
Devang Patel9b1ca9e2007-10-26 19:42:18 +0000990 // Match union field type.
Lauro Ramos Venancio63fc38f2008-02-07 19:29:53 +0000991 if (isUnion) {
Eli Friedman2e630542008-06-13 23:01:12 +0000992 const llvm::Type *FieldTy =
993 CGM.getTypes().ConvertTypeForMem(Field->getType());
Devang Patel0f2a8fb2007-10-30 20:59:40 +0000994 const llvm::PointerType * BaseTy =
995 cast<llvm::PointerType>(BaseValue->getType());
Eli Friedmancecdc6b2008-05-21 13:24:44 +0000996 unsigned AS = BaseTy->getAddressSpace();
997 V = Builder.CreateBitCast(V,
998 llvm::PointerType::get(FieldTy, AS),
999 "tmp");
Devang Patel9b1ca9e2007-10-26 19:42:18 +00001000 }
Lauro Ramos Venanciob40307c2008-01-22 20:17:04 +00001001
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +00001002 QualType::GCAttrTypes attr = QualType::GCNone;
Fariborz Jahanian80ff83c2009-02-18 17:52:36 +00001003 if (CGM.getLangOptions().ObjC1 &&
Fariborz Jahanian31804e12009-02-18 18:52:41 +00001004 CGM.getLangOptions().getGCMode() != LangOptions::NonGC) {
1005 QualType Ty = Field->getType();
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +00001006 attr = Ty.getObjCGCAttr();
Fariborz Jahaniancc59d472009-02-19 00:48:05 +00001007 if (attr != QualType::GCNone) {
Fariborz Jahanian31804e12009-02-18 18:52:41 +00001008 // __weak attribute on a field is ignored.
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +00001009 if (attr == QualType::Weak)
1010 attr = QualType::GCNone;
Fariborz Jahaniancc59d472009-02-19 00:48:05 +00001011 }
Fariborz Jahanian31804e12009-02-18 18:52:41 +00001012 else if (getContext().isObjCObjectPointerType(Ty))
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +00001013 attr = QualType::Strong;
Fariborz Jahanian31804e12009-02-18 18:52:41 +00001014 }
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +00001015 LValue LV =
1016 LValue::MakeAddr(V,
1017 Field->getType().getCVRQualifiers()|CVRQualifiers,
1018 attr);
Fariborz Jahanianf0ca65f2008-11-20 00:15:42 +00001019 return LV;
Devang Patel41b66252007-10-23 20:28:39 +00001020}
1021
Chris Lattner22523ba2009-03-18 18:28:57 +00001022LValue CodeGenFunction::EmitCompoundLiteralLValue(const CompoundLiteralExpr* E){
Eli Friedmanf3c2cb42008-05-13 23:18:27 +00001023 const llvm::Type *LTy = ConvertType(E->getType());
1024 llvm::Value *DeclPtr = CreateTempAlloca(LTy, ".compoundliteral");
1025
1026 const Expr* InitExpr = E->getInitializer();
Eli Friedman2e630542008-06-13 23:01:12 +00001027 LValue Result = LValue::MakeAddr(DeclPtr, E->getType().getCVRQualifiers());
Eli Friedmanf3c2cb42008-05-13 23:18:27 +00001028
1029 if (E->getType()->isComplexType()) {
1030 EmitComplexExprIntoAddr(InitExpr, DeclPtr, false);
1031 } else if (hasAggregateLLVMType(E->getType())) {
1032 EmitAnyExpr(InitExpr, DeclPtr, false);
1033 } else {
1034 EmitStoreThroughLValue(EmitAnyExpr(InitExpr), Result, E->getType());
1035 }
1036
1037 return Result;
1038}
1039
Daniel Dunbaraecb4932009-03-24 02:38:23 +00001040LValue CodeGenFunction::EmitConditionalOperator(const ConditionalOperator* E) {
1041 // We don't handle vectors yet.
1042 if (E->getType()->isVectorType())
1043 return EmitUnsupportedLValue(E, "conditional operator");
1044
1045 // ?: here should be an aggregate.
1046 assert((hasAggregateLLVMType(E->getType()) &&
1047 !E->getType()->isAnyComplexType()) &&
1048 "Unexpected conditional operator!");
1049
1050 llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType()));
1051 EmitAggExpr(E, Temp, false);
1052
1053 return LValue::MakeAddr(Temp, E->getType().getCVRQualifiers(),
1054 getContext().getObjCGCAttrKind(E->getType()));
1055
1056}
1057
Chris Lattner22523ba2009-03-18 18:28:57 +00001058/// EmitCastLValue - Casts are never lvalues. If a cast is needed by the code
1059/// generator in an lvalue context, then it must mean that we need the address
1060/// of an aggregate in order to access one of its fields. This can happen for
1061/// all the reasons that casts are permitted with aggregate result, including
1062/// noop aggregate casts, and cast from scalar to union.
1063LValue CodeGenFunction::EmitCastLValue(const CastExpr *E) {
1064 // If this is an aggregate-to-aggregate cast, just use the input's address as
1065 // the lvalue.
1066 if (getContext().hasSameUnqualifiedType(E->getType(),
1067 E->getSubExpr()->getType()))
1068 return EmitLValue(E->getSubExpr());
1069
1070 // Otherwise, we must have a cast from scalar to union.
1071 assert(E->getType()->isUnionType() && "Expected scalar-to-union cast");
1072
1073 // Casts are only lvalues when the source and destination types are the same.
1074 llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType()));
Chris Lattner96a3a2d2009-03-18 18:30:44 +00001075 EmitAnyExpr(E->getSubExpr(), Temp, false);
Chris Lattner22523ba2009-03-18 18:28:57 +00001076
1077 return LValue::MakeAddr(Temp, E->getType().getCVRQualifiers(),
1078 getContext().getObjCGCAttrKind(E->getType()));
1079}
1080
Chris Lattner4b009652007-07-25 00:24:17 +00001081//===--------------------------------------------------------------------===//
1082// Expression Emission
1083//===--------------------------------------------------------------------===//
1084
Chris Lattnerb2cb9cb2007-08-20 22:37:10 +00001085
Chris Lattner4b009652007-07-25 00:24:17 +00001086RValue CodeGenFunction::EmitCallExpr(const CallExpr *E) {
Daniel Dunbar191eb9e2009-02-20 18:06:48 +00001087 // Builtins never have block type.
Daniel Dunbare3a6a682009-01-09 16:50:52 +00001088 if (E->getCallee()->getType()->isBlockPointerType())
Anders Carlssond2a889b2009-02-12 00:39:25 +00001089 return EmitBlockCallExpr(E);
Daniel Dunbare3a6a682009-01-09 16:50:52 +00001090
Anders Carlsson7a9b2982009-04-03 22:50:24 +00001091 if (const CXXMemberCallExpr *CE = dyn_cast<CXXMemberCallExpr>(E))
1092 return EmitCXXMemberCallExpr(CE);
1093
Daniel Dunbar191eb9e2009-02-20 18:06:48 +00001094 const Decl *TargetDecl = 0;
Daniel Dunbar337f60a2009-02-20 19:34:33 +00001095 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E->getCallee())) {
1096 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CE->getSubExpr())) {
1097 TargetDecl = DRE->getDecl();
1098 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(TargetDecl))
1099 if (unsigned builtinID = FD->getBuiltinID(getContext()))
1100 return EmitBuiltinExpr(FD, builtinID, E);
Daniel Dunbar191eb9e2009-02-20 18:06:48 +00001101 }
1102 }
1103
Chris Lattner9fba49a2007-08-24 05:35:26 +00001104 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
Eli Friedman261f4ad2008-01-30 01:32:06 +00001105 return EmitCallExpr(Callee, E->getCallee()->getType(),
Daniel Dunbar191eb9e2009-02-20 18:06:48 +00001106 E->arg_begin(), E->arg_end(), TargetDecl);
Chris Lattner02c60f52007-08-31 04:44:06 +00001107}
1108
Daniel Dunbaref0d4c72008-09-04 03:20:13 +00001109LValue CodeGenFunction::EmitBinaryOperatorLValue(const BinaryOperator *E) {
Chris Lattnerb7062332009-05-12 21:28:12 +00001110 // Comma expressions just emit their LHS then their RHS as an l-value.
1111 if (E->getOpcode() == BinaryOperator::Comma) {
1112 EmitAnyExpr(E->getLHS());
1113 return EmitLValue(E->getRHS());
1114 }
1115
Daniel Dunbaref0d4c72008-09-04 03:20:13 +00001116 // Can only get l-value for binary operator expressions which are a
1117 // simple assignment of aggregate type.
1118 if (E->getOpcode() != BinaryOperator::Assign)
1119 return EmitUnsupportedLValue(E, "binary l-value expression");
1120
1121 llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType()));
1122 EmitAggExpr(E, Temp, false);
1123 // FIXME: Are these qualifiers correct?
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +00001124 return LValue::MakeAddr(Temp, E->getType().getCVRQualifiers(),
1125 getContext().getObjCGCAttrKind(E->getType()));
Daniel Dunbaref0d4c72008-09-04 03:20:13 +00001126}
1127
Christopher Lambad327ba2007-12-29 05:02:41 +00001128LValue CodeGenFunction::EmitCallExprLValue(const CallExpr *E) {
1129 // Can only get l-value for call expression returning aggregate type
1130 RValue RV = EmitCallExpr(E);
Eli Friedman2e630542008-06-13 23:01:12 +00001131 return LValue::MakeAddr(RV.getAggregateAddr(),
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +00001132 E->getType().getCVRQualifiers(),
1133 getContext().getObjCGCAttrKind(E->getType()));
Christopher Lambad327ba2007-12-29 05:02:41 +00001134}
1135
Daniel Dunbar95d08f22009-02-11 20:59:32 +00001136LValue CodeGenFunction::EmitVAArgExprLValue(const VAArgExpr *E) {
1137 // FIXME: This shouldn't require another copy.
1138 llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType()));
1139 EmitAggExpr(E, Temp, false);
1140 return LValue::MakeAddr(Temp, E->getType().getCVRQualifiers());
1141}
1142
Argiris Kirtzidisbf615b02008-09-10 02:36:38 +00001143LValue
1144CodeGenFunction::EmitCXXConditionDeclLValue(const CXXConditionDeclExpr *E) {
1145 EmitLocalBlockVarDecl(*E->getVarDecl());
1146 return EmitDeclRefLValue(E);
1147}
1148
Daniel Dunbar5e105892008-08-23 10:51:21 +00001149LValue CodeGenFunction::EmitObjCMessageExprLValue(const ObjCMessageExpr *E) {
1150 // Can only get l-value for message expression returning aggregate type
1151 RValue RV = EmitObjCMessageExpr(E);
1152 // FIXME: can this be volatile?
1153 return LValue::MakeAddr(RV.getAggregateAddr(),
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +00001154 E->getType().getCVRQualifiers(),
1155 getContext().getObjCGCAttrKind(E->getType()));
Daniel Dunbar5e105892008-08-23 10:51:21 +00001156}
1157
Daniel Dunbar61e14a62009-04-22 05:08:15 +00001158llvm::Value *CodeGenFunction::EmitIvarOffset(const ObjCInterfaceDecl *Interface,
Daniel Dunbare856ac22008-09-24 04:00:38 +00001159 const ObjCIvarDecl *Ivar) {
Fariborz Jahanian27cc6662009-02-10 19:02:04 +00001160 return CGM.getObjCRuntime().EmitIvarOffset(*this, Interface, Ivar);
Daniel Dunbare856ac22008-09-24 04:00:38 +00001161}
1162
Fariborz Jahanian55343922009-02-03 00:09:52 +00001163LValue CodeGenFunction::EmitLValueForIvar(QualType ObjectTy,
1164 llvm::Value *BaseValue,
Daniel Dunbare856ac22008-09-24 04:00:38 +00001165 const ObjCIvarDecl *Ivar,
1166 unsigned CVRQualifiers) {
Chris Lattner552914b2009-04-17 17:44:48 +00001167 return CGM.getObjCRuntime().EmitObjCValueForIvar(*this, ObjectTy, BaseValue,
Daniel Dunbarf5254bd2009-04-21 01:19:28 +00001168 Ivar, CVRQualifiers);
Daniel Dunbare856ac22008-09-24 04:00:38 +00001169}
1170
1171LValue CodeGenFunction::EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E) {
Anders Carlssonae61b002008-08-25 01:53:23 +00001172 // FIXME: A lot of the code below could be shared with EmitMemberExpr.
1173 llvm::Value *BaseValue = 0;
1174 const Expr *BaseExpr = E->getBase();
1175 unsigned CVRQualifiers = 0;
Fariborz Jahanian55343922009-02-03 00:09:52 +00001176 QualType ObjectTy;
Anders Carlssonae61b002008-08-25 01:53:23 +00001177 if (E->isArrow()) {
1178 BaseValue = EmitScalarExpr(BaseExpr);
Daniel Dunbard4271f62009-04-18 08:54:40 +00001179 const PointerType *PTy = BaseExpr->getType()->getAsPointerType();
Fariborz Jahanian55343922009-02-03 00:09:52 +00001180 ObjectTy = PTy->getPointeeType();
1181 CVRQualifiers = ObjectTy.getCVRQualifiers();
Anders Carlssonae61b002008-08-25 01:53:23 +00001182 } else {
1183 LValue BaseLV = EmitLValue(BaseExpr);
1184 // FIXME: this isn't right for bitfields.
1185 BaseValue = BaseLV.getAddress();
Fariborz Jahanian55343922009-02-03 00:09:52 +00001186 ObjectTy = BaseExpr->getType();
1187 CVRQualifiers = ObjectTy.getCVRQualifiers();
Anders Carlssonae61b002008-08-25 01:53:23 +00001188 }
Daniel Dunbare856ac22008-09-24 04:00:38 +00001189
Daniel Dunbarf5254bd2009-04-21 01:19:28 +00001190 return EmitLValueForIvar(ObjectTy, BaseValue, E->getDecl(), CVRQualifiers);
Chris Lattnerb326b172008-03-30 23:03:07 +00001191}
1192
Daniel Dunbare6c31752008-08-29 08:11:39 +00001193LValue
1194CodeGenFunction::EmitObjCPropertyRefLValue(const ObjCPropertyRefExpr *E) {
1195 // This is a special l-value that just issues sends when we load or
1196 // store through it.
1197 return LValue::MakePropertyRef(E, E->getType().getCVRQualifiers());
1198}
1199
Fariborz Jahanianb0973da2008-11-22 22:30:21 +00001200LValue
1201CodeGenFunction::EmitObjCKVCRefLValue(const ObjCKVCRefExpr *E) {
1202 // This is a special l-value that just issues sends when we load or
1203 // store through it.
1204 return LValue::MakeKVCRef(E, E->getType().getCVRQualifiers());
1205}
1206
Douglas Gregord8606632008-11-04 14:56:14 +00001207LValue
Chris Lattnereec3a592009-04-25 19:35:26 +00001208CodeGenFunction::EmitObjCSuperExprLValue(const ObjCSuperExpr *E) {
Douglas Gregord8606632008-11-04 14:56:14 +00001209 return EmitUnsupportedLValue(E, "use of super");
1210}
1211
Chris Lattnereec3a592009-04-25 19:35:26 +00001212LValue CodeGenFunction::EmitStmtExprLValue(const StmtExpr *E) {
1213
1214 // Can only get l-value for message expression returning aggregate type
1215 RValue RV = EmitAnyExprToTemp(E);
1216 // FIXME: can this be volatile?
1217 return LValue::MakeAddr(RV.getAggregateAddr(),
1218 E->getType().getCVRQualifiers(),
1219 getContext().getObjCGCAttrKind(E->getType()));
1220}
1221
1222
Daniel Dunbare3a6a682009-01-09 16:50:52 +00001223RValue CodeGenFunction::EmitCallExpr(llvm::Value *Callee, QualType CalleeType,
Ted Kremenek2719e982008-06-17 02:43:46 +00001224 CallExpr::const_arg_iterator ArgBeg,
Daniel Dunbar191eb9e2009-02-20 18:06:48 +00001225 CallExpr::const_arg_iterator ArgEnd,
1226 const Decl *TargetDecl) {
Daniel Dunbare3a6a682009-01-09 16:50:52 +00001227 // Get the actual function type. The callee type will always be a
1228 // pointer to function type or a block pointer type.
Anders Carlsson7acb3a42009-04-07 18:53:02 +00001229 assert(CalleeType->isFunctionPointerType() &&
1230 "Call must have function pointer type!");
1231
1232 QualType FnType = CalleeType->getAsPointerType()->getPointeeType();
1233 QualType ResultType = FnType->getAsFunctionType()->getResultType();
Daniel Dunbar0ed60b02008-08-30 03:02:31 +00001234
1235 CallArgList Args;
Anders Carlsson6759c4d2009-04-08 23:13:16 +00001236 EmitCallArgs(Args, FnType->getAsFunctionProtoType(), ArgBeg, ArgEnd);
Daniel Dunbar0ed60b02008-08-30 03:02:31 +00001237
Daniel Dunbar34bda882009-02-02 23:23:47 +00001238 return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args),
Daniel Dunbar191eb9e2009-02-20 18:06:48 +00001239 Callee, Args, TargetDecl);
Daniel Dunbara04840b2008-08-23 03:46:30 +00001240}