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 "llvm/ADT/SmallVector.h" |
| 21 | #include "clang/Basic/TargetInfo.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" |
| 26 | |
| 27 | #include <vector> |
| 28 | #include <map> |
| 29 | |
| 30 | #include "CGBuilder.h" |
| 31 | #include "CGCall.h" |
| 32 | #include "CGValue.h" |
| 33 | |
| 34 | namespace llvm { |
| 35 | class Module; |
| 36 | class Constant; |
| 37 | class Function; |
| 38 | class GlobalValue; |
| 39 | class TargetData; |
| 40 | class FunctionType; |
John McCall | 3d3ec1c | 2010-04-21 10:05:39 +0000 | [diff] [blame] | 41 | class PointerType; |
Mike Stump | 2a99814 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 42 | class Value; |
Benjamin Kramer | f21efe9 | 2009-08-11 17:46:57 +0000 | [diff] [blame] | 43 | class LLVMContext; |
Mike Stump | 2a99814 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Mike Stump | d883d84 | 2009-03-04 15:35:22 +0000 | [diff] [blame] | 46 | namespace clang { |
| 47 | |
| 48 | namespace CodeGen { |
Mike Stump | 90a9043 | 2009-03-04 18:47:42 +0000 | [diff] [blame] | 49 | class CodeGenModule; |
Mike Stump | d883d84 | 2009-03-04 15:35:22 +0000 | [diff] [blame] | 50 | |
| 51 | class BlockBase { |
| 52 | public: |
| 53 | enum { |
Mike Stump | d883d84 | 2009-03-04 15:35:22 +0000 | [diff] [blame] | 54 | BLOCK_HAS_COPY_DISPOSE = (1 << 25), |
| 55 | BLOCK_HAS_CXX_OBJ = (1 << 26), |
Mike Stump | d883d84 | 2009-03-04 15:35:22 +0000 | [diff] [blame] | 56 | BLOCK_IS_GLOBAL = (1 << 28), |
Blaine Garst | a36e223 | 2010-03-05 01:29:59 +0000 | [diff] [blame] | 57 | BLOCK_USE_STRET = (1 << 29), |
| 58 | BLOCK_HAS_SIGNATURE = (1 << 30) |
Mike Stump | d883d84 | 2009-03-04 15:35:22 +0000 | [diff] [blame] | 59 | }; |
| 60 | }; |
| 61 | |
Blaine Garst | a36e223 | 2010-03-05 01:29:59 +0000 | [diff] [blame] | 62 | |
Mike Stump | d883d84 | 2009-03-04 15:35:22 +0000 | [diff] [blame] | 63 | class BlockModule : public BlockBase { |
Mike Stump | 2a99814 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 64 | ASTContext &Context; |
| 65 | llvm::Module &TheModule; |
Mike Stump | 90a9043 | 2009-03-04 18:47:42 +0000 | [diff] [blame] | 66 | const llvm::TargetData &TheTargetData; |
Mike Stump | 2a99814 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 67 | CodeGenTypes &Types; |
Mike Stump | 90a9043 | 2009-03-04 18:47:42 +0000 | [diff] [blame] | 68 | CodeGenModule &CGM; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 69 | llvm::LLVMContext &VMContext; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 70 | |
Mike Stump | 2a99814 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 71 | ASTContext &getContext() const { return Context; } |
| 72 | llvm::Module &getModule() const { return TheModule; } |
| 73 | CodeGenTypes &getTypes() { return Types; } |
Mike Stump | 90a9043 | 2009-03-04 18:47:42 +0000 | [diff] [blame] | 74 | const llvm::TargetData &getTargetData() const { return TheTargetData; } |
Mike Stump | 2a99814 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 75 | public: |
Mike Stump | 2a99814 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 76 | int getGlobalUniqueCount() { return ++Block.GlobalUniqueCount; } |
| 77 | const llvm::Type *getBlockDescriptorType(); |
| 78 | |
| 79 | const llvm::Type *getGenericBlockLiteralType(); |
Mike Stump | 2a99814 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 80 | |
Mike Stump | 90a9043 | 2009-03-04 18:47:42 +0000 | [diff] [blame] | 81 | llvm::Constant *GetAddrOfGlobalBlock(const BlockExpr *BE, const char *); |
| 82 | |
Mike Stump | 2a99814 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 83 | const llvm::Type *BlockDescriptorType; |
| 84 | const llvm::Type *GenericBlockLiteralType; |
Blaine Garst | c0ea885 | 2010-02-19 00:24:37 +0000 | [diff] [blame] | 85 | |
Mike Stump | 2a99814 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 86 | struct { |
| 87 | int GlobalUniqueCount; |
| 88 | } Block; |
| 89 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 90 | const llvm::PointerType *PtrToInt8Ty; |
Mike Stump | 797b632 | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 91 | |
Mike Stump | 3899a7f | 2009-06-05 23:26:36 +0000 | [diff] [blame] | 92 | std::map<uint64_t, llvm::Constant *> AssignCache; |
| 93 | std::map<uint64_t, llvm::Constant *> DestroyCache; |
| 94 | |
Mike Stump | 90a9043 | 2009-03-04 18:47:42 +0000 | [diff] [blame] | 95 | BlockModule(ASTContext &C, llvm::Module &M, const llvm::TargetData &TD, |
| 96 | CodeGenTypes &T, CodeGenModule &CodeGen) |
| 97 | : Context(C), TheModule(M), TheTargetData(TD), Types(T), |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 98 | CGM(CodeGen), VMContext(M.getContext()), |
Daniel Dunbar | 673431a | 2010-07-16 00:00:15 +0000 | [diff] [blame] | 99 | BlockDescriptorType(0), GenericBlockLiteralType(0) { |
Mike Stump | 2a99814 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 100 | Block.GlobalUniqueCount = 0; |
Benjamin Kramer | 3c0ef8c | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 101 | PtrToInt8Ty = llvm::Type::getInt8PtrTy(M.getContext()); |
Mike Stump | 2a99814 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 102 | } |
Mike Stump | 39605b4 | 2009-09-22 02:12:52 +0000 | [diff] [blame] | 103 | |
Mike Stump | bf5fd78 | 2009-10-21 18:23:01 +0000 | [diff] [blame] | 104 | bool BlockRequiresCopying(QualType Ty) |
| 105 | { return getContext().BlockRequiresCopying(Ty); } |
Mike Stump | d883d84 | 2009-03-04 15:35:22 +0000 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | class BlockFunction : public BlockBase { |
Mike Stump | 797b632 | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 109 | CodeGenModule &CGM; |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 110 | ASTContext &getContext() const; |
Mike Stump | 797b632 | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 111 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 112 | protected: |
| 113 | llvm::LLVMContext &VMContext; |
| 114 | |
Mike Stump | d883d84 | 2009-03-04 15:35:22 +0000 | [diff] [blame] | 115 | public: |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 116 | CodeGenFunction &CGF; |
| 117 | |
John McCall | 3d3ec1c | 2010-04-21 10:05:39 +0000 | [diff] [blame] | 118 | const llvm::PointerType *PtrToInt8Ty; |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 119 | struct HelperInfo { |
| 120 | int index; |
| 121 | int flag; |
| 122 | bool RequiresCopying; |
| 123 | }; |
| 124 | |
Mike Stump | 797b632 | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 125 | enum { |
Mike Stump | d883d84 | 2009-03-04 15:35:22 +0000 | [diff] [blame] | 126 | BLOCK_FIELD_IS_OBJECT = 3, /* id, NSObject, __attribute__((NSObject)), |
| 127 | block, ... */ |
| 128 | BLOCK_FIELD_IS_BLOCK = 7, /* a block variable */ |
| 129 | BLOCK_FIELD_IS_BYREF = 8, /* the on stack structure holding the __block |
| 130 | variable */ |
| 131 | BLOCK_FIELD_IS_WEAK = 16, /* declared __weak, only used in byref copy |
| 132 | helpers */ |
Mike Stump | 3899a7f | 2009-06-05 23:26:36 +0000 | [diff] [blame] | 133 | BLOCK_BYREF_CALLER = 128, /* called from __block (byref) copy/dispose |
Mike Stump | d883d84 | 2009-03-04 15:35:22 +0000 | [diff] [blame] | 134 | support routines */ |
Mike Stump | 3899a7f | 2009-06-05 23:26:36 +0000 | [diff] [blame] | 135 | BLOCK_BYREF_CURRENT_MAX = 256 |
Mike Stump | d883d84 | 2009-03-04 15:35:22 +0000 | [diff] [blame] | 136 | }; |
Mike Stump | 3947de5 | 2009-03-04 18:57:26 +0000 | [diff] [blame] | 137 | |
| 138 | CGBuilderTy &Builder; |
| 139 | |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 140 | BlockFunction(CodeGenModule &cgm, CodeGenFunction &cgf, CGBuilderTy &B); |
| 141 | |
| 142 | /// BlockOffset - The offset in bytes for the next allocation of an |
| 143 | /// imported block variable. |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 144 | CharUnits BlockOffset; |
Ken Dyck | 30a8a27 | 2010-01-26 19:13:33 +0000 | [diff] [blame] | 145 | /// BlockAlign - Maximal alignment needed for the Block expressed in |
| 146 | /// characters. |
| 147 | CharUnits BlockAlign; |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 148 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 149 | /// getBlockOffset - Allocate a location within the block's storage |
| 150 | /// for a value with the given size and alignment requirements. |
| 151 | CharUnits getBlockOffset(CharUnits Size, CharUnits Align); |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 152 | |
| 153 | /// BlockHasCopyDispose - True iff the block uses copy/dispose. |
| 154 | bool BlockHasCopyDispose; |
| 155 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 156 | /// BlockLayout - The layout of the block's storage, represented as |
| 157 | /// a sequence of expressions which require such storage. The |
| 158 | /// expressions can be: |
| 159 | /// - a BlockDeclRefExpr, indicating that the given declaration |
| 160 | /// from an enclosing scope is needed by the block; |
| 161 | /// - a DeclRefExpr, which always wraps an anonymous VarDecl with |
| 162 | /// array type, used to insert padding into the block; or |
| 163 | /// - a CXXThisExpr, indicating that the C++ 'this' value should |
| 164 | /// propagate from the parent to the block. |
| 165 | /// This is a really silly representation. |
| 166 | llvm::SmallVector<const Expr *, 8> BlockLayout; |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 167 | |
| 168 | /// BlockDecls - Offsets for all Decls in BlockDeclRefExprs. |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 169 | llvm::DenseMap<const Decl*, CharUnits> BlockDecls; |
Fariborz Jahanian | df8b8ea | 2010-06-04 16:10:00 +0000 | [diff] [blame] | 170 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 171 | /// BlockCXXThisOffset - The offset of the C++ 'this' value within |
| 172 | /// the block structure. |
| 173 | CharUnits BlockCXXThisOffset; |
Mike Stump | 3947de5 | 2009-03-04 18:57:26 +0000 | [diff] [blame] | 174 | |
Mike Stump | ad75ab4 | 2009-03-04 19:03:44 +0000 | [diff] [blame] | 175 | ImplicitParamDecl *BlockStructDecl; |
| 176 | ImplicitParamDecl *getBlockStructDecl() { return BlockStructDecl; } |
| 177 | |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 178 | llvm::Constant *GenerateCopyHelperFunction(bool, const llvm::StructType *, |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 179 | std::vector<HelperInfo> *); |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 180 | llvm::Constant *GenerateDestroyHelperFunction(bool, const llvm::StructType *, |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 181 | std::vector<HelperInfo> *); |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 182 | |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 183 | llvm::Constant *BuildCopyHelper(const llvm::StructType *, |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 184 | std::vector<HelperInfo> *); |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 185 | llvm::Constant *BuildDestroyHelper(const llvm::StructType *, |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 186 | std::vector<HelperInfo> *); |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 187 | |
Mike Stump | ee09422 | 2009-03-06 06:12:24 +0000 | [diff] [blame] | 188 | llvm::Constant *GeneratebyrefCopyHelperFunction(const llvm::Type *, int flag); |
Mike Stump | 1851b68 | 2009-03-06 04:53:30 +0000 | [diff] [blame] | 189 | llvm::Constant *GeneratebyrefDestroyHelperFunction(const llvm::Type *T, int); |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 190 | |
Mike Stump | 3899a7f | 2009-06-05 23:26:36 +0000 | [diff] [blame] | 191 | llvm::Constant *BuildbyrefCopyHelper(const llvm::Type *T, int flag, |
| 192 | unsigned Align); |
| 193 | llvm::Constant *BuildbyrefDestroyHelper(const llvm::Type *T, int flag, |
| 194 | unsigned Align); |
Mike Stump | 3947de5 | 2009-03-04 18:57:26 +0000 | [diff] [blame] | 195 | |
Mike Stump | 1851b68 | 2009-03-06 04:53:30 +0000 | [diff] [blame] | 196 | void BuildBlockRelease(llvm::Value *DeclPtr, int flag = BLOCK_FIELD_IS_BYREF); |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 197 | |
Mike Stump | bf5fd78 | 2009-10-21 18:23:01 +0000 | [diff] [blame] | 198 | bool BlockRequiresCopying(QualType Ty) |
| 199 | { return getContext().BlockRequiresCopying(Ty); } |
Mike Stump | d883d84 | 2009-03-04 15:35:22 +0000 | [diff] [blame] | 200 | }; |
| 201 | |
| 202 | } // end namespace CodeGen |
| 203 | } // end namespace clang |
| 204 | |
| 205 | #endif |