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