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 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 62 | llvm::Type *ulong = CGM.getTypes().ConvertType(C.UnsignedLongTy); |
| 63 | llvm::Type *i8p = CGM.getTypes().ConvertType(C.VoidPtrTy); |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 64 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 65 | 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' |
Jay Foad | ef6de3d | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 167 | 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, |
Jay Foad | ef6de3d | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 171 | llvm::Type *type) |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 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, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 246 | SmallVectorImpl<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()); |
Jay Foad | ef6de3d | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 266 | llvm::Type *i8p = CGM.getTypes().ConvertType(C.VoidPtrTy); |
| 267 | llvm::Type *intTy = CGM.getTypes().ConvertType(C.IntTy); |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 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 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 283 | SmallVector<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. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 294 | SmallVector<BlockLayoutChunk, 16> layout; |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 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 | |
Jay Foad | ef6de3d | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 311 | llvm::Type *llvmType = CGM.getTypes().ConvertType(thisType); |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 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 | |
Jay Foad | ef6de3d | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 331 | llvm::Type *llvmType = CGM.getTypes().ConvertType(byRefPtrTy); |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 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 | |
Fariborz Jahanian | c637d73 | 2011-11-02 22:53:43 +0000 | [diff] [blame] | 383 | QualType VT = variable->getType(); |
Fariborz Jahanian | d8c4551 | 2011-10-31 23:44:33 +0000 | [diff] [blame] | 384 | CharUnits size = C.getTypeSizeInChars(VT); |
Fariborz Jahanian | c637d73 | 2011-11-02 22:53:43 +0000 | [diff] [blame] | 385 | CharUnits align = C.getDeclAlign(variable); |
Fariborz Jahanian | d8c4551 | 2011-10-31 23:44:33 +0000 | [diff] [blame] | 386 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 387 | maxFieldAlign = std::max(maxFieldAlign, align); |
| 388 | |
Jay Foad | ef6de3d | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 389 | llvm::Type *llvmType = |
Fariborz Jahanian | d8c4551 | 2011-10-31 23:44:33 +0000 | [diff] [blame] | 390 | CGM.getTypes().ConvertTypeForMem(VT); |
| 391 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 392 | layout.push_back(BlockLayoutChunk(align, size, &*ci, llvmType)); |
| 393 | } |
| 394 | |
| 395 | // If that was everything, we're done here. |
| 396 | if (layout.empty()) { |
| 397 | info.StructureType = |
| 398 | llvm::StructType::get(CGM.getLLVMContext(), elementTypes, true); |
| 399 | info.CanBeGlobal = true; |
| 400 | return; |
| 401 | } |
| 402 | |
| 403 | // Sort the layout by alignment. We have to use a stable sort here |
| 404 | // to get reproducible results. There should probably be an |
| 405 | // llvm::array_pod_stable_sort. |
| 406 | std::stable_sort(layout.begin(), layout.end()); |
| 407 | |
| 408 | CharUnits &blockSize = info.BlockSize; |
| 409 | info.BlockAlign = std::max(maxFieldAlign, info.BlockAlign); |
| 410 | |
| 411 | // Assuming that the first byte in the header is maximally aligned, |
| 412 | // get the alignment of the first byte following the header. |
| 413 | CharUnits endAlign = getLowBit(blockSize); |
| 414 | |
| 415 | // If the end of the header isn't satisfactorily aligned for the |
| 416 | // maximum thing, look for things that are okay with the header-end |
| 417 | // alignment, and keep appending them until we get something that's |
| 418 | // aligned right. This algorithm is only guaranteed optimal if |
| 419 | // that condition is satisfied at some point; otherwise we can get |
| 420 | // things like: |
| 421 | // header // next byte has alignment 4 |
| 422 | // something_with_size_5; // next byte has alignment 1 |
| 423 | // something_with_alignment_8; |
| 424 | // which has 7 bytes of padding, as opposed to the naive solution |
| 425 | // which might have less (?). |
| 426 | if (endAlign < maxFieldAlign) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 427 | SmallVectorImpl<BlockLayoutChunk>::iterator |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 428 | li = layout.begin() + 1, le = layout.end(); |
| 429 | |
| 430 | // Look for something that the header end is already |
| 431 | // satisfactorily aligned for. |
| 432 | for (; li != le && endAlign < li->Alignment; ++li) |
| 433 | ; |
| 434 | |
| 435 | // If we found something that's naturally aligned for the end of |
| 436 | // the header, keep adding things... |
| 437 | if (li != le) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 438 | SmallVectorImpl<BlockLayoutChunk>::iterator first = li; |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 439 | for (; li != le; ++li) { |
| 440 | assert(endAlign >= li->Alignment); |
| 441 | |
| 442 | li->setIndex(info, elementTypes.size()); |
| 443 | elementTypes.push_back(li->Type); |
| 444 | blockSize += li->Size; |
| 445 | endAlign = getLowBit(blockSize); |
| 446 | |
| 447 | // ...until we get to the alignment of the maximum field. |
| 448 | if (endAlign >= maxFieldAlign) |
| 449 | break; |
| 450 | } |
| 451 | |
| 452 | // Don't re-append everything we just appended. |
| 453 | layout.erase(first, li); |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | // At this point, we just have to add padding if the end align still |
| 458 | // isn't aligned right. |
| 459 | if (endAlign < maxFieldAlign) { |
| 460 | CharUnits padding = maxFieldAlign - endAlign; |
| 461 | |
John McCall | 5936e33 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 462 | elementTypes.push_back(llvm::ArrayType::get(CGM.Int8Ty, |
| 463 | padding.getQuantity())); |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 464 | blockSize += padding; |
| 465 | |
| 466 | endAlign = getLowBit(blockSize); |
| 467 | assert(endAlign >= maxFieldAlign); |
| 468 | } |
| 469 | |
| 470 | // Slam everything else on now. This works because they have |
| 471 | // strictly decreasing alignment and we expect that size is always a |
| 472 | // multiple of alignment. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 473 | for (SmallVectorImpl<BlockLayoutChunk>::iterator |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 474 | li = layout.begin(), le = layout.end(); li != le; ++li) { |
| 475 | assert(endAlign >= li->Alignment); |
| 476 | li->setIndex(info, elementTypes.size()); |
| 477 | elementTypes.push_back(li->Type); |
| 478 | blockSize += li->Size; |
| 479 | endAlign = getLowBit(blockSize); |
| 480 | } |
| 481 | |
| 482 | info.StructureType = |
| 483 | llvm::StructType::get(CGM.getLLVMContext(), elementTypes, true); |
| 484 | } |
| 485 | |
| 486 | /// Emit a block literal expression in the current function. |
| 487 | llvm::Value *CodeGenFunction::EmitBlockLiteral(const BlockExpr *blockExpr) { |
| 488 | std::string Name = CurFn->getName(); |
| 489 | CGBlockInfo blockInfo(blockExpr, Name.c_str()); |
| 490 | |
| 491 | // Compute information about the layout, etc., of this block. |
| 492 | computeBlockInfo(CGM, blockInfo); |
| 493 | |
| 494 | // Using that metadata, generate the actual block function. |
| 495 | llvm::Constant *blockFn |
| 496 | = CodeGenFunction(CGM).GenerateBlockFunction(CurGD, blockInfo, |
| 497 | CurFuncDecl, LocalDeclMap); |
John McCall | 5936e33 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 498 | blockFn = llvm::ConstantExpr::getBitCast(blockFn, VoidPtrTy); |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 499 | |
| 500 | // If there is nothing to capture, we can emit this as a global block. |
| 501 | if (blockInfo.CanBeGlobal) |
| 502 | return buildGlobalBlock(CGM, blockInfo, blockFn); |
| 503 | |
| 504 | // Otherwise, we have to emit this as a local block. |
| 505 | |
| 506 | llvm::Constant *isa = CGM.getNSConcreteStackBlock(); |
John McCall | 5936e33 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 507 | isa = llvm::ConstantExpr::getBitCast(isa, VoidPtrTy); |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 508 | |
| 509 | // Build the block descriptor. |
| 510 | llvm::Constant *descriptor = buildBlockDescriptor(CGM, blockInfo); |
| 511 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 512 | llvm::Type *intTy = ConvertType(getContext().IntTy); |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 513 | |
| 514 | llvm::AllocaInst *blockAddr = |
| 515 | CreateTempAlloca(blockInfo.StructureType, "block"); |
| 516 | blockAddr->setAlignment(blockInfo.BlockAlign.getQuantity()); |
| 517 | |
| 518 | // Compute the initial on-stack block flags. |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 519 | BlockFlags flags = BLOCK_HAS_SIGNATURE; |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 520 | if (blockInfo.NeedsCopyDispose) flags |= BLOCK_HAS_COPY_DISPOSE; |
| 521 | if (blockInfo.HasCXXObject) flags |= BLOCK_HAS_CXX_OBJ; |
John McCall | 64cd232 | 2011-03-09 08:39:33 +0000 | [diff] [blame] | 522 | if (blockInfo.UsesStret) flags |= BLOCK_USE_STRET; |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 523 | |
| 524 | // Initialize the block literal. |
| 525 | Builder.CreateStore(isa, Builder.CreateStructGEP(blockAddr, 0, "block.isa")); |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 526 | Builder.CreateStore(llvm::ConstantInt::get(intTy, flags.getBitMask()), |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 527 | Builder.CreateStructGEP(blockAddr, 1, "block.flags")); |
| 528 | Builder.CreateStore(llvm::ConstantInt::get(intTy, 0), |
| 529 | Builder.CreateStructGEP(blockAddr, 2, "block.reserved")); |
| 530 | Builder.CreateStore(blockFn, Builder.CreateStructGEP(blockAddr, 3, |
| 531 | "block.invoke")); |
| 532 | Builder.CreateStore(descriptor, Builder.CreateStructGEP(blockAddr, 4, |
| 533 | "block.descriptor")); |
| 534 | |
| 535 | // Finally, capture all the values into the block. |
| 536 | const BlockDecl *blockDecl = blockInfo.getBlockDecl(); |
| 537 | |
| 538 | // First, 'this'. |
| 539 | if (blockDecl->capturesCXXThis()) { |
| 540 | llvm::Value *addr = Builder.CreateStructGEP(blockAddr, |
| 541 | blockInfo.CXXThisIndex, |
| 542 | "block.captured-this.addr"); |
| 543 | Builder.CreateStore(LoadCXXThis(), addr); |
| 544 | } |
| 545 | |
| 546 | // Next, captured variables. |
| 547 | for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(), |
| 548 | ce = blockDecl->capture_end(); ci != ce; ++ci) { |
| 549 | const VarDecl *variable = ci->getVariable(); |
| 550 | const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable); |
| 551 | |
| 552 | // Ignore constant captures. |
| 553 | if (capture.isConstant()) continue; |
| 554 | |
| 555 | QualType type = variable->getType(); |
| 556 | |
| 557 | // This will be a [[type]]*, except that a byref entry will just be |
| 558 | // an i8**. |
| 559 | llvm::Value *blockField = |
| 560 | Builder.CreateStructGEP(blockAddr, capture.getIndex(), |
| 561 | "block.captured"); |
| 562 | |
| 563 | // Compute the address of the thing we're going to move into the |
| 564 | // block literal. |
| 565 | llvm::Value *src; |
| 566 | if (ci->isNested()) { |
| 567 | // We need to use the capture from the enclosing block. |
| 568 | const CGBlockInfo::Capture &enclosingCapture = |
| 569 | BlockInfo->getCapture(variable); |
| 570 | |
| 571 | // This is a [[type]]*, except that a byref entry wil just be an i8**. |
| 572 | src = Builder.CreateStructGEP(LoadBlockStruct(), |
| 573 | enclosingCapture.getIndex(), |
| 574 | "block.capture.addr"); |
| 575 | } else { |
| 576 | // This is a [[type]]*. |
| 577 | src = LocalDeclMap[variable]; |
| 578 | } |
| 579 | |
| 580 | // For byrefs, we just write the pointer to the byref struct into |
| 581 | // the block field. There's no need to chase the forwarding |
| 582 | // pointer at this point, since we're building something that will |
| 583 | // live a shorter life than the stack byref anyway. |
| 584 | if (ci->isByRef()) { |
John McCall | 5936e33 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 585 | // Get a void* that points to the byref struct. |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 586 | if (ci->isNested()) |
| 587 | src = Builder.CreateLoad(src, "byref.capture"); |
| 588 | else |
John McCall | 5936e33 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 589 | src = Builder.CreateBitCast(src, VoidPtrTy); |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 590 | |
John McCall | 5936e33 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 591 | // Write that void* into the capture field. |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 592 | Builder.CreateStore(src, blockField); |
| 593 | |
| 594 | // If we have a copy constructor, evaluate that into the block field. |
| 595 | } else if (const Expr *copyExpr = ci->getCopyExpr()) { |
| 596 | EmitSynthesizedCXXCopyCtor(blockField, src, copyExpr); |
| 597 | |
| 598 | // If it's a reference variable, copy the reference into the block field. |
Fariborz Jahanian | c637d73 | 2011-11-02 22:53:43 +0000 | [diff] [blame] | 599 | } else if (type->isReferenceType()) { |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 600 | Builder.CreateStore(Builder.CreateLoad(src, "ref.val"), blockField); |
| 601 | |
| 602 | // Otherwise, fake up a POD copy into the block field. |
| 603 | } else { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 604 | // Fake up a new variable so that EmitScalarInit doesn't think |
| 605 | // we're referring to the variable in its own initializer. |
| 606 | ImplicitParamDecl blockFieldPseudoVar(/*DC*/ 0, SourceLocation(), |
Fariborz Jahanian | c637d73 | 2011-11-02 22:53:43 +0000 | [diff] [blame] | 607 | /*name*/ 0, type); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 608 | |
John McCall | bb699b0 | 2011-02-07 18:37:40 +0000 | [diff] [blame] | 609 | // We use one of these or the other depending on whether the |
| 610 | // reference is nested. |
Fariborz Jahanian | c637d73 | 2011-11-02 22:53:43 +0000 | [diff] [blame] | 611 | DeclRefExpr notNested(const_cast<VarDecl*>(variable), type, VK_LValue, |
John McCall | bb699b0 | 2011-02-07 18:37:40 +0000 | [diff] [blame] | 612 | SourceLocation()); |
Fariborz Jahanian | c637d73 | 2011-11-02 22:53:43 +0000 | [diff] [blame] | 613 | BlockDeclRefExpr nested(const_cast<VarDecl*>(variable), type, |
John McCall | bb699b0 | 2011-02-07 18:37:40 +0000 | [diff] [blame] | 614 | VK_LValue, SourceLocation(), /*byref*/ false); |
| 615 | |
| 616 | Expr *declRef = |
| 617 | (ci->isNested() ? static_cast<Expr*>(&nested) : ¬Nested); |
| 618 | |
Fariborz Jahanian | c637d73 | 2011-11-02 22:53:43 +0000 | [diff] [blame] | 619 | ImplicitCastExpr l2r(ImplicitCastExpr::OnStack, type, CK_LValueToRValue, |
John McCall | bb699b0 | 2011-02-07 18:37:40 +0000 | [diff] [blame] | 620 | declRef, VK_RValue); |
John McCall | a07398e | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 621 | EmitExprAsInit(&l2r, &blockFieldPseudoVar, |
Fariborz Jahanian | c637d73 | 2011-11-02 22:53:43 +0000 | [diff] [blame] | 622 | MakeAddrLValue(blockField, type, |
Eli Friedman | 225bf77 | 2011-09-30 18:19:16 +0000 | [diff] [blame] | 623 | getContext().getDeclAlign(variable) |
| 624 | .getQuantity()), |
John McCall | df04520 | 2011-03-08 09:38:48 +0000 | [diff] [blame] | 625 | /*captured by init*/ false); |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | // Push a destructor if necessary. The semantics for when this |
| 629 | // actually gets run are really obscure. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 630 | if (!ci->isByRef()) { |
John McCall | 9928c48 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 631 | switch (QualType::DestructionKind dtorKind = type.isDestructedType()) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 632 | case QualType::DK_none: |
| 633 | break; |
John McCall | 9928c48 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 634 | |
| 635 | // Block captures count as local values and have imprecise semantics. |
| 636 | // They also can't be arrays, so need to worry about that. |
John McCall | 5bcd95e | 2011-07-12 16:53:04 +0000 | [diff] [blame] | 637 | case QualType::DK_objc_strong_lifetime: { |
| 638 | // This local is a GCC and MSVC compiler workaround. |
| 639 | Destroyer *destroyer = &destroyARCStrongImprecise; |
John McCall | 9928c48 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 640 | pushDestroy(getCleanupKind(dtorKind), blockField, type, |
John McCall | 5bcd95e | 2011-07-12 16:53:04 +0000 | [diff] [blame] | 641 | *destroyer, /*useEHCleanupForArray*/ false); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 642 | break; |
John McCall | 5bcd95e | 2011-07-12 16:53:04 +0000 | [diff] [blame] | 643 | } |
John McCall | 9928c48 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 644 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 645 | case QualType::DK_objc_weak_lifetime: |
John McCall | 9928c48 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 646 | case QualType::DK_cxx_destructor: |
| 647 | pushDestroy(dtorKind, blockField, type); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 648 | break; |
| 649 | } |
| 650 | } |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | // Cast to the converted block-pointer type, which happens (somewhat |
| 654 | // unfortunately) to be a pointer to function type. |
| 655 | llvm::Value *result = |
| 656 | Builder.CreateBitCast(blockAddr, |
| 657 | ConvertType(blockInfo.getBlockExpr()->getType())); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 658 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 659 | return result; |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 663 | llvm::Type *CodeGenModule::getBlockDescriptorType() { |
Mike Stump | ab69514 | 2009-02-13 15:16:56 +0000 | [diff] [blame] | 664 | if (BlockDescriptorType) |
| 665 | return BlockDescriptorType; |
| 666 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 667 | llvm::Type *UnsignedLongTy = |
Mike Stump | ab69514 | 2009-02-13 15:16:56 +0000 | [diff] [blame] | 668 | getTypes().ConvertType(getContext().UnsignedLongTy); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 669 | |
Mike Stump | ab69514 | 2009-02-13 15:16:56 +0000 | [diff] [blame] | 670 | // struct __block_descriptor { |
| 671 | // unsigned long reserved; |
| 672 | // unsigned long block_size; |
Blaine Garst | 2a7eb28 | 2010-02-23 21:51:17 +0000 | [diff] [blame] | 673 | // |
| 674 | // // later, the following will be added |
| 675 | // |
| 676 | // struct { |
| 677 | // void (*copyHelper)(); |
| 678 | // void (*copyHelper)(); |
| 679 | // } helpers; // !!! optional |
| 680 | // |
| 681 | // const char *signature; // the block signature |
| 682 | // const char *layout; // reserved |
Mike Stump | ab69514 | 2009-02-13 15:16:56 +0000 | [diff] [blame] | 683 | // }; |
Chris Lattner | 7650d95 | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 684 | BlockDescriptorType = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 685 | llvm::StructType::create("struct.__block_descriptor", |
| 686 | UnsignedLongTy, UnsignedLongTy, NULL); |
Mike Stump | ab69514 | 2009-02-13 15:16:56 +0000 | [diff] [blame] | 687 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 688 | // Now form a pointer to that. |
| 689 | BlockDescriptorType = llvm::PointerType::getUnqual(BlockDescriptorType); |
Mike Stump | ab69514 | 2009-02-13 15:16:56 +0000 | [diff] [blame] | 690 | return BlockDescriptorType; |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 691 | } |
| 692 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 693 | llvm::Type *CodeGenModule::getGenericBlockLiteralType() { |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 694 | if (GenericBlockLiteralType) |
| 695 | return GenericBlockLiteralType; |
| 696 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 697 | llvm::Type *BlockDescPtrTy = getBlockDescriptorType(); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 698 | |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 699 | // struct __block_literal_generic { |
Mike Stump | bd65cac | 2009-02-19 01:01:04 +0000 | [diff] [blame] | 700 | // void *__isa; |
| 701 | // int __flags; |
| 702 | // int __reserved; |
| 703 | // void (*__invoke)(void *); |
| 704 | // struct __block_descriptor *__descriptor; |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 705 | // }; |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 706 | GenericBlockLiteralType = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 707 | llvm::StructType::create("struct.__block_literal_generic", |
| 708 | VoidPtrTy, IntTy, IntTy, VoidPtrTy, |
| 709 | BlockDescPtrTy, NULL); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 710 | |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 711 | return GenericBlockLiteralType; |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 712 | } |
| 713 | |
Mike Stump | bd65cac | 2009-02-19 01:01:04 +0000 | [diff] [blame] | 714 | |
Anders Carlsson | a1736c0 | 2009-12-24 21:13:40 +0000 | [diff] [blame] | 715 | RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E, |
| 716 | ReturnValueSlot ReturnValue) { |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 717 | const BlockPointerType *BPT = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 718 | E->getCallee()->getType()->getAs<BlockPointerType>(); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 719 | |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 720 | llvm::Value *Callee = EmitScalarExpr(E->getCallee()); |
| 721 | |
| 722 | // Get a pointer to the generic block literal. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 723 | llvm::Type *BlockLiteralTy = |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 724 | llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType()); |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 725 | |
| 726 | // Bitcast the callee to a block literal. |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 727 | llvm::Value *BlockLiteral = |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 728 | Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal"); |
| 729 | |
| 730 | // Get the function pointer from the literal. |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 731 | llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3); |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 732 | |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 733 | BlockLiteral = Builder.CreateBitCast(BlockLiteral, VoidPtrTy); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 734 | |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 735 | // Add the block literal. |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 736 | CallArgList Args; |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 737 | Args.add(RValue::get(BlockLiteral), getContext().VoidPtrTy); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 738 | |
Anders Carlsson | 782f397 | 2009-04-08 23:13:16 +0000 | [diff] [blame] | 739 | QualType FnType = BPT->getPointeeType(); |
| 740 | |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 741 | // And the rest of the arguments. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 742 | EmitCallArgs(Args, FnType->getAs<FunctionProtoType>(), |
Anders Carlsson | 782f397 | 2009-04-08 23:13:16 +0000 | [diff] [blame] | 743 | E->arg_begin(), E->arg_end()); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 744 | |
Anders Carlsson | 6e460ff | 2009-04-07 22:10:22 +0000 | [diff] [blame] | 745 | // Load the function. |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 746 | llvm::Value *Func = Builder.CreateLoad(FuncPtr); |
Anders Carlsson | 6e460ff | 2009-04-07 22:10:22 +0000 | [diff] [blame] | 747 | |
John McCall | 64cd232 | 2011-03-09 08:39:33 +0000 | [diff] [blame] | 748 | const FunctionType *FuncTy = FnType->castAs<FunctionType>(); |
Eli Friedman | c55db3b | 2011-08-09 17:38:12 +0000 | [diff] [blame] | 749 | const CGFunctionInfo &FnInfo = CGM.getTypes().getFunctionInfo(Args, FuncTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 750 | |
Anders Carlsson | 6e460ff | 2009-04-07 22:10:22 +0000 | [diff] [blame] | 751 | // Cast the function pointer to the right type. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 752 | llvm::Type *BlockFTy = |
Anders Carlsson | a17d7cc | 2009-04-08 02:55:55 +0000 | [diff] [blame] | 753 | CGM.getTypes().GetFunctionType(FnInfo, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 754 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 755 | llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy); |
Anders Carlsson | 6e460ff | 2009-04-07 22:10:22 +0000 | [diff] [blame] | 756 | Func = Builder.CreateBitCast(Func, BlockFTyPtr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 757 | |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 758 | // And call the block. |
Anders Carlsson | a1736c0 | 2009-12-24 21:13:40 +0000 | [diff] [blame] | 759 | return EmitCall(FnInfo, Func, ReturnValue, Args); |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 760 | } |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 761 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 762 | llvm::Value *CodeGenFunction::GetAddrOfBlockDecl(const VarDecl *variable, |
| 763 | bool isByRef) { |
| 764 | assert(BlockInfo && "evaluating block ref without block information?"); |
| 765 | const CGBlockInfo::Capture &capture = BlockInfo->getCapture(variable); |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 766 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 767 | // Handle constant captures. |
| 768 | if (capture.isConstant()) return LocalDeclMap[variable]; |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 769 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 770 | llvm::Value *addr = |
| 771 | Builder.CreateStructGEP(LoadBlockStruct(), capture.getIndex(), |
| 772 | "block.capture.addr"); |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 773 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 774 | if (isByRef) { |
| 775 | // addr should be a void** right now. Load, then cast the result |
| 776 | // to byref*. |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 777 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 778 | addr = Builder.CreateLoad(addr); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 779 | llvm::PointerType *byrefPointerType |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 780 | = llvm::PointerType::get(BuildByRefType(variable), 0); |
| 781 | addr = Builder.CreateBitCast(addr, byrefPointerType, |
| 782 | "byref.addr"); |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 783 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 784 | // Follow the forwarding pointer. |
| 785 | addr = Builder.CreateStructGEP(addr, 1, "byref.forwarding"); |
| 786 | addr = Builder.CreateLoad(addr, "byref.addr.forwarded"); |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 787 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 788 | // Cast back to byref* and GEP over to the actual object. |
| 789 | addr = Builder.CreateBitCast(addr, byrefPointerType); |
| 790 | addr = Builder.CreateStructGEP(addr, getByRefValueLLVMField(variable), |
| 791 | variable->getNameAsString()); |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 792 | } |
| 793 | |
Fariborz Jahanian | c637d73 | 2011-11-02 22:53:43 +0000 | [diff] [blame] | 794 | if (variable->getType()->isReferenceType()) |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 795 | addr = Builder.CreateLoad(addr, "ref.tmp"); |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 796 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 797 | return addr; |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 798 | } |
| 799 | |
Mike Stump | 67a6448 | 2009-02-14 22:16:35 +0000 | [diff] [blame] | 800 | llvm::Constant * |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 801 | CodeGenModule::GetAddrOfGlobalBlock(const BlockExpr *blockExpr, |
John McCall | 5936e33 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 802 | const char *name) { |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 803 | CGBlockInfo blockInfo(blockExpr, name); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 804 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 805 | // Compute information about the layout, etc., of this block. |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 806 | computeBlockInfo(*this, blockInfo); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 807 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 808 | // Using that metadata, generate the actual block function. |
| 809 | llvm::Constant *blockFn; |
| 810 | { |
| 811 | llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap; |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 812 | blockFn = CodeGenFunction(*this).GenerateBlockFunction(GlobalDecl(), |
| 813 | blockInfo, |
| 814 | 0, LocalDeclMap); |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 815 | } |
John McCall | 5936e33 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 816 | blockFn = llvm::ConstantExpr::getBitCast(blockFn, VoidPtrTy); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 817 | |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 818 | return buildGlobalBlock(*this, blockInfo, blockFn); |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 819 | } |
| 820 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 821 | static llvm::Constant *buildGlobalBlock(CodeGenModule &CGM, |
| 822 | const CGBlockInfo &blockInfo, |
| 823 | llvm::Constant *blockFn) { |
| 824 | assert(blockInfo.CanBeGlobal); |
| 825 | |
| 826 | // Generate the constants for the block literal initializer. |
| 827 | llvm::Constant *fields[BlockHeaderSize]; |
| 828 | |
| 829 | // isa |
| 830 | fields[0] = CGM.getNSConcreteGlobalBlock(); |
| 831 | |
| 832 | // __flags |
John McCall | 64cd232 | 2011-03-09 08:39:33 +0000 | [diff] [blame] | 833 | BlockFlags flags = BLOCK_IS_GLOBAL | BLOCK_HAS_SIGNATURE; |
| 834 | if (blockInfo.UsesStret) flags |= BLOCK_USE_STRET; |
| 835 | |
John McCall | 5936e33 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 836 | fields[1] = llvm::ConstantInt::get(CGM.IntTy, flags.getBitMask()); |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 837 | |
| 838 | // Reserved |
John McCall | 5936e33 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 839 | fields[2] = llvm::Constant::getNullValue(CGM.IntTy); |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 840 | |
| 841 | // Function |
| 842 | fields[3] = blockFn; |
| 843 | |
| 844 | // Descriptor |
| 845 | fields[4] = buildBlockDescriptor(CGM, blockInfo); |
| 846 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 847 | llvm::Constant *init = llvm::ConstantStruct::getAnon(fields); |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 848 | |
| 849 | llvm::GlobalVariable *literal = |
| 850 | new llvm::GlobalVariable(CGM.getModule(), |
| 851 | init->getType(), |
| 852 | /*constant*/ true, |
| 853 | llvm::GlobalVariable::InternalLinkage, |
| 854 | init, |
| 855 | "__block_literal_global"); |
| 856 | literal->setAlignment(blockInfo.BlockAlign.getQuantity()); |
| 857 | |
| 858 | // Return a constant of the appropriately-casted type. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 859 | llvm::Type *requiredType = |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 860 | CGM.getTypes().ConvertType(blockInfo.getBlockExpr()->getType()); |
| 861 | return llvm::ConstantExpr::getBitCast(literal, requiredType); |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 862 | } |
| 863 | |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 864 | llvm::Function * |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 865 | CodeGenFunction::GenerateBlockFunction(GlobalDecl GD, |
| 866 | const CGBlockInfo &blockInfo, |
| 867 | const Decl *outerFnDecl, |
| 868 | const DeclMapTy &ldm) { |
| 869 | const BlockDecl *blockDecl = blockInfo.getBlockDecl(); |
Devang Patel | 963dfbd | 2009-04-15 21:51:44 +0000 | [diff] [blame] | 870 | |
Devang Patel | 6d1155b | 2011-03-07 21:53:18 +0000 | [diff] [blame] | 871 | // Check if we should generate debug info for this block function. |
| 872 | if (CGM.getModuleDebugInfo()) |
| 873 | DebugInfo = CGM.getModuleDebugInfo(); |
| 874 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 875 | BlockInfo = &blockInfo; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 876 | |
Mike Stump | 7f28a9c | 2009-03-13 23:34:28 +0000 | [diff] [blame] | 877 | // Arrange for local static and local extern declarations to appear |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 878 | // to be local to this function as well, in case they're directly |
| 879 | // referenced in a block. |
| 880 | for (DeclMapTy::const_iterator i = ldm.begin(), e = ldm.end(); i != e; ++i) { |
| 881 | const VarDecl *var = dyn_cast<VarDecl>(i->first); |
| 882 | if (var && !var->hasLocalStorage()) |
| 883 | LocalDeclMap[var] = i->second; |
Mike Stump | 7f28a9c | 2009-03-13 23:34:28 +0000 | [diff] [blame] | 884 | } |
| 885 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 886 | // Begin building the function declaration. |
Eli Friedman | 48f9122 | 2009-03-28 03:24:54 +0000 | [diff] [blame] | 887 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 888 | // Build the argument list. |
| 889 | FunctionArgList args; |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 890 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 891 | // The first argument is the block pointer. Just take it as a void* |
| 892 | // and cast it later. |
| 893 | QualType selfTy = getContext().VoidPtrTy; |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 894 | IdentifierInfo *II = &CGM.getContext().Idents.get(".block_descriptor"); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 895 | |
John McCall | 8178df3 | 2011-02-22 22:38:33 +0000 | [diff] [blame] | 896 | ImplicitParamDecl selfDecl(const_cast<BlockDecl*>(blockDecl), |
| 897 | SourceLocation(), II, selfTy); |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 898 | args.push_back(&selfDecl); |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 899 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 900 | // Now add the rest of the parameters. |
| 901 | for (BlockDecl::param_const_iterator i = blockDecl->param_begin(), |
| 902 | e = blockDecl->param_end(); i != e; ++i) |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 903 | args.push_back(*i); |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 904 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 905 | // Create the function declaration. |
| 906 | const FunctionProtoType *fnType = |
| 907 | cast<FunctionProtoType>(blockInfo.getBlockExpr()->getFunctionType()); |
| 908 | const CGFunctionInfo &fnInfo = |
| 909 | CGM.getTypes().getFunctionInfo(fnType->getResultType(), args, |
| 910 | fnType->getExtInfo()); |
John McCall | 64cd232 | 2011-03-09 08:39:33 +0000 | [diff] [blame] | 911 | if (CGM.ReturnTypeUsesSRet(fnInfo)) |
| 912 | blockInfo.UsesStret = true; |
| 913 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 914 | llvm::FunctionType *fnLLVMType = |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 915 | CGM.getTypes().GetFunctionType(fnInfo, fnType->isVariadic()); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 916 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 917 | MangleBuffer name; |
| 918 | CGM.getBlockMangledName(GD, name, blockDecl); |
| 919 | llvm::Function *fn = |
| 920 | llvm::Function::Create(fnLLVMType, llvm::GlobalValue::InternalLinkage, |
| 921 | name.getString(), &CGM.getModule()); |
| 922 | CGM.SetInternalFunctionAttributes(blockDecl, fn, fnInfo); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 923 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 924 | // Begin generating the function. |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 925 | StartFunction(blockDecl, fnType->getResultType(), fn, fnInfo, args, |
Devang Patel | 3f4cb25 | 2011-03-25 21:26:13 +0000 | [diff] [blame] | 926 | blockInfo.getBlockExpr()->getBody()->getLocStart()); |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 927 | CurFuncDecl = outerFnDecl; // StartFunction sets this to blockDecl |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 928 | |
John McCall | 8178df3 | 2011-02-22 22:38:33 +0000 | [diff] [blame] | 929 | // Okay. Undo some of what StartFunction did. |
| 930 | |
| 931 | // Pull the 'self' reference out of the local decl map. |
| 932 | llvm::Value *blockAddr = LocalDeclMap[&selfDecl]; |
| 933 | LocalDeclMap.erase(&selfDecl); |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 934 | BlockPointer = Builder.CreateBitCast(blockAddr, |
| 935 | blockInfo.StructureType->getPointerTo(), |
| 936 | "block"); |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 937 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 938 | // If we have a C++ 'this' reference, go ahead and force it into |
| 939 | // existence now. |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 940 | if (blockDecl->capturesCXXThis()) { |
| 941 | llvm::Value *addr = Builder.CreateStructGEP(BlockPointer, |
| 942 | blockInfo.CXXThisIndex, |
| 943 | "block.captured-this"); |
| 944 | CXXThisValue = Builder.CreateLoad(addr, "this"); |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 945 | } |
| 946 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 947 | // LoadObjCSelf() expects there to be an entry for 'self' in LocalDeclMap; |
| 948 | // appease it. |
| 949 | if (const ObjCMethodDecl *method |
| 950 | = dyn_cast_or_null<ObjCMethodDecl>(CurFuncDecl)) { |
| 951 | const VarDecl *self = method->getSelfDecl(); |
| 952 | |
| 953 | // There might not be a capture for 'self', but if there is... |
| 954 | if (blockInfo.Captures.count(self)) { |
| 955 | const CGBlockInfo::Capture &capture = blockInfo.getCapture(self); |
| 956 | llvm::Value *selfAddr = Builder.CreateStructGEP(BlockPointer, |
| 957 | capture.getIndex(), |
| 958 | "block.captured-self"); |
| 959 | LocalDeclMap[self] = selfAddr; |
| 960 | } |
| 961 | } |
| 962 | |
| 963 | // Also force all the constant captures. |
| 964 | for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(), |
| 965 | ce = blockDecl->capture_end(); ci != ce; ++ci) { |
| 966 | const VarDecl *variable = ci->getVariable(); |
| 967 | const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable); |
| 968 | if (!capture.isConstant()) continue; |
| 969 | |
| 970 | unsigned align = getContext().getDeclAlign(variable).getQuantity(); |
| 971 | |
| 972 | llvm::AllocaInst *alloca = |
| 973 | CreateMemTemp(variable->getType(), "block.captured-const"); |
| 974 | alloca->setAlignment(align); |
| 975 | |
| 976 | Builder.CreateStore(capture.getConstant(), alloca, align); |
| 977 | |
| 978 | LocalDeclMap[variable] = alloca; |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 979 | } |
| 980 | |
Mike Stump | b289b3f | 2009-10-01 22:29:41 +0000 | [diff] [blame] | 981 | // Save a spot to insert the debug information for all the BlockDeclRefDecls. |
| 982 | llvm::BasicBlock *entry = Builder.GetInsertBlock(); |
| 983 | llvm::BasicBlock::iterator entry_ptr = Builder.GetInsertPoint(); |
| 984 | --entry_ptr; |
| 985 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 986 | EmitStmt(blockDecl->getBody()); |
Mike Stump | b289b3f | 2009-10-01 22:29:41 +0000 | [diff] [blame] | 987 | |
Mike Stump | de8c5c7 | 2009-10-01 00:27:30 +0000 | [diff] [blame] | 988 | // Remember where we were... |
| 989 | llvm::BasicBlock *resume = Builder.GetInsertBlock(); |
Mike Stump | b289b3f | 2009-10-01 22:29:41 +0000 | [diff] [blame] | 990 | |
Mike Stump | de8c5c7 | 2009-10-01 00:27:30 +0000 | [diff] [blame] | 991 | // Go back to the entry. |
Mike Stump | b289b3f | 2009-10-01 22:29:41 +0000 | [diff] [blame] | 992 | ++entry_ptr; |
| 993 | Builder.SetInsertPoint(entry, entry_ptr); |
| 994 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 995 | // Emit debug information for all the BlockDeclRefDecls. |
| 996 | // FIXME: also for 'this' |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 997 | if (CGDebugInfo *DI = getDebugInfo()) { |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 998 | for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(), |
| 999 | ce = blockDecl->capture_end(); ci != ce; ++ci) { |
| 1000 | const VarDecl *variable = ci->getVariable(); |
Eric Christopher | 73fb350 | 2011-10-13 21:45:18 +0000 | [diff] [blame] | 1001 | DI->EmitLocation(Builder, variable->getLocation()); |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1002 | |
| 1003 | const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable); |
| 1004 | if (capture.isConstant()) { |
| 1005 | DI->EmitDeclareOfAutoVariable(variable, LocalDeclMap[variable], |
| 1006 | Builder); |
| 1007 | continue; |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 1008 | } |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1009 | |
John McCall | 8178df3 | 2011-02-22 22:38:33 +0000 | [diff] [blame] | 1010 | DI->EmitDeclareOfBlockDeclRefVariable(variable, BlockPointer, |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1011 | Builder, blockInfo); |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 1012 | } |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 1013 | } |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1014 | |
Mike Stump | de8c5c7 | 2009-10-01 00:27:30 +0000 | [diff] [blame] | 1015 | // And resume where we left off. |
| 1016 | if (resume == 0) |
| 1017 | Builder.ClearInsertionPoint(); |
| 1018 | else |
| 1019 | Builder.SetInsertPoint(resume); |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 1020 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1021 | FinishFunction(cast<CompoundStmt>(blockDecl->getBody())->getRBracLoc()); |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 1022 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1023 | return fn; |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 1024 | } |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 1025 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1026 | /* |
| 1027 | notes.push_back(HelperInfo()); |
| 1028 | HelperInfo ¬e = notes.back(); |
| 1029 | note.index = capture.getIndex(); |
| 1030 | note.RequiresCopying = (ci->hasCopyExpr() || BlockRequiresCopying(type)); |
| 1031 | note.cxxbar_import = ci->getCopyExpr(); |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 1032 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1033 | if (ci->isByRef()) { |
| 1034 | note.flag = BLOCK_FIELD_IS_BYREF; |
| 1035 | if (type.isObjCGCWeak()) |
| 1036 | note.flag |= BLOCK_FIELD_IS_WEAK; |
| 1037 | } else if (type->isBlockPointerType()) { |
| 1038 | note.flag = BLOCK_FIELD_IS_BLOCK; |
| 1039 | } else { |
| 1040 | note.flag = BLOCK_FIELD_IS_OBJECT; |
| 1041 | } |
| 1042 | */ |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 1043 | |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 1044 | |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 1045 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1046 | llvm::Constant * |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1047 | CodeGenFunction::GenerateCopyHelperFunction(const CGBlockInfo &blockInfo) { |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1048 | ASTContext &C = getContext(); |
| 1049 | |
| 1050 | FunctionArgList args; |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 1051 | ImplicitParamDecl dstDecl(0, SourceLocation(), 0, C.VoidPtrTy); |
| 1052 | args.push_back(&dstDecl); |
| 1053 | ImplicitParamDecl srcDecl(0, SourceLocation(), 0, C.VoidPtrTy); |
| 1054 | args.push_back(&srcDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1055 | |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 1056 | const CGFunctionInfo &FI = |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1057 | CGM.getTypes().getFunctionInfo(C.VoidTy, args, FunctionType::ExtInfo()); |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 1058 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1059 | // FIXME: it would be nice if these were mergeable with things with |
| 1060 | // identical semantics. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1061 | llvm::FunctionType *LTy = CGM.getTypes().GetFunctionType(FI, false); |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 1062 | |
| 1063 | llvm::Function *Fn = |
| 1064 | llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, |
Benjamin Kramer | 3cf7c5d | 2010-01-22 13:59:13 +0000 | [diff] [blame] | 1065 | "__copy_helper_block_", &CGM.getModule()); |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 1066 | |
| 1067 | IdentifierInfo *II |
| 1068 | = &CGM.getContext().Idents.get("__copy_helper_block_"); |
| 1069 | |
Devang Patel | 58dc5ca | 2011-05-02 20:37:08 +0000 | [diff] [blame] | 1070 | // Check if we should generate debug info for this block helper function. |
| 1071 | if (CGM.getModuleDebugInfo()) |
| 1072 | DebugInfo = CGM.getModuleDebugInfo(); |
| 1073 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1074 | FunctionDecl *FD = FunctionDecl::Create(C, |
| 1075 | C.getTranslationUnitDecl(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1076 | SourceLocation(), |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1077 | SourceLocation(), II, C.VoidTy, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1078 | SC_Static, |
| 1079 | SC_None, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 1080 | false, |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 1081 | true); |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 1082 | StartFunction(FD, C.VoidTy, Fn, FI, args, SourceLocation()); |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 1083 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1084 | llvm::Type *structPtrTy = blockInfo.StructureType->getPointerTo(); |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 1085 | |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 1086 | llvm::Value *src = GetAddrOfLocalVar(&srcDecl); |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1087 | src = Builder.CreateLoad(src); |
| 1088 | src = Builder.CreateBitCast(src, structPtrTy, "block.source"); |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 1089 | |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 1090 | llvm::Value *dst = GetAddrOfLocalVar(&dstDecl); |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1091 | dst = Builder.CreateLoad(dst); |
| 1092 | dst = Builder.CreateBitCast(dst, structPtrTy, "block.dest"); |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 1093 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1094 | const BlockDecl *blockDecl = blockInfo.getBlockDecl(); |
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 | for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(), |
| 1097 | ce = blockDecl->capture_end(); ci != ce; ++ci) { |
| 1098 | const VarDecl *variable = ci->getVariable(); |
| 1099 | QualType type = variable->getType(); |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 1100 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1101 | const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable); |
| 1102 | if (capture.isConstant()) continue; |
| 1103 | |
| 1104 | const Expr *copyExpr = ci->getCopyExpr(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1105 | BlockFieldFlags flags; |
| 1106 | |
| 1107 | bool isARCWeakCapture = false; |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1108 | |
| 1109 | if (copyExpr) { |
| 1110 | assert(!ci->isByRef()); |
| 1111 | // don't bother computing flags |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1112 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1113 | } else if (ci->isByRef()) { |
| 1114 | flags = BLOCK_FIELD_IS_BYREF; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1115 | if (type.isObjCGCWeak()) |
| 1116 | flags |= BLOCK_FIELD_IS_WEAK; |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1117 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1118 | } else if (type->isObjCRetainableType()) { |
| 1119 | flags = BLOCK_FIELD_IS_OBJECT; |
| 1120 | if (type->isBlockPointerType()) |
| 1121 | flags = BLOCK_FIELD_IS_BLOCK; |
| 1122 | |
| 1123 | // Special rules for ARC captures: |
| 1124 | if (getLangOptions().ObjCAutoRefCount) { |
| 1125 | Qualifiers qs = type.getQualifiers(); |
| 1126 | |
| 1127 | // Don't generate special copy logic for a captured object |
| 1128 | // unless it's __strong or __weak. |
| 1129 | if (!qs.hasStrongOrWeakObjCLifetime()) |
| 1130 | continue; |
| 1131 | |
| 1132 | // Support __weak direct captures. |
| 1133 | if (qs.getObjCLifetime() == Qualifiers::OCL_Weak) |
| 1134 | isARCWeakCapture = true; |
| 1135 | } |
| 1136 | } else { |
| 1137 | continue; |
| 1138 | } |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1139 | |
| 1140 | unsigned index = capture.getIndex(); |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1141 | llvm::Value *srcField = Builder.CreateStructGEP(src, index); |
| 1142 | llvm::Value *dstField = Builder.CreateStructGEP(dst, index); |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1143 | |
| 1144 | // If there's an explicit copy expression, we do that. |
| 1145 | if (copyExpr) { |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1146 | EmitSynthesizedCXXCopyCtor(dstField, srcField, copyExpr); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1147 | } else if (isARCWeakCapture) { |
| 1148 | EmitARCCopyWeak(dstField, srcField); |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1149 | } else { |
| 1150 | llvm::Value *srcValue = Builder.CreateLoad(srcField, "blockcopy.src"); |
John McCall | 5936e33 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 1151 | srcValue = Builder.CreateBitCast(srcValue, VoidPtrTy); |
| 1152 | llvm::Value *dstAddr = Builder.CreateBitCast(dstField, VoidPtrTy); |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1153 | Builder.CreateCall3(CGM.getBlockObjectAssign(), dstAddr, srcValue, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1154 | llvm::ConstantInt::get(Int32Ty, flags.getBitMask())); |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 1155 | } |
| 1156 | } |
| 1157 | |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1158 | FinishFunction(); |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 1159 | |
John McCall | 5936e33 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 1160 | return llvm::ConstantExpr::getBitCast(Fn, VoidPtrTy); |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 1161 | } |
| 1162 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1163 | llvm::Constant * |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1164 | CodeGenFunction::GenerateDestroyHelperFunction(const CGBlockInfo &blockInfo) { |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1165 | ASTContext &C = getContext(); |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 1166 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1167 | FunctionArgList args; |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 1168 | ImplicitParamDecl srcDecl(0, SourceLocation(), 0, C.VoidPtrTy); |
| 1169 | args.push_back(&srcDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1170 | |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 1171 | const CGFunctionInfo &FI = |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1172 | CGM.getTypes().getFunctionInfo(C.VoidTy, args, FunctionType::ExtInfo()); |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 1173 | |
Mike Stump | 3899a7f | 2009-06-05 23:26:36 +0000 | [diff] [blame] | 1174 | // FIXME: We'd like to put these into a mergable by content, with |
| 1175 | // internal linkage. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1176 | llvm::FunctionType *LTy = CGM.getTypes().GetFunctionType(FI, false); |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 1177 | |
| 1178 | llvm::Function *Fn = |
| 1179 | llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, |
Benjamin Kramer | 3cf7c5d | 2010-01-22 13:59:13 +0000 | [diff] [blame] | 1180 | "__destroy_helper_block_", &CGM.getModule()); |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 1181 | |
Devang Patel | 58dc5ca | 2011-05-02 20:37:08 +0000 | [diff] [blame] | 1182 | // Check if we should generate debug info for this block destroy function. |
| 1183 | if (CGM.getModuleDebugInfo()) |
| 1184 | DebugInfo = CGM.getModuleDebugInfo(); |
| 1185 | |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 1186 | IdentifierInfo *II |
| 1187 | = &CGM.getContext().Idents.get("__destroy_helper_block_"); |
| 1188 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1189 | FunctionDecl *FD = FunctionDecl::Create(C, C.getTranslationUnitDecl(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1190 | SourceLocation(), |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1191 | SourceLocation(), II, C.VoidTy, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1192 | SC_Static, |
| 1193 | SC_None, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 1194 | false, true); |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 1195 | StartFunction(FD, C.VoidTy, Fn, FI, args, SourceLocation()); |
Mike Stump | 1edf6b6 | 2009-03-07 02:53:18 +0000 | [diff] [blame] | 1196 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1197 | llvm::Type *structPtrTy = blockInfo.StructureType->getPointerTo(); |
Mike Stump | 1edf6b6 | 2009-03-07 02:53:18 +0000 | [diff] [blame] | 1198 | |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 1199 | llvm::Value *src = GetAddrOfLocalVar(&srcDecl); |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1200 | src = Builder.CreateLoad(src); |
| 1201 | src = Builder.CreateBitCast(src, structPtrTy, "block"); |
Mike Stump | 1edf6b6 | 2009-03-07 02:53:18 +0000 | [diff] [blame] | 1202 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1203 | const BlockDecl *blockDecl = blockInfo.getBlockDecl(); |
| 1204 | |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1205 | CodeGenFunction::RunCleanupsScope cleanups(*this); |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1206 | |
| 1207 | for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(), |
| 1208 | ce = blockDecl->capture_end(); ci != ce; ++ci) { |
| 1209 | const VarDecl *variable = ci->getVariable(); |
| 1210 | QualType type = variable->getType(); |
| 1211 | |
| 1212 | const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable); |
| 1213 | if (capture.isConstant()) continue; |
| 1214 | |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1215 | BlockFieldFlags flags; |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1216 | const CXXDestructorDecl *dtor = 0; |
| 1217 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1218 | bool isARCWeakCapture = false; |
| 1219 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1220 | if (ci->isByRef()) { |
| 1221 | flags = BLOCK_FIELD_IS_BYREF; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1222 | if (type.isObjCGCWeak()) |
| 1223 | flags |= BLOCK_FIELD_IS_WEAK; |
| 1224 | } else if (const CXXRecordDecl *record = type->getAsCXXRecordDecl()) { |
| 1225 | if (record->hasTrivialDestructor()) |
| 1226 | continue; |
| 1227 | dtor = record->getDestructor(); |
| 1228 | } else if (type->isObjCRetainableType()) { |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1229 | flags = BLOCK_FIELD_IS_OBJECT; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1230 | if (type->isBlockPointerType()) |
| 1231 | flags = BLOCK_FIELD_IS_BLOCK; |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1232 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1233 | // Special rules for ARC captures. |
| 1234 | if (getLangOptions().ObjCAutoRefCount) { |
| 1235 | Qualifiers qs = type.getQualifiers(); |
| 1236 | |
| 1237 | // Don't generate special dispose logic for a captured object |
| 1238 | // unless it's __strong or __weak. |
| 1239 | if (!qs.hasStrongOrWeakObjCLifetime()) |
| 1240 | continue; |
| 1241 | |
| 1242 | // Support __weak direct captures. |
| 1243 | if (qs.getObjCLifetime() == Qualifiers::OCL_Weak) |
| 1244 | isARCWeakCapture = true; |
| 1245 | } |
| 1246 | } else { |
| 1247 | continue; |
| 1248 | } |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1249 | |
| 1250 | unsigned index = capture.getIndex(); |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1251 | llvm::Value *srcField = Builder.CreateStructGEP(src, index); |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1252 | |
| 1253 | // If there's an explicit copy expression, we do that. |
| 1254 | if (dtor) { |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1255 | PushDestructorCleanup(dtor, srcField); |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1256 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1257 | // If this is a __weak capture, emit the release directly. |
| 1258 | } else if (isARCWeakCapture) { |
| 1259 | EmitARCDestroyWeak(srcField); |
| 1260 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1261 | // Otherwise we call _Block_object_dispose. It wouldn't be too |
| 1262 | // hard to just emit this as a cleanup if we wanted to make sure |
| 1263 | // that things were done in reverse. |
| 1264 | } else { |
| 1265 | llvm::Value *value = Builder.CreateLoad(srcField); |
John McCall | 5936e33 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 1266 | value = Builder.CreateBitCast(value, VoidPtrTy); |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1267 | BuildBlockRelease(value, flags); |
| 1268 | } |
Mike Stump | 1edf6b6 | 2009-03-07 02:53:18 +0000 | [diff] [blame] | 1269 | } |
| 1270 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1271 | cleanups.ForceCleanup(); |
| 1272 | |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1273 | FinishFunction(); |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 1274 | |
John McCall | 5936e33 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 1275 | return llvm::ConstantExpr::getBitCast(Fn, VoidPtrTy); |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 1276 | } |
| 1277 | |
John McCall | f0c11f7 | 2011-03-31 08:03:29 +0000 | [diff] [blame] | 1278 | namespace { |
| 1279 | |
| 1280 | /// Emits the copy/dispose helper functions for a __block object of id type. |
| 1281 | class ObjectByrefHelpers : public CodeGenModule::ByrefHelpers { |
| 1282 | BlockFieldFlags Flags; |
| 1283 | |
| 1284 | public: |
| 1285 | ObjectByrefHelpers(CharUnits alignment, BlockFieldFlags flags) |
| 1286 | : ByrefHelpers(alignment), Flags(flags) {} |
| 1287 | |
John McCall | 3617019 | 2011-03-31 09:19:20 +0000 | [diff] [blame] | 1288 | void emitCopy(CodeGenFunction &CGF, llvm::Value *destField, |
| 1289 | llvm::Value *srcField) { |
John McCall | f0c11f7 | 2011-03-31 08:03:29 +0000 | [diff] [blame] | 1290 | destField = CGF.Builder.CreateBitCast(destField, CGF.VoidPtrTy); |
| 1291 | |
| 1292 | srcField = CGF.Builder.CreateBitCast(srcField, CGF.VoidPtrPtrTy); |
| 1293 | llvm::Value *srcValue = CGF.Builder.CreateLoad(srcField); |
| 1294 | |
| 1295 | unsigned flags = (Flags | BLOCK_BYREF_CALLER).getBitMask(); |
| 1296 | |
| 1297 | llvm::Value *flagsVal = llvm::ConstantInt::get(CGF.Int32Ty, flags); |
| 1298 | llvm::Value *fn = CGF.CGM.getBlockObjectAssign(); |
| 1299 | CGF.Builder.CreateCall3(fn, destField, srcValue, flagsVal); |
| 1300 | } |
| 1301 | |
| 1302 | void emitDispose(CodeGenFunction &CGF, llvm::Value *field) { |
| 1303 | field = CGF.Builder.CreateBitCast(field, CGF.Int8PtrTy->getPointerTo(0)); |
| 1304 | llvm::Value *value = CGF.Builder.CreateLoad(field); |
| 1305 | |
| 1306 | CGF.BuildBlockRelease(value, Flags | BLOCK_BYREF_CALLER); |
| 1307 | } |
| 1308 | |
| 1309 | void profileImpl(llvm::FoldingSetNodeID &id) const { |
| 1310 | id.AddInteger(Flags.getBitMask()); |
| 1311 | } |
| 1312 | }; |
| 1313 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1314 | /// Emits the copy/dispose helpers for an ARC __block __weak variable. |
| 1315 | class ARCWeakByrefHelpers : public CodeGenModule::ByrefHelpers { |
| 1316 | public: |
| 1317 | ARCWeakByrefHelpers(CharUnits alignment) : ByrefHelpers(alignment) {} |
| 1318 | |
| 1319 | void emitCopy(CodeGenFunction &CGF, llvm::Value *destField, |
| 1320 | llvm::Value *srcField) { |
| 1321 | CGF.EmitARCMoveWeak(destField, srcField); |
| 1322 | } |
| 1323 | |
| 1324 | void emitDispose(CodeGenFunction &CGF, llvm::Value *field) { |
| 1325 | CGF.EmitARCDestroyWeak(field); |
| 1326 | } |
| 1327 | |
| 1328 | void profileImpl(llvm::FoldingSetNodeID &id) const { |
| 1329 | // 0 is distinguishable from all pointers and byref flags |
| 1330 | id.AddInteger(0); |
| 1331 | } |
| 1332 | }; |
| 1333 | |
| 1334 | /// Emits the copy/dispose helpers for an ARC __block __strong variable |
| 1335 | /// that's not of block-pointer type. |
| 1336 | class ARCStrongByrefHelpers : public CodeGenModule::ByrefHelpers { |
| 1337 | public: |
| 1338 | ARCStrongByrefHelpers(CharUnits alignment) : ByrefHelpers(alignment) {} |
| 1339 | |
| 1340 | void emitCopy(CodeGenFunction &CGF, llvm::Value *destField, |
| 1341 | llvm::Value *srcField) { |
| 1342 | // Do a "move" by copying the value and then zeroing out the old |
| 1343 | // variable. |
| 1344 | |
John McCall | a59e4b7 | 2011-11-09 03:17:26 +0000 | [diff] [blame] | 1345 | llvm::LoadInst *value = CGF.Builder.CreateLoad(srcField); |
| 1346 | value->setAlignment(Alignment.getQuantity()); |
| 1347 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1348 | llvm::Value *null = |
| 1349 | llvm::ConstantPointerNull::get(cast<llvm::PointerType>(value->getType())); |
John McCall | a59e4b7 | 2011-11-09 03:17:26 +0000 | [diff] [blame] | 1350 | |
| 1351 | llvm::StoreInst *store = CGF.Builder.CreateStore(value, destField); |
| 1352 | store->setAlignment(Alignment.getQuantity()); |
| 1353 | |
| 1354 | store = CGF.Builder.CreateStore(null, srcField); |
| 1355 | store->setAlignment(Alignment.getQuantity()); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1356 | } |
| 1357 | |
| 1358 | void emitDispose(CodeGenFunction &CGF, llvm::Value *field) { |
John McCall | a59e4b7 | 2011-11-09 03:17:26 +0000 | [diff] [blame] | 1359 | llvm::LoadInst *value = CGF.Builder.CreateLoad(field); |
| 1360 | value->setAlignment(Alignment.getQuantity()); |
| 1361 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1362 | CGF.EmitARCRelease(value, /*precise*/ false); |
| 1363 | } |
| 1364 | |
| 1365 | void profileImpl(llvm::FoldingSetNodeID &id) const { |
| 1366 | // 1 is distinguishable from all pointers and byref flags |
| 1367 | id.AddInteger(1); |
| 1368 | } |
| 1369 | }; |
| 1370 | |
John McCall | a59e4b7 | 2011-11-09 03:17:26 +0000 | [diff] [blame] | 1371 | /// Emits the copy/dispose helpers for an ARC __block __strong |
| 1372 | /// variable that's of block-pointer type. |
| 1373 | class ARCStrongBlockByrefHelpers : public CodeGenModule::ByrefHelpers { |
| 1374 | public: |
| 1375 | ARCStrongBlockByrefHelpers(CharUnits alignment) : ByrefHelpers(alignment) {} |
| 1376 | |
| 1377 | void emitCopy(CodeGenFunction &CGF, llvm::Value *destField, |
| 1378 | llvm::Value *srcField) { |
| 1379 | // Do the copy with objc_retainBlock; that's all that |
| 1380 | // _Block_object_assign would do anyway, and we'd have to pass the |
| 1381 | // right arguments to make sure it doesn't get no-op'ed. |
| 1382 | llvm::LoadInst *oldValue = CGF.Builder.CreateLoad(srcField); |
| 1383 | oldValue->setAlignment(Alignment.getQuantity()); |
| 1384 | |
| 1385 | llvm::Value *copy = CGF.EmitARCRetainBlock(oldValue, /*mandatory*/ true); |
| 1386 | |
| 1387 | llvm::StoreInst *store = CGF.Builder.CreateStore(copy, destField); |
| 1388 | store->setAlignment(Alignment.getQuantity()); |
| 1389 | } |
| 1390 | |
| 1391 | void emitDispose(CodeGenFunction &CGF, llvm::Value *field) { |
| 1392 | llvm::LoadInst *value = CGF.Builder.CreateLoad(field); |
| 1393 | value->setAlignment(Alignment.getQuantity()); |
| 1394 | |
| 1395 | CGF.EmitARCRelease(value, /*precise*/ false); |
| 1396 | } |
| 1397 | |
| 1398 | void profileImpl(llvm::FoldingSetNodeID &id) const { |
| 1399 | // 2 is distinguishable from all pointers and byref flags |
| 1400 | id.AddInteger(2); |
| 1401 | } |
| 1402 | }; |
| 1403 | |
John McCall | f0c11f7 | 2011-03-31 08:03:29 +0000 | [diff] [blame] | 1404 | /// Emits the copy/dispose helpers for a __block variable with a |
| 1405 | /// nontrivial copy constructor or destructor. |
| 1406 | class CXXByrefHelpers : public CodeGenModule::ByrefHelpers { |
| 1407 | QualType VarType; |
| 1408 | const Expr *CopyExpr; |
| 1409 | |
| 1410 | public: |
| 1411 | CXXByrefHelpers(CharUnits alignment, QualType type, |
| 1412 | const Expr *copyExpr) |
| 1413 | : ByrefHelpers(alignment), VarType(type), CopyExpr(copyExpr) {} |
| 1414 | |
| 1415 | bool needsCopy() const { return CopyExpr != 0; } |
| 1416 | void emitCopy(CodeGenFunction &CGF, llvm::Value *destField, |
| 1417 | llvm::Value *srcField) { |
| 1418 | if (!CopyExpr) return; |
| 1419 | CGF.EmitSynthesizedCXXCopyCtor(destField, srcField, CopyExpr); |
| 1420 | } |
| 1421 | |
| 1422 | void emitDispose(CodeGenFunction &CGF, llvm::Value *field) { |
| 1423 | EHScopeStack::stable_iterator cleanupDepth = CGF.EHStack.stable_begin(); |
| 1424 | CGF.PushDestructorCleanup(VarType, field); |
| 1425 | CGF.PopCleanupBlocks(cleanupDepth); |
| 1426 | } |
| 1427 | |
| 1428 | void profileImpl(llvm::FoldingSetNodeID &id) const { |
| 1429 | id.AddPointer(VarType.getCanonicalType().getAsOpaquePtr()); |
| 1430 | } |
| 1431 | }; |
| 1432 | } // end anonymous namespace |
| 1433 | |
| 1434 | static llvm::Constant * |
| 1435 | generateByrefCopyHelper(CodeGenFunction &CGF, |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1436 | llvm::StructType &byrefType, |
John McCall | f0c11f7 | 2011-03-31 08:03:29 +0000 | [diff] [blame] | 1437 | CodeGenModule::ByrefHelpers &byrefInfo) { |
| 1438 | ASTContext &Context = CGF.getContext(); |
| 1439 | |
| 1440 | QualType R = Context.VoidTy; |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1441 | |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 1442 | FunctionArgList args; |
John McCall | f0c11f7 | 2011-03-31 08:03:29 +0000 | [diff] [blame] | 1443 | ImplicitParamDecl dst(0, SourceLocation(), 0, Context.VoidPtrTy); |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 1444 | args.push_back(&dst); |
Mike Stump | ee09422 | 2009-03-06 06:12:24 +0000 | [diff] [blame] | 1445 | |
John McCall | f0c11f7 | 2011-03-31 08:03:29 +0000 | [diff] [blame] | 1446 | ImplicitParamDecl src(0, SourceLocation(), 0, Context.VoidPtrTy); |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 1447 | args.push_back(&src); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1448 | |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1449 | const CGFunctionInfo &FI = |
John McCall | f0c11f7 | 2011-03-31 08:03:29 +0000 | [diff] [blame] | 1450 | CGF.CGM.getTypes().getFunctionInfo(R, args, FunctionType::ExtInfo()); |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1451 | |
John McCall | f0c11f7 | 2011-03-31 08:03:29 +0000 | [diff] [blame] | 1452 | CodeGenTypes &Types = CGF.CGM.getTypes(); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1453 | llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1454 | |
Mike Stump | 3899a7f | 2009-06-05 23:26:36 +0000 | [diff] [blame] | 1455 | // FIXME: We'd like to put these into a mergable by content, with |
| 1456 | // internal linkage. |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1457 | llvm::Function *Fn = |
| 1458 | llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, |
John McCall | f0c11f7 | 2011-03-31 08:03:29 +0000 | [diff] [blame] | 1459 | "__Block_byref_object_copy_", &CGF.CGM.getModule()); |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1460 | |
| 1461 | IdentifierInfo *II |
John McCall | f0c11f7 | 2011-03-31 08:03:29 +0000 | [diff] [blame] | 1462 | = &Context.Idents.get("__Block_byref_object_copy_"); |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1463 | |
John McCall | f0c11f7 | 2011-03-31 08:03:29 +0000 | [diff] [blame] | 1464 | FunctionDecl *FD = FunctionDecl::Create(Context, |
| 1465 | Context.getTranslationUnitDecl(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1466 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 1467 | SourceLocation(), II, R, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1468 | SC_Static, |
| 1469 | SC_None, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 1470 | false, true); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1471 | |
John McCall | f0c11f7 | 2011-03-31 08:03:29 +0000 | [diff] [blame] | 1472 | CGF.StartFunction(FD, R, Fn, FI, args, SourceLocation()); |
Mike Stump | ee09422 | 2009-03-06 06:12:24 +0000 | [diff] [blame] | 1473 | |
John McCall | f0c11f7 | 2011-03-31 08:03:29 +0000 | [diff] [blame] | 1474 | if (byrefInfo.needsCopy()) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1475 | llvm::Type *byrefPtrType = byrefType.getPointerTo(0); |
Mike Stump | ee09422 | 2009-03-06 06:12:24 +0000 | [diff] [blame] | 1476 | |
John McCall | f0c11f7 | 2011-03-31 08:03:29 +0000 | [diff] [blame] | 1477 | // dst->x |
| 1478 | llvm::Value *destField = CGF.GetAddrOfLocalVar(&dst); |
| 1479 | destField = CGF.Builder.CreateLoad(destField); |
| 1480 | destField = CGF.Builder.CreateBitCast(destField, byrefPtrType); |
| 1481 | destField = CGF.Builder.CreateStructGEP(destField, 6, "x"); |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1482 | |
John McCall | f0c11f7 | 2011-03-31 08:03:29 +0000 | [diff] [blame] | 1483 | // src->x |
| 1484 | llvm::Value *srcField = CGF.GetAddrOfLocalVar(&src); |
| 1485 | srcField = CGF.Builder.CreateLoad(srcField); |
| 1486 | srcField = CGF.Builder.CreateBitCast(srcField, byrefPtrType); |
| 1487 | srcField = CGF.Builder.CreateStructGEP(srcField, 6, "x"); |
| 1488 | |
| 1489 | byrefInfo.emitCopy(CGF, destField, srcField); |
| 1490 | } |
| 1491 | |
| 1492 | CGF.FinishFunction(); |
| 1493 | |
| 1494 | return llvm::ConstantExpr::getBitCast(Fn, CGF.Int8PtrTy); |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1495 | } |
| 1496 | |
John McCall | f0c11f7 | 2011-03-31 08:03:29 +0000 | [diff] [blame] | 1497 | /// Build the copy helper for a __block variable. |
| 1498 | static llvm::Constant *buildByrefCopyHelper(CodeGenModule &CGM, |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1499 | llvm::StructType &byrefType, |
John McCall | f0c11f7 | 2011-03-31 08:03:29 +0000 | [diff] [blame] | 1500 | CodeGenModule::ByrefHelpers &info) { |
| 1501 | CodeGenFunction CGF(CGM); |
| 1502 | return generateByrefCopyHelper(CGF, byrefType, info); |
| 1503 | } |
| 1504 | |
| 1505 | /// Generate code for a __block variable's dispose helper. |
| 1506 | static llvm::Constant * |
| 1507 | generateByrefDisposeHelper(CodeGenFunction &CGF, |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1508 | llvm::StructType &byrefType, |
John McCall | f0c11f7 | 2011-03-31 08:03:29 +0000 | [diff] [blame] | 1509 | CodeGenModule::ByrefHelpers &byrefInfo) { |
| 1510 | ASTContext &Context = CGF.getContext(); |
| 1511 | QualType R = Context.VoidTy; |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1512 | |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 1513 | FunctionArgList args; |
John McCall | f0c11f7 | 2011-03-31 08:03:29 +0000 | [diff] [blame] | 1514 | ImplicitParamDecl src(0, SourceLocation(), 0, Context.VoidPtrTy); |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 1515 | args.push_back(&src); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1516 | |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1517 | const CGFunctionInfo &FI = |
John McCall | f0c11f7 | 2011-03-31 08:03:29 +0000 | [diff] [blame] | 1518 | CGF.CGM.getTypes().getFunctionInfo(R, args, FunctionType::ExtInfo()); |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1519 | |
John McCall | f0c11f7 | 2011-03-31 08:03:29 +0000 | [diff] [blame] | 1520 | CodeGenTypes &Types = CGF.CGM.getTypes(); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1521 | llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1522 | |
Mike Stump | 3899a7f | 2009-06-05 23:26:36 +0000 | [diff] [blame] | 1523 | // FIXME: We'd like to put these into a mergable by content, with |
| 1524 | // internal linkage. |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1525 | llvm::Function *Fn = |
| 1526 | llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1527 | "__Block_byref_object_dispose_", |
John McCall | f0c11f7 | 2011-03-31 08:03:29 +0000 | [diff] [blame] | 1528 | &CGF.CGM.getModule()); |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1529 | |
| 1530 | IdentifierInfo *II |
John McCall | f0c11f7 | 2011-03-31 08:03:29 +0000 | [diff] [blame] | 1531 | = &Context.Idents.get("__Block_byref_object_dispose_"); |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1532 | |
John McCall | f0c11f7 | 2011-03-31 08:03:29 +0000 | [diff] [blame] | 1533 | FunctionDecl *FD = FunctionDecl::Create(Context, |
| 1534 | Context.getTranslationUnitDecl(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1535 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 1536 | SourceLocation(), II, R, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1537 | SC_Static, |
| 1538 | SC_None, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 1539 | false, true); |
John McCall | f0c11f7 | 2011-03-31 08:03:29 +0000 | [diff] [blame] | 1540 | CGF.StartFunction(FD, R, Fn, FI, args, SourceLocation()); |
Mike Stump | 1851b68 | 2009-03-06 04:53:30 +0000 | [diff] [blame] | 1541 | |
John McCall | f0c11f7 | 2011-03-31 08:03:29 +0000 | [diff] [blame] | 1542 | if (byrefInfo.needsDispose()) { |
| 1543 | llvm::Value *V = CGF.GetAddrOfLocalVar(&src); |
| 1544 | V = CGF.Builder.CreateLoad(V); |
| 1545 | V = CGF.Builder.CreateBitCast(V, byrefType.getPointerTo(0)); |
| 1546 | V = CGF.Builder.CreateStructGEP(V, 6, "x"); |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1547 | |
John McCall | f0c11f7 | 2011-03-31 08:03:29 +0000 | [diff] [blame] | 1548 | byrefInfo.emitDispose(CGF, V); |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1549 | } |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1550 | |
John McCall | f0c11f7 | 2011-03-31 08:03:29 +0000 | [diff] [blame] | 1551 | CGF.FinishFunction(); |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1552 | |
John McCall | f0c11f7 | 2011-03-31 08:03:29 +0000 | [diff] [blame] | 1553 | return llvm::ConstantExpr::getBitCast(Fn, CGF.Int8PtrTy); |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1554 | } |
| 1555 | |
John McCall | f0c11f7 | 2011-03-31 08:03:29 +0000 | [diff] [blame] | 1556 | /// Build the dispose helper for a __block variable. |
| 1557 | static llvm::Constant *buildByrefDisposeHelper(CodeGenModule &CGM, |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1558 | llvm::StructType &byrefType, |
John McCall | f0c11f7 | 2011-03-31 08:03:29 +0000 | [diff] [blame] | 1559 | CodeGenModule::ByrefHelpers &info) { |
| 1560 | CodeGenFunction CGF(CGM); |
| 1561 | return generateByrefDisposeHelper(CGF, byrefType, info); |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1562 | } |
| 1563 | |
John McCall | f0c11f7 | 2011-03-31 08:03:29 +0000 | [diff] [blame] | 1564 | /// |
| 1565 | template <class T> static T *buildByrefHelpers(CodeGenModule &CGM, |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1566 | llvm::StructType &byrefTy, |
John McCall | f0c11f7 | 2011-03-31 08:03:29 +0000 | [diff] [blame] | 1567 | T &byrefInfo) { |
| 1568 | // Increase the field's alignment to be at least pointer alignment, |
| 1569 | // since the layout of the byref struct will guarantee at least that. |
| 1570 | byrefInfo.Alignment = std::max(byrefInfo.Alignment, |
| 1571 | CharUnits::fromQuantity(CGM.PointerAlignInBytes)); |
| 1572 | |
| 1573 | llvm::FoldingSetNodeID id; |
| 1574 | byrefInfo.Profile(id); |
| 1575 | |
| 1576 | void *insertPos; |
| 1577 | CodeGenModule::ByrefHelpers *node |
| 1578 | = CGM.ByrefHelpersCache.FindNodeOrInsertPos(id, insertPos); |
| 1579 | if (node) return static_cast<T*>(node); |
| 1580 | |
| 1581 | byrefInfo.CopyHelper = buildByrefCopyHelper(CGM, byrefTy, byrefInfo); |
| 1582 | byrefInfo.DisposeHelper = buildByrefDisposeHelper(CGM, byrefTy, byrefInfo); |
| 1583 | |
| 1584 | T *copy = new (CGM.getContext()) T(byrefInfo); |
| 1585 | CGM.ByrefHelpersCache.InsertNode(copy, insertPos); |
| 1586 | return copy; |
| 1587 | } |
| 1588 | |
| 1589 | CodeGenModule::ByrefHelpers * |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1590 | CodeGenFunction::buildByrefHelpers(llvm::StructType &byrefType, |
John McCall | f0c11f7 | 2011-03-31 08:03:29 +0000 | [diff] [blame] | 1591 | const AutoVarEmission &emission) { |
| 1592 | const VarDecl &var = *emission.Variable; |
| 1593 | QualType type = var.getType(); |
| 1594 | |
| 1595 | if (const CXXRecordDecl *record = type->getAsCXXRecordDecl()) { |
| 1596 | const Expr *copyExpr = CGM.getContext().getBlockVarCopyInits(&var); |
| 1597 | if (!copyExpr && record->hasTrivialDestructor()) return 0; |
| 1598 | |
| 1599 | CXXByrefHelpers byrefInfo(emission.Alignment, type, copyExpr); |
| 1600 | return ::buildByrefHelpers(CGM, byrefType, byrefInfo); |
| 1601 | } |
| 1602 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1603 | // Otherwise, if we don't have a retainable type, there's nothing to do. |
| 1604 | // that the runtime does extra copies. |
| 1605 | if (!type->isObjCRetainableType()) return 0; |
| 1606 | |
| 1607 | Qualifiers qs = type.getQualifiers(); |
| 1608 | |
| 1609 | // If we have lifetime, that dominates. |
| 1610 | if (Qualifiers::ObjCLifetime lifetime = qs.getObjCLifetime()) { |
| 1611 | assert(getLangOptions().ObjCAutoRefCount); |
| 1612 | |
| 1613 | switch (lifetime) { |
| 1614 | case Qualifiers::OCL_None: llvm_unreachable("impossible"); |
| 1615 | |
| 1616 | // These are just bits as far as the runtime is concerned. |
| 1617 | case Qualifiers::OCL_ExplicitNone: |
| 1618 | case Qualifiers::OCL_Autoreleasing: |
| 1619 | return 0; |
| 1620 | |
| 1621 | // Tell the runtime that this is ARC __weak, called by the |
| 1622 | // byref routines. |
| 1623 | case Qualifiers::OCL_Weak: { |
| 1624 | ARCWeakByrefHelpers byrefInfo(emission.Alignment); |
| 1625 | return ::buildByrefHelpers(CGM, byrefType, byrefInfo); |
| 1626 | } |
| 1627 | |
| 1628 | // ARC __strong __block variables need to be retained. |
| 1629 | case Qualifiers::OCL_Strong: |
John McCall | a59e4b7 | 2011-11-09 03:17:26 +0000 | [diff] [blame] | 1630 | // Block pointers need to be copied, and there's no direct |
| 1631 | // transfer possible. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1632 | if (type->isBlockPointerType()) { |
John McCall | a59e4b7 | 2011-11-09 03:17:26 +0000 | [diff] [blame] | 1633 | ARCStrongBlockByrefHelpers byrefInfo(emission.Alignment); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1634 | return ::buildByrefHelpers(CGM, byrefType, byrefInfo); |
| 1635 | |
| 1636 | // Otherwise, we transfer ownership of the retain from the stack |
| 1637 | // to the heap. |
| 1638 | } else { |
| 1639 | ARCStrongByrefHelpers byrefInfo(emission.Alignment); |
| 1640 | return ::buildByrefHelpers(CGM, byrefType, byrefInfo); |
| 1641 | } |
| 1642 | } |
| 1643 | llvm_unreachable("fell out of lifetime switch!"); |
| 1644 | } |
| 1645 | |
John McCall | f0c11f7 | 2011-03-31 08:03:29 +0000 | [diff] [blame] | 1646 | BlockFieldFlags flags; |
| 1647 | if (type->isBlockPointerType()) { |
| 1648 | flags |= BLOCK_FIELD_IS_BLOCK; |
| 1649 | } else if (CGM.getContext().isObjCNSObjectType(type) || |
| 1650 | type->isObjCObjectPointerType()) { |
| 1651 | flags |= BLOCK_FIELD_IS_OBJECT; |
| 1652 | } else { |
| 1653 | return 0; |
| 1654 | } |
| 1655 | |
| 1656 | if (type.isObjCGCWeak()) |
| 1657 | flags |= BLOCK_FIELD_IS_WEAK; |
| 1658 | |
| 1659 | ObjectByrefHelpers byrefInfo(emission.Alignment, flags); |
| 1660 | return ::buildByrefHelpers(CGM, byrefType, byrefInfo); |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1661 | } |
| 1662 | |
John McCall | 5af02db | 2011-03-31 01:59:53 +0000 | [diff] [blame] | 1663 | unsigned CodeGenFunction::getByRefValueLLVMField(const ValueDecl *VD) const { |
| 1664 | assert(ByRefValueInfo.count(VD) && "Did not find value!"); |
| 1665 | |
| 1666 | return ByRefValueInfo.find(VD)->second.second; |
| 1667 | } |
| 1668 | |
| 1669 | llvm::Value *CodeGenFunction::BuildBlockByrefAddress(llvm::Value *BaseAddr, |
| 1670 | const VarDecl *V) { |
| 1671 | llvm::Value *Loc = Builder.CreateStructGEP(BaseAddr, 1, "forwarding"); |
| 1672 | Loc = Builder.CreateLoad(Loc); |
| 1673 | Loc = Builder.CreateStructGEP(Loc, getByRefValueLLVMField(V), |
| 1674 | V->getNameAsString()); |
| 1675 | return Loc; |
| 1676 | } |
| 1677 | |
| 1678 | /// BuildByRefType - This routine changes a __block variable declared as T x |
| 1679 | /// into: |
| 1680 | /// |
| 1681 | /// struct { |
| 1682 | /// void *__isa; |
| 1683 | /// void *__forwarding; |
| 1684 | /// int32_t __flags; |
| 1685 | /// int32_t __size; |
| 1686 | /// void *__copy_helper; // only if needed |
| 1687 | /// void *__destroy_helper; // only if needed |
| 1688 | /// char padding[X]; // only if needed |
| 1689 | /// T x; |
| 1690 | /// } x |
| 1691 | /// |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1692 | llvm::Type *CodeGenFunction::BuildByRefType(const VarDecl *D) { |
| 1693 | std::pair<llvm::Type *, unsigned> &Info = ByRefValueInfo[D]; |
John McCall | 5af02db | 2011-03-31 01:59:53 +0000 | [diff] [blame] | 1694 | if (Info.first) |
| 1695 | return Info.first; |
| 1696 | |
| 1697 | QualType Ty = D->getType(); |
| 1698 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1699 | SmallVector<llvm::Type *, 8> types; |
John McCall | 5af02db | 2011-03-31 01:59:53 +0000 | [diff] [blame] | 1700 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1701 | llvm::StructType *ByRefType = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 1702 | llvm::StructType::create(getLLVMContext(), |
| 1703 | "struct.__block_byref_" + D->getNameAsString()); |
John McCall | 5af02db | 2011-03-31 01:59:53 +0000 | [diff] [blame] | 1704 | |
| 1705 | // void *__isa; |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 1706 | types.push_back(Int8PtrTy); |
John McCall | 5af02db | 2011-03-31 01:59:53 +0000 | [diff] [blame] | 1707 | |
| 1708 | // void *__forwarding; |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1709 | types.push_back(llvm::PointerType::getUnqual(ByRefType)); |
John McCall | 5af02db | 2011-03-31 01:59:53 +0000 | [diff] [blame] | 1710 | |
| 1711 | // int32_t __flags; |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 1712 | types.push_back(Int32Ty); |
John McCall | 5af02db | 2011-03-31 01:59:53 +0000 | [diff] [blame] | 1713 | |
| 1714 | // int32_t __size; |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 1715 | types.push_back(Int32Ty); |
John McCall | 5af02db | 2011-03-31 01:59:53 +0000 | [diff] [blame] | 1716 | |
| 1717 | bool HasCopyAndDispose = getContext().BlockRequiresCopying(Ty); |
| 1718 | if (HasCopyAndDispose) { |
| 1719 | /// void *__copy_helper; |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 1720 | types.push_back(Int8PtrTy); |
John McCall | 5af02db | 2011-03-31 01:59:53 +0000 | [diff] [blame] | 1721 | |
| 1722 | /// void *__destroy_helper; |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 1723 | types.push_back(Int8PtrTy); |
John McCall | 5af02db | 2011-03-31 01:59:53 +0000 | [diff] [blame] | 1724 | } |
| 1725 | |
| 1726 | bool Packed = false; |
| 1727 | CharUnits Align = getContext().getDeclAlign(D); |
| 1728 | if (Align > getContext().toCharUnitsFromBits(Target.getPointerAlign(0))) { |
| 1729 | // We have to insert padding. |
| 1730 | |
| 1731 | // The struct above has 2 32-bit integers. |
| 1732 | unsigned CurrentOffsetInBytes = 4 * 2; |
| 1733 | |
| 1734 | // And either 2 or 4 pointers. |
| 1735 | CurrentOffsetInBytes += (HasCopyAndDispose ? 4 : 2) * |
| 1736 | CGM.getTargetData().getTypeAllocSize(Int8PtrTy); |
| 1737 | |
| 1738 | // Align the offset. |
| 1739 | unsigned AlignedOffsetInBytes = |
| 1740 | llvm::RoundUpToAlignment(CurrentOffsetInBytes, Align.getQuantity()); |
| 1741 | |
| 1742 | unsigned NumPaddingBytes = AlignedOffsetInBytes - CurrentOffsetInBytes; |
| 1743 | if (NumPaddingBytes > 0) { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1744 | llvm::Type *Ty = llvm::Type::getInt8Ty(getLLVMContext()); |
John McCall | 5af02db | 2011-03-31 01:59:53 +0000 | [diff] [blame] | 1745 | // 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] | 1746 | // the maximal stack alignment and the alignment of malloc on the system. |
John McCall | 5af02db | 2011-03-31 01:59:53 +0000 | [diff] [blame] | 1747 | if (NumPaddingBytes > 1) |
| 1748 | Ty = llvm::ArrayType::get(Ty, NumPaddingBytes); |
| 1749 | |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 1750 | types.push_back(Ty); |
John McCall | 5af02db | 2011-03-31 01:59:53 +0000 | [diff] [blame] | 1751 | |
| 1752 | // We want a packed struct. |
| 1753 | Packed = true; |
| 1754 | } |
| 1755 | } |
| 1756 | |
| 1757 | // T x; |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 1758 | types.push_back(ConvertTypeForMem(Ty)); |
John McCall | 5af02db | 2011-03-31 01:59:53 +0000 | [diff] [blame] | 1759 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1760 | ByRefType->setBody(types, Packed); |
John McCall | 5af02db | 2011-03-31 01:59:53 +0000 | [diff] [blame] | 1761 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1762 | Info.first = ByRefType; |
John McCall | 5af02db | 2011-03-31 01:59:53 +0000 | [diff] [blame] | 1763 | |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 1764 | Info.second = types.size() - 1; |
John McCall | 5af02db | 2011-03-31 01:59:53 +0000 | [diff] [blame] | 1765 | |
| 1766 | return Info.first; |
| 1767 | } |
| 1768 | |
| 1769 | /// Initialize the structural components of a __block variable, i.e. |
| 1770 | /// everything but the actual object. |
| 1771 | void CodeGenFunction::emitByrefStructureInit(const AutoVarEmission &emission) { |
John McCall | f0c11f7 | 2011-03-31 08:03:29 +0000 | [diff] [blame] | 1772 | // Find the address of the local. |
| 1773 | llvm::Value *addr = emission.Address; |
John McCall | 5af02db | 2011-03-31 01:59:53 +0000 | [diff] [blame] | 1774 | |
John McCall | f0c11f7 | 2011-03-31 08:03:29 +0000 | [diff] [blame] | 1775 | // That's an alloca of the byref structure type. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1776 | llvm::StructType *byrefType = cast<llvm::StructType>( |
John McCall | f0c11f7 | 2011-03-31 08:03:29 +0000 | [diff] [blame] | 1777 | cast<llvm::PointerType>(addr->getType())->getElementType()); |
| 1778 | |
| 1779 | // Build the byref helpers if necessary. This is null if we don't need any. |
| 1780 | CodeGenModule::ByrefHelpers *helpers = |
| 1781 | buildByrefHelpers(*byrefType, emission); |
John McCall | 5af02db | 2011-03-31 01:59:53 +0000 | [diff] [blame] | 1782 | |
| 1783 | const VarDecl &D = *emission.Variable; |
| 1784 | QualType type = D.getType(); |
| 1785 | |
John McCall | f0c11f7 | 2011-03-31 08:03:29 +0000 | [diff] [blame] | 1786 | llvm::Value *V; |
John McCall | 5af02db | 2011-03-31 01:59:53 +0000 | [diff] [blame] | 1787 | |
| 1788 | // Initialize the 'isa', which is just 0 or 1. |
| 1789 | int isa = 0; |
John McCall | f0c11f7 | 2011-03-31 08:03:29 +0000 | [diff] [blame] | 1790 | if (type.isObjCGCWeak()) |
John McCall | 5af02db | 2011-03-31 01:59:53 +0000 | [diff] [blame] | 1791 | isa = 1; |
| 1792 | V = Builder.CreateIntToPtr(Builder.getInt32(isa), Int8PtrTy, "isa"); |
| 1793 | Builder.CreateStore(V, Builder.CreateStructGEP(addr, 0, "byref.isa")); |
| 1794 | |
| 1795 | // Store the address of the variable into its own forwarding pointer. |
| 1796 | Builder.CreateStore(addr, |
| 1797 | Builder.CreateStructGEP(addr, 1, "byref.forwarding")); |
| 1798 | |
| 1799 | // Blocks ABI: |
| 1800 | // c) the flags field is set to either 0 if no helper functions are |
| 1801 | // needed or BLOCK_HAS_COPY_DISPOSE if they are, |
| 1802 | BlockFlags flags; |
John McCall | f0c11f7 | 2011-03-31 08:03:29 +0000 | [diff] [blame] | 1803 | if (helpers) flags |= BLOCK_HAS_COPY_DISPOSE; |
John McCall | 5af02db | 2011-03-31 01:59:53 +0000 | [diff] [blame] | 1804 | Builder.CreateStore(llvm::ConstantInt::get(IntTy, flags.getBitMask()), |
| 1805 | Builder.CreateStructGEP(addr, 2, "byref.flags")); |
| 1806 | |
John McCall | f0c11f7 | 2011-03-31 08:03:29 +0000 | [diff] [blame] | 1807 | CharUnits byrefSize = CGM.GetTargetTypeStoreSize(byrefType); |
| 1808 | V = llvm::ConstantInt::get(IntTy, byrefSize.getQuantity()); |
John McCall | 5af02db | 2011-03-31 01:59:53 +0000 | [diff] [blame] | 1809 | Builder.CreateStore(V, Builder.CreateStructGEP(addr, 3, "byref.size")); |
| 1810 | |
John McCall | f0c11f7 | 2011-03-31 08:03:29 +0000 | [diff] [blame] | 1811 | if (helpers) { |
John McCall | 5af02db | 2011-03-31 01:59:53 +0000 | [diff] [blame] | 1812 | llvm::Value *copy_helper = Builder.CreateStructGEP(addr, 4); |
John McCall | f0c11f7 | 2011-03-31 08:03:29 +0000 | [diff] [blame] | 1813 | Builder.CreateStore(helpers->CopyHelper, copy_helper); |
John McCall | 5af02db | 2011-03-31 01:59:53 +0000 | [diff] [blame] | 1814 | |
| 1815 | llvm::Value *destroy_helper = Builder.CreateStructGEP(addr, 5); |
John McCall | f0c11f7 | 2011-03-31 08:03:29 +0000 | [diff] [blame] | 1816 | Builder.CreateStore(helpers->DisposeHelper, destroy_helper); |
John McCall | 5af02db | 2011-03-31 01:59:53 +0000 | [diff] [blame] | 1817 | } |
| 1818 | } |
| 1819 | |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1820 | void CodeGenFunction::BuildBlockRelease(llvm::Value *V, BlockFieldFlags flags) { |
Daniel Dunbar | 673431a | 2010-07-16 00:00:15 +0000 | [diff] [blame] | 1821 | llvm::Value *F = CGM.getBlockObjectDispose(); |
Mike Stump | 1851b68 | 2009-03-06 04:53:30 +0000 | [diff] [blame] | 1822 | llvm::Value *N; |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1823 | V = Builder.CreateBitCast(V, Int8PtrTy); |
| 1824 | N = llvm::ConstantInt::get(Int32Ty, flags.getBitMask()); |
Mike Stump | 797b632 | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 1825 | Builder.CreateCall2(F, V, N); |
| 1826 | } |
John McCall | 5af02db | 2011-03-31 01:59:53 +0000 | [diff] [blame] | 1827 | |
| 1828 | namespace { |
| 1829 | struct CallBlockRelease : EHScopeStack::Cleanup { |
| 1830 | llvm::Value *Addr; |
| 1831 | CallBlockRelease(llvm::Value *Addr) : Addr(Addr) {} |
| 1832 | |
John McCall | ad346f4 | 2011-07-12 20:27:29 +0000 | [diff] [blame] | 1833 | void Emit(CodeGenFunction &CGF, Flags flags) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1834 | // Should we be passing FIELD_IS_WEAK here? |
John McCall | 5af02db | 2011-03-31 01:59:53 +0000 | [diff] [blame] | 1835 | CGF.BuildBlockRelease(Addr, BLOCK_FIELD_IS_BYREF); |
| 1836 | } |
| 1837 | }; |
| 1838 | } |
| 1839 | |
| 1840 | /// Enter a cleanup to destroy a __block variable. Note that this |
| 1841 | /// cleanup should be a no-op if the variable hasn't left the stack |
| 1842 | /// yet; if a cleanup is required for the variable itself, that needs |
| 1843 | /// to be done externally. |
| 1844 | void CodeGenFunction::enterByrefCleanup(const AutoVarEmission &emission) { |
| 1845 | // We don't enter this cleanup if we're in pure-GC mode. |
Douglas Gregor | e289d81 | 2011-09-13 17:21:33 +0000 | [diff] [blame] | 1846 | if (CGM.getLangOptions().getGC() == LangOptions::GCOnly) |
John McCall | 5af02db | 2011-03-31 01:59:53 +0000 | [diff] [blame] | 1847 | return; |
| 1848 | |
| 1849 | EHStack.pushCleanup<CallBlockRelease>(NormalAndEHCleanup, emission.Address); |
| 1850 | } |
John McCall | 13db5cf | 2011-09-09 20:41:01 +0000 | [diff] [blame] | 1851 | |
| 1852 | /// Adjust the declaration of something from the blocks API. |
| 1853 | static void configureBlocksRuntimeObject(CodeGenModule &CGM, |
| 1854 | llvm::Constant *C) { |
| 1855 | if (!CGM.getLangOptions().BlocksRuntimeOptional) return; |
| 1856 | |
| 1857 | llvm::GlobalValue *GV = cast<llvm::GlobalValue>(C->stripPointerCasts()); |
| 1858 | if (GV->isDeclaration() && |
| 1859 | GV->getLinkage() == llvm::GlobalValue::ExternalLinkage) |
| 1860 | GV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); |
| 1861 | } |
| 1862 | |
| 1863 | llvm::Constant *CodeGenModule::getBlockObjectDispose() { |
| 1864 | if (BlockObjectDispose) |
| 1865 | return BlockObjectDispose; |
| 1866 | |
| 1867 | llvm::Type *args[] = { Int8PtrTy, Int32Ty }; |
| 1868 | llvm::FunctionType *fty |
| 1869 | = llvm::FunctionType::get(VoidTy, args, false); |
| 1870 | BlockObjectDispose = CreateRuntimeFunction(fty, "_Block_object_dispose"); |
| 1871 | configureBlocksRuntimeObject(*this, BlockObjectDispose); |
| 1872 | return BlockObjectDispose; |
| 1873 | } |
| 1874 | |
| 1875 | llvm::Constant *CodeGenModule::getBlockObjectAssign() { |
| 1876 | if (BlockObjectAssign) |
| 1877 | return BlockObjectAssign; |
| 1878 | |
| 1879 | llvm::Type *args[] = { Int8PtrTy, Int8PtrTy, Int32Ty }; |
| 1880 | llvm::FunctionType *fty |
| 1881 | = llvm::FunctionType::get(VoidTy, args, false); |
| 1882 | BlockObjectAssign = CreateRuntimeFunction(fty, "_Block_object_assign"); |
| 1883 | configureBlocksRuntimeObject(*this, BlockObjectAssign); |
| 1884 | return BlockObjectAssign; |
| 1885 | } |
| 1886 | |
| 1887 | llvm::Constant *CodeGenModule::getNSConcreteGlobalBlock() { |
| 1888 | if (NSConcreteGlobalBlock) |
| 1889 | return NSConcreteGlobalBlock; |
| 1890 | |
| 1891 | NSConcreteGlobalBlock = GetOrCreateLLVMGlobal("_NSConcreteGlobalBlock", |
| 1892 | Int8PtrTy->getPointerTo(), 0); |
| 1893 | configureBlocksRuntimeObject(*this, NSConcreteGlobalBlock); |
| 1894 | return NSConcreteGlobalBlock; |
| 1895 | } |
| 1896 | |
| 1897 | llvm::Constant *CodeGenModule::getNSConcreteStackBlock() { |
| 1898 | if (NSConcreteStackBlock) |
| 1899 | return NSConcreteStackBlock; |
| 1900 | |
| 1901 | NSConcreteStackBlock = GetOrCreateLLVMGlobal("_NSConcreteStackBlock", |
| 1902 | Int8PtrTy->getPointerTo(), 0); |
| 1903 | configureBlocksRuntimeObject(*this, NSConcreteStackBlock); |
| 1904 | return NSConcreteStackBlock; |
| 1905 | } |