blob: 65f6cb11e8256c9924c5cfac4726094934d3c774 [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 Stump8a2b4b12009-02-25 23:33:13 +0000112llvm::Value *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;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000119 llvm::Value *V;
Mike Stumpe5fee252009-02-13 16:19:19 +0000120
Mike Stumpe5fee252009-02-13 16:19:19 +0000121 {
122 // C = BuildBlockStructInitlist();
123 unsigned int flags = BLOCK_HAS_DESCRIPTOR;
124
125 if (BlockHasCopyDispose)
126 flags |= BLOCK_HAS_COPY_DISPOSE;
127
Mike Stump7d6dc4f2009-02-13 20:17:16 +0000128 // __isa
Mike Stump59c5b112009-02-13 19:29:27 +0000129 C = CGM.getNSConcreteStackBlock();
Mike Stumpe5fee252009-02-13 16:19:19 +0000130 if (!insideFunction ||
131 (!BlockRefDeclList && !BlockByrefDeclList)) {
Mike Stumpf99f1d02009-02-13 17:23:42 +0000132 C = CGM.getNSConcreteGlobalBlock();
Mike Stumpe5fee252009-02-13 16:19:19 +0000133 flags |= BLOCK_IS_GLOBAL;
134 }
Mike Stump59c5b112009-02-13 19:29:27 +0000135 const llvm::PointerType *PtrToInt8Ty
136 = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Mike Stumpe5fee252009-02-13 16:19:19 +0000137 C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty);
138 Elts.push_back(C);
139
140 // __flags
141 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
142 CGM.getTypes().ConvertType(CGM.getContext().IntTy));
143 C = llvm::ConstantInt::get(IntTy, flags);
144 Elts.push_back(C);
145
146 // __reserved
147 C = llvm::ConstantInt::get(IntTy, 0);
148 Elts.push_back(C);
149
Mike Stumpbd65cac2009-02-19 01:01:04 +0000150 // __invoke
Mike Stumpcb717222009-02-17 17:18:36 +0000151 const char *Name = "";
Mike Stump67a64482009-02-14 22:16:35 +0000152 if (const NamedDecl *ND = dyn_cast<NamedDecl>(CurFuncDecl))
Mike Stump19050612009-02-17 23:25:52 +0000153 if (ND->getIdentifier())
154 Name = ND->getNameAsCString();
Mike Stump67a64482009-02-14 22:16:35 +0000155 BlockInfo Info(0, Name);
Mike Stump8a2b4b12009-02-25 23:33:13 +0000156 uint64_t subBlockSize, subBlockAlign;
157 llvm::SmallVector<ValueDecl *, 8> subBlockDeclRefDecls;
Mike Stump4e7a1f72009-02-21 20:00:35 +0000158 llvm::Function *Fn
Mike Stump8a2b4b12009-02-25 23:33:13 +0000159 = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, subBlockSize,
160 subBlockAlign, subBlockDeclRefDecls);
Mike Stump67a64482009-02-14 22:16:35 +0000161 Elts.push_back(Fn);
Mike Stumpe5fee252009-02-13 16:19:19 +0000162
163 // __descriptor
Mike Stump4e7a1f72009-02-21 20:00:35 +0000164 Elts.push_back(BuildDescriptorBlockDecl(subBlockSize));
Mike Stumpe5fee252009-02-13 16:19:19 +0000165
Mike Stump8a2b4b12009-02-25 23:33:13 +0000166 // FIXME: Also check to make sure there are no byref variables
167 if (subBlockDeclRefDecls.size() == 0) {
168 C = llvm::ConstantStruct::get(Elts);
169
170 char Name[32];
171 sprintf(Name, "__block_holder_tmp_%d", CGM.getGlobalUniqueCount());
172 C = new llvm::GlobalVariable(C->getType(), true,
173 llvm::GlobalValue::InternalLinkage,
174 C, Name, &CGM.getModule());
175 QualType BPT = BE->getType();
176 C = llvm::ConstantExpr::getBitCast(C, ConvertType(BPT));
177 return C;
178 }
179
180 std::vector<const llvm::Type *> Types(5+subBlockDeclRefDecls.size());
181 for (int i=0; i<5; ++i)
182 Types[i] = Elts[i]->getType();
183
184 for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i)
185 Types[i+5] = ConvertType(subBlockDeclRefDecls[i]->getType());
186
187 llvm::Type *Ty = llvm::StructType::get(Types, true);
188
189 llvm::AllocaInst *A = CreateTempAlloca(Ty);
190 A->setAlignment(subBlockAlign);
191 V = A;
192
193 for (unsigned i=0; i<5; ++i)
194 Builder.CreateStore(Elts[i], Builder.CreateStructGEP(V, i, "block.tmp"));
195
196 for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i)
197 {
198 ValueDecl *VD = subBlockDeclRefDecls[i];
199
200 if (VD->getIdentifier() == 0)
201 continue;
202 SourceLocation Loc = VD->getLocation();
203 DeclRefExpr D(VD, VD->getType(), Loc);
204 llvm::Value* Addr = Builder.CreateStructGEP(V, i+5, "tmp");
205 RValue r = EmitAnyExpr(&D, Addr, false);
206 if (r.isScalar())
207 Builder.CreateStore(r.getScalarVal(), Addr);
208 else if (r.isComplex())
209 // FIXME: implement
210 ErrorUnsupported(BE, "complex in block literal");
211 else if (r.isAggregate())
212 ; // Already created into the destination
213 else
214 assert (0 && "bad block variable");
215 // FIXME: Ensure that the offset created by the backend for
216 // the struct matches the previously computed offset in BlockDecls.
217 }
218
219 // FIXME: Add block_byref_decl_list.
Mike Stumpe5fee252009-02-13 16:19:19 +0000220 }
221
Mike Stumpbd65cac2009-02-19 01:01:04 +0000222 QualType BPT = BE->getType();
Mike Stump8a2b4b12009-02-25 23:33:13 +0000223 return Builder.CreateBitCast(V, ConvertType(BPT));
Mike Stumpe5fee252009-02-13 16:19:19 +0000224}
225
226
Mike Stumpab695142009-02-13 15:16:56 +0000227const llvm::Type *CodeGenModule::getBlockDescriptorType() {
228 if (BlockDescriptorType)
229 return BlockDescriptorType;
230
Mike Stumpa5448542009-02-13 15:32:32 +0000231 const llvm::Type *UnsignedLongTy =
Mike Stumpab695142009-02-13 15:16:56 +0000232 getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000233
Mike Stumpab695142009-02-13 15:16:56 +0000234 // struct __block_descriptor {
235 // unsigned long reserved;
236 // unsigned long block_size;
237 // };
Mike Stumpa5448542009-02-13 15:32:32 +0000238 BlockDescriptorType = llvm::StructType::get(UnsignedLongTy,
239 UnsignedLongTy,
Mike Stumpab695142009-02-13 15:16:56 +0000240 NULL);
241
242 getModule().addTypeName("struct.__block_descriptor",
243 BlockDescriptorType);
244
245 return BlockDescriptorType;
Anders Carlssonacfde802009-02-12 00:39:25 +0000246}
247
Mike Stump9b8a7972009-02-13 15:25:34 +0000248const llvm::Type *
249CodeGenModule::getGenericBlockLiteralType() {
250 if (GenericBlockLiteralType)
251 return GenericBlockLiteralType;
252
Mike Stumpa5448542009-02-13 15:32:32 +0000253 const llvm::Type *Int8PtrTy =
Mike Stump9b8a7972009-02-13 15:25:34 +0000254 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Mike Stumpa5448542009-02-13 15:32:32 +0000255
256 const llvm::Type *BlockDescPtrTy =
Mike Stump9b8a7972009-02-13 15:25:34 +0000257 llvm::PointerType::getUnqual(getBlockDescriptorType());
Mike Stumpa5448542009-02-13 15:32:32 +0000258
Mike Stump7cbb3602009-02-13 16:01:35 +0000259 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
260 getTypes().ConvertType(getContext().IntTy));
261
Mike Stump9b8a7972009-02-13 15:25:34 +0000262 // struct __block_literal_generic {
Mike Stumpbd65cac2009-02-19 01:01:04 +0000263 // void *__isa;
264 // int __flags;
265 // int __reserved;
266 // void (*__invoke)(void *);
267 // struct __block_descriptor *__descriptor;
Mike Stump9b8a7972009-02-13 15:25:34 +0000268 // };
269 GenericBlockLiteralType = llvm::StructType::get(Int8PtrTy,
Mike Stump7cbb3602009-02-13 16:01:35 +0000270 IntTy,
271 IntTy,
Mike Stump9b8a7972009-02-13 15:25:34 +0000272 Int8PtrTy,
273 BlockDescPtrTy,
274 NULL);
Mike Stumpa5448542009-02-13 15:32:32 +0000275
Mike Stump9b8a7972009-02-13 15:25:34 +0000276 getModule().addTypeName("struct.__block_literal_generic",
277 GenericBlockLiteralType);
Mike Stumpa5448542009-02-13 15:32:32 +0000278
Mike Stump9b8a7972009-02-13 15:25:34 +0000279 return GenericBlockLiteralType;
Anders Carlssonacfde802009-02-12 00:39:25 +0000280}
281
Mike Stumpbd65cac2009-02-19 01:01:04 +0000282const llvm::Type *
283CodeGenModule::getGenericExtendedBlockLiteralType() {
284 if (GenericExtendedBlockLiteralType)
285 return GenericExtendedBlockLiteralType;
286
287 const llvm::Type *Int8PtrTy =
288 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
289
290 const llvm::Type *BlockDescPtrTy =
291 llvm::PointerType::getUnqual(getBlockDescriptorType());
292
293 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
294 getTypes().ConvertType(getContext().IntTy));
295
296 // struct __block_literal_generic {
297 // void *__isa;
298 // int __flags;
299 // int __reserved;
300 // void (*__invoke)(void *);
301 // struct __block_descriptor *__descriptor;
302 // void *__copy_func_helper_decl;
303 // void *__destroy_func_decl;
304 // };
305 GenericExtendedBlockLiteralType = llvm::StructType::get(Int8PtrTy,
306 IntTy,
307 IntTy,
308 Int8PtrTy,
309 BlockDescPtrTy,
310 Int8PtrTy,
311 Int8PtrTy,
312 NULL);
313
314 getModule().addTypeName("struct.__block_literal_extended_generic",
315 GenericExtendedBlockLiteralType);
316
317 return GenericExtendedBlockLiteralType;
318}
319
Mike Stumpa5448542009-02-13 15:32:32 +0000320/// getBlockFunctionType - Given a BlockPointerType, will return the
Anders Carlssonacfde802009-02-12 00:39:25 +0000321/// function type for the block, including the first block literal argument.
322static QualType getBlockFunctionType(ASTContext &Ctx,
Anders Carlssond5cab542009-02-12 17:55:02 +0000323 const BlockPointerType *BPT) {
Anders Carlssonacfde802009-02-12 00:39:25 +0000324 const FunctionTypeProto *FTy = cast<FunctionTypeProto>(BPT->getPointeeType());
Mike Stumpa5448542009-02-13 15:32:32 +0000325
Anders Carlssonacfde802009-02-12 00:39:25 +0000326 llvm::SmallVector<QualType, 8> Types;
327 Types.push_back(Ctx.getPointerType(Ctx.VoidTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000328
Anders Carlssonacfde802009-02-12 00:39:25 +0000329 for (FunctionTypeProto::arg_type_iterator i = FTy->arg_type_begin(),
330 e = FTy->arg_type_end(); i != e; ++i)
331 Types.push_back(*i);
Mike Stumpa5448542009-02-13 15:32:32 +0000332
Anders Carlssonacfde802009-02-12 00:39:25 +0000333 return Ctx.getFunctionType(FTy->getResultType(),
Mike Stumpa5448542009-02-13 15:32:32 +0000334 &Types[0], Types.size(),
Anders Carlssonacfde802009-02-12 00:39:25 +0000335 FTy->isVariadic(), 0);
336}
337
Anders Carlssond5cab542009-02-12 17:55:02 +0000338RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E) {
Mike Stumpa5448542009-02-13 15:32:32 +0000339 const BlockPointerType *BPT =
Anders Carlssonacfde802009-02-12 00:39:25 +0000340 E->getCallee()->getType()->getAsBlockPointerType();
Mike Stumpa5448542009-02-13 15:32:32 +0000341
Anders Carlssonacfde802009-02-12 00:39:25 +0000342 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
343
344 // Get a pointer to the generic block literal.
345 const llvm::Type *BlockLiteralTy =
Mike Stump9b8a7972009-02-13 15:25:34 +0000346 llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType());
Anders Carlssonacfde802009-02-12 00:39:25 +0000347
348 // Bitcast the callee to a block literal.
Mike Stumpa5448542009-02-13 15:32:32 +0000349 llvm::Value *BlockLiteral =
Anders Carlssonacfde802009-02-12 00:39:25 +0000350 Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal");
351
352 // Get the function pointer from the literal.
353 llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp");
Mike Stump20733cd2009-02-22 13:27:11 +0000354 llvm::Value *Func = Builder.CreateLoad(FuncPtr, false, "tmp");
Anders Carlssonacfde802009-02-12 00:39:25 +0000355
356 // Cast the function pointer to the right type.
Mike Stumpa5448542009-02-13 15:32:32 +0000357 const llvm::Type *BlockFTy =
Anders Carlssonacfde802009-02-12 00:39:25 +0000358 ConvertType(getBlockFunctionType(getContext(), BPT));
359 const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy);
360 Func = Builder.CreateBitCast(Func, BlockFTyPtr);
361
Mike Stumpa5448542009-02-13 15:32:32 +0000362 BlockLiteral =
363 Builder.CreateBitCast(BlockLiteral,
Anders Carlssonacfde802009-02-12 00:39:25 +0000364 llvm::PointerType::getUnqual(llvm::Type::Int8Ty),
365 "tmp");
Mike Stumpa5448542009-02-13 15:32:32 +0000366
Anders Carlssonacfde802009-02-12 00:39:25 +0000367 // Add the block literal.
368 QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy);
369 CallArgList Args;
370 Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000371
Anders Carlssonacfde802009-02-12 00:39:25 +0000372 // And the rest of the arguments.
Mike Stumpa5448542009-02-13 15:32:32 +0000373 for (CallExpr::const_arg_iterator i = E->arg_begin(), e = E->arg_end();
Anders Carlssonacfde802009-02-12 00:39:25 +0000374 i != e; ++i)
Mike Stumpa5448542009-02-13 15:32:32 +0000375 Args.push_back(std::make_pair(EmitAnyExprToTemp(*i),
Anders Carlssonacfde802009-02-12 00:39:25 +0000376 i->getType()));
Mike Stumpa5448542009-02-13 15:32:32 +0000377
Anders Carlssonacfde802009-02-12 00:39:25 +0000378 // And call the block.
Mike Stumpa5448542009-02-13 15:32:32 +0000379 return EmitCall(CGM.getTypes().getFunctionInfo(E->getType(), Args),
Anders Carlssonacfde802009-02-12 00:39:25 +0000380 Func, Args);
381}
Anders Carlssond5cab542009-02-12 17:55:02 +0000382
Mike Stump67a64482009-02-14 22:16:35 +0000383llvm::Constant *
Mike Stump30395dd2009-02-14 22:49:33 +0000384CodeGenModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) {
Anders Carlssond5cab542009-02-12 17:55:02 +0000385 // Generate the block descriptor.
386 const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy);
Mike Stump7cbb3602009-02-13 16:01:35 +0000387 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
388 getTypes().ConvertType(getContext().IntTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000389
Anders Carlssond5cab542009-02-12 17:55:02 +0000390 llvm::Constant *DescriptorFields[2];
Mike Stumpa5448542009-02-13 15:32:32 +0000391
Anders Carlssond5cab542009-02-12 17:55:02 +0000392 // Reserved
393 DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000394
Anders Carlssond5cab542009-02-12 17:55:02 +0000395 // Block literal size. For global blocks we just use the size of the generic
396 // block literal struct.
Mike Stumpa5448542009-02-13 15:32:32 +0000397 uint64_t BlockLiteralSize =
Mike Stump9b8a7972009-02-13 15:25:34 +0000398 TheTargetData.getTypeStoreSizeInBits(getGenericBlockLiteralType()) / 8;
Anders Carlssond5cab542009-02-12 17:55:02 +0000399 DescriptorFields[1] = llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize);
Mike Stumpa5448542009-02-13 15:32:32 +0000400
401 llvm::Constant *DescriptorStruct =
Anders Carlssond5cab542009-02-12 17:55:02 +0000402 llvm::ConstantStruct::get(&DescriptorFields[0], 2);
Mike Stumpa5448542009-02-13 15:32:32 +0000403
Anders Carlssond5cab542009-02-12 17:55:02 +0000404 llvm::GlobalVariable *Descriptor =
405 new llvm::GlobalVariable(DescriptorStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000406 llvm::GlobalVariable::InternalLinkage,
407 DescriptorStruct, "__block_descriptor_global",
Anders Carlssond5cab542009-02-12 17:55:02 +0000408 &getModule());
Mike Stumpa5448542009-02-13 15:32:32 +0000409
Anders Carlssond5cab542009-02-12 17:55:02 +0000410 // Generate the constants for the block literal.
411 llvm::Constant *LiteralFields[5];
Mike Stumpa5448542009-02-13 15:32:32 +0000412
Mike Stump67a64482009-02-14 22:16:35 +0000413 CodeGenFunction::BlockInfo Info(0, n);
Mike Stump8a2b4b12009-02-25 23:33:13 +0000414 uint64_t subBlockSize, subBlockAlign;
415 llvm::SmallVector<ValueDecl *, 8> subBlockDeclRefDecls;
Mike Stump4e7a1f72009-02-21 20:00:35 +0000416 llvm::Function *Fn
Mike Stump8a2b4b12009-02-25 23:33:13 +0000417 = CodeGenFunction(*this).GenerateBlockFunction(BE, Info, subBlockSize,
418 subBlockAlign,
419 subBlockDeclRefDecls);
Mike Stump4e7a1f72009-02-21 20:00:35 +0000420 assert(subBlockSize == BlockLiteralSize
421 && "no imports allowed for global block");
Mike Stumpa5448542009-02-13 15:32:32 +0000422
Anders Carlssond5cab542009-02-12 17:55:02 +0000423 // isa
Mike Stumpf99f1d02009-02-13 17:23:42 +0000424 LiteralFields[0] = getNSConcreteGlobalBlock();
Mike Stumpa5448542009-02-13 15:32:32 +0000425
Anders Carlssond5cab542009-02-12 17:55:02 +0000426 // Flags
Mike Stumpe5fee252009-02-13 16:19:19 +0000427 LiteralFields[1] = llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL);
Mike Stumpa5448542009-02-13 15:32:32 +0000428
Anders Carlssond5cab542009-02-12 17:55:02 +0000429 // Reserved
Mike Stump7cbb3602009-02-13 16:01:35 +0000430 LiteralFields[2] = llvm::Constant::getNullValue(IntTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000431
Anders Carlssond5cab542009-02-12 17:55:02 +0000432 // Function
433 LiteralFields[3] = Fn;
Mike Stumpa5448542009-02-13 15:32:32 +0000434
Anders Carlssond5cab542009-02-12 17:55:02 +0000435 // Descriptor
436 LiteralFields[4] = Descriptor;
Mike Stumpa5448542009-02-13 15:32:32 +0000437
438 llvm::Constant *BlockLiteralStruct =
Anders Carlssond5cab542009-02-12 17:55:02 +0000439 llvm::ConstantStruct::get(&LiteralFields[0], 5);
Mike Stumpa5448542009-02-13 15:32:32 +0000440
441 llvm::GlobalVariable *BlockLiteral =
Anders Carlssond5cab542009-02-12 17:55:02 +0000442 new llvm::GlobalVariable(BlockLiteralStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000443 llvm::GlobalVariable::InternalLinkage,
444 BlockLiteralStruct, "__block_literal_global",
Anders Carlssond5cab542009-02-12 17:55:02 +0000445 &getModule());
Mike Stumpa5448542009-02-13 15:32:32 +0000446
Anders Carlssond5cab542009-02-12 17:55:02 +0000447 return BlockLiteral;
448}
449
Mike Stump4e7a1f72009-02-21 20:00:35 +0000450llvm::Value *CodeGenFunction::LoadBlockStruct() {
451 return Builder.CreateLoad(LocalDeclMap[getBlockStructDecl()], "self");
452}
453
Anders Carlssond5cab542009-02-12 17:55:02 +0000454llvm::Function *CodeGenFunction::GenerateBlockFunction(const BlockExpr *Expr,
Mike Stump4e7a1f72009-02-21 20:00:35 +0000455 const BlockInfo& Info,
Mike Stump8a2b4b12009-02-25 23:33:13 +0000456 uint64_t &Size,
457 uint64_t &Align,
458 llvm::SmallVector<ValueDecl *, 8> &subBlockDeclRefDecls) {
Mike Stumpa5448542009-02-13 15:32:32 +0000459 const FunctionTypeProto *FTy =
Anders Carlssond5cab542009-02-12 17:55:02 +0000460 cast<FunctionTypeProto>(Expr->getFunctionType());
Mike Stumpa5448542009-02-13 15:32:32 +0000461
Anders Carlssond5cab542009-02-12 17:55:02 +0000462 FunctionArgList Args;
Mike Stumpa5448542009-02-13 15:32:32 +0000463
Anders Carlssond5cab542009-02-12 17:55:02 +0000464 const BlockDecl *BD = Expr->getBlockDecl();
465
466 // FIXME: This leaks
Mike Stumpa5448542009-02-13 15:32:32 +0000467 ImplicitParamDecl *SelfDecl =
Anders Carlssond5cab542009-02-12 17:55:02 +0000468 ImplicitParamDecl::Create(getContext(), 0,
469 SourceLocation(), 0,
470 getContext().getPointerType(getContext().VoidTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000471
Anders Carlssond5cab542009-02-12 17:55:02 +0000472 Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType()));
Mike Stump4e7a1f72009-02-21 20:00:35 +0000473 BlockStructDecl = SelfDecl;
Mike Stumpa5448542009-02-13 15:32:32 +0000474
475 for (BlockDecl::param_iterator i = BD->param_begin(),
Anders Carlssond5cab542009-02-12 17:55:02 +0000476 e = BD->param_end(); i != e; ++i)
Mike Stump19050612009-02-17 23:25:52 +0000477 Args.push_back(std::make_pair(*i, (*i)->getType()));
Mike Stumpa5448542009-02-13 15:32:32 +0000478
479 const CGFunctionInfo &FI =
Anders Carlssond5cab542009-02-12 17:55:02 +0000480 CGM.getTypes().getFunctionInfo(FTy->getResultType(), Args);
481
Mike Stump67a64482009-02-14 22:16:35 +0000482 std::string Name = std::string("__") + Info.Name + "_block_invoke_";
Anders Carlssond5cab542009-02-12 17:55:02 +0000483 CodeGenTypes &Types = CGM.getTypes();
484 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, FTy->isVariadic());
Mike Stumpa5448542009-02-13 15:32:32 +0000485
486 llvm::Function *Fn =
Anders Carlssond5cab542009-02-12 17:55:02 +0000487 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
488 Name,
489 &CGM.getModule());
Mike Stumpa5448542009-02-13 15:32:32 +0000490
491 StartFunction(BD, FTy->getResultType(), Fn, Args,
Anders Carlssond5cab542009-02-12 17:55:02 +0000492 Expr->getBody()->getLocEnd());
493 EmitStmt(Expr->getBody());
494 FinishFunction(cast<CompoundStmt>(Expr->getBody())->getRBracLoc());
495
Mike Stump8a2b4b12009-02-25 23:33:13 +0000496 // The runtime needs a minimum alignment of a void *.
497 uint64_t MinAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8;
498 BlockOffset = llvm::RoundUpToAlignment(BlockOffset, MinAlign);
499
Mike Stump4e7a1f72009-02-21 20:00:35 +0000500 Size = BlockOffset;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000501 Align = BlockAlign;
502 subBlockDeclRefDecls = BlockDeclRefDecls;
Mike Stump4e7a1f72009-02-21 20:00:35 +0000503
Anders Carlssond5cab542009-02-12 17:55:02 +0000504 return Fn;
505}