blob: 9588e4df5ed58f6e5dabc3be2065d0296592b652 [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 +000024// Block flags
25enum {
26 IsGlobal = 1 << 28
27};
28
Mike Stumpab695142009-02-13 15:16:56 +000029const llvm::Type *CodeGenModule::getBlockDescriptorType() {
30 if (BlockDescriptorType)
31 return BlockDescriptorType;
32
Mike Stumpa5448542009-02-13 15:32:32 +000033 const llvm::Type *UnsignedLongTy =
Mike Stumpab695142009-02-13 15:16:56 +000034 getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +000035
Mike Stumpab695142009-02-13 15:16:56 +000036 // struct __block_descriptor {
37 // unsigned long reserved;
38 // unsigned long block_size;
39 // };
Mike Stumpa5448542009-02-13 15:32:32 +000040 BlockDescriptorType = llvm::StructType::get(UnsignedLongTy,
41 UnsignedLongTy,
Mike Stumpab695142009-02-13 15:16:56 +000042 NULL);
43
44 getModule().addTypeName("struct.__block_descriptor",
45 BlockDescriptorType);
46
47 return BlockDescriptorType;
Anders Carlssonacfde802009-02-12 00:39:25 +000048}
49
Mike Stump9b8a7972009-02-13 15:25:34 +000050const llvm::Type *
51CodeGenModule::getGenericBlockLiteralType() {
52 if (GenericBlockLiteralType)
53 return GenericBlockLiteralType;
54
Mike Stumpa5448542009-02-13 15:32:32 +000055 const llvm::Type *Int8PtrTy =
Mike Stump9b8a7972009-02-13 15:25:34 +000056 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Mike Stumpa5448542009-02-13 15:32:32 +000057
58 const llvm::Type *BlockDescPtrTy =
Mike Stump9b8a7972009-02-13 15:25:34 +000059 llvm::PointerType::getUnqual(getBlockDescriptorType());
Mike Stumpa5448542009-02-13 15:32:32 +000060
Mike Stump9b8a7972009-02-13 15:25:34 +000061 // struct __block_literal_generic {
62 // void *isa;
63 // int flags;
64 // int reserved;
65 // void (*invoke)(void *);
66 // struct __block_descriptor *descriptor;
67 // };
68 GenericBlockLiteralType = llvm::StructType::get(Int8PtrTy,
69 llvm::Type::Int32Ty,
70 llvm::Type::Int32Ty,
71 Int8PtrTy,
72 BlockDescPtrTy,
73 NULL);
Mike Stumpa5448542009-02-13 15:32:32 +000074
Mike Stump9b8a7972009-02-13 15:25:34 +000075 getModule().addTypeName("struct.__block_literal_generic",
76 GenericBlockLiteralType);
Mike Stumpa5448542009-02-13 15:32:32 +000077
Mike Stump9b8a7972009-02-13 15:25:34 +000078 return GenericBlockLiteralType;
Anders Carlssonacfde802009-02-12 00:39:25 +000079}
80
Mike Stumpa5448542009-02-13 15:32:32 +000081/// getBlockFunctionType - Given a BlockPointerType, will return the
Anders Carlssonacfde802009-02-12 00:39:25 +000082/// function type for the block, including the first block literal argument.
83static QualType getBlockFunctionType(ASTContext &Ctx,
Anders Carlssond5cab542009-02-12 17:55:02 +000084 const BlockPointerType *BPT) {
Anders Carlssonacfde802009-02-12 00:39:25 +000085 const FunctionTypeProto *FTy = cast<FunctionTypeProto>(BPT->getPointeeType());
Mike Stumpa5448542009-02-13 15:32:32 +000086
Anders Carlssonacfde802009-02-12 00:39:25 +000087 llvm::SmallVector<QualType, 8> Types;
88 Types.push_back(Ctx.getPointerType(Ctx.VoidTy));
Mike Stumpa5448542009-02-13 15:32:32 +000089
Anders Carlssonacfde802009-02-12 00:39:25 +000090 for (FunctionTypeProto::arg_type_iterator i = FTy->arg_type_begin(),
91 e = FTy->arg_type_end(); i != e; ++i)
92 Types.push_back(*i);
Mike Stumpa5448542009-02-13 15:32:32 +000093
Anders Carlssonacfde802009-02-12 00:39:25 +000094 return Ctx.getFunctionType(FTy->getResultType(),
Mike Stumpa5448542009-02-13 15:32:32 +000095 &Types[0], Types.size(),
Anders Carlssonacfde802009-02-12 00:39:25 +000096 FTy->isVariadic(), 0);
97}
98
Anders Carlssond5cab542009-02-12 17:55:02 +000099RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E) {
Mike Stumpa5448542009-02-13 15:32:32 +0000100 const BlockPointerType *BPT =
Anders Carlssonacfde802009-02-12 00:39:25 +0000101 E->getCallee()->getType()->getAsBlockPointerType();
Mike Stumpa5448542009-02-13 15:32:32 +0000102
Anders Carlssonacfde802009-02-12 00:39:25 +0000103 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
104
105 // Get a pointer to the generic block literal.
106 const llvm::Type *BlockLiteralTy =
Mike Stump9b8a7972009-02-13 15:25:34 +0000107 llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType());
Anders Carlssonacfde802009-02-12 00:39:25 +0000108
109 // Bitcast the callee to a block literal.
Mike Stumpa5448542009-02-13 15:32:32 +0000110 llvm::Value *BlockLiteral =
Anders Carlssonacfde802009-02-12 00:39:25 +0000111 Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal");
112
113 // Get the function pointer from the literal.
114 llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp");
115 llvm::Value *Func = Builder.CreateLoad(FuncPtr, FuncPtr, "tmp");
116
117 // Cast the function pointer to the right type.
Mike Stumpa5448542009-02-13 15:32:32 +0000118 const llvm::Type *BlockFTy =
Anders Carlssonacfde802009-02-12 00:39:25 +0000119 ConvertType(getBlockFunctionType(getContext(), BPT));
120 const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy);
121 Func = Builder.CreateBitCast(Func, BlockFTyPtr);
122
Mike Stumpa5448542009-02-13 15:32:32 +0000123 BlockLiteral =
124 Builder.CreateBitCast(BlockLiteral,
Anders Carlssonacfde802009-02-12 00:39:25 +0000125 llvm::PointerType::getUnqual(llvm::Type::Int8Ty),
126 "tmp");
Mike Stumpa5448542009-02-13 15:32:32 +0000127
Anders Carlssonacfde802009-02-12 00:39:25 +0000128 // Add the block literal.
129 QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy);
130 CallArgList Args;
131 Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000132
Anders Carlssonacfde802009-02-12 00:39:25 +0000133 // And the rest of the arguments.
Mike Stumpa5448542009-02-13 15:32:32 +0000134 for (CallExpr::const_arg_iterator i = E->arg_begin(), e = E->arg_end();
Anders Carlssonacfde802009-02-12 00:39:25 +0000135 i != e; ++i)
Mike Stumpa5448542009-02-13 15:32:32 +0000136 Args.push_back(std::make_pair(EmitAnyExprToTemp(*i),
Anders Carlssonacfde802009-02-12 00:39:25 +0000137 i->getType()));
Mike Stumpa5448542009-02-13 15:32:32 +0000138
Anders Carlssonacfde802009-02-12 00:39:25 +0000139 // And call the block.
Mike Stumpa5448542009-02-13 15:32:32 +0000140 return EmitCall(CGM.getTypes().getFunctionInfo(E->getType(), Args),
Anders Carlssonacfde802009-02-12 00:39:25 +0000141 Func, Args);
142}
Anders Carlssond5cab542009-02-12 17:55:02 +0000143
144llvm::Constant *CodeGenModule::GetAddrOfGlobalBlock(const BlockExpr *BE) {
145 if (!NSConcreteGlobalBlock) {
146 const llvm::Type *Ty = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
147
Mike Stumpa5448542009-02-13 15:32:32 +0000148 // FIXME: Wee should have a CodeGenModule::AddRuntimeVariable that does the
Anders Carlssond5cab542009-02-12 17:55:02 +0000149 // same thing as CreateRuntimeFunction if there's already a variable with
150 // the same name.
Mike Stumpa5448542009-02-13 15:32:32 +0000151 NSConcreteGlobalBlock =
Anders Carlssond5cab542009-02-12 17:55:02 +0000152 new llvm::GlobalVariable(Ty, false,
Mike Stumpa5448542009-02-13 15:32:32 +0000153 llvm::GlobalVariable::ExternalLinkage, 0,
154 "_NSConcreteGlobalBlock", &getModule());
Anders Carlssond5cab542009-02-12 17:55:02 +0000155 }
156
157 // Generate the block descriptor.
158 const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000159
Anders Carlssond5cab542009-02-12 17:55:02 +0000160 llvm::Constant *DescriptorFields[2];
Mike Stumpa5448542009-02-13 15:32:32 +0000161
Anders Carlssond5cab542009-02-12 17:55:02 +0000162 // Reserved
163 DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000164
Anders Carlssond5cab542009-02-12 17:55:02 +0000165 // Block literal size. For global blocks we just use the size of the generic
166 // block literal struct.
Mike Stumpa5448542009-02-13 15:32:32 +0000167 uint64_t BlockLiteralSize =
Mike Stump9b8a7972009-02-13 15:25:34 +0000168 TheTargetData.getTypeStoreSizeInBits(getGenericBlockLiteralType()) / 8;
Anders Carlssond5cab542009-02-12 17:55:02 +0000169 DescriptorFields[1] = llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize);
Mike Stumpa5448542009-02-13 15:32:32 +0000170
171 llvm::Constant *DescriptorStruct =
Anders Carlssond5cab542009-02-12 17:55:02 +0000172 llvm::ConstantStruct::get(&DescriptorFields[0], 2);
Mike Stumpa5448542009-02-13 15:32:32 +0000173
Anders Carlssond5cab542009-02-12 17:55:02 +0000174 llvm::GlobalVariable *Descriptor =
175 new llvm::GlobalVariable(DescriptorStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000176 llvm::GlobalVariable::InternalLinkage,
177 DescriptorStruct, "__block_descriptor_global",
Anders Carlssond5cab542009-02-12 17:55:02 +0000178 &getModule());
Mike Stumpa5448542009-02-13 15:32:32 +0000179
Anders Carlssond5cab542009-02-12 17:55:02 +0000180 // Generate the constants for the block literal.
181 llvm::Constant *LiteralFields[5];
Mike Stumpa5448542009-02-13 15:32:32 +0000182
Anders Carlssond5cab542009-02-12 17:55:02 +0000183 CodeGenFunction::BlockInfo Info(0, "global");
184 llvm::Function *Fn = CodeGenFunction(*this).GenerateBlockFunction(BE, Info);
Mike Stumpa5448542009-02-13 15:32:32 +0000185
Anders Carlssond5cab542009-02-12 17:55:02 +0000186 // isa
187 LiteralFields[0] = NSConcreteGlobalBlock;
Mike Stumpa5448542009-02-13 15:32:32 +0000188
Anders Carlssond5cab542009-02-12 17:55:02 +0000189 // Flags
190 LiteralFields[1] = llvm::ConstantInt::get(llvm::Type::Int32Ty, IsGlobal);
Mike Stumpa5448542009-02-13 15:32:32 +0000191
Anders Carlssond5cab542009-02-12 17:55:02 +0000192 // Reserved
193 LiteralFields[2] = llvm::Constant::getNullValue(llvm::Type::Int32Ty);
Mike Stumpa5448542009-02-13 15:32:32 +0000194
Anders Carlssond5cab542009-02-12 17:55:02 +0000195 // Function
196 LiteralFields[3] = Fn;
Mike Stumpa5448542009-02-13 15:32:32 +0000197
Anders Carlssond5cab542009-02-12 17:55:02 +0000198 // Descriptor
199 LiteralFields[4] = Descriptor;
Mike Stumpa5448542009-02-13 15:32:32 +0000200
201 llvm::Constant *BlockLiteralStruct =
Anders Carlssond5cab542009-02-12 17:55:02 +0000202 llvm::ConstantStruct::get(&LiteralFields[0], 5);
Mike Stumpa5448542009-02-13 15:32:32 +0000203
204 llvm::GlobalVariable *BlockLiteral =
Anders Carlssond5cab542009-02-12 17:55:02 +0000205 new llvm::GlobalVariable(BlockLiteralStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000206 llvm::GlobalVariable::InternalLinkage,
207 BlockLiteralStruct, "__block_literal_global",
Anders Carlssond5cab542009-02-12 17:55:02 +0000208 &getModule());
Mike Stumpa5448542009-02-13 15:32:32 +0000209
Anders Carlssond5cab542009-02-12 17:55:02 +0000210 return BlockLiteral;
211}
212
213llvm::Function *CodeGenFunction::GenerateBlockFunction(const BlockExpr *Expr,
214 const BlockInfo& Info)
215{
Mike Stumpa5448542009-02-13 15:32:32 +0000216 const FunctionTypeProto *FTy =
Anders Carlssond5cab542009-02-12 17:55:02 +0000217 cast<FunctionTypeProto>(Expr->getFunctionType());
Mike Stumpa5448542009-02-13 15:32:32 +0000218
Anders Carlssond5cab542009-02-12 17:55:02 +0000219 FunctionArgList Args;
Mike Stumpa5448542009-02-13 15:32:32 +0000220
Anders Carlssond5cab542009-02-12 17:55:02 +0000221 const BlockDecl *BD = Expr->getBlockDecl();
222
223 // FIXME: This leaks
Mike Stumpa5448542009-02-13 15:32:32 +0000224 ImplicitParamDecl *SelfDecl =
Anders Carlssond5cab542009-02-12 17:55:02 +0000225 ImplicitParamDecl::Create(getContext(), 0,
226 SourceLocation(), 0,
227 getContext().getPointerType(getContext().VoidTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000228
Anders Carlssond5cab542009-02-12 17:55:02 +0000229 Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType()));
Mike Stumpa5448542009-02-13 15:32:32 +0000230
231 for (BlockDecl::param_iterator i = BD->param_begin(),
Anders Carlssond5cab542009-02-12 17:55:02 +0000232 e = BD->param_end(); i != e; ++i)
233 Args.push_back(std::make_pair(*e, (*e)->getType()));
Mike Stumpa5448542009-02-13 15:32:32 +0000234
235 const CGFunctionInfo &FI =
Anders Carlssond5cab542009-02-12 17:55:02 +0000236 CGM.getTypes().getFunctionInfo(FTy->getResultType(), Args);
237
238 std::string Name = std::string("__block_function_") + Info.NameSuffix;
239
240 CodeGenTypes &Types = CGM.getTypes();
241 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, FTy->isVariadic());
Mike Stumpa5448542009-02-13 15:32:32 +0000242
243 llvm::Function *Fn =
Anders Carlssond5cab542009-02-12 17:55:02 +0000244 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
245 Name,
246 &CGM.getModule());
Mike Stumpa5448542009-02-13 15:32:32 +0000247
248 StartFunction(BD, FTy->getResultType(), Fn, Args,
Anders Carlssond5cab542009-02-12 17:55:02 +0000249 Expr->getBody()->getLocEnd());
250 EmitStmt(Expr->getBody());
251 FinishFunction(cast<CompoundStmt>(Expr->getBody())->getRBracLoc());
252
253 return Fn;
254}