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