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