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