Mike Stump | d883d84 | 2009-03-04 15:35:22 +0000 | [diff] [blame] | 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 CLANG_CODEGEN_CGBLOCKS_H |
| 15 | #define CLANG_CODEGEN_CGBLOCKS_H |
| 16 | |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 17 | #include "CGBuilder.h" |
| 18 | #include "CGCall.h" |
| 19 | #include "CGValue.h" |
| 20 | #include "CodeGenFunction.h" |
Mike Stump | 2a99814 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 21 | #include "CodeGenTypes.h" |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 22 | #include "clang/AST/CharUnits.h" |
Mike Stump | 2a99814 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 23 | #include "clang/AST/Expr.h" |
| 24 | #include "clang/AST/ExprCXX.h" |
| 25 | #include "clang/AST/ExprObjC.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 26 | #include "clang/AST/Type.h" |
| 27 | #include "clang/Basic/TargetInfo.h" |
Chandler Carruth | 3b844ba | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 28 | #include "llvm/IR/Module.h" |
Mike Stump | 2a99814 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 29 | |
| 30 | namespace llvm { |
| 31 | class Module; |
| 32 | class Constant; |
| 33 | class Function; |
| 34 | class GlobalValue; |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 35 | class DataLayout; |
Mike Stump | 2a99814 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 36 | class FunctionType; |
John McCall | 3d3ec1c | 2010-04-21 10:05:39 +0000 | [diff] [blame] | 37 | class PointerType; |
Mike Stump | 2a99814 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 38 | class Value; |
Benjamin Kramer | f21efe9 | 2009-08-11 17:46:57 +0000 | [diff] [blame] | 39 | class LLVMContext; |
Mike Stump | 2a99814 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Mike Stump | d883d84 | 2009-03-04 15:35:22 +0000 | [diff] [blame] | 42 | namespace clang { |
| 43 | |
| 44 | namespace CodeGen { |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 45 | |
Mike Stump | 90a9043 | 2009-03-04 18:47:42 +0000 | [diff] [blame] | 46 | class CodeGenModule; |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 47 | class CGBlockInfo; |
Mike Stump | d883d84 | 2009-03-04 15:35:22 +0000 | [diff] [blame] | 48 | |
Fariborz Jahanian | 3dac20a | 2012-10-26 01:13:38 +0000 | [diff] [blame] | 49 | // Flags stored in __block variables. |
| 50 | enum 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 Jahanian | 7b5209c | 2012-10-26 20:33:59 +0000 | [diff] [blame] | 56 | BLOCK_BYREF_LAYOUT_WEAK = (4 << 28), |
| 57 | BLOCK_BYREF_LAYOUT_UNRETAINED = (5 << 28) |
Fariborz Jahanian | 3dac20a | 2012-10-26 01:13:38 +0000 | [diff] [blame] | 58 | }; |
| 59 | |
Fariborz Jahanian | 40effbb | 2012-10-25 22:55:52 +0000 | [diff] [blame] | 60 | enum BlockLiteralFlags { |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 61 | 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 Jahanian | a97d2ec | 2012-11-10 18:30:40 +0000 | [diff] [blame] | 65 | BLOCK_HAS_SIGNATURE = (1 << 30), |
| 66 | BLOCK_HAS_EXTENDED_LAYOUT = (1 << 31) |
Mike Stump | d883d84 | 2009-03-04 15:35:22 +0000 | [diff] [blame] | 67 | }; |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 68 | class BlockFlags { |
| 69 | uint32_t flags; |
Mike Stump | d883d84 | 2009-03-04 15:35:22 +0000 | [diff] [blame] | 70 | |
Mike Stump | 2a99814 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 71 | public: |
Fariborz Jahanian | 3ca23d7 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 72 | BlockFlags(uint32_t flags) : flags(flags) {} |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 73 | BlockFlags() : flags(0) {} |
Fariborz Jahanian | 40effbb | 2012-10-25 22:55:52 +0000 | [diff] [blame] | 74 | BlockFlags(BlockLiteralFlags flag) : flags(flag) {} |
Fariborz Jahanian | 3ca23d7 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 75 | BlockFlags(BlockByrefFlags flag) : flags(flag) {} |
| 76 | |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 77 | uint32_t getBitMask() const { return flags; } |
| 78 | bool empty() const { return flags == 0; } |
Mike Stump | 2a99814 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 79 | |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 80 | 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 Stump | 2a99814 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 89 | } |
Fariborz Jahanian | 3ca23d7 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 90 | bool operator==(BlockFlags r) { |
| 91 | return (flags == r.flags); |
| 92 | } |
Mike Stump | d883d84 | 2009-03-04 15:35:22 +0000 | [diff] [blame] | 93 | }; |
Fariborz Jahanian | 40effbb | 2012-10-25 22:55:52 +0000 | [diff] [blame] | 94 | inline BlockFlags operator|(BlockLiteralFlags l, BlockLiteralFlags r) { |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 95 | return BlockFlags(l) | BlockFlags(r); |
| 96 | } |
Mike Stump | d883d84 | 2009-03-04 15:35:22 +0000 | [diff] [blame] | 97 | |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 98 | enum BlockFieldFlag_t { |
| 99 | BLOCK_FIELD_IS_OBJECT = 0x03, /* id, NSObject, __attribute__((NSObject)), |
| 100 | block, ... */ |
| 101 | BLOCK_FIELD_IS_BLOCK = 0x07, /* a block variable */ |
Mike Stump | 797b632 | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 102 | |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 103 | 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 McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 107 | BLOCK_FIELD_IS_ARC = 0x40, /* field has ARC-specific semantics */ |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 108 | BLOCK_BYREF_CALLER = 128, /* called from __block (byref) copy/dispose |
Mike Stump | d883d84 | 2009-03-04 15:35:22 +0000 | [diff] [blame] | 109 | support routines */ |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 110 | BLOCK_BYREF_CURRENT_MAX = 256 |
| 111 | }; |
| 112 | |
| 113 | class BlockFieldFlags { |
| 114 | uint32_t flags; |
| 115 | |
| 116 | BlockFieldFlags(uint32_t flags) : flags(flags) {} |
| 117 | public: |
| 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 | }; |
| 139 | inline BlockFieldFlags operator|(BlockFieldFlag_t l, BlockFieldFlag_t r) { |
| 140 | return BlockFieldFlags(l) | BlockFieldFlags(r); |
| 141 | } |
| 142 | |
| 143 | /// CGBlockInfo - Information to generate a block literal. |
| 144 | class CGBlockInfo { |
| 145 | public: |
| 146 | /// Name - The name of the block, kindof. |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 147 | StringRef Name; |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 148 | |
| 149 | /// The field index of 'this' within the block, if there is one. |
| 150 | unsigned CXXThisIndex; |
| 151 | |
| 152 | class Capture { |
| 153 | uintptr_t Data; |
John McCall | 1a343eb | 2011-11-10 08:15:53 +0000 | [diff] [blame] | 154 | EHScopeStack::stable_iterator Cleanup; |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 155 | |
| 156 | public: |
| 157 | bool isIndex() const { return (Data & 1) != 0; } |
| 158 | bool isConstant() const { return !isIndex(); } |
| 159 | unsigned getIndex() const { assert(isIndex()); return Data >> 1; } |
| 160 | llvm::Value *getConstant() const { |
| 161 | assert(isConstant()); |
| 162 | return reinterpret_cast<llvm::Value*>(Data); |
| 163 | } |
John McCall | 1a343eb | 2011-11-10 08:15:53 +0000 | [diff] [blame] | 164 | EHScopeStack::stable_iterator getCleanup() const { |
| 165 | assert(isIndex()); |
| 166 | return Cleanup; |
| 167 | } |
| 168 | void setCleanup(EHScopeStack::stable_iterator cleanup) { |
| 169 | assert(isIndex()); |
| 170 | Cleanup = cleanup; |
| 171 | } |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 172 | |
| 173 | static Capture makeIndex(unsigned index) { |
| 174 | Capture v; |
| 175 | v.Data = (index << 1) | 1; |
| 176 | return v; |
| 177 | } |
| 178 | |
| 179 | static Capture makeConstant(llvm::Value *value) { |
| 180 | Capture v; |
| 181 | v.Data = reinterpret_cast<uintptr_t>(value); |
| 182 | return v; |
| 183 | } |
Mike Stump | d883d84 | 2009-03-04 15:35:22 +0000 | [diff] [blame] | 184 | }; |
Mike Stump | 3947de5 | 2009-03-04 18:57:26 +0000 | [diff] [blame] | 185 | |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 186 | /// CanBeGlobal - True if the block can be global, i.e. it has |
| 187 | /// no non-constant captures. |
| 188 | bool CanBeGlobal : 1; |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 189 | |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 190 | /// True if the block needs a custom copy or dispose function. |
| 191 | bool NeedsCopyDispose : 1; |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 192 | |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 193 | /// HasCXXObject - True if the block's custom copy/dispose functions |
| 194 | /// need to be run even in GC mode. |
| 195 | bool HasCXXObject : 1; |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 196 | |
John McCall | 64cd232 | 2011-03-09 08:39:33 +0000 | [diff] [blame] | 197 | /// UsesStret : True if the block uses an stret return. Mutable |
| 198 | /// because it gets set later in the block-creation process. |
| 199 | mutable bool UsesStret : 1; |
Fariborz Jahanian | f22ae65 | 2012-11-01 18:32:55 +0000 | [diff] [blame] | 200 | |
| 201 | /// HasCapturedVariableLayout : True if block has captured variables |
| 202 | /// and their layout meta-data has been generated. |
| 203 | bool HasCapturedVariableLayout : 1; |
John McCall | 64cd232 | 2011-03-09 08:39:33 +0000 | [diff] [blame] | 204 | |
John McCall | 1a343eb | 2011-11-10 08:15:53 +0000 | [diff] [blame] | 205 | /// The mapping of allocated indexes within the block. |
| 206 | llvm::DenseMap<const VarDecl*, Capture> Captures; |
| 207 | |
| 208 | llvm::AllocaInst *Address; |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 209 | llvm::StructType *StructureType; |
John McCall | 1a343eb | 2011-11-10 08:15:53 +0000 | [diff] [blame] | 210 | const BlockDecl *Block; |
| 211 | const BlockExpr *BlockExpression; |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 212 | CharUnits BlockSize; |
| 213 | CharUnits BlockAlign; |
Fariborz Jahanian | ff685c5 | 2012-12-04 17:20:57 +0000 | [diff] [blame] | 214 | |
| 215 | // Offset of the gap caused by block header having a smaller |
| 216 | // alignment than the alignment of the block descriptor. This |
| 217 | // is the gap offset before the first capturued field. |
| 218 | CharUnits BlockHeaderForcedGapOffset; |
| 219 | // Gap size caused by aligning first field after block header. |
| 220 | // This could be zero if no forced alignment is required. |
| 221 | CharUnits BlockHeaderForcedGapSize; |
John McCall | 6f103ba | 2011-11-10 10:43:54 +0000 | [diff] [blame] | 222 | |
| 223 | /// An instruction which dominates the full-expression that the |
| 224 | /// block is inside. |
| 225 | llvm::Instruction *DominatingIP; |
| 226 | |
| 227 | /// The next block in the block-info chain. Invalid if this block |
| 228 | /// info is not part of the CGF's block-info chain, which is true |
| 229 | /// if it corresponds to a global block or a block whose expression |
| 230 | /// has been encountered. |
John McCall | 1a343eb | 2011-11-10 08:15:53 +0000 | [diff] [blame] | 231 | CGBlockInfo *NextBlockInfo; |
Mike Stump | 3947de5 | 2009-03-04 18:57:26 +0000 | [diff] [blame] | 232 | |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 233 | const Capture &getCapture(const VarDecl *var) const { |
John McCall | 1a343eb | 2011-11-10 08:15:53 +0000 | [diff] [blame] | 234 | return const_cast<CGBlockInfo*>(this)->getCapture(var); |
| 235 | } |
| 236 | Capture &getCapture(const VarDecl *var) { |
| 237 | llvm::DenseMap<const VarDecl*, Capture>::iterator |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 238 | it = Captures.find(var); |
| 239 | assert(it != Captures.end() && "no entry for variable!"); |
| 240 | return it->second; |
| 241 | } |
| 242 | |
John McCall | 1a343eb | 2011-11-10 08:15:53 +0000 | [diff] [blame] | 243 | const BlockDecl *getBlockDecl() const { return Block; } |
| 244 | const BlockExpr *getBlockExpr() const { |
| 245 | assert(BlockExpression); |
| 246 | assert(BlockExpression->getBlockDecl() == Block); |
| 247 | return BlockExpression; |
| 248 | } |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 249 | |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 250 | CGBlockInfo(const BlockDecl *blockDecl, StringRef Name); |
Mike Stump | d883d84 | 2009-03-04 15:35:22 +0000 | [diff] [blame] | 251 | }; |
| 252 | |
| 253 | } // end namespace CodeGen |
| 254 | } // end namespace clang |
| 255 | |
| 256 | #endif |