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