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