Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 1 | //===--- CGBlocks.cpp - Emit LLVM Code for declarations -------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This contains code to emit blocks. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 14 | #include "CGDebugInfo.h" |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 15 | #include "CodeGenFunction.h" |
Fariborz Jahanian | 263c4de | 2010-02-10 23:34:57 +0000 | [diff] [blame] | 16 | #include "CGObjCRuntime.h" |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 17 | #include "CodeGenModule.h" |
Mike Stump | 6cc88f7 | 2009-03-20 21:53:12 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 19 | #include "llvm/Module.h" |
Benjamin Kramer | 6876fe6 | 2010-03-31 15:04:05 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SmallSet.h" |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 21 | #include "llvm/Target/TargetData.h" |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 22 | #include <algorithm> |
Torok Edwin | f42e4a6 | 2009-08-24 13:25:12 +0000 | [diff] [blame] | 23 | |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 24 | using namespace clang; |
| 25 | using namespace CodeGen; |
| 26 | |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 27 | /// CGBlockInfo - Information to generate a block literal. |
| 28 | class clang::CodeGen::CGBlockInfo { |
| 29 | public: |
| 30 | /// Name - The name of the block, kindof. |
| 31 | const char *Name; |
| 32 | |
| 33 | /// DeclRefs - Variables from parent scopes that have been |
| 34 | /// imported into this block. |
| 35 | llvm::SmallVector<const BlockDeclRefExpr *, 8> DeclRefs; |
| 36 | |
| 37 | /// InnerBlocks - This block and the blocks it encloses. |
| 38 | llvm::SmallPtrSet<const DeclContext *, 4> InnerBlocks; |
| 39 | |
| 40 | /// CXXThisRef - Non-null if 'this' was required somewhere, in |
| 41 | /// which case this is that expression. |
| 42 | const CXXThisExpr *CXXThisRef; |
| 43 | |
| 44 | /// NeedsObjCSelf - True if something in this block has an implicit |
| 45 | /// reference to 'self'. |
| 46 | bool NeedsObjCSelf; |
| 47 | |
| 48 | /// These are initialized by GenerateBlockFunction. |
| 49 | bool BlockHasCopyDispose; |
| 50 | CharUnits BlockSize; |
| 51 | CharUnits BlockAlign; |
| 52 | llvm::SmallVector<const Expr*, 8> BlockLayout; |
| 53 | |
| 54 | CGBlockInfo(const char *Name); |
| 55 | }; |
| 56 | |
| 57 | CGBlockInfo::CGBlockInfo(const char *N) |
| 58 | : Name(N), CXXThisRef(0), NeedsObjCSelf(false) { |
| 59 | |
| 60 | // Skip asm prefix, if any. |
| 61 | if (Name && Name[0] == '\01') |
| 62 | ++Name; |
| 63 | } |
| 64 | |
| 65 | |
Mike Stump | cf62d39 | 2009-03-06 18:42:23 +0000 | [diff] [blame] | 66 | llvm::Constant *CodeGenFunction:: |
Blaine Garst | 2a7eb28 | 2010-02-23 21:51:17 +0000 | [diff] [blame] | 67 | BuildDescriptorBlockDecl(const BlockExpr *BE, bool BlockHasCopyDispose, CharUnits Size, |
Mike Stump | a803b0e | 2009-03-25 17:58:24 +0000 | [diff] [blame] | 68 | const llvm::StructType* Ty, |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 69 | std::vector<HelperInfo> *NoteForHelper) { |
Mike Stump | 56129b1 | 2009-02-13 16:55:51 +0000 | [diff] [blame] | 70 | const llvm::Type *UnsignedLongTy |
| 71 | = CGM.getTypes().ConvertType(getContext().UnsignedLongTy); |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 72 | llvm::Constant *C; |
| 73 | std::vector<llvm::Constant*> Elts; |
| 74 | |
| 75 | // reserved |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 76 | C = llvm::ConstantInt::get(UnsignedLongTy, 0); |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 77 | Elts.push_back(C); |
| 78 | |
| 79 | // Size |
Mike Stump | d684000 | 2009-02-21 20:07:44 +0000 | [diff] [blame] | 80 | // FIXME: What is the right way to say this doesn't fit? We should give |
| 81 | // a user diagnostic in that case. Better fix would be to change the |
| 82 | // API to size_t. |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 83 | C = llvm::ConstantInt::get(UnsignedLongTy, Size.getQuantity()); |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 84 | Elts.push_back(C); |
| 85 | |
Blaine Garst | 2a7eb28 | 2010-02-23 21:51:17 +0000 | [diff] [blame] | 86 | // optional copy/dispose helpers |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 87 | if (BlockHasCopyDispose) { |
| 88 | // copy_func_helper_decl |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 89 | Elts.push_back(BuildCopyHelper(Ty, NoteForHelper)); |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 90 | |
| 91 | // destroy_func_decl |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 92 | Elts.push_back(BuildDestroyHelper(Ty, NoteForHelper)); |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Blaine Garst | 2a7eb28 | 2010-02-23 21:51:17 +0000 | [diff] [blame] | 95 | // Signature. non-optional ObjC-style method descriptor @encode sequence |
| 96 | std::string BlockTypeEncoding; |
| 97 | CGM.getContext().getObjCEncodingForBlock(BE, BlockTypeEncoding); |
| 98 | |
| 99 | Elts.push_back(llvm::ConstantExpr::getBitCast( |
| 100 | CGM.GetAddrOfConstantCString(BlockTypeEncoding), PtrToInt8Ty)); |
| 101 | |
| 102 | // Layout. |
| 103 | C = llvm::ConstantInt::get(UnsignedLongTy, 0); |
| 104 | Elts.push_back(C); |
| 105 | |
Nick Lewycky | 0d36dd2 | 2009-09-19 20:00:52 +0000 | [diff] [blame] | 106 | C = llvm::ConstantStruct::get(VMContext, Elts, false); |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 107 | |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 108 | C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true, |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 109 | llvm::GlobalValue::InternalLinkage, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 110 | C, "__block_descriptor_tmp"); |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 111 | return C; |
| 112 | } |
| 113 | |
Mike Stump | 2a99814 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 114 | llvm::Constant *BlockModule::getNSConcreteGlobalBlock() { |
Chris Lattner | e2f79b6 | 2009-05-13 02:50:56 +0000 | [diff] [blame] | 115 | if (NSConcreteGlobalBlock == 0) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 116 | NSConcreteGlobalBlock = CGM.CreateRuntimeVariable(PtrToInt8Ty, |
Chris Lattner | e2f79b6 | 2009-05-13 02:50:56 +0000 | [diff] [blame] | 117 | "_NSConcreteGlobalBlock"); |
Mike Stump | f99f1d0 | 2009-02-13 17:23:42 +0000 | [diff] [blame] | 118 | return NSConcreteGlobalBlock; |
| 119 | } |
| 120 | |
Mike Stump | 2a99814 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 121 | llvm::Constant *BlockModule::getNSConcreteStackBlock() { |
Chris Lattner | e2f79b6 | 2009-05-13 02:50:56 +0000 | [diff] [blame] | 122 | if (NSConcreteStackBlock == 0) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 123 | NSConcreteStackBlock = CGM.CreateRuntimeVariable(PtrToInt8Ty, |
Chris Lattner | e2f79b6 | 2009-05-13 02:50:56 +0000 | [diff] [blame] | 124 | "_NSConcreteStackBlock"); |
Mike Stump | 59c5b11 | 2009-02-13 19:29:27 +0000 | [diff] [blame] | 125 | return NSConcreteStackBlock; |
| 126 | } |
| 127 | |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 128 | static void CollectBlockDeclRefInfo(const Stmt *S, CGBlockInfo &Info) { |
Anders Carlsson | 4de9fce | 2009-03-01 01:09:12 +0000 | [diff] [blame] | 129 | for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end(); |
| 130 | I != E; ++I) |
Daniel Dunbar | 82573ee | 2009-03-02 07:00:57 +0000 | [diff] [blame] | 131 | if (*I) |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 132 | CollectBlockDeclRefInfo(*I, Info); |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 133 | |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 134 | // We want to ensure we walk down into block literals so we can find |
| 135 | // all nested BlockDeclRefExprs. |
Mike Stump | 38e1627 | 2009-10-21 22:01:24 +0000 | [diff] [blame] | 136 | if (const BlockExpr *BE = dyn_cast<BlockExpr>(S)) { |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 137 | Info.InnerBlocks.insert(BE->getBlockDecl()); |
| 138 | CollectBlockDeclRefInfo(BE->getBody(), Info); |
Mike Stump | 38e1627 | 2009-10-21 22:01:24 +0000 | [diff] [blame] | 139 | } |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 140 | |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 141 | else if (const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(S)) { |
| 142 | const ValueDecl *D = BDRE->getDecl(); |
Anders Carlsson | 4de9fce | 2009-03-01 01:09:12 +0000 | [diff] [blame] | 143 | // FIXME: Handle enums. |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 144 | if (isa<FunctionDecl>(D)) |
Anders Carlsson | 4de9fce | 2009-03-01 01:09:12 +0000 | [diff] [blame] | 145 | return; |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 146 | |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 147 | if (isa<ImplicitParamDecl>(D) && |
| 148 | isa<ObjCMethodDecl>(D->getDeclContext()) && |
| 149 | cast<ObjCMethodDecl>(D->getDeclContext())->getSelfDecl() == D) { |
| 150 | Info.NeedsObjCSelf = true; |
| 151 | return; |
| 152 | } |
| 153 | |
Mike Stump | 38e1627 | 2009-10-21 22:01:24 +0000 | [diff] [blame] | 154 | // Only Decls that escape are added. |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 155 | if (!Info.InnerBlocks.count(D->getDeclContext())) |
Mike Stump | 38e1627 | 2009-10-21 22:01:24 +0000 | [diff] [blame] | 156 | Info.DeclRefs.push_back(BDRE); |
Anders Carlsson | 4de9fce | 2009-03-01 01:09:12 +0000 | [diff] [blame] | 157 | } |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 158 | |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 159 | // Make sure to capture implicit 'self' references due to super calls. |
Chandler Carruth | f54b80f | 2010-05-21 10:29:28 +0000 | [diff] [blame] | 160 | else if (const ObjCMessageExpr *E = dyn_cast<ObjCMessageExpr>(S)) { |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 161 | if (E->getReceiverKind() == ObjCMessageExpr::SuperClass || |
| 162 | E->getReceiverKind() == ObjCMessageExpr::SuperInstance) |
| 163 | Info.NeedsObjCSelf = true; |
Chandler Carruth | f54b80f | 2010-05-21 10:29:28 +0000 | [diff] [blame] | 164 | } |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 165 | |
| 166 | // Getter/setter uses may also cause implicit super references, |
| 167 | // which we can check for with: |
| 168 | else if (isa<ObjCSuperExpr>(S)) |
| 169 | Info.NeedsObjCSelf = true; |
| 170 | |
| 171 | else if (isa<CXXThisExpr>(S)) |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 172 | Info.CXXThisRef = cast<CXXThisExpr>(S); |
Anders Carlsson | 4de9fce | 2009-03-01 01:09:12 +0000 | [diff] [blame] | 173 | } |
| 174 | |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 175 | /// CanBlockBeGlobal - Given a CGBlockInfo struct, determines if a block can be |
Mike Stump | 58a8514 | 2009-03-04 22:48:06 +0000 | [diff] [blame] | 176 | /// declared as a global variable instead of on the stack. |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 177 | static bool CanBlockBeGlobal(const CGBlockInfo &Info) { |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 178 | return Info.DeclRefs.empty(); |
| 179 | } |
| 180 | |
| 181 | /// AllocateAllBlockDeclRefs - Preallocate all nested BlockDeclRefExprs to |
| 182 | /// ensure we can generate the debug information for the parameter for the block |
| 183 | /// invoke function. |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 184 | static void AllocateAllBlockDeclRefs(CodeGenFunction &CGF, CGBlockInfo &Info) { |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 185 | if (Info.CXXThisRef) |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 186 | CGF.AllocateBlockCXXThisPointer(Info.CXXThisRef); |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 187 | |
| 188 | for (size_t i = 0; i < Info.DeclRefs.size(); ++i) |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 189 | CGF.AllocateBlockDecl(Info.DeclRefs[i]); |
| 190 | |
| 191 | if (Info.NeedsObjCSelf) { |
| 192 | ValueDecl *Self = cast<ObjCMethodDecl>(CGF.CurFuncDecl)->getSelfDecl(); |
| 193 | BlockDeclRefExpr *BDRE = |
| 194 | new (CGF.getContext()) BlockDeclRefExpr(Self, Self->getType(), |
| 195 | SourceLocation(), false); |
| 196 | Info.DeclRefs.push_back(BDRE); |
| 197 | CGF.AllocateBlockDecl(BDRE); |
| 198 | } |
Anders Carlsson | 4de9fce | 2009-03-01 01:09:12 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Mike Stump | 58a8514 | 2009-03-04 22:48:06 +0000 | [diff] [blame] | 201 | // FIXME: Push most into CGM, passing down a few bits, like current function |
| 202 | // name. |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 203 | llvm::Value *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) { |
Anders Carlsson | 4de9fce | 2009-03-01 01:09:12 +0000 | [diff] [blame] | 204 | std::string Name = CurFn->getName(); |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 205 | CGBlockInfo Info(Name.c_str()); |
| 206 | Info.InnerBlocks.insert(BE->getBlockDecl()); |
| 207 | CollectBlockDeclRefInfo(BE->getBody(), Info); |
Anders Carlsson | 4de9fce | 2009-03-01 01:09:12 +0000 | [diff] [blame] | 208 | |
| 209 | // Check if the block can be global. |
Mike Stump | 58a8514 | 2009-03-04 22:48:06 +0000 | [diff] [blame] | 210 | // FIXME: This test doesn't work for nested blocks yet. Longer term, I'd like |
| 211 | // to just have one code path. We should move this function into CGM and pass |
| 212 | // CGF, then we can just check to see if CGF is 0. |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 213 | if (0 && CanBlockBeGlobal(Info)) |
Anders Carlsson | 4de9fce | 2009-03-01 01:09:12 +0000 | [diff] [blame] | 214 | return CGM.GetAddrOfGlobalBlock(BE, Name.c_str()); |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 215 | |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 216 | size_t BlockFields = 5; |
| 217 | |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 218 | std::vector<llvm::Constant*> Elts(BlockFields); |
| 219 | |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 220 | llvm::Constant *C; |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 221 | llvm::Value *V; |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 222 | |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 223 | { |
| 224 | // C = BuildBlockStructInitlist(); |
Blaine Garst | a36e223 | 2010-03-05 01:29:59 +0000 | [diff] [blame] | 225 | unsigned int flags = BLOCK_HAS_SIGNATURE; |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 226 | |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 227 | // We run this first so that we set BlockHasCopyDispose from the entire |
| 228 | // block literal. |
| 229 | // __invoke |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 230 | llvm::Function *Fn |
Mike Stump | bb30419 | 2009-09-24 22:31:14 +0000 | [diff] [blame] | 231 | = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, CurFuncDecl, |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 232 | LocalDeclMap); |
| 233 | BlockHasCopyDispose |= Info.BlockHasCopyDispose; |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 234 | Elts[3] = Fn; |
| 235 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 236 | // FIXME: Don't use BlockHasCopyDispose, it is set more often then |
| 237 | // necessary, for example: { ^{ __block int i; ^{ i = 1; }(); }(); } |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 238 | if (Info.BlockHasCopyDispose) |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 239 | flags |= BLOCK_HAS_COPY_DISPOSE; |
| 240 | |
Mike Stump | 7d6dc4f | 2009-02-13 20:17:16 +0000 | [diff] [blame] | 241 | // __isa |
Mike Stump | 59c5b11 | 2009-02-13 19:29:27 +0000 | [diff] [blame] | 242 | C = CGM.getNSConcreteStackBlock(); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 243 | C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty); |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 244 | Elts[0] = C; |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 245 | |
| 246 | // __flags |
Blaine Garst | a36e223 | 2010-03-05 01:29:59 +0000 | [diff] [blame] | 247 | { |
| 248 | QualType BPT = BE->getType(); |
| 249 | const FunctionType *ftype = BPT->getPointeeType()->getAs<FunctionType>(); |
| 250 | QualType ResultType = ftype->getResultType(); |
| 251 | |
| 252 | CallArgList Args; |
| 253 | CodeGenTypes &Types = CGM.getTypes(); |
| 254 | const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, Args, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 255 | FunctionType::ExtInfo()); |
Blaine Garst | a36e223 | 2010-03-05 01:29:59 +0000 | [diff] [blame] | 256 | if (CGM.ReturnTypeUsesSret(FnInfo)) |
| 257 | flags |= BLOCK_USE_STRET; |
| 258 | } |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 259 | const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( |
| 260 | CGM.getTypes().ConvertType(CGM.getContext().IntTy)); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 261 | C = llvm::ConstantInt::get(IntTy, flags); |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 262 | Elts[1] = C; |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 263 | |
| 264 | // __reserved |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 265 | C = llvm::ConstantInt::get(IntTy, 0); |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 266 | Elts[2] = C; |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 267 | |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 268 | if (Info.BlockLayout.empty()) { |
Mike Stump | cf62d39 | 2009-03-06 18:42:23 +0000 | [diff] [blame] | 269 | // __descriptor |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 270 | Elts[4] = BuildDescriptorBlockDecl(BE, Info.BlockHasCopyDispose, |
| 271 | Info.BlockSize, 0, 0); |
Mike Stump | cf62d39 | 2009-03-06 18:42:23 +0000 | [diff] [blame] | 272 | |
Mike Stump | 5570cfe | 2009-03-01 20:07:53 +0000 | [diff] [blame] | 273 | // Optimize to being a global block. |
| 274 | Elts[0] = CGM.getNSConcreteGlobalBlock(); |
Blaine Garst | a36e223 | 2010-03-05 01:29:59 +0000 | [diff] [blame] | 275 | |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 276 | Elts[1] = llvm::ConstantInt::get(IntTy, flags|BLOCK_IS_GLOBAL); |
Mike Stump | 5570cfe | 2009-03-01 20:07:53 +0000 | [diff] [blame] | 277 | |
Nick Lewycky | 0d36dd2 | 2009-09-19 20:00:52 +0000 | [diff] [blame] | 278 | C = llvm::ConstantStruct::get(VMContext, Elts, false); |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 279 | |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 280 | C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true, |
Benjamin Kramer | 3cf7c5d | 2010-01-22 13:59:13 +0000 | [diff] [blame] | 281 | llvm::GlobalValue::InternalLinkage, C, |
| 282 | "__block_holder_tmp_" + |
| 283 | llvm::Twine(CGM.getGlobalUniqueCount())); |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 284 | QualType BPT = BE->getType(); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 285 | C = llvm::ConstantExpr::getBitCast(C, ConvertType(BPT)); |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 286 | return C; |
| 287 | } |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 288 | |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 289 | std::vector<const llvm::Type *> Types(BlockFields+Info.BlockLayout.size()); |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 290 | for (int i=0; i<4; ++i) |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 291 | Types[i] = Elts[i]->getType(); |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 292 | Types[4] = PtrToInt8Ty; |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 293 | |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 294 | for (unsigned i = 0, n = Info.BlockLayout.size(); i != n; ++i) { |
| 295 | const Expr *E = Info.BlockLayout[i]; |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 296 | const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E); |
| 297 | QualType Ty = E->getType(); |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 298 | if (BDRE && BDRE->isByRef()) { |
Fariborz Jahanian | 7965172 | 2010-06-02 21:35:17 +0000 | [diff] [blame] | 299 | Types[i+BlockFields] = |
| 300 | llvm::PointerType::get(BuildByRefType(BDRE->getDecl()), 0); |
| 301 | } else if (BDRE && BDRE->getDecl()->getType()->isReferenceType()) { |
| 302 | Types[i+BlockFields] = llvm::PointerType::get(ConvertType(Ty), 0); |
| 303 | } else |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 304 | Types[i+BlockFields] = ConvertType(Ty); |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 305 | } |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 306 | |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 307 | llvm::StructType *Ty = llvm::StructType::get(VMContext, Types, true); |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 308 | |
| 309 | llvm::AllocaInst *A = CreateTempAlloca(Ty); |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 310 | A->setAlignment(Info.BlockAlign.getQuantity()); |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 311 | V = A; |
| 312 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 313 | // Build layout / cleanup information for all the data entries in the |
| 314 | // layout, and write the enclosing fields into the type. |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 315 | std::vector<HelperInfo> NoteForHelper(Info.BlockLayout.size()); |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 316 | unsigned NumHelpers = 0; |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 317 | |
| 318 | for (unsigned i=0; i<4; ++i) |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 319 | Builder.CreateStore(Elts[i], Builder.CreateStructGEP(V, i, "block.tmp")); |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 320 | |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 321 | for (unsigned i=0; i < Info.BlockLayout.size(); ++i) { |
| 322 | const Expr *E = Info.BlockLayout[i]; |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 323 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 324 | // Skip padding. |
| 325 | if (isa<DeclRefExpr>(E)) continue; |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 326 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 327 | llvm::Value* Addr = Builder.CreateStructGEP(V, i+BlockFields, "tmp"); |
| 328 | HelperInfo &Note = NoteForHelper[NumHelpers++]; |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 329 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 330 | Note.index = i+5; |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 331 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 332 | if (isa<CXXThisExpr>(E)) { |
| 333 | Note.RequiresCopying = false; |
| 334 | Note.flag = BLOCK_FIELD_IS_OBJECT; |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 335 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 336 | Builder.CreateStore(LoadCXXThis(), Addr); |
| 337 | continue; |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 338 | } |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 339 | |
| 340 | const BlockDeclRefExpr *BDRE = cast<BlockDeclRefExpr>(E); |
| 341 | const ValueDecl *VD = BDRE->getDecl(); |
| 342 | QualType T = VD->getType(); |
| 343 | |
| 344 | Note.RequiresCopying = BlockRequiresCopying(T); |
| 345 | |
| 346 | if (BDRE->isByRef()) { |
| 347 | Note.flag = BLOCK_FIELD_IS_BYREF; |
| 348 | if (T.isObjCGCWeak()) |
| 349 | Note.flag |= BLOCK_FIELD_IS_WEAK; |
| 350 | } else if (T->isBlockPointerType()) { |
| 351 | Note.flag = BLOCK_FIELD_IS_BLOCK; |
| 352 | } else { |
| 353 | Note.flag = BLOCK_FIELD_IS_OBJECT; |
| 354 | } |
| 355 | |
| 356 | if (LocalDeclMap[VD]) { |
| 357 | if (BDRE->isByRef()) { |
| 358 | llvm::Value *Loc = LocalDeclMap[VD]; |
| 359 | Loc = Builder.CreateStructGEP(Loc, 1, "forwarding"); |
| 360 | Loc = Builder.CreateLoad(Loc); |
| 361 | Builder.CreateStore(Loc, Addr); |
| 362 | continue; |
| 363 | } else { |
| 364 | E = new (getContext()) DeclRefExpr(const_cast<ValueDecl*>(VD), |
Fariborz Jahanian | 7965172 | 2010-06-02 21:35:17 +0000 | [diff] [blame] | 365 | VD->getType().getNonReferenceType(), |
| 366 | SourceLocation()); |
Fariborz Jahanian | df8b8ea | 2010-06-04 16:10:00 +0000 | [diff] [blame^] | 367 | if (getContext().getLangOptions().CPlusPlus) { |
| 368 | if (VD->getType()->isReferenceType()) { |
| 369 | E = new (getContext()) |
| 370 | UnaryOperator(const_cast<Expr*>(E), UnaryOperator::AddrOf, |
| 371 | getContext().getPointerType(E->getType()), |
| 372 | SourceLocation()); |
| 373 | } |
| 374 | else { |
| 375 | QualType T = E->getType(); |
| 376 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 377 | CXXRecordDecl *Record = cast<CXXRecordDecl>(RT->getDecl()); |
| 378 | if (!Record->hasTrivialCopyConstructor()) { |
| 379 | CXXConstructorDecl *D = Record->getCopyConstructor(getContext(), |
| 380 | 0); |
| 381 | Expr *Arg = const_cast<Expr*>(E); |
| 382 | E = CXXConstructExpr::Create(getContext(), T, D->getLocation(), |
| 383 | D, false, &Arg, 1, false, |
| 384 | CXXConstructExpr::CK_Complete); |
| 385 | } |
| 386 | } |
| 387 | } |
| 388 | } |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 389 | } |
| 390 | } |
| 391 | |
| 392 | if (BDRE->isByRef()) { |
| 393 | E = new (getContext()) |
| 394 | UnaryOperator(const_cast<Expr*>(E), UnaryOperator::AddrOf, |
| 395 | getContext().getPointerType(E->getType()), |
| 396 | SourceLocation()); |
| 397 | } |
| 398 | |
| 399 | RValue r = EmitAnyExpr(E, Addr, false); |
| 400 | if (r.isScalar()) { |
| 401 | llvm::Value *Loc = r.getScalarVal(); |
| 402 | const llvm::Type *Ty = Types[i+BlockFields]; |
| 403 | if (BDRE->isByRef()) { |
| 404 | // E is now the address of the value field, instead, we want the |
| 405 | // address of the actual ByRef struct. We optimize this slightly |
| 406 | // compared to gcc by not grabbing the forwarding slot as this must |
| 407 | // be done during Block_copy for us, and we can postpone the work |
| 408 | // until then. |
| 409 | CharUnits offset = BlockDecls[BDRE->getDecl()]; |
| 410 | |
| 411 | llvm::Value *BlockLiteral = LoadBlockStruct(); |
| 412 | |
| 413 | Loc = Builder.CreateGEP(BlockLiteral, |
| 414 | llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), |
| 415 | offset.getQuantity()), |
| 416 | "block.literal"); |
| 417 | Ty = llvm::PointerType::get(Ty, 0); |
| 418 | Loc = Builder.CreateBitCast(Loc, Ty); |
| 419 | Loc = Builder.CreateLoad(Loc); |
| 420 | // Loc = Builder.CreateBitCast(Loc, Ty); |
| 421 | } |
| 422 | Builder.CreateStore(Loc, Addr); |
| 423 | } else if (r.isComplex()) |
| 424 | // FIXME: implement |
| 425 | ErrorUnsupported(BE, "complex in block literal"); |
| 426 | else if (r.isAggregate()) |
| 427 | ; // Already created into the destination |
| 428 | else |
| 429 | assert (0 && "bad block variable"); |
| 430 | // FIXME: Ensure that the offset created by the backend for |
| 431 | // the struct matches the previously computed offset in BlockDecls. |
| 432 | } |
| 433 | NoteForHelper.resize(NumHelpers); |
Mike Stump | cf62d39 | 2009-03-06 18:42:23 +0000 | [diff] [blame] | 434 | |
| 435 | // __descriptor |
Blaine Garst | 2a7eb28 | 2010-02-23 21:51:17 +0000 | [diff] [blame] | 436 | llvm::Value *Descriptor = BuildDescriptorBlockDecl(BE, |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 437 | Info.BlockHasCopyDispose, |
| 438 | Info.BlockSize, Ty, |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 439 | &NoteForHelper); |
| 440 | Descriptor = Builder.CreateBitCast(Descriptor, PtrToInt8Ty); |
| 441 | Builder.CreateStore(Descriptor, Builder.CreateStructGEP(V, 4, "block.tmp")); |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 442 | } |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 443 | |
Mike Stump | bd65cac | 2009-02-19 01:01:04 +0000 | [diff] [blame] | 444 | QualType BPT = BE->getType(); |
Fariborz Jahanian | 263c4de | 2010-02-10 23:34:57 +0000 | [diff] [blame] | 445 | V = Builder.CreateBitCast(V, ConvertType(BPT)); |
| 446 | // See if this is a __weak block variable and the must call objc_read_weak |
| 447 | // on it. |
| 448 | const FunctionType *ftype = BPT->getPointeeType()->getAs<FunctionType>(); |
| 449 | QualType RES = ftype->getResultType(); |
| 450 | if (RES.isObjCGCWeak()) { |
| 451 | // Must cast argument to id* |
| 452 | const llvm::Type *ObjectPtrTy = |
| 453 | ConvertType(CGM.getContext().getObjCIdType()); |
| 454 | const llvm::Type *PtrObjectPtrTy = |
| 455 | llvm::PointerType::getUnqual(ObjectPtrTy); |
| 456 | V = Builder.CreateBitCast(V, PtrObjectPtrTy); |
| 457 | V = CGM.getObjCRuntime().EmitObjCWeakRead(*this, V); |
| 458 | } |
| 459 | return V; |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | |
Mike Stump | 2a99814 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 463 | const llvm::Type *BlockModule::getBlockDescriptorType() { |
Mike Stump | ab69514 | 2009-02-13 15:16:56 +0000 | [diff] [blame] | 464 | if (BlockDescriptorType) |
| 465 | return BlockDescriptorType; |
| 466 | |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 467 | const llvm::Type *UnsignedLongTy = |
Mike Stump | ab69514 | 2009-02-13 15:16:56 +0000 | [diff] [blame] | 468 | getTypes().ConvertType(getContext().UnsignedLongTy); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 469 | |
Mike Stump | ab69514 | 2009-02-13 15:16:56 +0000 | [diff] [blame] | 470 | // struct __block_descriptor { |
| 471 | // unsigned long reserved; |
| 472 | // unsigned long block_size; |
Blaine Garst | 2a7eb28 | 2010-02-23 21:51:17 +0000 | [diff] [blame] | 473 | // |
| 474 | // // later, the following will be added |
| 475 | // |
| 476 | // struct { |
| 477 | // void (*copyHelper)(); |
| 478 | // void (*copyHelper)(); |
| 479 | // } helpers; // !!! optional |
| 480 | // |
| 481 | // const char *signature; // the block signature |
| 482 | // const char *layout; // reserved |
Mike Stump | ab69514 | 2009-02-13 15:16:56 +0000 | [diff] [blame] | 483 | // }; |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 484 | BlockDescriptorType = llvm::StructType::get(UnsignedLongTy->getContext(), |
| 485 | UnsignedLongTy, |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 486 | UnsignedLongTy, |
Mike Stump | ab69514 | 2009-02-13 15:16:56 +0000 | [diff] [blame] | 487 | NULL); |
| 488 | |
| 489 | getModule().addTypeName("struct.__block_descriptor", |
| 490 | BlockDescriptorType); |
| 491 | |
| 492 | return BlockDescriptorType; |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 493 | } |
| 494 | |
Mike Stump | 2a99814 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 495 | const llvm::Type *BlockModule::getGenericBlockLiteralType() { |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 496 | if (GenericBlockLiteralType) |
| 497 | return GenericBlockLiteralType; |
| 498 | |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 499 | const llvm::Type *BlockDescPtrTy = |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 500 | llvm::PointerType::getUnqual(getBlockDescriptorType()); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 501 | |
Mike Stump | 7cbb360 | 2009-02-13 16:01:35 +0000 | [diff] [blame] | 502 | const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( |
| 503 | getTypes().ConvertType(getContext().IntTy)); |
| 504 | |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 505 | // struct __block_literal_generic { |
Mike Stump | bd65cac | 2009-02-19 01:01:04 +0000 | [diff] [blame] | 506 | // void *__isa; |
| 507 | // int __flags; |
| 508 | // int __reserved; |
| 509 | // void (*__invoke)(void *); |
| 510 | // struct __block_descriptor *__descriptor; |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 511 | // }; |
Blaine Garst | 2a7eb28 | 2010-02-23 21:51:17 +0000 | [diff] [blame] | 512 | GenericBlockLiteralType = llvm::StructType::get(IntTy->getContext(), |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 513 | PtrToInt8Ty, |
Mike Stump | 7cbb360 | 2009-02-13 16:01:35 +0000 | [diff] [blame] | 514 | IntTy, |
| 515 | IntTy, |
Mike Stump | 797b632 | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 516 | PtrToInt8Ty, |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 517 | BlockDescPtrTy, |
| 518 | NULL); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 519 | |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 520 | getModule().addTypeName("struct.__block_literal_generic", |
| 521 | GenericBlockLiteralType); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 522 | |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 523 | return GenericBlockLiteralType; |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 524 | } |
| 525 | |
Mike Stump | bd65cac | 2009-02-19 01:01:04 +0000 | [diff] [blame] | 526 | |
Anders Carlsson | a1736c0 | 2009-12-24 21:13:40 +0000 | [diff] [blame] | 527 | RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E, |
| 528 | ReturnValueSlot ReturnValue) { |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 529 | const BlockPointerType *BPT = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 530 | E->getCallee()->getType()->getAs<BlockPointerType>(); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 531 | |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 532 | llvm::Value *Callee = EmitScalarExpr(E->getCallee()); |
| 533 | |
| 534 | // Get a pointer to the generic block literal. |
| 535 | const llvm::Type *BlockLiteralTy = |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 536 | llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType()); |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 537 | |
| 538 | // Bitcast the callee to a block literal. |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 539 | llvm::Value *BlockLiteral = |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 540 | Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal"); |
| 541 | |
| 542 | // Get the function pointer from the literal. |
| 543 | llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp"); |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 544 | |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 545 | BlockLiteral = |
| 546 | Builder.CreateBitCast(BlockLiteral, |
Benjamin Kramer | 3c0ef8c | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 547 | llvm::Type::getInt8PtrTy(VMContext), |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 548 | "tmp"); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 549 | |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 550 | // Add the block literal. |
| 551 | QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy); |
| 552 | CallArgList Args; |
| 553 | Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy)); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 554 | |
Anders Carlsson | 782f397 | 2009-04-08 23:13:16 +0000 | [diff] [blame] | 555 | QualType FnType = BPT->getPointeeType(); |
| 556 | |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 557 | // And the rest of the arguments. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 558 | EmitCallArgs(Args, FnType->getAs<FunctionProtoType>(), |
Anders Carlsson | 782f397 | 2009-04-08 23:13:16 +0000 | [diff] [blame] | 559 | E->arg_begin(), E->arg_end()); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 560 | |
Anders Carlsson | 6e460ff | 2009-04-07 22:10:22 +0000 | [diff] [blame] | 561 | // Load the function. |
Daniel Dunbar | 2da84ff | 2009-11-29 21:23:36 +0000 | [diff] [blame] | 562 | llvm::Value *Func = Builder.CreateLoad(FuncPtr, "tmp"); |
Anders Carlsson | 6e460ff | 2009-04-07 22:10:22 +0000 | [diff] [blame] | 563 | |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 564 | const FunctionType *FuncTy = FnType->getAs<FunctionType>(); |
| 565 | QualType ResultType = FuncTy->getResultType(); |
Anders Carlsson | a17d7cc | 2009-04-08 02:55:55 +0000 | [diff] [blame] | 566 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 567 | const CGFunctionInfo &FnInfo = |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 568 | CGM.getTypes().getFunctionInfo(ResultType, Args, |
| 569 | FuncTy->getExtInfo()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 570 | |
Anders Carlsson | 6e460ff | 2009-04-07 22:10:22 +0000 | [diff] [blame] | 571 | // Cast the function pointer to the right type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 572 | const llvm::Type *BlockFTy = |
Anders Carlsson | a17d7cc | 2009-04-08 02:55:55 +0000 | [diff] [blame] | 573 | CGM.getTypes().GetFunctionType(FnInfo, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 574 | |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 575 | const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy); |
Anders Carlsson | 6e460ff | 2009-04-07 22:10:22 +0000 | [diff] [blame] | 576 | Func = Builder.CreateBitCast(Func, BlockFTyPtr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 577 | |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 578 | // And call the block. |
Anders Carlsson | a1736c0 | 2009-12-24 21:13:40 +0000 | [diff] [blame] | 579 | return EmitCall(FnInfo, Func, ReturnValue, Args); |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 580 | } |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 581 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 582 | void CodeGenFunction::AllocateBlockCXXThisPointer(const CXXThisExpr *E) { |
| 583 | assert(BlockCXXThisOffset.isZero() && "already computed 'this' pointer"); |
| 584 | |
| 585 | // Figure out what the offset is. |
| 586 | QualType T = E->getType(); |
| 587 | std::pair<CharUnits,CharUnits> TypeInfo = getContext().getTypeInfoInChars(T); |
| 588 | CharUnits Offset = getBlockOffset(TypeInfo.first, TypeInfo.second); |
| 589 | |
| 590 | BlockCXXThisOffset = Offset; |
| 591 | BlockLayout.push_back(E); |
| 592 | } |
| 593 | |
| 594 | void CodeGenFunction::AllocateBlockDecl(const BlockDeclRefExpr *E) { |
Anders Carlsson | 7dfa407 | 2009-09-12 02:14:24 +0000 | [diff] [blame] | 595 | const ValueDecl *VD = E->getDecl(); |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 596 | CharUnits &Offset = BlockDecls[VD]; |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 597 | |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 598 | // See if we have already allocated an offset for this variable. |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 599 | if (!Offset.isZero()) |
| 600 | return; |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 601 | |
| 602 | // Don't run the expensive check, unless we have to. |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 603 | if (!BlockHasCopyDispose) |
| 604 | if (E->isByRef() |
| 605 | || BlockRequiresCopying(E->getType())) |
| 606 | BlockHasCopyDispose = true; |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 607 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 608 | const ValueDecl *D = cast<ValueDecl>(E->getDecl()); |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 609 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 610 | CharUnits Size; |
| 611 | CharUnits Align; |
| 612 | |
| 613 | if (E->isByRef()) { |
| 614 | llvm::tie(Size,Align) = |
| 615 | getContext().getTypeInfoInChars(getContext().VoidPtrTy); |
| 616 | } else { |
| 617 | Size = getContext().getTypeSizeInChars(D->getType()); |
| 618 | Align = getContext().getDeclAlign(D); |
| 619 | } |
| 620 | |
| 621 | Offset = getBlockOffset(Size, Align); |
| 622 | BlockLayout.push_back(E); |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 623 | } |
| 624 | |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 625 | llvm::Value *CodeGenFunction::GetAddrOfBlockDecl(const ValueDecl *VD, |
| 626 | bool IsByRef) { |
Fariborz Jahanian | df8b8ea | 2010-06-04 16:10:00 +0000 | [diff] [blame^] | 627 | llvm::Value *&VE = BlockDeclsValue[VD]; |
| 628 | if (VE) |
| 629 | return VE; |
| 630 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 631 | CharUnits offset = BlockDecls[VD]; |
| 632 | assert(!offset.isZero() && "getting address of unallocated decl"); |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 633 | |
| 634 | llvm::Value *BlockLiteral = LoadBlockStruct(); |
| 635 | llvm::Value *V = Builder.CreateGEP(BlockLiteral, |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 636 | llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 637 | offset.getQuantity()), |
Mike Stump | 58a8514 | 2009-03-04 22:48:06 +0000 | [diff] [blame] | 638 | "block.literal"); |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 639 | if (IsByRef) { |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 640 | const llvm::Type *PtrStructTy |
Anders Carlsson | 7dfa407 | 2009-09-12 02:14:24 +0000 | [diff] [blame] | 641 | = llvm::PointerType::get(BuildByRefType(VD), 0); |
Mike Stump | a8b60c9 | 2009-03-21 21:00:35 +0000 | [diff] [blame] | 642 | // The block literal will need a copy/destroy helper. |
| 643 | BlockHasCopyDispose = true; |
Anders Carlsson | 7dfa407 | 2009-09-12 02:14:24 +0000 | [diff] [blame] | 644 | |
| 645 | const llvm::Type *Ty = PtrStructTy; |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 646 | Ty = llvm::PointerType::get(Ty, 0); |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 647 | V = Builder.CreateBitCast(V, Ty); |
Daniel Dunbar | 2da84ff | 2009-11-29 21:23:36 +0000 | [diff] [blame] | 648 | V = Builder.CreateLoad(V); |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 649 | V = Builder.CreateStructGEP(V, 1, "forwarding"); |
Daniel Dunbar | 2da84ff | 2009-11-29 21:23:36 +0000 | [diff] [blame] | 650 | V = Builder.CreateLoad(V); |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 651 | V = Builder.CreateBitCast(V, PtrStructTy); |
Anders Carlsson | 7dfa407 | 2009-09-12 02:14:24 +0000 | [diff] [blame] | 652 | V = Builder.CreateStructGEP(V, getByRefValueLLVMField(VD), |
| 653 | VD->getNameAsString()); |
Fariborz Jahanian | d33ded5 | 2010-05-04 17:59:32 +0000 | [diff] [blame] | 654 | if (VD->getType()->isReferenceType()) |
| 655 | V = Builder.CreateLoad(V); |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 656 | } else { |
Anders Carlsson | 7dfa407 | 2009-09-12 02:14:24 +0000 | [diff] [blame] | 657 | const llvm::Type *Ty = CGM.getTypes().ConvertType(VD->getType()); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 658 | Ty = llvm::PointerType::get(Ty, 0); |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 659 | V = Builder.CreateBitCast(V, Ty); |
Fariborz Jahanian | 7965172 | 2010-06-02 21:35:17 +0000 | [diff] [blame] | 660 | if (VD->getType()->isReferenceType()) |
Fariborz Jahanian | df8b8ea | 2010-06-04 16:10:00 +0000 | [diff] [blame^] | 661 | V = Builder.CreateLoad(V, "ref.tmp"); |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 662 | } |
Fariborz Jahanian | df8b8ea | 2010-06-04 16:10:00 +0000 | [diff] [blame^] | 663 | VE = V; |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 664 | return V; |
| 665 | } |
| 666 | |
Mike Stump | 67a6448 | 2009-02-14 22:16:35 +0000 | [diff] [blame] | 667 | llvm::Constant * |
Mike Stump | 90a9043 | 2009-03-04 18:47:42 +0000 | [diff] [blame] | 668 | BlockModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) { |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 669 | // Generate the block descriptor. |
| 670 | const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy); |
Mike Stump | 7cbb360 | 2009-02-13 16:01:35 +0000 | [diff] [blame] | 671 | const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( |
| 672 | getTypes().ConvertType(getContext().IntTy)); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 673 | |
Blaine Garst | 2a7eb28 | 2010-02-23 21:51:17 +0000 | [diff] [blame] | 674 | llvm::Constant *DescriptorFields[4]; |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 675 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 676 | // Reserved |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 677 | DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 678 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 679 | // Block literal size. For global blocks we just use the size of the generic |
| 680 | // block literal struct. |
Ken Dyck | 687cc4a | 2010-01-26 13:48:07 +0000 | [diff] [blame] | 681 | CharUnits BlockLiteralSize = |
| 682 | CGM.GetTargetTypeStoreSize(getGenericBlockLiteralType()); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 683 | DescriptorFields[1] = |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 684 | llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize.getQuantity()); |
Blaine Garst | 2a7eb28 | 2010-02-23 21:51:17 +0000 | [diff] [blame] | 685 | |
| 686 | // signature. non-optional ObjC-style method descriptor @encode sequence |
| 687 | std::string BlockTypeEncoding; |
| 688 | CGM.getContext().getObjCEncodingForBlock(BE, BlockTypeEncoding); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 689 | |
Blaine Garst | 2a7eb28 | 2010-02-23 21:51:17 +0000 | [diff] [blame] | 690 | DescriptorFields[2] = llvm::ConstantExpr::getBitCast( |
| 691 | CGM.GetAddrOfConstantCString(BlockTypeEncoding), PtrToInt8Ty); |
| 692 | |
| 693 | // layout |
| 694 | DescriptorFields[3] = |
| 695 | llvm::ConstantInt::get(UnsignedLongTy,0); |
| 696 | |
| 697 | // build the structure from the 4 elements |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 698 | llvm::Constant *DescriptorStruct = |
Blaine Garst | 2a7eb28 | 2010-02-23 21:51:17 +0000 | [diff] [blame] | 699 | llvm::ConstantStruct::get(VMContext, &DescriptorFields[0], 4, false); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 700 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 701 | llvm::GlobalVariable *Descriptor = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 702 | new llvm::GlobalVariable(getModule(), DescriptorStruct->getType(), true, |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 703 | llvm::GlobalVariable::InternalLinkage, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 704 | DescriptorStruct, "__block_descriptor_global"); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 705 | |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 706 | int FieldCount = 5; |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 707 | // Generate the constants for the block literal. |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 708 | |
| 709 | std::vector<llvm::Constant*> LiteralFields(FieldCount); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 710 | |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 711 | CGBlockInfo Info(n); |
Mike Stump | 7f28a9c | 2009-03-13 23:34:28 +0000 | [diff] [blame] | 712 | llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap; |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 713 | llvm::Function *Fn |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 714 | = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, 0, LocalDeclMap); |
| 715 | assert(Info.BlockSize == BlockLiteralSize |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 716 | && "no imports allowed for global block"); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 717 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 718 | // isa |
Mike Stump | f99f1d0 | 2009-02-13 17:23:42 +0000 | [diff] [blame] | 719 | LiteralFields[0] = getNSConcreteGlobalBlock(); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 720 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 721 | // Flags |
Blaine Garst | 2a7eb28 | 2010-02-23 21:51:17 +0000 | [diff] [blame] | 722 | LiteralFields[1] = |
Blaine Garst | a36e223 | 2010-03-05 01:29:59 +0000 | [diff] [blame] | 723 | llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL | BLOCK_HAS_SIGNATURE); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 724 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 725 | // Reserved |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 726 | LiteralFields[2] = llvm::Constant::getNullValue(IntTy); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 727 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 728 | // Function |
| 729 | LiteralFields[3] = Fn; |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 730 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 731 | // Descriptor |
| 732 | LiteralFields[4] = Descriptor; |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 733 | |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 734 | llvm::Constant *BlockLiteralStruct = |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 735 | llvm::ConstantStruct::get(VMContext, LiteralFields, false); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 736 | |
| 737 | llvm::GlobalVariable *BlockLiteral = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 738 | new llvm::GlobalVariable(getModule(), BlockLiteralStruct->getType(), true, |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 739 | llvm::GlobalVariable::InternalLinkage, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 740 | BlockLiteralStruct, "__block_literal_global"); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 741 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 742 | return BlockLiteral; |
| 743 | } |
| 744 | |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 745 | llvm::Value *CodeGenFunction::LoadBlockStruct() { |
Mike Stump | bf1914b | 2009-10-20 20:30:01 +0000 | [diff] [blame] | 746 | llvm::Value *V = Builder.CreateLoad(LocalDeclMap[getBlockStructDecl()], |
| 747 | "self"); |
| 748 | // For now, we codegen based upon byte offsets. |
| 749 | return Builder.CreateBitCast(V, PtrToInt8Ty); |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 750 | } |
| 751 | |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 752 | llvm::Function * |
| 753 | CodeGenFunction::GenerateBlockFunction(const BlockExpr *BExpr, |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 754 | CGBlockInfo &Info, |
Mike Stump | 6cc88f7 | 2009-03-20 21:53:12 +0000 | [diff] [blame] | 755 | const Decl *OuterFuncDecl, |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 756 | llvm::DenseMap<const Decl*, llvm::Value*> ldm) { |
Devang Patel | 963dfbd | 2009-04-15 21:51:44 +0000 | [diff] [blame] | 757 | |
| 758 | // Check if we should generate debug info for this block. |
| 759 | if (CGM.getDebugInfo()) |
| 760 | DebugInfo = CGM.getDebugInfo(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 761 | |
Mike Stump | 7f28a9c | 2009-03-13 23:34:28 +0000 | [diff] [blame] | 762 | // Arrange for local static and local extern declarations to appear |
| 763 | // to be local to this function as well, as they are directly referenced |
| 764 | // in a block. |
| 765 | for (llvm::DenseMap<const Decl *, llvm::Value*>::iterator i = ldm.begin(); |
| 766 | i != ldm.end(); |
| 767 | ++i) { |
| 768 | const VarDecl *VD = dyn_cast<VarDecl>(i->first); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 769 | |
Daniel Dunbar | 5466c7b | 2009-04-14 02:25:56 +0000 | [diff] [blame] | 770 | if (VD->getStorageClass() == VarDecl::Static || VD->hasExternalStorage()) |
Mike Stump | 7f28a9c | 2009-03-13 23:34:28 +0000 | [diff] [blame] | 771 | LocalDeclMap[VD] = i->second; |
| 772 | } |
| 773 | |
Ken Dyck | 687cc4a | 2010-01-26 13:48:07 +0000 | [diff] [blame] | 774 | BlockOffset = |
| 775 | CGM.GetTargetTypeStoreSize(CGM.getGenericBlockLiteralType()); |
Ken Dyck | 30a8a27 | 2010-01-26 19:13:33 +0000 | [diff] [blame] | 776 | BlockAlign = getContext().getTypeAlignInChars(getContext().VoidPtrTy); |
Eli Friedman | 48f9122 | 2009-03-28 03:24:54 +0000 | [diff] [blame] | 777 | |
Fariborz Jahanian | 140fb26 | 2009-04-11 17:55:15 +0000 | [diff] [blame] | 778 | const FunctionType *BlockFunctionType = BExpr->getFunctionType(); |
| 779 | QualType ResultType; |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 780 | FunctionType::ExtInfo EInfo = getFunctionExtInfo(*BlockFunctionType); |
Fariborz Jahanian | 140fb26 | 2009-04-11 17:55:15 +0000 | [diff] [blame] | 781 | bool IsVariadic; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 782 | if (const FunctionProtoType *FTy = |
Fariborz Jahanian | da0895d | 2009-04-11 18:54:57 +0000 | [diff] [blame] | 783 | dyn_cast<FunctionProtoType>(BlockFunctionType)) { |
Fariborz Jahanian | 140fb26 | 2009-04-11 17:55:15 +0000 | [diff] [blame] | 784 | ResultType = FTy->getResultType(); |
| 785 | IsVariadic = FTy->isVariadic(); |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 786 | } else { |
Fariborz Jahanian | 140fb26 | 2009-04-11 17:55:15 +0000 | [diff] [blame] | 787 | // K&R style block. |
| 788 | ResultType = BlockFunctionType->getResultType(); |
| 789 | IsVariadic = false; |
| 790 | } |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 791 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 792 | FunctionArgList Args; |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 793 | |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 794 | CurFuncDecl = OuterFuncDecl; |
| 795 | |
Chris Lattner | 161d36d | 2009-02-28 19:01:03 +0000 | [diff] [blame] | 796 | const BlockDecl *BD = BExpr->getBlockDecl(); |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 797 | |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 798 | IdentifierInfo *II = &CGM.getContext().Idents.get(".block_descriptor"); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 799 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 800 | // Build the block struct now. |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 801 | AllocateAllBlockDeclRefs(*this, Info); |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 802 | |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 803 | QualType ParmTy = getContext().getBlockParmType(BlockHasCopyDispose, |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 804 | BlockLayout); |
| 805 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 806 | // FIXME: This leaks |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 807 | ImplicitParamDecl *SelfDecl = |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 808 | ImplicitParamDecl::Create(getContext(), const_cast<BlockDecl*>(BD), |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 809 | SourceLocation(), II, |
| 810 | ParmTy); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 811 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 812 | Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType())); |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 813 | BlockStructDecl = SelfDecl; |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 814 | |
Steve Naroff | e78b809 | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 815 | for (BlockDecl::param_const_iterator i = BD->param_begin(), |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 816 | e = BD->param_end(); i != e; ++i) |
Mike Stump | 1905061 | 2009-02-17 23:25:52 +0000 | [diff] [blame] | 817 | Args.push_back(std::make_pair(*i, (*i)->getType())); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 818 | |
| 819 | const CGFunctionInfo &FI = |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 820 | CGM.getTypes().getFunctionInfo(ResultType, Args, EInfo); |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 821 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 822 | CodeGenTypes &Types = CGM.getTypes(); |
Fariborz Jahanian | 140fb26 | 2009-04-11 17:55:15 +0000 | [diff] [blame] | 823 | const llvm::FunctionType *LTy = Types.GetFunctionType(FI, IsVariadic); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 824 | |
Douglas Gregor | 35415f5 | 2010-05-25 17:04:15 +0000 | [diff] [blame] | 825 | MangleBuffer Name; |
| 826 | CGM.getMangledName(Name, BD); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 827 | llvm::Function *Fn = |
Douglas Gregor | 6e5d9b0 | 2010-05-25 17:12:30 +0000 | [diff] [blame] | 828 | llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, |
| 829 | Name.getString(), &CGM.getModule()); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 830 | |
Daniel Dunbar | 0e4f40e | 2009-04-17 00:48:04 +0000 | [diff] [blame] | 831 | CGM.SetInternalFunctionAttributes(BD, Fn, FI); |
| 832 | |
Chris Lattner | 4863db4 | 2009-04-23 07:18:56 +0000 | [diff] [blame] | 833 | StartFunction(BD, ResultType, Fn, Args, |
Chris Lattner | 161d36d | 2009-02-28 19:01:03 +0000 | [diff] [blame] | 834 | BExpr->getBody()->getLocEnd()); |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 835 | |
Chris Lattner | 4863db4 | 2009-04-23 07:18:56 +0000 | [diff] [blame] | 836 | CurFuncDecl = OuterFuncDecl; |
Chris Lattner | b5437d2 | 2009-04-23 05:30:27 +0000 | [diff] [blame] | 837 | CurCodeDecl = BD; |
Mike Stump | b289b3f | 2009-10-01 22:29:41 +0000 | [diff] [blame] | 838 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 839 | // If we have a C++ 'this' reference, go ahead and force it into |
| 840 | // existence now. |
| 841 | if (Info.CXXThisRef) { |
| 842 | assert(!BlockCXXThisOffset.isZero() && |
| 843 | "haven't yet allocated 'this' reference"); |
| 844 | |
| 845 | // TODO: I have a dream that one day this will be typed. |
| 846 | llvm::Value *BlockLiteral = LoadBlockStruct(); |
| 847 | llvm::Value *ThisPtrRaw = |
| 848 | Builder.CreateConstInBoundsGEP1_64(BlockLiteral, |
| 849 | BlockCXXThisOffset.getQuantity(), |
| 850 | "this.ptr.raw"); |
| 851 | |
| 852 | const llvm::Type *Ty = |
| 853 | CGM.getTypes().ConvertType(Info.CXXThisRef->getType()); |
| 854 | Ty = llvm::PointerType::get(Ty, 0); |
| 855 | llvm::Value *ThisPtr = Builder.CreateBitCast(ThisPtrRaw, Ty, "this.ptr"); |
| 856 | |
| 857 | CXXThisValue = Builder.CreateLoad(ThisPtr, "this"); |
| 858 | } |
| 859 | |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 860 | // If we have an Objective C 'self' reference, go ahead and force it |
| 861 | // into existence now. |
| 862 | if (Info.NeedsObjCSelf) { |
| 863 | ValueDecl *Self = cast<ObjCMethodDecl>(CurFuncDecl)->getSelfDecl(); |
| 864 | LocalDeclMap[Self] = GetAddrOfBlockDecl(Self, false); |
| 865 | } |
| 866 | |
Mike Stump | b289b3f | 2009-10-01 22:29:41 +0000 | [diff] [blame] | 867 | // Save a spot to insert the debug information for all the BlockDeclRefDecls. |
| 868 | llvm::BasicBlock *entry = Builder.GetInsertBlock(); |
| 869 | llvm::BasicBlock::iterator entry_ptr = Builder.GetInsertPoint(); |
| 870 | --entry_ptr; |
| 871 | |
Chris Lattner | 161d36d | 2009-02-28 19:01:03 +0000 | [diff] [blame] | 872 | EmitStmt(BExpr->getBody()); |
Mike Stump | b289b3f | 2009-10-01 22:29:41 +0000 | [diff] [blame] | 873 | |
Mike Stump | de8c5c7 | 2009-10-01 00:27:30 +0000 | [diff] [blame] | 874 | // Remember where we were... |
| 875 | llvm::BasicBlock *resume = Builder.GetInsertBlock(); |
Mike Stump | b289b3f | 2009-10-01 22:29:41 +0000 | [diff] [blame] | 876 | |
Mike Stump | de8c5c7 | 2009-10-01 00:27:30 +0000 | [diff] [blame] | 877 | // Go back to the entry. |
Mike Stump | b289b3f | 2009-10-01 22:29:41 +0000 | [diff] [blame] | 878 | ++entry_ptr; |
| 879 | Builder.SetInsertPoint(entry, entry_ptr); |
| 880 | |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 881 | if (CGDebugInfo *DI = getDebugInfo()) { |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 882 | // Emit debug information for all the BlockDeclRefDecls. |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 883 | // FIXME: also for 'this' |
| 884 | for (unsigned i = 0, e = BlockLayout.size(); i != e; ++i) { |
| 885 | if (const BlockDeclRefExpr *BDRE = |
| 886 | dyn_cast<BlockDeclRefExpr>(BlockLayout[i])) { |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 887 | const ValueDecl *D = BDRE->getDecl(); |
| 888 | DI->setLocation(D->getLocation()); |
| 889 | DI->EmitDeclareOfBlockDeclRefVariable(BDRE, |
| 890 | LocalDeclMap[getBlockStructDecl()], |
| 891 | Builder, this); |
| 892 | } |
| 893 | } |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 894 | } |
Mike Stump | de8c5c7 | 2009-10-01 00:27:30 +0000 | [diff] [blame] | 895 | // And resume where we left off. |
| 896 | if (resume == 0) |
| 897 | Builder.ClearInsertionPoint(); |
| 898 | else |
| 899 | Builder.SetInsertPoint(resume); |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 900 | |
Chris Lattner | 161d36d | 2009-02-28 19:01:03 +0000 | [diff] [blame] | 901 | FinishFunction(cast<CompoundStmt>(BExpr->getBody())->getRBracLoc()); |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 902 | |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 903 | // The runtime needs a minimum alignment of a void *. |
Ken Dyck | 30a8a27 | 2010-01-26 19:13:33 +0000 | [diff] [blame] | 904 | CharUnits MinAlign = getContext().getTypeAlignInChars(getContext().VoidPtrTy); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 905 | BlockOffset = CharUnits::fromQuantity( |
Ken Dyck | 30a8a27 | 2010-01-26 19:13:33 +0000 | [diff] [blame] | 906 | llvm::RoundUpToAlignment(BlockOffset.getQuantity(), |
| 907 | MinAlign.getQuantity())); |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 908 | |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 909 | Info.BlockSize = BlockOffset; |
| 910 | Info.BlockAlign = BlockAlign; |
| 911 | Info.BlockLayout = BlockLayout; |
| 912 | Info.BlockHasCopyDispose = BlockHasCopyDispose; |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 913 | return Fn; |
| 914 | } |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 915 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 916 | CharUnits BlockFunction::getBlockOffset(CharUnits Size, CharUnits Align) { |
| 917 | assert((Align.isPositive()) && "alignment must be 1 byte or more"); |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 918 | |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 919 | CharUnits OldOffset = BlockOffset; |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 920 | |
| 921 | // Ensure proper alignment, even if it means we have to have a gap |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 922 | BlockOffset = CharUnits::fromQuantity( |
Ken Dyck | 30a8a27 | 2010-01-26 19:13:33 +0000 | [diff] [blame] | 923 | llvm::RoundUpToAlignment(BlockOffset.getQuantity(), Align.getQuantity())); |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 924 | BlockAlign = std::max(Align, BlockAlign); |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 925 | |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 926 | CharUnits Pad = BlockOffset - OldOffset; |
| 927 | if (Pad.isPositive()) { |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 928 | QualType PadTy = getContext().getConstantArrayType(getContext().CharTy, |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 929 | llvm::APInt(32, |
| 930 | Pad.getQuantity()), |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 931 | ArrayType::Normal, 0); |
Douglas Gregor | ba55ec2 | 2010-05-11 18:17:16 +0000 | [diff] [blame] | 932 | ValueDecl *PadDecl = VarDecl::Create(getContext(), |
| 933 | getContext().getTranslationUnitDecl(), |
| 934 | SourceLocation(), |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 935 | 0, QualType(PadTy), 0, |
| 936 | VarDecl::None, VarDecl::None); |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 937 | Expr *E = new (getContext()) DeclRefExpr(PadDecl, PadDecl->getType(), |
| 938 | SourceLocation()); |
| 939 | BlockLayout.push_back(E); |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 940 | } |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 941 | |
| 942 | BlockOffset += Size; |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 943 | return BlockOffset - Size; |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 944 | } |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 945 | |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 946 | llvm::Constant *BlockFunction:: |
| 947 | GenerateCopyHelperFunction(bool BlockHasCopyDispose, const llvm::StructType *T, |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 948 | std::vector<HelperInfo> *NoteForHelperp) { |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 949 | QualType R = getContext().VoidTy; |
| 950 | |
| 951 | FunctionArgList Args; |
| 952 | // FIXME: This leaks |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 953 | ImplicitParamDecl *Dst = |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 954 | ImplicitParamDecl::Create(getContext(), 0, |
| 955 | SourceLocation(), 0, |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 956 | getContext().getPointerType(getContext().VoidTy)); |
| 957 | Args.push_back(std::make_pair(Dst, Dst->getType())); |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 958 | ImplicitParamDecl *Src = |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 959 | ImplicitParamDecl::Create(getContext(), 0, |
| 960 | SourceLocation(), 0, |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 961 | getContext().getPointerType(getContext().VoidTy)); |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 962 | Args.push_back(std::make_pair(Src, Src->getType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 963 | |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 964 | const CGFunctionInfo &FI = |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 965 | CGM.getTypes().getFunctionInfo(R, Args, FunctionType::ExtInfo()); |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 966 | |
Mike Stump | 3899a7f | 2009-06-05 23:26:36 +0000 | [diff] [blame] | 967 | // FIXME: We'd like to put these into a mergable by content, with |
| 968 | // internal linkage. |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 969 | CodeGenTypes &Types = CGM.getTypes(); |
| 970 | const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); |
| 971 | |
| 972 | llvm::Function *Fn = |
| 973 | llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, |
Benjamin Kramer | 3cf7c5d | 2010-01-22 13:59:13 +0000 | [diff] [blame] | 974 | "__copy_helper_block_", &CGM.getModule()); |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 975 | |
| 976 | IdentifierInfo *II |
| 977 | = &CGM.getContext().Idents.get("__copy_helper_block_"); |
| 978 | |
| 979 | FunctionDecl *FD = FunctionDecl::Create(getContext(), |
| 980 | getContext().getTranslationUnitDecl(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 981 | SourceLocation(), II, R, 0, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 982 | FunctionDecl::Static, |
| 983 | FunctionDecl::None, |
| 984 | false, |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 985 | true); |
| 986 | CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 987 | |
| 988 | llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src); |
| 989 | llvm::Type *PtrPtrT; |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 990 | |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 991 | if (NoteForHelperp) { |
| 992 | std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp; |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 993 | |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 994 | PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0); |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 995 | SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT); |
| 996 | SrcObj = Builder.CreateLoad(SrcObj); |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 997 | |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 998 | llvm::Value *DstObj = CGF.GetAddrOfLocalVar(Dst); |
Mike Stump | c2f4c34 | 2009-04-15 22:11:36 +0000 | [diff] [blame] | 999 | llvm::Type *PtrPtrT; |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1000 | PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0); |
Mike Stump | c2f4c34 | 2009-04-15 22:11:36 +0000 | [diff] [blame] | 1001 | DstObj = Builder.CreateBitCast(DstObj, PtrPtrT); |
| 1002 | DstObj = Builder.CreateLoad(DstObj); |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 1003 | |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 1004 | for (unsigned i=0; i < NoteForHelper.size(); ++i) { |
| 1005 | int flag = NoteForHelper[i].flag; |
| 1006 | int index = NoteForHelper[i].index; |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 1007 | |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 1008 | if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF) |
| 1009 | || NoteForHelper[i].RequiresCopying) { |
| 1010 | llvm::Value *Srcv = SrcObj; |
| 1011 | Srcv = Builder.CreateStructGEP(Srcv, index); |
| 1012 | Srcv = Builder.CreateBitCast(Srcv, |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1013 | llvm::PointerType::get(PtrToInt8Ty, 0)); |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 1014 | Srcv = Builder.CreateLoad(Srcv); |
| 1015 | |
| 1016 | llvm::Value *Dstv = Builder.CreateStructGEP(DstObj, index); |
| 1017 | Dstv = Builder.CreateBitCast(Dstv, PtrToInt8Ty); |
| 1018 | |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1019 | llvm::Value *N = llvm::ConstantInt::get( |
| 1020 | llvm::Type::getInt32Ty(T->getContext()), flag); |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 1021 | llvm::Value *F = getBlockObjectAssign(); |
| 1022 | Builder.CreateCall3(F, Dstv, Srcv, N); |
| 1023 | } |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 1024 | } |
| 1025 | } |
| 1026 | |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 1027 | CGF.FinishFunction(); |
| 1028 | |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1029 | return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 1030 | } |
| 1031 | |
Mike Stump | cf62d39 | 2009-03-06 18:42:23 +0000 | [diff] [blame] | 1032 | llvm::Constant *BlockFunction:: |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 1033 | GenerateDestroyHelperFunction(bool BlockHasCopyDispose, |
| 1034 | const llvm::StructType* T, |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 1035 | std::vector<HelperInfo> *NoteForHelperp) { |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 1036 | QualType R = getContext().VoidTy; |
| 1037 | |
| 1038 | FunctionArgList Args; |
| 1039 | // FIXME: This leaks |
| 1040 | ImplicitParamDecl *Src = |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 1041 | ImplicitParamDecl::Create(getContext(), 0, |
| 1042 | SourceLocation(), 0, |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 1043 | getContext().getPointerType(getContext().VoidTy)); |
| 1044 | |
| 1045 | Args.push_back(std::make_pair(Src, Src->getType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1046 | |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 1047 | const CGFunctionInfo &FI = |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1048 | CGM.getTypes().getFunctionInfo(R, Args, FunctionType::ExtInfo()); |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 1049 | |
Mike Stump | 3899a7f | 2009-06-05 23:26:36 +0000 | [diff] [blame] | 1050 | // FIXME: We'd like to put these into a mergable by content, with |
| 1051 | // internal linkage. |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 1052 | CodeGenTypes &Types = CGM.getTypes(); |
| 1053 | const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); |
| 1054 | |
| 1055 | llvm::Function *Fn = |
| 1056 | llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, |
Benjamin Kramer | 3cf7c5d | 2010-01-22 13:59:13 +0000 | [diff] [blame] | 1057 | "__destroy_helper_block_", &CGM.getModule()); |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 1058 | |
| 1059 | IdentifierInfo *II |
| 1060 | = &CGM.getContext().Idents.get("__destroy_helper_block_"); |
| 1061 | |
| 1062 | FunctionDecl *FD = FunctionDecl::Create(getContext(), |
| 1063 | getContext().getTranslationUnitDecl(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 1064 | SourceLocation(), II, R, 0, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 1065 | FunctionDecl::Static, |
| 1066 | FunctionDecl::None, |
| 1067 | false, true); |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 1068 | CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); |
Mike Stump | 1edf6b6 | 2009-03-07 02:53:18 +0000 | [diff] [blame] | 1069 | |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 1070 | if (NoteForHelperp) { |
| 1071 | std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp; |
Mike Stump | 1edf6b6 | 2009-03-07 02:53:18 +0000 | [diff] [blame] | 1072 | |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 1073 | llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src); |
| 1074 | llvm::Type *PtrPtrT; |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1075 | PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0); |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 1076 | SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT); |
| 1077 | SrcObj = Builder.CreateLoad(SrcObj); |
Mike Stump | 1edf6b6 | 2009-03-07 02:53:18 +0000 | [diff] [blame] | 1078 | |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 1079 | for (unsigned i=0; i < NoteForHelper.size(); ++i) { |
| 1080 | int flag = NoteForHelper[i].flag; |
| 1081 | int index = NoteForHelper[i].index; |
Mike Stump | 1edf6b6 | 2009-03-07 02:53:18 +0000 | [diff] [blame] | 1082 | |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 1083 | if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF) |
| 1084 | || NoteForHelper[i].RequiresCopying) { |
| 1085 | llvm::Value *Srcv = SrcObj; |
| 1086 | Srcv = Builder.CreateStructGEP(Srcv, index); |
| 1087 | Srcv = Builder.CreateBitCast(Srcv, |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1088 | llvm::PointerType::get(PtrToInt8Ty, 0)); |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 1089 | Srcv = Builder.CreateLoad(Srcv); |
| 1090 | |
| 1091 | BuildBlockRelease(Srcv, flag); |
| 1092 | } |
Mike Stump | 1edf6b6 | 2009-03-07 02:53:18 +0000 | [diff] [blame] | 1093 | } |
| 1094 | } |
| 1095 | |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 1096 | CGF.FinishFunction(); |
| 1097 | |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1098 | return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 1099 | } |
| 1100 | |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 1101 | llvm::Constant *BlockFunction::BuildCopyHelper(const llvm::StructType *T, |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 1102 | std::vector<HelperInfo> *NoteForHelper) { |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 1103 | return CodeGenFunction(CGM).GenerateCopyHelperFunction(BlockHasCopyDispose, |
| 1104 | T, NoteForHelper); |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 1105 | } |
| 1106 | |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 1107 | llvm::Constant *BlockFunction::BuildDestroyHelper(const llvm::StructType *T, |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 1108 | std::vector<HelperInfo> *NoteForHelperp) { |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 1109 | return CodeGenFunction(CGM).GenerateDestroyHelperFunction(BlockHasCopyDispose, |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 1110 | T, NoteForHelperp); |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 1111 | } |
Mike Stump | 797b632 | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 1112 | |
Mike Stump | ee09422 | 2009-03-06 06:12:24 +0000 | [diff] [blame] | 1113 | llvm::Constant *BlockFunction:: |
| 1114 | GeneratebyrefCopyHelperFunction(const llvm::Type *T, int flag) { |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1115 | QualType R = getContext().VoidTy; |
| 1116 | |
| 1117 | FunctionArgList Args; |
| 1118 | // FIXME: This leaks |
Mike Stump | ee09422 | 2009-03-06 06:12:24 +0000 | [diff] [blame] | 1119 | ImplicitParamDecl *Dst = |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 1120 | ImplicitParamDecl::Create(getContext(), 0, |
| 1121 | SourceLocation(), 0, |
Mike Stump | ee09422 | 2009-03-06 06:12:24 +0000 | [diff] [blame] | 1122 | getContext().getPointerType(getContext().VoidTy)); |
| 1123 | Args.push_back(std::make_pair(Dst, Dst->getType())); |
| 1124 | |
| 1125 | // FIXME: This leaks |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1126 | ImplicitParamDecl *Src = |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 1127 | ImplicitParamDecl::Create(getContext(), 0, |
| 1128 | SourceLocation(), 0, |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1129 | getContext().getPointerType(getContext().VoidTy)); |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1130 | Args.push_back(std::make_pair(Src, Src->getType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1131 | |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1132 | const CGFunctionInfo &FI = |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1133 | CGM.getTypes().getFunctionInfo(R, Args, FunctionType::ExtInfo()); |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1134 | |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1135 | CodeGenTypes &Types = CGM.getTypes(); |
| 1136 | const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); |
| 1137 | |
Mike Stump | 3899a7f | 2009-06-05 23:26:36 +0000 | [diff] [blame] | 1138 | // FIXME: We'd like to put these into a mergable by content, with |
| 1139 | // internal linkage. |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1140 | llvm::Function *Fn = |
| 1141 | llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, |
Benjamin Kramer | 3cf7c5d | 2010-01-22 13:59:13 +0000 | [diff] [blame] | 1142 | "__Block_byref_id_object_copy_", &CGM.getModule()); |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1143 | |
| 1144 | IdentifierInfo *II |
| 1145 | = &CGM.getContext().Idents.get("__Block_byref_id_object_copy_"); |
| 1146 | |
| 1147 | FunctionDecl *FD = FunctionDecl::Create(getContext(), |
| 1148 | getContext().getTranslationUnitDecl(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 1149 | SourceLocation(), II, R, 0, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 1150 | FunctionDecl::Static, |
| 1151 | FunctionDecl::None, |
| 1152 | false, true); |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1153 | CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); |
Mike Stump | ee09422 | 2009-03-06 06:12:24 +0000 | [diff] [blame] | 1154 | |
| 1155 | // dst->x |
| 1156 | llvm::Value *V = CGF.GetAddrOfLocalVar(Dst); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1157 | V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0)); |
Mike Stump | c2f4c34 | 2009-04-15 22:11:36 +0000 | [diff] [blame] | 1158 | V = Builder.CreateLoad(V); |
Mike Stump | ee09422 | 2009-03-06 06:12:24 +0000 | [diff] [blame] | 1159 | V = Builder.CreateStructGEP(V, 6, "x"); |
| 1160 | llvm::Value *DstObj = Builder.CreateBitCast(V, PtrToInt8Ty); |
| 1161 | |
| 1162 | // src->x |
| 1163 | V = CGF.GetAddrOfLocalVar(Src); |
| 1164 | V = Builder.CreateLoad(V); |
| 1165 | V = Builder.CreateBitCast(V, T); |
| 1166 | V = Builder.CreateStructGEP(V, 6, "x"); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1167 | V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0)); |
Mike Stump | ee09422 | 2009-03-06 06:12:24 +0000 | [diff] [blame] | 1168 | llvm::Value *SrcObj = Builder.CreateLoad(V); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1169 | |
Mike Stump | ee09422 | 2009-03-06 06:12:24 +0000 | [diff] [blame] | 1170 | flag |= BLOCK_BYREF_CALLER; |
| 1171 | |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1172 | llvm::Value *N = llvm::ConstantInt::get( |
| 1173 | llvm::Type::getInt32Ty(T->getContext()), flag); |
Mike Stump | ee09422 | 2009-03-06 06:12:24 +0000 | [diff] [blame] | 1174 | llvm::Value *F = getBlockObjectAssign(); |
| 1175 | Builder.CreateCall3(F, DstObj, SrcObj, N); |
| 1176 | |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1177 | CGF.FinishFunction(); |
| 1178 | |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1179 | return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1180 | } |
| 1181 | |
Mike Stump | 1851b68 | 2009-03-06 04:53:30 +0000 | [diff] [blame] | 1182 | llvm::Constant * |
| 1183 | BlockFunction::GeneratebyrefDestroyHelperFunction(const llvm::Type *T, |
| 1184 | int flag) { |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1185 | QualType R = getContext().VoidTy; |
| 1186 | |
| 1187 | FunctionArgList Args; |
| 1188 | // FIXME: This leaks |
| 1189 | ImplicitParamDecl *Src = |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 1190 | ImplicitParamDecl::Create(getContext(), 0, |
| 1191 | SourceLocation(), 0, |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1192 | getContext().getPointerType(getContext().VoidTy)); |
| 1193 | |
| 1194 | Args.push_back(std::make_pair(Src, Src->getType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1195 | |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1196 | const CGFunctionInfo &FI = |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1197 | CGM.getTypes().getFunctionInfo(R, Args, FunctionType::ExtInfo()); |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1198 | |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1199 | CodeGenTypes &Types = CGM.getTypes(); |
| 1200 | const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); |
| 1201 | |
Mike Stump | 3899a7f | 2009-06-05 23:26:36 +0000 | [diff] [blame] | 1202 | // FIXME: We'd like to put these into a mergable by content, with |
| 1203 | // internal linkage. |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1204 | llvm::Function *Fn = |
| 1205 | llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, |
Benjamin Kramer | 3cf7c5d | 2010-01-22 13:59:13 +0000 | [diff] [blame] | 1206 | "__Block_byref_id_object_dispose_", |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1207 | &CGM.getModule()); |
| 1208 | |
| 1209 | IdentifierInfo *II |
| 1210 | = &CGM.getContext().Idents.get("__Block_byref_id_object_dispose_"); |
| 1211 | |
| 1212 | FunctionDecl *FD = FunctionDecl::Create(getContext(), |
| 1213 | getContext().getTranslationUnitDecl(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 1214 | SourceLocation(), II, R, 0, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 1215 | FunctionDecl::Static, |
| 1216 | FunctionDecl::None, |
| 1217 | false, true); |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1218 | CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); |
Mike Stump | 1851b68 | 2009-03-06 04:53:30 +0000 | [diff] [blame] | 1219 | |
| 1220 | llvm::Value *V = CGF.GetAddrOfLocalVar(Src); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1221 | V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0)); |
Mike Stump | c2f4c34 | 2009-04-15 22:11:36 +0000 | [diff] [blame] | 1222 | V = Builder.CreateLoad(V); |
Mike Stump | 1851b68 | 2009-03-06 04:53:30 +0000 | [diff] [blame] | 1223 | V = Builder.CreateStructGEP(V, 6, "x"); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1224 | V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0)); |
Mike Stump | c2f4c34 | 2009-04-15 22:11:36 +0000 | [diff] [blame] | 1225 | V = Builder.CreateLoad(V); |
Mike Stump | 1851b68 | 2009-03-06 04:53:30 +0000 | [diff] [blame] | 1226 | |
Mike Stump | 1851b68 | 2009-03-06 04:53:30 +0000 | [diff] [blame] | 1227 | flag |= BLOCK_BYREF_CALLER; |
| 1228 | BuildBlockRelease(V, flag); |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1229 | CGF.FinishFunction(); |
| 1230 | |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1231 | return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1232 | } |
| 1233 | |
Mike Stump | ee09422 | 2009-03-06 06:12:24 +0000 | [diff] [blame] | 1234 | llvm::Constant *BlockFunction::BuildbyrefCopyHelper(const llvm::Type *T, |
Chris Lattner | 10976d9 | 2009-12-05 08:21:30 +0000 | [diff] [blame] | 1235 | int Flag, unsigned Align) { |
| 1236 | // All alignments below that of pointer alignment collapse down to just |
Mike Stump | 3899a7f | 2009-06-05 23:26:36 +0000 | [diff] [blame] | 1237 | // pointer alignment, as we always have at least that much alignment to begin |
| 1238 | // with. |
| 1239 | Align /= unsigned(CGF.Target.getPointerAlign(0)/8); |
Chris Lattner | 10976d9 | 2009-12-05 08:21:30 +0000 | [diff] [blame] | 1240 | |
Mike Stump | 3899a7f | 2009-06-05 23:26:36 +0000 | [diff] [blame] | 1241 | // As an optimization, we only generate a single function of each kind we |
| 1242 | // might need. We need a different one for each alignment and for each |
| 1243 | // setting of flags. We mix Align and flag to get the kind. |
Chris Lattner | 10976d9 | 2009-12-05 08:21:30 +0000 | [diff] [blame] | 1244 | uint64_t Kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + Flag; |
| 1245 | llvm::Constant *&Entry = CGM.AssignCache[Kind]; |
Mike Stump | 3899a7f | 2009-06-05 23:26:36 +0000 | [diff] [blame] | 1246 | if (Entry) |
| 1247 | return Entry; |
Chris Lattner | 10976d9 | 2009-12-05 08:21:30 +0000 | [diff] [blame] | 1248 | return Entry = CodeGenFunction(CGM).GeneratebyrefCopyHelperFunction(T, Flag); |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1249 | } |
| 1250 | |
Mike Stump | 1851b68 | 2009-03-06 04:53:30 +0000 | [diff] [blame] | 1251 | llvm::Constant *BlockFunction::BuildbyrefDestroyHelper(const llvm::Type *T, |
Chris Lattner | 10976d9 | 2009-12-05 08:21:30 +0000 | [diff] [blame] | 1252 | int Flag, |
Mike Stump | 3899a7f | 2009-06-05 23:26:36 +0000 | [diff] [blame] | 1253 | unsigned Align) { |
| 1254 | // All alignments below that of pointer alignment collpase down to just |
| 1255 | // pointer alignment, as we always have at least that much alignment to begin |
| 1256 | // with. |
| 1257 | Align /= unsigned(CGF.Target.getPointerAlign(0)/8); |
Chris Lattner | 10976d9 | 2009-12-05 08:21:30 +0000 | [diff] [blame] | 1258 | |
Mike Stump | 3899a7f | 2009-06-05 23:26:36 +0000 | [diff] [blame] | 1259 | // As an optimization, we only generate a single function of each kind we |
| 1260 | // might need. We need a different one for each alignment and for each |
| 1261 | // setting of flags. We mix Align and flag to get the kind. |
Chris Lattner | 10976d9 | 2009-12-05 08:21:30 +0000 | [diff] [blame] | 1262 | uint64_t Kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + Flag; |
| 1263 | llvm::Constant *&Entry = CGM.DestroyCache[Kind]; |
Mike Stump | 3899a7f | 2009-06-05 23:26:36 +0000 | [diff] [blame] | 1264 | if (Entry) |
| 1265 | return Entry; |
Chris Lattner | 10976d9 | 2009-12-05 08:21:30 +0000 | [diff] [blame] | 1266 | return Entry=CodeGenFunction(CGM).GeneratebyrefDestroyHelperFunction(T, Flag); |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1267 | } |
| 1268 | |
Mike Stump | 797b632 | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 1269 | llvm::Value *BlockFunction::getBlockObjectDispose() { |
| 1270 | if (CGM.BlockObjectDispose == 0) { |
| 1271 | const llvm::FunctionType *FTy; |
| 1272 | std::vector<const llvm::Type*> ArgTys; |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1273 | const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext); |
Mike Stump | 797b632 | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 1274 | ArgTys.push_back(PtrToInt8Ty); |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1275 | ArgTys.push_back(llvm::Type::getInt32Ty(VMContext)); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1276 | FTy = llvm::FunctionType::get(ResultType, ArgTys, false); |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 1277 | CGM.BlockObjectDispose |
Mike Stump | 797b632 | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 1278 | = CGM.CreateRuntimeFunction(FTy, "_Block_object_dispose"); |
| 1279 | } |
| 1280 | return CGM.BlockObjectDispose; |
| 1281 | } |
| 1282 | |
Mike Stump | ee09422 | 2009-03-06 06:12:24 +0000 | [diff] [blame] | 1283 | llvm::Value *BlockFunction::getBlockObjectAssign() { |
| 1284 | if (CGM.BlockObjectAssign == 0) { |
| 1285 | const llvm::FunctionType *FTy; |
| 1286 | std::vector<const llvm::Type*> ArgTys; |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1287 | const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext); |
Mike Stump | ee09422 | 2009-03-06 06:12:24 +0000 | [diff] [blame] | 1288 | ArgTys.push_back(PtrToInt8Ty); |
| 1289 | ArgTys.push_back(PtrToInt8Ty); |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1290 | ArgTys.push_back(llvm::Type::getInt32Ty(VMContext)); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1291 | FTy = llvm::FunctionType::get(ResultType, ArgTys, false); |
Mike Stump | ee09422 | 2009-03-06 06:12:24 +0000 | [diff] [blame] | 1292 | CGM.BlockObjectAssign |
| 1293 | = CGM.CreateRuntimeFunction(FTy, "_Block_object_assign"); |
| 1294 | } |
| 1295 | return CGM.BlockObjectAssign; |
| 1296 | } |
| 1297 | |
Mike Stump | 1851b68 | 2009-03-06 04:53:30 +0000 | [diff] [blame] | 1298 | void BlockFunction::BuildBlockRelease(llvm::Value *V, int flag) { |
Mike Stump | 797b632 | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 1299 | llvm::Value *F = getBlockObjectDispose(); |
Mike Stump | 1851b68 | 2009-03-06 04:53:30 +0000 | [diff] [blame] | 1300 | llvm::Value *N; |
Mike Stump | 797b632 | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 1301 | V = Builder.CreateBitCast(V, PtrToInt8Ty); |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1302 | N = llvm::ConstantInt::get(llvm::Type::getInt32Ty(V->getContext()), flag); |
Mike Stump | 797b632 | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 1303 | Builder.CreateCall2(F, V, N); |
| 1304 | } |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 1305 | |
| 1306 | ASTContext &BlockFunction::getContext() const { return CGM.getContext(); } |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 1307 | |
| 1308 | BlockFunction::BlockFunction(CodeGenModule &cgm, CodeGenFunction &cgf, |
| 1309 | CGBuilderTy &B) |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1310 | : CGM(cgm), CGF(cgf), VMContext(cgm.getLLVMContext()), Builder(B) { |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1311 | PtrToInt8Ty = llvm::PointerType::getUnqual( |
| 1312 | llvm::Type::getInt8Ty(VMContext)); |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 1313 | |
| 1314 | BlockHasCopyDispose = false; |
| 1315 | } |