2009-02-12 08:39:25 +08:00
|
|
|
//===--- CGBlocks.cpp - Emit LLVM Code for declarations -------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This contains code to emit blocks.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "CGBlocks.h"
|
2009-09-30 10:43:10 +08:00
|
|
|
#include "CGDebugInfo.h"
|
2010-02-11 07:34:57 +08:00
|
|
|
#include "CGObjCRuntime.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "CodeGenFunction.h"
|
2009-02-12 08:39:25 +08:00
|
|
|
#include "CodeGenModule.h"
|
2009-03-21 05:53:12 +08:00
|
|
|
#include "clang/AST/DeclObjC.h"
|
2010-03-31 23:04:05 +08:00
|
|
|
#include "llvm/ADT/SmallSet.h"
|
2013-01-02 19:45:17 +08:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2013-03-01 03:01:20 +08:00
|
|
|
#include "llvm/Support/CallSite.h"
|
2009-02-12 08:39:25 +08:00
|
|
|
#include <algorithm>
|
2012-11-15 01:43:08 +08:00
|
|
|
#include <cstdio>
|
2009-08-24 21:25:12 +08:00
|
|
|
|
2009-02-12 08:39:25 +08:00
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
|
|
|
|
2011-11-10 16:15:53 +08:00
|
|
|
CGBlockInfo::CGBlockInfo(const BlockDecl *block, StringRef name)
|
|
|
|
: Name(name), CXXThisIndex(0), CanBeGlobal(false), NeedsCopyDispose(false),
|
2012-11-02 02:32:55 +08:00
|
|
|
HasCXXObject(false), UsesStret(false), HasCapturedVariableLayout(false),
|
|
|
|
StructureType(0), Block(block),
|
2011-11-10 18:43:54 +08:00
|
|
|
DominatingIP(0) {
|
2010-05-21 12:11:14 +08:00
|
|
|
|
2011-11-10 16:15:53 +08:00
|
|
|
// Skip asm prefix, if any. 'name' is usually taken directly from
|
|
|
|
// the mangled name of the enclosing function.
|
|
|
|
if (!name.empty() && name[0] == '\01')
|
|
|
|
name = name.substr(1);
|
2010-05-21 12:11:14 +08:00
|
|
|
}
|
|
|
|
|
2011-03-31 16:03:29 +08:00
|
|
|
// Anchor the vtable to this translation unit.
|
|
|
|
CodeGenModule::ByrefHelpers::~ByrefHelpers() {}
|
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
/// Build the given block as a global block.
|
|
|
|
static llvm::Constant *buildGlobalBlock(CodeGenModule &CGM,
|
|
|
|
const CGBlockInfo &blockInfo,
|
|
|
|
llvm::Constant *blockFn);
|
2010-05-21 12:11:14 +08:00
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
/// Build the helper function to copy a block.
|
|
|
|
static llvm::Constant *buildCopyHelper(CodeGenModule &CGM,
|
|
|
|
const CGBlockInfo &blockInfo) {
|
|
|
|
return CodeGenFunction(CGM).GenerateCopyHelperFunction(blockInfo);
|
|
|
|
}
|
|
|
|
|
2013-12-06 00:25:25 +08:00
|
|
|
/// Build the helper function to dispose of a block.
|
2011-02-07 18:33:21 +08:00
|
|
|
static llvm::Constant *buildDisposeHelper(CodeGenModule &CGM,
|
|
|
|
const CGBlockInfo &blockInfo) {
|
|
|
|
return CodeGenFunction(CGM).GenerateDestroyHelperFunction(blockInfo);
|
|
|
|
}
|
|
|
|
|
2012-10-26 02:06:53 +08:00
|
|
|
/// buildBlockDescriptor - Build the block descriptor meta-data for a block.
|
|
|
|
/// buildBlockDescriptor is accessed from 5th field of the Block_literal
|
|
|
|
/// meta-data and contains stationary information about the block literal.
|
|
|
|
/// Its definition will have 4 (or optinally 6) words.
|
2013-05-09 07:09:44 +08:00
|
|
|
/// \code
|
2012-10-26 02:06:53 +08:00
|
|
|
/// struct Block_descriptor {
|
|
|
|
/// unsigned long reserved;
|
|
|
|
/// unsigned long size; // size of Block_literal metadata in bytes.
|
|
|
|
/// void *copy_func_helper_decl; // optional copy helper.
|
|
|
|
/// void *destroy_func_decl; // optioanl destructor helper.
|
2013-05-09 07:09:44 +08:00
|
|
|
/// void *block_method_encoding_address; // @encode for block literal signature.
|
2012-10-26 02:06:53 +08:00
|
|
|
/// void *block_layout_info; // encoding of captured block variables.
|
|
|
|
/// };
|
2013-05-09 07:09:44 +08:00
|
|
|
/// \endcode
|
2011-02-07 18:33:21 +08:00
|
|
|
static llvm::Constant *buildBlockDescriptor(CodeGenModule &CGM,
|
|
|
|
const CGBlockInfo &blockInfo) {
|
|
|
|
ASTContext &C = CGM.getContext();
|
|
|
|
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *ulong = CGM.getTypes().ConvertType(C.UnsignedLongTy);
|
|
|
|
llvm::Type *i8p = CGM.getTypes().ConvertType(C.VoidPtrTy);
|
2011-02-07 18:33:21 +08:00
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<llvm::Constant*, 6> elements;
|
2009-02-14 00:19:19 +08:00
|
|
|
|
|
|
|
// reserved
|
2011-02-07 18:33:21 +08:00
|
|
|
elements.push_back(llvm::ConstantInt::get(ulong, 0));
|
2009-02-14 00:19:19 +08:00
|
|
|
|
|
|
|
// Size
|
2009-02-22 04:07:44 +08:00
|
|
|
// FIXME: What is the right way to say this doesn't fit? We should give
|
|
|
|
// a user diagnostic in that case. Better fix would be to change the
|
|
|
|
// API to size_t.
|
2011-02-07 18:33:21 +08:00
|
|
|
elements.push_back(llvm::ConstantInt::get(ulong,
|
|
|
|
blockInfo.BlockSize.getQuantity()));
|
2009-02-14 00:19:19 +08:00
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
// Optional copy/dispose helpers.
|
|
|
|
if (blockInfo.NeedsCopyDispose) {
|
2009-02-14 00:19:19 +08:00
|
|
|
// copy_func_helper_decl
|
2011-02-07 18:33:21 +08:00
|
|
|
elements.push_back(buildCopyHelper(CGM, blockInfo));
|
2009-02-14 00:19:19 +08:00
|
|
|
|
|
|
|
// destroy_func_decl
|
2011-02-07 18:33:21 +08:00
|
|
|
elements.push_back(buildDisposeHelper(CGM, blockInfo));
|
2009-02-14 00:19:19 +08:00
|
|
|
}
|
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
// Signature. Mandatory ObjC-style method descriptor @encode sequence.
|
|
|
|
std::string typeAtEncoding =
|
|
|
|
CGM.getContext().getObjCEncodingForBlock(blockInfo.getBlockExpr());
|
|
|
|
elements.push_back(llvm::ConstantExpr::getBitCast(
|
|
|
|
CGM.GetAddrOfConstantCString(typeAtEncoding), i8p));
|
2010-02-24 05:51:17 +08:00
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
// GC layout.
|
2012-10-28 05:10:38 +08:00
|
|
|
if (C.getLangOpts().ObjC1) {
|
|
|
|
if (CGM.getLangOpts().getGC() != LangOptions::NonGC)
|
|
|
|
elements.push_back(CGM.getObjCRuntime().BuildGCBlockLayout(CGM, blockInfo));
|
|
|
|
else
|
|
|
|
elements.push_back(CGM.getObjCRuntime().BuildRCBlockLayout(CGM, blockInfo));
|
|
|
|
}
|
2011-02-07 18:33:21 +08:00
|
|
|
else
|
|
|
|
elements.push_back(llvm::Constant::getNullValue(i8p));
|
2010-05-20 09:18:31 +08:00
|
|
|
|
2011-06-20 12:01:35 +08:00
|
|
|
llvm::Constant *init = llvm::ConstantStruct::getAnon(elements);
|
2009-03-01 09:09:12 +08:00
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
llvm::GlobalVariable *global =
|
|
|
|
new llvm::GlobalVariable(CGM.getModule(), init->getType(), true,
|
|
|
|
llvm::GlobalValue::InternalLinkage,
|
|
|
|
init, "__block_descriptor_tmp");
|
2009-10-21 11:49:08 +08:00
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
return llvm::ConstantExpr::getBitCast(global, CGM.getBlockDescriptorType());
|
2009-03-01 09:09:12 +08:00
|
|
|
}
|
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
/*
|
|
|
|
Purely notional variadic template describing the layout of a block.
|
|
|
|
|
|
|
|
template <class _ResultType, class... _ParamTypes, class... _CaptureTypes>
|
|
|
|
struct Block_literal {
|
|
|
|
/// Initialized to one of:
|
|
|
|
/// extern void *_NSConcreteStackBlock[];
|
|
|
|
/// extern void *_NSConcreteGlobalBlock[];
|
|
|
|
///
|
|
|
|
/// In theory, we could start one off malloc'ed by setting
|
|
|
|
/// BLOCK_NEEDS_FREE, giving it a refcount of 1, and using
|
|
|
|
/// this isa:
|
|
|
|
/// extern void *_NSConcreteMallocBlock[];
|
|
|
|
struct objc_class *isa;
|
|
|
|
|
|
|
|
/// These are the flags (with corresponding bit number) that the
|
|
|
|
/// compiler is actually supposed to know about.
|
|
|
|
/// 25. BLOCK_HAS_COPY_DISPOSE - indicates that the block
|
|
|
|
/// descriptor provides copy and dispose helper functions
|
|
|
|
/// 26. BLOCK_HAS_CXX_OBJ - indicates that there's a captured
|
|
|
|
/// object with a nontrivial destructor or copy constructor
|
|
|
|
/// 28. BLOCK_IS_GLOBAL - indicates that the block is allocated
|
|
|
|
/// as global memory
|
|
|
|
/// 29. BLOCK_USE_STRET - indicates that the block function
|
|
|
|
/// uses stret, which objc_msgSend needs to know about
|
|
|
|
/// 30. BLOCK_HAS_SIGNATURE - indicates that the block has an
|
|
|
|
/// @encoded signature string
|
|
|
|
/// And we're not supposed to manipulate these:
|
|
|
|
/// 24. BLOCK_NEEDS_FREE - indicates that the block has been moved
|
|
|
|
/// to malloc'ed memory
|
|
|
|
/// 27. BLOCK_IS_GC - indicates that the block has been moved to
|
|
|
|
/// to GC-allocated memory
|
|
|
|
/// Additionally, the bottom 16 bits are a reference count which
|
|
|
|
/// should be zero on the stack.
|
|
|
|
int flags;
|
|
|
|
|
|
|
|
/// Reserved; should be zero-initialized.
|
|
|
|
int reserved;
|
|
|
|
|
|
|
|
/// Function pointer generated from block literal.
|
|
|
|
_ResultType (*invoke)(Block_literal *, _ParamTypes...);
|
|
|
|
|
|
|
|
/// Block description metadata generated from block literal.
|
|
|
|
struct Block_descriptor *block_descriptor;
|
|
|
|
|
|
|
|
/// Captured values follow.
|
|
|
|
_CapturesTypes captures...;
|
|
|
|
};
|
|
|
|
*/
|
|
|
|
|
|
|
|
/// The number of fields in a block header.
|
|
|
|
const unsigned BlockHeaderSize = 5;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
/// A chunk of data that we actually have to capture in the block.
|
|
|
|
struct BlockLayoutChunk {
|
|
|
|
CharUnits Alignment;
|
|
|
|
CharUnits Size;
|
2013-01-17 08:25:06 +08:00
|
|
|
Qualifiers::ObjCLifetime Lifetime;
|
2011-02-07 18:33:21 +08:00
|
|
|
const BlockDecl::Capture *Capture; // null for 'this'
|
2011-07-11 17:56:20 +08:00
|
|
|
llvm::Type *Type;
|
2011-02-07 18:33:21 +08:00
|
|
|
|
|
|
|
BlockLayoutChunk(CharUnits align, CharUnits size,
|
2013-01-17 08:25:06 +08:00
|
|
|
Qualifiers::ObjCLifetime lifetime,
|
2011-02-07 18:33:21 +08:00
|
|
|
const BlockDecl::Capture *capture,
|
2011-07-11 17:56:20 +08:00
|
|
|
llvm::Type *type)
|
2013-01-17 08:25:06 +08:00
|
|
|
: Alignment(align), Size(size), Lifetime(lifetime),
|
|
|
|
Capture(capture), Type(type) {}
|
2011-02-07 18:33:21 +08:00
|
|
|
|
|
|
|
/// Tell the block info that this chunk has the given field index.
|
|
|
|
void setIndex(CGBlockInfo &info, unsigned index) {
|
|
|
|
if (!Capture)
|
|
|
|
info.CXXThisIndex = index;
|
|
|
|
else
|
|
|
|
info.Captures[Capture->getVariable()]
|
|
|
|
= CGBlockInfo::Capture::makeIndex(index);
|
|
|
|
}
|
|
|
|
};
|
2009-03-01 09:09:12 +08:00
|
|
|
|
2013-01-17 08:25:06 +08:00
|
|
|
/// Order by 1) all __strong together 2) next, all byfref together 3) next,
|
|
|
|
/// all __weak together. Preserve descending alignment in all situations.
|
2011-02-07 18:33:21 +08:00
|
|
|
bool operator<(const BlockLayoutChunk &left, const BlockLayoutChunk &right) {
|
2013-01-17 08:25:06 +08:00
|
|
|
CharUnits LeftValue, RightValue;
|
|
|
|
bool LeftByref = left.Capture ? left.Capture->isByRef() : false;
|
|
|
|
bool RightByref = right.Capture ? right.Capture->isByRef() : false;
|
|
|
|
|
|
|
|
if (left.Lifetime == Qualifiers::OCL_Strong &&
|
|
|
|
left.Alignment >= right.Alignment)
|
|
|
|
LeftValue = CharUnits::fromQuantity(64);
|
|
|
|
else if (LeftByref && left.Alignment >= right.Alignment)
|
|
|
|
LeftValue = CharUnits::fromQuantity(32);
|
|
|
|
else if (left.Lifetime == Qualifiers::OCL_Weak &&
|
|
|
|
left.Alignment >= right.Alignment)
|
|
|
|
LeftValue = CharUnits::fromQuantity(16);
|
|
|
|
else
|
|
|
|
LeftValue = left.Alignment;
|
|
|
|
if (right.Lifetime == Qualifiers::OCL_Strong &&
|
|
|
|
right.Alignment >= left.Alignment)
|
|
|
|
RightValue = CharUnits::fromQuantity(64);
|
|
|
|
else if (RightByref && right.Alignment >= left.Alignment)
|
|
|
|
RightValue = CharUnits::fromQuantity(32);
|
|
|
|
else if (right.Lifetime == Qualifiers::OCL_Weak &&
|
|
|
|
right.Alignment >= left.Alignment)
|
|
|
|
RightValue = CharUnits::fromQuantity(16);
|
|
|
|
else
|
|
|
|
RightValue = right.Alignment;
|
|
|
|
|
|
|
|
return LeftValue > RightValue;
|
2011-02-07 18:33:21 +08:00
|
|
|
}
|
|
|
|
}
|
2009-03-05 16:32:30 +08:00
|
|
|
|
2011-02-08 11:07:00 +08:00
|
|
|
/// Determines if the given type is safe for constant capture in C++.
|
|
|
|
static bool isSafeForCXXConstantCapture(QualType type) {
|
|
|
|
const RecordType *recordType =
|
|
|
|
type->getBaseElementTypeUnsafe()->getAs<RecordType>();
|
|
|
|
|
|
|
|
// Only records can be unsafe.
|
|
|
|
if (!recordType) return true;
|
|
|
|
|
|
|
|
const CXXRecordDecl *record = cast<CXXRecordDecl>(recordType->getDecl());
|
|
|
|
|
|
|
|
// Maintain semantics for classes with non-trivial dtors or copy ctors.
|
|
|
|
if (!record->hasTrivialDestructor()) return false;
|
2012-11-16 08:53:38 +08:00
|
|
|
if (record->hasNonTrivialCopyConstructor()) return false;
|
2011-02-08 11:07:00 +08:00
|
|
|
|
|
|
|
// Otherwise, we just have to make sure there aren't any mutable
|
|
|
|
// fields that might have changed since initialization.
|
2011-05-13 09:05:07 +08:00
|
|
|
return !record->hasMutableFields();
|
2011-02-08 11:07:00 +08:00
|
|
|
}
|
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
/// It is illegal to modify a const object after initialization.
|
|
|
|
/// Therefore, if a const object has a constant initializer, we don't
|
|
|
|
/// actually need to keep storage for it in the block; we'll just
|
|
|
|
/// rematerialize it at the start of the block function. This is
|
|
|
|
/// acceptable because we make no promises about address stability of
|
|
|
|
/// captured variables.
|
|
|
|
static llvm::Constant *tryCaptureAsConstant(CodeGenModule &CGM,
|
2012-01-14 12:30:29 +08:00
|
|
|
CodeGenFunction *CGF,
|
2011-02-07 18:33:21 +08:00
|
|
|
const VarDecl *var) {
|
|
|
|
QualType type = var->getType();
|
|
|
|
|
|
|
|
// We can only do this if the variable is const.
|
|
|
|
if (!type.isConstQualified()) return 0;
|
|
|
|
|
2011-02-08 11:07:00 +08:00
|
|
|
// Furthermore, in C++ we have to worry about mutable fields:
|
|
|
|
// C++ [dcl.type.cv]p4:
|
|
|
|
// Except that any class member declared mutable can be
|
|
|
|
// modified, any attempt to modify a const object during its
|
|
|
|
// lifetime results in undefined behavior.
|
2012-03-11 15:00:24 +08:00
|
|
|
if (CGM.getLangOpts().CPlusPlus && !isSafeForCXXConstantCapture(type))
|
2011-02-07 18:33:21 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
// If the variable doesn't have any initializer (shouldn't this be
|
|
|
|
// invalid?), it's not clear what we should do. Maybe capture as
|
|
|
|
// zero?
|
|
|
|
const Expr *init = var->getInit();
|
|
|
|
if (!init) return 0;
|
|
|
|
|
2012-01-14 12:30:29 +08:00
|
|
|
return CGM.EmitConstantInit(*var, CGF);
|
2011-02-07 18:33:21 +08:00
|
|
|
}
|
2009-11-18 03:33:30 +08:00
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
/// Get the low bit of a nonzero character count. This is the
|
|
|
|
/// alignment of the nth byte if the 0th byte is universally aligned.
|
|
|
|
static CharUnits getLowBit(CharUnits v) {
|
|
|
|
return CharUnits::fromQuantity(v.getQuantity() & (~v.getQuantity() + 1));
|
|
|
|
}
|
2009-11-18 03:33:30 +08:00
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
static void initializeForBlockHeader(CodeGenModule &CGM, CGBlockInfo &info,
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVectorImpl<llvm::Type*> &elementTypes) {
|
2011-02-07 18:33:21 +08:00
|
|
|
ASTContext &C = CGM.getContext();
|
2009-02-14 00:19:19 +08:00
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
// The header is basically a 'struct { void *; int; int; void *; void *; }'.
|
|
|
|
CharUnits ptrSize, ptrAlign, intSize, intAlign;
|
2014-03-02 21:01:17 +08:00
|
|
|
std::tie(ptrSize, ptrAlign) = C.getTypeInfoInChars(C.VoidPtrTy);
|
|
|
|
std::tie(intSize, intAlign) = C.getTypeInfoInChars(C.IntTy);
|
2011-01-05 20:14:39 +08:00
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
// Are there crazy embedded platforms where this isn't true?
|
|
|
|
assert(intSize <= ptrSize && "layout assumptions horribly violated");
|
2009-11-18 03:33:30 +08:00
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
CharUnits headerSize = ptrSize;
|
|
|
|
if (2 * intSize < ptrAlign) headerSize += ptrSize;
|
|
|
|
else headerSize += 2 * intSize;
|
|
|
|
headerSize += 2 * ptrSize;
|
2009-03-05 16:32:30 +08:00
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
info.BlockAlign = ptrAlign;
|
|
|
|
info.BlockSize = headerSize;
|
2009-02-26 07:33:13 +08:00
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
assert(elementTypes.empty());
|
2011-07-11 17:56:20 +08:00
|
|
|
llvm::Type *i8p = CGM.getTypes().ConvertType(C.VoidPtrTy);
|
|
|
|
llvm::Type *intTy = CGM.getTypes().ConvertType(C.IntTy);
|
2011-02-07 18:33:21 +08:00
|
|
|
elementTypes.push_back(i8p);
|
|
|
|
elementTypes.push_back(intTy);
|
|
|
|
elementTypes.push_back(intTy);
|
|
|
|
elementTypes.push_back(i8p);
|
|
|
|
elementTypes.push_back(CGM.getBlockDescriptorType());
|
2009-02-26 07:33:13 +08:00
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
assert(elementTypes.size() == BlockHeaderSize);
|
|
|
|
}
|
2009-02-26 07:33:13 +08:00
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
/// Compute the layout of the given block. Attempts to lay the block
|
|
|
|
/// out with minimal space requirements.
|
2012-01-14 12:30:29 +08:00
|
|
|
static void computeBlockInfo(CodeGenModule &CGM, CodeGenFunction *CGF,
|
|
|
|
CGBlockInfo &info) {
|
2011-02-07 18:33:21 +08:00
|
|
|
ASTContext &C = CGM.getContext();
|
|
|
|
const BlockDecl *block = info.getBlockDecl();
|
2009-03-07 10:35:30 +08:00
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<llvm::Type*, 8> elementTypes;
|
2011-02-07 18:33:21 +08:00
|
|
|
initializeForBlockHeader(CGM, info, elementTypes);
|
2009-03-05 16:32:30 +08:00
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
if (!block->hasCaptures()) {
|
|
|
|
info.StructureType =
|
|
|
|
llvm::StructType::get(CGM.getLLVMContext(), elementTypes, true);
|
|
|
|
info.CanBeGlobal = true;
|
|
|
|
return;
|
|
|
|
}
|
2012-11-02 02:32:55 +08:00
|
|
|
else if (C.getLangOpts().ObjC1 &&
|
|
|
|
CGM.getLangOpts().getGC() == LangOptions::NonGC)
|
|
|
|
info.HasCapturedVariableLayout = true;
|
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
// Collect the layout chunks.
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<BlockLayoutChunk, 16> layout;
|
2011-02-07 18:33:21 +08:00
|
|
|
layout.reserve(block->capturesCXXThis() +
|
|
|
|
(block->capture_end() - block->capture_begin()));
|
2010-05-20 09:18:31 +08:00
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
CharUnits maxFieldAlign;
|
2010-05-20 09:18:31 +08:00
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
// First, 'this'.
|
|
|
|
if (block->capturesCXXThis()) {
|
2013-07-13 06:05:26 +08:00
|
|
|
assert(CGF && CGF->CurFuncDecl && isa<CXXMethodDecl>(CGF->CurFuncDecl) &&
|
|
|
|
"Can't capture 'this' outside a method");
|
|
|
|
QualType thisType = cast<CXXMethodDecl>(CGF->CurFuncDecl)->getThisType(C);
|
2010-05-20 09:18:31 +08:00
|
|
|
|
2011-07-11 17:56:20 +08:00
|
|
|
llvm::Type *llvmType = CGM.getTypes().ConvertType(thisType);
|
2011-02-07 18:33:21 +08:00
|
|
|
std::pair<CharUnits,CharUnits> tinfo
|
|
|
|
= CGM.getContext().getTypeInfoInChars(thisType);
|
|
|
|
maxFieldAlign = std::max(maxFieldAlign, tinfo.second);
|
2010-05-20 09:18:31 +08:00
|
|
|
|
2013-01-17 08:25:06 +08:00
|
|
|
layout.push_back(BlockLayoutChunk(tinfo.second, tinfo.first,
|
|
|
|
Qualifiers::OCL_None,
|
|
|
|
0, llvmType));
|
2011-02-07 18:33:21 +08:00
|
|
|
}
|
2010-05-20 09:18:31 +08:00
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
// Next, all the block captures.
|
|
|
|
for (BlockDecl::capture_const_iterator ci = block->capture_begin(),
|
|
|
|
ce = block->capture_end(); ci != ce; ++ci) {
|
|
|
|
const VarDecl *variable = ci->getVariable();
|
2010-05-20 09:18:31 +08:00
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
if (ci->isByRef()) {
|
|
|
|
// We have to copy/dispose of the __block reference.
|
|
|
|
info.NeedsCopyDispose = true;
|
|
|
|
|
|
|
|
// Just use void* instead of a pointer to the byref type.
|
|
|
|
QualType byRefPtrTy = C.VoidPtrTy;
|
|
|
|
|
2011-07-11 17:56:20 +08:00
|
|
|
llvm::Type *llvmType = CGM.getTypes().ConvertType(byRefPtrTy);
|
2011-02-07 18:33:21 +08:00
|
|
|
std::pair<CharUnits,CharUnits> tinfo
|
|
|
|
= CGM.getContext().getTypeInfoInChars(byRefPtrTy);
|
|
|
|
maxFieldAlign = std::max(maxFieldAlign, tinfo.second);
|
|
|
|
|
|
|
|
layout.push_back(BlockLayoutChunk(tinfo.second, tinfo.first,
|
2013-01-17 08:25:06 +08:00
|
|
|
Qualifiers::OCL_None,
|
2011-02-07 18:33:21 +08:00
|
|
|
&*ci, llvmType));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, build a layout chunk with the size and alignment of
|
|
|
|
// the declaration.
|
2012-01-14 12:30:29 +08:00
|
|
|
if (llvm::Constant *constant = tryCaptureAsConstant(CGM, CGF, variable)) {
|
2011-02-07 18:33:21 +08:00
|
|
|
info.Captures[variable] = CGBlockInfo::Capture::makeConstant(constant);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-06-16 07:02:42 +08:00
|
|
|
// If we have a lifetime qualifier, honor it for capture purposes.
|
|
|
|
// That includes *not* copying it if it's __unsafe_unretained.
|
2013-01-17 08:25:06 +08:00
|
|
|
Qualifiers::ObjCLifetime lifetime =
|
|
|
|
variable->getType().getObjCLifetime();
|
|
|
|
if (lifetime) {
|
2011-06-16 07:02:42 +08:00
|
|
|
switch (lifetime) {
|
|
|
|
case Qualifiers::OCL_None: llvm_unreachable("impossible");
|
|
|
|
case Qualifiers::OCL_ExplicitNone:
|
|
|
|
case Qualifiers::OCL_Autoreleasing:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Qualifiers::OCL_Strong:
|
|
|
|
case Qualifiers::OCL_Weak:
|
|
|
|
info.NeedsCopyDispose = true;
|
|
|
|
}
|
2011-02-07 18:33:21 +08:00
|
|
|
|
2011-06-16 07:02:42 +08:00
|
|
|
// Block pointers require copy/dispose. So do Objective-C pointers.
|
|
|
|
} else if (variable->getType()->isObjCRetainableType()) {
|
2011-02-07 18:33:21 +08:00
|
|
|
info.NeedsCopyDispose = true;
|
2013-01-17 08:25:06 +08:00
|
|
|
// used for mrr below.
|
|
|
|
lifetime = Qualifiers::OCL_Strong;
|
2011-02-07 18:33:21 +08:00
|
|
|
|
|
|
|
// So do types that require non-trivial copy construction.
|
|
|
|
} else if (ci->hasCopyExpr()) {
|
|
|
|
info.NeedsCopyDispose = true;
|
|
|
|
info.HasCXXObject = true;
|
|
|
|
|
|
|
|
// And so do types with destructors.
|
2012-03-11 15:00:24 +08:00
|
|
|
} else if (CGM.getLangOpts().CPlusPlus) {
|
2011-02-07 18:33:21 +08:00
|
|
|
if (const CXXRecordDecl *record =
|
|
|
|
variable->getType()->getAsCXXRecordDecl()) {
|
|
|
|
if (!record->hasTrivialDestructor()) {
|
|
|
|
info.HasCXXObject = true;
|
|
|
|
info.NeedsCopyDispose = true;
|
2010-05-20 09:18:31 +08:00
|
|
|
}
|
2010-11-18 14:31:45 +08:00
|
|
|
}
|
2011-02-07 18:33:21 +08:00
|
|
|
}
|
|
|
|
|
2011-11-03 06:53:43 +08:00
|
|
|
QualType VT = variable->getType();
|
2011-11-01 07:44:33 +08:00
|
|
|
CharUnits size = C.getTypeSizeInChars(VT);
|
2011-11-03 06:53:43 +08:00
|
|
|
CharUnits align = C.getDeclAlign(variable);
|
2011-11-01 07:44:33 +08:00
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
maxFieldAlign = std::max(maxFieldAlign, align);
|
|
|
|
|
2011-07-11 17:56:20 +08:00
|
|
|
llvm::Type *llvmType =
|
2011-11-01 07:44:33 +08:00
|
|
|
CGM.getTypes().ConvertTypeForMem(VT);
|
|
|
|
|
2013-01-17 08:25:06 +08:00
|
|
|
layout.push_back(BlockLayoutChunk(align, size, lifetime, &*ci, llvmType));
|
2011-02-07 18:33:21 +08:00
|
|
|
}
|
2010-05-20 09:18:31 +08:00
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
// If that was everything, we're done here.
|
|
|
|
if (layout.empty()) {
|
|
|
|
info.StructureType =
|
|
|
|
llvm::StructType::get(CGM.getLLVMContext(), elementTypes, true);
|
|
|
|
info.CanBeGlobal = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort the layout by alignment. We have to use a stable sort here
|
|
|
|
// to get reproducible results. There should probably be an
|
|
|
|
// llvm::array_pod_stable_sort.
|
|
|
|
std::stable_sort(layout.begin(), layout.end());
|
2012-12-05 01:20:57 +08:00
|
|
|
|
|
|
|
// Needed for blocks layout info.
|
|
|
|
info.BlockHeaderForcedGapOffset = info.BlockSize;
|
|
|
|
info.BlockHeaderForcedGapSize = CharUnits::Zero();
|
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
CharUnits &blockSize = info.BlockSize;
|
|
|
|
info.BlockAlign = std::max(maxFieldAlign, info.BlockAlign);
|
|
|
|
|
|
|
|
// Assuming that the first byte in the header is maximally aligned,
|
|
|
|
// get the alignment of the first byte following the header.
|
|
|
|
CharUnits endAlign = getLowBit(blockSize);
|
|
|
|
|
|
|
|
// If the end of the header isn't satisfactorily aligned for the
|
|
|
|
// maximum thing, look for things that are okay with the header-end
|
|
|
|
// alignment, and keep appending them until we get something that's
|
|
|
|
// aligned right. This algorithm is only guaranteed optimal if
|
|
|
|
// that condition is satisfied at some point; otherwise we can get
|
|
|
|
// things like:
|
|
|
|
// header // next byte has alignment 4
|
|
|
|
// something_with_size_5; // next byte has alignment 1
|
|
|
|
// something_with_alignment_8;
|
|
|
|
// which has 7 bytes of padding, as opposed to the naive solution
|
|
|
|
// which might have less (?).
|
|
|
|
if (endAlign < maxFieldAlign) {
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVectorImpl<BlockLayoutChunk>::iterator
|
2011-02-07 18:33:21 +08:00
|
|
|
li = layout.begin() + 1, le = layout.end();
|
|
|
|
|
|
|
|
// Look for something that the header end is already
|
|
|
|
// satisfactorily aligned for.
|
|
|
|
for (; li != le && endAlign < li->Alignment; ++li)
|
|
|
|
;
|
|
|
|
|
|
|
|
// If we found something that's naturally aligned for the end of
|
|
|
|
// the header, keep adding things...
|
|
|
|
if (li != le) {
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVectorImpl<BlockLayoutChunk>::iterator first = li;
|
2011-02-07 18:33:21 +08:00
|
|
|
for (; li != le; ++li) {
|
|
|
|
assert(endAlign >= li->Alignment);
|
|
|
|
|
|
|
|
li->setIndex(info, elementTypes.size());
|
|
|
|
elementTypes.push_back(li->Type);
|
|
|
|
blockSize += li->Size;
|
|
|
|
endAlign = getLowBit(blockSize);
|
|
|
|
|
|
|
|
// ...until we get to the alignment of the maximum field.
|
2012-12-05 01:20:57 +08:00
|
|
|
if (endAlign >= maxFieldAlign) {
|
|
|
|
if (li == first) {
|
|
|
|
// No user field was appended. So, a gap was added.
|
|
|
|
// Save total gap size for use in block layout bit map.
|
|
|
|
info.BlockHeaderForcedGapSize = li->Size;
|
|
|
|
}
|
2011-02-07 18:33:21 +08:00
|
|
|
break;
|
2012-12-05 01:20:57 +08:00
|
|
|
}
|
2009-02-26 07:33:13 +08:00
|
|
|
}
|
2011-02-07 18:33:21 +08:00
|
|
|
// Don't re-append everything we just appended.
|
|
|
|
layout.erase(first, li);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-27 05:14:42 +08:00
|
|
|
assert(endAlign == getLowBit(blockSize));
|
2012-12-05 01:20:57 +08:00
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
// At this point, we just have to add padding if the end align still
|
|
|
|
// isn't aligned right.
|
|
|
|
if (endAlign < maxFieldAlign) {
|
2012-04-27 05:14:42 +08:00
|
|
|
CharUnits newBlockSize = blockSize.RoundUpToAlignment(maxFieldAlign);
|
|
|
|
CharUnits padding = newBlockSize - blockSize;
|
2011-02-07 18:33:21 +08:00
|
|
|
|
2011-02-15 17:22:45 +08:00
|
|
|
elementTypes.push_back(llvm::ArrayType::get(CGM.Int8Ty,
|
|
|
|
padding.getQuantity()));
|
2012-04-27 05:14:42 +08:00
|
|
|
blockSize = newBlockSize;
|
2012-05-02 04:28:00 +08:00
|
|
|
endAlign = getLowBit(blockSize); // might be > maxFieldAlign
|
2011-02-07 18:33:21 +08:00
|
|
|
}
|
|
|
|
|
2012-05-02 04:28:00 +08:00
|
|
|
assert(endAlign >= maxFieldAlign);
|
2012-04-27 05:14:42 +08:00
|
|
|
assert(endAlign == getLowBit(blockSize));
|
2011-02-07 18:33:21 +08:00
|
|
|
// Slam everything else on now. This works because they have
|
|
|
|
// strictly decreasing alignment and we expect that size is always a
|
|
|
|
// multiple of alignment.
|
2011-07-23 18:55:15 +08:00
|
|
|
for (SmallVectorImpl<BlockLayoutChunk>::iterator
|
2011-02-07 18:33:21 +08:00
|
|
|
li = layout.begin(), le = layout.end(); li != le; ++li) {
|
|
|
|
assert(endAlign >= li->Alignment);
|
|
|
|
li->setIndex(info, elementTypes.size());
|
|
|
|
elementTypes.push_back(li->Type);
|
|
|
|
blockSize += li->Size;
|
|
|
|
endAlign = getLowBit(blockSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
info.StructureType =
|
|
|
|
llvm::StructType::get(CGM.getLLVMContext(), elementTypes, true);
|
|
|
|
}
|
|
|
|
|
2011-11-10 16:15:53 +08:00
|
|
|
/// Enter the scope of a block. This should be run at the entrance to
|
|
|
|
/// a full-expression so that the block's cleanups are pushed at the
|
|
|
|
/// right place in the stack.
|
|
|
|
static void enterBlockScope(CodeGenFunction &CGF, BlockDecl *block) {
|
2012-04-14 02:44:05 +08:00
|
|
|
assert(CGF.HaveInsertPoint());
|
|
|
|
|
2011-11-10 16:15:53 +08:00
|
|
|
// Allocate the block info and place it at the head of the list.
|
|
|
|
CGBlockInfo &blockInfo =
|
|
|
|
*new CGBlockInfo(block, CGF.CurFn->getName());
|
|
|
|
blockInfo.NextBlockInfo = CGF.FirstBlockInfo;
|
|
|
|
CGF.FirstBlockInfo = &blockInfo;
|
|
|
|
|
|
|
|
// Compute information about the layout, etc., of this block,
|
|
|
|
// pushing cleanups as necessary.
|
2012-01-14 12:30:29 +08:00
|
|
|
computeBlockInfo(CGF.CGM, &CGF, blockInfo);
|
2011-11-10 16:15:53 +08:00
|
|
|
|
|
|
|
// Nothing else to do if it can be global.
|
|
|
|
if (blockInfo.CanBeGlobal) return;
|
|
|
|
|
|
|
|
// Make the allocation for the block.
|
|
|
|
blockInfo.Address =
|
|
|
|
CGF.CreateTempAlloca(blockInfo.StructureType, "block");
|
|
|
|
blockInfo.Address->setAlignment(blockInfo.BlockAlign.getQuantity());
|
|
|
|
|
|
|
|
// If there are cleanups to emit, enter them (but inactive).
|
|
|
|
if (!blockInfo.NeedsCopyDispose) return;
|
|
|
|
|
|
|
|
// Walk through the captures (in order) and find the ones not
|
|
|
|
// captured by constant.
|
|
|
|
for (BlockDecl::capture_const_iterator ci = block->capture_begin(),
|
|
|
|
ce = block->capture_end(); ci != ce; ++ci) {
|
|
|
|
// Ignore __block captures; there's nothing special in the
|
|
|
|
// on-stack block that we need to do for them.
|
|
|
|
if (ci->isByRef()) continue;
|
|
|
|
|
|
|
|
// Ignore variables that are constant-captured.
|
|
|
|
const VarDecl *variable = ci->getVariable();
|
|
|
|
CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
|
|
|
|
if (capture.isConstant()) continue;
|
|
|
|
|
|
|
|
// Ignore objects that aren't destructed.
|
|
|
|
QualType::DestructionKind dtorKind =
|
|
|
|
variable->getType().isDestructedType();
|
|
|
|
if (dtorKind == QualType::DK_none) continue;
|
|
|
|
|
|
|
|
CodeGenFunction::Destroyer *destroyer;
|
|
|
|
|
|
|
|
// Block captures count as local values and have imprecise semantics.
|
|
|
|
// They also can't be arrays, so need to worry about that.
|
|
|
|
if (dtorKind == QualType::DK_objc_strong_lifetime) {
|
2012-01-26 11:33:36 +08:00
|
|
|
destroyer = CodeGenFunction::destroyARCStrongImprecise;
|
2011-11-10 16:15:53 +08:00
|
|
|
} else {
|
2012-01-26 11:33:36 +08:00
|
|
|
destroyer = CGF.getDestroyer(dtorKind);
|
2011-11-10 16:15:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// GEP down to the address.
|
|
|
|
llvm::Value *addr = CGF.Builder.CreateStructGEP(blockInfo.Address,
|
|
|
|
capture.getIndex());
|
|
|
|
|
2011-11-10 18:43:54 +08:00
|
|
|
// We can use that GEP as the dominating IP.
|
|
|
|
if (!blockInfo.DominatingIP)
|
|
|
|
blockInfo.DominatingIP = cast<llvm::Instruction>(addr);
|
|
|
|
|
2011-11-10 16:15:53 +08:00
|
|
|
CleanupKind cleanupKind = InactiveNormalCleanup;
|
|
|
|
bool useArrayEHCleanup = CGF.needsEHCleanup(dtorKind);
|
|
|
|
if (useArrayEHCleanup)
|
|
|
|
cleanupKind = InactiveNormalAndEHCleanup;
|
|
|
|
|
|
|
|
CGF.pushDestroy(cleanupKind, addr, variable->getType(),
|
2012-01-26 11:33:36 +08:00
|
|
|
destroyer, useArrayEHCleanup);
|
2011-11-10 16:15:53 +08:00
|
|
|
|
|
|
|
// Remember where that cleanup was.
|
|
|
|
capture.setCleanup(CGF.EHStack.stable_begin());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Enter a full-expression with a non-trivial number of objects to
|
|
|
|
/// clean up. This is in this file because, at the moment, the only
|
|
|
|
/// kind of cleanup object is a BlockDecl*.
|
|
|
|
void CodeGenFunction::enterNonTrivialFullExpression(const ExprWithCleanups *E) {
|
|
|
|
assert(E->getNumObjects() != 0);
|
|
|
|
ArrayRef<ExprWithCleanups::CleanupObject> cleanups = E->getObjects();
|
|
|
|
for (ArrayRef<ExprWithCleanups::CleanupObject>::iterator
|
|
|
|
i = cleanups.begin(), e = cleanups.end(); i != e; ++i) {
|
|
|
|
enterBlockScope(*this, *i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Find the layout for the given block in a linked list and remove it.
|
|
|
|
static CGBlockInfo *findAndRemoveBlockInfo(CGBlockInfo **head,
|
|
|
|
const BlockDecl *block) {
|
|
|
|
while (true) {
|
|
|
|
assert(head && *head);
|
|
|
|
CGBlockInfo *cur = *head;
|
|
|
|
|
|
|
|
// If this is the block we're looking for, splice it out of the list.
|
|
|
|
if (cur->getBlockDecl() == block) {
|
|
|
|
*head = cur->NextBlockInfo;
|
|
|
|
return cur;
|
|
|
|
}
|
|
|
|
|
|
|
|
head = &cur->NextBlockInfo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Destroy a chain of block layouts.
|
|
|
|
void CodeGenFunction::destroyBlockInfos(CGBlockInfo *head) {
|
|
|
|
assert(head && "destroying an empty chain");
|
|
|
|
do {
|
|
|
|
CGBlockInfo *cur = head;
|
|
|
|
head = cur->NextBlockInfo;
|
|
|
|
delete cur;
|
|
|
|
} while (head != 0);
|
|
|
|
}
|
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
/// Emit a block literal expression in the current function.
|
|
|
|
llvm::Value *CodeGenFunction::EmitBlockLiteral(const BlockExpr *blockExpr) {
|
2011-11-10 16:15:53 +08:00
|
|
|
// If the block has no captures, we won't have a pre-computed
|
|
|
|
// layout for it.
|
|
|
|
if (!blockExpr->getBlockDecl()->hasCaptures()) {
|
|
|
|
CGBlockInfo blockInfo(blockExpr->getBlockDecl(), CurFn->getName());
|
2012-01-14 12:30:29 +08:00
|
|
|
computeBlockInfo(CGM, this, blockInfo);
|
2011-11-10 16:15:53 +08:00
|
|
|
blockInfo.BlockExpression = blockExpr;
|
|
|
|
return EmitBlockLiteral(blockInfo);
|
|
|
|
}
|
2011-02-07 18:33:21 +08:00
|
|
|
|
2011-11-10 16:15:53 +08:00
|
|
|
// Find the block info for this block and take ownership of it.
|
2012-02-05 10:12:40 +08:00
|
|
|
OwningPtr<CGBlockInfo> blockInfo;
|
2011-11-10 16:15:53 +08:00
|
|
|
blockInfo.reset(findAndRemoveBlockInfo(&FirstBlockInfo,
|
|
|
|
blockExpr->getBlockDecl()));
|
2011-02-07 18:33:21 +08:00
|
|
|
|
2011-11-10 16:15:53 +08:00
|
|
|
blockInfo->BlockExpression = blockExpr;
|
|
|
|
return EmitBlockLiteral(*blockInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Value *CodeGenFunction::EmitBlockLiteral(const CGBlockInfo &blockInfo) {
|
|
|
|
// Using the computed layout, generate the actual block function.
|
2012-03-01 12:01:32 +08:00
|
|
|
bool isLambdaConv = blockInfo.getBlockDecl()->isConversionFromLambda();
|
2011-02-07 18:33:21 +08:00
|
|
|
llvm::Constant *blockFn
|
2012-06-27 00:06:38 +08:00
|
|
|
= CodeGenFunction(CGM, true).GenerateBlockFunction(CurGD, blockInfo,
|
2013-05-03 15:33:41 +08:00
|
|
|
LocalDeclMap,
|
|
|
|
isLambdaConv);
|
2011-02-15 17:22:45 +08:00
|
|
|
blockFn = llvm::ConstantExpr::getBitCast(blockFn, VoidPtrTy);
|
2011-02-07 18:33:21 +08:00
|
|
|
|
|
|
|
// If there is nothing to capture, we can emit this as a global block.
|
|
|
|
if (blockInfo.CanBeGlobal)
|
|
|
|
return buildGlobalBlock(CGM, blockInfo, blockFn);
|
|
|
|
|
|
|
|
// Otherwise, we have to emit this as a local block.
|
|
|
|
|
|
|
|
llvm::Constant *isa = CGM.getNSConcreteStackBlock();
|
2011-02-15 17:22:45 +08:00
|
|
|
isa = llvm::ConstantExpr::getBitCast(isa, VoidPtrTy);
|
2011-02-07 18:33:21 +08:00
|
|
|
|
|
|
|
// Build the block descriptor.
|
|
|
|
llvm::Constant *descriptor = buildBlockDescriptor(CGM, blockInfo);
|
|
|
|
|
2011-11-10 16:15:53 +08:00
|
|
|
llvm::AllocaInst *blockAddr = blockInfo.Address;
|
|
|
|
assert(blockAddr && "block has no address!");
|
2011-02-07 18:33:21 +08:00
|
|
|
|
|
|
|
// Compute the initial on-stack block flags.
|
2011-02-08 16:22:06 +08:00
|
|
|
BlockFlags flags = BLOCK_HAS_SIGNATURE;
|
2012-11-02 02:32:55 +08:00
|
|
|
if (blockInfo.HasCapturedVariableLayout) flags |= BLOCK_HAS_EXTENDED_LAYOUT;
|
2011-02-07 18:33:21 +08:00
|
|
|
if (blockInfo.NeedsCopyDispose) flags |= BLOCK_HAS_COPY_DISPOSE;
|
|
|
|
if (blockInfo.HasCXXObject) flags |= BLOCK_HAS_CXX_OBJ;
|
2011-03-09 16:39:33 +08:00
|
|
|
if (blockInfo.UsesStret) flags |= BLOCK_USE_STRET;
|
2011-02-07 18:33:21 +08:00
|
|
|
|
|
|
|
// Initialize the block literal.
|
|
|
|
Builder.CreateStore(isa, Builder.CreateStructGEP(blockAddr, 0, "block.isa"));
|
2011-11-10 16:15:53 +08:00
|
|
|
Builder.CreateStore(llvm::ConstantInt::get(IntTy, flags.getBitMask()),
|
2011-02-07 18:33:21 +08:00
|
|
|
Builder.CreateStructGEP(blockAddr, 1, "block.flags"));
|
2011-11-10 16:15:53 +08:00
|
|
|
Builder.CreateStore(llvm::ConstantInt::get(IntTy, 0),
|
2011-02-07 18:33:21 +08:00
|
|
|
Builder.CreateStructGEP(blockAddr, 2, "block.reserved"));
|
|
|
|
Builder.CreateStore(blockFn, Builder.CreateStructGEP(blockAddr, 3,
|
|
|
|
"block.invoke"));
|
|
|
|
Builder.CreateStore(descriptor, Builder.CreateStructGEP(blockAddr, 4,
|
|
|
|
"block.descriptor"));
|
|
|
|
|
|
|
|
// Finally, capture all the values into the block.
|
|
|
|
const BlockDecl *blockDecl = blockInfo.getBlockDecl();
|
|
|
|
|
|
|
|
// First, 'this'.
|
|
|
|
if (blockDecl->capturesCXXThis()) {
|
|
|
|
llvm::Value *addr = Builder.CreateStructGEP(blockAddr,
|
|
|
|
blockInfo.CXXThisIndex,
|
|
|
|
"block.captured-this.addr");
|
|
|
|
Builder.CreateStore(LoadCXXThis(), addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next, captured variables.
|
|
|
|
for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
|
|
|
|
ce = blockDecl->capture_end(); ci != ce; ++ci) {
|
|
|
|
const VarDecl *variable = ci->getVariable();
|
|
|
|
const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
|
|
|
|
|
|
|
|
// Ignore constant captures.
|
|
|
|
if (capture.isConstant()) continue;
|
|
|
|
|
|
|
|
QualType type = variable->getType();
|
2013-04-09 07:27:49 +08:00
|
|
|
CharUnits align = getContext().getDeclAlign(variable);
|
2011-02-07 18:33:21 +08:00
|
|
|
|
|
|
|
// This will be a [[type]]*, except that a byref entry will just be
|
|
|
|
// an i8**.
|
|
|
|
llvm::Value *blockField =
|
|
|
|
Builder.CreateStructGEP(blockAddr, capture.getIndex(),
|
|
|
|
"block.captured");
|
|
|
|
|
|
|
|
// Compute the address of the thing we're going to move into the
|
|
|
|
// block literal.
|
|
|
|
llvm::Value *src;
|
2012-05-17 00:50:20 +08:00
|
|
|
if (BlockInfo && ci->isNested()) {
|
2011-02-07 18:33:21 +08:00
|
|
|
// We need to use the capture from the enclosing block.
|
|
|
|
const CGBlockInfo::Capture &enclosingCapture =
|
|
|
|
BlockInfo->getCapture(variable);
|
|
|
|
|
|
|
|
// This is a [[type]]*, except that a byref entry wil just be an i8**.
|
|
|
|
src = Builder.CreateStructGEP(LoadBlockStruct(),
|
|
|
|
enclosingCapture.getIndex(),
|
|
|
|
"block.capture.addr");
|
2012-03-01 12:01:32 +08:00
|
|
|
} else if (blockDecl->isConversionFromLambda()) {
|
2012-02-25 10:48:22 +08:00
|
|
|
// The lambda capture in a lambda's conversion-to-block-pointer is
|
2012-03-01 12:01:32 +08:00
|
|
|
// special; we'll simply emit it directly.
|
|
|
|
src = 0;
|
2011-02-07 18:33:21 +08:00
|
|
|
} else {
|
2013-03-04 14:32:36 +08:00
|
|
|
// Just look it up in the locals map, which will give us back a
|
|
|
|
// [[type]]*. If that doesn't work, do the more elaborate DRE
|
|
|
|
// emission.
|
|
|
|
src = LocalDeclMap.lookup(variable);
|
|
|
|
if (!src) {
|
|
|
|
DeclRefExpr declRef(const_cast<VarDecl*>(variable),
|
|
|
|
/*refersToEnclosing*/ ci->isNested(), type,
|
|
|
|
VK_LValue, SourceLocation());
|
|
|
|
src = EmitDeclRefLValue(&declRef).getAddress();
|
|
|
|
}
|
2011-02-07 18:33:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// For byrefs, we just write the pointer to the byref struct into
|
|
|
|
// the block field. There's no need to chase the forwarding
|
|
|
|
// pointer at this point, since we're building something that will
|
|
|
|
// live a shorter life than the stack byref anyway.
|
|
|
|
if (ci->isByRef()) {
|
2011-02-15 17:22:45 +08:00
|
|
|
// Get a void* that points to the byref struct.
|
2011-02-07 18:33:21 +08:00
|
|
|
if (ci->isNested())
|
2013-04-09 07:27:49 +08:00
|
|
|
src = Builder.CreateAlignedLoad(src, align.getQuantity(),
|
|
|
|
"byref.capture");
|
2010-05-20 09:18:31 +08:00
|
|
|
else
|
2011-02-15 17:22:45 +08:00
|
|
|
src = Builder.CreateBitCast(src, VoidPtrTy);
|
2011-02-07 18:33:21 +08:00
|
|
|
|
2011-02-15 17:22:45 +08:00
|
|
|
// Write that void* into the capture field.
|
2013-04-09 07:27:49 +08:00
|
|
|
Builder.CreateAlignedStore(src, blockField, align.getQuantity());
|
2011-02-07 18:33:21 +08:00
|
|
|
|
|
|
|
// If we have a copy constructor, evaluate that into the block field.
|
|
|
|
} else if (const Expr *copyExpr = ci->getCopyExpr()) {
|
2012-03-01 12:01:32 +08:00
|
|
|
if (blockDecl->isConversionFromLambda()) {
|
|
|
|
// If we have a lambda conversion, emit the expression
|
|
|
|
// directly into the block instead.
|
|
|
|
AggValueSlot Slot =
|
2013-04-09 07:27:49 +08:00
|
|
|
AggValueSlot::forAddr(blockField, align, Qualifiers(),
|
2012-03-01 12:01:32 +08:00
|
|
|
AggValueSlot::IsDestructed,
|
|
|
|
AggValueSlot::DoesNotNeedGCBarriers,
|
2012-03-30 01:37:10 +08:00
|
|
|
AggValueSlot::IsNotAliased);
|
2012-03-01 12:01:32 +08:00
|
|
|
EmitAggExpr(copyExpr, Slot);
|
|
|
|
} else {
|
|
|
|
EmitSynthesizedCXXCopyCtor(blockField, src, copyExpr);
|
|
|
|
}
|
2011-02-07 18:33:21 +08:00
|
|
|
|
|
|
|
// If it's a reference variable, copy the reference into the block field.
|
2011-11-03 06:53:43 +08:00
|
|
|
} else if (type->isReferenceType()) {
|
2013-04-09 07:27:49 +08:00
|
|
|
llvm::Value *ref =
|
|
|
|
Builder.CreateAlignedLoad(src, align.getQuantity(), "ref.val");
|
|
|
|
Builder.CreateAlignedStore(ref, blockField, align.getQuantity());
|
|
|
|
|
|
|
|
// If this is an ARC __strong block-pointer variable, don't do a
|
|
|
|
// block copy.
|
|
|
|
//
|
|
|
|
// TODO: this can be generalized into the normal initialization logic:
|
|
|
|
// we should never need to do a block-copy when initializing a local
|
|
|
|
// variable, because the local variable's lifetime should be strictly
|
|
|
|
// contained within the stack block's.
|
|
|
|
} else if (type.getObjCLifetime() == Qualifiers::OCL_Strong &&
|
|
|
|
type->isBlockPointerType()) {
|
|
|
|
// Load the block and do a simple retain.
|
|
|
|
LValue srcLV = MakeAddrLValue(src, type, align);
|
2013-10-02 10:29:49 +08:00
|
|
|
llvm::Value *value = EmitLoadOfScalar(srcLV, SourceLocation());
|
2013-04-09 07:27:49 +08:00
|
|
|
value = EmitARCRetainNonBlock(value);
|
|
|
|
|
|
|
|
// Do a primitive store to the block field.
|
|
|
|
LValue destLV = MakeAddrLValue(blockField, type, align);
|
|
|
|
EmitStoreOfScalar(value, destLV, /*init*/ true);
|
2011-02-07 18:33:21 +08:00
|
|
|
|
|
|
|
// Otherwise, fake up a POD copy into the block field.
|
|
|
|
} else {
|
2011-06-16 07:02:42 +08:00
|
|
|
// Fake up a new variable so that EmitScalarInit doesn't think
|
|
|
|
// we're referring to the variable in its own initializer.
|
|
|
|
ImplicitParamDecl blockFieldPseudoVar(/*DC*/ 0, SourceLocation(),
|
2011-11-03 06:53:43 +08:00
|
|
|
/*name*/ 0, type);
|
2011-06-16 07:02:42 +08:00
|
|
|
|
2011-02-08 02:37:40 +08:00
|
|
|
// We use one of these or the other depending on whether the
|
|
|
|
// reference is nested.
|
2012-03-10 17:33:50 +08:00
|
|
|
DeclRefExpr declRef(const_cast<VarDecl*>(variable),
|
|
|
|
/*refersToEnclosing*/ ci->isNested(), type,
|
|
|
|
VK_LValue, SourceLocation());
|
2011-02-08 02:37:40 +08:00
|
|
|
|
2011-11-03 06:53:43 +08:00
|
|
|
ImplicitCastExpr l2r(ImplicitCastExpr::OnStack, type, CK_LValueToRValue,
|
2012-03-10 17:33:50 +08:00
|
|
|
&declRef, VK_RValue);
|
2011-06-16 12:16:24 +08:00
|
|
|
EmitExprAsInit(&l2r, &blockFieldPseudoVar,
|
2013-04-09 07:27:49 +08:00
|
|
|
MakeAddrLValue(blockField, type, align),
|
2011-03-08 17:38:48 +08:00
|
|
|
/*captured by init*/ false);
|
2010-05-20 09:18:31 +08:00
|
|
|
}
|
2011-02-07 18:33:21 +08:00
|
|
|
|
2011-11-10 16:15:53 +08:00
|
|
|
// Activate the cleanup if layout pushed one.
|
2011-06-16 07:02:42 +08:00
|
|
|
if (!ci->isByRef()) {
|
2011-11-10 16:15:53 +08:00
|
|
|
EHScopeStack::stable_iterator cleanup = capture.getCleanup();
|
|
|
|
if (cleanup.isValid())
|
2011-11-10 18:43:54 +08:00
|
|
|
ActivateCleanupBlock(cleanup, blockInfo.DominatingIP);
|
2011-06-16 07:02:42 +08:00
|
|
|
}
|
2009-02-14 00:19:19 +08:00
|
|
|
}
|
2009-03-05 16:32:30 +08:00
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
// Cast to the converted block-pointer type, which happens (somewhat
|
|
|
|
// unfortunately) to be a pointer to function type.
|
|
|
|
llvm::Value *result =
|
|
|
|
Builder.CreateBitCast(blockAddr,
|
|
|
|
ConvertType(blockInfo.getBlockExpr()->getType()));
|
2011-01-05 20:14:39 +08:00
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
return result;
|
2009-02-14 00:19:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-10 01:41:47 +08:00
|
|
|
llvm::Type *CodeGenModule::getBlockDescriptorType() {
|
2009-02-13 23:16:56 +08:00
|
|
|
if (BlockDescriptorType)
|
|
|
|
return BlockDescriptorType;
|
|
|
|
|
2011-07-10 01:41:47 +08:00
|
|
|
llvm::Type *UnsignedLongTy =
|
2009-02-13 23:16:56 +08:00
|
|
|
getTypes().ConvertType(getContext().UnsignedLongTy);
|
2009-02-13 23:32:32 +08:00
|
|
|
|
2009-02-13 23:16:56 +08:00
|
|
|
// struct __block_descriptor {
|
|
|
|
// unsigned long reserved;
|
|
|
|
// unsigned long block_size;
|
2010-02-24 05:51:17 +08:00
|
|
|
//
|
|
|
|
// // later, the following will be added
|
|
|
|
//
|
|
|
|
// struct {
|
|
|
|
// void (*copyHelper)();
|
|
|
|
// void (*copyHelper)();
|
|
|
|
// } helpers; // !!! optional
|
|
|
|
//
|
|
|
|
// const char *signature; // the block signature
|
|
|
|
// const char *layout; // reserved
|
2009-02-13 23:16:56 +08:00
|
|
|
// };
|
2011-06-19 06:49:11 +08:00
|
|
|
BlockDescriptorType =
|
2011-08-13 01:43:31 +08:00
|
|
|
llvm::StructType::create("struct.__block_descriptor",
|
|
|
|
UnsignedLongTy, UnsignedLongTy, NULL);
|
2009-02-13 23:16:56 +08:00
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
// Now form a pointer to that.
|
|
|
|
BlockDescriptorType = llvm::PointerType::getUnqual(BlockDescriptorType);
|
2009-02-13 23:16:56 +08:00
|
|
|
return BlockDescriptorType;
|
2009-02-12 08:39:25 +08:00
|
|
|
}
|
|
|
|
|
2011-07-10 01:41:47 +08:00
|
|
|
llvm::Type *CodeGenModule::getGenericBlockLiteralType() {
|
2009-02-13 23:25:34 +08:00
|
|
|
if (GenericBlockLiteralType)
|
|
|
|
return GenericBlockLiteralType;
|
|
|
|
|
2011-07-10 01:41:47 +08:00
|
|
|
llvm::Type *BlockDescPtrTy = getBlockDescriptorType();
|
2009-02-13 23:32:32 +08:00
|
|
|
|
2009-02-13 23:25:34 +08:00
|
|
|
// struct __block_literal_generic {
|
2009-02-19 09:01:04 +08:00
|
|
|
// void *__isa;
|
|
|
|
// int __flags;
|
|
|
|
// int __reserved;
|
|
|
|
// void (*__invoke)(void *);
|
|
|
|
// struct __block_descriptor *__descriptor;
|
2009-02-13 23:25:34 +08:00
|
|
|
// };
|
2011-07-10 01:41:47 +08:00
|
|
|
GenericBlockLiteralType =
|
2011-08-13 01:43:31 +08:00
|
|
|
llvm::StructType::create("struct.__block_literal_generic",
|
|
|
|
VoidPtrTy, IntTy, IntTy, VoidPtrTy,
|
|
|
|
BlockDescPtrTy, NULL);
|
2009-02-13 23:32:32 +08:00
|
|
|
|
2009-02-13 23:25:34 +08:00
|
|
|
return GenericBlockLiteralType;
|
2009-02-12 08:39:25 +08:00
|
|
|
}
|
|
|
|
|
2009-02-19 09:01:04 +08:00
|
|
|
|
2013-10-02 10:29:49 +08:00
|
|
|
RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr *E,
|
2009-12-25 05:13:40 +08:00
|
|
|
ReturnValueSlot ReturnValue) {
|
2009-02-13 23:32:32 +08:00
|
|
|
const BlockPointerType *BPT =
|
2009-07-30 05:53:49 +08:00
|
|
|
E->getCallee()->getType()->getAs<BlockPointerType>();
|
2009-02-13 23:32:32 +08:00
|
|
|
|
2009-02-12 08:39:25 +08:00
|
|
|
llvm::Value *Callee = EmitScalarExpr(E->getCallee());
|
|
|
|
|
|
|
|
// Get a pointer to the generic block literal.
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *BlockLiteralTy =
|
2009-07-30 06:16:19 +08:00
|
|
|
llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType());
|
2009-02-12 08:39:25 +08:00
|
|
|
|
|
|
|
// Bitcast the callee to a block literal.
|
2009-02-13 23:32:32 +08:00
|
|
|
llvm::Value *BlockLiteral =
|
2009-02-12 08:39:25 +08:00
|
|
|
Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal");
|
|
|
|
|
|
|
|
// Get the function pointer from the literal.
|
2011-09-28 05:06:10 +08:00
|
|
|
llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3);
|
2009-02-12 08:39:25 +08:00
|
|
|
|
2011-09-28 05:06:10 +08:00
|
|
|
BlockLiteral = Builder.CreateBitCast(BlockLiteral, VoidPtrTy);
|
2009-02-13 23:32:32 +08:00
|
|
|
|
2009-02-12 08:39:25 +08:00
|
|
|
// Add the block literal.
|
|
|
|
CallArgList Args;
|
2011-05-15 09:53:33 +08:00
|
|
|
Args.add(RValue::get(BlockLiteral), getContext().VoidPtrTy);
|
2009-02-13 23:32:32 +08:00
|
|
|
|
2009-04-09 07:13:16 +08:00
|
|
|
QualType FnType = BPT->getPointeeType();
|
|
|
|
|
2009-02-12 08:39:25 +08:00
|
|
|
// And the rest of the arguments.
|
2009-09-22 07:43:11 +08:00
|
|
|
EmitCallArgs(Args, FnType->getAs<FunctionProtoType>(),
|
2009-04-09 07:13:16 +08:00
|
|
|
E->arg_begin(), E->arg_end());
|
2009-02-13 23:32:32 +08:00
|
|
|
|
2009-04-08 06:10:22 +08:00
|
|
|
// Load the function.
|
2011-09-28 05:06:10 +08:00
|
|
|
llvm::Value *Func = Builder.CreateLoad(FuncPtr);
|
2009-04-08 06:10:22 +08:00
|
|
|
|
2011-03-09 16:39:33 +08:00
|
|
|
const FunctionType *FuncTy = FnType->castAs<FunctionType>();
|
2012-02-17 11:33:10 +08:00
|
|
|
const CGFunctionInfo &FnInfo =
|
Fix the required args count for variadic blocks.
We were emitting calls to blocks as if all arguments were
required --- i.e. with signature (A,B,C,D,...) rather than
(A,B,...). This patch fixes that and accounts for the
implicit block-context argument as a required argument.
In addition, this patch changes the function type under which
we call unprototyped functions on platforms like x86-64 that
guarantee compatibility of variadic functions with unprototyped
function types; previously we would always call such functions
under the LLVM type T (...)*, but now we will call them under
the type T (A,B,C,D,...)*. This last change should have no
material effect except for making the type conventions more
explicit; it was a side-effect of the most convenient implementation.
llvm-svn: 169588
2012-12-07 15:03:17 +08:00
|
|
|
CGM.getTypes().arrangeBlockFunctionCall(Args, FuncTy);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-08 06:10:22 +08:00
|
|
|
// Cast the function pointer to the right type.
|
2012-02-17 11:33:10 +08:00
|
|
|
llvm::Type *BlockFTy = CGM.getTypes().GetFunctionType(FnInfo);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy);
|
2009-04-08 06:10:22 +08:00
|
|
|
Func = Builder.CreateBitCast(Func, BlockFTyPtr);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-12 08:39:25 +08:00
|
|
|
// And call the block.
|
2009-12-25 05:13:40 +08:00
|
|
|
return EmitCall(FnInfo, Func, ReturnValue, Args);
|
2009-02-12 08:39:25 +08:00
|
|
|
}
|
2009-02-13 01:55:02 +08:00
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
llvm::Value *CodeGenFunction::GetAddrOfBlockDecl(const VarDecl *variable,
|
|
|
|
bool isByRef) {
|
|
|
|
assert(BlockInfo && "evaluating block ref without block information?");
|
|
|
|
const CGBlockInfo::Capture &capture = BlockInfo->getCapture(variable);
|
2010-05-20 09:18:31 +08:00
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
// Handle constant captures.
|
|
|
|
if (capture.isConstant()) return LocalDeclMap[variable];
|
2010-05-20 09:18:31 +08:00
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
llvm::Value *addr =
|
|
|
|
Builder.CreateStructGEP(LoadBlockStruct(), capture.getIndex(),
|
|
|
|
"block.capture.addr");
|
2010-05-20 09:18:31 +08:00
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
if (isByRef) {
|
|
|
|
// addr should be a void** right now. Load, then cast the result
|
|
|
|
// to byref*.
|
2009-03-04 11:23:46 +08:00
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
addr = Builder.CreateLoad(addr);
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::PointerType *byrefPointerType
|
2011-02-07 18:33:21 +08:00
|
|
|
= llvm::PointerType::get(BuildByRefType(variable), 0);
|
|
|
|
addr = Builder.CreateBitCast(addr, byrefPointerType,
|
|
|
|
"byref.addr");
|
2009-10-21 11:49:08 +08:00
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
// Follow the forwarding pointer.
|
|
|
|
addr = Builder.CreateStructGEP(addr, 1, "byref.forwarding");
|
|
|
|
addr = Builder.CreateLoad(addr, "byref.addr.forwarded");
|
2010-05-20 09:18:31 +08:00
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
// Cast back to byref* and GEP over to the actual object.
|
|
|
|
addr = Builder.CreateBitCast(addr, byrefPointerType);
|
|
|
|
addr = Builder.CreateStructGEP(addr, getByRefValueLLVMField(variable),
|
|
|
|
variable->getNameAsString());
|
2010-05-20 09:18:31 +08:00
|
|
|
}
|
2009-10-21 11:49:08 +08:00
|
|
|
|
2011-11-03 06:53:43 +08:00
|
|
|
if (variable->getType()->isReferenceType())
|
2011-02-07 18:33:21 +08:00
|
|
|
addr = Builder.CreateLoad(addr, "ref.tmp");
|
2009-10-21 11:49:08 +08:00
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
return addr;
|
2009-03-04 11:23:46 +08:00
|
|
|
}
|
|
|
|
|
2009-02-15 06:16:35 +08:00
|
|
|
llvm::Constant *
|
2011-02-08 16:22:06 +08:00
|
|
|
CodeGenModule::GetAddrOfGlobalBlock(const BlockExpr *blockExpr,
|
2011-02-15 17:22:45 +08:00
|
|
|
const char *name) {
|
2011-11-10 16:15:53 +08:00
|
|
|
CGBlockInfo blockInfo(blockExpr->getBlockDecl(), name);
|
|
|
|
blockInfo.BlockExpression = blockExpr;
|
2009-02-13 23:32:32 +08:00
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
// Compute information about the layout, etc., of this block.
|
2012-01-14 12:30:29 +08:00
|
|
|
computeBlockInfo(*this, 0, blockInfo);
|
2010-02-24 05:51:17 +08:00
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
// Using that metadata, generate the actual block function.
|
|
|
|
llvm::Constant *blockFn;
|
|
|
|
{
|
|
|
|
llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap;
|
2011-02-08 16:22:06 +08:00
|
|
|
blockFn = CodeGenFunction(*this).GenerateBlockFunction(GlobalDecl(),
|
|
|
|
blockInfo,
|
2013-05-03 15:33:41 +08:00
|
|
|
LocalDeclMap,
|
2012-02-25 10:48:22 +08:00
|
|
|
false);
|
2011-02-07 18:33:21 +08:00
|
|
|
}
|
2011-02-15 17:22:45 +08:00
|
|
|
blockFn = llvm::ConstantExpr::getBitCast(blockFn, VoidPtrTy);
|
2009-02-13 23:32:32 +08:00
|
|
|
|
2011-02-08 16:22:06 +08:00
|
|
|
return buildGlobalBlock(*this, blockInfo, blockFn);
|
2011-02-07 18:33:21 +08:00
|
|
|
}
|
2009-11-18 03:33:30 +08:00
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
static llvm::Constant *buildGlobalBlock(CodeGenModule &CGM,
|
|
|
|
const CGBlockInfo &blockInfo,
|
|
|
|
llvm::Constant *blockFn) {
|
|
|
|
assert(blockInfo.CanBeGlobal);
|
2009-02-13 23:32:32 +08:00
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
// Generate the constants for the block literal initializer.
|
|
|
|
llvm::Constant *fields[BlockHeaderSize];
|
2009-02-13 23:32:32 +08:00
|
|
|
|
2009-02-13 01:55:02 +08:00
|
|
|
// isa
|
2011-02-07 18:33:21 +08:00
|
|
|
fields[0] = CGM.getNSConcreteGlobalBlock();
|
2009-02-13 23:32:32 +08:00
|
|
|
|
2010-07-29 03:07:18 +08:00
|
|
|
// __flags
|
2011-03-09 16:39:33 +08:00
|
|
|
BlockFlags flags = BLOCK_IS_GLOBAL | BLOCK_HAS_SIGNATURE;
|
|
|
|
if (blockInfo.UsesStret) flags |= BLOCK_USE_STRET;
|
|
|
|
|
2011-02-15 17:22:45 +08:00
|
|
|
fields[1] = llvm::ConstantInt::get(CGM.IntTy, flags.getBitMask());
|
2009-02-13 23:32:32 +08:00
|
|
|
|
2009-02-13 01:55:02 +08:00
|
|
|
// Reserved
|
2011-02-15 17:22:45 +08:00
|
|
|
fields[2] = llvm::Constant::getNullValue(CGM.IntTy);
|
2009-02-13 23:32:32 +08:00
|
|
|
|
2009-02-13 01:55:02 +08:00
|
|
|
// Function
|
2011-02-07 18:33:21 +08:00
|
|
|
fields[3] = blockFn;
|
2009-02-13 23:32:32 +08:00
|
|
|
|
2009-02-13 01:55:02 +08:00
|
|
|
// Descriptor
|
2011-02-07 18:33:21 +08:00
|
|
|
fields[4] = buildBlockDescriptor(CGM, blockInfo);
|
2009-02-13 23:32:32 +08:00
|
|
|
|
2011-06-20 12:01:35 +08:00
|
|
|
llvm::Constant *init = llvm::ConstantStruct::getAnon(fields);
|
2009-02-13 01:55:02 +08:00
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
llvm::GlobalVariable *literal =
|
|
|
|
new llvm::GlobalVariable(CGM.getModule(),
|
|
|
|
init->getType(),
|
|
|
|
/*constant*/ true,
|
|
|
|
llvm::GlobalVariable::InternalLinkage,
|
|
|
|
init,
|
|
|
|
"__block_literal_global");
|
|
|
|
literal->setAlignment(blockInfo.BlockAlign.getQuantity());
|
|
|
|
|
|
|
|
// Return a constant of the appropriately-casted type.
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *requiredType =
|
2011-02-07 18:33:21 +08:00
|
|
|
CGM.getTypes().ConvertType(blockInfo.getBlockExpr()->getType());
|
|
|
|
return llvm::ConstantExpr::getBitCast(literal, requiredType);
|
2009-02-22 04:00:35 +08:00
|
|
|
}
|
|
|
|
|
2009-03-05 16:32:30 +08:00
|
|
|
llvm::Function *
|
2011-02-07 18:33:21 +08:00
|
|
|
CodeGenFunction::GenerateBlockFunction(GlobalDecl GD,
|
|
|
|
const CGBlockInfo &blockInfo,
|
2012-02-25 10:48:22 +08:00
|
|
|
const DeclMapTy &ldm,
|
|
|
|
bool IsLambdaConversionToBlock) {
|
2011-02-07 18:33:21 +08:00
|
|
|
const BlockDecl *blockDecl = blockInfo.getBlockDecl();
|
2009-04-16 05:51:44 +08:00
|
|
|
|
2012-06-27 00:06:38 +08:00
|
|
|
CurGD = GD;
|
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
BlockInfo = &blockInfo;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-14 07:34:28 +08:00
|
|
|
// Arrange for local static and local extern declarations to appear
|
2011-02-07 18:33:21 +08:00
|
|
|
// to be local to this function as well, in case they're directly
|
|
|
|
// referenced in a block.
|
|
|
|
for (DeclMapTy::const_iterator i = ldm.begin(), e = ldm.end(); i != e; ++i) {
|
|
|
|
const VarDecl *var = dyn_cast<VarDecl>(i->first);
|
|
|
|
if (var && !var->hasLocalStorage())
|
|
|
|
LocalDeclMap[var] = i->second;
|
2009-03-14 07:34:28 +08:00
|
|
|
}
|
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
// Begin building the function declaration.
|
2009-02-13 23:32:32 +08:00
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
// Build the argument list.
|
|
|
|
FunctionArgList args;
|
2009-02-13 01:55:02 +08:00
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
// The first argument is the block pointer. Just take it as a void*
|
|
|
|
// and cast it later.
|
|
|
|
QualType selfTy = getContext().VoidPtrTy;
|
2009-10-21 11:49:08 +08:00
|
|
|
IdentifierInfo *II = &CGM.getContext().Idents.get(".block_descriptor");
|
|
|
|
|
2011-02-23 06:38:33 +08:00
|
|
|
ImplicitParamDecl selfDecl(const_cast<BlockDecl*>(blockDecl),
|
|
|
|
SourceLocation(), II, selfTy);
|
2011-03-09 12:27:21 +08:00
|
|
|
args.push_back(&selfDecl);
|
2011-02-07 18:33:21 +08:00
|
|
|
|
|
|
|
// Now add the rest of the parameters.
|
|
|
|
for (BlockDecl::param_const_iterator i = blockDecl->param_begin(),
|
|
|
|
e = blockDecl->param_end(); i != e; ++i)
|
2011-03-09 12:27:21 +08:00
|
|
|
args.push_back(*i);
|
2011-02-07 18:33:21 +08:00
|
|
|
|
|
|
|
// Create the function declaration.
|
2012-02-17 11:33:10 +08:00
|
|
|
const FunctionProtoType *fnType = blockInfo.getBlockExpr()->getFunctionType();
|
2014-02-01 06:54:50 +08:00
|
|
|
const CGFunctionInfo &fnInfo = CGM.getTypes().arrangeFreeFunctionDeclaration(
|
2014-01-26 00:55:45 +08:00
|
|
|
fnType->getReturnType(), args, fnType->getExtInfo(),
|
|
|
|
fnType->isVariadic());
|
2011-03-09 16:39:33 +08:00
|
|
|
if (CGM.ReturnTypeUsesSRet(fnInfo))
|
|
|
|
blockInfo.UsesStret = true;
|
|
|
|
|
2012-02-17 11:33:10 +08:00
|
|
|
llvm::FunctionType *fnLLVMType = CGM.getTypes().GetFunctionType(fnInfo);
|
2011-02-07 18:33:21 +08:00
|
|
|
|
|
|
|
MangleBuffer name;
|
|
|
|
CGM.getBlockMangledName(GD, name, blockDecl);
|
|
|
|
llvm::Function *fn =
|
|
|
|
llvm::Function::Create(fnLLVMType, llvm::GlobalValue::InternalLinkage,
|
|
|
|
name.getString(), &CGM.getModule());
|
|
|
|
CGM.SetInternalFunctionAttributes(blockDecl, fn, fnInfo);
|
|
|
|
|
|
|
|
// Begin generating the function.
|
2014-01-26 00:55:45 +08:00
|
|
|
StartFunction(blockDecl, fnType->getReturnType(), fn, fnInfo, args,
|
2011-03-26 05:26:13 +08:00
|
|
|
blockInfo.getBlockExpr()->getBody()->getLocStart());
|
2011-02-07 18:33:21 +08:00
|
|
|
|
2011-02-23 06:38:33 +08:00
|
|
|
// Okay. Undo some of what StartFunction did.
|
|
|
|
|
|
|
|
// Pull the 'self' reference out of the local decl map.
|
|
|
|
llvm::Value *blockAddr = LocalDeclMap[&selfDecl];
|
|
|
|
LocalDeclMap.erase(&selfDecl);
|
2011-02-07 18:33:21 +08:00
|
|
|
BlockPointer = Builder.CreateBitCast(blockAddr,
|
|
|
|
blockInfo.StructureType->getPointerTo(),
|
|
|
|
"block");
|
2013-03-30 03:20:35 +08:00
|
|
|
// At -O0 we generate an explicit alloca for the BlockPointer, so the RA
|
|
|
|
// won't delete the dbg.declare intrinsics for captured variables.
|
|
|
|
llvm::Value *BlockPointerDbgLoc = BlockPointer;
|
|
|
|
if (CGM.getCodeGenOpts().OptimizationLevel == 0) {
|
|
|
|
// Allocate a stack slot for it, so we can point the debugger to it
|
|
|
|
llvm::AllocaInst *Alloca = CreateTempAlloca(BlockPointer->getType(),
|
|
|
|
"block.addr");
|
|
|
|
unsigned Align = getContext().getDeclAlign(&selfDecl).getQuantity();
|
|
|
|
Alloca->setAlignment(Align);
|
2013-04-02 09:00:48 +08:00
|
|
|
// Set the DebugLocation to empty, so the store is recognized as a
|
|
|
|
// frame setup instruction by llvm::DwarfDebug::beginFunction().
|
2013-07-18 08:28:02 +08:00
|
|
|
NoLocation NL(*this, Builder);
|
2013-03-30 03:20:35 +08:00
|
|
|
Builder.CreateAlignedStore(BlockPointer, Alloca, Align);
|
|
|
|
BlockPointerDbgLoc = Alloca;
|
|
|
|
}
|
2009-02-13 23:32:32 +08:00
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
// If we have a C++ 'this' reference, go ahead and force it into
|
|
|
|
// existence now.
|
|
|
|
if (blockDecl->capturesCXXThis()) {
|
|
|
|
llvm::Value *addr = Builder.CreateStructGEP(BlockPointer,
|
|
|
|
blockInfo.CXXThisIndex,
|
|
|
|
"block.captured-this");
|
|
|
|
CXXThisValue = Builder.CreateLoad(addr, "this");
|
|
|
|
}
|
2009-02-13 23:32:32 +08:00
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
// Also force all the constant captures.
|
|
|
|
for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
|
|
|
|
ce = blockDecl->capture_end(); ci != ce; ++ci) {
|
|
|
|
const VarDecl *variable = ci->getVariable();
|
|
|
|
const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
|
|
|
|
if (!capture.isConstant()) continue;
|
2009-02-13 01:55:02 +08:00
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
unsigned align = getContext().getDeclAlign(variable).getQuantity();
|
2009-02-13 23:32:32 +08:00
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
llvm::AllocaInst *alloca =
|
|
|
|
CreateMemTemp(variable->getType(), "block.captured-const");
|
|
|
|
alloca->setAlignment(align);
|
2009-02-13 23:32:32 +08:00
|
|
|
|
2013-03-15 01:53:33 +08:00
|
|
|
Builder.CreateAlignedStore(capture.getConstant(), alloca, align);
|
2010-05-20 09:18:31 +08:00
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
LocalDeclMap[variable] = alloca;
|
2010-05-21 12:11:14 +08:00
|
|
|
}
|
|
|
|
|
2012-03-10 17:33:50 +08:00
|
|
|
// Save a spot to insert the debug information for all the DeclRefExprs.
|
2009-09-30 10:43:10 +08:00
|
|
|
llvm::BasicBlock *entry = Builder.GetInsertBlock();
|
2009-10-02 06:29:41 +08:00
|
|
|
llvm::BasicBlock::iterator entry_ptr = Builder.GetInsertPoint();
|
|
|
|
--entry_ptr;
|
2009-09-30 10:43:10 +08:00
|
|
|
|
2012-02-25 10:48:22 +08:00
|
|
|
if (IsLambdaConversionToBlock)
|
|
|
|
EmitLambdaBlockInvokeBody();
|
|
|
|
else
|
|
|
|
EmitStmt(blockDecl->getBody());
|
2009-10-02 06:29:41 +08:00
|
|
|
|
2009-10-01 08:27:30 +08:00
|
|
|
// Remember where we were...
|
|
|
|
llvm::BasicBlock *resume = Builder.GetInsertBlock();
|
2009-10-02 06:29:41 +08:00
|
|
|
|
2009-10-01 08:27:30 +08:00
|
|
|
// Go back to the entry.
|
2009-10-02 06:29:41 +08:00
|
|
|
++entry_ptr;
|
|
|
|
Builder.SetInsertPoint(entry, entry_ptr);
|
|
|
|
|
2012-03-10 17:33:50 +08:00
|
|
|
// Emit debug information for all the DeclRefExprs.
|
2011-02-07 18:33:21 +08:00
|
|
|
// FIXME: also for 'this'
|
2009-09-30 10:43:10 +08:00
|
|
|
if (CGDebugInfo *DI = getDebugInfo()) {
|
2011-02-07 18:33:21 +08:00
|
|
|
for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
|
|
|
|
ce = blockDecl->capture_end(); ci != ce; ++ci) {
|
|
|
|
const VarDecl *variable = ci->getVariable();
|
2011-10-14 05:45:18 +08:00
|
|
|
DI->EmitLocation(Builder, variable->getLocation());
|
2011-02-07 18:33:21 +08:00
|
|
|
|
2012-10-24 04:05:01 +08:00
|
|
|
if (CGM.getCodeGenOpts().getDebugInfo()
|
|
|
|
>= CodeGenOptions::LimitedDebugInfo) {
|
2012-05-04 15:39:27 +08:00
|
|
|
const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
|
|
|
|
if (capture.isConstant()) {
|
|
|
|
DI->EmitDeclareOfAutoVariable(variable, LocalDeclMap[variable],
|
|
|
|
Builder);
|
|
|
|
continue;
|
|
|
|
}
|
2011-02-07 18:33:21 +08:00
|
|
|
|
2013-03-30 03:20:35 +08:00
|
|
|
DI->EmitDeclareOfBlockDeclRefVariable(variable, BlockPointerDbgLoc,
|
2012-05-04 15:39:27 +08:00
|
|
|
Builder, blockInfo);
|
|
|
|
}
|
2009-09-30 10:43:10 +08:00
|
|
|
}
|
2013-01-05 02:51:35 +08:00
|
|
|
// Recover location if it was changed in the above loop.
|
|
|
|
DI->EmitLocation(Builder,
|
2013-04-09 04:52:12 +08:00
|
|
|
cast<CompoundStmt>(blockDecl->getBody())->getRBracLoc());
|
2009-09-30 10:43:10 +08:00
|
|
|
}
|
2011-02-07 18:33:21 +08:00
|
|
|
|
2009-10-01 08:27:30 +08:00
|
|
|
// And resume where we left off.
|
|
|
|
if (resume == 0)
|
|
|
|
Builder.ClearInsertionPoint();
|
|
|
|
else
|
|
|
|
Builder.SetInsertPoint(resume);
|
2009-09-30 10:43:10 +08:00
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
FinishFunction(cast<CompoundStmt>(blockDecl->getBody())->getRBracLoc());
|
2009-02-13 01:55:02 +08:00
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
return fn;
|
2009-02-13 01:55:02 +08:00
|
|
|
}
|
2009-02-28 17:07:16 +08:00
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
/*
|
|
|
|
notes.push_back(HelperInfo());
|
|
|
|
HelperInfo ¬e = notes.back();
|
|
|
|
note.index = capture.getIndex();
|
|
|
|
note.RequiresCopying = (ci->hasCopyExpr() || BlockRequiresCopying(type));
|
|
|
|
note.cxxbar_import = ci->getCopyExpr();
|
|
|
|
|
|
|
|
if (ci->isByRef()) {
|
|
|
|
note.flag = BLOCK_FIELD_IS_BYREF;
|
|
|
|
if (type.isObjCGCWeak())
|
|
|
|
note.flag |= BLOCK_FIELD_IS_WEAK;
|
|
|
|
} else if (type->isBlockPointerType()) {
|
|
|
|
note.flag = BLOCK_FIELD_IS_BLOCK;
|
|
|
|
} else {
|
|
|
|
note.flag = BLOCK_FIELD_IS_OBJECT;
|
|
|
|
}
|
|
|
|
*/
|
2009-02-28 17:07:16 +08:00
|
|
|
|
2009-03-04 11:23:46 +08:00
|
|
|
|
2013-01-22 11:56:22 +08:00
|
|
|
/// Generate the copy-helper function for a block closure object:
|
|
|
|
/// static void block_copy_helper(block_t *dst, block_t *src);
|
|
|
|
/// The runtime will have previously initialized 'dst' by doing a
|
|
|
|
/// bit-copy of 'src'.
|
|
|
|
///
|
|
|
|
/// Note that this copies an entire block closure object to the heap;
|
|
|
|
/// it should not be confused with a 'byref copy helper', which moves
|
|
|
|
/// the contents of an individual __block variable to the heap.
|
2011-02-07 18:33:21 +08:00
|
|
|
llvm::Constant *
|
2011-02-08 16:22:06 +08:00
|
|
|
CodeGenFunction::GenerateCopyHelperFunction(const CGBlockInfo &blockInfo) {
|
2011-02-07 18:33:21 +08:00
|
|
|
ASTContext &C = getContext();
|
|
|
|
|
|
|
|
FunctionArgList args;
|
2011-03-09 12:27:21 +08:00
|
|
|
ImplicitParamDecl dstDecl(0, SourceLocation(), 0, C.VoidPtrTy);
|
|
|
|
args.push_back(&dstDecl);
|
|
|
|
ImplicitParamDecl srcDecl(0, SourceLocation(), 0, C.VoidPtrTy);
|
|
|
|
args.push_back(&srcDecl);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2014-02-01 06:54:50 +08:00
|
|
|
const CGFunctionInfo &FI = CGM.getTypes().arrangeFreeFunctionDeclaration(
|
|
|
|
C.VoidTy, args, FunctionType::ExtInfo(), /*variadic=*/false);
|
2009-03-06 09:33:24 +08:00
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
// FIXME: it would be nice if these were mergeable with things with
|
|
|
|
// identical semantics.
|
2012-02-17 11:33:10 +08:00
|
|
|
llvm::FunctionType *LTy = CGM.getTypes().GetFunctionType(FI);
|
2009-03-06 09:33:24 +08:00
|
|
|
|
|
|
|
llvm::Function *Fn =
|
|
|
|
llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
|
2010-01-22 21:59:13 +08:00
|
|
|
"__copy_helper_block_", &CGM.getModule());
|
2009-03-06 09:33:24 +08:00
|
|
|
|
|
|
|
IdentifierInfo *II
|
|
|
|
= &CGM.getContext().Idents.get("__copy_helper_block_");
|
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
FunctionDecl *FD = FunctionDecl::Create(C,
|
|
|
|
C.getTranslationUnitDecl(),
|
2011-03-08 16:55:46 +08:00
|
|
|
SourceLocation(),
|
2011-02-07 18:33:21 +08:00
|
|
|
SourceLocation(), II, C.VoidTy, 0,
|
2010-08-26 11:08:43 +08:00
|
|
|
SC_Static,
|
2010-04-20 06:54:31 +08:00
|
|
|
false,
|
2012-04-12 08:35:04 +08:00
|
|
|
false);
|
2013-07-25 04:34:39 +08:00
|
|
|
// Create a scope with an artificial location for the body of this function.
|
2013-07-18 09:36:04 +08:00
|
|
|
ArtificialLocation AL(*this, Builder);
|
2013-07-25 04:34:39 +08:00
|
|
|
StartFunction(FD, C.VoidTy, Fn, FI, args, SourceLocation());
|
|
|
|
AL.Emit();
|
2009-03-07 10:35:30 +08:00
|
|
|
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *structPtrTy = blockInfo.StructureType->getPointerTo();
|
2011-02-07 18:33:21 +08:00
|
|
|
|
2011-03-09 12:27:21 +08:00
|
|
|
llvm::Value *src = GetAddrOfLocalVar(&srcDecl);
|
2011-02-08 16:22:06 +08:00
|
|
|
src = Builder.CreateLoad(src);
|
|
|
|
src = Builder.CreateBitCast(src, structPtrTy, "block.source");
|
2011-02-07 18:33:21 +08:00
|
|
|
|
2011-03-09 12:27:21 +08:00
|
|
|
llvm::Value *dst = GetAddrOfLocalVar(&dstDecl);
|
2011-02-08 16:22:06 +08:00
|
|
|
dst = Builder.CreateLoad(dst);
|
|
|
|
dst = Builder.CreateBitCast(dst, structPtrTy, "block.dest");
|
2011-02-07 18:33:21 +08:00
|
|
|
|
|
|
|
const BlockDecl *blockDecl = blockInfo.getBlockDecl();
|
|
|
|
|
|
|
|
for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
|
|
|
|
ce = blockDecl->capture_end(); ci != ce; ++ci) {
|
|
|
|
const VarDecl *variable = ci->getVariable();
|
|
|
|
QualType type = variable->getType();
|
|
|
|
|
|
|
|
const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
|
|
|
|
if (capture.isConstant()) continue;
|
|
|
|
|
|
|
|
const Expr *copyExpr = ci->getCopyExpr();
|
2011-06-16 07:02:42 +08:00
|
|
|
BlockFieldFlags flags;
|
|
|
|
|
2012-10-17 10:28:37 +08:00
|
|
|
bool useARCWeakCopy = false;
|
|
|
|
bool useARCStrongCopy = false;
|
2011-02-07 18:33:21 +08:00
|
|
|
|
|
|
|
if (copyExpr) {
|
|
|
|
assert(!ci->isByRef());
|
|
|
|
// don't bother computing flags
|
2011-06-16 07:02:42 +08:00
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
} else if (ci->isByRef()) {
|
|
|
|
flags = BLOCK_FIELD_IS_BYREF;
|
2011-06-16 07:02:42 +08:00
|
|
|
if (type.isObjCGCWeak())
|
|
|
|
flags |= BLOCK_FIELD_IS_WEAK;
|
|
|
|
|
|
|
|
} else if (type->isObjCRetainableType()) {
|
2011-02-07 18:33:21 +08:00
|
|
|
flags = BLOCK_FIELD_IS_OBJECT;
|
2012-10-17 10:28:37 +08:00
|
|
|
bool isBlockPointer = type->isBlockPointerType();
|
|
|
|
if (isBlockPointer)
|
2011-06-16 07:02:42 +08:00
|
|
|
flags = BLOCK_FIELD_IS_BLOCK;
|
2011-02-07 18:33:21 +08:00
|
|
|
|
2011-06-16 07:02:42 +08:00
|
|
|
// Special rules for ARC captures:
|
2012-03-11 15:00:24 +08:00
|
|
|
if (getLangOpts().ObjCAutoRefCount) {
|
2011-06-16 07:02:42 +08:00
|
|
|
Qualifiers qs = type.getQualifiers();
|
|
|
|
|
2012-10-17 10:28:37 +08:00
|
|
|
// We need to register __weak direct captures with the runtime.
|
|
|
|
if (qs.getObjCLifetime() == Qualifiers::OCL_Weak) {
|
|
|
|
useARCWeakCopy = true;
|
|
|
|
|
|
|
|
// We need to retain the copied value for __strong direct captures.
|
|
|
|
} else if (qs.getObjCLifetime() == Qualifiers::OCL_Strong) {
|
|
|
|
// If it's a block pointer, we have to copy the block and
|
|
|
|
// assign that to the destination pointer, so we might as
|
|
|
|
// well use _Block_object_assign. Otherwise we can avoid that.
|
|
|
|
if (!isBlockPointer)
|
|
|
|
useARCStrongCopy = true;
|
|
|
|
|
|
|
|
// Otherwise the memcpy is fine.
|
|
|
|
} else {
|
2011-06-16 07:02:42 +08:00
|
|
|
continue;
|
2012-10-17 10:28:37 +08:00
|
|
|
}
|
2011-06-16 07:02:42 +08:00
|
|
|
|
2012-10-17 10:28:37 +08:00
|
|
|
// Non-ARC captures of retainable pointers are strong and
|
|
|
|
// therefore require a call to _Block_object_assign.
|
|
|
|
} else {
|
|
|
|
// fall through
|
2011-06-16 07:02:42 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
2011-02-07 18:33:21 +08:00
|
|
|
|
|
|
|
unsigned index = capture.getIndex();
|
2011-02-08 16:22:06 +08:00
|
|
|
llvm::Value *srcField = Builder.CreateStructGEP(src, index);
|
|
|
|
llvm::Value *dstField = Builder.CreateStructGEP(dst, index);
|
2011-02-07 18:33:21 +08:00
|
|
|
|
|
|
|
// If there's an explicit copy expression, we do that.
|
|
|
|
if (copyExpr) {
|
2011-02-08 16:22:06 +08:00
|
|
|
EmitSynthesizedCXXCopyCtor(dstField, srcField, copyExpr);
|
2012-10-17 10:28:37 +08:00
|
|
|
} else if (useARCWeakCopy) {
|
2011-06-16 07:02:42 +08:00
|
|
|
EmitARCCopyWeak(dstField, srcField);
|
2011-02-07 18:33:21 +08:00
|
|
|
} else {
|
|
|
|
llvm::Value *srcValue = Builder.CreateLoad(srcField, "blockcopy.src");
|
2012-10-17 10:28:37 +08:00
|
|
|
if (useARCStrongCopy) {
|
|
|
|
// At -O0, store null into the destination field (so that the
|
|
|
|
// storeStrong doesn't over-release) and then call storeStrong.
|
|
|
|
// This is a workaround to not having an initStrong call.
|
|
|
|
if (CGM.getCodeGenOpts().OptimizationLevel == 0) {
|
|
|
|
llvm::PointerType *ty = cast<llvm::PointerType>(srcValue->getType());
|
|
|
|
llvm::Value *null = llvm::ConstantPointerNull::get(ty);
|
|
|
|
Builder.CreateStore(null, dstField);
|
|
|
|
EmitARCStoreStrongCall(dstField, srcValue, true);
|
|
|
|
|
|
|
|
// With optimization enabled, take advantage of the fact that
|
|
|
|
// the blocks runtime guarantees a memcpy of the block data, and
|
|
|
|
// just emit a retain of the src field.
|
|
|
|
} else {
|
|
|
|
EmitARCRetainNonBlock(srcValue);
|
|
|
|
|
|
|
|
// We don't need this anymore, so kill it. It's not quite
|
|
|
|
// worth the annoyance to avoid creating it in the first place.
|
|
|
|
cast<llvm::Instruction>(dstField)->eraseFromParent();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
srcValue = Builder.CreateBitCast(srcValue, VoidPtrTy);
|
|
|
|
llvm::Value *dstAddr = Builder.CreateBitCast(dstField, VoidPtrTy);
|
2013-03-01 03:01:20 +08:00
|
|
|
llvm::Value *args[] = {
|
|
|
|
dstAddr, srcValue, llvm::ConstantInt::get(Int32Ty, flags.getBitMask())
|
|
|
|
};
|
|
|
|
|
|
|
|
bool copyCanThrow = false;
|
|
|
|
if (ci->isByRef() && variable->getType()->getAsCXXRecordDecl()) {
|
|
|
|
const Expr *copyExpr =
|
|
|
|
CGM.getContext().getBlockVarCopyInits(variable);
|
|
|
|
if (copyExpr) {
|
|
|
|
copyCanThrow = true; // FIXME: reuse the noexcept logic
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (copyCanThrow) {
|
|
|
|
EmitRuntimeCallOrInvoke(CGM.getBlockObjectAssign(), args);
|
|
|
|
} else {
|
|
|
|
EmitNounwindRuntimeCall(CGM.getBlockObjectAssign(), args);
|
|
|
|
}
|
2012-10-17 10:28:37 +08:00
|
|
|
}
|
2009-03-07 10:35:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-08 16:22:06 +08:00
|
|
|
FinishFunction();
|
2009-03-06 09:33:24 +08:00
|
|
|
|
2011-02-15 17:22:45 +08:00
|
|
|
return llvm::ConstantExpr::getBitCast(Fn, VoidPtrTy);
|
2009-03-04 11:23:46 +08:00
|
|
|
}
|
|
|
|
|
2013-01-22 11:56:22 +08:00
|
|
|
/// Generate the destroy-helper function for a block closure object:
|
|
|
|
/// static void block_destroy_helper(block_t *theBlock);
|
|
|
|
///
|
|
|
|
/// Note that this destroys a heap-allocated block closure object;
|
|
|
|
/// it should not be confused with a 'byref destroy helper', which
|
|
|
|
/// destroys the heap-allocated contents of an individual __block
|
|
|
|
/// variable.
|
2011-02-07 18:33:21 +08:00
|
|
|
llvm::Constant *
|
2011-02-08 16:22:06 +08:00
|
|
|
CodeGenFunction::GenerateDestroyHelperFunction(const CGBlockInfo &blockInfo) {
|
2011-02-07 18:33:21 +08:00
|
|
|
ASTContext &C = getContext();
|
2009-03-06 09:33:24 +08:00
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
FunctionArgList args;
|
2011-03-09 12:27:21 +08:00
|
|
|
ImplicitParamDecl srcDecl(0, SourceLocation(), 0, C.VoidPtrTy);
|
|
|
|
args.push_back(&srcDecl);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2014-02-01 06:54:50 +08:00
|
|
|
const CGFunctionInfo &FI = CGM.getTypes().arrangeFreeFunctionDeclaration(
|
|
|
|
C.VoidTy, args, FunctionType::ExtInfo(), /*variadic=*/false);
|
2009-03-06 09:33:24 +08:00
|
|
|
|
2009-06-06 07:26:36 +08:00
|
|
|
// FIXME: We'd like to put these into a mergable by content, with
|
|
|
|
// internal linkage.
|
2012-02-17 11:33:10 +08:00
|
|
|
llvm::FunctionType *LTy = CGM.getTypes().GetFunctionType(FI);
|
2009-03-06 09:33:24 +08:00
|
|
|
|
|
|
|
llvm::Function *Fn =
|
|
|
|
llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
|
2010-01-22 21:59:13 +08:00
|
|
|
"__destroy_helper_block_", &CGM.getModule());
|
2009-03-06 09:33:24 +08:00
|
|
|
|
|
|
|
IdentifierInfo *II
|
|
|
|
= &CGM.getContext().Idents.get("__destroy_helper_block_");
|
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
FunctionDecl *FD = FunctionDecl::Create(C, C.getTranslationUnitDecl(),
|
2011-03-08 16:55:46 +08:00
|
|
|
SourceLocation(),
|
2011-02-07 18:33:21 +08:00
|
|
|
SourceLocation(), II, C.VoidTy, 0,
|
2010-08-26 11:08:43 +08:00
|
|
|
SC_Static,
|
2012-04-12 08:35:04 +08:00
|
|
|
false, false);
|
2013-07-25 04:34:39 +08:00
|
|
|
// Create a scope with an artificial location for the body of this function.
|
2013-07-18 09:36:04 +08:00
|
|
|
ArtificialLocation AL(*this, Builder);
|
2013-07-25 04:34:39 +08:00
|
|
|
StartFunction(FD, C.VoidTy, Fn, FI, args, SourceLocation());
|
|
|
|
AL.Emit();
|
2009-03-07 10:53:18 +08:00
|
|
|
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *structPtrTy = blockInfo.StructureType->getPointerTo();
|
2009-04-11 02:52:28 +08:00
|
|
|
|
2011-03-09 12:27:21 +08:00
|
|
|
llvm::Value *src = GetAddrOfLocalVar(&srcDecl);
|
2011-02-08 16:22:06 +08:00
|
|
|
src = Builder.CreateLoad(src);
|
|
|
|
src = Builder.CreateBitCast(src, structPtrTy, "block");
|
2011-02-07 18:33:21 +08:00
|
|
|
|
|
|
|
const BlockDecl *blockDecl = blockInfo.getBlockDecl();
|
|
|
|
|
2011-02-08 16:22:06 +08:00
|
|
|
CodeGenFunction::RunCleanupsScope cleanups(*this);
|
2011-02-07 18:33:21 +08:00
|
|
|
|
|
|
|
for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
|
|
|
|
ce = blockDecl->capture_end(); ci != ce; ++ci) {
|
|
|
|
const VarDecl *variable = ci->getVariable();
|
|
|
|
QualType type = variable->getType();
|
|
|
|
|
|
|
|
const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
|
|
|
|
if (capture.isConstant()) continue;
|
|
|
|
|
2011-02-08 16:22:06 +08:00
|
|
|
BlockFieldFlags flags;
|
2011-02-07 18:33:21 +08:00
|
|
|
const CXXDestructorDecl *dtor = 0;
|
|
|
|
|
2012-10-17 10:28:37 +08:00
|
|
|
bool useARCWeakDestroy = false;
|
|
|
|
bool useARCStrongDestroy = false;
|
2011-06-16 07:02:42 +08:00
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
if (ci->isByRef()) {
|
|
|
|
flags = BLOCK_FIELD_IS_BYREF;
|
2011-06-16 07:02:42 +08:00
|
|
|
if (type.isObjCGCWeak())
|
|
|
|
flags |= BLOCK_FIELD_IS_WEAK;
|
|
|
|
} else if (const CXXRecordDecl *record = type->getAsCXXRecordDecl()) {
|
|
|
|
if (record->hasTrivialDestructor())
|
|
|
|
continue;
|
|
|
|
dtor = record->getDestructor();
|
|
|
|
} else if (type->isObjCRetainableType()) {
|
2011-02-07 18:33:21 +08:00
|
|
|
flags = BLOCK_FIELD_IS_OBJECT;
|
2011-06-16 07:02:42 +08:00
|
|
|
if (type->isBlockPointerType())
|
|
|
|
flags = BLOCK_FIELD_IS_BLOCK;
|
|
|
|
|
|
|
|
// Special rules for ARC captures.
|
2012-03-11 15:00:24 +08:00
|
|
|
if (getLangOpts().ObjCAutoRefCount) {
|
2011-06-16 07:02:42 +08:00
|
|
|
Qualifiers qs = type.getQualifiers();
|
|
|
|
|
|
|
|
// Don't generate special dispose logic for a captured object
|
|
|
|
// unless it's __strong or __weak.
|
|
|
|
if (!qs.hasStrongOrWeakObjCLifetime())
|
|
|
|
continue;
|
2011-02-07 18:33:21 +08:00
|
|
|
|
2011-06-16 07:02:42 +08:00
|
|
|
// Support __weak direct captures.
|
|
|
|
if (qs.getObjCLifetime() == Qualifiers::OCL_Weak)
|
2012-10-17 10:28:37 +08:00
|
|
|
useARCWeakDestroy = true;
|
|
|
|
|
|
|
|
// Tools really want us to use objc_storeStrong here.
|
|
|
|
else
|
|
|
|
useARCStrongDestroy = true;
|
2011-06-16 07:02:42 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
2011-02-07 18:33:21 +08:00
|
|
|
|
|
|
|
unsigned index = capture.getIndex();
|
2011-02-08 16:22:06 +08:00
|
|
|
llvm::Value *srcField = Builder.CreateStructGEP(src, index);
|
2011-02-07 18:33:21 +08:00
|
|
|
|
|
|
|
// If there's an explicit copy expression, we do that.
|
|
|
|
if (dtor) {
|
2011-02-08 16:22:06 +08:00
|
|
|
PushDestructorCleanup(dtor, srcField);
|
2011-02-07 18:33:21 +08:00
|
|
|
|
2011-06-16 07:02:42 +08:00
|
|
|
// If this is a __weak capture, emit the release directly.
|
2012-10-17 10:28:37 +08:00
|
|
|
} else if (useARCWeakDestroy) {
|
2011-06-16 07:02:42 +08:00
|
|
|
EmitARCDestroyWeak(srcField);
|
|
|
|
|
2012-10-17 10:28:37 +08:00
|
|
|
// Destroy strong objects with a call if requested.
|
|
|
|
} else if (useARCStrongDestroy) {
|
2013-03-13 11:10:54 +08:00
|
|
|
EmitARCDestroyStrong(srcField, ARCImpreciseLifetime);
|
2012-10-17 10:28:37 +08:00
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
// Otherwise we call _Block_object_dispose. It wouldn't be too
|
|
|
|
// hard to just emit this as a cleanup if we wanted to make sure
|
|
|
|
// that things were done in reverse.
|
|
|
|
} else {
|
|
|
|
llvm::Value *value = Builder.CreateLoad(srcField);
|
2011-02-15 17:22:45 +08:00
|
|
|
value = Builder.CreateBitCast(value, VoidPtrTy);
|
2011-02-07 18:33:21 +08:00
|
|
|
BuildBlockRelease(value, flags);
|
2009-03-07 10:53:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
cleanups.ForceCleanup();
|
|
|
|
|
2011-02-08 16:22:06 +08:00
|
|
|
FinishFunction();
|
2009-03-06 09:33:24 +08:00
|
|
|
|
2011-02-15 17:22:45 +08:00
|
|
|
return llvm::ConstantExpr::getBitCast(Fn, VoidPtrTy);
|
2009-03-06 09:33:24 +08:00
|
|
|
}
|
|
|
|
|
2011-03-31 16:03:29 +08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
/// Emits the copy/dispose helper functions for a __block object of id type.
|
|
|
|
class ObjectByrefHelpers : public CodeGenModule::ByrefHelpers {
|
|
|
|
BlockFieldFlags Flags;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ObjectByrefHelpers(CharUnits alignment, BlockFieldFlags flags)
|
|
|
|
: ByrefHelpers(alignment), Flags(flags) {}
|
|
|
|
|
2011-03-31 17:19:20 +08:00
|
|
|
void emitCopy(CodeGenFunction &CGF, llvm::Value *destField,
|
|
|
|
llvm::Value *srcField) {
|
2011-03-31 16:03:29 +08:00
|
|
|
destField = CGF.Builder.CreateBitCast(destField, CGF.VoidPtrTy);
|
|
|
|
|
|
|
|
srcField = CGF.Builder.CreateBitCast(srcField, CGF.VoidPtrPtrTy);
|
|
|
|
llvm::Value *srcValue = CGF.Builder.CreateLoad(srcField);
|
|
|
|
|
|
|
|
unsigned flags = (Flags | BLOCK_BYREF_CALLER).getBitMask();
|
|
|
|
|
|
|
|
llvm::Value *flagsVal = llvm::ConstantInt::get(CGF.Int32Ty, flags);
|
|
|
|
llvm::Value *fn = CGF.CGM.getBlockObjectAssign();
|
2013-03-01 03:01:20 +08:00
|
|
|
|
|
|
|
llvm::Value *args[] = { destField, srcValue, flagsVal };
|
|
|
|
CGF.EmitNounwindRuntimeCall(fn, args);
|
2011-03-31 16:03:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void emitDispose(CodeGenFunction &CGF, llvm::Value *field) {
|
|
|
|
field = CGF.Builder.CreateBitCast(field, CGF.Int8PtrTy->getPointerTo(0));
|
|
|
|
llvm::Value *value = CGF.Builder.CreateLoad(field);
|
|
|
|
|
|
|
|
CGF.BuildBlockRelease(value, Flags | BLOCK_BYREF_CALLER);
|
|
|
|
}
|
|
|
|
|
|
|
|
void profileImpl(llvm::FoldingSetNodeID &id) const {
|
|
|
|
id.AddInteger(Flags.getBitMask());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-06-16 07:02:42 +08:00
|
|
|
/// Emits the copy/dispose helpers for an ARC __block __weak variable.
|
|
|
|
class ARCWeakByrefHelpers : public CodeGenModule::ByrefHelpers {
|
|
|
|
public:
|
|
|
|
ARCWeakByrefHelpers(CharUnits alignment) : ByrefHelpers(alignment) {}
|
|
|
|
|
|
|
|
void emitCopy(CodeGenFunction &CGF, llvm::Value *destField,
|
|
|
|
llvm::Value *srcField) {
|
|
|
|
CGF.EmitARCMoveWeak(destField, srcField);
|
|
|
|
}
|
|
|
|
|
|
|
|
void emitDispose(CodeGenFunction &CGF, llvm::Value *field) {
|
|
|
|
CGF.EmitARCDestroyWeak(field);
|
|
|
|
}
|
|
|
|
|
|
|
|
void profileImpl(llvm::FoldingSetNodeID &id) const {
|
|
|
|
// 0 is distinguishable from all pointers and byref flags
|
|
|
|
id.AddInteger(0);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Emits the copy/dispose helpers for an ARC __block __strong variable
|
|
|
|
/// that's not of block-pointer type.
|
|
|
|
class ARCStrongByrefHelpers : public CodeGenModule::ByrefHelpers {
|
|
|
|
public:
|
|
|
|
ARCStrongByrefHelpers(CharUnits alignment) : ByrefHelpers(alignment) {}
|
|
|
|
|
|
|
|
void emitCopy(CodeGenFunction &CGF, llvm::Value *destField,
|
|
|
|
llvm::Value *srcField) {
|
|
|
|
// Do a "move" by copying the value and then zeroing out the old
|
|
|
|
// variable.
|
|
|
|
|
2011-11-09 11:17:26 +08:00
|
|
|
llvm::LoadInst *value = CGF.Builder.CreateLoad(srcField);
|
|
|
|
value->setAlignment(Alignment.getQuantity());
|
|
|
|
|
2011-06-16 07:02:42 +08:00
|
|
|
llvm::Value *null =
|
|
|
|
llvm::ConstantPointerNull::get(cast<llvm::PointerType>(value->getType()));
|
2011-11-09 11:17:26 +08:00
|
|
|
|
2013-01-05 07:32:24 +08:00
|
|
|
if (CGF.CGM.getCodeGenOpts().OptimizationLevel == 0) {
|
2013-01-05 08:32:13 +08:00
|
|
|
llvm::StoreInst *store = CGF.Builder.CreateStore(null, destField);
|
|
|
|
store->setAlignment(Alignment.getQuantity());
|
2013-01-05 07:32:24 +08:00
|
|
|
CGF.EmitARCStoreStrongCall(destField, value, /*ignored*/ true);
|
|
|
|
CGF.EmitARCStoreStrongCall(srcField, null, /*ignored*/ true);
|
|
|
|
return;
|
|
|
|
}
|
2011-11-09 11:17:26 +08:00
|
|
|
llvm::StoreInst *store = CGF.Builder.CreateStore(value, destField);
|
|
|
|
store->setAlignment(Alignment.getQuantity());
|
|
|
|
|
|
|
|
store = CGF.Builder.CreateStore(null, srcField);
|
|
|
|
store->setAlignment(Alignment.getQuantity());
|
2011-06-16 07:02:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void emitDispose(CodeGenFunction &CGF, llvm::Value *field) {
|
2013-03-13 11:10:54 +08:00
|
|
|
CGF.EmitARCDestroyStrong(field, ARCImpreciseLifetime);
|
2011-06-16 07:02:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void profileImpl(llvm::FoldingSetNodeID &id) const {
|
|
|
|
// 1 is distinguishable from all pointers and byref flags
|
|
|
|
id.AddInteger(1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-11-09 11:17:26 +08:00
|
|
|
/// Emits the copy/dispose helpers for an ARC __block __strong
|
|
|
|
/// variable that's of block-pointer type.
|
|
|
|
class ARCStrongBlockByrefHelpers : public CodeGenModule::ByrefHelpers {
|
|
|
|
public:
|
|
|
|
ARCStrongBlockByrefHelpers(CharUnits alignment) : ByrefHelpers(alignment) {}
|
|
|
|
|
|
|
|
void emitCopy(CodeGenFunction &CGF, llvm::Value *destField,
|
|
|
|
llvm::Value *srcField) {
|
|
|
|
// Do the copy with objc_retainBlock; that's all that
|
|
|
|
// _Block_object_assign would do anyway, and we'd have to pass the
|
|
|
|
// right arguments to make sure it doesn't get no-op'ed.
|
|
|
|
llvm::LoadInst *oldValue = CGF.Builder.CreateLoad(srcField);
|
|
|
|
oldValue->setAlignment(Alignment.getQuantity());
|
|
|
|
|
|
|
|
llvm::Value *copy = CGF.EmitARCRetainBlock(oldValue, /*mandatory*/ true);
|
|
|
|
|
|
|
|
llvm::StoreInst *store = CGF.Builder.CreateStore(copy, destField);
|
|
|
|
store->setAlignment(Alignment.getQuantity());
|
|
|
|
}
|
|
|
|
|
|
|
|
void emitDispose(CodeGenFunction &CGF, llvm::Value *field) {
|
2013-03-13 11:10:54 +08:00
|
|
|
CGF.EmitARCDestroyStrong(field, ARCImpreciseLifetime);
|
2011-11-09 11:17:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void profileImpl(llvm::FoldingSetNodeID &id) const {
|
|
|
|
// 2 is distinguishable from all pointers and byref flags
|
|
|
|
id.AddInteger(2);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-03-31 16:03:29 +08:00
|
|
|
/// Emits the copy/dispose helpers for a __block variable with a
|
|
|
|
/// nontrivial copy constructor or destructor.
|
|
|
|
class CXXByrefHelpers : public CodeGenModule::ByrefHelpers {
|
|
|
|
QualType VarType;
|
|
|
|
const Expr *CopyExpr;
|
|
|
|
|
|
|
|
public:
|
|
|
|
CXXByrefHelpers(CharUnits alignment, QualType type,
|
|
|
|
const Expr *copyExpr)
|
|
|
|
: ByrefHelpers(alignment), VarType(type), CopyExpr(copyExpr) {}
|
|
|
|
|
|
|
|
bool needsCopy() const { return CopyExpr != 0; }
|
|
|
|
void emitCopy(CodeGenFunction &CGF, llvm::Value *destField,
|
|
|
|
llvm::Value *srcField) {
|
|
|
|
if (!CopyExpr) return;
|
|
|
|
CGF.EmitSynthesizedCXXCopyCtor(destField, srcField, CopyExpr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void emitDispose(CodeGenFunction &CGF, llvm::Value *field) {
|
|
|
|
EHScopeStack::stable_iterator cleanupDepth = CGF.EHStack.stable_begin();
|
|
|
|
CGF.PushDestructorCleanup(VarType, field);
|
|
|
|
CGF.PopCleanupBlocks(cleanupDepth);
|
|
|
|
}
|
|
|
|
|
|
|
|
void profileImpl(llvm::FoldingSetNodeID &id) const {
|
|
|
|
id.AddPointer(VarType.getCanonicalType().getAsOpaquePtr());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
static llvm::Constant *
|
|
|
|
generateByrefCopyHelper(CodeGenFunction &CGF,
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::StructType &byrefType,
|
2013-01-22 11:56:22 +08:00
|
|
|
unsigned valueFieldIndex,
|
2011-03-31 16:03:29 +08:00
|
|
|
CodeGenModule::ByrefHelpers &byrefInfo) {
|
|
|
|
ASTContext &Context = CGF.getContext();
|
|
|
|
|
|
|
|
QualType R = Context.VoidTy;
|
2009-03-06 10:29:21 +08:00
|
|
|
|
2011-03-09 12:27:21 +08:00
|
|
|
FunctionArgList args;
|
2011-03-31 16:03:29 +08:00
|
|
|
ImplicitParamDecl dst(0, SourceLocation(), 0, Context.VoidPtrTy);
|
2011-03-09 12:27:21 +08:00
|
|
|
args.push_back(&dst);
|
|
|
|
|
2011-03-31 16:03:29 +08:00
|
|
|
ImplicitParamDecl src(0, SourceLocation(), 0, Context.VoidPtrTy);
|
2011-03-09 12:27:21 +08:00
|
|
|
args.push_back(&src);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2014-02-01 06:54:50 +08:00
|
|
|
const CGFunctionInfo &FI = CGF.CGM.getTypes().arrangeFreeFunctionDeclaration(
|
|
|
|
R, args, FunctionType::ExtInfo(), /*variadic=*/false);
|
2009-03-06 10:29:21 +08:00
|
|
|
|
2011-03-31 16:03:29 +08:00
|
|
|
CodeGenTypes &Types = CGF.CGM.getTypes();
|
2012-02-17 11:33:10 +08:00
|
|
|
llvm::FunctionType *LTy = Types.GetFunctionType(FI);
|
2009-03-06 10:29:21 +08:00
|
|
|
|
2009-06-06 07:26:36 +08:00
|
|
|
// FIXME: We'd like to put these into a mergable by content, with
|
|
|
|
// internal linkage.
|
2009-03-06 10:29:21 +08:00
|
|
|
llvm::Function *Fn =
|
|
|
|
llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
|
2011-03-31 16:03:29 +08:00
|
|
|
"__Block_byref_object_copy_", &CGF.CGM.getModule());
|
2009-03-06 10:29:21 +08:00
|
|
|
|
|
|
|
IdentifierInfo *II
|
2011-03-31 16:03:29 +08:00
|
|
|
= &Context.Idents.get("__Block_byref_object_copy_");
|
2009-03-06 10:29:21 +08:00
|
|
|
|
2011-03-31 16:03:29 +08:00
|
|
|
FunctionDecl *FD = FunctionDecl::Create(Context,
|
|
|
|
Context.getTranslationUnitDecl(),
|
2011-03-08 16:55:46 +08:00
|
|
|
SourceLocation(),
|
2009-08-19 09:27:57 +08:00
|
|
|
SourceLocation(), II, R, 0,
|
2010-08-26 11:08:43 +08:00
|
|
|
SC_Static,
|
2012-04-12 10:16:49 +08:00
|
|
|
false, false);
|
2011-06-16 07:02:42 +08:00
|
|
|
|
2011-03-31 16:03:29 +08:00
|
|
|
CGF.StartFunction(FD, R, Fn, FI, args, SourceLocation());
|
2009-03-06 10:29:21 +08:00
|
|
|
|
2011-03-31 16:03:29 +08:00
|
|
|
if (byrefInfo.needsCopy()) {
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *byrefPtrType = byrefType.getPointerTo(0);
|
2011-03-31 16:03:29 +08:00
|
|
|
|
|
|
|
// dst->x
|
|
|
|
llvm::Value *destField = CGF.GetAddrOfLocalVar(&dst);
|
|
|
|
destField = CGF.Builder.CreateLoad(destField);
|
|
|
|
destField = CGF.Builder.CreateBitCast(destField, byrefPtrType);
|
2013-01-22 11:56:22 +08:00
|
|
|
destField = CGF.Builder.CreateStructGEP(destField, valueFieldIndex, "x");
|
2011-03-31 16:03:29 +08:00
|
|
|
|
|
|
|
// src->x
|
|
|
|
llvm::Value *srcField = CGF.GetAddrOfLocalVar(&src);
|
|
|
|
srcField = CGF.Builder.CreateLoad(srcField);
|
|
|
|
srcField = CGF.Builder.CreateBitCast(srcField, byrefPtrType);
|
2013-01-22 11:56:22 +08:00
|
|
|
srcField = CGF.Builder.CreateStructGEP(srcField, valueFieldIndex, "x");
|
2011-03-31 16:03:29 +08:00
|
|
|
|
|
|
|
byrefInfo.emitCopy(CGF, destField, srcField);
|
|
|
|
}
|
|
|
|
|
|
|
|
CGF.FinishFunction();
|
|
|
|
|
|
|
|
return llvm::ConstantExpr::getBitCast(Fn, CGF.Int8PtrTy);
|
2009-03-06 10:29:21 +08:00
|
|
|
}
|
|
|
|
|
2011-03-31 16:03:29 +08:00
|
|
|
/// Build the copy helper for a __block variable.
|
|
|
|
static llvm::Constant *buildByrefCopyHelper(CodeGenModule &CGM,
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::StructType &byrefType,
|
2013-01-22 11:56:22 +08:00
|
|
|
unsigned byrefValueIndex,
|
2011-03-31 16:03:29 +08:00
|
|
|
CodeGenModule::ByrefHelpers &info) {
|
|
|
|
CodeGenFunction CGF(CGM);
|
2013-01-22 11:56:22 +08:00
|
|
|
return generateByrefCopyHelper(CGF, byrefType, byrefValueIndex, info);
|
2011-03-31 16:03:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Generate code for a __block variable's dispose helper.
|
|
|
|
static llvm::Constant *
|
|
|
|
generateByrefDisposeHelper(CodeGenFunction &CGF,
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::StructType &byrefType,
|
2013-01-22 11:56:22 +08:00
|
|
|
unsigned byrefValueIndex,
|
2011-03-31 16:03:29 +08:00
|
|
|
CodeGenModule::ByrefHelpers &byrefInfo) {
|
|
|
|
ASTContext &Context = CGF.getContext();
|
|
|
|
QualType R = Context.VoidTy;
|
2009-03-06 10:29:21 +08:00
|
|
|
|
2011-03-09 12:27:21 +08:00
|
|
|
FunctionArgList args;
|
2011-03-31 16:03:29 +08:00
|
|
|
ImplicitParamDecl src(0, SourceLocation(), 0, Context.VoidPtrTy);
|
2011-03-09 12:27:21 +08:00
|
|
|
args.push_back(&src);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2014-02-01 06:54:50 +08:00
|
|
|
const CGFunctionInfo &FI = CGF.CGM.getTypes().arrangeFreeFunctionDeclaration(
|
|
|
|
R, args, FunctionType::ExtInfo(), /*variadic=*/false);
|
2009-03-06 10:29:21 +08:00
|
|
|
|
2011-03-31 16:03:29 +08:00
|
|
|
CodeGenTypes &Types = CGF.CGM.getTypes();
|
2012-02-17 11:33:10 +08:00
|
|
|
llvm::FunctionType *LTy = Types.GetFunctionType(FI);
|
2009-03-06 10:29:21 +08:00
|
|
|
|
2009-06-06 07:26:36 +08:00
|
|
|
// FIXME: We'd like to put these into a mergable by content, with
|
|
|
|
// internal linkage.
|
2009-03-06 10:29:21 +08:00
|
|
|
llvm::Function *Fn =
|
|
|
|
llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
|
2010-12-03 01:02:11 +08:00
|
|
|
"__Block_byref_object_dispose_",
|
2011-03-31 16:03:29 +08:00
|
|
|
&CGF.CGM.getModule());
|
2009-03-06 10:29:21 +08:00
|
|
|
|
|
|
|
IdentifierInfo *II
|
2011-03-31 16:03:29 +08:00
|
|
|
= &Context.Idents.get("__Block_byref_object_dispose_");
|
2009-03-06 10:29:21 +08:00
|
|
|
|
2011-03-31 16:03:29 +08:00
|
|
|
FunctionDecl *FD = FunctionDecl::Create(Context,
|
|
|
|
Context.getTranslationUnitDecl(),
|
2011-03-08 16:55:46 +08:00
|
|
|
SourceLocation(),
|
2009-08-19 09:27:57 +08:00
|
|
|
SourceLocation(), II, R, 0,
|
2010-08-26 11:08:43 +08:00
|
|
|
SC_Static,
|
2012-04-12 10:16:49 +08:00
|
|
|
false, false);
|
2011-03-31 16:03:29 +08:00
|
|
|
CGF.StartFunction(FD, R, Fn, FI, args, SourceLocation());
|
2009-03-06 12:53:30 +08:00
|
|
|
|
2011-03-31 16:03:29 +08:00
|
|
|
if (byrefInfo.needsDispose()) {
|
|
|
|
llvm::Value *V = CGF.GetAddrOfLocalVar(&src);
|
|
|
|
V = CGF.Builder.CreateLoad(V);
|
|
|
|
V = CGF.Builder.CreateBitCast(V, byrefType.getPointerTo(0));
|
2013-01-22 11:56:22 +08:00
|
|
|
V = CGF.Builder.CreateStructGEP(V, byrefValueIndex, "x");
|
2011-02-08 16:22:06 +08:00
|
|
|
|
2011-03-31 16:03:29 +08:00
|
|
|
byrefInfo.emitDispose(CGF, V);
|
2010-12-03 01:02:11 +08:00
|
|
|
}
|
2009-03-06 10:29:21 +08:00
|
|
|
|
2011-03-31 16:03:29 +08:00
|
|
|
CGF.FinishFunction();
|
2011-02-08 16:22:06 +08:00
|
|
|
|
2011-03-31 16:03:29 +08:00
|
|
|
return llvm::ConstantExpr::getBitCast(Fn, CGF.Int8PtrTy);
|
2009-03-06 10:29:21 +08:00
|
|
|
}
|
|
|
|
|
2011-03-31 16:03:29 +08:00
|
|
|
/// Build the dispose helper for a __block variable.
|
|
|
|
static llvm::Constant *buildByrefDisposeHelper(CodeGenModule &CGM,
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::StructType &byrefType,
|
2013-01-22 11:56:22 +08:00
|
|
|
unsigned byrefValueIndex,
|
2011-03-31 16:03:29 +08:00
|
|
|
CodeGenModule::ByrefHelpers &info) {
|
|
|
|
CodeGenFunction CGF(CGM);
|
2013-01-22 11:56:22 +08:00
|
|
|
return generateByrefDisposeHelper(CGF, byrefType, byrefValueIndex, info);
|
2009-03-06 10:29:21 +08:00
|
|
|
}
|
|
|
|
|
2013-01-22 11:56:22 +08:00
|
|
|
/// Lazily build the copy and dispose helpers for a __block variable
|
|
|
|
/// with the given information.
|
2011-03-31 16:03:29 +08:00
|
|
|
template <class T> static T *buildByrefHelpers(CodeGenModule &CGM,
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::StructType &byrefTy,
|
2013-01-22 11:56:22 +08:00
|
|
|
unsigned byrefValueIndex,
|
2011-03-31 16:03:29 +08:00
|
|
|
T &byrefInfo) {
|
|
|
|
// Increase the field's alignment to be at least pointer alignment,
|
|
|
|
// since the layout of the byref struct will guarantee at least that.
|
|
|
|
byrefInfo.Alignment = std::max(byrefInfo.Alignment,
|
|
|
|
CharUnits::fromQuantity(CGM.PointerAlignInBytes));
|
|
|
|
|
|
|
|
llvm::FoldingSetNodeID id;
|
|
|
|
byrefInfo.Profile(id);
|
|
|
|
|
|
|
|
void *insertPos;
|
|
|
|
CodeGenModule::ByrefHelpers *node
|
|
|
|
= CGM.ByrefHelpersCache.FindNodeOrInsertPos(id, insertPos);
|
|
|
|
if (node) return static_cast<T*>(node);
|
|
|
|
|
2013-01-22 11:56:22 +08:00
|
|
|
byrefInfo.CopyHelper =
|
|
|
|
buildByrefCopyHelper(CGM, byrefTy, byrefValueIndex, byrefInfo);
|
|
|
|
byrefInfo.DisposeHelper =
|
|
|
|
buildByrefDisposeHelper(CGM, byrefTy, byrefValueIndex,byrefInfo);
|
2011-03-31 16:03:29 +08:00
|
|
|
|
|
|
|
T *copy = new (CGM.getContext()) T(byrefInfo);
|
|
|
|
CGM.ByrefHelpersCache.InsertNode(copy, insertPos);
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
2013-01-22 11:56:22 +08:00
|
|
|
/// Build the copy and dispose helpers for the given __block variable
|
|
|
|
/// emission. Places the helpers in the global cache. Returns null
|
|
|
|
/// if no helpers are required.
|
2011-03-31 16:03:29 +08:00
|
|
|
CodeGenModule::ByrefHelpers *
|
2011-07-18 12:24:23 +08:00
|
|
|
CodeGenFunction::buildByrefHelpers(llvm::StructType &byrefType,
|
2011-03-31 16:03:29 +08:00
|
|
|
const AutoVarEmission &emission) {
|
|
|
|
const VarDecl &var = *emission.Variable;
|
|
|
|
QualType type = var.getType();
|
|
|
|
|
2013-01-22 11:56:22 +08:00
|
|
|
unsigned byrefValueIndex = getByRefValueLLVMField(&var);
|
|
|
|
|
2011-03-31 16:03:29 +08:00
|
|
|
if (const CXXRecordDecl *record = type->getAsCXXRecordDecl()) {
|
|
|
|
const Expr *copyExpr = CGM.getContext().getBlockVarCopyInits(&var);
|
|
|
|
if (!copyExpr && record->hasTrivialDestructor()) return 0;
|
|
|
|
|
|
|
|
CXXByrefHelpers byrefInfo(emission.Alignment, type, copyExpr);
|
2013-01-22 11:56:22 +08:00
|
|
|
return ::buildByrefHelpers(CGM, byrefType, byrefValueIndex, byrefInfo);
|
2011-03-31 16:03:29 +08:00
|
|
|
}
|
|
|
|
|
2011-06-16 07:02:42 +08:00
|
|
|
// Otherwise, if we don't have a retainable type, there's nothing to do.
|
|
|
|
// that the runtime does extra copies.
|
|
|
|
if (!type->isObjCRetainableType()) return 0;
|
|
|
|
|
|
|
|
Qualifiers qs = type.getQualifiers();
|
|
|
|
|
|
|
|
// If we have lifetime, that dominates.
|
|
|
|
if (Qualifiers::ObjCLifetime lifetime = qs.getObjCLifetime()) {
|
2012-03-11 15:00:24 +08:00
|
|
|
assert(getLangOpts().ObjCAutoRefCount);
|
2011-06-16 07:02:42 +08:00
|
|
|
|
|
|
|
switch (lifetime) {
|
|
|
|
case Qualifiers::OCL_None: llvm_unreachable("impossible");
|
|
|
|
|
|
|
|
// These are just bits as far as the runtime is concerned.
|
|
|
|
case Qualifiers::OCL_ExplicitNone:
|
|
|
|
case Qualifiers::OCL_Autoreleasing:
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// Tell the runtime that this is ARC __weak, called by the
|
|
|
|
// byref routines.
|
|
|
|
case Qualifiers::OCL_Weak: {
|
|
|
|
ARCWeakByrefHelpers byrefInfo(emission.Alignment);
|
2013-01-22 11:56:22 +08:00
|
|
|
return ::buildByrefHelpers(CGM, byrefType, byrefValueIndex, byrefInfo);
|
2011-06-16 07:02:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// ARC __strong __block variables need to be retained.
|
|
|
|
case Qualifiers::OCL_Strong:
|
2011-11-09 11:17:26 +08:00
|
|
|
// Block pointers need to be copied, and there's no direct
|
|
|
|
// transfer possible.
|
2011-06-16 07:02:42 +08:00
|
|
|
if (type->isBlockPointerType()) {
|
2011-11-09 11:17:26 +08:00
|
|
|
ARCStrongBlockByrefHelpers byrefInfo(emission.Alignment);
|
2013-01-22 11:56:22 +08:00
|
|
|
return ::buildByrefHelpers(CGM, byrefType, byrefValueIndex, byrefInfo);
|
2011-06-16 07:02:42 +08:00
|
|
|
|
|
|
|
// Otherwise, we transfer ownership of the retain from the stack
|
|
|
|
// to the heap.
|
|
|
|
} else {
|
|
|
|
ARCStrongByrefHelpers byrefInfo(emission.Alignment);
|
2013-01-22 11:56:22 +08:00
|
|
|
return ::buildByrefHelpers(CGM, byrefType, byrefValueIndex, byrefInfo);
|
2011-06-16 07:02:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
llvm_unreachable("fell out of lifetime switch!");
|
|
|
|
}
|
|
|
|
|
2011-03-31 16:03:29 +08:00
|
|
|
BlockFieldFlags flags;
|
|
|
|
if (type->isBlockPointerType()) {
|
|
|
|
flags |= BLOCK_FIELD_IS_BLOCK;
|
|
|
|
} else if (CGM.getContext().isObjCNSObjectType(type) ||
|
|
|
|
type->isObjCObjectPointerType()) {
|
|
|
|
flags |= BLOCK_FIELD_IS_OBJECT;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type.isObjCGCWeak())
|
|
|
|
flags |= BLOCK_FIELD_IS_WEAK;
|
|
|
|
|
|
|
|
ObjectByrefHelpers byrefInfo(emission.Alignment, flags);
|
2013-01-22 11:56:22 +08:00
|
|
|
return ::buildByrefHelpers(CGM, byrefType, byrefValueIndex, byrefInfo);
|
2009-03-06 10:29:21 +08:00
|
|
|
}
|
|
|
|
|
2011-03-31 09:59:53 +08:00
|
|
|
unsigned CodeGenFunction::getByRefValueLLVMField(const ValueDecl *VD) const {
|
|
|
|
assert(ByRefValueInfo.count(VD) && "Did not find value!");
|
|
|
|
|
|
|
|
return ByRefValueInfo.find(VD)->second.second;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Value *CodeGenFunction::BuildBlockByrefAddress(llvm::Value *BaseAddr,
|
|
|
|
const VarDecl *V) {
|
|
|
|
llvm::Value *Loc = Builder.CreateStructGEP(BaseAddr, 1, "forwarding");
|
|
|
|
Loc = Builder.CreateLoad(Loc);
|
|
|
|
Loc = Builder.CreateStructGEP(Loc, getByRefValueLLVMField(V),
|
|
|
|
V->getNameAsString());
|
|
|
|
return Loc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// BuildByRefType - This routine changes a __block variable declared as T x
|
|
|
|
/// into:
|
|
|
|
///
|
|
|
|
/// struct {
|
|
|
|
/// void *__isa;
|
|
|
|
/// void *__forwarding;
|
|
|
|
/// int32_t __flags;
|
|
|
|
/// int32_t __size;
|
|
|
|
/// void *__copy_helper; // only if needed
|
|
|
|
/// void *__destroy_helper; // only if needed
|
2012-11-15 01:15:51 +08:00
|
|
|
/// void *__byref_variable_layout;// only if needed
|
2011-03-31 09:59:53 +08:00
|
|
|
/// char padding[X]; // only if needed
|
|
|
|
/// T x;
|
|
|
|
/// } x
|
|
|
|
///
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *CodeGenFunction::BuildByRefType(const VarDecl *D) {
|
|
|
|
std::pair<llvm::Type *, unsigned> &Info = ByRefValueInfo[D];
|
2011-03-31 09:59:53 +08:00
|
|
|
if (Info.first)
|
|
|
|
return Info.first;
|
|
|
|
|
|
|
|
QualType Ty = D->getType();
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<llvm::Type *, 8> types;
|
2011-03-31 09:59:53 +08:00
|
|
|
|
2011-07-10 01:41:47 +08:00
|
|
|
llvm::StructType *ByRefType =
|
2011-08-13 01:43:31 +08:00
|
|
|
llvm::StructType::create(getLLVMContext(),
|
|
|
|
"struct.__block_byref_" + D->getNameAsString());
|
2011-03-31 09:59:53 +08:00
|
|
|
|
|
|
|
// void *__isa;
|
2011-05-15 09:53:33 +08:00
|
|
|
types.push_back(Int8PtrTy);
|
2011-03-31 09:59:53 +08:00
|
|
|
|
|
|
|
// void *__forwarding;
|
2011-07-10 01:41:47 +08:00
|
|
|
types.push_back(llvm::PointerType::getUnqual(ByRefType));
|
2011-03-31 09:59:53 +08:00
|
|
|
|
|
|
|
// int32_t __flags;
|
2011-05-15 09:53:33 +08:00
|
|
|
types.push_back(Int32Ty);
|
2011-03-31 09:59:53 +08:00
|
|
|
|
|
|
|
// int32_t __size;
|
2011-05-15 09:53:33 +08:00
|
|
|
types.push_back(Int32Ty);
|
2012-11-29 07:12:17 +08:00
|
|
|
// Note that this must match *exactly* the logic in buildByrefHelpers.
|
|
|
|
bool HasCopyAndDispose = getContext().BlockRequiresCopying(Ty, D);
|
2011-03-31 09:59:53 +08:00
|
|
|
if (HasCopyAndDispose) {
|
|
|
|
/// void *__copy_helper;
|
2011-05-15 09:53:33 +08:00
|
|
|
types.push_back(Int8PtrTy);
|
2011-03-31 09:59:53 +08:00
|
|
|
|
|
|
|
/// void *__destroy_helper;
|
2011-05-15 09:53:33 +08:00
|
|
|
types.push_back(Int8PtrTy);
|
2011-03-31 09:59:53 +08:00
|
|
|
}
|
2012-11-15 01:15:51 +08:00
|
|
|
bool HasByrefExtendedLayout = false;
|
|
|
|
Qualifiers::ObjCLifetime Lifetime;
|
|
|
|
if (getContext().getByrefLifetime(Ty, Lifetime, HasByrefExtendedLayout) &&
|
|
|
|
HasByrefExtendedLayout)
|
|
|
|
/// void *__byref_variable_layout;
|
|
|
|
types.push_back(Int8PtrTy);
|
2011-03-31 09:59:53 +08:00
|
|
|
|
|
|
|
bool Packed = false;
|
|
|
|
CharUnits Align = getContext().getDeclAlign(D);
|
2013-04-17 06:48:15 +08:00
|
|
|
if (Align >
|
|
|
|
getContext().toCharUnitsFromBits(getTarget().getPointerAlign(0))) {
|
2011-03-31 09:59:53 +08:00
|
|
|
// We have to insert padding.
|
|
|
|
|
|
|
|
// The struct above has 2 32-bit integers.
|
|
|
|
unsigned CurrentOffsetInBytes = 4 * 2;
|
|
|
|
|
2012-11-15 01:15:51 +08:00
|
|
|
// And either 2, 3, 4 or 5 pointers.
|
|
|
|
unsigned noPointers = 2;
|
|
|
|
if (HasCopyAndDispose)
|
|
|
|
noPointers += 2;
|
|
|
|
if (HasByrefExtendedLayout)
|
|
|
|
noPointers += 1;
|
|
|
|
|
|
|
|
CurrentOffsetInBytes += noPointers * CGM.getDataLayout().getTypeAllocSize(Int8PtrTy);
|
2011-03-31 09:59:53 +08:00
|
|
|
|
|
|
|
// Align the offset.
|
|
|
|
unsigned AlignedOffsetInBytes =
|
|
|
|
llvm::RoundUpToAlignment(CurrentOffsetInBytes, Align.getQuantity());
|
|
|
|
|
|
|
|
unsigned NumPaddingBytes = AlignedOffsetInBytes - CurrentOffsetInBytes;
|
|
|
|
if (NumPaddingBytes > 0) {
|
2012-02-07 08:39:47 +08:00
|
|
|
llvm::Type *Ty = Int8Ty;
|
2011-03-31 09:59:53 +08:00
|
|
|
// FIXME: We need a sema error for alignment larger than the minimum of
|
2011-05-15 09:53:33 +08:00
|
|
|
// the maximal stack alignment and the alignment of malloc on the system.
|
2011-03-31 09:59:53 +08:00
|
|
|
if (NumPaddingBytes > 1)
|
|
|
|
Ty = llvm::ArrayType::get(Ty, NumPaddingBytes);
|
|
|
|
|
2011-05-15 09:53:33 +08:00
|
|
|
types.push_back(Ty);
|
2011-03-31 09:59:53 +08:00
|
|
|
|
|
|
|
// We want a packed struct.
|
|
|
|
Packed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// T x;
|
2011-05-15 09:53:33 +08:00
|
|
|
types.push_back(ConvertTypeForMem(Ty));
|
2011-03-31 09:59:53 +08:00
|
|
|
|
2011-07-10 01:41:47 +08:00
|
|
|
ByRefType->setBody(types, Packed);
|
2011-03-31 09:59:53 +08:00
|
|
|
|
2011-07-10 01:41:47 +08:00
|
|
|
Info.first = ByRefType;
|
2011-03-31 09:59:53 +08:00
|
|
|
|
2011-05-15 09:53:33 +08:00
|
|
|
Info.second = types.size() - 1;
|
2011-03-31 09:59:53 +08:00
|
|
|
|
|
|
|
return Info.first;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Initialize the structural components of a __block variable, i.e.
|
|
|
|
/// everything but the actual object.
|
|
|
|
void CodeGenFunction::emitByrefStructureInit(const AutoVarEmission &emission) {
|
2011-03-31 16:03:29 +08:00
|
|
|
// Find the address of the local.
|
|
|
|
llvm::Value *addr = emission.Address;
|
|
|
|
|
|
|
|
// That's an alloca of the byref structure type.
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::StructType *byrefType = cast<llvm::StructType>(
|
2011-03-31 16:03:29 +08:00
|
|
|
cast<llvm::PointerType>(addr->getType())->getElementType());
|
2011-03-31 09:59:53 +08:00
|
|
|
|
2011-03-31 16:03:29 +08:00
|
|
|
// Build the byref helpers if necessary. This is null if we don't need any.
|
|
|
|
CodeGenModule::ByrefHelpers *helpers =
|
|
|
|
buildByrefHelpers(*byrefType, emission);
|
2011-03-31 09:59:53 +08:00
|
|
|
|
|
|
|
const VarDecl &D = *emission.Variable;
|
|
|
|
QualType type = D.getType();
|
|
|
|
|
2012-11-15 01:15:51 +08:00
|
|
|
bool HasByrefExtendedLayout;
|
|
|
|
Qualifiers::ObjCLifetime ByrefLifetime;
|
|
|
|
bool ByRefHasLifetime =
|
|
|
|
getContext().getByrefLifetime(type, ByrefLifetime, HasByrefExtendedLayout);
|
|
|
|
|
2011-03-31 16:03:29 +08:00
|
|
|
llvm::Value *V;
|
2011-03-31 09:59:53 +08:00
|
|
|
|
|
|
|
// Initialize the 'isa', which is just 0 or 1.
|
|
|
|
int isa = 0;
|
2011-03-31 16:03:29 +08:00
|
|
|
if (type.isObjCGCWeak())
|
2011-03-31 09:59:53 +08:00
|
|
|
isa = 1;
|
|
|
|
V = Builder.CreateIntToPtr(Builder.getInt32(isa), Int8PtrTy, "isa");
|
|
|
|
Builder.CreateStore(V, Builder.CreateStructGEP(addr, 0, "byref.isa"));
|
|
|
|
|
|
|
|
// Store the address of the variable into its own forwarding pointer.
|
|
|
|
Builder.CreateStore(addr,
|
|
|
|
Builder.CreateStructGEP(addr, 1, "byref.forwarding"));
|
|
|
|
|
|
|
|
// Blocks ABI:
|
|
|
|
// c) the flags field is set to either 0 if no helper functions are
|
2012-11-15 01:15:51 +08:00
|
|
|
// needed or BLOCK_BYREF_HAS_COPY_DISPOSE if they are,
|
2011-03-31 09:59:53 +08:00
|
|
|
BlockFlags flags;
|
2012-11-15 01:15:51 +08:00
|
|
|
if (helpers) flags |= BLOCK_BYREF_HAS_COPY_DISPOSE;
|
|
|
|
if (ByRefHasLifetime) {
|
|
|
|
if (HasByrefExtendedLayout) flags |= BLOCK_BYREF_LAYOUT_EXTENDED;
|
|
|
|
else switch (ByrefLifetime) {
|
|
|
|
case Qualifiers::OCL_Strong:
|
|
|
|
flags |= BLOCK_BYREF_LAYOUT_STRONG;
|
|
|
|
break;
|
|
|
|
case Qualifiers::OCL_Weak:
|
|
|
|
flags |= BLOCK_BYREF_LAYOUT_WEAK;
|
|
|
|
break;
|
|
|
|
case Qualifiers::OCL_ExplicitNone:
|
|
|
|
flags |= BLOCK_BYREF_LAYOUT_UNRETAINED;
|
|
|
|
break;
|
|
|
|
case Qualifiers::OCL_None:
|
|
|
|
if (!type->isObjCObjectPointerType() && !type->isBlockPointerType())
|
|
|
|
flags |= BLOCK_BYREF_LAYOUT_NON_OBJECT;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (CGM.getLangOpts().ObjCGCBitmapPrint) {
|
|
|
|
printf("\n Inline flag for BYREF variable layout (%d):", flags.getBitMask());
|
|
|
|
if (flags & BLOCK_BYREF_HAS_COPY_DISPOSE)
|
|
|
|
printf(" BLOCK_BYREF_HAS_COPY_DISPOSE");
|
|
|
|
if (flags & BLOCK_BYREF_LAYOUT_MASK) {
|
|
|
|
BlockFlags ThisFlag(flags.getBitMask() & BLOCK_BYREF_LAYOUT_MASK);
|
|
|
|
if (ThisFlag == BLOCK_BYREF_LAYOUT_EXTENDED)
|
|
|
|
printf(" BLOCK_BYREF_LAYOUT_EXTENDED");
|
|
|
|
if (ThisFlag == BLOCK_BYREF_LAYOUT_STRONG)
|
|
|
|
printf(" BLOCK_BYREF_LAYOUT_STRONG");
|
|
|
|
if (ThisFlag == BLOCK_BYREF_LAYOUT_WEAK)
|
|
|
|
printf(" BLOCK_BYREF_LAYOUT_WEAK");
|
|
|
|
if (ThisFlag == BLOCK_BYREF_LAYOUT_UNRETAINED)
|
|
|
|
printf(" BLOCK_BYREF_LAYOUT_UNRETAINED");
|
|
|
|
if (ThisFlag == BLOCK_BYREF_LAYOUT_NON_OBJECT)
|
|
|
|
printf(" BLOCK_BYREF_LAYOUT_NON_OBJECT");
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-31 09:59:53 +08:00
|
|
|
Builder.CreateStore(llvm::ConstantInt::get(IntTy, flags.getBitMask()),
|
|
|
|
Builder.CreateStructGEP(addr, 2, "byref.flags"));
|
|
|
|
|
2011-03-31 16:03:29 +08:00
|
|
|
CharUnits byrefSize = CGM.GetTargetTypeStoreSize(byrefType);
|
|
|
|
V = llvm::ConstantInt::get(IntTy, byrefSize.getQuantity());
|
2011-03-31 09:59:53 +08:00
|
|
|
Builder.CreateStore(V, Builder.CreateStructGEP(addr, 3, "byref.size"));
|
|
|
|
|
2011-03-31 16:03:29 +08:00
|
|
|
if (helpers) {
|
2011-03-31 09:59:53 +08:00
|
|
|
llvm::Value *copy_helper = Builder.CreateStructGEP(addr, 4);
|
2011-03-31 16:03:29 +08:00
|
|
|
Builder.CreateStore(helpers->CopyHelper, copy_helper);
|
2011-03-31 09:59:53 +08:00
|
|
|
|
|
|
|
llvm::Value *destroy_helper = Builder.CreateStructGEP(addr, 5);
|
2011-03-31 16:03:29 +08:00
|
|
|
Builder.CreateStore(helpers->DisposeHelper, destroy_helper);
|
2011-03-31 09:59:53 +08:00
|
|
|
}
|
2012-11-15 01:15:51 +08:00
|
|
|
if (ByRefHasLifetime && HasByrefExtendedLayout) {
|
|
|
|
llvm::Constant* ByrefLayoutInfo = CGM.getObjCRuntime().BuildByrefLayout(CGM, type);
|
|
|
|
llvm::Value *ByrefInfoAddr = Builder.CreateStructGEP(addr, helpers ? 6 : 4,
|
|
|
|
"byref.layout");
|
|
|
|
// cast destination to pointer to source type.
|
|
|
|
llvm::Type *DesTy = ByrefLayoutInfo->getType();
|
|
|
|
DesTy = DesTy->getPointerTo();
|
|
|
|
llvm::Value *BC = Builder.CreatePointerCast(ByrefInfoAddr, DesTy);
|
|
|
|
Builder.CreateStore(ByrefLayoutInfo, BC);
|
|
|
|
}
|
2011-03-31 09:59:53 +08:00
|
|
|
}
|
|
|
|
|
2011-02-08 16:22:06 +08:00
|
|
|
void CodeGenFunction::BuildBlockRelease(llvm::Value *V, BlockFieldFlags flags) {
|
2010-07-16 08:00:15 +08:00
|
|
|
llvm::Value *F = CGM.getBlockObjectDispose();
|
2013-03-01 03:01:20 +08:00
|
|
|
llvm::Value *args[] = {
|
|
|
|
Builder.CreateBitCast(V, Int8PtrTy),
|
|
|
|
llvm::ConstantInt::get(Int32Ty, flags.getBitMask())
|
|
|
|
};
|
|
|
|
EmitNounwindRuntimeCall(F, args); // FIXME: throwing destructors?
|
2009-03-05 09:23:13 +08:00
|
|
|
}
|
2011-03-31 09:59:53 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
struct CallBlockRelease : EHScopeStack::Cleanup {
|
|
|
|
llvm::Value *Addr;
|
|
|
|
CallBlockRelease(llvm::Value *Addr) : Addr(Addr) {}
|
|
|
|
|
2011-07-13 04:27:29 +08:00
|
|
|
void Emit(CodeGenFunction &CGF, Flags flags) {
|
2011-06-16 07:02:42 +08:00
|
|
|
// Should we be passing FIELD_IS_WEAK here?
|
2011-03-31 09:59:53 +08:00
|
|
|
CGF.BuildBlockRelease(Addr, BLOCK_FIELD_IS_BYREF);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Enter a cleanup to destroy a __block variable. Note that this
|
|
|
|
/// cleanup should be a no-op if the variable hasn't left the stack
|
|
|
|
/// yet; if a cleanup is required for the variable itself, that needs
|
|
|
|
/// to be done externally.
|
|
|
|
void CodeGenFunction::enterByrefCleanup(const AutoVarEmission &emission) {
|
|
|
|
// We don't enter this cleanup if we're in pure-GC mode.
|
2012-03-11 15:00:24 +08:00
|
|
|
if (CGM.getLangOpts().getGC() == LangOptions::GCOnly)
|
2011-03-31 09:59:53 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
EHStack.pushCleanup<CallBlockRelease>(NormalAndEHCleanup, emission.Address);
|
|
|
|
}
|
2011-09-10 04:41:01 +08:00
|
|
|
|
|
|
|
/// Adjust the declaration of something from the blocks API.
|
|
|
|
static void configureBlocksRuntimeObject(CodeGenModule &CGM,
|
|
|
|
llvm::Constant *C) {
|
2012-03-11 15:00:24 +08:00
|
|
|
if (!CGM.getLangOpts().BlocksRuntimeOptional) return;
|
2011-09-10 04:41:01 +08:00
|
|
|
|
|
|
|
llvm::GlobalValue *GV = cast<llvm::GlobalValue>(C->stripPointerCasts());
|
|
|
|
if (GV->isDeclaration() &&
|
|
|
|
GV->getLinkage() == llvm::GlobalValue::ExternalLinkage)
|
|
|
|
GV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Constant *CodeGenModule::getBlockObjectDispose() {
|
|
|
|
if (BlockObjectDispose)
|
|
|
|
return BlockObjectDispose;
|
|
|
|
|
|
|
|
llvm::Type *args[] = { Int8PtrTy, Int32Ty };
|
|
|
|
llvm::FunctionType *fty
|
|
|
|
= llvm::FunctionType::get(VoidTy, args, false);
|
|
|
|
BlockObjectDispose = CreateRuntimeFunction(fty, "_Block_object_dispose");
|
|
|
|
configureBlocksRuntimeObject(*this, BlockObjectDispose);
|
|
|
|
return BlockObjectDispose;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Constant *CodeGenModule::getBlockObjectAssign() {
|
|
|
|
if (BlockObjectAssign)
|
|
|
|
return BlockObjectAssign;
|
|
|
|
|
|
|
|
llvm::Type *args[] = { Int8PtrTy, Int8PtrTy, Int32Ty };
|
|
|
|
llvm::FunctionType *fty
|
|
|
|
= llvm::FunctionType::get(VoidTy, args, false);
|
|
|
|
BlockObjectAssign = CreateRuntimeFunction(fty, "_Block_object_assign");
|
|
|
|
configureBlocksRuntimeObject(*this, BlockObjectAssign);
|
|
|
|
return BlockObjectAssign;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Constant *CodeGenModule::getNSConcreteGlobalBlock() {
|
|
|
|
if (NSConcreteGlobalBlock)
|
|
|
|
return NSConcreteGlobalBlock;
|
|
|
|
|
|
|
|
NSConcreteGlobalBlock = GetOrCreateLLVMGlobal("_NSConcreteGlobalBlock",
|
|
|
|
Int8PtrTy->getPointerTo(), 0);
|
|
|
|
configureBlocksRuntimeObject(*this, NSConcreteGlobalBlock);
|
|
|
|
return NSConcreteGlobalBlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Constant *CodeGenModule::getNSConcreteStackBlock() {
|
|
|
|
if (NSConcreteStackBlock)
|
|
|
|
return NSConcreteStackBlock;
|
|
|
|
|
|
|
|
NSConcreteStackBlock = GetOrCreateLLVMGlobal("_NSConcreteStackBlock",
|
|
|
|
Int8PtrTy->getPointerTo(), 0);
|
|
|
|
configureBlocksRuntimeObject(*this, NSConcreteStackBlock);
|
|
|
|
return NSConcreteStackBlock;
|
|
|
|
}
|