blob: 21683517384ca96db4a59c47c5dbe2e8adf7e6d4 [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
33 const llvm::Type *UnsignedLongTy =
34 getTypes().ConvertType(getContext().UnsignedLongTy);
Anders Carlssonacfde802009-02-12 00:39:25 +000035
Mike Stumpab695142009-02-13 15:16:56 +000036 // struct __block_descriptor {
37 // unsigned long reserved;
38 // unsigned long block_size;
39 // };
40 BlockDescriptorType = llvm::StructType::get(UnsignedLongTy,
41 UnsignedLongTy,
42 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
55 const llvm::Type *Int8PtrTy =
56 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Anders Carlssonacfde802009-02-12 00:39:25 +000057
Mike Stump9b8a7972009-02-13 15:25:34 +000058 const llvm::Type *BlockDescPtrTy =
59 llvm::PointerType::getUnqual(getBlockDescriptorType());
Anders Carlssonacfde802009-02-12 00:39:25 +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);
Anders Carlssonacfde802009-02-12 00:39:25 +000074
Mike Stump9b8a7972009-02-13 15:25:34 +000075 getModule().addTypeName("struct.__block_literal_generic",
76 GenericBlockLiteralType);
Anders Carlssonacfde802009-02-12 00:39:25 +000077
Mike Stump9b8a7972009-02-13 15:25:34 +000078 return GenericBlockLiteralType;
Anders Carlssonacfde802009-02-12 00:39:25 +000079}
80
81/// getBlockFunctionType - Given a BlockPointerType, will return the
82/// 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());
86
87 llvm::SmallVector<QualType, 8> Types;
88 Types.push_back(Ctx.getPointerType(Ctx.VoidTy));
89
90 for (FunctionTypeProto::arg_type_iterator i = FTy->arg_type_begin(),
91 e = FTy->arg_type_end(); i != e; ++i)
92 Types.push_back(*i);
93
94 return Ctx.getFunctionType(FTy->getResultType(),
95 &Types[0], Types.size(),
96 FTy->isVariadic(), 0);
97}
98
Anders Carlssond5cab542009-02-12 17:55:02 +000099RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E) {
Anders Carlssonacfde802009-02-12 00:39:25 +0000100 const BlockPointerType *BPT =
101 E->getCallee()->getType()->getAsBlockPointerType();
102
103 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.
110 llvm::Value *BlockLiteral =
111 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.
118 const llvm::Type *BlockFTy =
119 ConvertType(getBlockFunctionType(getContext(), BPT));
120 const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy);
121 Func = Builder.CreateBitCast(Func, BlockFTyPtr);
122
123 BlockLiteral =
124 Builder.CreateBitCast(BlockLiteral,
125 llvm::PointerType::getUnqual(llvm::Type::Int8Ty),
126 "tmp");
127
128 // 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));
132
133 // And the rest of the arguments.
134 for (CallExpr::const_arg_iterator i = E->arg_begin(), e = E->arg_end();
135 i != e; ++i)
136 Args.push_back(std::make_pair(EmitAnyExprToTemp(*i),
137 i->getType()));
138
139 // And call the block.
140 return EmitCall(CGM.getTypes().getFunctionInfo(E->getType(), Args),
141 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
148 // FIXME: Wee should have a CodeGenModule::AddRuntimeVariable that does the
149 // same thing as CreateRuntimeFunction if there's already a variable with
150 // the same name.
151 NSConcreteGlobalBlock =
152 new llvm::GlobalVariable(Ty, false,
153 llvm::GlobalVariable::ExternalLinkage, 0,
154 "_NSConcreteGlobalBlock", &getModule());
155 }
156
157 // Generate the block descriptor.
158 const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy);
159
160 llvm::Constant *DescriptorFields[2];
161
162 // Reserved
163 DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy);
164
165 // Block literal size. For global blocks we just use the size of the generic
166 // block literal struct.
167 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);
170
171 llvm::Constant *DescriptorStruct =
172 llvm::ConstantStruct::get(&DescriptorFields[0], 2);
173
174 llvm::GlobalVariable *Descriptor =
175 new llvm::GlobalVariable(DescriptorStruct->getType(), true,
176 llvm::GlobalVariable::InternalLinkage,
177 DescriptorStruct, "__block_descriptor_global",
178 &getModule());
179
180 // Generate the constants for the block literal.
181 llvm::Constant *LiteralFields[5];
182
183 CodeGenFunction::BlockInfo Info(0, "global");
184 llvm::Function *Fn = CodeGenFunction(*this).GenerateBlockFunction(BE, Info);
185
186 // isa
187 LiteralFields[0] = NSConcreteGlobalBlock;
188
189 // Flags
190 LiteralFields[1] = llvm::ConstantInt::get(llvm::Type::Int32Ty, IsGlobal);
191
192 // Reserved
193 LiteralFields[2] = llvm::Constant::getNullValue(llvm::Type::Int32Ty);
194
195 // Function
196 LiteralFields[3] = Fn;
197
198 // Descriptor
199 LiteralFields[4] = Descriptor;
200
201 llvm::Constant *BlockLiteralStruct =
202 llvm::ConstantStruct::get(&LiteralFields[0], 5);
203
204 llvm::GlobalVariable *BlockLiteral =
205 new llvm::GlobalVariable(BlockLiteralStruct->getType(), true,
206 llvm::GlobalVariable::InternalLinkage,
207 BlockLiteralStruct, "__block_literal_global",
208 &getModule());
209
210 return BlockLiteral;
211}
212
213llvm::Function *CodeGenFunction::GenerateBlockFunction(const BlockExpr *Expr,
214 const BlockInfo& Info)
215{
216 const FunctionTypeProto *FTy =
217 cast<FunctionTypeProto>(Expr->getFunctionType());
218
219 FunctionArgList Args;
220
221 const BlockDecl *BD = Expr->getBlockDecl();
222
223 // FIXME: This leaks
224 ImplicitParamDecl *SelfDecl =
225 ImplicitParamDecl::Create(getContext(), 0,
226 SourceLocation(), 0,
227 getContext().getPointerType(getContext().VoidTy));
228
229 Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType()));
230
231 for (BlockDecl::param_iterator i = BD->param_begin(),
232 e = BD->param_end(); i != e; ++i)
233 Args.push_back(std::make_pair(*e, (*e)->getType()));
234
235 const CGFunctionInfo &FI =
236 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());
242
243 llvm::Function *Fn =
244 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
245 Name,
246 &CGM.getModule());
247
248 StartFunction(BD, FTy->getResultType(), Fn, Args,
249 Expr->getBody()->getLocEnd());
250 EmitStmt(Expr->getBody());
251 FinishFunction(cast<CompoundStmt>(Expr->getBody())->getRBracLoc());
252
253 return Fn;
254}
255
256