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