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