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