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