blob: 7708df5b0e91ba6c2d7f0820951524e897c095e5 [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 Stump7cbb3602009-02-13 16:01:35 +000061 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
62 getTypes().ConvertType(getContext().IntTy));
63
Mike Stump9b8a7972009-02-13 15:25:34 +000064 // struct __block_literal_generic {
65 // void *isa;
66 // int flags;
67 // int reserved;
68 // void (*invoke)(void *);
69 // struct __block_descriptor *descriptor;
70 // };
71 GenericBlockLiteralType = llvm::StructType::get(Int8PtrTy,
Mike Stump7cbb3602009-02-13 16:01:35 +000072 IntTy,
73 IntTy,
Mike Stump9b8a7972009-02-13 15:25:34 +000074 Int8PtrTy,
75 BlockDescPtrTy,
76 NULL);
Mike Stumpa5448542009-02-13 15:32:32 +000077
Mike Stump9b8a7972009-02-13 15:25:34 +000078 getModule().addTypeName("struct.__block_literal_generic",
79 GenericBlockLiteralType);
Mike Stumpa5448542009-02-13 15:32:32 +000080
Mike Stump9b8a7972009-02-13 15:25:34 +000081 return GenericBlockLiteralType;
Anders Carlssonacfde802009-02-12 00:39:25 +000082}
83
Mike Stumpa5448542009-02-13 15:32:32 +000084/// getBlockFunctionType - Given a BlockPointerType, will return the
Anders Carlssonacfde802009-02-12 00:39:25 +000085/// function type for the block, including the first block literal argument.
86static QualType getBlockFunctionType(ASTContext &Ctx,
Anders Carlssond5cab542009-02-12 17:55:02 +000087 const BlockPointerType *BPT) {
Anders Carlssonacfde802009-02-12 00:39:25 +000088 const FunctionTypeProto *FTy = cast<FunctionTypeProto>(BPT->getPointeeType());
Mike Stumpa5448542009-02-13 15:32:32 +000089
Anders Carlssonacfde802009-02-12 00:39:25 +000090 llvm::SmallVector<QualType, 8> Types;
91 Types.push_back(Ctx.getPointerType(Ctx.VoidTy));
Mike Stumpa5448542009-02-13 15:32:32 +000092
Anders Carlssonacfde802009-02-12 00:39:25 +000093 for (FunctionTypeProto::arg_type_iterator i = FTy->arg_type_begin(),
94 e = FTy->arg_type_end(); i != e; ++i)
95 Types.push_back(*i);
Mike Stumpa5448542009-02-13 15:32:32 +000096
Anders Carlssonacfde802009-02-12 00:39:25 +000097 return Ctx.getFunctionType(FTy->getResultType(),
Mike Stumpa5448542009-02-13 15:32:32 +000098 &Types[0], Types.size(),
Anders Carlssonacfde802009-02-12 00:39:25 +000099 FTy->isVariadic(), 0);
100}
101
Anders Carlssond5cab542009-02-12 17:55:02 +0000102RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E) {
Mike Stumpa5448542009-02-13 15:32:32 +0000103 const BlockPointerType *BPT =
Anders Carlssonacfde802009-02-12 00:39:25 +0000104 E->getCallee()->getType()->getAsBlockPointerType();
Mike Stumpa5448542009-02-13 15:32:32 +0000105
Anders Carlssonacfde802009-02-12 00:39:25 +0000106 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
107
108 // Get a pointer to the generic block literal.
109 const llvm::Type *BlockLiteralTy =
Mike Stump9b8a7972009-02-13 15:25:34 +0000110 llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType());
Anders Carlssonacfde802009-02-12 00:39:25 +0000111
112 // Bitcast the callee to a block literal.
Mike Stumpa5448542009-02-13 15:32:32 +0000113 llvm::Value *BlockLiteral =
Anders Carlssonacfde802009-02-12 00:39:25 +0000114 Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal");
115
116 // Get the function pointer from the literal.
117 llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp");
118 llvm::Value *Func = Builder.CreateLoad(FuncPtr, FuncPtr, "tmp");
119
120 // Cast the function pointer to the right type.
Mike Stumpa5448542009-02-13 15:32:32 +0000121 const llvm::Type *BlockFTy =
Anders Carlssonacfde802009-02-12 00:39:25 +0000122 ConvertType(getBlockFunctionType(getContext(), BPT));
123 const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy);
124 Func = Builder.CreateBitCast(Func, BlockFTyPtr);
125
Mike Stumpa5448542009-02-13 15:32:32 +0000126 BlockLiteral =
127 Builder.CreateBitCast(BlockLiteral,
Anders Carlssonacfde802009-02-12 00:39:25 +0000128 llvm::PointerType::getUnqual(llvm::Type::Int8Ty),
129 "tmp");
Mike Stumpa5448542009-02-13 15:32:32 +0000130
Anders Carlssonacfde802009-02-12 00:39:25 +0000131 // Add the block literal.
132 QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy);
133 CallArgList Args;
134 Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000135
Anders Carlssonacfde802009-02-12 00:39:25 +0000136 // And the rest of the arguments.
Mike Stumpa5448542009-02-13 15:32:32 +0000137 for (CallExpr::const_arg_iterator i = E->arg_begin(), e = E->arg_end();
Anders Carlssonacfde802009-02-12 00:39:25 +0000138 i != e; ++i)
Mike Stumpa5448542009-02-13 15:32:32 +0000139 Args.push_back(std::make_pair(EmitAnyExprToTemp(*i),
Anders Carlssonacfde802009-02-12 00:39:25 +0000140 i->getType()));
Mike Stumpa5448542009-02-13 15:32:32 +0000141
Anders Carlssonacfde802009-02-12 00:39:25 +0000142 // And call the block.
Mike Stumpa5448542009-02-13 15:32:32 +0000143 return EmitCall(CGM.getTypes().getFunctionInfo(E->getType(), Args),
Anders Carlssonacfde802009-02-12 00:39:25 +0000144 Func, Args);
145}
Anders Carlssond5cab542009-02-12 17:55:02 +0000146
147llvm::Constant *CodeGenModule::GetAddrOfGlobalBlock(const BlockExpr *BE) {
148 if (!NSConcreteGlobalBlock) {
149 const llvm::Type *Ty = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
150
Mike Stumpa5448542009-02-13 15:32:32 +0000151 // FIXME: Wee should have a CodeGenModule::AddRuntimeVariable that does the
Anders Carlssond5cab542009-02-12 17:55:02 +0000152 // same thing as CreateRuntimeFunction if there's already a variable with
153 // the same name.
Mike Stumpa5448542009-02-13 15:32:32 +0000154 NSConcreteGlobalBlock =
Anders Carlssond5cab542009-02-12 17:55:02 +0000155 new llvm::GlobalVariable(Ty, false,
Mike Stumpa5448542009-02-13 15:32:32 +0000156 llvm::GlobalVariable::ExternalLinkage, 0,
157 "_NSConcreteGlobalBlock", &getModule());
Anders Carlssond5cab542009-02-12 17:55:02 +0000158 }
159
160 // Generate the block descriptor.
161 const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy);
Mike Stump7cbb3602009-02-13 16:01:35 +0000162 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
163 getTypes().ConvertType(getContext().IntTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000164
Anders Carlssond5cab542009-02-12 17:55:02 +0000165 llvm::Constant *DescriptorFields[2];
Mike Stumpa5448542009-02-13 15:32:32 +0000166
Anders Carlssond5cab542009-02-12 17:55:02 +0000167 // Reserved
168 DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000169
Anders Carlssond5cab542009-02-12 17:55:02 +0000170 // Block literal size. For global blocks we just use the size of the generic
171 // block literal struct.
Mike Stumpa5448542009-02-13 15:32:32 +0000172 uint64_t BlockLiteralSize =
Mike Stump9b8a7972009-02-13 15:25:34 +0000173 TheTargetData.getTypeStoreSizeInBits(getGenericBlockLiteralType()) / 8;
Anders Carlssond5cab542009-02-12 17:55:02 +0000174 DescriptorFields[1] = llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize);
Mike Stumpa5448542009-02-13 15:32:32 +0000175
176 llvm::Constant *DescriptorStruct =
Anders Carlssond5cab542009-02-12 17:55:02 +0000177 llvm::ConstantStruct::get(&DescriptorFields[0], 2);
Mike Stumpa5448542009-02-13 15:32:32 +0000178
Anders Carlssond5cab542009-02-12 17:55:02 +0000179 llvm::GlobalVariable *Descriptor =
180 new llvm::GlobalVariable(DescriptorStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000181 llvm::GlobalVariable::InternalLinkage,
182 DescriptorStruct, "__block_descriptor_global",
Anders Carlssond5cab542009-02-12 17:55:02 +0000183 &getModule());
Mike Stumpa5448542009-02-13 15:32:32 +0000184
Anders Carlssond5cab542009-02-12 17:55:02 +0000185 // Generate the constants for the block literal.
186 llvm::Constant *LiteralFields[5];
Mike Stumpa5448542009-02-13 15:32:32 +0000187
Anders Carlssond5cab542009-02-12 17:55:02 +0000188 CodeGenFunction::BlockInfo Info(0, "global");
189 llvm::Function *Fn = CodeGenFunction(*this).GenerateBlockFunction(BE, Info);
Mike Stumpa5448542009-02-13 15:32:32 +0000190
Anders Carlssond5cab542009-02-12 17:55:02 +0000191 // isa
192 LiteralFields[0] = NSConcreteGlobalBlock;
Mike Stumpa5448542009-02-13 15:32:32 +0000193
Anders Carlssond5cab542009-02-12 17:55:02 +0000194 // Flags
Mike Stump7cbb3602009-02-13 16:01:35 +0000195 LiteralFields[1] = llvm::ConstantInt::get(IntTy, IsGlobal);
Mike Stumpa5448542009-02-13 15:32:32 +0000196
Anders Carlssond5cab542009-02-12 17:55:02 +0000197 // Reserved
Mike Stump7cbb3602009-02-13 16:01:35 +0000198 LiteralFields[2] = llvm::Constant::getNullValue(IntTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000199
Anders Carlssond5cab542009-02-12 17:55:02 +0000200 // Function
201 LiteralFields[3] = Fn;
Mike Stumpa5448542009-02-13 15:32:32 +0000202
Anders Carlssond5cab542009-02-12 17:55:02 +0000203 // Descriptor
204 LiteralFields[4] = Descriptor;
Mike Stumpa5448542009-02-13 15:32:32 +0000205
206 llvm::Constant *BlockLiteralStruct =
Anders Carlssond5cab542009-02-12 17:55:02 +0000207 llvm::ConstantStruct::get(&LiteralFields[0], 5);
Mike Stumpa5448542009-02-13 15:32:32 +0000208
209 llvm::GlobalVariable *BlockLiteral =
Anders Carlssond5cab542009-02-12 17:55:02 +0000210 new llvm::GlobalVariable(BlockLiteralStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000211 llvm::GlobalVariable::InternalLinkage,
212 BlockLiteralStruct, "__block_literal_global",
Anders Carlssond5cab542009-02-12 17:55:02 +0000213 &getModule());
Mike Stumpa5448542009-02-13 15:32:32 +0000214
Anders Carlssond5cab542009-02-12 17:55:02 +0000215 return BlockLiteral;
216}
217
218llvm::Function *CodeGenFunction::GenerateBlockFunction(const BlockExpr *Expr,
219 const BlockInfo& Info)
220{
Mike Stumpa5448542009-02-13 15:32:32 +0000221 const FunctionTypeProto *FTy =
Anders Carlssond5cab542009-02-12 17:55:02 +0000222 cast<FunctionTypeProto>(Expr->getFunctionType());
Mike Stumpa5448542009-02-13 15:32:32 +0000223
Anders Carlssond5cab542009-02-12 17:55:02 +0000224 FunctionArgList Args;
Mike Stumpa5448542009-02-13 15:32:32 +0000225
Anders Carlssond5cab542009-02-12 17:55:02 +0000226 const BlockDecl *BD = Expr->getBlockDecl();
227
228 // FIXME: This leaks
Mike Stumpa5448542009-02-13 15:32:32 +0000229 ImplicitParamDecl *SelfDecl =
Anders Carlssond5cab542009-02-12 17:55:02 +0000230 ImplicitParamDecl::Create(getContext(), 0,
231 SourceLocation(), 0,
232 getContext().getPointerType(getContext().VoidTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000233
Anders Carlssond5cab542009-02-12 17:55:02 +0000234 Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType()));
Mike Stumpa5448542009-02-13 15:32:32 +0000235
236 for (BlockDecl::param_iterator i = BD->param_begin(),
Anders Carlssond5cab542009-02-12 17:55:02 +0000237 e = BD->param_end(); i != e; ++i)
238 Args.push_back(std::make_pair(*e, (*e)->getType()));
Mike Stumpa5448542009-02-13 15:32:32 +0000239
240 const CGFunctionInfo &FI =
Anders Carlssond5cab542009-02-12 17:55:02 +0000241 CGM.getTypes().getFunctionInfo(FTy->getResultType(), Args);
242
243 std::string Name = std::string("__block_function_") + Info.NameSuffix;
244
245 CodeGenTypes &Types = CGM.getTypes();
246 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, FTy->isVariadic());
Mike Stumpa5448542009-02-13 15:32:32 +0000247
248 llvm::Function *Fn =
Anders Carlssond5cab542009-02-12 17:55:02 +0000249 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
250 Name,
251 &CGM.getModule());
Mike Stumpa5448542009-02-13 15:32:32 +0000252
253 StartFunction(BD, FTy->getResultType(), Fn, Args,
Anders Carlssond5cab542009-02-12 17:55:02 +0000254 Expr->getBody()->getLocEnd());
255 EmitStmt(Expr->getBody());
256 FinishFunction(cast<CompoundStmt>(Expr->getBody())->getRBracLoc());
257
258 return Fn;
259}