blob: 067dfc7fdfda8484243afc5cd8c61699e23ec760 [file] [log] [blame]
Mike Stumpd883d842009-03-04 15:35:22 +00001//===-- CGBlocks.h - state for LLVM CodeGen for blocks ----------*- C++ -*-===//
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 is the internal state used for llvm translation for block literals.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef CLANG_CODEGEN_CGBLOCKS_H
15#define CLANG_CODEGEN_CGBLOCKS_H
16
Mike Stump2a998142009-03-04 18:17:45 +000017#include "CodeGenTypes.h"
18#include "clang/AST/Type.h"
Owen Andersona1cf15f2009-07-14 23:10:40 +000019#include "llvm/Module.h"
Mike Stump2a998142009-03-04 18:17:45 +000020#include "llvm/ADT/SmallVector.h"
21#include "clang/Basic/TargetInfo.h"
Ken Dyck199c3d62010-01-11 17:06:35 +000022#include "clang/AST/CharUnits.h"
Mike Stump2a998142009-03-04 18:17:45 +000023#include "clang/AST/Expr.h"
24#include "clang/AST/ExprCXX.h"
25#include "clang/AST/ExprObjC.h"
26
27#include <vector>
28#include <map>
29
30#include "CGBuilder.h"
31#include "CGCall.h"
32#include "CGValue.h"
33
34namespace llvm {
35 class Module;
36 class Constant;
37 class Function;
38 class GlobalValue;
39 class TargetData;
40 class FunctionType;
John McCall3d3ec1c2010-04-21 10:05:39 +000041 class PointerType;
Mike Stump2a998142009-03-04 18:17:45 +000042 class Value;
Benjamin Kramerf21efe92009-08-11 17:46:57 +000043 class LLVMContext;
Mike Stump2a998142009-03-04 18:17:45 +000044}
45
Mike Stumpd883d842009-03-04 15:35:22 +000046namespace clang {
47
48namespace CodeGen {
Mike Stump90a90432009-03-04 18:47:42 +000049class CodeGenModule;
Mike Stumpd883d842009-03-04 15:35:22 +000050
51class BlockBase {
52public:
53 enum {
Mike Stumpd883d842009-03-04 15:35:22 +000054 BLOCK_HAS_COPY_DISPOSE = (1 << 25),
55 BLOCK_HAS_CXX_OBJ = (1 << 26),
Mike Stumpd883d842009-03-04 15:35:22 +000056 BLOCK_IS_GLOBAL = (1 << 28),
Blaine Garsta36e2232010-03-05 01:29:59 +000057 BLOCK_USE_STRET = (1 << 29),
58 BLOCK_HAS_SIGNATURE = (1 << 30)
Mike Stumpd883d842009-03-04 15:35:22 +000059 };
60};
61
Blaine Garsta36e2232010-03-05 01:29:59 +000062
Mike Stumpd883d842009-03-04 15:35:22 +000063class BlockModule : public BlockBase {
Mike Stump2a998142009-03-04 18:17:45 +000064 ASTContext &Context;
65 llvm::Module &TheModule;
Mike Stump90a90432009-03-04 18:47:42 +000066 const llvm::TargetData &TheTargetData;
Mike Stump2a998142009-03-04 18:17:45 +000067 CodeGenTypes &Types;
Mike Stump90a90432009-03-04 18:47:42 +000068 CodeGenModule &CGM;
Owen Andersona1cf15f2009-07-14 23:10:40 +000069 llvm::LLVMContext &VMContext;
Mike Stump1eb44332009-09-09 15:08:12 +000070
Mike Stump2a998142009-03-04 18:17:45 +000071 ASTContext &getContext() const { return Context; }
72 llvm::Module &getModule() const { return TheModule; }
73 CodeGenTypes &getTypes() { return Types; }
Mike Stump90a90432009-03-04 18:47:42 +000074 const llvm::TargetData &getTargetData() const { return TheTargetData; }
Mike Stump2a998142009-03-04 18:17:45 +000075public:
76 llvm::Constant *getNSConcreteGlobalBlock();
77 llvm::Constant *getNSConcreteStackBlock();
78 int getGlobalUniqueCount() { return ++Block.GlobalUniqueCount; }
79 const llvm::Type *getBlockDescriptorType();
80
81 const llvm::Type *getGenericBlockLiteralType();
Mike Stump2a998142009-03-04 18:17:45 +000082
Mike Stump90a90432009-03-04 18:47:42 +000083 llvm::Constant *GetAddrOfGlobalBlock(const BlockExpr *BE, const char *);
84
Mike Stump2a998142009-03-04 18:17:45 +000085 /// NSConcreteGlobalBlock - Cached reference to the class pointer for global
86 /// blocks.
87 llvm::Constant *NSConcreteGlobalBlock;
88
89 /// NSConcreteStackBlock - Cached reference to the class poinnter for stack
90 /// blocks.
91 llvm::Constant *NSConcreteStackBlock;
Mike Stump1eb44332009-09-09 15:08:12 +000092
Mike Stump2a998142009-03-04 18:17:45 +000093 const llvm::Type *BlockDescriptorType;
94 const llvm::Type *GenericBlockLiteralType;
Blaine Garstc0ea8852010-02-19 00:24:37 +000095
Mike Stump2a998142009-03-04 18:17:45 +000096 struct {
97 int GlobalUniqueCount;
98 } Block;
99
Mike Stumpee094222009-03-06 06:12:24 +0000100 llvm::Value *BlockObjectAssign;
Mike Stump797b6322009-03-05 01:23:13 +0000101 llvm::Value *BlockObjectDispose;
102 const llvm::Type *PtrToInt8Ty;
103
Mike Stump3899a7f2009-06-05 23:26:36 +0000104 std::map<uint64_t, llvm::Constant *> AssignCache;
105 std::map<uint64_t, llvm::Constant *> DestroyCache;
106
Mike Stump90a90432009-03-04 18:47:42 +0000107 BlockModule(ASTContext &C, llvm::Module &M, const llvm::TargetData &TD,
108 CodeGenTypes &T, CodeGenModule &CodeGen)
109 : Context(C), TheModule(M), TheTargetData(TD), Types(T),
Owen Andersona1cf15f2009-07-14 23:10:40 +0000110 CGM(CodeGen), VMContext(M.getContext()),
Mike Stump90a90432009-03-04 18:47:42 +0000111 NSConcreteGlobalBlock(0), NSConcreteStackBlock(0), BlockDescriptorType(0),
Blaine Garstc0ea8852010-02-19 00:24:37 +0000112 GenericBlockLiteralType(0),
Mike Stump08920992009-03-07 02:35:30 +0000113 BlockObjectAssign(0), BlockObjectDispose(0) {
Mike Stump2a998142009-03-04 18:17:45 +0000114 Block.GlobalUniqueCount = 0;
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000115 PtrToInt8Ty = llvm::Type::getInt8PtrTy(M.getContext());
Mike Stump2a998142009-03-04 18:17:45 +0000116 }
Mike Stump39605b42009-09-22 02:12:52 +0000117
Mike Stumpbf5fd782009-10-21 18:23:01 +0000118 bool BlockRequiresCopying(QualType Ty)
119 { return getContext().BlockRequiresCopying(Ty); }
Mike Stumpd883d842009-03-04 15:35:22 +0000120};
121
122class BlockFunction : public BlockBase {
Mike Stump797b6322009-03-05 01:23:13 +0000123 CodeGenModule &CGM;
Mike Stumpa4f668f2009-03-06 01:33:24 +0000124 CodeGenFunction &CGF;
Mike Stump00470a12009-03-05 08:32:30 +0000125 ASTContext &getContext() const;
Mike Stump797b6322009-03-05 01:23:13 +0000126
Owen Andersona1cf15f2009-07-14 23:10:40 +0000127protected:
128 llvm::LLVMContext &VMContext;
129
Mike Stumpd883d842009-03-04 15:35:22 +0000130public:
John McCall3d3ec1c2010-04-21 10:05:39 +0000131 const llvm::PointerType *PtrToInt8Ty;
Mike Stump08920992009-03-07 02:35:30 +0000132 struct HelperInfo {
133 int index;
134 int flag;
135 bool RequiresCopying;
136 };
137
Mike Stump797b6322009-03-05 01:23:13 +0000138 enum {
Mike Stumpd883d842009-03-04 15:35:22 +0000139 BLOCK_FIELD_IS_OBJECT = 3, /* id, NSObject, __attribute__((NSObject)),
140 block, ... */
141 BLOCK_FIELD_IS_BLOCK = 7, /* a block variable */
142 BLOCK_FIELD_IS_BYREF = 8, /* the on stack structure holding the __block
143 variable */
144 BLOCK_FIELD_IS_WEAK = 16, /* declared __weak, only used in byref copy
145 helpers */
Mike Stump3899a7f2009-06-05 23:26:36 +0000146 BLOCK_BYREF_CALLER = 128, /* called from __block (byref) copy/dispose
Mike Stumpd883d842009-03-04 15:35:22 +0000147 support routines */
Mike Stump3899a7f2009-06-05 23:26:36 +0000148 BLOCK_BYREF_CURRENT_MAX = 256
Mike Stumpd883d842009-03-04 15:35:22 +0000149 };
Mike Stump3947de52009-03-04 18:57:26 +0000150
Mike Stumpad75ab42009-03-04 19:03:44 +0000151 /// BlockInfo - Information to generate a block literal.
152 struct BlockInfo {
153 /// BlockLiteralTy - The type of the block literal.
154 const llvm::Type *BlockLiteralTy;
155
Daniel Dunbar49f59ec2009-05-14 16:42:16 +0000156 /// Name - the name of the function this block was created for, if any.
Mike Stumpad75ab42009-03-04 19:03:44 +0000157 const char *Name;
158
159 /// ByCopyDeclRefs - Variables from parent scopes that have been imported
160 /// into this block.
Mike Stumpea26cb52009-10-21 03:49:08 +0000161 llvm::SmallVector<const BlockDeclRefExpr *, 8> DeclRefs;
Mike Stump1eb44332009-09-09 15:08:12 +0000162
John McCallea1471e2010-05-20 01:18:31 +0000163 /// CXXThisRef - An expression referring to the required 'this'
164 /// expression.
165 const CXXThisExpr *CXXThisRef;
166
Mike Stumpad75ab42009-03-04 19:03:44 +0000167 BlockInfo(const llvm::Type *blt, const char *n)
John McCallea1471e2010-05-20 01:18:31 +0000168 : BlockLiteralTy(blt), Name(n), CXXThisRef(0) {
Daniel Dunbar49f59ec2009-05-14 16:42:16 +0000169 // Skip asm prefix, if any.
170 if (Name && Name[0] == '\01')
171 ++Name;
172 }
Mike Stumpad75ab42009-03-04 19:03:44 +0000173 };
174
Mike Stump3947de52009-03-04 18:57:26 +0000175 CGBuilderTy &Builder;
176
Mike Stump08920992009-03-07 02:35:30 +0000177 BlockFunction(CodeGenModule &cgm, CodeGenFunction &cgf, CGBuilderTy &B);
178
179 /// BlockOffset - The offset in bytes for the next allocation of an
180 /// imported block variable.
Ken Dyck199c3d62010-01-11 17:06:35 +0000181 CharUnits BlockOffset;
Ken Dyck30a8a272010-01-26 19:13:33 +0000182 /// BlockAlign - Maximal alignment needed for the Block expressed in
183 /// characters.
184 CharUnits BlockAlign;
Mike Stump08920992009-03-07 02:35:30 +0000185
John McCallea1471e2010-05-20 01:18:31 +0000186 /// getBlockOffset - Allocate a location within the block's storage
187 /// for a value with the given size and alignment requirements.
188 CharUnits getBlockOffset(CharUnits Size, CharUnits Align);
Mike Stump08920992009-03-07 02:35:30 +0000189
190 /// BlockHasCopyDispose - True iff the block uses copy/dispose.
191 bool BlockHasCopyDispose;
192
John McCallea1471e2010-05-20 01:18:31 +0000193 /// BlockLayout - The layout of the block's storage, represented as
194 /// a sequence of expressions which require such storage. The
195 /// expressions can be:
196 /// - a BlockDeclRefExpr, indicating that the given declaration
197 /// from an enclosing scope is needed by the block;
198 /// - a DeclRefExpr, which always wraps an anonymous VarDecl with
199 /// array type, used to insert padding into the block; or
200 /// - a CXXThisExpr, indicating that the C++ 'this' value should
201 /// propagate from the parent to the block.
202 /// This is a really silly representation.
203 llvm::SmallVector<const Expr *, 8> BlockLayout;
Mike Stump08920992009-03-07 02:35:30 +0000204
205 /// BlockDecls - Offsets for all Decls in BlockDeclRefExprs.
John McCallea1471e2010-05-20 01:18:31 +0000206 llvm::DenseMap<const Decl*, CharUnits> BlockDecls;
207
208 /// BlockCXXThisOffset - The offset of the C++ 'this' value within
209 /// the block structure.
210 CharUnits BlockCXXThisOffset;
Mike Stump3947de52009-03-04 18:57:26 +0000211
Mike Stumpad75ab42009-03-04 19:03:44 +0000212 ImplicitParamDecl *BlockStructDecl;
213 ImplicitParamDecl *getBlockStructDecl() { return BlockStructDecl; }
214
Mike Stump08920992009-03-07 02:35:30 +0000215 llvm::Constant *GenerateCopyHelperFunction(bool, const llvm::StructType *,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000216 std::vector<HelperInfo> *);
Mike Stump08920992009-03-07 02:35:30 +0000217 llvm::Constant *GenerateDestroyHelperFunction(bool, const llvm::StructType *,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000218 std::vector<HelperInfo> *);
Mike Stumpa4f668f2009-03-06 01:33:24 +0000219
Mike Stump08920992009-03-07 02:35:30 +0000220 llvm::Constant *BuildCopyHelper(const llvm::StructType *,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000221 std::vector<HelperInfo> *);
Mike Stump08920992009-03-07 02:35:30 +0000222 llvm::Constant *BuildDestroyHelper(const llvm::StructType *,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000223 std::vector<HelperInfo> *);
Mike Stump45031c02009-03-06 02:29:21 +0000224
Mike Stumpee094222009-03-06 06:12:24 +0000225 llvm::Constant *GeneratebyrefCopyHelperFunction(const llvm::Type *, int flag);
Mike Stump1851b682009-03-06 04:53:30 +0000226 llvm::Constant *GeneratebyrefDestroyHelperFunction(const llvm::Type *T, int);
Mike Stump45031c02009-03-06 02:29:21 +0000227
Mike Stump3899a7f2009-06-05 23:26:36 +0000228 llvm::Constant *BuildbyrefCopyHelper(const llvm::Type *T, int flag,
229 unsigned Align);
230 llvm::Constant *BuildbyrefDestroyHelper(const llvm::Type *T, int flag,
231 unsigned Align);
Mike Stump3947de52009-03-04 18:57:26 +0000232
Mike Stumpee094222009-03-06 06:12:24 +0000233 llvm::Value *getBlockObjectAssign();
Mike Stump797b6322009-03-05 01:23:13 +0000234 llvm::Value *getBlockObjectDispose();
Mike Stump1851b682009-03-06 04:53:30 +0000235 void BuildBlockRelease(llvm::Value *DeclPtr, int flag = BLOCK_FIELD_IS_BYREF);
Mike Stump00470a12009-03-05 08:32:30 +0000236
Mike Stumpbf5fd782009-10-21 18:23:01 +0000237 bool BlockRequiresCopying(QualType Ty)
238 { return getContext().BlockRequiresCopying(Ty); }
Mike Stumpd883d842009-03-04 15:35:22 +0000239};
240
241} // end namespace CodeGen
242} // end namespace clang
243
244#endif