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