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 | |
Mike Stump | 2a99814 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 17 | #include "CodeGenTypes.h" |
| 18 | #include "clang/AST/Type.h" |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 19 | #include "llvm/Module.h" |
Mike Stump | 2a99814 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 20 | #include "clang/Basic/TargetInfo.h" |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 21 | #include "clang/AST/CharUnits.h" |
Mike Stump | 2a99814 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 22 | #include "clang/AST/Expr.h" |
| 23 | #include "clang/AST/ExprCXX.h" |
| 24 | #include "clang/AST/ExprObjC.h" |
| 25 | |
John McCall | 1a343eb | 2011-11-10 08:15:53 +0000 | [diff] [blame] | 26 | #include "CodeGenFunction.h" |
Mike Stump | 2a99814 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 27 | #include "CGBuilder.h" |
| 28 | #include "CGCall.h" |
| 29 | #include "CGValue.h" |
| 30 | |
| 31 | namespace llvm { |
| 32 | class Module; |
| 33 | class Constant; |
| 34 | class Function; |
| 35 | class GlobalValue; |
| 36 | class TargetData; |
| 37 | class FunctionType; |
John McCall | 3d3ec1c | 2010-04-21 10:05:39 +0000 | [diff] [blame] | 38 | class PointerType; |
Mike Stump | 2a99814 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 39 | class Value; |
Benjamin Kramer | f21efe9 | 2009-08-11 17:46:57 +0000 | [diff] [blame] | 40 | class LLVMContext; |
Mike Stump | 2a99814 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Mike Stump | d883d84 | 2009-03-04 15:35:22 +0000 | [diff] [blame] | 43 | namespace clang { |
| 44 | |
| 45 | namespace CodeGen { |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 46 | |
Mike Stump | 90a9043 | 2009-03-04 18:47:42 +0000 | [diff] [blame] | 47 | class CodeGenModule; |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 48 | class CGBlockInfo; |
Mike Stump | d883d84 | 2009-03-04 15:35:22 +0000 | [diff] [blame] | 49 | |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 50 | enum BlockFlag_t { |
| 51 | BLOCK_HAS_COPY_DISPOSE = (1 << 25), |
| 52 | BLOCK_HAS_CXX_OBJ = (1 << 26), |
| 53 | BLOCK_IS_GLOBAL = (1 << 28), |
| 54 | BLOCK_USE_STRET = (1 << 29), |
| 55 | BLOCK_HAS_SIGNATURE = (1 << 30) |
Mike Stump | d883d84 | 2009-03-04 15:35:22 +0000 | [diff] [blame] | 56 | }; |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 57 | class BlockFlags { |
| 58 | uint32_t flags; |
Mike Stump | d883d84 | 2009-03-04 15:35:22 +0000 | [diff] [blame] | 59 | |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 60 | BlockFlags(uint32_t flags) : flags(flags) {} |
Mike Stump | 2a99814 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 61 | public: |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 62 | BlockFlags() : flags(0) {} |
| 63 | BlockFlags(BlockFlag_t flag) : flags(flag) {} |
Mike Stump | 2a99814 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 64 | |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 65 | uint32_t getBitMask() const { return flags; } |
| 66 | bool empty() const { return flags == 0; } |
Mike Stump | 2a99814 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 67 | |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 68 | friend BlockFlags operator|(BlockFlags l, BlockFlags r) { |
| 69 | return BlockFlags(l.flags | r.flags); |
| 70 | } |
| 71 | friend BlockFlags &operator|=(BlockFlags &l, BlockFlags r) { |
| 72 | l.flags |= r.flags; |
| 73 | return l; |
| 74 | } |
| 75 | friend bool operator&(BlockFlags l, BlockFlags r) { |
| 76 | return (l.flags & r.flags); |
Mike Stump | 2a99814 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 77 | } |
Mike Stump | d883d84 | 2009-03-04 15:35:22 +0000 | [diff] [blame] | 78 | }; |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 79 | inline BlockFlags operator|(BlockFlag_t l, BlockFlag_t r) { |
| 80 | return BlockFlags(l) | BlockFlags(r); |
| 81 | } |
Mike Stump | d883d84 | 2009-03-04 15:35:22 +0000 | [diff] [blame] | 82 | |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 83 | enum BlockFieldFlag_t { |
| 84 | BLOCK_FIELD_IS_OBJECT = 0x03, /* id, NSObject, __attribute__((NSObject)), |
| 85 | block, ... */ |
| 86 | BLOCK_FIELD_IS_BLOCK = 0x07, /* a block variable */ |
Mike Stump | 797b632 | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 87 | |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 88 | BLOCK_FIELD_IS_BYREF = 0x08, /* the on stack structure holding the __block |
| 89 | variable */ |
| 90 | BLOCK_FIELD_IS_WEAK = 0x10, /* declared __weak, only used in byref copy |
| 91 | helpers */ |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 92 | BLOCK_FIELD_IS_ARC = 0x40, /* field has ARC-specific semantics */ |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 93 | BLOCK_BYREF_CALLER = 128, /* called from __block (byref) copy/dispose |
Mike Stump | d883d84 | 2009-03-04 15:35:22 +0000 | [diff] [blame] | 94 | support routines */ |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 95 | BLOCK_BYREF_CURRENT_MAX = 256 |
| 96 | }; |
| 97 | |
| 98 | class BlockFieldFlags { |
| 99 | uint32_t flags; |
| 100 | |
| 101 | BlockFieldFlags(uint32_t flags) : flags(flags) {} |
| 102 | public: |
| 103 | BlockFieldFlags() : flags(0) {} |
| 104 | BlockFieldFlags(BlockFieldFlag_t flag) : flags(flag) {} |
| 105 | |
| 106 | uint32_t getBitMask() const { return flags; } |
| 107 | bool empty() const { return flags == 0; } |
| 108 | |
| 109 | /// Answers whether the flags indicate that this field is an object |
| 110 | /// or block pointer that requires _Block_object_assign/dispose. |
| 111 | bool isSpecialPointer() const { return flags & BLOCK_FIELD_IS_OBJECT; } |
| 112 | |
| 113 | friend BlockFieldFlags operator|(BlockFieldFlags l, BlockFieldFlags r) { |
| 114 | return BlockFieldFlags(l.flags | r.flags); |
| 115 | } |
| 116 | friend BlockFieldFlags &operator|=(BlockFieldFlags &l, BlockFieldFlags r) { |
| 117 | l.flags |= r.flags; |
| 118 | return l; |
| 119 | } |
| 120 | friend bool operator&(BlockFieldFlags l, BlockFieldFlags r) { |
| 121 | return (l.flags & r.flags); |
| 122 | } |
| 123 | }; |
| 124 | inline BlockFieldFlags operator|(BlockFieldFlag_t l, BlockFieldFlag_t r) { |
| 125 | return BlockFieldFlags(l) | BlockFieldFlags(r); |
| 126 | } |
| 127 | |
| 128 | /// CGBlockInfo - Information to generate a block literal. |
| 129 | class CGBlockInfo { |
| 130 | public: |
| 131 | /// Name - The name of the block, kindof. |
John McCall | 1a343eb | 2011-11-10 08:15:53 +0000 | [diff] [blame] | 132 | llvm::StringRef Name; |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 133 | |
| 134 | /// The field index of 'this' within the block, if there is one. |
| 135 | unsigned CXXThisIndex; |
| 136 | |
| 137 | class Capture { |
| 138 | uintptr_t Data; |
John McCall | 1a343eb | 2011-11-10 08:15:53 +0000 | [diff] [blame] | 139 | EHScopeStack::stable_iterator Cleanup; |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 140 | |
| 141 | public: |
| 142 | bool isIndex() const { return (Data & 1) != 0; } |
| 143 | bool isConstant() const { return !isIndex(); } |
| 144 | unsigned getIndex() const { assert(isIndex()); return Data >> 1; } |
| 145 | llvm::Value *getConstant() const { |
| 146 | assert(isConstant()); |
| 147 | return reinterpret_cast<llvm::Value*>(Data); |
| 148 | } |
John McCall | 1a343eb | 2011-11-10 08:15:53 +0000 | [diff] [blame] | 149 | EHScopeStack::stable_iterator getCleanup() const { |
| 150 | assert(isIndex()); |
| 151 | return Cleanup; |
| 152 | } |
| 153 | void setCleanup(EHScopeStack::stable_iterator cleanup) { |
| 154 | assert(isIndex()); |
| 155 | Cleanup = cleanup; |
| 156 | } |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 157 | |
| 158 | static Capture makeIndex(unsigned index) { |
| 159 | Capture v; |
| 160 | v.Data = (index << 1) | 1; |
| 161 | return v; |
| 162 | } |
| 163 | |
| 164 | static Capture makeConstant(llvm::Value *value) { |
| 165 | Capture v; |
| 166 | v.Data = reinterpret_cast<uintptr_t>(value); |
| 167 | return v; |
| 168 | } |
Mike Stump | d883d84 | 2009-03-04 15:35:22 +0000 | [diff] [blame] | 169 | }; |
Mike Stump | 3947de5 | 2009-03-04 18:57:26 +0000 | [diff] [blame] | 170 | |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 171 | /// CanBeGlobal - True if the block can be global, i.e. it has |
| 172 | /// no non-constant captures. |
| 173 | bool CanBeGlobal : 1; |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 174 | |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 175 | /// True if the block needs a custom copy or dispose function. |
| 176 | bool NeedsCopyDispose : 1; |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 177 | |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 178 | /// HasCXXObject - True if the block's custom copy/dispose functions |
| 179 | /// need to be run even in GC mode. |
| 180 | bool HasCXXObject : 1; |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 181 | |
John McCall | 64cd232 | 2011-03-09 08:39:33 +0000 | [diff] [blame] | 182 | /// UsesStret : True if the block uses an stret return. Mutable |
| 183 | /// because it gets set later in the block-creation process. |
| 184 | mutable bool UsesStret : 1; |
| 185 | |
John McCall | 1a343eb | 2011-11-10 08:15:53 +0000 | [diff] [blame] | 186 | /// The mapping of allocated indexes within the block. |
| 187 | llvm::DenseMap<const VarDecl*, Capture> Captures; |
| 188 | |
| 189 | llvm::AllocaInst *Address; |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 190 | llvm::StructType *StructureType; |
John McCall | 1a343eb | 2011-11-10 08:15:53 +0000 | [diff] [blame] | 191 | const BlockDecl *Block; |
| 192 | const BlockExpr *BlockExpression; |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 193 | CharUnits BlockSize; |
| 194 | CharUnits BlockAlign; |
John McCall | 6f103ba | 2011-11-10 10:43:54 +0000 | [diff] [blame] | 195 | |
| 196 | /// An instruction which dominates the full-expression that the |
| 197 | /// block is inside. |
| 198 | llvm::Instruction *DominatingIP; |
| 199 | |
| 200 | /// The next block in the block-info chain. Invalid if this block |
| 201 | /// info is not part of the CGF's block-info chain, which is true |
| 202 | /// if it corresponds to a global block or a block whose expression |
| 203 | /// has been encountered. |
John McCall | 1a343eb | 2011-11-10 08:15:53 +0000 | [diff] [blame] | 204 | CGBlockInfo *NextBlockInfo; |
Mike Stump | 3947de5 | 2009-03-04 18:57:26 +0000 | [diff] [blame] | 205 | |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 206 | const Capture &getCapture(const VarDecl *var) const { |
John McCall | 1a343eb | 2011-11-10 08:15:53 +0000 | [diff] [blame] | 207 | return const_cast<CGBlockInfo*>(this)->getCapture(var); |
| 208 | } |
| 209 | Capture &getCapture(const VarDecl *var) { |
| 210 | llvm::DenseMap<const VarDecl*, Capture>::iterator |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 211 | it = Captures.find(var); |
| 212 | assert(it != Captures.end() && "no entry for variable!"); |
| 213 | return it->second; |
| 214 | } |
| 215 | |
John McCall | 1a343eb | 2011-11-10 08:15:53 +0000 | [diff] [blame] | 216 | const BlockDecl *getBlockDecl() const { return Block; } |
| 217 | const BlockExpr *getBlockExpr() const { |
| 218 | assert(BlockExpression); |
| 219 | assert(BlockExpression->getBlockDecl() == Block); |
| 220 | return BlockExpression; |
| 221 | } |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 222 | |
John McCall | 1a343eb | 2011-11-10 08:15:53 +0000 | [diff] [blame] | 223 | CGBlockInfo(const BlockDecl *blockDecl, llvm::StringRef Name); |
Mike Stump | d883d84 | 2009-03-04 15:35:22 +0000 | [diff] [blame] | 224 | }; |
| 225 | |
| 226 | } // end namespace CodeGen |
| 227 | } // end namespace clang |
| 228 | |
| 229 | #endif |