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