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