CGExpr.cpp revision 0bc7049d636f63b4b0e9ee0a2edf37541f5e3bee
1//===--- CGExpr.cpp - Emit LLVM Code from Expressions ---------------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 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" 16#include "CGCall.h" 17#include "CGObjCRuntime.h" 18#include "clang/AST/ASTContext.h" 19#include "clang/AST/DeclObjC.h" 20#include "llvm/Target/TargetData.h" 21using namespace clang; 22using namespace CodeGen; 23 24//===--------------------------------------------------------------------===// 25// Miscellaneous Helper Methods 26//===--------------------------------------------------------------------===// 27 28/// CreateTempAlloca - This creates a alloca and inserts it into the entry 29/// block. 30llvm::AllocaInst *CodeGenFunction::CreateTempAlloca(const llvm::Type *Ty, 31 const llvm::Twine &Name) { 32 if (!Builder.isNamePreserving()) 33 return new llvm::AllocaInst(Ty, 0, "", AllocaInsertPt); 34 return new llvm::AllocaInst(Ty, 0, Name, AllocaInsertPt); 35} 36 37/// EvaluateExprAsBool - Perform the usual unary conversions on the specified 38/// expression and compare the result against zero, returning an Int1Ty value. 39llvm::Value *CodeGenFunction::EvaluateExprAsBool(const Expr *E) { 40 QualType BoolTy = getContext().BoolTy; 41 if (!E->getType()->isAnyComplexType()) 42 return EmitScalarConversion(EmitScalarExpr(E), E->getType(), BoolTy); 43 44 return EmitComplexToScalarConversion(EmitComplexExpr(E), E->getType(),BoolTy); 45} 46 47/// EmitAnyExpr - Emit code to compute the specified expression which can have 48/// any type. The result is returned as an RValue struct. If this is an 49/// aggregate expression, the aggloc/agglocvolatile arguments indicate where the 50/// result should be returned. 51RValue CodeGenFunction::EmitAnyExpr(const Expr *E, llvm::Value *AggLoc, 52 bool IsAggLocVolatile, bool IgnoreResult, 53 bool IsInitializer) { 54 if (!hasAggregateLLVMType(E->getType())) 55 return RValue::get(EmitScalarExpr(E, IgnoreResult)); 56 else if (E->getType()->isAnyComplexType()) 57 return RValue::getComplex(EmitComplexExpr(E, false, false, 58 IgnoreResult, IgnoreResult)); 59 60 EmitAggExpr(E, AggLoc, IsAggLocVolatile, IgnoreResult, IsInitializer); 61 return RValue::getAggregate(AggLoc, IsAggLocVolatile); 62} 63 64/// EmitAnyExprToTemp - Similary to EmitAnyExpr(), however, the result will 65/// always be accessible even if no aggregate location is provided. 66RValue CodeGenFunction::EmitAnyExprToTemp(const Expr *E, 67 bool IsAggLocVolatile, 68 bool IsInitializer) { 69 llvm::Value *AggLoc = 0; 70 71 if (hasAggregateLLVMType(E->getType()) && 72 !E->getType()->isAnyComplexType()) 73 AggLoc = CreateTempAlloca(ConvertType(E->getType()), "agg.tmp"); 74 return EmitAnyExpr(E, AggLoc, IsAggLocVolatile, /*IgnoreResult=*/false, 75 IsInitializer); 76} 77 78RValue CodeGenFunction::EmitReferenceBindingToExpr(const Expr* E, 79 QualType DestType, 80 bool IsInitializer) { 81 bool ShouldDestroyTemporaries = false; 82 unsigned OldNumLiveTemporaries = 0; 83 84 if (const CXXExprWithTemporaries *TE = dyn_cast<CXXExprWithTemporaries>(E)) { 85 ShouldDestroyTemporaries = TE->shouldDestroyTemporaries(); 86 87 // Keep track of the current cleanup stack depth. 88 if (ShouldDestroyTemporaries) 89 OldNumLiveTemporaries = LiveTemporaries.size(); 90 91 E = TE->getSubExpr(); 92 } 93 94 RValue Val; 95 if (E->isLvalue(getContext()) == Expr::LV_Valid) { 96 // Emit the expr as an lvalue. 97 LValue LV = EmitLValue(E); 98 if (LV.isSimple()) 99 return RValue::get(LV.getAddress()); 100 Val = EmitLoadOfLValue(LV, E->getType()); 101 102 if (ShouldDestroyTemporaries) { 103 // Pop temporaries. 104 while (LiveTemporaries.size() > OldNumLiveTemporaries) 105 PopCXXTemporary(); 106 } 107 } else { 108 const CXXRecordDecl *BaseClassDecl = 0; 109 const CXXRecordDecl *DerivedClassDecl = 0; 110 111 if (const CastExpr *CE = 112 dyn_cast<CastExpr>(E->IgnoreParenNoopCasts(getContext()))) { 113 if (CE->getCastKind() == CastExpr::CK_DerivedToBase) { 114 E = CE->getSubExpr(); 115 116 BaseClassDecl = 117 cast<CXXRecordDecl>(CE->getType()->getAs<RecordType>()->getDecl()); 118 DerivedClassDecl = 119 cast<CXXRecordDecl>(E->getType()->getAs<RecordType>()->getDecl()); 120 } 121 } 122 123 Val = EmitAnyExprToTemp(E, /*IsAggLocVolatile=*/false, 124 IsInitializer); 125 126 if (ShouldDestroyTemporaries) { 127 // Pop temporaries. 128 while (LiveTemporaries.size() > OldNumLiveTemporaries) 129 PopCXXTemporary(); 130 } 131 132 if (IsInitializer) { 133 // We might have to destroy the temporary variable. 134 if (const RecordType *RT = E->getType()->getAs<RecordType>()) { 135 if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) { 136 if (!ClassDecl->hasTrivialDestructor()) { 137 const CXXDestructorDecl *Dtor = 138 ClassDecl->getDestructor(getContext()); 139 140 CleanupScope scope(*this); 141 EmitCXXDestructorCall(Dtor, Dtor_Complete, Val.getAggregateAddr()); 142 } 143 } 144 } 145 } 146 147 // Check if need to perform the derived-to-base cast. 148 if (BaseClassDecl) { 149 llvm::Value *Derived = Val.getAggregateAddr(); 150 llvm::Value *Base = 151 GetAddressCXXOfBaseClass(Derived, DerivedClassDecl, BaseClassDecl, 152 /*NullCheckValue=*/false); 153 return RValue::get(Base); 154 } 155 } 156 157 if (Val.isAggregate()) { 158 Val = RValue::get(Val.getAggregateAddr()); 159 } else { 160 // Create a temporary variable that we can bind the reference to. 161 llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(E->getType()), 162 "reftmp"); 163 if (Val.isScalar()) 164 EmitStoreOfScalar(Val.getScalarVal(), Temp, false, E->getType()); 165 else 166 StoreComplexToAddr(Val.getComplexVal(), Temp, false); 167 Val = RValue::get(Temp); 168 } 169 170 return Val; 171} 172 173 174/// getAccessedFieldNo - Given an encoded value and a result number, return the 175/// input field number being accessed. 176unsigned CodeGenFunction::getAccessedFieldNo(unsigned Idx, 177 const llvm::Constant *Elts) { 178 if (isa<llvm::ConstantAggregateZero>(Elts)) 179 return 0; 180 181 return cast<llvm::ConstantInt>(Elts->getOperand(Idx))->getZExtValue(); 182} 183 184 185//===----------------------------------------------------------------------===// 186// LValue Expression Emission 187//===----------------------------------------------------------------------===// 188 189RValue CodeGenFunction::GetUndefRValue(QualType Ty) { 190 if (Ty->isVoidType()) 191 return RValue::get(0); 192 193 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { 194 const llvm::Type *EltTy = ConvertType(CTy->getElementType()); 195 llvm::Value *U = llvm::UndefValue::get(EltTy); 196 return RValue::getComplex(std::make_pair(U, U)); 197 } 198 199 if (hasAggregateLLVMType(Ty)) { 200 const llvm::Type *LTy = llvm::PointerType::getUnqual(ConvertType(Ty)); 201 return RValue::getAggregate(llvm::UndefValue::get(LTy)); 202 } 203 204 return RValue::get(llvm::UndefValue::get(ConvertType(Ty))); 205} 206 207RValue CodeGenFunction::EmitUnsupportedRValue(const Expr *E, 208 const char *Name) { 209 ErrorUnsupported(E, Name); 210 return GetUndefRValue(E->getType()); 211} 212 213LValue CodeGenFunction::EmitUnsupportedLValue(const Expr *E, 214 const char *Name) { 215 ErrorUnsupported(E, Name); 216 llvm::Type *Ty = llvm::PointerType::getUnqual(ConvertType(E->getType())); 217 return LValue::MakeAddr(llvm::UndefValue::get(Ty), 218 MakeQualifiers(E->getType())); 219} 220 221/// EmitLValue - Emit code to compute a designator that specifies the location 222/// of the expression. 223/// 224/// This can return one of two things: a simple address or a bitfield reference. 225/// In either case, the LLVM Value* in the LValue structure is guaranteed to be 226/// an LLVM pointer type. 227/// 228/// If this returns a bitfield reference, nothing about the pointee type of the 229/// LLVM value is known: For example, it may not be a pointer to an integer. 230/// 231/// If this returns a normal address, and if the lvalue's C type is fixed size, 232/// this method guarantees that the returned pointer type will point to an LLVM 233/// type of the same size of the lvalue's type. If the lvalue has a variable 234/// length type, this is not possible. 235/// 236LValue CodeGenFunction::EmitLValue(const Expr *E) { 237 switch (E->getStmtClass()) { 238 default: return EmitUnsupportedLValue(E, "l-value expression"); 239 240 case Expr::BinaryOperatorClass: 241 return EmitBinaryOperatorLValue(cast<BinaryOperator>(E)); 242 case Expr::CallExprClass: 243 case Expr::CXXMemberCallExprClass: 244 case Expr::CXXOperatorCallExprClass: 245 return EmitCallExprLValue(cast<CallExpr>(E)); 246 case Expr::VAArgExprClass: 247 return EmitVAArgExprLValue(cast<VAArgExpr>(E)); 248 case Expr::DeclRefExprClass: 249 return EmitDeclRefLValue(cast<DeclRefExpr>(E)); 250 case Expr::ParenExprClass:return EmitLValue(cast<ParenExpr>(E)->getSubExpr()); 251 case Expr::PredefinedExprClass: 252 return EmitPredefinedLValue(cast<PredefinedExpr>(E)); 253 case Expr::StringLiteralClass: 254 return EmitStringLiteralLValue(cast<StringLiteral>(E)); 255 case Expr::ObjCEncodeExprClass: 256 return EmitObjCEncodeExprLValue(cast<ObjCEncodeExpr>(E)); 257 258 case Expr::BlockDeclRefExprClass: 259 return EmitBlockDeclRefLValue(cast<BlockDeclRefExpr>(E)); 260 261 case Expr::CXXConditionDeclExprClass: 262 return EmitCXXConditionDeclLValue(cast<CXXConditionDeclExpr>(E)); 263 case Expr::CXXTemporaryObjectExprClass: 264 case Expr::CXXConstructExprClass: 265 return EmitCXXConstructLValue(cast<CXXConstructExpr>(E)); 266 case Expr::CXXBindTemporaryExprClass: 267 return EmitCXXBindTemporaryLValue(cast<CXXBindTemporaryExpr>(E)); 268 case Expr::CXXExprWithTemporariesClass: 269 return EmitCXXExprWithTemporariesLValue(cast<CXXExprWithTemporaries>(E)); 270 271 case Expr::ObjCMessageExprClass: 272 return EmitObjCMessageExprLValue(cast<ObjCMessageExpr>(E)); 273 case Expr::ObjCIvarRefExprClass: 274 return EmitObjCIvarRefLValue(cast<ObjCIvarRefExpr>(E)); 275 case Expr::ObjCPropertyRefExprClass: 276 return EmitObjCPropertyRefLValue(cast<ObjCPropertyRefExpr>(E)); 277 case Expr::ObjCImplicitSetterGetterRefExprClass: 278 return EmitObjCKVCRefLValue(cast<ObjCImplicitSetterGetterRefExpr>(E)); 279 case Expr::ObjCSuperExprClass: 280 return EmitObjCSuperExprLValue(cast<ObjCSuperExpr>(E)); 281 282 case Expr::StmtExprClass: 283 return EmitStmtExprLValue(cast<StmtExpr>(E)); 284 case Expr::UnaryOperatorClass: 285 return EmitUnaryOpLValue(cast<UnaryOperator>(E)); 286 case Expr::ArraySubscriptExprClass: 287 return EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E)); 288 case Expr::ExtVectorElementExprClass: 289 return EmitExtVectorElementExpr(cast<ExtVectorElementExpr>(E)); 290 case Expr::MemberExprClass: 291 return EmitMemberExpr(cast<MemberExpr>(E)); 292 case Expr::CompoundLiteralExprClass: 293 return EmitCompoundLiteralLValue(cast<CompoundLiteralExpr>(E)); 294 case Expr::ConditionalOperatorClass: 295 return EmitConditionalOperatorLValue(cast<ConditionalOperator>(E)); 296 case Expr::ChooseExprClass: 297 return EmitLValue(cast<ChooseExpr>(E)->getChosenSubExpr(getContext())); 298 case Expr::ImplicitCastExprClass: 299 case Expr::CStyleCastExprClass: 300 case Expr::CXXFunctionalCastExprClass: 301 case Expr::CXXStaticCastExprClass: 302 case Expr::CXXDynamicCastExprClass: 303 case Expr::CXXReinterpretCastExprClass: 304 case Expr::CXXConstCastExprClass: 305 return EmitCastLValue(cast<CastExpr>(E)); 306 case Expr::CXXZeroInitValueExprClass: 307 return EmitNullInitializationLValue(cast<CXXZeroInitValueExpr>(E)); 308 } 309} 310 311llvm::Value *CodeGenFunction::EmitLoadOfScalar(llvm::Value *Addr, bool Volatile, 312 QualType Ty) { 313 llvm::Value *V = Builder.CreateLoad(Addr, Volatile, "tmp"); 314 315 // Bool can have different representation in memory than in registers. 316 if (Ty->isBooleanType()) 317 if (V->getType() != llvm::Type::getInt1Ty(VMContext)) 318 V = Builder.CreateTrunc(V, llvm::Type::getInt1Ty(VMContext), "tobool"); 319 320 return V; 321} 322 323void CodeGenFunction::EmitStoreOfScalar(llvm::Value *Value, llvm::Value *Addr, 324 bool Volatile, QualType Ty) { 325 326 if (Ty->isBooleanType()) { 327 // Bool can have different representation in memory than in registers. 328 const llvm::Type *SrcTy = Value->getType(); 329 const llvm::PointerType *DstPtr = cast<llvm::PointerType>(Addr->getType()); 330 if (DstPtr->getElementType() != SrcTy) { 331 const llvm::Type *MemTy = 332 llvm::PointerType::get(SrcTy, DstPtr->getAddressSpace()); 333 Addr = Builder.CreateBitCast(Addr, MemTy, "storetmp"); 334 } 335 } 336 Builder.CreateStore(Value, Addr, Volatile); 337} 338 339/// EmitLoadOfLValue - Given an expression that represents a value lvalue, this 340/// method emits the address of the lvalue, then loads the result as an rvalue, 341/// returning the rvalue. 342RValue CodeGenFunction::EmitLoadOfLValue(LValue LV, QualType ExprType) { 343 if (LV.isObjCWeak()) { 344 // load of a __weak object. 345 llvm::Value *AddrWeakObj = LV.getAddress(); 346 return RValue::get(CGM.getObjCRuntime().EmitObjCWeakRead(*this, 347 AddrWeakObj)); 348 } 349 350 if (LV.isSimple()) { 351 llvm::Value *Ptr = LV.getAddress(); 352 const llvm::Type *EltTy = 353 cast<llvm::PointerType>(Ptr->getType())->getElementType(); 354 355 // Simple scalar l-value. 356 if (EltTy->isSingleValueType()) 357 return RValue::get(EmitLoadOfScalar(Ptr, LV.isVolatileQualified(), 358 ExprType)); 359 360 assert(ExprType->isFunctionType() && "Unknown scalar value"); 361 return RValue::get(Ptr); 362 } 363 364 if (LV.isVectorElt()) { 365 llvm::Value *Vec = Builder.CreateLoad(LV.getVectorAddr(), 366 LV.isVolatileQualified(), "tmp"); 367 return RValue::get(Builder.CreateExtractElement(Vec, LV.getVectorIdx(), 368 "vecext")); 369 } 370 371 // If this is a reference to a subset of the elements of a vector, either 372 // shuffle the input or extract/insert them as appropriate. 373 if (LV.isExtVectorElt()) 374 return EmitLoadOfExtVectorElementLValue(LV, ExprType); 375 376 if (LV.isBitfield()) 377 return EmitLoadOfBitfieldLValue(LV, ExprType); 378 379 if (LV.isPropertyRef()) 380 return EmitLoadOfPropertyRefLValue(LV, ExprType); 381 382 assert(LV.isKVCRef() && "Unknown LValue type!"); 383 return EmitLoadOfKVCRefLValue(LV, ExprType); 384} 385 386RValue CodeGenFunction::EmitLoadOfBitfieldLValue(LValue LV, 387 QualType ExprType) { 388 unsigned StartBit = LV.getBitfieldStartBit(); 389 unsigned BitfieldSize = LV.getBitfieldSize(); 390 llvm::Value *Ptr = LV.getBitfieldAddr(); 391 392 const llvm::Type *EltTy = 393 cast<llvm::PointerType>(Ptr->getType())->getElementType(); 394 unsigned EltTySize = CGM.getTargetData().getTypeSizeInBits(EltTy); 395 396 // In some cases the bitfield may straddle two memory locations. Currently we 397 // load the entire bitfield, then do the magic to sign-extend it if 398 // necessary. This results in somewhat more code than necessary for the common 399 // case (one load), since two shifts accomplish both the masking and sign 400 // extension. 401 unsigned LowBits = std::min(BitfieldSize, EltTySize - StartBit); 402 llvm::Value *Val = Builder.CreateLoad(Ptr, LV.isVolatileQualified(), "tmp"); 403 404 // Shift to proper location. 405 if (StartBit) 406 Val = Builder.CreateLShr(Val, llvm::ConstantInt::get(EltTy, StartBit), 407 "bf.lo"); 408 409 // Mask off unused bits. 410 llvm::Constant *LowMask = llvm::ConstantInt::get(VMContext, 411 llvm::APInt::getLowBitsSet(EltTySize, LowBits)); 412 Val = Builder.CreateAnd(Val, LowMask, "bf.lo.cleared"); 413 414 // Fetch the high bits if necessary. 415 if (LowBits < BitfieldSize) { 416 unsigned HighBits = BitfieldSize - LowBits; 417 llvm::Value *HighPtr = Builder.CreateGEP(Ptr, llvm::ConstantInt::get( 418 llvm::Type::getInt32Ty(VMContext), 1), "bf.ptr.hi"); 419 llvm::Value *HighVal = Builder.CreateLoad(HighPtr, 420 LV.isVolatileQualified(), 421 "tmp"); 422 423 // Mask off unused bits. 424 llvm::Constant *HighMask = llvm::ConstantInt::get(VMContext, 425 llvm::APInt::getLowBitsSet(EltTySize, HighBits)); 426 HighVal = Builder.CreateAnd(HighVal, HighMask, "bf.lo.cleared"); 427 428 // Shift to proper location and or in to bitfield value. 429 HighVal = Builder.CreateShl(HighVal, 430 llvm::ConstantInt::get(EltTy, LowBits)); 431 Val = Builder.CreateOr(Val, HighVal, "bf.val"); 432 } 433 434 // Sign extend if necessary. 435 if (LV.isBitfieldSigned()) { 436 llvm::Value *ExtraBits = llvm::ConstantInt::get(EltTy, 437 EltTySize - BitfieldSize); 438 Val = Builder.CreateAShr(Builder.CreateShl(Val, ExtraBits), 439 ExtraBits, "bf.val.sext"); 440 } 441 442 // The bitfield type and the normal type differ when the storage sizes differ 443 // (currently just _Bool). 444 Val = Builder.CreateIntCast(Val, ConvertType(ExprType), false, "tmp"); 445 446 return RValue::get(Val); 447} 448 449RValue CodeGenFunction::EmitLoadOfPropertyRefLValue(LValue LV, 450 QualType ExprType) { 451 return EmitObjCPropertyGet(LV.getPropertyRefExpr()); 452} 453 454RValue CodeGenFunction::EmitLoadOfKVCRefLValue(LValue LV, 455 QualType ExprType) { 456 return EmitObjCPropertyGet(LV.getKVCRefExpr()); 457} 458 459// If this is a reference to a subset of the elements of a vector, create an 460// appropriate shufflevector. 461RValue CodeGenFunction::EmitLoadOfExtVectorElementLValue(LValue LV, 462 QualType ExprType) { 463 llvm::Value *Vec = Builder.CreateLoad(LV.getExtVectorAddr(), 464 LV.isVolatileQualified(), "tmp"); 465 466 const llvm::Constant *Elts = LV.getExtVectorElts(); 467 468 // If the result of the expression is a non-vector type, we must be extracting 469 // a single element. Just codegen as an extractelement. 470 const VectorType *ExprVT = ExprType->getAs<VectorType>(); 471 if (!ExprVT) { 472 unsigned InIdx = getAccessedFieldNo(0, Elts); 473 llvm::Value *Elt = llvm::ConstantInt::get( 474 llvm::Type::getInt32Ty(VMContext), InIdx); 475 return RValue::get(Builder.CreateExtractElement(Vec, Elt, "tmp")); 476 } 477 478 // Always use shuffle vector to try to retain the original program structure 479 unsigned NumResultElts = ExprVT->getNumElements(); 480 481 llvm::SmallVector<llvm::Constant*, 4> Mask; 482 for (unsigned i = 0; i != NumResultElts; ++i) { 483 unsigned InIdx = getAccessedFieldNo(i, Elts); 484 Mask.push_back(llvm::ConstantInt::get( 485 llvm::Type::getInt32Ty(VMContext), InIdx)); 486 } 487 488 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size()); 489 Vec = Builder.CreateShuffleVector(Vec, 490 llvm::UndefValue::get(Vec->getType()), 491 MaskV, "tmp"); 492 return RValue::get(Vec); 493} 494 495 496 497/// EmitStoreThroughLValue - Store the specified rvalue into the specified 498/// lvalue, where both are guaranteed to the have the same type, and that type 499/// is 'Ty'. 500void CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst, 501 QualType Ty) { 502 if (!Dst.isSimple()) { 503 if (Dst.isVectorElt()) { 504 // Read/modify/write the vector, inserting the new element. 505 llvm::Value *Vec = Builder.CreateLoad(Dst.getVectorAddr(), 506 Dst.isVolatileQualified(), "tmp"); 507 Vec = Builder.CreateInsertElement(Vec, Src.getScalarVal(), 508 Dst.getVectorIdx(), "vecins"); 509 Builder.CreateStore(Vec, Dst.getVectorAddr(),Dst.isVolatileQualified()); 510 return; 511 } 512 513 // If this is an update of extended vector elements, insert them as 514 // appropriate. 515 if (Dst.isExtVectorElt()) 516 return EmitStoreThroughExtVectorComponentLValue(Src, Dst, Ty); 517 518 if (Dst.isBitfield()) 519 return EmitStoreThroughBitfieldLValue(Src, Dst, Ty); 520 521 if (Dst.isPropertyRef()) 522 return EmitStoreThroughPropertyRefLValue(Src, Dst, Ty); 523 524 assert(Dst.isKVCRef() && "Unknown LValue type"); 525 return EmitStoreThroughKVCRefLValue(Src, Dst, Ty); 526 } 527 528 if (Dst.isObjCWeak() && !Dst.isNonGC()) { 529 // load of a __weak object. 530 llvm::Value *LvalueDst = Dst.getAddress(); 531 llvm::Value *src = Src.getScalarVal(); 532 CGM.getObjCRuntime().EmitObjCWeakAssign(*this, src, LvalueDst); 533 return; 534 } 535 536 if (Dst.isObjCStrong() && !Dst.isNonGC()) { 537 // load of a __strong object. 538 llvm::Value *LvalueDst = Dst.getAddress(); 539 llvm::Value *src = Src.getScalarVal(); 540 if (Dst.isObjCIvar()) { 541 assert(Dst.getBaseIvarExp() && "BaseIvarExp is NULL"); 542 const llvm::Type *ResultType = ConvertType(getContext().LongTy); 543 llvm::Value *RHS = EmitScalarExpr(Dst.getBaseIvarExp()); 544 llvm::Value *dst = RHS; 545 RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast"); 546 llvm::Value *LHS = 547 Builder.CreatePtrToInt(LvalueDst, ResultType, "sub.ptr.lhs.cast"); 548 llvm::Value *BytesBetween = Builder.CreateSub(LHS, RHS, "ivar.offset"); 549 CGM.getObjCRuntime().EmitObjCIvarAssign(*this, src, dst, 550 BytesBetween); 551 } else if (Dst.isGlobalObjCRef()) 552 CGM.getObjCRuntime().EmitObjCGlobalAssign(*this, src, LvalueDst); 553 else 554 CGM.getObjCRuntime().EmitObjCStrongCastAssign(*this, src, LvalueDst); 555 return; 556 } 557 558 assert(Src.isScalar() && "Can't emit an agg store with this method"); 559 EmitStoreOfScalar(Src.getScalarVal(), Dst.getAddress(), 560 Dst.isVolatileQualified(), Ty); 561} 562 563void CodeGenFunction::EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst, 564 QualType Ty, 565 llvm::Value **Result) { 566 unsigned StartBit = Dst.getBitfieldStartBit(); 567 unsigned BitfieldSize = Dst.getBitfieldSize(); 568 llvm::Value *Ptr = Dst.getBitfieldAddr(); 569 570 const llvm::Type *EltTy = 571 cast<llvm::PointerType>(Ptr->getType())->getElementType(); 572 unsigned EltTySize = CGM.getTargetData().getTypeSizeInBits(EltTy); 573 574 // Get the new value, cast to the appropriate type and masked to exactly the 575 // size of the bit-field. 576 llvm::Value *SrcVal = Src.getScalarVal(); 577 llvm::Value *NewVal = Builder.CreateIntCast(SrcVal, EltTy, false, "tmp"); 578 llvm::Constant *Mask = llvm::ConstantInt::get(VMContext, 579 llvm::APInt::getLowBitsSet(EltTySize, BitfieldSize)); 580 NewVal = Builder.CreateAnd(NewVal, Mask, "bf.value"); 581 582 // Return the new value of the bit-field, if requested. 583 if (Result) { 584 // Cast back to the proper type for result. 585 const llvm::Type *SrcTy = SrcVal->getType(); 586 llvm::Value *SrcTrunc = Builder.CreateIntCast(NewVal, SrcTy, false, 587 "bf.reload.val"); 588 589 // Sign extend if necessary. 590 if (Dst.isBitfieldSigned()) { 591 unsigned SrcTySize = CGM.getTargetData().getTypeSizeInBits(SrcTy); 592 llvm::Value *ExtraBits = llvm::ConstantInt::get(SrcTy, 593 SrcTySize - BitfieldSize); 594 SrcTrunc = Builder.CreateAShr(Builder.CreateShl(SrcTrunc, ExtraBits), 595 ExtraBits, "bf.reload.sext"); 596 } 597 598 *Result = SrcTrunc; 599 } 600 601 // In some cases the bitfield may straddle two memory locations. Emit the low 602 // part first and check to see if the high needs to be done. 603 unsigned LowBits = std::min(BitfieldSize, EltTySize - StartBit); 604 llvm::Value *LowVal = Builder.CreateLoad(Ptr, Dst.isVolatileQualified(), 605 "bf.prev.low"); 606 607 // Compute the mask for zero-ing the low part of this bitfield. 608 llvm::Constant *InvMask = 609 llvm::ConstantInt::get(VMContext, 610 ~llvm::APInt::getBitsSet(EltTySize, StartBit, StartBit + LowBits)); 611 612 // Compute the new low part as 613 // LowVal = (LowVal & InvMask) | (NewVal << StartBit), 614 // with the shift of NewVal implicitly stripping the high bits. 615 llvm::Value *NewLowVal = 616 Builder.CreateShl(NewVal, llvm::ConstantInt::get(EltTy, StartBit), 617 "bf.value.lo"); 618 LowVal = Builder.CreateAnd(LowVal, InvMask, "bf.prev.lo.cleared"); 619 LowVal = Builder.CreateOr(LowVal, NewLowVal, "bf.new.lo"); 620 621 // Write back. 622 Builder.CreateStore(LowVal, Ptr, Dst.isVolatileQualified()); 623 624 // If the low part doesn't cover the bitfield emit a high part. 625 if (LowBits < BitfieldSize) { 626 unsigned HighBits = BitfieldSize - LowBits; 627 llvm::Value *HighPtr = Builder.CreateGEP(Ptr, llvm::ConstantInt::get( 628 llvm::Type::getInt32Ty(VMContext), 1), "bf.ptr.hi"); 629 llvm::Value *HighVal = Builder.CreateLoad(HighPtr, 630 Dst.isVolatileQualified(), 631 "bf.prev.hi"); 632 633 // Compute the mask for zero-ing the high part of this bitfield. 634 llvm::Constant *InvMask = 635 llvm::ConstantInt::get(VMContext, ~llvm::APInt::getLowBitsSet(EltTySize, 636 HighBits)); 637 638 // Compute the new high part as 639 // HighVal = (HighVal & InvMask) | (NewVal lshr LowBits), 640 // where the high bits of NewVal have already been cleared and the 641 // shift stripping the low bits. 642 llvm::Value *NewHighVal = 643 Builder.CreateLShr(NewVal, llvm::ConstantInt::get(EltTy, LowBits), 644 "bf.value.high"); 645 HighVal = Builder.CreateAnd(HighVal, InvMask, "bf.prev.hi.cleared"); 646 HighVal = Builder.CreateOr(HighVal, NewHighVal, "bf.new.hi"); 647 648 // Write back. 649 Builder.CreateStore(HighVal, HighPtr, Dst.isVolatileQualified()); 650 } 651} 652 653void CodeGenFunction::EmitStoreThroughPropertyRefLValue(RValue Src, 654 LValue Dst, 655 QualType Ty) { 656 EmitObjCPropertySet(Dst.getPropertyRefExpr(), Src); 657} 658 659void CodeGenFunction::EmitStoreThroughKVCRefLValue(RValue Src, 660 LValue Dst, 661 QualType Ty) { 662 EmitObjCPropertySet(Dst.getKVCRefExpr(), Src); 663} 664 665void CodeGenFunction::EmitStoreThroughExtVectorComponentLValue(RValue Src, 666 LValue Dst, 667 QualType Ty) { 668 // This access turns into a read/modify/write of the vector. Load the input 669 // value now. 670 llvm::Value *Vec = Builder.CreateLoad(Dst.getExtVectorAddr(), 671 Dst.isVolatileQualified(), "tmp"); 672 const llvm::Constant *Elts = Dst.getExtVectorElts(); 673 674 llvm::Value *SrcVal = Src.getScalarVal(); 675 676 if (const VectorType *VTy = Ty->getAs<VectorType>()) { 677 unsigned NumSrcElts = VTy->getNumElements(); 678 unsigned NumDstElts = 679 cast<llvm::VectorType>(Vec->getType())->getNumElements(); 680 if (NumDstElts == NumSrcElts) { 681 // Use shuffle vector is the src and destination are the same number of 682 // elements and restore the vector mask since it is on the side it will be 683 // stored. 684 llvm::SmallVector<llvm::Constant*, 4> Mask(NumDstElts); 685 for (unsigned i = 0; i != NumSrcElts; ++i) { 686 unsigned InIdx = getAccessedFieldNo(i, Elts); 687 Mask[InIdx] = llvm::ConstantInt::get( 688 llvm::Type::getInt32Ty(VMContext), i); 689 } 690 691 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size()); 692 Vec = Builder.CreateShuffleVector(SrcVal, 693 llvm::UndefValue::get(Vec->getType()), 694 MaskV, "tmp"); 695 } else if (NumDstElts > NumSrcElts) { 696 // Extended the source vector to the same length and then shuffle it 697 // into the destination. 698 // FIXME: since we're shuffling with undef, can we just use the indices 699 // into that? This could be simpler. 700 llvm::SmallVector<llvm::Constant*, 4> ExtMask; 701 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext); 702 unsigned i; 703 for (i = 0; i != NumSrcElts; ++i) 704 ExtMask.push_back(llvm::ConstantInt::get(Int32Ty, i)); 705 for (; i != NumDstElts; ++i) 706 ExtMask.push_back(llvm::UndefValue::get(Int32Ty)); 707 llvm::Value *ExtMaskV = llvm::ConstantVector::get(&ExtMask[0], 708 ExtMask.size()); 709 llvm::Value *ExtSrcVal = 710 Builder.CreateShuffleVector(SrcVal, 711 llvm::UndefValue::get(SrcVal->getType()), 712 ExtMaskV, "tmp"); 713 // build identity 714 llvm::SmallVector<llvm::Constant*, 4> Mask; 715 for (unsigned i = 0; i != NumDstElts; ++i) 716 Mask.push_back(llvm::ConstantInt::get(Int32Ty, i)); 717 718 // modify when what gets shuffled in 719 for (unsigned i = 0; i != NumSrcElts; ++i) { 720 unsigned Idx = getAccessedFieldNo(i, Elts); 721 Mask[Idx] = llvm::ConstantInt::get(Int32Ty, i+NumDstElts); 722 } 723 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size()); 724 Vec = Builder.CreateShuffleVector(Vec, ExtSrcVal, MaskV, "tmp"); 725 } else { 726 // We should never shorten the vector 727 assert(0 && "unexpected shorten vector length"); 728 } 729 } else { 730 // If the Src is a scalar (not a vector) it must be updating one element. 731 unsigned InIdx = getAccessedFieldNo(0, Elts); 732 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext); 733 llvm::Value *Elt = llvm::ConstantInt::get(Int32Ty, InIdx); 734 Vec = Builder.CreateInsertElement(Vec, SrcVal, Elt, "tmp"); 735 } 736 737 Builder.CreateStore(Vec, Dst.getExtVectorAddr(), Dst.isVolatileQualified()); 738} 739 740// setObjCGCLValueClass - sets class of he lvalue for the purpose of 741// generating write-barries API. It is currently a global, ivar, 742// or neither. 743static void setObjCGCLValueClass(const ASTContext &Ctx, const Expr *E, 744 LValue &LV) { 745 if (Ctx.getLangOptions().getGCMode() == LangOptions::NonGC) 746 return; 747 748 if (isa<ObjCIvarRefExpr>(E)) { 749 LV.SetObjCIvar(LV, true); 750 ObjCIvarRefExpr *Exp = cast<ObjCIvarRefExpr>(const_cast<Expr*>(E)); 751 LV.setBaseIvarExp(Exp->getBase()); 752 LV.SetObjCArray(LV, E->getType()->isArrayType()); 753 return; 754 } 755 756 if (const DeclRefExpr *Exp = dyn_cast<DeclRefExpr>(E)) { 757 if (const VarDecl *VD = dyn_cast<VarDecl>(Exp->getDecl())) { 758 if ((VD->isBlockVarDecl() && !VD->hasLocalStorage()) || 759 VD->isFileVarDecl()) 760 LV.SetGlobalObjCRef(LV, true); 761 } 762 LV.SetObjCArray(LV, E->getType()->isArrayType()); 763 return; 764 } 765 766 if (const UnaryOperator *Exp = dyn_cast<UnaryOperator>(E)) { 767 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV); 768 return; 769 } 770 771 if (const ParenExpr *Exp = dyn_cast<ParenExpr>(E)) { 772 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV); 773 if (LV.isObjCIvar()) { 774 // If cast is to a structure pointer, follow gcc's behavior and make it 775 // a non-ivar write-barrier. 776 QualType ExpTy = E->getType(); 777 if (ExpTy->isPointerType()) 778 ExpTy = ExpTy->getAs<PointerType>()->getPointeeType(); 779 if (ExpTy->isRecordType()) 780 LV.SetObjCIvar(LV, false); 781 } 782 return; 783 } 784 if (const ImplicitCastExpr *Exp = dyn_cast<ImplicitCastExpr>(E)) { 785 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV); 786 return; 787 } 788 789 if (const CStyleCastExpr *Exp = dyn_cast<CStyleCastExpr>(E)) { 790 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV); 791 return; 792 } 793 794 if (const ArraySubscriptExpr *Exp = dyn_cast<ArraySubscriptExpr>(E)) { 795 setObjCGCLValueClass(Ctx, Exp->getBase(), LV); 796 if (LV.isObjCIvar() && !LV.isObjCArray()) 797 // Using array syntax to assigning to what an ivar points to is not 798 // same as assigning to the ivar itself. {id *Names;} Names[i] = 0; 799 LV.SetObjCIvar(LV, false); 800 else if (LV.isGlobalObjCRef() && !LV.isObjCArray()) 801 // Using array syntax to assigning to what global points to is not 802 // same as assigning to the global itself. {id *G;} G[i] = 0; 803 LV.SetGlobalObjCRef(LV, false); 804 return; 805 } 806 807 if (const MemberExpr *Exp = dyn_cast<MemberExpr>(E)) { 808 setObjCGCLValueClass(Ctx, Exp->getBase(), LV); 809 // We don't know if member is an 'ivar', but this flag is looked at 810 // only in the context of LV.isObjCIvar(). 811 LV.SetObjCArray(LV, E->getType()->isArrayType()); 812 return; 813 } 814} 815 816LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) { 817 const VarDecl *VD = dyn_cast<VarDecl>(E->getDecl()); 818 819 if (VD && (VD->isBlockVarDecl() || isa<ParmVarDecl>(VD) || 820 isa<ImplicitParamDecl>(VD))) { 821 LValue LV; 822 if (VD->hasExternalStorage()) { 823 llvm::Value *V = CGM.GetAddrOfGlobalVar(VD); 824 if (VD->getType()->isReferenceType()) 825 V = Builder.CreateLoad(V, "tmp"); 826 LV = LValue::MakeAddr(V, MakeQualifiers(E->getType())); 827 setObjCGCLValueClass(getContext(), E, LV); 828 return LV; 829 } 830 831 bool NonGCable = VD->hasLocalStorage() && !VD->hasAttr<BlocksAttr>(); 832 833 llvm::Value *V = LocalDeclMap[VD]; 834 assert(V && "DeclRefExpr not entered in LocalDeclMap?"); 835 836 Qualifiers Quals = MakeQualifiers(E->getType()); 837 // local variables do not get their gc attribute set. 838 // local static? 839 if (NonGCable) Quals.removeObjCGCAttr(); 840 841 if (VD->hasAttr<BlocksAttr>()) { 842 V = Builder.CreateStructGEP(V, 1, "forwarding"); 843 V = Builder.CreateLoad(V, false); 844 V = Builder.CreateStructGEP(V, getByRefValueLLVMField(VD), 845 VD->getNameAsString()); 846 } 847 if (VD->getType()->isReferenceType()) 848 V = Builder.CreateLoad(V, "tmp"); 849 LV = LValue::MakeAddr(V, Quals); 850 LValue::SetObjCNonGC(LV, NonGCable); 851 setObjCGCLValueClass(getContext(), E, LV); 852 return LV; 853 } 854 855 if (VD && VD->isFileVarDecl()) { 856 llvm::Value *V = CGM.GetAddrOfGlobalVar(VD); 857 if (VD->getType()->isReferenceType()) 858 V = Builder.CreateLoad(V, "tmp"); 859 LValue LV = LValue::MakeAddr(V, MakeQualifiers(E->getType())); 860 setObjCGCLValueClass(getContext(), E, LV); 861 return LV; 862 } 863 864 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(E->getDecl())) { 865 llvm::Value* V = CGM.GetAddrOfFunction(FD); 866 if (!FD->hasPrototype()) { 867 if (const FunctionProtoType *Proto = 868 FD->getType()->getAs<FunctionProtoType>()) { 869 // Ugly case: for a K&R-style definition, the type of the definition 870 // isn't the same as the type of a use. Correct for this with a 871 // bitcast. 872 QualType NoProtoType = 873 getContext().getFunctionNoProtoType(Proto->getResultType()); 874 NoProtoType = getContext().getPointerType(NoProtoType); 875 V = Builder.CreateBitCast(V, ConvertType(NoProtoType), "tmp"); 876 } 877 } 878 return LValue::MakeAddr(V, MakeQualifiers(E->getType())); 879 } 880 881 if (const ImplicitParamDecl *IPD = dyn_cast<ImplicitParamDecl>(E->getDecl())){ 882 llvm::Value *V = LocalDeclMap[IPD]; 883 assert(V && "BlockVarDecl not entered in LocalDeclMap?"); 884 return LValue::MakeAddr(V, MakeQualifiers(E->getType())); 885 } 886 887 if (E->getQualifier()) { 888 // FIXME: the qualifier check does not seem sufficient here 889 return EmitPointerToDataMemberLValue(cast<FieldDecl>(E->getDecl())); 890 } 891 892 assert(0 && "Unimp declref"); 893 //an invalid LValue, but the assert will 894 //ensure that this point is never reached. 895 return LValue(); 896} 897 898LValue CodeGenFunction::EmitBlockDeclRefLValue(const BlockDeclRefExpr *E) { 899 return LValue::MakeAddr(GetAddrOfBlockDecl(E), MakeQualifiers(E->getType())); 900} 901 902LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) { 903 // __extension__ doesn't affect lvalue-ness. 904 if (E->getOpcode() == UnaryOperator::Extension) 905 return EmitLValue(E->getSubExpr()); 906 907 QualType ExprTy = getContext().getCanonicalType(E->getSubExpr()->getType()); 908 switch (E->getOpcode()) { 909 default: assert(0 && "Unknown unary operator lvalue!"); 910 case UnaryOperator::Deref: { 911 QualType T = E->getSubExpr()->getType()->getPointeeType(); 912 assert(!T.isNull() && "CodeGenFunction::EmitUnaryOpLValue: Illegal type"); 913 914 Qualifiers Quals = MakeQualifiers(T); 915 Quals.setAddressSpace(ExprTy.getAddressSpace()); 916 917 LValue LV = LValue::MakeAddr(EmitScalarExpr(E->getSubExpr()), Quals); 918 // We should not generate __weak write barrier on indirect reference 919 // of a pointer to object; as in void foo (__weak id *param); *param = 0; 920 // But, we continue to generate __strong write barrier on indirect write 921 // into a pointer to object. 922 if (getContext().getLangOptions().ObjC1 && 923 getContext().getLangOptions().getGCMode() != LangOptions::NonGC && 924 LV.isObjCWeak()) 925 LValue::SetObjCNonGC(LV, !E->isOBJCGCCandidate(getContext())); 926 return LV; 927 } 928 case UnaryOperator::Real: 929 case UnaryOperator::Imag: 930 LValue LV = EmitLValue(E->getSubExpr()); 931 unsigned Idx = E->getOpcode() == UnaryOperator::Imag; 932 return LValue::MakeAddr(Builder.CreateStructGEP(LV.getAddress(), 933 Idx, "idx"), 934 MakeQualifiers(ExprTy)); 935 } 936} 937 938LValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) { 939 return LValue::MakeAddr(CGM.GetAddrOfConstantStringFromLiteral(E), 940 Qualifiers()); 941} 942 943LValue CodeGenFunction::EmitObjCEncodeExprLValue(const ObjCEncodeExpr *E) { 944 return LValue::MakeAddr(CGM.GetAddrOfConstantStringFromObjCEncode(E), 945 Qualifiers()); 946} 947 948 949LValue CodeGenFunction::EmitPredefinedFunctionName(unsigned Type) { 950 std::string GlobalVarName; 951 952 switch (Type) { 953 default: assert(0 && "Invalid type"); 954 case PredefinedExpr::Func: 955 GlobalVarName = "__func__."; 956 break; 957 case PredefinedExpr::Function: 958 GlobalVarName = "__FUNCTION__."; 959 break; 960 case PredefinedExpr::PrettyFunction: 961 GlobalVarName = "__PRETTY_FUNCTION__."; 962 break; 963 } 964 965 llvm::StringRef FnName = CurFn->getName(); 966 if (FnName.startswith("\01")) 967 FnName = FnName.substr(1); 968 GlobalVarName += FnName; 969 970 std::string FunctionName = 971 PredefinedExpr::ComputeName(getContext(), (PredefinedExpr::IdentType)Type, 972 CurCodeDecl); 973 974 llvm::Constant *C = 975 CGM.GetAddrOfConstantCString(FunctionName, GlobalVarName.c_str()); 976 return LValue::MakeAddr(C, Qualifiers()); 977} 978 979LValue CodeGenFunction::EmitPredefinedLValue(const PredefinedExpr *E) { 980 switch (E->getIdentType()) { 981 default: 982 return EmitUnsupportedLValue(E, "predefined expression"); 983 case PredefinedExpr::Func: 984 case PredefinedExpr::Function: 985 case PredefinedExpr::PrettyFunction: 986 return EmitPredefinedFunctionName(E->getIdentType()); 987 } 988} 989 990LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) { 991 // The index must always be an integer, which is not an aggregate. Emit it. 992 llvm::Value *Idx = EmitScalarExpr(E->getIdx()); 993 QualType IdxTy = E->getIdx()->getType(); 994 bool IdxSigned = IdxTy->isSignedIntegerType(); 995 996 // If the base is a vector type, then we are forming a vector element lvalue 997 // with this subscript. 998 if (E->getBase()->getType()->isVectorType()) { 999 // Emit the vector as an lvalue to get its address. 1000 LValue LHS = EmitLValue(E->getBase()); 1001 assert(LHS.isSimple() && "Can only subscript lvalue vectors here!"); 1002 Idx = Builder.CreateIntCast(Idx, 1003 llvm::Type::getInt32Ty(VMContext), IdxSigned, "vidx"); 1004 return LValue::MakeVectorElt(LHS.getAddress(), Idx, 1005 E->getBase()->getType().getCVRQualifiers()); 1006 } 1007 1008 // The base must be a pointer, which is not an aggregate. Emit it. 1009 llvm::Value *Base = EmitScalarExpr(E->getBase()); 1010 1011 // Extend or truncate the index type to 32 or 64-bits. 1012 unsigned IdxBitwidth = cast<llvm::IntegerType>(Idx->getType())->getBitWidth(); 1013 if (IdxBitwidth != LLVMPointerWidth) 1014 Idx = Builder.CreateIntCast(Idx, 1015 llvm::IntegerType::get(VMContext, LLVMPointerWidth), 1016 IdxSigned, "idxprom"); 1017 1018 // We know that the pointer points to a type of the correct size, unless the 1019 // size is a VLA or Objective-C interface. 1020 llvm::Value *Address = 0; 1021 if (const VariableArrayType *VAT = 1022 getContext().getAsVariableArrayType(E->getType())) { 1023 llvm::Value *VLASize = GetVLASize(VAT); 1024 1025 Idx = Builder.CreateMul(Idx, VLASize); 1026 1027 QualType BaseType = getContext().getBaseElementType(VAT); 1028 1029 uint64_t BaseTypeSize = getContext().getTypeSize(BaseType) / 8; 1030 Idx = Builder.CreateUDiv(Idx, 1031 llvm::ConstantInt::get(Idx->getType(), 1032 BaseTypeSize)); 1033 Address = Builder.CreateInBoundsGEP(Base, Idx, "arrayidx"); 1034 } else if (const ObjCInterfaceType *OIT = 1035 dyn_cast<ObjCInterfaceType>(E->getType())) { 1036 llvm::Value *InterfaceSize = 1037 llvm::ConstantInt::get(Idx->getType(), 1038 getContext().getTypeSize(OIT) / 8); 1039 1040 Idx = Builder.CreateMul(Idx, InterfaceSize); 1041 1042 const llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(VMContext); 1043 Address = Builder.CreateGEP(Builder.CreateBitCast(Base, i8PTy), 1044 Idx, "arrayidx"); 1045 Address = Builder.CreateBitCast(Address, Base->getType()); 1046 } else { 1047 Address = Builder.CreateInBoundsGEP(Base, Idx, "arrayidx"); 1048 } 1049 1050 QualType T = E->getBase()->getType()->getPointeeType(); 1051 assert(!T.isNull() && 1052 "CodeGenFunction::EmitArraySubscriptExpr(): Illegal base type"); 1053 1054 Qualifiers Quals = MakeQualifiers(T); 1055 Quals.setAddressSpace(E->getBase()->getType().getAddressSpace()); 1056 1057 LValue LV = LValue::MakeAddr(Address, Quals); 1058 if (getContext().getLangOptions().ObjC1 && 1059 getContext().getLangOptions().getGCMode() != LangOptions::NonGC) { 1060 LValue::SetObjCNonGC(LV, !E->isOBJCGCCandidate(getContext())); 1061 setObjCGCLValueClass(getContext(), E, LV); 1062 } 1063 return LV; 1064} 1065 1066static 1067llvm::Constant *GenerateConstantVector(llvm::LLVMContext &VMContext, 1068 llvm::SmallVector<unsigned, 4> &Elts) { 1069 llvm::SmallVector<llvm::Constant *, 4> CElts; 1070 1071 for (unsigned i = 0, e = Elts.size(); i != e; ++i) 1072 CElts.push_back(llvm::ConstantInt::get( 1073 llvm::Type::getInt32Ty(VMContext), Elts[i])); 1074 1075 return llvm::ConstantVector::get(&CElts[0], CElts.size()); 1076} 1077 1078LValue CodeGenFunction:: 1079EmitExtVectorElementExpr(const ExtVectorElementExpr *E) { 1080 // Emit the base vector as an l-value. 1081 LValue Base; 1082 1083 // ExtVectorElementExpr's base can either be a vector or pointer to vector. 1084 if (!E->isArrow()) { 1085 assert(E->getBase()->getType()->isVectorType()); 1086 Base = EmitLValue(E->getBase()); 1087 } else { 1088 const PointerType *PT = E->getBase()->getType()->getAs<PointerType>(); 1089 llvm::Value *Ptr = EmitScalarExpr(E->getBase()); 1090 Qualifiers Quals = MakeQualifiers(PT->getPointeeType()); 1091 Quals.removeObjCGCAttr(); 1092 Base = LValue::MakeAddr(Ptr, Quals); 1093 } 1094 1095 // Encode the element access list into a vector of unsigned indices. 1096 llvm::SmallVector<unsigned, 4> Indices; 1097 E->getEncodedElementAccess(Indices); 1098 1099 if (Base.isSimple()) { 1100 llvm::Constant *CV = GenerateConstantVector(VMContext, Indices); 1101 return LValue::MakeExtVectorElt(Base.getAddress(), CV, 1102 Base.getVRQualifiers()); 1103 } 1104 assert(Base.isExtVectorElt() && "Can only subscript lvalue vec elts here!"); 1105 1106 llvm::Constant *BaseElts = Base.getExtVectorElts(); 1107 llvm::SmallVector<llvm::Constant *, 4> CElts; 1108 1109 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext); 1110 for (unsigned i = 0, e = Indices.size(); i != e; ++i) { 1111 if (isa<llvm::ConstantAggregateZero>(BaseElts)) 1112 CElts.push_back(llvm::ConstantInt::get(Int32Ty, 0)); 1113 else 1114 CElts.push_back(cast<llvm::Constant>(BaseElts->getOperand(Indices[i]))); 1115 } 1116 llvm::Constant *CV = llvm::ConstantVector::get(&CElts[0], CElts.size()); 1117 return LValue::MakeExtVectorElt(Base.getExtVectorAddr(), CV, 1118 Base.getVRQualifiers()); 1119} 1120 1121LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) { 1122 bool isUnion = false; 1123 bool isNonGC = false; 1124 Expr *BaseExpr = E->getBase(); 1125 llvm::Value *BaseValue = NULL; 1126 Qualifiers BaseQuals; 1127 1128 // If this is s.x, emit s as an lvalue. If it is s->x, emit s as a scalar. 1129 if (E->isArrow()) { 1130 BaseValue = EmitScalarExpr(BaseExpr); 1131 const PointerType *PTy = 1132 BaseExpr->getType()->getAs<PointerType>(); 1133 if (PTy->getPointeeType()->isUnionType()) 1134 isUnion = true; 1135 BaseQuals = PTy->getPointeeType().getQualifiers(); 1136 } else if (isa<ObjCPropertyRefExpr>(BaseExpr->IgnoreParens()) || 1137 isa<ObjCImplicitSetterGetterRefExpr>( 1138 BaseExpr->IgnoreParens())) { 1139 RValue RV = EmitObjCPropertyGet(BaseExpr); 1140 BaseValue = RV.getAggregateAddr(); 1141 if (BaseExpr->getType()->isUnionType()) 1142 isUnion = true; 1143 BaseQuals = BaseExpr->getType().getQualifiers(); 1144 } else { 1145 LValue BaseLV = EmitLValue(BaseExpr); 1146 if (BaseLV.isNonGC()) 1147 isNonGC = true; 1148 // FIXME: this isn't right for bitfields. 1149 BaseValue = BaseLV.getAddress(); 1150 QualType BaseTy = BaseExpr->getType(); 1151 if (BaseTy->isUnionType()) 1152 isUnion = true; 1153 BaseQuals = BaseTy.getQualifiers(); 1154 } 1155 1156 FieldDecl *Field = dyn_cast<FieldDecl>(E->getMemberDecl()); 1157 // FIXME: Handle non-field member expressions 1158 assert(Field && "No code generation for non-field member references"); 1159 LValue MemExpLV = EmitLValueForField(BaseValue, Field, isUnion, 1160 BaseQuals.getCVRQualifiers()); 1161 LValue::SetObjCNonGC(MemExpLV, isNonGC); 1162 setObjCGCLValueClass(getContext(), E, MemExpLV); 1163 return MemExpLV; 1164} 1165 1166LValue CodeGenFunction::EmitLValueForBitfield(llvm::Value* BaseValue, 1167 FieldDecl* Field, 1168 unsigned CVRQualifiers) { 1169 CodeGenTypes::BitFieldInfo Info = CGM.getTypes().getBitFieldInfo(Field); 1170 1171 // FIXME: CodeGenTypes should expose a method to get the appropriate type for 1172 // FieldTy (the appropriate type is ABI-dependent). 1173 const llvm::Type *FieldTy = 1174 CGM.getTypes().ConvertTypeForMem(Field->getType()); 1175 const llvm::PointerType *BaseTy = 1176 cast<llvm::PointerType>(BaseValue->getType()); 1177 unsigned AS = BaseTy->getAddressSpace(); 1178 BaseValue = Builder.CreateBitCast(BaseValue, 1179 llvm::PointerType::get(FieldTy, AS), 1180 "tmp"); 1181 1182 llvm::Value *Idx = 1183 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), Info.FieldNo); 1184 llvm::Value *V = Builder.CreateGEP(BaseValue, Idx, "tmp"); 1185 1186 return LValue::MakeBitfield(V, Info.Start, Info.Size, 1187 Field->getType()->isSignedIntegerType(), 1188 Field->getType().getCVRQualifiers()|CVRQualifiers); 1189} 1190 1191LValue CodeGenFunction::EmitLValueForField(llvm::Value* BaseValue, 1192 FieldDecl* Field, 1193 bool isUnion, 1194 unsigned CVRQualifiers) { 1195 if (Field->isBitField()) 1196 return EmitLValueForBitfield(BaseValue, Field, CVRQualifiers); 1197 1198 unsigned idx = CGM.getTypes().getLLVMFieldNo(Field); 1199 llvm::Value *V = Builder.CreateStructGEP(BaseValue, idx, "tmp"); 1200 1201 // Match union field type. 1202 if (isUnion) { 1203 const llvm::Type *FieldTy = 1204 CGM.getTypes().ConvertTypeForMem(Field->getType()); 1205 const llvm::PointerType * BaseTy = 1206 cast<llvm::PointerType>(BaseValue->getType()); 1207 unsigned AS = BaseTy->getAddressSpace(); 1208 V = Builder.CreateBitCast(V, 1209 llvm::PointerType::get(FieldTy, AS), 1210 "tmp"); 1211 } 1212 if (Field->getType()->isReferenceType()) 1213 V = Builder.CreateLoad(V, "tmp"); 1214 1215 Qualifiers Quals = MakeQualifiers(Field->getType()); 1216 Quals.addCVRQualifiers(CVRQualifiers); 1217 // __weak attribute on a field is ignored. 1218 if (Quals.getObjCGCAttr() == Qualifiers::Weak) 1219 Quals.removeObjCGCAttr(); 1220 1221 return LValue::MakeAddr(V, Quals); 1222} 1223 1224LValue CodeGenFunction::EmitCompoundLiteralLValue(const CompoundLiteralExpr* E){ 1225 const llvm::Type *LTy = ConvertType(E->getType()); 1226 llvm::Value *DeclPtr = CreateTempAlloca(LTy, ".compoundliteral"); 1227 1228 const Expr* InitExpr = E->getInitializer(); 1229 LValue Result = LValue::MakeAddr(DeclPtr, MakeQualifiers(E->getType())); 1230 1231 if (E->getType()->isComplexType()) 1232 EmitComplexExprIntoAddr(InitExpr, DeclPtr, false); 1233 else if (hasAggregateLLVMType(E->getType())) 1234 EmitAnyExpr(InitExpr, DeclPtr, false); 1235 else 1236 EmitStoreThroughLValue(EmitAnyExpr(InitExpr), Result, E->getType()); 1237 1238 return Result; 1239} 1240 1241LValue 1242CodeGenFunction::EmitConditionalOperatorLValue(const ConditionalOperator* E) { 1243 if (E->isLvalue(getContext()) == Expr::LV_Valid) { 1244 llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true"); 1245 llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false"); 1246 llvm::BasicBlock *ContBlock = createBasicBlock("cond.end"); 1247 1248 llvm::Value *Cond = EvaluateExprAsBool(E->getCond()); 1249 Builder.CreateCondBr(Cond, LHSBlock, RHSBlock); 1250 1251 EmitBlock(LHSBlock); 1252 1253 LValue LHS = EmitLValue(E->getLHS()); 1254 if (!LHS.isSimple()) 1255 return EmitUnsupportedLValue(E, "conditional operator"); 1256 1257 llvm::Value *Temp = CreateTempAlloca(LHS.getAddress()->getType(),"condtmp"); 1258 Builder.CreateStore(LHS.getAddress(), Temp); 1259 EmitBranch(ContBlock); 1260 1261 EmitBlock(RHSBlock); 1262 LValue RHS = EmitLValue(E->getRHS()); 1263 if (!RHS.isSimple()) 1264 return EmitUnsupportedLValue(E, "conditional operator"); 1265 1266 Builder.CreateStore(RHS.getAddress(), Temp); 1267 EmitBranch(ContBlock); 1268 1269 EmitBlock(ContBlock); 1270 1271 Temp = Builder.CreateLoad(Temp, "lv"); 1272 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType())); 1273 } 1274 1275 // ?: here should be an aggregate. 1276 assert((hasAggregateLLVMType(E->getType()) && 1277 !E->getType()->isAnyComplexType()) && 1278 "Unexpected conditional operator!"); 1279 1280 llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType())); 1281 EmitAggExpr(E, Temp, false); 1282 1283 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType())); 1284} 1285 1286/// EmitCastLValue - Casts are never lvalues. If a cast is needed by the code 1287/// generator in an lvalue context, then it must mean that we need the address 1288/// of an aggregate in order to access one of its fields. This can happen for 1289/// all the reasons that casts are permitted with aggregate result, including 1290/// noop aggregate casts, and cast from scalar to union. 1291LValue CodeGenFunction::EmitCastLValue(const CastExpr *E) { 1292 switch (E->getCastKind()) { 1293 default: 1294 // If this is an lvalue cast, treat it as a no-op. 1295 // FIXME: We shouldn't need to check for this explicitly! 1296 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) 1297 if (ICE->isLvalueCast()) 1298 return EmitLValue(E->getSubExpr()); 1299 1300 assert(0 && "Unhandled cast!"); 1301 1302 case CastExpr::CK_NoOp: 1303 case CastExpr::CK_ConstructorConversion: 1304 case CastExpr::CK_UserDefinedConversion: 1305 return EmitLValue(E->getSubExpr()); 1306 1307 case CastExpr::CK_DerivedToBase: { 1308 const RecordType *DerivedClassTy = 1309 E->getSubExpr()->getType()->getAs<RecordType>(); 1310 CXXRecordDecl *DerivedClassDecl = 1311 cast<CXXRecordDecl>(DerivedClassTy->getDecl()); 1312 1313 const RecordType *BaseClassTy = E->getType()->getAs<RecordType>(); 1314 CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseClassTy->getDecl()); 1315 1316 LValue LV = EmitLValue(E->getSubExpr()); 1317 1318 // Perform the derived-to-base conversion 1319 llvm::Value *Base = 1320 GetAddressCXXOfBaseClass(LV.getAddress(), DerivedClassDecl, 1321 BaseClassDecl, /*NullCheckValue=*/false); 1322 1323 return LValue::MakeAddr(Base, MakeQualifiers(E->getType())); 1324 } 1325 1326 case CastExpr::CK_ToUnion: { 1327 llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType())); 1328 EmitAnyExpr(E->getSubExpr(), Temp, false); 1329 1330 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType())); 1331 } 1332 } 1333} 1334 1335LValue CodeGenFunction::EmitNullInitializationLValue( 1336 const CXXZeroInitValueExpr *E) { 1337 QualType Ty = E->getType(); 1338 const llvm::Type *LTy = ConvertTypeForMem(Ty); 1339 llvm::AllocaInst *Alloc = CreateTempAlloca(LTy); 1340 unsigned Align = getContext().getTypeAlign(Ty)/8; 1341 Alloc->setAlignment(Align); 1342 LValue lvalue = LValue::MakeAddr(Alloc, Qualifiers()); 1343 EmitMemSetToZero(lvalue.getAddress(), Ty); 1344 return lvalue; 1345} 1346 1347//===--------------------------------------------------------------------===// 1348// Expression Emission 1349//===--------------------------------------------------------------------===// 1350 1351 1352RValue CodeGenFunction::EmitCallExpr(const CallExpr *E) { 1353 // Builtins never have block type. 1354 if (E->getCallee()->getType()->isBlockPointerType()) 1355 return EmitBlockCallExpr(E); 1356 1357 if (const CXXMemberCallExpr *CE = dyn_cast<CXXMemberCallExpr>(E)) 1358 return EmitCXXMemberCallExpr(CE); 1359 1360 const Decl *TargetDecl = 0; 1361 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E->getCallee())) { 1362 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CE->getSubExpr())) { 1363 TargetDecl = DRE->getDecl(); 1364 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(TargetDecl)) 1365 if (unsigned builtinID = FD->getBuiltinID()) 1366 return EmitBuiltinExpr(FD, builtinID, E); 1367 } 1368 } 1369 1370 if (const CXXOperatorCallExpr *CE = dyn_cast<CXXOperatorCallExpr>(E)) 1371 if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(TargetDecl)) 1372 return EmitCXXOperatorMemberCallExpr(CE, MD); 1373 1374 if (isa<CXXPseudoDestructorExpr>(E->getCallee())) { 1375 // C++ [expr.pseudo]p1: 1376 // The result shall only be used as the operand for the function call 1377 // operator (), and the result of such a call has type void. The only 1378 // effect is the evaluation of the postfix-expression before the dot or 1379 // arrow. 1380 EmitScalarExpr(E->getCallee()); 1381 return RValue::get(0); 1382 } 1383 1384 llvm::Value *Callee = EmitScalarExpr(E->getCallee()); 1385 return EmitCall(Callee, E->getCallee()->getType(), 1386 E->arg_begin(), E->arg_end(), TargetDecl); 1387} 1388 1389LValue CodeGenFunction::EmitBinaryOperatorLValue(const BinaryOperator *E) { 1390 // Comma expressions just emit their LHS then their RHS as an l-value. 1391 if (E->getOpcode() == BinaryOperator::Comma) { 1392 EmitAnyExpr(E->getLHS()); 1393 return EmitLValue(E->getRHS()); 1394 } 1395 1396 if (E->getOpcode() == BinaryOperator::PtrMemD || 1397 E->getOpcode() == BinaryOperator::PtrMemI) 1398 return EmitPointerToDataMemberBinaryExpr(E); 1399 1400 // Can only get l-value for binary operator expressions which are a 1401 // simple assignment of aggregate type. 1402 if (E->getOpcode() != BinaryOperator::Assign) 1403 return EmitUnsupportedLValue(E, "binary l-value expression"); 1404 1405 if (!hasAggregateLLVMType(E->getType())) { 1406 // Emit the LHS as an l-value. 1407 LValue LV = EmitLValue(E->getLHS()); 1408 1409 llvm::Value *RHS = EmitScalarExpr(E->getRHS()); 1410 EmitStoreOfScalar(RHS, LV.getAddress(), LV.isVolatileQualified(), 1411 E->getType()); 1412 return LV; 1413 } 1414 1415 llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType())); 1416 EmitAggExpr(E, Temp, false); 1417 // FIXME: Are these qualifiers correct? 1418 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType())); 1419} 1420 1421LValue CodeGenFunction::EmitCallExprLValue(const CallExpr *E) { 1422 RValue RV = EmitCallExpr(E); 1423 1424 if (!RV.isScalar()) 1425 return LValue::MakeAddr(RV.getAggregateAddr(),MakeQualifiers(E->getType())); 1426 1427 assert(E->getCallReturnType()->isReferenceType() && 1428 "Can't have a scalar return unless the return type is a " 1429 "reference type!"); 1430 1431 return LValue::MakeAddr(RV.getScalarVal(), MakeQualifiers(E->getType())); 1432} 1433 1434LValue CodeGenFunction::EmitVAArgExprLValue(const VAArgExpr *E) { 1435 // FIXME: This shouldn't require another copy. 1436 llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType())); 1437 EmitAggExpr(E, Temp, false); 1438 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType())); 1439} 1440 1441LValue 1442CodeGenFunction::EmitCXXConditionDeclLValue(const CXXConditionDeclExpr *E) { 1443 EmitLocalBlockVarDecl(*E->getVarDecl()); 1444 return EmitDeclRefLValue(E); 1445} 1446 1447LValue CodeGenFunction::EmitCXXConstructLValue(const CXXConstructExpr *E) { 1448 llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(E->getType()), "tmp"); 1449 EmitCXXConstructExpr(Temp, E); 1450 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType())); 1451} 1452 1453LValue 1454CodeGenFunction::EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr *E) { 1455 LValue LV = EmitLValue(E->getSubExpr()); 1456 PushCXXTemporary(E->getTemporary(), LV.getAddress()); 1457 return LV; 1458} 1459 1460LValue CodeGenFunction::EmitObjCMessageExprLValue(const ObjCMessageExpr *E) { 1461 // Can only get l-value for message expression returning aggregate type 1462 RValue RV = EmitObjCMessageExpr(E); 1463 // FIXME: can this be volatile? 1464 return LValue::MakeAddr(RV.getAggregateAddr(), MakeQualifiers(E->getType())); 1465} 1466 1467llvm::Value *CodeGenFunction::EmitIvarOffset(const ObjCInterfaceDecl *Interface, 1468 const ObjCIvarDecl *Ivar) { 1469 return CGM.getObjCRuntime().EmitIvarOffset(*this, Interface, Ivar); 1470} 1471 1472LValue CodeGenFunction::EmitLValueForIvar(QualType ObjectTy, 1473 llvm::Value *BaseValue, 1474 const ObjCIvarDecl *Ivar, 1475 unsigned CVRQualifiers) { 1476 return CGM.getObjCRuntime().EmitObjCValueForIvar(*this, ObjectTy, BaseValue, 1477 Ivar, CVRQualifiers); 1478} 1479 1480LValue CodeGenFunction::EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E) { 1481 // FIXME: A lot of the code below could be shared with EmitMemberExpr. 1482 llvm::Value *BaseValue = 0; 1483 const Expr *BaseExpr = E->getBase(); 1484 Qualifiers BaseQuals; 1485 QualType ObjectTy; 1486 if (E->isArrow()) { 1487 BaseValue = EmitScalarExpr(BaseExpr); 1488 ObjectTy = BaseExpr->getType()->getPointeeType(); 1489 BaseQuals = ObjectTy.getQualifiers(); 1490 } else { 1491 LValue BaseLV = EmitLValue(BaseExpr); 1492 // FIXME: this isn't right for bitfields. 1493 BaseValue = BaseLV.getAddress(); 1494 ObjectTy = BaseExpr->getType(); 1495 BaseQuals = ObjectTy.getQualifiers(); 1496 } 1497 1498 LValue LV = 1499 EmitLValueForIvar(ObjectTy, BaseValue, E->getDecl(), 1500 BaseQuals.getCVRQualifiers()); 1501 setObjCGCLValueClass(getContext(), E, LV); 1502 return LV; 1503} 1504 1505LValue 1506CodeGenFunction::EmitObjCPropertyRefLValue(const ObjCPropertyRefExpr *E) { 1507 // This is a special l-value that just issues sends when we load or store 1508 // through it. 1509 return LValue::MakePropertyRef(E, E->getType().getCVRQualifiers()); 1510} 1511 1512LValue CodeGenFunction::EmitObjCKVCRefLValue( 1513 const ObjCImplicitSetterGetterRefExpr *E) { 1514 // This is a special l-value that just issues sends when we load or store 1515 // through it. 1516 return LValue::MakeKVCRef(E, E->getType().getCVRQualifiers()); 1517} 1518 1519LValue CodeGenFunction::EmitObjCSuperExprLValue(const ObjCSuperExpr *E) { 1520 return EmitUnsupportedLValue(E, "use of super"); 1521} 1522 1523LValue CodeGenFunction::EmitStmtExprLValue(const StmtExpr *E) { 1524 // Can only get l-value for message expression returning aggregate type 1525 RValue RV = EmitAnyExprToTemp(E); 1526 // FIXME: can this be volatile? 1527 return LValue::MakeAddr(RV.getAggregateAddr(), MakeQualifiers(E->getType())); 1528} 1529 1530 1531LValue CodeGenFunction::EmitPointerToDataMemberLValue(const FieldDecl *Field) { 1532 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(Field->getDeclContext()); 1533 QualType NNSpecTy = 1534 getContext().getCanonicalType( 1535 getContext().getTypeDeclType(const_cast<CXXRecordDecl*>(ClassDecl))); 1536 NNSpecTy = getContext().getPointerType(NNSpecTy); 1537 llvm::Value *V = llvm::Constant::getNullValue(ConvertType(NNSpecTy)); 1538 LValue MemExpLV = EmitLValueForField(V, const_cast<FieldDecl*>(Field), 1539 /*isUnion*/false, /*Qualifiers*/0); 1540 const llvm::Type *ResultType = ConvertType(getContext().getPointerDiffType()); 1541 V = Builder.CreatePtrToInt(MemExpLV.getAddress(), ResultType, "datamember"); 1542 return LValue::MakeAddr(V, MakeQualifiers(Field->getType())); 1543} 1544 1545RValue CodeGenFunction::EmitCall(llvm::Value *Callee, QualType CalleeType, 1546 CallExpr::const_arg_iterator ArgBeg, 1547 CallExpr::const_arg_iterator ArgEnd, 1548 const Decl *TargetDecl) { 1549 // Get the actual function type. The callee type will always be a pointer to 1550 // function type or a block pointer type. 1551 assert(CalleeType->isFunctionPointerType() && 1552 "Call must have function pointer type!"); 1553 1554 CalleeType = getContext().getCanonicalType(CalleeType); 1555 1556 QualType FnType = cast<PointerType>(CalleeType)->getPointeeType(); 1557 QualType ResultType = cast<FunctionType>(FnType)->getResultType(); 1558 1559 CallArgList Args; 1560 EmitCallArgs(Args, dyn_cast<FunctionProtoType>(FnType), ArgBeg, ArgEnd); 1561 1562 // FIXME: We should not need to do this, it should be part of the function 1563 // type. 1564 unsigned CallingConvention = 0; 1565 if (const llvm::Function *F = 1566 dyn_cast<llvm::Function>(Callee->stripPointerCasts())) 1567 CallingConvention = F->getCallingConv(); 1568 return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args, 1569 CallingConvention), 1570 Callee, Args, TargetDecl); 1571} 1572 1573LValue CodeGenFunction:: 1574EmitPointerToDataMemberBinaryExpr(const BinaryOperator *E) { 1575 llvm::Value *BaseV = EmitLValue(E->getLHS()).getAddress(); 1576 if (E->getOpcode() == BinaryOperator::PtrMemI) 1577 BaseV = Builder.CreateLoad(BaseV, "indir.ptr"); 1578 const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(getLLVMContext()); 1579 BaseV = Builder.CreateBitCast(BaseV, i8Ty); 1580 LValue RHSLV = EmitLValue(E->getRHS()); 1581 llvm::Value *OffsetV = 1582 EmitLoadOfLValue(RHSLV, E->getRHS()->getType()).getScalarVal(); 1583 const llvm::Type* ResultType = ConvertType(getContext().getPointerDiffType()); 1584 OffsetV = Builder.CreateBitCast(OffsetV, ResultType); 1585 llvm::Value *AddV = Builder.CreateInBoundsGEP(BaseV, OffsetV, "add.ptr"); 1586 1587 QualType Ty = E->getRHS()->getType(); 1588 Ty = Ty->getAs<MemberPointerType>()->getPointeeType(); 1589 1590 const llvm::Type *PType = ConvertType(getContext().getPointerType(Ty)); 1591 AddV = Builder.CreateBitCast(AddV, PType); 1592 return LValue::MakeAddr(AddV, MakeQualifiers(Ty)); 1593} 1594 1595