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