blob: d368d20bcecb22dda66660be40917a6853b9522f [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
196 getModule().addTypeName("struct.__block_descriptor",
197 BlockDescriptorType);
198
199 return BlockDescriptorType;
Anders Carlssonacfde802009-02-12 00:39:25 +0000200}
201
Mike Stump9b8a7972009-02-13 15:25:34 +0000202const llvm::Type *
203CodeGenModule::getGenericBlockLiteralType() {
204 if (GenericBlockLiteralType)
205 return GenericBlockLiteralType;
206
Mike Stumpa5448542009-02-13 15:32:32 +0000207 const llvm::Type *Int8PtrTy =
Mike Stump9b8a7972009-02-13 15:25:34 +0000208 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Mike Stumpa5448542009-02-13 15:32:32 +0000209
210 const llvm::Type *BlockDescPtrTy =
Mike Stump9b8a7972009-02-13 15:25:34 +0000211 llvm::PointerType::getUnqual(getBlockDescriptorType());
Mike Stumpa5448542009-02-13 15:32:32 +0000212
Mike Stump7cbb3602009-02-13 16:01:35 +0000213 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
214 getTypes().ConvertType(getContext().IntTy));
215
Mike Stump9b8a7972009-02-13 15:25:34 +0000216 // struct __block_literal_generic {
Mike Stumpbd65cac2009-02-19 01:01:04 +0000217 // void *__isa;
218 // int __flags;
219 // int __reserved;
220 // void (*__invoke)(void *);
221 // struct __block_descriptor *__descriptor;
Mike Stump9b8a7972009-02-13 15:25:34 +0000222 // };
223 GenericBlockLiteralType = llvm::StructType::get(Int8PtrTy,
Mike Stump7cbb3602009-02-13 16:01:35 +0000224 IntTy,
225 IntTy,
Mike Stump9b8a7972009-02-13 15:25:34 +0000226 Int8PtrTy,
227 BlockDescPtrTy,
228 NULL);
Mike Stumpa5448542009-02-13 15:32:32 +0000229
Mike Stump9b8a7972009-02-13 15:25:34 +0000230 getModule().addTypeName("struct.__block_literal_generic",
231 GenericBlockLiteralType);
Mike Stumpa5448542009-02-13 15:32:32 +0000232
Mike Stump9b8a7972009-02-13 15:25:34 +0000233 return GenericBlockLiteralType;
Anders Carlssonacfde802009-02-12 00:39:25 +0000234}
235
Mike Stumpbd65cac2009-02-19 01:01:04 +0000236const llvm::Type *
237CodeGenModule::getGenericExtendedBlockLiteralType() {
238 if (GenericExtendedBlockLiteralType)
239 return GenericExtendedBlockLiteralType;
240
241 const llvm::Type *Int8PtrTy =
242 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
243
244 const llvm::Type *BlockDescPtrTy =
245 llvm::PointerType::getUnqual(getBlockDescriptorType());
246
247 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
248 getTypes().ConvertType(getContext().IntTy));
249
250 // struct __block_literal_generic {
251 // void *__isa;
252 // int __flags;
253 // int __reserved;
254 // void (*__invoke)(void *);
255 // struct __block_descriptor *__descriptor;
256 // void *__copy_func_helper_decl;
257 // void *__destroy_func_decl;
258 // };
259 GenericExtendedBlockLiteralType = llvm::StructType::get(Int8PtrTy,
260 IntTy,
261 IntTy,
262 Int8PtrTy,
263 BlockDescPtrTy,
264 Int8PtrTy,
265 Int8PtrTy,
266 NULL);
267
268 getModule().addTypeName("struct.__block_literal_extended_generic",
269 GenericExtendedBlockLiteralType);
270
271 return GenericExtendedBlockLiteralType;
272}
273
Mike Stumpa5448542009-02-13 15:32:32 +0000274/// getBlockFunctionType - Given a BlockPointerType, will return the
Anders Carlssonacfde802009-02-12 00:39:25 +0000275/// function type for the block, including the first block literal argument.
276static QualType getBlockFunctionType(ASTContext &Ctx,
Anders Carlssond5cab542009-02-12 17:55:02 +0000277 const BlockPointerType *BPT) {
Anders Carlssonacfde802009-02-12 00:39:25 +0000278 const FunctionTypeProto *FTy = cast<FunctionTypeProto>(BPT->getPointeeType());
Mike Stumpa5448542009-02-13 15:32:32 +0000279
Anders Carlssonacfde802009-02-12 00:39:25 +0000280 llvm::SmallVector<QualType, 8> Types;
281 Types.push_back(Ctx.getPointerType(Ctx.VoidTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000282
Anders Carlssonacfde802009-02-12 00:39:25 +0000283 for (FunctionTypeProto::arg_type_iterator i = FTy->arg_type_begin(),
284 e = FTy->arg_type_end(); i != e; ++i)
285 Types.push_back(*i);
Mike Stumpa5448542009-02-13 15:32:32 +0000286
Anders Carlssonacfde802009-02-12 00:39:25 +0000287 return Ctx.getFunctionType(FTy->getResultType(),
Mike Stumpa5448542009-02-13 15:32:32 +0000288 &Types[0], Types.size(),
Anders Carlssonacfde802009-02-12 00:39:25 +0000289 FTy->isVariadic(), 0);
290}
291
Anders Carlssond5cab542009-02-12 17:55:02 +0000292RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E) {
Mike Stumpa5448542009-02-13 15:32:32 +0000293 const BlockPointerType *BPT =
Anders Carlssonacfde802009-02-12 00:39:25 +0000294 E->getCallee()->getType()->getAsBlockPointerType();
Mike Stumpa5448542009-02-13 15:32:32 +0000295
Anders Carlssonacfde802009-02-12 00:39:25 +0000296 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
297
298 // Get a pointer to the generic block literal.
299 const llvm::Type *BlockLiteralTy =
Mike Stump9b8a7972009-02-13 15:25:34 +0000300 llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType());
Anders Carlssonacfde802009-02-12 00:39:25 +0000301
302 // Bitcast the callee to a block literal.
Mike Stumpa5448542009-02-13 15:32:32 +0000303 llvm::Value *BlockLiteral =
Anders Carlssonacfde802009-02-12 00:39:25 +0000304 Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal");
305
306 // Get the function pointer from the literal.
307 llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp");
Mike Stump20733cd2009-02-22 13:27:11 +0000308 llvm::Value *Func = Builder.CreateLoad(FuncPtr, false, "tmp");
Anders Carlssonacfde802009-02-12 00:39:25 +0000309
310 // Cast the function pointer to the right type.
Mike Stumpa5448542009-02-13 15:32:32 +0000311 const llvm::Type *BlockFTy =
Anders Carlssonacfde802009-02-12 00:39:25 +0000312 ConvertType(getBlockFunctionType(getContext(), BPT));
313 const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy);
314 Func = Builder.CreateBitCast(Func, BlockFTyPtr);
315
Mike Stumpa5448542009-02-13 15:32:32 +0000316 BlockLiteral =
317 Builder.CreateBitCast(BlockLiteral,
Anders Carlssonacfde802009-02-12 00:39:25 +0000318 llvm::PointerType::getUnqual(llvm::Type::Int8Ty),
319 "tmp");
Mike Stumpa5448542009-02-13 15:32:32 +0000320
Anders Carlssonacfde802009-02-12 00:39:25 +0000321 // Add the block literal.
322 QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy);
323 CallArgList Args;
324 Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000325
Anders Carlssonacfde802009-02-12 00:39:25 +0000326 // And the rest of the arguments.
Mike Stumpa5448542009-02-13 15:32:32 +0000327 for (CallExpr::const_arg_iterator i = E->arg_begin(), e = E->arg_end();
Anders Carlssonacfde802009-02-12 00:39:25 +0000328 i != e; ++i)
Mike Stumpa5448542009-02-13 15:32:32 +0000329 Args.push_back(std::make_pair(EmitAnyExprToTemp(*i),
Anders Carlssonacfde802009-02-12 00:39:25 +0000330 i->getType()));
Mike Stumpa5448542009-02-13 15:32:32 +0000331
Anders Carlssonacfde802009-02-12 00:39:25 +0000332 // And call the block.
Mike Stumpa5448542009-02-13 15:32:32 +0000333 return EmitCall(CGM.getTypes().getFunctionInfo(E->getType(), Args),
Anders Carlssonacfde802009-02-12 00:39:25 +0000334 Func, Args);
335}
Anders Carlssond5cab542009-02-12 17:55:02 +0000336
Mike Stump67a64482009-02-14 22:16:35 +0000337llvm::Constant *
Mike Stump30395dd2009-02-14 22:49:33 +0000338CodeGenModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) {
Anders Carlssond5cab542009-02-12 17:55:02 +0000339 // Generate the block descriptor.
340 const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy);
Mike Stump7cbb3602009-02-13 16:01:35 +0000341 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
342 getTypes().ConvertType(getContext().IntTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000343
Anders Carlssond5cab542009-02-12 17:55:02 +0000344 llvm::Constant *DescriptorFields[2];
Mike Stumpa5448542009-02-13 15:32:32 +0000345
Anders Carlssond5cab542009-02-12 17:55:02 +0000346 // Reserved
347 DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000348
Anders Carlssond5cab542009-02-12 17:55:02 +0000349 // Block literal size. For global blocks we just use the size of the generic
350 // block literal struct.
Mike Stumpa5448542009-02-13 15:32:32 +0000351 uint64_t BlockLiteralSize =
Mike Stump9b8a7972009-02-13 15:25:34 +0000352 TheTargetData.getTypeStoreSizeInBits(getGenericBlockLiteralType()) / 8;
Anders Carlssond5cab542009-02-12 17:55:02 +0000353 DescriptorFields[1] = llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize);
Mike Stumpa5448542009-02-13 15:32:32 +0000354
355 llvm::Constant *DescriptorStruct =
Anders Carlssond5cab542009-02-12 17:55:02 +0000356 llvm::ConstantStruct::get(&DescriptorFields[0], 2);
Mike Stumpa5448542009-02-13 15:32:32 +0000357
Anders Carlssond5cab542009-02-12 17:55:02 +0000358 llvm::GlobalVariable *Descriptor =
359 new llvm::GlobalVariable(DescriptorStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000360 llvm::GlobalVariable::InternalLinkage,
361 DescriptorStruct, "__block_descriptor_global",
Anders Carlssond5cab542009-02-12 17:55:02 +0000362 &getModule());
Mike Stumpa5448542009-02-13 15:32:32 +0000363
Anders Carlssond5cab542009-02-12 17:55:02 +0000364 // Generate the constants for the block literal.
365 llvm::Constant *LiteralFields[5];
Mike Stumpa5448542009-02-13 15:32:32 +0000366
Mike Stump67a64482009-02-14 22:16:35 +0000367 CodeGenFunction::BlockInfo Info(0, n);
Mike Stump4e7a1f72009-02-21 20:00:35 +0000368 uint64_t subBlockSize;
369 llvm::Function *Fn
370 = CodeGenFunction(*this).GenerateBlockFunction(BE, Info, subBlockSize);
371 assert(subBlockSize == BlockLiteralSize
372 && "no imports allowed for global block");
Mike Stumpa5448542009-02-13 15:32:32 +0000373
Anders Carlssond5cab542009-02-12 17:55:02 +0000374 // isa
Mike Stumpf99f1d02009-02-13 17:23:42 +0000375 LiteralFields[0] = getNSConcreteGlobalBlock();
Mike Stumpa5448542009-02-13 15:32:32 +0000376
Anders Carlssond5cab542009-02-12 17:55:02 +0000377 // Flags
Mike Stumpe5fee252009-02-13 16:19:19 +0000378 LiteralFields[1] = llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL);
Mike Stumpa5448542009-02-13 15:32:32 +0000379
Anders Carlssond5cab542009-02-12 17:55:02 +0000380 // Reserved
Mike Stump7cbb3602009-02-13 16:01:35 +0000381 LiteralFields[2] = llvm::Constant::getNullValue(IntTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000382
Anders Carlssond5cab542009-02-12 17:55:02 +0000383 // Function
384 LiteralFields[3] = Fn;
Mike Stumpa5448542009-02-13 15:32:32 +0000385
Anders Carlssond5cab542009-02-12 17:55:02 +0000386 // Descriptor
387 LiteralFields[4] = Descriptor;
Mike Stumpa5448542009-02-13 15:32:32 +0000388
389 llvm::Constant *BlockLiteralStruct =
Anders Carlssond5cab542009-02-12 17:55:02 +0000390 llvm::ConstantStruct::get(&LiteralFields[0], 5);
Mike Stumpa5448542009-02-13 15:32:32 +0000391
392 llvm::GlobalVariable *BlockLiteral =
Anders Carlssond5cab542009-02-12 17:55:02 +0000393 new llvm::GlobalVariable(BlockLiteralStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000394 llvm::GlobalVariable::InternalLinkage,
395 BlockLiteralStruct, "__block_literal_global",
Anders Carlssond5cab542009-02-12 17:55:02 +0000396 &getModule());
Mike Stumpa5448542009-02-13 15:32:32 +0000397
Anders Carlssond5cab542009-02-12 17:55:02 +0000398 return BlockLiteral;
399}
400
Mike Stump4e7a1f72009-02-21 20:00:35 +0000401llvm::Value *CodeGenFunction::LoadBlockStruct() {
402 return Builder.CreateLoad(LocalDeclMap[getBlockStructDecl()], "self");
403}
404
Anders Carlssond5cab542009-02-12 17:55:02 +0000405llvm::Function *CodeGenFunction::GenerateBlockFunction(const BlockExpr *Expr,
Mike Stump4e7a1f72009-02-21 20:00:35 +0000406 const BlockInfo& Info,
407 uint64_t &Size) {
Mike Stumpa5448542009-02-13 15:32:32 +0000408 const FunctionTypeProto *FTy =
Anders Carlssond5cab542009-02-12 17:55:02 +0000409 cast<FunctionTypeProto>(Expr->getFunctionType());
Mike Stumpa5448542009-02-13 15:32:32 +0000410
Anders Carlssond5cab542009-02-12 17:55:02 +0000411 FunctionArgList Args;
Mike Stumpa5448542009-02-13 15:32:32 +0000412
Anders Carlssond5cab542009-02-12 17:55:02 +0000413 const BlockDecl *BD = Expr->getBlockDecl();
414
415 // FIXME: This leaks
Mike Stumpa5448542009-02-13 15:32:32 +0000416 ImplicitParamDecl *SelfDecl =
Anders Carlssond5cab542009-02-12 17:55:02 +0000417 ImplicitParamDecl::Create(getContext(), 0,
418 SourceLocation(), 0,
419 getContext().getPointerType(getContext().VoidTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000420
Anders Carlssond5cab542009-02-12 17:55:02 +0000421 Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType()));
Mike Stump4e7a1f72009-02-21 20:00:35 +0000422 BlockStructDecl = SelfDecl;
Mike Stumpa5448542009-02-13 15:32:32 +0000423
424 for (BlockDecl::param_iterator i = BD->param_begin(),
Anders Carlssond5cab542009-02-12 17:55:02 +0000425 e = BD->param_end(); i != e; ++i)
Mike Stump19050612009-02-17 23:25:52 +0000426 Args.push_back(std::make_pair(*i, (*i)->getType()));
Mike Stumpa5448542009-02-13 15:32:32 +0000427
428 const CGFunctionInfo &FI =
Anders Carlssond5cab542009-02-12 17:55:02 +0000429 CGM.getTypes().getFunctionInfo(FTy->getResultType(), Args);
430
Mike Stump67a64482009-02-14 22:16:35 +0000431 std::string Name = std::string("__") + Info.Name + "_block_invoke_";
Anders Carlssond5cab542009-02-12 17:55:02 +0000432 CodeGenTypes &Types = CGM.getTypes();
433 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, FTy->isVariadic());
Mike Stumpa5448542009-02-13 15:32:32 +0000434
435 llvm::Function *Fn =
Anders Carlssond5cab542009-02-12 17:55:02 +0000436 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
437 Name,
438 &CGM.getModule());
Mike Stumpa5448542009-02-13 15:32:32 +0000439
440 StartFunction(BD, FTy->getResultType(), Fn, Args,
Anders Carlssond5cab542009-02-12 17:55:02 +0000441 Expr->getBody()->getLocEnd());
442 EmitStmt(Expr->getBody());
443 FinishFunction(cast<CompoundStmt>(Expr->getBody())->getRBracLoc());
444
Mike Stump4e7a1f72009-02-21 20:00:35 +0000445 Size = BlockOffset;
446
Anders Carlssond5cab542009-02-12 17:55:02 +0000447 return Fn;
448}