CGBlocks.h 7.8 KB

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