Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- CGExpr.cpp - Emit LLVM Code from Expressions ---------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 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 Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 16 | #include "CGCall.h" |
Daniel Dunbar | 198bcb4 | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 17 | #include "CGRecordLayout.h" |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 18 | #include "CGObjCRuntime.h" |
Daniel Dunbar | de7fb84 | 2008-08-11 05:00:27 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclObjC.h" |
Mike Stump | 15037ca | 2009-12-15 00:35:12 +0000 | [diff] [blame] | 21 | #include "llvm/Intrinsics.h" |
Chandler Carruth | 06057ce | 2010-06-15 23:19:56 +0000 | [diff] [blame] | 22 | #include "clang/Frontend/CodeGenOptions.h" |
Eli Friedman | 316bb1b | 2008-05-17 20:03:47 +0000 | [diff] [blame] | 23 | #include "llvm/Target/TargetData.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 24 | using namespace clang; |
| 25 | using namespace CodeGen; |
| 26 | |
| 27 | //===--------------------------------------------------------------------===// |
| 28 | // Miscellaneous Helper Methods |
| 29 | //===--------------------------------------------------------------------===// |
| 30 | |
| 31 | /// CreateTempAlloca - This creates a alloca and inserts it into the entry |
| 32 | /// block. |
| 33 | llvm::AllocaInst *CodeGenFunction::CreateTempAlloca(const llvm::Type *Ty, |
Daniel Dunbar | 259e9cc | 2009-10-19 01:21:05 +0000 | [diff] [blame] | 34 | const llvm::Twine &Name) { |
Chris Lattner | f146684 | 2009-03-22 00:24:14 +0000 | [diff] [blame] | 35 | if (!Builder.isNamePreserving()) |
Daniel Dunbar | 259e9cc | 2009-10-19 01:21:05 +0000 | [diff] [blame] | 36 | return new llvm::AllocaInst(Ty, 0, "", AllocaInsertPt); |
Devang Patel | d35e2e0 | 2009-10-12 22:29:02 +0000 | [diff] [blame] | 37 | return new llvm::AllocaInst(Ty, 0, Name, AllocaInsertPt); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 38 | } |
| 39 | |
John McCall | ac41816 | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 40 | void CodeGenFunction::InitTempAlloca(llvm::AllocaInst *Var, |
| 41 | llvm::Value *Init) { |
| 42 | llvm::StoreInst *Store = new llvm::StoreInst(Init, Var); |
| 43 | llvm::BasicBlock *Block = AllocaInsertPt->getParent(); |
| 44 | Block->getInstList().insertAfter(&*AllocaInsertPt, Store); |
| 45 | } |
| 46 | |
Chris Lattner | 121b3fa | 2010-07-05 20:21:00 +0000 | [diff] [blame] | 47 | llvm::AllocaInst *CodeGenFunction::CreateIRTemp(QualType Ty, |
| 48 | const llvm::Twine &Name) { |
Daniel Dunbar | 9bd4da2 | 2010-02-16 19:44:13 +0000 | [diff] [blame] | 49 | llvm::AllocaInst *Alloc = CreateTempAlloca(ConvertType(Ty), Name); |
| 50 | // FIXME: Should we prefer the preferred type alignment here? |
| 51 | CharUnits Align = getContext().getTypeAlignInChars(Ty); |
| 52 | Alloc->setAlignment(Align.getQuantity()); |
| 53 | return Alloc; |
| 54 | } |
| 55 | |
Chris Lattner | 121b3fa | 2010-07-05 20:21:00 +0000 | [diff] [blame] | 56 | llvm::AllocaInst *CodeGenFunction::CreateMemTemp(QualType Ty, |
| 57 | const llvm::Twine &Name) { |
Daniel Dunbar | 195337d | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 58 | llvm::AllocaInst *Alloc = CreateTempAlloca(ConvertTypeForMem(Ty), Name); |
| 59 | // FIXME: Should we prefer the preferred type alignment here? |
| 60 | CharUnits Align = getContext().getTypeAlignInChars(Ty); |
| 61 | Alloc->setAlignment(Align.getQuantity()); |
| 62 | return Alloc; |
| 63 | } |
| 64 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 65 | /// EvaluateExprAsBool - Perform the usual unary conversions on the specified |
| 66 | /// expression and compare the result against zero, returning an Int1Ty value. |
| 67 | llvm::Value *CodeGenFunction::EvaluateExprAsBool(const Expr *E) { |
Chris Lattner | 9069fa2 | 2007-08-26 16:46:58 +0000 | [diff] [blame] | 68 | QualType BoolTy = getContext().BoolTy; |
Eli Friedman | 3a17370 | 2009-12-11 09:26:29 +0000 | [diff] [blame] | 69 | if (E->getType()->isMemberFunctionPointerType()) { |
Daniel Dunbar | 18aba0d | 2010-02-05 19:38:31 +0000 | [diff] [blame] | 70 | LValue LV = EmitAggExprToLValue(E); |
Eli Friedman | 3a17370 | 2009-12-11 09:26:29 +0000 | [diff] [blame] | 71 | |
| 72 | // Get the pointer. |
Daniel Dunbar | 18aba0d | 2010-02-05 19:38:31 +0000 | [diff] [blame] | 73 | llvm::Value *FuncPtr = Builder.CreateStructGEP(LV.getAddress(), 0, |
| 74 | "src.ptr"); |
Eli Friedman | 3a17370 | 2009-12-11 09:26:29 +0000 | [diff] [blame] | 75 | FuncPtr = Builder.CreateLoad(FuncPtr); |
| 76 | |
| 77 | llvm::Value *IsNotNull = |
| 78 | Builder.CreateICmpNE(FuncPtr, |
| 79 | llvm::Constant::getNullValue(FuncPtr->getType()), |
| 80 | "tobool"); |
| 81 | |
| 82 | return IsNotNull; |
| 83 | } |
Chris Lattner | 9b2dc28 | 2008-04-04 16:54:41 +0000 | [diff] [blame] | 84 | if (!E->getType()->isAnyComplexType()) |
Chris Lattner | 9069fa2 | 2007-08-26 16:46:58 +0000 | [diff] [blame] | 85 | return EmitScalarConversion(EmitScalarExpr(E), E->getType(), BoolTy); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 86 | |
Chris Lattner | 9069fa2 | 2007-08-26 16:46:58 +0000 | [diff] [blame] | 87 | return EmitComplexToScalarConversion(EmitComplexExpr(E), E->getType(),BoolTy); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 90 | /// EmitAnyExpr - Emit code to compute the specified expression which can have |
| 91 | /// any type. The result is returned as an RValue struct. If this is an |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 92 | /// aggregate expression, the aggloc/agglocvolatile arguments indicate where the |
| 93 | /// result should be returned. |
| 94 | RValue CodeGenFunction::EmitAnyExpr(const Expr *E, llvm::Value *AggLoc, |
Anders Carlsson | 14c5cbf | 2009-08-16 07:36:22 +0000 | [diff] [blame] | 95 | bool IsAggLocVolatile, bool IgnoreResult, |
| 96 | bool IsInitializer) { |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 97 | if (!hasAggregateLLVMType(E->getType())) |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 98 | return RValue::get(EmitScalarExpr(E, IgnoreResult)); |
Chris Lattner | 9b2dc28 | 2008-04-04 16:54:41 +0000 | [diff] [blame] | 99 | else if (E->getType()->isAnyComplexType()) |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 100 | return RValue::getComplex(EmitComplexExpr(E, false, false, |
| 101 | IgnoreResult, IgnoreResult)); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 102 | |
Anders Carlsson | 14c5cbf | 2009-08-16 07:36:22 +0000 | [diff] [blame] | 103 | EmitAggExpr(E, AggLoc, IsAggLocVolatile, IgnoreResult, IsInitializer); |
| 104 | return RValue::getAggregate(AggLoc, IsAggLocVolatile); |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 107 | /// EmitAnyExprToTemp - Similary to EmitAnyExpr(), however, the result will |
| 108 | /// always be accessible even if no aggregate location is provided. |
| 109 | RValue CodeGenFunction::EmitAnyExprToTemp(const Expr *E, |
Anders Carlsson | 14c5cbf | 2009-08-16 07:36:22 +0000 | [diff] [blame] | 110 | bool IsAggLocVolatile, |
| 111 | bool IsInitializer) { |
| 112 | llvm::Value *AggLoc = 0; |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 113 | |
| 114 | if (hasAggregateLLVMType(E->getType()) && |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 115 | !E->getType()->isAnyComplexType()) |
John McCall | 63efd33 | 2010-02-15 01:23:36 +0000 | [diff] [blame] | 116 | AggLoc = CreateMemTemp(E->getType(), "agg.tmp"); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 117 | return EmitAnyExpr(E, AggLoc, IsAggLocVolatile, /*IgnoreResult=*/false, |
Anders Carlsson | 14c5cbf | 2009-08-16 07:36:22 +0000 | [diff] [blame] | 118 | IsInitializer); |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 119 | } |
| 120 | |
John McCall | 3d3ec1c | 2010-04-21 10:05:39 +0000 | [diff] [blame] | 121 | /// EmitAnyExprToMem - Evaluate an expression into a given memory |
| 122 | /// location. |
| 123 | void CodeGenFunction::EmitAnyExprToMem(const Expr *E, |
| 124 | llvm::Value *Location, |
| 125 | bool IsLocationVolatile, |
| 126 | bool IsInit) { |
| 127 | if (E->getType()->isComplexType()) |
| 128 | EmitComplexExprIntoAddr(E, Location, IsLocationVolatile); |
| 129 | else if (hasAggregateLLVMType(E->getType())) |
| 130 | EmitAggExpr(E, Location, IsLocationVolatile, /*Ignore*/ false, IsInit); |
| 131 | else { |
| 132 | RValue RV = RValue::get(EmitScalarExpr(E, /*Ignore*/ false)); |
| 133 | LValue LV = LValue::MakeAddr(Location, MakeQualifiers(E->getType())); |
| 134 | EmitStoreThroughLValue(RV, LV, E->getType()); |
| 135 | } |
| 136 | } |
| 137 | |
Douglas Gregor | 60dcb84 | 2010-05-20 08:36:28 +0000 | [diff] [blame] | 138 | /// \brief An adjustment to be made to the temporary created when emitting a |
| 139 | /// reference binding, which accesses a particular subobject of that temporary. |
| 140 | struct SubobjectAdjustment { |
| 141 | enum { DerivedToBaseAdjustment, FieldAdjustment } Kind; |
| 142 | |
| 143 | union { |
| 144 | struct { |
| 145 | const CXXBaseSpecifierArray *BasePath; |
| 146 | const CXXRecordDecl *DerivedClass; |
| 147 | } DerivedToBase; |
| 148 | |
| 149 | struct { |
| 150 | FieldDecl *Field; |
| 151 | unsigned CVRQualifiers; |
| 152 | } Field; |
| 153 | }; |
| 154 | |
| 155 | SubobjectAdjustment(const CXXBaseSpecifierArray *BasePath, |
| 156 | const CXXRecordDecl *DerivedClass) |
| 157 | : Kind(DerivedToBaseAdjustment) |
| 158 | { |
| 159 | DerivedToBase.BasePath = BasePath; |
| 160 | DerivedToBase.DerivedClass = DerivedClass; |
| 161 | } |
| 162 | |
| 163 | SubobjectAdjustment(FieldDecl *Field, unsigned CVRQualifiers) |
| 164 | : Kind(FieldAdjustment) |
| 165 | { |
| 166 | this->Field.Field = Field; |
| 167 | this->Field.CVRQualifiers = CVRQualifiers; |
| 168 | } |
| 169 | }; |
| 170 | |
Anders Carlsson | dca7ab2 | 2010-06-27 16:56:04 +0000 | [diff] [blame] | 171 | static llvm::Value * |
Anders Carlsson | 656746c | 2010-06-27 17:23:46 +0000 | [diff] [blame] | 172 | CreateReferenceTemporary(CodeGenFunction& CGF, QualType Type, |
| 173 | const NamedDecl *InitializedDecl) { |
| 174 | if (const VarDecl *VD = dyn_cast_or_null<VarDecl>(InitializedDecl)) { |
| 175 | if (VD->hasGlobalStorage()) { |
| 176 | llvm::SmallString<256> Name; |
| 177 | CGF.CGM.getMangleContext().mangleReferenceTemporary(VD, Name); |
| 178 | |
| 179 | const llvm::Type *RefTempTy = CGF.ConvertTypeForMem(Type); |
| 180 | |
| 181 | // Create the reference temporary. |
| 182 | llvm::GlobalValue *RefTemp = |
| 183 | new llvm::GlobalVariable(CGF.CGM.getModule(), |
| 184 | RefTempTy, /*isConstant=*/false, |
| 185 | llvm::GlobalValue::InternalLinkage, |
| 186 | llvm::Constant::getNullValue(RefTempTy), |
| 187 | Name.str()); |
| 188 | return RefTemp; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | return CGF.CreateMemTemp(Type, "ref.tmp"); |
| 193 | } |
| 194 | |
| 195 | static llvm::Value * |
Anders Carlsson | dca7ab2 | 2010-06-27 16:56:04 +0000 | [diff] [blame] | 196 | EmitExprForReferenceBinding(CodeGenFunction& CGF, const Expr* E, |
| 197 | llvm::Value *&ReferenceTemporary, |
| 198 | const CXXDestructorDecl *&ReferenceTemporaryDtor, |
Anders Carlsson | 656746c | 2010-06-27 17:23:46 +0000 | [diff] [blame] | 199 | const NamedDecl *InitializedDecl) { |
Eli Friedman | 27a9b72 | 2009-12-19 00:20:10 +0000 | [diff] [blame] | 200 | if (const CXXDefaultArgExpr *DAE = dyn_cast<CXXDefaultArgExpr>(E)) |
| 201 | E = DAE->getExpr(); |
Anders Carlsson | b3f7442 | 2009-10-15 00:51:46 +0000 | [diff] [blame] | 202 | |
Anders Carlsson | dca7ab2 | 2010-06-27 16:56:04 +0000 | [diff] [blame] | 203 | if (const CXXExprWithTemporaries *TE = dyn_cast<CXXExprWithTemporaries>(E)) { |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 204 | CodeGenFunction::RunCleanupsScope Scope(CGF); |
Anders Carlsson | dca7ab2 | 2010-06-27 16:56:04 +0000 | [diff] [blame] | 205 | |
| 206 | return EmitExprForReferenceBinding(CGF, TE->getSubExpr(), |
| 207 | ReferenceTemporary, |
| 208 | ReferenceTemporaryDtor, |
Anders Carlsson | 656746c | 2010-06-27 17:23:46 +0000 | [diff] [blame] | 209 | InitializedDecl); |
Anders Carlsson | dca7ab2 | 2010-06-27 16:56:04 +0000 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | RValue RV; |
| 213 | if (E->isLvalue(CGF.getContext()) == Expr::LV_Valid) { |
| 214 | // Emit the expression as an lvalue. |
| 215 | LValue LV = CGF.EmitLValue(E); |
| 216 | |
| 217 | if (LV.isSimple()) |
| 218 | return LV.getAddress(); |
Anders Carlsson | 0dc7366 | 2010-02-04 17:32:58 +0000 | [diff] [blame] | 219 | |
Anders Carlsson | dca7ab2 | 2010-06-27 16:56:04 +0000 | [diff] [blame] | 220 | // We have to load the lvalue. |
| 221 | RV = CGF.EmitLoadOfLValue(LV, E->getType()); |
Eli Friedman | 5df0d42 | 2009-05-20 02:31:19 +0000 | [diff] [blame] | 222 | } else { |
Douglas Gregor | 60dcb84 | 2010-05-20 08:36:28 +0000 | [diff] [blame] | 223 | QualType ResultTy = E->getType(); |
Anders Carlsson | dca7ab2 | 2010-06-27 16:56:04 +0000 | [diff] [blame] | 224 | |
Douglas Gregor | 60dcb84 | 2010-05-20 08:36:28 +0000 | [diff] [blame] | 225 | llvm::SmallVector<SubobjectAdjustment, 2> Adjustments; |
Anders Carlsson | dca7ab2 | 2010-06-27 16:56:04 +0000 | [diff] [blame] | 226 | while (true) { |
Douglas Gregor | 2d6b0e9 | 2010-05-22 05:17:18 +0000 | [diff] [blame] | 227 | if (const ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
| 228 | E = PE->getSubExpr(); |
| 229 | continue; |
| 230 | } |
| 231 | |
| 232 | if (const CastExpr *CE = dyn_cast<CastExpr>(E)) { |
| 233 | if ((CE->getCastKind() == CastExpr::CK_DerivedToBase || |
| 234 | CE->getCastKind() == CastExpr::CK_UncheckedDerivedToBase) && |
| 235 | E->getType()->isRecordType()) { |
Douglas Gregor | 60dcb84 | 2010-05-20 08:36:28 +0000 | [diff] [blame] | 236 | E = CE->getSubExpr(); |
| 237 | CXXRecordDecl *Derived |
| 238 | = cast<CXXRecordDecl>(E->getType()->getAs<RecordType>()->getDecl()); |
| 239 | Adjustments.push_back(SubobjectAdjustment(&CE->getBasePath(), |
| 240 | Derived)); |
| 241 | continue; |
| 242 | } |
Douglas Gregor | 2d6b0e9 | 2010-05-22 05:17:18 +0000 | [diff] [blame] | 243 | |
| 244 | if (CE->getCastKind() == CastExpr::CK_NoOp) { |
| 245 | E = CE->getSubExpr(); |
| 246 | continue; |
| 247 | } |
| 248 | } else if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) { |
Anders Carlsson | dca7ab2 | 2010-06-27 16:56:04 +0000 | [diff] [blame] | 249 | if (ME->getBase()->isLvalue(CGF.getContext()) != Expr::LV_Valid && |
Douglas Gregor | 60dcb84 | 2010-05-20 08:36:28 +0000 | [diff] [blame] | 250 | ME->getBase()->getType()->isRecordType()) { |
| 251 | if (FieldDecl *Field = dyn_cast<FieldDecl>(ME->getMemberDecl())) { |
| 252 | E = ME->getBase(); |
| 253 | Adjustments.push_back(SubobjectAdjustment(Field, |
| 254 | E->getType().getCVRQualifiers())); |
| 255 | continue; |
| 256 | } |
| 257 | } |
Anders Carlsson | b3f7442 | 2009-10-15 00:51:46 +0000 | [diff] [blame] | 258 | } |
Douglas Gregor | 2d6b0e9 | 2010-05-22 05:17:18 +0000 | [diff] [blame] | 259 | |
| 260 | // Nothing changed. |
| 261 | break; |
Anders Carlsson | 8478ce6 | 2009-08-16 17:50:25 +0000 | [diff] [blame] | 262 | } |
Anders Carlsson | b3f7442 | 2009-10-15 00:51:46 +0000 | [diff] [blame] | 263 | |
Anders Carlsson | dca7ab2 | 2010-06-27 16:56:04 +0000 | [diff] [blame] | 264 | // Create a reference temporary if necessary. |
| 265 | if (CGF.hasAggregateLLVMType(E->getType()) && |
| 266 | !E->getType()->isAnyComplexType()) |
Anders Carlsson | 656746c | 2010-06-27 17:23:46 +0000 | [diff] [blame] | 267 | ReferenceTemporary = CreateReferenceTemporary(CGF, E->getType(), |
| 268 | InitializedDecl); |
| 269 | |
Anders Carlsson | dca7ab2 | 2010-06-27 16:56:04 +0000 | [diff] [blame] | 270 | RV = CGF.EmitAnyExpr(E, ReferenceTemporary, /*IsAggLocVolatile=*/false, |
Anders Carlsson | 656746c | 2010-06-27 17:23:46 +0000 | [diff] [blame] | 271 | /*IgnoreResult=*/false, InitializedDecl); |
Anders Carlsson | dca7ab2 | 2010-06-27 16:56:04 +0000 | [diff] [blame] | 272 | |
Anders Carlsson | 656746c | 2010-06-27 17:23:46 +0000 | [diff] [blame] | 273 | if (InitializedDecl) { |
Anders Carlsson | dca7ab2 | 2010-06-27 16:56:04 +0000 | [diff] [blame] | 274 | // Get the destructor for the reference temporary. |
| 275 | if (const RecordType *RT = E->getType()->getAs<RecordType>()) { |
| 276 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
| 277 | if (!ClassDecl->hasTrivialDestructor()) |
Douglas Gregor | 1d110e0 | 2010-07-01 14:13:13 +0000 | [diff] [blame] | 278 | ReferenceTemporaryDtor = ClassDecl->getDestructor(); |
Anders Carlsson | dca7ab2 | 2010-06-27 16:56:04 +0000 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | |
Douglas Gregor | 60dcb84 | 2010-05-20 08:36:28 +0000 | [diff] [blame] | 282 | // Check if need to perform derived-to-base casts and/or field accesses, to |
| 283 | // get from the temporary object we created (and, potentially, for which we |
| 284 | // extended the lifetime) to the subobject we're binding the reference to. |
| 285 | if (!Adjustments.empty()) { |
Anders Carlsson | dca7ab2 | 2010-06-27 16:56:04 +0000 | [diff] [blame] | 286 | llvm::Value *Object = RV.getAggregateAddr(); |
Douglas Gregor | 60dcb84 | 2010-05-20 08:36:28 +0000 | [diff] [blame] | 287 | for (unsigned I = Adjustments.size(); I != 0; --I) { |
| 288 | SubobjectAdjustment &Adjustment = Adjustments[I-1]; |
| 289 | switch (Adjustment.Kind) { |
| 290 | case SubobjectAdjustment::DerivedToBaseAdjustment: |
Anders Carlsson | dca7ab2 | 2010-06-27 16:56:04 +0000 | [diff] [blame] | 291 | Object = |
| 292 | CGF.GetAddressOfBaseClass(Object, |
| 293 | Adjustment.DerivedToBase.DerivedClass, |
| 294 | *Adjustment.DerivedToBase.BasePath, |
| 295 | /*NullCheckValue=*/false); |
Douglas Gregor | 60dcb84 | 2010-05-20 08:36:28 +0000 | [diff] [blame] | 296 | break; |
| 297 | |
| 298 | case SubobjectAdjustment::FieldAdjustment: { |
| 299 | unsigned CVR = Adjustment.Field.CVRQualifiers; |
Anders Carlsson | dca7ab2 | 2010-06-27 16:56:04 +0000 | [diff] [blame] | 300 | LValue LV = |
| 301 | CGF.EmitLValueForField(Object, Adjustment.Field.Field, CVR); |
Douglas Gregor | 60dcb84 | 2010-05-20 08:36:28 +0000 | [diff] [blame] | 302 | if (LV.isSimple()) { |
| 303 | Object = LV.getAddress(); |
| 304 | break; |
| 305 | } |
| 306 | |
| 307 | // For non-simple lvalues, we actually have to create a copy of |
| 308 | // the object we're binding to. |
| 309 | QualType T = Adjustment.Field.Field->getType().getNonReferenceType() |
| 310 | .getUnqualifiedType(); |
Anders Carlsson | 045a6d8 | 2010-06-27 17:52:15 +0000 | [diff] [blame] | 311 | Object = CreateReferenceTemporary(CGF, T, InitializedDecl); |
| 312 | LValue TempLV = LValue::MakeAddr(Object, |
| 313 | Qualifiers::fromCVRMask(CVR)); |
Anders Carlsson | dca7ab2 | 2010-06-27 16:56:04 +0000 | [diff] [blame] | 314 | CGF.EmitStoreThroughLValue(CGF.EmitLoadOfLValue(LV, T), TempLV, T); |
Douglas Gregor | 60dcb84 | 2010-05-20 08:36:28 +0000 | [diff] [blame] | 315 | break; |
| 316 | } |
Anders Carlsson | dca7ab2 | 2010-06-27 16:56:04 +0000 | [diff] [blame] | 317 | |
Douglas Gregor | 60dcb84 | 2010-05-20 08:36:28 +0000 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | |
Anders Carlsson | dca7ab2 | 2010-06-27 16:56:04 +0000 | [diff] [blame] | 321 | const llvm::Type *ResultPtrTy = CGF.ConvertType(ResultTy)->getPointerTo(); |
| 322 | return CGF.Builder.CreateBitCast(Object, ResultPtrTy, "temp"); |
Anders Carlsson | b3f7442 | 2009-10-15 00:51:46 +0000 | [diff] [blame] | 323 | } |
Anders Carlsson | 4bbab92 | 2009-05-20 00:36:58 +0000 | [diff] [blame] | 324 | } |
Eli Friedman | 5df0d42 | 2009-05-20 02:31:19 +0000 | [diff] [blame] | 325 | |
Anders Carlsson | dca7ab2 | 2010-06-27 16:56:04 +0000 | [diff] [blame] | 326 | if (RV.isAggregate()) |
| 327 | return RV.getAggregateAddr(); |
Eli Friedman | 5df0d42 | 2009-05-20 02:31:19 +0000 | [diff] [blame] | 328 | |
Anders Carlsson | dca7ab2 | 2010-06-27 16:56:04 +0000 | [diff] [blame] | 329 | // Create a temporary variable that we can bind the reference to. |
Anders Carlsson | 656746c | 2010-06-27 17:23:46 +0000 | [diff] [blame] | 330 | ReferenceTemporary = CreateReferenceTemporary(CGF, E->getType(), |
| 331 | InitializedDecl); |
| 332 | |
Anders Carlsson | dca7ab2 | 2010-06-27 16:56:04 +0000 | [diff] [blame] | 333 | if (RV.isScalar()) |
| 334 | CGF.EmitStoreOfScalar(RV.getScalarVal(), ReferenceTemporary, |
| 335 | /*Volatile=*/false, E->getType()); |
| 336 | else |
| 337 | CGF.StoreComplexToAddr(RV.getComplexVal(), ReferenceTemporary, |
| 338 | /*Volatile=*/false); |
| 339 | return ReferenceTemporary; |
| 340 | } |
| 341 | |
| 342 | RValue |
| 343 | CodeGenFunction::EmitReferenceBindingToExpr(const Expr* E, |
| 344 | const NamedDecl *InitializedDecl) { |
| 345 | llvm::Value *ReferenceTemporary = 0; |
| 346 | const CXXDestructorDecl *ReferenceTemporaryDtor = 0; |
| 347 | llvm::Value *Value = EmitExprForReferenceBinding(*this, E, ReferenceTemporary, |
| 348 | ReferenceTemporaryDtor, |
| 349 | InitializedDecl); |
| 350 | |
Anders Carlsson | 045a6d8 | 2010-06-27 17:52:15 +0000 | [diff] [blame] | 351 | if (!ReferenceTemporaryDtor) |
| 352 | return RValue::get(Value); |
| 353 | |
Anders Carlsson | dca7ab2 | 2010-06-27 16:56:04 +0000 | [diff] [blame] | 354 | // Make sure to call the destructor for the reference temporary. |
Anders Carlsson | 045a6d8 | 2010-06-27 17:52:15 +0000 | [diff] [blame] | 355 | if (const VarDecl *VD = dyn_cast_or_null<VarDecl>(InitializedDecl)) { |
| 356 | if (VD->hasGlobalStorage()) { |
| 357 | llvm::Constant *DtorFn = |
| 358 | CGM.GetAddrOfCXXDestructor(ReferenceTemporaryDtor, Dtor_Complete); |
| 359 | CGF.EmitCXXGlobalDtorRegistration(DtorFn, |
| 360 | cast<llvm::Constant>(ReferenceTemporary)); |
| 361 | |
| 362 | return RValue::get(Value); |
| 363 | } |
| 364 | } |
John McCall | 81407d4 | 2010-07-21 06:29:51 +0000 | [diff] [blame^] | 365 | |
| 366 | PushDestructorCleanup(ReferenceTemporaryDtor, ReferenceTemporary); |
Anders Carlsson | dca7ab2 | 2010-06-27 16:56:04 +0000 | [diff] [blame] | 367 | |
| 368 | return RValue::get(Value); |
Anders Carlsson | 4029ca7 | 2009-05-20 00:24:07 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 372 | /// getAccessedFieldNo - Given an encoded value and a result number, return the |
| 373 | /// input field number being accessed. |
| 374 | unsigned CodeGenFunction::getAccessedFieldNo(unsigned Idx, |
Dan Gohman | 4f8d123 | 2008-05-22 00:50:06 +0000 | [diff] [blame] | 375 | const llvm::Constant *Elts) { |
| 376 | if (isa<llvm::ConstantAggregateZero>(Elts)) |
| 377 | return 0; |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 378 | |
Dan Gohman | 4f8d123 | 2008-05-22 00:50:06 +0000 | [diff] [blame] | 379 | return cast<llvm::ConstantInt>(Elts->getOperand(Idx))->getZExtValue(); |
| 380 | } |
| 381 | |
Mike Stump | b14e62d | 2009-12-16 02:57:00 +0000 | [diff] [blame] | 382 | void CodeGenFunction::EmitCheck(llvm::Value *Address, unsigned Size) { |
| 383 | if (!CatchUndefined) |
| 384 | return; |
| 385 | |
Mike Stump | b14e62d | 2009-12-16 02:57:00 +0000 | [diff] [blame] | 386 | Address = Builder.CreateBitCast(Address, PtrToInt8Ty); |
| 387 | |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 388 | const llvm::Type *IntPtrT = IntPtrTy; |
| 389 | llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::objectsize, &IntPtrT, 1); |
| 390 | const llvm::IntegerType *Int1Ty = llvm::Type::getInt1Ty(VMContext); |
Chris Lattner | c24b9c4 | 2010-04-10 18:34:14 +0000 | [diff] [blame] | 391 | |
Mike Stump | b14e62d | 2009-12-16 02:57:00 +0000 | [diff] [blame] | 392 | // In time, people may want to control this and use a 1 here. |
Chris Lattner | c24b9c4 | 2010-04-10 18:34:14 +0000 | [diff] [blame] | 393 | llvm::Value *Arg = llvm::ConstantInt::get(Int1Ty, 0); |
Mike Stump | b14e62d | 2009-12-16 02:57:00 +0000 | [diff] [blame] | 394 | llvm::Value *C = Builder.CreateCall2(F, Address, Arg); |
| 395 | llvm::BasicBlock *Cont = createBasicBlock(); |
| 396 | llvm::BasicBlock *Check = createBasicBlock(); |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 397 | llvm::Value *NegativeOne = llvm::ConstantInt::get(IntPtrTy, -1ULL); |
Mike Stump | b14e62d | 2009-12-16 02:57:00 +0000 | [diff] [blame] | 398 | Builder.CreateCondBr(Builder.CreateICmpEQ(C, NegativeOne), Cont, Check); |
| 399 | |
| 400 | EmitBlock(Check); |
| 401 | Builder.CreateCondBr(Builder.CreateICmpUGE(C, |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 402 | llvm::ConstantInt::get(IntPtrTy, Size)), |
Mike Stump | b14e62d | 2009-12-16 02:57:00 +0000 | [diff] [blame] | 403 | Cont, getTrapBB()); |
| 404 | EmitBlock(Cont); |
| 405 | } |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 406 | |
Chris Lattner | dd36d32 | 2010-01-09 21:40:03 +0000 | [diff] [blame] | 407 | |
Chris Lattner | dd36d32 | 2010-01-09 21:40:03 +0000 | [diff] [blame] | 408 | CodeGenFunction::ComplexPairTy CodeGenFunction:: |
| 409 | EmitComplexPrePostIncDec(const UnaryOperator *E, LValue LV, |
| 410 | bool isInc, bool isPre) { |
| 411 | ComplexPairTy InVal = LoadComplexFromAddr(LV.getAddress(), |
| 412 | LV.isVolatileQualified()); |
| 413 | |
| 414 | llvm::Value *NextVal; |
| 415 | if (isa<llvm::IntegerType>(InVal.first->getType())) { |
| 416 | uint64_t AmountVal = isInc ? 1 : -1; |
| 417 | NextVal = llvm::ConstantInt::get(InVal.first->getType(), AmountVal, true); |
| 418 | |
| 419 | // Add the inc/dec to the real part. |
| 420 | NextVal = Builder.CreateAdd(InVal.first, NextVal, isInc ? "inc" : "dec"); |
| 421 | } else { |
| 422 | QualType ElemTy = E->getType()->getAs<ComplexType>()->getElementType(); |
| 423 | llvm::APFloat FVal(getContext().getFloatTypeSemantics(ElemTy), 1); |
| 424 | if (!isInc) |
| 425 | FVal.changeSign(); |
| 426 | NextVal = llvm::ConstantFP::get(getLLVMContext(), FVal); |
| 427 | |
| 428 | // Add the inc/dec to the real part. |
| 429 | NextVal = Builder.CreateFAdd(InVal.first, NextVal, isInc ? "inc" : "dec"); |
| 430 | } |
| 431 | |
| 432 | ComplexPairTy IncVal(NextVal, InVal.second); |
| 433 | |
| 434 | // Store the updated result through the lvalue. |
| 435 | StoreComplexToAddr(IncVal, LV.getAddress(), LV.isVolatileQualified()); |
| 436 | |
| 437 | // If this is a postinc, return the value read from memory, otherwise use the |
| 438 | // updated value. |
| 439 | return isPre ? IncVal : InVal; |
| 440 | } |
| 441 | |
| 442 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 443 | //===----------------------------------------------------------------------===// |
| 444 | // LValue Expression Emission |
| 445 | //===----------------------------------------------------------------------===// |
| 446 | |
Daniel Dunbar | 13e8173 | 2009-02-05 07:09:07 +0000 | [diff] [blame] | 447 | RValue CodeGenFunction::GetUndefRValue(QualType Ty) { |
Chris Lattner | eb99b01 | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 448 | if (Ty->isVoidType()) |
Daniel Dunbar | 13e8173 | 2009-02-05 07:09:07 +0000 | [diff] [blame] | 449 | return RValue::get(0); |
Chris Lattner | eb99b01 | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 450 | |
| 451 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { |
Daniel Dunbar | 8fa73ed | 2009-01-09 20:09:28 +0000 | [diff] [blame] | 452 | const llvm::Type *EltTy = ConvertType(CTy->getElementType()); |
Owen Anderson | 03e2050 | 2009-07-30 23:11:26 +0000 | [diff] [blame] | 453 | llvm::Value *U = llvm::UndefValue::get(EltTy); |
Daniel Dunbar | 8fa73ed | 2009-01-09 20:09:28 +0000 | [diff] [blame] | 454 | return RValue::getComplex(std::make_pair(U, U)); |
Chris Lattner | eb99b01 | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | if (hasAggregateLLVMType(Ty)) { |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 458 | const llvm::Type *LTy = llvm::PointerType::getUnqual(ConvertType(Ty)); |
Owen Anderson | 03e2050 | 2009-07-30 23:11:26 +0000 | [diff] [blame] | 459 | return RValue::getAggregate(llvm::UndefValue::get(LTy)); |
Daniel Dunbar | 8fa73ed | 2009-01-09 20:09:28 +0000 | [diff] [blame] | 460 | } |
Chris Lattner | eb99b01 | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 461 | |
| 462 | return RValue::get(llvm::UndefValue::get(ConvertType(Ty))); |
Daniel Dunbar | ce1d38b | 2009-01-09 16:50:52 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Daniel Dunbar | 13e8173 | 2009-02-05 07:09:07 +0000 | [diff] [blame] | 465 | RValue CodeGenFunction::EmitUnsupportedRValue(const Expr *E, |
| 466 | const char *Name) { |
| 467 | ErrorUnsupported(E, Name); |
| 468 | return GetUndefRValue(E->getType()); |
| 469 | } |
| 470 | |
Daniel Dunbar | 6ba82a4 | 2008-08-25 20:45:57 +0000 | [diff] [blame] | 471 | LValue CodeGenFunction::EmitUnsupportedLValue(const Expr *E, |
| 472 | const char *Name) { |
| 473 | ErrorUnsupported(E, Name); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 474 | llvm::Type *Ty = llvm::PointerType::getUnqual(ConvertType(E->getType())); |
Owen Anderson | 03e2050 | 2009-07-30 23:11:26 +0000 | [diff] [blame] | 475 | return LValue::MakeAddr(llvm::UndefValue::get(Ty), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 476 | MakeQualifiers(E->getType())); |
Daniel Dunbar | 6ba82a4 | 2008-08-25 20:45:57 +0000 | [diff] [blame] | 477 | } |
| 478 | |
Mike Stump | b14e62d | 2009-12-16 02:57:00 +0000 | [diff] [blame] | 479 | LValue CodeGenFunction::EmitCheckedLValue(const Expr *E) { |
| 480 | LValue LV = EmitLValue(E); |
Daniel Dunbar | f0fe5bc | 2010-04-05 21:36:35 +0000 | [diff] [blame] | 481 | if (!isa<DeclRefExpr>(E) && !LV.isBitField() && LV.isSimple()) |
Mike Stump | b14e62d | 2009-12-16 02:57:00 +0000 | [diff] [blame] | 482 | EmitCheck(LV.getAddress(), getContext().getTypeSize(E->getType()) / 8); |
| 483 | return LV; |
| 484 | } |
| 485 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 486 | /// EmitLValue - Emit code to compute a designator that specifies the location |
| 487 | /// of the expression. |
| 488 | /// |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 489 | /// This can return one of two things: a simple address or a bitfield reference. |
| 490 | /// In either case, the LLVM Value* in the LValue structure is guaranteed to be |
| 491 | /// an LLVM pointer type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 492 | /// |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 493 | /// If this returns a bitfield reference, nothing about the pointee type of the |
| 494 | /// LLVM value is known: For example, it may not be a pointer to an integer. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 495 | /// |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 496 | /// If this returns a normal address, and if the lvalue's C type is fixed size, |
| 497 | /// this method guarantees that the returned pointer type will point to an LLVM |
| 498 | /// type of the same size of the lvalue's type. If the lvalue has a variable |
| 499 | /// length type, this is not possible. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 500 | /// |
| 501 | LValue CodeGenFunction::EmitLValue(const Expr *E) { |
| 502 | switch (E->getStmtClass()) { |
Daniel Dunbar | 6ba82a4 | 2008-08-25 20:45:57 +0000 | [diff] [blame] | 503 | default: return EmitUnsupportedLValue(E, "l-value expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 504 | |
Fariborz Jahanian | 03b2960 | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 505 | case Expr::ObjCSelectorExprClass: |
| 506 | return EmitObjCSelectorLValue(cast<ObjCSelectorExpr>(E)); |
Fariborz Jahanian | 820bca4 | 2009-12-09 23:35:29 +0000 | [diff] [blame] | 507 | case Expr::ObjCIsaExprClass: |
| 508 | return EmitObjCIsaExpr(cast<ObjCIsaExpr>(E)); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 509 | case Expr::BinaryOperatorClass: |
Daniel Dunbar | 80e62c2 | 2008-09-04 03:20:13 +0000 | [diff] [blame] | 510 | return EmitBinaryOperatorLValue(cast<BinaryOperator>(E)); |
Douglas Gregor | 6a03e34 | 2010-04-23 04:16:32 +0000 | [diff] [blame] | 511 | case Expr::CompoundAssignOperatorClass: |
| 512 | return EmitCompoundAssignOperatorLValue(cast<CompoundAssignOperator>(E)); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 513 | case Expr::CallExprClass: |
Anders Carlsson | faf8664 | 2009-09-01 21:18:52 +0000 | [diff] [blame] | 514 | case Expr::CXXMemberCallExprClass: |
Douglas Gregor | b460980 | 2008-11-14 16:09:21 +0000 | [diff] [blame] | 515 | case Expr::CXXOperatorCallExprClass: |
| 516 | return EmitCallExprLValue(cast<CallExpr>(E)); |
Daniel Dunbar | 5b5c9ef | 2009-02-11 20:59:32 +0000 | [diff] [blame] | 517 | case Expr::VAArgExprClass: |
| 518 | return EmitVAArgExprLValue(cast<VAArgExpr>(E)); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 519 | case Expr::DeclRefExprClass: |
Douglas Gregor | 1a49af9 | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 520 | return EmitDeclRefLValue(cast<DeclRefExpr>(E)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 521 | case Expr::ParenExprClass:return EmitLValue(cast<ParenExpr>(E)->getSubExpr()); |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 522 | case Expr::PredefinedExprClass: |
| 523 | return EmitPredefinedLValue(cast<PredefinedExpr>(E)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 524 | case Expr::StringLiteralClass: |
| 525 | return EmitStringLiteralLValue(cast<StringLiteral>(E)); |
Chris Lattner | eaf2bb8 | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 526 | case Expr::ObjCEncodeExprClass: |
| 527 | return EmitObjCEncodeExprLValue(cast<ObjCEncodeExpr>(E)); |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 528 | |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 529 | case Expr::BlockDeclRefExprClass: |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 530 | return EmitBlockDeclRefLValue(cast<BlockDeclRefExpr>(E)); |
| 531 | |
Anders Carlsson | b58d017 | 2009-05-30 23:23:33 +0000 | [diff] [blame] | 532 | case Expr::CXXTemporaryObjectExprClass: |
| 533 | case Expr::CXXConstructExprClass: |
Anders Carlsson | e61c9e8 | 2009-05-30 23:30:54 +0000 | [diff] [blame] | 534 | return EmitCXXConstructLValue(cast<CXXConstructExpr>(E)); |
| 535 | case Expr::CXXBindTemporaryExprClass: |
| 536 | return EmitCXXBindTemporaryLValue(cast<CXXBindTemporaryExpr>(E)); |
Anders Carlsson | b9ea0b5 | 2009-09-14 01:10:45 +0000 | [diff] [blame] | 537 | case Expr::CXXExprWithTemporariesClass: |
| 538 | return EmitCXXExprWithTemporariesLValue(cast<CXXExprWithTemporaries>(E)); |
Douglas Gregor | ed8abf1 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 539 | case Expr::CXXScalarValueInitExprClass: |
| 540 | return EmitNullInitializationLValue(cast<CXXScalarValueInitExpr>(E)); |
Anders Carlsson | 370e538 | 2009-11-14 01:51:50 +0000 | [diff] [blame] | 541 | case Expr::CXXDefaultArgExprClass: |
| 542 | return EmitLValue(cast<CXXDefaultArgExpr>(E)->getExpr()); |
Mike Stump | c2e84ae | 2009-11-15 08:09:41 +0000 | [diff] [blame] | 543 | case Expr::CXXTypeidExprClass: |
| 544 | return EmitCXXTypeidLValue(cast<CXXTypeidExpr>(E)); |
Anders Carlsson | e61c9e8 | 2009-05-30 23:30:54 +0000 | [diff] [blame] | 545 | |
Daniel Dunbar | 0a04d77 | 2008-08-23 10:51:21 +0000 | [diff] [blame] | 546 | case Expr::ObjCMessageExprClass: |
| 547 | return EmitObjCMessageExprLValue(cast<ObjCMessageExpr>(E)); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 548 | case Expr::ObjCIvarRefExprClass: |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 549 | return EmitObjCIvarRefLValue(cast<ObjCIvarRefExpr>(E)); |
Daniel Dunbar | 6ba82a4 | 2008-08-25 20:45:57 +0000 | [diff] [blame] | 550 | case Expr::ObjCPropertyRefExprClass: |
Daniel Dunbar | 85c59ed | 2008-08-29 08:11:39 +0000 | [diff] [blame] | 551 | return EmitObjCPropertyRefLValue(cast<ObjCPropertyRefExpr>(E)); |
Fariborz Jahanian | 09105f5 | 2009-08-20 17:02:02 +0000 | [diff] [blame] | 552 | case Expr::ObjCImplicitSetterGetterRefExprClass: |
| 553 | return EmitObjCKVCRefLValue(cast<ObjCImplicitSetterGetterRefExpr>(E)); |
Douglas Gregor | cd9b46e | 2008-11-04 14:56:14 +0000 | [diff] [blame] | 554 | case Expr::ObjCSuperExprClass: |
Chris Lattner | 6545994 | 2009-04-25 19:35:26 +0000 | [diff] [blame] | 555 | return EmitObjCSuperExprLValue(cast<ObjCSuperExpr>(E)); |
Douglas Gregor | cd9b46e | 2008-11-04 14:56:14 +0000 | [diff] [blame] | 556 | |
Chris Lattner | 6545994 | 2009-04-25 19:35:26 +0000 | [diff] [blame] | 557 | case Expr::StmtExprClass: |
| 558 | return EmitStmtExprLValue(cast<StmtExpr>(E)); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 559 | case Expr::UnaryOperatorClass: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 560 | return EmitUnaryOpLValue(cast<UnaryOperator>(E)); |
| 561 | case Expr::ArraySubscriptExprClass: |
| 562 | return EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E)); |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 563 | case Expr::ExtVectorElementExprClass: |
| 564 | return EmitExtVectorElementExpr(cast<ExtVectorElementExpr>(E)); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 565 | case Expr::MemberExprClass: |
Douglas Gregor | bd4c4ae | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 566 | return EmitMemberExpr(cast<MemberExpr>(E)); |
Eli Friedman | 06e863f | 2008-05-13 23:18:27 +0000 | [diff] [blame] | 567 | case Expr::CompoundLiteralExprClass: |
| 568 | return EmitCompoundLiteralLValue(cast<CompoundLiteralExpr>(E)); |
Daniel Dunbar | 9034558 | 2009-03-24 02:38:23 +0000 | [diff] [blame] | 569 | case Expr::ConditionalOperatorClass: |
Anders Carlsson | 6fcec8b | 2009-09-15 16:35:24 +0000 | [diff] [blame] | 570 | return EmitConditionalOperatorLValue(cast<ConditionalOperator>(E)); |
Chris Lattner | 670a62c | 2008-12-12 05:35:08 +0000 | [diff] [blame] | 571 | case Expr::ChooseExprClass: |
Eli Friedman | 7976932 | 2009-03-04 05:52:32 +0000 | [diff] [blame] | 572 | return EmitLValue(cast<ChooseExpr>(E)->getChosenSubExpr(getContext())); |
Chris Lattner | c3953a6 | 2009-03-18 04:02:57 +0000 | [diff] [blame] | 573 | case Expr::ImplicitCastExprClass: |
| 574 | case Expr::CStyleCastExprClass: |
| 575 | case Expr::CXXFunctionalCastExprClass: |
| 576 | case Expr::CXXStaticCastExprClass: |
| 577 | case Expr::CXXDynamicCastExprClass: |
| 578 | case Expr::CXXReinterpretCastExprClass: |
| 579 | case Expr::CXXConstCastExprClass: |
Chris Lattner | 75dfeda | 2009-03-18 18:28:57 +0000 | [diff] [blame] | 580 | return EmitCastLValue(cast<CastExpr>(E)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 581 | } |
| 582 | } |
| 583 | |
Daniel Dunbar | 9d9cc87 | 2009-02-10 00:57:50 +0000 | [diff] [blame] | 584 | llvm::Value *CodeGenFunction::EmitLoadOfScalar(llvm::Value *Addr, bool Volatile, |
| 585 | QualType Ty) { |
Daniel Dunbar | 2da84ff | 2009-11-29 21:23:36 +0000 | [diff] [blame] | 586 | llvm::LoadInst *Load = Builder.CreateLoad(Addr, "tmp"); |
| 587 | if (Volatile) |
| 588 | Load->setVolatile(true); |
Daniel Dunbar | 9d9cc87 | 2009-02-10 00:57:50 +0000 | [diff] [blame] | 589 | |
Anders Carlsson | 3bb423b | 2009-05-19 19:36:19 +0000 | [diff] [blame] | 590 | // Bool can have different representation in memory than in registers. |
Daniel Dunbar | 2da84ff | 2009-11-29 21:23:36 +0000 | [diff] [blame] | 591 | llvm::Value *V = Load; |
Daniel Dunbar | 9d9cc87 | 2009-02-10 00:57:50 +0000 | [diff] [blame] | 592 | if (Ty->isBooleanType()) |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 593 | if (V->getType() != llvm::Type::getInt1Ty(VMContext)) |
| 594 | V = Builder.CreateTrunc(V, llvm::Type::getInt1Ty(VMContext), "tobool"); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 595 | |
Daniel Dunbar | 9d9cc87 | 2009-02-10 00:57:50 +0000 | [diff] [blame] | 596 | return V; |
| 597 | } |
| 598 | |
| 599 | void CodeGenFunction::EmitStoreOfScalar(llvm::Value *Value, llvm::Value *Addr, |
Anders Carlsson | b4aa466 | 2009-05-19 18:50:41 +0000 | [diff] [blame] | 600 | bool Volatile, QualType Ty) { |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 601 | |
Anders Carlsson | 3bb423b | 2009-05-19 19:36:19 +0000 | [diff] [blame] | 602 | if (Ty->isBooleanType()) { |
| 603 | // Bool can have different representation in memory than in registers. |
Anders Carlsson | 3bb423b | 2009-05-19 19:36:19 +0000 | [diff] [blame] | 604 | const llvm::PointerType *DstPtr = cast<llvm::PointerType>(Addr->getType()); |
Eli Friedman | 2b06d34 | 2009-12-01 22:31:51 +0000 | [diff] [blame] | 605 | Value = Builder.CreateIntCast(Value, DstPtr->getElementType(), false); |
Daniel Dunbar | 9d9cc87 | 2009-02-10 00:57:50 +0000 | [diff] [blame] | 606 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 607 | Builder.CreateStore(Value, Addr, Volatile); |
Daniel Dunbar | 9d9cc87 | 2009-02-10 00:57:50 +0000 | [diff] [blame] | 608 | } |
| 609 | |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 610 | /// EmitLoadOfLValue - Given an expression that represents a value lvalue, this |
| 611 | /// method emits the address of the lvalue, then loads the result as an rvalue, |
| 612 | /// returning the rvalue. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 613 | RValue CodeGenFunction::EmitLoadOfLValue(LValue LV, QualType ExprType) { |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 614 | if (LV.isObjCWeak()) { |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 615 | // load of a __weak object. |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 616 | llvm::Value *AddrWeakObj = LV.getAddress(); |
Chris Lattner | eb99b01 | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 617 | return RValue::get(CGM.getObjCRuntime().EmitObjCWeakRead(*this, |
| 618 | AddrWeakObj)); |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 619 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 620 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 621 | if (LV.isSimple()) { |
| 622 | llvm::Value *Ptr = LV.getAddress(); |
Douglas Gregor | a500210 | 2010-02-05 21:10:36 +0000 | [diff] [blame] | 623 | const llvm::Type *EltTy = |
| 624 | cast<llvm::PointerType>(Ptr->getType())->getElementType(); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 625 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 626 | // Simple scalar l-value. |
Daniel Dunbar | e86bcf0 | 2010-02-08 22:53:07 +0000 | [diff] [blame] | 627 | // |
| 628 | // FIXME: We shouldn't have to use isSingleValueType here. |
Douglas Gregor | a500210 | 2010-02-05 21:10:36 +0000 | [diff] [blame] | 629 | if (EltTy->isSingleValueType()) |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 630 | return RValue::get(EmitLoadOfScalar(Ptr, LV.isVolatileQualified(), |
Daniel Dunbar | 9d9cc87 | 2009-02-10 00:57:50 +0000 | [diff] [blame] | 631 | ExprType)); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 632 | |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 633 | assert(ExprType->isFunctionType() && "Unknown scalar value"); |
| 634 | return RValue::get(Ptr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 635 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 636 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 637 | if (LV.isVectorElt()) { |
Eli Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 638 | llvm::Value *Vec = Builder.CreateLoad(LV.getVectorAddr(), |
| 639 | LV.isVolatileQualified(), "tmp"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 640 | return RValue::get(Builder.CreateExtractElement(Vec, LV.getVectorIdx(), |
| 641 | "vecext")); |
| 642 | } |
Chris Lattner | 46ea8eb | 2007-08-03 00:16:29 +0000 | [diff] [blame] | 643 | |
| 644 | // If this is a reference to a subset of the elements of a vector, either |
| 645 | // shuffle the input or extract/insert them as appropriate. |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 646 | if (LV.isExtVectorElt()) |
| 647 | return EmitLoadOfExtVectorElementLValue(LV, ExprType); |
Lauro Ramos Venancio | 3b8c22d | 2008-01-22 20:17:04 +0000 | [diff] [blame] | 648 | |
Daniel Dunbar | f0fe5bc | 2010-04-05 21:36:35 +0000 | [diff] [blame] | 649 | if (LV.isBitField()) |
Lauro Ramos Venancio | 3b8c22d | 2008-01-22 20:17:04 +0000 | [diff] [blame] | 650 | return EmitLoadOfBitfieldLValue(LV, ExprType); |
| 651 | |
Daniel Dunbar | 85c59ed | 2008-08-29 08:11:39 +0000 | [diff] [blame] | 652 | if (LV.isPropertyRef()) |
| 653 | return EmitLoadOfPropertyRefLValue(LV, ExprType); |
| 654 | |
Chris Lattner | 73525de | 2009-02-16 21:11:58 +0000 | [diff] [blame] | 655 | assert(LV.isKVCRef() && "Unknown LValue type!"); |
| 656 | return EmitLoadOfKVCRefLValue(LV, ExprType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 657 | } |
| 658 | |
Lauro Ramos Venancio | 3b8c22d | 2008-01-22 20:17:04 +0000 | [diff] [blame] | 659 | RValue CodeGenFunction::EmitLoadOfBitfieldLValue(LValue LV, |
| 660 | QualType ExprType) { |
Daniel Dunbar | efbf487 | 2010-04-06 01:07:44 +0000 | [diff] [blame] | 661 | const CGBitFieldInfo &Info = LV.getBitFieldInfo(); |
Daniel Dunbar | 10e3ded | 2008-08-06 05:08:45 +0000 | [diff] [blame] | 662 | |
Daniel Dunbar | ecdb41e | 2010-04-13 23:34:15 +0000 | [diff] [blame] | 663 | // Get the output type. |
| 664 | const llvm::Type *ResLTy = ConvertType(ExprType); |
| 665 | unsigned ResSizeInBits = CGM.getTargetData().getTypeSizeInBits(ResLTy); |
Lauro Ramos Venancio | 3b8c22d | 2008-01-22 20:17:04 +0000 | [diff] [blame] | 666 | |
Daniel Dunbar | ecdb41e | 2010-04-13 23:34:15 +0000 | [diff] [blame] | 667 | // Compute the result as an OR of all of the individual component accesses. |
| 668 | llvm::Value *Res = 0; |
| 669 | for (unsigned i = 0, e = Info.getNumComponents(); i != e; ++i) { |
| 670 | const CGBitFieldInfo::AccessInfo &AI = Info.getComponent(i); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 671 | |
Daniel Dunbar | ecdb41e | 2010-04-13 23:34:15 +0000 | [diff] [blame] | 672 | // Get the field pointer. |
| 673 | llvm::Value *Ptr = LV.getBitFieldBaseAddr(); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 674 | |
Daniel Dunbar | ecdb41e | 2010-04-13 23:34:15 +0000 | [diff] [blame] | 675 | // Only offset by the field index if used, so that incoming values are not |
| 676 | // required to be structures. |
| 677 | if (AI.FieldIndex) |
| 678 | Ptr = Builder.CreateStructGEP(Ptr, AI.FieldIndex, "bf.field"); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 679 | |
Daniel Dunbar | ecdb41e | 2010-04-13 23:34:15 +0000 | [diff] [blame] | 680 | // Offset by the byte offset, if used. |
| 681 | if (AI.FieldByteOffset) { |
| 682 | const llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(VMContext); |
| 683 | Ptr = Builder.CreateBitCast(Ptr, i8PTy); |
| 684 | Ptr = Builder.CreateConstGEP1_32(Ptr, AI.FieldByteOffset,"bf.field.offs"); |
| 685 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 686 | |
Daniel Dunbar | ecdb41e | 2010-04-13 23:34:15 +0000 | [diff] [blame] | 687 | // Cast to the access type. |
| 688 | const llvm::Type *PTy = llvm::Type::getIntNPtrTy(VMContext, AI.AccessWidth, |
| 689 | ExprType.getAddressSpace()); |
| 690 | Ptr = Builder.CreateBitCast(Ptr, PTy); |
Lauro Ramos Venancio | 3b8c22d | 2008-01-22 20:17:04 +0000 | [diff] [blame] | 691 | |
Daniel Dunbar | ecdb41e | 2010-04-13 23:34:15 +0000 | [diff] [blame] | 692 | // Perform the load. |
| 693 | llvm::LoadInst *Load = Builder.CreateLoad(Ptr, LV.isVolatileQualified()); |
| 694 | if (AI.AccessAlignment) |
| 695 | Load->setAlignment(AI.AccessAlignment); |
| 696 | |
| 697 | // Shift out unused low bits and mask out unused high bits. |
| 698 | llvm::Value *Val = Load; |
| 699 | if (AI.FieldBitStart) |
Daniel Dunbar | 2677261 | 2010-04-15 03:47:33 +0000 | [diff] [blame] | 700 | Val = Builder.CreateLShr(Load, AI.FieldBitStart); |
Daniel Dunbar | ecdb41e | 2010-04-13 23:34:15 +0000 | [diff] [blame] | 701 | Val = Builder.CreateAnd(Val, llvm::APInt::getLowBitsSet(AI.AccessWidth, |
| 702 | AI.TargetBitWidth), |
| 703 | "bf.clear"); |
| 704 | |
| 705 | // Extend or truncate to the target size. |
| 706 | if (AI.AccessWidth < ResSizeInBits) |
| 707 | Val = Builder.CreateZExt(Val, ResLTy); |
| 708 | else if (AI.AccessWidth > ResSizeInBits) |
| 709 | Val = Builder.CreateTrunc(Val, ResLTy); |
| 710 | |
| 711 | // Shift into place, and OR into the result. |
| 712 | if (AI.TargetBitOffset) |
| 713 | Val = Builder.CreateShl(Val, AI.TargetBitOffset); |
| 714 | Res = Res ? Builder.CreateOr(Res, Val) : Val; |
Daniel Dunbar | 10e3ded | 2008-08-06 05:08:45 +0000 | [diff] [blame] | 715 | } |
Lauro Ramos Venancio | 3b8c22d | 2008-01-22 20:17:04 +0000 | [diff] [blame] | 716 | |
Daniel Dunbar | ecdb41e | 2010-04-13 23:34:15 +0000 | [diff] [blame] | 717 | // If the bit-field is signed, perform the sign-extension. |
| 718 | // |
| 719 | // FIXME: This can easily be folded into the load of the high bits, which |
| 720 | // could also eliminate the mask of high bits in some situations. |
| 721 | if (Info.isSigned()) { |
Daniel Dunbar | 2677261 | 2010-04-15 03:47:33 +0000 | [diff] [blame] | 722 | unsigned ExtraBits = ResSizeInBits - Info.getSize(); |
Daniel Dunbar | ecdb41e | 2010-04-13 23:34:15 +0000 | [diff] [blame] | 723 | if (ExtraBits) |
| 724 | Res = Builder.CreateAShr(Builder.CreateShl(Res, ExtraBits), |
| 725 | ExtraBits, "bf.val.sext"); |
Daniel Dunbar | 10e3ded | 2008-08-06 05:08:45 +0000 | [diff] [blame] | 726 | } |
Eli Friedman | 316bb1b | 2008-05-17 20:03:47 +0000 | [diff] [blame] | 727 | |
Daniel Dunbar | ecdb41e | 2010-04-13 23:34:15 +0000 | [diff] [blame] | 728 | return RValue::get(Res); |
Lauro Ramos Venancio | 3b8c22d | 2008-01-22 20:17:04 +0000 | [diff] [blame] | 729 | } |
| 730 | |
Daniel Dunbar | 85c59ed | 2008-08-29 08:11:39 +0000 | [diff] [blame] | 731 | RValue CodeGenFunction::EmitLoadOfPropertyRefLValue(LValue LV, |
| 732 | QualType ExprType) { |
| 733 | return EmitObjCPropertyGet(LV.getPropertyRefExpr()); |
| 734 | } |
| 735 | |
Fariborz Jahanian | 43f4470 | 2008-11-22 22:30:21 +0000 | [diff] [blame] | 736 | RValue CodeGenFunction::EmitLoadOfKVCRefLValue(LValue LV, |
| 737 | QualType ExprType) { |
| 738 | return EmitObjCPropertyGet(LV.getKVCRefExpr()); |
| 739 | } |
| 740 | |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 741 | // If this is a reference to a subset of the elements of a vector, create an |
| 742 | // appropriate shufflevector. |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 743 | RValue CodeGenFunction::EmitLoadOfExtVectorElementLValue(LValue LV, |
| 744 | QualType ExprType) { |
Eli Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 745 | llvm::Value *Vec = Builder.CreateLoad(LV.getExtVectorAddr(), |
| 746 | LV.isVolatileQualified(), "tmp"); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 747 | |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 748 | const llvm::Constant *Elts = LV.getExtVectorElts(); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 749 | |
| 750 | // If the result of the expression is a non-vector type, we must be extracting |
| 751 | // a single element. Just codegen as an extractelement. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 752 | const VectorType *ExprVT = ExprType->getAs<VectorType>(); |
Chris Lattner | cf60cd2 | 2007-08-10 17:10:08 +0000 | [diff] [blame] | 753 | if (!ExprVT) { |
Dan Gohman | 4f8d123 | 2008-05-22 00:50:06 +0000 | [diff] [blame] | 754 | unsigned InIdx = getAccessedFieldNo(0, Elts); |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 755 | llvm::Value *Elt = llvm::ConstantInt::get(Int32Ty, InIdx); |
Chris Lattner | 34cdc86 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 756 | return RValue::get(Builder.CreateExtractElement(Vec, Elt, "tmp")); |
| 757 | } |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 758 | |
| 759 | // Always use shuffle vector to try to retain the original program structure |
Chris Lattner | cf60cd2 | 2007-08-10 17:10:08 +0000 | [diff] [blame] | 760 | unsigned NumResultElts = ExprVT->getNumElements(); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 761 | |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 762 | llvm::SmallVector<llvm::Constant*, 4> Mask; |
Chris Lattner | 34cdc86 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 763 | for (unsigned i = 0; i != NumResultElts; ++i) { |
Dan Gohman | 4f8d123 | 2008-05-22 00:50:06 +0000 | [diff] [blame] | 764 | unsigned InIdx = getAccessedFieldNo(i, Elts); |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 765 | Mask.push_back(llvm::ConstantInt::get(Int32Ty, InIdx)); |
Chris Lattner | 34cdc86 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 766 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 767 | |
Owen Anderson | 4a28932 | 2009-07-28 21:22:35 +0000 | [diff] [blame] | 768 | llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size()); |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 769 | Vec = Builder.CreateShuffleVector(Vec, |
Owen Anderson | 03e2050 | 2009-07-30 23:11:26 +0000 | [diff] [blame] | 770 | llvm::UndefValue::get(Vec->getType()), |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 771 | MaskV, "tmp"); |
| 772 | return RValue::get(Vec); |
Chris Lattner | 34cdc86 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 776 | |
| 777 | /// EmitStoreThroughLValue - Store the specified rvalue into the specified |
| 778 | /// lvalue, where both are guaranteed to the have the same type, and that type |
| 779 | /// is 'Ty'. |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 780 | void CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 781 | QualType Ty) { |
Chris Lattner | 017d6aa | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 782 | if (!Dst.isSimple()) { |
| 783 | if (Dst.isVectorElt()) { |
| 784 | // Read/modify/write the vector, inserting the new element. |
Eli Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 785 | llvm::Value *Vec = Builder.CreateLoad(Dst.getVectorAddr(), |
| 786 | Dst.isVolatileQualified(), "tmp"); |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 787 | Vec = Builder.CreateInsertElement(Vec, Src.getScalarVal(), |
Chris Lattner | 017d6aa | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 788 | Dst.getVectorIdx(), "vecins"); |
Eli Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 789 | Builder.CreateStore(Vec, Dst.getVectorAddr(),Dst.isVolatileQualified()); |
Chris Lattner | 017d6aa | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 790 | return; |
| 791 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 792 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 793 | // If this is an update of extended vector elements, insert them as |
| 794 | // appropriate. |
| 795 | if (Dst.isExtVectorElt()) |
| 796 | return EmitStoreThroughExtVectorComponentLValue(Src, Dst, Ty); |
Lauro Ramos Venancio | a0c5d0e | 2008-01-22 22:36:45 +0000 | [diff] [blame] | 797 | |
Daniel Dunbar | f0fe5bc | 2010-04-05 21:36:35 +0000 | [diff] [blame] | 798 | if (Dst.isBitField()) |
Lauro Ramos Venancio | a0c5d0e | 2008-01-22 22:36:45 +0000 | [diff] [blame] | 799 | return EmitStoreThroughBitfieldLValue(Src, Dst, Ty); |
| 800 | |
Daniel Dunbar | 85c59ed | 2008-08-29 08:11:39 +0000 | [diff] [blame] | 801 | if (Dst.isPropertyRef()) |
| 802 | return EmitStoreThroughPropertyRefLValue(Src, Dst, Ty); |
| 803 | |
Chris Lattner | eb99b01 | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 804 | assert(Dst.isKVCRef() && "Unknown LValue type"); |
| 805 | return EmitStoreThroughKVCRefLValue(Src, Dst, Ty); |
Chris Lattner | 017d6aa | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 806 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 807 | |
Fariborz Jahanian | 4f676ed | 2009-02-21 00:30:43 +0000 | [diff] [blame] | 808 | if (Dst.isObjCWeak() && !Dst.isNonGC()) { |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 809 | // load of a __weak object. |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 810 | llvm::Value *LvalueDst = Dst.getAddress(); |
| 811 | llvm::Value *src = Src.getScalarVal(); |
Mike Stump | f33651c | 2009-04-14 00:57:29 +0000 | [diff] [blame] | 812 | CGM.getObjCRuntime().EmitObjCWeakAssign(*this, src, LvalueDst); |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 813 | return; |
| 814 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 815 | |
Fariborz Jahanian | 4f676ed | 2009-02-21 00:30:43 +0000 | [diff] [blame] | 816 | if (Dst.isObjCStrong() && !Dst.isNonGC()) { |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 817 | // load of a __strong object. |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 818 | llvm::Value *LvalueDst = Dst.getAddress(); |
| 819 | llvm::Value *src = Src.getScalarVal(); |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 820 | if (Dst.isObjCIvar()) { |
| 821 | assert(Dst.getBaseIvarExp() && "BaseIvarExp is NULL"); |
| 822 | const llvm::Type *ResultType = ConvertType(getContext().LongTy); |
| 823 | llvm::Value *RHS = EmitScalarExpr(Dst.getBaseIvarExp()); |
Fariborz Jahanian | 76368e8 | 2009-09-25 00:00:20 +0000 | [diff] [blame] | 824 | llvm::Value *dst = RHS; |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 825 | RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast"); |
| 826 | llvm::Value *LHS = |
| 827 | Builder.CreatePtrToInt(LvalueDst, ResultType, "sub.ptr.lhs.cast"); |
| 828 | llvm::Value *BytesBetween = Builder.CreateSub(LHS, RHS, "ivar.offset"); |
Fariborz Jahanian | 76368e8 | 2009-09-25 00:00:20 +0000 | [diff] [blame] | 829 | CGM.getObjCRuntime().EmitObjCIvarAssign(*this, src, dst, |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 830 | BytesBetween); |
Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 831 | } else if (Dst.isGlobalObjCRef()) { |
| 832 | CGM.getObjCRuntime().EmitObjCGlobalAssign(*this, src, LvalueDst, |
| 833 | Dst.isThreadLocalRef()); |
| 834 | } |
Fariborz Jahanian | bf63b87 | 2009-05-04 23:27:20 +0000 | [diff] [blame] | 835 | else |
| 836 | CGM.getObjCRuntime().EmitObjCStrongCastAssign(*this, src, LvalueDst); |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 837 | return; |
| 838 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 839 | |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 840 | assert(Src.isScalar() && "Can't emit an agg store with this method"); |
Anders Carlsson | b4aa466 | 2009-05-19 18:50:41 +0000 | [diff] [blame] | 841 | EmitStoreOfScalar(Src.getScalarVal(), Dst.getAddress(), |
| 842 | Dst.isVolatileQualified(), Ty); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 843 | } |
| 844 | |
Lauro Ramos Venancio | a0c5d0e | 2008-01-22 22:36:45 +0000 | [diff] [blame] | 845 | void CodeGenFunction::EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst, |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 846 | QualType Ty, |
Daniel Dunbar | ed3849b | 2008-11-19 09:36:46 +0000 | [diff] [blame] | 847 | llvm::Value **Result) { |
Daniel Dunbar | efbf487 | 2010-04-06 01:07:44 +0000 | [diff] [blame] | 848 | const CGBitFieldInfo &Info = Dst.getBitFieldInfo(); |
Lauro Ramos Venancio | a0c5d0e | 2008-01-22 22:36:45 +0000 | [diff] [blame] | 849 | |
Daniel Dunbar | 2677261 | 2010-04-15 03:47:33 +0000 | [diff] [blame] | 850 | // Get the output type. |
Anders Carlsson | 4803535 | 2010-04-17 21:52:22 +0000 | [diff] [blame] | 851 | const llvm::Type *ResLTy = ConvertTypeForMem(Ty); |
Daniel Dunbar | 2677261 | 2010-04-15 03:47:33 +0000 | [diff] [blame] | 852 | unsigned ResSizeInBits = CGM.getTargetData().getTypeSizeInBits(ResLTy); |
Daniel Dunbar | 10e3ded | 2008-08-06 05:08:45 +0000 | [diff] [blame] | 853 | |
Daniel Dunbar | 2677261 | 2010-04-15 03:47:33 +0000 | [diff] [blame] | 854 | // Get the source value, truncated to the width of the bit-field. |
Daniel Dunbar | ed3849b | 2008-11-19 09:36:46 +0000 | [diff] [blame] | 855 | llvm::Value *SrcVal = Src.getScalarVal(); |
Anders Carlsson | 4803535 | 2010-04-17 21:52:22 +0000 | [diff] [blame] | 856 | |
| 857 | if (Ty->isBooleanType()) |
| 858 | SrcVal = Builder.CreateIntCast(SrcVal, ResLTy, /*IsSigned=*/false); |
| 859 | |
Daniel Dunbar | 2677261 | 2010-04-15 03:47:33 +0000 | [diff] [blame] | 860 | SrcVal = Builder.CreateAnd(SrcVal, llvm::APInt::getLowBitsSet(ResSizeInBits, |
| 861 | Info.getSize()), |
| 862 | "bf.value"); |
Lauro Ramos Venancio | a0c5d0e | 2008-01-22 22:36:45 +0000 | [diff] [blame] | 863 | |
Daniel Dunbar | ed3849b | 2008-11-19 09:36:46 +0000 | [diff] [blame] | 864 | // Return the new value of the bit-field, if requested. |
| 865 | if (Result) { |
| 866 | // Cast back to the proper type for result. |
Daniel Dunbar | 2677261 | 2010-04-15 03:47:33 +0000 | [diff] [blame] | 867 | const llvm::Type *SrcTy = Src.getScalarVal()->getType(); |
| 868 | llvm::Value *ReloadVal = Builder.CreateIntCast(SrcVal, SrcTy, false, |
| 869 | "bf.reload.val"); |
Daniel Dunbar | ed3849b | 2008-11-19 09:36:46 +0000 | [diff] [blame] | 870 | |
| 871 | // Sign extend if necessary. |
Daniel Dunbar | 2677261 | 2010-04-15 03:47:33 +0000 | [diff] [blame] | 872 | if (Info.isSigned()) { |
| 873 | unsigned ExtraBits = ResSizeInBits - Info.getSize(); |
| 874 | if (ExtraBits) |
| 875 | ReloadVal = Builder.CreateAShr(Builder.CreateShl(ReloadVal, ExtraBits), |
| 876 | ExtraBits, "bf.reload.sext"); |
Daniel Dunbar | ed3849b | 2008-11-19 09:36:46 +0000 | [diff] [blame] | 877 | } |
| 878 | |
Daniel Dunbar | 2677261 | 2010-04-15 03:47:33 +0000 | [diff] [blame] | 879 | *Result = ReloadVal; |
Daniel Dunbar | ed3849b | 2008-11-19 09:36:46 +0000 | [diff] [blame] | 880 | } |
| 881 | |
Daniel Dunbar | 2677261 | 2010-04-15 03:47:33 +0000 | [diff] [blame] | 882 | // Iterate over the components, writing each piece to memory. |
| 883 | for (unsigned i = 0, e = Info.getNumComponents(); i != e; ++i) { |
| 884 | const CGBitFieldInfo::AccessInfo &AI = Info.getComponent(i); |
Eli Friedman | 316bb1b | 2008-05-17 20:03:47 +0000 | [diff] [blame] | 885 | |
Daniel Dunbar | 2677261 | 2010-04-15 03:47:33 +0000 | [diff] [blame] | 886 | // Get the field pointer. |
| 887 | llvm::Value *Ptr = Dst.getBitFieldBaseAddr(); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 888 | |
Daniel Dunbar | 2677261 | 2010-04-15 03:47:33 +0000 | [diff] [blame] | 889 | // Only offset by the field index if used, so that incoming values are not |
| 890 | // required to be structures. |
| 891 | if (AI.FieldIndex) |
| 892 | Ptr = Builder.CreateStructGEP(Ptr, AI.FieldIndex, "bf.field"); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 893 | |
Daniel Dunbar | 2677261 | 2010-04-15 03:47:33 +0000 | [diff] [blame] | 894 | // Offset by the byte offset, if used. |
| 895 | if (AI.FieldByteOffset) { |
| 896 | const llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(VMContext); |
| 897 | Ptr = Builder.CreateBitCast(Ptr, i8PTy); |
| 898 | Ptr = Builder.CreateConstGEP1_32(Ptr, AI.FieldByteOffset,"bf.field.offs"); |
| 899 | } |
Eli Friedman | 316bb1b | 2008-05-17 20:03:47 +0000 | [diff] [blame] | 900 | |
Daniel Dunbar | 2677261 | 2010-04-15 03:47:33 +0000 | [diff] [blame] | 901 | // Cast to the access type. |
| 902 | const llvm::Type *PTy = llvm::Type::getIntNPtrTy(VMContext, AI.AccessWidth, |
| 903 | Ty.getAddressSpace()); |
| 904 | Ptr = Builder.CreateBitCast(Ptr, PTy); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 905 | |
Daniel Dunbar | 2677261 | 2010-04-15 03:47:33 +0000 | [diff] [blame] | 906 | // Extract the piece of the bit-field value to write in this access, limited |
| 907 | // to the values that are part of this access. |
| 908 | llvm::Value *Val = SrcVal; |
| 909 | if (AI.TargetBitOffset) |
| 910 | Val = Builder.CreateLShr(Val, AI.TargetBitOffset); |
| 911 | Val = Builder.CreateAnd(Val, llvm::APInt::getLowBitsSet(ResSizeInBits, |
| 912 | AI.TargetBitWidth)); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 913 | |
Daniel Dunbar | 2677261 | 2010-04-15 03:47:33 +0000 | [diff] [blame] | 914 | // Extend or truncate to the access size. |
| 915 | const llvm::Type *AccessLTy = |
| 916 | llvm::Type::getIntNTy(VMContext, AI.AccessWidth); |
| 917 | if (ResSizeInBits < AI.AccessWidth) |
| 918 | Val = Builder.CreateZExt(Val, AccessLTy); |
| 919 | else if (ResSizeInBits > AI.AccessWidth) |
| 920 | Val = Builder.CreateTrunc(Val, AccessLTy); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 921 | |
Daniel Dunbar | 2677261 | 2010-04-15 03:47:33 +0000 | [diff] [blame] | 922 | // Shift into the position in memory. |
| 923 | if (AI.FieldBitStart) |
| 924 | Val = Builder.CreateShl(Val, AI.FieldBitStart); |
| 925 | |
| 926 | // If necessary, load and OR in bits that are outside of the bit-field. |
| 927 | if (AI.TargetBitWidth != AI.AccessWidth) { |
| 928 | llvm::LoadInst *Load = Builder.CreateLoad(Ptr, Dst.isVolatileQualified()); |
| 929 | if (AI.AccessAlignment) |
| 930 | Load->setAlignment(AI.AccessAlignment); |
| 931 | |
| 932 | // Compute the mask for zeroing the bits that are part of the bit-field. |
| 933 | llvm::APInt InvMask = |
| 934 | ~llvm::APInt::getBitsSet(AI.AccessWidth, AI.FieldBitStart, |
| 935 | AI.FieldBitStart + AI.TargetBitWidth); |
| 936 | |
| 937 | // Apply the mask and OR in to the value to write. |
| 938 | Val = Builder.CreateOr(Builder.CreateAnd(Load, InvMask), Val); |
| 939 | } |
| 940 | |
| 941 | // Write the value. |
| 942 | llvm::StoreInst *Store = Builder.CreateStore(Val, Ptr, |
| 943 | Dst.isVolatileQualified()); |
| 944 | if (AI.AccessAlignment) |
| 945 | Store->setAlignment(AI.AccessAlignment); |
Daniel Dunbar | 10e3ded | 2008-08-06 05:08:45 +0000 | [diff] [blame] | 946 | } |
Lauro Ramos Venancio | a0c5d0e | 2008-01-22 22:36:45 +0000 | [diff] [blame] | 947 | } |
| 948 | |
Daniel Dunbar | 85c59ed | 2008-08-29 08:11:39 +0000 | [diff] [blame] | 949 | void CodeGenFunction::EmitStoreThroughPropertyRefLValue(RValue Src, |
| 950 | LValue Dst, |
| 951 | QualType Ty) { |
| 952 | EmitObjCPropertySet(Dst.getPropertyRefExpr(), Src); |
| 953 | } |
| 954 | |
Fariborz Jahanian | 43f4470 | 2008-11-22 22:30:21 +0000 | [diff] [blame] | 955 | void CodeGenFunction::EmitStoreThroughKVCRefLValue(RValue Src, |
| 956 | LValue Dst, |
| 957 | QualType Ty) { |
| 958 | EmitObjCPropertySet(Dst.getKVCRefExpr(), Src); |
| 959 | } |
| 960 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 961 | void CodeGenFunction::EmitStoreThroughExtVectorComponentLValue(RValue Src, |
| 962 | LValue Dst, |
| 963 | QualType Ty) { |
Chris Lattner | 017d6aa | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 964 | // This access turns into a read/modify/write of the vector. Load the input |
| 965 | // value now. |
Eli Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 966 | llvm::Value *Vec = Builder.CreateLoad(Dst.getExtVectorAddr(), |
| 967 | Dst.isVolatileQualified(), "tmp"); |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 968 | const llvm::Constant *Elts = Dst.getExtVectorElts(); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 969 | |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 970 | llvm::Value *SrcVal = Src.getScalarVal(); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 971 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 972 | if (const VectorType *VTy = Ty->getAs<VectorType>()) { |
Chris Lattner | 7e6b51b | 2007-08-03 16:37:04 +0000 | [diff] [blame] | 973 | unsigned NumSrcElts = VTy->getNumElements(); |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 974 | unsigned NumDstElts = |
| 975 | cast<llvm::VectorType>(Vec->getType())->getNumElements(); |
| 976 | if (NumDstElts == NumSrcElts) { |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 977 | // Use shuffle vector is the src and destination are the same number of |
| 978 | // elements and restore the vector mask since it is on the side it will be |
| 979 | // stored. |
Nate Begeman | 6e5dd86 | 2009-06-26 21:12:50 +0000 | [diff] [blame] | 980 | llvm::SmallVector<llvm::Constant*, 4> Mask(NumDstElts); |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 981 | for (unsigned i = 0; i != NumSrcElts; ++i) { |
| 982 | unsigned InIdx = getAccessedFieldNo(i, Elts); |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 983 | Mask[InIdx] = llvm::ConstantInt::get(Int32Ty, i); |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 984 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 985 | |
Owen Anderson | 4a28932 | 2009-07-28 21:22:35 +0000 | [diff] [blame] | 986 | llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size()); |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 987 | Vec = Builder.CreateShuffleVector(SrcVal, |
Owen Anderson | 03e2050 | 2009-07-30 23:11:26 +0000 | [diff] [blame] | 988 | llvm::UndefValue::get(Vec->getType()), |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 989 | MaskV, "tmp"); |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 990 | } else if (NumDstElts > NumSrcElts) { |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 991 | // Extended the source vector to the same length and then shuffle it |
| 992 | // into the destination. |
| 993 | // FIXME: since we're shuffling with undef, can we just use the indices |
| 994 | // into that? This could be simpler. |
| 995 | llvm::SmallVector<llvm::Constant*, 4> ExtMask; |
| 996 | unsigned i; |
| 997 | for (i = 0; i != NumSrcElts; ++i) |
Chris Lattner | eb99b01 | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 998 | ExtMask.push_back(llvm::ConstantInt::get(Int32Ty, i)); |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 999 | for (; i != NumDstElts; ++i) |
Chris Lattner | eb99b01 | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 1000 | ExtMask.push_back(llvm::UndefValue::get(Int32Ty)); |
Owen Anderson | 4a28932 | 2009-07-28 21:22:35 +0000 | [diff] [blame] | 1001 | llvm::Value *ExtMaskV = llvm::ConstantVector::get(&ExtMask[0], |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 1002 | ExtMask.size()); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1003 | llvm::Value *ExtSrcVal = |
Daniel Dunbar | bb76773 | 2009-02-17 18:31:04 +0000 | [diff] [blame] | 1004 | Builder.CreateShuffleVector(SrcVal, |
Owen Anderson | 03e2050 | 2009-07-30 23:11:26 +0000 | [diff] [blame] | 1005 | llvm::UndefValue::get(SrcVal->getType()), |
Daniel Dunbar | bb76773 | 2009-02-17 18:31:04 +0000 | [diff] [blame] | 1006 | ExtMaskV, "tmp"); |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 1007 | // build identity |
| 1008 | llvm::SmallVector<llvm::Constant*, 4> Mask; |
Chris Lattner | eb99b01 | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 1009 | for (unsigned i = 0; i != NumDstElts; ++i) |
| 1010 | Mask.push_back(llvm::ConstantInt::get(Int32Ty, i)); |
| 1011 | |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 1012 | // modify when what gets shuffled in |
| 1013 | for (unsigned i = 0; i != NumSrcElts; ++i) { |
| 1014 | unsigned Idx = getAccessedFieldNo(i, Elts); |
Chris Lattner | eb99b01 | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 1015 | Mask[Idx] = llvm::ConstantInt::get(Int32Ty, i+NumDstElts); |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 1016 | } |
Owen Anderson | 4a28932 | 2009-07-28 21:22:35 +0000 | [diff] [blame] | 1017 | llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size()); |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 1018 | Vec = Builder.CreateShuffleVector(Vec, ExtSrcVal, MaskV, "tmp"); |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 1019 | } else { |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 1020 | // We should never shorten the vector |
| 1021 | assert(0 && "unexpected shorten vector length"); |
Chris Lattner | 7e6b51b | 2007-08-03 16:37:04 +0000 | [diff] [blame] | 1022 | } |
| 1023 | } else { |
| 1024 | // If the Src is a scalar (not a vector) it must be updating one element. |
Dan Gohman | 4f8d123 | 2008-05-22 00:50:06 +0000 | [diff] [blame] | 1025 | unsigned InIdx = getAccessedFieldNo(0, Elts); |
Chris Lattner | eb99b01 | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 1026 | llvm::Value *Elt = llvm::ConstantInt::get(Int32Ty, InIdx); |
Chris Lattner | 017d6aa | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 1027 | Vec = Builder.CreateInsertElement(Vec, SrcVal, Elt, "tmp"); |
Chris Lattner | 017d6aa | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 1028 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1029 | |
Eli Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 1030 | Builder.CreateStore(Vec, Dst.getExtVectorAddr(), Dst.isVolatileQualified()); |
Chris Lattner | 017d6aa | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 1031 | } |
| 1032 | |
Fariborz Jahanian | b123ea3 | 2009-09-16 21:37:16 +0000 | [diff] [blame] | 1033 | // setObjCGCLValueClass - sets class of he lvalue for the purpose of |
| 1034 | // generating write-barries API. It is currently a global, ivar, |
| 1035 | // or neither. |
Chris Lattner | eb99b01 | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 1036 | static void setObjCGCLValueClass(const ASTContext &Ctx, const Expr *E, |
| 1037 | LValue &LV) { |
Fariborz Jahanian | b924259 | 2009-09-21 23:03:37 +0000 | [diff] [blame] | 1038 | if (Ctx.getLangOptions().getGCMode() == LangOptions::NonGC) |
Fariborz Jahanian | b123ea3 | 2009-09-16 21:37:16 +0000 | [diff] [blame] | 1039 | return; |
| 1040 | |
Fariborz Jahanian | dbf3cfd | 2009-09-16 23:11:23 +0000 | [diff] [blame] | 1041 | if (isa<ObjCIvarRefExpr>(E)) { |
| 1042 | LV.SetObjCIvar(LV, true); |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 1043 | ObjCIvarRefExpr *Exp = cast<ObjCIvarRefExpr>(const_cast<Expr*>(E)); |
| 1044 | LV.setBaseIvarExp(Exp->getBase()); |
Fariborz Jahanian | fd02ed7 | 2009-09-21 18:54:29 +0000 | [diff] [blame] | 1045 | LV.SetObjCArray(LV, E->getType()->isArrayType()); |
Fariborz Jahanian | dbf3cfd | 2009-09-16 23:11:23 +0000 | [diff] [blame] | 1046 | return; |
| 1047 | } |
Chris Lattner | eb99b01 | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 1048 | |
Fariborz Jahanian | b123ea3 | 2009-09-16 21:37:16 +0000 | [diff] [blame] | 1049 | if (const DeclRefExpr *Exp = dyn_cast<DeclRefExpr>(E)) { |
| 1050 | if (const VarDecl *VD = dyn_cast<VarDecl>(Exp->getDecl())) { |
| 1051 | if ((VD->isBlockVarDecl() && !VD->hasLocalStorage()) || |
Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 1052 | VD->isFileVarDecl()) { |
Fariborz Jahanian | b123ea3 | 2009-09-16 21:37:16 +0000 | [diff] [blame] | 1053 | LV.SetGlobalObjCRef(LV, true); |
Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 1054 | LV.SetThreadLocalRef(LV, VD->isThreadSpecified()); |
| 1055 | } |
Fariborz Jahanian | b123ea3 | 2009-09-16 21:37:16 +0000 | [diff] [blame] | 1056 | } |
Fariborz Jahanian | fd02ed7 | 2009-09-21 18:54:29 +0000 | [diff] [blame] | 1057 | LV.SetObjCArray(LV, E->getType()->isArrayType()); |
Chris Lattner | eb99b01 | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 1058 | return; |
Fariborz Jahanian | b123ea3 | 2009-09-16 21:37:16 +0000 | [diff] [blame] | 1059 | } |
Chris Lattner | eb99b01 | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 1060 | |
| 1061 | if (const UnaryOperator *Exp = dyn_cast<UnaryOperator>(E)) { |
Fariborz Jahanian | b123ea3 | 2009-09-16 21:37:16 +0000 | [diff] [blame] | 1062 | setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV); |
Chris Lattner | eb99b01 | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 1063 | return; |
| 1064 | } |
| 1065 | |
| 1066 | if (const ParenExpr *Exp = dyn_cast<ParenExpr>(E)) { |
Fariborz Jahanian | b123ea3 | 2009-09-16 21:37:16 +0000 | [diff] [blame] | 1067 | setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV); |
Fariborz Jahanian | 75b08f1 | 2009-09-30 17:10:29 +0000 | [diff] [blame] | 1068 | if (LV.isObjCIvar()) { |
| 1069 | // If cast is to a structure pointer, follow gcc's behavior and make it |
| 1070 | // a non-ivar write-barrier. |
| 1071 | QualType ExpTy = E->getType(); |
| 1072 | if (ExpTy->isPointerType()) |
| 1073 | ExpTy = ExpTy->getAs<PointerType>()->getPointeeType(); |
| 1074 | if (ExpTy->isRecordType()) |
| 1075 | LV.SetObjCIvar(LV, false); |
Chris Lattner | eb99b01 | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 1076 | } |
| 1077 | return; |
Fariborz Jahanian | 75b08f1 | 2009-09-30 17:10:29 +0000 | [diff] [blame] | 1078 | } |
Chris Lattner | eb99b01 | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 1079 | if (const ImplicitCastExpr *Exp = dyn_cast<ImplicitCastExpr>(E)) { |
Fariborz Jahanian | b123ea3 | 2009-09-16 21:37:16 +0000 | [diff] [blame] | 1080 | setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV); |
Chris Lattner | eb99b01 | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 1081 | return; |
| 1082 | } |
| 1083 | |
| 1084 | if (const CStyleCastExpr *Exp = dyn_cast<CStyleCastExpr>(E)) { |
Fariborz Jahanian | b123ea3 | 2009-09-16 21:37:16 +0000 | [diff] [blame] | 1085 | setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV); |
Chris Lattner | eb99b01 | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 1086 | return; |
| 1087 | } |
| 1088 | |
| 1089 | if (const ArraySubscriptExpr *Exp = dyn_cast<ArraySubscriptExpr>(E)) { |
Fariborz Jahanian | b123ea3 | 2009-09-16 21:37:16 +0000 | [diff] [blame] | 1090 | setObjCGCLValueClass(Ctx, Exp->getBase(), LV); |
Fariborz Jahanian | fd02ed7 | 2009-09-21 18:54:29 +0000 | [diff] [blame] | 1091 | if (LV.isObjCIvar() && !LV.isObjCArray()) |
Fariborz Jahanian | 1c1afc4 | 2009-09-18 00:04:00 +0000 | [diff] [blame] | 1092 | // Using array syntax to assigning to what an ivar points to is not |
| 1093 | // same as assigning to the ivar itself. {id *Names;} Names[i] = 0; |
| 1094 | LV.SetObjCIvar(LV, false); |
Fariborz Jahanian | fd02ed7 | 2009-09-21 18:54:29 +0000 | [diff] [blame] | 1095 | else if (LV.isGlobalObjCRef() && !LV.isObjCArray()) |
| 1096 | // Using array syntax to assigning to what global points to is not |
| 1097 | // same as assigning to the global itself. {id *G;} G[i] = 0; |
| 1098 | LV.SetGlobalObjCRef(LV, false); |
Chris Lattner | eb99b01 | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 1099 | return; |
Fariborz Jahanian | 1c1afc4 | 2009-09-18 00:04:00 +0000 | [diff] [blame] | 1100 | } |
Chris Lattner | eb99b01 | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 1101 | |
| 1102 | if (const MemberExpr *Exp = dyn_cast<MemberExpr>(E)) { |
Fariborz Jahanian | b123ea3 | 2009-09-16 21:37:16 +0000 | [diff] [blame] | 1103 | setObjCGCLValueClass(Ctx, Exp->getBase(), LV); |
Fariborz Jahanian | 1c1afc4 | 2009-09-18 00:04:00 +0000 | [diff] [blame] | 1104 | // We don't know if member is an 'ivar', but this flag is looked at |
| 1105 | // only in the context of LV.isObjCIvar(). |
Fariborz Jahanian | fd02ed7 | 2009-09-21 18:54:29 +0000 | [diff] [blame] | 1106 | LV.SetObjCArray(LV, E->getType()->isArrayType()); |
Chris Lattner | eb99b01 | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 1107 | return; |
Fariborz Jahanian | b123ea3 | 2009-09-16 21:37:16 +0000 | [diff] [blame] | 1108 | } |
| 1109 | } |
| 1110 | |
Anders Carlsson | ce53f7d | 2009-11-07 23:06:58 +0000 | [diff] [blame] | 1111 | static LValue EmitGlobalVarDeclLValue(CodeGenFunction &CGF, |
| 1112 | const Expr *E, const VarDecl *VD) { |
Daniel Dunbar | d2113f2 | 2009-11-08 09:46:46 +0000 | [diff] [blame] | 1113 | assert((VD->hasExternalStorage() || VD->isFileVarDecl()) && |
Anders Carlsson | ce53f7d | 2009-11-07 23:06:58 +0000 | [diff] [blame] | 1114 | "Var decl must have external storage or be a file var decl!"); |
| 1115 | |
| 1116 | llvm::Value *V = CGF.CGM.GetAddrOfGlobalVar(VD); |
| 1117 | if (VD->getType()->isReferenceType()) |
| 1118 | V = CGF.Builder.CreateLoad(V, "tmp"); |
| 1119 | LValue LV = LValue::MakeAddr(V, CGF.MakeQualifiers(E->getType())); |
| 1120 | setObjCGCLValueClass(CGF.getContext(), E, LV); |
| 1121 | return LV; |
| 1122 | } |
| 1123 | |
Eli Friedman | 9a14630 | 2009-11-26 06:08:14 +0000 | [diff] [blame] | 1124 | static LValue EmitFunctionDeclLValue(CodeGenFunction &CGF, |
| 1125 | const Expr *E, const FunctionDecl *FD) { |
| 1126 | llvm::Value* V = CGF.CGM.GetAddrOfFunction(FD); |
| 1127 | if (!FD->hasPrototype()) { |
| 1128 | if (const FunctionProtoType *Proto = |
| 1129 | FD->getType()->getAs<FunctionProtoType>()) { |
| 1130 | // Ugly case: for a K&R-style definition, the type of the definition |
| 1131 | // isn't the same as the type of a use. Correct for this with a |
| 1132 | // bitcast. |
| 1133 | QualType NoProtoType = |
| 1134 | CGF.getContext().getFunctionNoProtoType(Proto->getResultType()); |
| 1135 | NoProtoType = CGF.getContext().getPointerType(NoProtoType); |
| 1136 | V = CGF.Builder.CreateBitCast(V, CGF.ConvertType(NoProtoType), "tmp"); |
| 1137 | } |
| 1138 | } |
| 1139 | return LValue::MakeAddr(V, CGF.MakeQualifiers(E->getType())); |
| 1140 | } |
| 1141 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1142 | LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) { |
Anders Carlsson | 1e74c4f | 2009-11-07 22:53:10 +0000 | [diff] [blame] | 1143 | const NamedDecl *ND = E->getDecl(); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1144 | |
Rafael Espindola | 6a83670 | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 1145 | if (ND->hasAttr<WeakRefAttr>()) { |
| 1146 | const ValueDecl* VD = cast<ValueDecl>(ND); |
| 1147 | llvm::Constant *Aliasee = CGM.GetWeakRefReference(VD); |
| 1148 | |
| 1149 | Qualifiers Quals = MakeQualifiers(E->getType()); |
| 1150 | LValue LV = LValue::MakeAddr(Aliasee, Quals); |
| 1151 | |
| 1152 | return LV; |
| 1153 | } |
| 1154 | |
Anders Carlsson | 1e74c4f | 2009-11-07 22:53:10 +0000 | [diff] [blame] | 1155 | if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) { |
Anders Carlsson | 1e74c4f | 2009-11-07 22:53:10 +0000 | [diff] [blame] | 1156 | |
| 1157 | // Check if this is a global variable. |
Anders Carlsson | ce53f7d | 2009-11-07 23:06:58 +0000 | [diff] [blame] | 1158 | if (VD->hasExternalStorage() || VD->isFileVarDecl()) |
| 1159 | return EmitGlobalVarDeclLValue(*this, E, VD); |
Anders Carlsson | 0bc7049 | 2009-11-07 22:46:42 +0000 | [diff] [blame] | 1160 | |
| 1161 | bool NonGCable = VD->hasLocalStorage() && !VD->hasAttr<BlocksAttr>(); |
| 1162 | |
| 1163 | llvm::Value *V = LocalDeclMap[VD]; |
Fariborz Jahanian | 63326a5 | 2010-04-19 18:15:02 +0000 | [diff] [blame] | 1164 | if (!V && getContext().getLangOptions().CPlusPlus && |
| 1165 | VD->isStaticLocal()) |
| 1166 | V = CGM.getStaticLocalDeclAddress(VD); |
Anders Carlsson | 0bc7049 | 2009-11-07 22:46:42 +0000 | [diff] [blame] | 1167 | assert(V && "DeclRefExpr not entered in LocalDeclMap?"); |
| 1168 | |
| 1169 | Qualifiers Quals = MakeQualifiers(E->getType()); |
| 1170 | // local variables do not get their gc attribute set. |
| 1171 | // local static? |
| 1172 | if (NonGCable) Quals.removeObjCGCAttr(); |
| 1173 | |
| 1174 | if (VD->hasAttr<BlocksAttr>()) { |
| 1175 | V = Builder.CreateStructGEP(V, 1, "forwarding"); |
Daniel Dunbar | 2da84ff | 2009-11-29 21:23:36 +0000 | [diff] [blame] | 1176 | V = Builder.CreateLoad(V); |
Anders Carlsson | 0bc7049 | 2009-11-07 22:46:42 +0000 | [diff] [blame] | 1177 | V = Builder.CreateStructGEP(V, getByRefValueLLVMField(VD), |
| 1178 | VD->getNameAsString()); |
| 1179 | } |
| 1180 | if (VD->getType()->isReferenceType()) |
| 1181 | V = Builder.CreateLoad(V, "tmp"); |
Anders Carlsson | ce53f7d | 2009-11-07 23:06:58 +0000 | [diff] [blame] | 1182 | LValue LV = LValue::MakeAddr(V, Quals); |
Anders Carlsson | 0bc7049 | 2009-11-07 22:46:42 +0000 | [diff] [blame] | 1183 | LValue::SetObjCNonGC(LV, NonGCable); |
Fariborz Jahanian | b123ea3 | 2009-09-16 21:37:16 +0000 | [diff] [blame] | 1184 | setObjCGCLValueClass(getContext(), E, LV); |
Fariborz Jahanian | 2682d8b | 2008-11-20 00:15:42 +0000 | [diff] [blame] | 1185 | return LV; |
Chris Lattner | eb99b01 | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 1186 | } |
| 1187 | |
Eli Friedman | 9a14630 | 2009-11-26 06:08:14 +0000 | [diff] [blame] | 1188 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) |
| 1189 | return EmitFunctionDeclLValue(*this, E, FD); |
Chris Lattner | eb99b01 | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 1190 | |
Anders Carlsson | 45147d0 | 2010-02-02 03:37:46 +0000 | [diff] [blame] | 1191 | // FIXME: the qualifier check does not seem sufficient here |
Chris Lattner | eb99b01 | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 1192 | if (E->getQualifier()) { |
Anders Carlsson | 45147d0 | 2010-02-02 03:37:46 +0000 | [diff] [blame] | 1193 | const FieldDecl *FD = cast<FieldDecl>(ND); |
| 1194 | llvm::Value *V = CGM.EmitPointerToDataMember(FD); |
| 1195 | |
| 1196 | return LValue::MakeAddr(V, MakeQualifiers(FD->getType())); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 1197 | } |
Chris Lattner | eb99b01 | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 1198 | |
Anders Carlsson | 1e74c4f | 2009-11-07 22:53:10 +0000 | [diff] [blame] | 1199 | assert(false && "Unhandled DeclRefExpr"); |
| 1200 | |
| 1201 | // an invalid LValue, but the assert will |
| 1202 | // ensure that this point is never reached. |
Chris Lattner | b1776cb | 2007-09-16 19:23:47 +0000 | [diff] [blame] | 1203 | return LValue(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1204 | } |
| 1205 | |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 1206 | LValue CodeGenFunction::EmitBlockDeclRefLValue(const BlockDeclRefExpr *E) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1207 | return LValue::MakeAddr(GetAddrOfBlockDecl(E), MakeQualifiers(E->getType())); |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 1208 | } |
| 1209 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1210 | LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) { |
| 1211 | // __extension__ doesn't affect lvalue-ness. |
| 1212 | if (E->getOpcode() == UnaryOperator::Extension) |
| 1213 | return EmitLValue(E->getSubExpr()); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1214 | |
Chris Lattner | 9619662 | 2008-07-26 22:37:01 +0000 | [diff] [blame] | 1215 | QualType ExprTy = getContext().getCanonicalType(E->getSubExpr()->getType()); |
Chris Lattner | 7da36f6 | 2007-10-30 22:53:42 +0000 | [diff] [blame] | 1216 | switch (E->getOpcode()) { |
| 1217 | default: assert(0 && "Unknown unary operator lvalue!"); |
Chris Lattner | eb99b01 | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 1218 | case UnaryOperator::Deref: { |
| 1219 | QualType T = E->getSubExpr()->getType()->getPointeeType(); |
| 1220 | assert(!T.isNull() && "CodeGenFunction::EmitUnaryOpLValue: Illegal type"); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1221 | |
Chris Lattner | eb99b01 | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 1222 | Qualifiers Quals = MakeQualifiers(T); |
| 1223 | Quals.setAddressSpace(ExprTy.getAddressSpace()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1224 | |
Chris Lattner | eb99b01 | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 1225 | LValue LV = LValue::MakeAddr(EmitScalarExpr(E->getSubExpr()), Quals); |
| 1226 | // We should not generate __weak write barrier on indirect reference |
| 1227 | // of a pointer to object; as in void foo (__weak id *param); *param = 0; |
| 1228 | // But, we continue to generate __strong write barrier on indirect write |
| 1229 | // into a pointer to object. |
| 1230 | if (getContext().getLangOptions().ObjC1 && |
| 1231 | getContext().getLangOptions().getGCMode() != LangOptions::NonGC && |
| 1232 | LV.isObjCWeak()) |
| 1233 | LValue::SetObjCNonGC(LV, !E->isOBJCGCCandidate(getContext())); |
| 1234 | return LV; |
| 1235 | } |
Chris Lattner | 7da36f6 | 2007-10-30 22:53:42 +0000 | [diff] [blame] | 1236 | case UnaryOperator::Real: |
Eli Friedman | e401cd5 | 2009-11-09 04:20:47 +0000 | [diff] [blame] | 1237 | case UnaryOperator::Imag: { |
Chris Lattner | 7da36f6 | 2007-10-30 22:53:42 +0000 | [diff] [blame] | 1238 | LValue LV = EmitLValue(E->getSubExpr()); |
Chris Lattner | 36b6a0a | 2008-03-19 05:19:41 +0000 | [diff] [blame] | 1239 | unsigned Idx = E->getOpcode() == UnaryOperator::Imag; |
| 1240 | return LValue::MakeAddr(Builder.CreateStructGEP(LV.getAddress(), |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 1241 | Idx, "idx"), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1242 | MakeQualifiers(ExprTy)); |
Chris Lattner | 7da36f6 | 2007-10-30 22:53:42 +0000 | [diff] [blame] | 1243 | } |
Eli Friedman | e401cd5 | 2009-11-09 04:20:47 +0000 | [diff] [blame] | 1244 | case UnaryOperator::PreInc: |
Chris Lattner | 197a338 | 2010-01-09 21:44:40 +0000 | [diff] [blame] | 1245 | case UnaryOperator::PreDec: { |
| 1246 | LValue LV = EmitLValue(E->getSubExpr()); |
| 1247 | bool isInc = E->getOpcode() == UnaryOperator::PreInc; |
| 1248 | |
| 1249 | if (E->getType()->isAnyComplexType()) |
| 1250 | EmitComplexPrePostIncDec(E, LV, isInc, true/*isPre*/); |
| 1251 | else |
| 1252 | EmitScalarPrePostIncDec(E, LV, isInc, true/*isPre*/); |
| 1253 | return LV; |
| 1254 | } |
Eli Friedman | e401cd5 | 2009-11-09 04:20:47 +0000 | [diff] [blame] | 1255 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1256 | } |
| 1257 | |
| 1258 | LValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1259 | return LValue::MakeAddr(CGM.GetAddrOfConstantStringFromLiteral(E), |
| 1260 | Qualifiers()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1261 | } |
| 1262 | |
Chris Lattner | eaf2bb8 | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 1263 | LValue CodeGenFunction::EmitObjCEncodeExprLValue(const ObjCEncodeExpr *E) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1264 | return LValue::MakeAddr(CGM.GetAddrOfConstantStringFromObjCEncode(E), |
| 1265 | Qualifiers()); |
Chris Lattner | eaf2bb8 | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 1266 | } |
| 1267 | |
| 1268 | |
Daniel Dunbar | 662b71e | 2008-10-17 21:58:32 +0000 | [diff] [blame] | 1269 | LValue CodeGenFunction::EmitPredefinedFunctionName(unsigned Type) { |
Anders Carlsson | 2274266 | 2007-07-21 05:21:51 +0000 | [diff] [blame] | 1270 | std::string GlobalVarName; |
Daniel Dunbar | 662b71e | 2008-10-17 21:58:32 +0000 | [diff] [blame] | 1271 | |
| 1272 | switch (Type) { |
Chris Lattner | eb99b01 | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 1273 | default: assert(0 && "Invalid type"); |
Chris Lattner | eaf2bb8 | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 1274 | case PredefinedExpr::Func: |
| 1275 | GlobalVarName = "__func__."; |
| 1276 | break; |
| 1277 | case PredefinedExpr::Function: |
| 1278 | GlobalVarName = "__FUNCTION__."; |
| 1279 | break; |
| 1280 | case PredefinedExpr::PrettyFunction: |
Chris Lattner | eaf2bb8 | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 1281 | GlobalVarName = "__PRETTY_FUNCTION__."; |
| 1282 | break; |
Anders Carlsson | 2274266 | 2007-07-21 05:21:51 +0000 | [diff] [blame] | 1283 | } |
Daniel Dunbar | 662b71e | 2008-10-17 21:58:32 +0000 | [diff] [blame] | 1284 | |
Daniel Dunbar | 0a23d76 | 2009-09-12 23:06:21 +0000 | [diff] [blame] | 1285 | llvm::StringRef FnName = CurFn->getName(); |
| 1286 | if (FnName.startswith("\01")) |
| 1287 | FnName = FnName.substr(1); |
| 1288 | GlobalVarName += FnName; |
| 1289 | |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 1290 | std::string FunctionName = |
Anders Carlsson | 848fa64 | 2010-02-11 18:20:28 +0000 | [diff] [blame] | 1291 | PredefinedExpr::ComputeName((PredefinedExpr::IdentType)Type, CurCodeDecl); |
Daniel Dunbar | 662b71e | 2008-10-17 21:58:32 +0000 | [diff] [blame] | 1292 | |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1293 | llvm::Constant *C = |
Daniel Dunbar | 662b71e | 2008-10-17 21:58:32 +0000 | [diff] [blame] | 1294 | CGM.GetAddrOfConstantCString(FunctionName, GlobalVarName.c_str()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1295 | return LValue::MakeAddr(C, Qualifiers()); |
Daniel Dunbar | 662b71e | 2008-10-17 21:58:32 +0000 | [diff] [blame] | 1296 | } |
| 1297 | |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1298 | LValue CodeGenFunction::EmitPredefinedLValue(const PredefinedExpr *E) { |
Daniel Dunbar | 662b71e | 2008-10-17 21:58:32 +0000 | [diff] [blame] | 1299 | switch (E->getIdentType()) { |
| 1300 | default: |
| 1301 | return EmitUnsupportedLValue(E, "predefined expression"); |
| 1302 | case PredefinedExpr::Func: |
| 1303 | case PredefinedExpr::Function: |
| 1304 | case PredefinedExpr::PrettyFunction: |
| 1305 | return EmitPredefinedFunctionName(E->getIdentType()); |
| 1306 | } |
Anders Carlsson | 2274266 | 2007-07-21 05:21:51 +0000 | [diff] [blame] | 1307 | } |
| 1308 | |
Mike Stump | d8af360 | 2009-12-15 01:22:35 +0000 | [diff] [blame] | 1309 | llvm::BasicBlock *CodeGenFunction::getTrapBB() { |
Mike Stump | 4151344 | 2009-12-15 00:59:40 +0000 | [diff] [blame] | 1310 | const CodeGenOptions &GCO = CGM.getCodeGenOpts(); |
| 1311 | |
| 1312 | // If we are not optimzing, don't collapse all calls to trap in the function |
| 1313 | // to the same call, that way, in the debugger they can see which operation |
Chris Lattner | 6c552c1 | 2010-07-20 20:19:24 +0000 | [diff] [blame] | 1314 | // did in fact fail. If we are optimizing, we collapse all calls to trap down |
Mike Stump | 4151344 | 2009-12-15 00:59:40 +0000 | [diff] [blame] | 1315 | // to just one per function to save on codesize. |
Chris Lattner | 6c552c1 | 2010-07-20 20:19:24 +0000 | [diff] [blame] | 1316 | if (GCO.OptimizationLevel && TrapBB) |
Mike Stump | 15037ca | 2009-12-15 00:35:12 +0000 | [diff] [blame] | 1317 | return TrapBB; |
Mike Stump | 9c276ae | 2009-12-12 01:27:46 +0000 | [diff] [blame] | 1318 | |
| 1319 | llvm::BasicBlock *Cont = 0; |
| 1320 | if (HaveInsertPoint()) { |
| 1321 | Cont = createBasicBlock("cont"); |
| 1322 | EmitBranch(Cont); |
| 1323 | } |
Mike Stump | 15037ca | 2009-12-15 00:35:12 +0000 | [diff] [blame] | 1324 | TrapBB = createBasicBlock("trap"); |
| 1325 | EmitBlock(TrapBB); |
| 1326 | |
| 1327 | llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::trap, 0, 0); |
| 1328 | llvm::CallInst *TrapCall = Builder.CreateCall(F); |
| 1329 | TrapCall->setDoesNotReturn(); |
| 1330 | TrapCall->setDoesNotThrow(); |
Mike Stump | 9c276ae | 2009-12-12 01:27:46 +0000 | [diff] [blame] | 1331 | Builder.CreateUnreachable(); |
| 1332 | |
| 1333 | if (Cont) |
| 1334 | EmitBlock(Cont); |
Mike Stump | 15037ca | 2009-12-15 00:35:12 +0000 | [diff] [blame] | 1335 | return TrapBB; |
Mike Stump | 9c276ae | 2009-12-12 01:27:46 +0000 | [diff] [blame] | 1336 | } |
| 1337 | |
Chris Lattner | 9269d5c | 2010-06-26 23:03:20 +0000 | [diff] [blame] | 1338 | /// isSimpleArrayDecayOperand - If the specified expr is a simple decay from an |
| 1339 | /// array to pointer, return the array subexpression. |
| 1340 | static const Expr *isSimpleArrayDecayOperand(const Expr *E) { |
| 1341 | // If this isn't just an array->pointer decay, bail out. |
| 1342 | const CastExpr *CE = dyn_cast<CastExpr>(E); |
| 1343 | if (CE == 0 || CE->getCastKind() != CastExpr::CK_ArrayToPointerDecay) |
| 1344 | return 0; |
| 1345 | |
| 1346 | // If this is a decay from variable width array, bail out. |
| 1347 | const Expr *SubExpr = CE->getSubExpr(); |
| 1348 | if (SubExpr->getType()->isVariableArrayType()) |
| 1349 | return 0; |
| 1350 | |
| 1351 | return SubExpr; |
| 1352 | } |
| 1353 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1354 | LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) { |
Ted Kremenek | 2324512 | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 1355 | // The index must always be an integer, which is not an aggregate. Emit it. |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1356 | llvm::Value *Idx = EmitScalarExpr(E->getIdx()); |
Eli Friedman | 61d004a | 2009-06-06 19:09:26 +0000 | [diff] [blame] | 1357 | QualType IdxTy = E->getIdx()->getType(); |
| 1358 | bool IdxSigned = IdxTy->isSignedIntegerType(); |
| 1359 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1360 | // If the base is a vector type, then we are forming a vector element lvalue |
| 1361 | // with this subscript. |
Eli Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 1362 | if (E->getBase()->getType()->isVectorType()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1363 | // Emit the vector as an lvalue to get its address. |
Eli Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 1364 | LValue LHS = EmitLValue(E->getBase()); |
Ted Kremenek | 2324512 | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 1365 | assert(LHS.isSimple() && "Can only subscript lvalue vectors here!"); |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 1366 | Idx = Builder.CreateIntCast(Idx, CGF.Int32Ty, IdxSigned, "vidx"); |
Eli Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 1367 | return LValue::MakeVectorElt(LHS.getAddress(), Idx, |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1368 | E->getBase()->getType().getCVRQualifiers()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1369 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1370 | |
Ted Kremenek | 2324512 | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 1371 | // Extend or truncate the index type to 32 or 64-bits. |
Chris Lattner | a4d94ab | 2010-06-26 22:40:46 +0000 | [diff] [blame] | 1372 | if (!Idx->getType()->isIntegerTy(LLVMPointerWidth)) |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 1373 | Idx = Builder.CreateIntCast(Idx, IntPtrTy, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1374 | IdxSigned, "idxprom"); |
Chris Lattner | a4d94ab | 2010-06-26 22:40:46 +0000 | [diff] [blame] | 1375 | |
Mike Stump | b14e62d | 2009-12-16 02:57:00 +0000 | [diff] [blame] | 1376 | // FIXME: As llvm implements the object size checking, this can come out. |
Mike Stump | 9c276ae | 2009-12-12 01:27:46 +0000 | [diff] [blame] | 1377 | if (CatchUndefined) { |
Chris Lattner | a4d94ab | 2010-06-26 22:40:46 +0000 | [diff] [blame] | 1378 | if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E->getBase())){ |
Mike Stump | 9c276ae | 2009-12-12 01:27:46 +0000 | [diff] [blame] | 1379 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr())) { |
| 1380 | if (ICE->getCastKind() == CastExpr::CK_ArrayToPointerDecay) { |
| 1381 | if (const ConstantArrayType *CAT |
| 1382 | = getContext().getAsConstantArrayType(DRE->getType())) { |
| 1383 | llvm::APInt Size = CAT->getSize(); |
| 1384 | llvm::BasicBlock *Cont = createBasicBlock("cont"); |
Mike Stump | 750c85e | 2009-12-14 22:14:31 +0000 | [diff] [blame] | 1385 | Builder.CreateCondBr(Builder.CreateICmpULE(Idx, |
Mike Stump | 9c276ae | 2009-12-12 01:27:46 +0000 | [diff] [blame] | 1386 | llvm::ConstantInt::get(Idx->getType(), Size)), |
Mike Stump | 15037ca | 2009-12-15 00:35:12 +0000 | [diff] [blame] | 1387 | Cont, getTrapBB()); |
Mike Stump | 96a063a | 2009-12-14 20:52:00 +0000 | [diff] [blame] | 1388 | EmitBlock(Cont); |
Mike Stump | 9c276ae | 2009-12-12 01:27:46 +0000 | [diff] [blame] | 1389 | } |
| 1390 | } |
| 1391 | } |
| 1392 | } |
| 1393 | } |
| 1394 | |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1395 | // We know that the pointer points to a type of the correct size, unless the |
| 1396 | // size is a VLA or Objective-C interface. |
Daniel Dunbar | 2a86625 | 2009-04-25 05:08:32 +0000 | [diff] [blame] | 1397 | llvm::Value *Address = 0; |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1398 | if (const VariableArrayType *VAT = |
Anders Carlsson | 8b33c08 | 2008-12-21 00:11:23 +0000 | [diff] [blame] | 1399 | getContext().getAsVariableArrayType(E->getType())) { |
Chris Lattner | 881eb9c | 2009-08-14 23:43:22 +0000 | [diff] [blame] | 1400 | llvm::Value *VLASize = GetVLASize(VAT); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1401 | |
Anders Carlsson | 8b33c08 | 2008-12-21 00:11:23 +0000 | [diff] [blame] | 1402 | Idx = Builder.CreateMul(Idx, VLASize); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1403 | |
Anders Carlsson | 6183a99 | 2008-12-21 03:44:36 +0000 | [diff] [blame] | 1404 | QualType BaseType = getContext().getBaseElementType(VAT); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1405 | |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 1406 | CharUnits BaseTypeSize = getContext().getTypeSizeInChars(BaseType); |
Anders Carlsson | 8b33c08 | 2008-12-21 00:11:23 +0000 | [diff] [blame] | 1407 | Idx = Builder.CreateUDiv(Idx, |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1408 | llvm::ConstantInt::get(Idx->getType(), |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 1409 | BaseTypeSize.getQuantity())); |
Chris Lattner | 9269d5c | 2010-06-26 23:03:20 +0000 | [diff] [blame] | 1410 | |
| 1411 | // The base must be a pointer, which is not an aggregate. Emit it. |
| 1412 | llvm::Value *Base = EmitScalarExpr(E->getBase()); |
| 1413 | |
Dan Gohman | 664f893 | 2009-08-12 00:33:55 +0000 | [diff] [blame] | 1414 | Address = Builder.CreateInBoundsGEP(Base, Idx, "arrayidx"); |
Chris Lattner | 9269d5c | 2010-06-26 23:03:20 +0000 | [diff] [blame] | 1415 | } else if (const ObjCObjectType *OIT = E->getType()->getAs<ObjCObjectType>()){ |
| 1416 | // Indexing over an interface, as in "NSString *P; P[4];" |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1417 | llvm::Value *InterfaceSize = |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1418 | llvm::ConstantInt::get(Idx->getType(), |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 1419 | getContext().getTypeSizeInChars(OIT).getQuantity()); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1420 | |
Daniel Dunbar | 2a86625 | 2009-04-25 05:08:32 +0000 | [diff] [blame] | 1421 | Idx = Builder.CreateMul(Idx, InterfaceSize); |
| 1422 | |
Benjamin Kramer | 3c0ef8c | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 1423 | const llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(VMContext); |
Chris Lattner | 9269d5c | 2010-06-26 23:03:20 +0000 | [diff] [blame] | 1424 | |
| 1425 | // The base must be a pointer, which is not an aggregate. Emit it. |
| 1426 | llvm::Value *Base = EmitScalarExpr(E->getBase()); |
Dan Gohman | 664f893 | 2009-08-12 00:33:55 +0000 | [diff] [blame] | 1427 | Address = Builder.CreateGEP(Builder.CreateBitCast(Base, i8PTy), |
Daniel Dunbar | 2a86625 | 2009-04-25 05:08:32 +0000 | [diff] [blame] | 1428 | Idx, "arrayidx"); |
| 1429 | Address = Builder.CreateBitCast(Address, Base->getType()); |
Chris Lattner | 9269d5c | 2010-06-26 23:03:20 +0000 | [diff] [blame] | 1430 | } else if (const Expr *Array = isSimpleArrayDecayOperand(E->getBase())) { |
| 1431 | // If this is A[i] where A is an array, the frontend will have decayed the |
| 1432 | // base to be a ArrayToPointerDecay implicit cast. While correct, it is |
| 1433 | // inefficient at -O0 to emit a "gep A, 0, 0" when codegen'ing it, then a |
| 1434 | // "gep x, i" here. Emit one "gep A, 0, i". |
| 1435 | assert(Array->getType()->isArrayType() && |
| 1436 | "Array to pointer decay must have array source type!"); |
| 1437 | llvm::Value *ArrayPtr = EmitLValue(Array).getAddress(); |
Chris Lattner | 9269d5c | 2010-06-26 23:03:20 +0000 | [diff] [blame] | 1438 | llvm::Value *Zero = llvm::ConstantInt::get(Int32Ty, 0); |
| 1439 | llvm::Value *Args[] = { Zero, Idx }; |
| 1440 | |
| 1441 | Address = Builder.CreateInBoundsGEP(ArrayPtr, Args, Args+2, "arrayidx"); |
Daniel Dunbar | 2a86625 | 2009-04-25 05:08:32 +0000 | [diff] [blame] | 1442 | } else { |
Chris Lattner | 9269d5c | 2010-06-26 23:03:20 +0000 | [diff] [blame] | 1443 | // The base must be a pointer, which is not an aggregate. Emit it. |
| 1444 | llvm::Value *Base = EmitScalarExpr(E->getBase()); |
Dan Gohman | 664f893 | 2009-08-12 00:33:55 +0000 | [diff] [blame] | 1445 | Address = Builder.CreateInBoundsGEP(Base, Idx, "arrayidx"); |
Anders Carlsson | 8b33c08 | 2008-12-21 00:11:23 +0000 | [diff] [blame] | 1446 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1447 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1448 | QualType T = E->getBase()->getType()->getPointeeType(); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1449 | assert(!T.isNull() && |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1450 | "CodeGenFunction::EmitArraySubscriptExpr(): Illegal base type"); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1451 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1452 | Qualifiers Quals = MakeQualifiers(T); |
| 1453 | Quals.setAddressSpace(E->getBase()->getType().getAddressSpace()); |
| 1454 | |
| 1455 | LValue LV = LValue::MakeAddr(Address, Quals); |
Fariborz Jahanian | 643887a | 2009-02-21 23:37:19 +0000 | [diff] [blame] | 1456 | if (getContext().getLangOptions().ObjC1 && |
Fariborz Jahanian | b123ea3 | 2009-09-16 21:37:16 +0000 | [diff] [blame] | 1457 | getContext().getLangOptions().getGCMode() != LangOptions::NonGC) { |
Fariborz Jahanian | 102e390 | 2009-06-01 21:29:32 +0000 | [diff] [blame] | 1458 | LValue::SetObjCNonGC(LV, !E->isOBJCGCCandidate(getContext())); |
Fariborz Jahanian | b123ea3 | 2009-09-16 21:37:16 +0000 | [diff] [blame] | 1459 | setObjCGCLValueClass(getContext(), E, LV); |
| 1460 | } |
Fariborz Jahanian | 643887a | 2009-02-21 23:37:19 +0000 | [diff] [blame] | 1461 | return LV; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1462 | } |
| 1463 | |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1464 | static |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1465 | llvm::Constant *GenerateConstantVector(llvm::LLVMContext &VMContext, |
| 1466 | llvm::SmallVector<unsigned, 4> &Elts) { |
Chris Lattner | 998eab1 | 2009-12-23 21:31:11 +0000 | [diff] [blame] | 1467 | llvm::SmallVector<llvm::Constant*, 4> CElts; |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1468 | |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 1469 | const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext); |
Nate Begeman | 3b8d116 | 2008-05-13 21:03:02 +0000 | [diff] [blame] | 1470 | for (unsigned i = 0, e = Elts.size(); i != e; ++i) |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 1471 | CElts.push_back(llvm::ConstantInt::get(Int32Ty, Elts[i])); |
Nate Begeman | 3b8d116 | 2008-05-13 21:03:02 +0000 | [diff] [blame] | 1472 | |
Owen Anderson | 4a28932 | 2009-07-28 21:22:35 +0000 | [diff] [blame] | 1473 | return llvm::ConstantVector::get(&CElts[0], CElts.size()); |
Nate Begeman | 3b8d116 | 2008-05-13 21:03:02 +0000 | [diff] [blame] | 1474 | } |
| 1475 | |
Chris Lattner | 349aaec | 2007-08-02 23:37:31 +0000 | [diff] [blame] | 1476 | LValue CodeGenFunction:: |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1477 | EmitExtVectorElementExpr(const ExtVectorElementExpr *E) { |
Chris Lattner | 349aaec | 2007-08-02 23:37:31 +0000 | [diff] [blame] | 1478 | // Emit the base vector as an l-value. |
Chris Lattner | 73525de | 2009-02-16 21:11:58 +0000 | [diff] [blame] | 1479 | LValue Base; |
| 1480 | |
| 1481 | // ExtVectorElementExpr's base can either be a vector or pointer to vector. |
Chris Lattner | 998eab1 | 2009-12-23 21:31:11 +0000 | [diff] [blame] | 1482 | if (E->isArrow()) { |
| 1483 | // If it is a pointer to a vector, emit the address and form an lvalue with |
| 1484 | // it. |
Chris Lattner | 2140e90 | 2009-02-16 22:14:05 +0000 | [diff] [blame] | 1485 | llvm::Value *Ptr = EmitScalarExpr(E->getBase()); |
Chris Lattner | 998eab1 | 2009-12-23 21:31:11 +0000 | [diff] [blame] | 1486 | const PointerType *PT = E->getBase()->getType()->getAs<PointerType>(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1487 | Qualifiers Quals = MakeQualifiers(PT->getPointeeType()); |
| 1488 | Quals.removeObjCGCAttr(); |
| 1489 | Base = LValue::MakeAddr(Ptr, Quals); |
Chris Lattner | 998eab1 | 2009-12-23 21:31:11 +0000 | [diff] [blame] | 1490 | } else if (E->getBase()->isLvalue(getContext()) == Expr::LV_Valid) { |
| 1491 | // Otherwise, if the base is an lvalue ( as in the case of foo.x.x), |
| 1492 | // emit the base as an lvalue. |
| 1493 | assert(E->getBase()->getType()->isVectorType()); |
| 1494 | Base = EmitLValue(E->getBase()); |
| 1495 | } else { |
| 1496 | // Otherwise, the base is a normal rvalue (as in (V+V).x), emit it as such. |
Daniel Dunbar | 302c3c2 | 2010-01-04 18:02:28 +0000 | [diff] [blame] | 1497 | assert(E->getBase()->getType()->getAs<VectorType>() && |
| 1498 | "Result must be a vector"); |
Chris Lattner | 998eab1 | 2009-12-23 21:31:11 +0000 | [diff] [blame] | 1499 | llvm::Value *Vec = EmitScalarExpr(E->getBase()); |
| 1500 | |
Chris Lattner | 0ad57fb | 2009-12-23 21:33:41 +0000 | [diff] [blame] | 1501 | // Store the vector to memory (because LValue wants an address). |
Daniel Dunbar | 195337d | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 1502 | llvm::Value *VecMem = CreateMemTemp(E->getBase()->getType()); |
Chris Lattner | 998eab1 | 2009-12-23 21:31:11 +0000 | [diff] [blame] | 1503 | Builder.CreateStore(Vec, VecMem); |
Chris Lattner | 0ad57fb | 2009-12-23 21:33:41 +0000 | [diff] [blame] | 1504 | Base = LValue::MakeAddr(VecMem, Qualifiers()); |
Chris Lattner | 998eab1 | 2009-12-23 21:31:11 +0000 | [diff] [blame] | 1505 | } |
| 1506 | |
Nate Begeman | 3b8d116 | 2008-05-13 21:03:02 +0000 | [diff] [blame] | 1507 | // Encode the element access list into a vector of unsigned indices. |
| 1508 | llvm::SmallVector<unsigned, 4> Indices; |
| 1509 | E->getEncodedElementAccess(Indices); |
| 1510 | |
| 1511 | if (Base.isSimple()) { |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1512 | llvm::Constant *CV = GenerateConstantVector(VMContext, Indices); |
Eli Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 1513 | return LValue::MakeExtVectorElt(Base.getAddress(), CV, |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1514 | Base.getVRQualifiers()); |
Nate Begeman | 3b8d116 | 2008-05-13 21:03:02 +0000 | [diff] [blame] | 1515 | } |
| 1516 | assert(Base.isExtVectorElt() && "Can only subscript lvalue vec elts here!"); |
| 1517 | |
| 1518 | llvm::Constant *BaseElts = Base.getExtVectorElts(); |
| 1519 | llvm::SmallVector<llvm::Constant *, 4> CElts; |
| 1520 | |
| 1521 | for (unsigned i = 0, e = Indices.size(); i != e; ++i) { |
| 1522 | if (isa<llvm::ConstantAggregateZero>(BaseElts)) |
Chris Lattner | 6766586 | 2009-10-28 05:12:07 +0000 | [diff] [blame] | 1523 | CElts.push_back(llvm::ConstantInt::get(Int32Ty, 0)); |
Nate Begeman | 3b8d116 | 2008-05-13 21:03:02 +0000 | [diff] [blame] | 1524 | else |
Chris Lattner | 6766586 | 2009-10-28 05:12:07 +0000 | [diff] [blame] | 1525 | CElts.push_back(cast<llvm::Constant>(BaseElts->getOperand(Indices[i]))); |
Nate Begeman | 3b8d116 | 2008-05-13 21:03:02 +0000 | [diff] [blame] | 1526 | } |
Owen Anderson | 4a28932 | 2009-07-28 21:22:35 +0000 | [diff] [blame] | 1527 | llvm::Constant *CV = llvm::ConstantVector::get(&CElts[0], CElts.size()); |
Eli Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 1528 | return LValue::MakeExtVectorElt(Base.getExtVectorAddr(), CV, |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1529 | Base.getVRQualifiers()); |
Chris Lattner | 349aaec | 2007-08-02 23:37:31 +0000 | [diff] [blame] | 1530 | } |
| 1531 | |
Devang Patel | b9b00ad | 2007-10-23 20:28:39 +0000 | [diff] [blame] | 1532 | LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) { |
Fariborz Jahanian | 4f676ed | 2009-02-21 00:30:43 +0000 | [diff] [blame] | 1533 | bool isNonGC = false; |
Devang Patel | 126a856 | 2007-10-24 22:26:28 +0000 | [diff] [blame] | 1534 | Expr *BaseExpr = E->getBase(); |
Devang Patel | 126a856 | 2007-10-24 22:26:28 +0000 | [diff] [blame] | 1535 | llvm::Value *BaseValue = NULL; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1536 | Qualifiers BaseQuals; |
Eli Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 1537 | |
Chris Lattner | 12f65f6 | 2007-12-02 18:52:07 +0000 | [diff] [blame] | 1538 | // If this is s.x, emit s as an lvalue. If it is s->x, emit s as a scalar. |
Devang Patel | fe2419a | 2007-12-11 21:33:16 +0000 | [diff] [blame] | 1539 | if (E->isArrow()) { |
Devang Patel | 0a96118 | 2007-10-26 18:15:21 +0000 | [diff] [blame] | 1540 | BaseValue = EmitScalarExpr(BaseExpr); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1541 | const PointerType *PTy = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1542 | BaseExpr->getType()->getAs<PointerType>(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1543 | BaseQuals = PTy->getPointeeType().getQualifiers(); |
Fariborz Jahanian | d2e1eb0 | 2009-09-01 17:02:21 +0000 | [diff] [blame] | 1544 | } else if (isa<ObjCPropertyRefExpr>(BaseExpr->IgnoreParens()) || |
| 1545 | isa<ObjCImplicitSetterGetterRefExpr>( |
| 1546 | BaseExpr->IgnoreParens())) { |
Fariborz Jahanian | 35c3329 | 2009-01-12 23:27:26 +0000 | [diff] [blame] | 1547 | RValue RV = EmitObjCPropertyGet(BaseExpr); |
| 1548 | BaseValue = RV.getAggregateAddr(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1549 | BaseQuals = BaseExpr->getType().getQualifiers(); |
Chris Lattner | 1bd885e | 2009-02-16 22:25:49 +0000 | [diff] [blame] | 1550 | } else { |
Chris Lattner | 12f65f6 | 2007-12-02 18:52:07 +0000 | [diff] [blame] | 1551 | LValue BaseLV = EmitLValue(BaseExpr); |
Fariborz Jahanian | 4f676ed | 2009-02-21 00:30:43 +0000 | [diff] [blame] | 1552 | if (BaseLV.isNonGC()) |
| 1553 | isNonGC = true; |
Chris Lattner | 12f65f6 | 2007-12-02 18:52:07 +0000 | [diff] [blame] | 1554 | // FIXME: this isn't right for bitfields. |
| 1555 | BaseValue = BaseLV.getAddress(); |
Fariborz Jahanian | a91d6a6 | 2009-07-29 00:44:13 +0000 | [diff] [blame] | 1556 | QualType BaseTy = BaseExpr->getType(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1557 | BaseQuals = BaseTy.getQualifiers(); |
Chris Lattner | 12f65f6 | 2007-12-02 18:52:07 +0000 | [diff] [blame] | 1558 | } |
Devang Patel | b9b00ad | 2007-10-23 20:28:39 +0000 | [diff] [blame] | 1559 | |
Anders Carlsson | ce53f7d | 2009-11-07 23:06:58 +0000 | [diff] [blame] | 1560 | NamedDecl *ND = E->getMemberDecl(); |
| 1561 | if (FieldDecl *Field = dyn_cast<FieldDecl>(ND)) { |
Anders Carlsson | e6d2a53 | 2010-01-29 05:05:36 +0000 | [diff] [blame] | 1562 | LValue LV = EmitLValueForField(BaseValue, Field, |
Anders Carlsson | ce53f7d | 2009-11-07 23:06:58 +0000 | [diff] [blame] | 1563 | BaseQuals.getCVRQualifiers()); |
| 1564 | LValue::SetObjCNonGC(LV, isNonGC); |
| 1565 | setObjCGCLValueClass(getContext(), E, LV); |
| 1566 | return LV; |
| 1567 | } |
| 1568 | |
Anders Carlsson | 589f9e3 | 2009-11-07 23:16:50 +0000 | [diff] [blame] | 1569 | if (VarDecl *VD = dyn_cast<VarDecl>(ND)) |
| 1570 | return EmitGlobalVarDeclLValue(*this, E, VD); |
Eli Friedman | 9a14630 | 2009-11-26 06:08:14 +0000 | [diff] [blame] | 1571 | |
| 1572 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) |
| 1573 | return EmitFunctionDeclLValue(*this, E, FD); |
| 1574 | |
Anders Carlsson | ce53f7d | 2009-11-07 23:06:58 +0000 | [diff] [blame] | 1575 | assert(false && "Unhandled member declaration!"); |
| 1576 | return LValue(); |
Eli Friedman | 472778e | 2008-02-09 08:50:58 +0000 | [diff] [blame] | 1577 | } |
Devang Patel | b9b00ad | 2007-10-23 20:28:39 +0000 | [diff] [blame] | 1578 | |
Fariborz Jahanian | fd64bb6 | 2008-12-15 20:35:07 +0000 | [diff] [blame] | 1579 | LValue CodeGenFunction::EmitLValueForBitfield(llvm::Value* BaseValue, |
Anders Carlsson | 0ed303c | 2009-11-17 03:57:07 +0000 | [diff] [blame] | 1580 | const FieldDecl* Field, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1581 | unsigned CVRQualifiers) { |
Daniel Dunbar | 198bcb4 | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 1582 | const CGRecordLayout &RL = |
| 1583 | CGM.getTypes().getCGRecordLayout(Field->getParent()); |
Daniel Dunbar | 2eec0b2 | 2010-04-05 16:20:44 +0000 | [diff] [blame] | 1584 | const CGBitFieldInfo &Info = RL.getBitFieldInfo(Field); |
Daniel Dunbar | 7f28964 | 2010-04-08 02:59:45 +0000 | [diff] [blame] | 1585 | return LValue::MakeBitfield(BaseValue, Info, |
Daniel Dunbar | f0fe5bc | 2010-04-05 21:36:35 +0000 | [diff] [blame] | 1586 | Field->getType().getCVRQualifiers()|CVRQualifiers); |
Fariborz Jahanian | fd64bb6 | 2008-12-15 20:35:07 +0000 | [diff] [blame] | 1587 | } |
| 1588 | |
John McCall | a9976d3 | 2010-05-21 01:18:57 +0000 | [diff] [blame] | 1589 | /// EmitLValueForAnonRecordField - Given that the field is a member of |
| 1590 | /// an anonymous struct or union buried inside a record, and given |
| 1591 | /// that the base value is a pointer to the enclosing record, derive |
| 1592 | /// an lvalue for the ultimate field. |
| 1593 | LValue CodeGenFunction::EmitLValueForAnonRecordField(llvm::Value *BaseValue, |
| 1594 | const FieldDecl *Field, |
| 1595 | unsigned CVRQualifiers) { |
| 1596 | llvm::SmallVector<const FieldDecl *, 8> Path; |
| 1597 | Path.push_back(Field); |
| 1598 | |
| 1599 | while (Field->getParent()->isAnonymousStructOrUnion()) { |
| 1600 | const ValueDecl *VD = Field->getParent()->getAnonymousStructOrUnionObject(); |
| 1601 | if (!isa<FieldDecl>(VD)) break; |
| 1602 | Field = cast<FieldDecl>(VD); |
| 1603 | Path.push_back(Field); |
| 1604 | } |
| 1605 | |
| 1606 | llvm::SmallVectorImpl<const FieldDecl*>::reverse_iterator |
| 1607 | I = Path.rbegin(), E = Path.rend(); |
| 1608 | while (true) { |
| 1609 | LValue LV = EmitLValueForField(BaseValue, *I, CVRQualifiers); |
| 1610 | if (++I == E) return LV; |
| 1611 | |
| 1612 | assert(LV.isSimple()); |
| 1613 | BaseValue = LV.getAddress(); |
| 1614 | CVRQualifiers |= LV.getVRQualifiers(); |
| 1615 | } |
| 1616 | } |
| 1617 | |
Eli Friedman | 472778e | 2008-02-09 08:50:58 +0000 | [diff] [blame] | 1618 | LValue CodeGenFunction::EmitLValueForField(llvm::Value* BaseValue, |
Anders Carlsson | 0ed303c | 2009-11-17 03:57:07 +0000 | [diff] [blame] | 1619 | const FieldDecl* Field, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1620 | unsigned CVRQualifiers) { |
Fariborz Jahanian | fd64bb6 | 2008-12-15 20:35:07 +0000 | [diff] [blame] | 1621 | if (Field->isBitField()) |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1622 | return EmitLValueForBitfield(BaseValue, Field, CVRQualifiers); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1623 | |
Daniel Dunbar | 198bcb4 | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 1624 | const CGRecordLayout &RL = |
| 1625 | CGM.getTypes().getCGRecordLayout(Field->getParent()); |
| 1626 | unsigned idx = RL.getLLVMFieldNo(Field); |
Fariborz Jahanian | fd64bb6 | 2008-12-15 20:35:07 +0000 | [diff] [blame] | 1627 | llvm::Value *V = Builder.CreateStructGEP(BaseValue, idx, "tmp"); |
Eli Friedman | 1e86b34 | 2008-05-29 11:33:25 +0000 | [diff] [blame] | 1628 | |
Devang Patel | abad06c | 2007-10-26 19:42:18 +0000 | [diff] [blame] | 1629 | // Match union field type. |
Anders Carlsson | e6d2a53 | 2010-01-29 05:05:36 +0000 | [diff] [blame] | 1630 | if (Field->getParent()->isUnion()) { |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1631 | const llvm::Type *FieldTy = |
Eli Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 1632 | CGM.getTypes().ConvertTypeForMem(Field->getType()); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1633 | const llvm::PointerType * BaseTy = |
Devang Patel | e9b8c0a | 2007-10-30 20:59:40 +0000 | [diff] [blame] | 1634 | cast<llvm::PointerType>(BaseValue->getType()); |
Eli Friedman | 788d571 | 2008-05-21 13:24:44 +0000 | [diff] [blame] | 1635 | unsigned AS = BaseTy->getAddressSpace(); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1636 | V = Builder.CreateBitCast(V, |
| 1637 | llvm::PointerType::get(FieldTy, AS), |
Eli Friedman | 788d571 | 2008-05-21 13:24:44 +0000 | [diff] [blame] | 1638 | "tmp"); |
Devang Patel | abad06c | 2007-10-26 19:42:18 +0000 | [diff] [blame] | 1639 | } |
Eli Friedman | 2be5861 | 2009-05-30 21:09:44 +0000 | [diff] [blame] | 1640 | if (Field->getType()->isReferenceType()) |
| 1641 | V = Builder.CreateLoad(V, "tmp"); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1642 | |
| 1643 | Qualifiers Quals = MakeQualifiers(Field->getType()); |
| 1644 | Quals.addCVRQualifiers(CVRQualifiers); |
Fariborz Jahanian | fd02ed7 | 2009-09-21 18:54:29 +0000 | [diff] [blame] | 1645 | // __weak attribute on a field is ignored. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1646 | if (Quals.getObjCGCAttr() == Qualifiers::Weak) |
| 1647 | Quals.removeObjCGCAttr(); |
Fariborz Jahanian | fd02ed7 | 2009-09-21 18:54:29 +0000 | [diff] [blame] | 1648 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1649 | return LValue::MakeAddr(V, Quals); |
Devang Patel | b9b00ad | 2007-10-23 20:28:39 +0000 | [diff] [blame] | 1650 | } |
| 1651 | |
Anders Carlsson | 06a2970 | 2010-01-29 05:24:29 +0000 | [diff] [blame] | 1652 | LValue |
| 1653 | CodeGenFunction::EmitLValueForFieldInitialization(llvm::Value* BaseValue, |
| 1654 | const FieldDecl* Field, |
| 1655 | unsigned CVRQualifiers) { |
| 1656 | QualType FieldType = Field->getType(); |
| 1657 | |
| 1658 | if (!FieldType->isReferenceType()) |
| 1659 | return EmitLValueForField(BaseValue, Field, CVRQualifiers); |
| 1660 | |
Daniel Dunbar | 198bcb4 | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 1661 | const CGRecordLayout &RL = |
| 1662 | CGM.getTypes().getCGRecordLayout(Field->getParent()); |
| 1663 | unsigned idx = RL.getLLVMFieldNo(Field); |
Anders Carlsson | 06a2970 | 2010-01-29 05:24:29 +0000 | [diff] [blame] | 1664 | llvm::Value *V = Builder.CreateStructGEP(BaseValue, idx, "tmp"); |
| 1665 | |
| 1666 | assert(!FieldType.getObjCGCAttr() && "fields cannot have GC attrs"); |
| 1667 | |
| 1668 | return LValue::MakeAddr(V, MakeQualifiers(FieldType)); |
| 1669 | } |
| 1670 | |
Chris Lattner | 75dfeda | 2009-03-18 18:28:57 +0000 | [diff] [blame] | 1671 | LValue CodeGenFunction::EmitCompoundLiteralLValue(const CompoundLiteralExpr* E){ |
Daniel Dunbar | 1500657 | 2010-02-16 19:43:39 +0000 | [diff] [blame] | 1672 | llvm::Value *DeclPtr = CreateMemTemp(E->getType(), ".compoundliteral"); |
Eli Friedman | 06e863f | 2008-05-13 23:18:27 +0000 | [diff] [blame] | 1673 | const Expr* InitExpr = E->getInitializer(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1674 | LValue Result = LValue::MakeAddr(DeclPtr, MakeQualifiers(E->getType())); |
Eli Friedman | 06e863f | 2008-05-13 23:18:27 +0000 | [diff] [blame] | 1675 | |
John McCall | 3d3ec1c | 2010-04-21 10:05:39 +0000 | [diff] [blame] | 1676 | EmitAnyExprToMem(InitExpr, DeclPtr, /*Volatile*/ false); |
Eli Friedman | 06e863f | 2008-05-13 23:18:27 +0000 | [diff] [blame] | 1677 | |
| 1678 | return Result; |
| 1679 | } |
| 1680 | |
Anders Carlsson | 6fcec8b | 2009-09-15 16:35:24 +0000 | [diff] [blame] | 1681 | LValue |
| 1682 | CodeGenFunction::EmitConditionalOperatorLValue(const ConditionalOperator* E) { |
| 1683 | if (E->isLvalue(getContext()) == Expr::LV_Valid) { |
Eli Friedman | ab18995 | 2009-12-25 05:29:40 +0000 | [diff] [blame] | 1684 | if (int Cond = ConstantFoldsToSimpleInteger(E->getCond())) { |
| 1685 | Expr *Live = Cond == 1 ? E->getLHS() : E->getRHS(); |
| 1686 | if (Live) |
| 1687 | return EmitLValue(Live); |
| 1688 | } |
| 1689 | |
| 1690 | if (!E->getLHS()) |
| 1691 | return EmitUnsupportedLValue(E, "conditional operator with missing LHS"); |
| 1692 | |
Anders Carlsson | 6fcec8b | 2009-09-15 16:35:24 +0000 | [diff] [blame] | 1693 | llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true"); |
| 1694 | llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false"); |
| 1695 | llvm::BasicBlock *ContBlock = createBasicBlock("cond.end"); |
| 1696 | |
Eli Friedman | 8e274bd | 2009-12-25 06:17:05 +0000 | [diff] [blame] | 1697 | EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock); |
Anders Carlsson | 6fcec8b | 2009-09-15 16:35:24 +0000 | [diff] [blame] | 1698 | |
Anders Carlsson | c1b32f6 | 2010-02-04 17:26:01 +0000 | [diff] [blame] | 1699 | // Any temporaries created here are conditional. |
| 1700 | BeginConditionalBranch(); |
Anders Carlsson | 6fcec8b | 2009-09-15 16:35:24 +0000 | [diff] [blame] | 1701 | EmitBlock(LHSBlock); |
Anders Carlsson | 6fcec8b | 2009-09-15 16:35:24 +0000 | [diff] [blame] | 1702 | LValue LHS = EmitLValue(E->getLHS()); |
Anders Carlsson | c1b32f6 | 2010-02-04 17:26:01 +0000 | [diff] [blame] | 1703 | EndConditionalBranch(); |
| 1704 | |
Anders Carlsson | 6fcec8b | 2009-09-15 16:35:24 +0000 | [diff] [blame] | 1705 | if (!LHS.isSimple()) |
| 1706 | return EmitUnsupportedLValue(E, "conditional operator"); |
| 1707 | |
Daniel Dunbar | 195337d | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 1708 | // FIXME: We shouldn't need an alloca for this. |
Chris Lattner | eb99b01 | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 1709 | llvm::Value *Temp = CreateTempAlloca(LHS.getAddress()->getType(),"condtmp"); |
Anders Carlsson | 6fcec8b | 2009-09-15 16:35:24 +0000 | [diff] [blame] | 1710 | Builder.CreateStore(LHS.getAddress(), Temp); |
| 1711 | EmitBranch(ContBlock); |
| 1712 | |
Anders Carlsson | c1b32f6 | 2010-02-04 17:26:01 +0000 | [diff] [blame] | 1713 | // Any temporaries created here are conditional. |
| 1714 | BeginConditionalBranch(); |
Anders Carlsson | 6fcec8b | 2009-09-15 16:35:24 +0000 | [diff] [blame] | 1715 | EmitBlock(RHSBlock); |
| 1716 | LValue RHS = EmitLValue(E->getRHS()); |
Anders Carlsson | c1b32f6 | 2010-02-04 17:26:01 +0000 | [diff] [blame] | 1717 | EndConditionalBranch(); |
Anders Carlsson | 6fcec8b | 2009-09-15 16:35:24 +0000 | [diff] [blame] | 1718 | if (!RHS.isSimple()) |
| 1719 | return EmitUnsupportedLValue(E, "conditional operator"); |
| 1720 | |
| 1721 | Builder.CreateStore(RHS.getAddress(), Temp); |
| 1722 | EmitBranch(ContBlock); |
| 1723 | |
| 1724 | EmitBlock(ContBlock); |
| 1725 | |
| 1726 | Temp = Builder.CreateLoad(Temp, "lv"); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1727 | return LValue::MakeAddr(Temp, MakeQualifiers(E->getType())); |
Anders Carlsson | 6fcec8b | 2009-09-15 16:35:24 +0000 | [diff] [blame] | 1728 | } |
| 1729 | |
Daniel Dunbar | 9034558 | 2009-03-24 02:38:23 +0000 | [diff] [blame] | 1730 | // ?: here should be an aggregate. |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1731 | assert((hasAggregateLLVMType(E->getType()) && |
Daniel Dunbar | 9034558 | 2009-03-24 02:38:23 +0000 | [diff] [blame] | 1732 | !E->getType()->isAnyComplexType()) && |
| 1733 | "Unexpected conditional operator!"); |
| 1734 | |
Daniel Dunbar | 18aba0d | 2010-02-05 19:38:31 +0000 | [diff] [blame] | 1735 | return EmitAggExprToLValue(E); |
Daniel Dunbar | 9034558 | 2009-03-24 02:38:23 +0000 | [diff] [blame] | 1736 | } |
| 1737 | |
Mike Stump | c849c05 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 1738 | /// EmitCastLValue - Casts are never lvalues unless that cast is a dynamic_cast. |
| 1739 | /// If the cast is a dynamic_cast, we can have the usual lvalue result, |
| 1740 | /// otherwise if a cast is needed by the code generator in an lvalue context, |
| 1741 | /// then it must mean that we need the address of an aggregate in order to |
| 1742 | /// access one of its fields. This can happen for all the reasons that casts |
| 1743 | /// are permitted with aggregate result, including noop aggregate casts, and |
| 1744 | /// cast from scalar to union. |
Chris Lattner | 75dfeda | 2009-03-18 18:28:57 +0000 | [diff] [blame] | 1745 | LValue CodeGenFunction::EmitCastLValue(const CastExpr *E) { |
Anders Carlsson | 0ee33cf | 2009-09-12 16:16:49 +0000 | [diff] [blame] | 1746 | switch (E->getCastKind()) { |
Douglas Gregor | 7c7a793 | 2010-07-15 18:58:16 +0000 | [diff] [blame] | 1747 | case CastExpr::CK_ToVoid: |
Eli Friedman | eaae78a | 2009-11-16 05:48:01 +0000 | [diff] [blame] | 1748 | return EmitUnsupportedLValue(E, "unexpected cast lvalue"); |
Douglas Gregor | 7c7a793 | 2010-07-15 18:58:16 +0000 | [diff] [blame] | 1749 | |
| 1750 | case CastExpr::CK_NoOp: |
| 1751 | if (E->getSubExpr()->Classify(getContext()).getKind() |
| 1752 | != Expr::Classification::CL_PRValue) { |
| 1753 | LValue LV = EmitLValue(E->getSubExpr()); |
| 1754 | if (LV.isPropertyRef()) { |
| 1755 | QualType QT = E->getSubExpr()->getType(); |
| 1756 | RValue RV = EmitLoadOfPropertyRefLValue(LV, QT); |
| 1757 | assert(!RV.isScalar() && "EmitCastLValue-scalar cast of property ref"); |
| 1758 | llvm::Value *V = RV.getAggregateAddr(); |
| 1759 | return LValue::MakeAddr(V, MakeQualifiers(QT)); |
| 1760 | } |
| 1761 | return LV; |
| 1762 | } |
| 1763 | // Fall through to synthesize a temporary. |
| 1764 | |
| 1765 | case CastExpr::CK_Unknown: |
| 1766 | case CastExpr::CK_BitCast: |
| 1767 | case CastExpr::CK_ArrayToPointerDecay: |
| 1768 | case CastExpr::CK_FunctionToPointerDecay: |
| 1769 | case CastExpr::CK_NullToMemberPointer: |
| 1770 | case CastExpr::CK_IntegralToPointer: |
| 1771 | case CastExpr::CK_PointerToIntegral: |
| 1772 | case CastExpr::CK_VectorSplat: |
| 1773 | case CastExpr::CK_IntegralCast: |
| 1774 | case CastExpr::CK_IntegralToFloating: |
| 1775 | case CastExpr::CK_FloatingToIntegral: |
| 1776 | case CastExpr::CK_FloatingCast: |
| 1777 | case CastExpr::CK_DerivedToBaseMemberPointer: |
| 1778 | case CastExpr::CK_BaseToDerivedMemberPointer: |
| 1779 | case CastExpr::CK_MemberPointerToBoolean: |
| 1780 | case CastExpr::CK_AnyPointerToBlockPointerCast: { |
| 1781 | // These casts only produce lvalues when we're binding a reference to a |
| 1782 | // temporary realized from a (converted) pure rvalue. Emit the expression |
| 1783 | // as a value, copy it into a temporary, and return an lvalue referring to |
| 1784 | // that temporary. |
| 1785 | llvm::Value *V = CreateMemTemp(E->getType(), "ref.temp"); |
| 1786 | EmitAnyExprToMem(E, V, false, false); |
| 1787 | return LValue::MakeAddr(V, MakeQualifiers(E->getType())); |
| 1788 | } |
Eli Friedman | eaae78a | 2009-11-16 05:48:01 +0000 | [diff] [blame] | 1789 | |
Mike Stump | c849c05 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 1790 | case CastExpr::CK_Dynamic: { |
| 1791 | LValue LV = EmitLValue(E->getSubExpr()); |
| 1792 | llvm::Value *V = LV.getAddress(); |
| 1793 | const CXXDynamicCastExpr *DCE = cast<CXXDynamicCastExpr>(E); |
| 1794 | return LValue::MakeAddr(EmitDynamicCast(V, DCE), |
| 1795 | MakeQualifiers(E->getType())); |
| 1796 | } |
| 1797 | |
Anders Carlsson | 0ee33cf | 2009-09-12 16:16:49 +0000 | [diff] [blame] | 1798 | case CastExpr::CK_ConstructorConversion: |
| 1799 | case CastExpr::CK_UserDefinedConversion: |
Fariborz Jahanian | a7fa7cd | 2009-12-15 21:34:52 +0000 | [diff] [blame] | 1800 | case CastExpr::CK_AnyPointerToObjCPointerCast: |
Chris Lattner | 75dfeda | 2009-03-18 18:28:57 +0000 | [diff] [blame] | 1801 | return EmitLValue(E->getSubExpr()); |
Anders Carlsson | 0ee33cf | 2009-09-12 16:16:49 +0000 | [diff] [blame] | 1802 | |
John McCall | 23cba80 | 2010-03-30 23:58:03 +0000 | [diff] [blame] | 1803 | case CastExpr::CK_UncheckedDerivedToBase: |
Anders Carlsson | 0ee33cf | 2009-09-12 16:16:49 +0000 | [diff] [blame] | 1804 | case CastExpr::CK_DerivedToBase: { |
| 1805 | const RecordType *DerivedClassTy = |
| 1806 | E->getSubExpr()->getType()->getAs<RecordType>(); |
| 1807 | CXXRecordDecl *DerivedClassDecl = |
| 1808 | cast<CXXRecordDecl>(DerivedClassTy->getDecl()); |
Anders Carlsson | 0ee33cf | 2009-09-12 16:16:49 +0000 | [diff] [blame] | 1809 | |
| 1810 | LValue LV = EmitLValue(E->getSubExpr()); |
Fariborz Jahanian | 353b33b | 2010-06-17 23:00:29 +0000 | [diff] [blame] | 1811 | llvm::Value *This; |
| 1812 | if (LV.isPropertyRef()) { |
| 1813 | RValue RV = EmitLoadOfPropertyRefLValue(LV, E->getSubExpr()->getType()); |
| 1814 | assert (!RV.isScalar() && "EmitCastLValue"); |
| 1815 | This = RV.getAggregateAddr(); |
| 1816 | } |
| 1817 | else |
| 1818 | This = LV.getAddress(); |
Anders Carlsson | 0ee33cf | 2009-09-12 16:16:49 +0000 | [diff] [blame] | 1819 | |
| 1820 | // Perform the derived-to-base conversion |
| 1821 | llvm::Value *Base = |
Fariborz Jahanian | 353b33b | 2010-06-17 23:00:29 +0000 | [diff] [blame] | 1822 | GetAddressOfBaseClass(This, DerivedClassDecl, |
Anders Carlsson | fc89c31 | 2010-04-24 21:12:55 +0000 | [diff] [blame] | 1823 | E->getBasePath(), /*NullCheckValue=*/false); |
Anders Carlsson | 0ee33cf | 2009-09-12 16:16:49 +0000 | [diff] [blame] | 1824 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1825 | return LValue::MakeAddr(Base, MakeQualifiers(E->getType())); |
Anders Carlsson | 0ee33cf | 2009-09-12 16:16:49 +0000 | [diff] [blame] | 1826 | } |
Daniel Dunbar | b2cd777 | 2010-02-05 20:02:42 +0000 | [diff] [blame] | 1827 | case CastExpr::CK_ToUnion: |
| 1828 | return EmitAggExprToLValue(E); |
Eli Friedman | eaae78a | 2009-11-16 05:48:01 +0000 | [diff] [blame] | 1829 | case CastExpr::CK_BaseToDerived: { |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 1830 | const RecordType *DerivedClassTy = E->getType()->getAs<RecordType>(); |
| 1831 | CXXRecordDecl *DerivedClassDecl = |
| 1832 | cast<CXXRecordDecl>(DerivedClassTy->getDecl()); |
| 1833 | |
| 1834 | LValue LV = EmitLValue(E->getSubExpr()); |
| 1835 | |
| 1836 | // Perform the base-to-derived conversion |
| 1837 | llvm::Value *Derived = |
Anders Carlsson | a04efdf | 2010-04-24 21:23:59 +0000 | [diff] [blame] | 1838 | GetAddressOfDerivedClass(LV.getAddress(), DerivedClassDecl, |
| 1839 | E->getBasePath(),/*NullCheckValue=*/false); |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 1840 | |
| 1841 | return LValue::MakeAddr(Derived, MakeQualifiers(E->getType())); |
Eli Friedman | eaae78a | 2009-11-16 05:48:01 +0000 | [diff] [blame] | 1842 | } |
Douglas Gregor | e39a389 | 2010-07-13 23:17:26 +0000 | [diff] [blame] | 1843 | case CastExpr::CK_LValueBitCast: { |
Eli Friedman | eaae78a | 2009-11-16 05:48:01 +0000 | [diff] [blame] | 1844 | // This must be a reinterpret_cast (or c-style equivalent). |
| 1845 | const ExplicitCastExpr *CE = cast<ExplicitCastExpr>(E); |
Anders Carlsson | 658e812 | 2009-11-14 21:21:42 +0000 | [diff] [blame] | 1846 | |
| 1847 | LValue LV = EmitLValue(E->getSubExpr()); |
| 1848 | llvm::Value *V = Builder.CreateBitCast(LV.getAddress(), |
| 1849 | ConvertType(CE->getTypeAsWritten())); |
| 1850 | return LValue::MakeAddr(V, MakeQualifiers(E->getType())); |
| 1851 | } |
Anders Carlsson | 0ee33cf | 2009-09-12 16:16:49 +0000 | [diff] [blame] | 1852 | } |
Douglas Gregor | 7c7a793 | 2010-07-15 18:58:16 +0000 | [diff] [blame] | 1853 | |
| 1854 | llvm_unreachable("Unhandled lvalue cast kind?"); |
Chris Lattner | 75dfeda | 2009-03-18 18:28:57 +0000 | [diff] [blame] | 1855 | } |
| 1856 | |
Fariborz Jahanian | 48620ba | 2009-10-20 23:29:04 +0000 | [diff] [blame] | 1857 | LValue CodeGenFunction::EmitNullInitializationLValue( |
Douglas Gregor | ed8abf1 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 1858 | const CXXScalarValueInitExpr *E) { |
Fariborz Jahanian | 48620ba | 2009-10-20 23:29:04 +0000 | [diff] [blame] | 1859 | QualType Ty = E->getType(); |
Daniel Dunbar | 195337d | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 1860 | LValue LV = LValue::MakeAddr(CreateMemTemp(Ty), MakeQualifiers(Ty)); |
Anders Carlsson | 1884eb0 | 2010-05-22 17:35:42 +0000 | [diff] [blame] | 1861 | EmitNullInitialization(LV.getAddress(), Ty); |
Daniel Dunbar | 195337d | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 1862 | return LV; |
Fariborz Jahanian | 48620ba | 2009-10-20 23:29:04 +0000 | [diff] [blame] | 1863 | } |
| 1864 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1865 | //===--------------------------------------------------------------------===// |
| 1866 | // Expression Emission |
| 1867 | //===--------------------------------------------------------------------===// |
| 1868 | |
Chris Lattner | 7016a70 | 2007-08-20 22:37:10 +0000 | [diff] [blame] | 1869 | |
Anders Carlsson | d2490a9 | 2009-12-24 20:40:36 +0000 | [diff] [blame] | 1870 | RValue CodeGenFunction::EmitCallExpr(const CallExpr *E, |
| 1871 | ReturnValueSlot ReturnValue) { |
Daniel Dunbar | c0ef9f5 | 2009-02-20 18:06:48 +0000 | [diff] [blame] | 1872 | // Builtins never have block type. |
Daniel Dunbar | ce1d38b | 2009-01-09 16:50:52 +0000 | [diff] [blame] | 1873 | if (E->getCallee()->getType()->isBlockPointerType()) |
Anders Carlsson | a1736c0 | 2009-12-24 21:13:40 +0000 | [diff] [blame] | 1874 | return EmitBlockCallExpr(E, ReturnValue); |
Daniel Dunbar | ce1d38b | 2009-01-09 16:50:52 +0000 | [diff] [blame] | 1875 | |
Anders Carlsson | 774e7c6 | 2009-04-03 22:50:24 +0000 | [diff] [blame] | 1876 | if (const CXXMemberCallExpr *CE = dyn_cast<CXXMemberCallExpr>(E)) |
Anders Carlsson | a1736c0 | 2009-12-24 21:13:40 +0000 | [diff] [blame] | 1877 | return EmitCXXMemberCallExpr(CE, ReturnValue); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1878 | |
Daniel Dunbar | c0ef9f5 | 2009-02-20 18:06:48 +0000 | [diff] [blame] | 1879 | const Decl *TargetDecl = 0; |
Daniel Dunbar | dd7b897 | 2009-02-20 19:34:33 +0000 | [diff] [blame] | 1880 | if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E->getCallee())) { |
| 1881 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CE->getSubExpr())) { |
| 1882 | TargetDecl = DRE->getDecl(); |
| 1883 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(TargetDecl)) |
Douglas Gregor | 7814e6d | 2009-09-12 00:22:50 +0000 | [diff] [blame] | 1884 | if (unsigned builtinID = FD->getBuiltinID()) |
Daniel Dunbar | dd7b897 | 2009-02-20 19:34:33 +0000 | [diff] [blame] | 1885 | return EmitBuiltinExpr(FD, builtinID, E); |
Daniel Dunbar | c0ef9f5 | 2009-02-20 18:06:48 +0000 | [diff] [blame] | 1886 | } |
| 1887 | } |
| 1888 | |
Chris Lattner | 5db7ae5 | 2009-06-13 00:26:38 +0000 | [diff] [blame] | 1889 | if (const CXXOperatorCallExpr *CE = dyn_cast<CXXOperatorCallExpr>(E)) |
Anders Carlsson | 0f29463 | 2009-05-27 04:18:27 +0000 | [diff] [blame] | 1890 | if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(TargetDecl)) |
Anders Carlsson | a1736c0 | 2009-12-24 21:13:40 +0000 | [diff] [blame] | 1891 | return EmitCXXOperatorMemberCallExpr(CE, MD, ReturnValue); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1892 | |
Eli Friedman | c4451db | 2009-12-08 02:09:46 +0000 | [diff] [blame] | 1893 | if (isa<CXXPseudoDestructorExpr>(E->getCallee()->IgnoreParens())) { |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 1894 | // C++ [expr.pseudo]p1: |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1895 | // The result shall only be used as the operand for the function call |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 1896 | // operator (), and the result of such a call has type void. The only |
| 1897 | // effect is the evaluation of the postfix-expression before the dot or |
| 1898 | // arrow. |
| 1899 | EmitScalarExpr(E->getCallee()); |
| 1900 | return RValue::get(0); |
| 1901 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1902 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1903 | llvm::Value *Callee = EmitScalarExpr(E->getCallee()); |
Anders Carlsson | d2490a9 | 2009-12-24 20:40:36 +0000 | [diff] [blame] | 1904 | return EmitCall(E->getCallee()->getType(), Callee, ReturnValue, |
Anders Carlsson | 9864771 | 2009-05-27 01:22:39 +0000 | [diff] [blame] | 1905 | E->arg_begin(), E->arg_end(), TargetDecl); |
Chris Lattner | c5e940f | 2007-08-31 04:44:06 +0000 | [diff] [blame] | 1906 | } |
| 1907 | |
Daniel Dunbar | 80e62c2 | 2008-09-04 03:20:13 +0000 | [diff] [blame] | 1908 | LValue CodeGenFunction::EmitBinaryOperatorLValue(const BinaryOperator *E) { |
Chris Lattner | 7a574cc | 2009-05-12 21:28:12 +0000 | [diff] [blame] | 1909 | // Comma expressions just emit their LHS then their RHS as an l-value. |
| 1910 | if (E->getOpcode() == BinaryOperator::Comma) { |
| 1911 | EmitAnyExpr(E->getLHS()); |
Eli Friedman | 130c69e | 2009-12-07 20:18:11 +0000 | [diff] [blame] | 1912 | EnsureInsertPoint(); |
Chris Lattner | 7a574cc | 2009-05-12 21:28:12 +0000 | [diff] [blame] | 1913 | return EmitLValue(E->getRHS()); |
| 1914 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1915 | |
Fariborz Jahanian | 52f08bc | 2009-10-26 21:58:25 +0000 | [diff] [blame] | 1916 | if (E->getOpcode() == BinaryOperator::PtrMemD || |
| 1917 | E->getOpcode() == BinaryOperator::PtrMemI) |
Fariborz Jahanian | 8bfd31f | 2009-10-22 22:57:31 +0000 | [diff] [blame] | 1918 | return EmitPointerToDataMemberBinaryExpr(E); |
| 1919 | |
Daniel Dunbar | 80e62c2 | 2008-09-04 03:20:13 +0000 | [diff] [blame] | 1920 | // Can only get l-value for binary operator expressions which are a |
| 1921 | // simple assignment of aggregate type. |
| 1922 | if (E->getOpcode() != BinaryOperator::Assign) |
| 1923 | return EmitUnsupportedLValue(E, "binary l-value expression"); |
| 1924 | |
Anders Carlsson | 86aa0cd | 2009-10-19 18:28:22 +0000 | [diff] [blame] | 1925 | if (!hasAggregateLLVMType(E->getType())) { |
| 1926 | // Emit the LHS as an l-value. |
| 1927 | LValue LV = EmitLValue(E->getLHS()); |
| 1928 | |
| 1929 | llvm::Value *RHS = EmitScalarExpr(E->getRHS()); |
| 1930 | EmitStoreOfScalar(RHS, LV.getAddress(), LV.isVolatileQualified(), |
| 1931 | E->getType()); |
| 1932 | return LV; |
| 1933 | } |
| 1934 | |
Daniel Dunbar | 18aba0d | 2010-02-05 19:38:31 +0000 | [diff] [blame] | 1935 | return EmitAggExprToLValue(E); |
Daniel Dunbar | 80e62c2 | 2008-09-04 03:20:13 +0000 | [diff] [blame] | 1936 | } |
| 1937 | |
Christopher Lamb | 22c940e | 2007-12-29 05:02:41 +0000 | [diff] [blame] | 1938 | LValue CodeGenFunction::EmitCallExprLValue(const CallExpr *E) { |
Christopher Lamb | 22c940e | 2007-12-29 05:02:41 +0000 | [diff] [blame] | 1939 | RValue RV = EmitCallExpr(E); |
Anders Carlsson | 4826568 | 2009-05-27 01:45:47 +0000 | [diff] [blame] | 1940 | |
Chris Lattner | eb99b01 | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 1941 | if (!RV.isScalar()) |
| 1942 | return LValue::MakeAddr(RV.getAggregateAddr(),MakeQualifiers(E->getType())); |
| 1943 | |
| 1944 | assert(E->getCallReturnType()->isReferenceType() && |
| 1945 | "Can't have a scalar return unless the return type is a " |
| 1946 | "reference type!"); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1947 | |
Chris Lattner | eb99b01 | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 1948 | return LValue::MakeAddr(RV.getScalarVal(), MakeQualifiers(E->getType())); |
Christopher Lamb | 22c940e | 2007-12-29 05:02:41 +0000 | [diff] [blame] | 1949 | } |
| 1950 | |
Daniel Dunbar | 5b5c9ef | 2009-02-11 20:59:32 +0000 | [diff] [blame] | 1951 | LValue CodeGenFunction::EmitVAArgExprLValue(const VAArgExpr *E) { |
| 1952 | // FIXME: This shouldn't require another copy. |
Daniel Dunbar | 18aba0d | 2010-02-05 19:38:31 +0000 | [diff] [blame] | 1953 | return EmitAggExprToLValue(E); |
Daniel Dunbar | 5b5c9ef | 2009-02-11 20:59:32 +0000 | [diff] [blame] | 1954 | } |
| 1955 | |
Anders Carlsson | b58d017 | 2009-05-30 23:23:33 +0000 | [diff] [blame] | 1956 | LValue CodeGenFunction::EmitCXXConstructLValue(const CXXConstructExpr *E) { |
Daniel Dunbar | 195337d | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 1957 | llvm::Value *Temp = CreateMemTemp(E->getType(), "tmp"); |
Anders Carlsson | b58d017 | 2009-05-30 23:23:33 +0000 | [diff] [blame] | 1958 | EmitCXXConstructExpr(Temp, E); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1959 | return LValue::MakeAddr(Temp, MakeQualifiers(E->getType())); |
Anders Carlsson | b58d017 | 2009-05-30 23:23:33 +0000 | [diff] [blame] | 1960 | } |
| 1961 | |
Anders Carlsson | e61c9e8 | 2009-05-30 23:30:54 +0000 | [diff] [blame] | 1962 | LValue |
Mike Stump | c2e84ae | 2009-11-15 08:09:41 +0000 | [diff] [blame] | 1963 | CodeGenFunction::EmitCXXTypeidLValue(const CXXTypeidExpr *E) { |
| 1964 | llvm::Value *Temp = EmitCXXTypeidExpr(E); |
| 1965 | return LValue::MakeAddr(Temp, MakeQualifiers(E->getType())); |
| 1966 | } |
| 1967 | |
| 1968 | LValue |
Anders Carlsson | e61c9e8 | 2009-05-30 23:30:54 +0000 | [diff] [blame] | 1969 | CodeGenFunction::EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr *E) { |
| 1970 | LValue LV = EmitLValue(E->getSubExpr()); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1971 | EmitCXXTemporary(E->getTemporary(), LV.getAddress()); |
Anders Carlsson | e61c9e8 | 2009-05-30 23:30:54 +0000 | [diff] [blame] | 1972 | return LV; |
| 1973 | } |
| 1974 | |
Daniel Dunbar | 0a04d77 | 2008-08-23 10:51:21 +0000 | [diff] [blame] | 1975 | LValue CodeGenFunction::EmitObjCMessageExprLValue(const ObjCMessageExpr *E) { |
Daniel Dunbar | 0a04d77 | 2008-08-23 10:51:21 +0000 | [diff] [blame] | 1976 | RValue RV = EmitObjCMessageExpr(E); |
Anders Carlsson | 7e70fb2 | 2010-06-21 20:59:55 +0000 | [diff] [blame] | 1977 | |
| 1978 | if (!RV.isScalar()) |
| 1979 | return LValue::MakeAddr(RV.getAggregateAddr(), |
| 1980 | MakeQualifiers(E->getType())); |
| 1981 | |
| 1982 | assert(E->getMethodDecl()->getResultType()->isReferenceType() && |
| 1983 | "Can't have a scalar return unless the return type is a " |
| 1984 | "reference type!"); |
| 1985 | |
| 1986 | return LValue::MakeAddr(RV.getScalarVal(), MakeQualifiers(E->getType())); |
Daniel Dunbar | 0a04d77 | 2008-08-23 10:51:21 +0000 | [diff] [blame] | 1987 | } |
| 1988 | |
Fariborz Jahanian | 03b2960 | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 1989 | LValue CodeGenFunction::EmitObjCSelectorLValue(const ObjCSelectorExpr *E) { |
| 1990 | llvm::Value *V = |
| 1991 | CGM.getObjCRuntime().GetSelector(Builder, E->getSelector(), true); |
| 1992 | return LValue::MakeAddr(V, MakeQualifiers(E->getType())); |
| 1993 | } |
| 1994 | |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 1995 | llvm::Value *CodeGenFunction::EmitIvarOffset(const ObjCInterfaceDecl *Interface, |
Daniel Dunbar | 29e0bcc | 2008-09-24 04:00:38 +0000 | [diff] [blame] | 1996 | const ObjCIvarDecl *Ivar) { |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1997 | return CGM.getObjCRuntime().EmitIvarOffset(*this, Interface, Ivar); |
Daniel Dunbar | 29e0bcc | 2008-09-24 04:00:38 +0000 | [diff] [blame] | 1998 | } |
| 1999 | |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 2000 | LValue CodeGenFunction::EmitLValueForIvar(QualType ObjectTy, |
| 2001 | llvm::Value *BaseValue, |
Daniel Dunbar | 29e0bcc | 2008-09-24 04:00:38 +0000 | [diff] [blame] | 2002 | const ObjCIvarDecl *Ivar, |
| 2003 | unsigned CVRQualifiers) { |
Chris Lattner | 26f074b | 2009-04-17 17:44:48 +0000 | [diff] [blame] | 2004 | return CGM.getObjCRuntime().EmitObjCValueForIvar(*this, ObjectTy, BaseValue, |
Daniel Dunbar | 525c9b7 | 2009-04-21 01:19:28 +0000 | [diff] [blame] | 2005 | Ivar, CVRQualifiers); |
Daniel Dunbar | 29e0bcc | 2008-09-24 04:00:38 +0000 | [diff] [blame] | 2006 | } |
| 2007 | |
| 2008 | LValue CodeGenFunction::EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E) { |
Anders Carlsson | 29b7e50 | 2008-08-25 01:53:23 +0000 | [diff] [blame] | 2009 | // FIXME: A lot of the code below could be shared with EmitMemberExpr. |
| 2010 | llvm::Value *BaseValue = 0; |
| 2011 | const Expr *BaseExpr = E->getBase(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2012 | Qualifiers BaseQuals; |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 2013 | QualType ObjectTy; |
Anders Carlsson | 29b7e50 | 2008-08-25 01:53:23 +0000 | [diff] [blame] | 2014 | if (E->isArrow()) { |
| 2015 | BaseValue = EmitScalarExpr(BaseExpr); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2016 | ObjectTy = BaseExpr->getType()->getPointeeType(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2017 | BaseQuals = ObjectTy.getQualifiers(); |
Anders Carlsson | 29b7e50 | 2008-08-25 01:53:23 +0000 | [diff] [blame] | 2018 | } else { |
| 2019 | LValue BaseLV = EmitLValue(BaseExpr); |
| 2020 | // FIXME: this isn't right for bitfields. |
| 2021 | BaseValue = BaseLV.getAddress(); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 2022 | ObjectTy = BaseExpr->getType(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2023 | BaseQuals = ObjectTy.getQualifiers(); |
Anders Carlsson | 29b7e50 | 2008-08-25 01:53:23 +0000 | [diff] [blame] | 2024 | } |
Daniel Dunbar | 29e0bcc | 2008-09-24 04:00:38 +0000 | [diff] [blame] | 2025 | |
Fariborz Jahanian | dbf3cfd | 2009-09-16 23:11:23 +0000 | [diff] [blame] | 2026 | LValue LV = |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2027 | EmitLValueForIvar(ObjectTy, BaseValue, E->getDecl(), |
| 2028 | BaseQuals.getCVRQualifiers()); |
Fariborz Jahanian | dbf3cfd | 2009-09-16 23:11:23 +0000 | [diff] [blame] | 2029 | setObjCGCLValueClass(getContext(), E, LV); |
| 2030 | return LV; |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 2031 | } |
| 2032 | |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2033 | LValue |
Daniel Dunbar | 85c59ed | 2008-08-29 08:11:39 +0000 | [diff] [blame] | 2034 | CodeGenFunction::EmitObjCPropertyRefLValue(const ObjCPropertyRefExpr *E) { |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2035 | // This is a special l-value that just issues sends when we load or store |
| 2036 | // through it. |
Daniel Dunbar | 85c59ed | 2008-08-29 08:11:39 +0000 | [diff] [blame] | 2037 | return LValue::MakePropertyRef(E, E->getType().getCVRQualifiers()); |
| 2038 | } |
| 2039 | |
Chris Lattner | eb99b01 | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 2040 | LValue CodeGenFunction::EmitObjCKVCRefLValue( |
Fariborz Jahanian | 09105f5 | 2009-08-20 17:02:02 +0000 | [diff] [blame] | 2041 | const ObjCImplicitSetterGetterRefExpr *E) { |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2042 | // This is a special l-value that just issues sends when we load or store |
| 2043 | // through it. |
Fariborz Jahanian | 43f4470 | 2008-11-22 22:30:21 +0000 | [diff] [blame] | 2044 | return LValue::MakeKVCRef(E, E->getType().getCVRQualifiers()); |
| 2045 | } |
| 2046 | |
Chris Lattner | eb99b01 | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 2047 | LValue CodeGenFunction::EmitObjCSuperExprLValue(const ObjCSuperExpr *E) { |
Douglas Gregor | cd9b46e | 2008-11-04 14:56:14 +0000 | [diff] [blame] | 2048 | return EmitUnsupportedLValue(E, "use of super"); |
| 2049 | } |
| 2050 | |
Chris Lattner | 6545994 | 2009-04-25 19:35:26 +0000 | [diff] [blame] | 2051 | LValue CodeGenFunction::EmitStmtExprLValue(const StmtExpr *E) { |
Chris Lattner | 6545994 | 2009-04-25 19:35:26 +0000 | [diff] [blame] | 2052 | // Can only get l-value for message expression returning aggregate type |
| 2053 | RValue RV = EmitAnyExprToTemp(E); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2054 | return LValue::MakeAddr(RV.getAggregateAddr(), MakeQualifiers(E->getType())); |
Chris Lattner | 6545994 | 2009-04-25 19:35:26 +0000 | [diff] [blame] | 2055 | } |
| 2056 | |
Anders Carlsson | 31777a2 | 2009-12-24 19:08:58 +0000 | [diff] [blame] | 2057 | RValue CodeGenFunction::EmitCall(QualType CalleeType, llvm::Value *Callee, |
Anders Carlsson | d2490a9 | 2009-12-24 20:40:36 +0000 | [diff] [blame] | 2058 | ReturnValueSlot ReturnValue, |
Anders Carlsson | 9864771 | 2009-05-27 01:22:39 +0000 | [diff] [blame] | 2059 | CallExpr::const_arg_iterator ArgBeg, |
| 2060 | CallExpr::const_arg_iterator ArgEnd, |
| 2061 | const Decl *TargetDecl) { |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2062 | // Get the actual function type. The callee type will always be a pointer to |
| 2063 | // function type or a block pointer type. |
| 2064 | assert(CalleeType->isFunctionPointerType() && |
Anders Carlsson | 8ac67a7 | 2009-04-07 18:53:02 +0000 | [diff] [blame] | 2065 | "Call must have function pointer type!"); |
| 2066 | |
John McCall | 00a1ad9 | 2009-10-23 08:22:42 +0000 | [diff] [blame] | 2067 | CalleeType = getContext().getCanonicalType(CalleeType); |
| 2068 | |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2069 | const FunctionType *FnType |
| 2070 | = cast<FunctionType>(cast<PointerType>(CalleeType)->getPointeeType()); |
| 2071 | QualType ResultType = FnType->getResultType(); |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 2072 | |
| 2073 | CallArgList Args; |
John McCall | 00a1ad9 | 2009-10-23 08:22:42 +0000 | [diff] [blame] | 2074 | EmitCallArgs(Args, dyn_cast<FunctionProtoType>(FnType), ArgBeg, ArgEnd); |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 2075 | |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2076 | return EmitCall(CGM.getTypes().getFunctionInfo(Args, FnType), |
Anders Carlsson | d2490a9 | 2009-12-24 20:40:36 +0000 | [diff] [blame] | 2077 | Callee, ReturnValue, Args, TargetDecl); |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 2078 | } |
Fariborz Jahanian | 8bfd31f | 2009-10-22 22:57:31 +0000 | [diff] [blame] | 2079 | |
Chris Lattner | eb99b01 | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 2080 | LValue CodeGenFunction:: |
| 2081 | EmitPointerToDataMemberBinaryExpr(const BinaryOperator *E) { |
Eli Friedman | 1c5c1a0 | 2009-11-18 05:01:17 +0000 | [diff] [blame] | 2082 | llvm::Value *BaseV; |
Fariborz Jahanian | 52f08bc | 2009-10-26 21:58:25 +0000 | [diff] [blame] | 2083 | if (E->getOpcode() == BinaryOperator::PtrMemI) |
Eli Friedman | 1c5c1a0 | 2009-11-18 05:01:17 +0000 | [diff] [blame] | 2084 | BaseV = EmitScalarExpr(E->getLHS()); |
| 2085 | else |
| 2086 | BaseV = EmitLValue(E->getLHS()).getAddress(); |
Fariborz Jahanian | 8bfd31f | 2009-10-22 22:57:31 +0000 | [diff] [blame] | 2087 | const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(getLLVMContext()); |
| 2088 | BaseV = Builder.CreateBitCast(BaseV, i8Ty); |
Eli Friedman | 1c5c1a0 | 2009-11-18 05:01:17 +0000 | [diff] [blame] | 2089 | llvm::Value *OffsetV = EmitScalarExpr(E->getRHS()); |
Fariborz Jahanian | 8bfd31f | 2009-10-22 22:57:31 +0000 | [diff] [blame] | 2090 | llvm::Value *AddV = Builder.CreateInBoundsGEP(BaseV, OffsetV, "add.ptr"); |
Chris Lattner | eb99b01 | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 2091 | |
Fariborz Jahanian | 8bfd31f | 2009-10-22 22:57:31 +0000 | [diff] [blame] | 2092 | QualType Ty = E->getRHS()->getType(); |
Chris Lattner | eb99b01 | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 2093 | Ty = Ty->getAs<MemberPointerType>()->getPointeeType(); |
| 2094 | |
| 2095 | const llvm::Type *PType = ConvertType(getContext().getPointerType(Ty)); |
Fariborz Jahanian | 8bfd31f | 2009-10-22 22:57:31 +0000 | [diff] [blame] | 2096 | AddV = Builder.CreateBitCast(AddV, PType); |
Chris Lattner | eb99b01 | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 2097 | return LValue::MakeAddr(AddV, MakeQualifiers(Ty)); |
Fariborz Jahanian | 8bfd31f | 2009-10-22 22:57:31 +0000 | [diff] [blame] | 2098 | } |
| 2099 | |