CGExprAgg.cpp revision 2710c4159ff4761ba9867aca18f60a178b297686
1//===--- CGExprAgg.cpp - Emit LLVM Code from Aggregate 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 Aggregate Expr nodes as LLVM code. 11// 12//===----------------------------------------------------------------------===// 13 14#include "CodeGenFunction.h" 15#include "CodeGenModule.h" 16#include "CGObjCRuntime.h" 17#include "clang/AST/ASTContext.h" 18#include "clang/AST/DeclCXX.h" 19#include "clang/AST/StmtVisitor.h" 20#include "llvm/Constants.h" 21#include "llvm/Function.h" 22#include "llvm/GlobalVariable.h" 23#include "llvm/Support/Compiler.h" 24#include "llvm/Intrinsics.h" 25using namespace clang; 26using namespace CodeGen; 27 28//===----------------------------------------------------------------------===// 29// Aggregate Expression Emitter 30//===----------------------------------------------------------------------===// 31 32namespace { 33class VISIBILITY_HIDDEN AggExprEmitter : public StmtVisitor<AggExprEmitter> { 34 CodeGenFunction &CGF; 35 CGBuilderTy &Builder; 36 llvm::Value *DestPtr; 37 bool VolatileDest; 38 bool IgnoreResult; 39 bool IsInitializer; 40 bool RequiresGCollection; 41public: 42 AggExprEmitter(CodeGenFunction &cgf, llvm::Value *destPtr, bool v, 43 bool ignore, bool isinit, bool requiresGCollection) 44 : CGF(cgf), Builder(CGF.Builder), 45 DestPtr(destPtr), VolatileDest(v), IgnoreResult(ignore), 46 IsInitializer(isinit), RequiresGCollection(requiresGCollection) { 47 } 48 49 //===--------------------------------------------------------------------===// 50 // Utilities 51 //===--------------------------------------------------------------------===// 52 53 /// EmitAggLoadOfLValue - Given an expression with aggregate type that 54 /// represents a value lvalue, this method emits the address of the lvalue, 55 /// then loads the result into DestPtr. 56 void EmitAggLoadOfLValue(const Expr *E); 57 58 /// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired. 59 void EmitFinalDestCopy(const Expr *E, LValue Src, bool Ignore = false); 60 void EmitFinalDestCopy(const Expr *E, RValue Src, bool Ignore = false); 61 62 //===--------------------------------------------------------------------===// 63 // Visitor Methods 64 //===--------------------------------------------------------------------===// 65 66 void VisitStmt(Stmt *S) { 67 CGF.ErrorUnsupported(S, "aggregate expression"); 68 } 69 void VisitParenExpr(ParenExpr *PE) { Visit(PE->getSubExpr()); } 70 void VisitUnaryExtension(UnaryOperator *E) { Visit(E->getSubExpr()); } 71 72 // l-values. 73 void VisitDeclRefExpr(DeclRefExpr *DRE) { EmitAggLoadOfLValue(DRE); } 74 void VisitMemberExpr(MemberExpr *ME) { EmitAggLoadOfLValue(ME); } 75 void VisitUnaryDeref(UnaryOperator *E) { EmitAggLoadOfLValue(E); } 76 void VisitStringLiteral(StringLiteral *E) { EmitAggLoadOfLValue(E); } 77 void VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { 78 EmitAggLoadOfLValue(E); 79 } 80 void VisitArraySubscriptExpr(ArraySubscriptExpr *E) { 81 EmitAggLoadOfLValue(E); 82 } 83 void VisitBlockDeclRefExpr(const BlockDeclRefExpr *E) { 84 EmitAggLoadOfLValue(E); 85 } 86 void VisitPredefinedExpr(const PredefinedExpr *E) { 87 EmitAggLoadOfLValue(E); 88 } 89 90 // Operators. 91 void VisitCastExpr(CastExpr *E); 92 void VisitCallExpr(const CallExpr *E); 93 void VisitStmtExpr(const StmtExpr *E); 94 void VisitBinaryOperator(const BinaryOperator *BO); 95 void VisitPointerToDataMemberBinaryOperator(const BinaryOperator *BO); 96 void VisitBinAssign(const BinaryOperator *E); 97 void VisitBinComma(const BinaryOperator *E); 98 void VisitUnaryAddrOf(const UnaryOperator *E); 99 100 void VisitObjCMessageExpr(ObjCMessageExpr *E); 101 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) { 102 EmitAggLoadOfLValue(E); 103 } 104 void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E); 105 void VisitObjCImplicitSetterGetterRefExpr(ObjCImplicitSetterGetterRefExpr *E); 106 107 void VisitConditionalOperator(const ConditionalOperator *CO); 108 void VisitChooseExpr(const ChooseExpr *CE); 109 void VisitInitListExpr(InitListExpr *E); 110 void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) { 111 Visit(DAE->getExpr()); 112 } 113 void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E); 114 void VisitCXXConstructExpr(const CXXConstructExpr *E); 115 void VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E); 116 void VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E); 117 void VisitCXXTypeidExpr(CXXTypeidExpr *E) { EmitAggLoadOfLValue(E); } 118 119 void VisitVAArgExpr(VAArgExpr *E); 120 121 void EmitInitializationToLValue(Expr *E, LValue Address); 122 void EmitNullInitializationToLValue(LValue Address, QualType T); 123 // case Expr::ChooseExprClass: 124 125}; 126} // end anonymous namespace. 127 128//===----------------------------------------------------------------------===// 129// Utilities 130//===----------------------------------------------------------------------===// 131 132/// EmitAggLoadOfLValue - Given an expression with aggregate type that 133/// represents a value lvalue, this method emits the address of the lvalue, 134/// then loads the result into DestPtr. 135void AggExprEmitter::EmitAggLoadOfLValue(const Expr *E) { 136 LValue LV = CGF.EmitLValue(E); 137 EmitFinalDestCopy(E, LV); 138} 139 140/// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired. 141void AggExprEmitter::EmitFinalDestCopy(const Expr *E, RValue Src, bool Ignore) { 142 assert(Src.isAggregate() && "value must be aggregate value!"); 143 144 // If the result is ignored, don't copy from the value. 145 if (DestPtr == 0) { 146 if (!Src.isVolatileQualified() || (IgnoreResult && Ignore)) 147 return; 148 // If the source is volatile, we must read from it; to do that, we need 149 // some place to put it. 150 DestPtr = CGF.CreateTempAlloca(CGF.ConvertType(E->getType()), "agg.tmp"); 151 } 152 153 if (RequiresGCollection) { 154 CGF.CGM.getObjCRuntime().EmitGCMemmoveCollectable(CGF, 155 DestPtr, Src.getAggregateAddr(), 156 E->getType()); 157 return; 158 } 159 // If the result of the assignment is used, copy the LHS there also. 160 // FIXME: Pass VolatileDest as well. I think we also need to merge volatile 161 // from the source as well, as we can't eliminate it if either operand 162 // is volatile, unless copy has volatile for both source and destination.. 163 CGF.EmitAggregateCopy(DestPtr, Src.getAggregateAddr(), E->getType(), 164 VolatileDest|Src.isVolatileQualified()); 165} 166 167/// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired. 168void AggExprEmitter::EmitFinalDestCopy(const Expr *E, LValue Src, bool Ignore) { 169 assert(Src.isSimple() && "Can't have aggregate bitfield, vector, etc"); 170 171 EmitFinalDestCopy(E, RValue::getAggregate(Src.getAddress(), 172 Src.isVolatileQualified()), 173 Ignore); 174} 175 176//===----------------------------------------------------------------------===// 177// Visitor Methods 178//===----------------------------------------------------------------------===// 179 180void AggExprEmitter::VisitCastExpr(CastExpr *E) { 181 switch (E->getCastKind()) { 182 default: assert(0 && "Unhandled cast kind!"); 183 184 case CastExpr::CK_ToUnion: { 185 // GCC union extension 186 QualType PtrTy = 187 CGF.getContext().getPointerType(E->getSubExpr()->getType()); 188 llvm::Value *CastPtr = Builder.CreateBitCast(DestPtr, 189 CGF.ConvertType(PtrTy)); 190 EmitInitializationToLValue(E->getSubExpr(), 191 LValue::MakeAddr(CastPtr, Qualifiers())); 192 break; 193 } 194 195 // FIXME: Remove the CK_Unknown check here. 196 case CastExpr::CK_Unknown: 197 case CastExpr::CK_NoOp: 198 case CastExpr::CK_UserDefinedConversion: 199 case CastExpr::CK_ConstructorConversion: 200 assert(CGF.getContext().hasSameUnqualifiedType(E->getSubExpr()->getType(), 201 E->getType()) && 202 "Implicit cast types must be compatible"); 203 Visit(E->getSubExpr()); 204 break; 205 206 case CastExpr::CK_NullToMemberPointer: { 207 const llvm::Type *PtrDiffTy = 208 CGF.ConvertType(CGF.getContext().getPointerDiffType()); 209 210 llvm::Value *NullValue = llvm::Constant::getNullValue(PtrDiffTy); 211 llvm::Value *Ptr = Builder.CreateStructGEP(DestPtr, 0, "ptr"); 212 Builder.CreateStore(NullValue, Ptr, VolatileDest); 213 214 llvm::Value *Adj = Builder.CreateStructGEP(DestPtr, 1, "adj"); 215 Builder.CreateStore(NullValue, Adj, VolatileDest); 216 217 break; 218 } 219 220 case CastExpr::CK_BitCast: { 221 // This must be a member function pointer cast. 222 Visit(E->getSubExpr()); 223 break; 224 } 225 226 case CastExpr::CK_BaseToDerivedMemberPointer: { 227 QualType SrcType = E->getSubExpr()->getType(); 228 229 llvm::Value *Src = CGF.CreateTempAlloca(CGF.ConvertTypeForMem(SrcType), 230 "tmp"); 231 CGF.EmitAggExpr(E->getSubExpr(), Src, SrcType.isVolatileQualified()); 232 233 llvm::Value *SrcPtr = Builder.CreateStructGEP(Src, 0, "src.ptr"); 234 SrcPtr = Builder.CreateLoad(SrcPtr); 235 236 llvm::Value *SrcAdj = Builder.CreateStructGEP(Src, 1, "src.adj"); 237 SrcAdj = Builder.CreateLoad(SrcAdj); 238 239 llvm::Value *DstPtr = Builder.CreateStructGEP(DestPtr, 0, "dst.ptr"); 240 Builder.CreateStore(SrcPtr, DstPtr, VolatileDest); 241 242 llvm::Value *DstAdj = Builder.CreateStructGEP(DestPtr, 1, "dst.adj"); 243 244 // Now See if we need to update the adjustment. 245 const CXXRecordDecl *SrcDecl = 246 cast<CXXRecordDecl>(SrcType->getAs<MemberPointerType>()-> 247 getClass()->getAs<RecordType>()->getDecl()); 248 const CXXRecordDecl *DstDecl = 249 cast<CXXRecordDecl>(E->getType()->getAs<MemberPointerType>()-> 250 getClass()->getAs<RecordType>()->getDecl()); 251 252 llvm::Constant *Adj = CGF.CGM.GetCXXBaseClassOffset(DstDecl, SrcDecl); 253 if (Adj) 254 SrcAdj = Builder.CreateAdd(SrcAdj, Adj, "adj"); 255 256 Builder.CreateStore(SrcAdj, DstAdj, VolatileDest); 257 break; 258 } 259 } 260} 261 262void AggExprEmitter::VisitCallExpr(const CallExpr *E) { 263 if (E->getCallReturnType()->isReferenceType()) { 264 EmitAggLoadOfLValue(E); 265 return; 266 } 267 268 RValue RV = CGF.EmitCallExpr(E); 269 EmitFinalDestCopy(E, RV); 270} 271 272void AggExprEmitter::VisitObjCMessageExpr(ObjCMessageExpr *E) { 273 RValue RV = CGF.EmitObjCMessageExpr(E); 274 EmitFinalDestCopy(E, RV); 275} 276 277void AggExprEmitter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) { 278 RValue RV = CGF.EmitObjCPropertyGet(E); 279 EmitFinalDestCopy(E, RV); 280} 281 282void AggExprEmitter::VisitObjCImplicitSetterGetterRefExpr( 283 ObjCImplicitSetterGetterRefExpr *E) { 284 RValue RV = CGF.EmitObjCPropertyGet(E); 285 EmitFinalDestCopy(E, RV); 286} 287 288void AggExprEmitter::VisitBinComma(const BinaryOperator *E) { 289 CGF.EmitAnyExpr(E->getLHS(), 0, false, true); 290 CGF.EmitAggExpr(E->getRHS(), DestPtr, VolatileDest, 291 /*IgnoreResult=*/false, IsInitializer); 292} 293 294void AggExprEmitter::VisitUnaryAddrOf(const UnaryOperator *E) { 295 // We have a member function pointer. 296 const MemberPointerType *MPT = E->getType()->getAs<MemberPointerType>(); 297 (void) MPT; 298 assert(MPT->getPointeeType()->isFunctionProtoType() && 299 "Unexpected member pointer type!"); 300 301 const DeclRefExpr *DRE = cast<DeclRefExpr>(E->getSubExpr()); 302 const CXXMethodDecl *MD = cast<CXXMethodDecl>(DRE->getDecl()); 303 304 const llvm::Type *PtrDiffTy = 305 CGF.ConvertType(CGF.getContext().getPointerDiffType()); 306 307 llvm::Value *DstPtr = Builder.CreateStructGEP(DestPtr, 0, "dst.ptr"); 308 llvm::Value *FuncPtr; 309 310 if (MD->isVirtual()) { 311 int64_t Index = 312 CGF.CGM.getVtableInfo().getMethodVtableIndex(MD); 313 314 FuncPtr = llvm::ConstantInt::get(PtrDiffTy, Index + 1); 315 } else { 316 FuncPtr = llvm::ConstantExpr::getPtrToInt(CGF.CGM.GetAddrOfFunction(MD), 317 PtrDiffTy); 318 } 319 Builder.CreateStore(FuncPtr, DstPtr, VolatileDest); 320 321 llvm::Value *AdjPtr = Builder.CreateStructGEP(DestPtr, 1, "dst.adj"); 322 323 // The adjustment will always be 0. 324 Builder.CreateStore(llvm::ConstantInt::get(PtrDiffTy, 0), AdjPtr, 325 VolatileDest); 326} 327 328void AggExprEmitter::VisitStmtExpr(const StmtExpr *E) { 329 CGF.EmitCompoundStmt(*E->getSubStmt(), true, DestPtr, VolatileDest); 330} 331 332void AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) { 333 if (E->getOpcode() == BinaryOperator::PtrMemD || 334 E->getOpcode() == BinaryOperator::PtrMemI) 335 VisitPointerToDataMemberBinaryOperator(E); 336 else 337 CGF.ErrorUnsupported(E, "aggregate binary expression"); 338} 339 340void AggExprEmitter::VisitPointerToDataMemberBinaryOperator( 341 const BinaryOperator *E) { 342 LValue LV = CGF.EmitPointerToDataMemberBinaryExpr(E); 343 EmitFinalDestCopy(E, LV); 344} 345 346void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) { 347 // For an assignment to work, the value on the right has 348 // to be compatible with the value on the left. 349 assert(CGF.getContext().hasSameUnqualifiedType(E->getLHS()->getType(), 350 E->getRHS()->getType()) 351 && "Invalid assignment"); 352 LValue LHS = CGF.EmitLValue(E->getLHS()); 353 354 // We have to special case property setters, otherwise we must have 355 // a simple lvalue (no aggregates inside vectors, bitfields). 356 if (LHS.isPropertyRef()) { 357 llvm::Value *AggLoc = DestPtr; 358 if (!AggLoc) 359 AggLoc = CGF.CreateTempAlloca(CGF.ConvertType(E->getRHS()->getType())); 360 CGF.EmitAggExpr(E->getRHS(), AggLoc, VolatileDest); 361 CGF.EmitObjCPropertySet(LHS.getPropertyRefExpr(), 362 RValue::getAggregate(AggLoc, VolatileDest)); 363 } else if (LHS.isKVCRef()) { 364 llvm::Value *AggLoc = DestPtr; 365 if (!AggLoc) 366 AggLoc = CGF.CreateTempAlloca(CGF.ConvertType(E->getRHS()->getType())); 367 CGF.EmitAggExpr(E->getRHS(), AggLoc, VolatileDest); 368 CGF.EmitObjCPropertySet(LHS.getKVCRefExpr(), 369 RValue::getAggregate(AggLoc, VolatileDest)); 370 } else { 371 bool RequiresGCollection = false; 372 if (CGF.getContext().getLangOptions().NeXTRuntime) { 373 QualType LHSTy = E->getLHS()->getType(); 374 if (const RecordType *FDTTy = LHSTy.getTypePtr()->getAs<RecordType>()) 375 RequiresGCollection = FDTTy->getDecl()->hasObjectMember(); 376 } 377 // Codegen the RHS so that it stores directly into the LHS. 378 CGF.EmitAggExpr(E->getRHS(), LHS.getAddress(), LHS.isVolatileQualified(), 379 false, false, RequiresGCollection); 380 EmitFinalDestCopy(E, LHS, true); 381 } 382} 383 384void AggExprEmitter::VisitConditionalOperator(const ConditionalOperator *E) { 385 llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true"); 386 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false"); 387 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end"); 388 389 llvm::Value *Cond = CGF.EvaluateExprAsBool(E->getCond()); 390 Builder.CreateCondBr(Cond, LHSBlock, RHSBlock); 391 392 CGF.PushConditionalTempDestruction(); 393 CGF.EmitBlock(LHSBlock); 394 395 // Handle the GNU extension for missing LHS. 396 assert(E->getLHS() && "Must have LHS for aggregate value"); 397 398 Visit(E->getLHS()); 399 CGF.PopConditionalTempDestruction(); 400 CGF.EmitBranch(ContBlock); 401 402 CGF.PushConditionalTempDestruction(); 403 CGF.EmitBlock(RHSBlock); 404 405 Visit(E->getRHS()); 406 CGF.PopConditionalTempDestruction(); 407 CGF.EmitBranch(ContBlock); 408 409 CGF.EmitBlock(ContBlock); 410} 411 412void AggExprEmitter::VisitChooseExpr(const ChooseExpr *CE) { 413 Visit(CE->getChosenSubExpr(CGF.getContext())); 414} 415 416void AggExprEmitter::VisitVAArgExpr(VAArgExpr *VE) { 417 llvm::Value *ArgValue = CGF.EmitVAListRef(VE->getSubExpr()); 418 llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType()); 419 420 if (!ArgPtr) { 421 CGF.ErrorUnsupported(VE, "aggregate va_arg expression"); 422 return; 423 } 424 425 EmitFinalDestCopy(VE, LValue::MakeAddr(ArgPtr, Qualifiers())); 426} 427 428void AggExprEmitter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) { 429 llvm::Value *Val = DestPtr; 430 431 if (!Val) { 432 // Create a temporary variable. 433 Val = CGF.CreateTempAlloca(CGF.ConvertTypeForMem(E->getType()), "tmp"); 434 435 // FIXME: volatile 436 CGF.EmitAggExpr(E->getSubExpr(), Val, false); 437 } else 438 Visit(E->getSubExpr()); 439 440 // Don't make this a live temporary if we're emitting an initializer expr. 441 if (!IsInitializer) 442 CGF.PushCXXTemporary(E->getTemporary(), Val); 443} 444 445void 446AggExprEmitter::VisitCXXConstructExpr(const CXXConstructExpr *E) { 447 llvm::Value *Val = DestPtr; 448 449 if (!Val) { 450 // Create a temporary variable. 451 Val = CGF.CreateTempAlloca(CGF.ConvertTypeForMem(E->getType()), "tmp"); 452 } 453 454 CGF.EmitCXXConstructExpr(Val, E); 455} 456 457void AggExprEmitter::VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E) { 458 CGF.EmitCXXExprWithTemporaries(E, DestPtr, VolatileDest, IsInitializer); 459} 460 461void AggExprEmitter::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) { 462 LValue lvalue = LValue::MakeAddr(DestPtr, Qualifiers()); 463 EmitNullInitializationToLValue(lvalue, E->getType()); 464} 465 466void AggExprEmitter::EmitInitializationToLValue(Expr* E, LValue LV) { 467 // FIXME: Ignore result? 468 // FIXME: Are initializers affected by volatile? 469 if (isa<ImplicitValueInitExpr>(E)) { 470 EmitNullInitializationToLValue(LV, E->getType()); 471 } else if (E->getType()->isComplexType()) { 472 CGF.EmitComplexExprIntoAddr(E, LV.getAddress(), false); 473 } else if (CGF.hasAggregateLLVMType(E->getType())) { 474 CGF.EmitAnyExpr(E, LV.getAddress(), false); 475 } else { 476 CGF.EmitStoreThroughLValue(CGF.EmitAnyExpr(E), LV, E->getType()); 477 } 478} 479 480void AggExprEmitter::EmitNullInitializationToLValue(LValue LV, QualType T) { 481 if (!CGF.hasAggregateLLVMType(T)) { 482 // For non-aggregates, we can store zero 483 llvm::Value *Null = llvm::Constant::getNullValue(CGF.ConvertType(T)); 484 CGF.EmitStoreThroughLValue(RValue::get(Null), LV, T); 485 } else { 486 // Otherwise, just memset the whole thing to zero. This is legal 487 // because in LLVM, all default initializers are guaranteed to have a 488 // bit pattern of all zeros. 489 // FIXME: That isn't true for member pointers! 490 // There's a potential optimization opportunity in combining 491 // memsets; that would be easy for arrays, but relatively 492 // difficult for structures with the current code. 493 CGF.EmitMemSetToZero(LV.getAddress(), T); 494 } 495} 496 497void AggExprEmitter::VisitInitListExpr(InitListExpr *E) { 498#if 0 499 // FIXME: Disabled while we figure out what to do about 500 // test/CodeGen/bitfield.c 501 // 502 // If we can, prefer a copy from a global; this is a lot less code for long 503 // globals, and it's easier for the current optimizers to analyze. 504 // FIXME: Should we really be doing this? Should we try to avoid cases where 505 // we emit a global with a lot of zeros? Should we try to avoid short 506 // globals? 507 if (E->isConstantInitializer(CGF.getContext(), 0)) { 508 llvm::Constant* C = CGF.CGM.EmitConstantExpr(E, &CGF); 509 llvm::GlobalVariable* GV = 510 new llvm::GlobalVariable(C->getType(), true, 511 llvm::GlobalValue::InternalLinkage, 512 C, "", &CGF.CGM.getModule(), 0); 513 EmitFinalDestCopy(E, LValue::MakeAddr(GV, 0)); 514 return; 515 } 516#endif 517 if (E->hadArrayRangeDesignator()) { 518 CGF.ErrorUnsupported(E, "GNU array range designator extension"); 519 } 520 521 // Handle initialization of an array. 522 if (E->getType()->isArrayType()) { 523 const llvm::PointerType *APType = 524 cast<llvm::PointerType>(DestPtr->getType()); 525 const llvm::ArrayType *AType = 526 cast<llvm::ArrayType>(APType->getElementType()); 527 528 uint64_t NumInitElements = E->getNumInits(); 529 530 if (E->getNumInits() > 0) { 531 QualType T1 = E->getType(); 532 QualType T2 = E->getInit(0)->getType(); 533 if (CGF.getContext().hasSameUnqualifiedType(T1, T2)) { 534 EmitAggLoadOfLValue(E->getInit(0)); 535 return; 536 } 537 } 538 539 uint64_t NumArrayElements = AType->getNumElements(); 540 QualType ElementType = CGF.getContext().getCanonicalType(E->getType()); 541 ElementType = CGF.getContext().getAsArrayType(ElementType)->getElementType(); 542 543 // FIXME: were we intentionally ignoring address spaces and GC attributes? 544 Qualifiers Quals = CGF.MakeQualifiers(ElementType); 545 546 for (uint64_t i = 0; i != NumArrayElements; ++i) { 547 llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array"); 548 if (i < NumInitElements) 549 EmitInitializationToLValue(E->getInit(i), 550 LValue::MakeAddr(NextVal, Quals)); 551 else 552 EmitNullInitializationToLValue(LValue::MakeAddr(NextVal, Quals), 553 ElementType); 554 } 555 return; 556 } 557 558 assert(E->getType()->isRecordType() && "Only support structs/unions here!"); 559 560 // Do struct initialization; this code just sets each individual member 561 // to the approprate value. This makes bitfield support automatic; 562 // the disadvantage is that the generated code is more difficult for 563 // the optimizer, especially with bitfields. 564 unsigned NumInitElements = E->getNumInits(); 565 RecordDecl *SD = E->getType()->getAs<RecordType>()->getDecl(); 566 unsigned CurInitVal = 0; 567 568 if (E->getType()->isUnionType()) { 569 // Only initialize one field of a union. The field itself is 570 // specified by the initializer list. 571 if (!E->getInitializedFieldInUnion()) { 572 // Empty union; we have nothing to do. 573 574#ifndef NDEBUG 575 // Make sure that it's really an empty and not a failure of 576 // semantic analysis. 577 for (RecordDecl::field_iterator Field = SD->field_begin(), 578 FieldEnd = SD->field_end(); 579 Field != FieldEnd; ++Field) 580 assert(Field->isUnnamedBitfield() && "Only unnamed bitfields allowed"); 581#endif 582 return; 583 } 584 585 // FIXME: volatility 586 FieldDecl *Field = E->getInitializedFieldInUnion(); 587 LValue FieldLoc = CGF.EmitLValueForField(DestPtr, Field, true, 0); 588 589 if (NumInitElements) { 590 // Store the initializer into the field 591 EmitInitializationToLValue(E->getInit(0), FieldLoc); 592 } else { 593 // Default-initialize to null 594 EmitNullInitializationToLValue(FieldLoc, Field->getType()); 595 } 596 597 return; 598 } 599 600 // Here we iterate over the fields; this makes it simpler to both 601 // default-initialize fields and skip over unnamed fields. 602 for (RecordDecl::field_iterator Field = SD->field_begin(), 603 FieldEnd = SD->field_end(); 604 Field != FieldEnd; ++Field) { 605 // We're done once we hit the flexible array member 606 if (Field->getType()->isIncompleteArrayType()) 607 break; 608 609 if (Field->isUnnamedBitfield()) 610 continue; 611 612 // FIXME: volatility 613 LValue FieldLoc = CGF.EmitLValueForField(DestPtr, *Field, false, 0); 614 // We never generate write-barries for initialized fields. 615 LValue::SetObjCNonGC(FieldLoc, true); 616 if (CurInitVal < NumInitElements) { 617 // Store the initializer into the field 618 EmitInitializationToLValue(E->getInit(CurInitVal++), FieldLoc); 619 } else { 620 // We're out of initalizers; default-initialize to null 621 EmitNullInitializationToLValue(FieldLoc, Field->getType()); 622 } 623 } 624} 625 626//===----------------------------------------------------------------------===// 627// Entry Points into this File 628//===----------------------------------------------------------------------===// 629 630/// EmitAggExpr - Emit the computation of the specified expression of aggregate 631/// type. The result is computed into DestPtr. Note that if DestPtr is null, 632/// the value of the aggregate expression is not needed. If VolatileDest is 633/// true, DestPtr cannot be 0. 634void CodeGenFunction::EmitAggExpr(const Expr *E, llvm::Value *DestPtr, 635 bool VolatileDest, bool IgnoreResult, 636 bool IsInitializer, 637 bool RequiresGCollection) { 638 assert(E && hasAggregateLLVMType(E->getType()) && 639 "Invalid aggregate expression to emit"); 640 assert ((DestPtr != 0 || VolatileDest == false) 641 && "volatile aggregate can't be 0"); 642 643 AggExprEmitter(*this, DestPtr, VolatileDest, IgnoreResult, IsInitializer, 644 RequiresGCollection) 645 .Visit(const_cast<Expr*>(E)); 646} 647 648void CodeGenFunction::EmitAggregateClear(llvm::Value *DestPtr, QualType Ty) { 649 assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex"); 650 651 EmitMemSetToZero(DestPtr, Ty); 652} 653 654void CodeGenFunction::EmitAggregateCopy(llvm::Value *DestPtr, 655 llvm::Value *SrcPtr, QualType Ty, 656 bool isVolatile) { 657 assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex"); 658 659 // Aggregate assignment turns into llvm.memcpy. This is almost valid per 660 // C99 6.5.16.1p3, which states "If the value being stored in an object is 661 // read from another object that overlaps in anyway the storage of the first 662 // object, then the overlap shall be exact and the two objects shall have 663 // qualified or unqualified versions of a compatible type." 664 // 665 // memcpy is not defined if the source and destination pointers are exactly 666 // equal, but other compilers do this optimization, and almost every memcpy 667 // implementation handles this case safely. If there is a libc that does not 668 // safely handle this, we can add a target hook. 669 const llvm::Type *BP = llvm::Type::getInt8PtrTy(VMContext); 670 if (DestPtr->getType() != BP) 671 DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp"); 672 if (SrcPtr->getType() != BP) 673 SrcPtr = Builder.CreateBitCast(SrcPtr, BP, "tmp"); 674 675 // Get size and alignment info for this aggregate. 676 std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty); 677 678 // FIXME: Handle variable sized types. 679 const llvm::Type *IntPtr = 680 llvm::IntegerType::get(VMContext, LLVMPointerWidth); 681 682 // FIXME: If we have a volatile struct, the optimizer can remove what might 683 // appear to be `extra' memory ops: 684 // 685 // volatile struct { int i; } a, b; 686 // 687 // int main() { 688 // a = b; 689 // a = b; 690 // } 691 // 692 // we need to use a differnt call here. We use isVolatile to indicate when 693 // either the source or the destination is volatile. 694 Builder.CreateCall4(CGM.getMemCpyFn(), 695 DestPtr, SrcPtr, 696 // TypeInfo.first describes size in bits. 697 llvm::ConstantInt::get(IntPtr, TypeInfo.first/8), 698 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 699 TypeInfo.second/8)); 700} 701