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