blob: 38e02a70a4e25d16e57a0d15d73c3b2a033981a3 [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/DenseMap.h"
21#include "llvm/ADT/SmallVector.h"
22#include "clang/Basic/TargetInfo.h"
23#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;
41 class Value;
Benjamin Kramerf21efe92009-08-11 17:46:57 +000042 class LLVMContext;
Mike Stump2a998142009-03-04 18:17:45 +000043}
44
Mike Stumpd883d842009-03-04 15:35:22 +000045namespace clang {
46
47namespace CodeGen {
Mike Stump90a90432009-03-04 18:47:42 +000048class CodeGenModule;
Mike Stumpd883d842009-03-04 15:35:22 +000049
50class BlockBase {
51public:
52 enum {
53 BLOCK_NEEDS_FREE = (1 << 24),
54 BLOCK_HAS_COPY_DISPOSE = (1 << 25),
55 BLOCK_HAS_CXX_OBJ = (1 << 26),
56 BLOCK_IS_GC = (1 << 27),
57 BLOCK_IS_GLOBAL = (1 << 28),
David Chisnall5e530af2009-11-17 19:33:30 +000058 BLOCK_HAS_DESCRIPTOR = (1 << 29),
59 BLOCK_HAS_OBJC_TYPE = (1 << 30)
Mike Stumpd883d842009-03-04 15:35:22 +000060 };
61};
62
63class 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();
82 const llvm::Type *getGenericExtendedBlockLiteralType();
83
Mike Stump90a90432009-03-04 18:47:42 +000084 llvm::Constant *GetAddrOfGlobalBlock(const BlockExpr *BE, const char *);
85
Mike Stump2a998142009-03-04 18:17:45 +000086 /// NSConcreteGlobalBlock - Cached reference to the class pointer for global
87 /// blocks.
88 llvm::Constant *NSConcreteGlobalBlock;
89
90 /// NSConcreteStackBlock - Cached reference to the class poinnter for stack
91 /// blocks.
92 llvm::Constant *NSConcreteStackBlock;
Mike Stump1eb44332009-09-09 15:08:12 +000093
Mike Stump2a998142009-03-04 18:17:45 +000094 const llvm::Type *BlockDescriptorType;
95 const llvm::Type *GenericBlockLiteralType;
96 const llvm::Type *GenericExtendedBlockLiteralType;
97 struct {
98 int GlobalUniqueCount;
99 } Block;
100
Mike Stumpee094222009-03-06 06:12:24 +0000101 llvm::Value *BlockObjectAssign;
Mike Stump797b6322009-03-05 01:23:13 +0000102 llvm::Value *BlockObjectDispose;
103 const llvm::Type *PtrToInt8Ty;
104
Mike Stump3899a7f2009-06-05 23:26:36 +0000105 std::map<uint64_t, llvm::Constant *> AssignCache;
106 std::map<uint64_t, llvm::Constant *> DestroyCache;
107
Mike Stump90a90432009-03-04 18:47:42 +0000108 BlockModule(ASTContext &C, llvm::Module &M, const llvm::TargetData &TD,
109 CodeGenTypes &T, CodeGenModule &CodeGen)
110 : Context(C), TheModule(M), TheTargetData(TD), Types(T),
Owen Andersona1cf15f2009-07-14 23:10:40 +0000111 CGM(CodeGen), VMContext(M.getContext()),
Mike Stump90a90432009-03-04 18:47:42 +0000112 NSConcreteGlobalBlock(0), NSConcreteStackBlock(0), BlockDescriptorType(0),
Mike Stump08920992009-03-07 02:35:30 +0000113 GenericBlockLiteralType(0), GenericExtendedBlockLiteralType(0),
114 BlockObjectAssign(0), BlockObjectDispose(0) {
Mike Stump2a998142009-03-04 18:17:45 +0000115 Block.GlobalUniqueCount = 0;
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000116 PtrToInt8Ty = llvm::Type::getInt8PtrTy(M.getContext());
Mike Stump2a998142009-03-04 18:17:45 +0000117 }
Mike Stump39605b42009-09-22 02:12:52 +0000118
Mike Stumpbf5fd782009-10-21 18:23:01 +0000119 bool BlockRequiresCopying(QualType Ty)
120 { return getContext().BlockRequiresCopying(Ty); }
Mike Stumpd883d842009-03-04 15:35:22 +0000121};
122
123class BlockFunction : public BlockBase {
Mike Stump797b6322009-03-05 01:23:13 +0000124 CodeGenModule &CGM;
Mike Stumpa4f668f2009-03-06 01:33:24 +0000125 CodeGenFunction &CGF;
Mike Stump00470a12009-03-05 08:32:30 +0000126 ASTContext &getContext() const;
Mike Stump797b6322009-03-05 01:23:13 +0000127
Owen Andersona1cf15f2009-07-14 23:10:40 +0000128protected:
129 llvm::LLVMContext &VMContext;
130
Mike Stumpd883d842009-03-04 15:35:22 +0000131public:
Mike Stump797b6322009-03-05 01:23:13 +0000132 const llvm::Type *PtrToInt8Ty;
Mike Stump08920992009-03-07 02:35:30 +0000133 struct HelperInfo {
134 int index;
135 int flag;
136 bool RequiresCopying;
137 };
138
Mike Stump797b6322009-03-05 01:23:13 +0000139 enum {
Mike Stumpd883d842009-03-04 15:35:22 +0000140 BLOCK_FIELD_IS_OBJECT = 3, /* id, NSObject, __attribute__((NSObject)),
141 block, ... */
142 BLOCK_FIELD_IS_BLOCK = 7, /* a block variable */
143 BLOCK_FIELD_IS_BYREF = 8, /* the on stack structure holding the __block
144 variable */
145 BLOCK_FIELD_IS_WEAK = 16, /* declared __weak, only used in byref copy
146 helpers */
Mike Stump3899a7f2009-06-05 23:26:36 +0000147 BLOCK_BYREF_CALLER = 128, /* called from __block (byref) copy/dispose
Mike Stumpd883d842009-03-04 15:35:22 +0000148 support routines */
Mike Stump3899a7f2009-06-05 23:26:36 +0000149 BLOCK_BYREF_CURRENT_MAX = 256
Mike Stumpd883d842009-03-04 15:35:22 +0000150 };
Mike Stump3947de52009-03-04 18:57:26 +0000151
Mike Stumpad75ab42009-03-04 19:03:44 +0000152 /// BlockInfo - Information to generate a block literal.
153 struct BlockInfo {
154 /// BlockLiteralTy - The type of the block literal.
155 const llvm::Type *BlockLiteralTy;
156
Daniel Dunbar49f59ec2009-05-14 16:42:16 +0000157 /// Name - the name of the function this block was created for, if any.
Mike Stumpad75ab42009-03-04 19:03:44 +0000158 const char *Name;
159
160 /// ByCopyDeclRefs - Variables from parent scopes that have been imported
161 /// into this block.
Mike Stumpea26cb52009-10-21 03:49:08 +0000162 llvm::SmallVector<const BlockDeclRefExpr *, 8> DeclRefs;
Mike Stump1eb44332009-09-09 15:08:12 +0000163
Mike Stumpad75ab42009-03-04 19:03:44 +0000164 BlockInfo(const llvm::Type *blt, const char *n)
Daniel Dunbar49f59ec2009-05-14 16:42:16 +0000165 : BlockLiteralTy(blt), Name(n) {
166 // Skip asm prefix, if any.
167 if (Name && Name[0] == '\01')
168 ++Name;
169 }
Mike Stumpad75ab42009-03-04 19:03:44 +0000170 };
171
Mike Stump3947de52009-03-04 18:57:26 +0000172 CGBuilderTy &Builder;
173
Mike Stump08920992009-03-07 02:35:30 +0000174 BlockFunction(CodeGenModule &cgm, CodeGenFunction &cgf, CGBuilderTy &B);
175
176 /// BlockOffset - The offset in bytes for the next allocation of an
177 /// imported block variable.
178 uint64_t BlockOffset;
179 /// BlockAlign - Maximal alignment needed for the Block expressed in bytes.
180 uint64_t BlockAlign;
181
182 /// getBlockOffset - Allocate an offset for the ValueDecl from a
183 /// BlockDeclRefExpr in a block literal (BlockExpr).
184 uint64_t getBlockOffset(const BlockDeclRefExpr *E);
185
186 /// BlockHasCopyDispose - True iff the block uses copy/dispose.
187 bool BlockHasCopyDispose;
188
189 /// BlockDeclRefDecls - Decls from BlockDeclRefExprs in apperance order
190 /// in a block literal. Decls without names are used for padding.
191 llvm::SmallVector<const Expr *, 8> BlockDeclRefDecls;
192
193 /// BlockDecls - Offsets for all Decls in BlockDeclRefExprs.
194 std::map<const Decl*, uint64_t> BlockDecls;
Mike Stump3947de52009-03-04 18:57:26 +0000195
Mike Stumpad75ab42009-03-04 19:03:44 +0000196 ImplicitParamDecl *BlockStructDecl;
197 ImplicitParamDecl *getBlockStructDecl() { return BlockStructDecl; }
198
Mike Stump08920992009-03-07 02:35:30 +0000199 llvm::Constant *GenerateCopyHelperFunction(bool, const llvm::StructType *,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000200 std::vector<HelperInfo> *);
Mike Stump08920992009-03-07 02:35:30 +0000201 llvm::Constant *GenerateDestroyHelperFunction(bool, const llvm::StructType *,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000202 std::vector<HelperInfo> *);
Mike Stumpa4f668f2009-03-06 01:33:24 +0000203
Mike Stump08920992009-03-07 02:35:30 +0000204 llvm::Constant *BuildCopyHelper(const llvm::StructType *,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000205 std::vector<HelperInfo> *);
Mike Stump08920992009-03-07 02:35:30 +0000206 llvm::Constant *BuildDestroyHelper(const llvm::StructType *,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000207 std::vector<HelperInfo> *);
Mike Stump45031c02009-03-06 02:29:21 +0000208
Mike Stumpee094222009-03-06 06:12:24 +0000209 llvm::Constant *GeneratebyrefCopyHelperFunction(const llvm::Type *, int flag);
Mike Stump1851b682009-03-06 04:53:30 +0000210 llvm::Constant *GeneratebyrefDestroyHelperFunction(const llvm::Type *T, int);
Mike Stump45031c02009-03-06 02:29:21 +0000211
Mike Stump3899a7f2009-06-05 23:26:36 +0000212 llvm::Constant *BuildbyrefCopyHelper(const llvm::Type *T, int flag,
213 unsigned Align);
214 llvm::Constant *BuildbyrefDestroyHelper(const llvm::Type *T, int flag,
215 unsigned Align);
Mike Stump3947de52009-03-04 18:57:26 +0000216
Mike Stumpee094222009-03-06 06:12:24 +0000217 llvm::Value *getBlockObjectAssign();
Mike Stump797b6322009-03-05 01:23:13 +0000218 llvm::Value *getBlockObjectDispose();
Mike Stump1851b682009-03-06 04:53:30 +0000219 void BuildBlockRelease(llvm::Value *DeclPtr, int flag = BLOCK_FIELD_IS_BYREF);
Mike Stump00470a12009-03-05 08:32:30 +0000220
Mike Stumpbf5fd782009-10-21 18:23:01 +0000221 bool BlockRequiresCopying(QualType Ty)
222 { return getContext().BlockRequiresCopying(Ty); }
Mike Stumpd883d842009-03-04 15:35:22 +0000223};
224
225} // end namespace CodeGen
226} // end namespace clang
227
228#endif