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