blob: 9a130d7bc0e0fd7c2c1160bf97281b8f500c0796 [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 Stumpbd65cac2009-02-19 01:01:04 +000049 int sz;
50 if (!BlockHasCopyDispose)
51 sz = CGM.getTargetData()
52 .getTypeStoreSizeInBits(CGM.getGenericBlockLiteralType()) / 8;
53 else
54 sz = CGM.getTargetData()
55 .getTypeStoreSizeInBits(CGM.getGenericExtendedBlockLiteralType()) / 8;
Mike Stump3ba82152009-02-13 17:03:17 +000056 C = llvm::ConstantInt::get(UnsignedLongTy, sz);
Mike Stumpe5fee252009-02-13 16:19:19 +000057 Elts.push_back(C);
58
59 if (BlockHasCopyDispose) {
60 // copy_func_helper_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 // destroy_func_decl
Mike Stump56129b12009-02-13 16:55:51 +000066 C = llvm::ConstantInt::get(UnsignedLongTy, 0);
Mike Stumpe5fee252009-02-13 16:19:19 +000067 C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty);
68 Elts.push_back(C);
69 }
70
71 C = llvm::ConstantStruct::get(Elts);
72
Mike Stumpe5fee252009-02-13 16:19:19 +000073 C = new llvm::GlobalVariable(C->getType(), true,
74 llvm::GlobalValue::InternalLinkage,
Mike Stump7d6dc4f2009-02-13 20:17:16 +000075 C, "__block_descriptor_tmp", &CGM.getModule());
Mike Stumpe5fee252009-02-13 16:19:19 +000076 return C;
77}
78
Mike Stumpf99f1d02009-02-13 17:23:42 +000079llvm::Constant *CodeGenModule::getNSConcreteGlobalBlock() {
80 if (NSConcreteGlobalBlock)
81 return NSConcreteGlobalBlock;
82
83 const llvm::PointerType *PtrToInt8Ty
84 = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Mike Stumpf7448952009-02-13 19:38:12 +000085 // FIXME: We should have a CodeGenModule::AddRuntimeVariable that does the
Mike Stumpf99f1d02009-02-13 17:23:42 +000086 // same thing as CreateRuntimeFunction if there's already a variable with
87 // the same name.
88 NSConcreteGlobalBlock
89 = new llvm::GlobalVariable(PtrToInt8Ty, false,
90 llvm::GlobalValue::ExternalLinkage,
91 0, "_NSConcreteGlobalBlock",
92 &getModule());
93
94 return NSConcreteGlobalBlock;
95}
96
Mike Stump59c5b112009-02-13 19:29:27 +000097llvm::Constant *CodeGenModule::getNSConcreteStackBlock() {
98 if (NSConcreteStackBlock)
99 return NSConcreteStackBlock;
100
101 const llvm::PointerType *PtrToInt8Ty
102 = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Mike Stumpf7448952009-02-13 19:38:12 +0000103 // FIXME: We should have a CodeGenModule::AddRuntimeVariable that does the
Mike Stump59c5b112009-02-13 19:29:27 +0000104 // same thing as CreateRuntimeFunction if there's already a variable with
105 // the same name.
106 NSConcreteStackBlock
107 = new llvm::GlobalVariable(PtrToInt8Ty, false,
108 llvm::GlobalValue::ExternalLinkage,
109 0, "_NSConcreteStackBlock",
110 &getModule());
111
112 return NSConcreteStackBlock;
113}
114
Mike Stumpbd65cac2009-02-19 01:01:04 +0000115// FIXME: Push most into CGM, passing down a few bits, like current
116// function name.
Mike Stump67a64482009-02-14 22:16:35 +0000117llvm::Constant *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) {
Mike Stumpe5fee252009-02-13 16:19:19 +0000118 // FIXME: Push up
119 bool BlockHasCopyDispose = false;
120 bool insideFunction = false;
121 bool BlockRefDeclList = false;
122 bool BlockByrefDeclList = false;
123
124 std::vector<llvm::Constant*> Elts;
125 llvm::Constant *C;
126
Mike Stumpe5fee252009-02-13 16:19:19 +0000127 {
128 // C = BuildBlockStructInitlist();
129 unsigned int flags = BLOCK_HAS_DESCRIPTOR;
130
131 if (BlockHasCopyDispose)
132 flags |= BLOCK_HAS_COPY_DISPOSE;
133
Mike Stump7d6dc4f2009-02-13 20:17:16 +0000134 // __isa
Mike Stump59c5b112009-02-13 19:29:27 +0000135 C = CGM.getNSConcreteStackBlock();
Mike Stumpe5fee252009-02-13 16:19:19 +0000136 if (!insideFunction ||
137 (!BlockRefDeclList && !BlockByrefDeclList)) {
Mike Stumpf99f1d02009-02-13 17:23:42 +0000138 C = CGM.getNSConcreteGlobalBlock();
Mike Stumpe5fee252009-02-13 16:19:19 +0000139 flags |= BLOCK_IS_GLOBAL;
140 }
Mike Stump59c5b112009-02-13 19:29:27 +0000141 const llvm::PointerType *PtrToInt8Ty
142 = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Mike Stumpe5fee252009-02-13 16:19:19 +0000143 C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty);
144 Elts.push_back(C);
145
146 // __flags
147 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
148 CGM.getTypes().ConvertType(CGM.getContext().IntTy));
149 C = llvm::ConstantInt::get(IntTy, flags);
150 Elts.push_back(C);
151
152 // __reserved
153 C = llvm::ConstantInt::get(IntTy, 0);
154 Elts.push_back(C);
155
Mike Stumpbd65cac2009-02-19 01:01:04 +0000156 // __invoke
Mike Stumpcb717222009-02-17 17:18:36 +0000157 const char *Name = "";
Mike Stump67a64482009-02-14 22:16:35 +0000158 if (const NamedDecl *ND = dyn_cast<NamedDecl>(CurFuncDecl))
Mike Stump19050612009-02-17 23:25:52 +0000159 if (ND->getIdentifier())
160 Name = ND->getNameAsCString();
Mike Stump67a64482009-02-14 22:16:35 +0000161 BlockInfo Info(0, Name);
162 llvm::Function *Fn = CodeGenFunction(*this).GenerateBlockFunction(BE, Info);
163 Elts.push_back(Fn);
Mike Stumpe5fee252009-02-13 16:19:19 +0000164
165 // __descriptor
166 Elts.push_back(BuildDescriptorBlockDecl());
167
168 // FIXME: Add block_original_ref_decl_list and block_byref_decl_list.
169 }
170
171 C = llvm::ConstantStruct::get(Elts);
172
173 char Name[32];
Mike Stump26efc332009-02-13 18:36:05 +0000174 sprintf(Name, "__block_holder_tmp_%d", CGM.getGlobalUniqueCount());
Mike Stumpe5fee252009-02-13 16:19:19 +0000175 C = new llvm::GlobalVariable(C->getType(), true,
176 llvm::GlobalValue::InternalLinkage,
177 C, Name, &CGM.getModule());
Mike Stumpbd65cac2009-02-19 01:01:04 +0000178 QualType BPT = BE->getType();
179 C = llvm::ConstantExpr::getBitCast(C, ConvertType(BPT));
Mike Stumpe5fee252009-02-13 16:19:19 +0000180 return C;
181}
182
183
184
185
Mike Stumpab695142009-02-13 15:16:56 +0000186const llvm::Type *CodeGenModule::getBlockDescriptorType() {
187 if (BlockDescriptorType)
188 return BlockDescriptorType;
189
Mike Stumpa5448542009-02-13 15:32:32 +0000190 const llvm::Type *UnsignedLongTy =
Mike Stumpab695142009-02-13 15:16:56 +0000191 getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000192
Mike Stumpab695142009-02-13 15:16:56 +0000193 // struct __block_descriptor {
194 // unsigned long reserved;
195 // unsigned long block_size;
196 // };
Mike Stumpa5448542009-02-13 15:32:32 +0000197 BlockDescriptorType = llvm::StructType::get(UnsignedLongTy,
198 UnsignedLongTy,
Mike Stumpab695142009-02-13 15:16:56 +0000199 NULL);
200
201 getModule().addTypeName("struct.__block_descriptor",
202 BlockDescriptorType);
203
204 return BlockDescriptorType;
Anders Carlssonacfde802009-02-12 00:39:25 +0000205}
206
Mike Stump9b8a7972009-02-13 15:25:34 +0000207const llvm::Type *
208CodeGenModule::getGenericBlockLiteralType() {
209 if (GenericBlockLiteralType)
210 return GenericBlockLiteralType;
211
Mike Stumpa5448542009-02-13 15:32:32 +0000212 const llvm::Type *Int8PtrTy =
Mike Stump9b8a7972009-02-13 15:25:34 +0000213 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Mike Stumpa5448542009-02-13 15:32:32 +0000214
215 const llvm::Type *BlockDescPtrTy =
Mike Stump9b8a7972009-02-13 15:25:34 +0000216 llvm::PointerType::getUnqual(getBlockDescriptorType());
Mike Stumpa5448542009-02-13 15:32:32 +0000217
Mike Stump7cbb3602009-02-13 16:01:35 +0000218 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
219 getTypes().ConvertType(getContext().IntTy));
220
Mike Stump9b8a7972009-02-13 15:25:34 +0000221 // struct __block_literal_generic {
Mike Stumpbd65cac2009-02-19 01:01:04 +0000222 // void *__isa;
223 // int __flags;
224 // int __reserved;
225 // void (*__invoke)(void *);
226 // struct __block_descriptor *__descriptor;
Mike Stump9b8a7972009-02-13 15:25:34 +0000227 // };
228 GenericBlockLiteralType = llvm::StructType::get(Int8PtrTy,
Mike Stump7cbb3602009-02-13 16:01:35 +0000229 IntTy,
230 IntTy,
Mike Stump9b8a7972009-02-13 15:25:34 +0000231 Int8PtrTy,
232 BlockDescPtrTy,
233 NULL);
Mike Stumpa5448542009-02-13 15:32:32 +0000234
Mike Stump9b8a7972009-02-13 15:25:34 +0000235 getModule().addTypeName("struct.__block_literal_generic",
236 GenericBlockLiteralType);
Mike Stumpa5448542009-02-13 15:32:32 +0000237
Mike Stump9b8a7972009-02-13 15:25:34 +0000238 return GenericBlockLiteralType;
Anders Carlssonacfde802009-02-12 00:39:25 +0000239}
240
Mike Stumpbd65cac2009-02-19 01:01:04 +0000241const llvm::Type *
242CodeGenModule::getGenericExtendedBlockLiteralType() {
243 if (GenericExtendedBlockLiteralType)
244 return GenericExtendedBlockLiteralType;
245
246 const llvm::Type *Int8PtrTy =
247 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
248
249 const llvm::Type *BlockDescPtrTy =
250 llvm::PointerType::getUnqual(getBlockDescriptorType());
251
252 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
253 getTypes().ConvertType(getContext().IntTy));
254
255 // struct __block_literal_generic {
256 // void *__isa;
257 // int __flags;
258 // int __reserved;
259 // void (*__invoke)(void *);
260 // struct __block_descriptor *__descriptor;
261 // void *__copy_func_helper_decl;
262 // void *__destroy_func_decl;
263 // };
264 GenericExtendedBlockLiteralType = llvm::StructType::get(Int8PtrTy,
265 IntTy,
266 IntTy,
267 Int8PtrTy,
268 BlockDescPtrTy,
269 Int8PtrTy,
270 Int8PtrTy,
271 NULL);
272
273 getModule().addTypeName("struct.__block_literal_extended_generic",
274 GenericExtendedBlockLiteralType);
275
276 return GenericExtendedBlockLiteralType;
277}
278
Mike Stumpa5448542009-02-13 15:32:32 +0000279/// getBlockFunctionType - Given a BlockPointerType, will return the
Anders Carlssonacfde802009-02-12 00:39:25 +0000280/// function type for the block, including the first block literal argument.
281static QualType getBlockFunctionType(ASTContext &Ctx,
Anders Carlssond5cab542009-02-12 17:55:02 +0000282 const BlockPointerType *BPT) {
Anders Carlssonacfde802009-02-12 00:39:25 +0000283 const FunctionTypeProto *FTy = cast<FunctionTypeProto>(BPT->getPointeeType());
Mike Stumpa5448542009-02-13 15:32:32 +0000284
Anders Carlssonacfde802009-02-12 00:39:25 +0000285 llvm::SmallVector<QualType, 8> Types;
286 Types.push_back(Ctx.getPointerType(Ctx.VoidTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000287
Anders Carlssonacfde802009-02-12 00:39:25 +0000288 for (FunctionTypeProto::arg_type_iterator i = FTy->arg_type_begin(),
289 e = FTy->arg_type_end(); i != e; ++i)
290 Types.push_back(*i);
Mike Stumpa5448542009-02-13 15:32:32 +0000291
Anders Carlssonacfde802009-02-12 00:39:25 +0000292 return Ctx.getFunctionType(FTy->getResultType(),
Mike Stumpa5448542009-02-13 15:32:32 +0000293 &Types[0], Types.size(),
Anders Carlssonacfde802009-02-12 00:39:25 +0000294 FTy->isVariadic(), 0);
295}
296
Anders Carlssond5cab542009-02-12 17:55:02 +0000297RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E) {
Mike Stumpa5448542009-02-13 15:32:32 +0000298 const BlockPointerType *BPT =
Anders Carlssonacfde802009-02-12 00:39:25 +0000299 E->getCallee()->getType()->getAsBlockPointerType();
Mike Stumpa5448542009-02-13 15:32:32 +0000300
Anders Carlssonacfde802009-02-12 00:39:25 +0000301 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
302
303 // Get a pointer to the generic block literal.
304 const llvm::Type *BlockLiteralTy =
Mike Stump9b8a7972009-02-13 15:25:34 +0000305 llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType());
Anders Carlssonacfde802009-02-12 00:39:25 +0000306
307 // Bitcast the callee to a block literal.
Mike Stumpa5448542009-02-13 15:32:32 +0000308 llvm::Value *BlockLiteral =
Anders Carlssonacfde802009-02-12 00:39:25 +0000309 Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal");
310
311 // Get the function pointer from the literal.
312 llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp");
313 llvm::Value *Func = Builder.CreateLoad(FuncPtr, FuncPtr, "tmp");
314
315 // Cast the function pointer to the right type.
Mike Stumpa5448542009-02-13 15:32:32 +0000316 const llvm::Type *BlockFTy =
Anders Carlssonacfde802009-02-12 00:39:25 +0000317 ConvertType(getBlockFunctionType(getContext(), BPT));
318 const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy);
319 Func = Builder.CreateBitCast(Func, BlockFTyPtr);
320
Mike Stumpa5448542009-02-13 15:32:32 +0000321 BlockLiteral =
322 Builder.CreateBitCast(BlockLiteral,
Anders Carlssonacfde802009-02-12 00:39:25 +0000323 llvm::PointerType::getUnqual(llvm::Type::Int8Ty),
324 "tmp");
Mike Stumpa5448542009-02-13 15:32:32 +0000325
Anders Carlssonacfde802009-02-12 00:39:25 +0000326 // Add the block literal.
327 QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy);
328 CallArgList Args;
329 Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000330
Anders Carlssonacfde802009-02-12 00:39:25 +0000331 // And the rest of the arguments.
Mike Stumpa5448542009-02-13 15:32:32 +0000332 for (CallExpr::const_arg_iterator i = E->arg_begin(), e = E->arg_end();
Anders Carlssonacfde802009-02-12 00:39:25 +0000333 i != e; ++i)
Mike Stumpa5448542009-02-13 15:32:32 +0000334 Args.push_back(std::make_pair(EmitAnyExprToTemp(*i),
Anders Carlssonacfde802009-02-12 00:39:25 +0000335 i->getType()));
Mike Stumpa5448542009-02-13 15:32:32 +0000336
Anders Carlssonacfde802009-02-12 00:39:25 +0000337 // And call the block.
Mike Stumpa5448542009-02-13 15:32:32 +0000338 return EmitCall(CGM.getTypes().getFunctionInfo(E->getType(), Args),
Anders Carlssonacfde802009-02-12 00:39:25 +0000339 Func, Args);
340}
Anders Carlssond5cab542009-02-12 17:55:02 +0000341
Mike Stump67a64482009-02-14 22:16:35 +0000342llvm::Constant *
Mike Stump30395dd2009-02-14 22:49:33 +0000343CodeGenModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) {
Anders Carlssond5cab542009-02-12 17:55:02 +0000344 // Generate the block descriptor.
345 const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy);
Mike Stump7cbb3602009-02-13 16:01:35 +0000346 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
347 getTypes().ConvertType(getContext().IntTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000348
Anders Carlssond5cab542009-02-12 17:55:02 +0000349 llvm::Constant *DescriptorFields[2];
Mike Stumpa5448542009-02-13 15:32:32 +0000350
Anders Carlssond5cab542009-02-12 17:55:02 +0000351 // Reserved
352 DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000353
Anders Carlssond5cab542009-02-12 17:55:02 +0000354 // Block literal size. For global blocks we just use the size of the generic
355 // block literal struct.
Mike Stumpa5448542009-02-13 15:32:32 +0000356 uint64_t BlockLiteralSize =
Mike Stump9b8a7972009-02-13 15:25:34 +0000357 TheTargetData.getTypeStoreSizeInBits(getGenericBlockLiteralType()) / 8;
Anders Carlssond5cab542009-02-12 17:55:02 +0000358 DescriptorFields[1] = llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize);
Mike Stumpa5448542009-02-13 15:32:32 +0000359
360 llvm::Constant *DescriptorStruct =
Anders Carlssond5cab542009-02-12 17:55:02 +0000361 llvm::ConstantStruct::get(&DescriptorFields[0], 2);
Mike Stumpa5448542009-02-13 15:32:32 +0000362
Anders Carlssond5cab542009-02-12 17:55:02 +0000363 llvm::GlobalVariable *Descriptor =
364 new llvm::GlobalVariable(DescriptorStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000365 llvm::GlobalVariable::InternalLinkage,
366 DescriptorStruct, "__block_descriptor_global",
Anders Carlssond5cab542009-02-12 17:55:02 +0000367 &getModule());
Mike Stumpa5448542009-02-13 15:32:32 +0000368
Anders Carlssond5cab542009-02-12 17:55:02 +0000369 // Generate the constants for the block literal.
370 llvm::Constant *LiteralFields[5];
Mike Stumpa5448542009-02-13 15:32:32 +0000371
Mike Stump67a64482009-02-14 22:16:35 +0000372 CodeGenFunction::BlockInfo Info(0, n);
Anders Carlssond5cab542009-02-12 17:55:02 +0000373 llvm::Function *Fn = CodeGenFunction(*this).GenerateBlockFunction(BE, Info);
Mike Stumpa5448542009-02-13 15:32:32 +0000374
Anders Carlssond5cab542009-02-12 17:55:02 +0000375 // isa
Mike Stumpf99f1d02009-02-13 17:23:42 +0000376 LiteralFields[0] = getNSConcreteGlobalBlock();
Mike Stumpa5448542009-02-13 15:32:32 +0000377
Anders Carlssond5cab542009-02-12 17:55:02 +0000378 // Flags
Mike Stumpe5fee252009-02-13 16:19:19 +0000379 LiteralFields[1] = llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL);
Mike Stumpa5448542009-02-13 15:32:32 +0000380
Anders Carlssond5cab542009-02-12 17:55:02 +0000381 // Reserved
Mike Stump7cbb3602009-02-13 16:01:35 +0000382 LiteralFields[2] = llvm::Constant::getNullValue(IntTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000383
Anders Carlssond5cab542009-02-12 17:55:02 +0000384 // Function
385 LiteralFields[3] = Fn;
Mike Stumpa5448542009-02-13 15:32:32 +0000386
Anders Carlssond5cab542009-02-12 17:55:02 +0000387 // Descriptor
388 LiteralFields[4] = Descriptor;
Mike Stumpa5448542009-02-13 15:32:32 +0000389
390 llvm::Constant *BlockLiteralStruct =
Anders Carlssond5cab542009-02-12 17:55:02 +0000391 llvm::ConstantStruct::get(&LiteralFields[0], 5);
Mike Stumpa5448542009-02-13 15:32:32 +0000392
393 llvm::GlobalVariable *BlockLiteral =
Anders Carlssond5cab542009-02-12 17:55:02 +0000394 new llvm::GlobalVariable(BlockLiteralStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000395 llvm::GlobalVariable::InternalLinkage,
396 BlockLiteralStruct, "__block_literal_global",
Anders Carlssond5cab542009-02-12 17:55:02 +0000397 &getModule());
Mike Stumpa5448542009-02-13 15:32:32 +0000398
Anders Carlssond5cab542009-02-12 17:55:02 +0000399 return BlockLiteral;
400}
401
402llvm::Function *CodeGenFunction::GenerateBlockFunction(const BlockExpr *Expr,
403 const BlockInfo& Info)
404{
Mike Stumpa5448542009-02-13 15:32:32 +0000405 const FunctionTypeProto *FTy =
Anders Carlssond5cab542009-02-12 17:55:02 +0000406 cast<FunctionTypeProto>(Expr->getFunctionType());
Mike Stumpa5448542009-02-13 15:32:32 +0000407
Anders Carlssond5cab542009-02-12 17:55:02 +0000408 FunctionArgList Args;
Mike Stumpa5448542009-02-13 15:32:32 +0000409
Anders Carlssond5cab542009-02-12 17:55:02 +0000410 const BlockDecl *BD = Expr->getBlockDecl();
411
412 // FIXME: This leaks
Mike Stumpa5448542009-02-13 15:32:32 +0000413 ImplicitParamDecl *SelfDecl =
Anders Carlssond5cab542009-02-12 17:55:02 +0000414 ImplicitParamDecl::Create(getContext(), 0,
415 SourceLocation(), 0,
416 getContext().getPointerType(getContext().VoidTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000417
Anders Carlssond5cab542009-02-12 17:55:02 +0000418 Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType()));
Mike Stumpa5448542009-02-13 15:32:32 +0000419
420 for (BlockDecl::param_iterator i = BD->param_begin(),
Anders Carlssond5cab542009-02-12 17:55:02 +0000421 e = BD->param_end(); i != e; ++i)
Mike Stump19050612009-02-17 23:25:52 +0000422 Args.push_back(std::make_pair(*i, (*i)->getType()));
Mike Stumpa5448542009-02-13 15:32:32 +0000423
424 const CGFunctionInfo &FI =
Anders Carlssond5cab542009-02-12 17:55:02 +0000425 CGM.getTypes().getFunctionInfo(FTy->getResultType(), Args);
426
Mike Stump67a64482009-02-14 22:16:35 +0000427 std::string Name = std::string("__") + Info.Name + "_block_invoke_";
Anders Carlssond5cab542009-02-12 17:55:02 +0000428 CodeGenTypes &Types = CGM.getTypes();
429 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, FTy->isVariadic());
Mike Stumpa5448542009-02-13 15:32:32 +0000430
431 llvm::Function *Fn =
Anders Carlssond5cab542009-02-12 17:55:02 +0000432 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
433 Name,
434 &CGM.getModule());
Mike Stumpa5448542009-02-13 15:32:32 +0000435
436 StartFunction(BD, FTy->getResultType(), Fn, Args,
Anders Carlssond5cab542009-02-12 17:55:02 +0000437 Expr->getBody()->getLocEnd());
438 EmitStmt(Expr->getBody());
439 FinishFunction(cast<CompoundStmt>(Expr->getBody())->getRBracLoc());
440
441 return Fn;
442}