Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 1 | //===--- CGBlocks.cpp - Emit LLVM Code for declarations -------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This contains code to emit blocks. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 14 | #include "CGDebugInfo.h" |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 15 | #include "CodeGenFunction.h" |
Fariborz Jahanian | 263c4de | 2010-02-10 23:34:57 +0000 | [diff] [blame] | 16 | #include "CGObjCRuntime.h" |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 17 | #include "CodeGenModule.h" |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 18 | #include "CGBlocks.h" |
Mike Stump | 6cc88f7 | 2009-03-20 21:53:12 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 20 | #include "llvm/Module.h" |
Benjamin Kramer | 6876fe6 | 2010-03-31 15:04:05 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallSet.h" |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 22 | #include "llvm/Target/TargetData.h" |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 23 | #include <algorithm> |
Torok Edwin | f42e4a6 | 2009-08-24 13:25:12 +0000 | [diff] [blame] | 24 | |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 25 | using namespace clang; |
| 26 | using namespace CodeGen; |
| 27 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 28 | CGBlockInfo::CGBlockInfo(const BlockExpr *blockExpr, const char *N) |
| 29 | : Name(N), CXXThisIndex(0), CanBeGlobal(false), NeedsCopyDispose(false), |
John McCall | c20e204 | 2011-02-16 00:49:34 +0000 | [diff] [blame] | 30 | HasCXXObject(false), StructureType(0), Block(blockExpr) { |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 31 | |
| 32 | // Skip asm prefix, if any. |
| 33 | if (Name && Name[0] == '\01') |
| 34 | ++Name; |
| 35 | } |
| 36 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 37 | /// Build the given block as a global block. |
| 38 | static llvm::Constant *buildGlobalBlock(CodeGenModule &CGM, |
| 39 | const CGBlockInfo &blockInfo, |
| 40 | llvm::Constant *blockFn); |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 41 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 42 | /// Build the helper function to copy a block. |
| 43 | static llvm::Constant *buildCopyHelper(CodeGenModule &CGM, |
| 44 | const CGBlockInfo &blockInfo) { |
| 45 | return CodeGenFunction(CGM).GenerateCopyHelperFunction(blockInfo); |
| 46 | } |
| 47 | |
| 48 | /// Build the helper function to dipose of a block. |
| 49 | static llvm::Constant *buildDisposeHelper(CodeGenModule &CGM, |
| 50 | const CGBlockInfo &blockInfo) { |
| 51 | return CodeGenFunction(CGM).GenerateDestroyHelperFunction(blockInfo); |
| 52 | } |
| 53 | |
| 54 | /// Build the block descriptor constant for a block. |
| 55 | static llvm::Constant *buildBlockDescriptor(CodeGenModule &CGM, |
| 56 | const CGBlockInfo &blockInfo) { |
| 57 | ASTContext &C = CGM.getContext(); |
| 58 | |
| 59 | const llvm::Type *ulong = CGM.getTypes().ConvertType(C.UnsignedLongTy); |
| 60 | const llvm::Type *i8p = CGM.getTypes().ConvertType(C.VoidPtrTy); |
| 61 | |
| 62 | llvm::SmallVector<llvm::Constant*, 6> elements; |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 63 | |
| 64 | // reserved |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 65 | elements.push_back(llvm::ConstantInt::get(ulong, 0)); |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 66 | |
| 67 | // Size |
Mike Stump | d684000 | 2009-02-21 20:07:44 +0000 | [diff] [blame] | 68 | // FIXME: What is the right way to say this doesn't fit? We should give |
| 69 | // a user diagnostic in that case. Better fix would be to change the |
| 70 | // API to size_t. |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 71 | elements.push_back(llvm::ConstantInt::get(ulong, |
| 72 | blockInfo.BlockSize.getQuantity())); |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 73 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 74 | // Optional copy/dispose helpers. |
| 75 | if (blockInfo.NeedsCopyDispose) { |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 76 | // copy_func_helper_decl |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 77 | elements.push_back(buildCopyHelper(CGM, blockInfo)); |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 78 | |
| 79 | // destroy_func_decl |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 80 | elements.push_back(buildDisposeHelper(CGM, blockInfo)); |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 81 | } |
| 82 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 83 | // Signature. Mandatory ObjC-style method descriptor @encode sequence. |
| 84 | std::string typeAtEncoding = |
| 85 | CGM.getContext().getObjCEncodingForBlock(blockInfo.getBlockExpr()); |
| 86 | elements.push_back(llvm::ConstantExpr::getBitCast( |
| 87 | CGM.GetAddrOfConstantCString(typeAtEncoding), i8p)); |
Blaine Garst | 2a7eb28 | 2010-02-23 21:51:17 +0000 | [diff] [blame] | 88 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 89 | // GC layout. |
| 90 | if (C.getLangOptions().ObjC1) |
| 91 | elements.push_back(CGM.getObjCRuntime().BuildGCBlockLayout(CGM, blockInfo)); |
| 92 | else |
| 93 | elements.push_back(llvm::Constant::getNullValue(i8p)); |
Blaine Garst | 2a7eb28 | 2010-02-23 21:51:17 +0000 | [diff] [blame] | 94 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 95 | llvm::Constant *init = |
| 96 | llvm::ConstantStruct::get(CGM.getLLVMContext(), elements.data(), |
| 97 | elements.size(), false); |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 98 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 99 | llvm::GlobalVariable *global = |
| 100 | new llvm::GlobalVariable(CGM.getModule(), init->getType(), true, |
| 101 | llvm::GlobalValue::InternalLinkage, |
| 102 | init, "__block_descriptor_tmp"); |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 103 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 104 | return llvm::ConstantExpr::getBitCast(global, CGM.getBlockDescriptorType()); |
Anders Carlsson | 4de9fce | 2009-03-01 01:09:12 +0000 | [diff] [blame] | 105 | } |
| 106 | |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 107 | static BlockFlags computeBlockFlag(CodeGenModule &CGM, |
| 108 | const BlockExpr *BE, |
| 109 | BlockFlags flags) { |
| 110 | const FunctionType *ftype = BE->getFunctionType(); |
Fariborz Jahanian | 7edddb8 | 2010-07-28 19:07:18 +0000 | [diff] [blame] | 111 | |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 112 | // This is a bit overboard. |
| 113 | CallArgList args; |
| 114 | const CGFunctionInfo &fnInfo = |
| 115 | CGM.getTypes().getFunctionInfo(ftype->getResultType(), args, |
| 116 | ftype->getExtInfo()); |
| 117 | |
| 118 | if (CGM.ReturnTypeUsesSRet(fnInfo)) |
| 119 | flags |= BLOCK_USE_STRET; |
| 120 | |
Fariborz Jahanian | 7edddb8 | 2010-07-28 19:07:18 +0000 | [diff] [blame] | 121 | return flags; |
| 122 | } |
| 123 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 124 | /* |
| 125 | Purely notional variadic template describing the layout of a block. |
Anders Carlsson | 4de9fce | 2009-03-01 01:09:12 +0000 | [diff] [blame] | 126 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 127 | template <class _ResultType, class... _ParamTypes, class... _CaptureTypes> |
| 128 | struct Block_literal { |
| 129 | /// Initialized to one of: |
| 130 | /// extern void *_NSConcreteStackBlock[]; |
| 131 | /// extern void *_NSConcreteGlobalBlock[]; |
| 132 | /// |
| 133 | /// In theory, we could start one off malloc'ed by setting |
| 134 | /// BLOCK_NEEDS_FREE, giving it a refcount of 1, and using |
| 135 | /// this isa: |
| 136 | /// extern void *_NSConcreteMallocBlock[]; |
| 137 | struct objc_class *isa; |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 138 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 139 | /// These are the flags (with corresponding bit number) that the |
| 140 | /// compiler is actually supposed to know about. |
| 141 | /// 25. BLOCK_HAS_COPY_DISPOSE - indicates that the block |
| 142 | /// descriptor provides copy and dispose helper functions |
| 143 | /// 26. BLOCK_HAS_CXX_OBJ - indicates that there's a captured |
| 144 | /// object with a nontrivial destructor or copy constructor |
| 145 | /// 28. BLOCK_IS_GLOBAL - indicates that the block is allocated |
| 146 | /// as global memory |
| 147 | /// 29. BLOCK_USE_STRET - indicates that the block function |
| 148 | /// uses stret, which objc_msgSend needs to know about |
| 149 | /// 30. BLOCK_HAS_SIGNATURE - indicates that the block has an |
| 150 | /// @encoded signature string |
| 151 | /// And we're not supposed to manipulate these: |
| 152 | /// 24. BLOCK_NEEDS_FREE - indicates that the block has been moved |
| 153 | /// to malloc'ed memory |
| 154 | /// 27. BLOCK_IS_GC - indicates that the block has been moved to |
| 155 | /// to GC-allocated memory |
| 156 | /// Additionally, the bottom 16 bits are a reference count which |
| 157 | /// should be zero on the stack. |
| 158 | int flags; |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 159 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 160 | /// Reserved; should be zero-initialized. |
| 161 | int reserved; |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 162 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 163 | /// Function pointer generated from block literal. |
| 164 | _ResultType (*invoke)(Block_literal *, _ParamTypes...); |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 165 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 166 | /// Block description metadata generated from block literal. |
| 167 | struct Block_descriptor *block_descriptor; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 168 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 169 | /// Captured values follow. |
| 170 | _CapturesTypes captures...; |
| 171 | }; |
| 172 | */ |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 173 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 174 | /// The number of fields in a block header. |
| 175 | const unsigned BlockHeaderSize = 5; |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 176 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 177 | namespace { |
| 178 | /// A chunk of data that we actually have to capture in the block. |
| 179 | struct BlockLayoutChunk { |
| 180 | CharUnits Alignment; |
| 181 | CharUnits Size; |
| 182 | const BlockDecl::Capture *Capture; // null for 'this' |
| 183 | const llvm::Type *Type; |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 184 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 185 | BlockLayoutChunk(CharUnits align, CharUnits size, |
| 186 | const BlockDecl::Capture *capture, |
| 187 | const llvm::Type *type) |
| 188 | : Alignment(align), Size(size), Capture(capture), 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 | /// Tell the block info that this chunk has the given field index. |
| 191 | void setIndex(CGBlockInfo &info, unsigned index) { |
| 192 | if (!Capture) |
| 193 | info.CXXThisIndex = index; |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 194 | else |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 195 | info.Captures[Capture->getVariable()] |
| 196 | = CGBlockInfo::Capture::makeIndex(index); |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 197 | } |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 198 | }; |
Mike Stump | cf62d39 | 2009-03-06 18:42:23 +0000 | [diff] [blame] | 199 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 200 | /// Order by descending alignment. |
| 201 | bool operator<(const BlockLayoutChunk &left, const BlockLayoutChunk &right) { |
| 202 | return left.Alignment > right.Alignment; |
| 203 | } |
| 204 | } |
| 205 | |
John McCall | 461c9c1 | 2011-02-08 03:07:00 +0000 | [diff] [blame] | 206 | /// Determines if the given record type has a mutable field. |
| 207 | static bool hasMutableField(const CXXRecordDecl *record) { |
| 208 | for (CXXRecordDecl::field_iterator |
| 209 | i = record->field_begin(), e = record->field_end(); i != e; ++i) |
| 210 | if ((*i)->isMutable()) |
| 211 | return true; |
| 212 | |
| 213 | for (CXXRecordDecl::base_class_const_iterator |
| 214 | i = record->bases_begin(), e = record->bases_end(); i != e; ++i) { |
| 215 | const RecordType *record = i->getType()->castAs<RecordType>(); |
| 216 | if (hasMutableField(cast<CXXRecordDecl>(record->getDecl()))) |
| 217 | return true; |
| 218 | } |
| 219 | |
| 220 | return false; |
| 221 | } |
| 222 | |
| 223 | /// Determines if the given type is safe for constant capture in C++. |
| 224 | static bool isSafeForCXXConstantCapture(QualType type) { |
| 225 | const RecordType *recordType = |
| 226 | type->getBaseElementTypeUnsafe()->getAs<RecordType>(); |
| 227 | |
| 228 | // Only records can be unsafe. |
| 229 | if (!recordType) return true; |
| 230 | |
| 231 | const CXXRecordDecl *record = cast<CXXRecordDecl>(recordType->getDecl()); |
| 232 | |
| 233 | // Maintain semantics for classes with non-trivial dtors or copy ctors. |
| 234 | if (!record->hasTrivialDestructor()) return false; |
| 235 | if (!record->hasTrivialCopyConstructor()) return false; |
| 236 | |
| 237 | // Otherwise, we just have to make sure there aren't any mutable |
| 238 | // fields that might have changed since initialization. |
| 239 | return !hasMutableField(record); |
| 240 | } |
| 241 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 242 | /// It is illegal to modify a const object after initialization. |
| 243 | /// Therefore, if a const object has a constant initializer, we don't |
| 244 | /// actually need to keep storage for it in the block; we'll just |
| 245 | /// rematerialize it at the start of the block function. This is |
| 246 | /// acceptable because we make no promises about address stability of |
| 247 | /// captured variables. |
| 248 | static llvm::Constant *tryCaptureAsConstant(CodeGenModule &CGM, |
| 249 | const VarDecl *var) { |
| 250 | QualType type = var->getType(); |
| 251 | |
| 252 | // We can only do this if the variable is const. |
| 253 | if (!type.isConstQualified()) return 0; |
| 254 | |
John McCall | 461c9c1 | 2011-02-08 03:07:00 +0000 | [diff] [blame] | 255 | // Furthermore, in C++ we have to worry about mutable fields: |
| 256 | // C++ [dcl.type.cv]p4: |
| 257 | // Except that any class member declared mutable can be |
| 258 | // modified, any attempt to modify a const object during its |
| 259 | // lifetime results in undefined behavior. |
| 260 | if (CGM.getLangOptions().CPlusPlus && !isSafeForCXXConstantCapture(type)) |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 261 | return 0; |
| 262 | |
| 263 | // If the variable doesn't have any initializer (shouldn't this be |
| 264 | // invalid?), it's not clear what we should do. Maybe capture as |
| 265 | // zero? |
| 266 | const Expr *init = var->getInit(); |
| 267 | if (!init) return 0; |
| 268 | |
| 269 | return CGM.EmitConstantExpr(init, var->getType()); |
| 270 | } |
| 271 | |
| 272 | /// Get the low bit of a nonzero character count. This is the |
| 273 | /// alignment of the nth byte if the 0th byte is universally aligned. |
| 274 | static CharUnits getLowBit(CharUnits v) { |
| 275 | return CharUnits::fromQuantity(v.getQuantity() & (~v.getQuantity() + 1)); |
| 276 | } |
| 277 | |
| 278 | static void initializeForBlockHeader(CodeGenModule &CGM, CGBlockInfo &info, |
| 279 | std::vector<const llvm::Type*> &elementTypes) { |
| 280 | ASTContext &C = CGM.getContext(); |
| 281 | |
| 282 | // The header is basically a 'struct { void *; int; int; void *; void *; }'. |
| 283 | CharUnits ptrSize, ptrAlign, intSize, intAlign; |
| 284 | llvm::tie(ptrSize, ptrAlign) = C.getTypeInfoInChars(C.VoidPtrTy); |
| 285 | llvm::tie(intSize, intAlign) = C.getTypeInfoInChars(C.IntTy); |
| 286 | |
| 287 | // Are there crazy embedded platforms where this isn't true? |
| 288 | assert(intSize <= ptrSize && "layout assumptions horribly violated"); |
| 289 | |
| 290 | CharUnits headerSize = ptrSize; |
| 291 | if (2 * intSize < ptrAlign) headerSize += ptrSize; |
| 292 | else headerSize += 2 * intSize; |
| 293 | headerSize += 2 * ptrSize; |
| 294 | |
| 295 | info.BlockAlign = ptrAlign; |
| 296 | info.BlockSize = headerSize; |
| 297 | |
| 298 | assert(elementTypes.empty()); |
| 299 | const llvm::Type *i8p = CGM.getTypes().ConvertType(C.VoidPtrTy); |
| 300 | const llvm::Type *intTy = CGM.getTypes().ConvertType(C.IntTy); |
| 301 | elementTypes.push_back(i8p); |
| 302 | elementTypes.push_back(intTy); |
| 303 | elementTypes.push_back(intTy); |
| 304 | elementTypes.push_back(i8p); |
| 305 | elementTypes.push_back(CGM.getBlockDescriptorType()); |
| 306 | |
| 307 | assert(elementTypes.size() == BlockHeaderSize); |
| 308 | } |
| 309 | |
| 310 | /// Compute the layout of the given block. Attempts to lay the block |
| 311 | /// out with minimal space requirements. |
| 312 | static void computeBlockInfo(CodeGenModule &CGM, CGBlockInfo &info) { |
| 313 | ASTContext &C = CGM.getContext(); |
| 314 | const BlockDecl *block = info.getBlockDecl(); |
| 315 | |
| 316 | std::vector<const llvm::Type*> elementTypes; |
| 317 | initializeForBlockHeader(CGM, info, elementTypes); |
| 318 | |
| 319 | if (!block->hasCaptures()) { |
| 320 | info.StructureType = |
| 321 | llvm::StructType::get(CGM.getLLVMContext(), elementTypes, true); |
| 322 | info.CanBeGlobal = true; |
| 323 | return; |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 324 | } |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 325 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 326 | // Collect the layout chunks. |
| 327 | llvm::SmallVector<BlockLayoutChunk, 16> layout; |
| 328 | layout.reserve(block->capturesCXXThis() + |
| 329 | (block->capture_end() - block->capture_begin())); |
| 330 | |
| 331 | CharUnits maxFieldAlign; |
| 332 | |
| 333 | // First, 'this'. |
| 334 | if (block->capturesCXXThis()) { |
| 335 | const DeclContext *DC = block->getDeclContext(); |
| 336 | for (; isa<BlockDecl>(DC); DC = cast<BlockDecl>(DC)->getDeclContext()) |
| 337 | ; |
| 338 | QualType thisType = cast<CXXMethodDecl>(DC)->getThisType(C); |
| 339 | |
| 340 | const llvm::Type *llvmType = CGM.getTypes().ConvertType(thisType); |
| 341 | std::pair<CharUnits,CharUnits> tinfo |
| 342 | = CGM.getContext().getTypeInfoInChars(thisType); |
| 343 | maxFieldAlign = std::max(maxFieldAlign, tinfo.second); |
| 344 | |
| 345 | layout.push_back(BlockLayoutChunk(tinfo.second, tinfo.first, 0, llvmType)); |
| 346 | } |
| 347 | |
| 348 | // Next, all the block captures. |
| 349 | for (BlockDecl::capture_const_iterator ci = block->capture_begin(), |
| 350 | ce = block->capture_end(); ci != ce; ++ci) { |
| 351 | const VarDecl *variable = ci->getVariable(); |
| 352 | |
| 353 | if (ci->isByRef()) { |
| 354 | // We have to copy/dispose of the __block reference. |
| 355 | info.NeedsCopyDispose = true; |
| 356 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 357 | // Just use void* instead of a pointer to the byref type. |
| 358 | QualType byRefPtrTy = C.VoidPtrTy; |
| 359 | |
| 360 | const llvm::Type *llvmType = CGM.getTypes().ConvertType(byRefPtrTy); |
| 361 | std::pair<CharUnits,CharUnits> tinfo |
| 362 | = CGM.getContext().getTypeInfoInChars(byRefPtrTy); |
| 363 | maxFieldAlign = std::max(maxFieldAlign, tinfo.second); |
| 364 | |
| 365 | layout.push_back(BlockLayoutChunk(tinfo.second, tinfo.first, |
| 366 | &*ci, llvmType)); |
| 367 | continue; |
| 368 | } |
| 369 | |
| 370 | // Otherwise, build a layout chunk with the size and alignment of |
| 371 | // the declaration. |
| 372 | if (llvm::Constant *constant = tryCaptureAsConstant(CGM, variable)) { |
| 373 | info.Captures[variable] = CGBlockInfo::Capture::makeConstant(constant); |
| 374 | continue; |
| 375 | } |
| 376 | |
| 377 | // Block pointers require copy/dispose. |
| 378 | if (variable->getType()->isBlockPointerType()) { |
| 379 | info.NeedsCopyDispose = true; |
| 380 | |
| 381 | // So do Objective-C pointers. |
| 382 | } else if (variable->getType()->isObjCObjectPointerType() || |
| 383 | C.isObjCNSObjectType(variable->getType())) { |
| 384 | info.NeedsCopyDispose = true; |
| 385 | |
| 386 | // So do types that require non-trivial copy construction. |
| 387 | } else if (ci->hasCopyExpr()) { |
| 388 | info.NeedsCopyDispose = true; |
| 389 | info.HasCXXObject = true; |
| 390 | |
| 391 | // And so do types with destructors. |
| 392 | } else if (CGM.getLangOptions().CPlusPlus) { |
| 393 | if (const CXXRecordDecl *record = |
| 394 | variable->getType()->getAsCXXRecordDecl()) { |
| 395 | if (!record->hasTrivialDestructor()) { |
| 396 | info.HasCXXObject = true; |
| 397 | info.NeedsCopyDispose = true; |
| 398 | } |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | CharUnits size = C.getTypeSizeInChars(variable->getType()); |
| 403 | CharUnits align = C.getDeclAlign(variable); |
| 404 | maxFieldAlign = std::max(maxFieldAlign, align); |
| 405 | |
| 406 | const llvm::Type *llvmType = |
| 407 | CGM.getTypes().ConvertTypeForMem(variable->getType()); |
| 408 | |
| 409 | layout.push_back(BlockLayoutChunk(align, size, &*ci, llvmType)); |
| 410 | } |
| 411 | |
| 412 | // If that was everything, we're done here. |
| 413 | if (layout.empty()) { |
| 414 | info.StructureType = |
| 415 | llvm::StructType::get(CGM.getLLVMContext(), elementTypes, true); |
| 416 | info.CanBeGlobal = true; |
| 417 | return; |
| 418 | } |
| 419 | |
| 420 | // Sort the layout by alignment. We have to use a stable sort here |
| 421 | // to get reproducible results. There should probably be an |
| 422 | // llvm::array_pod_stable_sort. |
| 423 | std::stable_sort(layout.begin(), layout.end()); |
| 424 | |
| 425 | CharUnits &blockSize = info.BlockSize; |
| 426 | info.BlockAlign = std::max(maxFieldAlign, info.BlockAlign); |
| 427 | |
| 428 | // Assuming that the first byte in the header is maximally aligned, |
| 429 | // get the alignment of the first byte following the header. |
| 430 | CharUnits endAlign = getLowBit(blockSize); |
| 431 | |
| 432 | // If the end of the header isn't satisfactorily aligned for the |
| 433 | // maximum thing, look for things that are okay with the header-end |
| 434 | // alignment, and keep appending them until we get something that's |
| 435 | // aligned right. This algorithm is only guaranteed optimal if |
| 436 | // that condition is satisfied at some point; otherwise we can get |
| 437 | // things like: |
| 438 | // header // next byte has alignment 4 |
| 439 | // something_with_size_5; // next byte has alignment 1 |
| 440 | // something_with_alignment_8; |
| 441 | // which has 7 bytes of padding, as opposed to the naive solution |
| 442 | // which might have less (?). |
| 443 | if (endAlign < maxFieldAlign) { |
| 444 | llvm::SmallVectorImpl<BlockLayoutChunk>::iterator |
| 445 | li = layout.begin() + 1, le = layout.end(); |
| 446 | |
| 447 | // Look for something that the header end is already |
| 448 | // satisfactorily aligned for. |
| 449 | for (; li != le && endAlign < li->Alignment; ++li) |
| 450 | ; |
| 451 | |
| 452 | // If we found something that's naturally aligned for the end of |
| 453 | // the header, keep adding things... |
| 454 | if (li != le) { |
| 455 | llvm::SmallVectorImpl<BlockLayoutChunk>::iterator first = li; |
| 456 | for (; li != le; ++li) { |
| 457 | assert(endAlign >= li->Alignment); |
| 458 | |
| 459 | li->setIndex(info, elementTypes.size()); |
| 460 | elementTypes.push_back(li->Type); |
| 461 | blockSize += li->Size; |
| 462 | endAlign = getLowBit(blockSize); |
| 463 | |
| 464 | // ...until we get to the alignment of the maximum field. |
| 465 | if (endAlign >= maxFieldAlign) |
| 466 | break; |
| 467 | } |
| 468 | |
| 469 | // Don't re-append everything we just appended. |
| 470 | layout.erase(first, li); |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | // At this point, we just have to add padding if the end align still |
| 475 | // isn't aligned right. |
| 476 | if (endAlign < maxFieldAlign) { |
| 477 | CharUnits padding = maxFieldAlign - endAlign; |
| 478 | |
John McCall | 5936e33 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 479 | elementTypes.push_back(llvm::ArrayType::get(CGM.Int8Ty, |
| 480 | padding.getQuantity())); |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 481 | blockSize += padding; |
| 482 | |
| 483 | endAlign = getLowBit(blockSize); |
| 484 | assert(endAlign >= maxFieldAlign); |
| 485 | } |
| 486 | |
| 487 | // Slam everything else on now. This works because they have |
| 488 | // strictly decreasing alignment and we expect that size is always a |
| 489 | // multiple of alignment. |
| 490 | for (llvm::SmallVectorImpl<BlockLayoutChunk>::iterator |
| 491 | li = layout.begin(), le = layout.end(); li != le; ++li) { |
| 492 | assert(endAlign >= li->Alignment); |
| 493 | li->setIndex(info, elementTypes.size()); |
| 494 | elementTypes.push_back(li->Type); |
| 495 | blockSize += li->Size; |
| 496 | endAlign = getLowBit(blockSize); |
| 497 | } |
| 498 | |
| 499 | info.StructureType = |
| 500 | llvm::StructType::get(CGM.getLLVMContext(), elementTypes, true); |
| 501 | } |
| 502 | |
| 503 | /// Emit a block literal expression in the current function. |
| 504 | llvm::Value *CodeGenFunction::EmitBlockLiteral(const BlockExpr *blockExpr) { |
| 505 | std::string Name = CurFn->getName(); |
| 506 | CGBlockInfo blockInfo(blockExpr, Name.c_str()); |
| 507 | |
| 508 | // Compute information about the layout, etc., of this block. |
| 509 | computeBlockInfo(CGM, blockInfo); |
| 510 | |
| 511 | // Using that metadata, generate the actual block function. |
| 512 | llvm::Constant *blockFn |
| 513 | = CodeGenFunction(CGM).GenerateBlockFunction(CurGD, blockInfo, |
| 514 | CurFuncDecl, LocalDeclMap); |
John McCall | 5936e33 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 515 | blockFn = llvm::ConstantExpr::getBitCast(blockFn, VoidPtrTy); |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 516 | |
| 517 | // If there is nothing to capture, we can emit this as a global block. |
| 518 | if (blockInfo.CanBeGlobal) |
| 519 | return buildGlobalBlock(CGM, blockInfo, blockFn); |
| 520 | |
| 521 | // Otherwise, we have to emit this as a local block. |
| 522 | |
| 523 | llvm::Constant *isa = CGM.getNSConcreteStackBlock(); |
John McCall | 5936e33 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 524 | isa = llvm::ConstantExpr::getBitCast(isa, VoidPtrTy); |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 525 | |
| 526 | // Build the block descriptor. |
| 527 | llvm::Constant *descriptor = buildBlockDescriptor(CGM, blockInfo); |
| 528 | |
| 529 | const llvm::Type *intTy = ConvertType(getContext().IntTy); |
| 530 | |
| 531 | llvm::AllocaInst *blockAddr = |
| 532 | CreateTempAlloca(blockInfo.StructureType, "block"); |
| 533 | blockAddr->setAlignment(blockInfo.BlockAlign.getQuantity()); |
| 534 | |
| 535 | // Compute the initial on-stack block flags. |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 536 | BlockFlags flags = BLOCK_HAS_SIGNATURE; |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 537 | if (blockInfo.NeedsCopyDispose) flags |= BLOCK_HAS_COPY_DISPOSE; |
| 538 | if (blockInfo.HasCXXObject) flags |= BLOCK_HAS_CXX_OBJ; |
| 539 | flags = computeBlockFlag(CGM, blockInfo.getBlockExpr(), flags); |
| 540 | |
| 541 | // Initialize the block literal. |
| 542 | Builder.CreateStore(isa, Builder.CreateStructGEP(blockAddr, 0, "block.isa")); |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 543 | Builder.CreateStore(llvm::ConstantInt::get(intTy, flags.getBitMask()), |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 544 | Builder.CreateStructGEP(blockAddr, 1, "block.flags")); |
| 545 | Builder.CreateStore(llvm::ConstantInt::get(intTy, 0), |
| 546 | Builder.CreateStructGEP(blockAddr, 2, "block.reserved")); |
| 547 | Builder.CreateStore(blockFn, Builder.CreateStructGEP(blockAddr, 3, |
| 548 | "block.invoke")); |
| 549 | Builder.CreateStore(descriptor, Builder.CreateStructGEP(blockAddr, 4, |
| 550 | "block.descriptor")); |
| 551 | |
| 552 | // Finally, capture all the values into the block. |
| 553 | const BlockDecl *blockDecl = blockInfo.getBlockDecl(); |
| 554 | |
| 555 | // First, 'this'. |
| 556 | if (blockDecl->capturesCXXThis()) { |
| 557 | llvm::Value *addr = Builder.CreateStructGEP(blockAddr, |
| 558 | blockInfo.CXXThisIndex, |
| 559 | "block.captured-this.addr"); |
| 560 | Builder.CreateStore(LoadCXXThis(), addr); |
| 561 | } |
| 562 | |
| 563 | // Next, captured variables. |
| 564 | for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(), |
| 565 | ce = blockDecl->capture_end(); ci != ce; ++ci) { |
| 566 | const VarDecl *variable = ci->getVariable(); |
| 567 | const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable); |
| 568 | |
| 569 | // Ignore constant captures. |
| 570 | if (capture.isConstant()) continue; |
| 571 | |
| 572 | QualType type = variable->getType(); |
| 573 | |
| 574 | // This will be a [[type]]*, except that a byref entry will just be |
| 575 | // an i8**. |
| 576 | llvm::Value *blockField = |
| 577 | Builder.CreateStructGEP(blockAddr, capture.getIndex(), |
| 578 | "block.captured"); |
| 579 | |
| 580 | // Compute the address of the thing we're going to move into the |
| 581 | // block literal. |
| 582 | llvm::Value *src; |
| 583 | if (ci->isNested()) { |
| 584 | // We need to use the capture from the enclosing block. |
| 585 | const CGBlockInfo::Capture &enclosingCapture = |
| 586 | BlockInfo->getCapture(variable); |
| 587 | |
| 588 | // This is a [[type]]*, except that a byref entry wil just be an i8**. |
| 589 | src = Builder.CreateStructGEP(LoadBlockStruct(), |
| 590 | enclosingCapture.getIndex(), |
| 591 | "block.capture.addr"); |
| 592 | } else { |
| 593 | // This is a [[type]]*. |
| 594 | src = LocalDeclMap[variable]; |
| 595 | } |
| 596 | |
| 597 | // For byrefs, we just write the pointer to the byref struct into |
| 598 | // the block field. There's no need to chase the forwarding |
| 599 | // pointer at this point, since we're building something that will |
| 600 | // live a shorter life than the stack byref anyway. |
| 601 | if (ci->isByRef()) { |
John McCall | 5936e33 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 602 | // Get a void* that points to the byref struct. |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 603 | if (ci->isNested()) |
| 604 | src = Builder.CreateLoad(src, "byref.capture"); |
| 605 | else |
John McCall | 5936e33 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 606 | src = Builder.CreateBitCast(src, VoidPtrTy); |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 607 | |
John McCall | 5936e33 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 608 | // Write that void* into the capture field. |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 609 | Builder.CreateStore(src, blockField); |
| 610 | |
| 611 | // If we have a copy constructor, evaluate that into the block field. |
| 612 | } else if (const Expr *copyExpr = ci->getCopyExpr()) { |
| 613 | EmitSynthesizedCXXCopyCtor(blockField, src, copyExpr); |
| 614 | |
| 615 | // If it's a reference variable, copy the reference into the block field. |
| 616 | } else if (type->isReferenceType()) { |
| 617 | Builder.CreateStore(Builder.CreateLoad(src, "ref.val"), blockField); |
| 618 | |
| 619 | // Otherwise, fake up a POD copy into the block field. |
| 620 | } else { |
John McCall | bb699b0 | 2011-02-07 18:37:40 +0000 | [diff] [blame] | 621 | // We use one of these or the other depending on whether the |
| 622 | // reference is nested. |
| 623 | DeclRefExpr notNested(const_cast<VarDecl*>(variable), type, VK_LValue, |
| 624 | SourceLocation()); |
| 625 | BlockDeclRefExpr nested(const_cast<VarDecl*>(variable), type, |
| 626 | VK_LValue, SourceLocation(), /*byref*/ false); |
| 627 | |
| 628 | Expr *declRef = |
| 629 | (ci->isNested() ? static_cast<Expr*>(&nested) : ¬Nested); |
| 630 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 631 | ImplicitCastExpr l2r(ImplicitCastExpr::OnStack, type, CK_LValueToRValue, |
John McCall | bb699b0 | 2011-02-07 18:37:40 +0000 | [diff] [blame] | 632 | declRef, VK_RValue); |
John McCall | df04520 | 2011-03-08 09:38:48 +0000 | [diff] [blame] | 633 | EmitExprAsInit(&l2r, variable, blockField, |
| 634 | getContext().getDeclAlign(variable), |
| 635 | /*captured by init*/ false); |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | // Push a destructor if necessary. The semantics for when this |
| 639 | // actually gets run are really obscure. |
| 640 | if (!ci->isByRef() && CGM.getLangOptions().CPlusPlus) |
| 641 | PushDestructorCleanup(type, blockField); |
| 642 | } |
| 643 | |
| 644 | // Cast to the converted block-pointer type, which happens (somewhat |
| 645 | // unfortunately) to be a pointer to function type. |
| 646 | llvm::Value *result = |
| 647 | Builder.CreateBitCast(blockAddr, |
| 648 | ConvertType(blockInfo.getBlockExpr()->getType())); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 649 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 650 | return result; |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 654 | const llvm::Type *CodeGenModule::getBlockDescriptorType() { |
Mike Stump | ab69514 | 2009-02-13 15:16:56 +0000 | [diff] [blame] | 655 | if (BlockDescriptorType) |
| 656 | return BlockDescriptorType; |
| 657 | |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 658 | const llvm::Type *UnsignedLongTy = |
Mike Stump | ab69514 | 2009-02-13 15:16:56 +0000 | [diff] [blame] | 659 | getTypes().ConvertType(getContext().UnsignedLongTy); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 660 | |
Mike Stump | ab69514 | 2009-02-13 15:16:56 +0000 | [diff] [blame] | 661 | // struct __block_descriptor { |
| 662 | // unsigned long reserved; |
| 663 | // unsigned long block_size; |
Blaine Garst | 2a7eb28 | 2010-02-23 21:51:17 +0000 | [diff] [blame] | 664 | // |
| 665 | // // later, the following will be added |
| 666 | // |
| 667 | // struct { |
| 668 | // void (*copyHelper)(); |
| 669 | // void (*copyHelper)(); |
| 670 | // } helpers; // !!! optional |
| 671 | // |
| 672 | // const char *signature; // the block signature |
| 673 | // const char *layout; // reserved |
Mike Stump | ab69514 | 2009-02-13 15:16:56 +0000 | [diff] [blame] | 674 | // }; |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 675 | BlockDescriptorType = llvm::StructType::get(UnsignedLongTy->getContext(), |
| 676 | UnsignedLongTy, |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 677 | UnsignedLongTy, |
Mike Stump | ab69514 | 2009-02-13 15:16:56 +0000 | [diff] [blame] | 678 | NULL); |
| 679 | |
| 680 | getModule().addTypeName("struct.__block_descriptor", |
| 681 | BlockDescriptorType); |
| 682 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 683 | // Now form a pointer to that. |
| 684 | BlockDescriptorType = llvm::PointerType::getUnqual(BlockDescriptorType); |
Mike Stump | ab69514 | 2009-02-13 15:16:56 +0000 | [diff] [blame] | 685 | return BlockDescriptorType; |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 686 | } |
| 687 | |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 688 | const llvm::Type *CodeGenModule::getGenericBlockLiteralType() { |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 689 | if (GenericBlockLiteralType) |
| 690 | return GenericBlockLiteralType; |
| 691 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 692 | const llvm::Type *BlockDescPtrTy = getBlockDescriptorType(); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 693 | |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 694 | // struct __block_literal_generic { |
Mike Stump | bd65cac | 2009-02-19 01:01:04 +0000 | [diff] [blame] | 695 | // void *__isa; |
| 696 | // int __flags; |
| 697 | // int __reserved; |
| 698 | // void (*__invoke)(void *); |
| 699 | // struct __block_descriptor *__descriptor; |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 700 | // }; |
John McCall | 5936e33 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 701 | GenericBlockLiteralType = llvm::StructType::get(getLLVMContext(), |
| 702 | VoidPtrTy, |
Mike Stump | 7cbb360 | 2009-02-13 16:01:35 +0000 | [diff] [blame] | 703 | IntTy, |
| 704 | IntTy, |
John McCall | 5936e33 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 705 | VoidPtrTy, |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 706 | BlockDescPtrTy, |
| 707 | NULL); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 708 | |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 709 | getModule().addTypeName("struct.__block_literal_generic", |
| 710 | GenericBlockLiteralType); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 711 | |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 712 | return GenericBlockLiteralType; |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 713 | } |
| 714 | |
Mike Stump | bd65cac | 2009-02-19 01:01:04 +0000 | [diff] [blame] | 715 | |
Anders Carlsson | a1736c0 | 2009-12-24 21:13:40 +0000 | [diff] [blame] | 716 | RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E, |
| 717 | ReturnValueSlot ReturnValue) { |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 718 | const BlockPointerType *BPT = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 719 | E->getCallee()->getType()->getAs<BlockPointerType>(); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 720 | |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 721 | llvm::Value *Callee = EmitScalarExpr(E->getCallee()); |
| 722 | |
| 723 | // Get a pointer to the generic block literal. |
| 724 | const llvm::Type *BlockLiteralTy = |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 725 | llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType()); |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 726 | |
| 727 | // Bitcast the callee to a block literal. |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 728 | llvm::Value *BlockLiteral = |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 729 | Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal"); |
| 730 | |
| 731 | // Get the function pointer from the literal. |
| 732 | llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp"); |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 733 | |
John McCall | 5936e33 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 734 | BlockLiteral = Builder.CreateBitCast(BlockLiteral, VoidPtrTy, "tmp"); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 735 | |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 736 | // Add the block literal. |
| 737 | QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy); |
| 738 | CallArgList Args; |
| 739 | Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy)); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 740 | |
Anders Carlsson | 782f397 | 2009-04-08 23:13:16 +0000 | [diff] [blame] | 741 | QualType FnType = BPT->getPointeeType(); |
| 742 | |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 743 | // And the rest of the arguments. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 744 | EmitCallArgs(Args, FnType->getAs<FunctionProtoType>(), |
Anders Carlsson | 782f397 | 2009-04-08 23:13:16 +0000 | [diff] [blame] | 745 | E->arg_begin(), E->arg_end()); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 746 | |
Anders Carlsson | 6e460ff | 2009-04-07 22:10:22 +0000 | [diff] [blame] | 747 | // Load the function. |
Daniel Dunbar | 2da84ff | 2009-11-29 21:23:36 +0000 | [diff] [blame] | 748 | llvm::Value *Func = Builder.CreateLoad(FuncPtr, "tmp"); |
Anders Carlsson | 6e460ff | 2009-04-07 22:10:22 +0000 | [diff] [blame] | 749 | |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 750 | const FunctionType *FuncTy = FnType->getAs<FunctionType>(); |
| 751 | QualType ResultType = FuncTy->getResultType(); |
Anders Carlsson | a17d7cc | 2009-04-08 02:55:55 +0000 | [diff] [blame] | 752 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 753 | const CGFunctionInfo &FnInfo = |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 754 | CGM.getTypes().getFunctionInfo(ResultType, Args, |
| 755 | FuncTy->getExtInfo()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 756 | |
Anders Carlsson | 6e460ff | 2009-04-07 22:10:22 +0000 | [diff] [blame] | 757 | // Cast the function pointer to the right type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 758 | const llvm::Type *BlockFTy = |
Anders Carlsson | a17d7cc | 2009-04-08 02:55:55 +0000 | [diff] [blame] | 759 | CGM.getTypes().GetFunctionType(FnInfo, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 760 | |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 761 | const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy); |
Anders Carlsson | 6e460ff | 2009-04-07 22:10:22 +0000 | [diff] [blame] | 762 | Func = Builder.CreateBitCast(Func, BlockFTyPtr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 763 | |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 764 | // And call the block. |
Anders Carlsson | a1736c0 | 2009-12-24 21:13:40 +0000 | [diff] [blame] | 765 | return EmitCall(FnInfo, Func, ReturnValue, Args); |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 766 | } |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 767 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 768 | llvm::Value *CodeGenFunction::GetAddrOfBlockDecl(const VarDecl *variable, |
| 769 | bool isByRef) { |
| 770 | assert(BlockInfo && "evaluating block ref without block information?"); |
| 771 | const CGBlockInfo::Capture &capture = BlockInfo->getCapture(variable); |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 772 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 773 | // Handle constant captures. |
| 774 | if (capture.isConstant()) return LocalDeclMap[variable]; |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 775 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 776 | llvm::Value *addr = |
| 777 | Builder.CreateStructGEP(LoadBlockStruct(), capture.getIndex(), |
| 778 | "block.capture.addr"); |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 779 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 780 | if (isByRef) { |
| 781 | // addr should be a void** right now. Load, then cast the result |
| 782 | // to byref*. |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 783 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 784 | addr = Builder.CreateLoad(addr); |
| 785 | const llvm::PointerType *byrefPointerType |
| 786 | = llvm::PointerType::get(BuildByRefType(variable), 0); |
| 787 | addr = Builder.CreateBitCast(addr, byrefPointerType, |
| 788 | "byref.addr"); |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 789 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 790 | // Follow the forwarding pointer. |
| 791 | addr = Builder.CreateStructGEP(addr, 1, "byref.forwarding"); |
| 792 | addr = Builder.CreateLoad(addr, "byref.addr.forwarded"); |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 793 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 794 | // Cast back to byref* and GEP over to the actual object. |
| 795 | addr = Builder.CreateBitCast(addr, byrefPointerType); |
| 796 | addr = Builder.CreateStructGEP(addr, getByRefValueLLVMField(variable), |
| 797 | variable->getNameAsString()); |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 798 | } |
| 799 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 800 | if (variable->getType()->isReferenceType()) |
| 801 | addr = Builder.CreateLoad(addr, "ref.tmp"); |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 802 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 803 | return addr; |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 804 | } |
| 805 | |
Mike Stump | 67a6448 | 2009-02-14 22:16:35 +0000 | [diff] [blame] | 806 | llvm::Constant * |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 807 | CodeGenModule::GetAddrOfGlobalBlock(const BlockExpr *blockExpr, |
John McCall | 5936e33 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 808 | const char *name) { |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 809 | CGBlockInfo blockInfo(blockExpr, name); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 810 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 811 | // Compute information about the layout, etc., of this block. |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 812 | computeBlockInfo(*this, blockInfo); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 813 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 814 | // Using that metadata, generate the actual block function. |
| 815 | llvm::Constant *blockFn; |
| 816 | { |
| 817 | llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap; |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 818 | blockFn = CodeGenFunction(*this).GenerateBlockFunction(GlobalDecl(), |
| 819 | blockInfo, |
| 820 | 0, LocalDeclMap); |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 821 | } |
John McCall | 5936e33 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 822 | blockFn = llvm::ConstantExpr::getBitCast(blockFn, VoidPtrTy); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 823 | |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 824 | return buildGlobalBlock(*this, blockInfo, blockFn); |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 825 | } |
| 826 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 827 | static llvm::Constant *buildGlobalBlock(CodeGenModule &CGM, |
| 828 | const CGBlockInfo &blockInfo, |
| 829 | llvm::Constant *blockFn) { |
| 830 | assert(blockInfo.CanBeGlobal); |
| 831 | |
| 832 | // Generate the constants for the block literal initializer. |
| 833 | llvm::Constant *fields[BlockHeaderSize]; |
| 834 | |
| 835 | // isa |
| 836 | fields[0] = CGM.getNSConcreteGlobalBlock(); |
| 837 | |
| 838 | // __flags |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 839 | BlockFlags flags = computeBlockFlag(CGM, blockInfo.getBlockExpr(), |
| 840 | BLOCK_IS_GLOBAL | BLOCK_HAS_SIGNATURE); |
John McCall | 5936e33 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 841 | fields[1] = llvm::ConstantInt::get(CGM.IntTy, flags.getBitMask()); |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 842 | |
| 843 | // Reserved |
John McCall | 5936e33 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 844 | fields[2] = llvm::Constant::getNullValue(CGM.IntTy); |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 845 | |
| 846 | // Function |
| 847 | fields[3] = blockFn; |
| 848 | |
| 849 | // Descriptor |
| 850 | fields[4] = buildBlockDescriptor(CGM, blockInfo); |
| 851 | |
| 852 | llvm::Constant *init = |
| 853 | llvm::ConstantStruct::get(CGM.getLLVMContext(), fields, BlockHeaderSize, |
| 854 | /*packed*/ false); |
| 855 | |
| 856 | llvm::GlobalVariable *literal = |
| 857 | new llvm::GlobalVariable(CGM.getModule(), |
| 858 | init->getType(), |
| 859 | /*constant*/ true, |
| 860 | llvm::GlobalVariable::InternalLinkage, |
| 861 | init, |
| 862 | "__block_literal_global"); |
| 863 | literal->setAlignment(blockInfo.BlockAlign.getQuantity()); |
| 864 | |
| 865 | // Return a constant of the appropriately-casted type. |
| 866 | const llvm::Type *requiredType = |
| 867 | CGM.getTypes().ConvertType(blockInfo.getBlockExpr()->getType()); |
| 868 | return llvm::ConstantExpr::getBitCast(literal, requiredType); |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 869 | } |
| 870 | |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 871 | llvm::Function * |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 872 | CodeGenFunction::GenerateBlockFunction(GlobalDecl GD, |
| 873 | const CGBlockInfo &blockInfo, |
| 874 | const Decl *outerFnDecl, |
| 875 | const DeclMapTy &ldm) { |
| 876 | const BlockDecl *blockDecl = blockInfo.getBlockDecl(); |
Devang Patel | 963dfbd | 2009-04-15 21:51:44 +0000 | [diff] [blame] | 877 | |
Devang Patel | 6d1155b | 2011-03-07 21:53:18 +0000 | [diff] [blame] | 878 | // Check if we should generate debug info for this block function. |
| 879 | if (CGM.getModuleDebugInfo()) |
| 880 | DebugInfo = CGM.getModuleDebugInfo(); |
| 881 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 882 | BlockInfo = &blockInfo; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 883 | |
Mike Stump | 7f28a9c | 2009-03-13 23:34:28 +0000 | [diff] [blame] | 884 | // Arrange for local static and local extern declarations to appear |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 885 | // to be local to this function as well, in case they're directly |
| 886 | // referenced in a block. |
| 887 | for (DeclMapTy::const_iterator i = ldm.begin(), e = ldm.end(); i != e; ++i) { |
| 888 | const VarDecl *var = dyn_cast<VarDecl>(i->first); |
| 889 | if (var && !var->hasLocalStorage()) |
| 890 | LocalDeclMap[var] = i->second; |
Mike Stump | 7f28a9c | 2009-03-13 23:34:28 +0000 | [diff] [blame] | 891 | } |
| 892 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 893 | // Begin building the function declaration. |
Eli Friedman | 48f9122 | 2009-03-28 03:24:54 +0000 | [diff] [blame] | 894 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 895 | // Build the argument list. |
| 896 | FunctionArgList args; |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 897 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 898 | // The first argument is the block pointer. Just take it as a void* |
| 899 | // and cast it later. |
| 900 | QualType selfTy = getContext().VoidPtrTy; |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 901 | IdentifierInfo *II = &CGM.getContext().Idents.get(".block_descriptor"); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 902 | |
John McCall | 8178df3 | 2011-02-22 22:38:33 +0000 | [diff] [blame] | 903 | ImplicitParamDecl selfDecl(const_cast<BlockDecl*>(blockDecl), |
| 904 | SourceLocation(), II, selfTy); |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 905 | args.push_back(&selfDecl); |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 906 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 907 | // Now add the rest of the parameters. |
| 908 | for (BlockDecl::param_const_iterator i = blockDecl->param_begin(), |
| 909 | e = blockDecl->param_end(); i != e; ++i) |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 910 | args.push_back(*i); |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 911 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 912 | // Create the function declaration. |
| 913 | const FunctionProtoType *fnType = |
| 914 | cast<FunctionProtoType>(blockInfo.getBlockExpr()->getFunctionType()); |
| 915 | const CGFunctionInfo &fnInfo = |
| 916 | CGM.getTypes().getFunctionInfo(fnType->getResultType(), args, |
| 917 | fnType->getExtInfo()); |
| 918 | const llvm::FunctionType *fnLLVMType = |
| 919 | CGM.getTypes().GetFunctionType(fnInfo, fnType->isVariadic()); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 920 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 921 | MangleBuffer name; |
| 922 | CGM.getBlockMangledName(GD, name, blockDecl); |
| 923 | llvm::Function *fn = |
| 924 | llvm::Function::Create(fnLLVMType, llvm::GlobalValue::InternalLinkage, |
| 925 | name.getString(), &CGM.getModule()); |
| 926 | CGM.SetInternalFunctionAttributes(blockDecl, fn, fnInfo); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 927 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 928 | // Begin generating the function. |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 929 | StartFunction(blockDecl, fnType->getResultType(), fn, fnInfo, args, |
Tilmann Scheller | 9c6082f | 2011-03-02 21:36:49 +0000 | [diff] [blame] | 930 | blockInfo.getBlockExpr()->getBody()->getLocEnd()); |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 931 | CurFuncDecl = outerFnDecl; // StartFunction sets this to blockDecl |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 932 | |
John McCall | 8178df3 | 2011-02-22 22:38:33 +0000 | [diff] [blame] | 933 | // Okay. Undo some of what StartFunction did. |
| 934 | |
| 935 | // Pull the 'self' reference out of the local decl map. |
| 936 | llvm::Value *blockAddr = LocalDeclMap[&selfDecl]; |
| 937 | LocalDeclMap.erase(&selfDecl); |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 938 | BlockPointer = Builder.CreateBitCast(blockAddr, |
| 939 | blockInfo.StructureType->getPointerTo(), |
| 940 | "block"); |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 941 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 942 | // If we have a C++ 'this' reference, go ahead and force it into |
| 943 | // existence now. |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 944 | if (blockDecl->capturesCXXThis()) { |
| 945 | llvm::Value *addr = Builder.CreateStructGEP(BlockPointer, |
| 946 | blockInfo.CXXThisIndex, |
| 947 | "block.captured-this"); |
| 948 | CXXThisValue = Builder.CreateLoad(addr, "this"); |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 949 | } |
| 950 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 951 | // LoadObjCSelf() expects there to be an entry for 'self' in LocalDeclMap; |
| 952 | // appease it. |
| 953 | if (const ObjCMethodDecl *method |
| 954 | = dyn_cast_or_null<ObjCMethodDecl>(CurFuncDecl)) { |
| 955 | const VarDecl *self = method->getSelfDecl(); |
| 956 | |
| 957 | // There might not be a capture for 'self', but if there is... |
| 958 | if (blockInfo.Captures.count(self)) { |
| 959 | const CGBlockInfo::Capture &capture = blockInfo.getCapture(self); |
| 960 | llvm::Value *selfAddr = Builder.CreateStructGEP(BlockPointer, |
| 961 | capture.getIndex(), |
| 962 | "block.captured-self"); |
| 963 | LocalDeclMap[self] = selfAddr; |
| 964 | } |
| 965 | } |
| 966 | |
| 967 | // Also force all the constant captures. |
| 968 | for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(), |
| 969 | ce = blockDecl->capture_end(); ci != ce; ++ci) { |
| 970 | const VarDecl *variable = ci->getVariable(); |
| 971 | const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable); |
| 972 | if (!capture.isConstant()) continue; |
| 973 | |
| 974 | unsigned align = getContext().getDeclAlign(variable).getQuantity(); |
| 975 | |
| 976 | llvm::AllocaInst *alloca = |
| 977 | CreateMemTemp(variable->getType(), "block.captured-const"); |
| 978 | alloca->setAlignment(align); |
| 979 | |
| 980 | Builder.CreateStore(capture.getConstant(), alloca, align); |
| 981 | |
| 982 | LocalDeclMap[variable] = alloca; |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 983 | } |
| 984 | |
Mike Stump | b289b3f | 2009-10-01 22:29:41 +0000 | [diff] [blame] | 985 | // Save a spot to insert the debug information for all the BlockDeclRefDecls. |
| 986 | llvm::BasicBlock *entry = Builder.GetInsertBlock(); |
| 987 | llvm::BasicBlock::iterator entry_ptr = Builder.GetInsertPoint(); |
| 988 | --entry_ptr; |
| 989 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 990 | EmitStmt(blockDecl->getBody()); |
Mike Stump | b289b3f | 2009-10-01 22:29:41 +0000 | [diff] [blame] | 991 | |
Mike Stump | de8c5c7 | 2009-10-01 00:27:30 +0000 | [diff] [blame] | 992 | // Remember where we were... |
| 993 | llvm::BasicBlock *resume = Builder.GetInsertBlock(); |
Mike Stump | b289b3f | 2009-10-01 22:29:41 +0000 | [diff] [blame] | 994 | |
Mike Stump | de8c5c7 | 2009-10-01 00:27:30 +0000 | [diff] [blame] | 995 | // Go back to the entry. |
Mike Stump | b289b3f | 2009-10-01 22:29:41 +0000 | [diff] [blame] | 996 | ++entry_ptr; |
| 997 | Builder.SetInsertPoint(entry, entry_ptr); |
| 998 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 999 | // Emit debug information for all the BlockDeclRefDecls. |
| 1000 | // FIXME: also for 'this' |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 1001 | if (CGDebugInfo *DI = getDebugInfo()) { |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1002 | for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(), |
| 1003 | ce = blockDecl->capture_end(); ci != ce; ++ci) { |
| 1004 | const VarDecl *variable = ci->getVariable(); |
| 1005 | DI->setLocation(variable->getLocation()); |
| 1006 | |
| 1007 | const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable); |
| 1008 | if (capture.isConstant()) { |
| 1009 | DI->EmitDeclareOfAutoVariable(variable, LocalDeclMap[variable], |
| 1010 | Builder); |
| 1011 | continue; |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 1012 | } |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1013 | |
John McCall | 8178df3 | 2011-02-22 22:38:33 +0000 | [diff] [blame] | 1014 | DI->EmitDeclareOfBlockDeclRefVariable(variable, BlockPointer, |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1015 | Builder, blockInfo); |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 1016 | } |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 1017 | } |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1018 | |
Mike Stump | de8c5c7 | 2009-10-01 00:27:30 +0000 | [diff] [blame] | 1019 | // And resume where we left off. |
| 1020 | if (resume == 0) |
| 1021 | Builder.ClearInsertionPoint(); |
| 1022 | else |
| 1023 | Builder.SetInsertPoint(resume); |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 1024 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1025 | FinishFunction(cast<CompoundStmt>(blockDecl->getBody())->getRBracLoc()); |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 1026 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1027 | return fn; |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 1028 | } |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 1029 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1030 | /* |
| 1031 | notes.push_back(HelperInfo()); |
| 1032 | HelperInfo ¬e = notes.back(); |
| 1033 | note.index = capture.getIndex(); |
| 1034 | note.RequiresCopying = (ci->hasCopyExpr() || BlockRequiresCopying(type)); |
| 1035 | note.cxxbar_import = ci->getCopyExpr(); |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 1036 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1037 | if (ci->isByRef()) { |
| 1038 | note.flag = BLOCK_FIELD_IS_BYREF; |
| 1039 | if (type.isObjCGCWeak()) |
| 1040 | note.flag |= BLOCK_FIELD_IS_WEAK; |
| 1041 | } else if (type->isBlockPointerType()) { |
| 1042 | note.flag = BLOCK_FIELD_IS_BLOCK; |
| 1043 | } else { |
| 1044 | note.flag = BLOCK_FIELD_IS_OBJECT; |
| 1045 | } |
| 1046 | */ |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 1047 | |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 1048 | |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 1049 | |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 1050 | |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 1051 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1052 | llvm::Constant * |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1053 | CodeGenFunction::GenerateCopyHelperFunction(const CGBlockInfo &blockInfo) { |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1054 | ASTContext &C = getContext(); |
| 1055 | |
| 1056 | FunctionArgList args; |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 1057 | ImplicitParamDecl dstDecl(0, SourceLocation(), 0, C.VoidPtrTy); |
| 1058 | args.push_back(&dstDecl); |
| 1059 | ImplicitParamDecl srcDecl(0, SourceLocation(), 0, C.VoidPtrTy); |
| 1060 | args.push_back(&srcDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1061 | |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 1062 | const CGFunctionInfo &FI = |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1063 | CGM.getTypes().getFunctionInfo(C.VoidTy, args, FunctionType::ExtInfo()); |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 1064 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1065 | // FIXME: it would be nice if these were mergeable with things with |
| 1066 | // identical semantics. |
| 1067 | const llvm::FunctionType *LTy = CGM.getTypes().GetFunctionType(FI, false); |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 1068 | |
| 1069 | llvm::Function *Fn = |
| 1070 | llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, |
Benjamin Kramer | 3cf7c5d | 2010-01-22 13:59:13 +0000 | [diff] [blame] | 1071 | "__copy_helper_block_", &CGM.getModule()); |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 1072 | |
| 1073 | IdentifierInfo *II |
| 1074 | = &CGM.getContext().Idents.get("__copy_helper_block_"); |
| 1075 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1076 | FunctionDecl *FD = FunctionDecl::Create(C, |
| 1077 | C.getTranslationUnitDecl(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1078 | SourceLocation(), |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1079 | SourceLocation(), II, C.VoidTy, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1080 | SC_Static, |
| 1081 | SC_None, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 1082 | false, |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 1083 | true); |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 1084 | StartFunction(FD, C.VoidTy, Fn, FI, args, SourceLocation()); |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 1085 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1086 | const llvm::Type *structPtrTy = blockInfo.StructureType->getPointerTo(); |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 1087 | |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 1088 | llvm::Value *src = GetAddrOfLocalVar(&srcDecl); |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1089 | src = Builder.CreateLoad(src); |
| 1090 | src = Builder.CreateBitCast(src, structPtrTy, "block.source"); |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 1091 | |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 1092 | llvm::Value *dst = GetAddrOfLocalVar(&dstDecl); |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1093 | dst = Builder.CreateLoad(dst); |
| 1094 | dst = Builder.CreateBitCast(dst, structPtrTy, "block.dest"); |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 1095 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1096 | const BlockDecl *blockDecl = blockInfo.getBlockDecl(); |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 1097 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1098 | for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(), |
| 1099 | ce = blockDecl->capture_end(); ci != ce; ++ci) { |
| 1100 | const VarDecl *variable = ci->getVariable(); |
| 1101 | QualType type = variable->getType(); |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 1102 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1103 | const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable); |
| 1104 | if (capture.isConstant()) continue; |
| 1105 | |
| 1106 | const Expr *copyExpr = ci->getCopyExpr(); |
| 1107 | unsigned flags = 0; |
| 1108 | |
| 1109 | if (copyExpr) { |
| 1110 | assert(!ci->isByRef()); |
| 1111 | // don't bother computing flags |
| 1112 | } else if (ci->isByRef()) { |
| 1113 | flags = BLOCK_FIELD_IS_BYREF; |
| 1114 | if (type.isObjCGCWeak()) flags |= BLOCK_FIELD_IS_WEAK; |
| 1115 | } else if (type->isBlockPointerType()) { |
| 1116 | flags = BLOCK_FIELD_IS_BLOCK; |
| 1117 | } else if (type->isObjCObjectPointerType() || C.isObjCNSObjectType(type)) { |
| 1118 | flags = BLOCK_FIELD_IS_OBJECT; |
| 1119 | } |
| 1120 | |
| 1121 | if (!copyExpr && !flags) continue; |
| 1122 | |
| 1123 | unsigned index = capture.getIndex(); |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1124 | llvm::Value *srcField = Builder.CreateStructGEP(src, index); |
| 1125 | llvm::Value *dstField = Builder.CreateStructGEP(dst, index); |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1126 | |
| 1127 | // If there's an explicit copy expression, we do that. |
| 1128 | if (copyExpr) { |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1129 | EmitSynthesizedCXXCopyCtor(dstField, srcField, copyExpr); |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1130 | } else { |
| 1131 | llvm::Value *srcValue = Builder.CreateLoad(srcField, "blockcopy.src"); |
John McCall | 5936e33 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 1132 | srcValue = Builder.CreateBitCast(srcValue, VoidPtrTy); |
| 1133 | llvm::Value *dstAddr = Builder.CreateBitCast(dstField, VoidPtrTy); |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1134 | Builder.CreateCall3(CGM.getBlockObjectAssign(), dstAddr, srcValue, |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1135 | llvm::ConstantInt::get(Int32Ty, flags)); |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 1136 | } |
| 1137 | } |
| 1138 | |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1139 | FinishFunction(); |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 1140 | |
John McCall | 5936e33 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 1141 | return llvm::ConstantExpr::getBitCast(Fn, VoidPtrTy); |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 1142 | } |
| 1143 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1144 | llvm::Constant * |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1145 | CodeGenFunction::GenerateDestroyHelperFunction(const CGBlockInfo &blockInfo) { |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1146 | ASTContext &C = getContext(); |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 1147 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1148 | FunctionArgList args; |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 1149 | ImplicitParamDecl srcDecl(0, SourceLocation(), 0, C.VoidPtrTy); |
| 1150 | args.push_back(&srcDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1151 | |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 1152 | const CGFunctionInfo &FI = |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1153 | CGM.getTypes().getFunctionInfo(C.VoidTy, args, FunctionType::ExtInfo()); |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 1154 | |
Mike Stump | 3899a7f | 2009-06-05 23:26:36 +0000 | [diff] [blame] | 1155 | // FIXME: We'd like to put these into a mergable by content, with |
| 1156 | // internal linkage. |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1157 | const llvm::FunctionType *LTy = CGM.getTypes().GetFunctionType(FI, false); |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 1158 | |
| 1159 | llvm::Function *Fn = |
| 1160 | llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, |
Benjamin Kramer | 3cf7c5d | 2010-01-22 13:59:13 +0000 | [diff] [blame] | 1161 | "__destroy_helper_block_", &CGM.getModule()); |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 1162 | |
| 1163 | IdentifierInfo *II |
| 1164 | = &CGM.getContext().Idents.get("__destroy_helper_block_"); |
| 1165 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1166 | FunctionDecl *FD = FunctionDecl::Create(C, C.getTranslationUnitDecl(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1167 | SourceLocation(), |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1168 | SourceLocation(), II, C.VoidTy, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1169 | SC_Static, |
| 1170 | SC_None, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 1171 | false, true); |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 1172 | StartFunction(FD, C.VoidTy, Fn, FI, args, SourceLocation()); |
Mike Stump | 1edf6b6 | 2009-03-07 02:53:18 +0000 | [diff] [blame] | 1173 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1174 | const llvm::Type *structPtrTy = blockInfo.StructureType->getPointerTo(); |
Mike Stump | 1edf6b6 | 2009-03-07 02:53:18 +0000 | [diff] [blame] | 1175 | |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 1176 | llvm::Value *src = GetAddrOfLocalVar(&srcDecl); |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1177 | src = Builder.CreateLoad(src); |
| 1178 | src = Builder.CreateBitCast(src, structPtrTy, "block"); |
Mike Stump | 1edf6b6 | 2009-03-07 02:53:18 +0000 | [diff] [blame] | 1179 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1180 | const BlockDecl *blockDecl = blockInfo.getBlockDecl(); |
| 1181 | |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1182 | CodeGenFunction::RunCleanupsScope cleanups(*this); |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1183 | |
| 1184 | for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(), |
| 1185 | ce = blockDecl->capture_end(); ci != ce; ++ci) { |
| 1186 | const VarDecl *variable = ci->getVariable(); |
| 1187 | QualType type = variable->getType(); |
| 1188 | |
| 1189 | const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable); |
| 1190 | if (capture.isConstant()) continue; |
| 1191 | |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1192 | BlockFieldFlags flags; |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1193 | const CXXDestructorDecl *dtor = 0; |
| 1194 | |
| 1195 | if (ci->isByRef()) { |
| 1196 | flags = BLOCK_FIELD_IS_BYREF; |
| 1197 | if (type.isObjCGCWeak()) flags |= BLOCK_FIELD_IS_WEAK; |
| 1198 | } else if (type->isBlockPointerType()) { |
| 1199 | flags = BLOCK_FIELD_IS_BLOCK; |
| 1200 | } else if (type->isObjCObjectPointerType() || C.isObjCNSObjectType(type)) { |
| 1201 | flags = BLOCK_FIELD_IS_OBJECT; |
| 1202 | } else if (C.getLangOptions().CPlusPlus) { |
| 1203 | if (const CXXRecordDecl *record = type->getAsCXXRecordDecl()) |
| 1204 | if (!record->hasTrivialDestructor()) |
| 1205 | dtor = record->getDestructor(); |
Mike Stump | 1edf6b6 | 2009-03-07 02:53:18 +0000 | [diff] [blame] | 1206 | } |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1207 | |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1208 | if (!dtor && flags.empty()) continue; |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1209 | |
| 1210 | unsigned index = capture.getIndex(); |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1211 | llvm::Value *srcField = Builder.CreateStructGEP(src, index); |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1212 | |
| 1213 | // If there's an explicit copy expression, we do that. |
| 1214 | if (dtor) { |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1215 | PushDestructorCleanup(dtor, srcField); |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1216 | |
| 1217 | // Otherwise we call _Block_object_dispose. It wouldn't be too |
| 1218 | // hard to just emit this as a cleanup if we wanted to make sure |
| 1219 | // that things were done in reverse. |
| 1220 | } else { |
| 1221 | llvm::Value *value = Builder.CreateLoad(srcField); |
John McCall | 5936e33 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 1222 | value = Builder.CreateBitCast(value, VoidPtrTy); |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1223 | BuildBlockRelease(value, flags); |
| 1224 | } |
Mike Stump | 1edf6b6 | 2009-03-07 02:53:18 +0000 | [diff] [blame] | 1225 | } |
| 1226 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1227 | cleanups.ForceCleanup(); |
| 1228 | |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1229 | FinishFunction(); |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 1230 | |
John McCall | 5936e33 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 1231 | return llvm::ConstantExpr::getBitCast(Fn, VoidPtrTy); |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 1232 | } |
| 1233 | |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1234 | llvm::Constant *CodeGenFunction:: |
| 1235 | GeneratebyrefCopyHelperFunction(const llvm::Type *T, BlockFieldFlags flags, |
| 1236 | const VarDecl *variable) { |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1237 | QualType R = getContext().VoidTy; |
| 1238 | |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 1239 | FunctionArgList args; |
| 1240 | ImplicitParamDecl dst(0, SourceLocation(), 0, getContext().VoidPtrTy); |
| 1241 | args.push_back(&dst); |
Mike Stump | ee09422 | 2009-03-06 06:12:24 +0000 | [diff] [blame] | 1242 | |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 1243 | ImplicitParamDecl src(0, SourceLocation(), 0, getContext().VoidPtrTy); |
| 1244 | args.push_back(&src); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1245 | |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1246 | const CGFunctionInfo &FI = |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 1247 | CGM.getTypes().getFunctionInfo(R, args, FunctionType::ExtInfo()); |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1248 | |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1249 | CodeGenTypes &Types = CGM.getTypes(); |
| 1250 | const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); |
| 1251 | |
Mike Stump | 3899a7f | 2009-06-05 23:26:36 +0000 | [diff] [blame] | 1252 | // FIXME: We'd like to put these into a mergable by content, with |
| 1253 | // internal linkage. |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1254 | llvm::Function *Fn = |
| 1255 | llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1256 | "__Block_byref_object_copy_", &CGM.getModule()); |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1257 | |
| 1258 | IdentifierInfo *II |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1259 | = &CGM.getContext().Idents.get("__Block_byref_object_copy_"); |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1260 | |
| 1261 | FunctionDecl *FD = FunctionDecl::Create(getContext(), |
| 1262 | getContext().getTranslationUnitDecl(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1263 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 1264 | SourceLocation(), II, R, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1265 | SC_Static, |
| 1266 | SC_None, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 1267 | false, true); |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 1268 | StartFunction(FD, R, Fn, FI, args, SourceLocation()); |
Mike Stump | ee09422 | 2009-03-06 06:12:24 +0000 | [diff] [blame] | 1269 | |
| 1270 | // dst->x |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 1271 | llvm::Value *V = GetAddrOfLocalVar(&dst); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1272 | V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0)); |
Mike Stump | c2f4c34 | 2009-04-15 22:11:36 +0000 | [diff] [blame] | 1273 | V = Builder.CreateLoad(V); |
Mike Stump | ee09422 | 2009-03-06 06:12:24 +0000 | [diff] [blame] | 1274 | V = Builder.CreateStructGEP(V, 6, "x"); |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1275 | llvm::Value *DstObj = V; |
Mike Stump | ee09422 | 2009-03-06 06:12:24 +0000 | [diff] [blame] | 1276 | |
| 1277 | // src->x |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 1278 | V = GetAddrOfLocalVar(&src); |
Mike Stump | ee09422 | 2009-03-06 06:12:24 +0000 | [diff] [blame] | 1279 | V = Builder.CreateLoad(V); |
| 1280 | V = Builder.CreateBitCast(V, T); |
| 1281 | V = Builder.CreateStructGEP(V, 6, "x"); |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1282 | |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1283 | if (Expr *copyExpr = getContext().getBlockVarCopyInits(variable)) { |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1284 | llvm::Value *SrcObj = V; |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1285 | EmitSynthesizedCXXCopyCtor(DstObj, SrcObj, copyExpr); |
| 1286 | } else { |
John McCall | 5936e33 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 1287 | DstObj = Builder.CreateBitCast(DstObj, VoidPtrTy); |
| 1288 | V = Builder.CreateBitCast(V, VoidPtrPtrTy); |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1289 | llvm::Value *SrcObj = Builder.CreateLoad(V); |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1290 | flags |= BLOCK_BYREF_CALLER; |
| 1291 | llvm::Value *N = llvm::ConstantInt::get(Int32Ty, flags.getBitMask()); |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1292 | llvm::Value *F = CGM.getBlockObjectAssign(); |
| 1293 | Builder.CreateCall3(F, DstObj, SrcObj, N); |
| 1294 | } |
| 1295 | |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1296 | FinishFunction(); |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1297 | |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1298 | return llvm::ConstantExpr::getBitCast(Fn, Int8PtrTy); |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1299 | } |
| 1300 | |
Mike Stump | 1851b68 | 2009-03-06 04:53:30 +0000 | [diff] [blame] | 1301 | llvm::Constant * |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1302 | CodeGenFunction::GeneratebyrefDestroyHelperFunction(const llvm::Type *T, |
| 1303 | BlockFieldFlags flags, |
| 1304 | const VarDecl *variable) { |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1305 | QualType R = getContext().VoidTy; |
| 1306 | |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 1307 | FunctionArgList args; |
| 1308 | ImplicitParamDecl src(0, SourceLocation(), 0, getContext().VoidPtrTy); |
| 1309 | args.push_back(&src); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1310 | |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1311 | const CGFunctionInfo &FI = |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 1312 | CGM.getTypes().getFunctionInfo(R, args, FunctionType::ExtInfo()); |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1313 | |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1314 | CodeGenTypes &Types = CGM.getTypes(); |
| 1315 | const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); |
| 1316 | |
Mike Stump | 3899a7f | 2009-06-05 23:26:36 +0000 | [diff] [blame] | 1317 | // FIXME: We'd like to put these into a mergable by content, with |
| 1318 | // internal linkage. |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1319 | llvm::Function *Fn = |
| 1320 | llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1321 | "__Block_byref_object_dispose_", |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1322 | &CGM.getModule()); |
| 1323 | |
| 1324 | IdentifierInfo *II |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1325 | = &CGM.getContext().Idents.get("__Block_byref_object_dispose_"); |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1326 | |
| 1327 | FunctionDecl *FD = FunctionDecl::Create(getContext(), |
| 1328 | getContext().getTranslationUnitDecl(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1329 | SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 1330 | SourceLocation(), II, R, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1331 | SC_Static, |
| 1332 | SC_None, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 1333 | false, true); |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 1334 | StartFunction(FD, R, Fn, FI, args, SourceLocation()); |
Mike Stump | 1851b68 | 2009-03-06 04:53:30 +0000 | [diff] [blame] | 1335 | |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 1336 | llvm::Value *V = GetAddrOfLocalVar(&src); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1337 | V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0)); |
Mike Stump | c2f4c34 | 2009-04-15 22:11:36 +0000 | [diff] [blame] | 1338 | V = Builder.CreateLoad(V); |
Mike Stump | 1851b68 | 2009-03-06 04:53:30 +0000 | [diff] [blame] | 1339 | V = Builder.CreateStructGEP(V, 6, "x"); |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1340 | |
| 1341 | // If it's not any kind of special object, it must have a destructor |
| 1342 | // or something. |
| 1343 | if (!flags.isSpecialPointer()) { |
| 1344 | EHScopeStack::stable_iterator CleanupDepth = EHStack.stable_begin(); |
| 1345 | PushDestructorCleanup(variable->getType(), V); |
| 1346 | PopCleanupBlocks(CleanupDepth); |
| 1347 | |
| 1348 | // Otherwise, call _Block_object_dispose. |
| 1349 | } else { |
| 1350 | V = Builder.CreateBitCast(V, llvm::PointerType::get(Int8PtrTy, 0)); |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1351 | V = Builder.CreateLoad(V); |
Mike Stump | 1851b68 | 2009-03-06 04:53:30 +0000 | [diff] [blame] | 1352 | |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1353 | flags |= BLOCK_BYREF_CALLER; |
| 1354 | BuildBlockRelease(V, flags); |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1355 | } |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1356 | |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1357 | FinishFunction(); |
| 1358 | |
| 1359 | return llvm::ConstantExpr::getBitCast(Fn, Int8PtrTy); |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1360 | } |
| 1361 | |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1362 | llvm::Constant *CodeGenModule::BuildbyrefCopyHelper(const llvm::Type *T, |
| 1363 | BlockFieldFlags flags, |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1364 | unsigned align, |
| 1365 | const VarDecl *var) { |
John McCall | 3469585 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 1366 | // All alignments below pointer alignment are bumped up, as we |
| 1367 | // always have at least that much alignment to begin with. |
| 1368 | if (align < PointerAlignInBytes) align = PointerAlignInBytes; |
Chris Lattner | 10976d9 | 2009-12-05 08:21:30 +0000 | [diff] [blame] | 1369 | |
Mike Stump | 3899a7f | 2009-06-05 23:26:36 +0000 | [diff] [blame] | 1370 | // As an optimization, we only generate a single function of each kind we |
| 1371 | // might need. We need a different one for each alignment and for each |
| 1372 | // setting of flags. We mix Align and flag to get the kind. |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1373 | uint64_t Kind = (uint64_t)align*BLOCK_BYREF_CURRENT_MAX + flags.getBitMask(); |
| 1374 | llvm::Constant *&Entry = AssignCache[Kind]; |
| 1375 | if (!Entry) |
| 1376 | Entry = CodeGenFunction(*this). |
| 1377 | GeneratebyrefCopyHelperFunction(T, flags, var); |
| 1378 | return Entry; |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1379 | } |
| 1380 | |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1381 | llvm::Constant *CodeGenModule::BuildbyrefDestroyHelper(const llvm::Type *T, |
| 1382 | BlockFieldFlags flags, |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1383 | unsigned align, |
| 1384 | const VarDecl *var) { |
John McCall | 3469585 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 1385 | // All alignments below pointer alignment are bumped up, as we |
| 1386 | // always have at least that much alignment to begin with. |
| 1387 | if (align < PointerAlignInBytes) align = PointerAlignInBytes; |
Chris Lattner | 10976d9 | 2009-12-05 08:21:30 +0000 | [diff] [blame] | 1388 | |
Mike Stump | 3899a7f | 2009-06-05 23:26:36 +0000 | [diff] [blame] | 1389 | // As an optimization, we only generate a single function of each kind we |
| 1390 | // might need. We need a different one for each alignment and for each |
| 1391 | // setting of flags. We mix Align and flag to get the kind. |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1392 | uint64_t Kind = (uint64_t)align*BLOCK_BYREF_CURRENT_MAX + flags.getBitMask(); |
| 1393 | llvm::Constant *&Entry = DestroyCache[Kind]; |
| 1394 | if (!Entry) |
| 1395 | Entry = CodeGenFunction(*this). |
| 1396 | GeneratebyrefDestroyHelperFunction(T, flags, var); |
| 1397 | return Entry; |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1398 | } |
| 1399 | |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1400 | void CodeGenFunction::BuildBlockRelease(llvm::Value *V, BlockFieldFlags flags) { |
Daniel Dunbar | 673431a | 2010-07-16 00:00:15 +0000 | [diff] [blame] | 1401 | llvm::Value *F = CGM.getBlockObjectDispose(); |
Mike Stump | 1851b68 | 2009-03-06 04:53:30 +0000 | [diff] [blame] | 1402 | llvm::Value *N; |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 1403 | V = Builder.CreateBitCast(V, Int8PtrTy); |
| 1404 | N = llvm::ConstantInt::get(Int32Ty, flags.getBitMask()); |
Mike Stump | 797b632 | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 1405 | Builder.CreateCall2(F, V, N); |
| 1406 | } |