blob: 25c5ab6b712c014303c0a95ae943dd8428836b58 [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);
39 llvm::Constant *C;
40 std::vector<llvm::Constant*> Elts;
41
42 // reserved
43 const llvm::IntegerType *LongTy
44 = cast<llvm::IntegerType>(
45 CGM.getTypes().ConvertType(CGM.getContext().LongTy));
46 C = llvm::ConstantInt::get(LongTy, 0);
47 Elts.push_back(C);
48
49 // Size
50 // FIXME: This should be the size of BlockStructType
51 C = llvm::ConstantInt::get(LongTy, 20);
52 Elts.push_back(C);
53
54 if (BlockHasCopyDispose) {
55 // copy_func_helper_decl
56 C = llvm::ConstantInt::get(LongTy, 0);
57 C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty);
58 Elts.push_back(C);
59
60 // destroy_func_decl
61 C = llvm::ConstantInt::get(LongTy, 0);
62 C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty);
63 Elts.push_back(C);
64 }
65
66 C = llvm::ConstantStruct::get(Elts);
67
68 // FIXME: Should be in module?
69 static int desc_unique_count;
70 char Name[32];
71 sprintf(Name, "__block_descriptor_tmp_%d", ++desc_unique_count);
72 C = new llvm::GlobalVariable(C->getType(), true,
73 llvm::GlobalValue::InternalLinkage,
74 C, Name, &CGM.getModule());
75 return C;
76}
77
78llvm::Constant *CodeGenFunction::BuildBlockLiteralTmp() {
79 // FIXME: Push up
80 bool BlockHasCopyDispose = false;
81 bool insideFunction = false;
82 bool BlockRefDeclList = false;
83 bool BlockByrefDeclList = false;
84
85 std::vector<llvm::Constant*> Elts;
86 llvm::Constant *C;
87
88 bool staticBlockTmp = (BlockRefDeclList == 0
89 && BlockByrefDeclList == 0);
90
91 {
92 // C = BuildBlockStructInitlist();
93 unsigned int flags = BLOCK_HAS_DESCRIPTOR;
94
95 if (BlockHasCopyDispose)
96 flags |= BLOCK_HAS_COPY_DISPOSE;
97
98 const llvm::PointerType *PtrToInt8Ty
99 = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
100 // FIXME: static? What if we start up a new, unrelated module?
101 // logically we want 1 per module.
102 static llvm::Constant *NSConcreteGlobalBlock_decl
103 = new llvm::GlobalVariable(PtrToInt8Ty, false,
104 llvm::GlobalValue::ExternalLinkage,
105 0, "_NSConcreteGlobalBlock",
106 &CGM.getModule());
107 static llvm::Constant *NSConcreteStackBlock_decl
108 = new llvm::GlobalVariable(PtrToInt8Ty, false,
109 llvm::GlobalValue::ExternalLinkage,
110 0, "_NSConcreteStackBlock",
111 &CGM.getModule());
112 C = NSConcreteStackBlock_decl;
113 if (!insideFunction ||
114 (!BlockRefDeclList && !BlockByrefDeclList)) {
115 C = NSConcreteGlobalBlock_decl;
116 flags |= BLOCK_IS_GLOBAL;
117 }
118 C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty);
119 Elts.push_back(C);
120
121 // __flags
122 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
123 CGM.getTypes().ConvertType(CGM.getContext().IntTy));
124 C = llvm::ConstantInt::get(IntTy, flags);
125 Elts.push_back(C);
126
127 // __reserved
128 C = llvm::ConstantInt::get(IntTy, 0);
129 Elts.push_back(C);
130
131 // __FuncPtr
132 // FIXME: Build this up.
133 Elts.push_back(C);
134
135 // __descriptor
136 Elts.push_back(BuildDescriptorBlockDecl());
137
138 // FIXME: Add block_original_ref_decl_list and block_byref_decl_list.
139 }
140
141 C = llvm::ConstantStruct::get(Elts);
142
143 char Name[32];
144 // FIXME: Boost in CGM?
145 static int global_unique_count;
146 sprintf(Name, "__block_holder_tmp_%d", ++global_unique_count);
147 C = new llvm::GlobalVariable(C->getType(), true,
148 llvm::GlobalValue::InternalLinkage,
149 C, Name, &CGM.getModule());
150 return C;
151}
152
153
154
155
Mike Stumpab695142009-02-13 15:16:56 +0000156const llvm::Type *CodeGenModule::getBlockDescriptorType() {
157 if (BlockDescriptorType)
158 return BlockDescriptorType;
159
Mike Stumpa5448542009-02-13 15:32:32 +0000160 const llvm::Type *UnsignedLongTy =
Mike Stumpab695142009-02-13 15:16:56 +0000161 getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000162
Mike Stumpab695142009-02-13 15:16:56 +0000163 // struct __block_descriptor {
164 // unsigned long reserved;
165 // unsigned long block_size;
166 // };
Mike Stumpa5448542009-02-13 15:32:32 +0000167 BlockDescriptorType = llvm::StructType::get(UnsignedLongTy,
168 UnsignedLongTy,
Mike Stumpab695142009-02-13 15:16:56 +0000169 NULL);
170
171 getModule().addTypeName("struct.__block_descriptor",
172 BlockDescriptorType);
173
174 return BlockDescriptorType;
Anders Carlssonacfde802009-02-12 00:39:25 +0000175}
176
Mike Stump9b8a7972009-02-13 15:25:34 +0000177const llvm::Type *
178CodeGenModule::getGenericBlockLiteralType() {
179 if (GenericBlockLiteralType)
180 return GenericBlockLiteralType;
181
Mike Stumpa5448542009-02-13 15:32:32 +0000182 const llvm::Type *Int8PtrTy =
Mike Stump9b8a7972009-02-13 15:25:34 +0000183 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Mike Stumpa5448542009-02-13 15:32:32 +0000184
185 const llvm::Type *BlockDescPtrTy =
Mike Stump9b8a7972009-02-13 15:25:34 +0000186 llvm::PointerType::getUnqual(getBlockDescriptorType());
Mike Stumpa5448542009-02-13 15:32:32 +0000187
Mike Stump7cbb3602009-02-13 16:01:35 +0000188 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
189 getTypes().ConvertType(getContext().IntTy));
190
Mike Stump9b8a7972009-02-13 15:25:34 +0000191 // struct __block_literal_generic {
192 // void *isa;
193 // int flags;
194 // int reserved;
195 // void (*invoke)(void *);
196 // struct __block_descriptor *descriptor;
197 // };
198 GenericBlockLiteralType = llvm::StructType::get(Int8PtrTy,
Mike Stump7cbb3602009-02-13 16:01:35 +0000199 IntTy,
200 IntTy,
Mike Stump9b8a7972009-02-13 15:25:34 +0000201 Int8PtrTy,
202 BlockDescPtrTy,
203 NULL);
Mike Stumpa5448542009-02-13 15:32:32 +0000204
Mike Stump9b8a7972009-02-13 15:25:34 +0000205 getModule().addTypeName("struct.__block_literal_generic",
206 GenericBlockLiteralType);
Mike Stumpa5448542009-02-13 15:32:32 +0000207
Mike Stump9b8a7972009-02-13 15:25:34 +0000208 return GenericBlockLiteralType;
Anders Carlssonacfde802009-02-12 00:39:25 +0000209}
210
Mike Stumpa5448542009-02-13 15:32:32 +0000211/// getBlockFunctionType - Given a BlockPointerType, will return the
Anders Carlssonacfde802009-02-12 00:39:25 +0000212/// function type for the block, including the first block literal argument.
213static QualType getBlockFunctionType(ASTContext &Ctx,
Anders Carlssond5cab542009-02-12 17:55:02 +0000214 const BlockPointerType *BPT) {
Anders Carlssonacfde802009-02-12 00:39:25 +0000215 const FunctionTypeProto *FTy = cast<FunctionTypeProto>(BPT->getPointeeType());
Mike Stumpa5448542009-02-13 15:32:32 +0000216
Anders Carlssonacfde802009-02-12 00:39:25 +0000217 llvm::SmallVector<QualType, 8> Types;
218 Types.push_back(Ctx.getPointerType(Ctx.VoidTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000219
Anders Carlssonacfde802009-02-12 00:39:25 +0000220 for (FunctionTypeProto::arg_type_iterator i = FTy->arg_type_begin(),
221 e = FTy->arg_type_end(); i != e; ++i)
222 Types.push_back(*i);
Mike Stumpa5448542009-02-13 15:32:32 +0000223
Anders Carlssonacfde802009-02-12 00:39:25 +0000224 return Ctx.getFunctionType(FTy->getResultType(),
Mike Stumpa5448542009-02-13 15:32:32 +0000225 &Types[0], Types.size(),
Anders Carlssonacfde802009-02-12 00:39:25 +0000226 FTy->isVariadic(), 0);
227}
228
Anders Carlssond5cab542009-02-12 17:55:02 +0000229RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E) {
Mike Stumpa5448542009-02-13 15:32:32 +0000230 const BlockPointerType *BPT =
Anders Carlssonacfde802009-02-12 00:39:25 +0000231 E->getCallee()->getType()->getAsBlockPointerType();
Mike Stumpa5448542009-02-13 15:32:32 +0000232
Anders Carlssonacfde802009-02-12 00:39:25 +0000233 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
234
235 // Get a pointer to the generic block literal.
236 const llvm::Type *BlockLiteralTy =
Mike Stump9b8a7972009-02-13 15:25:34 +0000237 llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType());
Anders Carlssonacfde802009-02-12 00:39:25 +0000238
239 // Bitcast the callee to a block literal.
Mike Stumpa5448542009-02-13 15:32:32 +0000240 llvm::Value *BlockLiteral =
Anders Carlssonacfde802009-02-12 00:39:25 +0000241 Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal");
242
243 // Get the function pointer from the literal.
244 llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp");
245 llvm::Value *Func = Builder.CreateLoad(FuncPtr, FuncPtr, "tmp");
246
247 // Cast the function pointer to the right type.
Mike Stumpa5448542009-02-13 15:32:32 +0000248 const llvm::Type *BlockFTy =
Anders Carlssonacfde802009-02-12 00:39:25 +0000249 ConvertType(getBlockFunctionType(getContext(), BPT));
250 const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy);
251 Func = Builder.CreateBitCast(Func, BlockFTyPtr);
252
Mike Stumpa5448542009-02-13 15:32:32 +0000253 BlockLiteral =
254 Builder.CreateBitCast(BlockLiteral,
Anders Carlssonacfde802009-02-12 00:39:25 +0000255 llvm::PointerType::getUnqual(llvm::Type::Int8Ty),
256 "tmp");
Mike Stumpa5448542009-02-13 15:32:32 +0000257
Anders Carlssonacfde802009-02-12 00:39:25 +0000258 // Add the block literal.
259 QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy);
260 CallArgList Args;
261 Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000262
Anders Carlssonacfde802009-02-12 00:39:25 +0000263 // And the rest of the arguments.
Mike Stumpa5448542009-02-13 15:32:32 +0000264 for (CallExpr::const_arg_iterator i = E->arg_begin(), e = E->arg_end();
Anders Carlssonacfde802009-02-12 00:39:25 +0000265 i != e; ++i)
Mike Stumpa5448542009-02-13 15:32:32 +0000266 Args.push_back(std::make_pair(EmitAnyExprToTemp(*i),
Anders Carlssonacfde802009-02-12 00:39:25 +0000267 i->getType()));
Mike Stumpa5448542009-02-13 15:32:32 +0000268
Anders Carlssonacfde802009-02-12 00:39:25 +0000269 // And call the block.
Mike Stumpa5448542009-02-13 15:32:32 +0000270 return EmitCall(CGM.getTypes().getFunctionInfo(E->getType(), Args),
Anders Carlssonacfde802009-02-12 00:39:25 +0000271 Func, Args);
272}
Anders Carlssond5cab542009-02-12 17:55:02 +0000273
274llvm::Constant *CodeGenModule::GetAddrOfGlobalBlock(const BlockExpr *BE) {
275 if (!NSConcreteGlobalBlock) {
276 const llvm::Type *Ty = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
277
Mike Stumpa5448542009-02-13 15:32:32 +0000278 // FIXME: Wee should have a CodeGenModule::AddRuntimeVariable that does the
Anders Carlssond5cab542009-02-12 17:55:02 +0000279 // same thing as CreateRuntimeFunction if there's already a variable with
280 // the same name.
Mike Stumpa5448542009-02-13 15:32:32 +0000281 NSConcreteGlobalBlock =
Anders Carlssond5cab542009-02-12 17:55:02 +0000282 new llvm::GlobalVariable(Ty, false,
Mike Stumpa5448542009-02-13 15:32:32 +0000283 llvm::GlobalVariable::ExternalLinkage, 0,
284 "_NSConcreteGlobalBlock", &getModule());
Anders Carlssond5cab542009-02-12 17:55:02 +0000285 }
286
287 // Generate the block descriptor.
288 const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy);
Mike Stump7cbb3602009-02-13 16:01:35 +0000289 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
290 getTypes().ConvertType(getContext().IntTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000291
Anders Carlssond5cab542009-02-12 17:55:02 +0000292 llvm::Constant *DescriptorFields[2];
Mike Stumpa5448542009-02-13 15:32:32 +0000293
Anders Carlssond5cab542009-02-12 17:55:02 +0000294 // Reserved
295 DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000296
Anders Carlssond5cab542009-02-12 17:55:02 +0000297 // Block literal size. For global blocks we just use the size of the generic
298 // block literal struct.
Mike Stumpa5448542009-02-13 15:32:32 +0000299 uint64_t BlockLiteralSize =
Mike Stump9b8a7972009-02-13 15:25:34 +0000300 TheTargetData.getTypeStoreSizeInBits(getGenericBlockLiteralType()) / 8;
Anders Carlssond5cab542009-02-12 17:55:02 +0000301 DescriptorFields[1] = llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize);
Mike Stumpa5448542009-02-13 15:32:32 +0000302
303 llvm::Constant *DescriptorStruct =
Anders Carlssond5cab542009-02-12 17:55:02 +0000304 llvm::ConstantStruct::get(&DescriptorFields[0], 2);
Mike Stumpa5448542009-02-13 15:32:32 +0000305
Anders Carlssond5cab542009-02-12 17:55:02 +0000306 llvm::GlobalVariable *Descriptor =
307 new llvm::GlobalVariable(DescriptorStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000308 llvm::GlobalVariable::InternalLinkage,
309 DescriptorStruct, "__block_descriptor_global",
Anders Carlssond5cab542009-02-12 17:55:02 +0000310 &getModule());
Mike Stumpa5448542009-02-13 15:32:32 +0000311
Anders Carlssond5cab542009-02-12 17:55:02 +0000312 // Generate the constants for the block literal.
313 llvm::Constant *LiteralFields[5];
Mike Stumpa5448542009-02-13 15:32:32 +0000314
Anders Carlssond5cab542009-02-12 17:55:02 +0000315 CodeGenFunction::BlockInfo Info(0, "global");
316 llvm::Function *Fn = CodeGenFunction(*this).GenerateBlockFunction(BE, Info);
Mike Stumpa5448542009-02-13 15:32:32 +0000317
Anders Carlssond5cab542009-02-12 17:55:02 +0000318 // isa
319 LiteralFields[0] = NSConcreteGlobalBlock;
Mike Stumpa5448542009-02-13 15:32:32 +0000320
Anders Carlssond5cab542009-02-12 17:55:02 +0000321 // Flags
Mike Stumpe5fee252009-02-13 16:19:19 +0000322 LiteralFields[1] = llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL);
Mike Stumpa5448542009-02-13 15:32:32 +0000323
Anders Carlssond5cab542009-02-12 17:55:02 +0000324 // Reserved
Mike Stump7cbb3602009-02-13 16:01:35 +0000325 LiteralFields[2] = llvm::Constant::getNullValue(IntTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000326
Anders Carlssond5cab542009-02-12 17:55:02 +0000327 // Function
328 LiteralFields[3] = Fn;
Mike Stumpa5448542009-02-13 15:32:32 +0000329
Anders Carlssond5cab542009-02-12 17:55:02 +0000330 // Descriptor
331 LiteralFields[4] = Descriptor;
Mike Stumpa5448542009-02-13 15:32:32 +0000332
333 llvm::Constant *BlockLiteralStruct =
Anders Carlssond5cab542009-02-12 17:55:02 +0000334 llvm::ConstantStruct::get(&LiteralFields[0], 5);
Mike Stumpa5448542009-02-13 15:32:32 +0000335
336 llvm::GlobalVariable *BlockLiteral =
Anders Carlssond5cab542009-02-12 17:55:02 +0000337 new llvm::GlobalVariable(BlockLiteralStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000338 llvm::GlobalVariable::InternalLinkage,
339 BlockLiteralStruct, "__block_literal_global",
Anders Carlssond5cab542009-02-12 17:55:02 +0000340 &getModule());
Mike Stumpa5448542009-02-13 15:32:32 +0000341
Anders Carlssond5cab542009-02-12 17:55:02 +0000342 return BlockLiteral;
343}
344
345llvm::Function *CodeGenFunction::GenerateBlockFunction(const BlockExpr *Expr,
346 const BlockInfo& Info)
347{
Mike Stumpa5448542009-02-13 15:32:32 +0000348 const FunctionTypeProto *FTy =
Anders Carlssond5cab542009-02-12 17:55:02 +0000349 cast<FunctionTypeProto>(Expr->getFunctionType());
Mike Stumpa5448542009-02-13 15:32:32 +0000350
Anders Carlssond5cab542009-02-12 17:55:02 +0000351 FunctionArgList Args;
Mike Stumpa5448542009-02-13 15:32:32 +0000352
Anders Carlssond5cab542009-02-12 17:55:02 +0000353 const BlockDecl *BD = Expr->getBlockDecl();
354
355 // FIXME: This leaks
Mike Stumpa5448542009-02-13 15:32:32 +0000356 ImplicitParamDecl *SelfDecl =
Anders Carlssond5cab542009-02-12 17:55:02 +0000357 ImplicitParamDecl::Create(getContext(), 0,
358 SourceLocation(), 0,
359 getContext().getPointerType(getContext().VoidTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000360
Anders Carlssond5cab542009-02-12 17:55:02 +0000361 Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType()));
Mike Stumpa5448542009-02-13 15:32:32 +0000362
363 for (BlockDecl::param_iterator i = BD->param_begin(),
Anders Carlssond5cab542009-02-12 17:55:02 +0000364 e = BD->param_end(); i != e; ++i)
365 Args.push_back(std::make_pair(*e, (*e)->getType()));
Mike Stumpa5448542009-02-13 15:32:32 +0000366
367 const CGFunctionInfo &FI =
Anders Carlssond5cab542009-02-12 17:55:02 +0000368 CGM.getTypes().getFunctionInfo(FTy->getResultType(), Args);
369
370 std::string Name = std::string("__block_function_") + Info.NameSuffix;
371
372 CodeGenTypes &Types = CGM.getTypes();
373 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, FTy->isVariadic());
Mike Stumpa5448542009-02-13 15:32:32 +0000374
375 llvm::Function *Fn =
Anders Carlssond5cab542009-02-12 17:55:02 +0000376 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
377 Name,
378 &CGM.getModule());
Mike Stumpa5448542009-02-13 15:32:32 +0000379
380 StartFunction(BD, FTy->getResultType(), Fn, Args,
Anders Carlssond5cab542009-02-12 17:55:02 +0000381 Expr->getBody()->getLocEnd());
382 EmitStmt(Expr->getBody());
383 FinishFunction(cast<CompoundStmt>(Expr->getBody())->getRBracLoc());
384
385 return Fn;
386}