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