Mike Stump | 376e3c0 | 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 | 9543567 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 17 | #include "CodeGenTypes.h" |
| 18 | #include "clang/AST/Type.h" |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 19 | #include "llvm/Module.h" |
Mike Stump | 9543567 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SmallVector.h" |
| 21 | #include "clang/Basic/TargetInfo.h" |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 22 | #include "clang/AST/CharUnits.h" |
Mike Stump | 9543567 | 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 | 2188696 | 2010-04-21 10:05:39 +0000 | [diff] [blame] | 41 | class PointerType; |
Mike Stump | 9543567 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 42 | class Value; |
Benjamin Kramer | 9cd050a | 2009-08-11 17:46:57 +0000 | [diff] [blame] | 43 | class LLVMContext; |
Mike Stump | 9543567 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Mike Stump | 376e3c0 | 2009-03-04 15:35:22 +0000 | [diff] [blame] | 46 | namespace clang { |
| 47 | |
| 48 | namespace CodeGen { |
Mike Stump | 6c39666 | 2009-03-04 18:47:42 +0000 | [diff] [blame] | 49 | class CodeGenModule; |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 50 | class CGBlockInfo; |
Mike Stump | 376e3c0 | 2009-03-04 15:35:22 +0000 | [diff] [blame] | 51 | |
| 52 | class BlockBase { |
| 53 | public: |
| 54 | enum { |
Mike Stump | 376e3c0 | 2009-03-04 15:35:22 +0000 | [diff] [blame] | 55 | BLOCK_HAS_COPY_DISPOSE = (1 << 25), |
| 56 | BLOCK_HAS_CXX_OBJ = (1 << 26), |
Mike Stump | 376e3c0 | 2009-03-04 15:35:22 +0000 | [diff] [blame] | 57 | BLOCK_IS_GLOBAL = (1 << 28), |
Blaine Garst | f27ab71 | 2010-03-05 01:29:59 +0000 | [diff] [blame] | 58 | BLOCK_USE_STRET = (1 << 29), |
| 59 | BLOCK_HAS_SIGNATURE = (1 << 30) |
Mike Stump | 376e3c0 | 2009-03-04 15:35:22 +0000 | [diff] [blame] | 60 | }; |
| 61 | }; |
| 62 | |
Blaine Garst | f27ab71 | 2010-03-05 01:29:59 +0000 | [diff] [blame] | 63 | |
Mike Stump | 376e3c0 | 2009-03-04 15:35:22 +0000 | [diff] [blame] | 64 | class BlockModule : public BlockBase { |
Mike Stump | 9543567 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 65 | ASTContext &Context; |
| 66 | llvm::Module &TheModule; |
Mike Stump | 6c39666 | 2009-03-04 18:47:42 +0000 | [diff] [blame] | 67 | const llvm::TargetData &TheTargetData; |
Mike Stump | 9543567 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 68 | CodeGenTypes &Types; |
Mike Stump | 6c39666 | 2009-03-04 18:47:42 +0000 | [diff] [blame] | 69 | CodeGenModule &CGM; |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 70 | llvm::LLVMContext &VMContext; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 71 | |
Mike Stump | 9543567 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 72 | ASTContext &getContext() const { return Context; } |
| 73 | llvm::Module &getModule() const { return TheModule; } |
| 74 | CodeGenTypes &getTypes() { return Types; } |
Mike Stump | 6c39666 | 2009-03-04 18:47:42 +0000 | [diff] [blame] | 75 | const llvm::TargetData &getTargetData() const { return TheTargetData; } |
Mike Stump | 9543567 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 76 | public: |
Mike Stump | 9543567 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 77 | int getGlobalUniqueCount() { return ++Block.GlobalUniqueCount; } |
| 78 | const llvm::Type *getBlockDescriptorType(); |
| 79 | |
| 80 | const llvm::Type *getGenericBlockLiteralType(); |
Mike Stump | 9543567 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 81 | |
Mike Stump | 6c39666 | 2009-03-04 18:47:42 +0000 | [diff] [blame] | 82 | llvm::Constant *GetAddrOfGlobalBlock(const BlockExpr *BE, const char *); |
| 83 | |
Mike Stump | 9543567 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 84 | const llvm::Type *BlockDescriptorType; |
| 85 | const llvm::Type *GenericBlockLiteralType; |
Blaine Garst | 5421a83 | 2010-02-19 00:24:37 +0000 | [diff] [blame] | 86 | |
Mike Stump | 9543567 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 87 | struct { |
| 88 | int GlobalUniqueCount; |
| 89 | } Block; |
| 90 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 91 | const llvm::PointerType *PtrToInt8Ty; |
Mike Stump | 626aecc | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 92 | |
Mike Stump | cbc2bca | 2009-06-05 23:26:36 +0000 | [diff] [blame] | 93 | std::map<uint64_t, llvm::Constant *> AssignCache; |
| 94 | std::map<uint64_t, llvm::Constant *> DestroyCache; |
| 95 | |
Mike Stump | 6c39666 | 2009-03-04 18:47:42 +0000 | [diff] [blame] | 96 | BlockModule(ASTContext &C, llvm::Module &M, const llvm::TargetData &TD, |
| 97 | CodeGenTypes &T, CodeGenModule &CodeGen) |
| 98 | : Context(C), TheModule(M), TheTargetData(TD), Types(T), |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 99 | CGM(CodeGen), VMContext(M.getContext()), |
Daniel Dunbar | 900546d | 2010-07-16 00:00:15 +0000 | [diff] [blame] | 100 | BlockDescriptorType(0), GenericBlockLiteralType(0) { |
Mike Stump | 9543567 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 101 | Block.GlobalUniqueCount = 0; |
Benjamin Kramer | abd5b90 | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 102 | PtrToInt8Ty = llvm::Type::getInt8PtrTy(M.getContext()); |
Mike Stump | 9543567 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 103 | } |
Mike Stump | 376e3c0 | 2009-03-04 15:35:22 +0000 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | class BlockFunction : public BlockBase { |
Mike Stump | 626aecc | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 107 | CodeGenModule &CGM; |
Mike Stump | 4446dcf | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 108 | ASTContext &getContext() const; |
Mike Stump | 626aecc | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 109 | |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 110 | protected: |
| 111 | llvm::LLVMContext &VMContext; |
| 112 | |
Mike Stump | 376e3c0 | 2009-03-04 15:35:22 +0000 | [diff] [blame] | 113 | public: |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 114 | CodeGenFunction &CGF; |
| 115 | |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 116 | const CodeGen::CGBlockInfo *BlockInfo; |
| 117 | llvm::Value *BlockPointer; |
| 118 | |
John McCall | 2188696 | 2010-04-21 10:05:39 +0000 | [diff] [blame] | 119 | const llvm::PointerType *PtrToInt8Ty; |
Mike Stump | aeb0ffd | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 120 | struct HelperInfo { |
| 121 | int index; |
| 122 | int flag; |
Fariborz Jahanian | e988bda | 2010-11-13 21:53:34 +0000 | [diff] [blame] | 123 | const BlockDeclRefExpr *cxxvar_import; |
Mike Stump | aeb0ffd | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 124 | bool RequiresCopying; |
| 125 | }; |
| 126 | |
Mike Stump | 626aecc | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 127 | enum { |
Mike Stump | 376e3c0 | 2009-03-04 15:35:22 +0000 | [diff] [blame] | 128 | BLOCK_FIELD_IS_OBJECT = 3, /* id, NSObject, __attribute__((NSObject)), |
| 129 | block, ... */ |
| 130 | BLOCK_FIELD_IS_BLOCK = 7, /* a block variable */ |
| 131 | BLOCK_FIELD_IS_BYREF = 8, /* the on stack structure holding the __block |
| 132 | variable */ |
| 133 | BLOCK_FIELD_IS_WEAK = 16, /* declared __weak, only used in byref copy |
| 134 | helpers */ |
Mike Stump | cbc2bca | 2009-06-05 23:26:36 +0000 | [diff] [blame] | 135 | BLOCK_BYREF_CALLER = 128, /* called from __block (byref) copy/dispose |
Mike Stump | 376e3c0 | 2009-03-04 15:35:22 +0000 | [diff] [blame] | 136 | support routines */ |
Mike Stump | cbc2bca | 2009-06-05 23:26:36 +0000 | [diff] [blame] | 137 | BLOCK_BYREF_CURRENT_MAX = 256 |
Mike Stump | 376e3c0 | 2009-03-04 15:35:22 +0000 | [diff] [blame] | 138 | }; |
Mike Stump | 06acea8a | 2009-03-04 18:57:26 +0000 | [diff] [blame] | 139 | |
| 140 | CGBuilderTy &Builder; |
| 141 | |
Mike Stump | aeb0ffd | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 142 | BlockFunction(CodeGenModule &cgm, CodeGenFunction &cgf, CGBuilderTy &B); |
| 143 | |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 144 | llvm::Constant *GenerateCopyHelperFunction(const CGBlockInfo &blockInfo); |
| 145 | llvm::Constant *GenerateDestroyHelperFunction(const CGBlockInfo &blockInfo); |
Mike Stump | ee2a5ee | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 146 | |
Fariborz Jahanian | 5019809 | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 147 | llvm::Constant *GeneratebyrefCopyHelperFunction(const llvm::Type *, int flag, |
| 148 | const VarDecl *BD); |
| 149 | llvm::Constant *GeneratebyrefDestroyHelperFunction(const llvm::Type *T, |
| 150 | int flag, |
| 151 | const VarDecl *BD); |
Mike Stump | ee2a5ee | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 152 | |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 153 | llvm::Constant *BuildbyrefCopyHelper(const llvm::Type *T, uint32_t flags, |
Fariborz Jahanian | 5019809 | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 154 | unsigned Align, const VarDecl *BD); |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 155 | llvm::Constant *BuildbyrefDestroyHelper(const llvm::Type *T, uint32_t flags, |
Fariborz Jahanian | 5019809 | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 156 | unsigned Align, const VarDecl *BD); |
Mike Stump | 06acea8a | 2009-03-04 18:57:26 +0000 | [diff] [blame] | 157 | |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 158 | void BuildBlockRelease(llvm::Value *DeclPtr, uint32_t flags); |
Mike Stump | 376e3c0 | 2009-03-04 15:35:22 +0000 | [diff] [blame] | 159 | }; |
| 160 | |
| 161 | } // end namespace CodeGen |
| 162 | } // end namespace clang |
| 163 | |
| 164 | #endif |