Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 1 | //===--- CGBlocks.cpp - Emit LLVM Code for declarations -------------------===// |
| 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 contains code to emit blocks. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "CodeGenFunction.h" |
| 15 | #include "CodeGenModule.h" |
Mike Stump | 6cc88f7 | 2009-03-20 21:53:12 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 17 | #include "llvm/Module.h" |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 18 | #include "llvm/Target/TargetData.h" |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 19 | #include <algorithm> |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 20 | using namespace clang; |
| 21 | using namespace CodeGen; |
| 22 | |
Mike Stump | cf62d39 | 2009-03-06 18:42:23 +0000 | [diff] [blame] | 23 | llvm::Constant *CodeGenFunction:: |
Mike Stump | a803b0e | 2009-03-25 17:58:24 +0000 | [diff] [blame] | 24 | BuildDescriptorBlockDecl(bool BlockHasCopyDispose, uint64_t Size, |
| 25 | const llvm::StructType* Ty, |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 26 | std::vector<HelperInfo> *NoteForHelper) { |
Mike Stump | 56129b1 | 2009-02-13 16:55:51 +0000 | [diff] [blame] | 27 | const llvm::Type *UnsignedLongTy |
| 28 | = CGM.getTypes().ConvertType(getContext().UnsignedLongTy); |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 29 | llvm::Constant *C; |
| 30 | std::vector<llvm::Constant*> Elts; |
| 31 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 32 | llvm::LLVMContext &VMContext = CGM.getLLVMContext(); |
| 33 | |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 34 | // reserved |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 35 | C = VMContext.getConstantInt(UnsignedLongTy, 0); |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 36 | Elts.push_back(C); |
| 37 | |
| 38 | // Size |
Mike Stump | d684000 | 2009-02-21 20:07:44 +0000 | [diff] [blame] | 39 | // FIXME: What is the right way to say this doesn't fit? We should give |
| 40 | // a user diagnostic in that case. Better fix would be to change the |
| 41 | // API to size_t. |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 42 | C = VMContext.getConstantInt(UnsignedLongTy, Size); |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 43 | Elts.push_back(C); |
| 44 | |
| 45 | if (BlockHasCopyDispose) { |
| 46 | // copy_func_helper_decl |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 47 | Elts.push_back(BuildCopyHelper(Ty, NoteForHelper)); |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 48 | |
| 49 | // destroy_func_decl |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 50 | Elts.push_back(BuildDestroyHelper(Ty, NoteForHelper)); |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 53 | C = VMContext.getConstantStruct(Elts); |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 54 | |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 55 | C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true, |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 56 | llvm::GlobalValue::InternalLinkage, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 57 | C, "__block_descriptor_tmp"); |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 58 | return C; |
| 59 | } |
| 60 | |
Mike Stump | 2a99814 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 61 | llvm::Constant *BlockModule::getNSConcreteGlobalBlock() { |
Chris Lattner | e2f79b6 | 2009-05-13 02:50:56 +0000 | [diff] [blame] | 62 | if (NSConcreteGlobalBlock == 0) |
| 63 | NSConcreteGlobalBlock = CGM.CreateRuntimeVariable(PtrToInt8Ty, |
| 64 | "_NSConcreteGlobalBlock"); |
Mike Stump | f99f1d0 | 2009-02-13 17:23:42 +0000 | [diff] [blame] | 65 | return NSConcreteGlobalBlock; |
| 66 | } |
| 67 | |
Mike Stump | 2a99814 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 68 | llvm::Constant *BlockModule::getNSConcreteStackBlock() { |
Chris Lattner | e2f79b6 | 2009-05-13 02:50:56 +0000 | [diff] [blame] | 69 | if (NSConcreteStackBlock == 0) |
| 70 | NSConcreteStackBlock = CGM.CreateRuntimeVariable(PtrToInt8Ty, |
| 71 | "_NSConcreteStackBlock"); |
Mike Stump | 59c5b11 | 2009-02-13 19:29:27 +0000 | [diff] [blame] | 72 | return NSConcreteStackBlock; |
| 73 | } |
| 74 | |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 75 | static void CollectBlockDeclRefInfo(const Stmt *S, |
Anders Carlsson | 4de9fce | 2009-03-01 01:09:12 +0000 | [diff] [blame] | 76 | CodeGenFunction::BlockInfo &Info) { |
| 77 | for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end(); |
| 78 | I != E; ++I) |
Daniel Dunbar | 82573ee | 2009-03-02 07:00:57 +0000 | [diff] [blame] | 79 | if (*I) |
| 80 | CollectBlockDeclRefInfo(*I, Info); |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 81 | |
Anders Carlsson | 4de9fce | 2009-03-01 01:09:12 +0000 | [diff] [blame] | 82 | if (const BlockDeclRefExpr *DE = dyn_cast<BlockDeclRefExpr>(S)) { |
| 83 | // FIXME: Handle enums. |
| 84 | if (isa<FunctionDecl>(DE->getDecl())) |
| 85 | return; |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 86 | |
Anders Carlsson | 4de9fce | 2009-03-01 01:09:12 +0000 | [diff] [blame] | 87 | if (DE->isByRef()) |
| 88 | Info.ByRefDeclRefs.push_back(DE); |
| 89 | else |
| 90 | Info.ByCopyDeclRefs.push_back(DE); |
| 91 | } |
| 92 | } |
| 93 | |
Mike Stump | 58a8514 | 2009-03-04 22:48:06 +0000 | [diff] [blame] | 94 | /// CanBlockBeGlobal - Given a BlockInfo struct, determines if a block can be |
| 95 | /// declared as a global variable instead of on the stack. |
Chris Lattner | e2f79b6 | 2009-05-13 02:50:56 +0000 | [diff] [blame] | 96 | static bool CanBlockBeGlobal(const CodeGenFunction::BlockInfo &Info) { |
Anders Carlsson | 4de9fce | 2009-03-01 01:09:12 +0000 | [diff] [blame] | 97 | return Info.ByRefDeclRefs.empty() && Info.ByCopyDeclRefs.empty(); |
| 98 | } |
| 99 | |
Mike Stump | 58a8514 | 2009-03-04 22:48:06 +0000 | [diff] [blame] | 100 | // FIXME: Push most into CGM, passing down a few bits, like current function |
| 101 | // name. |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 102 | llvm::Value *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) { |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 103 | |
Anders Carlsson | 4de9fce | 2009-03-01 01:09:12 +0000 | [diff] [blame] | 104 | std::string Name = CurFn->getName(); |
| 105 | CodeGenFunction::BlockInfo Info(0, Name.c_str()); |
| 106 | CollectBlockDeclRefInfo(BE->getBody(), Info); |
| 107 | |
| 108 | // Check if the block can be global. |
Mike Stump | 58a8514 | 2009-03-04 22:48:06 +0000 | [diff] [blame] | 109 | // FIXME: This test doesn't work for nested blocks yet. Longer term, I'd like |
| 110 | // to just have one code path. We should move this function into CGM and pass |
| 111 | // CGF, then we can just check to see if CGF is 0. |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 112 | if (0 && CanBlockBeGlobal(Info)) |
Anders Carlsson | 4de9fce | 2009-03-01 01:09:12 +0000 | [diff] [blame] | 113 | return CGM.GetAddrOfGlobalBlock(BE, Name.c_str()); |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 114 | |
| 115 | std::vector<llvm::Constant*> Elts(5); |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 116 | llvm::Constant *C; |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 117 | llvm::Value *V; |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 118 | |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 119 | { |
| 120 | // C = BuildBlockStructInitlist(); |
| 121 | unsigned int flags = BLOCK_HAS_DESCRIPTOR; |
| 122 | |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 123 | // We run this first so that we set BlockHasCopyDispose from the entire |
| 124 | // block literal. |
| 125 | // __invoke |
| 126 | uint64_t subBlockSize, subBlockAlign; |
| 127 | llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls; |
Mike Stump | a803b0e | 2009-03-25 17:58:24 +0000 | [diff] [blame] | 128 | bool subBlockHasCopyDispose = false; |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 129 | llvm::Function *Fn |
Mike Stump | 6cc88f7 | 2009-03-20 21:53:12 +0000 | [diff] [blame] | 130 | = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, CurFuncDecl, LocalDeclMap, |
Mike Stump | 7f28a9c | 2009-03-13 23:34:28 +0000 | [diff] [blame] | 131 | subBlockSize, |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 132 | subBlockAlign, |
| 133 | subBlockDeclRefDecls, |
Mike Stump | a803b0e | 2009-03-25 17:58:24 +0000 | [diff] [blame] | 134 | subBlockHasCopyDispose); |
| 135 | BlockHasCopyDispose |= subBlockHasCopyDispose; |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 136 | Elts[3] = Fn; |
| 137 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 138 | // FIXME: Don't use BlockHasCopyDispose, it is set more often then |
| 139 | // necessary, for example: { ^{ __block int i; ^{ i = 1; }(); }(); } |
Mike Stump | a803b0e | 2009-03-25 17:58:24 +0000 | [diff] [blame] | 140 | if (subBlockHasCopyDispose) |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 141 | flags |= BLOCK_HAS_COPY_DISPOSE; |
| 142 | |
Mike Stump | 7d6dc4f | 2009-02-13 20:17:16 +0000 | [diff] [blame] | 143 | // __isa |
Mike Stump | 59c5b11 | 2009-02-13 19:29:27 +0000 | [diff] [blame] | 144 | C = CGM.getNSConcreteStackBlock(); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 145 | C = VMContext.getConstantExprBitCast(C, PtrToInt8Ty); |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 146 | Elts[0] = C; |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 147 | |
| 148 | // __flags |
| 149 | const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( |
| 150 | CGM.getTypes().ConvertType(CGM.getContext().IntTy)); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 151 | C = VMContext.getConstantInt(IntTy, flags); |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 152 | Elts[1] = C; |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 153 | |
| 154 | // __reserved |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 155 | C = VMContext.getConstantInt(IntTy, 0); |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 156 | Elts[2] = C; |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 157 | |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 158 | if (subBlockDeclRefDecls.size() == 0) { |
Mike Stump | cf62d39 | 2009-03-06 18:42:23 +0000 | [diff] [blame] | 159 | // __descriptor |
Mike Stump | a803b0e | 2009-03-25 17:58:24 +0000 | [diff] [blame] | 160 | Elts[4] = BuildDescriptorBlockDecl(subBlockHasCopyDispose, subBlockSize, 0, 0); |
Mike Stump | cf62d39 | 2009-03-06 18:42:23 +0000 | [diff] [blame] | 161 | |
Mike Stump | 5570cfe | 2009-03-01 20:07:53 +0000 | [diff] [blame] | 162 | // Optimize to being a global block. |
| 163 | Elts[0] = CGM.getNSConcreteGlobalBlock(); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 164 | Elts[1] = VMContext.getConstantInt(IntTy, flags|BLOCK_IS_GLOBAL); |
Mike Stump | 5570cfe | 2009-03-01 20:07:53 +0000 | [diff] [blame] | 165 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 166 | C = VMContext.getConstantStruct(Elts); |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 167 | |
| 168 | char Name[32]; |
| 169 | sprintf(Name, "__block_holder_tmp_%d", CGM.getGlobalUniqueCount()); |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 170 | C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true, |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 171 | llvm::GlobalValue::InternalLinkage, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 172 | C, Name); |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 173 | QualType BPT = BE->getType(); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 174 | C = VMContext.getConstantExprBitCast(C, ConvertType(BPT)); |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 175 | return C; |
| 176 | } |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 177 | |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 178 | std::vector<const llvm::Type *> Types(5+subBlockDeclRefDecls.size()); |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 179 | for (int i=0; i<4; ++i) |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 180 | Types[i] = Elts[i]->getType(); |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 181 | Types[4] = PtrToInt8Ty; |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 182 | |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 183 | for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i) { |
| 184 | const Expr *E = subBlockDeclRefDecls[i]; |
| 185 | const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E); |
| 186 | QualType Ty = E->getType(); |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 187 | if (BDRE && BDRE->isByRef()) { |
| 188 | uint64_t Align = getContext().getDeclAlignInBytes(BDRE->getDecl()); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 189 | Types[i+5] = VMContext.getPointerType(BuildByRefType(Ty, Align), 0); |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 190 | } else |
| 191 | Types[i+5] = ConvertType(Ty); |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 192 | } |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 193 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 194 | llvm::StructType *Ty = VMContext.getStructType(Types, true); |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 195 | |
| 196 | llvm::AllocaInst *A = CreateTempAlloca(Ty); |
| 197 | A->setAlignment(subBlockAlign); |
| 198 | V = A; |
| 199 | |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 200 | std::vector<HelperInfo> NoteForHelper(subBlockDeclRefDecls.size()); |
| 201 | int helpersize = 0; |
| 202 | |
| 203 | for (unsigned i=0; i<4; ++i) |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 204 | Builder.CreateStore(Elts[i], Builder.CreateStructGEP(V, i, "block.tmp")); |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 205 | |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 206 | for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i) |
| 207 | { |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 208 | // FIXME: Push const down. |
| 209 | Expr *E = const_cast<Expr*>(subBlockDeclRefDecls[i]); |
| 210 | DeclRefExpr *DR; |
| 211 | ValueDecl *VD; |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 212 | |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 213 | DR = dyn_cast<DeclRefExpr>(E); |
| 214 | // Skip padding. |
| 215 | if (DR) continue; |
| 216 | |
| 217 | BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E); |
| 218 | VD = BDRE->getDecl(); |
| 219 | |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 220 | llvm::Value* Addr = Builder.CreateStructGEP(V, i+5, "tmp"); |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 221 | NoteForHelper[helpersize].index = i+5; |
| 222 | NoteForHelper[helpersize].RequiresCopying = BlockRequiresCopying(VD->getType()); |
| 223 | NoteForHelper[helpersize].flag |
| 224 | = VD->getType()->isBlockPointerType() ? BLOCK_FIELD_IS_BLOCK : BLOCK_FIELD_IS_OBJECT; |
| 225 | |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 226 | if (LocalDeclMap[VD]) { |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 227 | if (BDRE->isByRef()) { |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 228 | NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF | |
Mike Stump | f4bc312 | 2009-03-07 06:04:31 +0000 | [diff] [blame] | 229 | // FIXME: Someone double check this. |
| 230 | (VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0); |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 231 | const llvm::Type *Ty = Types[i+5]; |
| 232 | llvm::Value *Loc = LocalDeclMap[VD]; |
| 233 | Loc = Builder.CreateStructGEP(Loc, 1, "forwarding"); |
| 234 | Loc = Builder.CreateLoad(Loc, false); |
| 235 | Loc = Builder.CreateBitCast(Loc, Ty); |
| 236 | Builder.CreateStore(Loc, Addr); |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 237 | ++helpersize; |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 238 | continue; |
| 239 | } else |
| 240 | E = new (getContext()) DeclRefExpr (cast<NamedDecl>(VD), |
| 241 | VD->getType(), SourceLocation(), |
| 242 | false, false); |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 243 | } |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 244 | if (BDRE->isByRef()) { |
Mike Stump | a8b60c9 | 2009-03-21 21:00:35 +0000 | [diff] [blame] | 245 | NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF | |
| 246 | // FIXME: Someone double check this. |
| 247 | (VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0); |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 248 | E = new (getContext()) |
| 249 | UnaryOperator(E, UnaryOperator::AddrOf, |
| 250 | getContext().getPointerType(E->getType()), |
| 251 | SourceLocation()); |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 252 | } |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 253 | ++helpersize; |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 254 | |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 255 | RValue r = EmitAnyExpr(E, Addr, false); |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 256 | if (r.isScalar()) { |
| 257 | llvm::Value *Loc = r.getScalarVal(); |
| 258 | const llvm::Type *Ty = Types[i+5]; |
| 259 | if (BDRE->isByRef()) { |
Mike Stump | 58a8514 | 2009-03-04 22:48:06 +0000 | [diff] [blame] | 260 | // E is now the address of the value field, instead, we want the |
| 261 | // address of the actual ByRef struct. We optimize this slightly |
| 262 | // compared to gcc by not grabbing the forwarding slot as this must |
| 263 | // be done during Block_copy for us, and we can postpone the work |
| 264 | // until then. |
| 265 | uint64_t offset = BlockDecls[BDRE->getDecl()]; |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 266 | |
Mike Stump | 58a8514 | 2009-03-04 22:48:06 +0000 | [diff] [blame] | 267 | llvm::Value *BlockLiteral = LoadBlockStruct(); |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 268 | |
Mike Stump | 58a8514 | 2009-03-04 22:48:06 +0000 | [diff] [blame] | 269 | Loc = Builder.CreateGEP(BlockLiteral, |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 270 | VMContext.getConstantInt(llvm::Type::Int64Ty, |
Mike Stump | 58a8514 | 2009-03-04 22:48:06 +0000 | [diff] [blame] | 271 | offset), |
| 272 | "block.literal"); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 273 | Ty = VMContext.getPointerType(Ty, 0); |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 274 | Loc = Builder.CreateBitCast(Loc, Ty); |
Mike Stump | 58a8514 | 2009-03-04 22:48:06 +0000 | [diff] [blame] | 275 | Loc = Builder.CreateLoad(Loc, false); |
| 276 | // Loc = Builder.CreateBitCast(Loc, Ty); |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 277 | } |
| 278 | Builder.CreateStore(Loc, Addr); |
| 279 | } else if (r.isComplex()) |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 280 | // FIXME: implement |
| 281 | ErrorUnsupported(BE, "complex in block literal"); |
| 282 | else if (r.isAggregate()) |
| 283 | ; // Already created into the destination |
| 284 | else |
| 285 | assert (0 && "bad block variable"); |
| 286 | // FIXME: Ensure that the offset created by the backend for |
| 287 | // the struct matches the previously computed offset in BlockDecls. |
| 288 | } |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 289 | NoteForHelper.resize(helpersize); |
Mike Stump | cf62d39 | 2009-03-06 18:42:23 +0000 | [diff] [blame] | 290 | |
| 291 | // __descriptor |
Mike Stump | a803b0e | 2009-03-25 17:58:24 +0000 | [diff] [blame] | 292 | llvm::Value *Descriptor = BuildDescriptorBlockDecl(subBlockHasCopyDispose, |
| 293 | subBlockSize, Ty, |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 294 | &NoteForHelper); |
| 295 | Descriptor = Builder.CreateBitCast(Descriptor, PtrToInt8Ty); |
| 296 | Builder.CreateStore(Descriptor, Builder.CreateStructGEP(V, 4, "block.tmp")); |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 297 | } |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 298 | |
Mike Stump | bd65cac | 2009-02-19 01:01:04 +0000 | [diff] [blame] | 299 | QualType BPT = BE->getType(); |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 300 | return Builder.CreateBitCast(V, ConvertType(BPT)); |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | |
Mike Stump | 2a99814 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 304 | const llvm::Type *BlockModule::getBlockDescriptorType() { |
Mike Stump | ab69514 | 2009-02-13 15:16:56 +0000 | [diff] [blame] | 305 | if (BlockDescriptorType) |
| 306 | return BlockDescriptorType; |
| 307 | |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 308 | const llvm::Type *UnsignedLongTy = |
Mike Stump | ab69514 | 2009-02-13 15:16:56 +0000 | [diff] [blame] | 309 | getTypes().ConvertType(getContext().UnsignedLongTy); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 310 | |
Mike Stump | ab69514 | 2009-02-13 15:16:56 +0000 | [diff] [blame] | 311 | // struct __block_descriptor { |
| 312 | // unsigned long reserved; |
| 313 | // unsigned long block_size; |
| 314 | // }; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 315 | BlockDescriptorType = VMContext.getStructType(UnsignedLongTy, |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 316 | UnsignedLongTy, |
Mike Stump | ab69514 | 2009-02-13 15:16:56 +0000 | [diff] [blame] | 317 | NULL); |
| 318 | |
| 319 | getModule().addTypeName("struct.__block_descriptor", |
| 320 | BlockDescriptorType); |
| 321 | |
| 322 | return BlockDescriptorType; |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 323 | } |
| 324 | |
Mike Stump | 2a99814 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 325 | const llvm::Type *BlockModule::getGenericBlockLiteralType() { |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 326 | if (GenericBlockLiteralType) |
| 327 | return GenericBlockLiteralType; |
| 328 | |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 329 | const llvm::Type *BlockDescPtrTy = |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 330 | VMContext.getPointerTypeUnqual(getBlockDescriptorType()); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 331 | |
Mike Stump | 7cbb360 | 2009-02-13 16:01:35 +0000 | [diff] [blame] | 332 | const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( |
| 333 | getTypes().ConvertType(getContext().IntTy)); |
| 334 | |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 335 | // struct __block_literal_generic { |
Mike Stump | bd65cac | 2009-02-19 01:01:04 +0000 | [diff] [blame] | 336 | // void *__isa; |
| 337 | // int __flags; |
| 338 | // int __reserved; |
| 339 | // void (*__invoke)(void *); |
| 340 | // struct __block_descriptor *__descriptor; |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 341 | // }; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 342 | GenericBlockLiteralType = VMContext.getStructType(PtrToInt8Ty, |
Mike Stump | 7cbb360 | 2009-02-13 16:01:35 +0000 | [diff] [blame] | 343 | IntTy, |
| 344 | IntTy, |
Mike Stump | 797b632 | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 345 | PtrToInt8Ty, |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 346 | BlockDescPtrTy, |
| 347 | NULL); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 348 | |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 349 | getModule().addTypeName("struct.__block_literal_generic", |
| 350 | GenericBlockLiteralType); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 351 | |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 352 | return GenericBlockLiteralType; |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 353 | } |
| 354 | |
Mike Stump | 2a99814 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 355 | const llvm::Type *BlockModule::getGenericExtendedBlockLiteralType() { |
Mike Stump | bd65cac | 2009-02-19 01:01:04 +0000 | [diff] [blame] | 356 | if (GenericExtendedBlockLiteralType) |
| 357 | return GenericExtendedBlockLiteralType; |
| 358 | |
Mike Stump | bd65cac | 2009-02-19 01:01:04 +0000 | [diff] [blame] | 359 | const llvm::Type *BlockDescPtrTy = |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 360 | VMContext.getPointerTypeUnqual(getBlockDescriptorType()); |
Mike Stump | bd65cac | 2009-02-19 01:01:04 +0000 | [diff] [blame] | 361 | |
| 362 | const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( |
| 363 | getTypes().ConvertType(getContext().IntTy)); |
| 364 | |
| 365 | // struct __block_literal_generic { |
| 366 | // void *__isa; |
| 367 | // int __flags; |
| 368 | // int __reserved; |
| 369 | // void (*__invoke)(void *); |
| 370 | // struct __block_descriptor *__descriptor; |
| 371 | // void *__copy_func_helper_decl; |
| 372 | // void *__destroy_func_decl; |
| 373 | // }; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 374 | GenericExtendedBlockLiteralType = VMContext.getStructType(PtrToInt8Ty, |
Mike Stump | bd65cac | 2009-02-19 01:01:04 +0000 | [diff] [blame] | 375 | IntTy, |
| 376 | IntTy, |
Mike Stump | 797b632 | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 377 | PtrToInt8Ty, |
Mike Stump | bd65cac | 2009-02-19 01:01:04 +0000 | [diff] [blame] | 378 | BlockDescPtrTy, |
Mike Stump | 797b632 | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 379 | PtrToInt8Ty, |
| 380 | PtrToInt8Ty, |
Mike Stump | bd65cac | 2009-02-19 01:01:04 +0000 | [diff] [blame] | 381 | NULL); |
| 382 | |
| 383 | getModule().addTypeName("struct.__block_literal_extended_generic", |
| 384 | GenericExtendedBlockLiteralType); |
| 385 | |
| 386 | return GenericExtendedBlockLiteralType; |
| 387 | } |
| 388 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 389 | RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E) { |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 390 | const BlockPointerType *BPT = |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 391 | E->getCallee()->getType()->getAsBlockPointerType(); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 392 | |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 393 | llvm::Value *Callee = EmitScalarExpr(E->getCallee()); |
| 394 | |
| 395 | // Get a pointer to the generic block literal. |
| 396 | const llvm::Type *BlockLiteralTy = |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 397 | VMContext.getPointerTypeUnqual(CGM.getGenericBlockLiteralType()); |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 398 | |
| 399 | // Bitcast the callee to a block literal. |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 400 | llvm::Value *BlockLiteral = |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 401 | Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal"); |
| 402 | |
| 403 | // Get the function pointer from the literal. |
| 404 | llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp"); |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 405 | |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 406 | BlockLiteral = |
| 407 | Builder.CreateBitCast(BlockLiteral, |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 408 | VMContext.getPointerTypeUnqual(llvm::Type::Int8Ty), |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 409 | "tmp"); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 410 | |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 411 | // Add the block literal. |
| 412 | QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy); |
| 413 | CallArgList Args; |
| 414 | Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy)); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 415 | |
Anders Carlsson | 782f397 | 2009-04-08 23:13:16 +0000 | [diff] [blame] | 416 | QualType FnType = BPT->getPointeeType(); |
| 417 | |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 418 | // And the rest of the arguments. |
Anders Carlsson | 782f397 | 2009-04-08 23:13:16 +0000 | [diff] [blame] | 419 | EmitCallArgs(Args, FnType->getAsFunctionProtoType(), |
| 420 | E->arg_begin(), E->arg_end()); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 421 | |
Anders Carlsson | 6e460ff | 2009-04-07 22:10:22 +0000 | [diff] [blame] | 422 | // Load the function. |
| 423 | llvm::Value *Func = Builder.CreateLoad(FuncPtr, false, "tmp"); |
| 424 | |
Anders Carlsson | a17d7cc | 2009-04-08 02:55:55 +0000 | [diff] [blame] | 425 | QualType ResultType = FnType->getAsFunctionType()->getResultType(); |
| 426 | |
| 427 | const CGFunctionInfo &FnInfo = |
| 428 | CGM.getTypes().getFunctionInfo(ResultType, Args); |
Anders Carlsson | 6e460ff | 2009-04-07 22:10:22 +0000 | [diff] [blame] | 429 | |
| 430 | // Cast the function pointer to the right type. |
| 431 | const llvm::Type *BlockFTy = |
Anders Carlsson | a17d7cc | 2009-04-08 02:55:55 +0000 | [diff] [blame] | 432 | CGM.getTypes().GetFunctionType(FnInfo, false); |
Anders Carlsson | 6e460ff | 2009-04-07 22:10:22 +0000 | [diff] [blame] | 433 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 434 | const llvm::Type *BlockFTyPtr = VMContext.getPointerTypeUnqual(BlockFTy); |
Anders Carlsson | 6e460ff | 2009-04-07 22:10:22 +0000 | [diff] [blame] | 435 | Func = Builder.CreateBitCast(Func, BlockFTyPtr); |
| 436 | |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 437 | // And call the block. |
Anders Carlsson | f8544a4 | 2009-04-07 00:20:24 +0000 | [diff] [blame] | 438 | return EmitCall(FnInfo, Func, Args); |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 439 | } |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 440 | |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 441 | llvm::Value *CodeGenFunction::GetAddrOfBlockDecl(const BlockDeclRefExpr *E) { |
| 442 | uint64_t &offset = BlockDecls[E->getDecl()]; |
| 443 | |
| 444 | const llvm::Type *Ty; |
| 445 | Ty = CGM.getTypes().ConvertType(E->getDecl()->getType()); |
| 446 | |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 447 | // See if we have already allocated an offset for this variable. |
| 448 | if (offset == 0) { |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 449 | // Don't run the expensive check, unless we have to. |
| 450 | if (!BlockHasCopyDispose && BlockRequiresCopying(E->getType())) |
| 451 | BlockHasCopyDispose = true; |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 452 | // if not, allocate one now. |
| 453 | offset = getBlockOffset(E); |
| 454 | } |
| 455 | |
| 456 | llvm::Value *BlockLiteral = LoadBlockStruct(); |
| 457 | llvm::Value *V = Builder.CreateGEP(BlockLiteral, |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 458 | VMContext.getConstantInt(llvm::Type::Int64Ty, |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 459 | offset), |
Mike Stump | 58a8514 | 2009-03-04 22:48:06 +0000 | [diff] [blame] | 460 | "block.literal"); |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 461 | if (E->isByRef()) { |
| 462 | bool needsCopyDispose = BlockRequiresCopying(E->getType()); |
| 463 | uint64_t Align = getContext().getDeclAlignInBytes(E->getDecl()); |
| 464 | const llvm::Type *PtrStructTy |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 465 | = VMContext.getPointerType(BuildByRefType(E->getType(), Align), 0); |
Mike Stump | a8b60c9 | 2009-03-21 21:00:35 +0000 | [diff] [blame] | 466 | // The block literal will need a copy/destroy helper. |
| 467 | BlockHasCopyDispose = true; |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 468 | Ty = PtrStructTy; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 469 | Ty = VMContext.getPointerType(Ty, 0); |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 470 | V = Builder.CreateBitCast(V, Ty); |
| 471 | V = Builder.CreateLoad(V, false); |
| 472 | V = Builder.CreateStructGEP(V, 1, "forwarding"); |
| 473 | V = Builder.CreateLoad(V, false); |
| 474 | V = Builder.CreateBitCast(V, PtrStructTy); |
| 475 | V = Builder.CreateStructGEP(V, needsCopyDispose*2 + 4, "x"); |
| 476 | } else { |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 477 | Ty = VMContext.getPointerType(Ty, 0); |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 478 | V = Builder.CreateBitCast(V, Ty); |
| 479 | } |
| 480 | return V; |
| 481 | } |
| 482 | |
Mike Stump | 6cc88f7 | 2009-03-20 21:53:12 +0000 | [diff] [blame] | 483 | void CodeGenFunction::BlockForwardSelf() { |
| 484 | const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl); |
| 485 | ImplicitParamDecl *SelfDecl = OMD->getSelfDecl(); |
| 486 | llvm::Value *&DMEntry = LocalDeclMap[SelfDecl]; |
| 487 | if (DMEntry) |
| 488 | return; |
| 489 | // FIXME - Eliminate BlockDeclRefExprs, clients don't need/want to care |
| 490 | BlockDeclRefExpr *BDRE = new (getContext()) |
| 491 | BlockDeclRefExpr(SelfDecl, |
| 492 | SelfDecl->getType(), SourceLocation(), false); |
| 493 | DMEntry = GetAddrOfBlockDecl(BDRE); |
| 494 | } |
| 495 | |
Mike Stump | 67a6448 | 2009-02-14 22:16:35 +0000 | [diff] [blame] | 496 | llvm::Constant * |
Mike Stump | 90a9043 | 2009-03-04 18:47:42 +0000 | [diff] [blame] | 497 | BlockModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) { |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 498 | // Generate the block descriptor. |
| 499 | const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy); |
Mike Stump | 7cbb360 | 2009-02-13 16:01:35 +0000 | [diff] [blame] | 500 | const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( |
| 501 | getTypes().ConvertType(getContext().IntTy)); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 502 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 503 | llvm::Constant *DescriptorFields[2]; |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 504 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 505 | // Reserved |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 506 | DescriptorFields[0] = getModule().getContext().getNullValue(UnsignedLongTy); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 507 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 508 | // Block literal size. For global blocks we just use the size of the generic |
| 509 | // block literal struct. |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 510 | uint64_t BlockLiteralSize = |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 511 | TheTargetData.getTypeStoreSizeInBits(getGenericBlockLiteralType()) / 8; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 512 | DescriptorFields[1] = |
| 513 | VMContext.getConstantInt(UnsignedLongTy,BlockLiteralSize); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 514 | |
| 515 | llvm::Constant *DescriptorStruct = |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 516 | VMContext.getConstantStruct(&DescriptorFields[0], 2); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 517 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 518 | llvm::GlobalVariable *Descriptor = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 519 | new llvm::GlobalVariable(getModule(), DescriptorStruct->getType(), true, |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 520 | llvm::GlobalVariable::InternalLinkage, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 521 | DescriptorStruct, "__block_descriptor_global"); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 522 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 523 | // Generate the constants for the block literal. |
| 524 | llvm::Constant *LiteralFields[5]; |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 525 | |
Mike Stump | 67a6448 | 2009-02-14 22:16:35 +0000 | [diff] [blame] | 526 | CodeGenFunction::BlockInfo Info(0, n); |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 527 | uint64_t subBlockSize, subBlockAlign; |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 528 | llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls; |
Daniel Dunbar | d67b09a | 2009-03-12 03:07:24 +0000 | [diff] [blame] | 529 | bool subBlockHasCopyDispose = false; |
Mike Stump | 7f28a9c | 2009-03-13 23:34:28 +0000 | [diff] [blame] | 530 | llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap; |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 531 | llvm::Function *Fn |
Mike Stump | 6cc88f7 | 2009-03-20 21:53:12 +0000 | [diff] [blame] | 532 | = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, 0, LocalDeclMap, |
Mike Stump | 7f28a9c | 2009-03-13 23:34:28 +0000 | [diff] [blame] | 533 | subBlockSize, |
Mike Stump | 90a9043 | 2009-03-04 18:47:42 +0000 | [diff] [blame] | 534 | subBlockAlign, |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 535 | subBlockDeclRefDecls, |
| 536 | subBlockHasCopyDispose); |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 537 | assert(subBlockSize == BlockLiteralSize |
| 538 | && "no imports allowed for global block"); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 539 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 540 | // isa |
Mike Stump | f99f1d0 | 2009-02-13 17:23:42 +0000 | [diff] [blame] | 541 | LiteralFields[0] = getNSConcreteGlobalBlock(); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 542 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 543 | // Flags |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 544 | LiteralFields[1] = |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 545 | VMContext.getConstantInt(IntTy, BLOCK_IS_GLOBAL | BLOCK_HAS_DESCRIPTOR); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 546 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 547 | // Reserved |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 548 | LiteralFields[2] = getModule().getContext().getNullValue(IntTy); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 549 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 550 | // Function |
| 551 | LiteralFields[3] = Fn; |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 552 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 553 | // Descriptor |
| 554 | LiteralFields[4] = Descriptor; |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 555 | |
| 556 | llvm::Constant *BlockLiteralStruct = |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 557 | VMContext.getConstantStruct(&LiteralFields[0], 5); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 558 | |
| 559 | llvm::GlobalVariable *BlockLiteral = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 560 | new llvm::GlobalVariable(getModule(), BlockLiteralStruct->getType(), true, |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 561 | llvm::GlobalVariable::InternalLinkage, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 562 | BlockLiteralStruct, "__block_literal_global"); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 563 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 564 | return BlockLiteral; |
| 565 | } |
| 566 | |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 567 | llvm::Value *CodeGenFunction::LoadBlockStruct() { |
| 568 | return Builder.CreateLoad(LocalDeclMap[getBlockStructDecl()], "self"); |
| 569 | } |
| 570 | |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 571 | llvm::Function * |
| 572 | CodeGenFunction::GenerateBlockFunction(const BlockExpr *BExpr, |
| 573 | const BlockInfo& Info, |
Mike Stump | 6cc88f7 | 2009-03-20 21:53:12 +0000 | [diff] [blame] | 574 | const Decl *OuterFuncDecl, |
Mike Stump | 7f28a9c | 2009-03-13 23:34:28 +0000 | [diff] [blame] | 575 | llvm::DenseMap<const Decl*, llvm::Value*> ldm, |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 576 | uint64_t &Size, |
| 577 | uint64_t &Align, |
| 578 | llvm::SmallVector<const Expr *, 8> &subBlockDeclRefDecls, |
| 579 | bool &subBlockHasCopyDispose) { |
Devang Patel | 963dfbd | 2009-04-15 21:51:44 +0000 | [diff] [blame] | 580 | |
| 581 | // Check if we should generate debug info for this block. |
| 582 | if (CGM.getDebugInfo()) |
| 583 | DebugInfo = CGM.getDebugInfo(); |
| 584 | |
Mike Stump | 7f28a9c | 2009-03-13 23:34:28 +0000 | [diff] [blame] | 585 | // Arrange for local static and local extern declarations to appear |
| 586 | // to be local to this function as well, as they are directly referenced |
| 587 | // in a block. |
| 588 | for (llvm::DenseMap<const Decl *, llvm::Value*>::iterator i = ldm.begin(); |
| 589 | i != ldm.end(); |
| 590 | ++i) { |
| 591 | const VarDecl *VD = dyn_cast<VarDecl>(i->first); |
| 592 | |
Daniel Dunbar | 5466c7b | 2009-04-14 02:25:56 +0000 | [diff] [blame] | 593 | if (VD->getStorageClass() == VarDecl::Static || VD->hasExternalStorage()) |
Mike Stump | 7f28a9c | 2009-03-13 23:34:28 +0000 | [diff] [blame] | 594 | LocalDeclMap[VD] = i->second; |
| 595 | } |
| 596 | |
Eli Friedman | 48f9122 | 2009-03-28 03:24:54 +0000 | [diff] [blame] | 597 | // FIXME: We need to rearrange the code for copy/dispose so we have this |
| 598 | // sooner, so we can calculate offsets correctly. |
| 599 | if (!BlockHasCopyDispose) |
| 600 | BlockOffset = CGM.getTargetData() |
| 601 | .getTypeStoreSizeInBits(CGM.getGenericBlockLiteralType()) / 8; |
| 602 | else |
| 603 | BlockOffset = CGM.getTargetData() |
| 604 | .getTypeStoreSizeInBits(CGM.getGenericExtendedBlockLiteralType()) / 8; |
| 605 | BlockAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8; |
| 606 | |
Fariborz Jahanian | 140fb26 | 2009-04-11 17:55:15 +0000 | [diff] [blame] | 607 | const FunctionType *BlockFunctionType = BExpr->getFunctionType(); |
| 608 | QualType ResultType; |
| 609 | bool IsVariadic; |
Fariborz Jahanian | da0895d | 2009-04-11 18:54:57 +0000 | [diff] [blame] | 610 | if (const FunctionProtoType *FTy = |
| 611 | dyn_cast<FunctionProtoType>(BlockFunctionType)) { |
Fariborz Jahanian | 140fb26 | 2009-04-11 17:55:15 +0000 | [diff] [blame] | 612 | ResultType = FTy->getResultType(); |
| 613 | IsVariadic = FTy->isVariadic(); |
| 614 | } |
| 615 | else { |
| 616 | // K&R style block. |
| 617 | ResultType = BlockFunctionType->getResultType(); |
| 618 | IsVariadic = false; |
| 619 | } |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 620 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 621 | FunctionArgList Args; |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 622 | |
Chris Lattner | 161d36d | 2009-02-28 19:01:03 +0000 | [diff] [blame] | 623 | const BlockDecl *BD = BExpr->getBlockDecl(); |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 624 | |
| 625 | // FIXME: This leaks |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 626 | ImplicitParamDecl *SelfDecl = |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 627 | ImplicitParamDecl::Create(getContext(), 0, |
| 628 | SourceLocation(), 0, |
| 629 | getContext().getPointerType(getContext().VoidTy)); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 630 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 631 | Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType())); |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 632 | BlockStructDecl = SelfDecl; |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 633 | |
Steve Naroff | e78b809 | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 634 | for (BlockDecl::param_const_iterator i = BD->param_begin(), |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 635 | e = BD->param_end(); i != e; ++i) |
Mike Stump | 1905061 | 2009-02-17 23:25:52 +0000 | [diff] [blame] | 636 | Args.push_back(std::make_pair(*i, (*i)->getType())); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 637 | |
| 638 | const CGFunctionInfo &FI = |
Fariborz Jahanian | 140fb26 | 2009-04-11 17:55:15 +0000 | [diff] [blame] | 639 | CGM.getTypes().getFunctionInfo(ResultType, Args); |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 640 | |
Mike Stump | 67a6448 | 2009-02-14 22:16:35 +0000 | [diff] [blame] | 641 | std::string Name = std::string("__") + Info.Name + "_block_invoke_"; |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 642 | CodeGenTypes &Types = CGM.getTypes(); |
Fariborz Jahanian | 140fb26 | 2009-04-11 17:55:15 +0000 | [diff] [blame] | 643 | const llvm::FunctionType *LTy = Types.GetFunctionType(FI, IsVariadic); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 644 | |
| 645 | llvm::Function *Fn = |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 646 | llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, |
| 647 | Name, |
| 648 | &CGM.getModule()); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 649 | |
Daniel Dunbar | 0e4f40e | 2009-04-17 00:48:04 +0000 | [diff] [blame] | 650 | CGM.SetInternalFunctionAttributes(BD, Fn, FI); |
| 651 | |
Chris Lattner | 4863db4 | 2009-04-23 07:18:56 +0000 | [diff] [blame] | 652 | StartFunction(BD, ResultType, Fn, Args, |
Chris Lattner | 161d36d | 2009-02-28 19:01:03 +0000 | [diff] [blame] | 653 | BExpr->getBody()->getLocEnd()); |
Chris Lattner | 4863db4 | 2009-04-23 07:18:56 +0000 | [diff] [blame] | 654 | CurFuncDecl = OuterFuncDecl; |
Chris Lattner | b5437d2 | 2009-04-23 05:30:27 +0000 | [diff] [blame] | 655 | CurCodeDecl = BD; |
Chris Lattner | 161d36d | 2009-02-28 19:01:03 +0000 | [diff] [blame] | 656 | EmitStmt(BExpr->getBody()); |
| 657 | FinishFunction(cast<CompoundStmt>(BExpr->getBody())->getRBracLoc()); |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 658 | |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 659 | // The runtime needs a minimum alignment of a void *. |
| 660 | uint64_t MinAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8; |
| 661 | BlockOffset = llvm::RoundUpToAlignment(BlockOffset, MinAlign); |
| 662 | |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 663 | Size = BlockOffset; |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 664 | Align = BlockAlign; |
| 665 | subBlockDeclRefDecls = BlockDeclRefDecls; |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 666 | subBlockHasCopyDispose |= BlockHasCopyDispose; |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 667 | return Fn; |
| 668 | } |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 669 | |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 670 | uint64_t BlockFunction::getBlockOffset(const BlockDeclRefExpr *BDRE) { |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 671 | const ValueDecl *D = dyn_cast<ValueDecl>(BDRE->getDecl()); |
| 672 | |
| 673 | uint64_t Size = getContext().getTypeSize(D->getType()) / 8; |
| 674 | uint64_t Align = getContext().getDeclAlignInBytes(D); |
| 675 | |
| 676 | if (BDRE->isByRef()) { |
| 677 | Size = getContext().getTypeSize(getContext().VoidPtrTy) / 8; |
| 678 | Align = getContext().getTypeAlign(getContext().VoidPtrTy) / 8; |
| 679 | } |
| 680 | |
| 681 | assert ((Align > 0) && "alignment must be 1 byte or more"); |
| 682 | |
| 683 | uint64_t OldOffset = BlockOffset; |
| 684 | |
| 685 | // Ensure proper alignment, even if it means we have to have a gap |
| 686 | BlockOffset = llvm::RoundUpToAlignment(BlockOffset, Align); |
| 687 | BlockAlign = std::max(Align, BlockAlign); |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 688 | |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 689 | uint64_t Pad = BlockOffset - OldOffset; |
| 690 | if (Pad) { |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 691 | VMContext.getArrayType(llvm::Type::Int8Ty, Pad); |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 692 | QualType PadTy = getContext().getConstantArrayType(getContext().CharTy, |
| 693 | llvm::APInt(32, Pad), |
| 694 | ArrayType::Normal, 0); |
| 695 | ValueDecl *PadDecl = VarDecl::Create(getContext(), 0, SourceLocation(), |
| 696 | 0, QualType(PadTy), VarDecl::None, |
| 697 | SourceLocation()); |
| 698 | Expr *E; |
| 699 | E = new (getContext()) DeclRefExpr(PadDecl, PadDecl->getType(), |
| 700 | SourceLocation(), false, false); |
| 701 | BlockDeclRefDecls.push_back(E); |
| 702 | } |
| 703 | BlockDeclRefDecls.push_back(BDRE); |
| 704 | |
| 705 | BlockOffset += Size; |
| 706 | return BlockOffset-Size; |
| 707 | } |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 708 | |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 709 | llvm::Constant *BlockFunction:: |
| 710 | GenerateCopyHelperFunction(bool BlockHasCopyDispose, const llvm::StructType *T, |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 711 | std::vector<HelperInfo> *NoteForHelperp) { |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 712 | QualType R = getContext().VoidTy; |
| 713 | |
| 714 | FunctionArgList Args; |
| 715 | // FIXME: This leaks |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 716 | ImplicitParamDecl *Dst = |
| 717 | ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, |
| 718 | getContext().getPointerType(getContext().VoidTy)); |
| 719 | Args.push_back(std::make_pair(Dst, Dst->getType())); |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 720 | ImplicitParamDecl *Src = |
| 721 | ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, |
| 722 | getContext().getPointerType(getContext().VoidTy)); |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 723 | Args.push_back(std::make_pair(Src, Src->getType())); |
| 724 | |
| 725 | const CGFunctionInfo &FI = |
| 726 | CGM.getTypes().getFunctionInfo(R, Args); |
| 727 | |
Mike Stump | 3899a7f | 2009-06-05 23:26:36 +0000 | [diff] [blame] | 728 | // FIXME: We'd like to put these into a mergable by content, with |
| 729 | // internal linkage. |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 730 | std::string Name = std::string("__copy_helper_block_"); |
| 731 | CodeGenTypes &Types = CGM.getTypes(); |
| 732 | const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); |
| 733 | |
| 734 | llvm::Function *Fn = |
| 735 | llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, |
| 736 | Name, |
| 737 | &CGM.getModule()); |
| 738 | |
| 739 | IdentifierInfo *II |
| 740 | = &CGM.getContext().Idents.get("__copy_helper_block_"); |
| 741 | |
| 742 | FunctionDecl *FD = FunctionDecl::Create(getContext(), |
| 743 | getContext().getTranslationUnitDecl(), |
| 744 | SourceLocation(), II, R, |
| 745 | FunctionDecl::Static, false, |
| 746 | true); |
| 747 | CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 748 | |
| 749 | llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src); |
| 750 | llvm::Type *PtrPtrT; |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 751 | |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 752 | if (NoteForHelperp) { |
| 753 | std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp; |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 754 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 755 | PtrPtrT = VMContext.getPointerType(VMContext.getPointerType(T, 0), 0); |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 756 | SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT); |
| 757 | SrcObj = Builder.CreateLoad(SrcObj); |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 758 | |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 759 | llvm::Value *DstObj = CGF.GetAddrOfLocalVar(Dst); |
Mike Stump | c2f4c34 | 2009-04-15 22:11:36 +0000 | [diff] [blame] | 760 | llvm::Type *PtrPtrT; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 761 | PtrPtrT = VMContext.getPointerType(VMContext.getPointerType(T, 0), 0); |
Mike Stump | c2f4c34 | 2009-04-15 22:11:36 +0000 | [diff] [blame] | 762 | DstObj = Builder.CreateBitCast(DstObj, PtrPtrT); |
| 763 | DstObj = Builder.CreateLoad(DstObj); |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 764 | |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 765 | for (unsigned i=0; i < NoteForHelper.size(); ++i) { |
| 766 | int flag = NoteForHelper[i].flag; |
| 767 | int index = NoteForHelper[i].index; |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 768 | |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 769 | if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF) |
| 770 | || NoteForHelper[i].RequiresCopying) { |
| 771 | llvm::Value *Srcv = SrcObj; |
| 772 | Srcv = Builder.CreateStructGEP(Srcv, index); |
| 773 | Srcv = Builder.CreateBitCast(Srcv, |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 774 | VMContext.getPointerType(PtrToInt8Ty, 0)); |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 775 | Srcv = Builder.CreateLoad(Srcv); |
| 776 | |
| 777 | llvm::Value *Dstv = Builder.CreateStructGEP(DstObj, index); |
| 778 | Dstv = Builder.CreateBitCast(Dstv, PtrToInt8Ty); |
| 779 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 780 | llvm::Value *N = VMContext.getConstantInt(llvm::Type::Int32Ty, flag); |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 781 | llvm::Value *F = getBlockObjectAssign(); |
| 782 | Builder.CreateCall3(F, Dstv, Srcv, N); |
| 783 | } |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 784 | } |
| 785 | } |
| 786 | |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 787 | CGF.FinishFunction(); |
| 788 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 789 | return VMContext.getConstantExprBitCast(Fn, PtrToInt8Ty); |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 790 | } |
| 791 | |
Mike Stump | cf62d39 | 2009-03-06 18:42:23 +0000 | [diff] [blame] | 792 | llvm::Constant *BlockFunction:: |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 793 | GenerateDestroyHelperFunction(bool BlockHasCopyDispose, |
| 794 | const llvm::StructType* T, |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 795 | std::vector<HelperInfo> *NoteForHelperp) { |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 796 | QualType R = getContext().VoidTy; |
| 797 | |
| 798 | FunctionArgList Args; |
| 799 | // FIXME: This leaks |
| 800 | ImplicitParamDecl *Src = |
| 801 | ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, |
| 802 | getContext().getPointerType(getContext().VoidTy)); |
| 803 | |
| 804 | Args.push_back(std::make_pair(Src, Src->getType())); |
| 805 | |
| 806 | const CGFunctionInfo &FI = |
| 807 | CGM.getTypes().getFunctionInfo(R, Args); |
| 808 | |
Mike Stump | 3899a7f | 2009-06-05 23:26:36 +0000 | [diff] [blame] | 809 | // FIXME: We'd like to put these into a mergable by content, with |
| 810 | // internal linkage. |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 811 | std::string Name = std::string("__destroy_helper_block_"); |
| 812 | CodeGenTypes &Types = CGM.getTypes(); |
| 813 | const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); |
| 814 | |
| 815 | llvm::Function *Fn = |
| 816 | llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, |
| 817 | Name, |
| 818 | &CGM.getModule()); |
| 819 | |
| 820 | IdentifierInfo *II |
| 821 | = &CGM.getContext().Idents.get("__destroy_helper_block_"); |
| 822 | |
| 823 | FunctionDecl *FD = FunctionDecl::Create(getContext(), |
| 824 | getContext().getTranslationUnitDecl(), |
| 825 | SourceLocation(), II, R, |
| 826 | FunctionDecl::Static, false, |
| 827 | true); |
| 828 | CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); |
Mike Stump | 1edf6b6 | 2009-03-07 02:53:18 +0000 | [diff] [blame] | 829 | |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 830 | if (NoteForHelperp) { |
| 831 | std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp; |
Mike Stump | 1edf6b6 | 2009-03-07 02:53:18 +0000 | [diff] [blame] | 832 | |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 833 | llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src); |
| 834 | llvm::Type *PtrPtrT; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 835 | PtrPtrT = VMContext.getPointerType(VMContext.getPointerType(T, 0), 0); |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 836 | SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT); |
| 837 | SrcObj = Builder.CreateLoad(SrcObj); |
Mike Stump | 1edf6b6 | 2009-03-07 02:53:18 +0000 | [diff] [blame] | 838 | |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 839 | for (unsigned i=0; i < NoteForHelper.size(); ++i) { |
| 840 | int flag = NoteForHelper[i].flag; |
| 841 | int index = NoteForHelper[i].index; |
Mike Stump | 1edf6b6 | 2009-03-07 02:53:18 +0000 | [diff] [blame] | 842 | |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 843 | if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF) |
| 844 | || NoteForHelper[i].RequiresCopying) { |
| 845 | llvm::Value *Srcv = SrcObj; |
| 846 | Srcv = Builder.CreateStructGEP(Srcv, index); |
| 847 | Srcv = Builder.CreateBitCast(Srcv, |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 848 | VMContext.getPointerType(PtrToInt8Ty, 0)); |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 849 | Srcv = Builder.CreateLoad(Srcv); |
| 850 | |
| 851 | BuildBlockRelease(Srcv, flag); |
| 852 | } |
Mike Stump | 1edf6b6 | 2009-03-07 02:53:18 +0000 | [diff] [blame] | 853 | } |
| 854 | } |
| 855 | |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 856 | CGF.FinishFunction(); |
| 857 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 858 | return VMContext.getConstantExprBitCast(Fn, PtrToInt8Ty); |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 859 | } |
| 860 | |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 861 | llvm::Constant *BlockFunction::BuildCopyHelper(const llvm::StructType *T, |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 862 | std::vector<HelperInfo> *NoteForHelper) { |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 863 | return CodeGenFunction(CGM).GenerateCopyHelperFunction(BlockHasCopyDispose, |
| 864 | T, NoteForHelper); |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 865 | } |
| 866 | |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 867 | llvm::Constant *BlockFunction::BuildDestroyHelper(const llvm::StructType *T, |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 868 | std::vector<HelperInfo> *NoteForHelperp) { |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 869 | return CodeGenFunction(CGM).GenerateDestroyHelperFunction(BlockHasCopyDispose, |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 870 | T, NoteForHelperp); |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 871 | } |
Mike Stump | 797b632 | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 872 | |
Mike Stump | ee09422 | 2009-03-06 06:12:24 +0000 | [diff] [blame] | 873 | llvm::Constant *BlockFunction:: |
| 874 | GeneratebyrefCopyHelperFunction(const llvm::Type *T, int flag) { |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 875 | QualType R = getContext().VoidTy; |
| 876 | |
| 877 | FunctionArgList Args; |
| 878 | // FIXME: This leaks |
Mike Stump | ee09422 | 2009-03-06 06:12:24 +0000 | [diff] [blame] | 879 | ImplicitParamDecl *Dst = |
| 880 | ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, |
| 881 | getContext().getPointerType(getContext().VoidTy)); |
| 882 | Args.push_back(std::make_pair(Dst, Dst->getType())); |
| 883 | |
| 884 | // FIXME: This leaks |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 885 | ImplicitParamDecl *Src = |
| 886 | ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, |
| 887 | getContext().getPointerType(getContext().VoidTy)); |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 888 | Args.push_back(std::make_pair(Src, Src->getType())); |
| 889 | |
| 890 | const CGFunctionInfo &FI = |
| 891 | CGM.getTypes().getFunctionInfo(R, Args); |
| 892 | |
| 893 | std::string Name = std::string("__Block_byref_id_object_copy_"); |
| 894 | CodeGenTypes &Types = CGM.getTypes(); |
| 895 | const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); |
| 896 | |
Mike Stump | 3899a7f | 2009-06-05 23:26:36 +0000 | [diff] [blame] | 897 | // FIXME: We'd like to put these into a mergable by content, with |
| 898 | // internal linkage. |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 899 | llvm::Function *Fn = |
| 900 | llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, |
| 901 | Name, |
| 902 | &CGM.getModule()); |
| 903 | |
| 904 | IdentifierInfo *II |
| 905 | = &CGM.getContext().Idents.get("__Block_byref_id_object_copy_"); |
| 906 | |
| 907 | FunctionDecl *FD = FunctionDecl::Create(getContext(), |
| 908 | getContext().getTranslationUnitDecl(), |
| 909 | SourceLocation(), II, R, |
| 910 | FunctionDecl::Static, false, |
| 911 | true); |
| 912 | CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); |
Mike Stump | ee09422 | 2009-03-06 06:12:24 +0000 | [diff] [blame] | 913 | |
| 914 | // dst->x |
| 915 | llvm::Value *V = CGF.GetAddrOfLocalVar(Dst); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 916 | V = Builder.CreateBitCast(V, VMContext.getPointerType(T, 0)); |
Mike Stump | c2f4c34 | 2009-04-15 22:11:36 +0000 | [diff] [blame] | 917 | V = Builder.CreateLoad(V); |
Mike Stump | ee09422 | 2009-03-06 06:12:24 +0000 | [diff] [blame] | 918 | V = Builder.CreateStructGEP(V, 6, "x"); |
| 919 | llvm::Value *DstObj = Builder.CreateBitCast(V, PtrToInt8Ty); |
| 920 | |
| 921 | // src->x |
| 922 | V = CGF.GetAddrOfLocalVar(Src); |
| 923 | V = Builder.CreateLoad(V); |
| 924 | V = Builder.CreateBitCast(V, T); |
| 925 | V = Builder.CreateStructGEP(V, 6, "x"); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 926 | V = Builder.CreateBitCast(V, VMContext.getPointerType(PtrToInt8Ty, 0)); |
Mike Stump | ee09422 | 2009-03-06 06:12:24 +0000 | [diff] [blame] | 927 | llvm::Value *SrcObj = Builder.CreateLoad(V); |
| 928 | |
| 929 | flag |= BLOCK_BYREF_CALLER; |
| 930 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 931 | llvm::Value *N = VMContext.getConstantInt(llvm::Type::Int32Ty, flag); |
Mike Stump | ee09422 | 2009-03-06 06:12:24 +0000 | [diff] [blame] | 932 | llvm::Value *F = getBlockObjectAssign(); |
| 933 | Builder.CreateCall3(F, DstObj, SrcObj, N); |
| 934 | |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 935 | CGF.FinishFunction(); |
| 936 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 937 | return VMContext.getConstantExprBitCast(Fn, PtrToInt8Ty); |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 938 | } |
| 939 | |
Mike Stump | 1851b68 | 2009-03-06 04:53:30 +0000 | [diff] [blame] | 940 | llvm::Constant * |
| 941 | BlockFunction::GeneratebyrefDestroyHelperFunction(const llvm::Type *T, |
| 942 | int flag) { |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 943 | QualType R = getContext().VoidTy; |
| 944 | |
| 945 | FunctionArgList Args; |
| 946 | // FIXME: This leaks |
| 947 | ImplicitParamDecl *Src = |
| 948 | ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, |
| 949 | getContext().getPointerType(getContext().VoidTy)); |
| 950 | |
| 951 | Args.push_back(std::make_pair(Src, Src->getType())); |
| 952 | |
| 953 | const CGFunctionInfo &FI = |
| 954 | CGM.getTypes().getFunctionInfo(R, Args); |
| 955 | |
| 956 | std::string Name = std::string("__Block_byref_id_object_dispose_"); |
| 957 | CodeGenTypes &Types = CGM.getTypes(); |
| 958 | const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); |
| 959 | |
Mike Stump | 3899a7f | 2009-06-05 23:26:36 +0000 | [diff] [blame] | 960 | // FIXME: We'd like to put these into a mergable by content, with |
| 961 | // internal linkage. |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 962 | llvm::Function *Fn = |
| 963 | llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, |
| 964 | Name, |
| 965 | &CGM.getModule()); |
| 966 | |
| 967 | IdentifierInfo *II |
| 968 | = &CGM.getContext().Idents.get("__Block_byref_id_object_dispose_"); |
| 969 | |
| 970 | FunctionDecl *FD = FunctionDecl::Create(getContext(), |
| 971 | getContext().getTranslationUnitDecl(), |
| 972 | SourceLocation(), II, R, |
| 973 | FunctionDecl::Static, false, |
| 974 | true); |
| 975 | CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); |
Mike Stump | 1851b68 | 2009-03-06 04:53:30 +0000 | [diff] [blame] | 976 | |
| 977 | llvm::Value *V = CGF.GetAddrOfLocalVar(Src); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 978 | V = Builder.CreateBitCast(V, VMContext.getPointerType(T, 0)); |
Mike Stump | c2f4c34 | 2009-04-15 22:11:36 +0000 | [diff] [blame] | 979 | V = Builder.CreateLoad(V); |
Mike Stump | 1851b68 | 2009-03-06 04:53:30 +0000 | [diff] [blame] | 980 | V = Builder.CreateStructGEP(V, 6, "x"); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 981 | V = Builder.CreateBitCast(V, VMContext.getPointerType(PtrToInt8Ty, 0)); |
Mike Stump | c2f4c34 | 2009-04-15 22:11:36 +0000 | [diff] [blame] | 982 | V = Builder.CreateLoad(V); |
Mike Stump | 1851b68 | 2009-03-06 04:53:30 +0000 | [diff] [blame] | 983 | |
Mike Stump | 1851b68 | 2009-03-06 04:53:30 +0000 | [diff] [blame] | 984 | flag |= BLOCK_BYREF_CALLER; |
| 985 | BuildBlockRelease(V, flag); |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 986 | CGF.FinishFunction(); |
| 987 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 988 | return VMContext.getConstantExprBitCast(Fn, PtrToInt8Ty); |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 989 | } |
| 990 | |
Mike Stump | ee09422 | 2009-03-06 06:12:24 +0000 | [diff] [blame] | 991 | llvm::Constant *BlockFunction::BuildbyrefCopyHelper(const llvm::Type *T, |
Mike Stump | 3899a7f | 2009-06-05 23:26:36 +0000 | [diff] [blame] | 992 | int flag, unsigned Align) { |
| 993 | // All alignments below that of pointer alignment collpase down to just |
| 994 | // pointer alignment, as we always have at least that much alignment to begin |
| 995 | // with. |
| 996 | Align /= unsigned(CGF.Target.getPointerAlign(0)/8); |
| 997 | // As an optimization, we only generate a single function of each kind we |
| 998 | // might need. We need a different one for each alignment and for each |
| 999 | // setting of flags. We mix Align and flag to get the kind. |
| 1000 | uint64_t kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + flag; |
| 1001 | llvm::Constant *& Entry = CGM.AssignCache[kind]; |
| 1002 | if (Entry) |
| 1003 | return Entry; |
| 1004 | return Entry=CodeGenFunction(CGM).GeneratebyrefCopyHelperFunction(T, flag); |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1005 | } |
| 1006 | |
Mike Stump | 1851b68 | 2009-03-06 04:53:30 +0000 | [diff] [blame] | 1007 | llvm::Constant *BlockFunction::BuildbyrefDestroyHelper(const llvm::Type *T, |
Mike Stump | 3899a7f | 2009-06-05 23:26:36 +0000 | [diff] [blame] | 1008 | int flag, |
| 1009 | unsigned Align) { |
| 1010 | // All alignments below that of pointer alignment collpase down to just |
| 1011 | // pointer alignment, as we always have at least that much alignment to begin |
| 1012 | // with. |
| 1013 | Align /= unsigned(CGF.Target.getPointerAlign(0)/8); |
| 1014 | // As an optimization, we only generate a single function of each kind we |
| 1015 | // might need. We need a different one for each alignment and for each |
| 1016 | // setting of flags. We mix Align and flag to get the kind. |
| 1017 | uint64_t kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + flag; |
| 1018 | llvm::Constant *& Entry = CGM.DestroyCache[kind]; |
| 1019 | if (Entry) |
| 1020 | return Entry; |
| 1021 | return Entry=CodeGenFunction(CGM).GeneratebyrefDestroyHelperFunction(T, flag); |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1022 | } |
| 1023 | |
Mike Stump | 797b632 | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 1024 | llvm::Value *BlockFunction::getBlockObjectDispose() { |
| 1025 | if (CGM.BlockObjectDispose == 0) { |
| 1026 | const llvm::FunctionType *FTy; |
| 1027 | std::vector<const llvm::Type*> ArgTys; |
| 1028 | const llvm::Type *ResultType = llvm::Type::VoidTy; |
| 1029 | ArgTys.push_back(PtrToInt8Ty); |
| 1030 | ArgTys.push_back(llvm::Type::Int32Ty); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1031 | FTy = VMContext.getFunctionType(ResultType, ArgTys, false); |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 1032 | CGM.BlockObjectDispose |
Mike Stump | 797b632 | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 1033 | = CGM.CreateRuntimeFunction(FTy, "_Block_object_dispose"); |
| 1034 | } |
| 1035 | return CGM.BlockObjectDispose; |
| 1036 | } |
| 1037 | |
Mike Stump | ee09422 | 2009-03-06 06:12:24 +0000 | [diff] [blame] | 1038 | llvm::Value *BlockFunction::getBlockObjectAssign() { |
| 1039 | if (CGM.BlockObjectAssign == 0) { |
| 1040 | const llvm::FunctionType *FTy; |
| 1041 | std::vector<const llvm::Type*> ArgTys; |
| 1042 | const llvm::Type *ResultType = llvm::Type::VoidTy; |
| 1043 | ArgTys.push_back(PtrToInt8Ty); |
| 1044 | ArgTys.push_back(PtrToInt8Ty); |
| 1045 | ArgTys.push_back(llvm::Type::Int32Ty); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1046 | FTy = VMContext.getFunctionType(ResultType, ArgTys, false); |
Mike Stump | ee09422 | 2009-03-06 06:12:24 +0000 | [diff] [blame] | 1047 | CGM.BlockObjectAssign |
| 1048 | = CGM.CreateRuntimeFunction(FTy, "_Block_object_assign"); |
| 1049 | } |
| 1050 | return CGM.BlockObjectAssign; |
| 1051 | } |
| 1052 | |
Mike Stump | 1851b68 | 2009-03-06 04:53:30 +0000 | [diff] [blame] | 1053 | void BlockFunction::BuildBlockRelease(llvm::Value *V, int flag) { |
Mike Stump | 797b632 | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 1054 | llvm::Value *F = getBlockObjectDispose(); |
Mike Stump | 1851b68 | 2009-03-06 04:53:30 +0000 | [diff] [blame] | 1055 | llvm::Value *N; |
Mike Stump | 797b632 | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 1056 | V = Builder.CreateBitCast(V, PtrToInt8Ty); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1057 | N = VMContext.getConstantInt(llvm::Type::Int32Ty, flag); |
Mike Stump | 797b632 | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 1058 | Builder.CreateCall2(F, V, N); |
| 1059 | } |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 1060 | |
| 1061 | ASTContext &BlockFunction::getContext() const { return CGM.getContext(); } |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 1062 | |
| 1063 | BlockFunction::BlockFunction(CodeGenModule &cgm, CodeGenFunction &cgf, |
| 1064 | CGBuilderTy &B) |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1065 | : CGM(cgm), CGF(cgf), VMContext(cgm.getLLVMContext()), Builder(B) { |
| 1066 | PtrToInt8Ty = VMContext.getPointerTypeUnqual(llvm::Type::Int8Ty); |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 1067 | |
| 1068 | BlockHasCopyDispose = false; |
| 1069 | } |