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" |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 15 | |
Daniel Dunbar | 034299e | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 16 | #include "CGRecordLayout.h" |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 17 | #include "CodeGenModule.h" |
Daniel Dunbar | a94ecd2 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 18 | #include "CodeGenFunction.h" |
John McCall | ad7c5c1 | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 19 | #include "CGBlocks.h" |
John McCall | ed1ae86 | 2011-01-28 11:13:47 +0000 | [diff] [blame] | 20 | #include "CGCleanup.h" |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 21 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | 221fa94 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 22 | #include "clang/AST/Decl.h" |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 23 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | 15b73de | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 24 | #include "clang/AST/RecordLayout.h" |
Chris Lattner | f0b64d7 | 2009-04-26 01:32:48 +0000 | [diff] [blame] | 25 | #include "clang/AST/StmtObjC.h" |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 26 | #include "clang/Basic/LangOptions.h" |
Chandler Carruth | 8509824 | 2010-06-15 23:19:56 +0000 | [diff] [blame] | 27 | #include "clang/Frontend/CodeGenOptions.h" |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 28 | |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 29 | #include "llvm/InlineAsm.h" |
| 30 | #include "llvm/IntrinsicInst.h" |
Owen Anderson | ae86c19 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 31 | #include "llvm/LLVMContext.h" |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 32 | #include "llvm/Module.h" |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/DenseSet.h" |
Daniel Dunbar | d027a92 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/SetVector.h" |
| 35 | #include "llvm/ADT/SmallString.h" |
Fariborz Jahanian | 751c1e7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 36 | #include "llvm/ADT/SmallPtrSet.h" |
John McCall | 5c08ab9 | 2010-07-13 22:12:14 +0000 | [diff] [blame] | 37 | #include "llvm/Support/CallSite.h" |
Daniel Dunbar | d027a92 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 38 | #include "llvm/Support/raw_ostream.h" |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 39 | #include "llvm/DataLayout.h" |
Torok Edwin | db71492 | 2009-08-24 13:25:12 +0000 | [diff] [blame] | 40 | #include <cstdio> |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 41 | |
| 42 | using namespace clang; |
Daniel Dunbar | 41cf9de | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 43 | using namespace CodeGen; |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 44 | |
| 45 | namespace { |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 46 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 47 | // FIXME: We should find a nicer way to make the labels for metadata, string |
| 48 | // concatenation is lame. |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 49 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 50 | class ObjCCommonTypesHelper { |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 51 | protected: |
| 52 | llvm::LLVMContext &VMContext; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 53 | |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 54 | private: |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 55 | // The types of these functions don't really matter because we |
| 56 | // should always bitcast before calling them. |
| 57 | |
| 58 | /// id objc_msgSend (id, SEL, ...) |
| 59 | /// |
| 60 | /// The default messenger, used for sends whose ABI is unchanged from |
| 61 | /// the all-integer/pointer case. |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 62 | llvm::Constant *getMessageSendFn() const { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 63 | // Add the non-lazy-bind attribute, since objc_msgSend is likely to |
| 64 | // be called a lot. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 65 | llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy }; |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 66 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 67 | params, true), |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 68 | "objc_msgSend", |
Bill Wendling | 311c832 | 2012-10-15 04:47:45 +0000 | [diff] [blame] | 69 | llvm::Attributes::get(CGM.getLLVMContext(), |
Bill Wendling | 507c351 | 2012-10-16 05:23:44 +0000 | [diff] [blame] | 70 | llvm::Attributes::NonLazyBind)); |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 71 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 72 | |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 73 | /// void objc_msgSend_stret (id, SEL, ...) |
| 74 | /// |
| 75 | /// The messenger used when the return value is an aggregate returned |
| 76 | /// by indirect reference in the first argument, and therefore the |
| 77 | /// self and selector parameters are shifted over by one. |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 78 | llvm::Constant *getMessageSendStretFn() const { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 79 | llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy }; |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 80 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy, |
| 81 | params, true), |
| 82 | "objc_msgSend_stret"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 83 | |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 84 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 85 | |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 86 | /// [double | long double] objc_msgSend_fpret(id self, SEL op, ...) |
| 87 | /// |
| 88 | /// The messenger used when the return value is returned on the x87 |
| 89 | /// floating-point stack; without a special entrypoint, the nil case |
| 90 | /// would be unbalanced. |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 91 | llvm::Constant *getMessageSendFpretFn() const { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 92 | llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy }; |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 93 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.DoubleTy, |
| 94 | params, true), |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 95 | "objc_msgSend_fpret"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 96 | |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 97 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 98 | |
Anders Carlsson | 2f1a6c3 | 2011-10-31 16:27:11 +0000 | [diff] [blame] | 99 | /// _Complex long double objc_msgSend_fp2ret(id self, SEL op, ...) |
| 100 | /// |
| 101 | /// The messenger used when the return value is returned in two values on the |
| 102 | /// x87 floating point stack; without a special entrypoint, the nil case |
| 103 | /// would be unbalanced. Only used on 64-bit X86. |
| 104 | llvm::Constant *getMessageSendFp2retFn() const { |
| 105 | llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy }; |
| 106 | llvm::Type *longDoubleType = llvm::Type::getX86_FP80Ty(VMContext); |
| 107 | llvm::Type *resultType = |
| 108 | llvm::StructType::get(longDoubleType, longDoubleType, NULL); |
| 109 | |
| 110 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(resultType, |
| 111 | params, true), |
| 112 | "objc_msgSend_fp2ret"); |
| 113 | } |
| 114 | |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 115 | /// id objc_msgSendSuper(struct objc_super *super, SEL op, ...) |
| 116 | /// |
| 117 | /// The messenger used for super calls, which have different dispatch |
| 118 | /// semantics. The class passed is the superclass of the current |
| 119 | /// class. |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 120 | llvm::Constant *getMessageSendSuperFn() const { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 121 | llvm::Type *params[] = { SuperPtrTy, SelectorPtrTy }; |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 122 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 123 | params, true), |
| 124 | "objc_msgSendSuper"); |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 125 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 126 | |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 127 | /// id objc_msgSendSuper2(struct objc_super *super, SEL op, ...) |
| 128 | /// |
| 129 | /// A slightly different messenger used for super calls. The class |
| 130 | /// passed is the current class. |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 131 | llvm::Constant *getMessageSendSuperFn2() const { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 132 | llvm::Type *params[] = { SuperPtrTy, SelectorPtrTy }; |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 133 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 134 | params, true), |
| 135 | "objc_msgSendSuper2"); |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 136 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 137 | |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 138 | /// void objc_msgSendSuper_stret(void *stretAddr, struct objc_super *super, |
| 139 | /// SEL op, ...) |
| 140 | /// |
| 141 | /// The messenger used for super calls which return an aggregate indirectly. |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 142 | llvm::Constant *getMessageSendSuperStretFn() const { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 143 | llvm::Type *params[] = { Int8PtrTy, SuperPtrTy, SelectorPtrTy }; |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 144 | return CGM.CreateRuntimeFunction( |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 145 | llvm::FunctionType::get(CGM.VoidTy, params, true), |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 146 | "objc_msgSendSuper_stret"); |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 147 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 148 | |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 149 | /// void objc_msgSendSuper2_stret(void * stretAddr, struct objc_super *super, |
| 150 | /// SEL op, ...) |
| 151 | /// |
| 152 | /// objc_msgSendSuper_stret with the super2 semantics. |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 153 | llvm::Constant *getMessageSendSuperStretFn2() const { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 154 | llvm::Type *params[] = { Int8PtrTy, SuperPtrTy, SelectorPtrTy }; |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 155 | return CGM.CreateRuntimeFunction( |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 156 | llvm::FunctionType::get(CGM.VoidTy, params, true), |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 157 | "objc_msgSendSuper2_stret"); |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 158 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 159 | |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 160 | llvm::Constant *getMessageSendSuperFpretFn() const { |
| 161 | // There is no objc_msgSendSuper_fpret? How can that work? |
| 162 | return getMessageSendSuperFn(); |
| 163 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 164 | |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 165 | llvm::Constant *getMessageSendSuperFpretFn2() const { |
| 166 | // There is no objc_msgSendSuper_fpret? How can that work? |
| 167 | return getMessageSendSuperFn2(); |
| 168 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 169 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 170 | protected: |
| 171 | CodeGen::CodeGenModule &CGM; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 172 | |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 173 | public: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 174 | llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy; |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 175 | llvm::Type *Int8PtrTy, *Int8PtrPtrTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 176 | |
Daniel Dunbar | 5d71559 | 2008-08-12 05:28:47 +0000 | [diff] [blame] | 177 | /// ObjectPtrTy - LLVM type for object handles (typeof(id)) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 178 | llvm::Type *ObjectPtrTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 179 | |
Fariborz Jahanian | 406b117 | 2008-11-18 20:18:11 +0000 | [diff] [blame] | 180 | /// PtrObjectPtrTy - LLVM type for id * |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 181 | llvm::Type *PtrObjectPtrTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 182 | |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 183 | /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL)) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 184 | llvm::Type *SelectorPtrTy; |
Douglas Gregor | 020de32 | 2012-01-17 18:36:30 +0000 | [diff] [blame] | 185 | |
| 186 | private: |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 187 | /// ProtocolPtrTy - LLVM type for external protocol handles |
| 188 | /// (typeof(Protocol)) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 189 | llvm::Type *ExternalProtocolPtrTy; |
Douglas Gregor | 020de32 | 2012-01-17 18:36:30 +0000 | [diff] [blame] | 190 | |
| 191 | public: |
| 192 | llvm::Type *getExternalProtocolPtrTy() { |
| 193 | if (!ExternalProtocolPtrTy) { |
| 194 | // FIXME: It would be nice to unify this with the opaque type, so that the |
| 195 | // IR comes out a bit cleaner. |
| 196 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 197 | ASTContext &Ctx = CGM.getContext(); |
| 198 | llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType()); |
| 199 | ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T); |
| 200 | } |
| 201 | |
| 202 | return ExternalProtocolPtrTy; |
| 203 | } |
| 204 | |
Daniel Dunbar | c722b85 | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 205 | // SuperCTy - clang type for struct objc_super. |
| 206 | QualType SuperCTy; |
| 207 | // SuperPtrCTy - clang type for struct objc_super *. |
| 208 | QualType SuperPtrCTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 209 | |
Daniel Dunbar | f6397fe | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 210 | /// SuperTy - LLVM type for struct objc_super. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 211 | llvm::StructType *SuperTy; |
Daniel Dunbar | 97ff50d | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 212 | /// SuperPtrTy - LLVM type for struct objc_super *. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 213 | llvm::Type *SuperPtrTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 214 | |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 215 | /// PropertyTy - LLVM type for struct objc_property (struct _prop_t |
| 216 | /// in GCC parlance). |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 217 | llvm::StructType *PropertyTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 218 | |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 219 | /// PropertyListTy - LLVM type for struct objc_property_list |
| 220 | /// (_prop_list_t in GCC parlance). |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 221 | llvm::StructType *PropertyListTy; |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 222 | /// PropertyListPtrTy - LLVM type for struct objc_property_list*. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 223 | llvm::Type *PropertyListPtrTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 224 | |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 225 | // MethodTy - LLVM type for struct objc_method. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 226 | llvm::StructType *MethodTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 227 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 228 | /// CacheTy - LLVM type for struct objc_cache. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 229 | llvm::Type *CacheTy; |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 230 | /// CachePtrTy - LLVM type for struct objc_cache *. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 231 | llvm::Type *CachePtrTy; |
Fariborz Jahanian | 0a3cfcc | 2011-06-22 20:21:51 +0000 | [diff] [blame] | 232 | |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 233 | llvm::Constant *getGetPropertyFn() { |
| 234 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 235 | ASTContext &Ctx = CGM.getContext(); |
| 236 | // id objc_getProperty (id, SEL, ptrdiff_t, bool) |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 237 | SmallVector<CanQualType,4> Params; |
John McCall | 2da83a3 | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 238 | CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType()); |
| 239 | CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType()); |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 240 | Params.push_back(IdType); |
| 241 | Params.push_back(SelType); |
David Chisnall | 08a4525 | 2011-03-22 20:03:13 +0000 | [diff] [blame] | 242 | Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified()); |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 243 | Params.push_back(Ctx.BoolTy); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 244 | llvm::FunctionType *FTy = |
John McCall | 8dda7b2 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 245 | Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(IdType, Params, |
| 246 | FunctionType::ExtInfo(), |
| 247 | RequiredArgs::All)); |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 248 | return CGM.CreateRuntimeFunction(FTy, "objc_getProperty"); |
| 249 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 250 | |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 251 | llvm::Constant *getSetPropertyFn() { |
| 252 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 253 | ASTContext &Ctx = CGM.getContext(); |
| 254 | // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool) |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 255 | SmallVector<CanQualType,6> Params; |
John McCall | 2da83a3 | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 256 | CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType()); |
| 257 | CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType()); |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 258 | Params.push_back(IdType); |
| 259 | Params.push_back(SelType); |
David Chisnall | 08a4525 | 2011-03-22 20:03:13 +0000 | [diff] [blame] | 260 | Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified()); |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 261 | Params.push_back(IdType); |
| 262 | Params.push_back(Ctx.BoolTy); |
| 263 | Params.push_back(Ctx.BoolTy); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 264 | llvm::FunctionType *FTy = |
John McCall | 8dda7b2 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 265 | Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, Params, |
| 266 | FunctionType::ExtInfo(), |
| 267 | RequiredArgs::All)); |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 268 | return CGM.CreateRuntimeFunction(FTy, "objc_setProperty"); |
| 269 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 270 | |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 271 | llvm::Constant *getOptimizedSetPropertyFn(bool atomic, bool copy) { |
| 272 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 273 | ASTContext &Ctx = CGM.getContext(); |
| 274 | // void objc_setProperty_atomic(id self, SEL _cmd, |
| 275 | // id newValue, ptrdiff_t offset); |
| 276 | // void objc_setProperty_nonatomic(id self, SEL _cmd, |
| 277 | // id newValue, ptrdiff_t offset); |
| 278 | // void objc_setProperty_atomic_copy(id self, SEL _cmd, |
| 279 | // id newValue, ptrdiff_t offset); |
| 280 | // void objc_setProperty_nonatomic_copy(id self, SEL _cmd, |
| 281 | // id newValue, ptrdiff_t offset); |
| 282 | |
| 283 | SmallVector<CanQualType,4> Params; |
| 284 | CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType()); |
| 285 | CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType()); |
| 286 | Params.push_back(IdType); |
| 287 | Params.push_back(SelType); |
| 288 | Params.push_back(IdType); |
| 289 | Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified()); |
| 290 | llvm::FunctionType *FTy = |
John McCall | 8dda7b2 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 291 | Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, Params, |
| 292 | FunctionType::ExtInfo(), |
| 293 | RequiredArgs::All)); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 294 | const char *name; |
| 295 | if (atomic && copy) |
| 296 | name = "objc_setProperty_atomic_copy"; |
| 297 | else if (atomic && !copy) |
| 298 | name = "objc_setProperty_atomic"; |
| 299 | else if (!atomic && copy) |
| 300 | name = "objc_setProperty_nonatomic_copy"; |
| 301 | else |
| 302 | name = "objc_setProperty_nonatomic"; |
| 303 | |
| 304 | return CGM.CreateRuntimeFunction(FTy, name); |
| 305 | } |
Fariborz Jahanian | 5a8c203 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 306 | |
| 307 | llvm::Constant *getCopyStructFn() { |
| 308 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 309 | ASTContext &Ctx = CGM.getContext(); |
| 310 | // void objc_copyStruct (void *, const void *, size_t, bool, bool) |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 311 | SmallVector<CanQualType,5> Params; |
Fariborz Jahanian | 5a8c203 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 312 | Params.push_back(Ctx.VoidPtrTy); |
| 313 | Params.push_back(Ctx.VoidPtrTy); |
| 314 | Params.push_back(Ctx.LongTy); |
| 315 | Params.push_back(Ctx.BoolTy); |
| 316 | Params.push_back(Ctx.BoolTy); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 317 | llvm::FunctionType *FTy = |
John McCall | 8dda7b2 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 318 | Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, Params, |
| 319 | FunctionType::ExtInfo(), |
| 320 | RequiredArgs::All)); |
Fariborz Jahanian | 5a8c203 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 321 | return CGM.CreateRuntimeFunction(FTy, "objc_copyStruct"); |
| 322 | } |
| 323 | |
Fariborz Jahanian | 1e1b549 | 2012-01-06 18:07:23 +0000 | [diff] [blame] | 324 | /// This routine declares and returns address of: |
| 325 | /// void objc_copyCppObjectAtomic( |
| 326 | /// void *dest, const void *src, |
| 327 | /// void (*copyHelper) (void *dest, const void *source)); |
| 328 | llvm::Constant *getCppAtomicObjectFunction() { |
| 329 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 330 | ASTContext &Ctx = CGM.getContext(); |
| 331 | /// void objc_copyCppObjectAtomic(void *dest, const void *src, void *helper); |
| 332 | SmallVector<CanQualType,3> Params; |
| 333 | Params.push_back(Ctx.VoidPtrTy); |
| 334 | Params.push_back(Ctx.VoidPtrTy); |
| 335 | Params.push_back(Ctx.VoidPtrTy); |
| 336 | llvm::FunctionType *FTy = |
John McCall | 8dda7b2 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 337 | Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, Params, |
| 338 | FunctionType::ExtInfo(), |
| 339 | RequiredArgs::All)); |
Fariborz Jahanian | 1e1b549 | 2012-01-06 18:07:23 +0000 | [diff] [blame] | 340 | return CGM.CreateRuntimeFunction(FTy, "objc_copyCppObjectAtomic"); |
| 341 | } |
| 342 | |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 343 | llvm::Constant *getEnumerationMutationFn() { |
Daniel Dunbar | 9d82da4 | 2009-07-11 20:32:50 +0000 | [diff] [blame] | 344 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 345 | ASTContext &Ctx = CGM.getContext(); |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 346 | // void objc_enumerationMutation (id) |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 347 | SmallVector<CanQualType,1> Params; |
John McCall | 2da83a3 | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 348 | Params.push_back(Ctx.getCanonicalParamType(Ctx.getObjCIdType())); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 349 | llvm::FunctionType *FTy = |
John McCall | 8dda7b2 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 350 | Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, Params, |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 351 | FunctionType::ExtInfo(), |
| 352 | RequiredArgs::All)); |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 353 | return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation"); |
| 354 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 355 | |
Fariborz Jahanian | eee54df | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 356 | /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function. |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 357 | llvm::Constant *getGcReadWeakFn() { |
| 358 | // id objc_read_weak (id *) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 359 | llvm::Type *args[] = { ObjectPtrTy->getPointerTo() }; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 360 | llvm::FunctionType *FTy = |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 361 | llvm::FunctionType::get(ObjectPtrTy, args, false); |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 362 | return CGM.CreateRuntimeFunction(FTy, "objc_read_weak"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 363 | } |
| 364 | |
Fariborz Jahanian | eee54df | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 365 | /// GcAssignWeakFn -- LLVM objc_assign_weak function. |
Chris Lattner | 6fdd57c | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 366 | llvm::Constant *getGcAssignWeakFn() { |
| 367 | // id objc_assign_weak (id, id *) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 368 | llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() }; |
Chris Lattner | 6fdd57c | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 369 | llvm::FunctionType *FTy = |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 370 | llvm::FunctionType::get(ObjectPtrTy, args, false); |
Chris Lattner | 6fdd57c | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 371 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak"); |
| 372 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 373 | |
Fariborz Jahanian | eee54df | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 374 | /// GcAssignGlobalFn -- LLVM objc_assign_global function. |
Chris Lattner | 0a696a42 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 375 | llvm::Constant *getGcAssignGlobalFn() { |
| 376 | // id objc_assign_global(id, id *) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 377 | llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() }; |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 378 | llvm::FunctionType *FTy = |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 379 | llvm::FunctionType::get(ObjectPtrTy, args, false); |
Chris Lattner | 0a696a42 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 380 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_global"); |
| 381 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 382 | |
Fariborz Jahanian | 217af24 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 383 | /// GcAssignThreadLocalFn -- LLVM objc_assign_threadlocal function. |
| 384 | llvm::Constant *getGcAssignThreadLocalFn() { |
| 385 | // id objc_assign_threadlocal(id src, id * dest) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 386 | llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() }; |
Fariborz Jahanian | 217af24 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 387 | llvm::FunctionType *FTy = |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 388 | llvm::FunctionType::get(ObjectPtrTy, args, false); |
Fariborz Jahanian | 217af24 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 389 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_threadlocal"); |
| 390 | } |
| 391 | |
Fariborz Jahanian | eee54df | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 392 | /// GcAssignIvarFn -- LLVM objc_assign_ivar function. |
Chris Lattner | 0a696a42 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 393 | llvm::Constant *getGcAssignIvarFn() { |
Fariborz Jahanian | 7a95d72 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 394 | // id objc_assign_ivar(id, id *, ptrdiff_t) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 395 | llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo(), |
| 396 | CGM.PtrDiffTy }; |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 397 | llvm::FunctionType *FTy = |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 398 | llvm::FunctionType::get(ObjectPtrTy, args, false); |
Chris Lattner | 0a696a42 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 399 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar"); |
| 400 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 401 | |
Fariborz Jahanian | 5f21d2f | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 402 | /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function. |
| 403 | llvm::Constant *GcMemmoveCollectableFn() { |
| 404 | // void *objc_memmove_collectable(void *dst, const void *src, size_t size) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 405 | llvm::Type *args[] = { Int8PtrTy, Int8PtrTy, LongTy }; |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 406 | llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, args, false); |
Fariborz Jahanian | 5f21d2f | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 407 | return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable"); |
| 408 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 409 | |
Fariborz Jahanian | eee54df | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 410 | /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function. |
Chris Lattner | 0a696a42 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 411 | llvm::Constant *getGcAssignStrongCastFn() { |
Fariborz Jahanian | 217af24 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 412 | // id objc_assign_strongCast(id, id *) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 413 | llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() }; |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 414 | llvm::FunctionType *FTy = |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 415 | llvm::FunctionType::get(ObjectPtrTy, args, false); |
Chris Lattner | 0a696a42 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 416 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast"); |
| 417 | } |
Anders Carlsson | 9ab53d1 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 418 | |
| 419 | /// ExceptionThrowFn - LLVM objc_exception_throw function. |
Chris Lattner | 0a696a42 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 420 | llvm::Constant *getExceptionThrowFn() { |
| 421 | // void objc_exception_throw(id) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 422 | llvm::Type *args[] = { ObjectPtrTy }; |
Chris Lattner | 0a696a42 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 423 | llvm::FunctionType *FTy = |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 424 | llvm::FunctionType::get(CGM.VoidTy, args, false); |
Chris Lattner | 0a696a42 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 425 | return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw"); |
| 426 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 427 | |
Fariborz Jahanian | 3336de1 | 2010-05-28 17:34:43 +0000 | [diff] [blame] | 428 | /// ExceptionRethrowFn - LLVM objc_exception_rethrow function. |
| 429 | llvm::Constant *getExceptionRethrowFn() { |
| 430 | // void objc_exception_rethrow(void) |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 431 | llvm::FunctionType *FTy = llvm::FunctionType::get(CGM.VoidTy, false); |
Fariborz Jahanian | 3336de1 | 2010-05-28 17:34:43 +0000 | [diff] [blame] | 432 | return CGM.CreateRuntimeFunction(FTy, "objc_exception_rethrow"); |
| 433 | } |
| 434 | |
Daniel Dunbar | 94ceb61 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 435 | /// SyncEnterFn - LLVM object_sync_enter function. |
Chris Lattner | dcceee7 | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 436 | llvm::Constant *getSyncEnterFn() { |
Aaron Ballman | 9c00446 | 2012-09-06 16:44:16 +0000 | [diff] [blame] | 437 | // int objc_sync_enter (id) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 438 | llvm::Type *args[] = { ObjectPtrTy }; |
Chris Lattner | dcceee7 | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 439 | llvm::FunctionType *FTy = |
Aaron Ballman | 9c00446 | 2012-09-06 16:44:16 +0000 | [diff] [blame] | 440 | llvm::FunctionType::get(CGM.IntTy, args, false); |
Chris Lattner | dcceee7 | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 441 | return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter"); |
| 442 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 443 | |
Daniel Dunbar | 94ceb61 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 444 | /// SyncExitFn - LLVM object_sync_exit function. |
Chris Lattner | 0a696a42 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 445 | llvm::Constant *getSyncExitFn() { |
Aaron Ballman | 9c00446 | 2012-09-06 16:44:16 +0000 | [diff] [blame] | 446 | // int objc_sync_exit (id) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 447 | llvm::Type *args[] = { ObjectPtrTy }; |
Chris Lattner | 0a696a42 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 448 | llvm::FunctionType *FTy = |
Aaron Ballman | 9c00446 | 2012-09-06 16:44:16 +0000 | [diff] [blame] | 449 | llvm::FunctionType::get(CGM.IntTy, args, false); |
Chris Lattner | 0a696a42 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 450 | return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit"); |
| 451 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 452 | |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 453 | llvm::Constant *getSendFn(bool IsSuper) const { |
| 454 | return IsSuper ? getMessageSendSuperFn() : getMessageSendFn(); |
| 455 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 456 | |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 457 | llvm::Constant *getSendFn2(bool IsSuper) const { |
| 458 | return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn(); |
| 459 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 460 | |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 461 | llvm::Constant *getSendStretFn(bool IsSuper) const { |
| 462 | return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn(); |
| 463 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 464 | |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 465 | llvm::Constant *getSendStretFn2(bool IsSuper) const { |
| 466 | return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn(); |
| 467 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 468 | |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 469 | llvm::Constant *getSendFpretFn(bool IsSuper) const { |
| 470 | return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn(); |
| 471 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 472 | |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 473 | llvm::Constant *getSendFpretFn2(bool IsSuper) const { |
| 474 | return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn(); |
| 475 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 476 | |
Anders Carlsson | 2f1a6c3 | 2011-10-31 16:27:11 +0000 | [diff] [blame] | 477 | llvm::Constant *getSendFp2retFn(bool IsSuper) const { |
| 478 | return IsSuper ? getMessageSendSuperFn() : getMessageSendFp2retFn(); |
| 479 | } |
| 480 | |
| 481 | llvm::Constant *getSendFp2RetFn2(bool IsSuper) const { |
| 482 | return IsSuper ? getMessageSendSuperFn2() : getMessageSendFp2retFn(); |
| 483 | } |
| 484 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 485 | ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm); |
| 486 | ~ObjCCommonTypesHelper(){} |
| 487 | }; |
Daniel Dunbar | f6397fe | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 488 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 489 | /// ObjCTypesHelper - Helper class that encapsulates lazy |
| 490 | /// construction of varies types used during ObjC generation. |
| 491 | class ObjCTypesHelper : public ObjCCommonTypesHelper { |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 492 | public: |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 493 | /// SymtabTy - LLVM type for struct objc_symtab. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 494 | llvm::StructType *SymtabTy; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 495 | /// SymtabPtrTy - LLVM type for struct objc_symtab *. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 496 | llvm::Type *SymtabPtrTy; |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 497 | /// ModuleTy - LLVM type for struct objc_module. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 498 | llvm::StructType *ModuleTy; |
Daniel Dunbar | cb515c8 | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 499 | |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 500 | /// ProtocolTy - LLVM type for struct objc_protocol. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 501 | llvm::StructType *ProtocolTy; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 502 | /// ProtocolPtrTy - LLVM type for struct objc_protocol *. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 503 | llvm::Type *ProtocolPtrTy; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 504 | /// ProtocolExtensionTy - LLVM type for struct |
| 505 | /// objc_protocol_extension. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 506 | llvm::StructType *ProtocolExtensionTy; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 507 | /// ProtocolExtensionTy - LLVM type for struct |
| 508 | /// objc_protocol_extension *. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 509 | llvm::Type *ProtocolExtensionPtrTy; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 510 | /// MethodDescriptionTy - LLVM type for struct |
| 511 | /// objc_method_description. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 512 | llvm::StructType *MethodDescriptionTy; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 513 | /// MethodDescriptionListTy - LLVM type for struct |
| 514 | /// objc_method_description_list. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 515 | llvm::StructType *MethodDescriptionListTy; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 516 | /// MethodDescriptionListPtrTy - LLVM type for struct |
| 517 | /// objc_method_description_list *. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 518 | llvm::Type *MethodDescriptionListPtrTy; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 519 | /// ProtocolListTy - LLVM type for struct objc_property_list. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 520 | llvm::StructType *ProtocolListTy; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 521 | /// ProtocolListPtrTy - LLVM type for struct objc_property_list*. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 522 | llvm::Type *ProtocolListPtrTy; |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 523 | /// CategoryTy - LLVM type for struct objc_category. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 524 | llvm::StructType *CategoryTy; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 525 | /// ClassTy - LLVM type for struct objc_class. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 526 | llvm::StructType *ClassTy; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 527 | /// ClassPtrTy - LLVM type for struct objc_class *. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 528 | llvm::Type *ClassPtrTy; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 529 | /// ClassExtensionTy - LLVM type for struct objc_class_ext. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 530 | llvm::StructType *ClassExtensionTy; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 531 | /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 532 | llvm::Type *ClassExtensionPtrTy; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 533 | // IvarTy - LLVM type for struct objc_ivar. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 534 | llvm::StructType *IvarTy; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 535 | /// IvarListTy - LLVM type for struct objc_ivar_list. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 536 | llvm::Type *IvarListTy; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 537 | /// IvarListPtrTy - LLVM type for struct objc_ivar_list *. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 538 | llvm::Type *IvarListPtrTy; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 539 | /// MethodListTy - LLVM type for struct objc_method_list. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 540 | llvm::Type *MethodListTy; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 541 | /// MethodListPtrTy - LLVM type for struct objc_method_list *. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 542 | llvm::Type *MethodListPtrTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 543 | |
Anders Carlsson | 9ff2248 | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 544 | /// ExceptionDataTy - LLVM type for struct _objc_exception_data. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 545 | llvm::Type *ExceptionDataTy; |
Fariborz Jahanian | 0a3cfcc | 2011-06-22 20:21:51 +0000 | [diff] [blame] | 546 | |
Anders Carlsson | 9ff2248 | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 547 | /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function. |
Chris Lattner | c6406db | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 548 | llvm::Constant *getExceptionTryEnterFn() { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 549 | llvm::Type *params[] = { ExceptionDataTy->getPointerTo() }; |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 550 | return CGM.CreateRuntimeFunction( |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 551 | llvm::FunctionType::get(CGM.VoidTy, params, false), |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 552 | "objc_exception_try_enter"); |
Chris Lattner | c6406db | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 553 | } |
Anders Carlsson | 9ff2248 | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 554 | |
| 555 | /// ExceptionTryExitFn - LLVM objc_exception_try_exit function. |
Chris Lattner | c6406db | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 556 | llvm::Constant *getExceptionTryExitFn() { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 557 | llvm::Type *params[] = { ExceptionDataTy->getPointerTo() }; |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 558 | return CGM.CreateRuntimeFunction( |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 559 | llvm::FunctionType::get(CGM.VoidTy, params, false), |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 560 | "objc_exception_try_exit"); |
Chris Lattner | c6406db | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 561 | } |
Anders Carlsson | 9ff2248 | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 562 | |
| 563 | /// ExceptionExtractFn - LLVM objc_exception_extract function. |
Chris Lattner | c6406db | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 564 | llvm::Constant *getExceptionExtractFn() { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 565 | llvm::Type *params[] = { ExceptionDataTy->getPointerTo() }; |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 566 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 567 | params, false), |
Chris Lattner | c6406db | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 568 | "objc_exception_extract"); |
Chris Lattner | c6406db | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 569 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 570 | |
Anders Carlsson | 9ff2248 | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 571 | /// ExceptionMatchFn - LLVM objc_exception_match function. |
Chris Lattner | c6406db | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 572 | llvm::Constant *getExceptionMatchFn() { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 573 | llvm::Type *params[] = { ClassPtrTy, ObjectPtrTy }; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 574 | return CGM.CreateRuntimeFunction( |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 575 | llvm::FunctionType::get(CGM.Int32Ty, params, false), |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 576 | "objc_exception_match"); |
| 577 | |
Chris Lattner | c6406db | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 578 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 579 | |
Anders Carlsson | 9ff2248 | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 580 | /// SetJmpFn - LLVM _setjmp function. |
Chris Lattner | c6406db | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 581 | llvm::Constant *getSetJmpFn() { |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 582 | // This is specifically the prototype for x86. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 583 | llvm::Type *params[] = { CGM.Int32Ty->getPointerTo() }; |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 584 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.Int32Ty, |
| 585 | params, false), |
Bill Wendling | bcefeae | 2011-11-29 00:10:10 +0000 | [diff] [blame] | 586 | "_setjmp", |
Bill Wendling | 311c832 | 2012-10-15 04:47:45 +0000 | [diff] [blame] | 587 | llvm::Attributes::get(CGM.getLLVMContext(), |
Bill Wendling | 507c351 | 2012-10-16 05:23:44 +0000 | [diff] [blame] | 588 | llvm::Attributes::NonLazyBind)); |
Chris Lattner | c6406db | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 589 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 590 | |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 591 | public: |
| 592 | ObjCTypesHelper(CodeGen::CodeGenModule &cgm); |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 593 | ~ObjCTypesHelper() {} |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 594 | }; |
| 595 | |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 596 | /// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 597 | /// modern abi |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 598 | class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper { |
Fariborz Jahanian | e4dc35d | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 599 | public: |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 600 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 601 | // MethodListnfABITy - LLVM for struct _method_list_t |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 602 | llvm::StructType *MethodListnfABITy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 603 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 604 | // MethodListnfABIPtrTy - LLVM for struct _method_list_t* |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 605 | llvm::Type *MethodListnfABIPtrTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 606 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 607 | // ProtocolnfABITy = LLVM for struct _protocol_t |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 608 | llvm::StructType *ProtocolnfABITy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 609 | |
Daniel Dunbar | 8de90f0 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 610 | // ProtocolnfABIPtrTy = LLVM for struct _protocol_t* |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 611 | llvm::Type *ProtocolnfABIPtrTy; |
Daniel Dunbar | 8de90f0 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 612 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 613 | // ProtocolListnfABITy - LLVM for struct _objc_protocol_list |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 614 | llvm::StructType *ProtocolListnfABITy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 615 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 616 | // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list* |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 617 | llvm::Type *ProtocolListnfABIPtrTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 618 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 619 | // ClassnfABITy - LLVM for struct _class_t |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 620 | llvm::StructType *ClassnfABITy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 621 | |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 622 | // ClassnfABIPtrTy - LLVM for struct _class_t* |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 623 | llvm::Type *ClassnfABIPtrTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 624 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 625 | // IvarnfABITy - LLVM for struct _ivar_t |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 626 | llvm::StructType *IvarnfABITy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 627 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 628 | // IvarListnfABITy - LLVM for struct _ivar_list_t |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 629 | llvm::StructType *IvarListnfABITy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 630 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 631 | // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t* |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 632 | llvm::Type *IvarListnfABIPtrTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 633 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 634 | // ClassRonfABITy - LLVM for struct _class_ro_t |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 635 | llvm::StructType *ClassRonfABITy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 636 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 637 | // ImpnfABITy - LLVM for id (*)(id, SEL, ...) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 638 | llvm::Type *ImpnfABITy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 639 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 640 | // CategorynfABITy - LLVM for struct _category_t |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 641 | llvm::StructType *CategorynfABITy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 642 | |
Fariborz Jahanian | 82c72e1 | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 643 | // New types for nonfragile abi messaging. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 644 | |
Fariborz Jahanian | 82c72e1 | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 645 | // MessageRefTy - LLVM for: |
| 646 | // struct _message_ref_t { |
| 647 | // IMP messenger; |
| 648 | // SEL name; |
| 649 | // }; |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 650 | llvm::StructType *MessageRefTy; |
Fariborz Jahanian | e4dc35d | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 651 | // MessageRefCTy - clang type for struct _message_ref_t |
| 652 | QualType MessageRefCTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 653 | |
Fariborz Jahanian | 82c72e1 | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 654 | // MessageRefPtrTy - LLVM for struct _message_ref_t* |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 655 | llvm::Type *MessageRefPtrTy; |
Fariborz Jahanian | e4dc35d | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 656 | // MessageRefCPtrTy - clang type for struct _message_ref_t* |
| 657 | QualType MessageRefCPtrTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 658 | |
Fariborz Jahanian | 4e87c83 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 659 | // MessengerTy - Type of the messenger (shown as IMP above) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 660 | llvm::FunctionType *MessengerTy; |
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 | // SuperMessageRefTy - LLVM for: |
| 663 | // struct _super_message_ref_t { |
| 664 | // SUPER_IMP messenger; |
| 665 | // SEL name; |
| 666 | // }; |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 667 | llvm::StructType *SuperMessageRefTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 668 | |
Fariborz Jahanian | 82c72e1 | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 669 | // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t* |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 670 | llvm::Type *SuperMessageRefPtrTy; |
Daniel Dunbar | 0b0dcd9 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 671 | |
Chris Lattner | 2dfdb3e | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 672 | llvm::Constant *getMessageSendFixupFn() { |
| 673 | // id objc_msgSend_fixup(id, struct message_ref_t*, ...) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 674 | llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy }; |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 675 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 676 | params, true), |
Chris Lattner | 2dfdb3e | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 677 | "objc_msgSend_fixup"); |
| 678 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 679 | |
Chris Lattner | 2dfdb3e | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 680 | llvm::Constant *getMessageSendFpretFixupFn() { |
| 681 | // id objc_msgSend_fpret_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_fpret_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 *getMessageSendStretFixupFn() { |
| 689 | // id objc_msgSend_stret_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_stret_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 *getMessageSendSuper2FixupFn() { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 697 | // id objc_msgSendSuper2_fixup (struct objc_super *, |
Chris Lattner | 2dfdb3e | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 698 | // struct _super_message_ref_t*, ...) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 699 | llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy }; |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 700 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 701 | params, true), |
Chris Lattner | 2dfdb3e | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 702 | "objc_msgSendSuper2_fixup"); |
| 703 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 704 | |
Chris Lattner | 2dfdb3e | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 705 | llvm::Constant *getMessageSendSuper2StretFixupFn() { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 706 | // id objc_msgSendSuper2_stret_fixup(struct objc_super *, |
Chris Lattner | 2dfdb3e | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 707 | // struct _super_message_ref_t*, ...) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 708 | llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy }; |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 709 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 710 | params, true), |
Chris Lattner | 2dfdb3e | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 711 | "objc_msgSendSuper2_stret_fixup"); |
| 712 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 713 | |
Chris Lattner | a7c00b4 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 714 | llvm::Constant *getObjCEndCatchFn() { |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 715 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy, false), |
Chris Lattner | a7c00b4 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 716 | "objc_end_catch"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 717 | |
Chris Lattner | a7c00b4 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 718 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 719 | |
Chris Lattner | a7c00b4 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 720 | llvm::Constant *getObjCBeginCatchFn() { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 721 | llvm::Type *params[] = { Int8PtrTy }; |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 722 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy, |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 723 | params, false), |
Chris Lattner | a7c00b4 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 724 | "objc_begin_catch"); |
| 725 | } |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 726 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 727 | llvm::StructType *EHTypeTy; |
| 728 | llvm::Type *EHTypePtrTy; |
Fariborz Jahanian | 0a3cfcc | 2011-06-22 20:21:51 +0000 | [diff] [blame] | 729 | |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 730 | ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm); |
| 731 | ~ObjCNonFragileABITypesHelper(){} |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 732 | }; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 733 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 734 | class CGObjCCommonMac : public CodeGen::CGObjCRuntime { |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 735 | public: |
| 736 | // FIXME - accessibility |
Fariborz Jahanian | 524bb20 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 737 | class GC_IVAR { |
Fariborz Jahanian | 3b0f886 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 738 | public: |
Daniel Dunbar | 7b89ace | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 739 | unsigned ivar_bytepos; |
| 740 | unsigned ivar_size; |
| 741 | GC_IVAR(unsigned bytepos = 0, unsigned size = 0) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 742 | : ivar_bytepos(bytepos), ivar_size(size) {} |
Daniel Dunbar | 22b0ada | 2009-04-23 01:29:05 +0000 | [diff] [blame] | 743 | |
| 744 | // Allow sorting based on byte pos. |
| 745 | bool operator<(const GC_IVAR &b) const { |
| 746 | return ivar_bytepos < b.ivar_bytepos; |
| 747 | } |
Fariborz Jahanian | 524bb20 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 748 | }; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 749 | |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 750 | class SKIP_SCAN { |
Daniel Dunbar | 7b89ace | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 751 | public: |
| 752 | unsigned skip; |
| 753 | unsigned scan; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 754 | SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0) |
Daniel Dunbar | 7b89ace | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 755 | : skip(_skip), scan(_scan) {} |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 756 | }; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 757 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 758 | protected: |
Owen Anderson | ae86c19 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 759 | llvm::LLVMContext &VMContext; |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 760 | // FIXME! May not be needing this after all. |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 761 | unsigned ObjCABI; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 762 | |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 763 | // gc ivar layout bitmap calculation helper caches. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 764 | SmallVector<GC_IVAR, 16> SkipIvars; |
| 765 | SmallVector<GC_IVAR, 16> IvarsInfo; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 766 | |
Daniel Dunbar | c61d0e9 | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 767 | /// LazySymbols - Symbols to generate a lazy reference for. See |
| 768 | /// DefinedSymbols and FinishModule(). |
Daniel Dunbar | d027a92 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 769 | llvm::SetVector<IdentifierInfo*> LazySymbols; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 770 | |
Daniel Dunbar | c61d0e9 | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 771 | /// DefinedSymbols - External symbols which are defined by this |
| 772 | /// module. The symbols in this list and LazySymbols are used to add |
| 773 | /// special linker symbols which ensure that Objective-C modules are |
| 774 | /// linked properly. |
Daniel Dunbar | d027a92 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 775 | llvm::SetVector<IdentifierInfo*> DefinedSymbols; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 776 | |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 777 | /// ClassNames - uniqued class names. |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 778 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 779 | |
Daniel Dunbar | cb515c8 | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 780 | /// MethodVarNames - uniqued method variable names. |
| 781 | llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 782 | |
Fariborz Jahanian | 9adb2e6 | 2010-06-21 22:05:18 +0000 | [diff] [blame] | 783 | /// DefinedCategoryNames - list of category names in form Class_Category. |
| 784 | llvm::SetVector<std::string> DefinedCategoryNames; |
| 785 | |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 786 | /// MethodVarTypes - uniqued method type signatures. We have to use |
| 787 | /// a StringMap here because have no other unique reference. |
| 788 | llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 789 | |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 790 | /// MethodDefinitions - map of methods which have been defined in |
| 791 | /// this translation unit. |
| 792 | llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 793 | |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 794 | /// PropertyNames - uniqued method variable names. |
| 795 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 796 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 797 | /// ClassReferences - uniqued class references. |
| 798 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 799 | |
Daniel Dunbar | cb515c8 | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 800 | /// SelectorReferences - uniqued selector references. |
| 801 | llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 802 | |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 803 | /// Protocols - Protocols for which an objc_protocol structure has |
| 804 | /// been emitted. Forward declarations are handled by creating an |
| 805 | /// empty structure whose initializer is filled in when/if defined. |
| 806 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 807 | |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 808 | /// DefinedProtocols - Protocols which have actually been |
| 809 | /// defined. We should not need this, see FIXME in GenerateProtocol. |
| 810 | llvm::DenseSet<IdentifierInfo*> DefinedProtocols; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 811 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 812 | /// DefinedClasses - List of defined classes. |
Bill Wendling | 8392a47 | 2012-02-07 09:40:07 +0000 | [diff] [blame] | 813 | llvm::SmallVector<llvm::GlobalValue*, 16> DefinedClasses; |
Daniel Dunbar | 9a017d7 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 814 | |
| 815 | /// DefinedNonLazyClasses - List of defined "non-lazy" classes. |
Bill Wendling | 8392a47 | 2012-02-07 09:40:07 +0000 | [diff] [blame] | 816 | llvm::SmallVector<llvm::GlobalValue*, 16> DefinedNonLazyClasses; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 817 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 818 | /// DefinedCategories - List of defined categories. |
Bill Wendling | 8392a47 | 2012-02-07 09:40:07 +0000 | [diff] [blame] | 819 | llvm::SmallVector<llvm::GlobalValue*, 16> DefinedCategories; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 820 | |
Daniel Dunbar | 9a017d7 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 821 | /// DefinedNonLazyCategories - List of defined "non-lazy" categories. |
Bill Wendling | 8392a47 | 2012-02-07 09:40:07 +0000 | [diff] [blame] | 822 | llvm::SmallVector<llvm::GlobalValue*, 16> DefinedNonLazyCategories; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 823 | |
Fariborz Jahanian | 0b1ccdc | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 824 | /// GetNameForMethod - Return a name for the given method. |
| 825 | /// \param[out] NameOut - The return value. |
| 826 | void GetNameForMethod(const ObjCMethodDecl *OMD, |
| 827 | const ObjCContainerDecl *CD, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 828 | SmallVectorImpl<char> &NameOut); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 829 | |
Fariborz Jahanian | 0b1ccdc | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 830 | /// GetMethodVarName - Return a unique constant for the given |
| 831 | /// selector's name. The return value has type char *. |
| 832 | llvm::Constant *GetMethodVarName(Selector Sel); |
| 833 | llvm::Constant *GetMethodVarName(IdentifierInfo *Ident); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 834 | |
Fariborz Jahanian | 0b1ccdc | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 835 | /// GetMethodVarType - Return a unique constant for the given |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 836 | /// method's type encoding string. The return value has type char *. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 837 | |
Fariborz Jahanian | 0b1ccdc | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 838 | // FIXME: This is a horrible name. |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 839 | llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D, |
| 840 | bool Extended = false); |
Daniel Dunbar | f5c1846 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 841 | llvm::Constant *GetMethodVarType(const FieldDecl *D); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 842 | |
Fariborz Jahanian | 0b1ccdc | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 843 | /// GetPropertyName - Return a unique constant for the given |
| 844 | /// name. The return value has type char *. |
| 845 | llvm::Constant *GetPropertyName(IdentifierInfo *Ident); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 846 | |
Fariborz Jahanian | 0b1ccdc | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 847 | // FIXME: This can be dropped once string functions are unified. |
| 848 | llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD, |
| 849 | const Decl *Container); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 850 | |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 851 | /// GetClassName - Return a unique constant for the given selector's |
| 852 | /// name. The return value has type char *. |
| 853 | llvm::Constant *GetClassName(IdentifierInfo *Ident); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 854 | |
Argyrios Kyrtzidis | 13257c5 | 2010-08-09 10:54:20 +0000 | [diff] [blame] | 855 | llvm::Function *GetMethodDefinition(const ObjCMethodDecl *MD); |
| 856 | |
Fariborz Jahanian | c559f3f | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 857 | /// BuildIvarLayout - Builds ivar layout bitmap for the class |
| 858 | /// implementation for the __strong or __weak case. |
| 859 | /// |
Fariborz Jahanian | 1bf7288 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 860 | llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI, |
| 861 | bool ForStrongLayout); |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 862 | |
Fariborz Jahanian | 1f78a9a | 2010-08-05 00:19:48 +0000 | [diff] [blame] | 863 | llvm::Constant *BuildIvarLayoutBitmap(std::string &BitMap); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 864 | |
Daniel Dunbar | 15bd888 | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 865 | void BuildAggrIvarRecordLayout(const RecordType *RT, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 866 | unsigned int BytePos, bool ForStrongLayout, |
| 867 | bool &HasUnion); |
Daniel Dunbar | 36e2a1e | 2009-05-03 21:05:10 +0000 | [diff] [blame] | 868 | void BuildAggrIvarLayout(const ObjCImplementationDecl *OI, |
Fariborz Jahanian | 1bf7288 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 869 | const llvm::StructLayout *Layout, |
Fariborz Jahanian | 524bb20 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 870 | const RecordDecl *RD, |
Bill Wendling | f1a3fca | 2012-02-22 09:30:11 +0000 | [diff] [blame] | 871 | ArrayRef<const FieldDecl*> RecFields, |
Fariborz Jahanian | c559f3f | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 872 | unsigned int BytePos, bool ForStrongLayout, |
Fariborz Jahanian | a123b64 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 873 | bool &HasUnion); |
Fariborz Jahanian | 1bf7288 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 874 | |
Fariborz Jahanian | 01dff42 | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 875 | /// GetIvarLayoutName - Returns a unique constant for the given |
| 876 | /// ivar layout bitmap. |
| 877 | llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident, |
| 878 | const ObjCCommonTypesHelper &ObjCTypes); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 879 | |
Fariborz Jahanian | 066347e | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 880 | /// EmitPropertyList - Emit the given property list. The return |
| 881 | /// value has type PropertyListPtrTy. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 882 | llvm::Constant *EmitPropertyList(Twine Name, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 883 | const Decl *Container, |
Fariborz Jahanian | 066347e | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 884 | const ObjCContainerDecl *OCD, |
| 885 | const ObjCCommonTypesHelper &ObjCTypes); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 886 | |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 887 | /// EmitProtocolMethodTypes - Generate the array of extended method type |
| 888 | /// strings. The return value has type Int8PtrPtrTy. |
| 889 | llvm::Constant *EmitProtocolMethodTypes(Twine Name, |
Bill Wendling | cb5b5ff | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 890 | ArrayRef<llvm::Constant*> MethodTypes, |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 891 | const ObjCCommonTypesHelper &ObjCTypes); |
| 892 | |
Fariborz Jahanian | 751c1e7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 893 | /// PushProtocolProperties - Push protocol's property on the input stack. |
Bill Wendling | a515b58 | 2012-02-09 22:16:49 +0000 | [diff] [blame] | 894 | void PushProtocolProperties( |
| 895 | llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet, |
| 896 | llvm::SmallVectorImpl<llvm::Constant*> &Properties, |
| 897 | const Decl *Container, |
| 898 | const ObjCProtocolDecl *PROTO, |
| 899 | const ObjCCommonTypesHelper &ObjCTypes); |
Fariborz Jahanian | 751c1e7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 900 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 901 | /// GetProtocolRef - Return a reference to the internal protocol |
| 902 | /// description, creating an empty one if it has not been |
| 903 | /// defined. The return value has type ProtocolPtrTy. |
| 904 | llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD); |
Fariborz Jahanian | 6772621 | 2009-03-08 20:18:37 +0000 | [diff] [blame] | 905 | |
Daniel Dunbar | 30c6536 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 906 | /// CreateMetadataVar - Create a global variable with internal |
| 907 | /// linkage for use by the Objective-C runtime. |
| 908 | /// |
| 909 | /// This is a convenience wrapper which not only creates the |
| 910 | /// variable, but also sets the section and alignment and adds the |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 911 | /// global to the "llvm.used" list. |
Daniel Dunbar | 463cc8a | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 912 | /// |
| 913 | /// \param Name - The variable name. |
| 914 | /// \param Init - The variable initializer; this is also used to |
| 915 | /// define the type of the variable. |
| 916 | /// \param Section - The section the variable should go into, or 0. |
| 917 | /// \param Align - The alignment for the variable, or 0. |
| 918 | /// \param AddToUsed - Whether the variable should be added to |
Daniel Dunbar | 4527d30 | 2009-04-14 17:42:51 +0000 | [diff] [blame] | 919 | /// "llvm.used". |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 920 | llvm::GlobalVariable *CreateMetadataVar(Twine Name, |
Daniel Dunbar | 30c6536 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 921 | llvm::Constant *Init, |
| 922 | const char *Section, |
Daniel Dunbar | 463cc8a | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 923 | unsigned Align, |
| 924 | bool AddToUsed); |
Daniel Dunbar | 30c6536 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 925 | |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 926 | CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
| 927 | ReturnValueSlot Return, |
| 928 | QualType ResultType, |
| 929 | llvm::Value *Sel, |
| 930 | llvm::Value *Arg0, |
| 931 | QualType Arg0Ty, |
| 932 | bool IsSuper, |
| 933 | const CallArgList &CallArgs, |
| 934 | const ObjCMethodDecl *OMD, |
| 935 | const ObjCCommonTypesHelper &ObjCTypes); |
Daniel Dunbar | f5c1846 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 936 | |
Daniel Dunbar | 5e63927 | 2010-04-25 20:39:01 +0000 | [diff] [blame] | 937 | /// EmitImageInfo - Emit the image info marker used to encode some module |
| 938 | /// level information. |
| 939 | void EmitImageInfo(); |
| 940 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 941 | public: |
Owen Anderson | ae86c19 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 942 | CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 943 | CGObjCRuntime(cgm), VMContext(cgm.getLLVMContext()) { } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 944 | |
David Chisnall | 481e3a8 | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 945 | virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 946 | |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 947 | virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD, |
| 948 | const ObjCContainerDecl *CD=0); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 949 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 950 | virtual void GenerateProtocol(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 951 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 952 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 953 | /// declaration, emitting it if necessary. The return value has type |
| 954 | /// ProtocolPtrTy. |
| 955 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 956 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 957 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 958 | /// object for the given declaration, emitting it if needed. These |
| 959 | /// forward references will be filled in with empty bodies if no |
| 960 | /// definition is seen. The return value has type ProtocolPtrTy. |
| 961 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0; |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 962 | virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM, |
| 963 | const CGBlockInfo &blockInfo); |
Fariborz Jahanian | c05349e | 2010-08-04 16:57:49 +0000 | [diff] [blame] | 964 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 965 | }; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 966 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 967 | class CGObjCMac : public CGObjCCommonMac { |
| 968 | private: |
| 969 | ObjCTypesHelper ObjCTypes; |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 970 | |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 971 | /// EmitModuleInfo - Another marker encoding module level |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 972 | /// information. |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 973 | void EmitModuleInfo(); |
| 974 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 975 | /// EmitModuleSymols - Emit module symbols, the list of defined |
| 976 | /// classes and categories. The result has type SymtabPtrTy. |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 977 | llvm::Constant *EmitModuleSymbols(); |
| 978 | |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 979 | /// FinishModule - Write out global data structures at the end of |
| 980 | /// processing a translation unit. |
| 981 | void FinishModule(); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 982 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 983 | /// EmitClassExtension - Generate the class extension structure used |
| 984 | /// to store the weak ivar layout and properties. The return value |
| 985 | /// has type ClassExtensionPtrTy. |
| 986 | llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID); |
| 987 | |
| 988 | /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
| 989 | /// for the given class. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 990 | llvm::Value *EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 991 | const ObjCInterfaceDecl *ID); |
Fariborz Jahanian | eb80c98 | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 992 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 993 | llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder, |
| 994 | IdentifierInfo *II); |
| 995 | |
| 996 | llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder); |
| 997 | |
Fariborz Jahanian | eb80c98 | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 998 | /// EmitSuperClassRef - Emits reference to class's main metadata class. |
| 999 | llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1000 | |
| 1001 | /// EmitIvarList - Emit the ivar list for the given |
| 1002 | /// implementation. If ForClass is true the list of class ivars |
| 1003 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 1004 | /// interface ivars will be emitted. The return value has type |
| 1005 | /// IvarListPtrTy. |
| 1006 | llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID, |
Fariborz Jahanian | b042a59 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 1007 | bool ForClass); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1008 | |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1009 | /// EmitMetaClass - Emit a forward reference to the class structure |
| 1010 | /// for the metaclass of the given interface. The return value has |
| 1011 | /// type ClassPtrTy. |
| 1012 | llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID); |
| 1013 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1014 | /// EmitMetaClass - Emit a class structure for the metaclass of the |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1015 | /// given implementation. The return value has type ClassPtrTy. |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1016 | llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID, |
| 1017 | llvm::Constant *Protocols, |
Bill Wendling | cb5b5ff | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 1018 | ArrayRef<llvm::Constant*> Methods); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1019 | |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1020 | llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1021 | |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1022 | llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1023 | |
| 1024 | /// EmitMethodList - Emit the method list for the given |
Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 1025 | /// implementation. The return value has type MethodListPtrTy. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1026 | llvm::Constant *EmitMethodList(Twine Name, |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1027 | const char *Section, |
Bill Wendling | cb5b5ff | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 1028 | ArrayRef<llvm::Constant*> Methods); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1029 | |
| 1030 | /// EmitMethodDescList - Emit a method description list for a list of |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1031 | /// method declarations. |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1032 | /// - TypeName: The name for the type containing the methods. |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 1033 | /// - IsProtocol: True iff these methods are for a protocol. |
| 1034 | /// - ClassMethds: True iff these are class methods. |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1035 | /// - Required: When true, only "required" methods are |
| 1036 | /// listed. Similarly, when false only "optional" methods are |
| 1037 | /// listed. For classes this should always be true. |
| 1038 | /// - begin, end: The method list to output. |
| 1039 | /// |
| 1040 | /// The return value has type MethodDescriptionListPtrTy. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1041 | llvm::Constant *EmitMethodDescList(Twine Name, |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1042 | const char *Section, |
Bill Wendling | cb5b5ff | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 1043 | ArrayRef<llvm::Constant*> Methods); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1044 | |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1045 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 1046 | /// declaration, emitting it if necessary. The return value has type |
| 1047 | /// ProtocolPtrTy. |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1048 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD); |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1049 | |
| 1050 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 1051 | /// object for the given declaration, emitting it if needed. These |
| 1052 | /// forward references will be filled in with empty bodies if no |
| 1053 | /// definition is seen. The return value has type ProtocolPtrTy. |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1054 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD); |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1055 | |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1056 | /// EmitProtocolExtension - Generate the protocol extension |
| 1057 | /// structure used to store optional instance and class methods, and |
| 1058 | /// protocol properties. The return value has type |
| 1059 | /// ProtocolExtensionPtrTy. |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1060 | llvm::Constant * |
| 1061 | EmitProtocolExtension(const ObjCProtocolDecl *PD, |
Bill Wendling | cb5b5ff | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 1062 | ArrayRef<llvm::Constant*> OptInstanceMethods, |
| 1063 | ArrayRef<llvm::Constant*> OptClassMethods, |
| 1064 | ArrayRef<llvm::Constant*> MethodTypesExt); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1065 | |
| 1066 | /// EmitProtocolList - Generate the list of referenced |
| 1067 | /// protocols. The return value has type ProtocolListPtrTy. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1068 | llvm::Constant *EmitProtocolList(Twine Name, |
Daniel Dunbar | dec75f8 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1069 | ObjCProtocolDecl::protocol_iterator begin, |
| 1070 | ObjCProtocolDecl::protocol_iterator end); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1071 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1072 | /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, |
| 1073 | /// for the given selector. |
Fariborz Jahanian | 9240f3d | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 1074 | llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel, |
| 1075 | bool lval=false); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1076 | |
| 1077 | public: |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1078 | CGObjCMac(CodeGen::CodeGenModule &cgm); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1079 | |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1080 | virtual llvm::Function *ModuleInitFunction(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1081 | |
Daniel Dunbar | 97db84c | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1082 | virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
John McCall | 78a1511 | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1083 | ReturnValueSlot Return, |
Daniel Dunbar | 4b8c6db | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1084 | QualType ResultType, |
| 1085 | Selector Sel, |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1086 | llvm::Value *Receiver, |
Fariborz Jahanian | f3648b8 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1087 | const CallArgList &CallArgs, |
David Chisnall | 01aa467 | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 1088 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | f3648b8 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1089 | const ObjCMethodDecl *Method); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1090 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1091 | virtual CodeGen::RValue |
Daniel Dunbar | 97db84c | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1092 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
John McCall | 78a1511 | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1093 | ReturnValueSlot Return, |
Daniel Dunbar | 4b8c6db | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1094 | QualType ResultType, |
| 1095 | Selector Sel, |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1096 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | bac73ac | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1097 | bool isCategoryImpl, |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1098 | llvm::Value *Receiver, |
Daniel Dunbar | c722b85 | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1099 | bool IsClassMessage, |
Daniel Dunbar | aff9fca | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 1100 | const CallArgList &CallArgs, |
| 1101 | const ObjCMethodDecl *Method); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1102 | |
Daniel Dunbar | cb46385 | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1103 | virtual llvm::Value *GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1104 | const ObjCInterfaceDecl *ID); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1105 | |
Fariborz Jahanian | 9240f3d | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 1106 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel, |
| 1107 | bool lval = false); |
Fariborz Jahanian | f3648b8 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1108 | |
| 1109 | /// The NeXT/Apple runtimes do not support typed selectors; just emit an |
| 1110 | /// untyped one. |
| 1111 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, |
| 1112 | const ObjCMethodDecl *Method); |
| 1113 | |
Fariborz Jahanian | 831f0fc | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 1114 | virtual llvm::Constant *GetEHType(QualType T); |
John McCall | 2ca705e | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 1115 | |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1116 | virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1117 | |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1118 | virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1119 | |
Daniel Dunbar | f3d3b01 | 2012-02-28 15:36:15 +0000 | [diff] [blame] | 1120 | virtual void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) {} |
David Chisnall | 92d436b | 2012-01-31 18:59:20 +0000 | [diff] [blame] | 1121 | |
Daniel Dunbar | cb46385 | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1122 | virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | 89da6ad | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1123 | const ObjCProtocolDecl *PD); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1124 | |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1125 | virtual llvm::Constant *GetPropertyGetFunction(); |
| 1126 | virtual llvm::Constant *GetPropertySetFunction(); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 1127 | virtual llvm::Constant *GetOptimizedPropertySetFunction(bool atomic, |
| 1128 | bool copy); |
David Chisnall | 168b80f | 2010-12-26 22:13:16 +0000 | [diff] [blame] | 1129 | virtual llvm::Constant *GetGetStructFunction(); |
| 1130 | virtual llvm::Constant *GetSetStructFunction(); |
Fariborz Jahanian | 1e1b549 | 2012-01-06 18:07:23 +0000 | [diff] [blame] | 1131 | virtual llvm::Constant *GetCppAtomicObjectFunction(); |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1132 | virtual llvm::Constant *EnumerationMutationFunction(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1133 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1134 | virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF, |
| 1135 | const ObjCAtTryStmt &S); |
| 1136 | virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 1137 | const ObjCAtSynchronizedStmt &S); |
| 1138 | void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const Stmt &S); |
Anders Carlsson | 1963b0c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 1139 | virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
| 1140 | const ObjCAtThrowStmt &S); |
Fariborz Jahanian | 83f45b55 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 1141 | virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1142 | llvm::Value *AddrWeakObj); |
Fariborz Jahanian | 83f45b55 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 1143 | virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1144 | llvm::Value *src, llvm::Value *dst); |
Fariborz Jahanian | d7db964 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 1145 | virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 217af24 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 1146 | llvm::Value *src, llvm::Value *dest, |
| 1147 | bool threadlocal = false); |
Fariborz Jahanian | e881b53 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 1148 | virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 7a95d72 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 1149 | llvm::Value *src, llvm::Value *dest, |
| 1150 | llvm::Value *ivarOffset); |
Fariborz Jahanian | d7db964 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 1151 | virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
| 1152 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 5f21d2f | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 1153 | virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF, |
| 1154 | llvm::Value *dest, llvm::Value *src, |
Fariborz Jahanian | 021510e | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 1155 | llvm::Value *size); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1156 | |
Fariborz Jahanian | 712bfa6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1157 | virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 1158 | QualType ObjectTy, |
| 1159 | llvm::Value *BaseValue, |
| 1160 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 712bfa6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1161 | unsigned CVRQualifiers); |
Fariborz Jahanian | 21fc74c | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1162 | virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 722f424 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 1163 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | 21fc74c | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1164 | const ObjCIvarDecl *Ivar); |
Fariborz Jahanian | 7bd3d1c | 2011-05-17 22:21:16 +0000 | [diff] [blame] | 1165 | |
| 1166 | /// GetClassGlobal - Return the global variable for the Objective-C |
| 1167 | /// class of the given name. |
| 1168 | virtual llvm::GlobalVariable *GetClassGlobal(const std::string &Name) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1169 | llvm_unreachable("CGObjCMac::GetClassGlobal"); |
Fariborz Jahanian | 7bd3d1c | 2011-05-17 22:21:16 +0000 | [diff] [blame] | 1170 | } |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1171 | }; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1172 | |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1173 | class CGObjCNonFragileABIMac : public CGObjCCommonMac { |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1174 | private: |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1175 | ObjCNonFragileABITypesHelper ObjCTypes; |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1176 | llvm::GlobalVariable* ObjCEmptyCacheVar; |
| 1177 | llvm::GlobalVariable* ObjCEmptyVtableVar; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1178 | |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1179 | /// SuperClassReferences - uniqued super class references. |
| 1180 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1181 | |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1182 | /// MetaClassReferences - uniqued meta class references. |
| 1183 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences; |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1184 | |
| 1185 | /// EHTypeReferences - uniqued class ehtype references. |
| 1186 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1187 | |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1188 | /// VTableDispatchMethods - List of methods for which we generate |
| 1189 | /// vtable-based message dispatch. |
| 1190 | llvm::DenseSet<Selector> VTableDispatchMethods; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1191 | |
Fariborz Jahanian | 6726055 | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 1192 | /// DefinedMetaClasses - List of defined meta-classes. |
| 1193 | std::vector<llvm::GlobalValue*> DefinedMetaClasses; |
| 1194 | |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1195 | /// isVTableDispatchedSelector - Returns true if SEL is a |
| 1196 | /// vtable-based selector. |
| 1197 | bool isVTableDispatchedSelector(Selector Sel); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1198 | |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1199 | /// FinishNonFragileABIModule - Write out global data structures at the end of |
| 1200 | /// processing a translation unit. |
| 1201 | void FinishNonFragileABIModule(); |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1202 | |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 1203 | /// AddModuleClassList - Add the given list of class pointers to the |
| 1204 | /// module with the provided symbol and section names. |
Bill Wendling | cb5b5ff | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 1205 | void AddModuleClassList(ArrayRef<llvm::GlobalValue*> Container, |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 1206 | const char *SymbolName, |
| 1207 | const char *SectionName); |
| 1208 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1209 | llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags, |
| 1210 | unsigned InstanceStart, |
| 1211 | unsigned InstanceSize, |
| 1212 | const ObjCImplementationDecl *ID); |
Fariborz Jahanian | 4723fb7 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 1213 | llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1214 | llvm::Constant *IsAGV, |
Fariborz Jahanian | 4723fb7 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 1215 | llvm::Constant *SuperClassGV, |
Fariborz Jahanian | 8220825 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 1216 | llvm::Constant *ClassRoGV, |
| 1217 | bool HiddenVisibility); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1218 | |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1219 | llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1220 | |
Fariborz Jahanian | d9c28b8 | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 1221 | llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1222 | |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1223 | /// EmitMethodList - Emit the method list for the given |
| 1224 | /// implementation. The return value has type MethodListnfABITy. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1225 | llvm::Constant *EmitMethodList(Twine Name, |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1226 | const char *Section, |
Bill Wendling | cb5b5ff | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 1227 | ArrayRef<llvm::Constant*> Methods); |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 1228 | /// EmitIvarList - Emit the ivar list for the given |
| 1229 | /// implementation. If ForClass is true the list of class ivars |
| 1230 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 1231 | /// interface ivars will be emitted. The return value has type |
| 1232 | /// IvarListnfABIPtrTy. |
| 1233 | llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1234 | |
Fariborz Jahanian | 4e7ae06 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 1235 | llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | 3d3426f | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 1236 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 40a4bcd | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 1237 | unsigned long int offset); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1238 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1239 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 1240 | /// declaration, emitting it if necessary. The return value has type |
| 1241 | /// ProtocolPtrTy. |
| 1242 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1243 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1244 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 1245 | /// object for the given declaration, emitting it if needed. These |
| 1246 | /// forward references will be filled in with empty bodies if no |
| 1247 | /// definition is seen. The return value has type ProtocolPtrTy. |
| 1248 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1249 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1250 | /// EmitProtocolList - Generate the list of referenced |
| 1251 | /// protocols. The return value has type ProtocolListPtrTy. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1252 | llvm::Constant *EmitProtocolList(Twine Name, |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1253 | ObjCProtocolDecl::protocol_iterator begin, |
Fariborz Jahanian | 3d9296e | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 1254 | ObjCProtocolDecl::protocol_iterator end); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1255 | |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1256 | CodeGen::RValue EmitVTableMessageSend(CodeGen::CodeGenFunction &CGF, |
| 1257 | ReturnValueSlot Return, |
| 1258 | QualType ResultType, |
| 1259 | Selector Sel, |
| 1260 | llvm::Value *Receiver, |
| 1261 | QualType Arg0Ty, |
| 1262 | bool IsSuper, |
| 1263 | const CallArgList &CallArgs, |
| 1264 | const ObjCMethodDecl *Method); |
Fariborz Jahanian | 7bd3d1c | 2011-05-17 22:21:16 +0000 | [diff] [blame] | 1265 | |
Daniel Dunbar | c6928bb | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 1266 | /// GetClassGlobal - Return the global variable for the Objective-C |
| 1267 | /// class of the given name. |
Fariborz Jahanian | 899e7eb | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 1268 | llvm::GlobalVariable *GetClassGlobal(const std::string &Name); |
Fariborz Jahanian | 7bd3d1c | 2011-05-17 22:21:16 +0000 | [diff] [blame] | 1269 | |
Fariborz Jahanian | 33f66e6 | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 1270 | /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1271 | /// for the given class reference. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1272 | llvm::Value *EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1273 | const ObjCInterfaceDecl *ID); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1274 | |
| 1275 | llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder, |
| 1276 | IdentifierInfo *II); |
| 1277 | |
| 1278 | llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1279 | |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1280 | /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
| 1281 | /// for the given super class reference. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1282 | llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder, |
| 1283 | const ObjCInterfaceDecl *ID); |
| 1284 | |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1285 | /// EmitMetaClassRef - Return a Value * of the address of _class_t |
| 1286 | /// meta-data |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1287 | llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder, |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1288 | const ObjCInterfaceDecl *ID); |
| 1289 | |
Fariborz Jahanian | 4e7ae06 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 1290 | /// ObjCIvarOffsetVariable - Returns the ivar offset variable for |
| 1291 | /// the given ivar. |
| 1292 | /// |
Daniel Dunbar | a106052 | 2009-04-19 00:31:15 +0000 | [diff] [blame] | 1293 | llvm::GlobalVariable * ObjCIvarOffsetVariable( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1294 | const ObjCInterfaceDecl *ID, |
| 1295 | const ObjCIvarDecl *Ivar); |
| 1296 | |
Fariborz Jahanian | 74b7722 | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 1297 | /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, |
| 1298 | /// for the given selector. |
Fariborz Jahanian | 9240f3d | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 1299 | llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel, |
| 1300 | bool lval=false); |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1301 | |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1302 | /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1303 | /// interface. The return value has type EHTypePtrTy. |
John McCall | 2ca705e | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 1304 | llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID, |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1305 | bool ForDefinition); |
Daniel Dunbar | 15894b7 | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 1306 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1307 | const char *getMetaclassSymbolPrefix() const { |
Daniel Dunbar | 15894b7 | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 1308 | return "OBJC_METACLASS_$_"; |
| 1309 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1310 | |
Daniel Dunbar | 15894b7 | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 1311 | const char *getClassSymbolPrefix() const { |
| 1312 | return "OBJC_CLASS_$_"; |
| 1313 | } |
| 1314 | |
Daniel Dunbar | 96120237 | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 1315 | void GetClassSizeInfo(const ObjCImplementationDecl *OID, |
Daniel Dunbar | 554fd79 | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 1316 | uint32_t &InstanceStart, |
| 1317 | uint32_t &InstanceSize); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1318 | |
Fariborz Jahanian | e412864 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1319 | // Shamelessly stolen from Analysis/CFRefCount.cpp |
Daniel Dunbar | 9a017d7 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1320 | Selector GetNullarySelector(const char* name) const { |
Fariborz Jahanian | e412864 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1321 | IdentifierInfo* II = &CGM.getContext().Idents.get(name); |
| 1322 | return CGM.getContext().Selectors.getSelector(0, &II); |
| 1323 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1324 | |
Daniel Dunbar | 9a017d7 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1325 | Selector GetUnarySelector(const char* name) const { |
Fariborz Jahanian | e412864 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1326 | IdentifierInfo* II = &CGM.getContext().Idents.get(name); |
| 1327 | return CGM.getContext().Selectors.getSelector(1, &II); |
| 1328 | } |
Daniel Dunbar | 554fd79 | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 1329 | |
Daniel Dunbar | 9a017d7 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1330 | /// ImplementationIsNonLazy - Check whether the given category or |
| 1331 | /// class implementation is "non-lazy". |
Fariborz Jahanian | a6bed83 | 2009-05-21 01:03:45 +0000 | [diff] [blame] | 1332 | bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const; |
Daniel Dunbar | 9a017d7 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1333 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1334 | public: |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1335 | CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm); |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1336 | // FIXME. All stubs for now! |
| 1337 | virtual llvm::Function *ModuleInitFunction(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1338 | |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1339 | virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
John McCall | 78a1511 | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1340 | ReturnValueSlot Return, |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1341 | QualType ResultType, |
| 1342 | Selector Sel, |
| 1343 | llvm::Value *Receiver, |
Fariborz Jahanian | f3648b8 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1344 | const CallArgList &CallArgs, |
David Chisnall | 01aa467 | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 1345 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | f3648b8 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1346 | const ObjCMethodDecl *Method); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1347 | |
| 1348 | virtual CodeGen::RValue |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1349 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
John McCall | 78a1511 | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1350 | ReturnValueSlot Return, |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1351 | QualType ResultType, |
| 1352 | Selector Sel, |
| 1353 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | bac73ac | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1354 | bool isCategoryImpl, |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1355 | llvm::Value *Receiver, |
| 1356 | bool IsClassMessage, |
Daniel Dunbar | aff9fca | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 1357 | const CallArgList &CallArgs, |
| 1358 | const ObjCMethodDecl *Method); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1359 | |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1360 | virtual llvm::Value *GetClass(CGBuilderTy &Builder, |
Fariborz Jahanian | 33f66e6 | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 1361 | const ObjCInterfaceDecl *ID); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1362 | |
Fariborz Jahanian | 9240f3d | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 1363 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel, |
| 1364 | bool lvalue = false) |
| 1365 | { return EmitSelector(Builder, Sel, lvalue); } |
Fariborz Jahanian | f3648b8 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1366 | |
| 1367 | /// The NeXT/Apple runtimes do not support typed selectors; just emit an |
| 1368 | /// untyped one. |
| 1369 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, |
| 1370 | const ObjCMethodDecl *Method) |
| 1371 | { return EmitSelector(Builder, Method->getSelector()); } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1372 | |
Fariborz Jahanian | 0c8d060 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 1373 | virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1374 | |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1375 | virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); |
David Chisnall | 92d436b | 2012-01-31 18:59:20 +0000 | [diff] [blame] | 1376 | |
Daniel Dunbar | f3d3b01 | 2012-02-28 15:36:15 +0000 | [diff] [blame] | 1377 | virtual void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) {} |
David Chisnall | 92d436b | 2012-01-31 18:59:20 +0000 | [diff] [blame] | 1378 | |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1379 | virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, |
Fariborz Jahanian | 097feda | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 1380 | const ObjCProtocolDecl *PD); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1381 | |
Fariborz Jahanian | 831f0fc | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 1382 | virtual llvm::Constant *GetEHType(QualType T); |
John McCall | 2ca705e | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 1383 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1384 | virtual llvm::Constant *GetPropertyGetFunction() { |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 1385 | return ObjCTypes.getGetPropertyFn(); |
Fariborz Jahanian | 21fc74c | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1386 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1387 | virtual llvm::Constant *GetPropertySetFunction() { |
| 1388 | return ObjCTypes.getSetPropertyFn(); |
Fariborz Jahanian | 21fc74c | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1389 | } |
Fariborz Jahanian | 5a8c203 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 1390 | |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 1391 | virtual llvm::Constant *GetOptimizedPropertySetFunction(bool atomic, |
| 1392 | bool copy) { |
| 1393 | return ObjCTypes.getOptimizedSetPropertyFn(atomic, copy); |
| 1394 | } |
| 1395 | |
David Chisnall | 168b80f | 2010-12-26 22:13:16 +0000 | [diff] [blame] | 1396 | virtual llvm::Constant *GetSetStructFunction() { |
| 1397 | return ObjCTypes.getCopyStructFn(); |
| 1398 | } |
| 1399 | virtual llvm::Constant *GetGetStructFunction() { |
Fariborz Jahanian | 5a8c203 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 1400 | return ObjCTypes.getCopyStructFn(); |
| 1401 | } |
Fariborz Jahanian | 1e1b549 | 2012-01-06 18:07:23 +0000 | [diff] [blame] | 1402 | virtual llvm::Constant *GetCppAtomicObjectFunction() { |
| 1403 | return ObjCTypes.getCppAtomicObjectFunction(); |
| 1404 | } |
Fariborz Jahanian | 5a8c203 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 1405 | |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1406 | virtual llvm::Constant *EnumerationMutationFunction() { |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 1407 | return ObjCTypes.getEnumerationMutationFn(); |
Daniel Dunbar | d73ea816 | 2009-02-16 18:48:45 +0000 | [diff] [blame] | 1408 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1409 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1410 | virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF, |
| 1411 | const ObjCAtTryStmt &S); |
| 1412 | virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 1413 | const ObjCAtSynchronizedStmt &S); |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1414 | virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Anders Carlsson | 9ab53d1 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 1415 | const ObjCAtThrowStmt &S); |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1416 | virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1417 | llvm::Value *AddrWeakObj); |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1418 | virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1419 | llvm::Value *src, llvm::Value *dst); |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1420 | virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 217af24 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 1421 | llvm::Value *src, llvm::Value *dest, |
| 1422 | bool threadlocal = false); |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1423 | virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 7a95d72 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 1424 | llvm::Value *src, llvm::Value *dest, |
| 1425 | llvm::Value *ivarOffset); |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1426 | virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1427 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 5f21d2f | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 1428 | virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF, |
| 1429 | llvm::Value *dest, llvm::Value *src, |
Fariborz Jahanian | 021510e | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 1430 | llvm::Value *size); |
Fariborz Jahanian | 712bfa6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1431 | virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 1432 | QualType ObjectTy, |
| 1433 | llvm::Value *BaseValue, |
| 1434 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 712bfa6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1435 | unsigned CVRQualifiers); |
Fariborz Jahanian | 21fc74c | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1436 | virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 722f424 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 1437 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | 21fc74c | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1438 | const ObjCIvarDecl *Ivar); |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1439 | }; |
Fariborz Jahanian | 715fdd5 | 2012-01-29 20:27:13 +0000 | [diff] [blame] | 1440 | |
| 1441 | /// A helper class for performing the null-initialization of a return |
| 1442 | /// value. |
| 1443 | struct NullReturnState { |
| 1444 | llvm::BasicBlock *NullBB; |
| 1445 | llvm::BasicBlock *callBB; |
| 1446 | NullReturnState() : NullBB(0), callBB(0) {} |
| 1447 | |
| 1448 | void init(CodeGenFunction &CGF, llvm::Value *receiver) { |
| 1449 | // Make blocks for the null-init and call edges. |
| 1450 | NullBB = CGF.createBasicBlock("msgSend.nullinit"); |
| 1451 | callBB = CGF.createBasicBlock("msgSend.call"); |
| 1452 | |
| 1453 | // Check for a null receiver and, if there is one, jump to the |
| 1454 | // null-init test. |
| 1455 | llvm::Value *isNull = CGF.Builder.CreateIsNull(receiver); |
| 1456 | CGF.Builder.CreateCondBr(isNull, NullBB, callBB); |
| 1457 | |
| 1458 | // Otherwise, start performing the call. |
| 1459 | CGF.EmitBlock(callBB); |
| 1460 | } |
| 1461 | |
Fariborz Jahanian | f2bda69 | 2012-01-30 21:40:37 +0000 | [diff] [blame] | 1462 | RValue complete(CodeGenFunction &CGF, RValue result, QualType resultType, |
| 1463 | const CallArgList &CallArgs, |
| 1464 | const ObjCMethodDecl *Method) { |
Fariborz Jahanian | 715fdd5 | 2012-01-29 20:27:13 +0000 | [diff] [blame] | 1465 | if (!NullBB) return result; |
Fariborz Jahanian | f2bda69 | 2012-01-30 21:40:37 +0000 | [diff] [blame] | 1466 | |
| 1467 | llvm::Value *NullInitPtr = 0; |
| 1468 | if (result.isScalar() && !resultType->isVoidType()) { |
| 1469 | NullInitPtr = CGF.CreateTempAlloca(result.getScalarVal()->getType()); |
| 1470 | CGF.Builder.CreateStore(result.getScalarVal(), NullInitPtr); |
| 1471 | } |
Fariborz Jahanian | 715fdd5 | 2012-01-29 20:27:13 +0000 | [diff] [blame] | 1472 | |
| 1473 | // Finish the call path. |
| 1474 | llvm::BasicBlock *contBB = CGF.createBasicBlock("msgSend.cont"); |
| 1475 | if (CGF.HaveInsertPoint()) CGF.Builder.CreateBr(contBB); |
| 1476 | |
| 1477 | // Emit the null-init block and perform the null-initialization there. |
| 1478 | CGF.EmitBlock(NullBB); |
Fariborz Jahanian | f2bda69 | 2012-01-30 21:40:37 +0000 | [diff] [blame] | 1479 | |
| 1480 | // Release consumed arguments along the null-receiver path. |
| 1481 | if (Method) { |
| 1482 | CallArgList::const_iterator I = CallArgs.begin(); |
| 1483 | for (ObjCMethodDecl::param_const_iterator i = Method->param_begin(), |
| 1484 | e = Method->param_end(); i != e; ++i, ++I) { |
| 1485 | const ParmVarDecl *ParamDecl = (*i); |
| 1486 | if (ParamDecl->hasAttr<NSConsumedAttr>()) { |
| 1487 | RValue RV = I->RV; |
| 1488 | assert(RV.isScalar() && |
| 1489 | "NullReturnState::complete - arg not on object"); |
| 1490 | CGF.EmitARCRelease(RV.getScalarVal(), true); |
| 1491 | } |
| 1492 | } |
| 1493 | } |
| 1494 | |
| 1495 | if (result.isScalar()) { |
| 1496 | if (NullInitPtr) |
| 1497 | CGF.EmitNullInitialization(NullInitPtr, resultType); |
| 1498 | // Jump to the continuation block. |
| 1499 | CGF.EmitBlock(contBB); |
| 1500 | return NullInitPtr ? RValue::get(CGF.Builder.CreateLoad(NullInitPtr)) |
| 1501 | : result; |
| 1502 | } |
| 1503 | |
Fariborz Jahanian | 715fdd5 | 2012-01-29 20:27:13 +0000 | [diff] [blame] | 1504 | if (!resultType->isAnyComplexType()) { |
| 1505 | assert(result.isAggregate() && "null init of non-aggregate result?"); |
| 1506 | CGF.EmitNullInitialization(result.getAggregateAddr(), resultType); |
| 1507 | // Jump to the continuation block. |
| 1508 | CGF.EmitBlock(contBB); |
| 1509 | return result; |
| 1510 | } |
| 1511 | |
| 1512 | // _Complex type |
| 1513 | // FIXME. Now easy to handle any other scalar type whose result is returned |
| 1514 | // in memory due to ABI limitations. |
| 1515 | CGF.EmitBlock(contBB); |
| 1516 | CodeGenFunction::ComplexPairTy CallCV = result.getComplexVal(); |
| 1517 | llvm::Type *MemberType = CallCV.first->getType(); |
| 1518 | llvm::Constant *ZeroCV = llvm::Constant::getNullValue(MemberType); |
| 1519 | // Create phi instruction for scalar complex value. |
| 1520 | llvm::PHINode *PHIReal = CGF.Builder.CreatePHI(MemberType, 2); |
| 1521 | PHIReal->addIncoming(ZeroCV, NullBB); |
| 1522 | PHIReal->addIncoming(CallCV.first, callBB); |
| 1523 | llvm::PHINode *PHIImag = CGF.Builder.CreatePHI(MemberType, 2); |
| 1524 | PHIImag->addIncoming(ZeroCV, NullBB); |
| 1525 | PHIImag->addIncoming(CallCV.second, callBB); |
| 1526 | return RValue::getComplex(PHIReal, PHIImag); |
| 1527 | } |
| 1528 | }; |
| 1529 | |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1530 | } // end anonymous namespace |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1531 | |
| 1532 | /* *** Helper Functions *** */ |
| 1533 | |
| 1534 | /// getConstantGEP() - Help routine to construct simple GEPs. |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1535 | static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1536 | llvm::Constant *C, |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1537 | unsigned idx0, |
| 1538 | unsigned idx1) { |
| 1539 | llvm::Value *Idxs[] = { |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1540 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0), |
| 1541 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1) |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1542 | }; |
Jay Foad | ed8db7d | 2011-07-21 14:31:17 +0000 | [diff] [blame] | 1543 | return llvm::ConstantExpr::getGetElementPtr(C, Idxs); |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1544 | } |
| 1545 | |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1546 | /// hasObjCExceptionAttribute - Return true if this class or any super |
| 1547 | /// class has the __objc_exception__ attribute. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1548 | static bool hasObjCExceptionAttribute(ASTContext &Context, |
Douglas Gregor | 78bd61f | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 1549 | const ObjCInterfaceDecl *OID) { |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1550 | if (OID->hasAttr<ObjCExceptionAttr>()) |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1551 | return true; |
| 1552 | if (const ObjCInterfaceDecl *Super = OID->getSuperClass()) |
Douglas Gregor | 78bd61f | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 1553 | return hasObjCExceptionAttribute(Context, Super); |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1554 | return false; |
| 1555 | } |
| 1556 | |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1557 | /* *** CGObjCMac Public Interface *** */ |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1558 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1559 | CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1560 | ObjCTypes(cgm) { |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1561 | ObjCABI = 1; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1562 | EmitImageInfo(); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1563 | } |
| 1564 | |
Daniel Dunbar | 7c6d3a7 | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 1565 | /// GetClass - Return a reference to the class for the given interface |
| 1566 | /// decl. |
Daniel Dunbar | cb46385 | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1567 | llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1568 | const ObjCInterfaceDecl *ID) { |
| 1569 | return EmitClassRef(Builder, ID); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1570 | } |
| 1571 | |
| 1572 | /// GetSelector - Return the pointer to the unique'd string for this selector. |
Fariborz Jahanian | 9240f3d | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 1573 | llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel, |
| 1574 | bool lval) { |
| 1575 | return EmitSelector(Builder, Sel, lval); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1576 | } |
Fariborz Jahanian | f3648b8 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1577 | llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1578 | *Method) { |
Fariborz Jahanian | f3648b8 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1579 | return EmitSelector(Builder, Method->getSelector()); |
| 1580 | } |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1581 | |
Fariborz Jahanian | 831f0fc | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 1582 | llvm::Constant *CGObjCMac::GetEHType(QualType T) { |
Fariborz Jahanian | 0a3cfcc | 2011-06-22 20:21:51 +0000 | [diff] [blame] | 1583 | if (T->isObjCIdType() || |
| 1584 | T->isObjCQualifiedIdType()) { |
| 1585 | return CGM.GetAddrOfRTTIDescriptor( |
Douglas Gregor | 9767347 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 1586 | CGM.getContext().getObjCIdRedefinitionType(), /*ForEH=*/true); |
Fariborz Jahanian | 0a3cfcc | 2011-06-22 20:21:51 +0000 | [diff] [blame] | 1587 | } |
Fariborz Jahanian | 831f0fc | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 1588 | if (T->isObjCClassType() || |
| 1589 | T->isObjCQualifiedClassType()) { |
| 1590 | return CGM.GetAddrOfRTTIDescriptor( |
Douglas Gregor | 9767347 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 1591 | CGM.getContext().getObjCClassRedefinitionType(), /*ForEH=*/true); |
Fariborz Jahanian | 831f0fc | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 1592 | } |
| 1593 | if (T->isObjCObjectPointerType()) |
| 1594 | return CGM.GetAddrOfRTTIDescriptor(T, /*ForEH=*/true); |
| 1595 | |
John McCall | 2ca705e | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 1596 | llvm_unreachable("asking for catch type for ObjC type in fragile runtime"); |
John McCall | 2ca705e | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 1597 | } |
| 1598 | |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1599 | /// Generate a constant CFString object. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1600 | /* |
| 1601 | struct __builtin_CFString { |
| 1602 | const int *isa; // point to __CFConstantStringClassReference |
| 1603 | int flags; |
| 1604 | const char *str; |
| 1605 | long length; |
| 1606 | }; |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1607 | */ |
| 1608 | |
Fariborz Jahanian | 63408e8 | 2010-04-22 20:26:39 +0000 | [diff] [blame] | 1609 | /// or Generate a constant NSString object. |
| 1610 | /* |
| 1611 | struct __builtin_NSString { |
| 1612 | const int *isa; // point to __NSConstantStringClassReference |
| 1613 | const char *str; |
| 1614 | unsigned int length; |
| 1615 | }; |
| 1616 | */ |
| 1617 | |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1618 | llvm::Constant *CGObjCCommonMac::GenerateConstantString( |
David Chisnall | 481e3a8 | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 1619 | const StringLiteral *SL) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1620 | return (CGM.getLangOpts().NoConstantCFStrings == 0 ? |
Fariborz Jahanian | 63408e8 | 2010-04-22 20:26:39 +0000 | [diff] [blame] | 1621 | CGM.GetAddrOfConstantCFString(SL) : |
Fariborz Jahanian | 50c925f | 2010-10-19 17:19:29 +0000 | [diff] [blame] | 1622 | CGM.GetAddrOfConstantString(SL)); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1623 | } |
| 1624 | |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 1625 | enum { |
| 1626 | kCFTaggedObjectID_Integer = (1 << 1) + 1 |
| 1627 | }; |
| 1628 | |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1629 | /// Generates a message send where the super is the receiver. This is |
| 1630 | /// a message send to self with special delivery semantics indicating |
| 1631 | /// which class's method should be called. |
Daniel Dunbar | 97db84c | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1632 | CodeGen::RValue |
| 1633 | CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
John McCall | 78a1511 | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1634 | ReturnValueSlot Return, |
Daniel Dunbar | 4b8c6db | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1635 | QualType ResultType, |
| 1636 | Selector Sel, |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1637 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | bac73ac | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1638 | bool isCategoryImpl, |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1639 | llvm::Value *Receiver, |
Daniel Dunbar | c722b85 | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1640 | bool IsClassMessage, |
Daniel Dunbar | aff9fca | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 1641 | const CodeGen::CallArgList &CallArgs, |
| 1642 | const ObjCMethodDecl *Method) { |
Daniel Dunbar | f6397fe | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1643 | // Create and init a super structure; this is a (receiver, class) |
| 1644 | // pair we will pass to objc_msgSendSuper. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1645 | llvm::Value *ObjCSuper = |
John McCall | 6647584 | 2011-03-04 08:00:29 +0000 | [diff] [blame] | 1646 | CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1647 | llvm::Value *ReceiverAsObject = |
Daniel Dunbar | f6397fe | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1648 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1649 | CGF.Builder.CreateStore(ReceiverAsObject, |
Daniel Dunbar | f6397fe | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1650 | CGF.Builder.CreateStructGEP(ObjCSuper, 0)); |
Daniel Dunbar | f6397fe | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1651 | |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1652 | // If this is a class message the metaclass is passed as the target. |
| 1653 | llvm::Value *Target; |
| 1654 | if (IsClassMessage) { |
Fariborz Jahanian | bac73ac | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1655 | if (isCategoryImpl) { |
| 1656 | // Message sent to 'super' in a class method defined in a category |
| 1657 | // implementation requires an odd treatment. |
| 1658 | // If we are in a class method, we must retrieve the |
| 1659 | // _metaclass_ for the current class, pointed at by |
| 1660 | // the class's "isa" pointer. The following assumes that |
| 1661 | // isa" is the first ivar in a class (which it must be). |
| 1662 | Target = EmitClassRef(CGF.Builder, Class->getSuperClass()); |
| 1663 | Target = CGF.Builder.CreateStructGEP(Target, 0); |
| 1664 | Target = CGF.Builder.CreateLoad(Target); |
Mike Stump | 658fe02 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 1665 | } else { |
Fariborz Jahanian | bac73ac | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1666 | llvm::Value *MetaClassPtr = EmitMetaClassRef(Class); |
| 1667 | llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1); |
| 1668 | llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr); |
| 1669 | Target = Super; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1670 | } |
Fariborz Jahanian | da2efb0 | 2009-11-14 02:18:31 +0000 | [diff] [blame] | 1671 | } |
| 1672 | else if (isCategoryImpl) |
| 1673 | Target = EmitClassRef(CGF.Builder, Class->getSuperClass()); |
| 1674 | else { |
Fariborz Jahanian | eb80c98 | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 1675 | llvm::Value *ClassPtr = EmitSuperClassRef(Class); |
| 1676 | ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1); |
| 1677 | Target = CGF.Builder.CreateLoad(ClassPtr); |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1678 | } |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1679 | // FIXME: We shouldn't need to do this cast, rectify the ASTContext and |
| 1680 | // ObjCTypes types. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1681 | llvm::Type *ClassTy = |
Daniel Dunbar | c722b85 | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1682 | CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1683 | Target = CGF.Builder.CreateBitCast(Target, ClassTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1684 | CGF.Builder.CreateStore(Target, |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1685 | CGF.Builder.CreateStructGEP(ObjCSuper, 1)); |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1686 | return EmitMessageSend(CGF, Return, ResultType, |
| 1687 | EmitSelector(CGF.Builder, Sel), |
| 1688 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
| 1689 | true, CallArgs, Method, ObjCTypes); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1690 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1691 | |
| 1692 | /// Generate code for a message send expression. |
Daniel Dunbar | 97db84c | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1693 | CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
John McCall | 78a1511 | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1694 | ReturnValueSlot Return, |
Daniel Dunbar | 4b8c6db | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1695 | QualType ResultType, |
| 1696 | Selector Sel, |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1697 | llvm::Value *Receiver, |
Fariborz Jahanian | f3648b8 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1698 | const CallArgList &CallArgs, |
David Chisnall | 01aa467 | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 1699 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | f3648b8 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1700 | const ObjCMethodDecl *Method) { |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1701 | return EmitMessageSend(CGF, Return, ResultType, |
| 1702 | EmitSelector(CGF.Builder, Sel), |
| 1703 | Receiver, CGF.getContext().getObjCIdType(), |
| 1704 | false, CallArgs, Method, ObjCTypes); |
Daniel Dunbar | 97ff50d | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1705 | } |
| 1706 | |
Daniel Dunbar | aff9fca | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 1707 | CodeGen::RValue |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1708 | CGObjCCommonMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
| 1709 | ReturnValueSlot Return, |
| 1710 | QualType ResultType, |
| 1711 | llvm::Value *Sel, |
| 1712 | llvm::Value *Arg0, |
| 1713 | QualType Arg0Ty, |
| 1714 | bool IsSuper, |
| 1715 | const CallArgList &CallArgs, |
| 1716 | const ObjCMethodDecl *Method, |
| 1717 | const ObjCCommonTypesHelper &ObjCTypes) { |
Daniel Dunbar | c722b85 | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1718 | CallArgList ActualArgs; |
Fariborz Jahanian | 969bc68 | 2009-04-24 21:07:43 +0000 | [diff] [blame] | 1719 | if (!IsSuper) |
Benjamin Kramer | 76399eb | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 1720 | Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy); |
Eli Friedman | 43dca6a | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 1721 | ActualArgs.add(RValue::get(Arg0), Arg0Ty); |
| 1722 | ActualArgs.add(RValue::get(Sel), CGF.getContext().getObjCSelType()); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1723 | ActualArgs.addFrom(CallArgs); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1724 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1725 | // If we're calling a method, use the formal signature. |
| 1726 | MessageSendInfo MSI = getMessageSendInfo(Method, ResultType, ActualArgs); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1727 | |
Anders Carlsson | 280e61f1 | 2010-06-21 20:59:55 +0000 | [diff] [blame] | 1728 | if (Method) |
| 1729 | assert(CGM.getContext().getCanonicalType(Method->getResultType()) == |
| 1730 | CGM.getContext().getCanonicalType(ResultType) && |
| 1731 | "Result type mismatch!"); |
| 1732 | |
John McCall | 5880fb8 | 2011-05-14 21:12:11 +0000 | [diff] [blame] | 1733 | NullReturnState nullReturn; |
| 1734 | |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1735 | llvm::Constant *Fn = NULL; |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1736 | if (CGM.ReturnTypeUsesSRet(MSI.CallInfo)) { |
Fariborz Jahanian | 715fdd5 | 2012-01-29 20:27:13 +0000 | [diff] [blame] | 1737 | if (!IsSuper) nullReturn.init(CGF, Arg0); |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1738 | Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1739 | : ObjCTypes.getSendStretFn(IsSuper); |
Daniel Dunbar | 6f2e839 | 2010-07-14 23:39:36 +0000 | [diff] [blame] | 1740 | } else if (CGM.ReturnTypeUsesFPRet(ResultType)) { |
| 1741 | Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper) |
| 1742 | : ObjCTypes.getSendFpretFn(IsSuper); |
Anders Carlsson | 2f1a6c3 | 2011-10-31 16:27:11 +0000 | [diff] [blame] | 1743 | } else if (CGM.ReturnTypeUsesFP2Ret(ResultType)) { |
| 1744 | Fn = (ObjCABI == 2) ? ObjCTypes.getSendFp2RetFn2(IsSuper) |
| 1745 | : ObjCTypes.getSendFp2retFn(IsSuper); |
Daniel Dunbar | 3c683f5b | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 1746 | } else { |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1747 | Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1748 | : ObjCTypes.getSendFn(IsSuper); |
Daniel Dunbar | 3c683f5b | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 1749 | } |
Fariborz Jahanian | f2bda69 | 2012-01-30 21:40:37 +0000 | [diff] [blame] | 1750 | |
| 1751 | bool requiresnullCheck = false; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1752 | if (CGM.getLangOpts().ObjCAutoRefCount && Method) |
Fariborz Jahanian | f2bda69 | 2012-01-30 21:40:37 +0000 | [diff] [blame] | 1753 | for (ObjCMethodDecl::param_const_iterator i = Method->param_begin(), |
| 1754 | e = Method->param_end(); i != e; ++i) { |
| 1755 | const ParmVarDecl *ParamDecl = (*i); |
| 1756 | if (ParamDecl->hasAttr<NSConsumedAttr>()) { |
| 1757 | if (!nullReturn.NullBB) |
| 1758 | nullReturn.init(CGF, Arg0); |
| 1759 | requiresnullCheck = true; |
| 1760 | break; |
| 1761 | } |
| 1762 | } |
| 1763 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1764 | Fn = llvm::ConstantExpr::getBitCast(Fn, MSI.MessengerType); |
| 1765 | RValue rvalue = CGF.EmitCall(MSI.CallInfo, Fn, Return, ActualArgs); |
Fariborz Jahanian | f2bda69 | 2012-01-30 21:40:37 +0000 | [diff] [blame] | 1766 | return nullReturn.complete(CGF, rvalue, ResultType, CallArgs, |
| 1767 | requiresnullCheck ? Method : 0); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1768 | } |
| 1769 | |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1770 | static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) { |
| 1771 | if (FQT.isObjCGCStrong()) |
| 1772 | return Qualifiers::Strong; |
| 1773 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1774 | if (FQT.isObjCGCWeak() || FQT.getObjCLifetime() == Qualifiers::OCL_Weak) |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1775 | return Qualifiers::Weak; |
| 1776 | |
Fariborz Jahanian | 430b35e | 2012-02-16 00:15:02 +0000 | [diff] [blame] | 1777 | // check for __unsafe_unretained |
| 1778 | if (FQT.getObjCLifetime() == Qualifiers::OCL_ExplicitNone) |
| 1779 | return Qualifiers::GCNone; |
| 1780 | |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1781 | if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType()) |
| 1782 | return Qualifiers::Strong; |
| 1783 | |
| 1784 | if (const PointerType *PT = FQT->getAs<PointerType>()) |
| 1785 | return GetGCAttrTypeForType(Ctx, PT->getPointeeType()); |
| 1786 | |
| 1787 | return Qualifiers::GCNone; |
| 1788 | } |
| 1789 | |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1790 | llvm::Constant *CGObjCCommonMac::BuildGCBlockLayout(CodeGenModule &CGM, |
| 1791 | const CGBlockInfo &blockInfo) { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1792 | llvm::Constant *nullPtr = llvm::Constant::getNullValue(CGM.Int8PtrTy); |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1793 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1794 | if (CGM.getLangOpts().getGC() == LangOptions::NonGC && |
| 1795 | !CGM.getLangOpts().ObjCAutoRefCount) |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1796 | return nullPtr; |
| 1797 | |
Fariborz Jahanian | f95e358 | 2010-08-06 16:28:55 +0000 | [diff] [blame] | 1798 | bool hasUnion = false; |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1799 | SkipIvars.clear(); |
| 1800 | IvarsInfo.clear(); |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1801 | unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0); |
| 1802 | unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth(); |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1803 | |
Fariborz Jahanian | cfddabf | 2010-09-09 00:21:45 +0000 | [diff] [blame] | 1804 | // __isa is the first field in block descriptor and must assume by runtime's |
| 1805 | // convention that it is GC'able. |
| 1806 | IvarsInfo.push_back(GC_IVAR(0, 1)); |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1807 | |
| 1808 | const BlockDecl *blockDecl = blockInfo.getBlockDecl(); |
| 1809 | |
| 1810 | // Calculate the basic layout of the block structure. |
| 1811 | const llvm::StructLayout *layout = |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 1812 | CGM.getDataLayout().getStructLayout(blockInfo.StructureType); |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1813 | |
| 1814 | // Ignore the optional 'this' capture: C++ objects are not assumed |
| 1815 | // to be GC'ed. |
| 1816 | |
| 1817 | // Walk the captured variables. |
| 1818 | for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(), |
| 1819 | ce = blockDecl->capture_end(); ci != ce; ++ci) { |
| 1820 | const VarDecl *variable = ci->getVariable(); |
| 1821 | QualType type = variable->getType(); |
| 1822 | |
| 1823 | const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable); |
| 1824 | |
| 1825 | // Ignore constant captures. |
| 1826 | if (capture.isConstant()) continue; |
| 1827 | |
| 1828 | uint64_t fieldOffset = layout->getElementOffset(capture.getIndex()); |
| 1829 | |
| 1830 | // __block variables are passed by their descriptor address. |
| 1831 | if (ci->isByRef()) { |
| 1832 | IvarsInfo.push_back(GC_IVAR(fieldOffset, /*size in words*/ 1)); |
Fariborz Jahanian | 933c672 | 2010-09-11 01:27:29 +0000 | [diff] [blame] | 1833 | continue; |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1834 | } |
| 1835 | |
| 1836 | assert(!type->isArrayType() && "array variable should not be caught"); |
| 1837 | if (const RecordType *record = type->getAs<RecordType>()) { |
| 1838 | BuildAggrIvarRecordLayout(record, fieldOffset, true, hasUnion); |
Fariborz Jahanian | 903aba3 | 2010-08-05 21:00:25 +0000 | [diff] [blame] | 1839 | continue; |
| 1840 | } |
Fariborz Jahanian | f95e358 | 2010-08-06 16:28:55 +0000 | [diff] [blame] | 1841 | |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1842 | Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), type); |
| 1843 | unsigned fieldSize = CGM.getContext().getTypeSize(type); |
| 1844 | |
| 1845 | if (GCAttr == Qualifiers::Strong) |
| 1846 | IvarsInfo.push_back(GC_IVAR(fieldOffset, |
| 1847 | fieldSize / WordSizeInBits)); |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1848 | else if (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak) |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1849 | SkipIvars.push_back(GC_IVAR(fieldOffset, |
| 1850 | fieldSize / ByteSizeInBits)); |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1851 | } |
| 1852 | |
| 1853 | if (IvarsInfo.empty()) |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1854 | return nullPtr; |
| 1855 | |
| 1856 | // Sort on byte position; captures might not be allocated in order, |
| 1857 | // and unions can do funny things. |
| 1858 | llvm::array_pod_sort(IvarsInfo.begin(), IvarsInfo.end()); |
| 1859 | llvm::array_pod_sort(SkipIvars.begin(), SkipIvars.end()); |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1860 | |
| 1861 | std::string BitMap; |
Fariborz Jahanian | 1f78a9a | 2010-08-05 00:19:48 +0000 | [diff] [blame] | 1862 | llvm::Constant *C = BuildIvarLayoutBitmap(BitMap); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1863 | if (CGM.getLangOpts().ObjCGCBitmapPrint) { |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1864 | printf("\n block variable layout for block: "); |
Roman Divacky | e637711 | 2012-09-06 15:59:27 +0000 | [diff] [blame] | 1865 | const unsigned char *s = (const unsigned char*)BitMap.c_str(); |
Bill Wendling | 5313685 | 2012-02-07 09:06:01 +0000 | [diff] [blame] | 1866 | for (unsigned i = 0, e = BitMap.size(); i < e; i++) |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1867 | if (!(s[i] & 0xf0)) |
| 1868 | printf("0x0%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 1869 | else |
| 1870 | printf("0x%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 1871 | printf("\n"); |
| 1872 | } |
| 1873 | |
| 1874 | return C; |
Fariborz Jahanian | c05349e | 2010-08-04 16:57:49 +0000 | [diff] [blame] | 1875 | } |
| 1876 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1877 | llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | 89da6ad | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1878 | const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 7050c55 | 2008-09-04 04:33:15 +0000 | [diff] [blame] | 1879 | // 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] | 1880 | // resolved. Investigate. Its also wasteful to look this up over and over. |
Daniel Dunbar | 7050c55 | 2008-09-04 04:33:15 +0000 | [diff] [blame] | 1881 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); |
| 1882 | |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1883 | return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD), |
Douglas Gregor | 020de32 | 2012-01-17 18:36:30 +0000 | [diff] [blame] | 1884 | ObjCTypes.getExternalProtocolPtrTy()); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1885 | } |
| 1886 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1887 | void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) { |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1888 | // FIXME: We shouldn't need this, the protocol decl should contain enough |
| 1889 | // information to tell us whether this was a declaration or a definition. |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1890 | DefinedProtocols.insert(PD->getIdentifier()); |
| 1891 | |
| 1892 | // If we have generated a forward reference to this protocol, emit |
| 1893 | // it now. Otherwise do nothing, the protocol objects are lazily |
| 1894 | // emitted. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1895 | if (Protocols.count(PD->getIdentifier())) |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1896 | GetOrEmitProtocol(PD); |
| 1897 | } |
| 1898 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1899 | llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1900 | if (DefinedProtocols.count(PD->getIdentifier())) |
| 1901 | return GetOrEmitProtocol(PD); |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 1902 | |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1903 | return GetOrEmitProtocolRef(PD); |
| 1904 | } |
| 1905 | |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1906 | /* |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1907 | // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions |
| 1908 | struct _objc_protocol { |
| 1909 | struct _objc_protocol_extension *isa; |
| 1910 | char *protocol_name; |
| 1911 | struct _objc_protocol_list *protocol_list; |
| 1912 | struct _objc__method_prototype_list *instance_methods; |
| 1913 | struct _objc__method_prototype_list *class_methods |
| 1914 | }; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1915 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1916 | See EmitProtocolExtension(). |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1917 | */ |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1918 | llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) { |
John McCall | f9582a7 | 2012-03-30 21:29:05 +0000 | [diff] [blame] | 1919 | llvm::GlobalVariable *Entry = Protocols[PD->getIdentifier()]; |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1920 | |
| 1921 | // Early exit if a defining object has already been generated. |
| 1922 | if (Entry && Entry->hasInitializer()) |
| 1923 | return Entry; |
| 1924 | |
Douglas Gregor | a715bff | 2012-01-01 19:51:50 +0000 | [diff] [blame] | 1925 | // Use the protocol definition, if there is one. |
| 1926 | if (const ObjCProtocolDecl *Def = PD->getDefinition()) |
| 1927 | PD = Def; |
| 1928 | |
Daniel Dunbar | c61d0e9 | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1929 | // 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] | 1930 | // resolved. Investigate. Its also wasteful to look this up over and over. |
Daniel Dunbar | c61d0e9 | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1931 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); |
| 1932 | |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1933 | // Construct method lists. |
| 1934 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 1935 | std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 1936 | std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1937 | for (ObjCProtocolDecl::instmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1938 | i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) { |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1939 | ObjCMethodDecl *MD = *i; |
| 1940 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 1941 | if (!C) |
| 1942 | return GetOrEmitProtocolRef(PD); |
| 1943 | |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1944 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 1945 | OptInstanceMethods.push_back(C); |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 1946 | OptMethodTypesExt.push_back(GetMethodVarType(MD, true)); |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1947 | } else { |
| 1948 | InstanceMethods.push_back(C); |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 1949 | MethodTypesExt.push_back(GetMethodVarType(MD, true)); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1950 | } |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1951 | } |
| 1952 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1953 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1954 | i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) { |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1955 | ObjCMethodDecl *MD = *i; |
| 1956 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 1957 | if (!C) |
| 1958 | return GetOrEmitProtocolRef(PD); |
| 1959 | |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1960 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 1961 | OptClassMethods.push_back(C); |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 1962 | OptMethodTypesExt.push_back(GetMethodVarType(MD, true)); |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1963 | } else { |
| 1964 | ClassMethods.push_back(C); |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 1965 | MethodTypesExt.push_back(GetMethodVarType(MD, true)); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1966 | } |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1967 | } |
| 1968 | |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 1969 | MethodTypesExt.insert(MethodTypesExt.end(), |
| 1970 | OptMethodTypesExt.begin(), OptMethodTypesExt.end()); |
| 1971 | |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 1972 | llvm::Constant *Values[] = { |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 1973 | EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods, |
| 1974 | MethodTypesExt), |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 1975 | GetClassName(PD->getIdentifier()), |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1976 | EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(), |
Daniel Dunbar | dec75f8 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1977 | PD->protocol_begin(), |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 1978 | PD->protocol_end()), |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1979 | EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(), |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1980 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 1981 | InstanceMethods), |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1982 | EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(), |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1983 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 1984 | ClassMethods) |
| 1985 | }; |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 1986 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1987 | Values); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1988 | |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1989 | if (Entry) { |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1990 | // Already created, fix the linkage and update the initializer. |
| 1991 | Entry->setLinkage(llvm::GlobalValue::InternalLinkage); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1992 | Entry->setInitializer(Init); |
| 1993 | } else { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1994 | Entry = |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 1995 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false, |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1996 | llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1997 | Init, |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1998 | "\01L_OBJC_PROTOCOL_" + PD->getName()); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1999 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2000 | // FIXME: Is this necessary? Why only for protocol? |
| 2001 | Entry->setAlignment(4); |
John McCall | f9582a7 | 2012-03-30 21:29:05 +0000 | [diff] [blame] | 2002 | |
| 2003 | Protocols[PD->getIdentifier()] = Entry; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2004 | } |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 2005 | CGM.AddUsedGlobal(Entry); |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2006 | |
| 2007 | return Entry; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2008 | } |
| 2009 | |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2010 | llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2011 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 2012 | |
| 2013 | if (!Entry) { |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2014 | // We use the initializer as a marker of whether this is a forward |
| 2015 | // reference or not. At module finalization we add the empty |
| 2016 | // contents for protocols which were referenced but never defined. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2017 | Entry = |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2018 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false, |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2019 | llvm::GlobalValue::ExternalLinkage, |
| 2020 | 0, |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2021 | "\01L_OBJC_PROTOCOL_" + PD->getName()); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2022 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2023 | // FIXME: Is this necessary? Why only for protocol? |
| 2024 | Entry->setAlignment(4); |
| 2025 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2026 | |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2027 | return Entry; |
| 2028 | } |
| 2029 | |
| 2030 | /* |
| 2031 | struct _objc_protocol_extension { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2032 | uint32_t size; |
| 2033 | struct objc_method_description_list *optional_instance_methods; |
| 2034 | struct objc_method_description_list *optional_class_methods; |
| 2035 | struct objc_property_list *instance_properties; |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 2036 | const char ** extendedMethodTypes; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2037 | }; |
| 2038 | */ |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2039 | llvm::Constant * |
| 2040 | CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD, |
Bill Wendling | cb5b5ff | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 2041 | ArrayRef<llvm::Constant*> OptInstanceMethods, |
| 2042 | ArrayRef<llvm::Constant*> OptClassMethods, |
| 2043 | ArrayRef<llvm::Constant*> MethodTypesExt) { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2044 | uint64_t Size = |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2045 | CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy); |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2046 | llvm::Constant *Values[] = { |
| 2047 | llvm::ConstantInt::get(ObjCTypes.IntTy, Size), |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2048 | EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_" |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2049 | + PD->getName(), |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2050 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2051 | OptInstanceMethods), |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2052 | EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(), |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2053 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2054 | OptClassMethods), |
| 2055 | EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(), 0, PD, |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 2056 | ObjCTypes), |
| 2057 | EmitProtocolMethodTypes("\01L_OBJC_PROTOCOL_METHOD_TYPES_" + PD->getName(), |
| 2058 | MethodTypesExt, ObjCTypes) |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2059 | }; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2060 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2061 | // Return null if no extension bits are used. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2062 | if (Values[1]->isNullValue() && Values[2]->isNullValue() && |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 2063 | Values[3]->isNullValue() && Values[4]->isNullValue()) |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2064 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2065 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2066 | llvm::Constant *Init = |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2067 | llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2068 | |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2069 | // No special section, but goes in llvm.used |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2070 | return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(), |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2071 | Init, |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2072 | 0, 0, true); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2073 | } |
| 2074 | |
| 2075 | /* |
| 2076 | struct objc_protocol_list { |
Bill Wendling | a515b58 | 2012-02-09 22:16:49 +0000 | [diff] [blame] | 2077 | struct objc_protocol_list *next; |
| 2078 | long count; |
| 2079 | Protocol *list[]; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2080 | }; |
| 2081 | */ |
Daniel Dunbar | dec75f8 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 2082 | llvm::Constant * |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2083 | CGObjCMac::EmitProtocolList(Twine Name, |
Daniel Dunbar | dec75f8 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 2084 | ObjCProtocolDecl::protocol_iterator begin, |
| 2085 | ObjCProtocolDecl::protocol_iterator end) { |
Bill Wendling | a515b58 | 2012-02-09 22:16:49 +0000 | [diff] [blame] | 2086 | llvm::SmallVector<llvm::Constant*, 16> ProtocolRefs; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2087 | |
Daniel Dunbar | dec75f8 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 2088 | for (; begin != end; ++begin) |
| 2089 | ProtocolRefs.push_back(GetProtocolRef(*begin)); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2090 | |
| 2091 | // Just return null for empty protocol lists |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2092 | if (ProtocolRefs.empty()) |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2093 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2094 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2095 | // This list is null terminated. |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2096 | ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy)); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2097 | |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2098 | llvm::Constant *Values[3]; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2099 | // This field is only used by the runtime. |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2100 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2101 | Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2102 | ProtocolRefs.size() - 1); |
| 2103 | Values[2] = |
| 2104 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy, |
| 2105 | ProtocolRefs.size()), |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2106 | ProtocolRefs); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2107 | |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2108 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2109 | llvm::GlobalVariable *GV = |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2110 | CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Daniel Dunbar | ae33384 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2111 | 4, false); |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2112 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2113 | } |
| 2114 | |
Bill Wendling | cb5b5ff | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 2115 | void CGObjCCommonMac:: |
| 2116 | PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*,16> &PropertySet, |
Bill Wendling | a515b58 | 2012-02-09 22:16:49 +0000 | [diff] [blame] | 2117 | llvm::SmallVectorImpl<llvm::Constant*> &Properties, |
Bill Wendling | cb5b5ff | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 2118 | const Decl *Container, |
| 2119 | const ObjCProtocolDecl *PROTO, |
| 2120 | const ObjCCommonTypesHelper &ObjCTypes) { |
Fariborz Jahanian | 751c1e7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 2121 | for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(), |
| 2122 | E = PROTO->protocol_end(); P != E; ++P) |
| 2123 | PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes); |
| 2124 | for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(), |
| 2125 | E = PROTO->prop_end(); I != E; ++I) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2126 | const ObjCPropertyDecl *PD = *I; |
Fariborz Jahanian | 751c1e7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 2127 | if (!PropertySet.insert(PD->getIdentifier())) |
| 2128 | continue; |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2129 | llvm::Constant *Prop[] = { |
| 2130 | GetPropertyName(PD->getIdentifier()), |
| 2131 | GetPropertyTypeString(PD, Container) |
| 2132 | }; |
Fariborz Jahanian | 751c1e7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 2133 | Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop)); |
| 2134 | } |
| 2135 | } |
| 2136 | |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2137 | /* |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2138 | struct _objc_property { |
Bill Wendling | a515b58 | 2012-02-09 22:16:49 +0000 | [diff] [blame] | 2139 | const char * const name; |
| 2140 | const char * const attributes; |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2141 | }; |
| 2142 | |
| 2143 | struct _objc_property_list { |
Bill Wendling | a515b58 | 2012-02-09 22:16:49 +0000 | [diff] [blame] | 2144 | uint32_t entsize; // sizeof (struct _objc_property) |
| 2145 | uint32_t prop_count; |
| 2146 | struct _objc_property[prop_count]; |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2147 | }; |
| 2148 | */ |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2149 | llvm::Constant *CGObjCCommonMac::EmitPropertyList(Twine Name, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2150 | const Decl *Container, |
| 2151 | const ObjCContainerDecl *OCD, |
| 2152 | const ObjCCommonTypesHelper &ObjCTypes) { |
Bill Wendling | a515b58 | 2012-02-09 22:16:49 +0000 | [diff] [blame] | 2153 | llvm::SmallVector<llvm::Constant*, 16> Properties; |
Fariborz Jahanian | 751c1e7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 2154 | llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2155 | for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(), |
| 2156 | E = OCD->prop_end(); I != E; ++I) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2157 | const ObjCPropertyDecl *PD = *I; |
Fariborz Jahanian | 751c1e7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 2158 | PropertySet.insert(PD->getIdentifier()); |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2159 | llvm::Constant *Prop[] = { |
| 2160 | GetPropertyName(PD->getIdentifier()), |
| 2161 | GetPropertyTypeString(PD, Container) |
| 2162 | }; |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2163 | Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2164 | Prop)); |
| 2165 | } |
Fariborz Jahanian | 7966aff | 2010-06-22 16:33:55 +0000 | [diff] [blame] | 2166 | if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) { |
Ted Kremenek | 0ef508d | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 2167 | for (ObjCInterfaceDecl::all_protocol_iterator |
| 2168 | P = OID->all_referenced_protocol_begin(), |
| 2169 | E = OID->all_referenced_protocol_end(); P != E; ++P) |
Fariborz Jahanian | 7966aff | 2010-06-22 16:33:55 +0000 | [diff] [blame] | 2170 | PushProtocolProperties(PropertySet, Properties, Container, (*P), |
| 2171 | ObjCTypes); |
| 2172 | } |
| 2173 | else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) { |
| 2174 | for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(), |
| 2175 | E = CD->protocol_end(); P != E; ++P) |
| 2176 | PushProtocolProperties(PropertySet, Properties, Container, (*P), |
| 2177 | ObjCTypes); |
| 2178 | } |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2179 | |
| 2180 | // Return null for empty list. |
| 2181 | if (Properties.empty()) |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2182 | return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2183 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2184 | unsigned PropertySize = |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2185 | CGM.getDataLayout().getTypeAllocSize(ObjCTypes.PropertyTy); |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2186 | llvm::Constant *Values[3]; |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2187 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize); |
| 2188 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size()); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2189 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy, |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2190 | Properties.size()); |
Owen Anderson | 47034e1 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 2191 | Values[2] = llvm::ConstantArray::get(AT, Properties); |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2192 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2193 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2194 | llvm::GlobalVariable *GV = |
| 2195 | CreateMetadataVar(Name, Init, |
| 2196 | (ObjCABI == 2) ? "__DATA, __objc_const" : |
Daniel Dunbar | b25452a | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2197 | "__OBJC,__property,regular,no_dead_strip", |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2198 | (ObjCABI == 2) ? 8 : 4, |
Daniel Dunbar | b25452a | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2199 | true); |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2200 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy); |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2201 | } |
| 2202 | |
Bill Wendling | cb5b5ff | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 2203 | llvm::Constant * |
| 2204 | CGObjCCommonMac::EmitProtocolMethodTypes(Twine Name, |
| 2205 | ArrayRef<llvm::Constant*> MethodTypes, |
| 2206 | const ObjCCommonTypesHelper &ObjCTypes) { |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 2207 | // Return null for empty list. |
| 2208 | if (MethodTypes.empty()) |
| 2209 | return llvm::Constant::getNullValue(ObjCTypes.Int8PtrPtrTy); |
| 2210 | |
| 2211 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
| 2212 | MethodTypes.size()); |
| 2213 | llvm::Constant *Init = llvm::ConstantArray::get(AT, MethodTypes); |
| 2214 | |
| 2215 | llvm::GlobalVariable *GV = |
| 2216 | CreateMetadataVar(Name, Init, |
| 2217 | (ObjCABI == 2) ? "__DATA, __objc_const" : 0, |
| 2218 | (ObjCABI == 2) ? 8 : 4, |
| 2219 | true); |
| 2220 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.Int8PtrPtrTy); |
| 2221 | } |
| 2222 | |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2223 | /* |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2224 | struct objc_method_description_list { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2225 | int count; |
| 2226 | struct objc_method_description list[]; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2227 | }; |
| 2228 | */ |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2229 | llvm::Constant * |
| 2230 | CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2231 | llvm::Constant *Desc[] = { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2232 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2233 | ObjCTypes.SelectorPtrTy), |
| 2234 | GetMethodVarType(MD) |
| 2235 | }; |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 2236 | if (!Desc[1]) |
| 2237 | return 0; |
| 2238 | |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2239 | return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy, |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2240 | Desc); |
| 2241 | } |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2242 | |
Bill Wendling | cb5b5ff | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 2243 | llvm::Constant * |
| 2244 | CGObjCMac::EmitMethodDescList(Twine Name, const char *Section, |
| 2245 | ArrayRef<llvm::Constant*> Methods) { |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2246 | // Return null for empty list. |
| 2247 | if (Methods.empty()) |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2248 | return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2249 | |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2250 | llvm::Constant *Values[2]; |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2251 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2252 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy, |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2253 | Methods.size()); |
Owen Anderson | 47034e1 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 2254 | Values[1] = llvm::ConstantArray::get(AT, Methods); |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2255 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2256 | |
Daniel Dunbar | b25452a | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2257 | llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2258 | return llvm::ConstantExpr::getBitCast(GV, |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2259 | ObjCTypes.MethodDescriptionListPtrTy); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2260 | } |
| 2261 | |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2262 | /* |
| 2263 | struct _objc_category { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2264 | char *category_name; |
| 2265 | char *class_name; |
| 2266 | struct _objc_method_list *instance_methods; |
| 2267 | struct _objc_method_list *class_methods; |
| 2268 | struct _objc_protocol_list *protocols; |
| 2269 | uint32_t size; // <rdar://4585769> |
| 2270 | struct _objc_property_list *instance_properties; |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2271 | }; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2272 | */ |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2273 | void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) { |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2274 | unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.CategoryTy); |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2275 | |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 2276 | // FIXME: This is poor design, the OCD should have a pointer to the category |
| 2277 | // decl. Additionally, note that Category can be null for the @implementation |
| 2278 | // w/o an @interface case. Sema should just create one for us as it does for |
| 2279 | // @implementation so everyone else can live life under a clear blue sky. |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2280 | const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2281 | const ObjCCategoryDecl *Category = |
Daniel Dunbar | 28e76ca | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 2282 | Interface->FindCategoryDeclaration(OCD->getIdentifier()); |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2283 | |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 2284 | SmallString<256> ExtName; |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2285 | llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_' |
| 2286 | << OCD->getName(); |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2287 | |
Bill Wendling | a515b58 | 2012-02-09 22:16:49 +0000 | [diff] [blame] | 2288 | llvm::SmallVector<llvm::Constant*, 16> InstanceMethods, ClassMethods; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2289 | for (ObjCCategoryImplDecl::instmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2290 | i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) { |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2291 | // Instance methods should always be defined. |
| 2292 | InstanceMethods.push_back(GetMethodConstant(*i)); |
| 2293 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2294 | for (ObjCCategoryImplDecl::classmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2295 | i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) { |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2296 | // Class methods should always be defined. |
| 2297 | ClassMethods.push_back(GetMethodConstant(*i)); |
| 2298 | } |
| 2299 | |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2300 | llvm::Constant *Values[7]; |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2301 | Values[0] = GetClassName(OCD->getIdentifier()); |
| 2302 | Values[1] = GetClassName(Interface->getIdentifier()); |
Fariborz Jahanian | e55f866 | 2009-04-29 20:40:05 +0000 | [diff] [blame] | 2303 | LazySymbols.insert(Interface->getIdentifier()); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2304 | Values[2] = |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2305 | EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(), |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2306 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2307 | InstanceMethods); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2308 | Values[3] = |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2309 | EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(), |
Daniel Dunbar | b25452a | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2310 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2311 | ClassMethods); |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2312 | if (Category) { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2313 | Values[4] = |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2314 | EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(), |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2315 | Category->protocol_begin(), |
| 2316 | Category->protocol_end()); |
| 2317 | } else { |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2318 | Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2319 | } |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2320 | Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | 28e76ca | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 2321 | |
| 2322 | // If there is no category @interface then there can be no properties. |
| 2323 | if (Category) { |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2324 | Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(), |
Fariborz Jahanian | 066347e | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 2325 | OCD, Category, ObjCTypes); |
Daniel Dunbar | 28e76ca | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 2326 | } else { |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2327 | Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
Daniel Dunbar | 28e76ca | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 2328 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2329 | |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2330 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy, |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2331 | Values); |
| 2332 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2333 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2334 | CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init, |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2335 | "__OBJC,__category,regular,no_dead_strip", |
Daniel Dunbar | ae33384 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2336 | 4, true); |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2337 | DefinedCategories.push_back(GV); |
Fariborz Jahanian | 9adb2e6 | 2010-06-21 22:05:18 +0000 | [diff] [blame] | 2338 | DefinedCategoryNames.insert(ExtName.str()); |
Fariborz Jahanian | c057794 | 2011-04-22 22:02:28 +0000 | [diff] [blame] | 2339 | // method definition entries must be clear for next implementation. |
| 2340 | MethodDefinitions.clear(); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2341 | } |
| 2342 | |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 2343 | enum FragileClassFlags { |
| 2344 | FragileABI_Class_Factory = 0x00001, |
| 2345 | FragileABI_Class_Meta = 0x00002, |
| 2346 | FragileABI_Class_HasCXXStructors = 0x02000, |
| 2347 | FragileABI_Class_Hidden = 0x20000 |
| 2348 | }; |
| 2349 | |
| 2350 | enum NonFragileClassFlags { |
| 2351 | /// Is a meta-class. |
| 2352 | NonFragileABI_Class_Meta = 0x00001, |
| 2353 | |
| 2354 | /// Is a root class. |
| 2355 | NonFragileABI_Class_Root = 0x00002, |
| 2356 | |
| 2357 | /// Has a C++ constructor and destructor. |
| 2358 | NonFragileABI_Class_HasCXXStructors = 0x00004, |
| 2359 | |
| 2360 | /// Has hidden visibility. |
| 2361 | NonFragileABI_Class_Hidden = 0x00010, |
| 2362 | |
| 2363 | /// Has the exception attribute. |
| 2364 | NonFragileABI_Class_Exception = 0x00020, |
| 2365 | |
| 2366 | /// (Obsolete) ARC-specific: this class has a .release_ivars method |
| 2367 | NonFragileABI_Class_HasIvarReleaser = 0x00040, |
| 2368 | |
| 2369 | /// Class implementation was compiled under ARC. |
John McCall | 0d54a17 | 2012-10-17 04:53:31 +0000 | [diff] [blame^] | 2370 | NonFragileABI_Class_CompiledByARC = 0x00080, |
| 2371 | |
| 2372 | /// Class has non-trivial destructors, but zero-initialization is okay. |
| 2373 | NonFragileABI_Class_HasCXXDestructorOnly = 0x00100 |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2374 | }; |
| 2375 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2376 | /* |
| 2377 | struct _objc_class { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2378 | Class isa; |
| 2379 | Class super_class; |
| 2380 | const char *name; |
| 2381 | long version; |
| 2382 | long info; |
| 2383 | long instance_size; |
| 2384 | struct _objc_ivar_list *ivars; |
| 2385 | struct _objc_method_list *methods; |
| 2386 | struct _objc_cache *cache; |
| 2387 | struct _objc_protocol_list *protocols; |
| 2388 | // Objective-C 1.0 extensions (<rdr://4585769>) |
| 2389 | const char *ivar_layout; |
| 2390 | struct _objc_class_ext *ext; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2391 | }; |
| 2392 | |
| 2393 | See EmitClassExtension(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2394 | */ |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2395 | void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) { |
Daniel Dunbar | c61d0e9 | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2396 | DefinedSymbols.insert(ID->getIdentifier()); |
| 2397 | |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 2398 | std::string ClassName = ID->getNameAsString(); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2399 | // FIXME: Gross |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2400 | ObjCInterfaceDecl *Interface = |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2401 | const_cast<ObjCInterfaceDecl*>(ID->getClassInterface()); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2402 | llvm::Constant *Protocols = |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2403 | EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(), |
Ted Kremenek | 0ef508d | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 2404 | Interface->all_referenced_protocol_begin(), |
| 2405 | Interface->all_referenced_protocol_end()); |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 2406 | unsigned Flags = FragileABI_Class_Factory; |
John McCall | 0d54a17 | 2012-10-17 04:53:31 +0000 | [diff] [blame^] | 2407 | if (ID->hasNonZeroConstructors() || ID->hasDestructors()) |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 2408 | Flags |= FragileABI_Class_HasCXXStructors; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2409 | unsigned Size = |
Ken Dyck | c8ae550 | 2011-02-09 01:59:34 +0000 | [diff] [blame] | 2410 | CGM.getContext().getASTObjCImplementationLayout(ID).getSize().getQuantity(); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2411 | |
| 2412 | // FIXME: Set CXX-structors flag. |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 2413 | if (ID->getClassInterface()->getVisibility() == HiddenVisibility) |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 2414 | Flags |= FragileABI_Class_Hidden; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2415 | |
Bill Wendling | a515b58 | 2012-02-09 22:16:49 +0000 | [diff] [blame] | 2416 | llvm::SmallVector<llvm::Constant*, 16> InstanceMethods, ClassMethods; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2417 | for (ObjCImplementationDecl::instmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2418 | i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) { |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2419 | // Instance methods should always be defined. |
| 2420 | InstanceMethods.push_back(GetMethodConstant(*i)); |
| 2421 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2422 | for (ObjCImplementationDecl::classmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2423 | i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) { |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2424 | // Class methods should always be defined. |
| 2425 | ClassMethods.push_back(GetMethodConstant(*i)); |
| 2426 | } |
| 2427 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2428 | for (ObjCImplementationDecl::propimpl_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2429 | i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2430 | ObjCPropertyImplDecl *PID = *i; |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2431 | |
| 2432 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) { |
| 2433 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 2434 | |
| 2435 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) |
| 2436 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 2437 | InstanceMethods.push_back(C); |
| 2438 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) |
| 2439 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 2440 | InstanceMethods.push_back(C); |
| 2441 | } |
| 2442 | } |
| 2443 | |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2444 | llvm::Constant *Values[12]; |
Daniel Dunbar | ccf6183 | 2009-05-03 08:56:52 +0000 | [diff] [blame] | 2445 | Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2446 | if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) { |
Daniel Dunbar | c61d0e9 | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2447 | // Record a reference to the super class. |
| 2448 | LazySymbols.insert(Super->getIdentifier()); |
| 2449 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2450 | Values[ 1] = |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2451 | llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2452 | ObjCTypes.ClassPtrTy); |
| 2453 | } else { |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2454 | Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2455 | } |
| 2456 | Values[ 2] = GetClassName(ID->getIdentifier()); |
| 2457 | // Version is always 0. |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2458 | Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 2459 | Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); |
| 2460 | Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Fariborz Jahanian | b042a59 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2461 | Values[ 6] = EmitIvarList(ID, false); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2462 | Values[ 7] = |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2463 | EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(), |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2464 | "__OBJC,__inst_meth,regular,no_dead_strip", |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2465 | InstanceMethods); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2466 | // cache is always NULL. |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2467 | Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2468 | Values[ 9] = Protocols; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2469 | Values[10] = BuildIvarLayout(ID, true); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2470 | Values[11] = EmitClassExtension(ID); |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2471 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2472 | Values); |
Fariborz Jahanian | eb80c98 | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 2473 | std::string Name("\01L_OBJC_CLASS_"); |
| 2474 | Name += ClassName; |
| 2475 | const char *Section = "__OBJC,__class,regular,no_dead_strip"; |
| 2476 | // Check for a forward reference. |
| 2477 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 2478 | if (GV) { |
| 2479 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 2480 | "Forward metaclass reference has incorrect type."); |
| 2481 | GV->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 2482 | GV->setInitializer(Init); |
| 2483 | GV->setSection(Section); |
| 2484 | GV->setAlignment(4); |
| 2485 | CGM.AddUsedGlobal(GV); |
| 2486 | } |
| 2487 | else |
| 2488 | GV = CreateMetadataVar(Name, Init, Section, 4, true); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2489 | DefinedClasses.push_back(GV); |
Fariborz Jahanian | c057794 | 2011-04-22 22:02:28 +0000 | [diff] [blame] | 2490 | // method definition entries must be clear for next implementation. |
| 2491 | MethodDefinitions.clear(); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2492 | } |
| 2493 | |
| 2494 | llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID, |
| 2495 | llvm::Constant *Protocols, |
Bill Wendling | cb5b5ff | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 2496 | ArrayRef<llvm::Constant*> Methods) { |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 2497 | unsigned Flags = FragileABI_Class_Meta; |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2498 | unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ClassTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2499 | |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 2500 | if (ID->getClassInterface()->getVisibility() == HiddenVisibility) |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 2501 | Flags |= FragileABI_Class_Hidden; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2502 | |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2503 | llvm::Constant *Values[12]; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2504 | // The isa for the metaclass is the root of the hierarchy. |
| 2505 | const ObjCInterfaceDecl *Root = ID->getClassInterface(); |
| 2506 | while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) |
| 2507 | Root = Super; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2508 | Values[ 0] = |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2509 | llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()), |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2510 | ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2511 | // The super class for the metaclass is emitted as the name of the |
| 2512 | // super class. The runtime fixes this up to point to the |
| 2513 | // *metaclass* for the super class. |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2514 | if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2515 | Values[ 1] = |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2516 | llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2517 | ObjCTypes.ClassPtrTy); |
| 2518 | } else { |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2519 | Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2520 | } |
| 2521 | Values[ 2] = GetClassName(ID->getIdentifier()); |
| 2522 | // Version is always 0. |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2523 | Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 2524 | Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); |
| 2525 | Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Fariborz Jahanian | b042a59 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2526 | Values[ 6] = EmitIvarList(ID, true); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2527 | Values[ 7] = |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2528 | EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(), |
Daniel Dunbar | b25452a | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2529 | "__OBJC,__cls_meth,regular,no_dead_strip", |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2530 | Methods); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2531 | // cache is always NULL. |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2532 | Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2533 | Values[ 9] = Protocols; |
| 2534 | // ivar_layout for metaclass is always NULL. |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2535 | Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2536 | // The class extension is always unused for metaclasses. |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2537 | Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy); |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2538 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2539 | Values); |
| 2540 | |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2541 | std::string Name("\01L_OBJC_METACLASS_"); |
Benjamin Kramer | 1bbcbd0 | 2012-07-31 11:45:39 +0000 | [diff] [blame] | 2542 | Name += ID->getName(); |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2543 | |
| 2544 | // Check for a forward reference. |
| 2545 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 2546 | if (GV) { |
| 2547 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 2548 | "Forward metaclass reference has incorrect type."); |
| 2549 | GV->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 2550 | GV->setInitializer(Init); |
| 2551 | } else { |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2552 | GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false, |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2553 | llvm::GlobalValue::InternalLinkage, |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2554 | Init, Name); |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2555 | } |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2556 | GV->setSection("__OBJC,__meta_class,regular,no_dead_strip"); |
Daniel Dunbar | ae33384 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2557 | GV->setAlignment(4); |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 2558 | CGM.AddUsedGlobal(GV); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2559 | |
| 2560 | return GV; |
| 2561 | } |
| 2562 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2563 | llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) { |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2564 | std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString(); |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2565 | |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 2566 | // FIXME: Should we look these up somewhere other than the module. Its a bit |
| 2567 | // silly since we only generate these while processing an implementation, so |
| 2568 | // exactly one pointer would work if know when we entered/exitted an |
| 2569 | // implementation block. |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2570 | |
| 2571 | // Check for an existing forward reference. |
Fariborz Jahanian | 475831b | 2009-01-07 20:11:22 +0000 | [diff] [blame] | 2572 | // Previously, metaclass with internal linkage may have been defined. |
| 2573 | // pass 'true' as 2nd argument so it is returned. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2574 | if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, |
| 2575 | true)) { |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2576 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 2577 | "Forward metaclass reference has incorrect type."); |
| 2578 | return GV; |
| 2579 | } else { |
| 2580 | // Generate as an external reference to keep a consistent |
| 2581 | // module. This will be patched up when we emit the metaclass. |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2582 | return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false, |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2583 | llvm::GlobalValue::ExternalLinkage, |
| 2584 | 0, |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2585 | Name); |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2586 | } |
| 2587 | } |
| 2588 | |
Fariborz Jahanian | eb80c98 | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 2589 | llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) { |
| 2590 | std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString(); |
| 2591 | |
| 2592 | if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, |
| 2593 | true)) { |
| 2594 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 2595 | "Forward class metadata reference has incorrect type."); |
| 2596 | return GV; |
| 2597 | } else { |
| 2598 | return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false, |
| 2599 | llvm::GlobalValue::ExternalLinkage, |
| 2600 | 0, |
| 2601 | Name); |
| 2602 | } |
| 2603 | } |
| 2604 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2605 | /* |
| 2606 | struct objc_class_ext { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2607 | uint32_t size; |
| 2608 | const char *weak_ivar_layout; |
| 2609 | struct _objc_property_list *properties; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2610 | }; |
| 2611 | */ |
| 2612 | llvm::Constant * |
| 2613 | CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2614 | uint64_t Size = |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2615 | CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ClassExtensionTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2616 | |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2617 | llvm::Constant *Values[3]; |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2618 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Fariborz Jahanian | 6df6986 | 2009-04-22 23:00:43 +0000 | [diff] [blame] | 2619 | Values[1] = BuildIvarLayout(ID, false); |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2620 | Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(), |
Fariborz Jahanian | 066347e | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 2621 | ID, ID->getClassInterface(), ObjCTypes); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2622 | |
| 2623 | // Return null if no extension bits are used. |
| 2624 | if (Values[1]->isNullValue() && Values[2]->isNullValue()) |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2625 | return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2626 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2627 | llvm::Constant *Init = |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2628 | llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values); |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2629 | return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(), |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2630 | Init, "__OBJC,__class_ext,regular,no_dead_strip", |
Daniel Dunbar | b25452a | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2631 | 4, true); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2632 | } |
| 2633 | |
| 2634 | /* |
| 2635 | struct objc_ivar { |
Bill Wendling | f1a3fca | 2012-02-22 09:30:11 +0000 | [diff] [blame] | 2636 | char *ivar_name; |
| 2637 | char *ivar_type; |
| 2638 | int ivar_offset; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2639 | }; |
| 2640 | |
| 2641 | struct objc_ivar_list { |
Bill Wendling | f1a3fca | 2012-02-22 09:30:11 +0000 | [diff] [blame] | 2642 | int ivar_count; |
| 2643 | struct objc_ivar list[count]; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2644 | }; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2645 | */ |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2646 | llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID, |
Fariborz Jahanian | b042a59 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2647 | bool ForClass) { |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2648 | std::vector<llvm::Constant*> Ivars; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2649 | |
| 2650 | // When emitting the root class GCC emits ivar entries for the |
| 2651 | // actual class structure. It is not clear if we need to follow this |
| 2652 | // behavior; for now lets try and get away with not doing it. If so, |
| 2653 | // the cleanest solution would be to make up an ObjCInterfaceDecl |
| 2654 | // for the class. |
| 2655 | if (ForClass) |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2656 | return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2657 | |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 2658 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2659 | |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 2660 | for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin(); |
Fariborz Jahanian | b26d578 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 2661 | IVD; IVD = IVD->getNextIvar()) { |
Fariborz Jahanian | 7c80959 | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 2662 | // Ignore unnamed bit-fields. |
| 2663 | if (!IVD->getDeclName()) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2664 | continue; |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2665 | llvm::Constant *Ivar[] = { |
| 2666 | GetMethodVarName(IVD->getIdentifier()), |
| 2667 | GetMethodVarType(IVD), |
| 2668 | llvm::ConstantInt::get(ObjCTypes.IntTy, |
| 2669 | ComputeIvarBaseOffset(CGM, OID, IVD)) |
| 2670 | }; |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2671 | Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar)); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2672 | } |
| 2673 | |
| 2674 | // Return null for empty list. |
| 2675 | if (Ivars.empty()) |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2676 | return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2677 | |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2678 | llvm::Constant *Values[2]; |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2679 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2680 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy, |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2681 | Ivars.size()); |
Owen Anderson | 47034e1 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 2682 | Values[1] = llvm::ConstantArray::get(AT, Ivars); |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2683 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2684 | |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2685 | llvm::GlobalVariable *GV; |
| 2686 | if (ForClass) |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2687 | GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(), |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2688 | Init, "__OBJC,__class_vars,regular,no_dead_strip", |
Daniel Dunbar | ae33384 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2689 | 4, true); |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2690 | else |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2691 | GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(), |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2692 | Init, "__OBJC,__instance_vars,regular,no_dead_strip", |
Daniel Dunbar | b25452a | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2693 | 4, true); |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2694 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2695 | } |
| 2696 | |
| 2697 | /* |
| 2698 | struct objc_method { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2699 | SEL method_name; |
| 2700 | char *method_types; |
| 2701 | void *method; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2702 | }; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2703 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2704 | struct objc_method_list { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2705 | struct objc_method_list *obsolete; |
| 2706 | int count; |
| 2707 | struct objc_method methods_list[count]; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2708 | }; |
| 2709 | */ |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2710 | |
| 2711 | /// GetMethodConstant - Return a struct objc_method constant for the |
| 2712 | /// given method if it has been defined. The result is null if the |
| 2713 | /// method has not been defined. The return value has type MethodPtrTy. |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2714 | llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) { |
Argyrios Kyrtzidis | 13257c5 | 2010-08-09 10:54:20 +0000 | [diff] [blame] | 2715 | llvm::Function *Fn = GetMethodDefinition(MD); |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2716 | if (!Fn) |
| 2717 | return 0; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2718 | |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2719 | llvm::Constant *Method[] = { |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2720 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2721 | ObjCTypes.SelectorPtrTy), |
| 2722 | GetMethodVarType(MD), |
| 2723 | llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy) |
| 2724 | }; |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2725 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method); |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2726 | } |
| 2727 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2728 | llvm::Constant *CGObjCMac::EmitMethodList(Twine Name, |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2729 | const char *Section, |
Bill Wendling | cb5b5ff | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 2730 | ArrayRef<llvm::Constant*> Methods) { |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2731 | // Return null for empty list. |
| 2732 | if (Methods.empty()) |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2733 | return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2734 | |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2735 | llvm::Constant *Values[3]; |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2736 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2737 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2738 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy, |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2739 | Methods.size()); |
Owen Anderson | 47034e1 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 2740 | Values[2] = llvm::ConstantArray::get(AT, Methods); |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2741 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2742 | |
Daniel Dunbar | b25452a | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2743 | llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true); |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2744 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListPtrTy); |
Daniel Dunbar | a94ecd2 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2745 | } |
| 2746 | |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 2747 | llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2748 | const ObjCContainerDecl *CD) { |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 2749 | SmallString<256> Name; |
Fariborz Jahanian | 0196a1c | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 2750 | GetNameForMethod(OMD, CD, Name); |
Daniel Dunbar | a94ecd2 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2751 | |
Daniel Dunbar | bf8c24a | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 2752 | CodeGenTypes &Types = CGM.getTypes(); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2753 | llvm::FunctionType *MethodTy = |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2754 | Types.GetFunctionType(Types.arrangeObjCMethodDeclaration(OMD)); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2755 | llvm::Function *Method = |
Daniel Dunbar | 7a95ca3 | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 2756 | llvm::Function::Create(MethodTy, |
Daniel Dunbar | a94ecd2 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2757 | llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | d238681 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 2758 | Name.str(), |
Daniel Dunbar | a94ecd2 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2759 | &CGM.getModule()); |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2760 | MethodDefinitions.insert(std::make_pair(OMD, Method)); |
Daniel Dunbar | a94ecd2 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2761 | |
Daniel Dunbar | a94ecd2 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2762 | return Method; |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2763 | } |
| 2764 | |
Daniel Dunbar | 30c6536 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2765 | llvm::GlobalVariable * |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2766 | CGObjCCommonMac::CreateMetadataVar(Twine Name, |
Daniel Dunbar | 30c6536 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2767 | llvm::Constant *Init, |
| 2768 | const char *Section, |
Daniel Dunbar | 463cc8a | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 2769 | unsigned Align, |
| 2770 | bool AddToUsed) { |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2771 | llvm::Type *Ty = Init->getType(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2772 | llvm::GlobalVariable *GV = |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2773 | new llvm::GlobalVariable(CGM.getModule(), Ty, false, |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 2774 | llvm::GlobalValue::InternalLinkage, Init, Name); |
Daniel Dunbar | 30c6536 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2775 | if (Section) |
| 2776 | GV->setSection(Section); |
Daniel Dunbar | 463cc8a | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 2777 | if (Align) |
| 2778 | GV->setAlignment(Align); |
| 2779 | if (AddToUsed) |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 2780 | CGM.AddUsedGlobal(GV); |
Daniel Dunbar | 30c6536 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2781 | return GV; |
| 2782 | } |
| 2783 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2784 | llvm::Function *CGObjCMac::ModuleInitFunction() { |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2785 | // Abuse this interface function as a place to finalize. |
| 2786 | FinishModule(); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2787 | return NULL; |
| 2788 | } |
| 2789 | |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2790 | llvm::Constant *CGObjCMac::GetPropertyGetFunction() { |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2791 | return ObjCTypes.getGetPropertyFn(); |
Daniel Dunbar | a91c3e0 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 2792 | } |
| 2793 | |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2794 | llvm::Constant *CGObjCMac::GetPropertySetFunction() { |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2795 | return ObjCTypes.getSetPropertyFn(); |
Daniel Dunbar | a91c3e0 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 2796 | } |
| 2797 | |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 2798 | llvm::Constant *CGObjCMac::GetOptimizedPropertySetFunction(bool atomic, |
| 2799 | bool copy) { |
| 2800 | return ObjCTypes.getOptimizedSetPropertyFn(atomic, copy); |
| 2801 | } |
| 2802 | |
David Chisnall | 168b80f | 2010-12-26 22:13:16 +0000 | [diff] [blame] | 2803 | llvm::Constant *CGObjCMac::GetGetStructFunction() { |
| 2804 | return ObjCTypes.getCopyStructFn(); |
| 2805 | } |
| 2806 | llvm::Constant *CGObjCMac::GetSetStructFunction() { |
Fariborz Jahanian | 5a8c203 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 2807 | return ObjCTypes.getCopyStructFn(); |
| 2808 | } |
| 2809 | |
Fariborz Jahanian | 1e1b549 | 2012-01-06 18:07:23 +0000 | [diff] [blame] | 2810 | llvm::Constant *CGObjCMac::GetCppAtomicObjectFunction() { |
| 2811 | return ObjCTypes.getCppAtomicObjectFunction(); |
| 2812 | } |
| 2813 | |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2814 | llvm::Constant *CGObjCMac::EnumerationMutationFunction() { |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2815 | return ObjCTypes.getEnumerationMutationFn(); |
Anders Carlsson | 3f35a26 | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 2816 | } |
| 2817 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 2818 | void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) { |
| 2819 | return EmitTryOrSynchronizedStmt(CGF, S); |
| 2820 | } |
| 2821 | |
| 2822 | void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF, |
| 2823 | const ObjCAtSynchronizedStmt &S) { |
| 2824 | return EmitTryOrSynchronizedStmt(CGF, S); |
| 2825 | } |
| 2826 | |
John McCall | 65bea08 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2827 | namespace { |
John McCall | cda666c | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 2828 | struct PerformFragileFinally : EHScopeStack::Cleanup { |
John McCall | 65bea08 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2829 | const Stmt &S; |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2830 | llvm::Value *SyncArgSlot; |
John McCall | 65bea08 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2831 | llvm::Value *CallTryExitVar; |
| 2832 | llvm::Value *ExceptionData; |
| 2833 | ObjCTypesHelper &ObjCTypes; |
| 2834 | PerformFragileFinally(const Stmt *S, |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2835 | llvm::Value *SyncArgSlot, |
John McCall | 65bea08 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2836 | llvm::Value *CallTryExitVar, |
| 2837 | llvm::Value *ExceptionData, |
| 2838 | ObjCTypesHelper *ObjCTypes) |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2839 | : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar), |
John McCall | 65bea08 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2840 | ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {} |
| 2841 | |
John McCall | 30317fd | 2011-07-12 20:27:29 +0000 | [diff] [blame] | 2842 | void Emit(CodeGenFunction &CGF, Flags flags) { |
John McCall | 65bea08 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2843 | // Check whether we need to call objc_exception_try_exit. |
| 2844 | // In optimized code, this branch will always be folded. |
| 2845 | llvm::BasicBlock *FinallyCallExit = |
| 2846 | CGF.createBasicBlock("finally.call_exit"); |
| 2847 | llvm::BasicBlock *FinallyNoCallExit = |
| 2848 | CGF.createBasicBlock("finally.no_call_exit"); |
| 2849 | CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar), |
| 2850 | FinallyCallExit, FinallyNoCallExit); |
| 2851 | |
| 2852 | CGF.EmitBlock(FinallyCallExit); |
| 2853 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData) |
| 2854 | ->setDoesNotThrow(); |
| 2855 | |
| 2856 | CGF.EmitBlock(FinallyNoCallExit); |
| 2857 | |
| 2858 | if (isa<ObjCAtTryStmt>(S)) { |
| 2859 | if (const ObjCAtFinallyStmt* FinallyStmt = |
John McCall | cebe0ca | 2010-08-11 00:16:14 +0000 | [diff] [blame] | 2860 | cast<ObjCAtTryStmt>(S).getFinallyStmt()) { |
| 2861 | // Save the current cleanup destination in case there's |
| 2862 | // control flow inside the finally statement. |
| 2863 | llvm::Value *CurCleanupDest = |
| 2864 | CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot()); |
| 2865 | |
John McCall | 65bea08 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2866 | CGF.EmitStmt(FinallyStmt->getFinallyBody()); |
| 2867 | |
John McCall | cebe0ca | 2010-08-11 00:16:14 +0000 | [diff] [blame] | 2868 | if (CGF.HaveInsertPoint()) { |
| 2869 | CGF.Builder.CreateStore(CurCleanupDest, |
| 2870 | CGF.getNormalCleanupDestSlot()); |
| 2871 | } else { |
| 2872 | // Currently, the end of the cleanup must always exist. |
| 2873 | CGF.EnsureInsertPoint(); |
| 2874 | } |
| 2875 | } |
John McCall | 65bea08 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2876 | } else { |
| 2877 | // Emit objc_sync_exit(expr); as finally's sole statement for |
| 2878 | // @synchronized. |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2879 | llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot); |
John McCall | 65bea08 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2880 | CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg) |
| 2881 | ->setDoesNotThrow(); |
| 2882 | } |
| 2883 | } |
| 2884 | }; |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2885 | |
| 2886 | class FragileHazards { |
| 2887 | CodeGenFunction &CGF; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2888 | SmallVector<llvm::Value*, 20> Locals; |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2889 | llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry; |
| 2890 | |
| 2891 | llvm::InlineAsm *ReadHazard; |
| 2892 | llvm::InlineAsm *WriteHazard; |
| 2893 | |
| 2894 | llvm::FunctionType *GetAsmFnType(); |
| 2895 | |
| 2896 | void collectLocals(); |
| 2897 | void emitReadHazard(CGBuilderTy &Builder); |
| 2898 | |
| 2899 | public: |
| 2900 | FragileHazards(CodeGenFunction &CGF); |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2901 | |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2902 | void emitWriteHazard(); |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2903 | void emitHazardsInNewBlocks(); |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2904 | }; |
| 2905 | } |
| 2906 | |
| 2907 | /// Create the fragile-ABI read and write hazards based on the current |
| 2908 | /// state of the function, which is presumed to be immediately prior |
| 2909 | /// to a @try block. These hazards are used to maintain correct |
| 2910 | /// semantics in the face of optimization and the fragile ABI's |
| 2911 | /// cavalier use of setjmp/longjmp. |
| 2912 | FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) { |
| 2913 | collectLocals(); |
| 2914 | |
| 2915 | if (Locals.empty()) return; |
| 2916 | |
| 2917 | // Collect all the blocks in the function. |
| 2918 | for (llvm::Function::iterator |
| 2919 | I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I) |
| 2920 | BlocksBeforeTry.insert(&*I); |
| 2921 | |
| 2922 | llvm::FunctionType *AsmFnTy = GetAsmFnType(); |
| 2923 | |
| 2924 | // Create a read hazard for the allocas. This inhibits dead-store |
| 2925 | // optimizations and forces the values to memory. This hazard is |
| 2926 | // inserted before any 'throwing' calls in the protected scope to |
| 2927 | // reflect the possibility that the variables might be read from the |
| 2928 | // catch block if the call throws. |
| 2929 | { |
| 2930 | std::string Constraint; |
| 2931 | for (unsigned I = 0, E = Locals.size(); I != E; ++I) { |
| 2932 | if (I) Constraint += ','; |
| 2933 | Constraint += "*m"; |
| 2934 | } |
| 2935 | |
| 2936 | ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false); |
| 2937 | } |
| 2938 | |
| 2939 | // Create a write hazard for the allocas. This inhibits folding |
| 2940 | // loads across the hazard. This hazard is inserted at the |
| 2941 | // beginning of the catch path to reflect the possibility that the |
| 2942 | // variables might have been written within the protected scope. |
| 2943 | { |
| 2944 | std::string Constraint; |
| 2945 | for (unsigned I = 0, E = Locals.size(); I != E; ++I) { |
| 2946 | if (I) Constraint += ','; |
| 2947 | Constraint += "=*m"; |
| 2948 | } |
| 2949 | |
| 2950 | WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false); |
| 2951 | } |
| 2952 | } |
| 2953 | |
| 2954 | /// Emit a write hazard at the current location. |
| 2955 | void FragileHazards::emitWriteHazard() { |
| 2956 | if (Locals.empty()) return; |
| 2957 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 2958 | CGF.Builder.CreateCall(WriteHazard, Locals)->setDoesNotThrow(); |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2959 | } |
| 2960 | |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2961 | void FragileHazards::emitReadHazard(CGBuilderTy &Builder) { |
| 2962 | assert(!Locals.empty()); |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 2963 | Builder.CreateCall(ReadHazard, Locals)->setDoesNotThrow(); |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2964 | } |
| 2965 | |
| 2966 | /// Emit read hazards in all the protected blocks, i.e. all the blocks |
| 2967 | /// which have been inserted since the beginning of the try. |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2968 | void FragileHazards::emitHazardsInNewBlocks() { |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2969 | if (Locals.empty()) return; |
| 2970 | |
| 2971 | CGBuilderTy Builder(CGF.getLLVMContext()); |
| 2972 | |
| 2973 | // Iterate through all blocks, skipping those prior to the try. |
| 2974 | for (llvm::Function::iterator |
| 2975 | FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) { |
| 2976 | llvm::BasicBlock &BB = *FI; |
| 2977 | if (BlocksBeforeTry.count(&BB)) continue; |
| 2978 | |
| 2979 | // Walk through all the calls in the block. |
| 2980 | for (llvm::BasicBlock::iterator |
| 2981 | BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) { |
| 2982 | llvm::Instruction &I = *BI; |
| 2983 | |
| 2984 | // Ignore instructions that aren't non-intrinsic calls. |
| 2985 | // These are the only calls that can possibly call longjmp. |
| 2986 | if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue; |
| 2987 | if (isa<llvm::IntrinsicInst>(I)) |
| 2988 | continue; |
| 2989 | |
| 2990 | // Ignore call sites marked nounwind. This may be questionable, |
| 2991 | // since 'nounwind' doesn't necessarily mean 'does not call longjmp'. |
| 2992 | llvm::CallSite CS(&I); |
| 2993 | if (CS.doesNotThrow()) continue; |
| 2994 | |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2995 | // Insert a read hazard before the call. This will ensure that |
| 2996 | // any writes to the locals are performed before making the |
| 2997 | // call. If the call throws, then this is sufficient to |
| 2998 | // guarantee correctness as long as it doesn't also write to any |
| 2999 | // locals. |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3000 | Builder.SetInsertPoint(&BB, BI); |
| 3001 | emitReadHazard(Builder); |
| 3002 | } |
| 3003 | } |
| 3004 | } |
| 3005 | |
| 3006 | static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) { |
| 3007 | if (V) S.insert(V); |
| 3008 | } |
| 3009 | |
| 3010 | void FragileHazards::collectLocals() { |
| 3011 | // Compute a set of allocas to ignore. |
| 3012 | llvm::DenseSet<llvm::Value*> AllocasToIgnore; |
| 3013 | addIfPresent(AllocasToIgnore, CGF.ReturnValue); |
| 3014 | addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest); |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3015 | |
| 3016 | // Collect all the allocas currently in the function. This is |
| 3017 | // probably way too aggressive. |
| 3018 | llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock(); |
| 3019 | for (llvm::BasicBlock::iterator |
| 3020 | I = Entry.begin(), E = Entry.end(); I != E; ++I) |
| 3021 | if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I)) |
| 3022 | Locals.push_back(&*I); |
| 3023 | } |
| 3024 | |
| 3025 | llvm::FunctionType *FragileHazards::GetAsmFnType() { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3026 | SmallVector<llvm::Type *, 16> tys(Locals.size()); |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 3027 | for (unsigned i = 0, e = Locals.size(); i != e; ++i) |
| 3028 | tys[i] = Locals[i]->getType(); |
| 3029 | return llvm::FunctionType::get(CGF.VoidTy, tys, false); |
John McCall | 65bea08 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 3030 | } |
| 3031 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3032 | /* |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3033 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3034 | Objective-C setjmp-longjmp (sjlj) Exception Handling |
| 3035 | -- |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3036 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3037 | A catch buffer is a setjmp buffer plus: |
| 3038 | - a pointer to the exception that was caught |
| 3039 | - a pointer to the previous exception data buffer |
| 3040 | - two pointers of reserved storage |
| 3041 | Therefore catch buffers form a stack, with a pointer to the top |
| 3042 | of the stack kept in thread-local storage. |
| 3043 | |
| 3044 | objc_exception_try_enter pushes a catch buffer onto the EH stack. |
| 3045 | objc_exception_try_exit pops the given catch buffer, which is |
| 3046 | required to be the top of the EH stack. |
| 3047 | objc_exception_throw pops the top of the EH stack, writes the |
| 3048 | thrown exception into the appropriate field, and longjmps |
| 3049 | to the setjmp buffer. It crashes the process (with a printf |
| 3050 | and an abort()) if there are no catch buffers on the stack. |
| 3051 | objc_exception_extract just reads the exception pointer out of the |
| 3052 | catch buffer. |
| 3053 | |
| 3054 | There's no reason an implementation couldn't use a light-weight |
| 3055 | setjmp here --- something like __builtin_setjmp, but API-compatible |
| 3056 | with the heavyweight setjmp. This will be more important if we ever |
| 3057 | want to implement correct ObjC/C++ exception interactions for the |
| 3058 | fragile ABI. |
| 3059 | |
| 3060 | Note that for this use of setjmp/longjmp to be correct, we may need |
| 3061 | to mark some local variables volatile: if a non-volatile local |
| 3062 | variable is modified between the setjmp and the longjmp, it has |
| 3063 | indeterminate value. For the purposes of LLVM IR, it may be |
| 3064 | sufficient to make loads and stores within the @try (to variables |
| 3065 | declared outside the @try) volatile. This is necessary for |
| 3066 | optimized correctness, but is not currently being done; this is |
| 3067 | being tracked as rdar://problem/8160285 |
| 3068 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3069 | The basic framework for a @try-catch-finally is as follows: |
| 3070 | { |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3071 | objc_exception_data d; |
| 3072 | id _rethrow = null; |
Anders Carlsson | da0e456 | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 3073 | bool _call_try_exit = true; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3074 | |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3075 | objc_exception_try_enter(&d); |
| 3076 | if (!setjmp(d.jmp_buf)) { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3077 | ... try body ... |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3078 | } else { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3079 | // exception path |
| 3080 | id _caught = objc_exception_extract(&d); |
| 3081 | |
| 3082 | // enter new try scope for handlers |
| 3083 | if (!setjmp(d.jmp_buf)) { |
| 3084 | ... match exception and execute catch blocks ... |
| 3085 | |
| 3086 | // fell off end, rethrow. |
| 3087 | _rethrow = _caught; |
| 3088 | ... jump-through-finally to finally_rethrow ... |
| 3089 | } else { |
| 3090 | // exception in catch block |
| 3091 | _rethrow = objc_exception_extract(&d); |
| 3092 | _call_try_exit = false; |
| 3093 | ... jump-through-finally to finally_rethrow ... |
| 3094 | } |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3095 | } |
Daniel Dunbar | 2efd538 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 3096 | ... jump-through-finally to finally_end ... |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3097 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3098 | finally: |
Anders Carlsson | da0e456 | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 3099 | if (_call_try_exit) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3100 | objc_exception_try_exit(&d); |
Anders Carlsson | da0e456 | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 3101 | |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3102 | ... finally block .... |
Daniel Dunbar | 2efd538 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 3103 | ... dispatch to finally destination ... |
| 3104 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3105 | finally_rethrow: |
Daniel Dunbar | 2efd538 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 3106 | objc_exception_throw(_rethrow); |
| 3107 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3108 | finally_end: |
| 3109 | } |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3110 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3111 | This framework differs slightly from the one gcc uses, in that gcc |
| 3112 | uses _rethrow to determine if objc_exception_try_exit should be called |
| 3113 | and if the object should be rethrown. This breaks in the face of |
| 3114 | throwing nil and introduces unnecessary branches. |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3115 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3116 | We specialize this framework for a few particular circumstances: |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3117 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3118 | - If there are no catch blocks, then we avoid emitting the second |
| 3119 | exception handling context. |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3120 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3121 | - If there is a catch-all catch block (i.e. @catch(...) or @catch(id |
| 3122 | e)) we avoid emitting the code to rethrow an uncaught exception. |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3123 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3124 | - FIXME: If there is no @finally block we can do a few more |
| 3125 | simplifications. |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3126 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3127 | Rethrows and Jumps-Through-Finally |
| 3128 | -- |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3129 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3130 | '@throw;' is supported by pushing the currently-caught exception |
| 3131 | onto ObjCEHStack while the @catch blocks are emitted. |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3132 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3133 | Branches through the @finally block are handled with an ordinary |
| 3134 | normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC |
| 3135 | exceptions are not compatible with C++ exceptions, and this is |
| 3136 | hardly the only place where this will go wrong. |
Daniel Dunbar | 2efd538 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 3137 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3138 | @synchronized(expr) { stmt; } is emitted as if it were: |
| 3139 | id synch_value = expr; |
| 3140 | objc_sync_enter(synch_value); |
| 3141 | @try { stmt; } @finally { objc_sync_exit(synch_value); } |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3142 | */ |
| 3143 | |
Fariborz Jahanian | c2ad6dc | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 3144 | void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 3145 | const Stmt &S) { |
| 3146 | bool isTry = isa<ObjCAtTryStmt>(S); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3147 | |
| 3148 | // A destination for the fall-through edges of the catch handlers to |
| 3149 | // jump to. |
| 3150 | CodeGenFunction::JumpDest FinallyEnd = |
| 3151 | CGF.getJumpDestInCurrentScope("finally.end"); |
| 3152 | |
| 3153 | // A destination for the rethrow edge of the catch handlers to jump |
| 3154 | // to. |
| 3155 | CodeGenFunction::JumpDest FinallyRethrow = |
| 3156 | CGF.getJumpDestInCurrentScope("finally.rethrow"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3157 | |
Daniel Dunbar | 94ceb61 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 3158 | // For @synchronized, call objc_sync_enter(sync.expr). The |
| 3159 | // evaluation of the expression must occur before we enter the |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3160 | // @synchronized. We can't avoid a temp here because we need the |
| 3161 | // value to be preserved. If the backend ever does liveness |
| 3162 | // correctly after setjmp, this will be unnecessary. |
| 3163 | llvm::Value *SyncArgSlot = 0; |
Daniel Dunbar | 94ceb61 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 3164 | if (!isTry) { |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3165 | llvm::Value *SyncArg = |
Daniel Dunbar | 94ceb61 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 3166 | CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr()); |
| 3167 | SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3168 | CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg) |
| 3169 | ->setDoesNotThrow(); |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3170 | |
| 3171 | SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg"); |
| 3172 | CGF.Builder.CreateStore(SyncArg, SyncArgSlot); |
Daniel Dunbar | 94ceb61 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 3173 | } |
Daniel Dunbar | 2efd538 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 3174 | |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3175 | // Allocate memory for the setjmp buffer. This needs to be kept |
| 3176 | // live throughout the try and catch blocks. |
| 3177 | llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy, |
| 3178 | "exceptiondata.ptr"); |
| 3179 | |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3180 | // Create the fragile hazards. Note that this will not capture any |
| 3181 | // of the allocas required for exception processing, but will |
| 3182 | // capture the current basic block (which extends all the way to the |
| 3183 | // setjmp call) as "before the @try". |
| 3184 | FragileHazards Hazards(CGF); |
| 3185 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3186 | // Create a flag indicating whether the cleanup needs to call |
| 3187 | // objc_exception_try_exit. This is true except when |
| 3188 | // - no catches match and we're branching through the cleanup |
| 3189 | // just to rethrow the exception, or |
| 3190 | // - a catch matched and we're falling out of the catch handler. |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3191 | // The setjmp-safety rule here is that we should always store to this |
| 3192 | // variable in a place that dominates the branch through the cleanup |
| 3193 | // without passing through any setjmps. |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3194 | llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(), |
Anders Carlsson | da0e456 | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 3195 | "_call_try_exit"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3196 | |
John McCall | 9916e3f | 2010-10-04 23:42:51 +0000 | [diff] [blame] | 3197 | // A slot containing the exception to rethrow. Only needed when we |
| 3198 | // have both a @catch and a @finally. |
| 3199 | llvm::Value *PropagatingExnVar = 0; |
| 3200 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3201 | // Push a normal cleanup to leave the try scope. |
John McCall | cda666c | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 3202 | CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S, |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3203 | SyncArgSlot, |
John McCall | cda666c | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 3204 | CallTryExitVar, |
| 3205 | ExceptionData, |
| 3206 | &ObjCTypes); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3207 | |
| 3208 | // Enter a try block: |
| 3209 | // - Call objc_exception_try_enter to push ExceptionData on top of |
| 3210 | // the EH stack. |
| 3211 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData) |
| 3212 | ->setDoesNotThrow(); |
| 3213 | |
| 3214 | // - Call setjmp on the exception data buffer. |
| 3215 | llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0); |
| 3216 | llvm::Value *GEPIndexes[] = { Zero, Zero, Zero }; |
| 3217 | llvm::Value *SetJmpBuffer = |
Jay Foad | 040dd82 | 2011-07-22 08:16:57 +0000 | [diff] [blame] | 3218 | CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, "setjmp_buffer"); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3219 | llvm::CallInst *SetJmpResult = |
| 3220 | CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result"); |
| 3221 | SetJmpResult->setDoesNotThrow(); |
Bill Wendling | bd26cf9 | 2011-12-19 23:53:28 +0000 | [diff] [blame] | 3222 | SetJmpResult->setCanReturnTwice(); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3223 | |
| 3224 | // If setjmp returned 0, enter the protected block; otherwise, |
| 3225 | // branch to the handler. |
Daniel Dunbar | 75283ff | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 3226 | llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try"); |
| 3227 | llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler"); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3228 | llvm::Value *DidCatch = |
John McCall | cebe0ca | 2010-08-11 00:16:14 +0000 | [diff] [blame] | 3229 | CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception"); |
| 3230 | CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock); |
Anders Carlsson | 4f1c7c3 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3231 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3232 | // Emit the protected block. |
Anders Carlsson | 4f1c7c3 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3233 | CGF.EmitBlock(TryBlock); |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3234 | CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3235 | CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody() |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3236 | : cast<ObjCAtSynchronizedStmt>(S).getSynchBody()); |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3237 | |
| 3238 | CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3239 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3240 | // Emit the exception handler block. |
Daniel Dunbar | 7f08678 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 3241 | CGF.EmitBlock(TryHandler); |
Daniel Dunbar | b22ff59 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 3242 | |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3243 | // Don't optimize loads of the in-scope locals across this point. |
| 3244 | Hazards.emitWriteHazard(); |
| 3245 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3246 | // For a @synchronized (or a @try with no catches), just branch |
| 3247 | // through the cleanup to the rethrow block. |
| 3248 | if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) { |
| 3249 | // Tell the cleanup not to re-pop the exit. |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3250 | CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar); |
Anders Carlsson | bfee7e9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 3251 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3252 | |
| 3253 | // Otherwise, we have to match against the caught exceptions. |
| 3254 | } else { |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3255 | // Retrieve the exception object. We may emit multiple blocks but |
| 3256 | // nothing can cross this so the value is already in SSA form. |
| 3257 | llvm::CallInst *Caught = |
| 3258 | CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(), |
| 3259 | ExceptionData, "caught"); |
| 3260 | Caught->setDoesNotThrow(); |
| 3261 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3262 | // Push the exception to rethrow onto the EH value stack for the |
| 3263 | // benefit of any @throws in the handlers. |
| 3264 | CGF.ObjCEHValueStack.push_back(Caught); |
| 3265 | |
Douglas Gregor | 96c7949 | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 3266 | const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3267 | |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3268 | bool HasFinally = (AtTryStmt->getFinallyStmt() != 0); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3269 | |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3270 | llvm::BasicBlock *CatchBlock = 0; |
| 3271 | llvm::BasicBlock *CatchHandler = 0; |
| 3272 | if (HasFinally) { |
John McCall | 9916e3f | 2010-10-04 23:42:51 +0000 | [diff] [blame] | 3273 | // Save the currently-propagating exception before |
| 3274 | // objc_exception_try_enter clears the exception slot. |
| 3275 | PropagatingExnVar = CGF.CreateTempAlloca(Caught->getType(), |
| 3276 | "propagating_exception"); |
| 3277 | CGF.Builder.CreateStore(Caught, PropagatingExnVar); |
| 3278 | |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3279 | // Enter a new exception try block (in case a @catch block |
| 3280 | // throws an exception). |
| 3281 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData) |
| 3282 | ->setDoesNotThrow(); |
Anders Carlsson | 4f1c7c3 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3283 | |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3284 | llvm::CallInst *SetJmpResult = |
| 3285 | CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, |
| 3286 | "setjmp.result"); |
| 3287 | SetJmpResult->setDoesNotThrow(); |
Bill Wendling | bd26cf9 | 2011-12-19 23:53:28 +0000 | [diff] [blame] | 3288 | SetJmpResult->setCanReturnTwice(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3289 | |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3290 | llvm::Value *Threw = |
| 3291 | CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception"); |
| 3292 | |
| 3293 | CatchBlock = CGF.createBasicBlock("catch"); |
| 3294 | CatchHandler = CGF.createBasicBlock("catch_for_catch"); |
| 3295 | CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock); |
| 3296 | |
| 3297 | CGF.EmitBlock(CatchBlock); |
| 3298 | } |
| 3299 | |
| 3300 | CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3301 | |
Daniel Dunbar | b22ff59 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 3302 | // Handle catch list. As a special case we check if everything is |
| 3303 | // matched and avoid generating code for falling off the end if |
| 3304 | // so. |
| 3305 | bool AllMatched = false; |
Douglas Gregor | 96c7949 | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 3306 | for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) { |
| 3307 | const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I); |
Anders Carlsson | 4f1c7c3 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3308 | |
Douglas Gregor | 46a572b | 2010-04-26 16:46:50 +0000 | [diff] [blame] | 3309 | const VarDecl *CatchParam = CatchStmt->getCatchParamDecl(); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3310 | const ObjCObjectPointerType *OPT = 0; |
Daniel Dunbar | 523208f | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 3311 | |
Anders Carlsson | 4f1c7c3 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3312 | // catch(...) always matches. |
Daniel Dunbar | b22ff59 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 3313 | if (!CatchParam) { |
| 3314 | AllMatched = true; |
| 3315 | } else { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3316 | OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3317 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3318 | // catch(id e) always matches under this ABI, since only |
| 3319 | // ObjC exceptions end up here in the first place. |
Daniel Dunbar | 86919f4 | 2008-09-27 22:21:14 +0000 | [diff] [blame] | 3320 | // FIXME: For the time being we also match id<X>; this should |
| 3321 | // be rejected by Sema instead. |
Eli Friedman | 55179ca | 2009-07-11 00:57:02 +0000 | [diff] [blame] | 3322 | if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType())) |
Daniel Dunbar | b22ff59 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 3323 | AllMatched = true; |
Anders Carlsson | 4f1c7c3 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3324 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3325 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3326 | // 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] | 3327 | if (AllMatched) { |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3328 | CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF); |
| 3329 | |
Anders Carlsson | 9396a89 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 3330 | if (CatchParam) { |
John McCall | 1c9c3fd | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 3331 | CGF.EmitAutoVarDecl(*CatchParam); |
Daniel Dunbar | 5c7e393 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 3332 | assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3333 | |
| 3334 | // These types work out because ConvertType(id) == i8*. |
Steve Naroff | 371b8fb | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 3335 | CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam)); |
Anders Carlsson | 9396a89 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 3336 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3337 | |
Anders Carlsson | 9396a89 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 3338 | CGF.EmitStmt(CatchStmt->getCatchBody()); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3339 | |
| 3340 | // The scope of the catch variable ends right here. |
| 3341 | CatchVarCleanups.ForceCleanup(); |
| 3342 | |
Anders Carlsson | bfee7e9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 3343 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Anders Carlsson | 4f1c7c3 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3344 | break; |
| 3345 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3346 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3347 | assert(OPT && "Unexpected non-object pointer type in @catch"); |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 3348 | const ObjCObjectType *ObjTy = OPT->getObjectType(); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3349 | |
| 3350 | // FIXME: @catch (Class c) ? |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 3351 | ObjCInterfaceDecl *IDecl = ObjTy->getInterface(); |
| 3352 | assert(IDecl && "Catch parameter must have Objective-C type!"); |
Anders Carlsson | 4f1c7c3 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3353 | |
| 3354 | // Check if the @catch block matches the exception object. |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 3355 | llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3356 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3357 | llvm::CallInst *Match = |
Chris Lattner | c6406db | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 3358 | CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(), |
| 3359 | Class, Caught, "match"); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3360 | Match->setDoesNotThrow(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3361 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3362 | llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match"); |
| 3363 | llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3364 | |
| 3365 | CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"), |
Daniel Dunbar | 7f08678 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 3366 | MatchedBlock, NextCatchBlock); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3367 | |
Anders Carlsson | 4f1c7c3 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3368 | // Emit the @catch block. |
| 3369 | CGF.EmitBlock(MatchedBlock); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3370 | |
| 3371 | // Collect any cleanups for the catch variable. The scope lasts until |
| 3372 | // the end of the catch body. |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3373 | CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3374 | |
John McCall | 1c9c3fd | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 3375 | CGF.EmitAutoVarDecl(*CatchParam); |
Daniel Dunbar | 5c7e393 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 3376 | assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3377 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3378 | // Initialize the catch variable. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3379 | llvm::Value *Tmp = |
| 3380 | CGF.Builder.CreateBitCast(Caught, |
Benjamin Kramer | 76399eb | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 3381 | CGF.ConvertType(CatchParam->getType())); |
Steve Naroff | 371b8fb | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 3382 | CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam)); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3383 | |
Anders Carlsson | 9396a89 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 3384 | CGF.EmitStmt(CatchStmt->getCatchBody()); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3385 | |
| 3386 | // We're done with the catch variable. |
| 3387 | CatchVarCleanups.ForceCleanup(); |
| 3388 | |
Anders Carlsson | bfee7e9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 3389 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3390 | |
Anders Carlsson | 4f1c7c3 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3391 | CGF.EmitBlock(NextCatchBlock); |
| 3392 | } |
| 3393 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3394 | CGF.ObjCEHValueStack.pop_back(); |
| 3395 | |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3396 | // If nothing wanted anything to do with the caught exception, |
| 3397 | // kill the extract call. |
| 3398 | if (Caught->use_empty()) |
| 3399 | Caught->eraseFromParent(); |
| 3400 | |
| 3401 | if (!AllMatched) |
| 3402 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 3403 | |
| 3404 | if (HasFinally) { |
| 3405 | // Emit the exception handler for the @catch blocks. |
| 3406 | CGF.EmitBlock(CatchHandler); |
| 3407 | |
| 3408 | // In theory we might now need a write hazard, but actually it's |
| 3409 | // unnecessary because there's no local-accessing code between |
| 3410 | // the try's write hazard and here. |
| 3411 | //Hazards.emitWriteHazard(); |
| 3412 | |
John McCall | 9916e3f | 2010-10-04 23:42:51 +0000 | [diff] [blame] | 3413 | // Extract the new exception and save it to the |
| 3414 | // propagating-exception slot. |
| 3415 | assert(PropagatingExnVar); |
| 3416 | llvm::CallInst *NewCaught = |
| 3417 | CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(), |
| 3418 | ExceptionData, "caught"); |
| 3419 | NewCaught->setDoesNotThrow(); |
| 3420 | CGF.Builder.CreateStore(NewCaught, PropagatingExnVar); |
| 3421 | |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3422 | // Don't pop the catch handler; the throw already did. |
| 3423 | CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar); |
Anders Carlsson | bfee7e9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 3424 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Daniel Dunbar | b22ff59 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 3425 | } |
Anders Carlsson | 4f1c7c3 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3426 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3427 | |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3428 | // Insert read hazards as required in the new blocks. |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3429 | Hazards.emitHazardsInNewBlocks(); |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3430 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3431 | // Pop the cleanup. |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3432 | CGF.Builder.restoreIP(TryFallthroughIP); |
| 3433 | if (CGF.HaveInsertPoint()) |
| 3434 | CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3435 | CGF.PopCleanupBlock(); |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3436 | CGF.EmitBlock(FinallyEnd.getBlock(), true); |
Anders Carlsson | bfee7e9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 3437 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3438 | // Emit the rethrow block. |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3439 | CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP(); |
John McCall | ad5d61e | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 3440 | CGF.EmitBlock(FinallyRethrow.getBlock(), true); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3441 | if (CGF.HaveInsertPoint()) { |
John McCall | 9916e3f | 2010-10-04 23:42:51 +0000 | [diff] [blame] | 3442 | // If we have a propagating-exception variable, check it. |
| 3443 | llvm::Value *PropagatingExn; |
| 3444 | if (PropagatingExnVar) { |
| 3445 | PropagatingExn = CGF.Builder.CreateLoad(PropagatingExnVar); |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3446 | |
John McCall | 9916e3f | 2010-10-04 23:42:51 +0000 | [diff] [blame] | 3447 | // Otherwise, just look in the buffer for the exception to throw. |
| 3448 | } else { |
| 3449 | llvm::CallInst *Caught = |
| 3450 | CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(), |
| 3451 | ExceptionData); |
| 3452 | Caught->setDoesNotThrow(); |
| 3453 | PropagatingExn = Caught; |
| 3454 | } |
| 3455 | |
| 3456 | CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), PropagatingExn) |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3457 | ->setDoesNotThrow(); |
| 3458 | CGF.Builder.CreateUnreachable(); |
Fariborz Jahanian | e2caaaa | 2008-11-21 19:21:53 +0000 | [diff] [blame] | 3459 | } |
Anders Carlsson | 4f1c7c3 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3460 | |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3461 | CGF.Builder.restoreIP(SavedIP); |
Anders Carlsson | 1963b0c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 3462 | } |
| 3463 | |
| 3464 | void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2efd538 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 3465 | const ObjCAtThrowStmt &S) { |
Anders Carlsson | e005aa1 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 3466 | llvm::Value *ExceptionAsObject; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3467 | |
Anders Carlsson | e005aa1 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 3468 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
John McCall | 248512a | 2011-10-01 10:32:24 +0000 | [diff] [blame] | 3469 | llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3470 | ExceptionAsObject = |
Benjamin Kramer | 76399eb | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 3471 | CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy); |
Anders Carlsson | e005aa1 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 3472 | } else { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3473 | assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) && |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3474 | "Unexpected rethrow outside @catch block."); |
Anders Carlsson | bf8a1be | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 3475 | ExceptionAsObject = CGF.ObjCEHValueStack.back(); |
Anders Carlsson | e005aa1 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 3476 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3477 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3478 | CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject) |
| 3479 | ->setDoesNotReturn(); |
Anders Carlsson | 4f1c7c3 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3480 | CGF.Builder.CreateUnreachable(); |
Daniel Dunbar | 5c7e393 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 3481 | |
| 3482 | // Clear the insertion point to indicate we are in unreachable code. |
| 3483 | CGF.Builder.ClearInsertionPoint(); |
Anders Carlsson | 1963b0c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 3484 | } |
| 3485 | |
Fariborz Jahanian | 83f45b55 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 3486 | /// EmitObjCWeakRead - Code gen for loading value of a __weak |
Fariborz Jahanian | f5125d1 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 3487 | /// object: objc_read_weak (id *src) |
| 3488 | /// |
Fariborz Jahanian | 83f45b55 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 3489 | llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3490 | llvm::Value *AddrWeakObj) { |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3491 | llvm::Type* DestTy = |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3492 | cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType(); |
| 3493 | AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, |
| 3494 | ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 3495 | llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(), |
Fariborz Jahanian | 83f45b55 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 3496 | AddrWeakObj, "weakread"); |
Eli Friedman | a374b68 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 3497 | read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy); |
Fariborz Jahanian | f5125d1 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 3498 | return read_weak; |
| 3499 | } |
| 3500 | |
Fariborz Jahanian | 83f45b55 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 3501 | /// EmitObjCWeakAssign - Code gen for assigning to a __weak object. |
| 3502 | /// objc_assign_weak (id src, id *dst) |
| 3503 | /// |
| 3504 | void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3505 | llvm::Value *src, llvm::Value *dst) { |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3506 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3507 | if (!isa<llvm::PointerType>(SrcTy)) { |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3508 | unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3509 | assert(Size <= 8 && "does not support size > 8"); |
| 3510 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3511 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 1b074a3 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 3512 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 3513 | } |
Fariborz Jahanian | 50a1270 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 3514 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 3515 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 6fdd57c | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 3516 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(), |
Fariborz Jahanian | 83f45b55 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 3517 | src, dst, "weakassign"); |
| 3518 | return; |
| 3519 | } |
| 3520 | |
Fariborz Jahanian | d7db964 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 3521 | /// EmitObjCGlobalAssign - Code gen for assigning to a __strong object. |
| 3522 | /// objc_assign_global (id src, id *dst) |
| 3523 | /// |
| 3524 | void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 217af24 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 3525 | llvm::Value *src, llvm::Value *dst, |
| 3526 | bool threadlocal) { |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3527 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3528 | if (!isa<llvm::PointerType>(SrcTy)) { |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3529 | unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3530 | assert(Size <= 8 && "does not support size > 8"); |
| 3531 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3532 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 1b074a3 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 3533 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 3534 | } |
Fariborz Jahanian | 50a1270 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 3535 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 3536 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Fariborz Jahanian | 217af24 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 3537 | if (!threadlocal) |
| 3538 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(), |
| 3539 | src, dst, "globalassign"); |
| 3540 | else |
| 3541 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(), |
| 3542 | src, dst, "threadlocalassign"); |
Fariborz Jahanian | d7db964 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 3543 | return; |
| 3544 | } |
| 3545 | |
Fariborz Jahanian | e881b53 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 3546 | /// EmitObjCIvarAssign - Code gen for assigning to a __strong object. |
Fariborz Jahanian | 7a95d72 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 3547 | /// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset) |
Fariborz Jahanian | e881b53 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 3548 | /// |
| 3549 | void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 7a95d72 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 3550 | llvm::Value *src, llvm::Value *dst, |
| 3551 | llvm::Value *ivarOffset) { |
| 3552 | assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3553 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3554 | if (!isa<llvm::PointerType>(SrcTy)) { |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3555 | unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3556 | assert(Size <= 8 && "does not support size > 8"); |
| 3557 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3558 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 1b074a3 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 3559 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 3560 | } |
Fariborz Jahanian | e881b53 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 3561 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 3562 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Fariborz Jahanian | 7a95d72 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 3563 | CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(), |
| 3564 | src, dst, ivarOffset); |
Fariborz Jahanian | e881b53 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 3565 | return; |
| 3566 | } |
| 3567 | |
Fariborz Jahanian | d7db964 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 3568 | /// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object. |
| 3569 | /// objc_assign_strongCast (id src, id *dst) |
| 3570 | /// |
| 3571 | void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3572 | llvm::Value *src, llvm::Value *dst) { |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3573 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3574 | if (!isa<llvm::PointerType>(SrcTy)) { |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3575 | unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3576 | assert(Size <= 8 && "does not support size > 8"); |
| 3577 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3578 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 1b074a3 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 3579 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 3580 | } |
Fariborz Jahanian | 50a1270 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 3581 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 3582 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 0a696a42 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 3583 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(), |
Fariborz Jahanian | d7db964 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 3584 | src, dst, "weakassign"); |
| 3585 | return; |
| 3586 | } |
| 3587 | |
Fariborz Jahanian | 5f21d2f | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 3588 | void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3589 | llvm::Value *DestPtr, |
| 3590 | llvm::Value *SrcPtr, |
Fariborz Jahanian | 021510e | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 3591 | llvm::Value *size) { |
Fariborz Jahanian | 5f21d2f | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 3592 | SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy); |
| 3593 | DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy); |
Fariborz Jahanian | 5f21d2f | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 3594 | CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(), |
Fariborz Jahanian | 021510e | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 3595 | DestPtr, SrcPtr, size); |
Fariborz Jahanian | 5f21d2f | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 3596 | return; |
| 3597 | } |
| 3598 | |
Fariborz Jahanian | 9f84b78 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 3599 | /// EmitObjCValueForIvar - Code Gen for ivar reference. |
| 3600 | /// |
Fariborz Jahanian | 712bfa6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 3601 | LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 3602 | QualType ObjectTy, |
| 3603 | llvm::Value *BaseValue, |
| 3604 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 712bfa6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 3605 | unsigned CVRQualifiers) { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3606 | const ObjCInterfaceDecl *ID = |
| 3607 | ObjectTy->getAs<ObjCObjectType>()->getInterface(); |
Daniel Dunbar | 9fd114d | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 3608 | return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers, |
| 3609 | EmitIvarOffset(CGF, ID, Ivar)); |
Fariborz Jahanian | 9f84b78 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 3610 | } |
| 3611 | |
Fariborz Jahanian | 21fc74c | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 3612 | llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 722f424 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 3613 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | 21fc74c | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 3614 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | 9fd114d | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 3615 | uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar); |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 3616 | return llvm::ConstantInt::get( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3617 | CGM.getTypes().ConvertType(CGM.getContext().LongTy), |
| 3618 | Offset); |
Fariborz Jahanian | 21fc74c | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 3619 | } |
| 3620 | |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3621 | /* *** Private Interface *** */ |
| 3622 | |
| 3623 | /// EmitImageInfo - Emit the image info marker used to encode some module |
| 3624 | /// level information. |
| 3625 | /// |
| 3626 | /// See: <rdr://4810609&4810587&4810587> |
| 3627 | /// struct IMAGE_INFO { |
| 3628 | /// unsigned version; |
| 3629 | /// unsigned flags; |
| 3630 | /// }; |
| 3631 | enum ImageInfoFlags { |
Daniel Dunbar | 5e63927 | 2010-04-25 20:39:01 +0000 | [diff] [blame] | 3632 | eImageInfo_FixAndContinue = (1 << 0), |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3633 | eImageInfo_GarbageCollected = (1 << 1), |
| 3634 | eImageInfo_GCOnly = (1 << 2), |
Daniel Dunbar | 75e909f | 2009-04-20 07:11:47 +0000 | [diff] [blame] | 3635 | eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set. |
| 3636 | |
Daniel Dunbar | 5e63927 | 2010-04-25 20:39:01 +0000 | [diff] [blame] | 3637 | // A flag indicating that the module has no instances of a @synthesize of a |
| 3638 | // superclass variable. <rdar://problem/6803242> |
Bill Wendling | 1e60a2c | 2012-04-24 11:04:57 +0000 | [diff] [blame] | 3639 | eImageInfo_CorrectedSynthesize = (1 << 4), |
| 3640 | eImageInfo_ImageIsSimulated = (1 << 5) |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3641 | }; |
| 3642 | |
Daniel Dunbar | 5e63927 | 2010-04-25 20:39:01 +0000 | [diff] [blame] | 3643 | void CGObjCCommonMac::EmitImageInfo() { |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3644 | unsigned version = 0; // Version is unused? |
Bill Wendling | b6f795e | 2012-02-16 01:13:30 +0000 | [diff] [blame] | 3645 | const char *Section = (ObjCABI == 1) ? |
| 3646 | "__OBJC, __image_info,regular" : |
| 3647 | "__DATA, __objc_imageinfo, regular, no_dead_strip"; |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3648 | |
Bill Wendling | b6f795e | 2012-02-16 01:13:30 +0000 | [diff] [blame] | 3649 | // Generate module-level named metadata to convey this information to the |
| 3650 | // linker and code-gen. |
| 3651 | llvm::Module &Mod = CGM.getModule(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3652 | |
Bill Wendling | b6f795e | 2012-02-16 01:13:30 +0000 | [diff] [blame] | 3653 | // Add the ObjC ABI version to the module flags. |
| 3654 | Mod.addModuleFlag(llvm::Module::Error, "Objective-C Version", ObjCABI); |
| 3655 | Mod.addModuleFlag(llvm::Module::Error, "Objective-C Image Info Version", |
| 3656 | version); |
| 3657 | Mod.addModuleFlag(llvm::Module::Error, "Objective-C Image Info Section", |
| 3658 | llvm::MDString::get(VMContext,Section)); |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3659 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3660 | if (CGM.getLangOpts().getGC() == LangOptions::NonGC) { |
Bill Wendling | b6f795e | 2012-02-16 01:13:30 +0000 | [diff] [blame] | 3661 | // Non-GC overrides those files which specify GC. |
| 3662 | Mod.addModuleFlag(llvm::Module::Override, |
| 3663 | "Objective-C Garbage Collection", (uint32_t)0); |
| 3664 | } else { |
| 3665 | // Add the ObjC garbage collection value. |
| 3666 | Mod.addModuleFlag(llvm::Module::Error, |
| 3667 | "Objective-C Garbage Collection", |
| 3668 | eImageInfo_GarbageCollected); |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3669 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3670 | if (CGM.getLangOpts().getGC() == LangOptions::GCOnly) { |
Bill Wendling | b6f795e | 2012-02-16 01:13:30 +0000 | [diff] [blame] | 3671 | // Add the ObjC GC Only value. |
| 3672 | Mod.addModuleFlag(llvm::Module::Error, "Objective-C GC Only", |
| 3673 | eImageInfo_GCOnly); |
| 3674 | |
| 3675 | // Require that GC be specified and set to eImageInfo_GarbageCollected. |
| 3676 | llvm::Value *Ops[2] = { |
| 3677 | llvm::MDString::get(VMContext, "Objective-C Garbage Collection"), |
| 3678 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), |
| 3679 | eImageInfo_GarbageCollected) |
| 3680 | }; |
| 3681 | Mod.addModuleFlag(llvm::Module::Require, "Objective-C GC Only", |
| 3682 | llvm::MDNode::get(VMContext, Ops)); |
| 3683 | } |
| 3684 | } |
Bill Wendling | 1e60a2c | 2012-04-24 11:04:57 +0000 | [diff] [blame] | 3685 | |
| 3686 | // Indicate whether we're compiling this to run on a simulator. |
| 3687 | const llvm::Triple &Triple = CGM.getTarget().getTriple(); |
| 3688 | if (Triple.getOS() == llvm::Triple::IOS && |
| 3689 | (Triple.getArch() == llvm::Triple::x86 || |
| 3690 | Triple.getArch() == llvm::Triple::x86_64)) |
| 3691 | Mod.addModuleFlag(llvm::Module::Error, "Objective-C Is Simulated", |
| 3692 | eImageInfo_ImageIsSimulated); |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3693 | } |
| 3694 | |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3695 | // struct objc_module { |
| 3696 | // unsigned long version; |
| 3697 | // unsigned long size; |
| 3698 | // const char *name; |
| 3699 | // Symtab symtab; |
| 3700 | // }; |
| 3701 | |
| 3702 | // FIXME: Get from somewhere |
| 3703 | static const int ModuleVersion = 7; |
| 3704 | |
| 3705 | void CGObjCMac::EmitModuleInfo() { |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3706 | uint64_t Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ModuleTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3707 | |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 3708 | llvm::Constant *Values[] = { |
| 3709 | llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion), |
| 3710 | llvm::ConstantInt::get(ObjCTypes.LongTy, Size), |
| 3711 | // This used to be the filename, now it is unused. <rdr://4327263> |
| 3712 | GetClassName(&CGM.getContext().Idents.get("")), |
| 3713 | EmitModuleSymbols() |
| 3714 | }; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3715 | CreateMetadataVar("\01L_OBJC_MODULES", |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 3716 | llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values), |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3717 | "__OBJC,__module_info,regular,no_dead_strip", |
Daniel Dunbar | ae33384 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 3718 | 4, true); |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3719 | } |
| 3720 | |
| 3721 | llvm::Constant *CGObjCMac::EmitModuleSymbols() { |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3722 | unsigned NumClasses = DefinedClasses.size(); |
| 3723 | unsigned NumCategories = DefinedCategories.size(); |
| 3724 | |
Daniel Dunbar | c61d0e9 | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3725 | // Return null if no symbols were defined. |
| 3726 | if (!NumClasses && !NumCategories) |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3727 | return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy); |
Daniel Dunbar | c61d0e9 | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3728 | |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 3729 | llvm::Constant *Values[5]; |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 3730 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3731 | Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy); |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 3732 | Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses); |
| 3733 | Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3734 | |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3735 | // The runtime expects exactly the list of defined classes followed |
| 3736 | // by the list of defined categories, in a single array. |
Chris Lattner | 3def9ae | 2012-02-06 22:16:34 +0000 | [diff] [blame] | 3737 | SmallVector<llvm::Constant*, 8> Symbols(NumClasses + NumCategories); |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3738 | for (unsigned i=0; i<NumClasses; i++) |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 3739 | Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i], |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3740 | ObjCTypes.Int8PtrTy); |
| 3741 | for (unsigned i=0; i<NumCategories; i++) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3742 | Symbols[NumClasses + i] = |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 3743 | llvm::ConstantExpr::getBitCast(DefinedCategories[i], |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3744 | ObjCTypes.Int8PtrTy); |
| 3745 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3746 | Values[4] = |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3747 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
Chris Lattner | 3def9ae | 2012-02-06 22:16:34 +0000 | [diff] [blame] | 3748 | Symbols.size()), |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3749 | Symbols); |
| 3750 | |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 3751 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3752 | |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3753 | llvm::GlobalVariable *GV = |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3754 | CreateMetadataVar("\01L_OBJC_SYMBOLS", Init, |
| 3755 | "__OBJC,__symbols,regular,no_dead_strip", |
Daniel Dunbar | b25452a | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 3756 | 4, true); |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 3757 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3758 | } |
| 3759 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3760 | llvm::Value *CGObjCMac::EmitClassRefFromId(CGBuilderTy &Builder, |
| 3761 | IdentifierInfo *II) { |
| 3762 | LazySymbols.insert(II); |
| 3763 | |
| 3764 | llvm::GlobalVariable *&Entry = ClassReferences[II]; |
| 3765 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3766 | if (!Entry) { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3767 | llvm::Constant *Casted = |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3768 | llvm::ConstantExpr::getBitCast(GetClassName(II), |
| 3769 | ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3770 | Entry = |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3771 | CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted, |
| 3772 | "__OBJC,__cls_refs,literal_pointers,no_dead_strip", |
| 3773 | 4, true); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3774 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3775 | |
Benjamin Kramer | 76399eb | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 3776 | return Builder.CreateLoad(Entry); |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3777 | } |
| 3778 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3779 | llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder, |
| 3780 | const ObjCInterfaceDecl *ID) { |
| 3781 | return EmitClassRefFromId(Builder, ID->getIdentifier()); |
| 3782 | } |
| 3783 | |
| 3784 | llvm::Value *CGObjCMac::EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder) { |
| 3785 | IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool"); |
| 3786 | return EmitClassRefFromId(Builder, II); |
| 3787 | } |
| 3788 | |
Fariborz Jahanian | 9240f3d | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 3789 | llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel, |
| 3790 | bool lvalue) { |
Daniel Dunbar | cb515c8 | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3791 | llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3792 | |
Daniel Dunbar | cb515c8 | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3793 | if (!Entry) { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3794 | llvm::Constant *Casted = |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 3795 | llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), |
Daniel Dunbar | cb515c8 | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3796 | ObjCTypes.SelectorPtrTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3797 | Entry = |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3798 | CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted, |
| 3799 | "__OBJC,__message_refs,literal_pointers,no_dead_strip", |
Daniel Dunbar | b25452a | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 3800 | 4, true); |
Daniel Dunbar | cb515c8 | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3801 | } |
| 3802 | |
Fariborz Jahanian | 9240f3d | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 3803 | if (lvalue) |
| 3804 | return Entry; |
Benjamin Kramer | 76399eb | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 3805 | return Builder.CreateLoad(Entry); |
Daniel Dunbar | cb515c8 | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3806 | } |
| 3807 | |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3808 | llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) { |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3809 | llvm::GlobalVariable *&Entry = ClassNames[Ident]; |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3810 | |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3811 | if (!Entry) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3812 | Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_", |
Chris Lattner | 9c81833 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 3813 | llvm::ConstantDataArray::getString(VMContext, |
| 3814 | Ident->getNameStart()), |
Daniel Dunbar | 7c9295a | 2011-03-25 20:09:09 +0000 | [diff] [blame] | 3815 | ((ObjCABI == 2) ? |
| 3816 | "__TEXT,__objc_classname,cstring_literals" : |
| 3817 | "__TEXT,__cstring,cstring_literals"), |
Daniel Dunbar | 3241fae | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3818 | 1, true); |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3819 | |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3820 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3821 | } |
| 3822 | |
Argyrios Kyrtzidis | 13257c5 | 2010-08-09 10:54:20 +0000 | [diff] [blame] | 3823 | llvm::Function *CGObjCCommonMac::GetMethodDefinition(const ObjCMethodDecl *MD) { |
| 3824 | llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*>::iterator |
| 3825 | I = MethodDefinitions.find(MD); |
| 3826 | if (I != MethodDefinitions.end()) |
| 3827 | return I->second; |
| 3828 | |
Argyrios Kyrtzidis | 13257c5 | 2010-08-09 10:54:20 +0000 | [diff] [blame] | 3829 | return NULL; |
| 3830 | } |
| 3831 | |
Fariborz Jahanian | 01dff42 | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 3832 | /// GetIvarLayoutName - Returns a unique constant for the given |
| 3833 | /// ivar layout bitmap. |
| 3834 | llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3835 | const ObjCCommonTypesHelper &ObjCTypes) { |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3836 | return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Fariborz Jahanian | 01dff42 | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 3837 | } |
| 3838 | |
Daniel Dunbar | 15bd888 | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3839 | void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3840 | unsigned int BytePos, |
Daniel Dunbar | 15bd888 | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3841 | bool ForStrongLayout, |
| 3842 | bool &HasUnion) { |
| 3843 | const RecordDecl *RD = RT->getDecl(); |
| 3844 | // FIXME - Use iterator. |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 3845 | SmallVector<const FieldDecl*, 16> Fields; |
| 3846 | for (RecordDecl::field_iterator i = RD->field_begin(), |
| 3847 | e = RD->field_end(); i != e; ++i) |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 3848 | Fields.push_back(*i); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3849 | llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0)); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3850 | const llvm::StructLayout *RecLayout = |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3851 | CGM.getDataLayout().getStructLayout(cast<llvm::StructType>(Ty)); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3852 | |
Daniel Dunbar | 15bd888 | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3853 | BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos, |
| 3854 | ForStrongLayout, HasUnion); |
| 3855 | } |
| 3856 | |
Daniel Dunbar | 36e2a1e | 2009-05-03 21:05:10 +0000 | [diff] [blame] | 3857 | void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3858 | const llvm::StructLayout *Layout, |
| 3859 | const RecordDecl *RD, |
Bill Wendling | f1a3fca | 2012-02-22 09:30:11 +0000 | [diff] [blame] | 3860 | ArrayRef<const FieldDecl*> RecFields, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3861 | unsigned int BytePos, bool ForStrongLayout, |
| 3862 | bool &HasUnion) { |
Fariborz Jahanian | 3b0f886 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3863 | bool IsUnion = (RD && RD->isUnion()); |
| 3864 | uint64_t MaxUnionIvarSize = 0; |
| 3865 | uint64_t MaxSkippedUnionIvarSize = 0; |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 3866 | const FieldDecl *MaxField = 0; |
| 3867 | const FieldDecl *MaxSkippedField = 0; |
| 3868 | const FieldDecl *LastFieldBitfieldOrUnnamed = 0; |
Daniel Dunbar | 5b74391 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3869 | uint64_t MaxFieldOffset = 0; |
| 3870 | uint64_t MaxSkippedFieldOffset = 0; |
Argyrios Kyrtzidis | 2fdb5b5 | 2010-09-06 12:00:10 +0000 | [diff] [blame] | 3871 | uint64_t LastBitfieldOrUnnamedOffset = 0; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3872 | uint64_t FirstFieldDelta = 0; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3873 | |
Fariborz Jahanian | 524bb20 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3874 | if (RecFields.empty()) |
| 3875 | return; |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3876 | unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0); |
| 3877 | unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3878 | if (!RD && CGM.getLangOpts().ObjCAutoRefCount) { |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 3879 | const FieldDecl *FirstField = RecFields[0]; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3880 | FirstFieldDelta = |
| 3881 | ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(FirstField)); |
| 3882 | } |
| 3883 | |
Chris Lattner | 5b36ddb | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3884 | for (unsigned i = 0, e = RecFields.size(); i != e; ++i) { |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 3885 | const FieldDecl *Field = RecFields[i]; |
Daniel Dunbar | 62b1070 | 2009-05-03 23:35:23 +0000 | [diff] [blame] | 3886 | uint64_t FieldOffset; |
Anders Carlsson | e2c6baf | 2009-07-24 17:23:54 +0000 | [diff] [blame] | 3887 | if (RD) { |
Daniel Dunbar | d51c1a6 | 2010-04-14 17:02:21 +0000 | [diff] [blame] | 3888 | // Note that 'i' here is actually the field index inside RD of Field, |
| 3889 | // although this dependency is hidden. |
| 3890 | const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3891 | FieldOffset = (RL.getFieldOffset(i) / ByteSizeInBits) - FirstFieldDelta; |
Anders Carlsson | e2c6baf | 2009-07-24 17:23:54 +0000 | [diff] [blame] | 3892 | } else |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3893 | FieldOffset = |
| 3894 | ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field)) - FirstFieldDelta; |
Daniel Dunbar | 94f46dc | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3895 | |
Fariborz Jahanian | 524bb20 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3896 | // Skip over unnamed or bitfields |
Fariborz Jahanian | f5fec02 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3897 | if (!Field->getIdentifier() || Field->isBitField()) { |
Argyrios Kyrtzidis | 2fdb5b5 | 2010-09-06 12:00:10 +0000 | [diff] [blame] | 3898 | LastFieldBitfieldOrUnnamed = Field; |
| 3899 | LastBitfieldOrUnnamedOffset = FieldOffset; |
Fariborz Jahanian | 524bb20 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3900 | continue; |
Fariborz Jahanian | f5fec02 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3901 | } |
Daniel Dunbar | 94f46dc | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3902 | |
Argyrios Kyrtzidis | 2fdb5b5 | 2010-09-06 12:00:10 +0000 | [diff] [blame] | 3903 | LastFieldBitfieldOrUnnamed = 0; |
Fariborz Jahanian | 524bb20 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3904 | QualType FQT = Field->getType(); |
Fariborz Jahanian | f909f92 | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 3905 | if (FQT->isRecordType() || FQT->isUnionType()) { |
Fariborz Jahanian | 524bb20 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3906 | if (FQT->isUnionType()) |
| 3907 | HasUnion = true; |
Fariborz Jahanian | 3b0f886 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3908 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3909 | BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(), |
Daniel Dunbar | 94f46dc | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3910 | BytePos + FieldOffset, |
Daniel Dunbar | 15bd888 | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3911 | ForStrongLayout, HasUnion); |
Fariborz Jahanian | 524bb20 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3912 | continue; |
| 3913 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3914 | |
Chris Lattner | 5b36ddb | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3915 | if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3916 | const ConstantArrayType *CArray = |
Daniel Dunbar | 7abf83c | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3917 | dyn_cast_or_null<ConstantArrayType>(Array); |
Fariborz Jahanian | f5fec02 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3918 | uint64_t ElCount = CArray->getSize().getZExtValue(); |
Daniel Dunbar | 7abf83c | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3919 | assert(CArray && "only array with known element size is supported"); |
Fariborz Jahanian | 3b0f886 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3920 | FQT = CArray->getElementType(); |
Fariborz Jahanian | f909f92 | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 3921 | while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { |
| 3922 | const ConstantArrayType *CArray = |
Daniel Dunbar | 7abf83c | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3923 | dyn_cast_or_null<ConstantArrayType>(Array); |
Fariborz Jahanian | f5fec02 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3924 | ElCount *= CArray->getSize().getZExtValue(); |
Fariborz Jahanian | f909f92 | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 3925 | FQT = CArray->getElementType(); |
| 3926 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3927 | |
| 3928 | assert(!FQT->isUnionType() && |
Fariborz Jahanian | 3b0f886 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3929 | "layout for array of unions not supported"); |
Fariborz Jahanian | 9a7d57d | 2011-01-03 19:23:18 +0000 | [diff] [blame] | 3930 | if (FQT->isRecordType() && ElCount) { |
Fariborz Jahanian | a123b64 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3931 | int OldIndex = IvarsInfo.size() - 1; |
| 3932 | int OldSkIndex = SkipIvars.size() -1; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3933 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3934 | const RecordType *RT = FQT->getAs<RecordType>(); |
Daniel Dunbar | 94f46dc | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3935 | BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset, |
Daniel Dunbar | 15bd888 | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3936 | ForStrongLayout, HasUnion); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3937 | |
Fariborz Jahanian | 3b0f886 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3938 | // Replicate layout information for each array element. Note that |
| 3939 | // one element is already done. |
| 3940 | uint64_t ElIx = 1; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3941 | for (int FirstIndex = IvarsInfo.size() - 1, |
| 3942 | FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) { |
Fariborz Jahanian | 1bf7288 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3943 | uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits; |
Daniel Dunbar | 7b89ace | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3944 | for (int i = OldIndex+1; i <= FirstIndex; ++i) |
| 3945 | IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx, |
| 3946 | IvarsInfo[i].ivar_size)); |
| 3947 | for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i) |
| 3948 | SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx, |
| 3949 | SkipIvars[i].ivar_size)); |
Fariborz Jahanian | 3b0f886 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3950 | } |
| 3951 | continue; |
| 3952 | } |
Fariborz Jahanian | 524bb20 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3953 | } |
Fariborz Jahanian | 3b0f886 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3954 | // At this point, we are done with Record/Union and array there of. |
| 3955 | // For other arrays we are down to its element type. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3956 | Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT); |
Daniel Dunbar | 7abf83c | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3957 | |
Daniel Dunbar | 7b89ace | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3958 | unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType()); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3959 | if ((ForStrongLayout && GCAttr == Qualifiers::Strong) |
| 3960 | || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) { |
Daniel Dunbar | 22007d3 | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3961 | if (IsUnion) { |
Daniel Dunbar | 7b89ace | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3962 | uint64_t UnionIvarSize = FieldSize / WordSizeInBits; |
Daniel Dunbar | 22007d3 | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3963 | if (UnionIvarSize > MaxUnionIvarSize) { |
Fariborz Jahanian | 3b0f886 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3964 | MaxUnionIvarSize = UnionIvarSize; |
| 3965 | MaxField = Field; |
Daniel Dunbar | 5b74391 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3966 | MaxFieldOffset = FieldOffset; |
Fariborz Jahanian | 3b0f886 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3967 | } |
Daniel Dunbar | 22007d3 | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3968 | } else { |
Daniel Dunbar | 94f46dc | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3969 | IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset, |
Daniel Dunbar | 7b89ace | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3970 | FieldSize / WordSizeInBits)); |
Fariborz Jahanian | 3b0f886 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3971 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3972 | } else if ((ForStrongLayout && |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3973 | (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)) |
| 3974 | || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) { |
Daniel Dunbar | 22007d3 | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3975 | if (IsUnion) { |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 3976 | // FIXME: Why the asymmetry? We divide by word size in bits on other |
| 3977 | // side. |
Daniel Dunbar | 7b89ace | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3978 | uint64_t UnionIvarSize = FieldSize; |
Daniel Dunbar | 22007d3 | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3979 | if (UnionIvarSize > MaxSkippedUnionIvarSize) { |
Fariborz Jahanian | 3b0f886 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3980 | MaxSkippedUnionIvarSize = UnionIvarSize; |
| 3981 | MaxSkippedField = Field; |
Daniel Dunbar | 5b74391 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3982 | MaxSkippedFieldOffset = FieldOffset; |
Fariborz Jahanian | 3b0f886 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3983 | } |
Daniel Dunbar | 22007d3 | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3984 | } else { |
Daniel Dunbar | 7b89ace | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3985 | // FIXME: Why the asymmetry, we divide by byte size in bits here? |
Daniel Dunbar | 94f46dc | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3986 | SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset, |
Daniel Dunbar | 7b89ace | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3987 | FieldSize / ByteSizeInBits)); |
Fariborz Jahanian | 3b0f886 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3988 | } |
| 3989 | } |
| 3990 | } |
Daniel Dunbar | 15bd888 | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3991 | |
Argyrios Kyrtzidis | 2fdb5b5 | 2010-09-06 12:00:10 +0000 | [diff] [blame] | 3992 | if (LastFieldBitfieldOrUnnamed) { |
| 3993 | if (LastFieldBitfieldOrUnnamed->isBitField()) { |
| 3994 | // Last field was a bitfield. Must update skip info. |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 3995 | uint64_t BitFieldSize |
| 3996 | = LastFieldBitfieldOrUnnamed->getBitWidthValue(CGM.getContext()); |
Argyrios Kyrtzidis | 2fdb5b5 | 2010-09-06 12:00:10 +0000 | [diff] [blame] | 3997 | GC_IVAR skivar; |
| 3998 | skivar.ivar_bytepos = BytePos + LastBitfieldOrUnnamedOffset; |
| 3999 | skivar.ivar_size = (BitFieldSize / ByteSizeInBits) |
| 4000 | + ((BitFieldSize % ByteSizeInBits) != 0); |
| 4001 | SkipIvars.push_back(skivar); |
| 4002 | } else { |
| 4003 | assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed"); |
| 4004 | // Last field was unnamed. Must update skip info. |
| 4005 | unsigned FieldSize |
| 4006 | = CGM.getContext().getTypeSize(LastFieldBitfieldOrUnnamed->getType()); |
| 4007 | SkipIvars.push_back(GC_IVAR(BytePos + LastBitfieldOrUnnamedOffset, |
| 4008 | FieldSize / ByteSizeInBits)); |
| 4009 | } |
Fariborz Jahanian | f5fec02 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 4010 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4011 | |
Daniel Dunbar | 7b89ace | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 4012 | if (MaxField) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4013 | IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset, |
Daniel Dunbar | 7b89ace | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 4014 | MaxUnionIvarSize)); |
| 4015 | if (MaxSkippedField) |
Daniel Dunbar | 5b74391 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 4016 | SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset, |
Daniel Dunbar | 7b89ace | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 4017 | MaxSkippedUnionIvarSize)); |
Fariborz Jahanian | c559f3f | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 4018 | } |
| 4019 | |
Fariborz Jahanian | 1f78a9a | 2010-08-05 00:19:48 +0000 | [diff] [blame] | 4020 | /// BuildIvarLayoutBitmap - This routine is the horsework for doing all |
| 4021 | /// the computations and returning the layout bitmap (for ivar or blocks) in |
| 4022 | /// the given argument BitMap string container. Routine reads |
| 4023 | /// two containers, IvarsInfo and SkipIvars which are assumed to be |
| 4024 | /// filled already by the caller. |
Chris Lattner | 9c81833 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 4025 | llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(std::string &BitMap) { |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4026 | unsigned int WordsToScan, WordsToSkip; |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4027 | llvm::Type *PtrTy = CGM.Int8PtrTy; |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4028 | |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4029 | // Build the string of skip/scan nibbles |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4030 | SmallVector<SKIP_SCAN, 32> SkipScanIvars; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4031 | unsigned int WordSize = |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 4032 | CGM.getTypes().getDataLayout().getTypeAllocSize(PtrTy); |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4033 | if (IvarsInfo[0].ivar_bytepos == 0) { |
| 4034 | WordsToSkip = 0; |
| 4035 | WordsToScan = IvarsInfo[0].ivar_size; |
Daniel Dunbar | d22aa4a | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4036 | } else { |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4037 | WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize; |
| 4038 | WordsToScan = IvarsInfo[0].ivar_size; |
| 4039 | } |
Daniel Dunbar | d22aa4a | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4040 | for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4041 | unsigned int TailPrevGCObjC = |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4042 | IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize; |
Daniel Dunbar | d22aa4a | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4043 | if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) { |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4044 | // consecutive 'scanned' object pointers. |
| 4045 | WordsToScan += IvarsInfo[i].ivar_size; |
Daniel Dunbar | d22aa4a | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4046 | } else { |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4047 | // Skip over 'gc'able object pointer which lay over each other. |
| 4048 | if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos) |
| 4049 | continue; |
| 4050 | // Must skip over 1 or more words. We save current skip/scan values |
| 4051 | // and start a new pair. |
Fariborz Jahanian | 1bf7288 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 4052 | SKIP_SCAN SkScan; |
| 4053 | SkScan.skip = WordsToSkip; |
| 4054 | SkScan.scan = WordsToScan; |
Fariborz Jahanian | a123b64 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 4055 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4056 | |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4057 | // Skip the hole. |
Fariborz Jahanian | 1bf7288 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 4058 | SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize; |
| 4059 | SkScan.scan = 0; |
Fariborz Jahanian | a123b64 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 4060 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4061 | WordsToSkip = 0; |
| 4062 | WordsToScan = IvarsInfo[i].ivar_size; |
| 4063 | } |
| 4064 | } |
Daniel Dunbar | d22aa4a | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4065 | if (WordsToScan > 0) { |
Fariborz Jahanian | 1bf7288 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 4066 | SKIP_SCAN SkScan; |
| 4067 | SkScan.skip = WordsToSkip; |
| 4068 | SkScan.scan = WordsToScan; |
Fariborz Jahanian | a123b64 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 4069 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4070 | } |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4071 | |
Daniel Dunbar | d22aa4a | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4072 | if (!SkipIvars.empty()) { |
Fariborz Jahanian | a123b64 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 4073 | unsigned int LastIndex = SkipIvars.size()-1; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4074 | int LastByteSkipped = |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4075 | SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size; |
Fariborz Jahanian | a123b64 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 4076 | LastIndex = IvarsInfo.size()-1; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4077 | int LastByteScanned = |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4078 | IvarsInfo[LastIndex].ivar_bytepos + |
| 4079 | IvarsInfo[LastIndex].ivar_size * WordSize; |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4080 | // 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] | 4081 | if (LastByteSkipped > LastByteScanned) { |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4082 | unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize; |
Fariborz Jahanian | 1bf7288 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 4083 | SKIP_SCAN SkScan; |
| 4084 | SkScan.skip = TotalWords - (LastByteScanned/WordSize); |
| 4085 | SkScan.scan = 0; |
Fariborz Jahanian | a123b64 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 4086 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4087 | } |
| 4088 | } |
| 4089 | // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced |
| 4090 | // as 0xMN. |
Fariborz Jahanian | a123b64 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 4091 | int SkipScan = SkipScanIvars.size()-1; |
Daniel Dunbar | d22aa4a | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4092 | for (int i = 0; i <= SkipScan; i++) { |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4093 | if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0 |
| 4094 | && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) { |
| 4095 | // 0xM0 followed by 0x0N detected. |
| 4096 | SkipScanIvars[i].scan = SkipScanIvars[i+1].scan; |
| 4097 | for (int j = i+1; j < SkipScan; j++) |
| 4098 | SkipScanIvars[j] = SkipScanIvars[j+1]; |
| 4099 | --SkipScan; |
| 4100 | } |
| 4101 | } |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4102 | |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4103 | // Generate the string. |
Daniel Dunbar | d22aa4a | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4104 | for (int i = 0; i <= SkipScan; i++) { |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4105 | unsigned char byte; |
| 4106 | unsigned int skip_small = SkipScanIvars[i].skip % 0xf; |
| 4107 | unsigned int scan_small = SkipScanIvars[i].scan % 0xf; |
| 4108 | unsigned int skip_big = SkipScanIvars[i].skip / 0xf; |
| 4109 | unsigned int scan_big = SkipScanIvars[i].scan / 0xf; |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4110 | |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4111 | // first skip big. |
| 4112 | for (unsigned int ix = 0; ix < skip_big; ix++) |
| 4113 | BitMap += (unsigned char)(0xf0); |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4114 | |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4115 | // next (skip small, scan) |
Daniel Dunbar | d22aa4a | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4116 | if (skip_small) { |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4117 | byte = skip_small << 4; |
Daniel Dunbar | d22aa4a | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4118 | if (scan_big > 0) { |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4119 | byte |= 0xf; |
| 4120 | --scan_big; |
Daniel Dunbar | d22aa4a | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4121 | } else if (scan_small) { |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4122 | byte |= scan_small; |
| 4123 | scan_small = 0; |
| 4124 | } |
| 4125 | BitMap += byte; |
| 4126 | } |
| 4127 | // next scan big |
| 4128 | for (unsigned int ix = 0; ix < scan_big; ix++) |
| 4129 | BitMap += (unsigned char)(0x0f); |
| 4130 | // last scan small |
Daniel Dunbar | d22aa4a | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4131 | if (scan_small) { |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4132 | byte = scan_small; |
| 4133 | BitMap += byte; |
| 4134 | } |
| 4135 | } |
| 4136 | // null terminate string. |
Fariborz Jahanian | f909f92 | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 4137 | unsigned char zero = 0; |
| 4138 | BitMap += zero; |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4139 | |
| 4140 | llvm::GlobalVariable * Entry = |
| 4141 | CreateMetadataVar("\01L_OBJC_CLASS_NAME_", |
Chris Lattner | 9c81833 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 4142 | llvm::ConstantDataArray::getString(VMContext, BitMap,false), |
Daniel Dunbar | 7c9295a | 2011-03-25 20:09:09 +0000 | [diff] [blame] | 4143 | ((ObjCABI == 2) ? |
| 4144 | "__TEXT,__objc_classname,cstring_literals" : |
| 4145 | "__TEXT,__cstring,cstring_literals"), |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4146 | 1, true); |
| 4147 | return getConstantGEP(VMContext, Entry, 0, 0); |
| 4148 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4149 | |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4150 | /// BuildIvarLayout - Builds ivar layout bitmap for the class |
| 4151 | /// implementation for the __strong or __weak case. |
| 4152 | /// The layout map displays which words in ivar list must be skipped |
| 4153 | /// and which must be scanned by GC (see below). String is built of bytes. |
| 4154 | /// Each byte is divided up in two nibbles (4-bit each). Left nibble is count |
| 4155 | /// of words to skip and right nibble is count of words to scan. So, each |
| 4156 | /// nibble represents up to 15 workds to skip or scan. Skipping the rest is |
| 4157 | /// represented by a 0x00 byte which also ends the string. |
| 4158 | /// 1. when ForStrongLayout is true, following ivars are scanned: |
| 4159 | /// - id, Class |
| 4160 | /// - object * |
| 4161 | /// - __strong anything |
| 4162 | /// |
| 4163 | /// 2. When ForStrongLayout is false, following ivars are scanned: |
| 4164 | /// - __weak anything |
| 4165 | /// |
| 4166 | llvm::Constant *CGObjCCommonMac::BuildIvarLayout( |
| 4167 | const ObjCImplementationDecl *OMD, |
| 4168 | bool ForStrongLayout) { |
| 4169 | bool hasUnion = false; |
| 4170 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4171 | llvm::Type *PtrTy = CGM.Int8PtrTy; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4172 | if (CGM.getLangOpts().getGC() == LangOptions::NonGC && |
| 4173 | !CGM.getLangOpts().ObjCAutoRefCount) |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4174 | return llvm::Constant::getNullValue(PtrTy); |
| 4175 | |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 4176 | const ObjCInterfaceDecl *OI = OMD->getClassInterface(); |
| 4177 | SmallVector<const FieldDecl*, 32> RecFields; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4178 | if (CGM.getLangOpts().ObjCAutoRefCount) { |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 4179 | for (const ObjCIvarDecl *IVD = OI->all_declared_ivar_begin(); |
Fariborz Jahanian | b26d578 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 4180 | IVD; IVD = IVD->getNextIvar()) |
| 4181 | RecFields.push_back(cast<FieldDecl>(IVD)); |
| 4182 | } |
| 4183 | else { |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 4184 | SmallVector<const ObjCIvarDecl*, 32> Ivars; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4185 | CGM.getContext().DeepCollectObjCIvars(OI, true, Ivars); |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4186 | |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 4187 | // FIXME: This is not ideal; we shouldn't have to do this copy. |
| 4188 | RecFields.append(Ivars.begin(), Ivars.end()); |
Fariborz Jahanian | b26d578 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 4189 | } |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4190 | |
| 4191 | if (RecFields.empty()) |
| 4192 | return llvm::Constant::getNullValue(PtrTy); |
| 4193 | |
| 4194 | SkipIvars.clear(); |
| 4195 | IvarsInfo.clear(); |
| 4196 | |
| 4197 | BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion); |
| 4198 | if (IvarsInfo.empty()) |
| 4199 | return llvm::Constant::getNullValue(PtrTy); |
Fariborz Jahanian | 1f78a9a | 2010-08-05 00:19:48 +0000 | [diff] [blame] | 4200 | // Sort on byte position in case we encounterred a union nested in |
| 4201 | // the ivar list. |
| 4202 | if (hasUnion && !IvarsInfo.empty()) |
| 4203 | std::sort(IvarsInfo.begin(), IvarsInfo.end()); |
| 4204 | if (hasUnion && !SkipIvars.empty()) |
| 4205 | std::sort(SkipIvars.begin(), SkipIvars.end()); |
| 4206 | |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4207 | std::string BitMap; |
Fariborz Jahanian | 1f78a9a | 2010-08-05 00:19:48 +0000 | [diff] [blame] | 4208 | llvm::Constant *C = BuildIvarLayoutBitmap(BitMap); |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4209 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4210 | if (CGM.getLangOpts().ObjCGCBitmapPrint) { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4211 | printf("\n%s ivar layout for class '%s': ", |
Fariborz Jahanian | 80c9ce2 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 4212 | ForStrongLayout ? "strong" : "weak", |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4213 | OMD->getClassInterface()->getName().data()); |
Roman Divacky | e637711 | 2012-09-06 15:59:27 +0000 | [diff] [blame] | 4214 | const unsigned char *s = (const unsigned char*)BitMap.c_str(); |
Bill Wendling | 5313685 | 2012-02-07 09:06:01 +0000 | [diff] [blame] | 4215 | for (unsigned i = 0, e = BitMap.size(); i < e; i++) |
Fariborz Jahanian | 80c9ce2 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 4216 | if (!(s[i] & 0xf0)) |
| 4217 | printf("0x0%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 4218 | else |
| 4219 | printf("0x%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 4220 | printf("\n"); |
| 4221 | } |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4222 | return C; |
Fariborz Jahanian | c559f3f | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 4223 | } |
| 4224 | |
Fariborz Jahanian | 0b1ccdc | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 4225 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) { |
Daniel Dunbar | cb515c8 | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 4226 | llvm::GlobalVariable *&Entry = MethodVarNames[Sel]; |
| 4227 | |
Chris Lattner | 3def9ae | 2012-02-06 22:16:34 +0000 | [diff] [blame] | 4228 | // FIXME: Avoid std::string in "Sel.getAsString()" |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 4229 | if (!Entry) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4230 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_", |
Chris Lattner | 9c81833 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 4231 | llvm::ConstantDataArray::getString(VMContext, Sel.getAsString()), |
Daniel Dunbar | 7c9295a | 2011-03-25 20:09:09 +0000 | [diff] [blame] | 4232 | ((ObjCABI == 2) ? |
| 4233 | "__TEXT,__objc_methname,cstring_literals" : |
| 4234 | "__TEXT,__cstring,cstring_literals"), |
Daniel Dunbar | 3241fae | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 4235 | 1, true); |
Daniel Dunbar | cb515c8 | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 4236 | |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4237 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4238 | } |
| 4239 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4240 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 0b1ccdc | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 4241 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) { |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4242 | return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID)); |
| 4243 | } |
| 4244 | |
Daniel Dunbar | f5c1846 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 4245 | llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) { |
Devang Patel | 4b6e4bb | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 4246 | std::string TypeStr; |
| 4247 | CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field); |
| 4248 | |
| 4249 | llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr]; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4250 | |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 4251 | if (!Entry) |
| 4252 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_", |
Chris Lattner | 9c81833 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 4253 | llvm::ConstantDataArray::getString(VMContext, TypeStr), |
Daniel Dunbar | 7c9295a | 2011-03-25 20:09:09 +0000 | [diff] [blame] | 4254 | ((ObjCABI == 2) ? |
| 4255 | "__TEXT,__objc_methtype,cstring_literals" : |
| 4256 | "__TEXT,__cstring,cstring_literals"), |
Daniel Dunbar | 3241fae | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 4257 | 1, true); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4258 | |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4259 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | cb515c8 | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 4260 | } |
| 4261 | |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4262 | llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D, |
| 4263 | bool Extended) { |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4264 | std::string TypeStr; |
Bill Wendling | e22bef7 | 2012-02-09 22:45:21 +0000 | [diff] [blame] | 4265 | if (CGM.getContext().getObjCEncodingForMethodDecl(D, TypeStr, Extended)) |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4266 | return 0; |
Devang Patel | 4b6e4bb | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 4267 | |
| 4268 | llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr]; |
| 4269 | |
Daniel Dunbar | 3241fae | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 4270 | if (!Entry) |
| 4271 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_", |
Chris Lattner | 9c81833 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 4272 | llvm::ConstantDataArray::getString(VMContext, TypeStr), |
Daniel Dunbar | 7c9295a | 2011-03-25 20:09:09 +0000 | [diff] [blame] | 4273 | ((ObjCABI == 2) ? |
| 4274 | "__TEXT,__objc_methtype,cstring_literals" : |
| 4275 | "__TEXT,__cstring,cstring_literals"), |
Daniel Dunbar | 3241fae | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 4276 | 1, true); |
Devang Patel | 4b6e4bb | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 4277 | |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4278 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4279 | } |
| 4280 | |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 4281 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 0b1ccdc | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 4282 | llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) { |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 4283 | llvm::GlobalVariable *&Entry = PropertyNames[Ident]; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4284 | |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 4285 | if (!Entry) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4286 | Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_", |
Chris Lattner | 9c81833 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 4287 | llvm::ConstantDataArray::getString(VMContext, |
| 4288 | Ident->getNameStart()), |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 4289 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | 3241fae | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 4290 | 1, true); |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 4291 | |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4292 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 4293 | } |
| 4294 | |
| 4295 | // FIXME: Merge into a single cstring creation function. |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4296 | // FIXME: This Decl should be more precise. |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 4297 | llvm::Constant * |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4298 | CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD, |
| 4299 | const Decl *Container) { |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4300 | std::string TypeStr; |
| 4301 | CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr); |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 4302 | return GetPropertyName(&CGM.getContext().Idents.get(TypeStr)); |
| 4303 | } |
| 4304 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4305 | void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D, |
Fariborz Jahanian | 0b1ccdc | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 4306 | const ObjCContainerDecl *CD, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4307 | SmallVectorImpl<char> &Name) { |
Daniel Dunbar | d238681 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 4308 | llvm::raw_svector_ostream OS(Name); |
Fariborz Jahanian | 0196a1c | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 4309 | assert (CD && "Missing container decl in GetNameForMethod"); |
Daniel Dunbar | d238681 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 4310 | OS << '\01' << (D->isInstanceMethod() ? '-' : '+') |
| 4311 | << '[' << CD->getName(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4312 | if (const ObjCCategoryImplDecl *CID = |
Daniel Dunbar | d238681 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 4313 | dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext())) |
Benjamin Kramer | 2f56992 | 2012-02-07 11:57:45 +0000 | [diff] [blame] | 4314 | OS << '(' << *CID << ')'; |
Daniel Dunbar | d238681 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 4315 | OS << ' ' << D->getSelector().getAsString() << ']'; |
Daniel Dunbar | a94ecd2 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 4316 | } |
| 4317 | |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 4318 | void CGObjCMac::FinishModule() { |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4319 | EmitModuleInfo(); |
| 4320 | |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 4321 | // Emit the dummy bodies for any protocols which were referenced but |
| 4322 | // never defined. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4323 | for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4324 | I = Protocols.begin(), e = Protocols.end(); I != e; ++I) { |
| 4325 | if (I->second->hasInitializer()) |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 4326 | continue; |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4327 | |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 4328 | llvm::Constant *Values[5]; |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4329 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4330 | Values[1] = GetClassName(I->first); |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4331 | Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 4332 | Values[3] = Values[4] = |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4333 | llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy); |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4334 | I->second->setLinkage(llvm::GlobalValue::InternalLinkage); |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 4335 | I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 4336 | Values)); |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4337 | CGM.AddUsedGlobal(I->second); |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 4338 | } |
| 4339 | |
Daniel Dunbar | c61d0e9 | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 4340 | // Add assembler directives to add lazy undefined symbol references |
| 4341 | // for classes which are referenced but not defined. This is |
| 4342 | // important for correct linker interaction. |
Daniel Dunbar | d027a92 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 4343 | // |
| 4344 | // FIXME: It would be nice if we had an LLVM construct for this. |
| 4345 | if (!LazySymbols.empty() || !DefinedSymbols.empty()) { |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 4346 | SmallString<256> Asm; |
Daniel Dunbar | d027a92 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 4347 | Asm += CGM.getModule().getModuleInlineAsm(); |
| 4348 | if (!Asm.empty() && Asm.back() != '\n') |
| 4349 | Asm += '\n'; |
Daniel Dunbar | c61d0e9 | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 4350 | |
Daniel Dunbar | d027a92 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 4351 | llvm::raw_svector_ostream OS(Asm); |
Daniel Dunbar | d027a92 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 4352 | for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(), |
| 4353 | e = DefinedSymbols.end(); I != e; ++I) |
Daniel Dunbar | 07d0785 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 4354 | OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n" |
| 4355 | << "\t.globl .objc_class_name_" << (*I)->getName() << "\n"; |
Chris Lattner | a299f2c | 2010-04-17 18:26:20 +0000 | [diff] [blame] | 4356 | for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(), |
Fariborz Jahanian | 9adb2e6 | 2010-06-21 22:05:18 +0000 | [diff] [blame] | 4357 | e = LazySymbols.end(); I != e; ++I) { |
Chris Lattner | a299f2c | 2010-04-17 18:26:20 +0000 | [diff] [blame] | 4358 | OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n"; |
Fariborz Jahanian | 9adb2e6 | 2010-06-21 22:05:18 +0000 | [diff] [blame] | 4359 | } |
| 4360 | |
Bill Wendling | 5313685 | 2012-02-07 09:06:01 +0000 | [diff] [blame] | 4361 | for (size_t i = 0, e = DefinedCategoryNames.size(); i < e; ++i) { |
Fariborz Jahanian | 9adb2e6 | 2010-06-21 22:05:18 +0000 | [diff] [blame] | 4362 | OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n" |
| 4363 | << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n"; |
| 4364 | } |
Chris Lattner | a299f2c | 2010-04-17 18:26:20 +0000 | [diff] [blame] | 4365 | |
Daniel Dunbar | d027a92 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 4366 | CGM.getModule().setModuleInlineAsm(OS.str()); |
Daniel Dunbar | c61d0e9 | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 4367 | } |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 4368 | } |
| 4369 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4370 | CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm) |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4371 | : CGObjCCommonMac(cgm), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4372 | ObjCTypes(cgm) { |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4373 | ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL; |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4374 | ObjCABI = 2; |
| 4375 | } |
| 4376 | |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 4377 | /* *** */ |
| 4378 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4379 | ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm) |
Douglas Gregor | a95f9aa | 2012-01-17 23:38:32 +0000 | [diff] [blame] | 4380 | : VMContext(cgm.getLLVMContext()), CGM(cgm), ExternalProtocolPtrTy(0) |
| 4381 | { |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4382 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 4383 | ASTContext &Ctx = CGM.getContext(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4384 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4385 | ShortTy = Types.ConvertType(Ctx.ShortTy); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4386 | IntTy = Types.ConvertType(Ctx.IntTy); |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4387 | LongTy = Types.ConvertType(Ctx.LongTy); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 4388 | LongLongTy = Types.ConvertType(Ctx.LongLongTy); |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4389 | Int8PtrTy = CGM.Int8PtrTy; |
| 4390 | Int8PtrPtrTy = CGM.Int8PtrPtrTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4391 | |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4392 | ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType()); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4393 | PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy); |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4394 | SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType()); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4395 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4396 | // I'm not sure I like this. The implicit coordination is a bit |
| 4397 | // gross. We should solve this in a reasonable fashion because this |
| 4398 | // is a pretty common task (match some runtime data structure with |
| 4399 | // an LLVM data structure). |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4400 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4401 | // FIXME: This is leaked. |
| 4402 | // FIXME: Merge with rewriter code? |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4403 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4404 | // struct _objc_super { |
| 4405 | // id self; |
| 4406 | // Class cls; |
| 4407 | // } |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 4408 | RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct, |
Daniel Dunbar | 0c00537 | 2010-04-29 16:29:11 +0000 | [diff] [blame] | 4409 | Ctx.getTranslationUnitDecl(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4410 | SourceLocation(), SourceLocation(), |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4411 | &Ctx.Idents.get("_objc_super")); |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4412 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0, |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 4413 | Ctx.getObjCIdType(), 0, 0, false, ICIS_NoInit)); |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4414 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0, |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 4415 | Ctx.getObjCClassType(), 0, 0, false, |
| 4416 | ICIS_NoInit)); |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 4417 | RD->completeDefinition(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4418 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4419 | SuperCTy = Ctx.getTagDeclType(RD); |
| 4420 | SuperPtrCTy = Ctx.getPointerType(SuperCTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4421 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4422 | SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy)); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4423 | SuperPtrTy = llvm::PointerType::getUnqual(SuperTy); |
| 4424 | |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4425 | // struct _prop_t { |
| 4426 | // char *name; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4427 | // char *attributes; |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4428 | // } |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4429 | PropertyTy = llvm::StructType::create("struct._prop_t", |
| 4430 | Int8PtrTy, Int8PtrTy, NULL); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4431 | |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4432 | // struct _prop_list_t { |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4433 | // uint32_t entsize; // sizeof(struct _prop_t) |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4434 | // uint32_t count_of_properties; |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4435 | // struct _prop_t prop_list[count_of_properties]; |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4436 | // } |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4437 | PropertyListTy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4438 | llvm::StructType::create("struct._prop_list_t", IntTy, IntTy, |
| 4439 | llvm::ArrayType::get(PropertyTy, 0), NULL); |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4440 | // struct _prop_list_t * |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4441 | PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4442 | |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4443 | // struct _objc_method { |
| 4444 | // SEL _cmd; |
| 4445 | // char *method_type; |
| 4446 | // char *_imp; |
| 4447 | // } |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4448 | MethodTy = llvm::StructType::create("struct._objc_method", |
| 4449 | SelectorPtrTy, Int8PtrTy, Int8PtrTy, |
| 4450 | NULL); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4451 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4452 | // struct _objc_cache * |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4453 | CacheTy = llvm::StructType::create(VMContext, "struct._objc_cache"); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4454 | CachePtrTy = llvm::PointerType::getUnqual(CacheTy); |
Fariborz Jahanian | 0a3cfcc | 2011-06-22 20:21:51 +0000 | [diff] [blame] | 4455 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4456 | } |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4457 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4458 | ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4459 | : ObjCCommonTypesHelper(cgm) { |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4460 | // struct _objc_method_description { |
| 4461 | // SEL name; |
| 4462 | // char *types; |
| 4463 | // } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4464 | MethodDescriptionTy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4465 | llvm::StructType::create("struct._objc_method_description", |
| 4466 | SelectorPtrTy, Int8PtrTy, NULL); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4467 | |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4468 | // struct _objc_method_description_list { |
| 4469 | // int count; |
| 4470 | // struct _objc_method_description[1]; |
| 4471 | // } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4472 | MethodDescriptionListTy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4473 | llvm::StructType::create("struct._objc_method_description_list", |
| 4474 | IntTy, |
| 4475 | llvm::ArrayType::get(MethodDescriptionTy, 0),NULL); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4476 | |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4477 | // struct _objc_method_description_list * |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4478 | MethodDescriptionListPtrTy = |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4479 | llvm::PointerType::getUnqual(MethodDescriptionListTy); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4480 | |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4481 | // Protocol description structures |
| 4482 | |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4483 | // struct _objc_protocol_extension { |
| 4484 | // uint32_t size; // sizeof(struct _objc_protocol_extension) |
| 4485 | // struct _objc_method_description_list *optional_instance_methods; |
| 4486 | // struct _objc_method_description_list *optional_class_methods; |
| 4487 | // struct _objc_property_list *instance_properties; |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4488 | // const char ** extendedMethodTypes; |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4489 | // } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4490 | ProtocolExtensionTy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4491 | llvm::StructType::create("struct._objc_protocol_extension", |
| 4492 | IntTy, MethodDescriptionListPtrTy, |
| 4493 | MethodDescriptionListPtrTy, PropertyListPtrTy, |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4494 | Int8PtrPtrTy, NULL); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4495 | |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4496 | // struct _objc_protocol_extension * |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4497 | ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4498 | |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 4499 | // Handle recursive construction of Protocol and ProtocolList types |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4500 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4501 | ProtocolTy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4502 | llvm::StructType::create(VMContext, "struct._objc_protocol"); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4503 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4504 | ProtocolListTy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4505 | llvm::StructType::create(VMContext, "struct._objc_protocol_list"); |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4506 | ProtocolListTy->setBody(llvm::PointerType::getUnqual(ProtocolListTy), |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4507 | LongTy, |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4508 | llvm::ArrayType::get(ProtocolTy, 0), |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4509 | NULL); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4510 | |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4511 | // struct _objc_protocol { |
| 4512 | // struct _objc_protocol_extension *isa; |
| 4513 | // char *protocol_name; |
| 4514 | // struct _objc_protocol **_objc_protocol_list; |
| 4515 | // struct _objc_method_description_list *instance_methods; |
| 4516 | // struct _objc_method_description_list *class_methods; |
| 4517 | // } |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4518 | ProtocolTy->setBody(ProtocolExtensionPtrTy, Int8PtrTy, |
| 4519 | llvm::PointerType::getUnqual(ProtocolListTy), |
| 4520 | MethodDescriptionListPtrTy, |
| 4521 | MethodDescriptionListPtrTy, |
| 4522 | NULL); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4523 | |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4524 | // struct _objc_protocol_list * |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4525 | ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4526 | |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4527 | ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4528 | |
| 4529 | // Class description structures |
| 4530 | |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4531 | // struct _objc_ivar { |
| 4532 | // char *ivar_name; |
| 4533 | // char *ivar_type; |
| 4534 | // int ivar_offset; |
| 4535 | // } |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4536 | IvarTy = llvm::StructType::create("struct._objc_ivar", |
| 4537 | Int8PtrTy, Int8PtrTy, IntTy, NULL); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4538 | |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4539 | // struct _objc_ivar_list * |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4540 | IvarListTy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4541 | llvm::StructType::create(VMContext, "struct._objc_ivar_list"); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4542 | IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4543 | |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4544 | // struct _objc_method_list * |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4545 | MethodListTy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4546 | llvm::StructType::create(VMContext, "struct._objc_method_list"); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4547 | MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4548 | |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4549 | // struct _objc_class_extension * |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4550 | ClassExtensionTy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4551 | llvm::StructType::create("struct._objc_class_extension", |
| 4552 | IntTy, Int8PtrTy, PropertyListPtrTy, NULL); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4553 | ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4554 | |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4555 | ClassTy = llvm::StructType::create(VMContext, "struct._objc_class"); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4556 | |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4557 | // struct _objc_class { |
| 4558 | // Class isa; |
| 4559 | // Class super_class; |
| 4560 | // char *name; |
| 4561 | // long version; |
| 4562 | // long info; |
| 4563 | // long instance_size; |
| 4564 | // struct _objc_ivar_list *ivars; |
| 4565 | // struct _objc_method_list *methods; |
| 4566 | // struct _objc_cache *cache; |
| 4567 | // struct _objc_protocol_list *protocols; |
| 4568 | // char *ivar_layout; |
| 4569 | // struct _objc_class_ext *ext; |
| 4570 | // }; |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4571 | ClassTy->setBody(llvm::PointerType::getUnqual(ClassTy), |
| 4572 | llvm::PointerType::getUnqual(ClassTy), |
| 4573 | Int8PtrTy, |
| 4574 | LongTy, |
| 4575 | LongTy, |
| 4576 | LongTy, |
| 4577 | IvarListPtrTy, |
| 4578 | MethodListPtrTy, |
| 4579 | CachePtrTy, |
| 4580 | ProtocolListPtrTy, |
| 4581 | Int8PtrTy, |
| 4582 | ClassExtensionPtrTy, |
| 4583 | NULL); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4584 | |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4585 | ClassPtrTy = llvm::PointerType::getUnqual(ClassTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4586 | |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4587 | // struct _objc_category { |
| 4588 | // char *category_name; |
| 4589 | // char *class_name; |
| 4590 | // struct _objc_method_list *instance_method; |
| 4591 | // struct _objc_method_list *class_method; |
| 4592 | // uint32_t size; // sizeof(struct _objc_category) |
| 4593 | // struct _objc_property_list *instance_properties;// category's @property |
| 4594 | // } |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4595 | CategoryTy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4596 | llvm::StructType::create("struct._objc_category", |
| 4597 | Int8PtrTy, Int8PtrTy, MethodListPtrTy, |
| 4598 | MethodListPtrTy, ProtocolListPtrTy, |
| 4599 | IntTy, PropertyListPtrTy, NULL); |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 4600 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4601 | // Global metadata structures |
| 4602 | |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4603 | // struct _objc_symtab { |
| 4604 | // long sel_ref_cnt; |
| 4605 | // SEL *refs; |
| 4606 | // short cls_def_cnt; |
| 4607 | // short cat_def_cnt; |
| 4608 | // char *defs[cls_def_cnt + cat_def_cnt]; |
| 4609 | // } |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4610 | SymtabTy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4611 | llvm::StructType::create("struct._objc_symtab", |
| 4612 | LongTy, SelectorPtrTy, ShortTy, ShortTy, |
| 4613 | llvm::ArrayType::get(Int8PtrTy, 0), NULL); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4614 | SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4615 | |
Fariborz Jahanian | eee54df | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 4616 | // struct _objc_module { |
| 4617 | // long version; |
| 4618 | // long size; // sizeof(struct _objc_module) |
| 4619 | // char *name; |
| 4620 | // struct _objc_symtab* symtab; |
| 4621 | // } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4622 | ModuleTy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4623 | llvm::StructType::create("struct._objc_module", |
| 4624 | LongTy, LongTy, Int8PtrTy, SymtabPtrTy, NULL); |
Daniel Dunbar | 97ff50d | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 4625 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4626 | |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 4627 | // FIXME: This is the size of the setjmp buffer and should be target |
| 4628 | // specific. 18 is what's used on 32-bit X86. |
Anders Carlsson | 9ff2248 | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 4629 | uint64_t SetJmpBufferSize = 18; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4630 | |
Anders Carlsson | 9ff2248 | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 4631 | // Exceptions |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4632 | llvm::Type *StackPtrTy = llvm::ArrayType::get(CGM.Int8PtrTy, 4); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4633 | |
| 4634 | ExceptionDataTy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4635 | llvm::StructType::create("struct._objc_exception_data", |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4636 | llvm::ArrayType::get(CGM.Int32Ty,SetJmpBufferSize), |
| 4637 | StackPtrTy, NULL); |
Anders Carlsson | 9ff2248 | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 4638 | |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 4639 | } |
| 4640 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4641 | ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4642 | : ObjCCommonTypesHelper(cgm) { |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4643 | // struct _method_list_t { |
| 4644 | // uint32_t entsize; // sizeof(struct _objc_method) |
| 4645 | // uint32_t method_count; |
| 4646 | // struct _objc_method method_list[method_count]; |
| 4647 | // } |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4648 | MethodListnfABITy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4649 | llvm::StructType::create("struct.__method_list_t", IntTy, IntTy, |
| 4650 | llvm::ArrayType::get(MethodTy, 0), NULL); |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4651 | // struct method_list_t * |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4652 | MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4653 | |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4654 | // struct _protocol_t { |
| 4655 | // id isa; // NULL |
| 4656 | // const char * const protocol_name; |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4657 | // const struct _protocol_list_t * protocol_list; // super protocols |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4658 | // const struct method_list_t * const instance_methods; |
| 4659 | // const struct method_list_t * const class_methods; |
| 4660 | // const struct method_list_t *optionalInstanceMethods; |
| 4661 | // const struct method_list_t *optionalClassMethods; |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4662 | // const struct _prop_list_t * properties; |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4663 | // const uint32_t size; // sizeof(struct _protocol_t) |
| 4664 | // const uint32_t flags; // = 0 |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4665 | // const char ** extendedMethodTypes; |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4666 | // } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4667 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4668 | // Holder for struct _protocol_list_t * |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4669 | ProtocolListnfABITy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4670 | llvm::StructType::create(VMContext, "struct._objc_protocol_list"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4671 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4672 | ProtocolnfABITy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4673 | llvm::StructType::create("struct._protocol_t", ObjectPtrTy, Int8PtrTy, |
| 4674 | llvm::PointerType::getUnqual(ProtocolListnfABITy), |
| 4675 | MethodListnfABIPtrTy, MethodListnfABIPtrTy, |
| 4676 | MethodListnfABIPtrTy, MethodListnfABIPtrTy, |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4677 | PropertyListPtrTy, IntTy, IntTy, Int8PtrPtrTy, |
| 4678 | NULL); |
Daniel Dunbar | 8de90f0 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4679 | |
| 4680 | // struct _protocol_t* |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4681 | ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4682 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4683 | // struct _protocol_list_t { |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4684 | // long protocol_count; // Note, this is 32/64 bit |
Daniel Dunbar | 8de90f0 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4685 | // struct _protocol_t *[protocol_count]; |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4686 | // } |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4687 | ProtocolListnfABITy->setBody(LongTy, |
| 4688 | llvm::ArrayType::get(ProtocolnfABIPtrTy, 0), |
| 4689 | NULL); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4690 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4691 | // struct _objc_protocol_list* |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4692 | ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4693 | |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4694 | // struct _ivar_t { |
| 4695 | // unsigned long int *offset; // pointer to ivar offset location |
| 4696 | // char *name; |
| 4697 | // char *type; |
| 4698 | // uint32_t alignment; |
| 4699 | // uint32_t size; |
| 4700 | // } |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4701 | IvarnfABITy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4702 | llvm::StructType::create("struct._ivar_t", |
| 4703 | llvm::PointerType::getUnqual(LongTy), |
| 4704 | Int8PtrTy, Int8PtrTy, IntTy, IntTy, NULL); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4705 | |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4706 | // struct _ivar_list_t { |
| 4707 | // uint32 entsize; // sizeof(struct _ivar_t) |
| 4708 | // uint32 count; |
| 4709 | // struct _iver_t list[count]; |
| 4710 | // } |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4711 | IvarListnfABITy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4712 | llvm::StructType::create("struct._ivar_list_t", IntTy, IntTy, |
| 4713 | llvm::ArrayType::get(IvarnfABITy, 0), NULL); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4714 | |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4715 | IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4716 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4717 | // struct _class_ro_t { |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4718 | // uint32_t const flags; |
| 4719 | // uint32_t const instanceStart; |
| 4720 | // uint32_t const instanceSize; |
| 4721 | // uint32_t const reserved; // only when building for 64bit targets |
| 4722 | // const uint8_t * const ivarLayout; |
| 4723 | // const char *const name; |
| 4724 | // const struct _method_list_t * const baseMethods; |
| 4725 | // const struct _objc_protocol_list *const baseProtocols; |
| 4726 | // const struct _ivar_list_t *const ivars; |
| 4727 | // const uint8_t * const weakIvarLayout; |
| 4728 | // const struct _prop_list_t * const properties; |
| 4729 | // } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4730 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4731 | // FIXME. Add 'reserved' field in 64bit abi mode! |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4732 | ClassRonfABITy = llvm::StructType::create("struct._class_ro_t", |
| 4733 | IntTy, IntTy, IntTy, Int8PtrTy, |
| 4734 | Int8PtrTy, MethodListnfABIPtrTy, |
| 4735 | ProtocolListnfABIPtrTy, |
| 4736 | IvarListnfABIPtrTy, |
| 4737 | Int8PtrTy, PropertyListPtrTy, NULL); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4738 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4739 | // ImpnfABITy - LLVM for id (*)(id, SEL, ...) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4740 | llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy }; |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 4741 | ImpnfABITy = llvm::FunctionType::get(ObjectPtrTy, params, false) |
| 4742 | ->getPointerTo(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4743 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4744 | // struct _class_t { |
| 4745 | // struct _class_t *isa; |
| 4746 | // struct _class_t * const superclass; |
| 4747 | // void *cache; |
| 4748 | // IMP *vtable; |
| 4749 | // struct class_ro_t *ro; |
| 4750 | // } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4751 | |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4752 | ClassnfABITy = llvm::StructType::create(VMContext, "struct._class_t"); |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4753 | ClassnfABITy->setBody(llvm::PointerType::getUnqual(ClassnfABITy), |
| 4754 | llvm::PointerType::getUnqual(ClassnfABITy), |
| 4755 | CachePtrTy, |
| 4756 | llvm::PointerType::getUnqual(ImpnfABITy), |
| 4757 | llvm::PointerType::getUnqual(ClassRonfABITy), |
| 4758 | NULL); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4759 | |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4760 | // LLVM for struct _class_t * |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4761 | ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4762 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4763 | // struct _category_t { |
| 4764 | // const char * const name; |
| 4765 | // struct _class_t *const cls; |
| 4766 | // const struct _method_list_t * const instance_methods; |
| 4767 | // const struct _method_list_t * const class_methods; |
| 4768 | // const struct _protocol_list_t * const protocols; |
| 4769 | // const struct _prop_list_t * const properties; |
Fariborz Jahanian | 5a63e4c | 2009-01-23 17:41:22 +0000 | [diff] [blame] | 4770 | // } |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4771 | CategorynfABITy = llvm::StructType::create("struct._category_t", |
| 4772 | Int8PtrTy, ClassnfABIPtrTy, |
| 4773 | MethodListnfABIPtrTy, |
| 4774 | MethodListnfABIPtrTy, |
| 4775 | ProtocolListnfABIPtrTy, |
| 4776 | PropertyListPtrTy, |
| 4777 | NULL); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4778 | |
Fariborz Jahanian | 82c72e1 | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 4779 | // New types for nonfragile abi messaging. |
Fariborz Jahanian | e4dc35d | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4780 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 4781 | ASTContext &Ctx = CGM.getContext(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4782 | |
Fariborz Jahanian | 82c72e1 | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 4783 | // MessageRefTy - LLVM for: |
| 4784 | // struct _message_ref_t { |
| 4785 | // IMP messenger; |
| 4786 | // SEL name; |
| 4787 | // }; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4788 | |
Fariborz Jahanian | e4dc35d | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4789 | // First the clang type for struct _message_ref_t |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 4790 | RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct, |
Daniel Dunbar | 0c00537 | 2010-04-29 16:29:11 +0000 | [diff] [blame] | 4791 | Ctx.getTranslationUnitDecl(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4792 | SourceLocation(), SourceLocation(), |
Fariborz Jahanian | e4dc35d | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4793 | &Ctx.Idents.get("_message_ref_t")); |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4794 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0, |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 4795 | Ctx.VoidPtrTy, 0, 0, false, ICIS_NoInit)); |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4796 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0, |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 4797 | Ctx.getObjCSelType(), 0, 0, false, |
| 4798 | ICIS_NoInit)); |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 4799 | RD->completeDefinition(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4800 | |
Fariborz Jahanian | e4dc35d | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4801 | MessageRefCTy = Ctx.getTagDeclType(RD); |
| 4802 | MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy); |
| 4803 | MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy)); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4804 | |
Fariborz Jahanian | 82c72e1 | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 4805 | // MessageRefPtrTy - LLVM for struct _message_ref_t* |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4806 | MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4807 | |
Fariborz Jahanian | 82c72e1 | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 4808 | // SuperMessageRefTy - LLVM for: |
| 4809 | // struct _super_message_ref_t { |
| 4810 | // SUPER_IMP messenger; |
| 4811 | // SEL name; |
| 4812 | // }; |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4813 | SuperMessageRefTy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4814 | llvm::StructType::create("struct._super_message_ref_t", |
| 4815 | ImpnfABITy, SelectorPtrTy, NULL); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4816 | |
Fariborz Jahanian | 82c72e1 | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 4817 | // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t* |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4818 | SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy); |
Fariborz Jahanian | 0a3cfcc | 2011-06-22 20:21:51 +0000 | [diff] [blame] | 4819 | |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 4820 | |
| 4821 | // struct objc_typeinfo { |
| 4822 | // const void** vtable; // objc_ehtype_vtable + 2 |
| 4823 | // const char* name; // c++ typeinfo string |
| 4824 | // Class cls; |
| 4825 | // }; |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4826 | EHTypeTy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4827 | llvm::StructType::create("struct._objc_typeinfo", |
| 4828 | llvm::PointerType::getUnqual(Int8PtrTy), |
| 4829 | Int8PtrTy, ClassnfABIPtrTy, NULL); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4830 | EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy); |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 4831 | } |
| 4832 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4833 | llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() { |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4834 | FinishNonFragileABIModule(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4835 | |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4836 | return NULL; |
| 4837 | } |
| 4838 | |
Bill Wendling | cb5b5ff | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 4839 | void CGObjCNonFragileABIMac:: |
| 4840 | AddModuleClassList(ArrayRef<llvm::GlobalValue*> Container, |
| 4841 | const char *SymbolName, |
| 4842 | const char *SectionName) { |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4843 | unsigned NumClasses = Container.size(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4844 | |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4845 | if (!NumClasses) |
| 4846 | return; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4847 | |
Chris Lattner | 3def9ae | 2012-02-06 22:16:34 +0000 | [diff] [blame] | 4848 | SmallVector<llvm::Constant*, 8> Symbols(NumClasses); |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4849 | for (unsigned i=0; i<NumClasses; i++) |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 4850 | Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i], |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4851 | ObjCTypes.Int8PtrTy); |
Chris Lattner | 3def9ae | 2012-02-06 22:16:34 +0000 | [diff] [blame] | 4852 | llvm::Constant *Init = |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4853 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
Chris Lattner | 3def9ae | 2012-02-06 22:16:34 +0000 | [diff] [blame] | 4854 | Symbols.size()), |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4855 | Symbols); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4856 | |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4857 | llvm::GlobalVariable *GV = |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4858 | new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4859 | llvm::GlobalValue::InternalLinkage, |
| 4860 | Init, |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4861 | SymbolName); |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 4862 | GV->setAlignment(CGM.getDataLayout().getABITypeAlignment(Init->getType())); |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4863 | GV->setSection(SectionName); |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4864 | CGM.AddUsedGlobal(GV); |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4865 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4866 | |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4867 | void CGObjCNonFragileABIMac::FinishNonFragileABIModule() { |
| 4868 | // nonfragile abi has no module definition. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4869 | |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4870 | // Build list of all implemented class addresses in array |
Fariborz Jahanian | 279abd3 | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4871 | // L_OBJC_LABEL_CLASS_$. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4872 | AddModuleClassList(DefinedClasses, |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4873 | "\01L_OBJC_LABEL_CLASS_$", |
| 4874 | "__DATA, __objc_classlist, regular, no_dead_strip"); |
Fariborz Jahanian | 6726055 | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 4875 | |
Bill Wendling | 5313685 | 2012-02-07 09:06:01 +0000 | [diff] [blame] | 4876 | for (unsigned i = 0, e = DefinedClasses.size(); i < e; i++) { |
Fariborz Jahanian | 6726055 | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 4877 | llvm::GlobalValue *IMPLGV = DefinedClasses[i]; |
| 4878 | if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage) |
| 4879 | continue; |
| 4880 | IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage); |
Fariborz Jahanian | 6726055 | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 4881 | } |
| 4882 | |
Bill Wendling | 5313685 | 2012-02-07 09:06:01 +0000 | [diff] [blame] | 4883 | for (unsigned i = 0, e = DefinedMetaClasses.size(); i < e; i++) { |
Fariborz Jahanian | a6227fd | 2009-12-01 18:25:24 +0000 | [diff] [blame] | 4884 | llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i]; |
| 4885 | if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage) |
| 4886 | continue; |
| 4887 | IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 4888 | } |
Fariborz Jahanian | 6726055 | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 4889 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4890 | AddModuleClassList(DefinedNonLazyClasses, |
Daniel Dunbar | 9a017d7 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 4891 | "\01L_OBJC_LABEL_NONLAZY_CLASS_$", |
| 4892 | "__DATA, __objc_nlclslist, regular, no_dead_strip"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4893 | |
Fariborz Jahanian | 279abd3 | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4894 | // Build list of all implemented category addresses in array |
| 4895 | // L_OBJC_LABEL_CATEGORY_$. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4896 | AddModuleClassList(DefinedCategories, |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4897 | "\01L_OBJC_LABEL_CATEGORY_$", |
| 4898 | "__DATA, __objc_catlist, regular, no_dead_strip"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4899 | AddModuleClassList(DefinedNonLazyCategories, |
Daniel Dunbar | 9a017d7 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 4900 | "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$", |
| 4901 | "__DATA, __objc_nlcatlist, regular, no_dead_strip"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4902 | |
Daniel Dunbar | 5e63927 | 2010-04-25 20:39:01 +0000 | [diff] [blame] | 4903 | EmitImageInfo(); |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4904 | } |
| 4905 | |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 4906 | /// isVTableDispatchedSelector - Returns true if SEL is not in the list of |
| 4907 | /// VTableDispatchMethods; false otherwise. What this means is that |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4908 | /// except for the 19 selectors in the list, we generate 32bit-style |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 4909 | /// message dispatch call for all the rest. |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 4910 | bool CGObjCNonFragileABIMac::isVTableDispatchedSelector(Selector Sel) { |
| 4911 | // At various points we've experimented with using vtable-based |
| 4912 | // dispatch for all methods. |
Daniel Dunbar | fca18c1b4 | 2010-04-24 17:56:46 +0000 | [diff] [blame] | 4913 | switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) { |
Daniel Dunbar | fca18c1b4 | 2010-04-24 17:56:46 +0000 | [diff] [blame] | 4914 | case CodeGenOptions::Legacy: |
Fariborz Jahanian | dfb3983 | 2010-04-19 17:53:30 +0000 | [diff] [blame] | 4915 | return false; |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 4916 | case CodeGenOptions::NonLegacy: |
| 4917 | return true; |
Daniel Dunbar | fca18c1b4 | 2010-04-24 17:56:46 +0000 | [diff] [blame] | 4918 | case CodeGenOptions::Mixed: |
| 4919 | break; |
| 4920 | } |
| 4921 | |
| 4922 | // If so, see whether this selector is in the white-list of things which must |
| 4923 | // 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] | 4924 | if (VTableDispatchMethods.empty()) { |
| 4925 | VTableDispatchMethods.insert(GetNullarySelector("alloc")); |
| 4926 | VTableDispatchMethods.insert(GetNullarySelector("class")); |
| 4927 | VTableDispatchMethods.insert(GetNullarySelector("self")); |
| 4928 | VTableDispatchMethods.insert(GetNullarySelector("isFlipped")); |
| 4929 | VTableDispatchMethods.insert(GetNullarySelector("length")); |
| 4930 | VTableDispatchMethods.insert(GetNullarySelector("count")); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4931 | |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 4932 | // These are vtable-based if GC is disabled. |
| 4933 | // Optimistically use vtable dispatch for hybrid compiles. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4934 | if (CGM.getLangOpts().getGC() != LangOptions::GCOnly) { |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 4935 | VTableDispatchMethods.insert(GetNullarySelector("retain")); |
| 4936 | VTableDispatchMethods.insert(GetNullarySelector("release")); |
| 4937 | VTableDispatchMethods.insert(GetNullarySelector("autorelease")); |
| 4938 | } |
| 4939 | |
| 4940 | VTableDispatchMethods.insert(GetUnarySelector("allocWithZone")); |
| 4941 | VTableDispatchMethods.insert(GetUnarySelector("isKindOfClass")); |
| 4942 | VTableDispatchMethods.insert(GetUnarySelector("respondsToSelector")); |
| 4943 | VTableDispatchMethods.insert(GetUnarySelector("objectForKey")); |
| 4944 | VTableDispatchMethods.insert(GetUnarySelector("objectAtIndex")); |
| 4945 | VTableDispatchMethods.insert(GetUnarySelector("isEqualToString")); |
| 4946 | VTableDispatchMethods.insert(GetUnarySelector("isEqual")); |
| 4947 | |
| 4948 | // These are vtable-based if GC is enabled. |
| 4949 | // Optimistically use vtable dispatch for hybrid compiles. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4950 | if (CGM.getLangOpts().getGC() != LangOptions::NonGC) { |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 4951 | VTableDispatchMethods.insert(GetNullarySelector("hash")); |
| 4952 | VTableDispatchMethods.insert(GetUnarySelector("addObject")); |
| 4953 | |
| 4954 | // "countByEnumeratingWithState:objects:count" |
| 4955 | IdentifierInfo *KeyIdents[] = { |
| 4956 | &CGM.getContext().Idents.get("countByEnumeratingWithState"), |
| 4957 | &CGM.getContext().Idents.get("objects"), |
| 4958 | &CGM.getContext().Idents.get("count") |
| 4959 | }; |
| 4960 | VTableDispatchMethods.insert( |
| 4961 | CGM.getContext().Selectors.getSelector(3, KeyIdents)); |
| 4962 | } |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 4963 | } |
Daniel Dunbar | fca18c1b4 | 2010-04-24 17:56:46 +0000 | [diff] [blame] | 4964 | |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 4965 | return VTableDispatchMethods.count(Sel); |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 4966 | } |
| 4967 | |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4968 | /// BuildClassRoTInitializer - generate meta-data for: |
| 4969 | /// struct _class_ro_t { |
| 4970 | /// uint32_t const flags; |
| 4971 | /// uint32_t const instanceStart; |
| 4972 | /// uint32_t const instanceSize; |
| 4973 | /// uint32_t const reserved; // only when building for 64bit targets |
| 4974 | /// const uint8_t * const ivarLayout; |
| 4975 | /// const char *const name; |
| 4976 | /// const struct _method_list_t * const baseMethods; |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4977 | /// const struct _protocol_list_t *const baseProtocols; |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4978 | /// const struct _ivar_list_t *const ivars; |
| 4979 | /// const uint8_t * const weakIvarLayout; |
| 4980 | /// const struct _prop_list_t * const properties; |
| 4981 | /// } |
| 4982 | /// |
| 4983 | llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4984 | unsigned flags, |
| 4985 | unsigned InstanceStart, |
| 4986 | unsigned InstanceSize, |
| 4987 | const ObjCImplementationDecl *ID) { |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4988 | std::string ClassName = ID->getNameAsString(); |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 4989 | llvm::Constant *Values[10]; // 11 for 64bit targets! |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4990 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4991 | if (CGM.getLangOpts().ObjCAutoRefCount) |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 4992 | flags |= NonFragileABI_Class_CompiledByARC; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4993 | |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 4994 | Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags); |
| 4995 | Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart); |
| 4996 | Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize); |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4997 | // FIXME. For 64bit targets add 0 here. |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 4998 | Values[ 3] = (flags & NonFragileABI_Class_Meta) |
| 4999 | ? GetIvarLayoutName(0, ObjCTypes) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5000 | : BuildIvarLayout(ID, true); |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5001 | Values[ 4] = GetClassName(ID->getIdentifier()); |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5002 | // const struct _method_list_t * const baseMethods; |
| 5003 | std::vector<llvm::Constant*> Methods; |
| 5004 | std::string MethodListName("\01l_OBJC_$_"); |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5005 | if (flags & NonFragileABI_Class_Meta) { |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5006 | MethodListName += "CLASS_METHODS_" + ID->getNameAsString(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5007 | for (ObjCImplementationDecl::classmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5008 | i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) { |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5009 | // Class methods should always be defined. |
| 5010 | Methods.push_back(GetMethodConstant(*i)); |
| 5011 | } |
| 5012 | } else { |
| 5013 | MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5014 | for (ObjCImplementationDecl::instmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5015 | i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) { |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5016 | // Instance methods should always be defined. |
| 5017 | Methods.push_back(GetMethodConstant(*i)); |
| 5018 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5019 | for (ObjCImplementationDecl::propimpl_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5020 | i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5021 | ObjCPropertyImplDecl *PID = *i; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5022 | |
Fariborz Jahanian | d27a820 | 2009-01-28 22:46:49 +0000 | [diff] [blame] | 5023 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){ |
| 5024 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5025 | |
Fariborz Jahanian | d27a820 | 2009-01-28 22:46:49 +0000 | [diff] [blame] | 5026 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) |
| 5027 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 5028 | Methods.push_back(C); |
| 5029 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) |
| 5030 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 5031 | Methods.push_back(C); |
| 5032 | } |
| 5033 | } |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5034 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5035 | Values[ 5] = EmitMethodList(MethodListName, |
| 5036 | "__DATA, __objc_const", Methods); |
| 5037 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5038 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
| 5039 | assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5040 | Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_" |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5041 | + OID->getName(), |
Ted Kremenek | 0ef508d | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 5042 | OID->all_referenced_protocol_begin(), |
| 5043 | OID->all_referenced_protocol_end()); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5044 | |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5045 | if (flags & NonFragileABI_Class_Meta) { |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5046 | Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy); |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5047 | Values[ 8] = GetIvarLayoutName(0, ObjCTypes); |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5048 | Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5049 | } else { |
| 5050 | Values[ 7] = EmitIvarList(ID); |
| 5051 | Values[ 8] = BuildIvarLayout(ID, false); |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5052 | Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(), |
| 5053 | ID, ID->getClassInterface(), ObjCTypes); |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5054 | } |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 5055 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy, |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5056 | Values); |
| 5057 | llvm::GlobalVariable *CLASS_RO_GV = |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5058 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false, |
| 5059 | llvm::GlobalValue::InternalLinkage, |
| 5060 | Init, |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5061 | (flags & NonFragileABI_Class_Meta) ? |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5062 | std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName : |
| 5063 | std::string("\01l_OBJC_CLASS_RO_$_")+ClassName); |
Fariborz Jahanian | c22f236 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 5064 | CLASS_RO_GV->setAlignment( |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 5065 | CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ClassRonfABITy)); |
Fariborz Jahanian | 40a4bcd | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 5066 | CLASS_RO_GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5067 | return CLASS_RO_GV; |
Fariborz Jahanian | 2612e14 | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5068 | |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5069 | } |
| 5070 | |
| 5071 | /// BuildClassMetaData - This routine defines that to-level meta-data |
| 5072 | /// for the given ClassName for: |
| 5073 | /// struct _class_t { |
| 5074 | /// struct _class_t *isa; |
| 5075 | /// struct _class_t * const superclass; |
| 5076 | /// void *cache; |
| 5077 | /// IMP *vtable; |
| 5078 | /// struct class_ro_t *ro; |
| 5079 | /// } |
| 5080 | /// |
Fariborz Jahanian | 4723fb7 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5081 | llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5082 | std::string &ClassName, |
| 5083 | llvm::Constant *IsAGV, |
| 5084 | llvm::Constant *SuperClassGV, |
| 5085 | llvm::Constant *ClassRoGV, |
| 5086 | bool HiddenVisibility) { |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 5087 | llvm::Constant *Values[] = { |
| 5088 | IsAGV, |
| 5089 | SuperClassGV, |
| 5090 | ObjCEmptyCacheVar, // &ObjCEmptyCacheVar |
| 5091 | ObjCEmptyVtableVar, // &ObjCEmptyVtableVar |
| 5092 | ClassRoGV // &CLASS_RO_GV |
| 5093 | }; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5094 | if (!Values[1]) |
| 5095 | Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5096 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy, |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5097 | Values); |
Daniel Dunbar | c6928bb | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5098 | llvm::GlobalVariable *GV = GetClassGlobal(ClassName); |
| 5099 | GV->setInitializer(Init); |
Fariborz Jahanian | 0408723 | 2009-01-31 01:07:39 +0000 | [diff] [blame] | 5100 | GV->setSection("__DATA, __objc_data"); |
Fariborz Jahanian | c22f236 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 5101 | GV->setAlignment( |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 5102 | CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ClassnfABITy)); |
Fariborz Jahanian | 8220825 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 5103 | if (HiddenVisibility) |
| 5104 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Fariborz Jahanian | 4723fb7 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5105 | return GV; |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5106 | } |
| 5107 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5108 | bool |
Fariborz Jahanian | a6bed83 | 2009-05-21 01:03:45 +0000 | [diff] [blame] | 5109 | CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const { |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5110 | return OD->getClassMethod(GetNullarySelector("load")) != 0; |
Daniel Dunbar | 9a017d7 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 5111 | } |
| 5112 | |
Daniel Dunbar | 96120237 | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 5113 | void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID, |
Daniel Dunbar | 554fd79 | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 5114 | uint32_t &InstanceStart, |
| 5115 | uint32_t &InstanceSize) { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5116 | const ASTRecordLayout &RL = |
Daniel Dunbar | 9252ee1 | 2009-05-04 21:26:30 +0000 | [diff] [blame] | 5117 | CGM.getContext().getASTObjCImplementationLayout(OID); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5118 | |
Daniel Dunbar | 9b042e0 | 2009-05-04 23:23:09 +0000 | [diff] [blame] | 5119 | // InstanceSize is really instance end. |
Ken Dyck | d5090c1 | 2011-02-11 02:20:09 +0000 | [diff] [blame] | 5120 | InstanceSize = RL.getDataSize().getQuantity(); |
Daniel Dunbar | 9b042e0 | 2009-05-04 23:23:09 +0000 | [diff] [blame] | 5121 | |
| 5122 | // If there are no fields, the start is the same as the end. |
| 5123 | if (!RL.getFieldCount()) |
| 5124 | InstanceStart = InstanceSize; |
| 5125 | else |
Ken Dyck | c5ca876 | 2011-04-14 00:43:09 +0000 | [diff] [blame] | 5126 | InstanceStart = RL.getFieldOffset(0) / CGM.getContext().getCharWidth(); |
Daniel Dunbar | 554fd79 | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 5127 | } |
| 5128 | |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5129 | void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) { |
| 5130 | std::string ClassName = ID->getNameAsString(); |
| 5131 | if (!ObjCEmptyCacheVar) { |
| 5132 | ObjCEmptyCacheVar = new llvm::GlobalVariable( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5133 | CGM.getModule(), |
| 5134 | ObjCTypes.CacheTy, |
| 5135 | false, |
| 5136 | llvm::GlobalValue::ExternalLinkage, |
| 5137 | 0, |
| 5138 | "_objc_empty_cache"); |
| 5139 | |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5140 | ObjCEmptyVtableVar = new llvm::GlobalVariable( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5141 | CGM.getModule(), |
| 5142 | ObjCTypes.ImpnfABITy, |
| 5143 | false, |
| 5144 | llvm::GlobalValue::ExternalLinkage, |
| 5145 | 0, |
| 5146 | "_objc_empty_vtable"); |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5147 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5148 | assert(ID->getClassInterface() && |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5149 | "CGObjCNonFragileABIMac::GenerateClass - class is 0"); |
Daniel Dunbar | e3f5cfc | 2009-04-20 20:18:54 +0000 | [diff] [blame] | 5150 | // FIXME: Is this correct (that meta class size is never computed)? |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5151 | uint32_t InstanceStart = |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 5152 | CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ClassnfABITy); |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5153 | uint32_t InstanceSize = InstanceStart; |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5154 | uint32_t flags = NonFragileABI_Class_Meta; |
Daniel Dunbar | 15894b7 | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5155 | std::string ObjCMetaClassName(getMetaclassSymbolPrefix()); |
| 5156 | std::string ObjCClassName(getClassSymbolPrefix()); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5157 | |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5158 | llvm::GlobalVariable *SuperClassGV, *IsAGV; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5159 | |
John McCall | 0d54a17 | 2012-10-17 04:53:31 +0000 | [diff] [blame^] | 5160 | // Build the flags for the metaclass. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5161 | bool classIsHidden = |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 5162 | ID->getClassInterface()->getVisibility() == HiddenVisibility; |
Fariborz Jahanian | 8220825 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 5163 | if (classIsHidden) |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5164 | flags |= NonFragileABI_Class_Hidden; |
John McCall | 0d54a17 | 2012-10-17 04:53:31 +0000 | [diff] [blame^] | 5165 | |
| 5166 | // FIXME: why is this flag set on the metaclass? |
| 5167 | // ObjC metaclasses have no fields and don't really get constructed. |
| 5168 | if (ID->hasNonZeroConstructors() || ID->hasDestructors()) { |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5169 | flags |= NonFragileABI_Class_HasCXXStructors; |
John McCall | 0d54a17 | 2012-10-17 04:53:31 +0000 | [diff] [blame^] | 5170 | if (!ID->hasNonZeroConstructors()) |
| 5171 | flags |= NonFragileABI_Class_HasCXXDestructorOnly; |
| 5172 | } |
| 5173 | |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5174 | if (!ID->getClassInterface()->getSuperClass()) { |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5175 | // class is root |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5176 | flags |= NonFragileABI_Class_Root; |
Daniel Dunbar | c6928bb | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5177 | SuperClassGV = GetClassGlobal(ObjCClassName + ClassName); |
Fariborz Jahanian | 899e7eb | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5178 | IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName); |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5179 | } else { |
Fariborz Jahanian | 4723fb7 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5180 | // Has a root. Current class is not a root. |
Fariborz Jahanian | 03b300b | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 5181 | const ObjCInterfaceDecl *Root = ID->getClassInterface(); |
| 5182 | while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) |
| 5183 | Root = Super; |
Fariborz Jahanian | 899e7eb | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5184 | IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString()); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5185 | if (Root->isWeakImported()) |
Fariborz Jahanian | a6227fd | 2009-12-01 18:25:24 +0000 | [diff] [blame] | 5186 | IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); |
Fariborz Jahanian | 03b300b | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 5187 | // work on super class metadata symbol. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5188 | std::string SuperClassName = |
Fariborz Jahanian | a6227fd | 2009-12-01 18:25:24 +0000 | [diff] [blame] | 5189 | ObjCMetaClassName + |
| 5190 | ID->getClassInterface()->getSuperClass()->getNameAsString(); |
Fariborz Jahanian | 899e7eb | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5191 | SuperClassGV = GetClassGlobal(SuperClassName); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5192 | if (ID->getClassInterface()->getSuperClass()->isWeakImported()) |
Fariborz Jahanian | 6726055 | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 5193 | SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5194 | } |
| 5195 | llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags, |
| 5196 | InstanceStart, |
| 5197 | InstanceSize,ID); |
Fariborz Jahanian | 4723fb7 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5198 | std::string TClassName = ObjCMetaClassName + ClassName; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5199 | llvm::GlobalVariable *MetaTClass = |
Fariborz Jahanian | 8220825 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 5200 | BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV, |
| 5201 | classIsHidden); |
Fariborz Jahanian | 6726055 | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 5202 | DefinedMetaClasses.push_back(MetaTClass); |
Daniel Dunbar | 15894b7 | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5203 | |
Fariborz Jahanian | 4723fb7 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5204 | // Metadata for the class |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5205 | flags = 0; |
Fariborz Jahanian | 8220825 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 5206 | if (classIsHidden) |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5207 | flags |= NonFragileABI_Class_Hidden; |
John McCall | 0d54a17 | 2012-10-17 04:53:31 +0000 | [diff] [blame^] | 5208 | |
| 5209 | if (ID->hasNonZeroConstructors() || ID->hasDestructors()) { |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5210 | flags |= NonFragileABI_Class_HasCXXStructors; |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5211 | |
John McCall | 0d54a17 | 2012-10-17 04:53:31 +0000 | [diff] [blame^] | 5212 | // Set a flag to enable a runtime optimization when a class has |
| 5213 | // fields that require destruction but which don't require |
| 5214 | // anything except zero-initialization during construction. This |
| 5215 | // is most notably true of __strong and __weak types, but you can |
| 5216 | // also imagine there being C++ types with non-trivial default |
| 5217 | // constructors that merely set all fields to null. |
| 5218 | if (!ID->hasNonZeroConstructors()) |
| 5219 | flags |= NonFragileABI_Class_HasCXXDestructorOnly; |
| 5220 | } |
| 5221 | |
Douglas Gregor | 78bd61f | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 5222 | if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface())) |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5223 | flags |= NonFragileABI_Class_Exception; |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5224 | |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5225 | if (!ID->getClassInterface()->getSuperClass()) { |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5226 | flags |= NonFragileABI_Class_Root; |
Fariborz Jahanian | 4723fb7 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5227 | SuperClassGV = 0; |
Chris Lattner | b433b27 | 2009-04-19 06:02:28 +0000 | [diff] [blame] | 5228 | } else { |
Fariborz Jahanian | 4723fb7 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5229 | // Has a root. Current class is not a root. |
Fariborz Jahanian | 03b300b | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 5230 | std::string RootClassName = |
Fariborz Jahanian | 4723fb7 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5231 | ID->getClassInterface()->getSuperClass()->getNameAsString(); |
Daniel Dunbar | c6928bb | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5232 | SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5233 | if (ID->getClassInterface()->getSuperClass()->isWeakImported()) |
Fariborz Jahanian | 6726055 | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 5234 | SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); |
Fariborz Jahanian | 4723fb7 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5235 | } |
Daniel Dunbar | 96120237 | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 5236 | GetClassSizeInfo(ID, InstanceStart, InstanceSize); |
Fariborz Jahanian | 4723fb7 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5237 | CLASS_RO_GV = BuildClassRoTInitializer(flags, |
Fariborz Jahanian | a887e63 | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 5238 | InstanceStart, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5239 | InstanceSize, |
Fariborz Jahanian | a887e63 | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 5240 | ID); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5241 | |
Fariborz Jahanian | 4723fb7 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5242 | TClassName = ObjCClassName + ClassName; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5243 | llvm::GlobalVariable *ClassMD = |
Fariborz Jahanian | 8220825 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 5244 | BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV, |
| 5245 | classIsHidden); |
Fariborz Jahanian | 279abd3 | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 5246 | DefinedClasses.push_back(ClassMD); |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5247 | |
Daniel Dunbar | 9a017d7 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 5248 | // Determine if this class is also "non-lazy". |
| 5249 | if (ImplementationIsNonLazy(ID)) |
| 5250 | DefinedNonLazyClasses.push_back(ClassMD); |
| 5251 | |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5252 | // Force the definition of the EHType if necessary. |
John McCall | ef19dbb | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5253 | if (flags & NonFragileABI_Class_Exception) |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5254 | GetInterfaceEHType(ID->getClassInterface(), true); |
Fariborz Jahanian | c057794 | 2011-04-22 22:02:28 +0000 | [diff] [blame] | 5255 | // Make sure method definition entries are all clear for next implementation. |
| 5256 | MethodDefinitions.clear(); |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5257 | } |
| 5258 | |
Fariborz Jahanian | 097feda | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5259 | /// GenerateProtocolRef - This routine is called to generate code for |
| 5260 | /// a protocol reference expression; as in: |
| 5261 | /// @code |
| 5262 | /// @protocol(Proto1); |
| 5263 | /// @endcode |
| 5264 | /// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1 |
| 5265 | /// which will hold address of the protocol meta-data. |
| 5266 | /// |
| 5267 | llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5268 | const ObjCProtocolDecl *PD) { |
| 5269 | |
Fariborz Jahanian | 464423d | 2009-04-10 18:47:34 +0000 | [diff] [blame] | 5270 | // This routine is called for @protocol only. So, we must build definition |
| 5271 | // of protocol's meta-data (not a reference to it!) |
| 5272 | // |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5273 | llvm::Constant *Init = |
| 5274 | llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD), |
Douglas Gregor | 020de32 | 2012-01-17 18:36:30 +0000 | [diff] [blame] | 5275 | ObjCTypes.getExternalProtocolPtrTy()); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5276 | |
Fariborz Jahanian | 097feda | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5277 | std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_"); |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 5278 | ProtocolName += PD->getName(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5279 | |
Fariborz Jahanian | 097feda | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5280 | llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName); |
| 5281 | if (PTGV) |
Benjamin Kramer | 76399eb | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 5282 | return Builder.CreateLoad(PTGV); |
Fariborz Jahanian | 097feda | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5283 | PTGV = new llvm::GlobalVariable( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5284 | CGM.getModule(), |
| 5285 | Init->getType(), false, |
| 5286 | llvm::GlobalValue::WeakAnyLinkage, |
| 5287 | Init, |
| 5288 | ProtocolName); |
Fariborz Jahanian | 097feda | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5289 | PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip"); |
| 5290 | PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5291 | CGM.AddUsedGlobal(PTGV); |
Benjamin Kramer | 76399eb | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 5292 | return Builder.CreateLoad(PTGV); |
Fariborz Jahanian | 097feda | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5293 | } |
| 5294 | |
Fariborz Jahanian | 0c8d060 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5295 | /// GenerateCategory - Build metadata for a category implementation. |
| 5296 | /// struct _category_t { |
| 5297 | /// const char * const name; |
| 5298 | /// struct _class_t *const cls; |
| 5299 | /// const struct _method_list_t * const instance_methods; |
| 5300 | /// const struct _method_list_t * const class_methods; |
| 5301 | /// const struct _protocol_list_t * const protocols; |
| 5302 | /// const struct _prop_list_t * const properties; |
| 5303 | /// } |
| 5304 | /// |
Daniel Dunbar | 9a017d7 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 5305 | void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) { |
Fariborz Jahanian | 0c8d060 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5306 | const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); |
Fariborz Jahanian | 2612e14 | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5307 | const char *Prefix = "\01l_OBJC_$_CATEGORY_"; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5308 | std::string ExtCatName(Prefix + Interface->getNameAsString()+ |
| 5309 | "_$_" + OCD->getNameAsString()); |
| 5310 | std::string ExtClassName(getClassSymbolPrefix() + |
Daniel Dunbar | 15894b7 | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5311 | Interface->getNameAsString()); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5312 | |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 5313 | llvm::Constant *Values[6]; |
Fariborz Jahanian | 0c8d060 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5314 | Values[0] = GetClassName(OCD->getIdentifier()); |
| 5315 | // meta-class entry symbol |
Daniel Dunbar | c6928bb | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5316 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5317 | if (Interface->isWeakImported()) |
Fariborz Jahanian | 3ad8dcf | 2009-11-17 22:02:21 +0000 | [diff] [blame] | 5318 | ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); |
| 5319 | |
Fariborz Jahanian | 0c8d060 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5320 | Values[1] = ClassGV; |
Fariborz Jahanian | 2612e14 | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5321 | std::vector<llvm::Constant*> Methods; |
| 5322 | std::string MethodListName(Prefix); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5323 | MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() + |
Fariborz Jahanian | 2612e14 | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5324 | "_$_" + OCD->getNameAsString(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5325 | |
| 5326 | for (ObjCCategoryImplDecl::instmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5327 | i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) { |
Fariborz Jahanian | 2612e14 | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5328 | // Instance methods should always be defined. |
| 5329 | Methods.push_back(GetMethodConstant(*i)); |
| 5330 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5331 | |
| 5332 | Values[2] = EmitMethodList(MethodListName, |
| 5333 | "__DATA, __objc_const", |
Fariborz Jahanian | 2612e14 | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5334 | Methods); |
| 5335 | |
| 5336 | MethodListName = Prefix; |
| 5337 | MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" + |
| 5338 | OCD->getNameAsString(); |
| 5339 | Methods.clear(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5340 | for (ObjCCategoryImplDecl::classmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5341 | i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) { |
Fariborz Jahanian | 2612e14 | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5342 | // Class methods should always be defined. |
| 5343 | Methods.push_back(GetMethodConstant(*i)); |
| 5344 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5345 | |
| 5346 | Values[3] = EmitMethodList(MethodListName, |
| 5347 | "__DATA, __objc_const", |
Fariborz Jahanian | 2612e14 | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5348 | Methods); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5349 | const ObjCCategoryDecl *Category = |
Fariborz Jahanian | 066347e | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 5350 | Interface->FindCategoryDeclaration(OCD->getIdentifier()); |
Fariborz Jahanian | d8fc105 | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 5351 | if (Category) { |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 5352 | SmallString<256> ExtName; |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5353 | llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_" |
| 5354 | << OCD->getName(); |
Fariborz Jahanian | d8fc105 | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 5355 | Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_" |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5356 | + Interface->getName() + "_$_" |
| 5357 | + Category->getName(), |
Fariborz Jahanian | d8fc105 | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 5358 | Category->protocol_begin(), |
| 5359 | Category->protocol_end()); |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5360 | Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(), |
| 5361 | OCD, Category, ObjCTypes); |
Mike Stump | 658fe02 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 5362 | } else { |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5363 | Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy); |
| 5364 | Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
Fariborz Jahanian | d8fc105 | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 5365 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5366 | |
| 5367 | llvm::Constant *Init = |
| 5368 | llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy, |
Fariborz Jahanian | 0c8d060 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5369 | Values); |
| 5370 | llvm::GlobalVariable *GCATV |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5371 | = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy, |
Fariborz Jahanian | 0c8d060 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5372 | false, |
| 5373 | llvm::GlobalValue::InternalLinkage, |
| 5374 | Init, |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5375 | ExtCatName); |
Fariborz Jahanian | c22f236 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 5376 | GCATV->setAlignment( |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 5377 | CGM.getDataLayout().getABITypeAlignment(ObjCTypes.CategorynfABITy)); |
Fariborz Jahanian | 40a4bcd | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 5378 | GCATV->setSection("__DATA, __objc_const"); |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5379 | CGM.AddUsedGlobal(GCATV); |
Fariborz Jahanian | 0c8d060 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5380 | DefinedCategories.push_back(GCATV); |
Daniel Dunbar | 9a017d7 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 5381 | |
| 5382 | // Determine if this category is also "non-lazy". |
| 5383 | if (ImplementationIsNonLazy(OCD)) |
| 5384 | DefinedNonLazyCategories.push_back(GCATV); |
Fariborz Jahanian | c057794 | 2011-04-22 22:02:28 +0000 | [diff] [blame] | 5385 | // method definition entries must be clear for next implementation. |
| 5386 | MethodDefinitions.clear(); |
Fariborz Jahanian | 0c8d060 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5387 | } |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5388 | |
| 5389 | /// GetMethodConstant - Return a struct objc_method constant for the |
| 5390 | /// given method if it has been defined. The result is null if the |
| 5391 | /// method has not been defined. The return value has type MethodPtrTy. |
| 5392 | llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5393 | const ObjCMethodDecl *MD) { |
Argyrios Kyrtzidis | 13257c5 | 2010-08-09 10:54:20 +0000 | [diff] [blame] | 5394 | llvm::Function *Fn = GetMethodDefinition(MD); |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5395 | if (!Fn) |
| 5396 | return 0; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5397 | |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 5398 | llvm::Constant *Method[] = { |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 5399 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 5400 | ObjCTypes.SelectorPtrTy), |
| 5401 | GetMethodVarType(MD), |
| 5402 | llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy) |
| 5403 | }; |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 5404 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method); |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5405 | } |
| 5406 | |
| 5407 | /// EmitMethodList - Build meta-data for method declarations |
| 5408 | /// struct _method_list_t { |
| 5409 | /// uint32_t entsize; // sizeof(struct _objc_method) |
| 5410 | /// uint32_t method_count; |
| 5411 | /// struct _objc_method method_list[method_count]; |
| 5412 | /// } |
| 5413 | /// |
Bill Wendling | cb5b5ff | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 5414 | llvm::Constant * |
| 5415 | CGObjCNonFragileABIMac::EmitMethodList(Twine Name, |
| 5416 | const char *Section, |
| 5417 | ArrayRef<llvm::Constant*> Methods) { |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5418 | // Return null for empty list. |
| 5419 | if (Methods.empty()) |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5420 | return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5421 | |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 5422 | llvm::Constant *Values[3]; |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5423 | // sizeof(struct _objc_method) |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 5424 | unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.MethodTy); |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 5425 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5426 | // method_count |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 5427 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5428 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy, |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5429 | Methods.size()); |
Owen Anderson | 47034e1 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 5430 | Values[2] = llvm::ConstantArray::get(AT, Methods); |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 5431 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5432 | |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5433 | llvm::GlobalVariable *GV = |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5434 | new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 5435 | llvm::GlobalValue::InternalLinkage, Init, Name); |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 5436 | GV->setAlignment(CGM.getDataLayout().getABITypeAlignment(Init->getType())); |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5437 | GV->setSection(Section); |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5438 | CGM.AddUsedGlobal(GV); |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 5439 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListnfABIPtrTy); |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5440 | } |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5441 | |
Fariborz Jahanian | 4e7ae06 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 5442 | /// ObjCIvarOffsetVariable - Returns the ivar offset variable for |
| 5443 | /// the given ivar. |
Daniel Dunbar | 8c7f981 | 2010-04-02 21:14:02 +0000 | [diff] [blame] | 5444 | llvm::GlobalVariable * |
| 5445 | CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID, |
| 5446 | const ObjCIvarDecl *Ivar) { |
| 5447 | const ObjCInterfaceDecl *Container = Ivar->getContainingInterface(); |
Daniel Dunbar | 3f86b9c | 2009-05-05 00:36:57 +0000 | [diff] [blame] | 5448 | std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() + |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 5449 | '.' + Ivar->getNameAsString(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5450 | llvm::GlobalVariable *IvarOffsetGV = |
Fariborz Jahanian | 4e7ae06 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 5451 | CGM.getModule().getGlobalVariable(Name); |
| 5452 | if (!IvarOffsetGV) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5453 | IvarOffsetGV = |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5454 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy, |
Fariborz Jahanian | 4e7ae06 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 5455 | false, |
| 5456 | llvm::GlobalValue::ExternalLinkage, |
| 5457 | 0, |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5458 | Name); |
Fariborz Jahanian | 4e7ae06 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 5459 | return IvarOffsetGV; |
| 5460 | } |
| 5461 | |
Daniel Dunbar | 8c7f981 | 2010-04-02 21:14:02 +0000 | [diff] [blame] | 5462 | llvm::Constant * |
| 5463 | CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID, |
| 5464 | const ObjCIvarDecl *Ivar, |
| 5465 | unsigned long int Offset) { |
Daniel Dunbar | bf90b33 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 5466 | llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5467 | IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy, |
Daniel Dunbar | bf90b33 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 5468 | Offset)); |
Fariborz Jahanian | c22f236 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 5469 | IvarOffsetGV->setAlignment( |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 5470 | CGM.getDataLayout().getABITypeAlignment(ObjCTypes.LongTy)); |
Daniel Dunbar | bf90b33 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 5471 | |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 5472 | // FIXME: This matches gcc, but shouldn't the visibility be set on the use as |
| 5473 | // well (i.e., in ObjCIvarOffsetVariable). |
Daniel Dunbar | bf90b33 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 5474 | if (Ivar->getAccessControl() == ObjCIvarDecl::Private || |
| 5475 | Ivar->getAccessControl() == ObjCIvarDecl::Package || |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 5476 | ID->getVisibility() == HiddenVisibility) |
Fariborz Jahanian | 3d3426f | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 5477 | IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | f5f359f | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 5478 | else |
Fariborz Jahanian | bc3c77b | 2009-04-06 18:30:00 +0000 | [diff] [blame] | 5479 | IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility); |
Bill Wendling | f7d4598 | 2011-05-04 21:37:25 +0000 | [diff] [blame] | 5480 | IvarOffsetGV->setSection("__DATA, __objc_ivar"); |
Fariborz Jahanian | c88a70d | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 5481 | return IvarOffsetGV; |
Fariborz Jahanian | 40a4bcd | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 5482 | } |
| 5483 | |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5484 | /// EmitIvarList - Emit the ivar list for the given |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5485 | /// implementation. The return value has type |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5486 | /// IvarListnfABIPtrTy. |
| 5487 | /// struct _ivar_t { |
| 5488 | /// unsigned long int *offset; // pointer to ivar offset location |
| 5489 | /// char *name; |
| 5490 | /// char *type; |
| 5491 | /// uint32_t alignment; |
| 5492 | /// uint32_t size; |
| 5493 | /// } |
| 5494 | /// struct _ivar_list_t { |
| 5495 | /// uint32 entsize; // sizeof(struct _ivar_t) |
| 5496 | /// uint32 count; |
| 5497 | /// struct _iver_t list[count]; |
| 5498 | /// } |
| 5499 | /// |
Daniel Dunbar | f5c1846 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 5500 | |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5501 | llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5502 | const ObjCImplementationDecl *ID) { |
| 5503 | |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 5504 | std::vector<llvm::Constant*> Ivars; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5505 | |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 5506 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5507 | assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5508 | |
Fariborz Jahanian | 40a4bcd | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 5509 | // FIXME. Consolidate this with similar code in GenerateClass. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5510 | |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 5511 | for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin(); |
Fariborz Jahanian | b26d578 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 5512 | IVD; IVD = IVD->getNextIvar()) { |
Fariborz Jahanian | 7c80959 | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 5513 | // Ignore unnamed bit-fields. |
| 5514 | if (!IVD->getDeclName()) |
| 5515 | continue; |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 5516 | llvm::Constant *Ivar[5]; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5517 | Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD, |
Daniel Dunbar | 96120237 | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 5518 | ComputeIvarBaseOffset(CGM, ID, IVD)); |
Daniel Dunbar | 725dc2c | 2009-04-22 08:22:17 +0000 | [diff] [blame] | 5519 | Ivar[1] = GetMethodVarName(IVD->getIdentifier()); |
| 5520 | Ivar[2] = GetMethodVarType(IVD); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 5521 | llvm::Type *FieldTy = |
Daniel Dunbar | 725dc2c | 2009-04-22 08:22:17 +0000 | [diff] [blame] | 5522 | CGM.getTypes().ConvertTypeForMem(IVD->getType()); |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 5523 | unsigned Size = CGM.getDataLayout().getTypeAllocSize(FieldTy); |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5524 | unsigned Align = CGM.getContext().getPreferredTypeAlign( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5525 | IVD->getType().getTypePtr()) >> 3; |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5526 | Align = llvm::Log2_32(Align); |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 5527 | Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align); |
Daniel Dunbar | ae03226 | 2009-04-20 00:33:43 +0000 | [diff] [blame] | 5528 | // NOTE. Size of a bitfield does not match gcc's, because of the |
| 5529 | // way bitfields are treated special in each. But I am told that |
| 5530 | // 'size' for bitfield ivars is ignored by the runtime so it does |
| 5531 | // not matter. If it matters, there is enough info to get the |
| 5532 | // bitfield right! |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 5533 | Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 5534 | Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar)); |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5535 | } |
| 5536 | // Return null for empty list. |
| 5537 | if (Ivars.empty()) |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5538 | return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy); |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 5539 | |
| 5540 | llvm::Constant *Values[3]; |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 5541 | unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.IvarnfABITy); |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 5542 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 5543 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5544 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy, |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5545 | Ivars.size()); |
Owen Anderson | 47034e1 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 5546 | Values[2] = llvm::ConstantArray::get(AT, Ivars); |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 5547 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5548 | const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_"; |
| 5549 | llvm::GlobalVariable *GV = |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5550 | new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5551 | llvm::GlobalValue::InternalLinkage, |
| 5552 | Init, |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5553 | Prefix + OID->getName()); |
Fariborz Jahanian | c22f236 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 5554 | GV->setAlignment( |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 5555 | CGM.getDataLayout().getABITypeAlignment(Init->getType())); |
Fariborz Jahanian | 40a4bcd | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 5556 | GV->setSection("__DATA, __objc_const"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5557 | |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5558 | CGM.AddUsedGlobal(GV); |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 5559 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy); |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5560 | } |
| 5561 | |
| 5562 | llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5563 | const ObjCProtocolDecl *PD) { |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5564 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5565 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5566 | if (!Entry) { |
| 5567 | // We use the initializer as a marker of whether this is a forward |
| 5568 | // reference or not. At module finalization we add the empty |
| 5569 | // contents for protocols which were referenced but never defined. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5570 | Entry = |
| 5571 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false, |
| 5572 | llvm::GlobalValue::ExternalLinkage, |
| 5573 | 0, |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5574 | "\01l_OBJC_PROTOCOL_$_" + PD->getName()); |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5575 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5576 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5577 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5578 | return Entry; |
| 5579 | } |
| 5580 | |
| 5581 | /// GetOrEmitProtocol - Generate the protocol meta-data: |
| 5582 | /// @code |
| 5583 | /// struct _protocol_t { |
| 5584 | /// id isa; // NULL |
| 5585 | /// const char * const protocol_name; |
| 5586 | /// const struct _protocol_list_t * protocol_list; // super protocols |
| 5587 | /// const struct method_list_t * const instance_methods; |
| 5588 | /// const struct method_list_t * const class_methods; |
| 5589 | /// const struct method_list_t *optionalInstanceMethods; |
| 5590 | /// const struct method_list_t *optionalClassMethods; |
| 5591 | /// const struct _prop_list_t * properties; |
| 5592 | /// const uint32_t size; // sizeof(struct _protocol_t) |
| 5593 | /// const uint32_t flags; // = 0 |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5594 | /// const char ** extendedMethodTypes; |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5595 | /// } |
| 5596 | /// @endcode |
| 5597 | /// |
| 5598 | |
| 5599 | llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5600 | const ObjCProtocolDecl *PD) { |
John McCall | f9582a7 | 2012-03-30 21:29:05 +0000 | [diff] [blame] | 5601 | llvm::GlobalVariable *Entry = Protocols[PD->getIdentifier()]; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5602 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5603 | // Early exit if a defining object has already been generated. |
| 5604 | if (Entry && Entry->hasInitializer()) |
| 5605 | return Entry; |
| 5606 | |
Douglas Gregor | a715bff | 2012-01-01 19:51:50 +0000 | [diff] [blame] | 5607 | // Use the protocol definition, if there is one. |
| 5608 | if (const ObjCProtocolDecl *Def = PD->getDefinition()) |
| 5609 | PD = Def; |
| 5610 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5611 | // Construct method lists. |
| 5612 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 5613 | std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5614 | std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5615 | for (ObjCProtocolDecl::instmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5616 | i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) { |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5617 | ObjCMethodDecl *MD = *i; |
Fariborz Jahanian | d9c28b8 | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 5618 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 5619 | if (!C) |
| 5620 | return GetOrEmitProtocolRef(PD); |
| 5621 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5622 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 5623 | OptInstanceMethods.push_back(C); |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5624 | OptMethodTypesExt.push_back(GetMethodVarType(MD, true)); |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5625 | } else { |
| 5626 | InstanceMethods.push_back(C); |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5627 | MethodTypesExt.push_back(GetMethodVarType(MD, true)); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5628 | } |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5629 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5630 | |
| 5631 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5632 | i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) { |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5633 | ObjCMethodDecl *MD = *i; |
Fariborz Jahanian | d9c28b8 | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 5634 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 5635 | if (!C) |
| 5636 | return GetOrEmitProtocolRef(PD); |
| 5637 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5638 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 5639 | OptClassMethods.push_back(C); |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5640 | OptMethodTypesExt.push_back(GetMethodVarType(MD, true)); |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5641 | } else { |
| 5642 | ClassMethods.push_back(C); |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5643 | MethodTypesExt.push_back(GetMethodVarType(MD, true)); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5644 | } |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5645 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5646 | |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5647 | MethodTypesExt.insert(MethodTypesExt.end(), |
| 5648 | OptMethodTypesExt.begin(), OptMethodTypesExt.end()); |
| 5649 | |
| 5650 | llvm::Constant *Values[11]; |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5651 | // isa is NULL |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5652 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy); |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5653 | Values[1] = GetClassName(PD->getIdentifier()); |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5654 | Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(), |
| 5655 | PD->protocol_begin(), |
| 5656 | PD->protocol_end()); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5657 | |
Fariborz Jahanian | d9c28b8 | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 5658 | Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_" |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5659 | + PD->getName(), |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5660 | "__DATA, __objc_const", |
| 5661 | InstanceMethods); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5662 | Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_" |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5663 | + PD->getName(), |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5664 | "__DATA, __objc_const", |
| 5665 | ClassMethods); |
Fariborz Jahanian | d9c28b8 | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 5666 | Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_" |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5667 | + PD->getName(), |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5668 | "__DATA, __objc_const", |
| 5669 | OptInstanceMethods); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5670 | Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_" |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5671 | + PD->getName(), |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5672 | "__DATA, __objc_const", |
| 5673 | OptClassMethods); |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5674 | Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(), |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5675 | 0, PD, ObjCTypes); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5676 | uint32_t Size = |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 5677 | CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ProtocolnfABITy); |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 5678 | Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5679 | Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy); |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5680 | Values[10] = EmitProtocolMethodTypes("\01l_OBJC_$_PROTOCOL_METHOD_TYPES_" |
| 5681 | + PD->getName(), |
| 5682 | MethodTypesExt, ObjCTypes); |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 5683 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy, |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5684 | Values); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5685 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5686 | if (Entry) { |
| 5687 | // Already created, fix the linkage and update the initializer. |
Mike Stump | a6ca334 | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 5688 | Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage); |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5689 | Entry->setInitializer(Init); |
| 5690 | } else { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5691 | Entry = |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5692 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, |
| 5693 | false, llvm::GlobalValue::WeakAnyLinkage, Init, |
| 5694 | "\01l_OBJC_PROTOCOL_$_" + PD->getName()); |
Fariborz Jahanian | c22f236 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 5695 | Entry->setAlignment( |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 5696 | CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ProtocolnfABITy)); |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5697 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
John McCall | f9582a7 | 2012-03-30 21:29:05 +0000 | [diff] [blame] | 5698 | |
| 5699 | Protocols[PD->getIdentifier()] = Entry; |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5700 | } |
Fariborz Jahanian | 61cd4b5 | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 5701 | Entry->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5702 | CGM.AddUsedGlobal(Entry); |
| 5703 | |
Fariborz Jahanian | 61cd4b5 | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 5704 | // Use this protocol meta-data to build protocol list table in section |
| 5705 | // __DATA, __objc_protolist |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5706 | llvm::GlobalVariable *PTGV = |
| 5707 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy, |
| 5708 | false, llvm::GlobalValue::WeakAnyLinkage, Entry, |
| 5709 | "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName()); |
Fariborz Jahanian | c22f236 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 5710 | PTGV->setAlignment( |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 5711 | CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy)); |
Daniel Dunbar | b25452a | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 5712 | PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip"); |
Fariborz Jahanian | 61cd4b5 | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 5713 | PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5714 | CGM.AddUsedGlobal(PTGV); |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5715 | return Entry; |
| 5716 | } |
| 5717 | |
| 5718 | /// EmitProtocolList - Generate protocol list meta-data: |
| 5719 | /// @code |
| 5720 | /// struct _protocol_list_t { |
| 5721 | /// long protocol_count; // Note, this is 32/64 bit |
| 5722 | /// struct _protocol_t[protocol_count]; |
| 5723 | /// } |
| 5724 | /// @endcode |
| 5725 | /// |
| 5726 | llvm::Constant * |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5727 | CGObjCNonFragileABIMac::EmitProtocolList(Twine Name, |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5728 | ObjCProtocolDecl::protocol_iterator begin, |
| 5729 | ObjCProtocolDecl::protocol_iterator end) { |
Bill Wendling | a515b58 | 2012-02-09 22:16:49 +0000 | [diff] [blame] | 5730 | llvm::SmallVector<llvm::Constant*, 16> ProtocolRefs; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5731 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5732 | // Just return null for empty protocol lists |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5733 | if (begin == end) |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5734 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5735 | |
Daniel Dunbar | 8de90f0 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 5736 | // FIXME: We shouldn't need to do this lookup here, should we? |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 5737 | SmallString<256> TmpName; |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5738 | Name.toVector(TmpName); |
| 5739 | llvm::GlobalVariable *GV = |
| 5740 | CGM.getModule().getGlobalVariable(TmpName.str(), true); |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5741 | if (GV) |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5742 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5743 | |
Daniel Dunbar | 8de90f0 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 5744 | for (; begin != end; ++begin) |
| 5745 | ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented??? |
| 5746 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5747 | // This list is null terminated. |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5748 | ProtocolRefs.push_back(llvm::Constant::getNullValue( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5749 | ObjCTypes.ProtocolnfABIPtrTy)); |
| 5750 | |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 5751 | llvm::Constant *Values[2]; |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 5752 | Values[0] = |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 5753 | llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5754 | Values[1] = |
Bill Wendling | a515b58 | 2012-02-09 22:16:49 +0000 | [diff] [blame] | 5755 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy, |
| 5756 | ProtocolRefs.size()), |
| 5757 | ProtocolRefs); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5758 | |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 5759 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5760 | GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5761 | llvm::GlobalValue::InternalLinkage, |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 5762 | Init, Name); |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5763 | GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | c22f236 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 5764 | GV->setAlignment( |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 5765 | CGM.getDataLayout().getABITypeAlignment(Init->getType())); |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5766 | CGM.AddUsedGlobal(GV); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5767 | return llvm::ConstantExpr::getBitCast(GV, |
Daniel Dunbar | 8de90f0 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 5768 | ObjCTypes.ProtocolListnfABIPtrTy); |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5769 | } |
| 5770 | |
Fariborz Jahanian | d9c28b8 | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 5771 | /// GetMethodDescriptionConstant - This routine build following meta-data: |
| 5772 | /// struct _objc_method { |
| 5773 | /// SEL _cmd; |
| 5774 | /// char *method_type; |
| 5775 | /// char *_imp; |
| 5776 | /// } |
| 5777 | |
| 5778 | llvm::Constant * |
| 5779 | CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 5780 | llvm::Constant *Desc[3]; |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 5781 | Desc[0] = |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5782 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 5783 | ObjCTypes.SelectorPtrTy); |
Fariborz Jahanian | d9c28b8 | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 5784 | Desc[1] = GetMethodVarType(MD); |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 5785 | if (!Desc[1]) |
| 5786 | return 0; |
| 5787 | |
Fariborz Jahanian | 097feda | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5788 | // Protocol methods have no implementation. So, this entry is always NULL. |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5789 | Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 5790 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc); |
Fariborz Jahanian | d9c28b8 | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 5791 | } |
Fariborz Jahanian | c88a70d | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 5792 | |
| 5793 | /// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference. |
| 5794 | /// This code gen. amounts to generating code for: |
| 5795 | /// @code |
| 5796 | /// (type *)((char *)base + _OBJC_IVAR_$_.ivar; |
| 5797 | /// @encode |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5798 | /// |
Fariborz Jahanian | 712bfa6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 5799 | LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar( |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 5800 | CodeGen::CodeGenFunction &CGF, |
| 5801 | QualType ObjectTy, |
| 5802 | llvm::Value *BaseValue, |
| 5803 | const ObjCIvarDecl *Ivar, |
| 5804 | unsigned CVRQualifiers) { |
| 5805 | ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface(); |
Fariborz Jahanian | caabf1b | 2012-02-20 22:42:22 +0000 | [diff] [blame] | 5806 | llvm::Value *Offset = EmitIvarOffset(CGF, ID, Ivar); |
| 5807 | if (llvm::LoadInst *LI = dyn_cast<llvm::LoadInst>(Offset)) |
| 5808 | LI->setMetadata(CGM.getModule().getMDKindID("invariant.load"), |
| 5809 | llvm::MDNode::get(VMContext, |
| 5810 | ArrayRef<llvm::Value*>())); |
Daniel Dunbar | 9fd114d | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 5811 | return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers, |
Fariborz Jahanian | caabf1b | 2012-02-20 22:42:22 +0000 | [diff] [blame] | 5812 | Offset); |
Fariborz Jahanian | c88a70d | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 5813 | } |
| 5814 | |
Fariborz Jahanian | 21fc74c | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 5815 | llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5816 | CodeGen::CodeGenFunction &CGF, |
| 5817 | const ObjCInterfaceDecl *Interface, |
| 5818 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | c76493a | 2009-11-29 21:23:36 +0000 | [diff] [blame] | 5819 | return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar"); |
Fariborz Jahanian | 21fc74c | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 5820 | } |
| 5821 | |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5822 | static void appendSelectorForMessageRefTable(std::string &buffer, |
| 5823 | Selector selector) { |
| 5824 | if (selector.isUnarySelector()) { |
| 5825 | buffer += selector.getNameForSlot(0); |
| 5826 | return; |
| 5827 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5828 | |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5829 | for (unsigned i = 0, e = selector.getNumArgs(); i != e; ++i) { |
| 5830 | buffer += selector.getNameForSlot(i); |
| 5831 | buffer += '_'; |
| 5832 | } |
| 5833 | } |
| 5834 | |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5835 | /// Emit a "v-table" message send. We emit a weak hidden-visibility |
| 5836 | /// struct, initially containing the selector pointer and a pointer to |
| 5837 | /// a "fixup" variant of the appropriate objc_msgSend. To call, we |
| 5838 | /// load and call the function pointer, passing the address of the |
| 5839 | /// struct as the second parameter. The runtime determines whether |
| 5840 | /// the selector is currently emitted using vtable dispatch; if so, it |
| 5841 | /// substitutes a stub function which simply tail-calls through the |
| 5842 | /// appropriate vtable slot, and if not, it substitues a stub function |
| 5843 | /// which tail-calls objc_msgSend. Both stubs adjust the selector |
| 5844 | /// argument to correctly point to the selector. |
| 5845 | RValue |
| 5846 | CGObjCNonFragileABIMac::EmitVTableMessageSend(CodeGenFunction &CGF, |
| 5847 | ReturnValueSlot returnSlot, |
| 5848 | QualType resultType, |
| 5849 | Selector selector, |
| 5850 | llvm::Value *arg0, |
| 5851 | QualType arg0Type, |
| 5852 | bool isSuper, |
| 5853 | const CallArgList &formalArgs, |
| 5854 | const ObjCMethodDecl *method) { |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5855 | // Compute the actual arguments. |
| 5856 | CallArgList args; |
| 5857 | |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5858 | // First argument: the receiver / super-call structure. |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5859 | if (!isSuper) |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5860 | arg0 = CGF.Builder.CreateBitCast(arg0, ObjCTypes.ObjectPtrTy); |
| 5861 | args.add(RValue::get(arg0), arg0Type); |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5862 | |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5863 | // Second argument: a pointer to the message ref structure. Leave |
| 5864 | // the actual argument value blank for now. |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5865 | args.add(RValue::get(0), ObjCTypes.MessageRefCPtrTy); |
| 5866 | |
| 5867 | args.insert(args.end(), formalArgs.begin(), formalArgs.end()); |
| 5868 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 5869 | MessageSendInfo MSI = getMessageSendInfo(method, resultType, args); |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5870 | |
John McCall | 5880fb8 | 2011-05-14 21:12:11 +0000 | [diff] [blame] | 5871 | NullReturnState nullReturn; |
| 5872 | |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5873 | // Find the function to call and the mangled name for the message |
| 5874 | // ref structure. Using a different mangled name wouldn't actually |
| 5875 | // be a problem; it would just be a waste. |
| 5876 | // |
| 5877 | // The runtime currently never uses vtable dispatch for anything |
| 5878 | // except normal, non-super message-sends. |
| 5879 | // FIXME: don't use this for that. |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5880 | llvm::Constant *fn = 0; |
| 5881 | std::string messageRefName("\01l_"); |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 5882 | if (CGM.ReturnTypeUsesSRet(MSI.CallInfo)) { |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5883 | if (isSuper) { |
| 5884 | fn = ObjCTypes.getMessageSendSuper2StretFixupFn(); |
| 5885 | messageRefName += "objc_msgSendSuper2_stret_fixup"; |
Chris Lattner | 396639d | 2010-08-18 16:09:06 +0000 | [diff] [blame] | 5886 | } else { |
John McCall | 5880fb8 | 2011-05-14 21:12:11 +0000 | [diff] [blame] | 5887 | nullReturn.init(CGF, arg0); |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5888 | fn = ObjCTypes.getMessageSendStretFixupFn(); |
| 5889 | messageRefName += "objc_msgSend_stret_fixup"; |
Chris Lattner | 396639d | 2010-08-18 16:09:06 +0000 | [diff] [blame] | 5890 | } |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5891 | } else if (!isSuper && CGM.ReturnTypeUsesFPRet(resultType)) { |
| 5892 | fn = ObjCTypes.getMessageSendFpretFixupFn(); |
| 5893 | messageRefName += "objc_msgSend_fpret_fixup"; |
Mike Stump | 658fe02 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 5894 | } else { |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5895 | if (isSuper) { |
| 5896 | fn = ObjCTypes.getMessageSendSuper2FixupFn(); |
| 5897 | messageRefName += "objc_msgSendSuper2_fixup"; |
Chris Lattner | 396639d | 2010-08-18 16:09:06 +0000 | [diff] [blame] | 5898 | } else { |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5899 | fn = ObjCTypes.getMessageSendFixupFn(); |
| 5900 | messageRefName += "objc_msgSend_fixup"; |
Chris Lattner | 396639d | 2010-08-18 16:09:06 +0000 | [diff] [blame] | 5901 | } |
Fariborz Jahanian | e4dc35d | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5902 | } |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5903 | assert(fn && "CGObjCNonFragileABIMac::EmitMessageSend"); |
| 5904 | messageRefName += '_'; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5905 | |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5906 | // Append the selector name, except use underscores anywhere we |
| 5907 | // would have used colons. |
| 5908 | appendSelectorForMessageRefTable(messageRefName, selector); |
| 5909 | |
| 5910 | llvm::GlobalVariable *messageRef |
| 5911 | = CGM.getModule().getGlobalVariable(messageRefName); |
| 5912 | if (!messageRef) { |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5913 | // Build the message ref structure. |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5914 | llvm::Constant *values[] = { fn, GetMethodVarName(selector) }; |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 5915 | llvm::Constant *init = llvm::ConstantStruct::getAnon(values); |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5916 | messageRef = new llvm::GlobalVariable(CGM.getModule(), |
| 5917 | init->getType(), |
| 5918 | /*constant*/ false, |
| 5919 | llvm::GlobalValue::WeakAnyLinkage, |
| 5920 | init, |
| 5921 | messageRefName); |
| 5922 | messageRef->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 5923 | messageRef->setAlignment(16); |
| 5924 | messageRef->setSection("__DATA, __objc_msgrefs, coalesced"); |
| 5925 | } |
Fariborz Jahanian | c93fa98 | 2012-01-30 23:39:30 +0000 | [diff] [blame] | 5926 | |
| 5927 | bool requiresnullCheck = false; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5928 | if (CGM.getLangOpts().ObjCAutoRefCount && method) |
Fariborz Jahanian | c93fa98 | 2012-01-30 23:39:30 +0000 | [diff] [blame] | 5929 | for (ObjCMethodDecl::param_const_iterator i = method->param_begin(), |
| 5930 | e = method->param_end(); i != e; ++i) { |
| 5931 | const ParmVarDecl *ParamDecl = (*i); |
| 5932 | if (ParamDecl->hasAttr<NSConsumedAttr>()) { |
| 5933 | if (!nullReturn.NullBB) |
| 5934 | nullReturn.init(CGF, arg0); |
| 5935 | requiresnullCheck = true; |
| 5936 | break; |
| 5937 | } |
| 5938 | } |
| 5939 | |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5940 | llvm::Value *mref = |
| 5941 | CGF.Builder.CreateBitCast(messageRef, ObjCTypes.MessageRefPtrTy); |
| 5942 | |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5943 | // Update the message ref argument. |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5944 | args[1].RV = RValue::get(mref); |
| 5945 | |
| 5946 | // Load the function to call from the message ref table. |
| 5947 | llvm::Value *callee = CGF.Builder.CreateStructGEP(mref, 0); |
| 5948 | callee = CGF.Builder.CreateLoad(callee, "msgSend_fn"); |
| 5949 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 5950 | callee = CGF.Builder.CreateBitCast(callee, MSI.MessengerType); |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5951 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 5952 | RValue result = CGF.EmitCall(MSI.CallInfo, callee, returnSlot, args); |
Fariborz Jahanian | c93fa98 | 2012-01-30 23:39:30 +0000 | [diff] [blame] | 5953 | return nullReturn.complete(CGF, result, resultType, formalArgs, |
| 5954 | requiresnullCheck ? method : 0); |
Fariborz Jahanian | 3d9296e | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5955 | } |
| 5956 | |
| 5957 | /// Generate code for a message send expression in the nonfragile abi. |
Daniel Dunbar | aff9fca | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 5958 | CodeGen::RValue |
| 5959 | CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
John McCall | 78a1511 | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 5960 | ReturnValueSlot Return, |
Daniel Dunbar | aff9fca | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 5961 | QualType ResultType, |
| 5962 | Selector Sel, |
| 5963 | llvm::Value *Receiver, |
Daniel Dunbar | aff9fca | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 5964 | const CallArgList &CallArgs, |
David Chisnall | 01aa467 | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 5965 | const ObjCInterfaceDecl *Class, |
Daniel Dunbar | aff9fca | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 5966 | const ObjCMethodDecl *Method) { |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5967 | return isVTableDispatchedSelector(Sel) |
| 5968 | ? EmitVTableMessageSend(CGF, Return, ResultType, Sel, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5969 | Receiver, CGF.getContext().getObjCIdType(), |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5970 | false, CallArgs, Method) |
| 5971 | : EmitMessageSend(CGF, Return, ResultType, |
| 5972 | EmitSelector(CGF.Builder, Sel), |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5973 | Receiver, CGF.getContext().getObjCIdType(), |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5974 | false, CallArgs, Method, ObjCTypes); |
Fariborz Jahanian | 3d9296e | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5975 | } |
| 5976 | |
Daniel Dunbar | c6928bb | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5977 | llvm::GlobalVariable * |
Fariborz Jahanian | 899e7eb | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5978 | CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) { |
Daniel Dunbar | c6928bb | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5979 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 5980 | |
Daniel Dunbar | a646834 | 2009-03-02 05:18:14 +0000 | [diff] [blame] | 5981 | if (!GV) { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5982 | GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy, |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5983 | false, llvm::GlobalValue::ExternalLinkage, |
| 5984 | 0, Name); |
Daniel Dunbar | c6928bb | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5985 | } |
| 5986 | |
| 5987 | return GV; |
| 5988 | } |
| 5989 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5990 | llvm::Value *CGObjCNonFragileABIMac::EmitClassRefFromId(CGBuilderTy &Builder, |
| 5991 | IdentifierInfo *II) { |
| 5992 | llvm::GlobalVariable *&Entry = ClassReferences[II]; |
| 5993 | |
Fariborz Jahanian | 33f66e6 | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5994 | if (!Entry) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5995 | std::string ClassName(getClassSymbolPrefix() + II->getName().str()); |
Daniel Dunbar | c6928bb | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5996 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5997 | Entry = |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5998 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, |
| 5999 | false, llvm::GlobalValue::InternalLinkage, |
| 6000 | ClassGV, |
| 6001 | "\01L_OBJC_CLASSLIST_REFERENCES_$_"); |
Fariborz Jahanian | 33f66e6 | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 6002 | Entry->setAlignment( |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 6003 | CGM.getDataLayout().getABITypeAlignment( |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6004 | ObjCTypes.ClassnfABIPtrTy)); |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 6005 | Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip"); |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 6006 | CGM.AddUsedGlobal(Entry); |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 6007 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6008 | |
Benjamin Kramer | 76399eb | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 6009 | return Builder.CreateLoad(Entry); |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 6010 | } |
Fariborz Jahanian | 33f66e6 | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 6011 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6012 | llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder, |
| 6013 | const ObjCInterfaceDecl *ID) { |
| 6014 | return EmitClassRefFromId(Builder, ID->getIdentifier()); |
| 6015 | } |
| 6016 | |
| 6017 | llvm::Value *CGObjCNonFragileABIMac::EmitNSAutoreleasePoolClassRef( |
| 6018 | CGBuilderTy &Builder) { |
| 6019 | IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool"); |
| 6020 | return EmitClassRefFromId(Builder, II); |
| 6021 | } |
| 6022 | |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 6023 | llvm::Value * |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6024 | CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 6025 | const ObjCInterfaceDecl *ID) { |
| 6026 | llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()]; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6027 | |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 6028 | if (!Entry) { |
| 6029 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
| 6030 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6031 | Entry = |
| 6032 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 6033 | false, llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6034 | ClassGV, |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 6035 | "\01L_OBJC_CLASSLIST_SUP_REFS_$_"); |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 6036 | Entry->setAlignment( |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 6037 | CGM.getDataLayout().getABITypeAlignment( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6038 | ObjCTypes.ClassnfABIPtrTy)); |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 6039 | Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip"); |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 6040 | CGM.AddUsedGlobal(Entry); |
Fariborz Jahanian | 33f66e6 | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 6041 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6042 | |
Benjamin Kramer | 76399eb | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 6043 | return Builder.CreateLoad(Entry); |
Fariborz Jahanian | 33f66e6 | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 6044 | } |
| 6045 | |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 6046 | /// EmitMetaClassRef - Return a Value * of the address of _class_t |
| 6047 | /// meta-data |
| 6048 | /// |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6049 | llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder, |
| 6050 | const ObjCInterfaceDecl *ID) { |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 6051 | llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()]; |
| 6052 | if (Entry) |
Benjamin Kramer | 76399eb | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 6053 | return Builder.CreateLoad(Entry); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6054 | |
Daniel Dunbar | 15894b7 | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 6055 | std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString()); |
Fariborz Jahanian | 899e7eb | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 6056 | llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6057 | Entry = |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 6058 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false, |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 6059 | llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6060 | MetaClassGV, |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 6061 | "\01L_OBJC_CLASSLIST_SUP_REFS_$_"); |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 6062 | Entry->setAlignment( |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 6063 | CGM.getDataLayout().getABITypeAlignment( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6064 | ObjCTypes.ClassnfABIPtrTy)); |
| 6065 | |
Daniel Dunbar | e60aa05 | 2009-04-15 19:03:14 +0000 | [diff] [blame] | 6066 | Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip"); |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 6067 | CGM.AddUsedGlobal(Entry); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6068 | |
Benjamin Kramer | 76399eb | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 6069 | return Builder.CreateLoad(Entry); |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 6070 | } |
| 6071 | |
Fariborz Jahanian | 33f66e6 | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 6072 | /// GetClass - Return a reference to the class for the given interface |
| 6073 | /// decl. |
| 6074 | llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder, |
| 6075 | const ObjCInterfaceDecl *ID) { |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 6076 | if (ID->isWeakImported()) { |
Fariborz Jahanian | 95ace55 | 2009-11-17 22:42:00 +0000 | [diff] [blame] | 6077 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
| 6078 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName); |
| 6079 | ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); |
| 6080 | } |
| 6081 | |
Fariborz Jahanian | 33f66e6 | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 6082 | return EmitClassRef(Builder, ID); |
| 6083 | } |
| 6084 | |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 6085 | /// Generates a message send where the super is the receiver. This is |
| 6086 | /// a message send to self with special delivery semantics indicating |
| 6087 | /// which class's method should be called. |
| 6088 | CodeGen::RValue |
| 6089 | CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
John McCall | 78a1511 | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 6090 | ReturnValueSlot Return, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6091 | QualType ResultType, |
| 6092 | Selector Sel, |
| 6093 | const ObjCInterfaceDecl *Class, |
| 6094 | bool isCategoryImpl, |
| 6095 | llvm::Value *Receiver, |
| 6096 | bool IsClassMessage, |
Daniel Dunbar | aff9fca | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 6097 | const CodeGen::CallArgList &CallArgs, |
| 6098 | const ObjCMethodDecl *Method) { |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 6099 | // ... |
| 6100 | // Create and init a super structure; this is a (receiver, class) |
| 6101 | // pair we will pass to objc_msgSendSuper. |
| 6102 | llvm::Value *ObjCSuper = |
John McCall | 6647584 | 2011-03-04 08:00:29 +0000 | [diff] [blame] | 6103 | CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6104 | |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 6105 | llvm::Value *ReceiverAsObject = |
| 6106 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); |
| 6107 | CGF.Builder.CreateStore(ReceiverAsObject, |
| 6108 | CGF.Builder.CreateStructGEP(ObjCSuper, 0)); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6109 | |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 6110 | // 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] | 6111 | llvm::Value *Target; |
Fariborz Jahanian | 27678b0 | 2012-10-10 23:11:18 +0000 | [diff] [blame] | 6112 | if (IsClassMessage) |
Fariborz Jahanian | bac73ac | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 6113 | Target = EmitMetaClassRef(CGF.Builder, Class); |
Fariborz Jahanian | 27678b0 | 2012-10-10 23:11:18 +0000 | [diff] [blame] | 6114 | else |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 6115 | Target = EmitSuperClassRef(CGF.Builder, Class); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6116 | |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 6117 | // FIXME: We shouldn't need to do this cast, rectify the ASTContext and |
| 6118 | // ObjCTypes types. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 6119 | llvm::Type *ClassTy = |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 6120 | CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); |
| 6121 | Target = CGF.Builder.CreateBitCast(Target, ClassTy); |
| 6122 | CGF.Builder.CreateStore(Target, |
| 6123 | CGF.Builder.CreateStructGEP(ObjCSuper, 1)); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6124 | |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 6125 | return (isVTableDispatchedSelector(Sel)) |
| 6126 | ? EmitVTableMessageSend(CGF, Return, ResultType, Sel, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6127 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 6128 | true, CallArgs, Method) |
| 6129 | : EmitMessageSend(CGF, Return, ResultType, |
| 6130 | EmitSelector(CGF.Builder, Sel), |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6131 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 6132 | true, CallArgs, Method, ObjCTypes); |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 6133 | } |
Fariborz Jahanian | 74b7722 | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 6134 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6135 | llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder, |
Fariborz Jahanian | 9240f3d | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 6136 | Selector Sel, bool lval) { |
Fariborz Jahanian | 74b7722 | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 6137 | llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6138 | |
Fariborz Jahanian | 74b7722 | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 6139 | if (!Entry) { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6140 | llvm::Constant *Casted = |
| 6141 | llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), |
| 6142 | ObjCTypes.SelectorPtrTy); |
| 6143 | Entry = |
| 6144 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false, |
| 6145 | llvm::GlobalValue::InternalLinkage, |
| 6146 | Casted, "\01L_OBJC_SELECTOR_REFERENCES_"); |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 6147 | Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip"); |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 6148 | CGM.AddUsedGlobal(Entry); |
Fariborz Jahanian | 74b7722 | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 6149 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6150 | |
Fariborz Jahanian | 9240f3d | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 6151 | if (lval) |
| 6152 | return Entry; |
Pete Cooper | 9d60551 | 2011-11-10 21:45:06 +0000 | [diff] [blame] | 6153 | llvm::LoadInst* LI = Builder.CreateLoad(Entry); |
| 6154 | |
| 6155 | LI->setMetadata(CGM.getModule().getMDKindID("invariant.load"), |
| 6156 | llvm::MDNode::get(VMContext, |
| 6157 | ArrayRef<llvm::Value*>())); |
| 6158 | return LI; |
Fariborz Jahanian | 74b7722 | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 6159 | } |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6160 | /// EmitObjCIvarAssign - Code gen for assigning to a __strong object. |
Fariborz Jahanian | 7a95d72 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 6161 | /// objc_assign_ivar (id src, id *dst, ptrdiff_t) |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6162 | /// |
| 6163 | void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6164 | llvm::Value *src, |
Fariborz Jahanian | 7a95d72 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 6165 | llvm::Value *dst, |
| 6166 | llvm::Value *ivarOffset) { |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 6167 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 6168 | if (!isa<llvm::PointerType>(SrcTy)) { |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 6169 | unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 6170 | assert(Size <= 8 && "does not support size > 8"); |
| 6171 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 6172 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 1b074a3 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 6173 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 6174 | } |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6175 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 6176 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Fariborz Jahanian | 7a95d72 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 6177 | CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(), |
| 6178 | src, dst, ivarOffset); |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6179 | return; |
| 6180 | } |
| 6181 | |
| 6182 | /// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object. |
| 6183 | /// objc_assign_strongCast (id src, id *dst) |
| 6184 | /// |
| 6185 | void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6186 | CodeGen::CodeGenFunction &CGF, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6187 | llvm::Value *src, llvm::Value *dst) { |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 6188 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 6189 | if (!isa<llvm::PointerType>(SrcTy)) { |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 6190 | unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 6191 | assert(Size <= 8 && "does not support size > 8"); |
| 6192 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6193 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 1b074a3 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 6194 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 6195 | } |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6196 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 6197 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 0a696a42 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 6198 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(), |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6199 | src, dst, "weakassign"); |
| 6200 | return; |
| 6201 | } |
| 6202 | |
Fariborz Jahanian | 5f21d2f | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 6203 | void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6204 | CodeGen::CodeGenFunction &CGF, |
| 6205 | llvm::Value *DestPtr, |
| 6206 | llvm::Value *SrcPtr, |
Fariborz Jahanian | 021510e | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 6207 | llvm::Value *Size) { |
Fariborz Jahanian | 5f21d2f | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 6208 | SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy); |
| 6209 | DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy); |
Fariborz Jahanian | 5f21d2f | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 6210 | CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(), |
Fariborz Jahanian | 021510e | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 6211 | DestPtr, SrcPtr, Size); |
Fariborz Jahanian | 5f21d2f | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 6212 | return; |
| 6213 | } |
| 6214 | |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6215 | /// EmitObjCWeakRead - Code gen for loading value of a __weak |
| 6216 | /// object: objc_read_weak (id *src) |
| 6217 | /// |
| 6218 | llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6219 | CodeGen::CodeGenFunction &CGF, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6220 | llvm::Value *AddrWeakObj) { |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 6221 | llvm::Type* DestTy = |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6222 | cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType(); |
| 6223 | AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 6224 | llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(), |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6225 | AddrWeakObj, "weakread"); |
Eli Friedman | a374b68 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 6226 | read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy); |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6227 | return read_weak; |
| 6228 | } |
| 6229 | |
| 6230 | /// EmitObjCWeakAssign - Code gen for assigning to a __weak object. |
| 6231 | /// objc_assign_weak (id src, id *dst) |
| 6232 | /// |
| 6233 | void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6234 | llvm::Value *src, llvm::Value *dst) { |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 6235 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 6236 | if (!isa<llvm::PointerType>(SrcTy)) { |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 6237 | unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 6238 | assert(Size <= 8 && "does not support size > 8"); |
| 6239 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 6240 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 1b074a3 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 6241 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 6242 | } |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6243 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 6244 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 6fdd57c | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 6245 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(), |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6246 | src, dst, "weakassign"); |
| 6247 | return; |
| 6248 | } |
| 6249 | |
| 6250 | /// EmitObjCGlobalAssign - Code gen for assigning to a __strong object. |
| 6251 | /// objc_assign_global (id src, id *dst) |
| 6252 | /// |
| 6253 | void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 217af24 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 6254 | llvm::Value *src, llvm::Value *dst, |
| 6255 | bool threadlocal) { |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 6256 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 6257 | if (!isa<llvm::PointerType>(SrcTy)) { |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 6258 | unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 6259 | assert(Size <= 8 && "does not support size > 8"); |
| 6260 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 6261 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 1b074a3 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 6262 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 6263 | } |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6264 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 6265 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Fariborz Jahanian | 217af24 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 6266 | if (!threadlocal) |
| 6267 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(), |
| 6268 | src, dst, "globalassign"); |
| 6269 | else |
| 6270 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(), |
| 6271 | src, dst, "threadlocalassign"); |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6272 | return; |
| 6273 | } |
Fariborz Jahanian | 74b7722 | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 6274 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6275 | void |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 6276 | CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 6277 | const ObjCAtSynchronizedStmt &S) { |
David Chisnall | 3e57560 | 2011-03-25 17:46:35 +0000 | [diff] [blame] | 6278 | EmitAtSynchronizedStmt(CGF, S, |
| 6279 | cast<llvm::Function>(ObjCTypes.getSyncEnterFn()), |
| 6280 | cast<llvm::Function>(ObjCTypes.getSyncExitFn())); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 6281 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6282 | |
John McCall | 2ca705e | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 6283 | llvm::Constant * |
Fariborz Jahanian | 831f0fc | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 6284 | CGObjCNonFragileABIMac::GetEHType(QualType T) { |
John McCall | 2ca705e | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 6285 | // There's a particular fixed type info for 'id'. |
| 6286 | if (T->isObjCIdType() || |
| 6287 | T->isObjCQualifiedIdType()) { |
| 6288 | llvm::Constant *IDEHType = |
| 6289 | CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id"); |
| 6290 | if (!IDEHType) |
| 6291 | IDEHType = |
| 6292 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, |
| 6293 | false, |
| 6294 | llvm::GlobalValue::ExternalLinkage, |
| 6295 | 0, "OBJC_EHTYPE_id"); |
| 6296 | return IDEHType; |
| 6297 | } |
| 6298 | |
| 6299 | // All other types should be Objective-C interface pointer types. |
| 6300 | const ObjCObjectPointerType *PT = |
| 6301 | T->getAs<ObjCObjectPointerType>(); |
| 6302 | assert(PT && "Invalid @catch type."); |
| 6303 | const ObjCInterfaceType *IT = PT->getInterfaceType(); |
| 6304 | assert(IT && "Invalid @catch type."); |
| 6305 | return GetInterfaceEHType(IT->getDecl(), false); |
| 6306 | } |
| 6307 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 6308 | void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF, |
| 6309 | const ObjCAtTryStmt &S) { |
David Chisnall | 3e57560 | 2011-03-25 17:46:35 +0000 | [diff] [blame] | 6310 | EmitTryCatchStmt(CGF, S, |
| 6311 | cast<llvm::Function>(ObjCTypes.getObjCBeginCatchFn()), |
| 6312 | cast<llvm::Function>(ObjCTypes.getObjCEndCatchFn()), |
| 6313 | cast<llvm::Function>(ObjCTypes.getExceptionRethrowFn())); |
Daniel Dunbar | 0b0dcd9 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 6314 | } |
| 6315 | |
Anders Carlsson | 9ab53d1 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 6316 | /// EmitThrowStmt - Generate code for a throw statement. |
| 6317 | void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
| 6318 | const ObjCAtThrowStmt &S) { |
Anders Carlsson | 9ab53d1 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 6319 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
John McCall | 248512a | 2011-10-01 10:32:24 +0000 | [diff] [blame] | 6320 | llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr); |
Benjamin Kramer | 76399eb | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 6321 | Exception = CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy); |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 6322 | CGF.EmitCallOrInvoke(ObjCTypes.getExceptionThrowFn(), Exception) |
John McCall | 17afe45 | 2010-10-16 08:21:07 +0000 | [diff] [blame] | 6323 | .setDoesNotReturn(); |
Anders Carlsson | 9ab53d1 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 6324 | } else { |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 6325 | CGF.EmitCallOrInvoke(ObjCTypes.getExceptionRethrowFn()) |
John McCall | 17afe45 | 2010-10-16 08:21:07 +0000 | [diff] [blame] | 6326 | .setDoesNotReturn(); |
Anders Carlsson | 9ab53d1 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 6327 | } |
| 6328 | |
John McCall | 17afe45 | 2010-10-16 08:21:07 +0000 | [diff] [blame] | 6329 | CGF.Builder.CreateUnreachable(); |
Anders Carlsson | 9ab53d1 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 6330 | CGF.Builder.ClearInsertionPoint(); |
| 6331 | } |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6332 | |
John McCall | 2ca705e | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 6333 | llvm::Constant * |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6334 | CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID, |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6335 | bool ForDefinition) { |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6336 | llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()]; |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6337 | |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6338 | // If we don't need a definition, return the entry if found or check |
| 6339 | // if we use an external reference. |
| 6340 | if (!ForDefinition) { |
| 6341 | if (Entry) |
| 6342 | return Entry; |
Daniel Dunbar | d7beeea | 2009-04-07 06:43:45 +0000 | [diff] [blame] | 6343 | |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6344 | // If this type (or a super class) has the __objc_exception__ |
| 6345 | // attribute, emit an external reference. |
Douglas Gregor | 78bd61f | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 6346 | if (hasObjCExceptionAttribute(CGM.getContext(), ID)) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6347 | return Entry = |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 6348 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false, |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6349 | llvm::GlobalValue::ExternalLinkage, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6350 | 0, |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 6351 | ("OBJC_EHTYPE_$_" + |
Daniel Dunbar | 07d0785 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 6352 | ID->getIdentifier()->getName())); |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6353 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6354 | |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6355 | // Otherwise we need to either make a new entry or fill in the |
| 6356 | // initializer. |
| 6357 | assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition"); |
Daniel Dunbar | 15894b7 | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 6358 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6359 | std::string VTableName = "objc_ehtype_vtable"; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6360 | llvm::GlobalVariable *VTableGV = |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6361 | CGM.getModule().getGlobalVariable(VTableName); |
| 6362 | if (!VTableGV) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6363 | VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy, |
| 6364 | false, |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6365 | llvm::GlobalValue::ExternalLinkage, |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 6366 | 0, VTableName); |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6367 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 6368 | llvm::Value *VTableIdx = llvm::ConstantInt::get(CGM.Int32Ty, 2); |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6369 | |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 6370 | llvm::Constant *Values[] = { |
| 6371 | llvm::ConstantExpr::getGetElementPtr(VTableGV, VTableIdx), |
| 6372 | GetClassName(ID->getIdentifier()), |
| 6373 | GetClassGlobal(ClassName) |
| 6374 | }; |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 6375 | llvm::Constant *Init = |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 6376 | llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values); |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6377 | |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6378 | if (Entry) { |
| 6379 | Entry->setInitializer(Init); |
| 6380 | } else { |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 6381 | Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false, |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6382 | llvm::GlobalValue::WeakAnyLinkage, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6383 | Init, |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 6384 | ("OBJC_EHTYPE_$_" + |
Daniel Dunbar | 07d0785 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 6385 | ID->getIdentifier()->getName())); |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6386 | } |
| 6387 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6388 | if (CGM.getLangOpts().getVisibilityMode() == HiddenVisibility) |
Daniel Dunbar | 15894b7 | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 6389 | Entry->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 6390 | Entry->setAlignment(CGM.getDataLayout().getABITypeAlignment( |
Daniel Dunbar | 710cb20 | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 6391 | ObjCTypes.EHTypeTy)); |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6392 | |
| 6393 | if (ForDefinition) { |
| 6394 | Entry->setSection("__DATA,__objc_const"); |
| 6395 | Entry->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 6396 | } else { |
| 6397 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
| 6398 | } |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6399 | |
| 6400 | return Entry; |
| 6401 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6402 | |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 6403 | /* *** */ |
| 6404 | |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 6405 | CodeGen::CGObjCRuntime * |
| 6406 | CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) { |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 6407 | switch (CGM.getLangOpts().ObjCRuntime.getKind()) { |
| 6408 | case ObjCRuntime::FragileMacOSX: |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 6409 | return new CGObjCMac(CGM); |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 6410 | |
| 6411 | case ObjCRuntime::MacOSX: |
| 6412 | case ObjCRuntime::iOS: |
| 6413 | return new CGObjCNonFragileABIMac(CGM); |
| 6414 | |
David Chisnall | b601c96 | 2012-07-03 20:49:52 +0000 | [diff] [blame] | 6415 | case ObjCRuntime::GNUstep: |
| 6416 | case ObjCRuntime::GCC: |
John McCall | 775086e | 2012-07-12 02:07:58 +0000 | [diff] [blame] | 6417 | case ObjCRuntime::ObjFW: |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 6418 | llvm_unreachable("these runtimes are not Mac runtimes"); |
| 6419 | } |
| 6420 | llvm_unreachable("bad runtime"); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 6421 | } |