blob: f987af70d327df6ee041d3b0a11c2cce1ff483e1 [file] [log] [blame]
Anders Carlssonacfde802009-02-12 00:39:25 +00001//===--- CGBlocks.cpp - Emit LLVM Code for declarations -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit blocks.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenFunction.h"
15#include "CodeGenModule.h"
16#include "llvm/Module.h"
Anders Carlssond5cab542009-02-12 17:55:02 +000017#include "llvm/Target/TargetData.h"
Anders Carlssonacfde802009-02-12 00:39:25 +000018
19#include <algorithm>
20
21using namespace clang;
22using namespace CodeGen;
23
Anders Carlssond5cab542009-02-12 17:55:02 +000024enum {
Mike Stumpe5fee252009-02-13 16:19:19 +000025 BLOCK_NEEDS_FREE = (1 << 24),
26 BLOCK_HAS_COPY_DISPOSE = (1 << 25),
27 BLOCK_HAS_CXX_OBJ = (1 << 26),
28 BLOCK_IS_GC = (1 << 27),
29 BLOCK_IS_GLOBAL = (1 << 28),
30 BLOCK_HAS_DESCRIPTOR = (1 << 29)
Anders Carlssond5cab542009-02-12 17:55:02 +000031};
32
Mike Stumpe5fee252009-02-13 16:19:19 +000033llvm::Constant *CodeGenFunction::BuildDescriptorBlockDecl() {
34 // FIXME: Push up.
35 bool BlockHasCopyDispose = false;
36
37 const llvm::PointerType *PtrToInt8Ty
38 = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Mike Stump56129b12009-02-13 16:55:51 +000039 const llvm::Type *UnsignedLongTy
40 = CGM.getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpe5fee252009-02-13 16:19:19 +000041 llvm::Constant *C;
42 std::vector<llvm::Constant*> Elts;
43
44 // reserved
Mike Stump56129b12009-02-13 16:55:51 +000045 C = llvm::ConstantInt::get(UnsignedLongTy, 0);
Mike Stumpe5fee252009-02-13 16:19:19 +000046 Elts.push_back(C);
47
48 // Size
49 // FIXME: This should be the size of BlockStructType
Mike Stump56129b12009-02-13 16:55:51 +000050 C = llvm::ConstantInt::get(UnsignedLongTy, 20);
Mike Stumpe5fee252009-02-13 16:19:19 +000051 Elts.push_back(C);
52
53 if (BlockHasCopyDispose) {
54 // copy_func_helper_decl
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 Stump56129b12009-02-13 16:55:51 +000060 C = llvm::ConstantInt::get(UnsignedLongTy, 0);
Mike Stumpe5fee252009-02-13 16:19:19 +000061 C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty);
62 Elts.push_back(C);
63 }
64
65 C = llvm::ConstantStruct::get(Elts);
66
67 // FIXME: Should be in module?
68 static int desc_unique_count;
69 char Name[32];
70 sprintf(Name, "__block_descriptor_tmp_%d", ++desc_unique_count);
71 C = new llvm::GlobalVariable(C->getType(), true,
72 llvm::GlobalValue::InternalLinkage,
73 C, Name, &CGM.getModule());
74 return C;
75}
76
77llvm::Constant *CodeGenFunction::BuildBlockLiteralTmp() {
78 // FIXME: Push up
79 bool BlockHasCopyDispose = false;
80 bool insideFunction = false;
81 bool BlockRefDeclList = false;
82 bool BlockByrefDeclList = false;
83
84 std::vector<llvm::Constant*> Elts;
85 llvm::Constant *C;
86
87 bool staticBlockTmp = (BlockRefDeclList == 0
88 && BlockByrefDeclList == 0);
89
90 {
91 // C = BuildBlockStructInitlist();
92 unsigned int flags = BLOCK_HAS_DESCRIPTOR;
93
94 if (BlockHasCopyDispose)
95 flags |= BLOCK_HAS_COPY_DISPOSE;
96
97 const llvm::PointerType *PtrToInt8Ty
98 = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
99 // FIXME: static? What if we start up a new, unrelated module?
100 // logically we want 1 per module.
101 static llvm::Constant *NSConcreteGlobalBlock_decl
102 = new llvm::GlobalVariable(PtrToInt8Ty, false,
103 llvm::GlobalValue::ExternalLinkage,
104 0, "_NSConcreteGlobalBlock",
105 &CGM.getModule());
106 static llvm::Constant *NSConcreteStackBlock_decl
107 = new llvm::GlobalVariable(PtrToInt8Ty, false,
108 llvm::GlobalValue::ExternalLinkage,
109 0, "_NSConcreteStackBlock",
110 &CGM.getModule());
111 C = NSConcreteStackBlock_decl;
112 if (!insideFunction ||
113 (!BlockRefDeclList && !BlockByrefDeclList)) {
114 C = NSConcreteGlobalBlock_decl;
115 flags |= BLOCK_IS_GLOBAL;
116 }
117 C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty);
118 Elts.push_back(C);
119
120 // __flags
121 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
122 CGM.getTypes().ConvertType(CGM.getContext().IntTy));
123 C = llvm::ConstantInt::get(IntTy, flags);
124 Elts.push_back(C);
125
126 // __reserved
127 C = llvm::ConstantInt::get(IntTy, 0);
128 Elts.push_back(C);
129
130 // __FuncPtr
131 // FIXME: Build this up.
132 Elts.push_back(C);
133
134 // __descriptor
135 Elts.push_back(BuildDescriptorBlockDecl());
136
137 // FIXME: Add block_original_ref_decl_list and block_byref_decl_list.
138 }
139
140 C = llvm::ConstantStruct::get(Elts);
141
142 char Name[32];
143 // FIXME: Boost in CGM?
144 static int global_unique_count;
145 sprintf(Name, "__block_holder_tmp_%d", ++global_unique_count);
146 C = new llvm::GlobalVariable(C->getType(), true,
147 llvm::GlobalValue::InternalLinkage,
148 C, Name, &CGM.getModule());
149 return C;
150}
151
152
153
154
Mike Stumpab695142009-02-13 15:16:56 +0000155const llvm::Type *CodeGenModule::getBlockDescriptorType() {
156 if (BlockDescriptorType)
157 return BlockDescriptorType;
158
Mike Stumpa5448542009-02-13 15:32:32 +0000159 const llvm::Type *UnsignedLongTy =
Mike Stumpab695142009-02-13 15:16:56 +0000160 getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000161
Mike Stumpab695142009-02-13 15:16:56 +0000162 // struct __block_descriptor {
163 // unsigned long reserved;
164 // unsigned long block_size;
165 // };
Mike Stumpa5448542009-02-13 15:32:32 +0000166 BlockDescriptorType = llvm::StructType::get(UnsignedLongTy,
167 UnsignedLongTy,
Mike Stumpab695142009-02-13 15:16:56 +0000168 NULL);
169
170 getModule().addTypeName("struct.__block_descriptor",
171 BlockDescriptorType);
172
173 return BlockDescriptorType;
Anders Carlssonacfde802009-02-12 00:39:25 +0000174}
175
Mike Stump9b8a7972009-02-13 15:25:34 +0000176const llvm::Type *
177CodeGenModule::getGenericBlockLiteralType() {
178 if (GenericBlockLiteralType)
179 return GenericBlockLiteralType;
180
Mike Stumpa5448542009-02-13 15:32:32 +0000181 const llvm::Type *Int8PtrTy =
Mike Stump9b8a7972009-02-13 15:25:34 +0000182 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Mike Stumpa5448542009-02-13 15:32:32 +0000183
184 const llvm::Type *BlockDescPtrTy =
Mike Stump9b8a7972009-02-13 15:25:34 +0000185 llvm::PointerType::getUnqual(getBlockDescriptorType());
Mike Stumpa5448542009-02-13 15:32:32 +0000186
Mike Stump7cbb3602009-02-13 16:01:35 +0000187 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
188 getTypes().ConvertType(getContext().IntTy));
189
Mike Stump9b8a7972009-02-13 15:25:34 +0000190 // struct __block_literal_generic {
191 // void *isa;
192 // int flags;
193 // int reserved;
194 // void (*invoke)(void *);
195 // struct __block_descriptor *descriptor;
196 // };
197 GenericBlockLiteralType = llvm::StructType::get(Int8PtrTy,
Mike Stump7cbb3602009-02-13 16:01:35 +0000198 IntTy,
199 IntTy,
Mike Stump9b8a7972009-02-13 15:25:34 +0000200 Int8PtrTy,
201 BlockDescPtrTy,
202 NULL);
Mike Stumpa5448542009-02-13 15:32:32 +0000203
Mike Stump9b8a7972009-02-13 15:25:34 +0000204 getModule().addTypeName("struct.__block_literal_generic",
205 GenericBlockLiteralType);
Mike Stumpa5448542009-02-13 15:32:32 +0000206
Mike Stump9b8a7972009-02-13 15:25:34 +0000207 return GenericBlockLiteralType;
Anders Carlssonacfde802009-02-12 00:39:25 +0000208}
209
Mike Stumpa5448542009-02-13 15:32:32 +0000210/// getBlockFunctionType - Given a BlockPointerType, will return the
Anders Carlssonacfde802009-02-12 00:39:25 +0000211/// function type for the block, including the first block literal argument.
212static QualType getBlockFunctionType(ASTContext &Ctx,
Anders Carlssond5cab542009-02-12 17:55:02 +0000213 const BlockPointerType *BPT) {
Anders Carlssonacfde802009-02-12 00:39:25 +0000214 const FunctionTypeProto *FTy = cast<FunctionTypeProto>(BPT->getPointeeType());
Mike Stumpa5448542009-02-13 15:32:32 +0000215
Anders Carlssonacfde802009-02-12 00:39:25 +0000216 llvm::SmallVector<QualType, 8> Types;
217 Types.push_back(Ctx.getPointerType(Ctx.VoidTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000218
Anders Carlssonacfde802009-02-12 00:39:25 +0000219 for (FunctionTypeProto::arg_type_iterator i = FTy->arg_type_begin(),
220 e = FTy->arg_type_end(); i != e; ++i)
221 Types.push_back(*i);
Mike Stumpa5448542009-02-13 15:32:32 +0000222
Anders Carlssonacfde802009-02-12 00:39:25 +0000223 return Ctx.getFunctionType(FTy->getResultType(),
Mike Stumpa5448542009-02-13 15:32:32 +0000224 &Types[0], Types.size(),
Anders Carlssonacfde802009-02-12 00:39:25 +0000225 FTy->isVariadic(), 0);
226}
227
Anders Carlssond5cab542009-02-12 17:55:02 +0000228RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E) {
Mike Stumpa5448542009-02-13 15:32:32 +0000229 const BlockPointerType *BPT =
Anders Carlssonacfde802009-02-12 00:39:25 +0000230 E->getCallee()->getType()->getAsBlockPointerType();
Mike Stumpa5448542009-02-13 15:32:32 +0000231
Anders Carlssonacfde802009-02-12 00:39:25 +0000232 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
233
234 // Get a pointer to the generic block literal.
235 const llvm::Type *BlockLiteralTy =
Mike Stump9b8a7972009-02-13 15:25:34 +0000236 llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType());
Anders Carlssonacfde802009-02-12 00:39:25 +0000237
238 // Bitcast the callee to a block literal.
Mike Stumpa5448542009-02-13 15:32:32 +0000239 llvm::Value *BlockLiteral =
Anders Carlssonacfde802009-02-12 00:39:25 +0000240 Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal");
241
242 // Get the function pointer from the literal.
243 llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp");
244 llvm::Value *Func = Builder.CreateLoad(FuncPtr, FuncPtr, "tmp");
245
246 // Cast the function pointer to the right type.
Mike Stumpa5448542009-02-13 15:32:32 +0000247 const llvm::Type *BlockFTy =
Anders Carlssonacfde802009-02-12 00:39:25 +0000248 ConvertType(getBlockFunctionType(getContext(), BPT));
249 const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy);
250 Func = Builder.CreateBitCast(Func, BlockFTyPtr);
251
Mike Stumpa5448542009-02-13 15:32:32 +0000252 BlockLiteral =
253 Builder.CreateBitCast(BlockLiteral,
Anders Carlssonacfde802009-02-12 00:39:25 +0000254 llvm::PointerType::getUnqual(llvm::Type::Int8Ty),
255 "tmp");
Mike Stumpa5448542009-02-13 15:32:32 +0000256
Anders Carlssonacfde802009-02-12 00:39:25 +0000257 // Add the block literal.
258 QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy);
259 CallArgList Args;
260 Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000261
Anders Carlssonacfde802009-02-12 00:39:25 +0000262 // And the rest of the arguments.
Mike Stumpa5448542009-02-13 15:32:32 +0000263 for (CallExpr::const_arg_iterator i = E->arg_begin(), e = E->arg_end();
Anders Carlssonacfde802009-02-12 00:39:25 +0000264 i != e; ++i)
Mike Stumpa5448542009-02-13 15:32:32 +0000265 Args.push_back(std::make_pair(EmitAnyExprToTemp(*i),
Anders Carlssonacfde802009-02-12 00:39:25 +0000266 i->getType()));
Mike Stumpa5448542009-02-13 15:32:32 +0000267
Anders Carlssonacfde802009-02-12 00:39:25 +0000268 // And call the block.
Mike Stumpa5448542009-02-13 15:32:32 +0000269 return EmitCall(CGM.getTypes().getFunctionInfo(E->getType(), Args),
Anders Carlssonacfde802009-02-12 00:39:25 +0000270 Func, Args);
271}
Anders Carlssond5cab542009-02-12 17:55:02 +0000272
273llvm::Constant *CodeGenModule::GetAddrOfGlobalBlock(const BlockExpr *BE) {
274 if (!NSConcreteGlobalBlock) {
275 const llvm::Type *Ty = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
276
Mike Stumpa5448542009-02-13 15:32:32 +0000277 // FIXME: Wee should have a CodeGenModule::AddRuntimeVariable that does the
Anders Carlssond5cab542009-02-12 17:55:02 +0000278 // same thing as CreateRuntimeFunction if there's already a variable with
279 // the same name.
Mike Stumpa5448542009-02-13 15:32:32 +0000280 NSConcreteGlobalBlock =
Anders Carlssond5cab542009-02-12 17:55:02 +0000281 new llvm::GlobalVariable(Ty, false,
Mike Stumpa5448542009-02-13 15:32:32 +0000282 llvm::GlobalVariable::ExternalLinkage, 0,
283 "_NSConcreteGlobalBlock", &getModule());
Anders Carlssond5cab542009-02-12 17:55:02 +0000284 }
285
286 // Generate the block descriptor.
287 const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy);
Mike Stump7cbb3602009-02-13 16:01:35 +0000288 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
289 getTypes().ConvertType(getContext().IntTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000290
Anders Carlssond5cab542009-02-12 17:55:02 +0000291 llvm::Constant *DescriptorFields[2];
Mike Stumpa5448542009-02-13 15:32:32 +0000292
Anders Carlssond5cab542009-02-12 17:55:02 +0000293 // Reserved
294 DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000295
Anders Carlssond5cab542009-02-12 17:55:02 +0000296 // Block literal size. For global blocks we just use the size of the generic
297 // block literal struct.
Mike Stumpa5448542009-02-13 15:32:32 +0000298 uint64_t BlockLiteralSize =
Mike Stump9b8a7972009-02-13 15:25:34 +0000299 TheTargetData.getTypeStoreSizeInBits(getGenericBlockLiteralType()) / 8;
Anders Carlssond5cab542009-02-12 17:55:02 +0000300 DescriptorFields[1] = llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize);
Mike Stumpa5448542009-02-13 15:32:32 +0000301
302 llvm::Constant *DescriptorStruct =
Anders Carlssond5cab542009-02-12 17:55:02 +0000303 llvm::ConstantStruct::get(&DescriptorFields[0], 2);
Mike Stumpa5448542009-02-13 15:32:32 +0000304
Anders Carlssond5cab542009-02-12 17:55:02 +0000305 llvm::GlobalVariable *Descriptor =
306 new llvm::GlobalVariable(DescriptorStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000307 llvm::GlobalVariable::InternalLinkage,
308 DescriptorStruct, "__block_descriptor_global",
Anders Carlssond5cab542009-02-12 17:55:02 +0000309 &getModule());
Mike Stumpa5448542009-02-13 15:32:32 +0000310
Anders Carlssond5cab542009-02-12 17:55:02 +0000311 // Generate the constants for the block literal.
312 llvm::Constant *LiteralFields[5];
Mike Stumpa5448542009-02-13 15:32:32 +0000313
Anders Carlssond5cab542009-02-12 17:55:02 +0000314 CodeGenFunction::BlockInfo Info(0, "global");
315 llvm::Function *Fn = CodeGenFunction(*this).GenerateBlockFunction(BE, Info);
Mike Stumpa5448542009-02-13 15:32:32 +0000316
Anders Carlssond5cab542009-02-12 17:55:02 +0000317 // isa
318 LiteralFields[0] = NSConcreteGlobalBlock;
Mike Stumpa5448542009-02-13 15:32:32 +0000319
Anders Carlssond5cab542009-02-12 17:55:02 +0000320 // Flags
Mike Stumpe5fee252009-02-13 16:19:19 +0000321 LiteralFields[1] = llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL);
Mike Stumpa5448542009-02-13 15:32:32 +0000322
Anders Carlssond5cab542009-02-12 17:55:02 +0000323 // Reserved
Mike Stump7cbb3602009-02-13 16:01:35 +0000324 LiteralFields[2] = llvm::Constant::getNullValue(IntTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000325
Anders Carlssond5cab542009-02-12 17:55:02 +0000326 // Function
327 LiteralFields[3] = Fn;
Mike Stumpa5448542009-02-13 15:32:32 +0000328
Anders Carlssond5cab542009-02-12 17:55:02 +0000329 // Descriptor
330 LiteralFields[4] = Descriptor;
Mike Stumpa5448542009-02-13 15:32:32 +0000331
332 llvm::Constant *BlockLiteralStruct =
Anders Carlssond5cab542009-02-12 17:55:02 +0000333 llvm::ConstantStruct::get(&LiteralFields[0], 5);
Mike Stumpa5448542009-02-13 15:32:32 +0000334
335 llvm::GlobalVariable *BlockLiteral =
Anders Carlssond5cab542009-02-12 17:55:02 +0000336 new llvm::GlobalVariable(BlockLiteralStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000337 llvm::GlobalVariable::InternalLinkage,
338 BlockLiteralStruct, "__block_literal_global",
Anders Carlssond5cab542009-02-12 17:55:02 +0000339 &getModule());
Mike Stumpa5448542009-02-13 15:32:32 +0000340
Anders Carlssond5cab542009-02-12 17:55:02 +0000341 return BlockLiteral;
342}
343
344llvm::Function *CodeGenFunction::GenerateBlockFunction(const BlockExpr *Expr,
345 const BlockInfo& Info)
346{
Mike Stumpa5448542009-02-13 15:32:32 +0000347 const FunctionTypeProto *FTy =
Anders Carlssond5cab542009-02-12 17:55:02 +0000348 cast<FunctionTypeProto>(Expr->getFunctionType());
Mike Stumpa5448542009-02-13 15:32:32 +0000349
Anders Carlssond5cab542009-02-12 17:55:02 +0000350 FunctionArgList Args;
Mike Stumpa5448542009-02-13 15:32:32 +0000351
Anders Carlssond5cab542009-02-12 17:55:02 +0000352 const BlockDecl *BD = Expr->getBlockDecl();
353
354 // FIXME: This leaks
Mike Stumpa5448542009-02-13 15:32:32 +0000355 ImplicitParamDecl *SelfDecl =
Anders Carlssond5cab542009-02-12 17:55:02 +0000356 ImplicitParamDecl::Create(getContext(), 0,
357 SourceLocation(), 0,
358 getContext().getPointerType(getContext().VoidTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000359
Anders Carlssond5cab542009-02-12 17:55:02 +0000360 Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType()));
Mike Stumpa5448542009-02-13 15:32:32 +0000361
362 for (BlockDecl::param_iterator i = BD->param_begin(),
Anders Carlssond5cab542009-02-12 17:55:02 +0000363 e = BD->param_end(); i != e; ++i)
364 Args.push_back(std::make_pair(*e, (*e)->getType()));
Mike Stumpa5448542009-02-13 15:32:32 +0000365
366 const CGFunctionInfo &FI =
Anders Carlssond5cab542009-02-12 17:55:02 +0000367 CGM.getTypes().getFunctionInfo(FTy->getResultType(), Args);
368
369 std::string Name = std::string("__block_function_") + Info.NameSuffix;
370
371 CodeGenTypes &Types = CGM.getTypes();
372 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, FTy->isVariadic());
Mike Stumpa5448542009-02-13 15:32:32 +0000373
374 llvm::Function *Fn =
Anders Carlssond5cab542009-02-12 17:55:02 +0000375 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
376 Name,
377 &CGM.getModule());
Mike Stumpa5448542009-02-13 15:32:32 +0000378
379 StartFunction(BD, FTy->getResultType(), Fn, Args,
Anders Carlssond5cab542009-02-12 17:55:02 +0000380 Expr->getBody()->getLocEnd());
381 EmitStmt(Expr->getBody());
382 FinishFunction(cast<CompoundStmt>(Expr->getBody())->getRBracLoc());
383
384 return Fn;
385}