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 | |
| 14 | #include "CodeGenFunction.h" |
| 15 | #include "CodeGenModule.h" |
| 16 | #include "llvm/Module.h" |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 17 | #include "llvm/Target/TargetData.h" |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 18 | |
| 19 | #include <algorithm> |
| 20 | |
| 21 | using namespace clang; |
| 22 | using namespace CodeGen; |
| 23 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 24 | enum { |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 25 | BLOCK_NEEDS_FREE = (1 << 24), |
| 26 | BLOCK_HAS_COPY_DISPOSE = (1 << 25), |
| 27 | BLOCK_HAS_CXX_OBJ = (1 << 26), |
| 28 | BLOCK_IS_GC = (1 << 27), |
| 29 | BLOCK_IS_GLOBAL = (1 << 28), |
| 30 | BLOCK_HAS_DESCRIPTOR = (1 << 29) |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 31 | }; |
| 32 | |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame^] | 33 | llvm::Constant *CodeGenFunction::BuildDescriptorBlockDecl(uint64_t Size) { |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 34 | const llvm::PointerType *PtrToInt8Ty |
| 35 | = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
Mike Stump | 56129b1 | 2009-02-13 16:55:51 +0000 | [diff] [blame] | 36 | const llvm::Type *UnsignedLongTy |
| 37 | = CGM.getTypes().ConvertType(getContext().UnsignedLongTy); |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 38 | llvm::Constant *C; |
| 39 | std::vector<llvm::Constant*> Elts; |
| 40 | |
| 41 | // reserved |
Mike Stump | 56129b1 | 2009-02-13 16:55:51 +0000 | [diff] [blame] | 42 | C = llvm::ConstantInt::get(UnsignedLongTy, 0); |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 43 | Elts.push_back(C); |
| 44 | |
| 45 | // Size |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame^] | 46 | C = llvm::ConstantInt::get(UnsignedLongTy, Size); |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 47 | Elts.push_back(C); |
| 48 | |
| 49 | if (BlockHasCopyDispose) { |
| 50 | // copy_func_helper_decl |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame^] | 51 | // FIXME: implement |
Mike Stump | 56129b1 | 2009-02-13 16:55:51 +0000 | [diff] [blame] | 52 | C = llvm::ConstantInt::get(UnsignedLongTy, 0); |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 53 | C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty); |
| 54 | Elts.push_back(C); |
| 55 | |
| 56 | // destroy_func_decl |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame^] | 57 | // FIXME: implement |
Mike Stump | 56129b1 | 2009-02-13 16:55:51 +0000 | [diff] [blame] | 58 | C = llvm::ConstantInt::get(UnsignedLongTy, 0); |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 59 | C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty); |
| 60 | Elts.push_back(C); |
| 61 | } |
| 62 | |
| 63 | C = llvm::ConstantStruct::get(Elts); |
| 64 | |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 65 | C = new llvm::GlobalVariable(C->getType(), true, |
| 66 | llvm::GlobalValue::InternalLinkage, |
Mike Stump | 7d6dc4f | 2009-02-13 20:17:16 +0000 | [diff] [blame] | 67 | C, "__block_descriptor_tmp", &CGM.getModule()); |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 68 | return C; |
| 69 | } |
| 70 | |
Mike Stump | f99f1d0 | 2009-02-13 17:23:42 +0000 | [diff] [blame] | 71 | llvm::Constant *CodeGenModule::getNSConcreteGlobalBlock() { |
| 72 | if (NSConcreteGlobalBlock) |
| 73 | return NSConcreteGlobalBlock; |
| 74 | |
| 75 | const llvm::PointerType *PtrToInt8Ty |
| 76 | = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
Mike Stump | f744895 | 2009-02-13 19:38:12 +0000 | [diff] [blame] | 77 | // FIXME: We should have a CodeGenModule::AddRuntimeVariable that does the |
Mike Stump | f99f1d0 | 2009-02-13 17:23:42 +0000 | [diff] [blame] | 78 | // same thing as CreateRuntimeFunction if there's already a variable with |
| 79 | // the same name. |
| 80 | NSConcreteGlobalBlock |
| 81 | = new llvm::GlobalVariable(PtrToInt8Ty, false, |
| 82 | llvm::GlobalValue::ExternalLinkage, |
| 83 | 0, "_NSConcreteGlobalBlock", |
| 84 | &getModule()); |
| 85 | |
| 86 | return NSConcreteGlobalBlock; |
| 87 | } |
| 88 | |
Mike Stump | 59c5b11 | 2009-02-13 19:29:27 +0000 | [diff] [blame] | 89 | llvm::Constant *CodeGenModule::getNSConcreteStackBlock() { |
| 90 | if (NSConcreteStackBlock) |
| 91 | return NSConcreteStackBlock; |
| 92 | |
| 93 | const llvm::PointerType *PtrToInt8Ty |
| 94 | = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
Mike Stump | f744895 | 2009-02-13 19:38:12 +0000 | [diff] [blame] | 95 | // FIXME: We should have a CodeGenModule::AddRuntimeVariable that does the |
Mike Stump | 59c5b11 | 2009-02-13 19:29:27 +0000 | [diff] [blame] | 96 | // same thing as CreateRuntimeFunction if there's already a variable with |
| 97 | // the same name. |
| 98 | NSConcreteStackBlock |
| 99 | = new llvm::GlobalVariable(PtrToInt8Ty, false, |
| 100 | llvm::GlobalValue::ExternalLinkage, |
| 101 | 0, "_NSConcreteStackBlock", |
| 102 | &getModule()); |
| 103 | |
| 104 | return NSConcreteStackBlock; |
| 105 | } |
| 106 | |
Mike Stump | bd65cac | 2009-02-19 01:01:04 +0000 | [diff] [blame] | 107 | // FIXME: Push most into CGM, passing down a few bits, like current |
| 108 | // function name. |
Mike Stump | 67a6448 | 2009-02-14 22:16:35 +0000 | [diff] [blame] | 109 | llvm::Constant *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) { |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 110 | bool insideFunction = false; |
| 111 | bool BlockRefDeclList = false; |
| 112 | bool BlockByrefDeclList = false; |
| 113 | |
| 114 | std::vector<llvm::Constant*> Elts; |
| 115 | llvm::Constant *C; |
| 116 | |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 117 | { |
| 118 | // C = BuildBlockStructInitlist(); |
| 119 | unsigned int flags = BLOCK_HAS_DESCRIPTOR; |
| 120 | |
| 121 | if (BlockHasCopyDispose) |
| 122 | flags |= BLOCK_HAS_COPY_DISPOSE; |
| 123 | |
Mike Stump | 7d6dc4f | 2009-02-13 20:17:16 +0000 | [diff] [blame] | 124 | // __isa |
Mike Stump | 59c5b11 | 2009-02-13 19:29:27 +0000 | [diff] [blame] | 125 | C = CGM.getNSConcreteStackBlock(); |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 126 | if (!insideFunction || |
| 127 | (!BlockRefDeclList && !BlockByrefDeclList)) { |
Mike Stump | f99f1d0 | 2009-02-13 17:23:42 +0000 | [diff] [blame] | 128 | C = CGM.getNSConcreteGlobalBlock(); |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 129 | flags |= BLOCK_IS_GLOBAL; |
| 130 | } |
Mike Stump | 59c5b11 | 2009-02-13 19:29:27 +0000 | [diff] [blame] | 131 | const llvm::PointerType *PtrToInt8Ty |
| 132 | = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 133 | C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty); |
| 134 | Elts.push_back(C); |
| 135 | |
| 136 | // __flags |
| 137 | const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( |
| 138 | CGM.getTypes().ConvertType(CGM.getContext().IntTy)); |
| 139 | C = llvm::ConstantInt::get(IntTy, flags); |
| 140 | Elts.push_back(C); |
| 141 | |
| 142 | // __reserved |
| 143 | C = llvm::ConstantInt::get(IntTy, 0); |
| 144 | Elts.push_back(C); |
| 145 | |
Mike Stump | bd65cac | 2009-02-19 01:01:04 +0000 | [diff] [blame] | 146 | // __invoke |
Mike Stump | cb71722 | 2009-02-17 17:18:36 +0000 | [diff] [blame] | 147 | const char *Name = ""; |
Mike Stump | 67a6448 | 2009-02-14 22:16:35 +0000 | [diff] [blame] | 148 | if (const NamedDecl *ND = dyn_cast<NamedDecl>(CurFuncDecl)) |
Mike Stump | 1905061 | 2009-02-17 23:25:52 +0000 | [diff] [blame] | 149 | if (ND->getIdentifier()) |
| 150 | Name = ND->getNameAsCString(); |
Mike Stump | 67a6448 | 2009-02-14 22:16:35 +0000 | [diff] [blame] | 151 | BlockInfo Info(0, Name); |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame^] | 152 | uint64_t subBlockSize; |
| 153 | llvm::Function *Fn |
| 154 | = CodeGenFunction(*this).GenerateBlockFunction(BE, Info, subBlockSize); |
Mike Stump | 67a6448 | 2009-02-14 22:16:35 +0000 | [diff] [blame] | 155 | Elts.push_back(Fn); |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 156 | |
| 157 | // __descriptor |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame^] | 158 | Elts.push_back(BuildDescriptorBlockDecl(subBlockSize)); |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 159 | |
| 160 | // FIXME: Add block_original_ref_decl_list and block_byref_decl_list. |
| 161 | } |
| 162 | |
| 163 | C = llvm::ConstantStruct::get(Elts); |
| 164 | |
| 165 | char Name[32]; |
Mike Stump | 26efc33 | 2009-02-13 18:36:05 +0000 | [diff] [blame] | 166 | sprintf(Name, "__block_holder_tmp_%d", CGM.getGlobalUniqueCount()); |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 167 | C = new llvm::GlobalVariable(C->getType(), true, |
| 168 | llvm::GlobalValue::InternalLinkage, |
| 169 | C, Name, &CGM.getModule()); |
Mike Stump | bd65cac | 2009-02-19 01:01:04 +0000 | [diff] [blame] | 170 | QualType BPT = BE->getType(); |
| 171 | C = llvm::ConstantExpr::getBitCast(C, ConvertType(BPT)); |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 172 | return C; |
| 173 | } |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
Mike Stump | ab69514 | 2009-02-13 15:16:56 +0000 | [diff] [blame] | 178 | const llvm::Type *CodeGenModule::getBlockDescriptorType() { |
| 179 | if (BlockDescriptorType) |
| 180 | return BlockDescriptorType; |
| 181 | |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 182 | const llvm::Type *UnsignedLongTy = |
Mike Stump | ab69514 | 2009-02-13 15:16:56 +0000 | [diff] [blame] | 183 | getTypes().ConvertType(getContext().UnsignedLongTy); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 184 | |
Mike Stump | ab69514 | 2009-02-13 15:16:56 +0000 | [diff] [blame] | 185 | // struct __block_descriptor { |
| 186 | // unsigned long reserved; |
| 187 | // unsigned long block_size; |
| 188 | // }; |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 189 | BlockDescriptorType = llvm::StructType::get(UnsignedLongTy, |
| 190 | UnsignedLongTy, |
Mike Stump | ab69514 | 2009-02-13 15:16:56 +0000 | [diff] [blame] | 191 | NULL); |
| 192 | |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame^] | 193 | // FIXME: This breaks an unrelated testcase in the testsuite, we |
| 194 | // _want_ llvm to not use structural equality, sometimes. What |
| 195 | // should we do, modify the testcase and do this anyway, or... |
| 196 | #if 0 |
Mike Stump | ab69514 | 2009-02-13 15:16:56 +0000 | [diff] [blame] | 197 | getModule().addTypeName("struct.__block_descriptor", |
| 198 | BlockDescriptorType); |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame^] | 199 | #endif |
Mike Stump | ab69514 | 2009-02-13 15:16:56 +0000 | [diff] [blame] | 200 | |
| 201 | return BlockDescriptorType; |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 204 | const llvm::Type * |
| 205 | CodeGenModule::getGenericBlockLiteralType() { |
| 206 | if (GenericBlockLiteralType) |
| 207 | return GenericBlockLiteralType; |
| 208 | |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 209 | const llvm::Type *Int8PtrTy = |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 210 | llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 211 | |
| 212 | const llvm::Type *BlockDescPtrTy = |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 213 | llvm::PointerType::getUnqual(getBlockDescriptorType()); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 214 | |
Mike Stump | 7cbb360 | 2009-02-13 16:01:35 +0000 | [diff] [blame] | 215 | const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( |
| 216 | getTypes().ConvertType(getContext().IntTy)); |
| 217 | |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 218 | // struct __block_literal_generic { |
Mike Stump | bd65cac | 2009-02-19 01:01:04 +0000 | [diff] [blame] | 219 | // void *__isa; |
| 220 | // int __flags; |
| 221 | // int __reserved; |
| 222 | // void (*__invoke)(void *); |
| 223 | // struct __block_descriptor *__descriptor; |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 224 | // }; |
| 225 | GenericBlockLiteralType = llvm::StructType::get(Int8PtrTy, |
Mike Stump | 7cbb360 | 2009-02-13 16:01:35 +0000 | [diff] [blame] | 226 | IntTy, |
| 227 | IntTy, |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 228 | Int8PtrTy, |
| 229 | BlockDescPtrTy, |
| 230 | NULL); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 231 | |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame^] | 232 | // FIXME: See struct.__block_descriptor |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 233 | getModule().addTypeName("struct.__block_literal_generic", |
| 234 | GenericBlockLiteralType); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 235 | |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 236 | return GenericBlockLiteralType; |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Mike Stump | bd65cac | 2009-02-19 01:01:04 +0000 | [diff] [blame] | 239 | const llvm::Type * |
| 240 | CodeGenModule::getGenericExtendedBlockLiteralType() { |
| 241 | if (GenericExtendedBlockLiteralType) |
| 242 | return GenericExtendedBlockLiteralType; |
| 243 | |
| 244 | const llvm::Type *Int8PtrTy = |
| 245 | llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 246 | |
| 247 | const llvm::Type *BlockDescPtrTy = |
| 248 | llvm::PointerType::getUnqual(getBlockDescriptorType()); |
| 249 | |
| 250 | const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( |
| 251 | getTypes().ConvertType(getContext().IntTy)); |
| 252 | |
| 253 | // struct __block_literal_generic { |
| 254 | // void *__isa; |
| 255 | // int __flags; |
| 256 | // int __reserved; |
| 257 | // void (*__invoke)(void *); |
| 258 | // struct __block_descriptor *__descriptor; |
| 259 | // void *__copy_func_helper_decl; |
| 260 | // void *__destroy_func_decl; |
| 261 | // }; |
| 262 | GenericExtendedBlockLiteralType = llvm::StructType::get(Int8PtrTy, |
| 263 | IntTy, |
| 264 | IntTy, |
| 265 | Int8PtrTy, |
| 266 | BlockDescPtrTy, |
| 267 | Int8PtrTy, |
| 268 | Int8PtrTy, |
| 269 | NULL); |
| 270 | |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame^] | 271 | // FIXME: See struct.__block_descriptor |
Mike Stump | bd65cac | 2009-02-19 01:01:04 +0000 | [diff] [blame] | 272 | getModule().addTypeName("struct.__block_literal_extended_generic", |
| 273 | GenericExtendedBlockLiteralType); |
| 274 | |
| 275 | return GenericExtendedBlockLiteralType; |
| 276 | } |
| 277 | |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 278 | /// getBlockFunctionType - Given a BlockPointerType, will return the |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 279 | /// function type for the block, including the first block literal argument. |
| 280 | static QualType getBlockFunctionType(ASTContext &Ctx, |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 281 | const BlockPointerType *BPT) { |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 282 | const FunctionTypeProto *FTy = cast<FunctionTypeProto>(BPT->getPointeeType()); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 283 | |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 284 | llvm::SmallVector<QualType, 8> Types; |
| 285 | Types.push_back(Ctx.getPointerType(Ctx.VoidTy)); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 286 | |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 287 | for (FunctionTypeProto::arg_type_iterator i = FTy->arg_type_begin(), |
| 288 | e = FTy->arg_type_end(); i != e; ++i) |
| 289 | Types.push_back(*i); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 290 | |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 291 | return Ctx.getFunctionType(FTy->getResultType(), |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 292 | &Types[0], Types.size(), |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 293 | FTy->isVariadic(), 0); |
| 294 | } |
| 295 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 296 | RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E) { |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 297 | const BlockPointerType *BPT = |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 298 | E->getCallee()->getType()->getAsBlockPointerType(); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 299 | |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 300 | llvm::Value *Callee = EmitScalarExpr(E->getCallee()); |
| 301 | |
| 302 | // Get a pointer to the generic block literal. |
| 303 | const llvm::Type *BlockLiteralTy = |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 304 | llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType()); |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 305 | |
| 306 | // Bitcast the callee to a block literal. |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 307 | llvm::Value *BlockLiteral = |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 308 | Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal"); |
| 309 | |
| 310 | // Get the function pointer from the literal. |
| 311 | llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp"); |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame^] | 312 | // FIXME: second argument should be false? |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 313 | llvm::Value *Func = Builder.CreateLoad(FuncPtr, FuncPtr, "tmp"); |
| 314 | |
| 315 | // Cast the function pointer to the right type. |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 316 | const llvm::Type *BlockFTy = |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 317 | ConvertType(getBlockFunctionType(getContext(), BPT)); |
| 318 | const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy); |
| 319 | Func = Builder.CreateBitCast(Func, BlockFTyPtr); |
| 320 | |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 321 | BlockLiteral = |
| 322 | Builder.CreateBitCast(BlockLiteral, |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 323 | llvm::PointerType::getUnqual(llvm::Type::Int8Ty), |
| 324 | "tmp"); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 325 | |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 326 | // Add the block literal. |
| 327 | QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy); |
| 328 | CallArgList Args; |
| 329 | Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy)); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 330 | |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 331 | // And the rest of the arguments. |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 332 | for (CallExpr::const_arg_iterator i = E->arg_begin(), e = E->arg_end(); |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 333 | i != e; ++i) |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 334 | Args.push_back(std::make_pair(EmitAnyExprToTemp(*i), |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 335 | i->getType())); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 336 | |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 337 | // And call the block. |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 338 | return EmitCall(CGM.getTypes().getFunctionInfo(E->getType(), Args), |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 339 | Func, Args); |
| 340 | } |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 341 | |
Mike Stump | 67a6448 | 2009-02-14 22:16:35 +0000 | [diff] [blame] | 342 | llvm::Constant * |
Mike Stump | 30395dd | 2009-02-14 22:49:33 +0000 | [diff] [blame] | 343 | CodeGenModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) { |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 344 | // Generate the block descriptor. |
| 345 | const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy); |
Mike Stump | 7cbb360 | 2009-02-13 16:01:35 +0000 | [diff] [blame] | 346 | const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( |
| 347 | getTypes().ConvertType(getContext().IntTy)); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 348 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 349 | llvm::Constant *DescriptorFields[2]; |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 350 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 351 | // Reserved |
| 352 | DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 353 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 354 | // Block literal size. For global blocks we just use the size of the generic |
| 355 | // block literal struct. |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 356 | uint64_t BlockLiteralSize = |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 357 | TheTargetData.getTypeStoreSizeInBits(getGenericBlockLiteralType()) / 8; |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 358 | DescriptorFields[1] = llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 359 | |
| 360 | llvm::Constant *DescriptorStruct = |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 361 | llvm::ConstantStruct::get(&DescriptorFields[0], 2); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 362 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 363 | llvm::GlobalVariable *Descriptor = |
| 364 | new llvm::GlobalVariable(DescriptorStruct->getType(), true, |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 365 | llvm::GlobalVariable::InternalLinkage, |
| 366 | DescriptorStruct, "__block_descriptor_global", |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 367 | &getModule()); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 368 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 369 | // Generate the constants for the block literal. |
| 370 | llvm::Constant *LiteralFields[5]; |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 371 | |
Mike Stump | 67a6448 | 2009-02-14 22:16:35 +0000 | [diff] [blame] | 372 | CodeGenFunction::BlockInfo Info(0, n); |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame^] | 373 | uint64_t subBlockSize; |
| 374 | llvm::Function *Fn |
| 375 | = CodeGenFunction(*this).GenerateBlockFunction(BE, Info, subBlockSize); |
| 376 | assert(subBlockSize == BlockLiteralSize |
| 377 | && "no imports allowed for global block"); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 378 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 379 | // isa |
Mike Stump | f99f1d0 | 2009-02-13 17:23:42 +0000 | [diff] [blame] | 380 | LiteralFields[0] = getNSConcreteGlobalBlock(); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 381 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 382 | // Flags |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 383 | LiteralFields[1] = llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 384 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 385 | // Reserved |
Mike Stump | 7cbb360 | 2009-02-13 16:01:35 +0000 | [diff] [blame] | 386 | LiteralFields[2] = llvm::Constant::getNullValue(IntTy); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 387 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 388 | // Function |
| 389 | LiteralFields[3] = Fn; |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 390 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 391 | // Descriptor |
| 392 | LiteralFields[4] = Descriptor; |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 393 | |
| 394 | llvm::Constant *BlockLiteralStruct = |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 395 | llvm::ConstantStruct::get(&LiteralFields[0], 5); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 396 | |
| 397 | llvm::GlobalVariable *BlockLiteral = |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 398 | new llvm::GlobalVariable(BlockLiteralStruct->getType(), true, |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 399 | llvm::GlobalVariable::InternalLinkage, |
| 400 | BlockLiteralStruct, "__block_literal_global", |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 401 | &getModule()); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 402 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 403 | return BlockLiteral; |
| 404 | } |
| 405 | |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame^] | 406 | llvm::Value *CodeGenFunction::LoadBlockStruct() { |
| 407 | return Builder.CreateLoad(LocalDeclMap[getBlockStructDecl()], "self"); |
| 408 | } |
| 409 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 410 | llvm::Function *CodeGenFunction::GenerateBlockFunction(const BlockExpr *Expr, |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame^] | 411 | const BlockInfo& Info, |
| 412 | uint64_t &Size) { |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 413 | const FunctionTypeProto *FTy = |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 414 | cast<FunctionTypeProto>(Expr->getFunctionType()); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 415 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 416 | FunctionArgList Args; |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 417 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 418 | const BlockDecl *BD = Expr->getBlockDecl(); |
| 419 | |
| 420 | // FIXME: This leaks |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 421 | ImplicitParamDecl *SelfDecl = |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 422 | ImplicitParamDecl::Create(getContext(), 0, |
| 423 | SourceLocation(), 0, |
| 424 | getContext().getPointerType(getContext().VoidTy)); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 425 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 426 | Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType())); |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame^] | 427 | BlockStructDecl = SelfDecl; |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 428 | |
| 429 | for (BlockDecl::param_iterator i = BD->param_begin(), |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 430 | e = BD->param_end(); i != e; ++i) |
Mike Stump | 1905061 | 2009-02-17 23:25:52 +0000 | [diff] [blame] | 431 | Args.push_back(std::make_pair(*i, (*i)->getType())); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 432 | |
| 433 | const CGFunctionInfo &FI = |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 434 | CGM.getTypes().getFunctionInfo(FTy->getResultType(), Args); |
| 435 | |
Mike Stump | 67a6448 | 2009-02-14 22:16:35 +0000 | [diff] [blame] | 436 | std::string Name = std::string("__") + Info.Name + "_block_invoke_"; |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 437 | CodeGenTypes &Types = CGM.getTypes(); |
| 438 | const llvm::FunctionType *LTy = Types.GetFunctionType(FI, FTy->isVariadic()); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 439 | |
| 440 | llvm::Function *Fn = |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 441 | llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, |
| 442 | Name, |
| 443 | &CGM.getModule()); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 444 | |
| 445 | StartFunction(BD, FTy->getResultType(), Fn, Args, |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 446 | Expr->getBody()->getLocEnd()); |
| 447 | EmitStmt(Expr->getBody()); |
| 448 | FinishFunction(cast<CompoundStmt>(Expr->getBody())->getRBracLoc()); |
| 449 | |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame^] | 450 | Size = BlockOffset; |
| 451 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 452 | return Fn; |
| 453 | } |