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