blob: ecc9406b2fe9c443f24cd2507d745f5c6276b7b0 [file] [log] [blame]
Anders Carlssonacfde802009-02-12 00:39:25 +00001//===--- 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 Carlssond5cab542009-02-12 17:55:02 +000017#include "llvm/Target/TargetData.h"
Anders Carlssonacfde802009-02-12 00:39:25 +000018
19#include <algorithm>
20
21using namespace clang;
22using namespace CodeGen;
23
Anders Carlssond5cab542009-02-12 17:55:02 +000024enum {
Mike Stumpe5fee252009-02-13 16:19:19 +000025 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 Carlssond5cab542009-02-12 17:55:02 +000031};
32
Mike Stumpe5fee252009-02-13 16:19:19 +000033llvm::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 Stump56129b12009-02-13 16:55:51 +000039 const llvm::Type *UnsignedLongTy
40 = CGM.getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpe5fee252009-02-13 16:19:19 +000041 llvm::Constant *C;
42 std::vector<llvm::Constant*> Elts;
43
44 // reserved
Mike Stump56129b12009-02-13 16:55:51 +000045 C = llvm::ConstantInt::get(UnsignedLongTy, 0);
Mike Stumpe5fee252009-02-13 16:19:19 +000046 Elts.push_back(C);
47
48 // Size
Mike Stump3ba82152009-02-13 17:03:17 +000049 int sz = CGM.getTargetData()
50 .getTypeStoreSizeInBits(CGM.getGenericBlockLiteralType()) / 8;
51 C = llvm::ConstantInt::get(UnsignedLongTy, sz);
Mike Stumpe5fee252009-02-13 16:19:19 +000052 Elts.push_back(C);
53
54 if (BlockHasCopyDispose) {
55 // copy_func_helper_decl
Mike Stump56129b12009-02-13 16:55:51 +000056 C = llvm::ConstantInt::get(UnsignedLongTy, 0);
Mike Stumpe5fee252009-02-13 16:19:19 +000057 C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty);
58 Elts.push_back(C);
59
60 // destroy_func_decl
Mike Stump56129b12009-02-13 16:55:51 +000061 C = llvm::ConstantInt::get(UnsignedLongTy, 0);
Mike Stumpe5fee252009-02-13 16:19:19 +000062 C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty);
63 Elts.push_back(C);
64 }
65
66 C = llvm::ConstantStruct::get(Elts);
67
Mike Stumpe5fee252009-02-13 16:19:19 +000068 C = new llvm::GlobalVariable(C->getType(), true,
69 llvm::GlobalValue::InternalLinkage,
Mike Stump7d6dc4f2009-02-13 20:17:16 +000070 C, "__block_descriptor_tmp", &CGM.getModule());
Mike Stumpe5fee252009-02-13 16:19:19 +000071 return C;
72}
73
Mike Stumpf99f1d02009-02-13 17:23:42 +000074llvm::Constant *CodeGenModule::getNSConcreteGlobalBlock() {
75 if (NSConcreteGlobalBlock)
76 return NSConcreteGlobalBlock;
77
78 const llvm::PointerType *PtrToInt8Ty
79 = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Mike Stumpf7448952009-02-13 19:38:12 +000080 // FIXME: We should have a CodeGenModule::AddRuntimeVariable that does the
Mike Stumpf99f1d02009-02-13 17:23:42 +000081 // 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 Stump59c5b112009-02-13 19:29:27 +000092llvm::Constant *CodeGenModule::getNSConcreteStackBlock() {
93 if (NSConcreteStackBlock)
94 return NSConcreteStackBlock;
95
96 const llvm::PointerType *PtrToInt8Ty
97 = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Mike Stumpf7448952009-02-13 19:38:12 +000098 // FIXME: We should have a CodeGenModule::AddRuntimeVariable that does the
Mike Stump59c5b112009-02-13 19:29:27 +000099 // 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 Stump67a64482009-02-14 22:16:35 +0000110llvm::Constant *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) {
Mike Stumpe5fee252009-02-13 16:19:19 +0000111 // 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 Stumpe5fee252009-02-13 16:19:19 +0000120 {
121 // C = BuildBlockStructInitlist();
122 unsigned int flags = BLOCK_HAS_DESCRIPTOR;
123
124 if (BlockHasCopyDispose)
125 flags |= BLOCK_HAS_COPY_DISPOSE;
126
Mike Stump7d6dc4f2009-02-13 20:17:16 +0000127 // __isa
Mike Stump59c5b112009-02-13 19:29:27 +0000128 C = CGM.getNSConcreteStackBlock();
Mike Stumpe5fee252009-02-13 16:19:19 +0000129 if (!insideFunction ||
130 (!BlockRefDeclList && !BlockByrefDeclList)) {
Mike Stumpf99f1d02009-02-13 17:23:42 +0000131 C = CGM.getNSConcreteGlobalBlock();
Mike Stumpe5fee252009-02-13 16:19:19 +0000132 flags |= BLOCK_IS_GLOBAL;
133 }
Mike Stump59c5b112009-02-13 19:29:27 +0000134 const llvm::PointerType *PtrToInt8Ty
135 = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Mike Stumpe5fee252009-02-13 16:19:19 +0000136 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 Stump67a64482009-02-14 22:16:35 +0000150 std::string Name;
151 if (const NamedDecl *ND = dyn_cast<NamedDecl>(CurFuncDecl))
152 Name = ND->getNameAsString();
153 BlockInfo Info(0, Name);
154 llvm::Function *Fn = CodeGenFunction(*this).GenerateBlockFunction(BE, Info);
155 Elts.push_back(Fn);
Mike Stumpe5fee252009-02-13 16:19:19 +0000156
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 Stump26efc332009-02-13 18:36:05 +0000166 sprintf(Name, "__block_holder_tmp_%d", CGM.getGlobalUniqueCount());
Mike Stumpe5fee252009-02-13 16:19:19 +0000167 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 Stumpab695142009-02-13 15:16:56 +0000176const llvm::Type *CodeGenModule::getBlockDescriptorType() {
177 if (BlockDescriptorType)
178 return BlockDescriptorType;
179
Mike Stumpa5448542009-02-13 15:32:32 +0000180 const llvm::Type *UnsignedLongTy =
Mike Stumpab695142009-02-13 15:16:56 +0000181 getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000182
Mike Stumpab695142009-02-13 15:16:56 +0000183 // struct __block_descriptor {
184 // unsigned long reserved;
185 // unsigned long block_size;
186 // };
Mike Stumpa5448542009-02-13 15:32:32 +0000187 BlockDescriptorType = llvm::StructType::get(UnsignedLongTy,
188 UnsignedLongTy,
Mike Stumpab695142009-02-13 15:16:56 +0000189 NULL);
190
191 getModule().addTypeName("struct.__block_descriptor",
192 BlockDescriptorType);
193
194 return BlockDescriptorType;
Anders Carlssonacfde802009-02-12 00:39:25 +0000195}
196
Mike Stump9b8a7972009-02-13 15:25:34 +0000197const llvm::Type *
198CodeGenModule::getGenericBlockLiteralType() {
199 if (GenericBlockLiteralType)
200 return GenericBlockLiteralType;
201
Mike Stumpa5448542009-02-13 15:32:32 +0000202 const llvm::Type *Int8PtrTy =
Mike Stump9b8a7972009-02-13 15:25:34 +0000203 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Mike Stumpa5448542009-02-13 15:32:32 +0000204
205 const llvm::Type *BlockDescPtrTy =
Mike Stump9b8a7972009-02-13 15:25:34 +0000206 llvm::PointerType::getUnqual(getBlockDescriptorType());
Mike Stumpa5448542009-02-13 15:32:32 +0000207
Mike Stump7cbb3602009-02-13 16:01:35 +0000208 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
209 getTypes().ConvertType(getContext().IntTy));
210
Mike Stump9b8a7972009-02-13 15:25:34 +0000211 // 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 Stump7cbb3602009-02-13 16:01:35 +0000219 IntTy,
220 IntTy,
Mike Stump9b8a7972009-02-13 15:25:34 +0000221 Int8PtrTy,
222 BlockDescPtrTy,
223 NULL);
Mike Stumpa5448542009-02-13 15:32:32 +0000224
Mike Stump9b8a7972009-02-13 15:25:34 +0000225 getModule().addTypeName("struct.__block_literal_generic",
226 GenericBlockLiteralType);
Mike Stumpa5448542009-02-13 15:32:32 +0000227
Mike Stump9b8a7972009-02-13 15:25:34 +0000228 return GenericBlockLiteralType;
Anders Carlssonacfde802009-02-12 00:39:25 +0000229}
230
Mike Stumpa5448542009-02-13 15:32:32 +0000231/// getBlockFunctionType - Given a BlockPointerType, will return the
Anders Carlssonacfde802009-02-12 00:39:25 +0000232/// function type for the block, including the first block literal argument.
233static QualType getBlockFunctionType(ASTContext &Ctx,
Anders Carlssond5cab542009-02-12 17:55:02 +0000234 const BlockPointerType *BPT) {
Anders Carlssonacfde802009-02-12 00:39:25 +0000235 const FunctionTypeProto *FTy = cast<FunctionTypeProto>(BPT->getPointeeType());
Mike Stumpa5448542009-02-13 15:32:32 +0000236
Anders Carlssonacfde802009-02-12 00:39:25 +0000237 llvm::SmallVector<QualType, 8> Types;
238 Types.push_back(Ctx.getPointerType(Ctx.VoidTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000239
Anders Carlssonacfde802009-02-12 00:39:25 +0000240 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 Stumpa5448542009-02-13 15:32:32 +0000243
Anders Carlssonacfde802009-02-12 00:39:25 +0000244 return Ctx.getFunctionType(FTy->getResultType(),
Mike Stumpa5448542009-02-13 15:32:32 +0000245 &Types[0], Types.size(),
Anders Carlssonacfde802009-02-12 00:39:25 +0000246 FTy->isVariadic(), 0);
247}
248
Anders Carlssond5cab542009-02-12 17:55:02 +0000249RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E) {
Mike Stumpa5448542009-02-13 15:32:32 +0000250 const BlockPointerType *BPT =
Anders Carlssonacfde802009-02-12 00:39:25 +0000251 E->getCallee()->getType()->getAsBlockPointerType();
Mike Stumpa5448542009-02-13 15:32:32 +0000252
Anders Carlssonacfde802009-02-12 00:39:25 +0000253 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
254
255 // Get a pointer to the generic block literal.
256 const llvm::Type *BlockLiteralTy =
Mike Stump9b8a7972009-02-13 15:25:34 +0000257 llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType());
Anders Carlssonacfde802009-02-12 00:39:25 +0000258
259 // Bitcast the callee to a block literal.
Mike Stumpa5448542009-02-13 15:32:32 +0000260 llvm::Value *BlockLiteral =
Anders Carlssonacfde802009-02-12 00:39:25 +0000261 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 Stumpa5448542009-02-13 15:32:32 +0000268 const llvm::Type *BlockFTy =
Anders Carlssonacfde802009-02-12 00:39:25 +0000269 ConvertType(getBlockFunctionType(getContext(), BPT));
270 const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy);
271 Func = Builder.CreateBitCast(Func, BlockFTyPtr);
272
Mike Stumpa5448542009-02-13 15:32:32 +0000273 BlockLiteral =
274 Builder.CreateBitCast(BlockLiteral,
Anders Carlssonacfde802009-02-12 00:39:25 +0000275 llvm::PointerType::getUnqual(llvm::Type::Int8Ty),
276 "tmp");
Mike Stumpa5448542009-02-13 15:32:32 +0000277
Anders Carlssonacfde802009-02-12 00:39:25 +0000278 // 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 Stumpa5448542009-02-13 15:32:32 +0000282
Anders Carlssonacfde802009-02-12 00:39:25 +0000283 // And the rest of the arguments.
Mike Stumpa5448542009-02-13 15:32:32 +0000284 for (CallExpr::const_arg_iterator i = E->arg_begin(), e = E->arg_end();
Anders Carlssonacfde802009-02-12 00:39:25 +0000285 i != e; ++i)
Mike Stumpa5448542009-02-13 15:32:32 +0000286 Args.push_back(std::make_pair(EmitAnyExprToTemp(*i),
Anders Carlssonacfde802009-02-12 00:39:25 +0000287 i->getType()));
Mike Stumpa5448542009-02-13 15:32:32 +0000288
Anders Carlssonacfde802009-02-12 00:39:25 +0000289 // And call the block.
Mike Stumpa5448542009-02-13 15:32:32 +0000290 return EmitCall(CGM.getTypes().getFunctionInfo(E->getType(), Args),
Anders Carlssonacfde802009-02-12 00:39:25 +0000291 Func, Args);
292}
Anders Carlssond5cab542009-02-12 17:55:02 +0000293
Mike Stump67a64482009-02-14 22:16:35 +0000294llvm::Constant *
295CodeGenModule::GetAddrOfGlobalBlock(const BlockExpr *BE, std::string n) {
Anders Carlssond5cab542009-02-12 17:55:02 +0000296 // Generate the block descriptor.
297 const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy);
Mike Stump7cbb3602009-02-13 16:01:35 +0000298 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
299 getTypes().ConvertType(getContext().IntTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000300
Anders Carlssond5cab542009-02-12 17:55:02 +0000301 llvm::Constant *DescriptorFields[2];
Mike Stumpa5448542009-02-13 15:32:32 +0000302
Anders Carlssond5cab542009-02-12 17:55:02 +0000303 // Reserved
304 DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000305
Anders Carlssond5cab542009-02-12 17:55:02 +0000306 // Block literal size. For global blocks we just use the size of the generic
307 // block literal struct.
Mike Stumpa5448542009-02-13 15:32:32 +0000308 uint64_t BlockLiteralSize =
Mike Stump9b8a7972009-02-13 15:25:34 +0000309 TheTargetData.getTypeStoreSizeInBits(getGenericBlockLiteralType()) / 8;
Anders Carlssond5cab542009-02-12 17:55:02 +0000310 DescriptorFields[1] = llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize);
Mike Stumpa5448542009-02-13 15:32:32 +0000311
312 llvm::Constant *DescriptorStruct =
Anders Carlssond5cab542009-02-12 17:55:02 +0000313 llvm::ConstantStruct::get(&DescriptorFields[0], 2);
Mike Stumpa5448542009-02-13 15:32:32 +0000314
Anders Carlssond5cab542009-02-12 17:55:02 +0000315 llvm::GlobalVariable *Descriptor =
316 new llvm::GlobalVariable(DescriptorStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000317 llvm::GlobalVariable::InternalLinkage,
318 DescriptorStruct, "__block_descriptor_global",
Anders Carlssond5cab542009-02-12 17:55:02 +0000319 &getModule());
Mike Stumpa5448542009-02-13 15:32:32 +0000320
Anders Carlssond5cab542009-02-12 17:55:02 +0000321 // Generate the constants for the block literal.
322 llvm::Constant *LiteralFields[5];
Mike Stumpa5448542009-02-13 15:32:32 +0000323
Mike Stump67a64482009-02-14 22:16:35 +0000324 CodeGenFunction::BlockInfo Info(0, n);
Anders Carlssond5cab542009-02-12 17:55:02 +0000325 llvm::Function *Fn = CodeGenFunction(*this).GenerateBlockFunction(BE, Info);
Mike Stumpa5448542009-02-13 15:32:32 +0000326
Anders Carlssond5cab542009-02-12 17:55:02 +0000327 // isa
Mike Stumpf99f1d02009-02-13 17:23:42 +0000328 LiteralFields[0] = getNSConcreteGlobalBlock();
Mike Stumpa5448542009-02-13 15:32:32 +0000329
Anders Carlssond5cab542009-02-12 17:55:02 +0000330 // Flags
Mike Stumpe5fee252009-02-13 16:19:19 +0000331 LiteralFields[1] = llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL);
Mike Stumpa5448542009-02-13 15:32:32 +0000332
Anders Carlssond5cab542009-02-12 17:55:02 +0000333 // Reserved
Mike Stump7cbb3602009-02-13 16:01:35 +0000334 LiteralFields[2] = llvm::Constant::getNullValue(IntTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000335
Anders Carlssond5cab542009-02-12 17:55:02 +0000336 // Function
337 LiteralFields[3] = Fn;
Mike Stumpa5448542009-02-13 15:32:32 +0000338
Anders Carlssond5cab542009-02-12 17:55:02 +0000339 // Descriptor
340 LiteralFields[4] = Descriptor;
Mike Stumpa5448542009-02-13 15:32:32 +0000341
342 llvm::Constant *BlockLiteralStruct =
Anders Carlssond5cab542009-02-12 17:55:02 +0000343 llvm::ConstantStruct::get(&LiteralFields[0], 5);
Mike Stumpa5448542009-02-13 15:32:32 +0000344
345 llvm::GlobalVariable *BlockLiteral =
Anders Carlssond5cab542009-02-12 17:55:02 +0000346 new llvm::GlobalVariable(BlockLiteralStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000347 llvm::GlobalVariable::InternalLinkage,
348 BlockLiteralStruct, "__block_literal_global",
Anders Carlssond5cab542009-02-12 17:55:02 +0000349 &getModule());
Mike Stumpa5448542009-02-13 15:32:32 +0000350
Anders Carlssond5cab542009-02-12 17:55:02 +0000351 return BlockLiteral;
352}
353
354llvm::Function *CodeGenFunction::GenerateBlockFunction(const BlockExpr *Expr,
355 const BlockInfo& Info)
356{
Mike Stumpa5448542009-02-13 15:32:32 +0000357 const FunctionTypeProto *FTy =
Anders Carlssond5cab542009-02-12 17:55:02 +0000358 cast<FunctionTypeProto>(Expr->getFunctionType());
Mike Stumpa5448542009-02-13 15:32:32 +0000359
Anders Carlssond5cab542009-02-12 17:55:02 +0000360 FunctionArgList Args;
Mike Stumpa5448542009-02-13 15:32:32 +0000361
Anders Carlssond5cab542009-02-12 17:55:02 +0000362 const BlockDecl *BD = Expr->getBlockDecl();
363
364 // FIXME: This leaks
Mike Stumpa5448542009-02-13 15:32:32 +0000365 ImplicitParamDecl *SelfDecl =
Anders Carlssond5cab542009-02-12 17:55:02 +0000366 ImplicitParamDecl::Create(getContext(), 0,
367 SourceLocation(), 0,
368 getContext().getPointerType(getContext().VoidTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000369
Anders Carlssond5cab542009-02-12 17:55:02 +0000370 Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType()));
Mike Stumpa5448542009-02-13 15:32:32 +0000371
372 for (BlockDecl::param_iterator i = BD->param_begin(),
Anders Carlssond5cab542009-02-12 17:55:02 +0000373 e = BD->param_end(); i != e; ++i)
374 Args.push_back(std::make_pair(*e, (*e)->getType()));
Mike Stumpa5448542009-02-13 15:32:32 +0000375
376 const CGFunctionInfo &FI =
Anders Carlssond5cab542009-02-12 17:55:02 +0000377 CGM.getTypes().getFunctionInfo(FTy->getResultType(), Args);
378
Mike Stump67a64482009-02-14 22:16:35 +0000379 std::string Name = std::string("__") + Info.Name + "_block_invoke_";
Anders Carlssond5cab542009-02-12 17:55:02 +0000380 CodeGenTypes &Types = CGM.getTypes();
381 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, FTy->isVariadic());
Mike Stumpa5448542009-02-13 15:32:32 +0000382
383 llvm::Function *Fn =
Anders Carlssond5cab542009-02-12 17:55:02 +0000384 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
385 Name,
386 &CGM.getModule());
Mike Stumpa5448542009-02-13 15:32:32 +0000387
388 StartFunction(BD, FTy->getResultType(), Fn, Args,
Anders Carlssond5cab542009-02-12 17:55:02 +0000389 Expr->getBody()->getLocEnd());
390 EmitStmt(Expr->getBody());
391 FinishFunction(cast<CompoundStmt>(Expr->getBody())->getRBracLoc());
392
393 return Fn;
394}