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