CGBlocks.cpp revision 2bb110125e0e5adb7c1c65d12adfa34151ca1c47
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 "CGDebugInfo.h"
15#include "CodeGenFunction.h"
16#include "CGObjCRuntime.h"
17#include "CodeGenModule.h"
18#include "CGBlocks.h"
19#include "clang/AST/DeclObjC.h"
20#include "llvm/Module.h"
21#include "llvm/ADT/SmallSet.h"
22#include "llvm/Target/TargetData.h"
23#include <algorithm>
24
25using namespace clang;
26using namespace CodeGen;
27
28CGBlockInfo::CGBlockInfo(const BlockExpr *blockExpr, const char *N)
29  : Name(N), CXXThisIndex(0), CanBeGlobal(false), NeedsCopyDispose(false),
30    HasCXXObject(false), UsesStret(false), StructureType(0), Block(blockExpr) {
31
32  // Skip asm prefix, if any.
33  if (Name && Name[0] == '\01')
34    ++Name;
35}
36
37// Anchor the vtable to this translation unit.
38CodeGenModule::ByrefHelpers::~ByrefHelpers() {}
39
40/// Build the given block as a global block.
41static llvm::Constant *buildGlobalBlock(CodeGenModule &CGM,
42                                        const CGBlockInfo &blockInfo,
43                                        llvm::Constant *blockFn);
44
45/// Build the helper function to copy a block.
46static llvm::Constant *buildCopyHelper(CodeGenModule &CGM,
47                                       const CGBlockInfo &blockInfo) {
48  return CodeGenFunction(CGM).GenerateCopyHelperFunction(blockInfo);
49}
50
51/// Build the helper function to dipose of a block.
52static llvm::Constant *buildDisposeHelper(CodeGenModule &CGM,
53                                          const CGBlockInfo &blockInfo) {
54  return CodeGenFunction(CGM).GenerateDestroyHelperFunction(blockInfo);
55}
56
57/// Build the block descriptor constant for a block.
58static llvm::Constant *buildBlockDescriptor(CodeGenModule &CGM,
59                                            const CGBlockInfo &blockInfo) {
60  ASTContext &C = CGM.getContext();
61
62  const llvm::Type *ulong = CGM.getTypes().ConvertType(C.UnsignedLongTy);
63  const llvm::Type *i8p = CGM.getTypes().ConvertType(C.VoidPtrTy);
64
65  llvm::SmallVector<llvm::Constant*, 6> elements;
66
67  // reserved
68  elements.push_back(llvm::ConstantInt::get(ulong, 0));
69
70  // Size
71  // FIXME: What is the right way to say this doesn't fit?  We should give
72  // a user diagnostic in that case.  Better fix would be to change the
73  // API to size_t.
74  elements.push_back(llvm::ConstantInt::get(ulong,
75                                            blockInfo.BlockSize.getQuantity()));
76
77  // Optional copy/dispose helpers.
78  if (blockInfo.NeedsCopyDispose) {
79    // copy_func_helper_decl
80    elements.push_back(buildCopyHelper(CGM, blockInfo));
81
82    // destroy_func_decl
83    elements.push_back(buildDisposeHelper(CGM, blockInfo));
84  }
85
86  // Signature.  Mandatory ObjC-style method descriptor @encode sequence.
87  std::string typeAtEncoding =
88    CGM.getContext().getObjCEncodingForBlock(blockInfo.getBlockExpr());
89  elements.push_back(llvm::ConstantExpr::getBitCast(
90                          CGM.GetAddrOfConstantCString(typeAtEncoding), i8p));
91
92  // GC layout.
93  if (C.getLangOptions().ObjC1)
94    elements.push_back(CGM.getObjCRuntime().BuildGCBlockLayout(CGM, blockInfo));
95  else
96    elements.push_back(llvm::Constant::getNullValue(i8p));
97
98  llvm::Constant *init =
99    llvm::ConstantStruct::get(CGM.getLLVMContext(), elements.data(),
100                              elements.size(), false);
101
102  llvm::GlobalVariable *global =
103    new llvm::GlobalVariable(CGM.getModule(), init->getType(), true,
104                             llvm::GlobalValue::InternalLinkage,
105                             init, "__block_descriptor_tmp");
106
107  return llvm::ConstantExpr::getBitCast(global, CGM.getBlockDescriptorType());
108}
109
110/*
111  Purely notional variadic template describing the layout of a block.
112
113  template <class _ResultType, class... _ParamTypes, class... _CaptureTypes>
114  struct Block_literal {
115    /// Initialized to one of:
116    ///   extern void *_NSConcreteStackBlock[];
117    ///   extern void *_NSConcreteGlobalBlock[];
118    ///
119    /// In theory, we could start one off malloc'ed by setting
120    /// BLOCK_NEEDS_FREE, giving it a refcount of 1, and using
121    /// this isa:
122    ///   extern void *_NSConcreteMallocBlock[];
123    struct objc_class *isa;
124
125    /// These are the flags (with corresponding bit number) that the
126    /// compiler is actually supposed to know about.
127    ///  25. BLOCK_HAS_COPY_DISPOSE - indicates that the block
128    ///   descriptor provides copy and dispose helper functions
129    ///  26. BLOCK_HAS_CXX_OBJ - indicates that there's a captured
130    ///   object with a nontrivial destructor or copy constructor
131    ///  28. BLOCK_IS_GLOBAL - indicates that the block is allocated
132    ///   as global memory
133    ///  29. BLOCK_USE_STRET - indicates that the block function
134    ///   uses stret, which objc_msgSend needs to know about
135    ///  30. BLOCK_HAS_SIGNATURE - indicates that the block has an
136    ///   @encoded signature string
137    /// And we're not supposed to manipulate these:
138    ///  24. BLOCK_NEEDS_FREE - indicates that the block has been moved
139    ///   to malloc'ed memory
140    ///  27. BLOCK_IS_GC - indicates that the block has been moved to
141    ///   to GC-allocated memory
142    /// Additionally, the bottom 16 bits are a reference count which
143    /// should be zero on the stack.
144    int flags;
145
146    /// Reserved;  should be zero-initialized.
147    int reserved;
148
149    /// Function pointer generated from block literal.
150    _ResultType (*invoke)(Block_literal *, _ParamTypes...);
151
152    /// Block description metadata generated from block literal.
153    struct Block_descriptor *block_descriptor;
154
155    /// Captured values follow.
156    _CapturesTypes captures...;
157  };
158 */
159
160/// The number of fields in a block header.
161const unsigned BlockHeaderSize = 5;
162
163namespace {
164  /// A chunk of data that we actually have to capture in the block.
165  struct BlockLayoutChunk {
166    CharUnits Alignment;
167    CharUnits Size;
168    const BlockDecl::Capture *Capture; // null for 'this'
169    const llvm::Type *Type;
170
171    BlockLayoutChunk(CharUnits align, CharUnits size,
172                     const BlockDecl::Capture *capture,
173                     const llvm::Type *type)
174      : Alignment(align), Size(size), Capture(capture), Type(type) {}
175
176    /// Tell the block info that this chunk has the given field index.
177    void setIndex(CGBlockInfo &info, unsigned index) {
178      if (!Capture)
179        info.CXXThisIndex = index;
180      else
181        info.Captures[Capture->getVariable()]
182          = CGBlockInfo::Capture::makeIndex(index);
183    }
184  };
185
186  /// Order by descending alignment.
187  bool operator<(const BlockLayoutChunk &left, const BlockLayoutChunk &right) {
188    return left.Alignment > right.Alignment;
189  }
190}
191
192/// Determines if the given type is safe for constant capture in C++.
193static bool isSafeForCXXConstantCapture(QualType type) {
194  const RecordType *recordType =
195    type->getBaseElementTypeUnsafe()->getAs<RecordType>();
196
197  // Only records can be unsafe.
198  if (!recordType) return true;
199
200  const CXXRecordDecl *record = cast<CXXRecordDecl>(recordType->getDecl());
201
202  // Maintain semantics for classes with non-trivial dtors or copy ctors.
203  if (!record->hasTrivialDestructor()) return false;
204  if (!record->hasTrivialCopyConstructor()) return false;
205
206  // Otherwise, we just have to make sure there aren't any mutable
207  // fields that might have changed since initialization.
208  return !record->hasMutableFields();
209}
210
211/// It is illegal to modify a const object after initialization.
212/// Therefore, if a const object has a constant initializer, we don't
213/// actually need to keep storage for it in the block; we'll just
214/// rematerialize it at the start of the block function.  This is
215/// acceptable because we make no promises about address stability of
216/// captured variables.
217static llvm::Constant *tryCaptureAsConstant(CodeGenModule &CGM,
218                                            const VarDecl *var) {
219  QualType type = var->getType();
220
221  // We can only do this if the variable is const.
222  if (!type.isConstQualified()) return 0;
223
224  // Furthermore, in C++ we have to worry about mutable fields:
225  // C++ [dcl.type.cv]p4:
226  //   Except that any class member declared mutable can be
227  //   modified, any attempt to modify a const object during its
228  //   lifetime results in undefined behavior.
229  if (CGM.getLangOptions().CPlusPlus && !isSafeForCXXConstantCapture(type))
230    return 0;
231
232  // If the variable doesn't have any initializer (shouldn't this be
233  // invalid?), it's not clear what we should do.  Maybe capture as
234  // zero?
235  const Expr *init = var->getInit();
236  if (!init) return 0;
237
238  return CGM.EmitConstantExpr(init, var->getType());
239}
240
241/// Get the low bit of a nonzero character count.  This is the
242/// alignment of the nth byte if the 0th byte is universally aligned.
243static CharUnits getLowBit(CharUnits v) {
244  return CharUnits::fromQuantity(v.getQuantity() & (~v.getQuantity() + 1));
245}
246
247static void initializeForBlockHeader(CodeGenModule &CGM, CGBlockInfo &info,
248                                std::vector<const llvm::Type*> &elementTypes) {
249  ASTContext &C = CGM.getContext();
250
251  // The header is basically a 'struct { void *; int; int; void *; void *; }'.
252  CharUnits ptrSize, ptrAlign, intSize, intAlign;
253  llvm::tie(ptrSize, ptrAlign) = C.getTypeInfoInChars(C.VoidPtrTy);
254  llvm::tie(intSize, intAlign) = C.getTypeInfoInChars(C.IntTy);
255
256  // Are there crazy embedded platforms where this isn't true?
257  assert(intSize <= ptrSize && "layout assumptions horribly violated");
258
259  CharUnits headerSize = ptrSize;
260  if (2 * intSize < ptrAlign) headerSize += ptrSize;
261  else headerSize += 2 * intSize;
262  headerSize += 2 * ptrSize;
263
264  info.BlockAlign = ptrAlign;
265  info.BlockSize = headerSize;
266
267  assert(elementTypes.empty());
268  const llvm::Type *i8p = CGM.getTypes().ConvertType(C.VoidPtrTy);
269  const llvm::Type *intTy = CGM.getTypes().ConvertType(C.IntTy);
270  elementTypes.push_back(i8p);
271  elementTypes.push_back(intTy);
272  elementTypes.push_back(intTy);
273  elementTypes.push_back(i8p);
274  elementTypes.push_back(CGM.getBlockDescriptorType());
275
276  assert(elementTypes.size() == BlockHeaderSize);
277}
278
279/// Compute the layout of the given block.  Attempts to lay the block
280/// out with minimal space requirements.
281static void computeBlockInfo(CodeGenModule &CGM, CGBlockInfo &info) {
282  ASTContext &C = CGM.getContext();
283  const BlockDecl *block = info.getBlockDecl();
284
285  std::vector<const llvm::Type*> elementTypes;
286  initializeForBlockHeader(CGM, info, elementTypes);
287
288  if (!block->hasCaptures()) {
289    info.StructureType =
290      llvm::StructType::get(CGM.getLLVMContext(), elementTypes, true);
291    info.CanBeGlobal = true;
292    return;
293  }
294
295  // Collect the layout chunks.
296  llvm::SmallVector<BlockLayoutChunk, 16> layout;
297  layout.reserve(block->capturesCXXThis() +
298                 (block->capture_end() - block->capture_begin()));
299
300  CharUnits maxFieldAlign;
301
302  // First, 'this'.
303  if (block->capturesCXXThis()) {
304    const DeclContext *DC = block->getDeclContext();
305    for (; isa<BlockDecl>(DC); DC = cast<BlockDecl>(DC)->getDeclContext())
306      ;
307    QualType thisType = cast<CXXMethodDecl>(DC)->getThisType(C);
308
309    const llvm::Type *llvmType = CGM.getTypes().ConvertType(thisType);
310    std::pair<CharUnits,CharUnits> tinfo
311      = CGM.getContext().getTypeInfoInChars(thisType);
312    maxFieldAlign = std::max(maxFieldAlign, tinfo.second);
313
314    layout.push_back(BlockLayoutChunk(tinfo.second, tinfo.first, 0, llvmType));
315  }
316
317  // Next, all the block captures.
318  for (BlockDecl::capture_const_iterator ci = block->capture_begin(),
319         ce = block->capture_end(); ci != ce; ++ci) {
320    const VarDecl *variable = ci->getVariable();
321
322    if (ci->isByRef()) {
323      // We have to copy/dispose of the __block reference.
324      info.NeedsCopyDispose = true;
325
326      // Just use void* instead of a pointer to the byref type.
327      QualType byRefPtrTy = C.VoidPtrTy;
328
329      const llvm::Type *llvmType = CGM.getTypes().ConvertType(byRefPtrTy);
330      std::pair<CharUnits,CharUnits> tinfo
331        = CGM.getContext().getTypeInfoInChars(byRefPtrTy);
332      maxFieldAlign = std::max(maxFieldAlign, tinfo.second);
333
334      layout.push_back(BlockLayoutChunk(tinfo.second, tinfo.first,
335                                        &*ci, llvmType));
336      continue;
337    }
338
339    // Otherwise, build a layout chunk with the size and alignment of
340    // the declaration.
341    if (llvm::Constant *constant = tryCaptureAsConstant(CGM, variable)) {
342      info.Captures[variable] = CGBlockInfo::Capture::makeConstant(constant);
343      continue;
344    }
345
346    // Block pointers require copy/dispose.
347    if (variable->getType()->isBlockPointerType()) {
348      info.NeedsCopyDispose = true;
349
350    // So do Objective-C pointers.
351    } else if (variable->getType()->isObjCObjectPointerType() ||
352               C.isObjCNSObjectType(variable->getType())) {
353      info.NeedsCopyDispose = true;
354
355    // So do types that require non-trivial copy construction.
356    } else if (ci->hasCopyExpr()) {
357      info.NeedsCopyDispose = true;
358      info.HasCXXObject = true;
359
360    // And so do types with destructors.
361    } else if (CGM.getLangOptions().CPlusPlus) {
362      if (const CXXRecordDecl *record =
363            variable->getType()->getAsCXXRecordDecl()) {
364        if (!record->hasTrivialDestructor()) {
365          info.HasCXXObject = true;
366          info.NeedsCopyDispose = true;
367        }
368      }
369    }
370
371    CharUnits size = C.getTypeSizeInChars(variable->getType());
372    CharUnits align = C.getDeclAlign(variable);
373    maxFieldAlign = std::max(maxFieldAlign, align);
374
375    const llvm::Type *llvmType =
376      CGM.getTypes().ConvertTypeForMem(variable->getType());
377
378    layout.push_back(BlockLayoutChunk(align, size, &*ci, llvmType));
379  }
380
381  // If that was everything, we're done here.
382  if (layout.empty()) {
383    info.StructureType =
384      llvm::StructType::get(CGM.getLLVMContext(), elementTypes, true);
385    info.CanBeGlobal = true;
386    return;
387  }
388
389  // Sort the layout by alignment.  We have to use a stable sort here
390  // to get reproducible results.  There should probably be an
391  // llvm::array_pod_stable_sort.
392  std::stable_sort(layout.begin(), layout.end());
393
394  CharUnits &blockSize = info.BlockSize;
395  info.BlockAlign = std::max(maxFieldAlign, info.BlockAlign);
396
397  // Assuming that the first byte in the header is maximally aligned,
398  // get the alignment of the first byte following the header.
399  CharUnits endAlign = getLowBit(blockSize);
400
401  // If the end of the header isn't satisfactorily aligned for the
402  // maximum thing, look for things that are okay with the header-end
403  // alignment, and keep appending them until we get something that's
404  // aligned right.  This algorithm is only guaranteed optimal if
405  // that condition is satisfied at some point; otherwise we can get
406  // things like:
407  //   header                 // next byte has alignment 4
408  //   something_with_size_5; // next byte has alignment 1
409  //   something_with_alignment_8;
410  // which has 7 bytes of padding, as opposed to the naive solution
411  // which might have less (?).
412  if (endAlign < maxFieldAlign) {
413    llvm::SmallVectorImpl<BlockLayoutChunk>::iterator
414      li = layout.begin() + 1, le = layout.end();
415
416    // Look for something that the header end is already
417    // satisfactorily aligned for.
418    for (; li != le && endAlign < li->Alignment; ++li)
419      ;
420
421    // If we found something that's naturally aligned for the end of
422    // the header, keep adding things...
423    if (li != le) {
424      llvm::SmallVectorImpl<BlockLayoutChunk>::iterator first = li;
425      for (; li != le; ++li) {
426        assert(endAlign >= li->Alignment);
427
428        li->setIndex(info, elementTypes.size());
429        elementTypes.push_back(li->Type);
430        blockSize += li->Size;
431        endAlign = getLowBit(blockSize);
432
433        // ...until we get to the alignment of the maximum field.
434        if (endAlign >= maxFieldAlign)
435          break;
436      }
437
438      // Don't re-append everything we just appended.
439      layout.erase(first, li);
440    }
441  }
442
443  // At this point, we just have to add padding if the end align still
444  // isn't aligned right.
445  if (endAlign < maxFieldAlign) {
446    CharUnits padding = maxFieldAlign - endAlign;
447
448    elementTypes.push_back(llvm::ArrayType::get(CGM.Int8Ty,
449                                                padding.getQuantity()));
450    blockSize += padding;
451
452    endAlign = getLowBit(blockSize);
453    assert(endAlign >= maxFieldAlign);
454  }
455
456  // Slam everything else on now.  This works because they have
457  // strictly decreasing alignment and we expect that size is always a
458  // multiple of alignment.
459  for (llvm::SmallVectorImpl<BlockLayoutChunk>::iterator
460         li = layout.begin(), le = layout.end(); li != le; ++li) {
461    assert(endAlign >= li->Alignment);
462    li->setIndex(info, elementTypes.size());
463    elementTypes.push_back(li->Type);
464    blockSize += li->Size;
465    endAlign = getLowBit(blockSize);
466  }
467
468  info.StructureType =
469    llvm::StructType::get(CGM.getLLVMContext(), elementTypes, true);
470}
471
472/// Emit a block literal expression in the current function.
473llvm::Value *CodeGenFunction::EmitBlockLiteral(const BlockExpr *blockExpr) {
474  std::string Name = CurFn->getName();
475  CGBlockInfo blockInfo(blockExpr, Name.c_str());
476
477  // Compute information about the layout, etc., of this block.
478  computeBlockInfo(CGM, blockInfo);
479
480  // Using that metadata, generate the actual block function.
481  llvm::Constant *blockFn
482    = CodeGenFunction(CGM).GenerateBlockFunction(CurGD, blockInfo,
483                                                 CurFuncDecl, LocalDeclMap);
484  blockFn = llvm::ConstantExpr::getBitCast(blockFn, VoidPtrTy);
485
486  // If there is nothing to capture, we can emit this as a global block.
487  if (blockInfo.CanBeGlobal)
488    return buildGlobalBlock(CGM, blockInfo, blockFn);
489
490  // Otherwise, we have to emit this as a local block.
491
492  llvm::Constant *isa = CGM.getNSConcreteStackBlock();
493  isa = llvm::ConstantExpr::getBitCast(isa, VoidPtrTy);
494
495  // Build the block descriptor.
496  llvm::Constant *descriptor = buildBlockDescriptor(CGM, blockInfo);
497
498  const llvm::Type *intTy = ConvertType(getContext().IntTy);
499
500  llvm::AllocaInst *blockAddr =
501    CreateTempAlloca(blockInfo.StructureType, "block");
502  blockAddr->setAlignment(blockInfo.BlockAlign.getQuantity());
503
504  // Compute the initial on-stack block flags.
505  BlockFlags flags = BLOCK_HAS_SIGNATURE;
506  if (blockInfo.NeedsCopyDispose) flags |= BLOCK_HAS_COPY_DISPOSE;
507  if (blockInfo.HasCXXObject) flags |= BLOCK_HAS_CXX_OBJ;
508  if (blockInfo.UsesStret) flags |= BLOCK_USE_STRET;
509
510  // Initialize the block literal.
511  Builder.CreateStore(isa, Builder.CreateStructGEP(blockAddr, 0, "block.isa"));
512  Builder.CreateStore(llvm::ConstantInt::get(intTy, flags.getBitMask()),
513                      Builder.CreateStructGEP(blockAddr, 1, "block.flags"));
514  Builder.CreateStore(llvm::ConstantInt::get(intTy, 0),
515                      Builder.CreateStructGEP(blockAddr, 2, "block.reserved"));
516  Builder.CreateStore(blockFn, Builder.CreateStructGEP(blockAddr, 3,
517                                                       "block.invoke"));
518  Builder.CreateStore(descriptor, Builder.CreateStructGEP(blockAddr, 4,
519                                                          "block.descriptor"));
520
521  // Finally, capture all the values into the block.
522  const BlockDecl *blockDecl = blockInfo.getBlockDecl();
523
524  // First, 'this'.
525  if (blockDecl->capturesCXXThis()) {
526    llvm::Value *addr = Builder.CreateStructGEP(blockAddr,
527                                                blockInfo.CXXThisIndex,
528                                                "block.captured-this.addr");
529    Builder.CreateStore(LoadCXXThis(), addr);
530  }
531
532  // Next, captured variables.
533  for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
534         ce = blockDecl->capture_end(); ci != ce; ++ci) {
535    const VarDecl *variable = ci->getVariable();
536    const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
537
538    // Ignore constant captures.
539    if (capture.isConstant()) continue;
540
541    QualType type = variable->getType();
542
543    // This will be a [[type]]*, except that a byref entry will just be
544    // an i8**.
545    llvm::Value *blockField =
546      Builder.CreateStructGEP(blockAddr, capture.getIndex(),
547                              "block.captured");
548
549    // Compute the address of the thing we're going to move into the
550    // block literal.
551    llvm::Value *src;
552    if (ci->isNested()) {
553      // We need to use the capture from the enclosing block.
554      const CGBlockInfo::Capture &enclosingCapture =
555        BlockInfo->getCapture(variable);
556
557      // This is a [[type]]*, except that a byref entry wil just be an i8**.
558      src = Builder.CreateStructGEP(LoadBlockStruct(),
559                                    enclosingCapture.getIndex(),
560                                    "block.capture.addr");
561    } else {
562      // This is a [[type]]*.
563      src = LocalDeclMap[variable];
564    }
565
566    // For byrefs, we just write the pointer to the byref struct into
567    // the block field.  There's no need to chase the forwarding
568    // pointer at this point, since we're building something that will
569    // live a shorter life than the stack byref anyway.
570    if (ci->isByRef()) {
571      // Get a void* that points to the byref struct.
572      if (ci->isNested())
573        src = Builder.CreateLoad(src, "byref.capture");
574      else
575        src = Builder.CreateBitCast(src, VoidPtrTy);
576
577      // Write that void* into the capture field.
578      Builder.CreateStore(src, blockField);
579
580    // If we have a copy constructor, evaluate that into the block field.
581    } else if (const Expr *copyExpr = ci->getCopyExpr()) {
582      EmitSynthesizedCXXCopyCtor(blockField, src, copyExpr);
583
584    // If it's a reference variable, copy the reference into the block field.
585    } else if (type->isReferenceType()) {
586      Builder.CreateStore(Builder.CreateLoad(src, "ref.val"), blockField);
587
588    // Otherwise, fake up a POD copy into the block field.
589    } else {
590      // We use one of these or the other depending on whether the
591      // reference is nested.
592      DeclRefExpr notNested(const_cast<VarDecl*>(variable), type, VK_LValue,
593                            SourceLocation());
594      BlockDeclRefExpr nested(const_cast<VarDecl*>(variable), type,
595                              VK_LValue, SourceLocation(), /*byref*/ false);
596
597      Expr *declRef =
598        (ci->isNested() ? static_cast<Expr*>(&nested) : &notNested);
599
600      ImplicitCastExpr l2r(ImplicitCastExpr::OnStack, type, CK_LValueToRValue,
601                           declRef, VK_RValue);
602      EmitExprAsInit(&l2r, variable, blockField,
603                     getContext().getDeclAlign(variable),
604                     /*captured by init*/ false);
605    }
606
607    // Push a destructor if necessary.  The semantics for when this
608    // actually gets run are really obscure.
609    if (!ci->isByRef() && CGM.getLangOptions().CPlusPlus)
610      PushDestructorCleanup(type, blockField);
611  }
612
613  // Cast to the converted block-pointer type, which happens (somewhat
614  // unfortunately) to be a pointer to function type.
615  llvm::Value *result =
616    Builder.CreateBitCast(blockAddr,
617                          ConvertType(blockInfo.getBlockExpr()->getType()));
618
619  return result;
620}
621
622
623const llvm::Type *CodeGenModule::getBlockDescriptorType() {
624  if (BlockDescriptorType)
625    return BlockDescriptorType;
626
627  const llvm::Type *UnsignedLongTy =
628    getTypes().ConvertType(getContext().UnsignedLongTy);
629
630  // struct __block_descriptor {
631  //   unsigned long reserved;
632  //   unsigned long block_size;
633  //
634  //   // later, the following will be added
635  //
636  //   struct {
637  //     void (*copyHelper)();
638  //     void (*copyHelper)();
639  //   } helpers;                // !!! optional
640  //
641  //   const char *signature;   // the block signature
642  //   const char *layout;      // reserved
643  // };
644  BlockDescriptorType = llvm::StructType::get(UnsignedLongTy->getContext(),
645                                              UnsignedLongTy,
646                                              UnsignedLongTy,
647                                              NULL);
648
649  getModule().addTypeName("struct.__block_descriptor",
650                          BlockDescriptorType);
651
652  // Now form a pointer to that.
653  BlockDescriptorType = llvm::PointerType::getUnqual(BlockDescriptorType);
654  return BlockDescriptorType;
655}
656
657const llvm::Type *CodeGenModule::getGenericBlockLiteralType() {
658  if (GenericBlockLiteralType)
659    return GenericBlockLiteralType;
660
661  const llvm::Type *BlockDescPtrTy = getBlockDescriptorType();
662
663  // struct __block_literal_generic {
664  //   void *__isa;
665  //   int __flags;
666  //   int __reserved;
667  //   void (*__invoke)(void *);
668  //   struct __block_descriptor *__descriptor;
669  // };
670  GenericBlockLiteralType = llvm::StructType::get(getLLVMContext(),
671                                                  VoidPtrTy,
672                                                  IntTy,
673                                                  IntTy,
674                                                  VoidPtrTy,
675                                                  BlockDescPtrTy,
676                                                  NULL);
677
678  getModule().addTypeName("struct.__block_literal_generic",
679                          GenericBlockLiteralType);
680
681  return GenericBlockLiteralType;
682}
683
684
685RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E,
686                                          ReturnValueSlot ReturnValue) {
687  const BlockPointerType *BPT =
688    E->getCallee()->getType()->getAs<BlockPointerType>();
689
690  llvm::Value *Callee = EmitScalarExpr(E->getCallee());
691
692  // Get a pointer to the generic block literal.
693  const llvm::Type *BlockLiteralTy =
694    llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType());
695
696  // Bitcast the callee to a block literal.
697  llvm::Value *BlockLiteral =
698    Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal");
699
700  // Get the function pointer from the literal.
701  llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp");
702
703  BlockLiteral = Builder.CreateBitCast(BlockLiteral, VoidPtrTy, "tmp");
704
705  // Add the block literal.
706  QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy);
707  CallArgList Args;
708  Args.add(RValue::get(BlockLiteral), VoidPtrTy);
709
710  QualType FnType = BPT->getPointeeType();
711
712  // And the rest of the arguments.
713  EmitCallArgs(Args, FnType->getAs<FunctionProtoType>(),
714               E->arg_begin(), E->arg_end());
715
716  // Load the function.
717  llvm::Value *Func = Builder.CreateLoad(FuncPtr, "tmp");
718
719  const FunctionType *FuncTy = FnType->castAs<FunctionType>();
720  QualType ResultType = FuncTy->getResultType();
721
722  const CGFunctionInfo &FnInfo =
723    CGM.getTypes().getFunctionInfo(ResultType, Args,
724                                   FuncTy->getExtInfo());
725
726  // Cast the function pointer to the right type.
727  const llvm::Type *BlockFTy =
728    CGM.getTypes().GetFunctionType(FnInfo, false);
729
730  const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy);
731  Func = Builder.CreateBitCast(Func, BlockFTyPtr);
732
733  // And call the block.
734  return EmitCall(FnInfo, Func, ReturnValue, Args);
735}
736
737llvm::Value *CodeGenFunction::GetAddrOfBlockDecl(const VarDecl *variable,
738                                                 bool isByRef) {
739  assert(BlockInfo && "evaluating block ref without block information?");
740  const CGBlockInfo::Capture &capture = BlockInfo->getCapture(variable);
741
742  // Handle constant captures.
743  if (capture.isConstant()) return LocalDeclMap[variable];
744
745  llvm::Value *addr =
746    Builder.CreateStructGEP(LoadBlockStruct(), capture.getIndex(),
747                            "block.capture.addr");
748
749  if (isByRef) {
750    // addr should be a void** right now.  Load, then cast the result
751    // to byref*.
752
753    addr = Builder.CreateLoad(addr);
754    const llvm::PointerType *byrefPointerType
755      = llvm::PointerType::get(BuildByRefType(variable), 0);
756    addr = Builder.CreateBitCast(addr, byrefPointerType,
757                                 "byref.addr");
758
759    // Follow the forwarding pointer.
760    addr = Builder.CreateStructGEP(addr, 1, "byref.forwarding");
761    addr = Builder.CreateLoad(addr, "byref.addr.forwarded");
762
763    // Cast back to byref* and GEP over to the actual object.
764    addr = Builder.CreateBitCast(addr, byrefPointerType);
765    addr = Builder.CreateStructGEP(addr, getByRefValueLLVMField(variable),
766                                   variable->getNameAsString());
767  }
768
769  if (variable->getType()->isReferenceType())
770    addr = Builder.CreateLoad(addr, "ref.tmp");
771
772  return addr;
773}
774
775llvm::Constant *
776CodeGenModule::GetAddrOfGlobalBlock(const BlockExpr *blockExpr,
777                                    const char *name) {
778  CGBlockInfo blockInfo(blockExpr, name);
779
780  // Compute information about the layout, etc., of this block.
781  computeBlockInfo(*this, blockInfo);
782
783  // Using that metadata, generate the actual block function.
784  llvm::Constant *blockFn;
785  {
786    llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap;
787    blockFn = CodeGenFunction(*this).GenerateBlockFunction(GlobalDecl(),
788                                                           blockInfo,
789                                                           0, LocalDeclMap);
790  }
791  blockFn = llvm::ConstantExpr::getBitCast(blockFn, VoidPtrTy);
792
793  return buildGlobalBlock(*this, blockInfo, blockFn);
794}
795
796static llvm::Constant *buildGlobalBlock(CodeGenModule &CGM,
797                                        const CGBlockInfo &blockInfo,
798                                        llvm::Constant *blockFn) {
799  assert(blockInfo.CanBeGlobal);
800
801  // Generate the constants for the block literal initializer.
802  llvm::Constant *fields[BlockHeaderSize];
803
804  // isa
805  fields[0] = CGM.getNSConcreteGlobalBlock();
806
807  // __flags
808  BlockFlags flags = BLOCK_IS_GLOBAL | BLOCK_HAS_SIGNATURE;
809  if (blockInfo.UsesStret) flags |= BLOCK_USE_STRET;
810
811  fields[1] = llvm::ConstantInt::get(CGM.IntTy, flags.getBitMask());
812
813  // Reserved
814  fields[2] = llvm::Constant::getNullValue(CGM.IntTy);
815
816  // Function
817  fields[3] = blockFn;
818
819  // Descriptor
820  fields[4] = buildBlockDescriptor(CGM, blockInfo);
821
822  llvm::Constant *init =
823    llvm::ConstantStruct::get(CGM.getLLVMContext(), fields, BlockHeaderSize,
824                              /*packed*/ false);
825
826  llvm::GlobalVariable *literal =
827    new llvm::GlobalVariable(CGM.getModule(),
828                             init->getType(),
829                             /*constant*/ true,
830                             llvm::GlobalVariable::InternalLinkage,
831                             init,
832                             "__block_literal_global");
833  literal->setAlignment(blockInfo.BlockAlign.getQuantity());
834
835  // Return a constant of the appropriately-casted type.
836  const llvm::Type *requiredType =
837    CGM.getTypes().ConvertType(blockInfo.getBlockExpr()->getType());
838  return llvm::ConstantExpr::getBitCast(literal, requiredType);
839}
840
841llvm::Function *
842CodeGenFunction::GenerateBlockFunction(GlobalDecl GD,
843                                       const CGBlockInfo &blockInfo,
844                                       const Decl *outerFnDecl,
845                                       const DeclMapTy &ldm) {
846  const BlockDecl *blockDecl = blockInfo.getBlockDecl();
847
848  // Check if we should generate debug info for this block function.
849  if (CGM.getModuleDebugInfo())
850    DebugInfo = CGM.getModuleDebugInfo();
851
852  BlockInfo = &blockInfo;
853
854  // Arrange for local static and local extern declarations to appear
855  // to be local to this function as well, in case they're directly
856  // referenced in a block.
857  for (DeclMapTy::const_iterator i = ldm.begin(), e = ldm.end(); i != e; ++i) {
858    const VarDecl *var = dyn_cast<VarDecl>(i->first);
859    if (var && !var->hasLocalStorage())
860      LocalDeclMap[var] = i->second;
861  }
862
863  // Begin building the function declaration.
864
865  // Build the argument list.
866  FunctionArgList args;
867
868  // The first argument is the block pointer.  Just take it as a void*
869  // and cast it later.
870  QualType selfTy = getContext().VoidPtrTy;
871  IdentifierInfo *II = &CGM.getContext().Idents.get(".block_descriptor");
872
873  ImplicitParamDecl selfDecl(const_cast<BlockDecl*>(blockDecl),
874                             SourceLocation(), II, selfTy);
875  args.push_back(&selfDecl);
876
877  // Now add the rest of the parameters.
878  for (BlockDecl::param_const_iterator i = blockDecl->param_begin(),
879       e = blockDecl->param_end(); i != e; ++i)
880    args.push_back(*i);
881
882  // Create the function declaration.
883  const FunctionProtoType *fnType =
884    cast<FunctionProtoType>(blockInfo.getBlockExpr()->getFunctionType());
885  const CGFunctionInfo &fnInfo =
886    CGM.getTypes().getFunctionInfo(fnType->getResultType(), args,
887                                   fnType->getExtInfo());
888  if (CGM.ReturnTypeUsesSRet(fnInfo))
889    blockInfo.UsesStret = true;
890
891  const llvm::FunctionType *fnLLVMType =
892    CGM.getTypes().GetFunctionType(fnInfo, fnType->isVariadic());
893
894  MangleBuffer name;
895  CGM.getBlockMangledName(GD, name, blockDecl);
896  llvm::Function *fn =
897    llvm::Function::Create(fnLLVMType, llvm::GlobalValue::InternalLinkage,
898                           name.getString(), &CGM.getModule());
899  CGM.SetInternalFunctionAttributes(blockDecl, fn, fnInfo);
900
901  // Begin generating the function.
902  StartFunction(blockDecl, fnType->getResultType(), fn, fnInfo, args,
903                blockInfo.getBlockExpr()->getBody()->getLocStart());
904  CurFuncDecl = outerFnDecl; // StartFunction sets this to blockDecl
905
906  // Okay.  Undo some of what StartFunction did.
907
908  // Pull the 'self' reference out of the local decl map.
909  llvm::Value *blockAddr = LocalDeclMap[&selfDecl];
910  LocalDeclMap.erase(&selfDecl);
911  BlockPointer = Builder.CreateBitCast(blockAddr,
912                                       blockInfo.StructureType->getPointerTo(),
913                                       "block");
914
915  // If we have a C++ 'this' reference, go ahead and force it into
916  // existence now.
917  if (blockDecl->capturesCXXThis()) {
918    llvm::Value *addr = Builder.CreateStructGEP(BlockPointer,
919                                                blockInfo.CXXThisIndex,
920                                                "block.captured-this");
921    CXXThisValue = Builder.CreateLoad(addr, "this");
922  }
923
924  // LoadObjCSelf() expects there to be an entry for 'self' in LocalDeclMap;
925  // appease it.
926  if (const ObjCMethodDecl *method
927        = dyn_cast_or_null<ObjCMethodDecl>(CurFuncDecl)) {
928    const VarDecl *self = method->getSelfDecl();
929
930    // There might not be a capture for 'self', but if there is...
931    if (blockInfo.Captures.count(self)) {
932      const CGBlockInfo::Capture &capture = blockInfo.getCapture(self);
933      llvm::Value *selfAddr = Builder.CreateStructGEP(BlockPointer,
934                                                      capture.getIndex(),
935                                                      "block.captured-self");
936      LocalDeclMap[self] = selfAddr;
937    }
938  }
939
940  // Also force all the constant captures.
941  for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
942         ce = blockDecl->capture_end(); ci != ce; ++ci) {
943    const VarDecl *variable = ci->getVariable();
944    const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
945    if (!capture.isConstant()) continue;
946
947    unsigned align = getContext().getDeclAlign(variable).getQuantity();
948
949    llvm::AllocaInst *alloca =
950      CreateMemTemp(variable->getType(), "block.captured-const");
951    alloca->setAlignment(align);
952
953    Builder.CreateStore(capture.getConstant(), alloca, align);
954
955    LocalDeclMap[variable] = alloca;
956  }
957
958  // Save a spot to insert the debug information for all the BlockDeclRefDecls.
959  llvm::BasicBlock *entry = Builder.GetInsertBlock();
960  llvm::BasicBlock::iterator entry_ptr = Builder.GetInsertPoint();
961  --entry_ptr;
962
963  EmitStmt(blockDecl->getBody());
964
965  // Remember where we were...
966  llvm::BasicBlock *resume = Builder.GetInsertBlock();
967
968  // Go back to the entry.
969  ++entry_ptr;
970  Builder.SetInsertPoint(entry, entry_ptr);
971
972  // Emit debug information for all the BlockDeclRefDecls.
973  // FIXME: also for 'this'
974  if (CGDebugInfo *DI = getDebugInfo()) {
975    for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
976           ce = blockDecl->capture_end(); ci != ce; ++ci) {
977      const VarDecl *variable = ci->getVariable();
978      DI->setLocation(variable->getLocation());
979
980      const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
981      if (capture.isConstant()) {
982        DI->EmitDeclareOfAutoVariable(variable, LocalDeclMap[variable],
983                                      Builder);
984        continue;
985      }
986
987      DI->EmitDeclareOfBlockDeclRefVariable(variable, BlockPointer,
988                                            Builder, blockInfo);
989    }
990  }
991
992  // And resume where we left off.
993  if (resume == 0)
994    Builder.ClearInsertionPoint();
995  else
996    Builder.SetInsertPoint(resume);
997
998  FinishFunction(cast<CompoundStmt>(blockDecl->getBody())->getRBracLoc());
999
1000  return fn;
1001}
1002
1003/*
1004    notes.push_back(HelperInfo());
1005    HelperInfo &note = notes.back();
1006    note.index = capture.getIndex();
1007    note.RequiresCopying = (ci->hasCopyExpr() || BlockRequiresCopying(type));
1008    note.cxxbar_import = ci->getCopyExpr();
1009
1010    if (ci->isByRef()) {
1011      note.flag = BLOCK_FIELD_IS_BYREF;
1012      if (type.isObjCGCWeak())
1013        note.flag |= BLOCK_FIELD_IS_WEAK;
1014    } else if (type->isBlockPointerType()) {
1015      note.flag = BLOCK_FIELD_IS_BLOCK;
1016    } else {
1017      note.flag = BLOCK_FIELD_IS_OBJECT;
1018    }
1019 */
1020
1021
1022
1023
1024
1025llvm::Constant *
1026CodeGenFunction::GenerateCopyHelperFunction(const CGBlockInfo &blockInfo) {
1027  ASTContext &C = getContext();
1028
1029  FunctionArgList args;
1030  ImplicitParamDecl dstDecl(0, SourceLocation(), 0, C.VoidPtrTy);
1031  args.push_back(&dstDecl);
1032  ImplicitParamDecl srcDecl(0, SourceLocation(), 0, C.VoidPtrTy);
1033  args.push_back(&srcDecl);
1034
1035  const CGFunctionInfo &FI =
1036      CGM.getTypes().getFunctionInfo(C.VoidTy, args, FunctionType::ExtInfo());
1037
1038  // FIXME: it would be nice if these were mergeable with things with
1039  // identical semantics.
1040  const llvm::FunctionType *LTy = CGM.getTypes().GetFunctionType(FI, false);
1041
1042  llvm::Function *Fn =
1043    llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
1044                           "__copy_helper_block_", &CGM.getModule());
1045
1046  IdentifierInfo *II
1047    = &CGM.getContext().Idents.get("__copy_helper_block_");
1048
1049  // Check if we should generate debug info for this block helper function.
1050  if (CGM.getModuleDebugInfo())
1051    DebugInfo = CGM.getModuleDebugInfo();
1052
1053  FunctionDecl *FD = FunctionDecl::Create(C,
1054                                          C.getTranslationUnitDecl(),
1055                                          SourceLocation(),
1056                                          SourceLocation(), II, C.VoidTy, 0,
1057                                          SC_Static,
1058                                          SC_None,
1059                                          false,
1060                                          true);
1061  StartFunction(FD, C.VoidTy, Fn, FI, args, SourceLocation());
1062
1063  const llvm::Type *structPtrTy = blockInfo.StructureType->getPointerTo();
1064
1065  llvm::Value *src = GetAddrOfLocalVar(&srcDecl);
1066  src = Builder.CreateLoad(src);
1067  src = Builder.CreateBitCast(src, structPtrTy, "block.source");
1068
1069  llvm::Value *dst = GetAddrOfLocalVar(&dstDecl);
1070  dst = Builder.CreateLoad(dst);
1071  dst = Builder.CreateBitCast(dst, structPtrTy, "block.dest");
1072
1073  const BlockDecl *blockDecl = blockInfo.getBlockDecl();
1074
1075  for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
1076         ce = blockDecl->capture_end(); ci != ce; ++ci) {
1077    const VarDecl *variable = ci->getVariable();
1078    QualType type = variable->getType();
1079
1080    const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
1081    if (capture.isConstant()) continue;
1082
1083    const Expr *copyExpr = ci->getCopyExpr();
1084    unsigned flags = 0;
1085
1086    if (copyExpr) {
1087      assert(!ci->isByRef());
1088      // don't bother computing flags
1089    } else if (ci->isByRef()) {
1090      flags = BLOCK_FIELD_IS_BYREF;
1091      if (type.isObjCGCWeak()) flags |= BLOCK_FIELD_IS_WEAK;
1092    } else if (type->isBlockPointerType()) {
1093      flags = BLOCK_FIELD_IS_BLOCK;
1094    } else if (type->isObjCObjectPointerType() || C.isObjCNSObjectType(type)) {
1095      flags = BLOCK_FIELD_IS_OBJECT;
1096    }
1097
1098    if (!copyExpr && !flags) continue;
1099
1100    unsigned index = capture.getIndex();
1101    llvm::Value *srcField = Builder.CreateStructGEP(src, index);
1102    llvm::Value *dstField = Builder.CreateStructGEP(dst, index);
1103
1104    // If there's an explicit copy expression, we do that.
1105    if (copyExpr) {
1106      EmitSynthesizedCXXCopyCtor(dstField, srcField, copyExpr);
1107    } else {
1108      llvm::Value *srcValue = Builder.CreateLoad(srcField, "blockcopy.src");
1109      srcValue = Builder.CreateBitCast(srcValue, VoidPtrTy);
1110      llvm::Value *dstAddr = Builder.CreateBitCast(dstField, VoidPtrTy);
1111      Builder.CreateCall3(CGM.getBlockObjectAssign(), dstAddr, srcValue,
1112                          llvm::ConstantInt::get(Int32Ty, flags));
1113    }
1114  }
1115
1116  FinishFunction();
1117
1118  return llvm::ConstantExpr::getBitCast(Fn, VoidPtrTy);
1119}
1120
1121llvm::Constant *
1122CodeGenFunction::GenerateDestroyHelperFunction(const CGBlockInfo &blockInfo) {
1123  ASTContext &C = getContext();
1124
1125  FunctionArgList args;
1126  ImplicitParamDecl srcDecl(0, SourceLocation(), 0, C.VoidPtrTy);
1127  args.push_back(&srcDecl);
1128
1129  const CGFunctionInfo &FI =
1130      CGM.getTypes().getFunctionInfo(C.VoidTy, args, FunctionType::ExtInfo());
1131
1132  // FIXME: We'd like to put these into a mergable by content, with
1133  // internal linkage.
1134  const llvm::FunctionType *LTy = CGM.getTypes().GetFunctionType(FI, false);
1135
1136  llvm::Function *Fn =
1137    llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
1138                           "__destroy_helper_block_", &CGM.getModule());
1139
1140  // Check if we should generate debug info for this block destroy function.
1141  if (CGM.getModuleDebugInfo())
1142    DebugInfo = CGM.getModuleDebugInfo();
1143
1144  IdentifierInfo *II
1145    = &CGM.getContext().Idents.get("__destroy_helper_block_");
1146
1147  FunctionDecl *FD = FunctionDecl::Create(C, C.getTranslationUnitDecl(),
1148                                          SourceLocation(),
1149                                          SourceLocation(), II, C.VoidTy, 0,
1150                                          SC_Static,
1151                                          SC_None,
1152                                          false, true);
1153  StartFunction(FD, C.VoidTy, Fn, FI, args, SourceLocation());
1154
1155  const llvm::Type *structPtrTy = blockInfo.StructureType->getPointerTo();
1156
1157  llvm::Value *src = GetAddrOfLocalVar(&srcDecl);
1158  src = Builder.CreateLoad(src);
1159  src = Builder.CreateBitCast(src, structPtrTy, "block");
1160
1161  const BlockDecl *blockDecl = blockInfo.getBlockDecl();
1162
1163  CodeGenFunction::RunCleanupsScope cleanups(*this);
1164
1165  for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
1166         ce = blockDecl->capture_end(); ci != ce; ++ci) {
1167    const VarDecl *variable = ci->getVariable();
1168    QualType type = variable->getType();
1169
1170    const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
1171    if (capture.isConstant()) continue;
1172
1173    BlockFieldFlags flags;
1174    const CXXDestructorDecl *dtor = 0;
1175
1176    if (ci->isByRef()) {
1177      flags = BLOCK_FIELD_IS_BYREF;
1178      if (type.isObjCGCWeak()) flags |= BLOCK_FIELD_IS_WEAK;
1179    } else if (type->isBlockPointerType()) {
1180      flags = BLOCK_FIELD_IS_BLOCK;
1181    } else if (type->isObjCObjectPointerType() || C.isObjCNSObjectType(type)) {
1182      flags = BLOCK_FIELD_IS_OBJECT;
1183    } else if (C.getLangOptions().CPlusPlus) {
1184      if (const CXXRecordDecl *record = type->getAsCXXRecordDecl())
1185        if (!record->hasTrivialDestructor())
1186          dtor = record->getDestructor();
1187    }
1188
1189    if (!dtor && flags.empty()) continue;
1190
1191    unsigned index = capture.getIndex();
1192    llvm::Value *srcField = Builder.CreateStructGEP(src, index);
1193
1194    // If there's an explicit copy expression, we do that.
1195    if (dtor) {
1196      PushDestructorCleanup(dtor, srcField);
1197
1198    // Otherwise we call _Block_object_dispose.  It wouldn't be too
1199    // hard to just emit this as a cleanup if we wanted to make sure
1200    // that things were done in reverse.
1201    } else {
1202      llvm::Value *value = Builder.CreateLoad(srcField);
1203      value = Builder.CreateBitCast(value, VoidPtrTy);
1204      BuildBlockRelease(value, flags);
1205    }
1206  }
1207
1208  cleanups.ForceCleanup();
1209
1210  FinishFunction();
1211
1212  return llvm::ConstantExpr::getBitCast(Fn, VoidPtrTy);
1213}
1214
1215namespace {
1216
1217/// Emits the copy/dispose helper functions for a __block object of id type.
1218class ObjectByrefHelpers : public CodeGenModule::ByrefHelpers {
1219  BlockFieldFlags Flags;
1220
1221public:
1222  ObjectByrefHelpers(CharUnits alignment, BlockFieldFlags flags)
1223    : ByrefHelpers(alignment), Flags(flags) {}
1224
1225  void emitCopy(CodeGenFunction &CGF, llvm::Value *destField,
1226                llvm::Value *srcField) {
1227    destField = CGF.Builder.CreateBitCast(destField, CGF.VoidPtrTy);
1228
1229    srcField = CGF.Builder.CreateBitCast(srcField, CGF.VoidPtrPtrTy);
1230    llvm::Value *srcValue = CGF.Builder.CreateLoad(srcField);
1231
1232    unsigned flags = (Flags | BLOCK_BYREF_CALLER).getBitMask();
1233
1234    llvm::Value *flagsVal = llvm::ConstantInt::get(CGF.Int32Ty, flags);
1235    llvm::Value *fn = CGF.CGM.getBlockObjectAssign();
1236    CGF.Builder.CreateCall3(fn, destField, srcValue, flagsVal);
1237  }
1238
1239  void emitDispose(CodeGenFunction &CGF, llvm::Value *field) {
1240    field = CGF.Builder.CreateBitCast(field, CGF.Int8PtrTy->getPointerTo(0));
1241    llvm::Value *value = CGF.Builder.CreateLoad(field);
1242
1243    CGF.BuildBlockRelease(value, Flags | BLOCK_BYREF_CALLER);
1244  }
1245
1246  void profileImpl(llvm::FoldingSetNodeID &id) const {
1247    id.AddInteger(Flags.getBitMask());
1248  }
1249};
1250
1251/// Emits the copy/dispose helpers for a __block variable with a
1252/// nontrivial copy constructor or destructor.
1253class CXXByrefHelpers : public CodeGenModule::ByrefHelpers {
1254  QualType VarType;
1255  const Expr *CopyExpr;
1256
1257public:
1258  CXXByrefHelpers(CharUnits alignment, QualType type,
1259                  const Expr *copyExpr)
1260    : ByrefHelpers(alignment), VarType(type), CopyExpr(copyExpr) {}
1261
1262  bool needsCopy() const { return CopyExpr != 0; }
1263  void emitCopy(CodeGenFunction &CGF, llvm::Value *destField,
1264                llvm::Value *srcField) {
1265    if (!CopyExpr) return;
1266    CGF.EmitSynthesizedCXXCopyCtor(destField, srcField, CopyExpr);
1267  }
1268
1269  void emitDispose(CodeGenFunction &CGF, llvm::Value *field) {
1270    EHScopeStack::stable_iterator cleanupDepth = CGF.EHStack.stable_begin();
1271    CGF.PushDestructorCleanup(VarType, field);
1272    CGF.PopCleanupBlocks(cleanupDepth);
1273  }
1274
1275  void profileImpl(llvm::FoldingSetNodeID &id) const {
1276    id.AddPointer(VarType.getCanonicalType().getAsOpaquePtr());
1277  }
1278};
1279} // end anonymous namespace
1280
1281static llvm::Constant *
1282generateByrefCopyHelper(CodeGenFunction &CGF,
1283                        const llvm::StructType &byrefType,
1284                        CodeGenModule::ByrefHelpers &byrefInfo) {
1285  ASTContext &Context = CGF.getContext();
1286
1287  QualType R = Context.VoidTy;
1288
1289  FunctionArgList args;
1290  ImplicitParamDecl dst(0, SourceLocation(), 0, Context.VoidPtrTy);
1291  args.push_back(&dst);
1292
1293  ImplicitParamDecl src(0, SourceLocation(), 0, Context.VoidPtrTy);
1294  args.push_back(&src);
1295
1296  const CGFunctionInfo &FI =
1297    CGF.CGM.getTypes().getFunctionInfo(R, args, FunctionType::ExtInfo());
1298
1299  CodeGenTypes &Types = CGF.CGM.getTypes();
1300  const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
1301
1302  // FIXME: We'd like to put these into a mergable by content, with
1303  // internal linkage.
1304  llvm::Function *Fn =
1305    llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
1306                           "__Block_byref_object_copy_", &CGF.CGM.getModule());
1307
1308  IdentifierInfo *II
1309    = &Context.Idents.get("__Block_byref_object_copy_");
1310
1311  FunctionDecl *FD = FunctionDecl::Create(Context,
1312                                          Context.getTranslationUnitDecl(),
1313                                          SourceLocation(),
1314                                          SourceLocation(), II, R, 0,
1315                                          SC_Static,
1316                                          SC_None,
1317                                          false, true);
1318  CGF.StartFunction(FD, R, Fn, FI, args, SourceLocation());
1319
1320  if (byrefInfo.needsCopy()) {
1321    const llvm::Type *byrefPtrType = byrefType.getPointerTo(0);
1322
1323    // dst->x
1324    llvm::Value *destField = CGF.GetAddrOfLocalVar(&dst);
1325    destField = CGF.Builder.CreateLoad(destField);
1326    destField = CGF.Builder.CreateBitCast(destField, byrefPtrType);
1327    destField = CGF.Builder.CreateStructGEP(destField, 6, "x");
1328
1329    // src->x
1330    llvm::Value *srcField = CGF.GetAddrOfLocalVar(&src);
1331    srcField = CGF.Builder.CreateLoad(srcField);
1332    srcField = CGF.Builder.CreateBitCast(srcField, byrefPtrType);
1333    srcField = CGF.Builder.CreateStructGEP(srcField, 6, "x");
1334
1335    byrefInfo.emitCopy(CGF, destField, srcField);
1336  }
1337
1338  CGF.FinishFunction();
1339
1340  return llvm::ConstantExpr::getBitCast(Fn, CGF.Int8PtrTy);
1341}
1342
1343/// Build the copy helper for a __block variable.
1344static llvm::Constant *buildByrefCopyHelper(CodeGenModule &CGM,
1345                                            const llvm::StructType &byrefType,
1346                                            CodeGenModule::ByrefHelpers &info) {
1347  CodeGenFunction CGF(CGM);
1348  return generateByrefCopyHelper(CGF, byrefType, info);
1349}
1350
1351/// Generate code for a __block variable's dispose helper.
1352static llvm::Constant *
1353generateByrefDisposeHelper(CodeGenFunction &CGF,
1354                           const llvm::StructType &byrefType,
1355                           CodeGenModule::ByrefHelpers &byrefInfo) {
1356  ASTContext &Context = CGF.getContext();
1357  QualType R = Context.VoidTy;
1358
1359  FunctionArgList args;
1360  ImplicitParamDecl src(0, SourceLocation(), 0, Context.VoidPtrTy);
1361  args.push_back(&src);
1362
1363  const CGFunctionInfo &FI =
1364    CGF.CGM.getTypes().getFunctionInfo(R, args, FunctionType::ExtInfo());
1365
1366  CodeGenTypes &Types = CGF.CGM.getTypes();
1367  const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
1368
1369  // FIXME: We'd like to put these into a mergable by content, with
1370  // internal linkage.
1371  llvm::Function *Fn =
1372    llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
1373                           "__Block_byref_object_dispose_",
1374                           &CGF.CGM.getModule());
1375
1376  IdentifierInfo *II
1377    = &Context.Idents.get("__Block_byref_object_dispose_");
1378
1379  FunctionDecl *FD = FunctionDecl::Create(Context,
1380                                          Context.getTranslationUnitDecl(),
1381                                          SourceLocation(),
1382                                          SourceLocation(), II, R, 0,
1383                                          SC_Static,
1384                                          SC_None,
1385                                          false, true);
1386  CGF.StartFunction(FD, R, Fn, FI, args, SourceLocation());
1387
1388  if (byrefInfo.needsDispose()) {
1389    llvm::Value *V = CGF.GetAddrOfLocalVar(&src);
1390    V = CGF.Builder.CreateLoad(V);
1391    V = CGF.Builder.CreateBitCast(V, byrefType.getPointerTo(0));
1392    V = CGF.Builder.CreateStructGEP(V, 6, "x");
1393
1394    byrefInfo.emitDispose(CGF, V);
1395  }
1396
1397  CGF.FinishFunction();
1398
1399  return llvm::ConstantExpr::getBitCast(Fn, CGF.Int8PtrTy);
1400}
1401
1402/// Build the dispose helper for a __block variable.
1403static llvm::Constant *buildByrefDisposeHelper(CodeGenModule &CGM,
1404                                              const llvm::StructType &byrefType,
1405                                            CodeGenModule::ByrefHelpers &info) {
1406  CodeGenFunction CGF(CGM);
1407  return generateByrefDisposeHelper(CGF, byrefType, info);
1408}
1409
1410///
1411template <class T> static T *buildByrefHelpers(CodeGenModule &CGM,
1412                                               const llvm::StructType &byrefTy,
1413                                               T &byrefInfo) {
1414  // Increase the field's alignment to be at least pointer alignment,
1415  // since the layout of the byref struct will guarantee at least that.
1416  byrefInfo.Alignment = std::max(byrefInfo.Alignment,
1417                              CharUnits::fromQuantity(CGM.PointerAlignInBytes));
1418
1419  llvm::FoldingSetNodeID id;
1420  byrefInfo.Profile(id);
1421
1422  void *insertPos;
1423  CodeGenModule::ByrefHelpers *node
1424    = CGM.ByrefHelpersCache.FindNodeOrInsertPos(id, insertPos);
1425  if (node) return static_cast<T*>(node);
1426
1427  byrefInfo.CopyHelper = buildByrefCopyHelper(CGM, byrefTy, byrefInfo);
1428  byrefInfo.DisposeHelper = buildByrefDisposeHelper(CGM, byrefTy, byrefInfo);
1429
1430  T *copy = new (CGM.getContext()) T(byrefInfo);
1431  CGM.ByrefHelpersCache.InsertNode(copy, insertPos);
1432  return copy;
1433}
1434
1435CodeGenModule::ByrefHelpers *
1436CodeGenFunction::buildByrefHelpers(const llvm::StructType &byrefType,
1437                                   const AutoVarEmission &emission) {
1438  const VarDecl &var = *emission.Variable;
1439  QualType type = var.getType();
1440
1441  if (const CXXRecordDecl *record = type->getAsCXXRecordDecl()) {
1442    const Expr *copyExpr = CGM.getContext().getBlockVarCopyInits(&var);
1443    if (!copyExpr && record->hasTrivialDestructor()) return 0;
1444
1445    CXXByrefHelpers byrefInfo(emission.Alignment, type, copyExpr);
1446    return ::buildByrefHelpers(CGM, byrefType, byrefInfo);
1447  }
1448
1449  BlockFieldFlags flags;
1450  if (type->isBlockPointerType()) {
1451    flags |= BLOCK_FIELD_IS_BLOCK;
1452  } else if (CGM.getContext().isObjCNSObjectType(type) ||
1453             type->isObjCObjectPointerType()) {
1454    flags |= BLOCK_FIELD_IS_OBJECT;
1455  } else {
1456    return 0;
1457  }
1458
1459  if (type.isObjCGCWeak())
1460    flags |= BLOCK_FIELD_IS_WEAK;
1461
1462  ObjectByrefHelpers byrefInfo(emission.Alignment, flags);
1463  return ::buildByrefHelpers(CGM, byrefType, byrefInfo);
1464}
1465
1466unsigned CodeGenFunction::getByRefValueLLVMField(const ValueDecl *VD) const {
1467  assert(ByRefValueInfo.count(VD) && "Did not find value!");
1468
1469  return ByRefValueInfo.find(VD)->second.second;
1470}
1471
1472llvm::Value *CodeGenFunction::BuildBlockByrefAddress(llvm::Value *BaseAddr,
1473                                                     const VarDecl *V) {
1474  llvm::Value *Loc = Builder.CreateStructGEP(BaseAddr, 1, "forwarding");
1475  Loc = Builder.CreateLoad(Loc);
1476  Loc = Builder.CreateStructGEP(Loc, getByRefValueLLVMField(V),
1477                                V->getNameAsString());
1478  return Loc;
1479}
1480
1481/// BuildByRefType - This routine changes a __block variable declared as T x
1482///   into:
1483///
1484///      struct {
1485///        void *__isa;
1486///        void *__forwarding;
1487///        int32_t __flags;
1488///        int32_t __size;
1489///        void *__copy_helper;       // only if needed
1490///        void *__destroy_helper;    // only if needed
1491///        char padding[X];           // only if needed
1492///        T x;
1493///      } x
1494///
1495const llvm::Type *CodeGenFunction::BuildByRefType(const VarDecl *D) {
1496  std::pair<const llvm::Type *, unsigned> &Info = ByRefValueInfo[D];
1497  if (Info.first)
1498    return Info.first;
1499
1500  QualType Ty = D->getType();
1501
1502  std::vector<const llvm::Type *> Types;
1503
1504  llvm::PATypeHolder ByRefTypeHolder = llvm::OpaqueType::get(getLLVMContext());
1505
1506  // void *__isa;
1507  Types.push_back(Int8PtrTy);
1508
1509  // void *__forwarding;
1510  Types.push_back(llvm::PointerType::getUnqual(ByRefTypeHolder));
1511
1512  // int32_t __flags;
1513  Types.push_back(Int32Ty);
1514
1515  // int32_t __size;
1516  Types.push_back(Int32Ty);
1517
1518  bool HasCopyAndDispose = getContext().BlockRequiresCopying(Ty);
1519  if (HasCopyAndDispose) {
1520    /// void *__copy_helper;
1521    Types.push_back(Int8PtrTy);
1522
1523    /// void *__destroy_helper;
1524    Types.push_back(Int8PtrTy);
1525  }
1526
1527  bool Packed = false;
1528  CharUnits Align = getContext().getDeclAlign(D);
1529  if (Align > getContext().toCharUnitsFromBits(Target.getPointerAlign(0))) {
1530    // We have to insert padding.
1531
1532    // The struct above has 2 32-bit integers.
1533    unsigned CurrentOffsetInBytes = 4 * 2;
1534
1535    // And either 2 or 4 pointers.
1536    CurrentOffsetInBytes += (HasCopyAndDispose ? 4 : 2) *
1537      CGM.getTargetData().getTypeAllocSize(Int8PtrTy);
1538
1539    // Align the offset.
1540    unsigned AlignedOffsetInBytes =
1541      llvm::RoundUpToAlignment(CurrentOffsetInBytes, Align.getQuantity());
1542
1543    unsigned NumPaddingBytes = AlignedOffsetInBytes - CurrentOffsetInBytes;
1544    if (NumPaddingBytes > 0) {
1545      const llvm::Type *Ty = llvm::Type::getInt8Ty(getLLVMContext());
1546      // FIXME: We need a sema error for alignment larger than the minimum of
1547      // the maximal stack alignmint and the alignment of malloc on the system.
1548      if (NumPaddingBytes > 1)
1549        Ty = llvm::ArrayType::get(Ty, NumPaddingBytes);
1550
1551      Types.push_back(Ty);
1552
1553      // We want a packed struct.
1554      Packed = true;
1555    }
1556  }
1557
1558  // T x;
1559  Types.push_back(ConvertTypeForMem(Ty));
1560
1561  const llvm::Type *T = llvm::StructType::get(getLLVMContext(), Types, Packed);
1562
1563  cast<llvm::OpaqueType>(ByRefTypeHolder.get())->refineAbstractTypeTo(T);
1564  CGM.getModule().addTypeName("struct.__block_byref_" + D->getNameAsString(),
1565                              ByRefTypeHolder.get());
1566
1567  Info.first = ByRefTypeHolder.get();
1568
1569  Info.second = Types.size() - 1;
1570
1571  return Info.first;
1572}
1573
1574/// Initialize the structural components of a __block variable, i.e.
1575/// everything but the actual object.
1576void CodeGenFunction::emitByrefStructureInit(const AutoVarEmission &emission) {
1577  // Find the address of the local.
1578  llvm::Value *addr = emission.Address;
1579
1580  // That's an alloca of the byref structure type.
1581  const llvm::StructType *byrefType = cast<llvm::StructType>(
1582                 cast<llvm::PointerType>(addr->getType())->getElementType());
1583
1584  // Build the byref helpers if necessary.  This is null if we don't need any.
1585  CodeGenModule::ByrefHelpers *helpers =
1586    buildByrefHelpers(*byrefType, emission);
1587
1588  const VarDecl &D = *emission.Variable;
1589  QualType type = D.getType();
1590
1591  llvm::Value *V;
1592
1593  // Initialize the 'isa', which is just 0 or 1.
1594  int isa = 0;
1595  if (type.isObjCGCWeak())
1596    isa = 1;
1597  V = Builder.CreateIntToPtr(Builder.getInt32(isa), Int8PtrTy, "isa");
1598  Builder.CreateStore(V, Builder.CreateStructGEP(addr, 0, "byref.isa"));
1599
1600  // Store the address of the variable into its own forwarding pointer.
1601  Builder.CreateStore(addr,
1602                      Builder.CreateStructGEP(addr, 1, "byref.forwarding"));
1603
1604  // Blocks ABI:
1605  //   c) the flags field is set to either 0 if no helper functions are
1606  //      needed or BLOCK_HAS_COPY_DISPOSE if they are,
1607  BlockFlags flags;
1608  if (helpers) flags |= BLOCK_HAS_COPY_DISPOSE;
1609  Builder.CreateStore(llvm::ConstantInt::get(IntTy, flags.getBitMask()),
1610                      Builder.CreateStructGEP(addr, 2, "byref.flags"));
1611
1612  CharUnits byrefSize = CGM.GetTargetTypeStoreSize(byrefType);
1613  V = llvm::ConstantInt::get(IntTy, byrefSize.getQuantity());
1614  Builder.CreateStore(V, Builder.CreateStructGEP(addr, 3, "byref.size"));
1615
1616  if (helpers) {
1617    llvm::Value *copy_helper = Builder.CreateStructGEP(addr, 4);
1618    Builder.CreateStore(helpers->CopyHelper, copy_helper);
1619
1620    llvm::Value *destroy_helper = Builder.CreateStructGEP(addr, 5);
1621    Builder.CreateStore(helpers->DisposeHelper, destroy_helper);
1622  }
1623}
1624
1625void CodeGenFunction::BuildBlockRelease(llvm::Value *V, BlockFieldFlags flags) {
1626  llvm::Value *F = CGM.getBlockObjectDispose();
1627  llvm::Value *N;
1628  V = Builder.CreateBitCast(V, Int8PtrTy);
1629  N = llvm::ConstantInt::get(Int32Ty, flags.getBitMask());
1630  Builder.CreateCall2(F, V, N);
1631}
1632
1633namespace {
1634  struct CallBlockRelease : EHScopeStack::Cleanup {
1635    llvm::Value *Addr;
1636    CallBlockRelease(llvm::Value *Addr) : Addr(Addr) {}
1637
1638    void Emit(CodeGenFunction &CGF, bool IsForEH) {
1639      CGF.BuildBlockRelease(Addr, BLOCK_FIELD_IS_BYREF);
1640    }
1641  };
1642}
1643
1644/// Enter a cleanup to destroy a __block variable.  Note that this
1645/// cleanup should be a no-op if the variable hasn't left the stack
1646/// yet; if a cleanup is required for the variable itself, that needs
1647/// to be done externally.
1648void CodeGenFunction::enterByrefCleanup(const AutoVarEmission &emission) {
1649  // We don't enter this cleanup if we're in pure-GC mode.
1650  if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
1651    return;
1652
1653  EHStack.pushCleanup<CallBlockRelease>(NormalAndEHCleanup, emission.Address);
1654}
1655