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" |
| 19 | #include "llvm/ADT/DenseMap.h" |
| 20 | #include "llvm/ADT/SmallVector.h" |
| 21 | #include "clang/Basic/TargetInfo.h" |
| 22 | #include "clang/AST/Expr.h" |
| 23 | #include "clang/AST/ExprCXX.h" |
| 24 | #include "clang/AST/ExprObjC.h" |
| 25 | |
| 26 | #include <vector> |
| 27 | #include <map> |
| 28 | |
| 29 | #include "CGBuilder.h" |
| 30 | #include "CGCall.h" |
| 31 | #include "CGValue.h" |
| 32 | |
| 33 | namespace llvm { |
| 34 | class Module; |
| 35 | class Constant; |
| 36 | class Function; |
| 37 | class GlobalValue; |
| 38 | class TargetData; |
| 39 | class FunctionType; |
| 40 | class Value; |
| 41 | } |
| 42 | |
Mike Stump | d883d84 | 2009-03-04 15:35:22 +0000 | [diff] [blame] | 43 | namespace clang { |
| 44 | |
| 45 | namespace CodeGen { |
Mike Stump | 90a9043 | 2009-03-04 18:47:42 +0000 | [diff] [blame] | 46 | class CodeGenModule; |
Mike Stump | d883d84 | 2009-03-04 15:35:22 +0000 | [diff] [blame] | 47 | |
| 48 | class BlockBase { |
| 49 | public: |
| 50 | enum { |
| 51 | BLOCK_NEEDS_FREE = (1 << 24), |
| 52 | BLOCK_HAS_COPY_DISPOSE = (1 << 25), |
| 53 | BLOCK_HAS_CXX_OBJ = (1 << 26), |
| 54 | BLOCK_IS_GC = (1 << 27), |
| 55 | BLOCK_IS_GLOBAL = (1 << 28), |
| 56 | BLOCK_HAS_DESCRIPTOR = (1 << 29) |
| 57 | }; |
| 58 | }; |
| 59 | |
| 60 | class BlockModule : public BlockBase { |
Mike Stump | 2a99814 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 61 | ASTContext &Context; |
| 62 | llvm::Module &TheModule; |
Mike Stump | 90a9043 | 2009-03-04 18:47:42 +0000 | [diff] [blame] | 63 | const llvm::TargetData &TheTargetData; |
Mike Stump | 2a99814 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 64 | CodeGenTypes &Types; |
Mike Stump | 90a9043 | 2009-03-04 18:47:42 +0000 | [diff] [blame] | 65 | CodeGenModule &CGM; |
Mike Stump | 2a99814 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 66 | |
| 67 | ASTContext &getContext() const { return Context; } |
| 68 | llvm::Module &getModule() const { return TheModule; } |
| 69 | CodeGenTypes &getTypes() { return Types; } |
Mike Stump | 90a9043 | 2009-03-04 18:47:42 +0000 | [diff] [blame] | 70 | const llvm::TargetData &getTargetData() const { return TheTargetData; } |
Mike Stump | 2a99814 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 71 | public: |
| 72 | llvm::Constant *getNSConcreteGlobalBlock(); |
| 73 | llvm::Constant *getNSConcreteStackBlock(); |
| 74 | int getGlobalUniqueCount() { return ++Block.GlobalUniqueCount; } |
| 75 | const llvm::Type *getBlockDescriptorType(); |
| 76 | |
| 77 | const llvm::Type *getGenericBlockLiteralType(); |
| 78 | const llvm::Type *getGenericExtendedBlockLiteralType(); |
| 79 | |
Mike Stump | 90a9043 | 2009-03-04 18:47:42 +0000 | [diff] [blame] | 80 | llvm::Constant *GetAddrOfGlobalBlock(const BlockExpr *BE, const char *); |
| 81 | |
Mike Stump | 2a99814 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 82 | /// NSConcreteGlobalBlock - Cached reference to the class pointer for global |
| 83 | /// blocks. |
| 84 | llvm::Constant *NSConcreteGlobalBlock; |
| 85 | |
| 86 | /// NSConcreteStackBlock - Cached reference to the class poinnter for stack |
| 87 | /// blocks. |
| 88 | llvm::Constant *NSConcreteStackBlock; |
| 89 | |
| 90 | const llvm::Type *BlockDescriptorType; |
| 91 | const llvm::Type *GenericBlockLiteralType; |
| 92 | const llvm::Type *GenericExtendedBlockLiteralType; |
| 93 | struct { |
| 94 | int GlobalUniqueCount; |
| 95 | } Block; |
| 96 | |
Mike Stump | ee09422 | 2009-03-06 06:12:24 +0000 | [diff] [blame] | 97 | llvm::Value *BlockObjectAssign; |
Mike Stump | 797b632 | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 98 | llvm::Value *BlockObjectDispose; |
| 99 | const llvm::Type *PtrToInt8Ty; |
| 100 | |
Mike Stump | 3899a7f | 2009-06-05 23:26:36 +0000 | [diff] [blame^] | 101 | std::map<uint64_t, llvm::Constant *> AssignCache; |
| 102 | std::map<uint64_t, llvm::Constant *> DestroyCache; |
| 103 | |
Mike Stump | 90a9043 | 2009-03-04 18:47:42 +0000 | [diff] [blame] | 104 | BlockModule(ASTContext &C, llvm::Module &M, const llvm::TargetData &TD, |
| 105 | CodeGenTypes &T, CodeGenModule &CodeGen) |
| 106 | : Context(C), TheModule(M), TheTargetData(TD), Types(T), |
| 107 | CGM(CodeGen), |
| 108 | NSConcreteGlobalBlock(0), NSConcreteStackBlock(0), BlockDescriptorType(0), |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 109 | GenericBlockLiteralType(0), GenericExtendedBlockLiteralType(0), |
| 110 | BlockObjectAssign(0), BlockObjectDispose(0) { |
Mike Stump | 2a99814 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 111 | Block.GlobalUniqueCount = 0; |
Mike Stump | 797b632 | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 112 | PtrToInt8Ty = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
Mike Stump | 2a99814 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 113 | } |
Mike Stump | d883d84 | 2009-03-04 15:35:22 +0000 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | class BlockFunction : public BlockBase { |
Mike Stump | 797b632 | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 117 | CodeGenModule &CGM; |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 118 | CodeGenFunction &CGF; |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 119 | ASTContext &getContext() const; |
Mike Stump | 797b632 | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 120 | |
Mike Stump | d883d84 | 2009-03-04 15:35:22 +0000 | [diff] [blame] | 121 | public: |
Mike Stump | 797b632 | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 122 | const llvm::Type *PtrToInt8Ty; |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 123 | struct HelperInfo { |
| 124 | int index; |
| 125 | int flag; |
| 126 | bool RequiresCopying; |
| 127 | }; |
| 128 | |
Mike Stump | 797b632 | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 129 | enum { |
Mike Stump | d883d84 | 2009-03-04 15:35:22 +0000 | [diff] [blame] | 130 | BLOCK_FIELD_IS_OBJECT = 3, /* id, NSObject, __attribute__((NSObject)), |
| 131 | block, ... */ |
| 132 | BLOCK_FIELD_IS_BLOCK = 7, /* a block variable */ |
| 133 | BLOCK_FIELD_IS_BYREF = 8, /* the on stack structure holding the __block |
| 134 | variable */ |
| 135 | BLOCK_FIELD_IS_WEAK = 16, /* declared __weak, only used in byref copy |
| 136 | helpers */ |
Mike Stump | 3899a7f | 2009-06-05 23:26:36 +0000 | [diff] [blame^] | 137 | BLOCK_BYREF_CALLER = 128, /* called from __block (byref) copy/dispose |
Mike Stump | d883d84 | 2009-03-04 15:35:22 +0000 | [diff] [blame] | 138 | support routines */ |
Mike Stump | 3899a7f | 2009-06-05 23:26:36 +0000 | [diff] [blame^] | 139 | BLOCK_BYREF_CURRENT_MAX = 256 |
Mike Stump | d883d84 | 2009-03-04 15:35:22 +0000 | [diff] [blame] | 140 | }; |
Mike Stump | 3947de5 | 2009-03-04 18:57:26 +0000 | [diff] [blame] | 141 | |
Mike Stump | ad75ab4 | 2009-03-04 19:03:44 +0000 | [diff] [blame] | 142 | /// BlockInfo - Information to generate a block literal. |
| 143 | struct BlockInfo { |
| 144 | /// BlockLiteralTy - The type of the block literal. |
| 145 | const llvm::Type *BlockLiteralTy; |
| 146 | |
Daniel Dunbar | 49f59ec | 2009-05-14 16:42:16 +0000 | [diff] [blame] | 147 | /// Name - the name of the function this block was created for, if any. |
Mike Stump | ad75ab4 | 2009-03-04 19:03:44 +0000 | [diff] [blame] | 148 | const char *Name; |
| 149 | |
| 150 | /// ByCopyDeclRefs - Variables from parent scopes that have been imported |
| 151 | /// into this block. |
| 152 | llvm::SmallVector<const BlockDeclRefExpr *, 8> ByCopyDeclRefs; |
| 153 | |
| 154 | // ByRefDeclRefs - __block variables from parent scopes that have been |
| 155 | // imported into this block. |
| 156 | llvm::SmallVector<const BlockDeclRefExpr *, 8> ByRefDeclRefs; |
| 157 | |
| 158 | BlockInfo(const llvm::Type *blt, const char *n) |
Daniel Dunbar | 49f59ec | 2009-05-14 16:42:16 +0000 | [diff] [blame] | 159 | : BlockLiteralTy(blt), Name(n) { |
| 160 | // Skip asm prefix, if any. |
| 161 | if (Name && Name[0] == '\01') |
| 162 | ++Name; |
| 163 | } |
Mike Stump | ad75ab4 | 2009-03-04 19:03:44 +0000 | [diff] [blame] | 164 | }; |
| 165 | |
Mike Stump | 3947de5 | 2009-03-04 18:57:26 +0000 | [diff] [blame] | 166 | CGBuilderTy &Builder; |
| 167 | |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 168 | BlockFunction(CodeGenModule &cgm, CodeGenFunction &cgf, CGBuilderTy &B); |
| 169 | |
| 170 | /// BlockOffset - The offset in bytes for the next allocation of an |
| 171 | /// imported block variable. |
| 172 | uint64_t BlockOffset; |
| 173 | /// BlockAlign - Maximal alignment needed for the Block expressed in bytes. |
| 174 | uint64_t BlockAlign; |
| 175 | |
| 176 | /// getBlockOffset - Allocate an offset for the ValueDecl from a |
| 177 | /// BlockDeclRefExpr in a block literal (BlockExpr). |
| 178 | uint64_t getBlockOffset(const BlockDeclRefExpr *E); |
| 179 | |
| 180 | /// BlockHasCopyDispose - True iff the block uses copy/dispose. |
| 181 | bool BlockHasCopyDispose; |
| 182 | |
| 183 | /// BlockDeclRefDecls - Decls from BlockDeclRefExprs in apperance order |
| 184 | /// in a block literal. Decls without names are used for padding. |
| 185 | llvm::SmallVector<const Expr *, 8> BlockDeclRefDecls; |
| 186 | |
| 187 | /// BlockDecls - Offsets for all Decls in BlockDeclRefExprs. |
| 188 | std::map<const Decl*, uint64_t> BlockDecls; |
Mike Stump | 3947de5 | 2009-03-04 18:57:26 +0000 | [diff] [blame] | 189 | |
Mike Stump | ad75ab4 | 2009-03-04 19:03:44 +0000 | [diff] [blame] | 190 | ImplicitParamDecl *BlockStructDecl; |
| 191 | ImplicitParamDecl *getBlockStructDecl() { return BlockStructDecl; } |
| 192 | |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 193 | llvm::Constant *GenerateCopyHelperFunction(bool, const llvm::StructType *, |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 194 | std::vector<HelperInfo> *); |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 195 | llvm::Constant *GenerateDestroyHelperFunction(bool, const llvm::StructType *, |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 196 | std::vector<HelperInfo> *); |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 197 | |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 198 | llvm::Constant *BuildCopyHelper(const llvm::StructType *, |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 199 | std::vector<HelperInfo> *); |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 200 | llvm::Constant *BuildDestroyHelper(const llvm::StructType *, |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 201 | std::vector<HelperInfo> *); |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 202 | |
Mike Stump | ee09422 | 2009-03-06 06:12:24 +0000 | [diff] [blame] | 203 | llvm::Constant *GeneratebyrefCopyHelperFunction(const llvm::Type *, int flag); |
Mike Stump | 1851b68 | 2009-03-06 04:53:30 +0000 | [diff] [blame] | 204 | llvm::Constant *GeneratebyrefDestroyHelperFunction(const llvm::Type *T, int); |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 205 | |
Mike Stump | 3899a7f | 2009-06-05 23:26:36 +0000 | [diff] [blame^] | 206 | llvm::Constant *BuildbyrefCopyHelper(const llvm::Type *T, int flag, |
| 207 | unsigned Align); |
| 208 | llvm::Constant *BuildbyrefDestroyHelper(const llvm::Type *T, int flag, |
| 209 | unsigned Align); |
Mike Stump | 3947de5 | 2009-03-04 18:57:26 +0000 | [diff] [blame] | 210 | |
Mike Stump | ee09422 | 2009-03-06 06:12:24 +0000 | [diff] [blame] | 211 | llvm::Value *getBlockObjectAssign(); |
Mike Stump | 797b632 | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 212 | llvm::Value *getBlockObjectDispose(); |
Mike Stump | 1851b68 | 2009-03-06 04:53:30 +0000 | [diff] [blame] | 213 | void BuildBlockRelease(llvm::Value *DeclPtr, int flag = BLOCK_FIELD_IS_BYREF); |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 214 | |
| 215 | bool BlockRequiresCopying(QualType Ty) { |
| 216 | if (Ty->isBlockPointerType()) |
| 217 | return true; |
| 218 | if (getContext().isObjCNSObjectType(Ty)) |
| 219 | return true; |
Mike Stump | c0c0ef0 | 2009-04-10 23:09:55 +0000 | [diff] [blame] | 220 | if (getContext().isObjCObjectPointerType(Ty)) |
| 221 | return true; |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 222 | return false; |
| 223 | } |
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 |