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 | |
| 14 | #include "CGObjCRuntime.h" |
John McCall | ad7c5c1 | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 15 | #include "CGBlocks.h" |
John McCall | ed1ae86 | 2011-01-28 11:13:47 +0000 | [diff] [blame] | 16 | #include "CGCleanup.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" |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 20 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | 221fa94 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 21 | #include "clang/AST/Decl.h" |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | 15b73de | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 23 | #include "clang/AST/RecordLayout.h" |
Chris Lattner | f0b64d7 | 2009-04-26 01:32:48 +0000 | [diff] [blame] | 24 | #include "clang/AST/StmtObjC.h" |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 25 | #include "clang/Basic/LangOptions.h" |
Mark Lacey | a8e7df3 | 2013-10-30 21:53:58 +0000 | [diff] [blame] | 26 | #include "clang/CodeGen/CGFunctionInfo.h" |
Chandler Carruth | 8509824 | 2010-06-15 23:19:56 +0000 | [diff] [blame] | 27 | #include "clang/Frontend/CodeGenOptions.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/DenseSet.h" |
| 29 | #include "llvm/ADT/SetVector.h" |
| 30 | #include "llvm/ADT/SmallPtrSet.h" |
| 31 | #include "llvm/ADT/SmallString.h" |
Chandler Carruth | c80ceea | 2014-03-04 11:02:08 +0000 | [diff] [blame] | 32 | #include "llvm/IR/CallSite.h" |
Chandler Carruth | ffd5551 | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 33 | #include "llvm/IR/DataLayout.h" |
| 34 | #include "llvm/IR/InlineAsm.h" |
| 35 | #include "llvm/IR/IntrinsicInst.h" |
| 36 | #include "llvm/IR/LLVMContext.h" |
| 37 | #include "llvm/IR/Module.h" |
Daniel Dunbar | d027a92 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 38 | #include "llvm/Support/raw_ostream.h" |
Torok Edwin | db71492 | 2009-08-24 13:25:12 +0000 | [diff] [blame] | 39 | #include <cstdio> |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 40 | |
| 41 | using namespace clang; |
Daniel Dunbar | 41cf9de | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 42 | using namespace CodeGen; |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 43 | |
| 44 | namespace { |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 45 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 46 | // FIXME: We should find a nicer way to make the labels for metadata, string |
| 47 | // concatenation is lame. |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 48 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 49 | class ObjCCommonTypesHelper { |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 50 | protected: |
| 51 | llvm::LLVMContext &VMContext; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 52 | |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 53 | private: |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 54 | // The types of these functions don't really matter because we |
| 55 | // should always bitcast before calling them. |
| 56 | |
| 57 | /// id objc_msgSend (id, SEL, ...) |
| 58 | /// |
| 59 | /// The default messenger, used for sends whose ABI is unchanged from |
| 60 | /// the all-integer/pointer case. |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 61 | llvm::Constant *getMessageSendFn() const { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 62 | // Add the non-lazy-bind attribute, since objc_msgSend is likely to |
| 63 | // be called a lot. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 64 | llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy }; |
Bill Wendling | 8594fcb | 2013-01-31 00:30:05 +0000 | [diff] [blame] | 65 | return |
| 66 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 67 | params, true), |
| 68 | "objc_msgSend", |
| 69 | llvm::AttributeSet::get(CGM.getLLVMContext(), |
| 70 | llvm::AttributeSet::FunctionIndex, |
| 71 | llvm::Attribute::NonLazyBind)); |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 72 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 73 | |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 74 | /// void objc_msgSend_stret (id, SEL, ...) |
| 75 | /// |
| 76 | /// The messenger used when the return value is an aggregate returned |
| 77 | /// by indirect reference in the first argument, and therefore the |
| 78 | /// self and selector parameters are shifted over by one. |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 79 | llvm::Constant *getMessageSendStretFn() const { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 80 | llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy }; |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 81 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy, |
| 82 | params, true), |
| 83 | "objc_msgSend_stret"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 84 | |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 85 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 86 | |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 87 | /// [double | long double] objc_msgSend_fpret(id self, SEL op, ...) |
| 88 | /// |
| 89 | /// The messenger used when the return value is returned on the x87 |
| 90 | /// floating-point stack; without a special entrypoint, the nil case |
| 91 | /// would be unbalanced. |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 92 | llvm::Constant *getMessageSendFpretFn() const { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 93 | llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy }; |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 94 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.DoubleTy, |
| 95 | params, true), |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 96 | "objc_msgSend_fpret"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 97 | |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 98 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 99 | |
Anders Carlsson | 2f1a6c3 | 2011-10-31 16:27:11 +0000 | [diff] [blame] | 100 | /// _Complex long double objc_msgSend_fp2ret(id self, SEL op, ...) |
| 101 | /// |
| 102 | /// The messenger used when the return value is returned in two values on the |
| 103 | /// x87 floating point stack; without a special entrypoint, the nil case |
| 104 | /// would be unbalanced. Only used on 64-bit X86. |
| 105 | llvm::Constant *getMessageSendFp2retFn() const { |
| 106 | llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy }; |
| 107 | llvm::Type *longDoubleType = llvm::Type::getX86_FP80Ty(VMContext); |
| 108 | llvm::Type *resultType = |
| 109 | llvm::StructType::get(longDoubleType, longDoubleType, NULL); |
| 110 | |
| 111 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(resultType, |
| 112 | params, true), |
| 113 | "objc_msgSend_fp2ret"); |
| 114 | } |
| 115 | |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 116 | /// id objc_msgSendSuper(struct objc_super *super, SEL op, ...) |
| 117 | /// |
| 118 | /// The messenger used for super calls, which have different dispatch |
| 119 | /// semantics. The class passed is the superclass of the current |
| 120 | /// class. |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 121 | llvm::Constant *getMessageSendSuperFn() const { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 122 | llvm::Type *params[] = { SuperPtrTy, SelectorPtrTy }; |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 123 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 124 | params, true), |
| 125 | "objc_msgSendSuper"); |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 126 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 127 | |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 128 | /// id objc_msgSendSuper2(struct objc_super *super, SEL op, ...) |
| 129 | /// |
| 130 | /// A slightly different messenger used for super calls. The class |
| 131 | /// passed is the current class. |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 132 | llvm::Constant *getMessageSendSuperFn2() const { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 133 | llvm::Type *params[] = { SuperPtrTy, SelectorPtrTy }; |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 134 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 135 | params, true), |
| 136 | "objc_msgSendSuper2"); |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 137 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 138 | |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 139 | /// void objc_msgSendSuper_stret(void *stretAddr, struct objc_super *super, |
| 140 | /// SEL op, ...) |
| 141 | /// |
| 142 | /// The messenger used for super calls which return an aggregate indirectly. |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 143 | llvm::Constant *getMessageSendSuperStretFn() const { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 144 | llvm::Type *params[] = { Int8PtrTy, SuperPtrTy, SelectorPtrTy }; |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 145 | return CGM.CreateRuntimeFunction( |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 146 | llvm::FunctionType::get(CGM.VoidTy, params, true), |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 147 | "objc_msgSendSuper_stret"); |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 148 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 149 | |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 150 | /// void objc_msgSendSuper2_stret(void * stretAddr, struct objc_super *super, |
| 151 | /// SEL op, ...) |
| 152 | /// |
| 153 | /// objc_msgSendSuper_stret with the super2 semantics. |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 154 | llvm::Constant *getMessageSendSuperStretFn2() const { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 155 | llvm::Type *params[] = { Int8PtrTy, SuperPtrTy, SelectorPtrTy }; |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 156 | return CGM.CreateRuntimeFunction( |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 157 | llvm::FunctionType::get(CGM.VoidTy, params, true), |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 158 | "objc_msgSendSuper2_stret"); |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 159 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 160 | |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 161 | llvm::Constant *getMessageSendSuperFpretFn() const { |
| 162 | // There is no objc_msgSendSuper_fpret? How can that work? |
| 163 | return getMessageSendSuperFn(); |
| 164 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 165 | |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 166 | llvm::Constant *getMessageSendSuperFpretFn2() const { |
| 167 | // There is no objc_msgSendSuper_fpret? How can that work? |
| 168 | return getMessageSendSuperFn2(); |
| 169 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 170 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 171 | protected: |
| 172 | CodeGen::CodeGenModule &CGM; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 173 | |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 174 | public: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 175 | llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy; |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 176 | llvm::Type *Int8PtrTy, *Int8PtrPtrTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 177 | |
Daniel Dunbar | 5d71559 | 2008-08-12 05:28:47 +0000 | [diff] [blame] | 178 | /// ObjectPtrTy - LLVM type for object handles (typeof(id)) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 179 | llvm::Type *ObjectPtrTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 180 | |
Fariborz Jahanian | 406b117 | 2008-11-18 20:18:11 +0000 | [diff] [blame] | 181 | /// PtrObjectPtrTy - LLVM type for id * |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 182 | llvm::Type *PtrObjectPtrTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 183 | |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 184 | /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL)) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 185 | llvm::Type *SelectorPtrTy; |
Douglas Gregor | 020de32 | 2012-01-17 18:36:30 +0000 | [diff] [blame] | 186 | |
| 187 | private: |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 188 | /// ProtocolPtrTy - LLVM type for external protocol handles |
| 189 | /// (typeof(Protocol)) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 190 | llvm::Type *ExternalProtocolPtrTy; |
Douglas Gregor | 020de32 | 2012-01-17 18:36:30 +0000 | [diff] [blame] | 191 | |
| 192 | public: |
| 193 | llvm::Type *getExternalProtocolPtrTy() { |
| 194 | if (!ExternalProtocolPtrTy) { |
| 195 | // FIXME: It would be nice to unify this with the opaque type, so that the |
| 196 | // IR comes out a bit cleaner. |
| 197 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 198 | ASTContext &Ctx = CGM.getContext(); |
| 199 | llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType()); |
| 200 | ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T); |
| 201 | } |
| 202 | |
| 203 | return ExternalProtocolPtrTy; |
| 204 | } |
| 205 | |
Daniel Dunbar | c722b85 | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 206 | // SuperCTy - clang type for struct objc_super. |
| 207 | QualType SuperCTy; |
| 208 | // SuperPtrCTy - clang type for struct objc_super *. |
| 209 | QualType SuperPtrCTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 210 | |
Daniel Dunbar | f6397fe | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 211 | /// SuperTy - LLVM type for struct objc_super. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 212 | llvm::StructType *SuperTy; |
Daniel Dunbar | 97ff50d | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 213 | /// SuperPtrTy - LLVM type for struct objc_super *. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 214 | llvm::Type *SuperPtrTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 215 | |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 216 | /// PropertyTy - LLVM type for struct objc_property (struct _prop_t |
| 217 | /// in GCC parlance). |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 218 | llvm::StructType *PropertyTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 219 | |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 220 | /// PropertyListTy - LLVM type for struct objc_property_list |
| 221 | /// (_prop_list_t in GCC parlance). |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 222 | llvm::StructType *PropertyListTy; |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 223 | /// PropertyListPtrTy - LLVM type for struct objc_property_list*. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 224 | llvm::Type *PropertyListPtrTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 225 | |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 226 | // MethodTy - LLVM type for struct objc_method. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 227 | llvm::StructType *MethodTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 228 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 229 | /// CacheTy - LLVM type for struct objc_cache. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 230 | llvm::Type *CacheTy; |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 231 | /// CachePtrTy - LLVM type for struct objc_cache *. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 232 | llvm::Type *CachePtrTy; |
Fariborz Jahanian | 0a3cfcc | 2011-06-22 20:21:51 +0000 | [diff] [blame] | 233 | |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 234 | llvm::Constant *getGetPropertyFn() { |
| 235 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 236 | ASTContext &Ctx = CGM.getContext(); |
| 237 | // id objc_getProperty (id, SEL, ptrdiff_t, bool) |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 238 | SmallVector<CanQualType,4> Params; |
John McCall | 2da83a3 | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 239 | CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType()); |
| 240 | CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType()); |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 241 | Params.push_back(IdType); |
| 242 | Params.push_back(SelType); |
David Chisnall | 08a4525 | 2011-03-22 20:03:13 +0000 | [diff] [blame] | 243 | Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified()); |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 244 | Params.push_back(Ctx.BoolTy); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 245 | llvm::FunctionType *FTy = |
Reid Kleckner | 4982b82 | 2014-01-31 22:54:50 +0000 | [diff] [blame] | 246 | Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(IdType, false, Params, |
| 247 | FunctionType::ExtInfo(), |
John McCall | 8dda7b2 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 248 | RequiredArgs::All)); |
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) |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 256 | SmallVector<CanQualType,6> Params; |
John McCall | 2da83a3 | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 257 | CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType()); |
| 258 | CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType()); |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 259 | Params.push_back(IdType); |
| 260 | Params.push_back(SelType); |
David Chisnall | 08a4525 | 2011-03-22 20:03:13 +0000 | [diff] [blame] | 261 | Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified()); |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 262 | Params.push_back(IdType); |
| 263 | Params.push_back(Ctx.BoolTy); |
| 264 | Params.push_back(Ctx.BoolTy); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 265 | llvm::FunctionType *FTy = |
Reid Kleckner | 4982b82 | 2014-01-31 22:54:50 +0000 | [diff] [blame] | 266 | Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, false, |
| 267 | Params, |
| 268 | FunctionType::ExtInfo(), |
John McCall | 8dda7b2 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 269 | RequiredArgs::All)); |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 270 | return CGM.CreateRuntimeFunction(FTy, "objc_setProperty"); |
| 271 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 272 | |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 273 | llvm::Constant *getOptimizedSetPropertyFn(bool atomic, bool copy) { |
| 274 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 275 | ASTContext &Ctx = CGM.getContext(); |
| 276 | // void objc_setProperty_atomic(id self, SEL _cmd, |
| 277 | // id newValue, ptrdiff_t offset); |
| 278 | // void objc_setProperty_nonatomic(id self, SEL _cmd, |
| 279 | // id newValue, ptrdiff_t offset); |
| 280 | // void objc_setProperty_atomic_copy(id self, SEL _cmd, |
| 281 | // id newValue, ptrdiff_t offset); |
| 282 | // void objc_setProperty_nonatomic_copy(id self, SEL _cmd, |
| 283 | // id newValue, ptrdiff_t offset); |
| 284 | |
| 285 | SmallVector<CanQualType,4> Params; |
| 286 | CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType()); |
| 287 | CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType()); |
| 288 | Params.push_back(IdType); |
| 289 | Params.push_back(SelType); |
| 290 | Params.push_back(IdType); |
| 291 | Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified()); |
| 292 | llvm::FunctionType *FTy = |
Reid Kleckner | 4982b82 | 2014-01-31 22:54:50 +0000 | [diff] [blame] | 293 | Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, false, |
| 294 | Params, |
John McCall | 8dda7b2 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 295 | FunctionType::ExtInfo(), |
| 296 | RequiredArgs::All)); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 297 | const char *name; |
| 298 | if (atomic && copy) |
| 299 | name = "objc_setProperty_atomic_copy"; |
| 300 | else if (atomic && !copy) |
| 301 | name = "objc_setProperty_atomic"; |
| 302 | else if (!atomic && copy) |
| 303 | name = "objc_setProperty_nonatomic_copy"; |
| 304 | else |
| 305 | name = "objc_setProperty_nonatomic"; |
| 306 | |
| 307 | return CGM.CreateRuntimeFunction(FTy, name); |
| 308 | } |
Fariborz Jahanian | 5a8c203 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 309 | |
| 310 | llvm::Constant *getCopyStructFn() { |
| 311 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 312 | ASTContext &Ctx = CGM.getContext(); |
| 313 | // void objc_copyStruct (void *, const void *, size_t, bool, bool) |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 314 | SmallVector<CanQualType,5> Params; |
Fariborz Jahanian | 5a8c203 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 315 | Params.push_back(Ctx.VoidPtrTy); |
| 316 | Params.push_back(Ctx.VoidPtrTy); |
| 317 | Params.push_back(Ctx.LongTy); |
| 318 | Params.push_back(Ctx.BoolTy); |
| 319 | Params.push_back(Ctx.BoolTy); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 320 | llvm::FunctionType *FTy = |
Reid Kleckner | 4982b82 | 2014-01-31 22:54:50 +0000 | [diff] [blame] | 321 | Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, false, |
| 322 | Params, |
| 323 | FunctionType::ExtInfo(), |
John McCall | 8dda7b2 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 324 | RequiredArgs::All)); |
Fariborz Jahanian | 5a8c203 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 325 | return CGM.CreateRuntimeFunction(FTy, "objc_copyStruct"); |
| 326 | } |
| 327 | |
Fariborz Jahanian | 1e1b549 | 2012-01-06 18:07:23 +0000 | [diff] [blame] | 328 | /// This routine declares and returns address of: |
| 329 | /// void objc_copyCppObjectAtomic( |
| 330 | /// void *dest, const void *src, |
| 331 | /// void (*copyHelper) (void *dest, const void *source)); |
| 332 | llvm::Constant *getCppAtomicObjectFunction() { |
| 333 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 334 | ASTContext &Ctx = CGM.getContext(); |
| 335 | /// void objc_copyCppObjectAtomic(void *dest, const void *src, void *helper); |
| 336 | SmallVector<CanQualType,3> Params; |
| 337 | Params.push_back(Ctx.VoidPtrTy); |
| 338 | Params.push_back(Ctx.VoidPtrTy); |
| 339 | Params.push_back(Ctx.VoidPtrTy); |
| 340 | llvm::FunctionType *FTy = |
Reid Kleckner | 4982b82 | 2014-01-31 22:54:50 +0000 | [diff] [blame] | 341 | Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, false, |
| 342 | Params, |
| 343 | FunctionType::ExtInfo(), |
John McCall | 8dda7b2 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 344 | RequiredArgs::All)); |
Fariborz Jahanian | 1e1b549 | 2012-01-06 18:07:23 +0000 | [diff] [blame] | 345 | return CGM.CreateRuntimeFunction(FTy, "objc_copyCppObjectAtomic"); |
| 346 | } |
| 347 | |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 348 | llvm::Constant *getEnumerationMutationFn() { |
Daniel Dunbar | 9d82da4 | 2009-07-11 20:32:50 +0000 | [diff] [blame] | 349 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 350 | ASTContext &Ctx = CGM.getContext(); |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 351 | // void objc_enumerationMutation (id) |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 352 | SmallVector<CanQualType,1> Params; |
John McCall | 2da83a3 | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 353 | Params.push_back(Ctx.getCanonicalParamType(Ctx.getObjCIdType())); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 354 | llvm::FunctionType *FTy = |
Reid Kleckner | 4982b82 | 2014-01-31 22:54:50 +0000 | [diff] [blame] | 355 | Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, false, |
| 356 | Params, |
| 357 | FunctionType::ExtInfo(), |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 358 | RequiredArgs::All)); |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 359 | return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation"); |
| 360 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 361 | |
Fariborz Jahanian | eee54df | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 362 | /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function. |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 363 | llvm::Constant *getGcReadWeakFn() { |
| 364 | // id objc_read_weak (id *) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 365 | llvm::Type *args[] = { ObjectPtrTy->getPointerTo() }; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 366 | llvm::FunctionType *FTy = |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 367 | llvm::FunctionType::get(ObjectPtrTy, args, false); |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 368 | return CGM.CreateRuntimeFunction(FTy, "objc_read_weak"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 369 | } |
| 370 | |
Fariborz Jahanian | eee54df | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 371 | /// GcAssignWeakFn -- LLVM objc_assign_weak function. |
Chris Lattner | 6fdd57c | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 372 | llvm::Constant *getGcAssignWeakFn() { |
| 373 | // id objc_assign_weak (id, id *) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 374 | llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() }; |
Chris Lattner | 6fdd57c | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 375 | llvm::FunctionType *FTy = |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 376 | llvm::FunctionType::get(ObjectPtrTy, args, false); |
Chris Lattner | 6fdd57c | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 377 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak"); |
| 378 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 379 | |
Fariborz Jahanian | eee54df | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 380 | /// GcAssignGlobalFn -- LLVM objc_assign_global function. |
Chris Lattner | 0a696a42 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 381 | llvm::Constant *getGcAssignGlobalFn() { |
| 382 | // id objc_assign_global(id, id *) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 383 | llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() }; |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 384 | llvm::FunctionType *FTy = |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 385 | llvm::FunctionType::get(ObjectPtrTy, args, false); |
Chris Lattner | 0a696a42 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 386 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_global"); |
| 387 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 388 | |
Fariborz Jahanian | 217af24 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 389 | /// GcAssignThreadLocalFn -- LLVM objc_assign_threadlocal function. |
| 390 | llvm::Constant *getGcAssignThreadLocalFn() { |
| 391 | // id objc_assign_threadlocal(id src, id * dest) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 392 | llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() }; |
Fariborz Jahanian | 217af24 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 393 | llvm::FunctionType *FTy = |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 394 | llvm::FunctionType::get(ObjectPtrTy, args, false); |
Fariborz Jahanian | 217af24 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 395 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_threadlocal"); |
| 396 | } |
| 397 | |
Fariborz Jahanian | eee54df | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 398 | /// GcAssignIvarFn -- LLVM objc_assign_ivar function. |
Chris Lattner | 0a696a42 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 399 | llvm::Constant *getGcAssignIvarFn() { |
Fariborz Jahanian | 7a95d72 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 400 | // id objc_assign_ivar(id, id *, ptrdiff_t) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 401 | llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo(), |
| 402 | CGM.PtrDiffTy }; |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 403 | llvm::FunctionType *FTy = |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 404 | llvm::FunctionType::get(ObjectPtrTy, args, false); |
Chris Lattner | 0a696a42 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 405 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar"); |
| 406 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 407 | |
Fariborz Jahanian | 5f21d2f | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 408 | /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function. |
| 409 | llvm::Constant *GcMemmoveCollectableFn() { |
| 410 | // void *objc_memmove_collectable(void *dst, const void *src, size_t size) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 411 | llvm::Type *args[] = { Int8PtrTy, Int8PtrTy, LongTy }; |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 412 | llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, args, false); |
Fariborz Jahanian | 5f21d2f | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 413 | return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable"); |
| 414 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 415 | |
Fariborz Jahanian | eee54df | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 416 | /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function. |
Chris Lattner | 0a696a42 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 417 | llvm::Constant *getGcAssignStrongCastFn() { |
Fariborz Jahanian | 217af24 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 418 | // id objc_assign_strongCast(id, id *) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 419 | llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() }; |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 420 | llvm::FunctionType *FTy = |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 421 | llvm::FunctionType::get(ObjectPtrTy, args, false); |
Chris Lattner | 0a696a42 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 422 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast"); |
| 423 | } |
Anders Carlsson | 9ab53d1 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 424 | |
| 425 | /// ExceptionThrowFn - LLVM objc_exception_throw function. |
Chris Lattner | 0a696a42 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 426 | llvm::Constant *getExceptionThrowFn() { |
| 427 | // void objc_exception_throw(id) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 428 | llvm::Type *args[] = { ObjectPtrTy }; |
Chris Lattner | 0a696a42 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 429 | llvm::FunctionType *FTy = |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 430 | llvm::FunctionType::get(CGM.VoidTy, args, false); |
Chris Lattner | 0a696a42 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 431 | return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw"); |
| 432 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 433 | |
Fariborz Jahanian | 3336de1 | 2010-05-28 17:34:43 +0000 | [diff] [blame] | 434 | /// ExceptionRethrowFn - LLVM objc_exception_rethrow function. |
| 435 | llvm::Constant *getExceptionRethrowFn() { |
| 436 | // void objc_exception_rethrow(void) |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 437 | llvm::FunctionType *FTy = llvm::FunctionType::get(CGM.VoidTy, false); |
Fariborz Jahanian | 3336de1 | 2010-05-28 17:34:43 +0000 | [diff] [blame] | 438 | return CGM.CreateRuntimeFunction(FTy, "objc_exception_rethrow"); |
| 439 | } |
| 440 | |
Daniel Dunbar | 94ceb61 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 441 | /// SyncEnterFn - LLVM object_sync_enter function. |
Chris Lattner | dcceee7 | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 442 | llvm::Constant *getSyncEnterFn() { |
Aaron Ballman | 9c00446 | 2012-09-06 16:44:16 +0000 | [diff] [blame] | 443 | // int objc_sync_enter (id) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 444 | llvm::Type *args[] = { ObjectPtrTy }; |
Chris Lattner | dcceee7 | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 445 | llvm::FunctionType *FTy = |
Aaron Ballman | 9c00446 | 2012-09-06 16:44:16 +0000 | [diff] [blame] | 446 | llvm::FunctionType::get(CGM.IntTy, args, false); |
Chris Lattner | dcceee7 | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 447 | return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter"); |
| 448 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 449 | |
Daniel Dunbar | 94ceb61 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 450 | /// SyncExitFn - LLVM object_sync_exit function. |
Chris Lattner | 0a696a42 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 451 | llvm::Constant *getSyncExitFn() { |
Aaron Ballman | 9c00446 | 2012-09-06 16:44:16 +0000 | [diff] [blame] | 452 | // int objc_sync_exit (id) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 453 | llvm::Type *args[] = { ObjectPtrTy }; |
Chris Lattner | 0a696a42 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 454 | llvm::FunctionType *FTy = |
Aaron Ballman | 9c00446 | 2012-09-06 16:44:16 +0000 | [diff] [blame] | 455 | llvm::FunctionType::get(CGM.IntTy, args, false); |
Chris Lattner | 0a696a42 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 456 | return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit"); |
| 457 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 458 | |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 459 | llvm::Constant *getSendFn(bool IsSuper) const { |
| 460 | return IsSuper ? getMessageSendSuperFn() : getMessageSendFn(); |
| 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 *getSendFn2(bool IsSuper) const { |
| 464 | return IsSuper ? getMessageSendSuperFn2() : 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 *getSendStretFn(bool IsSuper) const { |
| 468 | return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn(); |
| 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 *getSendStretFn2(bool IsSuper) const { |
| 472 | return IsSuper ? getMessageSendSuperStretFn2() : 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 *getSendFpretFn(bool IsSuper) const { |
| 476 | return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn(); |
| 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 *getSendFpretFn2(bool IsSuper) const { |
| 480 | return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn(); |
| 481 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 482 | |
Anders Carlsson | 2f1a6c3 | 2011-10-31 16:27:11 +0000 | [diff] [blame] | 483 | llvm::Constant *getSendFp2retFn(bool IsSuper) const { |
| 484 | return IsSuper ? getMessageSendSuperFn() : getMessageSendFp2retFn(); |
| 485 | } |
| 486 | |
| 487 | llvm::Constant *getSendFp2RetFn2(bool IsSuper) const { |
| 488 | return IsSuper ? getMessageSendSuperFn2() : getMessageSendFp2retFn(); |
| 489 | } |
| 490 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 491 | ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm); |
| 492 | ~ObjCCommonTypesHelper(){} |
| 493 | }; |
Daniel Dunbar | f6397fe | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 494 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 495 | /// ObjCTypesHelper - Helper class that encapsulates lazy |
| 496 | /// construction of varies types used during ObjC generation. |
| 497 | class ObjCTypesHelper : public ObjCCommonTypesHelper { |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 498 | public: |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 499 | /// SymtabTy - LLVM type for struct objc_symtab. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 500 | llvm::StructType *SymtabTy; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 501 | /// SymtabPtrTy - LLVM type for struct objc_symtab *. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 502 | llvm::Type *SymtabPtrTy; |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 503 | /// ModuleTy - LLVM type for struct objc_module. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 504 | llvm::StructType *ModuleTy; |
Daniel Dunbar | cb515c8 | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 505 | |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 506 | /// ProtocolTy - LLVM type for struct objc_protocol. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 507 | llvm::StructType *ProtocolTy; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 508 | /// ProtocolPtrTy - LLVM type for struct objc_protocol *. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 509 | llvm::Type *ProtocolPtrTy; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 510 | /// ProtocolExtensionTy - LLVM type for struct |
| 511 | /// objc_protocol_extension. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 512 | llvm::StructType *ProtocolExtensionTy; |
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::Type *ProtocolExtensionPtrTy; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 516 | /// MethodDescriptionTy - LLVM type for struct |
| 517 | /// objc_method_description. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 518 | llvm::StructType *MethodDescriptionTy; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 519 | /// MethodDescriptionListTy - LLVM type for struct |
| 520 | /// objc_method_description_list. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 521 | llvm::StructType *MethodDescriptionListTy; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 522 | /// MethodDescriptionListPtrTy - LLVM type for struct |
| 523 | /// objc_method_description_list *. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 524 | llvm::Type *MethodDescriptionListPtrTy; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 525 | /// ProtocolListTy - LLVM type for struct objc_property_list. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 526 | llvm::StructType *ProtocolListTy; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 527 | /// ProtocolListPtrTy - LLVM type for struct objc_property_list*. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 528 | llvm::Type *ProtocolListPtrTy; |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 529 | /// CategoryTy - LLVM type for struct objc_category. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 530 | llvm::StructType *CategoryTy; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 531 | /// ClassTy - LLVM type for struct objc_class. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 532 | llvm::StructType *ClassTy; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 533 | /// ClassPtrTy - LLVM type for struct objc_class *. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 534 | llvm::Type *ClassPtrTy; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 535 | /// ClassExtensionTy - LLVM type for struct objc_class_ext. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 536 | llvm::StructType *ClassExtensionTy; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 537 | /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 538 | llvm::Type *ClassExtensionPtrTy; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 539 | // IvarTy - LLVM type for struct objc_ivar. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 540 | llvm::StructType *IvarTy; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 541 | /// IvarListTy - LLVM type for struct objc_ivar_list. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 542 | llvm::Type *IvarListTy; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 543 | /// IvarListPtrTy - LLVM type for struct objc_ivar_list *. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 544 | llvm::Type *IvarListPtrTy; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 545 | /// MethodListTy - LLVM type for struct objc_method_list. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 546 | llvm::Type *MethodListTy; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 547 | /// MethodListPtrTy - LLVM type for struct objc_method_list *. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 548 | llvm::Type *MethodListPtrTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 549 | |
Anders Carlsson | 9ff2248 | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 550 | /// ExceptionDataTy - LLVM type for struct _objc_exception_data. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 551 | llvm::Type *ExceptionDataTy; |
Fariborz Jahanian | 0a3cfcc | 2011-06-22 20:21:51 +0000 | [diff] [blame] | 552 | |
Anders Carlsson | 9ff2248 | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 553 | /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function. |
Chris Lattner | c6406db | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 554 | llvm::Constant *getExceptionTryEnterFn() { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 555 | llvm::Type *params[] = { ExceptionDataTy->getPointerTo() }; |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 556 | return CGM.CreateRuntimeFunction( |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 557 | llvm::FunctionType::get(CGM.VoidTy, params, false), |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 558 | "objc_exception_try_enter"); |
Chris Lattner | c6406db | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 559 | } |
Anders Carlsson | 9ff2248 | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 560 | |
| 561 | /// ExceptionTryExitFn - LLVM objc_exception_try_exit function. |
Chris Lattner | c6406db | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 562 | llvm::Constant *getExceptionTryExitFn() { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 563 | llvm::Type *params[] = { ExceptionDataTy->getPointerTo() }; |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 564 | return CGM.CreateRuntimeFunction( |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 565 | llvm::FunctionType::get(CGM.VoidTy, params, false), |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 566 | "objc_exception_try_exit"); |
Chris Lattner | c6406db | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 567 | } |
Anders Carlsson | 9ff2248 | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 568 | |
| 569 | /// ExceptionExtractFn - LLVM objc_exception_extract function. |
Chris Lattner | c6406db | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 570 | llvm::Constant *getExceptionExtractFn() { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 571 | llvm::Type *params[] = { ExceptionDataTy->getPointerTo() }; |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 572 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 573 | params, false), |
Chris Lattner | c6406db | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 574 | "objc_exception_extract"); |
Chris Lattner | c6406db | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 575 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 576 | |
Anders Carlsson | 9ff2248 | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 577 | /// ExceptionMatchFn - LLVM objc_exception_match function. |
Chris Lattner | c6406db | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 578 | llvm::Constant *getExceptionMatchFn() { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 579 | llvm::Type *params[] = { ClassPtrTy, ObjectPtrTy }; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 580 | return CGM.CreateRuntimeFunction( |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 581 | llvm::FunctionType::get(CGM.Int32Ty, params, false), |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 582 | "objc_exception_match"); |
| 583 | |
Chris Lattner | c6406db | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 584 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 585 | |
Anders Carlsson | 9ff2248 | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 586 | /// SetJmpFn - LLVM _setjmp function. |
Chris Lattner | c6406db | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 587 | llvm::Constant *getSetJmpFn() { |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 588 | // This is specifically the prototype for x86. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 589 | llvm::Type *params[] = { CGM.Int32Ty->getPointerTo() }; |
Bill Wendling | 8594fcb | 2013-01-31 00:30:05 +0000 | [diff] [blame] | 590 | return |
| 591 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.Int32Ty, |
| 592 | params, false), |
| 593 | "_setjmp", |
| 594 | llvm::AttributeSet::get(CGM.getLLVMContext(), |
| 595 | llvm::AttributeSet::FunctionIndex, |
| 596 | llvm::Attribute::NonLazyBind)); |
Chris Lattner | c6406db | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 597 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 598 | |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 599 | public: |
| 600 | ObjCTypesHelper(CodeGen::CodeGenModule &cgm); |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 601 | ~ObjCTypesHelper() {} |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 602 | }; |
| 603 | |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 604 | /// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 605 | /// modern abi |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 606 | class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper { |
Fariborz Jahanian | e4dc35d | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 607 | public: |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 608 | |
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* |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 613 | llvm::Type *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* |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 619 | llvm::Type *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* |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 625 | llvm::Type *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* |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 631 | llvm::Type *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* |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 640 | llvm::Type *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, ...) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 646 | llvm::Type *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 | 4e87c83 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 667 | // MessengerTy - Type of the messenger (shown as IMP above) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 668 | llvm::FunctionType *MessengerTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 669 | |
Fariborz Jahanian | 82c72e1 | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 670 | // SuperMessageRefTy - LLVM for: |
| 671 | // struct _super_message_ref_t { |
| 672 | // SUPER_IMP messenger; |
| 673 | // SEL name; |
| 674 | // }; |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 675 | llvm::StructType *SuperMessageRefTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 676 | |
Fariborz Jahanian | 82c72e1 | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 677 | // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t* |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 678 | llvm::Type *SuperMessageRefPtrTy; |
Daniel Dunbar | 0b0dcd9 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 679 | |
Chris Lattner | 2dfdb3e | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 680 | llvm::Constant *getMessageSendFixupFn() { |
| 681 | // id objc_msgSend_fixup(id, struct message_ref_t*, ...) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 682 | llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy }; |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 683 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 684 | params, true), |
Chris Lattner | 2dfdb3e | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 685 | "objc_msgSend_fixup"); |
| 686 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 687 | |
Chris Lattner | 2dfdb3e | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 688 | llvm::Constant *getMessageSendFpretFixupFn() { |
| 689 | // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 690 | llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy }; |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 691 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 692 | params, true), |
Chris Lattner | 2dfdb3e | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 693 | "objc_msgSend_fpret_fixup"); |
| 694 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 695 | |
Chris Lattner | 2dfdb3e | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 696 | llvm::Constant *getMessageSendStretFixupFn() { |
| 697 | // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 698 | llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy }; |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 699 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 700 | params, true), |
Chris Lattner | 2dfdb3e | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 701 | "objc_msgSend_stret_fixup"); |
| 702 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 703 | |
Chris Lattner | 2dfdb3e | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 704 | llvm::Constant *getMessageSendSuper2FixupFn() { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 705 | // id objc_msgSendSuper2_fixup (struct objc_super *, |
Chris Lattner | 2dfdb3e | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 706 | // struct _super_message_ref_t*, ...) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 707 | llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy }; |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 708 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 709 | params, true), |
Chris Lattner | 2dfdb3e | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 710 | "objc_msgSendSuper2_fixup"); |
| 711 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 712 | |
Chris Lattner | 2dfdb3e | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 713 | llvm::Constant *getMessageSendSuper2StretFixupFn() { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 714 | // id objc_msgSendSuper2_stret_fixup(struct objc_super *, |
Chris Lattner | 2dfdb3e | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 715 | // struct _super_message_ref_t*, ...) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 716 | llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy }; |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 717 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 718 | params, true), |
Chris Lattner | 2dfdb3e | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 719 | "objc_msgSendSuper2_stret_fixup"); |
| 720 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 721 | |
Chris Lattner | a7c00b4 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 722 | llvm::Constant *getObjCEndCatchFn() { |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 723 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy, false), |
Chris Lattner | a7c00b4 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 724 | "objc_end_catch"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 725 | |
Chris Lattner | a7c00b4 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 726 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 727 | |
Chris Lattner | a7c00b4 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 728 | llvm::Constant *getObjCBeginCatchFn() { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 729 | llvm::Type *params[] = { Int8PtrTy }; |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 730 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy, |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 731 | params, false), |
Chris Lattner | a7c00b4 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 732 | "objc_begin_catch"); |
| 733 | } |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 734 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 735 | llvm::StructType *EHTypeTy; |
| 736 | llvm::Type *EHTypePtrTy; |
Fariborz Jahanian | 0a3cfcc | 2011-06-22 20:21:51 +0000 | [diff] [blame] | 737 | |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 738 | ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm); |
| 739 | ~ObjCNonFragileABITypesHelper(){} |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 740 | }; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 741 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 742 | class CGObjCCommonMac : public CodeGen::CGObjCRuntime { |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 743 | public: |
| 744 | // FIXME - accessibility |
Fariborz Jahanian | 524bb20 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 745 | class GC_IVAR { |
Fariborz Jahanian | 3b0f886 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 746 | public: |
Eli Friedman | 8cbca20 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 747 | unsigned ivar_bytepos; |
| 748 | unsigned ivar_size; |
| 749 | GC_IVAR(unsigned bytepos = 0, unsigned size = 0) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 750 | : ivar_bytepos(bytepos), ivar_size(size) {} |
Daniel Dunbar | 22b0ada | 2009-04-23 01:29:05 +0000 | [diff] [blame] | 751 | |
| 752 | // Allow sorting based on byte pos. |
| 753 | bool operator<(const GC_IVAR &b) const { |
| 754 | return ivar_bytepos < b.ivar_bytepos; |
| 755 | } |
Fariborz Jahanian | 524bb20 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 756 | }; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 757 | |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 758 | class SKIP_SCAN { |
Daniel Dunbar | 7b89ace | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 759 | public: |
| 760 | unsigned skip; |
| 761 | unsigned scan; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 762 | SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0) |
Daniel Dunbar | 7b89ace | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 763 | : skip(_skip), scan(_scan) {} |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 764 | }; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 765 | |
Fariborz Jahanian | 196f938 | 2012-10-25 21:15:04 +0000 | [diff] [blame] | 766 | /// opcode for captured block variables layout 'instructions'. |
| 767 | /// In the following descriptions, 'I' is the value of the immediate field. |
| 768 | /// (field following the opcode). |
| 769 | /// |
| 770 | enum BLOCK_LAYOUT_OPCODE { |
| 771 | /// An operator which affects how the following layout should be |
| 772 | /// interpreted. |
| 773 | /// I == 0: Halt interpretation and treat everything else as |
| 774 | /// a non-pointer. Note that this instruction is equal |
| 775 | /// to '\0'. |
| 776 | /// I != 0: Currently unused. |
| 777 | BLOCK_LAYOUT_OPERATOR = 0, |
| 778 | |
| 779 | /// The next I+1 bytes do not contain a value of object pointer type. |
| 780 | /// Note that this can leave the stream unaligned, meaning that |
| 781 | /// subsequent word-size instructions do not begin at a multiple of |
| 782 | /// the pointer size. |
| 783 | BLOCK_LAYOUT_NON_OBJECT_BYTES = 1, |
| 784 | |
| 785 | /// The next I+1 words do not contain a value of object pointer type. |
| 786 | /// This is simply an optimized version of BLOCK_LAYOUT_BYTES for |
| 787 | /// when the required skip quantity is a multiple of the pointer size. |
| 788 | BLOCK_LAYOUT_NON_OBJECT_WORDS = 2, |
| 789 | |
| 790 | /// The next I+1 words are __strong pointers to Objective-C |
| 791 | /// objects or blocks. |
| 792 | BLOCK_LAYOUT_STRONG = 3, |
| 793 | |
| 794 | /// The next I+1 words are pointers to __block variables. |
| 795 | BLOCK_LAYOUT_BYREF = 4, |
| 796 | |
| 797 | /// The next I+1 words are __weak pointers to Objective-C |
| 798 | /// objects or blocks. |
| 799 | BLOCK_LAYOUT_WEAK = 5, |
| 800 | |
| 801 | /// The next I+1 words are __unsafe_unretained pointers to |
| 802 | /// Objective-C objects or blocks. |
| 803 | BLOCK_LAYOUT_UNRETAINED = 6 |
| 804 | |
| 805 | /// The next I+1 words are block or object pointers with some |
| 806 | /// as-yet-unspecified ownership semantics. If we add more |
| 807 | /// flavors of ownership semantics, values will be taken from |
| 808 | /// this range. |
| 809 | /// |
| 810 | /// This is included so that older tools can at least continue |
| 811 | /// processing the layout past such things. |
| 812 | //BLOCK_LAYOUT_OWNERSHIP_UNKNOWN = 7..10, |
| 813 | |
| 814 | /// All other opcodes are reserved. Halt interpretation and |
| 815 | /// treat everything else as opaque. |
| 816 | }; |
| 817 | |
| 818 | class RUN_SKIP { |
| 819 | public: |
| 820 | enum BLOCK_LAYOUT_OPCODE opcode; |
Fariborz Jahanian | 7778d61 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 821 | CharUnits block_var_bytepos; |
| 822 | CharUnits block_var_size; |
Fariborz Jahanian | 196f938 | 2012-10-25 21:15:04 +0000 | [diff] [blame] | 823 | RUN_SKIP(enum BLOCK_LAYOUT_OPCODE Opcode = BLOCK_LAYOUT_OPERATOR, |
Fariborz Jahanian | 7778d61 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 824 | CharUnits BytePos = CharUnits::Zero(), |
| 825 | CharUnits Size = CharUnits::Zero()) |
Fariborz Jahanian | 0c58ce9 | 2012-10-27 21:10:38 +0000 | [diff] [blame] | 826 | : opcode(Opcode), block_var_bytepos(BytePos), block_var_size(Size) {} |
Fariborz Jahanian | 196f938 | 2012-10-25 21:15:04 +0000 | [diff] [blame] | 827 | |
| 828 | // Allow sorting based on byte pos. |
| 829 | bool operator<(const RUN_SKIP &b) const { |
| 830 | return block_var_bytepos < b.block_var_bytepos; |
| 831 | } |
| 832 | }; |
| 833 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 834 | protected: |
Owen Anderson | ae86c19 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 835 | llvm::LLVMContext &VMContext; |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 836 | // FIXME! May not be needing this after all. |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 837 | unsigned ObjCABI; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 838 | |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 839 | // gc ivar layout bitmap calculation helper caches. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 840 | SmallVector<GC_IVAR, 16> SkipIvars; |
| 841 | SmallVector<GC_IVAR, 16> IvarsInfo; |
Fariborz Jahanian | 196f938 | 2012-10-25 21:15:04 +0000 | [diff] [blame] | 842 | |
| 843 | // arc/mrr layout of captured block literal variables. |
| 844 | SmallVector<RUN_SKIP, 16> RunSkipBlockVars; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 845 | |
Daniel Dunbar | c61d0e9 | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 846 | /// LazySymbols - Symbols to generate a lazy reference for. See |
| 847 | /// DefinedSymbols and FinishModule(). |
Daniel Dunbar | d027a92 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 848 | llvm::SetVector<IdentifierInfo*> LazySymbols; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 849 | |
Daniel Dunbar | c61d0e9 | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 850 | /// DefinedSymbols - External symbols which are defined by this |
| 851 | /// module. The symbols in this list and LazySymbols are used to add |
| 852 | /// special linker symbols which ensure that Objective-C modules are |
| 853 | /// linked properly. |
Daniel Dunbar | d027a92 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 854 | llvm::SetVector<IdentifierInfo*> DefinedSymbols; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 855 | |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 856 | /// ClassNames - uniqued class names. |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 857 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 858 | |
Daniel Dunbar | cb515c8 | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 859 | /// MethodVarNames - uniqued method variable names. |
| 860 | llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 861 | |
Fariborz Jahanian | 9adb2e6 | 2010-06-21 22:05:18 +0000 | [diff] [blame] | 862 | /// DefinedCategoryNames - list of category names in form Class_Category. |
| 863 | llvm::SetVector<std::string> DefinedCategoryNames; |
| 864 | |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 865 | /// MethodVarTypes - uniqued method type signatures. We have to use |
| 866 | /// a StringMap here because have no other unique reference. |
| 867 | llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 868 | |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 869 | /// MethodDefinitions - map of methods which have been defined in |
| 870 | /// this translation unit. |
| 871 | llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 872 | |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 873 | /// PropertyNames - uniqued method variable names. |
| 874 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames; |
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 | /// ClassReferences - uniqued class references. |
| 877 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 878 | |
Daniel Dunbar | cb515c8 | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 879 | /// SelectorReferences - uniqued selector references. |
| 880 | llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 881 | |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 882 | /// Protocols - Protocols for which an objc_protocol structure has |
| 883 | /// been emitted. Forward declarations are handled by creating an |
| 884 | /// empty structure whose initializer is filled in when/if defined. |
| 885 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 886 | |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 887 | /// DefinedProtocols - Protocols which have actually been |
| 888 | /// defined. We should not need this, see FIXME in GenerateProtocol. |
| 889 | llvm::DenseSet<IdentifierInfo*> DefinedProtocols; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 890 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 891 | /// DefinedClasses - List of defined classes. |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 892 | SmallVector<llvm::GlobalValue*, 16> DefinedClasses; |
Fariborz Jahanian | f322f2f | 2014-03-11 00:25:05 +0000 | [diff] [blame] | 893 | |
| 894 | /// ImplementedClasses - List of @implemented classes. |
| 895 | SmallVector<const ObjCInterfaceDecl*, 16> ImplementedClasses; |
Daniel Dunbar | 9a017d7 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 896 | |
| 897 | /// DefinedNonLazyClasses - List of defined "non-lazy" classes. |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 898 | SmallVector<llvm::GlobalValue*, 16> DefinedNonLazyClasses; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 899 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 900 | /// DefinedCategories - List of defined categories. |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 901 | SmallVector<llvm::GlobalValue*, 16> DefinedCategories; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 902 | |
Daniel Dunbar | 9a017d7 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 903 | /// DefinedNonLazyCategories - List of defined "non-lazy" categories. |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 904 | SmallVector<llvm::GlobalValue*, 16> DefinedNonLazyCategories; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 905 | |
Fariborz Jahanian | 0b1ccdc | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 906 | /// GetNameForMethod - Return a name for the given method. |
| 907 | /// \param[out] NameOut - The return value. |
| 908 | void GetNameForMethod(const ObjCMethodDecl *OMD, |
| 909 | const ObjCContainerDecl *CD, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 910 | SmallVectorImpl<char> &NameOut); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 911 | |
Fariborz Jahanian | 0b1ccdc | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 912 | /// GetMethodVarName - Return a unique constant for the given |
| 913 | /// selector's name. The return value has type char *. |
| 914 | llvm::Constant *GetMethodVarName(Selector Sel); |
| 915 | llvm::Constant *GetMethodVarName(IdentifierInfo *Ident); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 916 | |
Fariborz Jahanian | 0b1ccdc | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 917 | /// GetMethodVarType - Return a unique constant for the given |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 918 | /// method's type encoding string. The return value has type char *. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 919 | |
Fariborz Jahanian | 0b1ccdc | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 920 | // FIXME: This is a horrible name. |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 921 | llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D, |
| 922 | bool Extended = false); |
Daniel Dunbar | f5c1846 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 923 | llvm::Constant *GetMethodVarType(const FieldDecl *D); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 924 | |
Fariborz Jahanian | 0b1ccdc | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 925 | /// GetPropertyName - Return a unique constant for the given |
| 926 | /// name. The return value has type char *. |
| 927 | llvm::Constant *GetPropertyName(IdentifierInfo *Ident); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 928 | |
Fariborz Jahanian | 0b1ccdc | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 929 | // FIXME: This can be dropped once string functions are unified. |
| 930 | llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD, |
| 931 | const Decl *Container); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 932 | |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 933 | /// GetClassName - Return a unique constant for the given selector's |
| 934 | /// name. The return value has type char *. |
| 935 | llvm::Constant *GetClassName(IdentifierInfo *Ident); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 936 | |
Argyrios Kyrtzidis | 13257c5 | 2010-08-09 10:54:20 +0000 | [diff] [blame] | 937 | llvm::Function *GetMethodDefinition(const ObjCMethodDecl *MD); |
| 938 | |
Fariborz Jahanian | c559f3f | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 939 | /// BuildIvarLayout - Builds ivar layout bitmap for the class |
| 940 | /// implementation for the __strong or __weak case. |
| 941 | /// |
Fariborz Jahanian | 1bf7288 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 942 | llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI, |
| 943 | bool ForStrongLayout); |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 944 | |
Fariborz Jahanian | 1f78a9a | 2010-08-05 00:19:48 +0000 | [diff] [blame] | 945 | llvm::Constant *BuildIvarLayoutBitmap(std::string &BitMap); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 946 | |
Daniel Dunbar | 15bd888 | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 947 | void BuildAggrIvarRecordLayout(const RecordType *RT, |
Eli Friedman | 8cbca20 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 948 | unsigned int BytePos, bool ForStrongLayout, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 949 | bool &HasUnion); |
Daniel Dunbar | 36e2a1e | 2009-05-03 21:05:10 +0000 | [diff] [blame] | 950 | void BuildAggrIvarLayout(const ObjCImplementationDecl *OI, |
Fariborz Jahanian | 1bf7288 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 951 | const llvm::StructLayout *Layout, |
Fariborz Jahanian | 524bb20 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 952 | const RecordDecl *RD, |
Bill Wendling | f1a3fca | 2012-02-22 09:30:11 +0000 | [diff] [blame] | 953 | ArrayRef<const FieldDecl*> RecFields, |
Eli Friedman | 8cbca20 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 954 | unsigned int BytePos, bool ForStrongLayout, |
Fariborz Jahanian | a123b64 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 955 | bool &HasUnion); |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 956 | |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 957 | Qualifiers::ObjCLifetime getBlockCaptureLifetime(QualType QT, bool ByrefLayout); |
Fariborz Jahanian | 2dd7819 | 2012-11-02 22:51:18 +0000 | [diff] [blame] | 958 | |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 959 | void UpdateRunSkipBlockVars(bool IsByref, |
| 960 | Qualifiers::ObjCLifetime LifeTime, |
Fariborz Jahanian | 7778d61 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 961 | CharUnits FieldOffset, |
| 962 | CharUnits FieldSize); |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 963 | |
| 964 | void BuildRCBlockVarRecordLayout(const RecordType *RT, |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 965 | CharUnits BytePos, bool &HasUnion, |
| 966 | bool ByrefLayout=false); |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 967 | |
| 968 | void BuildRCRecordLayout(const llvm::StructLayout *RecLayout, |
| 969 | const RecordDecl *RD, |
| 970 | ArrayRef<const FieldDecl*> RecFields, |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 971 | CharUnits BytePos, bool &HasUnion, |
| 972 | bool ByrefLayout); |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 973 | |
Fariborz Jahanian | 23290b0 | 2012-11-01 18:32:55 +0000 | [diff] [blame] | 974 | uint64_t InlineLayoutInstruction(SmallVectorImpl<unsigned char> &Layout); |
| 975 | |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 976 | llvm::Constant *getBitmapBlockLayout(bool ComputeByrefLayout); |
| 977 | |
Fariborz Jahanian | 1bf7288 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 978 | |
Fariborz Jahanian | 01dff42 | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 979 | /// GetIvarLayoutName - Returns a unique constant for the given |
| 980 | /// ivar layout bitmap. |
| 981 | llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident, |
| 982 | const ObjCCommonTypesHelper &ObjCTypes); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 983 | |
Fariborz Jahanian | 066347e | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 984 | /// EmitPropertyList - Emit the given property list. The return |
| 985 | /// value has type PropertyListPtrTy. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 986 | llvm::Constant *EmitPropertyList(Twine Name, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 987 | const Decl *Container, |
Fariborz Jahanian | 066347e | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 988 | const ObjCContainerDecl *OCD, |
| 989 | const ObjCCommonTypesHelper &ObjCTypes); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 990 | |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 991 | /// EmitProtocolMethodTypes - Generate the array of extended method type |
| 992 | /// strings. The return value has type Int8PtrPtrTy. |
| 993 | llvm::Constant *EmitProtocolMethodTypes(Twine Name, |
Bill Wendling | cb5b5ff | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 994 | ArrayRef<llvm::Constant*> MethodTypes, |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 995 | const ObjCCommonTypesHelper &ObjCTypes); |
| 996 | |
Fariborz Jahanian | 751c1e7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 997 | /// PushProtocolProperties - Push protocol's property on the input stack. |
Bill Wendling | a515b58 | 2012-02-09 22:16:49 +0000 | [diff] [blame] | 998 | void PushProtocolProperties( |
| 999 | llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet, |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1000 | SmallVectorImpl<llvm::Constant*> &Properties, |
Bill Wendling | a515b58 | 2012-02-09 22:16:49 +0000 | [diff] [blame] | 1001 | const Decl *Container, |
Aaron Ballman | dc4bea4 | 2014-03-13 18:47:37 +0000 | [diff] [blame] | 1002 | const ObjCProtocolDecl *Proto, |
Bill Wendling | a515b58 | 2012-02-09 22:16:49 +0000 | [diff] [blame] | 1003 | const ObjCCommonTypesHelper &ObjCTypes); |
Fariborz Jahanian | 751c1e7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 1004 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1005 | /// GetProtocolRef - Return a reference to the internal protocol |
| 1006 | /// description, creating an empty one if it has not been |
| 1007 | /// defined. The return value has type ProtocolPtrTy. |
| 1008 | llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD); |
Fariborz Jahanian | 6772621 | 2009-03-08 20:18:37 +0000 | [diff] [blame] | 1009 | |
Daniel Dunbar | 30c6536 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 1010 | /// CreateMetadataVar - Create a global variable with internal |
| 1011 | /// linkage for use by the Objective-C runtime. |
| 1012 | /// |
| 1013 | /// This is a convenience wrapper which not only creates the |
| 1014 | /// variable, but also sets the section and alignment and adds the |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 1015 | /// global to the "llvm.used" list. |
Daniel Dunbar | 463cc8a | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 1016 | /// |
| 1017 | /// \param Name - The variable name. |
| 1018 | /// \param Init - The variable initializer; this is also used to |
| 1019 | /// define the type of the variable. |
| 1020 | /// \param Section - The section the variable should go into, or 0. |
| 1021 | /// \param Align - The alignment for the variable, or 0. |
| 1022 | /// \param AddToUsed - Whether the variable should be added to |
Daniel Dunbar | 4527d30 | 2009-04-14 17:42:51 +0000 | [diff] [blame] | 1023 | /// "llvm.used". |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1024 | llvm::GlobalVariable *CreateMetadataVar(Twine Name, |
Daniel Dunbar | 30c6536 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 1025 | llvm::Constant *Init, |
| 1026 | const char *Section, |
Daniel Dunbar | 463cc8a | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 1027 | unsigned Align, |
| 1028 | bool AddToUsed); |
Daniel Dunbar | 30c6536 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 1029 | |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1030 | CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
| 1031 | ReturnValueSlot Return, |
| 1032 | QualType ResultType, |
| 1033 | llvm::Value *Sel, |
| 1034 | llvm::Value *Arg0, |
| 1035 | QualType Arg0Ty, |
| 1036 | bool IsSuper, |
| 1037 | const CallArgList &CallArgs, |
| 1038 | const ObjCMethodDecl *OMD, |
| 1039 | const ObjCCommonTypesHelper &ObjCTypes); |
Daniel Dunbar | f5c1846 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 1040 | |
Daniel Dunbar | 5e63927 | 2010-04-25 20:39:01 +0000 | [diff] [blame] | 1041 | /// EmitImageInfo - Emit the image info marker used to encode some module |
| 1042 | /// level information. |
| 1043 | void EmitImageInfo(); |
| 1044 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1045 | public: |
Owen Anderson | ae86c19 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 1046 | CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1047 | CGObjCRuntime(cgm), VMContext(cgm.getLLVMContext()) { } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1048 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1049 | llvm::Constant *GenerateConstantString(const StringLiteral *SL) override; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1050 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1051 | llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD, |
| 1052 | const ObjCContainerDecl *CD=0) override; |
| 1053 | |
| 1054 | void GenerateProtocol(const ObjCProtocolDecl *PD) override; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1055 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1056 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 1057 | /// declaration, emitting it if necessary. The return value has type |
| 1058 | /// ProtocolPtrTy. |
| 1059 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1060 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1061 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 1062 | /// object for the given declaration, emitting it if needed. These |
| 1063 | /// forward references will be filled in with empty bodies if no |
| 1064 | /// definition is seen. The return value has type ProtocolPtrTy. |
| 1065 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1066 | llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM, |
| 1067 | const CGBlockInfo &blockInfo) override; |
| 1068 | llvm::Constant *BuildRCBlockLayout(CodeGen::CodeGenModule &CGM, |
| 1069 | const CGBlockInfo &blockInfo) override; |
| 1070 | |
| 1071 | llvm::Constant *BuildByrefLayout(CodeGen::CodeGenModule &CGM, |
| 1072 | QualType T) override; |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1073 | }; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1074 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1075 | class CGObjCMac : public CGObjCCommonMac { |
| 1076 | private: |
| 1077 | ObjCTypesHelper ObjCTypes; |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 1078 | |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 1079 | /// EmitModuleInfo - Another marker encoding module level |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1080 | /// information. |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 1081 | void EmitModuleInfo(); |
| 1082 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1083 | /// EmitModuleSymols - Emit module symbols, the list of defined |
| 1084 | /// classes and categories. The result has type SymtabPtrTy. |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 1085 | llvm::Constant *EmitModuleSymbols(); |
| 1086 | |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 1087 | /// FinishModule - Write out global data structures at the end of |
| 1088 | /// processing a translation unit. |
| 1089 | void FinishModule(); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1090 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1091 | /// EmitClassExtension - Generate the class extension structure used |
| 1092 | /// to store the weak ivar layout and properties. The return value |
| 1093 | /// has type ClassExtensionPtrTy. |
| 1094 | llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID); |
| 1095 | |
| 1096 | /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
| 1097 | /// for the given class. |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1098 | llvm::Value *EmitClassRef(CodeGenFunction &CGF, |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1099 | const ObjCInterfaceDecl *ID); |
Fariborz Jahanian | eb80c98 | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 1100 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1101 | llvm::Value *EmitClassRefFromId(CodeGenFunction &CGF, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1102 | IdentifierInfo *II); |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1103 | |
| 1104 | llvm::Value *EmitNSAutoreleasePoolClassRef(CodeGenFunction &CGF) override; |
| 1105 | |
Fariborz Jahanian | eb80c98 | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 1106 | /// EmitSuperClassRef - Emits reference to class's main metadata class. |
| 1107 | llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1108 | |
| 1109 | /// EmitIvarList - Emit the ivar list for the given |
| 1110 | /// implementation. If ForClass is true the list of class ivars |
| 1111 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 1112 | /// interface ivars will be emitted. The return value has type |
| 1113 | /// IvarListPtrTy. |
| 1114 | llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID, |
Fariborz Jahanian | b042a59 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 1115 | bool ForClass); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1116 | |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1117 | /// EmitMetaClass - Emit a forward reference to the class structure |
| 1118 | /// for the metaclass of the given interface. The return value has |
| 1119 | /// type ClassPtrTy. |
| 1120 | llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID); |
| 1121 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1122 | /// EmitMetaClass - Emit a class structure for the metaclass of the |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1123 | /// given implementation. The return value has type ClassPtrTy. |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1124 | llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID, |
| 1125 | llvm::Constant *Protocols, |
Bill Wendling | cb5b5ff | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 1126 | ArrayRef<llvm::Constant*> Methods); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1127 | |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1128 | llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1129 | |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1130 | llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1131 | |
| 1132 | /// EmitMethodList - Emit the method list for the given |
Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 1133 | /// implementation. The return value has type MethodListPtrTy. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1134 | llvm::Constant *EmitMethodList(Twine Name, |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1135 | const char *Section, |
Bill Wendling | cb5b5ff | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 1136 | ArrayRef<llvm::Constant*> Methods); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1137 | |
| 1138 | /// EmitMethodDescList - Emit a method description list for a list of |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1139 | /// method declarations. |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1140 | /// - TypeName: The name for the type containing the methods. |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 1141 | /// - IsProtocol: True iff these methods are for a protocol. |
| 1142 | /// - ClassMethds: True iff these are class methods. |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1143 | /// - Required: When true, only "required" methods are |
| 1144 | /// listed. Similarly, when false only "optional" methods are |
| 1145 | /// listed. For classes this should always be true. |
| 1146 | /// - begin, end: The method list to output. |
| 1147 | /// |
| 1148 | /// The return value has type MethodDescriptionListPtrTy. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1149 | llvm::Constant *EmitMethodDescList(Twine Name, |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1150 | const char *Section, |
Bill Wendling | cb5b5ff | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 1151 | ArrayRef<llvm::Constant*> Methods); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1152 | |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1153 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 1154 | /// declaration, emitting it if necessary. The return value has type |
| 1155 | /// ProtocolPtrTy. |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1156 | llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD) override; |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1157 | |
| 1158 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 1159 | /// object for the given declaration, emitting it if needed. These |
| 1160 | /// forward references will be filled in with empty bodies if no |
| 1161 | /// definition is seen. The return value has type ProtocolPtrTy. |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1162 | llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) override; |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1163 | |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1164 | /// EmitProtocolExtension - Generate the protocol extension |
| 1165 | /// structure used to store optional instance and class methods, and |
| 1166 | /// protocol properties. The return value has type |
| 1167 | /// ProtocolExtensionPtrTy. |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1168 | llvm::Constant * |
| 1169 | EmitProtocolExtension(const ObjCProtocolDecl *PD, |
Bill Wendling | cb5b5ff | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 1170 | ArrayRef<llvm::Constant*> OptInstanceMethods, |
| 1171 | ArrayRef<llvm::Constant*> OptClassMethods, |
| 1172 | ArrayRef<llvm::Constant*> MethodTypesExt); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1173 | |
| 1174 | /// EmitProtocolList - Generate the list of referenced |
| 1175 | /// protocols. The return value has type ProtocolListPtrTy. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1176 | llvm::Constant *EmitProtocolList(Twine Name, |
Daniel Dunbar | dec75f8 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1177 | ObjCProtocolDecl::protocol_iterator begin, |
| 1178 | ObjCProtocolDecl::protocol_iterator end); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1179 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1180 | /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, |
| 1181 | /// for the given selector. |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1182 | llvm::Value *EmitSelector(CodeGenFunction &CGF, Selector Sel, |
Fariborz Jahanian | 9240f3d | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 1183 | bool lval=false); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1184 | |
| 1185 | public: |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1186 | CGObjCMac(CodeGen::CodeGenModule &cgm); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1187 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1188 | llvm::Function *ModuleInitFunction() override; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1189 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1190 | CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
| 1191 | ReturnValueSlot Return, |
| 1192 | QualType ResultType, |
| 1193 | Selector Sel, llvm::Value *Receiver, |
| 1194 | const CallArgList &CallArgs, |
| 1195 | const ObjCInterfaceDecl *Class, |
| 1196 | const ObjCMethodDecl *Method) override; |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1197 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1198 | CodeGen::RValue |
Daniel Dunbar | 97db84c | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1199 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1200 | ReturnValueSlot Return, QualType ResultType, |
| 1201 | Selector Sel, const ObjCInterfaceDecl *Class, |
| 1202 | bool isCategoryImpl, llvm::Value *Receiver, |
| 1203 | bool IsClassMessage, const CallArgList &CallArgs, |
| 1204 | const ObjCMethodDecl *Method) override; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1205 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1206 | llvm::Value *GetClass(CodeGenFunction &CGF, |
| 1207 | const ObjCInterfaceDecl *ID) override; |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1208 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1209 | llvm::Value *GetSelector(CodeGenFunction &CGF, Selector Sel, |
| 1210 | bool lval = false) override; |
Fariborz Jahanian | f3648b8 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1211 | |
| 1212 | /// The NeXT/Apple runtimes do not support typed selectors; just emit an |
| 1213 | /// untyped one. |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1214 | llvm::Value *GetSelector(CodeGenFunction &CGF, |
| 1215 | const ObjCMethodDecl *Method) override; |
Fariborz Jahanian | f3648b8 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1216 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1217 | llvm::Constant *GetEHType(QualType T) override; |
John McCall | 2ca705e | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 1218 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1219 | void GenerateCategory(const ObjCCategoryImplDecl *CMD) override; |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1220 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1221 | void GenerateClass(const ObjCImplementationDecl *ClassDecl) override; |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1222 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1223 | void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) override {} |
David Chisnall | 92d436b | 2012-01-31 18:59:20 +0000 | [diff] [blame] | 1224 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1225 | llvm::Value *GenerateProtocolRef(CodeGenFunction &CGF, |
| 1226 | const ObjCProtocolDecl *PD) override; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1227 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1228 | llvm::Constant *GetPropertyGetFunction() override; |
| 1229 | llvm::Constant *GetPropertySetFunction() override; |
| 1230 | llvm::Constant *GetOptimizedPropertySetFunction(bool atomic, |
| 1231 | bool copy) override; |
| 1232 | llvm::Constant *GetGetStructFunction() override; |
| 1233 | llvm::Constant *GetSetStructFunction() override; |
| 1234 | llvm::Constant *GetCppAtomicObjectGetFunction() override; |
| 1235 | llvm::Constant *GetCppAtomicObjectSetFunction() override; |
| 1236 | llvm::Constant *EnumerationMutationFunction() override; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1237 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1238 | void EmitTryStmt(CodeGen::CodeGenFunction &CGF, |
| 1239 | const ObjCAtTryStmt &S) override; |
| 1240 | void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 1241 | const ObjCAtSynchronizedStmt &S) override; |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1242 | void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const Stmt &S); |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1243 | void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, const ObjCAtThrowStmt &S, |
| 1244 | bool ClearInsertionPoint=true) override; |
| 1245 | llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
| 1246 | llvm::Value *AddrWeakObj) override; |
| 1247 | void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
| 1248 | llvm::Value *src, llvm::Value *dst) override; |
| 1249 | void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
| 1250 | llvm::Value *src, llvm::Value *dest, |
| 1251 | bool threadlocal = false) override; |
| 1252 | void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
| 1253 | llvm::Value *src, llvm::Value *dest, |
| 1254 | llvm::Value *ivarOffset) override; |
| 1255 | void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
| 1256 | llvm::Value *src, llvm::Value *dest) override; |
| 1257 | void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF, |
| 1258 | llvm::Value *dest, llvm::Value *src, |
| 1259 | llvm::Value *size) override; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1260 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1261 | LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, QualType ObjectTy, |
| 1262 | llvm::Value *BaseValue, const ObjCIvarDecl *Ivar, |
| 1263 | unsigned CVRQualifiers) override; |
| 1264 | llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
| 1265 | const ObjCInterfaceDecl *Interface, |
| 1266 | const ObjCIvarDecl *Ivar) override; |
| 1267 | |
Fariborz Jahanian | 7bd3d1c | 2011-05-17 22:21:16 +0000 | [diff] [blame] | 1268 | /// GetClassGlobal - Return the global variable for the Objective-C |
| 1269 | /// class of the given name. |
Rafael Espindola | 554256c | 2014-02-26 22:25:45 +0000 | [diff] [blame] | 1270 | llvm::GlobalVariable *GetClassGlobal(const std::string &Name, |
Craig Topper | a798a9d | 2014-03-02 09:32:10 +0000 | [diff] [blame] | 1271 | bool Weak = false) override { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1272 | llvm_unreachable("CGObjCMac::GetClassGlobal"); |
Fariborz Jahanian | 7bd3d1c | 2011-05-17 22:21:16 +0000 | [diff] [blame] | 1273 | } |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1274 | }; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1275 | |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1276 | class CGObjCNonFragileABIMac : public CGObjCCommonMac { |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1277 | private: |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1278 | ObjCNonFragileABITypesHelper ObjCTypes; |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1279 | llvm::GlobalVariable* ObjCEmptyCacheVar; |
| 1280 | llvm::GlobalVariable* ObjCEmptyVtableVar; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1281 | |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1282 | /// SuperClassReferences - uniqued super class references. |
| 1283 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1284 | |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1285 | /// MetaClassReferences - uniqued meta class references. |
| 1286 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences; |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1287 | |
| 1288 | /// EHTypeReferences - uniqued class ehtype references. |
| 1289 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1290 | |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1291 | /// VTableDispatchMethods - List of methods for which we generate |
| 1292 | /// vtable-based message dispatch. |
| 1293 | llvm::DenseSet<Selector> VTableDispatchMethods; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1294 | |
Fariborz Jahanian | 6726055 | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 1295 | /// DefinedMetaClasses - List of defined meta-classes. |
| 1296 | std::vector<llvm::GlobalValue*> DefinedMetaClasses; |
| 1297 | |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1298 | /// isVTableDispatchedSelector - Returns true if SEL is a |
| 1299 | /// vtable-based selector. |
| 1300 | bool isVTableDispatchedSelector(Selector Sel); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1301 | |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1302 | /// FinishNonFragileABIModule - Write out global data structures at the end of |
| 1303 | /// processing a translation unit. |
| 1304 | void FinishNonFragileABIModule(); |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1305 | |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 1306 | /// AddModuleClassList - Add the given list of class pointers to the |
| 1307 | /// module with the provided symbol and section names. |
Bill Wendling | cb5b5ff | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 1308 | void AddModuleClassList(ArrayRef<llvm::GlobalValue*> Container, |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 1309 | const char *SymbolName, |
| 1310 | const char *SectionName); |
| 1311 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1312 | llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags, |
| 1313 | unsigned InstanceStart, |
| 1314 | unsigned InstanceSize, |
| 1315 | const ObjCImplementationDecl *ID); |
Fariborz Jahanian | 4723fb7 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 1316 | llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1317 | llvm::Constant *IsAGV, |
Fariborz Jahanian | 4723fb7 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 1318 | llvm::Constant *SuperClassGV, |
Fariborz Jahanian | 8220825 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 1319 | llvm::Constant *ClassRoGV, |
Rafael Espindola | 554256c | 2014-02-26 22:25:45 +0000 | [diff] [blame] | 1320 | bool HiddenVisibility, |
Fariborz Jahanian | f322f2f | 2014-03-11 00:25:05 +0000 | [diff] [blame] | 1321 | bool Weak); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1322 | |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1323 | llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1324 | |
Fariborz Jahanian | d9c28b8 | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 1325 | llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1326 | |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1327 | /// EmitMethodList - Emit the method list for the given |
| 1328 | /// implementation. The return value has type MethodListnfABITy. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1329 | llvm::Constant *EmitMethodList(Twine Name, |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1330 | const char *Section, |
Bill Wendling | cb5b5ff | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 1331 | ArrayRef<llvm::Constant*> Methods); |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 1332 | /// EmitIvarList - Emit the ivar list for the given |
| 1333 | /// implementation. If ForClass is true the list of class ivars |
| 1334 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 1335 | /// interface ivars will be emitted. The return value has type |
| 1336 | /// IvarListnfABIPtrTy. |
| 1337 | llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1338 | |
Fariborz Jahanian | 4e7ae06 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 1339 | llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | 3d3426f | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 1340 | const ObjCIvarDecl *Ivar, |
Eli Friedman | 8cbca20 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 1341 | unsigned long int offset); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1342 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1343 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 1344 | /// declaration, emitting it if necessary. The return value has type |
| 1345 | /// ProtocolPtrTy. |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1346 | llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD) override; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1347 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1348 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 1349 | /// object for the given declaration, emitting it if needed. These |
| 1350 | /// forward references will be filled in with empty bodies if no |
| 1351 | /// definition is seen. The return value has type ProtocolPtrTy. |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1352 | llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) override; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1353 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1354 | /// EmitProtocolList - Generate the list of referenced |
| 1355 | /// protocols. The return value has type ProtocolListPtrTy. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1356 | llvm::Constant *EmitProtocolList(Twine Name, |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1357 | ObjCProtocolDecl::protocol_iterator begin, |
Fariborz Jahanian | 3d9296e | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 1358 | ObjCProtocolDecl::protocol_iterator end); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1359 | |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1360 | CodeGen::RValue EmitVTableMessageSend(CodeGen::CodeGenFunction &CGF, |
| 1361 | ReturnValueSlot Return, |
| 1362 | QualType ResultType, |
| 1363 | Selector Sel, |
| 1364 | llvm::Value *Receiver, |
| 1365 | QualType Arg0Ty, |
| 1366 | bool IsSuper, |
| 1367 | const CallArgList &CallArgs, |
| 1368 | const ObjCMethodDecl *Method); |
Fariborz Jahanian | 7bd3d1c | 2011-05-17 22:21:16 +0000 | [diff] [blame] | 1369 | |
Daniel Dunbar | c6928bb | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 1370 | /// GetClassGlobal - Return the global variable for the Objective-C |
| 1371 | /// class of the given name. |
Rafael Espindola | 554256c | 2014-02-26 22:25:45 +0000 | [diff] [blame] | 1372 | llvm::GlobalVariable *GetClassGlobal(const std::string &Name, |
Craig Topper | a798a9d | 2014-03-02 09:32:10 +0000 | [diff] [blame] | 1373 | bool Weak = false) override; |
Rafael Espindola | 554256c | 2014-02-26 22:25:45 +0000 | [diff] [blame] | 1374 | |
Fariborz Jahanian | 33f66e6 | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 1375 | /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1376 | /// for the given class reference. |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1377 | llvm::Value *EmitClassRef(CodeGenFunction &CGF, |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1378 | const ObjCInterfaceDecl *ID); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1379 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1380 | llvm::Value *EmitClassRefFromId(CodeGenFunction &CGF, |
Rafael Espindola | 554256c | 2014-02-26 22:25:45 +0000 | [diff] [blame] | 1381 | IdentifierInfo *II, bool Weak); |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1382 | |
| 1383 | llvm::Value *EmitNSAutoreleasePoolClassRef(CodeGenFunction &CGF) override; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1384 | |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1385 | /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
| 1386 | /// for the given super class reference. |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1387 | llvm::Value *EmitSuperClassRef(CodeGenFunction &CGF, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1388 | const ObjCInterfaceDecl *ID); |
| 1389 | |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1390 | /// EmitMetaClassRef - Return a Value * of the address of _class_t |
| 1391 | /// meta-data |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1392 | llvm::Value *EmitMetaClassRef(CodeGenFunction &CGF, |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1393 | const ObjCInterfaceDecl *ID); |
| 1394 | |
Fariborz Jahanian | 4e7ae06 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 1395 | /// ObjCIvarOffsetVariable - Returns the ivar offset variable for |
| 1396 | /// the given ivar. |
| 1397 | /// |
Daniel Dunbar | a106052 | 2009-04-19 00:31:15 +0000 | [diff] [blame] | 1398 | llvm::GlobalVariable * ObjCIvarOffsetVariable( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1399 | const ObjCInterfaceDecl *ID, |
| 1400 | const ObjCIvarDecl *Ivar); |
| 1401 | |
Fariborz Jahanian | 74b7722 | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 1402 | /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, |
| 1403 | /// for the given selector. |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1404 | llvm::Value *EmitSelector(CodeGenFunction &CGF, Selector Sel, |
Fariborz Jahanian | 9240f3d | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 1405 | bool lval=false); |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1406 | |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1407 | /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1408 | /// interface. The return value has type EHTypePtrTy. |
John McCall | 2ca705e | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 1409 | llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID, |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1410 | bool ForDefinition); |
Daniel Dunbar | 15894b7 | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 1411 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1412 | const char *getMetaclassSymbolPrefix() const { |
Daniel Dunbar | 15894b7 | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 1413 | return "OBJC_METACLASS_$_"; |
| 1414 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1415 | |
Daniel Dunbar | 15894b7 | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 1416 | const char *getClassSymbolPrefix() const { |
| 1417 | return "OBJC_CLASS_$_"; |
| 1418 | } |
| 1419 | |
Daniel Dunbar | 96120237 | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 1420 | void GetClassSizeInfo(const ObjCImplementationDecl *OID, |
Daniel Dunbar | 554fd79 | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 1421 | uint32_t &InstanceStart, |
| 1422 | uint32_t &InstanceSize); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1423 | |
Fariborz Jahanian | e412864 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1424 | // Shamelessly stolen from Analysis/CFRefCount.cpp |
Daniel Dunbar | 9a017d7 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1425 | Selector GetNullarySelector(const char* name) const { |
Fariborz Jahanian | e412864 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1426 | IdentifierInfo* II = &CGM.getContext().Idents.get(name); |
| 1427 | return CGM.getContext().Selectors.getSelector(0, &II); |
| 1428 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1429 | |
Daniel Dunbar | 9a017d7 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1430 | Selector GetUnarySelector(const char* name) const { |
Fariborz Jahanian | e412864 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1431 | IdentifierInfo* II = &CGM.getContext().Idents.get(name); |
| 1432 | return CGM.getContext().Selectors.getSelector(1, &II); |
| 1433 | } |
Daniel Dunbar | 554fd79 | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 1434 | |
Daniel Dunbar | 9a017d7 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1435 | /// ImplementationIsNonLazy - Check whether the given category or |
| 1436 | /// class implementation is "non-lazy". |
Fariborz Jahanian | a6bed83 | 2009-05-21 01:03:45 +0000 | [diff] [blame] | 1437 | bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const; |
Daniel Dunbar | 9a017d7 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1438 | |
Saleem Abdulrasool | 5f25bc3 | 2013-02-17 04:03:34 +0000 | [diff] [blame] | 1439 | bool IsIvarOffsetKnownIdempotent(const CodeGen::CodeGenFunction &CGF, |
Saleem Abdulrasool | 5f25bc3 | 2013-02-17 04:03:34 +0000 | [diff] [blame] | 1440 | const ObjCIvarDecl *IV) { |
Fariborz Jahanian | dafffbe | 2014-03-04 18:34:52 +0000 | [diff] [blame] | 1441 | // Annotate the load as an invariant load iff inside an instance method |
| 1442 | // and ivar belongs to instance method's class and one of its super class. |
| 1443 | // This check is needed because the ivar offset is a lazily |
Saleem Abdulrasool | 5f25bc3 | 2013-02-17 04:03:34 +0000 | [diff] [blame] | 1444 | // initialised value that may depend on objc_msgSend to perform a fixup on |
| 1445 | // the first message dispatch. |
| 1446 | // |
| 1447 | // An additional opportunity to mark the load as invariant arises when the |
| 1448 | // base of the ivar access is a parameter to an Objective C method. |
| 1449 | // However, because the parameters are not available in the current |
| 1450 | // interface, we cannot perform this check. |
Fariborz Jahanian | dafffbe | 2014-03-04 18:34:52 +0000 | [diff] [blame] | 1451 | if (const ObjCMethodDecl *MD = |
| 1452 | dyn_cast_or_null<ObjCMethodDecl>(CGF.CurFuncDecl)) |
Fariborz Jahanian | 7a58302 | 2014-03-04 22:57:32 +0000 | [diff] [blame] | 1453 | if (MD->isInstanceMethod()) |
Fariborz Jahanian | dafffbe | 2014-03-04 18:34:52 +0000 | [diff] [blame] | 1454 | if (const ObjCInterfaceDecl *ID = MD->getClassInterface()) |
| 1455 | return IV->getContainingInterface()->isSuperClassOf(ID); |
Saleem Abdulrasool | 5f25bc3 | 2013-02-17 04:03:34 +0000 | [diff] [blame] | 1456 | return false; |
| 1457 | } |
| 1458 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1459 | public: |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1460 | CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm); |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1461 | // FIXME. All stubs for now! |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1462 | llvm::Function *ModuleInitFunction() override; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1463 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1464 | CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
| 1465 | ReturnValueSlot Return, |
| 1466 | QualType ResultType, Selector Sel, |
| 1467 | llvm::Value *Receiver, |
| 1468 | const CallArgList &CallArgs, |
| 1469 | const ObjCInterfaceDecl *Class, |
| 1470 | const ObjCMethodDecl *Method) override; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1471 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1472 | CodeGen::RValue |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1473 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1474 | ReturnValueSlot Return, QualType ResultType, |
| 1475 | Selector Sel, const ObjCInterfaceDecl *Class, |
| 1476 | bool isCategoryImpl, llvm::Value *Receiver, |
| 1477 | bool IsClassMessage, const CallArgList &CallArgs, |
| 1478 | const ObjCMethodDecl *Method) override; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1479 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1480 | llvm::Value *GetClass(CodeGenFunction &CGF, |
| 1481 | const ObjCInterfaceDecl *ID) override; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1482 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1483 | llvm::Value *GetSelector(CodeGenFunction &CGF, Selector Sel, |
| 1484 | bool lvalue = false) override |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1485 | { return EmitSelector(CGF, Sel, lvalue); } |
Fariborz Jahanian | f3648b8 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1486 | |
| 1487 | /// The NeXT/Apple runtimes do not support typed selectors; just emit an |
| 1488 | /// untyped one. |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1489 | llvm::Value *GetSelector(CodeGenFunction &CGF, |
| 1490 | const ObjCMethodDecl *Method) override |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1491 | { return EmitSelector(CGF, Method->getSelector()); } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1492 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1493 | void GenerateCategory(const ObjCCategoryImplDecl *CMD) override; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1494 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1495 | void GenerateClass(const ObjCImplementationDecl *ClassDecl) override; |
David Chisnall | 92d436b | 2012-01-31 18:59:20 +0000 | [diff] [blame] | 1496 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1497 | void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) override {} |
David Chisnall | 92d436b | 2012-01-31 18:59:20 +0000 | [diff] [blame] | 1498 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1499 | llvm::Value *GenerateProtocolRef(CodeGenFunction &CGF, |
| 1500 | const ObjCProtocolDecl *PD) override; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1501 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1502 | llvm::Constant *GetEHType(QualType T) override; |
John McCall | 2ca705e | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 1503 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1504 | llvm::Constant *GetPropertyGetFunction() override { |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 1505 | return ObjCTypes.getGetPropertyFn(); |
Fariborz Jahanian | 21fc74c | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1506 | } |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1507 | llvm::Constant *GetPropertySetFunction() override { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1508 | return ObjCTypes.getSetPropertyFn(); |
Fariborz Jahanian | 21fc74c | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1509 | } |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1510 | |
| 1511 | llvm::Constant *GetOptimizedPropertySetFunction(bool atomic, |
| 1512 | bool copy) override { |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 1513 | return ObjCTypes.getOptimizedSetPropertyFn(atomic, copy); |
| 1514 | } |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1515 | |
| 1516 | llvm::Constant *GetSetStructFunction() override { |
David Chisnall | 168b80f | 2010-12-26 22:13:16 +0000 | [diff] [blame] | 1517 | return ObjCTypes.getCopyStructFn(); |
| 1518 | } |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1519 | llvm::Constant *GetGetStructFunction() override { |
Fariborz Jahanian | 5a8c203 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 1520 | return ObjCTypes.getCopyStructFn(); |
| 1521 | } |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1522 | llvm::Constant *GetCppAtomicObjectSetFunction() override { |
David Chisnall | 0d75e06 | 2012-12-17 18:54:24 +0000 | [diff] [blame] | 1523 | return ObjCTypes.getCppAtomicObjectFunction(); |
| 1524 | } |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1525 | llvm::Constant *GetCppAtomicObjectGetFunction() override { |
Fariborz Jahanian | 1e1b549 | 2012-01-06 18:07:23 +0000 | [diff] [blame] | 1526 | return ObjCTypes.getCppAtomicObjectFunction(); |
| 1527 | } |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1528 | |
| 1529 | llvm::Constant *EnumerationMutationFunction() override { |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 1530 | return ObjCTypes.getEnumerationMutationFn(); |
Daniel Dunbar | d73ea816 | 2009-02-16 18:48:45 +0000 | [diff] [blame] | 1531 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1532 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1533 | void EmitTryStmt(CodeGen::CodeGenFunction &CGF, |
| 1534 | const ObjCAtTryStmt &S) override; |
| 1535 | void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 1536 | const ObjCAtSynchronizedStmt &S) override; |
| 1537 | void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, const ObjCAtThrowStmt &S, |
| 1538 | bool ClearInsertionPoint=true) override; |
| 1539 | llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
| 1540 | llvm::Value *AddrWeakObj) override; |
| 1541 | void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
| 1542 | llvm::Value *src, llvm::Value *dst) override; |
| 1543 | void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
| 1544 | llvm::Value *src, llvm::Value *dest, |
| 1545 | bool threadlocal = false) override; |
| 1546 | void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
| 1547 | llvm::Value *src, llvm::Value *dest, |
| 1548 | llvm::Value *ivarOffset) override; |
| 1549 | void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
| 1550 | llvm::Value *src, llvm::Value *dest) override; |
| 1551 | void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF, |
| 1552 | llvm::Value *dest, llvm::Value *src, |
| 1553 | llvm::Value *size) override; |
| 1554 | LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, QualType ObjectTy, |
| 1555 | llvm::Value *BaseValue, const ObjCIvarDecl *Ivar, |
| 1556 | unsigned CVRQualifiers) override; |
| 1557 | llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
| 1558 | const ObjCInterfaceDecl *Interface, |
| 1559 | const ObjCIvarDecl *Ivar) override; |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1560 | }; |
Fariborz Jahanian | 715fdd5 | 2012-01-29 20:27:13 +0000 | [diff] [blame] | 1561 | |
| 1562 | /// A helper class for performing the null-initialization of a return |
| 1563 | /// value. |
| 1564 | struct NullReturnState { |
| 1565 | llvm::BasicBlock *NullBB; |
John McCall | 3d1e2c9 | 2013-02-12 05:53:35 +0000 | [diff] [blame] | 1566 | NullReturnState() : NullBB(0) {} |
Fariborz Jahanian | 715fdd5 | 2012-01-29 20:27:13 +0000 | [diff] [blame] | 1567 | |
John McCall | 3d1e2c9 | 2013-02-12 05:53:35 +0000 | [diff] [blame] | 1568 | /// Perform a null-check of the given receiver. |
Fariborz Jahanian | 715fdd5 | 2012-01-29 20:27:13 +0000 | [diff] [blame] | 1569 | void init(CodeGenFunction &CGF, llvm::Value *receiver) { |
John McCall | 3d1e2c9 | 2013-02-12 05:53:35 +0000 | [diff] [blame] | 1570 | // Make blocks for the null-receiver and call edges. |
| 1571 | NullBB = CGF.createBasicBlock("msgSend.null-receiver"); |
| 1572 | llvm::BasicBlock *callBB = CGF.createBasicBlock("msgSend.call"); |
Fariborz Jahanian | 715fdd5 | 2012-01-29 20:27:13 +0000 | [diff] [blame] | 1573 | |
| 1574 | // 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] | 1575 | // null-receiver block. There's no point in trying to avoid it: |
| 1576 | // we're always going to put *something* there, because otherwise |
| 1577 | // we shouldn't have done this null-check in the first place. |
Fariborz Jahanian | 715fdd5 | 2012-01-29 20:27:13 +0000 | [diff] [blame] | 1578 | llvm::Value *isNull = CGF.Builder.CreateIsNull(receiver); |
| 1579 | CGF.Builder.CreateCondBr(isNull, NullBB, callBB); |
| 1580 | |
| 1581 | // Otherwise, start performing the call. |
| 1582 | CGF.EmitBlock(callBB); |
| 1583 | } |
| 1584 | |
John McCall | 3d1e2c9 | 2013-02-12 05:53:35 +0000 | [diff] [blame] | 1585 | /// Complete the null-return operation. It is valid to call this |
| 1586 | /// regardless of whether 'init' has been called. |
Fariborz Jahanian | f2bda69 | 2012-01-30 21:40:37 +0000 | [diff] [blame] | 1587 | RValue complete(CodeGenFunction &CGF, RValue result, QualType resultType, |
| 1588 | const CallArgList &CallArgs, |
| 1589 | const ObjCMethodDecl *Method) { |
John McCall | 3d1e2c9 | 2013-02-12 05:53:35 +0000 | [diff] [blame] | 1590 | // 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] | 1591 | if (!NullBB) return result; |
John McCall | 3d1e2c9 | 2013-02-12 05:53:35 +0000 | [diff] [blame] | 1592 | |
| 1593 | // The continuation block. This will be left null if we don't have an |
| 1594 | // IP, which can happen if the method we're calling is marked noreturn. |
| 1595 | llvm::BasicBlock *contBB = 0; |
Fariborz Jahanian | f2bda69 | 2012-01-30 21:40:37 +0000 | [diff] [blame] | 1596 | |
John McCall | 3d1e2c9 | 2013-02-12 05:53:35 +0000 | [diff] [blame] | 1597 | // Finish the call path. |
| 1598 | llvm::BasicBlock *callBB = CGF.Builder.GetInsertBlock(); |
| 1599 | if (callBB) { |
| 1600 | contBB = CGF.createBasicBlock("msgSend.cont"); |
| 1601 | CGF.Builder.CreateBr(contBB); |
Fariborz Jahanian | f2bda69 | 2012-01-30 21:40:37 +0000 | [diff] [blame] | 1602 | } |
Fariborz Jahanian | 715fdd5 | 2012-01-29 20:27:13 +0000 | [diff] [blame] | 1603 | |
John McCall | 3d1e2c9 | 2013-02-12 05:53:35 +0000 | [diff] [blame] | 1604 | // Okay, start emitting the null-receiver block. |
Fariborz Jahanian | 715fdd5 | 2012-01-29 20:27:13 +0000 | [diff] [blame] | 1605 | CGF.EmitBlock(NullBB); |
Fariborz Jahanian | f2bda69 | 2012-01-30 21:40:37 +0000 | [diff] [blame] | 1606 | |
John McCall | 3d1e2c9 | 2013-02-12 05:53:35 +0000 | [diff] [blame] | 1607 | // Release any consumed arguments we've got. |
Fariborz Jahanian | f2bda69 | 2012-01-30 21:40:37 +0000 | [diff] [blame] | 1608 | if (Method) { |
| 1609 | CallArgList::const_iterator I = CallArgs.begin(); |
| 1610 | for (ObjCMethodDecl::param_const_iterator i = Method->param_begin(), |
| 1611 | e = Method->param_end(); i != e; ++i, ++I) { |
| 1612 | const ParmVarDecl *ParamDecl = (*i); |
| 1613 | if (ParamDecl->hasAttr<NSConsumedAttr>()) { |
| 1614 | RValue RV = I->RV; |
| 1615 | assert(RV.isScalar() && |
| 1616 | "NullReturnState::complete - arg not on object"); |
John McCall | cdda29c | 2013-03-13 03:10:54 +0000 | [diff] [blame] | 1617 | CGF.EmitARCRelease(RV.getScalarVal(), ARCImpreciseLifetime); |
Fariborz Jahanian | f2bda69 | 2012-01-30 21:40:37 +0000 | [diff] [blame] | 1618 | } |
| 1619 | } |
| 1620 | } |
John McCall | 3d1e2c9 | 2013-02-12 05:53:35 +0000 | [diff] [blame] | 1621 | |
| 1622 | // The phi code below assumes that we haven't needed any control flow yet. |
| 1623 | assert(CGF.Builder.GetInsertBlock() == NullBB); |
| 1624 | |
| 1625 | // If we've got a void return, just jump to the continuation block. |
| 1626 | if (result.isScalar() && resultType->isVoidType()) { |
| 1627 | // No jumps required if the message-send was noreturn. |
| 1628 | if (contBB) CGF.EmitBlock(contBB); |
Fariborz Jahanian | 715fdd5 | 2012-01-29 20:27:13 +0000 | [diff] [blame] | 1629 | return result; |
| 1630 | } |
| 1631 | |
John McCall | 3d1e2c9 | 2013-02-12 05:53:35 +0000 | [diff] [blame] | 1632 | // If we've got a scalar return, build a phi. |
| 1633 | if (result.isScalar()) { |
| 1634 | // Derive the null-initialization value. |
| 1635 | llvm::Constant *null = CGF.CGM.EmitNullConstant(resultType); |
| 1636 | |
| 1637 | // If no join is necessary, just flow out. |
| 1638 | if (!contBB) return RValue::get(null); |
| 1639 | |
| 1640 | // Otherwise, build a phi. |
| 1641 | CGF.EmitBlock(contBB); |
| 1642 | llvm::PHINode *phi = CGF.Builder.CreatePHI(null->getType(), 2); |
| 1643 | phi->addIncoming(result.getScalarVal(), callBB); |
| 1644 | phi->addIncoming(null, NullBB); |
| 1645 | return RValue::get(phi); |
| 1646 | } |
| 1647 | |
| 1648 | // If we've got an aggregate return, null the buffer out. |
| 1649 | // FIXME: maybe we should be doing things differently for all the |
| 1650 | // cases where the ABI has us returning (1) non-agg values in |
| 1651 | // memory or (2) agg values in registers. |
| 1652 | if (result.isAggregate()) { |
| 1653 | assert(result.isAggregate() && "null init of non-aggregate result?"); |
| 1654 | CGF.EmitNullInitialization(result.getAggregateAddr(), resultType); |
| 1655 | if (contBB) CGF.EmitBlock(contBB); |
| 1656 | return result; |
| 1657 | } |
| 1658 | |
| 1659 | // Complex types. |
Fariborz Jahanian | 715fdd5 | 2012-01-29 20:27:13 +0000 | [diff] [blame] | 1660 | CGF.EmitBlock(contBB); |
John McCall | 3d1e2c9 | 2013-02-12 05:53:35 +0000 | [diff] [blame] | 1661 | CodeGenFunction::ComplexPairTy callResult = result.getComplexVal(); |
| 1662 | |
| 1663 | // Find the scalar type and its zero value. |
| 1664 | llvm::Type *scalarTy = callResult.first->getType(); |
| 1665 | llvm::Constant *scalarZero = llvm::Constant::getNullValue(scalarTy); |
| 1666 | |
| 1667 | // Build phis for both coordinates. |
| 1668 | llvm::PHINode *real = CGF.Builder.CreatePHI(scalarTy, 2); |
| 1669 | real->addIncoming(callResult.first, callBB); |
| 1670 | real->addIncoming(scalarZero, NullBB); |
| 1671 | llvm::PHINode *imag = CGF.Builder.CreatePHI(scalarTy, 2); |
| 1672 | imag->addIncoming(callResult.second, callBB); |
| 1673 | imag->addIncoming(scalarZero, NullBB); |
| 1674 | return RValue::getComplex(real, imag); |
Fariborz Jahanian | 715fdd5 | 2012-01-29 20:27:13 +0000 | [diff] [blame] | 1675 | } |
| 1676 | }; |
| 1677 | |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1678 | } // end anonymous namespace |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1679 | |
| 1680 | /* *** Helper Functions *** */ |
| 1681 | |
| 1682 | /// getConstantGEP() - Help routine to construct simple GEPs. |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1683 | static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1684 | llvm::Constant *C, |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1685 | unsigned idx0, |
| 1686 | unsigned idx1) { |
| 1687 | llvm::Value *Idxs[] = { |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1688 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0), |
| 1689 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1) |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1690 | }; |
Jay Foad | ed8db7d | 2011-07-21 14:31:17 +0000 | [diff] [blame] | 1691 | return llvm::ConstantExpr::getGetElementPtr(C, Idxs); |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1692 | } |
| 1693 | |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1694 | /// hasObjCExceptionAttribute - Return true if this class or any super |
| 1695 | /// class has the __objc_exception__ attribute. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1696 | static bool hasObjCExceptionAttribute(ASTContext &Context, |
Douglas Gregor | 78bd61f | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 1697 | const ObjCInterfaceDecl *OID) { |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1698 | if (OID->hasAttr<ObjCExceptionAttr>()) |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1699 | return true; |
| 1700 | if (const ObjCInterfaceDecl *Super = OID->getSuperClass()) |
Douglas Gregor | 78bd61f | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 1701 | return hasObjCExceptionAttribute(Context, Super); |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1702 | return false; |
| 1703 | } |
| 1704 | |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1705 | /* *** CGObjCMac Public Interface *** */ |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1706 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1707 | CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1708 | ObjCTypes(cgm) { |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1709 | ObjCABI = 1; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1710 | EmitImageInfo(); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1711 | } |
| 1712 | |
Daniel Dunbar | 7c6d3a7 | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 1713 | /// GetClass - Return a reference to the class for the given interface |
| 1714 | /// decl. |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1715 | llvm::Value *CGObjCMac::GetClass(CodeGenFunction &CGF, |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1716 | const ObjCInterfaceDecl *ID) { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1717 | return EmitClassRef(CGF, ID); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1718 | } |
| 1719 | |
| 1720 | /// GetSelector - Return the pointer to the unique'd string for this selector. |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1721 | llvm::Value *CGObjCMac::GetSelector(CodeGenFunction &CGF, Selector Sel, |
Fariborz Jahanian | 9240f3d | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 1722 | bool lval) { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1723 | return EmitSelector(CGF, Sel, lval); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1724 | } |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1725 | llvm::Value *CGObjCMac::GetSelector(CodeGenFunction &CGF, const ObjCMethodDecl |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1726 | *Method) { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1727 | return EmitSelector(CGF, Method->getSelector()); |
Fariborz Jahanian | f3648b8 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1728 | } |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1729 | |
Fariborz Jahanian | 831f0fc | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 1730 | llvm::Constant *CGObjCMac::GetEHType(QualType T) { |
Fariborz Jahanian | 0a3cfcc | 2011-06-22 20:21:51 +0000 | [diff] [blame] | 1731 | if (T->isObjCIdType() || |
| 1732 | T->isObjCQualifiedIdType()) { |
| 1733 | return CGM.GetAddrOfRTTIDescriptor( |
Douglas Gregor | 9767347 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 1734 | CGM.getContext().getObjCIdRedefinitionType(), /*ForEH=*/true); |
Fariborz Jahanian | 0a3cfcc | 2011-06-22 20:21:51 +0000 | [diff] [blame] | 1735 | } |
Fariborz Jahanian | 831f0fc | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 1736 | if (T->isObjCClassType() || |
| 1737 | T->isObjCQualifiedClassType()) { |
| 1738 | return CGM.GetAddrOfRTTIDescriptor( |
Douglas Gregor | 9767347 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 1739 | CGM.getContext().getObjCClassRedefinitionType(), /*ForEH=*/true); |
Fariborz Jahanian | 831f0fc | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 1740 | } |
| 1741 | if (T->isObjCObjectPointerType()) |
| 1742 | return CGM.GetAddrOfRTTIDescriptor(T, /*ForEH=*/true); |
| 1743 | |
John McCall | 2ca705e | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 1744 | llvm_unreachable("asking for catch type for ObjC type in fragile runtime"); |
John McCall | 2ca705e | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 1745 | } |
| 1746 | |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1747 | /// Generate a constant CFString object. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1748 | /* |
| 1749 | struct __builtin_CFString { |
| 1750 | const int *isa; // point to __CFConstantStringClassReference |
| 1751 | int flags; |
| 1752 | const char *str; |
| 1753 | long length; |
| 1754 | }; |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1755 | */ |
| 1756 | |
Fariborz Jahanian | 63408e8 | 2010-04-22 20:26:39 +0000 | [diff] [blame] | 1757 | /// or Generate a constant NSString object. |
| 1758 | /* |
| 1759 | struct __builtin_NSString { |
| 1760 | const int *isa; // point to __NSConstantStringClassReference |
| 1761 | const char *str; |
| 1762 | unsigned int length; |
| 1763 | }; |
| 1764 | */ |
| 1765 | |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1766 | llvm::Constant *CGObjCCommonMac::GenerateConstantString( |
David Chisnall | 481e3a8 | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 1767 | const StringLiteral *SL) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1768 | return (CGM.getLangOpts().NoConstantCFStrings == 0 ? |
Fariborz Jahanian | 63408e8 | 2010-04-22 20:26:39 +0000 | [diff] [blame] | 1769 | CGM.GetAddrOfConstantCFString(SL) : |
Fariborz Jahanian | 50c925f | 2010-10-19 17:19:29 +0000 | [diff] [blame] | 1770 | CGM.GetAddrOfConstantString(SL)); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1771 | } |
| 1772 | |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 1773 | enum { |
| 1774 | kCFTaggedObjectID_Integer = (1 << 1) + 1 |
| 1775 | }; |
| 1776 | |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1777 | /// Generates a message send where the super is the receiver. This is |
| 1778 | /// a message send to self with special delivery semantics indicating |
| 1779 | /// which class's method should be called. |
Daniel Dunbar | 97db84c | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1780 | CodeGen::RValue |
| 1781 | CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
John McCall | 78a1511 | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1782 | ReturnValueSlot Return, |
Daniel Dunbar | 4b8c6db | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1783 | QualType ResultType, |
| 1784 | Selector Sel, |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1785 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | bac73ac | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1786 | bool isCategoryImpl, |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1787 | llvm::Value *Receiver, |
Daniel Dunbar | c722b85 | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1788 | bool IsClassMessage, |
Daniel Dunbar | aff9fca | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 1789 | const CodeGen::CallArgList &CallArgs, |
| 1790 | const ObjCMethodDecl *Method) { |
Daniel Dunbar | f6397fe | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1791 | // Create and init a super structure; this is a (receiver, class) |
| 1792 | // pair we will pass to objc_msgSendSuper. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1793 | llvm::Value *ObjCSuper = |
John McCall | 6647584 | 2011-03-04 08:00:29 +0000 | [diff] [blame] | 1794 | CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1795 | llvm::Value *ReceiverAsObject = |
Daniel Dunbar | f6397fe | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1796 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1797 | CGF.Builder.CreateStore(ReceiverAsObject, |
Daniel Dunbar | f6397fe | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1798 | CGF.Builder.CreateStructGEP(ObjCSuper, 0)); |
Daniel Dunbar | f6397fe | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1799 | |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1800 | // If this is a class message the metaclass is passed as the target. |
| 1801 | llvm::Value *Target; |
| 1802 | if (IsClassMessage) { |
Fariborz Jahanian | bac73ac | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1803 | if (isCategoryImpl) { |
| 1804 | // Message sent to 'super' in a class method defined in a category |
| 1805 | // implementation requires an odd treatment. |
| 1806 | // If we are in a class method, we must retrieve the |
| 1807 | // _metaclass_ for the current class, pointed at by |
| 1808 | // the class's "isa" pointer. The following assumes that |
| 1809 | // isa" is the first ivar in a class (which it must be). |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1810 | Target = EmitClassRef(CGF, Class->getSuperClass()); |
Fariborz Jahanian | bac73ac | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1811 | Target = CGF.Builder.CreateStructGEP(Target, 0); |
| 1812 | Target = CGF.Builder.CreateLoad(Target); |
Mike Stump | 658fe02 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 1813 | } else { |
Fariborz Jahanian | bac73ac | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1814 | llvm::Value *MetaClassPtr = EmitMetaClassRef(Class); |
| 1815 | llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1); |
| 1816 | llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr); |
| 1817 | Target = Super; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1818 | } |
Fariborz Jahanian | da2efb0 | 2009-11-14 02:18:31 +0000 | [diff] [blame] | 1819 | } |
| 1820 | else if (isCategoryImpl) |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1821 | Target = EmitClassRef(CGF, Class->getSuperClass()); |
Fariborz Jahanian | da2efb0 | 2009-11-14 02:18:31 +0000 | [diff] [blame] | 1822 | else { |
Fariborz Jahanian | eb80c98 | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 1823 | llvm::Value *ClassPtr = EmitSuperClassRef(Class); |
| 1824 | ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1); |
| 1825 | Target = CGF.Builder.CreateLoad(ClassPtr); |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1826 | } |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1827 | // FIXME: We shouldn't need to do this cast, rectify the ASTContext and |
| 1828 | // ObjCTypes types. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1829 | llvm::Type *ClassTy = |
Daniel Dunbar | c722b85 | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1830 | CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1831 | Target = CGF.Builder.CreateBitCast(Target, ClassTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1832 | CGF.Builder.CreateStore(Target, |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1833 | CGF.Builder.CreateStructGEP(ObjCSuper, 1)); |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1834 | return EmitMessageSend(CGF, Return, ResultType, |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1835 | EmitSelector(CGF, Sel), |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1836 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
| 1837 | true, CallArgs, Method, ObjCTypes); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1838 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1839 | |
| 1840 | /// Generate code for a message send expression. |
Daniel Dunbar | 97db84c | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1841 | CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
John McCall | 78a1511 | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1842 | ReturnValueSlot Return, |
Daniel Dunbar | 4b8c6db | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1843 | QualType ResultType, |
| 1844 | Selector Sel, |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1845 | llvm::Value *Receiver, |
Fariborz Jahanian | f3648b8 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1846 | const CallArgList &CallArgs, |
David Chisnall | 01aa467 | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 1847 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | f3648b8 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1848 | const ObjCMethodDecl *Method) { |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1849 | return EmitMessageSend(CGF, Return, ResultType, |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1850 | EmitSelector(CGF, Sel), |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1851 | Receiver, CGF.getContext().getObjCIdType(), |
| 1852 | false, CallArgs, Method, ObjCTypes); |
Daniel Dunbar | 97ff50d | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1853 | } |
| 1854 | |
Daniel Dunbar | aff9fca | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 1855 | CodeGen::RValue |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1856 | CGObjCCommonMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
| 1857 | ReturnValueSlot Return, |
| 1858 | QualType ResultType, |
| 1859 | llvm::Value *Sel, |
| 1860 | llvm::Value *Arg0, |
| 1861 | QualType Arg0Ty, |
| 1862 | bool IsSuper, |
| 1863 | const CallArgList &CallArgs, |
| 1864 | const ObjCMethodDecl *Method, |
| 1865 | const ObjCCommonTypesHelper &ObjCTypes) { |
Daniel Dunbar | c722b85 | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1866 | CallArgList ActualArgs; |
Fariborz Jahanian | 969bc68 | 2009-04-24 21:07:43 +0000 | [diff] [blame] | 1867 | if (!IsSuper) |
Benjamin Kramer | 76399eb | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 1868 | Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy); |
Eli Friedman | 43dca6a | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 1869 | ActualArgs.add(RValue::get(Arg0), Arg0Ty); |
| 1870 | ActualArgs.add(RValue::get(Sel), CGF.getContext().getObjCSelType()); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1871 | ActualArgs.addFrom(CallArgs); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1872 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1873 | // If we're calling a method, use the formal signature. |
| 1874 | MessageSendInfo MSI = getMessageSendInfo(Method, ResultType, ActualArgs); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1875 | |
Anders Carlsson | 280e61f1 | 2010-06-21 20:59:55 +0000 | [diff] [blame] | 1876 | if (Method) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1877 | assert(CGM.getContext().getCanonicalType(Method->getReturnType()) == |
| 1878 | CGM.getContext().getCanonicalType(ResultType) && |
Anders Carlsson | 280e61f1 | 2010-06-21 20:59:55 +0000 | [diff] [blame] | 1879 | "Result type mismatch!"); |
| 1880 | |
John McCall | 5880fb8 | 2011-05-14 21:12:11 +0000 | [diff] [blame] | 1881 | NullReturnState nullReturn; |
| 1882 | |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1883 | llvm::Constant *Fn = NULL; |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1884 | if (CGM.ReturnTypeUsesSRet(MSI.CallInfo)) { |
Fariborz Jahanian | 715fdd5 | 2012-01-29 20:27:13 +0000 | [diff] [blame] | 1885 | if (!IsSuper) nullReturn.init(CGF, Arg0); |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1886 | Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1887 | : ObjCTypes.getSendStretFn(IsSuper); |
Daniel Dunbar | 6f2e839 | 2010-07-14 23:39:36 +0000 | [diff] [blame] | 1888 | } else if (CGM.ReturnTypeUsesFPRet(ResultType)) { |
| 1889 | Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper) |
| 1890 | : ObjCTypes.getSendFpretFn(IsSuper); |
Anders Carlsson | 2f1a6c3 | 2011-10-31 16:27:11 +0000 | [diff] [blame] | 1891 | } else if (CGM.ReturnTypeUsesFP2Ret(ResultType)) { |
| 1892 | Fn = (ObjCABI == 2) ? ObjCTypes.getSendFp2RetFn2(IsSuper) |
| 1893 | : ObjCTypes.getSendFp2retFn(IsSuper); |
Daniel Dunbar | 3c683f5b | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 1894 | } else { |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1895 | Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1896 | : ObjCTypes.getSendFn(IsSuper); |
Daniel Dunbar | 3c683f5b | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 1897 | } |
Fariborz Jahanian | f2bda69 | 2012-01-30 21:40:37 +0000 | [diff] [blame] | 1898 | |
| 1899 | bool requiresnullCheck = false; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1900 | if (CGM.getLangOpts().ObjCAutoRefCount && Method) |
Aaron Ballman | 43b68be | 2014-03-07 17:50:17 +0000 | [diff] [blame] | 1901 | for (const auto *ParamDecl : Method->params()) { |
Fariborz Jahanian | f2bda69 | 2012-01-30 21:40:37 +0000 | [diff] [blame] | 1902 | if (ParamDecl->hasAttr<NSConsumedAttr>()) { |
| 1903 | if (!nullReturn.NullBB) |
| 1904 | nullReturn.init(CGF, Arg0); |
| 1905 | requiresnullCheck = true; |
| 1906 | break; |
| 1907 | } |
| 1908 | } |
| 1909 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1910 | Fn = llvm::ConstantExpr::getBitCast(Fn, MSI.MessengerType); |
| 1911 | RValue rvalue = CGF.EmitCall(MSI.CallInfo, Fn, Return, ActualArgs); |
Fariborz Jahanian | f2bda69 | 2012-01-30 21:40:37 +0000 | [diff] [blame] | 1912 | return nullReturn.complete(CGF, rvalue, ResultType, CallArgs, |
| 1913 | requiresnullCheck ? Method : 0); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1914 | } |
| 1915 | |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1916 | static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) { |
| 1917 | if (FQT.isObjCGCStrong()) |
| 1918 | return Qualifiers::Strong; |
| 1919 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1920 | if (FQT.isObjCGCWeak() || FQT.getObjCLifetime() == Qualifiers::OCL_Weak) |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1921 | return Qualifiers::Weak; |
| 1922 | |
Fariborz Jahanian | 430b35e | 2012-02-16 00:15:02 +0000 | [diff] [blame] | 1923 | // check for __unsafe_unretained |
| 1924 | if (FQT.getObjCLifetime() == Qualifiers::OCL_ExplicitNone) |
| 1925 | return Qualifiers::GCNone; |
| 1926 | |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1927 | if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType()) |
| 1928 | return Qualifiers::Strong; |
| 1929 | |
| 1930 | if (const PointerType *PT = FQT->getAs<PointerType>()) |
| 1931 | return GetGCAttrTypeForType(Ctx, PT->getPointeeType()); |
| 1932 | |
| 1933 | return Qualifiers::GCNone; |
| 1934 | } |
| 1935 | |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1936 | llvm::Constant *CGObjCCommonMac::BuildGCBlockLayout(CodeGenModule &CGM, |
| 1937 | const CGBlockInfo &blockInfo) { |
Fariborz Jahanian | 0c58ce9 | 2012-10-27 21:10:38 +0000 | [diff] [blame] | 1938 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1939 | llvm::Constant *nullPtr = llvm::Constant::getNullValue(CGM.Int8PtrTy); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1940 | if (CGM.getLangOpts().getGC() == LangOptions::NonGC && |
| 1941 | !CGM.getLangOpts().ObjCAutoRefCount) |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1942 | return nullPtr; |
| 1943 | |
Fariborz Jahanian | f95e358 | 2010-08-06 16:28:55 +0000 | [diff] [blame] | 1944 | bool hasUnion = false; |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1945 | SkipIvars.clear(); |
| 1946 | IvarsInfo.clear(); |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1947 | unsigned WordSizeInBits = CGM.getTarget().getPointerWidth(0); |
| 1948 | unsigned ByteSizeInBits = CGM.getTarget().getCharWidth(); |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1949 | |
Fariborz Jahanian | cfddabf | 2010-09-09 00:21:45 +0000 | [diff] [blame] | 1950 | // __isa is the first field in block descriptor and must assume by runtime's |
| 1951 | // convention that it is GC'able. |
Eli Friedman | 8cbca20 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 1952 | IvarsInfo.push_back(GC_IVAR(0, 1)); |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1953 | |
| 1954 | const BlockDecl *blockDecl = blockInfo.getBlockDecl(); |
| 1955 | |
| 1956 | // Calculate the basic layout of the block structure. |
| 1957 | const llvm::StructLayout *layout = |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 1958 | CGM.getDataLayout().getStructLayout(blockInfo.StructureType); |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1959 | |
| 1960 | // Ignore the optional 'this' capture: C++ objects are not assumed |
| 1961 | // to be GC'ed. |
| 1962 | |
| 1963 | // Walk the captured variables. |
| 1964 | for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(), |
| 1965 | ce = blockDecl->capture_end(); ci != ce; ++ci) { |
| 1966 | const VarDecl *variable = ci->getVariable(); |
| 1967 | QualType type = variable->getType(); |
| 1968 | |
| 1969 | const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable); |
| 1970 | |
| 1971 | // Ignore constant captures. |
| 1972 | if (capture.isConstant()) continue; |
| 1973 | |
Eli Friedman | 8cbca20 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 1974 | uint64_t fieldOffset = layout->getElementOffset(capture.getIndex()); |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1975 | |
| 1976 | // __block variables are passed by their descriptor address. |
| 1977 | if (ci->isByRef()) { |
Eli Friedman | 8cbca20 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 1978 | IvarsInfo.push_back(GC_IVAR(fieldOffset, /*size in words*/ 1)); |
Fariborz Jahanian | 933c672 | 2010-09-11 01:27:29 +0000 | [diff] [blame] | 1979 | continue; |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1980 | } |
| 1981 | |
| 1982 | assert(!type->isArrayType() && "array variable should not be caught"); |
| 1983 | if (const RecordType *record = type->getAs<RecordType>()) { |
| 1984 | BuildAggrIvarRecordLayout(record, fieldOffset, true, hasUnion); |
Fariborz Jahanian | 903aba3 | 2010-08-05 21:00:25 +0000 | [diff] [blame] | 1985 | continue; |
| 1986 | } |
Fariborz Jahanian | f95e358 | 2010-08-06 16:28:55 +0000 | [diff] [blame] | 1987 | |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1988 | Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), type); |
Eli Friedman | 8cbca20 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 1989 | unsigned fieldSize = CGM.getContext().getTypeSize(type); |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1990 | |
| 1991 | if (GCAttr == Qualifiers::Strong) |
Eli Friedman | 8cbca20 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 1992 | IvarsInfo.push_back(GC_IVAR(fieldOffset, |
| 1993 | fieldSize / WordSizeInBits)); |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1994 | else if (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak) |
Eli Friedman | 8cbca20 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 1995 | SkipIvars.push_back(GC_IVAR(fieldOffset, |
| 1996 | fieldSize / ByteSizeInBits)); |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1997 | } |
| 1998 | |
| 1999 | if (IvarsInfo.empty()) |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2000 | return nullPtr; |
| 2001 | |
| 2002 | // Sort on byte position; captures might not be allocated in order, |
| 2003 | // and unions can do funny things. |
| 2004 | llvm::array_pod_sort(IvarsInfo.begin(), IvarsInfo.end()); |
| 2005 | llvm::array_pod_sort(SkipIvars.begin(), SkipIvars.end()); |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 2006 | |
| 2007 | std::string BitMap; |
Fariborz Jahanian | 1f78a9a | 2010-08-05 00:19:48 +0000 | [diff] [blame] | 2008 | llvm::Constant *C = BuildIvarLayoutBitmap(BitMap); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2009 | if (CGM.getLangOpts().ObjCGCBitmapPrint) { |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 2010 | printf("\n block variable layout for block: "); |
Roman Divacky | e637711 | 2012-09-06 15:59:27 +0000 | [diff] [blame] | 2011 | const unsigned char *s = (const unsigned char*)BitMap.c_str(); |
Bill Wendling | 5313685 | 2012-02-07 09:06:01 +0000 | [diff] [blame] | 2012 | for (unsigned i = 0, e = BitMap.size(); i < e; i++) |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 2013 | if (!(s[i] & 0xf0)) |
| 2014 | printf("0x0%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 2015 | else |
| 2016 | printf("0x%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 2017 | printf("\n"); |
| 2018 | } |
| 2019 | |
| 2020 | return C; |
Fariborz Jahanian | c05349e | 2010-08-04 16:57:49 +0000 | [diff] [blame] | 2021 | } |
| 2022 | |
Fariborz Jahanian | 2c96d30 | 2012-11-04 18:19:40 +0000 | [diff] [blame] | 2023 | /// getBlockCaptureLifetime - This routine returns life time of the captured |
| 2024 | /// block variable for the purpose of block layout meta-data generation. FQT is |
| 2025 | /// the type of the variable captured in the block. |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 2026 | Qualifiers::ObjCLifetime CGObjCCommonMac::getBlockCaptureLifetime(QualType FQT, |
| 2027 | bool ByrefLayout) { |
Fariborz Jahanian | 2dd7819 | 2012-11-02 22:51:18 +0000 | [diff] [blame] | 2028 | if (CGM.getLangOpts().ObjCAutoRefCount) |
| 2029 | return FQT.getObjCLifetime(); |
| 2030 | |
Fariborz Jahanian | 2c96d30 | 2012-11-04 18:19:40 +0000 | [diff] [blame] | 2031 | // MRR. |
Fariborz Jahanian | 2dd7819 | 2012-11-02 22:51:18 +0000 | [diff] [blame] | 2032 | if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType()) |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 2033 | return ByrefLayout ? Qualifiers::OCL_ExplicitNone : Qualifiers::OCL_Strong; |
Fariborz Jahanian | 2dd7819 | 2012-11-02 22:51:18 +0000 | [diff] [blame] | 2034 | |
| 2035 | return Qualifiers::OCL_None; |
| 2036 | } |
| 2037 | |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2038 | void CGObjCCommonMac::UpdateRunSkipBlockVars(bool IsByref, |
| 2039 | Qualifiers::ObjCLifetime LifeTime, |
Fariborz Jahanian | 7778d61 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2040 | CharUnits FieldOffset, |
| 2041 | CharUnits FieldSize) { |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2042 | // __block variables are passed by their descriptor address. |
| 2043 | if (IsByref) |
| 2044 | RunSkipBlockVars.push_back(RUN_SKIP(BLOCK_LAYOUT_BYREF, FieldOffset, |
Fariborz Jahanian | 7778d61 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2045 | FieldSize)); |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2046 | else if (LifeTime == Qualifiers::OCL_Strong) |
| 2047 | RunSkipBlockVars.push_back(RUN_SKIP(BLOCK_LAYOUT_STRONG, FieldOffset, |
Fariborz Jahanian | 7778d61 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2048 | FieldSize)); |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2049 | else if (LifeTime == Qualifiers::OCL_Weak) |
| 2050 | RunSkipBlockVars.push_back(RUN_SKIP(BLOCK_LAYOUT_WEAK, FieldOffset, |
Fariborz Jahanian | 7778d61 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2051 | FieldSize)); |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2052 | else if (LifeTime == Qualifiers::OCL_ExplicitNone) |
| 2053 | RunSkipBlockVars.push_back(RUN_SKIP(BLOCK_LAYOUT_UNRETAINED, FieldOffset, |
Fariborz Jahanian | 7778d61 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2054 | FieldSize)); |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2055 | else |
| 2056 | RunSkipBlockVars.push_back(RUN_SKIP(BLOCK_LAYOUT_NON_OBJECT_BYTES, |
| 2057 | FieldOffset, |
Fariborz Jahanian | 7778d61 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2058 | FieldSize)); |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2059 | } |
| 2060 | |
| 2061 | void CGObjCCommonMac::BuildRCRecordLayout(const llvm::StructLayout *RecLayout, |
| 2062 | const RecordDecl *RD, |
| 2063 | ArrayRef<const FieldDecl*> RecFields, |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 2064 | CharUnits BytePos, bool &HasUnion, |
| 2065 | bool ByrefLayout) { |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2066 | bool IsUnion = (RD && RD->isUnion()); |
Fariborz Jahanian | 7778d61 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2067 | CharUnits MaxUnionSize = CharUnits::Zero(); |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2068 | const FieldDecl *MaxField = 0; |
| 2069 | const FieldDecl *LastFieldBitfieldOrUnnamed = 0; |
Fariborz Jahanian | 7778d61 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2070 | CharUnits MaxFieldOffset = CharUnits::Zero(); |
| 2071 | CharUnits LastBitfieldOrUnnamedOffset = CharUnits::Zero(); |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2072 | |
| 2073 | if (RecFields.empty()) |
| 2074 | return; |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 2075 | unsigned ByteSizeInBits = CGM.getTarget().getCharWidth(); |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2076 | |
| 2077 | for (unsigned i = 0, e = RecFields.size(); i != e; ++i) { |
| 2078 | const FieldDecl *Field = RecFields[i]; |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2079 | // Note that 'i' here is actually the field index inside RD of Field, |
| 2080 | // although this dependency is hidden. |
| 2081 | const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD); |
Fariborz Jahanian | 7778d61 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2082 | CharUnits FieldOffset = |
| 2083 | CGM.getContext().toCharUnitsFromBits(RL.getFieldOffset(i)); |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2084 | |
| 2085 | // Skip over unnamed or bitfields |
| 2086 | if (!Field->getIdentifier() || Field->isBitField()) { |
| 2087 | LastFieldBitfieldOrUnnamed = Field; |
| 2088 | LastBitfieldOrUnnamedOffset = FieldOffset; |
| 2089 | continue; |
| 2090 | } |
| 2091 | |
| 2092 | LastFieldBitfieldOrUnnamed = 0; |
| 2093 | QualType FQT = Field->getType(); |
| 2094 | if (FQT->isRecordType() || FQT->isUnionType()) { |
| 2095 | if (FQT->isUnionType()) |
| 2096 | HasUnion = true; |
| 2097 | |
| 2098 | BuildRCBlockVarRecordLayout(FQT->getAs<RecordType>(), |
| 2099 | BytePos + FieldOffset, HasUnion); |
| 2100 | continue; |
| 2101 | } |
| 2102 | |
| 2103 | if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { |
| 2104 | const ConstantArrayType *CArray = |
| 2105 | dyn_cast_or_null<ConstantArrayType>(Array); |
| 2106 | uint64_t ElCount = CArray->getSize().getZExtValue(); |
| 2107 | assert(CArray && "only array with known element size is supported"); |
| 2108 | FQT = CArray->getElementType(); |
| 2109 | while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { |
| 2110 | const ConstantArrayType *CArray = |
| 2111 | dyn_cast_or_null<ConstantArrayType>(Array); |
| 2112 | ElCount *= CArray->getSize().getZExtValue(); |
| 2113 | FQT = CArray->getElementType(); |
| 2114 | } |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2115 | if (FQT->isRecordType() && ElCount) { |
| 2116 | int OldIndex = RunSkipBlockVars.size() - 1; |
| 2117 | const RecordType *RT = FQT->getAs<RecordType>(); |
| 2118 | BuildRCBlockVarRecordLayout(RT, BytePos + FieldOffset, |
| 2119 | HasUnion); |
| 2120 | |
| 2121 | // Replicate layout information for each array element. Note that |
| 2122 | // one element is already done. |
| 2123 | uint64_t ElIx = 1; |
| 2124 | for (int FirstIndex = RunSkipBlockVars.size() - 1 ;ElIx < ElCount; ElIx++) { |
Fariborz Jahanian | 7778d61 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2125 | CharUnits Size = CGM.getContext().getTypeSizeInChars(RT); |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2126 | for (int i = OldIndex+1; i <= FirstIndex; ++i) |
| 2127 | RunSkipBlockVars.push_back( |
| 2128 | RUN_SKIP(RunSkipBlockVars[i].opcode, |
| 2129 | RunSkipBlockVars[i].block_var_bytepos + Size*ElIx, |
| 2130 | RunSkipBlockVars[i].block_var_size)); |
| 2131 | } |
| 2132 | continue; |
| 2133 | } |
| 2134 | } |
Fariborz Jahanian | 7778d61 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2135 | CharUnits FieldSize = CGM.getContext().getTypeSizeInChars(Field->getType()); |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2136 | if (IsUnion) { |
Fariborz Jahanian | 7778d61 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2137 | CharUnits UnionIvarSize = FieldSize; |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2138 | if (UnionIvarSize > MaxUnionSize) { |
| 2139 | MaxUnionSize = UnionIvarSize; |
| 2140 | MaxField = Field; |
| 2141 | MaxFieldOffset = FieldOffset; |
| 2142 | } |
| 2143 | } else { |
| 2144 | UpdateRunSkipBlockVars(false, |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 2145 | getBlockCaptureLifetime(FQT, ByrefLayout), |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2146 | BytePos + FieldOffset, |
| 2147 | FieldSize); |
| 2148 | } |
| 2149 | } |
| 2150 | |
| 2151 | if (LastFieldBitfieldOrUnnamed) { |
| 2152 | if (LastFieldBitfieldOrUnnamed->isBitField()) { |
| 2153 | // Last field was a bitfield. Must update the info. |
| 2154 | uint64_t BitFieldSize |
| 2155 | = LastFieldBitfieldOrUnnamed->getBitWidthValue(CGM.getContext()); |
Fariborz Jahanian | 7778d61 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2156 | unsigned UnsSize = (BitFieldSize / ByteSizeInBits) + |
Eli Friedman | 8cbca20 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 2157 | ((BitFieldSize % ByteSizeInBits) != 0); |
Fariborz Jahanian | 7778d61 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2158 | CharUnits Size = CharUnits::fromQuantity(UnsSize); |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2159 | Size += LastBitfieldOrUnnamedOffset; |
| 2160 | UpdateRunSkipBlockVars(false, |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 2161 | getBlockCaptureLifetime(LastFieldBitfieldOrUnnamed->getType(), |
| 2162 | ByrefLayout), |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2163 | BytePos + LastBitfieldOrUnnamedOffset, |
Fariborz Jahanian | 7778d61 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2164 | Size); |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2165 | } else { |
| 2166 | assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed"); |
| 2167 | // Last field was unnamed. Must update skip info. |
Fariborz Jahanian | 7778d61 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2168 | CharUnits FieldSize |
| 2169 | = CGM.getContext().getTypeSizeInChars(LastFieldBitfieldOrUnnamed->getType()); |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2170 | UpdateRunSkipBlockVars(false, |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 2171 | getBlockCaptureLifetime(LastFieldBitfieldOrUnnamed->getType(), |
| 2172 | ByrefLayout), |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2173 | BytePos + LastBitfieldOrUnnamedOffset, |
| 2174 | FieldSize); |
| 2175 | } |
| 2176 | } |
| 2177 | |
| 2178 | if (MaxField) |
| 2179 | UpdateRunSkipBlockVars(false, |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 2180 | getBlockCaptureLifetime(MaxField->getType(), ByrefLayout), |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2181 | BytePos + MaxFieldOffset, |
| 2182 | MaxUnionSize); |
| 2183 | } |
| 2184 | |
| 2185 | void CGObjCCommonMac::BuildRCBlockVarRecordLayout(const RecordType *RT, |
Fariborz Jahanian | 7778d61 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2186 | CharUnits BytePos, |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 2187 | bool &HasUnion, |
| 2188 | bool ByrefLayout) { |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2189 | const RecordDecl *RD = RT->getDecl(); |
Aaron Ballman | 62e47c4 | 2014-03-10 13:43:55 +0000 | [diff] [blame] | 2190 | SmallVector<const FieldDecl*, 16> Fields(RD->fields()); |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2191 | llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0)); |
| 2192 | const llvm::StructLayout *RecLayout = |
| 2193 | CGM.getDataLayout().getStructLayout(cast<llvm::StructType>(Ty)); |
| 2194 | |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 2195 | BuildRCRecordLayout(RecLayout, RD, Fields, BytePos, HasUnion, ByrefLayout); |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2196 | } |
| 2197 | |
Fariborz Jahanian | 23290b0 | 2012-11-01 18:32:55 +0000 | [diff] [blame] | 2198 | /// InlineLayoutInstruction - This routine produce an inline instruction for the |
| 2199 | /// block variable layout if it can. If not, it returns 0. Rules are as follow: |
| 2200 | /// If ((uintptr_t) layout) < (1 << 12), the layout is inline. In the 64bit world, |
| 2201 | /// an inline layout of value 0x0000000000000xyz is interpreted as follows: |
| 2202 | /// x captured object pointers of BLOCK_LAYOUT_STRONG. Followed by |
| 2203 | /// y captured object of BLOCK_LAYOUT_BYREF. Followed by |
| 2204 | /// z captured object of BLOCK_LAYOUT_WEAK. If any of the above is missing, zero |
| 2205 | /// replaces it. For example, 0x00000x00 means x BLOCK_LAYOUT_STRONG and no |
| 2206 | /// BLOCK_LAYOUT_BYREF and no BLOCK_LAYOUT_WEAK objects are captured. |
| 2207 | uint64_t CGObjCCommonMac::InlineLayoutInstruction( |
| 2208 | SmallVectorImpl<unsigned char> &Layout) { |
| 2209 | uint64_t Result = 0; |
| 2210 | if (Layout.size() <= 3) { |
| 2211 | unsigned size = Layout.size(); |
| 2212 | unsigned strong_word_count = 0, byref_word_count=0, weak_word_count=0; |
| 2213 | unsigned char inst; |
| 2214 | enum BLOCK_LAYOUT_OPCODE opcode ; |
| 2215 | switch (size) { |
| 2216 | case 3: |
| 2217 | inst = Layout[0]; |
| 2218 | opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4); |
| 2219 | if (opcode == BLOCK_LAYOUT_STRONG) |
| 2220 | strong_word_count = (inst & 0xF)+1; |
| 2221 | else |
| 2222 | return 0; |
| 2223 | inst = Layout[1]; |
| 2224 | opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4); |
| 2225 | if (opcode == BLOCK_LAYOUT_BYREF) |
| 2226 | byref_word_count = (inst & 0xF)+1; |
| 2227 | else |
| 2228 | return 0; |
| 2229 | inst = Layout[2]; |
| 2230 | opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4); |
| 2231 | if (opcode == BLOCK_LAYOUT_WEAK) |
| 2232 | weak_word_count = (inst & 0xF)+1; |
| 2233 | else |
| 2234 | return 0; |
| 2235 | break; |
| 2236 | |
| 2237 | case 2: |
| 2238 | inst = Layout[0]; |
| 2239 | opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4); |
| 2240 | if (opcode == BLOCK_LAYOUT_STRONG) { |
| 2241 | strong_word_count = (inst & 0xF)+1; |
| 2242 | inst = Layout[1]; |
| 2243 | opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4); |
| 2244 | if (opcode == BLOCK_LAYOUT_BYREF) |
| 2245 | byref_word_count = (inst & 0xF)+1; |
| 2246 | else if (opcode == BLOCK_LAYOUT_WEAK) |
| 2247 | weak_word_count = (inst & 0xF)+1; |
| 2248 | else |
| 2249 | return 0; |
| 2250 | } |
| 2251 | else if (opcode == BLOCK_LAYOUT_BYREF) { |
| 2252 | byref_word_count = (inst & 0xF)+1; |
| 2253 | inst = Layout[1]; |
| 2254 | opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4); |
| 2255 | if (opcode == BLOCK_LAYOUT_WEAK) |
| 2256 | weak_word_count = (inst & 0xF)+1; |
| 2257 | else |
| 2258 | return 0; |
| 2259 | } |
| 2260 | else |
| 2261 | return 0; |
| 2262 | break; |
| 2263 | |
| 2264 | case 1: |
| 2265 | inst = Layout[0]; |
| 2266 | opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4); |
| 2267 | if (opcode == BLOCK_LAYOUT_STRONG) |
| 2268 | strong_word_count = (inst & 0xF)+1; |
| 2269 | else if (opcode == BLOCK_LAYOUT_BYREF) |
| 2270 | byref_word_count = (inst & 0xF)+1; |
| 2271 | else if (opcode == BLOCK_LAYOUT_WEAK) |
| 2272 | weak_word_count = (inst & 0xF)+1; |
| 2273 | else |
| 2274 | return 0; |
| 2275 | break; |
| 2276 | |
| 2277 | default: |
| 2278 | return 0; |
| 2279 | } |
| 2280 | |
| 2281 | // Cannot inline when any of the word counts is 15. Because this is one less |
| 2282 | // than the actual work count (so 15 means 16 actual word counts), |
| 2283 | // and we can only display 0 thru 15 word counts. |
| 2284 | if (strong_word_count == 16 || byref_word_count == 16 || weak_word_count == 16) |
| 2285 | return 0; |
| 2286 | |
| 2287 | unsigned count = |
| 2288 | (strong_word_count != 0) + (byref_word_count != 0) + (weak_word_count != 0); |
| 2289 | |
| 2290 | if (size == count) { |
| 2291 | if (strong_word_count) |
| 2292 | Result = strong_word_count; |
| 2293 | Result <<= 4; |
| 2294 | if (byref_word_count) |
| 2295 | Result += byref_word_count; |
| 2296 | Result <<= 4; |
| 2297 | if (weak_word_count) |
| 2298 | Result += weak_word_count; |
| 2299 | } |
| 2300 | } |
| 2301 | return Result; |
| 2302 | } |
| 2303 | |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 2304 | llvm::Constant *CGObjCCommonMac::getBitmapBlockLayout(bool ComputeByrefLayout) { |
| 2305 | llvm::Constant *nullPtr = llvm::Constant::getNullValue(CGM.Int8PtrTy); |
| 2306 | if (RunSkipBlockVars.empty()) |
| 2307 | return nullPtr; |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 2308 | unsigned WordSizeInBits = CGM.getTarget().getPointerWidth(0); |
| 2309 | unsigned ByteSizeInBits = CGM.getTarget().getCharWidth(); |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 2310 | unsigned WordSizeInBytes = WordSizeInBits/ByteSizeInBits; |
| 2311 | |
| 2312 | // Sort on byte position; captures might not be allocated in order, |
| 2313 | // and unions can do funny things. |
| 2314 | llvm::array_pod_sort(RunSkipBlockVars.begin(), RunSkipBlockVars.end()); |
| 2315 | SmallVector<unsigned char, 16> Layout; |
| 2316 | |
| 2317 | unsigned size = RunSkipBlockVars.size(); |
| 2318 | for (unsigned i = 0; i < size; i++) { |
| 2319 | enum BLOCK_LAYOUT_OPCODE opcode = RunSkipBlockVars[i].opcode; |
| 2320 | CharUnits start_byte_pos = RunSkipBlockVars[i].block_var_bytepos; |
| 2321 | CharUnits end_byte_pos = start_byte_pos; |
| 2322 | unsigned j = i+1; |
| 2323 | while (j < size) { |
| 2324 | if (opcode == RunSkipBlockVars[j].opcode) { |
| 2325 | end_byte_pos = RunSkipBlockVars[j++].block_var_bytepos; |
| 2326 | i++; |
| 2327 | } |
| 2328 | else |
| 2329 | break; |
| 2330 | } |
| 2331 | CharUnits size_in_bytes = |
| 2332 | end_byte_pos - start_byte_pos + RunSkipBlockVars[j-1].block_var_size; |
| 2333 | if (j < size) { |
| 2334 | CharUnits gap = |
| 2335 | RunSkipBlockVars[j].block_var_bytepos - |
| 2336 | RunSkipBlockVars[j-1].block_var_bytepos - RunSkipBlockVars[j-1].block_var_size; |
| 2337 | size_in_bytes += gap; |
| 2338 | } |
| 2339 | CharUnits residue_in_bytes = CharUnits::Zero(); |
| 2340 | if (opcode == BLOCK_LAYOUT_NON_OBJECT_BYTES) { |
| 2341 | residue_in_bytes = size_in_bytes % WordSizeInBytes; |
| 2342 | size_in_bytes -= residue_in_bytes; |
| 2343 | opcode = BLOCK_LAYOUT_NON_OBJECT_WORDS; |
| 2344 | } |
| 2345 | |
| 2346 | unsigned size_in_words = size_in_bytes.getQuantity() / WordSizeInBytes; |
| 2347 | while (size_in_words >= 16) { |
| 2348 | // Note that value in imm. is one less that the actual |
| 2349 | // value. So, 0xf means 16 words follow! |
| 2350 | unsigned char inst = (opcode << 4) | 0xf; |
| 2351 | Layout.push_back(inst); |
| 2352 | size_in_words -= 16; |
| 2353 | } |
| 2354 | if (size_in_words > 0) { |
| 2355 | // Note that value in imm. is one less that the actual |
| 2356 | // value. So, we subtract 1 away! |
| 2357 | unsigned char inst = (opcode << 4) | (size_in_words-1); |
| 2358 | Layout.push_back(inst); |
| 2359 | } |
| 2360 | if (residue_in_bytes > CharUnits::Zero()) { |
| 2361 | unsigned char inst = |
| 2362 | (BLOCK_LAYOUT_NON_OBJECT_BYTES << 4) | (residue_in_bytes.getQuantity()-1); |
| 2363 | Layout.push_back(inst); |
| 2364 | } |
| 2365 | } |
| 2366 | |
| 2367 | int e = Layout.size()-1; |
| 2368 | while (e >= 0) { |
| 2369 | unsigned char inst = Layout[e--]; |
| 2370 | enum BLOCK_LAYOUT_OPCODE opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4); |
| 2371 | if (opcode == BLOCK_LAYOUT_NON_OBJECT_BYTES || opcode == BLOCK_LAYOUT_NON_OBJECT_WORDS) |
| 2372 | Layout.pop_back(); |
| 2373 | else |
| 2374 | break; |
| 2375 | } |
| 2376 | |
| 2377 | uint64_t Result = InlineLayoutInstruction(Layout); |
| 2378 | if (Result != 0) { |
| 2379 | // Block variable layout instruction has been inlined. |
| 2380 | if (CGM.getLangOpts().ObjCGCBitmapPrint) { |
| 2381 | if (ComputeByrefLayout) |
| 2382 | printf("\n Inline instruction for BYREF variable layout: "); |
| 2383 | else |
| 2384 | printf("\n Inline instruction for block variable layout: "); |
Benjamin Kramer | c272794 | 2013-04-22 16:10:38 +0000 | [diff] [blame] | 2385 | printf("0x0%" PRIx64 "\n", Result); |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 2386 | } |
| 2387 | if (WordSizeInBytes == 8) { |
| 2388 | const llvm::APInt Instruction(64, Result); |
| 2389 | return llvm::Constant::getIntegerValue(CGM.Int64Ty, Instruction); |
| 2390 | } |
| 2391 | else { |
| 2392 | const llvm::APInt Instruction(32, Result); |
| 2393 | return llvm::Constant::getIntegerValue(CGM.Int32Ty, Instruction); |
| 2394 | } |
| 2395 | } |
| 2396 | |
| 2397 | unsigned char inst = (BLOCK_LAYOUT_OPERATOR << 4) | 0; |
| 2398 | Layout.push_back(inst); |
| 2399 | std::string BitMap; |
| 2400 | for (unsigned i = 0, e = Layout.size(); i != e; i++) |
| 2401 | BitMap += Layout[i]; |
| 2402 | |
| 2403 | if (CGM.getLangOpts().ObjCGCBitmapPrint) { |
| 2404 | if (ComputeByrefLayout) |
| 2405 | printf("\n BYREF variable layout: "); |
| 2406 | else |
| 2407 | printf("\n block variable layout: "); |
| 2408 | for (unsigned i = 0, e = BitMap.size(); i != e; i++) { |
| 2409 | unsigned char inst = BitMap[i]; |
| 2410 | enum BLOCK_LAYOUT_OPCODE opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4); |
| 2411 | unsigned delta = 1; |
| 2412 | switch (opcode) { |
| 2413 | case BLOCK_LAYOUT_OPERATOR: |
| 2414 | printf("BL_OPERATOR:"); |
| 2415 | delta = 0; |
| 2416 | break; |
| 2417 | case BLOCK_LAYOUT_NON_OBJECT_BYTES: |
| 2418 | printf("BL_NON_OBJECT_BYTES:"); |
| 2419 | break; |
| 2420 | case BLOCK_LAYOUT_NON_OBJECT_WORDS: |
| 2421 | printf("BL_NON_OBJECT_WORD:"); |
| 2422 | break; |
| 2423 | case BLOCK_LAYOUT_STRONG: |
| 2424 | printf("BL_STRONG:"); |
| 2425 | break; |
| 2426 | case BLOCK_LAYOUT_BYREF: |
| 2427 | printf("BL_BYREF:"); |
| 2428 | break; |
| 2429 | case BLOCK_LAYOUT_WEAK: |
| 2430 | printf("BL_WEAK:"); |
| 2431 | break; |
| 2432 | case BLOCK_LAYOUT_UNRETAINED: |
| 2433 | printf("BL_UNRETAINED:"); |
| 2434 | break; |
| 2435 | } |
| 2436 | // Actual value of word count is one more that what is in the imm. |
| 2437 | // field of the instruction |
| 2438 | printf("%d", (inst & 0xf) + delta); |
| 2439 | if (i < e-1) |
| 2440 | printf(", "); |
| 2441 | else |
| 2442 | printf("\n"); |
| 2443 | } |
| 2444 | } |
| 2445 | |
| 2446 | llvm::GlobalVariable * Entry = |
| 2447 | CreateMetadataVar("\01L_OBJC_CLASS_NAME_", |
| 2448 | llvm::ConstantDataArray::getString(VMContext, BitMap,false), |
| 2449 | "__TEXT,__objc_classname,cstring_literals", 1, true); |
| 2450 | return getConstantGEP(VMContext, Entry, 0, 0); |
| 2451 | } |
| 2452 | |
Fariborz Jahanian | 0c58ce9 | 2012-10-27 21:10:38 +0000 | [diff] [blame] | 2453 | llvm::Constant *CGObjCCommonMac::BuildRCBlockLayout(CodeGenModule &CGM, |
| 2454 | const CGBlockInfo &blockInfo) { |
Fariborz Jahanian | 0c58ce9 | 2012-10-27 21:10:38 +0000 | [diff] [blame] | 2455 | assert(CGM.getLangOpts().getGC() == LangOptions::NonGC); |
| 2456 | |
Fariborz Jahanian | 0c58ce9 | 2012-10-27 21:10:38 +0000 | [diff] [blame] | 2457 | RunSkipBlockVars.clear(); |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2458 | bool hasUnion = false; |
| 2459 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 2460 | unsigned WordSizeInBits = CGM.getTarget().getPointerWidth(0); |
| 2461 | unsigned ByteSizeInBits = CGM.getTarget().getCharWidth(); |
Fariborz Jahanian | 0c58ce9 | 2012-10-27 21:10:38 +0000 | [diff] [blame] | 2462 | unsigned WordSizeInBytes = WordSizeInBits/ByteSizeInBits; |
| 2463 | |
| 2464 | const BlockDecl *blockDecl = blockInfo.getBlockDecl(); |
| 2465 | |
| 2466 | // Calculate the basic layout of the block structure. |
| 2467 | const llvm::StructLayout *layout = |
| 2468 | CGM.getDataLayout().getStructLayout(blockInfo.StructureType); |
| 2469 | |
| 2470 | // Ignore the optional 'this' capture: C++ objects are not assumed |
| 2471 | // to be GC'ed. |
Fariborz Jahanian | 4cf177e | 2012-12-04 17:20:57 +0000 | [diff] [blame] | 2472 | if (blockInfo.BlockHeaderForcedGapSize != CharUnits::Zero()) |
| 2473 | UpdateRunSkipBlockVars(false, Qualifiers::OCL_None, |
| 2474 | blockInfo.BlockHeaderForcedGapOffset, |
| 2475 | blockInfo.BlockHeaderForcedGapSize); |
Fariborz Jahanian | 0c58ce9 | 2012-10-27 21:10:38 +0000 | [diff] [blame] | 2476 | // Walk the captured variables. |
| 2477 | for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(), |
| 2478 | ce = blockDecl->capture_end(); ci != ce; ++ci) { |
| 2479 | const VarDecl *variable = ci->getVariable(); |
| 2480 | QualType type = variable->getType(); |
| 2481 | |
| 2482 | const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable); |
| 2483 | |
| 2484 | // Ignore constant captures. |
| 2485 | if (capture.isConstant()) continue; |
| 2486 | |
Fariborz Jahanian | 7778d61 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2487 | CharUnits fieldOffset = |
| 2488 | CharUnits::fromQuantity(layout->getElementOffset(capture.getIndex())); |
Fariborz Jahanian | 0c58ce9 | 2012-10-27 21:10:38 +0000 | [diff] [blame] | 2489 | |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2490 | assert(!type->isArrayType() && "array variable should not be caught"); |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 2491 | if (!ci->isByRef()) |
| 2492 | if (const RecordType *record = type->getAs<RecordType>()) { |
| 2493 | BuildRCBlockVarRecordLayout(record, fieldOffset, hasUnion); |
| 2494 | continue; |
| 2495 | } |
Fariborz Jahanian | 7778d61 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2496 | CharUnits fieldSize; |
| 2497 | if (ci->isByRef()) |
| 2498 | fieldSize = CharUnits::fromQuantity(WordSizeInBytes); |
| 2499 | else |
| 2500 | fieldSize = CGM.getContext().getTypeSizeInChars(type); |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 2501 | UpdateRunSkipBlockVars(ci->isByRef(), getBlockCaptureLifetime(type, false), |
Fariborz Jahanian | 39319c4 | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2502 | fieldOffset, fieldSize); |
Fariborz Jahanian | 0c58ce9 | 2012-10-27 21:10:38 +0000 | [diff] [blame] | 2503 | } |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 2504 | return getBitmapBlockLayout(false); |
| 2505 | } |
Fariborz Jahanian | 0c58ce9 | 2012-10-27 21:10:38 +0000 | [diff] [blame] | 2506 | |
Fariborz Jahanian | 0c58ce9 | 2012-10-27 21:10:38 +0000 | [diff] [blame] | 2507 | |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 2508 | llvm::Constant *CGObjCCommonMac::BuildByrefLayout(CodeGen::CodeGenModule &CGM, |
| 2509 | QualType T) { |
| 2510 | assert(CGM.getLangOpts().getGC() == LangOptions::NonGC); |
| 2511 | assert(!T->isArrayType() && "__block array variable should not be caught"); |
| 2512 | CharUnits fieldOffset; |
| 2513 | RunSkipBlockVars.clear(); |
| 2514 | bool hasUnion = false; |
| 2515 | if (const RecordType *record = T->getAs<RecordType>()) { |
| 2516 | BuildRCBlockVarRecordLayout(record, fieldOffset, hasUnion, true /*ByrefLayout */); |
| 2517 | llvm::Constant *Result = getBitmapBlockLayout(true); |
| 2518 | return Result; |
Fariborz Jahanian | 0c58ce9 | 2012-10-27 21:10:38 +0000 | [diff] [blame] | 2519 | } |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 2520 | llvm::Constant *nullPtr = llvm::Constant::getNullValue(CGM.Int8PtrTy); |
| 2521 | return nullPtr; |
Fariborz Jahanian | 0c58ce9 | 2012-10-27 21:10:38 +0000 | [diff] [blame] | 2522 | } |
| 2523 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 2524 | llvm::Value *CGObjCMac::GenerateProtocolRef(CodeGenFunction &CGF, |
Daniel Dunbar | 89da6ad | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 2525 | const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 7050c55 | 2008-09-04 04:33:15 +0000 | [diff] [blame] | 2526 | // 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] | 2527 | // resolved. Investigate. Its also wasteful to look this up over and over. |
Daniel Dunbar | 7050c55 | 2008-09-04 04:33:15 +0000 | [diff] [blame] | 2528 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); |
| 2529 | |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2530 | return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD), |
Douglas Gregor | 020de32 | 2012-01-17 18:36:30 +0000 | [diff] [blame] | 2531 | ObjCTypes.getExternalProtocolPtrTy()); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2532 | } |
| 2533 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 2534 | void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) { |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 2535 | // FIXME: We shouldn't need this, the protocol decl should contain enough |
| 2536 | // information to tell us whether this was a declaration or a definition. |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2537 | DefinedProtocols.insert(PD->getIdentifier()); |
| 2538 | |
| 2539 | // If we have generated a forward reference to this protocol, emit |
| 2540 | // it now. Otherwise do nothing, the protocol objects are lazily |
| 2541 | // emitted. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2542 | if (Protocols.count(PD->getIdentifier())) |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2543 | GetOrEmitProtocol(PD); |
| 2544 | } |
| 2545 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 2546 | llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2547 | if (DefinedProtocols.count(PD->getIdentifier())) |
| 2548 | return GetOrEmitProtocol(PD); |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 2549 | |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2550 | return GetOrEmitProtocolRef(PD); |
| 2551 | } |
| 2552 | |
Rafael Espindola | 21039aa | 2014-02-27 16:26:32 +0000 | [diff] [blame] | 2553 | static void assertPrivateName(const llvm::GlobalValue *GV) { |
| 2554 | StringRef NameRef = GV->getName(); |
Rui Ueyama | ad33e60 | 2014-02-27 20:50:04 +0000 | [diff] [blame] | 2555 | (void)NameRef; |
Rafael Espindola | 21039aa | 2014-02-27 16:26:32 +0000 | [diff] [blame] | 2556 | assert(NameRef[0] == '\01' && (NameRef[1] == 'L' || NameRef[1] == 'l')); |
Rafael Espindola | 5179a4e | 2014-02-27 19:01:11 +0000 | [diff] [blame] | 2557 | assert(GV->getVisibility() == llvm::GlobalValue::DefaultVisibility); |
| 2558 | assert(GV->getLinkage() == llvm::GlobalValue::PrivateLinkage); |
Rafael Espindola | 21039aa | 2014-02-27 16:26:32 +0000 | [diff] [blame] | 2559 | } |
| 2560 | |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2561 | /* |
Rafael Espindola | f9e1e5e | 2014-02-20 14:09:04 +0000 | [diff] [blame] | 2562 | // Objective-C 1.0 extensions |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2563 | struct _objc_protocol { |
| 2564 | struct _objc_protocol_extension *isa; |
| 2565 | char *protocol_name; |
| 2566 | struct _objc_protocol_list *protocol_list; |
| 2567 | struct _objc__method_prototype_list *instance_methods; |
| 2568 | struct _objc__method_prototype_list *class_methods |
| 2569 | }; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2570 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2571 | See EmitProtocolExtension(). |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2572 | */ |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2573 | llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) { |
John McCall | f9582a7 | 2012-03-30 21:29:05 +0000 | [diff] [blame] | 2574 | llvm::GlobalVariable *Entry = Protocols[PD->getIdentifier()]; |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2575 | |
| 2576 | // Early exit if a defining object has already been generated. |
| 2577 | if (Entry && Entry->hasInitializer()) |
| 2578 | return Entry; |
| 2579 | |
Douglas Gregor | a715bff | 2012-01-01 19:51:50 +0000 | [diff] [blame] | 2580 | // Use the protocol definition, if there is one. |
| 2581 | if (const ObjCProtocolDecl *Def = PD->getDefinition()) |
| 2582 | PD = Def; |
| 2583 | |
Daniel Dunbar | c61d0e9 | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2584 | // 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] | 2585 | // resolved. Investigate. Its also wasteful to look this up over and over. |
Daniel Dunbar | c61d0e9 | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2586 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); |
| 2587 | |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2588 | // Construct method lists. |
| 2589 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 2590 | std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 2591 | std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt; |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 2592 | for (const auto *MD : PD->instance_methods()) { |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2593 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 2594 | if (!C) |
| 2595 | return GetOrEmitProtocolRef(PD); |
| 2596 | |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2597 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 2598 | OptInstanceMethods.push_back(C); |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 2599 | OptMethodTypesExt.push_back(GetMethodVarType(MD, true)); |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2600 | } else { |
| 2601 | InstanceMethods.push_back(C); |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 2602 | MethodTypesExt.push_back(GetMethodVarType(MD, true)); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2603 | } |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2604 | } |
| 2605 | |
Aaron Ballman | e8a7dc9 | 2014-03-13 20:11:06 +0000 | [diff] [blame] | 2606 | for (const auto *MD : PD->class_methods()) { |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2607 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 2608 | if (!C) |
| 2609 | return GetOrEmitProtocolRef(PD); |
| 2610 | |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2611 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 2612 | OptClassMethods.push_back(C); |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 2613 | OptMethodTypesExt.push_back(GetMethodVarType(MD, true)); |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2614 | } else { |
| 2615 | ClassMethods.push_back(C); |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 2616 | MethodTypesExt.push_back(GetMethodVarType(MD, true)); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2617 | } |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2618 | } |
| 2619 | |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 2620 | MethodTypesExt.insert(MethodTypesExt.end(), |
| 2621 | OptMethodTypesExt.begin(), OptMethodTypesExt.end()); |
| 2622 | |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2623 | llvm::Constant *Values[] = { |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 2624 | EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods, |
| 2625 | MethodTypesExt), |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2626 | GetClassName(PD->getIdentifier()), |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2627 | EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(), |
Daniel Dunbar | dec75f8 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 2628 | PD->protocol_begin(), |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2629 | PD->protocol_end()), |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2630 | EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(), |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2631 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2632 | InstanceMethods), |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2633 | EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(), |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2634 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2635 | ClassMethods) |
| 2636 | }; |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2637 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2638 | Values); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2639 | |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2640 | if (Entry) { |
Rafael Espindola | 5d117f3 | 2014-03-06 01:10:46 +0000 | [diff] [blame] | 2641 | // Already created, update the initializer. |
| 2642 | assert(Entry->getLinkage() == llvm::GlobalValue::PrivateLinkage); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2643 | Entry->setInitializer(Init); |
| 2644 | } else { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2645 | Entry = |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2646 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false, |
Rafael Espindola | 5179a4e | 2014-02-27 19:01:11 +0000 | [diff] [blame] | 2647 | llvm::GlobalValue::PrivateLinkage, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2648 | Init, |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2649 | "\01L_OBJC_PROTOCOL_" + PD->getName()); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2650 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2651 | // FIXME: Is this necessary? Why only for protocol? |
| 2652 | Entry->setAlignment(4); |
John McCall | f9582a7 | 2012-03-30 21:29:05 +0000 | [diff] [blame] | 2653 | |
| 2654 | Protocols[PD->getIdentifier()] = Entry; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2655 | } |
Rafael Espindola | 21039aa | 2014-02-27 16:26:32 +0000 | [diff] [blame] | 2656 | assertPrivateName(Entry); |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 2657 | CGM.addCompilerUsedGlobal(Entry); |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2658 | |
| 2659 | return Entry; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2660 | } |
| 2661 | |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2662 | llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2663 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 2664 | |
| 2665 | if (!Entry) { |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2666 | // We use the initializer as a marker of whether this is a forward |
| 2667 | // reference or not. At module finalization we add the empty |
| 2668 | // contents for protocols which were referenced but never defined. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2669 | Entry = |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2670 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false, |
Rafael Espindola | 5179a4e | 2014-02-27 19:01:11 +0000 | [diff] [blame] | 2671 | llvm::GlobalValue::PrivateLinkage, |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2672 | 0, |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2673 | "\01L_OBJC_PROTOCOL_" + PD->getName()); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2674 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2675 | // FIXME: Is this necessary? Why only for protocol? |
| 2676 | Entry->setAlignment(4); |
| 2677 | } |
Rafael Espindola | 21039aa | 2014-02-27 16:26:32 +0000 | [diff] [blame] | 2678 | assertPrivateName(Entry); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2679 | |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2680 | return Entry; |
| 2681 | } |
| 2682 | |
| 2683 | /* |
| 2684 | struct _objc_protocol_extension { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2685 | uint32_t size; |
| 2686 | struct objc_method_description_list *optional_instance_methods; |
| 2687 | struct objc_method_description_list *optional_class_methods; |
| 2688 | struct objc_property_list *instance_properties; |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 2689 | const char ** extendedMethodTypes; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2690 | }; |
| 2691 | */ |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2692 | llvm::Constant * |
| 2693 | CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD, |
Bill Wendling | cb5b5ff | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 2694 | ArrayRef<llvm::Constant*> OptInstanceMethods, |
| 2695 | ArrayRef<llvm::Constant*> OptClassMethods, |
| 2696 | ArrayRef<llvm::Constant*> MethodTypesExt) { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2697 | uint64_t Size = |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2698 | CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy); |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2699 | llvm::Constant *Values[] = { |
| 2700 | llvm::ConstantInt::get(ObjCTypes.IntTy, Size), |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2701 | EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_" |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2702 | + PD->getName(), |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2703 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2704 | OptInstanceMethods), |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2705 | EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(), |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2706 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2707 | OptClassMethods), |
| 2708 | EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(), 0, PD, |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 2709 | ObjCTypes), |
| 2710 | EmitProtocolMethodTypes("\01L_OBJC_PROTOCOL_METHOD_TYPES_" + PD->getName(), |
| 2711 | MethodTypesExt, ObjCTypes) |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2712 | }; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2713 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2714 | // Return null if no extension bits are used. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2715 | if (Values[1]->isNullValue() && Values[2]->isNullValue() && |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 2716 | Values[3]->isNullValue() && Values[4]->isNullValue()) |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2717 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2718 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2719 | llvm::Constant *Init = |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2720 | llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2721 | |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2722 | // No special section, but goes in llvm.used |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2723 | return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(), |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2724 | Init, |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2725 | 0, 0, true); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2726 | } |
| 2727 | |
| 2728 | /* |
| 2729 | struct objc_protocol_list { |
Bill Wendling | a515b58 | 2012-02-09 22:16:49 +0000 | [diff] [blame] | 2730 | struct objc_protocol_list *next; |
| 2731 | long count; |
| 2732 | Protocol *list[]; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2733 | }; |
| 2734 | */ |
Daniel Dunbar | dec75f8 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 2735 | llvm::Constant * |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2736 | CGObjCMac::EmitProtocolList(Twine Name, |
Daniel Dunbar | dec75f8 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 2737 | ObjCProtocolDecl::protocol_iterator begin, |
| 2738 | ObjCProtocolDecl::protocol_iterator end) { |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2739 | SmallVector<llvm::Constant *, 16> ProtocolRefs; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2740 | |
Daniel Dunbar | dec75f8 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 2741 | for (; begin != end; ++begin) |
| 2742 | ProtocolRefs.push_back(GetProtocolRef(*begin)); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2743 | |
| 2744 | // Just return null for empty protocol lists |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2745 | if (ProtocolRefs.empty()) |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2746 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2747 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2748 | // This list is null terminated. |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2749 | ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy)); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2750 | |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2751 | llvm::Constant *Values[3]; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2752 | // This field is only used by the runtime. |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2753 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2754 | Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2755 | ProtocolRefs.size() - 1); |
| 2756 | Values[2] = |
| 2757 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy, |
| 2758 | ProtocolRefs.size()), |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2759 | ProtocolRefs); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2760 | |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2761 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2762 | llvm::GlobalVariable *GV = |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2763 | CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Daniel Dunbar | ae33384 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2764 | 4, false); |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2765 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2766 | } |
| 2767 | |
Bill Wendling | cb5b5ff | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 2768 | void CGObjCCommonMac:: |
| 2769 | PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*,16> &PropertySet, |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2770 | SmallVectorImpl<llvm::Constant *> &Properties, |
Bill Wendling | cb5b5ff | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 2771 | const Decl *Container, |
Aaron Ballman | dc4bea4 | 2014-03-13 18:47:37 +0000 | [diff] [blame] | 2772 | const ObjCProtocolDecl *Proto, |
Bill Wendling | cb5b5ff | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 2773 | const ObjCCommonTypesHelper &ObjCTypes) { |
Aaron Ballman | 0f6e64d | 2014-03-13 22:58:06 +0000 | [diff] [blame^] | 2774 | for (const auto *P : Proto->protocols()) |
| 2775 | PushProtocolProperties(PropertySet, Properties, Container, P, ObjCTypes); |
Aaron Ballman | d174edf | 2014-03-13 19:11:50 +0000 | [diff] [blame] | 2776 | for (const auto *PD : Proto->properties()) { |
Fariborz Jahanian | 751c1e7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 2777 | if (!PropertySet.insert(PD->getIdentifier())) |
| 2778 | continue; |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2779 | llvm::Constant *Prop[] = { |
| 2780 | GetPropertyName(PD->getIdentifier()), |
| 2781 | GetPropertyTypeString(PD, Container) |
| 2782 | }; |
Fariborz Jahanian | 751c1e7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 2783 | Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop)); |
| 2784 | } |
| 2785 | } |
| 2786 | |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2787 | /* |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2788 | struct _objc_property { |
Bill Wendling | a515b58 | 2012-02-09 22:16:49 +0000 | [diff] [blame] | 2789 | const char * const name; |
| 2790 | const char * const attributes; |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2791 | }; |
| 2792 | |
| 2793 | struct _objc_property_list { |
Bill Wendling | a515b58 | 2012-02-09 22:16:49 +0000 | [diff] [blame] | 2794 | uint32_t entsize; // sizeof (struct _objc_property) |
| 2795 | uint32_t prop_count; |
| 2796 | struct _objc_property[prop_count]; |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2797 | }; |
| 2798 | */ |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2799 | llvm::Constant *CGObjCCommonMac::EmitPropertyList(Twine Name, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2800 | const Decl *Container, |
| 2801 | const ObjCContainerDecl *OCD, |
| 2802 | const ObjCCommonTypesHelper &ObjCTypes) { |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2803 | SmallVector<llvm::Constant *, 16> Properties; |
Fariborz Jahanian | 751c1e7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 2804 | llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet; |
Aaron Ballman | d174edf | 2014-03-13 19:11:50 +0000 | [diff] [blame] | 2805 | for (const auto *PD : OCD->properties()) { |
Fariborz Jahanian | 751c1e7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 2806 | PropertySet.insert(PD->getIdentifier()); |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2807 | llvm::Constant *Prop[] = { |
| 2808 | GetPropertyName(PD->getIdentifier()), |
| 2809 | GetPropertyTypeString(PD, Container) |
| 2810 | }; |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2811 | Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2812 | Prop)); |
| 2813 | } |
Fariborz Jahanian | 7966aff | 2010-06-22 16:33:55 +0000 | [diff] [blame] | 2814 | if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) { |
Aaron Ballman | a9f49e3 | 2014-03-13 20:55:22 +0000 | [diff] [blame] | 2815 | for (const auto *P : OID->all_referenced_protocols()) |
| 2816 | PushProtocolProperties(PropertySet, Properties, Container, P, ObjCTypes); |
Fariborz Jahanian | 7966aff | 2010-06-22 16:33:55 +0000 | [diff] [blame] | 2817 | } |
| 2818 | else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) { |
| 2819 | for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(), |
| 2820 | E = CD->protocol_end(); P != E; ++P) |
| 2821 | PushProtocolProperties(PropertySet, Properties, Container, (*P), |
| 2822 | ObjCTypes); |
| 2823 | } |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2824 | |
| 2825 | // Return null for empty list. |
| 2826 | if (Properties.empty()) |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2827 | return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2828 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2829 | unsigned PropertySize = |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2830 | CGM.getDataLayout().getTypeAllocSize(ObjCTypes.PropertyTy); |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2831 | llvm::Constant *Values[3]; |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2832 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize); |
| 2833 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size()); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2834 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy, |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2835 | Properties.size()); |
Owen Anderson | 47034e1 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 2836 | Values[2] = llvm::ConstantArray::get(AT, Properties); |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2837 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2838 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2839 | llvm::GlobalVariable *GV = |
| 2840 | CreateMetadataVar(Name, Init, |
| 2841 | (ObjCABI == 2) ? "__DATA, __objc_const" : |
Daniel Dunbar | b25452a | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2842 | "__OBJC,__property,regular,no_dead_strip", |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2843 | (ObjCABI == 2) ? 8 : 4, |
Daniel Dunbar | b25452a | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2844 | true); |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2845 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy); |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2846 | } |
| 2847 | |
Bill Wendling | cb5b5ff | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 2848 | llvm::Constant * |
| 2849 | CGObjCCommonMac::EmitProtocolMethodTypes(Twine Name, |
| 2850 | ArrayRef<llvm::Constant*> MethodTypes, |
| 2851 | const ObjCCommonTypesHelper &ObjCTypes) { |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 2852 | // Return null for empty list. |
| 2853 | if (MethodTypes.empty()) |
| 2854 | return llvm::Constant::getNullValue(ObjCTypes.Int8PtrPtrTy); |
| 2855 | |
| 2856 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
| 2857 | MethodTypes.size()); |
| 2858 | llvm::Constant *Init = llvm::ConstantArray::get(AT, MethodTypes); |
| 2859 | |
| 2860 | llvm::GlobalVariable *GV = |
| 2861 | CreateMetadataVar(Name, Init, |
| 2862 | (ObjCABI == 2) ? "__DATA, __objc_const" : 0, |
| 2863 | (ObjCABI == 2) ? 8 : 4, |
| 2864 | true); |
| 2865 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.Int8PtrPtrTy); |
| 2866 | } |
| 2867 | |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2868 | /* |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2869 | struct objc_method_description_list { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2870 | int count; |
| 2871 | struct objc_method_description list[]; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2872 | }; |
| 2873 | */ |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2874 | llvm::Constant * |
| 2875 | CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2876 | llvm::Constant *Desc[] = { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2877 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2878 | ObjCTypes.SelectorPtrTy), |
| 2879 | GetMethodVarType(MD) |
| 2880 | }; |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 2881 | if (!Desc[1]) |
| 2882 | return 0; |
| 2883 | |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2884 | return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy, |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2885 | Desc); |
| 2886 | } |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2887 | |
Bill Wendling | cb5b5ff | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 2888 | llvm::Constant * |
| 2889 | CGObjCMac::EmitMethodDescList(Twine Name, const char *Section, |
| 2890 | ArrayRef<llvm::Constant*> Methods) { |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2891 | // Return null for empty list. |
| 2892 | if (Methods.empty()) |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2893 | return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2894 | |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2895 | llvm::Constant *Values[2]; |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2896 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2897 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy, |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2898 | Methods.size()); |
Owen Anderson | 47034e1 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 2899 | Values[1] = llvm::ConstantArray::get(AT, Methods); |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2900 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2901 | |
Daniel Dunbar | b25452a | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2902 | llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2903 | return llvm::ConstantExpr::getBitCast(GV, |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2904 | ObjCTypes.MethodDescriptionListPtrTy); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2905 | } |
| 2906 | |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2907 | /* |
| 2908 | struct _objc_category { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2909 | char *category_name; |
| 2910 | char *class_name; |
| 2911 | struct _objc_method_list *instance_methods; |
| 2912 | struct _objc_method_list *class_methods; |
| 2913 | struct _objc_protocol_list *protocols; |
| 2914 | uint32_t size; // <rdar://4585769> |
| 2915 | struct _objc_property_list *instance_properties; |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2916 | }; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2917 | */ |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2918 | void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) { |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2919 | unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.CategoryTy); |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2920 | |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 2921 | // FIXME: This is poor design, the OCD should have a pointer to the category |
| 2922 | // decl. Additionally, note that Category can be null for the @implementation |
| 2923 | // w/o an @interface case. Sema should just create one for us as it does for |
| 2924 | // @implementation so everyone else can live life under a clear blue sky. |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2925 | const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2926 | const ObjCCategoryDecl *Category = |
Daniel Dunbar | 28e76ca | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 2927 | Interface->FindCategoryDeclaration(OCD->getIdentifier()); |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2928 | |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 2929 | SmallString<256> ExtName; |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2930 | llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_' |
| 2931 | << OCD->getName(); |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2932 | |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2933 | SmallVector<llvm::Constant *, 16> InstanceMethods, ClassMethods; |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 2934 | for (const auto *I : OCD->instance_methods()) |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2935 | // Instance methods should always be defined. |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 2936 | InstanceMethods.push_back(GetMethodConstant(I)); |
| 2937 | |
Aaron Ballman | e8a7dc9 | 2014-03-13 20:11:06 +0000 | [diff] [blame] | 2938 | for (const auto *I : OCD->class_methods()) |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2939 | // Class methods should always be defined. |
Aaron Ballman | e8a7dc9 | 2014-03-13 20:11:06 +0000 | [diff] [blame] | 2940 | ClassMethods.push_back(GetMethodConstant(I)); |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2941 | |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2942 | llvm::Constant *Values[7]; |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2943 | Values[0] = GetClassName(OCD->getIdentifier()); |
| 2944 | Values[1] = GetClassName(Interface->getIdentifier()); |
Fariborz Jahanian | e55f866 | 2009-04-29 20:40:05 +0000 | [diff] [blame] | 2945 | LazySymbols.insert(Interface->getIdentifier()); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2946 | Values[2] = |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2947 | EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(), |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2948 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2949 | InstanceMethods); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2950 | Values[3] = |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2951 | EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(), |
Daniel Dunbar | b25452a | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2952 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2953 | ClassMethods); |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2954 | if (Category) { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2955 | Values[4] = |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2956 | EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(), |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2957 | Category->protocol_begin(), |
| 2958 | Category->protocol_end()); |
| 2959 | } else { |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2960 | Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2961 | } |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2962 | Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | 28e76ca | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 2963 | |
| 2964 | // If there is no category @interface then there can be no properties. |
| 2965 | if (Category) { |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2966 | Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(), |
Fariborz Jahanian | 066347e | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 2967 | OCD, Category, ObjCTypes); |
Daniel Dunbar | 28e76ca | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 2968 | } else { |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2969 | Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
Daniel Dunbar | 28e76ca | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 2970 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2971 | |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2972 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy, |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2973 | Values); |
| 2974 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2975 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2976 | CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init, |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2977 | "__OBJC,__category,regular,no_dead_strip", |
Daniel Dunbar | ae33384 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2978 | 4, true); |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2979 | DefinedCategories.push_back(GV); |
Fariborz Jahanian | 9adb2e6 | 2010-06-21 22:05:18 +0000 | [diff] [blame] | 2980 | DefinedCategoryNames.insert(ExtName.str()); |
Fariborz Jahanian | c057794 | 2011-04-22 22:02:28 +0000 | [diff] [blame] | 2981 | // method definition entries must be clear for next implementation. |
| 2982 | MethodDefinitions.clear(); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2983 | } |
| 2984 | |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 2985 | enum FragileClassFlags { |
| 2986 | FragileABI_Class_Factory = 0x00001, |
| 2987 | FragileABI_Class_Meta = 0x00002, |
| 2988 | FragileABI_Class_HasCXXStructors = 0x02000, |
| 2989 | FragileABI_Class_Hidden = 0x20000 |
| 2990 | }; |
| 2991 | |
| 2992 | enum NonFragileClassFlags { |
| 2993 | /// Is a meta-class. |
| 2994 | NonFragileABI_Class_Meta = 0x00001, |
| 2995 | |
| 2996 | /// Is a root class. |
| 2997 | NonFragileABI_Class_Root = 0x00002, |
| 2998 | |
| 2999 | /// Has a C++ constructor and destructor. |
| 3000 | NonFragileABI_Class_HasCXXStructors = 0x00004, |
| 3001 | |
| 3002 | /// Has hidden visibility. |
| 3003 | NonFragileABI_Class_Hidden = 0x00010, |
| 3004 | |
| 3005 | /// Has the exception attribute. |
| 3006 | NonFragileABI_Class_Exception = 0x00020, |
| 3007 | |
| 3008 | /// (Obsolete) ARC-specific: this class has a .release_ivars method |
| 3009 | NonFragileABI_Class_HasIvarReleaser = 0x00040, |
| 3010 | |
| 3011 | /// Class implementation was compiled under ARC. |
John McCall | 0d54a17 | 2012-10-17 04:53:31 +0000 | [diff] [blame] | 3012 | NonFragileABI_Class_CompiledByARC = 0x00080, |
| 3013 | |
| 3014 | /// Class has non-trivial destructors, but zero-initialization is okay. |
| 3015 | NonFragileABI_Class_HasCXXDestructorOnly = 0x00100 |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3016 | }; |
| 3017 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3018 | /* |
| 3019 | struct _objc_class { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3020 | Class isa; |
| 3021 | Class super_class; |
| 3022 | const char *name; |
| 3023 | long version; |
| 3024 | long info; |
| 3025 | long instance_size; |
| 3026 | struct _objc_ivar_list *ivars; |
| 3027 | struct _objc_method_list *methods; |
| 3028 | struct _objc_cache *cache; |
| 3029 | struct _objc_protocol_list *protocols; |
| 3030 | // Objective-C 1.0 extensions (<rdr://4585769>) |
| 3031 | const char *ivar_layout; |
| 3032 | struct _objc_class_ext *ext; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3033 | }; |
| 3034 | |
| 3035 | See EmitClassExtension(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3036 | */ |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3037 | void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) { |
Daniel Dunbar | c61d0e9 | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3038 | DefinedSymbols.insert(ID->getIdentifier()); |
| 3039 | |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3040 | std::string ClassName = ID->getNameAsString(); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3041 | // FIXME: Gross |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3042 | ObjCInterfaceDecl *Interface = |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3043 | const_cast<ObjCInterfaceDecl*>(ID->getClassInterface()); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3044 | llvm::Constant *Protocols = |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 3045 | EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(), |
Ted Kremenek | 0ef508d | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 3046 | Interface->all_referenced_protocol_begin(), |
| 3047 | Interface->all_referenced_protocol_end()); |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 3048 | unsigned Flags = FragileABI_Class_Factory; |
John McCall | 0d54a17 | 2012-10-17 04:53:31 +0000 | [diff] [blame] | 3049 | if (ID->hasNonZeroConstructors() || ID->hasDestructors()) |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 3050 | Flags |= FragileABI_Class_HasCXXStructors; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3051 | unsigned Size = |
Ken Dyck | c8ae550 | 2011-02-09 01:59:34 +0000 | [diff] [blame] | 3052 | CGM.getContext().getASTObjCImplementationLayout(ID).getSize().getQuantity(); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3053 | |
| 3054 | // FIXME: Set CXX-structors flag. |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 3055 | if (ID->getClassInterface()->getVisibility() == HiddenVisibility) |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 3056 | Flags |= FragileABI_Class_Hidden; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3057 | |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3058 | SmallVector<llvm::Constant *, 16> InstanceMethods, ClassMethods; |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 3059 | for (const auto *I : ID->instance_methods()) |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 3060 | // Instance methods should always be defined. |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 3061 | InstanceMethods.push_back(GetMethodConstant(I)); |
| 3062 | |
Aaron Ballman | e8a7dc9 | 2014-03-13 20:11:06 +0000 | [diff] [blame] | 3063 | for (const auto *I : ID->class_methods()) |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 3064 | // Class methods should always be defined. |
Aaron Ballman | e8a7dc9 | 2014-03-13 20:11:06 +0000 | [diff] [blame] | 3065 | ClassMethods.push_back(GetMethodConstant(I)); |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 3066 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3067 | for (ObjCImplementationDecl::propimpl_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3068 | i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 3069 | ObjCPropertyImplDecl *PID = *i; |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 3070 | |
| 3071 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) { |
| 3072 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 3073 | |
| 3074 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) |
| 3075 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 3076 | InstanceMethods.push_back(C); |
| 3077 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) |
| 3078 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 3079 | InstanceMethods.push_back(C); |
| 3080 | } |
| 3081 | } |
| 3082 | |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 3083 | llvm::Constant *Values[12]; |
Daniel Dunbar | ccf6183 | 2009-05-03 08:56:52 +0000 | [diff] [blame] | 3084 | Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3085 | if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) { |
Daniel Dunbar | c61d0e9 | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3086 | // Record a reference to the super class. |
| 3087 | LazySymbols.insert(Super->getIdentifier()); |
| 3088 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3089 | Values[ 1] = |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 3090 | llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3091 | ObjCTypes.ClassPtrTy); |
| 3092 | } else { |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3093 | Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3094 | } |
| 3095 | Values[ 2] = GetClassName(ID->getIdentifier()); |
| 3096 | // Version is always 0. |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 3097 | Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 3098 | Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); |
| 3099 | Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Fariborz Jahanian | b042a59 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 3100 | Values[ 6] = EmitIvarList(ID, false); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3101 | Values[ 7] = |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 3102 | EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(), |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3103 | "__OBJC,__inst_meth,regular,no_dead_strip", |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 3104 | InstanceMethods); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3105 | // cache is always NULL. |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3106 | Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3107 | Values[ 9] = Protocols; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3108 | Values[10] = BuildIvarLayout(ID, true); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3109 | Values[11] = EmitClassExtension(ID); |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 3110 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3111 | Values); |
Fariborz Jahanian | eb80c98 | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 3112 | std::string Name("\01L_OBJC_CLASS_"); |
| 3113 | Name += ClassName; |
| 3114 | const char *Section = "__OBJC,__class,regular,no_dead_strip"; |
| 3115 | // Check for a forward reference. |
Rafael Espindola | 554256c | 2014-02-26 22:25:45 +0000 | [diff] [blame] | 3116 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true); |
Fariborz Jahanian | eb80c98 | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 3117 | if (GV) { |
| 3118 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 3119 | "Forward metaclass reference has incorrect type."); |
Fariborz Jahanian | eb80c98 | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 3120 | GV->setInitializer(Init); |
| 3121 | GV->setSection(Section); |
| 3122 | GV->setAlignment(4); |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 3123 | CGM.addCompilerUsedGlobal(GV); |
Rafael Espindola | 21039aa | 2014-02-27 16:26:32 +0000 | [diff] [blame] | 3124 | } else |
Fariborz Jahanian | eb80c98 | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 3125 | GV = CreateMetadataVar(Name, Init, Section, 4, true); |
Rafael Espindola | 21039aa | 2014-02-27 16:26:32 +0000 | [diff] [blame] | 3126 | assertPrivateName(GV); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3127 | DefinedClasses.push_back(GV); |
Fariborz Jahanian | f322f2f | 2014-03-11 00:25:05 +0000 | [diff] [blame] | 3128 | ImplementedClasses.push_back(Interface); |
Fariborz Jahanian | c057794 | 2011-04-22 22:02:28 +0000 | [diff] [blame] | 3129 | // method definition entries must be clear for next implementation. |
| 3130 | MethodDefinitions.clear(); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3131 | } |
| 3132 | |
| 3133 | llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID, |
| 3134 | llvm::Constant *Protocols, |
Bill Wendling | cb5b5ff | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 3135 | ArrayRef<llvm::Constant*> Methods) { |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 3136 | unsigned Flags = FragileABI_Class_Meta; |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3137 | unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ClassTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3138 | |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 3139 | if (ID->getClassInterface()->getVisibility() == HiddenVisibility) |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 3140 | Flags |= FragileABI_Class_Hidden; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3141 | |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 3142 | llvm::Constant *Values[12]; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3143 | // The isa for the metaclass is the root of the hierarchy. |
| 3144 | const ObjCInterfaceDecl *Root = ID->getClassInterface(); |
| 3145 | while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) |
| 3146 | Root = Super; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3147 | Values[ 0] = |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 3148 | llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()), |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3149 | ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3150 | // The super class for the metaclass is emitted as the name of the |
| 3151 | // super class. The runtime fixes this up to point to the |
| 3152 | // *metaclass* for the super class. |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3153 | if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3154 | Values[ 1] = |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 3155 | llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3156 | ObjCTypes.ClassPtrTy); |
| 3157 | } else { |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3158 | Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3159 | } |
| 3160 | Values[ 2] = GetClassName(ID->getIdentifier()); |
| 3161 | // Version is always 0. |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 3162 | Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 3163 | Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); |
| 3164 | Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Fariborz Jahanian | b042a59 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 3165 | Values[ 6] = EmitIvarList(ID, true); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3166 | Values[ 7] = |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3167 | EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(), |
Daniel Dunbar | b25452a | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 3168 | "__OBJC,__cls_meth,regular,no_dead_strip", |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 3169 | Methods); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3170 | // cache is always NULL. |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3171 | Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3172 | Values[ 9] = Protocols; |
| 3173 | // ivar_layout for metaclass is always NULL. |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3174 | Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3175 | // The class extension is always unused for metaclasses. |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3176 | Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy); |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 3177 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3178 | Values); |
| 3179 | |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 3180 | std::string Name("\01L_OBJC_METACLASS_"); |
Benjamin Kramer | 1bbcbd0 | 2012-07-31 11:45:39 +0000 | [diff] [blame] | 3181 | Name += ID->getName(); |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 3182 | |
| 3183 | // Check for a forward reference. |
Rafael Espindola | 554256c | 2014-02-26 22:25:45 +0000 | [diff] [blame] | 3184 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true); |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 3185 | if (GV) { |
| 3186 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 3187 | "Forward metaclass reference has incorrect type."); |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 3188 | GV->setInitializer(Init); |
| 3189 | } else { |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 3190 | GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false, |
Rafael Espindola | 5179a4e | 2014-02-27 19:01:11 +0000 | [diff] [blame] | 3191 | llvm::GlobalValue::PrivateLinkage, |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 3192 | Init, Name); |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 3193 | } |
Rafael Espindola | 21039aa | 2014-02-27 16:26:32 +0000 | [diff] [blame] | 3194 | assertPrivateName(GV); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3195 | GV->setSection("__OBJC,__meta_class,regular,no_dead_strip"); |
Daniel Dunbar | ae33384 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 3196 | GV->setAlignment(4); |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 3197 | CGM.addCompilerUsedGlobal(GV); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3198 | |
| 3199 | return GV; |
| 3200 | } |
| 3201 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3202 | llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) { |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3203 | std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString(); |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 3204 | |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 3205 | // FIXME: Should we look these up somewhere other than the module. Its a bit |
| 3206 | // silly since we only generate these while processing an implementation, so |
| 3207 | // exactly one pointer would work if know when we entered/exitted an |
| 3208 | // implementation block. |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 3209 | |
| 3210 | // Check for an existing forward reference. |
Fariborz Jahanian | 475831b | 2009-01-07 20:11:22 +0000 | [diff] [blame] | 3211 | // Previously, metaclass with internal linkage may have been defined. |
| 3212 | // pass 'true' as 2nd argument so it is returned. |
Rafael Espindola | 21039aa | 2014-02-27 16:26:32 +0000 | [diff] [blame] | 3213 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true); |
| 3214 | if (!GV) |
| 3215 | GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false, |
Rafael Espindola | 5179a4e | 2014-02-27 19:01:11 +0000 | [diff] [blame] | 3216 | llvm::GlobalValue::PrivateLinkage, 0, Name); |
Rafael Espindola | 21039aa | 2014-02-27 16:26:32 +0000 | [diff] [blame] | 3217 | |
| 3218 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 3219 | "Forward metaclass reference has incorrect type."); |
| 3220 | assertPrivateName(GV); |
| 3221 | return GV; |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 3222 | } |
| 3223 | |
Fariborz Jahanian | eb80c98 | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 3224 | llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) { |
| 3225 | std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString(); |
Rafael Espindola | 21039aa | 2014-02-27 16:26:32 +0000 | [diff] [blame] | 3226 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true); |
| 3227 | |
| 3228 | if (!GV) |
| 3229 | GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false, |
Rafael Espindola | 5179a4e | 2014-02-27 19:01:11 +0000 | [diff] [blame] | 3230 | llvm::GlobalValue::PrivateLinkage, 0, Name); |
Rafael Espindola | 21039aa | 2014-02-27 16:26:32 +0000 | [diff] [blame] | 3231 | |
| 3232 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 3233 | "Forward class metadata reference has incorrect type."); |
| 3234 | assertPrivateName(GV); |
| 3235 | return GV; |
Fariborz Jahanian | eb80c98 | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 3236 | } |
| 3237 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3238 | /* |
| 3239 | struct objc_class_ext { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3240 | uint32_t size; |
| 3241 | const char *weak_ivar_layout; |
| 3242 | struct _objc_property_list *properties; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3243 | }; |
| 3244 | */ |
| 3245 | llvm::Constant * |
| 3246 | CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3247 | uint64_t Size = |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3248 | CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ClassExtensionTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3249 | |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 3250 | llvm::Constant *Values[3]; |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 3251 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Fariborz Jahanian | 6df6986 | 2009-04-22 23:00:43 +0000 | [diff] [blame] | 3252 | Values[1] = BuildIvarLayout(ID, false); |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 3253 | Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(), |
Fariborz Jahanian | 066347e | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 3254 | ID, ID->getClassInterface(), ObjCTypes); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3255 | |
| 3256 | // Return null if no extension bits are used. |
| 3257 | if (Values[1]->isNullValue() && Values[2]->isNullValue()) |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3258 | return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3259 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3260 | llvm::Constant *Init = |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 3261 | llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values); |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 3262 | return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(), |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3263 | Init, "__OBJC,__class_ext,regular,no_dead_strip", |
Daniel Dunbar | b25452a | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 3264 | 4, true); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3265 | } |
| 3266 | |
| 3267 | /* |
| 3268 | struct objc_ivar { |
Bill Wendling | f1a3fca | 2012-02-22 09:30:11 +0000 | [diff] [blame] | 3269 | char *ivar_name; |
| 3270 | char *ivar_type; |
| 3271 | int ivar_offset; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3272 | }; |
| 3273 | |
| 3274 | struct objc_ivar_list { |
Bill Wendling | f1a3fca | 2012-02-22 09:30:11 +0000 | [diff] [blame] | 3275 | int ivar_count; |
| 3276 | struct objc_ivar list[count]; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3277 | }; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3278 | */ |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3279 | llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID, |
Fariborz Jahanian | b042a59 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 3280 | bool ForClass) { |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 3281 | std::vector<llvm::Constant*> Ivars; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3282 | |
| 3283 | // When emitting the root class GCC emits ivar entries for the |
| 3284 | // actual class structure. It is not clear if we need to follow this |
| 3285 | // behavior; for now lets try and get away with not doing it. If so, |
| 3286 | // the cleanest solution would be to make up an ObjCInterfaceDecl |
| 3287 | // for the class. |
| 3288 | if (ForClass) |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3289 | return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3290 | |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 3291 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3292 | |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 3293 | for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin(); |
Fariborz Jahanian | b26d578 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 3294 | IVD; IVD = IVD->getNextIvar()) { |
Fariborz Jahanian | 7c80959 | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 3295 | // Ignore unnamed bit-fields. |
| 3296 | if (!IVD->getDeclName()) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3297 | continue; |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 3298 | llvm::Constant *Ivar[] = { |
| 3299 | GetMethodVarName(IVD->getIdentifier()), |
| 3300 | GetMethodVarType(IVD), |
| 3301 | llvm::ConstantInt::get(ObjCTypes.IntTy, |
Eli Friedman | 8cbca20 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 3302 | ComputeIvarBaseOffset(CGM, OID, IVD)) |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 3303 | }; |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 3304 | Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar)); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3305 | } |
| 3306 | |
| 3307 | // Return null for empty list. |
| 3308 | if (Ivars.empty()) |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3309 | return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3310 | |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 3311 | llvm::Constant *Values[2]; |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 3312 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3313 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy, |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3314 | Ivars.size()); |
Owen Anderson | 47034e1 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 3315 | Values[1] = llvm::ConstantArray::get(AT, Ivars); |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 3316 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3317 | |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3318 | llvm::GlobalVariable *GV; |
| 3319 | if (ForClass) |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 3320 | GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(), |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3321 | Init, "__OBJC,__class_vars,regular,no_dead_strip", |
Daniel Dunbar | ae33384 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 3322 | 4, true); |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3323 | else |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 3324 | GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(), |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3325 | Init, "__OBJC,__instance_vars,regular,no_dead_strip", |
Daniel Dunbar | b25452a | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 3326 | 4, true); |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 3327 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3328 | } |
| 3329 | |
| 3330 | /* |
| 3331 | struct objc_method { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3332 | SEL method_name; |
| 3333 | char *method_types; |
| 3334 | void *method; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3335 | }; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3336 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3337 | struct objc_method_list { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3338 | struct objc_method_list *obsolete; |
| 3339 | int count; |
| 3340 | struct objc_method methods_list[count]; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3341 | }; |
| 3342 | */ |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 3343 | |
| 3344 | /// GetMethodConstant - Return a struct objc_method constant for the |
| 3345 | /// given method if it has been defined. The result is null if the |
| 3346 | /// method has not been defined. The return value has type MethodPtrTy. |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 3347 | llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) { |
Argyrios Kyrtzidis | 13257c5 | 2010-08-09 10:54:20 +0000 | [diff] [blame] | 3348 | llvm::Function *Fn = GetMethodDefinition(MD); |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 3349 | if (!Fn) |
| 3350 | return 0; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3351 | |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 3352 | llvm::Constant *Method[] = { |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 3353 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 3354 | ObjCTypes.SelectorPtrTy), |
| 3355 | GetMethodVarType(MD), |
| 3356 | llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy) |
| 3357 | }; |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 3358 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method); |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 3359 | } |
| 3360 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3361 | llvm::Constant *CGObjCMac::EmitMethodList(Twine Name, |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3362 | const char *Section, |
Bill Wendling | cb5b5ff | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 3363 | ArrayRef<llvm::Constant*> Methods) { |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3364 | // Return null for empty list. |
| 3365 | if (Methods.empty()) |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3366 | return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3367 | |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 3368 | llvm::Constant *Values[3]; |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3369 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 3370 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3371 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy, |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3372 | Methods.size()); |
Owen Anderson | 47034e1 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 3373 | Values[2] = llvm::ConstantArray::get(AT, Methods); |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 3374 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3375 | |
Daniel Dunbar | b25452a | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 3376 | llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true); |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 3377 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListPtrTy); |
Daniel Dunbar | a94ecd2 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 3378 | } |
| 3379 | |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 3380 | llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3381 | const ObjCContainerDecl *CD) { |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 3382 | SmallString<256> Name; |
Fariborz Jahanian | 0196a1c | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 3383 | GetNameForMethod(OMD, CD, Name); |
Daniel Dunbar | a94ecd2 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 3384 | |
Daniel Dunbar | bf8c24a | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 3385 | CodeGenTypes &Types = CGM.getTypes(); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3386 | llvm::FunctionType *MethodTy = |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 3387 | Types.GetFunctionType(Types.arrangeObjCMethodDeclaration(OMD)); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3388 | llvm::Function *Method = |
Daniel Dunbar | 7a95ca3 | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 3389 | llvm::Function::Create(MethodTy, |
Daniel Dunbar | a94ecd2 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 3390 | llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | d238681 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 3391 | Name.str(), |
Daniel Dunbar | a94ecd2 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 3392 | &CGM.getModule()); |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 3393 | MethodDefinitions.insert(std::make_pair(OMD, Method)); |
Daniel Dunbar | a94ecd2 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 3394 | |
Daniel Dunbar | a94ecd2 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 3395 | return Method; |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 3396 | } |
| 3397 | |
Daniel Dunbar | 30c6536 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 3398 | llvm::GlobalVariable * |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3399 | CGObjCCommonMac::CreateMetadataVar(Twine Name, |
Daniel Dunbar | 30c6536 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 3400 | llvm::Constant *Init, |
| 3401 | const char *Section, |
Daniel Dunbar | 463cc8a | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 3402 | unsigned Align, |
| 3403 | bool AddToUsed) { |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3404 | llvm::Type *Ty = Init->getType(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3405 | llvm::GlobalVariable *GV = |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 3406 | new llvm::GlobalVariable(CGM.getModule(), Ty, false, |
Rafael Espindola | 5179a4e | 2014-02-27 19:01:11 +0000 | [diff] [blame] | 3407 | llvm::GlobalValue::PrivateLinkage, Init, Name); |
Rafael Espindola | 21039aa | 2014-02-27 16:26:32 +0000 | [diff] [blame] | 3408 | assertPrivateName(GV); |
Daniel Dunbar | 30c6536 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 3409 | if (Section) |
| 3410 | GV->setSection(Section); |
Daniel Dunbar | 463cc8a | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 3411 | if (Align) |
| 3412 | GV->setAlignment(Align); |
| 3413 | if (AddToUsed) |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 3414 | CGM.addCompilerUsedGlobal(GV); |
Daniel Dunbar | 30c6536 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 3415 | return GV; |
| 3416 | } |
| 3417 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3418 | llvm::Function *CGObjCMac::ModuleInitFunction() { |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3419 | // Abuse this interface function as a place to finalize. |
| 3420 | FinishModule(); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 3421 | return NULL; |
| 3422 | } |
| 3423 | |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 3424 | llvm::Constant *CGObjCMac::GetPropertyGetFunction() { |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 3425 | return ObjCTypes.getGetPropertyFn(); |
Daniel Dunbar | a91c3e0 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 3426 | } |
| 3427 | |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 3428 | llvm::Constant *CGObjCMac::GetPropertySetFunction() { |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 3429 | return ObjCTypes.getSetPropertyFn(); |
Daniel Dunbar | a91c3e0 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 3430 | } |
| 3431 | |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 3432 | llvm::Constant *CGObjCMac::GetOptimizedPropertySetFunction(bool atomic, |
| 3433 | bool copy) { |
| 3434 | return ObjCTypes.getOptimizedSetPropertyFn(atomic, copy); |
| 3435 | } |
| 3436 | |
David Chisnall | 168b80f | 2010-12-26 22:13:16 +0000 | [diff] [blame] | 3437 | llvm::Constant *CGObjCMac::GetGetStructFunction() { |
| 3438 | return ObjCTypes.getCopyStructFn(); |
| 3439 | } |
| 3440 | llvm::Constant *CGObjCMac::GetSetStructFunction() { |
Fariborz Jahanian | 5a8c203 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 3441 | return ObjCTypes.getCopyStructFn(); |
| 3442 | } |
| 3443 | |
David Chisnall | 0d75e06 | 2012-12-17 18:54:24 +0000 | [diff] [blame] | 3444 | llvm::Constant *CGObjCMac::GetCppAtomicObjectGetFunction() { |
| 3445 | return ObjCTypes.getCppAtomicObjectFunction(); |
| 3446 | } |
| 3447 | llvm::Constant *CGObjCMac::GetCppAtomicObjectSetFunction() { |
Fariborz Jahanian | 1e1b549 | 2012-01-06 18:07:23 +0000 | [diff] [blame] | 3448 | return ObjCTypes.getCppAtomicObjectFunction(); |
| 3449 | } |
| 3450 | |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 3451 | llvm::Constant *CGObjCMac::EnumerationMutationFunction() { |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 3452 | return ObjCTypes.getEnumerationMutationFn(); |
Anders Carlsson | 3f35a26 | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 3453 | } |
| 3454 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3455 | void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) { |
| 3456 | return EmitTryOrSynchronizedStmt(CGF, S); |
| 3457 | } |
| 3458 | |
| 3459 | void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF, |
| 3460 | const ObjCAtSynchronizedStmt &S) { |
| 3461 | return EmitTryOrSynchronizedStmt(CGF, S); |
| 3462 | } |
| 3463 | |
John McCall | 65bea08 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 3464 | namespace { |
John McCall | cda666c | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 3465 | struct PerformFragileFinally : EHScopeStack::Cleanup { |
John McCall | 65bea08 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 3466 | const Stmt &S; |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3467 | llvm::Value *SyncArgSlot; |
John McCall | 65bea08 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 3468 | llvm::Value *CallTryExitVar; |
| 3469 | llvm::Value *ExceptionData; |
| 3470 | ObjCTypesHelper &ObjCTypes; |
| 3471 | PerformFragileFinally(const Stmt *S, |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3472 | llvm::Value *SyncArgSlot, |
John McCall | 65bea08 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 3473 | llvm::Value *CallTryExitVar, |
| 3474 | llvm::Value *ExceptionData, |
| 3475 | ObjCTypesHelper *ObjCTypes) |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3476 | : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar), |
John McCall | 65bea08 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 3477 | ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {} |
| 3478 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3479 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
John McCall | 65bea08 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 3480 | // Check whether we need to call objc_exception_try_exit. |
| 3481 | // In optimized code, this branch will always be folded. |
| 3482 | llvm::BasicBlock *FinallyCallExit = |
| 3483 | CGF.createBasicBlock("finally.call_exit"); |
| 3484 | llvm::BasicBlock *FinallyNoCallExit = |
| 3485 | CGF.createBasicBlock("finally.no_call_exit"); |
| 3486 | CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar), |
| 3487 | FinallyCallExit, FinallyNoCallExit); |
| 3488 | |
| 3489 | CGF.EmitBlock(FinallyCallExit); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3490 | CGF.EmitNounwindRuntimeCall(ObjCTypes.getExceptionTryExitFn(), |
| 3491 | ExceptionData); |
John McCall | 65bea08 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 3492 | |
| 3493 | CGF.EmitBlock(FinallyNoCallExit); |
| 3494 | |
| 3495 | if (isa<ObjCAtTryStmt>(S)) { |
| 3496 | if (const ObjCAtFinallyStmt* FinallyStmt = |
John McCall | cebe0ca | 2010-08-11 00:16:14 +0000 | [diff] [blame] | 3497 | cast<ObjCAtTryStmt>(S).getFinallyStmt()) { |
John McCall | 638d4f5 | 2013-04-03 00:56:07 +0000 | [diff] [blame] | 3498 | // Don't try to do the @finally if this is an EH cleanup. |
| 3499 | if (flags.isForEHCleanup()) return; |
| 3500 | |
John McCall | cebe0ca | 2010-08-11 00:16:14 +0000 | [diff] [blame] | 3501 | // Save the current cleanup destination in case there's |
| 3502 | // control flow inside the finally statement. |
| 3503 | llvm::Value *CurCleanupDest = |
| 3504 | CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot()); |
| 3505 | |
John McCall | 65bea08 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 3506 | CGF.EmitStmt(FinallyStmt->getFinallyBody()); |
| 3507 | |
John McCall | cebe0ca | 2010-08-11 00:16:14 +0000 | [diff] [blame] | 3508 | if (CGF.HaveInsertPoint()) { |
| 3509 | CGF.Builder.CreateStore(CurCleanupDest, |
| 3510 | CGF.getNormalCleanupDestSlot()); |
| 3511 | } else { |
| 3512 | // Currently, the end of the cleanup must always exist. |
| 3513 | CGF.EnsureInsertPoint(); |
| 3514 | } |
| 3515 | } |
John McCall | 65bea08 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 3516 | } else { |
| 3517 | // Emit objc_sync_exit(expr); as finally's sole statement for |
| 3518 | // @synchronized. |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3519 | llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3520 | CGF.EmitNounwindRuntimeCall(ObjCTypes.getSyncExitFn(), SyncArg); |
John McCall | 65bea08 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 3521 | } |
| 3522 | } |
| 3523 | }; |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3524 | |
| 3525 | class FragileHazards { |
| 3526 | CodeGenFunction &CGF; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3527 | SmallVector<llvm::Value*, 20> Locals; |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3528 | llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry; |
| 3529 | |
| 3530 | llvm::InlineAsm *ReadHazard; |
| 3531 | llvm::InlineAsm *WriteHazard; |
| 3532 | |
| 3533 | llvm::FunctionType *GetAsmFnType(); |
| 3534 | |
| 3535 | void collectLocals(); |
| 3536 | void emitReadHazard(CGBuilderTy &Builder); |
| 3537 | |
| 3538 | public: |
| 3539 | FragileHazards(CodeGenFunction &CGF); |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3540 | |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3541 | void emitWriteHazard(); |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3542 | void emitHazardsInNewBlocks(); |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3543 | }; |
| 3544 | } |
| 3545 | |
| 3546 | /// Create the fragile-ABI read and write hazards based on the current |
| 3547 | /// state of the function, which is presumed to be immediately prior |
| 3548 | /// to a @try block. These hazards are used to maintain correct |
| 3549 | /// semantics in the face of optimization and the fragile ABI's |
| 3550 | /// cavalier use of setjmp/longjmp. |
| 3551 | FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) { |
| 3552 | collectLocals(); |
| 3553 | |
| 3554 | if (Locals.empty()) return; |
| 3555 | |
| 3556 | // Collect all the blocks in the function. |
| 3557 | for (llvm::Function::iterator |
| 3558 | I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I) |
| 3559 | BlocksBeforeTry.insert(&*I); |
| 3560 | |
| 3561 | llvm::FunctionType *AsmFnTy = GetAsmFnType(); |
| 3562 | |
| 3563 | // Create a read hazard for the allocas. This inhibits dead-store |
| 3564 | // optimizations and forces the values to memory. This hazard is |
| 3565 | // inserted before any 'throwing' calls in the protected scope to |
| 3566 | // reflect the possibility that the variables might be read from the |
| 3567 | // catch block if the call throws. |
| 3568 | { |
| 3569 | std::string Constraint; |
| 3570 | for (unsigned I = 0, E = Locals.size(); I != E; ++I) { |
| 3571 | if (I) Constraint += ','; |
| 3572 | Constraint += "*m"; |
| 3573 | } |
| 3574 | |
| 3575 | ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false); |
| 3576 | } |
| 3577 | |
| 3578 | // Create a write hazard for the allocas. This inhibits folding |
| 3579 | // loads across the hazard. This hazard is inserted at the |
| 3580 | // beginning of the catch path to reflect the possibility that the |
| 3581 | // variables might have been written within the protected scope. |
| 3582 | { |
| 3583 | std::string Constraint; |
| 3584 | for (unsigned I = 0, E = Locals.size(); I != E; ++I) { |
| 3585 | if (I) Constraint += ','; |
| 3586 | Constraint += "=*m"; |
| 3587 | } |
| 3588 | |
| 3589 | WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false); |
| 3590 | } |
| 3591 | } |
| 3592 | |
| 3593 | /// Emit a write hazard at the current location. |
| 3594 | void FragileHazards::emitWriteHazard() { |
| 3595 | if (Locals.empty()) return; |
| 3596 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3597 | CGF.EmitNounwindRuntimeCall(WriteHazard, Locals); |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3598 | } |
| 3599 | |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3600 | void FragileHazards::emitReadHazard(CGBuilderTy &Builder) { |
| 3601 | assert(!Locals.empty()); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3602 | llvm::CallInst *call = Builder.CreateCall(ReadHazard, Locals); |
| 3603 | call->setDoesNotThrow(); |
| 3604 | call->setCallingConv(CGF.getRuntimeCC()); |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3605 | } |
| 3606 | |
| 3607 | /// Emit read hazards in all the protected blocks, i.e. all the blocks |
| 3608 | /// which have been inserted since the beginning of the try. |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3609 | void FragileHazards::emitHazardsInNewBlocks() { |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3610 | if (Locals.empty()) return; |
| 3611 | |
| 3612 | CGBuilderTy Builder(CGF.getLLVMContext()); |
| 3613 | |
| 3614 | // Iterate through all blocks, skipping those prior to the try. |
| 3615 | for (llvm::Function::iterator |
| 3616 | FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) { |
| 3617 | llvm::BasicBlock &BB = *FI; |
| 3618 | if (BlocksBeforeTry.count(&BB)) continue; |
| 3619 | |
| 3620 | // Walk through all the calls in the block. |
| 3621 | for (llvm::BasicBlock::iterator |
| 3622 | BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) { |
| 3623 | llvm::Instruction &I = *BI; |
| 3624 | |
| 3625 | // Ignore instructions that aren't non-intrinsic calls. |
| 3626 | // These are the only calls that can possibly call longjmp. |
| 3627 | if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue; |
| 3628 | if (isa<llvm::IntrinsicInst>(I)) |
| 3629 | continue; |
| 3630 | |
| 3631 | // Ignore call sites marked nounwind. This may be questionable, |
| 3632 | // since 'nounwind' doesn't necessarily mean 'does not call longjmp'. |
| 3633 | llvm::CallSite CS(&I); |
| 3634 | if (CS.doesNotThrow()) continue; |
| 3635 | |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3636 | // Insert a read hazard before the call. This will ensure that |
| 3637 | // any writes to the locals are performed before making the |
| 3638 | // call. If the call throws, then this is sufficient to |
| 3639 | // guarantee correctness as long as it doesn't also write to any |
| 3640 | // locals. |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3641 | Builder.SetInsertPoint(&BB, BI); |
| 3642 | emitReadHazard(Builder); |
| 3643 | } |
| 3644 | } |
| 3645 | } |
| 3646 | |
| 3647 | static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) { |
| 3648 | if (V) S.insert(V); |
| 3649 | } |
| 3650 | |
| 3651 | void FragileHazards::collectLocals() { |
| 3652 | // Compute a set of allocas to ignore. |
| 3653 | llvm::DenseSet<llvm::Value*> AllocasToIgnore; |
| 3654 | addIfPresent(AllocasToIgnore, CGF.ReturnValue); |
| 3655 | addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest); |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3656 | |
| 3657 | // Collect all the allocas currently in the function. This is |
| 3658 | // probably way too aggressive. |
| 3659 | llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock(); |
| 3660 | for (llvm::BasicBlock::iterator |
| 3661 | I = Entry.begin(), E = Entry.end(); I != E; ++I) |
| 3662 | if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I)) |
| 3663 | Locals.push_back(&*I); |
| 3664 | } |
| 3665 | |
| 3666 | llvm::FunctionType *FragileHazards::GetAsmFnType() { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3667 | SmallVector<llvm::Type *, 16> tys(Locals.size()); |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 3668 | for (unsigned i = 0, e = Locals.size(); i != e; ++i) |
| 3669 | tys[i] = Locals[i]->getType(); |
| 3670 | return llvm::FunctionType::get(CGF.VoidTy, tys, false); |
John McCall | 65bea08 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 3671 | } |
| 3672 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3673 | /* |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3674 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3675 | Objective-C setjmp-longjmp (sjlj) Exception Handling |
| 3676 | -- |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3677 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3678 | A catch buffer is a setjmp buffer plus: |
| 3679 | - a pointer to the exception that was caught |
| 3680 | - a pointer to the previous exception data buffer |
| 3681 | - two pointers of reserved storage |
| 3682 | Therefore catch buffers form a stack, with a pointer to the top |
| 3683 | of the stack kept in thread-local storage. |
| 3684 | |
| 3685 | objc_exception_try_enter pushes a catch buffer onto the EH stack. |
| 3686 | objc_exception_try_exit pops the given catch buffer, which is |
| 3687 | required to be the top of the EH stack. |
| 3688 | objc_exception_throw pops the top of the EH stack, writes the |
| 3689 | thrown exception into the appropriate field, and longjmps |
| 3690 | to the setjmp buffer. It crashes the process (with a printf |
| 3691 | and an abort()) if there are no catch buffers on the stack. |
| 3692 | objc_exception_extract just reads the exception pointer out of the |
| 3693 | catch buffer. |
| 3694 | |
| 3695 | There's no reason an implementation couldn't use a light-weight |
| 3696 | setjmp here --- something like __builtin_setjmp, but API-compatible |
| 3697 | with the heavyweight setjmp. This will be more important if we ever |
| 3698 | want to implement correct ObjC/C++ exception interactions for the |
| 3699 | fragile ABI. |
| 3700 | |
| 3701 | Note that for this use of setjmp/longjmp to be correct, we may need |
| 3702 | to mark some local variables volatile: if a non-volatile local |
| 3703 | variable is modified between the setjmp and the longjmp, it has |
| 3704 | indeterminate value. For the purposes of LLVM IR, it may be |
| 3705 | sufficient to make loads and stores within the @try (to variables |
| 3706 | declared outside the @try) volatile. This is necessary for |
| 3707 | optimized correctness, but is not currently being done; this is |
| 3708 | being tracked as rdar://problem/8160285 |
| 3709 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3710 | The basic framework for a @try-catch-finally is as follows: |
| 3711 | { |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3712 | objc_exception_data d; |
| 3713 | id _rethrow = null; |
Anders Carlsson | da0e456 | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 3714 | bool _call_try_exit = true; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3715 | |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3716 | objc_exception_try_enter(&d); |
| 3717 | if (!setjmp(d.jmp_buf)) { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3718 | ... try body ... |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3719 | } else { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3720 | // exception path |
| 3721 | id _caught = objc_exception_extract(&d); |
| 3722 | |
| 3723 | // enter new try scope for handlers |
| 3724 | if (!setjmp(d.jmp_buf)) { |
| 3725 | ... match exception and execute catch blocks ... |
| 3726 | |
| 3727 | // fell off end, rethrow. |
| 3728 | _rethrow = _caught; |
| 3729 | ... jump-through-finally to finally_rethrow ... |
| 3730 | } else { |
| 3731 | // exception in catch block |
| 3732 | _rethrow = objc_exception_extract(&d); |
| 3733 | _call_try_exit = false; |
| 3734 | ... jump-through-finally to finally_rethrow ... |
| 3735 | } |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3736 | } |
Daniel Dunbar | 2efd538 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 3737 | ... jump-through-finally to finally_end ... |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3738 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3739 | finally: |
Anders Carlsson | da0e456 | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 3740 | if (_call_try_exit) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3741 | objc_exception_try_exit(&d); |
Anders Carlsson | da0e456 | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 3742 | |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3743 | ... finally block .... |
Daniel Dunbar | 2efd538 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 3744 | ... dispatch to finally destination ... |
| 3745 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3746 | finally_rethrow: |
Daniel Dunbar | 2efd538 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 3747 | objc_exception_throw(_rethrow); |
| 3748 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3749 | finally_end: |
| 3750 | } |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3751 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3752 | This framework differs slightly from the one gcc uses, in that gcc |
| 3753 | uses _rethrow to determine if objc_exception_try_exit should be called |
| 3754 | and if the object should be rethrown. This breaks in the face of |
| 3755 | throwing nil and introduces unnecessary branches. |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3756 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3757 | We specialize this framework for a few particular circumstances: |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3758 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3759 | - If there are no catch blocks, then we avoid emitting the second |
| 3760 | exception handling context. |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3761 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3762 | - If there is a catch-all catch block (i.e. @catch(...) or @catch(id |
| 3763 | e)) we avoid emitting the code to rethrow an uncaught exception. |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3764 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3765 | - FIXME: If there is no @finally block we can do a few more |
| 3766 | simplifications. |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3767 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3768 | Rethrows and Jumps-Through-Finally |
| 3769 | -- |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3770 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3771 | '@throw;' is supported by pushing the currently-caught exception |
| 3772 | onto ObjCEHStack while the @catch blocks are emitted. |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3773 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3774 | Branches through the @finally block are handled with an ordinary |
| 3775 | normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC |
| 3776 | exceptions are not compatible with C++ exceptions, and this is |
| 3777 | hardly the only place where this will go wrong. |
Daniel Dunbar | 2efd538 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 3778 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3779 | @synchronized(expr) { stmt; } is emitted as if it were: |
| 3780 | id synch_value = expr; |
| 3781 | objc_sync_enter(synch_value); |
| 3782 | @try { stmt; } @finally { objc_sync_exit(synch_value); } |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3783 | */ |
| 3784 | |
Fariborz Jahanian | c2ad6dc | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 3785 | void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 3786 | const Stmt &S) { |
| 3787 | bool isTry = isa<ObjCAtTryStmt>(S); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3788 | |
| 3789 | // A destination for the fall-through edges of the catch handlers to |
| 3790 | // jump to. |
| 3791 | CodeGenFunction::JumpDest FinallyEnd = |
| 3792 | CGF.getJumpDestInCurrentScope("finally.end"); |
| 3793 | |
| 3794 | // A destination for the rethrow edge of the catch handlers to jump |
| 3795 | // to. |
| 3796 | CodeGenFunction::JumpDest FinallyRethrow = |
| 3797 | CGF.getJumpDestInCurrentScope("finally.rethrow"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3798 | |
Daniel Dunbar | 94ceb61 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 3799 | // For @synchronized, call objc_sync_enter(sync.expr). The |
| 3800 | // evaluation of the expression must occur before we enter the |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3801 | // @synchronized. We can't avoid a temp here because we need the |
| 3802 | // value to be preserved. If the backend ever does liveness |
| 3803 | // correctly after setjmp, this will be unnecessary. |
| 3804 | llvm::Value *SyncArgSlot = 0; |
Daniel Dunbar | 94ceb61 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 3805 | if (!isTry) { |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3806 | llvm::Value *SyncArg = |
Daniel Dunbar | 94ceb61 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 3807 | CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr()); |
| 3808 | SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3809 | CGF.EmitNounwindRuntimeCall(ObjCTypes.getSyncEnterFn(), SyncArg); |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3810 | |
| 3811 | SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg"); |
| 3812 | CGF.Builder.CreateStore(SyncArg, SyncArgSlot); |
Daniel Dunbar | 94ceb61 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 3813 | } |
Daniel Dunbar | 2efd538 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 3814 | |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3815 | // Allocate memory for the setjmp buffer. This needs to be kept |
| 3816 | // live throughout the try and catch blocks. |
| 3817 | llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy, |
| 3818 | "exceptiondata.ptr"); |
| 3819 | |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3820 | // Create the fragile hazards. Note that this will not capture any |
| 3821 | // of the allocas required for exception processing, but will |
| 3822 | // capture the current basic block (which extends all the way to the |
| 3823 | // setjmp call) as "before the @try". |
| 3824 | FragileHazards Hazards(CGF); |
| 3825 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3826 | // Create a flag indicating whether the cleanup needs to call |
| 3827 | // objc_exception_try_exit. This is true except when |
| 3828 | // - no catches match and we're branching through the cleanup |
| 3829 | // just to rethrow the exception, or |
| 3830 | // - a catch matched and we're falling out of the catch handler. |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3831 | // The setjmp-safety rule here is that we should always store to this |
| 3832 | // variable in a place that dominates the branch through the cleanup |
| 3833 | // without passing through any setjmps. |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3834 | llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(), |
Anders Carlsson | da0e456 | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 3835 | "_call_try_exit"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3836 | |
John McCall | 9916e3f | 2010-10-04 23:42:51 +0000 | [diff] [blame] | 3837 | // A slot containing the exception to rethrow. Only needed when we |
| 3838 | // have both a @catch and a @finally. |
| 3839 | llvm::Value *PropagatingExnVar = 0; |
| 3840 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3841 | // Push a normal cleanup to leave the try scope. |
John McCall | 638d4f5 | 2013-04-03 00:56:07 +0000 | [diff] [blame] | 3842 | CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalAndEHCleanup, &S, |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3843 | SyncArgSlot, |
John McCall | cda666c | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 3844 | CallTryExitVar, |
| 3845 | ExceptionData, |
| 3846 | &ObjCTypes); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3847 | |
| 3848 | // Enter a try block: |
| 3849 | // - Call objc_exception_try_enter to push ExceptionData on top of |
| 3850 | // the EH stack. |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3851 | CGF.EmitNounwindRuntimeCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3852 | |
| 3853 | // - Call setjmp on the exception data buffer. |
| 3854 | llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0); |
| 3855 | llvm::Value *GEPIndexes[] = { Zero, Zero, Zero }; |
| 3856 | llvm::Value *SetJmpBuffer = |
Jay Foad | 040dd82 | 2011-07-22 08:16:57 +0000 | [diff] [blame] | 3857 | CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, "setjmp_buffer"); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3858 | llvm::CallInst *SetJmpResult = |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3859 | CGF.EmitNounwindRuntimeCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result"); |
Bill Wendling | bd26cf9 | 2011-12-19 23:53:28 +0000 | [diff] [blame] | 3860 | SetJmpResult->setCanReturnTwice(); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3861 | |
| 3862 | // If setjmp returned 0, enter the protected block; otherwise, |
| 3863 | // branch to the handler. |
Daniel Dunbar | 75283ff | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 3864 | llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try"); |
| 3865 | llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler"); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3866 | llvm::Value *DidCatch = |
John McCall | cebe0ca | 2010-08-11 00:16:14 +0000 | [diff] [blame] | 3867 | CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception"); |
| 3868 | CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock); |
Anders Carlsson | 4f1c7c3 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3869 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3870 | // Emit the protected block. |
Anders Carlsson | 4f1c7c3 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3871 | CGF.EmitBlock(TryBlock); |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3872 | CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3873 | CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody() |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3874 | : cast<ObjCAtSynchronizedStmt>(S).getSynchBody()); |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3875 | |
| 3876 | CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3877 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3878 | // Emit the exception handler block. |
Daniel Dunbar | 7f08678 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 3879 | CGF.EmitBlock(TryHandler); |
Daniel Dunbar | b22ff59 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 3880 | |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3881 | // Don't optimize loads of the in-scope locals across this point. |
| 3882 | Hazards.emitWriteHazard(); |
| 3883 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3884 | // For a @synchronized (or a @try with no catches), just branch |
| 3885 | // through the cleanup to the rethrow block. |
| 3886 | if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) { |
| 3887 | // Tell the cleanup not to re-pop the exit. |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3888 | CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar); |
Anders Carlsson | bfee7e9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 3889 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3890 | |
| 3891 | // Otherwise, we have to match against the caught exceptions. |
| 3892 | } else { |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3893 | // Retrieve the exception object. We may emit multiple blocks but |
| 3894 | // nothing can cross this so the value is already in SSA form. |
| 3895 | llvm::CallInst *Caught = |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3896 | CGF.EmitNounwindRuntimeCall(ObjCTypes.getExceptionExtractFn(), |
| 3897 | ExceptionData, "caught"); |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3898 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3899 | // Push the exception to rethrow onto the EH value stack for the |
| 3900 | // benefit of any @throws in the handlers. |
| 3901 | CGF.ObjCEHValueStack.push_back(Caught); |
| 3902 | |
Douglas Gregor | 96c7949 | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 3903 | const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3904 | |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3905 | bool HasFinally = (AtTryStmt->getFinallyStmt() != 0); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3906 | |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3907 | llvm::BasicBlock *CatchBlock = 0; |
| 3908 | llvm::BasicBlock *CatchHandler = 0; |
| 3909 | if (HasFinally) { |
John McCall | 9916e3f | 2010-10-04 23:42:51 +0000 | [diff] [blame] | 3910 | // Save the currently-propagating exception before |
| 3911 | // objc_exception_try_enter clears the exception slot. |
| 3912 | PropagatingExnVar = CGF.CreateTempAlloca(Caught->getType(), |
| 3913 | "propagating_exception"); |
| 3914 | CGF.Builder.CreateStore(Caught, PropagatingExnVar); |
| 3915 | |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3916 | // Enter a new exception try block (in case a @catch block |
| 3917 | // throws an exception). |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3918 | CGF.EmitNounwindRuntimeCall(ObjCTypes.getExceptionTryEnterFn(), |
| 3919 | ExceptionData); |
Anders Carlsson | 4f1c7c3 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3920 | |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3921 | llvm::CallInst *SetJmpResult = |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3922 | CGF.EmitNounwindRuntimeCall(ObjCTypes.getSetJmpFn(), |
| 3923 | SetJmpBuffer, "setjmp.result"); |
Bill Wendling | bd26cf9 | 2011-12-19 23:53:28 +0000 | [diff] [blame] | 3924 | SetJmpResult->setCanReturnTwice(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3925 | |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3926 | llvm::Value *Threw = |
| 3927 | CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception"); |
| 3928 | |
| 3929 | CatchBlock = CGF.createBasicBlock("catch"); |
| 3930 | CatchHandler = CGF.createBasicBlock("catch_for_catch"); |
| 3931 | CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock); |
| 3932 | |
| 3933 | CGF.EmitBlock(CatchBlock); |
| 3934 | } |
| 3935 | |
| 3936 | CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3937 | |
Daniel Dunbar | b22ff59 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 3938 | // Handle catch list. As a special case we check if everything is |
| 3939 | // matched and avoid generating code for falling off the end if |
| 3940 | // so. |
| 3941 | bool AllMatched = false; |
Douglas Gregor | 96c7949 | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 3942 | for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) { |
| 3943 | const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I); |
Anders Carlsson | 4f1c7c3 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3944 | |
Douglas Gregor | 46a572b | 2010-04-26 16:46:50 +0000 | [diff] [blame] | 3945 | const VarDecl *CatchParam = CatchStmt->getCatchParamDecl(); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3946 | const ObjCObjectPointerType *OPT = 0; |
Daniel Dunbar | 523208f | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 3947 | |
Anders Carlsson | 4f1c7c3 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3948 | // catch(...) always matches. |
Daniel Dunbar | b22ff59 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 3949 | if (!CatchParam) { |
| 3950 | AllMatched = true; |
| 3951 | } else { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3952 | OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3953 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3954 | // catch(id e) always matches under this ABI, since only |
| 3955 | // ObjC exceptions end up here in the first place. |
Daniel Dunbar | 86919f4 | 2008-09-27 22:21:14 +0000 | [diff] [blame] | 3956 | // FIXME: For the time being we also match id<X>; this should |
| 3957 | // be rejected by Sema instead. |
Eli Friedman | 55179ca | 2009-07-11 00:57:02 +0000 | [diff] [blame] | 3958 | if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType())) |
Daniel Dunbar | b22ff59 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 3959 | AllMatched = true; |
Anders Carlsson | 4f1c7c3 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3960 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3961 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3962 | // 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] | 3963 | if (AllMatched) { |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3964 | CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF); |
| 3965 | |
Anders Carlsson | 9396a89 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 3966 | if (CatchParam) { |
John McCall | 1c9c3fd | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 3967 | CGF.EmitAutoVarDecl(*CatchParam); |
Daniel Dunbar | 5c7e393 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 3968 | assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3969 | |
| 3970 | // These types work out because ConvertType(id) == i8*. |
Steve Naroff | 371b8fb | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 3971 | CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam)); |
Anders Carlsson | 9396a89 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 3972 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3973 | |
Anders Carlsson | 9396a89 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 3974 | CGF.EmitStmt(CatchStmt->getCatchBody()); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3975 | |
| 3976 | // The scope of the catch variable ends right here. |
| 3977 | CatchVarCleanups.ForceCleanup(); |
| 3978 | |
Anders Carlsson | bfee7e9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 3979 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Anders Carlsson | 4f1c7c3 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3980 | break; |
| 3981 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3982 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3983 | assert(OPT && "Unexpected non-object pointer type in @catch"); |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 3984 | const ObjCObjectType *ObjTy = OPT->getObjectType(); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3985 | |
| 3986 | // FIXME: @catch (Class c) ? |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 3987 | ObjCInterfaceDecl *IDecl = ObjTy->getInterface(); |
| 3988 | assert(IDecl && "Catch parameter must have Objective-C type!"); |
Anders Carlsson | 4f1c7c3 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3989 | |
| 3990 | // Check if the @catch block matches the exception object. |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3991 | llvm::Value *Class = EmitClassRef(CGF, IDecl); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3992 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3993 | llvm::Value *matchArgs[] = { Class, Caught }; |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3994 | llvm::CallInst *Match = |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3995 | CGF.EmitNounwindRuntimeCall(ObjCTypes.getExceptionMatchFn(), |
| 3996 | matchArgs, "match"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3997 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3998 | llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match"); |
| 3999 | llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4000 | |
| 4001 | CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"), |
Daniel Dunbar | 7f08678 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 4002 | MatchedBlock, NextCatchBlock); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4003 | |
Anders Carlsson | 4f1c7c3 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 4004 | // Emit the @catch block. |
| 4005 | CGF.EmitBlock(MatchedBlock); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 4006 | |
| 4007 | // Collect any cleanups for the catch variable. The scope lasts until |
| 4008 | // the end of the catch body. |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 4009 | CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 4010 | |
John McCall | 1c9c3fd | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 4011 | CGF.EmitAutoVarDecl(*CatchParam); |
Daniel Dunbar | 5c7e393 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 4012 | assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 4013 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 4014 | // Initialize the catch variable. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4015 | llvm::Value *Tmp = |
| 4016 | CGF.Builder.CreateBitCast(Caught, |
Benjamin Kramer | 76399eb | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 4017 | CGF.ConvertType(CatchParam->getType())); |
Steve Naroff | 371b8fb | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 4018 | CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam)); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4019 | |
Anders Carlsson | 9396a89 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 4020 | CGF.EmitStmt(CatchStmt->getCatchBody()); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 4021 | |
| 4022 | // We're done with the catch variable. |
| 4023 | CatchVarCleanups.ForceCleanup(); |
| 4024 | |
Anders Carlsson | bfee7e9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 4025 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4026 | |
Anders Carlsson | 4f1c7c3 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 4027 | CGF.EmitBlock(NextCatchBlock); |
| 4028 | } |
| 4029 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 4030 | CGF.ObjCEHValueStack.pop_back(); |
| 4031 | |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 4032 | // If nothing wanted anything to do with the caught exception, |
| 4033 | // kill the extract call. |
| 4034 | if (Caught->use_empty()) |
| 4035 | Caught->eraseFromParent(); |
| 4036 | |
| 4037 | if (!AllMatched) |
| 4038 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 4039 | |
| 4040 | if (HasFinally) { |
| 4041 | // Emit the exception handler for the @catch blocks. |
| 4042 | CGF.EmitBlock(CatchHandler); |
| 4043 | |
| 4044 | // In theory we might now need a write hazard, but actually it's |
| 4045 | // unnecessary because there's no local-accessing code between |
| 4046 | // the try's write hazard and here. |
| 4047 | //Hazards.emitWriteHazard(); |
| 4048 | |
John McCall | 9916e3f | 2010-10-04 23:42:51 +0000 | [diff] [blame] | 4049 | // Extract the new exception and save it to the |
| 4050 | // propagating-exception slot. |
| 4051 | assert(PropagatingExnVar); |
| 4052 | llvm::CallInst *NewCaught = |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4053 | CGF.EmitNounwindRuntimeCall(ObjCTypes.getExceptionExtractFn(), |
| 4054 | ExceptionData, "caught"); |
John McCall | 9916e3f | 2010-10-04 23:42:51 +0000 | [diff] [blame] | 4055 | CGF.Builder.CreateStore(NewCaught, PropagatingExnVar); |
| 4056 | |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 4057 | // Don't pop the catch handler; the throw already did. |
| 4058 | CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar); |
Anders Carlsson | bfee7e9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 4059 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Daniel Dunbar | b22ff59 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 4060 | } |
Anders Carlsson | 4f1c7c3 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 4061 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4062 | |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 4063 | // Insert read hazards as required in the new blocks. |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 4064 | Hazards.emitHazardsInNewBlocks(); |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 4065 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 4066 | // Pop the cleanup. |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 4067 | CGF.Builder.restoreIP(TryFallthroughIP); |
| 4068 | if (CGF.HaveInsertPoint()) |
| 4069 | CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 4070 | CGF.PopCleanupBlock(); |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 4071 | CGF.EmitBlock(FinallyEnd.getBlock(), true); |
Anders Carlsson | bfee7e9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 4072 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 4073 | // Emit the rethrow block. |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 4074 | CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP(); |
John McCall | ad5d61e | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 4075 | CGF.EmitBlock(FinallyRethrow.getBlock(), true); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 4076 | if (CGF.HaveInsertPoint()) { |
John McCall | 9916e3f | 2010-10-04 23:42:51 +0000 | [diff] [blame] | 4077 | // If we have a propagating-exception variable, check it. |
| 4078 | llvm::Value *PropagatingExn; |
| 4079 | if (PropagatingExnVar) { |
| 4080 | PropagatingExn = CGF.Builder.CreateLoad(PropagatingExnVar); |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 4081 | |
John McCall | 9916e3f | 2010-10-04 23:42:51 +0000 | [diff] [blame] | 4082 | // Otherwise, just look in the buffer for the exception to throw. |
| 4083 | } else { |
| 4084 | llvm::CallInst *Caught = |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4085 | CGF.EmitNounwindRuntimeCall(ObjCTypes.getExceptionExtractFn(), |
| 4086 | ExceptionData); |
John McCall | 9916e3f | 2010-10-04 23:42:51 +0000 | [diff] [blame] | 4087 | PropagatingExn = Caught; |
| 4088 | } |
| 4089 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4090 | CGF.EmitNounwindRuntimeCall(ObjCTypes.getExceptionThrowFn(), |
| 4091 | PropagatingExn); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 4092 | CGF.Builder.CreateUnreachable(); |
Fariborz Jahanian | e2caaaa | 2008-11-21 19:21:53 +0000 | [diff] [blame] | 4093 | } |
Anders Carlsson | 4f1c7c3 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 4094 | |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 4095 | CGF.Builder.restoreIP(SavedIP); |
Anders Carlsson | 1963b0c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 4096 | } |
| 4097 | |
| 4098 | void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 1eab052 | 2013-01-10 19:02:56 +0000 | [diff] [blame] | 4099 | const ObjCAtThrowStmt &S, |
| 4100 | bool ClearInsertionPoint) { |
Anders Carlsson | e005aa1 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 4101 | llvm::Value *ExceptionAsObject; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4102 | |
Anders Carlsson | e005aa1 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 4103 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
John McCall | 248512a | 2011-10-01 10:32:24 +0000 | [diff] [blame] | 4104 | llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4105 | ExceptionAsObject = |
Benjamin Kramer | 76399eb | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 4106 | CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy); |
Anders Carlsson | e005aa1 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 4107 | } else { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4108 | assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) && |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 4109 | "Unexpected rethrow outside @catch block."); |
Anders Carlsson | bf8a1be | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 4110 | ExceptionAsObject = CGF.ObjCEHValueStack.back(); |
Anders Carlsson | e005aa1 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 4111 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4112 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4113 | CGF.EmitRuntimeCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject) |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 4114 | ->setDoesNotReturn(); |
Anders Carlsson | 4f1c7c3 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 4115 | CGF.Builder.CreateUnreachable(); |
Daniel Dunbar | 5c7e393 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 4116 | |
| 4117 | // Clear the insertion point to indicate we are in unreachable code. |
Fariborz Jahanian | 1eab052 | 2013-01-10 19:02:56 +0000 | [diff] [blame] | 4118 | if (ClearInsertionPoint) |
| 4119 | CGF.Builder.ClearInsertionPoint(); |
Anders Carlsson | 1963b0c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 4120 | } |
| 4121 | |
Fariborz Jahanian | 83f45b55 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 4122 | /// EmitObjCWeakRead - Code gen for loading value of a __weak |
Fariborz Jahanian | f5125d1 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 4123 | /// object: objc_read_weak (id *src) |
| 4124 | /// |
Fariborz Jahanian | 83f45b55 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 4125 | llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4126 | llvm::Value *AddrWeakObj) { |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 4127 | llvm::Type* DestTy = |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4128 | cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType(); |
| 4129 | AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, |
| 4130 | ObjCTypes.PtrObjectPtrTy); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4131 | llvm::Value *read_weak = |
| 4132 | CGF.EmitNounwindRuntimeCall(ObjCTypes.getGcReadWeakFn(), |
| 4133 | AddrWeakObj, "weakread"); |
Eli Friedman | a374b68 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 4134 | read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy); |
Fariborz Jahanian | f5125d1 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 4135 | return read_weak; |
| 4136 | } |
| 4137 | |
Fariborz Jahanian | 83f45b55 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 4138 | /// EmitObjCWeakAssign - Code gen for assigning to a __weak object. |
| 4139 | /// objc_assign_weak (id src, id *dst) |
| 4140 | /// |
| 4141 | void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4142 | llvm::Value *src, llvm::Value *dst) { |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 4143 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 4144 | if (!isa<llvm::PointerType>(SrcTy)) { |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 4145 | unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 4146 | assert(Size <= 8 && "does not support size > 8"); |
| 4147 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4148 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 1b074a3 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 4149 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 4150 | } |
Fariborz Jahanian | 50a1270 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 4151 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 4152 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4153 | llvm::Value *args[] = { src, dst }; |
| 4154 | CGF.EmitNounwindRuntimeCall(ObjCTypes.getGcAssignWeakFn(), |
| 4155 | args, "weakassign"); |
Fariborz Jahanian | 83f45b55 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 4156 | return; |
| 4157 | } |
| 4158 | |
Fariborz Jahanian | d7db964 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 4159 | /// EmitObjCGlobalAssign - Code gen for assigning to a __strong object. |
| 4160 | /// objc_assign_global (id src, id *dst) |
| 4161 | /// |
| 4162 | void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 217af24 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 4163 | llvm::Value *src, llvm::Value *dst, |
| 4164 | bool threadlocal) { |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 4165 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 4166 | if (!isa<llvm::PointerType>(SrcTy)) { |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 4167 | unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 4168 | assert(Size <= 8 && "does not support size > 8"); |
| 4169 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4170 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 1b074a3 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 4171 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 4172 | } |
Fariborz Jahanian | 50a1270 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 4173 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 4174 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4175 | llvm::Value *args[] = { src, dst }; |
Fariborz Jahanian | 217af24 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 4176 | if (!threadlocal) |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4177 | CGF.EmitNounwindRuntimeCall(ObjCTypes.getGcAssignGlobalFn(), |
| 4178 | args, "globalassign"); |
Fariborz Jahanian | 217af24 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 4179 | else |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4180 | CGF.EmitNounwindRuntimeCall(ObjCTypes.getGcAssignThreadLocalFn(), |
| 4181 | args, "threadlocalassign"); |
Fariborz Jahanian | d7db964 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 4182 | return; |
| 4183 | } |
| 4184 | |
Fariborz Jahanian | e881b53 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 4185 | /// EmitObjCIvarAssign - Code gen for assigning to a __strong object. |
Fariborz Jahanian | 7a95d72 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 4186 | /// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset) |
Fariborz Jahanian | e881b53 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 4187 | /// |
| 4188 | void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 7a95d72 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 4189 | llvm::Value *src, llvm::Value *dst, |
| 4190 | llvm::Value *ivarOffset) { |
| 4191 | assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 4192 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 4193 | if (!isa<llvm::PointerType>(SrcTy)) { |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 4194 | unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 4195 | assert(Size <= 8 && "does not support size > 8"); |
| 4196 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4197 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 1b074a3 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 4198 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 4199 | } |
Fariborz Jahanian | e881b53 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 4200 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 4201 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4202 | llvm::Value *args[] = { src, dst, ivarOffset }; |
| 4203 | CGF.EmitNounwindRuntimeCall(ObjCTypes.getGcAssignIvarFn(), args); |
Fariborz Jahanian | e881b53 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 4204 | return; |
| 4205 | } |
| 4206 | |
Fariborz Jahanian | d7db964 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 4207 | /// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object. |
| 4208 | /// objc_assign_strongCast (id src, id *dst) |
| 4209 | /// |
| 4210 | void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4211 | llvm::Value *src, llvm::Value *dst) { |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 4212 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 4213 | if (!isa<llvm::PointerType>(SrcTy)) { |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 4214 | unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 4215 | assert(Size <= 8 && "does not support size > 8"); |
| 4216 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4217 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 1b074a3 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 4218 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 4219 | } |
Fariborz Jahanian | 50a1270 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 4220 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 4221 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4222 | llvm::Value *args[] = { src, dst }; |
| 4223 | CGF.EmitNounwindRuntimeCall(ObjCTypes.getGcAssignStrongCastFn(), |
| 4224 | args, "weakassign"); |
Fariborz Jahanian | d7db964 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 4225 | return; |
| 4226 | } |
| 4227 | |
Fariborz Jahanian | 5f21d2f | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 4228 | void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4229 | llvm::Value *DestPtr, |
| 4230 | llvm::Value *SrcPtr, |
Fariborz Jahanian | 021510e | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 4231 | llvm::Value *size) { |
Fariborz Jahanian | 5f21d2f | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 4232 | SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy); |
| 4233 | DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4234 | llvm::Value *args[] = { DestPtr, SrcPtr, size }; |
| 4235 | CGF.EmitNounwindRuntimeCall(ObjCTypes.GcMemmoveCollectableFn(), args); |
Fariborz Jahanian | 5f21d2f | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 4236 | } |
| 4237 | |
Fariborz Jahanian | 9f84b78 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 4238 | /// EmitObjCValueForIvar - Code Gen for ivar reference. |
| 4239 | /// |
Fariborz Jahanian | 712bfa6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 4240 | LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 4241 | QualType ObjectTy, |
| 4242 | llvm::Value *BaseValue, |
| 4243 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 712bfa6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 4244 | unsigned CVRQualifiers) { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4245 | const ObjCInterfaceDecl *ID = |
| 4246 | ObjectTy->getAs<ObjCObjectType>()->getInterface(); |
Daniel Dunbar | 9fd114d | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 4247 | return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers, |
| 4248 | EmitIvarOffset(CGF, ID, Ivar)); |
Fariborz Jahanian | 9f84b78 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 4249 | } |
| 4250 | |
Fariborz Jahanian | 21fc74c | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 4251 | llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 722f424 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 4252 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | 21fc74c | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 4253 | const ObjCIvarDecl *Ivar) { |
Eli Friedman | 8cbca20 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4254 | uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar); |
| 4255 | return llvm::ConstantInt::get( |
| 4256 | CGM.getTypes().ConvertType(CGM.getContext().LongTy), |
| 4257 | Offset); |
Fariborz Jahanian | 21fc74c | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 4258 | } |
| 4259 | |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 4260 | /* *** Private Interface *** */ |
| 4261 | |
| 4262 | /// EmitImageInfo - Emit the image info marker used to encode some module |
| 4263 | /// level information. |
| 4264 | /// |
| 4265 | /// See: <rdr://4810609&4810587&4810587> |
| 4266 | /// struct IMAGE_INFO { |
| 4267 | /// unsigned version; |
| 4268 | /// unsigned flags; |
| 4269 | /// }; |
| 4270 | enum ImageInfoFlags { |
Fariborz Jahanian | 39c17a8 | 2014-01-14 22:01:08 +0000 | [diff] [blame] | 4271 | eImageInfo_FixAndContinue = (1 << 0), // This flag is no longer set by clang. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4272 | eImageInfo_GarbageCollected = (1 << 1), |
| 4273 | eImageInfo_GCOnly = (1 << 2), |
Fariborz Jahanian | 39c17a8 | 2014-01-14 22:01:08 +0000 | [diff] [blame] | 4274 | 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] | 4275 | |
Daniel Dunbar | 5e63927 | 2010-04-25 20:39:01 +0000 | [diff] [blame] | 4276 | // A flag indicating that the module has no instances of a @synthesize of a |
| 4277 | // superclass variable. <rdar://problem/6803242> |
Fariborz Jahanian | 39c17a8 | 2014-01-14 22:01:08 +0000 | [diff] [blame] | 4278 | eImageInfo_CorrectedSynthesize = (1 << 4), // This flag is no longer set by clang. |
Bill Wendling | 1e60a2c | 2012-04-24 11:04:57 +0000 | [diff] [blame] | 4279 | eImageInfo_ImageIsSimulated = (1 << 5) |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 4280 | }; |
| 4281 | |
Daniel Dunbar | 5e63927 | 2010-04-25 20:39:01 +0000 | [diff] [blame] | 4282 | void CGObjCCommonMac::EmitImageInfo() { |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 4283 | unsigned version = 0; // Version is unused? |
Bill Wendling | b6f795e | 2012-02-16 01:13:30 +0000 | [diff] [blame] | 4284 | const char *Section = (ObjCABI == 1) ? |
| 4285 | "__OBJC, __image_info,regular" : |
| 4286 | "__DATA, __objc_imageinfo, regular, no_dead_strip"; |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 4287 | |
Bill Wendling | b6f795e | 2012-02-16 01:13:30 +0000 | [diff] [blame] | 4288 | // Generate module-level named metadata to convey this information to the |
| 4289 | // linker and code-gen. |
| 4290 | llvm::Module &Mod = CGM.getModule(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4291 | |
Bill Wendling | b6f795e | 2012-02-16 01:13:30 +0000 | [diff] [blame] | 4292 | // Add the ObjC ABI version to the module flags. |
| 4293 | Mod.addModuleFlag(llvm::Module::Error, "Objective-C Version", ObjCABI); |
| 4294 | Mod.addModuleFlag(llvm::Module::Error, "Objective-C Image Info Version", |
| 4295 | version); |
| 4296 | Mod.addModuleFlag(llvm::Module::Error, "Objective-C Image Info Section", |
| 4297 | llvm::MDString::get(VMContext,Section)); |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 4298 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4299 | if (CGM.getLangOpts().getGC() == LangOptions::NonGC) { |
Bill Wendling | b6f795e | 2012-02-16 01:13:30 +0000 | [diff] [blame] | 4300 | // Non-GC overrides those files which specify GC. |
| 4301 | Mod.addModuleFlag(llvm::Module::Override, |
| 4302 | "Objective-C Garbage Collection", (uint32_t)0); |
| 4303 | } else { |
| 4304 | // Add the ObjC garbage collection value. |
| 4305 | Mod.addModuleFlag(llvm::Module::Error, |
| 4306 | "Objective-C Garbage Collection", |
| 4307 | eImageInfo_GarbageCollected); |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 4308 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4309 | if (CGM.getLangOpts().getGC() == LangOptions::GCOnly) { |
Bill Wendling | b6f795e | 2012-02-16 01:13:30 +0000 | [diff] [blame] | 4310 | // Add the ObjC GC Only value. |
| 4311 | Mod.addModuleFlag(llvm::Module::Error, "Objective-C GC Only", |
| 4312 | eImageInfo_GCOnly); |
| 4313 | |
| 4314 | // Require that GC be specified and set to eImageInfo_GarbageCollected. |
| 4315 | llvm::Value *Ops[2] = { |
| 4316 | llvm::MDString::get(VMContext, "Objective-C Garbage Collection"), |
| 4317 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), |
| 4318 | eImageInfo_GarbageCollected) |
| 4319 | }; |
| 4320 | Mod.addModuleFlag(llvm::Module::Require, "Objective-C GC Only", |
| 4321 | llvm::MDNode::get(VMContext, Ops)); |
| 4322 | } |
| 4323 | } |
Bill Wendling | 1e60a2c | 2012-04-24 11:04:57 +0000 | [diff] [blame] | 4324 | |
| 4325 | // Indicate whether we're compiling this to run on a simulator. |
| 4326 | const llvm::Triple &Triple = CGM.getTarget().getTriple(); |
Cameron Esfahani | 556d91e | 2013-09-14 01:09:11 +0000 | [diff] [blame] | 4327 | if (Triple.isiOS() && |
Bill Wendling | 1e60a2c | 2012-04-24 11:04:57 +0000 | [diff] [blame] | 4328 | (Triple.getArch() == llvm::Triple::x86 || |
| 4329 | Triple.getArch() == llvm::Triple::x86_64)) |
| 4330 | Mod.addModuleFlag(llvm::Module::Error, "Objective-C Is Simulated", |
| 4331 | eImageInfo_ImageIsSimulated); |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 4332 | } |
| 4333 | |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4334 | // struct objc_module { |
| 4335 | // unsigned long version; |
| 4336 | // unsigned long size; |
| 4337 | // const char *name; |
| 4338 | // Symtab symtab; |
| 4339 | // }; |
| 4340 | |
| 4341 | // FIXME: Get from somewhere |
| 4342 | static const int ModuleVersion = 7; |
| 4343 | |
| 4344 | void CGObjCMac::EmitModuleInfo() { |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 4345 | uint64_t Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ModuleTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4346 | |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 4347 | llvm::Constant *Values[] = { |
| 4348 | llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion), |
| 4349 | llvm::ConstantInt::get(ObjCTypes.LongTy, Size), |
| 4350 | // This used to be the filename, now it is unused. <rdr://4327263> |
| 4351 | GetClassName(&CGM.getContext().Idents.get("")), |
| 4352 | EmitModuleSymbols() |
| 4353 | }; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4354 | CreateMetadataVar("\01L_OBJC_MODULES", |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 4355 | llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values), |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 4356 | "__OBJC,__module_info,regular,no_dead_strip", |
Daniel Dunbar | ae33384 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 4357 | 4, true); |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4358 | } |
| 4359 | |
| 4360 | llvm::Constant *CGObjCMac::EmitModuleSymbols() { |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4361 | unsigned NumClasses = DefinedClasses.size(); |
| 4362 | unsigned NumCategories = DefinedCategories.size(); |
| 4363 | |
Daniel Dunbar | c61d0e9 | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 4364 | // Return null if no symbols were defined. |
| 4365 | if (!NumClasses && !NumCategories) |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4366 | return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy); |
Daniel Dunbar | c61d0e9 | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 4367 | |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 4368 | llvm::Constant *Values[5]; |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 4369 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4370 | Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy); |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 4371 | Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses); |
| 4372 | Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4373 | |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 4374 | // The runtime expects exactly the list of defined classes followed |
| 4375 | // by the list of defined categories, in a single array. |
Chris Lattner | 3def9ae | 2012-02-06 22:16:34 +0000 | [diff] [blame] | 4376 | SmallVector<llvm::Constant*, 8> Symbols(NumClasses + NumCategories); |
Fariborz Jahanian | f322f2f | 2014-03-11 00:25:05 +0000 | [diff] [blame] | 4377 | for (unsigned i=0; i<NumClasses; i++) { |
| 4378 | const ObjCInterfaceDecl *ID = ImplementedClasses[i]; |
| 4379 | assert(ID); |
| 4380 | if (ObjCImplementationDecl *IMP = ID->getImplementation()) |
| 4381 | // We are implementing a weak imported interface. Give it external linkage |
| 4382 | if (ID->isWeakImported() && !IMP->isWeakImported()) |
| 4383 | DefinedClasses[i]->setLinkage(llvm::GlobalVariable::ExternalLinkage); |
| 4384 | |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 4385 | Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i], |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 4386 | ObjCTypes.Int8PtrTy); |
Fariborz Jahanian | f322f2f | 2014-03-11 00:25:05 +0000 | [diff] [blame] | 4387 | } |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 4388 | for (unsigned i=0; i<NumCategories; i++) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4389 | Symbols[NumClasses + i] = |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 4390 | llvm::ConstantExpr::getBitCast(DefinedCategories[i], |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 4391 | ObjCTypes.Int8PtrTy); |
| 4392 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4393 | Values[4] = |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4394 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
Chris Lattner | 3def9ae | 2012-02-06 22:16:34 +0000 | [diff] [blame] | 4395 | Symbols.size()), |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4396 | Symbols); |
| 4397 | |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 4398 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4399 | |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4400 | llvm::GlobalVariable *GV = |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 4401 | CreateMetadataVar("\01L_OBJC_SYMBOLS", Init, |
| 4402 | "__OBJC,__symbols,regular,no_dead_strip", |
Daniel Dunbar | b25452a | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 4403 | 4, true); |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 4404 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4405 | } |
| 4406 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4407 | llvm::Value *CGObjCMac::EmitClassRefFromId(CodeGenFunction &CGF, |
| 4408 | IdentifierInfo *II) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4409 | LazySymbols.insert(II); |
| 4410 | |
| 4411 | llvm::GlobalVariable *&Entry = ClassReferences[II]; |
| 4412 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4413 | if (!Entry) { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4414 | llvm::Constant *Casted = |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4415 | llvm::ConstantExpr::getBitCast(GetClassName(II), |
| 4416 | ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4417 | Entry = |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4418 | CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted, |
| 4419 | "__OBJC,__cls_refs,literal_pointers,no_dead_strip", |
| 4420 | 4, true); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4421 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4422 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4423 | return CGF.Builder.CreateLoad(Entry); |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4424 | } |
| 4425 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4426 | llvm::Value *CGObjCMac::EmitClassRef(CodeGenFunction &CGF, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4427 | const ObjCInterfaceDecl *ID) { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4428 | return EmitClassRefFromId(CGF, ID->getIdentifier()); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4429 | } |
| 4430 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4431 | llvm::Value *CGObjCMac::EmitNSAutoreleasePoolClassRef(CodeGenFunction &CGF) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4432 | IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool"); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4433 | return EmitClassRefFromId(CGF, II); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4434 | } |
| 4435 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4436 | llvm::Value *CGObjCMac::EmitSelector(CodeGenFunction &CGF, Selector Sel, |
Fariborz Jahanian | 9240f3d | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 4437 | bool lvalue) { |
Daniel Dunbar | cb515c8 | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 4438 | llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4439 | |
Daniel Dunbar | cb515c8 | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 4440 | if (!Entry) { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4441 | llvm::Constant *Casted = |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 4442 | llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), |
Daniel Dunbar | cb515c8 | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 4443 | ObjCTypes.SelectorPtrTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4444 | Entry = |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 4445 | CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted, |
| 4446 | "__OBJC,__message_refs,literal_pointers,no_dead_strip", |
Daniel Dunbar | b25452a | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 4447 | 4, true); |
Michael Gottesman | 5c20596 | 2013-02-05 23:08:45 +0000 | [diff] [blame] | 4448 | Entry->setExternallyInitialized(true); |
Daniel Dunbar | cb515c8 | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 4449 | } |
| 4450 | |
Fariborz Jahanian | 9240f3d | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 4451 | if (lvalue) |
| 4452 | return Entry; |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4453 | return CGF.Builder.CreateLoad(Entry); |
Daniel Dunbar | cb515c8 | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 4454 | } |
| 4455 | |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4456 | llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) { |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4457 | llvm::GlobalVariable *&Entry = ClassNames[Ident]; |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4458 | |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 4459 | if (!Entry) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4460 | Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_", |
Chris Lattner | 9c81833 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 4461 | llvm::ConstantDataArray::getString(VMContext, |
| 4462 | Ident->getNameStart()), |
Daniel Dunbar | 7c9295a | 2011-03-25 20:09:09 +0000 | [diff] [blame] | 4463 | ((ObjCABI == 2) ? |
| 4464 | "__TEXT,__objc_classname,cstring_literals" : |
| 4465 | "__TEXT,__cstring,cstring_literals"), |
Daniel Dunbar | 3241fae | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 4466 | 1, true); |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4467 | |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4468 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4469 | } |
| 4470 | |
Argyrios Kyrtzidis | 13257c5 | 2010-08-09 10:54:20 +0000 | [diff] [blame] | 4471 | llvm::Function *CGObjCCommonMac::GetMethodDefinition(const ObjCMethodDecl *MD) { |
| 4472 | llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*>::iterator |
| 4473 | I = MethodDefinitions.find(MD); |
| 4474 | if (I != MethodDefinitions.end()) |
| 4475 | return I->second; |
| 4476 | |
Argyrios Kyrtzidis | 13257c5 | 2010-08-09 10:54:20 +0000 | [diff] [blame] | 4477 | return NULL; |
| 4478 | } |
| 4479 | |
Fariborz Jahanian | 01dff42 | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 4480 | /// GetIvarLayoutName - Returns a unique constant for the given |
| 4481 | /// ivar layout bitmap. |
| 4482 | llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4483 | const ObjCCommonTypesHelper &ObjCTypes) { |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4484 | return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Fariborz Jahanian | 01dff42 | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 4485 | } |
| 4486 | |
Daniel Dunbar | 15bd888 | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 4487 | void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT, |
Eli Friedman | 8cbca20 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4488 | unsigned int BytePos, |
Daniel Dunbar | 15bd888 | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 4489 | bool ForStrongLayout, |
| 4490 | bool &HasUnion) { |
| 4491 | const RecordDecl *RD = RT->getDecl(); |
| 4492 | // FIXME - Use iterator. |
Aaron Ballman | 62e47c4 | 2014-03-10 13:43:55 +0000 | [diff] [blame] | 4493 | SmallVector<const FieldDecl*, 16> Fields(RD->fields()); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 4494 | llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0)); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4495 | const llvm::StructLayout *RecLayout = |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 4496 | CGM.getDataLayout().getStructLayout(cast<llvm::StructType>(Ty)); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4497 | |
Daniel Dunbar | 15bd888 | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 4498 | BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos, |
| 4499 | ForStrongLayout, HasUnion); |
| 4500 | } |
| 4501 | |
Daniel Dunbar | 36e2a1e | 2009-05-03 21:05:10 +0000 | [diff] [blame] | 4502 | void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4503 | const llvm::StructLayout *Layout, |
| 4504 | const RecordDecl *RD, |
Bill Wendling | f1a3fca | 2012-02-22 09:30:11 +0000 | [diff] [blame] | 4505 | ArrayRef<const FieldDecl*> RecFields, |
Eli Friedman | 8cbca20 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4506 | unsigned int BytePos, bool ForStrongLayout, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4507 | bool &HasUnion) { |
Fariborz Jahanian | 3b0f886 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 4508 | bool IsUnion = (RD && RD->isUnion()); |
Eli Friedman | 8cbca20 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4509 | uint64_t MaxUnionIvarSize = 0; |
| 4510 | uint64_t MaxSkippedUnionIvarSize = 0; |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 4511 | const FieldDecl *MaxField = 0; |
| 4512 | const FieldDecl *MaxSkippedField = 0; |
| 4513 | const FieldDecl *LastFieldBitfieldOrUnnamed = 0; |
Eli Friedman | 8cbca20 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4514 | uint64_t MaxFieldOffset = 0; |
| 4515 | uint64_t MaxSkippedFieldOffset = 0; |
| 4516 | uint64_t LastBitfieldOrUnnamedOffset = 0; |
| 4517 | uint64_t FirstFieldDelta = 0; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4518 | |
Fariborz Jahanian | 524bb20 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 4519 | if (RecFields.empty()) |
| 4520 | return; |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 4521 | unsigned WordSizeInBits = CGM.getTarget().getPointerWidth(0); |
| 4522 | unsigned ByteSizeInBits = CGM.getTarget().getCharWidth(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4523 | if (!RD && CGM.getLangOpts().ObjCAutoRefCount) { |
Eli Friedman | 8cbca20 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4524 | const FieldDecl *FirstField = RecFields[0]; |
| 4525 | FirstFieldDelta = |
| 4526 | ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(FirstField)); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4527 | } |
| 4528 | |
Chris Lattner | 5b36ddb | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 4529 | for (unsigned i = 0, e = RecFields.size(); i != e; ++i) { |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 4530 | const FieldDecl *Field = RecFields[i]; |
Eli Friedman | 8cbca20 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4531 | uint64_t FieldOffset; |
Anders Carlsson | e2c6baf | 2009-07-24 17:23:54 +0000 | [diff] [blame] | 4532 | if (RD) { |
Daniel Dunbar | d51c1a6 | 2010-04-14 17:02:21 +0000 | [diff] [blame] | 4533 | // Note that 'i' here is actually the field index inside RD of Field, |
| 4534 | // although this dependency is hidden. |
| 4535 | const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD); |
Eli Friedman | 8cbca20 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4536 | FieldOffset = (RL.getFieldOffset(i) / ByteSizeInBits) - FirstFieldDelta; |
Anders Carlsson | e2c6baf | 2009-07-24 17:23:54 +0000 | [diff] [blame] | 4537 | } else |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4538 | FieldOffset = |
| 4539 | ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field)) - FirstFieldDelta; |
Daniel Dunbar | 94f46dc | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 4540 | |
Fariborz Jahanian | 524bb20 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 4541 | // Skip over unnamed or bitfields |
Fariborz Jahanian | f5fec02 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 4542 | if (!Field->getIdentifier() || Field->isBitField()) { |
Argyrios Kyrtzidis | 2fdb5b5 | 2010-09-06 12:00:10 +0000 | [diff] [blame] | 4543 | LastFieldBitfieldOrUnnamed = Field; |
| 4544 | LastBitfieldOrUnnamedOffset = FieldOffset; |
Fariborz Jahanian | 524bb20 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 4545 | continue; |
Fariborz Jahanian | f5fec02 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 4546 | } |
Daniel Dunbar | 94f46dc | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 4547 | |
Argyrios Kyrtzidis | 2fdb5b5 | 2010-09-06 12:00:10 +0000 | [diff] [blame] | 4548 | LastFieldBitfieldOrUnnamed = 0; |
Fariborz Jahanian | 524bb20 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 4549 | QualType FQT = Field->getType(); |
Fariborz Jahanian | f909f92 | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 4550 | if (FQT->isRecordType() || FQT->isUnionType()) { |
Fariborz Jahanian | 524bb20 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 4551 | if (FQT->isUnionType()) |
| 4552 | HasUnion = true; |
Fariborz Jahanian | 3b0f886 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 4553 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4554 | BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(), |
Daniel Dunbar | 94f46dc | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 4555 | BytePos + FieldOffset, |
Daniel Dunbar | 15bd888 | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 4556 | ForStrongLayout, HasUnion); |
Fariborz Jahanian | 524bb20 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 4557 | continue; |
| 4558 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4559 | |
Chris Lattner | 5b36ddb | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 4560 | if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4561 | const ConstantArrayType *CArray = |
Daniel Dunbar | 7abf83c | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 4562 | dyn_cast_or_null<ConstantArrayType>(Array); |
Fariborz Jahanian | f5fec02 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 4563 | uint64_t ElCount = CArray->getSize().getZExtValue(); |
Daniel Dunbar | 7abf83c | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 4564 | assert(CArray && "only array with known element size is supported"); |
Fariborz Jahanian | 3b0f886 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 4565 | FQT = CArray->getElementType(); |
Fariborz Jahanian | f909f92 | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 4566 | while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { |
| 4567 | const ConstantArrayType *CArray = |
Daniel Dunbar | 7abf83c | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 4568 | dyn_cast_or_null<ConstantArrayType>(Array); |
Fariborz Jahanian | f5fec02 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 4569 | ElCount *= CArray->getSize().getZExtValue(); |
Fariborz Jahanian | f909f92 | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 4570 | FQT = CArray->getElementType(); |
| 4571 | } |
Fariborz Jahanian | 9a7d57d | 2011-01-03 19:23:18 +0000 | [diff] [blame] | 4572 | if (FQT->isRecordType() && ElCount) { |
Fariborz Jahanian | a123b64 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 4573 | int OldIndex = IvarsInfo.size() - 1; |
| 4574 | int OldSkIndex = SkipIvars.size() -1; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4575 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4576 | const RecordType *RT = FQT->getAs<RecordType>(); |
Daniel Dunbar | 94f46dc | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 4577 | BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset, |
Daniel Dunbar | 15bd888 | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 4578 | ForStrongLayout, HasUnion); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4579 | |
Fariborz Jahanian | 3b0f886 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 4580 | // Replicate layout information for each array element. Note that |
| 4581 | // one element is already done. |
| 4582 | uint64_t ElIx = 1; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4583 | for (int FirstIndex = IvarsInfo.size() - 1, |
| 4584 | FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) { |
Eli Friedman | 8cbca20 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4585 | uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits; |
Daniel Dunbar | 7b89ace | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 4586 | for (int i = OldIndex+1; i <= FirstIndex; ++i) |
| 4587 | IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx, |
| 4588 | IvarsInfo[i].ivar_size)); |
| 4589 | for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i) |
| 4590 | SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx, |
| 4591 | SkipIvars[i].ivar_size)); |
Fariborz Jahanian | 3b0f886 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 4592 | } |
| 4593 | continue; |
| 4594 | } |
Fariborz Jahanian | 524bb20 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 4595 | } |
Fariborz Jahanian | 3b0f886 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 4596 | // At this point, we are done with Record/Union and array there of. |
| 4597 | // For other arrays we are down to its element type. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4598 | Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT); |
Daniel Dunbar | 7abf83c | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 4599 | |
Eli Friedman | 8cbca20 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4600 | unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType()); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4601 | if ((ForStrongLayout && GCAttr == Qualifiers::Strong) |
| 4602 | || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) { |
Daniel Dunbar | 22007d3 | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 4603 | if (IsUnion) { |
Eli Friedman | 8cbca20 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4604 | uint64_t UnionIvarSize = FieldSize / WordSizeInBits; |
Daniel Dunbar | 22007d3 | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 4605 | if (UnionIvarSize > MaxUnionIvarSize) { |
Fariborz Jahanian | 3b0f886 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 4606 | MaxUnionIvarSize = UnionIvarSize; |
| 4607 | MaxField = Field; |
Daniel Dunbar | 5b74391 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 4608 | MaxFieldOffset = FieldOffset; |
Fariborz Jahanian | 3b0f886 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 4609 | } |
Daniel Dunbar | 22007d3 | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 4610 | } else { |
Eli Friedman | 8cbca20 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4611 | IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset, |
| 4612 | FieldSize / WordSizeInBits)); |
Fariborz Jahanian | 3b0f886 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 4613 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4614 | } else if ((ForStrongLayout && |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4615 | (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)) |
| 4616 | || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) { |
Daniel Dunbar | 22007d3 | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 4617 | if (IsUnion) { |
Eli Friedman | 8cbca20 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4618 | // FIXME: Why the asymmetry? We divide by word size in bits on other |
| 4619 | // side. |
| 4620 | uint64_t UnionIvarSize = FieldSize / ByteSizeInBits; |
Daniel Dunbar | 22007d3 | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 4621 | if (UnionIvarSize > MaxSkippedUnionIvarSize) { |
Fariborz Jahanian | 3b0f886 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 4622 | MaxSkippedUnionIvarSize = UnionIvarSize; |
| 4623 | MaxSkippedField = Field; |
Daniel Dunbar | 5b74391 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 4624 | MaxSkippedFieldOffset = FieldOffset; |
Fariborz Jahanian | 3b0f886 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 4625 | } |
Daniel Dunbar | 22007d3 | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 4626 | } else { |
Eli Friedman | 8cbca20 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4627 | // FIXME: Why the asymmetry, we divide by byte size in bits here? |
| 4628 | SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset, |
| 4629 | FieldSize / ByteSizeInBits)); |
Fariborz Jahanian | 3b0f886 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 4630 | } |
| 4631 | } |
| 4632 | } |
Daniel Dunbar | 15bd888 | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 4633 | |
Argyrios Kyrtzidis | 2fdb5b5 | 2010-09-06 12:00:10 +0000 | [diff] [blame] | 4634 | if (LastFieldBitfieldOrUnnamed) { |
| 4635 | if (LastFieldBitfieldOrUnnamed->isBitField()) { |
| 4636 | // Last field was a bitfield. Must update skip info. |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4637 | uint64_t BitFieldSize |
| 4638 | = LastFieldBitfieldOrUnnamed->getBitWidthValue(CGM.getContext()); |
Argyrios Kyrtzidis | 2fdb5b5 | 2010-09-06 12:00:10 +0000 | [diff] [blame] | 4639 | GC_IVAR skivar; |
| 4640 | skivar.ivar_bytepos = BytePos + LastBitfieldOrUnnamedOffset; |
Eli Friedman | 8cbca20 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4641 | skivar.ivar_size = (BitFieldSize / ByteSizeInBits) |
| 4642 | + ((BitFieldSize % ByteSizeInBits) != 0); |
Argyrios Kyrtzidis | 2fdb5b5 | 2010-09-06 12:00:10 +0000 | [diff] [blame] | 4643 | SkipIvars.push_back(skivar); |
| 4644 | } else { |
| 4645 | assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed"); |
| 4646 | // Last field was unnamed. Must update skip info. |
Eli Friedman | 8cbca20 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4647 | unsigned FieldSize |
| 4648 | = CGM.getContext().getTypeSize(LastFieldBitfieldOrUnnamed->getType()); |
Argyrios Kyrtzidis | 2fdb5b5 | 2010-09-06 12:00:10 +0000 | [diff] [blame] | 4649 | SkipIvars.push_back(GC_IVAR(BytePos + LastBitfieldOrUnnamedOffset, |
Eli Friedman | 8cbca20 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4650 | FieldSize / ByteSizeInBits)); |
Argyrios Kyrtzidis | 2fdb5b5 | 2010-09-06 12:00:10 +0000 | [diff] [blame] | 4651 | } |
Fariborz Jahanian | f5fec02 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 4652 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4653 | |
Daniel Dunbar | 7b89ace | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 4654 | if (MaxField) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4655 | IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset, |
Daniel Dunbar | 7b89ace | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 4656 | MaxUnionIvarSize)); |
| 4657 | if (MaxSkippedField) |
Daniel Dunbar | 5b74391 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 4658 | SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset, |
Daniel Dunbar | 7b89ace | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 4659 | MaxSkippedUnionIvarSize)); |
Fariborz Jahanian | c559f3f | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 4660 | } |
| 4661 | |
Fariborz Jahanian | 1f78a9a | 2010-08-05 00:19:48 +0000 | [diff] [blame] | 4662 | /// BuildIvarLayoutBitmap - This routine is the horsework for doing all |
| 4663 | /// the computations and returning the layout bitmap (for ivar or blocks) in |
| 4664 | /// the given argument BitMap string container. Routine reads |
| 4665 | /// two containers, IvarsInfo and SkipIvars which are assumed to be |
| 4666 | /// filled already by the caller. |
Chris Lattner | 9c81833 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 4667 | llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(std::string &BitMap) { |
Eli Friedman | 8cbca20 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4668 | unsigned int WordsToScan, WordsToSkip; |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4669 | llvm::Type *PtrTy = CGM.Int8PtrTy; |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4670 | |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4671 | // Build the string of skip/scan nibbles |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4672 | SmallVector<SKIP_SCAN, 32> SkipScanIvars; |
Eli Friedman | 8cbca20 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4673 | unsigned int WordSize = |
| 4674 | CGM.getTypes().getDataLayout().getTypeAllocSize(PtrTy); |
| 4675 | if (IvarsInfo[0].ivar_bytepos == 0) { |
| 4676 | WordsToSkip = 0; |
| 4677 | WordsToScan = IvarsInfo[0].ivar_size; |
| 4678 | } else { |
| 4679 | WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize; |
| 4680 | WordsToScan = IvarsInfo[0].ivar_size; |
| 4681 | } |
Daniel Dunbar | d22aa4a | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4682 | for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) { |
Eli Friedman | 8cbca20 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4683 | unsigned int TailPrevGCObjC = |
| 4684 | IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize; |
Daniel Dunbar | d22aa4a | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4685 | if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) { |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4686 | // consecutive 'scanned' object pointers. |
Eli Friedman | 8cbca20 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4687 | WordsToScan += IvarsInfo[i].ivar_size; |
Daniel Dunbar | d22aa4a | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4688 | } else { |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4689 | // Skip over 'gc'able object pointer which lay over each other. |
| 4690 | if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos) |
| 4691 | continue; |
| 4692 | // Must skip over 1 or more words. We save current skip/scan values |
| 4693 | // and start a new pair. |
Fariborz Jahanian | 1bf7288 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 4694 | SKIP_SCAN SkScan; |
| 4695 | SkScan.skip = WordsToSkip; |
| 4696 | SkScan.scan = WordsToScan; |
Fariborz Jahanian | a123b64 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 4697 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4698 | |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4699 | // Skip the hole. |
Fariborz Jahanian | 1bf7288 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 4700 | SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize; |
| 4701 | SkScan.scan = 0; |
Fariborz Jahanian | a123b64 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 4702 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4703 | WordsToSkip = 0; |
Eli Friedman | 8cbca20 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4704 | WordsToScan = IvarsInfo[i].ivar_size; |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4705 | } |
| 4706 | } |
Daniel Dunbar | d22aa4a | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4707 | if (WordsToScan > 0) { |
Fariborz Jahanian | 1bf7288 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 4708 | SKIP_SCAN SkScan; |
| 4709 | SkScan.skip = WordsToSkip; |
| 4710 | SkScan.scan = WordsToScan; |
Fariborz Jahanian | a123b64 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 4711 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4712 | } |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4713 | |
Daniel Dunbar | d22aa4a | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4714 | if (!SkipIvars.empty()) { |
Fariborz Jahanian | a123b64 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 4715 | unsigned int LastIndex = SkipIvars.size()-1; |
Eli Friedman | 8cbca20 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4716 | int LastByteSkipped = |
| 4717 | SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size; |
Fariborz Jahanian | a123b64 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 4718 | LastIndex = IvarsInfo.size()-1; |
Eli Friedman | 8cbca20 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4719 | int LastByteScanned = |
| 4720 | IvarsInfo[LastIndex].ivar_bytepos + |
| 4721 | IvarsInfo[LastIndex].ivar_size * WordSize; |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4722 | // Compute number of bytes to skip at the tail end of the last ivar scanned. |
Benjamin Kramer | d20ef75 | 2009-12-25 15:43:36 +0000 | [diff] [blame] | 4723 | if (LastByteSkipped > LastByteScanned) { |
Eli Friedman | 8cbca20 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4724 | unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize; |
Fariborz Jahanian | 1bf7288 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 4725 | SKIP_SCAN SkScan; |
Eli Friedman | 8cbca20 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4726 | SkScan.skip = TotalWords - (LastByteScanned/WordSize); |
Fariborz Jahanian | 1bf7288 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 4727 | SkScan.scan = 0; |
Fariborz Jahanian | a123b64 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 4728 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4729 | } |
| 4730 | } |
| 4731 | // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced |
| 4732 | // as 0xMN. |
Fariborz Jahanian | a123b64 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 4733 | int SkipScan = SkipScanIvars.size()-1; |
Daniel Dunbar | d22aa4a | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4734 | for (int i = 0; i <= SkipScan; i++) { |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4735 | if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0 |
| 4736 | && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) { |
| 4737 | // 0xM0 followed by 0x0N detected. |
| 4738 | SkipScanIvars[i].scan = SkipScanIvars[i+1].scan; |
| 4739 | for (int j = i+1; j < SkipScan; j++) |
| 4740 | SkipScanIvars[j] = SkipScanIvars[j+1]; |
| 4741 | --SkipScan; |
| 4742 | } |
| 4743 | } |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4744 | |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4745 | // Generate the string. |
Daniel Dunbar | d22aa4a | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4746 | for (int i = 0; i <= SkipScan; i++) { |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4747 | unsigned char byte; |
| 4748 | unsigned int skip_small = SkipScanIvars[i].skip % 0xf; |
| 4749 | unsigned int scan_small = SkipScanIvars[i].scan % 0xf; |
| 4750 | unsigned int skip_big = SkipScanIvars[i].skip / 0xf; |
| 4751 | unsigned int scan_big = SkipScanIvars[i].scan / 0xf; |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4752 | |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4753 | // first skip big. |
| 4754 | for (unsigned int ix = 0; ix < skip_big; ix++) |
| 4755 | BitMap += (unsigned char)(0xf0); |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4756 | |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4757 | // next (skip small, scan) |
Daniel Dunbar | d22aa4a | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4758 | if (skip_small) { |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4759 | byte = skip_small << 4; |
Daniel Dunbar | d22aa4a | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4760 | if (scan_big > 0) { |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4761 | byte |= 0xf; |
| 4762 | --scan_big; |
Daniel Dunbar | d22aa4a | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4763 | } else if (scan_small) { |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4764 | byte |= scan_small; |
| 4765 | scan_small = 0; |
| 4766 | } |
| 4767 | BitMap += byte; |
| 4768 | } |
| 4769 | // next scan big |
| 4770 | for (unsigned int ix = 0; ix < scan_big; ix++) |
| 4771 | BitMap += (unsigned char)(0x0f); |
| 4772 | // last scan small |
Daniel Dunbar | d22aa4a | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4773 | if (scan_small) { |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4774 | byte = scan_small; |
| 4775 | BitMap += byte; |
| 4776 | } |
| 4777 | } |
| 4778 | // null terminate string. |
Fariborz Jahanian | f909f92 | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 4779 | unsigned char zero = 0; |
| 4780 | BitMap += zero; |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4781 | |
| 4782 | llvm::GlobalVariable * Entry = |
| 4783 | CreateMetadataVar("\01L_OBJC_CLASS_NAME_", |
Chris Lattner | 9c81833 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 4784 | llvm::ConstantDataArray::getString(VMContext, BitMap,false), |
Daniel Dunbar | 7c9295a | 2011-03-25 20:09:09 +0000 | [diff] [blame] | 4785 | ((ObjCABI == 2) ? |
| 4786 | "__TEXT,__objc_classname,cstring_literals" : |
| 4787 | "__TEXT,__cstring,cstring_literals"), |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4788 | 1, true); |
| 4789 | return getConstantGEP(VMContext, Entry, 0, 0); |
| 4790 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4791 | |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4792 | /// BuildIvarLayout - Builds ivar layout bitmap for the class |
| 4793 | /// implementation for the __strong or __weak case. |
| 4794 | /// The layout map displays which words in ivar list must be skipped |
| 4795 | /// and which must be scanned by GC (see below). String is built of bytes. |
| 4796 | /// Each byte is divided up in two nibbles (4-bit each). Left nibble is count |
| 4797 | /// of words to skip and right nibble is count of words to scan. So, each |
| 4798 | /// nibble represents up to 15 workds to skip or scan. Skipping the rest is |
| 4799 | /// represented by a 0x00 byte which also ends the string. |
| 4800 | /// 1. when ForStrongLayout is true, following ivars are scanned: |
| 4801 | /// - id, Class |
| 4802 | /// - object * |
| 4803 | /// - __strong anything |
| 4804 | /// |
| 4805 | /// 2. When ForStrongLayout is false, following ivars are scanned: |
| 4806 | /// - __weak anything |
| 4807 | /// |
| 4808 | llvm::Constant *CGObjCCommonMac::BuildIvarLayout( |
| 4809 | const ObjCImplementationDecl *OMD, |
| 4810 | bool ForStrongLayout) { |
| 4811 | bool hasUnion = false; |
| 4812 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4813 | llvm::Type *PtrTy = CGM.Int8PtrTy; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4814 | if (CGM.getLangOpts().getGC() == LangOptions::NonGC && |
| 4815 | !CGM.getLangOpts().ObjCAutoRefCount) |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4816 | return llvm::Constant::getNullValue(PtrTy); |
| 4817 | |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 4818 | const ObjCInterfaceDecl *OI = OMD->getClassInterface(); |
| 4819 | SmallVector<const FieldDecl*, 32> RecFields; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4820 | if (CGM.getLangOpts().ObjCAutoRefCount) { |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 4821 | for (const ObjCIvarDecl *IVD = OI->all_declared_ivar_begin(); |
Fariborz Jahanian | b26d578 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 4822 | IVD; IVD = IVD->getNextIvar()) |
| 4823 | RecFields.push_back(cast<FieldDecl>(IVD)); |
| 4824 | } |
| 4825 | else { |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 4826 | SmallVector<const ObjCIvarDecl*, 32> Ivars; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4827 | CGM.getContext().DeepCollectObjCIvars(OI, true, Ivars); |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4828 | |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 4829 | // FIXME: This is not ideal; we shouldn't have to do this copy. |
| 4830 | RecFields.append(Ivars.begin(), Ivars.end()); |
Fariborz Jahanian | b26d578 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 4831 | } |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4832 | |
| 4833 | if (RecFields.empty()) |
| 4834 | return llvm::Constant::getNullValue(PtrTy); |
| 4835 | |
| 4836 | SkipIvars.clear(); |
| 4837 | IvarsInfo.clear(); |
| 4838 | |
Eli Friedman | 8cbca20 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4839 | BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion); |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4840 | if (IvarsInfo.empty()) |
| 4841 | return llvm::Constant::getNullValue(PtrTy); |
Fariborz Jahanian | 1f78a9a | 2010-08-05 00:19:48 +0000 | [diff] [blame] | 4842 | // Sort on byte position in case we encounterred a union nested in |
| 4843 | // the ivar list. |
| 4844 | if (hasUnion && !IvarsInfo.empty()) |
| 4845 | std::sort(IvarsInfo.begin(), IvarsInfo.end()); |
| 4846 | if (hasUnion && !SkipIvars.empty()) |
| 4847 | std::sort(SkipIvars.begin(), SkipIvars.end()); |
| 4848 | |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4849 | std::string BitMap; |
Fariborz Jahanian | 1f78a9a | 2010-08-05 00:19:48 +0000 | [diff] [blame] | 4850 | llvm::Constant *C = BuildIvarLayoutBitmap(BitMap); |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4851 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4852 | if (CGM.getLangOpts().ObjCGCBitmapPrint) { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4853 | printf("\n%s ivar layout for class '%s': ", |
Fariborz Jahanian | 80c9ce2 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 4854 | ForStrongLayout ? "strong" : "weak", |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4855 | OMD->getClassInterface()->getName().data()); |
Roman Divacky | e637711 | 2012-09-06 15:59:27 +0000 | [diff] [blame] | 4856 | const unsigned char *s = (const unsigned char*)BitMap.c_str(); |
Bill Wendling | 5313685 | 2012-02-07 09:06:01 +0000 | [diff] [blame] | 4857 | for (unsigned i = 0, e = BitMap.size(); i < e; i++) |
Fariborz Jahanian | 80c9ce2 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 4858 | if (!(s[i] & 0xf0)) |
| 4859 | printf("0x0%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 4860 | else |
| 4861 | printf("0x%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 4862 | printf("\n"); |
| 4863 | } |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4864 | return C; |
Fariborz Jahanian | c559f3f | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 4865 | } |
| 4866 | |
Fariborz Jahanian | 0b1ccdc | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 4867 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) { |
Daniel Dunbar | cb515c8 | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 4868 | llvm::GlobalVariable *&Entry = MethodVarNames[Sel]; |
| 4869 | |
Chris Lattner | 3def9ae | 2012-02-06 22:16:34 +0000 | [diff] [blame] | 4870 | // FIXME: Avoid std::string in "Sel.getAsString()" |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 4871 | if (!Entry) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4872 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_", |
Chris Lattner | 9c81833 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 4873 | llvm::ConstantDataArray::getString(VMContext, Sel.getAsString()), |
Daniel Dunbar | 7c9295a | 2011-03-25 20:09:09 +0000 | [diff] [blame] | 4874 | ((ObjCABI == 2) ? |
| 4875 | "__TEXT,__objc_methname,cstring_literals" : |
| 4876 | "__TEXT,__cstring,cstring_literals"), |
Daniel Dunbar | 3241fae | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 4877 | 1, true); |
Daniel Dunbar | cb515c8 | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 4878 | |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4879 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4880 | } |
| 4881 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4882 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 0b1ccdc | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 4883 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) { |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4884 | return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID)); |
| 4885 | } |
| 4886 | |
Daniel Dunbar | f5c1846 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 4887 | llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) { |
Devang Patel | 4b6e4bb | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 4888 | std::string TypeStr; |
| 4889 | CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field); |
| 4890 | |
| 4891 | llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr]; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4892 | |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 4893 | if (!Entry) |
| 4894 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_", |
Chris Lattner | 9c81833 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 4895 | llvm::ConstantDataArray::getString(VMContext, TypeStr), |
Daniel Dunbar | 7c9295a | 2011-03-25 20:09:09 +0000 | [diff] [blame] | 4896 | ((ObjCABI == 2) ? |
| 4897 | "__TEXT,__objc_methtype,cstring_literals" : |
| 4898 | "__TEXT,__cstring,cstring_literals"), |
Daniel Dunbar | 3241fae | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 4899 | 1, true); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4900 | |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4901 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | cb515c8 | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 4902 | } |
| 4903 | |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4904 | llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D, |
| 4905 | bool Extended) { |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4906 | std::string TypeStr; |
Bill Wendling | e22bef7 | 2012-02-09 22:45:21 +0000 | [diff] [blame] | 4907 | if (CGM.getContext().getObjCEncodingForMethodDecl(D, TypeStr, Extended)) |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4908 | return 0; |
Devang Patel | 4b6e4bb | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 4909 | |
| 4910 | llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr]; |
| 4911 | |
Daniel Dunbar | 3241fae | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 4912 | if (!Entry) |
| 4913 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_", |
Chris Lattner | 9c81833 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 4914 | llvm::ConstantDataArray::getString(VMContext, TypeStr), |
Daniel Dunbar | 7c9295a | 2011-03-25 20:09:09 +0000 | [diff] [blame] | 4915 | ((ObjCABI == 2) ? |
| 4916 | "__TEXT,__objc_methtype,cstring_literals" : |
| 4917 | "__TEXT,__cstring,cstring_literals"), |
Daniel Dunbar | 3241fae | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 4918 | 1, true); |
Devang Patel | 4b6e4bb | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 4919 | |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4920 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4921 | } |
| 4922 | |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 4923 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 0b1ccdc | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 4924 | llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) { |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 4925 | llvm::GlobalVariable *&Entry = PropertyNames[Ident]; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4926 | |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 4927 | if (!Entry) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4928 | Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_", |
Chris Lattner | 9c81833 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 4929 | llvm::ConstantDataArray::getString(VMContext, |
| 4930 | Ident->getNameStart()), |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 4931 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | 3241fae | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 4932 | 1, true); |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 4933 | |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4934 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 4935 | } |
| 4936 | |
| 4937 | // FIXME: Merge into a single cstring creation function. |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4938 | // FIXME: This Decl should be more precise. |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 4939 | llvm::Constant * |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4940 | CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD, |
| 4941 | const Decl *Container) { |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4942 | std::string TypeStr; |
| 4943 | CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr); |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 4944 | return GetPropertyName(&CGM.getContext().Idents.get(TypeStr)); |
| 4945 | } |
| 4946 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4947 | void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D, |
Fariborz Jahanian | 0b1ccdc | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 4948 | const ObjCContainerDecl *CD, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4949 | SmallVectorImpl<char> &Name) { |
Daniel Dunbar | d238681 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 4950 | llvm::raw_svector_ostream OS(Name); |
Fariborz Jahanian | 0196a1c | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 4951 | assert (CD && "Missing container decl in GetNameForMethod"); |
Daniel Dunbar | d238681 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 4952 | OS << '\01' << (D->isInstanceMethod() ? '-' : '+') |
| 4953 | << '[' << CD->getName(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4954 | if (const ObjCCategoryImplDecl *CID = |
Daniel Dunbar | d238681 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 4955 | dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext())) |
Benjamin Kramer | 2f56992 | 2012-02-07 11:57:45 +0000 | [diff] [blame] | 4956 | OS << '(' << *CID << ')'; |
Daniel Dunbar | d238681 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 4957 | OS << ' ' << D->getSelector().getAsString() << ']'; |
Daniel Dunbar | a94ecd2 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 4958 | } |
| 4959 | |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 4960 | void CGObjCMac::FinishModule() { |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4961 | EmitModuleInfo(); |
| 4962 | |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 4963 | // Emit the dummy bodies for any protocols which were referenced but |
| 4964 | // never defined. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4965 | for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4966 | I = Protocols.begin(), e = Protocols.end(); I != e; ++I) { |
| 4967 | if (I->second->hasInitializer()) |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 4968 | continue; |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4969 | |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 4970 | llvm::Constant *Values[5]; |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4971 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4972 | Values[1] = GetClassName(I->first); |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4973 | Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 4974 | Values[3] = Values[4] = |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4975 | llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy); |
Rafael Espindola | 21039aa | 2014-02-27 16:26:32 +0000 | [diff] [blame] | 4976 | assertPrivateName(I->second); |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 4977 | I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 4978 | Values)); |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 4979 | CGM.addCompilerUsedGlobal(I->second); |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 4980 | } |
| 4981 | |
Daniel Dunbar | c61d0e9 | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 4982 | // Add assembler directives to add lazy undefined symbol references |
| 4983 | // for classes which are referenced but not defined. This is |
| 4984 | // important for correct linker interaction. |
Daniel Dunbar | d027a92 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 4985 | // |
| 4986 | // FIXME: It would be nice if we had an LLVM construct for this. |
| 4987 | if (!LazySymbols.empty() || !DefinedSymbols.empty()) { |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 4988 | SmallString<256> Asm; |
Daniel Dunbar | d027a92 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 4989 | Asm += CGM.getModule().getModuleInlineAsm(); |
| 4990 | if (!Asm.empty() && Asm.back() != '\n') |
| 4991 | Asm += '\n'; |
Daniel Dunbar | c61d0e9 | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 4992 | |
Daniel Dunbar | d027a92 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 4993 | llvm::raw_svector_ostream OS(Asm); |
Daniel Dunbar | d027a92 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 4994 | for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(), |
| 4995 | e = DefinedSymbols.end(); I != e; ++I) |
Daniel Dunbar | 07d0785 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 4996 | OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n" |
| 4997 | << "\t.globl .objc_class_name_" << (*I)->getName() << "\n"; |
Chris Lattner | a299f2c | 2010-04-17 18:26:20 +0000 | [diff] [blame] | 4998 | for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(), |
Fariborz Jahanian | 9adb2e6 | 2010-06-21 22:05:18 +0000 | [diff] [blame] | 4999 | e = LazySymbols.end(); I != e; ++I) { |
Chris Lattner | a299f2c | 2010-04-17 18:26:20 +0000 | [diff] [blame] | 5000 | OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n"; |
Fariborz Jahanian | 9adb2e6 | 2010-06-21 22:05:18 +0000 | [diff] [blame] | 5001 | } |
| 5002 | |
Bill Wendling | 5313685 | 2012-02-07 09:06:01 +0000 | [diff] [blame] | 5003 | for (size_t i = 0, e = DefinedCategoryNames.size(); i < e; ++i) { |
Fariborz Jahanian | 9adb2e6 | 2010-06-21 22:05:18 +0000 | [diff] [blame] | 5004 | OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n" |
| 5005 | << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n"; |
| 5006 | } |
Chris Lattner | a299f2c | 2010-04-17 18:26:20 +0000 | [diff] [blame] | 5007 | |
Daniel Dunbar | d027a92 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 5008 | CGM.getModule().setModuleInlineAsm(OS.str()); |
Daniel Dunbar | c61d0e9 | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 5009 | } |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 5010 | } |
| 5011 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5012 | CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm) |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5013 | : CGObjCCommonMac(cgm), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5014 | ObjCTypes(cgm) { |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5015 | ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL; |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5016 | ObjCABI = 2; |
| 5017 | } |
| 5018 | |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 5019 | /* *** */ |
| 5020 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5021 | ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm) |
Douglas Gregor | a95f9aa | 2012-01-17 23:38:32 +0000 | [diff] [blame] | 5022 | : VMContext(cgm.getLLVMContext()), CGM(cgm), ExternalProtocolPtrTy(0) |
| 5023 | { |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 5024 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 5025 | ASTContext &Ctx = CGM.getContext(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5026 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 5027 | ShortTy = Types.ConvertType(Ctx.ShortTy); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 5028 | IntTy = Types.ConvertType(Ctx.IntTy); |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 5029 | LongTy = Types.ConvertType(Ctx.LongTy); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5030 | LongLongTy = Types.ConvertType(Ctx.LongLongTy); |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5031 | Int8PtrTy = CGM.Int8PtrTy; |
| 5032 | Int8PtrPtrTy = CGM.Int8PtrPtrTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5033 | |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 5034 | ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType()); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5035 | PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy); |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 5036 | SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType()); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5037 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5038 | // I'm not sure I like this. The implicit coordination is a bit |
| 5039 | // gross. We should solve this in a reasonable fashion because this |
| 5040 | // is a pretty common task (match some runtime data structure with |
| 5041 | // an LLVM data structure). |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5042 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5043 | // FIXME: This is leaked. |
| 5044 | // FIXME: Merge with rewriter code? |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5045 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5046 | // struct _objc_super { |
| 5047 | // id self; |
| 5048 | // Class cls; |
| 5049 | // } |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5050 | RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct, |
Daniel Dunbar | 0c00537 | 2010-04-29 16:29:11 +0000 | [diff] [blame] | 5051 | Ctx.getTranslationUnitDecl(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 5052 | SourceLocation(), SourceLocation(), |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5053 | &Ctx.Idents.get("_objc_super")); |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 5054 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0, |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 5055 | Ctx.getObjCIdType(), 0, 0, false, ICIS_NoInit)); |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 5056 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0, |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 5057 | Ctx.getObjCClassType(), 0, 0, false, |
| 5058 | ICIS_NoInit)); |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 5059 | RD->completeDefinition(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5060 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5061 | SuperCTy = Ctx.getTagDeclType(RD); |
| 5062 | SuperPtrCTy = Ctx.getPointerType(SuperCTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5063 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5064 | SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy)); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5065 | SuperPtrTy = llvm::PointerType::getUnqual(SuperTy); |
| 5066 | |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5067 | // struct _prop_t { |
| 5068 | // char *name; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5069 | // char *attributes; |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5070 | // } |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5071 | PropertyTy = llvm::StructType::create("struct._prop_t", |
| 5072 | Int8PtrTy, Int8PtrTy, NULL); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5073 | |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5074 | // struct _prop_list_t { |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 5075 | // uint32_t entsize; // sizeof(struct _prop_t) |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5076 | // uint32_t count_of_properties; |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 5077 | // struct _prop_t prop_list[count_of_properties]; |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5078 | // } |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5079 | PropertyListTy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5080 | llvm::StructType::create("struct._prop_list_t", IntTy, IntTy, |
| 5081 | llvm::ArrayType::get(PropertyTy, 0), NULL); |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5082 | // struct _prop_list_t * |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5083 | PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5084 | |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5085 | // struct _objc_method { |
| 5086 | // SEL _cmd; |
| 5087 | // char *method_type; |
| 5088 | // char *_imp; |
| 5089 | // } |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5090 | MethodTy = llvm::StructType::create("struct._objc_method", |
| 5091 | SelectorPtrTy, Int8PtrTy, Int8PtrTy, |
| 5092 | NULL); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5093 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 5094 | // struct _objc_cache * |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5095 | CacheTy = llvm::StructType::create(VMContext, "struct._objc_cache"); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5096 | CachePtrTy = llvm::PointerType::getUnqual(CacheTy); |
Fariborz Jahanian | 0a3cfcc | 2011-06-22 20:21:51 +0000 | [diff] [blame] | 5097 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5098 | } |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 5099 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5100 | ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5101 | : ObjCCommonTypesHelper(cgm) { |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 5102 | // struct _objc_method_description { |
| 5103 | // SEL name; |
| 5104 | // char *types; |
| 5105 | // } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5106 | MethodDescriptionTy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5107 | llvm::StructType::create("struct._objc_method_description", |
| 5108 | SelectorPtrTy, Int8PtrTy, NULL); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 5109 | |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 5110 | // struct _objc_method_description_list { |
| 5111 | // int count; |
| 5112 | // struct _objc_method_description[1]; |
| 5113 | // } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5114 | MethodDescriptionListTy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5115 | llvm::StructType::create("struct._objc_method_description_list", |
| 5116 | IntTy, |
| 5117 | llvm::ArrayType::get(MethodDescriptionTy, 0),NULL); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5118 | |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 5119 | // struct _objc_method_description_list * |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5120 | MethodDescriptionListPtrTy = |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5121 | llvm::PointerType::getUnqual(MethodDescriptionListTy); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 5122 | |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 5123 | // Protocol description structures |
| 5124 | |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 5125 | // struct _objc_protocol_extension { |
| 5126 | // uint32_t size; // sizeof(struct _objc_protocol_extension) |
| 5127 | // struct _objc_method_description_list *optional_instance_methods; |
| 5128 | // struct _objc_method_description_list *optional_class_methods; |
| 5129 | // struct _objc_property_list *instance_properties; |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5130 | // const char ** extendedMethodTypes; |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 5131 | // } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5132 | ProtocolExtensionTy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5133 | llvm::StructType::create("struct._objc_protocol_extension", |
| 5134 | IntTy, MethodDescriptionListPtrTy, |
| 5135 | MethodDescriptionListPtrTy, PropertyListPtrTy, |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5136 | Int8PtrPtrTy, NULL); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5137 | |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 5138 | // struct _objc_protocol_extension * |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5139 | ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 5140 | |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 5141 | // Handle recursive construction of Protocol and ProtocolList types |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 5142 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5143 | ProtocolTy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5144 | llvm::StructType::create(VMContext, "struct._objc_protocol"); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 5145 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5146 | ProtocolListTy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5147 | llvm::StructType::create(VMContext, "struct._objc_protocol_list"); |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5148 | ProtocolListTy->setBody(llvm::PointerType::getUnqual(ProtocolListTy), |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5149 | LongTy, |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5150 | llvm::ArrayType::get(ProtocolTy, 0), |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5151 | NULL); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 5152 | |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 5153 | // struct _objc_protocol { |
| 5154 | // struct _objc_protocol_extension *isa; |
| 5155 | // char *protocol_name; |
| 5156 | // struct _objc_protocol **_objc_protocol_list; |
| 5157 | // struct _objc_method_description_list *instance_methods; |
| 5158 | // struct _objc_method_description_list *class_methods; |
| 5159 | // } |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5160 | ProtocolTy->setBody(ProtocolExtensionPtrTy, Int8PtrTy, |
| 5161 | llvm::PointerType::getUnqual(ProtocolListTy), |
| 5162 | MethodDescriptionListPtrTy, |
| 5163 | MethodDescriptionListPtrTy, |
| 5164 | NULL); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 5165 | |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 5166 | // struct _objc_protocol_list * |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5167 | ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 5168 | |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5169 | ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 5170 | |
| 5171 | // Class description structures |
| 5172 | |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 5173 | // struct _objc_ivar { |
| 5174 | // char *ivar_name; |
| 5175 | // char *ivar_type; |
| 5176 | // int ivar_offset; |
| 5177 | // } |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5178 | IvarTy = llvm::StructType::create("struct._objc_ivar", |
| 5179 | Int8PtrTy, Int8PtrTy, IntTy, NULL); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 5180 | |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 5181 | // struct _objc_ivar_list * |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5182 | IvarListTy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5183 | llvm::StructType::create(VMContext, "struct._objc_ivar_list"); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5184 | IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 5185 | |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 5186 | // struct _objc_method_list * |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5187 | MethodListTy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5188 | llvm::StructType::create(VMContext, "struct._objc_method_list"); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5189 | MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 5190 | |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 5191 | // struct _objc_class_extension * |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5192 | ClassExtensionTy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5193 | llvm::StructType::create("struct._objc_class_extension", |
| 5194 | IntTy, Int8PtrTy, PropertyListPtrTy, NULL); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5195 | ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 5196 | |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5197 | ClassTy = llvm::StructType::create(VMContext, "struct._objc_class"); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 5198 | |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 5199 | // struct _objc_class { |
| 5200 | // Class isa; |
| 5201 | // Class super_class; |
| 5202 | // char *name; |
| 5203 | // long version; |
| 5204 | // long info; |
| 5205 | // long instance_size; |
| 5206 | // struct _objc_ivar_list *ivars; |
| 5207 | // struct _objc_method_list *methods; |
| 5208 | // struct _objc_cache *cache; |
| 5209 | // struct _objc_protocol_list *protocols; |
| 5210 | // char *ivar_layout; |
| 5211 | // struct _objc_class_ext *ext; |
| 5212 | // }; |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5213 | ClassTy->setBody(llvm::PointerType::getUnqual(ClassTy), |
| 5214 | llvm::PointerType::getUnqual(ClassTy), |
| 5215 | Int8PtrTy, |
| 5216 | LongTy, |
| 5217 | LongTy, |
| 5218 | LongTy, |
| 5219 | IvarListPtrTy, |
| 5220 | MethodListPtrTy, |
| 5221 | CachePtrTy, |
| 5222 | ProtocolListPtrTy, |
| 5223 | Int8PtrTy, |
| 5224 | ClassExtensionPtrTy, |
| 5225 | NULL); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5226 | |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5227 | ClassPtrTy = llvm::PointerType::getUnqual(ClassTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 5228 | |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 5229 | // struct _objc_category { |
| 5230 | // char *category_name; |
| 5231 | // char *class_name; |
| 5232 | // struct _objc_method_list *instance_method; |
| 5233 | // struct _objc_method_list *class_method; |
| 5234 | // uint32_t size; // sizeof(struct _objc_category) |
| 5235 | // struct _objc_property_list *instance_properties;// category's @property |
| 5236 | // } |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5237 | CategoryTy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5238 | llvm::StructType::create("struct._objc_category", |
| 5239 | Int8PtrTy, Int8PtrTy, MethodListPtrTy, |
| 5240 | MethodListPtrTy, ProtocolListPtrTy, |
| 5241 | IntTy, PropertyListPtrTy, NULL); |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 5242 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 5243 | // Global metadata structures |
| 5244 | |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 5245 | // struct _objc_symtab { |
| 5246 | // long sel_ref_cnt; |
| 5247 | // SEL *refs; |
| 5248 | // short cls_def_cnt; |
| 5249 | // short cat_def_cnt; |
| 5250 | // char *defs[cls_def_cnt + cat_def_cnt]; |
| 5251 | // } |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5252 | SymtabTy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5253 | llvm::StructType::create("struct._objc_symtab", |
| 5254 | LongTy, SelectorPtrTy, ShortTy, ShortTy, |
| 5255 | llvm::ArrayType::get(Int8PtrTy, 0), NULL); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5256 | SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 5257 | |
Fariborz Jahanian | eee54df | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 5258 | // struct _objc_module { |
| 5259 | // long version; |
| 5260 | // long size; // sizeof(struct _objc_module) |
| 5261 | // char *name; |
| 5262 | // struct _objc_symtab* symtab; |
| 5263 | // } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5264 | ModuleTy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5265 | llvm::StructType::create("struct._objc_module", |
| 5266 | LongTy, LongTy, Int8PtrTy, SymtabPtrTy, NULL); |
Daniel Dunbar | 97ff50d | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 5267 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5268 | |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 5269 | // FIXME: This is the size of the setjmp buffer and should be target |
| 5270 | // specific. 18 is what's used on 32-bit X86. |
Anders Carlsson | 9ff2248 | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 5271 | uint64_t SetJmpBufferSize = 18; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5272 | |
Anders Carlsson | 9ff2248 | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 5273 | // Exceptions |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5274 | llvm::Type *StackPtrTy = llvm::ArrayType::get(CGM.Int8PtrTy, 4); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5275 | |
| 5276 | ExceptionDataTy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5277 | llvm::StructType::create("struct._objc_exception_data", |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5278 | llvm::ArrayType::get(CGM.Int32Ty,SetJmpBufferSize), |
| 5279 | StackPtrTy, NULL); |
Anders Carlsson | 9ff2248 | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 5280 | |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 5281 | } |
| 5282 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5283 | ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5284 | : ObjCCommonTypesHelper(cgm) { |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5285 | // struct _method_list_t { |
| 5286 | // uint32_t entsize; // sizeof(struct _objc_method) |
| 5287 | // uint32_t method_count; |
| 5288 | // struct _objc_method method_list[method_count]; |
| 5289 | // } |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5290 | MethodListnfABITy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5291 | llvm::StructType::create("struct.__method_list_t", IntTy, IntTy, |
| 5292 | llvm::ArrayType::get(MethodTy, 0), NULL); |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 5293 | // struct method_list_t * |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5294 | MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5295 | |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5296 | // struct _protocol_t { |
| 5297 | // id isa; // NULL |
| 5298 | // const char * const protocol_name; |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 5299 | // const struct _protocol_list_t * protocol_list; // super protocols |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5300 | // const struct method_list_t * const instance_methods; |
| 5301 | // const struct method_list_t * const class_methods; |
| 5302 | // const struct method_list_t *optionalInstanceMethods; |
| 5303 | // const struct method_list_t *optionalClassMethods; |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 5304 | // const struct _prop_list_t * properties; |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5305 | // const uint32_t size; // sizeof(struct _protocol_t) |
| 5306 | // const uint32_t flags; // = 0 |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5307 | // const char ** extendedMethodTypes; |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5308 | // } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5309 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 5310 | // Holder for struct _protocol_list_t * |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5311 | ProtocolListnfABITy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5312 | llvm::StructType::create(VMContext, "struct._objc_protocol_list"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5313 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5314 | ProtocolnfABITy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5315 | llvm::StructType::create("struct._protocol_t", ObjectPtrTy, Int8PtrTy, |
| 5316 | llvm::PointerType::getUnqual(ProtocolListnfABITy), |
| 5317 | MethodListnfABIPtrTy, MethodListnfABIPtrTy, |
| 5318 | MethodListnfABIPtrTy, MethodListnfABIPtrTy, |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5319 | PropertyListPtrTy, IntTy, IntTy, Int8PtrPtrTy, |
| 5320 | NULL); |
Daniel Dunbar | 8de90f0 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 5321 | |
| 5322 | // struct _protocol_t* |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5323 | ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5324 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5325 | // struct _protocol_list_t { |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 5326 | // long protocol_count; // Note, this is 32/64 bit |
Daniel Dunbar | 8de90f0 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 5327 | // struct _protocol_t *[protocol_count]; |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5328 | // } |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5329 | ProtocolListnfABITy->setBody(LongTy, |
| 5330 | llvm::ArrayType::get(ProtocolnfABIPtrTy, 0), |
| 5331 | NULL); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5332 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 5333 | // struct _objc_protocol_list* |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5334 | ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5335 | |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5336 | // struct _ivar_t { |
| 5337 | // unsigned long int *offset; // pointer to ivar offset location |
| 5338 | // char *name; |
| 5339 | // char *type; |
| 5340 | // uint32_t alignment; |
| 5341 | // uint32_t size; |
| 5342 | // } |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5343 | IvarnfABITy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5344 | llvm::StructType::create("struct._ivar_t", |
| 5345 | llvm::PointerType::getUnqual(LongTy), |
| 5346 | Int8PtrTy, Int8PtrTy, IntTy, IntTy, NULL); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5347 | |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5348 | // struct _ivar_list_t { |
| 5349 | // uint32 entsize; // sizeof(struct _ivar_t) |
| 5350 | // uint32 count; |
| 5351 | // struct _iver_t list[count]; |
| 5352 | // } |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5353 | IvarListnfABITy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5354 | llvm::StructType::create("struct._ivar_list_t", IntTy, IntTy, |
| 5355 | llvm::ArrayType::get(IvarnfABITy, 0), NULL); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5356 | |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5357 | IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5358 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 5359 | // struct _class_ro_t { |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5360 | // uint32_t const flags; |
| 5361 | // uint32_t const instanceStart; |
| 5362 | // uint32_t const instanceSize; |
| 5363 | // uint32_t const reserved; // only when building for 64bit targets |
| 5364 | // const uint8_t * const ivarLayout; |
| 5365 | // const char *const name; |
| 5366 | // const struct _method_list_t * const baseMethods; |
| 5367 | // const struct _objc_protocol_list *const baseProtocols; |
| 5368 | // const struct _ivar_list_t *const ivars; |
| 5369 | // const uint8_t * const weakIvarLayout; |
| 5370 | // const struct _prop_list_t * const properties; |
| 5371 | // } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5372 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 5373 | // FIXME. Add 'reserved' field in 64bit abi mode! |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5374 | ClassRonfABITy = llvm::StructType::create("struct._class_ro_t", |
| 5375 | IntTy, IntTy, IntTy, Int8PtrTy, |
| 5376 | Int8PtrTy, MethodListnfABIPtrTy, |
| 5377 | ProtocolListnfABIPtrTy, |
| 5378 | IvarListnfABIPtrTy, |
| 5379 | Int8PtrTy, PropertyListPtrTy, NULL); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5380 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 5381 | // ImpnfABITy - LLVM for id (*)(id, SEL, ...) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5382 | llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy }; |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 5383 | ImpnfABITy = llvm::FunctionType::get(ObjectPtrTy, params, false) |
| 5384 | ->getPointerTo(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5385 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 5386 | // struct _class_t { |
| 5387 | // struct _class_t *isa; |
| 5388 | // struct _class_t * const superclass; |
| 5389 | // void *cache; |
| 5390 | // IMP *vtable; |
| 5391 | // struct class_ro_t *ro; |
| 5392 | // } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5393 | |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5394 | ClassnfABITy = llvm::StructType::create(VMContext, "struct._class_t"); |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5395 | ClassnfABITy->setBody(llvm::PointerType::getUnqual(ClassnfABITy), |
| 5396 | llvm::PointerType::getUnqual(ClassnfABITy), |
| 5397 | CachePtrTy, |
| 5398 | llvm::PointerType::getUnqual(ImpnfABITy), |
| 5399 | llvm::PointerType::getUnqual(ClassRonfABITy), |
| 5400 | NULL); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5401 | |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5402 | // LLVM for struct _class_t * |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5403 | ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5404 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 5405 | // struct _category_t { |
| 5406 | // const char * const name; |
| 5407 | // struct _class_t *const cls; |
| 5408 | // const struct _method_list_t * const instance_methods; |
| 5409 | // const struct _method_list_t * const class_methods; |
| 5410 | // const struct _protocol_list_t * const protocols; |
| 5411 | // const struct _prop_list_t * const properties; |
Fariborz Jahanian | 5a63e4c | 2009-01-23 17:41:22 +0000 | [diff] [blame] | 5412 | // } |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5413 | CategorynfABITy = llvm::StructType::create("struct._category_t", |
| 5414 | Int8PtrTy, ClassnfABIPtrTy, |
| 5415 | MethodListnfABIPtrTy, |
| 5416 | MethodListnfABIPtrTy, |
| 5417 | ProtocolListnfABIPtrTy, |
| 5418 | PropertyListPtrTy, |
| 5419 | NULL); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5420 | |
Fariborz Jahanian | 82c72e1 | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 5421 | // New types for nonfragile abi messaging. |
Fariborz Jahanian | e4dc35d | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5422 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 5423 | ASTContext &Ctx = CGM.getContext(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5424 | |
Fariborz Jahanian | 82c72e1 | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 5425 | // MessageRefTy - LLVM for: |
| 5426 | // struct _message_ref_t { |
| 5427 | // IMP messenger; |
| 5428 | // SEL name; |
| 5429 | // }; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5430 | |
Fariborz Jahanian | e4dc35d | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5431 | // First the clang type for struct _message_ref_t |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5432 | RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct, |
Daniel Dunbar | 0c00537 | 2010-04-29 16:29:11 +0000 | [diff] [blame] | 5433 | Ctx.getTranslationUnitDecl(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 5434 | SourceLocation(), SourceLocation(), |
Fariborz Jahanian | e4dc35d | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5435 | &Ctx.Idents.get("_message_ref_t")); |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 5436 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0, |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 5437 | Ctx.VoidPtrTy, 0, 0, false, ICIS_NoInit)); |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 5438 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0, |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 5439 | Ctx.getObjCSelType(), 0, 0, false, |
| 5440 | ICIS_NoInit)); |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 5441 | RD->completeDefinition(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5442 | |
Fariborz Jahanian | e4dc35d | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5443 | MessageRefCTy = Ctx.getTagDeclType(RD); |
| 5444 | MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy); |
| 5445 | MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy)); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5446 | |
Fariborz Jahanian | 82c72e1 | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 5447 | // MessageRefPtrTy - LLVM for struct _message_ref_t* |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5448 | MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5449 | |
Fariborz Jahanian | 82c72e1 | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 5450 | // SuperMessageRefTy - LLVM for: |
| 5451 | // struct _super_message_ref_t { |
| 5452 | // SUPER_IMP messenger; |
| 5453 | // SEL name; |
| 5454 | // }; |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5455 | SuperMessageRefTy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5456 | llvm::StructType::create("struct._super_message_ref_t", |
| 5457 | ImpnfABITy, SelectorPtrTy, NULL); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5458 | |
Fariborz Jahanian | 82c72e1 | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 5459 | // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t* |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5460 | SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy); |
Fariborz Jahanian | 0a3cfcc | 2011-06-22 20:21:51 +0000 | [diff] [blame] | 5461 | |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5462 | |
| 5463 | // struct objc_typeinfo { |
| 5464 | // const void** vtable; // objc_ehtype_vtable + 2 |
| 5465 | // const char* name; // c++ typeinfo string |
| 5466 | // Class cls; |
| 5467 | // }; |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5468 | EHTypeTy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5469 | llvm::StructType::create("struct._objc_typeinfo", |
| 5470 | llvm::PointerType::getUnqual(Int8PtrTy), |
| 5471 | Int8PtrTy, ClassnfABIPtrTy, NULL); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5472 | EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy); |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 5473 | } |
| 5474 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5475 | llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() { |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5476 | FinishNonFragileABIModule(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5477 | |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5478 | return NULL; |
| 5479 | } |
| 5480 | |
Bill Wendling | cb5b5ff | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 5481 | void CGObjCNonFragileABIMac:: |
| 5482 | AddModuleClassList(ArrayRef<llvm::GlobalValue*> Container, |
| 5483 | const char *SymbolName, |
| 5484 | const char *SectionName) { |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 5485 | unsigned NumClasses = Container.size(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5486 | |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 5487 | if (!NumClasses) |
| 5488 | return; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5489 | |
Chris Lattner | 3def9ae | 2012-02-06 22:16:34 +0000 | [diff] [blame] | 5490 | SmallVector<llvm::Constant*, 8> Symbols(NumClasses); |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 5491 | for (unsigned i=0; i<NumClasses; i++) |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 5492 | Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i], |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 5493 | ObjCTypes.Int8PtrTy); |
Chris Lattner | 3def9ae | 2012-02-06 22:16:34 +0000 | [diff] [blame] | 5494 | llvm::Constant *Init = |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5495 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
Chris Lattner | 3def9ae | 2012-02-06 22:16:34 +0000 | [diff] [blame] | 5496 | Symbols.size()), |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 5497 | Symbols); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5498 | |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 5499 | llvm::GlobalVariable *GV = |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5500 | new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Rafael Espindola | 5179a4e | 2014-02-27 19:01:11 +0000 | [diff] [blame] | 5501 | llvm::GlobalValue::PrivateLinkage, |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 5502 | Init, |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5503 | SymbolName); |
Rafael Espindola | 21039aa | 2014-02-27 16:26:32 +0000 | [diff] [blame] | 5504 | assertPrivateName(GV); |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 5505 | GV->setAlignment(CGM.getDataLayout().getABITypeAlignment(Init->getType())); |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 5506 | GV->setSection(SectionName); |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 5507 | CGM.addCompilerUsedGlobal(GV); |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 5508 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5509 | |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5510 | void CGObjCNonFragileABIMac::FinishNonFragileABIModule() { |
| 5511 | // nonfragile abi has no module definition. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5512 | |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 5513 | // Build list of all implemented class addresses in array |
Fariborz Jahanian | 279abd3 | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 5514 | // L_OBJC_LABEL_CLASS_$. |
Fariborz Jahanian | f322f2f | 2014-03-11 00:25:05 +0000 | [diff] [blame] | 5515 | |
| 5516 | for (unsigned i=0, NumClasses=ImplementedClasses.size(); i<NumClasses; i++) { |
| 5517 | const ObjCInterfaceDecl *ID = ImplementedClasses[i]; |
| 5518 | assert(ID); |
| 5519 | if (ObjCImplementationDecl *IMP = ID->getImplementation()) |
| 5520 | // We are implementing a weak imported interface. Give it external linkage |
| 5521 | if (ID->isWeakImported() && !IMP->isWeakImported()) |
| 5522 | DefinedClasses[i]->setLinkage(llvm::GlobalVariable::ExternalLinkage); |
| 5523 | } |
| 5524 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5525 | AddModuleClassList(DefinedClasses, |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 5526 | "\01L_OBJC_LABEL_CLASS_$", |
| 5527 | "__DATA, __objc_classlist, regular, no_dead_strip"); |
Rafael Espindola | 554256c | 2014-02-26 22:25:45 +0000 | [diff] [blame] | 5528 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5529 | AddModuleClassList(DefinedNonLazyClasses, |
Daniel Dunbar | 9a017d7 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 5530 | "\01L_OBJC_LABEL_NONLAZY_CLASS_$", |
| 5531 | "__DATA, __objc_nlclslist, regular, no_dead_strip"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5532 | |
Fariborz Jahanian | 279abd3 | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 5533 | // Build list of all implemented category addresses in array |
| 5534 | // L_OBJC_LABEL_CATEGORY_$. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5535 | AddModuleClassList(DefinedCategories, |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 5536 | "\01L_OBJC_LABEL_CATEGORY_$", |
| 5537 | "__DATA, __objc_catlist, regular, no_dead_strip"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5538 | AddModuleClassList(DefinedNonLazyCategories, |
Daniel Dunbar | 9a017d7 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 5539 | "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$", |
| 5540 | "__DATA, __objc_nlcatlist, regular, no_dead_strip"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5541 | |
Daniel Dunbar | 5e63927 | 2010-04-25 20:39:01 +0000 | [diff] [blame] | 5542 | EmitImageInfo(); |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5543 | } |
| 5544 | |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5545 | /// isVTableDispatchedSelector - Returns true if SEL is not in the list of |
| 5546 | /// VTableDispatchMethods; false otherwise. What this means is that |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5547 | /// except for the 19 selectors in the list, we generate 32bit-style |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 5548 | /// message dispatch call for all the rest. |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5549 | bool CGObjCNonFragileABIMac::isVTableDispatchedSelector(Selector Sel) { |
| 5550 | // At various points we've experimented with using vtable-based |
| 5551 | // dispatch for all methods. |
Daniel Dunbar | fca18c1b4 | 2010-04-24 17:56:46 +0000 | [diff] [blame] | 5552 | switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) { |
Daniel Dunbar | fca18c1b4 | 2010-04-24 17:56:46 +0000 | [diff] [blame] | 5553 | case CodeGenOptions::Legacy: |
Fariborz Jahanian | dfb3983 | 2010-04-19 17:53:30 +0000 | [diff] [blame] | 5554 | return false; |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5555 | case CodeGenOptions::NonLegacy: |
| 5556 | return true; |
Daniel Dunbar | fca18c1b4 | 2010-04-24 17:56:46 +0000 | [diff] [blame] | 5557 | case CodeGenOptions::Mixed: |
| 5558 | break; |
| 5559 | } |
| 5560 | |
| 5561 | // If so, see whether this selector is in the white-list of things which must |
| 5562 | // 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] | 5563 | if (VTableDispatchMethods.empty()) { |
| 5564 | VTableDispatchMethods.insert(GetNullarySelector("alloc")); |
| 5565 | VTableDispatchMethods.insert(GetNullarySelector("class")); |
| 5566 | VTableDispatchMethods.insert(GetNullarySelector("self")); |
| 5567 | VTableDispatchMethods.insert(GetNullarySelector("isFlipped")); |
| 5568 | VTableDispatchMethods.insert(GetNullarySelector("length")); |
| 5569 | VTableDispatchMethods.insert(GetNullarySelector("count")); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5570 | |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5571 | // These are vtable-based if GC is disabled. |
| 5572 | // Optimistically use vtable dispatch for hybrid compiles. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5573 | if (CGM.getLangOpts().getGC() != LangOptions::GCOnly) { |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5574 | VTableDispatchMethods.insert(GetNullarySelector("retain")); |
| 5575 | VTableDispatchMethods.insert(GetNullarySelector("release")); |
| 5576 | VTableDispatchMethods.insert(GetNullarySelector("autorelease")); |
| 5577 | } |
| 5578 | |
| 5579 | VTableDispatchMethods.insert(GetUnarySelector("allocWithZone")); |
| 5580 | VTableDispatchMethods.insert(GetUnarySelector("isKindOfClass")); |
| 5581 | VTableDispatchMethods.insert(GetUnarySelector("respondsToSelector")); |
| 5582 | VTableDispatchMethods.insert(GetUnarySelector("objectForKey")); |
| 5583 | VTableDispatchMethods.insert(GetUnarySelector("objectAtIndex")); |
| 5584 | VTableDispatchMethods.insert(GetUnarySelector("isEqualToString")); |
| 5585 | VTableDispatchMethods.insert(GetUnarySelector("isEqual")); |
| 5586 | |
| 5587 | // These are vtable-based if GC is enabled. |
| 5588 | // Optimistically use vtable dispatch for hybrid compiles. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5589 | if (CGM.getLangOpts().getGC() != LangOptions::NonGC) { |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5590 | VTableDispatchMethods.insert(GetNullarySelector("hash")); |
| 5591 | VTableDispatchMethods.insert(GetUnarySelector("addObject")); |
| 5592 | |
| 5593 | // "countByEnumeratingWithState:objects:count" |
| 5594 | IdentifierInfo *KeyIdents[] = { |
| 5595 | &CGM.getContext().Idents.get("countByEnumeratingWithState"), |
| 5596 | &CGM.getContext().Idents.get("objects"), |
| 5597 | &CGM.getContext().Idents.get("count") |
| 5598 | }; |
| 5599 | VTableDispatchMethods.insert( |
| 5600 | CGM.getContext().Selectors.getSelector(3, KeyIdents)); |
| 5601 | } |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 5602 | } |
Daniel Dunbar | fca18c1b4 | 2010-04-24 17:56:46 +0000 | [diff] [blame] | 5603 | |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5604 | return VTableDispatchMethods.count(Sel); |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 5605 | } |
| 5606 | |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5607 | /// BuildClassRoTInitializer - generate meta-data for: |
| 5608 | /// struct _class_ro_t { |
| 5609 | /// uint32_t const flags; |
| 5610 | /// uint32_t const instanceStart; |
| 5611 | /// uint32_t const instanceSize; |
| 5612 | /// uint32_t const reserved; // only when building for 64bit targets |
| 5613 | /// const uint8_t * const ivarLayout; |
| 5614 | /// const char *const name; |
| 5615 | /// const struct _method_list_t * const baseMethods; |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5616 | /// const struct _protocol_list_t *const baseProtocols; |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5617 | /// const struct _ivar_list_t *const ivars; |
| 5618 | /// const uint8_t * const weakIvarLayout; |
| 5619 | /// const struct _prop_list_t * const properties; |
| 5620 | /// } |
| 5621 | /// |
| 5622 | llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5623 | unsigned flags, |
| 5624 | unsigned InstanceStart, |
| 5625 | unsigned InstanceSize, |
| 5626 | const ObjCImplementationDecl *ID) { |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5627 | std::string ClassName = ID->getNameAsString(); |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 5628 | llvm::Constant *Values[10]; // 11 for 64bit targets! |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5629 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5630 | if (CGM.getLangOpts().ObjCAutoRefCount) |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5631 | flags |= NonFragileABI_Class_CompiledByARC; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5632 | |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 5633 | Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags); |
| 5634 | Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart); |
| 5635 | Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize); |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5636 | // FIXME. For 64bit targets add 0 here. |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5637 | Values[ 3] = (flags & NonFragileABI_Class_Meta) |
| 5638 | ? GetIvarLayoutName(0, ObjCTypes) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5639 | : BuildIvarLayout(ID, true); |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5640 | Values[ 4] = GetClassName(ID->getIdentifier()); |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5641 | // const struct _method_list_t * const baseMethods; |
| 5642 | std::vector<llvm::Constant*> Methods; |
| 5643 | std::string MethodListName("\01l_OBJC_$_"); |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5644 | if (flags & NonFragileABI_Class_Meta) { |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5645 | MethodListName += "CLASS_METHODS_" + ID->getNameAsString(); |
Aaron Ballman | e8a7dc9 | 2014-03-13 20:11:06 +0000 | [diff] [blame] | 5646 | for (const auto *I : ID->class_methods()) |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5647 | // Class methods should always be defined. |
Aaron Ballman | e8a7dc9 | 2014-03-13 20:11:06 +0000 | [diff] [blame] | 5648 | Methods.push_back(GetMethodConstant(I)); |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5649 | } else { |
| 5650 | MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString(); |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 5651 | for (const auto *I : ID->instance_methods()) |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5652 | // Instance methods should always be defined. |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 5653 | Methods.push_back(GetMethodConstant(I)); |
| 5654 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5655 | for (ObjCImplementationDecl::propimpl_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5656 | i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5657 | ObjCPropertyImplDecl *PID = *i; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5658 | |
Fariborz Jahanian | d27a820 | 2009-01-28 22:46:49 +0000 | [diff] [blame] | 5659 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){ |
| 5660 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5661 | |
Fariborz Jahanian | d27a820 | 2009-01-28 22:46:49 +0000 | [diff] [blame] | 5662 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) |
| 5663 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 5664 | Methods.push_back(C); |
| 5665 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) |
| 5666 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 5667 | Methods.push_back(C); |
| 5668 | } |
| 5669 | } |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5670 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5671 | Values[ 5] = EmitMethodList(MethodListName, |
| 5672 | "__DATA, __objc_const", Methods); |
| 5673 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5674 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
| 5675 | assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5676 | Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_" |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5677 | + OID->getName(), |
Ted Kremenek | 0ef508d | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 5678 | OID->all_referenced_protocol_begin(), |
| 5679 | OID->all_referenced_protocol_end()); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5680 | |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5681 | if (flags & NonFragileABI_Class_Meta) { |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5682 | Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy); |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5683 | Values[ 8] = GetIvarLayoutName(0, ObjCTypes); |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5684 | Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5685 | } else { |
| 5686 | Values[ 7] = EmitIvarList(ID); |
| 5687 | Values[ 8] = BuildIvarLayout(ID, false); |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5688 | Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(), |
| 5689 | ID, ID->getClassInterface(), ObjCTypes); |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5690 | } |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 5691 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy, |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5692 | Values); |
| 5693 | llvm::GlobalVariable *CLASS_RO_GV = |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5694 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false, |
Rafael Espindola | 5179a4e | 2014-02-27 19:01:11 +0000 | [diff] [blame] | 5695 | llvm::GlobalValue::PrivateLinkage, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5696 | Init, |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5697 | (flags & NonFragileABI_Class_Meta) ? |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5698 | std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName : |
| 5699 | std::string("\01l_OBJC_CLASS_RO_$_")+ClassName); |
Rafael Espindola | 21039aa | 2014-02-27 16:26:32 +0000 | [diff] [blame] | 5700 | assertPrivateName(CLASS_RO_GV); |
Fariborz Jahanian | c22f236 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 5701 | CLASS_RO_GV->setAlignment( |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 5702 | CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ClassRonfABITy)); |
Fariborz Jahanian | 40a4bcd | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 5703 | CLASS_RO_GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5704 | return CLASS_RO_GV; |
Fariborz Jahanian | 2612e14 | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5705 | |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5706 | } |
| 5707 | |
| 5708 | /// BuildClassMetaData - This routine defines that to-level meta-data |
| 5709 | /// for the given ClassName for: |
| 5710 | /// struct _class_t { |
| 5711 | /// struct _class_t *isa; |
| 5712 | /// struct _class_t * const superclass; |
| 5713 | /// void *cache; |
| 5714 | /// IMP *vtable; |
| 5715 | /// struct class_ro_t *ro; |
| 5716 | /// } |
| 5717 | /// |
Rafael Espindola | 554256c | 2014-02-26 22:25:45 +0000 | [diff] [blame] | 5718 | llvm::GlobalVariable *CGObjCNonFragileABIMac::BuildClassMetaData( |
| 5719 | std::string &ClassName, llvm::Constant *IsAGV, llvm::Constant *SuperClassGV, |
| 5720 | llvm::Constant *ClassRoGV, bool HiddenVisibility, bool Weak) { |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 5721 | llvm::Constant *Values[] = { |
| 5722 | IsAGV, |
| 5723 | SuperClassGV, |
| 5724 | ObjCEmptyCacheVar, // &ObjCEmptyCacheVar |
| 5725 | ObjCEmptyVtableVar, // &ObjCEmptyVtableVar |
| 5726 | ClassRoGV // &CLASS_RO_GV |
| 5727 | }; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5728 | if (!Values[1]) |
| 5729 | Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy); |
Fariborz Jahanian | 42d4955 | 2013-10-24 17:40:28 +0000 | [diff] [blame] | 5730 | if (!Values[3]) |
| 5731 | Values[3] = llvm::Constant::getNullValue( |
| 5732 | llvm::PointerType::getUnqual(ObjCTypes.ImpnfABITy)); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5733 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy, |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5734 | Values); |
Rafael Espindola | 554256c | 2014-02-26 22:25:45 +0000 | [diff] [blame] | 5735 | llvm::GlobalVariable *GV = GetClassGlobal(ClassName, Weak); |
Daniel Dunbar | c6928bb | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5736 | GV->setInitializer(Init); |
Fariborz Jahanian | 0408723 | 2009-01-31 01:07:39 +0000 | [diff] [blame] | 5737 | GV->setSection("__DATA, __objc_data"); |
Fariborz Jahanian | c22f236 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 5738 | GV->setAlignment( |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 5739 | CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ClassnfABITy)); |
Fariborz Jahanian | 8220825 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 5740 | if (HiddenVisibility) |
| 5741 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Fariborz Jahanian | 4723fb7 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5742 | return GV; |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5743 | } |
| 5744 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5745 | bool |
Fariborz Jahanian | a6bed83 | 2009-05-21 01:03:45 +0000 | [diff] [blame] | 5746 | CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const { |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5747 | return OD->getClassMethod(GetNullarySelector("load")) != 0; |
Daniel Dunbar | 9a017d7 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 5748 | } |
| 5749 | |
Daniel Dunbar | 96120237 | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 5750 | void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID, |
Daniel Dunbar | 554fd79 | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 5751 | uint32_t &InstanceStart, |
| 5752 | uint32_t &InstanceSize) { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5753 | const ASTRecordLayout &RL = |
Daniel Dunbar | 9252ee1 | 2009-05-04 21:26:30 +0000 | [diff] [blame] | 5754 | CGM.getContext().getASTObjCImplementationLayout(OID); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5755 | |
Daniel Dunbar | 9b042e0 | 2009-05-04 23:23:09 +0000 | [diff] [blame] | 5756 | // InstanceSize is really instance end. |
Ken Dyck | d5090c1 | 2011-02-11 02:20:09 +0000 | [diff] [blame] | 5757 | InstanceSize = RL.getDataSize().getQuantity(); |
Daniel Dunbar | 9b042e0 | 2009-05-04 23:23:09 +0000 | [diff] [blame] | 5758 | |
| 5759 | // If there are no fields, the start is the same as the end. |
| 5760 | if (!RL.getFieldCount()) |
| 5761 | InstanceStart = InstanceSize; |
| 5762 | else |
Ken Dyck | c5ca876 | 2011-04-14 00:43:09 +0000 | [diff] [blame] | 5763 | InstanceStart = RL.getFieldOffset(0) / CGM.getContext().getCharWidth(); |
Daniel Dunbar | 554fd79 | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 5764 | } |
| 5765 | |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5766 | void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) { |
| 5767 | std::string ClassName = ID->getNameAsString(); |
| 5768 | if (!ObjCEmptyCacheVar) { |
| 5769 | ObjCEmptyCacheVar = new llvm::GlobalVariable( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5770 | CGM.getModule(), |
| 5771 | ObjCTypes.CacheTy, |
| 5772 | false, |
| 5773 | llvm::GlobalValue::ExternalLinkage, |
| 5774 | 0, |
| 5775 | "_objc_empty_cache"); |
Fariborz Jahanian | 42d4955 | 2013-10-24 17:40:28 +0000 | [diff] [blame] | 5776 | |
| 5777 | // Make this entry NULL for any iOS device target, any iOS simulator target, |
| 5778 | // OS X with deployment target 10.9 or later. |
| 5779 | const llvm::Triple &Triple = CGM.getTarget().getTriple(); |
| 5780 | if (Triple.isiOS() || (Triple.isMacOSX() && !Triple.isMacOSXVersionLT(10, 9))) |
| 5781 | // This entry will be null. |
| 5782 | ObjCEmptyVtableVar = 0; |
| 5783 | else |
| 5784 | ObjCEmptyVtableVar = new llvm::GlobalVariable( |
| 5785 | CGM.getModule(), |
| 5786 | ObjCTypes.ImpnfABITy, |
| 5787 | false, |
| 5788 | llvm::GlobalValue::ExternalLinkage, |
| 5789 | 0, |
| 5790 | "_objc_empty_vtable"); |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5791 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5792 | assert(ID->getClassInterface() && |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5793 | "CGObjCNonFragileABIMac::GenerateClass - class is 0"); |
Daniel Dunbar | e3f5cfc | 2009-04-20 20:18:54 +0000 | [diff] [blame] | 5794 | // FIXME: Is this correct (that meta class size is never computed)? |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5795 | uint32_t InstanceStart = |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 5796 | CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ClassnfABITy); |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5797 | uint32_t InstanceSize = InstanceStart; |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5798 | uint32_t flags = NonFragileABI_Class_Meta; |
Daniel Dunbar | 15894b7 | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5799 | std::string ObjCMetaClassName(getMetaclassSymbolPrefix()); |
| 5800 | std::string ObjCClassName(getClassSymbolPrefix()); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5801 | |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5802 | llvm::GlobalVariable *SuperClassGV, *IsAGV; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5803 | |
John McCall | 0d54a17 | 2012-10-17 04:53:31 +0000 | [diff] [blame] | 5804 | // Build the flags for the metaclass. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5805 | bool classIsHidden = |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 5806 | ID->getClassInterface()->getVisibility() == HiddenVisibility; |
Fariborz Jahanian | 8220825 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 5807 | if (classIsHidden) |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5808 | flags |= NonFragileABI_Class_Hidden; |
John McCall | 0d54a17 | 2012-10-17 04:53:31 +0000 | [diff] [blame] | 5809 | |
| 5810 | // FIXME: why is this flag set on the metaclass? |
| 5811 | // ObjC metaclasses have no fields and don't really get constructed. |
| 5812 | if (ID->hasNonZeroConstructors() || ID->hasDestructors()) { |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5813 | flags |= NonFragileABI_Class_HasCXXStructors; |
John McCall | 0d54a17 | 2012-10-17 04:53:31 +0000 | [diff] [blame] | 5814 | if (!ID->hasNonZeroConstructors()) |
| 5815 | flags |= NonFragileABI_Class_HasCXXDestructorOnly; |
| 5816 | } |
| 5817 | |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5818 | if (!ID->getClassInterface()->getSuperClass()) { |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5819 | // class is root |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5820 | flags |= NonFragileABI_Class_Root; |
Fariborz Jahanian | f322f2f | 2014-03-11 00:25:05 +0000 | [diff] [blame] | 5821 | SuperClassGV = GetClassGlobal(ObjCClassName + ClassName, |
| 5822 | ID->getClassInterface()->isWeakImported()); |
Rafael Espindola | 554256c | 2014-02-26 22:25:45 +0000 | [diff] [blame] | 5823 | IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName, |
| 5824 | ID->getClassInterface()->isWeakImported()); |
| 5825 | |
| 5826 | // We are implementing a weak imported interface. Give it external |
| 5827 | // linkage. |
| 5828 | if (!ID->isWeakImported() && ID->getClassInterface()->isWeakImported()) |
| 5829 | IsAGV->setLinkage(llvm::GlobalVariable::ExternalLinkage); |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5830 | } else { |
Fariborz Jahanian | 4723fb7 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5831 | // Has a root. Current class is not a root. |
Fariborz Jahanian | 03b300b | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 5832 | const ObjCInterfaceDecl *Root = ID->getClassInterface(); |
| 5833 | while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) |
| 5834 | Root = Super; |
Rafael Espindola | 554256c | 2014-02-26 22:25:45 +0000 | [diff] [blame] | 5835 | IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString(), |
| 5836 | Root->isWeakImported()); |
Fariborz Jahanian | 03b300b | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 5837 | // work on super class metadata symbol. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5838 | std::string SuperClassName = |
Fariborz Jahanian | a6227fd | 2009-12-01 18:25:24 +0000 | [diff] [blame] | 5839 | ObjCMetaClassName + |
| 5840 | ID->getClassInterface()->getSuperClass()->getNameAsString(); |
Rafael Espindola | 554256c | 2014-02-26 22:25:45 +0000 | [diff] [blame] | 5841 | SuperClassGV = GetClassGlobal( |
| 5842 | SuperClassName, |
| 5843 | ID->getClassInterface()->getSuperClass()->isWeakImported()); |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5844 | } |
| 5845 | llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags, |
| 5846 | InstanceStart, |
| 5847 | InstanceSize,ID); |
Fariborz Jahanian | 4723fb7 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5848 | std::string TClassName = ObjCMetaClassName + ClassName; |
Rafael Espindola | 554256c | 2014-02-26 22:25:45 +0000 | [diff] [blame] | 5849 | llvm::GlobalVariable *MetaTClass = BuildClassMetaData( |
| 5850 | TClassName, IsAGV, SuperClassGV, CLASS_RO_GV, classIsHidden, |
| 5851 | ID->isWeakImported()); |
Fariborz Jahanian | 6726055 | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 5852 | DefinedMetaClasses.push_back(MetaTClass); |
Daniel Dunbar | 15894b7 | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5853 | |
Fariborz Jahanian | 4723fb7 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5854 | // Metadata for the class |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5855 | flags = 0; |
Fariborz Jahanian | 8220825 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 5856 | if (classIsHidden) |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5857 | flags |= NonFragileABI_Class_Hidden; |
John McCall | 0d54a17 | 2012-10-17 04:53:31 +0000 | [diff] [blame] | 5858 | |
| 5859 | if (ID->hasNonZeroConstructors() || ID->hasDestructors()) { |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5860 | flags |= NonFragileABI_Class_HasCXXStructors; |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5861 | |
John McCall | 0d54a17 | 2012-10-17 04:53:31 +0000 | [diff] [blame] | 5862 | // Set a flag to enable a runtime optimization when a class has |
| 5863 | // fields that require destruction but which don't require |
| 5864 | // anything except zero-initialization during construction. This |
| 5865 | // is most notably true of __strong and __weak types, but you can |
| 5866 | // also imagine there being C++ types with non-trivial default |
| 5867 | // constructors that merely set all fields to null. |
| 5868 | if (!ID->hasNonZeroConstructors()) |
| 5869 | flags |= NonFragileABI_Class_HasCXXDestructorOnly; |
| 5870 | } |
| 5871 | |
Douglas Gregor | 78bd61f | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 5872 | if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface())) |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5873 | flags |= NonFragileABI_Class_Exception; |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5874 | |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5875 | if (!ID->getClassInterface()->getSuperClass()) { |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5876 | flags |= NonFragileABI_Class_Root; |
Fariborz Jahanian | 4723fb7 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5877 | SuperClassGV = 0; |
Chris Lattner | b433b27 | 2009-04-19 06:02:28 +0000 | [diff] [blame] | 5878 | } else { |
Fariborz Jahanian | 4723fb7 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5879 | // Has a root. Current class is not a root. |
Fariborz Jahanian | 03b300b | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 5880 | std::string RootClassName = |
Fariborz Jahanian | 4723fb7 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5881 | ID->getClassInterface()->getSuperClass()->getNameAsString(); |
Rafael Espindola | 554256c | 2014-02-26 22:25:45 +0000 | [diff] [blame] | 5882 | SuperClassGV = GetClassGlobal( |
| 5883 | ObjCClassName + RootClassName, |
| 5884 | ID->getClassInterface()->getSuperClass()->isWeakImported()); |
Fariborz Jahanian | 4723fb7 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5885 | } |
Daniel Dunbar | 96120237 | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 5886 | GetClassSizeInfo(ID, InstanceStart, InstanceSize); |
Fariborz Jahanian | 4723fb7 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5887 | CLASS_RO_GV = BuildClassRoTInitializer(flags, |
Fariborz Jahanian | a887e63 | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 5888 | InstanceStart, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5889 | InstanceSize, |
Fariborz Jahanian | a887e63 | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 5890 | ID); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5891 | |
Fariborz Jahanian | 4723fb7 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5892 | TClassName = ObjCClassName + ClassName; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5893 | llvm::GlobalVariable *ClassMD = |
Fariborz Jahanian | 8220825 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 5894 | BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV, |
Fariborz Jahanian | f322f2f | 2014-03-11 00:25:05 +0000 | [diff] [blame] | 5895 | classIsHidden, |
| 5896 | ID->getClassInterface()->isWeakImported()); |
Fariborz Jahanian | 279abd3 | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 5897 | DefinedClasses.push_back(ClassMD); |
Fariborz Jahanian | f322f2f | 2014-03-11 00:25:05 +0000 | [diff] [blame] | 5898 | ImplementedClasses.push_back(ID->getClassInterface()); |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5899 | |
Daniel Dunbar | 9a017d7 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 5900 | // Determine if this class is also "non-lazy". |
| 5901 | if (ImplementationIsNonLazy(ID)) |
| 5902 | DefinedNonLazyClasses.push_back(ClassMD); |
| 5903 | |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5904 | // Force the definition of the EHType if necessary. |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5905 | if (flags & NonFragileABI_Class_Exception) |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5906 | GetInterfaceEHType(ID->getClassInterface(), true); |
Fariborz Jahanian | c057794 | 2011-04-22 22:02:28 +0000 | [diff] [blame] | 5907 | // Make sure method definition entries are all clear for next implementation. |
| 5908 | MethodDefinitions.clear(); |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5909 | } |
| 5910 | |
Fariborz Jahanian | 097feda | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5911 | /// GenerateProtocolRef - This routine is called to generate code for |
| 5912 | /// a protocol reference expression; as in: |
| 5913 | /// @code |
| 5914 | /// @protocol(Proto1); |
| 5915 | /// @endcode |
| 5916 | /// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1 |
| 5917 | /// which will hold address of the protocol meta-data. |
| 5918 | /// |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5919 | llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CodeGenFunction &CGF, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5920 | const ObjCProtocolDecl *PD) { |
| 5921 | |
Fariborz Jahanian | 464423d | 2009-04-10 18:47:34 +0000 | [diff] [blame] | 5922 | // This routine is called for @protocol only. So, we must build definition |
| 5923 | // of protocol's meta-data (not a reference to it!) |
| 5924 | // |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5925 | llvm::Constant *Init = |
| 5926 | llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD), |
Douglas Gregor | 020de32 | 2012-01-17 18:36:30 +0000 | [diff] [blame] | 5927 | ObjCTypes.getExternalProtocolPtrTy()); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5928 | |
Fariborz Jahanian | 097feda | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5929 | std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_"); |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 5930 | ProtocolName += PD->getName(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5931 | |
Fariborz Jahanian | 097feda | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5932 | llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName); |
| 5933 | if (PTGV) |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5934 | return CGF.Builder.CreateLoad(PTGV); |
Fariborz Jahanian | 097feda | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5935 | PTGV = new llvm::GlobalVariable( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5936 | CGM.getModule(), |
| 5937 | Init->getType(), false, |
Rafael Espindola | 70efc5b | 2014-03-06 18:54:12 +0000 | [diff] [blame] | 5938 | llvm::GlobalValue::WeakAnyLinkage, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5939 | Init, |
| 5940 | ProtocolName); |
Fariborz Jahanian | 097feda | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5941 | PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip"); |
Rafael Espindola | 70efc5b | 2014-03-06 18:54:12 +0000 | [diff] [blame] | 5942 | PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 5943 | CGM.addCompilerUsedGlobal(PTGV); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5944 | return CGF.Builder.CreateLoad(PTGV); |
Fariborz Jahanian | 097feda | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5945 | } |
| 5946 | |
Fariborz Jahanian | 0c8d060 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5947 | /// GenerateCategory - Build metadata for a category implementation. |
| 5948 | /// struct _category_t { |
| 5949 | /// const char * const name; |
| 5950 | /// struct _class_t *const cls; |
| 5951 | /// const struct _method_list_t * const instance_methods; |
| 5952 | /// const struct _method_list_t * const class_methods; |
| 5953 | /// const struct _protocol_list_t * const protocols; |
| 5954 | /// const struct _prop_list_t * const properties; |
| 5955 | /// } |
| 5956 | /// |
Daniel Dunbar | 9a017d7 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 5957 | void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) { |
Fariborz Jahanian | 0c8d060 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5958 | const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); |
Fariborz Jahanian | 2612e14 | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5959 | const char *Prefix = "\01l_OBJC_$_CATEGORY_"; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5960 | std::string ExtCatName(Prefix + Interface->getNameAsString()+ |
| 5961 | "_$_" + OCD->getNameAsString()); |
| 5962 | std::string ExtClassName(getClassSymbolPrefix() + |
Daniel Dunbar | 15894b7 | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5963 | Interface->getNameAsString()); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5964 | |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 5965 | llvm::Constant *Values[6]; |
Fariborz Jahanian | 0c8d060 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5966 | Values[0] = GetClassName(OCD->getIdentifier()); |
| 5967 | // meta-class entry symbol |
Rafael Espindola | 554256c | 2014-02-26 22:25:45 +0000 | [diff] [blame] | 5968 | llvm::GlobalVariable *ClassGV = |
| 5969 | GetClassGlobal(ExtClassName, Interface->isWeakImported()); |
| 5970 | |
Fariborz Jahanian | 0c8d060 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5971 | Values[1] = ClassGV; |
Fariborz Jahanian | 2612e14 | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5972 | std::vector<llvm::Constant*> Methods; |
| 5973 | std::string MethodListName(Prefix); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5974 | MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() + |
Fariborz Jahanian | 2612e14 | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5975 | "_$_" + OCD->getNameAsString(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5976 | |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 5977 | for (const auto *I : OCD->instance_methods()) |
Fariborz Jahanian | 2612e14 | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5978 | // Instance methods should always be defined. |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 5979 | Methods.push_back(GetMethodConstant(I)); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5980 | |
| 5981 | Values[2] = EmitMethodList(MethodListName, |
| 5982 | "__DATA, __objc_const", |
Fariborz Jahanian | 2612e14 | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5983 | Methods); |
| 5984 | |
| 5985 | MethodListName = Prefix; |
| 5986 | MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" + |
| 5987 | OCD->getNameAsString(); |
| 5988 | Methods.clear(); |
Aaron Ballman | e8a7dc9 | 2014-03-13 20:11:06 +0000 | [diff] [blame] | 5989 | for (const auto *I : OCD->class_methods()) |
Fariborz Jahanian | 2612e14 | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5990 | // Class methods should always be defined. |
Aaron Ballman | e8a7dc9 | 2014-03-13 20:11:06 +0000 | [diff] [blame] | 5991 | Methods.push_back(GetMethodConstant(I)); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5992 | |
| 5993 | Values[3] = EmitMethodList(MethodListName, |
| 5994 | "__DATA, __objc_const", |
Fariborz Jahanian | 2612e14 | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5995 | Methods); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5996 | const ObjCCategoryDecl *Category = |
Fariborz Jahanian | 066347e | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 5997 | Interface->FindCategoryDeclaration(OCD->getIdentifier()); |
Fariborz Jahanian | d8fc105 | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 5998 | if (Category) { |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 5999 | SmallString<256> ExtName; |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 6000 | llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_" |
| 6001 | << OCD->getName(); |
Fariborz Jahanian | d8fc105 | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 6002 | Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_" |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 6003 | + Interface->getName() + "_$_" |
| 6004 | + Category->getName(), |
Fariborz Jahanian | d8fc105 | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 6005 | Category->protocol_begin(), |
| 6006 | Category->protocol_end()); |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 6007 | Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(), |
| 6008 | OCD, Category, ObjCTypes); |
Mike Stump | 658fe02 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 6009 | } else { |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 6010 | Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy); |
| 6011 | Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
Fariborz Jahanian | d8fc105 | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 6012 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6013 | |
| 6014 | llvm::Constant *Init = |
| 6015 | llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy, |
Fariborz Jahanian | 0c8d060 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 6016 | Values); |
| 6017 | llvm::GlobalVariable *GCATV |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6018 | = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy, |
Fariborz Jahanian | 0c8d060 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 6019 | false, |
Rafael Espindola | 5179a4e | 2014-02-27 19:01:11 +0000 | [diff] [blame] | 6020 | llvm::GlobalValue::PrivateLinkage, |
Fariborz Jahanian | 0c8d060 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 6021 | Init, |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 6022 | ExtCatName); |
Rafael Espindola | 21039aa | 2014-02-27 16:26:32 +0000 | [diff] [blame] | 6023 | assertPrivateName(GCATV); |
Fariborz Jahanian | c22f236 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 6024 | GCATV->setAlignment( |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 6025 | CGM.getDataLayout().getABITypeAlignment(ObjCTypes.CategorynfABITy)); |
Fariborz Jahanian | 40a4bcd | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 6026 | GCATV->setSection("__DATA, __objc_const"); |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 6027 | CGM.addCompilerUsedGlobal(GCATV); |
Fariborz Jahanian | 0c8d060 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 6028 | DefinedCategories.push_back(GCATV); |
Daniel Dunbar | 9a017d7 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 6029 | |
| 6030 | // Determine if this category is also "non-lazy". |
| 6031 | if (ImplementationIsNonLazy(OCD)) |
| 6032 | DefinedNonLazyCategories.push_back(GCATV); |
Fariborz Jahanian | c057794 | 2011-04-22 22:02:28 +0000 | [diff] [blame] | 6033 | // method definition entries must be clear for next implementation. |
| 6034 | MethodDefinitions.clear(); |
Fariborz Jahanian | 0c8d060 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 6035 | } |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 6036 | |
| 6037 | /// GetMethodConstant - Return a struct objc_method constant for the |
| 6038 | /// given method if it has been defined. The result is null if the |
| 6039 | /// method has not been defined. The return value has type MethodPtrTy. |
| 6040 | llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6041 | const ObjCMethodDecl *MD) { |
Argyrios Kyrtzidis | 13257c5 | 2010-08-09 10:54:20 +0000 | [diff] [blame] | 6042 | llvm::Function *Fn = GetMethodDefinition(MD); |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 6043 | if (!Fn) |
| 6044 | return 0; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6045 | |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 6046 | llvm::Constant *Method[] = { |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 6047 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 6048 | ObjCTypes.SelectorPtrTy), |
| 6049 | GetMethodVarType(MD), |
| 6050 | llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy) |
| 6051 | }; |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 6052 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method); |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 6053 | } |
| 6054 | |
| 6055 | /// EmitMethodList - Build meta-data for method declarations |
| 6056 | /// struct _method_list_t { |
| 6057 | /// uint32_t entsize; // sizeof(struct _objc_method) |
| 6058 | /// uint32_t method_count; |
| 6059 | /// struct _objc_method method_list[method_count]; |
| 6060 | /// } |
| 6061 | /// |
Bill Wendling | cb5b5ff | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 6062 | llvm::Constant * |
| 6063 | CGObjCNonFragileABIMac::EmitMethodList(Twine Name, |
| 6064 | const char *Section, |
| 6065 | ArrayRef<llvm::Constant*> Methods) { |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 6066 | // Return null for empty list. |
| 6067 | if (Methods.empty()) |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 6068 | return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6069 | |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 6070 | llvm::Constant *Values[3]; |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 6071 | // sizeof(struct _objc_method) |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 6072 | unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.MethodTy); |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 6073 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 6074 | // method_count |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 6075 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 6076 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy, |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 6077 | Methods.size()); |
Owen Anderson | 47034e1 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 6078 | Values[2] = llvm::ConstantArray::get(AT, Methods); |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 6079 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6080 | |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 6081 | llvm::GlobalVariable *GV = |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 6082 | new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Rafael Espindola | 5179a4e | 2014-02-27 19:01:11 +0000 | [diff] [blame] | 6083 | llvm::GlobalValue::PrivateLinkage, Init, Name); |
Rafael Espindola | 21039aa | 2014-02-27 16:26:32 +0000 | [diff] [blame] | 6084 | assertPrivateName(GV); |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 6085 | GV->setAlignment(CGM.getDataLayout().getABITypeAlignment(Init->getType())); |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 6086 | GV->setSection(Section); |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 6087 | CGM.addCompilerUsedGlobal(GV); |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 6088 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListnfABIPtrTy); |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 6089 | } |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 6090 | |
Fariborz Jahanian | 4e7ae06 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 6091 | /// ObjCIvarOffsetVariable - Returns the ivar offset variable for |
| 6092 | /// the given ivar. |
Daniel Dunbar | 8c7f981 | 2010-04-02 21:14:02 +0000 | [diff] [blame] | 6093 | llvm::GlobalVariable * |
| 6094 | CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID, |
| 6095 | const ObjCIvarDecl *Ivar) { |
| 6096 | const ObjCInterfaceDecl *Container = Ivar->getContainingInterface(); |
Daniel Dunbar | 3f86b9c | 2009-05-05 00:36:57 +0000 | [diff] [blame] | 6097 | std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() + |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 6098 | '.' + Ivar->getNameAsString(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6099 | llvm::GlobalVariable *IvarOffsetGV = |
Fariborz Jahanian | 4e7ae06 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 6100 | CGM.getModule().getGlobalVariable(Name); |
| 6101 | if (!IvarOffsetGV) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6102 | IvarOffsetGV = |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 6103 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy, |
Fariborz Jahanian | 4e7ae06 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 6104 | false, |
| 6105 | llvm::GlobalValue::ExternalLinkage, |
| 6106 | 0, |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 6107 | Name); |
Fariborz Jahanian | 4e7ae06 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 6108 | return IvarOffsetGV; |
| 6109 | } |
| 6110 | |
Daniel Dunbar | 8c7f981 | 2010-04-02 21:14:02 +0000 | [diff] [blame] | 6111 | llvm::Constant * |
| 6112 | CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID, |
| 6113 | const ObjCIvarDecl *Ivar, |
Eli Friedman | 8cbca20 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 6114 | unsigned long int Offset) { |
Daniel Dunbar | bf90b33 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 6115 | llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6116 | IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy, |
Eli Friedman | 8cbca20 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 6117 | Offset)); |
Fariborz Jahanian | c22f236 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 6118 | IvarOffsetGV->setAlignment( |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 6119 | CGM.getDataLayout().getABITypeAlignment(ObjCTypes.LongTy)); |
Daniel Dunbar | bf90b33 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 6120 | |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 6121 | // FIXME: This matches gcc, but shouldn't the visibility be set on the use as |
| 6122 | // well (i.e., in ObjCIvarOffsetVariable). |
Daniel Dunbar | bf90b33 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 6123 | if (Ivar->getAccessControl() == ObjCIvarDecl::Private || |
| 6124 | Ivar->getAccessControl() == ObjCIvarDecl::Package || |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 6125 | ID->getVisibility() == HiddenVisibility) |
Fariborz Jahanian | 3d3426f | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 6126 | IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | f5f359f | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 6127 | else |
Fariborz Jahanian | bc3c77b | 2009-04-06 18:30:00 +0000 | [diff] [blame] | 6128 | IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility); |
Bill Wendling | f7d4598 | 2011-05-04 21:37:25 +0000 | [diff] [blame] | 6129 | IvarOffsetGV->setSection("__DATA, __objc_ivar"); |
Fariborz Jahanian | c88a70d | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 6130 | return IvarOffsetGV; |
Fariborz Jahanian | 40a4bcd | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 6131 | } |
| 6132 | |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 6133 | /// EmitIvarList - Emit the ivar list for the given |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 6134 | /// implementation. The return value has type |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 6135 | /// IvarListnfABIPtrTy. |
| 6136 | /// struct _ivar_t { |
| 6137 | /// unsigned long int *offset; // pointer to ivar offset location |
| 6138 | /// char *name; |
| 6139 | /// char *type; |
| 6140 | /// uint32_t alignment; |
| 6141 | /// uint32_t size; |
| 6142 | /// } |
| 6143 | /// struct _ivar_list_t { |
| 6144 | /// uint32 entsize; // sizeof(struct _ivar_t) |
| 6145 | /// uint32 count; |
| 6146 | /// struct _iver_t list[count]; |
| 6147 | /// } |
| 6148 | /// |
Daniel Dunbar | f5c1846 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 6149 | |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 6150 | llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6151 | const ObjCImplementationDecl *ID) { |
| 6152 | |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 6153 | std::vector<llvm::Constant*> Ivars; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6154 | |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 6155 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 6156 | assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6157 | |
Fariborz Jahanian | 40a4bcd | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 6158 | // FIXME. Consolidate this with similar code in GenerateClass. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6159 | |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 6160 | for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin(); |
Fariborz Jahanian | b26d578 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 6161 | IVD; IVD = IVD->getNextIvar()) { |
Fariborz Jahanian | 7c80959 | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 6162 | // Ignore unnamed bit-fields. |
| 6163 | if (!IVD->getDeclName()) |
| 6164 | continue; |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 6165 | llvm::Constant *Ivar[5]; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6166 | Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD, |
Daniel Dunbar | 96120237 | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 6167 | ComputeIvarBaseOffset(CGM, ID, IVD)); |
Daniel Dunbar | 725dc2c | 2009-04-22 08:22:17 +0000 | [diff] [blame] | 6168 | Ivar[1] = GetMethodVarName(IVD->getIdentifier()); |
| 6169 | Ivar[2] = GetMethodVarType(IVD); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 6170 | llvm::Type *FieldTy = |
Daniel Dunbar | 725dc2c | 2009-04-22 08:22:17 +0000 | [diff] [blame] | 6171 | CGM.getTypes().ConvertTypeForMem(IVD->getType()); |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 6172 | unsigned Size = CGM.getDataLayout().getTypeAllocSize(FieldTy); |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 6173 | unsigned Align = CGM.getContext().getPreferredTypeAlign( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6174 | IVD->getType().getTypePtr()) >> 3; |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 6175 | Align = llvm::Log2_32(Align); |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 6176 | Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align); |
Daniel Dunbar | ae03226 | 2009-04-20 00:33:43 +0000 | [diff] [blame] | 6177 | // NOTE. Size of a bitfield does not match gcc's, because of the |
| 6178 | // way bitfields are treated special in each. But I am told that |
| 6179 | // 'size' for bitfield ivars is ignored by the runtime so it does |
| 6180 | // not matter. If it matters, there is enough info to get the |
| 6181 | // bitfield right! |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 6182 | Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 6183 | Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar)); |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 6184 | } |
| 6185 | // Return null for empty list. |
| 6186 | if (Ivars.empty()) |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 6187 | return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy); |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 6188 | |
| 6189 | llvm::Constant *Values[3]; |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 6190 | unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.IvarnfABITy); |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 6191 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 6192 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 6193 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy, |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 6194 | Ivars.size()); |
Owen Anderson | 47034e1 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 6195 | Values[2] = llvm::ConstantArray::get(AT, Ivars); |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 6196 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 6197 | const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_"; |
| 6198 | llvm::GlobalVariable *GV = |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 6199 | new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Rafael Espindola | 5179a4e | 2014-02-27 19:01:11 +0000 | [diff] [blame] | 6200 | llvm::GlobalValue::PrivateLinkage, |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 6201 | Init, |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 6202 | Prefix + OID->getName()); |
Rafael Espindola | 21039aa | 2014-02-27 16:26:32 +0000 | [diff] [blame] | 6203 | assertPrivateName(GV); |
Fariborz Jahanian | c22f236 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 6204 | GV->setAlignment( |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 6205 | CGM.getDataLayout().getABITypeAlignment(Init->getType())); |
Fariborz Jahanian | 40a4bcd | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 6206 | GV->setSection("__DATA, __objc_const"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6207 | |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 6208 | CGM.addCompilerUsedGlobal(GV); |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 6209 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy); |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6210 | } |
| 6211 | |
| 6212 | llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6213 | const ObjCProtocolDecl *PD) { |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6214 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6215 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6216 | if (!Entry) { |
| 6217 | // We use the initializer as a marker of whether this is a forward |
| 6218 | // reference or not. At module finalization we add the empty |
| 6219 | // contents for protocols which were referenced but never defined. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6220 | Entry = |
Rafael Espindola | 5d117f3 | 2014-03-06 01:10:46 +0000 | [diff] [blame] | 6221 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, |
Rafael Espindola | 70efc5b | 2014-03-06 18:54:12 +0000 | [diff] [blame] | 6222 | false, llvm::GlobalValue::WeakAnyLinkage, 0, |
Rafael Espindola | 5d117f3 | 2014-03-06 01:10:46 +0000 | [diff] [blame] | 6223 | "\01l_OBJC_PROTOCOL_$_" + PD->getName()); |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6224 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6225 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6226 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6227 | return Entry; |
| 6228 | } |
| 6229 | |
| 6230 | /// GetOrEmitProtocol - Generate the protocol meta-data: |
| 6231 | /// @code |
| 6232 | /// struct _protocol_t { |
| 6233 | /// id isa; // NULL |
| 6234 | /// const char * const protocol_name; |
| 6235 | /// const struct _protocol_list_t * protocol_list; // super protocols |
| 6236 | /// const struct method_list_t * const instance_methods; |
| 6237 | /// const struct method_list_t * const class_methods; |
| 6238 | /// const struct method_list_t *optionalInstanceMethods; |
| 6239 | /// const struct method_list_t *optionalClassMethods; |
| 6240 | /// const struct _prop_list_t * properties; |
| 6241 | /// const uint32_t size; // sizeof(struct _protocol_t) |
| 6242 | /// const uint32_t flags; // = 0 |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 6243 | /// const char ** extendedMethodTypes; |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6244 | /// } |
| 6245 | /// @endcode |
| 6246 | /// |
| 6247 | |
| 6248 | llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6249 | const ObjCProtocolDecl *PD) { |
John McCall | f9582a7 | 2012-03-30 21:29:05 +0000 | [diff] [blame] | 6250 | llvm::GlobalVariable *Entry = Protocols[PD->getIdentifier()]; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6251 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6252 | // Early exit if a defining object has already been generated. |
| 6253 | if (Entry && Entry->hasInitializer()) |
| 6254 | return Entry; |
| 6255 | |
Douglas Gregor | a715bff | 2012-01-01 19:51:50 +0000 | [diff] [blame] | 6256 | // Use the protocol definition, if there is one. |
| 6257 | if (const ObjCProtocolDecl *Def = PD->getDefinition()) |
| 6258 | PD = Def; |
| 6259 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6260 | // Construct method lists. |
| 6261 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 6262 | std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 6263 | std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt; |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 6264 | for (const auto *MD : PD->instance_methods()) { |
Fariborz Jahanian | d9c28b8 | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 6265 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 6266 | if (!C) |
| 6267 | return GetOrEmitProtocolRef(PD); |
| 6268 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6269 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 6270 | OptInstanceMethods.push_back(C); |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 6271 | OptMethodTypesExt.push_back(GetMethodVarType(MD, true)); |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6272 | } else { |
| 6273 | InstanceMethods.push_back(C); |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 6274 | MethodTypesExt.push_back(GetMethodVarType(MD, true)); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6275 | } |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6276 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6277 | |
Aaron Ballman | e8a7dc9 | 2014-03-13 20:11:06 +0000 | [diff] [blame] | 6278 | for (const auto *MD : PD->class_methods()) { |
Fariborz Jahanian | d9c28b8 | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 6279 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 6280 | if (!C) |
| 6281 | return GetOrEmitProtocolRef(PD); |
| 6282 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6283 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 6284 | OptClassMethods.push_back(C); |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 6285 | OptMethodTypesExt.push_back(GetMethodVarType(MD, true)); |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6286 | } else { |
| 6287 | ClassMethods.push_back(C); |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 6288 | MethodTypesExt.push_back(GetMethodVarType(MD, true)); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6289 | } |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6290 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6291 | |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 6292 | MethodTypesExt.insert(MethodTypesExt.end(), |
| 6293 | OptMethodTypesExt.begin(), OptMethodTypesExt.end()); |
| 6294 | |
| 6295 | llvm::Constant *Values[11]; |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6296 | // isa is NULL |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 6297 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy); |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6298 | Values[1] = GetClassName(PD->getIdentifier()); |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 6299 | Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(), |
| 6300 | PD->protocol_begin(), |
| 6301 | PD->protocol_end()); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6302 | |
Fariborz Jahanian | d9c28b8 | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 6303 | Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_" |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 6304 | + PD->getName(), |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6305 | "__DATA, __objc_const", |
| 6306 | InstanceMethods); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6307 | Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_" |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 6308 | + PD->getName(), |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6309 | "__DATA, __objc_const", |
| 6310 | ClassMethods); |
Fariborz Jahanian | d9c28b8 | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 6311 | Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_" |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 6312 | + PD->getName(), |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6313 | "__DATA, __objc_const", |
| 6314 | OptInstanceMethods); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6315 | Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_" |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 6316 | + PD->getName(), |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6317 | "__DATA, __objc_const", |
| 6318 | OptClassMethods); |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 6319 | Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(), |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6320 | 0, PD, ObjCTypes); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6321 | uint32_t Size = |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 6322 | CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ProtocolnfABITy); |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 6323 | Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 6324 | Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy); |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 6325 | Values[10] = EmitProtocolMethodTypes("\01l_OBJC_$_PROTOCOL_METHOD_TYPES_" |
| 6326 | + PD->getName(), |
| 6327 | MethodTypesExt, ObjCTypes); |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 6328 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy, |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6329 | Values); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6330 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6331 | if (Entry) { |
Rafael Espindola | 5d117f3 | 2014-03-06 01:10:46 +0000 | [diff] [blame] | 6332 | // Already created, update the initializer. |
Rafael Espindola | 70efc5b | 2014-03-06 18:54:12 +0000 | [diff] [blame] | 6333 | assert(Entry->getLinkage() == llvm::GlobalValue::WeakAnyLinkage); |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6334 | Entry->setInitializer(Init); |
| 6335 | } else { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6336 | Entry = |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 6337 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, |
Rafael Espindola | 70efc5b | 2014-03-06 18:54:12 +0000 | [diff] [blame] | 6338 | false, llvm::GlobalValue::WeakAnyLinkage, Init, |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 6339 | "\01l_OBJC_PROTOCOL_$_" + PD->getName()); |
Fariborz Jahanian | c22f236 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 6340 | Entry->setAlignment( |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 6341 | CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ProtocolnfABITy)); |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6342 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
John McCall | f9582a7 | 2012-03-30 21:29:05 +0000 | [diff] [blame] | 6343 | |
| 6344 | Protocols[PD->getIdentifier()] = Entry; |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6345 | } |
Rafael Espindola | 70efc5b | 2014-03-06 18:54:12 +0000 | [diff] [blame] | 6346 | Entry->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 6347 | CGM.addCompilerUsedGlobal(Entry); |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 6348 | |
Fariborz Jahanian | 61cd4b5 | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 6349 | // Use this protocol meta-data to build protocol list table in section |
| 6350 | // __DATA, __objc_protolist |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 6351 | llvm::GlobalVariable *PTGV = |
| 6352 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy, |
Rafael Espindola | 70efc5b | 2014-03-06 18:54:12 +0000 | [diff] [blame] | 6353 | false, llvm::GlobalValue::WeakAnyLinkage, Entry, |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 6354 | "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName()); |
Fariborz Jahanian | c22f236 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 6355 | PTGV->setAlignment( |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 6356 | CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy)); |
Daniel Dunbar | b25452a | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 6357 | PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip"); |
Rafael Espindola | 70efc5b | 2014-03-06 18:54:12 +0000 | [diff] [blame] | 6358 | PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 6359 | CGM.addCompilerUsedGlobal(PTGV); |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6360 | return Entry; |
| 6361 | } |
| 6362 | |
| 6363 | /// EmitProtocolList - Generate protocol list meta-data: |
| 6364 | /// @code |
| 6365 | /// struct _protocol_list_t { |
| 6366 | /// long protocol_count; // Note, this is 32/64 bit |
| 6367 | /// struct _protocol_t[protocol_count]; |
| 6368 | /// } |
| 6369 | /// @endcode |
| 6370 | /// |
| 6371 | llvm::Constant * |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6372 | CGObjCNonFragileABIMac::EmitProtocolList(Twine Name, |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 6373 | ObjCProtocolDecl::protocol_iterator begin, |
| 6374 | ObjCProtocolDecl::protocol_iterator end) { |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 6375 | SmallVector<llvm::Constant *, 16> ProtocolRefs; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6376 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6377 | // Just return null for empty protocol lists |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6378 | if (begin == end) |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 6379 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6380 | |
Daniel Dunbar | 8de90f0 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 6381 | // FIXME: We shouldn't need to do this lookup here, should we? |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 6382 | SmallString<256> TmpName; |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 6383 | Name.toVector(TmpName); |
| 6384 | llvm::GlobalVariable *GV = |
| 6385 | CGM.getModule().getGlobalVariable(TmpName.str(), true); |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6386 | if (GV) |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 6387 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6388 | |
Daniel Dunbar | 8de90f0 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 6389 | for (; begin != end; ++begin) |
| 6390 | ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented??? |
| 6391 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6392 | // This list is null terminated. |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 6393 | ProtocolRefs.push_back(llvm::Constant::getNullValue( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6394 | ObjCTypes.ProtocolnfABIPtrTy)); |
| 6395 | |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 6396 | llvm::Constant *Values[2]; |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 6397 | Values[0] = |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 6398 | llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6399 | Values[1] = |
Bill Wendling | a515b58 | 2012-02-09 22:16:49 +0000 | [diff] [blame] | 6400 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy, |
| 6401 | ProtocolRefs.size()), |
| 6402 | ProtocolRefs); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6403 | |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 6404 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 6405 | GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Rafael Espindola | 5179a4e | 2014-02-27 19:01:11 +0000 | [diff] [blame] | 6406 | llvm::GlobalValue::PrivateLinkage, |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 6407 | Init, Name); |
Rafael Espindola | 21039aa | 2014-02-27 16:26:32 +0000 | [diff] [blame] | 6408 | assertPrivateName(GV); |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6409 | GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | c22f236 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 6410 | GV->setAlignment( |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 6411 | CGM.getDataLayout().getABITypeAlignment(Init->getType())); |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 6412 | CGM.addCompilerUsedGlobal(GV); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6413 | return llvm::ConstantExpr::getBitCast(GV, |
Daniel Dunbar | 8de90f0 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 6414 | ObjCTypes.ProtocolListnfABIPtrTy); |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6415 | } |
| 6416 | |
Fariborz Jahanian | d9c28b8 | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 6417 | /// GetMethodDescriptionConstant - This routine build following meta-data: |
| 6418 | /// struct _objc_method { |
| 6419 | /// SEL _cmd; |
| 6420 | /// char *method_type; |
| 6421 | /// char *_imp; |
| 6422 | /// } |
| 6423 | |
| 6424 | llvm::Constant * |
| 6425 | CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 6426 | llvm::Constant *Desc[3]; |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 6427 | Desc[0] = |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6428 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 6429 | ObjCTypes.SelectorPtrTy); |
Fariborz Jahanian | d9c28b8 | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 6430 | Desc[1] = GetMethodVarType(MD); |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 6431 | if (!Desc[1]) |
| 6432 | return 0; |
| 6433 | |
Fariborz Jahanian | 097feda | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 6434 | // Protocol methods have no implementation. So, this entry is always NULL. |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 6435 | Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 6436 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc); |
Fariborz Jahanian | d9c28b8 | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 6437 | } |
Fariborz Jahanian | c88a70d | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 6438 | |
| 6439 | /// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference. |
| 6440 | /// This code gen. amounts to generating code for: |
| 6441 | /// @code |
| 6442 | /// (type *)((char *)base + _OBJC_IVAR_$_.ivar; |
| 6443 | /// @encode |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6444 | /// |
Fariborz Jahanian | 712bfa6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 6445 | LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar( |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 6446 | CodeGen::CodeGenFunction &CGF, |
| 6447 | QualType ObjectTy, |
| 6448 | llvm::Value *BaseValue, |
| 6449 | const ObjCIvarDecl *Ivar, |
| 6450 | unsigned CVRQualifiers) { |
| 6451 | ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface(); |
Fariborz Jahanian | caabf1b | 2012-02-20 22:42:22 +0000 | [diff] [blame] | 6452 | llvm::Value *Offset = EmitIvarOffset(CGF, ID, Ivar); |
Saleem Abdulrasool | 5f25bc3 | 2013-02-17 04:03:34 +0000 | [diff] [blame] | 6453 | |
Fariborz Jahanian | dafffbe | 2014-03-04 18:34:52 +0000 | [diff] [blame] | 6454 | if (IsIvarOffsetKnownIdempotent(CGF, Ivar)) |
Saleem Abdulrasool | 5f25bc3 | 2013-02-17 04:03:34 +0000 | [diff] [blame] | 6455 | if (llvm::LoadInst *LI = cast<llvm::LoadInst>(Offset)) |
| 6456 | LI->setMetadata(CGM.getModule().getMDKindID("invariant.load"), |
| 6457 | llvm::MDNode::get(VMContext, ArrayRef<llvm::Value*>())); |
| 6458 | |
Daniel Dunbar | 9fd114d | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 6459 | return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers, |
Fariborz Jahanian | caabf1b | 2012-02-20 22:42:22 +0000 | [diff] [blame] | 6460 | Offset); |
Fariborz Jahanian | c88a70d | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 6461 | } |
| 6462 | |
Fariborz Jahanian | 21fc74c | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 6463 | llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6464 | CodeGen::CodeGenFunction &CGF, |
| 6465 | const ObjCInterfaceDecl *Interface, |
| 6466 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | c76493a | 2009-11-29 21:23:36 +0000 | [diff] [blame] | 6467 | return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar"); |
Fariborz Jahanian | 21fc74c | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 6468 | } |
| 6469 | |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6470 | static void appendSelectorForMessageRefTable(std::string &buffer, |
| 6471 | Selector selector) { |
| 6472 | if (selector.isUnarySelector()) { |
| 6473 | buffer += selector.getNameForSlot(0); |
| 6474 | return; |
| 6475 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6476 | |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6477 | for (unsigned i = 0, e = selector.getNumArgs(); i != e; ++i) { |
| 6478 | buffer += selector.getNameForSlot(i); |
| 6479 | buffer += '_'; |
| 6480 | } |
| 6481 | } |
| 6482 | |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 6483 | /// Emit a "v-table" message send. We emit a weak hidden-visibility |
| 6484 | /// struct, initially containing the selector pointer and a pointer to |
| 6485 | /// a "fixup" variant of the appropriate objc_msgSend. To call, we |
| 6486 | /// load and call the function pointer, passing the address of the |
| 6487 | /// struct as the second parameter. The runtime determines whether |
| 6488 | /// the selector is currently emitted using vtable dispatch; if so, it |
| 6489 | /// substitutes a stub function which simply tail-calls through the |
| 6490 | /// appropriate vtable slot, and if not, it substitues a stub function |
| 6491 | /// which tail-calls objc_msgSend. Both stubs adjust the selector |
| 6492 | /// argument to correctly point to the selector. |
| 6493 | RValue |
| 6494 | CGObjCNonFragileABIMac::EmitVTableMessageSend(CodeGenFunction &CGF, |
| 6495 | ReturnValueSlot returnSlot, |
| 6496 | QualType resultType, |
| 6497 | Selector selector, |
| 6498 | llvm::Value *arg0, |
| 6499 | QualType arg0Type, |
| 6500 | bool isSuper, |
| 6501 | const CallArgList &formalArgs, |
| 6502 | const ObjCMethodDecl *method) { |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6503 | // Compute the actual arguments. |
| 6504 | CallArgList args; |
| 6505 | |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 6506 | // First argument: the receiver / super-call structure. |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6507 | if (!isSuper) |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 6508 | arg0 = CGF.Builder.CreateBitCast(arg0, ObjCTypes.ObjectPtrTy); |
| 6509 | args.add(RValue::get(arg0), arg0Type); |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6510 | |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 6511 | // Second argument: a pointer to the message ref structure. Leave |
| 6512 | // the actual argument value blank for now. |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6513 | args.add(RValue::get(0), ObjCTypes.MessageRefCPtrTy); |
| 6514 | |
| 6515 | args.insert(args.end(), formalArgs.begin(), formalArgs.end()); |
| 6516 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 6517 | MessageSendInfo MSI = getMessageSendInfo(method, resultType, args); |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6518 | |
John McCall | 5880fb8 | 2011-05-14 21:12:11 +0000 | [diff] [blame] | 6519 | NullReturnState nullReturn; |
| 6520 | |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 6521 | // Find the function to call and the mangled name for the message |
| 6522 | // ref structure. Using a different mangled name wouldn't actually |
| 6523 | // be a problem; it would just be a waste. |
| 6524 | // |
| 6525 | // The runtime currently never uses vtable dispatch for anything |
| 6526 | // except normal, non-super message-sends. |
| 6527 | // FIXME: don't use this for that. |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6528 | llvm::Constant *fn = 0; |
| 6529 | std::string messageRefName("\01l_"); |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 6530 | if (CGM.ReturnTypeUsesSRet(MSI.CallInfo)) { |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6531 | if (isSuper) { |
| 6532 | fn = ObjCTypes.getMessageSendSuper2StretFixupFn(); |
| 6533 | messageRefName += "objc_msgSendSuper2_stret_fixup"; |
Chris Lattner | 396639d | 2010-08-18 16:09:06 +0000 | [diff] [blame] | 6534 | } else { |
John McCall | 5880fb8 | 2011-05-14 21:12:11 +0000 | [diff] [blame] | 6535 | nullReturn.init(CGF, arg0); |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6536 | fn = ObjCTypes.getMessageSendStretFixupFn(); |
| 6537 | messageRefName += "objc_msgSend_stret_fixup"; |
Chris Lattner | 396639d | 2010-08-18 16:09:06 +0000 | [diff] [blame] | 6538 | } |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6539 | } else if (!isSuper && CGM.ReturnTypeUsesFPRet(resultType)) { |
| 6540 | fn = ObjCTypes.getMessageSendFpretFixupFn(); |
| 6541 | messageRefName += "objc_msgSend_fpret_fixup"; |
Mike Stump | 658fe02 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 6542 | } else { |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6543 | if (isSuper) { |
| 6544 | fn = ObjCTypes.getMessageSendSuper2FixupFn(); |
| 6545 | messageRefName += "objc_msgSendSuper2_fixup"; |
Chris Lattner | 396639d | 2010-08-18 16:09:06 +0000 | [diff] [blame] | 6546 | } else { |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6547 | fn = ObjCTypes.getMessageSendFixupFn(); |
| 6548 | messageRefName += "objc_msgSend_fixup"; |
Chris Lattner | 396639d | 2010-08-18 16:09:06 +0000 | [diff] [blame] | 6549 | } |
Fariborz Jahanian | e4dc35d | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 6550 | } |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6551 | assert(fn && "CGObjCNonFragileABIMac::EmitMessageSend"); |
| 6552 | messageRefName += '_'; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6553 | |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6554 | // Append the selector name, except use underscores anywhere we |
| 6555 | // would have used colons. |
| 6556 | appendSelectorForMessageRefTable(messageRefName, selector); |
| 6557 | |
| 6558 | llvm::GlobalVariable *messageRef |
| 6559 | = CGM.getModule().getGlobalVariable(messageRefName); |
| 6560 | if (!messageRef) { |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 6561 | // Build the message ref structure. |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6562 | llvm::Constant *values[] = { fn, GetMethodVarName(selector) }; |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 6563 | llvm::Constant *init = llvm::ConstantStruct::getAnon(values); |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6564 | messageRef = new llvm::GlobalVariable(CGM.getModule(), |
| 6565 | init->getType(), |
| 6566 | /*constant*/ false, |
Rafael Espindola | 70efc5b | 2014-03-06 18:54:12 +0000 | [diff] [blame] | 6567 | llvm::GlobalValue::WeakAnyLinkage, |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6568 | init, |
| 6569 | messageRefName); |
Rafael Espindola | 70efc5b | 2014-03-06 18:54:12 +0000 | [diff] [blame] | 6570 | messageRef->setVisibility(llvm::GlobalValue::HiddenVisibility); |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6571 | messageRef->setAlignment(16); |
| 6572 | messageRef->setSection("__DATA, __objc_msgrefs, coalesced"); |
| 6573 | } |
Rafael Espindola | 70efc5b | 2014-03-06 18:54:12 +0000 | [diff] [blame] | 6574 | |
Fariborz Jahanian | c93fa98 | 2012-01-30 23:39:30 +0000 | [diff] [blame] | 6575 | bool requiresnullCheck = false; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6576 | if (CGM.getLangOpts().ObjCAutoRefCount && method) |
Aaron Ballman | 43b68be | 2014-03-07 17:50:17 +0000 | [diff] [blame] | 6577 | for (const auto *ParamDecl : method->params()) { |
Fariborz Jahanian | c93fa98 | 2012-01-30 23:39:30 +0000 | [diff] [blame] | 6578 | if (ParamDecl->hasAttr<NSConsumedAttr>()) { |
| 6579 | if (!nullReturn.NullBB) |
| 6580 | nullReturn.init(CGF, arg0); |
| 6581 | requiresnullCheck = true; |
| 6582 | break; |
| 6583 | } |
| 6584 | } |
| 6585 | |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6586 | llvm::Value *mref = |
| 6587 | CGF.Builder.CreateBitCast(messageRef, ObjCTypes.MessageRefPtrTy); |
| 6588 | |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 6589 | // Update the message ref argument. |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6590 | args[1].RV = RValue::get(mref); |
| 6591 | |
| 6592 | // Load the function to call from the message ref table. |
| 6593 | llvm::Value *callee = CGF.Builder.CreateStructGEP(mref, 0); |
| 6594 | callee = CGF.Builder.CreateLoad(callee, "msgSend_fn"); |
| 6595 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 6596 | callee = CGF.Builder.CreateBitCast(callee, MSI.MessengerType); |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6597 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 6598 | RValue result = CGF.EmitCall(MSI.CallInfo, callee, returnSlot, args); |
Fariborz Jahanian | c93fa98 | 2012-01-30 23:39:30 +0000 | [diff] [blame] | 6599 | return nullReturn.complete(CGF, result, resultType, formalArgs, |
| 6600 | requiresnullCheck ? method : 0); |
Fariborz Jahanian | 3d9296e | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 6601 | } |
| 6602 | |
| 6603 | /// Generate code for a message send expression in the nonfragile abi. |
Daniel Dunbar | aff9fca | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 6604 | CodeGen::RValue |
| 6605 | CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
John McCall | 78a1511 | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 6606 | ReturnValueSlot Return, |
Daniel Dunbar | aff9fca | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 6607 | QualType ResultType, |
| 6608 | Selector Sel, |
| 6609 | llvm::Value *Receiver, |
Daniel Dunbar | aff9fca | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 6610 | const CallArgList &CallArgs, |
David Chisnall | 01aa467 | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 6611 | const ObjCInterfaceDecl *Class, |
Daniel Dunbar | aff9fca | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 6612 | const ObjCMethodDecl *Method) { |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 6613 | return isVTableDispatchedSelector(Sel) |
| 6614 | ? EmitVTableMessageSend(CGF, Return, ResultType, Sel, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6615 | Receiver, CGF.getContext().getObjCIdType(), |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 6616 | false, CallArgs, Method) |
| 6617 | : EmitMessageSend(CGF, Return, ResultType, |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 6618 | EmitSelector(CGF, Sel), |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6619 | Receiver, CGF.getContext().getObjCIdType(), |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 6620 | false, CallArgs, Method, ObjCTypes); |
Fariborz Jahanian | 3d9296e | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 6621 | } |
| 6622 | |
Daniel Dunbar | c6928bb | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 6623 | llvm::GlobalVariable * |
Rafael Espindola | 554256c | 2014-02-26 22:25:45 +0000 | [diff] [blame] | 6624 | CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name, bool Weak) { |
| 6625 | llvm::GlobalValue::LinkageTypes L = |
| 6626 | Weak ? llvm::GlobalValue::ExternalWeakLinkage |
| 6627 | : llvm::GlobalValue::ExternalLinkage; |
| 6628 | |
Daniel Dunbar | c6928bb | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 6629 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 6630 | |
Rafael Espindola | 554256c | 2014-02-26 22:25:45 +0000 | [diff] [blame] | 6631 | if (!GV) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6632 | GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy, |
Rafael Espindola | 554256c | 2014-02-26 22:25:45 +0000 | [diff] [blame] | 6633 | false, L, 0, Name); |
Daniel Dunbar | c6928bb | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 6634 | |
Rafael Espindola | 554256c | 2014-02-26 22:25:45 +0000 | [diff] [blame] | 6635 | assert(GV->getLinkage() == L); |
Daniel Dunbar | c6928bb | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 6636 | return GV; |
| 6637 | } |
| 6638 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 6639 | llvm::Value *CGObjCNonFragileABIMac::EmitClassRefFromId(CodeGenFunction &CGF, |
Rafael Espindola | 554256c | 2014-02-26 22:25:45 +0000 | [diff] [blame] | 6640 | IdentifierInfo *II, |
| 6641 | bool Weak) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6642 | llvm::GlobalVariable *&Entry = ClassReferences[II]; |
| 6643 | |
Fariborz Jahanian | 33f66e6 | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 6644 | if (!Entry) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6645 | std::string ClassName(getClassSymbolPrefix() + II->getName().str()); |
Rafael Espindola | 554256c | 2014-02-26 22:25:45 +0000 | [diff] [blame] | 6646 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName, Weak); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6647 | Entry = |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6648 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, |
Rafael Espindola | 5179a4e | 2014-02-27 19:01:11 +0000 | [diff] [blame] | 6649 | false, llvm::GlobalValue::PrivateLinkage, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6650 | ClassGV, |
| 6651 | "\01L_OBJC_CLASSLIST_REFERENCES_$_"); |
Fariborz Jahanian | 33f66e6 | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 6652 | Entry->setAlignment( |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 6653 | CGM.getDataLayout().getABITypeAlignment( |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6654 | ObjCTypes.ClassnfABIPtrTy)); |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 6655 | Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip"); |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 6656 | CGM.addCompilerUsedGlobal(Entry); |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 6657 | } |
Rafael Espindola | 21039aa | 2014-02-27 16:26:32 +0000 | [diff] [blame] | 6658 | assertPrivateName(Entry); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 6659 | return CGF.Builder.CreateLoad(Entry); |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 6660 | } |
Fariborz Jahanian | 33f66e6 | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 6661 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 6662 | llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CodeGenFunction &CGF, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6663 | const ObjCInterfaceDecl *ID) { |
Rafael Espindola | 554256c | 2014-02-26 22:25:45 +0000 | [diff] [blame] | 6664 | return EmitClassRefFromId(CGF, ID->getIdentifier(), ID->isWeakImported()); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6665 | } |
| 6666 | |
| 6667 | llvm::Value *CGObjCNonFragileABIMac::EmitNSAutoreleasePoolClassRef( |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 6668 | CodeGenFunction &CGF) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6669 | IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool"); |
Rafael Espindola | 554256c | 2014-02-26 22:25:45 +0000 | [diff] [blame] | 6670 | return EmitClassRefFromId(CGF, II, false); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6671 | } |
| 6672 | |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 6673 | llvm::Value * |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 6674 | CGObjCNonFragileABIMac::EmitSuperClassRef(CodeGenFunction &CGF, |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 6675 | const ObjCInterfaceDecl *ID) { |
| 6676 | llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()]; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6677 | |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 6678 | if (!Entry) { |
| 6679 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
Fariborz Jahanian | f322f2f | 2014-03-11 00:25:05 +0000 | [diff] [blame] | 6680 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName, |
| 6681 | ID->isWeakImported()); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6682 | Entry = |
| 6683 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, |
Rafael Espindola | 5179a4e | 2014-02-27 19:01:11 +0000 | [diff] [blame] | 6684 | false, llvm::GlobalValue::PrivateLinkage, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6685 | ClassGV, |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 6686 | "\01L_OBJC_CLASSLIST_SUP_REFS_$_"); |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 6687 | Entry->setAlignment( |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 6688 | CGM.getDataLayout().getABITypeAlignment( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6689 | ObjCTypes.ClassnfABIPtrTy)); |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 6690 | Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip"); |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 6691 | CGM.addCompilerUsedGlobal(Entry); |
Fariborz Jahanian | 33f66e6 | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 6692 | } |
Rafael Espindola | 21039aa | 2014-02-27 16:26:32 +0000 | [diff] [blame] | 6693 | assertPrivateName(Entry); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 6694 | return CGF.Builder.CreateLoad(Entry); |
Fariborz Jahanian | 33f66e6 | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 6695 | } |
| 6696 | |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 6697 | /// EmitMetaClassRef - Return a Value * of the address of _class_t |
| 6698 | /// meta-data |
| 6699 | /// |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 6700 | llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CodeGenFunction &CGF, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6701 | const ObjCInterfaceDecl *ID) { |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 6702 | llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()]; |
Rafael Espindola | 21039aa | 2014-02-27 16:26:32 +0000 | [diff] [blame] | 6703 | if (!Entry) { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6704 | |
Rafael Espindola | 21039aa | 2014-02-27 16:26:32 +0000 | [diff] [blame] | 6705 | std::string MetaClassName(getMetaclassSymbolPrefix() + |
| 6706 | ID->getNameAsString()); |
| 6707 | llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName); |
| 6708 | Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, |
Rafael Espindola | 5179a4e | 2014-02-27 19:01:11 +0000 | [diff] [blame] | 6709 | false, llvm::GlobalValue::PrivateLinkage, |
Rafael Espindola | 21039aa | 2014-02-27 16:26:32 +0000 | [diff] [blame] | 6710 | MetaClassGV, |
| 6711 | "\01L_OBJC_CLASSLIST_SUP_REFS_$_"); |
| 6712 | Entry->setAlignment( |
| 6713 | CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ClassnfABIPtrTy)); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6714 | |
Rafael Espindola | 21039aa | 2014-02-27 16:26:32 +0000 | [diff] [blame] | 6715 | Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip"); |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 6716 | CGM.addCompilerUsedGlobal(Entry); |
Rafael Espindola | 21039aa | 2014-02-27 16:26:32 +0000 | [diff] [blame] | 6717 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6718 | |
Rafael Espindola | 21039aa | 2014-02-27 16:26:32 +0000 | [diff] [blame] | 6719 | assertPrivateName(Entry); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 6720 | return CGF.Builder.CreateLoad(Entry); |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 6721 | } |
| 6722 | |
Fariborz Jahanian | 33f66e6 | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 6723 | /// GetClass - Return a reference to the class for the given interface |
| 6724 | /// decl. |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 6725 | llvm::Value *CGObjCNonFragileABIMac::GetClass(CodeGenFunction &CGF, |
Fariborz Jahanian | 33f66e6 | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 6726 | const ObjCInterfaceDecl *ID) { |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 6727 | if (ID->isWeakImported()) { |
Fariborz Jahanian | 95ace55 | 2009-11-17 22:42:00 +0000 | [diff] [blame] | 6728 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
Rafael Espindola | 554256c | 2014-02-26 22:25:45 +0000 | [diff] [blame] | 6729 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName, true); |
Nick Lewycky | 9a44164 | 2014-02-27 00:36:00 +0000 | [diff] [blame] | 6730 | (void)ClassGV; |
Rafael Espindola | 554256c | 2014-02-26 22:25:45 +0000 | [diff] [blame] | 6731 | assert(ClassGV->getLinkage() == llvm::GlobalValue::ExternalWeakLinkage); |
Fariborz Jahanian | 95ace55 | 2009-11-17 22:42:00 +0000 | [diff] [blame] | 6732 | } |
| 6733 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 6734 | return EmitClassRef(CGF, ID); |
Fariborz Jahanian | 33f66e6 | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 6735 | } |
| 6736 | |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 6737 | /// Generates a message send where the super is the receiver. This is |
| 6738 | /// a message send to self with special delivery semantics indicating |
| 6739 | /// which class's method should be called. |
| 6740 | CodeGen::RValue |
| 6741 | CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
John McCall | 78a1511 | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 6742 | ReturnValueSlot Return, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6743 | QualType ResultType, |
| 6744 | Selector Sel, |
| 6745 | const ObjCInterfaceDecl *Class, |
| 6746 | bool isCategoryImpl, |
| 6747 | llvm::Value *Receiver, |
| 6748 | bool IsClassMessage, |
Daniel Dunbar | aff9fca | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 6749 | const CodeGen::CallArgList &CallArgs, |
| 6750 | const ObjCMethodDecl *Method) { |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 6751 | // ... |
| 6752 | // Create and init a super structure; this is a (receiver, class) |
| 6753 | // pair we will pass to objc_msgSendSuper. |
| 6754 | llvm::Value *ObjCSuper = |
John McCall | 6647584 | 2011-03-04 08:00:29 +0000 | [diff] [blame] | 6755 | CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6756 | |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 6757 | llvm::Value *ReceiverAsObject = |
| 6758 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); |
| 6759 | CGF.Builder.CreateStore(ReceiverAsObject, |
| 6760 | CGF.Builder.CreateStructGEP(ObjCSuper, 0)); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6761 | |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 6762 | // 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] | 6763 | llvm::Value *Target; |
Fariborz Jahanian | 27678b0 | 2012-10-10 23:11:18 +0000 | [diff] [blame] | 6764 | if (IsClassMessage) |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 6765 | Target = EmitMetaClassRef(CGF, Class); |
Fariborz Jahanian | 27678b0 | 2012-10-10 23:11:18 +0000 | [diff] [blame] | 6766 | else |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 6767 | Target = EmitSuperClassRef(CGF, Class); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6768 | |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 6769 | // FIXME: We shouldn't need to do this cast, rectify the ASTContext and |
| 6770 | // ObjCTypes types. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 6771 | llvm::Type *ClassTy = |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 6772 | CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); |
| 6773 | Target = CGF.Builder.CreateBitCast(Target, ClassTy); |
| 6774 | CGF.Builder.CreateStore(Target, |
| 6775 | CGF.Builder.CreateStructGEP(ObjCSuper, 1)); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6776 | |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 6777 | return (isVTableDispatchedSelector(Sel)) |
| 6778 | ? EmitVTableMessageSend(CGF, Return, ResultType, Sel, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6779 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 6780 | true, CallArgs, Method) |
| 6781 | : EmitMessageSend(CGF, Return, ResultType, |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 6782 | EmitSelector(CGF, Sel), |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6783 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 6784 | true, CallArgs, Method, ObjCTypes); |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 6785 | } |
Fariborz Jahanian | 74b7722 | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 6786 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 6787 | llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CodeGenFunction &CGF, |
Fariborz Jahanian | 9240f3d | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 6788 | Selector Sel, bool lval) { |
Fariborz Jahanian | 74b7722 | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 6789 | llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6790 | |
Fariborz Jahanian | 74b7722 | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 6791 | if (!Entry) { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6792 | llvm::Constant *Casted = |
| 6793 | llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), |
| 6794 | ObjCTypes.SelectorPtrTy); |
| 6795 | Entry = |
| 6796 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false, |
Rafael Espindola | 5179a4e | 2014-02-27 19:01:11 +0000 | [diff] [blame] | 6797 | llvm::GlobalValue::PrivateLinkage, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6798 | Casted, "\01L_OBJC_SELECTOR_REFERENCES_"); |
Michael Gottesman | 5c20596 | 2013-02-05 23:08:45 +0000 | [diff] [blame] | 6799 | Entry->setExternallyInitialized(true); |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 6800 | Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip"); |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 6801 | CGM.addCompilerUsedGlobal(Entry); |
Fariborz Jahanian | 74b7722 | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 6802 | } |
Rafael Espindola | 21039aa | 2014-02-27 16:26:32 +0000 | [diff] [blame] | 6803 | assertPrivateName(Entry); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6804 | |
Fariborz Jahanian | 9240f3d | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 6805 | if (lval) |
| 6806 | return Entry; |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 6807 | llvm::LoadInst* LI = CGF.Builder.CreateLoad(Entry); |
Pete Cooper | 9d60551 | 2011-11-10 21:45:06 +0000 | [diff] [blame] | 6808 | |
| 6809 | LI->setMetadata(CGM.getModule().getMDKindID("invariant.load"), |
| 6810 | llvm::MDNode::get(VMContext, |
| 6811 | ArrayRef<llvm::Value*>())); |
| 6812 | return LI; |
Fariborz Jahanian | 74b7722 | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 6813 | } |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6814 | /// EmitObjCIvarAssign - Code gen for assigning to a __strong object. |
Fariborz Jahanian | 7a95d72 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 6815 | /// objc_assign_ivar (id src, id *dst, ptrdiff_t) |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6816 | /// |
| 6817 | void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6818 | llvm::Value *src, |
Fariborz Jahanian | 7a95d72 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 6819 | llvm::Value *dst, |
| 6820 | llvm::Value *ivarOffset) { |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 6821 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 6822 | if (!isa<llvm::PointerType>(SrcTy)) { |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 6823 | unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 6824 | assert(Size <= 8 && "does not support size > 8"); |
| 6825 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 6826 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 1b074a3 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 6827 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 6828 | } |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6829 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 6830 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 6831 | llvm::Value *args[] = { src, dst, ivarOffset }; |
| 6832 | CGF.EmitNounwindRuntimeCall(ObjCTypes.getGcAssignIvarFn(), args); |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6833 | } |
| 6834 | |
| 6835 | /// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object. |
| 6836 | /// objc_assign_strongCast (id src, id *dst) |
| 6837 | /// |
| 6838 | void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6839 | CodeGen::CodeGenFunction &CGF, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6840 | llvm::Value *src, llvm::Value *dst) { |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 6841 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 6842 | if (!isa<llvm::PointerType>(SrcTy)) { |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 6843 | unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 6844 | assert(Size <= 8 && "does not support size > 8"); |
| 6845 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6846 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 1b074a3 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 6847 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 6848 | } |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6849 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 6850 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 6851 | llvm::Value *args[] = { src, dst }; |
| 6852 | CGF.EmitNounwindRuntimeCall(ObjCTypes.getGcAssignStrongCastFn(), |
| 6853 | args, "weakassign"); |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6854 | } |
| 6855 | |
Fariborz Jahanian | 5f21d2f | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 6856 | void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6857 | CodeGen::CodeGenFunction &CGF, |
| 6858 | llvm::Value *DestPtr, |
| 6859 | llvm::Value *SrcPtr, |
Fariborz Jahanian | 021510e | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 6860 | llvm::Value *Size) { |
Fariborz Jahanian | 5f21d2f | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 6861 | SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy); |
| 6862 | DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 6863 | llvm::Value *args[] = { DestPtr, SrcPtr, Size }; |
| 6864 | CGF.EmitNounwindRuntimeCall(ObjCTypes.GcMemmoveCollectableFn(), args); |
Fariborz Jahanian | 5f21d2f | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 6865 | } |
| 6866 | |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6867 | /// EmitObjCWeakRead - Code gen for loading value of a __weak |
| 6868 | /// object: objc_read_weak (id *src) |
| 6869 | /// |
| 6870 | llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6871 | CodeGen::CodeGenFunction &CGF, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6872 | llvm::Value *AddrWeakObj) { |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 6873 | llvm::Type* DestTy = |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6874 | cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType(); |
| 6875 | AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 6876 | llvm::Value *read_weak = |
| 6877 | CGF.EmitNounwindRuntimeCall(ObjCTypes.getGcReadWeakFn(), |
| 6878 | AddrWeakObj, "weakread"); |
Eli Friedman | a374b68 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 6879 | read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy); |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6880 | return read_weak; |
| 6881 | } |
| 6882 | |
| 6883 | /// EmitObjCWeakAssign - Code gen for assigning to a __weak object. |
| 6884 | /// objc_assign_weak (id src, id *dst) |
| 6885 | /// |
| 6886 | void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6887 | llvm::Value *src, llvm::Value *dst) { |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 6888 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 6889 | if (!isa<llvm::PointerType>(SrcTy)) { |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 6890 | unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 6891 | assert(Size <= 8 && "does not support size > 8"); |
| 6892 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 6893 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 1b074a3 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 6894 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 6895 | } |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6896 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 6897 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 6898 | llvm::Value *args[] = { src, dst }; |
| 6899 | CGF.EmitNounwindRuntimeCall(ObjCTypes.getGcAssignWeakFn(), |
| 6900 | args, "weakassign"); |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6901 | } |
| 6902 | |
| 6903 | /// EmitObjCGlobalAssign - Code gen for assigning to a __strong object. |
| 6904 | /// objc_assign_global (id src, id *dst) |
| 6905 | /// |
| 6906 | void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 217af24 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 6907 | llvm::Value *src, llvm::Value *dst, |
| 6908 | bool threadlocal) { |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 6909 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 6910 | if (!isa<llvm::PointerType>(SrcTy)) { |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 6911 | unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 6912 | assert(Size <= 8 && "does not support size > 8"); |
| 6913 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 6914 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 1b074a3 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 6915 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 6916 | } |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6917 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 6918 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 6919 | llvm::Value *args[] = { src, dst }; |
Fariborz Jahanian | 217af24 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 6920 | if (!threadlocal) |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 6921 | CGF.EmitNounwindRuntimeCall(ObjCTypes.getGcAssignGlobalFn(), |
| 6922 | args, "globalassign"); |
Fariborz Jahanian | 217af24 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 6923 | else |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 6924 | CGF.EmitNounwindRuntimeCall(ObjCTypes.getGcAssignThreadLocalFn(), |
| 6925 | args, "threadlocalassign"); |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6926 | } |
Fariborz Jahanian | 74b7722 | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 6927 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6928 | void |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 6929 | CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 6930 | const ObjCAtSynchronizedStmt &S) { |
David Chisnall | 3e57560 | 2011-03-25 17:46:35 +0000 | [diff] [blame] | 6931 | EmitAtSynchronizedStmt(CGF, S, |
| 6932 | cast<llvm::Function>(ObjCTypes.getSyncEnterFn()), |
| 6933 | cast<llvm::Function>(ObjCTypes.getSyncExitFn())); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 6934 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6935 | |
John McCall | 2ca705e | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 6936 | llvm::Constant * |
Fariborz Jahanian | 831f0fc | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 6937 | CGObjCNonFragileABIMac::GetEHType(QualType T) { |
John McCall | 2ca705e | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 6938 | // There's a particular fixed type info for 'id'. |
| 6939 | if (T->isObjCIdType() || |
| 6940 | T->isObjCQualifiedIdType()) { |
| 6941 | llvm::Constant *IDEHType = |
| 6942 | CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id"); |
| 6943 | if (!IDEHType) |
| 6944 | IDEHType = |
| 6945 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, |
| 6946 | false, |
| 6947 | llvm::GlobalValue::ExternalLinkage, |
| 6948 | 0, "OBJC_EHTYPE_id"); |
| 6949 | return IDEHType; |
| 6950 | } |
| 6951 | |
| 6952 | // All other types should be Objective-C interface pointer types. |
| 6953 | const ObjCObjectPointerType *PT = |
| 6954 | T->getAs<ObjCObjectPointerType>(); |
| 6955 | assert(PT && "Invalid @catch type."); |
| 6956 | const ObjCInterfaceType *IT = PT->getInterfaceType(); |
| 6957 | assert(IT && "Invalid @catch type."); |
| 6958 | return GetInterfaceEHType(IT->getDecl(), false); |
| 6959 | } |
| 6960 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 6961 | void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF, |
| 6962 | const ObjCAtTryStmt &S) { |
David Chisnall | 3e57560 | 2011-03-25 17:46:35 +0000 | [diff] [blame] | 6963 | EmitTryCatchStmt(CGF, S, |
| 6964 | cast<llvm::Function>(ObjCTypes.getObjCBeginCatchFn()), |
| 6965 | cast<llvm::Function>(ObjCTypes.getObjCEndCatchFn()), |
| 6966 | cast<llvm::Function>(ObjCTypes.getExceptionRethrowFn())); |
Daniel Dunbar | 0b0dcd9 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 6967 | } |
| 6968 | |
Anders Carlsson | 9ab53d1 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 6969 | /// EmitThrowStmt - Generate code for a throw statement. |
| 6970 | void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 1eab052 | 2013-01-10 19:02:56 +0000 | [diff] [blame] | 6971 | const ObjCAtThrowStmt &S, |
| 6972 | bool ClearInsertionPoint) { |
Anders Carlsson | 9ab53d1 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 6973 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
John McCall | 248512a | 2011-10-01 10:32:24 +0000 | [diff] [blame] | 6974 | llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr); |
Benjamin Kramer | 76399eb | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 6975 | Exception = CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 6976 | CGF.EmitRuntimeCallOrInvoke(ObjCTypes.getExceptionThrowFn(), Exception) |
John McCall | 17afe45 | 2010-10-16 08:21:07 +0000 | [diff] [blame] | 6977 | .setDoesNotReturn(); |
Anders Carlsson | 9ab53d1 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 6978 | } else { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 6979 | CGF.EmitRuntimeCallOrInvoke(ObjCTypes.getExceptionRethrowFn()) |
John McCall | 17afe45 | 2010-10-16 08:21:07 +0000 | [diff] [blame] | 6980 | .setDoesNotReturn(); |
Anders Carlsson | 9ab53d1 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 6981 | } |
| 6982 | |
John McCall | 17afe45 | 2010-10-16 08:21:07 +0000 | [diff] [blame] | 6983 | CGF.Builder.CreateUnreachable(); |
Fariborz Jahanian | 1eab052 | 2013-01-10 19:02:56 +0000 | [diff] [blame] | 6984 | if (ClearInsertionPoint) |
| 6985 | CGF.Builder.ClearInsertionPoint(); |
Anders Carlsson | 9ab53d1 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 6986 | } |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6987 | |
John McCall | 2ca705e | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 6988 | llvm::Constant * |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6989 | CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID, |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6990 | bool ForDefinition) { |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6991 | llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()]; |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6992 | |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6993 | // If we don't need a definition, return the entry if found or check |
| 6994 | // if we use an external reference. |
| 6995 | if (!ForDefinition) { |
| 6996 | if (Entry) |
| 6997 | return Entry; |
Daniel Dunbar | d7beeea | 2009-04-07 06:43:45 +0000 | [diff] [blame] | 6998 | |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6999 | // If this type (or a super class) has the __objc_exception__ |
| 7000 | // attribute, emit an external reference. |
Douglas Gregor | 78bd61f | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 7001 | if (hasObjCExceptionAttribute(CGM.getContext(), ID)) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 7002 | return Entry = |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 7003 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false, |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 7004 | llvm::GlobalValue::ExternalLinkage, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 7005 | 0, |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 7006 | ("OBJC_EHTYPE_$_" + |
Daniel Dunbar | 07d0785 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 7007 | ID->getIdentifier()->getName())); |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 7008 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 7009 | |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 7010 | // Otherwise we need to either make a new entry or fill in the |
| 7011 | // initializer. |
| 7012 | assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition"); |
Daniel Dunbar | 15894b7 | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 7013 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 7014 | std::string VTableName = "objc_ehtype_vtable"; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 7015 | llvm::GlobalVariable *VTableGV = |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 7016 | CGM.getModule().getGlobalVariable(VTableName); |
| 7017 | if (!VTableGV) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 7018 | VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy, |
| 7019 | false, |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 7020 | llvm::GlobalValue::ExternalLinkage, |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 7021 | 0, VTableName); |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 7022 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 7023 | llvm::Value *VTableIdx = llvm::ConstantInt::get(CGM.Int32Ty, 2); |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 7024 | |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 7025 | llvm::Constant *Values[] = { |
| 7026 | llvm::ConstantExpr::getGetElementPtr(VTableGV, VTableIdx), |
| 7027 | GetClassName(ID->getIdentifier()), |
| 7028 | GetClassGlobal(ClassName) |
| 7029 | }; |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 7030 | llvm::Constant *Init = |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 7031 | llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values); |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 7032 | |
Rafael Espindola | 554256c | 2014-02-26 22:25:45 +0000 | [diff] [blame] | 7033 | llvm::GlobalValue::LinkageTypes L = ForDefinition |
| 7034 | ? llvm::GlobalValue::ExternalLinkage |
| 7035 | : llvm::GlobalValue::WeakAnyLinkage; |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 7036 | if (Entry) { |
| 7037 | Entry->setInitializer(Init); |
| 7038 | } else { |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 7039 | Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false, |
Rafael Espindola | 554256c | 2014-02-26 22:25:45 +0000 | [diff] [blame] | 7040 | L, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 7041 | Init, |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 7042 | ("OBJC_EHTYPE_$_" + |
Daniel Dunbar | 07d0785 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 7043 | ID->getIdentifier()->getName())); |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 7044 | } |
Rafael Espindola | 554256c | 2014-02-26 22:25:45 +0000 | [diff] [blame] | 7045 | assert(Entry->getLinkage() == L); |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 7046 | |
John McCall | a496098 | 2013-02-19 01:57:29 +0000 | [diff] [blame] | 7047 | if (ID->getVisibility() == HiddenVisibility) |
Daniel Dunbar | 15894b7 | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 7048 | Entry->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 7049 | Entry->setAlignment(CGM.getDataLayout().getABITypeAlignment( |
Daniel Dunbar | 710cb20 | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 7050 | ObjCTypes.EHTypeTy)); |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 7051 | |
Rafael Espindola | 554256c | 2014-02-26 22:25:45 +0000 | [diff] [blame] | 7052 | if (ForDefinition) |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 7053 | Entry->setSection("__DATA,__objc_const"); |
Rafael Espindola | 554256c | 2014-02-26 22:25:45 +0000 | [diff] [blame] | 7054 | else |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 7055 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 7056 | |
| 7057 | return Entry; |
| 7058 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 7059 | |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 7060 | /* *** */ |
| 7061 | |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 7062 | CodeGen::CGObjCRuntime * |
| 7063 | CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) { |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 7064 | switch (CGM.getLangOpts().ObjCRuntime.getKind()) { |
| 7065 | case ObjCRuntime::FragileMacOSX: |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 7066 | return new CGObjCMac(CGM); |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 7067 | |
| 7068 | case ObjCRuntime::MacOSX: |
| 7069 | case ObjCRuntime::iOS: |
| 7070 | return new CGObjCNonFragileABIMac(CGM); |
| 7071 | |
David Chisnall | b601c96 | 2012-07-03 20:49:52 +0000 | [diff] [blame] | 7072 | case ObjCRuntime::GNUstep: |
| 7073 | case ObjCRuntime::GCC: |
John McCall | 775086e | 2012-07-12 02:07:58 +0000 | [diff] [blame] | 7074 | case ObjCRuntime::ObjFW: |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 7075 | llvm_unreachable("these runtimes are not Mac runtimes"); |
| 7076 | } |
| 7077 | llvm_unreachable("bad runtime"); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 7078 | } |