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