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