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