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