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