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