blob: fc18c7cc5647c8f500ee0c3cbfd6f2be96b4531c [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"
Ken Dyck199c3d62010-01-11 17:06:35 +000023#include "clang/AST/CharUnits.h"
Mike Stump2a998142009-03-04 18:17:45 +000024#include "clang/AST/Expr.h"
25#include "clang/AST/ExprCXX.h"
26#include "clang/AST/ExprObjC.h"
27
28#include <vector>
29#include <map>
30
31#include "CGBuilder.h"
32#include "CGCall.h"
33#include "CGValue.h"
34
35namespace llvm {
36 class Module;
37 class Constant;
38 class Function;
39 class GlobalValue;
40 class TargetData;
41 class FunctionType;
42 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),
David Chisnall5e530af2009-11-17 19:33:30 +000057 BLOCK_HAS_DESCRIPTOR = (1 << 29),
58 BLOCK_HAS_OBJC_TYPE = (1 << 30)
Mike Stumpd883d842009-03-04 15:35:22 +000059 };
60};
61
62class BlockModule : public BlockBase {
Mike Stump2a998142009-03-04 18:17:45 +000063 ASTContext &Context;
64 llvm::Module &TheModule;
Mike Stump90a90432009-03-04 18:47:42 +000065 const llvm::TargetData &TheTargetData;
Mike Stump2a998142009-03-04 18:17:45 +000066 CodeGenTypes &Types;
Mike Stump90a90432009-03-04 18:47:42 +000067 CodeGenModule &CGM;
Owen Andersona1cf15f2009-07-14 23:10:40 +000068 llvm::LLVMContext &VMContext;
Mike Stump1eb44332009-09-09 15:08:12 +000069
Mike Stump2a998142009-03-04 18:17:45 +000070 ASTContext &getContext() const { return Context; }
71 llvm::Module &getModule() const { return TheModule; }
72 CodeGenTypes &getTypes() { return Types; }
Mike Stump90a90432009-03-04 18:47:42 +000073 const llvm::TargetData &getTargetData() const { return TheTargetData; }
Mike Stump2a998142009-03-04 18:17:45 +000074public:
75 llvm::Constant *getNSConcreteGlobalBlock();
76 llvm::Constant *getNSConcreteStackBlock();
77 int getGlobalUniqueCount() { return ++Block.GlobalUniqueCount; }
78 const llvm::Type *getBlockDescriptorType();
79
80 const llvm::Type *getGenericBlockLiteralType();
Mike Stump2a998142009-03-04 18:17:45 +000081
Mike Stump90a90432009-03-04 18:47:42 +000082 llvm::Constant *GetAddrOfGlobalBlock(const BlockExpr *BE, const char *);
83
Mike Stump2a998142009-03-04 18:17:45 +000084 /// NSConcreteGlobalBlock - Cached reference to the class pointer for global
85 /// blocks.
86 llvm::Constant *NSConcreteGlobalBlock;
87
88 /// NSConcreteStackBlock - Cached reference to the class poinnter for stack
89 /// blocks.
90 llvm::Constant *NSConcreteStackBlock;
Mike Stump1eb44332009-09-09 15:08:12 +000091
Mike Stump2a998142009-03-04 18:17:45 +000092 const llvm::Type *BlockDescriptorType;
93 const llvm::Type *GenericBlockLiteralType;
Blaine Garstc0ea8852010-02-19 00:24:37 +000094
Mike Stump2a998142009-03-04 18:17:45 +000095 struct {
96 int GlobalUniqueCount;
97 } Block;
98
Mike Stumpee094222009-03-06 06:12:24 +000099 llvm::Value *BlockObjectAssign;
Mike Stump797b6322009-03-05 01:23:13 +0000100 llvm::Value *BlockObjectDispose;
101 const llvm::Type *PtrToInt8Ty;
102
Mike Stump3899a7f2009-06-05 23:26:36 +0000103 std::map<uint64_t, llvm::Constant *> AssignCache;
104 std::map<uint64_t, llvm::Constant *> DestroyCache;
105
Mike Stump90a90432009-03-04 18:47:42 +0000106 BlockModule(ASTContext &C, llvm::Module &M, const llvm::TargetData &TD,
107 CodeGenTypes &T, CodeGenModule &CodeGen)
108 : Context(C), TheModule(M), TheTargetData(TD), Types(T),
Owen Andersona1cf15f2009-07-14 23:10:40 +0000109 CGM(CodeGen), VMContext(M.getContext()),
Mike Stump90a90432009-03-04 18:47:42 +0000110 NSConcreteGlobalBlock(0), NSConcreteStackBlock(0), BlockDescriptorType(0),
Blaine Garstc0ea8852010-02-19 00:24:37 +0000111 GenericBlockLiteralType(0),
Mike Stump08920992009-03-07 02:35:30 +0000112 BlockObjectAssign(0), BlockObjectDispose(0) {
Mike Stump2a998142009-03-04 18:17:45 +0000113 Block.GlobalUniqueCount = 0;
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000114 PtrToInt8Ty = llvm::Type::getInt8PtrTy(M.getContext());
Mike Stump2a998142009-03-04 18:17:45 +0000115 }
Mike Stump39605b42009-09-22 02:12:52 +0000116
Mike Stumpbf5fd782009-10-21 18:23:01 +0000117 bool BlockRequiresCopying(QualType Ty)
118 { return getContext().BlockRequiresCopying(Ty); }
Mike Stumpd883d842009-03-04 15:35:22 +0000119};
120
121class BlockFunction : public BlockBase {
Mike Stump797b6322009-03-05 01:23:13 +0000122 CodeGenModule &CGM;
Mike Stumpa4f668f2009-03-06 01:33:24 +0000123 CodeGenFunction &CGF;
Mike Stump00470a12009-03-05 08:32:30 +0000124 ASTContext &getContext() const;
Mike Stump797b6322009-03-05 01:23:13 +0000125
Owen Andersona1cf15f2009-07-14 23:10:40 +0000126protected:
127 llvm::LLVMContext &VMContext;
128
Mike Stumpd883d842009-03-04 15:35:22 +0000129public:
Mike Stump797b6322009-03-05 01:23:13 +0000130 const llvm::Type *PtrToInt8Ty;
Mike Stump08920992009-03-07 02:35:30 +0000131 struct HelperInfo {
132 int index;
133 int flag;
134 bool RequiresCopying;
135 };
136
Mike Stump797b6322009-03-05 01:23:13 +0000137 enum {
Mike Stumpd883d842009-03-04 15:35:22 +0000138 BLOCK_FIELD_IS_OBJECT = 3, /* id, NSObject, __attribute__((NSObject)),
139 block, ... */
140 BLOCK_FIELD_IS_BLOCK = 7, /* a block variable */
141 BLOCK_FIELD_IS_BYREF = 8, /* the on stack structure holding the __block
142 variable */
143 BLOCK_FIELD_IS_WEAK = 16, /* declared __weak, only used in byref copy
144 helpers */
Mike Stump3899a7f2009-06-05 23:26:36 +0000145 BLOCK_BYREF_CALLER = 128, /* called from __block (byref) copy/dispose
Mike Stumpd883d842009-03-04 15:35:22 +0000146 support routines */
Mike Stump3899a7f2009-06-05 23:26:36 +0000147 BLOCK_BYREF_CURRENT_MAX = 256
Mike Stumpd883d842009-03-04 15:35:22 +0000148 };
Mike Stump3947de52009-03-04 18:57:26 +0000149
Mike Stumpad75ab42009-03-04 19:03:44 +0000150 /// BlockInfo - Information to generate a block literal.
151 struct BlockInfo {
152 /// BlockLiteralTy - The type of the block literal.
153 const llvm::Type *BlockLiteralTy;
154
Daniel Dunbar49f59ec2009-05-14 16:42:16 +0000155 /// Name - the name of the function this block was created for, if any.
Mike Stumpad75ab42009-03-04 19:03:44 +0000156 const char *Name;
157
158 /// ByCopyDeclRefs - Variables from parent scopes that have been imported
159 /// into this block.
Mike Stumpea26cb52009-10-21 03:49:08 +0000160 llvm::SmallVector<const BlockDeclRefExpr *, 8> DeclRefs;
Mike Stump1eb44332009-09-09 15:08:12 +0000161
Mike Stumpad75ab42009-03-04 19:03:44 +0000162 BlockInfo(const llvm::Type *blt, const char *n)
Daniel Dunbar49f59ec2009-05-14 16:42:16 +0000163 : BlockLiteralTy(blt), Name(n) {
164 // Skip asm prefix, if any.
165 if (Name && Name[0] == '\01')
166 ++Name;
167 }
Mike Stumpad75ab42009-03-04 19:03:44 +0000168 };
169
Mike Stump3947de52009-03-04 18:57:26 +0000170 CGBuilderTy &Builder;
171
Mike Stump08920992009-03-07 02:35:30 +0000172 BlockFunction(CodeGenModule &cgm, CodeGenFunction &cgf, CGBuilderTy &B);
173
174 /// BlockOffset - The offset in bytes for the next allocation of an
175 /// imported block variable.
Ken Dyck199c3d62010-01-11 17:06:35 +0000176 CharUnits BlockOffset;
Ken Dyck30a8a272010-01-26 19:13:33 +0000177 /// BlockAlign - Maximal alignment needed for the Block expressed in
178 /// characters.
179 CharUnits BlockAlign;
Mike Stump08920992009-03-07 02:35:30 +0000180
181 /// getBlockOffset - Allocate an offset for the ValueDecl from a
182 /// BlockDeclRefExpr in a block literal (BlockExpr).
Ken Dyck199c3d62010-01-11 17:06:35 +0000183 CharUnits getBlockOffset(const BlockDeclRefExpr *E);
Mike Stump08920992009-03-07 02:35:30 +0000184
185 /// BlockHasCopyDispose - True iff the block uses copy/dispose.
186 bool BlockHasCopyDispose;
187
188 /// BlockDeclRefDecls - Decls from BlockDeclRefExprs in apperance order
189 /// in a block literal. Decls without names are used for padding.
190 llvm::SmallVector<const Expr *, 8> BlockDeclRefDecls;
191
192 /// BlockDecls - Offsets for all Decls in BlockDeclRefExprs.
Ken Dyck199c3d62010-01-11 17:06:35 +0000193 std::map<const Decl*, CharUnits> BlockDecls;
Mike Stump3947de52009-03-04 18:57:26 +0000194
Mike Stumpad75ab42009-03-04 19:03:44 +0000195 ImplicitParamDecl *BlockStructDecl;
196 ImplicitParamDecl *getBlockStructDecl() { return BlockStructDecl; }
197
Mike Stump08920992009-03-07 02:35:30 +0000198 llvm::Constant *GenerateCopyHelperFunction(bool, const llvm::StructType *,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000199 std::vector<HelperInfo> *);
Mike Stump08920992009-03-07 02:35:30 +0000200 llvm::Constant *GenerateDestroyHelperFunction(bool, const llvm::StructType *,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000201 std::vector<HelperInfo> *);
Mike Stumpa4f668f2009-03-06 01:33:24 +0000202
Mike Stump08920992009-03-07 02:35:30 +0000203 llvm::Constant *BuildCopyHelper(const llvm::StructType *,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000204 std::vector<HelperInfo> *);
Mike Stump08920992009-03-07 02:35:30 +0000205 llvm::Constant *BuildDestroyHelper(const llvm::StructType *,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000206 std::vector<HelperInfo> *);
Mike Stump45031c02009-03-06 02:29:21 +0000207
Mike Stumpee094222009-03-06 06:12:24 +0000208 llvm::Constant *GeneratebyrefCopyHelperFunction(const llvm::Type *, int flag);
Mike Stump1851b682009-03-06 04:53:30 +0000209 llvm::Constant *GeneratebyrefDestroyHelperFunction(const llvm::Type *T, int);
Mike Stump45031c02009-03-06 02:29:21 +0000210
Mike Stump3899a7f2009-06-05 23:26:36 +0000211 llvm::Constant *BuildbyrefCopyHelper(const llvm::Type *T, int flag,
212 unsigned Align);
213 llvm::Constant *BuildbyrefDestroyHelper(const llvm::Type *T, int flag,
214 unsigned Align);
Mike Stump3947de52009-03-04 18:57:26 +0000215
Mike Stumpee094222009-03-06 06:12:24 +0000216 llvm::Value *getBlockObjectAssign();
Mike Stump797b6322009-03-05 01:23:13 +0000217 llvm::Value *getBlockObjectDispose();
Mike Stump1851b682009-03-06 04:53:30 +0000218 void BuildBlockRelease(llvm::Value *DeclPtr, int flag = BLOCK_FIELD_IS_BYREF);
Mike Stump00470a12009-03-05 08:32:30 +0000219
Mike Stumpbf5fd782009-10-21 18:23:01 +0000220 bool BlockRequiresCopying(QualType Ty)
221 { return getContext().BlockRequiresCopying(Ty); }
Mike Stumpd883d842009-03-04 15:35:22 +0000222};
223
224} // end namespace CodeGen
225} // end namespace clang
226
227#endif