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