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