blob: 48807af2094bf66635e173bfc9b248fa0ab0bdc3 [file] [log] [blame]
Mike Stump376e3c02009-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 Stump95435672009-03-04 18:17:45 +000017#include "CodeGenTypes.h"
18#include "clang/AST/Type.h"
Owen Anderson170229f2009-07-14 23:10:40 +000019#include "llvm/Module.h"
Mike Stump95435672009-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 Kramer9cd050a2009-08-11 17:46:57 +000042 class LLVMContext;
Mike Stump95435672009-03-04 18:17:45 +000043}
44
Mike Stump376e3c02009-03-04 15:35:22 +000045namespace clang {
46
47namespace CodeGen {
Mike Stump6c396662009-03-04 18:47:42 +000048class CodeGenModule;
Mike Stump376e3c02009-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),
58 BLOCK_HAS_DESCRIPTOR = (1 << 29)
59 };
60};
61
62class BlockModule : public BlockBase {
Mike Stump95435672009-03-04 18:17:45 +000063 ASTContext &Context;
64 llvm::Module &TheModule;
Mike Stump6c396662009-03-04 18:47:42 +000065 const llvm::TargetData &TheTargetData;
Mike Stump95435672009-03-04 18:17:45 +000066 CodeGenTypes &Types;
Mike Stump6c396662009-03-04 18:47:42 +000067 CodeGenModule &CGM;
Owen Anderson170229f2009-07-14 23:10:40 +000068 llvm::LLVMContext &VMContext;
Mike Stump11289f42009-09-09 15:08:12 +000069
Mike Stump95435672009-03-04 18:17:45 +000070 ASTContext &getContext() const { return Context; }
71 llvm::Module &getModule() const { return TheModule; }
72 CodeGenTypes &getTypes() { return Types; }
Mike Stump6c396662009-03-04 18:47:42 +000073 const llvm::TargetData &getTargetData() const { return TheTargetData; }
Mike Stump95435672009-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();
81 const llvm::Type *getGenericExtendedBlockLiteralType();
82
Mike Stump6c396662009-03-04 18:47:42 +000083 llvm::Constant *GetAddrOfGlobalBlock(const BlockExpr *BE, const char *);
84
Mike Stump95435672009-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 Stump11289f42009-09-09 15:08:12 +000092
Mike Stump95435672009-03-04 18:17:45 +000093 const llvm::Type *BlockDescriptorType;
94 const llvm::Type *GenericBlockLiteralType;
95 const llvm::Type *GenericExtendedBlockLiteralType;
96 struct {
97 int GlobalUniqueCount;
98 } Block;
99
Mike Stumpf89230d2009-03-06 06:12:24 +0000100 llvm::Value *BlockObjectAssign;
Mike Stump626aecc2009-03-05 01:23:13 +0000101 llvm::Value *BlockObjectDispose;
102 const llvm::Type *PtrToInt8Ty;
103
Mike Stumpcbc2bca2009-06-05 23:26:36 +0000104 std::map<uint64_t, llvm::Constant *> AssignCache;
105 std::map<uint64_t, llvm::Constant *> DestroyCache;
106
Mike Stump6c396662009-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 Anderson170229f2009-07-14 23:10:40 +0000110 CGM(CodeGen), VMContext(M.getContext()),
Mike Stump6c396662009-03-04 18:47:42 +0000111 NSConcreteGlobalBlock(0), NSConcreteStackBlock(0), BlockDescriptorType(0),
Mike Stumpaeb0ffd2009-03-07 02:35:30 +0000112 GenericBlockLiteralType(0), GenericExtendedBlockLiteralType(0),
113 BlockObjectAssign(0), BlockObjectDispose(0) {
Mike Stump95435672009-03-04 18:17:45 +0000114 Block.GlobalUniqueCount = 0;
Benjamin Kramerabd5b902009-10-13 10:07:13 +0000115 PtrToInt8Ty = llvm::Type::getInt8PtrTy(M.getContext());
Mike Stump95435672009-03-04 18:17:45 +0000116 }
Mike Stump2114d7c2009-09-22 02:12:52 +0000117
118 bool BlockRequiresCopying(QualType Ty) {
119 if (Ty->isBlockPointerType())
120 return true;
121 if (getContext().isObjCNSObjectType(Ty))
122 return true;
123 if (Ty->isObjCObjectPointerType())
124 return true;
125 return false;
126 }
Mike Stump376e3c02009-03-04 15:35:22 +0000127};
128
129class BlockFunction : public BlockBase {
Mike Stump626aecc2009-03-05 01:23:13 +0000130 CodeGenModule &CGM;
Mike Stump0c743272009-03-06 01:33:24 +0000131 CodeGenFunction &CGF;
Mike Stump4446dcf2009-03-05 08:32:30 +0000132 ASTContext &getContext() const;
Mike Stump626aecc2009-03-05 01:23:13 +0000133
Owen Anderson170229f2009-07-14 23:10:40 +0000134protected:
135 llvm::LLVMContext &VMContext;
136
Mike Stump376e3c02009-03-04 15:35:22 +0000137public:
Mike Stump626aecc2009-03-05 01:23:13 +0000138 const llvm::Type *PtrToInt8Ty;
Mike Stumpaeb0ffd2009-03-07 02:35:30 +0000139 struct HelperInfo {
140 int index;
141 int flag;
142 bool RequiresCopying;
143 };
144
Mike Stump626aecc2009-03-05 01:23:13 +0000145 enum {
Mike Stump376e3c02009-03-04 15:35:22 +0000146 BLOCK_FIELD_IS_OBJECT = 3, /* id, NSObject, __attribute__((NSObject)),
147 block, ... */
148 BLOCK_FIELD_IS_BLOCK = 7, /* a block variable */
149 BLOCK_FIELD_IS_BYREF = 8, /* the on stack structure holding the __block
150 variable */
151 BLOCK_FIELD_IS_WEAK = 16, /* declared __weak, only used in byref copy
152 helpers */
Mike Stumpcbc2bca2009-06-05 23:26:36 +0000153 BLOCK_BYREF_CALLER = 128, /* called from __block (byref) copy/dispose
Mike Stump376e3c02009-03-04 15:35:22 +0000154 support routines */
Mike Stumpcbc2bca2009-06-05 23:26:36 +0000155 BLOCK_BYREF_CURRENT_MAX = 256
Mike Stump376e3c02009-03-04 15:35:22 +0000156 };
Mike Stump06acea8a2009-03-04 18:57:26 +0000157
Mike Stump8473a122009-03-04 19:03:44 +0000158 /// BlockInfo - Information to generate a block literal.
159 struct BlockInfo {
160 /// BlockLiteralTy - The type of the block literal.
161 const llvm::Type *BlockLiteralTy;
162
Daniel Dunbar0a2171c2009-05-14 16:42:16 +0000163 /// Name - the name of the function this block was created for, if any.
Mike Stump8473a122009-03-04 19:03:44 +0000164 const char *Name;
165
166 /// ByCopyDeclRefs - Variables from parent scopes that have been imported
167 /// into this block.
Mike Stump7fe9cc12009-10-21 03:49:08 +0000168 llvm::SmallVector<const BlockDeclRefExpr *, 8> DeclRefs;
Mike Stump11289f42009-09-09 15:08:12 +0000169
Mike Stump8473a122009-03-04 19:03:44 +0000170 BlockInfo(const llvm::Type *blt, const char *n)
Daniel Dunbar0a2171c2009-05-14 16:42:16 +0000171 : BlockLiteralTy(blt), Name(n) {
172 // Skip asm prefix, if any.
173 if (Name && Name[0] == '\01')
174 ++Name;
175 }
Mike Stump8473a122009-03-04 19:03:44 +0000176 };
177
Mike Stump06acea8a2009-03-04 18:57:26 +0000178 CGBuilderTy &Builder;
179
Mike Stumpaeb0ffd2009-03-07 02:35:30 +0000180 BlockFunction(CodeGenModule &cgm, CodeGenFunction &cgf, CGBuilderTy &B);
181
182 /// BlockOffset - The offset in bytes for the next allocation of an
183 /// imported block variable.
184 uint64_t BlockOffset;
185 /// BlockAlign - Maximal alignment needed for the Block expressed in bytes.
186 uint64_t BlockAlign;
187
188 /// getBlockOffset - Allocate an offset for the ValueDecl from a
189 /// BlockDeclRefExpr in a block literal (BlockExpr).
190 uint64_t getBlockOffset(const BlockDeclRefExpr *E);
191
192 /// BlockHasCopyDispose - True iff the block uses copy/dispose.
193 bool BlockHasCopyDispose;
194
195 /// BlockDeclRefDecls - Decls from BlockDeclRefExprs in apperance order
196 /// in a block literal. Decls without names are used for padding.
197 llvm::SmallVector<const Expr *, 8> BlockDeclRefDecls;
198
199 /// BlockDecls - Offsets for all Decls in BlockDeclRefExprs.
200 std::map<const Decl*, uint64_t> BlockDecls;
Mike Stump06acea8a2009-03-04 18:57:26 +0000201
Mike Stump8473a122009-03-04 19:03:44 +0000202 ImplicitParamDecl *BlockStructDecl;
203 ImplicitParamDecl *getBlockStructDecl() { return BlockStructDecl; }
204
Mike Stumpaeb0ffd2009-03-07 02:35:30 +0000205 llvm::Constant *GenerateCopyHelperFunction(bool, const llvm::StructType *,
Mike Stump67645932009-04-10 18:52:28 +0000206 std::vector<HelperInfo> *);
Mike Stumpaeb0ffd2009-03-07 02:35:30 +0000207 llvm::Constant *GenerateDestroyHelperFunction(bool, const llvm::StructType *,
Mike Stump67645932009-04-10 18:52:28 +0000208 std::vector<HelperInfo> *);
Mike Stump0c743272009-03-06 01:33:24 +0000209
Mike Stumpaeb0ffd2009-03-07 02:35:30 +0000210 llvm::Constant *BuildCopyHelper(const llvm::StructType *,
Mike Stump67645932009-04-10 18:52:28 +0000211 std::vector<HelperInfo> *);
Mike Stumpaeb0ffd2009-03-07 02:35:30 +0000212 llvm::Constant *BuildDestroyHelper(const llvm::StructType *,
Mike Stump67645932009-04-10 18:52:28 +0000213 std::vector<HelperInfo> *);
Mike Stumpee2a5ee2009-03-06 02:29:21 +0000214
Mike Stumpf89230d2009-03-06 06:12:24 +0000215 llvm::Constant *GeneratebyrefCopyHelperFunction(const llvm::Type *, int flag);
Mike Stumpfbe25dd2009-03-06 04:53:30 +0000216 llvm::Constant *GeneratebyrefDestroyHelperFunction(const llvm::Type *T, int);
Mike Stumpee2a5ee2009-03-06 02:29:21 +0000217
Mike Stumpcbc2bca2009-06-05 23:26:36 +0000218 llvm::Constant *BuildbyrefCopyHelper(const llvm::Type *T, int flag,
219 unsigned Align);
220 llvm::Constant *BuildbyrefDestroyHelper(const llvm::Type *T, int flag,
221 unsigned Align);
Mike Stump06acea8a2009-03-04 18:57:26 +0000222
Mike Stumpf89230d2009-03-06 06:12:24 +0000223 llvm::Value *getBlockObjectAssign();
Mike Stump626aecc2009-03-05 01:23:13 +0000224 llvm::Value *getBlockObjectDispose();
Mike Stumpfbe25dd2009-03-06 04:53:30 +0000225 void BuildBlockRelease(llvm::Value *DeclPtr, int flag = BLOCK_FIELD_IS_BYREF);
Mike Stump4446dcf2009-03-05 08:32:30 +0000226
Mike Stump2114d7c2009-09-22 02:12:52 +0000227 bool BlockRequiresCopying(QualType Ty);
Mike Stump376e3c02009-03-04 15:35:22 +0000228};
229
230} // end namespace CodeGen
231} // end namespace clang
232
233#endif