1 //===--- CGBlocks.cpp - Emit LLVM Code for declarations -------------------===// 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 blocks. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "CGBlocks.h" 15 #include "CGDebugInfo.h" 16 #include "CGObjCRuntime.h" 17 #include "CodeGenFunction.h" 18 #include "CodeGenModule.h" 19 #include "clang/AST/DeclObjC.h" 20 #include "llvm/ADT/SmallSet.h" 21 #include "llvm/IR/DataLayout.h" 22 #include "llvm/IR/Module.h" 23 #include <algorithm> 24 #include <cstdio> 25 26 using namespace clang; 27 using namespace CodeGen; 28 29 CGBlockInfo::CGBlockInfo(const BlockDecl *block, StringRef name) 30 : Name(name), CXXThisIndex(0), CanBeGlobal(false), NeedsCopyDispose(false), 31 HasCXXObject(false), UsesStret(false), HasCapturedVariableLayout(false), 32 StructureType(0), Block(block), 33 DominatingIP(0) { 34 35 // Skip asm prefix, if any. 'name' is usually taken directly from 36 // the mangled name of the enclosing function. 37 if (!name.empty() && name[0] == '\01') 38 name = name.substr(1); 39 } 40 41 // Anchor the vtable to this translation unit. 42 CodeGenModule::ByrefHelpers::~ByrefHelpers() {} 43 44 /// Build the given block as a global block. 45 static llvm::Constant *buildGlobalBlock(CodeGenModule &CGM, 46 const CGBlockInfo &blockInfo, 47 llvm::Constant *blockFn); 48 49 /// Build the helper function to copy a block. 50 static llvm::Constant *buildCopyHelper(CodeGenModule &CGM, 51 const CGBlockInfo &blockInfo) { 52 return CodeGenFunction(CGM).GenerateCopyHelperFunction(blockInfo); 53 } 54 55 /// Build the helper function to dipose of a block. 56 static llvm::Constant *buildDisposeHelper(CodeGenModule &CGM, 57 const CGBlockInfo &blockInfo) { 58 return CodeGenFunction(CGM).GenerateDestroyHelperFunction(blockInfo); 59 } 60 61 /// buildBlockDescriptor - Build the block descriptor meta-data for a block. 62 /// buildBlockDescriptor is accessed from 5th field of the Block_literal 63 /// meta-data and contains stationary information about the block literal. 64 /// Its definition will have 4 (or optinally 6) words. 65 /// struct Block_descriptor { 66 /// unsigned long reserved; 67 /// unsigned long size; // size of Block_literal metadata in bytes. 68 /// void *copy_func_helper_decl; // optional copy helper. 69 /// void *destroy_func_decl; // optioanl destructor helper. 70 /// void *block_method_encoding_address;//@encode for block literal signature. 71 /// void *block_layout_info; // encoding of captured block variables. 72 /// }; 73 static llvm::Constant *buildBlockDescriptor(CodeGenModule &CGM, 74 const CGBlockInfo &blockInfo) { 75 ASTContext &C = CGM.getContext(); 76 77 llvm::Type *ulong = CGM.getTypes().ConvertType(C.UnsignedLongTy); 78 llvm::Type *i8p = CGM.getTypes().ConvertType(C.VoidPtrTy); 79 80 SmallVector<llvm::Constant*, 6> elements; 81 82 // reserved 83 elements.push_back(llvm::ConstantInt::get(ulong, 0)); 84 85 // Size 86 // FIXME: What is the right way to say this doesn't fit? We should give 87 // a user diagnostic in that case. Better fix would be to change the 88 // API to size_t. 89 elements.push_back(llvm::ConstantInt::get(ulong, 90 blockInfo.BlockSize.getQuantity())); 91 92 // Optional copy/dispose helpers. 93 if (blockInfo.NeedsCopyDispose) { 94 // copy_func_helper_decl 95 elements.push_back(buildCopyHelper(CGM, blockInfo)); 96 97 // destroy_func_decl 98 elements.push_back(buildDisposeHelper(CGM, blockInfo)); 99 } 100 101 // Signature. Mandatory ObjC-style method descriptor @encode sequence. 102 std::string typeAtEncoding = 103 CGM.getContext().getObjCEncodingForBlock(blockInfo.getBlockExpr()); 104 elements.push_back(llvm::ConstantExpr::getBitCast( 105 CGM.GetAddrOfConstantCString(typeAtEncoding), i8p)); 106 107 // GC layout. 108 if (C.getLangOpts().ObjC1) { 109 if (CGM.getLangOpts().getGC() != LangOptions::NonGC) 110 elements.push_back(CGM.getObjCRuntime().BuildGCBlockLayout(CGM, blockInfo)); 111 else 112 elements.push_back(CGM.getObjCRuntime().BuildRCBlockLayout(CGM, blockInfo)); 113 } 114 else 115 elements.push_back(llvm::Constant::getNullValue(i8p)); 116 117 llvm::Constant *init = llvm::ConstantStruct::getAnon(elements); 118 119 llvm::GlobalVariable *global = 120 new llvm::GlobalVariable(CGM.getModule(), init->getType(), true, 121 llvm::GlobalValue::InternalLinkage, 122 init, "__block_descriptor_tmp"); 123 124 return llvm::ConstantExpr::getBitCast(global, CGM.getBlockDescriptorType()); 125 } 126 127 /* 128 Purely notional variadic template describing the layout of a block. 129 130 template <class _ResultType, class... _ParamTypes, class... _CaptureTypes> 131 struct Block_literal { 132 /// Initialized to one of: 133 /// extern void *_NSConcreteStackBlock[]; 134 /// extern void *_NSConcreteGlobalBlock[]; 135 /// 136 /// In theory, we could start one off malloc'ed by setting 137 /// BLOCK_NEEDS_FREE, giving it a refcount of 1, and using 138 /// this isa: 139 /// extern void *_NSConcreteMallocBlock[]; 140 struct objc_class *isa; 141 142 /// These are the flags (with corresponding bit number) that the 143 /// compiler is actually supposed to know about. 144 /// 25. BLOCK_HAS_COPY_DISPOSE - indicates that the block 145 /// descriptor provides copy and dispose helper functions 146 /// 26. BLOCK_HAS_CXX_OBJ - indicates that there's a captured 147 /// object with a nontrivial destructor or copy constructor 148 /// 28. BLOCK_IS_GLOBAL - indicates that the block is allocated 149 /// as global memory 150 /// 29. BLOCK_USE_STRET - indicates that the block function 151 /// uses stret, which objc_msgSend needs to know about 152 /// 30. BLOCK_HAS_SIGNATURE - indicates that the block has an 153 /// @encoded signature string 154 /// And we're not supposed to manipulate these: 155 /// 24. BLOCK_NEEDS_FREE - indicates that the block has been moved 156 /// to malloc'ed memory 157 /// 27. BLOCK_IS_GC - indicates that the block has been moved to 158 /// to GC-allocated memory 159 /// Additionally, the bottom 16 bits are a reference count which 160 /// should be zero on the stack. 161 int flags; 162 163 /// Reserved; should be zero-initialized. 164 int reserved; 165 166 /// Function pointer generated from block literal. 167 _ResultType (*invoke)(Block_literal *, _ParamTypes...); 168 169 /// Block description metadata generated from block literal. 170 struct Block_descriptor *block_descriptor; 171 172 /// Captured values follow. 173 _CapturesTypes captures...; 174 }; 175 */ 176 177 /// The number of fields in a block header. 178 const unsigned BlockHeaderSize = 5; 179 180 namespace { 181 /// A chunk of data that we actually have to capture in the block. 182 struct BlockLayoutChunk { 183 CharUnits Alignment; 184 CharUnits Size; 185 const BlockDecl::Capture *Capture; // null for 'this' 186 llvm::Type *Type; 187 188 BlockLayoutChunk(CharUnits align, CharUnits size, 189 const BlockDecl::Capture *capture, 190 llvm::Type *type) 191 : Alignment(align), Size(size), Capture(capture), Type(type) {} 192 193 /// Tell the block info that this chunk has the given field index. 194 void setIndex(CGBlockInfo &info, unsigned index) { 195 if (!Capture) 196 info.CXXThisIndex = index; 197 else 198 info.Captures[Capture->getVariable()] 199 = CGBlockInfo::Capture::makeIndex(index); 200 } 201 }; 202 203 /// Order by descending alignment. 204 bool operator<(const BlockLayoutChunk &left, const BlockLayoutChunk &right) { 205 return left.Alignment > right.Alignment; 206 } 207 } 208 209 /// Determines if the given type is safe for constant capture in C++. 210 static bool isSafeForCXXConstantCapture(QualType type) { 211 const RecordType *recordType = 212 type->getBaseElementTypeUnsafe()->getAs<RecordType>(); 213 214 // Only records can be unsafe. 215 if (!recordType) return true; 216 217 const CXXRecordDecl *record = cast<CXXRecordDecl>(recordType->getDecl()); 218 219 // Maintain semantics for classes with non-trivial dtors or copy ctors. 220 if (!record->hasTrivialDestructor()) return false; 221 if (record->hasNonTrivialCopyConstructor()) return false; 222 223 // Otherwise, we just have to make sure there aren't any mutable 224 // fields that might have changed since initialization. 225 return !record->hasMutableFields(); 226 } 227 228 /// It is illegal to modify a const object after initialization. 229 /// Therefore, if a const object has a constant initializer, we don't 230 /// actually need to keep storage for it in the block; we'll just 231 /// rematerialize it at the start of the block function. This is 232 /// acceptable because we make no promises about address stability of 233 /// captured variables. 234 static llvm::Constant *tryCaptureAsConstant(CodeGenModule &CGM, 235 CodeGenFunction *CGF, 236 const VarDecl *var) { 237 QualType type = var->getType(); 238 239 // We can only do this if the variable is const. 240 if (!type.isConstQualified()) return 0; 241 242 // Furthermore, in C++ we have to worry about mutable fields: 243 // C++ [dcl.type.cv]p4: 244 // Except that any class member declared mutable can be 245 // modified, any attempt to modify a const object during its 246 // lifetime results in undefined behavior. 247 if (CGM.getLangOpts().CPlusPlus && !isSafeForCXXConstantCapture(type)) 248 return 0; 249 250 // If the variable doesn't have any initializer (shouldn't this be 251 // invalid?), it's not clear what we should do. Maybe capture as 252 // zero? 253 const Expr *init = var->getInit(); 254 if (!init) return 0; 255 256 return CGM.EmitConstantInit(*var, CGF); 257 } 258 259 /// Get the low bit of a nonzero character count. This is the 260 /// alignment of the nth byte if the 0th byte is universally aligned. 261 static CharUnits getLowBit(CharUnits v) { 262 return CharUnits::fromQuantity(v.getQuantity() & (~v.getQuantity() + 1)); 263 } 264 265 static void initializeForBlockHeader(CodeGenModule &CGM, CGBlockInfo &info, 266 SmallVectorImpl<llvm::Type*> &elementTypes) { 267 ASTContext &C = CGM.getContext(); 268 269 // The header is basically a 'struct { void *; int; int; void *; void *; }'. 270 CharUnits ptrSize, ptrAlign, intSize, intAlign; 271 llvm::tie(ptrSize, ptrAlign) = C.getTypeInfoInChars(C.VoidPtrTy); 272 llvm::tie(intSize, intAlign) = C.getTypeInfoInChars(C.IntTy); 273 274 // Are there crazy embedded platforms where this isn't true? 275 assert(intSize <= ptrSize && "layout assumptions horribly violated"); 276 277 CharUnits headerSize = ptrSize; 278 if (2 * intSize < ptrAlign) headerSize += ptrSize; 279 else headerSize += 2 * intSize; 280 headerSize += 2 * ptrSize; 281 282 info.BlockAlign = ptrAlign; 283 info.BlockSize = headerSize; 284 285 assert(elementTypes.empty()); 286 llvm::Type *i8p = CGM.getTypes().ConvertType(C.VoidPtrTy); 287 llvm::Type *intTy = CGM.getTypes().ConvertType(C.IntTy); 288 elementTypes.push_back(i8p); 289 elementTypes.push_back(intTy); 290 elementTypes.push_back(intTy); 291 elementTypes.push_back(i8p); 292 elementTypes.push_back(CGM.getBlockDescriptorType()); 293 294 assert(elementTypes.size() == BlockHeaderSize); 295 } 296 297 /// Compute the layout of the given block. Attempts to lay the block 298 /// out with minimal space requirements. 299 static void computeBlockInfo(CodeGenModule &CGM, CodeGenFunction *CGF, 300 CGBlockInfo &info) { 301 ASTContext &C = CGM.getContext(); 302 const BlockDecl *block = info.getBlockDecl(); 303 304 SmallVector<llvm::Type*, 8> elementTypes; 305 initializeForBlockHeader(CGM, info, elementTypes); 306 307 if (!block->hasCaptures()) { 308 info.StructureType = 309 llvm::StructType::get(CGM.getLLVMContext(), elementTypes, true); 310 info.CanBeGlobal = true; 311 return; 312 } 313 else if (C.getLangOpts().ObjC1 && 314 CGM.getLangOpts().getGC() == LangOptions::NonGC) 315 info.HasCapturedVariableLayout = true; 316 317 // Collect the layout chunks. 318 SmallVector<BlockLayoutChunk, 16> layout; 319 layout.reserve(block->capturesCXXThis() + 320 (block->capture_end() - block->capture_begin())); 321 322 CharUnits maxFieldAlign; 323 324 // First, 'this'. 325 if (block->capturesCXXThis()) { 326 const DeclContext *DC = block->getDeclContext(); 327 for (; isa<BlockDecl>(DC); DC = cast<BlockDecl>(DC)->getDeclContext()) 328 ; 329 QualType thisType; 330 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(DC)) 331 thisType = C.getPointerType(C.getRecordType(RD)); 332 else 333 thisType = cast<CXXMethodDecl>(DC)->getThisType(C); 334 335 llvm::Type *llvmType = CGM.getTypes().ConvertType(thisType); 336 std::pair<CharUnits,CharUnits> tinfo 337 = CGM.getContext().getTypeInfoInChars(thisType); 338 maxFieldAlign = std::max(maxFieldAlign, tinfo.second); 339 340 layout.push_back(BlockLayoutChunk(tinfo.second, tinfo.first, 0, llvmType)); 341 } 342 343 // Next, all the block captures. 344 for (BlockDecl::capture_const_iterator ci = block->capture_begin(), 345 ce = block->capture_end(); ci != ce; ++ci) { 346 const VarDecl *variable = ci->getVariable(); 347 348 if (ci->isByRef()) { 349 // We have to copy/dispose of the __block reference. 350 info.NeedsCopyDispose = true; 351 352 // Just use void* instead of a pointer to the byref type. 353 QualType byRefPtrTy = C.VoidPtrTy; 354 355 llvm::Type *llvmType = CGM.getTypes().ConvertType(byRefPtrTy); 356 std::pair<CharUnits,CharUnits> tinfo 357 = CGM.getContext().getTypeInfoInChars(byRefPtrTy); 358 maxFieldAlign = std::max(maxFieldAlign, tinfo.second); 359 360 layout.push_back(BlockLayoutChunk(tinfo.second, tinfo.first, 361 &*ci, llvmType)); 362 continue; 363 } 364 365 // Otherwise, build a layout chunk with the size and alignment of 366 // the declaration. 367 if (llvm::Constant *constant = tryCaptureAsConstant(CGM, CGF, variable)) { 368 info.Captures[variable] = CGBlockInfo::Capture::makeConstant(constant); 369 continue; 370 } 371 372 // If we have a lifetime qualifier, honor it for capture purposes. 373 // That includes *not* copying it if it's __unsafe_unretained. 374 if (Qualifiers::ObjCLifetime lifetime 375 = variable->getType().getObjCLifetime()) { 376 switch (lifetime) { 377 case Qualifiers::OCL_None: llvm_unreachable("impossible"); 378 case Qualifiers::OCL_ExplicitNone: 379 case Qualifiers::OCL_Autoreleasing: 380 break; 381 382 case Qualifiers::OCL_Strong: 383 case Qualifiers::OCL_Weak: 384 info.NeedsCopyDispose = true; 385 } 386 387 // Block pointers require copy/dispose. So do Objective-C pointers. 388 } else if (variable->getType()->isObjCRetainableType()) { 389 info.NeedsCopyDispose = true; 390 391 // So do types that require non-trivial copy construction. 392 } else if (ci->hasCopyExpr()) { 393 info.NeedsCopyDispose = true; 394 info.HasCXXObject = true; 395 396 // And so do types with destructors. 397 } else if (CGM.getLangOpts().CPlusPlus) { 398 if (const CXXRecordDecl *record = 399 variable->getType()->getAsCXXRecordDecl()) { 400 if (!record->hasTrivialDestructor()) { 401 info.HasCXXObject = true; 402 info.NeedsCopyDispose = true; 403 } 404 } 405 } 406 407 QualType VT = variable->getType(); 408 CharUnits size = C.getTypeSizeInChars(VT); 409 CharUnits align = C.getDeclAlign(variable); 410 411 maxFieldAlign = std::max(maxFieldAlign, align); 412 413 llvm::Type *llvmType = 414 CGM.getTypes().ConvertTypeForMem(VT); 415 416 layout.push_back(BlockLayoutChunk(align, size, &*ci, llvmType)); 417 } 418 419 // If that was everything, we're done here. 420 if (layout.empty()) { 421 info.StructureType = 422 llvm::StructType::get(CGM.getLLVMContext(), elementTypes, true); 423 info.CanBeGlobal = true; 424 return; 425 } 426 427 // Sort the layout by alignment. We have to use a stable sort here 428 // to get reproducible results. There should probably be an 429 // llvm::array_pod_stable_sort. 430 std::stable_sort(layout.begin(), layout.end()); 431 432 // Needed for blocks layout info. 433 info.BlockHeaderForcedGapOffset = info.BlockSize; 434 info.BlockHeaderForcedGapSize = CharUnits::Zero(); 435 436 CharUnits &blockSize = info.BlockSize; 437 info.BlockAlign = std::max(maxFieldAlign, info.BlockAlign); 438 439 // Assuming that the first byte in the header is maximally aligned, 440 // get the alignment of the first byte following the header. 441 CharUnits endAlign = getLowBit(blockSize); 442 443 // If the end of the header isn't satisfactorily aligned for the 444 // maximum thing, look for things that are okay with the header-end 445 // alignment, and keep appending them until we get something that's 446 // aligned right. This algorithm is only guaranteed optimal if 447 // that condition is satisfied at some point; otherwise we can get 448 // things like: 449 // header // next byte has alignment 4 450 // something_with_size_5; // next byte has alignment 1 451 // something_with_alignment_8; 452 // which has 7 bytes of padding, as opposed to the naive solution 453 // which might have less (?). 454 if (endAlign < maxFieldAlign) { 455 SmallVectorImpl<BlockLayoutChunk>::iterator 456 li = layout.begin() + 1, le = layout.end(); 457 458 // Look for something that the header end is already 459 // satisfactorily aligned for. 460 for (; li != le && endAlign < li->Alignment; ++li) 461 ; 462 463 // If we found something that's naturally aligned for the end of 464 // the header, keep adding things... 465 if (li != le) { 466 SmallVectorImpl<BlockLayoutChunk>::iterator first = li; 467 for (; li != le; ++li) { 468 assert(endAlign >= li->Alignment); 469 470 li->setIndex(info, elementTypes.size()); 471 elementTypes.push_back(li->Type); 472 blockSize += li->Size; 473 endAlign = getLowBit(blockSize); 474 475 // ...until we get to the alignment of the maximum field. 476 if (endAlign >= maxFieldAlign) { 477 if (li == first) { 478 // No user field was appended. So, a gap was added. 479 // Save total gap size for use in block layout bit map. 480 info.BlockHeaderForcedGapSize = li->Size; 481 } 482 break; 483 } 484 } 485 // Don't re-append everything we just appended. 486 layout.erase(first, li); 487 } 488 } 489 490 assert(endAlign == getLowBit(blockSize)); 491 492 // At this point, we just have to add padding if the end align still 493 // isn't aligned right. 494 if (endAlign < maxFieldAlign) { 495 CharUnits newBlockSize = blockSize.RoundUpToAlignment(maxFieldAlign); 496 CharUnits padding = newBlockSize - blockSize; 497 498 elementTypes.push_back(llvm::ArrayType::get(CGM.Int8Ty, 499 padding.getQuantity())); 500 blockSize = newBlockSize; 501 endAlign = getLowBit(blockSize); // might be > maxFieldAlign 502 } 503 504 assert(endAlign >= maxFieldAlign); 505 assert(endAlign == getLowBit(blockSize)); 506 // Slam everything else on now. This works because they have 507 // strictly decreasing alignment and we expect that size is always a 508 // multiple of alignment. 509 for (SmallVectorImpl<BlockLayoutChunk>::iterator 510 li = layout.begin(), le = layout.end(); li != le; ++li) { 511 assert(endAlign >= li->Alignment); 512 li->setIndex(info, elementTypes.size()); 513 elementTypes.push_back(li->Type); 514 blockSize += li->Size; 515 endAlign = getLowBit(blockSize); 516 } 517 518 info.StructureType = 519 llvm::StructType::get(CGM.getLLVMContext(), elementTypes, true); 520 } 521 522 /// Enter the scope of a block. This should be run at the entrance to 523 /// a full-expression so that the block's cleanups are pushed at the 524 /// right place in the stack. 525 static void enterBlockScope(CodeGenFunction &CGF, BlockDecl *block) { 526 assert(CGF.HaveInsertPoint()); 527 528 // Allocate the block info and place it at the head of the list. 529 CGBlockInfo &blockInfo = 530 *new CGBlockInfo(block, CGF.CurFn->getName()); 531 blockInfo.NextBlockInfo = CGF.FirstBlockInfo; 532 CGF.FirstBlockInfo = &blockInfo; 533 534 // Compute information about the layout, etc., of this block, 535 // pushing cleanups as necessary. 536 computeBlockInfo(CGF.CGM, &CGF, blockInfo); 537 538 // Nothing else to do if it can be global. 539 if (blockInfo.CanBeGlobal) return; 540 541 // Make the allocation for the block. 542 blockInfo.Address = 543 CGF.CreateTempAlloca(blockInfo.StructureType, "block"); 544 blockInfo.Address->setAlignment(blockInfo.BlockAlign.getQuantity()); 545 546 // If there are cleanups to emit, enter them (but inactive). 547 if (!blockInfo.NeedsCopyDispose) return; 548 549 // Walk through the captures (in order) and find the ones not 550 // captured by constant. 551 for (BlockDecl::capture_const_iterator ci = block->capture_begin(), 552 ce = block->capture_end(); ci != ce; ++ci) { 553 // Ignore __block captures; there's nothing special in the 554 // on-stack block that we need to do for them. 555 if (ci->isByRef()) continue; 556 557 // Ignore variables that are constant-captured. 558 const VarDecl *variable = ci->getVariable(); 559 CGBlockInfo::Capture &capture = blockInfo.getCapture(variable); 560 if (capture.isConstant()) continue; 561 562 // Ignore objects that aren't destructed. 563 QualType::DestructionKind dtorKind = 564 variable->getType().isDestructedType(); 565 if (dtorKind == QualType::DK_none) continue; 566 567 CodeGenFunction::Destroyer *destroyer; 568 569 // Block captures count as local values and have imprecise semantics. 570 // They also can't be arrays, so need to worry about that. 571 if (dtorKind == QualType::DK_objc_strong_lifetime) { 572 destroyer = CodeGenFunction::destroyARCStrongImprecise; 573 } else { 574 destroyer = CGF.getDestroyer(dtorKind); 575 } 576 577 // GEP down to the address. 578 llvm::Value *addr = CGF.Builder.CreateStructGEP(blockInfo.Address, 579 capture.getIndex()); 580 581 // We can use that GEP as the dominating IP. 582 if (!blockInfo.DominatingIP) 583 blockInfo.DominatingIP = cast<llvm::Instruction>(addr); 584 585 CleanupKind cleanupKind = InactiveNormalCleanup; 586 bool useArrayEHCleanup = CGF.needsEHCleanup(dtorKind); 587 if (useArrayEHCleanup) 588 cleanupKind = InactiveNormalAndEHCleanup; 589 590 CGF.pushDestroy(cleanupKind, addr, variable->getType(), 591 destroyer, useArrayEHCleanup); 592 593 // Remember where that cleanup was. 594 capture.setCleanup(CGF.EHStack.stable_begin()); 595 } 596 } 597 598 /// Enter a full-expression with a non-trivial number of objects to 599 /// clean up. This is in this file because, at the moment, the only 600 /// kind of cleanup object is a BlockDecl*. 601 void CodeGenFunction::enterNonTrivialFullExpression(const ExprWithCleanups *E) { 602 assert(E->getNumObjects() != 0); 603 ArrayRef<ExprWithCleanups::CleanupObject> cleanups = E->getObjects(); 604 for (ArrayRef<ExprWithCleanups::CleanupObject>::iterator 605 i = cleanups.begin(), e = cleanups.end(); i != e; ++i) { 606 enterBlockScope(*this, *i); 607 } 608 } 609 610 /// Find the layout for the given block in a linked list and remove it. 611 static CGBlockInfo *findAndRemoveBlockInfo(CGBlockInfo **head, 612 const BlockDecl *block) { 613 while (true) { 614 assert(head && *head); 615 CGBlockInfo *cur = *head; 616 617 // If this is the block we're looking for, splice it out of the list. 618 if (cur->getBlockDecl() == block) { 619 *head = cur->NextBlockInfo; 620 return cur; 621 } 622 623 head = &cur->NextBlockInfo; 624 } 625 } 626 627 /// Destroy a chain of block layouts. 628 void CodeGenFunction::destroyBlockInfos(CGBlockInfo *head) { 629 assert(head && "destroying an empty chain"); 630 do { 631 CGBlockInfo *cur = head; 632 head = cur->NextBlockInfo; 633 delete cur; 634 } while (head != 0); 635 } 636 637 /// Emit a block literal expression in the current function. 638 llvm::Value *CodeGenFunction::EmitBlockLiteral(const BlockExpr *blockExpr) { 639 // If the block has no captures, we won't have a pre-computed 640 // layout for it. 641 if (!blockExpr->getBlockDecl()->hasCaptures()) { 642 CGBlockInfo blockInfo(blockExpr->getBlockDecl(), CurFn->getName()); 643 computeBlockInfo(CGM, this, blockInfo); 644 blockInfo.BlockExpression = blockExpr; 645 return EmitBlockLiteral(blockInfo); 646 } 647 648 // Find the block info for this block and take ownership of it. 649 OwningPtr<CGBlockInfo> blockInfo; 650 blockInfo.reset(findAndRemoveBlockInfo(&FirstBlockInfo, 651 blockExpr->getBlockDecl())); 652 653 blockInfo->BlockExpression = blockExpr; 654 return EmitBlockLiteral(*blockInfo); 655 } 656 657 llvm::Value *CodeGenFunction::EmitBlockLiteral(const CGBlockInfo &blockInfo) { 658 // Using the computed layout, generate the actual block function. 659 bool isLambdaConv = blockInfo.getBlockDecl()->isConversionFromLambda(); 660 llvm::Constant *blockFn 661 = CodeGenFunction(CGM, true).GenerateBlockFunction(CurGD, blockInfo, 662 CurFuncDecl, LocalDeclMap, 663 isLambdaConv); 664 blockFn = llvm::ConstantExpr::getBitCast(blockFn, VoidPtrTy); 665 666 // If there is nothing to capture, we can emit this as a global block. 667 if (blockInfo.CanBeGlobal) 668 return buildGlobalBlock(CGM, blockInfo, blockFn); 669 670 // Otherwise, we have to emit this as a local block. 671 672 llvm::Constant *isa = CGM.getNSConcreteStackBlock(); 673 isa = llvm::ConstantExpr::getBitCast(isa, VoidPtrTy); 674 675 // Build the block descriptor. 676 llvm::Constant *descriptor = buildBlockDescriptor(CGM, blockInfo); 677 678 llvm::AllocaInst *blockAddr = blockInfo.Address; 679 assert(blockAddr && "block has no address!"); 680 681 // Compute the initial on-stack block flags. 682 BlockFlags flags = BLOCK_HAS_SIGNATURE; 683 if (blockInfo.HasCapturedVariableLayout) flags |= BLOCK_HAS_EXTENDED_LAYOUT; 684 if (blockInfo.NeedsCopyDispose) flags |= BLOCK_HAS_COPY_DISPOSE; 685 if (blockInfo.HasCXXObject) flags |= BLOCK_HAS_CXX_OBJ; 686 if (blockInfo.UsesStret) flags |= BLOCK_USE_STRET; 687 688 // Initialize the block literal. 689 Builder.CreateStore(isa, Builder.CreateStructGEP(blockAddr, 0, "block.isa")); 690 Builder.CreateStore(llvm::ConstantInt::get(IntTy, flags.getBitMask()), 691 Builder.CreateStructGEP(blockAddr, 1, "block.flags")); 692 Builder.CreateStore(llvm::ConstantInt::get(IntTy, 0), 693 Builder.CreateStructGEP(blockAddr, 2, "block.reserved")); 694 Builder.CreateStore(blockFn, Builder.CreateStructGEP(blockAddr, 3, 695 "block.invoke")); 696 Builder.CreateStore(descriptor, Builder.CreateStructGEP(blockAddr, 4, 697 "block.descriptor")); 698 699 // Finally, capture all the values into the block. 700 const BlockDecl *blockDecl = blockInfo.getBlockDecl(); 701 702 // First, 'this'. 703 if (blockDecl->capturesCXXThis()) { 704 llvm::Value *addr = Builder.CreateStructGEP(blockAddr, 705 blockInfo.CXXThisIndex, 706 "block.captured-this.addr"); 707 Builder.CreateStore(LoadCXXThis(), addr); 708 } 709 710 // Next, captured variables. 711 for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(), 712 ce = blockDecl->capture_end(); ci != ce; ++ci) { 713 const VarDecl *variable = ci->getVariable(); 714 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable); 715 716 // Ignore constant captures. 717 if (capture.isConstant()) continue; 718 719 QualType type = variable->getType(); 720 721 // This will be a [[type]]*, except that a byref entry will just be 722 // an i8**. 723 llvm::Value *blockField = 724 Builder.CreateStructGEP(blockAddr, capture.getIndex(), 725 "block.captured"); 726 727 // Compute the address of the thing we're going to move into the 728 // block literal. 729 llvm::Value *src; 730 if (BlockInfo && ci->isNested()) { 731 // We need to use the capture from the enclosing block. 732 const CGBlockInfo::Capture &enclosingCapture = 733 BlockInfo->getCapture(variable); 734 735 // This is a [[type]]*, except that a byref entry wil just be an i8**. 736 src = Builder.CreateStructGEP(LoadBlockStruct(), 737 enclosingCapture.getIndex(), 738 "block.capture.addr"); 739 } else if (blockDecl->isConversionFromLambda()) { 740 // The lambda capture in a lambda's conversion-to-block-pointer is 741 // special; we'll simply emit it directly. 742 src = 0; 743 } else { 744 // This is a [[type]]*. 745 src = LocalDeclMap[variable]; 746 } 747 748 // For byrefs, we just write the pointer to the byref struct into 749 // the block field. There's no need to chase the forwarding 750 // pointer at this point, since we're building something that will 751 // live a shorter life than the stack byref anyway. 752 if (ci->isByRef()) { 753 // Get a void* that points to the byref struct. 754 if (ci->isNested()) 755 src = Builder.CreateLoad(src, "byref.capture"); 756 else 757 src = Builder.CreateBitCast(src, VoidPtrTy); 758 759 // Write that void* into the capture field. 760 Builder.CreateStore(src, blockField); 761 762 // If we have a copy constructor, evaluate that into the block field. 763 } else if (const Expr *copyExpr = ci->getCopyExpr()) { 764 if (blockDecl->isConversionFromLambda()) { 765 // If we have a lambda conversion, emit the expression 766 // directly into the block instead. 767 CharUnits Align = getContext().getTypeAlignInChars(type); 768 AggValueSlot Slot = 769 AggValueSlot::forAddr(blockField, Align, Qualifiers(), 770 AggValueSlot::IsDestructed, 771 AggValueSlot::DoesNotNeedGCBarriers, 772 AggValueSlot::IsNotAliased); 773 EmitAggExpr(copyExpr, Slot); 774 } else { 775 EmitSynthesizedCXXCopyCtor(blockField, src, copyExpr); 776 } 777 778 // If it's a reference variable, copy the reference into the block field. 779 } else if (type->isReferenceType()) { 780 Builder.CreateStore(Builder.CreateLoad(src, "ref.val"), blockField); 781 782 // Otherwise, fake up a POD copy into the block field. 783 } else { 784 // Fake up a new variable so that EmitScalarInit doesn't think 785 // we're referring to the variable in its own initializer. 786 ImplicitParamDecl blockFieldPseudoVar(/*DC*/ 0, SourceLocation(), 787 /*name*/ 0, type); 788 789 // We use one of these or the other depending on whether the 790 // reference is nested. 791 DeclRefExpr declRef(const_cast<VarDecl*>(variable), 792 /*refersToEnclosing*/ ci->isNested(), type, 793 VK_LValue, SourceLocation()); 794 795 ImplicitCastExpr l2r(ImplicitCastExpr::OnStack, type, CK_LValueToRValue, 796 &declRef, VK_RValue); 797 EmitExprAsInit(&l2r, &blockFieldPseudoVar, 798 MakeAddrLValue(blockField, type, 799 getContext().getDeclAlign(variable)), 800 /*captured by init*/ false); 801 } 802 803 // Activate the cleanup if layout pushed one. 804 if (!ci->isByRef()) { 805 EHScopeStack::stable_iterator cleanup = capture.getCleanup(); 806 if (cleanup.isValid()) 807 ActivateCleanupBlock(cleanup, blockInfo.DominatingIP); 808 } 809 } 810 811 // Cast to the converted block-pointer type, which happens (somewhat 812 // unfortunately) to be a pointer to function type. 813 llvm::Value *result = 814 Builder.CreateBitCast(blockAddr, 815 ConvertType(blockInfo.getBlockExpr()->getType())); 816 817 return result; 818 } 819 820 821 llvm::Type *CodeGenModule::getBlockDescriptorType() { 822 if (BlockDescriptorType) 823 return BlockDescriptorType; 824 825 llvm::Type *UnsignedLongTy = 826 getTypes().ConvertType(getContext().UnsignedLongTy); 827 828 // struct __block_descriptor { 829 // unsigned long reserved; 830 // unsigned long block_size; 831 // 832 // // later, the following will be added 833 // 834 // struct { 835 // void (*copyHelper)(); 836 // void (*copyHelper)(); 837 // } helpers; // !!! optional 838 // 839 // const char *signature; // the block signature 840 // const char *layout; // reserved 841 // }; 842 BlockDescriptorType = 843 llvm::StructType::create("struct.__block_descriptor", 844 UnsignedLongTy, UnsignedLongTy, NULL); 845 846 // Now form a pointer to that. 847 BlockDescriptorType = llvm::PointerType::getUnqual(BlockDescriptorType); 848 return BlockDescriptorType; 849 } 850 851 llvm::Type *CodeGenModule::getGenericBlockLiteralType() { 852 if (GenericBlockLiteralType) 853 return GenericBlockLiteralType; 854 855 llvm::Type *BlockDescPtrTy = getBlockDescriptorType(); 856 857 // struct __block_literal_generic { 858 // void *__isa; 859 // int __flags; 860 // int __reserved; 861 // void (*__invoke)(void *); 862 // struct __block_descriptor *__descriptor; 863 // }; 864 GenericBlockLiteralType = 865 llvm::StructType::create("struct.__block_literal_generic", 866 VoidPtrTy, IntTy, IntTy, VoidPtrTy, 867 BlockDescPtrTy, NULL); 868 869 return GenericBlockLiteralType; 870 } 871 872 873 RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E, 874 ReturnValueSlot ReturnValue) { 875 const BlockPointerType *BPT = 876 E->getCallee()->getType()->getAs<BlockPointerType>(); 877 878 llvm::Value *Callee = EmitScalarExpr(E->getCallee()); 879 880 // Get a pointer to the generic block literal. 881 llvm::Type *BlockLiteralTy = 882 llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType()); 883 884 // Bitcast the callee to a block literal. 885 llvm::Value *BlockLiteral = 886 Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal"); 887 888 // Get the function pointer from the literal. 889 llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3); 890 891 BlockLiteral = Builder.CreateBitCast(BlockLiteral, VoidPtrTy); 892 893 // Add the block literal. 894 CallArgList Args; 895 Args.add(RValue::get(BlockLiteral), getContext().VoidPtrTy); 896 897 QualType FnType = BPT->getPointeeType(); 898 899 // And the rest of the arguments. 900 EmitCallArgs(Args, FnType->getAs<FunctionProtoType>(), 901 E->arg_begin(), E->arg_end()); 902 903 // Load the function. 904 llvm::Value *Func = Builder.CreateLoad(FuncPtr); 905 906 const FunctionType *FuncTy = FnType->castAs<FunctionType>(); 907 const CGFunctionInfo &FnInfo = 908 CGM.getTypes().arrangeBlockFunctionCall(Args, FuncTy); 909 910 // Cast the function pointer to the right type. 911 llvm::Type *BlockFTy = CGM.getTypes().GetFunctionType(FnInfo); 912 913 llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy); 914 Func = Builder.CreateBitCast(Func, BlockFTyPtr); 915 916 // And call the block. 917 return EmitCall(FnInfo, Func, ReturnValue, Args); 918 } 919 920 llvm::Value *CodeGenFunction::GetAddrOfBlockDecl(const VarDecl *variable, 921 bool isByRef) { 922 assert(BlockInfo && "evaluating block ref without block information?"); 923 const CGBlockInfo::Capture &capture = BlockInfo->getCapture(variable); 924 925 // Handle constant captures. 926 if (capture.isConstant()) return LocalDeclMap[variable]; 927 928 llvm::Value *addr = 929 Builder.CreateStructGEP(LoadBlockStruct(), capture.getIndex(), 930 "block.capture.addr"); 931 932 if (isByRef) { 933 // addr should be a void** right now. Load, then cast the result 934 // to byref*. 935 936 addr = Builder.CreateLoad(addr); 937 llvm::PointerType *byrefPointerType 938 = llvm::PointerType::get(BuildByRefType(variable), 0); 939 addr = Builder.CreateBitCast(addr, byrefPointerType, 940 "byref.addr"); 941 942 // Follow the forwarding pointer. 943 addr = Builder.CreateStructGEP(addr, 1, "byref.forwarding"); 944 addr = Builder.CreateLoad(addr, "byref.addr.forwarded"); 945 946 // Cast back to byref* and GEP over to the actual object. 947 addr = Builder.CreateBitCast(addr, byrefPointerType); 948 addr = Builder.CreateStructGEP(addr, getByRefValueLLVMField(variable), 949 variable->getNameAsString()); 950 } 951 952 if (variable->getType()->isReferenceType()) 953 addr = Builder.CreateLoad(addr, "ref.tmp"); 954 955 return addr; 956 } 957 958 llvm::Constant * 959 CodeGenModule::GetAddrOfGlobalBlock(const BlockExpr *blockExpr, 960 const char *name) { 961 CGBlockInfo blockInfo(blockExpr->getBlockDecl(), name); 962 blockInfo.BlockExpression = blockExpr; 963 964 // Compute information about the layout, etc., of this block. 965 computeBlockInfo(*this, 0, blockInfo); 966 967 // Using that metadata, generate the actual block function. 968 llvm::Constant *blockFn; 969 { 970 llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap; 971 blockFn = CodeGenFunction(*this).GenerateBlockFunction(GlobalDecl(), 972 blockInfo, 973 0, LocalDeclMap, 974 false); 975 } 976 blockFn = llvm::ConstantExpr::getBitCast(blockFn, VoidPtrTy); 977 978 return buildGlobalBlock(*this, blockInfo, blockFn); 979 } 980 981 static llvm::Constant *buildGlobalBlock(CodeGenModule &CGM, 982 const CGBlockInfo &blockInfo, 983 llvm::Constant *blockFn) { 984 assert(blockInfo.CanBeGlobal); 985 986 // Generate the constants for the block literal initializer. 987 llvm::Constant *fields[BlockHeaderSize]; 988 989 // isa 990 fields[0] = CGM.getNSConcreteGlobalBlock(); 991 992 // __flags 993 BlockFlags flags = BLOCK_IS_GLOBAL | BLOCK_HAS_SIGNATURE; 994 if (blockInfo.UsesStret) flags |= BLOCK_USE_STRET; 995 996 fields[1] = llvm::ConstantInt::get(CGM.IntTy, flags.getBitMask()); 997 998 // Reserved 999 fields[2] = llvm::Constant::getNullValue(CGM.IntTy); 1000 1001 // Function 1002 fields[3] = blockFn; 1003 1004 // Descriptor 1005 fields[4] = buildBlockDescriptor(CGM, blockInfo); 1006 1007 llvm::Constant *init = llvm::ConstantStruct::getAnon(fields); 1008 1009 llvm::GlobalVariable *literal = 1010 new llvm::GlobalVariable(CGM.getModule(), 1011 init->getType(), 1012 /*constant*/ true, 1013 llvm::GlobalVariable::InternalLinkage, 1014 init, 1015 "__block_literal_global"); 1016 literal->setAlignment(blockInfo.BlockAlign.getQuantity()); 1017 1018 // Return a constant of the appropriately-casted type. 1019 llvm::Type *requiredType = 1020 CGM.getTypes().ConvertType(blockInfo.getBlockExpr()->getType()); 1021 return llvm::ConstantExpr::getBitCast(literal, requiredType); 1022 } 1023 1024 llvm::Function * 1025 CodeGenFunction::GenerateBlockFunction(GlobalDecl GD, 1026 const CGBlockInfo &blockInfo, 1027 const Decl *outerFnDecl, 1028 const DeclMapTy &ldm, 1029 bool IsLambdaConversionToBlock) { 1030 const BlockDecl *blockDecl = blockInfo.getBlockDecl(); 1031 1032 // Check if we should generate debug info for this block function. 1033 maybeInitializeDebugInfo(); 1034 CurGD = GD; 1035 1036 BlockInfo = &blockInfo; 1037 1038 // Arrange for local static and local extern declarations to appear 1039 // to be local to this function as well, in case they're directly 1040 // referenced in a block. 1041 for (DeclMapTy::const_iterator i = ldm.begin(), e = ldm.end(); i != e; ++i) { 1042 const VarDecl *var = dyn_cast<VarDecl>(i->first); 1043 if (var && !var->hasLocalStorage()) 1044 LocalDeclMap[var] = i->second; 1045 } 1046 1047 // Begin building the function declaration. 1048 1049 // Build the argument list. 1050 FunctionArgList args; 1051 1052 // The first argument is the block pointer. Just take it as a void* 1053 // and cast it later. 1054 QualType selfTy = getContext().VoidPtrTy; 1055 IdentifierInfo *II = &CGM.getContext().Idents.get(".block_descriptor"); 1056 1057 ImplicitParamDecl selfDecl(const_cast<BlockDecl*>(blockDecl), 1058 SourceLocation(), II, selfTy); 1059 args.push_back(&selfDecl); 1060 1061 // Now add the rest of the parameters. 1062 for (BlockDecl::param_const_iterator i = blockDecl->param_begin(), 1063 e = blockDecl->param_end(); i != e; ++i) 1064 args.push_back(*i); 1065 1066 // Create the function declaration. 1067 const FunctionProtoType *fnType = blockInfo.getBlockExpr()->getFunctionType(); 1068 const CGFunctionInfo &fnInfo = 1069 CGM.getTypes().arrangeFunctionDeclaration(fnType->getResultType(), args, 1070 fnType->getExtInfo(), 1071 fnType->isVariadic()); 1072 if (CGM.ReturnTypeUsesSRet(fnInfo)) 1073 blockInfo.UsesStret = true; 1074 1075 llvm::FunctionType *fnLLVMType = CGM.getTypes().GetFunctionType(fnInfo); 1076 1077 MangleBuffer name; 1078 CGM.getBlockMangledName(GD, name, blockDecl); 1079 llvm::Function *fn = 1080 llvm::Function::Create(fnLLVMType, llvm::GlobalValue::InternalLinkage, 1081 name.getString(), &CGM.getModule()); 1082 CGM.SetInternalFunctionAttributes(blockDecl, fn, fnInfo); 1083 1084 // Begin generating the function. 1085 StartFunction(blockDecl, fnType->getResultType(), fn, fnInfo, args, 1086 blockInfo.getBlockExpr()->getBody()->getLocStart()); 1087 CurFuncDecl = outerFnDecl; // StartFunction sets this to blockDecl 1088 1089 // Okay. Undo some of what StartFunction did. 1090 1091 // Pull the 'self' reference out of the local decl map. 1092 llvm::Value *blockAddr = LocalDeclMap[&selfDecl]; 1093 LocalDeclMap.erase(&selfDecl); 1094 BlockPointer = Builder.CreateBitCast(blockAddr, 1095 blockInfo.StructureType->getPointerTo(), 1096 "block"); 1097 1098 // If we have a C++ 'this' reference, go ahead and force it into 1099 // existence now. 1100 if (blockDecl->capturesCXXThis()) { 1101 llvm::Value *addr = Builder.CreateStructGEP(BlockPointer, 1102 blockInfo.CXXThisIndex, 1103 "block.captured-this"); 1104 CXXThisValue = Builder.CreateLoad(addr, "this"); 1105 } 1106 1107 // LoadObjCSelf() expects there to be an entry for 'self' in LocalDeclMap; 1108 // appease it. 1109 if (const ObjCMethodDecl *method 1110 = dyn_cast_or_null<ObjCMethodDecl>(CurFuncDecl)) { 1111 const VarDecl *self = method->getSelfDecl(); 1112 1113 // There might not be a capture for 'self', but if there is... 1114 if (blockInfo.Captures.count(self)) { 1115 const CGBlockInfo::Capture &capture = blockInfo.getCapture(self); 1116 llvm::Value *selfAddr = Builder.CreateStructGEP(BlockPointer, 1117 capture.getIndex(), 1118 "block.captured-self"); 1119 LocalDeclMap[self] = selfAddr; 1120 } 1121 } 1122 1123 // Also force all the constant captures. 1124 for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(), 1125 ce = blockDecl->capture_end(); ci != ce; ++ci) { 1126 const VarDecl *variable = ci->getVariable(); 1127 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable); 1128 if (!capture.isConstant()) continue; 1129 1130 unsigned align = getContext().getDeclAlign(variable).getQuantity(); 1131 1132 llvm::AllocaInst *alloca = 1133 CreateMemTemp(variable->getType(), "block.captured-const"); 1134 alloca->setAlignment(align); 1135 1136 Builder.CreateStore(capture.getConstant(), alloca, align); 1137 1138 LocalDeclMap[variable] = alloca; 1139 } 1140 1141 // Save a spot to insert the debug information for all the DeclRefExprs. 1142 llvm::BasicBlock *entry = Builder.GetInsertBlock(); 1143 llvm::BasicBlock::iterator entry_ptr = Builder.GetInsertPoint(); 1144 --entry_ptr; 1145 1146 if (IsLambdaConversionToBlock) 1147 EmitLambdaBlockInvokeBody(); 1148 else 1149 EmitStmt(blockDecl->getBody()); 1150 1151 // Remember where we were... 1152 llvm::BasicBlock *resume = Builder.GetInsertBlock(); 1153 1154 // Go back to the entry. 1155 ++entry_ptr; 1156 Builder.SetInsertPoint(entry, entry_ptr); 1157 1158 // Emit debug information for all the DeclRefExprs. 1159 // FIXME: also for 'this' 1160 if (CGDebugInfo *DI = getDebugInfo()) { 1161 for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(), 1162 ce = blockDecl->capture_end(); ci != ce; ++ci) { 1163 const VarDecl *variable = ci->getVariable(); 1164 DI->EmitLocation(Builder, variable->getLocation()); 1165 1166 if (CGM.getCodeGenOpts().getDebugInfo() 1167 >= CodeGenOptions::LimitedDebugInfo) { 1168 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable); 1169 if (capture.isConstant()) { 1170 DI->EmitDeclareOfAutoVariable(variable, LocalDeclMap[variable], 1171 Builder); 1172 continue; 1173 } 1174 1175 DI->EmitDeclareOfBlockDeclRefVariable(variable, BlockPointer, 1176 Builder, blockInfo); 1177 } 1178 } 1179 // Recover location if it was changed in the above loop. 1180 DI->EmitLocation(Builder, 1181 cast<CompoundStmt>(blockDecl->getBody())->getRBracLoc()); 1182 } 1183 1184 // And resume where we left off. 1185 if (resume == 0) 1186 Builder.ClearInsertionPoint(); 1187 else 1188 Builder.SetInsertPoint(resume); 1189 1190 FinishFunction(cast<CompoundStmt>(blockDecl->getBody())->getRBracLoc()); 1191 1192 return fn; 1193 } 1194 1195 /* 1196 notes.push_back(HelperInfo()); 1197 HelperInfo ¬e = notes.back(); 1198 note.index = capture.getIndex(); 1199 note.RequiresCopying = (ci->hasCopyExpr() || BlockRequiresCopying(type)); 1200 note.cxxbar_import = ci->getCopyExpr(); 1201 1202 if (ci->isByRef()) { 1203 note.flag = BLOCK_FIELD_IS_BYREF; 1204 if (type.isObjCGCWeak()) 1205 note.flag |= BLOCK_FIELD_IS_WEAK; 1206 } else if (type->isBlockPointerType()) { 1207 note.flag = BLOCK_FIELD_IS_BLOCK; 1208 } else { 1209 note.flag = BLOCK_FIELD_IS_OBJECT; 1210 } 1211 */ 1212 1213 1214 1215 llvm::Constant * 1216 CodeGenFunction::GenerateCopyHelperFunction(const CGBlockInfo &blockInfo) { 1217 ASTContext &C = getContext(); 1218 1219 FunctionArgList args; 1220 ImplicitParamDecl dstDecl(0, SourceLocation(), 0, C.VoidPtrTy); 1221 args.push_back(&dstDecl); 1222 ImplicitParamDecl srcDecl(0, SourceLocation(), 0, C.VoidPtrTy); 1223 args.push_back(&srcDecl); 1224 1225 const CGFunctionInfo &FI = 1226 CGM.getTypes().arrangeFunctionDeclaration(C.VoidTy, args, 1227 FunctionType::ExtInfo(), 1228 /*variadic*/ false); 1229 1230 // FIXME: it would be nice if these were mergeable with things with 1231 // identical semantics. 1232 llvm::FunctionType *LTy = CGM.getTypes().GetFunctionType(FI); 1233 1234 llvm::Function *Fn = 1235 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 1236 "__copy_helper_block_", &CGM.getModule()); 1237 1238 IdentifierInfo *II 1239 = &CGM.getContext().Idents.get("__copy_helper_block_"); 1240 1241 // Check if we should generate debug info for this block helper function. 1242 maybeInitializeDebugInfo(); 1243 1244 FunctionDecl *FD = FunctionDecl::Create(C, 1245 C.getTranslationUnitDecl(), 1246 SourceLocation(), 1247 SourceLocation(), II, C.VoidTy, 0, 1248 SC_Static, 1249 SC_None, 1250 false, 1251 false); 1252 StartFunction(FD, C.VoidTy, Fn, FI, args, SourceLocation()); 1253 1254 llvm::Type *structPtrTy = blockInfo.StructureType->getPointerTo(); 1255 1256 llvm::Value *src = GetAddrOfLocalVar(&srcDecl); 1257 src = Builder.CreateLoad(src); 1258 src = Builder.CreateBitCast(src, structPtrTy, "block.source"); 1259 1260 llvm::Value *dst = GetAddrOfLocalVar(&dstDecl); 1261 dst = Builder.CreateLoad(dst); 1262 dst = Builder.CreateBitCast(dst, structPtrTy, "block.dest"); 1263 1264 const BlockDecl *blockDecl = blockInfo.getBlockDecl(); 1265 1266 for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(), 1267 ce = blockDecl->capture_end(); ci != ce; ++ci) { 1268 const VarDecl *variable = ci->getVariable(); 1269 QualType type = variable->getType(); 1270 1271 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable); 1272 if (capture.isConstant()) continue; 1273 1274 const Expr *copyExpr = ci->getCopyExpr(); 1275 BlockFieldFlags flags; 1276 1277 bool useARCWeakCopy = false; 1278 bool useARCStrongCopy = false; 1279 1280 if (copyExpr) { 1281 assert(!ci->isByRef()); 1282 // don't bother computing flags 1283 1284 } else if (ci->isByRef()) { 1285 flags = BLOCK_FIELD_IS_BYREF; 1286 if (type.isObjCGCWeak()) 1287 flags |= BLOCK_FIELD_IS_WEAK; 1288 1289 } else if (type->isObjCRetainableType()) { 1290 flags = BLOCK_FIELD_IS_OBJECT; 1291 bool isBlockPointer = type->isBlockPointerType(); 1292 if (isBlockPointer) 1293 flags = BLOCK_FIELD_IS_BLOCK; 1294 1295 // Special rules for ARC captures: 1296 if (getLangOpts().ObjCAutoRefCount) { 1297 Qualifiers qs = type.getQualifiers(); 1298 1299 // We need to register __weak direct captures with the runtime. 1300 if (qs.getObjCLifetime() == Qualifiers::OCL_Weak) { 1301 useARCWeakCopy = true; 1302 1303 // We need to retain the copied value for __strong direct captures. 1304 } else if (qs.getObjCLifetime() == Qualifiers::OCL_Strong) { 1305 // If it's a block pointer, we have to copy the block and 1306 // assign that to the destination pointer, so we might as 1307 // well use _Block_object_assign. Otherwise we can avoid that. 1308 if (!isBlockPointer) 1309 useARCStrongCopy = true; 1310 1311 // Otherwise the memcpy is fine. 1312 } else { 1313 continue; 1314 } 1315 1316 // Non-ARC captures of retainable pointers are strong and 1317 // therefore require a call to _Block_object_assign. 1318 } else { 1319 // fall through 1320 } 1321 } else { 1322 continue; 1323 } 1324 1325 unsigned index = capture.getIndex(); 1326 llvm::Value *srcField = Builder.CreateStructGEP(src, index); 1327 llvm::Value *dstField = Builder.CreateStructGEP(dst, index); 1328 1329 // If there's an explicit copy expression, we do that. 1330 if (copyExpr) { 1331 EmitSynthesizedCXXCopyCtor(dstField, srcField, copyExpr); 1332 } else if (useARCWeakCopy) { 1333 EmitARCCopyWeak(dstField, srcField); 1334 } else { 1335 llvm::Value *srcValue = Builder.CreateLoad(srcField, "blockcopy.src"); 1336 if (useARCStrongCopy) { 1337 // At -O0, store null into the destination field (so that the 1338 // storeStrong doesn't over-release) and then call storeStrong. 1339 // This is a workaround to not having an initStrong call. 1340 if (CGM.getCodeGenOpts().OptimizationLevel == 0) { 1341 llvm::PointerType *ty = cast<llvm::PointerType>(srcValue->getType()); 1342 llvm::Value *null = llvm::ConstantPointerNull::get(ty); 1343 Builder.CreateStore(null, dstField); 1344 EmitARCStoreStrongCall(dstField, srcValue, true); 1345 1346 // With optimization enabled, take advantage of the fact that 1347 // the blocks runtime guarantees a memcpy of the block data, and 1348 // just emit a retain of the src field. 1349 } else { 1350 EmitARCRetainNonBlock(srcValue); 1351 1352 // We don't need this anymore, so kill it. It's not quite 1353 // worth the annoyance to avoid creating it in the first place. 1354 cast<llvm::Instruction>(dstField)->eraseFromParent(); 1355 } 1356 } else { 1357 srcValue = Builder.CreateBitCast(srcValue, VoidPtrTy); 1358 llvm::Value *dstAddr = Builder.CreateBitCast(dstField, VoidPtrTy); 1359 Builder.CreateCall3(CGM.getBlockObjectAssign(), dstAddr, srcValue, 1360 llvm::ConstantInt::get(Int32Ty, flags.getBitMask())); 1361 } 1362 } 1363 } 1364 1365 FinishFunction(); 1366 1367 return llvm::ConstantExpr::getBitCast(Fn, VoidPtrTy); 1368 } 1369 1370 llvm::Constant * 1371 CodeGenFunction::GenerateDestroyHelperFunction(const CGBlockInfo &blockInfo) { 1372 ASTContext &C = getContext(); 1373 1374 FunctionArgList args; 1375 ImplicitParamDecl srcDecl(0, SourceLocation(), 0, C.VoidPtrTy); 1376 args.push_back(&srcDecl); 1377 1378 const CGFunctionInfo &FI = 1379 CGM.getTypes().arrangeFunctionDeclaration(C.VoidTy, args, 1380 FunctionType::ExtInfo(), 1381 /*variadic*/ false); 1382 1383 // FIXME: We'd like to put these into a mergable by content, with 1384 // internal linkage. 1385 llvm::FunctionType *LTy = CGM.getTypes().GetFunctionType(FI); 1386 1387 llvm::Function *Fn = 1388 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 1389 "__destroy_helper_block_", &CGM.getModule()); 1390 1391 // Check if we should generate debug info for this block destroy function. 1392 maybeInitializeDebugInfo(); 1393 1394 IdentifierInfo *II 1395 = &CGM.getContext().Idents.get("__destroy_helper_block_"); 1396 1397 FunctionDecl *FD = FunctionDecl::Create(C, C.getTranslationUnitDecl(), 1398 SourceLocation(), 1399 SourceLocation(), II, C.VoidTy, 0, 1400 SC_Static, 1401 SC_None, 1402 false, false); 1403 StartFunction(FD, C.VoidTy, Fn, FI, args, SourceLocation()); 1404 1405 llvm::Type *structPtrTy = blockInfo.StructureType->getPointerTo(); 1406 1407 llvm::Value *src = GetAddrOfLocalVar(&srcDecl); 1408 src = Builder.CreateLoad(src); 1409 src = Builder.CreateBitCast(src, structPtrTy, "block"); 1410 1411 const BlockDecl *blockDecl = blockInfo.getBlockDecl(); 1412 1413 CodeGenFunction::RunCleanupsScope cleanups(*this); 1414 1415 for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(), 1416 ce = blockDecl->capture_end(); ci != ce; ++ci) { 1417 const VarDecl *variable = ci->getVariable(); 1418 QualType type = variable->getType(); 1419 1420 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable); 1421 if (capture.isConstant()) continue; 1422 1423 BlockFieldFlags flags; 1424 const CXXDestructorDecl *dtor = 0; 1425 1426 bool useARCWeakDestroy = false; 1427 bool useARCStrongDestroy = false; 1428 1429 if (ci->isByRef()) { 1430 flags = BLOCK_FIELD_IS_BYREF; 1431 if (type.isObjCGCWeak()) 1432 flags |= BLOCK_FIELD_IS_WEAK; 1433 } else if (const CXXRecordDecl *record = type->getAsCXXRecordDecl()) { 1434 if (record->hasTrivialDestructor()) 1435 continue; 1436 dtor = record->getDestructor(); 1437 } else if (type->isObjCRetainableType()) { 1438 flags = BLOCK_FIELD_IS_OBJECT; 1439 if (type->isBlockPointerType()) 1440 flags = BLOCK_FIELD_IS_BLOCK; 1441 1442 // Special rules for ARC captures. 1443 if (getLangOpts().ObjCAutoRefCount) { 1444 Qualifiers qs = type.getQualifiers(); 1445 1446 // Don't generate special dispose logic for a captured object 1447 // unless it's __strong or __weak. 1448 if (!qs.hasStrongOrWeakObjCLifetime()) 1449 continue; 1450 1451 // Support __weak direct captures. 1452 if (qs.getObjCLifetime() == Qualifiers::OCL_Weak) 1453 useARCWeakDestroy = true; 1454 1455 // Tools really want us to use objc_storeStrong here. 1456 else 1457 useARCStrongDestroy = true; 1458 } 1459 } else { 1460 continue; 1461 } 1462 1463 unsigned index = capture.getIndex(); 1464 llvm::Value *srcField = Builder.CreateStructGEP(src, index); 1465 1466 // If there's an explicit copy expression, we do that. 1467 if (dtor) { 1468 PushDestructorCleanup(dtor, srcField); 1469 1470 // If this is a __weak capture, emit the release directly. 1471 } else if (useARCWeakDestroy) { 1472 EmitARCDestroyWeak(srcField); 1473 1474 // Destroy strong objects with a call if requested. 1475 } else if (useARCStrongDestroy) { 1476 EmitARCDestroyStrong(srcField, /*precise*/ false); 1477 1478 // Otherwise we call _Block_object_dispose. It wouldn't be too 1479 // hard to just emit this as a cleanup if we wanted to make sure 1480 // that things were done in reverse. 1481 } else { 1482 llvm::Value *value = Builder.CreateLoad(srcField); 1483 value = Builder.CreateBitCast(value, VoidPtrTy); 1484 BuildBlockRelease(value, flags); 1485 } 1486 } 1487 1488 cleanups.ForceCleanup(); 1489 1490 FinishFunction(); 1491 1492 return llvm::ConstantExpr::getBitCast(Fn, VoidPtrTy); 1493 } 1494 1495 namespace { 1496 1497 /// Emits the copy/dispose helper functions for a __block object of id type. 1498 class ObjectByrefHelpers : public CodeGenModule::ByrefHelpers { 1499 BlockFieldFlags Flags; 1500 1501 public: 1502 ObjectByrefHelpers(CharUnits alignment, BlockFieldFlags flags) 1503 : ByrefHelpers(alignment), Flags(flags) {} 1504 1505 void emitCopy(CodeGenFunction &CGF, llvm::Value *destField, 1506 llvm::Value *srcField) { 1507 destField = CGF.Builder.CreateBitCast(destField, CGF.VoidPtrTy); 1508 1509 srcField = CGF.Builder.CreateBitCast(srcField, CGF.VoidPtrPtrTy); 1510 llvm::Value *srcValue = CGF.Builder.CreateLoad(srcField); 1511 1512 unsigned flags = (Flags | BLOCK_BYREF_CALLER).getBitMask(); 1513 1514 llvm::Value *flagsVal = llvm::ConstantInt::get(CGF.Int32Ty, flags); 1515 llvm::Value *fn = CGF.CGM.getBlockObjectAssign(); 1516 CGF.Builder.CreateCall3(fn, destField, srcValue, flagsVal); 1517 } 1518 1519 void emitDispose(CodeGenFunction &CGF, llvm::Value *field) { 1520 field = CGF.Builder.CreateBitCast(field, CGF.Int8PtrTy->getPointerTo(0)); 1521 llvm::Value *value = CGF.Builder.CreateLoad(field); 1522 1523 CGF.BuildBlockRelease(value, Flags | BLOCK_BYREF_CALLER); 1524 } 1525 1526 void profileImpl(llvm::FoldingSetNodeID &id) const { 1527 id.AddInteger(Flags.getBitMask()); 1528 } 1529 }; 1530 1531 /// Emits the copy/dispose helpers for an ARC __block __weak variable. 1532 class ARCWeakByrefHelpers : public CodeGenModule::ByrefHelpers { 1533 public: 1534 ARCWeakByrefHelpers(CharUnits alignment) : ByrefHelpers(alignment) {} 1535 1536 void emitCopy(CodeGenFunction &CGF, llvm::Value *destField, 1537 llvm::Value *srcField) { 1538 CGF.EmitARCMoveWeak(destField, srcField); 1539 } 1540 1541 void emitDispose(CodeGenFunction &CGF, llvm::Value *field) { 1542 CGF.EmitARCDestroyWeak(field); 1543 } 1544 1545 void profileImpl(llvm::FoldingSetNodeID &id) const { 1546 // 0 is distinguishable from all pointers and byref flags 1547 id.AddInteger(0); 1548 } 1549 }; 1550 1551 /// Emits the copy/dispose helpers for an ARC __block __strong variable 1552 /// that's not of block-pointer type. 1553 class ARCStrongByrefHelpers : public CodeGenModule::ByrefHelpers { 1554 public: 1555 ARCStrongByrefHelpers(CharUnits alignment) : ByrefHelpers(alignment) {} 1556 1557 void emitCopy(CodeGenFunction &CGF, llvm::Value *destField, 1558 llvm::Value *srcField) { 1559 // Do a "move" by copying the value and then zeroing out the old 1560 // variable. 1561 1562 llvm::LoadInst *value = CGF.Builder.CreateLoad(srcField); 1563 value->setAlignment(Alignment.getQuantity()); 1564 1565 llvm::Value *null = 1566 llvm::ConstantPointerNull::get(cast<llvm::PointerType>(value->getType())); 1567 1568 if (CGF.CGM.getCodeGenOpts().OptimizationLevel == 0) { 1569 llvm::StoreInst *store = CGF.Builder.CreateStore(null, destField); 1570 store->setAlignment(Alignment.getQuantity()); 1571 CGF.EmitARCStoreStrongCall(destField, value, /*ignored*/ true); 1572 CGF.EmitARCStoreStrongCall(srcField, null, /*ignored*/ true); 1573 return; 1574 } 1575 llvm::StoreInst *store = CGF.Builder.CreateStore(value, destField); 1576 store->setAlignment(Alignment.getQuantity()); 1577 1578 store = CGF.Builder.CreateStore(null, srcField); 1579 store->setAlignment(Alignment.getQuantity()); 1580 } 1581 1582 void emitDispose(CodeGenFunction &CGF, llvm::Value *field) { 1583 CGF.EmitARCDestroyStrong(field, /*precise*/ false); 1584 } 1585 1586 void profileImpl(llvm::FoldingSetNodeID &id) const { 1587 // 1 is distinguishable from all pointers and byref flags 1588 id.AddInteger(1); 1589 } 1590 }; 1591 1592 /// Emits the copy/dispose helpers for an ARC __block __strong 1593 /// variable that's of block-pointer type. 1594 class ARCStrongBlockByrefHelpers : public CodeGenModule::ByrefHelpers { 1595 public: 1596 ARCStrongBlockByrefHelpers(CharUnits alignment) : ByrefHelpers(alignment) {} 1597 1598 void emitCopy(CodeGenFunction &CGF, llvm::Value *destField, 1599 llvm::Value *srcField) { 1600 // Do the copy with objc_retainBlock; that's all that 1601 // _Block_object_assign would do anyway, and we'd have to pass the 1602 // right arguments to make sure it doesn't get no-op'ed. 1603 llvm::LoadInst *oldValue = CGF.Builder.CreateLoad(srcField); 1604 oldValue->setAlignment(Alignment.getQuantity()); 1605 1606 llvm::Value *copy = CGF.EmitARCRetainBlock(oldValue, /*mandatory*/ true); 1607 1608 llvm::StoreInst *store = CGF.Builder.CreateStore(copy, destField); 1609 store->setAlignment(Alignment.getQuantity()); 1610 } 1611 1612 void emitDispose(CodeGenFunction &CGF, llvm::Value *field) { 1613 CGF.EmitARCDestroyStrong(field, /*precise*/ false); 1614 } 1615 1616 void profileImpl(llvm::FoldingSetNodeID &id) const { 1617 // 2 is distinguishable from all pointers and byref flags 1618 id.AddInteger(2); 1619 } 1620 }; 1621 1622 /// Emits the copy/dispose helpers for a __block variable with a 1623 /// nontrivial copy constructor or destructor. 1624 class CXXByrefHelpers : public CodeGenModule::ByrefHelpers { 1625 QualType VarType; 1626 const Expr *CopyExpr; 1627 1628 public: 1629 CXXByrefHelpers(CharUnits alignment, QualType type, 1630 const Expr *copyExpr) 1631 : ByrefHelpers(alignment), VarType(type), CopyExpr(copyExpr) {} 1632 1633 bool needsCopy() const { return CopyExpr != 0; } 1634 void emitCopy(CodeGenFunction &CGF, llvm::Value *destField, 1635 llvm::Value *srcField) { 1636 if (!CopyExpr) return; 1637 CGF.EmitSynthesizedCXXCopyCtor(destField, srcField, CopyExpr); 1638 } 1639 1640 void emitDispose(CodeGenFunction &CGF, llvm::Value *field) { 1641 EHScopeStack::stable_iterator cleanupDepth = CGF.EHStack.stable_begin(); 1642 CGF.PushDestructorCleanup(VarType, field); 1643 CGF.PopCleanupBlocks(cleanupDepth); 1644 } 1645 1646 void profileImpl(llvm::FoldingSetNodeID &id) const { 1647 id.AddPointer(VarType.getCanonicalType().getAsOpaquePtr()); 1648 } 1649 }; 1650 } // end anonymous namespace 1651 1652 static llvm::Constant * 1653 generateByrefCopyHelper(CodeGenFunction &CGF, 1654 llvm::StructType &byrefType, 1655 CodeGenModule::ByrefHelpers &byrefInfo) { 1656 ASTContext &Context = CGF.getContext(); 1657 1658 QualType R = Context.VoidTy; 1659 1660 FunctionArgList args; 1661 ImplicitParamDecl dst(0, SourceLocation(), 0, Context.VoidPtrTy); 1662 args.push_back(&dst); 1663 1664 ImplicitParamDecl src(0, SourceLocation(), 0, Context.VoidPtrTy); 1665 args.push_back(&src); 1666 1667 const CGFunctionInfo &FI = 1668 CGF.CGM.getTypes().arrangeFunctionDeclaration(R, args, 1669 FunctionType::ExtInfo(), 1670 /*variadic*/ false); 1671 1672 CodeGenTypes &Types = CGF.CGM.getTypes(); 1673 llvm::FunctionType *LTy = Types.GetFunctionType(FI); 1674 1675 // FIXME: We'd like to put these into a mergable by content, with 1676 // internal linkage. 1677 llvm::Function *Fn = 1678 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 1679 "__Block_byref_object_copy_", &CGF.CGM.getModule()); 1680 1681 IdentifierInfo *II 1682 = &Context.Idents.get("__Block_byref_object_copy_"); 1683 1684 FunctionDecl *FD = FunctionDecl::Create(Context, 1685 Context.getTranslationUnitDecl(), 1686 SourceLocation(), 1687 SourceLocation(), II, R, 0, 1688 SC_Static, 1689 SC_None, 1690 false, false); 1691 1692 // Initialize debug info if necessary. 1693 CGF.maybeInitializeDebugInfo(); 1694 CGF.StartFunction(FD, R, Fn, FI, args, SourceLocation()); 1695 1696 if (byrefInfo.needsCopy()) { 1697 llvm::Type *byrefPtrType = byrefType.getPointerTo(0); 1698 1699 // dst->x 1700 llvm::Value *destField = CGF.GetAddrOfLocalVar(&dst); 1701 destField = CGF.Builder.CreateLoad(destField); 1702 destField = CGF.Builder.CreateBitCast(destField, byrefPtrType); 1703 destField = CGF.Builder.CreateStructGEP(destField, 6, "x"); 1704 1705 // src->x 1706 llvm::Value *srcField = CGF.GetAddrOfLocalVar(&src); 1707 srcField = CGF.Builder.CreateLoad(srcField); 1708 srcField = CGF.Builder.CreateBitCast(srcField, byrefPtrType); 1709 srcField = CGF.Builder.CreateStructGEP(srcField, 6, "x"); 1710 1711 byrefInfo.emitCopy(CGF, destField, srcField); 1712 } 1713 1714 CGF.FinishFunction(); 1715 1716 return llvm::ConstantExpr::getBitCast(Fn, CGF.Int8PtrTy); 1717 } 1718 1719 /// Build the copy helper for a __block variable. 1720 static llvm::Constant *buildByrefCopyHelper(CodeGenModule &CGM, 1721 llvm::StructType &byrefType, 1722 CodeGenModule::ByrefHelpers &info) { 1723 CodeGenFunction CGF(CGM); 1724 return generateByrefCopyHelper(CGF, byrefType, info); 1725 } 1726 1727 /// Generate code for a __block variable's dispose helper. 1728 static llvm::Constant * 1729 generateByrefDisposeHelper(CodeGenFunction &CGF, 1730 llvm::StructType &byrefType, 1731 CodeGenModule::ByrefHelpers &byrefInfo) { 1732 ASTContext &Context = CGF.getContext(); 1733 QualType R = Context.VoidTy; 1734 1735 FunctionArgList args; 1736 ImplicitParamDecl src(0, SourceLocation(), 0, Context.VoidPtrTy); 1737 args.push_back(&src); 1738 1739 const CGFunctionInfo &FI = 1740 CGF.CGM.getTypes().arrangeFunctionDeclaration(R, args, 1741 FunctionType::ExtInfo(), 1742 /*variadic*/ false); 1743 1744 CodeGenTypes &Types = CGF.CGM.getTypes(); 1745 llvm::FunctionType *LTy = Types.GetFunctionType(FI); 1746 1747 // FIXME: We'd like to put these into a mergable by content, with 1748 // internal linkage. 1749 llvm::Function *Fn = 1750 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 1751 "__Block_byref_object_dispose_", 1752 &CGF.CGM.getModule()); 1753 1754 IdentifierInfo *II 1755 = &Context.Idents.get("__Block_byref_object_dispose_"); 1756 1757 FunctionDecl *FD = FunctionDecl::Create(Context, 1758 Context.getTranslationUnitDecl(), 1759 SourceLocation(), 1760 SourceLocation(), II, R, 0, 1761 SC_Static, 1762 SC_None, 1763 false, false); 1764 // Initialize debug info if necessary. 1765 CGF.maybeInitializeDebugInfo(); 1766 CGF.StartFunction(FD, R, Fn, FI, args, SourceLocation()); 1767 1768 if (byrefInfo.needsDispose()) { 1769 llvm::Value *V = CGF.GetAddrOfLocalVar(&src); 1770 V = CGF.Builder.CreateLoad(V); 1771 V = CGF.Builder.CreateBitCast(V, byrefType.getPointerTo(0)); 1772 V = CGF.Builder.CreateStructGEP(V, 6, "x"); 1773 1774 byrefInfo.emitDispose(CGF, V); 1775 } 1776 1777 CGF.FinishFunction(); 1778 1779 return llvm::ConstantExpr::getBitCast(Fn, CGF.Int8PtrTy); 1780 } 1781 1782 /// Build the dispose helper for a __block variable. 1783 static llvm::Constant *buildByrefDisposeHelper(CodeGenModule &CGM, 1784 llvm::StructType &byrefType, 1785 CodeGenModule::ByrefHelpers &info) { 1786 CodeGenFunction CGF(CGM); 1787 return generateByrefDisposeHelper(CGF, byrefType, info); 1788 } 1789 1790 /// 1791 template <class T> static T *buildByrefHelpers(CodeGenModule &CGM, 1792 llvm::StructType &byrefTy, 1793 T &byrefInfo) { 1794 // Increase the field's alignment to be at least pointer alignment, 1795 // since the layout of the byref struct will guarantee at least that. 1796 byrefInfo.Alignment = std::max(byrefInfo.Alignment, 1797 CharUnits::fromQuantity(CGM.PointerAlignInBytes)); 1798 1799 llvm::FoldingSetNodeID id; 1800 byrefInfo.Profile(id); 1801 1802 void *insertPos; 1803 CodeGenModule::ByrefHelpers *node 1804 = CGM.ByrefHelpersCache.FindNodeOrInsertPos(id, insertPos); 1805 if (node) return static_cast<T*>(node); 1806 1807 byrefInfo.CopyHelper = buildByrefCopyHelper(CGM, byrefTy, byrefInfo); 1808 byrefInfo.DisposeHelper = buildByrefDisposeHelper(CGM, byrefTy, byrefInfo); 1809 1810 T *copy = new (CGM.getContext()) T(byrefInfo); 1811 CGM.ByrefHelpersCache.InsertNode(copy, insertPos); 1812 return copy; 1813 } 1814 1815 CodeGenModule::ByrefHelpers * 1816 CodeGenFunction::buildByrefHelpers(llvm::StructType &byrefType, 1817 const AutoVarEmission &emission) { 1818 const VarDecl &var = *emission.Variable; 1819 QualType type = var.getType(); 1820 1821 if (const CXXRecordDecl *record = type->getAsCXXRecordDecl()) { 1822 const Expr *copyExpr = CGM.getContext().getBlockVarCopyInits(&var); 1823 if (!copyExpr && record->hasTrivialDestructor()) return 0; 1824 1825 CXXByrefHelpers byrefInfo(emission.Alignment, type, copyExpr); 1826 return ::buildByrefHelpers(CGM, byrefType, byrefInfo); 1827 } 1828 1829 // Otherwise, if we don't have a retainable type, there's nothing to do. 1830 // that the runtime does extra copies. 1831 if (!type->isObjCRetainableType()) return 0; 1832 1833 Qualifiers qs = type.getQualifiers(); 1834 1835 // If we have lifetime, that dominates. 1836 if (Qualifiers::ObjCLifetime lifetime = qs.getObjCLifetime()) { 1837 assert(getLangOpts().ObjCAutoRefCount); 1838 1839 switch (lifetime) { 1840 case Qualifiers::OCL_None: llvm_unreachable("impossible"); 1841 1842 // These are just bits as far as the runtime is concerned. 1843 case Qualifiers::OCL_ExplicitNone: 1844 case Qualifiers::OCL_Autoreleasing: 1845 return 0; 1846 1847 // Tell the runtime that this is ARC __weak, called by the 1848 // byref routines. 1849 case Qualifiers::OCL_Weak: { 1850 ARCWeakByrefHelpers byrefInfo(emission.Alignment); 1851 return ::buildByrefHelpers(CGM, byrefType, byrefInfo); 1852 } 1853 1854 // ARC __strong __block variables need to be retained. 1855 case Qualifiers::OCL_Strong: 1856 // Block pointers need to be copied, and there's no direct 1857 // transfer possible. 1858 if (type->isBlockPointerType()) { 1859 ARCStrongBlockByrefHelpers byrefInfo(emission.Alignment); 1860 return ::buildByrefHelpers(CGM, byrefType, byrefInfo); 1861 1862 // Otherwise, we transfer ownership of the retain from the stack 1863 // to the heap. 1864 } else { 1865 ARCStrongByrefHelpers byrefInfo(emission.Alignment); 1866 return ::buildByrefHelpers(CGM, byrefType, byrefInfo); 1867 } 1868 } 1869 llvm_unreachable("fell out of lifetime switch!"); 1870 } 1871 1872 BlockFieldFlags flags; 1873 if (type->isBlockPointerType()) { 1874 flags |= BLOCK_FIELD_IS_BLOCK; 1875 } else if (CGM.getContext().isObjCNSObjectType(type) || 1876 type->isObjCObjectPointerType()) { 1877 flags |= BLOCK_FIELD_IS_OBJECT; 1878 } else { 1879 return 0; 1880 } 1881 1882 if (type.isObjCGCWeak()) 1883 flags |= BLOCK_FIELD_IS_WEAK; 1884 1885 ObjectByrefHelpers byrefInfo(emission.Alignment, flags); 1886 return ::buildByrefHelpers(CGM, byrefType, byrefInfo); 1887 } 1888 1889 unsigned CodeGenFunction::getByRefValueLLVMField(const ValueDecl *VD) const { 1890 assert(ByRefValueInfo.count(VD) && "Did not find value!"); 1891 1892 return ByRefValueInfo.find(VD)->second.second; 1893 } 1894 1895 llvm::Value *CodeGenFunction::BuildBlockByrefAddress(llvm::Value *BaseAddr, 1896 const VarDecl *V) { 1897 llvm::Value *Loc = Builder.CreateStructGEP(BaseAddr, 1, "forwarding"); 1898 Loc = Builder.CreateLoad(Loc); 1899 Loc = Builder.CreateStructGEP(Loc, getByRefValueLLVMField(V), 1900 V->getNameAsString()); 1901 return Loc; 1902 } 1903 1904 /// BuildByRefType - This routine changes a __block variable declared as T x 1905 /// into: 1906 /// 1907 /// struct { 1908 /// void *__isa; 1909 /// void *__forwarding; 1910 /// int32_t __flags; 1911 /// int32_t __size; 1912 /// void *__copy_helper; // only if needed 1913 /// void *__destroy_helper; // only if needed 1914 /// void *__byref_variable_layout;// only if needed 1915 /// char padding[X]; // only if needed 1916 /// T x; 1917 /// } x 1918 /// 1919 llvm::Type *CodeGenFunction::BuildByRefType(const VarDecl *D) { 1920 std::pair<llvm::Type *, unsigned> &Info = ByRefValueInfo[D]; 1921 if (Info.first) 1922 return Info.first; 1923 1924 QualType Ty = D->getType(); 1925 1926 SmallVector<llvm::Type *, 8> types; 1927 1928 llvm::StructType *ByRefType = 1929 llvm::StructType::create(getLLVMContext(), 1930 "struct.__block_byref_" + D->getNameAsString()); 1931 1932 // void *__isa; 1933 types.push_back(Int8PtrTy); 1934 1935 // void *__forwarding; 1936 types.push_back(llvm::PointerType::getUnqual(ByRefType)); 1937 1938 // int32_t __flags; 1939 types.push_back(Int32Ty); 1940 1941 // int32_t __size; 1942 types.push_back(Int32Ty); 1943 // Note that this must match *exactly* the logic in buildByrefHelpers. 1944 bool HasCopyAndDispose = getContext().BlockRequiresCopying(Ty, D); 1945 if (HasCopyAndDispose) { 1946 /// void *__copy_helper; 1947 types.push_back(Int8PtrTy); 1948 1949 /// void *__destroy_helper; 1950 types.push_back(Int8PtrTy); 1951 } 1952 bool HasByrefExtendedLayout = false; 1953 Qualifiers::ObjCLifetime Lifetime; 1954 if (getContext().getByrefLifetime(Ty, Lifetime, HasByrefExtendedLayout) && 1955 HasByrefExtendedLayout) 1956 /// void *__byref_variable_layout; 1957 types.push_back(Int8PtrTy); 1958 1959 bool Packed = false; 1960 CharUnits Align = getContext().getDeclAlign(D); 1961 if (Align > getContext().toCharUnitsFromBits(Target.getPointerAlign(0))) { 1962 // We have to insert padding. 1963 1964 // The struct above has 2 32-bit integers. 1965 unsigned CurrentOffsetInBytes = 4 * 2; 1966 1967 // And either 2, 3, 4 or 5 pointers. 1968 unsigned noPointers = 2; 1969 if (HasCopyAndDispose) 1970 noPointers += 2; 1971 if (HasByrefExtendedLayout) 1972 noPointers += 1; 1973 1974 CurrentOffsetInBytes += noPointers * CGM.getDataLayout().getTypeAllocSize(Int8PtrTy); 1975 1976 // Align the offset. 1977 unsigned AlignedOffsetInBytes = 1978 llvm::RoundUpToAlignment(CurrentOffsetInBytes, Align.getQuantity()); 1979 1980 unsigned NumPaddingBytes = AlignedOffsetInBytes - CurrentOffsetInBytes; 1981 if (NumPaddingBytes > 0) { 1982 llvm::Type *Ty = Int8Ty; 1983 // FIXME: We need a sema error for alignment larger than the minimum of 1984 // the maximal stack alignment and the alignment of malloc on the system. 1985 if (NumPaddingBytes > 1) 1986 Ty = llvm::ArrayType::get(Ty, NumPaddingBytes); 1987 1988 types.push_back(Ty); 1989 1990 // We want a packed struct. 1991 Packed = true; 1992 } 1993 } 1994 1995 // T x; 1996 types.push_back(ConvertTypeForMem(Ty)); 1997 1998 ByRefType->setBody(types, Packed); 1999 2000 Info.first = ByRefType; 2001 2002 Info.second = types.size() - 1; 2003 2004 return Info.first; 2005 } 2006 2007 /// Initialize the structural components of a __block variable, i.e. 2008 /// everything but the actual object. 2009 void CodeGenFunction::emitByrefStructureInit(const AutoVarEmission &emission) { 2010 // Find the address of the local. 2011 llvm::Value *addr = emission.Address; 2012 2013 // That's an alloca of the byref structure type. 2014 llvm::StructType *byrefType = cast<llvm::StructType>( 2015 cast<llvm::PointerType>(addr->getType())->getElementType()); 2016 2017 // Build the byref helpers if necessary. This is null if we don't need any. 2018 CodeGenModule::ByrefHelpers *helpers = 2019 buildByrefHelpers(*byrefType, emission); 2020 2021 const VarDecl &D = *emission.Variable; 2022 QualType type = D.getType(); 2023 2024 bool HasByrefExtendedLayout; 2025 Qualifiers::ObjCLifetime ByrefLifetime; 2026 bool ByRefHasLifetime = 2027 getContext().getByrefLifetime(type, ByrefLifetime, HasByrefExtendedLayout); 2028 2029 llvm::Value *V; 2030 2031 // Initialize the 'isa', which is just 0 or 1. 2032 int isa = 0; 2033 if (type.isObjCGCWeak()) 2034 isa = 1; 2035 V = Builder.CreateIntToPtr(Builder.getInt32(isa), Int8PtrTy, "isa"); 2036 Builder.CreateStore(V, Builder.CreateStructGEP(addr, 0, "byref.isa")); 2037 2038 // Store the address of the variable into its own forwarding pointer. 2039 Builder.CreateStore(addr, 2040 Builder.CreateStructGEP(addr, 1, "byref.forwarding")); 2041 2042 // Blocks ABI: 2043 // c) the flags field is set to either 0 if no helper functions are 2044 // needed or BLOCK_BYREF_HAS_COPY_DISPOSE if they are, 2045 BlockFlags flags; 2046 if (helpers) flags |= BLOCK_BYREF_HAS_COPY_DISPOSE; 2047 if (ByRefHasLifetime) { 2048 if (HasByrefExtendedLayout) flags |= BLOCK_BYREF_LAYOUT_EXTENDED; 2049 else switch (ByrefLifetime) { 2050 case Qualifiers::OCL_Strong: 2051 flags |= BLOCK_BYREF_LAYOUT_STRONG; 2052 break; 2053 case Qualifiers::OCL_Weak: 2054 flags |= BLOCK_BYREF_LAYOUT_WEAK; 2055 break; 2056 case Qualifiers::OCL_ExplicitNone: 2057 flags |= BLOCK_BYREF_LAYOUT_UNRETAINED; 2058 break; 2059 case Qualifiers::OCL_None: 2060 if (!type->isObjCObjectPointerType() && !type->isBlockPointerType()) 2061 flags |= BLOCK_BYREF_LAYOUT_NON_OBJECT; 2062 break; 2063 default: 2064 break; 2065 } 2066 if (CGM.getLangOpts().ObjCGCBitmapPrint) { 2067 printf("\n Inline flag for BYREF variable layout (%d):", flags.getBitMask()); 2068 if (flags & BLOCK_BYREF_HAS_COPY_DISPOSE) 2069 printf(" BLOCK_BYREF_HAS_COPY_DISPOSE"); 2070 if (flags & BLOCK_BYREF_LAYOUT_MASK) { 2071 BlockFlags ThisFlag(flags.getBitMask() & BLOCK_BYREF_LAYOUT_MASK); 2072 if (ThisFlag == BLOCK_BYREF_LAYOUT_EXTENDED) 2073 printf(" BLOCK_BYREF_LAYOUT_EXTENDED"); 2074 if (ThisFlag == BLOCK_BYREF_LAYOUT_STRONG) 2075 printf(" BLOCK_BYREF_LAYOUT_STRONG"); 2076 if (ThisFlag == BLOCK_BYREF_LAYOUT_WEAK) 2077 printf(" BLOCK_BYREF_LAYOUT_WEAK"); 2078 if (ThisFlag == BLOCK_BYREF_LAYOUT_UNRETAINED) 2079 printf(" BLOCK_BYREF_LAYOUT_UNRETAINED"); 2080 if (ThisFlag == BLOCK_BYREF_LAYOUT_NON_OBJECT) 2081 printf(" BLOCK_BYREF_LAYOUT_NON_OBJECT"); 2082 } 2083 printf("\n"); 2084 } 2085 } 2086 2087 Builder.CreateStore(llvm::ConstantInt::get(IntTy, flags.getBitMask()), 2088 Builder.CreateStructGEP(addr, 2, "byref.flags")); 2089 2090 CharUnits byrefSize = CGM.GetTargetTypeStoreSize(byrefType); 2091 V = llvm::ConstantInt::get(IntTy, byrefSize.getQuantity()); 2092 Builder.CreateStore(V, Builder.CreateStructGEP(addr, 3, "byref.size")); 2093 2094 if (helpers) { 2095 llvm::Value *copy_helper = Builder.CreateStructGEP(addr, 4); 2096 Builder.CreateStore(helpers->CopyHelper, copy_helper); 2097 2098 llvm::Value *destroy_helper = Builder.CreateStructGEP(addr, 5); 2099 Builder.CreateStore(helpers->DisposeHelper, destroy_helper); 2100 } 2101 if (ByRefHasLifetime && HasByrefExtendedLayout) { 2102 llvm::Constant* ByrefLayoutInfo = CGM.getObjCRuntime().BuildByrefLayout(CGM, type); 2103 llvm::Value *ByrefInfoAddr = Builder.CreateStructGEP(addr, helpers ? 6 : 4, 2104 "byref.layout"); 2105 // cast destination to pointer to source type. 2106 llvm::Type *DesTy = ByrefLayoutInfo->getType(); 2107 DesTy = DesTy->getPointerTo(); 2108 llvm::Value *BC = Builder.CreatePointerCast(ByrefInfoAddr, DesTy); 2109 Builder.CreateStore(ByrefLayoutInfo, BC); 2110 } 2111 } 2112 2113 void CodeGenFunction::BuildBlockRelease(llvm::Value *V, BlockFieldFlags flags) { 2114 llvm::Value *F = CGM.getBlockObjectDispose(); 2115 llvm::Value *N; 2116 V = Builder.CreateBitCast(V, Int8PtrTy); 2117 N = llvm::ConstantInt::get(Int32Ty, flags.getBitMask()); 2118 Builder.CreateCall2(F, V, N); 2119 } 2120 2121 namespace { 2122 struct CallBlockRelease : EHScopeStack::Cleanup { 2123 llvm::Value *Addr; 2124 CallBlockRelease(llvm::Value *Addr) : Addr(Addr) {} 2125 2126 void Emit(CodeGenFunction &CGF, Flags flags) { 2127 // Should we be passing FIELD_IS_WEAK here? 2128 CGF.BuildBlockRelease(Addr, BLOCK_FIELD_IS_BYREF); 2129 } 2130 }; 2131 } 2132 2133 /// Enter a cleanup to destroy a __block variable. Note that this 2134 /// cleanup should be a no-op if the variable hasn't left the stack 2135 /// yet; if a cleanup is required for the variable itself, that needs 2136 /// to be done externally. 2137 void CodeGenFunction::enterByrefCleanup(const AutoVarEmission &emission) { 2138 // We don't enter this cleanup if we're in pure-GC mode. 2139 if (CGM.getLangOpts().getGC() == LangOptions::GCOnly) 2140 return; 2141 2142 EHStack.pushCleanup<CallBlockRelease>(NormalAndEHCleanup, emission.Address); 2143 } 2144 2145 /// Adjust the declaration of something from the blocks API. 2146 static void configureBlocksRuntimeObject(CodeGenModule &CGM, 2147 llvm::Constant *C) { 2148 if (!CGM.getLangOpts().BlocksRuntimeOptional) return; 2149 2150 llvm::GlobalValue *GV = cast<llvm::GlobalValue>(C->stripPointerCasts()); 2151 if (GV->isDeclaration() && 2152 GV->getLinkage() == llvm::GlobalValue::ExternalLinkage) 2153 GV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); 2154 } 2155 2156 llvm::Constant *CodeGenModule::getBlockObjectDispose() { 2157 if (BlockObjectDispose) 2158 return BlockObjectDispose; 2159 2160 llvm::Type *args[] = { Int8PtrTy, Int32Ty }; 2161 llvm::FunctionType *fty 2162 = llvm::FunctionType::get(VoidTy, args, false); 2163 BlockObjectDispose = CreateRuntimeFunction(fty, "_Block_object_dispose"); 2164 configureBlocksRuntimeObject(*this, BlockObjectDispose); 2165 return BlockObjectDispose; 2166 } 2167 2168 llvm::Constant *CodeGenModule::getBlockObjectAssign() { 2169 if (BlockObjectAssign) 2170 return BlockObjectAssign; 2171 2172 llvm::Type *args[] = { Int8PtrTy, Int8PtrTy, Int32Ty }; 2173 llvm::FunctionType *fty 2174 = llvm::FunctionType::get(VoidTy, args, false); 2175 BlockObjectAssign = CreateRuntimeFunction(fty, "_Block_object_assign"); 2176 configureBlocksRuntimeObject(*this, BlockObjectAssign); 2177 return BlockObjectAssign; 2178 } 2179 2180 llvm::Constant *CodeGenModule::getNSConcreteGlobalBlock() { 2181 if (NSConcreteGlobalBlock) 2182 return NSConcreteGlobalBlock; 2183 2184 NSConcreteGlobalBlock = GetOrCreateLLVMGlobal("_NSConcreteGlobalBlock", 2185 Int8PtrTy->getPointerTo(), 0); 2186 configureBlocksRuntimeObject(*this, NSConcreteGlobalBlock); 2187 return NSConcreteGlobalBlock; 2188 } 2189 2190 llvm::Constant *CodeGenModule::getNSConcreteStackBlock() { 2191 if (NSConcreteStackBlock) 2192 return NSConcreteStackBlock; 2193 2194 NSConcreteStackBlock = GetOrCreateLLVMGlobal("_NSConcreteStackBlock", 2195 Int8PtrTy->getPointerTo(), 0); 2196 configureBlocksRuntimeObject(*this, NSConcreteStackBlock); 2197 return NSConcreteStackBlock; 2198 } 2199