blob: 1edabef4ec7431e6c5a53285445ef8e4ed3ed550 [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
Stephen Hines176edba2014-12-01 14:53:08 -080014#ifndef LLVM_CLANG_LIB_CODEGEN_CGBLOCKS_H
15#define LLVM_CLANG_LIB_CODEGEN_CGBLOCKS_H
Mike Stumpd883d842009-03-04 15:35:22 +000016
Chandler Carruth55fc8732012-12-04 09:13:33 +000017#include "CGBuilder.h"
18#include "CGCall.h"
19#include "CGValue.h"
20#include "CodeGenFunction.h"
Mike Stump2a998142009-03-04 18:17:45 +000021#include "CodeGenTypes.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"
Chandler Carruth55fc8732012-12-04 09:13:33 +000026#include "clang/AST/Type.h"
27#include "clang/Basic/TargetInfo.h"
Chandler Carruth3b844ba2013-01-02 11:45:17 +000028#include "llvm/IR/Module.h"
Mike Stump2a998142009-03-04 18:17:45 +000029
30namespace llvm {
Stephen Hines6bcf27b2014-05-29 04:14:42 -070031class Module;
32class Constant;
33class Function;
34class GlobalValue;
35class DataLayout;
36class FunctionType;
37class PointerType;
38class Value;
39class LLVMContext;
Mike Stump2a998142009-03-04 18:17:45 +000040}
41
Mike Stumpd883d842009-03-04 15:35:22 +000042namespace clang {
43
44namespace CodeGen {
John McCalld16c2cf2011-02-08 08:22:06 +000045
Mike Stump90a90432009-03-04 18:47:42 +000046class CodeGenModule;
John McCall6b5a61b2011-02-07 10:33:21 +000047class CGBlockInfo;
Mike Stumpd883d842009-03-04 15:35:22 +000048
Fariborz Jahanian3dac20a2012-10-26 01:13:38 +000049// Flags stored in __block variables.
50enum BlockByrefFlags {
51 BLOCK_BYREF_HAS_COPY_DISPOSE = (1 << 25), // compiler
52 BLOCK_BYREF_LAYOUT_MASK = (0xF << 28), // compiler
53 BLOCK_BYREF_LAYOUT_EXTENDED = (1 << 28),
54 BLOCK_BYREF_LAYOUT_NON_OBJECT = (2 << 28),
55 BLOCK_BYREF_LAYOUT_STRONG = (3 << 28),
Fariborz Jahanian7b5209c2012-10-26 20:33:59 +000056 BLOCK_BYREF_LAYOUT_WEAK = (4 << 28),
57 BLOCK_BYREF_LAYOUT_UNRETAINED = (5 << 28)
Fariborz Jahanian3dac20a2012-10-26 01:13:38 +000058};
59
Fariborz Jahanian40effbb2012-10-25 22:55:52 +000060enum BlockLiteralFlags {
John McCalld16c2cf2011-02-08 08:22:06 +000061 BLOCK_HAS_COPY_DISPOSE = (1 << 25),
62 BLOCK_HAS_CXX_OBJ = (1 << 26),
63 BLOCK_IS_GLOBAL = (1 << 28),
64 BLOCK_USE_STRET = (1 << 29),
Fariborz Jahaniana97d2ec2012-11-10 18:30:40 +000065 BLOCK_HAS_SIGNATURE = (1 << 30),
66 BLOCK_HAS_EXTENDED_LAYOUT = (1 << 31)
Mike Stumpd883d842009-03-04 15:35:22 +000067};
John McCalld16c2cf2011-02-08 08:22:06 +000068class BlockFlags {
69 uint32_t flags;
Mike Stumpd883d842009-03-04 15:35:22 +000070
Mike Stump2a998142009-03-04 18:17:45 +000071public:
Fariborz Jahanian3ca23d72012-11-14 17:15:51 +000072 BlockFlags(uint32_t flags) : flags(flags) {}
John McCalld16c2cf2011-02-08 08:22:06 +000073 BlockFlags() : flags(0) {}
Fariborz Jahanian40effbb2012-10-25 22:55:52 +000074 BlockFlags(BlockLiteralFlags flag) : flags(flag) {}
Fariborz Jahanian3ca23d72012-11-14 17:15:51 +000075 BlockFlags(BlockByrefFlags flag) : flags(flag) {}
76
John McCalld16c2cf2011-02-08 08:22:06 +000077 uint32_t getBitMask() const { return flags; }
78 bool empty() const { return flags == 0; }
Mike Stump2a998142009-03-04 18:17:45 +000079
John McCalld16c2cf2011-02-08 08:22:06 +000080 friend BlockFlags operator|(BlockFlags l, BlockFlags r) {
81 return BlockFlags(l.flags | r.flags);
82 }
83 friend BlockFlags &operator|=(BlockFlags &l, BlockFlags r) {
84 l.flags |= r.flags;
85 return l;
86 }
87 friend bool operator&(BlockFlags l, BlockFlags r) {
88 return (l.flags & r.flags);
Mike Stump2a998142009-03-04 18:17:45 +000089 }
Fariborz Jahanian3ca23d72012-11-14 17:15:51 +000090 bool operator==(BlockFlags r) {
91 return (flags == r.flags);
92 }
Mike Stumpd883d842009-03-04 15:35:22 +000093};
Fariborz Jahanian40effbb2012-10-25 22:55:52 +000094inline BlockFlags operator|(BlockLiteralFlags l, BlockLiteralFlags r) {
John McCalld16c2cf2011-02-08 08:22:06 +000095 return BlockFlags(l) | BlockFlags(r);
96}
Mike Stumpd883d842009-03-04 15:35:22 +000097
John McCalld16c2cf2011-02-08 08:22:06 +000098enum BlockFieldFlag_t {
99 BLOCK_FIELD_IS_OBJECT = 0x03, /* id, NSObject, __attribute__((NSObject)),
100 block, ... */
101 BLOCK_FIELD_IS_BLOCK = 0x07, /* a block variable */
Mike Stump797b6322009-03-05 01:23:13 +0000102
John McCalld16c2cf2011-02-08 08:22:06 +0000103 BLOCK_FIELD_IS_BYREF = 0x08, /* the on stack structure holding the __block
104 variable */
105 BLOCK_FIELD_IS_WEAK = 0x10, /* declared __weak, only used in byref copy
106 helpers */
John McCallf85e1932011-06-15 23:02:42 +0000107 BLOCK_FIELD_IS_ARC = 0x40, /* field has ARC-specific semantics */
John McCalld16c2cf2011-02-08 08:22:06 +0000108 BLOCK_BYREF_CALLER = 128, /* called from __block (byref) copy/dispose
Mike Stumpd883d842009-03-04 15:35:22 +0000109 support routines */
John McCalld16c2cf2011-02-08 08:22:06 +0000110 BLOCK_BYREF_CURRENT_MAX = 256
111};
112
113class BlockFieldFlags {
114 uint32_t flags;
115
116 BlockFieldFlags(uint32_t flags) : flags(flags) {}
117public:
118 BlockFieldFlags() : flags(0) {}
119 BlockFieldFlags(BlockFieldFlag_t flag) : flags(flag) {}
120
121 uint32_t getBitMask() const { return flags; }
122 bool empty() const { return flags == 0; }
123
124 /// Answers whether the flags indicate that this field is an object
125 /// or block pointer that requires _Block_object_assign/dispose.
126 bool isSpecialPointer() const { return flags & BLOCK_FIELD_IS_OBJECT; }
127
128 friend BlockFieldFlags operator|(BlockFieldFlags l, BlockFieldFlags r) {
129 return BlockFieldFlags(l.flags | r.flags);
130 }
131 friend BlockFieldFlags &operator|=(BlockFieldFlags &l, BlockFieldFlags r) {
132 l.flags |= r.flags;
133 return l;
134 }
135 friend bool operator&(BlockFieldFlags l, BlockFieldFlags r) {
136 return (l.flags & r.flags);
137 }
138};
139inline BlockFieldFlags operator|(BlockFieldFlag_t l, BlockFieldFlag_t r) {
140 return BlockFieldFlags(l) | BlockFieldFlags(r);
141}
142
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800143/// Information about the layout of a __block variable.
144class BlockByrefInfo {
145public:
146 llvm::StructType *Type;
147 unsigned FieldIndex;
148 CharUnits ByrefAlignment;
149 CharUnits FieldOffset;
150};
151
John McCalld16c2cf2011-02-08 08:22:06 +0000152/// CGBlockInfo - Information to generate a block literal.
153class CGBlockInfo {
154public:
155 /// Name - The name of the block, kindof.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000156 StringRef Name;
John McCalld16c2cf2011-02-08 08:22:06 +0000157
158 /// The field index of 'this' within the block, if there is one.
159 unsigned CXXThisIndex;
160
161 class Capture {
162 uintptr_t Data;
John McCall1a343eb2011-11-10 08:15:53 +0000163 EHScopeStack::stable_iterator Cleanup;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800164 CharUnits::QuantityType Offset;
John McCalld16c2cf2011-02-08 08:22:06 +0000165
166 public:
167 bool isIndex() const { return (Data & 1) != 0; }
168 bool isConstant() const { return !isIndex(); }
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800169
170 unsigned getIndex() const {
171 assert(isIndex());
172 return Data >> 1;
173 }
174 CharUnits getOffset() const {
175 assert(isIndex());
176 return CharUnits::fromQuantity(Offset);
John McCalld16c2cf2011-02-08 08:22:06 +0000177 }
John McCall1a343eb2011-11-10 08:15:53 +0000178 EHScopeStack::stable_iterator getCleanup() const {
179 assert(isIndex());
180 return Cleanup;
181 }
182 void setCleanup(EHScopeStack::stable_iterator cleanup) {
183 assert(isIndex());
184 Cleanup = cleanup;
185 }
John McCalld16c2cf2011-02-08 08:22:06 +0000186
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800187 llvm::Value *getConstant() const {
188 assert(isConstant());
189 return reinterpret_cast<llvm::Value*>(Data);
190 }
191
192 static Capture makeIndex(unsigned index, CharUnits offset) {
John McCalld16c2cf2011-02-08 08:22:06 +0000193 Capture v;
194 v.Data = (index << 1) | 1;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800195 v.Offset = offset.getQuantity();
John McCalld16c2cf2011-02-08 08:22:06 +0000196 return v;
197 }
198
199 static Capture makeConstant(llvm::Value *value) {
200 Capture v;
201 v.Data = reinterpret_cast<uintptr_t>(value);
202 return v;
203 }
Mike Stumpd883d842009-03-04 15:35:22 +0000204 };
Mike Stump3947de52009-03-04 18:57:26 +0000205
John McCalld16c2cf2011-02-08 08:22:06 +0000206 /// CanBeGlobal - True if the block can be global, i.e. it has
207 /// no non-constant captures.
208 bool CanBeGlobal : 1;
Mike Stump08920992009-03-07 02:35:30 +0000209
John McCalld16c2cf2011-02-08 08:22:06 +0000210 /// True if the block needs a custom copy or dispose function.
211 bool NeedsCopyDispose : 1;
Mike Stump45031c02009-03-06 02:29:21 +0000212
John McCalld16c2cf2011-02-08 08:22:06 +0000213 /// HasCXXObject - True if the block's custom copy/dispose functions
214 /// need to be run even in GC mode.
215 bool HasCXXObject : 1;
Mike Stump45031c02009-03-06 02:29:21 +0000216
John McCall64cd2322011-03-09 08:39:33 +0000217 /// UsesStret : True if the block uses an stret return. Mutable
218 /// because it gets set later in the block-creation process.
219 mutable bool UsesStret : 1;
Fariborz Jahanianf22ae652012-11-01 18:32:55 +0000220
221 /// HasCapturedVariableLayout : True if block has captured variables
222 /// and their layout meta-data has been generated.
223 bool HasCapturedVariableLayout : 1;
John McCall64cd2322011-03-09 08:39:33 +0000224
John McCall1a343eb2011-11-10 08:15:53 +0000225 /// The mapping of allocated indexes within the block.
226 llvm::DenseMap<const VarDecl*, Capture> Captures;
227
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800228 Address LocalAddress;
Chris Lattner2acc6e32011-07-18 04:24:23 +0000229 llvm::StructType *StructureType;
John McCall1a343eb2011-11-10 08:15:53 +0000230 const BlockDecl *Block;
231 const BlockExpr *BlockExpression;
John McCalld16c2cf2011-02-08 08:22:06 +0000232 CharUnits BlockSize;
233 CharUnits BlockAlign;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800234 CharUnits CXXThisOffset;
Fariborz Jahanianff685c52012-12-04 17:20:57 +0000235
236 // Offset of the gap caused by block header having a smaller
237 // alignment than the alignment of the block descriptor. This
238 // is the gap offset before the first capturued field.
239 CharUnits BlockHeaderForcedGapOffset;
240 // Gap size caused by aligning first field after block header.
241 // This could be zero if no forced alignment is required.
242 CharUnits BlockHeaderForcedGapSize;
John McCall6f103ba2011-11-10 10:43:54 +0000243
244 /// An instruction which dominates the full-expression that the
245 /// block is inside.
246 llvm::Instruction *DominatingIP;
247
248 /// The next block in the block-info chain. Invalid if this block
249 /// info is not part of the CGF's block-info chain, which is true
250 /// if it corresponds to a global block or a block whose expression
251 /// has been encountered.
John McCall1a343eb2011-11-10 08:15:53 +0000252 CGBlockInfo *NextBlockInfo;
Mike Stump3947de52009-03-04 18:57:26 +0000253
John McCalld16c2cf2011-02-08 08:22:06 +0000254 const Capture &getCapture(const VarDecl *var) const {
John McCall1a343eb2011-11-10 08:15:53 +0000255 return const_cast<CGBlockInfo*>(this)->getCapture(var);
256 }
257 Capture &getCapture(const VarDecl *var) {
258 llvm::DenseMap<const VarDecl*, Capture>::iterator
John McCalld16c2cf2011-02-08 08:22:06 +0000259 it = Captures.find(var);
260 assert(it != Captures.end() && "no entry for variable!");
261 return it->second;
262 }
263
John McCall1a343eb2011-11-10 08:15:53 +0000264 const BlockDecl *getBlockDecl() const { return Block; }
265 const BlockExpr *getBlockExpr() const {
266 assert(BlockExpression);
267 assert(BlockExpression->getBlockDecl() == Block);
268 return BlockExpression;
269 }
John McCalld16c2cf2011-02-08 08:22:06 +0000270
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000271 CGBlockInfo(const BlockDecl *blockDecl, StringRef Name);
Mike Stumpd883d842009-03-04 15:35:22 +0000272};
273
274} // end namespace CodeGen
275} // end namespace clang
276
277#endif