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