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