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