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