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 | |
| 14 | #include "CodeGenFunction.h" |
| 15 | #include "CodeGenModule.h" |
Mike Stump | 692c6e3 | 2009-03-20 21:53:12 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | 2437cbf | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 17 | #include "llvm/Module.h" |
Anders Carlsson | 6a60fa2 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 18 | #include "llvm/Target/TargetData.h" |
Anders Carlsson | 2437cbf | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 19 | #include <algorithm> |
Torok Edwin | db71492 | 2009-08-24 13:25:12 +0000 | [diff] [blame] | 20 | #include <cstdio> |
| 21 | |
Anders Carlsson | 2437cbf | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 22 | using namespace clang; |
| 23 | using namespace CodeGen; |
| 24 | |
Mike Stump | d6ef62f | 2009-03-06 18:42:23 +0000 | [diff] [blame] | 25 | llvm::Constant *CodeGenFunction:: |
Mike Stump | d2142cf | 2009-03-25 17:58:24 +0000 | [diff] [blame] | 26 | BuildDescriptorBlockDecl(bool BlockHasCopyDispose, uint64_t Size, |
| 27 | const llvm::StructType* Ty, |
Mike Stump | aeb0ffd | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 28 | std::vector<HelperInfo> *NoteForHelper) { |
Mike Stump | c2c3833 | 2009-02-13 16:55:51 +0000 | [diff] [blame] | 29 | const llvm::Type *UnsignedLongTy |
| 30 | = CGM.getTypes().ConvertType(getContext().UnsignedLongTy); |
Mike Stump | 85284ba | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 31 | llvm::Constant *C; |
| 32 | std::vector<llvm::Constant*> Elts; |
| 33 | |
| 34 | // reserved |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 35 | C = llvm::ConstantInt::get(UnsignedLongTy, 0); |
Mike Stump | 85284ba | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 36 | Elts.push_back(C); |
| 37 | |
| 38 | // Size |
Mike Stump | 2ac40a9 | 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 | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 42 | C = llvm::ConstantInt::get(UnsignedLongTy, Size); |
Mike Stump | 85284ba | 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 | 6764593 | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 47 | Elts.push_back(BuildCopyHelper(Ty, NoteForHelper)); |
Mike Stump | 85284ba | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 48 | |
| 49 | // destroy_func_decl |
Mike Stump | 6764593 | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 50 | Elts.push_back(BuildDestroyHelper(Ty, NoteForHelper)); |
Mike Stump | 85284ba | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Nick Lewycky | 41eaf0a | 2009-09-19 20:00:52 +0000 | [diff] [blame] | 53 | C = llvm::ConstantStruct::get(VMContext, Elts, false); |
Mike Stump | 85284ba | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 54 | |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 55 | C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true, |
Mike Stump | 85284ba | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 56 | llvm::GlobalValue::InternalLinkage, |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 57 | C, "__block_descriptor_tmp"); |
Mike Stump | 85284ba | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 58 | return C; |
| 59 | } |
| 60 | |
Mike Stump | 9543567 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 61 | llvm::Constant *BlockModule::getNSConcreteGlobalBlock() { |
Chris Lattner | 46813bb | 2009-05-13 02:50:56 +0000 | [diff] [blame] | 62 | if (NSConcreteGlobalBlock == 0) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 63 | NSConcreteGlobalBlock = CGM.CreateRuntimeVariable(PtrToInt8Ty, |
Chris Lattner | 46813bb | 2009-05-13 02:50:56 +0000 | [diff] [blame] | 64 | "_NSConcreteGlobalBlock"); |
Mike Stump | 971f9b6 | 2009-02-13 17:23:42 +0000 | [diff] [blame] | 65 | return NSConcreteGlobalBlock; |
| 66 | } |
| 67 | |
Mike Stump | 9543567 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 68 | llvm::Constant *BlockModule::getNSConcreteStackBlock() { |
Chris Lattner | 46813bb | 2009-05-13 02:50:56 +0000 | [diff] [blame] | 69 | if (NSConcreteStackBlock == 0) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 70 | NSConcreteStackBlock = CGM.CreateRuntimeVariable(PtrToInt8Ty, |
Chris Lattner | 46813bb | 2009-05-13 02:50:56 +0000 | [diff] [blame] | 71 | "_NSConcreteStackBlock"); |
Mike Stump | 7ab278d | 2009-02-13 19:29:27 +0000 | [diff] [blame] | 72 | return NSConcreteStackBlock; |
| 73 | } |
| 74 | |
Mike Stump | 4446dcf | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 75 | static void CollectBlockDeclRefInfo(const Stmt *S, |
Anders Carlsson | ed5e69f | 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 | dd94271 | 2009-03-02 07:00:57 +0000 | [diff] [blame] | 79 | if (*I) |
| 80 | CollectBlockDeclRefInfo(*I, Info); |
Mike Stump | 4446dcf | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 81 | |
Anders Carlsson | ed5e69f | 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 | 4446dcf | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 86 | |
Anders Carlsson | ed5e69f | 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 | 2d33d63 | 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 | 46813bb | 2009-05-13 02:50:56 +0000 | [diff] [blame] | 96 | static bool CanBlockBeGlobal(const CodeGenFunction::BlockInfo &Info) { |
Anders Carlsson | ed5e69f | 2009-03-01 01:09:12 +0000 | [diff] [blame] | 97 | return Info.ByRefDeclRefs.empty() && Info.ByCopyDeclRefs.empty(); |
| 98 | } |
| 99 | |
Mike Stump | 2d33d63 | 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 | b750d92 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 102 | llvm::Value *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) { |
Mike Stump | 85284ba | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 103 | |
Anders Carlsson | ed5e69f | 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 | 2d33d63 | 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 | 97d01d5 | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 112 | if (0 && CanBlockBeGlobal(Info)) |
Anders Carlsson | ed5e69f | 2009-03-01 01:09:12 +0000 | [diff] [blame] | 113 | return CGM.GetAddrOfGlobalBlock(BE, Name.c_str()); |
Mike Stump | 4446dcf | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 114 | |
| 115 | std::vector<llvm::Constant*> Elts(5); |
Mike Stump | 85284ba | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 116 | llvm::Constant *C; |
Mike Stump | b750d92 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 117 | llvm::Value *V; |
Mike Stump | 85284ba | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 118 | |
Mike Stump | 85284ba | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 119 | { |
| 120 | // C = BuildBlockStructInitlist(); |
| 121 | unsigned int flags = BLOCK_HAS_DESCRIPTOR; |
| 122 | |
Mike Stump | 4446dcf | 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 | d2142cf | 2009-03-25 17:58:24 +0000 | [diff] [blame] | 128 | bool subBlockHasCopyDispose = false; |
Mike Stump | 4446dcf | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 129 | llvm::Function *Fn |
Mike Stump | 692c6e3 | 2009-03-20 21:53:12 +0000 | [diff] [blame] | 130 | = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, CurFuncDecl, LocalDeclMap, |
Mike Stump | 5469f29 | 2009-03-13 23:34:28 +0000 | [diff] [blame] | 131 | subBlockSize, |
Mike Stump | 4446dcf | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 132 | subBlockAlign, |
| 133 | subBlockDeclRefDecls, |
Mike Stump | d2142cf | 2009-03-25 17:58:24 +0000 | [diff] [blame] | 134 | subBlockHasCopyDispose); |
| 135 | BlockHasCopyDispose |= subBlockHasCopyDispose; |
Mike Stump | 4446dcf | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 136 | Elts[3] = Fn; |
| 137 | |
Mike Stump | 18bb928 | 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 | d2142cf | 2009-03-25 17:58:24 +0000 | [diff] [blame] | 140 | if (subBlockHasCopyDispose) |
Mike Stump | 85284ba | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 141 | flags |= BLOCK_HAS_COPY_DISPOSE; |
| 142 | |
Mike Stump | 499ae7e | 2009-02-13 20:17:16 +0000 | [diff] [blame] | 143 | // __isa |
Mike Stump | 7ab278d | 2009-02-13 19:29:27 +0000 | [diff] [blame] | 144 | C = CGM.getNSConcreteStackBlock(); |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 145 | C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty); |
Mike Stump | 4446dcf | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 146 | Elts[0] = C; |
Mike Stump | 85284ba | 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 | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 151 | C = llvm::ConstantInt::get(IntTy, flags); |
Mike Stump | 4446dcf | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 152 | Elts[1] = C; |
Mike Stump | 85284ba | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 153 | |
| 154 | // __reserved |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 155 | C = llvm::ConstantInt::get(IntTy, 0); |
Mike Stump | 4446dcf | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 156 | Elts[2] = C; |
Mike Stump | 85284ba | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 157 | |
Mike Stump | b750d92 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 158 | if (subBlockDeclRefDecls.size() == 0) { |
Mike Stump | d6ef62f | 2009-03-06 18:42:23 +0000 | [diff] [blame] | 159 | // __descriptor |
Mike Stump | d2142cf | 2009-03-25 17:58:24 +0000 | [diff] [blame] | 160 | Elts[4] = BuildDescriptorBlockDecl(subBlockHasCopyDispose, subBlockSize, 0, 0); |
Mike Stump | d6ef62f | 2009-03-06 18:42:23 +0000 | [diff] [blame] | 161 | |
Mike Stump | 3a139f3 | 2009-03-01 20:07:53 +0000 | [diff] [blame] | 162 | // Optimize to being a global block. |
| 163 | Elts[0] = CGM.getNSConcreteGlobalBlock(); |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 164 | Elts[1] = llvm::ConstantInt::get(IntTy, flags|BLOCK_IS_GLOBAL); |
Mike Stump | 3a139f3 | 2009-03-01 20:07:53 +0000 | [diff] [blame] | 165 | |
Nick Lewycky | 41eaf0a | 2009-09-19 20:00:52 +0000 | [diff] [blame] | 166 | C = llvm::ConstantStruct::get(VMContext, Elts, false); |
Mike Stump | b750d92 | 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 | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 170 | C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true, |
Mike Stump | b750d92 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 171 | llvm::GlobalValue::InternalLinkage, |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 172 | C, Name); |
Mike Stump | b750d92 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 173 | QualType BPT = BE->getType(); |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 174 | C = llvm::ConstantExpr::getBitCast(C, ConvertType(BPT)); |
Mike Stump | b750d92 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 175 | return C; |
| 176 | } |
Mike Stump | 4446dcf | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 177 | |
Mike Stump | b750d92 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 178 | std::vector<const llvm::Type *> Types(5+subBlockDeclRefDecls.size()); |
Mike Stump | aeb0ffd | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 179 | for (int i=0; i<4; ++i) |
Mike Stump | b750d92 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 180 | Types[i] = Elts[i]->getType(); |
Mike Stump | aeb0ffd | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 181 | Types[4] = PtrToInt8Ty; |
Mike Stump | b750d92 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 182 | |
Mike Stump | 1db7d04 | 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 | 97d01d5 | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 187 | if (BDRE && BDRE->isByRef()) { |
Anders Carlsson | 71d1d92 | 2009-09-09 02:51:03 +0000 | [diff] [blame] | 188 | Types[i+5] = llvm::PointerType::get(BuildByRefType(BDRE->getDecl()), 0); |
Mike Stump | 97d01d5 | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 189 | } else |
| 190 | Types[i+5] = ConvertType(Ty); |
Mike Stump | 1db7d04 | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 191 | } |
Mike Stump | b750d92 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 192 | |
Owen Anderson | 758428f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 193 | llvm::StructType *Ty = llvm::StructType::get(VMContext, Types, true); |
Mike Stump | b750d92 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 194 | |
| 195 | llvm::AllocaInst *A = CreateTempAlloca(Ty); |
| 196 | A->setAlignment(subBlockAlign); |
| 197 | V = A; |
| 198 | |
Mike Stump | aeb0ffd | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 199 | std::vector<HelperInfo> NoteForHelper(subBlockDeclRefDecls.size()); |
| 200 | int helpersize = 0; |
| 201 | |
| 202 | for (unsigned i=0; i<4; ++i) |
Mike Stump | b750d92 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 203 | Builder.CreateStore(Elts[i], Builder.CreateStructGEP(V, i, "block.tmp")); |
Mike Stump | 4446dcf | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 204 | |
Mike Stump | b750d92 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 205 | for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i) |
| 206 | { |
Mike Stump | 1db7d04 | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 207 | // FIXME: Push const down. |
| 208 | Expr *E = const_cast<Expr*>(subBlockDeclRefDecls[i]); |
| 209 | DeclRefExpr *DR; |
| 210 | ValueDecl *VD; |
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 | DR = dyn_cast<DeclRefExpr>(E); |
| 213 | // Skip padding. |
| 214 | if (DR) continue; |
| 215 | |
| 216 | BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E); |
| 217 | VD = BDRE->getDecl(); |
| 218 | |
Mike Stump | 97d01d5 | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 219 | llvm::Value* Addr = Builder.CreateStructGEP(V, i+5, "tmp"); |
Mike Stump | aeb0ffd | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 220 | NoteForHelper[helpersize].index = i+5; |
Mike Stump | 2114d7c | 2009-09-22 02:12:52 +0000 | [diff] [blame^] | 221 | NoteForHelper[helpersize].RequiresCopying |
| 222 | = BlockRequiresCopying(VD->getType()); |
Mike Stump | aeb0ffd | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 223 | NoteForHelper[helpersize].flag |
Mike Stump | 2114d7c | 2009-09-22 02:12:52 +0000 | [diff] [blame^] | 224 | = (VD->getType()->isBlockPointerType() |
| 225 | ? BLOCK_FIELD_IS_BLOCK |
| 226 | : BLOCK_FIELD_IS_OBJECT); |
Mike Stump | aeb0ffd | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 227 | |
Mike Stump | 1db7d04 | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 228 | if (LocalDeclMap[VD]) { |
Mike Stump | 97d01d5 | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 229 | if (BDRE->isByRef()) { |
Mike Stump | aeb0ffd | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 230 | NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF | |
Mike Stump | 90d8daf | 2009-03-07 06:04:31 +0000 | [diff] [blame] | 231 | // FIXME: Someone double check this. |
| 232 | (VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0); |
Mike Stump | 97d01d5 | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 233 | llvm::Value *Loc = LocalDeclMap[VD]; |
| 234 | Loc = Builder.CreateStructGEP(Loc, 1, "forwarding"); |
| 235 | Loc = Builder.CreateLoad(Loc, false); |
Mike Stump | 97d01d5 | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 236 | Builder.CreateStore(Loc, Addr); |
Mike Stump | aeb0ffd | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 237 | ++helpersize; |
Mike Stump | 97d01d5 | 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 | 1db7d04 | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 243 | } |
Mike Stump | 97d01d5 | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 244 | if (BDRE->isByRef()) { |
Mike Stump | efd7caa8 | 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 | 1db7d04 | 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 | 97d01d5 | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 252 | } |
Mike Stump | aeb0ffd | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 253 | ++helpersize; |
Mike Stump | 1db7d04 | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 254 | |
Mike Stump | 1db7d04 | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 255 | RValue r = EmitAnyExpr(E, Addr, false); |
Mike Stump | 97d01d5 | 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 | 2d33d63 | 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 | 4446dcf | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 266 | |
Mike Stump | 2d33d63 | 2009-03-04 22:48:06 +0000 | [diff] [blame] | 267 | llvm::Value *BlockLiteral = LoadBlockStruct(); |
Mike Stump | 4446dcf | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 268 | |
Mike Stump | 2d33d63 | 2009-03-04 22:48:06 +0000 | [diff] [blame] | 269 | Loc = Builder.CreateGEP(BlockLiteral, |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 270 | llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), |
Mike Stump | 2d33d63 | 2009-03-04 22:48:06 +0000 | [diff] [blame] | 271 | offset), |
| 272 | "block.literal"); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 273 | Ty = llvm::PointerType::get(Ty, 0); |
Mike Stump | 97d01d5 | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 274 | Loc = Builder.CreateBitCast(Loc, Ty); |
Mike Stump | 2d33d63 | 2009-03-04 22:48:06 +0000 | [diff] [blame] | 275 | Loc = Builder.CreateLoad(Loc, false); |
| 276 | // Loc = Builder.CreateBitCast(Loc, Ty); |
Mike Stump | 97d01d5 | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 277 | } |
| 278 | Builder.CreateStore(Loc, Addr); |
| 279 | } else if (r.isComplex()) |
Mike Stump | b750d92 | 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 | aeb0ffd | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 289 | NoteForHelper.resize(helpersize); |
Mike Stump | d6ef62f | 2009-03-06 18:42:23 +0000 | [diff] [blame] | 290 | |
| 291 | // __descriptor |
Mike Stump | d2142cf | 2009-03-25 17:58:24 +0000 | [diff] [blame] | 292 | llvm::Value *Descriptor = BuildDescriptorBlockDecl(subBlockHasCopyDispose, |
| 293 | subBlockSize, Ty, |
Mike Stump | aeb0ffd | 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 | 85284ba | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 297 | } |
Mike Stump | 4446dcf | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 298 | |
Mike Stump | 5d2534ad | 2009-02-19 01:01:04 +0000 | [diff] [blame] | 299 | QualType BPT = BE->getType(); |
Mike Stump | b750d92 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 300 | return Builder.CreateBitCast(V, ConvertType(BPT)); |
Mike Stump | 85284ba | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | |
Mike Stump | 9543567 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 304 | const llvm::Type *BlockModule::getBlockDescriptorType() { |
Mike Stump | 650c932 | 2009-02-13 15:16:56 +0000 | [diff] [blame] | 305 | if (BlockDescriptorType) |
| 306 | return BlockDescriptorType; |
| 307 | |
Mike Stump | b7074c0 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 308 | const llvm::Type *UnsignedLongTy = |
Mike Stump | 650c932 | 2009-02-13 15:16:56 +0000 | [diff] [blame] | 309 | getTypes().ConvertType(getContext().UnsignedLongTy); |
Mike Stump | b7074c0 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 310 | |
Mike Stump | 650c932 | 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 | 758428f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 315 | BlockDescriptorType = llvm::StructType::get(UnsignedLongTy->getContext(), |
| 316 | UnsignedLongTy, |
Mike Stump | b7074c0 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 317 | UnsignedLongTy, |
Mike Stump | 650c932 | 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 | 2437cbf | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Mike Stump | 9543567 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 326 | const llvm::Type *BlockModule::getGenericBlockLiteralType() { |
Mike Stump | 005c9a6 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 327 | if (GenericBlockLiteralType) |
| 328 | return GenericBlockLiteralType; |
| 329 | |
Mike Stump | b7074c0 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 330 | const llvm::Type *BlockDescPtrTy = |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 331 | llvm::PointerType::getUnqual(getBlockDescriptorType()); |
Mike Stump | b7074c0 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 332 | |
Mike Stump | 92bbd6d | 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 | 005c9a6 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 336 | // struct __block_literal_generic { |
Mike Stump | 5d2534ad | 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 | 005c9a6 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 342 | // }; |
Owen Anderson | 758428f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 343 | GenericBlockLiteralType = llvm::StructType::get(IntTy->getContext(), |
| 344 | PtrToInt8Ty, |
Mike Stump | 92bbd6d | 2009-02-13 16:01:35 +0000 | [diff] [blame] | 345 | IntTy, |
| 346 | IntTy, |
Mike Stump | 626aecc | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 347 | PtrToInt8Ty, |
Mike Stump | 005c9a6 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 348 | BlockDescPtrTy, |
| 349 | NULL); |
Mike Stump | b7074c0 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 350 | |
Mike Stump | 005c9a6 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 351 | getModule().addTypeName("struct.__block_literal_generic", |
| 352 | GenericBlockLiteralType); |
Mike Stump | b7074c0 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 353 | |
Mike Stump | 005c9a6 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 354 | return GenericBlockLiteralType; |
Anders Carlsson | 2437cbf | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Mike Stump | 9543567 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 357 | const llvm::Type *BlockModule::getGenericExtendedBlockLiteralType() { |
Mike Stump | 5d2534ad | 2009-02-19 01:01:04 +0000 | [diff] [blame] | 358 | if (GenericExtendedBlockLiteralType) |
| 359 | return GenericExtendedBlockLiteralType; |
| 360 | |
Mike Stump | 5d2534ad | 2009-02-19 01:01:04 +0000 | [diff] [blame] | 361 | const llvm::Type *BlockDescPtrTy = |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 362 | llvm::PointerType::getUnqual(getBlockDescriptorType()); |
Mike Stump | 5d2534ad | 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 | 758428f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 376 | GenericExtendedBlockLiteralType = llvm::StructType::get(IntTy->getContext(), |
| 377 | PtrToInt8Ty, |
Mike Stump | 5d2534ad | 2009-02-19 01:01:04 +0000 | [diff] [blame] | 378 | IntTy, |
| 379 | IntTy, |
Mike Stump | 626aecc | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 380 | PtrToInt8Ty, |
Mike Stump | 5d2534ad | 2009-02-19 01:01:04 +0000 | [diff] [blame] | 381 | BlockDescPtrTy, |
Mike Stump | 626aecc | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 382 | PtrToInt8Ty, |
| 383 | PtrToInt8Ty, |
Mike Stump | 5d2534ad | 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 | |
Mike Stump | 2114d7c | 2009-09-22 02:12:52 +0000 | [diff] [blame^] | 392 | bool BlockFunction::BlockRequiresCopying(QualType Ty) { |
| 393 | return CGM.BlockRequiresCopying(Ty); |
| 394 | } |
| 395 | |
Anders Carlsson | 6a60fa2 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 396 | RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E) { |
Mike Stump | b7074c0 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 397 | const BlockPointerType *BPT = |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 398 | E->getCallee()->getType()->getAs<BlockPointerType>(); |
Mike Stump | b7074c0 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 399 | |
Anders Carlsson | 2437cbf | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 400 | llvm::Value *Callee = EmitScalarExpr(E->getCallee()); |
| 401 | |
| 402 | // Get a pointer to the generic block literal. |
| 403 | const llvm::Type *BlockLiteralTy = |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 404 | llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType()); |
Anders Carlsson | 2437cbf | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 405 | |
| 406 | // Bitcast the callee to a block literal. |
Mike Stump | b7074c0 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 407 | llvm::Value *BlockLiteral = |
Anders Carlsson | 2437cbf | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 408 | Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal"); |
| 409 | |
| 410 | // Get the function pointer from the literal. |
| 411 | llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp"); |
Anders Carlsson | 2437cbf | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 412 | |
Mike Stump | b7074c0 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 413 | BlockLiteral = |
| 414 | Builder.CreateBitCast(BlockLiteral, |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 415 | llvm::PointerType::getUnqual( |
| 416 | llvm::Type::getInt8Ty(VMContext)), |
Anders Carlsson | 2437cbf | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 417 | "tmp"); |
Mike Stump | b7074c0 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 418 | |
Anders Carlsson | 2437cbf | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 419 | // Add the block literal. |
| 420 | QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy); |
| 421 | CallArgList Args; |
| 422 | Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy)); |
Mike Stump | b7074c0 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 423 | |
Anders Carlsson | 479e6fc | 2009-04-08 23:13:16 +0000 | [diff] [blame] | 424 | QualType FnType = BPT->getPointeeType(); |
| 425 | |
Anders Carlsson | 2437cbf | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 426 | // And the rest of the arguments. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 427 | EmitCallArgs(Args, FnType->getAs<FunctionProtoType>(), |
Anders Carlsson | 479e6fc | 2009-04-08 23:13:16 +0000 | [diff] [blame] | 428 | E->arg_begin(), E->arg_end()); |
Mike Stump | b7074c0 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 429 | |
Anders Carlsson | 5f50c65 | 2009-04-07 22:10:22 +0000 | [diff] [blame] | 430 | // Load the function. |
| 431 | llvm::Value *Func = Builder.CreateLoad(FuncPtr, false, "tmp"); |
| 432 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 433 | QualType ResultType = FnType->getAs<FunctionType>()->getResultType(); |
Anders Carlsson | a60cbcd | 2009-04-08 02:55:55 +0000 | [diff] [blame] | 434 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 435 | const CGFunctionInfo &FnInfo = |
Anders Carlsson | a60cbcd | 2009-04-08 02:55:55 +0000 | [diff] [blame] | 436 | CGM.getTypes().getFunctionInfo(ResultType, Args); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 437 | |
Anders Carlsson | 5f50c65 | 2009-04-07 22:10:22 +0000 | [diff] [blame] | 438 | // Cast the function pointer to the right type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 439 | const llvm::Type *BlockFTy = |
Anders Carlsson | a60cbcd | 2009-04-08 02:55:55 +0000 | [diff] [blame] | 440 | CGM.getTypes().GetFunctionType(FnInfo, false); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 441 | |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 442 | const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy); |
Anders Carlsson | 5f50c65 | 2009-04-07 22:10:22 +0000 | [diff] [blame] | 443 | Func = Builder.CreateBitCast(Func, BlockFTyPtr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 444 | |
Anders Carlsson | 2437cbf | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 445 | // And call the block. |
Anders Carlsson | 350da60 | 2009-04-07 00:20:24 +0000 | [diff] [blame] | 446 | return EmitCall(FnInfo, Func, Args); |
Anders Carlsson | 2437cbf | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 447 | } |
Anders Carlsson | 6a60fa2 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 448 | |
Mike Stump | 97d01d5 | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 449 | llvm::Value *CodeGenFunction::GetAddrOfBlockDecl(const BlockDeclRefExpr *E) { |
Anders Carlsson | 0168f4b | 2009-09-12 02:14:24 +0000 | [diff] [blame] | 450 | const ValueDecl *VD = E->getDecl(); |
| 451 | |
| 452 | uint64_t &offset = BlockDecls[VD]; |
Mike Stump | 97d01d5 | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 453 | |
Mike Stump | 97d01d5 | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 454 | |
Mike Stump | 97d01d5 | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 455 | // See if we have already allocated an offset for this variable. |
| 456 | if (offset == 0) { |
Mike Stump | 4446dcf | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 457 | // Don't run the expensive check, unless we have to. |
| 458 | if (!BlockHasCopyDispose && BlockRequiresCopying(E->getType())) |
| 459 | BlockHasCopyDispose = true; |
Mike Stump | 97d01d5 | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 460 | // if not, allocate one now. |
| 461 | offset = getBlockOffset(E); |
| 462 | } |
| 463 | |
| 464 | llvm::Value *BlockLiteral = LoadBlockStruct(); |
| 465 | llvm::Value *V = Builder.CreateGEP(BlockLiteral, |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 466 | llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 467 | offset), |
Mike Stump | 2d33d63 | 2009-03-04 22:48:06 +0000 | [diff] [blame] | 468 | "block.literal"); |
Mike Stump | 97d01d5 | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 469 | if (E->isByRef()) { |
Mike Stump | 97d01d5 | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 470 | const llvm::Type *PtrStructTy |
Anders Carlsson | 0168f4b | 2009-09-12 02:14:24 +0000 | [diff] [blame] | 471 | = llvm::PointerType::get(BuildByRefType(VD), 0); |
Mike Stump | efd7caa8 | 2009-03-21 21:00:35 +0000 | [diff] [blame] | 472 | // The block literal will need a copy/destroy helper. |
| 473 | BlockHasCopyDispose = true; |
Anders Carlsson | 0168f4b | 2009-09-12 02:14:24 +0000 | [diff] [blame] | 474 | |
| 475 | const llvm::Type *Ty = PtrStructTy; |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 476 | Ty = llvm::PointerType::get(Ty, 0); |
Mike Stump | 97d01d5 | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 477 | V = Builder.CreateBitCast(V, Ty); |
| 478 | V = Builder.CreateLoad(V, false); |
| 479 | V = Builder.CreateStructGEP(V, 1, "forwarding"); |
| 480 | V = Builder.CreateLoad(V, false); |
| 481 | V = Builder.CreateBitCast(V, PtrStructTy); |
Anders Carlsson | 0168f4b | 2009-09-12 02:14:24 +0000 | [diff] [blame] | 482 | V = Builder.CreateStructGEP(V, getByRefValueLLVMField(VD), |
| 483 | VD->getNameAsString()); |
Mike Stump | 97d01d5 | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 484 | } else { |
Anders Carlsson | 0168f4b | 2009-09-12 02:14:24 +0000 | [diff] [blame] | 485 | const llvm::Type *Ty = CGM.getTypes().ConvertType(VD->getType()); |
| 486 | |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 487 | Ty = llvm::PointerType::get(Ty, 0); |
Mike Stump | 97d01d5 | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 488 | V = Builder.CreateBitCast(V, Ty); |
| 489 | } |
| 490 | return V; |
| 491 | } |
| 492 | |
Mike Stump | 692c6e3 | 2009-03-20 21:53:12 +0000 | [diff] [blame] | 493 | void CodeGenFunction::BlockForwardSelf() { |
| 494 | const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl); |
| 495 | ImplicitParamDecl *SelfDecl = OMD->getSelfDecl(); |
| 496 | llvm::Value *&DMEntry = LocalDeclMap[SelfDecl]; |
| 497 | if (DMEntry) |
| 498 | return; |
| 499 | // FIXME - Eliminate BlockDeclRefExprs, clients don't need/want to care |
| 500 | BlockDeclRefExpr *BDRE = new (getContext()) |
| 501 | BlockDeclRefExpr(SelfDecl, |
| 502 | SelfDecl->getType(), SourceLocation(), false); |
| 503 | DMEntry = GetAddrOfBlockDecl(BDRE); |
| 504 | } |
| 505 | |
Mike Stump | 2d5a287 | 2009-02-14 22:16:35 +0000 | [diff] [blame] | 506 | llvm::Constant * |
Mike Stump | 6c39666 | 2009-03-04 18:47:42 +0000 | [diff] [blame] | 507 | BlockModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) { |
Anders Carlsson | 6a60fa2 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 508 | // Generate the block descriptor. |
| 509 | const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy); |
Mike Stump | 92bbd6d | 2009-02-13 16:01:35 +0000 | [diff] [blame] | 510 | const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( |
| 511 | getTypes().ConvertType(getContext().IntTy)); |
Mike Stump | b7074c0 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 512 | |
Anders Carlsson | 6a60fa2 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 513 | llvm::Constant *DescriptorFields[2]; |
Mike Stump | b7074c0 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 514 | |
Anders Carlsson | 6a60fa2 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 515 | // Reserved |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 516 | DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy); |
Mike Stump | b7074c0 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 517 | |
Anders Carlsson | 6a60fa2 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 518 | // Block literal size. For global blocks we just use the size of the generic |
| 519 | // block literal struct. |
Mike Stump | b7074c0 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 520 | uint64_t BlockLiteralSize = |
Mike Stump | 005c9a6 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 521 | TheTargetData.getTypeStoreSizeInBits(getGenericBlockLiteralType()) / 8; |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 522 | DescriptorFields[1] = |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 523 | llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize); |
Mike Stump | b7074c0 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 524 | |
| 525 | llvm::Constant *DescriptorStruct = |
Nick Lewycky | 41eaf0a | 2009-09-19 20:00:52 +0000 | [diff] [blame] | 526 | llvm::ConstantStruct::get(VMContext, &DescriptorFields[0], 2, false); |
Mike Stump | b7074c0 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 527 | |
Anders Carlsson | 6a60fa2 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 528 | llvm::GlobalVariable *Descriptor = |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 529 | new llvm::GlobalVariable(getModule(), DescriptorStruct->getType(), true, |
Mike Stump | b7074c0 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 530 | llvm::GlobalVariable::InternalLinkage, |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 531 | DescriptorStruct, "__block_descriptor_global"); |
Mike Stump | b7074c0 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 532 | |
Anders Carlsson | 6a60fa2 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 533 | // Generate the constants for the block literal. |
| 534 | llvm::Constant *LiteralFields[5]; |
Mike Stump | b7074c0 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 535 | |
Mike Stump | 2d5a287 | 2009-02-14 22:16:35 +0000 | [diff] [blame] | 536 | CodeGenFunction::BlockInfo Info(0, n); |
Mike Stump | b750d92 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 537 | uint64_t subBlockSize, subBlockAlign; |
Mike Stump | 1db7d04 | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 538 | llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls; |
Daniel Dunbar | a61bb4d | 2009-03-12 03:07:24 +0000 | [diff] [blame] | 539 | bool subBlockHasCopyDispose = false; |
Mike Stump | 5469f29 | 2009-03-13 23:34:28 +0000 | [diff] [blame] | 540 | llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap; |
Mike Stump | cb2fbcb | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 541 | llvm::Function *Fn |
Mike Stump | 692c6e3 | 2009-03-20 21:53:12 +0000 | [diff] [blame] | 542 | = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, 0, LocalDeclMap, |
Mike Stump | 5469f29 | 2009-03-13 23:34:28 +0000 | [diff] [blame] | 543 | subBlockSize, |
Mike Stump | 6c39666 | 2009-03-04 18:47:42 +0000 | [diff] [blame] | 544 | subBlockAlign, |
Mike Stump | 4446dcf | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 545 | subBlockDeclRefDecls, |
| 546 | subBlockHasCopyDispose); |
Mike Stump | cb2fbcb | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 547 | assert(subBlockSize == BlockLiteralSize |
| 548 | && "no imports allowed for global block"); |
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 | // isa |
Mike Stump | 971f9b6 | 2009-02-13 17:23:42 +0000 | [diff] [blame] | 551 | LiteralFields[0] = getNSConcreteGlobalBlock(); |
Mike Stump | b7074c0 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 552 | |
Anders Carlsson | 6a60fa2 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 553 | // Flags |
Mike Stump | 4446dcf | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 554 | LiteralFields[1] = |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 555 | llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL | BLOCK_HAS_DESCRIPTOR); |
Mike Stump | b7074c0 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 556 | |
Anders Carlsson | 6a60fa2 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 557 | // Reserved |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 558 | LiteralFields[2] = llvm::Constant::getNullValue(IntTy); |
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 | // Function |
| 561 | LiteralFields[3] = Fn; |
Mike Stump | b7074c0 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 562 | |
Anders Carlsson | 6a60fa2 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 563 | // Descriptor |
| 564 | LiteralFields[4] = Descriptor; |
Mike Stump | b7074c0 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 565 | |
| 566 | llvm::Constant *BlockLiteralStruct = |
Nick Lewycky | 41eaf0a | 2009-09-19 20:00:52 +0000 | [diff] [blame] | 567 | llvm::ConstantStruct::get(VMContext, &LiteralFields[0], 5, false); |
Mike Stump | b7074c0 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 568 | |
| 569 | llvm::GlobalVariable *BlockLiteral = |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 570 | new llvm::GlobalVariable(getModule(), BlockLiteralStruct->getType(), true, |
Mike Stump | b7074c0 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 571 | llvm::GlobalVariable::InternalLinkage, |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 572 | BlockLiteralStruct, "__block_literal_global"); |
Mike Stump | b7074c0 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 573 | |
Anders Carlsson | 6a60fa2 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 574 | return BlockLiteral; |
| 575 | } |
| 576 | |
Mike Stump | cb2fbcb | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 577 | llvm::Value *CodeGenFunction::LoadBlockStruct() { |
| 578 | return Builder.CreateLoad(LocalDeclMap[getBlockStructDecl()], "self"); |
| 579 | } |
| 580 | |
Mike Stump | 4446dcf | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 581 | llvm::Function * |
| 582 | CodeGenFunction::GenerateBlockFunction(const BlockExpr *BExpr, |
| 583 | const BlockInfo& Info, |
Mike Stump | 692c6e3 | 2009-03-20 21:53:12 +0000 | [diff] [blame] | 584 | const Decl *OuterFuncDecl, |
Mike Stump | 5469f29 | 2009-03-13 23:34:28 +0000 | [diff] [blame] | 585 | llvm::DenseMap<const Decl*, llvm::Value*> ldm, |
Mike Stump | 4446dcf | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 586 | uint64_t &Size, |
| 587 | uint64_t &Align, |
| 588 | llvm::SmallVector<const Expr *, 8> &subBlockDeclRefDecls, |
| 589 | bool &subBlockHasCopyDispose) { |
Devang Patel | 9074ed8 | 2009-04-15 21:51:44 +0000 | [diff] [blame] | 590 | |
| 591 | // Check if we should generate debug info for this block. |
| 592 | if (CGM.getDebugInfo()) |
| 593 | DebugInfo = CGM.getDebugInfo(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 594 | |
Mike Stump | 5469f29 | 2009-03-13 23:34:28 +0000 | [diff] [blame] | 595 | // Arrange for local static and local extern declarations to appear |
| 596 | // to be local to this function as well, as they are directly referenced |
| 597 | // in a block. |
| 598 | for (llvm::DenseMap<const Decl *, llvm::Value*>::iterator i = ldm.begin(); |
| 599 | i != ldm.end(); |
| 600 | ++i) { |
| 601 | const VarDecl *VD = dyn_cast<VarDecl>(i->first); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 602 | |
Daniel Dunbar | 0ca1660 | 2009-04-14 02:25:56 +0000 | [diff] [blame] | 603 | if (VD->getStorageClass() == VarDecl::Static || VD->hasExternalStorage()) |
Mike Stump | 5469f29 | 2009-03-13 23:34:28 +0000 | [diff] [blame] | 604 | LocalDeclMap[VD] = i->second; |
| 605 | } |
| 606 | |
Eli Friedman | 09a9b6e | 2009-03-28 03:24:54 +0000 | [diff] [blame] | 607 | // FIXME: We need to rearrange the code for copy/dispose so we have this |
| 608 | // sooner, so we can calculate offsets correctly. |
| 609 | if (!BlockHasCopyDispose) |
| 610 | BlockOffset = CGM.getTargetData() |
| 611 | .getTypeStoreSizeInBits(CGM.getGenericBlockLiteralType()) / 8; |
| 612 | else |
| 613 | BlockOffset = CGM.getTargetData() |
| 614 | .getTypeStoreSizeInBits(CGM.getGenericExtendedBlockLiteralType()) / 8; |
| 615 | BlockAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8; |
| 616 | |
Fariborz Jahanian | aae4349 | 2009-04-11 17:55:15 +0000 | [diff] [blame] | 617 | const FunctionType *BlockFunctionType = BExpr->getFunctionType(); |
| 618 | QualType ResultType; |
| 619 | bool IsVariadic; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 620 | if (const FunctionProtoType *FTy = |
Fariborz Jahanian | a4f6b6b | 2009-04-11 18:54:57 +0000 | [diff] [blame] | 621 | dyn_cast<FunctionProtoType>(BlockFunctionType)) { |
Fariborz Jahanian | aae4349 | 2009-04-11 17:55:15 +0000 | [diff] [blame] | 622 | ResultType = FTy->getResultType(); |
| 623 | IsVariadic = FTy->isVariadic(); |
Mike Stump | 658fe02 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 624 | } else { |
Fariborz Jahanian | aae4349 | 2009-04-11 17:55:15 +0000 | [diff] [blame] | 625 | // K&R style block. |
| 626 | ResultType = BlockFunctionType->getResultType(); |
| 627 | IsVariadic = false; |
| 628 | } |
Mike Stump | b7074c0 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 629 | |
Anders Carlsson | 6a60fa2 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 630 | FunctionArgList Args; |
Mike Stump | b7074c0 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 631 | |
Chris Lattner | 3385fe12 | 2009-02-28 19:01:03 +0000 | [diff] [blame] | 632 | const BlockDecl *BD = BExpr->getBlockDecl(); |
Anders Carlsson | 6a60fa2 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 633 | |
| 634 | // FIXME: This leaks |
Mike Stump | b7074c0 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 635 | ImplicitParamDecl *SelfDecl = |
Anders Carlsson | 6a60fa2 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 636 | ImplicitParamDecl::Create(getContext(), 0, |
| 637 | SourceLocation(), 0, |
| 638 | getContext().getPointerType(getContext().VoidTy)); |
Mike Stump | b7074c0 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 639 | |
Anders Carlsson | 6a60fa2 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 640 | Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType())); |
Mike Stump | cb2fbcb | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 641 | BlockStructDecl = SelfDecl; |
Mike Stump | b7074c0 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 642 | |
Steve Naroff | c4b30e5 | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 643 | for (BlockDecl::param_const_iterator i = BD->param_begin(), |
Anders Carlsson | 6a60fa2 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 644 | e = BD->param_end(); i != e; ++i) |
Mike Stump | eecd39f | 2009-02-17 23:25:52 +0000 | [diff] [blame] | 645 | Args.push_back(std::make_pair(*i, (*i)->getType())); |
Mike Stump | b7074c0 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 646 | |
| 647 | const CGFunctionInfo &FI = |
Fariborz Jahanian | aae4349 | 2009-04-11 17:55:15 +0000 | [diff] [blame] | 648 | CGM.getTypes().getFunctionInfo(ResultType, Args); |
Anders Carlsson | 6a60fa2 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 649 | |
Mike Stump | 2d5a287 | 2009-02-14 22:16:35 +0000 | [diff] [blame] | 650 | std::string Name = std::string("__") + Info.Name + "_block_invoke_"; |
Anders Carlsson | 6a60fa2 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 651 | CodeGenTypes &Types = CGM.getTypes(); |
Fariborz Jahanian | aae4349 | 2009-04-11 17:55:15 +0000 | [diff] [blame] | 652 | const llvm::FunctionType *LTy = Types.GetFunctionType(FI, IsVariadic); |
Mike Stump | b7074c0 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 653 | |
| 654 | llvm::Function *Fn = |
Anders Carlsson | 6a60fa2 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 655 | llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, |
| 656 | Name, |
| 657 | &CGM.getModule()); |
Mike Stump | b7074c0 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 658 | |
Daniel Dunbar | c3e7cff | 2009-04-17 00:48:04 +0000 | [diff] [blame] | 659 | CGM.SetInternalFunctionAttributes(BD, Fn, FI); |
| 660 | |
Chris Lattner | 324f80b | 2009-04-23 07:18:56 +0000 | [diff] [blame] | 661 | StartFunction(BD, ResultType, Fn, Args, |
Chris Lattner | 3385fe12 | 2009-02-28 19:01:03 +0000 | [diff] [blame] | 662 | BExpr->getBody()->getLocEnd()); |
Chris Lattner | 324f80b | 2009-04-23 07:18:56 +0000 | [diff] [blame] | 663 | CurFuncDecl = OuterFuncDecl; |
Chris Lattner | 28ec0cf | 2009-04-23 05:30:27 +0000 | [diff] [blame] | 664 | CurCodeDecl = BD; |
Chris Lattner | 3385fe12 | 2009-02-28 19:01:03 +0000 | [diff] [blame] | 665 | EmitStmt(BExpr->getBody()); |
| 666 | FinishFunction(cast<CompoundStmt>(BExpr->getBody())->getRBracLoc()); |
Anders Carlsson | 6a60fa2 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 667 | |
Mike Stump | b750d92 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 668 | // The runtime needs a minimum alignment of a void *. |
| 669 | uint64_t MinAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8; |
| 670 | BlockOffset = llvm::RoundUpToAlignment(BlockOffset, MinAlign); |
| 671 | |
Mike Stump | cb2fbcb | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 672 | Size = BlockOffset; |
Mike Stump | b750d92 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 673 | Align = BlockAlign; |
| 674 | subBlockDeclRefDecls = BlockDeclRefDecls; |
Mike Stump | 4446dcf | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 675 | subBlockHasCopyDispose |= BlockHasCopyDispose; |
Anders Carlsson | 6a60fa2 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 676 | return Fn; |
| 677 | } |
Mike Stump | 1db7d04 | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 678 | |
Mike Stump | aeb0ffd | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 679 | uint64_t BlockFunction::getBlockOffset(const BlockDeclRefExpr *BDRE) { |
Mike Stump | 1db7d04 | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 680 | const ValueDecl *D = dyn_cast<ValueDecl>(BDRE->getDecl()); |
| 681 | |
| 682 | uint64_t Size = getContext().getTypeSize(D->getType()) / 8; |
| 683 | uint64_t Align = getContext().getDeclAlignInBytes(D); |
| 684 | |
| 685 | if (BDRE->isByRef()) { |
| 686 | Size = getContext().getTypeSize(getContext().VoidPtrTy) / 8; |
| 687 | Align = getContext().getTypeAlign(getContext().VoidPtrTy) / 8; |
| 688 | } |
| 689 | |
| 690 | assert ((Align > 0) && "alignment must be 1 byte or more"); |
| 691 | |
| 692 | uint64_t OldOffset = BlockOffset; |
| 693 | |
| 694 | // Ensure proper alignment, even if it means we have to have a gap |
| 695 | BlockOffset = llvm::RoundUpToAlignment(BlockOffset, Align); |
| 696 | BlockAlign = std::max(Align, BlockAlign); |
Mike Stump | 4446dcf | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 697 | |
Mike Stump | 1db7d04 | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 698 | uint64_t Pad = BlockOffset - OldOffset; |
| 699 | if (Pad) { |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 700 | llvm::ArrayType::get(llvm::Type::getInt8Ty(VMContext), Pad); |
Mike Stump | 1db7d04 | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 701 | QualType PadTy = getContext().getConstantArrayType(getContext().CharTy, |
| 702 | llvm::APInt(32, Pad), |
| 703 | ArrayType::Normal, 0); |
| 704 | ValueDecl *PadDecl = VarDecl::Create(getContext(), 0, SourceLocation(), |
Argyrios Kyrtzidis | 6032ef1 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 705 | 0, QualType(PadTy), 0, VarDecl::None); |
Mike Stump | 1db7d04 | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 706 | Expr *E; |
| 707 | E = new (getContext()) DeclRefExpr(PadDecl, PadDecl->getType(), |
| 708 | SourceLocation(), false, false); |
| 709 | BlockDeclRefDecls.push_back(E); |
| 710 | } |
| 711 | BlockDeclRefDecls.push_back(BDRE); |
| 712 | |
| 713 | BlockOffset += Size; |
| 714 | return BlockOffset-Size; |
| 715 | } |
Mike Stump | 97d01d5 | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 716 | |
Mike Stump | aeb0ffd | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 717 | llvm::Constant *BlockFunction:: |
| 718 | GenerateCopyHelperFunction(bool BlockHasCopyDispose, const llvm::StructType *T, |
Mike Stump | 6764593 | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 719 | std::vector<HelperInfo> *NoteForHelperp) { |
Mike Stump | 0c74327 | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 720 | QualType R = getContext().VoidTy; |
| 721 | |
| 722 | FunctionArgList Args; |
| 723 | // FIXME: This leaks |
Mike Stump | aeb0ffd | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 724 | ImplicitParamDecl *Dst = |
| 725 | ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, |
| 726 | getContext().getPointerType(getContext().VoidTy)); |
| 727 | Args.push_back(std::make_pair(Dst, Dst->getType())); |
Mike Stump | 0c74327 | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 728 | ImplicitParamDecl *Src = |
| 729 | ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, |
| 730 | getContext().getPointerType(getContext().VoidTy)); |
Mike Stump | 0c74327 | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 731 | Args.push_back(std::make_pair(Src, Src->getType())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 732 | |
Mike Stump | 0c74327 | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 733 | const CGFunctionInfo &FI = |
| 734 | CGM.getTypes().getFunctionInfo(R, Args); |
| 735 | |
Mike Stump | cbc2bca | 2009-06-05 23:26:36 +0000 | [diff] [blame] | 736 | // FIXME: We'd like to put these into a mergable by content, with |
| 737 | // internal linkage. |
Mike Stump | 0c74327 | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 738 | std::string Name = std::string("__copy_helper_block_"); |
| 739 | CodeGenTypes &Types = CGM.getTypes(); |
| 740 | const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); |
| 741 | |
| 742 | llvm::Function *Fn = |
| 743 | llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, |
| 744 | Name, |
| 745 | &CGM.getModule()); |
| 746 | |
| 747 | IdentifierInfo *II |
| 748 | = &CGM.getContext().Idents.get("__copy_helper_block_"); |
| 749 | |
| 750 | FunctionDecl *FD = FunctionDecl::Create(getContext(), |
| 751 | getContext().getTranslationUnitDecl(), |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 752 | SourceLocation(), II, R, 0, |
Mike Stump | 0c74327 | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 753 | FunctionDecl::Static, false, |
| 754 | true); |
| 755 | CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); |
Mike Stump | aeb0ffd | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 756 | |
| 757 | llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src); |
| 758 | llvm::Type *PtrPtrT; |
Mike Stump | aeb0ffd | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 759 | |
Mike Stump | 6764593 | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 760 | if (NoteForHelperp) { |
| 761 | std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp; |
Mike Stump | aeb0ffd | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 762 | |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 763 | PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0); |
Mike Stump | 6764593 | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 764 | SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT); |
| 765 | SrcObj = Builder.CreateLoad(SrcObj); |
Mike Stump | aeb0ffd | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 766 | |
Mike Stump | 6764593 | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 767 | llvm::Value *DstObj = CGF.GetAddrOfLocalVar(Dst); |
Mike Stump | 3b65ac2 | 2009-04-15 22:11:36 +0000 | [diff] [blame] | 768 | llvm::Type *PtrPtrT; |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 769 | PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0); |
Mike Stump | 3b65ac2 | 2009-04-15 22:11:36 +0000 | [diff] [blame] | 770 | DstObj = Builder.CreateBitCast(DstObj, PtrPtrT); |
| 771 | DstObj = Builder.CreateLoad(DstObj); |
Mike Stump | aeb0ffd | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 772 | |
Mike Stump | 6764593 | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 773 | for (unsigned i=0; i < NoteForHelper.size(); ++i) { |
| 774 | int flag = NoteForHelper[i].flag; |
| 775 | int index = NoteForHelper[i].index; |
Mike Stump | aeb0ffd | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 776 | |
Mike Stump | 6764593 | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 777 | if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF) |
| 778 | || NoteForHelper[i].RequiresCopying) { |
| 779 | llvm::Value *Srcv = SrcObj; |
| 780 | Srcv = Builder.CreateStructGEP(Srcv, index); |
| 781 | Srcv = Builder.CreateBitCast(Srcv, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 782 | llvm::PointerType::get(PtrToInt8Ty, 0)); |
Mike Stump | 6764593 | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 783 | Srcv = Builder.CreateLoad(Srcv); |
| 784 | |
| 785 | llvm::Value *Dstv = Builder.CreateStructGEP(DstObj, index); |
| 786 | Dstv = Builder.CreateBitCast(Dstv, PtrToInt8Ty); |
| 787 | |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 788 | llvm::Value *N = llvm::ConstantInt::get( |
| 789 | llvm::Type::getInt32Ty(T->getContext()), flag); |
Mike Stump | 6764593 | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 790 | llvm::Value *F = getBlockObjectAssign(); |
| 791 | Builder.CreateCall3(F, Dstv, Srcv, N); |
| 792 | } |
Mike Stump | aeb0ffd | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 793 | } |
| 794 | } |
| 795 | |
Mike Stump | 0c74327 | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 796 | CGF.FinishFunction(); |
| 797 | |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 798 | return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); |
Mike Stump | 97d01d5 | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 799 | } |
| 800 | |
Mike Stump | d6ef62f | 2009-03-06 18:42:23 +0000 | [diff] [blame] | 801 | llvm::Constant *BlockFunction:: |
Mike Stump | aeb0ffd | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 802 | GenerateDestroyHelperFunction(bool BlockHasCopyDispose, |
| 803 | const llvm::StructType* T, |
Mike Stump | 6764593 | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 804 | std::vector<HelperInfo> *NoteForHelperp) { |
Mike Stump | 0c74327 | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 805 | QualType R = getContext().VoidTy; |
| 806 | |
| 807 | FunctionArgList Args; |
| 808 | // FIXME: This leaks |
| 809 | ImplicitParamDecl *Src = |
| 810 | ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, |
| 811 | getContext().getPointerType(getContext().VoidTy)); |
| 812 | |
| 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("__destroy_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("__destroy_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 | 6f7d9f8 | 2009-03-07 02:53:18 +0000 | [diff] [blame] | 838 | |
Mike Stump | 6764593 | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 839 | if (NoteForHelperp) { |
| 840 | std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp; |
Mike Stump | 6f7d9f8 | 2009-03-07 02:53:18 +0000 | [diff] [blame] | 841 | |
Mike Stump | 6764593 | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 842 | llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src); |
| 843 | llvm::Type *PtrPtrT; |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 844 | PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0); |
Mike Stump | 6764593 | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 845 | SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT); |
| 846 | SrcObj = Builder.CreateLoad(SrcObj); |
Mike Stump | 6f7d9f8 | 2009-03-07 02:53:18 +0000 | [diff] [blame] | 847 | |
Mike Stump | 6764593 | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 848 | for (unsigned i=0; i < NoteForHelper.size(); ++i) { |
| 849 | int flag = NoteForHelper[i].flag; |
| 850 | int index = NoteForHelper[i].index; |
Mike Stump | 6f7d9f8 | 2009-03-07 02:53:18 +0000 | [diff] [blame] | 851 | |
Mike Stump | 6764593 | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 852 | if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF) |
| 853 | || NoteForHelper[i].RequiresCopying) { |
| 854 | llvm::Value *Srcv = SrcObj; |
| 855 | Srcv = Builder.CreateStructGEP(Srcv, index); |
| 856 | Srcv = Builder.CreateBitCast(Srcv, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 857 | llvm::PointerType::get(PtrToInt8Ty, 0)); |
Mike Stump | 6764593 | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 858 | Srcv = Builder.CreateLoad(Srcv); |
| 859 | |
| 860 | BuildBlockRelease(Srcv, flag); |
| 861 | } |
Mike Stump | 6f7d9f8 | 2009-03-07 02:53:18 +0000 | [diff] [blame] | 862 | } |
| 863 | } |
| 864 | |
Mike Stump | 0c74327 | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 865 | CGF.FinishFunction(); |
| 866 | |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 867 | return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); |
Mike Stump | 0c74327 | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 868 | } |
| 869 | |
Mike Stump | aeb0ffd | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 870 | llvm::Constant *BlockFunction::BuildCopyHelper(const llvm::StructType *T, |
Mike Stump | 6764593 | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 871 | std::vector<HelperInfo> *NoteForHelper) { |
Mike Stump | aeb0ffd | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 872 | return CodeGenFunction(CGM).GenerateCopyHelperFunction(BlockHasCopyDispose, |
| 873 | T, NoteForHelper); |
Mike Stump | 0c74327 | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 874 | } |
| 875 | |
Mike Stump | aeb0ffd | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 876 | llvm::Constant *BlockFunction::BuildDestroyHelper(const llvm::StructType *T, |
Mike Stump | 6764593 | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 877 | std::vector<HelperInfo> *NoteForHelperp) { |
Mike Stump | aeb0ffd | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 878 | return CodeGenFunction(CGM).GenerateDestroyHelperFunction(BlockHasCopyDispose, |
Mike Stump | 6764593 | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 879 | T, NoteForHelperp); |
Mike Stump | 97d01d5 | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 880 | } |
Mike Stump | 626aecc | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 881 | |
Mike Stump | f89230d | 2009-03-06 06:12:24 +0000 | [diff] [blame] | 882 | llvm::Constant *BlockFunction:: |
| 883 | GeneratebyrefCopyHelperFunction(const llvm::Type *T, int flag) { |
Mike Stump | ee2a5ee | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 884 | QualType R = getContext().VoidTy; |
| 885 | |
| 886 | FunctionArgList Args; |
| 887 | // FIXME: This leaks |
Mike Stump | f89230d | 2009-03-06 06:12:24 +0000 | [diff] [blame] | 888 | ImplicitParamDecl *Dst = |
| 889 | ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, |
| 890 | getContext().getPointerType(getContext().VoidTy)); |
| 891 | Args.push_back(std::make_pair(Dst, Dst->getType())); |
| 892 | |
| 893 | // FIXME: This leaks |
Mike Stump | ee2a5ee | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 894 | ImplicitParamDecl *Src = |
| 895 | ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, |
| 896 | getContext().getPointerType(getContext().VoidTy)); |
Mike Stump | ee2a5ee | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 897 | Args.push_back(std::make_pair(Src, Src->getType())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 898 | |
Mike Stump | ee2a5ee | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 899 | const CGFunctionInfo &FI = |
| 900 | CGM.getTypes().getFunctionInfo(R, Args); |
| 901 | |
| 902 | std::string Name = std::string("__Block_byref_id_object_copy_"); |
| 903 | CodeGenTypes &Types = CGM.getTypes(); |
| 904 | const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); |
| 905 | |
Mike Stump | cbc2bca | 2009-06-05 23:26:36 +0000 | [diff] [blame] | 906 | // FIXME: We'd like to put these into a mergable by content, with |
| 907 | // internal linkage. |
Mike Stump | ee2a5ee | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 908 | llvm::Function *Fn = |
| 909 | llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, |
| 910 | Name, |
| 911 | &CGM.getModule()); |
| 912 | |
| 913 | IdentifierInfo *II |
| 914 | = &CGM.getContext().Idents.get("__Block_byref_id_object_copy_"); |
| 915 | |
| 916 | FunctionDecl *FD = FunctionDecl::Create(getContext(), |
| 917 | getContext().getTranslationUnitDecl(), |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 918 | SourceLocation(), II, R, 0, |
Mike Stump | ee2a5ee | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 919 | FunctionDecl::Static, false, |
| 920 | true); |
| 921 | CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); |
Mike Stump | f89230d | 2009-03-06 06:12:24 +0000 | [diff] [blame] | 922 | |
| 923 | // dst->x |
| 924 | llvm::Value *V = CGF.GetAddrOfLocalVar(Dst); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 925 | V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0)); |
Mike Stump | 3b65ac2 | 2009-04-15 22:11:36 +0000 | [diff] [blame] | 926 | V = Builder.CreateLoad(V); |
Mike Stump | f89230d | 2009-03-06 06:12:24 +0000 | [diff] [blame] | 927 | V = Builder.CreateStructGEP(V, 6, "x"); |
| 928 | llvm::Value *DstObj = Builder.CreateBitCast(V, PtrToInt8Ty); |
| 929 | |
| 930 | // src->x |
| 931 | V = CGF.GetAddrOfLocalVar(Src); |
| 932 | V = Builder.CreateLoad(V); |
| 933 | V = Builder.CreateBitCast(V, T); |
| 934 | V = Builder.CreateStructGEP(V, 6, "x"); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 935 | V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0)); |
Mike Stump | f89230d | 2009-03-06 06:12:24 +0000 | [diff] [blame] | 936 | llvm::Value *SrcObj = Builder.CreateLoad(V); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 937 | |
Mike Stump | f89230d | 2009-03-06 06:12:24 +0000 | [diff] [blame] | 938 | flag |= BLOCK_BYREF_CALLER; |
| 939 | |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 940 | llvm::Value *N = llvm::ConstantInt::get( |
| 941 | llvm::Type::getInt32Ty(T->getContext()), flag); |
Mike Stump | f89230d | 2009-03-06 06:12:24 +0000 | [diff] [blame] | 942 | llvm::Value *F = getBlockObjectAssign(); |
| 943 | Builder.CreateCall3(F, DstObj, SrcObj, N); |
| 944 | |
Mike Stump | ee2a5ee | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 945 | CGF.FinishFunction(); |
| 946 | |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 947 | return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); |
Mike Stump | ee2a5ee | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 948 | } |
| 949 | |
Mike Stump | fbe25dd | 2009-03-06 04:53:30 +0000 | [diff] [blame] | 950 | llvm::Constant * |
| 951 | BlockFunction::GeneratebyrefDestroyHelperFunction(const llvm::Type *T, |
| 952 | int flag) { |
Mike Stump | ee2a5ee | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 953 | QualType R = getContext().VoidTy; |
| 954 | |
| 955 | FunctionArgList Args; |
| 956 | // FIXME: This leaks |
| 957 | ImplicitParamDecl *Src = |
| 958 | ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, |
| 959 | getContext().getPointerType(getContext().VoidTy)); |
| 960 | |
| 961 | Args.push_back(std::make_pair(Src, Src->getType())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 962 | |
Mike Stump | ee2a5ee | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 963 | const CGFunctionInfo &FI = |
| 964 | CGM.getTypes().getFunctionInfo(R, Args); |
| 965 | |
| 966 | std::string Name = std::string("__Block_byref_id_object_dispose_"); |
| 967 | CodeGenTypes &Types = CGM.getTypes(); |
| 968 | const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); |
| 969 | |
Mike Stump | cbc2bca | 2009-06-05 23:26:36 +0000 | [diff] [blame] | 970 | // FIXME: We'd like to put these into a mergable by content, with |
| 971 | // internal linkage. |
Mike Stump | ee2a5ee | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 972 | llvm::Function *Fn = |
| 973 | llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, |
| 974 | Name, |
| 975 | &CGM.getModule()); |
| 976 | |
| 977 | IdentifierInfo *II |
| 978 | = &CGM.getContext().Idents.get("__Block_byref_id_object_dispose_"); |
| 979 | |
| 980 | FunctionDecl *FD = FunctionDecl::Create(getContext(), |
| 981 | getContext().getTranslationUnitDecl(), |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 982 | SourceLocation(), II, R, 0, |
Mike Stump | ee2a5ee | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 983 | FunctionDecl::Static, false, |
| 984 | true); |
| 985 | CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); |
Mike Stump | fbe25dd | 2009-03-06 04:53:30 +0000 | [diff] [blame] | 986 | |
| 987 | llvm::Value *V = CGF.GetAddrOfLocalVar(Src); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 988 | V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0)); |
Mike Stump | 3b65ac2 | 2009-04-15 22:11:36 +0000 | [diff] [blame] | 989 | V = Builder.CreateLoad(V); |
Mike Stump | fbe25dd | 2009-03-06 04:53:30 +0000 | [diff] [blame] | 990 | V = Builder.CreateStructGEP(V, 6, "x"); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 991 | V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0)); |
Mike Stump | 3b65ac2 | 2009-04-15 22:11:36 +0000 | [diff] [blame] | 992 | V = Builder.CreateLoad(V); |
Mike Stump | fbe25dd | 2009-03-06 04:53:30 +0000 | [diff] [blame] | 993 | |
Mike Stump | fbe25dd | 2009-03-06 04:53:30 +0000 | [diff] [blame] | 994 | flag |= BLOCK_BYREF_CALLER; |
| 995 | BuildBlockRelease(V, flag); |
Mike Stump | ee2a5ee | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 996 | CGF.FinishFunction(); |
| 997 | |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 998 | return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); |
Mike Stump | ee2a5ee | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 999 | } |
| 1000 | |
Mike Stump | f89230d | 2009-03-06 06:12:24 +0000 | [diff] [blame] | 1001 | llvm::Constant *BlockFunction::BuildbyrefCopyHelper(const llvm::Type *T, |
Mike Stump | cbc2bca | 2009-06-05 23:26:36 +0000 | [diff] [blame] | 1002 | int flag, unsigned Align) { |
| 1003 | // All alignments below that of pointer alignment collpase down to just |
| 1004 | // pointer alignment, as we always have at least that much alignment to begin |
| 1005 | // with. |
| 1006 | Align /= unsigned(CGF.Target.getPointerAlign(0)/8); |
| 1007 | // As an optimization, we only generate a single function of each kind we |
| 1008 | // might need. We need a different one for each alignment and for each |
| 1009 | // setting of flags. We mix Align and flag to get the kind. |
| 1010 | uint64_t kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + flag; |
| 1011 | llvm::Constant *& Entry = CGM.AssignCache[kind]; |
| 1012 | if (Entry) |
| 1013 | return Entry; |
| 1014 | return Entry=CodeGenFunction(CGM).GeneratebyrefCopyHelperFunction(T, flag); |
Mike Stump | ee2a5ee | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1015 | } |
| 1016 | |
Mike Stump | fbe25dd | 2009-03-06 04:53:30 +0000 | [diff] [blame] | 1017 | llvm::Constant *BlockFunction::BuildbyrefDestroyHelper(const llvm::Type *T, |
Mike Stump | cbc2bca | 2009-06-05 23:26:36 +0000 | [diff] [blame] | 1018 | int flag, |
| 1019 | unsigned Align) { |
| 1020 | // All alignments below that of pointer alignment collpase down to just |
| 1021 | // pointer alignment, as we always have at least that much alignment to begin |
| 1022 | // with. |
| 1023 | Align /= unsigned(CGF.Target.getPointerAlign(0)/8); |
| 1024 | // As an optimization, we only generate a single function of each kind we |
| 1025 | // might need. We need a different one for each alignment and for each |
| 1026 | // setting of flags. We mix Align and flag to get the kind. |
| 1027 | uint64_t kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + flag; |
| 1028 | llvm::Constant *& Entry = CGM.DestroyCache[kind]; |
| 1029 | if (Entry) |
| 1030 | return Entry; |
| 1031 | return Entry=CodeGenFunction(CGM).GeneratebyrefDestroyHelperFunction(T, flag); |
Mike Stump | ee2a5ee | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1032 | } |
| 1033 | |
Mike Stump | 626aecc | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 1034 | llvm::Value *BlockFunction::getBlockObjectDispose() { |
| 1035 | if (CGM.BlockObjectDispose == 0) { |
| 1036 | const llvm::FunctionType *FTy; |
| 1037 | std::vector<const llvm::Type*> ArgTys; |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1038 | const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext); |
Mike Stump | 626aecc | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 1039 | ArgTys.push_back(PtrToInt8Ty); |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1040 | ArgTys.push_back(llvm::Type::getInt32Ty(VMContext)); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1041 | FTy = llvm::FunctionType::get(ResultType, ArgTys, false); |
Mike Stump | 4446dcf | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 1042 | CGM.BlockObjectDispose |
Mike Stump | 626aecc | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 1043 | = CGM.CreateRuntimeFunction(FTy, "_Block_object_dispose"); |
| 1044 | } |
| 1045 | return CGM.BlockObjectDispose; |
| 1046 | } |
| 1047 | |
Mike Stump | f89230d | 2009-03-06 06:12:24 +0000 | [diff] [blame] | 1048 | llvm::Value *BlockFunction::getBlockObjectAssign() { |
| 1049 | if (CGM.BlockObjectAssign == 0) { |
| 1050 | const llvm::FunctionType *FTy; |
| 1051 | std::vector<const llvm::Type*> ArgTys; |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1052 | const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext); |
Mike Stump | f89230d | 2009-03-06 06:12:24 +0000 | [diff] [blame] | 1053 | ArgTys.push_back(PtrToInt8Ty); |
| 1054 | ArgTys.push_back(PtrToInt8Ty); |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1055 | ArgTys.push_back(llvm::Type::getInt32Ty(VMContext)); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1056 | FTy = llvm::FunctionType::get(ResultType, ArgTys, false); |
Mike Stump | f89230d | 2009-03-06 06:12:24 +0000 | [diff] [blame] | 1057 | CGM.BlockObjectAssign |
| 1058 | = CGM.CreateRuntimeFunction(FTy, "_Block_object_assign"); |
| 1059 | } |
| 1060 | return CGM.BlockObjectAssign; |
| 1061 | } |
| 1062 | |
Mike Stump | fbe25dd | 2009-03-06 04:53:30 +0000 | [diff] [blame] | 1063 | void BlockFunction::BuildBlockRelease(llvm::Value *V, int flag) { |
Mike Stump | 626aecc | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 1064 | llvm::Value *F = getBlockObjectDispose(); |
Mike Stump | fbe25dd | 2009-03-06 04:53:30 +0000 | [diff] [blame] | 1065 | llvm::Value *N; |
Mike Stump | 626aecc | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 1066 | V = Builder.CreateBitCast(V, PtrToInt8Ty); |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1067 | N = llvm::ConstantInt::get(llvm::Type::getInt32Ty(V->getContext()), flag); |
Mike Stump | 626aecc | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 1068 | Builder.CreateCall2(F, V, N); |
| 1069 | } |
Mike Stump | 4446dcf | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 1070 | |
| 1071 | ASTContext &BlockFunction::getContext() const { return CGM.getContext(); } |
Mike Stump | aeb0ffd | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 1072 | |
| 1073 | BlockFunction::BlockFunction(CodeGenModule &cgm, CodeGenFunction &cgf, |
| 1074 | CGBuilderTy &B) |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1075 | : CGM(cgm), CGF(cgf), VMContext(cgm.getLLVMContext()), Builder(B) { |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1076 | PtrToInt8Ty = llvm::PointerType::getUnqual( |
| 1077 | llvm::Type::getInt8Ty(VMContext)); |
Mike Stump | aeb0ffd | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 1078 | |
| 1079 | BlockHasCopyDispose = false; |
| 1080 | } |