blob: 60bf73f0b413be818bb9f46b0d543dbbc5bc3166 [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 Stump4e7a1f72009-02-21 20:00:35 +000033llvm::Constant *CodeGenFunction::BuildDescriptorBlockDecl(uint64_t Size) {
Mike Stumpe5fee252009-02-13 16:19:19 +000034 const llvm::PointerType *PtrToInt8Ty
35 = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Mike Stump56129b12009-02-13 16:55:51 +000036 const llvm::Type *UnsignedLongTy
37 = CGM.getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpe5fee252009-02-13 16:19:19 +000038 llvm::Constant *C;
39 std::vector<llvm::Constant*> Elts;
40
41 // reserved
Mike Stump56129b12009-02-13 16:55:51 +000042 C = llvm::ConstantInt::get(UnsignedLongTy, 0);
Mike Stumpe5fee252009-02-13 16:19:19 +000043 Elts.push_back(C);
44
45 // Size
Mike Stumpd6840002009-02-21 20:07:44 +000046 // FIXME: What is the right way to say this doesn't fit? We should give
47 // a user diagnostic in that case. Better fix would be to change the
48 // API to size_t.
Mike Stump4e7a1f72009-02-21 20:00:35 +000049 C = llvm::ConstantInt::get(UnsignedLongTy, Size);
Mike Stumpe5fee252009-02-13 16:19:19 +000050 Elts.push_back(C);
51
52 if (BlockHasCopyDispose) {
53 // copy_func_helper_decl
Mike Stump4e7a1f72009-02-21 20:00:35 +000054 // FIXME: implement
Mike Stump56129b12009-02-13 16:55:51 +000055 C = llvm::ConstantInt::get(UnsignedLongTy, 0);
Mike Stumpe5fee252009-02-13 16:19:19 +000056 C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty);
57 Elts.push_back(C);
58
59 // destroy_func_decl
Mike Stump4e7a1f72009-02-21 20:00:35 +000060 // FIXME: implement
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 Stumpbd65cac2009-02-19 01:01:04 +0000110// FIXME: Push most into CGM, passing down a few bits, like current
111// function name.
Mike Stump67a64482009-02-14 22:16:35 +0000112llvm::Constant *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) {
Mike Stumpe5fee252009-02-13 16:19:19 +0000113 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
Mike Stumpbd65cac2009-02-19 01:01:04 +0000149 // __invoke
Mike Stumpcb717222009-02-17 17:18:36 +0000150 const char *Name = "";
Mike Stump67a64482009-02-14 22:16:35 +0000151 if (const NamedDecl *ND = dyn_cast<NamedDecl>(CurFuncDecl))
Mike Stump19050612009-02-17 23:25:52 +0000152 if (ND->getIdentifier())
153 Name = ND->getNameAsCString();
Mike Stump67a64482009-02-14 22:16:35 +0000154 BlockInfo Info(0, Name);
Mike Stump4e7a1f72009-02-21 20:00:35 +0000155 uint64_t subBlockSize;
156 llvm::Function *Fn
157 = CodeGenFunction(*this).GenerateBlockFunction(BE, Info, subBlockSize);
Mike Stump67a64482009-02-14 22:16:35 +0000158 Elts.push_back(Fn);
Mike Stumpe5fee252009-02-13 16:19:19 +0000159
160 // __descriptor
Mike Stump4e7a1f72009-02-21 20:00:35 +0000161 Elts.push_back(BuildDescriptorBlockDecl(subBlockSize));
Mike Stumpe5fee252009-02-13 16:19:19 +0000162
163 // FIXME: Add block_original_ref_decl_list and block_byref_decl_list.
164 }
165
166 C = llvm::ConstantStruct::get(Elts);
167
168 char Name[32];
Mike Stump26efc332009-02-13 18:36:05 +0000169 sprintf(Name, "__block_holder_tmp_%d", CGM.getGlobalUniqueCount());
Mike Stumpe5fee252009-02-13 16:19:19 +0000170 C = new llvm::GlobalVariable(C->getType(), true,
171 llvm::GlobalValue::InternalLinkage,
172 C, Name, &CGM.getModule());
Mike Stumpbd65cac2009-02-19 01:01:04 +0000173 QualType BPT = BE->getType();
174 C = llvm::ConstantExpr::getBitCast(C, ConvertType(BPT));
Mike Stumpe5fee252009-02-13 16:19:19 +0000175 return C;
176}
177
178
179
180
Mike Stumpab695142009-02-13 15:16:56 +0000181const llvm::Type *CodeGenModule::getBlockDescriptorType() {
182 if (BlockDescriptorType)
183 return BlockDescriptorType;
184
Mike Stumpa5448542009-02-13 15:32:32 +0000185 const llvm::Type *UnsignedLongTy =
Mike Stumpab695142009-02-13 15:16:56 +0000186 getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000187
Mike Stumpab695142009-02-13 15:16:56 +0000188 // struct __block_descriptor {
189 // unsigned long reserved;
190 // unsigned long block_size;
191 // };
Mike Stumpa5448542009-02-13 15:32:32 +0000192 BlockDescriptorType = llvm::StructType::get(UnsignedLongTy,
193 UnsignedLongTy,
Mike Stumpab695142009-02-13 15:16:56 +0000194 NULL);
195
Mike Stump4e7a1f72009-02-21 20:00:35 +0000196 // FIXME: This breaks an unrelated testcase in the testsuite, we
197 // _want_ llvm to not use structural equality, sometimes. What
198 // should we do, modify the testcase and do this anyway, or...
199#if 0
Mike Stumpab695142009-02-13 15:16:56 +0000200 getModule().addTypeName("struct.__block_descriptor",
201 BlockDescriptorType);
Mike Stump4e7a1f72009-02-21 20:00:35 +0000202#endif
Mike Stumpab695142009-02-13 15:16:56 +0000203
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 Stump4e7a1f72009-02-21 20:00:35 +0000235 // FIXME: See struct.__block_descriptor
Mike Stump9b8a7972009-02-13 15:25:34 +0000236 getModule().addTypeName("struct.__block_literal_generic",
237 GenericBlockLiteralType);
Mike Stumpa5448542009-02-13 15:32:32 +0000238
Mike Stump9b8a7972009-02-13 15:25:34 +0000239 return GenericBlockLiteralType;
Anders Carlssonacfde802009-02-12 00:39:25 +0000240}
241
Mike Stumpbd65cac2009-02-19 01:01:04 +0000242const llvm::Type *
243CodeGenModule::getGenericExtendedBlockLiteralType() {
244 if (GenericExtendedBlockLiteralType)
245 return GenericExtendedBlockLiteralType;
246
247 const llvm::Type *Int8PtrTy =
248 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
249
250 const llvm::Type *BlockDescPtrTy =
251 llvm::PointerType::getUnqual(getBlockDescriptorType());
252
253 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
254 getTypes().ConvertType(getContext().IntTy));
255
256 // struct __block_literal_generic {
257 // void *__isa;
258 // int __flags;
259 // int __reserved;
260 // void (*__invoke)(void *);
261 // struct __block_descriptor *__descriptor;
262 // void *__copy_func_helper_decl;
263 // void *__destroy_func_decl;
264 // };
265 GenericExtendedBlockLiteralType = llvm::StructType::get(Int8PtrTy,
266 IntTy,
267 IntTy,
268 Int8PtrTy,
269 BlockDescPtrTy,
270 Int8PtrTy,
271 Int8PtrTy,
272 NULL);
273
Mike Stump4e7a1f72009-02-21 20:00:35 +0000274 // FIXME: See struct.__block_descriptor
Mike Stumpbd65cac2009-02-19 01:01:04 +0000275 getModule().addTypeName("struct.__block_literal_extended_generic",
276 GenericExtendedBlockLiteralType);
277
278 return GenericExtendedBlockLiteralType;
279}
280
Mike Stumpa5448542009-02-13 15:32:32 +0000281/// getBlockFunctionType - Given a BlockPointerType, will return the
Anders Carlssonacfde802009-02-12 00:39:25 +0000282/// function type for the block, including the first block literal argument.
283static QualType getBlockFunctionType(ASTContext &Ctx,
Anders Carlssond5cab542009-02-12 17:55:02 +0000284 const BlockPointerType *BPT) {
Anders Carlssonacfde802009-02-12 00:39:25 +0000285 const FunctionTypeProto *FTy = cast<FunctionTypeProto>(BPT->getPointeeType());
Mike Stumpa5448542009-02-13 15:32:32 +0000286
Anders Carlssonacfde802009-02-12 00:39:25 +0000287 llvm::SmallVector<QualType, 8> Types;
288 Types.push_back(Ctx.getPointerType(Ctx.VoidTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000289
Anders Carlssonacfde802009-02-12 00:39:25 +0000290 for (FunctionTypeProto::arg_type_iterator i = FTy->arg_type_begin(),
291 e = FTy->arg_type_end(); i != e; ++i)
292 Types.push_back(*i);
Mike Stumpa5448542009-02-13 15:32:32 +0000293
Anders Carlssonacfde802009-02-12 00:39:25 +0000294 return Ctx.getFunctionType(FTy->getResultType(),
Mike Stumpa5448542009-02-13 15:32:32 +0000295 &Types[0], Types.size(),
Anders Carlssonacfde802009-02-12 00:39:25 +0000296 FTy->isVariadic(), 0);
297}
298
Anders Carlssond5cab542009-02-12 17:55:02 +0000299RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E) {
Mike Stumpa5448542009-02-13 15:32:32 +0000300 const BlockPointerType *BPT =
Anders Carlssonacfde802009-02-12 00:39:25 +0000301 E->getCallee()->getType()->getAsBlockPointerType();
Mike Stumpa5448542009-02-13 15:32:32 +0000302
Anders Carlssonacfde802009-02-12 00:39:25 +0000303 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
304
305 // Get a pointer to the generic block literal.
306 const llvm::Type *BlockLiteralTy =
Mike Stump9b8a7972009-02-13 15:25:34 +0000307 llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType());
Anders Carlssonacfde802009-02-12 00:39:25 +0000308
309 // Bitcast the callee to a block literal.
Mike Stumpa5448542009-02-13 15:32:32 +0000310 llvm::Value *BlockLiteral =
Anders Carlssonacfde802009-02-12 00:39:25 +0000311 Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal");
312
313 // Get the function pointer from the literal.
314 llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp");
Mike Stump4e7a1f72009-02-21 20:00:35 +0000315 // FIXME: second argument should be false?
Anders Carlssonacfde802009-02-12 00:39:25 +0000316 llvm::Value *Func = Builder.CreateLoad(FuncPtr, FuncPtr, "tmp");
317
318 // Cast the function pointer to the right type.
Mike Stumpa5448542009-02-13 15:32:32 +0000319 const llvm::Type *BlockFTy =
Anders Carlssonacfde802009-02-12 00:39:25 +0000320 ConvertType(getBlockFunctionType(getContext(), BPT));
321 const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy);
322 Func = Builder.CreateBitCast(Func, BlockFTyPtr);
323
Mike Stumpa5448542009-02-13 15:32:32 +0000324 BlockLiteral =
325 Builder.CreateBitCast(BlockLiteral,
Anders Carlssonacfde802009-02-12 00:39:25 +0000326 llvm::PointerType::getUnqual(llvm::Type::Int8Ty),
327 "tmp");
Mike Stumpa5448542009-02-13 15:32:32 +0000328
Anders Carlssonacfde802009-02-12 00:39:25 +0000329 // Add the block literal.
330 QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy);
331 CallArgList Args;
332 Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000333
Anders Carlssonacfde802009-02-12 00:39:25 +0000334 // And the rest of the arguments.
Mike Stumpa5448542009-02-13 15:32:32 +0000335 for (CallExpr::const_arg_iterator i = E->arg_begin(), e = E->arg_end();
Anders Carlssonacfde802009-02-12 00:39:25 +0000336 i != e; ++i)
Mike Stumpa5448542009-02-13 15:32:32 +0000337 Args.push_back(std::make_pair(EmitAnyExprToTemp(*i),
Anders Carlssonacfde802009-02-12 00:39:25 +0000338 i->getType()));
Mike Stumpa5448542009-02-13 15:32:32 +0000339
Anders Carlssonacfde802009-02-12 00:39:25 +0000340 // And call the block.
Mike Stumpa5448542009-02-13 15:32:32 +0000341 return EmitCall(CGM.getTypes().getFunctionInfo(E->getType(), Args),
Anders Carlssonacfde802009-02-12 00:39:25 +0000342 Func, Args);
343}
Anders Carlssond5cab542009-02-12 17:55:02 +0000344
Mike Stump67a64482009-02-14 22:16:35 +0000345llvm::Constant *
Mike Stump30395dd2009-02-14 22:49:33 +0000346CodeGenModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) {
Anders Carlssond5cab542009-02-12 17:55:02 +0000347 // Generate the block descriptor.
348 const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy);
Mike Stump7cbb3602009-02-13 16:01:35 +0000349 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
350 getTypes().ConvertType(getContext().IntTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000351
Anders Carlssond5cab542009-02-12 17:55:02 +0000352 llvm::Constant *DescriptorFields[2];
Mike Stumpa5448542009-02-13 15:32:32 +0000353
Anders Carlssond5cab542009-02-12 17:55:02 +0000354 // Reserved
355 DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000356
Anders Carlssond5cab542009-02-12 17:55:02 +0000357 // Block literal size. For global blocks we just use the size of the generic
358 // block literal struct.
Mike Stumpa5448542009-02-13 15:32:32 +0000359 uint64_t BlockLiteralSize =
Mike Stump9b8a7972009-02-13 15:25:34 +0000360 TheTargetData.getTypeStoreSizeInBits(getGenericBlockLiteralType()) / 8;
Anders Carlssond5cab542009-02-12 17:55:02 +0000361 DescriptorFields[1] = llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize);
Mike Stumpa5448542009-02-13 15:32:32 +0000362
363 llvm::Constant *DescriptorStruct =
Anders Carlssond5cab542009-02-12 17:55:02 +0000364 llvm::ConstantStruct::get(&DescriptorFields[0], 2);
Mike Stumpa5448542009-02-13 15:32:32 +0000365
Anders Carlssond5cab542009-02-12 17:55:02 +0000366 llvm::GlobalVariable *Descriptor =
367 new llvm::GlobalVariable(DescriptorStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000368 llvm::GlobalVariable::InternalLinkage,
369 DescriptorStruct, "__block_descriptor_global",
Anders Carlssond5cab542009-02-12 17:55:02 +0000370 &getModule());
Mike Stumpa5448542009-02-13 15:32:32 +0000371
Anders Carlssond5cab542009-02-12 17:55:02 +0000372 // Generate the constants for the block literal.
373 llvm::Constant *LiteralFields[5];
Mike Stumpa5448542009-02-13 15:32:32 +0000374
Mike Stump67a64482009-02-14 22:16:35 +0000375 CodeGenFunction::BlockInfo Info(0, n);
Mike Stump4e7a1f72009-02-21 20:00:35 +0000376 uint64_t subBlockSize;
377 llvm::Function *Fn
378 = CodeGenFunction(*this).GenerateBlockFunction(BE, Info, subBlockSize);
379 assert(subBlockSize == BlockLiteralSize
380 && "no imports allowed for global block");
Mike Stumpa5448542009-02-13 15:32:32 +0000381
Anders Carlssond5cab542009-02-12 17:55:02 +0000382 // isa
Mike Stumpf99f1d02009-02-13 17:23:42 +0000383 LiteralFields[0] = getNSConcreteGlobalBlock();
Mike Stumpa5448542009-02-13 15:32:32 +0000384
Anders Carlssond5cab542009-02-12 17:55:02 +0000385 // Flags
Mike Stumpe5fee252009-02-13 16:19:19 +0000386 LiteralFields[1] = llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL);
Mike Stumpa5448542009-02-13 15:32:32 +0000387
Anders Carlssond5cab542009-02-12 17:55:02 +0000388 // Reserved
Mike Stump7cbb3602009-02-13 16:01:35 +0000389 LiteralFields[2] = llvm::Constant::getNullValue(IntTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000390
Anders Carlssond5cab542009-02-12 17:55:02 +0000391 // Function
392 LiteralFields[3] = Fn;
Mike Stumpa5448542009-02-13 15:32:32 +0000393
Anders Carlssond5cab542009-02-12 17:55:02 +0000394 // Descriptor
395 LiteralFields[4] = Descriptor;
Mike Stumpa5448542009-02-13 15:32:32 +0000396
397 llvm::Constant *BlockLiteralStruct =
Anders Carlssond5cab542009-02-12 17:55:02 +0000398 llvm::ConstantStruct::get(&LiteralFields[0], 5);
Mike Stumpa5448542009-02-13 15:32:32 +0000399
400 llvm::GlobalVariable *BlockLiteral =
Anders Carlssond5cab542009-02-12 17:55:02 +0000401 new llvm::GlobalVariable(BlockLiteralStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000402 llvm::GlobalVariable::InternalLinkage,
403 BlockLiteralStruct, "__block_literal_global",
Anders Carlssond5cab542009-02-12 17:55:02 +0000404 &getModule());
Mike Stumpa5448542009-02-13 15:32:32 +0000405
Anders Carlssond5cab542009-02-12 17:55:02 +0000406 return BlockLiteral;
407}
408
Mike Stump4e7a1f72009-02-21 20:00:35 +0000409llvm::Value *CodeGenFunction::LoadBlockStruct() {
410 return Builder.CreateLoad(LocalDeclMap[getBlockStructDecl()], "self");
411}
412
Anders Carlssond5cab542009-02-12 17:55:02 +0000413llvm::Function *CodeGenFunction::GenerateBlockFunction(const BlockExpr *Expr,
Mike Stump4e7a1f72009-02-21 20:00:35 +0000414 const BlockInfo& Info,
415 uint64_t &Size) {
Mike Stumpa5448542009-02-13 15:32:32 +0000416 const FunctionTypeProto *FTy =
Anders Carlssond5cab542009-02-12 17:55:02 +0000417 cast<FunctionTypeProto>(Expr->getFunctionType());
Mike Stumpa5448542009-02-13 15:32:32 +0000418
Anders Carlssond5cab542009-02-12 17:55:02 +0000419 FunctionArgList Args;
Mike Stumpa5448542009-02-13 15:32:32 +0000420
Anders Carlssond5cab542009-02-12 17:55:02 +0000421 const BlockDecl *BD = Expr->getBlockDecl();
422
423 // FIXME: This leaks
Mike Stumpa5448542009-02-13 15:32:32 +0000424 ImplicitParamDecl *SelfDecl =
Anders Carlssond5cab542009-02-12 17:55:02 +0000425 ImplicitParamDecl::Create(getContext(), 0,
426 SourceLocation(), 0,
427 getContext().getPointerType(getContext().VoidTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000428
Anders Carlssond5cab542009-02-12 17:55:02 +0000429 Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType()));
Mike Stump4e7a1f72009-02-21 20:00:35 +0000430 BlockStructDecl = SelfDecl;
Mike Stumpa5448542009-02-13 15:32:32 +0000431
432 for (BlockDecl::param_iterator i = BD->param_begin(),
Anders Carlssond5cab542009-02-12 17:55:02 +0000433 e = BD->param_end(); i != e; ++i)
Mike Stump19050612009-02-17 23:25:52 +0000434 Args.push_back(std::make_pair(*i, (*i)->getType()));
Mike Stumpa5448542009-02-13 15:32:32 +0000435
436 const CGFunctionInfo &FI =
Anders Carlssond5cab542009-02-12 17:55:02 +0000437 CGM.getTypes().getFunctionInfo(FTy->getResultType(), Args);
438
Mike Stump67a64482009-02-14 22:16:35 +0000439 std::string Name = std::string("__") + Info.Name + "_block_invoke_";
Anders Carlssond5cab542009-02-12 17:55:02 +0000440 CodeGenTypes &Types = CGM.getTypes();
441 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, FTy->isVariadic());
Mike Stumpa5448542009-02-13 15:32:32 +0000442
443 llvm::Function *Fn =
Anders Carlssond5cab542009-02-12 17:55:02 +0000444 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
445 Name,
446 &CGM.getModule());
Mike Stumpa5448542009-02-13 15:32:32 +0000447
448 StartFunction(BD, FTy->getResultType(), Fn, Args,
Anders Carlssond5cab542009-02-12 17:55:02 +0000449 Expr->getBody()->getLocEnd());
450 EmitStmt(Expr->getBody());
451 FinishFunction(cast<CompoundStmt>(Expr->getBody())->getRBracLoc());
452
Mike Stump4e7a1f72009-02-21 20:00:35 +0000453 Size = BlockOffset;
454
Anders Carlssond5cab542009-02-12 17:55:02 +0000455 return Fn;
456}