blob: d4e72acda01fe3bfb57cfaf1945c76616061c2a2 [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 Stump4e7a1f72009-02-21 20:00:35 +000046 C = llvm::ConstantInt::get(UnsignedLongTy, Size);
Mike Stumpe5fee252009-02-13 16:19:19 +000047 Elts.push_back(C);
48
49 if (BlockHasCopyDispose) {
50 // copy_func_helper_decl
Mike Stump4e7a1f72009-02-21 20:00:35 +000051 // FIXME: implement
Mike Stump56129b12009-02-13 16:55:51 +000052 C = llvm::ConstantInt::get(UnsignedLongTy, 0);
Mike Stumpe5fee252009-02-13 16:19:19 +000053 C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty);
54 Elts.push_back(C);
55
56 // destroy_func_decl
Mike Stump4e7a1f72009-02-21 20:00:35 +000057 // FIXME: implement
Mike Stump56129b12009-02-13 16:55:51 +000058 C = llvm::ConstantInt::get(UnsignedLongTy, 0);
Mike Stumpe5fee252009-02-13 16:19:19 +000059 C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty);
60 Elts.push_back(C);
61 }
62
63 C = llvm::ConstantStruct::get(Elts);
64
Mike Stumpe5fee252009-02-13 16:19:19 +000065 C = new llvm::GlobalVariable(C->getType(), true,
66 llvm::GlobalValue::InternalLinkage,
Mike Stump7d6dc4f2009-02-13 20:17:16 +000067 C, "__block_descriptor_tmp", &CGM.getModule());
Mike Stumpe5fee252009-02-13 16:19:19 +000068 return C;
69}
70
Mike Stumpf99f1d02009-02-13 17:23:42 +000071llvm::Constant *CodeGenModule::getNSConcreteGlobalBlock() {
72 if (NSConcreteGlobalBlock)
73 return NSConcreteGlobalBlock;
74
75 const llvm::PointerType *PtrToInt8Ty
76 = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Mike Stumpf7448952009-02-13 19:38:12 +000077 // FIXME: We should have a CodeGenModule::AddRuntimeVariable that does the
Mike Stumpf99f1d02009-02-13 17:23:42 +000078 // same thing as CreateRuntimeFunction if there's already a variable with
79 // the same name.
80 NSConcreteGlobalBlock
81 = new llvm::GlobalVariable(PtrToInt8Ty, false,
82 llvm::GlobalValue::ExternalLinkage,
83 0, "_NSConcreteGlobalBlock",
84 &getModule());
85
86 return NSConcreteGlobalBlock;
87}
88
Mike Stump59c5b112009-02-13 19:29:27 +000089llvm::Constant *CodeGenModule::getNSConcreteStackBlock() {
90 if (NSConcreteStackBlock)
91 return NSConcreteStackBlock;
92
93 const llvm::PointerType *PtrToInt8Ty
94 = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Mike Stumpf7448952009-02-13 19:38:12 +000095 // FIXME: We should have a CodeGenModule::AddRuntimeVariable that does the
Mike Stump59c5b112009-02-13 19:29:27 +000096 // same thing as CreateRuntimeFunction if there's already a variable with
97 // the same name.
98 NSConcreteStackBlock
99 = new llvm::GlobalVariable(PtrToInt8Ty, false,
100 llvm::GlobalValue::ExternalLinkage,
101 0, "_NSConcreteStackBlock",
102 &getModule());
103
104 return NSConcreteStackBlock;
105}
106
Mike Stumpbd65cac2009-02-19 01:01:04 +0000107// FIXME: Push most into CGM, passing down a few bits, like current
108// function name.
Mike Stump67a64482009-02-14 22:16:35 +0000109llvm::Constant *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) {
Mike Stumpe5fee252009-02-13 16:19:19 +0000110 bool insideFunction = false;
111 bool BlockRefDeclList = false;
112 bool BlockByrefDeclList = false;
113
114 std::vector<llvm::Constant*> Elts;
115 llvm::Constant *C;
116
Mike Stumpe5fee252009-02-13 16:19:19 +0000117 {
118 // C = BuildBlockStructInitlist();
119 unsigned int flags = BLOCK_HAS_DESCRIPTOR;
120
121 if (BlockHasCopyDispose)
122 flags |= BLOCK_HAS_COPY_DISPOSE;
123
Mike Stump7d6dc4f2009-02-13 20:17:16 +0000124 // __isa
Mike Stump59c5b112009-02-13 19:29:27 +0000125 C = CGM.getNSConcreteStackBlock();
Mike Stumpe5fee252009-02-13 16:19:19 +0000126 if (!insideFunction ||
127 (!BlockRefDeclList && !BlockByrefDeclList)) {
Mike Stumpf99f1d02009-02-13 17:23:42 +0000128 C = CGM.getNSConcreteGlobalBlock();
Mike Stumpe5fee252009-02-13 16:19:19 +0000129 flags |= BLOCK_IS_GLOBAL;
130 }
Mike Stump59c5b112009-02-13 19:29:27 +0000131 const llvm::PointerType *PtrToInt8Ty
132 = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Mike Stumpe5fee252009-02-13 16:19:19 +0000133 C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty);
134 Elts.push_back(C);
135
136 // __flags
137 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
138 CGM.getTypes().ConvertType(CGM.getContext().IntTy));
139 C = llvm::ConstantInt::get(IntTy, flags);
140 Elts.push_back(C);
141
142 // __reserved
143 C = llvm::ConstantInt::get(IntTy, 0);
144 Elts.push_back(C);
145
Mike Stumpbd65cac2009-02-19 01:01:04 +0000146 // __invoke
Mike Stumpcb717222009-02-17 17:18:36 +0000147 const char *Name = "";
Mike Stump67a64482009-02-14 22:16:35 +0000148 if (const NamedDecl *ND = dyn_cast<NamedDecl>(CurFuncDecl))
Mike Stump19050612009-02-17 23:25:52 +0000149 if (ND->getIdentifier())
150 Name = ND->getNameAsCString();
Mike Stump67a64482009-02-14 22:16:35 +0000151 BlockInfo Info(0, Name);
Mike Stump4e7a1f72009-02-21 20:00:35 +0000152 uint64_t subBlockSize;
153 llvm::Function *Fn
154 = CodeGenFunction(*this).GenerateBlockFunction(BE, Info, subBlockSize);
Mike Stump67a64482009-02-14 22:16:35 +0000155 Elts.push_back(Fn);
Mike Stumpe5fee252009-02-13 16:19:19 +0000156
157 // __descriptor
Mike Stump4e7a1f72009-02-21 20:00:35 +0000158 Elts.push_back(BuildDescriptorBlockDecl(subBlockSize));
Mike Stumpe5fee252009-02-13 16:19:19 +0000159
160 // FIXME: Add block_original_ref_decl_list and block_byref_decl_list.
161 }
162
163 C = llvm::ConstantStruct::get(Elts);
164
165 char Name[32];
Mike Stump26efc332009-02-13 18:36:05 +0000166 sprintf(Name, "__block_holder_tmp_%d", CGM.getGlobalUniqueCount());
Mike Stumpe5fee252009-02-13 16:19:19 +0000167 C = new llvm::GlobalVariable(C->getType(), true,
168 llvm::GlobalValue::InternalLinkage,
169 C, Name, &CGM.getModule());
Mike Stumpbd65cac2009-02-19 01:01:04 +0000170 QualType BPT = BE->getType();
171 C = llvm::ConstantExpr::getBitCast(C, ConvertType(BPT));
Mike Stumpe5fee252009-02-13 16:19:19 +0000172 return C;
173}
174
175
176
177
Mike Stumpab695142009-02-13 15:16:56 +0000178const llvm::Type *CodeGenModule::getBlockDescriptorType() {
179 if (BlockDescriptorType)
180 return BlockDescriptorType;
181
Mike Stumpa5448542009-02-13 15:32:32 +0000182 const llvm::Type *UnsignedLongTy =
Mike Stumpab695142009-02-13 15:16:56 +0000183 getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000184
Mike Stumpab695142009-02-13 15:16:56 +0000185 // struct __block_descriptor {
186 // unsigned long reserved;
187 // unsigned long block_size;
188 // };
Mike Stumpa5448542009-02-13 15:32:32 +0000189 BlockDescriptorType = llvm::StructType::get(UnsignedLongTy,
190 UnsignedLongTy,
Mike Stumpab695142009-02-13 15:16:56 +0000191 NULL);
192
Mike Stump4e7a1f72009-02-21 20:00:35 +0000193 // FIXME: This breaks an unrelated testcase in the testsuite, we
194 // _want_ llvm to not use structural equality, sometimes. What
195 // should we do, modify the testcase and do this anyway, or...
196#if 0
Mike Stumpab695142009-02-13 15:16:56 +0000197 getModule().addTypeName("struct.__block_descriptor",
198 BlockDescriptorType);
Mike Stump4e7a1f72009-02-21 20:00:35 +0000199#endif
Mike Stumpab695142009-02-13 15:16:56 +0000200
201 return BlockDescriptorType;
Anders Carlssonacfde802009-02-12 00:39:25 +0000202}
203
Mike Stump9b8a7972009-02-13 15:25:34 +0000204const llvm::Type *
205CodeGenModule::getGenericBlockLiteralType() {
206 if (GenericBlockLiteralType)
207 return GenericBlockLiteralType;
208
Mike Stumpa5448542009-02-13 15:32:32 +0000209 const llvm::Type *Int8PtrTy =
Mike Stump9b8a7972009-02-13 15:25:34 +0000210 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Mike Stumpa5448542009-02-13 15:32:32 +0000211
212 const llvm::Type *BlockDescPtrTy =
Mike Stump9b8a7972009-02-13 15:25:34 +0000213 llvm::PointerType::getUnqual(getBlockDescriptorType());
Mike Stumpa5448542009-02-13 15:32:32 +0000214
Mike Stump7cbb3602009-02-13 16:01:35 +0000215 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
216 getTypes().ConvertType(getContext().IntTy));
217
Mike Stump9b8a7972009-02-13 15:25:34 +0000218 // struct __block_literal_generic {
Mike Stumpbd65cac2009-02-19 01:01:04 +0000219 // void *__isa;
220 // int __flags;
221 // int __reserved;
222 // void (*__invoke)(void *);
223 // struct __block_descriptor *__descriptor;
Mike Stump9b8a7972009-02-13 15:25:34 +0000224 // };
225 GenericBlockLiteralType = llvm::StructType::get(Int8PtrTy,
Mike Stump7cbb3602009-02-13 16:01:35 +0000226 IntTy,
227 IntTy,
Mike Stump9b8a7972009-02-13 15:25:34 +0000228 Int8PtrTy,
229 BlockDescPtrTy,
230 NULL);
Mike Stumpa5448542009-02-13 15:32:32 +0000231
Mike Stump4e7a1f72009-02-21 20:00:35 +0000232 // FIXME: See struct.__block_descriptor
Mike Stump9b8a7972009-02-13 15:25:34 +0000233 getModule().addTypeName("struct.__block_literal_generic",
234 GenericBlockLiteralType);
Mike Stumpa5448542009-02-13 15:32:32 +0000235
Mike Stump9b8a7972009-02-13 15:25:34 +0000236 return GenericBlockLiteralType;
Anders Carlssonacfde802009-02-12 00:39:25 +0000237}
238
Mike Stumpbd65cac2009-02-19 01:01:04 +0000239const llvm::Type *
240CodeGenModule::getGenericExtendedBlockLiteralType() {
241 if (GenericExtendedBlockLiteralType)
242 return GenericExtendedBlockLiteralType;
243
244 const llvm::Type *Int8PtrTy =
245 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
246
247 const llvm::Type *BlockDescPtrTy =
248 llvm::PointerType::getUnqual(getBlockDescriptorType());
249
250 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
251 getTypes().ConvertType(getContext().IntTy));
252
253 // struct __block_literal_generic {
254 // void *__isa;
255 // int __flags;
256 // int __reserved;
257 // void (*__invoke)(void *);
258 // struct __block_descriptor *__descriptor;
259 // void *__copy_func_helper_decl;
260 // void *__destroy_func_decl;
261 // };
262 GenericExtendedBlockLiteralType = llvm::StructType::get(Int8PtrTy,
263 IntTy,
264 IntTy,
265 Int8PtrTy,
266 BlockDescPtrTy,
267 Int8PtrTy,
268 Int8PtrTy,
269 NULL);
270
Mike Stump4e7a1f72009-02-21 20:00:35 +0000271 // FIXME: See struct.__block_descriptor
Mike Stumpbd65cac2009-02-19 01:01:04 +0000272 getModule().addTypeName("struct.__block_literal_extended_generic",
273 GenericExtendedBlockLiteralType);
274
275 return GenericExtendedBlockLiteralType;
276}
277
Mike Stumpa5448542009-02-13 15:32:32 +0000278/// getBlockFunctionType - Given a BlockPointerType, will return the
Anders Carlssonacfde802009-02-12 00:39:25 +0000279/// function type for the block, including the first block literal argument.
280static QualType getBlockFunctionType(ASTContext &Ctx,
Anders Carlssond5cab542009-02-12 17:55:02 +0000281 const BlockPointerType *BPT) {
Anders Carlssonacfde802009-02-12 00:39:25 +0000282 const FunctionTypeProto *FTy = cast<FunctionTypeProto>(BPT->getPointeeType());
Mike Stumpa5448542009-02-13 15:32:32 +0000283
Anders Carlssonacfde802009-02-12 00:39:25 +0000284 llvm::SmallVector<QualType, 8> Types;
285 Types.push_back(Ctx.getPointerType(Ctx.VoidTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000286
Anders Carlssonacfde802009-02-12 00:39:25 +0000287 for (FunctionTypeProto::arg_type_iterator i = FTy->arg_type_begin(),
288 e = FTy->arg_type_end(); i != e; ++i)
289 Types.push_back(*i);
Mike Stumpa5448542009-02-13 15:32:32 +0000290
Anders Carlssonacfde802009-02-12 00:39:25 +0000291 return Ctx.getFunctionType(FTy->getResultType(),
Mike Stumpa5448542009-02-13 15:32:32 +0000292 &Types[0], Types.size(),
Anders Carlssonacfde802009-02-12 00:39:25 +0000293 FTy->isVariadic(), 0);
294}
295
Anders Carlssond5cab542009-02-12 17:55:02 +0000296RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E) {
Mike Stumpa5448542009-02-13 15:32:32 +0000297 const BlockPointerType *BPT =
Anders Carlssonacfde802009-02-12 00:39:25 +0000298 E->getCallee()->getType()->getAsBlockPointerType();
Mike Stumpa5448542009-02-13 15:32:32 +0000299
Anders Carlssonacfde802009-02-12 00:39:25 +0000300 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
301
302 // Get a pointer to the generic block literal.
303 const llvm::Type *BlockLiteralTy =
Mike Stump9b8a7972009-02-13 15:25:34 +0000304 llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType());
Anders Carlssonacfde802009-02-12 00:39:25 +0000305
306 // Bitcast the callee to a block literal.
Mike Stumpa5448542009-02-13 15:32:32 +0000307 llvm::Value *BlockLiteral =
Anders Carlssonacfde802009-02-12 00:39:25 +0000308 Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal");
309
310 // Get the function pointer from the literal.
311 llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp");
Mike Stump4e7a1f72009-02-21 20:00:35 +0000312 // FIXME: second argument should be false?
Anders Carlssonacfde802009-02-12 00:39:25 +0000313 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);
Mike Stump4e7a1f72009-02-21 20:00:35 +0000373 uint64_t subBlockSize;
374 llvm::Function *Fn
375 = CodeGenFunction(*this).GenerateBlockFunction(BE, Info, subBlockSize);
376 assert(subBlockSize == BlockLiteralSize
377 && "no imports allowed for global block");
Mike Stumpa5448542009-02-13 15:32:32 +0000378
Anders Carlssond5cab542009-02-12 17:55:02 +0000379 // isa
Mike Stumpf99f1d02009-02-13 17:23:42 +0000380 LiteralFields[0] = getNSConcreteGlobalBlock();
Mike Stumpa5448542009-02-13 15:32:32 +0000381
Anders Carlssond5cab542009-02-12 17:55:02 +0000382 // Flags
Mike Stumpe5fee252009-02-13 16:19:19 +0000383 LiteralFields[1] = llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL);
Mike Stumpa5448542009-02-13 15:32:32 +0000384
Anders Carlssond5cab542009-02-12 17:55:02 +0000385 // Reserved
Mike Stump7cbb3602009-02-13 16:01:35 +0000386 LiteralFields[2] = llvm::Constant::getNullValue(IntTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000387
Anders Carlssond5cab542009-02-12 17:55:02 +0000388 // Function
389 LiteralFields[3] = Fn;
Mike Stumpa5448542009-02-13 15:32:32 +0000390
Anders Carlssond5cab542009-02-12 17:55:02 +0000391 // Descriptor
392 LiteralFields[4] = Descriptor;
Mike Stumpa5448542009-02-13 15:32:32 +0000393
394 llvm::Constant *BlockLiteralStruct =
Anders Carlssond5cab542009-02-12 17:55:02 +0000395 llvm::ConstantStruct::get(&LiteralFields[0], 5);
Mike Stumpa5448542009-02-13 15:32:32 +0000396
397 llvm::GlobalVariable *BlockLiteral =
Anders Carlssond5cab542009-02-12 17:55:02 +0000398 new llvm::GlobalVariable(BlockLiteralStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000399 llvm::GlobalVariable::InternalLinkage,
400 BlockLiteralStruct, "__block_literal_global",
Anders Carlssond5cab542009-02-12 17:55:02 +0000401 &getModule());
Mike Stumpa5448542009-02-13 15:32:32 +0000402
Anders Carlssond5cab542009-02-12 17:55:02 +0000403 return BlockLiteral;
404}
405
Mike Stump4e7a1f72009-02-21 20:00:35 +0000406llvm::Value *CodeGenFunction::LoadBlockStruct() {
407 return Builder.CreateLoad(LocalDeclMap[getBlockStructDecl()], "self");
408}
409
Anders Carlssond5cab542009-02-12 17:55:02 +0000410llvm::Function *CodeGenFunction::GenerateBlockFunction(const BlockExpr *Expr,
Mike Stump4e7a1f72009-02-21 20:00:35 +0000411 const BlockInfo& Info,
412 uint64_t &Size) {
Mike Stumpa5448542009-02-13 15:32:32 +0000413 const FunctionTypeProto *FTy =
Anders Carlssond5cab542009-02-12 17:55:02 +0000414 cast<FunctionTypeProto>(Expr->getFunctionType());
Mike Stumpa5448542009-02-13 15:32:32 +0000415
Anders Carlssond5cab542009-02-12 17:55:02 +0000416 FunctionArgList Args;
Mike Stumpa5448542009-02-13 15:32:32 +0000417
Anders Carlssond5cab542009-02-12 17:55:02 +0000418 const BlockDecl *BD = Expr->getBlockDecl();
419
420 // FIXME: This leaks
Mike Stumpa5448542009-02-13 15:32:32 +0000421 ImplicitParamDecl *SelfDecl =
Anders Carlssond5cab542009-02-12 17:55:02 +0000422 ImplicitParamDecl::Create(getContext(), 0,
423 SourceLocation(), 0,
424 getContext().getPointerType(getContext().VoidTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000425
Anders Carlssond5cab542009-02-12 17:55:02 +0000426 Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType()));
Mike Stump4e7a1f72009-02-21 20:00:35 +0000427 BlockStructDecl = SelfDecl;
Mike Stumpa5448542009-02-13 15:32:32 +0000428
429 for (BlockDecl::param_iterator i = BD->param_begin(),
Anders Carlssond5cab542009-02-12 17:55:02 +0000430 e = BD->param_end(); i != e; ++i)
Mike Stump19050612009-02-17 23:25:52 +0000431 Args.push_back(std::make_pair(*i, (*i)->getType()));
Mike Stumpa5448542009-02-13 15:32:32 +0000432
433 const CGFunctionInfo &FI =
Anders Carlssond5cab542009-02-12 17:55:02 +0000434 CGM.getTypes().getFunctionInfo(FTy->getResultType(), Args);
435
Mike Stump67a64482009-02-14 22:16:35 +0000436 std::string Name = std::string("__") + Info.Name + "_block_invoke_";
Anders Carlssond5cab542009-02-12 17:55:02 +0000437 CodeGenTypes &Types = CGM.getTypes();
438 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, FTy->isVariadic());
Mike Stumpa5448542009-02-13 15:32:32 +0000439
440 llvm::Function *Fn =
Anders Carlssond5cab542009-02-12 17:55:02 +0000441 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
442 Name,
443 &CGM.getModule());
Mike Stumpa5448542009-02-13 15:32:32 +0000444
445 StartFunction(BD, FTy->getResultType(), Fn, Args,
Anders Carlssond5cab542009-02-12 17:55:02 +0000446 Expr->getBody()->getLocEnd());
447 EmitStmt(Expr->getBody());
448 FinishFunction(cast<CompoundStmt>(Expr->getBody())->getRBracLoc());
449
Mike Stump4e7a1f72009-02-21 20:00:35 +0000450 Size = BlockOffset;
451
Anders Carlssond5cab542009-02-12 17:55:02 +0000452 return Fn;
453}