Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1 | //===------- CGObjCMac.cpp - Interface to Apple Objective-C Runtime -------===// |
| 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 | // |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 10 | // This provides Objective-C code generation targeting the Apple runtime. |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
John McCall | ad7c5c1 | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 14 | #include "CGBlocks.h" |
John McCall | ed1ae86 | 2011-01-28 11:13:47 +0000 | [diff] [blame] | 15 | #include "CGCleanup.h" |
Justin Lebar | 5e83dfe | 2016-10-21 21:45:01 +0000 | [diff] [blame] | 16 | #include "CGObjCRuntime.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 17 | #include "CGRecordLayout.h" |
| 18 | #include "CodeGenFunction.h" |
| 19 | #include "CodeGenModule.h" |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 20 | #include "ConstantBuilder.h" |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 21 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | 221fa94 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 22 | #include "clang/AST/Decl.h" |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 23 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | 15b73de | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 24 | #include "clang/AST/RecordLayout.h" |
Chris Lattner | f0b64d7 | 2009-04-26 01:32:48 +0000 | [diff] [blame] | 25 | #include "clang/AST/StmtObjC.h" |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 26 | #include "clang/Basic/LangOptions.h" |
Mark Lacey | a8e7df3 | 2013-10-30 21:53:58 +0000 | [diff] [blame] | 27 | #include "clang/CodeGen/CGFunctionInfo.h" |
Saleem Abdulrasool | 10a4972 | 2016-04-08 16:52:00 +0000 | [diff] [blame] | 28 | #include "clang/Frontend/CodeGenOptions.h" |
Justin Lebar | 5e83dfe | 2016-10-21 21:45:01 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/CachedHashString.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/DenseSet.h" |
| 31 | #include "llvm/ADT/SetVector.h" |
| 32 | #include "llvm/ADT/SmallPtrSet.h" |
| 33 | #include "llvm/ADT/SmallString.h" |
Chandler Carruth | c80ceea | 2014-03-04 11:02:08 +0000 | [diff] [blame] | 34 | #include "llvm/IR/CallSite.h" |
Chandler Carruth | ffd5551 | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 35 | #include "llvm/IR/DataLayout.h" |
| 36 | #include "llvm/IR/InlineAsm.h" |
| 37 | #include "llvm/IR/IntrinsicInst.h" |
| 38 | #include "llvm/IR/LLVMContext.h" |
| 39 | #include "llvm/IR/Module.h" |
Daniel Dunbar | d027a92 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 40 | #include "llvm/Support/raw_ostream.h" |
Torok Edwin | db71492 | 2009-08-24 13:25:12 +0000 | [diff] [blame] | 41 | #include <cstdio> |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 42 | |
| 43 | using namespace clang; |
Daniel Dunbar | 41cf9de | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 44 | using namespace CodeGen; |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 45 | |
| 46 | namespace { |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 47 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 48 | // FIXME: We should find a nicer way to make the labels for metadata, string |
| 49 | // concatenation is lame. |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 50 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 51 | class ObjCCommonTypesHelper { |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 52 | protected: |
| 53 | llvm::LLVMContext &VMContext; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 54 | |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 55 | private: |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 56 | // The types of these functions don't really matter because we |
| 57 | // should always bitcast before calling them. |
| 58 | |
| 59 | /// id objc_msgSend (id, SEL, ...) |
| 60 | /// |
| 61 | /// The default messenger, used for sends whose ABI is unchanged from |
| 62 | /// the all-integer/pointer case. |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 63 | llvm::Constant *getMessageSendFn() const { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 64 | // Add the non-lazy-bind attribute, since objc_msgSend is likely to |
| 65 | // be called a lot. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 66 | llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy }; |
Bill Wendling | 8594fcb | 2013-01-31 00:30:05 +0000 | [diff] [blame] | 67 | return |
| 68 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 69 | params, true), |
| 70 | "objc_msgSend", |
| 71 | llvm::AttributeSet::get(CGM.getLLVMContext(), |
| 72 | llvm::AttributeSet::FunctionIndex, |
| 73 | llvm::Attribute::NonLazyBind)); |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 74 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 75 | |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 76 | /// void objc_msgSend_stret (id, SEL, ...) |
| 77 | /// |
| 78 | /// The messenger used when the return value is an aggregate returned |
| 79 | /// by indirect reference in the first argument, and therefore the |
| 80 | /// self and selector parameters are shifted over by one. |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 81 | llvm::Constant *getMessageSendStretFn() const { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 82 | llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy }; |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 83 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy, |
| 84 | params, true), |
| 85 | "objc_msgSend_stret"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 86 | |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 87 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 88 | |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 89 | /// [double | long double] objc_msgSend_fpret(id self, SEL op, ...) |
| 90 | /// |
| 91 | /// The messenger used when the return value is returned on the x87 |
| 92 | /// floating-point stack; without a special entrypoint, the nil case |
| 93 | /// would be unbalanced. |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 94 | llvm::Constant *getMessageSendFpretFn() const { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 95 | llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy }; |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 96 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.DoubleTy, |
| 97 | params, true), |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 98 | "objc_msgSend_fpret"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 99 | |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 100 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 101 | |
Anders Carlsson | 2f1a6c3 | 2011-10-31 16:27:11 +0000 | [diff] [blame] | 102 | /// _Complex long double objc_msgSend_fp2ret(id self, SEL op, ...) |
| 103 | /// |
| 104 | /// The messenger used when the return value is returned in two values on the |
| 105 | /// x87 floating point stack; without a special entrypoint, the nil case |
| 106 | /// would be unbalanced. Only used on 64-bit X86. |
| 107 | llvm::Constant *getMessageSendFp2retFn() const { |
| 108 | llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy }; |
| 109 | llvm::Type *longDoubleType = llvm::Type::getX86_FP80Ty(VMContext); |
| 110 | llvm::Type *resultType = |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 111 | llvm::StructType::get(longDoubleType, longDoubleType, nullptr); |
Anders Carlsson | 2f1a6c3 | 2011-10-31 16:27:11 +0000 | [diff] [blame] | 112 | |
| 113 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(resultType, |
| 114 | params, true), |
| 115 | "objc_msgSend_fp2ret"); |
| 116 | } |
| 117 | |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 118 | /// id objc_msgSendSuper(struct objc_super *super, SEL op, ...) |
| 119 | /// |
| 120 | /// The messenger used for super calls, which have different dispatch |
| 121 | /// semantics. The class passed is the superclass of the current |
| 122 | /// class. |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 123 | llvm::Constant *getMessageSendSuperFn() const { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 124 | llvm::Type *params[] = { SuperPtrTy, SelectorPtrTy }; |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 125 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 126 | params, true), |
| 127 | "objc_msgSendSuper"); |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 128 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 129 | |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 130 | /// id objc_msgSendSuper2(struct objc_super *super, SEL op, ...) |
| 131 | /// |
| 132 | /// A slightly different messenger used for super calls. The class |
| 133 | /// passed is the current class. |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 134 | llvm::Constant *getMessageSendSuperFn2() const { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 135 | llvm::Type *params[] = { SuperPtrTy, SelectorPtrTy }; |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 136 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 137 | params, true), |
| 138 | "objc_msgSendSuper2"); |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 139 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 140 | |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 141 | /// void objc_msgSendSuper_stret(void *stretAddr, struct objc_super *super, |
| 142 | /// SEL op, ...) |
| 143 | /// |
| 144 | /// The messenger used for super calls which return an aggregate indirectly. |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 145 | llvm::Constant *getMessageSendSuperStretFn() const { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 146 | llvm::Type *params[] = { Int8PtrTy, SuperPtrTy, SelectorPtrTy }; |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 147 | return CGM.CreateRuntimeFunction( |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 148 | llvm::FunctionType::get(CGM.VoidTy, params, true), |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 149 | "objc_msgSendSuper_stret"); |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 150 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 151 | |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 152 | /// void objc_msgSendSuper2_stret(void * stretAddr, struct objc_super *super, |
| 153 | /// SEL op, ...) |
| 154 | /// |
| 155 | /// objc_msgSendSuper_stret with the super2 semantics. |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 156 | llvm::Constant *getMessageSendSuperStretFn2() const { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 157 | llvm::Type *params[] = { Int8PtrTy, SuperPtrTy, SelectorPtrTy }; |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 158 | return CGM.CreateRuntimeFunction( |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 159 | llvm::FunctionType::get(CGM.VoidTy, params, true), |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 160 | "objc_msgSendSuper2_stret"); |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 161 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 162 | |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 163 | llvm::Constant *getMessageSendSuperFpretFn() const { |
| 164 | // There is no objc_msgSendSuper_fpret? How can that work? |
| 165 | return getMessageSendSuperFn(); |
| 166 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 167 | |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 168 | llvm::Constant *getMessageSendSuperFpretFn2() const { |
| 169 | // There is no objc_msgSendSuper_fpret? How can that work? |
| 170 | return getMessageSendSuperFn2(); |
| 171 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 172 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 173 | protected: |
| 174 | CodeGen::CodeGenModule &CGM; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 175 | |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 176 | public: |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 177 | llvm::IntegerType *ShortTy, *IntTy, *LongTy; |
| 178 | llvm::PointerType *Int8PtrTy, *Int8PtrPtrTy; |
Tim Northover | 238b508 | 2014-03-29 13:42:40 +0000 | [diff] [blame] | 179 | llvm::Type *IvarOffsetVarTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 180 | |
Daniel Dunbar | 5d71559 | 2008-08-12 05:28:47 +0000 | [diff] [blame] | 181 | /// ObjectPtrTy - LLVM type for object handles (typeof(id)) |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 182 | llvm::PointerType *ObjectPtrTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 183 | |
Fariborz Jahanian | 406b117 | 2008-11-18 20:18:11 +0000 | [diff] [blame] | 184 | /// PtrObjectPtrTy - LLVM type for id * |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 185 | llvm::PointerType *PtrObjectPtrTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 186 | |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 187 | /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL)) |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 188 | llvm::PointerType *SelectorPtrTy; |
Douglas Gregor | 020de32 | 2012-01-17 18:36:30 +0000 | [diff] [blame] | 189 | |
| 190 | private: |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 191 | /// ProtocolPtrTy - LLVM type for external protocol handles |
| 192 | /// (typeof(Protocol)) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 193 | llvm::Type *ExternalProtocolPtrTy; |
Douglas Gregor | 020de32 | 2012-01-17 18:36:30 +0000 | [diff] [blame] | 194 | |
| 195 | public: |
| 196 | llvm::Type *getExternalProtocolPtrTy() { |
| 197 | if (!ExternalProtocolPtrTy) { |
| 198 | // FIXME: It would be nice to unify this with the opaque type, so that the |
| 199 | // IR comes out a bit cleaner. |
| 200 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 201 | ASTContext &Ctx = CGM.getContext(); |
| 202 | llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType()); |
| 203 | ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T); |
| 204 | } |
| 205 | |
| 206 | return ExternalProtocolPtrTy; |
| 207 | } |
| 208 | |
Daniel Dunbar | c722b85 | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 209 | // SuperCTy - clang type for struct objc_super. |
| 210 | QualType SuperCTy; |
| 211 | // SuperPtrCTy - clang type for struct objc_super *. |
| 212 | QualType SuperPtrCTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 213 | |
Daniel Dunbar | f6397fe | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 214 | /// SuperTy - LLVM type for struct objc_super. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 215 | llvm::StructType *SuperTy; |
Daniel Dunbar | 97ff50d | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 216 | /// SuperPtrTy - LLVM type for struct objc_super *. |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 217 | llvm::PointerType *SuperPtrTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 218 | |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 219 | /// PropertyTy - LLVM type for struct objc_property (struct _prop_t |
| 220 | /// in GCC parlance). |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 221 | llvm::StructType *PropertyTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 222 | |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 223 | /// PropertyListTy - LLVM type for struct objc_property_list |
| 224 | /// (_prop_list_t in GCC parlance). |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 225 | llvm::StructType *PropertyListTy; |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 226 | /// PropertyListPtrTy - LLVM type for struct objc_property_list*. |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 227 | llvm::PointerType *PropertyListPtrTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 228 | |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 229 | // MethodTy - LLVM type for struct objc_method. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 230 | llvm::StructType *MethodTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 231 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 232 | /// CacheTy - LLVM type for struct objc_cache. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 233 | llvm::Type *CacheTy; |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 234 | /// CachePtrTy - LLVM type for struct objc_cache *. |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 235 | llvm::PointerType *CachePtrTy; |
Fariborz Jahanian | 0a3cfcc | 2011-06-22 20:21:51 +0000 | [diff] [blame] | 236 | |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 237 | llvm::Constant *getGetPropertyFn() { |
| 238 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 239 | ASTContext &Ctx = CGM.getContext(); |
| 240 | // id objc_getProperty (id, SEL, ptrdiff_t, bool) |
John McCall | 2da83a3 | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 241 | CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType()); |
| 242 | CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType()); |
Benjamin Kramer | 3093473 | 2016-07-02 11:41:41 +0000 | [diff] [blame] | 243 | CanQualType Params[] = { |
| 244 | IdType, SelType, |
| 245 | Ctx.getPointerDiffType()->getCanonicalTypeUnqualified(), Ctx.BoolTy}; |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 246 | llvm::FunctionType *FTy = |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 247 | Types.GetFunctionType( |
| 248 | Types.arrangeBuiltinFunctionDeclaration(IdType, Params)); |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 249 | return CGM.CreateRuntimeFunction(FTy, "objc_getProperty"); |
| 250 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 251 | |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 252 | llvm::Constant *getSetPropertyFn() { |
| 253 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 254 | ASTContext &Ctx = CGM.getContext(); |
| 255 | // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool) |
John McCall | 2da83a3 | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 256 | CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType()); |
| 257 | CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType()); |
Benjamin Kramer | 3093473 | 2016-07-02 11:41:41 +0000 | [diff] [blame] | 258 | CanQualType Params[] = { |
| 259 | IdType, |
| 260 | SelType, |
| 261 | Ctx.getPointerDiffType()->getCanonicalTypeUnqualified(), |
| 262 | IdType, |
| 263 | Ctx.BoolTy, |
| 264 | Ctx.BoolTy}; |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 265 | llvm::FunctionType *FTy = |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 266 | Types.GetFunctionType( |
| 267 | Types.arrangeBuiltinFunctionDeclaration(Ctx.VoidTy, Params)); |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 268 | return CGM.CreateRuntimeFunction(FTy, "objc_setProperty"); |
| 269 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 270 | |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 271 | llvm::Constant *getOptimizedSetPropertyFn(bool atomic, bool copy) { |
| 272 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 273 | ASTContext &Ctx = CGM.getContext(); |
| 274 | // void objc_setProperty_atomic(id self, SEL _cmd, |
| 275 | // id newValue, ptrdiff_t offset); |
| 276 | // void objc_setProperty_nonatomic(id self, SEL _cmd, |
| 277 | // id newValue, ptrdiff_t offset); |
| 278 | // void objc_setProperty_atomic_copy(id self, SEL _cmd, |
| 279 | // id newValue, ptrdiff_t offset); |
| 280 | // void objc_setProperty_nonatomic_copy(id self, SEL _cmd, |
| 281 | // id newValue, ptrdiff_t offset); |
| 282 | |
| 283 | SmallVector<CanQualType,4> Params; |
| 284 | CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType()); |
| 285 | CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType()); |
| 286 | Params.push_back(IdType); |
| 287 | Params.push_back(SelType); |
| 288 | Params.push_back(IdType); |
| 289 | Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified()); |
| 290 | llvm::FunctionType *FTy = |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 291 | Types.GetFunctionType( |
| 292 | Types.arrangeBuiltinFunctionDeclaration(Ctx.VoidTy, Params)); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 293 | const char *name; |
| 294 | if (atomic && copy) |
| 295 | name = "objc_setProperty_atomic_copy"; |
| 296 | else if (atomic && !copy) |
| 297 | name = "objc_setProperty_atomic"; |
| 298 | else if (!atomic && copy) |
| 299 | name = "objc_setProperty_nonatomic_copy"; |
| 300 | else |
| 301 | name = "objc_setProperty_nonatomic"; |
| 302 | |
| 303 | return CGM.CreateRuntimeFunction(FTy, name); |
| 304 | } |
Fariborz Jahanian | 5a8c203 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 305 | |
| 306 | llvm::Constant *getCopyStructFn() { |
| 307 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 308 | ASTContext &Ctx = CGM.getContext(); |
| 309 | // void objc_copyStruct (void *, const void *, size_t, bool, bool) |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 310 | SmallVector<CanQualType,5> Params; |
Fariborz Jahanian | 5a8c203 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 311 | Params.push_back(Ctx.VoidPtrTy); |
| 312 | Params.push_back(Ctx.VoidPtrTy); |
| 313 | Params.push_back(Ctx.LongTy); |
| 314 | Params.push_back(Ctx.BoolTy); |
| 315 | Params.push_back(Ctx.BoolTy); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 316 | llvm::FunctionType *FTy = |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 317 | Types.GetFunctionType( |
| 318 | Types.arrangeBuiltinFunctionDeclaration(Ctx.VoidTy, Params)); |
Fariborz Jahanian | 5a8c203 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 319 | return CGM.CreateRuntimeFunction(FTy, "objc_copyStruct"); |
| 320 | } |
| 321 | |
Fariborz Jahanian | 1e1b549 | 2012-01-06 18:07:23 +0000 | [diff] [blame] | 322 | /// This routine declares and returns address of: |
| 323 | /// void objc_copyCppObjectAtomic( |
| 324 | /// void *dest, const void *src, |
| 325 | /// void (*copyHelper) (void *dest, const void *source)); |
| 326 | llvm::Constant *getCppAtomicObjectFunction() { |
| 327 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 328 | ASTContext &Ctx = CGM.getContext(); |
| 329 | /// void objc_copyCppObjectAtomic(void *dest, const void *src, void *helper); |
| 330 | SmallVector<CanQualType,3> Params; |
| 331 | Params.push_back(Ctx.VoidPtrTy); |
| 332 | Params.push_back(Ctx.VoidPtrTy); |
| 333 | Params.push_back(Ctx.VoidPtrTy); |
| 334 | llvm::FunctionType *FTy = |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 335 | Types.GetFunctionType( |
| 336 | Types.arrangeBuiltinFunctionDeclaration(Ctx.VoidTy, Params)); |
Fariborz Jahanian | 1e1b549 | 2012-01-06 18:07:23 +0000 | [diff] [blame] | 337 | return CGM.CreateRuntimeFunction(FTy, "objc_copyCppObjectAtomic"); |
| 338 | } |
| 339 | |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 340 | llvm::Constant *getEnumerationMutationFn() { |
Daniel Dunbar | 9d82da4 | 2009-07-11 20:32:50 +0000 | [diff] [blame] | 341 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 342 | ASTContext &Ctx = CGM.getContext(); |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 343 | // void objc_enumerationMutation (id) |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 344 | SmallVector<CanQualType,1> Params; |
John McCall | 2da83a3 | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 345 | Params.push_back(Ctx.getCanonicalParamType(Ctx.getObjCIdType())); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 346 | llvm::FunctionType *FTy = |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 347 | Types.GetFunctionType( |
| 348 | Types.arrangeBuiltinFunctionDeclaration(Ctx.VoidTy, Params)); |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 349 | return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation"); |
| 350 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 351 | |
Douglas Gregor | 24ae22c | 2016-04-01 23:23:52 +0000 | [diff] [blame] | 352 | llvm::Constant *getLookUpClassFn() { |
| 353 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 354 | ASTContext &Ctx = CGM.getContext(); |
| 355 | // Class objc_lookUpClass (const char *) |
| 356 | SmallVector<CanQualType,1> Params; |
| 357 | Params.push_back( |
| 358 | Ctx.getCanonicalType(Ctx.getPointerType(Ctx.CharTy.withConst()))); |
| 359 | llvm::FunctionType *FTy = |
| 360 | Types.GetFunctionType(Types.arrangeBuiltinFunctionDeclaration( |
| 361 | Ctx.getCanonicalType(Ctx.getObjCClassType()), |
| 362 | Params)); |
| 363 | return CGM.CreateRuntimeFunction(FTy, "objc_lookUpClass"); |
| 364 | } |
| 365 | |
Fariborz Jahanian | eee54df | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 366 | /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function. |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 367 | llvm::Constant *getGcReadWeakFn() { |
| 368 | // id objc_read_weak (id *) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 369 | llvm::Type *args[] = { ObjectPtrTy->getPointerTo() }; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 370 | llvm::FunctionType *FTy = |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 371 | llvm::FunctionType::get(ObjectPtrTy, args, false); |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 372 | return CGM.CreateRuntimeFunction(FTy, "objc_read_weak"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 373 | } |
| 374 | |
Fariborz Jahanian | eee54df | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 375 | /// GcAssignWeakFn -- LLVM objc_assign_weak function. |
Chris Lattner | 6fdd57c | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 376 | llvm::Constant *getGcAssignWeakFn() { |
| 377 | // id objc_assign_weak (id, id *) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 378 | llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() }; |
Chris Lattner | 6fdd57c | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 379 | llvm::FunctionType *FTy = |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 380 | llvm::FunctionType::get(ObjectPtrTy, args, false); |
Chris Lattner | 6fdd57c | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 381 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak"); |
| 382 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 383 | |
Fariborz Jahanian | eee54df | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 384 | /// GcAssignGlobalFn -- LLVM objc_assign_global function. |
Chris Lattner | 0a696a42 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 385 | llvm::Constant *getGcAssignGlobalFn() { |
| 386 | // id objc_assign_global(id, id *) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 387 | llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() }; |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 388 | llvm::FunctionType *FTy = |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 389 | llvm::FunctionType::get(ObjectPtrTy, args, false); |
Chris Lattner | 0a696a42 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 390 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_global"); |
| 391 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 392 | |
Fariborz Jahanian | 217af24 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 393 | /// GcAssignThreadLocalFn -- LLVM objc_assign_threadlocal function. |
| 394 | llvm::Constant *getGcAssignThreadLocalFn() { |
| 395 | // id objc_assign_threadlocal(id src, id * dest) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 396 | llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() }; |
Fariborz Jahanian | 217af24 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 397 | llvm::FunctionType *FTy = |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 398 | llvm::FunctionType::get(ObjectPtrTy, args, false); |
Fariborz Jahanian | 217af24 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 399 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_threadlocal"); |
| 400 | } |
| 401 | |
Fariborz Jahanian | eee54df | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 402 | /// GcAssignIvarFn -- LLVM objc_assign_ivar function. |
Chris Lattner | 0a696a42 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 403 | llvm::Constant *getGcAssignIvarFn() { |
Fariborz Jahanian | 7a95d72 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 404 | // id objc_assign_ivar(id, id *, ptrdiff_t) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 405 | llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo(), |
| 406 | CGM.PtrDiffTy }; |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 407 | llvm::FunctionType *FTy = |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 408 | llvm::FunctionType::get(ObjectPtrTy, args, false); |
Chris Lattner | 0a696a42 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 409 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar"); |
| 410 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 411 | |
Fariborz Jahanian | 5f21d2f | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 412 | /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function. |
| 413 | llvm::Constant *GcMemmoveCollectableFn() { |
| 414 | // void *objc_memmove_collectable(void *dst, const void *src, size_t size) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 415 | llvm::Type *args[] = { Int8PtrTy, Int8PtrTy, LongTy }; |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 416 | llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, args, false); |
Fariborz Jahanian | 5f21d2f | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 417 | return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable"); |
| 418 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 419 | |
Fariborz Jahanian | eee54df | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 420 | /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function. |
Chris Lattner | 0a696a42 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 421 | llvm::Constant *getGcAssignStrongCastFn() { |
Fariborz Jahanian | 217af24 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 422 | // id objc_assign_strongCast(id, id *) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 423 | llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() }; |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 424 | llvm::FunctionType *FTy = |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 425 | llvm::FunctionType::get(ObjectPtrTy, args, false); |
Chris Lattner | 0a696a42 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 426 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast"); |
| 427 | } |
Anders Carlsson | 9ab53d1 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 428 | |
| 429 | /// ExceptionThrowFn - LLVM objc_exception_throw function. |
Chris Lattner | 0a696a42 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 430 | llvm::Constant *getExceptionThrowFn() { |
| 431 | // void objc_exception_throw(id) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 432 | llvm::Type *args[] = { ObjectPtrTy }; |
Chris Lattner | 0a696a42 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 433 | llvm::FunctionType *FTy = |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 434 | llvm::FunctionType::get(CGM.VoidTy, args, false); |
Chris Lattner | 0a696a42 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 435 | return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw"); |
| 436 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 437 | |
Fariborz Jahanian | 3336de1 | 2010-05-28 17:34:43 +0000 | [diff] [blame] | 438 | /// ExceptionRethrowFn - LLVM objc_exception_rethrow function. |
| 439 | llvm::Constant *getExceptionRethrowFn() { |
| 440 | // void objc_exception_rethrow(void) |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 441 | llvm::FunctionType *FTy = llvm::FunctionType::get(CGM.VoidTy, false); |
Fariborz Jahanian | 3336de1 | 2010-05-28 17:34:43 +0000 | [diff] [blame] | 442 | return CGM.CreateRuntimeFunction(FTy, "objc_exception_rethrow"); |
| 443 | } |
| 444 | |
Daniel Dunbar | 94ceb61 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 445 | /// SyncEnterFn - LLVM object_sync_enter function. |
Chris Lattner | dcceee7 | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 446 | llvm::Constant *getSyncEnterFn() { |
Aaron Ballman | 9c00446 | 2012-09-06 16:44:16 +0000 | [diff] [blame] | 447 | // int objc_sync_enter (id) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 448 | llvm::Type *args[] = { ObjectPtrTy }; |
Chris Lattner | dcceee7 | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 449 | llvm::FunctionType *FTy = |
Aaron Ballman | 9c00446 | 2012-09-06 16:44:16 +0000 | [diff] [blame] | 450 | llvm::FunctionType::get(CGM.IntTy, args, false); |
Chris Lattner | dcceee7 | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 451 | return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter"); |
| 452 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 453 | |
Daniel Dunbar | 94ceb61 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 454 | /// SyncExitFn - LLVM object_sync_exit function. |
Chris Lattner | 0a696a42 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 455 | llvm::Constant *getSyncExitFn() { |
Aaron Ballman | 9c00446 | 2012-09-06 16:44:16 +0000 | [diff] [blame] | 456 | // int objc_sync_exit (id) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 457 | llvm::Type *args[] = { ObjectPtrTy }; |
Chris Lattner | 0a696a42 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 458 | llvm::FunctionType *FTy = |
Aaron Ballman | 9c00446 | 2012-09-06 16:44:16 +0000 | [diff] [blame] | 459 | llvm::FunctionType::get(CGM.IntTy, args, false); |
Chris Lattner | 0a696a42 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 460 | return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit"); |
| 461 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 462 | |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 463 | llvm::Constant *getSendFn(bool IsSuper) const { |
| 464 | return IsSuper ? getMessageSendSuperFn() : getMessageSendFn(); |
| 465 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 466 | |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 467 | llvm::Constant *getSendFn2(bool IsSuper) const { |
| 468 | return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn(); |
| 469 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 470 | |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 471 | llvm::Constant *getSendStretFn(bool IsSuper) const { |
| 472 | return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn(); |
| 473 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 474 | |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 475 | llvm::Constant *getSendStretFn2(bool IsSuper) const { |
| 476 | return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn(); |
| 477 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 478 | |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 479 | llvm::Constant *getSendFpretFn(bool IsSuper) const { |
| 480 | return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn(); |
| 481 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 482 | |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 483 | llvm::Constant *getSendFpretFn2(bool IsSuper) const { |
| 484 | return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn(); |
| 485 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 486 | |
Anders Carlsson | 2f1a6c3 | 2011-10-31 16:27:11 +0000 | [diff] [blame] | 487 | llvm::Constant *getSendFp2retFn(bool IsSuper) const { |
| 488 | return IsSuper ? getMessageSendSuperFn() : getMessageSendFp2retFn(); |
| 489 | } |
| 490 | |
| 491 | llvm::Constant *getSendFp2RetFn2(bool IsSuper) const { |
| 492 | return IsSuper ? getMessageSendSuperFn2() : getMessageSendFp2retFn(); |
| 493 | } |
| 494 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 495 | ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm); |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 496 | }; |
Daniel Dunbar | f6397fe | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 497 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 498 | /// ObjCTypesHelper - Helper class that encapsulates lazy |
| 499 | /// construction of varies types used during ObjC generation. |
| 500 | class ObjCTypesHelper : public ObjCCommonTypesHelper { |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 501 | public: |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 502 | /// SymtabTy - LLVM type for struct objc_symtab. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 503 | llvm::StructType *SymtabTy; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 504 | /// SymtabPtrTy - LLVM type for struct objc_symtab *. |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 505 | llvm::PointerType *SymtabPtrTy; |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 506 | /// ModuleTy - LLVM type for struct objc_module. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 507 | llvm::StructType *ModuleTy; |
Daniel Dunbar | cb515c8 | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 508 | |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 509 | /// ProtocolTy - LLVM type for struct objc_protocol. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 510 | llvm::StructType *ProtocolTy; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 511 | /// ProtocolPtrTy - LLVM type for struct objc_protocol *. |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 512 | llvm::PointerType *ProtocolPtrTy; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 513 | /// ProtocolExtensionTy - LLVM type for struct |
| 514 | /// objc_protocol_extension. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 515 | llvm::StructType *ProtocolExtensionTy; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 516 | /// ProtocolExtensionTy - LLVM type for struct |
| 517 | /// objc_protocol_extension *. |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 518 | llvm::PointerType *ProtocolExtensionPtrTy; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 519 | /// MethodDescriptionTy - LLVM type for struct |
| 520 | /// objc_method_description. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 521 | llvm::StructType *MethodDescriptionTy; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 522 | /// MethodDescriptionListTy - LLVM type for struct |
| 523 | /// objc_method_description_list. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 524 | llvm::StructType *MethodDescriptionListTy; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 525 | /// MethodDescriptionListPtrTy - LLVM type for struct |
| 526 | /// objc_method_description_list *. |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 527 | llvm::PointerType *MethodDescriptionListPtrTy; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 528 | /// ProtocolListTy - LLVM type for struct objc_property_list. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 529 | llvm::StructType *ProtocolListTy; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 530 | /// ProtocolListPtrTy - LLVM type for struct objc_property_list*. |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 531 | llvm::PointerType *ProtocolListPtrTy; |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 532 | /// CategoryTy - LLVM type for struct objc_category. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 533 | llvm::StructType *CategoryTy; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 534 | /// ClassTy - LLVM type for struct objc_class. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 535 | llvm::StructType *ClassTy; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 536 | /// ClassPtrTy - LLVM type for struct objc_class *. |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 537 | llvm::PointerType *ClassPtrTy; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 538 | /// ClassExtensionTy - LLVM type for struct objc_class_ext. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 539 | llvm::StructType *ClassExtensionTy; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 540 | /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *. |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 541 | llvm::PointerType *ClassExtensionPtrTy; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 542 | // IvarTy - LLVM type for struct objc_ivar. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 543 | llvm::StructType *IvarTy; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 544 | /// IvarListTy - LLVM type for struct objc_ivar_list. |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 545 | llvm::StructType *IvarListTy; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 546 | /// IvarListPtrTy - LLVM type for struct objc_ivar_list *. |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 547 | llvm::PointerType *IvarListPtrTy; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 548 | /// MethodListTy - LLVM type for struct objc_method_list. |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 549 | llvm::StructType *MethodListTy; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 550 | /// MethodListPtrTy - LLVM type for struct objc_method_list *. |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 551 | llvm::PointerType *MethodListPtrTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 552 | |
Anders Carlsson | 9ff2248 | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 553 | /// ExceptionDataTy - LLVM type for struct _objc_exception_data. |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 554 | llvm::StructType *ExceptionDataTy; |
Fariborz Jahanian | 0a3cfcc | 2011-06-22 20:21:51 +0000 | [diff] [blame] | 555 | |
Anders Carlsson | 9ff2248 | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 556 | /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function. |
Chris Lattner | c6406db | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 557 | llvm::Constant *getExceptionTryEnterFn() { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 558 | llvm::Type *params[] = { ExceptionDataTy->getPointerTo() }; |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 559 | return CGM.CreateRuntimeFunction( |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 560 | llvm::FunctionType::get(CGM.VoidTy, params, false), |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 561 | "objc_exception_try_enter"); |
Chris Lattner | c6406db | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 562 | } |
Anders Carlsson | 9ff2248 | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 563 | |
| 564 | /// ExceptionTryExitFn - LLVM objc_exception_try_exit function. |
Chris Lattner | c6406db | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 565 | llvm::Constant *getExceptionTryExitFn() { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 566 | llvm::Type *params[] = { ExceptionDataTy->getPointerTo() }; |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 567 | return CGM.CreateRuntimeFunction( |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 568 | llvm::FunctionType::get(CGM.VoidTy, params, false), |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 569 | "objc_exception_try_exit"); |
Chris Lattner | c6406db | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 570 | } |
Anders Carlsson | 9ff2248 | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 571 | |
| 572 | /// ExceptionExtractFn - LLVM objc_exception_extract function. |
Chris Lattner | c6406db | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 573 | llvm::Constant *getExceptionExtractFn() { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 574 | llvm::Type *params[] = { ExceptionDataTy->getPointerTo() }; |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 575 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 576 | params, false), |
Chris Lattner | c6406db | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 577 | "objc_exception_extract"); |
Chris Lattner | c6406db | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 578 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 579 | |
Anders Carlsson | 9ff2248 | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 580 | /// ExceptionMatchFn - LLVM objc_exception_match function. |
Chris Lattner | c6406db | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 581 | llvm::Constant *getExceptionMatchFn() { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 582 | llvm::Type *params[] = { ClassPtrTy, ObjectPtrTy }; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 583 | return CGM.CreateRuntimeFunction( |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 584 | llvm::FunctionType::get(CGM.Int32Ty, params, false), |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 585 | "objc_exception_match"); |
Chris Lattner | c6406db | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 586 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 587 | |
Anders Carlsson | 9ff2248 | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 588 | /// SetJmpFn - LLVM _setjmp function. |
Chris Lattner | c6406db | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 589 | llvm::Constant *getSetJmpFn() { |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 590 | // This is specifically the prototype for x86. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 591 | llvm::Type *params[] = { CGM.Int32Ty->getPointerTo() }; |
Bill Wendling | 8594fcb | 2013-01-31 00:30:05 +0000 | [diff] [blame] | 592 | return |
| 593 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.Int32Ty, |
| 594 | params, false), |
| 595 | "_setjmp", |
| 596 | llvm::AttributeSet::get(CGM.getLLVMContext(), |
| 597 | llvm::AttributeSet::FunctionIndex, |
| 598 | llvm::Attribute::NonLazyBind)); |
Chris Lattner | c6406db | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 599 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 600 | |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 601 | public: |
| 602 | ObjCTypesHelper(CodeGen::CodeGenModule &cgm); |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 603 | }; |
| 604 | |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 605 | /// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 606 | /// modern abi |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 607 | class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper { |
Fariborz Jahanian | e4dc35d | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 608 | public: |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 609 | // MethodListnfABITy - LLVM for struct _method_list_t |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 610 | llvm::StructType *MethodListnfABITy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 611 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 612 | // MethodListnfABIPtrTy - LLVM for struct _method_list_t* |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 613 | llvm::PointerType *MethodListnfABIPtrTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 614 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 615 | // ProtocolnfABITy = LLVM for struct _protocol_t |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 616 | llvm::StructType *ProtocolnfABITy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 617 | |
Daniel Dunbar | 8de90f0 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 618 | // ProtocolnfABIPtrTy = LLVM for struct _protocol_t* |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 619 | llvm::PointerType *ProtocolnfABIPtrTy; |
Daniel Dunbar | 8de90f0 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 620 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 621 | // ProtocolListnfABITy - LLVM for struct _objc_protocol_list |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 622 | llvm::StructType *ProtocolListnfABITy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 623 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 624 | // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list* |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 625 | llvm::PointerType *ProtocolListnfABIPtrTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 626 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 627 | // ClassnfABITy - LLVM for struct _class_t |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 628 | llvm::StructType *ClassnfABITy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 629 | |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 630 | // ClassnfABIPtrTy - LLVM for struct _class_t* |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 631 | llvm::PointerType *ClassnfABIPtrTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 632 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 633 | // IvarnfABITy - LLVM for struct _ivar_t |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 634 | llvm::StructType *IvarnfABITy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 635 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 636 | // IvarListnfABITy - LLVM for struct _ivar_list_t |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 637 | llvm::StructType *IvarListnfABITy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 638 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 639 | // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t* |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 640 | llvm::PointerType *IvarListnfABIPtrTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 641 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 642 | // ClassRonfABITy - LLVM for struct _class_ro_t |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 643 | llvm::StructType *ClassRonfABITy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 644 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 645 | // ImpnfABITy - LLVM for id (*)(id, SEL, ...) |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 646 | llvm::PointerType *ImpnfABITy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 647 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 648 | // CategorynfABITy - LLVM for struct _category_t |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 649 | llvm::StructType *CategorynfABITy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 650 | |
Fariborz Jahanian | 82c72e1 | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 651 | // New types for nonfragile abi messaging. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 652 | |
Fariborz Jahanian | 82c72e1 | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 653 | // MessageRefTy - LLVM for: |
| 654 | // struct _message_ref_t { |
| 655 | // IMP messenger; |
| 656 | // SEL name; |
| 657 | // }; |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 658 | llvm::StructType *MessageRefTy; |
Fariborz Jahanian | e4dc35d | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 659 | // MessageRefCTy - clang type for struct _message_ref_t |
| 660 | QualType MessageRefCTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 661 | |
Fariborz Jahanian | 82c72e1 | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 662 | // MessageRefPtrTy - LLVM for struct _message_ref_t* |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 663 | llvm::Type *MessageRefPtrTy; |
Fariborz Jahanian | e4dc35d | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 664 | // MessageRefCPtrTy - clang type for struct _message_ref_t* |
| 665 | QualType MessageRefCPtrTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 666 | |
Fariborz Jahanian | 82c72e1 | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 667 | // SuperMessageRefTy - LLVM for: |
| 668 | // struct _super_message_ref_t { |
| 669 | // SUPER_IMP messenger; |
| 670 | // SEL name; |
| 671 | // }; |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 672 | llvm::StructType *SuperMessageRefTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 673 | |
Fariborz Jahanian | 82c72e1 | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 674 | // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t* |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 675 | llvm::PointerType *SuperMessageRefPtrTy; |
Daniel Dunbar | 0b0dcd9 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 676 | |
Chris Lattner | 2dfdb3e | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 677 | llvm::Constant *getMessageSendFixupFn() { |
| 678 | // id objc_msgSend_fixup(id, struct message_ref_t*, ...) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 679 | llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy }; |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 680 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 681 | params, true), |
Chris Lattner | 2dfdb3e | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 682 | "objc_msgSend_fixup"); |
| 683 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 684 | |
Chris Lattner | 2dfdb3e | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 685 | llvm::Constant *getMessageSendFpretFixupFn() { |
| 686 | // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 687 | llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy }; |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 688 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 689 | params, true), |
Chris Lattner | 2dfdb3e | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 690 | "objc_msgSend_fpret_fixup"); |
| 691 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 692 | |
Chris Lattner | 2dfdb3e | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 693 | llvm::Constant *getMessageSendStretFixupFn() { |
| 694 | // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 695 | llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy }; |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 696 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 697 | params, true), |
Chris Lattner | 2dfdb3e | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 698 | "objc_msgSend_stret_fixup"); |
| 699 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 700 | |
Chris Lattner | 2dfdb3e | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 701 | llvm::Constant *getMessageSendSuper2FixupFn() { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 702 | // id objc_msgSendSuper2_fixup (struct objc_super *, |
Chris Lattner | 2dfdb3e | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 703 | // struct _super_message_ref_t*, ...) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 704 | llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy }; |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 705 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 706 | params, true), |
Chris Lattner | 2dfdb3e | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 707 | "objc_msgSendSuper2_fixup"); |
| 708 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 709 | |
Chris Lattner | 2dfdb3e | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 710 | llvm::Constant *getMessageSendSuper2StretFixupFn() { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 711 | // id objc_msgSendSuper2_stret_fixup(struct objc_super *, |
Chris Lattner | 2dfdb3e | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 712 | // struct _super_message_ref_t*, ...) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 713 | llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy }; |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 714 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 715 | params, true), |
Chris Lattner | 2dfdb3e | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 716 | "objc_msgSendSuper2_stret_fixup"); |
| 717 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 718 | |
Chris Lattner | a7c00b4 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 719 | llvm::Constant *getObjCEndCatchFn() { |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 720 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy, false), |
Chris Lattner | a7c00b4 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 721 | "objc_end_catch"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 722 | |
Chris Lattner | a7c00b4 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 723 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 724 | |
Chris Lattner | a7c00b4 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 725 | llvm::Constant *getObjCBeginCatchFn() { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 726 | llvm::Type *params[] = { Int8PtrTy }; |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 727 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy, |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 728 | params, false), |
Chris Lattner | a7c00b4 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 729 | "objc_begin_catch"); |
| 730 | } |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 731 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 732 | llvm::StructType *EHTypeTy; |
| 733 | llvm::Type *EHTypePtrTy; |
Fariborz Jahanian | 0a3cfcc | 2011-06-22 20:21:51 +0000 | [diff] [blame] | 734 | |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 735 | ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm); |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 736 | }; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 737 | |
Saleem Abdulrasool | 271106c | 2016-09-16 23:41:13 +0000 | [diff] [blame] | 738 | enum class ObjCLabelType { |
| 739 | ClassName, |
| 740 | MethodVarName, |
| 741 | MethodVarType, |
| 742 | PropertyName, |
| 743 | }; |
| 744 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 745 | class CGObjCCommonMac : public CodeGen::CGObjCRuntime { |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 746 | public: |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 747 | class SKIP_SCAN { |
Daniel Dunbar | 7b89ace | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 748 | public: |
| 749 | unsigned skip; |
| 750 | unsigned scan; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 751 | SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0) |
Daniel Dunbar | 7b89ace | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 752 | : skip(_skip), scan(_scan) {} |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 753 | }; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 754 | |
Fariborz Jahanian | 196f938 | 2012-10-25 21:15:04 +0000 | [diff] [blame] | 755 | /// opcode for captured block variables layout 'instructions'. |
| 756 | /// In the following descriptions, 'I' is the value of the immediate field. |
| 757 | /// (field following the opcode). |
| 758 | /// |
| 759 | enum BLOCK_LAYOUT_OPCODE { |
| 760 | /// An operator which affects how the following layout should be |
| 761 | /// interpreted. |
| 762 | /// I == 0: Halt interpretation and treat everything else as |
| 763 | /// a non-pointer. Note that this instruction is equal |
| 764 | /// to '\0'. |
| 765 | /// I != 0: Currently unused. |
| 766 | BLOCK_LAYOUT_OPERATOR = 0, |
| 767 | |
| 768 | /// The next I+1 bytes do not contain a value of object pointer type. |
| 769 | /// Note that this can leave the stream unaligned, meaning that |
| 770 | /// subsequent word-size instructions do not begin at a multiple of |
| 771 | /// the pointer size. |
| 772 | BLOCK_LAYOUT_NON_OBJECT_BYTES = 1, |
| 773 | |
| 774 | /// The next I+1 words do not contain a value of object pointer type. |
| 775 | /// This is simply an optimized version of BLOCK_LAYOUT_BYTES for |
| 776 | /// when the required skip quantity is a multiple of the pointer size. |
| 777 | BLOCK_LAYOUT_NON_OBJECT_WORDS = 2, |
| 778 | |
| 779 | /// The next I+1 words are __strong pointers to Objective-C |
| 780 | /// objects or blocks. |
| 781 | BLOCK_LAYOUT_STRONG = 3, |
| 782 | |
| 783 | /// The next I+1 words are pointers to __block variables. |
| 784 | BLOCK_LAYOUT_BYREF = 4, |
| 785 | |
| 786 | /// The next I+1 words are __weak pointers to Objective-C |
| 787 | /// objects or blocks. |
| 788 | BLOCK_LAYOUT_WEAK = 5, |
| 789 | |
| 790 | /// The next I+1 words are __unsafe_unretained pointers to |
| 791 | /// Objective-C objects or blocks. |
| 792 | BLOCK_LAYOUT_UNRETAINED = 6 |
| 793 | |
| 794 | /// The next I+1 words are block or object pointers with some |
| 795 | /// as-yet-unspecified ownership semantics. If we add more |
| 796 | /// flavors of ownership semantics, values will be taken from |
| 797 | /// this range. |
| 798 | /// |
| 799 | /// This is included so that older tools can at least continue |
| 800 | /// processing the layout past such things. |
| 801 | //BLOCK_LAYOUT_OWNERSHIP_UNKNOWN = 7..10, |
| 802 | |
| 803 | /// All other opcodes are reserved. Halt interpretation and |
| 804 | /// treat everything else as opaque. |
| 805 | }; |
| 806 | |
| 807 | class RUN_SKIP { |
| 808 | public: |
| 809 | enum BLOCK_LAYOUT_OPCODE opcode; |
Fariborz Jahanian | 7778d61 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 810 | CharUnits block_var_bytepos; |
| 811 | CharUnits block_var_size; |
Fariborz Jahanian | 196f938 | 2012-10-25 21:15:04 +0000 | [diff] [blame] | 812 | RUN_SKIP(enum BLOCK_LAYOUT_OPCODE Opcode = BLOCK_LAYOUT_OPERATOR, |
Fariborz Jahanian | 7778d61 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 813 | CharUnits BytePos = CharUnits::Zero(), |
| 814 | CharUnits Size = CharUnits::Zero()) |
Fariborz Jahanian | 0c58ce9 | 2012-10-27 21:10:38 +0000 | [diff] [blame] | 815 | : opcode(Opcode), block_var_bytepos(BytePos), block_var_size(Size) {} |
Fariborz Jahanian | 196f938 | 2012-10-25 21:15:04 +0000 | [diff] [blame] | 816 | |
| 817 | // Allow sorting based on byte pos. |
| 818 | bool operator<(const RUN_SKIP &b) const { |
| 819 | return block_var_bytepos < b.block_var_bytepos; |
| 820 | } |
| 821 | }; |
| 822 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 823 | protected: |
Owen Anderson | ae86c19 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 824 | llvm::LLVMContext &VMContext; |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 825 | // FIXME! May not be needing this after all. |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 826 | unsigned ObjCABI; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 827 | |
Fariborz Jahanian | 196f938 | 2012-10-25 21:15:04 +0000 | [diff] [blame] | 828 | // arc/mrr layout of captured block literal variables. |
| 829 | SmallVector<RUN_SKIP, 16> RunSkipBlockVars; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 830 | |
Daniel Dunbar | c61d0e9 | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 831 | /// LazySymbols - Symbols to generate a lazy reference for. See |
| 832 | /// DefinedSymbols and FinishModule(). |
Daniel Dunbar | d027a92 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 833 | llvm::SetVector<IdentifierInfo*> LazySymbols; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 834 | |
Daniel Dunbar | c61d0e9 | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 835 | /// DefinedSymbols - External symbols which are defined by this |
| 836 | /// module. The symbols in this list and LazySymbols are used to add |
| 837 | /// special linker symbols which ensure that Objective-C modules are |
| 838 | /// linked properly. |
Daniel Dunbar | d027a92 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 839 | llvm::SetVector<IdentifierInfo*> DefinedSymbols; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 840 | |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 841 | /// ClassNames - uniqued class names. |
Fariborz Jahanian | 451b92a | 2014-07-16 16:16:04 +0000 | [diff] [blame] | 842 | llvm::StringMap<llvm::GlobalVariable*> ClassNames; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 843 | |
Daniel Dunbar | cb515c8 | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 844 | /// MethodVarNames - uniqued method variable names. |
| 845 | llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 846 | |
Fariborz Jahanian | 9adb2e6 | 2010-06-21 22:05:18 +0000 | [diff] [blame] | 847 | /// DefinedCategoryNames - list of category names in form Class_Category. |
Justin Lebar | 5e83dfe | 2016-10-21 21:45:01 +0000 | [diff] [blame] | 848 | llvm::SmallSetVector<llvm::CachedHashString, 16> DefinedCategoryNames; |
Fariborz Jahanian | 9adb2e6 | 2010-06-21 22:05:18 +0000 | [diff] [blame] | 849 | |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 850 | /// MethodVarTypes - uniqued method type signatures. We have to use |
| 851 | /// a StringMap here because have no other unique reference. |
| 852 | llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 853 | |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 854 | /// MethodDefinitions - map of methods which have been defined in |
| 855 | /// this translation unit. |
| 856 | llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 857 | |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 858 | /// PropertyNames - uniqued method variable names. |
| 859 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 860 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 861 | /// ClassReferences - uniqued class references. |
| 862 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 863 | |
Daniel Dunbar | cb515c8 | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 864 | /// SelectorReferences - uniqued selector references. |
| 865 | llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 866 | |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 867 | /// Protocols - Protocols for which an objc_protocol structure has |
| 868 | /// been emitted. Forward declarations are handled by creating an |
| 869 | /// empty structure whose initializer is filled in when/if defined. |
| 870 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 871 | |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 872 | /// DefinedProtocols - Protocols which have actually been |
| 873 | /// defined. We should not need this, see FIXME in GenerateProtocol. |
| 874 | llvm::DenseSet<IdentifierInfo*> DefinedProtocols; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 875 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 876 | /// DefinedClasses - List of defined classes. |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 877 | SmallVector<llvm::GlobalValue*, 16> DefinedClasses; |
Fariborz Jahanian | f322f2f | 2014-03-11 00:25:05 +0000 | [diff] [blame] | 878 | |
| 879 | /// ImplementedClasses - List of @implemented classes. |
| 880 | SmallVector<const ObjCInterfaceDecl*, 16> ImplementedClasses; |
Daniel Dunbar | 9a017d7 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 881 | |
| 882 | /// DefinedNonLazyClasses - List of defined "non-lazy" classes. |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 883 | SmallVector<llvm::GlobalValue*, 16> DefinedNonLazyClasses; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 884 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 885 | /// DefinedCategories - List of defined categories. |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 886 | SmallVector<llvm::GlobalValue*, 16> DefinedCategories; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 887 | |
Daniel Dunbar | 9a017d7 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 888 | /// DefinedNonLazyCategories - List of defined "non-lazy" categories. |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 889 | SmallVector<llvm::GlobalValue*, 16> DefinedNonLazyCategories; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 890 | |
Fariborz Jahanian | 0b1ccdc | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 891 | /// GetNameForMethod - Return a name for the given method. |
| 892 | /// \param[out] NameOut - The return value. |
| 893 | void GetNameForMethod(const ObjCMethodDecl *OMD, |
| 894 | const ObjCContainerDecl *CD, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 895 | SmallVectorImpl<char> &NameOut); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 896 | |
Fariborz Jahanian | 0b1ccdc | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 897 | /// GetMethodVarName - Return a unique constant for the given |
| 898 | /// selector's name. The return value has type char *. |
| 899 | llvm::Constant *GetMethodVarName(Selector Sel); |
| 900 | llvm::Constant *GetMethodVarName(IdentifierInfo *Ident); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 901 | |
Fariborz Jahanian | 0b1ccdc | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 902 | /// GetMethodVarType - Return a unique constant for the given |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 903 | /// method's type encoding string. The return value has type char *. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 904 | |
Fariborz Jahanian | 0b1ccdc | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 905 | // FIXME: This is a horrible name. |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 906 | llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D, |
| 907 | bool Extended = false); |
Daniel Dunbar | f5c1846 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 908 | llvm::Constant *GetMethodVarType(const FieldDecl *D); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 909 | |
Fariborz Jahanian | 0b1ccdc | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 910 | /// GetPropertyName - Return a unique constant for the given |
| 911 | /// name. The return value has type char *. |
| 912 | llvm::Constant *GetPropertyName(IdentifierInfo *Ident); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 913 | |
Fariborz Jahanian | 0b1ccdc | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 914 | // FIXME: This can be dropped once string functions are unified. |
| 915 | llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD, |
| 916 | const Decl *Container); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 917 | |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 918 | /// GetClassName - Return a unique constant for the given selector's |
Fariborz Jahanian | 451b92a | 2014-07-16 16:16:04 +0000 | [diff] [blame] | 919 | /// runtime name (which may change via use of objc_runtime_name attribute on |
| 920 | /// class or protocol definition. The return value has type char *. |
| 921 | llvm::Constant *GetClassName(StringRef RuntimeName); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 922 | |
Argyrios Kyrtzidis | 13257c5 | 2010-08-09 10:54:20 +0000 | [diff] [blame] | 923 | llvm::Function *GetMethodDefinition(const ObjCMethodDecl *MD); |
| 924 | |
Fariborz Jahanian | c559f3f | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 925 | /// BuildIvarLayout - Builds ivar layout bitmap for the class |
| 926 | /// implementation for the __strong or __weak case. |
| 927 | /// |
John McCall | 460ce58 | 2015-10-22 18:38:17 +0000 | [diff] [blame] | 928 | /// \param hasMRCWeakIvars - Whether we are compiling in MRC and there |
| 929 | /// are any weak ivars defined directly in the class. Meaningless unless |
| 930 | /// building a weak layout. Does not guarantee that the layout will |
| 931 | /// actually have any entries, because the ivar might be under-aligned. |
Fariborz Jahanian | 1bf7288 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 932 | llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI, |
John McCall | 3fd13f06 | 2015-10-21 18:06:47 +0000 | [diff] [blame] | 933 | CharUnits beginOffset, |
| 934 | CharUnits endOffset, |
John McCall | 460ce58 | 2015-10-22 18:38:17 +0000 | [diff] [blame] | 935 | bool forStrongLayout, |
| 936 | bool hasMRCWeakIvars); |
| 937 | |
| 938 | llvm::Constant *BuildStrongIvarLayout(const ObjCImplementationDecl *OI, |
| 939 | CharUnits beginOffset, |
| 940 | CharUnits endOffset) { |
| 941 | return BuildIvarLayout(OI, beginOffset, endOffset, true, false); |
| 942 | } |
| 943 | |
| 944 | llvm::Constant *BuildWeakIvarLayout(const ObjCImplementationDecl *OI, |
| 945 | CharUnits beginOffset, |
| 946 | CharUnits endOffset, |
| 947 | bool hasMRCWeakIvars) { |
| 948 | return BuildIvarLayout(OI, beginOffset, endOffset, false, hasMRCWeakIvars); |
| 949 | } |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 950 | |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 951 | Qualifiers::ObjCLifetime getBlockCaptureLifetime(QualType QT, bool ByrefLayout); |
Fariborz Jahanian | 2dd7819 | 2012-11-02 22:51:18 +0000 | [diff] [blame] | 952 | |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 953 | void UpdateRunSkipBlockVars(bool IsByref, |
| 954 | Qualifiers::ObjCLifetime LifeTime, |
Fariborz Jahanian | 7778d61 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 955 | CharUnits FieldOffset, |
| 956 | CharUnits FieldSize); |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 957 | |
| 958 | void BuildRCBlockVarRecordLayout(const RecordType *RT, |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 959 | CharUnits BytePos, bool &HasUnion, |
| 960 | bool ByrefLayout=false); |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 961 | |
| 962 | void BuildRCRecordLayout(const llvm::StructLayout *RecLayout, |
| 963 | const RecordDecl *RD, |
| 964 | ArrayRef<const FieldDecl*> RecFields, |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 965 | CharUnits BytePos, bool &HasUnion, |
| 966 | bool ByrefLayout); |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 967 | |
Fariborz Jahanian | 23290b0 | 2012-11-01 18:32:55 +0000 | [diff] [blame] | 968 | uint64_t InlineLayoutInstruction(SmallVectorImpl<unsigned char> &Layout); |
| 969 | |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 970 | llvm::Constant *getBitmapBlockLayout(bool ComputeByrefLayout); |
| 971 | |
Fariborz Jahanian | 01dff42 | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 972 | /// GetIvarLayoutName - Returns a unique constant for the given |
| 973 | /// ivar layout bitmap. |
| 974 | llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident, |
| 975 | const ObjCCommonTypesHelper &ObjCTypes); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 976 | |
Fariborz Jahanian | 066347e | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 977 | /// EmitPropertyList - Emit the given property list. The return |
| 978 | /// value has type PropertyListPtrTy. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 979 | llvm::Constant *EmitPropertyList(Twine Name, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 980 | const Decl *Container, |
Fariborz Jahanian | 066347e | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 981 | const ObjCContainerDecl *OCD, |
Manman Ren | ad0e791 | 2016-01-29 19:22:54 +0000 | [diff] [blame] | 982 | const ObjCCommonTypesHelper &ObjCTypes, |
| 983 | bool IsClassProperty); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 984 | |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 985 | /// EmitProtocolMethodTypes - Generate the array of extended method type |
| 986 | /// strings. The return value has type Int8PtrPtrTy. |
| 987 | llvm::Constant *EmitProtocolMethodTypes(Twine Name, |
Bill Wendling | cb5b5ff | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 988 | ArrayRef<llvm::Constant*> MethodTypes, |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 989 | const ObjCCommonTypesHelper &ObjCTypes); |
| 990 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 991 | /// GetProtocolRef - Return a reference to the internal protocol |
| 992 | /// description, creating an empty one if it has not been |
| 993 | /// defined. The return value has type ProtocolPtrTy. |
| 994 | llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD); |
Fariborz Jahanian | 6772621 | 2009-03-08 20:18:37 +0000 | [diff] [blame] | 995 | |
Douglas Gregor | 24ae22c | 2016-04-01 23:23:52 +0000 | [diff] [blame] | 996 | /// Return a reference to the given Class using runtime calls rather than |
| 997 | /// by a symbol reference. |
| 998 | llvm::Value *EmitClassRefViaRuntime(CodeGenFunction &CGF, |
| 999 | const ObjCInterfaceDecl *ID, |
| 1000 | ObjCCommonTypesHelper &ObjCTypes); |
| 1001 | |
John McCall | 3fd13f06 | 2015-10-21 18:06:47 +0000 | [diff] [blame] | 1002 | public: |
Daniel Dunbar | 30c6536 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 1003 | /// CreateMetadataVar - Create a global variable with internal |
| 1004 | /// linkage for use by the Objective-C runtime. |
| 1005 | /// |
| 1006 | /// This is a convenience wrapper which not only creates the |
| 1007 | /// variable, but also sets the section and alignment and adds the |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 1008 | /// global to the "llvm.used" list. |
Daniel Dunbar | 463cc8a | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 1009 | /// |
| 1010 | /// \param Name - The variable name. |
| 1011 | /// \param Init - The variable initializer; this is also used to |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 1012 | /// define the type of the variable. |
Alp Toker | 541d507 | 2014-06-07 23:30:53 +0000 | [diff] [blame] | 1013 | /// \param Section - The section the variable should go into, or empty. |
Daniel Dunbar | 463cc8a | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 1014 | /// \param Align - The alignment for the variable, or 0. |
| 1015 | /// \param AddToUsed - Whether the variable should be added to |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 1016 | /// "llvm.used". |
| 1017 | llvm::GlobalVariable *CreateMetadataVar(Twine Name, |
| 1018 | ConstantStructBuilder &Init, |
| 1019 | StringRef Section, CharUnits Align, |
| 1020 | bool AddToUsed); |
| 1021 | llvm::GlobalVariable *CreateMetadataVar(Twine Name, |
| 1022 | llvm::Constant *Init, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1023 | StringRef Section, CharUnits Align, |
Daniel Dunbar | 463cc8a | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 1024 | bool AddToUsed); |
Daniel Dunbar | 30c6536 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 1025 | |
Saleem Abdulrasool | 271106c | 2016-09-16 23:41:13 +0000 | [diff] [blame] | 1026 | llvm::GlobalVariable *CreateCStringLiteral(StringRef Name, |
Saleem Abdulrasool | 82f6add | 2016-09-20 18:38:54 +0000 | [diff] [blame] | 1027 | ObjCLabelType LabelType, |
| 1028 | bool ForceNonFragileABI = false, |
| 1029 | bool NullTerminate = true); |
Saleem Abdulrasool | 271106c | 2016-09-16 23:41:13 +0000 | [diff] [blame] | 1030 | |
John McCall | 3fd13f06 | 2015-10-21 18:06:47 +0000 | [diff] [blame] | 1031 | protected: |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1032 | CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
| 1033 | ReturnValueSlot Return, |
| 1034 | QualType ResultType, |
| 1035 | llvm::Value *Sel, |
| 1036 | llvm::Value *Arg0, |
| 1037 | QualType Arg0Ty, |
| 1038 | bool IsSuper, |
| 1039 | const CallArgList &CallArgs, |
| 1040 | const ObjCMethodDecl *OMD, |
John McCall | 1e3157b | 2015-09-10 22:27:50 +0000 | [diff] [blame] | 1041 | const ObjCInterfaceDecl *ClassReceiver, |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1042 | const ObjCCommonTypesHelper &ObjCTypes); |
Daniel Dunbar | f5c1846 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 1043 | |
Daniel Dunbar | 5e63927 | 2010-04-25 20:39:01 +0000 | [diff] [blame] | 1044 | /// EmitImageInfo - Emit the image info marker used to encode some module |
| 1045 | /// level information. |
| 1046 | void EmitImageInfo(); |
| 1047 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1048 | public: |
Owen Anderson | ae86c19 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 1049 | CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1050 | CGObjCRuntime(cgm), VMContext(cgm.getLLVMContext()) { } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1051 | |
John McCall | 3fd13f06 | 2015-10-21 18:06:47 +0000 | [diff] [blame] | 1052 | bool isNonFragileABI() const { |
| 1053 | return ObjCABI == 2; |
| 1054 | } |
| 1055 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1056 | ConstantAddress GenerateConstantString(const StringLiteral *SL) override; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1057 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1058 | llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD, |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1059 | const ObjCContainerDecl *CD=nullptr) override; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1060 | |
| 1061 | void GenerateProtocol(const ObjCProtocolDecl *PD) override; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1062 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1063 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 1064 | /// declaration, emitting it if necessary. The return value has type |
| 1065 | /// ProtocolPtrTy. |
| 1066 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1067 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1068 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 1069 | /// object for the given declaration, emitting it if needed. These |
| 1070 | /// forward references will be filled in with empty bodies if no |
| 1071 | /// definition is seen. The return value has type ProtocolPtrTy. |
| 1072 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1073 | llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM, |
| 1074 | const CGBlockInfo &blockInfo) override; |
| 1075 | llvm::Constant *BuildRCBlockLayout(CodeGen::CodeGenModule &CGM, |
| 1076 | const CGBlockInfo &blockInfo) override; |
| 1077 | |
| 1078 | llvm::Constant *BuildByrefLayout(CodeGen::CodeGenModule &CGM, |
| 1079 | QualType T) override; |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1080 | }; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1081 | |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 1082 | namespace { |
| 1083 | |
Saleem Abdulrasool | d48b0a3 | 2016-10-24 20:47:58 +0000 | [diff] [blame] | 1084 | enum class MethodListType { |
| 1085 | CategoryInstanceMethods, |
| 1086 | CategoryClassMethods, |
| 1087 | InstanceMethods, |
| 1088 | ClassMethods, |
| 1089 | ProtocolInstanceMethods, |
| 1090 | ProtocolClassMethods, |
| 1091 | OptionalProtocolInstanceMethods, |
| 1092 | OptionalProtocolClassMethods, |
| 1093 | }; |
| 1094 | |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 1095 | /// A convenience class for splitting the methods of a protocol into |
| 1096 | /// the four interesting groups. |
| 1097 | class ProtocolMethodLists { |
| 1098 | public: |
| 1099 | enum Kind { |
| 1100 | RequiredInstanceMethods, |
| 1101 | RequiredClassMethods, |
| 1102 | OptionalInstanceMethods, |
| 1103 | OptionalClassMethods |
| 1104 | }; |
| 1105 | enum { |
| 1106 | NumProtocolMethodLists = 4 |
| 1107 | }; |
| 1108 | |
| 1109 | static MethodListType getMethodListKind(Kind kind) { |
| 1110 | switch (kind) { |
| 1111 | case RequiredInstanceMethods: |
| 1112 | return MethodListType::ProtocolInstanceMethods; |
| 1113 | case RequiredClassMethods: |
| 1114 | return MethodListType::ProtocolClassMethods; |
| 1115 | case OptionalInstanceMethods: |
| 1116 | return MethodListType::OptionalProtocolInstanceMethods; |
| 1117 | case OptionalClassMethods: |
| 1118 | return MethodListType::OptionalProtocolClassMethods; |
| 1119 | } |
| 1120 | llvm_unreachable("bad kind"); |
| 1121 | } |
| 1122 | |
| 1123 | SmallVector<const ObjCMethodDecl *, 4> Methods[NumProtocolMethodLists]; |
| 1124 | |
| 1125 | static ProtocolMethodLists get(const ObjCProtocolDecl *PD) { |
| 1126 | ProtocolMethodLists result; |
| 1127 | |
| 1128 | for (auto MD : PD->methods()) { |
| 1129 | size_t index = (2 * size_t(MD->isOptional())) |
| 1130 | + (size_t(MD->isClassMethod())); |
| 1131 | result.Methods[index].push_back(MD); |
| 1132 | } |
| 1133 | |
| 1134 | return result; |
| 1135 | } |
| 1136 | |
| 1137 | template <class Self> |
| 1138 | SmallVector<llvm::Constant*, 8> emitExtendedTypesArray(Self *self) const { |
| 1139 | // In both ABIs, the method types list is parallel with the |
| 1140 | // concatenation of the methods arrays in the following order: |
| 1141 | // instance methods |
| 1142 | // class methods |
| 1143 | // optional instance methods |
| 1144 | // optional class methods |
| 1145 | SmallVector<llvm::Constant*, 8> result; |
| 1146 | |
| 1147 | // Methods is already in the correct order for both ABIs. |
| 1148 | for (auto &list : Methods) { |
| 1149 | for (auto MD : list) { |
| 1150 | result.push_back(self->GetMethodVarType(MD, true)); |
| 1151 | } |
| 1152 | } |
| 1153 | |
| 1154 | return result; |
| 1155 | } |
| 1156 | |
| 1157 | template <class Self> |
| 1158 | llvm::Constant *emitMethodList(Self *self, const ObjCProtocolDecl *PD, |
| 1159 | Kind kind) const { |
| 1160 | return self->emitMethodList(PD->getObjCRuntimeNameAsString(), |
| 1161 | getMethodListKind(kind), Methods[kind]); |
| 1162 | } |
| 1163 | }; |
| 1164 | |
| 1165 | } // end anonymous namespace |
| 1166 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1167 | class CGObjCMac : public CGObjCCommonMac { |
| 1168 | private: |
John McCall | c4ae1dd | 2016-11-30 04:18:19 +0000 | [diff] [blame^] | 1169 | friend ProtocolMethodLists; |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 1170 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1171 | ObjCTypesHelper ObjCTypes; |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 1172 | |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 1173 | /// EmitModuleInfo - Another marker encoding module level |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1174 | /// information. |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 1175 | void EmitModuleInfo(); |
| 1176 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1177 | /// EmitModuleSymols - Emit module symbols, the list of defined |
| 1178 | /// classes and categories. The result has type SymtabPtrTy. |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 1179 | llvm::Constant *EmitModuleSymbols(); |
| 1180 | |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 1181 | /// FinishModule - Write out global data structures at the end of |
| 1182 | /// processing a translation unit. |
| 1183 | void FinishModule(); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1184 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1185 | /// EmitClassExtension - Generate the class extension structure used |
| 1186 | /// to store the weak ivar layout and properties. The return value |
| 1187 | /// has type ClassExtensionPtrTy. |
John McCall | 3fd13f06 | 2015-10-21 18:06:47 +0000 | [diff] [blame] | 1188 | llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID, |
John McCall | 460ce58 | 2015-10-22 18:38:17 +0000 | [diff] [blame] | 1189 | CharUnits instanceSize, |
Manman Ren | ad0e791 | 2016-01-29 19:22:54 +0000 | [diff] [blame] | 1190 | bool hasMRCWeakIvars, |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 1191 | bool isMetaclass); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1192 | |
| 1193 | /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
| 1194 | /// for the given class. |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1195 | llvm::Value *EmitClassRef(CodeGenFunction &CGF, |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1196 | const ObjCInterfaceDecl *ID); |
Fariborz Jahanian | eb80c98 | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 1197 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1198 | llvm::Value *EmitClassRefFromId(CodeGenFunction &CGF, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1199 | IdentifierInfo *II); |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1200 | |
| 1201 | llvm::Value *EmitNSAutoreleasePoolClassRef(CodeGenFunction &CGF) override; |
| 1202 | |
Fariborz Jahanian | eb80c98 | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 1203 | /// EmitSuperClassRef - Emits reference to class's main metadata class. |
| 1204 | llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1205 | |
| 1206 | /// EmitIvarList - Emit the ivar list for the given |
| 1207 | /// implementation. If ForClass is true the list of class ivars |
| 1208 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 1209 | /// interface ivars will be emitted. The return value has type |
| 1210 | /// IvarListPtrTy. |
| 1211 | llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID, |
Fariborz Jahanian | b042a59 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 1212 | bool ForClass); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1213 | |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1214 | /// EmitMetaClass - Emit a forward reference to the class structure |
| 1215 | /// for the metaclass of the given interface. The return value has |
| 1216 | /// type ClassPtrTy. |
| 1217 | llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID); |
| 1218 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1219 | /// EmitMetaClass - Emit a class structure for the metaclass of the |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1220 | /// given implementation. The return value has type ClassPtrTy. |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1221 | llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID, |
| 1222 | llvm::Constant *Protocols, |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 1223 | ArrayRef<const ObjCMethodDecl *> Methods); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1224 | |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 1225 | void emitMethodConstant(ConstantArrayBuilder &builder, |
| 1226 | const ObjCMethodDecl *MD); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1227 | |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 1228 | void emitMethodDescriptionConstant(ConstantArrayBuilder &builder, |
| 1229 | const ObjCMethodDecl *MD); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1230 | |
| 1231 | /// EmitMethodList - Emit the method list for the given |
Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 1232 | /// implementation. The return value has type MethodListPtrTy. |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 1233 | llvm::Constant *emitMethodList(Twine Name, MethodListType MLT, |
| 1234 | ArrayRef<const ObjCMethodDecl *> Methods); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1235 | |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1236 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 1237 | /// declaration, emitting it if necessary. The return value has type |
| 1238 | /// ProtocolPtrTy. |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1239 | llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD) override; |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1240 | |
| 1241 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 1242 | /// object for the given declaration, emitting it if needed. These |
| 1243 | /// forward references will be filled in with empty bodies if no |
| 1244 | /// definition is seen. The return value has type ProtocolPtrTy. |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1245 | llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) override; |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1246 | |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1247 | /// EmitProtocolExtension - Generate the protocol extension |
| 1248 | /// structure used to store optional instance and class methods, and |
| 1249 | /// protocol properties. The return value has type |
| 1250 | /// ProtocolExtensionPtrTy. |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1251 | llvm::Constant * |
| 1252 | EmitProtocolExtension(const ObjCProtocolDecl *PD, |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 1253 | const ProtocolMethodLists &methodLists); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1254 | |
| 1255 | /// EmitProtocolList - Generate the list of referenced |
| 1256 | /// protocols. The return value has type ProtocolListPtrTy. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1257 | llvm::Constant *EmitProtocolList(Twine Name, |
Daniel Dunbar | dec75f8 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1258 | ObjCProtocolDecl::protocol_iterator begin, |
| 1259 | ObjCProtocolDecl::protocol_iterator end); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1260 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1261 | /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, |
| 1262 | /// for the given selector. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1263 | llvm::Value *EmitSelector(CodeGenFunction &CGF, Selector Sel); |
| 1264 | Address EmitSelectorAddr(CodeGenFunction &CGF, Selector Sel); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1265 | |
| 1266 | public: |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1267 | CGObjCMac(CodeGen::CodeGenModule &cgm); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1268 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1269 | llvm::Function *ModuleInitFunction() override; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1270 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1271 | CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
| 1272 | ReturnValueSlot Return, |
| 1273 | QualType ResultType, |
| 1274 | Selector Sel, llvm::Value *Receiver, |
| 1275 | const CallArgList &CallArgs, |
| 1276 | const ObjCInterfaceDecl *Class, |
| 1277 | const ObjCMethodDecl *Method) override; |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1278 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1279 | CodeGen::RValue |
Daniel Dunbar | 97db84c | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1280 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1281 | ReturnValueSlot Return, QualType ResultType, |
| 1282 | Selector Sel, const ObjCInterfaceDecl *Class, |
| 1283 | bool isCategoryImpl, llvm::Value *Receiver, |
| 1284 | bool IsClassMessage, const CallArgList &CallArgs, |
| 1285 | const ObjCMethodDecl *Method) override; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1286 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1287 | llvm::Value *GetClass(CodeGenFunction &CGF, |
| 1288 | const ObjCInterfaceDecl *ID) override; |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1289 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1290 | llvm::Value *GetSelector(CodeGenFunction &CGF, Selector Sel) override; |
| 1291 | Address GetAddrOfSelector(CodeGenFunction &CGF, Selector Sel) override; |
Fariborz Jahanian | f3648b8 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1292 | |
| 1293 | /// The NeXT/Apple runtimes do not support typed selectors; just emit an |
| 1294 | /// untyped one. |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1295 | llvm::Value *GetSelector(CodeGenFunction &CGF, |
| 1296 | const ObjCMethodDecl *Method) override; |
Fariborz Jahanian | f3648b8 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1297 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1298 | llvm::Constant *GetEHType(QualType T) override; |
John McCall | 2ca705e | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 1299 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1300 | void GenerateCategory(const ObjCCategoryImplDecl *CMD) override; |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1301 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1302 | void GenerateClass(const ObjCImplementationDecl *ClassDecl) override; |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1303 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1304 | void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) override {} |
David Chisnall | 92d436b | 2012-01-31 18:59:20 +0000 | [diff] [blame] | 1305 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1306 | llvm::Value *GenerateProtocolRef(CodeGenFunction &CGF, |
| 1307 | const ObjCProtocolDecl *PD) override; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1308 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1309 | llvm::Constant *GetPropertyGetFunction() override; |
| 1310 | llvm::Constant *GetPropertySetFunction() override; |
| 1311 | llvm::Constant *GetOptimizedPropertySetFunction(bool atomic, |
| 1312 | bool copy) override; |
| 1313 | llvm::Constant *GetGetStructFunction() override; |
| 1314 | llvm::Constant *GetSetStructFunction() override; |
| 1315 | llvm::Constant *GetCppAtomicObjectGetFunction() override; |
| 1316 | llvm::Constant *GetCppAtomicObjectSetFunction() override; |
| 1317 | llvm::Constant *EnumerationMutationFunction() override; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1318 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1319 | void EmitTryStmt(CodeGen::CodeGenFunction &CGF, |
| 1320 | const ObjCAtTryStmt &S) override; |
| 1321 | void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 1322 | const ObjCAtSynchronizedStmt &S) override; |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1323 | void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const Stmt &S); |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1324 | void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, const ObjCAtThrowStmt &S, |
| 1325 | bool ClearInsertionPoint=true) override; |
| 1326 | llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1327 | Address AddrWeakObj) override; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1328 | void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1329 | llvm::Value *src, Address dst) override; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1330 | void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1331 | llvm::Value *src, Address dest, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1332 | bool threadlocal = false) override; |
| 1333 | void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1334 | llvm::Value *src, Address dest, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1335 | llvm::Value *ivarOffset) override; |
| 1336 | void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1337 | llvm::Value *src, Address dest) override; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1338 | void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1339 | Address dest, Address src, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1340 | llvm::Value *size) override; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1341 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1342 | LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, QualType ObjectTy, |
| 1343 | llvm::Value *BaseValue, const ObjCIvarDecl *Ivar, |
| 1344 | unsigned CVRQualifiers) override; |
| 1345 | llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
| 1346 | const ObjCInterfaceDecl *Interface, |
| 1347 | const ObjCIvarDecl *Ivar) override; |
| 1348 | |
Fariborz Jahanian | 7bd3d1c | 2011-05-17 22:21:16 +0000 | [diff] [blame] | 1349 | /// GetClassGlobal - Return the global variable for the Objective-C |
| 1350 | /// class of the given name. |
Benjamin Kramer | 0772c42 | 2016-02-13 13:42:54 +0000 | [diff] [blame] | 1351 | llvm::GlobalVariable *GetClassGlobal(StringRef Name, |
Craig Topper | a798a9d | 2014-03-02 09:32:10 +0000 | [diff] [blame] | 1352 | bool Weak = false) override { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1353 | llvm_unreachable("CGObjCMac::GetClassGlobal"); |
Fariborz Jahanian | 7bd3d1c | 2011-05-17 22:21:16 +0000 | [diff] [blame] | 1354 | } |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1355 | }; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1356 | |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1357 | class CGObjCNonFragileABIMac : public CGObjCCommonMac { |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1358 | private: |
John McCall | c4ae1dd | 2016-11-30 04:18:19 +0000 | [diff] [blame^] | 1359 | friend ProtocolMethodLists; |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1360 | ObjCNonFragileABITypesHelper ObjCTypes; |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1361 | llvm::GlobalVariable* ObjCEmptyCacheVar; |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 1362 | llvm::Constant* ObjCEmptyVtableVar; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1363 | |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1364 | /// SuperClassReferences - uniqued super class references. |
| 1365 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1366 | |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1367 | /// MetaClassReferences - uniqued meta class references. |
| 1368 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences; |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1369 | |
| 1370 | /// EHTypeReferences - uniqued class ehtype references. |
| 1371 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1372 | |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1373 | /// VTableDispatchMethods - List of methods for which we generate |
| 1374 | /// vtable-based message dispatch. |
| 1375 | llvm::DenseSet<Selector> VTableDispatchMethods; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1376 | |
Fariborz Jahanian | 6726055 | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 1377 | /// DefinedMetaClasses - List of defined meta-classes. |
| 1378 | std::vector<llvm::GlobalValue*> DefinedMetaClasses; |
| 1379 | |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1380 | /// isVTableDispatchedSelector - Returns true if SEL is a |
| 1381 | /// vtable-based selector. |
| 1382 | bool isVTableDispatchedSelector(Selector Sel); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1383 | |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1384 | /// FinishNonFragileABIModule - Write out global data structures at the end of |
| 1385 | /// processing a translation unit. |
| 1386 | void FinishNonFragileABIModule(); |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1387 | |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 1388 | /// AddModuleClassList - Add the given list of class pointers to the |
| 1389 | /// module with the provided symbol and section names. |
Saleem Abdulrasool | 1e6c406 | 2016-05-16 05:06:49 +0000 | [diff] [blame] | 1390 | void AddModuleClassList(ArrayRef<llvm::GlobalValue *> Container, |
| 1391 | StringRef SymbolName, StringRef SectionName); |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 1392 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1393 | llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags, |
| 1394 | unsigned InstanceStart, |
| 1395 | unsigned InstanceSize, |
| 1396 | const ObjCImplementationDecl *ID); |
Fariborz Jahanian | 451b92a | 2014-07-16 16:16:04 +0000 | [diff] [blame] | 1397 | llvm::GlobalVariable * BuildClassMetaData(const std::string &ClassName, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1398 | llvm::Constant *IsAGV, |
Fariborz Jahanian | 4723fb7 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 1399 | llvm::Constant *SuperClassGV, |
Fariborz Jahanian | 8220825 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 1400 | llvm::Constant *ClassRoGV, |
Rafael Espindola | 554256c | 2014-02-26 22:25:45 +0000 | [diff] [blame] | 1401 | bool HiddenVisibility, |
Fariborz Jahanian | f322f2f | 2014-03-11 00:25:05 +0000 | [diff] [blame] | 1402 | bool Weak); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1403 | |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 1404 | void emitMethodConstant(ConstantArrayBuilder &builder, |
| 1405 | const ObjCMethodDecl *MD, |
| 1406 | bool forProtocol); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1407 | |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 1408 | /// Emit the method list for the given implementation. The return value |
| 1409 | /// has type MethodListnfABITy. |
| 1410 | llvm::Constant *emitMethodList(Twine Name, MethodListType MLT, |
| 1411 | ArrayRef<const ObjCMethodDecl *> Methods); |
Saleem Abdulrasool | d48b0a3 | 2016-10-24 20:47:58 +0000 | [diff] [blame] | 1412 | |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 1413 | /// EmitIvarList - Emit the ivar list for the given |
| 1414 | /// implementation. If ForClass is true the list of class ivars |
| 1415 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 1416 | /// interface ivars will be emitted. The return value has type |
| 1417 | /// IvarListnfABIPtrTy. |
| 1418 | llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1419 | |
Fariborz Jahanian | 4e7ae06 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 1420 | llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | 3d3426f | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 1421 | const ObjCIvarDecl *Ivar, |
Eli Friedman | 8cbca20 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 1422 | unsigned long int offset); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1423 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1424 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 1425 | /// declaration, emitting it if necessary. The return value has type |
| 1426 | /// ProtocolPtrTy. |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1427 | llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD) override; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1428 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1429 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 1430 | /// object for the given declaration, emitting it if needed. These |
| 1431 | /// forward references will be filled in with empty bodies if no |
| 1432 | /// definition is seen. The return value has type ProtocolPtrTy. |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1433 | llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) override; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1434 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1435 | /// EmitProtocolList - Generate the list of referenced |
| 1436 | /// protocols. The return value has type ProtocolListPtrTy. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1437 | llvm::Constant *EmitProtocolList(Twine Name, |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1438 | ObjCProtocolDecl::protocol_iterator begin, |
Fariborz Jahanian | 3d9296e | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 1439 | ObjCProtocolDecl::protocol_iterator end); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1440 | |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1441 | CodeGen::RValue EmitVTableMessageSend(CodeGen::CodeGenFunction &CGF, |
| 1442 | ReturnValueSlot Return, |
| 1443 | QualType ResultType, |
| 1444 | Selector Sel, |
| 1445 | llvm::Value *Receiver, |
| 1446 | QualType Arg0Ty, |
| 1447 | bool IsSuper, |
| 1448 | const CallArgList &CallArgs, |
| 1449 | const ObjCMethodDecl *Method); |
Fariborz Jahanian | 7bd3d1c | 2011-05-17 22:21:16 +0000 | [diff] [blame] | 1450 | |
Daniel Dunbar | c6928bb | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 1451 | /// GetClassGlobal - Return the global variable for the Objective-C |
| 1452 | /// class of the given name. |
Benjamin Kramer | 0772c42 | 2016-02-13 13:42:54 +0000 | [diff] [blame] | 1453 | llvm::GlobalVariable *GetClassGlobal(StringRef Name, |
Craig Topper | a798a9d | 2014-03-02 09:32:10 +0000 | [diff] [blame] | 1454 | bool Weak = false) override; |
Rafael Espindola | 554256c | 2014-02-26 22:25:45 +0000 | [diff] [blame] | 1455 | |
Fariborz Jahanian | 33f66e6 | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 1456 | /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1457 | /// for the given class reference. |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1458 | llvm::Value *EmitClassRef(CodeGenFunction &CGF, |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1459 | const ObjCInterfaceDecl *ID); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1460 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1461 | llvm::Value *EmitClassRefFromId(CodeGenFunction &CGF, |
Fariborz Jahanian | 451b92a | 2014-07-16 16:16:04 +0000 | [diff] [blame] | 1462 | IdentifierInfo *II, bool Weak, |
| 1463 | const ObjCInterfaceDecl *ID); |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1464 | |
| 1465 | llvm::Value *EmitNSAutoreleasePoolClassRef(CodeGenFunction &CGF) override; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1466 | |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1467 | /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
| 1468 | /// for the given super class reference. |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1469 | llvm::Value *EmitSuperClassRef(CodeGenFunction &CGF, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1470 | const ObjCInterfaceDecl *ID); |
| 1471 | |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1472 | /// EmitMetaClassRef - Return a Value * of the address of _class_t |
| 1473 | /// meta-data |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1474 | llvm::Value *EmitMetaClassRef(CodeGenFunction &CGF, |
Fariborz Jahanian | 0b3bc24 | 2014-06-10 17:08:04 +0000 | [diff] [blame] | 1475 | const ObjCInterfaceDecl *ID, bool Weak); |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1476 | |
Fariborz Jahanian | 4e7ae06 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 1477 | /// ObjCIvarOffsetVariable - Returns the ivar offset variable for |
| 1478 | /// the given ivar. |
| 1479 | /// |
Daniel Dunbar | a106052 | 2009-04-19 00:31:15 +0000 | [diff] [blame] | 1480 | llvm::GlobalVariable * ObjCIvarOffsetVariable( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1481 | const ObjCInterfaceDecl *ID, |
| 1482 | const ObjCIvarDecl *Ivar); |
| 1483 | |
Fariborz Jahanian | 74b7722 | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 1484 | /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, |
| 1485 | /// for the given selector. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1486 | llvm::Value *EmitSelector(CodeGenFunction &CGF, Selector Sel); |
| 1487 | Address EmitSelectorAddr(CodeGenFunction &CGF, Selector Sel); |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1488 | |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1489 | /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1490 | /// interface. The return value has type EHTypePtrTy. |
John McCall | 2ca705e | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 1491 | llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID, |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1492 | bool ForDefinition); |
Daniel Dunbar | 15894b7 | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 1493 | |
Saleem Abdulrasool | 1e6c406 | 2016-05-16 05:06:49 +0000 | [diff] [blame] | 1494 | StringRef getMetaclassSymbolPrefix() const { return "OBJC_METACLASS_$_"; } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1495 | |
Saleem Abdulrasool | 1e6c406 | 2016-05-16 05:06:49 +0000 | [diff] [blame] | 1496 | StringRef getClassSymbolPrefix() const { return "OBJC_CLASS_$_"; } |
Daniel Dunbar | 15894b7 | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 1497 | |
Daniel Dunbar | 96120237 | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 1498 | void GetClassSizeInfo(const ObjCImplementationDecl *OID, |
Daniel Dunbar | 554fd79 | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 1499 | uint32_t &InstanceStart, |
| 1500 | uint32_t &InstanceSize); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1501 | |
Fariborz Jahanian | e412864 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1502 | // Shamelessly stolen from Analysis/CFRefCount.cpp |
Daniel Dunbar | 9a017d7 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1503 | Selector GetNullarySelector(const char* name) const { |
Fariborz Jahanian | e412864 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1504 | IdentifierInfo* II = &CGM.getContext().Idents.get(name); |
| 1505 | return CGM.getContext().Selectors.getSelector(0, &II); |
| 1506 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1507 | |
Daniel Dunbar | 9a017d7 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1508 | Selector GetUnarySelector(const char* name) const { |
Fariborz Jahanian | e412864 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1509 | IdentifierInfo* II = &CGM.getContext().Idents.get(name); |
| 1510 | return CGM.getContext().Selectors.getSelector(1, &II); |
| 1511 | } |
Daniel Dunbar | 554fd79 | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 1512 | |
Daniel Dunbar | 9a017d7 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1513 | /// ImplementationIsNonLazy - Check whether the given category or |
| 1514 | /// class implementation is "non-lazy". |
Fariborz Jahanian | a6bed83 | 2009-05-21 01:03:45 +0000 | [diff] [blame] | 1515 | bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const; |
Daniel Dunbar | 9a017d7 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1516 | |
Saleem Abdulrasool | 5f25bc3 | 2013-02-17 04:03:34 +0000 | [diff] [blame] | 1517 | bool IsIvarOffsetKnownIdempotent(const CodeGen::CodeGenFunction &CGF, |
Saleem Abdulrasool | 5f25bc3 | 2013-02-17 04:03:34 +0000 | [diff] [blame] | 1518 | const ObjCIvarDecl *IV) { |
Fariborz Jahanian | dafffbe | 2014-03-04 18:34:52 +0000 | [diff] [blame] | 1519 | // Annotate the load as an invariant load iff inside an instance method |
| 1520 | // and ivar belongs to instance method's class and one of its super class. |
| 1521 | // This check is needed because the ivar offset is a lazily |
Saleem Abdulrasool | 5f25bc3 | 2013-02-17 04:03:34 +0000 | [diff] [blame] | 1522 | // initialised value that may depend on objc_msgSend to perform a fixup on |
| 1523 | // the first message dispatch. |
| 1524 | // |
| 1525 | // An additional opportunity to mark the load as invariant arises when the |
| 1526 | // base of the ivar access is a parameter to an Objective C method. |
| 1527 | // However, because the parameters are not available in the current |
| 1528 | // interface, we cannot perform this check. |
Fariborz Jahanian | dafffbe | 2014-03-04 18:34:52 +0000 | [diff] [blame] | 1529 | if (const ObjCMethodDecl *MD = |
| 1530 | dyn_cast_or_null<ObjCMethodDecl>(CGF.CurFuncDecl)) |
Fariborz Jahanian | 7a58302 | 2014-03-04 22:57:32 +0000 | [diff] [blame] | 1531 | if (MD->isInstanceMethod()) |
Fariborz Jahanian | dafffbe | 2014-03-04 18:34:52 +0000 | [diff] [blame] | 1532 | if (const ObjCInterfaceDecl *ID = MD->getClassInterface()) |
| 1533 | return IV->getContainingInterface()->isSuperClassOf(ID); |
Saleem Abdulrasool | 5f25bc3 | 2013-02-17 04:03:34 +0000 | [diff] [blame] | 1534 | return false; |
| 1535 | } |
| 1536 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1537 | public: |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1538 | CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm); |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1539 | // FIXME. All stubs for now! |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1540 | llvm::Function *ModuleInitFunction() override; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1541 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1542 | CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
| 1543 | ReturnValueSlot Return, |
| 1544 | QualType ResultType, Selector Sel, |
| 1545 | llvm::Value *Receiver, |
| 1546 | const CallArgList &CallArgs, |
| 1547 | const ObjCInterfaceDecl *Class, |
| 1548 | const ObjCMethodDecl *Method) override; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1549 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1550 | CodeGen::RValue |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1551 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1552 | ReturnValueSlot Return, QualType ResultType, |
| 1553 | Selector Sel, const ObjCInterfaceDecl *Class, |
| 1554 | bool isCategoryImpl, llvm::Value *Receiver, |
| 1555 | bool IsClassMessage, const CallArgList &CallArgs, |
| 1556 | const ObjCMethodDecl *Method) override; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1557 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1558 | llvm::Value *GetClass(CodeGenFunction &CGF, |
| 1559 | const ObjCInterfaceDecl *ID) override; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1560 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1561 | llvm::Value *GetSelector(CodeGenFunction &CGF, Selector Sel) override |
| 1562 | { return EmitSelector(CGF, Sel); } |
| 1563 | Address GetAddrOfSelector(CodeGenFunction &CGF, Selector Sel) override |
| 1564 | { return EmitSelectorAddr(CGF, Sel); } |
Fariborz Jahanian | f3648b8 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1565 | |
| 1566 | /// The NeXT/Apple runtimes do not support typed selectors; just emit an |
| 1567 | /// untyped one. |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1568 | llvm::Value *GetSelector(CodeGenFunction &CGF, |
| 1569 | const ObjCMethodDecl *Method) override |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1570 | { return EmitSelector(CGF, Method->getSelector()); } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1571 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1572 | void GenerateCategory(const ObjCCategoryImplDecl *CMD) override; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1573 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1574 | void GenerateClass(const ObjCImplementationDecl *ClassDecl) override; |
David Chisnall | 92d436b | 2012-01-31 18:59:20 +0000 | [diff] [blame] | 1575 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1576 | void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) override {} |
David Chisnall | 92d436b | 2012-01-31 18:59:20 +0000 | [diff] [blame] | 1577 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1578 | llvm::Value *GenerateProtocolRef(CodeGenFunction &CGF, |
| 1579 | const ObjCProtocolDecl *PD) override; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1580 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1581 | llvm::Constant *GetEHType(QualType T) override; |
John McCall | 2ca705e | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 1582 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1583 | llvm::Constant *GetPropertyGetFunction() override { |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 1584 | return ObjCTypes.getGetPropertyFn(); |
Fariborz Jahanian | 21fc74c | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1585 | } |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1586 | llvm::Constant *GetPropertySetFunction() override { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1587 | return ObjCTypes.getSetPropertyFn(); |
Fariborz Jahanian | 21fc74c | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1588 | } |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1589 | |
| 1590 | llvm::Constant *GetOptimizedPropertySetFunction(bool atomic, |
| 1591 | bool copy) override { |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 1592 | return ObjCTypes.getOptimizedSetPropertyFn(atomic, copy); |
| 1593 | } |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1594 | |
| 1595 | llvm::Constant *GetSetStructFunction() override { |
David Chisnall | 168b80f | 2010-12-26 22:13:16 +0000 | [diff] [blame] | 1596 | return ObjCTypes.getCopyStructFn(); |
| 1597 | } |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 1598 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1599 | llvm::Constant *GetGetStructFunction() override { |
Fariborz Jahanian | 5a8c203 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 1600 | return ObjCTypes.getCopyStructFn(); |
| 1601 | } |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 1602 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1603 | llvm::Constant *GetCppAtomicObjectSetFunction() override { |
David Chisnall | 0d75e06 | 2012-12-17 18:54:24 +0000 | [diff] [blame] | 1604 | return ObjCTypes.getCppAtomicObjectFunction(); |
| 1605 | } |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 1606 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1607 | llvm::Constant *GetCppAtomicObjectGetFunction() override { |
Fariborz Jahanian | 1e1b549 | 2012-01-06 18:07:23 +0000 | [diff] [blame] | 1608 | return ObjCTypes.getCppAtomicObjectFunction(); |
| 1609 | } |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1610 | |
| 1611 | llvm::Constant *EnumerationMutationFunction() override { |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 1612 | return ObjCTypes.getEnumerationMutationFn(); |
Daniel Dunbar | d73ea816 | 2009-02-16 18:48:45 +0000 | [diff] [blame] | 1613 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1614 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1615 | void EmitTryStmt(CodeGen::CodeGenFunction &CGF, |
| 1616 | const ObjCAtTryStmt &S) override; |
| 1617 | void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 1618 | const ObjCAtSynchronizedStmt &S) override; |
| 1619 | void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, const ObjCAtThrowStmt &S, |
| 1620 | bool ClearInsertionPoint=true) override; |
| 1621 | llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1622 | Address AddrWeakObj) override; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1623 | void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1624 | llvm::Value *src, Address edst) override; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1625 | void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1626 | llvm::Value *src, Address dest, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1627 | bool threadlocal = false) override; |
| 1628 | void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1629 | llvm::Value *src, Address dest, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1630 | llvm::Value *ivarOffset) override; |
| 1631 | void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1632 | llvm::Value *src, Address dest) override; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1633 | void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1634 | Address dest, Address src, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1635 | llvm::Value *size) override; |
| 1636 | LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, QualType ObjectTy, |
| 1637 | llvm::Value *BaseValue, const ObjCIvarDecl *Ivar, |
| 1638 | unsigned CVRQualifiers) override; |
| 1639 | llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
| 1640 | const ObjCInterfaceDecl *Interface, |
| 1641 | const ObjCIvarDecl *Ivar) override; |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1642 | }; |
Fariborz Jahanian | 715fdd5 | 2012-01-29 20:27:13 +0000 | [diff] [blame] | 1643 | |
| 1644 | /// A helper class for performing the null-initialization of a return |
| 1645 | /// value. |
| 1646 | struct NullReturnState { |
| 1647 | llvm::BasicBlock *NullBB; |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1648 | NullReturnState() : NullBB(nullptr) {} |
Fariborz Jahanian | 715fdd5 | 2012-01-29 20:27:13 +0000 | [diff] [blame] | 1649 | |
John McCall | 3d1e2c9 | 2013-02-12 05:53:35 +0000 | [diff] [blame] | 1650 | /// Perform a null-check of the given receiver. |
Fariborz Jahanian | 715fdd5 | 2012-01-29 20:27:13 +0000 | [diff] [blame] | 1651 | void init(CodeGenFunction &CGF, llvm::Value *receiver) { |
John McCall | 3d1e2c9 | 2013-02-12 05:53:35 +0000 | [diff] [blame] | 1652 | // Make blocks for the null-receiver and call edges. |
| 1653 | NullBB = CGF.createBasicBlock("msgSend.null-receiver"); |
| 1654 | llvm::BasicBlock *callBB = CGF.createBasicBlock("msgSend.call"); |
Fariborz Jahanian | 715fdd5 | 2012-01-29 20:27:13 +0000 | [diff] [blame] | 1655 | |
| 1656 | // Check for a null receiver and, if there is one, jump to the |
John McCall | 3d1e2c9 | 2013-02-12 05:53:35 +0000 | [diff] [blame] | 1657 | // null-receiver block. There's no point in trying to avoid it: |
| 1658 | // we're always going to put *something* there, because otherwise |
| 1659 | // we shouldn't have done this null-check in the first place. |
Fariborz Jahanian | 715fdd5 | 2012-01-29 20:27:13 +0000 | [diff] [blame] | 1660 | llvm::Value *isNull = CGF.Builder.CreateIsNull(receiver); |
| 1661 | CGF.Builder.CreateCondBr(isNull, NullBB, callBB); |
| 1662 | |
| 1663 | // Otherwise, start performing the call. |
| 1664 | CGF.EmitBlock(callBB); |
| 1665 | } |
| 1666 | |
John McCall | 3d1e2c9 | 2013-02-12 05:53:35 +0000 | [diff] [blame] | 1667 | /// Complete the null-return operation. It is valid to call this |
| 1668 | /// regardless of whether 'init' has been called. |
Fariborz Jahanian | f2bda69 | 2012-01-30 21:40:37 +0000 | [diff] [blame] | 1669 | RValue complete(CodeGenFunction &CGF, RValue result, QualType resultType, |
| 1670 | const CallArgList &CallArgs, |
| 1671 | const ObjCMethodDecl *Method) { |
John McCall | 3d1e2c9 | 2013-02-12 05:53:35 +0000 | [diff] [blame] | 1672 | // If we never had to do a null-check, just use the raw result. |
Fariborz Jahanian | 715fdd5 | 2012-01-29 20:27:13 +0000 | [diff] [blame] | 1673 | if (!NullBB) return result; |
John McCall | 3d1e2c9 | 2013-02-12 05:53:35 +0000 | [diff] [blame] | 1674 | |
| 1675 | // The continuation block. This will be left null if we don't have an |
| 1676 | // IP, which can happen if the method we're calling is marked noreturn. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1677 | llvm::BasicBlock *contBB = nullptr; |
| 1678 | |
John McCall | 3d1e2c9 | 2013-02-12 05:53:35 +0000 | [diff] [blame] | 1679 | // Finish the call path. |
| 1680 | llvm::BasicBlock *callBB = CGF.Builder.GetInsertBlock(); |
| 1681 | if (callBB) { |
| 1682 | contBB = CGF.createBasicBlock("msgSend.cont"); |
| 1683 | CGF.Builder.CreateBr(contBB); |
Fariborz Jahanian | f2bda69 | 2012-01-30 21:40:37 +0000 | [diff] [blame] | 1684 | } |
Fariborz Jahanian | 715fdd5 | 2012-01-29 20:27:13 +0000 | [diff] [blame] | 1685 | |
John McCall | 3d1e2c9 | 2013-02-12 05:53:35 +0000 | [diff] [blame] | 1686 | // Okay, start emitting the null-receiver block. |
Fariborz Jahanian | 715fdd5 | 2012-01-29 20:27:13 +0000 | [diff] [blame] | 1687 | CGF.EmitBlock(NullBB); |
Fariborz Jahanian | f2bda69 | 2012-01-30 21:40:37 +0000 | [diff] [blame] | 1688 | |
John McCall | 3d1e2c9 | 2013-02-12 05:53:35 +0000 | [diff] [blame] | 1689 | // Release any consumed arguments we've got. |
Fariborz Jahanian | f2bda69 | 2012-01-30 21:40:37 +0000 | [diff] [blame] | 1690 | if (Method) { |
| 1691 | CallArgList::const_iterator I = CallArgs.begin(); |
| 1692 | for (ObjCMethodDecl::param_const_iterator i = Method->param_begin(), |
| 1693 | e = Method->param_end(); i != e; ++i, ++I) { |
| 1694 | const ParmVarDecl *ParamDecl = (*i); |
| 1695 | if (ParamDecl->hasAttr<NSConsumedAttr>()) { |
| 1696 | RValue RV = I->RV; |
| 1697 | assert(RV.isScalar() && |
| 1698 | "NullReturnState::complete - arg not on object"); |
John McCall | cdda29c | 2013-03-13 03:10:54 +0000 | [diff] [blame] | 1699 | CGF.EmitARCRelease(RV.getScalarVal(), ARCImpreciseLifetime); |
Fariborz Jahanian | f2bda69 | 2012-01-30 21:40:37 +0000 | [diff] [blame] | 1700 | } |
| 1701 | } |
| 1702 | } |
John McCall | 3d1e2c9 | 2013-02-12 05:53:35 +0000 | [diff] [blame] | 1703 | |
| 1704 | // The phi code below assumes that we haven't needed any control flow yet. |
| 1705 | assert(CGF.Builder.GetInsertBlock() == NullBB); |
| 1706 | |
| 1707 | // If we've got a void return, just jump to the continuation block. |
| 1708 | if (result.isScalar() && resultType->isVoidType()) { |
| 1709 | // No jumps required if the message-send was noreturn. |
| 1710 | if (contBB) CGF.EmitBlock(contBB); |
Fariborz Jahanian | 715fdd5 | 2012-01-29 20:27:13 +0000 | [diff] [blame] | 1711 | return result; |
| 1712 | } |
| 1713 | |
John McCall | 3d1e2c9 | 2013-02-12 05:53:35 +0000 | [diff] [blame] | 1714 | // If we've got a scalar return, build a phi. |
| 1715 | if (result.isScalar()) { |
| 1716 | // Derive the null-initialization value. |
| 1717 | llvm::Constant *null = CGF.CGM.EmitNullConstant(resultType); |
| 1718 | |
| 1719 | // If no join is necessary, just flow out. |
| 1720 | if (!contBB) return RValue::get(null); |
| 1721 | |
| 1722 | // Otherwise, build a phi. |
| 1723 | CGF.EmitBlock(contBB); |
| 1724 | llvm::PHINode *phi = CGF.Builder.CreatePHI(null->getType(), 2); |
| 1725 | phi->addIncoming(result.getScalarVal(), callBB); |
| 1726 | phi->addIncoming(null, NullBB); |
| 1727 | return RValue::get(phi); |
| 1728 | } |
| 1729 | |
| 1730 | // If we've got an aggregate return, null the buffer out. |
| 1731 | // FIXME: maybe we should be doing things differently for all the |
| 1732 | // cases where the ABI has us returning (1) non-agg values in |
| 1733 | // memory or (2) agg values in registers. |
| 1734 | if (result.isAggregate()) { |
| 1735 | assert(result.isAggregate() && "null init of non-aggregate result?"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1736 | CGF.EmitNullInitialization(result.getAggregateAddress(), resultType); |
John McCall | 3d1e2c9 | 2013-02-12 05:53:35 +0000 | [diff] [blame] | 1737 | if (contBB) CGF.EmitBlock(contBB); |
| 1738 | return result; |
| 1739 | } |
| 1740 | |
| 1741 | // Complex types. |
Fariborz Jahanian | 715fdd5 | 2012-01-29 20:27:13 +0000 | [diff] [blame] | 1742 | CGF.EmitBlock(contBB); |
John McCall | 3d1e2c9 | 2013-02-12 05:53:35 +0000 | [diff] [blame] | 1743 | CodeGenFunction::ComplexPairTy callResult = result.getComplexVal(); |
| 1744 | |
| 1745 | // Find the scalar type and its zero value. |
| 1746 | llvm::Type *scalarTy = callResult.first->getType(); |
| 1747 | llvm::Constant *scalarZero = llvm::Constant::getNullValue(scalarTy); |
| 1748 | |
| 1749 | // Build phis for both coordinates. |
| 1750 | llvm::PHINode *real = CGF.Builder.CreatePHI(scalarTy, 2); |
| 1751 | real->addIncoming(callResult.first, callBB); |
| 1752 | real->addIncoming(scalarZero, NullBB); |
| 1753 | llvm::PHINode *imag = CGF.Builder.CreatePHI(scalarTy, 2); |
| 1754 | imag->addIncoming(callResult.second, callBB); |
| 1755 | imag->addIncoming(scalarZero, NullBB); |
| 1756 | return RValue::getComplex(real, imag); |
Fariborz Jahanian | 715fdd5 | 2012-01-29 20:27:13 +0000 | [diff] [blame] | 1757 | } |
| 1758 | }; |
| 1759 | |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1760 | } // end anonymous namespace |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1761 | |
| 1762 | /* *** Helper Functions *** */ |
| 1763 | |
| 1764 | /// getConstantGEP() - Help routine to construct simple GEPs. |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1765 | static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext, |
David Blaikie | e3b172a | 2015-04-02 18:55:21 +0000 | [diff] [blame] | 1766 | llvm::GlobalVariable *C, unsigned idx0, |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1767 | unsigned idx1) { |
| 1768 | llvm::Value *Idxs[] = { |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1769 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0), |
| 1770 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1) |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1771 | }; |
David Blaikie | e3b172a | 2015-04-02 18:55:21 +0000 | [diff] [blame] | 1772 | return llvm::ConstantExpr::getGetElementPtr(C->getValueType(), C, Idxs); |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1773 | } |
| 1774 | |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1775 | /// hasObjCExceptionAttribute - Return true if this class or any super |
| 1776 | /// class has the __objc_exception__ attribute. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1777 | static bool hasObjCExceptionAttribute(ASTContext &Context, |
Douglas Gregor | 78bd61f | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 1778 | const ObjCInterfaceDecl *OID) { |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1779 | if (OID->hasAttr<ObjCExceptionAttr>()) |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1780 | return true; |
| 1781 | if (const ObjCInterfaceDecl *Super = OID->getSuperClass()) |
Douglas Gregor | 78bd61f | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 1782 | return hasObjCExceptionAttribute(Context, Super); |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1783 | return false; |
| 1784 | } |
| 1785 | |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1786 | /* *** CGObjCMac Public Interface *** */ |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1787 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1788 | CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1789 | ObjCTypes(cgm) { |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1790 | ObjCABI = 1; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1791 | EmitImageInfo(); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1792 | } |
| 1793 | |
Daniel Dunbar | 7c6d3a7 | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 1794 | /// GetClass - Return a reference to the class for the given interface |
| 1795 | /// decl. |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1796 | llvm::Value *CGObjCMac::GetClass(CodeGenFunction &CGF, |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1797 | const ObjCInterfaceDecl *ID) { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1798 | return EmitClassRef(CGF, ID); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1799 | } |
| 1800 | |
| 1801 | /// GetSelector - Return the pointer to the unique'd string for this selector. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1802 | llvm::Value *CGObjCMac::GetSelector(CodeGenFunction &CGF, Selector Sel) { |
| 1803 | return EmitSelector(CGF, Sel); |
| 1804 | } |
| 1805 | Address CGObjCMac::GetAddrOfSelector(CodeGenFunction &CGF, Selector Sel) { |
| 1806 | return EmitSelectorAddr(CGF, Sel); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1807 | } |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1808 | llvm::Value *CGObjCMac::GetSelector(CodeGenFunction &CGF, const ObjCMethodDecl |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1809 | *Method) { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1810 | return EmitSelector(CGF, Method->getSelector()); |
Fariborz Jahanian | f3648b8 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1811 | } |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1812 | |
Fariborz Jahanian | 831f0fc | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 1813 | llvm::Constant *CGObjCMac::GetEHType(QualType T) { |
Fariborz Jahanian | 0a3cfcc | 2011-06-22 20:21:51 +0000 | [diff] [blame] | 1814 | if (T->isObjCIdType() || |
| 1815 | T->isObjCQualifiedIdType()) { |
| 1816 | return CGM.GetAddrOfRTTIDescriptor( |
Douglas Gregor | 9767347 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 1817 | CGM.getContext().getObjCIdRedefinitionType(), /*ForEH=*/true); |
Fariborz Jahanian | 0a3cfcc | 2011-06-22 20:21:51 +0000 | [diff] [blame] | 1818 | } |
Fariborz Jahanian | 831f0fc | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 1819 | if (T->isObjCClassType() || |
| 1820 | T->isObjCQualifiedClassType()) { |
| 1821 | return CGM.GetAddrOfRTTIDescriptor( |
Douglas Gregor | 9767347 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 1822 | CGM.getContext().getObjCClassRedefinitionType(), /*ForEH=*/true); |
Fariborz Jahanian | 831f0fc | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 1823 | } |
| 1824 | if (T->isObjCObjectPointerType()) |
| 1825 | return CGM.GetAddrOfRTTIDescriptor(T, /*ForEH=*/true); |
| 1826 | |
John McCall | 2ca705e | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 1827 | llvm_unreachable("asking for catch type for ObjC type in fragile runtime"); |
John McCall | 2ca705e | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 1828 | } |
| 1829 | |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1830 | /// Generate a constant CFString object. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1831 | /* |
| 1832 | struct __builtin_CFString { |
| 1833 | const int *isa; // point to __CFConstantStringClassReference |
| 1834 | int flags; |
| 1835 | const char *str; |
| 1836 | long length; |
| 1837 | }; |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1838 | */ |
| 1839 | |
Fariborz Jahanian | 63408e8 | 2010-04-22 20:26:39 +0000 | [diff] [blame] | 1840 | /// or Generate a constant NSString object. |
| 1841 | /* |
| 1842 | struct __builtin_NSString { |
| 1843 | const int *isa; // point to __NSConstantStringClassReference |
| 1844 | const char *str; |
| 1845 | unsigned int length; |
| 1846 | }; |
| 1847 | */ |
| 1848 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1849 | ConstantAddress CGObjCCommonMac::GenerateConstantString( |
David Chisnall | 481e3a8 | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 1850 | const StringLiteral *SL) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1851 | return (CGM.getLangOpts().NoConstantCFStrings == 0 ? |
Fariborz Jahanian | 63408e8 | 2010-04-22 20:26:39 +0000 | [diff] [blame] | 1852 | CGM.GetAddrOfConstantCFString(SL) : |
Fariborz Jahanian | 50c925f | 2010-10-19 17:19:29 +0000 | [diff] [blame] | 1853 | CGM.GetAddrOfConstantString(SL)); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1854 | } |
| 1855 | |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 1856 | enum { |
| 1857 | kCFTaggedObjectID_Integer = (1 << 1) + 1 |
| 1858 | }; |
| 1859 | |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1860 | /// Generates a message send where the super is the receiver. This is |
| 1861 | /// a message send to self with special delivery semantics indicating |
| 1862 | /// which class's method should be called. |
Daniel Dunbar | 97db84c | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1863 | CodeGen::RValue |
| 1864 | CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
John McCall | 78a1511 | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1865 | ReturnValueSlot Return, |
Daniel Dunbar | 4b8c6db | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1866 | QualType ResultType, |
| 1867 | Selector Sel, |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1868 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | bac73ac | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1869 | bool isCategoryImpl, |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1870 | llvm::Value *Receiver, |
Daniel Dunbar | c722b85 | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1871 | bool IsClassMessage, |
Daniel Dunbar | aff9fca | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 1872 | const CodeGen::CallArgList &CallArgs, |
| 1873 | const ObjCMethodDecl *Method) { |
Daniel Dunbar | f6397fe | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1874 | // Create and init a super structure; this is a (receiver, class) |
| 1875 | // pair we will pass to objc_msgSendSuper. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1876 | Address ObjCSuper = |
| 1877 | CGF.CreateTempAlloca(ObjCTypes.SuperTy, CGF.getPointerAlign(), |
| 1878 | "objc_super"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1879 | llvm::Value *ReceiverAsObject = |
Daniel Dunbar | f6397fe | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1880 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 1881 | CGF.Builder.CreateStore( |
| 1882 | ReceiverAsObject, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1883 | CGF.Builder.CreateStructGEP(ObjCSuper, 0, CharUnits::Zero())); |
Daniel Dunbar | f6397fe | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1884 | |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1885 | // If this is a class message the metaclass is passed as the target. |
| 1886 | llvm::Value *Target; |
| 1887 | if (IsClassMessage) { |
Fariborz Jahanian | bac73ac | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1888 | if (isCategoryImpl) { |
| 1889 | // Message sent to 'super' in a class method defined in a category |
| 1890 | // implementation requires an odd treatment. |
| 1891 | // If we are in a class method, we must retrieve the |
| 1892 | // _metaclass_ for the current class, pointed at by |
| 1893 | // the class's "isa" pointer. The following assumes that |
| 1894 | // isa" is the first ivar in a class (which it must be). |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1895 | Target = EmitClassRef(CGF, Class->getSuperClass()); |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 1896 | Target = CGF.Builder.CreateStructGEP(ObjCTypes.ClassTy, Target, 0); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1897 | Target = CGF.Builder.CreateAlignedLoad(Target, CGF.getPointerAlign()); |
Mike Stump | 658fe02 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 1898 | } else { |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 1899 | llvm::Constant *MetaClassPtr = EmitMetaClassRef(Class); |
| 1900 | llvm::Value *SuperPtr = |
| 1901 | CGF.Builder.CreateStructGEP(ObjCTypes.ClassTy, MetaClassPtr, 1); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1902 | llvm::Value *Super = |
| 1903 | CGF.Builder.CreateAlignedLoad(SuperPtr, CGF.getPointerAlign()); |
Fariborz Jahanian | bac73ac | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1904 | Target = Super; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1905 | } |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 1906 | } else if (isCategoryImpl) |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1907 | Target = EmitClassRef(CGF, Class->getSuperClass()); |
Fariborz Jahanian | da2efb0 | 2009-11-14 02:18:31 +0000 | [diff] [blame] | 1908 | else { |
Fariborz Jahanian | eb80c98 | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 1909 | llvm::Value *ClassPtr = EmitSuperClassRef(Class); |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 1910 | ClassPtr = CGF.Builder.CreateStructGEP(ObjCTypes.ClassTy, ClassPtr, 1); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1911 | Target = CGF.Builder.CreateAlignedLoad(ClassPtr, CGF.getPointerAlign()); |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1912 | } |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1913 | // FIXME: We shouldn't need to do this cast, rectify the ASTContext and |
| 1914 | // ObjCTypes types. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1915 | llvm::Type *ClassTy = |
Daniel Dunbar | c722b85 | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1916 | CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1917 | Target = CGF.Builder.CreateBitCast(Target, ClassTy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1918 | CGF.Builder.CreateStore(Target, |
| 1919 | CGF.Builder.CreateStructGEP(ObjCSuper, 1, CGF.getPointerSize())); |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1920 | return EmitMessageSend(CGF, Return, ResultType, |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1921 | EmitSelector(CGF, Sel), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1922 | ObjCSuper.getPointer(), ObjCTypes.SuperPtrCTy, |
John McCall | 1e3157b | 2015-09-10 22:27:50 +0000 | [diff] [blame] | 1923 | true, CallArgs, Method, Class, ObjCTypes); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1924 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1925 | |
| 1926 | /// Generate code for a message send expression. |
Daniel Dunbar | 97db84c | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1927 | CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
John McCall | 78a1511 | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1928 | ReturnValueSlot Return, |
Daniel Dunbar | 4b8c6db | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1929 | QualType ResultType, |
| 1930 | Selector Sel, |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1931 | llvm::Value *Receiver, |
Fariborz Jahanian | f3648b8 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1932 | const CallArgList &CallArgs, |
David Chisnall | 01aa467 | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 1933 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | f3648b8 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1934 | const ObjCMethodDecl *Method) { |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1935 | return EmitMessageSend(CGF, Return, ResultType, |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1936 | EmitSelector(CGF, Sel), |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1937 | Receiver, CGF.getContext().getObjCIdType(), |
John McCall | 1e3157b | 2015-09-10 22:27:50 +0000 | [diff] [blame] | 1938 | false, CallArgs, Method, Class, ObjCTypes); |
| 1939 | } |
| 1940 | |
| 1941 | static bool isWeakLinkedClass(const ObjCInterfaceDecl *ID) { |
| 1942 | do { |
| 1943 | if (ID->isWeakImported()) |
| 1944 | return true; |
| 1945 | } while ((ID = ID->getSuperClass())); |
| 1946 | |
| 1947 | return false; |
Daniel Dunbar | 97ff50d | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1948 | } |
| 1949 | |
Daniel Dunbar | aff9fca | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 1950 | CodeGen::RValue |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1951 | CGObjCCommonMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
| 1952 | ReturnValueSlot Return, |
| 1953 | QualType ResultType, |
| 1954 | llvm::Value *Sel, |
| 1955 | llvm::Value *Arg0, |
| 1956 | QualType Arg0Ty, |
| 1957 | bool IsSuper, |
| 1958 | const CallArgList &CallArgs, |
| 1959 | const ObjCMethodDecl *Method, |
John McCall | 1e3157b | 2015-09-10 22:27:50 +0000 | [diff] [blame] | 1960 | const ObjCInterfaceDecl *ClassReceiver, |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1961 | const ObjCCommonTypesHelper &ObjCTypes) { |
Daniel Dunbar | c722b85 | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1962 | CallArgList ActualArgs; |
Fariborz Jahanian | 969bc68 | 2009-04-24 21:07:43 +0000 | [diff] [blame] | 1963 | if (!IsSuper) |
Benjamin Kramer | 76399eb | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 1964 | Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy); |
Eli Friedman | 43dca6a | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 1965 | ActualArgs.add(RValue::get(Arg0), Arg0Ty); |
| 1966 | ActualArgs.add(RValue::get(Sel), CGF.getContext().getObjCSelType()); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1967 | ActualArgs.addFrom(CallArgs); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1968 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1969 | // If we're calling a method, use the formal signature. |
| 1970 | MessageSendInfo MSI = getMessageSendInfo(Method, ResultType, ActualArgs); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1971 | |
Anders Carlsson | 280e61f1 | 2010-06-21 20:59:55 +0000 | [diff] [blame] | 1972 | if (Method) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1973 | assert(CGM.getContext().getCanonicalType(Method->getReturnType()) == |
| 1974 | CGM.getContext().getCanonicalType(ResultType) && |
Anders Carlsson | 280e61f1 | 2010-06-21 20:59:55 +0000 | [diff] [blame] | 1975 | "Result type mismatch!"); |
| 1976 | |
John McCall | 1e3157b | 2015-09-10 22:27:50 +0000 | [diff] [blame] | 1977 | bool ReceiverCanBeNull = true; |
| 1978 | |
| 1979 | // Super dispatch assumes that self is non-null; even the messenger |
| 1980 | // doesn't have a null check internally. |
| 1981 | if (IsSuper) { |
| 1982 | ReceiverCanBeNull = false; |
| 1983 | |
| 1984 | // If this is a direct dispatch of a class method, check whether the class, |
| 1985 | // or anything in its hierarchy, was weak-linked. |
| 1986 | } else if (ClassReceiver && Method && Method->isClassMethod()) { |
| 1987 | ReceiverCanBeNull = isWeakLinkedClass(ClassReceiver); |
| 1988 | |
| 1989 | // If we're emitting a method, and self is const (meaning just ARC, for now), |
| 1990 | // and the receiver is a load of self, then self is a valid object. |
| 1991 | } else if (auto CurMethod = |
| 1992 | dyn_cast_or_null<ObjCMethodDecl>(CGF.CurCodeDecl)) { |
| 1993 | auto Self = CurMethod->getSelfDecl(); |
| 1994 | if (Self->getType().isConstQualified()) { |
| 1995 | if (auto LI = dyn_cast<llvm::LoadInst>(Arg0->stripPointerCasts())) { |
| 1996 | llvm::Value *SelfAddr = CGF.GetAddrOfLocalVar(Self).getPointer(); |
| 1997 | if (SelfAddr == LI->getPointerOperand()) { |
| 1998 | ReceiverCanBeNull = false; |
| 1999 | } |
| 2000 | } |
| 2001 | } |
| 2002 | } |
| 2003 | |
John McCall | 5880fb8 | 2011-05-14 21:12:11 +0000 | [diff] [blame] | 2004 | NullReturnState nullReturn; |
| 2005 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2006 | llvm::Constant *Fn = nullptr; |
Tim Northover | e77cc39 | 2014-03-29 13:28:05 +0000 | [diff] [blame] | 2007 | if (CGM.ReturnSlotInterferesWithArgs(MSI.CallInfo)) { |
John McCall | 1e3157b | 2015-09-10 22:27:50 +0000 | [diff] [blame] | 2008 | if (ReceiverCanBeNull) nullReturn.init(CGF, Arg0); |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 2009 | Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2010 | : ObjCTypes.getSendStretFn(IsSuper); |
Daniel Dunbar | 6f2e839 | 2010-07-14 23:39:36 +0000 | [diff] [blame] | 2011 | } else if (CGM.ReturnTypeUsesFPRet(ResultType)) { |
| 2012 | Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper) |
| 2013 | : ObjCTypes.getSendFpretFn(IsSuper); |
Anders Carlsson | 2f1a6c3 | 2011-10-31 16:27:11 +0000 | [diff] [blame] | 2014 | } else if (CGM.ReturnTypeUsesFP2Ret(ResultType)) { |
| 2015 | Fn = (ObjCABI == 2) ? ObjCTypes.getSendFp2RetFn2(IsSuper) |
| 2016 | : ObjCTypes.getSendFp2retFn(IsSuper); |
Daniel Dunbar | 3c683f5b | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 2017 | } else { |
Tim Northover | e77cc39 | 2014-03-29 13:28:05 +0000 | [diff] [blame] | 2018 | // arm64 uses objc_msgSend for stret methods and yet null receiver check |
| 2019 | // must be made for it. |
Ahmed Bougacha | a3df87b | 2015-10-02 22:41:59 +0000 | [diff] [blame] | 2020 | if (ReceiverCanBeNull && CGM.ReturnTypeUsesSRet(MSI.CallInfo)) |
Tim Northover | e77cc39 | 2014-03-29 13:28:05 +0000 | [diff] [blame] | 2021 | nullReturn.init(CGF, Arg0); |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 2022 | Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2023 | : ObjCTypes.getSendFn(IsSuper); |
Daniel Dunbar | 3c683f5b | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 2024 | } |
John McCall | 1e3157b | 2015-09-10 22:27:50 +0000 | [diff] [blame] | 2025 | |
| 2026 | // Emit a null-check if there's a consumed argument other than the receiver. |
| 2027 | bool RequiresNullCheck = false; |
| 2028 | if (ReceiverCanBeNull && CGM.getLangOpts().ObjCAutoRefCount && Method) { |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 2029 | for (const auto *ParamDecl : Method->parameters()) { |
Fariborz Jahanian | f2bda69 | 2012-01-30 21:40:37 +0000 | [diff] [blame] | 2030 | if (ParamDecl->hasAttr<NSConsumedAttr>()) { |
| 2031 | if (!nullReturn.NullBB) |
| 2032 | nullReturn.init(CGF, Arg0); |
John McCall | 1e3157b | 2015-09-10 22:27:50 +0000 | [diff] [blame] | 2033 | RequiresNullCheck = true; |
Fariborz Jahanian | f2bda69 | 2012-01-30 21:40:37 +0000 | [diff] [blame] | 2034 | break; |
| 2035 | } |
| 2036 | } |
John McCall | 1e3157b | 2015-09-10 22:27:50 +0000 | [diff] [blame] | 2037 | } |
Fariborz Jahanian | f2bda69 | 2012-01-30 21:40:37 +0000 | [diff] [blame] | 2038 | |
John McCall | 1e3157b | 2015-09-10 22:27:50 +0000 | [diff] [blame] | 2039 | llvm::Instruction *CallSite; |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2040 | Fn = llvm::ConstantExpr::getBitCast(Fn, MSI.MessengerType); |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 2041 | CGCallee Callee = CGCallee::forDirect(Fn); |
| 2042 | RValue rvalue = CGF.EmitCall(MSI.CallInfo, Callee, Return, ActualArgs, |
| 2043 | &CallSite); |
John McCall | 1e3157b | 2015-09-10 22:27:50 +0000 | [diff] [blame] | 2044 | |
| 2045 | // Mark the call as noreturn if the method is marked noreturn and the |
| 2046 | // receiver cannot be null. |
| 2047 | if (Method && Method->hasAttr<NoReturnAttr>() && !ReceiverCanBeNull) { |
| 2048 | llvm::CallSite(CallSite).setDoesNotReturn(); |
| 2049 | } |
| 2050 | |
Fariborz Jahanian | f2bda69 | 2012-01-30 21:40:37 +0000 | [diff] [blame] | 2051 | return nullReturn.complete(CGF, rvalue, ResultType, CallArgs, |
John McCall | 1e3157b | 2015-09-10 22:27:50 +0000 | [diff] [blame] | 2052 | RequiresNullCheck ? Method : nullptr); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2053 | } |
| 2054 | |
John McCall | 3fd13f06 | 2015-10-21 18:06:47 +0000 | [diff] [blame] | 2055 | static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT, |
| 2056 | bool pointee = false) { |
| 2057 | // Note that GC qualification applies recursively to C pointer types |
| 2058 | // that aren't otherwise decorated. This is weird, but it's probably |
| 2059 | // an intentional workaround to the unreliable placement of GC qualifiers. |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 2060 | if (FQT.isObjCGCStrong()) |
| 2061 | return Qualifiers::Strong; |
John McCall | 3fd13f06 | 2015-10-21 18:06:47 +0000 | [diff] [blame] | 2062 | |
| 2063 | if (FQT.isObjCGCWeak()) |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 2064 | return Qualifiers::Weak; |
John McCall | 3fd13f06 | 2015-10-21 18:06:47 +0000 | [diff] [blame] | 2065 | |
| 2066 | if (auto ownership = FQT.getObjCLifetime()) { |
| 2067 | // Ownership does not apply recursively to C pointer types. |
| 2068 | if (pointee) return Qualifiers::GCNone; |
| 2069 | switch (ownership) { |
| 2070 | case Qualifiers::OCL_Weak: return Qualifiers::Weak; |
| 2071 | case Qualifiers::OCL_Strong: return Qualifiers::Strong; |
| 2072 | case Qualifiers::OCL_ExplicitNone: return Qualifiers::GCNone; |
| 2073 | case Qualifiers::OCL_Autoreleasing: llvm_unreachable("autoreleasing ivar?"); |
| 2074 | case Qualifiers::OCL_None: llvm_unreachable("known nonzero"); |
| 2075 | } |
| 2076 | llvm_unreachable("bad objc ownership"); |
| 2077 | } |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 2078 | |
John McCall | 3fd13f06 | 2015-10-21 18:06:47 +0000 | [diff] [blame] | 2079 | // Treat unqualified retainable pointers as strong. |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 2080 | if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType()) |
| 2081 | return Qualifiers::Strong; |
| 2082 | |
John McCall | 3fd13f06 | 2015-10-21 18:06:47 +0000 | [diff] [blame] | 2083 | // Walk into C pointer types, but only in GC. |
| 2084 | if (Ctx.getLangOpts().getGC() != LangOptions::NonGC) { |
| 2085 | if (const PointerType *PT = FQT->getAs<PointerType>()) |
| 2086 | return GetGCAttrTypeForType(Ctx, PT->getPointeeType(), /*pointee*/ true); |
| 2087 | } |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 2088 | |
| 2089 | return Qualifiers::GCNone; |
| 2090 | } |
| 2091 | |
John McCall | 3fd13f06 | 2015-10-21 18:06:47 +0000 | [diff] [blame] | 2092 | namespace { |
| 2093 | struct IvarInfo { |
| 2094 | CharUnits Offset; |
| 2095 | uint64_t SizeInWords; |
| 2096 | IvarInfo(CharUnits offset, uint64_t sizeInWords) |
| 2097 | : Offset(offset), SizeInWords(sizeInWords) {} |
| 2098 | |
| 2099 | // Allow sorting based on byte pos. |
| 2100 | bool operator<(const IvarInfo &other) const { |
| 2101 | return Offset < other.Offset; |
| 2102 | } |
| 2103 | }; |
| 2104 | |
| 2105 | /// A helper class for building GC layout strings. |
| 2106 | class IvarLayoutBuilder { |
| 2107 | CodeGenModule &CGM; |
| 2108 | |
| 2109 | /// The start of the layout. Offsets will be relative to this value, |
| 2110 | /// and entries less than this value will be silently discarded. |
| 2111 | CharUnits InstanceBegin; |
| 2112 | |
| 2113 | /// The end of the layout. Offsets will never exceed this value. |
| 2114 | CharUnits InstanceEnd; |
| 2115 | |
| 2116 | /// Whether we're generating the strong layout or the weak layout. |
| 2117 | bool ForStrongLayout; |
| 2118 | |
| 2119 | /// Whether the offsets in IvarsInfo might be out-of-order. |
| 2120 | bool IsDisordered = false; |
| 2121 | |
| 2122 | llvm::SmallVector<IvarInfo, 8> IvarsInfo; |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 2123 | |
John McCall | 3fd13f06 | 2015-10-21 18:06:47 +0000 | [diff] [blame] | 2124 | public: |
| 2125 | IvarLayoutBuilder(CodeGenModule &CGM, CharUnits instanceBegin, |
| 2126 | CharUnits instanceEnd, bool forStrongLayout) |
| 2127 | : CGM(CGM), InstanceBegin(instanceBegin), InstanceEnd(instanceEnd), |
| 2128 | ForStrongLayout(forStrongLayout) { |
| 2129 | } |
| 2130 | |
| 2131 | void visitRecord(const RecordType *RT, CharUnits offset); |
| 2132 | |
| 2133 | template <class Iterator, class GetOffsetFn> |
| 2134 | void visitAggregate(Iterator begin, Iterator end, |
| 2135 | CharUnits aggrOffset, |
| 2136 | const GetOffsetFn &getOffset); |
| 2137 | |
| 2138 | void visitField(const FieldDecl *field, CharUnits offset); |
| 2139 | |
| 2140 | /// Add the layout of a block implementation. |
| 2141 | void visitBlock(const CGBlockInfo &blockInfo); |
| 2142 | |
| 2143 | /// Is there any information for an interesting bitmap? |
| 2144 | bool hasBitmapData() const { return !IvarsInfo.empty(); } |
| 2145 | |
| 2146 | llvm::Constant *buildBitmap(CGObjCCommonMac &CGObjC, |
| 2147 | llvm::SmallVectorImpl<unsigned char> &buffer); |
| 2148 | |
| 2149 | static void dump(ArrayRef<unsigned char> buffer) { |
| 2150 | const unsigned char *s = buffer.data(); |
| 2151 | for (unsigned i = 0, e = buffer.size(); i < e; i++) |
| 2152 | if (!(s[i] & 0xf0)) |
| 2153 | printf("0x0%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 2154 | else |
| 2155 | printf("0x%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 2156 | printf("\n"); |
| 2157 | } |
| 2158 | }; |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 2159 | } // end anonymous namespace |
John McCall | 3fd13f06 | 2015-10-21 18:06:47 +0000 | [diff] [blame] | 2160 | |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2161 | llvm::Constant *CGObjCCommonMac::BuildGCBlockLayout(CodeGenModule &CGM, |
| 2162 | const CGBlockInfo &blockInfo) { |
Fariborz Jahanian | 0c58ce9 | 2012-10-27 21:10:38 +0000 | [diff] [blame] | 2163 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2164 | llvm::Constant *nullPtr = llvm::Constant::getNullValue(CGM.Int8PtrTy); |
John McCall | 460ce58 | 2015-10-22 18:38:17 +0000 | [diff] [blame] | 2165 | if (CGM.getLangOpts().getGC() == LangOptions::NonGC) |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2166 | return nullPtr; |
| 2167 | |
John McCall | 3fd13f06 | 2015-10-21 18:06:47 +0000 | [diff] [blame] | 2168 | IvarLayoutBuilder builder(CGM, CharUnits::Zero(), blockInfo.BlockSize, |
| 2169 | /*for strong layout*/ true); |
| 2170 | |
| 2171 | builder.visitBlock(blockInfo); |
| 2172 | |
| 2173 | if (!builder.hasBitmapData()) |
| 2174 | return nullPtr; |
| 2175 | |
| 2176 | llvm::SmallVector<unsigned char, 32> buffer; |
| 2177 | llvm::Constant *C = builder.buildBitmap(*this, buffer); |
John McCall | f5ea072 | 2015-10-29 23:36:14 +0000 | [diff] [blame] | 2178 | if (CGM.getLangOpts().ObjCGCBitmapPrint && !buffer.empty()) { |
John McCall | 3fd13f06 | 2015-10-21 18:06:47 +0000 | [diff] [blame] | 2179 | printf("\n block variable layout for block: "); |
| 2180 | builder.dump(buffer); |
| 2181 | } |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 2182 | |
John McCall | 3fd13f06 | 2015-10-21 18:06:47 +0000 | [diff] [blame] | 2183 | return C; |
| 2184 | } |
| 2185 | |
| 2186 | void IvarLayoutBuilder::visitBlock(const CGBlockInfo &blockInfo) { |
Fariborz Jahanian | cfddabf | 2010-09-09 00:21:45 +0000 | [diff] [blame] | 2187 | // __isa is the first field in block descriptor and must assume by runtime's |
| 2188 | // convention that it is GC'able. |
John McCall | 3fd13f06 | 2015-10-21 18:06:47 +0000 | [diff] [blame] | 2189 | IvarsInfo.push_back(IvarInfo(CharUnits::Zero(), 1)); |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2190 | |
| 2191 | const BlockDecl *blockDecl = blockInfo.getBlockDecl(); |
| 2192 | |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2193 | // Ignore the optional 'this' capture: C++ objects are not assumed |
| 2194 | // to be GC'ed. |
| 2195 | |
John McCall | 3fd13f06 | 2015-10-21 18:06:47 +0000 | [diff] [blame] | 2196 | CharUnits lastFieldOffset; |
| 2197 | |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2198 | // Walk the captured variables. |
Aaron Ballman | 9371dd2 | 2014-03-14 18:34:04 +0000 | [diff] [blame] | 2199 | for (const auto &CI : blockDecl->captures()) { |
| 2200 | const VarDecl *variable = CI.getVariable(); |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2201 | QualType type = variable->getType(); |
| 2202 | |
| 2203 | const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable); |
| 2204 | |
| 2205 | // Ignore constant captures. |
| 2206 | if (capture.isConstant()) continue; |
| 2207 | |
John McCall | 3fd13f06 | 2015-10-21 18:06:47 +0000 | [diff] [blame] | 2208 | CharUnits fieldOffset = capture.getOffset(); |
| 2209 | |
| 2210 | // Block fields are not necessarily ordered; if we detect that we're |
| 2211 | // adding them out-of-order, make sure we sort later. |
| 2212 | if (fieldOffset < lastFieldOffset) |
| 2213 | IsDisordered = true; |
| 2214 | lastFieldOffset = fieldOffset; |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2215 | |
| 2216 | // __block variables are passed by their descriptor address. |
Aaron Ballman | 9371dd2 | 2014-03-14 18:34:04 +0000 | [diff] [blame] | 2217 | if (CI.isByRef()) { |
John McCall | 3fd13f06 | 2015-10-21 18:06:47 +0000 | [diff] [blame] | 2218 | IvarsInfo.push_back(IvarInfo(fieldOffset, /*size in words*/ 1)); |
Fariborz Jahanian | 933c672 | 2010-09-11 01:27:29 +0000 | [diff] [blame] | 2219 | continue; |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2220 | } |
| 2221 | |
| 2222 | assert(!type->isArrayType() && "array variable should not be caught"); |
| 2223 | if (const RecordType *record = type->getAs<RecordType>()) { |
John McCall | 3fd13f06 | 2015-10-21 18:06:47 +0000 | [diff] [blame] | 2224 | visitRecord(record, fieldOffset); |
Fariborz Jahanian | 903aba3 | 2010-08-05 21:00:25 +0000 | [diff] [blame] | 2225 | continue; |
| 2226 | } |
Fariborz Jahanian | f95e358 | 2010-08-06 16:28:55 +0000 | [diff] [blame] | 2227 | |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2228 | Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), type); |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2229 | |
John McCall | 3fd13f06 | 2015-10-21 18:06:47 +0000 | [diff] [blame] | 2230 | if (GCAttr == Qualifiers::Strong) { |
| 2231 | assert(CGM.getContext().getTypeSize(type) |
| 2232 | == CGM.getTarget().getPointerWidth(0)); |
| 2233 | IvarsInfo.push_back(IvarInfo(fieldOffset, /*size in words*/ 1)); |
| 2234 | } |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 2235 | } |
Fariborz Jahanian | c05349e | 2010-08-04 16:57:49 +0000 | [diff] [blame] | 2236 | } |
| 2237 | |
Fariborz Jahanian | 2c96d30 | 2012-11-04 18:19:40 +0000 | [diff] [blame] | 2238 | /// getBlockCaptureLifetime - This routine returns life time of the captured |
| 2239 | /// block variable for the purpose of block layout meta-data generation. FQT is |
| 2240 | /// the type of the variable captured in the block. |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 2241 | Qualifiers::ObjCLifetime CGObjCCommonMac::getBlockCaptureLifetime(QualType FQT, |
| 2242 | bool ByrefLayout) { |
John McCall | 460ce58 | 2015-10-22 18:38:17 +0000 | [diff] [blame] | 2243 | // If it has an ownership qualifier, we're done. |
| 2244 | if (auto lifetime = FQT.getObjCLifetime()) |
| 2245 | return lifetime; |
| 2246 | |
| 2247 | // If it doesn't, and this is ARC, it has no ownership. |
Fariborz Jahanian | 2dd7819 | 2012-11-02 22:51:18 +0000 | [diff] [blame] | 2248 | if (CGM.getLangOpts().ObjCAutoRefCount) |
John McCall | 460ce58 | 2015-10-22 18:38:17 +0000 | [diff] [blame] | 2249 | return Qualifiers::OCL_None; |
Fariborz Jahanian | 2dd7819 | 2012-11-02 22:51:18 +0000 | [diff] [blame] | 2250 | |
John McCall | 460ce58 | 2015-10-22 18:38:17 +0000 | [diff] [blame] | 2251 | // In MRC, retainable pointers are owned by non-__block variables. |
Fariborz Jahanian | 2dd7819 | 2012-11-02 22:51:18 +0000 | [diff] [blame] | 2252 | if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType()) |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 2253 | return ByrefLayout ? Qualifiers::OCL_ExplicitNone : Qualifiers::OCL_Strong; |
Fariborz Jahanian | 2dd7819 | 2012-11-02 22:51:18 +0000 | [diff] [blame] | 2254 | |
| 2255 | return Qualifiers::OCL_None; |
| 2256 | } |
| 2257 | |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2258 | void CGObjCCommonMac::UpdateRunSkipBlockVars(bool IsByref, |
| 2259 | Qualifiers::ObjCLifetime LifeTime, |
Fariborz Jahanian | 7778d61 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2260 | CharUnits FieldOffset, |
| 2261 | CharUnits FieldSize) { |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2262 | // __block variables are passed by their descriptor address. |
| 2263 | if (IsByref) |
| 2264 | RunSkipBlockVars.push_back(RUN_SKIP(BLOCK_LAYOUT_BYREF, FieldOffset, |
Fariborz Jahanian | 7778d61 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2265 | FieldSize)); |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2266 | else if (LifeTime == Qualifiers::OCL_Strong) |
| 2267 | RunSkipBlockVars.push_back(RUN_SKIP(BLOCK_LAYOUT_STRONG, FieldOffset, |
Fariborz Jahanian | 7778d61 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2268 | FieldSize)); |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2269 | else if (LifeTime == Qualifiers::OCL_Weak) |
| 2270 | RunSkipBlockVars.push_back(RUN_SKIP(BLOCK_LAYOUT_WEAK, FieldOffset, |
Fariborz Jahanian | 7778d61 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2271 | FieldSize)); |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2272 | else if (LifeTime == Qualifiers::OCL_ExplicitNone) |
| 2273 | RunSkipBlockVars.push_back(RUN_SKIP(BLOCK_LAYOUT_UNRETAINED, FieldOffset, |
Fariborz Jahanian | 7778d61 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2274 | FieldSize)); |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2275 | else |
| 2276 | RunSkipBlockVars.push_back(RUN_SKIP(BLOCK_LAYOUT_NON_OBJECT_BYTES, |
| 2277 | FieldOffset, |
Fariborz Jahanian | 7778d61 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2278 | FieldSize)); |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2279 | } |
| 2280 | |
| 2281 | void CGObjCCommonMac::BuildRCRecordLayout(const llvm::StructLayout *RecLayout, |
| 2282 | const RecordDecl *RD, |
| 2283 | ArrayRef<const FieldDecl*> RecFields, |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 2284 | CharUnits BytePos, bool &HasUnion, |
| 2285 | bool ByrefLayout) { |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2286 | bool IsUnion = (RD && RD->isUnion()); |
Fariborz Jahanian | 7778d61 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2287 | CharUnits MaxUnionSize = CharUnits::Zero(); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2288 | const FieldDecl *MaxField = nullptr; |
| 2289 | const FieldDecl *LastFieldBitfieldOrUnnamed = nullptr; |
Fariborz Jahanian | 7778d61 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2290 | CharUnits MaxFieldOffset = CharUnits::Zero(); |
| 2291 | CharUnits LastBitfieldOrUnnamedOffset = CharUnits::Zero(); |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2292 | |
| 2293 | if (RecFields.empty()) |
| 2294 | return; |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 2295 | unsigned ByteSizeInBits = CGM.getTarget().getCharWidth(); |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2296 | |
| 2297 | for (unsigned i = 0, e = RecFields.size(); i != e; ++i) { |
| 2298 | const FieldDecl *Field = RecFields[i]; |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2299 | // Note that 'i' here is actually the field index inside RD of Field, |
| 2300 | // although this dependency is hidden. |
| 2301 | const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD); |
Fariborz Jahanian | 7778d61 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2302 | CharUnits FieldOffset = |
| 2303 | CGM.getContext().toCharUnitsFromBits(RL.getFieldOffset(i)); |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2304 | |
| 2305 | // Skip over unnamed or bitfields |
| 2306 | if (!Field->getIdentifier() || Field->isBitField()) { |
| 2307 | LastFieldBitfieldOrUnnamed = Field; |
| 2308 | LastBitfieldOrUnnamedOffset = FieldOffset; |
| 2309 | continue; |
| 2310 | } |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2311 | |
| 2312 | LastFieldBitfieldOrUnnamed = nullptr; |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2313 | QualType FQT = Field->getType(); |
| 2314 | if (FQT->isRecordType() || FQT->isUnionType()) { |
| 2315 | if (FQT->isUnionType()) |
| 2316 | HasUnion = true; |
| 2317 | |
| 2318 | BuildRCBlockVarRecordLayout(FQT->getAs<RecordType>(), |
| 2319 | BytePos + FieldOffset, HasUnion); |
| 2320 | continue; |
| 2321 | } |
| 2322 | |
| 2323 | if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { |
| 2324 | const ConstantArrayType *CArray = |
| 2325 | dyn_cast_or_null<ConstantArrayType>(Array); |
| 2326 | uint64_t ElCount = CArray->getSize().getZExtValue(); |
| 2327 | assert(CArray && "only array with known element size is supported"); |
| 2328 | FQT = CArray->getElementType(); |
| 2329 | while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { |
| 2330 | const ConstantArrayType *CArray = |
| 2331 | dyn_cast_or_null<ConstantArrayType>(Array); |
| 2332 | ElCount *= CArray->getSize().getZExtValue(); |
| 2333 | FQT = CArray->getElementType(); |
| 2334 | } |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2335 | if (FQT->isRecordType() && ElCount) { |
| 2336 | int OldIndex = RunSkipBlockVars.size() - 1; |
| 2337 | const RecordType *RT = FQT->getAs<RecordType>(); |
| 2338 | BuildRCBlockVarRecordLayout(RT, BytePos + FieldOffset, |
| 2339 | HasUnion); |
| 2340 | |
| 2341 | // Replicate layout information for each array element. Note that |
| 2342 | // one element is already done. |
| 2343 | uint64_t ElIx = 1; |
| 2344 | for (int FirstIndex = RunSkipBlockVars.size() - 1 ;ElIx < ElCount; ElIx++) { |
Fariborz Jahanian | 7778d61 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2345 | CharUnits Size = CGM.getContext().getTypeSizeInChars(RT); |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2346 | for (int i = OldIndex+1; i <= FirstIndex; ++i) |
| 2347 | RunSkipBlockVars.push_back( |
| 2348 | RUN_SKIP(RunSkipBlockVars[i].opcode, |
| 2349 | RunSkipBlockVars[i].block_var_bytepos + Size*ElIx, |
| 2350 | RunSkipBlockVars[i].block_var_size)); |
| 2351 | } |
| 2352 | continue; |
| 2353 | } |
| 2354 | } |
Fariborz Jahanian | 7778d61 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2355 | CharUnits FieldSize = CGM.getContext().getTypeSizeInChars(Field->getType()); |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2356 | if (IsUnion) { |
Fariborz Jahanian | 7778d61 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2357 | CharUnits UnionIvarSize = FieldSize; |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2358 | if (UnionIvarSize > MaxUnionSize) { |
| 2359 | MaxUnionSize = UnionIvarSize; |
| 2360 | MaxField = Field; |
| 2361 | MaxFieldOffset = FieldOffset; |
| 2362 | } |
| 2363 | } else { |
| 2364 | UpdateRunSkipBlockVars(false, |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 2365 | getBlockCaptureLifetime(FQT, ByrefLayout), |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2366 | BytePos + FieldOffset, |
| 2367 | FieldSize); |
| 2368 | } |
| 2369 | } |
| 2370 | |
| 2371 | if (LastFieldBitfieldOrUnnamed) { |
| 2372 | if (LastFieldBitfieldOrUnnamed->isBitField()) { |
| 2373 | // Last field was a bitfield. Must update the info. |
| 2374 | uint64_t BitFieldSize |
| 2375 | = LastFieldBitfieldOrUnnamed->getBitWidthValue(CGM.getContext()); |
Fariborz Jahanian | 7778d61 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2376 | unsigned UnsSize = (BitFieldSize / ByteSizeInBits) + |
Eli Friedman | 8cbca20 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 2377 | ((BitFieldSize % ByteSizeInBits) != 0); |
Fariborz Jahanian | 7778d61 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2378 | CharUnits Size = CharUnits::fromQuantity(UnsSize); |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2379 | Size += LastBitfieldOrUnnamedOffset; |
| 2380 | UpdateRunSkipBlockVars(false, |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 2381 | getBlockCaptureLifetime(LastFieldBitfieldOrUnnamed->getType(), |
| 2382 | ByrefLayout), |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2383 | BytePos + LastBitfieldOrUnnamedOffset, |
Fariborz Jahanian | 7778d61 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2384 | Size); |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2385 | } else { |
| 2386 | assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed"); |
| 2387 | // Last field was unnamed. Must update skip info. |
Fariborz Jahanian | 7778d61 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2388 | CharUnits FieldSize |
| 2389 | = CGM.getContext().getTypeSizeInChars(LastFieldBitfieldOrUnnamed->getType()); |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2390 | UpdateRunSkipBlockVars(false, |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 2391 | getBlockCaptureLifetime(LastFieldBitfieldOrUnnamed->getType(), |
| 2392 | ByrefLayout), |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2393 | BytePos + LastBitfieldOrUnnamedOffset, |
| 2394 | FieldSize); |
| 2395 | } |
| 2396 | } |
| 2397 | |
| 2398 | if (MaxField) |
| 2399 | UpdateRunSkipBlockVars(false, |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 2400 | getBlockCaptureLifetime(MaxField->getType(), ByrefLayout), |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2401 | BytePos + MaxFieldOffset, |
| 2402 | MaxUnionSize); |
| 2403 | } |
| 2404 | |
| 2405 | void CGObjCCommonMac::BuildRCBlockVarRecordLayout(const RecordType *RT, |
Fariborz Jahanian | 7778d61 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2406 | CharUnits BytePos, |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 2407 | bool &HasUnion, |
| 2408 | bool ByrefLayout) { |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2409 | const RecordDecl *RD = RT->getDecl(); |
Aaron Ballman | 62e47c4 | 2014-03-10 13:43:55 +0000 | [diff] [blame] | 2410 | SmallVector<const FieldDecl*, 16> Fields(RD->fields()); |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2411 | llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0)); |
| 2412 | const llvm::StructLayout *RecLayout = |
| 2413 | CGM.getDataLayout().getStructLayout(cast<llvm::StructType>(Ty)); |
| 2414 | |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 2415 | BuildRCRecordLayout(RecLayout, RD, Fields, BytePos, HasUnion, ByrefLayout); |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2416 | } |
| 2417 | |
Fariborz Jahanian | 23290b0 | 2012-11-01 18:32:55 +0000 | [diff] [blame] | 2418 | /// InlineLayoutInstruction - This routine produce an inline instruction for the |
| 2419 | /// block variable layout if it can. If not, it returns 0. Rules are as follow: |
| 2420 | /// If ((uintptr_t) layout) < (1 << 12), the layout is inline. In the 64bit world, |
| 2421 | /// an inline layout of value 0x0000000000000xyz is interpreted as follows: |
| 2422 | /// x captured object pointers of BLOCK_LAYOUT_STRONG. Followed by |
| 2423 | /// y captured object of BLOCK_LAYOUT_BYREF. Followed by |
| 2424 | /// z captured object of BLOCK_LAYOUT_WEAK. If any of the above is missing, zero |
| 2425 | /// replaces it. For example, 0x00000x00 means x BLOCK_LAYOUT_STRONG and no |
| 2426 | /// BLOCK_LAYOUT_BYREF and no BLOCK_LAYOUT_WEAK objects are captured. |
| 2427 | uint64_t CGObjCCommonMac::InlineLayoutInstruction( |
| 2428 | SmallVectorImpl<unsigned char> &Layout) { |
| 2429 | uint64_t Result = 0; |
| 2430 | if (Layout.size() <= 3) { |
| 2431 | unsigned size = Layout.size(); |
| 2432 | unsigned strong_word_count = 0, byref_word_count=0, weak_word_count=0; |
| 2433 | unsigned char inst; |
| 2434 | enum BLOCK_LAYOUT_OPCODE opcode ; |
| 2435 | switch (size) { |
| 2436 | case 3: |
| 2437 | inst = Layout[0]; |
| 2438 | opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4); |
| 2439 | if (opcode == BLOCK_LAYOUT_STRONG) |
| 2440 | strong_word_count = (inst & 0xF)+1; |
| 2441 | else |
| 2442 | return 0; |
| 2443 | inst = Layout[1]; |
| 2444 | opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4); |
| 2445 | if (opcode == BLOCK_LAYOUT_BYREF) |
| 2446 | byref_word_count = (inst & 0xF)+1; |
| 2447 | else |
| 2448 | return 0; |
| 2449 | inst = Layout[2]; |
| 2450 | opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4); |
| 2451 | if (opcode == BLOCK_LAYOUT_WEAK) |
| 2452 | weak_word_count = (inst & 0xF)+1; |
| 2453 | else |
| 2454 | return 0; |
| 2455 | break; |
| 2456 | |
| 2457 | case 2: |
| 2458 | inst = Layout[0]; |
| 2459 | opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4); |
| 2460 | if (opcode == BLOCK_LAYOUT_STRONG) { |
| 2461 | strong_word_count = (inst & 0xF)+1; |
| 2462 | inst = Layout[1]; |
| 2463 | opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4); |
| 2464 | if (opcode == BLOCK_LAYOUT_BYREF) |
| 2465 | byref_word_count = (inst & 0xF)+1; |
| 2466 | else if (opcode == BLOCK_LAYOUT_WEAK) |
| 2467 | weak_word_count = (inst & 0xF)+1; |
| 2468 | else |
| 2469 | return 0; |
| 2470 | } |
| 2471 | else if (opcode == BLOCK_LAYOUT_BYREF) { |
| 2472 | byref_word_count = (inst & 0xF)+1; |
| 2473 | inst = Layout[1]; |
| 2474 | opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4); |
| 2475 | if (opcode == BLOCK_LAYOUT_WEAK) |
| 2476 | weak_word_count = (inst & 0xF)+1; |
| 2477 | else |
| 2478 | return 0; |
| 2479 | } |
| 2480 | else |
| 2481 | return 0; |
| 2482 | break; |
| 2483 | |
| 2484 | case 1: |
| 2485 | inst = Layout[0]; |
| 2486 | opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4); |
| 2487 | if (opcode == BLOCK_LAYOUT_STRONG) |
| 2488 | strong_word_count = (inst & 0xF)+1; |
| 2489 | else if (opcode == BLOCK_LAYOUT_BYREF) |
| 2490 | byref_word_count = (inst & 0xF)+1; |
| 2491 | else if (opcode == BLOCK_LAYOUT_WEAK) |
| 2492 | weak_word_count = (inst & 0xF)+1; |
| 2493 | else |
| 2494 | return 0; |
| 2495 | break; |
| 2496 | |
| 2497 | default: |
| 2498 | return 0; |
| 2499 | } |
| 2500 | |
| 2501 | // Cannot inline when any of the word counts is 15. Because this is one less |
| 2502 | // than the actual work count (so 15 means 16 actual word counts), |
| 2503 | // and we can only display 0 thru 15 word counts. |
| 2504 | if (strong_word_count == 16 || byref_word_count == 16 || weak_word_count == 16) |
| 2505 | return 0; |
| 2506 | |
| 2507 | unsigned count = |
| 2508 | (strong_word_count != 0) + (byref_word_count != 0) + (weak_word_count != 0); |
| 2509 | |
| 2510 | if (size == count) { |
| 2511 | if (strong_word_count) |
| 2512 | Result = strong_word_count; |
| 2513 | Result <<= 4; |
| 2514 | if (byref_word_count) |
| 2515 | Result += byref_word_count; |
| 2516 | Result <<= 4; |
| 2517 | if (weak_word_count) |
| 2518 | Result += weak_word_count; |
| 2519 | } |
| 2520 | } |
| 2521 | return Result; |
| 2522 | } |
| 2523 | |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 2524 | llvm::Constant *CGObjCCommonMac::getBitmapBlockLayout(bool ComputeByrefLayout) { |
| 2525 | llvm::Constant *nullPtr = llvm::Constant::getNullValue(CGM.Int8PtrTy); |
| 2526 | if (RunSkipBlockVars.empty()) |
| 2527 | return nullPtr; |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 2528 | unsigned WordSizeInBits = CGM.getTarget().getPointerWidth(0); |
| 2529 | unsigned ByteSizeInBits = CGM.getTarget().getCharWidth(); |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 2530 | unsigned WordSizeInBytes = WordSizeInBits/ByteSizeInBits; |
| 2531 | |
| 2532 | // Sort on byte position; captures might not be allocated in order, |
| 2533 | // and unions can do funny things. |
| 2534 | llvm::array_pod_sort(RunSkipBlockVars.begin(), RunSkipBlockVars.end()); |
| 2535 | SmallVector<unsigned char, 16> Layout; |
| 2536 | |
| 2537 | unsigned size = RunSkipBlockVars.size(); |
| 2538 | for (unsigned i = 0; i < size; i++) { |
| 2539 | enum BLOCK_LAYOUT_OPCODE opcode = RunSkipBlockVars[i].opcode; |
| 2540 | CharUnits start_byte_pos = RunSkipBlockVars[i].block_var_bytepos; |
| 2541 | CharUnits end_byte_pos = start_byte_pos; |
| 2542 | unsigned j = i+1; |
| 2543 | while (j < size) { |
| 2544 | if (opcode == RunSkipBlockVars[j].opcode) { |
| 2545 | end_byte_pos = RunSkipBlockVars[j++].block_var_bytepos; |
| 2546 | i++; |
| 2547 | } |
| 2548 | else |
| 2549 | break; |
| 2550 | } |
| 2551 | CharUnits size_in_bytes = |
| 2552 | end_byte_pos - start_byte_pos + RunSkipBlockVars[j-1].block_var_size; |
| 2553 | if (j < size) { |
| 2554 | CharUnits gap = |
| 2555 | RunSkipBlockVars[j].block_var_bytepos - |
| 2556 | RunSkipBlockVars[j-1].block_var_bytepos - RunSkipBlockVars[j-1].block_var_size; |
| 2557 | size_in_bytes += gap; |
| 2558 | } |
| 2559 | CharUnits residue_in_bytes = CharUnits::Zero(); |
| 2560 | if (opcode == BLOCK_LAYOUT_NON_OBJECT_BYTES) { |
| 2561 | residue_in_bytes = size_in_bytes % WordSizeInBytes; |
| 2562 | size_in_bytes -= residue_in_bytes; |
| 2563 | opcode = BLOCK_LAYOUT_NON_OBJECT_WORDS; |
| 2564 | } |
| 2565 | |
| 2566 | unsigned size_in_words = size_in_bytes.getQuantity() / WordSizeInBytes; |
| 2567 | while (size_in_words >= 16) { |
| 2568 | // Note that value in imm. is one less that the actual |
| 2569 | // value. So, 0xf means 16 words follow! |
| 2570 | unsigned char inst = (opcode << 4) | 0xf; |
| 2571 | Layout.push_back(inst); |
| 2572 | size_in_words -= 16; |
| 2573 | } |
| 2574 | if (size_in_words > 0) { |
| 2575 | // Note that value in imm. is one less that the actual |
| 2576 | // value. So, we subtract 1 away! |
| 2577 | unsigned char inst = (opcode << 4) | (size_in_words-1); |
| 2578 | Layout.push_back(inst); |
| 2579 | } |
| 2580 | if (residue_in_bytes > CharUnits::Zero()) { |
| 2581 | unsigned char inst = |
| 2582 | (BLOCK_LAYOUT_NON_OBJECT_BYTES << 4) | (residue_in_bytes.getQuantity()-1); |
| 2583 | Layout.push_back(inst); |
| 2584 | } |
| 2585 | } |
| 2586 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2587 | while (!Layout.empty()) { |
| 2588 | unsigned char inst = Layout.back(); |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 2589 | enum BLOCK_LAYOUT_OPCODE opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4); |
| 2590 | if (opcode == BLOCK_LAYOUT_NON_OBJECT_BYTES || opcode == BLOCK_LAYOUT_NON_OBJECT_WORDS) |
| 2591 | Layout.pop_back(); |
| 2592 | else |
| 2593 | break; |
| 2594 | } |
| 2595 | |
| 2596 | uint64_t Result = InlineLayoutInstruction(Layout); |
| 2597 | if (Result != 0) { |
| 2598 | // Block variable layout instruction has been inlined. |
| 2599 | if (CGM.getLangOpts().ObjCGCBitmapPrint) { |
| 2600 | if (ComputeByrefLayout) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2601 | printf("\n Inline BYREF variable layout: "); |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 2602 | else |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2603 | printf("\n Inline block variable layout: "); |
| 2604 | printf("0x0%" PRIx64 "", Result); |
| 2605 | if (auto numStrong = (Result & 0xF00) >> 8) |
| 2606 | printf(", BL_STRONG:%d", (int) numStrong); |
| 2607 | if (auto numByref = (Result & 0x0F0) >> 4) |
| 2608 | printf(", BL_BYREF:%d", (int) numByref); |
| 2609 | if (auto numWeak = (Result & 0x00F) >> 0) |
| 2610 | printf(", BL_WEAK:%d", (int) numWeak); |
| 2611 | printf(", BL_OPERATOR:0\n"); |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 2612 | } |
Vedant Kumar | 3ed0df0 | 2015-12-21 19:43:25 +0000 | [diff] [blame] | 2613 | return llvm::ConstantInt::get(CGM.IntPtrTy, Result); |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 2614 | } |
| 2615 | |
| 2616 | unsigned char inst = (BLOCK_LAYOUT_OPERATOR << 4) | 0; |
| 2617 | Layout.push_back(inst); |
| 2618 | std::string BitMap; |
| 2619 | for (unsigned i = 0, e = Layout.size(); i != e; i++) |
| 2620 | BitMap += Layout[i]; |
| 2621 | |
| 2622 | if (CGM.getLangOpts().ObjCGCBitmapPrint) { |
| 2623 | if (ComputeByrefLayout) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2624 | printf("\n Byref variable layout: "); |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 2625 | else |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2626 | printf("\n Block variable layout: "); |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 2627 | for (unsigned i = 0, e = BitMap.size(); i != e; i++) { |
| 2628 | unsigned char inst = BitMap[i]; |
| 2629 | enum BLOCK_LAYOUT_OPCODE opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4); |
| 2630 | unsigned delta = 1; |
| 2631 | switch (opcode) { |
| 2632 | case BLOCK_LAYOUT_OPERATOR: |
| 2633 | printf("BL_OPERATOR:"); |
| 2634 | delta = 0; |
| 2635 | break; |
| 2636 | case BLOCK_LAYOUT_NON_OBJECT_BYTES: |
| 2637 | printf("BL_NON_OBJECT_BYTES:"); |
| 2638 | break; |
| 2639 | case BLOCK_LAYOUT_NON_OBJECT_WORDS: |
| 2640 | printf("BL_NON_OBJECT_WORD:"); |
| 2641 | break; |
| 2642 | case BLOCK_LAYOUT_STRONG: |
| 2643 | printf("BL_STRONG:"); |
| 2644 | break; |
| 2645 | case BLOCK_LAYOUT_BYREF: |
| 2646 | printf("BL_BYREF:"); |
| 2647 | break; |
| 2648 | case BLOCK_LAYOUT_WEAK: |
| 2649 | printf("BL_WEAK:"); |
| 2650 | break; |
| 2651 | case BLOCK_LAYOUT_UNRETAINED: |
| 2652 | printf("BL_UNRETAINED:"); |
| 2653 | break; |
| 2654 | } |
| 2655 | // Actual value of word count is one more that what is in the imm. |
| 2656 | // field of the instruction |
| 2657 | printf("%d", (inst & 0xf) + delta); |
| 2658 | if (i < e-1) |
| 2659 | printf(", "); |
| 2660 | else |
| 2661 | printf("\n"); |
| 2662 | } |
| 2663 | } |
Rafael Espindola | 8b27bdb | 2014-11-06 13:30:38 +0000 | [diff] [blame] | 2664 | |
Saleem Abdulrasool | 82f6add | 2016-09-20 18:38:54 +0000 | [diff] [blame] | 2665 | auto *Entry = CreateCStringLiteral(BitMap, ObjCLabelType::ClassName, |
| 2666 | /*ForceNonFragileABI=*/true, |
| 2667 | /*NullTerminate=*/false); |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 2668 | return getConstantGEP(VMContext, Entry, 0, 0); |
| 2669 | } |
| 2670 | |
Fariborz Jahanian | 0c58ce9 | 2012-10-27 21:10:38 +0000 | [diff] [blame] | 2671 | llvm::Constant *CGObjCCommonMac::BuildRCBlockLayout(CodeGenModule &CGM, |
| 2672 | const CGBlockInfo &blockInfo) { |
Fariborz Jahanian | 0c58ce9 | 2012-10-27 21:10:38 +0000 | [diff] [blame] | 2673 | assert(CGM.getLangOpts().getGC() == LangOptions::NonGC); |
| 2674 | |
Fariborz Jahanian | 0c58ce9 | 2012-10-27 21:10:38 +0000 | [diff] [blame] | 2675 | RunSkipBlockVars.clear(); |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2676 | bool hasUnion = false; |
| 2677 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 2678 | unsigned WordSizeInBits = CGM.getTarget().getPointerWidth(0); |
| 2679 | unsigned ByteSizeInBits = CGM.getTarget().getCharWidth(); |
Fariborz Jahanian | 0c58ce9 | 2012-10-27 21:10:38 +0000 | [diff] [blame] | 2680 | unsigned WordSizeInBytes = WordSizeInBits/ByteSizeInBits; |
| 2681 | |
| 2682 | const BlockDecl *blockDecl = blockInfo.getBlockDecl(); |
| 2683 | |
| 2684 | // Calculate the basic layout of the block structure. |
| 2685 | const llvm::StructLayout *layout = |
| 2686 | CGM.getDataLayout().getStructLayout(blockInfo.StructureType); |
| 2687 | |
| 2688 | // Ignore the optional 'this' capture: C++ objects are not assumed |
| 2689 | // to be GC'ed. |
Fariborz Jahanian | 4cf177e | 2012-12-04 17:20:57 +0000 | [diff] [blame] | 2690 | if (blockInfo.BlockHeaderForcedGapSize != CharUnits::Zero()) |
| 2691 | UpdateRunSkipBlockVars(false, Qualifiers::OCL_None, |
| 2692 | blockInfo.BlockHeaderForcedGapOffset, |
| 2693 | blockInfo.BlockHeaderForcedGapSize); |
Fariborz Jahanian | 0c58ce9 | 2012-10-27 21:10:38 +0000 | [diff] [blame] | 2694 | // Walk the captured variables. |
Aaron Ballman | 9371dd2 | 2014-03-14 18:34:04 +0000 | [diff] [blame] | 2695 | for (const auto &CI : blockDecl->captures()) { |
| 2696 | const VarDecl *variable = CI.getVariable(); |
Fariborz Jahanian | 0c58ce9 | 2012-10-27 21:10:38 +0000 | [diff] [blame] | 2697 | QualType type = variable->getType(); |
| 2698 | |
| 2699 | const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable); |
| 2700 | |
| 2701 | // Ignore constant captures. |
| 2702 | if (capture.isConstant()) continue; |
| 2703 | |
Fariborz Jahanian | 7778d61 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2704 | CharUnits fieldOffset = |
| 2705 | CharUnits::fromQuantity(layout->getElementOffset(capture.getIndex())); |
Fariborz Jahanian | 0c58ce9 | 2012-10-27 21:10:38 +0000 | [diff] [blame] | 2706 | |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2707 | assert(!type->isArrayType() && "array variable should not be caught"); |
Aaron Ballman | 9371dd2 | 2014-03-14 18:34:04 +0000 | [diff] [blame] | 2708 | if (!CI.isByRef()) |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 2709 | if (const RecordType *record = type->getAs<RecordType>()) { |
| 2710 | BuildRCBlockVarRecordLayout(record, fieldOffset, hasUnion); |
| 2711 | continue; |
| 2712 | } |
Fariborz Jahanian | 7778d61 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2713 | CharUnits fieldSize; |
Aaron Ballman | 9371dd2 | 2014-03-14 18:34:04 +0000 | [diff] [blame] | 2714 | if (CI.isByRef()) |
Fariborz Jahanian | 7778d61 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2715 | fieldSize = CharUnits::fromQuantity(WordSizeInBytes); |
| 2716 | else |
| 2717 | fieldSize = CGM.getContext().getTypeSizeInChars(type); |
Aaron Ballman | 9371dd2 | 2014-03-14 18:34:04 +0000 | [diff] [blame] | 2718 | UpdateRunSkipBlockVars(CI.isByRef(), getBlockCaptureLifetime(type, false), |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2719 | fieldOffset, fieldSize); |
Fariborz Jahanian | 0c58ce9 | 2012-10-27 21:10:38 +0000 | [diff] [blame] | 2720 | } |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 2721 | return getBitmapBlockLayout(false); |
| 2722 | } |
Fariborz Jahanian | 0c58ce9 | 2012-10-27 21:10:38 +0000 | [diff] [blame] | 2723 | |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 2724 | llvm::Constant *CGObjCCommonMac::BuildByrefLayout(CodeGen::CodeGenModule &CGM, |
| 2725 | QualType T) { |
| 2726 | assert(CGM.getLangOpts().getGC() == LangOptions::NonGC); |
| 2727 | assert(!T->isArrayType() && "__block array variable should not be caught"); |
| 2728 | CharUnits fieldOffset; |
| 2729 | RunSkipBlockVars.clear(); |
| 2730 | bool hasUnion = false; |
| 2731 | if (const RecordType *record = T->getAs<RecordType>()) { |
| 2732 | BuildRCBlockVarRecordLayout(record, fieldOffset, hasUnion, true /*ByrefLayout */); |
| 2733 | llvm::Constant *Result = getBitmapBlockLayout(true); |
Vedant Kumar | 2f5bb115 | 2015-12-21 20:21:15 +0000 | [diff] [blame] | 2734 | if (isa<llvm::ConstantInt>(Result)) |
| 2735 | Result = llvm::ConstantExpr::getIntToPtr(Result, CGM.Int8PtrTy); |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 2736 | return Result; |
Fariborz Jahanian | 0c58ce9 | 2012-10-27 21:10:38 +0000 | [diff] [blame] | 2737 | } |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 2738 | llvm::Constant *nullPtr = llvm::Constant::getNullValue(CGM.Int8PtrTy); |
| 2739 | return nullPtr; |
Fariborz Jahanian | 0c58ce9 | 2012-10-27 21:10:38 +0000 | [diff] [blame] | 2740 | } |
| 2741 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 2742 | llvm::Value *CGObjCMac::GenerateProtocolRef(CodeGenFunction &CGF, |
Daniel Dunbar | 89da6ad | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 2743 | const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 7050c55 | 2008-09-04 04:33:15 +0000 | [diff] [blame] | 2744 | // FIXME: I don't understand why gcc generates this, or where it is |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 2745 | // resolved. Investigate. Its also wasteful to look this up over and over. |
Daniel Dunbar | 7050c55 | 2008-09-04 04:33:15 +0000 | [diff] [blame] | 2746 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); |
| 2747 | |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2748 | return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD), |
Douglas Gregor | 020de32 | 2012-01-17 18:36:30 +0000 | [diff] [blame] | 2749 | ObjCTypes.getExternalProtocolPtrTy()); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2750 | } |
| 2751 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 2752 | void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) { |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 2753 | // FIXME: We shouldn't need this, the protocol decl should contain enough |
| 2754 | // information to tell us whether this was a declaration or a definition. |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2755 | DefinedProtocols.insert(PD->getIdentifier()); |
| 2756 | |
| 2757 | // If we have generated a forward reference to this protocol, emit |
| 2758 | // it now. Otherwise do nothing, the protocol objects are lazily |
| 2759 | // emitted. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2760 | if (Protocols.count(PD->getIdentifier())) |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2761 | GetOrEmitProtocol(PD); |
| 2762 | } |
| 2763 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 2764 | llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2765 | if (DefinedProtocols.count(PD->getIdentifier())) |
| 2766 | return GetOrEmitProtocol(PD); |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 2767 | |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2768 | return GetOrEmitProtocolRef(PD); |
| 2769 | } |
| 2770 | |
Douglas Gregor | 24ae22c | 2016-04-01 23:23:52 +0000 | [diff] [blame] | 2771 | llvm::Value *CGObjCCommonMac::EmitClassRefViaRuntime( |
| 2772 | CodeGenFunction &CGF, |
| 2773 | const ObjCInterfaceDecl *ID, |
| 2774 | ObjCCommonTypesHelper &ObjCTypes) { |
| 2775 | llvm::Constant *lookUpClassFn = ObjCTypes.getLookUpClassFn(); |
| 2776 | |
| 2777 | llvm::Value *className = |
| 2778 | CGF.CGM.GetAddrOfConstantCString(ID->getObjCRuntimeNameAsString()) |
| 2779 | .getPointer(); |
| 2780 | ASTContext &ctx = CGF.CGM.getContext(); |
| 2781 | className = |
| 2782 | CGF.Builder.CreateBitCast(className, |
| 2783 | CGF.ConvertType( |
| 2784 | ctx.getPointerType(ctx.CharTy.withConst()))); |
| 2785 | llvm::CallInst *call = CGF.Builder.CreateCall(lookUpClassFn, className); |
| 2786 | call->setDoesNotThrow(); |
| 2787 | return call; |
| 2788 | } |
| 2789 | |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2790 | /* |
Rafael Espindola | f9e1e5e | 2014-02-20 14:09:04 +0000 | [diff] [blame] | 2791 | // Objective-C 1.0 extensions |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2792 | struct _objc_protocol { |
| 2793 | struct _objc_protocol_extension *isa; |
| 2794 | char *protocol_name; |
| 2795 | struct _objc_protocol_list *protocol_list; |
| 2796 | struct _objc__method_prototype_list *instance_methods; |
| 2797 | struct _objc__method_prototype_list *class_methods |
| 2798 | }; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2799 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2800 | See EmitProtocolExtension(). |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2801 | */ |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2802 | llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) { |
John McCall | f9582a7 | 2012-03-30 21:29:05 +0000 | [diff] [blame] | 2803 | llvm::GlobalVariable *Entry = Protocols[PD->getIdentifier()]; |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2804 | |
| 2805 | // Early exit if a defining object has already been generated. |
| 2806 | if (Entry && Entry->hasInitializer()) |
| 2807 | return Entry; |
| 2808 | |
Douglas Gregor | a715bff | 2012-01-01 19:51:50 +0000 | [diff] [blame] | 2809 | // Use the protocol definition, if there is one. |
| 2810 | if (const ObjCProtocolDecl *Def = PD->getDefinition()) |
| 2811 | PD = Def; |
| 2812 | |
Daniel Dunbar | c61d0e9 | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2813 | // FIXME: I don't understand why gcc generates this, or where it is |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 2814 | // resolved. Investigate. Its also wasteful to look this up over and over. |
Daniel Dunbar | c61d0e9 | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2815 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); |
| 2816 | |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2817 | // Construct method lists. |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 2818 | auto methodLists = ProtocolMethodLists::get(PD); |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2819 | |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 2820 | ConstantInitBuilder builder(CGM); |
| 2821 | auto values = builder.beginStruct(ObjCTypes.ProtocolTy); |
| 2822 | values.add(EmitProtocolExtension(PD, methodLists)); |
| 2823 | values.add(GetClassName(PD->getObjCRuntimeNameAsString())); |
| 2824 | values.add(EmitProtocolList("OBJC_PROTOCOL_REFS_" + PD->getName(), |
| 2825 | PD->protocol_begin(), PD->protocol_end())); |
| 2826 | values.add(methodLists.emitMethodList(this, PD, |
| 2827 | ProtocolMethodLists::RequiredInstanceMethods)); |
| 2828 | values.add(methodLists.emitMethodList(this, PD, |
| 2829 | ProtocolMethodLists::RequiredClassMethods)); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2830 | |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2831 | if (Entry) { |
Rafael Espindola | 5d117f3 | 2014-03-06 01:10:46 +0000 | [diff] [blame] | 2832 | // Already created, update the initializer. |
Rafael Espindola | b326295 | 2014-05-09 00:43:37 +0000 | [diff] [blame] | 2833 | assert(Entry->hasPrivateLinkage()); |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 2834 | values.finishAndSetAsInitializer(Entry); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2835 | } else { |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 2836 | Entry = values.finishAndCreateGlobal("OBJC_PROTOCOL_" + PD->getName(), |
| 2837 | CGM.getPointerAlign(), |
| 2838 | /*constant*/ false, |
| 2839 | llvm::GlobalValue::PrivateLinkage); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2840 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); |
John McCall | f9582a7 | 2012-03-30 21:29:05 +0000 | [diff] [blame] | 2841 | |
| 2842 | Protocols[PD->getIdentifier()] = Entry; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2843 | } |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 2844 | CGM.addCompilerUsedGlobal(Entry); |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2845 | |
| 2846 | return Entry; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2847 | } |
| 2848 | |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2849 | llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2850 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 2851 | |
| 2852 | if (!Entry) { |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2853 | // We use the initializer as a marker of whether this is a forward |
| 2854 | // reference or not. At module finalization we add the empty |
| 2855 | // contents for protocols which were referenced but never defined. |
Rafael Espindola | 8b27bdb | 2014-11-06 13:30:38 +0000 | [diff] [blame] | 2856 | Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, |
| 2857 | false, llvm::GlobalValue::PrivateLinkage, |
| 2858 | nullptr, "OBJC_PROTOCOL_" + PD->getName()); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2859 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2860 | // FIXME: Is this necessary? Why only for protocol? |
| 2861 | Entry->setAlignment(4); |
| 2862 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2863 | |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2864 | return Entry; |
| 2865 | } |
| 2866 | |
| 2867 | /* |
| 2868 | struct _objc_protocol_extension { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2869 | uint32_t size; |
| 2870 | struct objc_method_description_list *optional_instance_methods; |
| 2871 | struct objc_method_description_list *optional_class_methods; |
| 2872 | struct objc_property_list *instance_properties; |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 2873 | const char ** extendedMethodTypes; |
Manman Ren | ce7bff5 | 2016-01-29 23:46:55 +0000 | [diff] [blame] | 2874 | struct objc_property_list *class_properties; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2875 | }; |
| 2876 | */ |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2877 | llvm::Constant * |
| 2878 | CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD, |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 2879 | const ProtocolMethodLists &methodLists) { |
| 2880 | auto optInstanceMethods = |
| 2881 | methodLists.emitMethodList(this, PD, |
| 2882 | ProtocolMethodLists::OptionalInstanceMethods); |
| 2883 | auto optClassMethods = |
| 2884 | methodLists.emitMethodList(this, PD, |
| 2885 | ProtocolMethodLists::OptionalClassMethods); |
| 2886 | |
| 2887 | auto extendedMethodTypes = |
| 2888 | EmitProtocolMethodTypes("OBJC_PROTOCOL_METHOD_TYPES_" + PD->getName(), |
| 2889 | methodLists.emitExtendedTypesArray(this), |
| 2890 | ObjCTypes); |
| 2891 | |
| 2892 | auto instanceProperties = |
| 2893 | EmitPropertyList("OBJC_$_PROP_PROTO_LIST_" + PD->getName(), nullptr, PD, |
| 2894 | ObjCTypes, false); |
| 2895 | auto classProperties = |
| 2896 | EmitPropertyList("OBJC_$_CLASS_PROP_PROTO_LIST_" + PD->getName(), nullptr, |
| 2897 | PD, ObjCTypes, true); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2898 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2899 | // Return null if no extension bits are used. |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 2900 | if (optInstanceMethods->isNullValue() && |
| 2901 | optClassMethods->isNullValue() && |
| 2902 | extendedMethodTypes->isNullValue() && |
| 2903 | instanceProperties->isNullValue() && |
| 2904 | classProperties->isNullValue()) { |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2905 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 2906 | } |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2907 | |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 2908 | uint64_t size = |
| 2909 | CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy); |
| 2910 | |
| 2911 | ConstantInitBuilder builder(CGM); |
| 2912 | auto values = builder.beginStruct(ObjCTypes.ProtocolExtensionTy); |
| 2913 | values.addInt(ObjCTypes.IntTy, size); |
| 2914 | values.add(optInstanceMethods); |
| 2915 | values.add(optClassMethods); |
| 2916 | values.add(instanceProperties); |
| 2917 | values.add(extendedMethodTypes); |
| 2918 | values.add(classProperties); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2919 | |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2920 | // No special section, but goes in llvm.used |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 2921 | return CreateMetadataVar("\01l_OBJC_PROTOCOLEXT_" + PD->getName(), values, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2922 | StringRef(), CGM.getPointerAlign(), true); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2923 | } |
| 2924 | |
| 2925 | /* |
| 2926 | struct objc_protocol_list { |
Bill Wendling | a515b58 | 2012-02-09 22:16:49 +0000 | [diff] [blame] | 2927 | struct objc_protocol_list *next; |
| 2928 | long count; |
| 2929 | Protocol *list[]; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2930 | }; |
| 2931 | */ |
Daniel Dunbar | dec75f8 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 2932 | llvm::Constant * |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 2933 | CGObjCMac::EmitProtocolList(Twine name, |
Daniel Dunbar | dec75f8 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 2934 | ObjCProtocolDecl::protocol_iterator begin, |
| 2935 | ObjCProtocolDecl::protocol_iterator end) { |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2936 | // Just return null for empty protocol lists |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 2937 | if (begin == end) |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2938 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2939 | |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 2940 | ConstantInitBuilder builder(CGM); |
| 2941 | auto values = builder.beginStruct(); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2942 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2943 | // This field is only used by the runtime. |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 2944 | values.addNullPointer(ObjCTypes.ProtocolListPtrTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2945 | |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 2946 | // Reserve a slot for the count. |
| 2947 | auto countSlot = values.addPlaceholder(); |
| 2948 | |
| 2949 | auto refsArray = values.beginArray(ObjCTypes.ProtocolPtrTy); |
| 2950 | for (; begin != end; ++begin) { |
| 2951 | refsArray.add(GetProtocolRef(*begin)); |
| 2952 | } |
| 2953 | auto count = refsArray.size(); |
| 2954 | |
| 2955 | // This list is null terminated. |
| 2956 | refsArray.addNullPointer(ObjCTypes.ProtocolPtrTy); |
| 2957 | |
| 2958 | refsArray.finishAndAddTo(values); |
| 2959 | values.fillPlaceholderWithInt(countSlot, ObjCTypes.LongTy, count); |
| 2960 | |
| 2961 | StringRef section; |
Saleem Abdulrasool | 3c628af | 2016-10-25 21:43:28 +0000 | [diff] [blame] | 2962 | if (CGM.getTriple().isOSBinFormatMachO()) |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 2963 | section = "__OBJC,__cat_cls_meth,regular,no_dead_strip"; |
Saleem Abdulrasool | 3c628af | 2016-10-25 21:43:28 +0000 | [diff] [blame] | 2964 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2965 | llvm::GlobalVariable *GV = |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 2966 | CreateMetadataVar(name, values, section, CGM.getPointerAlign(), false); |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2967 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2968 | } |
| 2969 | |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 2970 | static void |
Bill Wendling | cb5b5ff | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 2971 | PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*,16> &PropertySet, |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 2972 | SmallVectorImpl<const ObjCPropertyDecl *> &Properties, |
Aaron Ballman | dc4bea4 | 2014-03-13 18:47:37 +0000 | [diff] [blame] | 2973 | const ObjCProtocolDecl *Proto, |
Manman Ren | ad0e791 | 2016-01-29 19:22:54 +0000 | [diff] [blame] | 2974 | bool IsClassProperty) { |
Aaron Ballman | 0f6e64d | 2014-03-13 22:58:06 +0000 | [diff] [blame] | 2975 | for (const auto *P : Proto->protocols()) |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 2976 | PushProtocolProperties(PropertySet, Properties, P, IsClassProperty); |
Manman Ren | ad0e791 | 2016-01-29 19:22:54 +0000 | [diff] [blame] | 2977 | |
| 2978 | for (const auto *PD : Proto->properties()) { |
| 2979 | if (IsClassProperty != PD->isClassProperty()) |
| 2980 | continue; |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 2981 | if (!PropertySet.insert(PD->getIdentifier()).second) |
Fariborz Jahanian | 751c1e7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 2982 | continue; |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 2983 | Properties.push_back(PD); |
Fariborz Jahanian | 751c1e7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 2984 | } |
| 2985 | } |
| 2986 | |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2987 | /* |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2988 | struct _objc_property { |
Bill Wendling | a515b58 | 2012-02-09 22:16:49 +0000 | [diff] [blame] | 2989 | const char * const name; |
| 2990 | const char * const attributes; |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2991 | }; |
| 2992 | |
| 2993 | struct _objc_property_list { |
Bill Wendling | a515b58 | 2012-02-09 22:16:49 +0000 | [diff] [blame] | 2994 | uint32_t entsize; // sizeof (struct _objc_property) |
| 2995 | uint32_t prop_count; |
| 2996 | struct _objc_property[prop_count]; |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2997 | }; |
| 2998 | */ |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2999 | llvm::Constant *CGObjCCommonMac::EmitPropertyList(Twine Name, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3000 | const Decl *Container, |
| 3001 | const ObjCContainerDecl *OCD, |
Manman Ren | ad0e791 | 2016-01-29 19:22:54 +0000 | [diff] [blame] | 3002 | const ObjCCommonTypesHelper &ObjCTypes, |
| 3003 | bool IsClassProperty) { |
Manman Ren | 01b705e | 2016-04-19 19:05:03 +0000 | [diff] [blame] | 3004 | if (IsClassProperty) { |
| 3005 | // Make this entry NULL for OS X with deployment target < 10.11, for iOS |
| 3006 | // with deployment target < 9.0. |
| 3007 | const llvm::Triple &Triple = CGM.getTarget().getTriple(); |
| 3008 | if ((Triple.isMacOSX() && Triple.isMacOSXVersionLT(10, 11)) || |
| 3009 | (Triple.isiOS() && Triple.isOSVersionLT(9))) |
| 3010 | return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 3011 | } |
| 3012 | |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3013 | SmallVector<const ObjCPropertyDecl *, 16> Properties; |
Fariborz Jahanian | 751c1e7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 3014 | llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet; |
Nico Weber | 08c9333 | 2015-12-03 17:44:51 +0000 | [diff] [blame] | 3015 | |
Nico Weber | 08c9333 | 2015-12-03 17:44:51 +0000 | [diff] [blame] | 3016 | if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) |
| 3017 | for (const ObjCCategoryDecl *ClassExt : OID->known_extensions()) |
Manman Ren | ad0e791 | 2016-01-29 19:22:54 +0000 | [diff] [blame] | 3018 | for (auto *PD : ClassExt->properties()) { |
| 3019 | if (IsClassProperty != PD->isClassProperty()) |
| 3020 | continue; |
Nico Weber | 08c9333 | 2015-12-03 17:44:51 +0000 | [diff] [blame] | 3021 | PropertySet.insert(PD->getIdentifier()); |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3022 | Properties.push_back(PD); |
Nico Weber | 08c9333 | 2015-12-03 17:44:51 +0000 | [diff] [blame] | 3023 | } |
Manman Ren | ad0e791 | 2016-01-29 19:22:54 +0000 | [diff] [blame] | 3024 | |
| 3025 | for (const auto *PD : OCD->properties()) { |
| 3026 | if (IsClassProperty != PD->isClassProperty()) |
| 3027 | continue; |
Nico Weber | 08c9333 | 2015-12-03 17:44:51 +0000 | [diff] [blame] | 3028 | // Don't emit duplicate metadata for properties that were already in a |
| 3029 | // class extension. |
| 3030 | if (!PropertySet.insert(PD->getIdentifier()).second) |
| 3031 | continue; |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3032 | Properties.push_back(PD); |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3033 | } |
Nico Weber | 08c9333 | 2015-12-03 17:44:51 +0000 | [diff] [blame] | 3034 | |
Fariborz Jahanian | 7966aff | 2010-06-22 16:33:55 +0000 | [diff] [blame] | 3035 | if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) { |
Aaron Ballman | a9f49e3 | 2014-03-13 20:55:22 +0000 | [diff] [blame] | 3036 | for (const auto *P : OID->all_referenced_protocols()) |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3037 | PushProtocolProperties(PropertySet, Properties, P, IsClassProperty); |
Fariborz Jahanian | 7966aff | 2010-06-22 16:33:55 +0000 | [diff] [blame] | 3038 | } |
| 3039 | else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) { |
Aaron Ballman | 19a4176 | 2014-03-14 12:55:57 +0000 | [diff] [blame] | 3040 | for (const auto *P : CD->protocols()) |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3041 | PushProtocolProperties(PropertySet, Properties, P, IsClassProperty); |
Fariborz Jahanian | 7966aff | 2010-06-22 16:33:55 +0000 | [diff] [blame] | 3042 | } |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3043 | |
| 3044 | // Return null for empty list. |
| 3045 | if (Properties.empty()) |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3046 | return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3047 | |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3048 | unsigned propertySize = |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3049 | CGM.getDataLayout().getTypeAllocSize(ObjCTypes.PropertyTy); |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3050 | |
| 3051 | ConstantInitBuilder builder(CGM); |
| 3052 | auto values = builder.beginStruct(); |
| 3053 | values.addInt(ObjCTypes.IntTy, propertySize); |
| 3054 | values.addInt(ObjCTypes.IntTy, Properties.size()); |
| 3055 | auto propertiesArray = values.beginArray(ObjCTypes.PropertyTy); |
| 3056 | for (auto PD : Properties) { |
| 3057 | auto property = propertiesArray.beginStruct(ObjCTypes.PropertyTy); |
| 3058 | property.add(GetPropertyName(PD->getIdentifier())); |
| 3059 | property.add(GetPropertyTypeString(PD, Container)); |
| 3060 | property.finishAndAddTo(propertiesArray); |
| 3061 | } |
| 3062 | propertiesArray.finishAndAddTo(values); |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3063 | |
Saleem Abdulrasool | 3c628af | 2016-10-25 21:43:28 +0000 | [diff] [blame] | 3064 | StringRef Section; |
| 3065 | if (CGM.getTriple().isOSBinFormatMachO()) |
| 3066 | Section = (ObjCABI == 2) ? "__DATA, __objc_const" |
| 3067 | : "__OBJC,__property,regular,no_dead_strip"; |
| 3068 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3069 | llvm::GlobalVariable *GV = |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3070 | CreateMetadataVar(Name, values, Section, CGM.getPointerAlign(), true); |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 3071 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy); |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3072 | } |
| 3073 | |
Bill Wendling | cb5b5ff | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 3074 | llvm::Constant * |
| 3075 | CGObjCCommonMac::EmitProtocolMethodTypes(Twine Name, |
| 3076 | ArrayRef<llvm::Constant*> MethodTypes, |
| 3077 | const ObjCCommonTypesHelper &ObjCTypes) { |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 3078 | // Return null for empty list. |
| 3079 | if (MethodTypes.empty()) |
| 3080 | return llvm::Constant::getNullValue(ObjCTypes.Int8PtrPtrTy); |
| 3081 | |
| 3082 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
| 3083 | MethodTypes.size()); |
| 3084 | llvm::Constant *Init = llvm::ConstantArray::get(AT, MethodTypes); |
| 3085 | |
Saleem Abdulrasool | 3c628af | 2016-10-25 21:43:28 +0000 | [diff] [blame] | 3086 | StringRef Section; |
| 3087 | if (CGM.getTriple().isOSBinFormatMachO() && ObjCABI == 2) |
| 3088 | Section = "__DATA, __objc_const"; |
| 3089 | |
| 3090 | llvm::GlobalVariable *GV = |
| 3091 | CreateMetadataVar(Name, Init, Section, CGM.getPointerAlign(), true); |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 3092 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.Int8PtrPtrTy); |
| 3093 | } |
| 3094 | |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3095 | /* |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3096 | struct _objc_category { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3097 | char *category_name; |
| 3098 | char *class_name; |
| 3099 | struct _objc_method_list *instance_methods; |
| 3100 | struct _objc_method_list *class_methods; |
| 3101 | struct _objc_protocol_list *protocols; |
| 3102 | uint32_t size; // <rdar://4585769> |
| 3103 | struct _objc_property_list *instance_properties; |
Manman Ren | 96df0b3 | 2016-01-29 23:45:01 +0000 | [diff] [blame] | 3104 | struct _objc_property_list *class_properties; |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3105 | }; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3106 | */ |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 3107 | void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) { |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3108 | unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.CategoryTy); |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3109 | |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 3110 | // FIXME: This is poor design, the OCD should have a pointer to the category |
| 3111 | // decl. Additionally, note that Category can be null for the @implementation |
| 3112 | // w/o an @interface case. Sema should just create one for us as it does for |
| 3113 | // @implementation so everyone else can live life under a clear blue sky. |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3114 | const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3115 | const ObjCCategoryDecl *Category = |
Daniel Dunbar | 28e76ca | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 3116 | Interface->FindCategoryDeclaration(OCD->getIdentifier()); |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 3117 | |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 3118 | SmallString<256> ExtName; |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 3119 | llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_' |
| 3120 | << OCD->getName(); |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3121 | |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3122 | ConstantInitBuilder Builder(CGM); |
| 3123 | auto Values = Builder.beginStruct(ObjCTypes.CategoryTy); |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 3124 | |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3125 | enum { |
| 3126 | InstanceMethods, |
| 3127 | ClassMethods, |
| 3128 | NumMethodLists |
| 3129 | }; |
| 3130 | SmallVector<const ObjCMethodDecl *, 16> Methods[NumMethodLists]; |
| 3131 | for (const auto *MD : OCD->methods()) { |
| 3132 | Methods[unsigned(MD->isClassMethod())].push_back(MD); |
| 3133 | } |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 3134 | |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3135 | Values.add(GetClassName(OCD->getName())); |
| 3136 | Values.add(GetClassName(Interface->getObjCRuntimeNameAsString())); |
Fariborz Jahanian | e55f866 | 2009-04-29 20:40:05 +0000 | [diff] [blame] | 3137 | LazySymbols.insert(Interface->getIdentifier()); |
Saleem Abdulrasool | d48b0a3 | 2016-10-24 20:47:58 +0000 | [diff] [blame] | 3138 | |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3139 | Values.add(emitMethodList(ExtName, MethodListType::CategoryInstanceMethods, |
| 3140 | Methods[InstanceMethods])); |
| 3141 | Values.add(emitMethodList(ExtName, MethodListType::CategoryClassMethods, |
| 3142 | Methods[ClassMethods])); |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 3143 | if (Category) { |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3144 | Values.add( |
Rafael Espindola | 8b27bdb | 2014-11-06 13:30:38 +0000 | [diff] [blame] | 3145 | EmitProtocolList("OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(), |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3146 | Category->protocol_begin(), Category->protocol_end())); |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 3147 | } else { |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3148 | Values.addNullPointer(ObjCTypes.ProtocolListPtrTy); |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 3149 | } |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3150 | Values.addInt(ObjCTypes.IntTy, Size); |
Daniel Dunbar | 28e76ca | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 3151 | |
| 3152 | // If there is no category @interface then there can be no properties. |
| 3153 | if (Category) { |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3154 | Values.add(EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(), |
| 3155 | OCD, Category, ObjCTypes, false)); |
| 3156 | Values.add(EmitPropertyList("\01l_OBJC_$_CLASS_PROP_LIST_" + ExtName.str(), |
| 3157 | OCD, Category, ObjCTypes, true)); |
Daniel Dunbar | 28e76ca | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 3158 | } else { |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3159 | Values.addNullPointer(ObjCTypes.PropertyListPtrTy); |
| 3160 | Values.addNullPointer(ObjCTypes.PropertyListPtrTy); |
Daniel Dunbar | 28e76ca | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 3161 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3162 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3163 | llvm::GlobalVariable *GV = |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3164 | CreateMetadataVar("OBJC_CATEGORY_" + ExtName.str(), Values, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3165 | "__OBJC,__category,regular,no_dead_strip", |
| 3166 | CGM.getPointerAlign(), true); |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3167 | DefinedCategories.push_back(GV); |
Justin Lebar | 5e83dfe | 2016-10-21 21:45:01 +0000 | [diff] [blame] | 3168 | DefinedCategoryNames.insert(llvm::CachedHashString(ExtName)); |
Fariborz Jahanian | c057794 | 2011-04-22 22:02:28 +0000 | [diff] [blame] | 3169 | // method definition entries must be clear for next implementation. |
| 3170 | MethodDefinitions.clear(); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 3171 | } |
| 3172 | |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 3173 | enum FragileClassFlags { |
John McCall | 460ce58 | 2015-10-22 18:38:17 +0000 | [diff] [blame] | 3174 | /// Apparently: is not a meta-class. |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 3175 | FragileABI_Class_Factory = 0x00001, |
John McCall | 460ce58 | 2015-10-22 18:38:17 +0000 | [diff] [blame] | 3176 | |
| 3177 | /// Is a meta-class. |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 3178 | FragileABI_Class_Meta = 0x00002, |
John McCall | 460ce58 | 2015-10-22 18:38:17 +0000 | [diff] [blame] | 3179 | |
| 3180 | /// Has a non-trivial constructor or destructor. |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 3181 | FragileABI_Class_HasCXXStructors = 0x02000, |
John McCall | 460ce58 | 2015-10-22 18:38:17 +0000 | [diff] [blame] | 3182 | |
| 3183 | /// Has hidden visibility. |
John McCall | 09ec1ec | 2015-10-21 22:06:03 +0000 | [diff] [blame] | 3184 | FragileABI_Class_Hidden = 0x20000, |
John McCall | 460ce58 | 2015-10-22 18:38:17 +0000 | [diff] [blame] | 3185 | |
| 3186 | /// Class implementation was compiled under ARC. |
| 3187 | FragileABI_Class_CompiledByARC = 0x04000000, |
| 3188 | |
| 3189 | /// Class implementation was compiled under MRC and has MRC weak ivars. |
| 3190 | /// Exclusive with CompiledByARC. |
| 3191 | FragileABI_Class_HasMRCWeakIvars = 0x08000000, |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 3192 | }; |
| 3193 | |
| 3194 | enum NonFragileClassFlags { |
| 3195 | /// Is a meta-class. |
| 3196 | NonFragileABI_Class_Meta = 0x00001, |
| 3197 | |
| 3198 | /// Is a root class. |
| 3199 | NonFragileABI_Class_Root = 0x00002, |
| 3200 | |
John McCall | 460ce58 | 2015-10-22 18:38:17 +0000 | [diff] [blame] | 3201 | /// Has a non-trivial constructor or destructor. |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 3202 | NonFragileABI_Class_HasCXXStructors = 0x00004, |
| 3203 | |
| 3204 | /// Has hidden visibility. |
| 3205 | NonFragileABI_Class_Hidden = 0x00010, |
| 3206 | |
| 3207 | /// Has the exception attribute. |
| 3208 | NonFragileABI_Class_Exception = 0x00020, |
| 3209 | |
| 3210 | /// (Obsolete) ARC-specific: this class has a .release_ivars method |
| 3211 | NonFragileABI_Class_HasIvarReleaser = 0x00040, |
| 3212 | |
| 3213 | /// Class implementation was compiled under ARC. |
John McCall | 0d54a17 | 2012-10-17 04:53:31 +0000 | [diff] [blame] | 3214 | NonFragileABI_Class_CompiledByARC = 0x00080, |
| 3215 | |
| 3216 | /// Class has non-trivial destructors, but zero-initialization is okay. |
John McCall | 460ce58 | 2015-10-22 18:38:17 +0000 | [diff] [blame] | 3217 | NonFragileABI_Class_HasCXXDestructorOnly = 0x00100, |
| 3218 | |
| 3219 | /// Class implementation was compiled under MRC and has MRC weak ivars. |
| 3220 | /// Exclusive with CompiledByARC. |
| 3221 | NonFragileABI_Class_HasMRCWeakIvars = 0x00200, |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3222 | }; |
| 3223 | |
John McCall | 460ce58 | 2015-10-22 18:38:17 +0000 | [diff] [blame] | 3224 | static bool hasWeakMember(QualType type) { |
| 3225 | if (type.getObjCLifetime() == Qualifiers::OCL_Weak) { |
| 3226 | return true; |
| 3227 | } |
| 3228 | |
| 3229 | if (auto recType = type->getAs<RecordType>()) { |
| 3230 | for (auto field : recType->getDecl()->fields()) { |
| 3231 | if (hasWeakMember(field->getType())) |
| 3232 | return true; |
| 3233 | } |
| 3234 | } |
| 3235 | |
| 3236 | return false; |
| 3237 | } |
| 3238 | |
| 3239 | /// For compatibility, we only want to set the "HasMRCWeakIvars" flag |
| 3240 | /// (and actually fill in a layout string) if we really do have any |
| 3241 | /// __weak ivars. |
| 3242 | static bool hasMRCWeakIvars(CodeGenModule &CGM, |
| 3243 | const ObjCImplementationDecl *ID) { |
| 3244 | if (!CGM.getLangOpts().ObjCWeak) return false; |
| 3245 | assert(CGM.getLangOpts().getGC() == LangOptions::NonGC); |
| 3246 | |
| 3247 | for (const ObjCIvarDecl *ivar = |
| 3248 | ID->getClassInterface()->all_declared_ivar_begin(); |
| 3249 | ivar; ivar = ivar->getNextIvar()) { |
| 3250 | if (hasWeakMember(ivar->getType())) |
| 3251 | return true; |
| 3252 | } |
| 3253 | |
| 3254 | return false; |
| 3255 | } |
| 3256 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3257 | /* |
| 3258 | struct _objc_class { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3259 | Class isa; |
| 3260 | Class super_class; |
| 3261 | const char *name; |
| 3262 | long version; |
| 3263 | long info; |
| 3264 | long instance_size; |
| 3265 | struct _objc_ivar_list *ivars; |
| 3266 | struct _objc_method_list *methods; |
| 3267 | struct _objc_cache *cache; |
| 3268 | struct _objc_protocol_list *protocols; |
| 3269 | // Objective-C 1.0 extensions (<rdr://4585769>) |
| 3270 | const char *ivar_layout; |
| 3271 | struct _objc_class_ext *ext; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3272 | }; |
| 3273 | |
| 3274 | See EmitClassExtension(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3275 | */ |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3276 | void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) { |
Daniel Dunbar | c61d0e9 | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3277 | DefinedSymbols.insert(ID->getIdentifier()); |
| 3278 | |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3279 | std::string ClassName = ID->getNameAsString(); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3280 | // FIXME: Gross |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3281 | ObjCInterfaceDecl *Interface = |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3282 | const_cast<ObjCInterfaceDecl*>(ID->getClassInterface()); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3283 | llvm::Constant *Protocols = |
Rafael Espindola | 8b27bdb | 2014-11-06 13:30:38 +0000 | [diff] [blame] | 3284 | EmitProtocolList("OBJC_CLASS_PROTOCOLS_" + ID->getName(), |
| 3285 | Interface->all_referenced_protocol_begin(), |
| 3286 | Interface->all_referenced_protocol_end()); |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 3287 | unsigned Flags = FragileABI_Class_Factory; |
John McCall | 0d54a17 | 2012-10-17 04:53:31 +0000 | [diff] [blame] | 3288 | if (ID->hasNonZeroConstructors() || ID->hasDestructors()) |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 3289 | Flags |= FragileABI_Class_HasCXXStructors; |
John McCall | 09ec1ec | 2015-10-21 22:06:03 +0000 | [diff] [blame] | 3290 | |
John McCall | 460ce58 | 2015-10-22 18:38:17 +0000 | [diff] [blame] | 3291 | bool hasMRCWeak = false; |
| 3292 | |
John McCall | 09ec1ec | 2015-10-21 22:06:03 +0000 | [diff] [blame] | 3293 | if (CGM.getLangOpts().ObjCAutoRefCount) |
| 3294 | Flags |= FragileABI_Class_CompiledByARC; |
John McCall | 460ce58 | 2015-10-22 18:38:17 +0000 | [diff] [blame] | 3295 | else if ((hasMRCWeak = hasMRCWeakIvars(CGM, ID))) |
| 3296 | Flags |= FragileABI_Class_HasMRCWeakIvars; |
John McCall | 09ec1ec | 2015-10-21 22:06:03 +0000 | [diff] [blame] | 3297 | |
John McCall | 3fd13f06 | 2015-10-21 18:06:47 +0000 | [diff] [blame] | 3298 | CharUnits Size = |
| 3299 | CGM.getContext().getASTObjCImplementationLayout(ID).getSize(); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3300 | |
| 3301 | // FIXME: Set CXX-structors flag. |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 3302 | if (ID->getClassInterface()->getVisibility() == HiddenVisibility) |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 3303 | Flags |= FragileABI_Class_Hidden; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3304 | |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3305 | enum { |
| 3306 | InstanceMethods, |
| 3307 | ClassMethods, |
| 3308 | NumMethodLists |
| 3309 | }; |
| 3310 | SmallVector<const ObjCMethodDecl *, 16> Methods[NumMethodLists]; |
| 3311 | for (const auto *MD : ID->methods()) { |
| 3312 | Methods[unsigned(MD->isClassMethod())].push_back(MD); |
| 3313 | } |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 3314 | |
Aaron Ballman | d85eff4 | 2014-03-14 15:02:45 +0000 | [diff] [blame] | 3315 | for (const auto *PID : ID->property_impls()) { |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 3316 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) { |
| 3317 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 3318 | |
| 3319 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3320 | if (GetMethodDefinition(MD)) |
| 3321 | Methods[InstanceMethods].push_back(MD); |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 3322 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3323 | if (GetMethodDefinition(MD)) |
| 3324 | Methods[InstanceMethods].push_back(MD); |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 3325 | } |
| 3326 | } |
| 3327 | |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3328 | ConstantInitBuilder builder(CGM); |
| 3329 | auto values = builder.beginStruct(ObjCTypes.ClassTy); |
| 3330 | values.add(EmitMetaClass(ID, Protocols, Methods[ClassMethods])); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3331 | if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) { |
Daniel Dunbar | c61d0e9 | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3332 | // Record a reference to the super class. |
| 3333 | LazySymbols.insert(Super->getIdentifier()); |
| 3334 | |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3335 | values.addBitCast(GetClassName(Super->getObjCRuntimeNameAsString()), |
| 3336 | ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3337 | } else { |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3338 | values.addNullPointer(ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3339 | } |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3340 | values.add(GetClassName(ID->getObjCRuntimeNameAsString())); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3341 | // Version is always 0. |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3342 | values.addInt(ObjCTypes.LongTy, 0); |
| 3343 | values.addInt(ObjCTypes.LongTy, Flags); |
| 3344 | values.addInt(ObjCTypes.LongTy, Size.getQuantity()); |
| 3345 | values.add(EmitIvarList(ID, false)); |
| 3346 | values.add(emitMethodList(ID->getName(), MethodListType::InstanceMethods, |
| 3347 | Methods[InstanceMethods])); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3348 | // cache is always NULL. |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3349 | values.addNullPointer(ObjCTypes.CachePtrTy); |
| 3350 | values.add(Protocols); |
| 3351 | values.add(BuildStrongIvarLayout(ID, CharUnits::Zero(), Size)); |
| 3352 | values.add(EmitClassExtension(ID, Size, hasMRCWeak, |
| 3353 | /*isMetaclass*/ false)); |
| 3354 | |
Rafael Espindola | 8b27bdb | 2014-11-06 13:30:38 +0000 | [diff] [blame] | 3355 | std::string Name("OBJC_CLASS_"); |
Fariborz Jahanian | eb80c98 | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 3356 | Name += ClassName; |
| 3357 | const char *Section = "__OBJC,__class,regular,no_dead_strip"; |
| 3358 | // Check for a forward reference. |
Rafael Espindola | 554256c | 2014-02-26 22:25:45 +0000 | [diff] [blame] | 3359 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true); |
Fariborz Jahanian | eb80c98 | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 3360 | if (GV) { |
| 3361 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 3362 | "Forward metaclass reference has incorrect type."); |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3363 | values.finishAndSetAsInitializer(GV); |
Fariborz Jahanian | eb80c98 | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 3364 | GV->setSection(Section); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3365 | GV->setAlignment(CGM.getPointerAlign().getQuantity()); |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 3366 | CGM.addCompilerUsedGlobal(GV); |
Rafael Espindola | 21039aa | 2014-02-27 16:26:32 +0000 | [diff] [blame] | 3367 | } else |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3368 | GV = CreateMetadataVar(Name, values, Section, CGM.getPointerAlign(), true); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3369 | DefinedClasses.push_back(GV); |
Fariborz Jahanian | f322f2f | 2014-03-11 00:25:05 +0000 | [diff] [blame] | 3370 | ImplementedClasses.push_back(Interface); |
Fariborz Jahanian | c057794 | 2011-04-22 22:02:28 +0000 | [diff] [blame] | 3371 | // method definition entries must be clear for next implementation. |
| 3372 | MethodDefinitions.clear(); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3373 | } |
| 3374 | |
| 3375 | llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID, |
| 3376 | llvm::Constant *Protocols, |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3377 | ArrayRef<const ObjCMethodDecl*> Methods) { |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 3378 | unsigned Flags = FragileABI_Class_Meta; |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3379 | unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ClassTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3380 | |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 3381 | if (ID->getClassInterface()->getVisibility() == HiddenVisibility) |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 3382 | Flags |= FragileABI_Class_Hidden; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3383 | |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3384 | ConstantInitBuilder builder(CGM); |
| 3385 | auto values = builder.beginStruct(ObjCTypes.ClassTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3386 | // The isa for the metaclass is the root of the hierarchy. |
| 3387 | const ObjCInterfaceDecl *Root = ID->getClassInterface(); |
| 3388 | while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) |
| 3389 | Root = Super; |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3390 | values.addBitCast(GetClassName(Root->getObjCRuntimeNameAsString()), |
| 3391 | ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3392 | // The super class for the metaclass is emitted as the name of the |
| 3393 | // super class. The runtime fixes this up to point to the |
| 3394 | // *metaclass* for the super class. |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3395 | if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) { |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3396 | values.addBitCast(GetClassName(Super->getObjCRuntimeNameAsString()), |
| 3397 | ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3398 | } else { |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3399 | values.addNullPointer(ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3400 | } |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3401 | values.add(GetClassName(ID->getObjCRuntimeNameAsString())); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3402 | // Version is always 0. |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3403 | values.addInt(ObjCTypes.LongTy, 0); |
| 3404 | values.addInt(ObjCTypes.LongTy, Flags); |
| 3405 | values.addInt(ObjCTypes.LongTy, Size); |
| 3406 | values.add(EmitIvarList(ID, true)); |
| 3407 | values.add(emitMethodList(ID->getName(), MethodListType::ClassMethods, |
| 3408 | Methods)); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3409 | // cache is always NULL. |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3410 | values.addNullPointer(ObjCTypes.CachePtrTy); |
| 3411 | values.add(Protocols); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3412 | // ivar_layout for metaclass is always NULL. |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3413 | values.addNullPointer(ObjCTypes.Int8PtrTy); |
Manman Ren | ad0e791 | 2016-01-29 19:22:54 +0000 | [diff] [blame] | 3414 | // The class extension is used to store class properties for metaclasses. |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3415 | values.add(EmitClassExtension(ID, CharUnits::Zero(), false/*hasMRCWeak*/, |
| 3416 | /*isMetaclass*/true)); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3417 | |
Rafael Espindola | 8b27bdb | 2014-11-06 13:30:38 +0000 | [diff] [blame] | 3418 | std::string Name("OBJC_METACLASS_"); |
Benjamin Kramer | 1bbcbd0 | 2012-07-31 11:45:39 +0000 | [diff] [blame] | 3419 | Name += ID->getName(); |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 3420 | |
| 3421 | // Check for a forward reference. |
Rafael Espindola | 554256c | 2014-02-26 22:25:45 +0000 | [diff] [blame] | 3422 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true); |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 3423 | if (GV) { |
| 3424 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 3425 | "Forward metaclass reference has incorrect type."); |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3426 | values.finishAndSetAsInitializer(GV); |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 3427 | } else { |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3428 | GV = values.finishAndCreateGlobal(Name, CGM.getPointerAlign(), |
| 3429 | /*constant*/ false, |
| 3430 | llvm::GlobalValue::PrivateLinkage); |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 3431 | } |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3432 | GV->setSection("__OBJC,__meta_class,regular,no_dead_strip"); |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 3433 | CGM.addCompilerUsedGlobal(GV); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3434 | |
| 3435 | return GV; |
| 3436 | } |
| 3437 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3438 | llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) { |
Rafael Espindola | 8b27bdb | 2014-11-06 13:30:38 +0000 | [diff] [blame] | 3439 | std::string Name = "OBJC_METACLASS_" + ID->getNameAsString(); |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 3440 | |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 3441 | // FIXME: Should we look these up somewhere other than the module. Its a bit |
| 3442 | // silly since we only generate these while processing an implementation, so |
| 3443 | // exactly one pointer would work if know when we entered/exitted an |
| 3444 | // implementation block. |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 3445 | |
| 3446 | // Check for an existing forward reference. |
Fariborz Jahanian | 475831b | 2009-01-07 20:11:22 +0000 | [diff] [blame] | 3447 | // Previously, metaclass with internal linkage may have been defined. |
| 3448 | // pass 'true' as 2nd argument so it is returned. |
Rafael Espindola | 21039aa | 2014-02-27 16:26:32 +0000 | [diff] [blame] | 3449 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true); |
| 3450 | if (!GV) |
| 3451 | GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false, |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3452 | llvm::GlobalValue::PrivateLinkage, nullptr, |
| 3453 | Name); |
Rafael Espindola | 21039aa | 2014-02-27 16:26:32 +0000 | [diff] [blame] | 3454 | |
| 3455 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 3456 | "Forward metaclass reference has incorrect type."); |
Rafael Espindola | 21039aa | 2014-02-27 16:26:32 +0000 | [diff] [blame] | 3457 | return GV; |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 3458 | } |
| 3459 | |
Fariborz Jahanian | eb80c98 | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 3460 | llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) { |
Rafael Espindola | 8b27bdb | 2014-11-06 13:30:38 +0000 | [diff] [blame] | 3461 | std::string Name = "OBJC_CLASS_" + ID->getNameAsString(); |
Rafael Espindola | 21039aa | 2014-02-27 16:26:32 +0000 | [diff] [blame] | 3462 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true); |
| 3463 | |
| 3464 | if (!GV) |
| 3465 | GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false, |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3466 | llvm::GlobalValue::PrivateLinkage, nullptr, |
| 3467 | Name); |
Rafael Espindola | 21039aa | 2014-02-27 16:26:32 +0000 | [diff] [blame] | 3468 | |
| 3469 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 3470 | "Forward class metadata reference has incorrect type."); |
Rafael Espindola | 21039aa | 2014-02-27 16:26:32 +0000 | [diff] [blame] | 3471 | return GV; |
Fariborz Jahanian | eb80c98 | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 3472 | } |
| 3473 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3474 | /* |
John McCall | 3fd13f06 | 2015-10-21 18:06:47 +0000 | [diff] [blame] | 3475 | Emit a "class extension", which in this specific context means extra |
| 3476 | data that doesn't fit in the normal fragile-ABI class structure, and |
| 3477 | has nothing to do with the language concept of a class extension. |
| 3478 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3479 | struct objc_class_ext { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3480 | uint32_t size; |
| 3481 | const char *weak_ivar_layout; |
| 3482 | struct _objc_property_list *properties; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3483 | }; |
| 3484 | */ |
| 3485 | llvm::Constant * |
John McCall | 3fd13f06 | 2015-10-21 18:06:47 +0000 | [diff] [blame] | 3486 | CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID, |
Manman Ren | ad0e791 | 2016-01-29 19:22:54 +0000 | [diff] [blame] | 3487 | CharUnits InstanceSize, bool hasMRCWeakIvars, |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3488 | bool isMetaclass) { |
| 3489 | // Weak ivar layout. |
| 3490 | llvm::Constant *layout; |
| 3491 | if (isMetaclass) { |
| 3492 | layout = llvm::ConstantPointerNull::get(CGM.Int8PtrTy); |
| 3493 | } else { |
| 3494 | layout = BuildWeakIvarLayout(ID, CharUnits::Zero(), InstanceSize, |
| 3495 | hasMRCWeakIvars); |
| 3496 | } |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3497 | |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3498 | // Properties. |
| 3499 | llvm::Constant *propertyList = |
| 3500 | EmitPropertyList((isMetaclass ? Twine("\01l_OBJC_$_CLASS_PROP_LIST_") |
| 3501 | : Twine("\01l_OBJC_$_PROP_LIST_")) |
| 3502 | + ID->getName(), |
| 3503 | ID, ID->getClassInterface(), ObjCTypes, isMetaclass); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3504 | |
| 3505 | // Return null if no extension bits are used. |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3506 | if (layout->isNullValue() && propertyList->isNullValue()) { |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3507 | return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy); |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3508 | } |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3509 | |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3510 | uint64_t size = |
| 3511 | CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ClassExtensionTy); |
| 3512 | |
| 3513 | ConstantInitBuilder builder(CGM); |
| 3514 | auto values = builder.beginStruct(ObjCTypes.ClassExtensionTy); |
| 3515 | values.addInt(ObjCTypes.IntTy, size); |
| 3516 | values.add(layout); |
| 3517 | values.add(propertyList); |
| 3518 | |
| 3519 | return CreateMetadataVar("OBJC_CLASSEXT_" + ID->getName(), values, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3520 | "__OBJC,__class_ext,regular,no_dead_strip", |
| 3521 | CGM.getPointerAlign(), true); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3522 | } |
| 3523 | |
| 3524 | /* |
| 3525 | struct objc_ivar { |
Bill Wendling | f1a3fca | 2012-02-22 09:30:11 +0000 | [diff] [blame] | 3526 | char *ivar_name; |
| 3527 | char *ivar_type; |
| 3528 | int ivar_offset; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3529 | }; |
| 3530 | |
| 3531 | struct objc_ivar_list { |
Bill Wendling | f1a3fca | 2012-02-22 09:30:11 +0000 | [diff] [blame] | 3532 | int ivar_count; |
| 3533 | struct objc_ivar list[count]; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3534 | }; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3535 | */ |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3536 | llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID, |
Fariborz Jahanian | b042a59 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 3537 | bool ForClass) { |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3538 | // When emitting the root class GCC emits ivar entries for the |
| 3539 | // actual class structure. It is not clear if we need to follow this |
| 3540 | // behavior; for now lets try and get away with not doing it. If so, |
| 3541 | // the cleanest solution would be to make up an ObjCInterfaceDecl |
| 3542 | // for the class. |
| 3543 | if (ForClass) |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3544 | return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3545 | |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 3546 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3547 | |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3548 | ConstantInitBuilder builder(CGM); |
| 3549 | auto ivarList = builder.beginStruct(); |
| 3550 | auto countSlot = ivarList.addPlaceholder(); |
| 3551 | auto ivars = ivarList.beginArray(ObjCTypes.IvarTy); |
| 3552 | |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 3553 | for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin(); |
Fariborz Jahanian | b26d578 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 3554 | IVD; IVD = IVD->getNextIvar()) { |
Fariborz Jahanian | 7c80959 | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 3555 | // Ignore unnamed bit-fields. |
| 3556 | if (!IVD->getDeclName()) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3557 | continue; |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3558 | |
| 3559 | auto ivar = ivars.beginStruct(ObjCTypes.IvarTy); |
| 3560 | ivar.add(GetMethodVarName(IVD->getIdentifier())); |
| 3561 | ivar.add(GetMethodVarType(IVD)); |
| 3562 | ivar.addInt(ObjCTypes.IntTy, ComputeIvarBaseOffset(CGM, OID, IVD)); |
| 3563 | ivar.finishAndAddTo(ivars); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3564 | } |
| 3565 | |
| 3566 | // Return null for empty list. |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3567 | auto count = ivars.size(); |
| 3568 | if (count == 0) { |
| 3569 | ivars.abandon(); |
| 3570 | ivarList.abandon(); |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3571 | return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3572 | } |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3573 | |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3574 | ivars.finishAndAddTo(ivarList); |
| 3575 | ivarList.fillPlaceholderWithInt(countSlot, ObjCTypes.IntTy, count); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3576 | |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3577 | llvm::GlobalVariable *GV; |
| 3578 | if (ForClass) |
Rafael Espindola | 8b27bdb | 2014-11-06 13:30:38 +0000 | [diff] [blame] | 3579 | GV = |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3580 | CreateMetadataVar("OBJC_CLASS_VARIABLES_" + ID->getName(), ivarList, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3581 | "__OBJC,__class_vars,regular,no_dead_strip", |
| 3582 | CGM.getPointerAlign(), true); |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3583 | else |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3584 | GV = CreateMetadataVar("OBJC_INSTANCE_VARIABLES_" + ID->getName(), ivarList, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3585 | "__OBJC,__instance_vars,regular,no_dead_strip", |
| 3586 | CGM.getPointerAlign(), true); |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 3587 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3588 | } |
| 3589 | |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3590 | /// Build a struct objc_method_description constant for the given method. |
| 3591 | /// |
| 3592 | /// struct objc_method_description { |
| 3593 | /// SEL method_name; |
| 3594 | /// char *method_types; |
| 3595 | /// }; |
| 3596 | void CGObjCMac::emitMethodDescriptionConstant(ConstantArrayBuilder &builder, |
| 3597 | const ObjCMethodDecl *MD) { |
| 3598 | auto description = builder.beginStruct(ObjCTypes.MethodDescriptionTy); |
| 3599 | description.addBitCast(GetMethodVarName(MD->getSelector()), |
| 3600 | ObjCTypes.SelectorPtrTy); |
| 3601 | description.add(GetMethodVarType(MD)); |
| 3602 | description.finishAndAddTo(builder); |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 3603 | } |
| 3604 | |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3605 | /// Build a struct objc_method constant for the given method. |
| 3606 | /// |
| 3607 | /// struct objc_method { |
| 3608 | /// SEL method_name; |
| 3609 | /// char *method_types; |
| 3610 | /// void *method; |
| 3611 | /// }; |
| 3612 | void CGObjCMac::emitMethodConstant(ConstantArrayBuilder &builder, |
| 3613 | const ObjCMethodDecl *MD) { |
| 3614 | llvm::Function *fn = GetMethodDefinition(MD); |
| 3615 | assert(fn && "no definition registered for method"); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3616 | |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3617 | auto method = builder.beginStruct(ObjCTypes.MethodTy); |
| 3618 | method.addBitCast(GetMethodVarName(MD->getSelector()), |
| 3619 | ObjCTypes.SelectorPtrTy); |
| 3620 | method.add(GetMethodVarType(MD)); |
| 3621 | method.addBitCast(fn, ObjCTypes.Int8PtrTy); |
| 3622 | method.finishAndAddTo(builder); |
| 3623 | } |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3624 | |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3625 | /// Build a struct objc_method_list or struct objc_method_description_list, |
| 3626 | /// as appropriate. |
| 3627 | /// |
| 3628 | /// struct objc_method_list { |
| 3629 | /// struct objc_method_list *obsolete; |
| 3630 | /// int count; |
| 3631 | /// struct objc_method methods_list[count]; |
| 3632 | /// }; |
| 3633 | /// |
| 3634 | /// struct objc_method_description_list { |
| 3635 | /// int count; |
| 3636 | /// struct objc_method_description list[count]; |
| 3637 | /// }; |
| 3638 | llvm::Constant *CGObjCMac::emitMethodList(Twine name, MethodListType MLT, |
| 3639 | ArrayRef<const ObjCMethodDecl *> methods) { |
| 3640 | StringRef prefix; |
| 3641 | StringRef section; |
| 3642 | bool forProtocol = false; |
Saleem Abdulrasool | d48b0a3 | 2016-10-24 20:47:58 +0000 | [diff] [blame] | 3643 | switch (MLT) { |
| 3644 | case MethodListType::CategoryInstanceMethods: |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3645 | prefix = "OBJC_CATEGORY_INSTANCE_METHODS_"; |
| 3646 | section = "__OBJC,__cat_inst_meth,regular,no_dead_strip"; |
| 3647 | forProtocol = false; |
Saleem Abdulrasool | d48b0a3 | 2016-10-24 20:47:58 +0000 | [diff] [blame] | 3648 | break; |
| 3649 | case MethodListType::CategoryClassMethods: |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3650 | prefix = "OBJC_CATEGORY_CLASS_METHODS_"; |
| 3651 | section = "__OBJC,__cat_cls_meth,regular,no_dead_strip"; |
| 3652 | forProtocol = false; |
Saleem Abdulrasool | d48b0a3 | 2016-10-24 20:47:58 +0000 | [diff] [blame] | 3653 | break; |
| 3654 | case MethodListType::InstanceMethods: |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3655 | prefix = "OBJC_INSTANCE_METHODS_"; |
| 3656 | section = "__OBJC,__inst_meth,regular,no_dead_strip"; |
| 3657 | forProtocol = false; |
Saleem Abdulrasool | d48b0a3 | 2016-10-24 20:47:58 +0000 | [diff] [blame] | 3658 | break; |
| 3659 | case MethodListType::ClassMethods: |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3660 | prefix = "OBJC_CLASS_METHODS_"; |
| 3661 | section = "__OBJC,__cls_meth,regular,no_dead_strip"; |
| 3662 | forProtocol = false; |
Saleem Abdulrasool | d48b0a3 | 2016-10-24 20:47:58 +0000 | [diff] [blame] | 3663 | break; |
Saleem Abdulrasool | d48b0a3 | 2016-10-24 20:47:58 +0000 | [diff] [blame] | 3664 | case MethodListType::ProtocolInstanceMethods: |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3665 | prefix = "OBJC_PROTOCOL_INSTANCE_METHODS_"; |
| 3666 | section = "__OBJC,__cat_inst_meth,regular,no_dead_strip"; |
| 3667 | forProtocol = true; |
| 3668 | break; |
Saleem Abdulrasool | d48b0a3 | 2016-10-24 20:47:58 +0000 | [diff] [blame] | 3669 | case MethodListType::ProtocolClassMethods: |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3670 | prefix = "OBJC_PROTOCOL_CLASS_METHODS_"; |
| 3671 | section = "__OBJC,__cat_cls_meth,regular,no_dead_strip"; |
| 3672 | forProtocol = true; |
| 3673 | break; |
Saleem Abdulrasool | d48b0a3 | 2016-10-24 20:47:58 +0000 | [diff] [blame] | 3674 | case MethodListType::OptionalProtocolInstanceMethods: |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3675 | prefix = "OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"; |
| 3676 | section = "__OBJC,__cat_inst_meth,regular,no_dead_strip"; |
| 3677 | forProtocol = true; |
| 3678 | break; |
Saleem Abdulrasool | d48b0a3 | 2016-10-24 20:47:58 +0000 | [diff] [blame] | 3679 | case MethodListType::OptionalProtocolClassMethods: |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3680 | prefix = "OBJC_PROTOCOL_CLASS_METHODS_OPT_"; |
| 3681 | section = "__OBJC,__cat_cls_meth,regular,no_dead_strip"; |
| 3682 | forProtocol = true; |
| 3683 | break; |
Saleem Abdulrasool | d48b0a3 | 2016-10-24 20:47:58 +0000 | [diff] [blame] | 3684 | } |
| 3685 | |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3686 | // Return null for empty list. |
| 3687 | if (methods.empty()) |
| 3688 | return llvm::Constant::getNullValue(forProtocol |
| 3689 | ? ObjCTypes.MethodDescriptionListPtrTy |
| 3690 | : ObjCTypes.MethodListPtrTy); |
| 3691 | |
| 3692 | // For protocols, this is an objc_method_description_list, which has |
| 3693 | // a slightly different structure. |
| 3694 | if (forProtocol) { |
| 3695 | ConstantInitBuilder builder(CGM); |
| 3696 | auto values = builder.beginStruct(); |
| 3697 | values.addInt(ObjCTypes.IntTy, methods.size()); |
| 3698 | auto methodArray = values.beginArray(ObjCTypes.MethodDescriptionTy); |
| 3699 | for (auto MD : methods) { |
| 3700 | emitMethodDescriptionConstant(methodArray, MD); |
| 3701 | } |
| 3702 | methodArray.finishAndAddTo(values); |
| 3703 | |
| 3704 | llvm::GlobalVariable *GV = CreateMetadataVar(prefix + name, values, section, |
| 3705 | CGM.getPointerAlign(), true); |
| 3706 | return llvm::ConstantExpr::getBitCast(GV, |
| 3707 | ObjCTypes.MethodDescriptionListPtrTy); |
| 3708 | } |
| 3709 | |
| 3710 | // Otherwise, it's an objc_method_list. |
| 3711 | ConstantInitBuilder builder(CGM); |
| 3712 | auto values = builder.beginStruct(); |
| 3713 | values.addNullPointer(ObjCTypes.Int8PtrTy); |
| 3714 | values.addInt(ObjCTypes.IntTy, methods.size()); |
| 3715 | auto methodArray = values.beginArray(ObjCTypes.MethodTy); |
| 3716 | for (auto MD : methods) { |
| 3717 | emitMethodConstant(methodArray, MD); |
| 3718 | } |
| 3719 | methodArray.finishAndAddTo(values); |
| 3720 | |
| 3721 | llvm::GlobalVariable *GV = CreateMetadataVar(prefix + name, values, section, |
Saleem Abdulrasool | d48b0a3 | 2016-10-24 20:47:58 +0000 | [diff] [blame] | 3722 | CGM.getPointerAlign(), true); |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 3723 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListPtrTy); |
Daniel Dunbar | a94ecd2 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 3724 | } |
| 3725 | |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 3726 | llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3727 | const ObjCContainerDecl *CD) { |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 3728 | SmallString<256> Name; |
Fariborz Jahanian | 0196a1c | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 3729 | GetNameForMethod(OMD, CD, Name); |
Daniel Dunbar | a94ecd2 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 3730 | |
Daniel Dunbar | bf8c24a | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 3731 | CodeGenTypes &Types = CGM.getTypes(); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3732 | llvm::FunctionType *MethodTy = |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 3733 | Types.GetFunctionType(Types.arrangeObjCMethodDeclaration(OMD)); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3734 | llvm::Function *Method = |
Daniel Dunbar | 7a95ca3 | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 3735 | llvm::Function::Create(MethodTy, |
Daniel Dunbar | a94ecd2 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 3736 | llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | d238681 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 3737 | Name.str(), |
Daniel Dunbar | a94ecd2 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 3738 | &CGM.getModule()); |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 3739 | MethodDefinitions.insert(std::make_pair(OMD, Method)); |
Daniel Dunbar | a94ecd2 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 3740 | |
Daniel Dunbar | a94ecd2 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 3741 | return Method; |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 3742 | } |
| 3743 | |
Alp Toker | 541d507 | 2014-06-07 23:30:53 +0000 | [diff] [blame] | 3744 | llvm::GlobalVariable *CGObjCCommonMac::CreateMetadataVar(Twine Name, |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 3745 | ConstantStructBuilder &Init, |
| 3746 | StringRef Section, |
| 3747 | CharUnits Align, |
| 3748 | bool AddToUsed) { |
| 3749 | llvm::GlobalVariable *GV = |
| 3750 | Init.finishAndCreateGlobal(Name, Align, /*constant*/ false, |
| 3751 | llvm::GlobalValue::PrivateLinkage); |
| 3752 | if (!Section.empty()) |
| 3753 | GV->setSection(Section); |
| 3754 | if (AddToUsed) |
| 3755 | CGM.addCompilerUsedGlobal(GV); |
| 3756 | return GV; |
| 3757 | } |
| 3758 | |
| 3759 | llvm::GlobalVariable *CGObjCCommonMac::CreateMetadataVar(Twine Name, |
Alp Toker | 541d507 | 2014-06-07 23:30:53 +0000 | [diff] [blame] | 3760 | llvm::Constant *Init, |
| 3761 | StringRef Section, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3762 | CharUnits Align, |
Alp Toker | 541d507 | 2014-06-07 23:30:53 +0000 | [diff] [blame] | 3763 | bool AddToUsed) { |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3764 | llvm::Type *Ty = Init->getType(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3765 | llvm::GlobalVariable *GV = |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 3766 | new llvm::GlobalVariable(CGM.getModule(), Ty, false, |
Rafael Espindola | 5179a4e | 2014-02-27 19:01:11 +0000 | [diff] [blame] | 3767 | llvm::GlobalValue::PrivateLinkage, Init, Name); |
Alp Toker | 541d507 | 2014-06-07 23:30:53 +0000 | [diff] [blame] | 3768 | if (!Section.empty()) |
Daniel Dunbar | 30c6536 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 3769 | GV->setSection(Section); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3770 | GV->setAlignment(Align.getQuantity()); |
Daniel Dunbar | 463cc8a | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 3771 | if (AddToUsed) |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 3772 | CGM.addCompilerUsedGlobal(GV); |
Daniel Dunbar | 30c6536 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 3773 | return GV; |
| 3774 | } |
| 3775 | |
Saleem Abdulrasool | 271106c | 2016-09-16 23:41:13 +0000 | [diff] [blame] | 3776 | llvm::GlobalVariable * |
Saleem Abdulrasool | 82f6add | 2016-09-20 18:38:54 +0000 | [diff] [blame] | 3777 | CGObjCCommonMac::CreateCStringLiteral(StringRef Name, ObjCLabelType Type, |
| 3778 | bool ForceNonFragileABI, |
| 3779 | bool NullTerminate) { |
Saleem Abdulrasool | 271106c | 2016-09-16 23:41:13 +0000 | [diff] [blame] | 3780 | StringRef Label; |
| 3781 | switch (Type) { |
| 3782 | case ObjCLabelType::ClassName: Label = "OBJC_CLASS_NAME_"; break; |
| 3783 | case ObjCLabelType::MethodVarName: Label = "OBJC_METH_VAR_NAME_"; break; |
| 3784 | case ObjCLabelType::MethodVarType: Label = "OBJC_METH_VAR_TYPE_"; break; |
| 3785 | case ObjCLabelType::PropertyName: Label = "OBJC_PROP_NAME_ATTR_"; break; |
| 3786 | } |
| 3787 | |
Saleem Abdulrasool | 82f6add | 2016-09-20 18:38:54 +0000 | [diff] [blame] | 3788 | bool NonFragile = ForceNonFragileABI || isNonFragileABI(); |
| 3789 | |
Saleem Abdulrasool | 271106c | 2016-09-16 23:41:13 +0000 | [diff] [blame] | 3790 | StringRef Section; |
| 3791 | switch (Type) { |
| 3792 | case ObjCLabelType::ClassName: |
Saleem Abdulrasool | 82f6add | 2016-09-20 18:38:54 +0000 | [diff] [blame] | 3793 | Section = NonFragile ? "__TEXT,__objc_classname,cstring_literals" |
| 3794 | : "__TEXT,__cstring,cstring_literals"; |
Saleem Abdulrasool | 271106c | 2016-09-16 23:41:13 +0000 | [diff] [blame] | 3795 | break; |
| 3796 | case ObjCLabelType::MethodVarName: |
Saleem Abdulrasool | 82f6add | 2016-09-20 18:38:54 +0000 | [diff] [blame] | 3797 | Section = NonFragile ? "__TEXT,__objc_methname,cstring_literals" |
| 3798 | : "__TEXT,__cstring,cstring_literals"; |
Saleem Abdulrasool | 271106c | 2016-09-16 23:41:13 +0000 | [diff] [blame] | 3799 | break; |
| 3800 | case ObjCLabelType::MethodVarType: |
Saleem Abdulrasool | 82f6add | 2016-09-20 18:38:54 +0000 | [diff] [blame] | 3801 | Section = NonFragile ? "__TEXT,__objc_methtype,cstring_literals" |
| 3802 | : "__TEXT,__cstring,cstring_literals"; |
Saleem Abdulrasool | 271106c | 2016-09-16 23:41:13 +0000 | [diff] [blame] | 3803 | break; |
| 3804 | case ObjCLabelType::PropertyName: |
| 3805 | Section = "__TEXT,__cstring,cstring_literals"; |
| 3806 | break; |
| 3807 | } |
| 3808 | |
Saleem Abdulrasool | 82f6add | 2016-09-20 18:38:54 +0000 | [diff] [blame] | 3809 | llvm::Constant *Value = |
| 3810 | llvm::ConstantDataArray::getString(VMContext, Name, NullTerminate); |
Saleem Abdulrasool | 271106c | 2016-09-16 23:41:13 +0000 | [diff] [blame] | 3811 | llvm::GlobalVariable *GV = |
Saleem Abdulrasool | 0c54dc8 | 2016-09-18 16:12:04 +0000 | [diff] [blame] | 3812 | new llvm::GlobalVariable(CGM.getModule(), Value->getType(), |
| 3813 | /*isConstant=*/true, |
Saleem Abdulrasool | 271106c | 2016-09-16 23:41:13 +0000 | [diff] [blame] | 3814 | llvm::GlobalValue::PrivateLinkage, Value, Label); |
Saleem Abdulrasool | 3f30751 | 2016-09-18 16:12:14 +0000 | [diff] [blame] | 3815 | if (CGM.getTriple().isOSBinFormatMachO()) |
| 3816 | GV->setSection(Section); |
| 3817 | GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
Saleem Abdulrasool | 271106c | 2016-09-16 23:41:13 +0000 | [diff] [blame] | 3818 | GV->setAlignment(CharUnits::One().getQuantity()); |
| 3819 | CGM.addCompilerUsedGlobal(GV); |
| 3820 | |
| 3821 | return GV; |
| 3822 | } |
| 3823 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3824 | llvm::Function *CGObjCMac::ModuleInitFunction() { |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3825 | // Abuse this interface function as a place to finalize. |
| 3826 | FinishModule(); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3827 | return nullptr; |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 3828 | } |
| 3829 | |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 3830 | llvm::Constant *CGObjCMac::GetPropertyGetFunction() { |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 3831 | return ObjCTypes.getGetPropertyFn(); |
Daniel Dunbar | a91c3e0 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 3832 | } |
| 3833 | |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 3834 | llvm::Constant *CGObjCMac::GetPropertySetFunction() { |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 3835 | return ObjCTypes.getSetPropertyFn(); |
Daniel Dunbar | a91c3e0 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 3836 | } |
| 3837 | |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 3838 | llvm::Constant *CGObjCMac::GetOptimizedPropertySetFunction(bool atomic, |
| 3839 | bool copy) { |
| 3840 | return ObjCTypes.getOptimizedSetPropertyFn(atomic, copy); |
| 3841 | } |
| 3842 | |
David Chisnall | 168b80f | 2010-12-26 22:13:16 +0000 | [diff] [blame] | 3843 | llvm::Constant *CGObjCMac::GetGetStructFunction() { |
| 3844 | return ObjCTypes.getCopyStructFn(); |
| 3845 | } |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 3846 | |
David Chisnall | 168b80f | 2010-12-26 22:13:16 +0000 | [diff] [blame] | 3847 | llvm::Constant *CGObjCMac::GetSetStructFunction() { |
Fariborz Jahanian | 5a8c203 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 3848 | return ObjCTypes.getCopyStructFn(); |
| 3849 | } |
| 3850 | |
David Chisnall | 0d75e06 | 2012-12-17 18:54:24 +0000 | [diff] [blame] | 3851 | llvm::Constant *CGObjCMac::GetCppAtomicObjectGetFunction() { |
| 3852 | return ObjCTypes.getCppAtomicObjectFunction(); |
| 3853 | } |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 3854 | |
David Chisnall | 0d75e06 | 2012-12-17 18:54:24 +0000 | [diff] [blame] | 3855 | llvm::Constant *CGObjCMac::GetCppAtomicObjectSetFunction() { |
Fariborz Jahanian | 1e1b549 | 2012-01-06 18:07:23 +0000 | [diff] [blame] | 3856 | return ObjCTypes.getCppAtomicObjectFunction(); |
| 3857 | } |
| 3858 | |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 3859 | llvm::Constant *CGObjCMac::EnumerationMutationFunction() { |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 3860 | return ObjCTypes.getEnumerationMutationFn(); |
Anders Carlsson | 3f35a26 | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 3861 | } |
| 3862 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3863 | void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) { |
| 3864 | return EmitTryOrSynchronizedStmt(CGF, S); |
| 3865 | } |
| 3866 | |
| 3867 | void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF, |
| 3868 | const ObjCAtSynchronizedStmt &S) { |
| 3869 | return EmitTryOrSynchronizedStmt(CGF, S); |
| 3870 | } |
| 3871 | |
John McCall | 65bea08 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 3872 | namespace { |
David Blaikie | 7e70d68 | 2015-08-18 22:40:54 +0000 | [diff] [blame] | 3873 | struct PerformFragileFinally final : EHScopeStack::Cleanup { |
John McCall | 65bea08 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 3874 | const Stmt &S; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3875 | Address SyncArgSlot; |
| 3876 | Address CallTryExitVar; |
| 3877 | Address ExceptionData; |
John McCall | 65bea08 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 3878 | ObjCTypesHelper &ObjCTypes; |
| 3879 | PerformFragileFinally(const Stmt *S, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3880 | Address SyncArgSlot, |
| 3881 | Address CallTryExitVar, |
| 3882 | Address ExceptionData, |
John McCall | 65bea08 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 3883 | ObjCTypesHelper *ObjCTypes) |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3884 | : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar), |
John McCall | 65bea08 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 3885 | ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {} |
| 3886 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3887 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
John McCall | 65bea08 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 3888 | // Check whether we need to call objc_exception_try_exit. |
| 3889 | // In optimized code, this branch will always be folded. |
| 3890 | llvm::BasicBlock *FinallyCallExit = |
| 3891 | CGF.createBasicBlock("finally.call_exit"); |
| 3892 | llvm::BasicBlock *FinallyNoCallExit = |
| 3893 | CGF.createBasicBlock("finally.no_call_exit"); |
| 3894 | CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar), |
| 3895 | FinallyCallExit, FinallyNoCallExit); |
| 3896 | |
| 3897 | CGF.EmitBlock(FinallyCallExit); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3898 | CGF.EmitNounwindRuntimeCall(ObjCTypes.getExceptionTryExitFn(), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3899 | ExceptionData.getPointer()); |
John McCall | 65bea08 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 3900 | |
| 3901 | CGF.EmitBlock(FinallyNoCallExit); |
| 3902 | |
| 3903 | if (isa<ObjCAtTryStmt>(S)) { |
| 3904 | if (const ObjCAtFinallyStmt* FinallyStmt = |
John McCall | cebe0ca | 2010-08-11 00:16:14 +0000 | [diff] [blame] | 3905 | cast<ObjCAtTryStmt>(S).getFinallyStmt()) { |
John McCall | 638d4f5 | 2013-04-03 00:56:07 +0000 | [diff] [blame] | 3906 | // Don't try to do the @finally if this is an EH cleanup. |
| 3907 | if (flags.isForEHCleanup()) return; |
| 3908 | |
John McCall | cebe0ca | 2010-08-11 00:16:14 +0000 | [diff] [blame] | 3909 | // Save the current cleanup destination in case there's |
| 3910 | // control flow inside the finally statement. |
| 3911 | llvm::Value *CurCleanupDest = |
| 3912 | CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot()); |
| 3913 | |
John McCall | 65bea08 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 3914 | CGF.EmitStmt(FinallyStmt->getFinallyBody()); |
| 3915 | |
John McCall | cebe0ca | 2010-08-11 00:16:14 +0000 | [diff] [blame] | 3916 | if (CGF.HaveInsertPoint()) { |
| 3917 | CGF.Builder.CreateStore(CurCleanupDest, |
| 3918 | CGF.getNormalCleanupDestSlot()); |
| 3919 | } else { |
| 3920 | // Currently, the end of the cleanup must always exist. |
| 3921 | CGF.EnsureInsertPoint(); |
| 3922 | } |
| 3923 | } |
John McCall | 65bea08 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 3924 | } else { |
| 3925 | // Emit objc_sync_exit(expr); as finally's sole statement for |
| 3926 | // @synchronized. |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3927 | llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3928 | CGF.EmitNounwindRuntimeCall(ObjCTypes.getSyncExitFn(), SyncArg); |
John McCall | 65bea08 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 3929 | } |
| 3930 | } |
| 3931 | }; |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3932 | |
| 3933 | class FragileHazards { |
| 3934 | CodeGenFunction &CGF; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3935 | SmallVector<llvm::Value*, 20> Locals; |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3936 | llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry; |
| 3937 | |
| 3938 | llvm::InlineAsm *ReadHazard; |
| 3939 | llvm::InlineAsm *WriteHazard; |
| 3940 | |
| 3941 | llvm::FunctionType *GetAsmFnType(); |
| 3942 | |
| 3943 | void collectLocals(); |
| 3944 | void emitReadHazard(CGBuilderTy &Builder); |
| 3945 | |
| 3946 | public: |
| 3947 | FragileHazards(CodeGenFunction &CGF); |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3948 | |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3949 | void emitWriteHazard(); |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3950 | void emitHazardsInNewBlocks(); |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3951 | }; |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 3952 | } // end anonymous namespace |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3953 | |
| 3954 | /// Create the fragile-ABI read and write hazards based on the current |
| 3955 | /// state of the function, which is presumed to be immediately prior |
| 3956 | /// to a @try block. These hazards are used to maintain correct |
| 3957 | /// semantics in the face of optimization and the fragile ABI's |
| 3958 | /// cavalier use of setjmp/longjmp. |
| 3959 | FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) { |
| 3960 | collectLocals(); |
| 3961 | |
| 3962 | if (Locals.empty()) return; |
| 3963 | |
| 3964 | // Collect all the blocks in the function. |
| 3965 | for (llvm::Function::iterator |
| 3966 | I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I) |
| 3967 | BlocksBeforeTry.insert(&*I); |
| 3968 | |
| 3969 | llvm::FunctionType *AsmFnTy = GetAsmFnType(); |
| 3970 | |
| 3971 | // Create a read hazard for the allocas. This inhibits dead-store |
| 3972 | // optimizations and forces the values to memory. This hazard is |
| 3973 | // inserted before any 'throwing' calls in the protected scope to |
| 3974 | // reflect the possibility that the variables might be read from the |
| 3975 | // catch block if the call throws. |
| 3976 | { |
| 3977 | std::string Constraint; |
| 3978 | for (unsigned I = 0, E = Locals.size(); I != E; ++I) { |
| 3979 | if (I) Constraint += ','; |
| 3980 | Constraint += "*m"; |
| 3981 | } |
| 3982 | |
| 3983 | ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false); |
| 3984 | } |
| 3985 | |
| 3986 | // Create a write hazard for the allocas. This inhibits folding |
| 3987 | // loads across the hazard. This hazard is inserted at the |
| 3988 | // beginning of the catch path to reflect the possibility that the |
| 3989 | // variables might have been written within the protected scope. |
| 3990 | { |
| 3991 | std::string Constraint; |
| 3992 | for (unsigned I = 0, E = Locals.size(); I != E; ++I) { |
| 3993 | if (I) Constraint += ','; |
| 3994 | Constraint += "=*m"; |
| 3995 | } |
| 3996 | |
| 3997 | WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false); |
| 3998 | } |
| 3999 | } |
| 4000 | |
| 4001 | /// Emit a write hazard at the current location. |
| 4002 | void FragileHazards::emitWriteHazard() { |
| 4003 | if (Locals.empty()) return; |
| 4004 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4005 | CGF.EmitNounwindRuntimeCall(WriteHazard, Locals); |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 4006 | } |
| 4007 | |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 4008 | void FragileHazards::emitReadHazard(CGBuilderTy &Builder) { |
| 4009 | assert(!Locals.empty()); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4010 | llvm::CallInst *call = Builder.CreateCall(ReadHazard, Locals); |
| 4011 | call->setDoesNotThrow(); |
| 4012 | call->setCallingConv(CGF.getRuntimeCC()); |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 4013 | } |
| 4014 | |
| 4015 | /// Emit read hazards in all the protected blocks, i.e. all the blocks |
| 4016 | /// which have been inserted since the beginning of the try. |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 4017 | void FragileHazards::emitHazardsInNewBlocks() { |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 4018 | if (Locals.empty()) return; |
| 4019 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4020 | CGBuilderTy Builder(CGF, CGF.getLLVMContext()); |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 4021 | |
| 4022 | // Iterate through all blocks, skipping those prior to the try. |
| 4023 | for (llvm::Function::iterator |
| 4024 | FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) { |
| 4025 | llvm::BasicBlock &BB = *FI; |
| 4026 | if (BlocksBeforeTry.count(&BB)) continue; |
| 4027 | |
| 4028 | // Walk through all the calls in the block. |
| 4029 | for (llvm::BasicBlock::iterator |
| 4030 | BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) { |
| 4031 | llvm::Instruction &I = *BI; |
| 4032 | |
| 4033 | // Ignore instructions that aren't non-intrinsic calls. |
| 4034 | // These are the only calls that can possibly call longjmp. |
| 4035 | if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue; |
| 4036 | if (isa<llvm::IntrinsicInst>(I)) |
| 4037 | continue; |
| 4038 | |
| 4039 | // Ignore call sites marked nounwind. This may be questionable, |
| 4040 | // since 'nounwind' doesn't necessarily mean 'does not call longjmp'. |
| 4041 | llvm::CallSite CS(&I); |
| 4042 | if (CS.doesNotThrow()) continue; |
| 4043 | |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 4044 | // Insert a read hazard before the call. This will ensure that |
| 4045 | // any writes to the locals are performed before making the |
| 4046 | // call. If the call throws, then this is sufficient to |
| 4047 | // guarantee correctness as long as it doesn't also write to any |
| 4048 | // locals. |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 4049 | Builder.SetInsertPoint(&BB, BI); |
| 4050 | emitReadHazard(Builder); |
| 4051 | } |
| 4052 | } |
| 4053 | } |
| 4054 | |
| 4055 | static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) { |
| 4056 | if (V) S.insert(V); |
| 4057 | } |
| 4058 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4059 | static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, Address V) { |
| 4060 | if (V.isValid()) S.insert(V.getPointer()); |
| 4061 | } |
| 4062 | |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 4063 | void FragileHazards::collectLocals() { |
| 4064 | // Compute a set of allocas to ignore. |
| 4065 | llvm::DenseSet<llvm::Value*> AllocasToIgnore; |
| 4066 | addIfPresent(AllocasToIgnore, CGF.ReturnValue); |
| 4067 | addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest); |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 4068 | |
| 4069 | // Collect all the allocas currently in the function. This is |
| 4070 | // probably way too aggressive. |
| 4071 | llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock(); |
| 4072 | for (llvm::BasicBlock::iterator |
| 4073 | I = Entry.begin(), E = Entry.end(); I != E; ++I) |
| 4074 | if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I)) |
| 4075 | Locals.push_back(&*I); |
| 4076 | } |
| 4077 | |
| 4078 | llvm::FunctionType *FragileHazards::GetAsmFnType() { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4079 | SmallVector<llvm::Type *, 16> tys(Locals.size()); |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 4080 | for (unsigned i = 0, e = Locals.size(); i != e; ++i) |
| 4081 | tys[i] = Locals[i]->getType(); |
| 4082 | return llvm::FunctionType::get(CGF.VoidTy, tys, false); |
John McCall | 65bea08 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 4083 | } |
| 4084 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4085 | /* |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 4086 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4087 | Objective-C setjmp-longjmp (sjlj) Exception Handling |
| 4088 | -- |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 4089 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 4090 | A catch buffer is a setjmp buffer plus: |
| 4091 | - a pointer to the exception that was caught |
| 4092 | - a pointer to the previous exception data buffer |
| 4093 | - two pointers of reserved storage |
| 4094 | Therefore catch buffers form a stack, with a pointer to the top |
| 4095 | of the stack kept in thread-local storage. |
| 4096 | |
| 4097 | objc_exception_try_enter pushes a catch buffer onto the EH stack. |
| 4098 | objc_exception_try_exit pops the given catch buffer, which is |
| 4099 | required to be the top of the EH stack. |
| 4100 | objc_exception_throw pops the top of the EH stack, writes the |
| 4101 | thrown exception into the appropriate field, and longjmps |
| 4102 | to the setjmp buffer. It crashes the process (with a printf |
| 4103 | and an abort()) if there are no catch buffers on the stack. |
| 4104 | objc_exception_extract just reads the exception pointer out of the |
| 4105 | catch buffer. |
| 4106 | |
| 4107 | There's no reason an implementation couldn't use a light-weight |
| 4108 | setjmp here --- something like __builtin_setjmp, but API-compatible |
| 4109 | with the heavyweight setjmp. This will be more important if we ever |
| 4110 | want to implement correct ObjC/C++ exception interactions for the |
| 4111 | fragile ABI. |
| 4112 | |
| 4113 | Note that for this use of setjmp/longjmp to be correct, we may need |
| 4114 | to mark some local variables volatile: if a non-volatile local |
| 4115 | variable is modified between the setjmp and the longjmp, it has |
| 4116 | indeterminate value. For the purposes of LLVM IR, it may be |
| 4117 | sufficient to make loads and stores within the @try (to variables |
| 4118 | declared outside the @try) volatile. This is necessary for |
| 4119 | optimized correctness, but is not currently being done; this is |
| 4120 | being tracked as rdar://problem/8160285 |
| 4121 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4122 | The basic framework for a @try-catch-finally is as follows: |
| 4123 | { |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 4124 | objc_exception_data d; |
| 4125 | id _rethrow = null; |
Anders Carlsson | da0e456 | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 4126 | bool _call_try_exit = true; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4127 | |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 4128 | objc_exception_try_enter(&d); |
| 4129 | if (!setjmp(d.jmp_buf)) { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4130 | ... try body ... |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 4131 | } else { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4132 | // exception path |
| 4133 | id _caught = objc_exception_extract(&d); |
| 4134 | |
| 4135 | // enter new try scope for handlers |
| 4136 | if (!setjmp(d.jmp_buf)) { |
| 4137 | ... match exception and execute catch blocks ... |
| 4138 | |
| 4139 | // fell off end, rethrow. |
| 4140 | _rethrow = _caught; |
| 4141 | ... jump-through-finally to finally_rethrow ... |
| 4142 | } else { |
| 4143 | // exception in catch block |
| 4144 | _rethrow = objc_exception_extract(&d); |
| 4145 | _call_try_exit = false; |
| 4146 | ... jump-through-finally to finally_rethrow ... |
| 4147 | } |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 4148 | } |
Daniel Dunbar | 2efd538 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 4149 | ... jump-through-finally to finally_end ... |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 4150 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4151 | finally: |
Anders Carlsson | da0e456 | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 4152 | if (_call_try_exit) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4153 | objc_exception_try_exit(&d); |
Anders Carlsson | da0e456 | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 4154 | |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 4155 | ... finally block .... |
Daniel Dunbar | 2efd538 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 4156 | ... dispatch to finally destination ... |
| 4157 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4158 | finally_rethrow: |
Daniel Dunbar | 2efd538 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 4159 | objc_exception_throw(_rethrow); |
| 4160 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4161 | finally_end: |
| 4162 | } |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 4163 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4164 | This framework differs slightly from the one gcc uses, in that gcc |
| 4165 | uses _rethrow to determine if objc_exception_try_exit should be called |
| 4166 | and if the object should be rethrown. This breaks in the face of |
| 4167 | throwing nil and introduces unnecessary branches. |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 4168 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4169 | We specialize this framework for a few particular circumstances: |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 4170 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4171 | - If there are no catch blocks, then we avoid emitting the second |
| 4172 | exception handling context. |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 4173 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4174 | - If there is a catch-all catch block (i.e. @catch(...) or @catch(id |
| 4175 | e)) we avoid emitting the code to rethrow an uncaught exception. |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 4176 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4177 | - FIXME: If there is no @finally block we can do a few more |
| 4178 | simplifications. |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 4179 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4180 | Rethrows and Jumps-Through-Finally |
| 4181 | -- |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 4182 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 4183 | '@throw;' is supported by pushing the currently-caught exception |
| 4184 | onto ObjCEHStack while the @catch blocks are emitted. |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 4185 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 4186 | Branches through the @finally block are handled with an ordinary |
| 4187 | normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC |
| 4188 | exceptions are not compatible with C++ exceptions, and this is |
| 4189 | hardly the only place where this will go wrong. |
Daniel Dunbar | 2efd538 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 4190 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 4191 | @synchronized(expr) { stmt; } is emitted as if it were: |
| 4192 | id synch_value = expr; |
| 4193 | objc_sync_enter(synch_value); |
| 4194 | @try { stmt; } @finally { objc_sync_exit(synch_value); } |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 4195 | */ |
| 4196 | |
Fariborz Jahanian | c2ad6dc | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 4197 | void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 4198 | const Stmt &S) { |
| 4199 | bool isTry = isa<ObjCAtTryStmt>(S); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 4200 | |
| 4201 | // A destination for the fall-through edges of the catch handlers to |
| 4202 | // jump to. |
| 4203 | CodeGenFunction::JumpDest FinallyEnd = |
| 4204 | CGF.getJumpDestInCurrentScope("finally.end"); |
| 4205 | |
| 4206 | // A destination for the rethrow edge of the catch handlers to jump |
| 4207 | // to. |
| 4208 | CodeGenFunction::JumpDest FinallyRethrow = |
| 4209 | CGF.getJumpDestInCurrentScope("finally.rethrow"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4210 | |
Daniel Dunbar | 94ceb61 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 4211 | // For @synchronized, call objc_sync_enter(sync.expr). The |
| 4212 | // evaluation of the expression must occur before we enter the |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 4213 | // @synchronized. We can't avoid a temp here because we need the |
| 4214 | // value to be preserved. If the backend ever does liveness |
| 4215 | // correctly after setjmp, this will be unnecessary. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4216 | Address SyncArgSlot = Address::invalid(); |
Daniel Dunbar | 94ceb61 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 4217 | if (!isTry) { |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 4218 | llvm::Value *SyncArg = |
Daniel Dunbar | 94ceb61 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 4219 | CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr()); |
| 4220 | SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4221 | CGF.EmitNounwindRuntimeCall(ObjCTypes.getSyncEnterFn(), SyncArg); |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 4222 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4223 | SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), |
| 4224 | CGF.getPointerAlign(), "sync.arg"); |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 4225 | CGF.Builder.CreateStore(SyncArg, SyncArgSlot); |
Daniel Dunbar | 94ceb61 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 4226 | } |
Daniel Dunbar | 2efd538 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 4227 | |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 4228 | // Allocate memory for the setjmp buffer. This needs to be kept |
| 4229 | // live throughout the try and catch blocks. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4230 | Address ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy, |
| 4231 | CGF.getPointerAlign(), |
| 4232 | "exceptiondata.ptr"); |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 4233 | |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 4234 | // Create the fragile hazards. Note that this will not capture any |
| 4235 | // of the allocas required for exception processing, but will |
| 4236 | // capture the current basic block (which extends all the way to the |
| 4237 | // setjmp call) as "before the @try". |
| 4238 | FragileHazards Hazards(CGF); |
| 4239 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 4240 | // Create a flag indicating whether the cleanup needs to call |
| 4241 | // objc_exception_try_exit. This is true except when |
| 4242 | // - no catches match and we're branching through the cleanup |
| 4243 | // just to rethrow the exception, or |
| 4244 | // - a catch matched and we're falling out of the catch handler. |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 4245 | // The setjmp-safety rule here is that we should always store to this |
| 4246 | // variable in a place that dominates the branch through the cleanup |
| 4247 | // without passing through any setjmps. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4248 | Address CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(), |
| 4249 | CharUnits::One(), |
| 4250 | "_call_try_exit"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4251 | |
John McCall | 9916e3f | 2010-10-04 23:42:51 +0000 | [diff] [blame] | 4252 | // A slot containing the exception to rethrow. Only needed when we |
| 4253 | // have both a @catch and a @finally. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4254 | Address PropagatingExnVar = Address::invalid(); |
John McCall | 9916e3f | 2010-10-04 23:42:51 +0000 | [diff] [blame] | 4255 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 4256 | // Push a normal cleanup to leave the try scope. |
John McCall | 638d4f5 | 2013-04-03 00:56:07 +0000 | [diff] [blame] | 4257 | CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalAndEHCleanup, &S, |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 4258 | SyncArgSlot, |
John McCall | cda666c | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 4259 | CallTryExitVar, |
| 4260 | ExceptionData, |
| 4261 | &ObjCTypes); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 4262 | |
| 4263 | // Enter a try block: |
| 4264 | // - Call objc_exception_try_enter to push ExceptionData on top of |
| 4265 | // the EH stack. |
Nico Weber | 6307cf0 | 2015-02-26 20:43:00 +0000 | [diff] [blame] | 4266 | CGF.EmitNounwindRuntimeCall(ObjCTypes.getExceptionTryEnterFn(), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4267 | ExceptionData.getPointer()); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 4268 | |
| 4269 | // - Call setjmp on the exception data buffer. |
| 4270 | llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0); |
| 4271 | llvm::Value *GEPIndexes[] = { Zero, Zero, Zero }; |
David Blaikie | 6b2a830 | 2015-04-03 17:47:16 +0000 | [diff] [blame] | 4272 | llvm::Value *SetJmpBuffer = CGF.Builder.CreateGEP( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4273 | ObjCTypes.ExceptionDataTy, ExceptionData.getPointer(), GEPIndexes, |
| 4274 | "setjmp_buffer"); |
Nico Weber | 6307cf0 | 2015-02-26 20:43:00 +0000 | [diff] [blame] | 4275 | llvm::CallInst *SetJmpResult = CGF.EmitNounwindRuntimeCall( |
| 4276 | ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result"); |
Bill Wendling | bd26cf9 | 2011-12-19 23:53:28 +0000 | [diff] [blame] | 4277 | SetJmpResult->setCanReturnTwice(); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 4278 | |
| 4279 | // If setjmp returned 0, enter the protected block; otherwise, |
| 4280 | // branch to the handler. |
Daniel Dunbar | 75283ff | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 4281 | llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try"); |
| 4282 | llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler"); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 4283 | llvm::Value *DidCatch = |
John McCall | cebe0ca | 2010-08-11 00:16:14 +0000 | [diff] [blame] | 4284 | CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception"); |
| 4285 | CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock); |
Anders Carlsson | 4f1c7c3 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 4286 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 4287 | // Emit the protected block. |
Anders Carlsson | 4f1c7c3 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 4288 | CGF.EmitBlock(TryBlock); |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 4289 | CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4290 | CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody() |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 4291 | : cast<ObjCAtSynchronizedStmt>(S).getSynchBody()); |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 4292 | |
| 4293 | CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4294 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 4295 | // Emit the exception handler block. |
Daniel Dunbar | 7f08678 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 4296 | CGF.EmitBlock(TryHandler); |
Daniel Dunbar | b22ff59 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 4297 | |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 4298 | // Don't optimize loads of the in-scope locals across this point. |
| 4299 | Hazards.emitWriteHazard(); |
| 4300 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 4301 | // For a @synchronized (or a @try with no catches), just branch |
| 4302 | // through the cleanup to the rethrow block. |
| 4303 | if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) { |
| 4304 | // Tell the cleanup not to re-pop the exit. |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 4305 | CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar); |
Anders Carlsson | bfee7e9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 4306 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 4307 | |
| 4308 | // Otherwise, we have to match against the caught exceptions. |
| 4309 | } else { |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 4310 | // Retrieve the exception object. We may emit multiple blocks but |
| 4311 | // nothing can cross this so the value is already in SSA form. |
| 4312 | llvm::CallInst *Caught = |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4313 | CGF.EmitNounwindRuntimeCall(ObjCTypes.getExceptionExtractFn(), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4314 | ExceptionData.getPointer(), "caught"); |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 4315 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 4316 | // Push the exception to rethrow onto the EH value stack for the |
| 4317 | // benefit of any @throws in the handlers. |
| 4318 | CGF.ObjCEHValueStack.push_back(Caught); |
| 4319 | |
Douglas Gregor | 96c7949 | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 4320 | const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4321 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4322 | bool HasFinally = (AtTryStmt->getFinallyStmt() != nullptr); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 4323 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4324 | llvm::BasicBlock *CatchBlock = nullptr; |
| 4325 | llvm::BasicBlock *CatchHandler = nullptr; |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 4326 | if (HasFinally) { |
John McCall | 9916e3f | 2010-10-04 23:42:51 +0000 | [diff] [blame] | 4327 | // Save the currently-propagating exception before |
| 4328 | // objc_exception_try_enter clears the exception slot. |
| 4329 | PropagatingExnVar = CGF.CreateTempAlloca(Caught->getType(), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4330 | CGF.getPointerAlign(), |
John McCall | 9916e3f | 2010-10-04 23:42:51 +0000 | [diff] [blame] | 4331 | "propagating_exception"); |
| 4332 | CGF.Builder.CreateStore(Caught, PropagatingExnVar); |
| 4333 | |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 4334 | // Enter a new exception try block (in case a @catch block |
| 4335 | // throws an exception). |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4336 | CGF.EmitNounwindRuntimeCall(ObjCTypes.getExceptionTryEnterFn(), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4337 | ExceptionData.getPointer()); |
Anders Carlsson | 4f1c7c3 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 4338 | |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 4339 | llvm::CallInst *SetJmpResult = |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4340 | CGF.EmitNounwindRuntimeCall(ObjCTypes.getSetJmpFn(), |
| 4341 | SetJmpBuffer, "setjmp.result"); |
Bill Wendling | bd26cf9 | 2011-12-19 23:53:28 +0000 | [diff] [blame] | 4342 | SetJmpResult->setCanReturnTwice(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4343 | |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 4344 | llvm::Value *Threw = |
| 4345 | CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception"); |
| 4346 | |
| 4347 | CatchBlock = CGF.createBasicBlock("catch"); |
| 4348 | CatchHandler = CGF.createBasicBlock("catch_for_catch"); |
| 4349 | CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock); |
| 4350 | |
| 4351 | CGF.EmitBlock(CatchBlock); |
| 4352 | } |
| 4353 | |
| 4354 | CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4355 | |
Daniel Dunbar | b22ff59 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 4356 | // Handle catch list. As a special case we check if everything is |
| 4357 | // matched and avoid generating code for falling off the end if |
| 4358 | // so. |
| 4359 | bool AllMatched = false; |
Douglas Gregor | 96c7949 | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 4360 | for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) { |
| 4361 | const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I); |
Anders Carlsson | 4f1c7c3 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 4362 | |
Douglas Gregor | 46a572b | 2010-04-26 16:46:50 +0000 | [diff] [blame] | 4363 | const VarDecl *CatchParam = CatchStmt->getCatchParamDecl(); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4364 | const ObjCObjectPointerType *OPT = nullptr; |
Daniel Dunbar | 523208f | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 4365 | |
Anders Carlsson | 4f1c7c3 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 4366 | // catch(...) always matches. |
Daniel Dunbar | b22ff59 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 4367 | if (!CatchParam) { |
| 4368 | AllMatched = true; |
| 4369 | } else { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4370 | OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4371 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 4372 | // catch(id e) always matches under this ABI, since only |
| 4373 | // ObjC exceptions end up here in the first place. |
Daniel Dunbar | 86919f4 | 2008-09-27 22:21:14 +0000 | [diff] [blame] | 4374 | // FIXME: For the time being we also match id<X>; this should |
| 4375 | // be rejected by Sema instead. |
Eli Friedman | 55179ca | 2009-07-11 00:57:02 +0000 | [diff] [blame] | 4376 | if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType())) |
Daniel Dunbar | b22ff59 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 4377 | AllMatched = true; |
Anders Carlsson | 4f1c7c3 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 4378 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4379 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 4380 | // If this is a catch-all, we don't need to test anything. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4381 | if (AllMatched) { |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 4382 | CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF); |
| 4383 | |
Anders Carlsson | 9396a89 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 4384 | if (CatchParam) { |
John McCall | 1c9c3fd | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 4385 | CGF.EmitAutoVarDecl(*CatchParam); |
Daniel Dunbar | 5c7e393 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 4386 | assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 4387 | |
| 4388 | // These types work out because ConvertType(id) == i8*. |
John McCall | 17f0275 | 2015-10-30 00:56:02 +0000 | [diff] [blame] | 4389 | EmitInitOfCatchParam(CGF, Caught, CatchParam); |
Anders Carlsson | 9396a89 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 4390 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4391 | |
Anders Carlsson | 9396a89 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 4392 | CGF.EmitStmt(CatchStmt->getCatchBody()); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 4393 | |
| 4394 | // The scope of the catch variable ends right here. |
| 4395 | CatchVarCleanups.ForceCleanup(); |
| 4396 | |
Anders Carlsson | bfee7e9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 4397 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Anders Carlsson | 4f1c7c3 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 4398 | break; |
| 4399 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4400 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4401 | assert(OPT && "Unexpected non-object pointer type in @catch"); |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 4402 | const ObjCObjectType *ObjTy = OPT->getObjectType(); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 4403 | |
| 4404 | // FIXME: @catch (Class c) ? |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 4405 | ObjCInterfaceDecl *IDecl = ObjTy->getInterface(); |
| 4406 | assert(IDecl && "Catch parameter must have Objective-C type!"); |
Anders Carlsson | 4f1c7c3 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 4407 | |
| 4408 | // Check if the @catch block matches the exception object. |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4409 | llvm::Value *Class = EmitClassRef(CGF, IDecl); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4410 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4411 | llvm::Value *matchArgs[] = { Class, Caught }; |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 4412 | llvm::CallInst *Match = |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4413 | CGF.EmitNounwindRuntimeCall(ObjCTypes.getExceptionMatchFn(), |
| 4414 | matchArgs, "match"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4415 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 4416 | llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match"); |
| 4417 | llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4418 | |
| 4419 | CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"), |
Daniel Dunbar | 7f08678 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 4420 | MatchedBlock, NextCatchBlock); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4421 | |
Anders Carlsson | 4f1c7c3 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 4422 | // Emit the @catch block. |
| 4423 | CGF.EmitBlock(MatchedBlock); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 4424 | |
| 4425 | // Collect any cleanups for the catch variable. The scope lasts until |
| 4426 | // the end of the catch body. |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 4427 | CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 4428 | |
John McCall | 1c9c3fd | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 4429 | CGF.EmitAutoVarDecl(*CatchParam); |
Daniel Dunbar | 5c7e393 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 4430 | assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 4431 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 4432 | // Initialize the catch variable. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4433 | llvm::Value *Tmp = |
| 4434 | CGF.Builder.CreateBitCast(Caught, |
Benjamin Kramer | 76399eb | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 4435 | CGF.ConvertType(CatchParam->getType())); |
John McCall | 17f0275 | 2015-10-30 00:56:02 +0000 | [diff] [blame] | 4436 | EmitInitOfCatchParam(CGF, Tmp, CatchParam); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4437 | |
Anders Carlsson | 9396a89 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 4438 | CGF.EmitStmt(CatchStmt->getCatchBody()); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 4439 | |
| 4440 | // We're done with the catch variable. |
| 4441 | CatchVarCleanups.ForceCleanup(); |
| 4442 | |
Anders Carlsson | bfee7e9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 4443 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4444 | |
Anders Carlsson | 4f1c7c3 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 4445 | CGF.EmitBlock(NextCatchBlock); |
| 4446 | } |
| 4447 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 4448 | CGF.ObjCEHValueStack.pop_back(); |
| 4449 | |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 4450 | // If nothing wanted anything to do with the caught exception, |
| 4451 | // kill the extract call. |
| 4452 | if (Caught->use_empty()) |
| 4453 | Caught->eraseFromParent(); |
| 4454 | |
| 4455 | if (!AllMatched) |
| 4456 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 4457 | |
| 4458 | if (HasFinally) { |
| 4459 | // Emit the exception handler for the @catch blocks. |
| 4460 | CGF.EmitBlock(CatchHandler); |
| 4461 | |
| 4462 | // In theory we might now need a write hazard, but actually it's |
| 4463 | // unnecessary because there's no local-accessing code between |
| 4464 | // the try's write hazard and here. |
| 4465 | //Hazards.emitWriteHazard(); |
| 4466 | |
John McCall | 9916e3f | 2010-10-04 23:42:51 +0000 | [diff] [blame] | 4467 | // Extract the new exception and save it to the |
| 4468 | // propagating-exception slot. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4469 | assert(PropagatingExnVar.isValid()); |
John McCall | 9916e3f | 2010-10-04 23:42:51 +0000 | [diff] [blame] | 4470 | llvm::CallInst *NewCaught = |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4471 | CGF.EmitNounwindRuntimeCall(ObjCTypes.getExceptionExtractFn(), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4472 | ExceptionData.getPointer(), "caught"); |
John McCall | 9916e3f | 2010-10-04 23:42:51 +0000 | [diff] [blame] | 4473 | CGF.Builder.CreateStore(NewCaught, PropagatingExnVar); |
| 4474 | |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 4475 | // Don't pop the catch handler; the throw already did. |
| 4476 | CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar); |
Anders Carlsson | bfee7e9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 4477 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Daniel Dunbar | b22ff59 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 4478 | } |
Anders Carlsson | 4f1c7c3 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 4479 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4480 | |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 4481 | // Insert read hazards as required in the new blocks. |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 4482 | Hazards.emitHazardsInNewBlocks(); |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 4483 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 4484 | // Pop the cleanup. |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 4485 | CGF.Builder.restoreIP(TryFallthroughIP); |
| 4486 | if (CGF.HaveInsertPoint()) |
| 4487 | CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 4488 | CGF.PopCleanupBlock(); |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 4489 | CGF.EmitBlock(FinallyEnd.getBlock(), true); |
Anders Carlsson | bfee7e9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 4490 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 4491 | // Emit the rethrow block. |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 4492 | CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP(); |
John McCall | ad5d61e | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 4493 | CGF.EmitBlock(FinallyRethrow.getBlock(), true); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 4494 | if (CGF.HaveInsertPoint()) { |
John McCall | 9916e3f | 2010-10-04 23:42:51 +0000 | [diff] [blame] | 4495 | // If we have a propagating-exception variable, check it. |
| 4496 | llvm::Value *PropagatingExn; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4497 | if (PropagatingExnVar.isValid()) { |
John McCall | 9916e3f | 2010-10-04 23:42:51 +0000 | [diff] [blame] | 4498 | PropagatingExn = CGF.Builder.CreateLoad(PropagatingExnVar); |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 4499 | |
John McCall | 9916e3f | 2010-10-04 23:42:51 +0000 | [diff] [blame] | 4500 | // Otherwise, just look in the buffer for the exception to throw. |
| 4501 | } else { |
| 4502 | llvm::CallInst *Caught = |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4503 | CGF.EmitNounwindRuntimeCall(ObjCTypes.getExceptionExtractFn(), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4504 | ExceptionData.getPointer()); |
John McCall | 9916e3f | 2010-10-04 23:42:51 +0000 | [diff] [blame] | 4505 | PropagatingExn = Caught; |
| 4506 | } |
| 4507 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4508 | CGF.EmitNounwindRuntimeCall(ObjCTypes.getExceptionThrowFn(), |
| 4509 | PropagatingExn); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 4510 | CGF.Builder.CreateUnreachable(); |
Fariborz Jahanian | e2caaaa | 2008-11-21 19:21:53 +0000 | [diff] [blame] | 4511 | } |
Anders Carlsson | 4f1c7c3 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 4512 | |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 4513 | CGF.Builder.restoreIP(SavedIP); |
Anders Carlsson | 1963b0c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 4514 | } |
| 4515 | |
| 4516 | void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 1eab052 | 2013-01-10 19:02:56 +0000 | [diff] [blame] | 4517 | const ObjCAtThrowStmt &S, |
| 4518 | bool ClearInsertionPoint) { |
Anders Carlsson | e005aa1 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 4519 | llvm::Value *ExceptionAsObject; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4520 | |
Anders Carlsson | e005aa1 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 4521 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
John McCall | 248512a | 2011-10-01 10:32:24 +0000 | [diff] [blame] | 4522 | llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4523 | ExceptionAsObject = |
Benjamin Kramer | 76399eb | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 4524 | CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy); |
Anders Carlsson | e005aa1 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 4525 | } else { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4526 | assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) && |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 4527 | "Unexpected rethrow outside @catch block."); |
Anders Carlsson | bf8a1be | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 4528 | ExceptionAsObject = CGF.ObjCEHValueStack.back(); |
Anders Carlsson | e005aa1 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 4529 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4530 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4531 | CGF.EmitRuntimeCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject) |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 4532 | ->setDoesNotReturn(); |
Anders Carlsson | 4f1c7c3 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 4533 | CGF.Builder.CreateUnreachable(); |
Daniel Dunbar | 5c7e393 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 4534 | |
| 4535 | // Clear the insertion point to indicate we are in unreachable code. |
Fariborz Jahanian | 1eab052 | 2013-01-10 19:02:56 +0000 | [diff] [blame] | 4536 | if (ClearInsertionPoint) |
| 4537 | CGF.Builder.ClearInsertionPoint(); |
Anders Carlsson | 1963b0c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 4538 | } |
| 4539 | |
Fariborz Jahanian | 83f45b55 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 4540 | /// EmitObjCWeakRead - Code gen for loading value of a __weak |
Fariborz Jahanian | f5125d1 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 4541 | /// object: objc_read_weak (id *src) |
| 4542 | /// |
Fariborz Jahanian | 83f45b55 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 4543 | llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4544 | Address AddrWeakObj) { |
| 4545 | llvm::Type* DestTy = AddrWeakObj.getElementType(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4546 | AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, |
| 4547 | ObjCTypes.PtrObjectPtrTy); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4548 | llvm::Value *read_weak = |
| 4549 | CGF.EmitNounwindRuntimeCall(ObjCTypes.getGcReadWeakFn(), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4550 | AddrWeakObj.getPointer(), "weakread"); |
Eli Friedman | a374b68 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 4551 | read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy); |
Fariborz Jahanian | f5125d1 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 4552 | return read_weak; |
| 4553 | } |
| 4554 | |
Fariborz Jahanian | 83f45b55 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 4555 | /// EmitObjCWeakAssign - Code gen for assigning to a __weak object. |
| 4556 | /// objc_assign_weak (id src, id *dst) |
| 4557 | /// |
| 4558 | void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4559 | llvm::Value *src, Address dst) { |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 4560 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 4561 | if (!isa<llvm::PointerType>(SrcTy)) { |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 4562 | unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 4563 | assert(Size <= 8 && "does not support size > 8"); |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 4564 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, CGM.Int32Ty) |
| 4565 | : CGF.Builder.CreateBitCast(src, CGM.Int64Ty); |
Fariborz Jahanian | 1b074a3 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 4566 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 4567 | } |
Fariborz Jahanian | 50a1270 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 4568 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 4569 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4570 | llvm::Value *args[] = { src, dst.getPointer() }; |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4571 | CGF.EmitNounwindRuntimeCall(ObjCTypes.getGcAssignWeakFn(), |
| 4572 | args, "weakassign"); |
Fariborz Jahanian | 83f45b55 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 4573 | } |
| 4574 | |
Fariborz Jahanian | d7db964 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 4575 | /// EmitObjCGlobalAssign - Code gen for assigning to a __strong object. |
| 4576 | /// objc_assign_global (id src, id *dst) |
| 4577 | /// |
| 4578 | void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4579 | llvm::Value *src, Address dst, |
Fariborz Jahanian | 217af24 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 4580 | bool threadlocal) { |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 4581 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 4582 | if (!isa<llvm::PointerType>(SrcTy)) { |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 4583 | unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 4584 | assert(Size <= 8 && "does not support size > 8"); |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 4585 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, CGM.Int32Ty) |
| 4586 | : CGF.Builder.CreateBitCast(src, CGM.Int64Ty); |
Fariborz Jahanian | 1b074a3 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 4587 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 4588 | } |
Fariborz Jahanian | 50a1270 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 4589 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 4590 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4591 | llvm::Value *args[] = { src, dst.getPointer() }; |
Fariborz Jahanian | 217af24 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 4592 | if (!threadlocal) |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4593 | CGF.EmitNounwindRuntimeCall(ObjCTypes.getGcAssignGlobalFn(), |
| 4594 | args, "globalassign"); |
Fariborz Jahanian | 217af24 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 4595 | else |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4596 | CGF.EmitNounwindRuntimeCall(ObjCTypes.getGcAssignThreadLocalFn(), |
| 4597 | args, "threadlocalassign"); |
Fariborz Jahanian | d7db964 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 4598 | } |
| 4599 | |
Fariborz Jahanian | e881b53 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 4600 | /// EmitObjCIvarAssign - Code gen for assigning to a __strong object. |
Fariborz Jahanian | 7a95d72 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 4601 | /// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset) |
Fariborz Jahanian | e881b53 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 4602 | /// |
| 4603 | void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4604 | llvm::Value *src, Address dst, |
Fariborz Jahanian | 7a95d72 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 4605 | llvm::Value *ivarOffset) { |
| 4606 | assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 4607 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 4608 | if (!isa<llvm::PointerType>(SrcTy)) { |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 4609 | unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 4610 | assert(Size <= 8 && "does not support size > 8"); |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 4611 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, CGM.Int32Ty) |
| 4612 | : CGF.Builder.CreateBitCast(src, CGM.Int64Ty); |
Fariborz Jahanian | 1b074a3 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 4613 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 4614 | } |
Fariborz Jahanian | e881b53 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 4615 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 4616 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4617 | llvm::Value *args[] = { src, dst.getPointer(), ivarOffset }; |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4618 | CGF.EmitNounwindRuntimeCall(ObjCTypes.getGcAssignIvarFn(), args); |
Fariborz Jahanian | e881b53 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 4619 | } |
| 4620 | |
Fariborz Jahanian | d7db964 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 4621 | /// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object. |
| 4622 | /// objc_assign_strongCast (id src, id *dst) |
| 4623 | /// |
| 4624 | void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4625 | llvm::Value *src, Address dst) { |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 4626 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 4627 | if (!isa<llvm::PointerType>(SrcTy)) { |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 4628 | unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 4629 | assert(Size <= 8 && "does not support size > 8"); |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 4630 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, CGM.Int32Ty) |
| 4631 | : CGF.Builder.CreateBitCast(src, CGM.Int64Ty); |
Fariborz Jahanian | 1b074a3 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 4632 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 4633 | } |
Fariborz Jahanian | 50a1270 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 4634 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 4635 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4636 | llvm::Value *args[] = { src, dst.getPointer() }; |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4637 | CGF.EmitNounwindRuntimeCall(ObjCTypes.getGcAssignStrongCastFn(), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4638 | args, "strongassign"); |
Fariborz Jahanian | d7db964 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 4639 | } |
| 4640 | |
Fariborz Jahanian | 5f21d2f | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 4641 | void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4642 | Address DestPtr, |
| 4643 | Address SrcPtr, |
Fariborz Jahanian | 021510e | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 4644 | llvm::Value *size) { |
Fariborz Jahanian | 5f21d2f | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 4645 | SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy); |
| 4646 | DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4647 | llvm::Value *args[] = { DestPtr.getPointer(), SrcPtr.getPointer(), size }; |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4648 | CGF.EmitNounwindRuntimeCall(ObjCTypes.GcMemmoveCollectableFn(), args); |
Fariborz Jahanian | 5f21d2f | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 4649 | } |
| 4650 | |
Fariborz Jahanian | 9f84b78 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 4651 | /// EmitObjCValueForIvar - Code Gen for ivar reference. |
| 4652 | /// |
Fariborz Jahanian | 712bfa6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 4653 | LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 4654 | QualType ObjectTy, |
| 4655 | llvm::Value *BaseValue, |
| 4656 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 712bfa6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 4657 | unsigned CVRQualifiers) { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4658 | const ObjCInterfaceDecl *ID = |
| 4659 | ObjectTy->getAs<ObjCObjectType>()->getInterface(); |
Daniel Dunbar | 9fd114d | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 4660 | return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers, |
| 4661 | EmitIvarOffset(CGF, ID, Ivar)); |
Fariborz Jahanian | 9f84b78 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 4662 | } |
| 4663 | |
Fariborz Jahanian | 21fc74c | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 4664 | llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 722f424 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 4665 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | 21fc74c | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 4666 | const ObjCIvarDecl *Ivar) { |
Eli Friedman | 8cbca20 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4667 | uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar); |
| 4668 | return llvm::ConstantInt::get( |
| 4669 | CGM.getTypes().ConvertType(CGM.getContext().LongTy), |
| 4670 | Offset); |
Fariborz Jahanian | 21fc74c | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 4671 | } |
| 4672 | |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 4673 | /* *** Private Interface *** */ |
| 4674 | |
| 4675 | /// EmitImageInfo - Emit the image info marker used to encode some module |
| 4676 | /// level information. |
| 4677 | /// |
| 4678 | /// See: <rdr://4810609&4810587&4810587> |
| 4679 | /// struct IMAGE_INFO { |
| 4680 | /// unsigned version; |
| 4681 | /// unsigned flags; |
| 4682 | /// }; |
| 4683 | enum ImageInfoFlags { |
Fariborz Jahanian | 39c17a8 | 2014-01-14 22:01:08 +0000 | [diff] [blame] | 4684 | eImageInfo_FixAndContinue = (1 << 0), // This flag is no longer set by clang. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4685 | eImageInfo_GarbageCollected = (1 << 1), |
| 4686 | eImageInfo_GCOnly = (1 << 2), |
Fariborz Jahanian | 39c17a8 | 2014-01-14 22:01:08 +0000 | [diff] [blame] | 4687 | eImageInfo_OptimizedByDyld = (1 << 3), // This flag is set by the dyld shared cache. |
Daniel Dunbar | 75e909f | 2009-04-20 07:11:47 +0000 | [diff] [blame] | 4688 | |
Daniel Dunbar | 5e63927 | 2010-04-25 20:39:01 +0000 | [diff] [blame] | 4689 | // A flag indicating that the module has no instances of a @synthesize of a |
| 4690 | // superclass variable. <rdar://problem/6803242> |
Fariborz Jahanian | 39c17a8 | 2014-01-14 22:01:08 +0000 | [diff] [blame] | 4691 | eImageInfo_CorrectedSynthesize = (1 << 4), // This flag is no longer set by clang. |
Manman Ren | 96df0b3 | 2016-01-29 23:45:01 +0000 | [diff] [blame] | 4692 | eImageInfo_ImageIsSimulated = (1 << 5), |
| 4693 | eImageInfo_ClassProperties = (1 << 6) |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 4694 | }; |
| 4695 | |
Daniel Dunbar | 5e63927 | 2010-04-25 20:39:01 +0000 | [diff] [blame] | 4696 | void CGObjCCommonMac::EmitImageInfo() { |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 4697 | unsigned version = 0; // Version is unused? |
Bill Wendling | b6f795e | 2012-02-16 01:13:30 +0000 | [diff] [blame] | 4698 | const char *Section = (ObjCABI == 1) ? |
| 4699 | "__OBJC, __image_info,regular" : |
| 4700 | "__DATA, __objc_imageinfo, regular, no_dead_strip"; |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 4701 | |
Bill Wendling | b6f795e | 2012-02-16 01:13:30 +0000 | [diff] [blame] | 4702 | // Generate module-level named metadata to convey this information to the |
| 4703 | // linker and code-gen. |
| 4704 | llvm::Module &Mod = CGM.getModule(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4705 | |
Bill Wendling | b6f795e | 2012-02-16 01:13:30 +0000 | [diff] [blame] | 4706 | // Add the ObjC ABI version to the module flags. |
| 4707 | Mod.addModuleFlag(llvm::Module::Error, "Objective-C Version", ObjCABI); |
| 4708 | Mod.addModuleFlag(llvm::Module::Error, "Objective-C Image Info Version", |
| 4709 | version); |
| 4710 | Mod.addModuleFlag(llvm::Module::Error, "Objective-C Image Info Section", |
| 4711 | llvm::MDString::get(VMContext,Section)); |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 4712 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4713 | if (CGM.getLangOpts().getGC() == LangOptions::NonGC) { |
Bill Wendling | b6f795e | 2012-02-16 01:13:30 +0000 | [diff] [blame] | 4714 | // Non-GC overrides those files which specify GC. |
| 4715 | Mod.addModuleFlag(llvm::Module::Override, |
| 4716 | "Objective-C Garbage Collection", (uint32_t)0); |
| 4717 | } else { |
| 4718 | // Add the ObjC garbage collection value. |
| 4719 | Mod.addModuleFlag(llvm::Module::Error, |
| 4720 | "Objective-C Garbage Collection", |
| 4721 | eImageInfo_GarbageCollected); |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 4722 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4723 | if (CGM.getLangOpts().getGC() == LangOptions::GCOnly) { |
Bill Wendling | b6f795e | 2012-02-16 01:13:30 +0000 | [diff] [blame] | 4724 | // Add the ObjC GC Only value. |
| 4725 | Mod.addModuleFlag(llvm::Module::Error, "Objective-C GC Only", |
| 4726 | eImageInfo_GCOnly); |
| 4727 | |
| 4728 | // Require that GC be specified and set to eImageInfo_GarbageCollected. |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 4729 | llvm::Metadata *Ops[2] = { |
| 4730 | llvm::MDString::get(VMContext, "Objective-C Garbage Collection"), |
| 4731 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::get( |
| 4732 | llvm::Type::getInt32Ty(VMContext), eImageInfo_GarbageCollected))}; |
Bill Wendling | b6f795e | 2012-02-16 01:13:30 +0000 | [diff] [blame] | 4733 | Mod.addModuleFlag(llvm::Module::Require, "Objective-C GC Only", |
| 4734 | llvm::MDNode::get(VMContext, Ops)); |
| 4735 | } |
| 4736 | } |
Bill Wendling | 1e60a2c | 2012-04-24 11:04:57 +0000 | [diff] [blame] | 4737 | |
| 4738 | // Indicate whether we're compiling this to run on a simulator. |
| 4739 | const llvm::Triple &Triple = CGM.getTarget().getTriple(); |
Tim Northover | 756447a | 2015-10-30 16:30:36 +0000 | [diff] [blame] | 4740 | if ((Triple.isiOS() || Triple.isWatchOS()) && |
Bill Wendling | 1e60a2c | 2012-04-24 11:04:57 +0000 | [diff] [blame] | 4741 | (Triple.getArch() == llvm::Triple::x86 || |
| 4742 | Triple.getArch() == llvm::Triple::x86_64)) |
| 4743 | Mod.addModuleFlag(llvm::Module::Error, "Objective-C Is Simulated", |
| 4744 | eImageInfo_ImageIsSimulated); |
Manman Ren | 96df0b3 | 2016-01-29 23:45:01 +0000 | [diff] [blame] | 4745 | |
| 4746 | // Indicate whether we are generating class properties. |
| 4747 | Mod.addModuleFlag(llvm::Module::Error, "Objective-C Class Properties", |
| 4748 | eImageInfo_ClassProperties); |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 4749 | } |
| 4750 | |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4751 | // struct objc_module { |
| 4752 | // unsigned long version; |
| 4753 | // unsigned long size; |
| 4754 | // const char *name; |
| 4755 | // Symtab symtab; |
| 4756 | // }; |
| 4757 | |
| 4758 | // FIXME: Get from somewhere |
| 4759 | static const int ModuleVersion = 7; |
| 4760 | |
| 4761 | void CGObjCMac::EmitModuleInfo() { |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 4762 | uint64_t Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ModuleTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4763 | |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 4764 | ConstantInitBuilder builder(CGM); |
| 4765 | auto values = builder.beginStruct(ObjCTypes.ModuleTy); |
| 4766 | values.addInt(ObjCTypes.LongTy, ModuleVersion); |
| 4767 | values.addInt(ObjCTypes.LongTy, Size); |
| 4768 | // This used to be the filename, now it is unused. <rdr://4327263> |
| 4769 | values.add(GetClassName(StringRef(""))); |
| 4770 | values.add(EmitModuleSymbols()); |
| 4771 | CreateMetadataVar("OBJC_MODULES", values, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4772 | "__OBJC,__module_info,regular,no_dead_strip", |
| 4773 | CGM.getPointerAlign(), true); |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4774 | } |
| 4775 | |
| 4776 | llvm::Constant *CGObjCMac::EmitModuleSymbols() { |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4777 | unsigned NumClasses = DefinedClasses.size(); |
| 4778 | unsigned NumCategories = DefinedCategories.size(); |
| 4779 | |
Daniel Dunbar | c61d0e9 | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 4780 | // Return null if no symbols were defined. |
| 4781 | if (!NumClasses && !NumCategories) |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4782 | return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy); |
Daniel Dunbar | c61d0e9 | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 4783 | |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 4784 | ConstantInitBuilder builder(CGM); |
| 4785 | auto values = builder.beginStruct(); |
| 4786 | values.addInt(ObjCTypes.LongTy, 0); |
| 4787 | values.addNullPointer(ObjCTypes.SelectorPtrTy); |
| 4788 | values.addInt(ObjCTypes.ShortTy, NumClasses); |
| 4789 | values.addInt(ObjCTypes.ShortTy, NumCategories); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4790 | |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 4791 | // The runtime expects exactly the list of defined classes followed |
| 4792 | // by the list of defined categories, in a single array. |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 4793 | auto array = values.beginArray(ObjCTypes.Int8PtrTy); |
Fariborz Jahanian | f322f2f | 2014-03-11 00:25:05 +0000 | [diff] [blame] | 4794 | for (unsigned i=0; i<NumClasses; i++) { |
| 4795 | const ObjCInterfaceDecl *ID = ImplementedClasses[i]; |
| 4796 | assert(ID); |
| 4797 | if (ObjCImplementationDecl *IMP = ID->getImplementation()) |
| 4798 | // We are implementing a weak imported interface. Give it external linkage |
| 4799 | if (ID->isWeakImported() && !IMP->isWeakImported()) |
| 4800 | DefinedClasses[i]->setLinkage(llvm::GlobalVariable::ExternalLinkage); |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 4801 | |
| 4802 | array.addBitCast(DefinedClasses[i], ObjCTypes.Int8PtrTy); |
Fariborz Jahanian | f322f2f | 2014-03-11 00:25:05 +0000 | [diff] [blame] | 4803 | } |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 4804 | for (unsigned i=0; i<NumCategories; i++) |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 4805 | array.addBitCast(DefinedCategories[i], ObjCTypes.Int8PtrTy); |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 4806 | |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 4807 | array.finishAndAddTo(values); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4808 | |
Rafael Espindola | 8b27bdb | 2014-11-06 13:30:38 +0000 | [diff] [blame] | 4809 | llvm::GlobalVariable *GV = CreateMetadataVar( |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 4810 | "OBJC_SYMBOLS", values, "__OBJC,__symbols,regular,no_dead_strip", |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4811 | CGM.getPointerAlign(), true); |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 4812 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4813 | } |
| 4814 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4815 | llvm::Value *CGObjCMac::EmitClassRefFromId(CodeGenFunction &CGF, |
| 4816 | IdentifierInfo *II) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4817 | LazySymbols.insert(II); |
| 4818 | |
| 4819 | llvm::GlobalVariable *&Entry = ClassReferences[II]; |
| 4820 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4821 | if (!Entry) { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4822 | llvm::Constant *Casted = |
Fariborz Jahanian | 451b92a | 2014-07-16 16:16:04 +0000 | [diff] [blame] | 4823 | llvm::ConstantExpr::getBitCast(GetClassName(II->getName()), |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4824 | ObjCTypes.ClassPtrTy); |
Rafael Espindola | 8b27bdb | 2014-11-06 13:30:38 +0000 | [diff] [blame] | 4825 | Entry = CreateMetadataVar( |
| 4826 | "OBJC_CLASS_REFERENCES_", Casted, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4827 | "__OBJC,__cls_refs,literal_pointers,no_dead_strip", |
| 4828 | CGM.getPointerAlign(), true); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4829 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4830 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4831 | return CGF.Builder.CreateAlignedLoad(Entry, CGF.getPointerAlign()); |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4832 | } |
| 4833 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4834 | llvm::Value *CGObjCMac::EmitClassRef(CodeGenFunction &CGF, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4835 | const ObjCInterfaceDecl *ID) { |
Douglas Gregor | 24ae22c | 2016-04-01 23:23:52 +0000 | [diff] [blame] | 4836 | // If the class has the objc_runtime_visible attribute, we need to |
| 4837 | // use the Objective-C runtime to get the class. |
| 4838 | if (ID->hasAttr<ObjCRuntimeVisibleAttr>()) |
| 4839 | return EmitClassRefViaRuntime(CGF, ID, ObjCTypes); |
| 4840 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4841 | return EmitClassRefFromId(CGF, ID->getIdentifier()); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4842 | } |
| 4843 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4844 | llvm::Value *CGObjCMac::EmitNSAutoreleasePoolClassRef(CodeGenFunction &CGF) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4845 | IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool"); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4846 | return EmitClassRefFromId(CGF, II); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4847 | } |
| 4848 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4849 | llvm::Value *CGObjCMac::EmitSelector(CodeGenFunction &CGF, Selector Sel) { |
| 4850 | return CGF.Builder.CreateLoad(EmitSelectorAddr(CGF, Sel)); |
| 4851 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4852 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4853 | Address CGObjCMac::EmitSelectorAddr(CodeGenFunction &CGF, Selector Sel) { |
| 4854 | CharUnits Align = CGF.getPointerAlign(); |
| 4855 | |
| 4856 | llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; |
Daniel Dunbar | cb515c8 | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 4857 | if (!Entry) { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4858 | llvm::Constant *Casted = |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 4859 | llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), |
Daniel Dunbar | cb515c8 | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 4860 | ObjCTypes.SelectorPtrTy); |
Rafael Espindola | 8b27bdb | 2014-11-06 13:30:38 +0000 | [diff] [blame] | 4861 | Entry = CreateMetadataVar( |
| 4862 | "OBJC_SELECTOR_REFERENCES_", Casted, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4863 | "__OBJC,__message_refs,literal_pointers,no_dead_strip", Align, true); |
Michael Gottesman | 5c20596 | 2013-02-05 23:08:45 +0000 | [diff] [blame] | 4864 | Entry->setExternallyInitialized(true); |
Daniel Dunbar | cb515c8 | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 4865 | } |
| 4866 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4867 | return Address(Entry, Align); |
Daniel Dunbar | cb515c8 | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 4868 | } |
| 4869 | |
Fariborz Jahanian | 451b92a | 2014-07-16 16:16:04 +0000 | [diff] [blame] | 4870 | llvm::Constant *CGObjCCommonMac::GetClassName(StringRef RuntimeName) { |
| 4871 | llvm::GlobalVariable *&Entry = ClassNames[RuntimeName]; |
| 4872 | if (!Entry) |
Saleem Abdulrasool | 271106c | 2016-09-16 23:41:13 +0000 | [diff] [blame] | 4873 | Entry = CreateCStringLiteral(RuntimeName, ObjCLabelType::ClassName); |
Fariborz Jahanian | 451b92a | 2014-07-16 16:16:04 +0000 | [diff] [blame] | 4874 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4875 | } |
| 4876 | |
Argyrios Kyrtzidis | 13257c5 | 2010-08-09 10:54:20 +0000 | [diff] [blame] | 4877 | llvm::Function *CGObjCCommonMac::GetMethodDefinition(const ObjCMethodDecl *MD) { |
| 4878 | llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*>::iterator |
| 4879 | I = MethodDefinitions.find(MD); |
| 4880 | if (I != MethodDefinitions.end()) |
| 4881 | return I->second; |
| 4882 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4883 | return nullptr; |
Argyrios Kyrtzidis | 13257c5 | 2010-08-09 10:54:20 +0000 | [diff] [blame] | 4884 | } |
| 4885 | |
Fariborz Jahanian | 01dff42 | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 4886 | /// GetIvarLayoutName - Returns a unique constant for the given |
| 4887 | /// ivar layout bitmap. |
| 4888 | llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4889 | const ObjCCommonTypesHelper &ObjCTypes) { |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4890 | return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Fariborz Jahanian | 01dff42 | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 4891 | } |
| 4892 | |
John McCall | 3fd13f06 | 2015-10-21 18:06:47 +0000 | [diff] [blame] | 4893 | void IvarLayoutBuilder::visitRecord(const RecordType *RT, |
| 4894 | CharUnits offset) { |
Daniel Dunbar | 15bd888 | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 4895 | const RecordDecl *RD = RT->getDecl(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4896 | |
John McCall | 3fd13f06 | 2015-10-21 18:06:47 +0000 | [diff] [blame] | 4897 | // If this is a union, remember that we had one, because it might mess |
| 4898 | // up the ordering of layout entries. |
| 4899 | if (RD->isUnion()) |
| 4900 | IsDisordered = true; |
| 4901 | |
| 4902 | const ASTRecordLayout *recLayout = nullptr; |
| 4903 | visitAggregate(RD->field_begin(), RD->field_end(), offset, |
| 4904 | [&](const FieldDecl *field) -> CharUnits { |
| 4905 | if (!recLayout) |
| 4906 | recLayout = &CGM.getContext().getASTRecordLayout(RD); |
| 4907 | auto offsetInBits = recLayout->getFieldOffset(field->getFieldIndex()); |
| 4908 | return CGM.getContext().toCharUnitsFromBits(offsetInBits); |
| 4909 | }); |
Daniel Dunbar | 15bd888 | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 4910 | } |
| 4911 | |
John McCall | 3fd13f06 | 2015-10-21 18:06:47 +0000 | [diff] [blame] | 4912 | template <class Iterator, class GetOffsetFn> |
| 4913 | void IvarLayoutBuilder::visitAggregate(Iterator begin, Iterator end, |
| 4914 | CharUnits aggregateOffset, |
| 4915 | const GetOffsetFn &getOffset) { |
| 4916 | for (; begin != end; ++begin) { |
| 4917 | auto field = *begin; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4918 | |
John McCall | 3fd13f06 | 2015-10-21 18:06:47 +0000 | [diff] [blame] | 4919 | // Skip over bitfields. |
| 4920 | if (field->isBitField()) { |
| 4921 | continue; |
| 4922 | } |
| 4923 | |
| 4924 | // Compute the offset of the field within the aggregate. |
| 4925 | CharUnits fieldOffset = aggregateOffset + getOffset(field); |
| 4926 | |
| 4927 | visitField(field, fieldOffset); |
| 4928 | } |
| 4929 | } |
| 4930 | |
| 4931 | /// Collect layout information for the given fields into IvarsInfo. |
| 4932 | void IvarLayoutBuilder::visitField(const FieldDecl *field, |
| 4933 | CharUnits fieldOffset) { |
| 4934 | QualType fieldType = field->getType(); |
| 4935 | |
| 4936 | // Drill down into arrays. |
| 4937 | uint64_t numElts = 1; |
| 4938 | while (auto arrayType = CGM.getContext().getAsConstantArrayType(fieldType)) { |
| 4939 | numElts *= arrayType->getSize().getZExtValue(); |
| 4940 | fieldType = arrayType->getElementType(); |
| 4941 | } |
| 4942 | |
| 4943 | assert(!fieldType->isArrayType() && "ivar of non-constant array type?"); |
| 4944 | |
| 4945 | // If we ended up with a zero-sized array, we've done what we can do within |
| 4946 | // the limits of this layout encoding. |
| 4947 | if (numElts == 0) return; |
| 4948 | |
| 4949 | // Recurse if the base element type is a record type. |
| 4950 | if (auto recType = fieldType->getAs<RecordType>()) { |
| 4951 | size_t oldEnd = IvarsInfo.size(); |
| 4952 | |
| 4953 | visitRecord(recType, fieldOffset); |
| 4954 | |
| 4955 | // If we have an array, replicate the first entry's layout information. |
| 4956 | auto numEltEntries = IvarsInfo.size() - oldEnd; |
| 4957 | if (numElts != 1 && numEltEntries != 0) { |
| 4958 | CharUnits eltSize = CGM.getContext().getTypeSizeInChars(recType); |
| 4959 | for (uint64_t eltIndex = 1; eltIndex != numElts; ++eltIndex) { |
| 4960 | // Copy the last numEltEntries onto the end of the array, adjusting |
| 4961 | // each for the element size. |
| 4962 | for (size_t i = 0; i != numEltEntries; ++i) { |
| 4963 | auto firstEntry = IvarsInfo[oldEnd + i]; |
| 4964 | IvarsInfo.push_back(IvarInfo(firstEntry.Offset + eltIndex * eltSize, |
| 4965 | firstEntry.SizeInWords)); |
| 4966 | } |
| 4967 | } |
| 4968 | } |
| 4969 | |
Fariborz Jahanian | 524bb20 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 4970 | return; |
Fariborz Jahanian | 3b0f886 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 4971 | } |
Daniel Dunbar | 15bd888 | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 4972 | |
John McCall | 3fd13f06 | 2015-10-21 18:06:47 +0000 | [diff] [blame] | 4973 | // Classify the element type. |
| 4974 | Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), fieldType); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4975 | |
John McCall | 3fd13f06 | 2015-10-21 18:06:47 +0000 | [diff] [blame] | 4976 | // If it matches what we're looking for, add an entry. |
| 4977 | if ((ForStrongLayout && GCAttr == Qualifiers::Strong) |
| 4978 | || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) { |
| 4979 | assert(CGM.getContext().getTypeSizeInChars(fieldType) |
| 4980 | == CGM.getPointerSize()); |
| 4981 | IvarsInfo.push_back(IvarInfo(fieldOffset, numElts)); |
| 4982 | } |
Fariborz Jahanian | c559f3f | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 4983 | } |
| 4984 | |
John McCall | 3fd13f06 | 2015-10-21 18:06:47 +0000 | [diff] [blame] | 4985 | /// buildBitmap - This routine does the horsework of taking the offsets of |
| 4986 | /// strong/weak references and creating a bitmap. The bitmap is also |
| 4987 | /// returned in the given buffer, suitable for being passed to \c dump(). |
| 4988 | llvm::Constant *IvarLayoutBuilder::buildBitmap(CGObjCCommonMac &CGObjC, |
| 4989 | llvm::SmallVectorImpl<unsigned char> &buffer) { |
| 4990 | // The bitmap is a series of skip/scan instructions, aligned to word |
| 4991 | // boundaries. The skip is performed first. |
| 4992 | const unsigned char MaxNibble = 0xF; |
| 4993 | const unsigned char SkipMask = 0xF0, SkipShift = 4; |
| 4994 | const unsigned char ScanMask = 0x0F, ScanShift = 0; |
Rafael Espindola | 8b27bdb | 2014-11-06 13:30:38 +0000 | [diff] [blame] | 4995 | |
John McCall | 3fd13f06 | 2015-10-21 18:06:47 +0000 | [diff] [blame] | 4996 | assert(!IvarsInfo.empty() && "generating bitmap for no data"); |
| 4997 | |
| 4998 | // Sort the ivar info on byte position in case we encounterred a |
| 4999 | // union nested in the ivar list. |
| 5000 | if (IsDisordered) { |
| 5001 | // This isn't a stable sort, but our algorithm should handle it fine. |
| 5002 | llvm::array_pod_sort(IvarsInfo.begin(), IvarsInfo.end()); |
| 5003 | } else { |
Craig Topper | a346705 | 2016-01-03 19:43:20 +0000 | [diff] [blame] | 5004 | assert(std::is_sorted(IvarsInfo.begin(), IvarsInfo.end())); |
John McCall | 3fd13f06 | 2015-10-21 18:06:47 +0000 | [diff] [blame] | 5005 | } |
| 5006 | assert(IvarsInfo.back().Offset < InstanceEnd); |
| 5007 | |
| 5008 | assert(buffer.empty()); |
| 5009 | |
| 5010 | // Skip the next N words. |
| 5011 | auto skip = [&](unsigned numWords) { |
| 5012 | assert(numWords > 0); |
| 5013 | |
| 5014 | // Try to merge into the previous byte. Since scans happen second, we |
| 5015 | // can't do this if it includes a scan. |
| 5016 | if (!buffer.empty() && !(buffer.back() & ScanMask)) { |
| 5017 | unsigned lastSkip = buffer.back() >> SkipShift; |
| 5018 | if (lastSkip < MaxNibble) { |
| 5019 | unsigned claimed = std::min(MaxNibble - lastSkip, numWords); |
| 5020 | numWords -= claimed; |
| 5021 | lastSkip += claimed; |
| 5022 | buffer.back() = (lastSkip << SkipShift); |
| 5023 | } |
| 5024 | } |
| 5025 | |
| 5026 | while (numWords >= MaxNibble) { |
| 5027 | buffer.push_back(MaxNibble << SkipShift); |
| 5028 | numWords -= MaxNibble; |
| 5029 | } |
| 5030 | if (numWords) { |
| 5031 | buffer.push_back(numWords << SkipShift); |
| 5032 | } |
| 5033 | }; |
| 5034 | |
| 5035 | // Scan the next N words. |
| 5036 | auto scan = [&](unsigned numWords) { |
| 5037 | assert(numWords > 0); |
| 5038 | |
| 5039 | // Try to merge into the previous byte. Since scans happen second, we can |
| 5040 | // do this even if it includes a skip. |
| 5041 | if (!buffer.empty()) { |
| 5042 | unsigned lastScan = (buffer.back() & ScanMask) >> ScanShift; |
| 5043 | if (lastScan < MaxNibble) { |
| 5044 | unsigned claimed = std::min(MaxNibble - lastScan, numWords); |
| 5045 | numWords -= claimed; |
| 5046 | lastScan += claimed; |
| 5047 | buffer.back() = (buffer.back() & SkipMask) | (lastScan << ScanShift); |
| 5048 | } |
| 5049 | } |
| 5050 | |
| 5051 | while (numWords >= MaxNibble) { |
| 5052 | buffer.push_back(MaxNibble << ScanShift); |
| 5053 | numWords -= MaxNibble; |
| 5054 | } |
| 5055 | if (numWords) { |
| 5056 | buffer.push_back(numWords << ScanShift); |
| 5057 | } |
| 5058 | }; |
| 5059 | |
| 5060 | // One past the end of the last scan. |
| 5061 | unsigned endOfLastScanInWords = 0; |
| 5062 | const CharUnits WordSize = CGM.getPointerSize(); |
| 5063 | |
| 5064 | // Consider all the scan requests. |
| 5065 | for (auto &request : IvarsInfo) { |
| 5066 | CharUnits beginOfScan = request.Offset - InstanceBegin; |
| 5067 | |
| 5068 | // Ignore scan requests that don't start at an even multiple of the |
| 5069 | // word size. We can't encode them. |
| 5070 | if ((beginOfScan % WordSize) != 0) continue; |
| 5071 | |
| 5072 | // Ignore scan requests that start before the instance start. |
| 5073 | // This assumes that scans never span that boundary. The boundary |
| 5074 | // isn't the true start of the ivars, because in the fragile-ARC case |
| 5075 | // it's rounded up to word alignment, but the test above should leave |
| 5076 | // us ignoring that possibility. |
| 5077 | if (beginOfScan.isNegative()) { |
| 5078 | assert(request.Offset + request.SizeInWords * WordSize <= InstanceBegin); |
| 5079 | continue; |
| 5080 | } |
| 5081 | |
| 5082 | unsigned beginOfScanInWords = beginOfScan / WordSize; |
| 5083 | unsigned endOfScanInWords = beginOfScanInWords + request.SizeInWords; |
| 5084 | |
| 5085 | // If the scan starts some number of words after the last one ended, |
| 5086 | // skip forward. |
| 5087 | if (beginOfScanInWords > endOfLastScanInWords) { |
| 5088 | skip(beginOfScanInWords - endOfLastScanInWords); |
| 5089 | |
| 5090 | // Otherwise, start scanning where the last left off. |
| 5091 | } else { |
| 5092 | beginOfScanInWords = endOfLastScanInWords; |
| 5093 | |
| 5094 | // If that leaves us with nothing to scan, ignore this request. |
| 5095 | if (beginOfScanInWords >= endOfScanInWords) continue; |
| 5096 | } |
| 5097 | |
| 5098 | // Scan to the end of the request. |
| 5099 | assert(beginOfScanInWords < endOfScanInWords); |
| 5100 | scan(endOfScanInWords - beginOfScanInWords); |
| 5101 | endOfLastScanInWords = endOfScanInWords; |
| 5102 | } |
| 5103 | |
John McCall | f5ea072 | 2015-10-29 23:36:14 +0000 | [diff] [blame] | 5104 | if (buffer.empty()) |
| 5105 | return llvm::ConstantPointerNull::get(CGM.Int8PtrTy); |
| 5106 | |
John McCall | 3fd13f06 | 2015-10-21 18:06:47 +0000 | [diff] [blame] | 5107 | // For GC layouts, emit a skip to the end of the allocation so that we |
| 5108 | // have precise information about the entire thing. This isn't useful |
| 5109 | // or necessary for the ARC-style layout strings. |
| 5110 | if (CGM.getLangOpts().getGC() != LangOptions::NonGC) { |
| 5111 | unsigned lastOffsetInWords = |
| 5112 | (InstanceEnd - InstanceBegin + WordSize - CharUnits::One()) / WordSize; |
| 5113 | if (lastOffsetInWords > endOfLastScanInWords) { |
| 5114 | skip(lastOffsetInWords - endOfLastScanInWords); |
| 5115 | } |
| 5116 | } |
| 5117 | |
| 5118 | // Null terminate the string. |
| 5119 | buffer.push_back(0); |
| 5120 | |
Saleem Abdulrasool | 271106c | 2016-09-16 23:41:13 +0000 | [diff] [blame] | 5121 | auto *Entry = CGObjC.CreateCStringLiteral( |
| 5122 | reinterpret_cast<char *>(buffer.data()), ObjCLabelType::ClassName); |
John McCall | 3fd13f06 | 2015-10-21 18:06:47 +0000 | [diff] [blame] | 5123 | return getConstantGEP(CGM.getLLVMContext(), Entry, 0, 0); |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 5124 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5125 | |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 5126 | /// BuildIvarLayout - Builds ivar layout bitmap for the class |
| 5127 | /// implementation for the __strong or __weak case. |
| 5128 | /// The layout map displays which words in ivar list must be skipped |
| 5129 | /// and which must be scanned by GC (see below). String is built of bytes. |
| 5130 | /// Each byte is divided up in two nibbles (4-bit each). Left nibble is count |
| 5131 | /// of words to skip and right nibble is count of words to scan. So, each |
| 5132 | /// nibble represents up to 15 workds to skip or scan. Skipping the rest is |
| 5133 | /// represented by a 0x00 byte which also ends the string. |
| 5134 | /// 1. when ForStrongLayout is true, following ivars are scanned: |
| 5135 | /// - id, Class |
| 5136 | /// - object * |
| 5137 | /// - __strong anything |
| 5138 | /// |
| 5139 | /// 2. When ForStrongLayout is false, following ivars are scanned: |
| 5140 | /// - __weak anything |
| 5141 | /// |
John McCall | 3fd13f06 | 2015-10-21 18:06:47 +0000 | [diff] [blame] | 5142 | llvm::Constant * |
| 5143 | CGObjCCommonMac::BuildIvarLayout(const ObjCImplementationDecl *OMD, |
| 5144 | CharUnits beginOffset, CharUnits endOffset, |
John McCall | 460ce58 | 2015-10-22 18:38:17 +0000 | [diff] [blame] | 5145 | bool ForStrongLayout, bool HasMRCWeakIvars) { |
| 5146 | // If this is MRC, and we're either building a strong layout or there |
| 5147 | // are no weak ivars, bail out early. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5148 | llvm::Type *PtrTy = CGM.Int8PtrTy; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5149 | if (CGM.getLangOpts().getGC() == LangOptions::NonGC && |
John McCall | 460ce58 | 2015-10-22 18:38:17 +0000 | [diff] [blame] | 5150 | !CGM.getLangOpts().ObjCAutoRefCount && |
| 5151 | (ForStrongLayout || !HasMRCWeakIvars)) |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 5152 | return llvm::Constant::getNullValue(PtrTy); |
| 5153 | |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 5154 | const ObjCInterfaceDecl *OI = OMD->getClassInterface(); |
John McCall | 3fd13f06 | 2015-10-21 18:06:47 +0000 | [diff] [blame] | 5155 | SmallVector<const ObjCIvarDecl*, 32> ivars; |
| 5156 | |
| 5157 | // GC layout strings include the complete object layout, possibly |
| 5158 | // inaccurately in the non-fragile ABI; the runtime knows how to fix this |
| 5159 | // up. |
| 5160 | // |
| 5161 | // ARC layout strings only include the class's ivars. In non-fragile |
John McCall | f5ea072 | 2015-10-29 23:36:14 +0000 | [diff] [blame] | 5162 | // runtimes, that means starting at InstanceStart, rounded up to word |
| 5163 | // alignment. In fragile runtimes, there's no InstanceStart, so it means |
John McCall | d80218f | 2015-11-19 02:27:55 +0000 | [diff] [blame] | 5164 | // starting at the offset of the first ivar, rounded up to word alignment. |
John McCall | 460ce58 | 2015-10-22 18:38:17 +0000 | [diff] [blame] | 5165 | // |
| 5166 | // MRC weak layout strings follow the ARC style. |
John McCall | 3fd13f06 | 2015-10-21 18:06:47 +0000 | [diff] [blame] | 5167 | CharUnits baseOffset; |
John McCall | 460ce58 | 2015-10-22 18:38:17 +0000 | [diff] [blame] | 5168 | if (CGM.getLangOpts().getGC() == LangOptions::NonGC) { |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 5169 | for (const ObjCIvarDecl *IVD = OI->all_declared_ivar_begin(); |
Fariborz Jahanian | b26d578 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 5170 | IVD; IVD = IVD->getNextIvar()) |
John McCall | 3fd13f06 | 2015-10-21 18:06:47 +0000 | [diff] [blame] | 5171 | ivars.push_back(IVD); |
| 5172 | |
| 5173 | if (isNonFragileABI()) { |
| 5174 | baseOffset = beginOffset; // InstanceStart |
John McCall | d80218f | 2015-11-19 02:27:55 +0000 | [diff] [blame] | 5175 | } else if (!ivars.empty()) { |
| 5176 | baseOffset = |
| 5177 | CharUnits::fromQuantity(ComputeIvarBaseOffset(CGM, OMD, ivars[0])); |
John McCall | 3fd13f06 | 2015-10-21 18:06:47 +0000 | [diff] [blame] | 5178 | } else { |
| 5179 | baseOffset = CharUnits::Zero(); |
| 5180 | } |
John McCall | f5ea072 | 2015-10-29 23:36:14 +0000 | [diff] [blame] | 5181 | |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 5182 | baseOffset = baseOffset.alignTo(CGM.getPointerAlign()); |
Fariborz Jahanian | b26d578 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 5183 | } |
| 5184 | else { |
John McCall | 3fd13f06 | 2015-10-21 18:06:47 +0000 | [diff] [blame] | 5185 | CGM.getContext().DeepCollectObjCIvars(OI, true, ivars); |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 5186 | |
John McCall | 3fd13f06 | 2015-10-21 18:06:47 +0000 | [diff] [blame] | 5187 | baseOffset = CharUnits::Zero(); |
Fariborz Jahanian | b26d578 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 5188 | } |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 5189 | |
John McCall | 3fd13f06 | 2015-10-21 18:06:47 +0000 | [diff] [blame] | 5190 | if (ivars.empty()) |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 5191 | return llvm::Constant::getNullValue(PtrTy); |
| 5192 | |
John McCall | 3fd13f06 | 2015-10-21 18:06:47 +0000 | [diff] [blame] | 5193 | IvarLayoutBuilder builder(CGM, baseOffset, endOffset, ForStrongLayout); |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 5194 | |
John McCall | 3fd13f06 | 2015-10-21 18:06:47 +0000 | [diff] [blame] | 5195 | builder.visitAggregate(ivars.begin(), ivars.end(), CharUnits::Zero(), |
| 5196 | [&](const ObjCIvarDecl *ivar) -> CharUnits { |
| 5197 | return CharUnits::fromQuantity(ComputeIvarBaseOffset(CGM, OMD, ivar)); |
| 5198 | }); |
| 5199 | |
| 5200 | if (!builder.hasBitmapData()) |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 5201 | return llvm::Constant::getNullValue(PtrTy); |
John McCall | 3fd13f06 | 2015-10-21 18:06:47 +0000 | [diff] [blame] | 5202 | |
| 5203 | llvm::SmallVector<unsigned char, 4> buffer; |
| 5204 | llvm::Constant *C = builder.buildBitmap(*this, buffer); |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 5205 | |
John McCall | f5ea072 | 2015-10-29 23:36:14 +0000 | [diff] [blame] | 5206 | if (CGM.getLangOpts().ObjCGCBitmapPrint && !buffer.empty()) { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5207 | printf("\n%s ivar layout for class '%s': ", |
Fariborz Jahanian | 80c9ce2 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 5208 | ForStrongLayout ? "strong" : "weak", |
Ben Langmuir | e013bdc | 2014-07-11 00:43:47 +0000 | [diff] [blame] | 5209 | OMD->getClassInterface()->getName().str().c_str()); |
John McCall | 3fd13f06 | 2015-10-21 18:06:47 +0000 | [diff] [blame] | 5210 | builder.dump(buffer); |
Fariborz Jahanian | 80c9ce2 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 5211 | } |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 5212 | return C; |
Fariborz Jahanian | c559f3f | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 5213 | } |
| 5214 | |
Fariborz Jahanian | 0b1ccdc | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 5215 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) { |
Daniel Dunbar | cb515c8 | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 5216 | llvm::GlobalVariable *&Entry = MethodVarNames[Sel]; |
Chris Lattner | 3def9ae | 2012-02-06 22:16:34 +0000 | [diff] [blame] | 5217 | // FIXME: Avoid std::string in "Sel.getAsString()" |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 5218 | if (!Entry) |
Saleem Abdulrasool | 271106c | 2016-09-16 23:41:13 +0000 | [diff] [blame] | 5219 | Entry = CreateCStringLiteral(Sel.getAsString(), ObjCLabelType::MethodVarName); |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 5220 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 5221 | } |
| 5222 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 5223 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 0b1ccdc | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 5224 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) { |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 5225 | return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID)); |
| 5226 | } |
| 5227 | |
Daniel Dunbar | f5c1846 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 5228 | llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) { |
Devang Patel | 4b6e4bb | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 5229 | std::string TypeStr; |
| 5230 | CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field); |
| 5231 | |
| 5232 | llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr]; |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 5233 | if (!Entry) |
Saleem Abdulrasool | 271106c | 2016-09-16 23:41:13 +0000 | [diff] [blame] | 5234 | Entry = CreateCStringLiteral(TypeStr, ObjCLabelType::MethodVarType); |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 5235 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | cb515c8 | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 5236 | } |
| 5237 | |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5238 | llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D, |
| 5239 | bool Extended) { |
John McCall | 843dfcc | 2016-11-29 21:57:00 +0000 | [diff] [blame] | 5240 | std::string TypeStr = |
| 5241 | CGM.getContext().getObjCEncodingForMethodDecl(D, Extended); |
Devang Patel | 4b6e4bb | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 5242 | |
| 5243 | llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr]; |
Daniel Dunbar | 3241fae | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 5244 | if (!Entry) |
Saleem Abdulrasool | 271106c | 2016-09-16 23:41:13 +0000 | [diff] [blame] | 5245 | Entry = CreateCStringLiteral(TypeStr, ObjCLabelType::MethodVarType); |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 5246 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 5247 | } |
| 5248 | |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 5249 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 0b1ccdc | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 5250 | llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) { |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 5251 | llvm::GlobalVariable *&Entry = PropertyNames[Ident]; |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 5252 | if (!Entry) |
Saleem Abdulrasool | 271106c | 2016-09-16 23:41:13 +0000 | [diff] [blame] | 5253 | Entry = CreateCStringLiteral(Ident->getName(), ObjCLabelType::PropertyName); |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 5254 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 5255 | } |
| 5256 | |
| 5257 | // FIXME: Merge into a single cstring creation function. |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 5258 | // FIXME: This Decl should be more precise. |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 5259 | llvm::Constant * |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5260 | CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD, |
| 5261 | const Decl *Container) { |
John McCall | 843dfcc | 2016-11-29 21:57:00 +0000 | [diff] [blame] | 5262 | std::string TypeStr = |
| 5263 | CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container); |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 5264 | return GetPropertyName(&CGM.getContext().Idents.get(TypeStr)); |
| 5265 | } |
| 5266 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5267 | void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D, |
Fariborz Jahanian | 0b1ccdc | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 5268 | const ObjCContainerDecl *CD, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5269 | SmallVectorImpl<char> &Name) { |
Daniel Dunbar | d238681 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 5270 | llvm::raw_svector_ostream OS(Name); |
Fariborz Jahanian | 0196a1c | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 5271 | assert (CD && "Missing container decl in GetNameForMethod"); |
Daniel Dunbar | d238681 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 5272 | OS << '\01' << (D->isInstanceMethod() ? '-' : '+') |
| 5273 | << '[' << CD->getName(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5274 | if (const ObjCCategoryImplDecl *CID = |
Daniel Dunbar | d238681 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 5275 | dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext())) |
Benjamin Kramer | 2f56992 | 2012-02-07 11:57:45 +0000 | [diff] [blame] | 5276 | OS << '(' << *CID << ')'; |
Daniel Dunbar | d238681 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 5277 | OS << ' ' << D->getSelector().getAsString() << ']'; |
Daniel Dunbar | a94ecd2 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 5278 | } |
| 5279 | |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 5280 | void CGObjCMac::FinishModule() { |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 5281 | EmitModuleInfo(); |
| 5282 | |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 5283 | // Emit the dummy bodies for any protocols which were referenced but |
| 5284 | // never defined. |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 5285 | for (auto &entry : Protocols) { |
| 5286 | llvm::GlobalVariable *global = entry.second; |
| 5287 | if (global->hasInitializer()) |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 5288 | continue; |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 5289 | |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 5290 | ConstantInitBuilder builder(CGM); |
| 5291 | auto values = builder.beginStruct(ObjCTypes.ProtocolTy); |
| 5292 | values.addNullPointer(ObjCTypes.ProtocolExtensionPtrTy); |
| 5293 | values.add(GetClassName(entry.first->getName())); |
| 5294 | values.addNullPointer(ObjCTypes.ProtocolListPtrTy); |
| 5295 | values.addNullPointer(ObjCTypes.MethodDescriptionListPtrTy); |
| 5296 | values.addNullPointer(ObjCTypes.MethodDescriptionListPtrTy); |
| 5297 | values.finishAndSetAsInitializer(global); |
| 5298 | CGM.addCompilerUsedGlobal(global); |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 5299 | } |
| 5300 | |
Daniel Dunbar | c61d0e9 | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 5301 | // Add assembler directives to add lazy undefined symbol references |
| 5302 | // for classes which are referenced but not defined. This is |
| 5303 | // important for correct linker interaction. |
Daniel Dunbar | d027a92 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 5304 | // |
| 5305 | // FIXME: It would be nice if we had an LLVM construct for this. |
Saleem Abdulrasool | 62c07eb | 2016-09-12 21:15:23 +0000 | [diff] [blame] | 5306 | if ((!LazySymbols.empty() || !DefinedSymbols.empty()) && |
| 5307 | CGM.getTriple().isOSBinFormatMachO()) { |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 5308 | SmallString<256> Asm; |
Daniel Dunbar | d027a92 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 5309 | Asm += CGM.getModule().getModuleInlineAsm(); |
| 5310 | if (!Asm.empty() && Asm.back() != '\n') |
| 5311 | Asm += '\n'; |
Daniel Dunbar | c61d0e9 | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 5312 | |
Daniel Dunbar | d027a92 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 5313 | llvm::raw_svector_ostream OS(Asm); |
Saleem Abdulrasool | 39217d4 | 2016-09-16 14:24:26 +0000 | [diff] [blame] | 5314 | for (const auto *Sym : DefinedSymbols) |
Saleem Abdulrasool | 62c07eb | 2016-09-12 21:15:23 +0000 | [diff] [blame] | 5315 | OS << "\t.objc_class_name_" << Sym->getName() << "=0\n" |
| 5316 | << "\t.globl .objc_class_name_" << Sym->getName() << "\n"; |
Saleem Abdulrasool | 39217d4 | 2016-09-16 14:24:26 +0000 | [diff] [blame] | 5317 | for (const auto *Sym : LazySymbols) |
Saleem Abdulrasool | 62c07eb | 2016-09-12 21:15:23 +0000 | [diff] [blame] | 5318 | OS << "\t.lazy_reference .objc_class_name_" << Sym->getName() << "\n"; |
| 5319 | for (const auto &Category : DefinedCategoryNames) |
| 5320 | OS << "\t.objc_category_name_" << Category << "=0\n" |
| 5321 | << "\t.globl .objc_category_name_" << Category << "\n"; |
Fariborz Jahanian | 9adb2e6 | 2010-06-21 22:05:18 +0000 | [diff] [blame] | 5322 | |
Daniel Dunbar | d027a92 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 5323 | CGM.getModule().setModuleInlineAsm(OS.str()); |
Daniel Dunbar | c61d0e9 | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 5324 | } |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 5325 | } |
| 5326 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5327 | CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm) |
Saleem Abdulrasool | 4f515a6 | 2016-07-13 02:58:44 +0000 | [diff] [blame] | 5328 | : CGObjCCommonMac(cgm), ObjCTypes(cgm), ObjCEmptyCacheVar(nullptr), |
| 5329 | ObjCEmptyVtableVar(nullptr) { |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5330 | ObjCABI = 2; |
| 5331 | } |
| 5332 | |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 5333 | /* *** */ |
| 5334 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5335 | ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5336 | : VMContext(cgm.getLLVMContext()), CGM(cgm), ExternalProtocolPtrTy(nullptr) |
Douglas Gregor | a95f9aa | 2012-01-17 23:38:32 +0000 | [diff] [blame] | 5337 | { |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 5338 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 5339 | ASTContext &Ctx = CGM.getContext(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5340 | |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 5341 | ShortTy = cast<llvm::IntegerType>(Types.ConvertType(Ctx.ShortTy)); |
| 5342 | IntTy = CGM.IntTy; |
| 5343 | LongTy = cast<llvm::IntegerType>(Types.ConvertType(Ctx.LongTy)); |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5344 | Int8PtrTy = CGM.Int8PtrTy; |
| 5345 | Int8PtrPtrTy = CGM.Int8PtrPtrTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5346 | |
Tim Northover | 238b508 | 2014-03-29 13:42:40 +0000 | [diff] [blame] | 5347 | // arm64 targets use "int" ivar offset variables. All others, |
| 5348 | // including OS X x86_64 and Windows x86_64, use "long" ivar offsets. |
Tim Northover | 40956e6 | 2014-07-23 12:32:58 +0000 | [diff] [blame] | 5349 | if (CGM.getTarget().getTriple().getArch() == llvm::Triple::aarch64) |
Tim Northover | 238b508 | 2014-03-29 13:42:40 +0000 | [diff] [blame] | 5350 | IvarOffsetVarTy = IntTy; |
| 5351 | else |
| 5352 | IvarOffsetVarTy = LongTy; |
| 5353 | |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 5354 | ObjectPtrTy = |
| 5355 | cast<llvm::PointerType>(Types.ConvertType(Ctx.getObjCIdType())); |
| 5356 | PtrObjectPtrTy = |
| 5357 | llvm::PointerType::getUnqual(ObjectPtrTy); |
| 5358 | SelectorPtrTy = |
| 5359 | cast<llvm::PointerType>(Types.ConvertType(Ctx.getObjCSelType())); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5360 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5361 | // I'm not sure I like this. The implicit coordination is a bit |
| 5362 | // gross. We should solve this in a reasonable fashion because this |
| 5363 | // is a pretty common task (match some runtime data structure with |
| 5364 | // an LLVM data structure). |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5365 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5366 | // FIXME: This is leaked. |
| 5367 | // FIXME: Merge with rewriter code? |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5368 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5369 | // struct _objc_super { |
| 5370 | // id self; |
| 5371 | // Class cls; |
| 5372 | // } |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5373 | RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct, |
Daniel Dunbar | 0c00537 | 2010-04-29 16:29:11 +0000 | [diff] [blame] | 5374 | Ctx.getTranslationUnitDecl(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 5375 | SourceLocation(), SourceLocation(), |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5376 | &Ctx.Idents.get("_objc_super")); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5377 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), |
| 5378 | nullptr, Ctx.getObjCIdType(), nullptr, nullptr, |
| 5379 | false, ICIS_NoInit)); |
| 5380 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), |
| 5381 | nullptr, Ctx.getObjCClassType(), nullptr, |
| 5382 | nullptr, false, ICIS_NoInit)); |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 5383 | RD->completeDefinition(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5384 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5385 | SuperCTy = Ctx.getTagDeclType(RD); |
| 5386 | SuperPtrCTy = Ctx.getPointerType(SuperCTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5387 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5388 | SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy)); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5389 | SuperPtrTy = llvm::PointerType::getUnqual(SuperTy); |
| 5390 | |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5391 | // struct _prop_t { |
| 5392 | // char *name; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5393 | // char *attributes; |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5394 | // } |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5395 | PropertyTy = llvm::StructType::create("struct._prop_t", |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 5396 | Int8PtrTy, Int8PtrTy, nullptr); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5397 | |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5398 | // struct _prop_list_t { |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 5399 | // uint32_t entsize; // sizeof(struct _prop_t) |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5400 | // uint32_t count_of_properties; |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 5401 | // struct _prop_t prop_list[count_of_properties]; |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5402 | // } |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5403 | PropertyListTy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5404 | llvm::StructType::create("struct._prop_list_t", IntTy, IntTy, |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 5405 | llvm::ArrayType::get(PropertyTy, 0), nullptr); |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5406 | // struct _prop_list_t * |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5407 | PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5408 | |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5409 | // struct _objc_method { |
| 5410 | // SEL _cmd; |
| 5411 | // char *method_type; |
| 5412 | // char *_imp; |
| 5413 | // } |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5414 | MethodTy = llvm::StructType::create("struct._objc_method", |
| 5415 | SelectorPtrTy, Int8PtrTy, Int8PtrTy, |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 5416 | nullptr); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5417 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 5418 | // struct _objc_cache * |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5419 | CacheTy = llvm::StructType::create(VMContext, "struct._objc_cache"); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5420 | CachePtrTy = llvm::PointerType::getUnqual(CacheTy); |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5421 | } |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 5422 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5423 | ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5424 | : ObjCCommonTypesHelper(cgm) { |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 5425 | // struct _objc_method_description { |
| 5426 | // SEL name; |
| 5427 | // char *types; |
| 5428 | // } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5429 | MethodDescriptionTy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5430 | llvm::StructType::create("struct._objc_method_description", |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 5431 | SelectorPtrTy, Int8PtrTy, nullptr); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 5432 | |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 5433 | // struct _objc_method_description_list { |
| 5434 | // int count; |
| 5435 | // struct _objc_method_description[1]; |
| 5436 | // } |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 5437 | MethodDescriptionListTy = llvm::StructType::create( |
| 5438 | "struct._objc_method_description_list", IntTy, |
| 5439 | llvm::ArrayType::get(MethodDescriptionTy, 0), nullptr); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5440 | |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 5441 | // struct _objc_method_description_list * |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5442 | MethodDescriptionListPtrTy = |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5443 | llvm::PointerType::getUnqual(MethodDescriptionListTy); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 5444 | |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 5445 | // Protocol description structures |
| 5446 | |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 5447 | // struct _objc_protocol_extension { |
| 5448 | // uint32_t size; // sizeof(struct _objc_protocol_extension) |
| 5449 | // struct _objc_method_description_list *optional_instance_methods; |
| 5450 | // struct _objc_method_description_list *optional_class_methods; |
| 5451 | // struct _objc_property_list *instance_properties; |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5452 | // const char ** extendedMethodTypes; |
Manman Ren | ce7bff5 | 2016-01-29 23:46:55 +0000 | [diff] [blame] | 5453 | // struct _objc_property_list *class_properties; |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 5454 | // } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5455 | ProtocolExtensionTy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5456 | llvm::StructType::create("struct._objc_protocol_extension", |
| 5457 | IntTy, MethodDescriptionListPtrTy, |
| 5458 | MethodDescriptionListPtrTy, PropertyListPtrTy, |
Manman Ren | ce7bff5 | 2016-01-29 23:46:55 +0000 | [diff] [blame] | 5459 | Int8PtrPtrTy, PropertyListPtrTy, nullptr); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5460 | |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 5461 | // struct _objc_protocol_extension * |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5462 | ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 5463 | |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 5464 | // Handle recursive construction of Protocol and ProtocolList types |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 5465 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5466 | ProtocolTy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5467 | llvm::StructType::create(VMContext, "struct._objc_protocol"); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 5468 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5469 | ProtocolListTy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5470 | llvm::StructType::create(VMContext, "struct._objc_protocol_list"); |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5471 | ProtocolListTy->setBody(llvm::PointerType::getUnqual(ProtocolListTy), |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5472 | LongTy, |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5473 | llvm::ArrayType::get(ProtocolTy, 0), |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 5474 | nullptr); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 5475 | |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 5476 | // struct _objc_protocol { |
| 5477 | // struct _objc_protocol_extension *isa; |
| 5478 | // char *protocol_name; |
| 5479 | // struct _objc_protocol **_objc_protocol_list; |
| 5480 | // struct _objc_method_description_list *instance_methods; |
| 5481 | // struct _objc_method_description_list *class_methods; |
| 5482 | // } |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5483 | ProtocolTy->setBody(ProtocolExtensionPtrTy, Int8PtrTy, |
| 5484 | llvm::PointerType::getUnqual(ProtocolListTy), |
| 5485 | MethodDescriptionListPtrTy, |
| 5486 | MethodDescriptionListPtrTy, |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 5487 | nullptr); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 5488 | |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 5489 | // struct _objc_protocol_list * |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5490 | ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 5491 | |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5492 | ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 5493 | |
| 5494 | // Class description structures |
| 5495 | |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 5496 | // struct _objc_ivar { |
| 5497 | // char *ivar_name; |
| 5498 | // char *ivar_type; |
| 5499 | // int ivar_offset; |
| 5500 | // } |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5501 | IvarTy = llvm::StructType::create("struct._objc_ivar", |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 5502 | Int8PtrTy, Int8PtrTy, IntTy, nullptr); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 5503 | |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 5504 | // struct _objc_ivar_list * |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5505 | IvarListTy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5506 | llvm::StructType::create(VMContext, "struct._objc_ivar_list"); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5507 | IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 5508 | |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 5509 | // struct _objc_method_list * |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5510 | MethodListTy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5511 | llvm::StructType::create(VMContext, "struct._objc_method_list"); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5512 | MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 5513 | |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 5514 | // struct _objc_class_extension * |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5515 | ClassExtensionTy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5516 | llvm::StructType::create("struct._objc_class_extension", |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 5517 | IntTy, Int8PtrTy, PropertyListPtrTy, nullptr); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5518 | ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 5519 | |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5520 | ClassTy = llvm::StructType::create(VMContext, "struct._objc_class"); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 5521 | |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 5522 | // struct _objc_class { |
| 5523 | // Class isa; |
| 5524 | // Class super_class; |
| 5525 | // char *name; |
| 5526 | // long version; |
| 5527 | // long info; |
| 5528 | // long instance_size; |
| 5529 | // struct _objc_ivar_list *ivars; |
| 5530 | // struct _objc_method_list *methods; |
| 5531 | // struct _objc_cache *cache; |
| 5532 | // struct _objc_protocol_list *protocols; |
| 5533 | // char *ivar_layout; |
| 5534 | // struct _objc_class_ext *ext; |
| 5535 | // }; |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5536 | ClassTy->setBody(llvm::PointerType::getUnqual(ClassTy), |
| 5537 | llvm::PointerType::getUnqual(ClassTy), |
| 5538 | Int8PtrTy, |
| 5539 | LongTy, |
| 5540 | LongTy, |
| 5541 | LongTy, |
| 5542 | IvarListPtrTy, |
| 5543 | MethodListPtrTy, |
| 5544 | CachePtrTy, |
| 5545 | ProtocolListPtrTy, |
| 5546 | Int8PtrTy, |
| 5547 | ClassExtensionPtrTy, |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 5548 | nullptr); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5549 | |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5550 | ClassPtrTy = llvm::PointerType::getUnqual(ClassTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 5551 | |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 5552 | // struct _objc_category { |
| 5553 | // char *category_name; |
| 5554 | // char *class_name; |
| 5555 | // struct _objc_method_list *instance_method; |
| 5556 | // struct _objc_method_list *class_method; |
Manman Ren | b373677 | 2016-01-25 22:37:47 +0000 | [diff] [blame] | 5557 | // struct _objc_protocol_list *protocols; |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 5558 | // uint32_t size; // sizeof(struct _objc_category) |
| 5559 | // struct _objc_property_list *instance_properties;// category's @property |
Manman Ren | 96df0b3 | 2016-01-29 23:45:01 +0000 | [diff] [blame] | 5560 | // struct _objc_property_list *class_properties; |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 5561 | // } |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5562 | CategoryTy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5563 | llvm::StructType::create("struct._objc_category", |
| 5564 | Int8PtrTy, Int8PtrTy, MethodListPtrTy, |
| 5565 | MethodListPtrTy, ProtocolListPtrTy, |
Manman Ren | 96df0b3 | 2016-01-29 23:45:01 +0000 | [diff] [blame] | 5566 | IntTy, PropertyListPtrTy, PropertyListPtrTy, |
| 5567 | nullptr); |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 5568 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 5569 | // Global metadata structures |
| 5570 | |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 5571 | // struct _objc_symtab { |
| 5572 | // long sel_ref_cnt; |
| 5573 | // SEL *refs; |
| 5574 | // short cls_def_cnt; |
| 5575 | // short cat_def_cnt; |
| 5576 | // char *defs[cls_def_cnt + cat_def_cnt]; |
| 5577 | // } |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5578 | SymtabTy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5579 | llvm::StructType::create("struct._objc_symtab", |
| 5580 | LongTy, SelectorPtrTy, ShortTy, ShortTy, |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 5581 | llvm::ArrayType::get(Int8PtrTy, 0), nullptr); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5582 | SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 5583 | |
Fariborz Jahanian | eee54df | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 5584 | // struct _objc_module { |
| 5585 | // long version; |
| 5586 | // long size; // sizeof(struct _objc_module) |
| 5587 | // char *name; |
| 5588 | // struct _objc_symtab* symtab; |
| 5589 | // } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5590 | ModuleTy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5591 | llvm::StructType::create("struct._objc_module", |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 5592 | LongTy, LongTy, Int8PtrTy, SymtabPtrTy, nullptr); |
Daniel Dunbar | 97ff50d | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 5593 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5594 | |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 5595 | // FIXME: This is the size of the setjmp buffer and should be target |
| 5596 | // specific. 18 is what's used on 32-bit X86. |
Anders Carlsson | 9ff2248 | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 5597 | uint64_t SetJmpBufferSize = 18; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5598 | |
Anders Carlsson | 9ff2248 | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 5599 | // Exceptions |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5600 | llvm::Type *StackPtrTy = llvm::ArrayType::get(CGM.Int8PtrTy, 4); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5601 | |
| 5602 | ExceptionDataTy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5603 | llvm::StructType::create("struct._objc_exception_data", |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5604 | llvm::ArrayType::get(CGM.Int32Ty,SetJmpBufferSize), |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 5605 | StackPtrTy, nullptr); |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 5606 | } |
| 5607 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5608 | ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5609 | : ObjCCommonTypesHelper(cgm) { |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5610 | // struct _method_list_t { |
| 5611 | // uint32_t entsize; // sizeof(struct _objc_method) |
| 5612 | // uint32_t method_count; |
| 5613 | // struct _objc_method method_list[method_count]; |
| 5614 | // } |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5615 | MethodListnfABITy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5616 | llvm::StructType::create("struct.__method_list_t", IntTy, IntTy, |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 5617 | llvm::ArrayType::get(MethodTy, 0), nullptr); |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 5618 | // struct method_list_t * |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5619 | MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5620 | |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5621 | // struct _protocol_t { |
| 5622 | // id isa; // NULL |
| 5623 | // const char * const protocol_name; |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 5624 | // const struct _protocol_list_t * protocol_list; // super protocols |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5625 | // const struct method_list_t * const instance_methods; |
| 5626 | // const struct method_list_t * const class_methods; |
| 5627 | // const struct method_list_t *optionalInstanceMethods; |
| 5628 | // const struct method_list_t *optionalClassMethods; |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 5629 | // const struct _prop_list_t * properties; |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5630 | // const uint32_t size; // sizeof(struct _protocol_t) |
| 5631 | // const uint32_t flags; // = 0 |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5632 | // const char ** extendedMethodTypes; |
Fariborz Jahanian | 6a9c46b | 2015-03-31 22:22:40 +0000 | [diff] [blame] | 5633 | // const char *demangledName; |
Manman Ren | ce7bff5 | 2016-01-29 23:46:55 +0000 | [diff] [blame] | 5634 | // const struct _prop_list_t * class_properties; |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5635 | // } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5636 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 5637 | // Holder for struct _protocol_list_t * |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5638 | ProtocolListnfABITy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5639 | llvm::StructType::create(VMContext, "struct._objc_protocol_list"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5640 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5641 | ProtocolnfABITy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5642 | llvm::StructType::create("struct._protocol_t", ObjectPtrTy, Int8PtrTy, |
| 5643 | llvm::PointerType::getUnqual(ProtocolListnfABITy), |
| 5644 | MethodListnfABIPtrTy, MethodListnfABIPtrTy, |
| 5645 | MethodListnfABIPtrTy, MethodListnfABIPtrTy, |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5646 | PropertyListPtrTy, IntTy, IntTy, Int8PtrPtrTy, |
Manman Ren | ce7bff5 | 2016-01-29 23:46:55 +0000 | [diff] [blame] | 5647 | Int8PtrTy, PropertyListPtrTy, |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 5648 | nullptr); |
Daniel Dunbar | 8de90f0 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 5649 | |
| 5650 | // struct _protocol_t* |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5651 | ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5652 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5653 | // struct _protocol_list_t { |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 5654 | // long protocol_count; // Note, this is 32/64 bit |
Daniel Dunbar | 8de90f0 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 5655 | // struct _protocol_t *[protocol_count]; |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5656 | // } |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5657 | ProtocolListnfABITy->setBody(LongTy, |
| 5658 | llvm::ArrayType::get(ProtocolnfABIPtrTy, 0), |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 5659 | nullptr); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5660 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 5661 | // struct _objc_protocol_list* |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5662 | ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5663 | |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5664 | // struct _ivar_t { |
Tim Northover | 238b508 | 2014-03-29 13:42:40 +0000 | [diff] [blame] | 5665 | // unsigned [long] int *offset; // pointer to ivar offset location |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5666 | // char *name; |
| 5667 | // char *type; |
| 5668 | // uint32_t alignment; |
| 5669 | // uint32_t size; |
| 5670 | // } |
Tim Northover | 238b508 | 2014-03-29 13:42:40 +0000 | [diff] [blame] | 5671 | IvarnfABITy = llvm::StructType::create( |
| 5672 | "struct._ivar_t", llvm::PointerType::getUnqual(IvarOffsetVarTy), |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 5673 | Int8PtrTy, Int8PtrTy, IntTy, IntTy, nullptr); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5674 | |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5675 | // struct _ivar_list_t { |
| 5676 | // uint32 entsize; // sizeof(struct _ivar_t) |
| 5677 | // uint32 count; |
| 5678 | // struct _iver_t list[count]; |
| 5679 | // } |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5680 | IvarListnfABITy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5681 | llvm::StructType::create("struct._ivar_list_t", IntTy, IntTy, |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 5682 | llvm::ArrayType::get(IvarnfABITy, 0), nullptr); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5683 | |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5684 | IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5685 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 5686 | // struct _class_ro_t { |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5687 | // uint32_t const flags; |
| 5688 | // uint32_t const instanceStart; |
| 5689 | // uint32_t const instanceSize; |
| 5690 | // uint32_t const reserved; // only when building for 64bit targets |
| 5691 | // const uint8_t * const ivarLayout; |
| 5692 | // const char *const name; |
| 5693 | // const struct _method_list_t * const baseMethods; |
| 5694 | // const struct _objc_protocol_list *const baseProtocols; |
| 5695 | // const struct _ivar_list_t *const ivars; |
| 5696 | // const uint8_t * const weakIvarLayout; |
| 5697 | // const struct _prop_list_t * const properties; |
| 5698 | // } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5699 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 5700 | // FIXME. Add 'reserved' field in 64bit abi mode! |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5701 | ClassRonfABITy = llvm::StructType::create("struct._class_ro_t", |
| 5702 | IntTy, IntTy, IntTy, Int8PtrTy, |
| 5703 | Int8PtrTy, MethodListnfABIPtrTy, |
| 5704 | ProtocolListnfABIPtrTy, |
| 5705 | IvarListnfABIPtrTy, |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 5706 | Int8PtrTy, PropertyListPtrTy, |
| 5707 | nullptr); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5708 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 5709 | // ImpnfABITy - LLVM for id (*)(id, SEL, ...) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5710 | llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy }; |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 5711 | ImpnfABITy = llvm::FunctionType::get(ObjectPtrTy, params, false) |
| 5712 | ->getPointerTo(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5713 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 5714 | // struct _class_t { |
| 5715 | // struct _class_t *isa; |
| 5716 | // struct _class_t * const superclass; |
| 5717 | // void *cache; |
| 5718 | // IMP *vtable; |
| 5719 | // struct class_ro_t *ro; |
| 5720 | // } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5721 | |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5722 | ClassnfABITy = llvm::StructType::create(VMContext, "struct._class_t"); |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5723 | ClassnfABITy->setBody(llvm::PointerType::getUnqual(ClassnfABITy), |
| 5724 | llvm::PointerType::getUnqual(ClassnfABITy), |
| 5725 | CachePtrTy, |
| 5726 | llvm::PointerType::getUnqual(ImpnfABITy), |
| 5727 | llvm::PointerType::getUnqual(ClassRonfABITy), |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 5728 | nullptr); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5729 | |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5730 | // LLVM for struct _class_t * |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5731 | ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5732 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 5733 | // struct _category_t { |
| 5734 | // const char * const name; |
| 5735 | // struct _class_t *const cls; |
| 5736 | // const struct _method_list_t * const instance_methods; |
| 5737 | // const struct _method_list_t * const class_methods; |
| 5738 | // const struct _protocol_list_t * const protocols; |
| 5739 | // const struct _prop_list_t * const properties; |
Manman Ren | 96df0b3 | 2016-01-29 23:45:01 +0000 | [diff] [blame] | 5740 | // const struct _prop_list_t * const class_properties; |
Manman Ren | 42ff390 | 2016-02-24 17:49:50 +0000 | [diff] [blame] | 5741 | // const uint32_t size; |
Fariborz Jahanian | 5a63e4c | 2009-01-23 17:41:22 +0000 | [diff] [blame] | 5742 | // } |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5743 | CategorynfABITy = llvm::StructType::create("struct._category_t", |
| 5744 | Int8PtrTy, ClassnfABIPtrTy, |
| 5745 | MethodListnfABIPtrTy, |
| 5746 | MethodListnfABIPtrTy, |
| 5747 | ProtocolListnfABIPtrTy, |
| 5748 | PropertyListPtrTy, |
Manman Ren | 96df0b3 | 2016-01-29 23:45:01 +0000 | [diff] [blame] | 5749 | PropertyListPtrTy, |
Manman Ren | 42ff390 | 2016-02-24 17:49:50 +0000 | [diff] [blame] | 5750 | IntTy, |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 5751 | nullptr); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5752 | |
Fariborz Jahanian | 82c72e1 | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 5753 | // New types for nonfragile abi messaging. |
Fariborz Jahanian | e4dc35d | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5754 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 5755 | ASTContext &Ctx = CGM.getContext(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5756 | |
Fariborz Jahanian | 82c72e1 | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 5757 | // MessageRefTy - LLVM for: |
| 5758 | // struct _message_ref_t { |
| 5759 | // IMP messenger; |
| 5760 | // SEL name; |
| 5761 | // }; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5762 | |
Fariborz Jahanian | e4dc35d | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5763 | // First the clang type for struct _message_ref_t |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5764 | RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct, |
Daniel Dunbar | 0c00537 | 2010-04-29 16:29:11 +0000 | [diff] [blame] | 5765 | Ctx.getTranslationUnitDecl(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 5766 | SourceLocation(), SourceLocation(), |
Fariborz Jahanian | e4dc35d | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5767 | &Ctx.Idents.get("_message_ref_t")); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5768 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), |
| 5769 | nullptr, Ctx.VoidPtrTy, nullptr, nullptr, false, |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 5770 | ICIS_NoInit)); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5771 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), |
| 5772 | nullptr, Ctx.getObjCSelType(), nullptr, nullptr, |
| 5773 | false, ICIS_NoInit)); |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 5774 | RD->completeDefinition(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5775 | |
Fariborz Jahanian | e4dc35d | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5776 | MessageRefCTy = Ctx.getTagDeclType(RD); |
| 5777 | MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy); |
| 5778 | MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy)); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5779 | |
Fariborz Jahanian | 82c72e1 | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 5780 | // MessageRefPtrTy - LLVM for struct _message_ref_t* |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5781 | MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5782 | |
Fariborz Jahanian | 82c72e1 | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 5783 | // SuperMessageRefTy - LLVM for: |
| 5784 | // struct _super_message_ref_t { |
| 5785 | // SUPER_IMP messenger; |
| 5786 | // SEL name; |
| 5787 | // }; |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5788 | SuperMessageRefTy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5789 | llvm::StructType::create("struct._super_message_ref_t", |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 5790 | ImpnfABITy, SelectorPtrTy, nullptr); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5791 | |
Fariborz Jahanian | 82c72e1 | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 5792 | // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t* |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5793 | SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy); |
Fariborz Jahanian | 0a3cfcc | 2011-06-22 20:21:51 +0000 | [diff] [blame] | 5794 | |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5795 | |
| 5796 | // struct objc_typeinfo { |
| 5797 | // const void** vtable; // objc_ehtype_vtable + 2 |
| 5798 | // const char* name; // c++ typeinfo string |
| 5799 | // Class cls; |
| 5800 | // }; |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5801 | EHTypeTy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5802 | llvm::StructType::create("struct._objc_typeinfo", |
| 5803 | llvm::PointerType::getUnqual(Int8PtrTy), |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 5804 | Int8PtrTy, ClassnfABIPtrTy, nullptr); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5805 | EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy); |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 5806 | } |
| 5807 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5808 | llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() { |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5809 | FinishNonFragileABIModule(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5810 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5811 | return nullptr; |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5812 | } |
| 5813 | |
Saleem Abdulrasool | 1e6c406 | 2016-05-16 05:06:49 +0000 | [diff] [blame] | 5814 | void CGObjCNonFragileABIMac::AddModuleClassList( |
| 5815 | ArrayRef<llvm::GlobalValue *> Container, StringRef SymbolName, |
| 5816 | StringRef SectionName) { |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 5817 | unsigned NumClasses = Container.size(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5818 | |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 5819 | if (!NumClasses) |
| 5820 | return; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5821 | |
Chris Lattner | 3def9ae | 2012-02-06 22:16:34 +0000 | [diff] [blame] | 5822 | SmallVector<llvm::Constant*, 8> Symbols(NumClasses); |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 5823 | for (unsigned i=0; i<NumClasses; i++) |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 5824 | Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i], |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 5825 | ObjCTypes.Int8PtrTy); |
Chris Lattner | 3def9ae | 2012-02-06 22:16:34 +0000 | [diff] [blame] | 5826 | llvm::Constant *Init = |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5827 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
Chris Lattner | 3def9ae | 2012-02-06 22:16:34 +0000 | [diff] [blame] | 5828 | Symbols.size()), |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 5829 | Symbols); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5830 | |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 5831 | llvm::GlobalVariable *GV = |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5832 | new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Rafael Espindola | 5179a4e | 2014-02-27 19:01:11 +0000 | [diff] [blame] | 5833 | llvm::GlobalValue::PrivateLinkage, |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 5834 | Init, |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5835 | SymbolName); |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 5836 | GV->setAlignment(CGM.getDataLayout().getABITypeAlignment(Init->getType())); |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 5837 | GV->setSection(SectionName); |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 5838 | CGM.addCompilerUsedGlobal(GV); |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 5839 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5840 | |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5841 | void CGObjCNonFragileABIMac::FinishNonFragileABIModule() { |
| 5842 | // nonfragile abi has no module definition. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5843 | |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 5844 | // Build list of all implemented class addresses in array |
Fariborz Jahanian | 279abd3 | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 5845 | // L_OBJC_LABEL_CLASS_$. |
Fariborz Jahanian | f322f2f | 2014-03-11 00:25:05 +0000 | [diff] [blame] | 5846 | |
| 5847 | for (unsigned i=0, NumClasses=ImplementedClasses.size(); i<NumClasses; i++) { |
| 5848 | const ObjCInterfaceDecl *ID = ImplementedClasses[i]; |
| 5849 | assert(ID); |
| 5850 | if (ObjCImplementationDecl *IMP = ID->getImplementation()) |
| 5851 | // We are implementing a weak imported interface. Give it external linkage |
Fariborz Jahanian | bc94c94 | 2014-07-15 17:14:34 +0000 | [diff] [blame] | 5852 | if (ID->isWeakImported() && !IMP->isWeakImported()) { |
Fariborz Jahanian | f322f2f | 2014-03-11 00:25:05 +0000 | [diff] [blame] | 5853 | DefinedClasses[i]->setLinkage(llvm::GlobalVariable::ExternalLinkage); |
Fariborz Jahanian | bc94c94 | 2014-07-15 17:14:34 +0000 | [diff] [blame] | 5854 | DefinedMetaClasses[i]->setLinkage(llvm::GlobalVariable::ExternalLinkage); |
| 5855 | } |
Fariborz Jahanian | f322f2f | 2014-03-11 00:25:05 +0000 | [diff] [blame] | 5856 | } |
Rafael Espindola | 8b27bdb | 2014-11-06 13:30:38 +0000 | [diff] [blame] | 5857 | |
| 5858 | AddModuleClassList(DefinedClasses, "OBJC_LABEL_CLASS_$", |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 5859 | "__DATA, __objc_classlist, regular, no_dead_strip"); |
Rafael Espindola | 554256c | 2014-02-26 22:25:45 +0000 | [diff] [blame] | 5860 | |
Rafael Espindola | 8b27bdb | 2014-11-06 13:30:38 +0000 | [diff] [blame] | 5861 | AddModuleClassList(DefinedNonLazyClasses, "OBJC_LABEL_NONLAZY_CLASS_$", |
Daniel Dunbar | 9a017d7 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 5862 | "__DATA, __objc_nlclslist, regular, no_dead_strip"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5863 | |
Fariborz Jahanian | 279abd3 | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 5864 | // Build list of all implemented category addresses in array |
| 5865 | // L_OBJC_LABEL_CATEGORY_$. |
Rafael Espindola | 8b27bdb | 2014-11-06 13:30:38 +0000 | [diff] [blame] | 5866 | AddModuleClassList(DefinedCategories, "OBJC_LABEL_CATEGORY_$", |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 5867 | "__DATA, __objc_catlist, regular, no_dead_strip"); |
Rafael Espindola | 8b27bdb | 2014-11-06 13:30:38 +0000 | [diff] [blame] | 5868 | AddModuleClassList(DefinedNonLazyCategories, "OBJC_LABEL_NONLAZY_CATEGORY_$", |
Daniel Dunbar | 9a017d7 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 5869 | "__DATA, __objc_nlcatlist, regular, no_dead_strip"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5870 | |
Daniel Dunbar | 5e63927 | 2010-04-25 20:39:01 +0000 | [diff] [blame] | 5871 | EmitImageInfo(); |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5872 | } |
| 5873 | |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5874 | /// isVTableDispatchedSelector - Returns true if SEL is not in the list of |
| 5875 | /// VTableDispatchMethods; false otherwise. What this means is that |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5876 | /// except for the 19 selectors in the list, we generate 32bit-style |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 5877 | /// message dispatch call for all the rest. |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5878 | bool CGObjCNonFragileABIMac::isVTableDispatchedSelector(Selector Sel) { |
| 5879 | // At various points we've experimented with using vtable-based |
| 5880 | // dispatch for all methods. |
Daniel Dunbar | fca18c1b4 | 2010-04-24 17:56:46 +0000 | [diff] [blame] | 5881 | switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) { |
Daniel Dunbar | fca18c1b4 | 2010-04-24 17:56:46 +0000 | [diff] [blame] | 5882 | case CodeGenOptions::Legacy: |
Fariborz Jahanian | dfb3983 | 2010-04-19 17:53:30 +0000 | [diff] [blame] | 5883 | return false; |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5884 | case CodeGenOptions::NonLegacy: |
| 5885 | return true; |
Daniel Dunbar | fca18c1b4 | 2010-04-24 17:56:46 +0000 | [diff] [blame] | 5886 | case CodeGenOptions::Mixed: |
| 5887 | break; |
| 5888 | } |
| 5889 | |
| 5890 | // If so, see whether this selector is in the white-list of things which must |
| 5891 | // use the new dispatch convention. We lazily build a dense set for this. |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5892 | if (VTableDispatchMethods.empty()) { |
| 5893 | VTableDispatchMethods.insert(GetNullarySelector("alloc")); |
| 5894 | VTableDispatchMethods.insert(GetNullarySelector("class")); |
| 5895 | VTableDispatchMethods.insert(GetNullarySelector("self")); |
| 5896 | VTableDispatchMethods.insert(GetNullarySelector("isFlipped")); |
| 5897 | VTableDispatchMethods.insert(GetNullarySelector("length")); |
| 5898 | VTableDispatchMethods.insert(GetNullarySelector("count")); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5899 | |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5900 | // These are vtable-based if GC is disabled. |
| 5901 | // Optimistically use vtable dispatch for hybrid compiles. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5902 | if (CGM.getLangOpts().getGC() != LangOptions::GCOnly) { |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5903 | VTableDispatchMethods.insert(GetNullarySelector("retain")); |
| 5904 | VTableDispatchMethods.insert(GetNullarySelector("release")); |
| 5905 | VTableDispatchMethods.insert(GetNullarySelector("autorelease")); |
| 5906 | } |
| 5907 | |
| 5908 | VTableDispatchMethods.insert(GetUnarySelector("allocWithZone")); |
| 5909 | VTableDispatchMethods.insert(GetUnarySelector("isKindOfClass")); |
| 5910 | VTableDispatchMethods.insert(GetUnarySelector("respondsToSelector")); |
| 5911 | VTableDispatchMethods.insert(GetUnarySelector("objectForKey")); |
| 5912 | VTableDispatchMethods.insert(GetUnarySelector("objectAtIndex")); |
| 5913 | VTableDispatchMethods.insert(GetUnarySelector("isEqualToString")); |
| 5914 | VTableDispatchMethods.insert(GetUnarySelector("isEqual")); |
| 5915 | |
| 5916 | // These are vtable-based if GC is enabled. |
| 5917 | // Optimistically use vtable dispatch for hybrid compiles. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5918 | if (CGM.getLangOpts().getGC() != LangOptions::NonGC) { |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5919 | VTableDispatchMethods.insert(GetNullarySelector("hash")); |
| 5920 | VTableDispatchMethods.insert(GetUnarySelector("addObject")); |
| 5921 | |
| 5922 | // "countByEnumeratingWithState:objects:count" |
| 5923 | IdentifierInfo *KeyIdents[] = { |
| 5924 | &CGM.getContext().Idents.get("countByEnumeratingWithState"), |
| 5925 | &CGM.getContext().Idents.get("objects"), |
| 5926 | &CGM.getContext().Idents.get("count") |
| 5927 | }; |
| 5928 | VTableDispatchMethods.insert( |
| 5929 | CGM.getContext().Selectors.getSelector(3, KeyIdents)); |
| 5930 | } |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 5931 | } |
Daniel Dunbar | fca18c1b4 | 2010-04-24 17:56:46 +0000 | [diff] [blame] | 5932 | |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5933 | return VTableDispatchMethods.count(Sel); |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 5934 | } |
| 5935 | |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5936 | /// BuildClassRoTInitializer - generate meta-data for: |
| 5937 | /// struct _class_ro_t { |
| 5938 | /// uint32_t const flags; |
| 5939 | /// uint32_t const instanceStart; |
| 5940 | /// uint32_t const instanceSize; |
| 5941 | /// uint32_t const reserved; // only when building for 64bit targets |
| 5942 | /// const uint8_t * const ivarLayout; |
| 5943 | /// const char *const name; |
| 5944 | /// const struct _method_list_t * const baseMethods; |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5945 | /// const struct _protocol_list_t *const baseProtocols; |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5946 | /// const struct _ivar_list_t *const ivars; |
| 5947 | /// const uint8_t * const weakIvarLayout; |
| 5948 | /// const struct _prop_list_t * const properties; |
| 5949 | /// } |
| 5950 | /// |
| 5951 | llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5952 | unsigned flags, |
| 5953 | unsigned InstanceStart, |
| 5954 | unsigned InstanceSize, |
| 5955 | const ObjCImplementationDecl *ID) { |
Fariborz Jahanian | 451b92a | 2014-07-16 16:16:04 +0000 | [diff] [blame] | 5956 | std::string ClassName = ID->getObjCRuntimeNameAsString(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5957 | |
John McCall | 3fd13f06 | 2015-10-21 18:06:47 +0000 | [diff] [blame] | 5958 | CharUnits beginInstance = CharUnits::fromQuantity(InstanceStart); |
| 5959 | CharUnits endInstance = CharUnits::fromQuantity(InstanceSize); |
| 5960 | |
John McCall | 460ce58 | 2015-10-22 18:38:17 +0000 | [diff] [blame] | 5961 | bool hasMRCWeak = false; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5962 | if (CGM.getLangOpts().ObjCAutoRefCount) |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5963 | flags |= NonFragileABI_Class_CompiledByARC; |
John McCall | 460ce58 | 2015-10-22 18:38:17 +0000 | [diff] [blame] | 5964 | else if ((hasMRCWeak = hasMRCWeakIvars(CGM, ID))) |
| 5965 | flags |= NonFragileABI_Class_HasMRCWeakIvars; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5966 | |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 5967 | ConstantInitBuilder builder(CGM); |
| 5968 | auto values = builder.beginStruct(ObjCTypes.ClassRonfABITy); |
| 5969 | |
| 5970 | values.addInt(ObjCTypes.IntTy, flags); |
| 5971 | values.addInt(ObjCTypes.IntTy, InstanceStart); |
| 5972 | values.addInt(ObjCTypes.IntTy, InstanceSize); |
| 5973 | values.add((flags & NonFragileABI_Class_Meta) |
| 5974 | ? GetIvarLayoutName(nullptr, ObjCTypes) |
| 5975 | : BuildStrongIvarLayout(ID, beginInstance, endInstance)); |
| 5976 | values.add(GetClassName(ID->getObjCRuntimeNameAsString())); |
| 5977 | |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5978 | // const struct _method_list_t * const baseMethods; |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 5979 | SmallVector<const ObjCMethodDecl*, 16> methods; |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5980 | if (flags & NonFragileABI_Class_Meta) { |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 5981 | for (const auto *MD : ID->class_methods()) |
| 5982 | methods.push_back(MD); |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5983 | } else { |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 5984 | for (const auto *MD : ID->instance_methods()) |
| 5985 | methods.push_back(MD); |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 5986 | |
Aaron Ballman | d85eff4 | 2014-03-14 15:02:45 +0000 | [diff] [blame] | 5987 | for (const auto *PID : ID->property_impls()) { |
Fariborz Jahanian | d27a820 | 2009-01-28 22:46:49 +0000 | [diff] [blame] | 5988 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){ |
| 5989 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5990 | |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 5991 | if (auto MD = PD->getGetterMethodDecl()) |
| 5992 | if (GetMethodDefinition(MD)) |
| 5993 | methods.push_back(MD); |
| 5994 | if (auto MD = PD->getSetterMethodDecl()) |
| 5995 | if (GetMethodDefinition(MD)) |
| 5996 | methods.push_back(MD); |
Fariborz Jahanian | d27a820 | 2009-01-28 22:46:49 +0000 | [diff] [blame] | 5997 | } |
| 5998 | } |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5999 | } |
Saleem Abdulrasool | d48b0a3 | 2016-10-24 20:47:58 +0000 | [diff] [blame] | 6000 | |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 6001 | values.add(emitMethodList(ID->getObjCRuntimeNameAsString(), |
| 6002 | (flags & NonFragileABI_Class_Meta) |
| 6003 | ? MethodListType::ClassMethods |
| 6004 | : MethodListType::InstanceMethods, |
| 6005 | methods)); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6006 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6007 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
| 6008 | assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer"); |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 6009 | values.add(EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_" |
Fariborz Jahanian | 451b92a | 2014-07-16 16:16:04 +0000 | [diff] [blame] | 6010 | + OID->getObjCRuntimeNameAsString(), |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 6011 | OID->all_referenced_protocol_begin(), |
| 6012 | OID->all_referenced_protocol_end())); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6013 | |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 6014 | if (flags & NonFragileABI_Class_Meta) { |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 6015 | values.addNullPointer(ObjCTypes.IvarListnfABIPtrTy); |
| 6016 | values.add(GetIvarLayoutName(nullptr, ObjCTypes)); |
| 6017 | values.add(EmitPropertyList( |
Manman Ren | ad0e791 | 2016-01-29 19:22:54 +0000 | [diff] [blame] | 6018 | "\01l_OBJC_$_CLASS_PROP_LIST_" + ID->getObjCRuntimeNameAsString(), |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 6019 | ID, ID->getClassInterface(), ObjCTypes, true)); |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 6020 | } else { |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 6021 | values.add(EmitIvarList(ID)); |
| 6022 | values.add(BuildWeakIvarLayout(ID, beginInstance, endInstance, hasMRCWeak)); |
| 6023 | values.add(EmitPropertyList( |
Manman Ren | ad0e791 | 2016-01-29 19:22:54 +0000 | [diff] [blame] | 6024 | "\01l_OBJC_$_PROP_LIST_" + ID->getObjCRuntimeNameAsString(), |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 6025 | ID, ID->getClassInterface(), ObjCTypes, false)); |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 6026 | } |
Saleem Abdulrasool | 3c628af | 2016-10-25 21:43:28 +0000 | [diff] [blame] | 6027 | |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 6028 | llvm::SmallString<64> roLabel; |
| 6029 | llvm::raw_svector_ostream(roLabel) |
Saleem Abdulrasool | 3c628af | 2016-10-25 21:43:28 +0000 | [diff] [blame] | 6030 | << ((flags & NonFragileABI_Class_Meta) ? "\01l_OBJC_METACLASS_RO_$_" |
| 6031 | : "\01l_OBJC_CLASS_RO_$_") |
| 6032 | << ClassName; |
| 6033 | |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 6034 | llvm::GlobalVariable *CLASS_RO_GV = |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 6035 | values.finishAndCreateGlobal(roLabel, CGM.getPointerAlign(), |
| 6036 | /*constant*/ false, |
| 6037 | llvm::GlobalValue::PrivateLinkage); |
Saleem Abdulrasool | 3c628af | 2016-10-25 21:43:28 +0000 | [diff] [blame] | 6038 | if (CGM.getTriple().isOSBinFormatMachO()) |
| 6039 | CLASS_RO_GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 6040 | return CLASS_RO_GV; |
| 6041 | } |
| 6042 | |
| 6043 | /// BuildClassMetaData - This routine defines that to-level meta-data |
| 6044 | /// for the given ClassName for: |
| 6045 | /// struct _class_t { |
| 6046 | /// struct _class_t *isa; |
| 6047 | /// struct _class_t * const superclass; |
| 6048 | /// void *cache; |
| 6049 | /// IMP *vtable; |
| 6050 | /// struct class_ro_t *ro; |
| 6051 | /// } |
| 6052 | /// |
Rafael Espindola | 554256c | 2014-02-26 22:25:45 +0000 | [diff] [blame] | 6053 | llvm::GlobalVariable *CGObjCNonFragileABIMac::BuildClassMetaData( |
Fariborz Jahanian | 451b92a | 2014-07-16 16:16:04 +0000 | [diff] [blame] | 6054 | const std::string &ClassName, llvm::Constant *IsAGV, llvm::Constant *SuperClassGV, |
Rafael Espindola | 554256c | 2014-02-26 22:25:45 +0000 | [diff] [blame] | 6055 | llvm::Constant *ClassRoGV, bool HiddenVisibility, bool Weak) { |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 6056 | ConstantInitBuilder builder(CGM); |
| 6057 | auto values = builder.beginStruct(ObjCTypes.ClassnfABITy); |
| 6058 | values.add(IsAGV); |
| 6059 | if (SuperClassGV) { |
| 6060 | values.add(SuperClassGV); |
| 6061 | } else { |
| 6062 | values.addNullPointer(ObjCTypes.ClassnfABIPtrTy); |
| 6063 | } |
| 6064 | values.add(ObjCEmptyCacheVar); |
| 6065 | values.add(ObjCEmptyVtableVar); |
| 6066 | values.add(ClassRoGV); |
| 6067 | |
Rafael Espindola | 554256c | 2014-02-26 22:25:45 +0000 | [diff] [blame] | 6068 | llvm::GlobalVariable *GV = GetClassGlobal(ClassName, Weak); |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 6069 | values.finishAndSetAsInitializer(GV); |
| 6070 | |
Saleem Abdulrasool | 3c628af | 2016-10-25 21:43:28 +0000 | [diff] [blame] | 6071 | if (CGM.getTriple().isOSBinFormatMachO()) |
| 6072 | GV->setSection("__DATA, __objc_data"); |
Fariborz Jahanian | c22f236 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 6073 | GV->setAlignment( |
Saleem Abdulrasool | 3c628af | 2016-10-25 21:43:28 +0000 | [diff] [blame] | 6074 | CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ClassnfABITy)); |
Saleem Abdulrasool | 7093e21 | 2016-07-17 22:27:44 +0000 | [diff] [blame] | 6075 | if (!CGM.getTriple().isOSBinFormatCOFF()) |
| 6076 | if (HiddenVisibility) |
| 6077 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Fariborz Jahanian | 4723fb7 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 6078 | return GV; |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 6079 | } |
| 6080 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6081 | bool |
Fariborz Jahanian | a6bed83 | 2009-05-21 01:03:45 +0000 | [diff] [blame] | 6082 | CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 6083 | return OD->getClassMethod(GetNullarySelector("load")) != nullptr; |
Daniel Dunbar | 9a017d7 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 6084 | } |
| 6085 | |
Daniel Dunbar | 96120237 | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 6086 | void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID, |
Daniel Dunbar | 554fd79 | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 6087 | uint32_t &InstanceStart, |
| 6088 | uint32_t &InstanceSize) { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6089 | const ASTRecordLayout &RL = |
Daniel Dunbar | 9252ee1 | 2009-05-04 21:26:30 +0000 | [diff] [blame] | 6090 | CGM.getContext().getASTObjCImplementationLayout(OID); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6091 | |
Daniel Dunbar | 9b042e0 | 2009-05-04 23:23:09 +0000 | [diff] [blame] | 6092 | // InstanceSize is really instance end. |
Ken Dyck | d5090c1 | 2011-02-11 02:20:09 +0000 | [diff] [blame] | 6093 | InstanceSize = RL.getDataSize().getQuantity(); |
Daniel Dunbar | 9b042e0 | 2009-05-04 23:23:09 +0000 | [diff] [blame] | 6094 | |
| 6095 | // If there are no fields, the start is the same as the end. |
| 6096 | if (!RL.getFieldCount()) |
| 6097 | InstanceStart = InstanceSize; |
| 6098 | else |
Ken Dyck | c5ca876 | 2011-04-14 00:43:09 +0000 | [diff] [blame] | 6099 | InstanceStart = RL.getFieldOffset(0) / CGM.getContext().getCharWidth(); |
Daniel Dunbar | 554fd79 | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 6100 | } |
| 6101 | |
Saleem Abdulrasool | 7093e21 | 2016-07-17 22:27:44 +0000 | [diff] [blame] | 6102 | static llvm::GlobalValue::DLLStorageClassTypes getStorage(CodeGenModule &CGM, |
| 6103 | StringRef Name) { |
| 6104 | IdentifierInfo &II = CGM.getContext().Idents.get(Name); |
| 6105 | TranslationUnitDecl *TUDecl = CGM.getContext().getTranslationUnitDecl(); |
| 6106 | DeclContext *DC = TranslationUnitDecl::castToDeclContext(TUDecl); |
| 6107 | |
| 6108 | const VarDecl *VD = nullptr; |
| 6109 | for (const auto &Result : DC->lookup(&II)) |
| 6110 | if ((VD = dyn_cast<VarDecl>(Result))) |
| 6111 | break; |
| 6112 | |
| 6113 | if (!VD) |
| 6114 | return llvm::GlobalValue::DLLImportStorageClass; |
| 6115 | if (VD->hasAttr<DLLExportAttr>()) |
| 6116 | return llvm::GlobalValue::DLLExportStorageClass; |
| 6117 | if (VD->hasAttr<DLLImportAttr>()) |
| 6118 | return llvm::GlobalValue::DLLImportStorageClass; |
| 6119 | return llvm::GlobalValue::DefaultStorageClass; |
| 6120 | } |
| 6121 | |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 6122 | void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) { |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 6123 | if (!ObjCEmptyCacheVar) { |
Saleem Abdulrasool | 7093e21 | 2016-07-17 22:27:44 +0000 | [diff] [blame] | 6124 | ObjCEmptyCacheVar = |
| 6125 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CacheTy, false, |
| 6126 | llvm::GlobalValue::ExternalLinkage, nullptr, |
| 6127 | "_objc_empty_cache"); |
| 6128 | if (CGM.getTriple().isOSBinFormatCOFF()) |
| 6129 | ObjCEmptyCacheVar->setDLLStorageClass(getStorage(CGM, "_objc_empty_cache")); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 6130 | |
Saleem Abdulrasool | 4f515a6 | 2016-07-13 02:58:44 +0000 | [diff] [blame] | 6131 | // Only OS X with deployment version <10.9 use the empty vtable symbol |
Fariborz Jahanian | 42d4955 | 2013-10-24 17:40:28 +0000 | [diff] [blame] | 6132 | const llvm::Triple &Triple = CGM.getTarget().getTriple(); |
Saleem Abdulrasool | 4f515a6 | 2016-07-13 02:58:44 +0000 | [diff] [blame] | 6133 | if (Triple.isMacOSX() && Triple.isMacOSXVersionLT(10, 9)) |
Saleem Abdulrasool | 7093e21 | 2016-07-17 22:27:44 +0000 | [diff] [blame] | 6134 | ObjCEmptyVtableVar = |
| 6135 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ImpnfABITy, false, |
| 6136 | llvm::GlobalValue::ExternalLinkage, nullptr, |
| 6137 | "_objc_empty_vtable"); |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 6138 | else |
| 6139 | ObjCEmptyVtableVar = |
| 6140 | llvm::ConstantPointerNull::get(ObjCTypes.ImpnfABITy->getPointerTo()); |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 6141 | } |
Saleem Abdulrasool | bc2d999 | 2016-07-16 22:42:04 +0000 | [diff] [blame] | 6142 | |
Daniel Dunbar | e3f5cfc | 2009-04-20 20:18:54 +0000 | [diff] [blame] | 6143 | // FIXME: Is this correct (that meta class size is never computed)? |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6144 | uint32_t InstanceStart = |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 6145 | CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ClassnfABITy); |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 6146 | uint32_t InstanceSize = InstanceStart; |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 6147 | uint32_t flags = NonFragileABI_Class_Meta; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6148 | |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 6149 | llvm::GlobalVariable *SuperClassGV, *IsAGV; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6150 | |
Saleem Abdulrasool | 10fd1ff | 2016-07-16 22:42:06 +0000 | [diff] [blame] | 6151 | StringRef ClassName = ID->getObjCRuntimeNameAsString(); |
Saleem Abdulrasool | bc2d999 | 2016-07-16 22:42:04 +0000 | [diff] [blame] | 6152 | const auto *CI = ID->getClassInterface(); |
| 6153 | assert(CI && "CGObjCNonFragileABIMac::GenerateClass - class is 0"); |
| 6154 | |
John McCall | 0d54a17 | 2012-10-17 04:53:31 +0000 | [diff] [blame] | 6155 | // Build the flags for the metaclass. |
Saleem Abdulrasool | 7093e21 | 2016-07-17 22:27:44 +0000 | [diff] [blame] | 6156 | bool classIsHidden = (CGM.getTriple().isOSBinFormatCOFF()) |
| 6157 | ? !CI->hasAttr<DLLExportAttr>() |
| 6158 | : CI->getVisibility() == HiddenVisibility; |
Fariborz Jahanian | 8220825 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 6159 | if (classIsHidden) |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 6160 | flags |= NonFragileABI_Class_Hidden; |
John McCall | 0d54a17 | 2012-10-17 04:53:31 +0000 | [diff] [blame] | 6161 | |
| 6162 | // FIXME: why is this flag set on the metaclass? |
| 6163 | // ObjC metaclasses have no fields and don't really get constructed. |
| 6164 | if (ID->hasNonZeroConstructors() || ID->hasDestructors()) { |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 6165 | flags |= NonFragileABI_Class_HasCXXStructors; |
John McCall | 0d54a17 | 2012-10-17 04:53:31 +0000 | [diff] [blame] | 6166 | if (!ID->hasNonZeroConstructors()) |
Saleem Abdulrasool | 7093e21 | 2016-07-17 22:27:44 +0000 | [diff] [blame] | 6167 | flags |= NonFragileABI_Class_HasCXXDestructorOnly; |
John McCall | 0d54a17 | 2012-10-17 04:53:31 +0000 | [diff] [blame] | 6168 | } |
| 6169 | |
Saleem Abdulrasool | bc2d999 | 2016-07-16 22:42:04 +0000 | [diff] [blame] | 6170 | if (!CI->getSuperClass()) { |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 6171 | // class is root |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 6172 | flags |= NonFragileABI_Class_Root; |
Saleem Abdulrasool | bc2d999 | 2016-07-16 22:42:04 +0000 | [diff] [blame] | 6173 | |
Saleem Abdulrasool | 10fd1ff | 2016-07-16 22:42:06 +0000 | [diff] [blame] | 6174 | SuperClassGV = GetClassGlobal((getClassSymbolPrefix() + ClassName).str(), |
| 6175 | CI->isWeakImported()); |
Saleem Abdulrasool | 7093e21 | 2016-07-17 22:27:44 +0000 | [diff] [blame] | 6176 | if (CGM.getTriple().isOSBinFormatCOFF()) |
| 6177 | if (CI->hasAttr<DLLImportAttr>()) |
| 6178 | SuperClassGV->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass); |
Saleem Abdulrasool | bc2d999 | 2016-07-16 22:42:04 +0000 | [diff] [blame] | 6179 | |
Saleem Abdulrasool | 10fd1ff | 2016-07-16 22:42:06 +0000 | [diff] [blame] | 6180 | IsAGV = GetClassGlobal((getMetaclassSymbolPrefix() + ClassName).str(), |
| 6181 | CI->isWeakImported()); |
Saleem Abdulrasool | 7093e21 | 2016-07-17 22:27:44 +0000 | [diff] [blame] | 6182 | if (CGM.getTriple().isOSBinFormatCOFF()) |
| 6183 | if (CI->hasAttr<DLLImportAttr>()) |
| 6184 | IsAGV->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass); |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 6185 | } else { |
Fariborz Jahanian | 4723fb7 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 6186 | // Has a root. Current class is not a root. |
Fariborz Jahanian | 03b300b | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 6187 | const ObjCInterfaceDecl *Root = ID->getClassInterface(); |
| 6188 | while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) |
| 6189 | Root = Super; |
Saleem Abdulrasool | bc2d999 | 2016-07-16 22:42:04 +0000 | [diff] [blame] | 6190 | |
| 6191 | const auto *Super = CI->getSuperClass(); |
Saleem Abdulrasool | 10fd1ff | 2016-07-16 22:42:06 +0000 | [diff] [blame] | 6192 | StringRef RootClassName = Root->getObjCRuntimeNameAsString(); |
| 6193 | StringRef SuperClassName = Super->getObjCRuntimeNameAsString(); |
Saleem Abdulrasool | bc2d999 | 2016-07-16 22:42:04 +0000 | [diff] [blame] | 6194 | |
Saleem Abdulrasool | 10fd1ff | 2016-07-16 22:42:06 +0000 | [diff] [blame] | 6195 | IsAGV = GetClassGlobal((getMetaclassSymbolPrefix() + RootClassName).str(), |
| 6196 | Root->isWeakImported()); |
Saleem Abdulrasool | 7093e21 | 2016-07-17 22:27:44 +0000 | [diff] [blame] | 6197 | if (CGM.getTriple().isOSBinFormatCOFF()) |
| 6198 | if (Root->hasAttr<DLLImportAttr>()) |
| 6199 | IsAGV->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass); |
Fariborz Jahanian | 451b92a | 2014-07-16 16:16:04 +0000 | [diff] [blame] | 6200 | |
Fariborz Jahanian | 03b300b | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 6201 | // work on super class metadata symbol. |
Saleem Abdulrasool | 10fd1ff | 2016-07-16 22:42:06 +0000 | [diff] [blame] | 6202 | SuperClassGV = |
| 6203 | GetClassGlobal((getMetaclassSymbolPrefix() + SuperClassName).str(), |
| 6204 | Super->isWeakImported()); |
Saleem Abdulrasool | 7093e21 | 2016-07-17 22:27:44 +0000 | [diff] [blame] | 6205 | if (CGM.getTriple().isOSBinFormatCOFF()) |
| 6206 | if (Super->hasAttr<DLLImportAttr>()) |
| 6207 | SuperClassGV->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass); |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 6208 | } |
Saleem Abdulrasool | bc2d999 | 2016-07-16 22:42:04 +0000 | [diff] [blame] | 6209 | |
| 6210 | llvm::GlobalVariable *CLASS_RO_GV = |
| 6211 | BuildClassRoTInitializer(flags, InstanceStart, InstanceSize, ID); |
| 6212 | |
Saleem Abdulrasool | bc2d999 | 2016-07-16 22:42:04 +0000 | [diff] [blame] | 6213 | llvm::GlobalVariable *MetaTClass = |
Saleem Abdulrasool | 10fd1ff | 2016-07-16 22:42:06 +0000 | [diff] [blame] | 6214 | BuildClassMetaData((getMetaclassSymbolPrefix() + ClassName).str(), IsAGV, |
| 6215 | SuperClassGV, CLASS_RO_GV, classIsHidden, |
| 6216 | CI->isWeakImported()); |
Saleem Abdulrasool | 7093e21 | 2016-07-17 22:27:44 +0000 | [diff] [blame] | 6217 | if (CGM.getTriple().isOSBinFormatCOFF()) |
| 6218 | if (CI->hasAttr<DLLExportAttr>()) |
| 6219 | MetaTClass->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass); |
Fariborz Jahanian | 6726055 | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 6220 | DefinedMetaClasses.push_back(MetaTClass); |
Daniel Dunbar | 15894b7 | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 6221 | |
Fariborz Jahanian | 4723fb7 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 6222 | // Metadata for the class |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 6223 | flags = 0; |
Fariborz Jahanian | 8220825 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 6224 | if (classIsHidden) |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 6225 | flags |= NonFragileABI_Class_Hidden; |
John McCall | 0d54a17 | 2012-10-17 04:53:31 +0000 | [diff] [blame] | 6226 | |
| 6227 | if (ID->hasNonZeroConstructors() || ID->hasDestructors()) { |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 6228 | flags |= NonFragileABI_Class_HasCXXStructors; |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6229 | |
John McCall | 0d54a17 | 2012-10-17 04:53:31 +0000 | [diff] [blame] | 6230 | // Set a flag to enable a runtime optimization when a class has |
| 6231 | // fields that require destruction but which don't require |
| 6232 | // anything except zero-initialization during construction. This |
| 6233 | // is most notably true of __strong and __weak types, but you can |
| 6234 | // also imagine there being C++ types with non-trivial default |
| 6235 | // constructors that merely set all fields to null. |
| 6236 | if (!ID->hasNonZeroConstructors()) |
| 6237 | flags |= NonFragileABI_Class_HasCXXDestructorOnly; |
| 6238 | } |
| 6239 | |
Saleem Abdulrasool | bc2d999 | 2016-07-16 22:42:04 +0000 | [diff] [blame] | 6240 | if (hasObjCExceptionAttribute(CGM.getContext(), CI)) |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 6241 | flags |= NonFragileABI_Class_Exception; |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6242 | |
Saleem Abdulrasool | bc2d999 | 2016-07-16 22:42:04 +0000 | [diff] [blame] | 6243 | if (!CI->getSuperClass()) { |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 6244 | flags |= NonFragileABI_Class_Root; |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 6245 | SuperClassGV = nullptr; |
Chris Lattner | b433b27 | 2009-04-19 06:02:28 +0000 | [diff] [blame] | 6246 | } else { |
Fariborz Jahanian | 4723fb7 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 6247 | // Has a root. Current class is not a root. |
Saleem Abdulrasool | bc2d999 | 2016-07-16 22:42:04 +0000 | [diff] [blame] | 6248 | const auto *Super = CI->getSuperClass(); |
Saleem Abdulrasool | 10fd1ff | 2016-07-16 22:42:06 +0000 | [diff] [blame] | 6249 | StringRef SuperClassName = Super->getObjCRuntimeNameAsString(); |
Saleem Abdulrasool | bc2d999 | 2016-07-16 22:42:04 +0000 | [diff] [blame] | 6250 | |
Saleem Abdulrasool | 10fd1ff | 2016-07-16 22:42:06 +0000 | [diff] [blame] | 6251 | SuperClassGV = |
| 6252 | GetClassGlobal((getClassSymbolPrefix() + SuperClassName).str(), |
| 6253 | Super->isWeakImported()); |
Saleem Abdulrasool | 7093e21 | 2016-07-17 22:27:44 +0000 | [diff] [blame] | 6254 | if (CGM.getTriple().isOSBinFormatCOFF()) |
| 6255 | if (Super->hasAttr<DLLImportAttr>()) |
| 6256 | SuperClassGV->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass); |
Fariborz Jahanian | 4723fb7 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 6257 | } |
Saleem Abdulrasool | bc2d999 | 2016-07-16 22:42:04 +0000 | [diff] [blame] | 6258 | |
Daniel Dunbar | 96120237 | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 6259 | GetClassSizeInfo(ID, InstanceStart, InstanceSize); |
Saleem Abdulrasool | bc2d999 | 2016-07-16 22:42:04 +0000 | [diff] [blame] | 6260 | CLASS_RO_GV = |
| 6261 | BuildClassRoTInitializer(flags, InstanceStart, InstanceSize, ID); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6262 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6263 | llvm::GlobalVariable *ClassMD = |
Saleem Abdulrasool | 10fd1ff | 2016-07-16 22:42:06 +0000 | [diff] [blame] | 6264 | BuildClassMetaData((getClassSymbolPrefix() + ClassName).str(), MetaTClass, |
| 6265 | SuperClassGV, CLASS_RO_GV, classIsHidden, |
| 6266 | CI->isWeakImported()); |
Saleem Abdulrasool | 7093e21 | 2016-07-17 22:27:44 +0000 | [diff] [blame] | 6267 | if (CGM.getTriple().isOSBinFormatCOFF()) |
| 6268 | if (CI->hasAttr<DLLExportAttr>()) |
| 6269 | ClassMD->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass); |
Fariborz Jahanian | 279abd3 | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 6270 | DefinedClasses.push_back(ClassMD); |
Saleem Abdulrasool | bc2d999 | 2016-07-16 22:42:04 +0000 | [diff] [blame] | 6271 | ImplementedClasses.push_back(CI); |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6272 | |
Daniel Dunbar | 9a017d7 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 6273 | // Determine if this class is also "non-lazy". |
| 6274 | if (ImplementationIsNonLazy(ID)) |
| 6275 | DefinedNonLazyClasses.push_back(ClassMD); |
| 6276 | |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6277 | // Force the definition of the EHType if necessary. |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 6278 | if (flags & NonFragileABI_Class_Exception) |
Saleem Abdulrasool | bc2d999 | 2016-07-16 22:42:04 +0000 | [diff] [blame] | 6279 | GetInterfaceEHType(CI, true); |
Fariborz Jahanian | c057794 | 2011-04-22 22:02:28 +0000 | [diff] [blame] | 6280 | // Make sure method definition entries are all clear for next implementation. |
| 6281 | MethodDefinitions.clear(); |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 6282 | } |
| 6283 | |
Fariborz Jahanian | 097feda | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 6284 | /// GenerateProtocolRef - This routine is called to generate code for |
| 6285 | /// a protocol reference expression; as in: |
| 6286 | /// @code |
| 6287 | /// @protocol(Proto1); |
| 6288 | /// @endcode |
| 6289 | /// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1 |
| 6290 | /// which will hold address of the protocol meta-data. |
| 6291 | /// |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 6292 | llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CodeGenFunction &CGF, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6293 | const ObjCProtocolDecl *PD) { |
| 6294 | |
Fariborz Jahanian | 464423d | 2009-04-10 18:47:34 +0000 | [diff] [blame] | 6295 | // This routine is called for @protocol only. So, we must build definition |
| 6296 | // of protocol's meta-data (not a reference to it!) |
| 6297 | // |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6298 | llvm::Constant *Init = |
| 6299 | llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD), |
Douglas Gregor | 020de32 | 2012-01-17 18:36:30 +0000 | [diff] [blame] | 6300 | ObjCTypes.getExternalProtocolPtrTy()); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6301 | |
Fariborz Jahanian | 097feda | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 6302 | std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_"); |
Fariborz Jahanian | 451b92a | 2014-07-16 16:16:04 +0000 | [diff] [blame] | 6303 | ProtocolName += PD->getObjCRuntimeNameAsString(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6304 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6305 | CharUnits Align = CGF.getPointerAlign(); |
| 6306 | |
Fariborz Jahanian | 097feda | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 6307 | llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName); |
| 6308 | if (PTGV) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6309 | return CGF.Builder.CreateAlignedLoad(PTGV, Align); |
Fariborz Jahanian | 097feda | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 6310 | PTGV = new llvm::GlobalVariable( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6311 | CGM.getModule(), |
| 6312 | Init->getType(), false, |
Rafael Espindola | 70efc5b | 2014-03-06 18:54:12 +0000 | [diff] [blame] | 6313 | llvm::GlobalValue::WeakAnyLinkage, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6314 | Init, |
| 6315 | ProtocolName); |
Fariborz Jahanian | 097feda | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 6316 | PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip"); |
Rafael Espindola | 70efc5b | 2014-03-06 18:54:12 +0000 | [diff] [blame] | 6317 | PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6318 | PTGV->setAlignment(Align.getQuantity()); |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 6319 | CGM.addCompilerUsedGlobal(PTGV); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6320 | return CGF.Builder.CreateAlignedLoad(PTGV, Align); |
Fariborz Jahanian | 097feda | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 6321 | } |
| 6322 | |
Fariborz Jahanian | 0c8d060 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 6323 | /// GenerateCategory - Build metadata for a category implementation. |
| 6324 | /// struct _category_t { |
| 6325 | /// const char * const name; |
| 6326 | /// struct _class_t *const cls; |
| 6327 | /// const struct _method_list_t * const instance_methods; |
| 6328 | /// const struct _method_list_t * const class_methods; |
| 6329 | /// const struct _protocol_list_t * const protocols; |
| 6330 | /// const struct _prop_list_t * const properties; |
Manman Ren | 96df0b3 | 2016-01-29 23:45:01 +0000 | [diff] [blame] | 6331 | /// const struct _prop_list_t * const class_properties; |
Manman Ren | 42ff390 | 2016-02-24 17:49:50 +0000 | [diff] [blame] | 6332 | /// const uint32_t size; |
Fariborz Jahanian | 0c8d060 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 6333 | /// } |
| 6334 | /// |
Daniel Dunbar | 9a017d7 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 6335 | void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) { |
Fariborz Jahanian | 0c8d060 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 6336 | const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); |
Fariborz Jahanian | 2612e14 | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 6337 | const char *Prefix = "\01l_OBJC_$_CATEGORY_"; |
Fariborz Jahanian | 451b92a | 2014-07-16 16:16:04 +0000 | [diff] [blame] | 6338 | |
| 6339 | llvm::SmallString<64> ExtCatName(Prefix); |
| 6340 | ExtCatName += Interface->getObjCRuntimeNameAsString(); |
| 6341 | ExtCatName += "_$_"; |
| 6342 | ExtCatName += OCD->getNameAsString(); |
| 6343 | |
| 6344 | llvm::SmallString<64> ExtClassName(getClassSymbolPrefix()); |
| 6345 | ExtClassName += Interface->getObjCRuntimeNameAsString(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6346 | |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 6347 | ConstantInitBuilder builder(CGM); |
| 6348 | auto values = builder.beginStruct(ObjCTypes.CategorynfABITy); |
| 6349 | values.add(GetClassName(OCD->getIdentifier()->getName())); |
Fariborz Jahanian | 0c8d060 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 6350 | // meta-class entry symbol |
Rafael Espindola | 554256c | 2014-02-26 22:25:45 +0000 | [diff] [blame] | 6351 | llvm::GlobalVariable *ClassGV = |
Fariborz Jahanian | 451b92a | 2014-07-16 16:16:04 +0000 | [diff] [blame] | 6352 | GetClassGlobal(ExtClassName.str(), Interface->isWeakImported()); |
Rafael Espindola | 554256c | 2014-02-26 22:25:45 +0000 | [diff] [blame] | 6353 | |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 6354 | values.add(ClassGV); |
| 6355 | std::string listName = |
Saleem Abdulrasool | 209150a | 2016-10-24 21:25:57 +0000 | [diff] [blame] | 6356 | (Interface->getObjCRuntimeNameAsString() + "_$_" + OCD->getName()).str(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6357 | |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 6358 | SmallVector<const ObjCMethodDecl *, 16> instanceMethods; |
| 6359 | SmallVector<const ObjCMethodDecl *, 8> classMethods; |
| 6360 | for (const auto *MD : OCD->methods()) { |
| 6361 | if (MD->isInstanceMethod()) { |
| 6362 | instanceMethods.push_back(MD); |
| 6363 | } else { |
| 6364 | classMethods.push_back(MD); |
| 6365 | } |
| 6366 | } |
Fariborz Jahanian | 2612e14 | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 6367 | |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 6368 | values.add(emitMethodList(listName, MethodListType::CategoryInstanceMethods, |
| 6369 | instanceMethods)); |
| 6370 | values.add(emitMethodList(listName, MethodListType::CategoryClassMethods, |
| 6371 | classMethods)); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6372 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6373 | const ObjCCategoryDecl *Category = |
Fariborz Jahanian | 066347e | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 6374 | Interface->FindCategoryDeclaration(OCD->getIdentifier()); |
Fariborz Jahanian | d8fc105 | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 6375 | if (Category) { |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 6376 | SmallString<256> ExtName; |
Fariborz Jahanian | 451b92a | 2014-07-16 16:16:04 +0000 | [diff] [blame] | 6377 | llvm::raw_svector_ostream(ExtName) << Interface->getObjCRuntimeNameAsString() << "_$_" |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 6378 | << OCD->getName(); |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 6379 | values.add(EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_" |
Fariborz Jahanian | 451b92a | 2014-07-16 16:16:04 +0000 | [diff] [blame] | 6380 | + Interface->getObjCRuntimeNameAsString() + "_$_" |
| 6381 | + Category->getName(), |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 6382 | Category->protocol_begin(), |
| 6383 | Category->protocol_end())); |
| 6384 | values.add(EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(), |
| 6385 | OCD, Category, ObjCTypes, false)); |
| 6386 | values.add(EmitPropertyList("\01l_OBJC_$_CLASS_PROP_LIST_" + ExtName.str(), |
| 6387 | OCD, Category, ObjCTypes, true)); |
Mike Stump | 658fe02 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 6388 | } else { |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 6389 | values.addNullPointer(ObjCTypes.ProtocolListnfABIPtrTy); |
| 6390 | values.addNullPointer(ObjCTypes.PropertyListPtrTy); |
| 6391 | values.addNullPointer(ObjCTypes.PropertyListPtrTy); |
Fariborz Jahanian | d8fc105 | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 6392 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6393 | |
Manman Ren | 42ff390 | 2016-02-24 17:49:50 +0000 | [diff] [blame] | 6394 | unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.CategorynfABITy); |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 6395 | values.addInt(ObjCTypes.IntTy, Size); |
Manman Ren | 42ff390 | 2016-02-24 17:49:50 +0000 | [diff] [blame] | 6396 | |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 6397 | llvm::GlobalVariable *GCATV = |
| 6398 | values.finishAndCreateGlobal(ExtCatName.str(), CGM.getPointerAlign(), |
| 6399 | /*constant*/ false, |
| 6400 | llvm::GlobalValue::PrivateLinkage); |
Saleem Abdulrasool | 3c628af | 2016-10-25 21:43:28 +0000 | [diff] [blame] | 6401 | if (CGM.getTriple().isOSBinFormatMachO()) |
| 6402 | GCATV->setSection("__DATA, __objc_const"); |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 6403 | CGM.addCompilerUsedGlobal(GCATV); |
Fariborz Jahanian | 0c8d060 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 6404 | DefinedCategories.push_back(GCATV); |
Daniel Dunbar | 9a017d7 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 6405 | |
| 6406 | // Determine if this category is also "non-lazy". |
| 6407 | if (ImplementationIsNonLazy(OCD)) |
| 6408 | DefinedNonLazyCategories.push_back(GCATV); |
Fariborz Jahanian | c057794 | 2011-04-22 22:02:28 +0000 | [diff] [blame] | 6409 | // method definition entries must be clear for next implementation. |
| 6410 | MethodDefinitions.clear(); |
Fariborz Jahanian | 0c8d060 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 6411 | } |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 6412 | |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 6413 | /// emitMethodConstant - Return a struct objc_method constant. If |
| 6414 | /// forProtocol is true, the implementation will be null; otherwise, |
| 6415 | /// the method must have a definition registered with the runtime. |
| 6416 | /// |
| 6417 | /// struct _objc_method { |
| 6418 | /// SEL _cmd; |
| 6419 | /// char *method_type; |
| 6420 | /// char *_imp; |
| 6421 | /// } |
| 6422 | void CGObjCNonFragileABIMac::emitMethodConstant(ConstantArrayBuilder &builder, |
| 6423 | const ObjCMethodDecl *MD, |
| 6424 | bool forProtocol) { |
| 6425 | auto method = builder.beginStruct(ObjCTypes.MethodTy); |
| 6426 | method.addBitCast(GetMethodVarName(MD->getSelector()), |
| 6427 | ObjCTypes.SelectorPtrTy); |
| 6428 | method.add(GetMethodVarType(MD)); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6429 | |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 6430 | if (forProtocol) { |
| 6431 | // Protocol methods have no implementation. So, this entry is always NULL. |
| 6432 | method.addNullPointer(ObjCTypes.Int8PtrTy); |
| 6433 | } else { |
| 6434 | llvm::Function *fn = GetMethodDefinition(MD); |
| 6435 | assert(fn && "no definition for method?"); |
| 6436 | method.addBitCast(fn, ObjCTypes.Int8PtrTy); |
| 6437 | } |
| 6438 | |
| 6439 | method.finishAndAddTo(builder); |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 6440 | } |
| 6441 | |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 6442 | /// Build meta-data for method declarations. |
| 6443 | /// |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 6444 | /// struct _method_list_t { |
| 6445 | /// uint32_t entsize; // sizeof(struct _objc_method) |
| 6446 | /// uint32_t method_count; |
| 6447 | /// struct _objc_method method_list[method_count]; |
| 6448 | /// } |
| 6449 | /// |
Bill Wendling | cb5b5ff | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 6450 | llvm::Constant * |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 6451 | CGObjCNonFragileABIMac::emitMethodList(Twine name, MethodListType kind, |
| 6452 | ArrayRef<const ObjCMethodDecl *> methods) { |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 6453 | // Return null for empty list. |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 6454 | if (methods.empty()) |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 6455 | return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6456 | |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 6457 | StringRef prefix; |
| 6458 | bool forProtocol; |
| 6459 | switch (kind) { |
Saleem Abdulrasool | d48b0a3 | 2016-10-24 20:47:58 +0000 | [diff] [blame] | 6460 | case MethodListType::CategoryInstanceMethods: |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 6461 | prefix = "\01l_OBJC_$_CATEGORY_INSTANCE_METHODS_"; |
| 6462 | forProtocol = false; |
Saleem Abdulrasool | d48b0a3 | 2016-10-24 20:47:58 +0000 | [diff] [blame] | 6463 | break; |
| 6464 | case MethodListType::CategoryClassMethods: |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 6465 | prefix = "\01l_OBJC_$_CATEGORY_CLASS_METHODS_"; |
| 6466 | forProtocol = false; |
Saleem Abdulrasool | d48b0a3 | 2016-10-24 20:47:58 +0000 | [diff] [blame] | 6467 | break; |
| 6468 | case MethodListType::InstanceMethods: |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 6469 | prefix = "\01l_OBJC_$_INSTANCE_METHODS_"; |
| 6470 | forProtocol = false; |
Saleem Abdulrasool | d48b0a3 | 2016-10-24 20:47:58 +0000 | [diff] [blame] | 6471 | break; |
| 6472 | case MethodListType::ClassMethods: |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 6473 | prefix = "\01l_OBJC_$_CLASS_METHODS_"; |
| 6474 | forProtocol = false; |
Saleem Abdulrasool | d48b0a3 | 2016-10-24 20:47:58 +0000 | [diff] [blame] | 6475 | break; |
| 6476 | |
| 6477 | case MethodListType::ProtocolInstanceMethods: |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 6478 | prefix = "\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"; |
| 6479 | forProtocol = true; |
Saleem Abdulrasool | d48b0a3 | 2016-10-24 20:47:58 +0000 | [diff] [blame] | 6480 | break; |
| 6481 | case MethodListType::ProtocolClassMethods: |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 6482 | prefix = "\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"; |
| 6483 | forProtocol = true; |
Saleem Abdulrasool | d48b0a3 | 2016-10-24 20:47:58 +0000 | [diff] [blame] | 6484 | break; |
| 6485 | case MethodListType::OptionalProtocolInstanceMethods: |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 6486 | prefix = "\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"; |
| 6487 | forProtocol = true; |
Saleem Abdulrasool | d48b0a3 | 2016-10-24 20:47:58 +0000 | [diff] [blame] | 6488 | break; |
| 6489 | case MethodListType::OptionalProtocolClassMethods: |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 6490 | prefix = "\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"; |
| 6491 | forProtocol = true; |
Saleem Abdulrasool | d48b0a3 | 2016-10-24 20:47:58 +0000 | [diff] [blame] | 6492 | break; |
| 6493 | } |
| 6494 | |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 6495 | ConstantInitBuilder builder(CGM); |
| 6496 | auto values = builder.beginStruct(); |
| 6497 | |
| 6498 | // sizeof(struct _objc_method) |
| 6499 | unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.MethodTy); |
| 6500 | values.addInt(ObjCTypes.IntTy, Size); |
| 6501 | // method_count |
| 6502 | values.addInt(ObjCTypes.IntTy, methods.size()); |
| 6503 | auto methodArray = values.beginArray(ObjCTypes.MethodTy); |
| 6504 | for (auto MD : methods) { |
| 6505 | emitMethodConstant(methodArray, MD, forProtocol); |
| 6506 | } |
| 6507 | methodArray.finishAndAddTo(values); |
| 6508 | |
| 6509 | auto *GV = values.finishAndCreateGlobal(prefix + name, CGM.getPointerAlign(), |
| 6510 | /*constant*/ false, |
| 6511 | llvm::GlobalValue::PrivateLinkage); |
Saleem Abdulrasool | 3c628af | 2016-10-25 21:43:28 +0000 | [diff] [blame] | 6512 | if (CGM.getTriple().isOSBinFormatMachO()) |
| 6513 | GV->setSection("__DATA, __objc_const"); |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 6514 | CGM.addCompilerUsedGlobal(GV); |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 6515 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListnfABIPtrTy); |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 6516 | } |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 6517 | |
Fariborz Jahanian | 4e7ae06 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 6518 | /// ObjCIvarOffsetVariable - Returns the ivar offset variable for |
| 6519 | /// the given ivar. |
Daniel Dunbar | 8c7f981 | 2010-04-02 21:14:02 +0000 | [diff] [blame] | 6520 | llvm::GlobalVariable * |
| 6521 | CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID, |
| 6522 | const ObjCIvarDecl *Ivar) { |
| 6523 | const ObjCInterfaceDecl *Container = Ivar->getContainingInterface(); |
Fariborz Jahanian | 451b92a | 2014-07-16 16:16:04 +0000 | [diff] [blame] | 6524 | llvm::SmallString<64> Name("OBJC_IVAR_$_"); |
| 6525 | Name += Container->getObjCRuntimeNameAsString(); |
| 6526 | Name += "."; |
| 6527 | Name += Ivar->getName(); |
Saleem Abdulrasool | 7093e21 | 2016-07-17 22:27:44 +0000 | [diff] [blame] | 6528 | llvm::GlobalVariable *IvarOffsetGV = CGM.getModule().getGlobalVariable(Name); |
| 6529 | if (!IvarOffsetGV) { |
| 6530 | IvarOffsetGV = |
| 6531 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.IvarOffsetVarTy, |
| 6532 | false, llvm::GlobalValue::ExternalLinkage, |
| 6533 | nullptr, Name.str()); |
| 6534 | if (CGM.getTriple().isOSBinFormatCOFF()) { |
| 6535 | bool IsPrivateOrPackage = |
| 6536 | Ivar->getAccessControl() == ObjCIvarDecl::Private || |
| 6537 | Ivar->getAccessControl() == ObjCIvarDecl::Package; |
| 6538 | |
| 6539 | if (ID->hasAttr<DLLExportAttr>() && !IsPrivateOrPackage) |
| 6540 | IvarOffsetGV->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass); |
| 6541 | else if (ID->hasAttr<DLLImportAttr>()) |
| 6542 | IvarOffsetGV->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass); |
| 6543 | } |
| 6544 | } |
Fariborz Jahanian | 4e7ae06 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 6545 | return IvarOffsetGV; |
| 6546 | } |
| 6547 | |
Daniel Dunbar | 8c7f981 | 2010-04-02 21:14:02 +0000 | [diff] [blame] | 6548 | llvm::Constant * |
| 6549 | CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID, |
| 6550 | const ObjCIvarDecl *Ivar, |
Eli Friedman | 8cbca20 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 6551 | unsigned long int Offset) { |
Daniel Dunbar | bf90b33 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 6552 | llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar); |
Tim Northover | 238b508 | 2014-03-29 13:42:40 +0000 | [diff] [blame] | 6553 | IvarOffsetGV->setInitializer( |
| 6554 | llvm::ConstantInt::get(ObjCTypes.IvarOffsetVarTy, Offset)); |
Fariborz Jahanian | c22f236 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 6555 | IvarOffsetGV->setAlignment( |
Tim Northover | 238b508 | 2014-03-29 13:42:40 +0000 | [diff] [blame] | 6556 | CGM.getDataLayout().getABITypeAlignment(ObjCTypes.IvarOffsetVarTy)); |
Daniel Dunbar | bf90b33 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 6557 | |
Saleem Abdulrasool | 7093e21 | 2016-07-17 22:27:44 +0000 | [diff] [blame] | 6558 | if (!CGM.getTriple().isOSBinFormatCOFF()) { |
| 6559 | // FIXME: This matches gcc, but shouldn't the visibility be set on the use |
| 6560 | // as well (i.e., in ObjCIvarOffsetVariable). |
| 6561 | if (Ivar->getAccessControl() == ObjCIvarDecl::Private || |
| 6562 | Ivar->getAccessControl() == ObjCIvarDecl::Package || |
| 6563 | ID->getVisibility() == HiddenVisibility) |
| 6564 | IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 6565 | else |
| 6566 | IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility); |
| 6567 | } |
| 6568 | |
Saleem Abdulrasool | 3c628af | 2016-10-25 21:43:28 +0000 | [diff] [blame] | 6569 | if (CGM.getTriple().isOSBinFormatMachO()) |
| 6570 | IvarOffsetGV->setSection("__DATA, __objc_ivar"); |
Fariborz Jahanian | c88a70d | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 6571 | return IvarOffsetGV; |
Fariborz Jahanian | 40a4bcd | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 6572 | } |
| 6573 | |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 6574 | /// EmitIvarList - Emit the ivar list for the given |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 6575 | /// implementation. The return value has type |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 6576 | /// IvarListnfABIPtrTy. |
| 6577 | /// struct _ivar_t { |
Tim Northover | 238b508 | 2014-03-29 13:42:40 +0000 | [diff] [blame] | 6578 | /// unsigned [long] int *offset; // pointer to ivar offset location |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 6579 | /// char *name; |
| 6580 | /// char *type; |
| 6581 | /// uint32_t alignment; |
| 6582 | /// uint32_t size; |
| 6583 | /// } |
| 6584 | /// struct _ivar_list_t { |
| 6585 | /// uint32 entsize; // sizeof(struct _ivar_t) |
| 6586 | /// uint32 count; |
| 6587 | /// struct _iver_t list[count]; |
| 6588 | /// } |
| 6589 | /// |
Daniel Dunbar | f5c1846 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 6590 | |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 6591 | llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6592 | const ObjCImplementationDecl *ID) { |
| 6593 | |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 6594 | ConstantInitBuilder builder(CGM); |
| 6595 | auto ivarList = builder.beginStruct(); |
| 6596 | ivarList.addInt(ObjCTypes.IntTy, |
| 6597 | CGM.getDataLayout().getTypeAllocSize(ObjCTypes.IvarnfABITy)); |
| 6598 | auto ivarCountSlot = ivarList.addPlaceholder(); |
| 6599 | auto ivars = ivarList.beginArray(ObjCTypes.IvarnfABITy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6600 | |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 6601 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 6602 | assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6603 | |
Fariborz Jahanian | 40a4bcd | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 6604 | // FIXME. Consolidate this with similar code in GenerateClass. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6605 | |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 6606 | for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin(); |
Fariborz Jahanian | b26d578 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 6607 | IVD; IVD = IVD->getNextIvar()) { |
Fariborz Jahanian | 7c80959 | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 6608 | // Ignore unnamed bit-fields. |
| 6609 | if (!IVD->getDeclName()) |
| 6610 | continue; |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 6611 | |
| 6612 | auto ivar = ivars.beginStruct(ObjCTypes.IvarnfABITy); |
| 6613 | ivar.add(EmitIvarOffsetVar(ID->getClassInterface(), IVD, |
| 6614 | ComputeIvarBaseOffset(CGM, ID, IVD))); |
| 6615 | ivar.add(GetMethodVarName(IVD->getIdentifier())); |
| 6616 | ivar.add(GetMethodVarType(IVD)); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 6617 | llvm::Type *FieldTy = |
Daniel Dunbar | 725dc2c | 2009-04-22 08:22:17 +0000 | [diff] [blame] | 6618 | CGM.getTypes().ConvertTypeForMem(IVD->getType()); |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 6619 | unsigned Size = CGM.getDataLayout().getTypeAllocSize(FieldTy); |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 6620 | unsigned Align = CGM.getContext().getPreferredTypeAlign( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6621 | IVD->getType().getTypePtr()) >> 3; |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 6622 | Align = llvm::Log2_32(Align); |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 6623 | ivar.addInt(ObjCTypes.IntTy, Align); |
Daniel Dunbar | ae03226 | 2009-04-20 00:33:43 +0000 | [diff] [blame] | 6624 | // NOTE. Size of a bitfield does not match gcc's, because of the |
| 6625 | // way bitfields are treated special in each. But I am told that |
| 6626 | // 'size' for bitfield ivars is ignored by the runtime so it does |
| 6627 | // not matter. If it matters, there is enough info to get the |
| 6628 | // bitfield right! |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 6629 | ivar.addInt(ObjCTypes.IntTy, Size); |
| 6630 | ivar.finishAndAddTo(ivars); |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 6631 | } |
| 6632 | // Return null for empty list. |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 6633 | if (ivars.empty()) { |
| 6634 | ivars.abandon(); |
| 6635 | ivarList.abandon(); |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 6636 | return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy); |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 6637 | } |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 6638 | |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 6639 | auto ivarCount = ivars.size(); |
| 6640 | ivars.finishAndAddTo(ivarList); |
| 6641 | ivarList.fillPlaceholderWithInt(ivarCountSlot, ObjCTypes.IntTy, ivarCount); |
| 6642 | |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 6643 | const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_"; |
| 6644 | llvm::GlobalVariable *GV = |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 6645 | ivarList.finishAndCreateGlobal(Prefix + OID->getObjCRuntimeNameAsString(), |
| 6646 | CGM.getPointerAlign(), /*constant*/ false, |
| 6647 | llvm::GlobalValue::PrivateLinkage); |
Saleem Abdulrasool | 3c628af | 2016-10-25 21:43:28 +0000 | [diff] [blame] | 6648 | if (CGM.getTriple().isOSBinFormatMachO()) |
| 6649 | GV->setSection("__DATA, __objc_const"); |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 6650 | CGM.addCompilerUsedGlobal(GV); |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 6651 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy); |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6652 | } |
| 6653 | |
| 6654 | llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6655 | const ObjCProtocolDecl *PD) { |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6656 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6657 | |
Saleem Abdulrasool | 9ccc7ad | 2016-10-25 14:50:44 +0000 | [diff] [blame] | 6658 | if (!Entry) { |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6659 | // We use the initializer as a marker of whether this is a forward |
| 6660 | // reference or not. At module finalization we add the empty |
| 6661 | // contents for protocols which were referenced but never defined. |
Saleem Abdulrasool | 9ccc7ad | 2016-10-25 14:50:44 +0000 | [diff] [blame] | 6662 | llvm::SmallString<64> Protocol; |
| 6663 | llvm::raw_svector_ostream(Protocol) << "\01l_OBJC_PROTOCOL_$_" |
| 6664 | << PD->getObjCRuntimeNameAsString(); |
| 6665 | |
| 6666 | Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, |
| 6667 | false, llvm::GlobalValue::ExternalLinkage, |
| 6668 | nullptr, Protocol); |
| 6669 | if (!CGM.getTriple().isOSBinFormatMachO()) |
| 6670 | Entry->setComdat(CGM.getModule().getOrInsertComdat(Protocol)); |
| 6671 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6672 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6673 | return Entry; |
| 6674 | } |
| 6675 | |
| 6676 | /// GetOrEmitProtocol - Generate the protocol meta-data: |
| 6677 | /// @code |
| 6678 | /// struct _protocol_t { |
| 6679 | /// id isa; // NULL |
| 6680 | /// const char * const protocol_name; |
| 6681 | /// const struct _protocol_list_t * protocol_list; // super protocols |
| 6682 | /// const struct method_list_t * const instance_methods; |
| 6683 | /// const struct method_list_t * const class_methods; |
| 6684 | /// const struct method_list_t *optionalInstanceMethods; |
| 6685 | /// const struct method_list_t *optionalClassMethods; |
| 6686 | /// const struct _prop_list_t * properties; |
| 6687 | /// const uint32_t size; // sizeof(struct _protocol_t) |
| 6688 | /// const uint32_t flags; // = 0 |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 6689 | /// const char ** extendedMethodTypes; |
Fariborz Jahanian | 6a9c46b | 2015-03-31 22:22:40 +0000 | [diff] [blame] | 6690 | /// const char *demangledName; |
Manman Ren | ce7bff5 | 2016-01-29 23:46:55 +0000 | [diff] [blame] | 6691 | /// const struct _prop_list_t * class_properties; |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6692 | /// } |
| 6693 | /// @endcode |
| 6694 | /// |
| 6695 | |
| 6696 | llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6697 | const ObjCProtocolDecl *PD) { |
John McCall | f9582a7 | 2012-03-30 21:29:05 +0000 | [diff] [blame] | 6698 | llvm::GlobalVariable *Entry = Protocols[PD->getIdentifier()]; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6699 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6700 | // Early exit if a defining object has already been generated. |
| 6701 | if (Entry && Entry->hasInitializer()) |
| 6702 | return Entry; |
| 6703 | |
Douglas Gregor | a715bff | 2012-01-01 19:51:50 +0000 | [diff] [blame] | 6704 | // Use the protocol definition, if there is one. |
| 6705 | if (const ObjCProtocolDecl *Def = PD->getDefinition()) |
| 6706 | PD = Def; |
| 6707 | |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 6708 | auto methodLists = ProtocolMethodLists::get(PD); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6709 | |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 6710 | ConstantInitBuilder builder(CGM); |
| 6711 | auto values = builder.beginStruct(ObjCTypes.ProtocolnfABITy); |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 6712 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6713 | // isa is NULL |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 6714 | values.addNullPointer(ObjCTypes.ObjectPtrTy); |
| 6715 | values.add(GetClassName(PD->getObjCRuntimeNameAsString())); |
| 6716 | values.add(EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" |
| 6717 | + PD->getObjCRuntimeNameAsString(), |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 6718 | PD->protocol_begin(), |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 6719 | PD->protocol_end())); |
| 6720 | values.add(methodLists.emitMethodList(this, PD, |
| 6721 | ProtocolMethodLists::RequiredInstanceMethods)); |
| 6722 | values.add(methodLists.emitMethodList(this, PD, |
| 6723 | ProtocolMethodLists::RequiredClassMethods)); |
| 6724 | values.add(methodLists.emitMethodList(this, PD, |
| 6725 | ProtocolMethodLists::OptionalInstanceMethods)); |
| 6726 | values.add(methodLists.emitMethodList(this, PD, |
| 6727 | ProtocolMethodLists::OptionalClassMethods)); |
| 6728 | values.add(EmitPropertyList( |
| 6729 | "\01l_OBJC_$_PROP_LIST_" + PD->getObjCRuntimeNameAsString(), |
| 6730 | nullptr, PD, ObjCTypes, false)); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6731 | uint32_t Size = |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 6732 | CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ProtocolnfABITy); |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 6733 | values.addInt(ObjCTypes.IntTy, Size); |
| 6734 | values.addInt(ObjCTypes.IntTy, 0); |
| 6735 | values.add(EmitProtocolMethodTypes("\01l_OBJC_$_PROTOCOL_METHOD_TYPES_" |
Fariborz Jahanian | 451b92a | 2014-07-16 16:16:04 +0000 | [diff] [blame] | 6736 | + PD->getObjCRuntimeNameAsString(), |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 6737 | methodLists.emitExtendedTypesArray(this), |
| 6738 | ObjCTypes)); |
| 6739 | |
Fariborz Jahanian | 6a9c46b | 2015-03-31 22:22:40 +0000 | [diff] [blame] | 6740 | // const char *demangledName; |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 6741 | values.addNullPointer(ObjCTypes.Int8PtrTy); |
Manman Ren | ce7bff5 | 2016-01-29 23:46:55 +0000 | [diff] [blame] | 6742 | |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 6743 | values.add(EmitPropertyList( |
Manman Ren | ce7bff5 | 2016-01-29 23:46:55 +0000 | [diff] [blame] | 6744 | "\01l_OBJC_$_CLASS_PROP_LIST_" + PD->getObjCRuntimeNameAsString(), |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 6745 | nullptr, PD, ObjCTypes, true)); |
Fariborz Jahanian | 6a9c46b | 2015-03-31 22:22:40 +0000 | [diff] [blame] | 6746 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6747 | if (Entry) { |
Rafael Espindola | 2461509 | 2014-09-12 20:14:20 +0000 | [diff] [blame] | 6748 | // Already created, fix the linkage and update the initializer. |
| 6749 | Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage); |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 6750 | values.finishAndSetAsInitializer(Entry); |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6751 | } else { |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 6752 | llvm::SmallString<64> symbolName; |
| 6753 | llvm::raw_svector_ostream(symbolName) |
| 6754 | << "\01l_OBJC_PROTOCOL_$_" << PD->getObjCRuntimeNameAsString(); |
Saleem Abdulrasool | 9ccc7ad | 2016-10-25 14:50:44 +0000 | [diff] [blame] | 6755 | |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 6756 | Entry = values.finishAndCreateGlobal(symbolName, CGM.getPointerAlign(), |
| 6757 | /*constant*/ false, |
| 6758 | llvm::GlobalValue::WeakAnyLinkage); |
Saleem Abdulrasool | 9ccc7ad | 2016-10-25 14:50:44 +0000 | [diff] [blame] | 6759 | if (!CGM.getTriple().isOSBinFormatMachO()) |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 6760 | Entry->setComdat(CGM.getModule().getOrInsertComdat(symbolName)); |
John McCall | f9582a7 | 2012-03-30 21:29:05 +0000 | [diff] [blame] | 6761 | |
| 6762 | Protocols[PD->getIdentifier()] = Entry; |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6763 | } |
Rafael Espindola | 70efc5b | 2014-03-06 18:54:12 +0000 | [diff] [blame] | 6764 | Entry->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 6765 | CGM.addCompilerUsedGlobal(Entry); |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 6766 | |
Fariborz Jahanian | 61cd4b5 | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 6767 | // Use this protocol meta-data to build protocol list table in section |
| 6768 | // __DATA, __objc_protolist |
Saleem Abdulrasool | 9ccc7ad | 2016-10-25 14:50:44 +0000 | [diff] [blame] | 6769 | llvm::SmallString<64> ProtocolRef; |
| 6770 | llvm::raw_svector_ostream(ProtocolRef) << "\01l_OBJC_LABEL_PROTOCOL_$_" |
| 6771 | << PD->getObjCRuntimeNameAsString(); |
| 6772 | |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 6773 | llvm::GlobalVariable *PTGV = |
| 6774 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy, |
Rafael Espindola | 70efc5b | 2014-03-06 18:54:12 +0000 | [diff] [blame] | 6775 | false, llvm::GlobalValue::WeakAnyLinkage, Entry, |
Saleem Abdulrasool | 9ccc7ad | 2016-10-25 14:50:44 +0000 | [diff] [blame] | 6776 | ProtocolRef); |
| 6777 | if (!CGM.getTriple().isOSBinFormatMachO()) |
| 6778 | PTGV->setComdat(CGM.getModule().getOrInsertComdat(ProtocolRef)); |
Fariborz Jahanian | c22f236 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 6779 | PTGV->setAlignment( |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 6780 | CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy)); |
Saleem Abdulrasool | 9ccc7ad | 2016-10-25 14:50:44 +0000 | [diff] [blame] | 6781 | if (CGM.getTriple().isOSBinFormatMachO()) |
| 6782 | PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip"); |
Rafael Espindola | 70efc5b | 2014-03-06 18:54:12 +0000 | [diff] [blame] | 6783 | PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 6784 | CGM.addCompilerUsedGlobal(PTGV); |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6785 | return Entry; |
| 6786 | } |
| 6787 | |
| 6788 | /// EmitProtocolList - Generate protocol list meta-data: |
| 6789 | /// @code |
| 6790 | /// struct _protocol_list_t { |
| 6791 | /// long protocol_count; // Note, this is 32/64 bit |
| 6792 | /// struct _protocol_t[protocol_count]; |
| 6793 | /// } |
| 6794 | /// @endcode |
| 6795 | /// |
| 6796 | llvm::Constant * |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6797 | CGObjCNonFragileABIMac::EmitProtocolList(Twine Name, |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 6798 | ObjCProtocolDecl::protocol_iterator begin, |
| 6799 | ObjCProtocolDecl::protocol_iterator end) { |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 6800 | SmallVector<llvm::Constant *, 16> ProtocolRefs; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6801 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6802 | // Just return null for empty protocol lists |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6803 | if (begin == end) |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 6804 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6805 | |
Daniel Dunbar | 8de90f0 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 6806 | // FIXME: We shouldn't need to do this lookup here, should we? |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 6807 | SmallString<256> TmpName; |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 6808 | Name.toVector(TmpName); |
| 6809 | llvm::GlobalVariable *GV = |
| 6810 | CGM.getModule().getGlobalVariable(TmpName.str(), true); |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6811 | if (GV) |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 6812 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6813 | |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 6814 | ConstantInitBuilder builder(CGM); |
| 6815 | auto values = builder.beginStruct(); |
| 6816 | auto countSlot = values.addPlaceholder(); |
| 6817 | |
| 6818 | // A null-terminated array of protocols. |
| 6819 | auto array = values.beginArray(ObjCTypes.ProtocolnfABIPtrTy); |
Daniel Dunbar | 8de90f0 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 6820 | for (; begin != end; ++begin) |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 6821 | array.add(GetProtocolRef(*begin)); // Implemented??? |
| 6822 | auto count = array.size(); |
| 6823 | array.addNullPointer(ObjCTypes.ProtocolnfABIPtrTy); |
Daniel Dunbar | 8de90f0 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 6824 | |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 6825 | array.finishAndAddTo(values); |
| 6826 | values.fillPlaceholderWithInt(countSlot, ObjCTypes.LongTy, count); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6827 | |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 6828 | GV = values.finishAndCreateGlobal(Name, CGM.getPointerAlign(), |
| 6829 | /*constant*/ false, |
| 6830 | llvm::GlobalValue::PrivateLinkage); |
Saleem Abdulrasool | 3c628af | 2016-10-25 21:43:28 +0000 | [diff] [blame] | 6831 | if (CGM.getTriple().isOSBinFormatMachO()) |
| 6832 | GV->setSection("__DATA, __objc_const"); |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 6833 | CGM.addCompilerUsedGlobal(GV); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6834 | return llvm::ConstantExpr::getBitCast(GV, |
Daniel Dunbar | 8de90f0 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 6835 | ObjCTypes.ProtocolListnfABIPtrTy); |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6836 | } |
| 6837 | |
Fariborz Jahanian | c88a70d | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 6838 | /// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference. |
| 6839 | /// This code gen. amounts to generating code for: |
| 6840 | /// @code |
| 6841 | /// (type *)((char *)base + _OBJC_IVAR_$_.ivar; |
| 6842 | /// @encode |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6843 | /// |
Fariborz Jahanian | 712bfa6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 6844 | LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar( |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 6845 | CodeGen::CodeGenFunction &CGF, |
| 6846 | QualType ObjectTy, |
| 6847 | llvm::Value *BaseValue, |
| 6848 | const ObjCIvarDecl *Ivar, |
| 6849 | unsigned CVRQualifiers) { |
| 6850 | ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface(); |
Fariborz Jahanian | caabf1b | 2012-02-20 22:42:22 +0000 | [diff] [blame] | 6851 | llvm::Value *Offset = EmitIvarOffset(CGF, ID, Ivar); |
Daniel Dunbar | 9fd114d | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 6852 | return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers, |
Fariborz Jahanian | caabf1b | 2012-02-20 22:42:22 +0000 | [diff] [blame] | 6853 | Offset); |
Fariborz Jahanian | c88a70d | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 6854 | } |
| 6855 | |
Fariborz Jahanian | 21fc74c | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 6856 | llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6857 | CodeGen::CodeGenFunction &CGF, |
| 6858 | const ObjCInterfaceDecl *Interface, |
| 6859 | const ObjCIvarDecl *Ivar) { |
Tim Northover | 238b508 | 2014-03-29 13:42:40 +0000 | [diff] [blame] | 6860 | llvm::Value *IvarOffsetValue = ObjCIvarOffsetVariable(Interface, Ivar); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6861 | IvarOffsetValue = CGF.Builder.CreateAlignedLoad(IvarOffsetValue, |
| 6862 | CGF.getSizeAlign(), "ivar"); |
Tim Northover | 238b508 | 2014-03-29 13:42:40 +0000 | [diff] [blame] | 6863 | if (IsIvarOffsetKnownIdempotent(CGF, Ivar)) |
| 6864 | cast<llvm::LoadInst>(IvarOffsetValue) |
| 6865 | ->setMetadata(CGM.getModule().getMDKindID("invariant.load"), |
Craig Topper | 5fc8fc2 | 2014-08-27 06:28:36 +0000 | [diff] [blame] | 6866 | llvm::MDNode::get(VMContext, None)); |
Tim Northover | 238b508 | 2014-03-29 13:42:40 +0000 | [diff] [blame] | 6867 | |
| 6868 | // This could be 32bit int or 64bit integer depending on the architecture. |
| 6869 | // Cast it to 64bit integer value, if it is a 32bit integer ivar offset value |
| 6870 | // as this is what caller always expectes. |
| 6871 | if (ObjCTypes.IvarOffsetVarTy == ObjCTypes.IntTy) |
| 6872 | IvarOffsetValue = CGF.Builder.CreateIntCast( |
| 6873 | IvarOffsetValue, ObjCTypes.LongTy, true, "ivar.conv"); |
| 6874 | return IvarOffsetValue; |
Fariborz Jahanian | 21fc74c | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 6875 | } |
| 6876 | |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6877 | static void appendSelectorForMessageRefTable(std::string &buffer, |
| 6878 | Selector selector) { |
| 6879 | if (selector.isUnarySelector()) { |
| 6880 | buffer += selector.getNameForSlot(0); |
| 6881 | return; |
| 6882 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6883 | |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6884 | for (unsigned i = 0, e = selector.getNumArgs(); i != e; ++i) { |
| 6885 | buffer += selector.getNameForSlot(i); |
| 6886 | buffer += '_'; |
| 6887 | } |
| 6888 | } |
| 6889 | |
Eric Christopher | d160c50 | 2016-01-29 01:35:53 +0000 | [diff] [blame] | 6890 | /// Emit a "vtable" message send. We emit a weak hidden-visibility |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 6891 | /// struct, initially containing the selector pointer and a pointer to |
| 6892 | /// a "fixup" variant of the appropriate objc_msgSend. To call, we |
| 6893 | /// load and call the function pointer, passing the address of the |
| 6894 | /// struct as the second parameter. The runtime determines whether |
| 6895 | /// the selector is currently emitted using vtable dispatch; if so, it |
| 6896 | /// substitutes a stub function which simply tail-calls through the |
| 6897 | /// appropriate vtable slot, and if not, it substitues a stub function |
| 6898 | /// which tail-calls objc_msgSend. Both stubs adjust the selector |
| 6899 | /// argument to correctly point to the selector. |
| 6900 | RValue |
| 6901 | CGObjCNonFragileABIMac::EmitVTableMessageSend(CodeGenFunction &CGF, |
| 6902 | ReturnValueSlot returnSlot, |
| 6903 | QualType resultType, |
| 6904 | Selector selector, |
| 6905 | llvm::Value *arg0, |
| 6906 | QualType arg0Type, |
| 6907 | bool isSuper, |
| 6908 | const CallArgList &formalArgs, |
| 6909 | const ObjCMethodDecl *method) { |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6910 | // Compute the actual arguments. |
| 6911 | CallArgList args; |
| 6912 | |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 6913 | // First argument: the receiver / super-call structure. |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6914 | if (!isSuper) |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 6915 | arg0 = CGF.Builder.CreateBitCast(arg0, ObjCTypes.ObjectPtrTy); |
| 6916 | args.add(RValue::get(arg0), arg0Type); |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6917 | |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 6918 | // Second argument: a pointer to the message ref structure. Leave |
| 6919 | // the actual argument value blank for now. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 6920 | args.add(RValue::get(nullptr), ObjCTypes.MessageRefCPtrTy); |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6921 | |
| 6922 | args.insert(args.end(), formalArgs.begin(), formalArgs.end()); |
| 6923 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 6924 | MessageSendInfo MSI = getMessageSendInfo(method, resultType, args); |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6925 | |
John McCall | 5880fb8 | 2011-05-14 21:12:11 +0000 | [diff] [blame] | 6926 | NullReturnState nullReturn; |
| 6927 | |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 6928 | // Find the function to call and the mangled name for the message |
| 6929 | // ref structure. Using a different mangled name wouldn't actually |
| 6930 | // be a problem; it would just be a waste. |
| 6931 | // |
| 6932 | // The runtime currently never uses vtable dispatch for anything |
| 6933 | // except normal, non-super message-sends. |
| 6934 | // FIXME: don't use this for that. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 6935 | llvm::Constant *fn = nullptr; |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6936 | std::string messageRefName("\01l_"); |
Tim Northover | e77cc39 | 2014-03-29 13:28:05 +0000 | [diff] [blame] | 6937 | if (CGM.ReturnSlotInterferesWithArgs(MSI.CallInfo)) { |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6938 | if (isSuper) { |
| 6939 | fn = ObjCTypes.getMessageSendSuper2StretFixupFn(); |
| 6940 | messageRefName += "objc_msgSendSuper2_stret_fixup"; |
Chris Lattner | 396639d | 2010-08-18 16:09:06 +0000 | [diff] [blame] | 6941 | } else { |
John McCall | 5880fb8 | 2011-05-14 21:12:11 +0000 | [diff] [blame] | 6942 | nullReturn.init(CGF, arg0); |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6943 | fn = ObjCTypes.getMessageSendStretFixupFn(); |
| 6944 | messageRefName += "objc_msgSend_stret_fixup"; |
Chris Lattner | 396639d | 2010-08-18 16:09:06 +0000 | [diff] [blame] | 6945 | } |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6946 | } else if (!isSuper && CGM.ReturnTypeUsesFPRet(resultType)) { |
| 6947 | fn = ObjCTypes.getMessageSendFpretFixupFn(); |
| 6948 | messageRefName += "objc_msgSend_fpret_fixup"; |
Mike Stump | 658fe02 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 6949 | } else { |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6950 | if (isSuper) { |
| 6951 | fn = ObjCTypes.getMessageSendSuper2FixupFn(); |
| 6952 | messageRefName += "objc_msgSendSuper2_fixup"; |
Chris Lattner | 396639d | 2010-08-18 16:09:06 +0000 | [diff] [blame] | 6953 | } else { |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6954 | fn = ObjCTypes.getMessageSendFixupFn(); |
| 6955 | messageRefName += "objc_msgSend_fixup"; |
Chris Lattner | 396639d | 2010-08-18 16:09:06 +0000 | [diff] [blame] | 6956 | } |
Fariborz Jahanian | e4dc35d | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 6957 | } |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6958 | assert(fn && "CGObjCNonFragileABIMac::EmitMessageSend"); |
| 6959 | messageRefName += '_'; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6960 | |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6961 | // Append the selector name, except use underscores anywhere we |
| 6962 | // would have used colons. |
| 6963 | appendSelectorForMessageRefTable(messageRefName, selector); |
| 6964 | |
| 6965 | llvm::GlobalVariable *messageRef |
| 6966 | = CGM.getModule().getGlobalVariable(messageRefName); |
| 6967 | if (!messageRef) { |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 6968 | // Build the message ref structure. |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 6969 | ConstantInitBuilder builder(CGM); |
| 6970 | auto values = builder.beginStruct(); |
| 6971 | values.add(fn); |
| 6972 | values.add(GetMethodVarName(selector)); |
| 6973 | messageRef = values.finishAndCreateGlobal(messageRefName, |
| 6974 | CharUnits::fromQuantity(16), |
| 6975 | /*constant*/ false, |
| 6976 | llvm::GlobalValue::WeakAnyLinkage); |
Rafael Espindola | 70efc5b | 2014-03-06 18:54:12 +0000 | [diff] [blame] | 6977 | messageRef->setVisibility(llvm::GlobalValue::HiddenVisibility); |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6978 | messageRef->setSection("__DATA, __objc_msgrefs, coalesced"); |
| 6979 | } |
Rafael Espindola | 70efc5b | 2014-03-06 18:54:12 +0000 | [diff] [blame] | 6980 | |
Fariborz Jahanian | c93fa98 | 2012-01-30 23:39:30 +0000 | [diff] [blame] | 6981 | bool requiresnullCheck = false; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6982 | if (CGM.getLangOpts().ObjCAutoRefCount && method) |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 6983 | for (const auto *ParamDecl : method->parameters()) { |
Fariborz Jahanian | c93fa98 | 2012-01-30 23:39:30 +0000 | [diff] [blame] | 6984 | if (ParamDecl->hasAttr<NSConsumedAttr>()) { |
| 6985 | if (!nullReturn.NullBB) |
| 6986 | nullReturn.init(CGF, arg0); |
| 6987 | requiresnullCheck = true; |
| 6988 | break; |
| 6989 | } |
| 6990 | } |
| 6991 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6992 | Address mref = |
| 6993 | Address(CGF.Builder.CreateBitCast(messageRef, ObjCTypes.MessageRefPtrTy), |
| 6994 | CGF.getPointerAlign()); |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6995 | |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 6996 | // Update the message ref argument. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6997 | args[1].RV = RValue::get(mref.getPointer()); |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6998 | |
| 6999 | // Load the function to call from the message ref table. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7000 | Address calleeAddr = |
| 7001 | CGF.Builder.CreateStructGEP(mref, 0, CharUnits::Zero()); |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 7002 | llvm::Value *calleePtr = CGF.Builder.CreateLoad(calleeAddr, "msgSend_fn"); |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 7003 | |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 7004 | calleePtr = CGF.Builder.CreateBitCast(calleePtr, MSI.MessengerType); |
| 7005 | CGCallee callee(CGCalleeInfo(), calleePtr); |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 7006 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 7007 | RValue result = CGF.EmitCall(MSI.CallInfo, callee, returnSlot, args); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 7008 | return nullReturn.complete(CGF, result, resultType, formalArgs, |
| 7009 | requiresnullCheck ? method : nullptr); |
Fariborz Jahanian | 3d9296e | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 7010 | } |
| 7011 | |
| 7012 | /// Generate code for a message send expression in the nonfragile abi. |
Daniel Dunbar | aff9fca | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 7013 | CodeGen::RValue |
| 7014 | CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
John McCall | 78a1511 | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 7015 | ReturnValueSlot Return, |
Daniel Dunbar | aff9fca | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 7016 | QualType ResultType, |
| 7017 | Selector Sel, |
| 7018 | llvm::Value *Receiver, |
Daniel Dunbar | aff9fca | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 7019 | const CallArgList &CallArgs, |
David Chisnall | 01aa467 | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 7020 | const ObjCInterfaceDecl *Class, |
Daniel Dunbar | aff9fca | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 7021 | const ObjCMethodDecl *Method) { |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 7022 | return isVTableDispatchedSelector(Sel) |
| 7023 | ? EmitVTableMessageSend(CGF, Return, ResultType, Sel, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 7024 | Receiver, CGF.getContext().getObjCIdType(), |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 7025 | false, CallArgs, Method) |
| 7026 | : EmitMessageSend(CGF, Return, ResultType, |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 7027 | EmitSelector(CGF, Sel), |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 7028 | Receiver, CGF.getContext().getObjCIdType(), |
John McCall | 1e3157b | 2015-09-10 22:27:50 +0000 | [diff] [blame] | 7029 | false, CallArgs, Method, Class, ObjCTypes); |
Fariborz Jahanian | 3d9296e | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 7030 | } |
| 7031 | |
Daniel Dunbar | c6928bb | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 7032 | llvm::GlobalVariable * |
Benjamin Kramer | 0772c42 | 2016-02-13 13:42:54 +0000 | [diff] [blame] | 7033 | CGObjCNonFragileABIMac::GetClassGlobal(StringRef Name, bool Weak) { |
Rafael Espindola | 554256c | 2014-02-26 22:25:45 +0000 | [diff] [blame] | 7034 | llvm::GlobalValue::LinkageTypes L = |
| 7035 | Weak ? llvm::GlobalValue::ExternalWeakLinkage |
| 7036 | : llvm::GlobalValue::ExternalLinkage; |
| 7037 | |
Daniel Dunbar | c6928bb | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 7038 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 7039 | |
Rafael Espindola | 554256c | 2014-02-26 22:25:45 +0000 | [diff] [blame] | 7040 | if (!GV) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 7041 | GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy, |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 7042 | false, L, nullptr, Name); |
Daniel Dunbar | c6928bb | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 7043 | |
Rafael Espindola | 554256c | 2014-02-26 22:25:45 +0000 | [diff] [blame] | 7044 | assert(GV->getLinkage() == L); |
Daniel Dunbar | c6928bb | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 7045 | return GV; |
| 7046 | } |
| 7047 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 7048 | llvm::Value *CGObjCNonFragileABIMac::EmitClassRefFromId(CodeGenFunction &CGF, |
Rafael Espindola | 554256c | 2014-02-26 22:25:45 +0000 | [diff] [blame] | 7049 | IdentifierInfo *II, |
Fariborz Jahanian | 451b92a | 2014-07-16 16:16:04 +0000 | [diff] [blame] | 7050 | bool Weak, |
| 7051 | const ObjCInterfaceDecl *ID) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7052 | CharUnits Align = CGF.getPointerAlign(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7053 | llvm::GlobalVariable *&Entry = ClassReferences[II]; |
| 7054 | |
Fariborz Jahanian | 33f66e6 | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 7055 | if (!Entry) { |
Saleem Abdulrasool | 1e6c406 | 2016-05-16 05:06:49 +0000 | [diff] [blame] | 7056 | StringRef Name = ID ? ID->getObjCRuntimeNameAsString() : II->getName(); |
| 7057 | std::string ClassName = (getClassSymbolPrefix() + Name).str(); |
Rafael Espindola | 554256c | 2014-02-26 22:25:45 +0000 | [diff] [blame] | 7058 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName, Weak); |
Rafael Espindola | 8b27bdb | 2014-11-06 13:30:38 +0000 | [diff] [blame] | 7059 | Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, |
| 7060 | false, llvm::GlobalValue::PrivateLinkage, |
| 7061 | ClassGV, "OBJC_CLASSLIST_REFERENCES_$_"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7062 | Entry->setAlignment(Align.getQuantity()); |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 7063 | Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip"); |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 7064 | CGM.addCompilerUsedGlobal(Entry); |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 7065 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7066 | return CGF.Builder.CreateAlignedLoad(Entry, Align); |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 7067 | } |
Fariborz Jahanian | 33f66e6 | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 7068 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 7069 | llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CodeGenFunction &CGF, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7070 | const ObjCInterfaceDecl *ID) { |
Douglas Gregor | 24ae22c | 2016-04-01 23:23:52 +0000 | [diff] [blame] | 7071 | // If the class has the objc_runtime_visible attribute, we need to |
| 7072 | // use the Objective-C runtime to get the class. |
| 7073 | if (ID->hasAttr<ObjCRuntimeVisibleAttr>()) |
| 7074 | return EmitClassRefViaRuntime(CGF, ID, ObjCTypes); |
| 7075 | |
Fariborz Jahanian | 451b92a | 2014-07-16 16:16:04 +0000 | [diff] [blame] | 7076 | return EmitClassRefFromId(CGF, ID->getIdentifier(), ID->isWeakImported(), ID); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7077 | } |
| 7078 | |
| 7079 | llvm::Value *CGObjCNonFragileABIMac::EmitNSAutoreleasePoolClassRef( |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 7080 | CodeGenFunction &CGF) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7081 | IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool"); |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 7082 | return EmitClassRefFromId(CGF, II, false, nullptr); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7083 | } |
| 7084 | |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 7085 | llvm::Value * |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 7086 | CGObjCNonFragileABIMac::EmitSuperClassRef(CodeGenFunction &CGF, |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 7087 | const ObjCInterfaceDecl *ID) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7088 | CharUnits Align = CGF.getPointerAlign(); |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 7089 | llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()]; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 7090 | |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 7091 | if (!Entry) { |
Fariborz Jahanian | 451b92a | 2014-07-16 16:16:04 +0000 | [diff] [blame] | 7092 | llvm::SmallString<64> ClassName(getClassSymbolPrefix()); |
| 7093 | ClassName += ID->getObjCRuntimeNameAsString(); |
| 7094 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName.str(), |
Fariborz Jahanian | f322f2f | 2014-03-11 00:25:05 +0000 | [diff] [blame] | 7095 | ID->isWeakImported()); |
Rafael Espindola | 8b27bdb | 2014-11-06 13:30:38 +0000 | [diff] [blame] | 7096 | Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, |
| 7097 | false, llvm::GlobalValue::PrivateLinkage, |
| 7098 | ClassGV, "OBJC_CLASSLIST_SUP_REFS_$_"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7099 | Entry->setAlignment(Align.getQuantity()); |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 7100 | Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip"); |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 7101 | CGM.addCompilerUsedGlobal(Entry); |
Fariborz Jahanian | 33f66e6 | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 7102 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7103 | return CGF.Builder.CreateAlignedLoad(Entry, Align); |
Fariborz Jahanian | 33f66e6 | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 7104 | } |
| 7105 | |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 7106 | /// EmitMetaClassRef - Return a Value * of the address of _class_t |
| 7107 | /// meta-data |
| 7108 | /// |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 7109 | llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CodeGenFunction &CGF, |
Fariborz Jahanian | 0b3bc24 | 2014-06-10 17:08:04 +0000 | [diff] [blame] | 7110 | const ObjCInterfaceDecl *ID, |
| 7111 | bool Weak) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7112 | CharUnits Align = CGF.getPointerAlign(); |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 7113 | llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()]; |
Rafael Espindola | 21039aa | 2014-02-27 16:26:32 +0000 | [diff] [blame] | 7114 | if (!Entry) { |
Fariborz Jahanian | 451b92a | 2014-07-16 16:16:04 +0000 | [diff] [blame] | 7115 | llvm::SmallString<64> MetaClassName(getMetaclassSymbolPrefix()); |
| 7116 | MetaClassName += ID->getObjCRuntimeNameAsString(); |
Fariborz Jahanian | 0b3bc24 | 2014-06-10 17:08:04 +0000 | [diff] [blame] | 7117 | llvm::GlobalVariable *MetaClassGV = |
Fariborz Jahanian | 451b92a | 2014-07-16 16:16:04 +0000 | [diff] [blame] | 7118 | GetClassGlobal(MetaClassName.str(), Weak); |
Rafael Espindola | 8b27bdb | 2014-11-06 13:30:38 +0000 | [diff] [blame] | 7119 | |
Rafael Espindola | 21039aa | 2014-02-27 16:26:32 +0000 | [diff] [blame] | 7120 | Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, |
Rafael Espindola | 5179a4e | 2014-02-27 19:01:11 +0000 | [diff] [blame] | 7121 | false, llvm::GlobalValue::PrivateLinkage, |
Rafael Espindola | 8b27bdb | 2014-11-06 13:30:38 +0000 | [diff] [blame] | 7122 | MetaClassGV, "OBJC_CLASSLIST_SUP_REFS_$_"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7123 | Entry->setAlignment(Align.getQuantity()); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 7124 | |
Rafael Espindola | 21039aa | 2014-02-27 16:26:32 +0000 | [diff] [blame] | 7125 | Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip"); |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 7126 | CGM.addCompilerUsedGlobal(Entry); |
Rafael Espindola | 21039aa | 2014-02-27 16:26:32 +0000 | [diff] [blame] | 7127 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 7128 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7129 | return CGF.Builder.CreateAlignedLoad(Entry, Align); |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 7130 | } |
| 7131 | |
Fariborz Jahanian | 33f66e6 | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 7132 | /// GetClass - Return a reference to the class for the given interface |
| 7133 | /// decl. |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 7134 | llvm::Value *CGObjCNonFragileABIMac::GetClass(CodeGenFunction &CGF, |
Fariborz Jahanian | 33f66e6 | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 7135 | const ObjCInterfaceDecl *ID) { |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 7136 | if (ID->isWeakImported()) { |
Fariborz Jahanian | 451b92a | 2014-07-16 16:16:04 +0000 | [diff] [blame] | 7137 | llvm::SmallString<64> ClassName(getClassSymbolPrefix()); |
| 7138 | ClassName += ID->getObjCRuntimeNameAsString(); |
| 7139 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName.str(), true); |
Nick Lewycky | 9a44164 | 2014-02-27 00:36:00 +0000 | [diff] [blame] | 7140 | (void)ClassGV; |
Rafael Espindola | b326295 | 2014-05-09 00:43:37 +0000 | [diff] [blame] | 7141 | assert(ClassGV->hasExternalWeakLinkage()); |
Fariborz Jahanian | 95ace55 | 2009-11-17 22:42:00 +0000 | [diff] [blame] | 7142 | } |
| 7143 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 7144 | return EmitClassRef(CGF, ID); |
Fariborz Jahanian | 33f66e6 | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 7145 | } |
| 7146 | |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 7147 | /// Generates a message send where the super is the receiver. This is |
| 7148 | /// a message send to self with special delivery semantics indicating |
| 7149 | /// which class's method should be called. |
| 7150 | CodeGen::RValue |
| 7151 | CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
John McCall | 78a1511 | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 7152 | ReturnValueSlot Return, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 7153 | QualType ResultType, |
| 7154 | Selector Sel, |
| 7155 | const ObjCInterfaceDecl *Class, |
| 7156 | bool isCategoryImpl, |
| 7157 | llvm::Value *Receiver, |
| 7158 | bool IsClassMessage, |
Daniel Dunbar | aff9fca | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 7159 | const CodeGen::CallArgList &CallArgs, |
| 7160 | const ObjCMethodDecl *Method) { |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 7161 | // ... |
| 7162 | // Create and init a super structure; this is a (receiver, class) |
| 7163 | // pair we will pass to objc_msgSendSuper. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7164 | Address ObjCSuper = |
| 7165 | CGF.CreateTempAlloca(ObjCTypes.SuperTy, CGF.getPointerAlign(), |
| 7166 | "objc_super"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 7167 | |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 7168 | llvm::Value *ReceiverAsObject = |
| 7169 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 7170 | CGF.Builder.CreateStore( |
| 7171 | ReceiverAsObject, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7172 | CGF.Builder.CreateStructGEP(ObjCSuper, 0, CharUnits::Zero())); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 7173 | |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 7174 | // If this is a class message the metaclass is passed as the target. |
Fariborz Jahanian | bac73ac | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 7175 | llvm::Value *Target; |
Fariborz Jahanian | 27678b0 | 2012-10-10 23:11:18 +0000 | [diff] [blame] | 7176 | if (IsClassMessage) |
Fariborz Jahanian | 85b99da | 2014-08-28 17:05:17 +0000 | [diff] [blame] | 7177 | Target = EmitMetaClassRef(CGF, Class, Class->isWeakImported()); |
Fariborz Jahanian | 27678b0 | 2012-10-10 23:11:18 +0000 | [diff] [blame] | 7178 | else |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 7179 | Target = EmitSuperClassRef(CGF, Class); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 7180 | |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 7181 | // FIXME: We shouldn't need to do this cast, rectify the ASTContext and |
| 7182 | // ObjCTypes types. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 7183 | llvm::Type *ClassTy = |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 7184 | CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); |
| 7185 | Target = CGF.Builder.CreateBitCast(Target, ClassTy); |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 7186 | CGF.Builder.CreateStore( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7187 | Target, CGF.Builder.CreateStructGEP(ObjCSuper, 1, CGF.getPointerSize())); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 7188 | |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 7189 | return (isVTableDispatchedSelector(Sel)) |
| 7190 | ? EmitVTableMessageSend(CGF, Return, ResultType, Sel, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7191 | ObjCSuper.getPointer(), ObjCTypes.SuperPtrCTy, |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 7192 | true, CallArgs, Method) |
| 7193 | : EmitMessageSend(CGF, Return, ResultType, |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 7194 | EmitSelector(CGF, Sel), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7195 | ObjCSuper.getPointer(), ObjCTypes.SuperPtrCTy, |
John McCall | 1e3157b | 2015-09-10 22:27:50 +0000 | [diff] [blame] | 7196 | true, CallArgs, Method, Class, ObjCTypes); |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 7197 | } |
Fariborz Jahanian | 74b7722 | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 7198 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 7199 | llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CodeGenFunction &CGF, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7200 | Selector Sel) { |
| 7201 | Address Addr = EmitSelectorAddr(CGF, Sel); |
| 7202 | |
| 7203 | llvm::LoadInst* LI = CGF.Builder.CreateLoad(Addr); |
| 7204 | LI->setMetadata(CGM.getModule().getMDKindID("invariant.load"), |
| 7205 | llvm::MDNode::get(VMContext, None)); |
| 7206 | return LI; |
| 7207 | } |
| 7208 | |
| 7209 | Address CGObjCNonFragileABIMac::EmitSelectorAddr(CodeGenFunction &CGF, |
| 7210 | Selector Sel) { |
Fariborz Jahanian | 74b7722 | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 7211 | llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 7212 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7213 | CharUnits Align = CGF.getPointerAlign(); |
Fariborz Jahanian | 74b7722 | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 7214 | if (!Entry) { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 7215 | llvm::Constant *Casted = |
| 7216 | llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), |
| 7217 | ObjCTypes.SelectorPtrTy); |
Rafael Espindola | 8b27bdb | 2014-11-06 13:30:38 +0000 | [diff] [blame] | 7218 | Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, |
| 7219 | false, llvm::GlobalValue::PrivateLinkage, |
| 7220 | Casted, "OBJC_SELECTOR_REFERENCES_"); |
Michael Gottesman | 5c20596 | 2013-02-05 23:08:45 +0000 | [diff] [blame] | 7221 | Entry->setExternallyInitialized(true); |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 7222 | Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7223 | Entry->setAlignment(Align.getQuantity()); |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 7224 | CGM.addCompilerUsedGlobal(Entry); |
Fariborz Jahanian | 74b7722 | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 7225 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 7226 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7227 | return Address(Entry, Align); |
Fariborz Jahanian | 74b7722 | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 7228 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7229 | |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 7230 | /// EmitObjCIvarAssign - Code gen for assigning to a __strong object. |
Fariborz Jahanian | 7a95d72 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 7231 | /// objc_assign_ivar (id src, id *dst, ptrdiff_t) |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 7232 | /// |
| 7233 | void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7234 | llvm::Value *src, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7235 | Address dst, |
Fariborz Jahanian | 7a95d72 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 7236 | llvm::Value *ivarOffset) { |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 7237 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 7238 | if (!isa<llvm::PointerType>(SrcTy)) { |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 7239 | unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 7240 | assert(Size <= 8 && "does not support size > 8"); |
| 7241 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 7242 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 1b074a3 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 7243 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 7244 | } |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 7245 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 7246 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7247 | llvm::Value *args[] = { src, dst.getPointer(), ivarOffset }; |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 7248 | CGF.EmitNounwindRuntimeCall(ObjCTypes.getGcAssignIvarFn(), args); |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 7249 | } |
| 7250 | |
| 7251 | /// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object. |
| 7252 | /// objc_assign_strongCast (id src, id *dst) |
| 7253 | /// |
| 7254 | void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 7255 | CodeGen::CodeGenFunction &CGF, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7256 | llvm::Value *src, Address dst) { |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 7257 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 7258 | if (!isa<llvm::PointerType>(SrcTy)) { |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 7259 | unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 7260 | assert(Size <= 8 && "does not support size > 8"); |
| 7261 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 7262 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 1b074a3 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 7263 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 7264 | } |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 7265 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 7266 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7267 | llvm::Value *args[] = { src, dst.getPointer() }; |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 7268 | CGF.EmitNounwindRuntimeCall(ObjCTypes.getGcAssignStrongCastFn(), |
| 7269 | args, "weakassign"); |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 7270 | } |
| 7271 | |
Fariborz Jahanian | 5f21d2f | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 7272 | void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 7273 | CodeGen::CodeGenFunction &CGF, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7274 | Address DestPtr, |
| 7275 | Address SrcPtr, |
Fariborz Jahanian | 021510e | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 7276 | llvm::Value *Size) { |
Fariborz Jahanian | 5f21d2f | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 7277 | SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy); |
| 7278 | DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7279 | llvm::Value *args[] = { DestPtr.getPointer(), SrcPtr.getPointer(), Size }; |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 7280 | CGF.EmitNounwindRuntimeCall(ObjCTypes.GcMemmoveCollectableFn(), args); |
Fariborz Jahanian | 5f21d2f | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 7281 | } |
| 7282 | |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 7283 | /// EmitObjCWeakRead - Code gen for loading value of a __weak |
| 7284 | /// object: objc_read_weak (id *src) |
| 7285 | /// |
| 7286 | llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 7287 | CodeGen::CodeGenFunction &CGF, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7288 | Address AddrWeakObj) { |
| 7289 | llvm::Type *DestTy = AddrWeakObj.getElementType(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 7290 | AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 7291 | llvm::Value *read_weak = |
| 7292 | CGF.EmitNounwindRuntimeCall(ObjCTypes.getGcReadWeakFn(), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7293 | AddrWeakObj.getPointer(), "weakread"); |
Eli Friedman | a374b68 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 7294 | read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy); |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 7295 | return read_weak; |
| 7296 | } |
| 7297 | |
| 7298 | /// EmitObjCWeakAssign - Code gen for assigning to a __weak object. |
| 7299 | /// objc_assign_weak (id src, id *dst) |
| 7300 | /// |
| 7301 | void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7302 | llvm::Value *src, Address dst) { |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 7303 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 7304 | if (!isa<llvm::PointerType>(SrcTy)) { |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 7305 | unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 7306 | assert(Size <= 8 && "does not support size > 8"); |
| 7307 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 7308 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 1b074a3 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 7309 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 7310 | } |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 7311 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 7312 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7313 | llvm::Value *args[] = { src, dst.getPointer() }; |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 7314 | CGF.EmitNounwindRuntimeCall(ObjCTypes.getGcAssignWeakFn(), |
| 7315 | args, "weakassign"); |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 7316 | } |
| 7317 | |
| 7318 | /// EmitObjCGlobalAssign - Code gen for assigning to a __strong object. |
| 7319 | /// objc_assign_global (id src, id *dst) |
| 7320 | /// |
| 7321 | void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7322 | llvm::Value *src, Address dst, |
Fariborz Jahanian | 217af24 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 7323 | bool threadlocal) { |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 7324 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 7325 | if (!isa<llvm::PointerType>(SrcTy)) { |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 7326 | unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 7327 | assert(Size <= 8 && "does not support size > 8"); |
| 7328 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 7329 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 1b074a3 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 7330 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 7331 | } |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 7332 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 7333 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7334 | llvm::Value *args[] = { src, dst.getPointer() }; |
Fariborz Jahanian | 217af24 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 7335 | if (!threadlocal) |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 7336 | CGF.EmitNounwindRuntimeCall(ObjCTypes.getGcAssignGlobalFn(), |
| 7337 | args, "globalassign"); |
Fariborz Jahanian | 217af24 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 7338 | else |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 7339 | CGF.EmitNounwindRuntimeCall(ObjCTypes.getGcAssignThreadLocalFn(), |
| 7340 | args, "threadlocalassign"); |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 7341 | } |
Fariborz Jahanian | 74b7722 | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 7342 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 7343 | void |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 7344 | CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 7345 | const ObjCAtSynchronizedStmt &S) { |
David Chisnall | 3e57560 | 2011-03-25 17:46:35 +0000 | [diff] [blame] | 7346 | EmitAtSynchronizedStmt(CGF, S, |
| 7347 | cast<llvm::Function>(ObjCTypes.getSyncEnterFn()), |
| 7348 | cast<llvm::Function>(ObjCTypes.getSyncExitFn())); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 7349 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 7350 | |
John McCall | 2ca705e | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 7351 | llvm::Constant * |
Fariborz Jahanian | 831f0fc | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 7352 | CGObjCNonFragileABIMac::GetEHType(QualType T) { |
John McCall | 2ca705e | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 7353 | // There's a particular fixed type info for 'id'. |
Saleem Abdulrasool | e5f3eae | 2016-07-17 22:27:38 +0000 | [diff] [blame] | 7354 | if (T->isObjCIdType() || T->isObjCQualifiedIdType()) { |
| 7355 | auto *IDEHType = CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id"); |
Saleem Abdulrasool | 7093e21 | 2016-07-17 22:27:44 +0000 | [diff] [blame] | 7356 | if (!IDEHType) { |
John McCall | 2ca705e | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 7357 | IDEHType = |
Saleem Abdulrasool | e5f3eae | 2016-07-17 22:27:38 +0000 | [diff] [blame] | 7358 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false, |
| 7359 | llvm::GlobalValue::ExternalLinkage, nullptr, |
| 7360 | "OBJC_EHTYPE_id"); |
Saleem Abdulrasool | 7093e21 | 2016-07-17 22:27:44 +0000 | [diff] [blame] | 7361 | if (CGM.getTriple().isOSBinFormatCOFF()) |
| 7362 | IDEHType->setDLLStorageClass(getStorage(CGM, "OBJC_EHTYPE_id")); |
| 7363 | } |
John McCall | 2ca705e | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 7364 | return IDEHType; |
| 7365 | } |
| 7366 | |
| 7367 | // All other types should be Objective-C interface pointer types. |
Saleem Abdulrasool | e5f3eae | 2016-07-17 22:27:38 +0000 | [diff] [blame] | 7368 | const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>(); |
John McCall | 2ca705e | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 7369 | assert(PT && "Invalid @catch type."); |
Saleem Abdulrasool | e5f3eae | 2016-07-17 22:27:38 +0000 | [diff] [blame] | 7370 | |
John McCall | 2ca705e | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 7371 | const ObjCInterfaceType *IT = PT->getInterfaceType(); |
| 7372 | assert(IT && "Invalid @catch type."); |
Saleem Abdulrasool | e5f3eae | 2016-07-17 22:27:38 +0000 | [diff] [blame] | 7373 | |
John McCall | 2ca705e | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 7374 | return GetInterfaceEHType(IT->getDecl(), false); |
Saleem Abdulrasool | e5f3eae | 2016-07-17 22:27:38 +0000 | [diff] [blame] | 7375 | } |
John McCall | 2ca705e | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 7376 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 7377 | void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF, |
| 7378 | const ObjCAtTryStmt &S) { |
David Chisnall | 3e57560 | 2011-03-25 17:46:35 +0000 | [diff] [blame] | 7379 | EmitTryCatchStmt(CGF, S, |
| 7380 | cast<llvm::Function>(ObjCTypes.getObjCBeginCatchFn()), |
| 7381 | cast<llvm::Function>(ObjCTypes.getObjCEndCatchFn()), |
| 7382 | cast<llvm::Function>(ObjCTypes.getExceptionRethrowFn())); |
Daniel Dunbar | 0b0dcd9 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 7383 | } |
| 7384 | |
Anders Carlsson | 9ab53d1 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 7385 | /// EmitThrowStmt - Generate code for a throw statement. |
| 7386 | void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 1eab052 | 2013-01-10 19:02:56 +0000 | [diff] [blame] | 7387 | const ObjCAtThrowStmt &S, |
| 7388 | bool ClearInsertionPoint) { |
Anders Carlsson | 9ab53d1 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 7389 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
John McCall | 248512a | 2011-10-01 10:32:24 +0000 | [diff] [blame] | 7390 | llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr); |
Benjamin Kramer | 76399eb | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 7391 | Exception = CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 7392 | CGF.EmitRuntimeCallOrInvoke(ObjCTypes.getExceptionThrowFn(), Exception) |
John McCall | 17afe45 | 2010-10-16 08:21:07 +0000 | [diff] [blame] | 7393 | .setDoesNotReturn(); |
Anders Carlsson | 9ab53d1 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 7394 | } else { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 7395 | CGF.EmitRuntimeCallOrInvoke(ObjCTypes.getExceptionRethrowFn()) |
John McCall | 17afe45 | 2010-10-16 08:21:07 +0000 | [diff] [blame] | 7396 | .setDoesNotReturn(); |
Anders Carlsson | 9ab53d1 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 7397 | } |
| 7398 | |
John McCall | 17afe45 | 2010-10-16 08:21:07 +0000 | [diff] [blame] | 7399 | CGF.Builder.CreateUnreachable(); |
Fariborz Jahanian | 1eab052 | 2013-01-10 19:02:56 +0000 | [diff] [blame] | 7400 | if (ClearInsertionPoint) |
| 7401 | CGF.Builder.ClearInsertionPoint(); |
Anders Carlsson | 9ab53d1 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 7402 | } |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 7403 | |
John McCall | 2ca705e | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 7404 | llvm::Constant * |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 7405 | CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID, |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 7406 | bool ForDefinition) { |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 7407 | llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()]; |
Saleem Abdulrasool | e5f3eae | 2016-07-17 22:27:38 +0000 | [diff] [blame] | 7408 | StringRef ClassName = ID->getObjCRuntimeNameAsString(); |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 7409 | |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 7410 | // If we don't need a definition, return the entry if found or check |
| 7411 | // if we use an external reference. |
| 7412 | if (!ForDefinition) { |
| 7413 | if (Entry) |
| 7414 | return Entry; |
Daniel Dunbar | d7beeea | 2009-04-07 06:43:45 +0000 | [diff] [blame] | 7415 | |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 7416 | // If this type (or a super class) has the __objc_exception__ |
| 7417 | // attribute, emit an external reference. |
Saleem Abdulrasool | 7093e21 | 2016-07-17 22:27:44 +0000 | [diff] [blame] | 7418 | if (hasObjCExceptionAttribute(CGM.getContext(), ID)) { |
| 7419 | std::string EHTypeName = ("OBJC_EHTYPE_$_" + ClassName).str(); |
| 7420 | Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, |
| 7421 | false, llvm::GlobalValue::ExternalLinkage, |
| 7422 | nullptr, EHTypeName); |
| 7423 | if (CGM.getTriple().isOSBinFormatCOFF()) { |
| 7424 | if (ID->hasAttr<DLLExportAttr>()) |
| 7425 | Entry->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass); |
| 7426 | else if (ID->hasAttr<DLLImportAttr>()) |
| 7427 | Entry->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass); |
| 7428 | } |
| 7429 | return Entry; |
| 7430 | } |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 7431 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 7432 | |
Saleem Abdulrasool | e5f3eae | 2016-07-17 22:27:38 +0000 | [diff] [blame] | 7433 | // Otherwise we need to either make a new entry or fill in the initializer. |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 7434 | assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition"); |
Saleem Abdulrasool | e5f3eae | 2016-07-17 22:27:38 +0000 | [diff] [blame] | 7435 | |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 7436 | std::string VTableName = "objc_ehtype_vtable"; |
Saleem Abdulrasool | e5f3eae | 2016-07-17 22:27:38 +0000 | [diff] [blame] | 7437 | auto *VTableGV = CGM.getModule().getGlobalVariable(VTableName); |
Saleem Abdulrasool | 7093e21 | 2016-07-17 22:27:44 +0000 | [diff] [blame] | 7438 | if (!VTableGV) { |
Saleem Abdulrasool | e5f3eae | 2016-07-17 22:27:38 +0000 | [diff] [blame] | 7439 | VTableGV = |
| 7440 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy, false, |
| 7441 | llvm::GlobalValue::ExternalLinkage, nullptr, |
| 7442 | VTableName); |
Saleem Abdulrasool | 7093e21 | 2016-07-17 22:27:44 +0000 | [diff] [blame] | 7443 | if (CGM.getTriple().isOSBinFormatCOFF()) |
| 7444 | VTableGV->setDLLStorageClass(getStorage(CGM, VTableName)); |
| 7445 | } |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 7446 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 7447 | llvm::Value *VTableIdx = llvm::ConstantInt::get(CGM.Int32Ty, 2); |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 7448 | ConstantInitBuilder builder(CGM); |
| 7449 | auto values = builder.beginStruct(ObjCTypes.EHTypeTy); |
| 7450 | values.add(llvm::ConstantExpr::getGetElementPtr(VTableGV->getValueType(), |
| 7451 | VTableGV, VTableIdx)); |
| 7452 | values.add(GetClassName(ID->getObjCRuntimeNameAsString())); |
| 7453 | values.add(GetClassGlobal((getClassSymbolPrefix() + ClassName).str())); |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 7454 | |
Rafael Espindola | 554256c | 2014-02-26 22:25:45 +0000 | [diff] [blame] | 7455 | llvm::GlobalValue::LinkageTypes L = ForDefinition |
| 7456 | ? llvm::GlobalValue::ExternalLinkage |
| 7457 | : llvm::GlobalValue::WeakAnyLinkage; |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 7458 | if (Entry) { |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 7459 | values.finishAndSetAsInitializer(Entry); |
| 7460 | Entry->setAlignment(CGM.getPointerAlign().getQuantity()); |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 7461 | } else { |
John McCall | 176f892 | 2016-11-30 02:39:18 +0000 | [diff] [blame] | 7462 | Entry = values.finishAndCreateGlobal("OBJC_EHTYPE_$_" + ClassName, |
| 7463 | CGM.getPointerAlign(), |
| 7464 | /*constant*/ false, |
| 7465 | L); |
Saleem Abdulrasool | 7093e21 | 2016-07-17 22:27:44 +0000 | [diff] [blame] | 7466 | if (CGM.getTriple().isOSBinFormatCOFF()) |
| 7467 | if (hasObjCExceptionAttribute(CGM.getContext(), ID)) |
| 7468 | if (ID->hasAttr<DLLExportAttr>()) |
| 7469 | Entry->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass); |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 7470 | } |
Rafael Espindola | 554256c | 2014-02-26 22:25:45 +0000 | [diff] [blame] | 7471 | assert(Entry->getLinkage() == L); |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 7472 | |
Saleem Abdulrasool | 7093e21 | 2016-07-17 22:27:44 +0000 | [diff] [blame] | 7473 | if (!CGM.getTriple().isOSBinFormatCOFF()) |
| 7474 | if (ID->getVisibility() == HiddenVisibility) |
| 7475 | Entry->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Saleem Abdulrasool | e5f3eae | 2016-07-17 22:27:38 +0000 | [diff] [blame] | 7476 | |
Rafael Espindola | 554256c | 2014-02-26 22:25:45 +0000 | [diff] [blame] | 7477 | if (ForDefinition) |
Saleem Abdulrasool | 3c628af | 2016-10-25 21:43:28 +0000 | [diff] [blame] | 7478 | if (CGM.getTriple().isOSBinFormatMachO()) |
| 7479 | Entry->setSection("__DATA,__objc_const"); |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 7480 | |
| 7481 | return Entry; |
| 7482 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 7483 | |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 7484 | /* *** */ |
| 7485 | |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 7486 | CodeGen::CGObjCRuntime * |
| 7487 | CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) { |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 7488 | switch (CGM.getLangOpts().ObjCRuntime.getKind()) { |
| 7489 | case ObjCRuntime::FragileMacOSX: |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 7490 | return new CGObjCMac(CGM); |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 7491 | |
| 7492 | case ObjCRuntime::MacOSX: |
| 7493 | case ObjCRuntime::iOS: |
Tim Northover | 756447a | 2015-10-30 16:30:36 +0000 | [diff] [blame] | 7494 | case ObjCRuntime::WatchOS: |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 7495 | return new CGObjCNonFragileABIMac(CGM); |
| 7496 | |
David Chisnall | b601c96 | 2012-07-03 20:49:52 +0000 | [diff] [blame] | 7497 | case ObjCRuntime::GNUstep: |
| 7498 | case ObjCRuntime::GCC: |
John McCall | 775086e | 2012-07-12 02:07:58 +0000 | [diff] [blame] | 7499 | case ObjCRuntime::ObjFW: |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 7500 | llvm_unreachable("these runtimes are not Mac runtimes"); |
| 7501 | } |
| 7502 | llvm_unreachable("bad runtime"); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 7503 | } |