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