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" |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 39 | #include "llvm/Target/TargetData.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", |
| 69 | llvm::Attribute::NonLazyBind); |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 70 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 71 | |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 72 | /// void objc_msgSend_stret (id, SEL, ...) |
| 73 | /// |
| 74 | /// The messenger used when the return value is an aggregate returned |
| 75 | /// by indirect reference in the first argument, and therefore the |
| 76 | /// self and selector parameters are shifted over by one. |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 77 | llvm::Constant *getMessageSendStretFn() const { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 78 | llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy }; |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 79 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy, |
| 80 | params, true), |
| 81 | "objc_msgSend_stret"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 82 | |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 83 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 84 | |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 85 | /// [double | long double] objc_msgSend_fpret(id self, SEL op, ...) |
| 86 | /// |
| 87 | /// The messenger used when the return value is returned on the x87 |
| 88 | /// floating-point stack; without a special entrypoint, the nil case |
| 89 | /// would be unbalanced. |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 90 | llvm::Constant *getMessageSendFpretFn() const { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 91 | llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy }; |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 92 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.DoubleTy, |
| 93 | params, true), |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 94 | "objc_msgSend_fpret"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 95 | |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 96 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 97 | |
Anders Carlsson | 2f1a6c3 | 2011-10-31 16:27:11 +0000 | [diff] [blame] | 98 | /// _Complex long double objc_msgSend_fp2ret(id self, SEL op, ...) |
| 99 | /// |
| 100 | /// The messenger used when the return value is returned in two values on the |
| 101 | /// x87 floating point stack; without a special entrypoint, the nil case |
| 102 | /// would be unbalanced. Only used on 64-bit X86. |
| 103 | llvm::Constant *getMessageSendFp2retFn() const { |
| 104 | llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy }; |
| 105 | llvm::Type *longDoubleType = llvm::Type::getX86_FP80Ty(VMContext); |
| 106 | llvm::Type *resultType = |
| 107 | llvm::StructType::get(longDoubleType, longDoubleType, NULL); |
| 108 | |
| 109 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(resultType, |
| 110 | params, true), |
| 111 | "objc_msgSend_fp2ret"); |
| 112 | } |
| 113 | |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 114 | /// id objc_msgSendSuper(struct objc_super *super, SEL op, ...) |
| 115 | /// |
| 116 | /// The messenger used for super calls, which have different dispatch |
| 117 | /// semantics. The class passed is the superclass of the current |
| 118 | /// class. |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 119 | llvm::Constant *getMessageSendSuperFn() const { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 120 | llvm::Type *params[] = { SuperPtrTy, SelectorPtrTy }; |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 121 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 122 | params, true), |
| 123 | "objc_msgSendSuper"); |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 124 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 125 | |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 126 | /// id objc_msgSendSuper2(struct objc_super *super, SEL op, ...) |
| 127 | /// |
| 128 | /// A slightly different messenger used for super calls. The class |
| 129 | /// passed is the current class. |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 130 | llvm::Constant *getMessageSendSuperFn2() const { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 131 | llvm::Type *params[] = { SuperPtrTy, SelectorPtrTy }; |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 132 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 133 | params, true), |
| 134 | "objc_msgSendSuper2"); |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 135 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 136 | |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 137 | /// void objc_msgSendSuper_stret(void *stretAddr, struct objc_super *super, |
| 138 | /// SEL op, ...) |
| 139 | /// |
| 140 | /// The messenger used for super calls which return an aggregate indirectly. |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 141 | llvm::Constant *getMessageSendSuperStretFn() const { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 142 | llvm::Type *params[] = { Int8PtrTy, SuperPtrTy, SelectorPtrTy }; |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 143 | return CGM.CreateRuntimeFunction( |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 144 | llvm::FunctionType::get(CGM.VoidTy, params, true), |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 145 | "objc_msgSendSuper_stret"); |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 146 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 147 | |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 148 | /// void objc_msgSendSuper2_stret(void * stretAddr, struct objc_super *super, |
| 149 | /// SEL op, ...) |
| 150 | /// |
| 151 | /// objc_msgSendSuper_stret with the super2 semantics. |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 152 | llvm::Constant *getMessageSendSuperStretFn2() const { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 153 | llvm::Type *params[] = { Int8PtrTy, SuperPtrTy, SelectorPtrTy }; |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 154 | return CGM.CreateRuntimeFunction( |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 155 | llvm::FunctionType::get(CGM.VoidTy, params, true), |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 156 | "objc_msgSendSuper2_stret"); |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 157 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 158 | |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 159 | llvm::Constant *getMessageSendSuperFpretFn() const { |
| 160 | // There is no objc_msgSendSuper_fpret? How can that work? |
| 161 | return getMessageSendSuperFn(); |
| 162 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 163 | |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 164 | llvm::Constant *getMessageSendSuperFpretFn2() const { |
| 165 | // There is no objc_msgSendSuper_fpret? How can that work? |
| 166 | return getMessageSendSuperFn2(); |
| 167 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 168 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 169 | protected: |
| 170 | CodeGen::CodeGenModule &CGM; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 171 | |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 172 | public: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 173 | llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy; |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 174 | llvm::Type *Int8PtrTy, *Int8PtrPtrTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 175 | |
Daniel Dunbar | 5d71559 | 2008-08-12 05:28:47 +0000 | [diff] [blame] | 176 | /// ObjectPtrTy - LLVM type for object handles (typeof(id)) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 177 | llvm::Type *ObjectPtrTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 178 | |
Fariborz Jahanian | 406b117 | 2008-11-18 20:18:11 +0000 | [diff] [blame] | 179 | /// PtrObjectPtrTy - LLVM type for id * |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 180 | llvm::Type *PtrObjectPtrTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 181 | |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 182 | /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL)) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 183 | llvm::Type *SelectorPtrTy; |
Douglas Gregor | 020de32 | 2012-01-17 18:36:30 +0000 | [diff] [blame] | 184 | |
| 185 | private: |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 186 | /// ProtocolPtrTy - LLVM type for external protocol handles |
| 187 | /// (typeof(Protocol)) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 188 | llvm::Type *ExternalProtocolPtrTy; |
Douglas Gregor | 020de32 | 2012-01-17 18:36:30 +0000 | [diff] [blame] | 189 | |
| 190 | public: |
| 191 | llvm::Type *getExternalProtocolPtrTy() { |
| 192 | if (!ExternalProtocolPtrTy) { |
| 193 | // FIXME: It would be nice to unify this with the opaque type, so that the |
| 194 | // IR comes out a bit cleaner. |
| 195 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 196 | ASTContext &Ctx = CGM.getContext(); |
| 197 | llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType()); |
| 198 | ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T); |
| 199 | } |
| 200 | |
| 201 | return ExternalProtocolPtrTy; |
| 202 | } |
| 203 | |
Daniel Dunbar | c722b85 | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 204 | // SuperCTy - clang type for struct objc_super. |
| 205 | QualType SuperCTy; |
| 206 | // SuperPtrCTy - clang type for struct objc_super *. |
| 207 | QualType SuperPtrCTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 208 | |
Daniel Dunbar | f6397fe | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 209 | /// SuperTy - LLVM type for struct objc_super. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 210 | llvm::StructType *SuperTy; |
Daniel Dunbar | 97ff50d | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 211 | /// SuperPtrTy - LLVM type for struct objc_super *. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 212 | llvm::Type *SuperPtrTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 213 | |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 214 | /// PropertyTy - LLVM type for struct objc_property (struct _prop_t |
| 215 | /// in GCC parlance). |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 216 | llvm::StructType *PropertyTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 217 | |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 218 | /// PropertyListTy - LLVM type for struct objc_property_list |
| 219 | /// (_prop_list_t in GCC parlance). |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 220 | llvm::StructType *PropertyListTy; |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 221 | /// PropertyListPtrTy - LLVM type for struct objc_property_list*. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 222 | llvm::Type *PropertyListPtrTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 223 | |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 224 | // MethodTy - LLVM type for struct objc_method. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 225 | llvm::StructType *MethodTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 226 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 227 | /// CacheTy - LLVM type for struct objc_cache. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 228 | llvm::Type *CacheTy; |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 229 | /// CachePtrTy - LLVM type for struct objc_cache *. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 230 | llvm::Type *CachePtrTy; |
Fariborz Jahanian | 0a3cfcc | 2011-06-22 20:21:51 +0000 | [diff] [blame] | 231 | |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 232 | llvm::Constant *getGetPropertyFn() { |
| 233 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 234 | ASTContext &Ctx = CGM.getContext(); |
| 235 | // id objc_getProperty (id, SEL, ptrdiff_t, bool) |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 236 | SmallVector<CanQualType,4> Params; |
John McCall | 2da83a3 | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 237 | CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType()); |
| 238 | CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType()); |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 239 | Params.push_back(IdType); |
| 240 | Params.push_back(SelType); |
David Chisnall | 08a4525 | 2011-03-22 20:03:13 +0000 | [diff] [blame] | 241 | Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified()); |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 242 | Params.push_back(Ctx.BoolTy); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 243 | llvm::FunctionType *FTy = |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 244 | Types.GetFunctionType(Types.arrangeFunctionType(IdType, Params, |
| 245 | FunctionType::ExtInfo(), |
| 246 | RequiredArgs::All)); |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 247 | return CGM.CreateRuntimeFunction(FTy, "objc_getProperty"); |
| 248 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 249 | |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 250 | llvm::Constant *getSetPropertyFn() { |
| 251 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 252 | ASTContext &Ctx = CGM.getContext(); |
| 253 | // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool) |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 254 | SmallVector<CanQualType,6> Params; |
John McCall | 2da83a3 | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 255 | CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType()); |
| 256 | CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType()); |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 257 | Params.push_back(IdType); |
| 258 | Params.push_back(SelType); |
David Chisnall | 08a4525 | 2011-03-22 20:03:13 +0000 | [diff] [blame] | 259 | Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified()); |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 260 | Params.push_back(IdType); |
| 261 | Params.push_back(Ctx.BoolTy); |
| 262 | Params.push_back(Ctx.BoolTy); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 263 | llvm::FunctionType *FTy = |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 264 | Types.GetFunctionType(Types.arrangeFunctionType(Ctx.VoidTy, Params, |
| 265 | FunctionType::ExtInfo(), |
| 266 | RequiredArgs::All)); |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 267 | return CGM.CreateRuntimeFunction(FTy, "objc_setProperty"); |
| 268 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 269 | |
Fariborz Jahanian | 5a8c203 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 270 | |
| 271 | llvm::Constant *getCopyStructFn() { |
| 272 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 273 | ASTContext &Ctx = CGM.getContext(); |
| 274 | // void objc_copyStruct (void *, const void *, size_t, bool, bool) |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 275 | SmallVector<CanQualType,5> Params; |
Fariborz Jahanian | 5a8c203 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 276 | Params.push_back(Ctx.VoidPtrTy); |
| 277 | Params.push_back(Ctx.VoidPtrTy); |
| 278 | Params.push_back(Ctx.LongTy); |
| 279 | Params.push_back(Ctx.BoolTy); |
| 280 | Params.push_back(Ctx.BoolTy); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 281 | llvm::FunctionType *FTy = |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 282 | Types.GetFunctionType(Types.arrangeFunctionType(Ctx.VoidTy, Params, |
| 283 | FunctionType::ExtInfo(), |
| 284 | RequiredArgs::All)); |
Fariborz Jahanian | 5a8c203 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 285 | return CGM.CreateRuntimeFunction(FTy, "objc_copyStruct"); |
| 286 | } |
| 287 | |
Fariborz Jahanian | 1e1b549 | 2012-01-06 18:07:23 +0000 | [diff] [blame] | 288 | /// This routine declares and returns address of: |
| 289 | /// void objc_copyCppObjectAtomic( |
| 290 | /// void *dest, const void *src, |
| 291 | /// void (*copyHelper) (void *dest, const void *source)); |
| 292 | llvm::Constant *getCppAtomicObjectFunction() { |
| 293 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 294 | ASTContext &Ctx = CGM.getContext(); |
| 295 | /// void objc_copyCppObjectAtomic(void *dest, const void *src, void *helper); |
| 296 | SmallVector<CanQualType,3> Params; |
| 297 | Params.push_back(Ctx.VoidPtrTy); |
| 298 | Params.push_back(Ctx.VoidPtrTy); |
| 299 | Params.push_back(Ctx.VoidPtrTy); |
| 300 | llvm::FunctionType *FTy = |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 301 | Types.GetFunctionType(Types.arrangeFunctionType(Ctx.VoidTy, Params, |
| 302 | FunctionType::ExtInfo(), |
| 303 | RequiredArgs::All)); |
Fariborz Jahanian | 1e1b549 | 2012-01-06 18:07:23 +0000 | [diff] [blame] | 304 | return CGM.CreateRuntimeFunction(FTy, "objc_copyCppObjectAtomic"); |
| 305 | } |
| 306 | |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 307 | llvm::Constant *getEnumerationMutationFn() { |
Daniel Dunbar | 9d82da4 | 2009-07-11 20:32:50 +0000 | [diff] [blame] | 308 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 309 | ASTContext &Ctx = CGM.getContext(); |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 310 | // void objc_enumerationMutation (id) |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 311 | SmallVector<CanQualType,1> Params; |
John McCall | 2da83a3 | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 312 | Params.push_back(Ctx.getCanonicalParamType(Ctx.getObjCIdType())); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 313 | llvm::FunctionType *FTy = |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 314 | Types.GetFunctionType(Types.arrangeFunctionType(Ctx.VoidTy, Params, |
| 315 | FunctionType::ExtInfo(), |
| 316 | RequiredArgs::All)); |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 317 | return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation"); |
| 318 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 319 | |
Fariborz Jahanian | eee54df | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 320 | /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function. |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 321 | llvm::Constant *getGcReadWeakFn() { |
| 322 | // id objc_read_weak (id *) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 323 | llvm::Type *args[] = { ObjectPtrTy->getPointerTo() }; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 324 | llvm::FunctionType *FTy = |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 325 | llvm::FunctionType::get(ObjectPtrTy, args, false); |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 326 | return CGM.CreateRuntimeFunction(FTy, "objc_read_weak"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Fariborz Jahanian | eee54df | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 329 | /// GcAssignWeakFn -- LLVM objc_assign_weak function. |
Chris Lattner | 6fdd57c | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 330 | llvm::Constant *getGcAssignWeakFn() { |
| 331 | // id objc_assign_weak (id, id *) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 332 | llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() }; |
Chris Lattner | 6fdd57c | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 333 | llvm::FunctionType *FTy = |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 334 | llvm::FunctionType::get(ObjectPtrTy, args, false); |
Chris Lattner | 6fdd57c | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 335 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak"); |
| 336 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 337 | |
Fariborz Jahanian | eee54df | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 338 | /// GcAssignGlobalFn -- LLVM objc_assign_global function. |
Chris Lattner | 0a696a42 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 339 | llvm::Constant *getGcAssignGlobalFn() { |
| 340 | // id objc_assign_global(id, id *) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 341 | llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() }; |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 342 | llvm::FunctionType *FTy = |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 343 | llvm::FunctionType::get(ObjectPtrTy, args, false); |
Chris Lattner | 0a696a42 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 344 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_global"); |
| 345 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 346 | |
Fariborz Jahanian | 217af24 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 347 | /// GcAssignThreadLocalFn -- LLVM objc_assign_threadlocal function. |
| 348 | llvm::Constant *getGcAssignThreadLocalFn() { |
| 349 | // id objc_assign_threadlocal(id src, id * dest) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 350 | llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() }; |
Fariborz Jahanian | 217af24 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 351 | llvm::FunctionType *FTy = |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 352 | llvm::FunctionType::get(ObjectPtrTy, args, false); |
Fariborz Jahanian | 217af24 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 353 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_threadlocal"); |
| 354 | } |
| 355 | |
Fariborz Jahanian | eee54df | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 356 | /// GcAssignIvarFn -- LLVM objc_assign_ivar function. |
Chris Lattner | 0a696a42 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 357 | llvm::Constant *getGcAssignIvarFn() { |
Fariborz Jahanian | 7a95d72 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 358 | // id objc_assign_ivar(id, id *, ptrdiff_t) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 359 | llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo(), |
| 360 | CGM.PtrDiffTy }; |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 361 | llvm::FunctionType *FTy = |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 362 | llvm::FunctionType::get(ObjectPtrTy, args, false); |
Chris Lattner | 0a696a42 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 363 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar"); |
| 364 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 365 | |
Fariborz Jahanian | 5f21d2f | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 366 | /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function. |
| 367 | llvm::Constant *GcMemmoveCollectableFn() { |
| 368 | // void *objc_memmove_collectable(void *dst, const void *src, size_t size) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 369 | llvm::Type *args[] = { Int8PtrTy, Int8PtrTy, LongTy }; |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 370 | llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, args, false); |
Fariborz Jahanian | 5f21d2f | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 371 | return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable"); |
| 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 | /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function. |
Chris Lattner | 0a696a42 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 375 | llvm::Constant *getGcAssignStrongCastFn() { |
Fariborz Jahanian | 217af24 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 376 | // id objc_assign_strongCast(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_strongCast"); |
| 381 | } |
Anders Carlsson | 9ab53d1 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 382 | |
| 383 | /// ExceptionThrowFn - LLVM objc_exception_throw function. |
Chris Lattner | 0a696a42 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 384 | llvm::Constant *getExceptionThrowFn() { |
| 385 | // void objc_exception_throw(id) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 386 | llvm::Type *args[] = { ObjectPtrTy }; |
Chris Lattner | 0a696a42 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 387 | llvm::FunctionType *FTy = |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 388 | llvm::FunctionType::get(CGM.VoidTy, args, false); |
Chris Lattner | 0a696a42 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 389 | return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw"); |
| 390 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 391 | |
Fariborz Jahanian | 3336de1 | 2010-05-28 17:34:43 +0000 | [diff] [blame] | 392 | /// ExceptionRethrowFn - LLVM objc_exception_rethrow function. |
| 393 | llvm::Constant *getExceptionRethrowFn() { |
| 394 | // void objc_exception_rethrow(void) |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 395 | llvm::FunctionType *FTy = llvm::FunctionType::get(CGM.VoidTy, false); |
Fariborz Jahanian | 3336de1 | 2010-05-28 17:34:43 +0000 | [diff] [blame] | 396 | return CGM.CreateRuntimeFunction(FTy, "objc_exception_rethrow"); |
| 397 | } |
| 398 | |
Daniel Dunbar | 94ceb61 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 399 | /// SyncEnterFn - LLVM object_sync_enter function. |
Chris Lattner | dcceee7 | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 400 | llvm::Constant *getSyncEnterFn() { |
| 401 | // void objc_sync_enter (id) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 402 | llvm::Type *args[] = { ObjectPtrTy }; |
Chris Lattner | dcceee7 | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 403 | llvm::FunctionType *FTy = |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 404 | llvm::FunctionType::get(CGM.VoidTy, args, false); |
Chris Lattner | dcceee7 | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 405 | return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter"); |
| 406 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 407 | |
Daniel Dunbar | 94ceb61 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 408 | /// SyncExitFn - LLVM object_sync_exit function. |
Chris Lattner | 0a696a42 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 409 | llvm::Constant *getSyncExitFn() { |
| 410 | // void objc_sync_exit (id) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 411 | llvm::Type *args[] = { ObjectPtrTy }; |
Chris Lattner | 0a696a42 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 412 | llvm::FunctionType *FTy = |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 413 | llvm::FunctionType::get(CGM.VoidTy, args, false); |
Chris Lattner | 0a696a42 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 414 | return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit"); |
| 415 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 416 | |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 417 | llvm::Constant *getSendFn(bool IsSuper) const { |
| 418 | return IsSuper ? getMessageSendSuperFn() : getMessageSendFn(); |
| 419 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 420 | |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 421 | llvm::Constant *getSendFn2(bool IsSuper) const { |
| 422 | return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn(); |
| 423 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 424 | |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 425 | llvm::Constant *getSendStretFn(bool IsSuper) const { |
| 426 | return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn(); |
| 427 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 428 | |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 429 | llvm::Constant *getSendStretFn2(bool IsSuper) const { |
| 430 | return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn(); |
| 431 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 432 | |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 433 | llvm::Constant *getSendFpretFn(bool IsSuper) const { |
| 434 | return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn(); |
| 435 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 436 | |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 437 | llvm::Constant *getSendFpretFn2(bool IsSuper) const { |
| 438 | return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn(); |
| 439 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 440 | |
Anders Carlsson | 2f1a6c3 | 2011-10-31 16:27:11 +0000 | [diff] [blame] | 441 | llvm::Constant *getSendFp2retFn(bool IsSuper) const { |
| 442 | return IsSuper ? getMessageSendSuperFn() : getMessageSendFp2retFn(); |
| 443 | } |
| 444 | |
| 445 | llvm::Constant *getSendFp2RetFn2(bool IsSuper) const { |
| 446 | return IsSuper ? getMessageSendSuperFn2() : getMessageSendFp2retFn(); |
| 447 | } |
| 448 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 449 | ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm); |
| 450 | ~ObjCCommonTypesHelper(){} |
| 451 | }; |
Daniel Dunbar | f6397fe | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 452 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 453 | /// ObjCTypesHelper - Helper class that encapsulates lazy |
| 454 | /// construction of varies types used during ObjC generation. |
| 455 | class ObjCTypesHelper : public ObjCCommonTypesHelper { |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 456 | public: |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 457 | /// SymtabTy - LLVM type for struct objc_symtab. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 458 | llvm::StructType *SymtabTy; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 459 | /// SymtabPtrTy - LLVM type for struct objc_symtab *. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 460 | llvm::Type *SymtabPtrTy; |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 461 | /// ModuleTy - LLVM type for struct objc_module. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 462 | llvm::StructType *ModuleTy; |
Daniel Dunbar | cb515c8 | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 463 | |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 464 | /// ProtocolTy - LLVM type for struct objc_protocol. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 465 | llvm::StructType *ProtocolTy; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 466 | /// ProtocolPtrTy - LLVM type for struct objc_protocol *. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 467 | llvm::Type *ProtocolPtrTy; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 468 | /// ProtocolExtensionTy - LLVM type for struct |
| 469 | /// objc_protocol_extension. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 470 | llvm::StructType *ProtocolExtensionTy; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 471 | /// ProtocolExtensionTy - LLVM type for struct |
| 472 | /// objc_protocol_extension *. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 473 | llvm::Type *ProtocolExtensionPtrTy; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 474 | /// MethodDescriptionTy - LLVM type for struct |
| 475 | /// objc_method_description. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 476 | llvm::StructType *MethodDescriptionTy; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 477 | /// MethodDescriptionListTy - LLVM type for struct |
| 478 | /// objc_method_description_list. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 479 | llvm::StructType *MethodDescriptionListTy; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 480 | /// MethodDescriptionListPtrTy - LLVM type for struct |
| 481 | /// objc_method_description_list *. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 482 | llvm::Type *MethodDescriptionListPtrTy; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 483 | /// ProtocolListTy - LLVM type for struct objc_property_list. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 484 | llvm::StructType *ProtocolListTy; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 485 | /// ProtocolListPtrTy - LLVM type for struct objc_property_list*. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 486 | llvm::Type *ProtocolListPtrTy; |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 487 | /// CategoryTy - LLVM type for struct objc_category. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 488 | llvm::StructType *CategoryTy; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 489 | /// ClassTy - LLVM type for struct objc_class. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 490 | llvm::StructType *ClassTy; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 491 | /// ClassPtrTy - LLVM type for struct objc_class *. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 492 | llvm::Type *ClassPtrTy; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 493 | /// ClassExtensionTy - LLVM type for struct objc_class_ext. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 494 | llvm::StructType *ClassExtensionTy; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 495 | /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 496 | llvm::Type *ClassExtensionPtrTy; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 497 | // IvarTy - LLVM type for struct objc_ivar. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 498 | llvm::StructType *IvarTy; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 499 | /// IvarListTy - LLVM type for struct objc_ivar_list. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 500 | llvm::Type *IvarListTy; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 501 | /// IvarListPtrTy - LLVM type for struct objc_ivar_list *. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 502 | llvm::Type *IvarListPtrTy; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 503 | /// MethodListTy - LLVM type for struct objc_method_list. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 504 | llvm::Type *MethodListTy; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 505 | /// MethodListPtrTy - LLVM type for struct objc_method_list *. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 506 | llvm::Type *MethodListPtrTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 507 | |
Anders Carlsson | 9ff2248 | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 508 | /// ExceptionDataTy - LLVM type for struct _objc_exception_data. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 509 | llvm::Type *ExceptionDataTy; |
Fariborz Jahanian | 0a3cfcc | 2011-06-22 20:21:51 +0000 | [diff] [blame] | 510 | |
Anders Carlsson | 9ff2248 | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 511 | /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function. |
Chris Lattner | c6406db | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 512 | llvm::Constant *getExceptionTryEnterFn() { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 513 | llvm::Type *params[] = { ExceptionDataTy->getPointerTo() }; |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 514 | return CGM.CreateRuntimeFunction( |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 515 | llvm::FunctionType::get(CGM.VoidTy, params, false), |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 516 | "objc_exception_try_enter"); |
Chris Lattner | c6406db | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 517 | } |
Anders Carlsson | 9ff2248 | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 518 | |
| 519 | /// ExceptionTryExitFn - LLVM objc_exception_try_exit function. |
Chris Lattner | c6406db | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 520 | llvm::Constant *getExceptionTryExitFn() { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 521 | llvm::Type *params[] = { ExceptionDataTy->getPointerTo() }; |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 522 | return CGM.CreateRuntimeFunction( |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 523 | llvm::FunctionType::get(CGM.VoidTy, params, false), |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 524 | "objc_exception_try_exit"); |
Chris Lattner | c6406db | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 525 | } |
Anders Carlsson | 9ff2248 | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 526 | |
| 527 | /// ExceptionExtractFn - LLVM objc_exception_extract function. |
Chris Lattner | c6406db | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 528 | llvm::Constant *getExceptionExtractFn() { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 529 | llvm::Type *params[] = { ExceptionDataTy->getPointerTo() }; |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 530 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 531 | params, false), |
Chris Lattner | c6406db | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 532 | "objc_exception_extract"); |
Chris Lattner | c6406db | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 533 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 534 | |
Anders Carlsson | 9ff2248 | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 535 | /// ExceptionMatchFn - LLVM objc_exception_match function. |
Chris Lattner | c6406db | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 536 | llvm::Constant *getExceptionMatchFn() { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 537 | llvm::Type *params[] = { ClassPtrTy, ObjectPtrTy }; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 538 | return CGM.CreateRuntimeFunction( |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 539 | llvm::FunctionType::get(CGM.Int32Ty, params, false), |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 540 | "objc_exception_match"); |
| 541 | |
Chris Lattner | c6406db | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 542 | } |
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 | /// SetJmpFn - LLVM _setjmp function. |
Chris Lattner | c6406db | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 545 | llvm::Constant *getSetJmpFn() { |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 546 | // This is specifically the prototype for x86. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 547 | llvm::Type *params[] = { CGM.Int32Ty->getPointerTo() }; |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 548 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.Int32Ty, |
| 549 | params, false), |
Bill Wendling | bcefeae | 2011-11-29 00:10:10 +0000 | [diff] [blame] | 550 | "_setjmp", |
| 551 | llvm::Attribute::ReturnsTwice); |
Chris Lattner | c6406db | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 552 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 553 | |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 554 | public: |
| 555 | ObjCTypesHelper(CodeGen::CodeGenModule &cgm); |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 556 | ~ObjCTypesHelper() {} |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 557 | }; |
| 558 | |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 559 | /// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 560 | /// modern abi |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 561 | class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper { |
Fariborz Jahanian | e4dc35d | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 562 | public: |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 563 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 564 | // MethodListnfABITy - LLVM for struct _method_list_t |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 565 | llvm::StructType *MethodListnfABITy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 566 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 567 | // MethodListnfABIPtrTy - LLVM for struct _method_list_t* |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 568 | llvm::Type *MethodListnfABIPtrTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 569 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 570 | // ProtocolnfABITy = LLVM for struct _protocol_t |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 571 | llvm::StructType *ProtocolnfABITy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 572 | |
Daniel Dunbar | 8de90f0 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 573 | // ProtocolnfABIPtrTy = LLVM for struct _protocol_t* |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 574 | llvm::Type *ProtocolnfABIPtrTy; |
Daniel Dunbar | 8de90f0 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 575 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 576 | // ProtocolListnfABITy - LLVM for struct _objc_protocol_list |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 577 | llvm::StructType *ProtocolListnfABITy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 578 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 579 | // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list* |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 580 | llvm::Type *ProtocolListnfABIPtrTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 581 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 582 | // ClassnfABITy - LLVM for struct _class_t |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 583 | llvm::StructType *ClassnfABITy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 584 | |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 585 | // ClassnfABIPtrTy - LLVM for struct _class_t* |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 586 | llvm::Type *ClassnfABIPtrTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 587 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 588 | // IvarnfABITy - LLVM for struct _ivar_t |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 589 | llvm::StructType *IvarnfABITy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 590 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 591 | // IvarListnfABITy - LLVM for struct _ivar_list_t |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 592 | llvm::StructType *IvarListnfABITy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 593 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 594 | // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t* |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 595 | llvm::Type *IvarListnfABIPtrTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 596 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 597 | // ClassRonfABITy - LLVM for struct _class_ro_t |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 598 | llvm::StructType *ClassRonfABITy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 599 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 600 | // ImpnfABITy - LLVM for id (*)(id, SEL, ...) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 601 | llvm::Type *ImpnfABITy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 602 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 603 | // CategorynfABITy - LLVM for struct _category_t |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 604 | llvm::StructType *CategorynfABITy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 605 | |
Fariborz Jahanian | 82c72e1 | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 606 | // New types for nonfragile abi messaging. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 607 | |
Fariborz Jahanian | 82c72e1 | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 608 | // MessageRefTy - LLVM for: |
| 609 | // struct _message_ref_t { |
| 610 | // IMP messenger; |
| 611 | // SEL name; |
| 612 | // }; |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 613 | llvm::StructType *MessageRefTy; |
Fariborz Jahanian | e4dc35d | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 614 | // MessageRefCTy - clang type for struct _message_ref_t |
| 615 | QualType MessageRefCTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 616 | |
Fariborz Jahanian | 82c72e1 | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 617 | // MessageRefPtrTy - LLVM for struct _message_ref_t* |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 618 | llvm::Type *MessageRefPtrTy; |
Fariborz Jahanian | e4dc35d | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 619 | // MessageRefCPtrTy - clang type for struct _message_ref_t* |
| 620 | QualType MessageRefCPtrTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 621 | |
Fariborz Jahanian | 4e87c83 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 622 | // MessengerTy - Type of the messenger (shown as IMP above) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 623 | llvm::FunctionType *MessengerTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 624 | |
Fariborz Jahanian | 82c72e1 | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 625 | // SuperMessageRefTy - LLVM for: |
| 626 | // struct _super_message_ref_t { |
| 627 | // SUPER_IMP messenger; |
| 628 | // SEL name; |
| 629 | // }; |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 630 | llvm::StructType *SuperMessageRefTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 631 | |
Fariborz Jahanian | 82c72e1 | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 632 | // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t* |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 633 | llvm::Type *SuperMessageRefPtrTy; |
Daniel Dunbar | 0b0dcd9 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 634 | |
Chris Lattner | 2dfdb3e | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 635 | llvm::Constant *getMessageSendFixupFn() { |
| 636 | // id objc_msgSend_fixup(id, struct message_ref_t*, ...) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 637 | llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy }; |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 638 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 639 | params, true), |
Chris Lattner | 2dfdb3e | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 640 | "objc_msgSend_fixup"); |
| 641 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 642 | |
Chris Lattner | 2dfdb3e | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 643 | llvm::Constant *getMessageSendFpretFixupFn() { |
| 644 | // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 645 | llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy }; |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 646 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 647 | params, true), |
Chris Lattner | 2dfdb3e | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 648 | "objc_msgSend_fpret_fixup"); |
| 649 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 650 | |
Chris Lattner | 2dfdb3e | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 651 | llvm::Constant *getMessageSendStretFixupFn() { |
| 652 | // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 653 | llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy }; |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 654 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 655 | params, true), |
Chris Lattner | 2dfdb3e | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 656 | "objc_msgSend_stret_fixup"); |
| 657 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 658 | |
Chris Lattner | 2dfdb3e | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 659 | llvm::Constant *getMessageSendSuper2FixupFn() { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 660 | // id objc_msgSendSuper2_fixup (struct objc_super *, |
Chris Lattner | 2dfdb3e | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 661 | // struct _super_message_ref_t*, ...) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 662 | llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy }; |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 663 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 664 | params, true), |
Chris Lattner | 2dfdb3e | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 665 | "objc_msgSendSuper2_fixup"); |
| 666 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 667 | |
Chris Lattner | 2dfdb3e | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 668 | llvm::Constant *getMessageSendSuper2StretFixupFn() { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 669 | // id objc_msgSendSuper2_stret_fixup(struct objc_super *, |
Chris Lattner | 2dfdb3e | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 670 | // struct _super_message_ref_t*, ...) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 671 | llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy }; |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 672 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 673 | params, true), |
Chris Lattner | 2dfdb3e | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 674 | "objc_msgSendSuper2_stret_fixup"); |
| 675 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 676 | |
Chris Lattner | a7c00b4 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 677 | llvm::Constant *getObjCEndCatchFn() { |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 678 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy, false), |
Chris Lattner | a7c00b4 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 679 | "objc_end_catch"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 680 | |
Chris Lattner | a7c00b4 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 681 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 682 | |
Chris Lattner | a7c00b4 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 683 | llvm::Constant *getObjCBeginCatchFn() { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 684 | llvm::Type *params[] = { Int8PtrTy }; |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 685 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy, |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 686 | params, false), |
Chris Lattner | a7c00b4 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 687 | "objc_begin_catch"); |
| 688 | } |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 689 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 690 | llvm::StructType *EHTypeTy; |
| 691 | llvm::Type *EHTypePtrTy; |
Fariborz Jahanian | 0a3cfcc | 2011-06-22 20:21:51 +0000 | [diff] [blame] | 692 | |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 693 | ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm); |
| 694 | ~ObjCNonFragileABITypesHelper(){} |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 695 | }; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 696 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 697 | class CGObjCCommonMac : public CodeGen::CGObjCRuntime { |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 698 | public: |
| 699 | // FIXME - accessibility |
Fariborz Jahanian | 524bb20 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 700 | class GC_IVAR { |
Fariborz Jahanian | 3b0f886 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 701 | public: |
Daniel Dunbar | 7b89ace | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 702 | unsigned ivar_bytepos; |
| 703 | unsigned ivar_size; |
| 704 | GC_IVAR(unsigned bytepos = 0, unsigned size = 0) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 705 | : ivar_bytepos(bytepos), ivar_size(size) {} |
Daniel Dunbar | 22b0ada | 2009-04-23 01:29:05 +0000 | [diff] [blame] | 706 | |
| 707 | // Allow sorting based on byte pos. |
| 708 | bool operator<(const GC_IVAR &b) const { |
| 709 | return ivar_bytepos < b.ivar_bytepos; |
| 710 | } |
Fariborz Jahanian | 524bb20 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 711 | }; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 712 | |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 713 | class SKIP_SCAN { |
Daniel Dunbar | 7b89ace | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 714 | public: |
| 715 | unsigned skip; |
| 716 | unsigned scan; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 717 | SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0) |
Daniel Dunbar | 7b89ace | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 718 | : skip(_skip), scan(_scan) {} |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 719 | }; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 720 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 721 | protected: |
Owen Anderson | ae86c19 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 722 | llvm::LLVMContext &VMContext; |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 723 | // FIXME! May not be needing this after all. |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 724 | unsigned ObjCABI; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 725 | |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 726 | // gc ivar layout bitmap calculation helper caches. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 727 | SmallVector<GC_IVAR, 16> SkipIvars; |
| 728 | SmallVector<GC_IVAR, 16> IvarsInfo; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 729 | |
Daniel Dunbar | c61d0e9 | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 730 | /// LazySymbols - Symbols to generate a lazy reference for. See |
| 731 | /// DefinedSymbols and FinishModule(). |
Daniel Dunbar | d027a92 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 732 | llvm::SetVector<IdentifierInfo*> LazySymbols; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 733 | |
Daniel Dunbar | c61d0e9 | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 734 | /// DefinedSymbols - External symbols which are defined by this |
| 735 | /// module. The symbols in this list and LazySymbols are used to add |
| 736 | /// special linker symbols which ensure that Objective-C modules are |
| 737 | /// linked properly. |
Daniel Dunbar | d027a92 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 738 | llvm::SetVector<IdentifierInfo*> DefinedSymbols; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 739 | |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 740 | /// ClassNames - uniqued class names. |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 741 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 742 | |
Daniel Dunbar | cb515c8 | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 743 | /// MethodVarNames - uniqued method variable names. |
| 744 | llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 745 | |
Fariborz Jahanian | 9adb2e6 | 2010-06-21 22:05:18 +0000 | [diff] [blame] | 746 | /// DefinedCategoryNames - list of category names in form Class_Category. |
| 747 | llvm::SetVector<std::string> DefinedCategoryNames; |
| 748 | |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 749 | /// MethodVarTypes - uniqued method type signatures. We have to use |
| 750 | /// a StringMap here because have no other unique reference. |
| 751 | llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 752 | |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 753 | /// MethodDefinitions - map of methods which have been defined in |
| 754 | /// this translation unit. |
| 755 | llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 756 | |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 757 | /// PropertyNames - uniqued method variable names. |
| 758 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 759 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 760 | /// ClassReferences - uniqued class references. |
| 761 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 762 | |
Daniel Dunbar | cb515c8 | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 763 | /// SelectorReferences - uniqued selector references. |
| 764 | llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 765 | |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 766 | /// Protocols - Protocols for which an objc_protocol structure has |
| 767 | /// been emitted. Forward declarations are handled by creating an |
| 768 | /// empty structure whose initializer is filled in when/if defined. |
| 769 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 770 | |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 771 | /// DefinedProtocols - Protocols which have actually been |
| 772 | /// defined. We should not need this, see FIXME in GenerateProtocol. |
| 773 | llvm::DenseSet<IdentifierInfo*> DefinedProtocols; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 774 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 775 | /// DefinedClasses - List of defined classes. |
Bill Wendling | 8392a47 | 2012-02-07 09:40:07 +0000 | [diff] [blame] | 776 | llvm::SmallVector<llvm::GlobalValue*, 16> DefinedClasses; |
Daniel Dunbar | 9a017d7 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 777 | |
| 778 | /// DefinedNonLazyClasses - List of defined "non-lazy" classes. |
Bill Wendling | 8392a47 | 2012-02-07 09:40:07 +0000 | [diff] [blame] | 779 | llvm::SmallVector<llvm::GlobalValue*, 16> DefinedNonLazyClasses; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 780 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 781 | /// DefinedCategories - List of defined categories. |
Bill Wendling | 8392a47 | 2012-02-07 09:40:07 +0000 | [diff] [blame] | 782 | llvm::SmallVector<llvm::GlobalValue*, 16> DefinedCategories; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 783 | |
Daniel Dunbar | 9a017d7 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 784 | /// DefinedNonLazyCategories - List of defined "non-lazy" categories. |
Bill Wendling | 8392a47 | 2012-02-07 09:40:07 +0000 | [diff] [blame] | 785 | llvm::SmallVector<llvm::GlobalValue*, 16> DefinedNonLazyCategories; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 786 | |
Fariborz Jahanian | 0b1ccdc | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 787 | /// GetNameForMethod - Return a name for the given method. |
| 788 | /// \param[out] NameOut - The return value. |
| 789 | void GetNameForMethod(const ObjCMethodDecl *OMD, |
| 790 | const ObjCContainerDecl *CD, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 791 | SmallVectorImpl<char> &NameOut); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 792 | |
Fariborz Jahanian | 0b1ccdc | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 793 | /// GetMethodVarName - Return a unique constant for the given |
| 794 | /// selector's name. The return value has type char *. |
| 795 | llvm::Constant *GetMethodVarName(Selector Sel); |
| 796 | llvm::Constant *GetMethodVarName(IdentifierInfo *Ident); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 797 | |
Fariborz Jahanian | 0b1ccdc | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 798 | /// GetMethodVarType - Return a unique constant for the given |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 799 | /// method's type encoding string. The return value has type char *. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 800 | |
Fariborz Jahanian | 0b1ccdc | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 801 | // FIXME: This is a horrible name. |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 802 | llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D, |
| 803 | bool Extended = false); |
Daniel Dunbar | f5c1846 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 804 | llvm::Constant *GetMethodVarType(const FieldDecl *D); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 805 | |
Fariborz Jahanian | 0b1ccdc | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 806 | /// GetPropertyName - Return a unique constant for the given |
| 807 | /// name. The return value has type char *. |
| 808 | llvm::Constant *GetPropertyName(IdentifierInfo *Ident); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 809 | |
Fariborz Jahanian | 0b1ccdc | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 810 | // FIXME: This can be dropped once string functions are unified. |
| 811 | llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD, |
| 812 | const Decl *Container); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 813 | |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 814 | /// GetClassName - Return a unique constant for the given selector's |
| 815 | /// name. The return value has type char *. |
| 816 | llvm::Constant *GetClassName(IdentifierInfo *Ident); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 817 | |
Argyrios Kyrtzidis | 13257c5 | 2010-08-09 10:54:20 +0000 | [diff] [blame] | 818 | llvm::Function *GetMethodDefinition(const ObjCMethodDecl *MD); |
| 819 | |
Fariborz Jahanian | c559f3f | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 820 | /// BuildIvarLayout - Builds ivar layout bitmap for the class |
| 821 | /// implementation for the __strong or __weak case. |
| 822 | /// |
Fariborz Jahanian | 1bf7288 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 823 | llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI, |
| 824 | bool ForStrongLayout); |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 825 | |
Fariborz Jahanian | 1f78a9a | 2010-08-05 00:19:48 +0000 | [diff] [blame] | 826 | llvm::Constant *BuildIvarLayoutBitmap(std::string &BitMap); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 827 | |
Daniel Dunbar | 15bd888 | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 828 | void BuildAggrIvarRecordLayout(const RecordType *RT, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 829 | unsigned int BytePos, bool ForStrongLayout, |
| 830 | bool &HasUnion); |
Daniel Dunbar | 36e2a1e | 2009-05-03 21:05:10 +0000 | [diff] [blame] | 831 | void BuildAggrIvarLayout(const ObjCImplementationDecl *OI, |
Fariborz Jahanian | 1bf7288 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 832 | const llvm::StructLayout *Layout, |
Fariborz Jahanian | 524bb20 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 833 | const RecordDecl *RD, |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 834 | const SmallVectorImpl<const FieldDecl*> &RecFields, |
Fariborz Jahanian | c559f3f | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 835 | unsigned int BytePos, bool ForStrongLayout, |
Fariborz Jahanian | a123b64 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 836 | bool &HasUnion); |
Fariborz Jahanian | 1bf7288 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 837 | |
Fariborz Jahanian | 01dff42 | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 838 | /// GetIvarLayoutName - Returns a unique constant for the given |
| 839 | /// ivar layout bitmap. |
| 840 | llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident, |
| 841 | const ObjCCommonTypesHelper &ObjCTypes); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 842 | |
Fariborz Jahanian | 066347e | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 843 | /// EmitPropertyList - Emit the given property list. The return |
| 844 | /// value has type PropertyListPtrTy. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 845 | llvm::Constant *EmitPropertyList(Twine Name, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 846 | const Decl *Container, |
Fariborz Jahanian | 066347e | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 847 | const ObjCContainerDecl *OCD, |
| 848 | const ObjCCommonTypesHelper &ObjCTypes); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 849 | |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 850 | /// EmitProtocolMethodTypes - Generate the array of extended method type |
| 851 | /// strings. The return value has type Int8PtrPtrTy. |
| 852 | llvm::Constant *EmitProtocolMethodTypes(Twine Name, |
Bill Wendling | cb5b5ff | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 853 | ArrayRef<llvm::Constant*> MethodTypes, |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 854 | const ObjCCommonTypesHelper &ObjCTypes); |
| 855 | |
Fariborz Jahanian | 751c1e7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 856 | /// PushProtocolProperties - Push protocol's property on the input stack. |
Bill Wendling | a515b58 | 2012-02-09 22:16:49 +0000 | [diff] [blame] | 857 | void PushProtocolProperties( |
| 858 | llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet, |
| 859 | llvm::SmallVectorImpl<llvm::Constant*> &Properties, |
| 860 | const Decl *Container, |
| 861 | const ObjCProtocolDecl *PROTO, |
| 862 | const ObjCCommonTypesHelper &ObjCTypes); |
Fariborz Jahanian | 751c1e7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 863 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 864 | /// GetProtocolRef - Return a reference to the internal protocol |
| 865 | /// description, creating an empty one if it has not been |
| 866 | /// defined. The return value has type ProtocolPtrTy. |
| 867 | llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD); |
Fariborz Jahanian | 6772621 | 2009-03-08 20:18:37 +0000 | [diff] [blame] | 868 | |
Daniel Dunbar | 30c6536 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 869 | /// CreateMetadataVar - Create a global variable with internal |
| 870 | /// linkage for use by the Objective-C runtime. |
| 871 | /// |
| 872 | /// This is a convenience wrapper which not only creates the |
| 873 | /// variable, but also sets the section and alignment and adds the |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 874 | /// global to the "llvm.used" list. |
Daniel Dunbar | 463cc8a | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 875 | /// |
| 876 | /// \param Name - The variable name. |
| 877 | /// \param Init - The variable initializer; this is also used to |
| 878 | /// define the type of the variable. |
| 879 | /// \param Section - The section the variable should go into, or 0. |
| 880 | /// \param Align - The alignment for the variable, or 0. |
| 881 | /// \param AddToUsed - Whether the variable should be added to |
Daniel Dunbar | 4527d30 | 2009-04-14 17:42:51 +0000 | [diff] [blame] | 882 | /// "llvm.used". |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 883 | llvm::GlobalVariable *CreateMetadataVar(Twine Name, |
Daniel Dunbar | 30c6536 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 884 | llvm::Constant *Init, |
| 885 | const char *Section, |
Daniel Dunbar | 463cc8a | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 886 | unsigned Align, |
| 887 | bool AddToUsed); |
Daniel Dunbar | 30c6536 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 888 | |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 889 | CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
| 890 | ReturnValueSlot Return, |
| 891 | QualType ResultType, |
| 892 | llvm::Value *Sel, |
| 893 | llvm::Value *Arg0, |
| 894 | QualType Arg0Ty, |
| 895 | bool IsSuper, |
| 896 | const CallArgList &CallArgs, |
| 897 | const ObjCMethodDecl *OMD, |
| 898 | const ObjCCommonTypesHelper &ObjCTypes); |
Daniel Dunbar | f5c1846 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 899 | |
Daniel Dunbar | 5e63927 | 2010-04-25 20:39:01 +0000 | [diff] [blame] | 900 | /// EmitImageInfo - Emit the image info marker used to encode some module |
| 901 | /// level information. |
| 902 | void EmitImageInfo(); |
| 903 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 904 | public: |
Owen Anderson | ae86c19 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 905 | CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 906 | CGObjCRuntime(cgm), VMContext(cgm.getLLVMContext()) { } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 907 | |
David Chisnall | 481e3a8 | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 908 | virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 909 | |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 910 | virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD, |
| 911 | const ObjCContainerDecl *CD=0); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 912 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 913 | virtual void GenerateProtocol(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 914 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 915 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 916 | /// declaration, emitting it if necessary. The return value has type |
| 917 | /// ProtocolPtrTy. |
| 918 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 919 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 920 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 921 | /// object for the given declaration, emitting it if needed. These |
| 922 | /// forward references will be filled in with empty bodies if no |
| 923 | /// definition is seen. The return value has type ProtocolPtrTy. |
| 924 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0; |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 925 | virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM, |
| 926 | const CGBlockInfo &blockInfo); |
Fariborz Jahanian | c05349e | 2010-08-04 16:57:49 +0000 | [diff] [blame] | 927 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 928 | }; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 929 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 930 | class CGObjCMac : public CGObjCCommonMac { |
| 931 | private: |
| 932 | ObjCTypesHelper ObjCTypes; |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 933 | |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 934 | /// EmitModuleInfo - Another marker encoding module level |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 935 | /// information. |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 936 | void EmitModuleInfo(); |
| 937 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 938 | /// EmitModuleSymols - Emit module symbols, the list of defined |
| 939 | /// classes and categories. The result has type SymtabPtrTy. |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 940 | llvm::Constant *EmitModuleSymbols(); |
| 941 | |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 942 | /// FinishModule - Write out global data structures at the end of |
| 943 | /// processing a translation unit. |
| 944 | void FinishModule(); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 945 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 946 | /// EmitClassExtension - Generate the class extension structure used |
| 947 | /// to store the weak ivar layout and properties. The return value |
| 948 | /// has type ClassExtensionPtrTy. |
| 949 | llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID); |
| 950 | |
| 951 | /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
| 952 | /// for the given class. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 953 | llvm::Value *EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 954 | const ObjCInterfaceDecl *ID); |
Fariborz Jahanian | eb80c98 | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 955 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 956 | llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder, |
| 957 | IdentifierInfo *II); |
| 958 | |
| 959 | llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder); |
| 960 | |
Fariborz Jahanian | eb80c98 | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 961 | /// EmitSuperClassRef - Emits reference to class's main metadata class. |
| 962 | llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 963 | |
| 964 | /// EmitIvarList - Emit the ivar list for the given |
| 965 | /// implementation. If ForClass is true the list of class ivars |
| 966 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 967 | /// interface ivars will be emitted. The return value has type |
| 968 | /// IvarListPtrTy. |
| 969 | llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID, |
Fariborz Jahanian | b042a59 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 970 | bool ForClass); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 971 | |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 972 | /// EmitMetaClass - Emit a forward reference to the class structure |
| 973 | /// for the metaclass of the given interface. The return value has |
| 974 | /// type ClassPtrTy. |
| 975 | llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID); |
| 976 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 977 | /// EmitMetaClass - Emit a class structure for the metaclass of the |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 978 | /// given implementation. The return value has type ClassPtrTy. |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 979 | llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID, |
| 980 | llvm::Constant *Protocols, |
Bill Wendling | cb5b5ff | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 981 | ArrayRef<llvm::Constant*> Methods); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 982 | |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 983 | llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 984 | |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 985 | llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 986 | |
| 987 | /// EmitMethodList - Emit the method list for the given |
Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 988 | /// implementation. The return value has type MethodListPtrTy. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 989 | llvm::Constant *EmitMethodList(Twine Name, |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 990 | const char *Section, |
Bill Wendling | cb5b5ff | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 991 | ArrayRef<llvm::Constant*> Methods); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 992 | |
| 993 | /// EmitMethodDescList - Emit a method description list for a list of |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 994 | /// method declarations. |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 995 | /// - TypeName: The name for the type containing the methods. |
| 996 | /// - IsProtocol: True iff these methods are for a protocol. |
| 997 | /// - ClassMethds: True iff these are class methods. |
| 998 | /// - Required: When true, only "required" methods are |
| 999 | /// listed. Similarly, when false only "optional" methods are |
| 1000 | /// listed. For classes this should always be true. |
| 1001 | /// - begin, end: The method list to output. |
| 1002 | /// |
| 1003 | /// The return value has type MethodDescriptionListPtrTy. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1004 | llvm::Constant *EmitMethodDescList(Twine Name, |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1005 | const char *Section, |
Bill Wendling | cb5b5ff | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 1006 | ArrayRef<llvm::Constant*> Methods); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1007 | |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1008 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 1009 | /// declaration, emitting it if necessary. The return value has type |
| 1010 | /// ProtocolPtrTy. |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1011 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD); |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1012 | |
| 1013 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 1014 | /// object for the given declaration, emitting it if needed. These |
| 1015 | /// forward references will be filled in with empty bodies if no |
| 1016 | /// definition is seen. The return value has type ProtocolPtrTy. |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1017 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD); |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1018 | |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1019 | /// EmitProtocolExtension - Generate the protocol extension |
| 1020 | /// structure used to store optional instance and class methods, and |
| 1021 | /// protocol properties. The return value has type |
| 1022 | /// ProtocolExtensionPtrTy. |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1023 | llvm::Constant * |
| 1024 | EmitProtocolExtension(const ObjCProtocolDecl *PD, |
Bill Wendling | cb5b5ff | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 1025 | ArrayRef<llvm::Constant*> OptInstanceMethods, |
| 1026 | ArrayRef<llvm::Constant*> OptClassMethods, |
| 1027 | ArrayRef<llvm::Constant*> MethodTypesExt); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1028 | |
| 1029 | /// EmitProtocolList - Generate the list of referenced |
| 1030 | /// protocols. The return value has type ProtocolListPtrTy. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1031 | llvm::Constant *EmitProtocolList(Twine Name, |
Daniel Dunbar | dec75f8 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1032 | ObjCProtocolDecl::protocol_iterator begin, |
| 1033 | ObjCProtocolDecl::protocol_iterator end); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1034 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1035 | /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, |
| 1036 | /// for the given selector. |
Fariborz Jahanian | 9240f3d | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 1037 | llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel, |
| 1038 | bool lval=false); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1039 | |
| 1040 | public: |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1041 | CGObjCMac(CodeGen::CodeGenModule &cgm); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1042 | |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1043 | virtual llvm::Function *ModuleInitFunction(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1044 | |
Daniel Dunbar | 97db84c | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1045 | virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
John McCall | 78a1511 | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1046 | ReturnValueSlot Return, |
Daniel Dunbar | 4b8c6db | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1047 | QualType ResultType, |
| 1048 | Selector Sel, |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1049 | llvm::Value *Receiver, |
Fariborz Jahanian | f3648b8 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1050 | const CallArgList &CallArgs, |
David Chisnall | 01aa467 | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 1051 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | f3648b8 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1052 | const ObjCMethodDecl *Method); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1053 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1054 | virtual CodeGen::RValue |
Daniel Dunbar | 97db84c | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1055 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
John McCall | 78a1511 | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1056 | ReturnValueSlot Return, |
Daniel Dunbar | 4b8c6db | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1057 | QualType ResultType, |
| 1058 | Selector Sel, |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1059 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | bac73ac | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1060 | bool isCategoryImpl, |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1061 | llvm::Value *Receiver, |
Daniel Dunbar | c722b85 | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1062 | bool IsClassMessage, |
Daniel Dunbar | aff9fca | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 1063 | const CallArgList &CallArgs, |
| 1064 | const ObjCMethodDecl *Method); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1065 | |
Daniel Dunbar | cb46385 | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1066 | virtual llvm::Value *GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1067 | const ObjCInterfaceDecl *ID); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1068 | |
Fariborz Jahanian | 9240f3d | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 1069 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel, |
| 1070 | bool lval = false); |
Fariborz Jahanian | f3648b8 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1071 | |
| 1072 | /// The NeXT/Apple runtimes do not support typed selectors; just emit an |
| 1073 | /// untyped one. |
| 1074 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, |
| 1075 | const ObjCMethodDecl *Method); |
| 1076 | |
Fariborz Jahanian | 831f0fc | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 1077 | virtual llvm::Constant *GetEHType(QualType T); |
John McCall | 2ca705e | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 1078 | |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1079 | virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1080 | |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1081 | virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1082 | |
David Chisnall | 92d436b | 2012-01-31 18:59:20 +0000 | [diff] [blame] | 1083 | virtual void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) {}; |
| 1084 | |
Daniel Dunbar | cb46385 | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1085 | virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | 89da6ad | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1086 | const ObjCProtocolDecl *PD); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1087 | |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1088 | virtual llvm::Constant *GetPropertyGetFunction(); |
| 1089 | virtual llvm::Constant *GetPropertySetFunction(); |
David Chisnall | 168b80f | 2010-12-26 22:13:16 +0000 | [diff] [blame] | 1090 | virtual llvm::Constant *GetGetStructFunction(); |
| 1091 | virtual llvm::Constant *GetSetStructFunction(); |
Fariborz Jahanian | 1e1b549 | 2012-01-06 18:07:23 +0000 | [diff] [blame] | 1092 | virtual llvm::Constant *GetCppAtomicObjectFunction(); |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1093 | virtual llvm::Constant *EnumerationMutationFunction(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1094 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1095 | virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF, |
| 1096 | const ObjCAtTryStmt &S); |
| 1097 | virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 1098 | const ObjCAtSynchronizedStmt &S); |
| 1099 | void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const Stmt &S); |
Anders Carlsson | 1963b0c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 1100 | virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
| 1101 | const ObjCAtThrowStmt &S); |
Fariborz Jahanian | 83f45b55 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 1102 | virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1103 | llvm::Value *AddrWeakObj); |
Fariborz Jahanian | 83f45b55 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 1104 | virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1105 | llvm::Value *src, llvm::Value *dst); |
Fariborz Jahanian | d7db964 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 1106 | virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 217af24 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 1107 | llvm::Value *src, llvm::Value *dest, |
| 1108 | bool threadlocal = false); |
Fariborz Jahanian | e881b53 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 1109 | virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 7a95d72 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 1110 | llvm::Value *src, llvm::Value *dest, |
| 1111 | llvm::Value *ivarOffset); |
Fariborz Jahanian | d7db964 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 1112 | virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
| 1113 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 5f21d2f | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 1114 | virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF, |
| 1115 | llvm::Value *dest, llvm::Value *src, |
Fariborz Jahanian | 021510e | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 1116 | llvm::Value *size); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1117 | |
Fariborz Jahanian | 712bfa6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1118 | virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 1119 | QualType ObjectTy, |
| 1120 | llvm::Value *BaseValue, |
| 1121 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 712bfa6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1122 | unsigned CVRQualifiers); |
Fariborz Jahanian | 21fc74c | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1123 | virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 722f424 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 1124 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | 21fc74c | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1125 | const ObjCIvarDecl *Ivar); |
Fariborz Jahanian | 7bd3d1c | 2011-05-17 22:21:16 +0000 | [diff] [blame] | 1126 | |
| 1127 | /// GetClassGlobal - Return the global variable for the Objective-C |
| 1128 | /// class of the given name. |
| 1129 | virtual llvm::GlobalVariable *GetClassGlobal(const std::string &Name) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1130 | llvm_unreachable("CGObjCMac::GetClassGlobal"); |
Fariborz Jahanian | 7bd3d1c | 2011-05-17 22:21:16 +0000 | [diff] [blame] | 1131 | } |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1132 | }; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1133 | |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1134 | class CGObjCNonFragileABIMac : public CGObjCCommonMac { |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1135 | private: |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1136 | ObjCNonFragileABITypesHelper ObjCTypes; |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1137 | llvm::GlobalVariable* ObjCEmptyCacheVar; |
| 1138 | llvm::GlobalVariable* ObjCEmptyVtableVar; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1139 | |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1140 | /// SuperClassReferences - uniqued super class references. |
| 1141 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1142 | |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1143 | /// MetaClassReferences - uniqued meta class references. |
| 1144 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences; |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1145 | |
| 1146 | /// EHTypeReferences - uniqued class ehtype references. |
| 1147 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1148 | |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1149 | /// VTableDispatchMethods - List of methods for which we generate |
| 1150 | /// vtable-based message dispatch. |
| 1151 | llvm::DenseSet<Selector> VTableDispatchMethods; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1152 | |
Fariborz Jahanian | 6726055 | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 1153 | /// DefinedMetaClasses - List of defined meta-classes. |
| 1154 | std::vector<llvm::GlobalValue*> DefinedMetaClasses; |
| 1155 | |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1156 | /// isVTableDispatchedSelector - Returns true if SEL is a |
| 1157 | /// vtable-based selector. |
| 1158 | bool isVTableDispatchedSelector(Selector Sel); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1159 | |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1160 | /// FinishNonFragileABIModule - Write out global data structures at the end of |
| 1161 | /// processing a translation unit. |
| 1162 | void FinishNonFragileABIModule(); |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1163 | |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 1164 | /// AddModuleClassList - Add the given list of class pointers to the |
| 1165 | /// module with the provided symbol and section names. |
Bill Wendling | cb5b5ff | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 1166 | void AddModuleClassList(ArrayRef<llvm::GlobalValue*> Container, |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 1167 | const char *SymbolName, |
| 1168 | const char *SectionName); |
| 1169 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1170 | llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags, |
| 1171 | unsigned InstanceStart, |
| 1172 | unsigned InstanceSize, |
| 1173 | const ObjCImplementationDecl *ID); |
Fariborz Jahanian | 4723fb7 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 1174 | llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1175 | llvm::Constant *IsAGV, |
Fariborz Jahanian | 4723fb7 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 1176 | llvm::Constant *SuperClassGV, |
Fariborz Jahanian | 8220825 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 1177 | llvm::Constant *ClassRoGV, |
| 1178 | bool HiddenVisibility); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1179 | |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1180 | llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1181 | |
Fariborz Jahanian | d9c28b8 | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 1182 | llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1183 | |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1184 | /// EmitMethodList - Emit the method list for the given |
| 1185 | /// implementation. The return value has type MethodListnfABITy. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1186 | llvm::Constant *EmitMethodList(Twine Name, |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1187 | const char *Section, |
Bill Wendling | cb5b5ff | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 1188 | ArrayRef<llvm::Constant*> Methods); |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 1189 | /// EmitIvarList - Emit the ivar list for the given |
| 1190 | /// implementation. If ForClass is true the list of class ivars |
| 1191 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 1192 | /// interface ivars will be emitted. The return value has type |
| 1193 | /// IvarListnfABIPtrTy. |
| 1194 | llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1195 | |
Fariborz Jahanian | 4e7ae06 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 1196 | llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | 3d3426f | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 1197 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 40a4bcd | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 1198 | unsigned long int offset); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1199 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1200 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 1201 | /// declaration, emitting it if necessary. The return value has type |
| 1202 | /// ProtocolPtrTy. |
| 1203 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1204 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1205 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 1206 | /// object for the given declaration, emitting it if needed. These |
| 1207 | /// forward references will be filled in with empty bodies if no |
| 1208 | /// definition is seen. The return value has type ProtocolPtrTy. |
| 1209 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1210 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1211 | /// EmitProtocolList - Generate the list of referenced |
| 1212 | /// protocols. The return value has type ProtocolListPtrTy. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1213 | llvm::Constant *EmitProtocolList(Twine Name, |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1214 | ObjCProtocolDecl::protocol_iterator begin, |
Fariborz Jahanian | 3d9296e | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 1215 | ObjCProtocolDecl::protocol_iterator end); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1216 | |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1217 | CodeGen::RValue EmitVTableMessageSend(CodeGen::CodeGenFunction &CGF, |
| 1218 | ReturnValueSlot Return, |
| 1219 | QualType ResultType, |
| 1220 | Selector Sel, |
| 1221 | llvm::Value *Receiver, |
| 1222 | QualType Arg0Ty, |
| 1223 | bool IsSuper, |
| 1224 | const CallArgList &CallArgs, |
| 1225 | const ObjCMethodDecl *Method); |
Fariborz Jahanian | 7bd3d1c | 2011-05-17 22:21:16 +0000 | [diff] [blame] | 1226 | |
Daniel Dunbar | c6928bb | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 1227 | /// GetClassGlobal - Return the global variable for the Objective-C |
| 1228 | /// class of the given name. |
Fariborz Jahanian | 899e7eb | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 1229 | llvm::GlobalVariable *GetClassGlobal(const std::string &Name); |
Fariborz Jahanian | 7bd3d1c | 2011-05-17 22:21:16 +0000 | [diff] [blame] | 1230 | |
Fariborz Jahanian | 33f66e6 | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 1231 | /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1232 | /// for the given class reference. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1233 | llvm::Value *EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1234 | const ObjCInterfaceDecl *ID); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1235 | |
| 1236 | llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder, |
| 1237 | IdentifierInfo *II); |
| 1238 | |
| 1239 | llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1240 | |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1241 | /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
| 1242 | /// for the given super class reference. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1243 | llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder, |
| 1244 | const ObjCInterfaceDecl *ID); |
| 1245 | |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1246 | /// EmitMetaClassRef - Return a Value * of the address of _class_t |
| 1247 | /// meta-data |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1248 | llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder, |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1249 | const ObjCInterfaceDecl *ID); |
| 1250 | |
Fariborz Jahanian | 4e7ae06 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 1251 | /// ObjCIvarOffsetVariable - Returns the ivar offset variable for |
| 1252 | /// the given ivar. |
| 1253 | /// |
Daniel Dunbar | a106052 | 2009-04-19 00:31:15 +0000 | [diff] [blame] | 1254 | llvm::GlobalVariable * ObjCIvarOffsetVariable( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1255 | const ObjCInterfaceDecl *ID, |
| 1256 | const ObjCIvarDecl *Ivar); |
| 1257 | |
Fariborz Jahanian | 74b7722 | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 1258 | /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, |
| 1259 | /// for the given selector. |
Fariborz Jahanian | 9240f3d | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 1260 | llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel, |
| 1261 | bool lval=false); |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1262 | |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1263 | /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1264 | /// interface. The return value has type EHTypePtrTy. |
John McCall | 2ca705e | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 1265 | llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID, |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1266 | bool ForDefinition); |
Daniel Dunbar | 15894b7 | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 1267 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1268 | const char *getMetaclassSymbolPrefix() const { |
Daniel Dunbar | 15894b7 | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 1269 | return "OBJC_METACLASS_$_"; |
| 1270 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1271 | |
Daniel Dunbar | 15894b7 | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 1272 | const char *getClassSymbolPrefix() const { |
| 1273 | return "OBJC_CLASS_$_"; |
| 1274 | } |
| 1275 | |
Daniel Dunbar | 96120237 | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 1276 | void GetClassSizeInfo(const ObjCImplementationDecl *OID, |
Daniel Dunbar | 554fd79 | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 1277 | uint32_t &InstanceStart, |
| 1278 | uint32_t &InstanceSize); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1279 | |
Fariborz Jahanian | e412864 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1280 | // Shamelessly stolen from Analysis/CFRefCount.cpp |
Daniel Dunbar | 9a017d7 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1281 | Selector GetNullarySelector(const char* name) const { |
Fariborz Jahanian | e412864 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1282 | IdentifierInfo* II = &CGM.getContext().Idents.get(name); |
| 1283 | return CGM.getContext().Selectors.getSelector(0, &II); |
| 1284 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1285 | |
Daniel Dunbar | 9a017d7 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1286 | Selector GetUnarySelector(const char* name) const { |
Fariborz Jahanian | e412864 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1287 | IdentifierInfo* II = &CGM.getContext().Idents.get(name); |
| 1288 | return CGM.getContext().Selectors.getSelector(1, &II); |
| 1289 | } |
Daniel Dunbar | 554fd79 | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 1290 | |
Daniel Dunbar | 9a017d7 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1291 | /// ImplementationIsNonLazy - Check whether the given category or |
| 1292 | /// class implementation is "non-lazy". |
Fariborz Jahanian | a6bed83 | 2009-05-21 01:03:45 +0000 | [diff] [blame] | 1293 | bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const; |
Daniel Dunbar | 9a017d7 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1294 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1295 | public: |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1296 | CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm); |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1297 | // FIXME. All stubs for now! |
| 1298 | virtual llvm::Function *ModuleInitFunction(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1299 | |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1300 | virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
John McCall | 78a1511 | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1301 | ReturnValueSlot Return, |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1302 | QualType ResultType, |
| 1303 | Selector Sel, |
| 1304 | llvm::Value *Receiver, |
Fariborz Jahanian | f3648b8 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1305 | const CallArgList &CallArgs, |
David Chisnall | 01aa467 | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 1306 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | f3648b8 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1307 | const ObjCMethodDecl *Method); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1308 | |
| 1309 | virtual CodeGen::RValue |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1310 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
John McCall | 78a1511 | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1311 | ReturnValueSlot Return, |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1312 | QualType ResultType, |
| 1313 | Selector Sel, |
| 1314 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | bac73ac | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1315 | bool isCategoryImpl, |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1316 | llvm::Value *Receiver, |
| 1317 | bool IsClassMessage, |
Daniel Dunbar | aff9fca | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 1318 | const CallArgList &CallArgs, |
| 1319 | const ObjCMethodDecl *Method); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1320 | |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1321 | virtual llvm::Value *GetClass(CGBuilderTy &Builder, |
Fariborz Jahanian | 33f66e6 | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 1322 | const ObjCInterfaceDecl *ID); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1323 | |
Fariborz Jahanian | 9240f3d | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 1324 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel, |
| 1325 | bool lvalue = false) |
| 1326 | { return EmitSelector(Builder, Sel, lvalue); } |
Fariborz Jahanian | f3648b8 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1327 | |
| 1328 | /// The NeXT/Apple runtimes do not support typed selectors; just emit an |
| 1329 | /// untyped one. |
| 1330 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, |
| 1331 | const ObjCMethodDecl *Method) |
| 1332 | { return EmitSelector(Builder, Method->getSelector()); } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1333 | |
Fariborz Jahanian | 0c8d060 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 1334 | virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1335 | |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1336 | virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); |
David Chisnall | 92d436b | 2012-01-31 18:59:20 +0000 | [diff] [blame] | 1337 | |
| 1338 | virtual void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) {}; |
| 1339 | |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1340 | virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, |
Fariborz Jahanian | 097feda | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 1341 | const ObjCProtocolDecl *PD); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1342 | |
Fariborz Jahanian | 831f0fc | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 1343 | virtual llvm::Constant *GetEHType(QualType T); |
John McCall | 2ca705e | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 1344 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1345 | virtual llvm::Constant *GetPropertyGetFunction() { |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 1346 | return ObjCTypes.getGetPropertyFn(); |
Fariborz Jahanian | 21fc74c | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1347 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1348 | virtual llvm::Constant *GetPropertySetFunction() { |
| 1349 | return ObjCTypes.getSetPropertyFn(); |
Fariborz Jahanian | 21fc74c | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1350 | } |
Fariborz Jahanian | 5a8c203 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 1351 | |
David Chisnall | 168b80f | 2010-12-26 22:13:16 +0000 | [diff] [blame] | 1352 | virtual llvm::Constant *GetSetStructFunction() { |
| 1353 | return ObjCTypes.getCopyStructFn(); |
| 1354 | } |
| 1355 | virtual llvm::Constant *GetGetStructFunction() { |
Fariborz Jahanian | 5a8c203 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 1356 | return ObjCTypes.getCopyStructFn(); |
| 1357 | } |
Fariborz Jahanian | 1e1b549 | 2012-01-06 18:07:23 +0000 | [diff] [blame] | 1358 | virtual llvm::Constant *GetCppAtomicObjectFunction() { |
| 1359 | return ObjCTypes.getCppAtomicObjectFunction(); |
| 1360 | } |
Fariborz Jahanian | 5a8c203 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 1361 | |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1362 | virtual llvm::Constant *EnumerationMutationFunction() { |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 1363 | return ObjCTypes.getEnumerationMutationFn(); |
Daniel Dunbar | d73ea816 | 2009-02-16 18:48:45 +0000 | [diff] [blame] | 1364 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1365 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1366 | virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF, |
| 1367 | const ObjCAtTryStmt &S); |
| 1368 | virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 1369 | const ObjCAtSynchronizedStmt &S); |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1370 | virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Anders Carlsson | 9ab53d1 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 1371 | const ObjCAtThrowStmt &S); |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1372 | virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1373 | llvm::Value *AddrWeakObj); |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1374 | virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1375 | llvm::Value *src, llvm::Value *dst); |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1376 | virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 217af24 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 1377 | llvm::Value *src, llvm::Value *dest, |
| 1378 | bool threadlocal = false); |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1379 | virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 7a95d72 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 1380 | llvm::Value *src, llvm::Value *dest, |
| 1381 | llvm::Value *ivarOffset); |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1382 | virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1383 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 5f21d2f | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 1384 | virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF, |
| 1385 | llvm::Value *dest, llvm::Value *src, |
Fariborz Jahanian | 021510e | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 1386 | llvm::Value *size); |
Fariborz Jahanian | 712bfa6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1387 | virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 1388 | QualType ObjectTy, |
| 1389 | llvm::Value *BaseValue, |
| 1390 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 712bfa6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1391 | unsigned CVRQualifiers); |
Fariborz Jahanian | 21fc74c | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1392 | virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 722f424 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 1393 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | 21fc74c | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1394 | const ObjCIvarDecl *Ivar); |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1395 | }; |
Fariborz Jahanian | 715fdd5 | 2012-01-29 20:27:13 +0000 | [diff] [blame] | 1396 | |
| 1397 | /// A helper class for performing the null-initialization of a return |
| 1398 | /// value. |
| 1399 | struct NullReturnState { |
| 1400 | llvm::BasicBlock *NullBB; |
| 1401 | llvm::BasicBlock *callBB; |
| 1402 | NullReturnState() : NullBB(0), callBB(0) {} |
| 1403 | |
| 1404 | void init(CodeGenFunction &CGF, llvm::Value *receiver) { |
| 1405 | // Make blocks for the null-init and call edges. |
| 1406 | NullBB = CGF.createBasicBlock("msgSend.nullinit"); |
| 1407 | callBB = CGF.createBasicBlock("msgSend.call"); |
| 1408 | |
| 1409 | // Check for a null receiver and, if there is one, jump to the |
| 1410 | // null-init test. |
| 1411 | llvm::Value *isNull = CGF.Builder.CreateIsNull(receiver); |
| 1412 | CGF.Builder.CreateCondBr(isNull, NullBB, callBB); |
| 1413 | |
| 1414 | // Otherwise, start performing the call. |
| 1415 | CGF.EmitBlock(callBB); |
| 1416 | } |
| 1417 | |
Fariborz Jahanian | f2bda69 | 2012-01-30 21:40:37 +0000 | [diff] [blame] | 1418 | RValue complete(CodeGenFunction &CGF, RValue result, QualType resultType, |
| 1419 | const CallArgList &CallArgs, |
| 1420 | const ObjCMethodDecl *Method) { |
Fariborz Jahanian | 715fdd5 | 2012-01-29 20:27:13 +0000 | [diff] [blame] | 1421 | if (!NullBB) return result; |
Fariborz Jahanian | f2bda69 | 2012-01-30 21:40:37 +0000 | [diff] [blame] | 1422 | |
| 1423 | llvm::Value *NullInitPtr = 0; |
| 1424 | if (result.isScalar() && !resultType->isVoidType()) { |
| 1425 | NullInitPtr = CGF.CreateTempAlloca(result.getScalarVal()->getType()); |
| 1426 | CGF.Builder.CreateStore(result.getScalarVal(), NullInitPtr); |
| 1427 | } |
Fariborz Jahanian | 715fdd5 | 2012-01-29 20:27:13 +0000 | [diff] [blame] | 1428 | |
| 1429 | // Finish the call path. |
| 1430 | llvm::BasicBlock *contBB = CGF.createBasicBlock("msgSend.cont"); |
| 1431 | if (CGF.HaveInsertPoint()) CGF.Builder.CreateBr(contBB); |
| 1432 | |
| 1433 | // Emit the null-init block and perform the null-initialization there. |
| 1434 | CGF.EmitBlock(NullBB); |
Fariborz Jahanian | f2bda69 | 2012-01-30 21:40:37 +0000 | [diff] [blame] | 1435 | |
| 1436 | // Release consumed arguments along the null-receiver path. |
| 1437 | if (Method) { |
| 1438 | CallArgList::const_iterator I = CallArgs.begin(); |
| 1439 | for (ObjCMethodDecl::param_const_iterator i = Method->param_begin(), |
| 1440 | e = Method->param_end(); i != e; ++i, ++I) { |
| 1441 | const ParmVarDecl *ParamDecl = (*i); |
| 1442 | if (ParamDecl->hasAttr<NSConsumedAttr>()) { |
| 1443 | RValue RV = I->RV; |
| 1444 | assert(RV.isScalar() && |
| 1445 | "NullReturnState::complete - arg not on object"); |
| 1446 | CGF.EmitARCRelease(RV.getScalarVal(), true); |
| 1447 | } |
| 1448 | } |
| 1449 | } |
| 1450 | |
| 1451 | if (result.isScalar()) { |
| 1452 | if (NullInitPtr) |
| 1453 | CGF.EmitNullInitialization(NullInitPtr, resultType); |
| 1454 | // Jump to the continuation block. |
| 1455 | CGF.EmitBlock(contBB); |
| 1456 | return NullInitPtr ? RValue::get(CGF.Builder.CreateLoad(NullInitPtr)) |
| 1457 | : result; |
| 1458 | } |
| 1459 | |
Fariborz Jahanian | 715fdd5 | 2012-01-29 20:27:13 +0000 | [diff] [blame] | 1460 | if (!resultType->isAnyComplexType()) { |
| 1461 | assert(result.isAggregate() && "null init of non-aggregate result?"); |
| 1462 | CGF.EmitNullInitialization(result.getAggregateAddr(), resultType); |
| 1463 | // Jump to the continuation block. |
| 1464 | CGF.EmitBlock(contBB); |
| 1465 | return result; |
| 1466 | } |
| 1467 | |
| 1468 | // _Complex type |
| 1469 | // FIXME. Now easy to handle any other scalar type whose result is returned |
| 1470 | // in memory due to ABI limitations. |
| 1471 | CGF.EmitBlock(contBB); |
| 1472 | CodeGenFunction::ComplexPairTy CallCV = result.getComplexVal(); |
| 1473 | llvm::Type *MemberType = CallCV.first->getType(); |
| 1474 | llvm::Constant *ZeroCV = llvm::Constant::getNullValue(MemberType); |
| 1475 | // Create phi instruction for scalar complex value. |
| 1476 | llvm::PHINode *PHIReal = CGF.Builder.CreatePHI(MemberType, 2); |
| 1477 | PHIReal->addIncoming(ZeroCV, NullBB); |
| 1478 | PHIReal->addIncoming(CallCV.first, callBB); |
| 1479 | llvm::PHINode *PHIImag = CGF.Builder.CreatePHI(MemberType, 2); |
| 1480 | PHIImag->addIncoming(ZeroCV, NullBB); |
| 1481 | PHIImag->addIncoming(CallCV.second, callBB); |
| 1482 | return RValue::getComplex(PHIReal, PHIImag); |
| 1483 | } |
| 1484 | }; |
| 1485 | |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1486 | } // end anonymous namespace |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1487 | |
| 1488 | /* *** Helper Functions *** */ |
| 1489 | |
| 1490 | /// getConstantGEP() - Help routine to construct simple GEPs. |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1491 | static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1492 | llvm::Constant *C, |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1493 | unsigned idx0, |
| 1494 | unsigned idx1) { |
| 1495 | llvm::Value *Idxs[] = { |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1496 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0), |
| 1497 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1) |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1498 | }; |
Jay Foad | ed8db7d | 2011-07-21 14:31:17 +0000 | [diff] [blame] | 1499 | return llvm::ConstantExpr::getGetElementPtr(C, Idxs); |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1500 | } |
| 1501 | |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1502 | /// hasObjCExceptionAttribute - Return true if this class or any super |
| 1503 | /// class has the __objc_exception__ attribute. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1504 | static bool hasObjCExceptionAttribute(ASTContext &Context, |
Douglas Gregor | 78bd61f | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 1505 | const ObjCInterfaceDecl *OID) { |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1506 | if (OID->hasAttr<ObjCExceptionAttr>()) |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1507 | return true; |
| 1508 | if (const ObjCInterfaceDecl *Super = OID->getSuperClass()) |
Douglas Gregor | 78bd61f | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 1509 | return hasObjCExceptionAttribute(Context, Super); |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1510 | return false; |
| 1511 | } |
| 1512 | |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1513 | /* *** CGObjCMac Public Interface *** */ |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1514 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1515 | CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1516 | ObjCTypes(cgm) { |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1517 | ObjCABI = 1; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1518 | EmitImageInfo(); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1519 | } |
| 1520 | |
Daniel Dunbar | 7c6d3a7 | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 1521 | /// GetClass - Return a reference to the class for the given interface |
| 1522 | /// decl. |
Daniel Dunbar | cb46385 | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1523 | llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1524 | const ObjCInterfaceDecl *ID) { |
| 1525 | return EmitClassRef(Builder, ID); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1526 | } |
| 1527 | |
| 1528 | /// GetSelector - Return the pointer to the unique'd string for this selector. |
Fariborz Jahanian | 9240f3d | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 1529 | llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel, |
| 1530 | bool lval) { |
| 1531 | return EmitSelector(Builder, Sel, lval); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1532 | } |
Fariborz Jahanian | f3648b8 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1533 | llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1534 | *Method) { |
Fariborz Jahanian | f3648b8 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1535 | return EmitSelector(Builder, Method->getSelector()); |
| 1536 | } |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1537 | |
Fariborz Jahanian | 831f0fc | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 1538 | llvm::Constant *CGObjCMac::GetEHType(QualType T) { |
Fariborz Jahanian | 0a3cfcc | 2011-06-22 20:21:51 +0000 | [diff] [blame] | 1539 | if (T->isObjCIdType() || |
| 1540 | T->isObjCQualifiedIdType()) { |
| 1541 | return CGM.GetAddrOfRTTIDescriptor( |
Douglas Gregor | 9767347 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 1542 | CGM.getContext().getObjCIdRedefinitionType(), /*ForEH=*/true); |
Fariborz Jahanian | 0a3cfcc | 2011-06-22 20:21:51 +0000 | [diff] [blame] | 1543 | } |
Fariborz Jahanian | 831f0fc | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 1544 | if (T->isObjCClassType() || |
| 1545 | T->isObjCQualifiedClassType()) { |
| 1546 | return CGM.GetAddrOfRTTIDescriptor( |
Douglas Gregor | 9767347 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 1547 | CGM.getContext().getObjCClassRedefinitionType(), /*ForEH=*/true); |
Fariborz Jahanian | 831f0fc | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 1548 | } |
| 1549 | if (T->isObjCObjectPointerType()) |
| 1550 | return CGM.GetAddrOfRTTIDescriptor(T, /*ForEH=*/true); |
| 1551 | |
John McCall | 2ca705e | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 1552 | llvm_unreachable("asking for catch type for ObjC type in fragile runtime"); |
John McCall | 2ca705e | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 1553 | } |
| 1554 | |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1555 | /// Generate a constant CFString object. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1556 | /* |
| 1557 | struct __builtin_CFString { |
| 1558 | const int *isa; // point to __CFConstantStringClassReference |
| 1559 | int flags; |
| 1560 | const char *str; |
| 1561 | long length; |
| 1562 | }; |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1563 | */ |
| 1564 | |
Fariborz Jahanian | 63408e8 | 2010-04-22 20:26:39 +0000 | [diff] [blame] | 1565 | /// or Generate a constant NSString object. |
| 1566 | /* |
| 1567 | struct __builtin_NSString { |
| 1568 | const int *isa; // point to __NSConstantStringClassReference |
| 1569 | const char *str; |
| 1570 | unsigned int length; |
| 1571 | }; |
| 1572 | */ |
| 1573 | |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1574 | llvm::Constant *CGObjCCommonMac::GenerateConstantString( |
David Chisnall | 481e3a8 | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 1575 | const StringLiteral *SL) { |
Fariborz Jahanian | 63408e8 | 2010-04-22 20:26:39 +0000 | [diff] [blame] | 1576 | return (CGM.getLangOptions().NoConstantCFStrings == 0 ? |
| 1577 | CGM.GetAddrOfConstantCFString(SL) : |
Fariborz Jahanian | 50c925f | 2010-10-19 17:19:29 +0000 | [diff] [blame] | 1578 | CGM.GetAddrOfConstantString(SL)); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1579 | } |
| 1580 | |
| 1581 | /// Generates a message send where the super is the receiver. This is |
| 1582 | /// a message send to self with special delivery semantics indicating |
| 1583 | /// which class's method should be called. |
Daniel Dunbar | 97db84c | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1584 | CodeGen::RValue |
| 1585 | CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
John McCall | 78a1511 | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1586 | ReturnValueSlot Return, |
Daniel Dunbar | 4b8c6db | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1587 | QualType ResultType, |
| 1588 | Selector Sel, |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1589 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | bac73ac | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1590 | bool isCategoryImpl, |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1591 | llvm::Value *Receiver, |
Daniel Dunbar | c722b85 | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1592 | bool IsClassMessage, |
Daniel Dunbar | aff9fca | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 1593 | const CodeGen::CallArgList &CallArgs, |
| 1594 | const ObjCMethodDecl *Method) { |
Daniel Dunbar | f6397fe | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1595 | // Create and init a super structure; this is a (receiver, class) |
| 1596 | // pair we will pass to objc_msgSendSuper. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1597 | llvm::Value *ObjCSuper = |
John McCall | 6647584 | 2011-03-04 08:00:29 +0000 | [diff] [blame] | 1598 | CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1599 | llvm::Value *ReceiverAsObject = |
Daniel Dunbar | f6397fe | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1600 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1601 | CGF.Builder.CreateStore(ReceiverAsObject, |
Daniel Dunbar | f6397fe | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1602 | CGF.Builder.CreateStructGEP(ObjCSuper, 0)); |
Daniel Dunbar | f6397fe | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1603 | |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1604 | // If this is a class message the metaclass is passed as the target. |
| 1605 | llvm::Value *Target; |
| 1606 | if (IsClassMessage) { |
Fariborz Jahanian | bac73ac | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1607 | if (isCategoryImpl) { |
| 1608 | // Message sent to 'super' in a class method defined in a category |
| 1609 | // implementation requires an odd treatment. |
| 1610 | // If we are in a class method, we must retrieve the |
| 1611 | // _metaclass_ for the current class, pointed at by |
| 1612 | // the class's "isa" pointer. The following assumes that |
| 1613 | // isa" is the first ivar in a class (which it must be). |
| 1614 | Target = EmitClassRef(CGF.Builder, Class->getSuperClass()); |
| 1615 | Target = CGF.Builder.CreateStructGEP(Target, 0); |
| 1616 | Target = CGF.Builder.CreateLoad(Target); |
Mike Stump | 658fe02 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 1617 | } else { |
Fariborz Jahanian | bac73ac | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1618 | llvm::Value *MetaClassPtr = EmitMetaClassRef(Class); |
| 1619 | llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1); |
| 1620 | llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr); |
| 1621 | Target = Super; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1622 | } |
Fariborz Jahanian | da2efb0 | 2009-11-14 02:18:31 +0000 | [diff] [blame] | 1623 | } |
| 1624 | else if (isCategoryImpl) |
| 1625 | Target = EmitClassRef(CGF.Builder, Class->getSuperClass()); |
| 1626 | else { |
Fariborz Jahanian | eb80c98 | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 1627 | llvm::Value *ClassPtr = EmitSuperClassRef(Class); |
| 1628 | ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1); |
| 1629 | Target = CGF.Builder.CreateLoad(ClassPtr); |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1630 | } |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1631 | // FIXME: We shouldn't need to do this cast, rectify the ASTContext and |
| 1632 | // ObjCTypes types. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1633 | llvm::Type *ClassTy = |
Daniel Dunbar | c722b85 | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1634 | CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1635 | Target = CGF.Builder.CreateBitCast(Target, ClassTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1636 | CGF.Builder.CreateStore(Target, |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1637 | CGF.Builder.CreateStructGEP(ObjCSuper, 1)); |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1638 | return EmitMessageSend(CGF, Return, ResultType, |
| 1639 | EmitSelector(CGF.Builder, Sel), |
| 1640 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
| 1641 | true, CallArgs, Method, ObjCTypes); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1642 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1643 | |
| 1644 | /// Generate code for a message send expression. |
Daniel Dunbar | 97db84c | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1645 | CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
John McCall | 78a1511 | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1646 | ReturnValueSlot Return, |
Daniel Dunbar | 4b8c6db | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1647 | QualType ResultType, |
| 1648 | Selector Sel, |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1649 | llvm::Value *Receiver, |
Fariborz Jahanian | f3648b8 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1650 | const CallArgList &CallArgs, |
David Chisnall | 01aa467 | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 1651 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | f3648b8 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1652 | const ObjCMethodDecl *Method) { |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1653 | return EmitMessageSend(CGF, Return, ResultType, |
| 1654 | EmitSelector(CGF.Builder, Sel), |
| 1655 | Receiver, CGF.getContext().getObjCIdType(), |
| 1656 | false, CallArgs, Method, ObjCTypes); |
Daniel Dunbar | 97ff50d | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1657 | } |
| 1658 | |
Daniel Dunbar | aff9fca | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 1659 | CodeGen::RValue |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1660 | CGObjCCommonMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
| 1661 | ReturnValueSlot Return, |
| 1662 | QualType ResultType, |
| 1663 | llvm::Value *Sel, |
| 1664 | llvm::Value *Arg0, |
| 1665 | QualType Arg0Ty, |
| 1666 | bool IsSuper, |
| 1667 | const CallArgList &CallArgs, |
| 1668 | const ObjCMethodDecl *Method, |
| 1669 | const ObjCCommonTypesHelper &ObjCTypes) { |
Daniel Dunbar | c722b85 | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1670 | CallArgList ActualArgs; |
Fariborz Jahanian | 969bc68 | 2009-04-24 21:07:43 +0000 | [diff] [blame] | 1671 | if (!IsSuper) |
Benjamin Kramer | 76399eb | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 1672 | Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy); |
Eli Friedman | 43dca6a | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 1673 | ActualArgs.add(RValue::get(Arg0), Arg0Ty); |
| 1674 | ActualArgs.add(RValue::get(Sel), CGF.getContext().getObjCSelType()); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1675 | ActualArgs.addFrom(CallArgs); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1676 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1677 | // If we're calling a method, use the formal signature. |
| 1678 | MessageSendInfo MSI = getMessageSendInfo(Method, ResultType, ActualArgs); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1679 | |
Anders Carlsson | 280e61f1 | 2010-06-21 20:59:55 +0000 | [diff] [blame] | 1680 | if (Method) |
| 1681 | assert(CGM.getContext().getCanonicalType(Method->getResultType()) == |
| 1682 | CGM.getContext().getCanonicalType(ResultType) && |
| 1683 | "Result type mismatch!"); |
| 1684 | |
John McCall | 5880fb8 | 2011-05-14 21:12:11 +0000 | [diff] [blame] | 1685 | NullReturnState nullReturn; |
| 1686 | |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1687 | llvm::Constant *Fn = NULL; |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1688 | if (CGM.ReturnTypeUsesSRet(MSI.CallInfo)) { |
Fariborz Jahanian | 715fdd5 | 2012-01-29 20:27:13 +0000 | [diff] [blame] | 1689 | if (!IsSuper) nullReturn.init(CGF, Arg0); |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1690 | Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1691 | : ObjCTypes.getSendStretFn(IsSuper); |
Daniel Dunbar | 6f2e839 | 2010-07-14 23:39:36 +0000 | [diff] [blame] | 1692 | } else if (CGM.ReturnTypeUsesFPRet(ResultType)) { |
| 1693 | Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper) |
| 1694 | : ObjCTypes.getSendFpretFn(IsSuper); |
Anders Carlsson | 2f1a6c3 | 2011-10-31 16:27:11 +0000 | [diff] [blame] | 1695 | } else if (CGM.ReturnTypeUsesFP2Ret(ResultType)) { |
| 1696 | Fn = (ObjCABI == 2) ? ObjCTypes.getSendFp2RetFn2(IsSuper) |
| 1697 | : ObjCTypes.getSendFp2retFn(IsSuper); |
Daniel Dunbar | 3c683f5b | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 1698 | } else { |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1699 | Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1700 | : ObjCTypes.getSendFn(IsSuper); |
Daniel Dunbar | 3c683f5b | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 1701 | } |
Fariborz Jahanian | f2bda69 | 2012-01-30 21:40:37 +0000 | [diff] [blame] | 1702 | |
| 1703 | bool requiresnullCheck = false; |
| 1704 | if (CGM.getLangOptions().ObjCAutoRefCount && Method) |
| 1705 | for (ObjCMethodDecl::param_const_iterator i = Method->param_begin(), |
| 1706 | e = Method->param_end(); i != e; ++i) { |
| 1707 | const ParmVarDecl *ParamDecl = (*i); |
| 1708 | if (ParamDecl->hasAttr<NSConsumedAttr>()) { |
| 1709 | if (!nullReturn.NullBB) |
| 1710 | nullReturn.init(CGF, Arg0); |
| 1711 | requiresnullCheck = true; |
| 1712 | break; |
| 1713 | } |
| 1714 | } |
| 1715 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1716 | Fn = llvm::ConstantExpr::getBitCast(Fn, MSI.MessengerType); |
| 1717 | RValue rvalue = CGF.EmitCall(MSI.CallInfo, Fn, Return, ActualArgs); |
Fariborz Jahanian | f2bda69 | 2012-01-30 21:40:37 +0000 | [diff] [blame] | 1718 | return nullReturn.complete(CGF, rvalue, ResultType, CallArgs, |
| 1719 | requiresnullCheck ? Method : 0); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1720 | } |
| 1721 | |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1722 | static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) { |
| 1723 | if (FQT.isObjCGCStrong()) |
| 1724 | return Qualifiers::Strong; |
| 1725 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1726 | if (FQT.isObjCGCWeak() || FQT.getObjCLifetime() == Qualifiers::OCL_Weak) |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1727 | return Qualifiers::Weak; |
| 1728 | |
Fariborz Jahanian | 430b35e | 2012-02-16 00:15:02 +0000 | [diff] [blame] | 1729 | // check for __unsafe_unretained |
| 1730 | if (FQT.getObjCLifetime() == Qualifiers::OCL_ExplicitNone) |
| 1731 | return Qualifiers::GCNone; |
| 1732 | |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1733 | if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType()) |
| 1734 | return Qualifiers::Strong; |
| 1735 | |
| 1736 | if (const PointerType *PT = FQT->getAs<PointerType>()) |
| 1737 | return GetGCAttrTypeForType(Ctx, PT->getPointeeType()); |
| 1738 | |
| 1739 | return Qualifiers::GCNone; |
| 1740 | } |
| 1741 | |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1742 | llvm::Constant *CGObjCCommonMac::BuildGCBlockLayout(CodeGenModule &CGM, |
| 1743 | const CGBlockInfo &blockInfo) { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1744 | llvm::Constant *nullPtr = llvm::Constant::getNullValue(CGM.Int8PtrTy); |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1745 | |
Douglas Gregor | 79a9141 | 2011-09-13 17:21:33 +0000 | [diff] [blame] | 1746 | if (CGM.getLangOptions().getGC() == LangOptions::NonGC && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1747 | !CGM.getLangOptions().ObjCAutoRefCount) |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1748 | return nullPtr; |
| 1749 | |
Fariborz Jahanian | f95e358 | 2010-08-06 16:28:55 +0000 | [diff] [blame] | 1750 | bool hasUnion = false; |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1751 | SkipIvars.clear(); |
| 1752 | IvarsInfo.clear(); |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1753 | unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0); |
| 1754 | unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth(); |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1755 | |
Fariborz Jahanian | cfddabf | 2010-09-09 00:21:45 +0000 | [diff] [blame] | 1756 | // __isa is the first field in block descriptor and must assume by runtime's |
| 1757 | // convention that it is GC'able. |
| 1758 | IvarsInfo.push_back(GC_IVAR(0, 1)); |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1759 | |
| 1760 | const BlockDecl *blockDecl = blockInfo.getBlockDecl(); |
| 1761 | |
| 1762 | // Calculate the basic layout of the block structure. |
| 1763 | const llvm::StructLayout *layout = |
| 1764 | CGM.getTargetData().getStructLayout(blockInfo.StructureType); |
| 1765 | |
| 1766 | // Ignore the optional 'this' capture: C++ objects are not assumed |
| 1767 | // to be GC'ed. |
| 1768 | |
| 1769 | // Walk the captured variables. |
| 1770 | for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(), |
| 1771 | ce = blockDecl->capture_end(); ci != ce; ++ci) { |
| 1772 | const VarDecl *variable = ci->getVariable(); |
| 1773 | QualType type = variable->getType(); |
| 1774 | |
| 1775 | const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable); |
| 1776 | |
| 1777 | // Ignore constant captures. |
| 1778 | if (capture.isConstant()) continue; |
| 1779 | |
| 1780 | uint64_t fieldOffset = layout->getElementOffset(capture.getIndex()); |
| 1781 | |
| 1782 | // __block variables are passed by their descriptor address. |
| 1783 | if (ci->isByRef()) { |
| 1784 | IvarsInfo.push_back(GC_IVAR(fieldOffset, /*size in words*/ 1)); |
Fariborz Jahanian | 933c672 | 2010-09-11 01:27:29 +0000 | [diff] [blame] | 1785 | continue; |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1786 | } |
| 1787 | |
| 1788 | assert(!type->isArrayType() && "array variable should not be caught"); |
| 1789 | if (const RecordType *record = type->getAs<RecordType>()) { |
| 1790 | BuildAggrIvarRecordLayout(record, fieldOffset, true, hasUnion); |
Fariborz Jahanian | 903aba3 | 2010-08-05 21:00:25 +0000 | [diff] [blame] | 1791 | continue; |
| 1792 | } |
Fariborz Jahanian | f95e358 | 2010-08-06 16:28:55 +0000 | [diff] [blame] | 1793 | |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1794 | Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), type); |
| 1795 | unsigned fieldSize = CGM.getContext().getTypeSize(type); |
| 1796 | |
| 1797 | if (GCAttr == Qualifiers::Strong) |
| 1798 | IvarsInfo.push_back(GC_IVAR(fieldOffset, |
| 1799 | fieldSize / WordSizeInBits)); |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1800 | else if (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak) |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1801 | SkipIvars.push_back(GC_IVAR(fieldOffset, |
| 1802 | fieldSize / ByteSizeInBits)); |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1803 | } |
| 1804 | |
| 1805 | if (IvarsInfo.empty()) |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1806 | return nullPtr; |
| 1807 | |
| 1808 | // Sort on byte position; captures might not be allocated in order, |
| 1809 | // and unions can do funny things. |
| 1810 | llvm::array_pod_sort(IvarsInfo.begin(), IvarsInfo.end()); |
| 1811 | llvm::array_pod_sort(SkipIvars.begin(), SkipIvars.end()); |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1812 | |
| 1813 | std::string BitMap; |
Fariborz Jahanian | 1f78a9a | 2010-08-05 00:19:48 +0000 | [diff] [blame] | 1814 | llvm::Constant *C = BuildIvarLayoutBitmap(BitMap); |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1815 | if (CGM.getLangOptions().ObjCGCBitmapPrint) { |
| 1816 | printf("\n block variable layout for block: "); |
| 1817 | const unsigned char *s = (unsigned char*)BitMap.c_str(); |
Bill Wendling | 5313685 | 2012-02-07 09:06:01 +0000 | [diff] [blame] | 1818 | for (unsigned i = 0, e = BitMap.size(); i < e; i++) |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1819 | if (!(s[i] & 0xf0)) |
| 1820 | printf("0x0%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 1821 | else |
| 1822 | printf("0x%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 1823 | printf("\n"); |
| 1824 | } |
| 1825 | |
| 1826 | return C; |
Fariborz Jahanian | c05349e | 2010-08-04 16:57:49 +0000 | [diff] [blame] | 1827 | } |
| 1828 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1829 | llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | 89da6ad | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1830 | const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 7050c55 | 2008-09-04 04:33:15 +0000 | [diff] [blame] | 1831 | // 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] | 1832 | // resolved. Investigate. Its also wasteful to look this up over and over. |
Daniel Dunbar | 7050c55 | 2008-09-04 04:33:15 +0000 | [diff] [blame] | 1833 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); |
| 1834 | |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1835 | return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD), |
Douglas Gregor | 020de32 | 2012-01-17 18:36:30 +0000 | [diff] [blame] | 1836 | ObjCTypes.getExternalProtocolPtrTy()); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1837 | } |
| 1838 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1839 | void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) { |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1840 | // FIXME: We shouldn't need this, the protocol decl should contain enough |
| 1841 | // information to tell us whether this was a declaration or a definition. |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1842 | DefinedProtocols.insert(PD->getIdentifier()); |
| 1843 | |
| 1844 | // If we have generated a forward reference to this protocol, emit |
| 1845 | // it now. Otherwise do nothing, the protocol objects are lazily |
| 1846 | // emitted. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1847 | if (Protocols.count(PD->getIdentifier())) |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1848 | GetOrEmitProtocol(PD); |
| 1849 | } |
| 1850 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1851 | llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1852 | if (DefinedProtocols.count(PD->getIdentifier())) |
| 1853 | return GetOrEmitProtocol(PD); |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 1854 | |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1855 | return GetOrEmitProtocolRef(PD); |
| 1856 | } |
| 1857 | |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1858 | /* |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1859 | // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions |
| 1860 | struct _objc_protocol { |
| 1861 | struct _objc_protocol_extension *isa; |
| 1862 | char *protocol_name; |
| 1863 | struct _objc_protocol_list *protocol_list; |
| 1864 | struct _objc__method_prototype_list *instance_methods; |
| 1865 | struct _objc__method_prototype_list *class_methods |
| 1866 | }; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1867 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1868 | See EmitProtocolExtension(). |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1869 | */ |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1870 | llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) { |
| 1871 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 1872 | |
| 1873 | // Early exit if a defining object has already been generated. |
| 1874 | if (Entry && Entry->hasInitializer()) |
| 1875 | return Entry; |
| 1876 | |
Douglas Gregor | a715bff | 2012-01-01 19:51:50 +0000 | [diff] [blame] | 1877 | // Use the protocol definition, if there is one. |
| 1878 | if (const ObjCProtocolDecl *Def = PD->getDefinition()) |
| 1879 | PD = Def; |
| 1880 | |
Daniel Dunbar | c61d0e9 | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1881 | // 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] | 1882 | // resolved. Investigate. Its also wasteful to look this up over and over. |
Daniel Dunbar | c61d0e9 | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1883 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); |
| 1884 | |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1885 | // Construct method lists. |
| 1886 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 1887 | std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 1888 | std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1889 | for (ObjCProtocolDecl::instmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1890 | i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) { |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1891 | ObjCMethodDecl *MD = *i; |
| 1892 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 1893 | if (!C) |
| 1894 | return GetOrEmitProtocolRef(PD); |
| 1895 | |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1896 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 1897 | OptInstanceMethods.push_back(C); |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 1898 | OptMethodTypesExt.push_back(GetMethodVarType(MD, true)); |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1899 | } else { |
| 1900 | InstanceMethods.push_back(C); |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 1901 | MethodTypesExt.push_back(GetMethodVarType(MD, true)); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1902 | } |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1903 | } |
| 1904 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1905 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1906 | i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) { |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1907 | ObjCMethodDecl *MD = *i; |
| 1908 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 1909 | if (!C) |
| 1910 | return GetOrEmitProtocolRef(PD); |
| 1911 | |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1912 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 1913 | OptClassMethods.push_back(C); |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 1914 | OptMethodTypesExt.push_back(GetMethodVarType(MD, true)); |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1915 | } else { |
| 1916 | ClassMethods.push_back(C); |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 1917 | MethodTypesExt.push_back(GetMethodVarType(MD, true)); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1918 | } |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1919 | } |
| 1920 | |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 1921 | MethodTypesExt.insert(MethodTypesExt.end(), |
| 1922 | OptMethodTypesExt.begin(), OptMethodTypesExt.end()); |
| 1923 | |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 1924 | llvm::Constant *Values[] = { |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 1925 | EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods, |
| 1926 | MethodTypesExt), |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 1927 | GetClassName(PD->getIdentifier()), |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1928 | EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(), |
Daniel Dunbar | dec75f8 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1929 | PD->protocol_begin(), |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 1930 | PD->protocol_end()), |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1931 | EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(), |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1932 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 1933 | InstanceMethods), |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1934 | EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(), |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1935 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 1936 | ClassMethods) |
| 1937 | }; |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 1938 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1939 | Values); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1940 | |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1941 | if (Entry) { |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1942 | // Already created, fix the linkage and update the initializer. |
| 1943 | Entry->setLinkage(llvm::GlobalValue::InternalLinkage); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1944 | Entry->setInitializer(Init); |
| 1945 | } else { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1946 | Entry = |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 1947 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false, |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1948 | llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1949 | Init, |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1950 | "\01L_OBJC_PROTOCOL_" + PD->getName()); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1951 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1952 | // FIXME: Is this necessary? Why only for protocol? |
| 1953 | Entry->setAlignment(4); |
| 1954 | } |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 1955 | CGM.AddUsedGlobal(Entry); |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1956 | |
| 1957 | return Entry; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1958 | } |
| 1959 | |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1960 | llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1961 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 1962 | |
| 1963 | if (!Entry) { |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1964 | // We use the initializer as a marker of whether this is a forward |
| 1965 | // reference or not. At module finalization we add the empty |
| 1966 | // contents for protocols which were referenced but never defined. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1967 | Entry = |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 1968 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false, |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1969 | llvm::GlobalValue::ExternalLinkage, |
| 1970 | 0, |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1971 | "\01L_OBJC_PROTOCOL_" + PD->getName()); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1972 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1973 | // FIXME: Is this necessary? Why only for protocol? |
| 1974 | Entry->setAlignment(4); |
| 1975 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1976 | |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1977 | return Entry; |
| 1978 | } |
| 1979 | |
| 1980 | /* |
| 1981 | struct _objc_protocol_extension { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1982 | uint32_t size; |
| 1983 | struct objc_method_description_list *optional_instance_methods; |
| 1984 | struct objc_method_description_list *optional_class_methods; |
| 1985 | struct objc_property_list *instance_properties; |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 1986 | const char ** extendedMethodTypes; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1987 | }; |
| 1988 | */ |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1989 | llvm::Constant * |
| 1990 | CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD, |
Bill Wendling | cb5b5ff | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 1991 | ArrayRef<llvm::Constant*> OptInstanceMethods, |
| 1992 | ArrayRef<llvm::Constant*> OptClassMethods, |
| 1993 | ArrayRef<llvm::Constant*> MethodTypesExt) { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1994 | uint64_t Size = |
Duncan Sands | c76fe8b | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 1995 | CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy); |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 1996 | llvm::Constant *Values[] = { |
| 1997 | llvm::ConstantInt::get(ObjCTypes.IntTy, Size), |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1998 | EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_" |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1999 | + PD->getName(), |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2000 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2001 | OptInstanceMethods), |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2002 | EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(), |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2003 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2004 | OptClassMethods), |
| 2005 | EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(), 0, PD, |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 2006 | ObjCTypes), |
| 2007 | EmitProtocolMethodTypes("\01L_OBJC_PROTOCOL_METHOD_TYPES_" + PD->getName(), |
| 2008 | MethodTypesExt, ObjCTypes) |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2009 | }; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2010 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2011 | // Return null if no extension bits are used. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2012 | if (Values[1]->isNullValue() && Values[2]->isNullValue() && |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 2013 | Values[3]->isNullValue() && Values[4]->isNullValue()) |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2014 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2015 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2016 | llvm::Constant *Init = |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2017 | llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2018 | |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2019 | // No special section, but goes in llvm.used |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2020 | return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(), |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2021 | Init, |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2022 | 0, 0, true); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2023 | } |
| 2024 | |
| 2025 | /* |
| 2026 | struct objc_protocol_list { |
Bill Wendling | a515b58 | 2012-02-09 22:16:49 +0000 | [diff] [blame] | 2027 | struct objc_protocol_list *next; |
| 2028 | long count; |
| 2029 | Protocol *list[]; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2030 | }; |
| 2031 | */ |
Daniel Dunbar | dec75f8 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 2032 | llvm::Constant * |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2033 | CGObjCMac::EmitProtocolList(Twine Name, |
Daniel Dunbar | dec75f8 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 2034 | ObjCProtocolDecl::protocol_iterator begin, |
| 2035 | ObjCProtocolDecl::protocol_iterator end) { |
Bill Wendling | a515b58 | 2012-02-09 22:16:49 +0000 | [diff] [blame] | 2036 | llvm::SmallVector<llvm::Constant*, 16> ProtocolRefs; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2037 | |
Daniel Dunbar | dec75f8 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 2038 | for (; begin != end; ++begin) |
| 2039 | ProtocolRefs.push_back(GetProtocolRef(*begin)); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2040 | |
| 2041 | // Just return null for empty protocol lists |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2042 | if (ProtocolRefs.empty()) |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2043 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2044 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2045 | // This list is null terminated. |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2046 | ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy)); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2047 | |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2048 | llvm::Constant *Values[3]; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2049 | // This field is only used by the runtime. |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2050 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2051 | Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2052 | ProtocolRefs.size() - 1); |
| 2053 | Values[2] = |
| 2054 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy, |
| 2055 | ProtocolRefs.size()), |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2056 | ProtocolRefs); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2057 | |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2058 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2059 | llvm::GlobalVariable *GV = |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2060 | CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Daniel Dunbar | ae33384 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2061 | 4, false); |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2062 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2063 | } |
| 2064 | |
Bill Wendling | cb5b5ff | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 2065 | void CGObjCCommonMac:: |
| 2066 | PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*,16> &PropertySet, |
Bill Wendling | a515b58 | 2012-02-09 22:16:49 +0000 | [diff] [blame] | 2067 | llvm::SmallVectorImpl<llvm::Constant*> &Properties, |
Bill Wendling | cb5b5ff | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 2068 | const Decl *Container, |
| 2069 | const ObjCProtocolDecl *PROTO, |
| 2070 | const ObjCCommonTypesHelper &ObjCTypes) { |
Fariborz Jahanian | 751c1e7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 2071 | for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(), |
| 2072 | E = PROTO->protocol_end(); P != E; ++P) |
| 2073 | PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes); |
| 2074 | for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(), |
| 2075 | E = PROTO->prop_end(); I != E; ++I) { |
| 2076 | const ObjCPropertyDecl *PD = *I; |
| 2077 | if (!PropertySet.insert(PD->getIdentifier())) |
| 2078 | continue; |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2079 | llvm::Constant *Prop[] = { |
| 2080 | GetPropertyName(PD->getIdentifier()), |
| 2081 | GetPropertyTypeString(PD, Container) |
| 2082 | }; |
Fariborz Jahanian | 751c1e7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 2083 | Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop)); |
| 2084 | } |
| 2085 | } |
| 2086 | |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2087 | /* |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2088 | struct _objc_property { |
Bill Wendling | a515b58 | 2012-02-09 22:16:49 +0000 | [diff] [blame] | 2089 | const char * const name; |
| 2090 | const char * const attributes; |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2091 | }; |
| 2092 | |
| 2093 | struct _objc_property_list { |
Bill Wendling | a515b58 | 2012-02-09 22:16:49 +0000 | [diff] [blame] | 2094 | uint32_t entsize; // sizeof (struct _objc_property) |
| 2095 | uint32_t prop_count; |
| 2096 | struct _objc_property[prop_count]; |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2097 | }; |
| 2098 | */ |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2099 | llvm::Constant *CGObjCCommonMac::EmitPropertyList(Twine Name, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2100 | const Decl *Container, |
| 2101 | const ObjCContainerDecl *OCD, |
| 2102 | const ObjCCommonTypesHelper &ObjCTypes) { |
Bill Wendling | a515b58 | 2012-02-09 22:16:49 +0000 | [diff] [blame] | 2103 | llvm::SmallVector<llvm::Constant*, 16> Properties; |
Fariborz Jahanian | 751c1e7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 2104 | llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2105 | for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(), |
| 2106 | E = OCD->prop_end(); I != E; ++I) { |
Steve Naroff | ba3dc38 | 2009-01-11 12:47:58 +0000 | [diff] [blame] | 2107 | const ObjCPropertyDecl *PD = *I; |
Fariborz Jahanian | 751c1e7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 2108 | PropertySet.insert(PD->getIdentifier()); |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2109 | llvm::Constant *Prop[] = { |
| 2110 | GetPropertyName(PD->getIdentifier()), |
| 2111 | GetPropertyTypeString(PD, Container) |
| 2112 | }; |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2113 | Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2114 | Prop)); |
| 2115 | } |
Fariborz Jahanian | 7966aff | 2010-06-22 16:33:55 +0000 | [diff] [blame] | 2116 | if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) { |
Ted Kremenek | 0ef508d | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 2117 | for (ObjCInterfaceDecl::all_protocol_iterator |
| 2118 | P = OID->all_referenced_protocol_begin(), |
| 2119 | E = OID->all_referenced_protocol_end(); P != E; ++P) |
Fariborz Jahanian | 7966aff | 2010-06-22 16:33:55 +0000 | [diff] [blame] | 2120 | PushProtocolProperties(PropertySet, Properties, Container, (*P), |
| 2121 | ObjCTypes); |
| 2122 | } |
| 2123 | else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) { |
| 2124 | for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(), |
| 2125 | E = CD->protocol_end(); P != E; ++P) |
| 2126 | PushProtocolProperties(PropertySet, Properties, Container, (*P), |
| 2127 | ObjCTypes); |
| 2128 | } |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2129 | |
| 2130 | // Return null for empty list. |
| 2131 | if (Properties.empty()) |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2132 | return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2133 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2134 | unsigned PropertySize = |
Duncan Sands | c76fe8b | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2135 | CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy); |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2136 | llvm::Constant *Values[3]; |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2137 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize); |
| 2138 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size()); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2139 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy, |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2140 | Properties.size()); |
Owen Anderson | 47034e1 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 2141 | Values[2] = llvm::ConstantArray::get(AT, Properties); |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2142 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2143 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2144 | llvm::GlobalVariable *GV = |
| 2145 | CreateMetadataVar(Name, Init, |
| 2146 | (ObjCABI == 2) ? "__DATA, __objc_const" : |
Daniel Dunbar | b25452a | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2147 | "__OBJC,__property,regular,no_dead_strip", |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2148 | (ObjCABI == 2) ? 8 : 4, |
Daniel Dunbar | b25452a | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2149 | true); |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2150 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy); |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2151 | } |
| 2152 | |
Bill Wendling | cb5b5ff | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 2153 | llvm::Constant * |
| 2154 | CGObjCCommonMac::EmitProtocolMethodTypes(Twine Name, |
| 2155 | ArrayRef<llvm::Constant*> MethodTypes, |
| 2156 | const ObjCCommonTypesHelper &ObjCTypes) { |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 2157 | // Return null for empty list. |
| 2158 | if (MethodTypes.empty()) |
| 2159 | return llvm::Constant::getNullValue(ObjCTypes.Int8PtrPtrTy); |
| 2160 | |
| 2161 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
| 2162 | MethodTypes.size()); |
| 2163 | llvm::Constant *Init = llvm::ConstantArray::get(AT, MethodTypes); |
| 2164 | |
| 2165 | llvm::GlobalVariable *GV = |
| 2166 | CreateMetadataVar(Name, Init, |
| 2167 | (ObjCABI == 2) ? "__DATA, __objc_const" : 0, |
| 2168 | (ObjCABI == 2) ? 8 : 4, |
| 2169 | true); |
| 2170 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.Int8PtrPtrTy); |
| 2171 | } |
| 2172 | |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2173 | /* |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2174 | struct objc_method_description_list { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2175 | int count; |
| 2176 | struct objc_method_description list[]; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2177 | }; |
| 2178 | */ |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2179 | llvm::Constant * |
| 2180 | CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2181 | llvm::Constant *Desc[] = { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2182 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2183 | ObjCTypes.SelectorPtrTy), |
| 2184 | GetMethodVarType(MD) |
| 2185 | }; |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 2186 | if (!Desc[1]) |
| 2187 | return 0; |
| 2188 | |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2189 | return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy, |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2190 | Desc); |
| 2191 | } |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2192 | |
Bill Wendling | cb5b5ff | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 2193 | llvm::Constant * |
| 2194 | CGObjCMac::EmitMethodDescList(Twine Name, const char *Section, |
| 2195 | ArrayRef<llvm::Constant*> Methods) { |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2196 | // Return null for empty list. |
| 2197 | if (Methods.empty()) |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2198 | return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2199 | |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2200 | llvm::Constant *Values[2]; |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2201 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2202 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy, |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2203 | Methods.size()); |
Owen Anderson | 47034e1 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 2204 | Values[1] = llvm::ConstantArray::get(AT, Methods); |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2205 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2206 | |
Daniel Dunbar | b25452a | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2207 | llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2208 | return llvm::ConstantExpr::getBitCast(GV, |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2209 | ObjCTypes.MethodDescriptionListPtrTy); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2210 | } |
| 2211 | |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2212 | /* |
| 2213 | struct _objc_category { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2214 | char *category_name; |
| 2215 | char *class_name; |
| 2216 | struct _objc_method_list *instance_methods; |
| 2217 | struct _objc_method_list *class_methods; |
| 2218 | struct _objc_protocol_list *protocols; |
| 2219 | uint32_t size; // <rdar://4585769> |
| 2220 | struct _objc_property_list *instance_properties; |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2221 | }; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2222 | */ |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2223 | void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) { |
Duncan Sands | c76fe8b | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2224 | unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy); |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2225 | |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 2226 | // FIXME: This is poor design, the OCD should have a pointer to the category |
| 2227 | // decl. Additionally, note that Category can be null for the @implementation |
| 2228 | // w/o an @interface case. Sema should just create one for us as it does for |
| 2229 | // @implementation so everyone else can live life under a clear blue sky. |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2230 | const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2231 | const ObjCCategoryDecl *Category = |
Daniel Dunbar | 28e76ca | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 2232 | Interface->FindCategoryDeclaration(OCD->getIdentifier()); |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2233 | |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 2234 | SmallString<256> ExtName; |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2235 | llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_' |
| 2236 | << OCD->getName(); |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2237 | |
Bill Wendling | a515b58 | 2012-02-09 22:16:49 +0000 | [diff] [blame] | 2238 | llvm::SmallVector<llvm::Constant*, 16> InstanceMethods, ClassMethods; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2239 | for (ObjCCategoryImplDecl::instmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2240 | i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) { |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2241 | // Instance methods should always be defined. |
| 2242 | InstanceMethods.push_back(GetMethodConstant(*i)); |
| 2243 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2244 | for (ObjCCategoryImplDecl::classmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2245 | i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) { |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2246 | // Class methods should always be defined. |
| 2247 | ClassMethods.push_back(GetMethodConstant(*i)); |
| 2248 | } |
| 2249 | |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2250 | llvm::Constant *Values[7]; |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2251 | Values[0] = GetClassName(OCD->getIdentifier()); |
| 2252 | Values[1] = GetClassName(Interface->getIdentifier()); |
Fariborz Jahanian | e55f866 | 2009-04-29 20:40:05 +0000 | [diff] [blame] | 2253 | LazySymbols.insert(Interface->getIdentifier()); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2254 | Values[2] = |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2255 | EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(), |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2256 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2257 | InstanceMethods); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2258 | Values[3] = |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2259 | EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(), |
Daniel Dunbar | b25452a | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2260 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2261 | ClassMethods); |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2262 | if (Category) { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2263 | Values[4] = |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2264 | EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(), |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2265 | Category->protocol_begin(), |
| 2266 | Category->protocol_end()); |
| 2267 | } else { |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2268 | Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2269 | } |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2270 | Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | 28e76ca | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 2271 | |
| 2272 | // If there is no category @interface then there can be no properties. |
| 2273 | if (Category) { |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2274 | Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(), |
Fariborz Jahanian | 066347e | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 2275 | OCD, Category, ObjCTypes); |
Daniel Dunbar | 28e76ca | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 2276 | } else { |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2277 | Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
Daniel Dunbar | 28e76ca | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 2278 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2279 | |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2280 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy, |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2281 | Values); |
| 2282 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2283 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2284 | CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init, |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2285 | "__OBJC,__category,regular,no_dead_strip", |
Daniel Dunbar | ae33384 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2286 | 4, true); |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2287 | DefinedCategories.push_back(GV); |
Fariborz Jahanian | 9adb2e6 | 2010-06-21 22:05:18 +0000 | [diff] [blame] | 2288 | DefinedCategoryNames.insert(ExtName.str()); |
Fariborz Jahanian | c057794 | 2011-04-22 22:02:28 +0000 | [diff] [blame] | 2289 | // method definition entries must be clear for next implementation. |
| 2290 | MethodDefinitions.clear(); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2291 | } |
| 2292 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2293 | // FIXME: Get from somewhere? |
| 2294 | enum ClassFlags { |
| 2295 | eClassFlags_Factory = 0x00001, |
| 2296 | eClassFlags_Meta = 0x00002, |
| 2297 | // <rdr://5142207> |
| 2298 | eClassFlags_HasCXXStructors = 0x02000, |
| 2299 | eClassFlags_Hidden = 0x20000, |
| 2300 | eClassFlags_ABI2_Hidden = 0x00010, |
| 2301 | eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634> |
| 2302 | }; |
| 2303 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2304 | /* |
| 2305 | struct _objc_class { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2306 | Class isa; |
| 2307 | Class super_class; |
| 2308 | const char *name; |
| 2309 | long version; |
| 2310 | long info; |
| 2311 | long instance_size; |
| 2312 | struct _objc_ivar_list *ivars; |
| 2313 | struct _objc_method_list *methods; |
| 2314 | struct _objc_cache *cache; |
| 2315 | struct _objc_protocol_list *protocols; |
| 2316 | // Objective-C 1.0 extensions (<rdr://4585769>) |
| 2317 | const char *ivar_layout; |
| 2318 | struct _objc_class_ext *ext; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2319 | }; |
| 2320 | |
| 2321 | See EmitClassExtension(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2322 | */ |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2323 | void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) { |
Daniel Dunbar | c61d0e9 | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2324 | DefinedSymbols.insert(ID->getIdentifier()); |
| 2325 | |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 2326 | std::string ClassName = ID->getNameAsString(); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2327 | // FIXME: Gross |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2328 | ObjCInterfaceDecl *Interface = |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2329 | const_cast<ObjCInterfaceDecl*>(ID->getClassInterface()); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2330 | llvm::Constant *Protocols = |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2331 | EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(), |
Ted Kremenek | 0ef508d | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 2332 | Interface->all_referenced_protocol_begin(), |
| 2333 | Interface->all_referenced_protocol_end()); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2334 | unsigned Flags = eClassFlags_Factory; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2335 | if (ID->hasCXXStructors()) |
Fariborz Jahanian | 0dec1e0 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 2336 | Flags |= eClassFlags_HasCXXStructors; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2337 | unsigned Size = |
Ken Dyck | c8ae550 | 2011-02-09 01:59:34 +0000 | [diff] [blame] | 2338 | CGM.getContext().getASTObjCImplementationLayout(ID).getSize().getQuantity(); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2339 | |
| 2340 | // FIXME: Set CXX-structors flag. |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 2341 | if (ID->getClassInterface()->getVisibility() == HiddenVisibility) |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2342 | Flags |= eClassFlags_Hidden; |
| 2343 | |
Bill Wendling | a515b58 | 2012-02-09 22:16:49 +0000 | [diff] [blame] | 2344 | llvm::SmallVector<llvm::Constant*, 16> InstanceMethods, ClassMethods; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2345 | for (ObjCImplementationDecl::instmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2346 | i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) { |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2347 | // Instance methods should always be defined. |
| 2348 | InstanceMethods.push_back(GetMethodConstant(*i)); |
| 2349 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2350 | for (ObjCImplementationDecl::classmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2351 | i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) { |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2352 | // Class methods should always be defined. |
| 2353 | ClassMethods.push_back(GetMethodConstant(*i)); |
| 2354 | } |
| 2355 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2356 | for (ObjCImplementationDecl::propimpl_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2357 | i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) { |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2358 | ObjCPropertyImplDecl *PID = *i; |
| 2359 | |
| 2360 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) { |
| 2361 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 2362 | |
| 2363 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) |
| 2364 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 2365 | InstanceMethods.push_back(C); |
| 2366 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) |
| 2367 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 2368 | InstanceMethods.push_back(C); |
| 2369 | } |
| 2370 | } |
| 2371 | |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2372 | llvm::Constant *Values[12]; |
Daniel Dunbar | ccf6183 | 2009-05-03 08:56:52 +0000 | [diff] [blame] | 2373 | Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2374 | if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) { |
Daniel Dunbar | c61d0e9 | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2375 | // Record a reference to the super class. |
| 2376 | LazySymbols.insert(Super->getIdentifier()); |
| 2377 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2378 | Values[ 1] = |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2379 | llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2380 | ObjCTypes.ClassPtrTy); |
| 2381 | } else { |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2382 | Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2383 | } |
| 2384 | Values[ 2] = GetClassName(ID->getIdentifier()); |
| 2385 | // Version is always 0. |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2386 | Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 2387 | Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); |
| 2388 | Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Fariborz Jahanian | b042a59 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2389 | Values[ 6] = EmitIvarList(ID, false); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2390 | Values[ 7] = |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2391 | EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(), |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2392 | "__OBJC,__inst_meth,regular,no_dead_strip", |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2393 | InstanceMethods); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2394 | // cache is always NULL. |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2395 | Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2396 | Values[ 9] = Protocols; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2397 | Values[10] = BuildIvarLayout(ID, true); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2398 | Values[11] = EmitClassExtension(ID); |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2399 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2400 | Values); |
Fariborz Jahanian | eb80c98 | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 2401 | std::string Name("\01L_OBJC_CLASS_"); |
| 2402 | Name += ClassName; |
| 2403 | const char *Section = "__OBJC,__class,regular,no_dead_strip"; |
| 2404 | // Check for a forward reference. |
| 2405 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 2406 | if (GV) { |
| 2407 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 2408 | "Forward metaclass reference has incorrect type."); |
| 2409 | GV->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 2410 | GV->setInitializer(Init); |
| 2411 | GV->setSection(Section); |
| 2412 | GV->setAlignment(4); |
| 2413 | CGM.AddUsedGlobal(GV); |
| 2414 | } |
| 2415 | else |
| 2416 | GV = CreateMetadataVar(Name, Init, Section, 4, true); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2417 | DefinedClasses.push_back(GV); |
Fariborz Jahanian | c057794 | 2011-04-22 22:02:28 +0000 | [diff] [blame] | 2418 | // method definition entries must be clear for next implementation. |
| 2419 | MethodDefinitions.clear(); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2420 | } |
| 2421 | |
| 2422 | llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID, |
| 2423 | llvm::Constant *Protocols, |
Bill Wendling | cb5b5ff | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 2424 | ArrayRef<llvm::Constant*> Methods) { |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2425 | unsigned Flags = eClassFlags_Meta; |
Duncan Sands | c76fe8b | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2426 | unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2427 | |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 2428 | if (ID->getClassInterface()->getVisibility() == HiddenVisibility) |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2429 | Flags |= eClassFlags_Hidden; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2430 | |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2431 | llvm::Constant *Values[12]; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2432 | // The isa for the metaclass is the root of the hierarchy. |
| 2433 | const ObjCInterfaceDecl *Root = ID->getClassInterface(); |
| 2434 | while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) |
| 2435 | Root = Super; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2436 | Values[ 0] = |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2437 | llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()), |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2438 | ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2439 | // The super class for the metaclass is emitted as the name of the |
| 2440 | // super class. The runtime fixes this up to point to the |
| 2441 | // *metaclass* for the super class. |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2442 | if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2443 | Values[ 1] = |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2444 | llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2445 | ObjCTypes.ClassPtrTy); |
| 2446 | } else { |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2447 | Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2448 | } |
| 2449 | Values[ 2] = GetClassName(ID->getIdentifier()); |
| 2450 | // Version is always 0. |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2451 | Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 2452 | Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); |
| 2453 | Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Fariborz Jahanian | b042a59 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2454 | Values[ 6] = EmitIvarList(ID, true); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2455 | Values[ 7] = |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2456 | EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(), |
Daniel Dunbar | b25452a | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2457 | "__OBJC,__cls_meth,regular,no_dead_strip", |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2458 | Methods); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2459 | // cache is always NULL. |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2460 | Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2461 | Values[ 9] = Protocols; |
| 2462 | // ivar_layout for metaclass is always NULL. |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2463 | Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2464 | // The class extension is always unused for metaclasses. |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2465 | Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy); |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2466 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2467 | Values); |
| 2468 | |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2469 | std::string Name("\01L_OBJC_METACLASS_"); |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 2470 | Name += ID->getNameAsCString(); |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2471 | |
| 2472 | // Check for a forward reference. |
| 2473 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 2474 | if (GV) { |
| 2475 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 2476 | "Forward metaclass reference has incorrect type."); |
| 2477 | GV->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 2478 | GV->setInitializer(Init); |
| 2479 | } else { |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2480 | GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false, |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2481 | llvm::GlobalValue::InternalLinkage, |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2482 | Init, Name); |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2483 | } |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2484 | GV->setSection("__OBJC,__meta_class,regular,no_dead_strip"); |
Daniel Dunbar | ae33384 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2485 | GV->setAlignment(4); |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 2486 | CGM.AddUsedGlobal(GV); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2487 | |
| 2488 | return GV; |
| 2489 | } |
| 2490 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2491 | llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) { |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2492 | std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString(); |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2493 | |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 2494 | // FIXME: Should we look these up somewhere other than the module. Its a bit |
| 2495 | // silly since we only generate these while processing an implementation, so |
| 2496 | // exactly one pointer would work if know when we entered/exitted an |
| 2497 | // implementation block. |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2498 | |
| 2499 | // Check for an existing forward reference. |
Fariborz Jahanian | 475831b | 2009-01-07 20:11:22 +0000 | [diff] [blame] | 2500 | // Previously, metaclass with internal linkage may have been defined. |
| 2501 | // pass 'true' as 2nd argument so it is returned. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2502 | if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, |
| 2503 | true)) { |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2504 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 2505 | "Forward metaclass reference has incorrect type."); |
| 2506 | return GV; |
| 2507 | } else { |
| 2508 | // Generate as an external reference to keep a consistent |
| 2509 | // module. This will be patched up when we emit the metaclass. |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2510 | return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false, |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2511 | llvm::GlobalValue::ExternalLinkage, |
| 2512 | 0, |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2513 | Name); |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2514 | } |
| 2515 | } |
| 2516 | |
Fariborz Jahanian | eb80c98 | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 2517 | llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) { |
| 2518 | std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString(); |
| 2519 | |
| 2520 | if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, |
| 2521 | true)) { |
| 2522 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 2523 | "Forward class metadata reference has incorrect type."); |
| 2524 | return GV; |
| 2525 | } else { |
| 2526 | return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false, |
| 2527 | llvm::GlobalValue::ExternalLinkage, |
| 2528 | 0, |
| 2529 | Name); |
| 2530 | } |
| 2531 | } |
| 2532 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2533 | /* |
| 2534 | struct objc_class_ext { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2535 | uint32_t size; |
| 2536 | const char *weak_ivar_layout; |
| 2537 | struct _objc_property_list *properties; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2538 | }; |
| 2539 | */ |
| 2540 | llvm::Constant * |
| 2541 | CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2542 | uint64_t Size = |
Duncan Sands | c76fe8b | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2543 | CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2544 | |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2545 | llvm::Constant *Values[3]; |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2546 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Fariborz Jahanian | 6df6986 | 2009-04-22 23:00:43 +0000 | [diff] [blame] | 2547 | Values[1] = BuildIvarLayout(ID, false); |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2548 | Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(), |
Fariborz Jahanian | 066347e | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 2549 | ID, ID->getClassInterface(), ObjCTypes); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2550 | |
| 2551 | // Return null if no extension bits are used. |
| 2552 | if (Values[1]->isNullValue() && Values[2]->isNullValue()) |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2553 | return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2554 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2555 | llvm::Constant *Init = |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2556 | llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values); |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2557 | return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(), |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2558 | Init, "__OBJC,__class_ext,regular,no_dead_strip", |
Daniel Dunbar | b25452a | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2559 | 4, true); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2560 | } |
| 2561 | |
| 2562 | /* |
| 2563 | struct objc_ivar { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2564 | char *ivar_name; |
| 2565 | char *ivar_type; |
| 2566 | int ivar_offset; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2567 | }; |
| 2568 | |
| 2569 | struct objc_ivar_list { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2570 | int ivar_count; |
| 2571 | struct objc_ivar list[count]; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2572 | }; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2573 | */ |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2574 | llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID, |
Fariborz Jahanian | b042a59 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2575 | bool ForClass) { |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2576 | std::vector<llvm::Constant*> Ivars; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2577 | |
| 2578 | // When emitting the root class GCC emits ivar entries for the |
| 2579 | // actual class structure. It is not clear if we need to follow this |
| 2580 | // behavior; for now lets try and get away with not doing it. If so, |
| 2581 | // the cleanest solution would be to make up an ObjCInterfaceDecl |
| 2582 | // for the class. |
| 2583 | if (ForClass) |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2584 | return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2585 | |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 2586 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2587 | |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 2588 | for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin(); |
Fariborz Jahanian | b26d578 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 2589 | IVD; IVD = IVD->getNextIvar()) { |
Fariborz Jahanian | 7c80959 | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 2590 | // Ignore unnamed bit-fields. |
| 2591 | if (!IVD->getDeclName()) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2592 | continue; |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2593 | llvm::Constant *Ivar[] = { |
| 2594 | GetMethodVarName(IVD->getIdentifier()), |
| 2595 | GetMethodVarType(IVD), |
| 2596 | llvm::ConstantInt::get(ObjCTypes.IntTy, |
| 2597 | ComputeIvarBaseOffset(CGM, OID, IVD)) |
| 2598 | }; |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2599 | Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar)); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2600 | } |
| 2601 | |
| 2602 | // Return null for empty list. |
| 2603 | if (Ivars.empty()) |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2604 | return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2605 | |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2606 | llvm::Constant *Values[2]; |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2607 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2608 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy, |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2609 | Ivars.size()); |
Owen Anderson | 47034e1 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 2610 | Values[1] = llvm::ConstantArray::get(AT, Ivars); |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2611 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2612 | |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2613 | llvm::GlobalVariable *GV; |
| 2614 | if (ForClass) |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2615 | GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(), |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2616 | Init, "__OBJC,__class_vars,regular,no_dead_strip", |
Daniel Dunbar | ae33384 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2617 | 4, true); |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2618 | else |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2619 | GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(), |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2620 | Init, "__OBJC,__instance_vars,regular,no_dead_strip", |
Daniel Dunbar | b25452a | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2621 | 4, true); |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2622 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2623 | } |
| 2624 | |
| 2625 | /* |
| 2626 | struct objc_method { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2627 | SEL method_name; |
| 2628 | char *method_types; |
| 2629 | void *method; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2630 | }; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2631 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2632 | struct objc_method_list { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2633 | struct objc_method_list *obsolete; |
| 2634 | int count; |
| 2635 | struct objc_method methods_list[count]; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2636 | }; |
| 2637 | */ |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2638 | |
| 2639 | /// GetMethodConstant - Return a struct objc_method constant for the |
| 2640 | /// given method if it has been defined. The result is null if the |
| 2641 | /// method has not been defined. The return value has type MethodPtrTy. |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2642 | llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) { |
Argyrios Kyrtzidis | 13257c5 | 2010-08-09 10:54:20 +0000 | [diff] [blame] | 2643 | llvm::Function *Fn = GetMethodDefinition(MD); |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2644 | if (!Fn) |
| 2645 | return 0; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2646 | |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2647 | llvm::Constant *Method[] = { |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2648 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2649 | ObjCTypes.SelectorPtrTy), |
| 2650 | GetMethodVarType(MD), |
| 2651 | llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy) |
| 2652 | }; |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2653 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method); |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2654 | } |
| 2655 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2656 | llvm::Constant *CGObjCMac::EmitMethodList(Twine Name, |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2657 | const char *Section, |
Bill Wendling | cb5b5ff | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 2658 | ArrayRef<llvm::Constant*> Methods) { |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2659 | // Return null for empty list. |
| 2660 | if (Methods.empty()) |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2661 | return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2662 | |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2663 | llvm::Constant *Values[3]; |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2664 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2665 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2666 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy, |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2667 | Methods.size()); |
Owen Anderson | 47034e1 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 2668 | Values[2] = llvm::ConstantArray::get(AT, Methods); |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2669 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2670 | |
Daniel Dunbar | b25452a | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2671 | llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true); |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2672 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListPtrTy); |
Daniel Dunbar | a94ecd2 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2673 | } |
| 2674 | |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 2675 | llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2676 | const ObjCContainerDecl *CD) { |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 2677 | SmallString<256> Name; |
Fariborz Jahanian | 0196a1c | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 2678 | GetNameForMethod(OMD, CD, Name); |
Daniel Dunbar | a94ecd2 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2679 | |
Daniel Dunbar | bf8c24a | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 2680 | CodeGenTypes &Types = CGM.getTypes(); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2681 | llvm::FunctionType *MethodTy = |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2682 | Types.GetFunctionType(Types.arrangeObjCMethodDeclaration(OMD)); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2683 | llvm::Function *Method = |
Daniel Dunbar | 7a95ca3 | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 2684 | llvm::Function::Create(MethodTy, |
Daniel Dunbar | a94ecd2 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2685 | llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | d238681 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 2686 | Name.str(), |
Daniel Dunbar | a94ecd2 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2687 | &CGM.getModule()); |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2688 | MethodDefinitions.insert(std::make_pair(OMD, Method)); |
Daniel Dunbar | a94ecd2 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2689 | |
Daniel Dunbar | a94ecd2 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2690 | return Method; |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2691 | } |
| 2692 | |
Daniel Dunbar | 30c6536 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2693 | llvm::GlobalVariable * |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2694 | CGObjCCommonMac::CreateMetadataVar(Twine Name, |
Daniel Dunbar | 30c6536 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2695 | llvm::Constant *Init, |
| 2696 | const char *Section, |
Daniel Dunbar | 463cc8a | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 2697 | unsigned Align, |
| 2698 | bool AddToUsed) { |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2699 | llvm::Type *Ty = Init->getType(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2700 | llvm::GlobalVariable *GV = |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2701 | new llvm::GlobalVariable(CGM.getModule(), Ty, false, |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 2702 | llvm::GlobalValue::InternalLinkage, Init, Name); |
Daniel Dunbar | 30c6536 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2703 | if (Section) |
| 2704 | GV->setSection(Section); |
Daniel Dunbar | 463cc8a | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 2705 | if (Align) |
| 2706 | GV->setAlignment(Align); |
| 2707 | if (AddToUsed) |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 2708 | CGM.AddUsedGlobal(GV); |
Daniel Dunbar | 30c6536 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2709 | return GV; |
| 2710 | } |
| 2711 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2712 | llvm::Function *CGObjCMac::ModuleInitFunction() { |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2713 | // Abuse this interface function as a place to finalize. |
| 2714 | FinishModule(); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2715 | return NULL; |
| 2716 | } |
| 2717 | |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2718 | llvm::Constant *CGObjCMac::GetPropertyGetFunction() { |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2719 | return ObjCTypes.getGetPropertyFn(); |
Daniel Dunbar | a91c3e0 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 2720 | } |
| 2721 | |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2722 | llvm::Constant *CGObjCMac::GetPropertySetFunction() { |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2723 | return ObjCTypes.getSetPropertyFn(); |
Daniel Dunbar | a91c3e0 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 2724 | } |
| 2725 | |
David Chisnall | 168b80f | 2010-12-26 22:13:16 +0000 | [diff] [blame] | 2726 | llvm::Constant *CGObjCMac::GetGetStructFunction() { |
| 2727 | return ObjCTypes.getCopyStructFn(); |
| 2728 | } |
| 2729 | llvm::Constant *CGObjCMac::GetSetStructFunction() { |
Fariborz Jahanian | 5a8c203 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 2730 | return ObjCTypes.getCopyStructFn(); |
| 2731 | } |
| 2732 | |
Fariborz Jahanian | 1e1b549 | 2012-01-06 18:07:23 +0000 | [diff] [blame] | 2733 | llvm::Constant *CGObjCMac::GetCppAtomicObjectFunction() { |
| 2734 | return ObjCTypes.getCppAtomicObjectFunction(); |
| 2735 | } |
| 2736 | |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2737 | llvm::Constant *CGObjCMac::EnumerationMutationFunction() { |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2738 | return ObjCTypes.getEnumerationMutationFn(); |
Anders Carlsson | 3f35a26 | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 2739 | } |
| 2740 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 2741 | void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) { |
| 2742 | return EmitTryOrSynchronizedStmt(CGF, S); |
| 2743 | } |
| 2744 | |
| 2745 | void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF, |
| 2746 | const ObjCAtSynchronizedStmt &S) { |
| 2747 | return EmitTryOrSynchronizedStmt(CGF, S); |
| 2748 | } |
| 2749 | |
John McCall | 65bea08 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2750 | namespace { |
John McCall | cda666c | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 2751 | struct PerformFragileFinally : EHScopeStack::Cleanup { |
John McCall | 65bea08 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2752 | const Stmt &S; |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2753 | llvm::Value *SyncArgSlot; |
John McCall | 65bea08 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2754 | llvm::Value *CallTryExitVar; |
| 2755 | llvm::Value *ExceptionData; |
| 2756 | ObjCTypesHelper &ObjCTypes; |
| 2757 | PerformFragileFinally(const Stmt *S, |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2758 | llvm::Value *SyncArgSlot, |
John McCall | 65bea08 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2759 | llvm::Value *CallTryExitVar, |
| 2760 | llvm::Value *ExceptionData, |
| 2761 | ObjCTypesHelper *ObjCTypes) |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2762 | : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar), |
John McCall | 65bea08 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2763 | ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {} |
| 2764 | |
John McCall | 30317fd | 2011-07-12 20:27:29 +0000 | [diff] [blame] | 2765 | void Emit(CodeGenFunction &CGF, Flags flags) { |
John McCall | 65bea08 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2766 | // Check whether we need to call objc_exception_try_exit. |
| 2767 | // In optimized code, this branch will always be folded. |
| 2768 | llvm::BasicBlock *FinallyCallExit = |
| 2769 | CGF.createBasicBlock("finally.call_exit"); |
| 2770 | llvm::BasicBlock *FinallyNoCallExit = |
| 2771 | CGF.createBasicBlock("finally.no_call_exit"); |
| 2772 | CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar), |
| 2773 | FinallyCallExit, FinallyNoCallExit); |
| 2774 | |
| 2775 | CGF.EmitBlock(FinallyCallExit); |
| 2776 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData) |
| 2777 | ->setDoesNotThrow(); |
| 2778 | |
| 2779 | CGF.EmitBlock(FinallyNoCallExit); |
| 2780 | |
| 2781 | if (isa<ObjCAtTryStmt>(S)) { |
| 2782 | if (const ObjCAtFinallyStmt* FinallyStmt = |
John McCall | cebe0ca | 2010-08-11 00:16:14 +0000 | [diff] [blame] | 2783 | cast<ObjCAtTryStmt>(S).getFinallyStmt()) { |
| 2784 | // Save the current cleanup destination in case there's |
| 2785 | // control flow inside the finally statement. |
| 2786 | llvm::Value *CurCleanupDest = |
| 2787 | CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot()); |
| 2788 | |
John McCall | 65bea08 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2789 | CGF.EmitStmt(FinallyStmt->getFinallyBody()); |
| 2790 | |
John McCall | cebe0ca | 2010-08-11 00:16:14 +0000 | [diff] [blame] | 2791 | if (CGF.HaveInsertPoint()) { |
| 2792 | CGF.Builder.CreateStore(CurCleanupDest, |
| 2793 | CGF.getNormalCleanupDestSlot()); |
| 2794 | } else { |
| 2795 | // Currently, the end of the cleanup must always exist. |
| 2796 | CGF.EnsureInsertPoint(); |
| 2797 | } |
| 2798 | } |
John McCall | 65bea08 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2799 | } else { |
| 2800 | // Emit objc_sync_exit(expr); as finally's sole statement for |
| 2801 | // @synchronized. |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2802 | llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot); |
John McCall | 65bea08 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2803 | CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg) |
| 2804 | ->setDoesNotThrow(); |
| 2805 | } |
| 2806 | } |
| 2807 | }; |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2808 | |
| 2809 | class FragileHazards { |
| 2810 | CodeGenFunction &CGF; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2811 | SmallVector<llvm::Value*, 20> Locals; |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2812 | llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry; |
| 2813 | |
| 2814 | llvm::InlineAsm *ReadHazard; |
| 2815 | llvm::InlineAsm *WriteHazard; |
| 2816 | |
| 2817 | llvm::FunctionType *GetAsmFnType(); |
| 2818 | |
| 2819 | void collectLocals(); |
| 2820 | void emitReadHazard(CGBuilderTy &Builder); |
| 2821 | |
| 2822 | public: |
| 2823 | FragileHazards(CodeGenFunction &CGF); |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2824 | |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2825 | void emitWriteHazard(); |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2826 | void emitHazardsInNewBlocks(); |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2827 | }; |
| 2828 | } |
| 2829 | |
| 2830 | /// Create the fragile-ABI read and write hazards based on the current |
| 2831 | /// state of the function, which is presumed to be immediately prior |
| 2832 | /// to a @try block. These hazards are used to maintain correct |
| 2833 | /// semantics in the face of optimization and the fragile ABI's |
| 2834 | /// cavalier use of setjmp/longjmp. |
| 2835 | FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) { |
| 2836 | collectLocals(); |
| 2837 | |
| 2838 | if (Locals.empty()) return; |
| 2839 | |
| 2840 | // Collect all the blocks in the function. |
| 2841 | for (llvm::Function::iterator |
| 2842 | I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I) |
| 2843 | BlocksBeforeTry.insert(&*I); |
| 2844 | |
| 2845 | llvm::FunctionType *AsmFnTy = GetAsmFnType(); |
| 2846 | |
| 2847 | // Create a read hazard for the allocas. This inhibits dead-store |
| 2848 | // optimizations and forces the values to memory. This hazard is |
| 2849 | // inserted before any 'throwing' calls in the protected scope to |
| 2850 | // reflect the possibility that the variables might be read from the |
| 2851 | // catch block if the call throws. |
| 2852 | { |
| 2853 | std::string Constraint; |
| 2854 | for (unsigned I = 0, E = Locals.size(); I != E; ++I) { |
| 2855 | if (I) Constraint += ','; |
| 2856 | Constraint += "*m"; |
| 2857 | } |
| 2858 | |
| 2859 | ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false); |
| 2860 | } |
| 2861 | |
| 2862 | // Create a write hazard for the allocas. This inhibits folding |
| 2863 | // loads across the hazard. This hazard is inserted at the |
| 2864 | // beginning of the catch path to reflect the possibility that the |
| 2865 | // variables might have been written within the protected scope. |
| 2866 | { |
| 2867 | std::string Constraint; |
| 2868 | for (unsigned I = 0, E = Locals.size(); I != E; ++I) { |
| 2869 | if (I) Constraint += ','; |
| 2870 | Constraint += "=*m"; |
| 2871 | } |
| 2872 | |
| 2873 | WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false); |
| 2874 | } |
| 2875 | } |
| 2876 | |
| 2877 | /// Emit a write hazard at the current location. |
| 2878 | void FragileHazards::emitWriteHazard() { |
| 2879 | if (Locals.empty()) return; |
| 2880 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 2881 | CGF.Builder.CreateCall(WriteHazard, Locals)->setDoesNotThrow(); |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2882 | } |
| 2883 | |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2884 | void FragileHazards::emitReadHazard(CGBuilderTy &Builder) { |
| 2885 | assert(!Locals.empty()); |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 2886 | Builder.CreateCall(ReadHazard, Locals)->setDoesNotThrow(); |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2887 | } |
| 2888 | |
| 2889 | /// Emit read hazards in all the protected blocks, i.e. all the blocks |
| 2890 | /// which have been inserted since the beginning of the try. |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2891 | void FragileHazards::emitHazardsInNewBlocks() { |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2892 | if (Locals.empty()) return; |
| 2893 | |
| 2894 | CGBuilderTy Builder(CGF.getLLVMContext()); |
| 2895 | |
| 2896 | // Iterate through all blocks, skipping those prior to the try. |
| 2897 | for (llvm::Function::iterator |
| 2898 | FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) { |
| 2899 | llvm::BasicBlock &BB = *FI; |
| 2900 | if (BlocksBeforeTry.count(&BB)) continue; |
| 2901 | |
| 2902 | // Walk through all the calls in the block. |
| 2903 | for (llvm::BasicBlock::iterator |
| 2904 | BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) { |
| 2905 | llvm::Instruction &I = *BI; |
| 2906 | |
| 2907 | // Ignore instructions that aren't non-intrinsic calls. |
| 2908 | // These are the only calls that can possibly call longjmp. |
| 2909 | if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue; |
| 2910 | if (isa<llvm::IntrinsicInst>(I)) |
| 2911 | continue; |
| 2912 | |
| 2913 | // Ignore call sites marked nounwind. This may be questionable, |
| 2914 | // since 'nounwind' doesn't necessarily mean 'does not call longjmp'. |
| 2915 | llvm::CallSite CS(&I); |
| 2916 | if (CS.doesNotThrow()) continue; |
| 2917 | |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2918 | // Insert a read hazard before the call. This will ensure that |
| 2919 | // any writes to the locals are performed before making the |
| 2920 | // call. If the call throws, then this is sufficient to |
| 2921 | // guarantee correctness as long as it doesn't also write to any |
| 2922 | // locals. |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2923 | Builder.SetInsertPoint(&BB, BI); |
| 2924 | emitReadHazard(Builder); |
| 2925 | } |
| 2926 | } |
| 2927 | } |
| 2928 | |
| 2929 | static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) { |
| 2930 | if (V) S.insert(V); |
| 2931 | } |
| 2932 | |
| 2933 | void FragileHazards::collectLocals() { |
| 2934 | // Compute a set of allocas to ignore. |
| 2935 | llvm::DenseSet<llvm::Value*> AllocasToIgnore; |
| 2936 | addIfPresent(AllocasToIgnore, CGF.ReturnValue); |
| 2937 | addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest); |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2938 | |
| 2939 | // Collect all the allocas currently in the function. This is |
| 2940 | // probably way too aggressive. |
| 2941 | llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock(); |
| 2942 | for (llvm::BasicBlock::iterator |
| 2943 | I = Entry.begin(), E = Entry.end(); I != E; ++I) |
| 2944 | if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I)) |
| 2945 | Locals.push_back(&*I); |
| 2946 | } |
| 2947 | |
| 2948 | llvm::FunctionType *FragileHazards::GetAsmFnType() { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2949 | SmallVector<llvm::Type *, 16> tys(Locals.size()); |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 2950 | for (unsigned i = 0, e = Locals.size(); i != e; ++i) |
| 2951 | tys[i] = Locals[i]->getType(); |
| 2952 | return llvm::FunctionType::get(CGF.VoidTy, tys, false); |
John McCall | 65bea08 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2953 | } |
| 2954 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2955 | /* |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2956 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2957 | Objective-C setjmp-longjmp (sjlj) Exception Handling |
| 2958 | -- |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2959 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 2960 | A catch buffer is a setjmp buffer plus: |
| 2961 | - a pointer to the exception that was caught |
| 2962 | - a pointer to the previous exception data buffer |
| 2963 | - two pointers of reserved storage |
| 2964 | Therefore catch buffers form a stack, with a pointer to the top |
| 2965 | of the stack kept in thread-local storage. |
| 2966 | |
| 2967 | objc_exception_try_enter pushes a catch buffer onto the EH stack. |
| 2968 | objc_exception_try_exit pops the given catch buffer, which is |
| 2969 | required to be the top of the EH stack. |
| 2970 | objc_exception_throw pops the top of the EH stack, writes the |
| 2971 | thrown exception into the appropriate field, and longjmps |
| 2972 | to the setjmp buffer. It crashes the process (with a printf |
| 2973 | and an abort()) if there are no catch buffers on the stack. |
| 2974 | objc_exception_extract just reads the exception pointer out of the |
| 2975 | catch buffer. |
| 2976 | |
| 2977 | There's no reason an implementation couldn't use a light-weight |
| 2978 | setjmp here --- something like __builtin_setjmp, but API-compatible |
| 2979 | with the heavyweight setjmp. This will be more important if we ever |
| 2980 | want to implement correct ObjC/C++ exception interactions for the |
| 2981 | fragile ABI. |
| 2982 | |
| 2983 | Note that for this use of setjmp/longjmp to be correct, we may need |
| 2984 | to mark some local variables volatile: if a non-volatile local |
| 2985 | variable is modified between the setjmp and the longjmp, it has |
| 2986 | indeterminate value. For the purposes of LLVM IR, it may be |
| 2987 | sufficient to make loads and stores within the @try (to variables |
| 2988 | declared outside the @try) volatile. This is necessary for |
| 2989 | optimized correctness, but is not currently being done; this is |
| 2990 | being tracked as rdar://problem/8160285 |
| 2991 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2992 | The basic framework for a @try-catch-finally is as follows: |
| 2993 | { |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2994 | objc_exception_data d; |
| 2995 | id _rethrow = null; |
Anders Carlsson | da0e456 | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2996 | bool _call_try_exit = true; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2997 | |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2998 | objc_exception_try_enter(&d); |
| 2999 | if (!setjmp(d.jmp_buf)) { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3000 | ... try body ... |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3001 | } else { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3002 | // exception path |
| 3003 | id _caught = objc_exception_extract(&d); |
| 3004 | |
| 3005 | // enter new try scope for handlers |
| 3006 | if (!setjmp(d.jmp_buf)) { |
| 3007 | ... match exception and execute catch blocks ... |
| 3008 | |
| 3009 | // fell off end, rethrow. |
| 3010 | _rethrow = _caught; |
| 3011 | ... jump-through-finally to finally_rethrow ... |
| 3012 | } else { |
| 3013 | // exception in catch block |
| 3014 | _rethrow = objc_exception_extract(&d); |
| 3015 | _call_try_exit = false; |
| 3016 | ... jump-through-finally to finally_rethrow ... |
| 3017 | } |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3018 | } |
Daniel Dunbar | 2efd538 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 3019 | ... jump-through-finally to finally_end ... |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3020 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3021 | finally: |
Anders Carlsson | da0e456 | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 3022 | if (_call_try_exit) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3023 | objc_exception_try_exit(&d); |
Anders Carlsson | da0e456 | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 3024 | |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3025 | ... finally block .... |
Daniel Dunbar | 2efd538 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 3026 | ... dispatch to finally destination ... |
| 3027 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3028 | finally_rethrow: |
Daniel Dunbar | 2efd538 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 3029 | objc_exception_throw(_rethrow); |
| 3030 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3031 | finally_end: |
| 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 | This framework differs slightly from the one gcc uses, in that gcc |
| 3035 | uses _rethrow to determine if objc_exception_try_exit should be called |
| 3036 | and if the object should be rethrown. This breaks in the face of |
| 3037 | throwing nil and introduces unnecessary branches. |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3038 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3039 | We specialize this framework for a few particular circumstances: |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3040 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3041 | - If there are no catch blocks, then we avoid emitting the second |
| 3042 | exception handling context. |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3043 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3044 | - If there is a catch-all catch block (i.e. @catch(...) or @catch(id |
| 3045 | e)) we avoid emitting the code to rethrow an uncaught exception. |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3046 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3047 | - FIXME: If there is no @finally block we can do a few more |
| 3048 | simplifications. |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3049 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3050 | Rethrows and Jumps-Through-Finally |
| 3051 | -- |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3052 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3053 | '@throw;' is supported by pushing the currently-caught exception |
| 3054 | onto ObjCEHStack while the @catch blocks are emitted. |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3055 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3056 | Branches through the @finally block are handled with an ordinary |
| 3057 | normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC |
| 3058 | exceptions are not compatible with C++ exceptions, and this is |
| 3059 | hardly the only place where this will go wrong. |
Daniel Dunbar | 2efd538 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 3060 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3061 | @synchronized(expr) { stmt; } is emitted as if it were: |
| 3062 | id synch_value = expr; |
| 3063 | objc_sync_enter(synch_value); |
| 3064 | @try { stmt; } @finally { objc_sync_exit(synch_value); } |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3065 | */ |
| 3066 | |
Fariborz Jahanian | c2ad6dc | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 3067 | void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 3068 | const Stmt &S) { |
| 3069 | bool isTry = isa<ObjCAtTryStmt>(S); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3070 | |
| 3071 | // A destination for the fall-through edges of the catch handlers to |
| 3072 | // jump to. |
| 3073 | CodeGenFunction::JumpDest FinallyEnd = |
| 3074 | CGF.getJumpDestInCurrentScope("finally.end"); |
| 3075 | |
| 3076 | // A destination for the rethrow edge of the catch handlers to jump |
| 3077 | // to. |
| 3078 | CodeGenFunction::JumpDest FinallyRethrow = |
| 3079 | CGF.getJumpDestInCurrentScope("finally.rethrow"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3080 | |
Daniel Dunbar | 94ceb61 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 3081 | // For @synchronized, call objc_sync_enter(sync.expr). The |
| 3082 | // evaluation of the expression must occur before we enter the |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3083 | // @synchronized. We can't avoid a temp here because we need the |
| 3084 | // value to be preserved. If the backend ever does liveness |
| 3085 | // correctly after setjmp, this will be unnecessary. |
| 3086 | llvm::Value *SyncArgSlot = 0; |
Daniel Dunbar | 94ceb61 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 3087 | if (!isTry) { |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3088 | llvm::Value *SyncArg = |
Daniel Dunbar | 94ceb61 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 3089 | CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr()); |
| 3090 | SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3091 | CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg) |
| 3092 | ->setDoesNotThrow(); |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3093 | |
| 3094 | SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg"); |
| 3095 | CGF.Builder.CreateStore(SyncArg, SyncArgSlot); |
Daniel Dunbar | 94ceb61 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 3096 | } |
Daniel Dunbar | 2efd538 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 3097 | |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3098 | // Allocate memory for the setjmp buffer. This needs to be kept |
| 3099 | // live throughout the try and catch blocks. |
| 3100 | llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy, |
| 3101 | "exceptiondata.ptr"); |
| 3102 | |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3103 | // Create the fragile hazards. Note that this will not capture any |
| 3104 | // of the allocas required for exception processing, but will |
| 3105 | // capture the current basic block (which extends all the way to the |
| 3106 | // setjmp call) as "before the @try". |
| 3107 | FragileHazards Hazards(CGF); |
| 3108 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3109 | // Create a flag indicating whether the cleanup needs to call |
| 3110 | // objc_exception_try_exit. This is true except when |
| 3111 | // - no catches match and we're branching through the cleanup |
| 3112 | // just to rethrow the exception, or |
| 3113 | // - a catch matched and we're falling out of the catch handler. |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3114 | // The setjmp-safety rule here is that we should always store to this |
| 3115 | // variable in a place that dominates the branch through the cleanup |
| 3116 | // without passing through any setjmps. |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3117 | llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(), |
Anders Carlsson | da0e456 | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 3118 | "_call_try_exit"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3119 | |
John McCall | 9916e3f | 2010-10-04 23:42:51 +0000 | [diff] [blame] | 3120 | // A slot containing the exception to rethrow. Only needed when we |
| 3121 | // have both a @catch and a @finally. |
| 3122 | llvm::Value *PropagatingExnVar = 0; |
| 3123 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3124 | // Push a normal cleanup to leave the try scope. |
John McCall | cda666c | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 3125 | CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S, |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3126 | SyncArgSlot, |
John McCall | cda666c | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 3127 | CallTryExitVar, |
| 3128 | ExceptionData, |
| 3129 | &ObjCTypes); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3130 | |
| 3131 | // Enter a try block: |
| 3132 | // - Call objc_exception_try_enter to push ExceptionData on top of |
| 3133 | // the EH stack. |
| 3134 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData) |
| 3135 | ->setDoesNotThrow(); |
| 3136 | |
| 3137 | // - Call setjmp on the exception data buffer. |
| 3138 | llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0); |
| 3139 | llvm::Value *GEPIndexes[] = { Zero, Zero, Zero }; |
| 3140 | llvm::Value *SetJmpBuffer = |
Jay Foad | 040dd82 | 2011-07-22 08:16:57 +0000 | [diff] [blame] | 3141 | CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, "setjmp_buffer"); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3142 | llvm::CallInst *SetJmpResult = |
| 3143 | CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result"); |
| 3144 | SetJmpResult->setDoesNotThrow(); |
Bill Wendling | bd26cf9 | 2011-12-19 23:53:28 +0000 | [diff] [blame] | 3145 | SetJmpResult->setCanReturnTwice(); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3146 | |
| 3147 | // If setjmp returned 0, enter the protected block; otherwise, |
| 3148 | // branch to the handler. |
Daniel Dunbar | 75283ff | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 3149 | llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try"); |
| 3150 | llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler"); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3151 | llvm::Value *DidCatch = |
John McCall | cebe0ca | 2010-08-11 00:16:14 +0000 | [diff] [blame] | 3152 | CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception"); |
| 3153 | CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock); |
Anders Carlsson | 4f1c7c3 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3154 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3155 | // Emit the protected block. |
Anders Carlsson | 4f1c7c3 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3156 | CGF.EmitBlock(TryBlock); |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3157 | CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3158 | CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody() |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3159 | : cast<ObjCAtSynchronizedStmt>(S).getSynchBody()); |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3160 | |
| 3161 | CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3162 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3163 | // Emit the exception handler block. |
Daniel Dunbar | 7f08678 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 3164 | CGF.EmitBlock(TryHandler); |
Daniel Dunbar | b22ff59 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 3165 | |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3166 | // Don't optimize loads of the in-scope locals across this point. |
| 3167 | Hazards.emitWriteHazard(); |
| 3168 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3169 | // For a @synchronized (or a @try with no catches), just branch |
| 3170 | // through the cleanup to the rethrow block. |
| 3171 | if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) { |
| 3172 | // Tell the cleanup not to re-pop the exit. |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3173 | CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar); |
Anders Carlsson | bfee7e9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 3174 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3175 | |
| 3176 | // Otherwise, we have to match against the caught exceptions. |
| 3177 | } else { |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3178 | // Retrieve the exception object. We may emit multiple blocks but |
| 3179 | // nothing can cross this so the value is already in SSA form. |
| 3180 | llvm::CallInst *Caught = |
| 3181 | CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(), |
| 3182 | ExceptionData, "caught"); |
| 3183 | Caught->setDoesNotThrow(); |
| 3184 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3185 | // Push the exception to rethrow onto the EH value stack for the |
| 3186 | // benefit of any @throws in the handlers. |
| 3187 | CGF.ObjCEHValueStack.push_back(Caught); |
| 3188 | |
Douglas Gregor | 96c7949 | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 3189 | const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3190 | |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3191 | bool HasFinally = (AtTryStmt->getFinallyStmt() != 0); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3192 | |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3193 | llvm::BasicBlock *CatchBlock = 0; |
| 3194 | llvm::BasicBlock *CatchHandler = 0; |
| 3195 | if (HasFinally) { |
John McCall | 9916e3f | 2010-10-04 23:42:51 +0000 | [diff] [blame] | 3196 | // Save the currently-propagating exception before |
| 3197 | // objc_exception_try_enter clears the exception slot. |
| 3198 | PropagatingExnVar = CGF.CreateTempAlloca(Caught->getType(), |
| 3199 | "propagating_exception"); |
| 3200 | CGF.Builder.CreateStore(Caught, PropagatingExnVar); |
| 3201 | |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3202 | // Enter a new exception try block (in case a @catch block |
| 3203 | // throws an exception). |
| 3204 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData) |
| 3205 | ->setDoesNotThrow(); |
Anders Carlsson | 4f1c7c3 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3206 | |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3207 | llvm::CallInst *SetJmpResult = |
| 3208 | CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, |
| 3209 | "setjmp.result"); |
| 3210 | SetJmpResult->setDoesNotThrow(); |
Bill Wendling | bd26cf9 | 2011-12-19 23:53:28 +0000 | [diff] [blame] | 3211 | SetJmpResult->setCanReturnTwice(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3212 | |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3213 | llvm::Value *Threw = |
| 3214 | CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception"); |
| 3215 | |
| 3216 | CatchBlock = CGF.createBasicBlock("catch"); |
| 3217 | CatchHandler = CGF.createBasicBlock("catch_for_catch"); |
| 3218 | CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock); |
| 3219 | |
| 3220 | CGF.EmitBlock(CatchBlock); |
| 3221 | } |
| 3222 | |
| 3223 | CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3224 | |
Daniel Dunbar | b22ff59 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 3225 | // Handle catch list. As a special case we check if everything is |
| 3226 | // matched and avoid generating code for falling off the end if |
| 3227 | // so. |
| 3228 | bool AllMatched = false; |
Douglas Gregor | 96c7949 | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 3229 | for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) { |
| 3230 | const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I); |
Anders Carlsson | 4f1c7c3 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3231 | |
Douglas Gregor | 46a572b | 2010-04-26 16:46:50 +0000 | [diff] [blame] | 3232 | const VarDecl *CatchParam = CatchStmt->getCatchParamDecl(); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3233 | const ObjCObjectPointerType *OPT = 0; |
Daniel Dunbar | 523208f | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 3234 | |
Anders Carlsson | 4f1c7c3 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3235 | // catch(...) always matches. |
Daniel Dunbar | b22ff59 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 3236 | if (!CatchParam) { |
| 3237 | AllMatched = true; |
| 3238 | } else { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3239 | OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3240 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3241 | // catch(id e) always matches under this ABI, since only |
| 3242 | // ObjC exceptions end up here in the first place. |
Daniel Dunbar | 86919f4 | 2008-09-27 22:21:14 +0000 | [diff] [blame] | 3243 | // FIXME: For the time being we also match id<X>; this should |
| 3244 | // be rejected by Sema instead. |
Eli Friedman | 55179ca | 2009-07-11 00:57:02 +0000 | [diff] [blame] | 3245 | if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType())) |
Daniel Dunbar | b22ff59 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 3246 | AllMatched = true; |
Anders Carlsson | 4f1c7c3 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3247 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3248 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3249 | // 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] | 3250 | if (AllMatched) { |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3251 | CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF); |
| 3252 | |
Anders Carlsson | 9396a89 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 3253 | if (CatchParam) { |
John McCall | 1c9c3fd | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 3254 | CGF.EmitAutoVarDecl(*CatchParam); |
Daniel Dunbar | 5c7e393 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 3255 | assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3256 | |
| 3257 | // These types work out because ConvertType(id) == i8*. |
Steve Naroff | 371b8fb | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 3258 | CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam)); |
Anders Carlsson | 9396a89 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 3259 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3260 | |
Anders Carlsson | 9396a89 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 3261 | CGF.EmitStmt(CatchStmt->getCatchBody()); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3262 | |
| 3263 | // The scope of the catch variable ends right here. |
| 3264 | CatchVarCleanups.ForceCleanup(); |
| 3265 | |
Anders Carlsson | bfee7e9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 3266 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Anders Carlsson | 4f1c7c3 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3267 | break; |
| 3268 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3269 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3270 | assert(OPT && "Unexpected non-object pointer type in @catch"); |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 3271 | const ObjCObjectType *ObjTy = OPT->getObjectType(); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3272 | |
| 3273 | // FIXME: @catch (Class c) ? |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 3274 | ObjCInterfaceDecl *IDecl = ObjTy->getInterface(); |
| 3275 | assert(IDecl && "Catch parameter must have Objective-C type!"); |
Anders Carlsson | 4f1c7c3 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3276 | |
| 3277 | // Check if the @catch block matches the exception object. |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 3278 | llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3279 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3280 | llvm::CallInst *Match = |
Chris Lattner | c6406db | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 3281 | CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(), |
| 3282 | Class, Caught, "match"); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3283 | Match->setDoesNotThrow(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3284 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3285 | llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match"); |
| 3286 | llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3287 | |
| 3288 | CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"), |
Daniel Dunbar | 7f08678 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 3289 | MatchedBlock, NextCatchBlock); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3290 | |
Anders Carlsson | 4f1c7c3 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3291 | // Emit the @catch block. |
| 3292 | CGF.EmitBlock(MatchedBlock); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3293 | |
| 3294 | // Collect any cleanups for the catch variable. The scope lasts until |
| 3295 | // the end of the catch body. |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3296 | CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3297 | |
John McCall | 1c9c3fd | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 3298 | CGF.EmitAutoVarDecl(*CatchParam); |
Daniel Dunbar | 5c7e393 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 3299 | assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3300 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3301 | // Initialize the catch variable. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3302 | llvm::Value *Tmp = |
| 3303 | CGF.Builder.CreateBitCast(Caught, |
Benjamin Kramer | 76399eb | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 3304 | CGF.ConvertType(CatchParam->getType())); |
Steve Naroff | 371b8fb | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 3305 | CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam)); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3306 | |
Anders Carlsson | 9396a89 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 3307 | CGF.EmitStmt(CatchStmt->getCatchBody()); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3308 | |
| 3309 | // We're done with the catch variable. |
| 3310 | CatchVarCleanups.ForceCleanup(); |
| 3311 | |
Anders Carlsson | bfee7e9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 3312 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3313 | |
Anders Carlsson | 4f1c7c3 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3314 | CGF.EmitBlock(NextCatchBlock); |
| 3315 | } |
| 3316 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3317 | CGF.ObjCEHValueStack.pop_back(); |
| 3318 | |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3319 | // If nothing wanted anything to do with the caught exception, |
| 3320 | // kill the extract call. |
| 3321 | if (Caught->use_empty()) |
| 3322 | Caught->eraseFromParent(); |
| 3323 | |
| 3324 | if (!AllMatched) |
| 3325 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 3326 | |
| 3327 | if (HasFinally) { |
| 3328 | // Emit the exception handler for the @catch blocks. |
| 3329 | CGF.EmitBlock(CatchHandler); |
| 3330 | |
| 3331 | // In theory we might now need a write hazard, but actually it's |
| 3332 | // unnecessary because there's no local-accessing code between |
| 3333 | // the try's write hazard and here. |
| 3334 | //Hazards.emitWriteHazard(); |
| 3335 | |
John McCall | 9916e3f | 2010-10-04 23:42:51 +0000 | [diff] [blame] | 3336 | // Extract the new exception and save it to the |
| 3337 | // propagating-exception slot. |
| 3338 | assert(PropagatingExnVar); |
| 3339 | llvm::CallInst *NewCaught = |
| 3340 | CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(), |
| 3341 | ExceptionData, "caught"); |
| 3342 | NewCaught->setDoesNotThrow(); |
| 3343 | CGF.Builder.CreateStore(NewCaught, PropagatingExnVar); |
| 3344 | |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3345 | // Don't pop the catch handler; the throw already did. |
| 3346 | CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar); |
Anders Carlsson | bfee7e9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 3347 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Daniel Dunbar | b22ff59 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 3348 | } |
Anders Carlsson | 4f1c7c3 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3349 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3350 | |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3351 | // Insert read hazards as required in the new blocks. |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3352 | Hazards.emitHazardsInNewBlocks(); |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3353 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3354 | // Pop the cleanup. |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3355 | CGF.Builder.restoreIP(TryFallthroughIP); |
| 3356 | if (CGF.HaveInsertPoint()) |
| 3357 | CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3358 | CGF.PopCleanupBlock(); |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3359 | CGF.EmitBlock(FinallyEnd.getBlock(), true); |
Anders Carlsson | bfee7e9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 3360 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3361 | // Emit the rethrow block. |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3362 | CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP(); |
John McCall | ad5d61e | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 3363 | CGF.EmitBlock(FinallyRethrow.getBlock(), true); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3364 | if (CGF.HaveInsertPoint()) { |
John McCall | 9916e3f | 2010-10-04 23:42:51 +0000 | [diff] [blame] | 3365 | // If we have a propagating-exception variable, check it. |
| 3366 | llvm::Value *PropagatingExn; |
| 3367 | if (PropagatingExnVar) { |
| 3368 | PropagatingExn = CGF.Builder.CreateLoad(PropagatingExnVar); |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3369 | |
John McCall | 9916e3f | 2010-10-04 23:42:51 +0000 | [diff] [blame] | 3370 | // Otherwise, just look in the buffer for the exception to throw. |
| 3371 | } else { |
| 3372 | llvm::CallInst *Caught = |
| 3373 | CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(), |
| 3374 | ExceptionData); |
| 3375 | Caught->setDoesNotThrow(); |
| 3376 | PropagatingExn = Caught; |
| 3377 | } |
| 3378 | |
| 3379 | CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), PropagatingExn) |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3380 | ->setDoesNotThrow(); |
| 3381 | CGF.Builder.CreateUnreachable(); |
Fariborz Jahanian | e2caaaa | 2008-11-21 19:21:53 +0000 | [diff] [blame] | 3382 | } |
Anders Carlsson | 4f1c7c3 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3383 | |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3384 | CGF.Builder.restoreIP(SavedIP); |
Anders Carlsson | 1963b0c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 3385 | } |
| 3386 | |
| 3387 | void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2efd538 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 3388 | const ObjCAtThrowStmt &S) { |
Anders Carlsson | e005aa1 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 3389 | llvm::Value *ExceptionAsObject; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3390 | |
Anders Carlsson | e005aa1 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 3391 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
John McCall | 248512a | 2011-10-01 10:32:24 +0000 | [diff] [blame] | 3392 | llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3393 | ExceptionAsObject = |
Benjamin Kramer | 76399eb | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 3394 | CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy); |
Anders Carlsson | e005aa1 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 3395 | } else { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3396 | assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) && |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3397 | "Unexpected rethrow outside @catch block."); |
Anders Carlsson | bf8a1be | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 3398 | ExceptionAsObject = CGF.ObjCEHValueStack.back(); |
Anders Carlsson | e005aa1 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 3399 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3400 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3401 | CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject) |
| 3402 | ->setDoesNotReturn(); |
Anders Carlsson | 4f1c7c3 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3403 | CGF.Builder.CreateUnreachable(); |
Daniel Dunbar | 5c7e393 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 3404 | |
| 3405 | // Clear the insertion point to indicate we are in unreachable code. |
| 3406 | CGF.Builder.ClearInsertionPoint(); |
Anders Carlsson | 1963b0c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 3407 | } |
| 3408 | |
Fariborz Jahanian | 83f45b55 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 3409 | /// EmitObjCWeakRead - Code gen for loading value of a __weak |
Fariborz Jahanian | f5125d1 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 3410 | /// object: objc_read_weak (id *src) |
| 3411 | /// |
Fariborz Jahanian | 83f45b55 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 3412 | llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3413 | llvm::Value *AddrWeakObj) { |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3414 | llvm::Type* DestTy = |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3415 | cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType(); |
| 3416 | AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, |
| 3417 | ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 3418 | llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(), |
Fariborz Jahanian | 83f45b55 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 3419 | AddrWeakObj, "weakread"); |
Eli Friedman | a374b68 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 3420 | read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy); |
Fariborz Jahanian | f5125d1 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 3421 | return read_weak; |
| 3422 | } |
| 3423 | |
Fariborz Jahanian | 83f45b55 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 3424 | /// EmitObjCWeakAssign - Code gen for assigning to a __weak object. |
| 3425 | /// objc_assign_weak (id src, id *dst) |
| 3426 | /// |
| 3427 | void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3428 | llvm::Value *src, llvm::Value *dst) { |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3429 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3430 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | c76fe8b | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 3431 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3432 | assert(Size <= 8 && "does not support size > 8"); |
| 3433 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3434 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 1b074a3 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 3435 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 3436 | } |
Fariborz Jahanian | 50a1270 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 3437 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 3438 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 6fdd57c | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 3439 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(), |
Fariborz Jahanian | 83f45b55 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 3440 | src, dst, "weakassign"); |
| 3441 | return; |
| 3442 | } |
| 3443 | |
Fariborz Jahanian | d7db964 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 3444 | /// EmitObjCGlobalAssign - Code gen for assigning to a __strong object. |
| 3445 | /// objc_assign_global (id src, id *dst) |
| 3446 | /// |
| 3447 | void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 217af24 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 3448 | llvm::Value *src, llvm::Value *dst, |
| 3449 | bool threadlocal) { |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3450 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3451 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | c76fe8b | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 3452 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3453 | assert(Size <= 8 && "does not support size > 8"); |
| 3454 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3455 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 1b074a3 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 3456 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 3457 | } |
Fariborz Jahanian | 50a1270 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 3458 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 3459 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Fariborz Jahanian | 217af24 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 3460 | if (!threadlocal) |
| 3461 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(), |
| 3462 | src, dst, "globalassign"); |
| 3463 | else |
| 3464 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(), |
| 3465 | src, dst, "threadlocalassign"); |
Fariborz Jahanian | d7db964 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 3466 | return; |
| 3467 | } |
| 3468 | |
Fariborz Jahanian | e881b53 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 3469 | /// EmitObjCIvarAssign - Code gen for assigning to a __strong object. |
Fariborz Jahanian | 7a95d72 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 3470 | /// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset) |
Fariborz Jahanian | e881b53 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 3471 | /// |
| 3472 | void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 7a95d72 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 3473 | llvm::Value *src, llvm::Value *dst, |
| 3474 | llvm::Value *ivarOffset) { |
| 3475 | assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3476 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3477 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | c76fe8b | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 3478 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3479 | assert(Size <= 8 && "does not support size > 8"); |
| 3480 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3481 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 1b074a3 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 3482 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 3483 | } |
Fariborz Jahanian | e881b53 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 3484 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 3485 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Fariborz Jahanian | 7a95d72 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 3486 | CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(), |
| 3487 | src, dst, ivarOffset); |
Fariborz Jahanian | e881b53 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 3488 | return; |
| 3489 | } |
| 3490 | |
Fariborz Jahanian | d7db964 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 3491 | /// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object. |
| 3492 | /// objc_assign_strongCast (id src, id *dst) |
| 3493 | /// |
| 3494 | void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3495 | llvm::Value *src, llvm::Value *dst) { |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3496 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3497 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | c76fe8b | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 3498 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3499 | assert(Size <= 8 && "does not support size > 8"); |
| 3500 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3501 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 1b074a3 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 3502 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 3503 | } |
Fariborz Jahanian | 50a1270 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 3504 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 3505 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 0a696a42 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 3506 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(), |
Fariborz Jahanian | d7db964 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 3507 | src, dst, "weakassign"); |
| 3508 | return; |
| 3509 | } |
| 3510 | |
Fariborz Jahanian | 5f21d2f | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 3511 | void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3512 | llvm::Value *DestPtr, |
| 3513 | llvm::Value *SrcPtr, |
Fariborz Jahanian | 021510e | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 3514 | llvm::Value *size) { |
Fariborz Jahanian | 5f21d2f | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 3515 | SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy); |
| 3516 | DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy); |
Fariborz Jahanian | 5f21d2f | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 3517 | CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(), |
Fariborz Jahanian | 021510e | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 3518 | DestPtr, SrcPtr, size); |
Fariborz Jahanian | 5f21d2f | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 3519 | return; |
| 3520 | } |
| 3521 | |
Fariborz Jahanian | 9f84b78 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 3522 | /// EmitObjCValueForIvar - Code Gen for ivar reference. |
| 3523 | /// |
Fariborz Jahanian | 712bfa6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 3524 | LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 3525 | QualType ObjectTy, |
| 3526 | llvm::Value *BaseValue, |
| 3527 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 712bfa6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 3528 | unsigned CVRQualifiers) { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3529 | const ObjCInterfaceDecl *ID = |
| 3530 | ObjectTy->getAs<ObjCObjectType>()->getInterface(); |
Daniel Dunbar | 9fd114d | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 3531 | return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers, |
| 3532 | EmitIvarOffset(CGF, ID, Ivar)); |
Fariborz Jahanian | 9f84b78 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 3533 | } |
| 3534 | |
Fariborz Jahanian | 21fc74c | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 3535 | llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 722f424 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 3536 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | 21fc74c | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 3537 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | 9fd114d | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 3538 | uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar); |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 3539 | return llvm::ConstantInt::get( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3540 | CGM.getTypes().ConvertType(CGM.getContext().LongTy), |
| 3541 | Offset); |
Fariborz Jahanian | 21fc74c | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 3542 | } |
| 3543 | |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3544 | /* *** Private Interface *** */ |
| 3545 | |
| 3546 | /// EmitImageInfo - Emit the image info marker used to encode some module |
| 3547 | /// level information. |
| 3548 | /// |
| 3549 | /// See: <rdr://4810609&4810587&4810587> |
| 3550 | /// struct IMAGE_INFO { |
| 3551 | /// unsigned version; |
| 3552 | /// unsigned flags; |
| 3553 | /// }; |
| 3554 | enum ImageInfoFlags { |
Daniel Dunbar | 5e63927 | 2010-04-25 20:39:01 +0000 | [diff] [blame] | 3555 | eImageInfo_FixAndContinue = (1 << 0), |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3556 | eImageInfo_GarbageCollected = (1 << 1), |
| 3557 | eImageInfo_GCOnly = (1 << 2), |
Daniel Dunbar | 75e909f | 2009-04-20 07:11:47 +0000 | [diff] [blame] | 3558 | eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set. |
| 3559 | |
Daniel Dunbar | 5e63927 | 2010-04-25 20:39:01 +0000 | [diff] [blame] | 3560 | // A flag indicating that the module has no instances of a @synthesize of a |
| 3561 | // superclass variable. <rdar://problem/6803242> |
Daniel Dunbar | 75e909f | 2009-04-20 07:11:47 +0000 | [diff] [blame] | 3562 | eImageInfo_CorrectedSynthesize = (1 << 4) |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3563 | }; |
| 3564 | |
Daniel Dunbar | 5e63927 | 2010-04-25 20:39:01 +0000 | [diff] [blame] | 3565 | void CGObjCCommonMac::EmitImageInfo() { |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3566 | unsigned version = 0; // Version is unused? |
Bill Wendling | b6f795e | 2012-02-16 01:13:30 +0000 | [diff] [blame] | 3567 | const char *Section = (ObjCABI == 1) ? |
| 3568 | "__OBJC, __image_info,regular" : |
| 3569 | "__DATA, __objc_imageinfo, regular, no_dead_strip"; |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3570 | |
Bill Wendling | b6f795e | 2012-02-16 01:13:30 +0000 | [diff] [blame] | 3571 | // Generate module-level named metadata to convey this information to the |
| 3572 | // linker and code-gen. |
| 3573 | llvm::Module &Mod = CGM.getModule(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3574 | |
Bill Wendling | b6f795e | 2012-02-16 01:13:30 +0000 | [diff] [blame] | 3575 | // Add the ObjC ABI version to the module flags. |
| 3576 | Mod.addModuleFlag(llvm::Module::Error, "Objective-C Version", ObjCABI); |
| 3577 | Mod.addModuleFlag(llvm::Module::Error, "Objective-C Image Info Version", |
| 3578 | version); |
| 3579 | Mod.addModuleFlag(llvm::Module::Error, "Objective-C Image Info Section", |
| 3580 | llvm::MDString::get(VMContext,Section)); |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3581 | |
Bill Wendling | b6f795e | 2012-02-16 01:13:30 +0000 | [diff] [blame] | 3582 | if (CGM.getLangOptions().getGC() == LangOptions::NonGC) { |
| 3583 | // Non-GC overrides those files which specify GC. |
| 3584 | Mod.addModuleFlag(llvm::Module::Override, |
| 3585 | "Objective-C Garbage Collection", (uint32_t)0); |
| 3586 | } else { |
| 3587 | // Add the ObjC garbage collection value. |
| 3588 | Mod.addModuleFlag(llvm::Module::Error, |
| 3589 | "Objective-C Garbage Collection", |
| 3590 | eImageInfo_GarbageCollected); |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3591 | |
Bill Wendling | b6f795e | 2012-02-16 01:13:30 +0000 | [diff] [blame] | 3592 | if (CGM.getLangOptions().getGC() == LangOptions::GCOnly) { |
| 3593 | // Add the ObjC GC Only value. |
| 3594 | Mod.addModuleFlag(llvm::Module::Error, "Objective-C GC Only", |
| 3595 | eImageInfo_GCOnly); |
| 3596 | |
| 3597 | // Require that GC be specified and set to eImageInfo_GarbageCollected. |
| 3598 | llvm::Value *Ops[2] = { |
| 3599 | llvm::MDString::get(VMContext, "Objective-C Garbage Collection"), |
| 3600 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), |
| 3601 | eImageInfo_GarbageCollected) |
| 3602 | }; |
| 3603 | Mod.addModuleFlag(llvm::Module::Require, "Objective-C GC Only", |
| 3604 | llvm::MDNode::get(VMContext, Ops)); |
| 3605 | } |
| 3606 | } |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3607 | } |
| 3608 | |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3609 | // struct objc_module { |
| 3610 | // unsigned long version; |
| 3611 | // unsigned long size; |
| 3612 | // const char *name; |
| 3613 | // Symtab symtab; |
| 3614 | // }; |
| 3615 | |
| 3616 | // FIXME: Get from somewhere |
| 3617 | static const int ModuleVersion = 7; |
| 3618 | |
| 3619 | void CGObjCMac::EmitModuleInfo() { |
Duncan Sands | c76fe8b | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 3620 | uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3621 | |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 3622 | llvm::Constant *Values[] = { |
| 3623 | llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion), |
| 3624 | llvm::ConstantInt::get(ObjCTypes.LongTy, Size), |
| 3625 | // This used to be the filename, now it is unused. <rdr://4327263> |
| 3626 | GetClassName(&CGM.getContext().Idents.get("")), |
| 3627 | EmitModuleSymbols() |
| 3628 | }; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3629 | CreateMetadataVar("\01L_OBJC_MODULES", |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 3630 | llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values), |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3631 | "__OBJC,__module_info,regular,no_dead_strip", |
Daniel Dunbar | ae33384 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 3632 | 4, true); |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3633 | } |
| 3634 | |
| 3635 | llvm::Constant *CGObjCMac::EmitModuleSymbols() { |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3636 | unsigned NumClasses = DefinedClasses.size(); |
| 3637 | unsigned NumCategories = DefinedCategories.size(); |
| 3638 | |
Daniel Dunbar | c61d0e9 | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3639 | // Return null if no symbols were defined. |
| 3640 | if (!NumClasses && !NumCategories) |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3641 | return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy); |
Daniel Dunbar | c61d0e9 | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3642 | |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 3643 | llvm::Constant *Values[5]; |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 3644 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3645 | Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy); |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 3646 | Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses); |
| 3647 | Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3648 | |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3649 | // The runtime expects exactly the list of defined classes followed |
| 3650 | // by the list of defined categories, in a single array. |
Chris Lattner | 3def9ae | 2012-02-06 22:16:34 +0000 | [diff] [blame] | 3651 | SmallVector<llvm::Constant*, 8> Symbols(NumClasses + NumCategories); |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3652 | for (unsigned i=0; i<NumClasses; i++) |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 3653 | Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i], |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3654 | ObjCTypes.Int8PtrTy); |
| 3655 | for (unsigned i=0; i<NumCategories; i++) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3656 | Symbols[NumClasses + i] = |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 3657 | llvm::ConstantExpr::getBitCast(DefinedCategories[i], |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3658 | ObjCTypes.Int8PtrTy); |
| 3659 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3660 | Values[4] = |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3661 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
Chris Lattner | 3def9ae | 2012-02-06 22:16:34 +0000 | [diff] [blame] | 3662 | Symbols.size()), |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3663 | Symbols); |
| 3664 | |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 3665 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3666 | |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3667 | llvm::GlobalVariable *GV = |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3668 | CreateMetadataVar("\01L_OBJC_SYMBOLS", Init, |
| 3669 | "__OBJC,__symbols,regular,no_dead_strip", |
Daniel Dunbar | b25452a | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 3670 | 4, true); |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 3671 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3672 | } |
| 3673 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3674 | llvm::Value *CGObjCMac::EmitClassRefFromId(CGBuilderTy &Builder, |
| 3675 | IdentifierInfo *II) { |
| 3676 | LazySymbols.insert(II); |
| 3677 | |
| 3678 | llvm::GlobalVariable *&Entry = ClassReferences[II]; |
| 3679 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3680 | if (!Entry) { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3681 | llvm::Constant *Casted = |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3682 | llvm::ConstantExpr::getBitCast(GetClassName(II), |
| 3683 | ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3684 | Entry = |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3685 | CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted, |
| 3686 | "__OBJC,__cls_refs,literal_pointers,no_dead_strip", |
| 3687 | 4, true); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3688 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3689 | |
Benjamin Kramer | 76399eb | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 3690 | return Builder.CreateLoad(Entry); |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3691 | } |
| 3692 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3693 | llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder, |
| 3694 | const ObjCInterfaceDecl *ID) { |
| 3695 | return EmitClassRefFromId(Builder, ID->getIdentifier()); |
| 3696 | } |
| 3697 | |
| 3698 | llvm::Value *CGObjCMac::EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder) { |
| 3699 | IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool"); |
| 3700 | return EmitClassRefFromId(Builder, II); |
| 3701 | } |
| 3702 | |
Fariborz Jahanian | 9240f3d | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 3703 | llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel, |
| 3704 | bool lvalue) { |
Daniel Dunbar | cb515c8 | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3705 | llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3706 | |
Daniel Dunbar | cb515c8 | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3707 | if (!Entry) { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3708 | llvm::Constant *Casted = |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 3709 | llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), |
Daniel Dunbar | cb515c8 | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3710 | ObjCTypes.SelectorPtrTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3711 | Entry = |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3712 | CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted, |
| 3713 | "__OBJC,__message_refs,literal_pointers,no_dead_strip", |
Daniel Dunbar | b25452a | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 3714 | 4, true); |
Daniel Dunbar | cb515c8 | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3715 | } |
| 3716 | |
Fariborz Jahanian | 9240f3d | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 3717 | if (lvalue) |
| 3718 | return Entry; |
Benjamin Kramer | 76399eb | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 3719 | return Builder.CreateLoad(Entry); |
Daniel Dunbar | cb515c8 | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3720 | } |
| 3721 | |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3722 | llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) { |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3723 | llvm::GlobalVariable *&Entry = ClassNames[Ident]; |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3724 | |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3725 | if (!Entry) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3726 | Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_", |
Chris Lattner | 9c81833 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 3727 | llvm::ConstantDataArray::getString(VMContext, |
| 3728 | Ident->getNameStart()), |
Daniel Dunbar | 7c9295a | 2011-03-25 20:09:09 +0000 | [diff] [blame] | 3729 | ((ObjCABI == 2) ? |
| 3730 | "__TEXT,__objc_classname,cstring_literals" : |
| 3731 | "__TEXT,__cstring,cstring_literals"), |
Daniel Dunbar | 3241fae | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3732 | 1, true); |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3733 | |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3734 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3735 | } |
| 3736 | |
Argyrios Kyrtzidis | 13257c5 | 2010-08-09 10:54:20 +0000 | [diff] [blame] | 3737 | llvm::Function *CGObjCCommonMac::GetMethodDefinition(const ObjCMethodDecl *MD) { |
| 3738 | llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*>::iterator |
| 3739 | I = MethodDefinitions.find(MD); |
| 3740 | if (I != MethodDefinitions.end()) |
| 3741 | return I->second; |
| 3742 | |
Argyrios Kyrtzidis | 13257c5 | 2010-08-09 10:54:20 +0000 | [diff] [blame] | 3743 | return NULL; |
| 3744 | } |
| 3745 | |
Fariborz Jahanian | 01dff42 | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 3746 | /// GetIvarLayoutName - Returns a unique constant for the given |
| 3747 | /// ivar layout bitmap. |
| 3748 | llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3749 | const ObjCCommonTypesHelper &ObjCTypes) { |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3750 | return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Fariborz Jahanian | 01dff42 | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 3751 | } |
| 3752 | |
Daniel Dunbar | 15bd888 | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3753 | void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3754 | unsigned int BytePos, |
Daniel Dunbar | 15bd888 | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3755 | bool ForStrongLayout, |
| 3756 | bool &HasUnion) { |
| 3757 | const RecordDecl *RD = RT->getDecl(); |
| 3758 | // FIXME - Use iterator. |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 3759 | SmallVector<const FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end()); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3760 | llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0)); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3761 | const llvm::StructLayout *RecLayout = |
Daniel Dunbar | 15bd888 | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3762 | CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty)); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3763 | |
Daniel Dunbar | 15bd888 | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3764 | BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos, |
| 3765 | ForStrongLayout, HasUnion); |
| 3766 | } |
| 3767 | |
Daniel Dunbar | 36e2a1e | 2009-05-03 21:05:10 +0000 | [diff] [blame] | 3768 | void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3769 | const llvm::StructLayout *Layout, |
| 3770 | const RecordDecl *RD, |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 3771 | const SmallVectorImpl<const FieldDecl*> &RecFields, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3772 | unsigned int BytePos, bool ForStrongLayout, |
| 3773 | bool &HasUnion) { |
Fariborz Jahanian | 3b0f886 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3774 | bool IsUnion = (RD && RD->isUnion()); |
| 3775 | uint64_t MaxUnionIvarSize = 0; |
| 3776 | uint64_t MaxSkippedUnionIvarSize = 0; |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 3777 | const FieldDecl *MaxField = 0; |
| 3778 | const FieldDecl *MaxSkippedField = 0; |
| 3779 | const FieldDecl *LastFieldBitfieldOrUnnamed = 0; |
Daniel Dunbar | 5b74391 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3780 | uint64_t MaxFieldOffset = 0; |
| 3781 | uint64_t MaxSkippedFieldOffset = 0; |
Argyrios Kyrtzidis | 2fdb5b5 | 2010-09-06 12:00:10 +0000 | [diff] [blame] | 3782 | uint64_t LastBitfieldOrUnnamedOffset = 0; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3783 | uint64_t FirstFieldDelta = 0; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3784 | |
Fariborz Jahanian | 524bb20 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3785 | if (RecFields.empty()) |
| 3786 | return; |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3787 | unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0); |
| 3788 | unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3789 | if (!RD && CGM.getLangOptions().ObjCAutoRefCount) { |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 3790 | const FieldDecl *FirstField = RecFields[0]; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3791 | FirstFieldDelta = |
| 3792 | ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(FirstField)); |
| 3793 | } |
| 3794 | |
Chris Lattner | 5b36ddb | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3795 | for (unsigned i = 0, e = RecFields.size(); i != e; ++i) { |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 3796 | const FieldDecl *Field = RecFields[i]; |
Daniel Dunbar | 62b1070 | 2009-05-03 23:35:23 +0000 | [diff] [blame] | 3797 | uint64_t FieldOffset; |
Anders Carlsson | e2c6baf | 2009-07-24 17:23:54 +0000 | [diff] [blame] | 3798 | if (RD) { |
Daniel Dunbar | d51c1a6 | 2010-04-14 17:02:21 +0000 | [diff] [blame] | 3799 | // Note that 'i' here is actually the field index inside RD of Field, |
| 3800 | // although this dependency is hidden. |
| 3801 | const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3802 | FieldOffset = (RL.getFieldOffset(i) / ByteSizeInBits) - FirstFieldDelta; |
Anders Carlsson | e2c6baf | 2009-07-24 17:23:54 +0000 | [diff] [blame] | 3803 | } else |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3804 | FieldOffset = |
| 3805 | ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field)) - FirstFieldDelta; |
Daniel Dunbar | 94f46dc | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3806 | |
Fariborz Jahanian | 524bb20 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3807 | // Skip over unnamed or bitfields |
Fariborz Jahanian | f5fec02 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3808 | if (!Field->getIdentifier() || Field->isBitField()) { |
Argyrios Kyrtzidis | 2fdb5b5 | 2010-09-06 12:00:10 +0000 | [diff] [blame] | 3809 | LastFieldBitfieldOrUnnamed = Field; |
| 3810 | LastBitfieldOrUnnamedOffset = FieldOffset; |
Fariborz Jahanian | 524bb20 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3811 | continue; |
Fariborz Jahanian | f5fec02 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3812 | } |
Daniel Dunbar | 94f46dc | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3813 | |
Argyrios Kyrtzidis | 2fdb5b5 | 2010-09-06 12:00:10 +0000 | [diff] [blame] | 3814 | LastFieldBitfieldOrUnnamed = 0; |
Fariborz Jahanian | 524bb20 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3815 | QualType FQT = Field->getType(); |
Fariborz Jahanian | f909f92 | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 3816 | if (FQT->isRecordType() || FQT->isUnionType()) { |
Fariborz Jahanian | 524bb20 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3817 | if (FQT->isUnionType()) |
| 3818 | HasUnion = true; |
Fariborz Jahanian | 3b0f886 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3819 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3820 | BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(), |
Daniel Dunbar | 94f46dc | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3821 | BytePos + FieldOffset, |
Daniel Dunbar | 15bd888 | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3822 | ForStrongLayout, HasUnion); |
Fariborz Jahanian | 524bb20 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3823 | continue; |
| 3824 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3825 | |
Chris Lattner | 5b36ddb | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3826 | if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3827 | const ConstantArrayType *CArray = |
Daniel Dunbar | 7abf83c | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3828 | dyn_cast_or_null<ConstantArrayType>(Array); |
Fariborz Jahanian | f5fec02 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3829 | uint64_t ElCount = CArray->getSize().getZExtValue(); |
Daniel Dunbar | 7abf83c | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3830 | assert(CArray && "only array with known element size is supported"); |
Fariborz Jahanian | 3b0f886 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3831 | FQT = CArray->getElementType(); |
Fariborz Jahanian | f909f92 | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 3832 | while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { |
| 3833 | const ConstantArrayType *CArray = |
Daniel Dunbar | 7abf83c | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3834 | dyn_cast_or_null<ConstantArrayType>(Array); |
Fariborz Jahanian | f5fec02 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3835 | ElCount *= CArray->getSize().getZExtValue(); |
Fariborz Jahanian | f909f92 | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 3836 | FQT = CArray->getElementType(); |
| 3837 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3838 | |
| 3839 | assert(!FQT->isUnionType() && |
Fariborz Jahanian | 3b0f886 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3840 | "layout for array of unions not supported"); |
Fariborz Jahanian | 9a7d57d | 2011-01-03 19:23:18 +0000 | [diff] [blame] | 3841 | if (FQT->isRecordType() && ElCount) { |
Fariborz Jahanian | a123b64 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3842 | int OldIndex = IvarsInfo.size() - 1; |
| 3843 | int OldSkIndex = SkipIvars.size() -1; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3844 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3845 | const RecordType *RT = FQT->getAs<RecordType>(); |
Daniel Dunbar | 94f46dc | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3846 | BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset, |
Daniel Dunbar | 15bd888 | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3847 | ForStrongLayout, HasUnion); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3848 | |
Fariborz Jahanian | 3b0f886 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3849 | // Replicate layout information for each array element. Note that |
| 3850 | // one element is already done. |
| 3851 | uint64_t ElIx = 1; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3852 | for (int FirstIndex = IvarsInfo.size() - 1, |
| 3853 | FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) { |
Fariborz Jahanian | 1bf7288 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3854 | uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits; |
Daniel Dunbar | 7b89ace | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3855 | for (int i = OldIndex+1; i <= FirstIndex; ++i) |
| 3856 | IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx, |
| 3857 | IvarsInfo[i].ivar_size)); |
| 3858 | for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i) |
| 3859 | SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx, |
| 3860 | SkipIvars[i].ivar_size)); |
Fariborz Jahanian | 3b0f886 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3861 | } |
| 3862 | continue; |
| 3863 | } |
Fariborz Jahanian | 524bb20 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3864 | } |
Fariborz Jahanian | 3b0f886 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3865 | // At this point, we are done with Record/Union and array there of. |
| 3866 | // For other arrays we are down to its element type. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3867 | Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT); |
Daniel Dunbar | 7abf83c | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3868 | |
Daniel Dunbar | 7b89ace | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3869 | unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType()); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3870 | if ((ForStrongLayout && GCAttr == Qualifiers::Strong) |
| 3871 | || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) { |
Daniel Dunbar | 22007d3 | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3872 | if (IsUnion) { |
Daniel Dunbar | 7b89ace | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3873 | uint64_t UnionIvarSize = FieldSize / WordSizeInBits; |
Daniel Dunbar | 22007d3 | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3874 | if (UnionIvarSize > MaxUnionIvarSize) { |
Fariborz Jahanian | 3b0f886 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3875 | MaxUnionIvarSize = UnionIvarSize; |
| 3876 | MaxField = Field; |
Daniel Dunbar | 5b74391 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3877 | MaxFieldOffset = FieldOffset; |
Fariborz Jahanian | 3b0f886 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3878 | } |
Daniel Dunbar | 22007d3 | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3879 | } else { |
Daniel Dunbar | 94f46dc | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3880 | IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset, |
Daniel Dunbar | 7b89ace | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3881 | FieldSize / WordSizeInBits)); |
Fariborz Jahanian | 3b0f886 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3882 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3883 | } else if ((ForStrongLayout && |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3884 | (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)) |
| 3885 | || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) { |
Daniel Dunbar | 22007d3 | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3886 | if (IsUnion) { |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 3887 | // FIXME: Why the asymmetry? We divide by word size in bits on other |
| 3888 | // side. |
Daniel Dunbar | 7b89ace | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3889 | uint64_t UnionIvarSize = FieldSize; |
Daniel Dunbar | 22007d3 | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3890 | if (UnionIvarSize > MaxSkippedUnionIvarSize) { |
Fariborz Jahanian | 3b0f886 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3891 | MaxSkippedUnionIvarSize = UnionIvarSize; |
| 3892 | MaxSkippedField = Field; |
Daniel Dunbar | 5b74391 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3893 | MaxSkippedFieldOffset = FieldOffset; |
Fariborz Jahanian | 3b0f886 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3894 | } |
Daniel Dunbar | 22007d3 | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3895 | } else { |
Daniel Dunbar | 7b89ace | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3896 | // FIXME: Why the asymmetry, we divide by byte size in bits here? |
Daniel Dunbar | 94f46dc | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3897 | SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset, |
Daniel Dunbar | 7b89ace | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3898 | FieldSize / ByteSizeInBits)); |
Fariborz Jahanian | 3b0f886 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3899 | } |
| 3900 | } |
| 3901 | } |
Daniel Dunbar | 15bd888 | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3902 | |
Argyrios Kyrtzidis | 2fdb5b5 | 2010-09-06 12:00:10 +0000 | [diff] [blame] | 3903 | if (LastFieldBitfieldOrUnnamed) { |
| 3904 | if (LastFieldBitfieldOrUnnamed->isBitField()) { |
| 3905 | // Last field was a bitfield. Must update skip info. |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 3906 | uint64_t BitFieldSize |
| 3907 | = LastFieldBitfieldOrUnnamed->getBitWidthValue(CGM.getContext()); |
Argyrios Kyrtzidis | 2fdb5b5 | 2010-09-06 12:00:10 +0000 | [diff] [blame] | 3908 | GC_IVAR skivar; |
| 3909 | skivar.ivar_bytepos = BytePos + LastBitfieldOrUnnamedOffset; |
| 3910 | skivar.ivar_size = (BitFieldSize / ByteSizeInBits) |
| 3911 | + ((BitFieldSize % ByteSizeInBits) != 0); |
| 3912 | SkipIvars.push_back(skivar); |
| 3913 | } else { |
| 3914 | assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed"); |
| 3915 | // Last field was unnamed. Must update skip info. |
| 3916 | unsigned FieldSize |
| 3917 | = CGM.getContext().getTypeSize(LastFieldBitfieldOrUnnamed->getType()); |
| 3918 | SkipIvars.push_back(GC_IVAR(BytePos + LastBitfieldOrUnnamedOffset, |
| 3919 | FieldSize / ByteSizeInBits)); |
| 3920 | } |
Fariborz Jahanian | f5fec02 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3921 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3922 | |
Daniel Dunbar | 7b89ace | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3923 | if (MaxField) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3924 | IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset, |
Daniel Dunbar | 7b89ace | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3925 | MaxUnionIvarSize)); |
| 3926 | if (MaxSkippedField) |
Daniel Dunbar | 5b74391 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3927 | SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset, |
Daniel Dunbar | 7b89ace | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3928 | MaxSkippedUnionIvarSize)); |
Fariborz Jahanian | c559f3f | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3929 | } |
| 3930 | |
Fariborz Jahanian | 1f78a9a | 2010-08-05 00:19:48 +0000 | [diff] [blame] | 3931 | /// BuildIvarLayoutBitmap - This routine is the horsework for doing all |
| 3932 | /// the computations and returning the layout bitmap (for ivar or blocks) in |
| 3933 | /// the given argument BitMap string container. Routine reads |
| 3934 | /// two containers, IvarsInfo and SkipIvars which are assumed to be |
| 3935 | /// filled already by the caller. |
Chris Lattner | 9c81833 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 3936 | llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(std::string &BitMap) { |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3937 | unsigned int WordsToScan, WordsToSkip; |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 3938 | llvm::Type *PtrTy = CGM.Int8PtrTy; |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3939 | |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3940 | // Build the string of skip/scan nibbles |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3941 | SmallVector<SKIP_SCAN, 32> SkipScanIvars; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3942 | unsigned int WordSize = |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3943 | CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy); |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3944 | if (IvarsInfo[0].ivar_bytepos == 0) { |
| 3945 | WordsToSkip = 0; |
| 3946 | WordsToScan = IvarsInfo[0].ivar_size; |
Daniel Dunbar | d22aa4a | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3947 | } else { |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3948 | WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize; |
| 3949 | WordsToScan = IvarsInfo[0].ivar_size; |
| 3950 | } |
Daniel Dunbar | d22aa4a | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3951 | for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3952 | unsigned int TailPrevGCObjC = |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3953 | IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize; |
Daniel Dunbar | d22aa4a | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3954 | if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) { |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3955 | // consecutive 'scanned' object pointers. |
| 3956 | WordsToScan += IvarsInfo[i].ivar_size; |
Daniel Dunbar | d22aa4a | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3957 | } else { |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3958 | // Skip over 'gc'able object pointer which lay over each other. |
| 3959 | if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos) |
| 3960 | continue; |
| 3961 | // Must skip over 1 or more words. We save current skip/scan values |
| 3962 | // and start a new pair. |
Fariborz Jahanian | 1bf7288 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3963 | SKIP_SCAN SkScan; |
| 3964 | SkScan.skip = WordsToSkip; |
| 3965 | SkScan.scan = WordsToScan; |
Fariborz Jahanian | a123b64 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3966 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3967 | |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3968 | // Skip the hole. |
Fariborz Jahanian | 1bf7288 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3969 | SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize; |
| 3970 | SkScan.scan = 0; |
Fariborz Jahanian | a123b64 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3971 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3972 | WordsToSkip = 0; |
| 3973 | WordsToScan = IvarsInfo[i].ivar_size; |
| 3974 | } |
| 3975 | } |
Daniel Dunbar | d22aa4a | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3976 | if (WordsToScan > 0) { |
Fariborz Jahanian | 1bf7288 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3977 | SKIP_SCAN SkScan; |
| 3978 | SkScan.skip = WordsToSkip; |
| 3979 | SkScan.scan = WordsToScan; |
Fariborz Jahanian | a123b64 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3980 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3981 | } |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3982 | |
Daniel Dunbar | d22aa4a | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3983 | if (!SkipIvars.empty()) { |
Fariborz Jahanian | a123b64 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3984 | unsigned int LastIndex = SkipIvars.size()-1; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3985 | int LastByteSkipped = |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3986 | SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size; |
Fariborz Jahanian | a123b64 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3987 | LastIndex = IvarsInfo.size()-1; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3988 | int LastByteScanned = |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3989 | IvarsInfo[LastIndex].ivar_bytepos + |
| 3990 | IvarsInfo[LastIndex].ivar_size * WordSize; |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3991 | // 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] | 3992 | if (LastByteSkipped > LastByteScanned) { |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3993 | unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize; |
Fariborz Jahanian | 1bf7288 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3994 | SKIP_SCAN SkScan; |
| 3995 | SkScan.skip = TotalWords - (LastByteScanned/WordSize); |
| 3996 | SkScan.scan = 0; |
Fariborz Jahanian | a123b64 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3997 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3998 | } |
| 3999 | } |
| 4000 | // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced |
| 4001 | // as 0xMN. |
Fariborz Jahanian | a123b64 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 4002 | int SkipScan = SkipScanIvars.size()-1; |
Daniel Dunbar | d22aa4a | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4003 | for (int i = 0; i <= SkipScan; i++) { |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4004 | if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0 |
| 4005 | && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) { |
| 4006 | // 0xM0 followed by 0x0N detected. |
| 4007 | SkipScanIvars[i].scan = SkipScanIvars[i+1].scan; |
| 4008 | for (int j = i+1; j < SkipScan; j++) |
| 4009 | SkipScanIvars[j] = SkipScanIvars[j+1]; |
| 4010 | --SkipScan; |
| 4011 | } |
| 4012 | } |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4013 | |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4014 | // Generate the string. |
Daniel Dunbar | d22aa4a | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4015 | for (int i = 0; i <= SkipScan; i++) { |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4016 | unsigned char byte; |
| 4017 | unsigned int skip_small = SkipScanIvars[i].skip % 0xf; |
| 4018 | unsigned int scan_small = SkipScanIvars[i].scan % 0xf; |
| 4019 | unsigned int skip_big = SkipScanIvars[i].skip / 0xf; |
| 4020 | unsigned int scan_big = SkipScanIvars[i].scan / 0xf; |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4021 | |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4022 | // first skip big. |
| 4023 | for (unsigned int ix = 0; ix < skip_big; ix++) |
| 4024 | BitMap += (unsigned char)(0xf0); |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4025 | |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4026 | // next (skip small, scan) |
Daniel Dunbar | d22aa4a | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4027 | if (skip_small) { |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4028 | byte = skip_small << 4; |
Daniel Dunbar | d22aa4a | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4029 | if (scan_big > 0) { |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4030 | byte |= 0xf; |
| 4031 | --scan_big; |
Daniel Dunbar | d22aa4a | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4032 | } else if (scan_small) { |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4033 | byte |= scan_small; |
| 4034 | scan_small = 0; |
| 4035 | } |
| 4036 | BitMap += byte; |
| 4037 | } |
| 4038 | // next scan big |
| 4039 | for (unsigned int ix = 0; ix < scan_big; ix++) |
| 4040 | BitMap += (unsigned char)(0x0f); |
| 4041 | // last scan small |
Daniel Dunbar | d22aa4a | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4042 | if (scan_small) { |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4043 | byte = scan_small; |
| 4044 | BitMap += byte; |
| 4045 | } |
| 4046 | } |
| 4047 | // null terminate string. |
Fariborz Jahanian | f909f92 | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 4048 | unsigned char zero = 0; |
| 4049 | BitMap += zero; |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4050 | |
| 4051 | llvm::GlobalVariable * Entry = |
| 4052 | CreateMetadataVar("\01L_OBJC_CLASS_NAME_", |
Chris Lattner | 9c81833 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 4053 | llvm::ConstantDataArray::getString(VMContext, BitMap,false), |
Daniel Dunbar | 7c9295a | 2011-03-25 20:09:09 +0000 | [diff] [blame] | 4054 | ((ObjCABI == 2) ? |
| 4055 | "__TEXT,__objc_classname,cstring_literals" : |
| 4056 | "__TEXT,__cstring,cstring_literals"), |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4057 | 1, true); |
| 4058 | return getConstantGEP(VMContext, Entry, 0, 0); |
| 4059 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4060 | |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4061 | /// BuildIvarLayout - Builds ivar layout bitmap for the class |
| 4062 | /// implementation for the __strong or __weak case. |
| 4063 | /// The layout map displays which words in ivar list must be skipped |
| 4064 | /// and which must be scanned by GC (see below). String is built of bytes. |
| 4065 | /// Each byte is divided up in two nibbles (4-bit each). Left nibble is count |
| 4066 | /// of words to skip and right nibble is count of words to scan. So, each |
| 4067 | /// nibble represents up to 15 workds to skip or scan. Skipping the rest is |
| 4068 | /// represented by a 0x00 byte which also ends the string. |
| 4069 | /// 1. when ForStrongLayout is true, following ivars are scanned: |
| 4070 | /// - id, Class |
| 4071 | /// - object * |
| 4072 | /// - __strong anything |
| 4073 | /// |
| 4074 | /// 2. When ForStrongLayout is false, following ivars are scanned: |
| 4075 | /// - __weak anything |
| 4076 | /// |
| 4077 | llvm::Constant *CGObjCCommonMac::BuildIvarLayout( |
| 4078 | const ObjCImplementationDecl *OMD, |
| 4079 | bool ForStrongLayout) { |
| 4080 | bool hasUnion = false; |
| 4081 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4082 | llvm::Type *PtrTy = CGM.Int8PtrTy; |
Douglas Gregor | 79a9141 | 2011-09-13 17:21:33 +0000 | [diff] [blame] | 4083 | if (CGM.getLangOptions().getGC() == LangOptions::NonGC && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4084 | !CGM.getLangOptions().ObjCAutoRefCount) |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4085 | return llvm::Constant::getNullValue(PtrTy); |
| 4086 | |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 4087 | const ObjCInterfaceDecl *OI = OMD->getClassInterface(); |
| 4088 | SmallVector<const FieldDecl*, 32> RecFields; |
Fariborz Jahanian | b26d578 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 4089 | if (CGM.getLangOptions().ObjCAutoRefCount) { |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 4090 | for (const ObjCIvarDecl *IVD = OI->all_declared_ivar_begin(); |
Fariborz Jahanian | b26d578 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 4091 | IVD; IVD = IVD->getNextIvar()) |
| 4092 | RecFields.push_back(cast<FieldDecl>(IVD)); |
| 4093 | } |
| 4094 | else { |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 4095 | SmallVector<const ObjCIvarDecl*, 32> Ivars; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4096 | CGM.getContext().DeepCollectObjCIvars(OI, true, Ivars); |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4097 | |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 4098 | // FIXME: This is not ideal; we shouldn't have to do this copy. |
| 4099 | RecFields.append(Ivars.begin(), Ivars.end()); |
Fariborz Jahanian | b26d578 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 4100 | } |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4101 | |
| 4102 | if (RecFields.empty()) |
| 4103 | return llvm::Constant::getNullValue(PtrTy); |
| 4104 | |
| 4105 | SkipIvars.clear(); |
| 4106 | IvarsInfo.clear(); |
| 4107 | |
| 4108 | BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion); |
| 4109 | if (IvarsInfo.empty()) |
| 4110 | return llvm::Constant::getNullValue(PtrTy); |
Fariborz Jahanian | 1f78a9a | 2010-08-05 00:19:48 +0000 | [diff] [blame] | 4111 | // Sort on byte position in case we encounterred a union nested in |
| 4112 | // the ivar list. |
| 4113 | if (hasUnion && !IvarsInfo.empty()) |
| 4114 | std::sort(IvarsInfo.begin(), IvarsInfo.end()); |
| 4115 | if (hasUnion && !SkipIvars.empty()) |
| 4116 | std::sort(SkipIvars.begin(), SkipIvars.end()); |
| 4117 | |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4118 | std::string BitMap; |
Fariborz Jahanian | 1f78a9a | 2010-08-05 00:19:48 +0000 | [diff] [blame] | 4119 | llvm::Constant *C = BuildIvarLayoutBitmap(BitMap); |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4120 | |
| 4121 | if (CGM.getLangOptions().ObjCGCBitmapPrint) { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4122 | printf("\n%s ivar layout for class '%s': ", |
Fariborz Jahanian | 80c9ce2 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 4123 | ForStrongLayout ? "strong" : "weak", |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4124 | OMD->getClassInterface()->getName().data()); |
Fariborz Jahanian | 80c9ce2 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 4125 | const unsigned char *s = (unsigned char*)BitMap.c_str(); |
Bill Wendling | 5313685 | 2012-02-07 09:06:01 +0000 | [diff] [blame] | 4126 | for (unsigned i = 0, e = BitMap.size(); i < e; i++) |
Fariborz Jahanian | 80c9ce2 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 4127 | if (!(s[i] & 0xf0)) |
| 4128 | printf("0x0%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 4129 | else |
| 4130 | printf("0x%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 4131 | printf("\n"); |
| 4132 | } |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4133 | return C; |
Fariborz Jahanian | c559f3f | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 4134 | } |
| 4135 | |
Fariborz Jahanian | 0b1ccdc | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 4136 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) { |
Daniel Dunbar | cb515c8 | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 4137 | llvm::GlobalVariable *&Entry = MethodVarNames[Sel]; |
| 4138 | |
Chris Lattner | 3def9ae | 2012-02-06 22:16:34 +0000 | [diff] [blame] | 4139 | // FIXME: Avoid std::string in "Sel.getAsString()" |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 4140 | if (!Entry) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4141 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_", |
Chris Lattner | 9c81833 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 4142 | llvm::ConstantDataArray::getString(VMContext, Sel.getAsString()), |
Daniel Dunbar | 7c9295a | 2011-03-25 20:09:09 +0000 | [diff] [blame] | 4143 | ((ObjCABI == 2) ? |
| 4144 | "__TEXT,__objc_methname,cstring_literals" : |
| 4145 | "__TEXT,__cstring,cstring_literals"), |
Daniel Dunbar | 3241fae | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 4146 | 1, true); |
Daniel Dunbar | cb515c8 | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 4147 | |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4148 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4149 | } |
| 4150 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4151 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 0b1ccdc | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 4152 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) { |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4153 | return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID)); |
| 4154 | } |
| 4155 | |
Daniel Dunbar | f5c1846 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 4156 | llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) { |
Devang Patel | 4b6e4bb | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 4157 | std::string TypeStr; |
| 4158 | CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field); |
| 4159 | |
| 4160 | llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr]; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4161 | |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 4162 | if (!Entry) |
| 4163 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_", |
Chris Lattner | 9c81833 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 4164 | llvm::ConstantDataArray::getString(VMContext, TypeStr), |
Daniel Dunbar | 7c9295a | 2011-03-25 20:09:09 +0000 | [diff] [blame] | 4165 | ((ObjCABI == 2) ? |
| 4166 | "__TEXT,__objc_methtype,cstring_literals" : |
| 4167 | "__TEXT,__cstring,cstring_literals"), |
Daniel Dunbar | 3241fae | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 4168 | 1, true); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4169 | |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4170 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | cb515c8 | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 4171 | } |
| 4172 | |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4173 | llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D, |
| 4174 | bool Extended) { |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4175 | std::string TypeStr; |
Bill Wendling | e22bef7 | 2012-02-09 22:45:21 +0000 | [diff] [blame] | 4176 | if (CGM.getContext().getObjCEncodingForMethodDecl(D, TypeStr, Extended)) |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4177 | return 0; |
Devang Patel | 4b6e4bb | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 4178 | |
| 4179 | llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr]; |
| 4180 | |
Daniel Dunbar | 3241fae | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 4181 | if (!Entry) |
| 4182 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_", |
Chris Lattner | 9c81833 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 4183 | llvm::ConstantDataArray::getString(VMContext, TypeStr), |
Daniel Dunbar | 7c9295a | 2011-03-25 20:09:09 +0000 | [diff] [blame] | 4184 | ((ObjCABI == 2) ? |
| 4185 | "__TEXT,__objc_methtype,cstring_literals" : |
| 4186 | "__TEXT,__cstring,cstring_literals"), |
Daniel Dunbar | 3241fae | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 4187 | 1, true); |
Devang Patel | 4b6e4bb | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 4188 | |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4189 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4190 | } |
| 4191 | |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 4192 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 0b1ccdc | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 4193 | llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) { |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 4194 | llvm::GlobalVariable *&Entry = PropertyNames[Ident]; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4195 | |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 4196 | if (!Entry) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4197 | Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_", |
Chris Lattner | 9c81833 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 4198 | llvm::ConstantDataArray::getString(VMContext, |
| 4199 | Ident->getNameStart()), |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 4200 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | 3241fae | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 4201 | 1, true); |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 4202 | |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4203 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 4204 | } |
| 4205 | |
| 4206 | // FIXME: Merge into a single cstring creation function. |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4207 | // FIXME: This Decl should be more precise. |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 4208 | llvm::Constant * |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4209 | CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD, |
| 4210 | const Decl *Container) { |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4211 | std::string TypeStr; |
| 4212 | CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr); |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 4213 | return GetPropertyName(&CGM.getContext().Idents.get(TypeStr)); |
| 4214 | } |
| 4215 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4216 | void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D, |
Fariborz Jahanian | 0b1ccdc | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 4217 | const ObjCContainerDecl *CD, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4218 | SmallVectorImpl<char> &Name) { |
Daniel Dunbar | d238681 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 4219 | llvm::raw_svector_ostream OS(Name); |
Fariborz Jahanian | 0196a1c | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 4220 | assert (CD && "Missing container decl in GetNameForMethod"); |
Daniel Dunbar | d238681 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 4221 | OS << '\01' << (D->isInstanceMethod() ? '-' : '+') |
| 4222 | << '[' << CD->getName(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4223 | if (const ObjCCategoryImplDecl *CID = |
Daniel Dunbar | d238681 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 4224 | dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext())) |
Benjamin Kramer | 2f56992 | 2012-02-07 11:57:45 +0000 | [diff] [blame] | 4225 | OS << '(' << *CID << ')'; |
Daniel Dunbar | d238681 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 4226 | OS << ' ' << D->getSelector().getAsString() << ']'; |
Daniel Dunbar | a94ecd2 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 4227 | } |
| 4228 | |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 4229 | void CGObjCMac::FinishModule() { |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4230 | EmitModuleInfo(); |
| 4231 | |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 4232 | // Emit the dummy bodies for any protocols which were referenced but |
| 4233 | // never defined. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4234 | for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4235 | I = Protocols.begin(), e = Protocols.end(); I != e; ++I) { |
| 4236 | if (I->second->hasInitializer()) |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 4237 | continue; |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4238 | |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 4239 | llvm::Constant *Values[5]; |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4240 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4241 | Values[1] = GetClassName(I->first); |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4242 | Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 4243 | Values[3] = Values[4] = |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4244 | llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy); |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4245 | I->second->setLinkage(llvm::GlobalValue::InternalLinkage); |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 4246 | I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 4247 | Values)); |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4248 | CGM.AddUsedGlobal(I->second); |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 4249 | } |
| 4250 | |
Daniel Dunbar | c61d0e9 | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 4251 | // Add assembler directives to add lazy undefined symbol references |
| 4252 | // for classes which are referenced but not defined. This is |
| 4253 | // important for correct linker interaction. |
Daniel Dunbar | d027a92 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 4254 | // |
| 4255 | // FIXME: It would be nice if we had an LLVM construct for this. |
| 4256 | if (!LazySymbols.empty() || !DefinedSymbols.empty()) { |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 4257 | SmallString<256> Asm; |
Daniel Dunbar | d027a92 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 4258 | Asm += CGM.getModule().getModuleInlineAsm(); |
| 4259 | if (!Asm.empty() && Asm.back() != '\n') |
| 4260 | Asm += '\n'; |
Daniel Dunbar | c61d0e9 | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 4261 | |
Daniel Dunbar | d027a92 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 4262 | llvm::raw_svector_ostream OS(Asm); |
Daniel Dunbar | d027a92 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 4263 | for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(), |
| 4264 | e = DefinedSymbols.end(); I != e; ++I) |
Daniel Dunbar | 07d0785 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 4265 | OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n" |
| 4266 | << "\t.globl .objc_class_name_" << (*I)->getName() << "\n"; |
Chris Lattner | a299f2c | 2010-04-17 18:26:20 +0000 | [diff] [blame] | 4267 | for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(), |
Fariborz Jahanian | 9adb2e6 | 2010-06-21 22:05:18 +0000 | [diff] [blame] | 4268 | e = LazySymbols.end(); I != e; ++I) { |
Chris Lattner | a299f2c | 2010-04-17 18:26:20 +0000 | [diff] [blame] | 4269 | OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n"; |
Fariborz Jahanian | 9adb2e6 | 2010-06-21 22:05:18 +0000 | [diff] [blame] | 4270 | } |
| 4271 | |
Bill Wendling | 5313685 | 2012-02-07 09:06:01 +0000 | [diff] [blame] | 4272 | for (size_t i = 0, e = DefinedCategoryNames.size(); i < e; ++i) { |
Fariborz Jahanian | 9adb2e6 | 2010-06-21 22:05:18 +0000 | [diff] [blame] | 4273 | OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n" |
| 4274 | << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n"; |
| 4275 | } |
Chris Lattner | a299f2c | 2010-04-17 18:26:20 +0000 | [diff] [blame] | 4276 | |
Daniel Dunbar | d027a92 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 4277 | CGM.getModule().setModuleInlineAsm(OS.str()); |
Daniel Dunbar | c61d0e9 | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 4278 | } |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 4279 | } |
| 4280 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4281 | CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm) |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4282 | : CGObjCCommonMac(cgm), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4283 | ObjCTypes(cgm) { |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4284 | ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL; |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4285 | ObjCABI = 2; |
| 4286 | } |
| 4287 | |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 4288 | /* *** */ |
| 4289 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4290 | ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm) |
Douglas Gregor | a95f9aa | 2012-01-17 23:38:32 +0000 | [diff] [blame] | 4291 | : VMContext(cgm.getLLVMContext()), CGM(cgm), ExternalProtocolPtrTy(0) |
| 4292 | { |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4293 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 4294 | ASTContext &Ctx = CGM.getContext(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4295 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4296 | ShortTy = Types.ConvertType(Ctx.ShortTy); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4297 | IntTy = Types.ConvertType(Ctx.IntTy); |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4298 | LongTy = Types.ConvertType(Ctx.LongTy); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 4299 | LongLongTy = Types.ConvertType(Ctx.LongLongTy); |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4300 | Int8PtrTy = CGM.Int8PtrTy; |
| 4301 | Int8PtrPtrTy = CGM.Int8PtrPtrTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4302 | |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4303 | ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType()); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4304 | PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy); |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4305 | SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType()); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4306 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4307 | // I'm not sure I like this. The implicit coordination is a bit |
| 4308 | // gross. We should solve this in a reasonable fashion because this |
| 4309 | // is a pretty common task (match some runtime data structure with |
| 4310 | // an LLVM data structure). |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4311 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4312 | // FIXME: This is leaked. |
| 4313 | // FIXME: Merge with rewriter code? |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4314 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4315 | // struct _objc_super { |
| 4316 | // id self; |
| 4317 | // Class cls; |
| 4318 | // } |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 4319 | RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct, |
Daniel Dunbar | 0c00537 | 2010-04-29 16:29:11 +0000 | [diff] [blame] | 4320 | Ctx.getTranslationUnitDecl(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4321 | SourceLocation(), SourceLocation(), |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4322 | &Ctx.Idents.get("_objc_super")); |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4323 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0, |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 4324 | Ctx.getObjCIdType(), 0, 0, false, false)); |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4325 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0, |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 4326 | Ctx.getObjCClassType(), 0, 0, false, false)); |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 4327 | RD->completeDefinition(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4328 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4329 | SuperCTy = Ctx.getTagDeclType(RD); |
| 4330 | SuperPtrCTy = Ctx.getPointerType(SuperCTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4331 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4332 | SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy)); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4333 | SuperPtrTy = llvm::PointerType::getUnqual(SuperTy); |
| 4334 | |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4335 | // struct _prop_t { |
| 4336 | // char *name; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4337 | // char *attributes; |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4338 | // } |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4339 | PropertyTy = llvm::StructType::create("struct._prop_t", |
| 4340 | Int8PtrTy, Int8PtrTy, NULL); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4341 | |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4342 | // struct _prop_list_t { |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4343 | // uint32_t entsize; // sizeof(struct _prop_t) |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4344 | // uint32_t count_of_properties; |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4345 | // struct _prop_t prop_list[count_of_properties]; |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4346 | // } |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4347 | PropertyListTy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4348 | llvm::StructType::create("struct._prop_list_t", IntTy, IntTy, |
| 4349 | llvm::ArrayType::get(PropertyTy, 0), NULL); |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4350 | // struct _prop_list_t * |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4351 | PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4352 | |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4353 | // struct _objc_method { |
| 4354 | // SEL _cmd; |
| 4355 | // char *method_type; |
| 4356 | // char *_imp; |
| 4357 | // } |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4358 | MethodTy = llvm::StructType::create("struct._objc_method", |
| 4359 | SelectorPtrTy, Int8PtrTy, Int8PtrTy, |
| 4360 | NULL); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4361 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4362 | // struct _objc_cache * |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4363 | CacheTy = llvm::StructType::create(VMContext, "struct._objc_cache"); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4364 | CachePtrTy = llvm::PointerType::getUnqual(CacheTy); |
Fariborz Jahanian | 0a3cfcc | 2011-06-22 20:21:51 +0000 | [diff] [blame] | 4365 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4366 | } |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4367 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4368 | ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4369 | : ObjCCommonTypesHelper(cgm) { |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4370 | // struct _objc_method_description { |
| 4371 | // SEL name; |
| 4372 | // char *types; |
| 4373 | // } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4374 | MethodDescriptionTy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4375 | llvm::StructType::create("struct._objc_method_description", |
| 4376 | SelectorPtrTy, Int8PtrTy, NULL); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4377 | |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4378 | // struct _objc_method_description_list { |
| 4379 | // int count; |
| 4380 | // struct _objc_method_description[1]; |
| 4381 | // } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4382 | MethodDescriptionListTy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4383 | llvm::StructType::create("struct._objc_method_description_list", |
| 4384 | IntTy, |
| 4385 | llvm::ArrayType::get(MethodDescriptionTy, 0),NULL); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4386 | |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4387 | // struct _objc_method_description_list * |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4388 | MethodDescriptionListPtrTy = |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4389 | llvm::PointerType::getUnqual(MethodDescriptionListTy); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4390 | |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4391 | // Protocol description structures |
| 4392 | |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4393 | // struct _objc_protocol_extension { |
| 4394 | // uint32_t size; // sizeof(struct _objc_protocol_extension) |
| 4395 | // struct _objc_method_description_list *optional_instance_methods; |
| 4396 | // struct _objc_method_description_list *optional_class_methods; |
| 4397 | // struct _objc_property_list *instance_properties; |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4398 | // const char ** extendedMethodTypes; |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4399 | // } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4400 | ProtocolExtensionTy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4401 | llvm::StructType::create("struct._objc_protocol_extension", |
| 4402 | IntTy, MethodDescriptionListPtrTy, |
| 4403 | MethodDescriptionListPtrTy, PropertyListPtrTy, |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4404 | Int8PtrPtrTy, NULL); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4405 | |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4406 | // struct _objc_protocol_extension * |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4407 | ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4408 | |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 4409 | // Handle recursive construction of Protocol and ProtocolList types |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4410 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4411 | ProtocolTy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4412 | llvm::StructType::create(VMContext, "struct._objc_protocol"); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4413 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4414 | ProtocolListTy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4415 | llvm::StructType::create(VMContext, "struct._objc_protocol_list"); |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4416 | ProtocolListTy->setBody(llvm::PointerType::getUnqual(ProtocolListTy), |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4417 | LongTy, |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4418 | llvm::ArrayType::get(ProtocolTy, 0), |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4419 | NULL); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4420 | |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4421 | // struct _objc_protocol { |
| 4422 | // struct _objc_protocol_extension *isa; |
| 4423 | // char *protocol_name; |
| 4424 | // struct _objc_protocol **_objc_protocol_list; |
| 4425 | // struct _objc_method_description_list *instance_methods; |
| 4426 | // struct _objc_method_description_list *class_methods; |
| 4427 | // } |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4428 | ProtocolTy->setBody(ProtocolExtensionPtrTy, Int8PtrTy, |
| 4429 | llvm::PointerType::getUnqual(ProtocolListTy), |
| 4430 | MethodDescriptionListPtrTy, |
| 4431 | MethodDescriptionListPtrTy, |
| 4432 | NULL); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4433 | |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4434 | // struct _objc_protocol_list * |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4435 | ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4436 | |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4437 | ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4438 | |
| 4439 | // Class description structures |
| 4440 | |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4441 | // struct _objc_ivar { |
| 4442 | // char *ivar_name; |
| 4443 | // char *ivar_type; |
| 4444 | // int ivar_offset; |
| 4445 | // } |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4446 | IvarTy = llvm::StructType::create("struct._objc_ivar", |
| 4447 | Int8PtrTy, Int8PtrTy, IntTy, NULL); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4448 | |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4449 | // struct _objc_ivar_list * |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4450 | IvarListTy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4451 | llvm::StructType::create(VMContext, "struct._objc_ivar_list"); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4452 | IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4453 | |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4454 | // struct _objc_method_list * |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4455 | MethodListTy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4456 | llvm::StructType::create(VMContext, "struct._objc_method_list"); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4457 | MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4458 | |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4459 | // struct _objc_class_extension * |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4460 | ClassExtensionTy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4461 | llvm::StructType::create("struct._objc_class_extension", |
| 4462 | IntTy, Int8PtrTy, PropertyListPtrTy, NULL); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4463 | ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4464 | |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4465 | ClassTy = llvm::StructType::create(VMContext, "struct._objc_class"); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4466 | |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4467 | // struct _objc_class { |
| 4468 | // Class isa; |
| 4469 | // Class super_class; |
| 4470 | // char *name; |
| 4471 | // long version; |
| 4472 | // long info; |
| 4473 | // long instance_size; |
| 4474 | // struct _objc_ivar_list *ivars; |
| 4475 | // struct _objc_method_list *methods; |
| 4476 | // struct _objc_cache *cache; |
| 4477 | // struct _objc_protocol_list *protocols; |
| 4478 | // char *ivar_layout; |
| 4479 | // struct _objc_class_ext *ext; |
| 4480 | // }; |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4481 | ClassTy->setBody(llvm::PointerType::getUnqual(ClassTy), |
| 4482 | llvm::PointerType::getUnqual(ClassTy), |
| 4483 | Int8PtrTy, |
| 4484 | LongTy, |
| 4485 | LongTy, |
| 4486 | LongTy, |
| 4487 | IvarListPtrTy, |
| 4488 | MethodListPtrTy, |
| 4489 | CachePtrTy, |
| 4490 | ProtocolListPtrTy, |
| 4491 | Int8PtrTy, |
| 4492 | ClassExtensionPtrTy, |
| 4493 | NULL); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4494 | |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4495 | ClassPtrTy = llvm::PointerType::getUnqual(ClassTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4496 | |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4497 | // struct _objc_category { |
| 4498 | // char *category_name; |
| 4499 | // char *class_name; |
| 4500 | // struct _objc_method_list *instance_method; |
| 4501 | // struct _objc_method_list *class_method; |
| 4502 | // uint32_t size; // sizeof(struct _objc_category) |
| 4503 | // struct _objc_property_list *instance_properties;// category's @property |
| 4504 | // } |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4505 | CategoryTy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4506 | llvm::StructType::create("struct._objc_category", |
| 4507 | Int8PtrTy, Int8PtrTy, MethodListPtrTy, |
| 4508 | MethodListPtrTy, ProtocolListPtrTy, |
| 4509 | IntTy, PropertyListPtrTy, NULL); |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 4510 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4511 | // Global metadata structures |
| 4512 | |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4513 | // struct _objc_symtab { |
| 4514 | // long sel_ref_cnt; |
| 4515 | // SEL *refs; |
| 4516 | // short cls_def_cnt; |
| 4517 | // short cat_def_cnt; |
| 4518 | // char *defs[cls_def_cnt + cat_def_cnt]; |
| 4519 | // } |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4520 | SymtabTy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4521 | llvm::StructType::create("struct._objc_symtab", |
| 4522 | LongTy, SelectorPtrTy, ShortTy, ShortTy, |
| 4523 | llvm::ArrayType::get(Int8PtrTy, 0), NULL); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4524 | SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4525 | |
Fariborz Jahanian | eee54df | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 4526 | // struct _objc_module { |
| 4527 | // long version; |
| 4528 | // long size; // sizeof(struct _objc_module) |
| 4529 | // char *name; |
| 4530 | // struct _objc_symtab* symtab; |
| 4531 | // } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4532 | ModuleTy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4533 | llvm::StructType::create("struct._objc_module", |
| 4534 | LongTy, LongTy, Int8PtrTy, SymtabPtrTy, NULL); |
Daniel Dunbar | 97ff50d | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 4535 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4536 | |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 4537 | // FIXME: This is the size of the setjmp buffer and should be target |
| 4538 | // specific. 18 is what's used on 32-bit X86. |
Anders Carlsson | 9ff2248 | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 4539 | uint64_t SetJmpBufferSize = 18; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4540 | |
Anders Carlsson | 9ff2248 | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 4541 | // Exceptions |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4542 | llvm::Type *StackPtrTy = llvm::ArrayType::get(CGM.Int8PtrTy, 4); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4543 | |
| 4544 | ExceptionDataTy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4545 | llvm::StructType::create("struct._objc_exception_data", |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4546 | llvm::ArrayType::get(CGM.Int32Ty,SetJmpBufferSize), |
| 4547 | StackPtrTy, NULL); |
Anders Carlsson | 9ff2248 | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 4548 | |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 4549 | } |
| 4550 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4551 | ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4552 | : ObjCCommonTypesHelper(cgm) { |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4553 | // struct _method_list_t { |
| 4554 | // uint32_t entsize; // sizeof(struct _objc_method) |
| 4555 | // uint32_t method_count; |
| 4556 | // struct _objc_method method_list[method_count]; |
| 4557 | // } |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4558 | MethodListnfABITy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4559 | llvm::StructType::create("struct.__method_list_t", IntTy, IntTy, |
| 4560 | llvm::ArrayType::get(MethodTy, 0), NULL); |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4561 | // struct method_list_t * |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4562 | MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4563 | |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4564 | // struct _protocol_t { |
| 4565 | // id isa; // NULL |
| 4566 | // const char * const protocol_name; |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4567 | // const struct _protocol_list_t * protocol_list; // super protocols |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4568 | // const struct method_list_t * const instance_methods; |
| 4569 | // const struct method_list_t * const class_methods; |
| 4570 | // const struct method_list_t *optionalInstanceMethods; |
| 4571 | // const struct method_list_t *optionalClassMethods; |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4572 | // const struct _prop_list_t * properties; |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4573 | // const uint32_t size; // sizeof(struct _protocol_t) |
| 4574 | // const uint32_t flags; // = 0 |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4575 | // const char ** extendedMethodTypes; |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4576 | // } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4577 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4578 | // Holder for struct _protocol_list_t * |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4579 | ProtocolListnfABITy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4580 | llvm::StructType::create(VMContext, "struct._objc_protocol_list"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4581 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4582 | ProtocolnfABITy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4583 | llvm::StructType::create("struct._protocol_t", ObjectPtrTy, Int8PtrTy, |
| 4584 | llvm::PointerType::getUnqual(ProtocolListnfABITy), |
| 4585 | MethodListnfABIPtrTy, MethodListnfABIPtrTy, |
| 4586 | MethodListnfABIPtrTy, MethodListnfABIPtrTy, |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4587 | PropertyListPtrTy, IntTy, IntTy, Int8PtrPtrTy, |
| 4588 | NULL); |
Daniel Dunbar | 8de90f0 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4589 | |
| 4590 | // struct _protocol_t* |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4591 | ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4592 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4593 | // struct _protocol_list_t { |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4594 | // long protocol_count; // Note, this is 32/64 bit |
Daniel Dunbar | 8de90f0 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4595 | // struct _protocol_t *[protocol_count]; |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4596 | // } |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4597 | ProtocolListnfABITy->setBody(LongTy, |
| 4598 | llvm::ArrayType::get(ProtocolnfABIPtrTy, 0), |
| 4599 | NULL); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4600 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4601 | // struct _objc_protocol_list* |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4602 | ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4603 | |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4604 | // struct _ivar_t { |
| 4605 | // unsigned long int *offset; // pointer to ivar offset location |
| 4606 | // char *name; |
| 4607 | // char *type; |
| 4608 | // uint32_t alignment; |
| 4609 | // uint32_t size; |
| 4610 | // } |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4611 | IvarnfABITy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4612 | llvm::StructType::create("struct._ivar_t", |
| 4613 | llvm::PointerType::getUnqual(LongTy), |
| 4614 | Int8PtrTy, Int8PtrTy, IntTy, IntTy, NULL); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4615 | |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4616 | // struct _ivar_list_t { |
| 4617 | // uint32 entsize; // sizeof(struct _ivar_t) |
| 4618 | // uint32 count; |
| 4619 | // struct _iver_t list[count]; |
| 4620 | // } |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4621 | IvarListnfABITy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4622 | llvm::StructType::create("struct._ivar_list_t", IntTy, IntTy, |
| 4623 | llvm::ArrayType::get(IvarnfABITy, 0), NULL); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4624 | |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4625 | IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4626 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4627 | // struct _class_ro_t { |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4628 | // uint32_t const flags; |
| 4629 | // uint32_t const instanceStart; |
| 4630 | // uint32_t const instanceSize; |
| 4631 | // uint32_t const reserved; // only when building for 64bit targets |
| 4632 | // const uint8_t * const ivarLayout; |
| 4633 | // const char *const name; |
| 4634 | // const struct _method_list_t * const baseMethods; |
| 4635 | // const struct _objc_protocol_list *const baseProtocols; |
| 4636 | // const struct _ivar_list_t *const ivars; |
| 4637 | // const uint8_t * const weakIvarLayout; |
| 4638 | // const struct _prop_list_t * const properties; |
| 4639 | // } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4640 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4641 | // FIXME. Add 'reserved' field in 64bit abi mode! |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4642 | ClassRonfABITy = llvm::StructType::create("struct._class_ro_t", |
| 4643 | IntTy, IntTy, IntTy, Int8PtrTy, |
| 4644 | Int8PtrTy, MethodListnfABIPtrTy, |
| 4645 | ProtocolListnfABIPtrTy, |
| 4646 | IvarListnfABIPtrTy, |
| 4647 | Int8PtrTy, PropertyListPtrTy, NULL); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4648 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4649 | // ImpnfABITy - LLVM for id (*)(id, SEL, ...) |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4650 | llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy }; |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 4651 | ImpnfABITy = llvm::FunctionType::get(ObjectPtrTy, params, false) |
| 4652 | ->getPointerTo(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4653 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4654 | // struct _class_t { |
| 4655 | // struct _class_t *isa; |
| 4656 | // struct _class_t * const superclass; |
| 4657 | // void *cache; |
| 4658 | // IMP *vtable; |
| 4659 | // struct class_ro_t *ro; |
| 4660 | // } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4661 | |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4662 | ClassnfABITy = llvm::StructType::create(VMContext, "struct._class_t"); |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4663 | ClassnfABITy->setBody(llvm::PointerType::getUnqual(ClassnfABITy), |
| 4664 | llvm::PointerType::getUnqual(ClassnfABITy), |
| 4665 | CachePtrTy, |
| 4666 | llvm::PointerType::getUnqual(ImpnfABITy), |
| 4667 | llvm::PointerType::getUnqual(ClassRonfABITy), |
| 4668 | NULL); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4669 | |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4670 | // LLVM for struct _class_t * |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4671 | ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4672 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4673 | // struct _category_t { |
| 4674 | // const char * const name; |
| 4675 | // struct _class_t *const cls; |
| 4676 | // const struct _method_list_t * const instance_methods; |
| 4677 | // const struct _method_list_t * const class_methods; |
| 4678 | // const struct _protocol_list_t * const protocols; |
| 4679 | // const struct _prop_list_t * const properties; |
Fariborz Jahanian | 5a63e4c | 2009-01-23 17:41:22 +0000 | [diff] [blame] | 4680 | // } |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4681 | CategorynfABITy = llvm::StructType::create("struct._category_t", |
| 4682 | Int8PtrTy, ClassnfABIPtrTy, |
| 4683 | MethodListnfABIPtrTy, |
| 4684 | MethodListnfABIPtrTy, |
| 4685 | ProtocolListnfABIPtrTy, |
| 4686 | PropertyListPtrTy, |
| 4687 | NULL); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4688 | |
Fariborz Jahanian | 82c72e1 | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 4689 | // New types for nonfragile abi messaging. |
Fariborz Jahanian | e4dc35d | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4690 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 4691 | ASTContext &Ctx = CGM.getContext(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4692 | |
Fariborz Jahanian | 82c72e1 | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 4693 | // MessageRefTy - LLVM for: |
| 4694 | // struct _message_ref_t { |
| 4695 | // IMP messenger; |
| 4696 | // SEL name; |
| 4697 | // }; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4698 | |
Fariborz Jahanian | e4dc35d | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4699 | // First the clang type for struct _message_ref_t |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 4700 | RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct, |
Daniel Dunbar | 0c00537 | 2010-04-29 16:29:11 +0000 | [diff] [blame] | 4701 | Ctx.getTranslationUnitDecl(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4702 | SourceLocation(), SourceLocation(), |
Fariborz Jahanian | e4dc35d | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4703 | &Ctx.Idents.get("_message_ref_t")); |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4704 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0, |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 4705 | Ctx.VoidPtrTy, 0, 0, false, false)); |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4706 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0, |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 4707 | Ctx.getObjCSelType(), 0, 0, false, false)); |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 4708 | RD->completeDefinition(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4709 | |
Fariborz Jahanian | e4dc35d | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4710 | MessageRefCTy = Ctx.getTagDeclType(RD); |
| 4711 | MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy); |
| 4712 | MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy)); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4713 | |
Fariborz Jahanian | 82c72e1 | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 4714 | // MessageRefPtrTy - LLVM for struct _message_ref_t* |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4715 | MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4716 | |
Fariborz Jahanian | 82c72e1 | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 4717 | // SuperMessageRefTy - LLVM for: |
| 4718 | // struct _super_message_ref_t { |
| 4719 | // SUPER_IMP messenger; |
| 4720 | // SEL name; |
| 4721 | // }; |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4722 | SuperMessageRefTy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4723 | llvm::StructType::create("struct._super_message_ref_t", |
| 4724 | ImpnfABITy, SelectorPtrTy, NULL); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4725 | |
Fariborz Jahanian | 82c72e1 | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 4726 | // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t* |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4727 | SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy); |
Fariborz Jahanian | 0a3cfcc | 2011-06-22 20:21:51 +0000 | [diff] [blame] | 4728 | |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 4729 | |
| 4730 | // struct objc_typeinfo { |
| 4731 | // const void** vtable; // objc_ehtype_vtable + 2 |
| 4732 | // const char* name; // c++ typeinfo string |
| 4733 | // Class cls; |
| 4734 | // }; |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4735 | EHTypeTy = |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4736 | llvm::StructType::create("struct._objc_typeinfo", |
| 4737 | llvm::PointerType::getUnqual(Int8PtrTy), |
| 4738 | Int8PtrTy, ClassnfABIPtrTy, NULL); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4739 | EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy); |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 4740 | } |
| 4741 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4742 | llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() { |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4743 | FinishNonFragileABIModule(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4744 | |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4745 | return NULL; |
| 4746 | } |
| 4747 | |
Bill Wendling | cb5b5ff | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 4748 | void CGObjCNonFragileABIMac:: |
| 4749 | AddModuleClassList(ArrayRef<llvm::GlobalValue*> Container, |
| 4750 | const char *SymbolName, |
| 4751 | const char *SectionName) { |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4752 | unsigned NumClasses = Container.size(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4753 | |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4754 | if (!NumClasses) |
| 4755 | return; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4756 | |
Chris Lattner | 3def9ae | 2012-02-06 22:16:34 +0000 | [diff] [blame] | 4757 | SmallVector<llvm::Constant*, 8> Symbols(NumClasses); |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4758 | for (unsigned i=0; i<NumClasses; i++) |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 4759 | Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i], |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4760 | ObjCTypes.Int8PtrTy); |
Chris Lattner | 3def9ae | 2012-02-06 22:16:34 +0000 | [diff] [blame] | 4761 | llvm::Constant *Init = |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4762 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
Chris Lattner | 3def9ae | 2012-02-06 22:16:34 +0000 | [diff] [blame] | 4763 | Symbols.size()), |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4764 | Symbols); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4765 | |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4766 | llvm::GlobalVariable *GV = |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4767 | new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4768 | llvm::GlobalValue::InternalLinkage, |
| 4769 | Init, |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4770 | SymbolName); |
Daniel Dunbar | 710cb20 | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 4771 | GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType())); |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4772 | GV->setSection(SectionName); |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4773 | CGM.AddUsedGlobal(GV); |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4774 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4775 | |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4776 | void CGObjCNonFragileABIMac::FinishNonFragileABIModule() { |
| 4777 | // nonfragile abi has no module definition. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4778 | |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4779 | // Build list of all implemented class addresses in array |
Fariborz Jahanian | 279abd3 | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4780 | // L_OBJC_LABEL_CLASS_$. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4781 | AddModuleClassList(DefinedClasses, |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4782 | "\01L_OBJC_LABEL_CLASS_$", |
| 4783 | "__DATA, __objc_classlist, regular, no_dead_strip"); |
Fariborz Jahanian | 6726055 | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 4784 | |
Bill Wendling | 5313685 | 2012-02-07 09:06:01 +0000 | [diff] [blame] | 4785 | for (unsigned i = 0, e = DefinedClasses.size(); i < e; i++) { |
Fariborz Jahanian | 6726055 | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 4786 | llvm::GlobalValue *IMPLGV = DefinedClasses[i]; |
| 4787 | if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage) |
| 4788 | continue; |
| 4789 | IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage); |
Fariborz Jahanian | 6726055 | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 4790 | } |
| 4791 | |
Bill Wendling | 5313685 | 2012-02-07 09:06:01 +0000 | [diff] [blame] | 4792 | for (unsigned i = 0, e = DefinedMetaClasses.size(); i < e; i++) { |
Fariborz Jahanian | a6227fd | 2009-12-01 18:25:24 +0000 | [diff] [blame] | 4793 | llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i]; |
| 4794 | if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage) |
| 4795 | continue; |
| 4796 | IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 4797 | } |
Fariborz Jahanian | 6726055 | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 4798 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4799 | AddModuleClassList(DefinedNonLazyClasses, |
Daniel Dunbar | 9a017d7 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 4800 | "\01L_OBJC_LABEL_NONLAZY_CLASS_$", |
| 4801 | "__DATA, __objc_nlclslist, regular, no_dead_strip"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4802 | |
Fariborz Jahanian | 279abd3 | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4803 | // Build list of all implemented category addresses in array |
| 4804 | // L_OBJC_LABEL_CATEGORY_$. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4805 | AddModuleClassList(DefinedCategories, |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4806 | "\01L_OBJC_LABEL_CATEGORY_$", |
| 4807 | "__DATA, __objc_catlist, regular, no_dead_strip"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4808 | AddModuleClassList(DefinedNonLazyCategories, |
Daniel Dunbar | 9a017d7 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 4809 | "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$", |
| 4810 | "__DATA, __objc_nlcatlist, regular, no_dead_strip"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4811 | |
Daniel Dunbar | 5e63927 | 2010-04-25 20:39:01 +0000 | [diff] [blame] | 4812 | EmitImageInfo(); |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4813 | } |
| 4814 | |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 4815 | /// isVTableDispatchedSelector - Returns true if SEL is not in the list of |
| 4816 | /// VTableDispatchMethods; false otherwise. What this means is that |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4817 | /// except for the 19 selectors in the list, we generate 32bit-style |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 4818 | /// message dispatch call for all the rest. |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 4819 | bool CGObjCNonFragileABIMac::isVTableDispatchedSelector(Selector Sel) { |
| 4820 | // At various points we've experimented with using vtable-based |
| 4821 | // dispatch for all methods. |
Daniel Dunbar | fca18c1b4 | 2010-04-24 17:56:46 +0000 | [diff] [blame] | 4822 | switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) { |
Daniel Dunbar | fca18c1b4 | 2010-04-24 17:56:46 +0000 | [diff] [blame] | 4823 | case CodeGenOptions::Legacy: |
Fariborz Jahanian | dfb3983 | 2010-04-19 17:53:30 +0000 | [diff] [blame] | 4824 | return false; |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 4825 | case CodeGenOptions::NonLegacy: |
| 4826 | return true; |
Daniel Dunbar | fca18c1b4 | 2010-04-24 17:56:46 +0000 | [diff] [blame] | 4827 | case CodeGenOptions::Mixed: |
| 4828 | break; |
| 4829 | } |
| 4830 | |
| 4831 | // If so, see whether this selector is in the white-list of things which must |
| 4832 | // 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] | 4833 | if (VTableDispatchMethods.empty()) { |
| 4834 | VTableDispatchMethods.insert(GetNullarySelector("alloc")); |
| 4835 | VTableDispatchMethods.insert(GetNullarySelector("class")); |
| 4836 | VTableDispatchMethods.insert(GetNullarySelector("self")); |
| 4837 | VTableDispatchMethods.insert(GetNullarySelector("isFlipped")); |
| 4838 | VTableDispatchMethods.insert(GetNullarySelector("length")); |
| 4839 | VTableDispatchMethods.insert(GetNullarySelector("count")); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4840 | |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 4841 | // These are vtable-based if GC is disabled. |
| 4842 | // Optimistically use vtable dispatch for hybrid compiles. |
Douglas Gregor | 79a9141 | 2011-09-13 17:21:33 +0000 | [diff] [blame] | 4843 | if (CGM.getLangOptions().getGC() != LangOptions::GCOnly) { |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 4844 | VTableDispatchMethods.insert(GetNullarySelector("retain")); |
| 4845 | VTableDispatchMethods.insert(GetNullarySelector("release")); |
| 4846 | VTableDispatchMethods.insert(GetNullarySelector("autorelease")); |
| 4847 | } |
| 4848 | |
| 4849 | VTableDispatchMethods.insert(GetUnarySelector("allocWithZone")); |
| 4850 | VTableDispatchMethods.insert(GetUnarySelector("isKindOfClass")); |
| 4851 | VTableDispatchMethods.insert(GetUnarySelector("respondsToSelector")); |
| 4852 | VTableDispatchMethods.insert(GetUnarySelector("objectForKey")); |
| 4853 | VTableDispatchMethods.insert(GetUnarySelector("objectAtIndex")); |
| 4854 | VTableDispatchMethods.insert(GetUnarySelector("isEqualToString")); |
| 4855 | VTableDispatchMethods.insert(GetUnarySelector("isEqual")); |
| 4856 | |
| 4857 | // These are vtable-based if GC is enabled. |
| 4858 | // Optimistically use vtable dispatch for hybrid compiles. |
Douglas Gregor | 79a9141 | 2011-09-13 17:21:33 +0000 | [diff] [blame] | 4859 | if (CGM.getLangOptions().getGC() != LangOptions::NonGC) { |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 4860 | VTableDispatchMethods.insert(GetNullarySelector("hash")); |
| 4861 | VTableDispatchMethods.insert(GetUnarySelector("addObject")); |
| 4862 | |
| 4863 | // "countByEnumeratingWithState:objects:count" |
| 4864 | IdentifierInfo *KeyIdents[] = { |
| 4865 | &CGM.getContext().Idents.get("countByEnumeratingWithState"), |
| 4866 | &CGM.getContext().Idents.get("objects"), |
| 4867 | &CGM.getContext().Idents.get("count") |
| 4868 | }; |
| 4869 | VTableDispatchMethods.insert( |
| 4870 | CGM.getContext().Selectors.getSelector(3, KeyIdents)); |
| 4871 | } |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 4872 | } |
Daniel Dunbar | fca18c1b4 | 2010-04-24 17:56:46 +0000 | [diff] [blame] | 4873 | |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 4874 | return VTableDispatchMethods.count(Sel); |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 4875 | } |
| 4876 | |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4877 | // Metadata flags |
| 4878 | enum MetaDataDlags { |
| 4879 | CLS = 0x0, |
| 4880 | CLS_META = 0x1, |
| 4881 | CLS_ROOT = 0x2, |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4882 | OBJC2_CLS_HIDDEN = 0x10, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4883 | CLS_EXCEPTION = 0x20, |
| 4884 | |
| 4885 | /// (Obsolete) ARC-specific: this class has a .release_ivars method |
| 4886 | CLS_HAS_IVAR_RELEASER = 0x40, |
| 4887 | /// class was compiled with -fobjc-arr |
| 4888 | CLS_COMPILED_BY_ARC = 0x80 // (1<<7) |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4889 | }; |
| 4890 | /// BuildClassRoTInitializer - generate meta-data for: |
| 4891 | /// struct _class_ro_t { |
| 4892 | /// uint32_t const flags; |
| 4893 | /// uint32_t const instanceStart; |
| 4894 | /// uint32_t const instanceSize; |
| 4895 | /// uint32_t const reserved; // only when building for 64bit targets |
| 4896 | /// const uint8_t * const ivarLayout; |
| 4897 | /// const char *const name; |
| 4898 | /// const struct _method_list_t * const baseMethods; |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4899 | /// const struct _protocol_list_t *const baseProtocols; |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4900 | /// const struct _ivar_list_t *const ivars; |
| 4901 | /// const uint8_t * const weakIvarLayout; |
| 4902 | /// const struct _prop_list_t * const properties; |
| 4903 | /// } |
| 4904 | /// |
| 4905 | llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4906 | unsigned flags, |
| 4907 | unsigned InstanceStart, |
| 4908 | unsigned InstanceSize, |
| 4909 | const ObjCImplementationDecl *ID) { |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4910 | std::string ClassName = ID->getNameAsString(); |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 4911 | llvm::Constant *Values[10]; // 11 for 64bit targets! |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4912 | |
| 4913 | if (CGM.getLangOptions().ObjCAutoRefCount) |
| 4914 | flags |= CLS_COMPILED_BY_ARC; |
| 4915 | |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 4916 | Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags); |
| 4917 | Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart); |
| 4918 | Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize); |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4919 | // FIXME. For 64bit targets add 0 here. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4920 | Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes) |
| 4921 | : BuildIvarLayout(ID, true); |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4922 | Values[ 4] = GetClassName(ID->getIdentifier()); |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4923 | // const struct _method_list_t * const baseMethods; |
| 4924 | std::vector<llvm::Constant*> Methods; |
| 4925 | std::string MethodListName("\01l_OBJC_$_"); |
| 4926 | if (flags & CLS_META) { |
| 4927 | MethodListName += "CLASS_METHODS_" + ID->getNameAsString(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4928 | for (ObjCImplementationDecl::classmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4929 | i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) { |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4930 | // Class methods should always be defined. |
| 4931 | Methods.push_back(GetMethodConstant(*i)); |
| 4932 | } |
| 4933 | } else { |
| 4934 | MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4935 | for (ObjCImplementationDecl::instmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4936 | i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) { |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4937 | // Instance methods should always be defined. |
| 4938 | Methods.push_back(GetMethodConstant(*i)); |
| 4939 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4940 | for (ObjCImplementationDecl::propimpl_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4941 | i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) { |
Fariborz Jahanian | d27a820 | 2009-01-28 22:46:49 +0000 | [diff] [blame] | 4942 | ObjCPropertyImplDecl *PID = *i; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4943 | |
Fariborz Jahanian | d27a820 | 2009-01-28 22:46:49 +0000 | [diff] [blame] | 4944 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){ |
| 4945 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4946 | |
Fariborz Jahanian | d27a820 | 2009-01-28 22:46:49 +0000 | [diff] [blame] | 4947 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) |
| 4948 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 4949 | Methods.push_back(C); |
| 4950 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) |
| 4951 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 4952 | Methods.push_back(C); |
| 4953 | } |
| 4954 | } |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4955 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4956 | Values[ 5] = EmitMethodList(MethodListName, |
| 4957 | "__DATA, __objc_const", Methods); |
| 4958 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4959 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
| 4960 | assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4961 | Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_" |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 4962 | + OID->getName(), |
Ted Kremenek | 0ef508d | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 4963 | OID->all_referenced_protocol_begin(), |
| 4964 | OID->all_referenced_protocol_end()); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4965 | |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4966 | if (flags & CLS_META) |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4967 | Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy); |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4968 | else |
| 4969 | Values[ 7] = EmitIvarList(ID); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4970 | Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes) |
| 4971 | : BuildIvarLayout(ID, false); |
Fariborz Jahanian | 066347e | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 4972 | if (flags & CLS_META) |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4973 | Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
Fariborz Jahanian | 066347e | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 4974 | else |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 4975 | Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(), |
| 4976 | ID, ID->getClassInterface(), ObjCTypes); |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 4977 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy, |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4978 | Values); |
| 4979 | llvm::GlobalVariable *CLASS_RO_GV = |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4980 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false, |
| 4981 | llvm::GlobalValue::InternalLinkage, |
| 4982 | Init, |
| 4983 | (flags & CLS_META) ? |
| 4984 | std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName : |
| 4985 | std::string("\01l_OBJC_CLASS_RO_$_")+ClassName); |
Fariborz Jahanian | c22f236 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4986 | CLASS_RO_GV->setAlignment( |
Daniel Dunbar | 710cb20 | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 4987 | CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassRonfABITy)); |
Fariborz Jahanian | 40a4bcd | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4988 | CLASS_RO_GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4989 | return CLASS_RO_GV; |
Fariborz Jahanian | 2612e14 | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4990 | |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4991 | } |
| 4992 | |
| 4993 | /// BuildClassMetaData - This routine defines that to-level meta-data |
| 4994 | /// for the given ClassName for: |
| 4995 | /// struct _class_t { |
| 4996 | /// struct _class_t *isa; |
| 4997 | /// struct _class_t * const superclass; |
| 4998 | /// void *cache; |
| 4999 | /// IMP *vtable; |
| 5000 | /// struct class_ro_t *ro; |
| 5001 | /// } |
| 5002 | /// |
Fariborz Jahanian | 4723fb7 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5003 | llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5004 | std::string &ClassName, |
| 5005 | llvm::Constant *IsAGV, |
| 5006 | llvm::Constant *SuperClassGV, |
| 5007 | llvm::Constant *ClassRoGV, |
| 5008 | bool HiddenVisibility) { |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 5009 | llvm::Constant *Values[] = { |
| 5010 | IsAGV, |
| 5011 | SuperClassGV, |
| 5012 | ObjCEmptyCacheVar, // &ObjCEmptyCacheVar |
| 5013 | ObjCEmptyVtableVar, // &ObjCEmptyVtableVar |
| 5014 | ClassRoGV // &CLASS_RO_GV |
| 5015 | }; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5016 | if (!Values[1]) |
| 5017 | Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5018 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy, |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5019 | Values); |
Daniel Dunbar | c6928bb | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5020 | llvm::GlobalVariable *GV = GetClassGlobal(ClassName); |
| 5021 | GV->setInitializer(Init); |
Fariborz Jahanian | 0408723 | 2009-01-31 01:07:39 +0000 | [diff] [blame] | 5022 | GV->setSection("__DATA, __objc_data"); |
Fariborz Jahanian | c22f236 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 5023 | GV->setAlignment( |
Daniel Dunbar | 710cb20 | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 5024 | CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassnfABITy)); |
Fariborz Jahanian | 8220825 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 5025 | if (HiddenVisibility) |
| 5026 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Fariborz Jahanian | 4723fb7 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5027 | return GV; |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5028 | } |
| 5029 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5030 | bool |
Fariborz Jahanian | a6bed83 | 2009-05-21 01:03:45 +0000 | [diff] [blame] | 5031 | CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const { |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5032 | return OD->getClassMethod(GetNullarySelector("load")) != 0; |
Daniel Dunbar | 9a017d7 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 5033 | } |
| 5034 | |
Daniel Dunbar | 96120237 | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 5035 | void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID, |
Daniel Dunbar | 554fd79 | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 5036 | uint32_t &InstanceStart, |
| 5037 | uint32_t &InstanceSize) { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5038 | const ASTRecordLayout &RL = |
Daniel Dunbar | 9252ee1 | 2009-05-04 21:26:30 +0000 | [diff] [blame] | 5039 | CGM.getContext().getASTObjCImplementationLayout(OID); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5040 | |
Daniel Dunbar | 9b042e0 | 2009-05-04 23:23:09 +0000 | [diff] [blame] | 5041 | // InstanceSize is really instance end. |
Ken Dyck | d5090c1 | 2011-02-11 02:20:09 +0000 | [diff] [blame] | 5042 | InstanceSize = RL.getDataSize().getQuantity(); |
Daniel Dunbar | 9b042e0 | 2009-05-04 23:23:09 +0000 | [diff] [blame] | 5043 | |
| 5044 | // If there are no fields, the start is the same as the end. |
| 5045 | if (!RL.getFieldCount()) |
| 5046 | InstanceStart = InstanceSize; |
| 5047 | else |
Ken Dyck | c5ca876 | 2011-04-14 00:43:09 +0000 | [diff] [blame] | 5048 | InstanceStart = RL.getFieldOffset(0) / CGM.getContext().getCharWidth(); |
Daniel Dunbar | 554fd79 | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 5049 | } |
| 5050 | |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5051 | void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) { |
| 5052 | std::string ClassName = ID->getNameAsString(); |
| 5053 | if (!ObjCEmptyCacheVar) { |
| 5054 | ObjCEmptyCacheVar = new llvm::GlobalVariable( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5055 | CGM.getModule(), |
| 5056 | ObjCTypes.CacheTy, |
| 5057 | false, |
| 5058 | llvm::GlobalValue::ExternalLinkage, |
| 5059 | 0, |
| 5060 | "_objc_empty_cache"); |
| 5061 | |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5062 | ObjCEmptyVtableVar = new llvm::GlobalVariable( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5063 | CGM.getModule(), |
| 5064 | ObjCTypes.ImpnfABITy, |
| 5065 | false, |
| 5066 | llvm::GlobalValue::ExternalLinkage, |
| 5067 | 0, |
| 5068 | "_objc_empty_vtable"); |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5069 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5070 | assert(ID->getClassInterface() && |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5071 | "CGObjCNonFragileABIMac::GenerateClass - class is 0"); |
Daniel Dunbar | e3f5cfc | 2009-04-20 20:18:54 +0000 | [diff] [blame] | 5072 | // FIXME: Is this correct (that meta class size is never computed)? |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5073 | uint32_t InstanceStart = |
Duncan Sands | c76fe8b | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 5074 | CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy); |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5075 | uint32_t InstanceSize = InstanceStart; |
| 5076 | uint32_t flags = CLS_META; |
Daniel Dunbar | 15894b7 | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5077 | std::string ObjCMetaClassName(getMetaclassSymbolPrefix()); |
| 5078 | std::string ObjCClassName(getClassSymbolPrefix()); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5079 | |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5080 | llvm::GlobalVariable *SuperClassGV, *IsAGV; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5081 | |
| 5082 | bool classIsHidden = |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 5083 | ID->getClassInterface()->getVisibility() == HiddenVisibility; |
Fariborz Jahanian | 8220825 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 5084 | if (classIsHidden) |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5085 | flags |= OBJC2_CLS_HIDDEN; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5086 | if (ID->hasCXXStructors()) |
Fariborz Jahanian | 0dec1e0 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 5087 | flags |= eClassFlags_ABI2_HasCXXStructors; |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5088 | if (!ID->getClassInterface()->getSuperClass()) { |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5089 | // class is root |
| 5090 | flags |= CLS_ROOT; |
Daniel Dunbar | c6928bb | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5091 | SuperClassGV = GetClassGlobal(ObjCClassName + ClassName); |
Fariborz Jahanian | 899e7eb | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5092 | IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName); |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5093 | } else { |
Fariborz Jahanian | 4723fb7 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5094 | // Has a root. Current class is not a root. |
Fariborz Jahanian | 03b300b | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 5095 | const ObjCInterfaceDecl *Root = ID->getClassInterface(); |
| 5096 | while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) |
| 5097 | Root = Super; |
Fariborz Jahanian | 899e7eb | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5098 | IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString()); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5099 | if (Root->isWeakImported()) |
Fariborz Jahanian | a6227fd | 2009-12-01 18:25:24 +0000 | [diff] [blame] | 5100 | IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); |
Fariborz Jahanian | 03b300b | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 5101 | // work on super class metadata symbol. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5102 | std::string SuperClassName = |
Fariborz Jahanian | a6227fd | 2009-12-01 18:25:24 +0000 | [diff] [blame] | 5103 | ObjCMetaClassName + |
| 5104 | ID->getClassInterface()->getSuperClass()->getNameAsString(); |
Fariborz Jahanian | 899e7eb | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5105 | SuperClassGV = GetClassGlobal(SuperClassName); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5106 | if (ID->getClassInterface()->getSuperClass()->isWeakImported()) |
Fariborz Jahanian | 6726055 | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 5107 | SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5108 | } |
| 5109 | llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags, |
| 5110 | InstanceStart, |
| 5111 | InstanceSize,ID); |
Fariborz Jahanian | 4723fb7 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5112 | std::string TClassName = ObjCMetaClassName + ClassName; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5113 | llvm::GlobalVariable *MetaTClass = |
Fariborz Jahanian | 8220825 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 5114 | BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV, |
| 5115 | classIsHidden); |
Fariborz Jahanian | 6726055 | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 5116 | DefinedMetaClasses.push_back(MetaTClass); |
Daniel Dunbar | 15894b7 | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5117 | |
Fariborz Jahanian | 4723fb7 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5118 | // Metadata for the class |
| 5119 | flags = CLS; |
Fariborz Jahanian | 8220825 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 5120 | if (classIsHidden) |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5121 | flags |= OBJC2_CLS_HIDDEN; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5122 | if (ID->hasCXXStructors()) |
Fariborz Jahanian | 0dec1e0 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 5123 | flags |= eClassFlags_ABI2_HasCXXStructors; |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5124 | |
Douglas Gregor | 78bd61f | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 5125 | if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface())) |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5126 | flags |= CLS_EXCEPTION; |
| 5127 | |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5128 | if (!ID->getClassInterface()->getSuperClass()) { |
Fariborz Jahanian | 4723fb7 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5129 | flags |= CLS_ROOT; |
| 5130 | SuperClassGV = 0; |
Chris Lattner | b433b27 | 2009-04-19 06:02:28 +0000 | [diff] [blame] | 5131 | } else { |
Fariborz Jahanian | 4723fb7 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5132 | // Has a root. Current class is not a root. |
Fariborz Jahanian | 03b300b | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 5133 | std::string RootClassName = |
Fariborz Jahanian | 4723fb7 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5134 | ID->getClassInterface()->getSuperClass()->getNameAsString(); |
Daniel Dunbar | c6928bb | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5135 | SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5136 | if (ID->getClassInterface()->getSuperClass()->isWeakImported()) |
Fariborz Jahanian | 6726055 | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 5137 | SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); |
Fariborz Jahanian | 4723fb7 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5138 | } |
Daniel Dunbar | 96120237 | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 5139 | GetClassSizeInfo(ID, InstanceStart, InstanceSize); |
Fariborz Jahanian | 4723fb7 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5140 | CLASS_RO_GV = BuildClassRoTInitializer(flags, |
Fariborz Jahanian | a887e63 | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 5141 | InstanceStart, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5142 | InstanceSize, |
Fariborz Jahanian | a887e63 | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 5143 | ID); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5144 | |
Fariborz Jahanian | 4723fb7 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5145 | TClassName = ObjCClassName + ClassName; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5146 | llvm::GlobalVariable *ClassMD = |
Fariborz Jahanian | 8220825 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 5147 | BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV, |
| 5148 | classIsHidden); |
Fariborz Jahanian | 279abd3 | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 5149 | DefinedClasses.push_back(ClassMD); |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5150 | |
Daniel Dunbar | 9a017d7 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 5151 | // Determine if this class is also "non-lazy". |
| 5152 | if (ImplementationIsNonLazy(ID)) |
| 5153 | DefinedNonLazyClasses.push_back(ClassMD); |
| 5154 | |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5155 | // Force the definition of the EHType if necessary. |
| 5156 | if (flags & CLS_EXCEPTION) |
| 5157 | GetInterfaceEHType(ID->getClassInterface(), true); |
Fariborz Jahanian | c057794 | 2011-04-22 22:02:28 +0000 | [diff] [blame] | 5158 | // Make sure method definition entries are all clear for next implementation. |
| 5159 | MethodDefinitions.clear(); |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5160 | } |
| 5161 | |
Fariborz Jahanian | 097feda | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5162 | /// GenerateProtocolRef - This routine is called to generate code for |
| 5163 | /// a protocol reference expression; as in: |
| 5164 | /// @code |
| 5165 | /// @protocol(Proto1); |
| 5166 | /// @endcode |
| 5167 | /// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1 |
| 5168 | /// which will hold address of the protocol meta-data. |
| 5169 | /// |
| 5170 | llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5171 | const ObjCProtocolDecl *PD) { |
| 5172 | |
Fariborz Jahanian | 464423d | 2009-04-10 18:47:34 +0000 | [diff] [blame] | 5173 | // This routine is called for @protocol only. So, we must build definition |
| 5174 | // of protocol's meta-data (not a reference to it!) |
| 5175 | // |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5176 | llvm::Constant *Init = |
| 5177 | llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD), |
Douglas Gregor | 020de32 | 2012-01-17 18:36:30 +0000 | [diff] [blame] | 5178 | ObjCTypes.getExternalProtocolPtrTy()); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5179 | |
Fariborz Jahanian | 097feda | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5180 | std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_"); |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 5181 | ProtocolName += PD->getName(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5182 | |
Fariborz Jahanian | 097feda | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5183 | llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName); |
| 5184 | if (PTGV) |
Benjamin Kramer | 76399eb | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 5185 | return Builder.CreateLoad(PTGV); |
Fariborz Jahanian | 097feda | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5186 | PTGV = new llvm::GlobalVariable( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5187 | CGM.getModule(), |
| 5188 | Init->getType(), false, |
| 5189 | llvm::GlobalValue::WeakAnyLinkage, |
| 5190 | Init, |
| 5191 | ProtocolName); |
Fariborz Jahanian | 097feda | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5192 | PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip"); |
| 5193 | PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5194 | CGM.AddUsedGlobal(PTGV); |
Benjamin Kramer | 76399eb | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 5195 | return Builder.CreateLoad(PTGV); |
Fariborz Jahanian | 097feda | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5196 | } |
| 5197 | |
Fariborz Jahanian | 0c8d060 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5198 | /// GenerateCategory - Build metadata for a category implementation. |
| 5199 | /// struct _category_t { |
| 5200 | /// const char * const name; |
| 5201 | /// struct _class_t *const cls; |
| 5202 | /// const struct _method_list_t * const instance_methods; |
| 5203 | /// const struct _method_list_t * const class_methods; |
| 5204 | /// const struct _protocol_list_t * const protocols; |
| 5205 | /// const struct _prop_list_t * const properties; |
| 5206 | /// } |
| 5207 | /// |
Daniel Dunbar | 9a017d7 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 5208 | void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) { |
Fariborz Jahanian | 0c8d060 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5209 | const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); |
Fariborz Jahanian | 2612e14 | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5210 | const char *Prefix = "\01l_OBJC_$_CATEGORY_"; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5211 | std::string ExtCatName(Prefix + Interface->getNameAsString()+ |
| 5212 | "_$_" + OCD->getNameAsString()); |
| 5213 | std::string ExtClassName(getClassSymbolPrefix() + |
Daniel Dunbar | 15894b7 | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5214 | Interface->getNameAsString()); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5215 | |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 5216 | llvm::Constant *Values[6]; |
Fariborz Jahanian | 0c8d060 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5217 | Values[0] = GetClassName(OCD->getIdentifier()); |
| 5218 | // meta-class entry symbol |
Daniel Dunbar | c6928bb | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5219 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5220 | if (Interface->isWeakImported()) |
Fariborz Jahanian | 3ad8dcf | 2009-11-17 22:02:21 +0000 | [diff] [blame] | 5221 | ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); |
| 5222 | |
Fariborz Jahanian | 0c8d060 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5223 | Values[1] = ClassGV; |
Fariborz Jahanian | 2612e14 | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5224 | std::vector<llvm::Constant*> Methods; |
| 5225 | std::string MethodListName(Prefix); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5226 | MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() + |
Fariborz Jahanian | 2612e14 | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5227 | "_$_" + OCD->getNameAsString(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5228 | |
| 5229 | for (ObjCCategoryImplDecl::instmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5230 | i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) { |
Fariborz Jahanian | 2612e14 | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5231 | // Instance methods should always be defined. |
| 5232 | Methods.push_back(GetMethodConstant(*i)); |
| 5233 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5234 | |
| 5235 | Values[2] = EmitMethodList(MethodListName, |
| 5236 | "__DATA, __objc_const", |
Fariborz Jahanian | 2612e14 | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5237 | Methods); |
| 5238 | |
| 5239 | MethodListName = Prefix; |
| 5240 | MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" + |
| 5241 | OCD->getNameAsString(); |
| 5242 | Methods.clear(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5243 | for (ObjCCategoryImplDecl::classmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5244 | i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) { |
Fariborz Jahanian | 2612e14 | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5245 | // Class methods should always be defined. |
| 5246 | Methods.push_back(GetMethodConstant(*i)); |
| 5247 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5248 | |
| 5249 | Values[3] = EmitMethodList(MethodListName, |
| 5250 | "__DATA, __objc_const", |
Fariborz Jahanian | 2612e14 | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5251 | Methods); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5252 | const ObjCCategoryDecl *Category = |
Fariborz Jahanian | 066347e | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 5253 | Interface->FindCategoryDeclaration(OCD->getIdentifier()); |
Fariborz Jahanian | d8fc105 | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 5254 | if (Category) { |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 5255 | SmallString<256> ExtName; |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5256 | llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_" |
| 5257 | << OCD->getName(); |
Fariborz Jahanian | d8fc105 | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 5258 | Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_" |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5259 | + Interface->getName() + "_$_" |
| 5260 | + Category->getName(), |
Fariborz Jahanian | d8fc105 | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 5261 | Category->protocol_begin(), |
| 5262 | Category->protocol_end()); |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5263 | Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(), |
| 5264 | OCD, Category, ObjCTypes); |
Mike Stump | 658fe02 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 5265 | } else { |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5266 | Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy); |
| 5267 | Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
Fariborz Jahanian | d8fc105 | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 5268 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5269 | |
| 5270 | llvm::Constant *Init = |
| 5271 | llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy, |
Fariborz Jahanian | 0c8d060 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5272 | Values); |
| 5273 | llvm::GlobalVariable *GCATV |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5274 | = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy, |
Fariborz Jahanian | 0c8d060 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5275 | false, |
| 5276 | llvm::GlobalValue::InternalLinkage, |
| 5277 | Init, |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5278 | ExtCatName); |
Fariborz Jahanian | c22f236 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 5279 | GCATV->setAlignment( |
Daniel Dunbar | 710cb20 | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 5280 | CGM.getTargetData().getABITypeAlignment(ObjCTypes.CategorynfABITy)); |
Fariborz Jahanian | 40a4bcd | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 5281 | GCATV->setSection("__DATA, __objc_const"); |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5282 | CGM.AddUsedGlobal(GCATV); |
Fariborz Jahanian | 0c8d060 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5283 | DefinedCategories.push_back(GCATV); |
Daniel Dunbar | 9a017d7 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 5284 | |
| 5285 | // Determine if this category is also "non-lazy". |
| 5286 | if (ImplementationIsNonLazy(OCD)) |
| 5287 | DefinedNonLazyCategories.push_back(GCATV); |
Fariborz Jahanian | c057794 | 2011-04-22 22:02:28 +0000 | [diff] [blame] | 5288 | // method definition entries must be clear for next implementation. |
| 5289 | MethodDefinitions.clear(); |
Fariborz Jahanian | 0c8d060 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5290 | } |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5291 | |
| 5292 | /// GetMethodConstant - Return a struct objc_method constant for the |
| 5293 | /// given method if it has been defined. The result is null if the |
| 5294 | /// method has not been defined. The return value has type MethodPtrTy. |
| 5295 | llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5296 | const ObjCMethodDecl *MD) { |
Argyrios Kyrtzidis | 13257c5 | 2010-08-09 10:54:20 +0000 | [diff] [blame] | 5297 | llvm::Function *Fn = GetMethodDefinition(MD); |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5298 | if (!Fn) |
| 5299 | return 0; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5300 | |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 5301 | llvm::Constant *Method[] = { |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 5302 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 5303 | ObjCTypes.SelectorPtrTy), |
| 5304 | GetMethodVarType(MD), |
| 5305 | llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy) |
| 5306 | }; |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 5307 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method); |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5308 | } |
| 5309 | |
| 5310 | /// EmitMethodList - Build meta-data for method declarations |
| 5311 | /// struct _method_list_t { |
| 5312 | /// uint32_t entsize; // sizeof(struct _objc_method) |
| 5313 | /// uint32_t method_count; |
| 5314 | /// struct _objc_method method_list[method_count]; |
| 5315 | /// } |
| 5316 | /// |
Bill Wendling | cb5b5ff | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 5317 | llvm::Constant * |
| 5318 | CGObjCNonFragileABIMac::EmitMethodList(Twine Name, |
| 5319 | const char *Section, |
| 5320 | ArrayRef<llvm::Constant*> Methods) { |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5321 | // Return null for empty list. |
| 5322 | if (Methods.empty()) |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5323 | return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5324 | |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 5325 | llvm::Constant *Values[3]; |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5326 | // sizeof(struct _objc_method) |
Duncan Sands | c76fe8b | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 5327 | unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy); |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 5328 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5329 | // method_count |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 5330 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5331 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy, |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5332 | Methods.size()); |
Owen Anderson | 47034e1 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 5333 | Values[2] = llvm::ConstantArray::get(AT, Methods); |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 5334 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5335 | |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5336 | llvm::GlobalVariable *GV = |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5337 | new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 5338 | llvm::GlobalValue::InternalLinkage, Init, Name); |
| 5339 | GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType())); |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5340 | GV->setSection(Section); |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5341 | CGM.AddUsedGlobal(GV); |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 5342 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListnfABIPtrTy); |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5343 | } |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5344 | |
Fariborz Jahanian | 4e7ae06 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 5345 | /// ObjCIvarOffsetVariable - Returns the ivar offset variable for |
| 5346 | /// the given ivar. |
Daniel Dunbar | 8c7f981 | 2010-04-02 21:14:02 +0000 | [diff] [blame] | 5347 | llvm::GlobalVariable * |
| 5348 | CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID, |
| 5349 | const ObjCIvarDecl *Ivar) { |
| 5350 | const ObjCInterfaceDecl *Container = Ivar->getContainingInterface(); |
Daniel Dunbar | 3f86b9c | 2009-05-05 00:36:57 +0000 | [diff] [blame] | 5351 | std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() + |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 5352 | '.' + Ivar->getNameAsString(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5353 | llvm::GlobalVariable *IvarOffsetGV = |
Fariborz Jahanian | 4e7ae06 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 5354 | CGM.getModule().getGlobalVariable(Name); |
| 5355 | if (!IvarOffsetGV) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5356 | IvarOffsetGV = |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5357 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy, |
Fariborz Jahanian | 4e7ae06 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 5358 | false, |
| 5359 | llvm::GlobalValue::ExternalLinkage, |
| 5360 | 0, |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5361 | Name); |
Fariborz Jahanian | 4e7ae06 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 5362 | return IvarOffsetGV; |
| 5363 | } |
| 5364 | |
Daniel Dunbar | 8c7f981 | 2010-04-02 21:14:02 +0000 | [diff] [blame] | 5365 | llvm::Constant * |
| 5366 | CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID, |
| 5367 | const ObjCIvarDecl *Ivar, |
| 5368 | unsigned long int Offset) { |
Daniel Dunbar | bf90b33 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 5369 | llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5370 | IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy, |
Daniel Dunbar | bf90b33 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 5371 | Offset)); |
Fariborz Jahanian | c22f236 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 5372 | IvarOffsetGV->setAlignment( |
Daniel Dunbar | 710cb20 | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 5373 | CGM.getTargetData().getABITypeAlignment(ObjCTypes.LongTy)); |
Daniel Dunbar | bf90b33 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 5374 | |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 5375 | // FIXME: This matches gcc, but shouldn't the visibility be set on the use as |
| 5376 | // well (i.e., in ObjCIvarOffsetVariable). |
Daniel Dunbar | bf90b33 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 5377 | if (Ivar->getAccessControl() == ObjCIvarDecl::Private || |
| 5378 | Ivar->getAccessControl() == ObjCIvarDecl::Package || |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 5379 | ID->getVisibility() == HiddenVisibility) |
Fariborz Jahanian | 3d3426f | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 5380 | IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | f5f359f | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 5381 | else |
Fariborz Jahanian | bc3c77b | 2009-04-06 18:30:00 +0000 | [diff] [blame] | 5382 | IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility); |
Bill Wendling | f7d4598 | 2011-05-04 21:37:25 +0000 | [diff] [blame] | 5383 | IvarOffsetGV->setSection("__DATA, __objc_ivar"); |
Fariborz Jahanian | c88a70d | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 5384 | return IvarOffsetGV; |
Fariborz Jahanian | 40a4bcd | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 5385 | } |
| 5386 | |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5387 | /// EmitIvarList - Emit the ivar list for the given |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5388 | /// implementation. The return value has type |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5389 | /// IvarListnfABIPtrTy. |
| 5390 | /// struct _ivar_t { |
| 5391 | /// unsigned long int *offset; // pointer to ivar offset location |
| 5392 | /// char *name; |
| 5393 | /// char *type; |
| 5394 | /// uint32_t alignment; |
| 5395 | /// uint32_t size; |
| 5396 | /// } |
| 5397 | /// struct _ivar_list_t { |
| 5398 | /// uint32 entsize; // sizeof(struct _ivar_t) |
| 5399 | /// uint32 count; |
| 5400 | /// struct _iver_t list[count]; |
| 5401 | /// } |
| 5402 | /// |
Daniel Dunbar | f5c1846 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 5403 | |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5404 | llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5405 | const ObjCImplementationDecl *ID) { |
| 5406 | |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 5407 | std::vector<llvm::Constant*> Ivars; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5408 | |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 5409 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5410 | assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5411 | |
Fariborz Jahanian | 40a4bcd | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 5412 | // FIXME. Consolidate this with similar code in GenerateClass. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5413 | |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 5414 | for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin(); |
Fariborz Jahanian | b26d578 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 5415 | IVD; IVD = IVD->getNextIvar()) { |
Fariborz Jahanian | 7c80959 | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 5416 | // Ignore unnamed bit-fields. |
| 5417 | if (!IVD->getDeclName()) |
| 5418 | continue; |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 5419 | llvm::Constant *Ivar[5]; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5420 | Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD, |
Daniel Dunbar | 96120237 | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 5421 | ComputeIvarBaseOffset(CGM, ID, IVD)); |
Daniel Dunbar | 725dc2c | 2009-04-22 08:22:17 +0000 | [diff] [blame] | 5422 | Ivar[1] = GetMethodVarName(IVD->getIdentifier()); |
| 5423 | Ivar[2] = GetMethodVarType(IVD); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 5424 | llvm::Type *FieldTy = |
Daniel Dunbar | 725dc2c | 2009-04-22 08:22:17 +0000 | [diff] [blame] | 5425 | CGM.getTypes().ConvertTypeForMem(IVD->getType()); |
Duncan Sands | c76fe8b | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 5426 | unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy); |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5427 | unsigned Align = CGM.getContext().getPreferredTypeAlign( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5428 | IVD->getType().getTypePtr()) >> 3; |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5429 | Align = llvm::Log2_32(Align); |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 5430 | Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align); |
Daniel Dunbar | ae03226 | 2009-04-20 00:33:43 +0000 | [diff] [blame] | 5431 | // NOTE. Size of a bitfield does not match gcc's, because of the |
| 5432 | // way bitfields are treated special in each. But I am told that |
| 5433 | // 'size' for bitfield ivars is ignored by the runtime so it does |
| 5434 | // not matter. If it matters, there is enough info to get the |
| 5435 | // bitfield right! |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 5436 | Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 5437 | Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar)); |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5438 | } |
| 5439 | // Return null for empty list. |
| 5440 | if (Ivars.empty()) |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5441 | return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy); |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 5442 | |
| 5443 | llvm::Constant *Values[3]; |
Duncan Sands | c76fe8b | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 5444 | unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy); |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 5445 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 5446 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5447 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy, |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5448 | Ivars.size()); |
Owen Anderson | 47034e1 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 5449 | Values[2] = llvm::ConstantArray::get(AT, Ivars); |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 5450 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5451 | const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_"; |
| 5452 | llvm::GlobalVariable *GV = |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5453 | new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5454 | llvm::GlobalValue::InternalLinkage, |
| 5455 | Init, |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5456 | Prefix + OID->getName()); |
Fariborz Jahanian | c22f236 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 5457 | GV->setAlignment( |
Daniel Dunbar | 710cb20 | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 5458 | CGM.getTargetData().getABITypeAlignment(Init->getType())); |
Fariborz Jahanian | 40a4bcd | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 5459 | GV->setSection("__DATA, __objc_const"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5460 | |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5461 | CGM.AddUsedGlobal(GV); |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 5462 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy); |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5463 | } |
| 5464 | |
| 5465 | llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5466 | const ObjCProtocolDecl *PD) { |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5467 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5468 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5469 | if (!Entry) { |
| 5470 | // We use the initializer as a marker of whether this is a forward |
| 5471 | // reference or not. At module finalization we add the empty |
| 5472 | // contents for protocols which were referenced but never defined. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5473 | Entry = |
| 5474 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false, |
| 5475 | llvm::GlobalValue::ExternalLinkage, |
| 5476 | 0, |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5477 | "\01l_OBJC_PROTOCOL_$_" + PD->getName()); |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5478 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5479 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5480 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5481 | return Entry; |
| 5482 | } |
| 5483 | |
| 5484 | /// GetOrEmitProtocol - Generate the protocol meta-data: |
| 5485 | /// @code |
| 5486 | /// struct _protocol_t { |
| 5487 | /// id isa; // NULL |
| 5488 | /// const char * const protocol_name; |
| 5489 | /// const struct _protocol_list_t * protocol_list; // super protocols |
| 5490 | /// const struct method_list_t * const instance_methods; |
| 5491 | /// const struct method_list_t * const class_methods; |
| 5492 | /// const struct method_list_t *optionalInstanceMethods; |
| 5493 | /// const struct method_list_t *optionalClassMethods; |
| 5494 | /// const struct _prop_list_t * properties; |
| 5495 | /// const uint32_t size; // sizeof(struct _protocol_t) |
| 5496 | /// const uint32_t flags; // = 0 |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5497 | /// const char ** extendedMethodTypes; |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5498 | /// } |
| 5499 | /// @endcode |
| 5500 | /// |
| 5501 | |
| 5502 | llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5503 | const ObjCProtocolDecl *PD) { |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5504 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5505 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5506 | // Early exit if a defining object has already been generated. |
| 5507 | if (Entry && Entry->hasInitializer()) |
| 5508 | return Entry; |
| 5509 | |
Douglas Gregor | a715bff | 2012-01-01 19:51:50 +0000 | [diff] [blame] | 5510 | // Use the protocol definition, if there is one. |
| 5511 | if (const ObjCProtocolDecl *Def = PD->getDefinition()) |
| 5512 | PD = Def; |
| 5513 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5514 | // Construct method lists. |
| 5515 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 5516 | std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5517 | std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5518 | for (ObjCProtocolDecl::instmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5519 | i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) { |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5520 | ObjCMethodDecl *MD = *i; |
Fariborz Jahanian | d9c28b8 | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 5521 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 5522 | if (!C) |
| 5523 | return GetOrEmitProtocolRef(PD); |
| 5524 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5525 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 5526 | OptInstanceMethods.push_back(C); |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5527 | OptMethodTypesExt.push_back(GetMethodVarType(MD, true)); |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5528 | } else { |
| 5529 | InstanceMethods.push_back(C); |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5530 | MethodTypesExt.push_back(GetMethodVarType(MD, true)); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5531 | } |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5532 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5533 | |
| 5534 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5535 | i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) { |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5536 | ObjCMethodDecl *MD = *i; |
Fariborz Jahanian | d9c28b8 | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 5537 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 5538 | if (!C) |
| 5539 | return GetOrEmitProtocolRef(PD); |
| 5540 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5541 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 5542 | OptClassMethods.push_back(C); |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5543 | OptMethodTypesExt.push_back(GetMethodVarType(MD, true)); |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5544 | } else { |
| 5545 | ClassMethods.push_back(C); |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5546 | MethodTypesExt.push_back(GetMethodVarType(MD, true)); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5547 | } |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5548 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5549 | |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5550 | MethodTypesExt.insert(MethodTypesExt.end(), |
| 5551 | OptMethodTypesExt.begin(), OptMethodTypesExt.end()); |
| 5552 | |
| 5553 | llvm::Constant *Values[11]; |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5554 | // isa is NULL |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5555 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy); |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5556 | Values[1] = GetClassName(PD->getIdentifier()); |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5557 | Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(), |
| 5558 | PD->protocol_begin(), |
| 5559 | PD->protocol_end()); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5560 | |
Fariborz Jahanian | d9c28b8 | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 5561 | Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_" |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5562 | + PD->getName(), |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5563 | "__DATA, __objc_const", |
| 5564 | InstanceMethods); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5565 | Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_" |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5566 | + PD->getName(), |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5567 | "__DATA, __objc_const", |
| 5568 | ClassMethods); |
Fariborz Jahanian | d9c28b8 | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 5569 | Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_" |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5570 | + PD->getName(), |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5571 | "__DATA, __objc_const", |
| 5572 | OptInstanceMethods); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5573 | Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_" |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5574 | + PD->getName(), |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5575 | "__DATA, __objc_const", |
| 5576 | OptClassMethods); |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5577 | Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(), |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5578 | 0, PD, ObjCTypes); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5579 | uint32_t Size = |
Duncan Sands | c76fe8b | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 5580 | CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy); |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 5581 | Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5582 | Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy); |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5583 | Values[10] = EmitProtocolMethodTypes("\01l_OBJC_$_PROTOCOL_METHOD_TYPES_" |
| 5584 | + PD->getName(), |
| 5585 | MethodTypesExt, ObjCTypes); |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 5586 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy, |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5587 | Values); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5588 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5589 | if (Entry) { |
| 5590 | // Already created, fix the linkage and update the initializer. |
Mike Stump | a6ca334 | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 5591 | Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage); |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5592 | Entry->setInitializer(Init); |
| 5593 | } else { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5594 | Entry = |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5595 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, |
| 5596 | false, llvm::GlobalValue::WeakAnyLinkage, Init, |
| 5597 | "\01l_OBJC_PROTOCOL_$_" + PD->getName()); |
Fariborz Jahanian | c22f236 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 5598 | Entry->setAlignment( |
Daniel Dunbar | 710cb20 | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 5599 | CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABITy)); |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5600 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5601 | } |
Fariborz Jahanian | 61cd4b5 | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 5602 | Entry->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5603 | CGM.AddUsedGlobal(Entry); |
| 5604 | |
Fariborz Jahanian | 61cd4b5 | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 5605 | // Use this protocol meta-data to build protocol list table in section |
| 5606 | // __DATA, __objc_protolist |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5607 | llvm::GlobalVariable *PTGV = |
| 5608 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy, |
| 5609 | false, llvm::GlobalValue::WeakAnyLinkage, Entry, |
| 5610 | "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName()); |
Fariborz Jahanian | c22f236 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 5611 | PTGV->setAlignment( |
Daniel Dunbar | 710cb20 | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 5612 | CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy)); |
Daniel Dunbar | b25452a | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 5613 | PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip"); |
Fariborz Jahanian | 61cd4b5 | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 5614 | PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5615 | CGM.AddUsedGlobal(PTGV); |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5616 | return Entry; |
| 5617 | } |
| 5618 | |
| 5619 | /// EmitProtocolList - Generate protocol list meta-data: |
| 5620 | /// @code |
| 5621 | /// struct _protocol_list_t { |
| 5622 | /// long protocol_count; // Note, this is 32/64 bit |
| 5623 | /// struct _protocol_t[protocol_count]; |
| 5624 | /// } |
| 5625 | /// @endcode |
| 5626 | /// |
| 5627 | llvm::Constant * |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5628 | CGObjCNonFragileABIMac::EmitProtocolList(Twine Name, |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5629 | ObjCProtocolDecl::protocol_iterator begin, |
| 5630 | ObjCProtocolDecl::protocol_iterator end) { |
Bill Wendling | a515b58 | 2012-02-09 22:16:49 +0000 | [diff] [blame] | 5631 | llvm::SmallVector<llvm::Constant*, 16> ProtocolRefs; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5632 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5633 | // Just return null for empty protocol lists |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5634 | if (begin == end) |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5635 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5636 | |
Daniel Dunbar | 8de90f0 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 5637 | // FIXME: We shouldn't need to do this lookup here, should we? |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 5638 | SmallString<256> TmpName; |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5639 | Name.toVector(TmpName); |
| 5640 | llvm::GlobalVariable *GV = |
| 5641 | CGM.getModule().getGlobalVariable(TmpName.str(), true); |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5642 | if (GV) |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5643 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5644 | |
Daniel Dunbar | 8de90f0 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 5645 | for (; begin != end; ++begin) |
| 5646 | ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented??? |
| 5647 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5648 | // This list is null terminated. |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5649 | ProtocolRefs.push_back(llvm::Constant::getNullValue( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5650 | ObjCTypes.ProtocolnfABIPtrTy)); |
| 5651 | |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 5652 | llvm::Constant *Values[2]; |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 5653 | Values[0] = |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 5654 | llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5655 | Values[1] = |
Bill Wendling | a515b58 | 2012-02-09 22:16:49 +0000 | [diff] [blame] | 5656 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy, |
| 5657 | ProtocolRefs.size()), |
| 5658 | ProtocolRefs); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5659 | |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 5660 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5661 | GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5662 | llvm::GlobalValue::InternalLinkage, |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 5663 | Init, Name); |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5664 | GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | c22f236 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 5665 | GV->setAlignment( |
Daniel Dunbar | 710cb20 | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 5666 | CGM.getTargetData().getABITypeAlignment(Init->getType())); |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5667 | CGM.AddUsedGlobal(GV); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5668 | return llvm::ConstantExpr::getBitCast(GV, |
Daniel Dunbar | 8de90f0 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 5669 | ObjCTypes.ProtocolListnfABIPtrTy); |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5670 | } |
| 5671 | |
Fariborz Jahanian | d9c28b8 | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 5672 | /// GetMethodDescriptionConstant - This routine build following meta-data: |
| 5673 | /// struct _objc_method { |
| 5674 | /// SEL _cmd; |
| 5675 | /// char *method_type; |
| 5676 | /// char *_imp; |
| 5677 | /// } |
| 5678 | |
| 5679 | llvm::Constant * |
| 5680 | CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 5681 | llvm::Constant *Desc[3]; |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 5682 | Desc[0] = |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5683 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 5684 | ObjCTypes.SelectorPtrTy); |
Fariborz Jahanian | d9c28b8 | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 5685 | Desc[1] = GetMethodVarType(MD); |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 5686 | if (!Desc[1]) |
| 5687 | return 0; |
| 5688 | |
Fariborz Jahanian | 097feda | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5689 | // Protocol methods have no implementation. So, this entry is always NULL. |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5690 | Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 5691 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc); |
Fariborz Jahanian | d9c28b8 | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 5692 | } |
Fariborz Jahanian | c88a70d | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 5693 | |
| 5694 | /// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference. |
| 5695 | /// This code gen. amounts to generating code for: |
| 5696 | /// @code |
| 5697 | /// (type *)((char *)base + _OBJC_IVAR_$_.ivar; |
| 5698 | /// @encode |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5699 | /// |
Fariborz Jahanian | 712bfa6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 5700 | LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar( |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 5701 | CodeGen::CodeGenFunction &CGF, |
| 5702 | QualType ObjectTy, |
| 5703 | llvm::Value *BaseValue, |
| 5704 | const ObjCIvarDecl *Ivar, |
| 5705 | unsigned CVRQualifiers) { |
| 5706 | ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface(); |
Fariborz Jahanian | caabf1b | 2012-02-20 22:42:22 +0000 | [diff] [blame^] | 5707 | llvm::Value *Offset = EmitIvarOffset(CGF, ID, Ivar); |
| 5708 | if (llvm::LoadInst *LI = dyn_cast<llvm::LoadInst>(Offset)) |
| 5709 | LI->setMetadata(CGM.getModule().getMDKindID("invariant.load"), |
| 5710 | llvm::MDNode::get(VMContext, |
| 5711 | ArrayRef<llvm::Value*>())); |
Daniel Dunbar | 9fd114d | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 5712 | return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers, |
Fariborz Jahanian | caabf1b | 2012-02-20 22:42:22 +0000 | [diff] [blame^] | 5713 | Offset); |
Fariborz Jahanian | c88a70d | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 5714 | } |
| 5715 | |
Fariborz Jahanian | 21fc74c | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 5716 | llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5717 | CodeGen::CodeGenFunction &CGF, |
| 5718 | const ObjCInterfaceDecl *Interface, |
| 5719 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | c76493a | 2009-11-29 21:23:36 +0000 | [diff] [blame] | 5720 | return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar"); |
Fariborz Jahanian | 21fc74c | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 5721 | } |
| 5722 | |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5723 | static void appendSelectorForMessageRefTable(std::string &buffer, |
| 5724 | Selector selector) { |
| 5725 | if (selector.isUnarySelector()) { |
| 5726 | buffer += selector.getNameForSlot(0); |
| 5727 | return; |
| 5728 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5729 | |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5730 | for (unsigned i = 0, e = selector.getNumArgs(); i != e; ++i) { |
| 5731 | buffer += selector.getNameForSlot(i); |
| 5732 | buffer += '_'; |
| 5733 | } |
| 5734 | } |
| 5735 | |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5736 | /// Emit a "v-table" message send. We emit a weak hidden-visibility |
| 5737 | /// struct, initially containing the selector pointer and a pointer to |
| 5738 | /// a "fixup" variant of the appropriate objc_msgSend. To call, we |
| 5739 | /// load and call the function pointer, passing the address of the |
| 5740 | /// struct as the second parameter. The runtime determines whether |
| 5741 | /// the selector is currently emitted using vtable dispatch; if so, it |
| 5742 | /// substitutes a stub function which simply tail-calls through the |
| 5743 | /// appropriate vtable slot, and if not, it substitues a stub function |
| 5744 | /// which tail-calls objc_msgSend. Both stubs adjust the selector |
| 5745 | /// argument to correctly point to the selector. |
| 5746 | RValue |
| 5747 | CGObjCNonFragileABIMac::EmitVTableMessageSend(CodeGenFunction &CGF, |
| 5748 | ReturnValueSlot returnSlot, |
| 5749 | QualType resultType, |
| 5750 | Selector selector, |
| 5751 | llvm::Value *arg0, |
| 5752 | QualType arg0Type, |
| 5753 | bool isSuper, |
| 5754 | const CallArgList &formalArgs, |
| 5755 | const ObjCMethodDecl *method) { |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5756 | // Compute the actual arguments. |
| 5757 | CallArgList args; |
| 5758 | |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5759 | // First argument: the receiver / super-call structure. |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5760 | if (!isSuper) |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5761 | arg0 = CGF.Builder.CreateBitCast(arg0, ObjCTypes.ObjectPtrTy); |
| 5762 | args.add(RValue::get(arg0), arg0Type); |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5763 | |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5764 | // Second argument: a pointer to the message ref structure. Leave |
| 5765 | // the actual argument value blank for now. |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5766 | args.add(RValue::get(0), ObjCTypes.MessageRefCPtrTy); |
| 5767 | |
| 5768 | args.insert(args.end(), formalArgs.begin(), formalArgs.end()); |
| 5769 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 5770 | MessageSendInfo MSI = getMessageSendInfo(method, resultType, args); |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5771 | |
John McCall | 5880fb8 | 2011-05-14 21:12:11 +0000 | [diff] [blame] | 5772 | NullReturnState nullReturn; |
| 5773 | |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5774 | // Find the function to call and the mangled name for the message |
| 5775 | // ref structure. Using a different mangled name wouldn't actually |
| 5776 | // be a problem; it would just be a waste. |
| 5777 | // |
| 5778 | // The runtime currently never uses vtable dispatch for anything |
| 5779 | // except normal, non-super message-sends. |
| 5780 | // FIXME: don't use this for that. |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5781 | llvm::Constant *fn = 0; |
| 5782 | std::string messageRefName("\01l_"); |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 5783 | if (CGM.ReturnTypeUsesSRet(MSI.CallInfo)) { |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5784 | if (isSuper) { |
| 5785 | fn = ObjCTypes.getMessageSendSuper2StretFixupFn(); |
| 5786 | messageRefName += "objc_msgSendSuper2_stret_fixup"; |
Chris Lattner | 396639d | 2010-08-18 16:09:06 +0000 | [diff] [blame] | 5787 | } else { |
John McCall | 5880fb8 | 2011-05-14 21:12:11 +0000 | [diff] [blame] | 5788 | nullReturn.init(CGF, arg0); |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5789 | fn = ObjCTypes.getMessageSendStretFixupFn(); |
| 5790 | messageRefName += "objc_msgSend_stret_fixup"; |
Chris Lattner | 396639d | 2010-08-18 16:09:06 +0000 | [diff] [blame] | 5791 | } |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5792 | } else if (!isSuper && CGM.ReturnTypeUsesFPRet(resultType)) { |
| 5793 | fn = ObjCTypes.getMessageSendFpretFixupFn(); |
| 5794 | messageRefName += "objc_msgSend_fpret_fixup"; |
Mike Stump | 658fe02 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 5795 | } else { |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5796 | if (isSuper) { |
| 5797 | fn = ObjCTypes.getMessageSendSuper2FixupFn(); |
| 5798 | messageRefName += "objc_msgSendSuper2_fixup"; |
Chris Lattner | 396639d | 2010-08-18 16:09:06 +0000 | [diff] [blame] | 5799 | } else { |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5800 | fn = ObjCTypes.getMessageSendFixupFn(); |
| 5801 | messageRefName += "objc_msgSend_fixup"; |
Chris Lattner | 396639d | 2010-08-18 16:09:06 +0000 | [diff] [blame] | 5802 | } |
Fariborz Jahanian | e4dc35d | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5803 | } |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5804 | assert(fn && "CGObjCNonFragileABIMac::EmitMessageSend"); |
| 5805 | messageRefName += '_'; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5806 | |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5807 | // Append the selector name, except use underscores anywhere we |
| 5808 | // would have used colons. |
| 5809 | appendSelectorForMessageRefTable(messageRefName, selector); |
| 5810 | |
| 5811 | llvm::GlobalVariable *messageRef |
| 5812 | = CGM.getModule().getGlobalVariable(messageRefName); |
| 5813 | if (!messageRef) { |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5814 | // Build the message ref structure. |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5815 | llvm::Constant *values[] = { fn, GetMethodVarName(selector) }; |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 5816 | llvm::Constant *init = llvm::ConstantStruct::getAnon(values); |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5817 | messageRef = new llvm::GlobalVariable(CGM.getModule(), |
| 5818 | init->getType(), |
| 5819 | /*constant*/ false, |
| 5820 | llvm::GlobalValue::WeakAnyLinkage, |
| 5821 | init, |
| 5822 | messageRefName); |
| 5823 | messageRef->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 5824 | messageRef->setAlignment(16); |
| 5825 | messageRef->setSection("__DATA, __objc_msgrefs, coalesced"); |
| 5826 | } |
Fariborz Jahanian | c93fa98 | 2012-01-30 23:39:30 +0000 | [diff] [blame] | 5827 | |
| 5828 | bool requiresnullCheck = false; |
| 5829 | if (CGM.getLangOptions().ObjCAutoRefCount && method) |
| 5830 | for (ObjCMethodDecl::param_const_iterator i = method->param_begin(), |
| 5831 | e = method->param_end(); i != e; ++i) { |
| 5832 | const ParmVarDecl *ParamDecl = (*i); |
| 5833 | if (ParamDecl->hasAttr<NSConsumedAttr>()) { |
| 5834 | if (!nullReturn.NullBB) |
| 5835 | nullReturn.init(CGF, arg0); |
| 5836 | requiresnullCheck = true; |
| 5837 | break; |
| 5838 | } |
| 5839 | } |
| 5840 | |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5841 | llvm::Value *mref = |
| 5842 | CGF.Builder.CreateBitCast(messageRef, ObjCTypes.MessageRefPtrTy); |
| 5843 | |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5844 | // Update the message ref argument. |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5845 | args[1].RV = RValue::get(mref); |
| 5846 | |
| 5847 | // Load the function to call from the message ref table. |
| 5848 | llvm::Value *callee = CGF.Builder.CreateStructGEP(mref, 0); |
| 5849 | callee = CGF.Builder.CreateLoad(callee, "msgSend_fn"); |
| 5850 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 5851 | callee = CGF.Builder.CreateBitCast(callee, MSI.MessengerType); |
John McCall | 234eac8 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5852 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 5853 | RValue result = CGF.EmitCall(MSI.CallInfo, callee, returnSlot, args); |
Fariborz Jahanian | c93fa98 | 2012-01-30 23:39:30 +0000 | [diff] [blame] | 5854 | return nullReturn.complete(CGF, result, resultType, formalArgs, |
| 5855 | requiresnullCheck ? method : 0); |
Fariborz Jahanian | 3d9296e | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5856 | } |
| 5857 | |
| 5858 | /// Generate code for a message send expression in the nonfragile abi. |
Daniel Dunbar | aff9fca | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 5859 | CodeGen::RValue |
| 5860 | CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
John McCall | 78a1511 | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 5861 | ReturnValueSlot Return, |
Daniel Dunbar | aff9fca | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 5862 | QualType ResultType, |
| 5863 | Selector Sel, |
| 5864 | llvm::Value *Receiver, |
Daniel Dunbar | aff9fca | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 5865 | const CallArgList &CallArgs, |
David Chisnall | 01aa467 | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 5866 | const ObjCInterfaceDecl *Class, |
Daniel Dunbar | aff9fca | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 5867 | const ObjCMethodDecl *Method) { |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5868 | return isVTableDispatchedSelector(Sel) |
| 5869 | ? EmitVTableMessageSend(CGF, Return, ResultType, Sel, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5870 | Receiver, CGF.getContext().getObjCIdType(), |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5871 | false, CallArgs, Method) |
| 5872 | : EmitMessageSend(CGF, Return, ResultType, |
| 5873 | EmitSelector(CGF.Builder, Sel), |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5874 | Receiver, CGF.getContext().getObjCIdType(), |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5875 | false, CallArgs, Method, ObjCTypes); |
Fariborz Jahanian | 3d9296e | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5876 | } |
| 5877 | |
Daniel Dunbar | c6928bb | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5878 | llvm::GlobalVariable * |
Fariborz Jahanian | 899e7eb | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5879 | CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) { |
Daniel Dunbar | c6928bb | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5880 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 5881 | |
Daniel Dunbar | a646834 | 2009-03-02 05:18:14 +0000 | [diff] [blame] | 5882 | if (!GV) { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5883 | GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy, |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5884 | false, llvm::GlobalValue::ExternalLinkage, |
| 5885 | 0, Name); |
Daniel Dunbar | c6928bb | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5886 | } |
| 5887 | |
| 5888 | return GV; |
| 5889 | } |
| 5890 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5891 | llvm::Value *CGObjCNonFragileABIMac::EmitClassRefFromId(CGBuilderTy &Builder, |
| 5892 | IdentifierInfo *II) { |
| 5893 | llvm::GlobalVariable *&Entry = ClassReferences[II]; |
| 5894 | |
Fariborz Jahanian | 33f66e6 | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5895 | if (!Entry) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5896 | std::string ClassName(getClassSymbolPrefix() + II->getName().str()); |
Daniel Dunbar | c6928bb | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5897 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5898 | Entry = |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5899 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, |
| 5900 | false, llvm::GlobalValue::InternalLinkage, |
| 5901 | ClassGV, |
| 5902 | "\01L_OBJC_CLASSLIST_REFERENCES_$_"); |
Fariborz Jahanian | 33f66e6 | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5903 | Entry->setAlignment( |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5904 | CGM.getTargetData().getABITypeAlignment( |
| 5905 | ObjCTypes.ClassnfABIPtrTy)); |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5906 | Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip"); |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5907 | CGM.AddUsedGlobal(Entry); |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5908 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5909 | |
Benjamin Kramer | 76399eb | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 5910 | return Builder.CreateLoad(Entry); |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5911 | } |
Fariborz Jahanian | 33f66e6 | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5912 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5913 | llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder, |
| 5914 | const ObjCInterfaceDecl *ID) { |
| 5915 | return EmitClassRefFromId(Builder, ID->getIdentifier()); |
| 5916 | } |
| 5917 | |
| 5918 | llvm::Value *CGObjCNonFragileABIMac::EmitNSAutoreleasePoolClassRef( |
| 5919 | CGBuilderTy &Builder) { |
| 5920 | IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool"); |
| 5921 | return EmitClassRefFromId(Builder, II); |
| 5922 | } |
| 5923 | |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5924 | llvm::Value * |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5925 | CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5926 | const ObjCInterfaceDecl *ID) { |
| 5927 | llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()]; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5928 | |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5929 | if (!Entry) { |
| 5930 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
| 5931 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5932 | Entry = |
| 5933 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5934 | false, llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5935 | ClassGV, |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5936 | "\01L_OBJC_CLASSLIST_SUP_REFS_$_"); |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5937 | Entry->setAlignment( |
Daniel Dunbar | 710cb20 | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 5938 | CGM.getTargetData().getABITypeAlignment( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5939 | ObjCTypes.ClassnfABIPtrTy)); |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5940 | Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip"); |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5941 | CGM.AddUsedGlobal(Entry); |
Fariborz Jahanian | 33f66e6 | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5942 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5943 | |
Benjamin Kramer | 76399eb | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 5944 | return Builder.CreateLoad(Entry); |
Fariborz Jahanian | 33f66e6 | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5945 | } |
| 5946 | |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5947 | /// EmitMetaClassRef - Return a Value * of the address of _class_t |
| 5948 | /// meta-data |
| 5949 | /// |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5950 | llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder, |
| 5951 | const ObjCInterfaceDecl *ID) { |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5952 | llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()]; |
| 5953 | if (Entry) |
Benjamin Kramer | 76399eb | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 5954 | return Builder.CreateLoad(Entry); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5955 | |
Daniel Dunbar | 15894b7 | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5956 | std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString()); |
Fariborz Jahanian | 899e7eb | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5957 | llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5958 | Entry = |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5959 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false, |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5960 | llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5961 | MetaClassGV, |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5962 | "\01L_OBJC_CLASSLIST_SUP_REFS_$_"); |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5963 | Entry->setAlignment( |
Daniel Dunbar | 710cb20 | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 5964 | CGM.getTargetData().getABITypeAlignment( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5965 | ObjCTypes.ClassnfABIPtrTy)); |
| 5966 | |
Daniel Dunbar | e60aa05 | 2009-04-15 19:03:14 +0000 | [diff] [blame] | 5967 | Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip"); |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5968 | CGM.AddUsedGlobal(Entry); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5969 | |
Benjamin Kramer | 76399eb | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 5970 | return Builder.CreateLoad(Entry); |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5971 | } |
| 5972 | |
Fariborz Jahanian | 33f66e6 | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5973 | /// GetClass - Return a reference to the class for the given interface |
| 5974 | /// decl. |
| 5975 | llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder, |
| 5976 | const ObjCInterfaceDecl *ID) { |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5977 | if (ID->isWeakImported()) { |
Fariborz Jahanian | 95ace55 | 2009-11-17 22:42:00 +0000 | [diff] [blame] | 5978 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
| 5979 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName); |
| 5980 | ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); |
| 5981 | } |
| 5982 | |
Fariborz Jahanian | 33f66e6 | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5983 | return EmitClassRef(Builder, ID); |
| 5984 | } |
| 5985 | |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5986 | /// Generates a message send where the super is the receiver. This is |
| 5987 | /// a message send to self with special delivery semantics indicating |
| 5988 | /// which class's method should be called. |
| 5989 | CodeGen::RValue |
| 5990 | CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
John McCall | 78a1511 | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 5991 | ReturnValueSlot Return, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5992 | QualType ResultType, |
| 5993 | Selector Sel, |
| 5994 | const ObjCInterfaceDecl *Class, |
| 5995 | bool isCategoryImpl, |
| 5996 | llvm::Value *Receiver, |
| 5997 | bool IsClassMessage, |
Daniel Dunbar | aff9fca | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 5998 | const CodeGen::CallArgList &CallArgs, |
| 5999 | const ObjCMethodDecl *Method) { |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 6000 | // ... |
| 6001 | // Create and init a super structure; this is a (receiver, class) |
| 6002 | // pair we will pass to objc_msgSendSuper. |
| 6003 | llvm::Value *ObjCSuper = |
John McCall | 6647584 | 2011-03-04 08:00:29 +0000 | [diff] [blame] | 6004 | CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6005 | |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 6006 | llvm::Value *ReceiverAsObject = |
| 6007 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); |
| 6008 | CGF.Builder.CreateStore(ReceiverAsObject, |
| 6009 | CGF.Builder.CreateStructGEP(ObjCSuper, 0)); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6010 | |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 6011 | // 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] | 6012 | llvm::Value *Target; |
| 6013 | if (IsClassMessage) { |
| 6014 | if (isCategoryImpl) { |
| 6015 | // Message sent to "super' in a class method defined in |
| 6016 | // a category implementation. |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 6017 | Target = EmitClassRef(CGF.Builder, Class); |
Fariborz Jahanian | bac73ac | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 6018 | Target = CGF.Builder.CreateStructGEP(Target, 0); |
| 6019 | Target = CGF.Builder.CreateLoad(Target); |
Mike Stump | 658fe02 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 6020 | } else |
Fariborz Jahanian | bac73ac | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 6021 | Target = EmitMetaClassRef(CGF.Builder, Class); |
Mike Stump | 658fe02 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 6022 | } else |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 6023 | Target = EmitSuperClassRef(CGF.Builder, Class); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6024 | |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 6025 | // FIXME: We shouldn't need to do this cast, rectify the ASTContext and |
| 6026 | // ObjCTypes types. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 6027 | llvm::Type *ClassTy = |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 6028 | CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); |
| 6029 | Target = CGF.Builder.CreateBitCast(Target, ClassTy); |
| 6030 | CGF.Builder.CreateStore(Target, |
| 6031 | CGF.Builder.CreateStructGEP(ObjCSuper, 1)); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6032 | |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 6033 | return (isVTableDispatchedSelector(Sel)) |
| 6034 | ? EmitVTableMessageSend(CGF, Return, ResultType, Sel, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6035 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 6036 | true, CallArgs, Method) |
| 6037 | : EmitMessageSend(CGF, Return, ResultType, |
| 6038 | EmitSelector(CGF.Builder, Sel), |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6039 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
John McCall | 9e8bb00 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 6040 | true, CallArgs, Method, ObjCTypes); |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 6041 | } |
Fariborz Jahanian | 74b7722 | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 6042 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6043 | llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder, |
Fariborz Jahanian | 9240f3d | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 6044 | Selector Sel, bool lval) { |
Fariborz Jahanian | 74b7722 | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 6045 | llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6046 | |
Fariborz Jahanian | 74b7722 | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 6047 | if (!Entry) { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6048 | llvm::Constant *Casted = |
| 6049 | llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), |
| 6050 | ObjCTypes.SelectorPtrTy); |
| 6051 | Entry = |
| 6052 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false, |
| 6053 | llvm::GlobalValue::InternalLinkage, |
| 6054 | Casted, "\01L_OBJC_SELECTOR_REFERENCES_"); |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 6055 | Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip"); |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 6056 | CGM.AddUsedGlobal(Entry); |
Fariborz Jahanian | 74b7722 | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 6057 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6058 | |
Fariborz Jahanian | 9240f3d | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 6059 | if (lval) |
| 6060 | return Entry; |
Pete Cooper | 9d60551 | 2011-11-10 21:45:06 +0000 | [diff] [blame] | 6061 | llvm::LoadInst* LI = Builder.CreateLoad(Entry); |
| 6062 | |
| 6063 | LI->setMetadata(CGM.getModule().getMDKindID("invariant.load"), |
| 6064 | llvm::MDNode::get(VMContext, |
| 6065 | ArrayRef<llvm::Value*>())); |
| 6066 | return LI; |
Fariborz Jahanian | 74b7722 | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 6067 | } |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6068 | /// EmitObjCIvarAssign - Code gen for assigning to a __strong object. |
Fariborz Jahanian | 7a95d72 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 6069 | /// objc_assign_ivar (id src, id *dst, ptrdiff_t) |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6070 | /// |
| 6071 | void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6072 | llvm::Value *src, |
Fariborz Jahanian | 7a95d72 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 6073 | llvm::Value *dst, |
| 6074 | llvm::Value *ivarOffset) { |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 6075 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 6076 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | c76fe8b | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 6077 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 6078 | assert(Size <= 8 && "does not support size > 8"); |
| 6079 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 6080 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 1b074a3 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 6081 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 6082 | } |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6083 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 6084 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Fariborz Jahanian | 7a95d72 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 6085 | CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(), |
| 6086 | src, dst, ivarOffset); |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6087 | return; |
| 6088 | } |
| 6089 | |
| 6090 | /// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object. |
| 6091 | /// objc_assign_strongCast (id src, id *dst) |
| 6092 | /// |
| 6093 | void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6094 | CodeGen::CodeGenFunction &CGF, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6095 | llvm::Value *src, llvm::Value *dst) { |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 6096 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 6097 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | c76fe8b | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 6098 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 6099 | assert(Size <= 8 && "does not support size > 8"); |
| 6100 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6101 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 1b074a3 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 6102 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 6103 | } |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6104 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 6105 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 0a696a42 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 6106 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(), |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6107 | src, dst, "weakassign"); |
| 6108 | return; |
| 6109 | } |
| 6110 | |
Fariborz Jahanian | 5f21d2f | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 6111 | void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6112 | CodeGen::CodeGenFunction &CGF, |
| 6113 | llvm::Value *DestPtr, |
| 6114 | llvm::Value *SrcPtr, |
Fariborz Jahanian | 021510e | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 6115 | llvm::Value *Size) { |
Fariborz Jahanian | 5f21d2f | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 6116 | SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy); |
| 6117 | DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy); |
Fariborz Jahanian | 5f21d2f | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 6118 | CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(), |
Fariborz Jahanian | 021510e | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 6119 | DestPtr, SrcPtr, Size); |
Fariborz Jahanian | 5f21d2f | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 6120 | return; |
| 6121 | } |
| 6122 | |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6123 | /// EmitObjCWeakRead - Code gen for loading value of a __weak |
| 6124 | /// object: objc_read_weak (id *src) |
| 6125 | /// |
| 6126 | llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6127 | CodeGen::CodeGenFunction &CGF, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6128 | llvm::Value *AddrWeakObj) { |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 6129 | llvm::Type* DestTy = |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6130 | cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType(); |
| 6131 | AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 6132 | llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(), |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6133 | AddrWeakObj, "weakread"); |
Eli Friedman | a374b68 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 6134 | read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy); |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6135 | return read_weak; |
| 6136 | } |
| 6137 | |
| 6138 | /// EmitObjCWeakAssign - Code gen for assigning to a __weak object. |
| 6139 | /// objc_assign_weak (id src, id *dst) |
| 6140 | /// |
| 6141 | void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6142 | llvm::Value *src, llvm::Value *dst) { |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 6143 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 6144 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | c76fe8b | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 6145 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 6146 | assert(Size <= 8 && "does not support size > 8"); |
| 6147 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 6148 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 1b074a3 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 6149 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 6150 | } |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6151 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 6152 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 6fdd57c | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 6153 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(), |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6154 | src, dst, "weakassign"); |
| 6155 | return; |
| 6156 | } |
| 6157 | |
| 6158 | /// EmitObjCGlobalAssign - Code gen for assigning to a __strong object. |
| 6159 | /// objc_assign_global (id src, id *dst) |
| 6160 | /// |
| 6161 | void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 217af24 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 6162 | llvm::Value *src, llvm::Value *dst, |
| 6163 | bool threadlocal) { |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 6164 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 6165 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | c76fe8b | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 6166 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 6167 | assert(Size <= 8 && "does not support size > 8"); |
| 6168 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 6169 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 1b074a3 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 6170 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 6171 | } |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6172 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 6173 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Fariborz Jahanian | 217af24 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 6174 | if (!threadlocal) |
| 6175 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(), |
| 6176 | src, dst, "globalassign"); |
| 6177 | else |
| 6178 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(), |
| 6179 | src, dst, "threadlocalassign"); |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6180 | return; |
| 6181 | } |
Fariborz Jahanian | 74b7722 | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 6182 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6183 | void |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 6184 | CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 6185 | const ObjCAtSynchronizedStmt &S) { |
David Chisnall | 3e57560 | 2011-03-25 17:46:35 +0000 | [diff] [blame] | 6186 | EmitAtSynchronizedStmt(CGF, S, |
| 6187 | cast<llvm::Function>(ObjCTypes.getSyncEnterFn()), |
| 6188 | cast<llvm::Function>(ObjCTypes.getSyncExitFn())); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 6189 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6190 | |
John McCall | 2ca705e | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 6191 | llvm::Constant * |
Fariborz Jahanian | 831f0fc | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 6192 | CGObjCNonFragileABIMac::GetEHType(QualType T) { |
John McCall | 2ca705e | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 6193 | // There's a particular fixed type info for 'id'. |
| 6194 | if (T->isObjCIdType() || |
| 6195 | T->isObjCQualifiedIdType()) { |
| 6196 | llvm::Constant *IDEHType = |
| 6197 | CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id"); |
| 6198 | if (!IDEHType) |
| 6199 | IDEHType = |
| 6200 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, |
| 6201 | false, |
| 6202 | llvm::GlobalValue::ExternalLinkage, |
| 6203 | 0, "OBJC_EHTYPE_id"); |
| 6204 | return IDEHType; |
| 6205 | } |
| 6206 | |
| 6207 | // All other types should be Objective-C interface pointer types. |
| 6208 | const ObjCObjectPointerType *PT = |
| 6209 | T->getAs<ObjCObjectPointerType>(); |
| 6210 | assert(PT && "Invalid @catch type."); |
| 6211 | const ObjCInterfaceType *IT = PT->getInterfaceType(); |
| 6212 | assert(IT && "Invalid @catch type."); |
| 6213 | return GetInterfaceEHType(IT->getDecl(), false); |
| 6214 | } |
| 6215 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 6216 | void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF, |
| 6217 | const ObjCAtTryStmt &S) { |
David Chisnall | 3e57560 | 2011-03-25 17:46:35 +0000 | [diff] [blame] | 6218 | EmitTryCatchStmt(CGF, S, |
| 6219 | cast<llvm::Function>(ObjCTypes.getObjCBeginCatchFn()), |
| 6220 | cast<llvm::Function>(ObjCTypes.getObjCEndCatchFn()), |
| 6221 | cast<llvm::Function>(ObjCTypes.getExceptionRethrowFn())); |
Daniel Dunbar | 0b0dcd9 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 6222 | } |
| 6223 | |
Anders Carlsson | 9ab53d1 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 6224 | /// EmitThrowStmt - Generate code for a throw statement. |
| 6225 | void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
| 6226 | const ObjCAtThrowStmt &S) { |
Anders Carlsson | 9ab53d1 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 6227 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
John McCall | 248512a | 2011-10-01 10:32:24 +0000 | [diff] [blame] | 6228 | llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr); |
Benjamin Kramer | 76399eb | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 6229 | Exception = CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy); |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 6230 | CGF.EmitCallOrInvoke(ObjCTypes.getExceptionThrowFn(), Exception) |
John McCall | 17afe45 | 2010-10-16 08:21:07 +0000 | [diff] [blame] | 6231 | .setDoesNotReturn(); |
Anders Carlsson | 9ab53d1 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 6232 | } else { |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 6233 | CGF.EmitCallOrInvoke(ObjCTypes.getExceptionRethrowFn()) |
John McCall | 17afe45 | 2010-10-16 08:21:07 +0000 | [diff] [blame] | 6234 | .setDoesNotReturn(); |
Anders Carlsson | 9ab53d1 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 6235 | } |
| 6236 | |
John McCall | 17afe45 | 2010-10-16 08:21:07 +0000 | [diff] [blame] | 6237 | CGF.Builder.CreateUnreachable(); |
Anders Carlsson | 9ab53d1 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 6238 | CGF.Builder.ClearInsertionPoint(); |
| 6239 | } |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6240 | |
John McCall | 2ca705e | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 6241 | llvm::Constant * |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6242 | CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID, |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6243 | bool ForDefinition) { |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6244 | llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()]; |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6245 | |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6246 | // If we don't need a definition, return the entry if found or check |
| 6247 | // if we use an external reference. |
| 6248 | if (!ForDefinition) { |
| 6249 | if (Entry) |
| 6250 | return Entry; |
Daniel Dunbar | d7beeea | 2009-04-07 06:43:45 +0000 | [diff] [blame] | 6251 | |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6252 | // If this type (or a super class) has the __objc_exception__ |
| 6253 | // attribute, emit an external reference. |
Douglas Gregor | 78bd61f | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 6254 | if (hasObjCExceptionAttribute(CGM.getContext(), ID)) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6255 | return Entry = |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 6256 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false, |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6257 | llvm::GlobalValue::ExternalLinkage, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6258 | 0, |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 6259 | ("OBJC_EHTYPE_$_" + |
Daniel Dunbar | 07d0785 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 6260 | ID->getIdentifier()->getName())); |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6261 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6262 | |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6263 | // Otherwise we need to either make a new entry or fill in the |
| 6264 | // initializer. |
| 6265 | assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition"); |
Daniel Dunbar | 15894b7 | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 6266 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6267 | std::string VTableName = "objc_ehtype_vtable"; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6268 | llvm::GlobalVariable *VTableGV = |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6269 | CGM.getModule().getGlobalVariable(VTableName); |
| 6270 | if (!VTableGV) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6271 | VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy, |
| 6272 | false, |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6273 | llvm::GlobalValue::ExternalLinkage, |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 6274 | 0, VTableName); |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6275 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 6276 | llvm::Value *VTableIdx = llvm::ConstantInt::get(CGM.Int32Ty, 2); |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6277 | |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 6278 | llvm::Constant *Values[] = { |
| 6279 | llvm::ConstantExpr::getGetElementPtr(VTableGV, VTableIdx), |
| 6280 | GetClassName(ID->getIdentifier()), |
| 6281 | GetClassGlobal(ClassName) |
| 6282 | }; |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 6283 | llvm::Constant *Init = |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 6284 | llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values); |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6285 | |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6286 | if (Entry) { |
| 6287 | Entry->setInitializer(Init); |
| 6288 | } else { |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 6289 | Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false, |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6290 | llvm::GlobalValue::WeakAnyLinkage, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6291 | Init, |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 6292 | ("OBJC_EHTYPE_$_" + |
Daniel Dunbar | 07d0785 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 6293 | ID->getIdentifier()->getName())); |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6294 | } |
| 6295 | |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 6296 | if (CGM.getLangOptions().getVisibilityMode() == HiddenVisibility) |
Daniel Dunbar | 15894b7 | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 6297 | Entry->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | 710cb20 | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 6298 | Entry->setAlignment(CGM.getTargetData().getABITypeAlignment( |
| 6299 | ObjCTypes.EHTypeTy)); |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6300 | |
| 6301 | if (ForDefinition) { |
| 6302 | Entry->setSection("__DATA,__objc_const"); |
| 6303 | Entry->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 6304 | } else { |
| 6305 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
| 6306 | } |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6307 | |
| 6308 | return Entry; |
| 6309 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6310 | |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 6311 | /* *** */ |
| 6312 | |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 6313 | CodeGen::CGObjCRuntime * |
| 6314 | CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) { |
David Chisnall | 067f0ed | 2011-03-22 21:21:24 +0000 | [diff] [blame] | 6315 | if (CGM.getLangOptions().ObjCNonFragileABI) |
| 6316 | return new CGObjCNonFragileABIMac(CGM); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 6317 | return new CGObjCMac(CGM); |
| 6318 | } |