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