1 //===-- 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 LLVM_CLANG_LIB_CODEGEN_CGBLOCKS_H
15 #define LLVM_CLANG_LIB_CODEGEN_CGBLOCKS_H
16 
17 #include "CGBuilder.h"
18 #include "CGCall.h"
19 #include "CGValue.h"
20 #include "CodeGenFunction.h"
21 #include "CodeGenTypes.h"
22 #include "clang/AST/CharUnits.h"
23 #include "clang/AST/Expr.h"
24 #include "clang/AST/ExprCXX.h"
25 #include "clang/AST/ExprObjC.h"
26 #include "clang/AST/Type.h"
27 #include "clang/Basic/TargetInfo.h"
28 
29 namespace llvm {
30 class Constant;
31 class Function;
32 class GlobalValue;
33 class DataLayout;
34 class FunctionType;
35 class PointerType;
36 class Value;
37 class LLVMContext;
38 }
39 
40 namespace clang {
41 namespace CodeGen {
42 
43 class CGBlockInfo;
44 
45 // Flags stored in __block variables.
46 enum BlockByrefFlags {
47   BLOCK_BYREF_HAS_COPY_DISPOSE         = (1   << 25), // compiler
48   BLOCK_BYREF_LAYOUT_MASK              = (0xF << 28), // compiler
49   BLOCK_BYREF_LAYOUT_EXTENDED          = (1   << 28),
50   BLOCK_BYREF_LAYOUT_NON_OBJECT        = (2   << 28),
51   BLOCK_BYREF_LAYOUT_STRONG            = (3   << 28),
52   BLOCK_BYREF_LAYOUT_WEAK              = (4   << 28),
53   BLOCK_BYREF_LAYOUT_UNRETAINED        = (5   << 28)
54 };
55 
56 enum BlockLiteralFlags {
57   BLOCK_HAS_COPY_DISPOSE =  (1 << 25),
58   BLOCK_HAS_CXX_OBJ =       (1 << 26),
59   BLOCK_IS_GLOBAL =         (1 << 28),
60   BLOCK_USE_STRET =         (1 << 29),
61   BLOCK_HAS_SIGNATURE  =    (1 << 30),
62   BLOCK_HAS_EXTENDED_LAYOUT = (1 << 31)
63 };
64 class BlockFlags {
65   uint32_t flags;
66 
67 public:
68   BlockFlags(uint32_t flags) : flags(flags) {}
69   BlockFlags() : flags(0) {}
70   BlockFlags(BlockLiteralFlags flag) : flags(flag) {}
71   BlockFlags(BlockByrefFlags flag) : flags(flag) {}
72 
73   uint32_t getBitMask() const { return flags; }
74   bool empty() const { return flags == 0; }
75 
76   friend BlockFlags operator|(BlockFlags l, BlockFlags r) {
77     return BlockFlags(l.flags | r.flags);
78   }
79   friend BlockFlags &operator|=(BlockFlags &l, BlockFlags r) {
80     l.flags |= r.flags;
81     return l;
82   }
83   friend bool operator&(BlockFlags l, BlockFlags r) {
84     return (l.flags & r.flags);
85   }
86   bool operator==(BlockFlags r) {
87     return (flags == r.flags);
88   }
89 };
90 inline BlockFlags operator|(BlockLiteralFlags l, BlockLiteralFlags r) {
91   return BlockFlags(l) | BlockFlags(r);
92 }
93 
94 enum BlockFieldFlag_t {
95   BLOCK_FIELD_IS_OBJECT   = 0x03,  /* id, NSObject, __attribute__((NSObject)),
96                                     block, ... */
97   BLOCK_FIELD_IS_BLOCK    = 0x07,  /* a block variable */
98 
99   BLOCK_FIELD_IS_BYREF    = 0x08,  /* the on stack structure holding the __block
100                                     variable */
101   BLOCK_FIELD_IS_WEAK     = 0x10,  /* declared __weak, only used in byref copy
102                                     helpers */
103   BLOCK_FIELD_IS_ARC      = 0x40,  /* field has ARC-specific semantics */
104   BLOCK_BYREF_CALLER      = 128,   /* called from __block (byref) copy/dispose
105                                       support routines */
106   BLOCK_BYREF_CURRENT_MAX = 256
107 };
108 
109 class BlockFieldFlags {
110   uint32_t flags;
111 
112   BlockFieldFlags(uint32_t flags) : flags(flags) {}
113 public:
114   BlockFieldFlags() : flags(0) {}
115   BlockFieldFlags(BlockFieldFlag_t flag) : flags(flag) {}
116 
117   uint32_t getBitMask() const { return flags; }
118   bool empty() const { return flags == 0; }
119 
120   /// Answers whether the flags indicate that this field is an object
121   /// or block pointer that requires _Block_object_assign/dispose.
122   bool isSpecialPointer() const { return flags & BLOCK_FIELD_IS_OBJECT; }
123 
124   friend BlockFieldFlags operator|(BlockFieldFlags l, BlockFieldFlags r) {
125     return BlockFieldFlags(l.flags | r.flags);
126   }
127   friend BlockFieldFlags &operator|=(BlockFieldFlags &l, BlockFieldFlags r) {
128     l.flags |= r.flags;
129     return l;
130   }
131   friend bool operator&(BlockFieldFlags l, BlockFieldFlags r) {
132     return (l.flags & r.flags);
133   }
134 };
135 inline BlockFieldFlags operator|(BlockFieldFlag_t l, BlockFieldFlag_t r) {
136   return BlockFieldFlags(l) | BlockFieldFlags(r);
137 }
138 
139 /// Information about the layout of a __block variable.
140 class BlockByrefInfo {
141 public:
142   llvm::StructType *Type;
143   unsigned FieldIndex;
144   CharUnits ByrefAlignment;
145   CharUnits FieldOffset;
146 };
147 
148 /// CGBlockInfo - Information to generate a block literal.
149 class CGBlockInfo {
150 public:
151   /// Name - The name of the block, kindof.
152   StringRef Name;
153 
154   /// The field index of 'this' within the block, if there is one.
155   unsigned CXXThisIndex;
156 
157   class Capture {
158     uintptr_t Data;
159     EHScopeStack::stable_iterator Cleanup;
160     CharUnits::QuantityType Offset;
161 
162   public:
163     bool isIndex() const { return (Data & 1) != 0; }
164     bool isConstant() const { return !isIndex(); }
165 
166     unsigned getIndex() const {
167       assert(isIndex());
168       return Data >> 1;
169     }
170     CharUnits getOffset() const {
171       assert(isIndex());
172       return CharUnits::fromQuantity(Offset);
173     }
174     EHScopeStack::stable_iterator getCleanup() const {
175       assert(isIndex());
176       return Cleanup;
177     }
178     void setCleanup(EHScopeStack::stable_iterator cleanup) {
179       assert(isIndex());
180       Cleanup = cleanup;
181     }
182 
183     llvm::Value *getConstant() const {
184       assert(isConstant());
185       return reinterpret_cast<llvm::Value*>(Data);
186     }
187 
188     static Capture makeIndex(unsigned index, CharUnits offset) {
189       Capture v;
190       v.Data = (index << 1) | 1;
191       v.Offset = offset.getQuantity();
192       return v;
193     }
194 
195     static Capture makeConstant(llvm::Value *value) {
196       Capture v;
197       v.Data = reinterpret_cast<uintptr_t>(value);
198       return v;
199     }
200   };
201 
202   /// CanBeGlobal - True if the block can be global, i.e. it has
203   /// no non-constant captures.
204   bool CanBeGlobal : 1;
205 
206   /// True if the block needs a custom copy or dispose function.
207   bool NeedsCopyDispose : 1;
208 
209   /// HasCXXObject - True if the block's custom copy/dispose functions
210   /// need to be run even in GC mode.
211   bool HasCXXObject : 1;
212 
213   /// UsesStret : True if the block uses an stret return.  Mutable
214   /// because it gets set later in the block-creation process.
215   mutable bool UsesStret : 1;
216 
217   /// HasCapturedVariableLayout : True if block has captured variables
218   /// and their layout meta-data has been generated.
219   bool HasCapturedVariableLayout : 1;
220 
221   /// The mapping of allocated indexes within the block.
222   llvm::DenseMap<const VarDecl*, Capture> Captures;
223 
224   Address LocalAddress;
225   llvm::StructType *StructureType;
226   const BlockDecl *Block;
227   const BlockExpr *BlockExpression;
228   CharUnits BlockSize;
229   CharUnits BlockAlign;
230   CharUnits CXXThisOffset;
231 
232   // Offset of the gap caused by block header having a smaller
233   // alignment than the alignment of the block descriptor. This
234   // is the gap offset before the first capturued field.
235   CharUnits BlockHeaderForcedGapOffset;
236   // Gap size caused by aligning first field after block header.
237   // This could be zero if no forced alignment is required.
238   CharUnits BlockHeaderForcedGapSize;
239 
240   /// An instruction which dominates the full-expression that the
241   /// block is inside.
242   llvm::Instruction *DominatingIP;
243 
244   /// The next block in the block-info chain.  Invalid if this block
245   /// info is not part of the CGF's block-info chain, which is true
246   /// if it corresponds to a global block or a block whose expression
247   /// has been encountered.
248   CGBlockInfo *NextBlockInfo;
249 
250   const Capture &getCapture(const VarDecl *var) const {
251     return const_cast<CGBlockInfo*>(this)->getCapture(var);
252   }
253   Capture &getCapture(const VarDecl *var) {
254     llvm::DenseMap<const VarDecl*, Capture>::iterator
255       it = Captures.find(var);
256     assert(it != Captures.end() && "no entry for variable!");
257     return it->second;
258   }
259 
260   const BlockDecl *getBlockDecl() const { return Block; }
261   const BlockExpr *getBlockExpr() const {
262     assert(BlockExpression);
263     assert(BlockExpression->getBlockDecl() == Block);
264     return BlockExpression;
265   }
266 
267   CGBlockInfo(const BlockDecl *blockDecl, StringRef Name);
268 };
269 
270 }  // end namespace CodeGen
271 }  // end namespace clang
272 
273 #endif
274